blob: 52c4f2dfcfd3cbeae038cce17fcfc35691b814ac [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/**
Robert Love34f42a02009-02-27 10:55:45 -0800142 * fc_lport_rport_callback() - Event handler for rport events
Robert Love42e9a922008-12-09 15:10:17 -0800143 * @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/**
Robert Love34f42a02009-02-27 10:55:45 -0800198 * fc_lport_state() - Return a string which represents the lport's state
Robert Love42e9a922008-12-09 15:10:17 -0800199 * @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/**
Robert Love34f42a02009-02-27 10:55:45 -0800212 * fc_lport_ptp_setup() - Create an rport for point-to-point mode
Robert Love42e9a922008-12-09 15:10:17 -0800213 * @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
Robert Love5101ff92009-02-27 10:55:18 -0800235 lport->ptp_rp = lport->tt.rport_create(&dp);
Robert Love42e9a922008-12-09 15:10:17 -0800236
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{
Robert Love42e9a922008-12-09 15:10:17 -0800270 struct fc_host_statistics *fcoe_stats;
271 struct fc_lport *lp = shost_priv(shost);
272 struct timespec v0, v1;
Robert Love582b45b2009-03-31 15:51:50 -0700273 unsigned int cpu;
Robert Love42e9a922008-12-09 15:10:17 -0800274
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
Robert Love582b45b2009-03-31 15:51:50 -0700282 for_each_possible_cpu(cpu) {
283 struct fcoe_dev_stats *stats;
284
285 stats = per_cpu_ptr(lp->dev_stats, cpu);
286
Robert Love42e9a922008-12-09 15:10:17 -0800287 fcoe_stats->tx_frames += stats->TxFrames;
288 fcoe_stats->tx_words += stats->TxWords;
289 fcoe_stats->rx_frames += stats->RxFrames;
290 fcoe_stats->rx_words += stats->RxWords;
291 fcoe_stats->error_frames += stats->ErrorFrames;
292 fcoe_stats->invalid_crc_count += stats->InvalidCRCCount;
293 fcoe_stats->fcp_input_requests += stats->InputRequests;
294 fcoe_stats->fcp_output_requests += stats->OutputRequests;
295 fcoe_stats->fcp_control_requests += stats->ControlRequests;
296 fcoe_stats->fcp_input_megabytes += stats->InputMegabytes;
297 fcoe_stats->fcp_output_megabytes += stats->OutputMegabytes;
298 fcoe_stats->link_failure_count += stats->LinkFailureCount;
299 }
300 fcoe_stats->lip_count = -1;
301 fcoe_stats->nos_count = -1;
302 fcoe_stats->loss_of_sync_count = -1;
303 fcoe_stats->loss_of_signal_count = -1;
304 fcoe_stats->prim_seq_protocol_err_count = -1;
305 fcoe_stats->dumped_frames = -1;
306 return fcoe_stats;
307}
308EXPORT_SYMBOL(fc_get_host_stats);
309
310/*
311 * Fill in FLOGI command for request.
312 */
313static void
314fc_lport_flogi_fill(struct fc_lport *lport, struct fc_els_flogi *flogi,
315 unsigned int op)
316{
317 struct fc_els_csp *sp;
318 struct fc_els_cssp *cp;
319
320 memset(flogi, 0, sizeof(*flogi));
321 flogi->fl_cmd = (u8) op;
322 put_unaligned_be64(lport->wwpn, &flogi->fl_wwpn);
323 put_unaligned_be64(lport->wwnn, &flogi->fl_wwnn);
324 sp = &flogi->fl_csp;
325 sp->sp_hi_ver = 0x20;
326 sp->sp_lo_ver = 0x20;
327 sp->sp_bb_cred = htons(10); /* this gets set by gateway */
328 sp->sp_bb_data = htons((u16) lport->mfs);
329 cp = &flogi->fl_cssp[3 - 1]; /* class 3 parameters */
330 cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
331 if (op != ELS_FLOGI) {
332 sp->sp_features = htons(FC_SP_FT_CIRO);
333 sp->sp_tot_seq = htons(255); /* seq. we accept */
334 sp->sp_rel_off = htons(0x1f);
335 sp->sp_e_d_tov = htonl(lport->e_d_tov);
336
337 cp->cp_rdfs = htons((u16) lport->mfs);
338 cp->cp_con_seq = htons(255);
339 cp->cp_open_seq = 1;
340 }
341}
342
343/*
344 * Add a supported FC-4 type.
345 */
346static void fc_lport_add_fc4_type(struct fc_lport *lport, enum fc_fh_type type)
347{
348 __be32 *mp;
349
350 mp = &lport->fcts.ff_type_map[type / FC_NS_BPW];
351 *mp = htonl(ntohl(*mp) | 1UL << (type % FC_NS_BPW));
352}
353
354/**
Robert Love34f42a02009-02-27 10:55:45 -0800355 * fc_lport_recv_rlir_req() - Handle received Registered Link Incident Report.
Robert Love42e9a922008-12-09 15:10:17 -0800356 * @lport: Fibre Channel local port recieving the RLIR
357 * @sp: current sequence in the RLIR exchange
358 * @fp: RLIR request frame
359 *
360 * Locking Note: The lport lock is exected to be held before calling
361 * this function.
362 */
363static void fc_lport_recv_rlir_req(struct fc_seq *sp, struct fc_frame *fp,
364 struct fc_lport *lport)
365{
366 FC_DEBUG_LPORT("Received RLIR request while in state %s\n",
367 fc_lport_state(lport));
368
369 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
370 fc_frame_free(fp);
371}
372
373/**
Robert Love34f42a02009-02-27 10:55:45 -0800374 * fc_lport_recv_echo_req() - Handle received ECHO request
Robert Love42e9a922008-12-09 15:10:17 -0800375 * @lport: Fibre Channel local port recieving the ECHO
376 * @sp: current sequence in the ECHO exchange
377 * @fp: ECHO request frame
378 *
379 * Locking Note: The lport lock is exected to be held before calling
380 * this function.
381 */
382static void fc_lport_recv_echo_req(struct fc_seq *sp, struct fc_frame *in_fp,
383 struct fc_lport *lport)
384{
385 struct fc_frame *fp;
386 struct fc_exch *ep = fc_seq_exch(sp);
387 unsigned int len;
388 void *pp;
389 void *dp;
390 u32 f_ctl;
391
392 FC_DEBUG_LPORT("Received RLIR request while in state %s\n",
393 fc_lport_state(lport));
394
395 len = fr_len(in_fp) - sizeof(struct fc_frame_header);
396 pp = fc_frame_payload_get(in_fp, len);
397
398 if (len < sizeof(__be32))
399 len = sizeof(__be32);
400
401 fp = fc_frame_alloc(lport, len);
402 if (fp) {
403 dp = fc_frame_payload_get(fp, len);
404 memcpy(dp, pp, len);
405 *((u32 *)dp) = htonl(ELS_LS_ACC << 24);
406 sp = lport->tt.seq_start_next(sp);
407 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
408 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
409 FC_TYPE_ELS, f_ctl, 0);
410 lport->tt.seq_send(lport, sp, fp);
411 }
412 fc_frame_free(in_fp);
413}
414
415/**
Robert Love34f42a02009-02-27 10:55:45 -0800416 * fc_lport_recv_echo_req() - Handle received Request Node ID data request
Robert Love42e9a922008-12-09 15:10:17 -0800417 * @lport: Fibre Channel local port recieving the RNID
418 * @sp: current sequence in the RNID exchange
419 * @fp: RNID request frame
420 *
421 * Locking Note: The lport lock is exected to be held before calling
422 * this function.
423 */
424static void fc_lport_recv_rnid_req(struct fc_seq *sp, struct fc_frame *in_fp,
425 struct fc_lport *lport)
426{
427 struct fc_frame *fp;
428 struct fc_exch *ep = fc_seq_exch(sp);
429 struct fc_els_rnid *req;
430 struct {
431 struct fc_els_rnid_resp rnid;
432 struct fc_els_rnid_cid cid;
433 struct fc_els_rnid_gen gen;
434 } *rp;
435 struct fc_seq_els_data rjt_data;
436 u8 fmt;
437 size_t len;
438 u32 f_ctl;
439
440 FC_DEBUG_LPORT("Received RNID request while in state %s\n",
441 fc_lport_state(lport));
442
443 req = fc_frame_payload_get(in_fp, sizeof(*req));
444 if (!req) {
445 rjt_data.fp = NULL;
446 rjt_data.reason = ELS_RJT_LOGIC;
447 rjt_data.explan = ELS_EXPL_NONE;
448 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
449 } else {
450 fmt = req->rnid_fmt;
451 len = sizeof(*rp);
452 if (fmt != ELS_RNIDF_GEN ||
453 ntohl(lport->rnid_gen.rnid_atype) == 0) {
454 fmt = ELS_RNIDF_NONE; /* nothing to provide */
455 len -= sizeof(rp->gen);
456 }
457 fp = fc_frame_alloc(lport, len);
458 if (fp) {
459 rp = fc_frame_payload_get(fp, len);
460 memset(rp, 0, len);
461 rp->rnid.rnid_cmd = ELS_LS_ACC;
462 rp->rnid.rnid_fmt = fmt;
463 rp->rnid.rnid_cid_len = sizeof(rp->cid);
464 rp->cid.rnid_wwpn = htonll(lport->wwpn);
465 rp->cid.rnid_wwnn = htonll(lport->wwnn);
466 if (fmt == ELS_RNIDF_GEN) {
467 rp->rnid.rnid_sid_len = sizeof(rp->gen);
468 memcpy(&rp->gen, &lport->rnid_gen,
469 sizeof(rp->gen));
470 }
471 sp = lport->tt.seq_start_next(sp);
472 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
473 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
474 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
475 FC_TYPE_ELS, f_ctl, 0);
476 lport->tt.seq_send(lport, sp, fp);
477 }
478 }
479 fc_frame_free(in_fp);
480}
481
482/**
Robert Love34f42a02009-02-27 10:55:45 -0800483 * fc_lport_recv_adisc_req() - Handle received Address Discovery Request
Robert Love42e9a922008-12-09 15:10:17 -0800484 * @lport: Fibre Channel local port recieving the ADISC
485 * @sp: current sequence in the ADISC exchange
486 * @fp: ADISC request frame
487 *
488 * Locking Note: The lport lock is expected to be held before calling
489 * this function.
490 */
491static void fc_lport_recv_adisc_req(struct fc_seq *sp, struct fc_frame *in_fp,
492 struct fc_lport *lport)
493{
494 struct fc_frame *fp;
495 struct fc_exch *ep = fc_seq_exch(sp);
496 struct fc_els_adisc *req, *rp;
497 struct fc_seq_els_data rjt_data;
498 size_t len;
499 u32 f_ctl;
500
501 FC_DEBUG_LPORT("Received ADISC request while in state %s\n",
502 fc_lport_state(lport));
503
504 req = fc_frame_payload_get(in_fp, sizeof(*req));
505 if (!req) {
506 rjt_data.fp = NULL;
507 rjt_data.reason = ELS_RJT_LOGIC;
508 rjt_data.explan = ELS_EXPL_NONE;
509 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
510 } else {
511 len = sizeof(*rp);
512 fp = fc_frame_alloc(lport, len);
513 if (fp) {
514 rp = fc_frame_payload_get(fp, len);
515 memset(rp, 0, len);
516 rp->adisc_cmd = ELS_LS_ACC;
517 rp->adisc_wwpn = htonll(lport->wwpn);
518 rp->adisc_wwnn = htonll(lport->wwnn);
519 hton24(rp->adisc_port_id,
520 fc_host_port_id(lport->host));
521 sp = lport->tt.seq_start_next(sp);
522 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
523 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
524 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
525 FC_TYPE_ELS, f_ctl, 0);
526 lport->tt.seq_send(lport, sp, fp);
527 }
528 }
529 fc_frame_free(in_fp);
530}
531
532/**
Robert Love34f42a02009-02-27 10:55:45 -0800533 * fc_lport_recv_logo_req() - Handle received fabric LOGO request
Robert Love42e9a922008-12-09 15:10:17 -0800534 * @lport: Fibre Channel local port recieving the LOGO
535 * @sp: current sequence in the LOGO exchange
536 * @fp: LOGO request frame
537 *
538 * Locking Note: The lport lock is exected to be held before calling
539 * this function.
540 */
541static void fc_lport_recv_logo_req(struct fc_seq *sp, struct fc_frame *fp,
542 struct fc_lport *lport)
543{
544 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
545 fc_lport_enter_reset(lport);
546 fc_frame_free(fp);
547}
548
549/**
Robert Love34f42a02009-02-27 10:55:45 -0800550 * fc_fabric_login() - Start the lport state machine
Robert Love42e9a922008-12-09 15:10:17 -0800551 * @lport: The lport that should log into the fabric
552 *
553 * Locking Note: This function should not be called
554 * with the lport lock held.
555 */
556int fc_fabric_login(struct fc_lport *lport)
557{
558 int rc = -1;
559
560 mutex_lock(&lport->lp_mutex);
561 if (lport->state == LPORT_ST_NONE) {
562 fc_lport_enter_reset(lport);
563 rc = 0;
564 }
565 mutex_unlock(&lport->lp_mutex);
566
567 return rc;
568}
569EXPORT_SYMBOL(fc_fabric_login);
570
571/**
Robert Love34f42a02009-02-27 10:55:45 -0800572 * fc_linkup() - Handler for transport linkup events
Robert Love42e9a922008-12-09 15:10:17 -0800573 * @lport: The lport whose link is up
574 */
575void fc_linkup(struct fc_lport *lport)
576{
577 FC_DEBUG_LPORT("Link is up for port (%6x)\n",
578 fc_host_port_id(lport->host));
579
580 mutex_lock(&lport->lp_mutex);
Vasu Devbc0e17f2009-02-27 10:54:57 -0800581 if (!lport->link_up) {
582 lport->link_up = 1;
Robert Love42e9a922008-12-09 15:10:17 -0800583
584 if (lport->state == LPORT_ST_RESET)
585 fc_lport_enter_flogi(lport);
586 }
587 mutex_unlock(&lport->lp_mutex);
588}
589EXPORT_SYMBOL(fc_linkup);
590
591/**
Robert Love34f42a02009-02-27 10:55:45 -0800592 * fc_linkdown() - Handler for transport linkdown events
Robert Love42e9a922008-12-09 15:10:17 -0800593 * @lport: The lport whose link is down
594 */
595void fc_linkdown(struct fc_lport *lport)
596{
597 mutex_lock(&lport->lp_mutex);
598 FC_DEBUG_LPORT("Link is down for port (%6x)\n",
599 fc_host_port_id(lport->host));
600
Vasu Devbc0e17f2009-02-27 10:54:57 -0800601 if (lport->link_up) {
602 lport->link_up = 0;
Robert Love42e9a922008-12-09 15:10:17 -0800603 fc_lport_enter_reset(lport);
604 lport->tt.fcp_cleanup(lport);
605 }
606 mutex_unlock(&lport->lp_mutex);
607}
608EXPORT_SYMBOL(fc_linkdown);
609
610/**
Robert Love34f42a02009-02-27 10:55:45 -0800611 * fc_fabric_logoff() - Logout of the fabric
Robert Love42e9a922008-12-09 15:10:17 -0800612 * @lport: fc_lport pointer to logoff the fabric
613 *
614 * Return value:
615 * 0 for success, -1 for failure
Robert Love34f42a02009-02-27 10:55:45 -0800616 */
Robert Love42e9a922008-12-09 15:10:17 -0800617int fc_fabric_logoff(struct fc_lport *lport)
618{
619 lport->tt.disc_stop_final(lport);
620 mutex_lock(&lport->lp_mutex);
621 fc_lport_enter_logo(lport);
622 mutex_unlock(&lport->lp_mutex);
Steve Maf7db2c12009-02-27 10:55:13 -0800623 cancel_delayed_work_sync(&lport->retry_work);
Robert Love42e9a922008-12-09 15:10:17 -0800624 return 0;
625}
626EXPORT_SYMBOL(fc_fabric_logoff);
627
628/**
Robert Love34f42a02009-02-27 10:55:45 -0800629 * fc_lport_destroy() - unregister a fc_lport
Robert Love42e9a922008-12-09 15:10:17 -0800630 * @lport: fc_lport pointer to unregister
631 *
632 * Return value:
633 * None
634 * Note:
635 * exit routine for fc_lport instance
636 * clean-up all the allocated memory
637 * and free up other system resources.
638 *
Robert Love34f42a02009-02-27 10:55:45 -0800639 */
Robert Love42e9a922008-12-09 15:10:17 -0800640int fc_lport_destroy(struct fc_lport *lport)
641{
642 lport->tt.frame_send = fc_frame_drop;
643 lport->tt.fcp_abort_io(lport);
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -0800644 lport->tt.exch_mgr_reset(lport, 0, 0);
Robert Love42e9a922008-12-09 15:10:17 -0800645 return 0;
646}
647EXPORT_SYMBOL(fc_lport_destroy);
648
649/**
Robert Love34f42a02009-02-27 10:55:45 -0800650 * fc_set_mfs() - sets up the mfs for the corresponding fc_lport
Robert Love42e9a922008-12-09 15:10:17 -0800651 * @lport: fc_lport pointer to unregister
652 * @mfs: the new mfs for fc_lport
653 *
654 * Set mfs for the given fc_lport to the new mfs.
655 *
656 * Return: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800657 */
Robert Love42e9a922008-12-09 15:10:17 -0800658int fc_set_mfs(struct fc_lport *lport, u32 mfs)
659{
660 unsigned int old_mfs;
661 int rc = -EINVAL;
662
663 mutex_lock(&lport->lp_mutex);
664
665 old_mfs = lport->mfs;
666
667 if (mfs >= FC_MIN_MAX_FRAME) {
668 mfs &= ~3;
669 if (mfs > FC_MAX_FRAME)
670 mfs = FC_MAX_FRAME;
671 mfs -= sizeof(struct fc_frame_header);
672 lport->mfs = mfs;
673 rc = 0;
674 }
675
676 if (!rc && mfs < old_mfs)
677 fc_lport_enter_reset(lport);
678
679 mutex_unlock(&lport->lp_mutex);
680
681 return rc;
682}
683EXPORT_SYMBOL(fc_set_mfs);
684
685/**
Robert Love34f42a02009-02-27 10:55:45 -0800686 * fc_lport_disc_callback() - Callback for discovery events
Robert Love42e9a922008-12-09 15:10:17 -0800687 * @lport: FC local port
688 * @event: The discovery event
689 */
690void fc_lport_disc_callback(struct fc_lport *lport, enum fc_disc_event event)
691{
692 switch (event) {
693 case DISC_EV_SUCCESS:
694 FC_DEBUG_LPORT("Got a SUCCESS event for port (%6x)\n",
695 fc_host_port_id(lport->host));
696 break;
697 case DISC_EV_FAILED:
698 FC_DEBUG_LPORT("Got a FAILED event for port (%6x)\n",
699 fc_host_port_id(lport->host));
700 mutex_lock(&lport->lp_mutex);
701 fc_lport_enter_reset(lport);
702 mutex_unlock(&lport->lp_mutex);
703 break;
704 case DISC_EV_NONE:
705 WARN_ON(1);
706 break;
707 }
708}
709
710/**
Robert Love34f42a02009-02-27 10:55:45 -0800711 * fc_rport_enter_ready() - Enter the ready state and start discovery
Robert Love42e9a922008-12-09 15:10:17 -0800712 * @lport: Fibre Channel local port that is ready
713 *
714 * Locking Note: The lport lock is expected to be held before calling
715 * this routine.
716 */
717static void fc_lport_enter_ready(struct fc_lport *lport)
718{
719 FC_DEBUG_LPORT("Port (%6x) entered Ready from state %s\n",
720 fc_host_port_id(lport->host), fc_lport_state(lport));
721
722 fc_lport_state_enter(lport, LPORT_ST_READY);
723
724 lport->tt.disc_start(fc_lport_disc_callback, lport);
725}
726
727/**
Robert Love34f42a02009-02-27 10:55:45 -0800728 * fc_lport_recv_flogi_req() - Receive a FLOGI request
Robert Love42e9a922008-12-09 15:10:17 -0800729 * @sp_in: The sequence the FLOGI is on
730 * @rx_fp: The frame the FLOGI is in
731 * @lport: The lport that recieved the request
732 *
733 * A received FLOGI request indicates a point-to-point connection.
734 * Accept it with the common service parameters indicating our N port.
735 * Set up to do a PLOGI if we have the higher-number WWPN.
736 *
737 * Locking Note: The lport lock is exected to be held before calling
738 * this function.
739 */
740static void fc_lport_recv_flogi_req(struct fc_seq *sp_in,
741 struct fc_frame *rx_fp,
742 struct fc_lport *lport)
743{
744 struct fc_frame *fp;
745 struct fc_frame_header *fh;
746 struct fc_seq *sp;
747 struct fc_exch *ep;
748 struct fc_els_flogi *flp;
749 struct fc_els_flogi *new_flp;
750 u64 remote_wwpn;
751 u32 remote_fid;
752 u32 local_fid;
753 u32 f_ctl;
754
755 FC_DEBUG_LPORT("Received FLOGI request while in state %s\n",
756 fc_lport_state(lport));
757
758 fh = fc_frame_header_get(rx_fp);
759 remote_fid = ntoh24(fh->fh_s_id);
760 flp = fc_frame_payload_get(rx_fp, sizeof(*flp));
761 if (!flp)
762 goto out;
763 remote_wwpn = get_unaligned_be64(&flp->fl_wwpn);
764 if (remote_wwpn == lport->wwpn) {
765 FC_DBG("FLOGI from port with same WWPN %llx "
FUJITA Tomonori6e7490c2009-01-11 17:38:12 +0900766 "possible configuration error\n",
767 (unsigned long long)remote_wwpn);
Robert Love42e9a922008-12-09 15:10:17 -0800768 goto out;
769 }
FUJITA Tomonori6e7490c2009-01-11 17:38:12 +0900770 FC_DBG("FLOGI from port WWPN %llx\n", (unsigned long long)remote_wwpn);
Robert Love42e9a922008-12-09 15:10:17 -0800771
772 /*
773 * XXX what is the right thing to do for FIDs?
774 * The originator might expect our S_ID to be 0xfffffe.
775 * But if so, both of us could end up with the same FID.
776 */
777 local_fid = FC_LOCAL_PTP_FID_LO;
778 if (remote_wwpn < lport->wwpn) {
779 local_fid = FC_LOCAL_PTP_FID_HI;
780 if (!remote_fid || remote_fid == local_fid)
781 remote_fid = FC_LOCAL_PTP_FID_LO;
782 } else if (!remote_fid) {
783 remote_fid = FC_LOCAL_PTP_FID_HI;
784 }
785
786 fc_host_port_id(lport->host) = local_fid;
787
788 fp = fc_frame_alloc(lport, sizeof(*flp));
789 if (fp) {
790 sp = lport->tt.seq_start_next(fr_seq(rx_fp));
791 new_flp = fc_frame_payload_get(fp, sizeof(*flp));
792 fc_lport_flogi_fill(lport, new_flp, ELS_FLOGI);
793 new_flp->fl_cmd = (u8) ELS_LS_ACC;
794
795 /*
796 * Send the response. If this fails, the originator should
797 * repeat the sequence.
798 */
799 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
800 ep = fc_seq_exch(sp);
801 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
802 FC_TYPE_ELS, f_ctl, 0);
803 lport->tt.seq_send(lport, sp, fp);
804
805 } else {
806 fc_lport_error(lport, fp);
807 }
808 fc_lport_ptp_setup(lport, remote_fid, remote_wwpn,
809 get_unaligned_be64(&flp->fl_wwnn));
810
811 lport->tt.disc_start(fc_lport_disc_callback, lport);
812
813out:
814 sp = fr_seq(rx_fp);
815 fc_frame_free(rx_fp);
816}
817
818/**
Robert Love34f42a02009-02-27 10:55:45 -0800819 * fc_lport_recv_req() - The generic lport request handler
Robert Love42e9a922008-12-09 15:10:17 -0800820 * @lport: The lport that received the request
821 * @sp: The sequence the request is on
822 * @fp: The frame the request is in
823 *
824 * This function will see if the lport handles the request or
825 * if an rport should handle the request.
826 *
827 * Locking Note: This function should not be called with the lport
828 * lock held becuase it will grab the lock.
829 */
830static void fc_lport_recv_req(struct fc_lport *lport, struct fc_seq *sp,
831 struct fc_frame *fp)
832{
833 struct fc_frame_header *fh = fc_frame_header_get(fp);
834 void (*recv) (struct fc_seq *, struct fc_frame *, struct fc_lport *);
835 struct fc_rport *rport;
836 u32 s_id;
837 u32 d_id;
838 struct fc_seq_els_data rjt_data;
839
840 mutex_lock(&lport->lp_mutex);
841
842 /*
843 * Handle special ELS cases like FLOGI, LOGO, and
844 * RSCN here. These don't require a session.
845 * Even if we had a session, it might not be ready.
846 */
847 if (fh->fh_type == FC_TYPE_ELS && fh->fh_r_ctl == FC_RCTL_ELS_REQ) {
848 /*
849 * Check opcode.
850 */
851 recv = NULL;
852 switch (fc_frame_payload_op(fp)) {
853 case ELS_FLOGI:
854 recv = fc_lport_recv_flogi_req;
855 break;
856 case ELS_LOGO:
857 fh = fc_frame_header_get(fp);
858 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI)
859 recv = fc_lport_recv_logo_req;
860 break;
861 case ELS_RSCN:
862 recv = lport->tt.disc_recv_req;
863 break;
864 case ELS_ECHO:
865 recv = fc_lport_recv_echo_req;
866 break;
867 case ELS_RLIR:
868 recv = fc_lport_recv_rlir_req;
869 break;
870 case ELS_RNID:
871 recv = fc_lport_recv_rnid_req;
872 break;
873 case ELS_ADISC:
874 recv = fc_lport_recv_adisc_req;
875 break;
876 }
877
878 if (recv)
879 recv(sp, fp, lport);
880 else {
881 /*
882 * Find session.
883 * If this is a new incoming PLOGI, we won't find it.
884 */
885 s_id = ntoh24(fh->fh_s_id);
886 d_id = ntoh24(fh->fh_d_id);
887
888 rport = lport->tt.rport_lookup(lport, s_id);
889 if (rport)
890 lport->tt.rport_recv_req(sp, fp, rport);
891 else {
892 rjt_data.fp = NULL;
893 rjt_data.reason = ELS_RJT_UNAB;
894 rjt_data.explan = ELS_EXPL_NONE;
895 lport->tt.seq_els_rsp_send(sp,
896 ELS_LS_RJT,
897 &rjt_data);
898 fc_frame_free(fp);
899 }
900 }
901 } else {
902 FC_DBG("dropping invalid frame (eof %x)\n", fr_eof(fp));
903 fc_frame_free(fp);
904 }
905 mutex_unlock(&lport->lp_mutex);
906
907 /*
908 * The common exch_done for all request may not be good
909 * if any request requires longer hold on exhange. XXX
910 */
911 lport->tt.exch_done(sp);
912}
913
914/**
Robert Love34f42a02009-02-27 10:55:45 -0800915 * fc_lport_reset() - Reset an lport
Robert Love42e9a922008-12-09 15:10:17 -0800916 * @lport: The lport which should be reset
917 *
918 * Locking Note: This functions should not be called with the
919 * lport lock held.
920 */
921int fc_lport_reset(struct fc_lport *lport)
922{
Steve Maf7db2c12009-02-27 10:55:13 -0800923 cancel_delayed_work_sync(&lport->retry_work);
Robert Love42e9a922008-12-09 15:10:17 -0800924 mutex_lock(&lport->lp_mutex);
925 fc_lport_enter_reset(lport);
926 mutex_unlock(&lport->lp_mutex);
927 return 0;
928}
929EXPORT_SYMBOL(fc_lport_reset);
930
931/**
Robert Love34f42a02009-02-27 10:55:45 -0800932 * fc_rport_enter_reset() - Reset the local port
Robert Love42e9a922008-12-09 15:10:17 -0800933 * @lport: Fibre Channel local port to be reset
934 *
935 * Locking Note: The lport lock is expected to be held before calling
936 * this routine.
937 */
938static void fc_lport_enter_reset(struct fc_lport *lport)
939{
940 FC_DEBUG_LPORT("Port (%6x) entered RESET state from %s state\n",
941 fc_host_port_id(lport->host), fc_lport_state(lport));
942
943 fc_lport_state_enter(lport, LPORT_ST_RESET);
944
945 if (lport->dns_rp)
946 lport->tt.rport_logoff(lport->dns_rp);
947
948 if (lport->ptp_rp) {
949 lport->tt.rport_logoff(lport->ptp_rp);
950 lport->ptp_rp = NULL;
951 }
952
953 lport->tt.disc_stop(lport);
954
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -0800955 lport->tt.exch_mgr_reset(lport, 0, 0);
Robert Love42e9a922008-12-09 15:10:17 -0800956 fc_host_fabric_name(lport->host) = 0;
957 fc_host_port_id(lport->host) = 0;
958
Vasu Devbc0e17f2009-02-27 10:54:57 -0800959 if (lport->link_up)
Robert Love42e9a922008-12-09 15:10:17 -0800960 fc_lport_enter_flogi(lport);
961}
962
963/**
Robert Love34f42a02009-02-27 10:55:45 -0800964 * fc_lport_error() - Handler for any errors
Robert Love42e9a922008-12-09 15:10:17 -0800965 * @lport: The fc_lport object
966 * @fp: The frame pointer
967 *
968 * If the error was caused by a resource allocation failure
969 * then wait for half a second and retry, otherwise retry
970 * after the e_d_tov time.
971 */
972static void fc_lport_error(struct fc_lport *lport, struct fc_frame *fp)
973{
974 unsigned long delay = 0;
975 FC_DEBUG_LPORT("Error %ld in state %s, retries %d\n",
976 PTR_ERR(fp), fc_lport_state(lport),
977 lport->retry_count);
978
979 if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
980 /*
981 * Memory allocation failure, or the exchange timed out.
982 * Retry after delay
983 */
984 if (lport->retry_count < lport->max_retry_count) {
985 lport->retry_count++;
986 if (!fp)
987 delay = msecs_to_jiffies(500);
988 else
989 delay = msecs_to_jiffies(lport->e_d_tov);
990
991 schedule_delayed_work(&lport->retry_work, delay);
992 } else {
993 switch (lport->state) {
994 case LPORT_ST_NONE:
995 case LPORT_ST_READY:
996 case LPORT_ST_RESET:
997 case LPORT_ST_RPN_ID:
998 case LPORT_ST_RFT_ID:
999 case LPORT_ST_SCR:
1000 case LPORT_ST_DNS:
1001 case LPORT_ST_FLOGI:
1002 case LPORT_ST_LOGO:
1003 fc_lport_enter_reset(lport);
1004 break;
1005 }
1006 }
1007 }
1008}
1009
1010/**
Robert Love34f42a02009-02-27 10:55:45 -08001011 * fc_lport_rft_id_resp() - Handle response to Register Fibre
1012 * Channel Types by ID (RPN_ID) request
Robert Love42e9a922008-12-09 15:10:17 -08001013 * @sp: current sequence in RPN_ID exchange
1014 * @fp: response frame
1015 * @lp_arg: Fibre Channel host port instance
1016 *
1017 * Locking Note: This function will be called without the lport lock
1018 * held, but it will lock, call an _enter_* function or fc_lport_error
1019 * and then unlock the lport.
1020 */
1021static void fc_lport_rft_id_resp(struct fc_seq *sp, struct fc_frame *fp,
1022 void *lp_arg)
1023{
1024 struct fc_lport *lport = lp_arg;
1025 struct fc_frame_header *fh;
1026 struct fc_ct_hdr *ct;
1027
1028 if (fp == ERR_PTR(-FC_EX_CLOSED))
1029 return;
1030
1031 mutex_lock(&lport->lp_mutex);
1032
1033 FC_DEBUG_LPORT("Received a RFT_ID response\n");
1034
1035 if (lport->state != LPORT_ST_RFT_ID) {
1036 FC_DBG("Received a RFT_ID response, but in state %s\n",
1037 fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001038 if (IS_ERR(fp))
1039 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001040 goto out;
1041 }
1042
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001043 if (IS_ERR(fp)) {
1044 fc_lport_error(lport, fp);
1045 goto err;
1046 }
1047
Robert Love42e9a922008-12-09 15:10:17 -08001048 fh = fc_frame_header_get(fp);
1049 ct = fc_frame_payload_get(fp, sizeof(*ct));
1050
1051 if (fh && ct && fh->fh_type == FC_TYPE_CT &&
1052 ct->ct_fs_type == FC_FST_DIR &&
1053 ct->ct_fs_subtype == FC_NS_SUBTYPE &&
1054 ntohs(ct->ct_cmd) == FC_FS_ACC)
1055 fc_lport_enter_scr(lport);
1056 else
1057 fc_lport_error(lport, fp);
1058out:
1059 fc_frame_free(fp);
1060err:
1061 mutex_unlock(&lport->lp_mutex);
1062}
1063
1064/**
Robert Love34f42a02009-02-27 10:55:45 -08001065 * fc_lport_rpn_id_resp() - Handle response to Register Port
1066 * Name by ID (RPN_ID) request
Robert Love42e9a922008-12-09 15:10:17 -08001067 * @sp: current sequence in RPN_ID exchange
1068 * @fp: response frame
1069 * @lp_arg: Fibre Channel host port instance
1070 *
1071 * Locking Note: This function will be called without the lport lock
1072 * held, but it will lock, call an _enter_* function or fc_lport_error
1073 * and then unlock the lport.
1074 */
1075static void fc_lport_rpn_id_resp(struct fc_seq *sp, struct fc_frame *fp,
1076 void *lp_arg)
1077{
1078 struct fc_lport *lport = lp_arg;
1079 struct fc_frame_header *fh;
1080 struct fc_ct_hdr *ct;
1081
1082 if (fp == ERR_PTR(-FC_EX_CLOSED))
1083 return;
1084
1085 mutex_lock(&lport->lp_mutex);
1086
1087 FC_DEBUG_LPORT("Received a RPN_ID response\n");
1088
1089 if (lport->state != LPORT_ST_RPN_ID) {
1090 FC_DBG("Received a RPN_ID response, but in state %s\n",
1091 fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001092 if (IS_ERR(fp))
1093 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001094 goto out;
1095 }
1096
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001097 if (IS_ERR(fp)) {
1098 fc_lport_error(lport, fp);
1099 goto err;
1100 }
1101
Robert Love42e9a922008-12-09 15:10:17 -08001102 fh = fc_frame_header_get(fp);
1103 ct = fc_frame_payload_get(fp, sizeof(*ct));
1104 if (fh && ct && fh->fh_type == FC_TYPE_CT &&
1105 ct->ct_fs_type == FC_FST_DIR &&
1106 ct->ct_fs_subtype == FC_NS_SUBTYPE &&
1107 ntohs(ct->ct_cmd) == FC_FS_ACC)
1108 fc_lport_enter_rft_id(lport);
1109 else
1110 fc_lport_error(lport, fp);
1111
1112out:
1113 fc_frame_free(fp);
1114err:
1115 mutex_unlock(&lport->lp_mutex);
1116}
1117
1118/**
Robert Love34f42a02009-02-27 10:55:45 -08001119 * fc_lport_scr_resp() - Handle response to State Change Register (SCR) request
Robert Love42e9a922008-12-09 15:10:17 -08001120 * @sp: current sequence in SCR exchange
1121 * @fp: response frame
1122 * @lp_arg: Fibre Channel lport port instance that sent the registration request
1123 *
1124 * Locking Note: This function will be called without the lport lock
1125 * held, but it will lock, call an _enter_* function or fc_lport_error
1126 * and then unlock the lport.
1127 */
1128static void fc_lport_scr_resp(struct fc_seq *sp, struct fc_frame *fp,
1129 void *lp_arg)
1130{
1131 struct fc_lport *lport = lp_arg;
1132 u8 op;
1133
1134 if (fp == ERR_PTR(-FC_EX_CLOSED))
1135 return;
1136
1137 mutex_lock(&lport->lp_mutex);
1138
1139 FC_DEBUG_LPORT("Received a SCR response\n");
1140
1141 if (lport->state != LPORT_ST_SCR) {
1142 FC_DBG("Received a SCR response, but in state %s\n",
1143 fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001144 if (IS_ERR(fp))
1145 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001146 goto out;
1147 }
1148
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001149 if (IS_ERR(fp)) {
1150 fc_lport_error(lport, fp);
1151 goto err;
1152 }
1153
Robert Love42e9a922008-12-09 15:10:17 -08001154 op = fc_frame_payload_op(fp);
1155 if (op == ELS_LS_ACC)
1156 fc_lport_enter_ready(lport);
1157 else
1158 fc_lport_error(lport, fp);
1159
1160out:
1161 fc_frame_free(fp);
1162err:
1163 mutex_unlock(&lport->lp_mutex);
1164}
1165
1166/**
Robert Love34f42a02009-02-27 10:55:45 -08001167 * fc_lport_enter_scr() - Send a State Change Register (SCR) request
Robert Love42e9a922008-12-09 15:10:17 -08001168 * @lport: Fibre Channel local port to register for state changes
1169 *
1170 * Locking Note: The lport lock is expected to be held before calling
1171 * this routine.
1172 */
1173static void fc_lport_enter_scr(struct fc_lport *lport)
1174{
1175 struct fc_frame *fp;
1176
1177 FC_DEBUG_LPORT("Port (%6x) entered SCR state from %s state\n",
1178 fc_host_port_id(lport->host), fc_lport_state(lport));
1179
1180 fc_lport_state_enter(lport, LPORT_ST_SCR);
1181
1182 fp = fc_frame_alloc(lport, sizeof(struct fc_els_scr));
1183 if (!fp) {
1184 fc_lport_error(lport, fp);
1185 return;
1186 }
1187
1188 if (!lport->tt.elsct_send(lport, NULL, fp, ELS_SCR,
1189 fc_lport_scr_resp, lport, lport->e_d_tov))
1190 fc_lport_error(lport, fp);
1191}
1192
1193/**
Robert Love34f42a02009-02-27 10:55:45 -08001194 * fc_lport_enter_rft_id() - Register FC4-types with the name server
Robert Love42e9a922008-12-09 15:10:17 -08001195 * @lport: Fibre Channel local port to register
1196 *
1197 * Locking Note: The lport lock is expected to be held before calling
1198 * this routine.
1199 */
1200static void fc_lport_enter_rft_id(struct fc_lport *lport)
1201{
1202 struct fc_frame *fp;
1203 struct fc_ns_fts *lps;
1204 int i;
1205
1206 FC_DEBUG_LPORT("Port (%6x) entered RFT_ID state from %s state\n",
1207 fc_host_port_id(lport->host), fc_lport_state(lport));
1208
1209 fc_lport_state_enter(lport, LPORT_ST_RFT_ID);
1210
1211 lps = &lport->fcts;
1212 i = sizeof(lps->ff_type_map) / sizeof(lps->ff_type_map[0]);
1213 while (--i >= 0)
1214 if (ntohl(lps->ff_type_map[i]) != 0)
1215 break;
1216 if (i < 0) {
1217 /* nothing to register, move on to SCR */
1218 fc_lport_enter_scr(lport);
1219 return;
1220 }
1221
1222 fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
1223 sizeof(struct fc_ns_rft));
1224 if (!fp) {
1225 fc_lport_error(lport, fp);
1226 return;
1227 }
1228
1229 if (!lport->tt.elsct_send(lport, NULL, fp, FC_NS_RFT_ID,
1230 fc_lport_rft_id_resp,
1231 lport, lport->e_d_tov))
1232 fc_lport_error(lport, fp);
1233}
1234
1235/**
Robert Love34f42a02009-02-27 10:55:45 -08001236 * fc_rport_enter_rft_id() - Register port name with the name server
Robert Love42e9a922008-12-09 15:10:17 -08001237 * @lport: Fibre Channel local port to register
1238 *
1239 * Locking Note: The lport lock is expected to be held before calling
1240 * this routine.
1241 */
1242static void fc_lport_enter_rpn_id(struct fc_lport *lport)
1243{
1244 struct fc_frame *fp;
1245
1246 FC_DEBUG_LPORT("Port (%6x) entered RPN_ID state from %s state\n",
1247 fc_host_port_id(lport->host), fc_lport_state(lport));
1248
1249 fc_lport_state_enter(lport, LPORT_ST_RPN_ID);
1250
1251 fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
1252 sizeof(struct fc_ns_rn_id));
1253 if (!fp) {
1254 fc_lport_error(lport, fp);
1255 return;
1256 }
1257
1258 if (!lport->tt.elsct_send(lport, NULL, fp, FC_NS_RPN_ID,
1259 fc_lport_rpn_id_resp,
1260 lport, lport->e_d_tov))
1261 fc_lport_error(lport, fp);
1262}
1263
1264static struct fc_rport_operations fc_lport_rport_ops = {
1265 .event_callback = fc_lport_rport_callback,
1266};
1267
1268/**
Robert Love34f42a02009-02-27 10:55:45 -08001269 * fc_rport_enter_dns() - Create a rport to the name server
Robert Love42e9a922008-12-09 15:10:17 -08001270 * @lport: Fibre Channel local port requesting a rport for the name server
1271 *
1272 * Locking Note: The lport lock is expected to be held before calling
1273 * this routine.
1274 */
1275static void fc_lport_enter_dns(struct fc_lport *lport)
1276{
1277 struct fc_rport *rport;
1278 struct fc_rport_libfc_priv *rdata;
1279 struct fc_disc_port dp;
1280
1281 dp.ids.port_id = FC_FID_DIR_SERV;
1282 dp.ids.port_name = -1;
1283 dp.ids.node_name = -1;
1284 dp.ids.roles = FC_RPORT_ROLE_UNKNOWN;
1285 dp.lp = lport;
1286
1287 FC_DEBUG_LPORT("Port (%6x) entered DNS state from %s state\n",
1288 fc_host_port_id(lport->host), fc_lport_state(lport));
1289
1290 fc_lport_state_enter(lport, LPORT_ST_DNS);
1291
Robert Love5101ff92009-02-27 10:55:18 -08001292 rport = lport->tt.rport_create(&dp);
Robert Love42e9a922008-12-09 15:10:17 -08001293 if (!rport)
1294 goto err;
1295
1296 rdata = rport->dd_data;
1297 rdata->ops = &fc_lport_rport_ops;
1298 lport->tt.rport_login(rport);
1299 return;
1300
1301err:
1302 fc_lport_error(lport, NULL);
1303}
1304
1305/**
Robert Love34f42a02009-02-27 10:55:45 -08001306 * fc_lport_timeout() - Handler for the retry_work timer.
Robert Love42e9a922008-12-09 15:10:17 -08001307 * @work: The work struct of the fc_lport
1308 */
1309static void fc_lport_timeout(struct work_struct *work)
1310{
1311 struct fc_lport *lport =
1312 container_of(work, struct fc_lport,
1313 retry_work.work);
1314
1315 mutex_lock(&lport->lp_mutex);
1316
1317 switch (lport->state) {
1318 case LPORT_ST_NONE:
1319 case LPORT_ST_READY:
1320 case LPORT_ST_RESET:
1321 WARN_ON(1);
1322 break;
1323 case LPORT_ST_FLOGI:
1324 fc_lport_enter_flogi(lport);
1325 break;
1326 case LPORT_ST_DNS:
1327 fc_lport_enter_dns(lport);
1328 break;
1329 case LPORT_ST_RPN_ID:
1330 fc_lport_enter_rpn_id(lport);
1331 break;
1332 case LPORT_ST_RFT_ID:
1333 fc_lport_enter_rft_id(lport);
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/**
Robert Love34f42a02009-02-27 10:55:45 -08001347 * fc_lport_logo_resp() - Handle response to LOGO request
Robert Love42e9a922008-12-09 15:10:17 -08001348 * @sp: current sequence in LOGO exchange
1349 * @fp: response frame
1350 * @lp_arg: Fibre Channel lport port instance that sent 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 */
1356static 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 if (fp == ERR_PTR(-FC_EX_CLOSED))
1363 return;
1364
1365 mutex_lock(&lport->lp_mutex);
1366
1367 FC_DEBUG_LPORT("Received a LOGO response\n");
1368
1369 if (lport->state != LPORT_ST_LOGO) {
1370 FC_DBG("Received a LOGO response, but in state %s\n",
1371 fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001372 if (IS_ERR(fp))
1373 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001374 goto out;
1375 }
1376
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001377 if (IS_ERR(fp)) {
1378 fc_lport_error(lport, fp);
1379 goto err;
1380 }
1381
Robert Love42e9a922008-12-09 15:10:17 -08001382 op = fc_frame_payload_op(fp);
1383 if (op == ELS_LS_ACC)
1384 fc_lport_enter_reset(lport);
1385 else
1386 fc_lport_error(lport, fp);
1387
1388out:
1389 fc_frame_free(fp);
1390err:
1391 mutex_unlock(&lport->lp_mutex);
1392}
1393
1394/**
Robert Love34f42a02009-02-27 10:55:45 -08001395 * fc_rport_enter_logo() - Logout of the fabric
Robert Love42e9a922008-12-09 15:10:17 -08001396 * @lport: Fibre Channel local port to be logged out
1397 *
1398 * Locking Note: The lport lock is expected to be held before calling
1399 * this routine.
1400 */
1401static void fc_lport_enter_logo(struct fc_lport *lport)
1402{
1403 struct fc_frame *fp;
1404 struct fc_els_logo *logo;
1405
1406 FC_DEBUG_LPORT("Port (%6x) entered LOGO state from %s state\n",
1407 fc_host_port_id(lport->host), fc_lport_state(lport));
1408
1409 fc_lport_state_enter(lport, LPORT_ST_LOGO);
1410
1411 /* DNS session should be closed so we can release it here */
1412 if (lport->dns_rp)
1413 lport->tt.rport_logoff(lport->dns_rp);
1414
1415 fp = fc_frame_alloc(lport, sizeof(*logo));
1416 if (!fp) {
1417 fc_lport_error(lport, fp);
1418 return;
1419 }
1420
1421 if (!lport->tt.elsct_send(lport, NULL, fp, ELS_LOGO, fc_lport_logo_resp,
1422 lport, lport->e_d_tov))
1423 fc_lport_error(lport, fp);
1424}
1425
1426/**
Robert Love34f42a02009-02-27 10:55:45 -08001427 * fc_lport_flogi_resp() - Handle response to FLOGI request
Robert Love42e9a922008-12-09 15:10:17 -08001428 * @sp: current sequence in FLOGI exchange
1429 * @fp: response frame
1430 * @lp_arg: Fibre Channel lport port instance that sent the FLOGI request
1431 *
1432 * Locking Note: This function will be called without the lport lock
1433 * held, but it will lock, call an _enter_* function or fc_lport_error
1434 * and then unlock the lport.
1435 */
1436static void fc_lport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
1437 void *lp_arg)
1438{
1439 struct fc_lport *lport = lp_arg;
1440 struct fc_frame_header *fh;
1441 struct fc_els_flogi *flp;
1442 u32 did;
1443 u16 csp_flags;
1444 unsigned int r_a_tov;
1445 unsigned int e_d_tov;
1446 u16 mfs;
1447
1448 if (fp == ERR_PTR(-FC_EX_CLOSED))
1449 return;
1450
1451 mutex_lock(&lport->lp_mutex);
1452
1453 FC_DEBUG_LPORT("Received a FLOGI response\n");
1454
1455 if (lport->state != LPORT_ST_FLOGI) {
1456 FC_DBG("Received a FLOGI response, but in state %s\n",
1457 fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001458 if (IS_ERR(fp))
1459 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001460 goto out;
1461 }
1462
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001463 if (IS_ERR(fp)) {
1464 fc_lport_error(lport, fp);
1465 goto err;
1466 }
1467
Robert Love42e9a922008-12-09 15:10:17 -08001468 fh = fc_frame_header_get(fp);
1469 did = ntoh24(fh->fh_d_id);
1470 if (fc_frame_payload_op(fp) == ELS_LS_ACC && did != 0) {
1471
1472 FC_DEBUG_LPORT("Assigned fid %x\n", did);
1473 fc_host_port_id(lport->host) = did;
1474
1475 flp = fc_frame_payload_get(fp, sizeof(*flp));
1476 if (flp) {
1477 mfs = ntohs(flp->fl_csp.sp_bb_data) &
1478 FC_SP_BB_DATA_MASK;
1479 if (mfs >= FC_SP_MIN_MAX_PAYLOAD &&
1480 mfs < lport->mfs)
1481 lport->mfs = mfs;
1482 csp_flags = ntohs(flp->fl_csp.sp_features);
1483 r_a_tov = ntohl(flp->fl_csp.sp_r_a_tov);
1484 e_d_tov = ntohl(flp->fl_csp.sp_e_d_tov);
1485 if (csp_flags & FC_SP_FT_EDTR)
1486 e_d_tov /= 1000000;
1487 if ((csp_flags & FC_SP_FT_FPORT) == 0) {
1488 if (e_d_tov > lport->e_d_tov)
1489 lport->e_d_tov = e_d_tov;
1490 lport->r_a_tov = 2 * e_d_tov;
1491 FC_DBG("Point-to-Point mode\n");
1492 fc_lport_ptp_setup(lport, ntoh24(fh->fh_s_id),
1493 get_unaligned_be64(
1494 &flp->fl_wwpn),
1495 get_unaligned_be64(
1496 &flp->fl_wwnn));
1497 } else {
1498 lport->e_d_tov = e_d_tov;
1499 lport->r_a_tov = r_a_tov;
1500 fc_host_fabric_name(lport->host) =
1501 get_unaligned_be64(&flp->fl_wwnn);
1502 fc_lport_enter_dns(lport);
1503 }
1504 }
1505
1506 if (flp) {
1507 csp_flags = ntohs(flp->fl_csp.sp_features);
1508 if ((csp_flags & FC_SP_FT_FPORT) == 0) {
1509 lport->tt.disc_start(fc_lport_disc_callback,
1510 lport);
1511 }
1512 }
1513 } else {
1514 FC_DBG("bad FLOGI response\n");
1515 }
1516
1517out:
1518 fc_frame_free(fp);
1519err:
1520 mutex_unlock(&lport->lp_mutex);
1521}
1522
1523/**
Robert Love34f42a02009-02-27 10:55:45 -08001524 * fc_rport_enter_flogi() - Send a FLOGI request to the fabric manager
Robert Love42e9a922008-12-09 15:10:17 -08001525 * @lport: Fibre Channel local port to be logged in to the fabric
1526 *
1527 * Locking Note: The lport lock is expected to be held before calling
1528 * this routine.
1529 */
1530void fc_lport_enter_flogi(struct fc_lport *lport)
1531{
1532 struct fc_frame *fp;
1533
1534 FC_DEBUG_LPORT("Processing FLOGI state\n");
1535
1536 fc_lport_state_enter(lport, LPORT_ST_FLOGI);
1537
1538 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
1539 if (!fp)
1540 return fc_lport_error(lport, fp);
1541
1542 if (!lport->tt.elsct_send(lport, NULL, fp, ELS_FLOGI,
1543 fc_lport_flogi_resp, lport, lport->e_d_tov))
1544 fc_lport_error(lport, fp);
1545}
1546
1547/* Configure a fc_lport */
1548int fc_lport_config(struct fc_lport *lport)
1549{
1550 INIT_DELAYED_WORK(&lport->retry_work, fc_lport_timeout);
1551 mutex_init(&lport->lp_mutex);
1552
1553 fc_lport_state_enter(lport, LPORT_ST_NONE);
1554
1555 fc_lport_add_fc4_type(lport, FC_TYPE_FCP);
1556 fc_lport_add_fc4_type(lport, FC_TYPE_CT);
1557
1558 return 0;
1559}
1560EXPORT_SYMBOL(fc_lport_config);
1561
1562int fc_lport_init(struct fc_lport *lport)
1563{
1564 if (!lport->tt.lport_recv)
1565 lport->tt.lport_recv = fc_lport_recv_req;
1566
1567 if (!lport->tt.lport_reset)
1568 lport->tt.lport_reset = fc_lport_reset;
1569
1570 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
1571 fc_host_node_name(lport->host) = lport->wwnn;
1572 fc_host_port_name(lport->host) = lport->wwpn;
1573 fc_host_supported_classes(lport->host) = FC_COS_CLASS3;
1574 memset(fc_host_supported_fc4s(lport->host), 0,
1575 sizeof(fc_host_supported_fc4s(lport->host)));
1576 fc_host_supported_fc4s(lport->host)[2] = 1;
1577 fc_host_supported_fc4s(lport->host)[7] = 1;
1578
1579 /* This value is also unchanging */
1580 memset(fc_host_active_fc4s(lport->host), 0,
1581 sizeof(fc_host_active_fc4s(lport->host)));
1582 fc_host_active_fc4s(lport->host)[2] = 1;
1583 fc_host_active_fc4s(lport->host)[7] = 1;
1584 fc_host_maxframe_size(lport->host) = lport->mfs;
1585 fc_host_supported_speeds(lport->host) = 0;
1586 if (lport->link_supported_speeds & FC_PORTSPEED_1GBIT)
1587 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_1GBIT;
1588 if (lport->link_supported_speeds & FC_PORTSPEED_10GBIT)
1589 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_10GBIT;
1590
1591 return 0;
1592}
1593EXPORT_SYMBOL(fc_lport_init);