blob: 7bb451ab0b87fac352b0948ae36d4ee11d468995 [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
Robert Love42e9a922008-12-09 15:10:17 -0800104static void fc_lport_error(struct fc_lport *, struct fc_frame *);
105
106static void fc_lport_enter_reset(struct fc_lport *);
107static void fc_lport_enter_flogi(struct fc_lport *);
108static void fc_lport_enter_dns(struct fc_lport *);
109static void fc_lport_enter_rpn_id(struct fc_lport *);
110static void fc_lport_enter_rft_id(struct fc_lport *);
111static void fc_lport_enter_scr(struct fc_lport *);
112static void fc_lport_enter_ready(struct fc_lport *);
113static void fc_lport_enter_logo(struct fc_lport *);
114
115static const char *fc_lport_state_names[] = {
Joe Eykholtb1d9fd52009-07-29 17:04:22 -0700116 [LPORT_ST_DISABLED] = "disabled",
Robert Love42e9a922008-12-09 15:10:17 -0800117 [LPORT_ST_FLOGI] = "FLOGI",
118 [LPORT_ST_DNS] = "dNS",
119 [LPORT_ST_RPN_ID] = "RPN_ID",
120 [LPORT_ST_RFT_ID] = "RFT_ID",
121 [LPORT_ST_SCR] = "SCR",
122 [LPORT_ST_READY] = "Ready",
123 [LPORT_ST_LOGO] = "LOGO",
124 [LPORT_ST_RESET] = "reset",
125};
126
127static int fc_frame_drop(struct fc_lport *lport, struct fc_frame *fp)
128{
129 fc_frame_free(fp);
130 return 0;
131}
132
133/**
Robert Love34f42a02009-02-27 10:55:45 -0800134 * fc_lport_rport_callback() - Event handler for rport events
Robert Love42e9a922008-12-09 15:10:17 -0800135 * @lport: The lport which is receiving the event
136 * @rport: The rport which the event has occured on
137 * @event: The event that occured
138 *
139 * Locking Note: The rport lock should not be held when calling
140 * this function.
141 */
142static void fc_lport_rport_callback(struct fc_lport *lport,
143 struct fc_rport *rport,
144 enum fc_rport_event event)
145{
Robert Love74147052009-06-10 15:31:10 -0700146 FC_LPORT_DBG(lport, "Received a %d event for port (%6x)\n", event,
147 rport->port_id);
Robert Love42e9a922008-12-09 15:10:17 -0800148
149 switch (event) {
150 case RPORT_EV_CREATED:
151 if (rport->port_id == FC_FID_DIR_SERV) {
152 mutex_lock(&lport->lp_mutex);
153 if (lport->state == LPORT_ST_DNS) {
154 lport->dns_rp = rport;
155 fc_lport_enter_rpn_id(lport);
156 } else {
Robert Love74147052009-06-10 15:31:10 -0700157 FC_LPORT_DBG(lport, "Received an CREATED event "
158 "on port (%6x) for the directory "
159 "server, but the lport is not "
160 "in the DNS state, it's in the "
161 "%d state", rport->port_id,
162 lport->state);
Robert Love42e9a922008-12-09 15:10:17 -0800163 lport->tt.rport_logoff(rport);
164 }
165 mutex_unlock(&lport->lp_mutex);
166 } else
Robert Love74147052009-06-10 15:31:10 -0700167 FC_LPORT_DBG(lport, "Received an event for port (%6x) "
168 "which is not the directory server\n",
169 rport->port_id);
Robert Love42e9a922008-12-09 15:10:17 -0800170 break;
171 case RPORT_EV_LOGO:
172 case RPORT_EV_FAILED:
173 case RPORT_EV_STOP:
174 if (rport->port_id == FC_FID_DIR_SERV) {
175 mutex_lock(&lport->lp_mutex);
176 lport->dns_rp = NULL;
177 mutex_unlock(&lport->lp_mutex);
178
179 } else
Robert Love74147052009-06-10 15:31:10 -0700180 FC_LPORT_DBG(lport, "Received an event for port (%6x) "
181 "which is not the directory server\n",
182 rport->port_id);
Robert Love42e9a922008-12-09 15:10:17 -0800183 break;
184 case RPORT_EV_NONE:
185 break;
186 }
187}
188
189/**
Robert Love34f42a02009-02-27 10:55:45 -0800190 * fc_lport_state() - Return a string which represents the lport's state
Robert Love42e9a922008-12-09 15:10:17 -0800191 * @lport: The lport whose state is to converted to a string
192 */
193static const char *fc_lport_state(struct fc_lport *lport)
194{
195 const char *cp;
196
197 cp = fc_lport_state_names[lport->state];
198 if (!cp)
199 cp = "unknown";
200 return cp;
201}
202
203/**
Robert Love34f42a02009-02-27 10:55:45 -0800204 * fc_lport_ptp_setup() - Create an rport for point-to-point mode
Robert Love42e9a922008-12-09 15:10:17 -0800205 * @lport: The lport to attach the ptp rport to
206 * @fid: The FID of the ptp rport
207 * @remote_wwpn: The WWPN of the ptp rport
208 * @remote_wwnn: The WWNN of the ptp rport
209 */
210static void fc_lport_ptp_setup(struct fc_lport *lport,
211 u32 remote_fid, u64 remote_wwpn,
212 u64 remote_wwnn)
213{
214 struct fc_disc_port dp;
215
216 dp.lp = lport;
217 dp.ids.port_id = remote_fid;
218 dp.ids.port_name = remote_wwpn;
219 dp.ids.node_name = remote_wwnn;
220 dp.ids.roles = FC_RPORT_ROLE_UNKNOWN;
221
222 if (lport->ptp_rp) {
223 lport->tt.rport_logoff(lport->ptp_rp);
224 lport->ptp_rp = NULL;
225 }
226
Robert Love5101ff92009-02-27 10:55:18 -0800227 lport->ptp_rp = lport->tt.rport_create(&dp);
Robert Love42e9a922008-12-09 15:10:17 -0800228
229 lport->tt.rport_login(lport->ptp_rp);
230
231 fc_lport_enter_ready(lport);
232}
233
234void fc_get_host_port_type(struct Scsi_Host *shost)
235{
236 /* TODO - currently just NPORT */
237 fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
238}
239EXPORT_SYMBOL(fc_get_host_port_type);
240
241void fc_get_host_port_state(struct Scsi_Host *shost)
242{
243 struct fc_lport *lp = shost_priv(shost);
244
Vasu Devbc0e17f2009-02-27 10:54:57 -0800245 if (lp->link_up)
Robert Love42e9a922008-12-09 15:10:17 -0800246 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
247 else
248 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
249}
250EXPORT_SYMBOL(fc_get_host_port_state);
251
252void fc_get_host_speed(struct Scsi_Host *shost)
253{
254 struct fc_lport *lport = shost_priv(shost);
255
256 fc_host_speed(shost) = lport->link_speed;
257}
258EXPORT_SYMBOL(fc_get_host_speed);
259
260struct fc_host_statistics *fc_get_host_stats(struct Scsi_Host *shost)
261{
Robert Love42e9a922008-12-09 15:10:17 -0800262 struct fc_host_statistics *fcoe_stats;
263 struct fc_lport *lp = shost_priv(shost);
264 struct timespec v0, v1;
Robert Love582b45b2009-03-31 15:51:50 -0700265 unsigned int cpu;
Robert Love42e9a922008-12-09 15:10:17 -0800266
267 fcoe_stats = &lp->host_stats;
268 memset(fcoe_stats, 0, sizeof(struct fc_host_statistics));
269
270 jiffies_to_timespec(jiffies, &v0);
271 jiffies_to_timespec(lp->boot_time, &v1);
272 fcoe_stats->seconds_since_last_reset = (v0.tv_sec - v1.tv_sec);
273
Robert Love582b45b2009-03-31 15:51:50 -0700274 for_each_possible_cpu(cpu) {
275 struct fcoe_dev_stats *stats;
276
277 stats = per_cpu_ptr(lp->dev_stats, cpu);
278
Robert Love42e9a922008-12-09 15:10:17 -0800279 fcoe_stats->tx_frames += stats->TxFrames;
280 fcoe_stats->tx_words += stats->TxWords;
281 fcoe_stats->rx_frames += stats->RxFrames;
282 fcoe_stats->rx_words += stats->RxWords;
283 fcoe_stats->error_frames += stats->ErrorFrames;
284 fcoe_stats->invalid_crc_count += stats->InvalidCRCCount;
285 fcoe_stats->fcp_input_requests += stats->InputRequests;
286 fcoe_stats->fcp_output_requests += stats->OutputRequests;
287 fcoe_stats->fcp_control_requests += stats->ControlRequests;
288 fcoe_stats->fcp_input_megabytes += stats->InputMegabytes;
289 fcoe_stats->fcp_output_megabytes += stats->OutputMegabytes;
290 fcoe_stats->link_failure_count += stats->LinkFailureCount;
291 }
292 fcoe_stats->lip_count = -1;
293 fcoe_stats->nos_count = -1;
294 fcoe_stats->loss_of_sync_count = -1;
295 fcoe_stats->loss_of_signal_count = -1;
296 fcoe_stats->prim_seq_protocol_err_count = -1;
297 fcoe_stats->dumped_frames = -1;
298 return fcoe_stats;
299}
300EXPORT_SYMBOL(fc_get_host_stats);
301
302/*
303 * Fill in FLOGI command for request.
304 */
305static void
306fc_lport_flogi_fill(struct fc_lport *lport, struct fc_els_flogi *flogi,
307 unsigned int op)
308{
309 struct fc_els_csp *sp;
310 struct fc_els_cssp *cp;
311
312 memset(flogi, 0, sizeof(*flogi));
313 flogi->fl_cmd = (u8) op;
314 put_unaligned_be64(lport->wwpn, &flogi->fl_wwpn);
315 put_unaligned_be64(lport->wwnn, &flogi->fl_wwnn);
316 sp = &flogi->fl_csp;
317 sp->sp_hi_ver = 0x20;
318 sp->sp_lo_ver = 0x20;
319 sp->sp_bb_cred = htons(10); /* this gets set by gateway */
320 sp->sp_bb_data = htons((u16) lport->mfs);
321 cp = &flogi->fl_cssp[3 - 1]; /* class 3 parameters */
322 cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
323 if (op != ELS_FLOGI) {
324 sp->sp_features = htons(FC_SP_FT_CIRO);
325 sp->sp_tot_seq = htons(255); /* seq. we accept */
326 sp->sp_rel_off = htons(0x1f);
327 sp->sp_e_d_tov = htonl(lport->e_d_tov);
328
329 cp->cp_rdfs = htons((u16) lport->mfs);
330 cp->cp_con_seq = htons(255);
331 cp->cp_open_seq = 1;
332 }
333}
334
335/*
336 * Add a supported FC-4 type.
337 */
338static void fc_lport_add_fc4_type(struct fc_lport *lport, enum fc_fh_type type)
339{
340 __be32 *mp;
341
342 mp = &lport->fcts.ff_type_map[type / FC_NS_BPW];
343 *mp = htonl(ntohl(*mp) | 1UL << (type % FC_NS_BPW));
344}
345
346/**
Robert Love34f42a02009-02-27 10:55:45 -0800347 * fc_lport_recv_rlir_req() - Handle received Registered Link Incident Report.
Robert Love42e9a922008-12-09 15:10:17 -0800348 * @lport: Fibre Channel local port recieving the RLIR
349 * @sp: current sequence in the RLIR exchange
350 * @fp: RLIR request frame
351 *
352 * Locking Note: The lport lock is exected to be held before calling
353 * this function.
354 */
355static void fc_lport_recv_rlir_req(struct fc_seq *sp, struct fc_frame *fp,
356 struct fc_lport *lport)
357{
Robert Love74147052009-06-10 15:31:10 -0700358 FC_LPORT_DBG(lport, "Received RLIR request while in state %s\n",
359 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800360
361 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
362 fc_frame_free(fp);
363}
364
365/**
Robert Love34f42a02009-02-27 10:55:45 -0800366 * fc_lport_recv_echo_req() - Handle received ECHO request
Robert Love42e9a922008-12-09 15:10:17 -0800367 * @lport: Fibre Channel local port recieving the ECHO
368 * @sp: current sequence in the ECHO exchange
369 * @fp: ECHO request frame
370 *
371 * Locking Note: The lport lock is exected to be held before calling
372 * this function.
373 */
374static void fc_lport_recv_echo_req(struct fc_seq *sp, struct fc_frame *in_fp,
375 struct fc_lport *lport)
376{
377 struct fc_frame *fp;
378 struct fc_exch *ep = fc_seq_exch(sp);
379 unsigned int len;
380 void *pp;
381 void *dp;
382 u32 f_ctl;
383
Robert Love74147052009-06-10 15:31:10 -0700384 FC_LPORT_DBG(lport, "Received RLIR request while in state %s\n",
385 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800386
387 len = fr_len(in_fp) - sizeof(struct fc_frame_header);
388 pp = fc_frame_payload_get(in_fp, len);
389
390 if (len < sizeof(__be32))
391 len = sizeof(__be32);
392
393 fp = fc_frame_alloc(lport, len);
394 if (fp) {
395 dp = fc_frame_payload_get(fp, len);
396 memcpy(dp, pp, len);
397 *((u32 *)dp) = htonl(ELS_LS_ACC << 24);
398 sp = lport->tt.seq_start_next(sp);
399 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
400 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
401 FC_TYPE_ELS, f_ctl, 0);
402 lport->tt.seq_send(lport, sp, fp);
403 }
404 fc_frame_free(in_fp);
405}
406
407/**
Robert Love34f42a02009-02-27 10:55:45 -0800408 * fc_lport_recv_echo_req() - Handle received Request Node ID data request
Robert Love42e9a922008-12-09 15:10:17 -0800409 * @lport: Fibre Channel local port recieving the RNID
410 * @sp: current sequence in the RNID exchange
411 * @fp: RNID request frame
412 *
413 * Locking Note: The lport lock is exected to be held before calling
414 * this function.
415 */
416static void fc_lport_recv_rnid_req(struct fc_seq *sp, struct fc_frame *in_fp,
417 struct fc_lport *lport)
418{
419 struct fc_frame *fp;
420 struct fc_exch *ep = fc_seq_exch(sp);
421 struct fc_els_rnid *req;
422 struct {
423 struct fc_els_rnid_resp rnid;
424 struct fc_els_rnid_cid cid;
425 struct fc_els_rnid_gen gen;
426 } *rp;
427 struct fc_seq_els_data rjt_data;
428 u8 fmt;
429 size_t len;
430 u32 f_ctl;
431
Robert Love74147052009-06-10 15:31:10 -0700432 FC_LPORT_DBG(lport, "Received RNID request while in state %s\n",
433 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800434
435 req = fc_frame_payload_get(in_fp, sizeof(*req));
436 if (!req) {
437 rjt_data.fp = NULL;
438 rjt_data.reason = ELS_RJT_LOGIC;
439 rjt_data.explan = ELS_EXPL_NONE;
440 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
441 } else {
442 fmt = req->rnid_fmt;
443 len = sizeof(*rp);
444 if (fmt != ELS_RNIDF_GEN ||
445 ntohl(lport->rnid_gen.rnid_atype) == 0) {
446 fmt = ELS_RNIDF_NONE; /* nothing to provide */
447 len -= sizeof(rp->gen);
448 }
449 fp = fc_frame_alloc(lport, len);
450 if (fp) {
451 rp = fc_frame_payload_get(fp, len);
452 memset(rp, 0, len);
453 rp->rnid.rnid_cmd = ELS_LS_ACC;
454 rp->rnid.rnid_fmt = fmt;
455 rp->rnid.rnid_cid_len = sizeof(rp->cid);
456 rp->cid.rnid_wwpn = htonll(lport->wwpn);
457 rp->cid.rnid_wwnn = htonll(lport->wwnn);
458 if (fmt == ELS_RNIDF_GEN) {
459 rp->rnid.rnid_sid_len = sizeof(rp->gen);
460 memcpy(&rp->gen, &lport->rnid_gen,
461 sizeof(rp->gen));
462 }
463 sp = lport->tt.seq_start_next(sp);
464 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
465 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
466 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
467 FC_TYPE_ELS, f_ctl, 0);
468 lport->tt.seq_send(lport, sp, fp);
469 }
470 }
471 fc_frame_free(in_fp);
472}
473
474/**
Robert Love34f42a02009-02-27 10:55:45 -0800475 * fc_lport_recv_adisc_req() - Handle received Address Discovery Request
Robert Love42e9a922008-12-09 15:10:17 -0800476 * @lport: Fibre Channel local port recieving the ADISC
477 * @sp: current sequence in the ADISC exchange
478 * @fp: ADISC request frame
479 *
480 * Locking Note: The lport lock is expected to be held before calling
481 * this function.
482 */
483static void fc_lport_recv_adisc_req(struct fc_seq *sp, struct fc_frame *in_fp,
484 struct fc_lport *lport)
485{
486 struct fc_frame *fp;
487 struct fc_exch *ep = fc_seq_exch(sp);
488 struct fc_els_adisc *req, *rp;
489 struct fc_seq_els_data rjt_data;
490 size_t len;
491 u32 f_ctl;
492
Robert Love74147052009-06-10 15:31:10 -0700493 FC_LPORT_DBG(lport, "Received ADISC request while in state %s\n",
494 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800495
496 req = fc_frame_payload_get(in_fp, sizeof(*req));
497 if (!req) {
498 rjt_data.fp = NULL;
499 rjt_data.reason = ELS_RJT_LOGIC;
500 rjt_data.explan = ELS_EXPL_NONE;
501 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
502 } else {
503 len = sizeof(*rp);
504 fp = fc_frame_alloc(lport, len);
505 if (fp) {
506 rp = fc_frame_payload_get(fp, len);
507 memset(rp, 0, len);
508 rp->adisc_cmd = ELS_LS_ACC;
509 rp->adisc_wwpn = htonll(lport->wwpn);
510 rp->adisc_wwnn = htonll(lport->wwnn);
511 hton24(rp->adisc_port_id,
512 fc_host_port_id(lport->host));
513 sp = lport->tt.seq_start_next(sp);
514 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
515 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
516 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
517 FC_TYPE_ELS, f_ctl, 0);
518 lport->tt.seq_send(lport, sp, fp);
519 }
520 }
521 fc_frame_free(in_fp);
522}
523
524/**
Robert Love34f42a02009-02-27 10:55:45 -0800525 * fc_lport_recv_logo_req() - Handle received fabric LOGO request
Robert Love42e9a922008-12-09 15:10:17 -0800526 * @lport: Fibre Channel local port recieving the LOGO
527 * @sp: current sequence in the LOGO exchange
528 * @fp: LOGO request frame
529 *
530 * Locking Note: The lport lock is exected to be held before calling
531 * this function.
532 */
533static void fc_lport_recv_logo_req(struct fc_seq *sp, struct fc_frame *fp,
534 struct fc_lport *lport)
535{
536 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
537 fc_lport_enter_reset(lport);
538 fc_frame_free(fp);
539}
540
541/**
Robert Love34f42a02009-02-27 10:55:45 -0800542 * fc_fabric_login() - Start the lport state machine
Robert Love42e9a922008-12-09 15:10:17 -0800543 * @lport: The lport that should log into the fabric
544 *
545 * Locking Note: This function should not be called
546 * with the lport lock held.
547 */
548int fc_fabric_login(struct fc_lport *lport)
549{
550 int rc = -1;
551
552 mutex_lock(&lport->lp_mutex);
Joe Eykholtb1d9fd52009-07-29 17:04:22 -0700553 if (lport->state == LPORT_ST_DISABLED) {
Robert Love42e9a922008-12-09 15:10:17 -0800554 fc_lport_enter_reset(lport);
555 rc = 0;
556 }
557 mutex_unlock(&lport->lp_mutex);
558
559 return rc;
560}
561EXPORT_SYMBOL(fc_fabric_login);
562
563/**
Robert Love34f42a02009-02-27 10:55:45 -0800564 * fc_linkup() - Handler for transport linkup events
Robert Love42e9a922008-12-09 15:10:17 -0800565 * @lport: The lport whose link is up
566 */
567void fc_linkup(struct fc_lport *lport)
568{
Robert Love74147052009-06-10 15:31:10 -0700569 printk(KERN_INFO "libfc: Link up on port (%6x)\n",
570 fc_host_port_id(lport->host));
Robert Love42e9a922008-12-09 15:10:17 -0800571
572 mutex_lock(&lport->lp_mutex);
Vasu Devbc0e17f2009-02-27 10:54:57 -0800573 if (!lport->link_up) {
574 lport->link_up = 1;
Robert Love42e9a922008-12-09 15:10:17 -0800575
576 if (lport->state == LPORT_ST_RESET)
577 fc_lport_enter_flogi(lport);
578 }
579 mutex_unlock(&lport->lp_mutex);
580}
581EXPORT_SYMBOL(fc_linkup);
582
583/**
Robert Love34f42a02009-02-27 10:55:45 -0800584 * fc_linkdown() - Handler for transport linkdown events
Robert Love42e9a922008-12-09 15:10:17 -0800585 * @lport: The lport whose link is down
586 */
587void fc_linkdown(struct fc_lport *lport)
588{
589 mutex_lock(&lport->lp_mutex);
Robert Love74147052009-06-10 15:31:10 -0700590 printk(KERN_INFO "libfc: Link down on port (%6x)\n",
591 fc_host_port_id(lport->host));
Robert Love42e9a922008-12-09 15:10:17 -0800592
Vasu Devbc0e17f2009-02-27 10:54:57 -0800593 if (lport->link_up) {
594 lport->link_up = 0;
Robert Love42e9a922008-12-09 15:10:17 -0800595 fc_lport_enter_reset(lport);
596 lport->tt.fcp_cleanup(lport);
597 }
598 mutex_unlock(&lport->lp_mutex);
599}
600EXPORT_SYMBOL(fc_linkdown);
601
602/**
Robert Love34f42a02009-02-27 10:55:45 -0800603 * fc_fabric_logoff() - Logout of the fabric
Robert Love42e9a922008-12-09 15:10:17 -0800604 * @lport: fc_lport pointer to logoff the fabric
605 *
606 * Return value:
607 * 0 for success, -1 for failure
Robert Love34f42a02009-02-27 10:55:45 -0800608 */
Robert Love42e9a922008-12-09 15:10:17 -0800609int fc_fabric_logoff(struct fc_lport *lport)
610{
611 lport->tt.disc_stop_final(lport);
612 mutex_lock(&lport->lp_mutex);
Abhijeet Joglekara0fd2e42009-04-21 16:27:09 -0700613 if (lport->dns_rp)
614 lport->tt.rport_logoff(lport->dns_rp);
615 mutex_unlock(&lport->lp_mutex);
616 lport->tt.rport_flush_queue();
617 mutex_lock(&lport->lp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800618 fc_lport_enter_logo(lport);
619 mutex_unlock(&lport->lp_mutex);
Steve Maf7db2c12009-02-27 10:55:13 -0800620 cancel_delayed_work_sync(&lport->retry_work);
Robert Love42e9a922008-12-09 15:10:17 -0800621 return 0;
622}
623EXPORT_SYMBOL(fc_fabric_logoff);
624
625/**
Robert Love34f42a02009-02-27 10:55:45 -0800626 * fc_lport_destroy() - unregister a fc_lport
Robert Love42e9a922008-12-09 15:10:17 -0800627 * @lport: fc_lport pointer to unregister
628 *
629 * Return value:
630 * None
631 * Note:
632 * exit routine for fc_lport instance
633 * clean-up all the allocated memory
634 * and free up other system resources.
635 *
Robert Love34f42a02009-02-27 10:55:45 -0800636 */
Robert Love42e9a922008-12-09 15:10:17 -0800637int fc_lport_destroy(struct fc_lport *lport)
638{
Abhijeet Joglekarbbf15662009-04-21 16:27:14 -0700639 mutex_lock(&lport->lp_mutex);
Joe Eykholtb1d9fd52009-07-29 17:04:22 -0700640 lport->state = LPORT_ST_DISABLED;
Abhijeet Joglekarbbf15662009-04-21 16:27:14 -0700641 lport->link_up = 0;
Robert Love42e9a922008-12-09 15:10:17 -0800642 lport->tt.frame_send = fc_frame_drop;
Abhijeet Joglekarbbf15662009-04-21 16:27:14 -0700643 mutex_unlock(&lport->lp_mutex);
644
Robert Love42e9a922008-12-09 15:10:17 -0800645 lport->tt.fcp_abort_io(lport);
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -0800646 lport->tt.exch_mgr_reset(lport, 0, 0);
Robert Love42e9a922008-12-09 15:10:17 -0800647 return 0;
648}
649EXPORT_SYMBOL(fc_lport_destroy);
650
651/**
Robert Love34f42a02009-02-27 10:55:45 -0800652 * fc_set_mfs() - sets up the mfs for the corresponding fc_lport
Robert Love42e9a922008-12-09 15:10:17 -0800653 * @lport: fc_lport pointer to unregister
654 * @mfs: the new mfs for fc_lport
655 *
656 * Set mfs for the given fc_lport to the new mfs.
657 *
658 * Return: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800659 */
Robert Love42e9a922008-12-09 15:10:17 -0800660int fc_set_mfs(struct fc_lport *lport, u32 mfs)
661{
662 unsigned int old_mfs;
663 int rc = -EINVAL;
664
665 mutex_lock(&lport->lp_mutex);
666
667 old_mfs = lport->mfs;
668
669 if (mfs >= FC_MIN_MAX_FRAME) {
670 mfs &= ~3;
671 if (mfs > FC_MAX_FRAME)
672 mfs = FC_MAX_FRAME;
673 mfs -= sizeof(struct fc_frame_header);
674 lport->mfs = mfs;
675 rc = 0;
676 }
677
678 if (!rc && mfs < old_mfs)
679 fc_lport_enter_reset(lport);
680
681 mutex_unlock(&lport->lp_mutex);
682
683 return rc;
684}
685EXPORT_SYMBOL(fc_set_mfs);
686
687/**
Robert Love34f42a02009-02-27 10:55:45 -0800688 * fc_lport_disc_callback() - Callback for discovery events
Robert Love42e9a922008-12-09 15:10:17 -0800689 * @lport: FC local port
690 * @event: The discovery event
691 */
692void fc_lport_disc_callback(struct fc_lport *lport, enum fc_disc_event event)
693{
694 switch (event) {
695 case DISC_EV_SUCCESS:
Robert Love74147052009-06-10 15:31:10 -0700696 FC_LPORT_DBG(lport, "Discovery succeeded\n");
Robert Love42e9a922008-12-09 15:10:17 -0800697 break;
698 case DISC_EV_FAILED:
Robert Love74147052009-06-10 15:31:10 -0700699 printk(KERN_ERR "libfc: Discovery failed for port (%6x)\n",
700 fc_host_port_id(lport->host));
Robert Love42e9a922008-12-09 15:10:17 -0800701 mutex_lock(&lport->lp_mutex);
702 fc_lport_enter_reset(lport);
703 mutex_unlock(&lport->lp_mutex);
704 break;
705 case DISC_EV_NONE:
706 WARN_ON(1);
707 break;
708 }
709}
710
711/**
Robert Love34f42a02009-02-27 10:55:45 -0800712 * fc_rport_enter_ready() - Enter the ready state and start discovery
Robert Love42e9a922008-12-09 15:10:17 -0800713 * @lport: Fibre Channel local port that is ready
714 *
715 * Locking Note: The lport lock is expected to be held before calling
716 * this routine.
717 */
718static void fc_lport_enter_ready(struct fc_lport *lport)
719{
Robert Love74147052009-06-10 15:31:10 -0700720 FC_LPORT_DBG(lport, "Entered READY from state %s\n",
721 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800722
723 fc_lport_state_enter(lport, LPORT_ST_READY);
724
725 lport->tt.disc_start(fc_lport_disc_callback, lport);
726}
727
728/**
Robert Love34f42a02009-02-27 10:55:45 -0800729 * fc_lport_recv_flogi_req() - Receive a FLOGI request
Robert Love42e9a922008-12-09 15:10:17 -0800730 * @sp_in: The sequence the FLOGI is on
731 * @rx_fp: The frame the FLOGI is in
732 * @lport: The lport that recieved the request
733 *
734 * A received FLOGI request indicates a point-to-point connection.
735 * Accept it with the common service parameters indicating our N port.
736 * Set up to do a PLOGI if we have the higher-number WWPN.
737 *
738 * Locking Note: The lport lock is exected to be held before calling
739 * this function.
740 */
741static void fc_lport_recv_flogi_req(struct fc_seq *sp_in,
742 struct fc_frame *rx_fp,
743 struct fc_lport *lport)
744{
745 struct fc_frame *fp;
746 struct fc_frame_header *fh;
747 struct fc_seq *sp;
748 struct fc_exch *ep;
749 struct fc_els_flogi *flp;
750 struct fc_els_flogi *new_flp;
751 u64 remote_wwpn;
752 u32 remote_fid;
753 u32 local_fid;
754 u32 f_ctl;
755
Robert Love74147052009-06-10 15:31:10 -0700756 FC_LPORT_DBG(lport, "Received FLOGI request while in state %s\n",
757 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800758
759 fh = fc_frame_header_get(rx_fp);
760 remote_fid = ntoh24(fh->fh_s_id);
761 flp = fc_frame_payload_get(rx_fp, sizeof(*flp));
762 if (!flp)
763 goto out;
764 remote_wwpn = get_unaligned_be64(&flp->fl_wwpn);
765 if (remote_wwpn == lport->wwpn) {
Robert Love74147052009-06-10 15:31:10 -0700766 printk(KERN_WARNING "libfc: Received FLOGI from port "
767 "with same WWPN %llx\n", remote_wwpn);
Robert Love42e9a922008-12-09 15:10:17 -0800768 goto out;
769 }
Robert Love74147052009-06-10 15:31:10 -0700770 FC_LPORT_DBG(lport, "FLOGI from port WWPN %llx\n", 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 {
Robert Love74147052009-06-10 15:31:10 -0700902 FC_LPORT_DBG(lport, "dropping invalid frame (eof %x)\n",
903 fr_eof(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800904 fc_frame_free(fp);
905 }
906 mutex_unlock(&lport->lp_mutex);
907
908 /*
909 * The common exch_done for all request may not be good
910 * if any request requires longer hold on exhange. XXX
911 */
912 lport->tt.exch_done(sp);
913}
914
915/**
Robert Love34f42a02009-02-27 10:55:45 -0800916 * fc_lport_reset() - Reset an lport
Robert Love42e9a922008-12-09 15:10:17 -0800917 * @lport: The lport which should be reset
918 *
919 * Locking Note: This functions should not be called with the
920 * lport lock held.
921 */
922int fc_lport_reset(struct fc_lport *lport)
923{
Steve Maf7db2c12009-02-27 10:55:13 -0800924 cancel_delayed_work_sync(&lport->retry_work);
Robert Love42e9a922008-12-09 15:10:17 -0800925 mutex_lock(&lport->lp_mutex);
926 fc_lport_enter_reset(lport);
927 mutex_unlock(&lport->lp_mutex);
928 return 0;
929}
930EXPORT_SYMBOL(fc_lport_reset);
931
932/**
Joe Eykholt1190d922009-07-29 17:04:27 -0700933 * fc_lport_reset_locked() - Reset the local port
Robert Love42e9a922008-12-09 15:10:17 -0800934 * @lport: Fibre Channel local port to be reset
935 *
936 * Locking Note: The lport lock is expected to be held before calling
937 * this routine.
938 */
Joe Eykholt1190d922009-07-29 17:04:27 -0700939static void fc_lport_reset_locked(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -0800940{
Robert Love42e9a922008-12-09 15:10:17 -0800941 if (lport->dns_rp)
942 lport->tt.rport_logoff(lport->dns_rp);
943
944 if (lport->ptp_rp) {
945 lport->tt.rport_logoff(lport->ptp_rp);
946 lport->ptp_rp = NULL;
947 }
948
949 lport->tt.disc_stop(lport);
950
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -0800951 lport->tt.exch_mgr_reset(lport, 0, 0);
Robert Love42e9a922008-12-09 15:10:17 -0800952 fc_host_fabric_name(lport->host) = 0;
953 fc_host_port_id(lport->host) = 0;
Joe Eykholt1190d922009-07-29 17:04:27 -0700954}
Robert Love42e9a922008-12-09 15:10:17 -0800955
Joe Eykholt1190d922009-07-29 17:04:27 -0700956/**
957 * fc_lport_enter_reset() - Reset the local port
958 * @lport: Fibre Channel local port to be reset
959 *
960 * Locking Note: The lport lock is expected to be held before calling
961 * this routine.
962 */
963static void fc_lport_enter_reset(struct fc_lport *lport)
964{
965 FC_LPORT_DBG(lport, "Entered RESET state from %s state\n",
966 fc_lport_state(lport));
967
968 fc_lport_state_enter(lport, LPORT_ST_RESET);
969 fc_lport_reset_locked(lport);
Vasu Devbc0e17f2009-02-27 10:54:57 -0800970 if (lport->link_up)
Robert Love42e9a922008-12-09 15:10:17 -0800971 fc_lport_enter_flogi(lport);
972}
973
974/**
Joe Eykholt1190d922009-07-29 17:04:27 -0700975 * fc_lport_enter_disabled() - disable the local port
976 * @lport: Fibre Channel local port to be reset
977 *
978 * Locking Note: The lport lock is expected to be held before calling
979 * this routine.
980 */
981static void fc_lport_enter_disabled(struct fc_lport *lport)
982{
983 FC_LPORT_DBG(lport, "Entered disabled state from %s state\n",
984 fc_lport_state(lport));
985
986 fc_lport_state_enter(lport, LPORT_ST_DISABLED);
987 fc_lport_reset_locked(lport);
988}
989
990/**
Robert Love34f42a02009-02-27 10:55:45 -0800991 * fc_lport_error() - Handler for any errors
Robert Love42e9a922008-12-09 15:10:17 -0800992 * @lport: The fc_lport object
993 * @fp: The frame pointer
994 *
995 * If the error was caused by a resource allocation failure
996 * then wait for half a second and retry, otherwise retry
997 * after the e_d_tov time.
998 */
999static void fc_lport_error(struct fc_lport *lport, struct fc_frame *fp)
1000{
1001 unsigned long delay = 0;
Robert Love74147052009-06-10 15:31:10 -07001002 FC_LPORT_DBG(lport, "Error %ld in state %s, retries %d\n",
1003 PTR_ERR(fp), fc_lport_state(lport),
1004 lport->retry_count);
Robert Love42e9a922008-12-09 15:10:17 -08001005
1006 if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
1007 /*
1008 * Memory allocation failure, or the exchange timed out.
1009 * Retry after delay
1010 */
1011 if (lport->retry_count < lport->max_retry_count) {
1012 lport->retry_count++;
1013 if (!fp)
1014 delay = msecs_to_jiffies(500);
1015 else
1016 delay = msecs_to_jiffies(lport->e_d_tov);
1017
1018 schedule_delayed_work(&lport->retry_work, delay);
1019 } else {
1020 switch (lport->state) {
Joe Eykholtb1d9fd52009-07-29 17:04:22 -07001021 case LPORT_ST_DISABLED:
Robert Love42e9a922008-12-09 15:10:17 -08001022 case LPORT_ST_READY:
1023 case LPORT_ST_RESET:
1024 case LPORT_ST_RPN_ID:
1025 case LPORT_ST_RFT_ID:
1026 case LPORT_ST_SCR:
1027 case LPORT_ST_DNS:
1028 case LPORT_ST_FLOGI:
1029 case LPORT_ST_LOGO:
1030 fc_lport_enter_reset(lport);
1031 break;
1032 }
1033 }
1034 }
1035}
1036
1037/**
Robert Love34f42a02009-02-27 10:55:45 -08001038 * fc_lport_rft_id_resp() - Handle response to Register Fibre
1039 * Channel Types by ID (RPN_ID) request
Robert Love42e9a922008-12-09 15:10:17 -08001040 * @sp: current sequence in RPN_ID exchange
1041 * @fp: response frame
1042 * @lp_arg: Fibre Channel host port instance
1043 *
1044 * Locking Note: This function will be called without the lport lock
1045 * held, but it will lock, call an _enter_* function or fc_lport_error
1046 * and then unlock the lport.
1047 */
1048static void fc_lport_rft_id_resp(struct fc_seq *sp, struct fc_frame *fp,
1049 void *lp_arg)
1050{
1051 struct fc_lport *lport = lp_arg;
1052 struct fc_frame_header *fh;
1053 struct fc_ct_hdr *ct;
1054
1055 if (fp == ERR_PTR(-FC_EX_CLOSED))
1056 return;
1057
1058 mutex_lock(&lport->lp_mutex);
1059
Robert Love74147052009-06-10 15:31:10 -07001060 FC_LPORT_DBG(lport, "Received a RFT_ID response\n");
Robert Love42e9a922008-12-09 15:10:17 -08001061
1062 if (lport->state != LPORT_ST_RFT_ID) {
Robert Love74147052009-06-10 15:31:10 -07001063 FC_LPORT_DBG(lport, "Received a RFT_ID response, but in state "
1064 "%s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001065 if (IS_ERR(fp))
1066 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001067 goto out;
1068 }
1069
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001070 if (IS_ERR(fp)) {
1071 fc_lport_error(lport, fp);
1072 goto err;
1073 }
1074
Robert Love42e9a922008-12-09 15:10:17 -08001075 fh = fc_frame_header_get(fp);
1076 ct = fc_frame_payload_get(fp, sizeof(*ct));
1077
1078 if (fh && ct && fh->fh_type == FC_TYPE_CT &&
1079 ct->ct_fs_type == FC_FST_DIR &&
1080 ct->ct_fs_subtype == FC_NS_SUBTYPE &&
1081 ntohs(ct->ct_cmd) == FC_FS_ACC)
1082 fc_lport_enter_scr(lport);
1083 else
1084 fc_lport_error(lport, fp);
1085out:
1086 fc_frame_free(fp);
1087err:
1088 mutex_unlock(&lport->lp_mutex);
1089}
1090
1091/**
Robert Love34f42a02009-02-27 10:55:45 -08001092 * fc_lport_rpn_id_resp() - Handle response to Register Port
1093 * Name by ID (RPN_ID) request
Robert Love42e9a922008-12-09 15:10:17 -08001094 * @sp: current sequence in RPN_ID exchange
1095 * @fp: response frame
1096 * @lp_arg: Fibre Channel host port instance
1097 *
1098 * Locking Note: This function will be called without the lport lock
1099 * held, but it will lock, call an _enter_* function or fc_lport_error
1100 * and then unlock the lport.
1101 */
1102static void fc_lport_rpn_id_resp(struct fc_seq *sp, struct fc_frame *fp,
1103 void *lp_arg)
1104{
1105 struct fc_lport *lport = lp_arg;
1106 struct fc_frame_header *fh;
1107 struct fc_ct_hdr *ct;
1108
1109 if (fp == ERR_PTR(-FC_EX_CLOSED))
1110 return;
1111
1112 mutex_lock(&lport->lp_mutex);
1113
Robert Love74147052009-06-10 15:31:10 -07001114 FC_LPORT_DBG(lport, "Received a RPN_ID response\n");
Robert Love42e9a922008-12-09 15:10:17 -08001115
1116 if (lport->state != LPORT_ST_RPN_ID) {
Robert Love74147052009-06-10 15:31:10 -07001117 FC_LPORT_DBG(lport, "Received a RPN_ID response, but in state "
1118 "%s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001119 if (IS_ERR(fp))
1120 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001121 goto out;
1122 }
1123
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001124 if (IS_ERR(fp)) {
1125 fc_lport_error(lport, fp);
1126 goto err;
1127 }
1128
Robert Love42e9a922008-12-09 15:10:17 -08001129 fh = fc_frame_header_get(fp);
1130 ct = fc_frame_payload_get(fp, sizeof(*ct));
1131 if (fh && ct && fh->fh_type == FC_TYPE_CT &&
1132 ct->ct_fs_type == FC_FST_DIR &&
1133 ct->ct_fs_subtype == FC_NS_SUBTYPE &&
1134 ntohs(ct->ct_cmd) == FC_FS_ACC)
1135 fc_lport_enter_rft_id(lport);
1136 else
1137 fc_lport_error(lport, fp);
1138
1139out:
1140 fc_frame_free(fp);
1141err:
1142 mutex_unlock(&lport->lp_mutex);
1143}
1144
1145/**
Robert Love34f42a02009-02-27 10:55:45 -08001146 * fc_lport_scr_resp() - Handle response to State Change Register (SCR) request
Robert Love42e9a922008-12-09 15:10:17 -08001147 * @sp: current sequence in SCR exchange
1148 * @fp: response frame
1149 * @lp_arg: Fibre Channel lport port instance that sent the registration request
1150 *
1151 * Locking Note: This function will be called without the lport lock
1152 * held, but it will lock, call an _enter_* function or fc_lport_error
1153 * and then unlock the lport.
1154 */
1155static void fc_lport_scr_resp(struct fc_seq *sp, struct fc_frame *fp,
1156 void *lp_arg)
1157{
1158 struct fc_lport *lport = lp_arg;
1159 u8 op;
1160
1161 if (fp == ERR_PTR(-FC_EX_CLOSED))
1162 return;
1163
1164 mutex_lock(&lport->lp_mutex);
1165
Robert Love74147052009-06-10 15:31:10 -07001166 FC_LPORT_DBG(lport, "Received a SCR response\n");
Robert Love42e9a922008-12-09 15:10:17 -08001167
1168 if (lport->state != LPORT_ST_SCR) {
Robert Love74147052009-06-10 15:31:10 -07001169 FC_LPORT_DBG(lport, "Received a SCR response, but in state "
1170 "%s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001171 if (IS_ERR(fp))
1172 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001173 goto out;
1174 }
1175
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001176 if (IS_ERR(fp)) {
1177 fc_lport_error(lport, fp);
1178 goto err;
1179 }
1180
Robert Love42e9a922008-12-09 15:10:17 -08001181 op = fc_frame_payload_op(fp);
1182 if (op == ELS_LS_ACC)
1183 fc_lport_enter_ready(lport);
1184 else
1185 fc_lport_error(lport, fp);
1186
1187out:
1188 fc_frame_free(fp);
1189err:
1190 mutex_unlock(&lport->lp_mutex);
1191}
1192
1193/**
Robert Love34f42a02009-02-27 10:55:45 -08001194 * fc_lport_enter_scr() - Send a State Change Register (SCR) request
Robert Love42e9a922008-12-09 15:10:17 -08001195 * @lport: Fibre Channel local port to register for state changes
1196 *
1197 * Locking Note: The lport lock is expected to be held before calling
1198 * this routine.
1199 */
1200static void fc_lport_enter_scr(struct fc_lport *lport)
1201{
1202 struct fc_frame *fp;
1203
Robert Love74147052009-06-10 15:31:10 -07001204 FC_LPORT_DBG(lport, "Entered SCR state from %s state\n",
1205 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001206
1207 fc_lport_state_enter(lport, LPORT_ST_SCR);
1208
1209 fp = fc_frame_alloc(lport, sizeof(struct fc_els_scr));
1210 if (!fp) {
1211 fc_lport_error(lport, fp);
1212 return;
1213 }
1214
1215 if (!lport->tt.elsct_send(lport, NULL, fp, ELS_SCR,
1216 fc_lport_scr_resp, lport, lport->e_d_tov))
1217 fc_lport_error(lport, fp);
1218}
1219
1220/**
Robert Love34f42a02009-02-27 10:55:45 -08001221 * fc_lport_enter_rft_id() - Register FC4-types with the name server
Robert Love42e9a922008-12-09 15:10:17 -08001222 * @lport: Fibre Channel local port to register
1223 *
1224 * Locking Note: The lport lock is expected to be held before calling
1225 * this routine.
1226 */
1227static void fc_lport_enter_rft_id(struct fc_lport *lport)
1228{
1229 struct fc_frame *fp;
1230 struct fc_ns_fts *lps;
1231 int i;
1232
Robert Love74147052009-06-10 15:31:10 -07001233 FC_LPORT_DBG(lport, "Entered RFT_ID state from %s state\n",
1234 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001235
1236 fc_lport_state_enter(lport, LPORT_ST_RFT_ID);
1237
1238 lps = &lport->fcts;
1239 i = sizeof(lps->ff_type_map) / sizeof(lps->ff_type_map[0]);
1240 while (--i >= 0)
1241 if (ntohl(lps->ff_type_map[i]) != 0)
1242 break;
1243 if (i < 0) {
1244 /* nothing to register, move on to SCR */
1245 fc_lport_enter_scr(lport);
1246 return;
1247 }
1248
1249 fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
1250 sizeof(struct fc_ns_rft));
1251 if (!fp) {
1252 fc_lport_error(lport, fp);
1253 return;
1254 }
1255
1256 if (!lport->tt.elsct_send(lport, NULL, fp, FC_NS_RFT_ID,
1257 fc_lport_rft_id_resp,
1258 lport, lport->e_d_tov))
1259 fc_lport_error(lport, fp);
1260}
1261
1262/**
Robert Love34f42a02009-02-27 10:55:45 -08001263 * fc_rport_enter_rft_id() - Register port name with the name server
Robert Love42e9a922008-12-09 15:10:17 -08001264 * @lport: Fibre Channel local port to register
1265 *
1266 * Locking Note: The lport lock is expected to be held before calling
1267 * this routine.
1268 */
1269static void fc_lport_enter_rpn_id(struct fc_lport *lport)
1270{
1271 struct fc_frame *fp;
1272
Robert Love74147052009-06-10 15:31:10 -07001273 FC_LPORT_DBG(lport, "Entered RPN_ID state from %s state\n",
1274 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001275
1276 fc_lport_state_enter(lport, LPORT_ST_RPN_ID);
1277
1278 fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
1279 sizeof(struct fc_ns_rn_id));
1280 if (!fp) {
1281 fc_lport_error(lport, fp);
1282 return;
1283 }
1284
1285 if (!lport->tt.elsct_send(lport, NULL, fp, FC_NS_RPN_ID,
1286 fc_lport_rpn_id_resp,
1287 lport, lport->e_d_tov))
1288 fc_lport_error(lport, fp);
1289}
1290
1291static struct fc_rport_operations fc_lport_rport_ops = {
1292 .event_callback = fc_lport_rport_callback,
1293};
1294
1295/**
Robert Love34f42a02009-02-27 10:55:45 -08001296 * fc_rport_enter_dns() - Create a rport to the name server
Robert Love42e9a922008-12-09 15:10:17 -08001297 * @lport: Fibre Channel local port requesting a rport for the name server
1298 *
1299 * Locking Note: The lport lock is expected to be held before calling
1300 * this routine.
1301 */
1302static void fc_lport_enter_dns(struct fc_lport *lport)
1303{
1304 struct fc_rport *rport;
1305 struct fc_rport_libfc_priv *rdata;
1306 struct fc_disc_port dp;
1307
1308 dp.ids.port_id = FC_FID_DIR_SERV;
1309 dp.ids.port_name = -1;
1310 dp.ids.node_name = -1;
1311 dp.ids.roles = FC_RPORT_ROLE_UNKNOWN;
1312 dp.lp = lport;
1313
Robert Love74147052009-06-10 15:31:10 -07001314 FC_LPORT_DBG(lport, "Entered DNS state from %s state\n",
1315 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001316
1317 fc_lport_state_enter(lport, LPORT_ST_DNS);
1318
Robert Love5101ff92009-02-27 10:55:18 -08001319 rport = lport->tt.rport_create(&dp);
Robert Love42e9a922008-12-09 15:10:17 -08001320 if (!rport)
1321 goto err;
1322
1323 rdata = rport->dd_data;
1324 rdata->ops = &fc_lport_rport_ops;
1325 lport->tt.rport_login(rport);
1326 return;
1327
1328err:
1329 fc_lport_error(lport, NULL);
1330}
1331
1332/**
Robert Love34f42a02009-02-27 10:55:45 -08001333 * fc_lport_timeout() - Handler for the retry_work timer.
Robert Love42e9a922008-12-09 15:10:17 -08001334 * @work: The work struct of the fc_lport
1335 */
1336static void fc_lport_timeout(struct work_struct *work)
1337{
1338 struct fc_lport *lport =
1339 container_of(work, struct fc_lport,
1340 retry_work.work);
1341
1342 mutex_lock(&lport->lp_mutex);
1343
1344 switch (lport->state) {
Joe Eykholtb1d9fd52009-07-29 17:04:22 -07001345 case LPORT_ST_DISABLED:
Robert Love42e9a922008-12-09 15:10:17 -08001346 case LPORT_ST_READY:
1347 case LPORT_ST_RESET:
1348 WARN_ON(1);
1349 break;
1350 case LPORT_ST_FLOGI:
1351 fc_lport_enter_flogi(lport);
1352 break;
1353 case LPORT_ST_DNS:
1354 fc_lport_enter_dns(lport);
1355 break;
1356 case LPORT_ST_RPN_ID:
1357 fc_lport_enter_rpn_id(lport);
1358 break;
1359 case LPORT_ST_RFT_ID:
1360 fc_lport_enter_rft_id(lport);
1361 break;
1362 case LPORT_ST_SCR:
1363 fc_lport_enter_scr(lport);
1364 break;
1365 case LPORT_ST_LOGO:
1366 fc_lport_enter_logo(lport);
1367 break;
1368 }
1369
1370 mutex_unlock(&lport->lp_mutex);
1371}
1372
1373/**
Robert Love34f42a02009-02-27 10:55:45 -08001374 * fc_lport_logo_resp() - Handle response to LOGO request
Robert Love42e9a922008-12-09 15:10:17 -08001375 * @sp: current sequence in LOGO exchange
1376 * @fp: response frame
1377 * @lp_arg: Fibre Channel lport port instance that sent the LOGO request
1378 *
1379 * Locking Note: This function will be called without the lport lock
1380 * held, but it will lock, call an _enter_* function or fc_lport_error
1381 * and then unlock the lport.
1382 */
1383static void fc_lport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
1384 void *lp_arg)
1385{
1386 struct fc_lport *lport = lp_arg;
1387 u8 op;
1388
1389 if (fp == ERR_PTR(-FC_EX_CLOSED))
1390 return;
1391
1392 mutex_lock(&lport->lp_mutex);
1393
Robert Love74147052009-06-10 15:31:10 -07001394 FC_LPORT_DBG(lport, "Received a LOGO response\n");
Robert Love42e9a922008-12-09 15:10:17 -08001395
1396 if (lport->state != LPORT_ST_LOGO) {
Robert Love74147052009-06-10 15:31:10 -07001397 FC_LPORT_DBG(lport, "Received a LOGO response, but in state "
1398 "%s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001399 if (IS_ERR(fp))
1400 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001401 goto out;
1402 }
1403
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001404 if (IS_ERR(fp)) {
1405 fc_lport_error(lport, fp);
1406 goto err;
1407 }
1408
Robert Love42e9a922008-12-09 15:10:17 -08001409 op = fc_frame_payload_op(fp);
1410 if (op == ELS_LS_ACC)
Joe Eykholt1190d922009-07-29 17:04:27 -07001411 fc_lport_enter_disabled(lport);
Robert Love42e9a922008-12-09 15:10:17 -08001412 else
1413 fc_lport_error(lport, fp);
1414
1415out:
1416 fc_frame_free(fp);
1417err:
1418 mutex_unlock(&lport->lp_mutex);
1419}
1420
1421/**
Robert Love34f42a02009-02-27 10:55:45 -08001422 * fc_rport_enter_logo() - Logout of the fabric
Robert Love42e9a922008-12-09 15:10:17 -08001423 * @lport: Fibre Channel local port to be logged out
1424 *
1425 * Locking Note: The lport lock is expected to be held before calling
1426 * this routine.
1427 */
1428static void fc_lport_enter_logo(struct fc_lport *lport)
1429{
1430 struct fc_frame *fp;
1431 struct fc_els_logo *logo;
1432
Robert Love74147052009-06-10 15:31:10 -07001433 FC_LPORT_DBG(lport, "Entered LOGO state from %s state\n",
1434 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001435
1436 fc_lport_state_enter(lport, LPORT_ST_LOGO);
1437
Robert Love42e9a922008-12-09 15:10:17 -08001438 fp = fc_frame_alloc(lport, sizeof(*logo));
1439 if (!fp) {
1440 fc_lport_error(lport, fp);
1441 return;
1442 }
1443
1444 if (!lport->tt.elsct_send(lport, NULL, fp, ELS_LOGO, fc_lport_logo_resp,
1445 lport, lport->e_d_tov))
1446 fc_lport_error(lport, fp);
1447}
1448
1449/**
Robert Love34f42a02009-02-27 10:55:45 -08001450 * fc_lport_flogi_resp() - Handle response to FLOGI request
Robert Love42e9a922008-12-09 15:10:17 -08001451 * @sp: current sequence in FLOGI exchange
1452 * @fp: response frame
1453 * @lp_arg: Fibre Channel lport port instance that sent the FLOGI request
1454 *
1455 * Locking Note: This function will be called without the lport lock
1456 * held, but it will lock, call an _enter_* function or fc_lport_error
1457 * and then unlock the lport.
1458 */
1459static void fc_lport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
1460 void *lp_arg)
1461{
1462 struct fc_lport *lport = lp_arg;
1463 struct fc_frame_header *fh;
1464 struct fc_els_flogi *flp;
1465 u32 did;
1466 u16 csp_flags;
1467 unsigned int r_a_tov;
1468 unsigned int e_d_tov;
1469 u16 mfs;
1470
1471 if (fp == ERR_PTR(-FC_EX_CLOSED))
1472 return;
1473
1474 mutex_lock(&lport->lp_mutex);
1475
Robert Love74147052009-06-10 15:31:10 -07001476 FC_LPORT_DBG(lport, "Received a FLOGI response\n");
Robert Love42e9a922008-12-09 15:10:17 -08001477
1478 if (lport->state != LPORT_ST_FLOGI) {
Robert Love74147052009-06-10 15:31:10 -07001479 FC_LPORT_DBG(lport, "Received a FLOGI response, but in state "
1480 "%s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001481 if (IS_ERR(fp))
1482 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001483 goto out;
1484 }
1485
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001486 if (IS_ERR(fp)) {
1487 fc_lport_error(lport, fp);
1488 goto err;
1489 }
1490
Robert Love42e9a922008-12-09 15:10:17 -08001491 fh = fc_frame_header_get(fp);
1492 did = ntoh24(fh->fh_d_id);
1493 if (fc_frame_payload_op(fp) == ELS_LS_ACC && did != 0) {
1494
Robert Love74147052009-06-10 15:31:10 -07001495 printk(KERN_INFO "libfc: Assigned FID (%6x) in FLOGI response\n",
1496 did);
Robert Love42e9a922008-12-09 15:10:17 -08001497 fc_host_port_id(lport->host) = did;
1498
1499 flp = fc_frame_payload_get(fp, sizeof(*flp));
1500 if (flp) {
1501 mfs = ntohs(flp->fl_csp.sp_bb_data) &
1502 FC_SP_BB_DATA_MASK;
1503 if (mfs >= FC_SP_MIN_MAX_PAYLOAD &&
1504 mfs < lport->mfs)
1505 lport->mfs = mfs;
1506 csp_flags = ntohs(flp->fl_csp.sp_features);
1507 r_a_tov = ntohl(flp->fl_csp.sp_r_a_tov);
1508 e_d_tov = ntohl(flp->fl_csp.sp_e_d_tov);
1509 if (csp_flags & FC_SP_FT_EDTR)
1510 e_d_tov /= 1000000;
1511 if ((csp_flags & FC_SP_FT_FPORT) == 0) {
1512 if (e_d_tov > lport->e_d_tov)
1513 lport->e_d_tov = e_d_tov;
1514 lport->r_a_tov = 2 * e_d_tov;
Robert Love74147052009-06-10 15:31:10 -07001515 printk(KERN_INFO "libfc: Port (%6x) entered "
1516 "point to point mode\n", did);
Robert Love42e9a922008-12-09 15:10:17 -08001517 fc_lport_ptp_setup(lport, ntoh24(fh->fh_s_id),
1518 get_unaligned_be64(
1519 &flp->fl_wwpn),
1520 get_unaligned_be64(
1521 &flp->fl_wwnn));
1522 } else {
1523 lport->e_d_tov = e_d_tov;
1524 lport->r_a_tov = r_a_tov;
1525 fc_host_fabric_name(lport->host) =
1526 get_unaligned_be64(&flp->fl_wwnn);
1527 fc_lport_enter_dns(lport);
1528 }
1529 }
1530
1531 if (flp) {
1532 csp_flags = ntohs(flp->fl_csp.sp_features);
1533 if ((csp_flags & FC_SP_FT_FPORT) == 0) {
1534 lport->tt.disc_start(fc_lport_disc_callback,
1535 lport);
1536 }
1537 }
1538 } else {
Robert Love74147052009-06-10 15:31:10 -07001539 FC_LPORT_DBG(lport, "Bad FLOGI response\n");
Robert Love42e9a922008-12-09 15:10:17 -08001540 }
1541
1542out:
1543 fc_frame_free(fp);
1544err:
1545 mutex_unlock(&lport->lp_mutex);
1546}
1547
1548/**
Robert Love34f42a02009-02-27 10:55:45 -08001549 * fc_rport_enter_flogi() - Send a FLOGI request to the fabric manager
Robert Love42e9a922008-12-09 15:10:17 -08001550 * @lport: Fibre Channel local port to be logged in to the fabric
1551 *
1552 * Locking Note: The lport lock is expected to be held before calling
1553 * this routine.
1554 */
1555void fc_lport_enter_flogi(struct fc_lport *lport)
1556{
1557 struct fc_frame *fp;
1558
Robert Love74147052009-06-10 15:31:10 -07001559 FC_LPORT_DBG(lport, "Entered FLOGI state from %s state\n",
1560 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001561
1562 fc_lport_state_enter(lport, LPORT_ST_FLOGI);
1563
1564 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
1565 if (!fp)
1566 return fc_lport_error(lport, fp);
1567
1568 if (!lport->tt.elsct_send(lport, NULL, fp, ELS_FLOGI,
1569 fc_lport_flogi_resp, lport, lport->e_d_tov))
1570 fc_lport_error(lport, fp);
1571}
1572
1573/* Configure a fc_lport */
1574int fc_lport_config(struct fc_lport *lport)
1575{
1576 INIT_DELAYED_WORK(&lport->retry_work, fc_lport_timeout);
1577 mutex_init(&lport->lp_mutex);
1578
Joe Eykholtb1d9fd52009-07-29 17:04:22 -07001579 fc_lport_state_enter(lport, LPORT_ST_DISABLED);
Robert Love42e9a922008-12-09 15:10:17 -08001580
1581 fc_lport_add_fc4_type(lport, FC_TYPE_FCP);
1582 fc_lport_add_fc4_type(lport, FC_TYPE_CT);
1583
1584 return 0;
1585}
1586EXPORT_SYMBOL(fc_lport_config);
1587
1588int fc_lport_init(struct fc_lport *lport)
1589{
1590 if (!lport->tt.lport_recv)
1591 lport->tt.lport_recv = fc_lport_recv_req;
1592
1593 if (!lport->tt.lport_reset)
1594 lport->tt.lport_reset = fc_lport_reset;
1595
1596 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
1597 fc_host_node_name(lport->host) = lport->wwnn;
1598 fc_host_port_name(lport->host) = lport->wwpn;
1599 fc_host_supported_classes(lport->host) = FC_COS_CLASS3;
1600 memset(fc_host_supported_fc4s(lport->host), 0,
1601 sizeof(fc_host_supported_fc4s(lport->host)));
1602 fc_host_supported_fc4s(lport->host)[2] = 1;
1603 fc_host_supported_fc4s(lport->host)[7] = 1;
1604
1605 /* This value is also unchanging */
1606 memset(fc_host_active_fc4s(lport->host), 0,
1607 sizeof(fc_host_active_fc4s(lport->host)));
1608 fc_host_active_fc4s(lport->host)[2] = 1;
1609 fc_host_active_fc4s(lport->host)[7] = 1;
1610 fc_host_maxframe_size(lport->host) = lport->mfs;
1611 fc_host_supported_speeds(lport->host) = 0;
1612 if (lport->link_supported_speeds & FC_PORTSPEED_1GBIT)
1613 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_1GBIT;
1614 if (lport->link_supported_speeds & FC_PORTSPEED_10GBIT)
1615 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_10GBIT;
1616
1617 return 0;
1618}
1619EXPORT_SYMBOL(fc_lport_init);