blob: f33e5732e3fc2f3c5f74a18c7c08da4abeac14e1 [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
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700136 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800137 * @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,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700143 struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -0800144 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,
Joe Eykholtf211fa52009-08-25 14:01:01 -0700147 rdata->ids.port_id);
Robert Love42e9a922008-12-09 15:10:17 -0800148
Joe Eykholtb5cbf082009-08-25 14:01:44 -0700149 mutex_lock(&lport->lp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800150 switch (event) {
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700151 case RPORT_EV_READY:
Joe Eykholtb5cbf082009-08-25 14:01:44 -0700152 if (lport->state == LPORT_ST_DNS) {
153 lport->dns_rp = rdata;
154 fc_lport_enter_rpn_id(lport);
155 } else {
156 FC_LPORT_DBG(lport, "Received an READY event "
157 "on port (%6x) for the directory "
158 "server, but the lport is not "
159 "in the DNS state, it's in the "
160 "%d state", rdata->ids.port_id,
161 lport->state);
162 lport->tt.rport_logoff(rdata);
163 }
Robert Love42e9a922008-12-09 15:10:17 -0800164 break;
165 case RPORT_EV_LOGO:
166 case RPORT_EV_FAILED:
167 case RPORT_EV_STOP:
Joe Eykholtb5cbf082009-08-25 14:01:44 -0700168 lport->dns_rp = NULL;
Robert Love42e9a922008-12-09 15:10:17 -0800169 break;
170 case RPORT_EV_NONE:
171 break;
172 }
Joe Eykholtb5cbf082009-08-25 14:01:44 -0700173 mutex_unlock(&lport->lp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800174}
175
176/**
Robert Love34f42a02009-02-27 10:55:45 -0800177 * fc_lport_state() - Return a string which represents the lport's state
Robert Love42e9a922008-12-09 15:10:17 -0800178 * @lport: The lport whose state is to converted to a string
179 */
180static const char *fc_lport_state(struct fc_lport *lport)
181{
182 const char *cp;
183
184 cp = fc_lport_state_names[lport->state];
185 if (!cp)
186 cp = "unknown";
187 return cp;
188}
189
190/**
Robert Love34f42a02009-02-27 10:55:45 -0800191 * fc_lport_ptp_setup() - Create an rport for point-to-point mode
Robert Love42e9a922008-12-09 15:10:17 -0800192 * @lport: The lport to attach the ptp rport to
193 * @fid: The FID of the ptp rport
194 * @remote_wwpn: The WWPN of the ptp rport
195 * @remote_wwnn: The WWNN of the ptp rport
196 */
197static void fc_lport_ptp_setup(struct fc_lport *lport,
198 u32 remote_fid, u64 remote_wwpn,
199 u64 remote_wwnn)
200{
Joe Eykholt795d86f2009-08-25 14:00:39 -0700201 struct fc_rport_identifiers ids;
Robert Love42e9a922008-12-09 15:10:17 -0800202
Joe Eykholt795d86f2009-08-25 14:00:39 -0700203 ids.port_id = remote_fid;
204 ids.port_name = remote_wwpn;
205 ids.node_name = remote_wwnn;
206 ids.roles = FC_RPORT_ROLE_UNKNOWN;
Robert Love42e9a922008-12-09 15:10:17 -0800207
Joe Eykholt48f00902009-08-25 14:01:50 -0700208 mutex_lock(&lport->disc.disc_mutex);
209 if (lport->ptp_rp)
Robert Love42e9a922008-12-09 15:10:17 -0800210 lport->tt.rport_logoff(lport->ptp_rp);
Joe Eykholt795d86f2009-08-25 14:00:39 -0700211 lport->ptp_rp = lport->tt.rport_create(lport, &ids);
Joe Eykholt48f00902009-08-25 14:01:50 -0700212 mutex_unlock(&lport->disc.disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800213
214 lport->tt.rport_login(lport->ptp_rp);
215
216 fc_lport_enter_ready(lport);
217}
218
219void fc_get_host_port_type(struct Scsi_Host *shost)
220{
221 /* TODO - currently just NPORT */
222 fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
223}
224EXPORT_SYMBOL(fc_get_host_port_type);
225
226void fc_get_host_port_state(struct Scsi_Host *shost)
227{
228 struct fc_lport *lp = shost_priv(shost);
229
Vasu Devbc0e17f2009-02-27 10:54:57 -0800230 if (lp->link_up)
Robert Love42e9a922008-12-09 15:10:17 -0800231 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
232 else
233 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
234}
235EXPORT_SYMBOL(fc_get_host_port_state);
236
237void fc_get_host_speed(struct Scsi_Host *shost)
238{
239 struct fc_lport *lport = shost_priv(shost);
240
241 fc_host_speed(shost) = lport->link_speed;
242}
243EXPORT_SYMBOL(fc_get_host_speed);
244
245struct fc_host_statistics *fc_get_host_stats(struct Scsi_Host *shost)
246{
Robert Love42e9a922008-12-09 15:10:17 -0800247 struct fc_host_statistics *fcoe_stats;
248 struct fc_lport *lp = shost_priv(shost);
249 struct timespec v0, v1;
Robert Love582b45b2009-03-31 15:51:50 -0700250 unsigned int cpu;
Robert Love42e9a922008-12-09 15:10:17 -0800251
252 fcoe_stats = &lp->host_stats;
253 memset(fcoe_stats, 0, sizeof(struct fc_host_statistics));
254
255 jiffies_to_timespec(jiffies, &v0);
256 jiffies_to_timespec(lp->boot_time, &v1);
257 fcoe_stats->seconds_since_last_reset = (v0.tv_sec - v1.tv_sec);
258
Robert Love582b45b2009-03-31 15:51:50 -0700259 for_each_possible_cpu(cpu) {
260 struct fcoe_dev_stats *stats;
261
262 stats = per_cpu_ptr(lp->dev_stats, cpu);
263
Robert Love42e9a922008-12-09 15:10:17 -0800264 fcoe_stats->tx_frames += stats->TxFrames;
265 fcoe_stats->tx_words += stats->TxWords;
266 fcoe_stats->rx_frames += stats->RxFrames;
267 fcoe_stats->rx_words += stats->RxWords;
268 fcoe_stats->error_frames += stats->ErrorFrames;
269 fcoe_stats->invalid_crc_count += stats->InvalidCRCCount;
270 fcoe_stats->fcp_input_requests += stats->InputRequests;
271 fcoe_stats->fcp_output_requests += stats->OutputRequests;
272 fcoe_stats->fcp_control_requests += stats->ControlRequests;
273 fcoe_stats->fcp_input_megabytes += stats->InputMegabytes;
274 fcoe_stats->fcp_output_megabytes += stats->OutputMegabytes;
275 fcoe_stats->link_failure_count += stats->LinkFailureCount;
276 }
277 fcoe_stats->lip_count = -1;
278 fcoe_stats->nos_count = -1;
279 fcoe_stats->loss_of_sync_count = -1;
280 fcoe_stats->loss_of_signal_count = -1;
281 fcoe_stats->prim_seq_protocol_err_count = -1;
282 fcoe_stats->dumped_frames = -1;
283 return fcoe_stats;
284}
285EXPORT_SYMBOL(fc_get_host_stats);
286
287/*
288 * Fill in FLOGI command for request.
289 */
290static void
291fc_lport_flogi_fill(struct fc_lport *lport, struct fc_els_flogi *flogi,
292 unsigned int op)
293{
294 struct fc_els_csp *sp;
295 struct fc_els_cssp *cp;
296
297 memset(flogi, 0, sizeof(*flogi));
298 flogi->fl_cmd = (u8) op;
299 put_unaligned_be64(lport->wwpn, &flogi->fl_wwpn);
300 put_unaligned_be64(lport->wwnn, &flogi->fl_wwnn);
301 sp = &flogi->fl_csp;
302 sp->sp_hi_ver = 0x20;
303 sp->sp_lo_ver = 0x20;
304 sp->sp_bb_cred = htons(10); /* this gets set by gateway */
305 sp->sp_bb_data = htons((u16) lport->mfs);
306 cp = &flogi->fl_cssp[3 - 1]; /* class 3 parameters */
307 cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
308 if (op != ELS_FLOGI) {
309 sp->sp_features = htons(FC_SP_FT_CIRO);
310 sp->sp_tot_seq = htons(255); /* seq. we accept */
311 sp->sp_rel_off = htons(0x1f);
312 sp->sp_e_d_tov = htonl(lport->e_d_tov);
313
314 cp->cp_rdfs = htons((u16) lport->mfs);
315 cp->cp_con_seq = htons(255);
316 cp->cp_open_seq = 1;
317 }
318}
319
320/*
321 * Add a supported FC-4 type.
322 */
323static void fc_lport_add_fc4_type(struct fc_lport *lport, enum fc_fh_type type)
324{
325 __be32 *mp;
326
327 mp = &lport->fcts.ff_type_map[type / FC_NS_BPW];
328 *mp = htonl(ntohl(*mp) | 1UL << (type % FC_NS_BPW));
329}
330
331/**
Robert Love34f42a02009-02-27 10:55:45 -0800332 * fc_lport_recv_rlir_req() - Handle received Registered Link Incident Report.
Robert Love42e9a922008-12-09 15:10:17 -0800333 * @lport: Fibre Channel local port recieving the RLIR
334 * @sp: current sequence in the RLIR exchange
335 * @fp: RLIR request frame
336 *
337 * Locking Note: The lport lock is exected to be held before calling
338 * this function.
339 */
340static void fc_lport_recv_rlir_req(struct fc_seq *sp, struct fc_frame *fp,
341 struct fc_lport *lport)
342{
Robert Love74147052009-06-10 15:31:10 -0700343 FC_LPORT_DBG(lport, "Received RLIR request while in state %s\n",
344 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800345
346 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
347 fc_frame_free(fp);
348}
349
350/**
Robert Love34f42a02009-02-27 10:55:45 -0800351 * fc_lport_recv_echo_req() - Handle received ECHO request
Robert Love42e9a922008-12-09 15:10:17 -0800352 * @lport: Fibre Channel local port recieving the ECHO
353 * @sp: current sequence in the ECHO exchange
354 * @fp: ECHO request frame
355 *
356 * Locking Note: The lport lock is exected to be held before calling
357 * this function.
358 */
359static void fc_lport_recv_echo_req(struct fc_seq *sp, struct fc_frame *in_fp,
360 struct fc_lport *lport)
361{
362 struct fc_frame *fp;
363 struct fc_exch *ep = fc_seq_exch(sp);
364 unsigned int len;
365 void *pp;
366 void *dp;
367 u32 f_ctl;
368
Robert Love74147052009-06-10 15:31:10 -0700369 FC_LPORT_DBG(lport, "Received RLIR request while in state %s\n",
370 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800371
372 len = fr_len(in_fp) - sizeof(struct fc_frame_header);
373 pp = fc_frame_payload_get(in_fp, len);
374
375 if (len < sizeof(__be32))
376 len = sizeof(__be32);
377
378 fp = fc_frame_alloc(lport, len);
379 if (fp) {
380 dp = fc_frame_payload_get(fp, len);
381 memcpy(dp, pp, len);
382 *((u32 *)dp) = htonl(ELS_LS_ACC << 24);
383 sp = lport->tt.seq_start_next(sp);
384 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
385 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
386 FC_TYPE_ELS, f_ctl, 0);
387 lport->tt.seq_send(lport, sp, fp);
388 }
389 fc_frame_free(in_fp);
390}
391
392/**
Robert Love34f42a02009-02-27 10:55:45 -0800393 * fc_lport_recv_echo_req() - Handle received Request Node ID data request
Robert Love42e9a922008-12-09 15:10:17 -0800394 * @lport: Fibre Channel local port recieving the RNID
395 * @sp: current sequence in the RNID exchange
396 * @fp: RNID request frame
397 *
398 * Locking Note: The lport lock is exected to be held before calling
399 * this function.
400 */
401static void fc_lport_recv_rnid_req(struct fc_seq *sp, struct fc_frame *in_fp,
402 struct fc_lport *lport)
403{
404 struct fc_frame *fp;
405 struct fc_exch *ep = fc_seq_exch(sp);
406 struct fc_els_rnid *req;
407 struct {
408 struct fc_els_rnid_resp rnid;
409 struct fc_els_rnid_cid cid;
410 struct fc_els_rnid_gen gen;
411 } *rp;
412 struct fc_seq_els_data rjt_data;
413 u8 fmt;
414 size_t len;
415 u32 f_ctl;
416
Robert Love74147052009-06-10 15:31:10 -0700417 FC_LPORT_DBG(lport, "Received RNID request while in state %s\n",
418 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800419
420 req = fc_frame_payload_get(in_fp, sizeof(*req));
421 if (!req) {
422 rjt_data.fp = NULL;
423 rjt_data.reason = ELS_RJT_LOGIC;
424 rjt_data.explan = ELS_EXPL_NONE;
425 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
426 } else {
427 fmt = req->rnid_fmt;
428 len = sizeof(*rp);
429 if (fmt != ELS_RNIDF_GEN ||
430 ntohl(lport->rnid_gen.rnid_atype) == 0) {
431 fmt = ELS_RNIDF_NONE; /* nothing to provide */
432 len -= sizeof(rp->gen);
433 }
434 fp = fc_frame_alloc(lport, len);
435 if (fp) {
436 rp = fc_frame_payload_get(fp, len);
437 memset(rp, 0, len);
438 rp->rnid.rnid_cmd = ELS_LS_ACC;
439 rp->rnid.rnid_fmt = fmt;
440 rp->rnid.rnid_cid_len = sizeof(rp->cid);
441 rp->cid.rnid_wwpn = htonll(lport->wwpn);
442 rp->cid.rnid_wwnn = htonll(lport->wwnn);
443 if (fmt == ELS_RNIDF_GEN) {
444 rp->rnid.rnid_sid_len = sizeof(rp->gen);
445 memcpy(&rp->gen, &lport->rnid_gen,
446 sizeof(rp->gen));
447 }
448 sp = lport->tt.seq_start_next(sp);
449 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
450 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
451 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
452 FC_TYPE_ELS, f_ctl, 0);
453 lport->tt.seq_send(lport, sp, fp);
454 }
455 }
456 fc_frame_free(in_fp);
457}
458
459/**
Robert Love34f42a02009-02-27 10:55:45 -0800460 * fc_lport_recv_adisc_req() - Handle received Address Discovery Request
Robert Love42e9a922008-12-09 15:10:17 -0800461 * @lport: Fibre Channel local port recieving the ADISC
462 * @sp: current sequence in the ADISC exchange
463 * @fp: ADISC request frame
464 *
465 * Locking Note: The lport lock is expected to be held before calling
466 * this function.
467 */
468static void fc_lport_recv_adisc_req(struct fc_seq *sp, struct fc_frame *in_fp,
469 struct fc_lport *lport)
470{
471 struct fc_frame *fp;
472 struct fc_exch *ep = fc_seq_exch(sp);
473 struct fc_els_adisc *req, *rp;
474 struct fc_seq_els_data rjt_data;
475 size_t len;
476 u32 f_ctl;
477
Robert Love74147052009-06-10 15:31:10 -0700478 FC_LPORT_DBG(lport, "Received ADISC request while in state %s\n",
479 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800480
481 req = fc_frame_payload_get(in_fp, sizeof(*req));
482 if (!req) {
483 rjt_data.fp = NULL;
484 rjt_data.reason = ELS_RJT_LOGIC;
485 rjt_data.explan = ELS_EXPL_NONE;
486 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
487 } else {
488 len = sizeof(*rp);
489 fp = fc_frame_alloc(lport, len);
490 if (fp) {
491 rp = fc_frame_payload_get(fp, len);
492 memset(rp, 0, len);
493 rp->adisc_cmd = ELS_LS_ACC;
494 rp->adisc_wwpn = htonll(lport->wwpn);
495 rp->adisc_wwnn = htonll(lport->wwnn);
496 hton24(rp->adisc_port_id,
497 fc_host_port_id(lport->host));
498 sp = lport->tt.seq_start_next(sp);
499 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
500 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
501 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
502 FC_TYPE_ELS, f_ctl, 0);
503 lport->tt.seq_send(lport, sp, fp);
504 }
505 }
506 fc_frame_free(in_fp);
507}
508
509/**
Robert Love34f42a02009-02-27 10:55:45 -0800510 * fc_lport_recv_logo_req() - Handle received fabric LOGO request
Robert Love42e9a922008-12-09 15:10:17 -0800511 * @lport: Fibre Channel local port recieving the LOGO
512 * @sp: current sequence in the LOGO exchange
513 * @fp: LOGO request frame
514 *
515 * Locking Note: The lport lock is exected to be held before calling
516 * this function.
517 */
518static void fc_lport_recv_logo_req(struct fc_seq *sp, struct fc_frame *fp,
519 struct fc_lport *lport)
520{
521 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
522 fc_lport_enter_reset(lport);
523 fc_frame_free(fp);
524}
525
526/**
Robert Love34f42a02009-02-27 10:55:45 -0800527 * fc_fabric_login() - Start the lport state machine
Robert Love42e9a922008-12-09 15:10:17 -0800528 * @lport: The lport that should log into the fabric
529 *
530 * Locking Note: This function should not be called
531 * with the lport lock held.
532 */
533int fc_fabric_login(struct fc_lport *lport)
534{
535 int rc = -1;
536
537 mutex_lock(&lport->lp_mutex);
Joe Eykholtb1d9fd52009-07-29 17:04:22 -0700538 if (lport->state == LPORT_ST_DISABLED) {
Robert Love42e9a922008-12-09 15:10:17 -0800539 fc_lport_enter_reset(lport);
540 rc = 0;
541 }
542 mutex_unlock(&lport->lp_mutex);
543
544 return rc;
545}
546EXPORT_SYMBOL(fc_fabric_login);
547
548/**
Robert Love34f42a02009-02-27 10:55:45 -0800549 * fc_linkup() - Handler for transport linkup events
Robert Love42e9a922008-12-09 15:10:17 -0800550 * @lport: The lport whose link is up
551 */
552void fc_linkup(struct fc_lport *lport)
553{
Robert Love74147052009-06-10 15:31:10 -0700554 printk(KERN_INFO "libfc: Link up on port (%6x)\n",
555 fc_host_port_id(lport->host));
Robert Love42e9a922008-12-09 15:10:17 -0800556
557 mutex_lock(&lport->lp_mutex);
Vasu Devbc0e17f2009-02-27 10:54:57 -0800558 if (!lport->link_up) {
559 lport->link_up = 1;
Robert Love42e9a922008-12-09 15:10:17 -0800560
561 if (lport->state == LPORT_ST_RESET)
562 fc_lport_enter_flogi(lport);
563 }
564 mutex_unlock(&lport->lp_mutex);
565}
566EXPORT_SYMBOL(fc_linkup);
567
568/**
Robert Love34f42a02009-02-27 10:55:45 -0800569 * fc_linkdown() - Handler for transport linkdown events
Robert Love42e9a922008-12-09 15:10:17 -0800570 * @lport: The lport whose link is down
571 */
572void fc_linkdown(struct fc_lport *lport)
573{
574 mutex_lock(&lport->lp_mutex);
Robert Love74147052009-06-10 15:31:10 -0700575 printk(KERN_INFO "libfc: Link down on port (%6x)\n",
576 fc_host_port_id(lport->host));
Robert Love42e9a922008-12-09 15:10:17 -0800577
Vasu Devbc0e17f2009-02-27 10:54:57 -0800578 if (lport->link_up) {
579 lport->link_up = 0;
Robert Love42e9a922008-12-09 15:10:17 -0800580 fc_lport_enter_reset(lport);
581 lport->tt.fcp_cleanup(lport);
582 }
583 mutex_unlock(&lport->lp_mutex);
584}
585EXPORT_SYMBOL(fc_linkdown);
586
587/**
Robert Love34f42a02009-02-27 10:55:45 -0800588 * fc_fabric_logoff() - Logout of the fabric
Robert Love42e9a922008-12-09 15:10:17 -0800589 * @lport: fc_lport pointer to logoff the fabric
590 *
591 * Return value:
592 * 0 for success, -1 for failure
Robert Love34f42a02009-02-27 10:55:45 -0800593 */
Robert Love42e9a922008-12-09 15:10:17 -0800594int fc_fabric_logoff(struct fc_lport *lport)
595{
596 lport->tt.disc_stop_final(lport);
597 mutex_lock(&lport->lp_mutex);
Abhijeet Joglekara0fd2e42009-04-21 16:27:09 -0700598 if (lport->dns_rp)
599 lport->tt.rport_logoff(lport->dns_rp);
600 mutex_unlock(&lport->lp_mutex);
601 lport->tt.rport_flush_queue();
602 mutex_lock(&lport->lp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800603 fc_lport_enter_logo(lport);
604 mutex_unlock(&lport->lp_mutex);
Steve Maf7db2c12009-02-27 10:55:13 -0800605 cancel_delayed_work_sync(&lport->retry_work);
Robert Love42e9a922008-12-09 15:10:17 -0800606 return 0;
607}
608EXPORT_SYMBOL(fc_fabric_logoff);
609
610/**
Robert Love34f42a02009-02-27 10:55:45 -0800611 * fc_lport_destroy() - unregister a fc_lport
Robert Love42e9a922008-12-09 15:10:17 -0800612 * @lport: fc_lport pointer to unregister
613 *
614 * Return value:
615 * None
616 * Note:
617 * exit routine for fc_lport instance
618 * clean-up all the allocated memory
619 * and free up other system resources.
620 *
Robert Love34f42a02009-02-27 10:55:45 -0800621 */
Robert Love42e9a922008-12-09 15:10:17 -0800622int fc_lport_destroy(struct fc_lport *lport)
623{
Abhijeet Joglekarbbf15662009-04-21 16:27:14 -0700624 mutex_lock(&lport->lp_mutex);
Joe Eykholtb1d9fd52009-07-29 17:04:22 -0700625 lport->state = LPORT_ST_DISABLED;
Abhijeet Joglekarbbf15662009-04-21 16:27:14 -0700626 lport->link_up = 0;
Robert Love42e9a922008-12-09 15:10:17 -0800627 lport->tt.frame_send = fc_frame_drop;
Abhijeet Joglekarbbf15662009-04-21 16:27:14 -0700628 mutex_unlock(&lport->lp_mutex);
629
Robert Love42e9a922008-12-09 15:10:17 -0800630 lport->tt.fcp_abort_io(lport);
Joe Eykholte9ba8b42009-07-29 17:04:33 -0700631 lport->tt.disc_stop_final(lport);
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -0800632 lport->tt.exch_mgr_reset(lport, 0, 0);
Robert Love42e9a922008-12-09 15:10:17 -0800633 return 0;
634}
635EXPORT_SYMBOL(fc_lport_destroy);
636
637/**
Robert Love34f42a02009-02-27 10:55:45 -0800638 * fc_set_mfs() - sets up the mfs for the corresponding fc_lport
Robert Love42e9a922008-12-09 15:10:17 -0800639 * @lport: fc_lport pointer to unregister
640 * @mfs: the new mfs for fc_lport
641 *
642 * Set mfs for the given fc_lport to the new mfs.
643 *
644 * Return: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800645 */
Robert Love42e9a922008-12-09 15:10:17 -0800646int fc_set_mfs(struct fc_lport *lport, u32 mfs)
647{
648 unsigned int old_mfs;
649 int rc = -EINVAL;
650
651 mutex_lock(&lport->lp_mutex);
652
653 old_mfs = lport->mfs;
654
655 if (mfs >= FC_MIN_MAX_FRAME) {
656 mfs &= ~3;
657 if (mfs > FC_MAX_FRAME)
658 mfs = FC_MAX_FRAME;
659 mfs -= sizeof(struct fc_frame_header);
660 lport->mfs = mfs;
661 rc = 0;
662 }
663
664 if (!rc && mfs < old_mfs)
665 fc_lport_enter_reset(lport);
666
667 mutex_unlock(&lport->lp_mutex);
668
669 return rc;
670}
671EXPORT_SYMBOL(fc_set_mfs);
672
673/**
Robert Love34f42a02009-02-27 10:55:45 -0800674 * fc_lport_disc_callback() - Callback for discovery events
Robert Love42e9a922008-12-09 15:10:17 -0800675 * @lport: FC local port
676 * @event: The discovery event
677 */
678void fc_lport_disc_callback(struct fc_lport *lport, enum fc_disc_event event)
679{
680 switch (event) {
681 case DISC_EV_SUCCESS:
Robert Love74147052009-06-10 15:31:10 -0700682 FC_LPORT_DBG(lport, "Discovery succeeded\n");
Robert Love42e9a922008-12-09 15:10:17 -0800683 break;
684 case DISC_EV_FAILED:
Robert Love74147052009-06-10 15:31:10 -0700685 printk(KERN_ERR "libfc: Discovery failed for port (%6x)\n",
686 fc_host_port_id(lport->host));
Robert Love42e9a922008-12-09 15:10:17 -0800687 mutex_lock(&lport->lp_mutex);
688 fc_lport_enter_reset(lport);
689 mutex_unlock(&lport->lp_mutex);
690 break;
691 case DISC_EV_NONE:
692 WARN_ON(1);
693 break;
694 }
695}
696
697/**
Robert Love34f42a02009-02-27 10:55:45 -0800698 * fc_rport_enter_ready() - Enter the ready state and start discovery
Robert Love42e9a922008-12-09 15:10:17 -0800699 * @lport: Fibre Channel local port that is ready
700 *
701 * Locking Note: The lport lock is expected to be held before calling
702 * this routine.
703 */
704static void fc_lport_enter_ready(struct fc_lport *lport)
705{
Robert Love74147052009-06-10 15:31:10 -0700706 FC_LPORT_DBG(lport, "Entered READY from state %s\n",
707 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800708
709 fc_lport_state_enter(lport, LPORT_ST_READY);
710
711 lport->tt.disc_start(fc_lport_disc_callback, lport);
712}
713
714/**
Robert Love34f42a02009-02-27 10:55:45 -0800715 * fc_lport_recv_flogi_req() - Receive a FLOGI request
Robert Love42e9a922008-12-09 15:10:17 -0800716 * @sp_in: The sequence the FLOGI is on
717 * @rx_fp: The frame the FLOGI is in
718 * @lport: The lport that recieved the request
719 *
720 * A received FLOGI request indicates a point-to-point connection.
721 * Accept it with the common service parameters indicating our N port.
722 * Set up to do a PLOGI if we have the higher-number WWPN.
723 *
724 * Locking Note: The lport lock is exected to be held before calling
725 * this function.
726 */
727static void fc_lport_recv_flogi_req(struct fc_seq *sp_in,
728 struct fc_frame *rx_fp,
729 struct fc_lport *lport)
730{
731 struct fc_frame *fp;
732 struct fc_frame_header *fh;
733 struct fc_seq *sp;
734 struct fc_exch *ep;
735 struct fc_els_flogi *flp;
736 struct fc_els_flogi *new_flp;
737 u64 remote_wwpn;
738 u32 remote_fid;
739 u32 local_fid;
740 u32 f_ctl;
741
Robert Love74147052009-06-10 15:31:10 -0700742 FC_LPORT_DBG(lport, "Received FLOGI request while in state %s\n",
743 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800744
745 fh = fc_frame_header_get(rx_fp);
746 remote_fid = ntoh24(fh->fh_s_id);
747 flp = fc_frame_payload_get(rx_fp, sizeof(*flp));
748 if (!flp)
749 goto out;
750 remote_wwpn = get_unaligned_be64(&flp->fl_wwpn);
751 if (remote_wwpn == lport->wwpn) {
Robert Love74147052009-06-10 15:31:10 -0700752 printk(KERN_WARNING "libfc: Received FLOGI from port "
753 "with same WWPN %llx\n", remote_wwpn);
Robert Love42e9a922008-12-09 15:10:17 -0800754 goto out;
755 }
Robert Love74147052009-06-10 15:31:10 -0700756 FC_LPORT_DBG(lport, "FLOGI from port WWPN %llx\n", remote_wwpn);
Robert Love42e9a922008-12-09 15:10:17 -0800757
758 /*
759 * XXX what is the right thing to do for FIDs?
760 * The originator might expect our S_ID to be 0xfffffe.
761 * But if so, both of us could end up with the same FID.
762 */
763 local_fid = FC_LOCAL_PTP_FID_LO;
764 if (remote_wwpn < lport->wwpn) {
765 local_fid = FC_LOCAL_PTP_FID_HI;
766 if (!remote_fid || remote_fid == local_fid)
767 remote_fid = FC_LOCAL_PTP_FID_LO;
768 } else if (!remote_fid) {
769 remote_fid = FC_LOCAL_PTP_FID_HI;
770 }
771
772 fc_host_port_id(lport->host) = local_fid;
773
774 fp = fc_frame_alloc(lport, sizeof(*flp));
775 if (fp) {
776 sp = lport->tt.seq_start_next(fr_seq(rx_fp));
777 new_flp = fc_frame_payload_get(fp, sizeof(*flp));
778 fc_lport_flogi_fill(lport, new_flp, ELS_FLOGI);
779 new_flp->fl_cmd = (u8) ELS_LS_ACC;
780
781 /*
782 * Send the response. If this fails, the originator should
783 * repeat the sequence.
784 */
785 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
786 ep = fc_seq_exch(sp);
787 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
788 FC_TYPE_ELS, f_ctl, 0);
789 lport->tt.seq_send(lport, sp, fp);
790
791 } else {
792 fc_lport_error(lport, fp);
793 }
794 fc_lport_ptp_setup(lport, remote_fid, remote_wwpn,
795 get_unaligned_be64(&flp->fl_wwnn));
796
797 lport->tt.disc_start(fc_lport_disc_callback, lport);
798
799out:
800 sp = fr_seq(rx_fp);
801 fc_frame_free(rx_fp);
802}
803
804/**
Robert Love34f42a02009-02-27 10:55:45 -0800805 * fc_lport_recv_req() - The generic lport request handler
Robert Love42e9a922008-12-09 15:10:17 -0800806 * @lport: The lport that received the request
807 * @sp: The sequence the request is on
808 * @fp: The frame the request is in
809 *
810 * This function will see if the lport handles the request or
811 * if an rport should handle the request.
812 *
813 * Locking Note: This function should not be called with the lport
814 * lock held becuase it will grab the lock.
815 */
816static void fc_lport_recv_req(struct fc_lport *lport, struct fc_seq *sp,
817 struct fc_frame *fp)
818{
819 struct fc_frame_header *fh = fc_frame_header_get(fp);
820 void (*recv) (struct fc_seq *, struct fc_frame *, struct fc_lport *);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700821 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800822 u32 s_id;
823 u32 d_id;
824 struct fc_seq_els_data rjt_data;
825
826 mutex_lock(&lport->lp_mutex);
827
828 /*
829 * Handle special ELS cases like FLOGI, LOGO, and
830 * RSCN here. These don't require a session.
831 * Even if we had a session, it might not be ready.
832 */
Joe Eykholte9ba8b42009-07-29 17:04:33 -0700833 if (!lport->link_up)
834 fc_frame_free(fp);
835 else if (fh->fh_type == FC_TYPE_ELS &&
836 fh->fh_r_ctl == FC_RCTL_ELS_REQ) {
Robert Love42e9a922008-12-09 15:10:17 -0800837 /*
838 * Check opcode.
839 */
840 recv = NULL;
841 switch (fc_frame_payload_op(fp)) {
842 case ELS_FLOGI:
843 recv = fc_lport_recv_flogi_req;
844 break;
845 case ELS_LOGO:
846 fh = fc_frame_header_get(fp);
847 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI)
848 recv = fc_lport_recv_logo_req;
849 break;
850 case ELS_RSCN:
851 recv = lport->tt.disc_recv_req;
852 break;
853 case ELS_ECHO:
854 recv = fc_lport_recv_echo_req;
855 break;
856 case ELS_RLIR:
857 recv = fc_lport_recv_rlir_req;
858 break;
859 case ELS_RNID:
860 recv = fc_lport_recv_rnid_req;
861 break;
862 case ELS_ADISC:
863 recv = fc_lport_recv_adisc_req;
864 break;
865 }
866
867 if (recv)
868 recv(sp, fp, lport);
869 else {
870 /*
871 * Find session.
872 * If this is a new incoming PLOGI, we won't find it.
873 */
874 s_id = ntoh24(fh->fh_s_id);
875 d_id = ntoh24(fh->fh_d_id);
876
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700877 rdata = lport->tt.rport_lookup(lport, s_id);
878 if (rdata)
879 lport->tt.rport_recv_req(sp, fp, rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800880 else {
881 rjt_data.fp = NULL;
882 rjt_data.reason = ELS_RJT_UNAB;
883 rjt_data.explan = ELS_EXPL_NONE;
884 lport->tt.seq_els_rsp_send(sp,
885 ELS_LS_RJT,
886 &rjt_data);
887 fc_frame_free(fp);
888 }
889 }
890 } else {
Robert Love74147052009-06-10 15:31:10 -0700891 FC_LPORT_DBG(lport, "dropping invalid frame (eof %x)\n",
892 fr_eof(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800893 fc_frame_free(fp);
894 }
895 mutex_unlock(&lport->lp_mutex);
896
897 /*
898 * The common exch_done for all request may not be good
899 * if any request requires longer hold on exhange. XXX
900 */
901 lport->tt.exch_done(sp);
902}
903
904/**
Robert Love34f42a02009-02-27 10:55:45 -0800905 * fc_lport_reset() - Reset an lport
Robert Love42e9a922008-12-09 15:10:17 -0800906 * @lport: The lport which should be reset
907 *
908 * Locking Note: This functions should not be called with the
909 * lport lock held.
910 */
911int fc_lport_reset(struct fc_lport *lport)
912{
Steve Maf7db2c12009-02-27 10:55:13 -0800913 cancel_delayed_work_sync(&lport->retry_work);
Robert Love42e9a922008-12-09 15:10:17 -0800914 mutex_lock(&lport->lp_mutex);
915 fc_lport_enter_reset(lport);
916 mutex_unlock(&lport->lp_mutex);
917 return 0;
918}
919EXPORT_SYMBOL(fc_lport_reset);
920
921/**
Joe Eykholt1190d922009-07-29 17:04:27 -0700922 * fc_lport_reset_locked() - Reset the local port
Robert Love42e9a922008-12-09 15:10:17 -0800923 * @lport: Fibre Channel local port to be reset
924 *
925 * Locking Note: The lport lock is expected to be held before calling
926 * this routine.
927 */
Joe Eykholt1190d922009-07-29 17:04:27 -0700928static void fc_lport_reset_locked(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -0800929{
Robert Love42e9a922008-12-09 15:10:17 -0800930 if (lport->dns_rp)
931 lport->tt.rport_logoff(lport->dns_rp);
932
Joe Eykholt48f00902009-08-25 14:01:50 -0700933 lport->ptp_rp = NULL;
Robert Love42e9a922008-12-09 15:10:17 -0800934
935 lport->tt.disc_stop(lport);
936
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -0800937 lport->tt.exch_mgr_reset(lport, 0, 0);
Robert Love42e9a922008-12-09 15:10:17 -0800938 fc_host_fabric_name(lport->host) = 0;
939 fc_host_port_id(lport->host) = 0;
Joe Eykholt1190d922009-07-29 17:04:27 -0700940}
Robert Love42e9a922008-12-09 15:10:17 -0800941
Joe Eykholt1190d922009-07-29 17:04:27 -0700942/**
943 * fc_lport_enter_reset() - Reset the local port
944 * @lport: Fibre Channel local port to be reset
945 *
946 * Locking Note: The lport lock is expected to be held before calling
947 * this routine.
948 */
949static void fc_lport_enter_reset(struct fc_lport *lport)
950{
951 FC_LPORT_DBG(lport, "Entered RESET state from %s state\n",
952 fc_lport_state(lport));
953
954 fc_lport_state_enter(lport, LPORT_ST_RESET);
955 fc_lport_reset_locked(lport);
Vasu Devbc0e17f2009-02-27 10:54:57 -0800956 if (lport->link_up)
Robert Love42e9a922008-12-09 15:10:17 -0800957 fc_lport_enter_flogi(lport);
958}
959
960/**
Joe Eykholt1190d922009-07-29 17:04:27 -0700961 * fc_lport_enter_disabled() - disable the local port
962 * @lport: Fibre Channel local port to be reset
963 *
964 * Locking Note: The lport lock is expected to be held before calling
965 * this routine.
966 */
967static void fc_lport_enter_disabled(struct fc_lport *lport)
968{
969 FC_LPORT_DBG(lport, "Entered disabled state from %s state\n",
970 fc_lport_state(lport));
971
972 fc_lport_state_enter(lport, LPORT_ST_DISABLED);
973 fc_lport_reset_locked(lport);
974}
975
976/**
Robert Love34f42a02009-02-27 10:55:45 -0800977 * fc_lport_error() - Handler for any errors
Robert Love42e9a922008-12-09 15:10:17 -0800978 * @lport: The fc_lport object
979 * @fp: The frame pointer
980 *
981 * If the error was caused by a resource allocation failure
982 * then wait for half a second and retry, otherwise retry
983 * after the e_d_tov time.
984 */
985static void fc_lport_error(struct fc_lport *lport, struct fc_frame *fp)
986{
987 unsigned long delay = 0;
Robert Love74147052009-06-10 15:31:10 -0700988 FC_LPORT_DBG(lport, "Error %ld in state %s, retries %d\n",
989 PTR_ERR(fp), fc_lport_state(lport),
990 lport->retry_count);
Robert Love42e9a922008-12-09 15:10:17 -0800991
992 if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
993 /*
994 * Memory allocation failure, or the exchange timed out.
995 * Retry after delay
996 */
997 if (lport->retry_count < lport->max_retry_count) {
998 lport->retry_count++;
999 if (!fp)
1000 delay = msecs_to_jiffies(500);
1001 else
1002 delay = msecs_to_jiffies(lport->e_d_tov);
1003
1004 schedule_delayed_work(&lport->retry_work, delay);
1005 } else {
1006 switch (lport->state) {
Joe Eykholtb1d9fd52009-07-29 17:04:22 -07001007 case LPORT_ST_DISABLED:
Robert Love42e9a922008-12-09 15:10:17 -08001008 case LPORT_ST_READY:
1009 case LPORT_ST_RESET:
1010 case LPORT_ST_RPN_ID:
1011 case LPORT_ST_RFT_ID:
1012 case LPORT_ST_SCR:
1013 case LPORT_ST_DNS:
1014 case LPORT_ST_FLOGI:
1015 case LPORT_ST_LOGO:
1016 fc_lport_enter_reset(lport);
1017 break;
1018 }
1019 }
1020 }
1021}
1022
1023/**
Robert Love34f42a02009-02-27 10:55:45 -08001024 * fc_lport_rft_id_resp() - Handle response to Register Fibre
1025 * Channel Types by ID (RPN_ID) request
Robert Love42e9a922008-12-09 15:10:17 -08001026 * @sp: current sequence in RPN_ID exchange
1027 * @fp: response frame
1028 * @lp_arg: Fibre Channel host port instance
1029 *
1030 * Locking Note: This function will be called without the lport lock
1031 * held, but it will lock, call an _enter_* function or fc_lport_error
1032 * and then unlock the lport.
1033 */
1034static void fc_lport_rft_id_resp(struct fc_seq *sp, struct fc_frame *fp,
1035 void *lp_arg)
1036{
1037 struct fc_lport *lport = lp_arg;
1038 struct fc_frame_header *fh;
1039 struct fc_ct_hdr *ct;
1040
1041 if (fp == ERR_PTR(-FC_EX_CLOSED))
1042 return;
1043
1044 mutex_lock(&lport->lp_mutex);
1045
Robert Love74147052009-06-10 15:31:10 -07001046 FC_LPORT_DBG(lport, "Received a RFT_ID response\n");
Robert Love42e9a922008-12-09 15:10:17 -08001047
1048 if (lport->state != LPORT_ST_RFT_ID) {
Robert Love74147052009-06-10 15:31:10 -07001049 FC_LPORT_DBG(lport, "Received a RFT_ID response, but in state "
1050 "%s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001051 if (IS_ERR(fp))
1052 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001053 goto out;
1054 }
1055
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001056 if (IS_ERR(fp)) {
1057 fc_lport_error(lport, fp);
1058 goto err;
1059 }
1060
Robert Love42e9a922008-12-09 15:10:17 -08001061 fh = fc_frame_header_get(fp);
1062 ct = fc_frame_payload_get(fp, sizeof(*ct));
1063
1064 if (fh && ct && fh->fh_type == FC_TYPE_CT &&
1065 ct->ct_fs_type == FC_FST_DIR &&
1066 ct->ct_fs_subtype == FC_NS_SUBTYPE &&
1067 ntohs(ct->ct_cmd) == FC_FS_ACC)
1068 fc_lport_enter_scr(lport);
1069 else
1070 fc_lport_error(lport, fp);
1071out:
1072 fc_frame_free(fp);
1073err:
1074 mutex_unlock(&lport->lp_mutex);
1075}
1076
1077/**
Robert Love34f42a02009-02-27 10:55:45 -08001078 * fc_lport_rpn_id_resp() - Handle response to Register Port
1079 * Name by ID (RPN_ID) request
Robert Love42e9a922008-12-09 15:10:17 -08001080 * @sp: current sequence in RPN_ID exchange
1081 * @fp: response frame
1082 * @lp_arg: Fibre Channel host port instance
1083 *
1084 * Locking Note: This function will be called without the lport lock
1085 * held, but it will lock, call an _enter_* function or fc_lport_error
1086 * and then unlock the lport.
1087 */
1088static void fc_lport_rpn_id_resp(struct fc_seq *sp, struct fc_frame *fp,
1089 void *lp_arg)
1090{
1091 struct fc_lport *lport = lp_arg;
1092 struct fc_frame_header *fh;
1093 struct fc_ct_hdr *ct;
1094
1095 if (fp == ERR_PTR(-FC_EX_CLOSED))
1096 return;
1097
1098 mutex_lock(&lport->lp_mutex);
1099
Robert Love74147052009-06-10 15:31:10 -07001100 FC_LPORT_DBG(lport, "Received a RPN_ID response\n");
Robert Love42e9a922008-12-09 15:10:17 -08001101
1102 if (lport->state != LPORT_ST_RPN_ID) {
Robert Love74147052009-06-10 15:31:10 -07001103 FC_LPORT_DBG(lport, "Received a RPN_ID response, but in state "
1104 "%s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001105 if (IS_ERR(fp))
1106 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001107 goto out;
1108 }
1109
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001110 if (IS_ERR(fp)) {
1111 fc_lport_error(lport, fp);
1112 goto err;
1113 }
1114
Robert Love42e9a922008-12-09 15:10:17 -08001115 fh = fc_frame_header_get(fp);
1116 ct = fc_frame_payload_get(fp, sizeof(*ct));
1117 if (fh && ct && fh->fh_type == FC_TYPE_CT &&
1118 ct->ct_fs_type == FC_FST_DIR &&
1119 ct->ct_fs_subtype == FC_NS_SUBTYPE &&
1120 ntohs(ct->ct_cmd) == FC_FS_ACC)
1121 fc_lport_enter_rft_id(lport);
1122 else
1123 fc_lport_error(lport, fp);
1124
1125out:
1126 fc_frame_free(fp);
1127err:
1128 mutex_unlock(&lport->lp_mutex);
1129}
1130
1131/**
Robert Love34f42a02009-02-27 10:55:45 -08001132 * fc_lport_scr_resp() - Handle response to State Change Register (SCR) request
Robert Love42e9a922008-12-09 15:10:17 -08001133 * @sp: current sequence in SCR exchange
1134 * @fp: response frame
1135 * @lp_arg: Fibre Channel lport port instance that sent the registration request
1136 *
1137 * Locking Note: This function will be called without the lport lock
1138 * held, but it will lock, call an _enter_* function or fc_lport_error
1139 * and then unlock the lport.
1140 */
1141static void fc_lport_scr_resp(struct fc_seq *sp, struct fc_frame *fp,
1142 void *lp_arg)
1143{
1144 struct fc_lport *lport = lp_arg;
1145 u8 op;
1146
1147 if (fp == ERR_PTR(-FC_EX_CLOSED))
1148 return;
1149
1150 mutex_lock(&lport->lp_mutex);
1151
Robert Love74147052009-06-10 15:31:10 -07001152 FC_LPORT_DBG(lport, "Received a SCR response\n");
Robert Love42e9a922008-12-09 15:10:17 -08001153
1154 if (lport->state != LPORT_ST_SCR) {
Robert Love74147052009-06-10 15:31:10 -07001155 FC_LPORT_DBG(lport, "Received a SCR response, but in state "
1156 "%s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001157 if (IS_ERR(fp))
1158 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001159 goto out;
1160 }
1161
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001162 if (IS_ERR(fp)) {
1163 fc_lport_error(lport, fp);
1164 goto err;
1165 }
1166
Robert Love42e9a922008-12-09 15:10:17 -08001167 op = fc_frame_payload_op(fp);
1168 if (op == ELS_LS_ACC)
1169 fc_lport_enter_ready(lport);
1170 else
1171 fc_lport_error(lport, fp);
1172
1173out:
1174 fc_frame_free(fp);
1175err:
1176 mutex_unlock(&lport->lp_mutex);
1177}
1178
1179/**
Robert Love34f42a02009-02-27 10:55:45 -08001180 * fc_lport_enter_scr() - Send a State Change Register (SCR) request
Robert Love42e9a922008-12-09 15:10:17 -08001181 * @lport: Fibre Channel local port to register for state changes
1182 *
1183 * Locking Note: The lport lock is expected to be held before calling
1184 * this routine.
1185 */
1186static void fc_lport_enter_scr(struct fc_lport *lport)
1187{
1188 struct fc_frame *fp;
1189
Robert Love74147052009-06-10 15:31:10 -07001190 FC_LPORT_DBG(lport, "Entered SCR state from %s state\n",
1191 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001192
1193 fc_lport_state_enter(lport, LPORT_ST_SCR);
1194
1195 fp = fc_frame_alloc(lport, sizeof(struct fc_els_scr));
1196 if (!fp) {
1197 fc_lport_error(lport, fp);
1198 return;
1199 }
1200
Joe Eykholta46f3272009-08-25 14:00:55 -07001201 if (!lport->tt.elsct_send(lport, FC_FID_FCTRL, fp, ELS_SCR,
Robert Love42e9a922008-12-09 15:10:17 -08001202 fc_lport_scr_resp, lport, lport->e_d_tov))
1203 fc_lport_error(lport, fp);
1204}
1205
1206/**
Robert Love34f42a02009-02-27 10:55:45 -08001207 * fc_lport_enter_rft_id() - Register FC4-types with the name server
Robert Love42e9a922008-12-09 15:10:17 -08001208 * @lport: Fibre Channel local port to register
1209 *
1210 * Locking Note: The lport lock is expected to be held before calling
1211 * this routine.
1212 */
1213static void fc_lport_enter_rft_id(struct fc_lport *lport)
1214{
1215 struct fc_frame *fp;
1216 struct fc_ns_fts *lps;
1217 int i;
1218
Robert Love74147052009-06-10 15:31:10 -07001219 FC_LPORT_DBG(lport, "Entered RFT_ID state from %s state\n",
1220 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001221
1222 fc_lport_state_enter(lport, LPORT_ST_RFT_ID);
1223
1224 lps = &lport->fcts;
1225 i = sizeof(lps->ff_type_map) / sizeof(lps->ff_type_map[0]);
1226 while (--i >= 0)
1227 if (ntohl(lps->ff_type_map[i]) != 0)
1228 break;
1229 if (i < 0) {
1230 /* nothing to register, move on to SCR */
1231 fc_lport_enter_scr(lport);
1232 return;
1233 }
1234
1235 fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
1236 sizeof(struct fc_ns_rft));
1237 if (!fp) {
1238 fc_lport_error(lport, fp);
1239 return;
1240 }
1241
Joe Eykholta46f3272009-08-25 14:00:55 -07001242 if (!lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RFT_ID,
Robert Love42e9a922008-12-09 15:10:17 -08001243 fc_lport_rft_id_resp,
1244 lport, lport->e_d_tov))
1245 fc_lport_error(lport, fp);
1246}
1247
1248/**
Robert Love34f42a02009-02-27 10:55:45 -08001249 * fc_rport_enter_rft_id() - Register port name with the name server
Robert Love42e9a922008-12-09 15:10:17 -08001250 * @lport: Fibre Channel local port to register
1251 *
1252 * Locking Note: The lport lock is expected to be held before calling
1253 * this routine.
1254 */
1255static void fc_lport_enter_rpn_id(struct fc_lport *lport)
1256{
1257 struct fc_frame *fp;
1258
Robert Love74147052009-06-10 15:31:10 -07001259 FC_LPORT_DBG(lport, "Entered RPN_ID state from %s state\n",
1260 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001261
1262 fc_lport_state_enter(lport, LPORT_ST_RPN_ID);
1263
1264 fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
1265 sizeof(struct fc_ns_rn_id));
1266 if (!fp) {
1267 fc_lport_error(lport, fp);
1268 return;
1269 }
1270
Joe Eykholta46f3272009-08-25 14:00:55 -07001271 if (!lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RPN_ID,
Robert Love42e9a922008-12-09 15:10:17 -08001272 fc_lport_rpn_id_resp,
1273 lport, lport->e_d_tov))
1274 fc_lport_error(lport, fp);
1275}
1276
1277static struct fc_rport_operations fc_lport_rport_ops = {
1278 .event_callback = fc_lport_rport_callback,
1279};
1280
1281/**
Robert Love34f42a02009-02-27 10:55:45 -08001282 * fc_rport_enter_dns() - Create a rport to the name server
Robert Love42e9a922008-12-09 15:10:17 -08001283 * @lport: Fibre Channel local port requesting a rport for the name server
1284 *
1285 * Locking Note: The lport lock is expected to be held before calling
1286 * this routine.
1287 */
1288static void fc_lport_enter_dns(struct fc_lport *lport)
1289{
Joe Eykholtab28f1f2009-08-25 14:00:34 -07001290 struct fc_rport_priv *rdata;
Joe Eykholt795d86f2009-08-25 14:00:39 -07001291 struct fc_rport_identifiers ids;
Robert Love42e9a922008-12-09 15:10:17 -08001292
Joe Eykholt795d86f2009-08-25 14:00:39 -07001293 ids.port_id = FC_FID_DIR_SERV;
1294 ids.port_name = -1;
1295 ids.node_name = -1;
1296 ids.roles = FC_RPORT_ROLE_UNKNOWN;
Robert Love42e9a922008-12-09 15:10:17 -08001297
Robert Love74147052009-06-10 15:31:10 -07001298 FC_LPORT_DBG(lport, "Entered DNS state from %s state\n",
1299 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001300
1301 fc_lport_state_enter(lport, LPORT_ST_DNS);
1302
Joe Eykholt48f00902009-08-25 14:01:50 -07001303 mutex_lock(&lport->disc.disc_mutex);
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001304 rdata = lport->tt.rport_create(lport, &ids);
Joe Eykholt48f00902009-08-25 14:01:50 -07001305 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001306 if (!rdata)
Robert Love42e9a922008-12-09 15:10:17 -08001307 goto err;
1308
Robert Love42e9a922008-12-09 15:10:17 -08001309 rdata->ops = &fc_lport_rport_ops;
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001310 lport->tt.rport_login(rdata);
Robert Love42e9a922008-12-09 15:10:17 -08001311 return;
1312
1313err:
1314 fc_lport_error(lport, NULL);
1315}
1316
1317/**
Robert Love34f42a02009-02-27 10:55:45 -08001318 * fc_lport_timeout() - Handler for the retry_work timer.
Robert Love42e9a922008-12-09 15:10:17 -08001319 * @work: The work struct of the fc_lport
1320 */
1321static void fc_lport_timeout(struct work_struct *work)
1322{
1323 struct fc_lport *lport =
1324 container_of(work, struct fc_lport,
1325 retry_work.work);
1326
1327 mutex_lock(&lport->lp_mutex);
1328
1329 switch (lport->state) {
Joe Eykholtb1d9fd52009-07-29 17:04:22 -07001330 case LPORT_ST_DISABLED:
Robert Love42e9a922008-12-09 15:10:17 -08001331 case LPORT_ST_READY:
1332 case LPORT_ST_RESET:
1333 WARN_ON(1);
1334 break;
1335 case LPORT_ST_FLOGI:
1336 fc_lport_enter_flogi(lport);
1337 break;
1338 case LPORT_ST_DNS:
1339 fc_lport_enter_dns(lport);
1340 break;
1341 case LPORT_ST_RPN_ID:
1342 fc_lport_enter_rpn_id(lport);
1343 break;
1344 case LPORT_ST_RFT_ID:
1345 fc_lport_enter_rft_id(lport);
1346 break;
1347 case LPORT_ST_SCR:
1348 fc_lport_enter_scr(lport);
1349 break;
1350 case LPORT_ST_LOGO:
1351 fc_lport_enter_logo(lport);
1352 break;
1353 }
1354
1355 mutex_unlock(&lport->lp_mutex);
1356}
1357
1358/**
Robert Love34f42a02009-02-27 10:55:45 -08001359 * fc_lport_logo_resp() - Handle response to LOGO request
Robert Love42e9a922008-12-09 15:10:17 -08001360 * @sp: current sequence in LOGO exchange
1361 * @fp: response frame
1362 * @lp_arg: Fibre Channel lport port instance that sent the LOGO request
1363 *
1364 * Locking Note: This function will be called without the lport lock
1365 * held, but it will lock, call an _enter_* function or fc_lport_error
1366 * and then unlock the lport.
1367 */
1368static void fc_lport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
1369 void *lp_arg)
1370{
1371 struct fc_lport *lport = lp_arg;
1372 u8 op;
1373
1374 if (fp == ERR_PTR(-FC_EX_CLOSED))
1375 return;
1376
1377 mutex_lock(&lport->lp_mutex);
1378
Robert Love74147052009-06-10 15:31:10 -07001379 FC_LPORT_DBG(lport, "Received a LOGO response\n");
Robert Love42e9a922008-12-09 15:10:17 -08001380
1381 if (lport->state != LPORT_ST_LOGO) {
Robert Love74147052009-06-10 15:31:10 -07001382 FC_LPORT_DBG(lport, "Received a LOGO response, but in state "
1383 "%s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001384 if (IS_ERR(fp))
1385 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001386 goto out;
1387 }
1388
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001389 if (IS_ERR(fp)) {
1390 fc_lport_error(lport, fp);
1391 goto err;
1392 }
1393
Robert Love42e9a922008-12-09 15:10:17 -08001394 op = fc_frame_payload_op(fp);
1395 if (op == ELS_LS_ACC)
Joe Eykholt1190d922009-07-29 17:04:27 -07001396 fc_lport_enter_disabled(lport);
Robert Love42e9a922008-12-09 15:10:17 -08001397 else
1398 fc_lport_error(lport, fp);
1399
1400out:
1401 fc_frame_free(fp);
1402err:
1403 mutex_unlock(&lport->lp_mutex);
1404}
1405
1406/**
Robert Love34f42a02009-02-27 10:55:45 -08001407 * fc_rport_enter_logo() - Logout of the fabric
Robert Love42e9a922008-12-09 15:10:17 -08001408 * @lport: Fibre Channel local port to be logged out
1409 *
1410 * Locking Note: The lport lock is expected to be held before calling
1411 * this routine.
1412 */
1413static void fc_lport_enter_logo(struct fc_lport *lport)
1414{
1415 struct fc_frame *fp;
1416 struct fc_els_logo *logo;
1417
Robert Love74147052009-06-10 15:31:10 -07001418 FC_LPORT_DBG(lport, "Entered LOGO state from %s state\n",
1419 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001420
1421 fc_lport_state_enter(lport, LPORT_ST_LOGO);
1422
Robert Love42e9a922008-12-09 15:10:17 -08001423 fp = fc_frame_alloc(lport, sizeof(*logo));
1424 if (!fp) {
1425 fc_lport_error(lport, fp);
1426 return;
1427 }
1428
Joe Eykholta46f3272009-08-25 14:00:55 -07001429 if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp, ELS_LOGO,
1430 fc_lport_logo_resp, lport, lport->e_d_tov))
Robert Love42e9a922008-12-09 15:10:17 -08001431 fc_lport_error(lport, fp);
1432}
1433
1434/**
Robert Love34f42a02009-02-27 10:55:45 -08001435 * fc_lport_flogi_resp() - Handle response to FLOGI request
Robert Love42e9a922008-12-09 15:10:17 -08001436 * @sp: current sequence in FLOGI exchange
1437 * @fp: response frame
1438 * @lp_arg: Fibre Channel lport port instance that sent the FLOGI request
1439 *
1440 * Locking Note: This function will be called without the lport lock
1441 * held, but it will lock, call an _enter_* function or fc_lport_error
1442 * and then unlock the lport.
1443 */
1444static void fc_lport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
1445 void *lp_arg)
1446{
1447 struct fc_lport *lport = lp_arg;
1448 struct fc_frame_header *fh;
1449 struct fc_els_flogi *flp;
1450 u32 did;
1451 u16 csp_flags;
1452 unsigned int r_a_tov;
1453 unsigned int e_d_tov;
1454 u16 mfs;
1455
1456 if (fp == ERR_PTR(-FC_EX_CLOSED))
1457 return;
1458
1459 mutex_lock(&lport->lp_mutex);
1460
Robert Love74147052009-06-10 15:31:10 -07001461 FC_LPORT_DBG(lport, "Received a FLOGI response\n");
Robert Love42e9a922008-12-09 15:10:17 -08001462
1463 if (lport->state != LPORT_ST_FLOGI) {
Robert Love74147052009-06-10 15:31:10 -07001464 FC_LPORT_DBG(lport, "Received a FLOGI response, but in state "
1465 "%s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001466 if (IS_ERR(fp))
1467 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001468 goto out;
1469 }
1470
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001471 if (IS_ERR(fp)) {
1472 fc_lport_error(lport, fp);
1473 goto err;
1474 }
1475
Robert Love42e9a922008-12-09 15:10:17 -08001476 fh = fc_frame_header_get(fp);
1477 did = ntoh24(fh->fh_d_id);
1478 if (fc_frame_payload_op(fp) == ELS_LS_ACC && did != 0) {
1479
Robert Love74147052009-06-10 15:31:10 -07001480 printk(KERN_INFO "libfc: Assigned FID (%6x) in FLOGI response\n",
1481 did);
Robert Love42e9a922008-12-09 15:10:17 -08001482 fc_host_port_id(lport->host) = did;
1483
1484 flp = fc_frame_payload_get(fp, sizeof(*flp));
1485 if (flp) {
1486 mfs = ntohs(flp->fl_csp.sp_bb_data) &
1487 FC_SP_BB_DATA_MASK;
1488 if (mfs >= FC_SP_MIN_MAX_PAYLOAD &&
1489 mfs < lport->mfs)
1490 lport->mfs = mfs;
1491 csp_flags = ntohs(flp->fl_csp.sp_features);
1492 r_a_tov = ntohl(flp->fl_csp.sp_r_a_tov);
1493 e_d_tov = ntohl(flp->fl_csp.sp_e_d_tov);
1494 if (csp_flags & FC_SP_FT_EDTR)
1495 e_d_tov /= 1000000;
1496 if ((csp_flags & FC_SP_FT_FPORT) == 0) {
1497 if (e_d_tov > lport->e_d_tov)
1498 lport->e_d_tov = e_d_tov;
1499 lport->r_a_tov = 2 * e_d_tov;
Robert Love74147052009-06-10 15:31:10 -07001500 printk(KERN_INFO "libfc: Port (%6x) entered "
1501 "point to point mode\n", did);
Robert Love42e9a922008-12-09 15:10:17 -08001502 fc_lport_ptp_setup(lport, ntoh24(fh->fh_s_id),
1503 get_unaligned_be64(
1504 &flp->fl_wwpn),
1505 get_unaligned_be64(
1506 &flp->fl_wwnn));
1507 } else {
1508 lport->e_d_tov = e_d_tov;
1509 lport->r_a_tov = r_a_tov;
1510 fc_host_fabric_name(lport->host) =
1511 get_unaligned_be64(&flp->fl_wwnn);
1512 fc_lport_enter_dns(lport);
1513 }
1514 }
1515
1516 if (flp) {
1517 csp_flags = ntohs(flp->fl_csp.sp_features);
1518 if ((csp_flags & FC_SP_FT_FPORT) == 0) {
1519 lport->tt.disc_start(fc_lport_disc_callback,
1520 lport);
1521 }
1522 }
1523 } else {
Robert Love74147052009-06-10 15:31:10 -07001524 FC_LPORT_DBG(lport, "Bad FLOGI response\n");
Robert Love42e9a922008-12-09 15:10:17 -08001525 }
1526
1527out:
1528 fc_frame_free(fp);
1529err:
1530 mutex_unlock(&lport->lp_mutex);
1531}
1532
1533/**
Robert Love34f42a02009-02-27 10:55:45 -08001534 * fc_rport_enter_flogi() - Send a FLOGI request to the fabric manager
Robert Love42e9a922008-12-09 15:10:17 -08001535 * @lport: Fibre Channel local port to be logged in to the fabric
1536 *
1537 * Locking Note: The lport lock is expected to be held before calling
1538 * this routine.
1539 */
1540void fc_lport_enter_flogi(struct fc_lport *lport)
1541{
1542 struct fc_frame *fp;
1543
Robert Love74147052009-06-10 15:31:10 -07001544 FC_LPORT_DBG(lport, "Entered FLOGI state from %s state\n",
1545 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001546
1547 fc_lport_state_enter(lport, LPORT_ST_FLOGI);
1548
1549 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
1550 if (!fp)
1551 return fc_lport_error(lport, fp);
1552
Joe Eykholta46f3272009-08-25 14:00:55 -07001553 if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp, ELS_FLOGI,
Robert Love42e9a922008-12-09 15:10:17 -08001554 fc_lport_flogi_resp, lport, lport->e_d_tov))
1555 fc_lport_error(lport, fp);
1556}
1557
1558/* Configure a fc_lport */
1559int fc_lport_config(struct fc_lport *lport)
1560{
1561 INIT_DELAYED_WORK(&lport->retry_work, fc_lport_timeout);
1562 mutex_init(&lport->lp_mutex);
1563
Joe Eykholtb1d9fd52009-07-29 17:04:22 -07001564 fc_lport_state_enter(lport, LPORT_ST_DISABLED);
Robert Love42e9a922008-12-09 15:10:17 -08001565
1566 fc_lport_add_fc4_type(lport, FC_TYPE_FCP);
1567 fc_lport_add_fc4_type(lport, FC_TYPE_CT);
1568
1569 return 0;
1570}
1571EXPORT_SYMBOL(fc_lport_config);
1572
1573int fc_lport_init(struct fc_lport *lport)
1574{
1575 if (!lport->tt.lport_recv)
1576 lport->tt.lport_recv = fc_lport_recv_req;
1577
1578 if (!lport->tt.lport_reset)
1579 lport->tt.lport_reset = fc_lport_reset;
1580
1581 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
1582 fc_host_node_name(lport->host) = lport->wwnn;
1583 fc_host_port_name(lport->host) = lport->wwpn;
1584 fc_host_supported_classes(lport->host) = FC_COS_CLASS3;
1585 memset(fc_host_supported_fc4s(lport->host), 0,
1586 sizeof(fc_host_supported_fc4s(lport->host)));
1587 fc_host_supported_fc4s(lport->host)[2] = 1;
1588 fc_host_supported_fc4s(lport->host)[7] = 1;
1589
1590 /* This value is also unchanging */
1591 memset(fc_host_active_fc4s(lport->host), 0,
1592 sizeof(fc_host_active_fc4s(lport->host)));
1593 fc_host_active_fc4s(lport->host)[2] = 1;
1594 fc_host_active_fc4s(lport->host)[7] = 1;
1595 fc_host_maxframe_size(lport->host) = lport->mfs;
1596 fc_host_supported_speeds(lport->host) = 0;
1597 if (lport->link_supported_speeds & FC_PORTSPEED_1GBIT)
1598 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_1GBIT;
1599 if (lport->link_supported_speeds & FC_PORTSPEED_10GBIT)
1600 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_10GBIT;
1601
Vasu Dev96316092009-07-29 17:05:00 -07001602 INIT_LIST_HEAD(&lport->ema_list);
Robert Love42e9a922008-12-09 15:10:17 -08001603 return 0;
1604}
1605EXPORT_SYMBOL(fc_lport_init);