blob: cc389c03f698303d4b07733d898a1d2219682e8b [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 *);
Chris Leechc9c7bd72009-11-03 11:46:51 -0800111static void fc_lport_enter_rnn_id(struct fc_lport *);
Chris Leech5baa17c2009-11-03 11:46:56 -0800112static void fc_lport_enter_rsnn_nn(struct fc_lport *);
Robert Love42e9a922008-12-09 15:10:17 -0800113static void fc_lport_enter_rft_id(struct fc_lport *);
114static void fc_lport_enter_scr(struct fc_lport *);
115static void fc_lport_enter_ready(struct fc_lport *);
116static void fc_lport_enter_logo(struct fc_lport *);
117
118static const char *fc_lport_state_names[] = {
Joe Eykholtb1d9fd52009-07-29 17:04:22 -0700119 [LPORT_ST_DISABLED] = "disabled",
Robert Love42e9a922008-12-09 15:10:17 -0800120 [LPORT_ST_FLOGI] = "FLOGI",
121 [LPORT_ST_DNS] = "dNS",
Chris Leechc9c7bd72009-11-03 11:46:51 -0800122 [LPORT_ST_RNN_ID] = "RNN_ID",
Chris Leech5baa17c2009-11-03 11:46:56 -0800123 [LPORT_ST_RSNN_NN] = "RSNN_NN",
Robert Love42e9a922008-12-09 15:10:17 -0800124 [LPORT_ST_RFT_ID] = "RFT_ID",
125 [LPORT_ST_SCR] = "SCR",
126 [LPORT_ST_READY] = "Ready",
127 [LPORT_ST_LOGO] = "LOGO",
128 [LPORT_ST_RESET] = "reset",
129};
130
131static int fc_frame_drop(struct fc_lport *lport, struct fc_frame *fp)
132{
133 fc_frame_free(fp);
134 return 0;
135}
136
137/**
Robert Love34f42a02009-02-27 10:55:45 -0800138 * fc_lport_rport_callback() - Event handler for rport events
Robert Love42e9a922008-12-09 15:10:17 -0800139 * @lport: The lport which is receiving the event
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700140 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800141 * @event: The event that occured
142 *
143 * Locking Note: The rport lock should not be held when calling
144 * this function.
145 */
146static void fc_lport_rport_callback(struct fc_lport *lport,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700147 struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -0800148 enum fc_rport_event event)
149{
Robert Love74147052009-06-10 15:31:10 -0700150 FC_LPORT_DBG(lport, "Received a %d event for port (%6x)\n", event,
Joe Eykholtf211fa52009-08-25 14:01:01 -0700151 rdata->ids.port_id);
Robert Love42e9a922008-12-09 15:10:17 -0800152
Joe Eykholtb5cbf082009-08-25 14:01:44 -0700153 mutex_lock(&lport->lp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800154 switch (event) {
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700155 case RPORT_EV_READY:
Joe Eykholtb5cbf082009-08-25 14:01:44 -0700156 if (lport->state == LPORT_ST_DNS) {
157 lport->dns_rp = rdata;
Chris Leechc9c7bd72009-11-03 11:46:51 -0800158 fc_lport_enter_rnn_id(lport);
Joe Eykholtb5cbf082009-08-25 14:01:44 -0700159 } else {
160 FC_LPORT_DBG(lport, "Received an READY event "
161 "on port (%6x) for the directory "
162 "server, but the lport is not "
163 "in the DNS state, it's in the "
164 "%d state", rdata->ids.port_id,
165 lport->state);
166 lport->tt.rport_logoff(rdata);
167 }
Robert Love42e9a922008-12-09 15:10:17 -0800168 break;
169 case RPORT_EV_LOGO:
170 case RPORT_EV_FAILED:
171 case RPORT_EV_STOP:
Joe Eykholtb5cbf082009-08-25 14:01:44 -0700172 lport->dns_rp = NULL;
Robert Love42e9a922008-12-09 15:10:17 -0800173 break;
174 case RPORT_EV_NONE:
175 break;
176 }
Joe Eykholtb5cbf082009-08-25 14:01:44 -0700177 mutex_unlock(&lport->lp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800178}
179
180/**
Robert Love34f42a02009-02-27 10:55:45 -0800181 * fc_lport_state() - Return a string which represents the lport's state
Robert Love42e9a922008-12-09 15:10:17 -0800182 * @lport: The lport whose state is to converted to a string
183 */
184static const char *fc_lport_state(struct fc_lport *lport)
185{
186 const char *cp;
187
188 cp = fc_lport_state_names[lport->state];
189 if (!cp)
190 cp = "unknown";
191 return cp;
192}
193
194/**
Robert Love34f42a02009-02-27 10:55:45 -0800195 * fc_lport_ptp_setup() - Create an rport for point-to-point mode
Robert Love42e9a922008-12-09 15:10:17 -0800196 * @lport: The lport to attach the ptp rport to
197 * @fid: The FID of the ptp rport
198 * @remote_wwpn: The WWPN of the ptp rport
199 * @remote_wwnn: The WWNN of the ptp rport
200 */
201static void fc_lport_ptp_setup(struct fc_lport *lport,
202 u32 remote_fid, u64 remote_wwpn,
203 u64 remote_wwnn)
204{
Joe Eykholt48f00902009-08-25 14:01:50 -0700205 mutex_lock(&lport->disc.disc_mutex);
206 if (lport->ptp_rp)
Robert Love42e9a922008-12-09 15:10:17 -0800207 lport->tt.rport_logoff(lport->ptp_rp);
Robert Love9737e6a2009-08-25 14:02:59 -0700208 lport->ptp_rp = lport->tt.rport_create(lport, remote_fid);
209 lport->ptp_rp->ids.port_name = remote_wwpn;
210 lport->ptp_rp->ids.node_name = remote_wwnn;
Joe Eykholt48f00902009-08-25 14:01:50 -0700211 mutex_unlock(&lport->disc.disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800212
213 lport->tt.rport_login(lport->ptp_rp);
214
215 fc_lport_enter_ready(lport);
216}
217
218void fc_get_host_port_type(struct Scsi_Host *shost)
219{
220 /* TODO - currently just NPORT */
221 fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
222}
223EXPORT_SYMBOL(fc_get_host_port_type);
224
225void fc_get_host_port_state(struct Scsi_Host *shost)
226{
227 struct fc_lport *lp = shost_priv(shost);
228
Chris Leech8faecdd2009-11-03 11:46:19 -0800229 mutex_lock(&lp->lp_mutex);
230 if (!lp->link_up)
231 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
Robert Love42e9a922008-12-09 15:10:17 -0800232 else
Chris Leech8faecdd2009-11-03 11:46:19 -0800233 switch (lp->state) {
234 case LPORT_ST_READY:
235 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
236 break;
237 default:
238 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
239 }
240 mutex_unlock(&lp->lp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800241}
242EXPORT_SYMBOL(fc_get_host_port_state);
243
244void fc_get_host_speed(struct Scsi_Host *shost)
245{
246 struct fc_lport *lport = shost_priv(shost);
247
248 fc_host_speed(shost) = lport->link_speed;
249}
250EXPORT_SYMBOL(fc_get_host_speed);
251
252struct fc_host_statistics *fc_get_host_stats(struct Scsi_Host *shost)
253{
Robert Love42e9a922008-12-09 15:10:17 -0800254 struct fc_host_statistics *fcoe_stats;
255 struct fc_lport *lp = shost_priv(shost);
256 struct timespec v0, v1;
Robert Love582b45b2009-03-31 15:51:50 -0700257 unsigned int cpu;
Robert Love42e9a922008-12-09 15:10:17 -0800258
259 fcoe_stats = &lp->host_stats;
260 memset(fcoe_stats, 0, sizeof(struct fc_host_statistics));
261
262 jiffies_to_timespec(jiffies, &v0);
263 jiffies_to_timespec(lp->boot_time, &v1);
264 fcoe_stats->seconds_since_last_reset = (v0.tv_sec - v1.tv_sec);
265
Robert Love582b45b2009-03-31 15:51:50 -0700266 for_each_possible_cpu(cpu) {
267 struct fcoe_dev_stats *stats;
268
269 stats = per_cpu_ptr(lp->dev_stats, cpu);
270
Robert Love42e9a922008-12-09 15:10:17 -0800271 fcoe_stats->tx_frames += stats->TxFrames;
272 fcoe_stats->tx_words += stats->TxWords;
273 fcoe_stats->rx_frames += stats->RxFrames;
274 fcoe_stats->rx_words += stats->RxWords;
275 fcoe_stats->error_frames += stats->ErrorFrames;
276 fcoe_stats->invalid_crc_count += stats->InvalidCRCCount;
277 fcoe_stats->fcp_input_requests += stats->InputRequests;
278 fcoe_stats->fcp_output_requests += stats->OutputRequests;
279 fcoe_stats->fcp_control_requests += stats->ControlRequests;
280 fcoe_stats->fcp_input_megabytes += stats->InputMegabytes;
281 fcoe_stats->fcp_output_megabytes += stats->OutputMegabytes;
282 fcoe_stats->link_failure_count += stats->LinkFailureCount;
283 }
284 fcoe_stats->lip_count = -1;
285 fcoe_stats->nos_count = -1;
286 fcoe_stats->loss_of_sync_count = -1;
287 fcoe_stats->loss_of_signal_count = -1;
288 fcoe_stats->prim_seq_protocol_err_count = -1;
289 fcoe_stats->dumped_frames = -1;
290 return fcoe_stats;
291}
292EXPORT_SYMBOL(fc_get_host_stats);
293
294/*
295 * Fill in FLOGI command for request.
296 */
297static void
298fc_lport_flogi_fill(struct fc_lport *lport, struct fc_els_flogi *flogi,
299 unsigned int op)
300{
301 struct fc_els_csp *sp;
302 struct fc_els_cssp *cp;
303
304 memset(flogi, 0, sizeof(*flogi));
305 flogi->fl_cmd = (u8) op;
306 put_unaligned_be64(lport->wwpn, &flogi->fl_wwpn);
307 put_unaligned_be64(lport->wwnn, &flogi->fl_wwnn);
308 sp = &flogi->fl_csp;
309 sp->sp_hi_ver = 0x20;
310 sp->sp_lo_ver = 0x20;
311 sp->sp_bb_cred = htons(10); /* this gets set by gateway */
312 sp->sp_bb_data = htons((u16) lport->mfs);
313 cp = &flogi->fl_cssp[3 - 1]; /* class 3 parameters */
314 cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
315 if (op != ELS_FLOGI) {
316 sp->sp_features = htons(FC_SP_FT_CIRO);
317 sp->sp_tot_seq = htons(255); /* seq. we accept */
318 sp->sp_rel_off = htons(0x1f);
319 sp->sp_e_d_tov = htonl(lport->e_d_tov);
320
321 cp->cp_rdfs = htons((u16) lport->mfs);
322 cp->cp_con_seq = htons(255);
323 cp->cp_open_seq = 1;
324 }
325}
326
327/*
328 * Add a supported FC-4 type.
329 */
330static void fc_lport_add_fc4_type(struct fc_lport *lport, enum fc_fh_type type)
331{
332 __be32 *mp;
333
334 mp = &lport->fcts.ff_type_map[type / FC_NS_BPW];
335 *mp = htonl(ntohl(*mp) | 1UL << (type % FC_NS_BPW));
336}
337
338/**
Robert Love34f42a02009-02-27 10:55:45 -0800339 * fc_lport_recv_rlir_req() - Handle received Registered Link Incident Report.
Robert Love42e9a922008-12-09 15:10:17 -0800340 * @lport: Fibre Channel local port recieving the RLIR
341 * @sp: current sequence in the RLIR exchange
342 * @fp: RLIR request frame
343 *
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700344 * Locking Note: The lport lock is expected to be held before calling
Robert Love42e9a922008-12-09 15:10:17 -0800345 * this function.
346 */
347static void fc_lport_recv_rlir_req(struct fc_seq *sp, struct fc_frame *fp,
348 struct fc_lport *lport)
349{
Robert Love74147052009-06-10 15:31:10 -0700350 FC_LPORT_DBG(lport, "Received RLIR request while in state %s\n",
351 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800352
353 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
354 fc_frame_free(fp);
355}
356
357/**
Robert Love34f42a02009-02-27 10:55:45 -0800358 * fc_lport_recv_echo_req() - Handle received ECHO request
Robert Love42e9a922008-12-09 15:10:17 -0800359 * @lport: Fibre Channel local port recieving the ECHO
360 * @sp: current sequence in the ECHO exchange
361 * @fp: ECHO request frame
362 *
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700363 * Locking Note: The lport lock is expected to be held before calling
Robert Love42e9a922008-12-09 15:10:17 -0800364 * this function.
365 */
366static void fc_lport_recv_echo_req(struct fc_seq *sp, struct fc_frame *in_fp,
367 struct fc_lport *lport)
368{
369 struct fc_frame *fp;
370 struct fc_exch *ep = fc_seq_exch(sp);
371 unsigned int len;
372 void *pp;
373 void *dp;
374 u32 f_ctl;
375
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700376 FC_LPORT_DBG(lport, "Received ECHO request while in state %s\n",
Robert Love74147052009-06-10 15:31:10 -0700377 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800378
379 len = fr_len(in_fp) - sizeof(struct fc_frame_header);
380 pp = fc_frame_payload_get(in_fp, len);
381
382 if (len < sizeof(__be32))
383 len = sizeof(__be32);
384
385 fp = fc_frame_alloc(lport, len);
386 if (fp) {
387 dp = fc_frame_payload_get(fp, len);
388 memcpy(dp, pp, len);
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700389 *((__be32 *)dp) = htonl(ELS_LS_ACC << 24);
Robert Love42e9a922008-12-09 15:10:17 -0800390 sp = lport->tt.seq_start_next(sp);
391 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
392 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
393 FC_TYPE_ELS, f_ctl, 0);
394 lport->tt.seq_send(lport, sp, fp);
395 }
396 fc_frame_free(in_fp);
397}
398
399/**
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700400 * fc_lport_recv_rnid_req() - Handle received Request Node ID data request
401 * @sp: The sequence in the RNID exchange
402 * @fp: The RNID request frame
403 * @lport: The local port recieving the RNID
Robert Love42e9a922008-12-09 15:10:17 -0800404 *
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700405 * Locking Note: The lport lock is expected to be held before calling
Robert Love42e9a922008-12-09 15:10:17 -0800406 * this function.
407 */
408static void fc_lport_recv_rnid_req(struct fc_seq *sp, struct fc_frame *in_fp,
409 struct fc_lport *lport)
410{
411 struct fc_frame *fp;
412 struct fc_exch *ep = fc_seq_exch(sp);
413 struct fc_els_rnid *req;
414 struct {
415 struct fc_els_rnid_resp rnid;
416 struct fc_els_rnid_cid cid;
417 struct fc_els_rnid_gen gen;
418 } *rp;
419 struct fc_seq_els_data rjt_data;
420 u8 fmt;
421 size_t len;
422 u32 f_ctl;
423
Robert Love74147052009-06-10 15:31:10 -0700424 FC_LPORT_DBG(lport, "Received RNID request while in state %s\n",
425 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800426
427 req = fc_frame_payload_get(in_fp, sizeof(*req));
428 if (!req) {
429 rjt_data.fp = NULL;
430 rjt_data.reason = ELS_RJT_LOGIC;
431 rjt_data.explan = ELS_EXPL_NONE;
432 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
433 } else {
434 fmt = req->rnid_fmt;
435 len = sizeof(*rp);
436 if (fmt != ELS_RNIDF_GEN ||
437 ntohl(lport->rnid_gen.rnid_atype) == 0) {
438 fmt = ELS_RNIDF_NONE; /* nothing to provide */
439 len -= sizeof(rp->gen);
440 }
441 fp = fc_frame_alloc(lport, len);
442 if (fp) {
443 rp = fc_frame_payload_get(fp, len);
444 memset(rp, 0, len);
445 rp->rnid.rnid_cmd = ELS_LS_ACC;
446 rp->rnid.rnid_fmt = fmt;
447 rp->rnid.rnid_cid_len = sizeof(rp->cid);
448 rp->cid.rnid_wwpn = htonll(lport->wwpn);
449 rp->cid.rnid_wwnn = htonll(lport->wwnn);
450 if (fmt == ELS_RNIDF_GEN) {
451 rp->rnid.rnid_sid_len = sizeof(rp->gen);
452 memcpy(&rp->gen, &lport->rnid_gen,
453 sizeof(rp->gen));
454 }
455 sp = lport->tt.seq_start_next(sp);
456 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
457 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
458 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
459 FC_TYPE_ELS, f_ctl, 0);
460 lport->tt.seq_send(lport, sp, fp);
461 }
462 }
463 fc_frame_free(in_fp);
464}
465
466/**
Robert Love34f42a02009-02-27 10:55:45 -0800467 * fc_lport_recv_logo_req() - Handle received fabric LOGO request
Robert Love42e9a922008-12-09 15:10:17 -0800468 * @lport: Fibre Channel local port recieving the LOGO
469 * @sp: current sequence in the LOGO exchange
470 * @fp: LOGO request frame
471 *
472 * Locking Note: The lport lock is exected to be held before calling
473 * this function.
474 */
475static void fc_lport_recv_logo_req(struct fc_seq *sp, struct fc_frame *fp,
476 struct fc_lport *lport)
477{
478 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
479 fc_lport_enter_reset(lport);
480 fc_frame_free(fp);
481}
482
483/**
Robert Love34f42a02009-02-27 10:55:45 -0800484 * fc_fabric_login() - Start the lport state machine
Robert Love42e9a922008-12-09 15:10:17 -0800485 * @lport: The lport that should log into the fabric
486 *
487 * Locking Note: This function should not be called
488 * with the lport lock held.
489 */
490int fc_fabric_login(struct fc_lport *lport)
491{
492 int rc = -1;
493
494 mutex_lock(&lport->lp_mutex);
Joe Eykholtb1d9fd52009-07-29 17:04:22 -0700495 if (lport->state == LPORT_ST_DISABLED) {
Robert Love42e9a922008-12-09 15:10:17 -0800496 fc_lport_enter_reset(lport);
497 rc = 0;
498 }
499 mutex_unlock(&lport->lp_mutex);
500
501 return rc;
502}
503EXPORT_SYMBOL(fc_fabric_login);
504
505/**
Chris Leech8faecdd2009-11-03 11:46:19 -0800506 * __fc_linkup() - Handler for transport linkup events
507 * @lport: The lport whose link is up
508 *
509 * Locking: must be called with the lp_mutex held
510 */
511void __fc_linkup(struct fc_lport *lport)
512{
513 if (!lport->link_up) {
514 lport->link_up = 1;
515
516 if (lport->state == LPORT_ST_RESET)
517 fc_lport_enter_flogi(lport);
518 }
519}
520
521/**
Robert Love34f42a02009-02-27 10:55:45 -0800522 * fc_linkup() - Handler for transport linkup events
Robert Love42e9a922008-12-09 15:10:17 -0800523 * @lport: The lport whose link is up
524 */
525void fc_linkup(struct fc_lport *lport)
526{
Robert Love74147052009-06-10 15:31:10 -0700527 printk(KERN_INFO "libfc: Link up on port (%6x)\n",
528 fc_host_port_id(lport->host));
Robert Love42e9a922008-12-09 15:10:17 -0800529
530 mutex_lock(&lport->lp_mutex);
Chris Leech8faecdd2009-11-03 11:46:19 -0800531 __fc_linkup(lport);
Robert Love42e9a922008-12-09 15:10:17 -0800532 mutex_unlock(&lport->lp_mutex);
533}
534EXPORT_SYMBOL(fc_linkup);
535
536/**
Chris Leech8faecdd2009-11-03 11:46:19 -0800537 * __fc_linkdown() - Handler for transport linkdown events
538 * @lport: The lport whose link is down
539 *
540 * Locking: must be called with the lp_mutex held
541 */
542void __fc_linkdown(struct fc_lport *lport)
543{
544 if (lport->link_up) {
545 lport->link_up = 0;
546 fc_lport_enter_reset(lport);
547 lport->tt.fcp_cleanup(lport);
548 }
549}
550
551/**
Robert Love34f42a02009-02-27 10:55:45 -0800552 * fc_linkdown() - Handler for transport linkdown events
Robert Love42e9a922008-12-09 15:10:17 -0800553 * @lport: The lport whose link is down
554 */
555void fc_linkdown(struct fc_lport *lport)
556{
Robert Love74147052009-06-10 15:31:10 -0700557 printk(KERN_INFO "libfc: Link down on port (%6x)\n",
558 fc_host_port_id(lport->host));
Robert Love42e9a922008-12-09 15:10:17 -0800559
Chris Leech8faecdd2009-11-03 11:46:19 -0800560 mutex_lock(&lport->lp_mutex);
561 __fc_linkdown(lport);
Robert Love42e9a922008-12-09 15:10:17 -0800562 mutex_unlock(&lport->lp_mutex);
563}
564EXPORT_SYMBOL(fc_linkdown);
565
566/**
Robert Love34f42a02009-02-27 10:55:45 -0800567 * fc_fabric_logoff() - Logout of the fabric
Robert Love42e9a922008-12-09 15:10:17 -0800568 * @lport: fc_lport pointer to logoff the fabric
569 *
570 * Return value:
571 * 0 for success, -1 for failure
Robert Love34f42a02009-02-27 10:55:45 -0800572 */
Robert Love42e9a922008-12-09 15:10:17 -0800573int fc_fabric_logoff(struct fc_lport *lport)
574{
575 lport->tt.disc_stop_final(lport);
576 mutex_lock(&lport->lp_mutex);
Abhijeet Joglekara0fd2e42009-04-21 16:27:09 -0700577 if (lport->dns_rp)
578 lport->tt.rport_logoff(lport->dns_rp);
579 mutex_unlock(&lport->lp_mutex);
580 lport->tt.rport_flush_queue();
581 mutex_lock(&lport->lp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800582 fc_lport_enter_logo(lport);
583 mutex_unlock(&lport->lp_mutex);
Steve Maf7db2c152009-02-27 10:55:13 -0800584 cancel_delayed_work_sync(&lport->retry_work);
Robert Love42e9a922008-12-09 15:10:17 -0800585 return 0;
586}
587EXPORT_SYMBOL(fc_fabric_logoff);
588
589/**
Robert Love34f42a02009-02-27 10:55:45 -0800590 * fc_lport_destroy() - unregister a fc_lport
Robert Love42e9a922008-12-09 15:10:17 -0800591 * @lport: fc_lport pointer to unregister
592 *
593 * Return value:
594 * None
595 * Note:
596 * exit routine for fc_lport instance
597 * clean-up all the allocated memory
598 * and free up other system resources.
599 *
Robert Love34f42a02009-02-27 10:55:45 -0800600 */
Robert Love42e9a922008-12-09 15:10:17 -0800601int fc_lport_destroy(struct fc_lport *lport)
602{
Abhijeet Joglekarbbf15662009-04-21 16:27:14 -0700603 mutex_lock(&lport->lp_mutex);
Joe Eykholtb1d9fd52009-07-29 17:04:22 -0700604 lport->state = LPORT_ST_DISABLED;
Abhijeet Joglekarbbf15662009-04-21 16:27:14 -0700605 lport->link_up = 0;
Robert Love42e9a922008-12-09 15:10:17 -0800606 lport->tt.frame_send = fc_frame_drop;
Abhijeet Joglekarbbf15662009-04-21 16:27:14 -0700607 mutex_unlock(&lport->lp_mutex);
608
Robert Love42e9a922008-12-09 15:10:17 -0800609 lport->tt.fcp_abort_io(lport);
Joe Eykholte9ba8b42009-07-29 17:04:33 -0700610 lport->tt.disc_stop_final(lport);
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -0800611 lport->tt.exch_mgr_reset(lport, 0, 0);
Robert Love42e9a922008-12-09 15:10:17 -0800612 return 0;
613}
614EXPORT_SYMBOL(fc_lport_destroy);
615
616/**
Robert Love34f42a02009-02-27 10:55:45 -0800617 * fc_set_mfs() - sets up the mfs for the corresponding fc_lport
Robert Love42e9a922008-12-09 15:10:17 -0800618 * @lport: fc_lport pointer to unregister
619 * @mfs: the new mfs for fc_lport
620 *
621 * Set mfs for the given fc_lport to the new mfs.
622 *
623 * Return: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800624 */
Robert Love42e9a922008-12-09 15:10:17 -0800625int fc_set_mfs(struct fc_lport *lport, u32 mfs)
626{
627 unsigned int old_mfs;
628 int rc = -EINVAL;
629
630 mutex_lock(&lport->lp_mutex);
631
632 old_mfs = lport->mfs;
633
634 if (mfs >= FC_MIN_MAX_FRAME) {
635 mfs &= ~3;
636 if (mfs > FC_MAX_FRAME)
637 mfs = FC_MAX_FRAME;
638 mfs -= sizeof(struct fc_frame_header);
639 lport->mfs = mfs;
640 rc = 0;
641 }
642
643 if (!rc && mfs < old_mfs)
644 fc_lport_enter_reset(lport);
645
646 mutex_unlock(&lport->lp_mutex);
647
648 return rc;
649}
650EXPORT_SYMBOL(fc_set_mfs);
651
652/**
Robert Love34f42a02009-02-27 10:55:45 -0800653 * fc_lport_disc_callback() - Callback for discovery events
Robert Love42e9a922008-12-09 15:10:17 -0800654 * @lport: FC local port
655 * @event: The discovery event
656 */
657void fc_lport_disc_callback(struct fc_lport *lport, enum fc_disc_event event)
658{
659 switch (event) {
660 case DISC_EV_SUCCESS:
Robert Love74147052009-06-10 15:31:10 -0700661 FC_LPORT_DBG(lport, "Discovery succeeded\n");
Robert Love42e9a922008-12-09 15:10:17 -0800662 break;
663 case DISC_EV_FAILED:
Robert Love74147052009-06-10 15:31:10 -0700664 printk(KERN_ERR "libfc: Discovery failed for port (%6x)\n",
665 fc_host_port_id(lport->host));
Robert Love42e9a922008-12-09 15:10:17 -0800666 mutex_lock(&lport->lp_mutex);
667 fc_lport_enter_reset(lport);
668 mutex_unlock(&lport->lp_mutex);
669 break;
670 case DISC_EV_NONE:
671 WARN_ON(1);
672 break;
673 }
674}
675
676/**
Robert Love34f42a02009-02-27 10:55:45 -0800677 * fc_rport_enter_ready() - Enter the ready state and start discovery
Robert Love42e9a922008-12-09 15:10:17 -0800678 * @lport: Fibre Channel local port that is ready
679 *
680 * Locking Note: The lport lock is expected to be held before calling
681 * this routine.
682 */
683static void fc_lport_enter_ready(struct fc_lport *lport)
684{
Robert Love74147052009-06-10 15:31:10 -0700685 FC_LPORT_DBG(lport, "Entered READY from state %s\n",
686 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800687
688 fc_lport_state_enter(lport, LPORT_ST_READY);
Chris Leech8faecdd2009-11-03 11:46:19 -0800689 if (lport->vport)
690 fc_vport_set_state(lport->vport, FC_VPORT_ACTIVE);
691 fc_vports_linkchange(lport);
Robert Love42e9a922008-12-09 15:10:17 -0800692
Joe Eykholt29d898e2009-08-25 14:02:49 -0700693 if (!lport->ptp_rp)
694 lport->tt.disc_start(fc_lport_disc_callback, lport);
Robert Love42e9a922008-12-09 15:10:17 -0800695}
696
697/**
Robert Love34f42a02009-02-27 10:55:45 -0800698 * fc_lport_recv_flogi_req() - Receive a FLOGI request
Robert Love42e9a922008-12-09 15:10:17 -0800699 * @sp_in: The sequence the FLOGI is on
700 * @rx_fp: The frame the FLOGI is in
701 * @lport: The lport that recieved the request
702 *
703 * A received FLOGI request indicates a point-to-point connection.
704 * Accept it with the common service parameters indicating our N port.
705 * Set up to do a PLOGI if we have the higher-number WWPN.
706 *
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700707 * Locking Note: The lport lock is expected to be held before calling
Robert Love42e9a922008-12-09 15:10:17 -0800708 * this function.
709 */
710static void fc_lport_recv_flogi_req(struct fc_seq *sp_in,
711 struct fc_frame *rx_fp,
712 struct fc_lport *lport)
713{
714 struct fc_frame *fp;
715 struct fc_frame_header *fh;
716 struct fc_seq *sp;
717 struct fc_exch *ep;
718 struct fc_els_flogi *flp;
719 struct fc_els_flogi *new_flp;
720 u64 remote_wwpn;
721 u32 remote_fid;
722 u32 local_fid;
723 u32 f_ctl;
724
Robert Love74147052009-06-10 15:31:10 -0700725 FC_LPORT_DBG(lport, "Received FLOGI request while in state %s\n",
726 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800727
728 fh = fc_frame_header_get(rx_fp);
729 remote_fid = ntoh24(fh->fh_s_id);
730 flp = fc_frame_payload_get(rx_fp, sizeof(*flp));
731 if (!flp)
732 goto out;
733 remote_wwpn = get_unaligned_be64(&flp->fl_wwpn);
734 if (remote_wwpn == lport->wwpn) {
Robert Love74147052009-06-10 15:31:10 -0700735 printk(KERN_WARNING "libfc: Received FLOGI from port "
736 "with same WWPN %llx\n", remote_wwpn);
Robert Love42e9a922008-12-09 15:10:17 -0800737 goto out;
738 }
Robert Love74147052009-06-10 15:31:10 -0700739 FC_LPORT_DBG(lport, "FLOGI from port WWPN %llx\n", remote_wwpn);
Robert Love42e9a922008-12-09 15:10:17 -0800740
741 /*
742 * XXX what is the right thing to do for FIDs?
743 * The originator might expect our S_ID to be 0xfffffe.
744 * But if so, both of us could end up with the same FID.
745 */
746 local_fid = FC_LOCAL_PTP_FID_LO;
747 if (remote_wwpn < lport->wwpn) {
748 local_fid = FC_LOCAL_PTP_FID_HI;
749 if (!remote_fid || remote_fid == local_fid)
750 remote_fid = FC_LOCAL_PTP_FID_LO;
751 } else if (!remote_fid) {
752 remote_fid = FC_LOCAL_PTP_FID_HI;
753 }
754
755 fc_host_port_id(lport->host) = local_fid;
756
757 fp = fc_frame_alloc(lport, sizeof(*flp));
758 if (fp) {
759 sp = lport->tt.seq_start_next(fr_seq(rx_fp));
760 new_flp = fc_frame_payload_get(fp, sizeof(*flp));
761 fc_lport_flogi_fill(lport, new_flp, ELS_FLOGI);
762 new_flp->fl_cmd = (u8) ELS_LS_ACC;
763
764 /*
765 * Send the response. If this fails, the originator should
766 * repeat the sequence.
767 */
768 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
769 ep = fc_seq_exch(sp);
770 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
771 FC_TYPE_ELS, f_ctl, 0);
772 lport->tt.seq_send(lport, sp, fp);
773
774 } else {
775 fc_lport_error(lport, fp);
776 }
777 fc_lport_ptp_setup(lport, remote_fid, remote_wwpn,
778 get_unaligned_be64(&flp->fl_wwnn));
779
Robert Love42e9a922008-12-09 15:10:17 -0800780out:
781 sp = fr_seq(rx_fp);
782 fc_frame_free(rx_fp);
783}
784
785/**
Robert Love34f42a02009-02-27 10:55:45 -0800786 * fc_lport_recv_req() - The generic lport request handler
Robert Love42e9a922008-12-09 15:10:17 -0800787 * @lport: The lport that received the request
788 * @sp: The sequence the request is on
789 * @fp: The frame the request is in
790 *
791 * This function will see if the lport handles the request or
792 * if an rport should handle the request.
793 *
794 * Locking Note: This function should not be called with the lport
795 * lock held becuase it will grab the lock.
796 */
797static void fc_lport_recv_req(struct fc_lport *lport, struct fc_seq *sp,
798 struct fc_frame *fp)
799{
800 struct fc_frame_header *fh = fc_frame_header_get(fp);
801 void (*recv) (struct fc_seq *, struct fc_frame *, struct fc_lport *);
Robert Love42e9a922008-12-09 15:10:17 -0800802
803 mutex_lock(&lport->lp_mutex);
804
805 /*
806 * Handle special ELS cases like FLOGI, LOGO, and
807 * RSCN here. These don't require a session.
808 * Even if we had a session, it might not be ready.
809 */
Joe Eykholte9ba8b42009-07-29 17:04:33 -0700810 if (!lport->link_up)
811 fc_frame_free(fp);
812 else if (fh->fh_type == FC_TYPE_ELS &&
813 fh->fh_r_ctl == FC_RCTL_ELS_REQ) {
Robert Love42e9a922008-12-09 15:10:17 -0800814 /*
815 * Check opcode.
816 */
Joe Eykholt131203a2009-08-25 14:03:10 -0700817 recv = lport->tt.rport_recv_req;
Robert Love42e9a922008-12-09 15:10:17 -0800818 switch (fc_frame_payload_op(fp)) {
819 case ELS_FLOGI:
820 recv = fc_lport_recv_flogi_req;
821 break;
822 case ELS_LOGO:
823 fh = fc_frame_header_get(fp);
824 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI)
825 recv = fc_lport_recv_logo_req;
826 break;
827 case ELS_RSCN:
828 recv = lport->tt.disc_recv_req;
829 break;
830 case ELS_ECHO:
831 recv = fc_lport_recv_echo_req;
832 break;
833 case ELS_RLIR:
834 recv = fc_lport_recv_rlir_req;
835 break;
836 case ELS_RNID:
837 recv = fc_lport_recv_rnid_req;
838 break;
Robert Love42e9a922008-12-09 15:10:17 -0800839 }
840
Joe Eykholt131203a2009-08-25 14:03:10 -0700841 recv(sp, fp, lport);
Robert Love42e9a922008-12-09 15:10:17 -0800842 } else {
Robert Love74147052009-06-10 15:31:10 -0700843 FC_LPORT_DBG(lport, "dropping invalid frame (eof %x)\n",
844 fr_eof(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800845 fc_frame_free(fp);
846 }
847 mutex_unlock(&lport->lp_mutex);
848
849 /*
850 * The common exch_done for all request may not be good
851 * if any request requires longer hold on exhange. XXX
852 */
853 lport->tt.exch_done(sp);
854}
855
856/**
Robert Love34f42a02009-02-27 10:55:45 -0800857 * fc_lport_reset() - Reset an lport
Robert Love42e9a922008-12-09 15:10:17 -0800858 * @lport: The lport which should be reset
859 *
860 * Locking Note: This functions should not be called with the
861 * lport lock held.
862 */
863int fc_lport_reset(struct fc_lport *lport)
864{
Steve Maf7db2c152009-02-27 10:55:13 -0800865 cancel_delayed_work_sync(&lport->retry_work);
Robert Love42e9a922008-12-09 15:10:17 -0800866 mutex_lock(&lport->lp_mutex);
867 fc_lport_enter_reset(lport);
868 mutex_unlock(&lport->lp_mutex);
869 return 0;
870}
871EXPORT_SYMBOL(fc_lport_reset);
872
873/**
Joe Eykholt1190d922009-07-29 17:04:27 -0700874 * fc_lport_reset_locked() - Reset the local port
Robert Love42e9a922008-12-09 15:10:17 -0800875 * @lport: Fibre Channel local port to be reset
876 *
877 * Locking Note: The lport lock is expected to be held before calling
878 * this routine.
879 */
Joe Eykholt1190d922009-07-29 17:04:27 -0700880static void fc_lport_reset_locked(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -0800881{
Robert Love42e9a922008-12-09 15:10:17 -0800882 if (lport->dns_rp)
883 lport->tt.rport_logoff(lport->dns_rp);
884
Joe Eykholt48f00902009-08-25 14:01:50 -0700885 lport->ptp_rp = NULL;
Robert Love42e9a922008-12-09 15:10:17 -0800886
887 lport->tt.disc_stop(lport);
888
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -0800889 lport->tt.exch_mgr_reset(lport, 0, 0);
Robert Love42e9a922008-12-09 15:10:17 -0800890 fc_host_fabric_name(lport->host) = 0;
891 fc_host_port_id(lport->host) = 0;
Joe Eykholt1190d922009-07-29 17:04:27 -0700892}
Robert Love42e9a922008-12-09 15:10:17 -0800893
Joe Eykholt1190d922009-07-29 17:04:27 -0700894/**
895 * fc_lport_enter_reset() - Reset the local port
896 * @lport: Fibre Channel local port to be reset
897 *
898 * Locking Note: The lport lock is expected to be held before calling
899 * this routine.
900 */
901static void fc_lport_enter_reset(struct fc_lport *lport)
902{
903 FC_LPORT_DBG(lport, "Entered RESET state from %s state\n",
904 fc_lport_state(lport));
905
Chris Leech8faecdd2009-11-03 11:46:19 -0800906 if (lport->vport) {
907 if (lport->link_up)
908 fc_vport_set_state(lport->vport, FC_VPORT_INITIALIZING);
909 else
910 fc_vport_set_state(lport->vport, FC_VPORT_LINKDOWN);
911 }
Joe Eykholt1190d922009-07-29 17:04:27 -0700912 fc_lport_state_enter(lport, LPORT_ST_RESET);
Chris Leech8faecdd2009-11-03 11:46:19 -0800913 fc_vports_linkchange(lport);
Joe Eykholt1190d922009-07-29 17:04:27 -0700914 fc_lport_reset_locked(lport);
Vasu Devbc0e17f2009-02-27 10:54:57 -0800915 if (lport->link_up)
Robert Love42e9a922008-12-09 15:10:17 -0800916 fc_lport_enter_flogi(lport);
917}
918
919/**
Joe Eykholt1190d922009-07-29 17:04:27 -0700920 * fc_lport_enter_disabled() - disable the local port
921 * @lport: Fibre Channel local port to be reset
922 *
923 * Locking Note: The lport lock is expected to be held before calling
924 * this routine.
925 */
926static void fc_lport_enter_disabled(struct fc_lport *lport)
927{
928 FC_LPORT_DBG(lport, "Entered disabled state from %s state\n",
929 fc_lport_state(lport));
930
931 fc_lport_state_enter(lport, LPORT_ST_DISABLED);
Chris Leech8faecdd2009-11-03 11:46:19 -0800932 fc_vports_linkchange(lport);
Joe Eykholt1190d922009-07-29 17:04:27 -0700933 fc_lport_reset_locked(lport);
934}
935
936/**
Robert Love34f42a02009-02-27 10:55:45 -0800937 * fc_lport_error() - Handler for any errors
Robert Love42e9a922008-12-09 15:10:17 -0800938 * @lport: The fc_lport object
939 * @fp: The frame pointer
940 *
941 * If the error was caused by a resource allocation failure
942 * then wait for half a second and retry, otherwise retry
943 * after the e_d_tov time.
944 */
945static void fc_lport_error(struct fc_lport *lport, struct fc_frame *fp)
946{
947 unsigned long delay = 0;
Robert Love74147052009-06-10 15:31:10 -0700948 FC_LPORT_DBG(lport, "Error %ld in state %s, retries %d\n",
949 PTR_ERR(fp), fc_lport_state(lport),
950 lport->retry_count);
Robert Love42e9a922008-12-09 15:10:17 -0800951
952 if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
953 /*
954 * Memory allocation failure, or the exchange timed out.
955 * Retry after delay
956 */
957 if (lport->retry_count < lport->max_retry_count) {
958 lport->retry_count++;
959 if (!fp)
960 delay = msecs_to_jiffies(500);
961 else
962 delay = msecs_to_jiffies(lport->e_d_tov);
963
964 schedule_delayed_work(&lport->retry_work, delay);
965 } else {
966 switch (lport->state) {
Joe Eykholtb1d9fd52009-07-29 17:04:22 -0700967 case LPORT_ST_DISABLED:
Robert Love42e9a922008-12-09 15:10:17 -0800968 case LPORT_ST_READY:
969 case LPORT_ST_RESET:
Chris Leechc9c7bd72009-11-03 11:46:51 -0800970 case LPORT_ST_RNN_ID:
Chris Leech5baa17c2009-11-03 11:46:56 -0800971 case LPORT_ST_RSNN_NN:
Robert Love42e9a922008-12-09 15:10:17 -0800972 case LPORT_ST_RFT_ID:
973 case LPORT_ST_SCR:
974 case LPORT_ST_DNS:
975 case LPORT_ST_FLOGI:
976 case LPORT_ST_LOGO:
977 fc_lport_enter_reset(lport);
978 break;
979 }
980 }
981 }
982}
983
984/**
Robert Love34f42a02009-02-27 10:55:45 -0800985 * fc_lport_rft_id_resp() - Handle response to Register Fibre
Chris Leech28cc0e32009-11-03 11:46:46 -0800986 * Channel Types by ID (RFT_ID) request
987 * @sp: current sequence in RFT_ID exchange
Robert Love42e9a922008-12-09 15:10:17 -0800988 * @fp: response frame
989 * @lp_arg: Fibre Channel host port instance
990 *
991 * Locking Note: This function will be called without the lport lock
992 * held, but it will lock, call an _enter_* function or fc_lport_error
993 * and then unlock the lport.
994 */
995static void fc_lport_rft_id_resp(struct fc_seq *sp, struct fc_frame *fp,
996 void *lp_arg)
997{
998 struct fc_lport *lport = lp_arg;
999 struct fc_frame_header *fh;
1000 struct fc_ct_hdr *ct;
1001
Joe Eykholtf657d292009-08-25 14:03:21 -07001002 FC_LPORT_DBG(lport, "Received a RFT_ID %s\n", fc_els_resp_type(fp));
1003
Robert Love42e9a922008-12-09 15:10:17 -08001004 if (fp == ERR_PTR(-FC_EX_CLOSED))
1005 return;
1006
1007 mutex_lock(&lport->lp_mutex);
1008
Robert Love42e9a922008-12-09 15:10:17 -08001009 if (lport->state != LPORT_ST_RFT_ID) {
Robert Love74147052009-06-10 15:31:10 -07001010 FC_LPORT_DBG(lport, "Received a RFT_ID response, but in state "
1011 "%s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001012 if (IS_ERR(fp))
1013 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001014 goto out;
1015 }
1016
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001017 if (IS_ERR(fp)) {
1018 fc_lport_error(lport, fp);
1019 goto err;
1020 }
1021
Robert Love42e9a922008-12-09 15:10:17 -08001022 fh = fc_frame_header_get(fp);
1023 ct = fc_frame_payload_get(fp, sizeof(*ct));
1024
1025 if (fh && ct && fh->fh_type == FC_TYPE_CT &&
1026 ct->ct_fs_type == FC_FST_DIR &&
1027 ct->ct_fs_subtype == FC_NS_SUBTYPE &&
1028 ntohs(ct->ct_cmd) == FC_FS_ACC)
1029 fc_lport_enter_scr(lport);
1030 else
1031 fc_lport_error(lport, fp);
1032out:
1033 fc_frame_free(fp);
1034err:
1035 mutex_unlock(&lport->lp_mutex);
1036}
1037
1038/**
Chris Leech5baa17c2009-11-03 11:46:56 -08001039 * fc_lport_rsnn_nn_resp() - Handle response to Register Symbolic Node Name
1040 * by Node Name (RSNN_NN) request
1041 * @sp: current sequence in RSNN_NN exchange
1042 * @fp: response frame
1043 * @lp_arg: Fibre Channel host port instance
1044 *
1045 * Locking Note: This function will be called without the lport lock
1046 * held, but it will lock, call an _enter_* function or fc_lport_error
1047 * and then unlock the lport.
1048 */
1049static void fc_lport_rsnn_nn_resp(struct fc_seq *sp, struct fc_frame *fp,
1050 void *lp_arg)
1051{
1052 struct fc_lport *lport = lp_arg;
1053 struct fc_frame_header *fh;
1054 struct fc_ct_hdr *ct;
1055
1056 FC_LPORT_DBG(lport, "Received a RSNN_NN %s\n", fc_els_resp_type(fp));
1057
1058 if (fp == ERR_PTR(-FC_EX_CLOSED))
1059 return;
1060
1061 mutex_lock(&lport->lp_mutex);
1062
1063 if (lport->state != LPORT_ST_RSNN_NN) {
1064 FC_LPORT_DBG(lport, "Received a RSNN_NN response, but in state "
1065 "%s\n", fc_lport_state(lport));
1066 if (IS_ERR(fp))
1067 goto err;
1068 goto out;
1069 }
1070
1071 if (IS_ERR(fp)) {
1072 fc_lport_error(lport, fp);
1073 goto err;
1074 }
1075
1076 fh = fc_frame_header_get(fp);
1077 ct = fc_frame_payload_get(fp, sizeof(*ct));
1078 if (fh && ct && fh->fh_type == FC_TYPE_CT &&
1079 ct->ct_fs_type == FC_FST_DIR &&
1080 ct->ct_fs_subtype == FC_NS_SUBTYPE &&
1081 ntohs(ct->ct_cmd) == FC_FS_ACC)
1082 fc_lport_enter_rsnn_nn(lport);
1083 else
1084 fc_lport_error(lport, fp);
1085
1086out:
1087 fc_frame_free(fp);
1088err:
1089 mutex_unlock(&lport->lp_mutex);
1090}
1091
1092/**
Chris Leechc9c7bd72009-11-03 11:46:51 -08001093 * fc_lport_rnn_id_resp() - Handle response to Register Node
1094 * Name by ID (RNN_ID) request
1095 * @sp: current sequence in RNN_ID exchange
1096 * @fp: response frame
1097 * @lp_arg: Fibre Channel host port instance
1098 *
1099 * Locking Note: This function will be called without the lport lock
1100 * held, but it will lock, call an _enter_* function or fc_lport_error
1101 * and then unlock the lport.
1102 */
1103static void fc_lport_rnn_id_resp(struct fc_seq *sp, struct fc_frame *fp,
1104 void *lp_arg)
1105{
1106 struct fc_lport *lport = lp_arg;
1107 struct fc_frame_header *fh;
1108 struct fc_ct_hdr *ct;
1109
1110 FC_LPORT_DBG(lport, "Received a RNN_ID %s\n", fc_els_resp_type(fp));
1111
1112 if (fp == ERR_PTR(-FC_EX_CLOSED))
1113 return;
1114
1115 mutex_lock(&lport->lp_mutex);
1116
1117 if (lport->state != LPORT_ST_RNN_ID) {
1118 FC_LPORT_DBG(lport, "Received a RNN_ID response, but in state "
1119 "%s\n", fc_lport_state(lport));
1120 if (IS_ERR(fp))
1121 goto err;
1122 goto out;
1123 }
1124
1125 if (IS_ERR(fp)) {
1126 fc_lport_error(lport, fp);
1127 goto err;
1128 }
1129
1130 fh = fc_frame_header_get(fp);
1131 ct = fc_frame_payload_get(fp, sizeof(*ct));
1132 if (fh && ct && fh->fh_type == FC_TYPE_CT &&
1133 ct->ct_fs_type == FC_FST_DIR &&
1134 ct->ct_fs_subtype == FC_NS_SUBTYPE &&
1135 ntohs(ct->ct_cmd) == FC_FS_ACC)
1136 fc_lport_enter_rft_id(lport);
1137 else
1138 fc_lport_error(lport, fp);
1139
1140out:
1141 fc_frame_free(fp);
1142err:
1143 mutex_unlock(&lport->lp_mutex);
1144}
1145
1146/**
Robert Love34f42a02009-02-27 10:55:45 -08001147 * fc_lport_scr_resp() - Handle response to State Change Register (SCR) request
Robert Love42e9a922008-12-09 15:10:17 -08001148 * @sp: current sequence in SCR exchange
1149 * @fp: response frame
1150 * @lp_arg: Fibre Channel lport port instance that sent the registration request
1151 *
1152 * Locking Note: This function will be called without the lport lock
1153 * held, but it will lock, call an _enter_* function or fc_lport_error
1154 * and then unlock the lport.
1155 */
1156static void fc_lport_scr_resp(struct fc_seq *sp, struct fc_frame *fp,
1157 void *lp_arg)
1158{
1159 struct fc_lport *lport = lp_arg;
1160 u8 op;
1161
Joe Eykholtf657d292009-08-25 14:03:21 -07001162 FC_LPORT_DBG(lport, "Received a SCR %s\n", fc_els_resp_type(fp));
1163
Robert Love42e9a922008-12-09 15:10:17 -08001164 if (fp == ERR_PTR(-FC_EX_CLOSED))
1165 return;
1166
1167 mutex_lock(&lport->lp_mutex);
1168
Robert Love42e9a922008-12-09 15:10:17 -08001169 if (lport->state != LPORT_ST_SCR) {
Robert Love74147052009-06-10 15:31:10 -07001170 FC_LPORT_DBG(lport, "Received a SCR response, but in state "
1171 "%s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001172 if (IS_ERR(fp))
1173 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001174 goto out;
1175 }
1176
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001177 if (IS_ERR(fp)) {
1178 fc_lport_error(lport, fp);
1179 goto err;
1180 }
1181
Robert Love42e9a922008-12-09 15:10:17 -08001182 op = fc_frame_payload_op(fp);
1183 if (op == ELS_LS_ACC)
1184 fc_lport_enter_ready(lport);
1185 else
1186 fc_lport_error(lport, fp);
1187
1188out:
1189 fc_frame_free(fp);
1190err:
1191 mutex_unlock(&lport->lp_mutex);
1192}
1193
1194/**
Robert Love34f42a02009-02-27 10:55:45 -08001195 * fc_lport_enter_scr() - Send a State Change Register (SCR) request
Robert Love42e9a922008-12-09 15:10:17 -08001196 * @lport: Fibre Channel local port to register for state changes
1197 *
1198 * Locking Note: The lport lock is expected to be held before calling
1199 * this routine.
1200 */
1201static void fc_lport_enter_scr(struct fc_lport *lport)
1202{
1203 struct fc_frame *fp;
1204
Robert Love74147052009-06-10 15:31:10 -07001205 FC_LPORT_DBG(lport, "Entered SCR state from %s state\n",
1206 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001207
1208 fc_lport_state_enter(lport, LPORT_ST_SCR);
1209
1210 fp = fc_frame_alloc(lport, sizeof(struct fc_els_scr));
1211 if (!fp) {
1212 fc_lport_error(lport, fp);
1213 return;
1214 }
1215
Joe Eykholta46f3272009-08-25 14:00:55 -07001216 if (!lport->tt.elsct_send(lport, FC_FID_FCTRL, fp, ELS_SCR,
Robert Love42e9a922008-12-09 15:10:17 -08001217 fc_lport_scr_resp, lport, lport->e_d_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001218 fc_lport_error(lport, NULL);
Robert Love42e9a922008-12-09 15:10:17 -08001219}
1220
1221/**
Robert Love34f42a02009-02-27 10:55:45 -08001222 * fc_lport_enter_rft_id() - Register FC4-types with the name server
Robert Love42e9a922008-12-09 15:10:17 -08001223 * @lport: Fibre Channel local port to register
1224 *
1225 * Locking Note: The lport lock is expected to be held before calling
1226 * this routine.
1227 */
1228static void fc_lport_enter_rft_id(struct fc_lport *lport)
1229{
1230 struct fc_frame *fp;
1231 struct fc_ns_fts *lps;
1232 int i;
1233
Robert Love74147052009-06-10 15:31:10 -07001234 FC_LPORT_DBG(lport, "Entered RFT_ID state from %s state\n",
1235 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001236
1237 fc_lport_state_enter(lport, LPORT_ST_RFT_ID);
1238
1239 lps = &lport->fcts;
1240 i = sizeof(lps->ff_type_map) / sizeof(lps->ff_type_map[0]);
1241 while (--i >= 0)
1242 if (ntohl(lps->ff_type_map[i]) != 0)
1243 break;
1244 if (i < 0) {
1245 /* nothing to register, move on to SCR */
1246 fc_lport_enter_scr(lport);
1247 return;
1248 }
1249
1250 fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
1251 sizeof(struct fc_ns_rft));
1252 if (!fp) {
1253 fc_lport_error(lport, fp);
1254 return;
1255 }
1256
Joe Eykholta46f3272009-08-25 14:00:55 -07001257 if (!lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RFT_ID,
Robert Love42e9a922008-12-09 15:10:17 -08001258 fc_lport_rft_id_resp,
1259 lport, lport->e_d_tov))
1260 fc_lport_error(lport, fp);
1261}
1262
Chris Leechc9c7bd72009-11-03 11:46:51 -08001263/**
Chris Leech5baa17c2009-11-03 11:46:56 -08001264 * fc_rport_enter_rsnn_nn() - Register symbolic node name with the name server
1265 * @lport: Fibre Channel local port to register
1266 *
1267 * Locking Note: The lport lock is expected to be held before calling
1268 * this routine.
1269 */
1270static void fc_lport_enter_rsnn_nn(struct fc_lport *lport)
1271{
1272 struct fc_frame *fp;
1273 size_t len;
1274
1275 FC_LPORT_DBG(lport, "Entered RSNN_NN state from %s state\n",
1276 fc_lport_state(lport));
1277
1278 fc_lport_state_enter(lport, LPORT_ST_RSNN_NN);
1279
1280 len = strnlen(fc_host_symbolic_name(lport->host), 255);
1281 fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
1282 sizeof(struct fc_ns_rsnn) + len);
1283 if (!fp) {
1284 fc_lport_error(lport, fp);
1285 return;
1286 }
1287
1288 if (!lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSNN_NN,
1289 fc_lport_rsnn_nn_resp,
1290 lport, lport->e_d_tov))
1291 fc_lport_error(lport, fp);
1292}
1293
1294/**
Chris Leechc9c7bd72009-11-03 11:46:51 -08001295 * fc_rport_enter_rnn_id() - Register node name with the name server
1296 * @lport: Fibre Channel local port to register
1297 *
1298 * Locking Note: The lport lock is expected to be held before calling
1299 * this routine.
1300 */
1301static void fc_lport_enter_rnn_id(struct fc_lport *lport)
1302{
1303 struct fc_frame *fp;
1304
1305 FC_LPORT_DBG(lport, "Entered RNN_ID state from %s state\n",
1306 fc_lport_state(lport));
1307
1308 fc_lport_state_enter(lport, LPORT_ST_RNN_ID);
1309
1310 fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
1311 sizeof(struct fc_ns_rn_id));
1312 if (!fp) {
1313 fc_lport_error(lport, fp);
1314 return;
1315 }
1316
1317 if (!lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RNN_ID,
1318 fc_lport_rnn_id_resp,
1319 lport, lport->e_d_tov))
1320 fc_lport_error(lport, fp);
1321}
1322
Robert Love42e9a922008-12-09 15:10:17 -08001323static struct fc_rport_operations fc_lport_rport_ops = {
1324 .event_callback = fc_lport_rport_callback,
1325};
1326
1327/**
Robert Love34f42a02009-02-27 10:55:45 -08001328 * fc_rport_enter_dns() - Create a rport to the name server
Robert Love42e9a922008-12-09 15:10:17 -08001329 * @lport: Fibre Channel local port requesting a rport for the name server
1330 *
1331 * Locking Note: The lport lock is expected to be held before calling
1332 * this routine.
1333 */
1334static void fc_lport_enter_dns(struct fc_lport *lport)
1335{
Joe Eykholtab28f1f2009-08-25 14:00:34 -07001336 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -08001337
Robert Love74147052009-06-10 15:31:10 -07001338 FC_LPORT_DBG(lport, "Entered DNS state from %s state\n",
1339 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001340
1341 fc_lport_state_enter(lport, LPORT_ST_DNS);
1342
Joe Eykholt48f00902009-08-25 14:01:50 -07001343 mutex_lock(&lport->disc.disc_mutex);
Robert Love9737e6a2009-08-25 14:02:59 -07001344 rdata = lport->tt.rport_create(lport, FC_FID_DIR_SERV);
Joe Eykholt48f00902009-08-25 14:01:50 -07001345 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001346 if (!rdata)
Robert Love42e9a922008-12-09 15:10:17 -08001347 goto err;
1348
Robert Love42e9a922008-12-09 15:10:17 -08001349 rdata->ops = &fc_lport_rport_ops;
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001350 lport->tt.rport_login(rdata);
Robert Love42e9a922008-12-09 15:10:17 -08001351 return;
1352
1353err:
1354 fc_lport_error(lport, NULL);
1355}
1356
1357/**
Robert Love34f42a02009-02-27 10:55:45 -08001358 * fc_lport_timeout() - Handler for the retry_work timer.
Robert Love42e9a922008-12-09 15:10:17 -08001359 * @work: The work struct of the fc_lport
1360 */
1361static void fc_lport_timeout(struct work_struct *work)
1362{
1363 struct fc_lport *lport =
1364 container_of(work, struct fc_lport,
1365 retry_work.work);
1366
1367 mutex_lock(&lport->lp_mutex);
1368
1369 switch (lport->state) {
Joe Eykholtb1d9fd52009-07-29 17:04:22 -07001370 case LPORT_ST_DISABLED:
Robert Love42e9a922008-12-09 15:10:17 -08001371 WARN_ON(1);
1372 break;
Joe Eykholt22655ac2009-10-21 16:27:22 -07001373 case LPORT_ST_READY:
1374 WARN_ON(1);
1375 break;
1376 case LPORT_ST_RESET:
1377 break;
Robert Love42e9a922008-12-09 15:10:17 -08001378 case LPORT_ST_FLOGI:
1379 fc_lport_enter_flogi(lport);
1380 break;
1381 case LPORT_ST_DNS:
1382 fc_lport_enter_dns(lport);
1383 break;
Chris Leechc9c7bd72009-11-03 11:46:51 -08001384 case LPORT_ST_RNN_ID:
1385 fc_lport_enter_rnn_id(lport);
1386 break;
Chris Leech5baa17c2009-11-03 11:46:56 -08001387 case LPORT_ST_RSNN_NN:
1388 fc_lport_enter_rsnn_nn(lport);
1389 break;
Robert Love42e9a922008-12-09 15:10:17 -08001390 case LPORT_ST_RFT_ID:
1391 fc_lport_enter_rft_id(lport);
1392 break;
1393 case LPORT_ST_SCR:
1394 fc_lport_enter_scr(lport);
1395 break;
1396 case LPORT_ST_LOGO:
1397 fc_lport_enter_logo(lport);
1398 break;
1399 }
1400
1401 mutex_unlock(&lport->lp_mutex);
1402}
1403
1404/**
Robert Love34f42a02009-02-27 10:55:45 -08001405 * fc_lport_logo_resp() - Handle response to LOGO request
Robert Love42e9a922008-12-09 15:10:17 -08001406 * @sp: current sequence in LOGO exchange
1407 * @fp: response frame
1408 * @lp_arg: Fibre Channel lport port instance that sent the LOGO request
1409 *
1410 * Locking Note: This function will be called without the lport lock
1411 * held, but it will lock, call an _enter_* function or fc_lport_error
1412 * and then unlock the lport.
1413 */
Chris Leech11b56182009-11-03 11:46:29 -08001414void fc_lport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
Robert Love42e9a922008-12-09 15:10:17 -08001415 void *lp_arg)
1416{
1417 struct fc_lport *lport = lp_arg;
1418 u8 op;
1419
Joe Eykholtf657d292009-08-25 14:03:21 -07001420 FC_LPORT_DBG(lport, "Received a LOGO %s\n", fc_els_resp_type(fp));
1421
Robert Love42e9a922008-12-09 15:10:17 -08001422 if (fp == ERR_PTR(-FC_EX_CLOSED))
1423 return;
1424
1425 mutex_lock(&lport->lp_mutex);
1426
Robert Love42e9a922008-12-09 15:10:17 -08001427 if (lport->state != LPORT_ST_LOGO) {
Robert Love74147052009-06-10 15:31:10 -07001428 FC_LPORT_DBG(lport, "Received a LOGO response, but in state "
1429 "%s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001430 if (IS_ERR(fp))
1431 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001432 goto out;
1433 }
1434
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001435 if (IS_ERR(fp)) {
1436 fc_lport_error(lport, fp);
1437 goto err;
1438 }
1439
Robert Love42e9a922008-12-09 15:10:17 -08001440 op = fc_frame_payload_op(fp);
1441 if (op == ELS_LS_ACC)
Joe Eykholt1190d922009-07-29 17:04:27 -07001442 fc_lport_enter_disabled(lport);
Robert Love42e9a922008-12-09 15:10:17 -08001443 else
1444 fc_lport_error(lport, fp);
1445
1446out:
1447 fc_frame_free(fp);
1448err:
1449 mutex_unlock(&lport->lp_mutex);
1450}
Chris Leech11b56182009-11-03 11:46:29 -08001451EXPORT_SYMBOL(fc_lport_logo_resp);
Robert Love42e9a922008-12-09 15:10:17 -08001452
1453/**
Robert Love34f42a02009-02-27 10:55:45 -08001454 * fc_rport_enter_logo() - Logout of the fabric
Robert Love42e9a922008-12-09 15:10:17 -08001455 * @lport: Fibre Channel local port to be logged out
1456 *
1457 * Locking Note: The lport lock is expected to be held before calling
1458 * this routine.
1459 */
1460static void fc_lport_enter_logo(struct fc_lport *lport)
1461{
1462 struct fc_frame *fp;
1463 struct fc_els_logo *logo;
1464
Robert Love74147052009-06-10 15:31:10 -07001465 FC_LPORT_DBG(lport, "Entered LOGO state from %s state\n",
1466 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001467
1468 fc_lport_state_enter(lport, LPORT_ST_LOGO);
Chris Leech8faecdd2009-11-03 11:46:19 -08001469 fc_vports_linkchange(lport);
Robert Love42e9a922008-12-09 15:10:17 -08001470
Robert Love42e9a922008-12-09 15:10:17 -08001471 fp = fc_frame_alloc(lport, sizeof(*logo));
1472 if (!fp) {
1473 fc_lport_error(lport, fp);
1474 return;
1475 }
1476
Joe Eykholta46f3272009-08-25 14:00:55 -07001477 if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp, ELS_LOGO,
1478 fc_lport_logo_resp, lport, lport->e_d_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001479 fc_lport_error(lport, NULL);
Robert Love42e9a922008-12-09 15:10:17 -08001480}
1481
1482/**
Robert Love34f42a02009-02-27 10:55:45 -08001483 * fc_lport_flogi_resp() - Handle response to FLOGI request
Robert Love42e9a922008-12-09 15:10:17 -08001484 * @sp: current sequence in FLOGI exchange
1485 * @fp: response frame
1486 * @lp_arg: Fibre Channel lport port instance that sent the FLOGI request
1487 *
1488 * Locking Note: This function will be called without the lport lock
1489 * held, but it will lock, call an _enter_* function or fc_lport_error
1490 * and then unlock the lport.
1491 */
Chris Leech11b56182009-11-03 11:46:29 -08001492void fc_lport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
Robert Love42e9a922008-12-09 15:10:17 -08001493 void *lp_arg)
1494{
1495 struct fc_lport *lport = lp_arg;
1496 struct fc_frame_header *fh;
1497 struct fc_els_flogi *flp;
1498 u32 did;
1499 u16 csp_flags;
1500 unsigned int r_a_tov;
1501 unsigned int e_d_tov;
1502 u16 mfs;
1503
Joe Eykholtf657d292009-08-25 14:03:21 -07001504 FC_LPORT_DBG(lport, "Received a FLOGI %s\n", fc_els_resp_type(fp));
1505
Robert Love42e9a922008-12-09 15:10:17 -08001506 if (fp == ERR_PTR(-FC_EX_CLOSED))
1507 return;
1508
1509 mutex_lock(&lport->lp_mutex);
1510
Robert Love42e9a922008-12-09 15:10:17 -08001511 if (lport->state != LPORT_ST_FLOGI) {
Robert Love74147052009-06-10 15:31:10 -07001512 FC_LPORT_DBG(lport, "Received a FLOGI response, but in state "
1513 "%s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001514 if (IS_ERR(fp))
1515 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001516 goto out;
1517 }
1518
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001519 if (IS_ERR(fp)) {
1520 fc_lport_error(lport, fp);
1521 goto err;
1522 }
1523
Robert Love42e9a922008-12-09 15:10:17 -08001524 fh = fc_frame_header_get(fp);
1525 did = ntoh24(fh->fh_d_id);
1526 if (fc_frame_payload_op(fp) == ELS_LS_ACC && did != 0) {
1527
Robert Love74147052009-06-10 15:31:10 -07001528 printk(KERN_INFO "libfc: Assigned FID (%6x) in FLOGI response\n",
1529 did);
Robert Love42e9a922008-12-09 15:10:17 -08001530 fc_host_port_id(lport->host) = did;
1531
1532 flp = fc_frame_payload_get(fp, sizeof(*flp));
1533 if (flp) {
1534 mfs = ntohs(flp->fl_csp.sp_bb_data) &
1535 FC_SP_BB_DATA_MASK;
1536 if (mfs >= FC_SP_MIN_MAX_PAYLOAD &&
1537 mfs < lport->mfs)
1538 lport->mfs = mfs;
1539 csp_flags = ntohs(flp->fl_csp.sp_features);
1540 r_a_tov = ntohl(flp->fl_csp.sp_r_a_tov);
1541 e_d_tov = ntohl(flp->fl_csp.sp_e_d_tov);
1542 if (csp_flags & FC_SP_FT_EDTR)
1543 e_d_tov /= 1000000;
Chris Leechdb36c062009-11-03 11:46:24 -08001544
1545 lport->npiv_enabled = !!(csp_flags & FC_SP_FT_NPIV_ACC);
1546
Robert Love42e9a922008-12-09 15:10:17 -08001547 if ((csp_flags & FC_SP_FT_FPORT) == 0) {
1548 if (e_d_tov > lport->e_d_tov)
1549 lport->e_d_tov = e_d_tov;
1550 lport->r_a_tov = 2 * e_d_tov;
Robert Love74147052009-06-10 15:31:10 -07001551 printk(KERN_INFO "libfc: Port (%6x) entered "
1552 "point to point mode\n", did);
Robert Love42e9a922008-12-09 15:10:17 -08001553 fc_lport_ptp_setup(lport, ntoh24(fh->fh_s_id),
1554 get_unaligned_be64(
1555 &flp->fl_wwpn),
1556 get_unaligned_be64(
1557 &flp->fl_wwnn));
1558 } else {
1559 lport->e_d_tov = e_d_tov;
1560 lport->r_a_tov = r_a_tov;
1561 fc_host_fabric_name(lport->host) =
1562 get_unaligned_be64(&flp->fl_wwnn);
1563 fc_lport_enter_dns(lport);
1564 }
1565 }
Robert Love42e9a922008-12-09 15:10:17 -08001566 } else {
Robert Love74147052009-06-10 15:31:10 -07001567 FC_LPORT_DBG(lport, "Bad FLOGI response\n");
Robert Love42e9a922008-12-09 15:10:17 -08001568 }
1569
1570out:
1571 fc_frame_free(fp);
1572err:
1573 mutex_unlock(&lport->lp_mutex);
1574}
Chris Leech11b56182009-11-03 11:46:29 -08001575EXPORT_SYMBOL(fc_lport_flogi_resp);
Robert Love42e9a922008-12-09 15:10:17 -08001576
1577/**
Robert Love34f42a02009-02-27 10:55:45 -08001578 * fc_rport_enter_flogi() - Send a FLOGI request to the fabric manager
Robert Love42e9a922008-12-09 15:10:17 -08001579 * @lport: Fibre Channel local port to be logged in to the fabric
1580 *
1581 * Locking Note: The lport lock is expected to be held before calling
1582 * this routine.
1583 */
1584void fc_lport_enter_flogi(struct fc_lport *lport)
1585{
1586 struct fc_frame *fp;
1587
Robert Love74147052009-06-10 15:31:10 -07001588 FC_LPORT_DBG(lport, "Entered FLOGI state from %s state\n",
1589 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001590
1591 fc_lport_state_enter(lport, LPORT_ST_FLOGI);
1592
1593 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
1594 if (!fp)
1595 return fc_lport_error(lport, fp);
1596
Chris Leechdb36c062009-11-03 11:46:24 -08001597 if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp,
1598 lport->vport ? ELS_FDISC : ELS_FLOGI,
Robert Love42e9a922008-12-09 15:10:17 -08001599 fc_lport_flogi_resp, lport, lport->e_d_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001600 fc_lport_error(lport, NULL);
Robert Love42e9a922008-12-09 15:10:17 -08001601}
1602
1603/* Configure a fc_lport */
1604int fc_lport_config(struct fc_lport *lport)
1605{
1606 INIT_DELAYED_WORK(&lport->retry_work, fc_lport_timeout);
1607 mutex_init(&lport->lp_mutex);
1608
Joe Eykholtb1d9fd52009-07-29 17:04:22 -07001609 fc_lport_state_enter(lport, LPORT_ST_DISABLED);
Robert Love42e9a922008-12-09 15:10:17 -08001610
1611 fc_lport_add_fc4_type(lport, FC_TYPE_FCP);
1612 fc_lport_add_fc4_type(lport, FC_TYPE_CT);
1613
1614 return 0;
1615}
1616EXPORT_SYMBOL(fc_lport_config);
1617
1618int fc_lport_init(struct fc_lport *lport)
1619{
1620 if (!lport->tt.lport_recv)
1621 lport->tt.lport_recv = fc_lport_recv_req;
1622
1623 if (!lport->tt.lport_reset)
1624 lport->tt.lport_reset = fc_lport_reset;
1625
1626 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
1627 fc_host_node_name(lport->host) = lport->wwnn;
1628 fc_host_port_name(lport->host) = lport->wwpn;
1629 fc_host_supported_classes(lport->host) = FC_COS_CLASS3;
1630 memset(fc_host_supported_fc4s(lport->host), 0,
1631 sizeof(fc_host_supported_fc4s(lport->host)));
1632 fc_host_supported_fc4s(lport->host)[2] = 1;
1633 fc_host_supported_fc4s(lport->host)[7] = 1;
1634
1635 /* This value is also unchanging */
1636 memset(fc_host_active_fc4s(lport->host), 0,
1637 sizeof(fc_host_active_fc4s(lport->host)));
1638 fc_host_active_fc4s(lport->host)[2] = 1;
1639 fc_host_active_fc4s(lport->host)[7] = 1;
1640 fc_host_maxframe_size(lport->host) = lport->mfs;
1641 fc_host_supported_speeds(lport->host) = 0;
1642 if (lport->link_supported_speeds & FC_PORTSPEED_1GBIT)
1643 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_1GBIT;
1644 if (lport->link_supported_speeds & FC_PORTSPEED_10GBIT)
1645 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_10GBIT;
1646
Robert Love42e9a922008-12-09 15:10:17 -08001647 return 0;
1648}
1649EXPORT_SYMBOL(fc_lport_init);