blob: 2162e6b0f43e3334c100d4b5abbd5557e89bcd02 [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>
Steve Maa51ab392009-11-03 11:47:34 -080097#include <linux/scatterlist.h>
Robert Love42e9a922008-12-09 15:10:17 -080098
Robert Love8866a5d2009-11-03 11:45:58 -080099#include "fc_libfc.h"
100
Robert Love42e9a922008-12-09 15:10:17 -0800101/* Fabric IDs to use for point-to-point mode, chosen on whims. */
102#define FC_LOCAL_PTP_FID_LO 0x010101
103#define FC_LOCAL_PTP_FID_HI 0x010102
104
105#define DNS_DELAY 3 /* Discovery delay after RSCN (in seconds)*/
106
Robert Love42e9a922008-12-09 15:10:17 -0800107static void fc_lport_error(struct fc_lport *, struct fc_frame *);
108
109static void fc_lport_enter_reset(struct fc_lport *);
110static void fc_lport_enter_flogi(struct fc_lport *);
111static void fc_lport_enter_dns(struct fc_lport *);
Chris Leechc914f7d2009-11-03 11:47:12 -0800112static void fc_lport_enter_ns(struct fc_lport *, enum fc_lport_state);
Robert Love42e9a922008-12-09 15:10:17 -0800113static void fc_lport_enter_scr(struct fc_lport *);
114static void fc_lport_enter_ready(struct fc_lport *);
115static void fc_lport_enter_logo(struct fc_lport *);
116
117static const char *fc_lport_state_names[] = {
Joe Eykholtb1d9fd52009-07-29 17:04:22 -0700118 [LPORT_ST_DISABLED] = "disabled",
Robert Love42e9a922008-12-09 15:10:17 -0800119 [LPORT_ST_FLOGI] = "FLOGI",
120 [LPORT_ST_DNS] = "dNS",
Chris Leechc9c7bd72009-11-03 11:46:51 -0800121 [LPORT_ST_RNN_ID] = "RNN_ID",
Chris Leech5baa17c2009-11-03 11:46:56 -0800122 [LPORT_ST_RSNN_NN] = "RSNN_NN",
Chris Leechc9866a52009-11-03 11:47:01 -0800123 [LPORT_ST_RSPN_ID] = "RSPN_ID",
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
Steve Maa51ab392009-11-03 11:47:34 -0800131/**
132 * struct fc_bsg_info - FC Passthrough managemet structure
133 * @job: The passthrough job
134 * @lport: The local port to pass through a command
135 * @rsp_code: The expected response code
136 * @sg: job->reply_payload.sg_list
137 * @nents: job->reply_payload.sg_cnt
138 * @offset: The offset into the response data
139 */
140struct fc_bsg_info {
141 struct fc_bsg_job *job;
142 struct fc_lport *lport;
143 u16 rsp_code;
144 struct scatterlist *sg;
145 u32 nents;
146 size_t offset;
147};
148
Robert Love42e9a922008-12-09 15:10:17 -0800149static int fc_frame_drop(struct fc_lport *lport, struct fc_frame *fp)
150{
151 fc_frame_free(fp);
152 return 0;
153}
154
155/**
Robert Love34f42a02009-02-27 10:55:45 -0800156 * fc_lport_rport_callback() - Event handler for rport events
Robert Love42e9a922008-12-09 15:10:17 -0800157 * @lport: The lport which is receiving the event
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700158 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800159 * @event: The event that occured
160 *
161 * Locking Note: The rport lock should not be held when calling
162 * this function.
163 */
164static void fc_lport_rport_callback(struct fc_lport *lport,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700165 struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -0800166 enum fc_rport_event event)
167{
Robert Love74147052009-06-10 15:31:10 -0700168 FC_LPORT_DBG(lport, "Received a %d event for port (%6x)\n", event,
Joe Eykholtf211fa52009-08-25 14:01:01 -0700169 rdata->ids.port_id);
Robert Love42e9a922008-12-09 15:10:17 -0800170
Joe Eykholtb5cbf082009-08-25 14:01:44 -0700171 mutex_lock(&lport->lp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800172 switch (event) {
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700173 case RPORT_EV_READY:
Joe Eykholtb5cbf082009-08-25 14:01:44 -0700174 if (lport->state == LPORT_ST_DNS) {
175 lport->dns_rp = rdata;
Chris Leechc914f7d2009-11-03 11:47:12 -0800176 fc_lport_enter_ns(lport, LPORT_ST_RNN_ID);
Joe Eykholtb5cbf082009-08-25 14:01:44 -0700177 } else {
178 FC_LPORT_DBG(lport, "Received an READY event "
179 "on port (%6x) for the directory "
180 "server, but the lport is not "
181 "in the DNS state, it's in the "
182 "%d state", rdata->ids.port_id,
183 lport->state);
184 lport->tt.rport_logoff(rdata);
185 }
Robert Love42e9a922008-12-09 15:10:17 -0800186 break;
187 case RPORT_EV_LOGO:
188 case RPORT_EV_FAILED:
189 case RPORT_EV_STOP:
Joe Eykholtb5cbf082009-08-25 14:01:44 -0700190 lport->dns_rp = NULL;
Robert Love42e9a922008-12-09 15:10:17 -0800191 break;
192 case RPORT_EV_NONE:
193 break;
194 }
Joe Eykholtb5cbf082009-08-25 14:01:44 -0700195 mutex_unlock(&lport->lp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800196}
197
198/**
Robert Love34f42a02009-02-27 10:55:45 -0800199 * fc_lport_state() - Return a string which represents the lport's state
Robert Love42e9a922008-12-09 15:10:17 -0800200 * @lport: The lport whose state is to converted to a string
201 */
202static const char *fc_lport_state(struct fc_lport *lport)
203{
204 const char *cp;
205
206 cp = fc_lport_state_names[lport->state];
207 if (!cp)
208 cp = "unknown";
209 return cp;
210}
211
212/**
Robert Love34f42a02009-02-27 10:55:45 -0800213 * fc_lport_ptp_setup() - Create an rport for point-to-point mode
Robert Love42e9a922008-12-09 15:10:17 -0800214 * @lport: The lport to attach the ptp rport to
215 * @fid: The FID of the ptp rport
216 * @remote_wwpn: The WWPN of the ptp rport
217 * @remote_wwnn: The WWNN of the ptp rport
218 */
219static void fc_lport_ptp_setup(struct fc_lport *lport,
220 u32 remote_fid, u64 remote_wwpn,
221 u64 remote_wwnn)
222{
Joe Eykholt48f00902009-08-25 14:01:50 -0700223 mutex_lock(&lport->disc.disc_mutex);
224 if (lport->ptp_rp)
Robert Love42e9a922008-12-09 15:10:17 -0800225 lport->tt.rport_logoff(lport->ptp_rp);
Robert Love9737e6a2009-08-25 14:02:59 -0700226 lport->ptp_rp = lport->tt.rport_create(lport, remote_fid);
227 lport->ptp_rp->ids.port_name = remote_wwpn;
228 lport->ptp_rp->ids.node_name = remote_wwnn;
Joe Eykholt48f00902009-08-25 14:01:50 -0700229 mutex_unlock(&lport->disc.disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800230
231 lport->tt.rport_login(lport->ptp_rp);
232
233 fc_lport_enter_ready(lport);
234}
235
236void fc_get_host_port_type(struct Scsi_Host *shost)
237{
238 /* TODO - currently just NPORT */
239 fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
240}
241EXPORT_SYMBOL(fc_get_host_port_type);
242
243void fc_get_host_port_state(struct Scsi_Host *shost)
244{
245 struct fc_lport *lp = shost_priv(shost);
246
Chris Leech8faecdd2009-11-03 11:46:19 -0800247 mutex_lock(&lp->lp_mutex);
248 if (!lp->link_up)
249 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
Robert Love42e9a922008-12-09 15:10:17 -0800250 else
Chris Leech8faecdd2009-11-03 11:46:19 -0800251 switch (lp->state) {
252 case LPORT_ST_READY:
253 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
254 break;
255 default:
256 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
257 }
258 mutex_unlock(&lp->lp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800259}
260EXPORT_SYMBOL(fc_get_host_port_state);
261
262void fc_get_host_speed(struct Scsi_Host *shost)
263{
264 struct fc_lport *lport = shost_priv(shost);
265
266 fc_host_speed(shost) = lport->link_speed;
267}
268EXPORT_SYMBOL(fc_get_host_speed);
269
270struct fc_host_statistics *fc_get_host_stats(struct Scsi_Host *shost)
271{
Robert Love42e9a922008-12-09 15:10:17 -0800272 struct fc_host_statistics *fcoe_stats;
273 struct fc_lport *lp = shost_priv(shost);
274 struct timespec v0, v1;
Robert Love582b45b2009-03-31 15:51:50 -0700275 unsigned int cpu;
Robert Love42e9a922008-12-09 15:10:17 -0800276
277 fcoe_stats = &lp->host_stats;
278 memset(fcoe_stats, 0, sizeof(struct fc_host_statistics));
279
280 jiffies_to_timespec(jiffies, &v0);
281 jiffies_to_timespec(lp->boot_time, &v1);
282 fcoe_stats->seconds_since_last_reset = (v0.tv_sec - v1.tv_sec);
283
Robert Love582b45b2009-03-31 15:51:50 -0700284 for_each_possible_cpu(cpu) {
285 struct fcoe_dev_stats *stats;
286
287 stats = per_cpu_ptr(lp->dev_stats, cpu);
288
Robert Love42e9a922008-12-09 15:10:17 -0800289 fcoe_stats->tx_frames += stats->TxFrames;
290 fcoe_stats->tx_words += stats->TxWords;
291 fcoe_stats->rx_frames += stats->RxFrames;
292 fcoe_stats->rx_words += stats->RxWords;
293 fcoe_stats->error_frames += stats->ErrorFrames;
294 fcoe_stats->invalid_crc_count += stats->InvalidCRCCount;
295 fcoe_stats->fcp_input_requests += stats->InputRequests;
296 fcoe_stats->fcp_output_requests += stats->OutputRequests;
297 fcoe_stats->fcp_control_requests += stats->ControlRequests;
298 fcoe_stats->fcp_input_megabytes += stats->InputMegabytes;
299 fcoe_stats->fcp_output_megabytes += stats->OutputMegabytes;
300 fcoe_stats->link_failure_count += stats->LinkFailureCount;
301 }
302 fcoe_stats->lip_count = -1;
303 fcoe_stats->nos_count = -1;
304 fcoe_stats->loss_of_sync_count = -1;
305 fcoe_stats->loss_of_signal_count = -1;
306 fcoe_stats->prim_seq_protocol_err_count = -1;
307 fcoe_stats->dumped_frames = -1;
308 return fcoe_stats;
309}
310EXPORT_SYMBOL(fc_get_host_stats);
311
312/*
313 * Fill in FLOGI command for request.
314 */
315static void
316fc_lport_flogi_fill(struct fc_lport *lport, struct fc_els_flogi *flogi,
317 unsigned int op)
318{
319 struct fc_els_csp *sp;
320 struct fc_els_cssp *cp;
321
322 memset(flogi, 0, sizeof(*flogi));
323 flogi->fl_cmd = (u8) op;
324 put_unaligned_be64(lport->wwpn, &flogi->fl_wwpn);
325 put_unaligned_be64(lport->wwnn, &flogi->fl_wwnn);
326 sp = &flogi->fl_csp;
327 sp->sp_hi_ver = 0x20;
328 sp->sp_lo_ver = 0x20;
329 sp->sp_bb_cred = htons(10); /* this gets set by gateway */
330 sp->sp_bb_data = htons((u16) lport->mfs);
331 cp = &flogi->fl_cssp[3 - 1]; /* class 3 parameters */
332 cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
333 if (op != ELS_FLOGI) {
334 sp->sp_features = htons(FC_SP_FT_CIRO);
335 sp->sp_tot_seq = htons(255); /* seq. we accept */
336 sp->sp_rel_off = htons(0x1f);
337 sp->sp_e_d_tov = htonl(lport->e_d_tov);
338
339 cp->cp_rdfs = htons((u16) lport->mfs);
340 cp->cp_con_seq = htons(255);
341 cp->cp_open_seq = 1;
342 }
343}
344
345/*
346 * Add a supported FC-4 type.
347 */
348static void fc_lport_add_fc4_type(struct fc_lport *lport, enum fc_fh_type type)
349{
350 __be32 *mp;
351
352 mp = &lport->fcts.ff_type_map[type / FC_NS_BPW];
353 *mp = htonl(ntohl(*mp) | 1UL << (type % FC_NS_BPW));
354}
355
356/**
Robert Love34f42a02009-02-27 10:55:45 -0800357 * fc_lport_recv_rlir_req() - Handle received Registered Link Incident Report.
Robert Love42e9a922008-12-09 15:10:17 -0800358 * @lport: Fibre Channel local port recieving the RLIR
359 * @sp: current sequence in the RLIR exchange
360 * @fp: RLIR request frame
361 *
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700362 * Locking Note: The lport lock is expected to be held before calling
Robert Love42e9a922008-12-09 15:10:17 -0800363 * this function.
364 */
365static void fc_lport_recv_rlir_req(struct fc_seq *sp, struct fc_frame *fp,
366 struct fc_lport *lport)
367{
Robert Love74147052009-06-10 15:31:10 -0700368 FC_LPORT_DBG(lport, "Received RLIR request while in state %s\n",
369 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800370
371 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
372 fc_frame_free(fp);
373}
374
375/**
Robert Love34f42a02009-02-27 10:55:45 -0800376 * fc_lport_recv_echo_req() - Handle received ECHO request
Robert Love42e9a922008-12-09 15:10:17 -0800377 * @lport: Fibre Channel local port recieving the ECHO
378 * @sp: current sequence in the ECHO exchange
379 * @fp: ECHO request frame
380 *
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700381 * Locking Note: The lport lock is expected to be held before calling
Robert Love42e9a922008-12-09 15:10:17 -0800382 * this function.
383 */
384static void fc_lport_recv_echo_req(struct fc_seq *sp, struct fc_frame *in_fp,
385 struct fc_lport *lport)
386{
387 struct fc_frame *fp;
388 struct fc_exch *ep = fc_seq_exch(sp);
389 unsigned int len;
390 void *pp;
391 void *dp;
392 u32 f_ctl;
393
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700394 FC_LPORT_DBG(lport, "Received ECHO request while in state %s\n",
Robert Love74147052009-06-10 15:31:10 -0700395 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800396
397 len = fr_len(in_fp) - sizeof(struct fc_frame_header);
398 pp = fc_frame_payload_get(in_fp, len);
399
400 if (len < sizeof(__be32))
401 len = sizeof(__be32);
402
403 fp = fc_frame_alloc(lport, len);
404 if (fp) {
405 dp = fc_frame_payload_get(fp, len);
406 memcpy(dp, pp, len);
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700407 *((__be32 *)dp) = htonl(ELS_LS_ACC << 24);
Robert Love42e9a922008-12-09 15:10:17 -0800408 sp = lport->tt.seq_start_next(sp);
409 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
410 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
411 FC_TYPE_ELS, f_ctl, 0);
412 lport->tt.seq_send(lport, sp, fp);
413 }
414 fc_frame_free(in_fp);
415}
416
417/**
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700418 * fc_lport_recv_rnid_req() - Handle received Request Node ID data request
419 * @sp: The sequence in the RNID exchange
420 * @fp: The RNID request frame
421 * @lport: The local port recieving the RNID
Robert Love42e9a922008-12-09 15:10:17 -0800422 *
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700423 * Locking Note: The lport lock is expected to be held before calling
Robert Love42e9a922008-12-09 15:10:17 -0800424 * this function.
425 */
426static void fc_lport_recv_rnid_req(struct fc_seq *sp, struct fc_frame *in_fp,
427 struct fc_lport *lport)
428{
429 struct fc_frame *fp;
430 struct fc_exch *ep = fc_seq_exch(sp);
431 struct fc_els_rnid *req;
432 struct {
433 struct fc_els_rnid_resp rnid;
434 struct fc_els_rnid_cid cid;
435 struct fc_els_rnid_gen gen;
436 } *rp;
437 struct fc_seq_els_data rjt_data;
438 u8 fmt;
439 size_t len;
440 u32 f_ctl;
441
Robert Love74147052009-06-10 15:31:10 -0700442 FC_LPORT_DBG(lport, "Received RNID request while in state %s\n",
443 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800444
445 req = fc_frame_payload_get(in_fp, sizeof(*req));
446 if (!req) {
447 rjt_data.fp = NULL;
448 rjt_data.reason = ELS_RJT_LOGIC;
449 rjt_data.explan = ELS_EXPL_NONE;
450 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
451 } else {
452 fmt = req->rnid_fmt;
453 len = sizeof(*rp);
454 if (fmt != ELS_RNIDF_GEN ||
455 ntohl(lport->rnid_gen.rnid_atype) == 0) {
456 fmt = ELS_RNIDF_NONE; /* nothing to provide */
457 len -= sizeof(rp->gen);
458 }
459 fp = fc_frame_alloc(lport, len);
460 if (fp) {
461 rp = fc_frame_payload_get(fp, len);
462 memset(rp, 0, len);
463 rp->rnid.rnid_cmd = ELS_LS_ACC;
464 rp->rnid.rnid_fmt = fmt;
465 rp->rnid.rnid_cid_len = sizeof(rp->cid);
466 rp->cid.rnid_wwpn = htonll(lport->wwpn);
467 rp->cid.rnid_wwnn = htonll(lport->wwnn);
468 if (fmt == ELS_RNIDF_GEN) {
469 rp->rnid.rnid_sid_len = sizeof(rp->gen);
470 memcpy(&rp->gen, &lport->rnid_gen,
471 sizeof(rp->gen));
472 }
473 sp = lport->tt.seq_start_next(sp);
474 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
475 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
476 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
477 FC_TYPE_ELS, f_ctl, 0);
478 lport->tt.seq_send(lport, sp, fp);
479 }
480 }
481 fc_frame_free(in_fp);
482}
483
484/**
Robert Love34f42a02009-02-27 10:55:45 -0800485 * fc_lport_recv_logo_req() - Handle received fabric LOGO request
Robert Love42e9a922008-12-09 15:10:17 -0800486 * @lport: Fibre Channel local port recieving the LOGO
487 * @sp: current sequence in the LOGO exchange
488 * @fp: LOGO request frame
489 *
490 * Locking Note: The lport lock is exected to be held before calling
491 * this function.
492 */
493static void fc_lport_recv_logo_req(struct fc_seq *sp, struct fc_frame *fp,
494 struct fc_lport *lport)
495{
496 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
497 fc_lport_enter_reset(lport);
498 fc_frame_free(fp);
499}
500
501/**
Robert Love34f42a02009-02-27 10:55:45 -0800502 * fc_fabric_login() - Start the lport state machine
Robert Love42e9a922008-12-09 15:10:17 -0800503 * @lport: The lport that should log into the fabric
504 *
505 * Locking Note: This function should not be called
506 * with the lport lock held.
507 */
508int fc_fabric_login(struct fc_lport *lport)
509{
510 int rc = -1;
511
512 mutex_lock(&lport->lp_mutex);
Joe Eykholtb1d9fd52009-07-29 17:04:22 -0700513 if (lport->state == LPORT_ST_DISABLED) {
Robert Love42e9a922008-12-09 15:10:17 -0800514 fc_lport_enter_reset(lport);
515 rc = 0;
516 }
517 mutex_unlock(&lport->lp_mutex);
518
519 return rc;
520}
521EXPORT_SYMBOL(fc_fabric_login);
522
523/**
Chris Leech8faecdd2009-11-03 11:46:19 -0800524 * __fc_linkup() - Handler for transport linkup events
525 * @lport: The lport whose link is up
526 *
527 * Locking: must be called with the lp_mutex held
528 */
529void __fc_linkup(struct fc_lport *lport)
530{
531 if (!lport->link_up) {
532 lport->link_up = 1;
533
534 if (lport->state == LPORT_ST_RESET)
535 fc_lport_enter_flogi(lport);
536 }
537}
538
539/**
Robert Love34f42a02009-02-27 10:55:45 -0800540 * fc_linkup() - Handler for transport linkup events
Robert Love42e9a922008-12-09 15:10:17 -0800541 * @lport: The lport whose link is up
542 */
543void fc_linkup(struct fc_lport *lport)
544{
Robert Love74147052009-06-10 15:31:10 -0700545 printk(KERN_INFO "libfc: Link up on port (%6x)\n",
546 fc_host_port_id(lport->host));
Robert Love42e9a922008-12-09 15:10:17 -0800547
548 mutex_lock(&lport->lp_mutex);
Chris Leech8faecdd2009-11-03 11:46:19 -0800549 __fc_linkup(lport);
Robert Love42e9a922008-12-09 15:10:17 -0800550 mutex_unlock(&lport->lp_mutex);
551}
552EXPORT_SYMBOL(fc_linkup);
553
554/**
Chris Leech8faecdd2009-11-03 11:46:19 -0800555 * __fc_linkdown() - Handler for transport linkdown events
556 * @lport: The lport whose link is down
557 *
558 * Locking: must be called with the lp_mutex held
559 */
560void __fc_linkdown(struct fc_lport *lport)
561{
562 if (lport->link_up) {
563 lport->link_up = 0;
564 fc_lport_enter_reset(lport);
565 lport->tt.fcp_cleanup(lport);
566 }
567}
568
569/**
Robert Love34f42a02009-02-27 10:55:45 -0800570 * fc_linkdown() - Handler for transport linkdown events
Robert Love42e9a922008-12-09 15:10:17 -0800571 * @lport: The lport whose link is down
572 */
573void fc_linkdown(struct fc_lport *lport)
574{
Robert Love74147052009-06-10 15:31:10 -0700575 printk(KERN_INFO "libfc: Link down on port (%6x)\n",
576 fc_host_port_id(lport->host));
Robert Love42e9a922008-12-09 15:10:17 -0800577
Chris Leech8faecdd2009-11-03 11:46:19 -0800578 mutex_lock(&lport->lp_mutex);
579 __fc_linkdown(lport);
Robert Love42e9a922008-12-09 15:10:17 -0800580 mutex_unlock(&lport->lp_mutex);
581}
582EXPORT_SYMBOL(fc_linkdown);
583
584/**
Robert Love34f42a02009-02-27 10:55:45 -0800585 * fc_fabric_logoff() - Logout of the fabric
Robert Love42e9a922008-12-09 15:10:17 -0800586 * @lport: fc_lport pointer to logoff the fabric
587 *
588 * Return value:
589 * 0 for success, -1 for failure
Robert Love34f42a02009-02-27 10:55:45 -0800590 */
Robert Love42e9a922008-12-09 15:10:17 -0800591int fc_fabric_logoff(struct fc_lport *lport)
592{
593 lport->tt.disc_stop_final(lport);
594 mutex_lock(&lport->lp_mutex);
Abhijeet Joglekara0fd2e42009-04-21 16:27:09 -0700595 if (lport->dns_rp)
596 lport->tt.rport_logoff(lport->dns_rp);
597 mutex_unlock(&lport->lp_mutex);
598 lport->tt.rport_flush_queue();
599 mutex_lock(&lport->lp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800600 fc_lport_enter_logo(lport);
601 mutex_unlock(&lport->lp_mutex);
Steve Maf7db2c12009-02-27 10:55:13 -0800602 cancel_delayed_work_sync(&lport->retry_work);
Robert Love42e9a922008-12-09 15:10:17 -0800603 return 0;
604}
605EXPORT_SYMBOL(fc_fabric_logoff);
606
607/**
Robert Love34f42a02009-02-27 10:55:45 -0800608 * fc_lport_destroy() - unregister a fc_lport
Robert Love42e9a922008-12-09 15:10:17 -0800609 * @lport: fc_lport pointer to unregister
610 *
611 * Return value:
612 * None
613 * Note:
614 * exit routine for fc_lport instance
615 * clean-up all the allocated memory
616 * and free up other system resources.
617 *
Robert Love34f42a02009-02-27 10:55:45 -0800618 */
Robert Love42e9a922008-12-09 15:10:17 -0800619int fc_lport_destroy(struct fc_lport *lport)
620{
Abhijeet Joglekarbbf15662009-04-21 16:27:14 -0700621 mutex_lock(&lport->lp_mutex);
Joe Eykholtb1d9fd52009-07-29 17:04:22 -0700622 lport->state = LPORT_ST_DISABLED;
Abhijeet Joglekarbbf15662009-04-21 16:27:14 -0700623 lport->link_up = 0;
Robert Love42e9a922008-12-09 15:10:17 -0800624 lport->tt.frame_send = fc_frame_drop;
Abhijeet Joglekarbbf15662009-04-21 16:27:14 -0700625 mutex_unlock(&lport->lp_mutex);
626
Robert Love42e9a922008-12-09 15:10:17 -0800627 lport->tt.fcp_abort_io(lport);
Joe Eykholte9ba8b42009-07-29 17:04:33 -0700628 lport->tt.disc_stop_final(lport);
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -0800629 lport->tt.exch_mgr_reset(lport, 0, 0);
Robert Love42e9a922008-12-09 15:10:17 -0800630 return 0;
631}
632EXPORT_SYMBOL(fc_lport_destroy);
633
634/**
Robert Love34f42a02009-02-27 10:55:45 -0800635 * fc_set_mfs() - sets up the mfs for the corresponding fc_lport
Robert Love42e9a922008-12-09 15:10:17 -0800636 * @lport: fc_lport pointer to unregister
637 * @mfs: the new mfs for fc_lport
638 *
639 * Set mfs for the given fc_lport to the new mfs.
640 *
641 * Return: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800642 */
Robert Love42e9a922008-12-09 15:10:17 -0800643int fc_set_mfs(struct fc_lport *lport, u32 mfs)
644{
645 unsigned int old_mfs;
646 int rc = -EINVAL;
647
648 mutex_lock(&lport->lp_mutex);
649
650 old_mfs = lport->mfs;
651
652 if (mfs >= FC_MIN_MAX_FRAME) {
653 mfs &= ~3;
654 if (mfs > FC_MAX_FRAME)
655 mfs = FC_MAX_FRAME;
656 mfs -= sizeof(struct fc_frame_header);
657 lport->mfs = mfs;
658 rc = 0;
659 }
660
661 if (!rc && mfs < old_mfs)
662 fc_lport_enter_reset(lport);
663
664 mutex_unlock(&lport->lp_mutex);
665
666 return rc;
667}
668EXPORT_SYMBOL(fc_set_mfs);
669
670/**
Robert Love34f42a02009-02-27 10:55:45 -0800671 * fc_lport_disc_callback() - Callback for discovery events
Robert Love42e9a922008-12-09 15:10:17 -0800672 * @lport: FC local port
673 * @event: The discovery event
674 */
675void fc_lport_disc_callback(struct fc_lport *lport, enum fc_disc_event event)
676{
677 switch (event) {
678 case DISC_EV_SUCCESS:
Robert Love74147052009-06-10 15:31:10 -0700679 FC_LPORT_DBG(lport, "Discovery succeeded\n");
Robert Love42e9a922008-12-09 15:10:17 -0800680 break;
681 case DISC_EV_FAILED:
Robert Love74147052009-06-10 15:31:10 -0700682 printk(KERN_ERR "libfc: Discovery failed for port (%6x)\n",
683 fc_host_port_id(lport->host));
Robert Love42e9a922008-12-09 15:10:17 -0800684 mutex_lock(&lport->lp_mutex);
685 fc_lport_enter_reset(lport);
686 mutex_unlock(&lport->lp_mutex);
687 break;
688 case DISC_EV_NONE:
689 WARN_ON(1);
690 break;
691 }
692}
693
694/**
Robert Love34f42a02009-02-27 10:55:45 -0800695 * fc_rport_enter_ready() - Enter the ready state and start discovery
Robert Love42e9a922008-12-09 15:10:17 -0800696 * @lport: Fibre Channel local port that is ready
697 *
698 * Locking Note: The lport lock is expected to be held before calling
699 * this routine.
700 */
701static void fc_lport_enter_ready(struct fc_lport *lport)
702{
Robert Love74147052009-06-10 15:31:10 -0700703 FC_LPORT_DBG(lport, "Entered READY from state %s\n",
704 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800705
706 fc_lport_state_enter(lport, LPORT_ST_READY);
Chris Leech8faecdd2009-11-03 11:46:19 -0800707 if (lport->vport)
708 fc_vport_set_state(lport->vport, FC_VPORT_ACTIVE);
709 fc_vports_linkchange(lport);
Robert Love42e9a922008-12-09 15:10:17 -0800710
Joe Eykholt29d898e2009-08-25 14:02:49 -0700711 if (!lport->ptp_rp)
712 lport->tt.disc_start(fc_lport_disc_callback, lport);
Robert Love42e9a922008-12-09 15:10:17 -0800713}
714
715/**
Robert Love34f42a02009-02-27 10:55:45 -0800716 * fc_lport_recv_flogi_req() - Receive a FLOGI request
Robert Love42e9a922008-12-09 15:10:17 -0800717 * @sp_in: The sequence the FLOGI is on
718 * @rx_fp: The frame the FLOGI is in
719 * @lport: The lport that recieved the request
720 *
721 * A received FLOGI request indicates a point-to-point connection.
722 * Accept it with the common service parameters indicating our N port.
723 * Set up to do a PLOGI if we have the higher-number WWPN.
724 *
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700725 * Locking Note: The lport lock is expected to be held before calling
Robert Love42e9a922008-12-09 15:10:17 -0800726 * this function.
727 */
728static void fc_lport_recv_flogi_req(struct fc_seq *sp_in,
729 struct fc_frame *rx_fp,
730 struct fc_lport *lport)
731{
732 struct fc_frame *fp;
733 struct fc_frame_header *fh;
734 struct fc_seq *sp;
735 struct fc_exch *ep;
736 struct fc_els_flogi *flp;
737 struct fc_els_flogi *new_flp;
738 u64 remote_wwpn;
739 u32 remote_fid;
740 u32 local_fid;
741 u32 f_ctl;
742
Robert Love74147052009-06-10 15:31:10 -0700743 FC_LPORT_DBG(lport, "Received FLOGI request while in state %s\n",
744 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800745
746 fh = fc_frame_header_get(rx_fp);
747 remote_fid = ntoh24(fh->fh_s_id);
748 flp = fc_frame_payload_get(rx_fp, sizeof(*flp));
749 if (!flp)
750 goto out;
751 remote_wwpn = get_unaligned_be64(&flp->fl_wwpn);
752 if (remote_wwpn == lport->wwpn) {
Robert Love74147052009-06-10 15:31:10 -0700753 printk(KERN_WARNING "libfc: Received FLOGI from port "
754 "with same WWPN %llx\n", remote_wwpn);
Robert Love42e9a922008-12-09 15:10:17 -0800755 goto out;
756 }
Robert Love74147052009-06-10 15:31:10 -0700757 FC_LPORT_DBG(lport, "FLOGI from port WWPN %llx\n", remote_wwpn);
Robert Love42e9a922008-12-09 15:10:17 -0800758
759 /*
760 * XXX what is the right thing to do for FIDs?
761 * The originator might expect our S_ID to be 0xfffffe.
762 * But if so, both of us could end up with the same FID.
763 */
764 local_fid = FC_LOCAL_PTP_FID_LO;
765 if (remote_wwpn < lport->wwpn) {
766 local_fid = FC_LOCAL_PTP_FID_HI;
767 if (!remote_fid || remote_fid == local_fid)
768 remote_fid = FC_LOCAL_PTP_FID_LO;
769 } else if (!remote_fid) {
770 remote_fid = FC_LOCAL_PTP_FID_HI;
771 }
772
773 fc_host_port_id(lport->host) = local_fid;
774
775 fp = fc_frame_alloc(lport, sizeof(*flp));
776 if (fp) {
777 sp = lport->tt.seq_start_next(fr_seq(rx_fp));
778 new_flp = fc_frame_payload_get(fp, sizeof(*flp));
779 fc_lport_flogi_fill(lport, new_flp, ELS_FLOGI);
780 new_flp->fl_cmd = (u8) ELS_LS_ACC;
781
782 /*
783 * Send the response. If this fails, the originator should
784 * repeat the sequence.
785 */
786 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
787 ep = fc_seq_exch(sp);
788 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
789 FC_TYPE_ELS, f_ctl, 0);
790 lport->tt.seq_send(lport, sp, fp);
791
792 } else {
793 fc_lport_error(lport, fp);
794 }
795 fc_lport_ptp_setup(lport, remote_fid, remote_wwpn,
796 get_unaligned_be64(&flp->fl_wwnn));
797
Robert Love42e9a922008-12-09 15:10:17 -0800798out:
799 sp = fr_seq(rx_fp);
800 fc_frame_free(rx_fp);
801}
802
803/**
Robert Love34f42a02009-02-27 10:55:45 -0800804 * fc_lport_recv_req() - The generic lport request handler
Robert Love42e9a922008-12-09 15:10:17 -0800805 * @lport: The lport that received the request
806 * @sp: The sequence the request is on
807 * @fp: The frame the request is in
808 *
809 * This function will see if the lport handles the request or
810 * if an rport should handle the request.
811 *
812 * Locking Note: This function should not be called with the lport
813 * lock held becuase it will grab the lock.
814 */
815static void fc_lport_recv_req(struct fc_lport *lport, struct fc_seq *sp,
816 struct fc_frame *fp)
817{
818 struct fc_frame_header *fh = fc_frame_header_get(fp);
819 void (*recv) (struct fc_seq *, struct fc_frame *, struct fc_lport *);
Robert Love42e9a922008-12-09 15:10:17 -0800820
821 mutex_lock(&lport->lp_mutex);
822
823 /*
824 * Handle special ELS cases like FLOGI, LOGO, and
825 * RSCN here. These don't require a session.
826 * Even if we had a session, it might not be ready.
827 */
Joe Eykholte9ba8b42009-07-29 17:04:33 -0700828 if (!lport->link_up)
829 fc_frame_free(fp);
830 else if (fh->fh_type == FC_TYPE_ELS &&
831 fh->fh_r_ctl == FC_RCTL_ELS_REQ) {
Robert Love42e9a922008-12-09 15:10:17 -0800832 /*
833 * Check opcode.
834 */
Joe Eykholt131203a2009-08-25 14:03:10 -0700835 recv = lport->tt.rport_recv_req;
Robert Love42e9a922008-12-09 15:10:17 -0800836 switch (fc_frame_payload_op(fp)) {
837 case ELS_FLOGI:
838 recv = fc_lport_recv_flogi_req;
839 break;
840 case ELS_LOGO:
841 fh = fc_frame_header_get(fp);
842 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI)
843 recv = fc_lport_recv_logo_req;
844 break;
845 case ELS_RSCN:
846 recv = lport->tt.disc_recv_req;
847 break;
848 case ELS_ECHO:
849 recv = fc_lport_recv_echo_req;
850 break;
851 case ELS_RLIR:
852 recv = fc_lport_recv_rlir_req;
853 break;
854 case ELS_RNID:
855 recv = fc_lport_recv_rnid_req;
856 break;
Robert Love42e9a922008-12-09 15:10:17 -0800857 }
858
Joe Eykholt131203a2009-08-25 14:03:10 -0700859 recv(sp, fp, lport);
Robert Love42e9a922008-12-09 15:10:17 -0800860 } else {
Robert Love74147052009-06-10 15:31:10 -0700861 FC_LPORT_DBG(lport, "dropping invalid frame (eof %x)\n",
862 fr_eof(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800863 fc_frame_free(fp);
864 }
865 mutex_unlock(&lport->lp_mutex);
866
867 /*
868 * The common exch_done for all request may not be good
869 * if any request requires longer hold on exhange. XXX
870 */
871 lport->tt.exch_done(sp);
872}
873
874/**
Robert Love34f42a02009-02-27 10:55:45 -0800875 * fc_lport_reset() - Reset an lport
Robert Love42e9a922008-12-09 15:10:17 -0800876 * @lport: The lport which should be reset
877 *
878 * Locking Note: This functions should not be called with the
879 * lport lock held.
880 */
881int fc_lport_reset(struct fc_lport *lport)
882{
Steve Maf7db2c12009-02-27 10:55:13 -0800883 cancel_delayed_work_sync(&lport->retry_work);
Robert Love42e9a922008-12-09 15:10:17 -0800884 mutex_lock(&lport->lp_mutex);
885 fc_lport_enter_reset(lport);
886 mutex_unlock(&lport->lp_mutex);
887 return 0;
888}
889EXPORT_SYMBOL(fc_lport_reset);
890
891/**
Joe Eykholt1190d922009-07-29 17:04:27 -0700892 * fc_lport_reset_locked() - Reset the local port
Robert Love42e9a922008-12-09 15:10:17 -0800893 * @lport: Fibre Channel local port to be reset
894 *
895 * Locking Note: The lport lock is expected to be held before calling
896 * this routine.
897 */
Joe Eykholt1190d922009-07-29 17:04:27 -0700898static void fc_lport_reset_locked(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -0800899{
Robert Love42e9a922008-12-09 15:10:17 -0800900 if (lport->dns_rp)
901 lport->tt.rport_logoff(lport->dns_rp);
902
Joe Eykholt48f00902009-08-25 14:01:50 -0700903 lport->ptp_rp = NULL;
Robert Love42e9a922008-12-09 15:10:17 -0800904
905 lport->tt.disc_stop(lport);
906
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -0800907 lport->tt.exch_mgr_reset(lport, 0, 0);
Robert Love42e9a922008-12-09 15:10:17 -0800908 fc_host_fabric_name(lport->host) = 0;
909 fc_host_port_id(lport->host) = 0;
Joe Eykholt1190d922009-07-29 17:04:27 -0700910}
Robert Love42e9a922008-12-09 15:10:17 -0800911
Joe Eykholt1190d922009-07-29 17:04:27 -0700912/**
913 * fc_lport_enter_reset() - Reset the local port
914 * @lport: Fibre Channel local port to be reset
915 *
916 * Locking Note: The lport lock is expected to be held before calling
917 * this routine.
918 */
919static void fc_lport_enter_reset(struct fc_lport *lport)
920{
921 FC_LPORT_DBG(lport, "Entered RESET state from %s state\n",
922 fc_lport_state(lport));
923
Chris Leech8faecdd2009-11-03 11:46:19 -0800924 if (lport->vport) {
925 if (lport->link_up)
926 fc_vport_set_state(lport->vport, FC_VPORT_INITIALIZING);
927 else
928 fc_vport_set_state(lport->vport, FC_VPORT_LINKDOWN);
929 }
Joe Eykholt1190d922009-07-29 17:04:27 -0700930 fc_lport_state_enter(lport, LPORT_ST_RESET);
Chris Leech8faecdd2009-11-03 11:46:19 -0800931 fc_vports_linkchange(lport);
Joe Eykholt1190d922009-07-29 17:04:27 -0700932 fc_lport_reset_locked(lport);
Vasu Devbc0e17f2009-02-27 10:54:57 -0800933 if (lport->link_up)
Robert Love42e9a922008-12-09 15:10:17 -0800934 fc_lport_enter_flogi(lport);
935}
936
937/**
Joe Eykholt1190d922009-07-29 17:04:27 -0700938 * fc_lport_enter_disabled() - disable the local port
939 * @lport: Fibre Channel local port to be reset
940 *
941 * Locking Note: The lport lock is expected to be held before calling
942 * this routine.
943 */
944static void fc_lport_enter_disabled(struct fc_lport *lport)
945{
946 FC_LPORT_DBG(lport, "Entered disabled state from %s state\n",
947 fc_lport_state(lport));
948
949 fc_lport_state_enter(lport, LPORT_ST_DISABLED);
Chris Leech8faecdd2009-11-03 11:46:19 -0800950 fc_vports_linkchange(lport);
Joe Eykholt1190d922009-07-29 17:04:27 -0700951 fc_lport_reset_locked(lport);
952}
953
954/**
Robert Love34f42a02009-02-27 10:55:45 -0800955 * fc_lport_error() - Handler for any errors
Robert Love42e9a922008-12-09 15:10:17 -0800956 * @lport: The fc_lport object
957 * @fp: The frame pointer
958 *
959 * If the error was caused by a resource allocation failure
960 * then wait for half a second and retry, otherwise retry
961 * after the e_d_tov time.
962 */
963static void fc_lport_error(struct fc_lport *lport, struct fc_frame *fp)
964{
965 unsigned long delay = 0;
Robert Love74147052009-06-10 15:31:10 -0700966 FC_LPORT_DBG(lport, "Error %ld in state %s, retries %d\n",
967 PTR_ERR(fp), fc_lport_state(lport),
968 lport->retry_count);
Robert Love42e9a922008-12-09 15:10:17 -0800969
970 if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
971 /*
972 * Memory allocation failure, or the exchange timed out.
973 * Retry after delay
974 */
975 if (lport->retry_count < lport->max_retry_count) {
976 lport->retry_count++;
977 if (!fp)
978 delay = msecs_to_jiffies(500);
979 else
980 delay = msecs_to_jiffies(lport->e_d_tov);
981
982 schedule_delayed_work(&lport->retry_work, delay);
983 } else {
984 switch (lport->state) {
Joe Eykholtb1d9fd52009-07-29 17:04:22 -0700985 case LPORT_ST_DISABLED:
Robert Love42e9a922008-12-09 15:10:17 -0800986 case LPORT_ST_READY:
987 case LPORT_ST_RESET:
Chris Leechc9c7bd72009-11-03 11:46:51 -0800988 case LPORT_ST_RNN_ID:
Chris Leech5baa17c2009-11-03 11:46:56 -0800989 case LPORT_ST_RSNN_NN:
Chris Leechc9866a52009-11-03 11:47:01 -0800990 case LPORT_ST_RSPN_ID:
Robert Love42e9a922008-12-09 15:10:17 -0800991 case LPORT_ST_RFT_ID:
992 case LPORT_ST_SCR:
993 case LPORT_ST_DNS:
994 case LPORT_ST_FLOGI:
995 case LPORT_ST_LOGO:
996 fc_lport_enter_reset(lport);
997 break;
998 }
999 }
1000 }
1001}
1002
1003/**
Chris Leech7cccc152009-11-03 11:47:07 -08001004 * fc_lport_ns_resp() - Handle response to a name server
1005 * registration exchange
1006 * @sp: current sequence in exchange
Robert Love42e9a922008-12-09 15:10:17 -08001007 * @fp: response frame
1008 * @lp_arg: Fibre Channel host port instance
1009 *
1010 * Locking Note: This function will be called without the lport lock
1011 * held, but it will lock, call an _enter_* function or fc_lport_error
1012 * and then unlock the lport.
1013 */
Chris Leech7cccc152009-11-03 11:47:07 -08001014static void fc_lport_ns_resp(struct fc_seq *sp, struct fc_frame *fp,
1015 void *lp_arg)
Robert Love42e9a922008-12-09 15:10:17 -08001016{
1017 struct fc_lport *lport = lp_arg;
1018 struct fc_frame_header *fh;
1019 struct fc_ct_hdr *ct;
1020
Chris Leech7cccc152009-11-03 11:47:07 -08001021 FC_LPORT_DBG(lport, "Received a ns %s\n", fc_els_resp_type(fp));
Joe Eykholtf657d292009-08-25 14:03:21 -07001022
Robert Love42e9a922008-12-09 15:10:17 -08001023 if (fp == ERR_PTR(-FC_EX_CLOSED))
1024 return;
1025
1026 mutex_lock(&lport->lp_mutex);
1027
Chris Leech7cccc152009-11-03 11:47:07 -08001028 if (lport->state < LPORT_ST_RNN_ID || lport->state > LPORT_ST_RFT_ID) {
1029 FC_LPORT_DBG(lport, "Received a name server response, "
1030 "but in state %s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001031 if (IS_ERR(fp))
1032 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001033 goto out;
1034 }
1035
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001036 if (IS_ERR(fp)) {
1037 fc_lport_error(lport, fp);
1038 goto err;
1039 }
1040
Robert Love42e9a922008-12-09 15:10:17 -08001041 fh = fc_frame_header_get(fp);
1042 ct = fc_frame_payload_get(fp, sizeof(*ct));
1043
1044 if (fh && ct && fh->fh_type == FC_TYPE_CT &&
1045 ct->ct_fs_type == FC_FST_DIR &&
1046 ct->ct_fs_subtype == FC_NS_SUBTYPE &&
1047 ntohs(ct->ct_cmd) == FC_FS_ACC)
Chris Leech7cccc152009-11-03 11:47:07 -08001048 switch (lport->state) {
1049 case LPORT_ST_RNN_ID:
Chris Leechc914f7d2009-11-03 11:47:12 -08001050 fc_lport_enter_ns(lport, LPORT_ST_RSNN_NN);
Chris Leech7cccc152009-11-03 11:47:07 -08001051 break;
1052 case LPORT_ST_RSNN_NN:
Chris Leechc914f7d2009-11-03 11:47:12 -08001053 fc_lport_enter_ns(lport, LPORT_ST_RSPN_ID);
Chris Leech7cccc152009-11-03 11:47:07 -08001054 break;
1055 case LPORT_ST_RSPN_ID:
Chris Leechc914f7d2009-11-03 11:47:12 -08001056 fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
Chris Leech7cccc152009-11-03 11:47:07 -08001057 break;
1058 case LPORT_ST_RFT_ID:
1059 fc_lport_enter_scr(lport);
1060 break;
1061 default:
1062 /* should have already been caught by state checks */
1063 break;
1064 }
Robert Love42e9a922008-12-09 15:10:17 -08001065 else
1066 fc_lport_error(lport, fp);
1067out:
1068 fc_frame_free(fp);
1069err:
1070 mutex_unlock(&lport->lp_mutex);
1071}
1072
1073/**
Robert Love34f42a02009-02-27 10:55:45 -08001074 * fc_lport_scr_resp() - Handle response to State Change Register (SCR) request
Robert Love42e9a922008-12-09 15:10:17 -08001075 * @sp: current sequence in SCR exchange
1076 * @fp: response frame
1077 * @lp_arg: Fibre Channel lport port instance that sent the registration request
1078 *
1079 * Locking Note: This function will be called without the lport lock
1080 * held, but it will lock, call an _enter_* function or fc_lport_error
1081 * and then unlock the lport.
1082 */
1083static void fc_lport_scr_resp(struct fc_seq *sp, struct fc_frame *fp,
1084 void *lp_arg)
1085{
1086 struct fc_lport *lport = lp_arg;
1087 u8 op;
1088
Joe Eykholtf657d292009-08-25 14:03:21 -07001089 FC_LPORT_DBG(lport, "Received a SCR %s\n", fc_els_resp_type(fp));
1090
Robert Love42e9a922008-12-09 15:10:17 -08001091 if (fp == ERR_PTR(-FC_EX_CLOSED))
1092 return;
1093
1094 mutex_lock(&lport->lp_mutex);
1095
Robert Love42e9a922008-12-09 15:10:17 -08001096 if (lport->state != LPORT_ST_SCR) {
Robert Love74147052009-06-10 15:31:10 -07001097 FC_LPORT_DBG(lport, "Received a SCR response, but in state "
1098 "%s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001099 if (IS_ERR(fp))
1100 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001101 goto out;
1102 }
1103
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001104 if (IS_ERR(fp)) {
1105 fc_lport_error(lport, fp);
1106 goto err;
1107 }
1108
Robert Love42e9a922008-12-09 15:10:17 -08001109 op = fc_frame_payload_op(fp);
1110 if (op == ELS_LS_ACC)
1111 fc_lport_enter_ready(lport);
1112 else
1113 fc_lport_error(lport, fp);
1114
1115out:
1116 fc_frame_free(fp);
1117err:
1118 mutex_unlock(&lport->lp_mutex);
1119}
1120
1121/**
Robert Love34f42a02009-02-27 10:55:45 -08001122 * fc_lport_enter_scr() - Send a State Change Register (SCR) request
Robert Love42e9a922008-12-09 15:10:17 -08001123 * @lport: Fibre Channel local port to register for state changes
1124 *
1125 * Locking Note: The lport lock is expected to be held before calling
1126 * this routine.
1127 */
1128static void fc_lport_enter_scr(struct fc_lport *lport)
1129{
1130 struct fc_frame *fp;
1131
Robert Love74147052009-06-10 15:31:10 -07001132 FC_LPORT_DBG(lport, "Entered SCR state from %s state\n",
1133 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001134
1135 fc_lport_state_enter(lport, LPORT_ST_SCR);
1136
1137 fp = fc_frame_alloc(lport, sizeof(struct fc_els_scr));
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_FCTRL, fp, ELS_SCR,
Robert Love42e9a922008-12-09 15:10:17 -08001144 fc_lport_scr_resp, lport, lport->e_d_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001145 fc_lport_error(lport, NULL);
Robert Love42e9a922008-12-09 15:10:17 -08001146}
1147
1148/**
Chris Leechc914f7d2009-11-03 11:47:12 -08001149 * fc_lport_enter_ns() - register some object with the name server
Robert Love42e9a922008-12-09 15:10:17 -08001150 * @lport: Fibre Channel local port to register
1151 *
1152 * Locking Note: The lport lock is expected to be held before calling
1153 * this routine.
1154 */
Chris Leechc914f7d2009-11-03 11:47:12 -08001155static void fc_lport_enter_ns(struct fc_lport *lport, enum fc_lport_state state)
Robert Love42e9a922008-12-09 15:10:17 -08001156{
1157 struct fc_frame *fp;
Chris Leechc914f7d2009-11-03 11:47:12 -08001158 enum fc_ns_req cmd;
1159 int size = sizeof(struct fc_ct_hdr);
Chris Leechc9866a52009-11-03 11:47:01 -08001160 size_t len;
1161
Chris Leechc914f7d2009-11-03 11:47:12 -08001162 FC_LPORT_DBG(lport, "Entered %s state from %s state\n",
1163 fc_lport_state_names[state],
Chris Leechc9866a52009-11-03 11:47:01 -08001164 fc_lport_state(lport));
1165
Chris Leechc914f7d2009-11-03 11:47:12 -08001166 fc_lport_state_enter(lport, state);
Chris Leechc9866a52009-11-03 11:47:01 -08001167
Chris Leechc914f7d2009-11-03 11:47:12 -08001168 switch (state) {
1169 case LPORT_ST_RNN_ID:
1170 cmd = FC_NS_RNN_ID;
1171 size += sizeof(struct fc_ns_rn_id);
1172 break;
1173 case LPORT_ST_RSNN_NN:
1174 len = strnlen(fc_host_symbolic_name(lport->host), 255);
1175 /* if there is no symbolic name, skip to RFT_ID */
1176 if (!len)
1177 return fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
1178 cmd = FC_NS_RSNN_NN;
1179 size += sizeof(struct fc_ns_rsnn) + len;
1180 break;
1181 case LPORT_ST_RSPN_ID:
1182 len = strnlen(fc_host_symbolic_name(lport->host), 255);
1183 /* if there is no symbolic name, skip to RFT_ID */
1184 if (!len)
1185 return fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
1186 cmd = FC_NS_RSPN_ID;
1187 size += sizeof(struct fc_ns_rspn) + len;
1188 break;
1189 case LPORT_ST_RFT_ID:
1190 cmd = FC_NS_RFT_ID;
1191 size += sizeof(struct fc_ns_rft);
1192 break;
1193 default:
1194 fc_lport_error(lport, NULL);
1195 return;
1196 }
1197
1198 fp = fc_frame_alloc(lport, size);
Chris Leechc9866a52009-11-03 11:47:01 -08001199 if (!fp) {
1200 fc_lport_error(lport, fp);
1201 return;
1202 }
1203
Chris Leechc914f7d2009-11-03 11:47:12 -08001204 if (!lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, cmd,
Chris Leech7cccc152009-11-03 11:47:07 -08001205 fc_lport_ns_resp,
Chris Leechc9c7bd72009-11-03 11:46:51 -08001206 lport, lport->e_d_tov))
1207 fc_lport_error(lport, fp);
1208}
1209
Robert Love42e9a922008-12-09 15:10:17 -08001210static struct fc_rport_operations fc_lport_rport_ops = {
1211 .event_callback = fc_lport_rport_callback,
1212};
1213
1214/**
Robert Love34f42a02009-02-27 10:55:45 -08001215 * fc_rport_enter_dns() - Create a rport to the name server
Robert Love42e9a922008-12-09 15:10:17 -08001216 * @lport: Fibre Channel local port requesting a rport for the name server
1217 *
1218 * Locking Note: The lport lock is expected to be held before calling
1219 * this routine.
1220 */
1221static void fc_lport_enter_dns(struct fc_lport *lport)
1222{
Joe Eykholtab28f1f2009-08-25 14:00:34 -07001223 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -08001224
Robert Love74147052009-06-10 15:31:10 -07001225 FC_LPORT_DBG(lport, "Entered DNS state from %s state\n",
1226 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001227
1228 fc_lport_state_enter(lport, LPORT_ST_DNS);
1229
Joe Eykholt48f00902009-08-25 14:01:50 -07001230 mutex_lock(&lport->disc.disc_mutex);
Robert Love9737e6a2009-08-25 14:02:59 -07001231 rdata = lport->tt.rport_create(lport, FC_FID_DIR_SERV);
Joe Eykholt48f00902009-08-25 14:01:50 -07001232 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001233 if (!rdata)
Robert Love42e9a922008-12-09 15:10:17 -08001234 goto err;
1235
Robert Love42e9a922008-12-09 15:10:17 -08001236 rdata->ops = &fc_lport_rport_ops;
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001237 lport->tt.rport_login(rdata);
Robert Love42e9a922008-12-09 15:10:17 -08001238 return;
1239
1240err:
1241 fc_lport_error(lport, NULL);
1242}
1243
1244/**
Robert Love34f42a02009-02-27 10:55:45 -08001245 * fc_lport_timeout() - Handler for the retry_work timer.
Robert Love42e9a922008-12-09 15:10:17 -08001246 * @work: The work struct of the fc_lport
1247 */
1248static void fc_lport_timeout(struct work_struct *work)
1249{
1250 struct fc_lport *lport =
1251 container_of(work, struct fc_lport,
1252 retry_work.work);
1253
1254 mutex_lock(&lport->lp_mutex);
1255
1256 switch (lport->state) {
Joe Eykholtb1d9fd52009-07-29 17:04:22 -07001257 case LPORT_ST_DISABLED:
Robert Love42e9a922008-12-09 15:10:17 -08001258 WARN_ON(1);
1259 break;
Joe Eykholt22655ac2009-10-21 16:27:22 -07001260 case LPORT_ST_READY:
1261 WARN_ON(1);
1262 break;
1263 case LPORT_ST_RESET:
1264 break;
Robert Love42e9a922008-12-09 15:10:17 -08001265 case LPORT_ST_FLOGI:
1266 fc_lport_enter_flogi(lport);
1267 break;
1268 case LPORT_ST_DNS:
1269 fc_lport_enter_dns(lport);
1270 break;
Chris Leechc9c7bd72009-11-03 11:46:51 -08001271 case LPORT_ST_RNN_ID:
Chris Leech5baa17c2009-11-03 11:46:56 -08001272 case LPORT_ST_RSNN_NN:
Chris Leechc9866a52009-11-03 11:47:01 -08001273 case LPORT_ST_RSPN_ID:
Robert Love42e9a922008-12-09 15:10:17 -08001274 case LPORT_ST_RFT_ID:
Chris Leechc914f7d2009-11-03 11:47:12 -08001275 fc_lport_enter_ns(lport, lport->state);
Robert Love42e9a922008-12-09 15:10:17 -08001276 break;
1277 case LPORT_ST_SCR:
1278 fc_lport_enter_scr(lport);
1279 break;
1280 case LPORT_ST_LOGO:
1281 fc_lport_enter_logo(lport);
1282 break;
1283 }
1284
1285 mutex_unlock(&lport->lp_mutex);
1286}
1287
1288/**
Robert Love34f42a02009-02-27 10:55:45 -08001289 * fc_lport_logo_resp() - Handle response to LOGO request
Robert Love42e9a922008-12-09 15:10:17 -08001290 * @sp: current sequence in LOGO exchange
1291 * @fp: response frame
1292 * @lp_arg: Fibre Channel lport port instance that sent the LOGO request
1293 *
1294 * Locking Note: This function will be called without the lport lock
1295 * held, but it will lock, call an _enter_* function or fc_lport_error
1296 * and then unlock the lport.
1297 */
Chris Leech11b56182009-11-03 11:46:29 -08001298void fc_lport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
Robert Love42e9a922008-12-09 15:10:17 -08001299 void *lp_arg)
1300{
1301 struct fc_lport *lport = lp_arg;
1302 u8 op;
1303
Joe Eykholtf657d292009-08-25 14:03:21 -07001304 FC_LPORT_DBG(lport, "Received a LOGO %s\n", fc_els_resp_type(fp));
1305
Robert Love42e9a922008-12-09 15:10:17 -08001306 if (fp == ERR_PTR(-FC_EX_CLOSED))
1307 return;
1308
1309 mutex_lock(&lport->lp_mutex);
1310
Robert Love42e9a922008-12-09 15:10:17 -08001311 if (lport->state != LPORT_ST_LOGO) {
Robert Love74147052009-06-10 15:31:10 -07001312 FC_LPORT_DBG(lport, "Received a LOGO response, but in state "
1313 "%s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001314 if (IS_ERR(fp))
1315 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001316 goto out;
1317 }
1318
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001319 if (IS_ERR(fp)) {
1320 fc_lport_error(lport, fp);
1321 goto err;
1322 }
1323
Robert Love42e9a922008-12-09 15:10:17 -08001324 op = fc_frame_payload_op(fp);
1325 if (op == ELS_LS_ACC)
Joe Eykholt1190d922009-07-29 17:04:27 -07001326 fc_lport_enter_disabled(lport);
Robert Love42e9a922008-12-09 15:10:17 -08001327 else
1328 fc_lport_error(lport, fp);
1329
1330out:
1331 fc_frame_free(fp);
1332err:
1333 mutex_unlock(&lport->lp_mutex);
1334}
Chris Leech11b56182009-11-03 11:46:29 -08001335EXPORT_SYMBOL(fc_lport_logo_resp);
Robert Love42e9a922008-12-09 15:10:17 -08001336
1337/**
Robert Love34f42a02009-02-27 10:55:45 -08001338 * fc_rport_enter_logo() - Logout of the fabric
Robert Love42e9a922008-12-09 15:10:17 -08001339 * @lport: Fibre Channel local port to be logged out
1340 *
1341 * Locking Note: The lport lock is expected to be held before calling
1342 * this routine.
1343 */
1344static void fc_lport_enter_logo(struct fc_lport *lport)
1345{
1346 struct fc_frame *fp;
1347 struct fc_els_logo *logo;
1348
Robert Love74147052009-06-10 15:31:10 -07001349 FC_LPORT_DBG(lport, "Entered LOGO state from %s state\n",
1350 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001351
1352 fc_lport_state_enter(lport, LPORT_ST_LOGO);
Chris Leech8faecdd2009-11-03 11:46:19 -08001353 fc_vports_linkchange(lport);
Robert Love42e9a922008-12-09 15:10:17 -08001354
Robert Love42e9a922008-12-09 15:10:17 -08001355 fp = fc_frame_alloc(lport, sizeof(*logo));
1356 if (!fp) {
1357 fc_lport_error(lport, fp);
1358 return;
1359 }
1360
Joe Eykholta46f3272009-08-25 14:00:55 -07001361 if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp, ELS_LOGO,
1362 fc_lport_logo_resp, lport, lport->e_d_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001363 fc_lport_error(lport, NULL);
Robert Love42e9a922008-12-09 15:10:17 -08001364}
1365
1366/**
Robert Love34f42a02009-02-27 10:55:45 -08001367 * fc_lport_flogi_resp() - Handle response to FLOGI request
Robert Love42e9a922008-12-09 15:10:17 -08001368 * @sp: current sequence in FLOGI exchange
1369 * @fp: response frame
1370 * @lp_arg: Fibre Channel lport port instance that sent the FLOGI request
1371 *
1372 * Locking Note: This function will be called without the lport lock
1373 * held, but it will lock, call an _enter_* function or fc_lport_error
1374 * and then unlock the lport.
1375 */
Chris Leech11b56182009-11-03 11:46:29 -08001376void fc_lport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
Robert Love42e9a922008-12-09 15:10:17 -08001377 void *lp_arg)
1378{
1379 struct fc_lport *lport = lp_arg;
1380 struct fc_frame_header *fh;
1381 struct fc_els_flogi *flp;
1382 u32 did;
1383 u16 csp_flags;
1384 unsigned int r_a_tov;
1385 unsigned int e_d_tov;
1386 u16 mfs;
1387
Joe Eykholtf657d292009-08-25 14:03:21 -07001388 FC_LPORT_DBG(lport, "Received a FLOGI %s\n", fc_els_resp_type(fp));
1389
Robert Love42e9a922008-12-09 15:10:17 -08001390 if (fp == ERR_PTR(-FC_EX_CLOSED))
1391 return;
1392
1393 mutex_lock(&lport->lp_mutex);
1394
Robert Love42e9a922008-12-09 15:10:17 -08001395 if (lport->state != LPORT_ST_FLOGI) {
Robert Love74147052009-06-10 15:31:10 -07001396 FC_LPORT_DBG(lport, "Received a FLOGI response, but in state "
1397 "%s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001398 if (IS_ERR(fp))
1399 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001400 goto out;
1401 }
1402
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001403 if (IS_ERR(fp)) {
1404 fc_lport_error(lport, fp);
1405 goto err;
1406 }
1407
Robert Love42e9a922008-12-09 15:10:17 -08001408 fh = fc_frame_header_get(fp);
1409 did = ntoh24(fh->fh_d_id);
1410 if (fc_frame_payload_op(fp) == ELS_LS_ACC && did != 0) {
1411
Robert Love74147052009-06-10 15:31:10 -07001412 printk(KERN_INFO "libfc: Assigned FID (%6x) in FLOGI response\n",
1413 did);
Robert Love42e9a922008-12-09 15:10:17 -08001414 fc_host_port_id(lport->host) = did;
1415
1416 flp = fc_frame_payload_get(fp, sizeof(*flp));
1417 if (flp) {
1418 mfs = ntohs(flp->fl_csp.sp_bb_data) &
1419 FC_SP_BB_DATA_MASK;
1420 if (mfs >= FC_SP_MIN_MAX_PAYLOAD &&
1421 mfs < lport->mfs)
1422 lport->mfs = mfs;
1423 csp_flags = ntohs(flp->fl_csp.sp_features);
1424 r_a_tov = ntohl(flp->fl_csp.sp_r_a_tov);
1425 e_d_tov = ntohl(flp->fl_csp.sp_e_d_tov);
1426 if (csp_flags & FC_SP_FT_EDTR)
1427 e_d_tov /= 1000000;
Chris Leechdb36c062009-11-03 11:46:24 -08001428
1429 lport->npiv_enabled = !!(csp_flags & FC_SP_FT_NPIV_ACC);
1430
Robert Love42e9a922008-12-09 15:10:17 -08001431 if ((csp_flags & FC_SP_FT_FPORT) == 0) {
1432 if (e_d_tov > lport->e_d_tov)
1433 lport->e_d_tov = e_d_tov;
1434 lport->r_a_tov = 2 * e_d_tov;
Robert Love74147052009-06-10 15:31:10 -07001435 printk(KERN_INFO "libfc: Port (%6x) entered "
1436 "point to point mode\n", did);
Robert Love42e9a922008-12-09 15:10:17 -08001437 fc_lport_ptp_setup(lport, ntoh24(fh->fh_s_id),
1438 get_unaligned_be64(
1439 &flp->fl_wwpn),
1440 get_unaligned_be64(
1441 &flp->fl_wwnn));
1442 } else {
1443 lport->e_d_tov = e_d_tov;
1444 lport->r_a_tov = r_a_tov;
1445 fc_host_fabric_name(lport->host) =
1446 get_unaligned_be64(&flp->fl_wwnn);
1447 fc_lport_enter_dns(lport);
1448 }
1449 }
Robert Love42e9a922008-12-09 15:10:17 -08001450 } else {
Robert Love74147052009-06-10 15:31:10 -07001451 FC_LPORT_DBG(lport, "Bad FLOGI response\n");
Robert Love42e9a922008-12-09 15:10:17 -08001452 }
1453
1454out:
1455 fc_frame_free(fp);
1456err:
1457 mutex_unlock(&lport->lp_mutex);
1458}
Chris Leech11b56182009-11-03 11:46:29 -08001459EXPORT_SYMBOL(fc_lport_flogi_resp);
Robert Love42e9a922008-12-09 15:10:17 -08001460
1461/**
Robert Love34f42a02009-02-27 10:55:45 -08001462 * fc_rport_enter_flogi() - Send a FLOGI request to the fabric manager
Robert Love42e9a922008-12-09 15:10:17 -08001463 * @lport: Fibre Channel local port to be logged in to the fabric
1464 *
1465 * Locking Note: The lport lock is expected to be held before calling
1466 * this routine.
1467 */
1468void fc_lport_enter_flogi(struct fc_lport *lport)
1469{
1470 struct fc_frame *fp;
1471
Robert Love74147052009-06-10 15:31:10 -07001472 FC_LPORT_DBG(lport, "Entered FLOGI state from %s state\n",
1473 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001474
1475 fc_lport_state_enter(lport, LPORT_ST_FLOGI);
1476
1477 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
1478 if (!fp)
1479 return fc_lport_error(lport, fp);
1480
Chris Leechdb36c062009-11-03 11:46:24 -08001481 if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp,
1482 lport->vport ? ELS_FDISC : ELS_FLOGI,
Robert Love42e9a922008-12-09 15:10:17 -08001483 fc_lport_flogi_resp, lport, lport->e_d_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001484 fc_lport_error(lport, NULL);
Robert Love42e9a922008-12-09 15:10:17 -08001485}
1486
1487/* Configure a fc_lport */
1488int fc_lport_config(struct fc_lport *lport)
1489{
1490 INIT_DELAYED_WORK(&lport->retry_work, fc_lport_timeout);
1491 mutex_init(&lport->lp_mutex);
1492
Joe Eykholtb1d9fd52009-07-29 17:04:22 -07001493 fc_lport_state_enter(lport, LPORT_ST_DISABLED);
Robert Love42e9a922008-12-09 15:10:17 -08001494
1495 fc_lport_add_fc4_type(lport, FC_TYPE_FCP);
1496 fc_lport_add_fc4_type(lport, FC_TYPE_CT);
1497
1498 return 0;
1499}
1500EXPORT_SYMBOL(fc_lport_config);
1501
1502int fc_lport_init(struct fc_lport *lport)
1503{
1504 if (!lport->tt.lport_recv)
1505 lport->tt.lport_recv = fc_lport_recv_req;
1506
1507 if (!lport->tt.lport_reset)
1508 lport->tt.lport_reset = fc_lport_reset;
1509
1510 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
1511 fc_host_node_name(lport->host) = lport->wwnn;
1512 fc_host_port_name(lport->host) = lport->wwpn;
1513 fc_host_supported_classes(lport->host) = FC_COS_CLASS3;
1514 memset(fc_host_supported_fc4s(lport->host), 0,
1515 sizeof(fc_host_supported_fc4s(lport->host)));
1516 fc_host_supported_fc4s(lport->host)[2] = 1;
1517 fc_host_supported_fc4s(lport->host)[7] = 1;
1518
1519 /* This value is also unchanging */
1520 memset(fc_host_active_fc4s(lport->host), 0,
1521 sizeof(fc_host_active_fc4s(lport->host)));
1522 fc_host_active_fc4s(lport->host)[2] = 1;
1523 fc_host_active_fc4s(lport->host)[7] = 1;
1524 fc_host_maxframe_size(lport->host) = lport->mfs;
1525 fc_host_supported_speeds(lport->host) = 0;
1526 if (lport->link_supported_speeds & FC_PORTSPEED_1GBIT)
1527 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_1GBIT;
1528 if (lport->link_supported_speeds & FC_PORTSPEED_10GBIT)
1529 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_10GBIT;
1530
Robert Love42e9a922008-12-09 15:10:17 -08001531 return 0;
1532}
1533EXPORT_SYMBOL(fc_lport_init);
Steve Maa51ab392009-11-03 11:47:34 -08001534
1535/**
1536 * fc_lport_bsg_resp() - The common response handler for fc pass-thru requests
1537 * @sp: current sequence in the fc pass-thru request exchange
1538 * @fp: received response frame
1539 * @info_arg: pointer to struct fc_bsg_info
1540 */
1541static void fc_lport_bsg_resp(struct fc_seq *sp, struct fc_frame *fp,
1542 void *info_arg)
1543{
1544 struct fc_bsg_info *info = info_arg;
1545 struct fc_bsg_job *job = info->job;
1546 struct fc_lport *lport = info->lport;
1547 struct fc_frame_header *fh;
1548 size_t len;
1549 void *buf;
1550
1551 if (IS_ERR(fp)) {
1552 job->reply->result = (PTR_ERR(fp) == -FC_EX_CLOSED) ?
1553 -ECONNABORTED : -ETIMEDOUT;
1554 job->reply_len = sizeof(uint32_t);
1555 job->state_flags |= FC_RQST_STATE_DONE;
1556 job->job_done(job);
1557 kfree(info);
1558 return;
1559 }
1560
1561 mutex_lock(&lport->lp_mutex);
1562 fh = fc_frame_header_get(fp);
1563 len = fr_len(fp) - sizeof(*fh);
1564 buf = fc_frame_payload_get(fp, 0);
1565
1566 if (fr_sof(fp) == FC_SOF_I3 && !ntohs(fh->fh_seq_cnt)) {
1567 /* Get the response code from the first frame payload */
1568 unsigned short cmd = (info->rsp_code == FC_FS_ACC) ?
1569 ntohs(((struct fc_ct_hdr *)buf)->ct_cmd) :
1570 (unsigned short)fc_frame_payload_op(fp);
1571
1572 /* Save the reply status of the job */
1573 job->reply->reply_data.ctels_reply.status =
1574 (cmd == info->rsp_code) ?
1575 FC_CTELS_STATUS_OK : FC_CTELS_STATUS_REJECT;
1576 }
1577
1578 job->reply->reply_payload_rcv_len +=
1579 fc_copy_buffer_to_sglist(buf, len, info->sg, &info->nents,
1580 &info->offset, KM_BIO_SRC_IRQ, NULL);
1581
1582 if (fr_eof(fp) == FC_EOF_T &&
1583 (ntoh24(fh->fh_f_ctl) & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
1584 (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
1585 if (job->reply->reply_payload_rcv_len >
1586 job->reply_payload.payload_len)
1587 job->reply->reply_payload_rcv_len =
1588 job->reply_payload.payload_len;
1589 job->reply->result = 0;
1590 job->state_flags |= FC_RQST_STATE_DONE;
1591 job->job_done(job);
1592 kfree(info);
1593 }
1594 fc_frame_free(fp);
1595 mutex_unlock(&lport->lp_mutex);
1596}
1597
1598/**
1599 * fc_lport_els_request() - Send ELS pass-thru request
1600 * @job: The bsg fc pass-thru job structure
1601 * @lport: The local port sending the request
1602 * @did: The destination port id.
1603 *
1604 * Locking Note: The lport lock is expected to be held before calling
1605 * this routine.
1606 */
1607static int fc_lport_els_request(struct fc_bsg_job *job,
1608 struct fc_lport *lport,
1609 u32 did, u32 tov)
1610{
1611 struct fc_bsg_info *info;
1612 struct fc_frame *fp;
1613 struct fc_frame_header *fh;
1614 char *pp;
1615 int len;
1616
1617 fp = fc_frame_alloc(lport, sizeof(struct fc_frame_header) +
1618 job->request_payload.payload_len);
1619 if (!fp)
1620 return -ENOMEM;
1621
1622 len = job->request_payload.payload_len;
1623 pp = fc_frame_payload_get(fp, len);
1624
1625 sg_copy_to_buffer(job->request_payload.sg_list,
1626 job->request_payload.sg_cnt,
1627 pp, len);
1628
1629 fh = fc_frame_header_get(fp);
1630 fh->fh_r_ctl = FC_RCTL_ELS_REQ;
1631 hton24(fh->fh_d_id, did);
1632 hton24(fh->fh_s_id, fc_host_port_id(lport->host));
1633 fh->fh_type = FC_TYPE_ELS;
1634 hton24(fh->fh_f_ctl, FC_FC_FIRST_SEQ |
1635 FC_FC_END_SEQ | FC_FC_SEQ_INIT);
1636 fh->fh_cs_ctl = 0;
1637 fh->fh_df_ctl = 0;
1638 fh->fh_parm_offset = 0;
1639
1640 info = kzalloc(sizeof(struct fc_bsg_info), GFP_KERNEL);
1641 if (!info) {
1642 fc_frame_free(fp);
1643 return -ENOMEM;
1644 }
1645
1646 info->job = job;
1647 info->lport = lport;
1648 info->rsp_code = ELS_LS_ACC;
1649 info->nents = job->reply_payload.sg_cnt;
1650 info->sg = job->reply_payload.sg_list;
1651
1652 if (!lport->tt.exch_seq_send(lport, fp, fc_lport_bsg_resp,
1653 NULL, info, tov))
1654 return -ECOMM;
1655 return 0;
1656}
1657
1658/**
1659 * fc_lport_ct_request() - Send CT pass-thru request
1660 * @job: The bsg fc pass-thru job structure
1661 * @lport: The local port sending the request
1662 * @did: The destination FC-ID
1663 * @tov: The time to wait for a response
1664 *
1665 * Locking Note: The lport lock is expected to be held before calling
1666 * this routine.
1667 */
1668static int fc_lport_ct_request(struct fc_bsg_job *job,
1669 struct fc_lport *lport, u32 did, u32 tov)
1670{
1671 struct fc_bsg_info *info;
1672 struct fc_frame *fp;
1673 struct fc_frame_header *fh;
1674 struct fc_ct_req *ct;
1675 size_t len;
1676
1677 fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
1678 job->request_payload.payload_len);
1679 if (!fp)
1680 return -ENOMEM;
1681
1682 len = job->request_payload.payload_len;
1683 ct = fc_frame_payload_get(fp, len);
1684
1685 sg_copy_to_buffer(job->request_payload.sg_list,
1686 job->request_payload.sg_cnt,
1687 ct, len);
1688
1689 fh = fc_frame_header_get(fp);
1690 fh->fh_r_ctl = FC_RCTL_DD_UNSOL_CTL;
1691 hton24(fh->fh_d_id, did);
1692 hton24(fh->fh_s_id, fc_host_port_id(lport->host));
1693 fh->fh_type = FC_TYPE_CT;
1694 hton24(fh->fh_f_ctl, FC_FC_FIRST_SEQ |
1695 FC_FC_END_SEQ | FC_FC_SEQ_INIT);
1696 fh->fh_cs_ctl = 0;
1697 fh->fh_df_ctl = 0;
1698 fh->fh_parm_offset = 0;
1699
1700 info = kzalloc(sizeof(struct fc_bsg_info), GFP_KERNEL);
1701 if (!info) {
1702 fc_frame_free(fp);
1703 return -ENOMEM;
1704 }
1705
1706 info->job = job;
1707 info->lport = lport;
1708 info->rsp_code = FC_FS_ACC;
1709 info->nents = job->reply_payload.sg_cnt;
1710 info->sg = job->reply_payload.sg_list;
1711
1712 if (!lport->tt.exch_seq_send(lport, fp, fc_lport_bsg_resp,
1713 NULL, info, tov))
1714 return -ECOMM;
1715 return 0;
1716}
1717
1718/**
1719 * fc_lport_bsg_request() - The common entry point for sending
1720 * fc pass-thru requests
1721 * @job: The fc pass-thru job structure
1722 */
1723int fc_lport_bsg_request(struct fc_bsg_job *job)
1724{
1725 struct request *rsp = job->req->next_rq;
1726 struct Scsi_Host *shost = job->shost;
1727 struct fc_lport *lport = shost_priv(shost);
1728 struct fc_rport *rport;
1729 struct fc_rport_priv *rdata;
1730 int rc = -EINVAL;
1731 u32 did;
1732
1733 job->reply->reply_payload_rcv_len = 0;
1734 rsp->resid_len = job->reply_payload.payload_len;
1735
1736 mutex_lock(&lport->lp_mutex);
1737
1738 switch (job->request->msgcode) {
1739 case FC_BSG_RPT_ELS:
1740 rport = job->rport;
1741 if (!rport)
1742 break;
1743
1744 rdata = rport->dd_data;
1745 rc = fc_lport_els_request(job, lport, rport->port_id,
1746 rdata->e_d_tov);
1747 break;
1748
1749 case FC_BSG_RPT_CT:
1750 rport = job->rport;
1751 if (!rport)
1752 break;
1753
1754 rdata = rport->dd_data;
1755 rc = fc_lport_ct_request(job, lport, rport->port_id,
1756 rdata->e_d_tov);
1757 break;
1758
1759 case FC_BSG_HST_CT:
1760 did = ntoh24(job->request->rqst_data.h_ct.port_id);
1761 if (did == FC_FID_DIR_SERV)
1762 rdata = lport->dns_rp;
1763 else
1764 rdata = lport->tt.rport_lookup(lport, did);
1765
1766 if (!rdata)
1767 break;
1768
1769 rc = fc_lport_ct_request(job, lport, did, rdata->e_d_tov);
1770 break;
1771
1772 case FC_BSG_HST_ELS_NOLOGIN:
1773 did = ntoh24(job->request->rqst_data.h_els.port_id);
1774 rc = fc_lport_els_request(job, lport, did, lport->e_d_tov);
1775 break;
1776 }
1777
1778 mutex_unlock(&lport->lp_mutex);
1779 return rc;
1780}
1781EXPORT_SYMBOL(fc_lport_bsg_request);