blob: a6ab692f5f51dba9f4fba5c183725e054d7326b2 [file] [log] [blame]
Robert Love42e9a922008-12-09 15:10:17 -08001/*
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
104static 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
112static void fc_lport_error(struct fc_lport *, struct fc_frame *);
113
114static void fc_lport_enter_reset(struct fc_lport *);
115static void fc_lport_enter_flogi(struct fc_lport *);
116static void fc_lport_enter_dns(struct fc_lport *);
117static void fc_lport_enter_rpn_id(struct fc_lport *);
118static void fc_lport_enter_rft_id(struct fc_lport *);
119static void fc_lport_enter_scr(struct fc_lport *);
120static void fc_lport_enter_ready(struct fc_lport *);
121static void fc_lport_enter_logo(struct fc_lport *);
122
123static 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
135static 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 */
150static 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 */
201static 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 */
218static 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
242void 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}
247EXPORT_SYMBOL(fc_get_host_port_type);
248
249void fc_get_host_port_state(struct Scsi_Host *shost)
250{
251 struct fc_lport *lp = shost_priv(shost);
252
Vasu Devbc0e17f2009-02-27 10:54:57 -0800253 if (lp->link_up)
Robert Love42e9a922008-12-09 15:10:17 -0800254 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
255 else
256 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
257}
258EXPORT_SYMBOL(fc_get_host_port_state);
259
260void 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}
266EXPORT_SYMBOL(fc_get_host_speed);
267
268struct 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}
307EXPORT_SYMBOL(fc_get_host_stats);
308
309/*
310 * Fill in FLOGI command for request.
311 */
312static void
313fc_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 */
345static 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 */
362static 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 */
381static 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 */
423static 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 */
490static 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 */
540static 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 */
555int 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}
568EXPORT_SYMBOL(fc_fabric_login);
569
570/**
571 * fc_linkup - Handler for transport linkup events
572 * @lport: The lport whose link is up
573 */
574void 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);
Vasu Devbc0e17f2009-02-27 10:54:57 -0800580 if (!lport->link_up) {
581 lport->link_up = 1;
Robert Love42e9a922008-12-09 15:10:17 -0800582
583 if (lport->state == LPORT_ST_RESET)
584 fc_lport_enter_flogi(lport);
585 }
586 mutex_unlock(&lport->lp_mutex);
587}
588EXPORT_SYMBOL(fc_linkup);
589
590/**
591 * fc_linkdown - Handler for transport linkdown events
592 * @lport: The lport whose link is down
593 */
594void 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
Vasu Devbc0e17f2009-02-27 10:54:57 -0800600 if (lport->link_up) {
601 lport->link_up = 0;
Robert Love42e9a922008-12-09 15:10:17 -0800602 fc_lport_enter_reset(lport);
603 lport->tt.fcp_cleanup(lport);
604 }
605 mutex_unlock(&lport->lp_mutex);
606}
607EXPORT_SYMBOL(fc_linkdown);
608
609/**
Robert Love42e9a922008-12-09 15:10:17 -0800610 * fc_fabric_logoff - Logout of the fabric
611 * @lport: fc_lport pointer to logoff the fabric
612 *
613 * Return value:
614 * 0 for success, -1 for failure
615 **/
616int fc_fabric_logoff(struct fc_lport *lport)
617{
618 lport->tt.disc_stop_final(lport);
619 mutex_lock(&lport->lp_mutex);
620 fc_lport_enter_logo(lport);
621 mutex_unlock(&lport->lp_mutex);
622 return 0;
623}
624EXPORT_SYMBOL(fc_fabric_logoff);
625
626/**
627 * fc_lport_destroy - unregister a fc_lport
628 * @lport: fc_lport pointer to unregister
629 *
630 * Return value:
631 * None
632 * Note:
633 * exit routine for fc_lport instance
634 * clean-up all the allocated memory
635 * and free up other system resources.
636 *
637 **/
638int fc_lport_destroy(struct fc_lport *lport)
639{
640 lport->tt.frame_send = fc_frame_drop;
641 lport->tt.fcp_abort_io(lport);
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -0800642 lport->tt.exch_mgr_reset(lport, 0, 0);
Robert Love42e9a922008-12-09 15:10:17 -0800643 return 0;
644}
645EXPORT_SYMBOL(fc_lport_destroy);
646
647/**
648 * fc_set_mfs - sets up the mfs for the corresponding fc_lport
649 * @lport: fc_lport pointer to unregister
650 * @mfs: the new mfs for fc_lport
651 *
652 * Set mfs for the given fc_lport to the new mfs.
653 *
654 * Return: 0 for success
655 *
656 **/
657int fc_set_mfs(struct fc_lport *lport, u32 mfs)
658{
659 unsigned int old_mfs;
660 int rc = -EINVAL;
661
662 mutex_lock(&lport->lp_mutex);
663
664 old_mfs = lport->mfs;
665
666 if (mfs >= FC_MIN_MAX_FRAME) {
667 mfs &= ~3;
668 if (mfs > FC_MAX_FRAME)
669 mfs = FC_MAX_FRAME;
670 mfs -= sizeof(struct fc_frame_header);
671 lport->mfs = mfs;
672 rc = 0;
673 }
674
675 if (!rc && mfs < old_mfs)
676 fc_lport_enter_reset(lport);
677
678 mutex_unlock(&lport->lp_mutex);
679
680 return rc;
681}
682EXPORT_SYMBOL(fc_set_mfs);
683
684/**
685 * fc_lport_disc_callback - Callback for discovery events
686 * @lport: FC local port
687 * @event: The discovery event
688 */
689void fc_lport_disc_callback(struct fc_lport *lport, enum fc_disc_event event)
690{
691 switch (event) {
692 case DISC_EV_SUCCESS:
693 FC_DEBUG_LPORT("Got a SUCCESS event for port (%6x)\n",
694 fc_host_port_id(lport->host));
695 break;
696 case DISC_EV_FAILED:
697 FC_DEBUG_LPORT("Got a FAILED event for port (%6x)\n",
698 fc_host_port_id(lport->host));
699 mutex_lock(&lport->lp_mutex);
700 fc_lport_enter_reset(lport);
701 mutex_unlock(&lport->lp_mutex);
702 break;
703 case DISC_EV_NONE:
704 WARN_ON(1);
705 break;
706 }
707}
708
709/**
710 * fc_rport_enter_ready - Enter the ready state and start discovery
711 * @lport: Fibre Channel local port that is ready
712 *
713 * Locking Note: The lport lock is expected to be held before calling
714 * this routine.
715 */
716static void fc_lport_enter_ready(struct fc_lport *lport)
717{
718 FC_DEBUG_LPORT("Port (%6x) entered Ready from state %s\n",
719 fc_host_port_id(lport->host), fc_lport_state(lport));
720
721 fc_lport_state_enter(lport, LPORT_ST_READY);
722
723 lport->tt.disc_start(fc_lport_disc_callback, lport);
724}
725
726/**
727 * fc_lport_recv_flogi_req - Receive a FLOGI request
728 * @sp_in: The sequence the FLOGI is on
729 * @rx_fp: The frame the FLOGI is in
730 * @lport: The lport that recieved the request
731 *
732 * A received FLOGI request indicates a point-to-point connection.
733 * Accept it with the common service parameters indicating our N port.
734 * Set up to do a PLOGI if we have the higher-number WWPN.
735 *
736 * Locking Note: The lport lock is exected to be held before calling
737 * this function.
738 */
739static void fc_lport_recv_flogi_req(struct fc_seq *sp_in,
740 struct fc_frame *rx_fp,
741 struct fc_lport *lport)
742{
743 struct fc_frame *fp;
744 struct fc_frame_header *fh;
745 struct fc_seq *sp;
746 struct fc_exch *ep;
747 struct fc_els_flogi *flp;
748 struct fc_els_flogi *new_flp;
749 u64 remote_wwpn;
750 u32 remote_fid;
751 u32 local_fid;
752 u32 f_ctl;
753
754 FC_DEBUG_LPORT("Received FLOGI request while in state %s\n",
755 fc_lport_state(lport));
756
757 fh = fc_frame_header_get(rx_fp);
758 remote_fid = ntoh24(fh->fh_s_id);
759 flp = fc_frame_payload_get(rx_fp, sizeof(*flp));
760 if (!flp)
761 goto out;
762 remote_wwpn = get_unaligned_be64(&flp->fl_wwpn);
763 if (remote_wwpn == lport->wwpn) {
764 FC_DBG("FLOGI from port with same WWPN %llx "
765 "possible configuration error\n", remote_wwpn);
766 goto out;
767 }
768 FC_DBG("FLOGI from port WWPN %llx\n", remote_wwpn);
769
770 /*
771 * XXX what is the right thing to do for FIDs?
772 * The originator might expect our S_ID to be 0xfffffe.
773 * But if so, both of us could end up with the same FID.
774 */
775 local_fid = FC_LOCAL_PTP_FID_LO;
776 if (remote_wwpn < lport->wwpn) {
777 local_fid = FC_LOCAL_PTP_FID_HI;
778 if (!remote_fid || remote_fid == local_fid)
779 remote_fid = FC_LOCAL_PTP_FID_LO;
780 } else if (!remote_fid) {
781 remote_fid = FC_LOCAL_PTP_FID_HI;
782 }
783
784 fc_host_port_id(lport->host) = local_fid;
785
786 fp = fc_frame_alloc(lport, sizeof(*flp));
787 if (fp) {
788 sp = lport->tt.seq_start_next(fr_seq(rx_fp));
789 new_flp = fc_frame_payload_get(fp, sizeof(*flp));
790 fc_lport_flogi_fill(lport, new_flp, ELS_FLOGI);
791 new_flp->fl_cmd = (u8) ELS_LS_ACC;
792
793 /*
794 * Send the response. If this fails, the originator should
795 * repeat the sequence.
796 */
797 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
798 ep = fc_seq_exch(sp);
799 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
800 FC_TYPE_ELS, f_ctl, 0);
801 lport->tt.seq_send(lport, sp, fp);
802
803 } else {
804 fc_lport_error(lport, fp);
805 }
806 fc_lport_ptp_setup(lport, remote_fid, remote_wwpn,
807 get_unaligned_be64(&flp->fl_wwnn));
808
809 lport->tt.disc_start(fc_lport_disc_callback, lport);
810
811out:
812 sp = fr_seq(rx_fp);
813 fc_frame_free(rx_fp);
814}
815
816/**
817 * fc_lport_recv_req - The generic lport request handler
818 * @lport: The lport that received the request
819 * @sp: The sequence the request is on
820 * @fp: The frame the request is in
821 *
822 * This function will see if the lport handles the request or
823 * if an rport should handle the request.
824 *
825 * Locking Note: This function should not be called with the lport
826 * lock held becuase it will grab the lock.
827 */
828static void fc_lport_recv_req(struct fc_lport *lport, struct fc_seq *sp,
829 struct fc_frame *fp)
830{
831 struct fc_frame_header *fh = fc_frame_header_get(fp);
832 void (*recv) (struct fc_seq *, struct fc_frame *, struct fc_lport *);
833 struct fc_rport *rport;
834 u32 s_id;
835 u32 d_id;
836 struct fc_seq_els_data rjt_data;
837
838 mutex_lock(&lport->lp_mutex);
839
840 /*
841 * Handle special ELS cases like FLOGI, LOGO, and
842 * RSCN here. These don't require a session.
843 * Even if we had a session, it might not be ready.
844 */
845 if (fh->fh_type == FC_TYPE_ELS && fh->fh_r_ctl == FC_RCTL_ELS_REQ) {
846 /*
847 * Check opcode.
848 */
849 recv = NULL;
850 switch (fc_frame_payload_op(fp)) {
851 case ELS_FLOGI:
852 recv = fc_lport_recv_flogi_req;
853 break;
854 case ELS_LOGO:
855 fh = fc_frame_header_get(fp);
856 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI)
857 recv = fc_lport_recv_logo_req;
858 break;
859 case ELS_RSCN:
860 recv = lport->tt.disc_recv_req;
861 break;
862 case ELS_ECHO:
863 recv = fc_lport_recv_echo_req;
864 break;
865 case ELS_RLIR:
866 recv = fc_lport_recv_rlir_req;
867 break;
868 case ELS_RNID:
869 recv = fc_lport_recv_rnid_req;
870 break;
871 case ELS_ADISC:
872 recv = fc_lport_recv_adisc_req;
873 break;
874 }
875
876 if (recv)
877 recv(sp, fp, lport);
878 else {
879 /*
880 * Find session.
881 * If this is a new incoming PLOGI, we won't find it.
882 */
883 s_id = ntoh24(fh->fh_s_id);
884 d_id = ntoh24(fh->fh_d_id);
885
886 rport = lport->tt.rport_lookup(lport, s_id);
887 if (rport)
888 lport->tt.rport_recv_req(sp, fp, rport);
889 else {
890 rjt_data.fp = NULL;
891 rjt_data.reason = ELS_RJT_UNAB;
892 rjt_data.explan = ELS_EXPL_NONE;
893 lport->tt.seq_els_rsp_send(sp,
894 ELS_LS_RJT,
895 &rjt_data);
896 fc_frame_free(fp);
897 }
898 }
899 } else {
900 FC_DBG("dropping invalid frame (eof %x)\n", fr_eof(fp));
901 fc_frame_free(fp);
902 }
903 mutex_unlock(&lport->lp_mutex);
904
905 /*
906 * The common exch_done for all request may not be good
907 * if any request requires longer hold on exhange. XXX
908 */
909 lport->tt.exch_done(sp);
910}
911
912/**
913 * fc_lport_reset - Reset an lport
914 * @lport: The lport which should be reset
915 *
916 * Locking Note: This functions should not be called with the
917 * lport lock held.
918 */
919int fc_lport_reset(struct fc_lport *lport)
920{
921 mutex_lock(&lport->lp_mutex);
922 fc_lport_enter_reset(lport);
923 mutex_unlock(&lport->lp_mutex);
924 return 0;
925}
926EXPORT_SYMBOL(fc_lport_reset);
927
928/**
929 * fc_rport_enter_reset - Reset the local port
930 * @lport: Fibre Channel local port to be reset
931 *
932 * Locking Note: The lport lock is expected to be held before calling
933 * this routine.
934 */
935static void fc_lport_enter_reset(struct fc_lport *lport)
936{
937 FC_DEBUG_LPORT("Port (%6x) entered RESET state from %s state\n",
938 fc_host_port_id(lport->host), fc_lport_state(lport));
939
940 fc_lport_state_enter(lport, LPORT_ST_RESET);
941
942 if (lport->dns_rp)
943 lport->tt.rport_logoff(lport->dns_rp);
944
945 if (lport->ptp_rp) {
946 lport->tt.rport_logoff(lport->ptp_rp);
947 lport->ptp_rp = NULL;
948 }
949
950 lport->tt.disc_stop(lport);
951
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -0800952 lport->tt.exch_mgr_reset(lport, 0, 0);
Robert Love42e9a922008-12-09 15:10:17 -0800953 fc_host_fabric_name(lport->host) = 0;
954 fc_host_port_id(lport->host) = 0;
955
Vasu Devbc0e17f2009-02-27 10:54:57 -0800956 if (lport->link_up)
Robert Love42e9a922008-12-09 15:10:17 -0800957 fc_lport_enter_flogi(lport);
958}
959
960/**
961 * fc_lport_error - Handler for any errors
962 * @lport: The fc_lport object
963 * @fp: The frame pointer
964 *
965 * If the error was caused by a resource allocation failure
966 * then wait for half a second and retry, otherwise retry
967 * after the e_d_tov time.
968 */
969static void fc_lport_error(struct fc_lport *lport, struct fc_frame *fp)
970{
971 unsigned long delay = 0;
972 FC_DEBUG_LPORT("Error %ld in state %s, retries %d\n",
973 PTR_ERR(fp), fc_lport_state(lport),
974 lport->retry_count);
975
976 if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
977 /*
978 * Memory allocation failure, or the exchange timed out.
979 * Retry after delay
980 */
981 if (lport->retry_count < lport->max_retry_count) {
982 lport->retry_count++;
983 if (!fp)
984 delay = msecs_to_jiffies(500);
985 else
986 delay = msecs_to_jiffies(lport->e_d_tov);
987
988 schedule_delayed_work(&lport->retry_work, delay);
989 } else {
990 switch (lport->state) {
991 case LPORT_ST_NONE:
992 case LPORT_ST_READY:
993 case LPORT_ST_RESET:
994 case LPORT_ST_RPN_ID:
995 case LPORT_ST_RFT_ID:
996 case LPORT_ST_SCR:
997 case LPORT_ST_DNS:
998 case LPORT_ST_FLOGI:
999 case LPORT_ST_LOGO:
1000 fc_lport_enter_reset(lport);
1001 break;
1002 }
1003 }
1004 }
1005}
1006
1007/**
1008 * fc_lport_rft_id_resp - Handle response to Register Fibre
1009 * Channel Types by ID (RPN_ID) request
1010 * @sp: current sequence in RPN_ID exchange
1011 * @fp: response frame
1012 * @lp_arg: Fibre Channel host port instance
1013 *
1014 * Locking Note: This function will be called without the lport lock
1015 * held, but it will lock, call an _enter_* function or fc_lport_error
1016 * and then unlock the lport.
1017 */
1018static void fc_lport_rft_id_resp(struct fc_seq *sp, struct fc_frame *fp,
1019 void *lp_arg)
1020{
1021 struct fc_lport *lport = lp_arg;
1022 struct fc_frame_header *fh;
1023 struct fc_ct_hdr *ct;
1024
1025 if (fp == ERR_PTR(-FC_EX_CLOSED))
1026 return;
1027
1028 mutex_lock(&lport->lp_mutex);
1029
1030 FC_DEBUG_LPORT("Received a RFT_ID response\n");
1031
1032 if (lport->state != LPORT_ST_RFT_ID) {
1033 FC_DBG("Received a RFT_ID response, but in state %s\n",
1034 fc_lport_state(lport));
1035 goto out;
1036 }
1037
1038 if (IS_ERR(fp)) {
1039 fc_lport_error(lport, fp);
1040 goto err;
1041 }
1042
1043 fh = fc_frame_header_get(fp);
1044 ct = fc_frame_payload_get(fp, sizeof(*ct));
1045
1046 if (fh && ct && fh->fh_type == FC_TYPE_CT &&
1047 ct->ct_fs_type == FC_FST_DIR &&
1048 ct->ct_fs_subtype == FC_NS_SUBTYPE &&
1049 ntohs(ct->ct_cmd) == FC_FS_ACC)
1050 fc_lport_enter_scr(lport);
1051 else
1052 fc_lport_error(lport, fp);
1053out:
1054 fc_frame_free(fp);
1055err:
1056 mutex_unlock(&lport->lp_mutex);
1057}
1058
1059/**
1060 * fc_lport_rpn_id_resp - Handle response to Register Port
1061 * Name by ID (RPN_ID) request
1062 * @sp: current sequence in RPN_ID exchange
1063 * @fp: response frame
1064 * @lp_arg: Fibre Channel host port instance
1065 *
1066 * Locking Note: This function will be called without the lport lock
1067 * held, but it will lock, call an _enter_* function or fc_lport_error
1068 * and then unlock the lport.
1069 */
1070static void fc_lport_rpn_id_resp(struct fc_seq *sp, struct fc_frame *fp,
1071 void *lp_arg)
1072{
1073 struct fc_lport *lport = lp_arg;
1074 struct fc_frame_header *fh;
1075 struct fc_ct_hdr *ct;
1076
1077 if (fp == ERR_PTR(-FC_EX_CLOSED))
1078 return;
1079
1080 mutex_lock(&lport->lp_mutex);
1081
1082 FC_DEBUG_LPORT("Received a RPN_ID response\n");
1083
1084 if (lport->state != LPORT_ST_RPN_ID) {
1085 FC_DBG("Received a RPN_ID response, but in state %s\n",
1086 fc_lport_state(lport));
1087 goto out;
1088 }
1089
1090 if (IS_ERR(fp)) {
1091 fc_lport_error(lport, fp);
1092 goto err;
1093 }
1094
1095 fh = fc_frame_header_get(fp);
1096 ct = fc_frame_payload_get(fp, sizeof(*ct));
1097 if (fh && ct && fh->fh_type == FC_TYPE_CT &&
1098 ct->ct_fs_type == FC_FST_DIR &&
1099 ct->ct_fs_subtype == FC_NS_SUBTYPE &&
1100 ntohs(ct->ct_cmd) == FC_FS_ACC)
1101 fc_lport_enter_rft_id(lport);
1102 else
1103 fc_lport_error(lport, fp);
1104
1105out:
1106 fc_frame_free(fp);
1107err:
1108 mutex_unlock(&lport->lp_mutex);
1109}
1110
1111/**
1112 * fc_lport_scr_resp - Handle response to State Change Register (SCR) request
1113 * @sp: current sequence in SCR exchange
1114 * @fp: response frame
1115 * @lp_arg: Fibre Channel lport port instance that sent the registration request
1116 *
1117 * Locking Note: This function will be called without the lport lock
1118 * held, but it will lock, call an _enter_* function or fc_lport_error
1119 * and then unlock the lport.
1120 */
1121static void fc_lport_scr_resp(struct fc_seq *sp, struct fc_frame *fp,
1122 void *lp_arg)
1123{
1124 struct fc_lport *lport = lp_arg;
1125 u8 op;
1126
1127 if (fp == ERR_PTR(-FC_EX_CLOSED))
1128 return;
1129
1130 mutex_lock(&lport->lp_mutex);
1131
1132 FC_DEBUG_LPORT("Received a SCR response\n");
1133
1134 if (lport->state != LPORT_ST_SCR) {
1135 FC_DBG("Received a SCR response, but in state %s\n",
1136 fc_lport_state(lport));
1137 goto out;
1138 }
1139
1140 if (IS_ERR(fp)) {
1141 fc_lport_error(lport, fp);
1142 goto err;
1143 }
1144
1145 op = fc_frame_payload_op(fp);
1146 if (op == ELS_LS_ACC)
1147 fc_lport_enter_ready(lport);
1148 else
1149 fc_lport_error(lport, fp);
1150
1151out:
1152 fc_frame_free(fp);
1153err:
1154 mutex_unlock(&lport->lp_mutex);
1155}
1156
1157/**
1158 * fc_lport_enter_scr - Send a State Change Register (SCR) request
1159 * @lport: Fibre Channel local port to register for state changes
1160 *
1161 * Locking Note: The lport lock is expected to be held before calling
1162 * this routine.
1163 */
1164static void fc_lport_enter_scr(struct fc_lport *lport)
1165{
1166 struct fc_frame *fp;
1167
1168 FC_DEBUG_LPORT("Port (%6x) entered SCR state from %s state\n",
1169 fc_host_port_id(lport->host), fc_lport_state(lport));
1170
1171 fc_lport_state_enter(lport, LPORT_ST_SCR);
1172
1173 fp = fc_frame_alloc(lport, sizeof(struct fc_els_scr));
1174 if (!fp) {
1175 fc_lport_error(lport, fp);
1176 return;
1177 }
1178
1179 if (!lport->tt.elsct_send(lport, NULL, fp, ELS_SCR,
1180 fc_lport_scr_resp, lport, lport->e_d_tov))
1181 fc_lport_error(lport, fp);
1182}
1183
1184/**
1185 * fc_lport_enter_rft_id - Register FC4-types with the name server
1186 * @lport: Fibre Channel local port to register
1187 *
1188 * Locking Note: The lport lock is expected to be held before calling
1189 * this routine.
1190 */
1191static void fc_lport_enter_rft_id(struct fc_lport *lport)
1192{
1193 struct fc_frame *fp;
1194 struct fc_ns_fts *lps;
1195 int i;
1196
1197 FC_DEBUG_LPORT("Port (%6x) entered RFT_ID state from %s state\n",
1198 fc_host_port_id(lport->host), fc_lport_state(lport));
1199
1200 fc_lport_state_enter(lport, LPORT_ST_RFT_ID);
1201
1202 lps = &lport->fcts;
1203 i = sizeof(lps->ff_type_map) / sizeof(lps->ff_type_map[0]);
1204 while (--i >= 0)
1205 if (ntohl(lps->ff_type_map[i]) != 0)
1206 break;
1207 if (i < 0) {
1208 /* nothing to register, move on to SCR */
1209 fc_lport_enter_scr(lport);
1210 return;
1211 }
1212
1213 fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
1214 sizeof(struct fc_ns_rft));
1215 if (!fp) {
1216 fc_lport_error(lport, fp);
1217 return;
1218 }
1219
1220 if (!lport->tt.elsct_send(lport, NULL, fp, FC_NS_RFT_ID,
1221 fc_lport_rft_id_resp,
1222 lport, lport->e_d_tov))
1223 fc_lport_error(lport, fp);
1224}
1225
1226/**
1227 * fc_rport_enter_rft_id - Register port name with the name server
1228 * @lport: Fibre Channel local port to register
1229 *
1230 * Locking Note: The lport lock is expected to be held before calling
1231 * this routine.
1232 */
1233static void fc_lport_enter_rpn_id(struct fc_lport *lport)
1234{
1235 struct fc_frame *fp;
1236
1237 FC_DEBUG_LPORT("Port (%6x) entered RPN_ID state from %s state\n",
1238 fc_host_port_id(lport->host), fc_lport_state(lport));
1239
1240 fc_lport_state_enter(lport, LPORT_ST_RPN_ID);
1241
1242 fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
1243 sizeof(struct fc_ns_rn_id));
1244 if (!fp) {
1245 fc_lport_error(lport, fp);
1246 return;
1247 }
1248
1249 if (!lport->tt.elsct_send(lport, NULL, fp, FC_NS_RPN_ID,
1250 fc_lport_rpn_id_resp,
1251 lport, lport->e_d_tov))
1252 fc_lport_error(lport, fp);
1253}
1254
1255static struct fc_rport_operations fc_lport_rport_ops = {
1256 .event_callback = fc_lport_rport_callback,
1257};
1258
1259/**
1260 * fc_rport_enter_dns - Create a rport to the name server
1261 * @lport: Fibre Channel local port requesting a rport for the name server
1262 *
1263 * Locking Note: The lport lock is expected to be held before calling
1264 * this routine.
1265 */
1266static void fc_lport_enter_dns(struct fc_lport *lport)
1267{
1268 struct fc_rport *rport;
1269 struct fc_rport_libfc_priv *rdata;
1270 struct fc_disc_port dp;
1271
1272 dp.ids.port_id = FC_FID_DIR_SERV;
1273 dp.ids.port_name = -1;
1274 dp.ids.node_name = -1;
1275 dp.ids.roles = FC_RPORT_ROLE_UNKNOWN;
1276 dp.lp = lport;
1277
1278 FC_DEBUG_LPORT("Port (%6x) entered DNS state from %s state\n",
1279 fc_host_port_id(lport->host), fc_lport_state(lport));
1280
1281 fc_lport_state_enter(lport, LPORT_ST_DNS);
1282
1283 rport = fc_rport_rogue_create(&dp);
1284 if (!rport)
1285 goto err;
1286
1287 rdata = rport->dd_data;
1288 rdata->ops = &fc_lport_rport_ops;
1289 lport->tt.rport_login(rport);
1290 return;
1291
1292err:
1293 fc_lport_error(lport, NULL);
1294}
1295
1296/**
1297 * fc_lport_timeout - Handler for the retry_work timer.
1298 * @work: The work struct of the fc_lport
1299 */
1300static void fc_lport_timeout(struct work_struct *work)
1301{
1302 struct fc_lport *lport =
1303 container_of(work, struct fc_lport,
1304 retry_work.work);
1305
1306 mutex_lock(&lport->lp_mutex);
1307
1308 switch (lport->state) {
1309 case LPORT_ST_NONE:
1310 case LPORT_ST_READY:
1311 case LPORT_ST_RESET:
1312 WARN_ON(1);
1313 break;
1314 case LPORT_ST_FLOGI:
1315 fc_lport_enter_flogi(lport);
1316 break;
1317 case LPORT_ST_DNS:
1318 fc_lport_enter_dns(lport);
1319 break;
1320 case LPORT_ST_RPN_ID:
1321 fc_lport_enter_rpn_id(lport);
1322 break;
1323 case LPORT_ST_RFT_ID:
1324 fc_lport_enter_rft_id(lport);
1325 break;
1326 case LPORT_ST_SCR:
1327 fc_lport_enter_scr(lport);
1328 break;
1329 case LPORT_ST_LOGO:
1330 fc_lport_enter_logo(lport);
1331 break;
1332 }
1333
1334 mutex_unlock(&lport->lp_mutex);
1335}
1336
1337/**
1338 * fc_lport_logo_resp - Handle response to LOGO request
1339 * @sp: current sequence in LOGO exchange
1340 * @fp: response frame
1341 * @lp_arg: Fibre Channel lport port instance that sent the LOGO request
1342 *
1343 * Locking Note: This function will be called without the lport lock
1344 * held, but it will lock, call an _enter_* function or fc_lport_error
1345 * and then unlock the lport.
1346 */
1347static void fc_lport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
1348 void *lp_arg)
1349{
1350 struct fc_lport *lport = lp_arg;
1351 u8 op;
1352
1353 if (fp == ERR_PTR(-FC_EX_CLOSED))
1354 return;
1355
1356 mutex_lock(&lport->lp_mutex);
1357
1358 FC_DEBUG_LPORT("Received a LOGO response\n");
1359
1360 if (lport->state != LPORT_ST_LOGO) {
1361 FC_DBG("Received a LOGO response, but in state %s\n",
1362 fc_lport_state(lport));
1363 goto out;
1364 }
1365
1366 if (IS_ERR(fp)) {
1367 fc_lport_error(lport, fp);
1368 goto err;
1369 }
1370
1371 op = fc_frame_payload_op(fp);
1372 if (op == ELS_LS_ACC)
1373 fc_lport_enter_reset(lport);
1374 else
1375 fc_lport_error(lport, fp);
1376
1377out:
1378 fc_frame_free(fp);
1379err:
1380 mutex_unlock(&lport->lp_mutex);
1381}
1382
1383/**
1384 * fc_rport_enter_logo - Logout of the fabric
1385 * @lport: Fibre Channel local port to be logged out
1386 *
1387 * Locking Note: The lport lock is expected to be held before calling
1388 * this routine.
1389 */
1390static void fc_lport_enter_logo(struct fc_lport *lport)
1391{
1392 struct fc_frame *fp;
1393 struct fc_els_logo *logo;
1394
1395 FC_DEBUG_LPORT("Port (%6x) entered LOGO state from %s state\n",
1396 fc_host_port_id(lport->host), fc_lport_state(lport));
1397
1398 fc_lport_state_enter(lport, LPORT_ST_LOGO);
1399
1400 /* DNS session should be closed so we can release it here */
1401 if (lport->dns_rp)
1402 lport->tt.rport_logoff(lport->dns_rp);
1403
1404 fp = fc_frame_alloc(lport, sizeof(*logo));
1405 if (!fp) {
1406 fc_lport_error(lport, fp);
1407 return;
1408 }
1409
1410 if (!lport->tt.elsct_send(lport, NULL, fp, ELS_LOGO, fc_lport_logo_resp,
1411 lport, lport->e_d_tov))
1412 fc_lport_error(lport, fp);
1413}
1414
1415/**
1416 * fc_lport_flogi_resp - Handle response to FLOGI request
1417 * @sp: current sequence in FLOGI exchange
1418 * @fp: response frame
1419 * @lp_arg: Fibre Channel lport port instance that sent the FLOGI request
1420 *
1421 * Locking Note: This function will be called without the lport lock
1422 * held, but it will lock, call an _enter_* function or fc_lport_error
1423 * and then unlock the lport.
1424 */
1425static void fc_lport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
1426 void *lp_arg)
1427{
1428 struct fc_lport *lport = lp_arg;
1429 struct fc_frame_header *fh;
1430 struct fc_els_flogi *flp;
1431 u32 did;
1432 u16 csp_flags;
1433 unsigned int r_a_tov;
1434 unsigned int e_d_tov;
1435 u16 mfs;
1436
1437 if (fp == ERR_PTR(-FC_EX_CLOSED))
1438 return;
1439
1440 mutex_lock(&lport->lp_mutex);
1441
1442 FC_DEBUG_LPORT("Received a FLOGI response\n");
1443
1444 if (lport->state != LPORT_ST_FLOGI) {
1445 FC_DBG("Received a FLOGI response, but in state %s\n",
1446 fc_lport_state(lport));
1447 goto out;
1448 }
1449
1450 if (IS_ERR(fp)) {
1451 fc_lport_error(lport, fp);
1452 goto err;
1453 }
1454
1455 fh = fc_frame_header_get(fp);
1456 did = ntoh24(fh->fh_d_id);
1457 if (fc_frame_payload_op(fp) == ELS_LS_ACC && did != 0) {
1458
1459 FC_DEBUG_LPORT("Assigned fid %x\n", did);
1460 fc_host_port_id(lport->host) = did;
1461
1462 flp = fc_frame_payload_get(fp, sizeof(*flp));
1463 if (flp) {
1464 mfs = ntohs(flp->fl_csp.sp_bb_data) &
1465 FC_SP_BB_DATA_MASK;
1466 if (mfs >= FC_SP_MIN_MAX_PAYLOAD &&
1467 mfs < lport->mfs)
1468 lport->mfs = mfs;
1469 csp_flags = ntohs(flp->fl_csp.sp_features);
1470 r_a_tov = ntohl(flp->fl_csp.sp_r_a_tov);
1471 e_d_tov = ntohl(flp->fl_csp.sp_e_d_tov);
1472 if (csp_flags & FC_SP_FT_EDTR)
1473 e_d_tov /= 1000000;
1474 if ((csp_flags & FC_SP_FT_FPORT) == 0) {
1475 if (e_d_tov > lport->e_d_tov)
1476 lport->e_d_tov = e_d_tov;
1477 lport->r_a_tov = 2 * e_d_tov;
1478 FC_DBG("Point-to-Point mode\n");
1479 fc_lport_ptp_setup(lport, ntoh24(fh->fh_s_id),
1480 get_unaligned_be64(
1481 &flp->fl_wwpn),
1482 get_unaligned_be64(
1483 &flp->fl_wwnn));
1484 } else {
1485 lport->e_d_tov = e_d_tov;
1486 lport->r_a_tov = r_a_tov;
1487 fc_host_fabric_name(lport->host) =
1488 get_unaligned_be64(&flp->fl_wwnn);
1489 fc_lport_enter_dns(lport);
1490 }
1491 }
1492
1493 if (flp) {
1494 csp_flags = ntohs(flp->fl_csp.sp_features);
1495 if ((csp_flags & FC_SP_FT_FPORT) == 0) {
1496 lport->tt.disc_start(fc_lport_disc_callback,
1497 lport);
1498 }
1499 }
1500 } else {
1501 FC_DBG("bad FLOGI response\n");
1502 }
1503
1504out:
1505 fc_frame_free(fp);
1506err:
1507 mutex_unlock(&lport->lp_mutex);
1508}
1509
1510/**
1511 * fc_rport_enter_flogi - Send a FLOGI request to the fabric manager
1512 * @lport: Fibre Channel local port to be logged in to the fabric
1513 *
1514 * Locking Note: The lport lock is expected to be held before calling
1515 * this routine.
1516 */
1517void fc_lport_enter_flogi(struct fc_lport *lport)
1518{
1519 struct fc_frame *fp;
1520
1521 FC_DEBUG_LPORT("Processing FLOGI state\n");
1522
1523 fc_lport_state_enter(lport, LPORT_ST_FLOGI);
1524
1525 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
1526 if (!fp)
1527 return fc_lport_error(lport, fp);
1528
1529 if (!lport->tt.elsct_send(lport, NULL, fp, ELS_FLOGI,
1530 fc_lport_flogi_resp, lport, lport->e_d_tov))
1531 fc_lport_error(lport, fp);
1532}
1533
1534/* Configure a fc_lport */
1535int fc_lport_config(struct fc_lport *lport)
1536{
1537 INIT_DELAYED_WORK(&lport->retry_work, fc_lport_timeout);
1538 mutex_init(&lport->lp_mutex);
1539
1540 fc_lport_state_enter(lport, LPORT_ST_NONE);
1541
1542 fc_lport_add_fc4_type(lport, FC_TYPE_FCP);
1543 fc_lport_add_fc4_type(lport, FC_TYPE_CT);
1544
1545 return 0;
1546}
1547EXPORT_SYMBOL(fc_lport_config);
1548
1549int fc_lport_init(struct fc_lport *lport)
1550{
1551 if (!lport->tt.lport_recv)
1552 lport->tt.lport_recv = fc_lport_recv_req;
1553
1554 if (!lport->tt.lport_reset)
1555 lport->tt.lport_reset = fc_lport_reset;
1556
1557 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
1558 fc_host_node_name(lport->host) = lport->wwnn;
1559 fc_host_port_name(lport->host) = lport->wwpn;
1560 fc_host_supported_classes(lport->host) = FC_COS_CLASS3;
1561 memset(fc_host_supported_fc4s(lport->host), 0,
1562 sizeof(fc_host_supported_fc4s(lport->host)));
1563 fc_host_supported_fc4s(lport->host)[2] = 1;
1564 fc_host_supported_fc4s(lport->host)[7] = 1;
1565
1566 /* This value is also unchanging */
1567 memset(fc_host_active_fc4s(lport->host), 0,
1568 sizeof(fc_host_active_fc4s(lport->host)));
1569 fc_host_active_fc4s(lport->host)[2] = 1;
1570 fc_host_active_fc4s(lport->host)[7] = 1;
1571 fc_host_maxframe_size(lport->host) = lport->mfs;
1572 fc_host_supported_speeds(lport->host) = 0;
1573 if (lport->link_supported_speeds & FC_PORTSPEED_1GBIT)
1574 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_1GBIT;
1575 if (lport->link_supported_speeds & FC_PORTSPEED_10GBIT)
1576 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_10GBIT;
1577
1578 return 0;
1579}
1580EXPORT_SYMBOL(fc_lport_init);