[SCSI] libfc: combine name server registration request functions
[linux-2.6-block.git] / drivers / scsi / libfc / fc_lport.c
CommitLineData
42e9a92f
RL
1/*
2 * Copyright(c) 2007 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
16 *
17 * Maintained at www.Open-FCoE.org
18 */
19
20/*
21 * PORT LOCKING NOTES
22 *
23 * These comments only apply to the 'port code' which consists of the lport,
24 * disc and rport blocks.
25 *
26 * MOTIVATION
27 *
28 * The lport, disc and rport blocks all have mutexes that are used to protect
29 * those objects. The main motivation for these locks is to prevent from
30 * having an lport reset just before we send a frame. In that scenario the
31 * lport's FID would get set to zero and then we'd send a frame with an
32 * invalid SID. We also need to ensure that states don't change unexpectedly
33 * while processing another state.
34 *
35 * HEIRARCHY
36 *
37 * The following heirarchy defines the locking rules. A greater lock
38 * may be held before acquiring a lesser lock, but a lesser lock should never
39 * be held while attempting to acquire a greater lock. Here is the heirarchy-
40 *
41 * lport > disc, lport > rport, disc > rport
42 *
43 * CALLBACKS
44 *
45 * The callbacks cause complications with this scheme. There is a callback
46 * from the rport (to either lport or disc) and a callback from disc
47 * (to the lport).
48 *
49 * As rports exit the rport state machine a callback is made to the owner of
50 * the rport to notify success or failure. Since the callback is likely to
51 * cause the lport or disc to grab its lock we cannot hold the rport lock
52 * while making the callback. To ensure that the rport is not free'd while
53 * processing the callback the rport callbacks are serialized through a
54 * single-threaded workqueue. An rport would never be free'd while in a
55 * callback handler becuase no other rport work in this queue can be executed
56 * at the same time.
57 *
58 * When discovery succeeds or fails a callback is made to the lport as
59 * notification. Currently, succesful discovery causes the lport to take no
60 * action. A failure will cause the lport to reset. There is likely a circular
61 * locking problem with this implementation.
62 */
63
64/*
65 * LPORT LOCKING
66 *
67 * The critical sections protected by the lport's mutex are quite broad and
68 * may be improved upon in the future. The lport code and its locking doesn't
69 * influence the I/O path, so excessive locking doesn't penalize I/O
70 * performance.
71 *
72 * The strategy is to lock whenever processing a request or response. Note
73 * that every _enter_* function corresponds to a state change. They generally
74 * change the lports state and then send a request out on the wire. We lock
75 * before calling any of these functions to protect that state change. This
76 * means that the entry points into the lport block manage the locks while
77 * the state machine can transition between states (i.e. _enter_* functions)
78 * while always staying protected.
79 *
80 * When handling responses we also hold the lport mutex broadly. When the
81 * lport receives the response frame it locks the mutex and then calls the
82 * appropriate handler for the particuar response. Generally a response will
83 * trigger a state change and so the lock must already be held.
84 *
85 * Retries also have to consider the locking. The retries occur from a work
86 * context and the work function will lock the lport and then retry the state
87 * (i.e. _enter_* function).
88 */
89
90#include <linux/timer.h>
91#include <asm/unaligned.h>
92
93#include <scsi/fc/fc_gs.h>
94
95#include <scsi/libfc.h>
96#include <scsi/fc_encode.h>
97
8866a5d9
RL
98#include "fc_libfc.h"
99
42e9a92f
RL
100/* Fabric IDs to use for point-to-point mode, chosen on whims. */
101#define FC_LOCAL_PTP_FID_LO 0x010101
102#define FC_LOCAL_PTP_FID_HI 0x010102
103
104#define DNS_DELAY 3 /* Discovery delay after RSCN (in seconds)*/
105
42e9a92f
RL
106static void fc_lport_error(struct fc_lport *, struct fc_frame *);
107
108static void fc_lport_enter_reset(struct fc_lport *);
109static void fc_lport_enter_flogi(struct fc_lport *);
110static void fc_lport_enter_dns(struct fc_lport *);
c914f7d1 111static void fc_lport_enter_ns(struct fc_lport *, enum fc_lport_state);
42e9a92f
RL
112static void fc_lport_enter_scr(struct fc_lport *);
113static void fc_lport_enter_ready(struct fc_lport *);
114static void fc_lport_enter_logo(struct fc_lport *);
115
116static const char *fc_lport_state_names[] = {
b1d9fd55 117 [LPORT_ST_DISABLED] = "disabled",
42e9a92f
RL
118 [LPORT_ST_FLOGI] = "FLOGI",
119 [LPORT_ST_DNS] = "dNS",
c9c7bd7a 120 [LPORT_ST_RNN_ID] = "RNN_ID",
5baa17c3 121 [LPORT_ST_RSNN_NN] = "RSNN_NN",
c9866a54 122 [LPORT_ST_RSPN_ID] = "RSPN_ID",
42e9a92f
RL
123 [LPORT_ST_RFT_ID] = "RFT_ID",
124 [LPORT_ST_SCR] = "SCR",
125 [LPORT_ST_READY] = "Ready",
126 [LPORT_ST_LOGO] = "LOGO",
127 [LPORT_ST_RESET] = "reset",
128};
129
130static int fc_frame_drop(struct fc_lport *lport, struct fc_frame *fp)
131{
132 fc_frame_free(fp);
133 return 0;
134}
135
136/**
34f42a07 137 * fc_lport_rport_callback() - Event handler for rport events
42e9a92f 138 * @lport: The lport which is receiving the event
9fb9d328 139 * @rdata: private remote port data
42e9a92f
RL
140 * @event: The event that occured
141 *
142 * Locking Note: The rport lock should not be held when calling
143 * this function.
144 */
145static void fc_lport_rport_callback(struct fc_lport *lport,
9fb9d328 146 struct fc_rport_priv *rdata,
42e9a92f
RL
147 enum fc_rport_event event)
148{
7414705e 149 FC_LPORT_DBG(lport, "Received a %d event for port (%6x)\n", event,
f211fa51 150 rdata->ids.port_id);
42e9a92f 151
b5cbf083 152 mutex_lock(&lport->lp_mutex);
42e9a92f 153 switch (event) {
4c0f62b5 154 case RPORT_EV_READY:
b5cbf083
JE
155 if (lport->state == LPORT_ST_DNS) {
156 lport->dns_rp = rdata;
c914f7d1 157 fc_lport_enter_ns(lport, LPORT_ST_RNN_ID);
b5cbf083
JE
158 } else {
159 FC_LPORT_DBG(lport, "Received an READY event "
160 "on port (%6x) for the directory "
161 "server, but the lport is not "
162 "in the DNS state, it's in the "
163 "%d state", rdata->ids.port_id,
164 lport->state);
165 lport->tt.rport_logoff(rdata);
166 }
42e9a92f
RL
167 break;
168 case RPORT_EV_LOGO:
169 case RPORT_EV_FAILED:
170 case RPORT_EV_STOP:
b5cbf083 171 lport->dns_rp = NULL;
42e9a92f
RL
172 break;
173 case RPORT_EV_NONE:
174 break;
175 }
b5cbf083 176 mutex_unlock(&lport->lp_mutex);
42e9a92f
RL
177}
178
179/**
34f42a07 180 * fc_lport_state() - Return a string which represents the lport's state
42e9a92f
RL
181 * @lport: The lport whose state is to converted to a string
182 */
183static const char *fc_lport_state(struct fc_lport *lport)
184{
185 const char *cp;
186
187 cp = fc_lport_state_names[lport->state];
188 if (!cp)
189 cp = "unknown";
190 return cp;
191}
192
193/**
34f42a07 194 * fc_lport_ptp_setup() - Create an rport for point-to-point mode
42e9a92f
RL
195 * @lport: The lport to attach the ptp rport to
196 * @fid: The FID of the ptp rport
197 * @remote_wwpn: The WWPN of the ptp rport
198 * @remote_wwnn: The WWNN of the ptp rport
199 */
200static void fc_lport_ptp_setup(struct fc_lport *lport,
201 u32 remote_fid, u64 remote_wwpn,
202 u64 remote_wwnn)
203{
48f00902
JE
204 mutex_lock(&lport->disc.disc_mutex);
205 if (lport->ptp_rp)
42e9a92f 206 lport->tt.rport_logoff(lport->ptp_rp);
9737e6a7
RL
207 lport->ptp_rp = lport->tt.rport_create(lport, remote_fid);
208 lport->ptp_rp->ids.port_name = remote_wwpn;
209 lport->ptp_rp->ids.node_name = remote_wwnn;
48f00902 210 mutex_unlock(&lport->disc.disc_mutex);
42e9a92f
RL
211
212 lport->tt.rport_login(lport->ptp_rp);
213
214 fc_lport_enter_ready(lport);
215}
216
217void fc_get_host_port_type(struct Scsi_Host *shost)
218{
219 /* TODO - currently just NPORT */
220 fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
221}
222EXPORT_SYMBOL(fc_get_host_port_type);
223
224void fc_get_host_port_state(struct Scsi_Host *shost)
225{
226 struct fc_lport *lp = shost_priv(shost);
227
8faecddb
CL
228 mutex_lock(&lp->lp_mutex);
229 if (!lp->link_up)
230 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
42e9a92f 231 else
8faecddb
CL
232 switch (lp->state) {
233 case LPORT_ST_READY:
234 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
235 break;
236 default:
237 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
238 }
239 mutex_unlock(&lp->lp_mutex);
42e9a92f
RL
240}
241EXPORT_SYMBOL(fc_get_host_port_state);
242
243void fc_get_host_speed(struct Scsi_Host *shost)
244{
245 struct fc_lport *lport = shost_priv(shost);
246
247 fc_host_speed(shost) = lport->link_speed;
248}
249EXPORT_SYMBOL(fc_get_host_speed);
250
251struct fc_host_statistics *fc_get_host_stats(struct Scsi_Host *shost)
252{
42e9a92f
RL
253 struct fc_host_statistics *fcoe_stats;
254 struct fc_lport *lp = shost_priv(shost);
255 struct timespec v0, v1;
582b45bc 256 unsigned int cpu;
42e9a92f
RL
257
258 fcoe_stats = &lp->host_stats;
259 memset(fcoe_stats, 0, sizeof(struct fc_host_statistics));
260
261 jiffies_to_timespec(jiffies, &v0);
262 jiffies_to_timespec(lp->boot_time, &v1);
263 fcoe_stats->seconds_since_last_reset = (v0.tv_sec - v1.tv_sec);
264
582b45bc
RL
265 for_each_possible_cpu(cpu) {
266 struct fcoe_dev_stats *stats;
267
268 stats = per_cpu_ptr(lp->dev_stats, cpu);
269
42e9a92f
RL
270 fcoe_stats->tx_frames += stats->TxFrames;
271 fcoe_stats->tx_words += stats->TxWords;
272 fcoe_stats->rx_frames += stats->RxFrames;
273 fcoe_stats->rx_words += stats->RxWords;
274 fcoe_stats->error_frames += stats->ErrorFrames;
275 fcoe_stats->invalid_crc_count += stats->InvalidCRCCount;
276 fcoe_stats->fcp_input_requests += stats->InputRequests;
277 fcoe_stats->fcp_output_requests += stats->OutputRequests;
278 fcoe_stats->fcp_control_requests += stats->ControlRequests;
279 fcoe_stats->fcp_input_megabytes += stats->InputMegabytes;
280 fcoe_stats->fcp_output_megabytes += stats->OutputMegabytes;
281 fcoe_stats->link_failure_count += stats->LinkFailureCount;
282 }
283 fcoe_stats->lip_count = -1;
284 fcoe_stats->nos_count = -1;
285 fcoe_stats->loss_of_sync_count = -1;
286 fcoe_stats->loss_of_signal_count = -1;
287 fcoe_stats->prim_seq_protocol_err_count = -1;
288 fcoe_stats->dumped_frames = -1;
289 return fcoe_stats;
290}
291EXPORT_SYMBOL(fc_get_host_stats);
292
293/*
294 * Fill in FLOGI command for request.
295 */
296static void
297fc_lport_flogi_fill(struct fc_lport *lport, struct fc_els_flogi *flogi,
298 unsigned int op)
299{
300 struct fc_els_csp *sp;
301 struct fc_els_cssp *cp;
302
303 memset(flogi, 0, sizeof(*flogi));
304 flogi->fl_cmd = (u8) op;
305 put_unaligned_be64(lport->wwpn, &flogi->fl_wwpn);
306 put_unaligned_be64(lport->wwnn, &flogi->fl_wwnn);
307 sp = &flogi->fl_csp;
308 sp->sp_hi_ver = 0x20;
309 sp->sp_lo_ver = 0x20;
310 sp->sp_bb_cred = htons(10); /* this gets set by gateway */
311 sp->sp_bb_data = htons((u16) lport->mfs);
312 cp = &flogi->fl_cssp[3 - 1]; /* class 3 parameters */
313 cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
314 if (op != ELS_FLOGI) {
315 sp->sp_features = htons(FC_SP_FT_CIRO);
316 sp->sp_tot_seq = htons(255); /* seq. we accept */
317 sp->sp_rel_off = htons(0x1f);
318 sp->sp_e_d_tov = htonl(lport->e_d_tov);
319
320 cp->cp_rdfs = htons((u16) lport->mfs);
321 cp->cp_con_seq = htons(255);
322 cp->cp_open_seq = 1;
323 }
324}
325
326/*
327 * Add a supported FC-4 type.
328 */
329static void fc_lport_add_fc4_type(struct fc_lport *lport, enum fc_fh_type type)
330{
331 __be32 *mp;
332
333 mp = &lport->fcts.ff_type_map[type / FC_NS_BPW];
334 *mp = htonl(ntohl(*mp) | 1UL << (type % FC_NS_BPW));
335}
336
337/**
34f42a07 338 * fc_lport_recv_rlir_req() - Handle received Registered Link Incident Report.
42e9a92f
RL
339 * @lport: Fibre Channel local port recieving the RLIR
340 * @sp: current sequence in the RLIR exchange
341 * @fp: RLIR request frame
342 *
1b69bc06 343 * Locking Note: The lport lock is expected to be held before calling
42e9a92f
RL
344 * this function.
345 */
346static void fc_lport_recv_rlir_req(struct fc_seq *sp, struct fc_frame *fp,
347 struct fc_lport *lport)
348{
7414705e
RL
349 FC_LPORT_DBG(lport, "Received RLIR request while in state %s\n",
350 fc_lport_state(lport));
42e9a92f
RL
351
352 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
353 fc_frame_free(fp);
354}
355
356/**
34f42a07 357 * fc_lport_recv_echo_req() - Handle received ECHO request
42e9a92f
RL
358 * @lport: Fibre Channel local port recieving the ECHO
359 * @sp: current sequence in the ECHO exchange
360 * @fp: ECHO request frame
361 *
1b69bc06 362 * Locking Note: The lport lock is expected to be held before calling
42e9a92f
RL
363 * this function.
364 */
365static void fc_lport_recv_echo_req(struct fc_seq *sp, struct fc_frame *in_fp,
366 struct fc_lport *lport)
367{
368 struct fc_frame *fp;
369 struct fc_exch *ep = fc_seq_exch(sp);
370 unsigned int len;
371 void *pp;
372 void *dp;
373 u32 f_ctl;
374
1b69bc06 375 FC_LPORT_DBG(lport, "Received ECHO request while in state %s\n",
7414705e 376 fc_lport_state(lport));
42e9a92f
RL
377
378 len = fr_len(in_fp) - sizeof(struct fc_frame_header);
379 pp = fc_frame_payload_get(in_fp, len);
380
381 if (len < sizeof(__be32))
382 len = sizeof(__be32);
383
384 fp = fc_frame_alloc(lport, len);
385 if (fp) {
386 dp = fc_frame_payload_get(fp, len);
387 memcpy(dp, pp, len);
1b69bc06 388 *((__be32 *)dp) = htonl(ELS_LS_ACC << 24);
42e9a92f
RL
389 sp = lport->tt.seq_start_next(sp);
390 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
391 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
392 FC_TYPE_ELS, f_ctl, 0);
393 lport->tt.seq_send(lport, sp, fp);
394 }
395 fc_frame_free(in_fp);
396}
397
398/**
1b69bc06
JE
399 * fc_lport_recv_rnid_req() - Handle received Request Node ID data request
400 * @sp: The sequence in the RNID exchange
401 * @fp: The RNID request frame
402 * @lport: The local port recieving the RNID
42e9a92f 403 *
1b69bc06 404 * Locking Note: The lport lock is expected to be held before calling
42e9a92f
RL
405 * this function.
406 */
407static void fc_lport_recv_rnid_req(struct fc_seq *sp, struct fc_frame *in_fp,
408 struct fc_lport *lport)
409{
410 struct fc_frame *fp;
411 struct fc_exch *ep = fc_seq_exch(sp);
412 struct fc_els_rnid *req;
413 struct {
414 struct fc_els_rnid_resp rnid;
415 struct fc_els_rnid_cid cid;
416 struct fc_els_rnid_gen gen;
417 } *rp;
418 struct fc_seq_els_data rjt_data;
419 u8 fmt;
420 size_t len;
421 u32 f_ctl;
422
7414705e
RL
423 FC_LPORT_DBG(lport, "Received RNID request while in state %s\n",
424 fc_lport_state(lport));
42e9a92f
RL
425
426 req = fc_frame_payload_get(in_fp, sizeof(*req));
427 if (!req) {
428 rjt_data.fp = NULL;
429 rjt_data.reason = ELS_RJT_LOGIC;
430 rjt_data.explan = ELS_EXPL_NONE;
431 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
432 } else {
433 fmt = req->rnid_fmt;
434 len = sizeof(*rp);
435 if (fmt != ELS_RNIDF_GEN ||
436 ntohl(lport->rnid_gen.rnid_atype) == 0) {
437 fmt = ELS_RNIDF_NONE; /* nothing to provide */
438 len -= sizeof(rp->gen);
439 }
440 fp = fc_frame_alloc(lport, len);
441 if (fp) {
442 rp = fc_frame_payload_get(fp, len);
443 memset(rp, 0, len);
444 rp->rnid.rnid_cmd = ELS_LS_ACC;
445 rp->rnid.rnid_fmt = fmt;
446 rp->rnid.rnid_cid_len = sizeof(rp->cid);
447 rp->cid.rnid_wwpn = htonll(lport->wwpn);
448 rp->cid.rnid_wwnn = htonll(lport->wwnn);
449 if (fmt == ELS_RNIDF_GEN) {
450 rp->rnid.rnid_sid_len = sizeof(rp->gen);
451 memcpy(&rp->gen, &lport->rnid_gen,
452 sizeof(rp->gen));
453 }
454 sp = lport->tt.seq_start_next(sp);
455 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
456 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
457 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
458 FC_TYPE_ELS, f_ctl, 0);
459 lport->tt.seq_send(lport, sp, fp);
460 }
461 }
462 fc_frame_free(in_fp);
463}
464
42e9a92f 465/**
34f42a07 466 * fc_lport_recv_logo_req() - Handle received fabric LOGO request
42e9a92f
RL
467 * @lport: Fibre Channel local port recieving the LOGO
468 * @sp: current sequence in the LOGO exchange
469 * @fp: LOGO request frame
470 *
471 * Locking Note: The lport lock is exected to be held before calling
472 * this function.
473 */
474static void fc_lport_recv_logo_req(struct fc_seq *sp, struct fc_frame *fp,
475 struct fc_lport *lport)
476{
477 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
478 fc_lport_enter_reset(lport);
479 fc_frame_free(fp);
480}
481
482/**
34f42a07 483 * fc_fabric_login() - Start the lport state machine
42e9a92f
RL
484 * @lport: The lport that should log into the fabric
485 *
486 * Locking Note: This function should not be called
487 * with the lport lock held.
488 */
489int fc_fabric_login(struct fc_lport *lport)
490{
491 int rc = -1;
492
493 mutex_lock(&lport->lp_mutex);
b1d9fd55 494 if (lport->state == LPORT_ST_DISABLED) {
42e9a92f
RL
495 fc_lport_enter_reset(lport);
496 rc = 0;
497 }
498 mutex_unlock(&lport->lp_mutex);
499
500 return rc;
501}
502EXPORT_SYMBOL(fc_fabric_login);
503
504/**
8faecddb 505 * __fc_linkup() - Handler for transport linkup events
42e9a92f 506 * @lport: The lport whose link is up
8faecddb
CL
507 *
508 * Locking: must be called with the lp_mutex held
42e9a92f 509 */
8faecddb 510void __fc_linkup(struct fc_lport *lport)
42e9a92f 511{
bc0e17f6
VD
512 if (!lport->link_up) {
513 lport->link_up = 1;
42e9a92f
RL
514
515 if (lport->state == LPORT_ST_RESET)
516 fc_lport_enter_flogi(lport);
517 }
8faecddb
CL
518}
519
520/**
521 * fc_linkup() - Handler for transport linkup events
522 * @lport: The lport whose link is up
523 */
524void fc_linkup(struct fc_lport *lport)
525{
526 printk(KERN_INFO "libfc: Link up on port (%6x)\n",
527 fc_host_port_id(lport->host));
528
529 mutex_lock(&lport->lp_mutex);
530 __fc_linkup(lport);
42e9a92f
RL
531 mutex_unlock(&lport->lp_mutex);
532}
533EXPORT_SYMBOL(fc_linkup);
534
535/**
8faecddb 536 * __fc_linkdown() - Handler for transport linkdown events
42e9a92f 537 * @lport: The lport whose link is down
8faecddb
CL
538 *
539 * Locking: must be called with the lp_mutex held
42e9a92f 540 */
8faecddb 541void __fc_linkdown(struct fc_lport *lport)
42e9a92f 542{
bc0e17f6
VD
543 if (lport->link_up) {
544 lport->link_up = 0;
42e9a92f
RL
545 fc_lport_enter_reset(lport);
546 lport->tt.fcp_cleanup(lport);
547 }
8faecddb
CL
548}
549
550/**
551 * fc_linkdown() - Handler for transport linkdown events
552 * @lport: The lport whose link is down
553 */
554void fc_linkdown(struct fc_lport *lport)
555{
556 printk(KERN_INFO "libfc: Link down on port (%6x)\n",
557 fc_host_port_id(lport->host));
558
559 mutex_lock(&lport->lp_mutex);
560 __fc_linkdown(lport);
42e9a92f
RL
561 mutex_unlock(&lport->lp_mutex);
562}
563EXPORT_SYMBOL(fc_linkdown);
564
42e9a92f 565/**
34f42a07 566 * fc_fabric_logoff() - Logout of the fabric
42e9a92f
RL
567 * @lport: fc_lport pointer to logoff the fabric
568 *
569 * Return value:
570 * 0 for success, -1 for failure
34f42a07 571 */
42e9a92f
RL
572int fc_fabric_logoff(struct fc_lport *lport)
573{
574 lport->tt.disc_stop_final(lport);
575 mutex_lock(&lport->lp_mutex);
a0fd2e49
AJ
576 if (lport->dns_rp)
577 lport->tt.rport_logoff(lport->dns_rp);
578 mutex_unlock(&lport->lp_mutex);
579 lport->tt.rport_flush_queue();
580 mutex_lock(&lport->lp_mutex);
42e9a92f
RL
581 fc_lport_enter_logo(lport);
582 mutex_unlock(&lport->lp_mutex);
f7db2c15 583 cancel_delayed_work_sync(&lport->retry_work);
42e9a92f
RL
584 return 0;
585}
586EXPORT_SYMBOL(fc_fabric_logoff);
587
588/**
34f42a07 589 * fc_lport_destroy() - unregister a fc_lport
42e9a92f
RL
590 * @lport: fc_lport pointer to unregister
591 *
592 * Return value:
593 * None
594 * Note:
595 * exit routine for fc_lport instance
596 * clean-up all the allocated memory
597 * and free up other system resources.
598 *
34f42a07 599 */
42e9a92f
RL
600int fc_lport_destroy(struct fc_lport *lport)
601{
bbf15669 602 mutex_lock(&lport->lp_mutex);
b1d9fd55 603 lport->state = LPORT_ST_DISABLED;
bbf15669 604 lport->link_up = 0;
42e9a92f 605 lport->tt.frame_send = fc_frame_drop;
bbf15669
AJ
606 mutex_unlock(&lport->lp_mutex);
607
42e9a92f 608 lport->tt.fcp_abort_io(lport);
e9ba8b42 609 lport->tt.disc_stop_final(lport);
1f6ff364 610 lport->tt.exch_mgr_reset(lport, 0, 0);
42e9a92f
RL
611 return 0;
612}
613EXPORT_SYMBOL(fc_lport_destroy);
614
615/**
34f42a07 616 * fc_set_mfs() - sets up the mfs for the corresponding fc_lport
42e9a92f
RL
617 * @lport: fc_lport pointer to unregister
618 * @mfs: the new mfs for fc_lport
619 *
620 * Set mfs for the given fc_lport to the new mfs.
621 *
622 * Return: 0 for success
34f42a07 623 */
42e9a92f
RL
624int fc_set_mfs(struct fc_lport *lport, u32 mfs)
625{
626 unsigned int old_mfs;
627 int rc = -EINVAL;
628
629 mutex_lock(&lport->lp_mutex);
630
631 old_mfs = lport->mfs;
632
633 if (mfs >= FC_MIN_MAX_FRAME) {
634 mfs &= ~3;
635 if (mfs > FC_MAX_FRAME)
636 mfs = FC_MAX_FRAME;
637 mfs -= sizeof(struct fc_frame_header);
638 lport->mfs = mfs;
639 rc = 0;
640 }
641
642 if (!rc && mfs < old_mfs)
643 fc_lport_enter_reset(lport);
644
645 mutex_unlock(&lport->lp_mutex);
646
647 return rc;
648}
649EXPORT_SYMBOL(fc_set_mfs);
650
651/**
34f42a07 652 * fc_lport_disc_callback() - Callback for discovery events
42e9a92f
RL
653 * @lport: FC local port
654 * @event: The discovery event
655 */
656void fc_lport_disc_callback(struct fc_lport *lport, enum fc_disc_event event)
657{
658 switch (event) {
659 case DISC_EV_SUCCESS:
7414705e 660 FC_LPORT_DBG(lport, "Discovery succeeded\n");
42e9a92f
RL
661 break;
662 case DISC_EV_FAILED:
7414705e
RL
663 printk(KERN_ERR "libfc: Discovery failed for port (%6x)\n",
664 fc_host_port_id(lport->host));
42e9a92f
RL
665 mutex_lock(&lport->lp_mutex);
666 fc_lport_enter_reset(lport);
667 mutex_unlock(&lport->lp_mutex);
668 break;
669 case DISC_EV_NONE:
670 WARN_ON(1);
671 break;
672 }
673}
674
675/**
34f42a07 676 * fc_rport_enter_ready() - Enter the ready state and start discovery
42e9a92f
RL
677 * @lport: Fibre Channel local port that is ready
678 *
679 * Locking Note: The lport lock is expected to be held before calling
680 * this routine.
681 */
682static void fc_lport_enter_ready(struct fc_lport *lport)
683{
7414705e
RL
684 FC_LPORT_DBG(lport, "Entered READY from state %s\n",
685 fc_lport_state(lport));
42e9a92f
RL
686
687 fc_lport_state_enter(lport, LPORT_ST_READY);
8faecddb
CL
688 if (lport->vport)
689 fc_vport_set_state(lport->vport, FC_VPORT_ACTIVE);
690 fc_vports_linkchange(lport);
42e9a92f 691
29d898e9
JE
692 if (!lport->ptp_rp)
693 lport->tt.disc_start(fc_lport_disc_callback, lport);
42e9a92f
RL
694}
695
696/**
34f42a07 697 * fc_lport_recv_flogi_req() - Receive a FLOGI request
42e9a92f
RL
698 * @sp_in: The sequence the FLOGI is on
699 * @rx_fp: The frame the FLOGI is in
700 * @lport: The lport that recieved the request
701 *
702 * A received FLOGI request indicates a point-to-point connection.
703 * Accept it with the common service parameters indicating our N port.
704 * Set up to do a PLOGI if we have the higher-number WWPN.
705 *
1b69bc06 706 * Locking Note: The lport lock is expected to be held before calling
42e9a92f
RL
707 * this function.
708 */
709static void fc_lport_recv_flogi_req(struct fc_seq *sp_in,
710 struct fc_frame *rx_fp,
711 struct fc_lport *lport)
712{
713 struct fc_frame *fp;
714 struct fc_frame_header *fh;
715 struct fc_seq *sp;
716 struct fc_exch *ep;
717 struct fc_els_flogi *flp;
718 struct fc_els_flogi *new_flp;
719 u64 remote_wwpn;
720 u32 remote_fid;
721 u32 local_fid;
722 u32 f_ctl;
723
7414705e
RL
724 FC_LPORT_DBG(lport, "Received FLOGI request while in state %s\n",
725 fc_lport_state(lport));
42e9a92f
RL
726
727 fh = fc_frame_header_get(rx_fp);
728 remote_fid = ntoh24(fh->fh_s_id);
729 flp = fc_frame_payload_get(rx_fp, sizeof(*flp));
730 if (!flp)
731 goto out;
732 remote_wwpn = get_unaligned_be64(&flp->fl_wwpn);
733 if (remote_wwpn == lport->wwpn) {
7414705e
RL
734 printk(KERN_WARNING "libfc: Received FLOGI from port "
735 "with same WWPN %llx\n", remote_wwpn);
42e9a92f
RL
736 goto out;
737 }
7414705e 738 FC_LPORT_DBG(lport, "FLOGI from port WWPN %llx\n", remote_wwpn);
42e9a92f
RL
739
740 /*
741 * XXX what is the right thing to do for FIDs?
742 * The originator might expect our S_ID to be 0xfffffe.
743 * But if so, both of us could end up with the same FID.
744 */
745 local_fid = FC_LOCAL_PTP_FID_LO;
746 if (remote_wwpn < lport->wwpn) {
747 local_fid = FC_LOCAL_PTP_FID_HI;
748 if (!remote_fid || remote_fid == local_fid)
749 remote_fid = FC_LOCAL_PTP_FID_LO;
750 } else if (!remote_fid) {
751 remote_fid = FC_LOCAL_PTP_FID_HI;
752 }
753
754 fc_host_port_id(lport->host) = local_fid;
755
756 fp = fc_frame_alloc(lport, sizeof(*flp));
757 if (fp) {
758 sp = lport->tt.seq_start_next(fr_seq(rx_fp));
759 new_flp = fc_frame_payload_get(fp, sizeof(*flp));
760 fc_lport_flogi_fill(lport, new_flp, ELS_FLOGI);
761 new_flp->fl_cmd = (u8) ELS_LS_ACC;
762
763 /*
764 * Send the response. If this fails, the originator should
765 * repeat the sequence.
766 */
767 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
768 ep = fc_seq_exch(sp);
769 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
770 FC_TYPE_ELS, f_ctl, 0);
771 lport->tt.seq_send(lport, sp, fp);
772
773 } else {
774 fc_lport_error(lport, fp);
775 }
776 fc_lport_ptp_setup(lport, remote_fid, remote_wwpn,
777 get_unaligned_be64(&flp->fl_wwnn));
778
42e9a92f
RL
779out:
780 sp = fr_seq(rx_fp);
781 fc_frame_free(rx_fp);
782}
783
784/**
34f42a07 785 * fc_lport_recv_req() - The generic lport request handler
42e9a92f
RL
786 * @lport: The lport that received the request
787 * @sp: The sequence the request is on
788 * @fp: The frame the request is in
789 *
790 * This function will see if the lport handles the request or
791 * if an rport should handle the request.
792 *
793 * Locking Note: This function should not be called with the lport
794 * lock held becuase it will grab the lock.
795 */
796static void fc_lport_recv_req(struct fc_lport *lport, struct fc_seq *sp,
797 struct fc_frame *fp)
798{
799 struct fc_frame_header *fh = fc_frame_header_get(fp);
800 void (*recv) (struct fc_seq *, struct fc_frame *, struct fc_lport *);
42e9a92f
RL
801
802 mutex_lock(&lport->lp_mutex);
803
804 /*
805 * Handle special ELS cases like FLOGI, LOGO, and
806 * RSCN here. These don't require a session.
807 * Even if we had a session, it might not be ready.
808 */
e9ba8b42
JE
809 if (!lport->link_up)
810 fc_frame_free(fp);
811 else if (fh->fh_type == FC_TYPE_ELS &&
812 fh->fh_r_ctl == FC_RCTL_ELS_REQ) {
42e9a92f
RL
813 /*
814 * Check opcode.
815 */
131203a1 816 recv = lport->tt.rport_recv_req;
42e9a92f
RL
817 switch (fc_frame_payload_op(fp)) {
818 case ELS_FLOGI:
819 recv = fc_lport_recv_flogi_req;
820 break;
821 case ELS_LOGO:
822 fh = fc_frame_header_get(fp);
823 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI)
824 recv = fc_lport_recv_logo_req;
825 break;
826 case ELS_RSCN:
827 recv = lport->tt.disc_recv_req;
828 break;
829 case ELS_ECHO:
830 recv = fc_lport_recv_echo_req;
831 break;
832 case ELS_RLIR:
833 recv = fc_lport_recv_rlir_req;
834 break;
835 case ELS_RNID:
836 recv = fc_lport_recv_rnid_req;
837 break;
42e9a92f
RL
838 }
839
131203a1 840 recv(sp, fp, lport);
42e9a92f 841 } else {
7414705e
RL
842 FC_LPORT_DBG(lport, "dropping invalid frame (eof %x)\n",
843 fr_eof(fp));
42e9a92f
RL
844 fc_frame_free(fp);
845 }
846 mutex_unlock(&lport->lp_mutex);
847
848 /*
849 * The common exch_done for all request may not be good
850 * if any request requires longer hold on exhange. XXX
851 */
852 lport->tt.exch_done(sp);
853}
854
855/**
34f42a07 856 * fc_lport_reset() - Reset an lport
42e9a92f
RL
857 * @lport: The lport which should be reset
858 *
859 * Locking Note: This functions should not be called with the
860 * lport lock held.
861 */
862int fc_lport_reset(struct fc_lport *lport)
863{
f7db2c15 864 cancel_delayed_work_sync(&lport->retry_work);
42e9a92f
RL
865 mutex_lock(&lport->lp_mutex);
866 fc_lport_enter_reset(lport);
867 mutex_unlock(&lport->lp_mutex);
868 return 0;
869}
870EXPORT_SYMBOL(fc_lport_reset);
871
872/**
1190d925 873 * fc_lport_reset_locked() - Reset the local port
42e9a92f
RL
874 * @lport: Fibre Channel local port to be reset
875 *
876 * Locking Note: The lport lock is expected to be held before calling
877 * this routine.
878 */
1190d925 879static void fc_lport_reset_locked(struct fc_lport *lport)
42e9a92f 880{
42e9a92f
RL
881 if (lport->dns_rp)
882 lport->tt.rport_logoff(lport->dns_rp);
883
48f00902 884 lport->ptp_rp = NULL;
42e9a92f
RL
885
886 lport->tt.disc_stop(lport);
887
1f6ff364 888 lport->tt.exch_mgr_reset(lport, 0, 0);
42e9a92f
RL
889 fc_host_fabric_name(lport->host) = 0;
890 fc_host_port_id(lport->host) = 0;
1190d925 891}
42e9a92f 892
1190d925
JE
893/**
894 * fc_lport_enter_reset() - Reset the local port
895 * @lport: Fibre Channel local port to be reset
896 *
897 * Locking Note: The lport lock is expected to be held before calling
898 * this routine.
899 */
900static void fc_lport_enter_reset(struct fc_lport *lport)
901{
902 FC_LPORT_DBG(lport, "Entered RESET state from %s state\n",
903 fc_lport_state(lport));
904
8faecddb
CL
905 if (lport->vport) {
906 if (lport->link_up)
907 fc_vport_set_state(lport->vport, FC_VPORT_INITIALIZING);
908 else
909 fc_vport_set_state(lport->vport, FC_VPORT_LINKDOWN);
910 }
1190d925 911 fc_lport_state_enter(lport, LPORT_ST_RESET);
8faecddb 912 fc_vports_linkchange(lport);
1190d925 913 fc_lport_reset_locked(lport);
bc0e17f6 914 if (lport->link_up)
42e9a92f
RL
915 fc_lport_enter_flogi(lport);
916}
917
1190d925
JE
918/**
919 * fc_lport_enter_disabled() - disable the local port
920 * @lport: Fibre Channel local port to be reset
921 *
922 * Locking Note: The lport lock is expected to be held before calling
923 * this routine.
924 */
925static void fc_lport_enter_disabled(struct fc_lport *lport)
926{
927 FC_LPORT_DBG(lport, "Entered disabled state from %s state\n",
928 fc_lport_state(lport));
929
930 fc_lport_state_enter(lport, LPORT_ST_DISABLED);
8faecddb 931 fc_vports_linkchange(lport);
1190d925
JE
932 fc_lport_reset_locked(lport);
933}
934
42e9a92f 935/**
34f42a07 936 * fc_lport_error() - Handler for any errors
42e9a92f
RL
937 * @lport: The fc_lport object
938 * @fp: The frame pointer
939 *
940 * If the error was caused by a resource allocation failure
941 * then wait for half a second and retry, otherwise retry
942 * after the e_d_tov time.
943 */
944static void fc_lport_error(struct fc_lport *lport, struct fc_frame *fp)
945{
946 unsigned long delay = 0;
7414705e
RL
947 FC_LPORT_DBG(lport, "Error %ld in state %s, retries %d\n",
948 PTR_ERR(fp), fc_lport_state(lport),
949 lport->retry_count);
42e9a92f
RL
950
951 if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
952 /*
953 * Memory allocation failure, or the exchange timed out.
954 * Retry after delay
955 */
956 if (lport->retry_count < lport->max_retry_count) {
957 lport->retry_count++;
958 if (!fp)
959 delay = msecs_to_jiffies(500);
960 else
961 delay = msecs_to_jiffies(lport->e_d_tov);
962
963 schedule_delayed_work(&lport->retry_work, delay);
964 } else {
965 switch (lport->state) {
b1d9fd55 966 case LPORT_ST_DISABLED:
42e9a92f
RL
967 case LPORT_ST_READY:
968 case LPORT_ST_RESET:
c9c7bd7a 969 case LPORT_ST_RNN_ID:
5baa17c3 970 case LPORT_ST_RSNN_NN:
c9866a54 971 case LPORT_ST_RSPN_ID:
42e9a92f
RL
972 case LPORT_ST_RFT_ID:
973 case LPORT_ST_SCR:
974 case LPORT_ST_DNS:
975 case LPORT_ST_FLOGI:
976 case LPORT_ST_LOGO:
977 fc_lport_enter_reset(lport);
978 break;
979 }
980 }
981 }
982}
983
984/**
7cccc157
CL
985 * fc_lport_ns_resp() - Handle response to a name server
986 * registration exchange
987 * @sp: current sequence in exchange
42e9a92f
RL
988 * @fp: response frame
989 * @lp_arg: Fibre Channel host port instance
990 *
991 * Locking Note: This function will be called without the lport lock
992 * held, but it will lock, call an _enter_* function or fc_lport_error
993 * and then unlock the lport.
994 */
7cccc157
CL
995static void fc_lport_ns_resp(struct fc_seq *sp, struct fc_frame *fp,
996 void *lp_arg)
42e9a92f
RL
997{
998 struct fc_lport *lport = lp_arg;
999 struct fc_frame_header *fh;
1000 struct fc_ct_hdr *ct;
1001
7cccc157 1002 FC_LPORT_DBG(lport, "Received a ns %s\n", fc_els_resp_type(fp));
f657d299 1003
42e9a92f
RL
1004 if (fp == ERR_PTR(-FC_EX_CLOSED))
1005 return;
1006
1007 mutex_lock(&lport->lp_mutex);
1008
7cccc157
CL
1009 if (lport->state < LPORT_ST_RNN_ID || lport->state > LPORT_ST_RFT_ID) {
1010 FC_LPORT_DBG(lport, "Received a name server response, "
1011 "but in state %s\n", fc_lport_state(lport));
76f6804e
AJ
1012 if (IS_ERR(fp))
1013 goto err;
42e9a92f
RL
1014 goto out;
1015 }
1016
76f6804e
AJ
1017 if (IS_ERR(fp)) {
1018 fc_lport_error(lport, fp);
1019 goto err;
1020 }
1021
42e9a92f
RL
1022 fh = fc_frame_header_get(fp);
1023 ct = fc_frame_payload_get(fp, sizeof(*ct));
1024
1025 if (fh && ct && fh->fh_type == FC_TYPE_CT &&
1026 ct->ct_fs_type == FC_FST_DIR &&
1027 ct->ct_fs_subtype == FC_NS_SUBTYPE &&
1028 ntohs(ct->ct_cmd) == FC_FS_ACC)
7cccc157
CL
1029 switch (lport->state) {
1030 case LPORT_ST_RNN_ID:
c914f7d1 1031 fc_lport_enter_ns(lport, LPORT_ST_RSNN_NN);
7cccc157
CL
1032 break;
1033 case LPORT_ST_RSNN_NN:
c914f7d1 1034 fc_lport_enter_ns(lport, LPORT_ST_RSPN_ID);
7cccc157
CL
1035 break;
1036 case LPORT_ST_RSPN_ID:
c914f7d1 1037 fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
7cccc157
CL
1038 break;
1039 case LPORT_ST_RFT_ID:
1040 fc_lport_enter_scr(lport);
1041 break;
1042 default:
1043 /* should have already been caught by state checks */
1044 break;
1045 }
c9c7bd7a
CL
1046 else
1047 fc_lport_error(lport, fp);
c9c7bd7a
CL
1048out:
1049 fc_frame_free(fp);
1050err:
1051 mutex_unlock(&lport->lp_mutex);
1052}
1053
42e9a92f 1054/**
34f42a07 1055 * fc_lport_scr_resp() - Handle response to State Change Register (SCR) request
42e9a92f
RL
1056 * @sp: current sequence in SCR exchange
1057 * @fp: response frame
1058 * @lp_arg: Fibre Channel lport port instance that sent the registration request
1059 *
1060 * Locking Note: This function will be called without the lport lock
1061 * held, but it will lock, call an _enter_* function or fc_lport_error
1062 * and then unlock the lport.
1063 */
1064static void fc_lport_scr_resp(struct fc_seq *sp, struct fc_frame *fp,
1065 void *lp_arg)
1066{
1067 struct fc_lport *lport = lp_arg;
1068 u8 op;
1069
f657d299
JE
1070 FC_LPORT_DBG(lport, "Received a SCR %s\n", fc_els_resp_type(fp));
1071
42e9a92f
RL
1072 if (fp == ERR_PTR(-FC_EX_CLOSED))
1073 return;
1074
1075 mutex_lock(&lport->lp_mutex);
1076
42e9a92f 1077 if (lport->state != LPORT_ST_SCR) {
7414705e
RL
1078 FC_LPORT_DBG(lport, "Received a SCR response, but in state "
1079 "%s\n", fc_lport_state(lport));
76f6804e
AJ
1080 if (IS_ERR(fp))
1081 goto err;
42e9a92f
RL
1082 goto out;
1083 }
1084
76f6804e
AJ
1085 if (IS_ERR(fp)) {
1086 fc_lport_error(lport, fp);
1087 goto err;
1088 }
1089
42e9a92f
RL
1090 op = fc_frame_payload_op(fp);
1091 if (op == ELS_LS_ACC)
1092 fc_lport_enter_ready(lport);
1093 else
1094 fc_lport_error(lport, fp);
1095
1096out:
1097 fc_frame_free(fp);
1098err:
1099 mutex_unlock(&lport->lp_mutex);
1100}
1101
1102/**
34f42a07 1103 * fc_lport_enter_scr() - Send a State Change Register (SCR) request
42e9a92f
RL
1104 * @lport: Fibre Channel local port to register for state changes
1105 *
1106 * Locking Note: The lport lock is expected to be held before calling
1107 * this routine.
1108 */
1109static void fc_lport_enter_scr(struct fc_lport *lport)
1110{
1111 struct fc_frame *fp;
1112
7414705e
RL
1113 FC_LPORT_DBG(lport, "Entered SCR state from %s state\n",
1114 fc_lport_state(lport));
42e9a92f
RL
1115
1116 fc_lport_state_enter(lport, LPORT_ST_SCR);
1117
1118 fp = fc_frame_alloc(lport, sizeof(struct fc_els_scr));
1119 if (!fp) {
1120 fc_lport_error(lport, fp);
1121 return;
1122 }
1123
a46f327a 1124 if (!lport->tt.elsct_send(lport, FC_FID_FCTRL, fp, ELS_SCR,
42e9a92f 1125 fc_lport_scr_resp, lport, lport->e_d_tov))
8f550f93 1126 fc_lport_error(lport, NULL);
42e9a92f
RL
1127}
1128
1129/**
c914f7d1 1130 * fc_lport_enter_ns() - register some object with the name server
42e9a92f
RL
1131 * @lport: Fibre Channel local port to register
1132 *
1133 * Locking Note: The lport lock is expected to be held before calling
1134 * this routine.
1135 */
c914f7d1 1136static void fc_lport_enter_ns(struct fc_lport *lport, enum fc_lport_state state)
c9866a54
CL
1137{
1138 struct fc_frame *fp;
c914f7d1
CL
1139 enum fc_ns_req cmd;
1140 int size = sizeof(struct fc_ct_hdr);
c9866a54
CL
1141 size_t len;
1142
c914f7d1
CL
1143 FC_LPORT_DBG(lport, "Entered %s state from %s state\n",
1144 fc_lport_state_names[state],
c9866a54
CL
1145 fc_lport_state(lport));
1146
c914f7d1 1147 fc_lport_state_enter(lport, state);
c9866a54 1148
c914f7d1
CL
1149 switch (state) {
1150 case LPORT_ST_RNN_ID:
1151 cmd = FC_NS_RNN_ID;
1152 size += sizeof(struct fc_ns_rn_id);
1153 break;
1154 case LPORT_ST_RSNN_NN:
1155 len = strnlen(fc_host_symbolic_name(lport->host), 255);
1156 /* if there is no symbolic name, skip to RFT_ID */
1157 if (!len)
1158 return fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
1159 cmd = FC_NS_RSNN_NN;
1160 size += sizeof(struct fc_ns_rsnn) + len;
1161 break;
1162 case LPORT_ST_RSPN_ID:
1163 len = strnlen(fc_host_symbolic_name(lport->host), 255);
1164 /* if there is no symbolic name, skip to RFT_ID */
1165 if (!len)
1166 return fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
1167 cmd = FC_NS_RSPN_ID;
1168 size += sizeof(struct fc_ns_rspn) + len;
1169 break;
1170 case LPORT_ST_RFT_ID:
1171 cmd = FC_NS_RFT_ID;
1172 size += sizeof(struct fc_ns_rft);
1173 break;
1174 default:
1175 fc_lport_error(lport, NULL);
5baa17c3
CL
1176 return;
1177 }
1178
c914f7d1 1179 fp = fc_frame_alloc(lport, size);
c9c7bd7a
CL
1180 if (!fp) {
1181 fc_lport_error(lport, fp);
1182 return;
1183 }
1184
c914f7d1 1185 if (!lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, cmd,
7cccc157 1186 fc_lport_ns_resp,
c9c7bd7a
CL
1187 lport, lport->e_d_tov))
1188 fc_lport_error(lport, fp);
1189}
1190
42e9a92f
RL
1191static struct fc_rport_operations fc_lport_rport_ops = {
1192 .event_callback = fc_lport_rport_callback,
1193};
1194
1195/**
34f42a07 1196 * fc_rport_enter_dns() - Create a rport to the name server
42e9a92f
RL
1197 * @lport: Fibre Channel local port requesting a rport for the name server
1198 *
1199 * Locking Note: The lport lock is expected to be held before calling
1200 * this routine.
1201 */
1202static void fc_lport_enter_dns(struct fc_lport *lport)
1203{
ab28f1fd 1204 struct fc_rport_priv *rdata;
42e9a92f 1205
7414705e
RL
1206 FC_LPORT_DBG(lport, "Entered DNS state from %s state\n",
1207 fc_lport_state(lport));
42e9a92f
RL
1208
1209 fc_lport_state_enter(lport, LPORT_ST_DNS);
1210
48f00902 1211 mutex_lock(&lport->disc.disc_mutex);
9737e6a7 1212 rdata = lport->tt.rport_create(lport, FC_FID_DIR_SERV);
48f00902 1213 mutex_unlock(&lport->disc.disc_mutex);
9fb9d328 1214 if (!rdata)
42e9a92f
RL
1215 goto err;
1216
42e9a92f 1217 rdata->ops = &fc_lport_rport_ops;
9fb9d328 1218 lport->tt.rport_login(rdata);
42e9a92f
RL
1219 return;
1220
1221err:
1222 fc_lport_error(lport, NULL);
1223}
1224
1225/**
34f42a07 1226 * fc_lport_timeout() - Handler for the retry_work timer.
42e9a92f
RL
1227 * @work: The work struct of the fc_lport
1228 */
1229static void fc_lport_timeout(struct work_struct *work)
1230{
1231 struct fc_lport *lport =
1232 container_of(work, struct fc_lport,
1233 retry_work.work);
1234
1235 mutex_lock(&lport->lp_mutex);
1236
1237 switch (lport->state) {
b1d9fd55 1238 case LPORT_ST_DISABLED:
22655ac2
JE
1239 WARN_ON(1);
1240 break;
42e9a92f 1241 case LPORT_ST_READY:
42e9a92f
RL
1242 WARN_ON(1);
1243 break;
22655ac2
JE
1244 case LPORT_ST_RESET:
1245 break;
42e9a92f
RL
1246 case LPORT_ST_FLOGI:
1247 fc_lport_enter_flogi(lport);
1248 break;
1249 case LPORT_ST_DNS:
1250 fc_lport_enter_dns(lport);
1251 break;
c9c7bd7a 1252 case LPORT_ST_RNN_ID:
5baa17c3 1253 case LPORT_ST_RSNN_NN:
c9866a54 1254 case LPORT_ST_RSPN_ID:
42e9a92f 1255 case LPORT_ST_RFT_ID:
c914f7d1 1256 fc_lport_enter_ns(lport, lport->state);
42e9a92f
RL
1257 break;
1258 case LPORT_ST_SCR:
1259 fc_lport_enter_scr(lport);
1260 break;
1261 case LPORT_ST_LOGO:
1262 fc_lport_enter_logo(lport);
1263 break;
1264 }
1265
1266 mutex_unlock(&lport->lp_mutex);
1267}
1268
1269/**
34f42a07 1270 * fc_lport_logo_resp() - Handle response to LOGO request
42e9a92f
RL
1271 * @sp: current sequence in LOGO exchange
1272 * @fp: response frame
1273 * @lp_arg: Fibre Channel lport port instance that sent the LOGO request
1274 *
1275 * Locking Note: This function will be called without the lport lock
1276 * held, but it will lock, call an _enter_* function or fc_lport_error
1277 * and then unlock the lport.
1278 */
11b56188 1279void fc_lport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
42e9a92f
RL
1280 void *lp_arg)
1281{
1282 struct fc_lport *lport = lp_arg;
1283 u8 op;
1284
f657d299
JE
1285 FC_LPORT_DBG(lport, "Received a LOGO %s\n", fc_els_resp_type(fp));
1286
42e9a92f
RL
1287 if (fp == ERR_PTR(-FC_EX_CLOSED))
1288 return;
1289
1290 mutex_lock(&lport->lp_mutex);
1291
42e9a92f 1292 if (lport->state != LPORT_ST_LOGO) {
7414705e
RL
1293 FC_LPORT_DBG(lport, "Received a LOGO response, but in state "
1294 "%s\n", fc_lport_state(lport));
76f6804e
AJ
1295 if (IS_ERR(fp))
1296 goto err;
42e9a92f
RL
1297 goto out;
1298 }
1299
76f6804e
AJ
1300 if (IS_ERR(fp)) {
1301 fc_lport_error(lport, fp);
1302 goto err;
1303 }
1304
42e9a92f
RL
1305 op = fc_frame_payload_op(fp);
1306 if (op == ELS_LS_ACC)
1190d925 1307 fc_lport_enter_disabled(lport);
42e9a92f
RL
1308 else
1309 fc_lport_error(lport, fp);
1310
1311out:
1312 fc_frame_free(fp);
1313err:
1314 mutex_unlock(&lport->lp_mutex);
1315}
11b56188 1316EXPORT_SYMBOL(fc_lport_logo_resp);
42e9a92f
RL
1317
1318/**
34f42a07 1319 * fc_rport_enter_logo() - Logout of the fabric
42e9a92f
RL
1320 * @lport: Fibre Channel local port to be logged out
1321 *
1322 * Locking Note: The lport lock is expected to be held before calling
1323 * this routine.
1324 */
1325static void fc_lport_enter_logo(struct fc_lport *lport)
1326{
1327 struct fc_frame *fp;
1328 struct fc_els_logo *logo;
1329
7414705e
RL
1330 FC_LPORT_DBG(lport, "Entered LOGO state from %s state\n",
1331 fc_lport_state(lport));
42e9a92f
RL
1332
1333 fc_lport_state_enter(lport, LPORT_ST_LOGO);
8faecddb 1334 fc_vports_linkchange(lport);
42e9a92f 1335
42e9a92f
RL
1336 fp = fc_frame_alloc(lport, sizeof(*logo));
1337 if (!fp) {
1338 fc_lport_error(lport, fp);
1339 return;
1340 }
1341
a46f327a
JE
1342 if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp, ELS_LOGO,
1343 fc_lport_logo_resp, lport, lport->e_d_tov))
8f550f93 1344 fc_lport_error(lport, NULL);
42e9a92f
RL
1345}
1346
1347/**
34f42a07 1348 * fc_lport_flogi_resp() - Handle response to FLOGI request
42e9a92f
RL
1349 * @sp: current sequence in FLOGI exchange
1350 * @fp: response frame
1351 * @lp_arg: Fibre Channel lport port instance that sent the FLOGI request
1352 *
1353 * Locking Note: This function will be called without the lport lock
1354 * held, but it will lock, call an _enter_* function or fc_lport_error
1355 * and then unlock the lport.
1356 */
11b56188 1357void fc_lport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
42e9a92f
RL
1358 void *lp_arg)
1359{
1360 struct fc_lport *lport = lp_arg;
1361 struct fc_frame_header *fh;
1362 struct fc_els_flogi *flp;
1363 u32 did;
1364 u16 csp_flags;
1365 unsigned int r_a_tov;
1366 unsigned int e_d_tov;
1367 u16 mfs;
1368
f657d299
JE
1369 FC_LPORT_DBG(lport, "Received a FLOGI %s\n", fc_els_resp_type(fp));
1370
42e9a92f
RL
1371 if (fp == ERR_PTR(-FC_EX_CLOSED))
1372 return;
1373
1374 mutex_lock(&lport->lp_mutex);
1375
42e9a92f 1376 if (lport->state != LPORT_ST_FLOGI) {
7414705e
RL
1377 FC_LPORT_DBG(lport, "Received a FLOGI response, but in state "
1378 "%s\n", fc_lport_state(lport));
76f6804e
AJ
1379 if (IS_ERR(fp))
1380 goto err;
42e9a92f
RL
1381 goto out;
1382 }
1383
76f6804e
AJ
1384 if (IS_ERR(fp)) {
1385 fc_lport_error(lport, fp);
1386 goto err;
1387 }
1388
42e9a92f
RL
1389 fh = fc_frame_header_get(fp);
1390 did = ntoh24(fh->fh_d_id);
1391 if (fc_frame_payload_op(fp) == ELS_LS_ACC && did != 0) {
1392
7414705e
RL
1393 printk(KERN_INFO "libfc: Assigned FID (%6x) in FLOGI response\n",
1394 did);
42e9a92f
RL
1395 fc_host_port_id(lport->host) = did;
1396
1397 flp = fc_frame_payload_get(fp, sizeof(*flp));
1398 if (flp) {
1399 mfs = ntohs(flp->fl_csp.sp_bb_data) &
1400 FC_SP_BB_DATA_MASK;
1401 if (mfs >= FC_SP_MIN_MAX_PAYLOAD &&
1402 mfs < lport->mfs)
1403 lport->mfs = mfs;
1404 csp_flags = ntohs(flp->fl_csp.sp_features);
1405 r_a_tov = ntohl(flp->fl_csp.sp_r_a_tov);
1406 e_d_tov = ntohl(flp->fl_csp.sp_e_d_tov);
1407 if (csp_flags & FC_SP_FT_EDTR)
1408 e_d_tov /= 1000000;
db36c06c
CL
1409
1410 lport->npiv_enabled = !!(csp_flags & FC_SP_FT_NPIV_ACC);
1411
42e9a92f
RL
1412 if ((csp_flags & FC_SP_FT_FPORT) == 0) {
1413 if (e_d_tov > lport->e_d_tov)
1414 lport->e_d_tov = e_d_tov;
1415 lport->r_a_tov = 2 * e_d_tov;
7414705e
RL
1416 printk(KERN_INFO "libfc: Port (%6x) entered "
1417 "point to point mode\n", did);
42e9a92f
RL
1418 fc_lport_ptp_setup(lport, ntoh24(fh->fh_s_id),
1419 get_unaligned_be64(
1420 &flp->fl_wwpn),
1421 get_unaligned_be64(
1422 &flp->fl_wwnn));
1423 } else {
1424 lport->e_d_tov = e_d_tov;
1425 lport->r_a_tov = r_a_tov;
1426 fc_host_fabric_name(lport->host) =
1427 get_unaligned_be64(&flp->fl_wwnn);
1428 fc_lport_enter_dns(lport);
1429 }
1430 }
42e9a92f 1431 } else {
7414705e 1432 FC_LPORT_DBG(lport, "Bad FLOGI response\n");
42e9a92f
RL
1433 }
1434
1435out:
1436 fc_frame_free(fp);
1437err:
1438 mutex_unlock(&lport->lp_mutex);
1439}
11b56188 1440EXPORT_SYMBOL(fc_lport_flogi_resp);
42e9a92f
RL
1441
1442/**
34f42a07 1443 * fc_rport_enter_flogi() - Send a FLOGI request to the fabric manager
42e9a92f
RL
1444 * @lport: Fibre Channel local port to be logged in to the fabric
1445 *
1446 * Locking Note: The lport lock is expected to be held before calling
1447 * this routine.
1448 */
1449void fc_lport_enter_flogi(struct fc_lport *lport)
1450{
1451 struct fc_frame *fp;
1452
7414705e
RL
1453 FC_LPORT_DBG(lport, "Entered FLOGI state from %s state\n",
1454 fc_lport_state(lport));
42e9a92f
RL
1455
1456 fc_lport_state_enter(lport, LPORT_ST_FLOGI);
1457
1458 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
1459 if (!fp)
1460 return fc_lport_error(lport, fp);
1461
db36c06c
CL
1462 if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp,
1463 lport->vport ? ELS_FDISC : ELS_FLOGI,
42e9a92f 1464 fc_lport_flogi_resp, lport, lport->e_d_tov))
8f550f93 1465 fc_lport_error(lport, NULL);
42e9a92f
RL
1466}
1467
1468/* Configure a fc_lport */
1469int fc_lport_config(struct fc_lport *lport)
1470{
1471 INIT_DELAYED_WORK(&lport->retry_work, fc_lport_timeout);
1472 mutex_init(&lport->lp_mutex);
1473
b1d9fd55 1474 fc_lport_state_enter(lport, LPORT_ST_DISABLED);
42e9a92f
RL
1475
1476 fc_lport_add_fc4_type(lport, FC_TYPE_FCP);
1477 fc_lport_add_fc4_type(lport, FC_TYPE_CT);
1478
1479 return 0;
1480}
1481EXPORT_SYMBOL(fc_lport_config);
1482
1483int fc_lport_init(struct fc_lport *lport)
1484{
1485 if (!lport->tt.lport_recv)
1486 lport->tt.lport_recv = fc_lport_recv_req;
1487
1488 if (!lport->tt.lport_reset)
1489 lport->tt.lport_reset = fc_lport_reset;
1490
1491 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
1492 fc_host_node_name(lport->host) = lport->wwnn;
1493 fc_host_port_name(lport->host) = lport->wwpn;
1494 fc_host_supported_classes(lport->host) = FC_COS_CLASS3;
1495 memset(fc_host_supported_fc4s(lport->host), 0,
1496 sizeof(fc_host_supported_fc4s(lport->host)));
1497 fc_host_supported_fc4s(lport->host)[2] = 1;
1498 fc_host_supported_fc4s(lport->host)[7] = 1;
1499
1500 /* This value is also unchanging */
1501 memset(fc_host_active_fc4s(lport->host), 0,
1502 sizeof(fc_host_active_fc4s(lport->host)));
1503 fc_host_active_fc4s(lport->host)[2] = 1;
1504 fc_host_active_fc4s(lport->host)[7] = 1;
1505 fc_host_maxframe_size(lport->host) = lport->mfs;
1506 fc_host_supported_speeds(lport->host) = 0;
1507 if (lport->link_supported_speeds & FC_PORTSPEED_1GBIT)
1508 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_1GBIT;
1509 if (lport->link_supported_speeds & FC_PORTSPEED_10GBIT)
1510 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_10GBIT;
1511
1512 return 0;
1513}
1514EXPORT_SYMBOL(fc_lport_init);