blob: 47577e4a2e87dadd759cd8eb5d44d0643a1c0678 [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
Robert Love8866a5d2009-11-03 11:45:58 -080098#include "fc_libfc.h"
99
Robert Love42e9a922008-12-09 15:10:17 -0800100/* Fabric IDs to use for point-to-point mode, chosen on whims. */
101#define FC_LOCAL_PTP_FID_LO 0x010101
102#define FC_LOCAL_PTP_FID_HI 0x010102
103
104#define DNS_DELAY 3 /* Discovery delay after RSCN (in seconds)*/
105
Robert Love42e9a922008-12-09 15:10:17 -0800106static void fc_lport_error(struct fc_lport *, struct fc_frame *);
107
108static void fc_lport_enter_reset(struct fc_lport *);
109static void fc_lport_enter_flogi(struct fc_lport *);
110static void fc_lport_enter_dns(struct fc_lport *);
Robert Love42e9a922008-12-09 15:10:17 -0800111static void fc_lport_enter_rft_id(struct fc_lport *);
112static void fc_lport_enter_scr(struct fc_lport *);
113static void fc_lport_enter_ready(struct fc_lport *);
114static void fc_lport_enter_logo(struct fc_lport *);
115
116static const char *fc_lport_state_names[] = {
Joe Eykholtb1d9fd52009-07-29 17:04:22 -0700117 [LPORT_ST_DISABLED] = "disabled",
Robert Love42e9a922008-12-09 15:10:17 -0800118 [LPORT_ST_FLOGI] = "FLOGI",
119 [LPORT_ST_DNS] = "dNS",
Robert Love42e9a922008-12-09 15:10:17 -0800120 [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;
Chris Leech28cc0e32009-11-03 11:46:46 -0800154 fc_lport_enter_rft_id(lport);
Joe Eykholtb5cbf082009-08-25 14:01:44 -0700155 } 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 Eykholt48f00902009-08-25 14:01:50 -0700201 mutex_lock(&lport->disc.disc_mutex);
202 if (lport->ptp_rp)
Robert Love42e9a922008-12-09 15:10:17 -0800203 lport->tt.rport_logoff(lport->ptp_rp);
Robert Love9737e6a2009-08-25 14:02:59 -0700204 lport->ptp_rp = lport->tt.rport_create(lport, remote_fid);
205 lport->ptp_rp->ids.port_name = remote_wwpn;
206 lport->ptp_rp->ids.node_name = remote_wwnn;
Joe Eykholt48f00902009-08-25 14:01:50 -0700207 mutex_unlock(&lport->disc.disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800208
209 lport->tt.rport_login(lport->ptp_rp);
210
211 fc_lport_enter_ready(lport);
212}
213
214void fc_get_host_port_type(struct Scsi_Host *shost)
215{
216 /* TODO - currently just NPORT */
217 fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
218}
219EXPORT_SYMBOL(fc_get_host_port_type);
220
221void fc_get_host_port_state(struct Scsi_Host *shost)
222{
223 struct fc_lport *lp = shost_priv(shost);
224
Chris Leech8faecdd2009-11-03 11:46:19 -0800225 mutex_lock(&lp->lp_mutex);
226 if (!lp->link_up)
227 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
Robert Love42e9a922008-12-09 15:10:17 -0800228 else
Chris Leech8faecdd2009-11-03 11:46:19 -0800229 switch (lp->state) {
230 case LPORT_ST_READY:
231 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
232 break;
233 default:
234 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
235 }
236 mutex_unlock(&lp->lp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800237}
238EXPORT_SYMBOL(fc_get_host_port_state);
239
240void fc_get_host_speed(struct Scsi_Host *shost)
241{
242 struct fc_lport *lport = shost_priv(shost);
243
244 fc_host_speed(shost) = lport->link_speed;
245}
246EXPORT_SYMBOL(fc_get_host_speed);
247
248struct fc_host_statistics *fc_get_host_stats(struct Scsi_Host *shost)
249{
Robert Love42e9a922008-12-09 15:10:17 -0800250 struct fc_host_statistics *fcoe_stats;
251 struct fc_lport *lp = shost_priv(shost);
252 struct timespec v0, v1;
Robert Love582b45b2009-03-31 15:51:50 -0700253 unsigned int cpu;
Robert Love42e9a922008-12-09 15:10:17 -0800254
255 fcoe_stats = &lp->host_stats;
256 memset(fcoe_stats, 0, sizeof(struct fc_host_statistics));
257
258 jiffies_to_timespec(jiffies, &v0);
259 jiffies_to_timespec(lp->boot_time, &v1);
260 fcoe_stats->seconds_since_last_reset = (v0.tv_sec - v1.tv_sec);
261
Robert Love582b45b2009-03-31 15:51:50 -0700262 for_each_possible_cpu(cpu) {
263 struct fcoe_dev_stats *stats;
264
265 stats = per_cpu_ptr(lp->dev_stats, cpu);
266
Robert Love42e9a922008-12-09 15:10:17 -0800267 fcoe_stats->tx_frames += stats->TxFrames;
268 fcoe_stats->tx_words += stats->TxWords;
269 fcoe_stats->rx_frames += stats->RxFrames;
270 fcoe_stats->rx_words += stats->RxWords;
271 fcoe_stats->error_frames += stats->ErrorFrames;
272 fcoe_stats->invalid_crc_count += stats->InvalidCRCCount;
273 fcoe_stats->fcp_input_requests += stats->InputRequests;
274 fcoe_stats->fcp_output_requests += stats->OutputRequests;
275 fcoe_stats->fcp_control_requests += stats->ControlRequests;
276 fcoe_stats->fcp_input_megabytes += stats->InputMegabytes;
277 fcoe_stats->fcp_output_megabytes += stats->OutputMegabytes;
278 fcoe_stats->link_failure_count += stats->LinkFailureCount;
279 }
280 fcoe_stats->lip_count = -1;
281 fcoe_stats->nos_count = -1;
282 fcoe_stats->loss_of_sync_count = -1;
283 fcoe_stats->loss_of_signal_count = -1;
284 fcoe_stats->prim_seq_protocol_err_count = -1;
285 fcoe_stats->dumped_frames = -1;
286 return fcoe_stats;
287}
288EXPORT_SYMBOL(fc_get_host_stats);
289
290/*
291 * Fill in FLOGI command for request.
292 */
293static void
294fc_lport_flogi_fill(struct fc_lport *lport, struct fc_els_flogi *flogi,
295 unsigned int op)
296{
297 struct fc_els_csp *sp;
298 struct fc_els_cssp *cp;
299
300 memset(flogi, 0, sizeof(*flogi));
301 flogi->fl_cmd = (u8) op;
302 put_unaligned_be64(lport->wwpn, &flogi->fl_wwpn);
303 put_unaligned_be64(lport->wwnn, &flogi->fl_wwnn);
304 sp = &flogi->fl_csp;
305 sp->sp_hi_ver = 0x20;
306 sp->sp_lo_ver = 0x20;
307 sp->sp_bb_cred = htons(10); /* this gets set by gateway */
308 sp->sp_bb_data = htons((u16) lport->mfs);
309 cp = &flogi->fl_cssp[3 - 1]; /* class 3 parameters */
310 cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
311 if (op != ELS_FLOGI) {
312 sp->sp_features = htons(FC_SP_FT_CIRO);
313 sp->sp_tot_seq = htons(255); /* seq. we accept */
314 sp->sp_rel_off = htons(0x1f);
315 sp->sp_e_d_tov = htonl(lport->e_d_tov);
316
317 cp->cp_rdfs = htons((u16) lport->mfs);
318 cp->cp_con_seq = htons(255);
319 cp->cp_open_seq = 1;
320 }
321}
322
323/*
324 * Add a supported FC-4 type.
325 */
326static void fc_lport_add_fc4_type(struct fc_lport *lport, enum fc_fh_type type)
327{
328 __be32 *mp;
329
330 mp = &lport->fcts.ff_type_map[type / FC_NS_BPW];
331 *mp = htonl(ntohl(*mp) | 1UL << (type % FC_NS_BPW));
332}
333
334/**
Robert Love34f42a02009-02-27 10:55:45 -0800335 * fc_lport_recv_rlir_req() - Handle received Registered Link Incident Report.
Robert Love42e9a922008-12-09 15:10:17 -0800336 * @lport: Fibre Channel local port recieving the RLIR
337 * @sp: current sequence in the RLIR exchange
338 * @fp: RLIR request frame
339 *
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700340 * Locking Note: The lport lock is expected to be held before calling
Robert Love42e9a922008-12-09 15:10:17 -0800341 * this function.
342 */
343static void fc_lport_recv_rlir_req(struct fc_seq *sp, struct fc_frame *fp,
344 struct fc_lport *lport)
345{
Robert Love74147052009-06-10 15:31:10 -0700346 FC_LPORT_DBG(lport, "Received RLIR request while in state %s\n",
347 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800348
349 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
350 fc_frame_free(fp);
351}
352
353/**
Robert Love34f42a02009-02-27 10:55:45 -0800354 * fc_lport_recv_echo_req() - Handle received ECHO request
Robert Love42e9a922008-12-09 15:10:17 -0800355 * @lport: Fibre Channel local port recieving the ECHO
356 * @sp: current sequence in the ECHO exchange
357 * @fp: ECHO request frame
358 *
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700359 * Locking Note: The lport lock is expected to be held before calling
Robert Love42e9a922008-12-09 15:10:17 -0800360 * this function.
361 */
362static void fc_lport_recv_echo_req(struct fc_seq *sp, struct fc_frame *in_fp,
363 struct fc_lport *lport)
364{
365 struct fc_frame *fp;
366 struct fc_exch *ep = fc_seq_exch(sp);
367 unsigned int len;
368 void *pp;
369 void *dp;
370 u32 f_ctl;
371
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700372 FC_LPORT_DBG(lport, "Received ECHO request while in state %s\n",
Robert Love74147052009-06-10 15:31:10 -0700373 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800374
375 len = fr_len(in_fp) - sizeof(struct fc_frame_header);
376 pp = fc_frame_payload_get(in_fp, len);
377
378 if (len < sizeof(__be32))
379 len = sizeof(__be32);
380
381 fp = fc_frame_alloc(lport, len);
382 if (fp) {
383 dp = fc_frame_payload_get(fp, len);
384 memcpy(dp, pp, len);
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700385 *((__be32 *)dp) = htonl(ELS_LS_ACC << 24);
Robert Love42e9a922008-12-09 15:10:17 -0800386 sp = lport->tt.seq_start_next(sp);
387 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
388 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
389 FC_TYPE_ELS, f_ctl, 0);
390 lport->tt.seq_send(lport, sp, fp);
391 }
392 fc_frame_free(in_fp);
393}
394
395/**
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700396 * fc_lport_recv_rnid_req() - Handle received Request Node ID data request
397 * @sp: The sequence in the RNID exchange
398 * @fp: The RNID request frame
399 * @lport: The local port recieving the RNID
Robert Love42e9a922008-12-09 15:10:17 -0800400 *
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700401 * Locking Note: The lport lock is expected to be held before calling
Robert Love42e9a922008-12-09 15:10:17 -0800402 * this function.
403 */
404static void fc_lport_recv_rnid_req(struct fc_seq *sp, struct fc_frame *in_fp,
405 struct fc_lport *lport)
406{
407 struct fc_frame *fp;
408 struct fc_exch *ep = fc_seq_exch(sp);
409 struct fc_els_rnid *req;
410 struct {
411 struct fc_els_rnid_resp rnid;
412 struct fc_els_rnid_cid cid;
413 struct fc_els_rnid_gen gen;
414 } *rp;
415 struct fc_seq_els_data rjt_data;
416 u8 fmt;
417 size_t len;
418 u32 f_ctl;
419
Robert Love74147052009-06-10 15:31:10 -0700420 FC_LPORT_DBG(lport, "Received RNID request while in state %s\n",
421 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800422
423 req = fc_frame_payload_get(in_fp, sizeof(*req));
424 if (!req) {
425 rjt_data.fp = NULL;
426 rjt_data.reason = ELS_RJT_LOGIC;
427 rjt_data.explan = ELS_EXPL_NONE;
428 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
429 } else {
430 fmt = req->rnid_fmt;
431 len = sizeof(*rp);
432 if (fmt != ELS_RNIDF_GEN ||
433 ntohl(lport->rnid_gen.rnid_atype) == 0) {
434 fmt = ELS_RNIDF_NONE; /* nothing to provide */
435 len -= sizeof(rp->gen);
436 }
437 fp = fc_frame_alloc(lport, len);
438 if (fp) {
439 rp = fc_frame_payload_get(fp, len);
440 memset(rp, 0, len);
441 rp->rnid.rnid_cmd = ELS_LS_ACC;
442 rp->rnid.rnid_fmt = fmt;
443 rp->rnid.rnid_cid_len = sizeof(rp->cid);
444 rp->cid.rnid_wwpn = htonll(lport->wwpn);
445 rp->cid.rnid_wwnn = htonll(lport->wwnn);
446 if (fmt == ELS_RNIDF_GEN) {
447 rp->rnid.rnid_sid_len = sizeof(rp->gen);
448 memcpy(&rp->gen, &lport->rnid_gen,
449 sizeof(rp->gen));
450 }
451 sp = lport->tt.seq_start_next(sp);
452 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
453 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
454 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
455 FC_TYPE_ELS, f_ctl, 0);
456 lport->tt.seq_send(lport, sp, fp);
457 }
458 }
459 fc_frame_free(in_fp);
460}
461
462/**
Robert Love34f42a02009-02-27 10:55:45 -0800463 * fc_lport_recv_logo_req() - Handle received fabric LOGO request
Robert Love42e9a922008-12-09 15:10:17 -0800464 * @lport: Fibre Channel local port recieving the LOGO
465 * @sp: current sequence in the LOGO exchange
466 * @fp: LOGO request frame
467 *
468 * Locking Note: The lport lock is exected to be held before calling
469 * this function.
470 */
471static void fc_lport_recv_logo_req(struct fc_seq *sp, struct fc_frame *fp,
472 struct fc_lport *lport)
473{
474 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
475 fc_lport_enter_reset(lport);
476 fc_frame_free(fp);
477}
478
479/**
Robert Love34f42a02009-02-27 10:55:45 -0800480 * fc_fabric_login() - Start the lport state machine
Robert Love42e9a922008-12-09 15:10:17 -0800481 * @lport: The lport that should log into the fabric
482 *
483 * Locking Note: This function should not be called
484 * with the lport lock held.
485 */
486int fc_fabric_login(struct fc_lport *lport)
487{
488 int rc = -1;
489
490 mutex_lock(&lport->lp_mutex);
Joe Eykholtb1d9fd52009-07-29 17:04:22 -0700491 if (lport->state == LPORT_ST_DISABLED) {
Robert Love42e9a922008-12-09 15:10:17 -0800492 fc_lport_enter_reset(lport);
493 rc = 0;
494 }
495 mutex_unlock(&lport->lp_mutex);
496
497 return rc;
498}
499EXPORT_SYMBOL(fc_fabric_login);
500
501/**
Chris Leech8faecdd2009-11-03 11:46:19 -0800502 * __fc_linkup() - Handler for transport linkup events
503 * @lport: The lport whose link is up
504 *
505 * Locking: must be called with the lp_mutex held
506 */
507void __fc_linkup(struct fc_lport *lport)
508{
509 if (!lport->link_up) {
510 lport->link_up = 1;
511
512 if (lport->state == LPORT_ST_RESET)
513 fc_lport_enter_flogi(lport);
514 }
515}
516
517/**
Robert Love34f42a02009-02-27 10:55:45 -0800518 * fc_linkup() - Handler for transport linkup events
Robert Love42e9a922008-12-09 15:10:17 -0800519 * @lport: The lport whose link is up
520 */
521void fc_linkup(struct fc_lport *lport)
522{
Robert Love74147052009-06-10 15:31:10 -0700523 printk(KERN_INFO "libfc: Link up on port (%6x)\n",
524 fc_host_port_id(lport->host));
Robert Love42e9a922008-12-09 15:10:17 -0800525
526 mutex_lock(&lport->lp_mutex);
Chris Leech8faecdd2009-11-03 11:46:19 -0800527 __fc_linkup(lport);
Robert Love42e9a922008-12-09 15:10:17 -0800528 mutex_unlock(&lport->lp_mutex);
529}
530EXPORT_SYMBOL(fc_linkup);
531
532/**
Chris Leech8faecdd2009-11-03 11:46:19 -0800533 * __fc_linkdown() - Handler for transport linkdown events
534 * @lport: The lport whose link is down
535 *
536 * Locking: must be called with the lp_mutex held
537 */
538void __fc_linkdown(struct fc_lport *lport)
539{
540 if (lport->link_up) {
541 lport->link_up = 0;
542 fc_lport_enter_reset(lport);
543 lport->tt.fcp_cleanup(lport);
544 }
545}
546
547/**
Robert Love34f42a02009-02-27 10:55:45 -0800548 * fc_linkdown() - Handler for transport linkdown events
Robert Love42e9a922008-12-09 15:10:17 -0800549 * @lport: The lport whose link is down
550 */
551void fc_linkdown(struct fc_lport *lport)
552{
Robert Love74147052009-06-10 15:31:10 -0700553 printk(KERN_INFO "libfc: Link down on port (%6x)\n",
554 fc_host_port_id(lport->host));
Robert Love42e9a922008-12-09 15:10:17 -0800555
Chris Leech8faecdd2009-11-03 11:46:19 -0800556 mutex_lock(&lport->lp_mutex);
557 __fc_linkdown(lport);
Robert Love42e9a922008-12-09 15:10:17 -0800558 mutex_unlock(&lport->lp_mutex);
559}
560EXPORT_SYMBOL(fc_linkdown);
561
562/**
Robert Love34f42a02009-02-27 10:55:45 -0800563 * fc_fabric_logoff() - Logout of the fabric
Robert Love42e9a922008-12-09 15:10:17 -0800564 * @lport: fc_lport pointer to logoff the fabric
565 *
566 * Return value:
567 * 0 for success, -1 for failure
Robert Love34f42a02009-02-27 10:55:45 -0800568 */
Robert Love42e9a922008-12-09 15:10:17 -0800569int fc_fabric_logoff(struct fc_lport *lport)
570{
571 lport->tt.disc_stop_final(lport);
572 mutex_lock(&lport->lp_mutex);
Abhijeet Joglekara0fd2e42009-04-21 16:27:09 -0700573 if (lport->dns_rp)
574 lport->tt.rport_logoff(lport->dns_rp);
575 mutex_unlock(&lport->lp_mutex);
576 lport->tt.rport_flush_queue();
577 mutex_lock(&lport->lp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800578 fc_lport_enter_logo(lport);
579 mutex_unlock(&lport->lp_mutex);
Steve Maf7db2c152009-02-27 10:55:13 -0800580 cancel_delayed_work_sync(&lport->retry_work);
Robert Love42e9a922008-12-09 15:10:17 -0800581 return 0;
582}
583EXPORT_SYMBOL(fc_fabric_logoff);
584
585/**
Robert Love34f42a02009-02-27 10:55:45 -0800586 * fc_lport_destroy() - unregister a fc_lport
Robert Love42e9a922008-12-09 15:10:17 -0800587 * @lport: fc_lport pointer to unregister
588 *
589 * Return value:
590 * None
591 * Note:
592 * exit routine for fc_lport instance
593 * clean-up all the allocated memory
594 * and free up other system resources.
595 *
Robert Love34f42a02009-02-27 10:55:45 -0800596 */
Robert Love42e9a922008-12-09 15:10:17 -0800597int fc_lport_destroy(struct fc_lport *lport)
598{
Abhijeet Joglekarbbf15662009-04-21 16:27:14 -0700599 mutex_lock(&lport->lp_mutex);
Joe Eykholtb1d9fd52009-07-29 17:04:22 -0700600 lport->state = LPORT_ST_DISABLED;
Abhijeet Joglekarbbf15662009-04-21 16:27:14 -0700601 lport->link_up = 0;
Robert Love42e9a922008-12-09 15:10:17 -0800602 lport->tt.frame_send = fc_frame_drop;
Abhijeet Joglekarbbf15662009-04-21 16:27:14 -0700603 mutex_unlock(&lport->lp_mutex);
604
Robert Love42e9a922008-12-09 15:10:17 -0800605 lport->tt.fcp_abort_io(lport);
Joe Eykholte9ba8b42009-07-29 17:04:33 -0700606 lport->tt.disc_stop_final(lport);
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -0800607 lport->tt.exch_mgr_reset(lport, 0, 0);
Robert Love42e9a922008-12-09 15:10:17 -0800608 return 0;
609}
610EXPORT_SYMBOL(fc_lport_destroy);
611
612/**
Robert Love34f42a02009-02-27 10:55:45 -0800613 * fc_set_mfs() - sets up the mfs for the corresponding fc_lport
Robert Love42e9a922008-12-09 15:10:17 -0800614 * @lport: fc_lport pointer to unregister
615 * @mfs: the new mfs for fc_lport
616 *
617 * Set mfs for the given fc_lport to the new mfs.
618 *
619 * Return: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800620 */
Robert Love42e9a922008-12-09 15:10:17 -0800621int fc_set_mfs(struct fc_lport *lport, u32 mfs)
622{
623 unsigned int old_mfs;
624 int rc = -EINVAL;
625
626 mutex_lock(&lport->lp_mutex);
627
628 old_mfs = lport->mfs;
629
630 if (mfs >= FC_MIN_MAX_FRAME) {
631 mfs &= ~3;
632 if (mfs > FC_MAX_FRAME)
633 mfs = FC_MAX_FRAME;
634 mfs -= sizeof(struct fc_frame_header);
635 lport->mfs = mfs;
636 rc = 0;
637 }
638
639 if (!rc && mfs < old_mfs)
640 fc_lport_enter_reset(lport);
641
642 mutex_unlock(&lport->lp_mutex);
643
644 return rc;
645}
646EXPORT_SYMBOL(fc_set_mfs);
647
648/**
Robert Love34f42a02009-02-27 10:55:45 -0800649 * fc_lport_disc_callback() - Callback for discovery events
Robert Love42e9a922008-12-09 15:10:17 -0800650 * @lport: FC local port
651 * @event: The discovery event
652 */
653void fc_lport_disc_callback(struct fc_lport *lport, enum fc_disc_event event)
654{
655 switch (event) {
656 case DISC_EV_SUCCESS:
Robert Love74147052009-06-10 15:31:10 -0700657 FC_LPORT_DBG(lport, "Discovery succeeded\n");
Robert Love42e9a922008-12-09 15:10:17 -0800658 break;
659 case DISC_EV_FAILED:
Robert Love74147052009-06-10 15:31:10 -0700660 printk(KERN_ERR "libfc: Discovery failed for port (%6x)\n",
661 fc_host_port_id(lport->host));
Robert Love42e9a922008-12-09 15:10:17 -0800662 mutex_lock(&lport->lp_mutex);
663 fc_lport_enter_reset(lport);
664 mutex_unlock(&lport->lp_mutex);
665 break;
666 case DISC_EV_NONE:
667 WARN_ON(1);
668 break;
669 }
670}
671
672/**
Robert Love34f42a02009-02-27 10:55:45 -0800673 * fc_rport_enter_ready() - Enter the ready state and start discovery
Robert Love42e9a922008-12-09 15:10:17 -0800674 * @lport: Fibre Channel local port that is ready
675 *
676 * Locking Note: The lport lock is expected to be held before calling
677 * this routine.
678 */
679static void fc_lport_enter_ready(struct fc_lport *lport)
680{
Robert Love74147052009-06-10 15:31:10 -0700681 FC_LPORT_DBG(lport, "Entered READY from state %s\n",
682 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800683
684 fc_lport_state_enter(lport, LPORT_ST_READY);
Chris Leech8faecdd2009-11-03 11:46:19 -0800685 if (lport->vport)
686 fc_vport_set_state(lport->vport, FC_VPORT_ACTIVE);
687 fc_vports_linkchange(lport);
Robert Love42e9a922008-12-09 15:10:17 -0800688
Joe Eykholt29d898e2009-08-25 14:02:49 -0700689 if (!lport->ptp_rp)
690 lport->tt.disc_start(fc_lport_disc_callback, lport);
Robert Love42e9a922008-12-09 15:10:17 -0800691}
692
693/**
Robert Love34f42a02009-02-27 10:55:45 -0800694 * fc_lport_recv_flogi_req() - Receive a FLOGI request
Robert Love42e9a922008-12-09 15:10:17 -0800695 * @sp_in: The sequence the FLOGI is on
696 * @rx_fp: The frame the FLOGI is in
697 * @lport: The lport that recieved the request
698 *
699 * A received FLOGI request indicates a point-to-point connection.
700 * Accept it with the common service parameters indicating our N port.
701 * Set up to do a PLOGI if we have the higher-number WWPN.
702 *
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700703 * Locking Note: The lport lock is expected to be held before calling
Robert Love42e9a922008-12-09 15:10:17 -0800704 * this function.
705 */
706static void fc_lport_recv_flogi_req(struct fc_seq *sp_in,
707 struct fc_frame *rx_fp,
708 struct fc_lport *lport)
709{
710 struct fc_frame *fp;
711 struct fc_frame_header *fh;
712 struct fc_seq *sp;
713 struct fc_exch *ep;
714 struct fc_els_flogi *flp;
715 struct fc_els_flogi *new_flp;
716 u64 remote_wwpn;
717 u32 remote_fid;
718 u32 local_fid;
719 u32 f_ctl;
720
Robert Love74147052009-06-10 15:31:10 -0700721 FC_LPORT_DBG(lport, "Received FLOGI request while in state %s\n",
722 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800723
724 fh = fc_frame_header_get(rx_fp);
725 remote_fid = ntoh24(fh->fh_s_id);
726 flp = fc_frame_payload_get(rx_fp, sizeof(*flp));
727 if (!flp)
728 goto out;
729 remote_wwpn = get_unaligned_be64(&flp->fl_wwpn);
730 if (remote_wwpn == lport->wwpn) {
Robert Love74147052009-06-10 15:31:10 -0700731 printk(KERN_WARNING "libfc: Received FLOGI from port "
732 "with same WWPN %llx\n", remote_wwpn);
Robert Love42e9a922008-12-09 15:10:17 -0800733 goto out;
734 }
Robert Love74147052009-06-10 15:31:10 -0700735 FC_LPORT_DBG(lport, "FLOGI from port WWPN %llx\n", remote_wwpn);
Robert Love42e9a922008-12-09 15:10:17 -0800736
737 /*
738 * XXX what is the right thing to do for FIDs?
739 * The originator might expect our S_ID to be 0xfffffe.
740 * But if so, both of us could end up with the same FID.
741 */
742 local_fid = FC_LOCAL_PTP_FID_LO;
743 if (remote_wwpn < lport->wwpn) {
744 local_fid = FC_LOCAL_PTP_FID_HI;
745 if (!remote_fid || remote_fid == local_fid)
746 remote_fid = FC_LOCAL_PTP_FID_LO;
747 } else if (!remote_fid) {
748 remote_fid = FC_LOCAL_PTP_FID_HI;
749 }
750
751 fc_host_port_id(lport->host) = local_fid;
752
753 fp = fc_frame_alloc(lport, sizeof(*flp));
754 if (fp) {
755 sp = lport->tt.seq_start_next(fr_seq(rx_fp));
756 new_flp = fc_frame_payload_get(fp, sizeof(*flp));
757 fc_lport_flogi_fill(lport, new_flp, ELS_FLOGI);
758 new_flp->fl_cmd = (u8) ELS_LS_ACC;
759
760 /*
761 * Send the response. If this fails, the originator should
762 * repeat the sequence.
763 */
764 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
765 ep = fc_seq_exch(sp);
766 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
767 FC_TYPE_ELS, f_ctl, 0);
768 lport->tt.seq_send(lport, sp, fp);
769
770 } else {
771 fc_lport_error(lport, fp);
772 }
773 fc_lport_ptp_setup(lport, remote_fid, remote_wwpn,
774 get_unaligned_be64(&flp->fl_wwnn));
775
Robert Love42e9a922008-12-09 15:10:17 -0800776out:
777 sp = fr_seq(rx_fp);
778 fc_frame_free(rx_fp);
779}
780
781/**
Robert Love34f42a02009-02-27 10:55:45 -0800782 * fc_lport_recv_req() - The generic lport request handler
Robert Love42e9a922008-12-09 15:10:17 -0800783 * @lport: The lport that received the request
784 * @sp: The sequence the request is on
785 * @fp: The frame the request is in
786 *
787 * This function will see if the lport handles the request or
788 * if an rport should handle the request.
789 *
790 * Locking Note: This function should not be called with the lport
791 * lock held becuase it will grab the lock.
792 */
793static void fc_lport_recv_req(struct fc_lport *lport, struct fc_seq *sp,
794 struct fc_frame *fp)
795{
796 struct fc_frame_header *fh = fc_frame_header_get(fp);
797 void (*recv) (struct fc_seq *, struct fc_frame *, struct fc_lport *);
Robert Love42e9a922008-12-09 15:10:17 -0800798
799 mutex_lock(&lport->lp_mutex);
800
801 /*
802 * Handle special ELS cases like FLOGI, LOGO, and
803 * RSCN here. These don't require a session.
804 * Even if we had a session, it might not be ready.
805 */
Joe Eykholte9ba8b42009-07-29 17:04:33 -0700806 if (!lport->link_up)
807 fc_frame_free(fp);
808 else if (fh->fh_type == FC_TYPE_ELS &&
809 fh->fh_r_ctl == FC_RCTL_ELS_REQ) {
Robert Love42e9a922008-12-09 15:10:17 -0800810 /*
811 * Check opcode.
812 */
Joe Eykholt131203a2009-08-25 14:03:10 -0700813 recv = lport->tt.rport_recv_req;
Robert Love42e9a922008-12-09 15:10:17 -0800814 switch (fc_frame_payload_op(fp)) {
815 case ELS_FLOGI:
816 recv = fc_lport_recv_flogi_req;
817 break;
818 case ELS_LOGO:
819 fh = fc_frame_header_get(fp);
820 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI)
821 recv = fc_lport_recv_logo_req;
822 break;
823 case ELS_RSCN:
824 recv = lport->tt.disc_recv_req;
825 break;
826 case ELS_ECHO:
827 recv = fc_lport_recv_echo_req;
828 break;
829 case ELS_RLIR:
830 recv = fc_lport_recv_rlir_req;
831 break;
832 case ELS_RNID:
833 recv = fc_lport_recv_rnid_req;
834 break;
Robert Love42e9a922008-12-09 15:10:17 -0800835 }
836
Joe Eykholt131203a2009-08-25 14:03:10 -0700837 recv(sp, fp, lport);
Robert Love42e9a922008-12-09 15:10:17 -0800838 } else {
Robert Love74147052009-06-10 15:31:10 -0700839 FC_LPORT_DBG(lport, "dropping invalid frame (eof %x)\n",
840 fr_eof(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800841 fc_frame_free(fp);
842 }
843 mutex_unlock(&lport->lp_mutex);
844
845 /*
846 * The common exch_done for all request may not be good
847 * if any request requires longer hold on exhange. XXX
848 */
849 lport->tt.exch_done(sp);
850}
851
852/**
Robert Love34f42a02009-02-27 10:55:45 -0800853 * fc_lport_reset() - Reset an lport
Robert Love42e9a922008-12-09 15:10:17 -0800854 * @lport: The lport which should be reset
855 *
856 * Locking Note: This functions should not be called with the
857 * lport lock held.
858 */
859int fc_lport_reset(struct fc_lport *lport)
860{
Steve Maf7db2c152009-02-27 10:55:13 -0800861 cancel_delayed_work_sync(&lport->retry_work);
Robert Love42e9a922008-12-09 15:10:17 -0800862 mutex_lock(&lport->lp_mutex);
863 fc_lport_enter_reset(lport);
864 mutex_unlock(&lport->lp_mutex);
865 return 0;
866}
867EXPORT_SYMBOL(fc_lport_reset);
868
869/**
Joe Eykholt1190d922009-07-29 17:04:27 -0700870 * fc_lport_reset_locked() - Reset the local port
Robert Love42e9a922008-12-09 15:10:17 -0800871 * @lport: Fibre Channel local port to be reset
872 *
873 * Locking Note: The lport lock is expected to be held before calling
874 * this routine.
875 */
Joe Eykholt1190d922009-07-29 17:04:27 -0700876static void fc_lport_reset_locked(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -0800877{
Robert Love42e9a922008-12-09 15:10:17 -0800878 if (lport->dns_rp)
879 lport->tt.rport_logoff(lport->dns_rp);
880
Joe Eykholt48f00902009-08-25 14:01:50 -0700881 lport->ptp_rp = NULL;
Robert Love42e9a922008-12-09 15:10:17 -0800882
883 lport->tt.disc_stop(lport);
884
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -0800885 lport->tt.exch_mgr_reset(lport, 0, 0);
Robert Love42e9a922008-12-09 15:10:17 -0800886 fc_host_fabric_name(lport->host) = 0;
887 fc_host_port_id(lport->host) = 0;
Joe Eykholt1190d922009-07-29 17:04:27 -0700888}
Robert Love42e9a922008-12-09 15:10:17 -0800889
Joe Eykholt1190d922009-07-29 17:04:27 -0700890/**
891 * fc_lport_enter_reset() - Reset the local port
892 * @lport: Fibre Channel local port to be reset
893 *
894 * Locking Note: The lport lock is expected to be held before calling
895 * this routine.
896 */
897static void fc_lport_enter_reset(struct fc_lport *lport)
898{
899 FC_LPORT_DBG(lport, "Entered RESET state from %s state\n",
900 fc_lport_state(lport));
901
Chris Leech8faecdd2009-11-03 11:46:19 -0800902 if (lport->vport) {
903 if (lport->link_up)
904 fc_vport_set_state(lport->vport, FC_VPORT_INITIALIZING);
905 else
906 fc_vport_set_state(lport->vport, FC_VPORT_LINKDOWN);
907 }
Joe Eykholt1190d922009-07-29 17:04:27 -0700908 fc_lport_state_enter(lport, LPORT_ST_RESET);
Chris Leech8faecdd2009-11-03 11:46:19 -0800909 fc_vports_linkchange(lport);
Joe Eykholt1190d922009-07-29 17:04:27 -0700910 fc_lport_reset_locked(lport);
Vasu Devbc0e17f2009-02-27 10:54:57 -0800911 if (lport->link_up)
Robert Love42e9a922008-12-09 15:10:17 -0800912 fc_lport_enter_flogi(lport);
913}
914
915/**
Joe Eykholt1190d922009-07-29 17:04:27 -0700916 * fc_lport_enter_disabled() - disable the local port
917 * @lport: Fibre Channel local port to be reset
918 *
919 * Locking Note: The lport lock is expected to be held before calling
920 * this routine.
921 */
922static void fc_lport_enter_disabled(struct fc_lport *lport)
923{
924 FC_LPORT_DBG(lport, "Entered disabled state from %s state\n",
925 fc_lport_state(lport));
926
927 fc_lport_state_enter(lport, LPORT_ST_DISABLED);
Chris Leech8faecdd2009-11-03 11:46:19 -0800928 fc_vports_linkchange(lport);
Joe Eykholt1190d922009-07-29 17:04:27 -0700929 fc_lport_reset_locked(lport);
930}
931
932/**
Robert Love34f42a02009-02-27 10:55:45 -0800933 * fc_lport_error() - Handler for any errors
Robert Love42e9a922008-12-09 15:10:17 -0800934 * @lport: The fc_lport object
935 * @fp: The frame pointer
936 *
937 * If the error was caused by a resource allocation failure
938 * then wait for half a second and retry, otherwise retry
939 * after the e_d_tov time.
940 */
941static void fc_lport_error(struct fc_lport *lport, struct fc_frame *fp)
942{
943 unsigned long delay = 0;
Robert Love74147052009-06-10 15:31:10 -0700944 FC_LPORT_DBG(lport, "Error %ld in state %s, retries %d\n",
945 PTR_ERR(fp), fc_lport_state(lport),
946 lport->retry_count);
Robert Love42e9a922008-12-09 15:10:17 -0800947
948 if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
949 /*
950 * Memory allocation failure, or the exchange timed out.
951 * Retry after delay
952 */
953 if (lport->retry_count < lport->max_retry_count) {
954 lport->retry_count++;
955 if (!fp)
956 delay = msecs_to_jiffies(500);
957 else
958 delay = msecs_to_jiffies(lport->e_d_tov);
959
960 schedule_delayed_work(&lport->retry_work, delay);
961 } else {
962 switch (lport->state) {
Joe Eykholtb1d9fd52009-07-29 17:04:22 -0700963 case LPORT_ST_DISABLED:
Robert Love42e9a922008-12-09 15:10:17 -0800964 case LPORT_ST_READY:
965 case LPORT_ST_RESET:
Robert Love42e9a922008-12-09 15:10:17 -0800966 case LPORT_ST_RFT_ID:
967 case LPORT_ST_SCR:
968 case LPORT_ST_DNS:
969 case LPORT_ST_FLOGI:
970 case LPORT_ST_LOGO:
971 fc_lport_enter_reset(lport);
972 break;
973 }
974 }
975 }
976}
977
978/**
Robert Love34f42a02009-02-27 10:55:45 -0800979 * fc_lport_rft_id_resp() - Handle response to Register Fibre
Chris Leech28cc0e32009-11-03 11:46:46 -0800980 * Channel Types by ID (RFT_ID) request
981 * @sp: current sequence in RFT_ID exchange
Robert Love42e9a922008-12-09 15:10:17 -0800982 * @fp: response frame
983 * @lp_arg: Fibre Channel host port instance
984 *
985 * Locking Note: This function will be called without the lport lock
986 * held, but it will lock, call an _enter_* function or fc_lport_error
987 * and then unlock the lport.
988 */
989static void fc_lport_rft_id_resp(struct fc_seq *sp, struct fc_frame *fp,
990 void *lp_arg)
991{
992 struct fc_lport *lport = lp_arg;
993 struct fc_frame_header *fh;
994 struct fc_ct_hdr *ct;
995
Joe Eykholtf657d292009-08-25 14:03:21 -0700996 FC_LPORT_DBG(lport, "Received a RFT_ID %s\n", fc_els_resp_type(fp));
997
Robert Love42e9a922008-12-09 15:10:17 -0800998 if (fp == ERR_PTR(-FC_EX_CLOSED))
999 return;
1000
1001 mutex_lock(&lport->lp_mutex);
1002
Robert Love42e9a922008-12-09 15:10:17 -08001003 if (lport->state != LPORT_ST_RFT_ID) {
Robert Love74147052009-06-10 15:31:10 -07001004 FC_LPORT_DBG(lport, "Received a RFT_ID response, but in state "
1005 "%s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001006 if (IS_ERR(fp))
1007 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001008 goto out;
1009 }
1010
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001011 if (IS_ERR(fp)) {
1012 fc_lport_error(lport, fp);
1013 goto err;
1014 }
1015
Robert Love42e9a922008-12-09 15:10:17 -08001016 fh = fc_frame_header_get(fp);
1017 ct = fc_frame_payload_get(fp, sizeof(*ct));
1018
1019 if (fh && ct && fh->fh_type == FC_TYPE_CT &&
1020 ct->ct_fs_type == FC_FST_DIR &&
1021 ct->ct_fs_subtype == FC_NS_SUBTYPE &&
1022 ntohs(ct->ct_cmd) == FC_FS_ACC)
1023 fc_lport_enter_scr(lport);
1024 else
1025 fc_lport_error(lport, fp);
1026out:
1027 fc_frame_free(fp);
1028err:
1029 mutex_unlock(&lport->lp_mutex);
1030}
1031
1032/**
Robert Love34f42a02009-02-27 10:55:45 -08001033 * fc_lport_scr_resp() - Handle response to State Change Register (SCR) request
Robert Love42e9a922008-12-09 15:10:17 -08001034 * @sp: current sequence in SCR exchange
1035 * @fp: response frame
1036 * @lp_arg: Fibre Channel lport port instance that sent the registration request
1037 *
1038 * Locking Note: This function will be called without the lport lock
1039 * held, but it will lock, call an _enter_* function or fc_lport_error
1040 * and then unlock the lport.
1041 */
1042static void fc_lport_scr_resp(struct fc_seq *sp, struct fc_frame *fp,
1043 void *lp_arg)
1044{
1045 struct fc_lport *lport = lp_arg;
1046 u8 op;
1047
Joe Eykholtf657d292009-08-25 14:03:21 -07001048 FC_LPORT_DBG(lport, "Received a SCR %s\n", fc_els_resp_type(fp));
1049
Robert Love42e9a922008-12-09 15:10:17 -08001050 if (fp == ERR_PTR(-FC_EX_CLOSED))
1051 return;
1052
1053 mutex_lock(&lport->lp_mutex);
1054
Robert Love42e9a922008-12-09 15:10:17 -08001055 if (lport->state != LPORT_ST_SCR) {
Robert Love74147052009-06-10 15:31:10 -07001056 FC_LPORT_DBG(lport, "Received a SCR response, but in state "
1057 "%s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001058 if (IS_ERR(fp))
1059 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001060 goto out;
1061 }
1062
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001063 if (IS_ERR(fp)) {
1064 fc_lport_error(lport, fp);
1065 goto err;
1066 }
1067
Robert Love42e9a922008-12-09 15:10:17 -08001068 op = fc_frame_payload_op(fp);
1069 if (op == ELS_LS_ACC)
1070 fc_lport_enter_ready(lport);
1071 else
1072 fc_lport_error(lport, fp);
1073
1074out:
1075 fc_frame_free(fp);
1076err:
1077 mutex_unlock(&lport->lp_mutex);
1078}
1079
1080/**
Robert Love34f42a02009-02-27 10:55:45 -08001081 * fc_lport_enter_scr() - Send a State Change Register (SCR) request
Robert Love42e9a922008-12-09 15:10:17 -08001082 * @lport: Fibre Channel local port to register for state changes
1083 *
1084 * Locking Note: The lport lock is expected to be held before calling
1085 * this routine.
1086 */
1087static void fc_lport_enter_scr(struct fc_lport *lport)
1088{
1089 struct fc_frame *fp;
1090
Robert Love74147052009-06-10 15:31:10 -07001091 FC_LPORT_DBG(lport, "Entered SCR state from %s state\n",
1092 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001093
1094 fc_lport_state_enter(lport, LPORT_ST_SCR);
1095
1096 fp = fc_frame_alloc(lport, sizeof(struct fc_els_scr));
1097 if (!fp) {
1098 fc_lport_error(lport, fp);
1099 return;
1100 }
1101
Joe Eykholta46f3272009-08-25 14:00:55 -07001102 if (!lport->tt.elsct_send(lport, FC_FID_FCTRL, fp, ELS_SCR,
Robert Love42e9a922008-12-09 15:10:17 -08001103 fc_lport_scr_resp, lport, lport->e_d_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001104 fc_lport_error(lport, NULL);
Robert Love42e9a922008-12-09 15:10:17 -08001105}
1106
1107/**
Robert Love34f42a02009-02-27 10:55:45 -08001108 * fc_lport_enter_rft_id() - Register FC4-types with the name server
Robert Love42e9a922008-12-09 15:10:17 -08001109 * @lport: Fibre Channel local port to register
1110 *
1111 * Locking Note: The lport lock is expected to be held before calling
1112 * this routine.
1113 */
1114static void fc_lport_enter_rft_id(struct fc_lport *lport)
1115{
1116 struct fc_frame *fp;
1117 struct fc_ns_fts *lps;
1118 int i;
1119
Robert Love74147052009-06-10 15:31:10 -07001120 FC_LPORT_DBG(lport, "Entered RFT_ID state from %s state\n",
1121 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001122
1123 fc_lport_state_enter(lport, LPORT_ST_RFT_ID);
1124
1125 lps = &lport->fcts;
1126 i = sizeof(lps->ff_type_map) / sizeof(lps->ff_type_map[0]);
1127 while (--i >= 0)
1128 if (ntohl(lps->ff_type_map[i]) != 0)
1129 break;
1130 if (i < 0) {
1131 /* nothing to register, move on to SCR */
1132 fc_lport_enter_scr(lport);
1133 return;
1134 }
1135
1136 fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
1137 sizeof(struct fc_ns_rft));
1138 if (!fp) {
1139 fc_lport_error(lport, fp);
1140 return;
1141 }
1142
Joe Eykholta46f3272009-08-25 14:00:55 -07001143 if (!lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RFT_ID,
Robert Love42e9a922008-12-09 15:10:17 -08001144 fc_lport_rft_id_resp,
1145 lport, lport->e_d_tov))
1146 fc_lport_error(lport, fp);
1147}
1148
Robert Love42e9a922008-12-09 15:10:17 -08001149static struct fc_rport_operations fc_lport_rport_ops = {
1150 .event_callback = fc_lport_rport_callback,
1151};
1152
1153/**
Robert Love34f42a02009-02-27 10:55:45 -08001154 * fc_rport_enter_dns() - Create a rport to the name server
Robert Love42e9a922008-12-09 15:10:17 -08001155 * @lport: Fibre Channel local port requesting a rport for the name server
1156 *
1157 * Locking Note: The lport lock is expected to be held before calling
1158 * this routine.
1159 */
1160static void fc_lport_enter_dns(struct fc_lport *lport)
1161{
Joe Eykholtab28f1f2009-08-25 14:00:34 -07001162 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -08001163
Robert Love74147052009-06-10 15:31:10 -07001164 FC_LPORT_DBG(lport, "Entered DNS state from %s state\n",
1165 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001166
1167 fc_lport_state_enter(lport, LPORT_ST_DNS);
1168
Joe Eykholt48f00902009-08-25 14:01:50 -07001169 mutex_lock(&lport->disc.disc_mutex);
Robert Love9737e6a2009-08-25 14:02:59 -07001170 rdata = lport->tt.rport_create(lport, FC_FID_DIR_SERV);
Joe Eykholt48f00902009-08-25 14:01:50 -07001171 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001172 if (!rdata)
Robert Love42e9a922008-12-09 15:10:17 -08001173 goto err;
1174
Robert Love42e9a922008-12-09 15:10:17 -08001175 rdata->ops = &fc_lport_rport_ops;
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001176 lport->tt.rport_login(rdata);
Robert Love42e9a922008-12-09 15:10:17 -08001177 return;
1178
1179err:
1180 fc_lport_error(lport, NULL);
1181}
1182
1183/**
Robert Love34f42a02009-02-27 10:55:45 -08001184 * fc_lport_timeout() - Handler for the retry_work timer.
Robert Love42e9a922008-12-09 15:10:17 -08001185 * @work: The work struct of the fc_lport
1186 */
1187static void fc_lport_timeout(struct work_struct *work)
1188{
1189 struct fc_lport *lport =
1190 container_of(work, struct fc_lport,
1191 retry_work.work);
1192
1193 mutex_lock(&lport->lp_mutex);
1194
1195 switch (lport->state) {
Joe Eykholtb1d9fd52009-07-29 17:04:22 -07001196 case LPORT_ST_DISABLED:
Robert Love42e9a922008-12-09 15:10:17 -08001197 WARN_ON(1);
1198 break;
Joe Eykholt22655ac2009-10-21 16:27:22 -07001199 case LPORT_ST_READY:
1200 WARN_ON(1);
1201 break;
1202 case LPORT_ST_RESET:
1203 break;
Robert Love42e9a922008-12-09 15:10:17 -08001204 case LPORT_ST_FLOGI:
1205 fc_lport_enter_flogi(lport);
1206 break;
1207 case LPORT_ST_DNS:
1208 fc_lport_enter_dns(lport);
1209 break;
Robert Love42e9a922008-12-09 15:10:17 -08001210 case LPORT_ST_RFT_ID:
1211 fc_lport_enter_rft_id(lport);
1212 break;
1213 case LPORT_ST_SCR:
1214 fc_lport_enter_scr(lport);
1215 break;
1216 case LPORT_ST_LOGO:
1217 fc_lport_enter_logo(lport);
1218 break;
1219 }
1220
1221 mutex_unlock(&lport->lp_mutex);
1222}
1223
1224/**
Robert Love34f42a02009-02-27 10:55:45 -08001225 * fc_lport_logo_resp() - Handle response to LOGO request
Robert Love42e9a922008-12-09 15:10:17 -08001226 * @sp: current sequence in LOGO exchange
1227 * @fp: response frame
1228 * @lp_arg: Fibre Channel lport port instance that sent the LOGO request
1229 *
1230 * Locking Note: This function will be called without the lport lock
1231 * held, but it will lock, call an _enter_* function or fc_lport_error
1232 * and then unlock the lport.
1233 */
Chris Leech11b56182009-11-03 11:46:29 -08001234void fc_lport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
Robert Love42e9a922008-12-09 15:10:17 -08001235 void *lp_arg)
1236{
1237 struct fc_lport *lport = lp_arg;
1238 u8 op;
1239
Joe Eykholtf657d292009-08-25 14:03:21 -07001240 FC_LPORT_DBG(lport, "Received a LOGO %s\n", fc_els_resp_type(fp));
1241
Robert Love42e9a922008-12-09 15:10:17 -08001242 if (fp == ERR_PTR(-FC_EX_CLOSED))
1243 return;
1244
1245 mutex_lock(&lport->lp_mutex);
1246
Robert Love42e9a922008-12-09 15:10:17 -08001247 if (lport->state != LPORT_ST_LOGO) {
Robert Love74147052009-06-10 15:31:10 -07001248 FC_LPORT_DBG(lport, "Received a LOGO response, but in state "
1249 "%s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001250 if (IS_ERR(fp))
1251 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001252 goto out;
1253 }
1254
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001255 if (IS_ERR(fp)) {
1256 fc_lport_error(lport, fp);
1257 goto err;
1258 }
1259
Robert Love42e9a922008-12-09 15:10:17 -08001260 op = fc_frame_payload_op(fp);
1261 if (op == ELS_LS_ACC)
Joe Eykholt1190d922009-07-29 17:04:27 -07001262 fc_lport_enter_disabled(lport);
Robert Love42e9a922008-12-09 15:10:17 -08001263 else
1264 fc_lport_error(lport, fp);
1265
1266out:
1267 fc_frame_free(fp);
1268err:
1269 mutex_unlock(&lport->lp_mutex);
1270}
Chris Leech11b56182009-11-03 11:46:29 -08001271EXPORT_SYMBOL(fc_lport_logo_resp);
Robert Love42e9a922008-12-09 15:10:17 -08001272
1273/**
Robert Love34f42a02009-02-27 10:55:45 -08001274 * fc_rport_enter_logo() - Logout of the fabric
Robert Love42e9a922008-12-09 15:10:17 -08001275 * @lport: Fibre Channel local port to be logged out
1276 *
1277 * Locking Note: The lport lock is expected to be held before calling
1278 * this routine.
1279 */
1280static void fc_lport_enter_logo(struct fc_lport *lport)
1281{
1282 struct fc_frame *fp;
1283 struct fc_els_logo *logo;
1284
Robert Love74147052009-06-10 15:31:10 -07001285 FC_LPORT_DBG(lport, "Entered LOGO state from %s state\n",
1286 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001287
1288 fc_lport_state_enter(lport, LPORT_ST_LOGO);
Chris Leech8faecdd2009-11-03 11:46:19 -08001289 fc_vports_linkchange(lport);
Robert Love42e9a922008-12-09 15:10:17 -08001290
Robert Love42e9a922008-12-09 15:10:17 -08001291 fp = fc_frame_alloc(lport, sizeof(*logo));
1292 if (!fp) {
1293 fc_lport_error(lport, fp);
1294 return;
1295 }
1296
Joe Eykholta46f3272009-08-25 14:00:55 -07001297 if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp, ELS_LOGO,
1298 fc_lport_logo_resp, lport, lport->e_d_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001299 fc_lport_error(lport, NULL);
Robert Love42e9a922008-12-09 15:10:17 -08001300}
1301
1302/**
Robert Love34f42a02009-02-27 10:55:45 -08001303 * fc_lport_flogi_resp() - Handle response to FLOGI request
Robert Love42e9a922008-12-09 15:10:17 -08001304 * @sp: current sequence in FLOGI exchange
1305 * @fp: response frame
1306 * @lp_arg: Fibre Channel lport port instance that sent the FLOGI request
1307 *
1308 * Locking Note: This function will be called without the lport lock
1309 * held, but it will lock, call an _enter_* function or fc_lport_error
1310 * and then unlock the lport.
1311 */
Chris Leech11b56182009-11-03 11:46:29 -08001312void fc_lport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
Robert Love42e9a922008-12-09 15:10:17 -08001313 void *lp_arg)
1314{
1315 struct fc_lport *lport = lp_arg;
1316 struct fc_frame_header *fh;
1317 struct fc_els_flogi *flp;
1318 u32 did;
1319 u16 csp_flags;
1320 unsigned int r_a_tov;
1321 unsigned int e_d_tov;
1322 u16 mfs;
1323
Joe Eykholtf657d292009-08-25 14:03:21 -07001324 FC_LPORT_DBG(lport, "Received a FLOGI %s\n", fc_els_resp_type(fp));
1325
Robert Love42e9a922008-12-09 15:10:17 -08001326 if (fp == ERR_PTR(-FC_EX_CLOSED))
1327 return;
1328
1329 mutex_lock(&lport->lp_mutex);
1330
Robert Love42e9a922008-12-09 15:10:17 -08001331 if (lport->state != LPORT_ST_FLOGI) {
Robert Love74147052009-06-10 15:31:10 -07001332 FC_LPORT_DBG(lport, "Received a FLOGI response, but in state "
1333 "%s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001334 if (IS_ERR(fp))
1335 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001336 goto out;
1337 }
1338
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001339 if (IS_ERR(fp)) {
1340 fc_lport_error(lport, fp);
1341 goto err;
1342 }
1343
Robert Love42e9a922008-12-09 15:10:17 -08001344 fh = fc_frame_header_get(fp);
1345 did = ntoh24(fh->fh_d_id);
1346 if (fc_frame_payload_op(fp) == ELS_LS_ACC && did != 0) {
1347
Robert Love74147052009-06-10 15:31:10 -07001348 printk(KERN_INFO "libfc: Assigned FID (%6x) in FLOGI response\n",
1349 did);
Robert Love42e9a922008-12-09 15:10:17 -08001350 fc_host_port_id(lport->host) = did;
1351
1352 flp = fc_frame_payload_get(fp, sizeof(*flp));
1353 if (flp) {
1354 mfs = ntohs(flp->fl_csp.sp_bb_data) &
1355 FC_SP_BB_DATA_MASK;
1356 if (mfs >= FC_SP_MIN_MAX_PAYLOAD &&
1357 mfs < lport->mfs)
1358 lport->mfs = mfs;
1359 csp_flags = ntohs(flp->fl_csp.sp_features);
1360 r_a_tov = ntohl(flp->fl_csp.sp_r_a_tov);
1361 e_d_tov = ntohl(flp->fl_csp.sp_e_d_tov);
1362 if (csp_flags & FC_SP_FT_EDTR)
1363 e_d_tov /= 1000000;
Chris Leechdb36c062009-11-03 11:46:24 -08001364
1365 lport->npiv_enabled = !!(csp_flags & FC_SP_FT_NPIV_ACC);
1366
Robert Love42e9a922008-12-09 15:10:17 -08001367 if ((csp_flags & FC_SP_FT_FPORT) == 0) {
1368 if (e_d_tov > lport->e_d_tov)
1369 lport->e_d_tov = e_d_tov;
1370 lport->r_a_tov = 2 * e_d_tov;
Robert Love74147052009-06-10 15:31:10 -07001371 printk(KERN_INFO "libfc: Port (%6x) entered "
1372 "point to point mode\n", did);
Robert Love42e9a922008-12-09 15:10:17 -08001373 fc_lport_ptp_setup(lport, ntoh24(fh->fh_s_id),
1374 get_unaligned_be64(
1375 &flp->fl_wwpn),
1376 get_unaligned_be64(
1377 &flp->fl_wwnn));
1378 } else {
1379 lport->e_d_tov = e_d_tov;
1380 lport->r_a_tov = r_a_tov;
1381 fc_host_fabric_name(lport->host) =
1382 get_unaligned_be64(&flp->fl_wwnn);
1383 fc_lport_enter_dns(lport);
1384 }
1385 }
Robert Love42e9a922008-12-09 15:10:17 -08001386 } else {
Robert Love74147052009-06-10 15:31:10 -07001387 FC_LPORT_DBG(lport, "Bad FLOGI response\n");
Robert Love42e9a922008-12-09 15:10:17 -08001388 }
1389
1390out:
1391 fc_frame_free(fp);
1392err:
1393 mutex_unlock(&lport->lp_mutex);
1394}
Chris Leech11b56182009-11-03 11:46:29 -08001395EXPORT_SYMBOL(fc_lport_flogi_resp);
Robert Love42e9a922008-12-09 15:10:17 -08001396
1397/**
Robert Love34f42a02009-02-27 10:55:45 -08001398 * fc_rport_enter_flogi() - Send a FLOGI request to the fabric manager
Robert Love42e9a922008-12-09 15:10:17 -08001399 * @lport: Fibre Channel local port to be logged in to the fabric
1400 *
1401 * Locking Note: The lport lock is expected to be held before calling
1402 * this routine.
1403 */
1404void fc_lport_enter_flogi(struct fc_lport *lport)
1405{
1406 struct fc_frame *fp;
1407
Robert Love74147052009-06-10 15:31:10 -07001408 FC_LPORT_DBG(lport, "Entered FLOGI state from %s state\n",
1409 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001410
1411 fc_lport_state_enter(lport, LPORT_ST_FLOGI);
1412
1413 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
1414 if (!fp)
1415 return fc_lport_error(lport, fp);
1416
Chris Leechdb36c062009-11-03 11:46:24 -08001417 if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp,
1418 lport->vport ? ELS_FDISC : ELS_FLOGI,
Robert Love42e9a922008-12-09 15:10:17 -08001419 fc_lport_flogi_resp, lport, lport->e_d_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001420 fc_lport_error(lport, NULL);
Robert Love42e9a922008-12-09 15:10:17 -08001421}
1422
1423/* Configure a fc_lport */
1424int fc_lport_config(struct fc_lport *lport)
1425{
1426 INIT_DELAYED_WORK(&lport->retry_work, fc_lport_timeout);
1427 mutex_init(&lport->lp_mutex);
1428
Joe Eykholtb1d9fd52009-07-29 17:04:22 -07001429 fc_lport_state_enter(lport, LPORT_ST_DISABLED);
Robert Love42e9a922008-12-09 15:10:17 -08001430
1431 fc_lport_add_fc4_type(lport, FC_TYPE_FCP);
1432 fc_lport_add_fc4_type(lport, FC_TYPE_CT);
1433
1434 return 0;
1435}
1436EXPORT_SYMBOL(fc_lport_config);
1437
1438int fc_lport_init(struct fc_lport *lport)
1439{
1440 if (!lport->tt.lport_recv)
1441 lport->tt.lport_recv = fc_lport_recv_req;
1442
1443 if (!lport->tt.lport_reset)
1444 lport->tt.lport_reset = fc_lport_reset;
1445
1446 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
1447 fc_host_node_name(lport->host) = lport->wwnn;
1448 fc_host_port_name(lport->host) = lport->wwpn;
1449 fc_host_supported_classes(lport->host) = FC_COS_CLASS3;
1450 memset(fc_host_supported_fc4s(lport->host), 0,
1451 sizeof(fc_host_supported_fc4s(lport->host)));
1452 fc_host_supported_fc4s(lport->host)[2] = 1;
1453 fc_host_supported_fc4s(lport->host)[7] = 1;
1454
1455 /* This value is also unchanging */
1456 memset(fc_host_active_fc4s(lport->host), 0,
1457 sizeof(fc_host_active_fc4s(lport->host)));
1458 fc_host_active_fc4s(lport->host)[2] = 1;
1459 fc_host_active_fc4s(lport->host)[7] = 1;
1460 fc_host_maxframe_size(lport->host) = lport->mfs;
1461 fc_host_supported_speeds(lport->host) = 0;
1462 if (lport->link_supported_speeds & FC_PORTSPEED_1GBIT)
1463 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_1GBIT;
1464 if (lport->link_supported_speeds & FC_PORTSPEED_10GBIT)
1465 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_10GBIT;
1466
Robert Love42e9a922008-12-09 15:10:17 -08001467 return 0;
1468}
1469EXPORT_SYMBOL(fc_lport_init);