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