blob: 90930c43545527431cdf3c9b8d52ea8c1dcf3a84 [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
Robert Love3a3b42b2009-11-03 11:47:39 -0800136 * @sg: job->reply_payload.sg_list
Steve Maa51ab392009-11-03 11:47:34 -0800137 * @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 Love3a3b42b2009-11-03 11:47:39 -0800149/**
150 * fc_frame_drop() - Dummy frame handler
151 * @lport: The local port the frame was received on
152 * @fp: The received frame
153 */
Robert Love42e9a922008-12-09 15:10:17 -0800154static int fc_frame_drop(struct fc_lport *lport, struct fc_frame *fp)
155{
156 fc_frame_free(fp);
157 return 0;
158}
159
160/**
Robert Love34f42a02009-02-27 10:55:45 -0800161 * fc_lport_rport_callback() - Event handler for rport events
Robert Love42e9a922008-12-09 15:10:17 -0800162 * @lport: The lport which is receiving the event
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700163 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800164 * @event: The event that occured
165 *
166 * Locking Note: The rport lock should not be held when calling
167 * this function.
168 */
169static void fc_lport_rport_callback(struct fc_lport *lport,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700170 struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -0800171 enum fc_rport_event event)
172{
Robert Love74147052009-06-10 15:31:10 -0700173 FC_LPORT_DBG(lport, "Received a %d event for port (%6x)\n", event,
Joe Eykholtf211fa52009-08-25 14:01:01 -0700174 rdata->ids.port_id);
Robert Love42e9a922008-12-09 15:10:17 -0800175
Joe Eykholtb5cbf082009-08-25 14:01:44 -0700176 mutex_lock(&lport->lp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800177 switch (event) {
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700178 case RPORT_EV_READY:
Joe Eykholtb5cbf082009-08-25 14:01:44 -0700179 if (lport->state == LPORT_ST_DNS) {
Robert Love3a3b42b2009-11-03 11:47:39 -0800180 lport->dns_rdata = rdata;
Chris Leechc914f7d2009-11-03 11:47:12 -0800181 fc_lport_enter_ns(lport, LPORT_ST_RNN_ID);
Joe Eykholtb5cbf082009-08-25 14:01:44 -0700182 } else {
183 FC_LPORT_DBG(lport, "Received an READY event "
184 "on port (%6x) for the directory "
185 "server, but the lport is not "
186 "in the DNS state, it's in the "
187 "%d state", rdata->ids.port_id,
188 lport->state);
189 lport->tt.rport_logoff(rdata);
190 }
Robert Love42e9a922008-12-09 15:10:17 -0800191 break;
192 case RPORT_EV_LOGO:
193 case RPORT_EV_FAILED:
194 case RPORT_EV_STOP:
Robert Love3a3b42b2009-11-03 11:47:39 -0800195 lport->dns_rdata = NULL;
Robert Love42e9a922008-12-09 15:10:17 -0800196 break;
197 case RPORT_EV_NONE:
198 break;
199 }
Joe Eykholtb5cbf082009-08-25 14:01:44 -0700200 mutex_unlock(&lport->lp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800201}
202
203/**
Robert Love34f42a02009-02-27 10:55:45 -0800204 * fc_lport_state() - Return a string which represents the lport's state
Robert Love42e9a922008-12-09 15:10:17 -0800205 * @lport: The lport whose state is to converted to a string
206 */
207static const char *fc_lport_state(struct fc_lport *lport)
208{
209 const char *cp;
210
211 cp = fc_lport_state_names[lport->state];
212 if (!cp)
213 cp = "unknown";
214 return cp;
215}
216
217/**
Robert Love34f42a02009-02-27 10:55:45 -0800218 * fc_lport_ptp_setup() - Create an rport for point-to-point mode
Robert Love3a3b42b2009-11-03 11:47:39 -0800219 * @lport: The lport to attach the ptp rport to
220 * @remote_fid: The FID of the ptp rport
Robert Love42e9a922008-12-09 15:10:17 -0800221 * @remote_wwpn: The WWPN of the ptp rport
222 * @remote_wwnn: The WWNN of the ptp rport
223 */
224static void fc_lport_ptp_setup(struct fc_lport *lport,
225 u32 remote_fid, u64 remote_wwpn,
226 u64 remote_wwnn)
227{
Joe Eykholt48f00902009-08-25 14:01:50 -0700228 mutex_lock(&lport->disc.disc_mutex);
Robert Love3a3b42b2009-11-03 11:47:39 -0800229 if (lport->ptp_rdata)
230 lport->tt.rport_logoff(lport->ptp_rdata);
231 lport->ptp_rdata = lport->tt.rport_create(lport, remote_fid);
232 lport->ptp_rdata->ids.port_name = remote_wwpn;
233 lport->ptp_rdata->ids.node_name = remote_wwnn;
Joe Eykholt48f00902009-08-25 14:01:50 -0700234 mutex_unlock(&lport->disc.disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800235
Robert Love3a3b42b2009-11-03 11:47:39 -0800236 lport->tt.rport_login(lport->ptp_rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800237
238 fc_lport_enter_ready(lport);
239}
240
Robert Love3a3b42b2009-11-03 11:47:39 -0800241/**
242 * fc_get_host_port_type() - Return the port type of the given Scsi_Host
243 * @shost: The SCSI host whose port type is to be determined
244 */
Robert Love42e9a922008-12-09 15:10:17 -0800245void fc_get_host_port_type(struct Scsi_Host *shost)
246{
247 /* TODO - currently just NPORT */
248 fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
249}
250EXPORT_SYMBOL(fc_get_host_port_type);
251
Robert Love3a3b42b2009-11-03 11:47:39 -0800252/**
253 * fc_get_host_port_state() - Return the port state of the given Scsi_Host
254 * @shost: The SCSI host whose port state is to be determined
255 */
Robert Love42e9a922008-12-09 15:10:17 -0800256void fc_get_host_port_state(struct Scsi_Host *shost)
257{
Robert Love3a3b42b2009-11-03 11:47:39 -0800258 struct fc_lport *lport = shost_priv(shost);
Robert Love42e9a922008-12-09 15:10:17 -0800259
Robert Love3a3b42b2009-11-03 11:47:39 -0800260 mutex_lock(&lport->lp_mutex);
261 if (!lport->link_up)
Chris Leech8faecdd2009-11-03 11:46:19 -0800262 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
Robert Love42e9a922008-12-09 15:10:17 -0800263 else
Robert Love3a3b42b2009-11-03 11:47:39 -0800264 switch (lport->state) {
Chris Leech8faecdd2009-11-03 11:46:19 -0800265 case LPORT_ST_READY:
266 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
267 break;
268 default:
269 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
270 }
Robert Love3a3b42b2009-11-03 11:47:39 -0800271 mutex_unlock(&lport->lp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800272}
273EXPORT_SYMBOL(fc_get_host_port_state);
274
Robert Love3a3b42b2009-11-03 11:47:39 -0800275/**
276 * fc_get_host_speed() - Return the speed of the given Scsi_Host
277 * @shost: The SCSI host whose port speed is to be determined
278 */
Robert Love42e9a922008-12-09 15:10:17 -0800279void fc_get_host_speed(struct Scsi_Host *shost)
280{
281 struct fc_lport *lport = shost_priv(shost);
282
283 fc_host_speed(shost) = lport->link_speed;
284}
285EXPORT_SYMBOL(fc_get_host_speed);
286
Robert Love3a3b42b2009-11-03 11:47:39 -0800287/**
288 * fc_get_host_stats() - Return the Scsi_Host's statistics
289 * @shost: The SCSI host whose statistics are to be returned
290 */
Robert Love42e9a922008-12-09 15:10:17 -0800291struct fc_host_statistics *fc_get_host_stats(struct Scsi_Host *shost)
292{
Robert Love42e9a922008-12-09 15:10:17 -0800293 struct fc_host_statistics *fcoe_stats;
Robert Love3a3b42b2009-11-03 11:47:39 -0800294 struct fc_lport *lport = shost_priv(shost);
Robert Love42e9a922008-12-09 15:10:17 -0800295 struct timespec v0, v1;
Robert Love582b45b2009-03-31 15:51:50 -0700296 unsigned int cpu;
Robert Love42e9a922008-12-09 15:10:17 -0800297
Robert Love3a3b42b2009-11-03 11:47:39 -0800298 fcoe_stats = &lport->host_stats;
Robert Love42e9a922008-12-09 15:10:17 -0800299 memset(fcoe_stats, 0, sizeof(struct fc_host_statistics));
300
301 jiffies_to_timespec(jiffies, &v0);
Robert Love3a3b42b2009-11-03 11:47:39 -0800302 jiffies_to_timespec(lport->boot_time, &v1);
Robert Love42e9a922008-12-09 15:10:17 -0800303 fcoe_stats->seconds_since_last_reset = (v0.tv_sec - v1.tv_sec);
304
Robert Love582b45b2009-03-31 15:51:50 -0700305 for_each_possible_cpu(cpu) {
306 struct fcoe_dev_stats *stats;
307
Robert Love3a3b42b2009-11-03 11:47:39 -0800308 stats = per_cpu_ptr(lport->dev_stats, cpu);
Robert Love582b45b2009-03-31 15:51:50 -0700309
Robert Love42e9a922008-12-09 15:10:17 -0800310 fcoe_stats->tx_frames += stats->TxFrames;
311 fcoe_stats->tx_words += stats->TxWords;
312 fcoe_stats->rx_frames += stats->RxFrames;
313 fcoe_stats->rx_words += stats->RxWords;
314 fcoe_stats->error_frames += stats->ErrorFrames;
315 fcoe_stats->invalid_crc_count += stats->InvalidCRCCount;
316 fcoe_stats->fcp_input_requests += stats->InputRequests;
317 fcoe_stats->fcp_output_requests += stats->OutputRequests;
318 fcoe_stats->fcp_control_requests += stats->ControlRequests;
319 fcoe_stats->fcp_input_megabytes += stats->InputMegabytes;
320 fcoe_stats->fcp_output_megabytes += stats->OutputMegabytes;
321 fcoe_stats->link_failure_count += stats->LinkFailureCount;
322 }
323 fcoe_stats->lip_count = -1;
324 fcoe_stats->nos_count = -1;
325 fcoe_stats->loss_of_sync_count = -1;
326 fcoe_stats->loss_of_signal_count = -1;
327 fcoe_stats->prim_seq_protocol_err_count = -1;
328 fcoe_stats->dumped_frames = -1;
329 return fcoe_stats;
330}
331EXPORT_SYMBOL(fc_get_host_stats);
332
Robert Love3a3b42b2009-11-03 11:47:39 -0800333/**
334 * fc_lport_flogi_fill() - Fill in FLOGI command for request
335 * @lport: The local port the FLOGI is for
336 * @flogi: The FLOGI command
337 * @op: The opcode
Robert Love42e9a922008-12-09 15:10:17 -0800338 */
Robert Love3a3b42b2009-11-03 11:47:39 -0800339static void fc_lport_flogi_fill(struct fc_lport *lport,
340 struct fc_els_flogi *flogi,
341 unsigned int op)
Robert Love42e9a922008-12-09 15:10:17 -0800342{
343 struct fc_els_csp *sp;
344 struct fc_els_cssp *cp;
345
346 memset(flogi, 0, sizeof(*flogi));
347 flogi->fl_cmd = (u8) op;
348 put_unaligned_be64(lport->wwpn, &flogi->fl_wwpn);
349 put_unaligned_be64(lport->wwnn, &flogi->fl_wwnn);
350 sp = &flogi->fl_csp;
351 sp->sp_hi_ver = 0x20;
352 sp->sp_lo_ver = 0x20;
353 sp->sp_bb_cred = htons(10); /* this gets set by gateway */
354 sp->sp_bb_data = htons((u16) lport->mfs);
355 cp = &flogi->fl_cssp[3 - 1]; /* class 3 parameters */
356 cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
357 if (op != ELS_FLOGI) {
358 sp->sp_features = htons(FC_SP_FT_CIRO);
359 sp->sp_tot_seq = htons(255); /* seq. we accept */
360 sp->sp_rel_off = htons(0x1f);
361 sp->sp_e_d_tov = htonl(lport->e_d_tov);
362
363 cp->cp_rdfs = htons((u16) lport->mfs);
364 cp->cp_con_seq = htons(255);
365 cp->cp_open_seq = 1;
366 }
367}
368
Robert Love3a3b42b2009-11-03 11:47:39 -0800369/**
370 * fc_lport_add_fc4_type() - Add a supported FC-4 type to a local port
371 * @lport: The local port to add a new FC-4 type to
372 * @type: The new FC-4 type
Robert Love42e9a922008-12-09 15:10:17 -0800373 */
374static void fc_lport_add_fc4_type(struct fc_lport *lport, enum fc_fh_type type)
375{
376 __be32 *mp;
377
378 mp = &lport->fcts.ff_type_map[type / FC_NS_BPW];
379 *mp = htonl(ntohl(*mp) | 1UL << (type % FC_NS_BPW));
380}
381
382/**
Robert Love34f42a02009-02-27 10:55:45 -0800383 * fc_lport_recv_rlir_req() - Handle received Registered Link Incident Report.
Robert Love3a3b42b2009-11-03 11:47:39 -0800384 * @sp: The sequence in the RLIR exchange
385 * @fp: The RLIR request frame
Robert Love42e9a922008-12-09 15:10:17 -0800386 * @lport: Fibre Channel local port recieving the RLIR
Robert Love42e9a922008-12-09 15:10:17 -0800387 *
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700388 * Locking Note: The lport lock is expected to be held before calling
Robert Love42e9a922008-12-09 15:10:17 -0800389 * this function.
390 */
391static void fc_lport_recv_rlir_req(struct fc_seq *sp, struct fc_frame *fp,
392 struct fc_lport *lport)
393{
Robert Love74147052009-06-10 15:31:10 -0700394 FC_LPORT_DBG(lport, "Received RLIR request while in state %s\n",
395 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800396
397 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
398 fc_frame_free(fp);
399}
400
401/**
Robert Love34f42a02009-02-27 10:55:45 -0800402 * fc_lport_recv_echo_req() - Handle received ECHO request
Robert Love3a3b42b2009-11-03 11:47:39 -0800403 * @sp: The sequence in the ECHO exchange
404 * @fp: ECHO request frame
405 * @lport: The local port recieving the ECHO
Robert Love42e9a922008-12-09 15:10:17 -0800406 *
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700407 * Locking Note: The lport lock is expected to be held before calling
Robert Love42e9a922008-12-09 15:10:17 -0800408 * this function.
409 */
410static void fc_lport_recv_echo_req(struct fc_seq *sp, struct fc_frame *in_fp,
411 struct fc_lport *lport)
412{
413 struct fc_frame *fp;
414 struct fc_exch *ep = fc_seq_exch(sp);
415 unsigned int len;
416 void *pp;
417 void *dp;
418 u32 f_ctl;
419
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700420 FC_LPORT_DBG(lport, "Received ECHO request while in state %s\n",
Robert Love74147052009-06-10 15:31:10 -0700421 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800422
423 len = fr_len(in_fp) - sizeof(struct fc_frame_header);
424 pp = fc_frame_payload_get(in_fp, len);
425
426 if (len < sizeof(__be32))
427 len = sizeof(__be32);
428
429 fp = fc_frame_alloc(lport, len);
430 if (fp) {
431 dp = fc_frame_payload_get(fp, len);
432 memcpy(dp, pp, len);
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700433 *((__be32 *)dp) = htonl(ELS_LS_ACC << 24);
Robert Love42e9a922008-12-09 15:10:17 -0800434 sp = lport->tt.seq_start_next(sp);
435 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
436 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
437 FC_TYPE_ELS, f_ctl, 0);
438 lport->tt.seq_send(lport, sp, fp);
439 }
440 fc_frame_free(in_fp);
441}
442
443/**
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700444 * fc_lport_recv_rnid_req() - Handle received Request Node ID data request
445 * @sp: The sequence in the RNID exchange
446 * @fp: The RNID request frame
447 * @lport: The local port recieving the RNID
Robert Love42e9a922008-12-09 15:10:17 -0800448 *
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700449 * Locking Note: The lport lock is expected to be held before calling
Robert Love42e9a922008-12-09 15:10:17 -0800450 * this function.
451 */
452static void fc_lport_recv_rnid_req(struct fc_seq *sp, struct fc_frame *in_fp,
453 struct fc_lport *lport)
454{
455 struct fc_frame *fp;
456 struct fc_exch *ep = fc_seq_exch(sp);
457 struct fc_els_rnid *req;
458 struct {
459 struct fc_els_rnid_resp rnid;
460 struct fc_els_rnid_cid cid;
461 struct fc_els_rnid_gen gen;
462 } *rp;
463 struct fc_seq_els_data rjt_data;
464 u8 fmt;
465 size_t len;
466 u32 f_ctl;
467
Robert Love74147052009-06-10 15:31:10 -0700468 FC_LPORT_DBG(lport, "Received RNID request while in state %s\n",
469 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800470
471 req = fc_frame_payload_get(in_fp, sizeof(*req));
472 if (!req) {
473 rjt_data.fp = NULL;
474 rjt_data.reason = ELS_RJT_LOGIC;
475 rjt_data.explan = ELS_EXPL_NONE;
476 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
477 } else {
478 fmt = req->rnid_fmt;
479 len = sizeof(*rp);
480 if (fmt != ELS_RNIDF_GEN ||
481 ntohl(lport->rnid_gen.rnid_atype) == 0) {
482 fmt = ELS_RNIDF_NONE; /* nothing to provide */
483 len -= sizeof(rp->gen);
484 }
485 fp = fc_frame_alloc(lport, len);
486 if (fp) {
487 rp = fc_frame_payload_get(fp, len);
488 memset(rp, 0, len);
489 rp->rnid.rnid_cmd = ELS_LS_ACC;
490 rp->rnid.rnid_fmt = fmt;
491 rp->rnid.rnid_cid_len = sizeof(rp->cid);
492 rp->cid.rnid_wwpn = htonll(lport->wwpn);
493 rp->cid.rnid_wwnn = htonll(lport->wwnn);
494 if (fmt == ELS_RNIDF_GEN) {
495 rp->rnid.rnid_sid_len = sizeof(rp->gen);
496 memcpy(&rp->gen, &lport->rnid_gen,
497 sizeof(rp->gen));
498 }
499 sp = lport->tt.seq_start_next(sp);
500 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
501 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
502 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
503 FC_TYPE_ELS, f_ctl, 0);
504 lport->tt.seq_send(lport, sp, fp);
505 }
506 }
507 fc_frame_free(in_fp);
508}
509
510/**
Robert Love34f42a02009-02-27 10:55:45 -0800511 * fc_lport_recv_logo_req() - Handle received fabric LOGO request
Robert Love3a3b42b2009-11-03 11:47:39 -0800512 * @sp: The sequence in the LOGO exchange
513 * @fp: The LOGO request frame
514 * @lport: The local port recieving the LOGO
Robert Love42e9a922008-12-09 15:10:17 -0800515 *
516 * Locking Note: The lport lock is exected to be held before calling
517 * this function.
518 */
519static void fc_lport_recv_logo_req(struct fc_seq *sp, struct fc_frame *fp,
520 struct fc_lport *lport)
521{
522 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
523 fc_lport_enter_reset(lport);
524 fc_frame_free(fp);
525}
526
527/**
Robert Love34f42a02009-02-27 10:55:45 -0800528 * fc_fabric_login() - Start the lport state machine
Robert Love3a3b42b2009-11-03 11:47:39 -0800529 * @lport: The local port that should log into the fabric
Robert Love42e9a922008-12-09 15:10:17 -0800530 *
531 * Locking Note: This function should not be called
532 * with the lport lock held.
533 */
534int fc_fabric_login(struct fc_lport *lport)
535{
536 int rc = -1;
537
538 mutex_lock(&lport->lp_mutex);
Joe Eykholtb1d9fd52009-07-29 17:04:22 -0700539 if (lport->state == LPORT_ST_DISABLED) {
Robert Love42e9a922008-12-09 15:10:17 -0800540 fc_lport_enter_reset(lport);
541 rc = 0;
542 }
543 mutex_unlock(&lport->lp_mutex);
544
545 return rc;
546}
547EXPORT_SYMBOL(fc_fabric_login);
548
549/**
Chris Leech8faecdd2009-11-03 11:46:19 -0800550 * __fc_linkup() - Handler for transport linkup events
551 * @lport: The lport whose link is up
552 *
553 * Locking: must be called with the lp_mutex held
554 */
555void __fc_linkup(struct fc_lport *lport)
556{
557 if (!lport->link_up) {
558 lport->link_up = 1;
559
560 if (lport->state == LPORT_ST_RESET)
561 fc_lport_enter_flogi(lport);
562 }
563}
564
565/**
Robert Love34f42a02009-02-27 10:55:45 -0800566 * fc_linkup() - Handler for transport linkup events
Robert Love3a3b42b2009-11-03 11:47:39 -0800567 * @lport: The local port whose link is up
Robert Love42e9a922008-12-09 15:10:17 -0800568 */
569void fc_linkup(struct fc_lport *lport)
570{
Robert Love74147052009-06-10 15:31:10 -0700571 printk(KERN_INFO "libfc: Link up on port (%6x)\n",
572 fc_host_port_id(lport->host));
Robert Love42e9a922008-12-09 15:10:17 -0800573
574 mutex_lock(&lport->lp_mutex);
Chris Leech8faecdd2009-11-03 11:46:19 -0800575 __fc_linkup(lport);
Robert Love42e9a922008-12-09 15:10:17 -0800576 mutex_unlock(&lport->lp_mutex);
577}
578EXPORT_SYMBOL(fc_linkup);
579
580/**
Chris Leech8faecdd2009-11-03 11:46:19 -0800581 * __fc_linkdown() - Handler for transport linkdown events
582 * @lport: The lport whose link is down
583 *
584 * Locking: must be called with the lp_mutex held
585 */
586void __fc_linkdown(struct fc_lport *lport)
587{
588 if (lport->link_up) {
589 lport->link_up = 0;
590 fc_lport_enter_reset(lport);
591 lport->tt.fcp_cleanup(lport);
592 }
593}
594
595/**
Robert Love34f42a02009-02-27 10:55:45 -0800596 * fc_linkdown() - Handler for transport linkdown events
Robert Love3a3b42b2009-11-03 11:47:39 -0800597 * @lport: The local port whose link is down
Robert Love42e9a922008-12-09 15:10:17 -0800598 */
599void fc_linkdown(struct fc_lport *lport)
600{
Robert Love74147052009-06-10 15:31:10 -0700601 printk(KERN_INFO "libfc: Link down on port (%6x)\n",
602 fc_host_port_id(lport->host));
Robert Love42e9a922008-12-09 15:10:17 -0800603
Chris Leech8faecdd2009-11-03 11:46:19 -0800604 mutex_lock(&lport->lp_mutex);
605 __fc_linkdown(lport);
Robert Love42e9a922008-12-09 15:10:17 -0800606 mutex_unlock(&lport->lp_mutex);
607}
608EXPORT_SYMBOL(fc_linkdown);
609
610/**
Robert Love34f42a02009-02-27 10:55:45 -0800611 * fc_fabric_logoff() - Logout of the fabric
Robert Love3a3b42b2009-11-03 11:47:39 -0800612 * @lport: The local port to logoff the fabric
Robert Love42e9a922008-12-09 15:10:17 -0800613 *
614 * Return value:
615 * 0 for success, -1 for failure
Robert Love34f42a02009-02-27 10:55:45 -0800616 */
Robert Love42e9a922008-12-09 15:10:17 -0800617int fc_fabric_logoff(struct fc_lport *lport)
618{
619 lport->tt.disc_stop_final(lport);
620 mutex_lock(&lport->lp_mutex);
Robert Love3a3b42b2009-11-03 11:47:39 -0800621 if (lport->dns_rdata)
622 lport->tt.rport_logoff(lport->dns_rdata);
Abhijeet Joglekara0fd2e42009-04-21 16:27:09 -0700623 mutex_unlock(&lport->lp_mutex);
624 lport->tt.rport_flush_queue();
625 mutex_lock(&lport->lp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800626 fc_lport_enter_logo(lport);
627 mutex_unlock(&lport->lp_mutex);
Steve Maf7db2c12009-02-27 10:55:13 -0800628 cancel_delayed_work_sync(&lport->retry_work);
Robert Love42e9a922008-12-09 15:10:17 -0800629 return 0;
630}
631EXPORT_SYMBOL(fc_fabric_logoff);
632
633/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800634 * fc_lport_destroy() - Unregister a fc_lport
635 * @lport: The local port to unregister
Robert Love42e9a922008-12-09 15:10:17 -0800636 *
Robert Love42e9a922008-12-09 15:10:17 -0800637 * Note:
638 * exit routine for fc_lport instance
639 * clean-up all the allocated memory
640 * and free up other system resources.
641 *
Robert Love34f42a02009-02-27 10:55:45 -0800642 */
Robert Love42e9a922008-12-09 15:10:17 -0800643int fc_lport_destroy(struct fc_lport *lport)
644{
Abhijeet Joglekarbbf15662009-04-21 16:27:14 -0700645 mutex_lock(&lport->lp_mutex);
Joe Eykholtb1d9fd52009-07-29 17:04:22 -0700646 lport->state = LPORT_ST_DISABLED;
Abhijeet Joglekarbbf15662009-04-21 16:27:14 -0700647 lport->link_up = 0;
Robert Love42e9a922008-12-09 15:10:17 -0800648 lport->tt.frame_send = fc_frame_drop;
Abhijeet Joglekarbbf15662009-04-21 16:27:14 -0700649 mutex_unlock(&lport->lp_mutex);
650
Robert Love42e9a922008-12-09 15:10:17 -0800651 lport->tt.fcp_abort_io(lport);
Joe Eykholte9ba8b42009-07-29 17:04:33 -0700652 lport->tt.disc_stop_final(lport);
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -0800653 lport->tt.exch_mgr_reset(lport, 0, 0);
Robert Love42e9a922008-12-09 15:10:17 -0800654 return 0;
655}
656EXPORT_SYMBOL(fc_lport_destroy);
657
658/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800659 * fc_set_mfs() - Set the maximum frame size for a local port
660 * @lport: The local port to set the MFS for
661 * @mfs: The new MFS
Robert Love34f42a02009-02-27 10:55:45 -0800662 */
Robert Love42e9a922008-12-09 15:10:17 -0800663int fc_set_mfs(struct fc_lport *lport, u32 mfs)
664{
665 unsigned int old_mfs;
666 int rc = -EINVAL;
667
668 mutex_lock(&lport->lp_mutex);
669
670 old_mfs = lport->mfs;
671
672 if (mfs >= FC_MIN_MAX_FRAME) {
673 mfs &= ~3;
674 if (mfs > FC_MAX_FRAME)
675 mfs = FC_MAX_FRAME;
676 mfs -= sizeof(struct fc_frame_header);
677 lport->mfs = mfs;
678 rc = 0;
679 }
680
681 if (!rc && mfs < old_mfs)
682 fc_lport_enter_reset(lport);
683
684 mutex_unlock(&lport->lp_mutex);
685
686 return rc;
687}
688EXPORT_SYMBOL(fc_set_mfs);
689
690/**
Robert Love34f42a02009-02-27 10:55:45 -0800691 * fc_lport_disc_callback() - Callback for discovery events
Robert Love3a3b42b2009-11-03 11:47:39 -0800692 * @lport: The local port receiving the event
Robert Love42e9a922008-12-09 15:10:17 -0800693 * @event: The discovery event
694 */
695void fc_lport_disc_callback(struct fc_lport *lport, enum fc_disc_event event)
696{
697 switch (event) {
698 case DISC_EV_SUCCESS:
Robert Love74147052009-06-10 15:31:10 -0700699 FC_LPORT_DBG(lport, "Discovery succeeded\n");
Robert Love42e9a922008-12-09 15:10:17 -0800700 break;
701 case DISC_EV_FAILED:
Robert Love74147052009-06-10 15:31:10 -0700702 printk(KERN_ERR "libfc: Discovery failed for port (%6x)\n",
703 fc_host_port_id(lport->host));
Robert Love42e9a922008-12-09 15:10:17 -0800704 mutex_lock(&lport->lp_mutex);
705 fc_lport_enter_reset(lport);
706 mutex_unlock(&lport->lp_mutex);
707 break;
708 case DISC_EV_NONE:
709 WARN_ON(1);
710 break;
711 }
712}
713
714/**
Robert Love34f42a02009-02-27 10:55:45 -0800715 * fc_rport_enter_ready() - Enter the ready state and start discovery
Robert Love3a3b42b2009-11-03 11:47:39 -0800716 * @lport: The local port that is ready
Robert Love42e9a922008-12-09 15:10:17 -0800717 *
718 * Locking Note: The lport lock is expected to be held before calling
719 * this routine.
720 */
721static void fc_lport_enter_ready(struct fc_lport *lport)
722{
Robert Love74147052009-06-10 15:31:10 -0700723 FC_LPORT_DBG(lport, "Entered READY from state %s\n",
724 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800725
726 fc_lport_state_enter(lport, LPORT_ST_READY);
Chris Leech8faecdd2009-11-03 11:46:19 -0800727 if (lport->vport)
728 fc_vport_set_state(lport->vport, FC_VPORT_ACTIVE);
729 fc_vports_linkchange(lport);
Robert Love42e9a922008-12-09 15:10:17 -0800730
Robert Love3a3b42b2009-11-03 11:47:39 -0800731 if (!lport->ptp_rdata)
Joe Eykholt29d898e2009-08-25 14:02:49 -0700732 lport->tt.disc_start(fc_lport_disc_callback, lport);
Robert Love42e9a922008-12-09 15:10:17 -0800733}
734
735/**
Robert Love34f42a02009-02-27 10:55:45 -0800736 * fc_lport_recv_flogi_req() - Receive a FLOGI request
Robert Love42e9a922008-12-09 15:10:17 -0800737 * @sp_in: The sequence the FLOGI is on
Robert Love3a3b42b2009-11-03 11:47:39 -0800738 * @rx_fp: The FLOGI frame
739 * @lport: The local port that recieved the request
Robert Love42e9a922008-12-09 15:10:17 -0800740 *
741 * A received FLOGI request indicates a point-to-point connection.
742 * Accept it with the common service parameters indicating our N port.
743 * Set up to do a PLOGI if we have the higher-number WWPN.
744 *
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700745 * Locking Note: The lport lock is expected to be held before calling
Robert Love42e9a922008-12-09 15:10:17 -0800746 * this function.
747 */
748static void fc_lport_recv_flogi_req(struct fc_seq *sp_in,
749 struct fc_frame *rx_fp,
750 struct fc_lport *lport)
751{
752 struct fc_frame *fp;
753 struct fc_frame_header *fh;
754 struct fc_seq *sp;
755 struct fc_exch *ep;
756 struct fc_els_flogi *flp;
757 struct fc_els_flogi *new_flp;
758 u64 remote_wwpn;
759 u32 remote_fid;
760 u32 local_fid;
761 u32 f_ctl;
762
Robert Love74147052009-06-10 15:31:10 -0700763 FC_LPORT_DBG(lport, "Received FLOGI request while in state %s\n",
764 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800765
766 fh = fc_frame_header_get(rx_fp);
767 remote_fid = ntoh24(fh->fh_s_id);
768 flp = fc_frame_payload_get(rx_fp, sizeof(*flp));
769 if (!flp)
770 goto out;
771 remote_wwpn = get_unaligned_be64(&flp->fl_wwpn);
772 if (remote_wwpn == lport->wwpn) {
Robert Love74147052009-06-10 15:31:10 -0700773 printk(KERN_WARNING "libfc: Received FLOGI from port "
774 "with same WWPN %llx\n", remote_wwpn);
Robert Love42e9a922008-12-09 15:10:17 -0800775 goto out;
776 }
Robert Love74147052009-06-10 15:31:10 -0700777 FC_LPORT_DBG(lport, "FLOGI from port WWPN %llx\n", remote_wwpn);
Robert Love42e9a922008-12-09 15:10:17 -0800778
779 /*
780 * XXX what is the right thing to do for FIDs?
781 * The originator might expect our S_ID to be 0xfffffe.
782 * But if so, both of us could end up with the same FID.
783 */
784 local_fid = FC_LOCAL_PTP_FID_LO;
785 if (remote_wwpn < lport->wwpn) {
786 local_fid = FC_LOCAL_PTP_FID_HI;
787 if (!remote_fid || remote_fid == local_fid)
788 remote_fid = FC_LOCAL_PTP_FID_LO;
789 } else if (!remote_fid) {
790 remote_fid = FC_LOCAL_PTP_FID_HI;
791 }
792
793 fc_host_port_id(lport->host) = local_fid;
794
795 fp = fc_frame_alloc(lport, sizeof(*flp));
796 if (fp) {
797 sp = lport->tt.seq_start_next(fr_seq(rx_fp));
798 new_flp = fc_frame_payload_get(fp, sizeof(*flp));
799 fc_lport_flogi_fill(lport, new_flp, ELS_FLOGI);
800 new_flp->fl_cmd = (u8) ELS_LS_ACC;
801
802 /*
803 * Send the response. If this fails, the originator should
804 * repeat the sequence.
805 */
806 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
807 ep = fc_seq_exch(sp);
808 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
809 FC_TYPE_ELS, f_ctl, 0);
810 lport->tt.seq_send(lport, sp, fp);
811
812 } else {
813 fc_lport_error(lport, fp);
814 }
815 fc_lport_ptp_setup(lport, remote_fid, remote_wwpn,
816 get_unaligned_be64(&flp->fl_wwnn));
817
Robert Love42e9a922008-12-09 15:10:17 -0800818out:
819 sp = fr_seq(rx_fp);
820 fc_frame_free(rx_fp);
821}
822
823/**
Robert Love34f42a02009-02-27 10:55:45 -0800824 * fc_lport_recv_req() - The generic lport request handler
Robert Love3a3b42b2009-11-03 11:47:39 -0800825 * @lport: The local port that received the request
826 * @sp: The sequence the request is on
827 * @fp: The request frame
Robert Love42e9a922008-12-09 15:10:17 -0800828 *
829 * This function will see if the lport handles the request or
830 * if an rport should handle the request.
831 *
832 * Locking Note: This function should not be called with the lport
833 * lock held becuase it will grab the lock.
834 */
835static void fc_lport_recv_req(struct fc_lport *lport, struct fc_seq *sp,
836 struct fc_frame *fp)
837{
838 struct fc_frame_header *fh = fc_frame_header_get(fp);
839 void (*recv) (struct fc_seq *, struct fc_frame *, struct fc_lport *);
Robert Love42e9a922008-12-09 15:10:17 -0800840
841 mutex_lock(&lport->lp_mutex);
842
843 /*
844 * Handle special ELS cases like FLOGI, LOGO, and
845 * RSCN here. These don't require a session.
846 * Even if we had a session, it might not be ready.
847 */
Joe Eykholte9ba8b42009-07-29 17:04:33 -0700848 if (!lport->link_up)
849 fc_frame_free(fp);
850 else if (fh->fh_type == FC_TYPE_ELS &&
851 fh->fh_r_ctl == FC_RCTL_ELS_REQ) {
Robert Love42e9a922008-12-09 15:10:17 -0800852 /*
853 * Check opcode.
854 */
Joe Eykholt131203a2009-08-25 14:03:10 -0700855 recv = lport->tt.rport_recv_req;
Robert Love42e9a922008-12-09 15:10:17 -0800856 switch (fc_frame_payload_op(fp)) {
857 case ELS_FLOGI:
858 recv = fc_lport_recv_flogi_req;
859 break;
860 case ELS_LOGO:
861 fh = fc_frame_header_get(fp);
862 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI)
863 recv = fc_lport_recv_logo_req;
864 break;
865 case ELS_RSCN:
866 recv = lport->tt.disc_recv_req;
867 break;
868 case ELS_ECHO:
869 recv = fc_lport_recv_echo_req;
870 break;
871 case ELS_RLIR:
872 recv = fc_lport_recv_rlir_req;
873 break;
874 case ELS_RNID:
875 recv = fc_lport_recv_rnid_req;
876 break;
Robert Love42e9a922008-12-09 15:10:17 -0800877 }
878
Joe Eykholt131203a2009-08-25 14:03:10 -0700879 recv(sp, fp, lport);
Robert Love42e9a922008-12-09 15:10:17 -0800880 } else {
Robert Love74147052009-06-10 15:31:10 -0700881 FC_LPORT_DBG(lport, "dropping invalid frame (eof %x)\n",
882 fr_eof(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800883 fc_frame_free(fp);
884 }
885 mutex_unlock(&lport->lp_mutex);
886
887 /*
888 * The common exch_done for all request may not be good
889 * if any request requires longer hold on exhange. XXX
890 */
891 lport->tt.exch_done(sp);
892}
893
894/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800895 * fc_lport_reset() - Reset a local port
896 * @lport: The local port which should be reset
Robert Love42e9a922008-12-09 15:10:17 -0800897 *
898 * Locking Note: This functions should not be called with the
899 * lport lock held.
900 */
901int fc_lport_reset(struct fc_lport *lport)
902{
Steve Maf7db2c12009-02-27 10:55:13 -0800903 cancel_delayed_work_sync(&lport->retry_work);
Robert Love42e9a922008-12-09 15:10:17 -0800904 mutex_lock(&lport->lp_mutex);
905 fc_lport_enter_reset(lport);
906 mutex_unlock(&lport->lp_mutex);
907 return 0;
908}
909EXPORT_SYMBOL(fc_lport_reset);
910
911/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800912 * fc_lport_reset_locked() - Reset the local port w/ the lport lock held
913 * @lport: The local port to be reset
Robert Love42e9a922008-12-09 15:10:17 -0800914 *
915 * Locking Note: The lport lock is expected to be held before calling
916 * this routine.
917 */
Joe Eykholt1190d922009-07-29 17:04:27 -0700918static void fc_lport_reset_locked(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -0800919{
Robert Love3a3b42b2009-11-03 11:47:39 -0800920 if (lport->dns_rdata)
921 lport->tt.rport_logoff(lport->dns_rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800922
Robert Love3a3b42b2009-11-03 11:47:39 -0800923 lport->ptp_rdata = NULL;
Robert Love42e9a922008-12-09 15:10:17 -0800924
925 lport->tt.disc_stop(lport);
926
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -0800927 lport->tt.exch_mgr_reset(lport, 0, 0);
Robert Love42e9a922008-12-09 15:10:17 -0800928 fc_host_fabric_name(lport->host) = 0;
929 fc_host_port_id(lport->host) = 0;
Joe Eykholt1190d922009-07-29 17:04:27 -0700930}
Robert Love42e9a922008-12-09 15:10:17 -0800931
Joe Eykholt1190d922009-07-29 17:04:27 -0700932/**
933 * fc_lport_enter_reset() - Reset the local port
Robert Love3a3b42b2009-11-03 11:47:39 -0800934 * @lport: The local port to be reset
Joe Eykholt1190d922009-07-29 17:04:27 -0700935 *
936 * Locking Note: The lport lock is expected to be held before calling
937 * this routine.
938 */
939static void fc_lport_enter_reset(struct fc_lport *lport)
940{
941 FC_LPORT_DBG(lport, "Entered RESET state from %s state\n",
942 fc_lport_state(lport));
943
Chris Leech8faecdd2009-11-03 11:46:19 -0800944 if (lport->vport) {
945 if (lport->link_up)
946 fc_vport_set_state(lport->vport, FC_VPORT_INITIALIZING);
947 else
948 fc_vport_set_state(lport->vport, FC_VPORT_LINKDOWN);
949 }
Joe Eykholt1190d922009-07-29 17:04:27 -0700950 fc_lport_state_enter(lport, LPORT_ST_RESET);
Chris Leech8faecdd2009-11-03 11:46:19 -0800951 fc_vports_linkchange(lport);
Joe Eykholt1190d922009-07-29 17:04:27 -0700952 fc_lport_reset_locked(lport);
Vasu Devbc0e17f2009-02-27 10:54:57 -0800953 if (lport->link_up)
Robert Love42e9a922008-12-09 15:10:17 -0800954 fc_lport_enter_flogi(lport);
955}
956
957/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800958 * fc_lport_enter_disabled() - Disable the local port
959 * @lport: The local port to be reset
Joe Eykholt1190d922009-07-29 17:04:27 -0700960 *
961 * Locking Note: The lport lock is expected to be held before calling
962 * this routine.
963 */
964static void fc_lport_enter_disabled(struct fc_lport *lport)
965{
966 FC_LPORT_DBG(lport, "Entered disabled state from %s state\n",
967 fc_lport_state(lport));
968
969 fc_lport_state_enter(lport, LPORT_ST_DISABLED);
Chris Leech8faecdd2009-11-03 11:46:19 -0800970 fc_vports_linkchange(lport);
Joe Eykholt1190d922009-07-29 17:04:27 -0700971 fc_lport_reset_locked(lport);
972}
973
974/**
Robert Love34f42a02009-02-27 10:55:45 -0800975 * fc_lport_error() - Handler for any errors
Robert Love3a3b42b2009-11-03 11:47:39 -0800976 * @lport: The local port that the error was on
977 * @fp: The error code encoded in a frame pointer
Robert Love42e9a922008-12-09 15:10:17 -0800978 *
979 * If the error was caused by a resource allocation failure
980 * then wait for half a second and retry, otherwise retry
981 * after the e_d_tov time.
982 */
983static void fc_lport_error(struct fc_lport *lport, struct fc_frame *fp)
984{
985 unsigned long delay = 0;
Robert Love74147052009-06-10 15:31:10 -0700986 FC_LPORT_DBG(lport, "Error %ld in state %s, retries %d\n",
987 PTR_ERR(fp), fc_lport_state(lport),
988 lport->retry_count);
Robert Love42e9a922008-12-09 15:10:17 -0800989
990 if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
991 /*
992 * Memory allocation failure, or the exchange timed out.
993 * Retry after delay
994 */
995 if (lport->retry_count < lport->max_retry_count) {
996 lport->retry_count++;
997 if (!fp)
998 delay = msecs_to_jiffies(500);
999 else
1000 delay = msecs_to_jiffies(lport->e_d_tov);
1001
1002 schedule_delayed_work(&lport->retry_work, delay);
1003 } else {
1004 switch (lport->state) {
Joe Eykholtb1d9fd52009-07-29 17:04:22 -07001005 case LPORT_ST_DISABLED:
Robert Love42e9a922008-12-09 15:10:17 -08001006 case LPORT_ST_READY:
1007 case LPORT_ST_RESET:
Chris Leechc9c7bd72009-11-03 11:46:51 -08001008 case LPORT_ST_RNN_ID:
Chris Leech5baa17c2009-11-03 11:46:56 -08001009 case LPORT_ST_RSNN_NN:
Chris Leechc9866a52009-11-03 11:47:01 -08001010 case LPORT_ST_RSPN_ID:
Robert Love42e9a922008-12-09 15:10:17 -08001011 case LPORT_ST_RFT_ID:
1012 case LPORT_ST_SCR:
1013 case LPORT_ST_DNS:
1014 case LPORT_ST_FLOGI:
1015 case LPORT_ST_LOGO:
1016 fc_lport_enter_reset(lport);
1017 break;
1018 }
1019 }
1020 }
1021}
1022
1023/**
Chris Leech7cccc152009-11-03 11:47:07 -08001024 * fc_lport_ns_resp() - Handle response to a name server
Robert Love3a3b42b2009-11-03 11:47:39 -08001025 * registration exchange
1026 * @sp: current sequence in exchange
1027 * @fp: response frame
Robert Love42e9a922008-12-09 15:10:17 -08001028 * @lp_arg: Fibre Channel host port instance
1029 *
1030 * Locking Note: This function will be called without the lport lock
Robert Love3a3b42b2009-11-03 11:47:39 -08001031 * held, but it will lock, call an _enter_* function or fc_lport_error()
Robert Love42e9a922008-12-09 15:10:17 -08001032 * and then unlock the lport.
1033 */
Chris Leech7cccc152009-11-03 11:47:07 -08001034static void fc_lport_ns_resp(struct fc_seq *sp, struct fc_frame *fp,
1035 void *lp_arg)
Robert Love42e9a922008-12-09 15:10:17 -08001036{
1037 struct fc_lport *lport = lp_arg;
1038 struct fc_frame_header *fh;
1039 struct fc_ct_hdr *ct;
1040
Chris Leech7cccc152009-11-03 11:47:07 -08001041 FC_LPORT_DBG(lport, "Received a ns %s\n", fc_els_resp_type(fp));
Joe Eykholtf657d292009-08-25 14:03:21 -07001042
Robert Love42e9a922008-12-09 15:10:17 -08001043 if (fp == ERR_PTR(-FC_EX_CLOSED))
1044 return;
1045
1046 mutex_lock(&lport->lp_mutex);
1047
Chris Leech7cccc152009-11-03 11:47:07 -08001048 if (lport->state < LPORT_ST_RNN_ID || lport->state > LPORT_ST_RFT_ID) {
1049 FC_LPORT_DBG(lport, "Received a name server response, "
Robert Love3a3b42b2009-11-03 11:47:39 -08001050 "but in state %s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001051 if (IS_ERR(fp))
1052 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001053 goto out;
1054 }
1055
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001056 if (IS_ERR(fp)) {
1057 fc_lport_error(lport, fp);
1058 goto err;
1059 }
1060
Robert Love42e9a922008-12-09 15:10:17 -08001061 fh = fc_frame_header_get(fp);
1062 ct = fc_frame_payload_get(fp, sizeof(*ct));
1063
1064 if (fh && ct && fh->fh_type == FC_TYPE_CT &&
1065 ct->ct_fs_type == FC_FST_DIR &&
1066 ct->ct_fs_subtype == FC_NS_SUBTYPE &&
1067 ntohs(ct->ct_cmd) == FC_FS_ACC)
Chris Leech7cccc152009-11-03 11:47:07 -08001068 switch (lport->state) {
1069 case LPORT_ST_RNN_ID:
Chris Leechc914f7d2009-11-03 11:47:12 -08001070 fc_lport_enter_ns(lport, LPORT_ST_RSNN_NN);
Chris Leech7cccc152009-11-03 11:47:07 -08001071 break;
1072 case LPORT_ST_RSNN_NN:
Chris Leechc914f7d2009-11-03 11:47:12 -08001073 fc_lport_enter_ns(lport, LPORT_ST_RSPN_ID);
Chris Leech7cccc152009-11-03 11:47:07 -08001074 break;
1075 case LPORT_ST_RSPN_ID:
Chris Leechc914f7d2009-11-03 11:47:12 -08001076 fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
Chris Leech7cccc152009-11-03 11:47:07 -08001077 break;
1078 case LPORT_ST_RFT_ID:
1079 fc_lport_enter_scr(lport);
1080 break;
1081 default:
1082 /* should have already been caught by state checks */
1083 break;
1084 }
Robert Love42e9a922008-12-09 15:10:17 -08001085 else
1086 fc_lport_error(lport, fp);
1087out:
1088 fc_frame_free(fp);
1089err:
1090 mutex_unlock(&lport->lp_mutex);
1091}
1092
1093/**
Robert Love34f42a02009-02-27 10:55:45 -08001094 * fc_lport_scr_resp() - Handle response to State Change Register (SCR) request
Robert Love3a3b42b2009-11-03 11:47:39 -08001095 * @sp: current sequence in SCR exchange
1096 * @fp: response frame
Robert Love42e9a922008-12-09 15:10:17 -08001097 * @lp_arg: Fibre Channel lport port instance that sent the registration request
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_scr_resp(struct fc_seq *sp, struct fc_frame *fp,
1104 void *lp_arg)
1105{
1106 struct fc_lport *lport = lp_arg;
1107 u8 op;
1108
Joe Eykholtf657d292009-08-25 14:03:21 -07001109 FC_LPORT_DBG(lport, "Received a SCR %s\n", fc_els_resp_type(fp));
1110
Robert Love42e9a922008-12-09 15:10:17 -08001111 if (fp == ERR_PTR(-FC_EX_CLOSED))
1112 return;
1113
1114 mutex_lock(&lport->lp_mutex);
1115
Robert Love42e9a922008-12-09 15:10:17 -08001116 if (lport->state != LPORT_ST_SCR) {
Robert Love74147052009-06-10 15:31:10 -07001117 FC_LPORT_DBG(lport, "Received a SCR response, but in state "
1118 "%s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001119 if (IS_ERR(fp))
1120 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001121 goto out;
1122 }
1123
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001124 if (IS_ERR(fp)) {
1125 fc_lport_error(lport, fp);
1126 goto err;
1127 }
1128
Robert Love42e9a922008-12-09 15:10:17 -08001129 op = fc_frame_payload_op(fp);
1130 if (op == ELS_LS_ACC)
1131 fc_lport_enter_ready(lport);
1132 else
1133 fc_lport_error(lport, fp);
1134
1135out:
1136 fc_frame_free(fp);
1137err:
1138 mutex_unlock(&lport->lp_mutex);
1139}
1140
1141/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001142 * fc_lport_enter_scr() - Send a SCR (State Change Register) request
1143 * @lport: The local port to register for state changes
Robert Love42e9a922008-12-09 15:10:17 -08001144 *
1145 * Locking Note: The lport lock is expected to be held before calling
1146 * this routine.
1147 */
1148static void fc_lport_enter_scr(struct fc_lport *lport)
1149{
1150 struct fc_frame *fp;
1151
Robert Love74147052009-06-10 15:31:10 -07001152 FC_LPORT_DBG(lport, "Entered SCR state from %s state\n",
1153 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001154
1155 fc_lport_state_enter(lport, LPORT_ST_SCR);
1156
1157 fp = fc_frame_alloc(lport, sizeof(struct fc_els_scr));
1158 if (!fp) {
1159 fc_lport_error(lport, fp);
1160 return;
1161 }
1162
Joe Eykholta46f3272009-08-25 14:00:55 -07001163 if (!lport->tt.elsct_send(lport, FC_FID_FCTRL, fp, ELS_SCR,
Robert Love42e9a922008-12-09 15:10:17 -08001164 fc_lport_scr_resp, lport, lport->e_d_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001165 fc_lport_error(lport, NULL);
Robert Love42e9a922008-12-09 15:10:17 -08001166}
1167
1168/**
Chris Leechc914f7d2009-11-03 11:47:12 -08001169 * fc_lport_enter_ns() - register some object with the name server
Robert Love42e9a922008-12-09 15:10:17 -08001170 * @lport: Fibre Channel local port to register
1171 *
1172 * Locking Note: The lport lock is expected to be held before calling
1173 * this routine.
1174 */
Chris Leechc914f7d2009-11-03 11:47:12 -08001175static void fc_lport_enter_ns(struct fc_lport *lport, enum fc_lport_state state)
Robert Love42e9a922008-12-09 15:10:17 -08001176{
1177 struct fc_frame *fp;
Chris Leechc914f7d2009-11-03 11:47:12 -08001178 enum fc_ns_req cmd;
1179 int size = sizeof(struct fc_ct_hdr);
Chris Leechc9866a52009-11-03 11:47:01 -08001180 size_t len;
1181
Chris Leechc914f7d2009-11-03 11:47:12 -08001182 FC_LPORT_DBG(lport, "Entered %s state from %s state\n",
1183 fc_lport_state_names[state],
Chris Leechc9866a52009-11-03 11:47:01 -08001184 fc_lport_state(lport));
1185
Chris Leechc914f7d2009-11-03 11:47:12 -08001186 fc_lport_state_enter(lport, state);
Chris Leechc9866a52009-11-03 11:47:01 -08001187
Chris Leechc914f7d2009-11-03 11:47:12 -08001188 switch (state) {
1189 case LPORT_ST_RNN_ID:
1190 cmd = FC_NS_RNN_ID;
1191 size += sizeof(struct fc_ns_rn_id);
1192 break;
1193 case LPORT_ST_RSNN_NN:
1194 len = strnlen(fc_host_symbolic_name(lport->host), 255);
1195 /* if there is no symbolic name, skip to RFT_ID */
1196 if (!len)
1197 return fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
1198 cmd = FC_NS_RSNN_NN;
1199 size += sizeof(struct fc_ns_rsnn) + len;
1200 break;
1201 case LPORT_ST_RSPN_ID:
1202 len = strnlen(fc_host_symbolic_name(lport->host), 255);
1203 /* if there is no symbolic name, skip to RFT_ID */
1204 if (!len)
1205 return fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
1206 cmd = FC_NS_RSPN_ID;
1207 size += sizeof(struct fc_ns_rspn) + len;
1208 break;
1209 case LPORT_ST_RFT_ID:
1210 cmd = FC_NS_RFT_ID;
1211 size += sizeof(struct fc_ns_rft);
1212 break;
1213 default:
1214 fc_lport_error(lport, NULL);
1215 return;
1216 }
1217
1218 fp = fc_frame_alloc(lport, size);
Chris Leechc9866a52009-11-03 11:47:01 -08001219 if (!fp) {
1220 fc_lport_error(lport, fp);
1221 return;
1222 }
1223
Chris Leechc914f7d2009-11-03 11:47:12 -08001224 if (!lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, cmd,
Chris Leech7cccc152009-11-03 11:47:07 -08001225 fc_lport_ns_resp,
Chris Leechc9c7bd72009-11-03 11:46:51 -08001226 lport, lport->e_d_tov))
1227 fc_lport_error(lport, fp);
1228}
1229
Robert Love42e9a922008-12-09 15:10:17 -08001230static struct fc_rport_operations fc_lport_rport_ops = {
1231 .event_callback = fc_lport_rport_callback,
1232};
1233
1234/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001235 * fc_rport_enter_dns() - Create a fc_rport for the name server
1236 * @lport: The local port requesting a remote port for the name server
Robert Love42e9a922008-12-09 15:10:17 -08001237 *
1238 * Locking Note: The lport lock is expected to be held before calling
1239 * this routine.
1240 */
1241static void fc_lport_enter_dns(struct fc_lport *lport)
1242{
Joe Eykholtab28f1f2009-08-25 14:00:34 -07001243 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -08001244
Robert Love74147052009-06-10 15:31:10 -07001245 FC_LPORT_DBG(lport, "Entered DNS state from %s state\n",
1246 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001247
1248 fc_lport_state_enter(lport, LPORT_ST_DNS);
1249
Joe Eykholt48f00902009-08-25 14:01:50 -07001250 mutex_lock(&lport->disc.disc_mutex);
Robert Love9737e6a2009-08-25 14:02:59 -07001251 rdata = lport->tt.rport_create(lport, FC_FID_DIR_SERV);
Joe Eykholt48f00902009-08-25 14:01:50 -07001252 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001253 if (!rdata)
Robert Love42e9a922008-12-09 15:10:17 -08001254 goto err;
1255
Robert Love42e9a922008-12-09 15:10:17 -08001256 rdata->ops = &fc_lport_rport_ops;
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001257 lport->tt.rport_login(rdata);
Robert Love42e9a922008-12-09 15:10:17 -08001258 return;
1259
1260err:
1261 fc_lport_error(lport, NULL);
1262}
1263
1264/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001265 * fc_lport_timeout() - Handler for the retry_work timer
1266 * @work: The work struct of the local port
Robert Love42e9a922008-12-09 15:10:17 -08001267 */
1268static void fc_lport_timeout(struct work_struct *work)
1269{
1270 struct fc_lport *lport =
1271 container_of(work, struct fc_lport,
1272 retry_work.work);
1273
1274 mutex_lock(&lport->lp_mutex);
1275
1276 switch (lport->state) {
Joe Eykholtb1d9fd52009-07-29 17:04:22 -07001277 case LPORT_ST_DISABLED:
Robert Love42e9a922008-12-09 15:10:17 -08001278 WARN_ON(1);
1279 break;
Joe Eykholt22655ac2009-10-21 16:27:22 -07001280 case LPORT_ST_READY:
1281 WARN_ON(1);
1282 break;
1283 case LPORT_ST_RESET:
1284 break;
Robert Love42e9a922008-12-09 15:10:17 -08001285 case LPORT_ST_FLOGI:
1286 fc_lport_enter_flogi(lport);
1287 break;
1288 case LPORT_ST_DNS:
1289 fc_lport_enter_dns(lport);
1290 break;
Chris Leechc9c7bd72009-11-03 11:46:51 -08001291 case LPORT_ST_RNN_ID:
Chris Leech5baa17c2009-11-03 11:46:56 -08001292 case LPORT_ST_RSNN_NN:
Chris Leechc9866a52009-11-03 11:47:01 -08001293 case LPORT_ST_RSPN_ID:
Robert Love42e9a922008-12-09 15:10:17 -08001294 case LPORT_ST_RFT_ID:
Chris Leechc914f7d2009-11-03 11:47:12 -08001295 fc_lport_enter_ns(lport, lport->state);
Robert Love42e9a922008-12-09 15:10:17 -08001296 break;
1297 case LPORT_ST_SCR:
1298 fc_lport_enter_scr(lport);
1299 break;
1300 case LPORT_ST_LOGO:
1301 fc_lport_enter_logo(lport);
1302 break;
1303 }
1304
1305 mutex_unlock(&lport->lp_mutex);
1306}
1307
1308/**
Robert Love34f42a02009-02-27 10:55:45 -08001309 * fc_lport_logo_resp() - Handle response to LOGO request
Robert Love3a3b42b2009-11-03 11:47:39 -08001310 * @sp: The sequence that the LOGO was on
1311 * @fp: The LOGO frame
1312 * @lp_arg: The lport port that received the LOGO request
Robert Love42e9a922008-12-09 15:10:17 -08001313 *
1314 * Locking Note: This function will be called without the lport lock
Robert Love3a3b42b2009-11-03 11:47:39 -08001315 * held, but it will lock, call an _enter_* function or fc_lport_error()
Robert Love42e9a922008-12-09 15:10:17 -08001316 * and then unlock the lport.
1317 */
Chris Leech11b56182009-11-03 11:46:29 -08001318void fc_lport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
Robert Love3a3b42b2009-11-03 11:47:39 -08001319 void *lp_arg)
Robert Love42e9a922008-12-09 15:10:17 -08001320{
1321 struct fc_lport *lport = lp_arg;
1322 u8 op;
1323
Joe Eykholtf657d292009-08-25 14:03:21 -07001324 FC_LPORT_DBG(lport, "Received a LOGO %s\n", fc_els_resp_type(fp));
1325
Robert Love42e9a922008-12-09 15:10:17 -08001326 if (fp == ERR_PTR(-FC_EX_CLOSED))
1327 return;
1328
1329 mutex_lock(&lport->lp_mutex);
1330
Robert Love42e9a922008-12-09 15:10:17 -08001331 if (lport->state != LPORT_ST_LOGO) {
Robert Love74147052009-06-10 15:31:10 -07001332 FC_LPORT_DBG(lport, "Received a LOGO response, but in state "
1333 "%s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001334 if (IS_ERR(fp))
1335 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001336 goto out;
1337 }
1338
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001339 if (IS_ERR(fp)) {
1340 fc_lport_error(lport, fp);
1341 goto err;
1342 }
1343
Robert Love42e9a922008-12-09 15:10:17 -08001344 op = fc_frame_payload_op(fp);
1345 if (op == ELS_LS_ACC)
Joe Eykholt1190d922009-07-29 17:04:27 -07001346 fc_lport_enter_disabled(lport);
Robert Love42e9a922008-12-09 15:10:17 -08001347 else
1348 fc_lport_error(lport, fp);
1349
1350out:
1351 fc_frame_free(fp);
1352err:
1353 mutex_unlock(&lport->lp_mutex);
1354}
Chris Leech11b56182009-11-03 11:46:29 -08001355EXPORT_SYMBOL(fc_lport_logo_resp);
Robert Love42e9a922008-12-09 15:10:17 -08001356
1357/**
Robert Love34f42a02009-02-27 10:55:45 -08001358 * fc_rport_enter_logo() - Logout of the fabric
Robert Love3a3b42b2009-11-03 11:47:39 -08001359 * @lport: The local port to be logged out
Robert Love42e9a922008-12-09 15:10:17 -08001360 *
1361 * Locking Note: The lport lock is expected to be held before calling
1362 * this routine.
1363 */
1364static void fc_lport_enter_logo(struct fc_lport *lport)
1365{
1366 struct fc_frame *fp;
1367 struct fc_els_logo *logo;
1368
Robert Love74147052009-06-10 15:31:10 -07001369 FC_LPORT_DBG(lport, "Entered LOGO state from %s state\n",
1370 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001371
1372 fc_lport_state_enter(lport, LPORT_ST_LOGO);
Chris Leech8faecdd2009-11-03 11:46:19 -08001373 fc_vports_linkchange(lport);
Robert Love42e9a922008-12-09 15:10:17 -08001374
Robert Love42e9a922008-12-09 15:10:17 -08001375 fp = fc_frame_alloc(lport, sizeof(*logo));
1376 if (!fp) {
1377 fc_lport_error(lport, fp);
1378 return;
1379 }
1380
Joe Eykholta46f3272009-08-25 14:00:55 -07001381 if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp, ELS_LOGO,
1382 fc_lport_logo_resp, lport, lport->e_d_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001383 fc_lport_error(lport, NULL);
Robert Love42e9a922008-12-09 15:10:17 -08001384}
1385
1386/**
Robert Love34f42a02009-02-27 10:55:45 -08001387 * fc_lport_flogi_resp() - Handle response to FLOGI request
Robert Love3a3b42b2009-11-03 11:47:39 -08001388 * @sp: The sequence that the FLOGI was on
1389 * @fp: The FLOGI response frame
1390 * @lp_arg: The lport port that received the FLOGI response
Robert Love42e9a922008-12-09 15:10:17 -08001391 *
1392 * Locking Note: This function will be called without the lport lock
Robert Love3a3b42b2009-11-03 11:47:39 -08001393 * held, but it will lock, call an _enter_* function or fc_lport_error()
Robert Love42e9a922008-12-09 15:10:17 -08001394 * and then unlock the lport.
1395 */
Chris Leech11b56182009-11-03 11:46:29 -08001396void fc_lport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
Robert Love3a3b42b2009-11-03 11:47:39 -08001397 void *lp_arg)
Robert Love42e9a922008-12-09 15:10:17 -08001398{
1399 struct fc_lport *lport = lp_arg;
1400 struct fc_frame_header *fh;
1401 struct fc_els_flogi *flp;
1402 u32 did;
1403 u16 csp_flags;
1404 unsigned int r_a_tov;
1405 unsigned int e_d_tov;
1406 u16 mfs;
1407
Joe Eykholtf657d292009-08-25 14:03:21 -07001408 FC_LPORT_DBG(lport, "Received a FLOGI %s\n", fc_els_resp_type(fp));
1409
Robert Love42e9a922008-12-09 15:10:17 -08001410 if (fp == ERR_PTR(-FC_EX_CLOSED))
1411 return;
1412
1413 mutex_lock(&lport->lp_mutex);
1414
Robert Love42e9a922008-12-09 15:10:17 -08001415 if (lport->state != LPORT_ST_FLOGI) {
Robert Love74147052009-06-10 15:31:10 -07001416 FC_LPORT_DBG(lport, "Received a FLOGI response, but in state "
1417 "%s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001418 if (IS_ERR(fp))
1419 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001420 goto out;
1421 }
1422
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001423 if (IS_ERR(fp)) {
1424 fc_lport_error(lport, fp);
1425 goto err;
1426 }
1427
Robert Love42e9a922008-12-09 15:10:17 -08001428 fh = fc_frame_header_get(fp);
1429 did = ntoh24(fh->fh_d_id);
1430 if (fc_frame_payload_op(fp) == ELS_LS_ACC && did != 0) {
1431
Robert Love74147052009-06-10 15:31:10 -07001432 printk(KERN_INFO "libfc: Assigned FID (%6x) in FLOGI response\n",
1433 did);
Robert Love42e9a922008-12-09 15:10:17 -08001434 fc_host_port_id(lport->host) = did;
1435
1436 flp = fc_frame_payload_get(fp, sizeof(*flp));
1437 if (flp) {
1438 mfs = ntohs(flp->fl_csp.sp_bb_data) &
1439 FC_SP_BB_DATA_MASK;
1440 if (mfs >= FC_SP_MIN_MAX_PAYLOAD &&
1441 mfs < lport->mfs)
1442 lport->mfs = mfs;
1443 csp_flags = ntohs(flp->fl_csp.sp_features);
1444 r_a_tov = ntohl(flp->fl_csp.sp_r_a_tov);
1445 e_d_tov = ntohl(flp->fl_csp.sp_e_d_tov);
1446 if (csp_flags & FC_SP_FT_EDTR)
1447 e_d_tov /= 1000000;
Chris Leechdb36c062009-11-03 11:46:24 -08001448
1449 lport->npiv_enabled = !!(csp_flags & FC_SP_FT_NPIV_ACC);
1450
Robert Love42e9a922008-12-09 15:10:17 -08001451 if ((csp_flags & FC_SP_FT_FPORT) == 0) {
1452 if (e_d_tov > lport->e_d_tov)
1453 lport->e_d_tov = e_d_tov;
1454 lport->r_a_tov = 2 * e_d_tov;
Robert Love74147052009-06-10 15:31:10 -07001455 printk(KERN_INFO "libfc: Port (%6x) entered "
1456 "point to point mode\n", did);
Robert Love42e9a922008-12-09 15:10:17 -08001457 fc_lport_ptp_setup(lport, ntoh24(fh->fh_s_id),
1458 get_unaligned_be64(
1459 &flp->fl_wwpn),
1460 get_unaligned_be64(
1461 &flp->fl_wwnn));
1462 } else {
1463 lport->e_d_tov = e_d_tov;
1464 lport->r_a_tov = r_a_tov;
1465 fc_host_fabric_name(lport->host) =
1466 get_unaligned_be64(&flp->fl_wwnn);
1467 fc_lport_enter_dns(lport);
1468 }
1469 }
Robert Love42e9a922008-12-09 15:10:17 -08001470 } else {
Robert Love74147052009-06-10 15:31:10 -07001471 FC_LPORT_DBG(lport, "Bad FLOGI response\n");
Robert Love42e9a922008-12-09 15:10:17 -08001472 }
1473
1474out:
1475 fc_frame_free(fp);
1476err:
1477 mutex_unlock(&lport->lp_mutex);
1478}
Chris Leech11b56182009-11-03 11:46:29 -08001479EXPORT_SYMBOL(fc_lport_flogi_resp);
Robert Love42e9a922008-12-09 15:10:17 -08001480
1481/**
Robert Love34f42a02009-02-27 10:55:45 -08001482 * fc_rport_enter_flogi() - Send a FLOGI request to the fabric manager
Robert Love42e9a922008-12-09 15:10:17 -08001483 * @lport: Fibre Channel local port to be logged in to the fabric
1484 *
1485 * Locking Note: The lport lock is expected to be held before calling
1486 * this routine.
1487 */
1488void fc_lport_enter_flogi(struct fc_lport *lport)
1489{
1490 struct fc_frame *fp;
1491
Robert Love74147052009-06-10 15:31:10 -07001492 FC_LPORT_DBG(lport, "Entered FLOGI state from %s state\n",
1493 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001494
1495 fc_lport_state_enter(lport, LPORT_ST_FLOGI);
1496
1497 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
1498 if (!fp)
1499 return fc_lport_error(lport, fp);
1500
Chris Leechdb36c062009-11-03 11:46:24 -08001501 if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp,
1502 lport->vport ? ELS_FDISC : ELS_FLOGI,
Robert Love42e9a922008-12-09 15:10:17 -08001503 fc_lport_flogi_resp, lport, lport->e_d_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001504 fc_lport_error(lport, NULL);
Robert Love42e9a922008-12-09 15:10:17 -08001505}
1506
Robert Love3a3b42b2009-11-03 11:47:39 -08001507/**
1508 * fc_lport_config() - Configure a fc_lport
1509 * @lport: The local port to be configured
1510 */
Robert Love42e9a922008-12-09 15:10:17 -08001511int fc_lport_config(struct fc_lport *lport)
1512{
1513 INIT_DELAYED_WORK(&lport->retry_work, fc_lport_timeout);
1514 mutex_init(&lport->lp_mutex);
1515
Joe Eykholtb1d9fd52009-07-29 17:04:22 -07001516 fc_lport_state_enter(lport, LPORT_ST_DISABLED);
Robert Love42e9a922008-12-09 15:10:17 -08001517
1518 fc_lport_add_fc4_type(lport, FC_TYPE_FCP);
1519 fc_lport_add_fc4_type(lport, FC_TYPE_CT);
1520
1521 return 0;
1522}
1523EXPORT_SYMBOL(fc_lport_config);
1524
Robert Love3a3b42b2009-11-03 11:47:39 -08001525/**
1526 * fc_lport_init() - Initialize the lport layer for a local port
1527 * @lport: The local port to initialize the exchange layer for
1528 */
Robert Love42e9a922008-12-09 15:10:17 -08001529int fc_lport_init(struct fc_lport *lport)
1530{
1531 if (!lport->tt.lport_recv)
1532 lport->tt.lport_recv = fc_lport_recv_req;
1533
1534 if (!lport->tt.lport_reset)
1535 lport->tt.lport_reset = fc_lport_reset;
1536
1537 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
1538 fc_host_node_name(lport->host) = lport->wwnn;
1539 fc_host_port_name(lport->host) = lport->wwpn;
1540 fc_host_supported_classes(lport->host) = FC_COS_CLASS3;
1541 memset(fc_host_supported_fc4s(lport->host), 0,
1542 sizeof(fc_host_supported_fc4s(lport->host)));
1543 fc_host_supported_fc4s(lport->host)[2] = 1;
1544 fc_host_supported_fc4s(lport->host)[7] = 1;
1545
1546 /* This value is also unchanging */
1547 memset(fc_host_active_fc4s(lport->host), 0,
1548 sizeof(fc_host_active_fc4s(lport->host)));
1549 fc_host_active_fc4s(lport->host)[2] = 1;
1550 fc_host_active_fc4s(lport->host)[7] = 1;
1551 fc_host_maxframe_size(lport->host) = lport->mfs;
1552 fc_host_supported_speeds(lport->host) = 0;
1553 if (lport->link_supported_speeds & FC_PORTSPEED_1GBIT)
1554 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_1GBIT;
1555 if (lport->link_supported_speeds & FC_PORTSPEED_10GBIT)
1556 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_10GBIT;
1557
Robert Love42e9a922008-12-09 15:10:17 -08001558 return 0;
1559}
1560EXPORT_SYMBOL(fc_lport_init);
Steve Maa51ab392009-11-03 11:47:34 -08001561
1562/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001563 * fc_lport_bsg_resp() - The common response handler for FC Passthrough requests
1564 * @sp: The sequence for the FC Passthrough response
1565 * @fp: The response frame
1566 * @info_arg: The BSG info that the response is for
Steve Maa51ab392009-11-03 11:47:34 -08001567 */
1568static void fc_lport_bsg_resp(struct fc_seq *sp, struct fc_frame *fp,
1569 void *info_arg)
1570{
1571 struct fc_bsg_info *info = info_arg;
1572 struct fc_bsg_job *job = info->job;
1573 struct fc_lport *lport = info->lport;
1574 struct fc_frame_header *fh;
1575 size_t len;
1576 void *buf;
1577
1578 if (IS_ERR(fp)) {
1579 job->reply->result = (PTR_ERR(fp) == -FC_EX_CLOSED) ?
1580 -ECONNABORTED : -ETIMEDOUT;
1581 job->reply_len = sizeof(uint32_t);
1582 job->state_flags |= FC_RQST_STATE_DONE;
1583 job->job_done(job);
1584 kfree(info);
1585 return;
1586 }
1587
1588 mutex_lock(&lport->lp_mutex);
1589 fh = fc_frame_header_get(fp);
1590 len = fr_len(fp) - sizeof(*fh);
1591 buf = fc_frame_payload_get(fp, 0);
1592
1593 if (fr_sof(fp) == FC_SOF_I3 && !ntohs(fh->fh_seq_cnt)) {
1594 /* Get the response code from the first frame payload */
1595 unsigned short cmd = (info->rsp_code == FC_FS_ACC) ?
1596 ntohs(((struct fc_ct_hdr *)buf)->ct_cmd) :
1597 (unsigned short)fc_frame_payload_op(fp);
1598
1599 /* Save the reply status of the job */
1600 job->reply->reply_data.ctels_reply.status =
1601 (cmd == info->rsp_code) ?
1602 FC_CTELS_STATUS_OK : FC_CTELS_STATUS_REJECT;
1603 }
1604
1605 job->reply->reply_payload_rcv_len +=
1606 fc_copy_buffer_to_sglist(buf, len, info->sg, &info->nents,
1607 &info->offset, KM_BIO_SRC_IRQ, NULL);
1608
1609 if (fr_eof(fp) == FC_EOF_T &&
1610 (ntoh24(fh->fh_f_ctl) & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
1611 (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
1612 if (job->reply->reply_payload_rcv_len >
1613 job->reply_payload.payload_len)
1614 job->reply->reply_payload_rcv_len =
1615 job->reply_payload.payload_len;
1616 job->reply->result = 0;
1617 job->state_flags |= FC_RQST_STATE_DONE;
1618 job->job_done(job);
1619 kfree(info);
1620 }
1621 fc_frame_free(fp);
1622 mutex_unlock(&lport->lp_mutex);
1623}
1624
1625/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001626 * fc_lport_els_request() - Send ELS passthrough request
1627 * @job: The BSG Passthrough job
Steve Maa51ab392009-11-03 11:47:34 -08001628 * @lport: The local port sending the request
Robert Love3a3b42b2009-11-03 11:47:39 -08001629 * @did: The destination port id
Steve Maa51ab392009-11-03 11:47:34 -08001630 *
1631 * Locking Note: The lport lock is expected to be held before calling
1632 * this routine.
1633 */
1634static int fc_lport_els_request(struct fc_bsg_job *job,
1635 struct fc_lport *lport,
1636 u32 did, u32 tov)
1637{
1638 struct fc_bsg_info *info;
1639 struct fc_frame *fp;
1640 struct fc_frame_header *fh;
1641 char *pp;
1642 int len;
1643
1644 fp = fc_frame_alloc(lport, sizeof(struct fc_frame_header) +
1645 job->request_payload.payload_len);
1646 if (!fp)
1647 return -ENOMEM;
1648
1649 len = job->request_payload.payload_len;
1650 pp = fc_frame_payload_get(fp, len);
1651
1652 sg_copy_to_buffer(job->request_payload.sg_list,
1653 job->request_payload.sg_cnt,
1654 pp, len);
1655
1656 fh = fc_frame_header_get(fp);
1657 fh->fh_r_ctl = FC_RCTL_ELS_REQ;
1658 hton24(fh->fh_d_id, did);
1659 hton24(fh->fh_s_id, fc_host_port_id(lport->host));
1660 fh->fh_type = FC_TYPE_ELS;
1661 hton24(fh->fh_f_ctl, FC_FC_FIRST_SEQ |
1662 FC_FC_END_SEQ | FC_FC_SEQ_INIT);
1663 fh->fh_cs_ctl = 0;
1664 fh->fh_df_ctl = 0;
1665 fh->fh_parm_offset = 0;
1666
1667 info = kzalloc(sizeof(struct fc_bsg_info), GFP_KERNEL);
1668 if (!info) {
1669 fc_frame_free(fp);
1670 return -ENOMEM;
1671 }
1672
1673 info->job = job;
1674 info->lport = lport;
1675 info->rsp_code = ELS_LS_ACC;
1676 info->nents = job->reply_payload.sg_cnt;
1677 info->sg = job->reply_payload.sg_list;
1678
1679 if (!lport->tt.exch_seq_send(lport, fp, fc_lport_bsg_resp,
1680 NULL, info, tov))
1681 return -ECOMM;
1682 return 0;
1683}
1684
1685/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001686 * fc_lport_ct_request() - Send CT Passthrough request
1687 * @job: The BSG Passthrough job
Steve Maa51ab392009-11-03 11:47:34 -08001688 * @lport: The local port sending the request
1689 * @did: The destination FC-ID
Robert Love3a3b42b2009-11-03 11:47:39 -08001690 * @tov: The timeout period to wait for the response
Steve Maa51ab392009-11-03 11:47:34 -08001691 *
1692 * Locking Note: The lport lock is expected to be held before calling
1693 * this routine.
1694 */
1695static int fc_lport_ct_request(struct fc_bsg_job *job,
1696 struct fc_lport *lport, u32 did, u32 tov)
1697{
1698 struct fc_bsg_info *info;
1699 struct fc_frame *fp;
1700 struct fc_frame_header *fh;
1701 struct fc_ct_req *ct;
1702 size_t len;
1703
1704 fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
1705 job->request_payload.payload_len);
1706 if (!fp)
1707 return -ENOMEM;
1708
1709 len = job->request_payload.payload_len;
1710 ct = fc_frame_payload_get(fp, len);
1711
1712 sg_copy_to_buffer(job->request_payload.sg_list,
1713 job->request_payload.sg_cnt,
1714 ct, len);
1715
1716 fh = fc_frame_header_get(fp);
1717 fh->fh_r_ctl = FC_RCTL_DD_UNSOL_CTL;
1718 hton24(fh->fh_d_id, did);
1719 hton24(fh->fh_s_id, fc_host_port_id(lport->host));
1720 fh->fh_type = FC_TYPE_CT;
1721 hton24(fh->fh_f_ctl, FC_FC_FIRST_SEQ |
1722 FC_FC_END_SEQ | FC_FC_SEQ_INIT);
1723 fh->fh_cs_ctl = 0;
1724 fh->fh_df_ctl = 0;
1725 fh->fh_parm_offset = 0;
1726
1727 info = kzalloc(sizeof(struct fc_bsg_info), GFP_KERNEL);
1728 if (!info) {
1729 fc_frame_free(fp);
1730 return -ENOMEM;
1731 }
1732
1733 info->job = job;
1734 info->lport = lport;
1735 info->rsp_code = FC_FS_ACC;
1736 info->nents = job->reply_payload.sg_cnt;
1737 info->sg = job->reply_payload.sg_list;
1738
1739 if (!lport->tt.exch_seq_send(lport, fp, fc_lport_bsg_resp,
1740 NULL, info, tov))
1741 return -ECOMM;
1742 return 0;
1743}
1744
1745/**
1746 * fc_lport_bsg_request() - The common entry point for sending
Robert Love3a3b42b2009-11-03 11:47:39 -08001747 * FC Passthrough requests
1748 * @job: The BSG passthrough job
Steve Maa51ab392009-11-03 11:47:34 -08001749 */
1750int fc_lport_bsg_request(struct fc_bsg_job *job)
1751{
1752 struct request *rsp = job->req->next_rq;
1753 struct Scsi_Host *shost = job->shost;
1754 struct fc_lport *lport = shost_priv(shost);
1755 struct fc_rport *rport;
1756 struct fc_rport_priv *rdata;
1757 int rc = -EINVAL;
1758 u32 did;
1759
1760 job->reply->reply_payload_rcv_len = 0;
1761 rsp->resid_len = job->reply_payload.payload_len;
1762
1763 mutex_lock(&lport->lp_mutex);
1764
1765 switch (job->request->msgcode) {
1766 case FC_BSG_RPT_ELS:
1767 rport = job->rport;
1768 if (!rport)
1769 break;
1770
1771 rdata = rport->dd_data;
1772 rc = fc_lport_els_request(job, lport, rport->port_id,
1773 rdata->e_d_tov);
1774 break;
1775
1776 case FC_BSG_RPT_CT:
1777 rport = job->rport;
1778 if (!rport)
1779 break;
1780
1781 rdata = rport->dd_data;
1782 rc = fc_lport_ct_request(job, lport, rport->port_id,
1783 rdata->e_d_tov);
1784 break;
1785
1786 case FC_BSG_HST_CT:
1787 did = ntoh24(job->request->rqst_data.h_ct.port_id);
1788 if (did == FC_FID_DIR_SERV)
Robert Love3a3b42b2009-11-03 11:47:39 -08001789 rdata = lport->dns_rdata;
Steve Maa51ab392009-11-03 11:47:34 -08001790 else
1791 rdata = lport->tt.rport_lookup(lport, did);
1792
1793 if (!rdata)
1794 break;
1795
1796 rc = fc_lport_ct_request(job, lport, did, rdata->e_d_tov);
1797 break;
1798
1799 case FC_BSG_HST_ELS_NOLOGIN:
1800 did = ntoh24(job->request->rqst_data.h_els.port_id);
1801 rc = fc_lport_els_request(job, lport, did, lport->e_d_tov);
1802 break;
1803 }
1804
1805 mutex_unlock(&lport->lp_mutex);
1806 return rc;
1807}
1808EXPORT_SYMBOL(fc_lport_bsg_request);