[SCSI] libfc: Register Symbolic Node Name (RSNN_NN)
[safe/jmp/linux-2.6] / drivers / scsi / libfc / fc_lport.c
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
98 #include "fc_libfc.h"
99
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
106 static void fc_lport_error(struct fc_lport *, struct fc_frame *);
107
108 static void fc_lport_enter_reset(struct fc_lport *);
109 static void fc_lport_enter_flogi(struct fc_lport *);
110 static void fc_lport_enter_dns(struct fc_lport *);
111 static void fc_lport_enter_rnn_id(struct fc_lport *);
112 static void fc_lport_enter_rsnn_nn(struct fc_lport *);
113 static void fc_lport_enter_rft_id(struct fc_lport *);
114 static void fc_lport_enter_scr(struct fc_lport *);
115 static void fc_lport_enter_ready(struct fc_lport *);
116 static void fc_lport_enter_logo(struct fc_lport *);
117
118 static const char *fc_lport_state_names[] = {
119         [LPORT_ST_DISABLED] = "disabled",
120         [LPORT_ST_FLOGI] =    "FLOGI",
121         [LPORT_ST_DNS] =      "dNS",
122         [LPORT_ST_RNN_ID] =   "RNN_ID",
123         [LPORT_ST_RSNN_NN] =  "RSNN_NN",
124         [LPORT_ST_RFT_ID] =   "RFT_ID",
125         [LPORT_ST_SCR] =      "SCR",
126         [LPORT_ST_READY] =    "Ready",
127         [LPORT_ST_LOGO] =     "LOGO",
128         [LPORT_ST_RESET] =    "reset",
129 };
130
131 static int fc_frame_drop(struct fc_lport *lport, struct fc_frame *fp)
132 {
133         fc_frame_free(fp);
134         return 0;
135 }
136
137 /**
138  * fc_lport_rport_callback() - Event handler for rport events
139  * @lport: The lport which is receiving the event
140  * @rdata: private remote port data
141  * @event: The event that occured
142  *
143  * Locking Note: The rport lock should not be held when calling
144  *               this function.
145  */
146 static void fc_lport_rport_callback(struct fc_lport *lport,
147                                     struct fc_rport_priv *rdata,
148                                     enum fc_rport_event event)
149 {
150         FC_LPORT_DBG(lport, "Received a %d event for port (%6x)\n", event,
151                      rdata->ids.port_id);
152
153         mutex_lock(&lport->lp_mutex);
154         switch (event) {
155         case RPORT_EV_READY:
156                 if (lport->state == LPORT_ST_DNS) {
157                         lport->dns_rp = rdata;
158                         fc_lport_enter_rnn_id(lport);
159                 } else {
160                         FC_LPORT_DBG(lport, "Received an READY event "
161                                      "on port (%6x) for the directory "
162                                      "server, but the lport is not "
163                                      "in the DNS state, it's in the "
164                                      "%d state", rdata->ids.port_id,
165                                      lport->state);
166                         lport->tt.rport_logoff(rdata);
167                 }
168                 break;
169         case RPORT_EV_LOGO:
170         case RPORT_EV_FAILED:
171         case RPORT_EV_STOP:
172                 lport->dns_rp = NULL;
173                 break;
174         case RPORT_EV_NONE:
175                 break;
176         }
177         mutex_unlock(&lport->lp_mutex);
178 }
179
180 /**
181  * fc_lport_state() - Return a string which represents the lport's state
182  * @lport: The lport whose state is to converted to a string
183  */
184 static const char *fc_lport_state(struct fc_lport *lport)
185 {
186         const char *cp;
187
188         cp = fc_lport_state_names[lport->state];
189         if (!cp)
190                 cp = "unknown";
191         return cp;
192 }
193
194 /**
195  * fc_lport_ptp_setup() - Create an rport for point-to-point mode
196  * @lport: The lport to attach the ptp rport to
197  * @fid: The FID of the ptp rport
198  * @remote_wwpn: The WWPN of the ptp rport
199  * @remote_wwnn: The WWNN of the ptp rport
200  */
201 static void fc_lport_ptp_setup(struct fc_lport *lport,
202                                u32 remote_fid, u64 remote_wwpn,
203                                u64 remote_wwnn)
204 {
205         mutex_lock(&lport->disc.disc_mutex);
206         if (lport->ptp_rp)
207                 lport->tt.rport_logoff(lport->ptp_rp);
208         lport->ptp_rp = lport->tt.rport_create(lport, remote_fid);
209         lport->ptp_rp->ids.port_name = remote_wwpn;
210         lport->ptp_rp->ids.node_name = remote_wwnn;
211         mutex_unlock(&lport->disc.disc_mutex);
212
213         lport->tt.rport_login(lport->ptp_rp);
214
215         fc_lport_enter_ready(lport);
216 }
217
218 void fc_get_host_port_type(struct Scsi_Host *shost)
219 {
220         /* TODO - currently just NPORT */
221         fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
222 }
223 EXPORT_SYMBOL(fc_get_host_port_type);
224
225 void fc_get_host_port_state(struct Scsi_Host *shost)
226 {
227         struct fc_lport *lp = shost_priv(shost);
228
229         mutex_lock(&lp->lp_mutex);
230         if (!lp->link_up)
231                 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
232         else
233                 switch (lp->state) {
234                 case LPORT_ST_READY:
235                         fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
236                         break;
237                 default:
238                         fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
239                 }
240         mutex_unlock(&lp->lp_mutex);
241 }
242 EXPORT_SYMBOL(fc_get_host_port_state);
243
244 void fc_get_host_speed(struct Scsi_Host *shost)
245 {
246         struct fc_lport *lport = shost_priv(shost);
247
248         fc_host_speed(shost) = lport->link_speed;
249 }
250 EXPORT_SYMBOL(fc_get_host_speed);
251
252 struct fc_host_statistics *fc_get_host_stats(struct Scsi_Host *shost)
253 {
254         struct fc_host_statistics *fcoe_stats;
255         struct fc_lport *lp = shost_priv(shost);
256         struct timespec v0, v1;
257         unsigned int cpu;
258
259         fcoe_stats = &lp->host_stats;
260         memset(fcoe_stats, 0, sizeof(struct fc_host_statistics));
261
262         jiffies_to_timespec(jiffies, &v0);
263         jiffies_to_timespec(lp->boot_time, &v1);
264         fcoe_stats->seconds_since_last_reset = (v0.tv_sec - v1.tv_sec);
265
266         for_each_possible_cpu(cpu) {
267                 struct fcoe_dev_stats *stats;
268
269                 stats = per_cpu_ptr(lp->dev_stats, cpu);
270
271                 fcoe_stats->tx_frames += stats->TxFrames;
272                 fcoe_stats->tx_words += stats->TxWords;
273                 fcoe_stats->rx_frames += stats->RxFrames;
274                 fcoe_stats->rx_words += stats->RxWords;
275                 fcoe_stats->error_frames += stats->ErrorFrames;
276                 fcoe_stats->invalid_crc_count += stats->InvalidCRCCount;
277                 fcoe_stats->fcp_input_requests += stats->InputRequests;
278                 fcoe_stats->fcp_output_requests += stats->OutputRequests;
279                 fcoe_stats->fcp_control_requests += stats->ControlRequests;
280                 fcoe_stats->fcp_input_megabytes += stats->InputMegabytes;
281                 fcoe_stats->fcp_output_megabytes += stats->OutputMegabytes;
282                 fcoe_stats->link_failure_count += stats->LinkFailureCount;
283         }
284         fcoe_stats->lip_count = -1;
285         fcoe_stats->nos_count = -1;
286         fcoe_stats->loss_of_sync_count = -1;
287         fcoe_stats->loss_of_signal_count = -1;
288         fcoe_stats->prim_seq_protocol_err_count = -1;
289         fcoe_stats->dumped_frames = -1;
290         return fcoe_stats;
291 }
292 EXPORT_SYMBOL(fc_get_host_stats);
293
294 /*
295  * Fill in FLOGI command for request.
296  */
297 static void
298 fc_lport_flogi_fill(struct fc_lport *lport, struct fc_els_flogi *flogi,
299                     unsigned int op)
300 {
301         struct fc_els_csp *sp;
302         struct fc_els_cssp *cp;
303
304         memset(flogi, 0, sizeof(*flogi));
305         flogi->fl_cmd = (u8) op;
306         put_unaligned_be64(lport->wwpn, &flogi->fl_wwpn);
307         put_unaligned_be64(lport->wwnn, &flogi->fl_wwnn);
308         sp = &flogi->fl_csp;
309         sp->sp_hi_ver = 0x20;
310         sp->sp_lo_ver = 0x20;
311         sp->sp_bb_cred = htons(10);     /* this gets set by gateway */
312         sp->sp_bb_data = htons((u16) lport->mfs);
313         cp = &flogi->fl_cssp[3 - 1];    /* class 3 parameters */
314         cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
315         if (op != ELS_FLOGI) {
316                 sp->sp_features = htons(FC_SP_FT_CIRO);
317                 sp->sp_tot_seq = htons(255);    /* seq. we accept */
318                 sp->sp_rel_off = htons(0x1f);
319                 sp->sp_e_d_tov = htonl(lport->e_d_tov);
320
321                 cp->cp_rdfs = htons((u16) lport->mfs);
322                 cp->cp_con_seq = htons(255);
323                 cp->cp_open_seq = 1;
324         }
325 }
326
327 /*
328  * Add a supported FC-4 type.
329  */
330 static void fc_lport_add_fc4_type(struct fc_lport *lport, enum fc_fh_type type)
331 {
332         __be32 *mp;
333
334         mp = &lport->fcts.ff_type_map[type / FC_NS_BPW];
335         *mp = htonl(ntohl(*mp) | 1UL << (type % FC_NS_BPW));
336 }
337
338 /**
339  * fc_lport_recv_rlir_req() - Handle received Registered Link Incident Report.
340  * @lport: Fibre Channel local port recieving the RLIR
341  * @sp: current sequence in the RLIR exchange
342  * @fp: RLIR request frame
343  *
344  * Locking Note: The lport lock is expected to be held before calling
345  * this function.
346  */
347 static void fc_lport_recv_rlir_req(struct fc_seq *sp, struct fc_frame *fp,
348                                    struct fc_lport *lport)
349 {
350         FC_LPORT_DBG(lport, "Received RLIR request while in state %s\n",
351                      fc_lport_state(lport));
352
353         lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
354         fc_frame_free(fp);
355 }
356
357 /**
358  * fc_lport_recv_echo_req() - Handle received ECHO request
359  * @lport: Fibre Channel local port recieving the ECHO
360  * @sp: current sequence in the ECHO exchange
361  * @fp: ECHO request frame
362  *
363  * Locking Note: The lport lock is expected to be held before calling
364  * this function.
365  */
366 static void fc_lport_recv_echo_req(struct fc_seq *sp, struct fc_frame *in_fp,
367                                    struct fc_lport *lport)
368 {
369         struct fc_frame *fp;
370         struct fc_exch *ep = fc_seq_exch(sp);
371         unsigned int len;
372         void *pp;
373         void *dp;
374         u32 f_ctl;
375
376         FC_LPORT_DBG(lport, "Received ECHO request while in state %s\n",
377                      fc_lport_state(lport));
378
379         len = fr_len(in_fp) - sizeof(struct fc_frame_header);
380         pp = fc_frame_payload_get(in_fp, len);
381
382         if (len < sizeof(__be32))
383                 len = sizeof(__be32);
384
385         fp = fc_frame_alloc(lport, len);
386         if (fp) {
387                 dp = fc_frame_payload_get(fp, len);
388                 memcpy(dp, pp, len);
389                 *((__be32 *)dp) = htonl(ELS_LS_ACC << 24);
390                 sp = lport->tt.seq_start_next(sp);
391                 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
392                 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
393                                FC_TYPE_ELS, f_ctl, 0);
394                 lport->tt.seq_send(lport, sp, fp);
395         }
396         fc_frame_free(in_fp);
397 }
398
399 /**
400  * fc_lport_recv_rnid_req() - Handle received Request Node ID data request
401  * @sp:    The sequence in the RNID exchange
402  * @fp:    The RNID request frame
403  * @lport: The local port recieving the RNID
404  *
405  * Locking Note: The lport lock is expected to be held before calling
406  * this function.
407  */
408 static void fc_lport_recv_rnid_req(struct fc_seq *sp, struct fc_frame *in_fp,
409                                    struct fc_lport *lport)
410 {
411         struct fc_frame *fp;
412         struct fc_exch *ep = fc_seq_exch(sp);
413         struct fc_els_rnid *req;
414         struct {
415                 struct fc_els_rnid_resp rnid;
416                 struct fc_els_rnid_cid cid;
417                 struct fc_els_rnid_gen gen;
418         } *rp;
419         struct fc_seq_els_data rjt_data;
420         u8 fmt;
421         size_t len;
422         u32 f_ctl;
423
424         FC_LPORT_DBG(lport, "Received RNID request while in state %s\n",
425                      fc_lport_state(lport));
426
427         req = fc_frame_payload_get(in_fp, sizeof(*req));
428         if (!req) {
429                 rjt_data.fp = NULL;
430                 rjt_data.reason = ELS_RJT_LOGIC;
431                 rjt_data.explan = ELS_EXPL_NONE;
432                 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
433         } else {
434                 fmt = req->rnid_fmt;
435                 len = sizeof(*rp);
436                 if (fmt != ELS_RNIDF_GEN ||
437                     ntohl(lport->rnid_gen.rnid_atype) == 0) {
438                         fmt = ELS_RNIDF_NONE;   /* nothing to provide */
439                         len -= sizeof(rp->gen);
440                 }
441                 fp = fc_frame_alloc(lport, len);
442                 if (fp) {
443                         rp = fc_frame_payload_get(fp, len);
444                         memset(rp, 0, len);
445                         rp->rnid.rnid_cmd = ELS_LS_ACC;
446                         rp->rnid.rnid_fmt = fmt;
447                         rp->rnid.rnid_cid_len = sizeof(rp->cid);
448                         rp->cid.rnid_wwpn = htonll(lport->wwpn);
449                         rp->cid.rnid_wwnn = htonll(lport->wwnn);
450                         if (fmt == ELS_RNIDF_GEN) {
451                                 rp->rnid.rnid_sid_len = sizeof(rp->gen);
452                                 memcpy(&rp->gen, &lport->rnid_gen,
453                                        sizeof(rp->gen));
454                         }
455                         sp = lport->tt.seq_start_next(sp);
456                         f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
457                         f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
458                         fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
459                                        FC_TYPE_ELS, f_ctl, 0);
460                         lport->tt.seq_send(lport, sp, fp);
461                 }
462         }
463         fc_frame_free(in_fp);
464 }
465
466 /**
467  * fc_lport_recv_logo_req() - Handle received fabric LOGO request
468  * @lport: Fibre Channel local port recieving the LOGO
469  * @sp: current sequence in the LOGO exchange
470  * @fp: LOGO request frame
471  *
472  * Locking Note: The lport lock is exected to be held before calling
473  * this function.
474  */
475 static void fc_lport_recv_logo_req(struct fc_seq *sp, struct fc_frame *fp,
476                                    struct fc_lport *lport)
477 {
478         lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
479         fc_lport_enter_reset(lport);
480         fc_frame_free(fp);
481 }
482
483 /**
484  * fc_fabric_login() - Start the lport state machine
485  * @lport: The lport that should log into the fabric
486  *
487  * Locking Note: This function should not be called
488  *               with the lport lock held.
489  */
490 int fc_fabric_login(struct fc_lport *lport)
491 {
492         int rc = -1;
493
494         mutex_lock(&lport->lp_mutex);
495         if (lport->state == LPORT_ST_DISABLED) {
496                 fc_lport_enter_reset(lport);
497                 rc = 0;
498         }
499         mutex_unlock(&lport->lp_mutex);
500
501         return rc;
502 }
503 EXPORT_SYMBOL(fc_fabric_login);
504
505 /**
506  * __fc_linkup() - Handler for transport linkup events
507  * @lport: The lport whose link is up
508  *
509  * Locking: must be called with the lp_mutex held
510  */
511 void __fc_linkup(struct fc_lport *lport)
512 {
513         if (!lport->link_up) {
514                 lport->link_up = 1;
515
516                 if (lport->state == LPORT_ST_RESET)
517                         fc_lport_enter_flogi(lport);
518         }
519 }
520
521 /**
522  * fc_linkup() - Handler for transport linkup events
523  * @lport: The lport whose link is up
524  */
525 void fc_linkup(struct fc_lport *lport)
526 {
527         printk(KERN_INFO "libfc: Link up on port (%6x)\n",
528                fc_host_port_id(lport->host));
529
530         mutex_lock(&lport->lp_mutex);
531         __fc_linkup(lport);
532         mutex_unlock(&lport->lp_mutex);
533 }
534 EXPORT_SYMBOL(fc_linkup);
535
536 /**
537  * __fc_linkdown() - Handler for transport linkdown events
538  * @lport: The lport whose link is down
539  *
540  * Locking: must be called with the lp_mutex held
541  */
542 void __fc_linkdown(struct fc_lport *lport)
543 {
544         if (lport->link_up) {
545                 lport->link_up = 0;
546                 fc_lport_enter_reset(lport);
547                 lport->tt.fcp_cleanup(lport);
548         }
549 }
550
551 /**
552  * fc_linkdown() - Handler for transport linkdown events
553  * @lport: The lport whose link is down
554  */
555 void fc_linkdown(struct fc_lport *lport)
556 {
557         printk(KERN_INFO "libfc: Link down on port (%6x)\n",
558                fc_host_port_id(lport->host));
559
560         mutex_lock(&lport->lp_mutex);
561         __fc_linkdown(lport);
562         mutex_unlock(&lport->lp_mutex);
563 }
564 EXPORT_SYMBOL(fc_linkdown);
565
566 /**
567  * fc_fabric_logoff() - Logout of the fabric
568  * @lport:            fc_lport pointer to logoff the fabric
569  *
570  * Return value:
571  *      0 for success, -1 for failure
572  */
573 int fc_fabric_logoff(struct fc_lport *lport)
574 {
575         lport->tt.disc_stop_final(lport);
576         mutex_lock(&lport->lp_mutex);
577         if (lport->dns_rp)
578                 lport->tt.rport_logoff(lport->dns_rp);
579         mutex_unlock(&lport->lp_mutex);
580         lport->tt.rport_flush_queue();
581         mutex_lock(&lport->lp_mutex);
582         fc_lport_enter_logo(lport);
583         mutex_unlock(&lport->lp_mutex);
584         cancel_delayed_work_sync(&lport->retry_work);
585         return 0;
586 }
587 EXPORT_SYMBOL(fc_fabric_logoff);
588
589 /**
590  * fc_lport_destroy() - unregister a fc_lport
591  * @lport:            fc_lport pointer to unregister
592  *
593  * Return value:
594  *      None
595  * Note:
596  * exit routine for fc_lport instance
597  * clean-up all the allocated memory
598  * and free up other system resources.
599  *
600  */
601 int fc_lport_destroy(struct fc_lport *lport)
602 {
603         mutex_lock(&lport->lp_mutex);
604         lport->state = LPORT_ST_DISABLED;
605         lport->link_up = 0;
606         lport->tt.frame_send = fc_frame_drop;
607         mutex_unlock(&lport->lp_mutex);
608
609         lport->tt.fcp_abort_io(lport);
610         lport->tt.disc_stop_final(lport);
611         lport->tt.exch_mgr_reset(lport, 0, 0);
612         return 0;
613 }
614 EXPORT_SYMBOL(fc_lport_destroy);
615
616 /**
617  * fc_set_mfs() - sets up the mfs for the corresponding fc_lport
618  * @lport: fc_lport pointer to unregister
619  * @mfs: the new mfs for fc_lport
620  *
621  * Set mfs for the given fc_lport to the new mfs.
622  *
623  * Return: 0 for success
624  */
625 int fc_set_mfs(struct fc_lport *lport, u32 mfs)
626 {
627         unsigned int old_mfs;
628         int rc = -EINVAL;
629
630         mutex_lock(&lport->lp_mutex);
631
632         old_mfs = lport->mfs;
633
634         if (mfs >= FC_MIN_MAX_FRAME) {
635                 mfs &= ~3;
636                 if (mfs > FC_MAX_FRAME)
637                         mfs = FC_MAX_FRAME;
638                 mfs -= sizeof(struct fc_frame_header);
639                 lport->mfs = mfs;
640                 rc = 0;
641         }
642
643         if (!rc && mfs < old_mfs)
644                 fc_lport_enter_reset(lport);
645
646         mutex_unlock(&lport->lp_mutex);
647
648         return rc;
649 }
650 EXPORT_SYMBOL(fc_set_mfs);
651
652 /**
653  * fc_lport_disc_callback() - Callback for discovery events
654  * @lport: FC local port
655  * @event: The discovery event
656  */
657 void fc_lport_disc_callback(struct fc_lport *lport, enum fc_disc_event event)
658 {
659         switch (event) {
660         case DISC_EV_SUCCESS:
661                 FC_LPORT_DBG(lport, "Discovery succeeded\n");
662                 break;
663         case DISC_EV_FAILED:
664                 printk(KERN_ERR "libfc: Discovery failed for port (%6x)\n",
665                        fc_host_port_id(lport->host));
666                 mutex_lock(&lport->lp_mutex);
667                 fc_lport_enter_reset(lport);
668                 mutex_unlock(&lport->lp_mutex);
669                 break;
670         case DISC_EV_NONE:
671                 WARN_ON(1);
672                 break;
673         }
674 }
675
676 /**
677  * fc_rport_enter_ready() - Enter the ready state and start discovery
678  * @lport: Fibre Channel local port that is ready
679  *
680  * Locking Note: The lport lock is expected to be held before calling
681  * this routine.
682  */
683 static void fc_lport_enter_ready(struct fc_lport *lport)
684 {
685         FC_LPORT_DBG(lport, "Entered READY from state %s\n",
686                      fc_lport_state(lport));
687
688         fc_lport_state_enter(lport, LPORT_ST_READY);
689         if (lport->vport)
690                 fc_vport_set_state(lport->vport, FC_VPORT_ACTIVE);
691         fc_vports_linkchange(lport);
692
693         if (!lport->ptp_rp)
694                 lport->tt.disc_start(fc_lport_disc_callback, lport);
695 }
696
697 /**
698  * fc_lport_recv_flogi_req() - Receive a FLOGI request
699  * @sp_in: The sequence the FLOGI is on
700  * @rx_fp: The frame the FLOGI is in
701  * @lport: The lport that recieved the request
702  *
703  * A received FLOGI request indicates a point-to-point connection.
704  * Accept it with the common service parameters indicating our N port.
705  * Set up to do a PLOGI if we have the higher-number WWPN.
706  *
707  * Locking Note: The lport lock is expected to be held before calling
708  * this function.
709  */
710 static void fc_lport_recv_flogi_req(struct fc_seq *sp_in,
711                                     struct fc_frame *rx_fp,
712                                     struct fc_lport *lport)
713 {
714         struct fc_frame *fp;
715         struct fc_frame_header *fh;
716         struct fc_seq *sp;
717         struct fc_exch *ep;
718         struct fc_els_flogi *flp;
719         struct fc_els_flogi *new_flp;
720         u64 remote_wwpn;
721         u32 remote_fid;
722         u32 local_fid;
723         u32 f_ctl;
724
725         FC_LPORT_DBG(lport, "Received FLOGI request while in state %s\n",
726                      fc_lport_state(lport));
727
728         fh = fc_frame_header_get(rx_fp);
729         remote_fid = ntoh24(fh->fh_s_id);
730         flp = fc_frame_payload_get(rx_fp, sizeof(*flp));
731         if (!flp)
732                 goto out;
733         remote_wwpn = get_unaligned_be64(&flp->fl_wwpn);
734         if (remote_wwpn == lport->wwpn) {
735                 printk(KERN_WARNING "libfc: Received FLOGI from port "
736                        "with same WWPN %llx\n", remote_wwpn);
737                 goto out;
738         }
739         FC_LPORT_DBG(lport, "FLOGI from port WWPN %llx\n", remote_wwpn);
740
741         /*
742          * XXX what is the right thing to do for FIDs?
743          * The originator might expect our S_ID to be 0xfffffe.
744          * But if so, both of us could end up with the same FID.
745          */
746         local_fid = FC_LOCAL_PTP_FID_LO;
747         if (remote_wwpn < lport->wwpn) {
748                 local_fid = FC_LOCAL_PTP_FID_HI;
749                 if (!remote_fid || remote_fid == local_fid)
750                         remote_fid = FC_LOCAL_PTP_FID_LO;
751         } else if (!remote_fid) {
752                 remote_fid = FC_LOCAL_PTP_FID_HI;
753         }
754
755         fc_host_port_id(lport->host) = local_fid;
756
757         fp = fc_frame_alloc(lport, sizeof(*flp));
758         if (fp) {
759                 sp = lport->tt.seq_start_next(fr_seq(rx_fp));
760                 new_flp = fc_frame_payload_get(fp, sizeof(*flp));
761                 fc_lport_flogi_fill(lport, new_flp, ELS_FLOGI);
762                 new_flp->fl_cmd = (u8) ELS_LS_ACC;
763
764                 /*
765                  * Send the response.  If this fails, the originator should
766                  * repeat the sequence.
767                  */
768                 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
769                 ep = fc_seq_exch(sp);
770                 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
771                                FC_TYPE_ELS, f_ctl, 0);
772                 lport->tt.seq_send(lport, sp, fp);
773
774         } else {
775                 fc_lport_error(lport, fp);
776         }
777         fc_lport_ptp_setup(lport, remote_fid, remote_wwpn,
778                            get_unaligned_be64(&flp->fl_wwnn));
779
780 out:
781         sp = fr_seq(rx_fp);
782         fc_frame_free(rx_fp);
783 }
784
785 /**
786  * fc_lport_recv_req() - The generic lport request handler
787  * @lport: The lport that received the request
788  * @sp: The sequence the request is on
789  * @fp: The frame the request is in
790  *
791  * This function will see if the lport handles the request or
792  * if an rport should handle the request.
793  *
794  * Locking Note: This function should not be called with the lport
795  *               lock held becuase it will grab the lock.
796  */
797 static void fc_lport_recv_req(struct fc_lport *lport, struct fc_seq *sp,
798                               struct fc_frame *fp)
799 {
800         struct fc_frame_header *fh = fc_frame_header_get(fp);
801         void (*recv) (struct fc_seq *, struct fc_frame *, struct fc_lport *);
802
803         mutex_lock(&lport->lp_mutex);
804
805         /*
806          * Handle special ELS cases like FLOGI, LOGO, and
807          * RSCN here.  These don't require a session.
808          * Even if we had a session, it might not be ready.
809          */
810         if (!lport->link_up)
811                 fc_frame_free(fp);
812         else if (fh->fh_type == FC_TYPE_ELS &&
813                  fh->fh_r_ctl == FC_RCTL_ELS_REQ) {
814                 /*
815                  * Check opcode.
816                  */
817                 recv = lport->tt.rport_recv_req;
818                 switch (fc_frame_payload_op(fp)) {
819                 case ELS_FLOGI:
820                         recv = fc_lport_recv_flogi_req;
821                         break;
822                 case ELS_LOGO:
823                         fh = fc_frame_header_get(fp);
824                         if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI)
825                                 recv = fc_lport_recv_logo_req;
826                         break;
827                 case ELS_RSCN:
828                         recv = lport->tt.disc_recv_req;
829                         break;
830                 case ELS_ECHO:
831                         recv = fc_lport_recv_echo_req;
832                         break;
833                 case ELS_RLIR:
834                         recv = fc_lport_recv_rlir_req;
835                         break;
836                 case ELS_RNID:
837                         recv = fc_lport_recv_rnid_req;
838                         break;
839                 }
840
841                 recv(sp, fp, lport);
842         } else {
843                 FC_LPORT_DBG(lport, "dropping invalid frame (eof %x)\n",
844                              fr_eof(fp));
845                 fc_frame_free(fp);
846         }
847         mutex_unlock(&lport->lp_mutex);
848
849         /*
850          *  The common exch_done for all request may not be good
851          *  if any request requires longer hold on exhange. XXX
852          */
853         lport->tt.exch_done(sp);
854 }
855
856 /**
857  * fc_lport_reset() - Reset an lport
858  * @lport: The lport which should be reset
859  *
860  * Locking Note: This functions should not be called with the
861  *               lport lock held.
862  */
863 int fc_lport_reset(struct fc_lport *lport)
864 {
865         cancel_delayed_work_sync(&lport->retry_work);
866         mutex_lock(&lport->lp_mutex);
867         fc_lport_enter_reset(lport);
868         mutex_unlock(&lport->lp_mutex);
869         return 0;
870 }
871 EXPORT_SYMBOL(fc_lport_reset);
872
873 /**
874  * fc_lport_reset_locked() - Reset the local port
875  * @lport: Fibre Channel local port to be reset
876  *
877  * Locking Note: The lport lock is expected to be held before calling
878  * this routine.
879  */
880 static void fc_lport_reset_locked(struct fc_lport *lport)
881 {
882         if (lport->dns_rp)
883                 lport->tt.rport_logoff(lport->dns_rp);
884
885         lport->ptp_rp = NULL;
886
887         lport->tt.disc_stop(lport);
888
889         lport->tt.exch_mgr_reset(lport, 0, 0);
890         fc_host_fabric_name(lport->host) = 0;
891         fc_host_port_id(lport->host) = 0;
892 }
893
894 /**
895  * fc_lport_enter_reset() - Reset the local port
896  * @lport: Fibre Channel local port to be reset
897  *
898  * Locking Note: The lport lock is expected to be held before calling
899  * this routine.
900  */
901 static void fc_lport_enter_reset(struct fc_lport *lport)
902 {
903         FC_LPORT_DBG(lport, "Entered RESET state from %s state\n",
904                      fc_lport_state(lport));
905
906         if (lport->vport) {
907                 if (lport->link_up)
908                         fc_vport_set_state(lport->vport, FC_VPORT_INITIALIZING);
909                 else
910                         fc_vport_set_state(lport->vport, FC_VPORT_LINKDOWN);
911         }
912         fc_lport_state_enter(lport, LPORT_ST_RESET);
913         fc_vports_linkchange(lport);
914         fc_lport_reset_locked(lport);
915         if (lport->link_up)
916                 fc_lport_enter_flogi(lport);
917 }
918
919 /**
920  * fc_lport_enter_disabled() - disable the local port
921  * @lport: Fibre Channel local port to be reset
922  *
923  * Locking Note: The lport lock is expected to be held before calling
924  * this routine.
925  */
926 static void fc_lport_enter_disabled(struct fc_lport *lport)
927 {
928         FC_LPORT_DBG(lport, "Entered disabled state from %s state\n",
929                      fc_lport_state(lport));
930
931         fc_lport_state_enter(lport, LPORT_ST_DISABLED);
932         fc_vports_linkchange(lport);
933         fc_lport_reset_locked(lport);
934 }
935
936 /**
937  * fc_lport_error() - Handler for any errors
938  * @lport: The fc_lport object
939  * @fp: The frame pointer
940  *
941  * If the error was caused by a resource allocation failure
942  * then wait for half a second and retry, otherwise retry
943  * after the e_d_tov time.
944  */
945 static void fc_lport_error(struct fc_lport *lport, struct fc_frame *fp)
946 {
947         unsigned long delay = 0;
948         FC_LPORT_DBG(lport, "Error %ld in state %s, retries %d\n",
949                      PTR_ERR(fp), fc_lport_state(lport),
950                      lport->retry_count);
951
952         if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
953                 /*
954                  * Memory allocation failure, or the exchange timed out.
955                  *  Retry after delay
956                  */
957                 if (lport->retry_count < lport->max_retry_count) {
958                         lport->retry_count++;
959                         if (!fp)
960                                 delay = msecs_to_jiffies(500);
961                         else
962                                 delay = msecs_to_jiffies(lport->e_d_tov);
963
964                         schedule_delayed_work(&lport->retry_work, delay);
965                 } else {
966                         switch (lport->state) {
967                         case LPORT_ST_DISABLED:
968                         case LPORT_ST_READY:
969                         case LPORT_ST_RESET:
970                         case LPORT_ST_RNN_ID:
971                         case LPORT_ST_RSNN_NN:
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 /**
985  * fc_lport_rft_id_resp() - Handle response to Register Fibre
986  *                          Channel Types by ID (RFT_ID) request
987  * @sp: current sequence in RFT_ID exchange
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  */
995 static void fc_lport_rft_id_resp(struct fc_seq *sp, struct fc_frame *fp,
996                                  void *lp_arg)
997 {
998         struct fc_lport *lport = lp_arg;
999         struct fc_frame_header *fh;
1000         struct fc_ct_hdr *ct;
1001
1002         FC_LPORT_DBG(lport, "Received a RFT_ID %s\n", fc_els_resp_type(fp));
1003
1004         if (fp == ERR_PTR(-FC_EX_CLOSED))
1005                 return;
1006
1007         mutex_lock(&lport->lp_mutex);
1008
1009         if (lport->state != LPORT_ST_RFT_ID) {
1010                 FC_LPORT_DBG(lport, "Received a RFT_ID response, but in state "
1011                              "%s\n", fc_lport_state(lport));
1012                 if (IS_ERR(fp))
1013                         goto err;
1014                 goto out;
1015         }
1016
1017         if (IS_ERR(fp)) {
1018                 fc_lport_error(lport, fp);
1019                 goto err;
1020         }
1021
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)
1029                 fc_lport_enter_scr(lport);
1030         else
1031                 fc_lport_error(lport, fp);
1032 out:
1033         fc_frame_free(fp);
1034 err:
1035         mutex_unlock(&lport->lp_mutex);
1036 }
1037
1038 /**
1039  * fc_lport_rsnn_nn_resp() - Handle response to Register Symbolic Node Name
1040  *                           by Node Name (RSNN_NN) request
1041  * @sp: current sequence in RSNN_NN exchange
1042  * @fp: response frame
1043  * @lp_arg: Fibre Channel host port instance
1044  *
1045  * Locking Note: This function will be called without the lport lock
1046  * held, but it will lock, call an _enter_* function or fc_lport_error
1047  * and then unlock the lport.
1048  */
1049 static void fc_lport_rsnn_nn_resp(struct fc_seq *sp, struct fc_frame *fp,
1050                                   void *lp_arg)
1051 {
1052         struct fc_lport *lport = lp_arg;
1053         struct fc_frame_header *fh;
1054         struct fc_ct_hdr *ct;
1055
1056         FC_LPORT_DBG(lport, "Received a RSNN_NN %s\n", fc_els_resp_type(fp));
1057
1058         if (fp == ERR_PTR(-FC_EX_CLOSED))
1059                 return;
1060
1061         mutex_lock(&lport->lp_mutex);
1062
1063         if (lport->state != LPORT_ST_RSNN_NN) {
1064                 FC_LPORT_DBG(lport, "Received a RSNN_NN response, but in state "
1065                              "%s\n", fc_lport_state(lport));
1066                 if (IS_ERR(fp))
1067                         goto err;
1068                 goto out;
1069         }
1070
1071         if (IS_ERR(fp)) {
1072                 fc_lport_error(lport, fp);
1073                 goto err;
1074         }
1075
1076         fh = fc_frame_header_get(fp);
1077         ct = fc_frame_payload_get(fp, sizeof(*ct));
1078         if (fh && ct && fh->fh_type == FC_TYPE_CT &&
1079             ct->ct_fs_type == FC_FST_DIR &&
1080             ct->ct_fs_subtype == FC_NS_SUBTYPE &&
1081             ntohs(ct->ct_cmd) == FC_FS_ACC)
1082                 fc_lport_enter_rsnn_nn(lport);
1083         else
1084                 fc_lport_error(lport, fp);
1085
1086 out:
1087         fc_frame_free(fp);
1088 err:
1089         mutex_unlock(&lport->lp_mutex);
1090 }
1091
1092 /**
1093  * fc_lport_rnn_id_resp() - Handle response to Register Node
1094  *                          Name by ID (RNN_ID) request
1095  * @sp: current sequence in RNN_ID exchange
1096  * @fp: response frame
1097  * @lp_arg: Fibre Channel host port instance
1098  *
1099  * Locking Note: This function will be called without the lport lock
1100  * held, but it will lock, call an _enter_* function or fc_lport_error
1101  * and then unlock the lport.
1102  */
1103 static void fc_lport_rnn_id_resp(struct fc_seq *sp, struct fc_frame *fp,
1104                                  void *lp_arg)
1105 {
1106         struct fc_lport *lport = lp_arg;
1107         struct fc_frame_header *fh;
1108         struct fc_ct_hdr *ct;
1109
1110         FC_LPORT_DBG(lport, "Received a RNN_ID %s\n", fc_els_resp_type(fp));
1111
1112         if (fp == ERR_PTR(-FC_EX_CLOSED))
1113                 return;
1114
1115         mutex_lock(&lport->lp_mutex);
1116
1117         if (lport->state != LPORT_ST_RNN_ID) {
1118                 FC_LPORT_DBG(lport, "Received a RNN_ID response, but in state "
1119                              "%s\n", fc_lport_state(lport));
1120                 if (IS_ERR(fp))
1121                         goto err;
1122                 goto out;
1123         }
1124
1125         if (IS_ERR(fp)) {
1126                 fc_lport_error(lport, fp);
1127                 goto err;
1128         }
1129
1130         fh = fc_frame_header_get(fp);
1131         ct = fc_frame_payload_get(fp, sizeof(*ct));
1132         if (fh && ct && fh->fh_type == FC_TYPE_CT &&
1133             ct->ct_fs_type == FC_FST_DIR &&
1134             ct->ct_fs_subtype == FC_NS_SUBTYPE &&
1135             ntohs(ct->ct_cmd) == FC_FS_ACC)
1136                 fc_lport_enter_rft_id(lport);
1137         else
1138                 fc_lport_error(lport, fp);
1139
1140 out:
1141         fc_frame_free(fp);
1142 err:
1143         mutex_unlock(&lport->lp_mutex);
1144 }
1145
1146 /**
1147  * fc_lport_scr_resp() - Handle response to State Change Register (SCR) request
1148  * @sp: current sequence in SCR exchange
1149  * @fp: response frame
1150  * @lp_arg: Fibre Channel lport port instance that sent the registration request
1151  *
1152  * Locking Note: This function will be called without the lport lock
1153  * held, but it will lock, call an _enter_* function or fc_lport_error
1154  * and then unlock the lport.
1155  */
1156 static void fc_lport_scr_resp(struct fc_seq *sp, struct fc_frame *fp,
1157                               void *lp_arg)
1158 {
1159         struct fc_lport *lport = lp_arg;
1160         u8 op;
1161
1162         FC_LPORT_DBG(lport, "Received a SCR %s\n", fc_els_resp_type(fp));
1163
1164         if (fp == ERR_PTR(-FC_EX_CLOSED))
1165                 return;
1166
1167         mutex_lock(&lport->lp_mutex);
1168
1169         if (lport->state != LPORT_ST_SCR) {
1170                 FC_LPORT_DBG(lport, "Received a SCR response, but in state "
1171                              "%s\n", fc_lport_state(lport));
1172                 if (IS_ERR(fp))
1173                         goto err;
1174                 goto out;
1175         }
1176
1177         if (IS_ERR(fp)) {
1178                 fc_lport_error(lport, fp);
1179                 goto err;
1180         }
1181
1182         op = fc_frame_payload_op(fp);
1183         if (op == ELS_LS_ACC)
1184                 fc_lport_enter_ready(lport);
1185         else
1186                 fc_lport_error(lport, fp);
1187
1188 out:
1189         fc_frame_free(fp);
1190 err:
1191         mutex_unlock(&lport->lp_mutex);
1192 }
1193
1194 /**
1195  * fc_lport_enter_scr() - Send a State Change Register (SCR) request
1196  * @lport: Fibre Channel local port to register for state changes
1197  *
1198  * Locking Note: The lport lock is expected to be held before calling
1199  * this routine.
1200  */
1201 static void fc_lport_enter_scr(struct fc_lport *lport)
1202 {
1203         struct fc_frame *fp;
1204
1205         FC_LPORT_DBG(lport, "Entered SCR state from %s state\n",
1206                      fc_lport_state(lport));
1207
1208         fc_lport_state_enter(lport, LPORT_ST_SCR);
1209
1210         fp = fc_frame_alloc(lport, sizeof(struct fc_els_scr));
1211         if (!fp) {
1212                 fc_lport_error(lport, fp);
1213                 return;
1214         }
1215
1216         if (!lport->tt.elsct_send(lport, FC_FID_FCTRL, fp, ELS_SCR,
1217                                   fc_lport_scr_resp, lport, lport->e_d_tov))
1218                 fc_lport_error(lport, NULL);
1219 }
1220
1221 /**
1222  * fc_lport_enter_rft_id() - Register FC4-types with the name server
1223  * @lport: Fibre Channel local port to register
1224  *
1225  * Locking Note: The lport lock is expected to be held before calling
1226  * this routine.
1227  */
1228 static void fc_lport_enter_rft_id(struct fc_lport *lport)
1229 {
1230         struct fc_frame *fp;
1231         struct fc_ns_fts *lps;
1232         int i;
1233
1234         FC_LPORT_DBG(lport, "Entered RFT_ID state from %s state\n",
1235                      fc_lport_state(lport));
1236
1237         fc_lport_state_enter(lport, LPORT_ST_RFT_ID);
1238
1239         lps = &lport->fcts;
1240         i = sizeof(lps->ff_type_map) / sizeof(lps->ff_type_map[0]);
1241         while (--i >= 0)
1242                 if (ntohl(lps->ff_type_map[i]) != 0)
1243                         break;
1244         if (i < 0) {
1245                 /* nothing to register, move on to SCR */
1246                 fc_lport_enter_scr(lport);
1247                 return;
1248         }
1249
1250         fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
1251                             sizeof(struct fc_ns_rft));
1252         if (!fp) {
1253                 fc_lport_error(lport, fp);
1254                 return;
1255         }
1256
1257         if (!lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RFT_ID,
1258                                   fc_lport_rft_id_resp,
1259                                   lport, lport->e_d_tov))
1260                 fc_lport_error(lport, fp);
1261 }
1262
1263 /**
1264  * fc_rport_enter_rsnn_nn() - Register symbolic node name with the name server
1265  * @lport: Fibre Channel local port to register
1266  *
1267  * Locking Note: The lport lock is expected to be held before calling
1268  * this routine.
1269  */
1270 static void fc_lport_enter_rsnn_nn(struct fc_lport *lport)
1271 {
1272         struct fc_frame *fp;
1273         size_t len;
1274
1275         FC_LPORT_DBG(lport, "Entered RSNN_NN state from %s state\n",
1276                      fc_lport_state(lport));
1277
1278         fc_lport_state_enter(lport, LPORT_ST_RSNN_NN);
1279
1280         len = strnlen(fc_host_symbolic_name(lport->host), 255);
1281         fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
1282                             sizeof(struct fc_ns_rsnn) + len);
1283         if (!fp) {
1284                 fc_lport_error(lport, fp);
1285                 return;
1286         }
1287
1288         if (!lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSNN_NN,
1289                                   fc_lport_rsnn_nn_resp,
1290                                   lport, lport->e_d_tov))
1291                 fc_lport_error(lport, fp);
1292 }
1293
1294 /**
1295  * fc_rport_enter_rnn_id() - Register node name with the name server
1296  * @lport: Fibre Channel local port to register
1297  *
1298  * Locking Note: The lport lock is expected to be held before calling
1299  * this routine.
1300  */
1301 static void fc_lport_enter_rnn_id(struct fc_lport *lport)
1302 {
1303         struct fc_frame *fp;
1304
1305         FC_LPORT_DBG(lport, "Entered RNN_ID state from %s state\n",
1306                      fc_lport_state(lport));
1307
1308         fc_lport_state_enter(lport, LPORT_ST_RNN_ID);
1309
1310         fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
1311                             sizeof(struct fc_ns_rn_id));
1312         if (!fp) {
1313                 fc_lport_error(lport, fp);
1314                 return;
1315         }
1316
1317         if (!lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RNN_ID,
1318                                   fc_lport_rnn_id_resp,
1319                                   lport, lport->e_d_tov))
1320                 fc_lport_error(lport, fp);
1321 }
1322
1323 static struct fc_rport_operations fc_lport_rport_ops = {
1324         .event_callback = fc_lport_rport_callback,
1325 };
1326
1327 /**
1328  * fc_rport_enter_dns() - Create a rport to the name server
1329  * @lport: Fibre Channel local port requesting a rport for the name server
1330  *
1331  * Locking Note: The lport lock is expected to be held before calling
1332  * this routine.
1333  */
1334 static void fc_lport_enter_dns(struct fc_lport *lport)
1335 {
1336         struct fc_rport_priv *rdata;
1337
1338         FC_LPORT_DBG(lport, "Entered DNS state from %s state\n",
1339                      fc_lport_state(lport));
1340
1341         fc_lport_state_enter(lport, LPORT_ST_DNS);
1342
1343         mutex_lock(&lport->disc.disc_mutex);
1344         rdata = lport->tt.rport_create(lport, FC_FID_DIR_SERV);
1345         mutex_unlock(&lport->disc.disc_mutex);
1346         if (!rdata)
1347                 goto err;
1348
1349         rdata->ops = &fc_lport_rport_ops;
1350         lport->tt.rport_login(rdata);
1351         return;
1352
1353 err:
1354         fc_lport_error(lport, NULL);
1355 }
1356
1357 /**
1358  * fc_lport_timeout() - Handler for the retry_work timer.
1359  * @work: The work struct of the fc_lport
1360  */
1361 static void fc_lport_timeout(struct work_struct *work)
1362 {
1363         struct fc_lport *lport =
1364                 container_of(work, struct fc_lport,
1365                              retry_work.work);
1366
1367         mutex_lock(&lport->lp_mutex);
1368
1369         switch (lport->state) {
1370         case LPORT_ST_DISABLED:
1371                 WARN_ON(1);
1372                 break;
1373         case LPORT_ST_READY:
1374                 WARN_ON(1);
1375                 break;
1376         case LPORT_ST_RESET:
1377                 break;
1378         case LPORT_ST_FLOGI:
1379                 fc_lport_enter_flogi(lport);
1380                 break;
1381         case LPORT_ST_DNS:
1382                 fc_lport_enter_dns(lport);
1383                 break;
1384         case LPORT_ST_RNN_ID:
1385                 fc_lport_enter_rnn_id(lport);
1386                 break;
1387         case LPORT_ST_RSNN_NN:
1388                 fc_lport_enter_rsnn_nn(lport);
1389                 break;
1390         case LPORT_ST_RFT_ID:
1391                 fc_lport_enter_rft_id(lport);
1392                 break;
1393         case LPORT_ST_SCR:
1394                 fc_lport_enter_scr(lport);
1395                 break;
1396         case LPORT_ST_LOGO:
1397                 fc_lport_enter_logo(lport);
1398                 break;
1399         }
1400
1401         mutex_unlock(&lport->lp_mutex);
1402 }
1403
1404 /**
1405  * fc_lport_logo_resp() - Handle response to LOGO request
1406  * @sp: current sequence in LOGO exchange
1407  * @fp: response frame
1408  * @lp_arg: Fibre Channel lport port instance that sent the LOGO request
1409  *
1410  * Locking Note: This function will be called without the lport lock
1411  * held, but it will lock, call an _enter_* function or fc_lport_error
1412  * and then unlock the lport.
1413  */
1414 void fc_lport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
1415                                void *lp_arg)
1416 {
1417         struct fc_lport *lport = lp_arg;
1418         u8 op;
1419
1420         FC_LPORT_DBG(lport, "Received a LOGO %s\n", fc_els_resp_type(fp));
1421
1422         if (fp == ERR_PTR(-FC_EX_CLOSED))
1423                 return;
1424
1425         mutex_lock(&lport->lp_mutex);
1426
1427         if (lport->state != LPORT_ST_LOGO) {
1428                 FC_LPORT_DBG(lport, "Received a LOGO response, but in state "
1429                              "%s\n", fc_lport_state(lport));
1430                 if (IS_ERR(fp))
1431                         goto err;
1432                 goto out;
1433         }
1434
1435         if (IS_ERR(fp)) {
1436                 fc_lport_error(lport, fp);
1437                 goto err;
1438         }
1439
1440         op = fc_frame_payload_op(fp);
1441         if (op == ELS_LS_ACC)
1442                 fc_lport_enter_disabled(lport);
1443         else
1444                 fc_lport_error(lport, fp);
1445
1446 out:
1447         fc_frame_free(fp);
1448 err:
1449         mutex_unlock(&lport->lp_mutex);
1450 }
1451 EXPORT_SYMBOL(fc_lport_logo_resp);
1452
1453 /**
1454  * fc_rport_enter_logo() - Logout of the fabric
1455  * @lport: Fibre Channel local port to be logged out
1456  *
1457  * Locking Note: The lport lock is expected to be held before calling
1458  * this routine.
1459  */
1460 static void fc_lport_enter_logo(struct fc_lport *lport)
1461 {
1462         struct fc_frame *fp;
1463         struct fc_els_logo *logo;
1464
1465         FC_LPORT_DBG(lport, "Entered LOGO state from %s state\n",
1466                      fc_lport_state(lport));
1467
1468         fc_lport_state_enter(lport, LPORT_ST_LOGO);
1469         fc_vports_linkchange(lport);
1470
1471         fp = fc_frame_alloc(lport, sizeof(*logo));
1472         if (!fp) {
1473                 fc_lport_error(lport, fp);
1474                 return;
1475         }
1476
1477         if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp, ELS_LOGO,
1478                                   fc_lport_logo_resp, lport, lport->e_d_tov))
1479                 fc_lport_error(lport, NULL);
1480 }
1481
1482 /**
1483  * fc_lport_flogi_resp() - Handle response to FLOGI request
1484  * @sp: current sequence in FLOGI exchange
1485  * @fp: response frame
1486  * @lp_arg: Fibre Channel lport port instance that sent the FLOGI request
1487  *
1488  * Locking Note: This function will be called without the lport lock
1489  * held, but it will lock, call an _enter_* function or fc_lport_error
1490  * and then unlock the lport.
1491  */
1492 void fc_lport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
1493                                 void *lp_arg)
1494 {
1495         struct fc_lport *lport = lp_arg;
1496         struct fc_frame_header *fh;
1497         struct fc_els_flogi *flp;
1498         u32 did;
1499         u16 csp_flags;
1500         unsigned int r_a_tov;
1501         unsigned int e_d_tov;
1502         u16 mfs;
1503
1504         FC_LPORT_DBG(lport, "Received a FLOGI %s\n", fc_els_resp_type(fp));
1505
1506         if (fp == ERR_PTR(-FC_EX_CLOSED))
1507                 return;
1508
1509         mutex_lock(&lport->lp_mutex);
1510
1511         if (lport->state != LPORT_ST_FLOGI) {
1512                 FC_LPORT_DBG(lport, "Received a FLOGI response, but in state "
1513                              "%s\n", fc_lport_state(lport));
1514                 if (IS_ERR(fp))
1515                         goto err;
1516                 goto out;
1517         }
1518
1519         if (IS_ERR(fp)) {
1520                 fc_lport_error(lport, fp);
1521                 goto err;
1522         }
1523
1524         fh = fc_frame_header_get(fp);
1525         did = ntoh24(fh->fh_d_id);
1526         if (fc_frame_payload_op(fp) == ELS_LS_ACC && did != 0) {
1527
1528                 printk(KERN_INFO "libfc: Assigned FID (%6x) in FLOGI response\n",
1529                        did);
1530                 fc_host_port_id(lport->host) = did;
1531
1532                 flp = fc_frame_payload_get(fp, sizeof(*flp));
1533                 if (flp) {
1534                         mfs = ntohs(flp->fl_csp.sp_bb_data) &
1535                                 FC_SP_BB_DATA_MASK;
1536                         if (mfs >= FC_SP_MIN_MAX_PAYLOAD &&
1537                             mfs < lport->mfs)
1538                                 lport->mfs = mfs;
1539                         csp_flags = ntohs(flp->fl_csp.sp_features);
1540                         r_a_tov = ntohl(flp->fl_csp.sp_r_a_tov);
1541                         e_d_tov = ntohl(flp->fl_csp.sp_e_d_tov);
1542                         if (csp_flags & FC_SP_FT_EDTR)
1543                                 e_d_tov /= 1000000;
1544
1545                         lport->npiv_enabled = !!(csp_flags & FC_SP_FT_NPIV_ACC);
1546
1547                         if ((csp_flags & FC_SP_FT_FPORT) == 0) {
1548                                 if (e_d_tov > lport->e_d_tov)
1549                                         lport->e_d_tov = e_d_tov;
1550                                 lport->r_a_tov = 2 * e_d_tov;
1551                                 printk(KERN_INFO "libfc: Port (%6x) entered "
1552                                        "point to point mode\n", did);
1553                                 fc_lport_ptp_setup(lport, ntoh24(fh->fh_s_id),
1554                                                    get_unaligned_be64(
1555                                                            &flp->fl_wwpn),
1556                                                    get_unaligned_be64(
1557                                                            &flp->fl_wwnn));
1558                         } else {
1559                                 lport->e_d_tov = e_d_tov;
1560                                 lport->r_a_tov = r_a_tov;
1561                                 fc_host_fabric_name(lport->host) =
1562                                         get_unaligned_be64(&flp->fl_wwnn);
1563                                 fc_lport_enter_dns(lport);
1564                         }
1565                 }
1566         } else {
1567                 FC_LPORT_DBG(lport, "Bad FLOGI response\n");
1568         }
1569
1570 out:
1571         fc_frame_free(fp);
1572 err:
1573         mutex_unlock(&lport->lp_mutex);
1574 }
1575 EXPORT_SYMBOL(fc_lport_flogi_resp);
1576
1577 /**
1578  * fc_rport_enter_flogi() - Send a FLOGI request to the fabric manager
1579  * @lport: Fibre Channel local port to be logged in to the fabric
1580  *
1581  * Locking Note: The lport lock is expected to be held before calling
1582  * this routine.
1583  */
1584 void fc_lport_enter_flogi(struct fc_lport *lport)
1585 {
1586         struct fc_frame *fp;
1587
1588         FC_LPORT_DBG(lport, "Entered FLOGI state from %s state\n",
1589                      fc_lport_state(lport));
1590
1591         fc_lport_state_enter(lport, LPORT_ST_FLOGI);
1592
1593         fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
1594         if (!fp)
1595                 return fc_lport_error(lport, fp);
1596
1597         if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp,
1598                                   lport->vport ? ELS_FDISC : ELS_FLOGI,
1599                                   fc_lport_flogi_resp, lport, lport->e_d_tov))
1600                 fc_lport_error(lport, NULL);
1601 }
1602
1603 /* Configure a fc_lport */
1604 int fc_lport_config(struct fc_lport *lport)
1605 {
1606         INIT_DELAYED_WORK(&lport->retry_work, fc_lport_timeout);
1607         mutex_init(&lport->lp_mutex);
1608
1609         fc_lport_state_enter(lport, LPORT_ST_DISABLED);
1610
1611         fc_lport_add_fc4_type(lport, FC_TYPE_FCP);
1612         fc_lport_add_fc4_type(lport, FC_TYPE_CT);
1613
1614         return 0;
1615 }
1616 EXPORT_SYMBOL(fc_lport_config);
1617
1618 int fc_lport_init(struct fc_lport *lport)
1619 {
1620         if (!lport->tt.lport_recv)
1621                 lport->tt.lport_recv = fc_lport_recv_req;
1622
1623         if (!lport->tt.lport_reset)
1624                 lport->tt.lport_reset = fc_lport_reset;
1625
1626         fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
1627         fc_host_node_name(lport->host) = lport->wwnn;
1628         fc_host_port_name(lport->host) = lport->wwpn;
1629         fc_host_supported_classes(lport->host) = FC_COS_CLASS3;
1630         memset(fc_host_supported_fc4s(lport->host), 0,
1631                sizeof(fc_host_supported_fc4s(lport->host)));
1632         fc_host_supported_fc4s(lport->host)[2] = 1;
1633         fc_host_supported_fc4s(lport->host)[7] = 1;
1634
1635         /* This value is also unchanging */
1636         memset(fc_host_active_fc4s(lport->host), 0,
1637                sizeof(fc_host_active_fc4s(lport->host)));
1638         fc_host_active_fc4s(lport->host)[2] = 1;
1639         fc_host_active_fc4s(lport->host)[7] = 1;
1640         fc_host_maxframe_size(lport->host) = lport->mfs;
1641         fc_host_supported_speeds(lport->host) = 0;
1642         if (lport->link_supported_speeds & FC_PORTSPEED_1GBIT)
1643                 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_1GBIT;
1644         if (lport->link_supported_speeds & FC_PORTSPEED_10GBIT)
1645                 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_10GBIT;
1646
1647         return 0;
1648 }
1649 EXPORT_SYMBOL(fc_lport_init);