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