blob: be3c2cee829fff4905435c3608e87bb9c2184c03 [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
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020059 * notification. Currently, successful discovery causes the lport to take no
Robert Love42e9a922008-12-09 15:10:17 -080060 * 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090091#include <linux/slab.h>
Robert Love42e9a922008-12-09 15:10:17 -080092#include <asm/unaligned.h>
93
94#include <scsi/fc/fc_gs.h>
95
96#include <scsi/libfc.h>
97#include <scsi/fc_encode.h>
Steve Maa51ab392009-11-03 11:47:34 -080098#include <linux/scatterlist.h>
Robert Love42e9a922008-12-09 15:10:17 -080099
Robert Love8866a5d2009-11-03 11:45:58 -0800100#include "fc_libfc.h"
101
Robert Love42e9a922008-12-09 15:10:17 -0800102/* Fabric IDs to use for point-to-point mode, chosen on whims. */
103#define FC_LOCAL_PTP_FID_LO 0x010101
104#define FC_LOCAL_PTP_FID_HI 0x010102
105
106#define DNS_DELAY 3 /* Discovery delay after RSCN (in seconds)*/
107
Robert Love42e9a922008-12-09 15:10:17 -0800108static void fc_lport_error(struct fc_lport *, struct fc_frame *);
109
110static void fc_lport_enter_reset(struct fc_lport *);
111static void fc_lport_enter_flogi(struct fc_lport *);
112static void fc_lport_enter_dns(struct fc_lport *);
Chris Leechc914f7d2009-11-03 11:47:12 -0800113static void fc_lport_enter_ns(struct fc_lport *, enum fc_lport_state);
Robert Love42e9a922008-12-09 15:10:17 -0800114static void fc_lport_enter_scr(struct fc_lport *);
115static void fc_lport_enter_ready(struct fc_lport *);
116static void fc_lport_enter_logo(struct fc_lport *);
117
118static const char *fc_lport_state_names[] = {
Joe Eykholtb1d9fd52009-07-29 17:04:22 -0700119 [LPORT_ST_DISABLED] = "disabled",
Robert Love42e9a922008-12-09 15:10:17 -0800120 [LPORT_ST_FLOGI] = "FLOGI",
121 [LPORT_ST_DNS] = "dNS",
Chris Leechc9c7bd72009-11-03 11:46:51 -0800122 [LPORT_ST_RNN_ID] = "RNN_ID",
Chris Leech5baa17c2009-11-03 11:46:56 -0800123 [LPORT_ST_RSNN_NN] = "RSNN_NN",
Chris Leechc9866a52009-11-03 11:47:01 -0800124 [LPORT_ST_RSPN_ID] = "RSPN_ID",
Robert Love42e9a922008-12-09 15:10:17 -0800125 [LPORT_ST_RFT_ID] = "RFT_ID",
Joe Eykholtab593b12009-11-03 11:49:27 -0800126 [LPORT_ST_RFF_ID] = "RFF_ID",
Robert Love42e9a922008-12-09 15:10:17 -0800127 [LPORT_ST_SCR] = "SCR",
128 [LPORT_ST_READY] = "Ready",
129 [LPORT_ST_LOGO] = "LOGO",
130 [LPORT_ST_RESET] = "reset",
131};
132
Steve Maa51ab392009-11-03 11:47:34 -0800133/**
134 * struct fc_bsg_info - FC Passthrough managemet structure
135 * @job: The passthrough job
136 * @lport: The local port to pass through a command
137 * @rsp_code: The expected response code
Robert Love3a3b42b2009-11-03 11:47:39 -0800138 * @sg: job->reply_payload.sg_list
Steve Maa51ab392009-11-03 11:47:34 -0800139 * @nents: job->reply_payload.sg_cnt
140 * @offset: The offset into the response data
141 */
142struct fc_bsg_info {
143 struct fc_bsg_job *job;
144 struct fc_lport *lport;
145 u16 rsp_code;
146 struct scatterlist *sg;
147 u32 nents;
148 size_t offset;
149};
150
Robert Love3a3b42b2009-11-03 11:47:39 -0800151/**
152 * fc_frame_drop() - Dummy frame handler
153 * @lport: The local port the frame was received on
154 * @fp: The received frame
155 */
Robert Love42e9a922008-12-09 15:10:17 -0800156static int fc_frame_drop(struct fc_lport *lport, struct fc_frame *fp)
157{
158 fc_frame_free(fp);
159 return 0;
160}
161
162/**
Robert Love34f42a02009-02-27 10:55:45 -0800163 * fc_lport_rport_callback() - Event handler for rport events
Robert Love42e9a922008-12-09 15:10:17 -0800164 * @lport: The lport which is receiving the event
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700165 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800166 * @event: The event that occured
167 *
168 * Locking Note: The rport lock should not be held when calling
169 * this function.
170 */
171static void fc_lport_rport_callback(struct fc_lport *lport,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700172 struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -0800173 enum fc_rport_event event)
174{
Chris Leechce8b5df2010-04-09 14:23:10 -0700175 FC_LPORT_DBG(lport, "Received a %d event for port (%6.6x)\n", event,
Joe Eykholtf211fa52009-08-25 14:01:01 -0700176 rdata->ids.port_id);
Robert Love42e9a922008-12-09 15:10:17 -0800177
Joe Eykholtb5cbf082009-08-25 14:01:44 -0700178 mutex_lock(&lport->lp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800179 switch (event) {
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700180 case RPORT_EV_READY:
Joe Eykholtb5cbf082009-08-25 14:01:44 -0700181 if (lport->state == LPORT_ST_DNS) {
Robert Love3a3b42b2009-11-03 11:47:39 -0800182 lport->dns_rdata = rdata;
Chris Leechc914f7d2009-11-03 11:47:12 -0800183 fc_lport_enter_ns(lport, LPORT_ST_RNN_ID);
Joe Eykholtb5cbf082009-08-25 14:01:44 -0700184 } else {
185 FC_LPORT_DBG(lport, "Received an READY event "
Chris Leechce8b5df2010-04-09 14:23:10 -0700186 "on port (%6.6x) for the directory "
Joe Eykholtb5cbf082009-08-25 14:01:44 -0700187 "server, but the lport is not "
188 "in the DNS state, it's in the "
189 "%d state", rdata->ids.port_id,
190 lport->state);
191 lport->tt.rport_logoff(rdata);
192 }
Robert Love42e9a922008-12-09 15:10:17 -0800193 break;
194 case RPORT_EV_LOGO:
195 case RPORT_EV_FAILED:
196 case RPORT_EV_STOP:
Robert Love3a3b42b2009-11-03 11:47:39 -0800197 lport->dns_rdata = NULL;
Robert Love42e9a922008-12-09 15:10:17 -0800198 break;
199 case RPORT_EV_NONE:
200 break;
201 }
Joe Eykholtb5cbf082009-08-25 14:01:44 -0700202 mutex_unlock(&lport->lp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800203}
204
205/**
Robert Love34f42a02009-02-27 10:55:45 -0800206 * fc_lport_state() - Return a string which represents the lport's state
Robert Love42e9a922008-12-09 15:10:17 -0800207 * @lport: The lport whose state is to converted to a string
208 */
209static const char *fc_lport_state(struct fc_lport *lport)
210{
211 const char *cp;
212
213 cp = fc_lport_state_names[lport->state];
214 if (!cp)
215 cp = "unknown";
216 return cp;
217}
218
219/**
Robert Love34f42a02009-02-27 10:55:45 -0800220 * fc_lport_ptp_setup() - Create an rport for point-to-point mode
Robert Love3a3b42b2009-11-03 11:47:39 -0800221 * @lport: The lport to attach the ptp rport to
222 * @remote_fid: The FID of the ptp rport
Robert Love42e9a922008-12-09 15:10:17 -0800223 * @remote_wwpn: The WWPN of the ptp rport
224 * @remote_wwnn: The WWNN of the ptp rport
225 */
226static void fc_lport_ptp_setup(struct fc_lport *lport,
227 u32 remote_fid, u64 remote_wwpn,
228 u64 remote_wwnn)
229{
Joe Eykholt48f00902009-08-25 14:01:50 -0700230 mutex_lock(&lport->disc.disc_mutex);
Joe Eykholt2f2ac4a2010-03-12 16:07:46 -0800231 if (lport->ptp_rdata) {
Robert Love3a3b42b2009-11-03 11:47:39 -0800232 lport->tt.rport_logoff(lport->ptp_rdata);
Joe Eykholt2f2ac4a2010-03-12 16:07:46 -0800233 kref_put(&lport->ptp_rdata->kref, lport->tt.rport_destroy);
234 }
Robert Love3a3b42b2009-11-03 11:47:39 -0800235 lport->ptp_rdata = lport->tt.rport_create(lport, remote_fid);
Joe Eykholt2f2ac4a2010-03-12 16:07:46 -0800236 kref_get(&lport->ptp_rdata->kref);
Robert Love3a3b42b2009-11-03 11:47:39 -0800237 lport->ptp_rdata->ids.port_name = remote_wwpn;
238 lport->ptp_rdata->ids.node_name = remote_wwnn;
Joe Eykholt48f00902009-08-25 14:01:50 -0700239 mutex_unlock(&lport->disc.disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800240
Robert Love3a3b42b2009-11-03 11:47:39 -0800241 lport->tt.rport_login(lport->ptp_rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800242
243 fc_lport_enter_ready(lport);
244}
245
Robert Love3a3b42b2009-11-03 11:47:39 -0800246/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800247 * fc_get_host_port_state() - Return the port state of the given Scsi_Host
248 * @shost: The SCSI host whose port state is to be determined
249 */
Robert Love42e9a922008-12-09 15:10:17 -0800250void fc_get_host_port_state(struct Scsi_Host *shost)
251{
Robert Love3a3b42b2009-11-03 11:47:39 -0800252 struct fc_lport *lport = shost_priv(shost);
Robert Love42e9a922008-12-09 15:10:17 -0800253
Robert Love3a3b42b2009-11-03 11:47:39 -0800254 mutex_lock(&lport->lp_mutex);
255 if (!lport->link_up)
Chris Leech8faecdd2009-11-03 11:46:19 -0800256 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
Robert Love42e9a922008-12-09 15:10:17 -0800257 else
Robert Love3a3b42b2009-11-03 11:47:39 -0800258 switch (lport->state) {
Chris Leech8faecdd2009-11-03 11:46:19 -0800259 case LPORT_ST_READY:
260 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
261 break;
262 default:
263 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
264 }
Robert Love3a3b42b2009-11-03 11:47:39 -0800265 mutex_unlock(&lport->lp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800266}
267EXPORT_SYMBOL(fc_get_host_port_state);
268
Robert Love3a3b42b2009-11-03 11:47:39 -0800269/**
270 * fc_get_host_speed() - Return the speed of the given Scsi_Host
271 * @shost: The SCSI host whose port speed is to be determined
272 */
Robert Love42e9a922008-12-09 15:10:17 -0800273void fc_get_host_speed(struct Scsi_Host *shost)
274{
275 struct fc_lport *lport = shost_priv(shost);
276
277 fc_host_speed(shost) = lport->link_speed;
278}
279EXPORT_SYMBOL(fc_get_host_speed);
280
Robert Love3a3b42b2009-11-03 11:47:39 -0800281/**
282 * fc_get_host_stats() - Return the Scsi_Host's statistics
283 * @shost: The SCSI host whose statistics are to be returned
284 */
Robert Love42e9a922008-12-09 15:10:17 -0800285struct fc_host_statistics *fc_get_host_stats(struct Scsi_Host *shost)
286{
Robert Love42e9a922008-12-09 15:10:17 -0800287 struct fc_host_statistics *fcoe_stats;
Robert Love3a3b42b2009-11-03 11:47:39 -0800288 struct fc_lport *lport = shost_priv(shost);
Robert Love42e9a922008-12-09 15:10:17 -0800289 struct timespec v0, v1;
Robert Love582b45b2009-03-31 15:51:50 -0700290 unsigned int cpu;
Robert Love42e9a922008-12-09 15:10:17 -0800291
Robert Love3a3b42b2009-11-03 11:47:39 -0800292 fcoe_stats = &lport->host_stats;
Robert Love42e9a922008-12-09 15:10:17 -0800293 memset(fcoe_stats, 0, sizeof(struct fc_host_statistics));
294
295 jiffies_to_timespec(jiffies, &v0);
Robert Love3a3b42b2009-11-03 11:47:39 -0800296 jiffies_to_timespec(lport->boot_time, &v1);
Robert Love42e9a922008-12-09 15:10:17 -0800297 fcoe_stats->seconds_since_last_reset = (v0.tv_sec - v1.tv_sec);
298
Robert Love582b45b2009-03-31 15:51:50 -0700299 for_each_possible_cpu(cpu) {
300 struct fcoe_dev_stats *stats;
301
Robert Love3a3b42b2009-11-03 11:47:39 -0800302 stats = per_cpu_ptr(lport->dev_stats, cpu);
Robert Love582b45b2009-03-31 15:51:50 -0700303
Robert Love42e9a922008-12-09 15:10:17 -0800304 fcoe_stats->tx_frames += stats->TxFrames;
305 fcoe_stats->tx_words += stats->TxWords;
306 fcoe_stats->rx_frames += stats->RxFrames;
307 fcoe_stats->rx_words += stats->RxWords;
308 fcoe_stats->error_frames += stats->ErrorFrames;
309 fcoe_stats->invalid_crc_count += stats->InvalidCRCCount;
310 fcoe_stats->fcp_input_requests += stats->InputRequests;
311 fcoe_stats->fcp_output_requests += stats->OutputRequests;
312 fcoe_stats->fcp_control_requests += stats->ControlRequests;
313 fcoe_stats->fcp_input_megabytes += stats->InputMegabytes;
314 fcoe_stats->fcp_output_megabytes += stats->OutputMegabytes;
315 fcoe_stats->link_failure_count += stats->LinkFailureCount;
316 }
317 fcoe_stats->lip_count = -1;
318 fcoe_stats->nos_count = -1;
319 fcoe_stats->loss_of_sync_count = -1;
320 fcoe_stats->loss_of_signal_count = -1;
321 fcoe_stats->prim_seq_protocol_err_count = -1;
322 fcoe_stats->dumped_frames = -1;
323 return fcoe_stats;
324}
325EXPORT_SYMBOL(fc_get_host_stats);
326
Robert Love3a3b42b2009-11-03 11:47:39 -0800327/**
328 * fc_lport_flogi_fill() - Fill in FLOGI command for request
329 * @lport: The local port the FLOGI is for
330 * @flogi: The FLOGI command
331 * @op: The opcode
Robert Love42e9a922008-12-09 15:10:17 -0800332 */
Robert Love3a3b42b2009-11-03 11:47:39 -0800333static void fc_lport_flogi_fill(struct fc_lport *lport,
334 struct fc_els_flogi *flogi,
335 unsigned int op)
Robert Love42e9a922008-12-09 15:10:17 -0800336{
337 struct fc_els_csp *sp;
338 struct fc_els_cssp *cp;
339
340 memset(flogi, 0, sizeof(*flogi));
341 flogi->fl_cmd = (u8) op;
342 put_unaligned_be64(lport->wwpn, &flogi->fl_wwpn);
343 put_unaligned_be64(lport->wwnn, &flogi->fl_wwnn);
344 sp = &flogi->fl_csp;
345 sp->sp_hi_ver = 0x20;
346 sp->sp_lo_ver = 0x20;
347 sp->sp_bb_cred = htons(10); /* this gets set by gateway */
348 sp->sp_bb_data = htons((u16) lport->mfs);
349 cp = &flogi->fl_cssp[3 - 1]; /* class 3 parameters */
350 cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
351 if (op != ELS_FLOGI) {
352 sp->sp_features = htons(FC_SP_FT_CIRO);
353 sp->sp_tot_seq = htons(255); /* seq. we accept */
354 sp->sp_rel_off = htons(0x1f);
355 sp->sp_e_d_tov = htonl(lport->e_d_tov);
356
357 cp->cp_rdfs = htons((u16) lport->mfs);
358 cp->cp_con_seq = htons(255);
359 cp->cp_open_seq = 1;
360 }
361}
362
Robert Love3a3b42b2009-11-03 11:47:39 -0800363/**
364 * fc_lport_add_fc4_type() - Add a supported FC-4 type to a local port
365 * @lport: The local port to add a new FC-4 type to
366 * @type: The new FC-4 type
Robert Love42e9a922008-12-09 15:10:17 -0800367 */
368static void fc_lport_add_fc4_type(struct fc_lport *lport, enum fc_fh_type type)
369{
370 __be32 *mp;
371
372 mp = &lport->fcts.ff_type_map[type / FC_NS_BPW];
373 *mp = htonl(ntohl(*mp) | 1UL << (type % FC_NS_BPW));
374}
375
376/**
Robert Love34f42a02009-02-27 10:55:45 -0800377 * fc_lport_recv_rlir_req() - Handle received Registered Link Incident Report.
Robert Love3a3b42b2009-11-03 11:47:39 -0800378 * @sp: The sequence in the RLIR exchange
379 * @fp: The RLIR request frame
Robert Love42e9a922008-12-09 15:10:17 -0800380 * @lport: Fibre Channel local port recieving the RLIR
Robert Love42e9a922008-12-09 15:10:17 -0800381 *
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700382 * Locking Note: The lport lock is expected to be held before calling
Robert Love42e9a922008-12-09 15:10:17 -0800383 * this function.
384 */
385static void fc_lport_recv_rlir_req(struct fc_seq *sp, struct fc_frame *fp,
386 struct fc_lport *lport)
387{
Robert Love74147052009-06-10 15:31:10 -0700388 FC_LPORT_DBG(lport, "Received RLIR request while in state %s\n",
389 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800390
391 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
392 fc_frame_free(fp);
393}
394
395/**
Robert Love34f42a02009-02-27 10:55:45 -0800396 * fc_lport_recv_echo_req() - Handle received ECHO request
Robert Love3a3b42b2009-11-03 11:47:39 -0800397 * @sp: The sequence in the ECHO exchange
398 * @fp: ECHO request frame
399 * @lport: The local port recieving the ECHO
Robert Love42e9a922008-12-09 15:10:17 -0800400 *
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700401 * Locking Note: The lport lock is expected to be held before calling
Robert Love42e9a922008-12-09 15:10:17 -0800402 * this function.
403 */
404static void fc_lport_recv_echo_req(struct fc_seq *sp, struct fc_frame *in_fp,
405 struct fc_lport *lport)
406{
407 struct fc_frame *fp;
408 struct fc_exch *ep = fc_seq_exch(sp);
409 unsigned int len;
410 void *pp;
411 void *dp;
412 u32 f_ctl;
413
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700414 FC_LPORT_DBG(lport, "Received ECHO request while in state %s\n",
Robert Love74147052009-06-10 15:31:10 -0700415 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800416
417 len = fr_len(in_fp) - sizeof(struct fc_frame_header);
418 pp = fc_frame_payload_get(in_fp, len);
419
420 if (len < sizeof(__be32))
421 len = sizeof(__be32);
422
423 fp = fc_frame_alloc(lport, len);
424 if (fp) {
425 dp = fc_frame_payload_get(fp, len);
426 memcpy(dp, pp, len);
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700427 *((__be32 *)dp) = htonl(ELS_LS_ACC << 24);
Robert Love42e9a922008-12-09 15:10:17 -0800428 sp = lport->tt.seq_start_next(sp);
429 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
430 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
431 FC_TYPE_ELS, f_ctl, 0);
432 lport->tt.seq_send(lport, sp, fp);
433 }
434 fc_frame_free(in_fp);
435}
436
437/**
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700438 * fc_lport_recv_rnid_req() - Handle received Request Node ID data request
439 * @sp: The sequence in the RNID exchange
440 * @fp: The RNID request frame
441 * @lport: The local port recieving the RNID
Robert Love42e9a922008-12-09 15:10:17 -0800442 *
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700443 * Locking Note: The lport lock is expected to be held before calling
Robert Love42e9a922008-12-09 15:10:17 -0800444 * this function.
445 */
446static void fc_lport_recv_rnid_req(struct fc_seq *sp, struct fc_frame *in_fp,
447 struct fc_lport *lport)
448{
449 struct fc_frame *fp;
450 struct fc_exch *ep = fc_seq_exch(sp);
451 struct fc_els_rnid *req;
452 struct {
453 struct fc_els_rnid_resp rnid;
454 struct fc_els_rnid_cid cid;
455 struct fc_els_rnid_gen gen;
456 } *rp;
457 struct fc_seq_els_data rjt_data;
458 u8 fmt;
459 size_t len;
460 u32 f_ctl;
461
Robert Love74147052009-06-10 15:31:10 -0700462 FC_LPORT_DBG(lport, "Received RNID request while in state %s\n",
463 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800464
465 req = fc_frame_payload_get(in_fp, sizeof(*req));
466 if (!req) {
467 rjt_data.fp = NULL;
468 rjt_data.reason = ELS_RJT_LOGIC;
469 rjt_data.explan = ELS_EXPL_NONE;
470 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
471 } else {
472 fmt = req->rnid_fmt;
473 len = sizeof(*rp);
474 if (fmt != ELS_RNIDF_GEN ||
475 ntohl(lport->rnid_gen.rnid_atype) == 0) {
476 fmt = ELS_RNIDF_NONE; /* nothing to provide */
477 len -= sizeof(rp->gen);
478 }
479 fp = fc_frame_alloc(lport, len);
480 if (fp) {
481 rp = fc_frame_payload_get(fp, len);
482 memset(rp, 0, len);
483 rp->rnid.rnid_cmd = ELS_LS_ACC;
484 rp->rnid.rnid_fmt = fmt;
485 rp->rnid.rnid_cid_len = sizeof(rp->cid);
486 rp->cid.rnid_wwpn = htonll(lport->wwpn);
487 rp->cid.rnid_wwnn = htonll(lport->wwnn);
488 if (fmt == ELS_RNIDF_GEN) {
489 rp->rnid.rnid_sid_len = sizeof(rp->gen);
490 memcpy(&rp->gen, &lport->rnid_gen,
491 sizeof(rp->gen));
492 }
493 sp = lport->tt.seq_start_next(sp);
494 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
495 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
496 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
497 FC_TYPE_ELS, f_ctl, 0);
498 lport->tt.seq_send(lport, sp, fp);
499 }
500 }
501 fc_frame_free(in_fp);
502}
503
504/**
Robert Love34f42a02009-02-27 10:55:45 -0800505 * fc_lport_recv_logo_req() - Handle received fabric LOGO request
Robert Love3a3b42b2009-11-03 11:47:39 -0800506 * @sp: The sequence in the LOGO exchange
507 * @fp: The LOGO request frame
508 * @lport: The local port recieving the LOGO
Robert Love42e9a922008-12-09 15:10:17 -0800509 *
510 * Locking Note: The lport lock is exected to be held before calling
511 * this function.
512 */
513static void fc_lport_recv_logo_req(struct fc_seq *sp, struct fc_frame *fp,
514 struct fc_lport *lport)
515{
516 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
517 fc_lport_enter_reset(lport);
518 fc_frame_free(fp);
519}
520
521/**
Robert Love34f42a02009-02-27 10:55:45 -0800522 * fc_fabric_login() - Start the lport state machine
Robert Love3a3b42b2009-11-03 11:47:39 -0800523 * @lport: The local port that should log into the fabric
Robert Love42e9a922008-12-09 15:10:17 -0800524 *
525 * Locking Note: This function should not be called
526 * with the lport lock held.
527 */
528int fc_fabric_login(struct fc_lport *lport)
529{
530 int rc = -1;
531
532 mutex_lock(&lport->lp_mutex);
Vasu Dev55a66d32009-12-10 09:59:31 -0800533 if (lport->state == LPORT_ST_DISABLED ||
534 lport->state == LPORT_ST_LOGO) {
535 fc_lport_state_enter(lport, LPORT_ST_RESET);
Robert Love42e9a922008-12-09 15:10:17 -0800536 fc_lport_enter_reset(lport);
537 rc = 0;
538 }
539 mutex_unlock(&lport->lp_mutex);
540
541 return rc;
542}
543EXPORT_SYMBOL(fc_fabric_login);
544
545/**
Chris Leech8faecdd2009-11-03 11:46:19 -0800546 * __fc_linkup() - Handler for transport linkup events
547 * @lport: The lport whose link is up
548 *
549 * Locking: must be called with the lp_mutex held
550 */
551void __fc_linkup(struct fc_lport *lport)
552{
553 if (!lport->link_up) {
554 lport->link_up = 1;
555
556 if (lport->state == LPORT_ST_RESET)
557 fc_lport_enter_flogi(lport);
558 }
559}
560
561/**
Robert Love34f42a02009-02-27 10:55:45 -0800562 * fc_linkup() - Handler for transport linkup events
Robert Love3a3b42b2009-11-03 11:47:39 -0800563 * @lport: The local port whose link is up
Robert Love42e9a922008-12-09 15:10:17 -0800564 */
565void fc_linkup(struct fc_lport *lport)
566{
Chris Leechce8b5df2010-04-09 14:23:10 -0700567 printk(KERN_INFO "host%d: libfc: Link up on port (%6.6x)\n",
Robert Love7b2787e2010-05-07 15:18:41 -0700568 lport->host->host_no, lport->port_id);
Robert Love42e9a922008-12-09 15:10:17 -0800569
570 mutex_lock(&lport->lp_mutex);
Chris Leech8faecdd2009-11-03 11:46:19 -0800571 __fc_linkup(lport);
Robert Love42e9a922008-12-09 15:10:17 -0800572 mutex_unlock(&lport->lp_mutex);
573}
574EXPORT_SYMBOL(fc_linkup);
575
576/**
Chris Leech8faecdd2009-11-03 11:46:19 -0800577 * __fc_linkdown() - Handler for transport linkdown events
578 * @lport: The lport whose link is down
579 *
580 * Locking: must be called with the lp_mutex held
581 */
582void __fc_linkdown(struct fc_lport *lport)
583{
584 if (lport->link_up) {
585 lport->link_up = 0;
586 fc_lport_enter_reset(lport);
587 lport->tt.fcp_cleanup(lport);
588 }
589}
590
591/**
Robert Love34f42a02009-02-27 10:55:45 -0800592 * fc_linkdown() - Handler for transport linkdown events
Robert Love3a3b42b2009-11-03 11:47:39 -0800593 * @lport: The local port whose link is down
Robert Love42e9a922008-12-09 15:10:17 -0800594 */
595void fc_linkdown(struct fc_lport *lport)
596{
Chris Leechce8b5df2010-04-09 14:23:10 -0700597 printk(KERN_INFO "host%d: libfc: Link down on port (%6.6x)\n",
Robert Love7b2787e2010-05-07 15:18:41 -0700598 lport->host->host_no, lport->port_id);
Robert Love42e9a922008-12-09 15:10:17 -0800599
Chris Leech8faecdd2009-11-03 11:46:19 -0800600 mutex_lock(&lport->lp_mutex);
601 __fc_linkdown(lport);
Robert Love42e9a922008-12-09 15:10:17 -0800602 mutex_unlock(&lport->lp_mutex);
603}
604EXPORT_SYMBOL(fc_linkdown);
605
606/**
Robert Love34f42a02009-02-27 10:55:45 -0800607 * fc_fabric_logoff() - Logout of the fabric
Robert Love3a3b42b2009-11-03 11:47:39 -0800608 * @lport: The local port to logoff the fabric
Robert Love42e9a922008-12-09 15:10:17 -0800609 *
610 * Return value:
611 * 0 for success, -1 for failure
Robert Love34f42a02009-02-27 10:55:45 -0800612 */
Robert Love42e9a922008-12-09 15:10:17 -0800613int fc_fabric_logoff(struct fc_lport *lport)
614{
615 lport->tt.disc_stop_final(lport);
616 mutex_lock(&lport->lp_mutex);
Robert Love3a3b42b2009-11-03 11:47:39 -0800617 if (lport->dns_rdata)
618 lport->tt.rport_logoff(lport->dns_rdata);
Abhijeet Joglekara0fd2e42009-04-21 16:27:09 -0700619 mutex_unlock(&lport->lp_mutex);
620 lport->tt.rport_flush_queue();
621 mutex_lock(&lport->lp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800622 fc_lport_enter_logo(lport);
623 mutex_unlock(&lport->lp_mutex);
Steve Maf7db2c12009-02-27 10:55:13 -0800624 cancel_delayed_work_sync(&lport->retry_work);
Robert Love42e9a922008-12-09 15:10:17 -0800625 return 0;
626}
627EXPORT_SYMBOL(fc_fabric_logoff);
628
629/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800630 * fc_lport_destroy() - Unregister a fc_lport
631 * @lport: The local port to unregister
Robert Love42e9a922008-12-09 15:10:17 -0800632 *
Robert Love42e9a922008-12-09 15:10:17 -0800633 * Note:
634 * exit routine for fc_lport instance
635 * clean-up all the allocated memory
636 * and free up other system resources.
637 *
Robert Love34f42a02009-02-27 10:55:45 -0800638 */
Robert Love42e9a922008-12-09 15:10:17 -0800639int fc_lport_destroy(struct fc_lport *lport)
640{
Abhijeet Joglekarbbf15662009-04-21 16:27:14 -0700641 mutex_lock(&lport->lp_mutex);
Joe Eykholtb1d9fd52009-07-29 17:04:22 -0700642 lport->state = LPORT_ST_DISABLED;
Abhijeet Joglekarbbf15662009-04-21 16:27:14 -0700643 lport->link_up = 0;
Robert Love42e9a922008-12-09 15:10:17 -0800644 lport->tt.frame_send = fc_frame_drop;
Abhijeet Joglekarbbf15662009-04-21 16:27:14 -0700645 mutex_unlock(&lport->lp_mutex);
646
Robert Love42e9a922008-12-09 15:10:17 -0800647 lport->tt.fcp_abort_io(lport);
Joe Eykholte9ba8b42009-07-29 17:04:33 -0700648 lport->tt.disc_stop_final(lport);
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -0800649 lport->tt.exch_mgr_reset(lport, 0, 0);
Robert Love42e9a922008-12-09 15:10:17 -0800650 return 0;
651}
652EXPORT_SYMBOL(fc_lport_destroy);
653
654/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800655 * fc_set_mfs() - Set the maximum frame size for a local port
656 * @lport: The local port to set the MFS for
657 * @mfs: The new MFS
Robert Love34f42a02009-02-27 10:55:45 -0800658 */
Robert Love42e9a922008-12-09 15:10:17 -0800659int fc_set_mfs(struct fc_lport *lport, u32 mfs)
660{
661 unsigned int old_mfs;
662 int rc = -EINVAL;
663
664 mutex_lock(&lport->lp_mutex);
665
666 old_mfs = lport->mfs;
667
668 if (mfs >= FC_MIN_MAX_FRAME) {
669 mfs &= ~3;
670 if (mfs > FC_MAX_FRAME)
671 mfs = FC_MAX_FRAME;
672 mfs -= sizeof(struct fc_frame_header);
673 lport->mfs = mfs;
674 rc = 0;
675 }
676
677 if (!rc && mfs < old_mfs)
678 fc_lport_enter_reset(lport);
679
680 mutex_unlock(&lport->lp_mutex);
681
682 return rc;
683}
684EXPORT_SYMBOL(fc_set_mfs);
685
686/**
Robert Love34f42a02009-02-27 10:55:45 -0800687 * fc_lport_disc_callback() - Callback for discovery events
Robert Love3a3b42b2009-11-03 11:47:39 -0800688 * @lport: The local port receiving the event
Robert Love42e9a922008-12-09 15:10:17 -0800689 * @event: The discovery event
690 */
691void fc_lport_disc_callback(struct fc_lport *lport, enum fc_disc_event event)
692{
693 switch (event) {
694 case DISC_EV_SUCCESS:
Robert Love74147052009-06-10 15:31:10 -0700695 FC_LPORT_DBG(lport, "Discovery succeeded\n");
Robert Love42e9a922008-12-09 15:10:17 -0800696 break;
697 case DISC_EV_FAILED:
Joe Eykholte6d8a1b2009-11-03 11:49:11 -0800698 printk(KERN_ERR "host%d: libfc: "
Chris Leechce8b5df2010-04-09 14:23:10 -0700699 "Discovery failed for port (%6.6x)\n",
Robert Love7b2787e2010-05-07 15:18:41 -0700700 lport->host->host_no, lport->port_id);
Robert Love42e9a922008-12-09 15:10:17 -0800701 mutex_lock(&lport->lp_mutex);
702 fc_lport_enter_reset(lport);
703 mutex_unlock(&lport->lp_mutex);
704 break;
705 case DISC_EV_NONE:
706 WARN_ON(1);
707 break;
708 }
709}
710
711/**
Robert Love34f42a02009-02-27 10:55:45 -0800712 * fc_rport_enter_ready() - Enter the ready state and start discovery
Robert Love3a3b42b2009-11-03 11:47:39 -0800713 * @lport: The local port that is ready
Robert Love42e9a922008-12-09 15:10:17 -0800714 *
715 * Locking Note: The lport lock is expected to be held before calling
716 * this routine.
717 */
718static void fc_lport_enter_ready(struct fc_lport *lport)
719{
Robert Love74147052009-06-10 15:31:10 -0700720 FC_LPORT_DBG(lport, "Entered READY from state %s\n",
721 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800722
723 fc_lport_state_enter(lport, LPORT_ST_READY);
Chris Leech8faecdd2009-11-03 11:46:19 -0800724 if (lport->vport)
725 fc_vport_set_state(lport->vport, FC_VPORT_ACTIVE);
726 fc_vports_linkchange(lport);
Robert Love42e9a922008-12-09 15:10:17 -0800727
Robert Love3a3b42b2009-11-03 11:47:39 -0800728 if (!lport->ptp_rdata)
Joe Eykholt29d898e2009-08-25 14:02:49 -0700729 lport->tt.disc_start(fc_lport_disc_callback, lport);
Robert Love42e9a922008-12-09 15:10:17 -0800730}
731
732/**
Joe Eykholt093bb6a2009-11-03 11:49:05 -0800733 * fc_lport_set_port_id() - set the local port Port ID
734 * @lport: The local port which will have its Port ID set.
735 * @port_id: The new port ID.
736 * @fp: The frame containing the incoming request, or NULL.
737 *
738 * Locking Note: The lport lock is expected to be held before calling
739 * this function.
740 */
741static void fc_lport_set_port_id(struct fc_lport *lport, u32 port_id,
742 struct fc_frame *fp)
743{
744 if (port_id)
Chris Leechce8b5df2010-04-09 14:23:10 -0700745 printk(KERN_INFO "host%d: Assigned Port ID %6.6x\n",
Joe Eykholt093bb6a2009-11-03 11:49:05 -0800746 lport->host->host_no, port_id);
747
Robert Love7b2787e2010-05-07 15:18:41 -0700748 lport->port_id = port_id;
749
750 /* Update the fc_host */
Joe Eykholt093bb6a2009-11-03 11:49:05 -0800751 fc_host_port_id(lport->host) = port_id;
Robert Love7b2787e2010-05-07 15:18:41 -0700752
Joe Eykholt093bb6a2009-11-03 11:49:05 -0800753 if (lport->tt.lport_set_port_id)
754 lport->tt.lport_set_port_id(lport, port_id, fp);
755}
756
757/**
Joe Eykholt3726f352010-07-20 15:20:03 -0700758 * fc_lport_set_port_id() - set the local port Port ID for point-to-multipoint
759 * @lport: The local port which will have its Port ID set.
760 * @port_id: The new port ID.
761 *
762 * Called by the lower-level driver when transport sets the local port_id.
763 * This is used in VN_port to VN_port mode for FCoE, and causes FLOGI and
764 * discovery to be skipped.
765 */
766void fc_lport_set_local_id(struct fc_lport *lport, u32 port_id)
767{
768 mutex_lock(&lport->lp_mutex);
769
770 fc_lport_set_port_id(lport, port_id, NULL);
771
772 switch (lport->state) {
773 case LPORT_ST_RESET:
774 case LPORT_ST_FLOGI:
775 if (port_id)
776 fc_lport_enter_ready(lport);
777 break;
778 default:
779 break;
780 }
781 mutex_unlock(&lport->lp_mutex);
782}
783EXPORT_SYMBOL(fc_lport_set_local_id);
784
785/**
Robert Love34f42a02009-02-27 10:55:45 -0800786 * fc_lport_recv_flogi_req() - Receive a FLOGI request
Robert Love42e9a922008-12-09 15:10:17 -0800787 * @sp_in: The sequence the FLOGI is on
Robert Love3a3b42b2009-11-03 11:47:39 -0800788 * @rx_fp: The FLOGI frame
789 * @lport: The local port that recieved the request
Robert Love42e9a922008-12-09 15:10:17 -0800790 *
791 * A received FLOGI request indicates a point-to-point connection.
792 * Accept it with the common service parameters indicating our N port.
793 * Set up to do a PLOGI if we have the higher-number WWPN.
794 *
Joe Eykholt1b69bc02009-10-21 16:27:17 -0700795 * Locking Note: The lport lock is expected to be held before calling
Robert Love42e9a922008-12-09 15:10:17 -0800796 * this function.
797 */
798static void fc_lport_recv_flogi_req(struct fc_seq *sp_in,
799 struct fc_frame *rx_fp,
800 struct fc_lport *lport)
801{
802 struct fc_frame *fp;
Robert Love42e9a922008-12-09 15:10:17 -0800803 struct fc_seq *sp;
804 struct fc_exch *ep;
805 struct fc_els_flogi *flp;
806 struct fc_els_flogi *new_flp;
807 u64 remote_wwpn;
808 u32 remote_fid;
809 u32 local_fid;
810 u32 f_ctl;
811
Robert Love74147052009-06-10 15:31:10 -0700812 FC_LPORT_DBG(lport, "Received FLOGI request while in state %s\n",
813 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -0800814
Joe Eykholt251748a2010-07-20 15:20:56 -0700815 remote_fid = fc_frame_sid(rx_fp);
Robert Love42e9a922008-12-09 15:10:17 -0800816 flp = fc_frame_payload_get(rx_fp, sizeof(*flp));
817 if (!flp)
818 goto out;
819 remote_wwpn = get_unaligned_be64(&flp->fl_wwpn);
820 if (remote_wwpn == lport->wwpn) {
Joe Eykholte6d8a1b2009-11-03 11:49:11 -0800821 printk(KERN_WARNING "host%d: libfc: Received FLOGI from port "
Chris Leech9f8f3aa2010-04-09 14:23:16 -0700822 "with same WWPN %16.16llx\n",
Joe Eykholte6d8a1b2009-11-03 11:49:11 -0800823 lport->host->host_no, remote_wwpn);
Robert Love42e9a922008-12-09 15:10:17 -0800824 goto out;
825 }
Chris Leech9f8f3aa2010-04-09 14:23:16 -0700826 FC_LPORT_DBG(lport, "FLOGI from port WWPN %16.16llx\n", remote_wwpn);
Robert Love42e9a922008-12-09 15:10:17 -0800827
828 /*
829 * XXX what is the right thing to do for FIDs?
830 * The originator might expect our S_ID to be 0xfffffe.
831 * But if so, both of us could end up with the same FID.
832 */
833 local_fid = FC_LOCAL_PTP_FID_LO;
834 if (remote_wwpn < lport->wwpn) {
835 local_fid = FC_LOCAL_PTP_FID_HI;
836 if (!remote_fid || remote_fid == local_fid)
837 remote_fid = FC_LOCAL_PTP_FID_LO;
838 } else if (!remote_fid) {
839 remote_fid = FC_LOCAL_PTP_FID_HI;
840 }
841
Joe Eykholt093bb6a2009-11-03 11:49:05 -0800842 fc_lport_set_port_id(lport, local_fid, rx_fp);
Robert Love42e9a922008-12-09 15:10:17 -0800843
844 fp = fc_frame_alloc(lport, sizeof(*flp));
845 if (fp) {
846 sp = lport->tt.seq_start_next(fr_seq(rx_fp));
847 new_flp = fc_frame_payload_get(fp, sizeof(*flp));
848 fc_lport_flogi_fill(lport, new_flp, ELS_FLOGI);
849 new_flp->fl_cmd = (u8) ELS_LS_ACC;
850
851 /*
852 * Send the response. If this fails, the originator should
853 * repeat the sequence.
854 */
855 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
856 ep = fc_seq_exch(sp);
Joe Eykholtccfc3092010-03-12 16:08:12 -0800857 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, remote_fid, local_fid,
Robert Love42e9a922008-12-09 15:10:17 -0800858 FC_TYPE_ELS, f_ctl, 0);
859 lport->tt.seq_send(lport, sp, fp);
860
861 } else {
862 fc_lport_error(lport, fp);
863 }
864 fc_lport_ptp_setup(lport, remote_fid, remote_wwpn,
865 get_unaligned_be64(&flp->fl_wwnn));
866
Robert Love42e9a922008-12-09 15:10:17 -0800867out:
868 sp = fr_seq(rx_fp);
869 fc_frame_free(rx_fp);
870}
871
872/**
Robert Love34f42a02009-02-27 10:55:45 -0800873 * fc_lport_recv_req() - The generic lport request handler
Robert Love3a3b42b2009-11-03 11:47:39 -0800874 * @lport: The local port that received the request
875 * @sp: The sequence the request is on
876 * @fp: The request frame
Robert Love42e9a922008-12-09 15:10:17 -0800877 *
878 * This function will see if the lport handles the request or
879 * if an rport should handle the request.
880 *
881 * Locking Note: This function should not be called with the lport
882 * lock held becuase it will grab the lock.
883 */
884static void fc_lport_recv_req(struct fc_lport *lport, struct fc_seq *sp,
885 struct fc_frame *fp)
886{
887 struct fc_frame_header *fh = fc_frame_header_get(fp);
888 void (*recv) (struct fc_seq *, struct fc_frame *, struct fc_lport *);
Robert Love42e9a922008-12-09 15:10:17 -0800889
890 mutex_lock(&lport->lp_mutex);
891
892 /*
893 * Handle special ELS cases like FLOGI, LOGO, and
894 * RSCN here. These don't require a session.
895 * Even if we had a session, it might not be ready.
896 */
Joe Eykholte9ba8b42009-07-29 17:04:33 -0700897 if (!lport->link_up)
898 fc_frame_free(fp);
899 else if (fh->fh_type == FC_TYPE_ELS &&
900 fh->fh_r_ctl == FC_RCTL_ELS_REQ) {
Robert Love42e9a922008-12-09 15:10:17 -0800901 /*
902 * Check opcode.
903 */
Joe Eykholt131203a2009-08-25 14:03:10 -0700904 recv = lport->tt.rport_recv_req;
Robert Love42e9a922008-12-09 15:10:17 -0800905 switch (fc_frame_payload_op(fp)) {
906 case ELS_FLOGI:
Joe Eykholta7b12a22010-07-20 15:20:08 -0700907 if (!lport->point_to_multipoint)
908 recv = fc_lport_recv_flogi_req;
Robert Love42e9a922008-12-09 15:10:17 -0800909 break;
910 case ELS_LOGO:
Joe Eykholt251748a2010-07-20 15:20:56 -0700911 if (fc_frame_sid(fp) == FC_FID_FLOGI)
Robert Love42e9a922008-12-09 15:10:17 -0800912 recv = fc_lport_recv_logo_req;
913 break;
914 case ELS_RSCN:
915 recv = lport->tt.disc_recv_req;
916 break;
917 case ELS_ECHO:
918 recv = fc_lport_recv_echo_req;
919 break;
920 case ELS_RLIR:
921 recv = fc_lport_recv_rlir_req;
922 break;
923 case ELS_RNID:
924 recv = fc_lport_recv_rnid_req;
925 break;
Robert Love42e9a922008-12-09 15:10:17 -0800926 }
927
Joe Eykholt131203a2009-08-25 14:03:10 -0700928 recv(sp, fp, lport);
Robert Love42e9a922008-12-09 15:10:17 -0800929 } else {
Robert Love74147052009-06-10 15:31:10 -0700930 FC_LPORT_DBG(lport, "dropping invalid frame (eof %x)\n",
931 fr_eof(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800932 fc_frame_free(fp);
933 }
934 mutex_unlock(&lport->lp_mutex);
935
936 /*
937 * The common exch_done for all request may not be good
938 * if any request requires longer hold on exhange. XXX
939 */
940 lport->tt.exch_done(sp);
941}
942
943/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800944 * fc_lport_reset() - Reset a local port
945 * @lport: The local port which should be reset
Robert Love42e9a922008-12-09 15:10:17 -0800946 *
947 * Locking Note: This functions should not be called with the
948 * lport lock held.
949 */
950int fc_lport_reset(struct fc_lport *lport)
951{
Steve Maf7db2c12009-02-27 10:55:13 -0800952 cancel_delayed_work_sync(&lport->retry_work);
Robert Love42e9a922008-12-09 15:10:17 -0800953 mutex_lock(&lport->lp_mutex);
954 fc_lport_enter_reset(lport);
955 mutex_unlock(&lport->lp_mutex);
956 return 0;
957}
958EXPORT_SYMBOL(fc_lport_reset);
959
960/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800961 * fc_lport_reset_locked() - Reset the local port w/ the lport lock held
962 * @lport: The local port to be reset
Robert Love42e9a922008-12-09 15:10:17 -0800963 *
964 * Locking Note: The lport lock is expected to be held before calling
965 * this routine.
966 */
Joe Eykholt1190d922009-07-29 17:04:27 -0700967static void fc_lport_reset_locked(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -0800968{
Robert Love3a3b42b2009-11-03 11:47:39 -0800969 if (lport->dns_rdata)
970 lport->tt.rport_logoff(lport->dns_rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800971
Joe Eykholt2f2ac4a2010-03-12 16:07:46 -0800972 if (lport->ptp_rdata) {
973 lport->tt.rport_logoff(lport->ptp_rdata);
974 kref_put(&lport->ptp_rdata->kref, lport->tt.rport_destroy);
975 lport->ptp_rdata = NULL;
976 }
Robert Love42e9a922008-12-09 15:10:17 -0800977
978 lport->tt.disc_stop(lport);
979
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -0800980 lport->tt.exch_mgr_reset(lport, 0, 0);
Robert Love42e9a922008-12-09 15:10:17 -0800981 fc_host_fabric_name(lport->host) = 0;
Joe Eykholt093bb6a2009-11-03 11:49:05 -0800982
Joe Eykholt3726f352010-07-20 15:20:03 -0700983 if (lport->port_id && (!lport->point_to_multipoint || !lport->link_up))
Joe Eykholt093bb6a2009-11-03 11:49:05 -0800984 fc_lport_set_port_id(lport, 0, NULL);
Joe Eykholt1190d922009-07-29 17:04:27 -0700985}
Robert Love42e9a922008-12-09 15:10:17 -0800986
Joe Eykholt1190d922009-07-29 17:04:27 -0700987/**
988 * fc_lport_enter_reset() - Reset the local port
Robert Love3a3b42b2009-11-03 11:47:39 -0800989 * @lport: The local port to be reset
Joe Eykholt1190d922009-07-29 17:04:27 -0700990 *
991 * Locking Note: The lport lock is expected to be held before calling
992 * this routine.
993 */
994static void fc_lport_enter_reset(struct fc_lport *lport)
995{
996 FC_LPORT_DBG(lport, "Entered RESET state from %s state\n",
997 fc_lport_state(lport));
998
Vasu Dev55a66d32009-12-10 09:59:31 -0800999 if (lport->state == LPORT_ST_DISABLED || lport->state == LPORT_ST_LOGO)
1000 return;
1001
Chris Leech8faecdd2009-11-03 11:46:19 -08001002 if (lport->vport) {
1003 if (lport->link_up)
1004 fc_vport_set_state(lport->vport, FC_VPORT_INITIALIZING);
1005 else
1006 fc_vport_set_state(lport->vport, FC_VPORT_LINKDOWN);
1007 }
Joe Eykholt1190d922009-07-29 17:04:27 -07001008 fc_lport_state_enter(lport, LPORT_ST_RESET);
Chris Leech8faecdd2009-11-03 11:46:19 -08001009 fc_vports_linkchange(lport);
Joe Eykholt1190d922009-07-29 17:04:27 -07001010 fc_lport_reset_locked(lport);
Vasu Devbc0e17f2009-02-27 10:54:57 -08001011 if (lport->link_up)
Robert Love42e9a922008-12-09 15:10:17 -08001012 fc_lport_enter_flogi(lport);
1013}
1014
1015/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001016 * fc_lport_enter_disabled() - Disable the local port
1017 * @lport: The local port to be reset
Joe Eykholt1190d922009-07-29 17:04:27 -07001018 *
1019 * Locking Note: The lport lock is expected to be held before calling
1020 * this routine.
1021 */
1022static void fc_lport_enter_disabled(struct fc_lport *lport)
1023{
1024 FC_LPORT_DBG(lport, "Entered disabled state from %s state\n",
1025 fc_lport_state(lport));
1026
1027 fc_lport_state_enter(lport, LPORT_ST_DISABLED);
Chris Leech8faecdd2009-11-03 11:46:19 -08001028 fc_vports_linkchange(lport);
Joe Eykholt1190d922009-07-29 17:04:27 -07001029 fc_lport_reset_locked(lport);
1030}
1031
1032/**
Robert Love34f42a02009-02-27 10:55:45 -08001033 * fc_lport_error() - Handler for any errors
Robert Love3a3b42b2009-11-03 11:47:39 -08001034 * @lport: The local port that the error was on
1035 * @fp: The error code encoded in a frame pointer
Robert Love42e9a922008-12-09 15:10:17 -08001036 *
1037 * If the error was caused by a resource allocation failure
1038 * then wait for half a second and retry, otherwise retry
1039 * after the e_d_tov time.
1040 */
1041static void fc_lport_error(struct fc_lport *lport, struct fc_frame *fp)
1042{
1043 unsigned long delay = 0;
Robert Love74147052009-06-10 15:31:10 -07001044 FC_LPORT_DBG(lport, "Error %ld in state %s, retries %d\n",
1045 PTR_ERR(fp), fc_lport_state(lport),
1046 lport->retry_count);
Robert Love42e9a922008-12-09 15:10:17 -08001047
1048 if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
1049 /*
1050 * Memory allocation failure, or the exchange timed out.
1051 * Retry after delay
1052 */
1053 if (lport->retry_count < lport->max_retry_count) {
1054 lport->retry_count++;
1055 if (!fp)
1056 delay = msecs_to_jiffies(500);
1057 else
1058 delay = msecs_to_jiffies(lport->e_d_tov);
1059
1060 schedule_delayed_work(&lport->retry_work, delay);
1061 } else {
1062 switch (lport->state) {
Joe Eykholtb1d9fd52009-07-29 17:04:22 -07001063 case LPORT_ST_DISABLED:
Robert Love42e9a922008-12-09 15:10:17 -08001064 case LPORT_ST_READY:
1065 case LPORT_ST_RESET:
Chris Leechc9c7bd72009-11-03 11:46:51 -08001066 case LPORT_ST_RNN_ID:
Chris Leech5baa17c2009-11-03 11:46:56 -08001067 case LPORT_ST_RSNN_NN:
Chris Leechc9866a52009-11-03 11:47:01 -08001068 case LPORT_ST_RSPN_ID:
Robert Love42e9a922008-12-09 15:10:17 -08001069 case LPORT_ST_RFT_ID:
Joe Eykholtab593b12009-11-03 11:49:27 -08001070 case LPORT_ST_RFF_ID:
Robert Love42e9a922008-12-09 15:10:17 -08001071 case LPORT_ST_SCR:
1072 case LPORT_ST_DNS:
1073 case LPORT_ST_FLOGI:
1074 case LPORT_ST_LOGO:
1075 fc_lport_enter_reset(lport);
1076 break;
1077 }
1078 }
1079 }
1080}
1081
1082/**
Chris Leech7cccc152009-11-03 11:47:07 -08001083 * fc_lport_ns_resp() - Handle response to a name server
Robert Love3a3b42b2009-11-03 11:47:39 -08001084 * registration exchange
1085 * @sp: current sequence in exchange
1086 * @fp: response frame
Robert Love42e9a922008-12-09 15:10:17 -08001087 * @lp_arg: Fibre Channel host port instance
1088 *
1089 * Locking Note: This function will be called without the lport lock
Robert Love3a3b42b2009-11-03 11:47:39 -08001090 * held, but it will lock, call an _enter_* function or fc_lport_error()
Robert Love42e9a922008-12-09 15:10:17 -08001091 * and then unlock the lport.
1092 */
Chris Leech7cccc152009-11-03 11:47:07 -08001093static void fc_lport_ns_resp(struct fc_seq *sp, struct fc_frame *fp,
1094 void *lp_arg)
Robert Love42e9a922008-12-09 15:10:17 -08001095{
1096 struct fc_lport *lport = lp_arg;
1097 struct fc_frame_header *fh;
1098 struct fc_ct_hdr *ct;
1099
Chris Leech7cccc152009-11-03 11:47:07 -08001100 FC_LPORT_DBG(lport, "Received a ns %s\n", fc_els_resp_type(fp));
Joe Eykholtf657d292009-08-25 14:03:21 -07001101
Robert Love42e9a922008-12-09 15:10:17 -08001102 if (fp == ERR_PTR(-FC_EX_CLOSED))
1103 return;
1104
1105 mutex_lock(&lport->lp_mutex);
1106
Joe Eykholtab593b12009-11-03 11:49:27 -08001107 if (lport->state < LPORT_ST_RNN_ID || lport->state > LPORT_ST_RFF_ID) {
Chris Leech7cccc152009-11-03 11:47:07 -08001108 FC_LPORT_DBG(lport, "Received a name server response, "
Robert Love3a3b42b2009-11-03 11:47:39 -08001109 "but in state %s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001110 if (IS_ERR(fp))
1111 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001112 goto out;
1113 }
1114
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001115 if (IS_ERR(fp)) {
1116 fc_lport_error(lport, fp);
1117 goto err;
1118 }
1119
Robert Love42e9a922008-12-09 15:10:17 -08001120 fh = fc_frame_header_get(fp);
1121 ct = fc_frame_payload_get(fp, sizeof(*ct));
1122
1123 if (fh && ct && fh->fh_type == FC_TYPE_CT &&
1124 ct->ct_fs_type == FC_FST_DIR &&
1125 ct->ct_fs_subtype == FC_NS_SUBTYPE &&
1126 ntohs(ct->ct_cmd) == FC_FS_ACC)
Chris Leech7cccc152009-11-03 11:47:07 -08001127 switch (lport->state) {
1128 case LPORT_ST_RNN_ID:
Chris Leechc914f7d2009-11-03 11:47:12 -08001129 fc_lport_enter_ns(lport, LPORT_ST_RSNN_NN);
Chris Leech7cccc152009-11-03 11:47:07 -08001130 break;
1131 case LPORT_ST_RSNN_NN:
Chris Leechc914f7d2009-11-03 11:47:12 -08001132 fc_lport_enter_ns(lport, LPORT_ST_RSPN_ID);
Chris Leech7cccc152009-11-03 11:47:07 -08001133 break;
1134 case LPORT_ST_RSPN_ID:
Chris Leechc914f7d2009-11-03 11:47:12 -08001135 fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
Chris Leech7cccc152009-11-03 11:47:07 -08001136 break;
1137 case LPORT_ST_RFT_ID:
Joe Eykholtab593b12009-11-03 11:49:27 -08001138 fc_lport_enter_ns(lport, LPORT_ST_RFF_ID);
1139 break;
1140 case LPORT_ST_RFF_ID:
Chris Leech7cccc152009-11-03 11:47:07 -08001141 fc_lport_enter_scr(lport);
1142 break;
1143 default:
1144 /* should have already been caught by state checks */
1145 break;
1146 }
Robert Love42e9a922008-12-09 15:10:17 -08001147 else
1148 fc_lport_error(lport, fp);
1149out:
1150 fc_frame_free(fp);
1151err:
1152 mutex_unlock(&lport->lp_mutex);
1153}
1154
1155/**
Robert Love34f42a02009-02-27 10:55:45 -08001156 * fc_lport_scr_resp() - Handle response to State Change Register (SCR) request
Robert Love3a3b42b2009-11-03 11:47:39 -08001157 * @sp: current sequence in SCR exchange
1158 * @fp: response frame
Robert Love42e9a922008-12-09 15:10:17 -08001159 * @lp_arg: Fibre Channel lport port instance that sent the registration request
1160 *
1161 * Locking Note: This function will be called without the lport lock
1162 * held, but it will lock, call an _enter_* function or fc_lport_error
1163 * and then unlock the lport.
1164 */
1165static void fc_lport_scr_resp(struct fc_seq *sp, struct fc_frame *fp,
1166 void *lp_arg)
1167{
1168 struct fc_lport *lport = lp_arg;
1169 u8 op;
1170
Joe Eykholtf657d292009-08-25 14:03:21 -07001171 FC_LPORT_DBG(lport, "Received a SCR %s\n", fc_els_resp_type(fp));
1172
Robert Love42e9a922008-12-09 15:10:17 -08001173 if (fp == ERR_PTR(-FC_EX_CLOSED))
1174 return;
1175
1176 mutex_lock(&lport->lp_mutex);
1177
Robert Love42e9a922008-12-09 15:10:17 -08001178 if (lport->state != LPORT_ST_SCR) {
Robert Love74147052009-06-10 15:31:10 -07001179 FC_LPORT_DBG(lport, "Received a SCR response, but in state "
1180 "%s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001181 if (IS_ERR(fp))
1182 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001183 goto out;
1184 }
1185
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001186 if (IS_ERR(fp)) {
1187 fc_lport_error(lport, fp);
1188 goto err;
1189 }
1190
Robert Love42e9a922008-12-09 15:10:17 -08001191 op = fc_frame_payload_op(fp);
1192 if (op == ELS_LS_ACC)
1193 fc_lport_enter_ready(lport);
1194 else
1195 fc_lport_error(lport, fp);
1196
1197out:
1198 fc_frame_free(fp);
1199err:
1200 mutex_unlock(&lport->lp_mutex);
1201}
1202
1203/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001204 * fc_lport_enter_scr() - Send a SCR (State Change Register) request
1205 * @lport: The local port to register for state changes
Robert Love42e9a922008-12-09 15:10:17 -08001206 *
1207 * Locking Note: The lport lock is expected to be held before calling
1208 * this routine.
1209 */
1210static void fc_lport_enter_scr(struct fc_lport *lport)
1211{
1212 struct fc_frame *fp;
1213
Robert Love74147052009-06-10 15:31:10 -07001214 FC_LPORT_DBG(lport, "Entered SCR state from %s state\n",
1215 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001216
1217 fc_lport_state_enter(lport, LPORT_ST_SCR);
1218
1219 fp = fc_frame_alloc(lport, sizeof(struct fc_els_scr));
1220 if (!fp) {
1221 fc_lport_error(lport, fp);
1222 return;
1223 }
1224
Joe Eykholta46f3272009-08-25 14:00:55 -07001225 if (!lport->tt.elsct_send(lport, FC_FID_FCTRL, fp, ELS_SCR,
Joe Eykholtb94f8952009-11-03 11:50:21 -08001226 fc_lport_scr_resp, lport,
1227 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001228 fc_lport_error(lport, NULL);
Robert Love42e9a922008-12-09 15:10:17 -08001229}
1230
1231/**
Chris Leechc914f7d2009-11-03 11:47:12 -08001232 * fc_lport_enter_ns() - register some object with the name server
Robert Love42e9a922008-12-09 15:10:17 -08001233 * @lport: Fibre Channel local port to register
1234 *
1235 * Locking Note: The lport lock is expected to be held before calling
1236 * this routine.
1237 */
Chris Leechc914f7d2009-11-03 11:47:12 -08001238static void fc_lport_enter_ns(struct fc_lport *lport, enum fc_lport_state state)
Robert Love42e9a922008-12-09 15:10:17 -08001239{
1240 struct fc_frame *fp;
Chris Leechc914f7d2009-11-03 11:47:12 -08001241 enum fc_ns_req cmd;
1242 int size = sizeof(struct fc_ct_hdr);
Chris Leechc9866a52009-11-03 11:47:01 -08001243 size_t len;
1244
Chris Leechc914f7d2009-11-03 11:47:12 -08001245 FC_LPORT_DBG(lport, "Entered %s state from %s state\n",
1246 fc_lport_state_names[state],
Chris Leechc9866a52009-11-03 11:47:01 -08001247 fc_lport_state(lport));
1248
Chris Leechc914f7d2009-11-03 11:47:12 -08001249 fc_lport_state_enter(lport, state);
Chris Leechc9866a52009-11-03 11:47:01 -08001250
Chris Leechc914f7d2009-11-03 11:47:12 -08001251 switch (state) {
1252 case LPORT_ST_RNN_ID:
1253 cmd = FC_NS_RNN_ID;
1254 size += sizeof(struct fc_ns_rn_id);
1255 break;
1256 case LPORT_ST_RSNN_NN:
1257 len = strnlen(fc_host_symbolic_name(lport->host), 255);
1258 /* if there is no symbolic name, skip to RFT_ID */
1259 if (!len)
1260 return fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
1261 cmd = FC_NS_RSNN_NN;
1262 size += sizeof(struct fc_ns_rsnn) + len;
1263 break;
1264 case LPORT_ST_RSPN_ID:
1265 len = strnlen(fc_host_symbolic_name(lport->host), 255);
1266 /* if there is no symbolic name, skip to RFT_ID */
1267 if (!len)
1268 return fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
1269 cmd = FC_NS_RSPN_ID;
1270 size += sizeof(struct fc_ns_rspn) + len;
1271 break;
1272 case LPORT_ST_RFT_ID:
1273 cmd = FC_NS_RFT_ID;
1274 size += sizeof(struct fc_ns_rft);
1275 break;
Joe Eykholtab593b12009-11-03 11:49:27 -08001276 case LPORT_ST_RFF_ID:
1277 cmd = FC_NS_RFF_ID;
1278 size += sizeof(struct fc_ns_rff_id);
1279 break;
Chris Leechc914f7d2009-11-03 11:47:12 -08001280 default:
1281 fc_lport_error(lport, NULL);
1282 return;
1283 }
1284
1285 fp = fc_frame_alloc(lport, size);
Chris Leechc9866a52009-11-03 11:47:01 -08001286 if (!fp) {
1287 fc_lport_error(lport, fp);
1288 return;
1289 }
1290
Chris Leechc914f7d2009-11-03 11:47:12 -08001291 if (!lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, cmd,
Chris Leech7cccc152009-11-03 11:47:07 -08001292 fc_lport_ns_resp,
Joe Eykholtb94f8952009-11-03 11:50:21 -08001293 lport, 3 * lport->r_a_tov))
Chris Leechc9c7bd72009-11-03 11:46:51 -08001294 fc_lport_error(lport, fp);
1295}
1296
Robert Love42e9a922008-12-09 15:10:17 -08001297static struct fc_rport_operations fc_lport_rport_ops = {
1298 .event_callback = fc_lport_rport_callback,
1299};
1300
1301/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001302 * fc_rport_enter_dns() - Create a fc_rport for the name server
1303 * @lport: The local port requesting a remote port for the name server
Robert Love42e9a922008-12-09 15:10:17 -08001304 *
1305 * Locking Note: The lport lock is expected to be held before calling
1306 * this routine.
1307 */
1308static void fc_lport_enter_dns(struct fc_lport *lport)
1309{
Joe Eykholtab28f1f2009-08-25 14:00:34 -07001310 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -08001311
Robert Love74147052009-06-10 15:31:10 -07001312 FC_LPORT_DBG(lport, "Entered DNS state from %s state\n",
1313 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001314
1315 fc_lport_state_enter(lport, LPORT_ST_DNS);
1316
Joe Eykholt48f00902009-08-25 14:01:50 -07001317 mutex_lock(&lport->disc.disc_mutex);
Robert Love9737e6a2009-08-25 14:02:59 -07001318 rdata = lport->tt.rport_create(lport, FC_FID_DIR_SERV);
Joe Eykholt48f00902009-08-25 14:01:50 -07001319 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001320 if (!rdata)
Robert Love42e9a922008-12-09 15:10:17 -08001321 goto err;
1322
Robert Love42e9a922008-12-09 15:10:17 -08001323 rdata->ops = &fc_lport_rport_ops;
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001324 lport->tt.rport_login(rdata);
Robert Love42e9a922008-12-09 15:10:17 -08001325 return;
1326
1327err:
1328 fc_lport_error(lport, NULL);
1329}
1330
1331/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001332 * fc_lport_timeout() - Handler for the retry_work timer
1333 * @work: The work struct of the local port
Robert Love42e9a922008-12-09 15:10:17 -08001334 */
1335static void fc_lport_timeout(struct work_struct *work)
1336{
1337 struct fc_lport *lport =
1338 container_of(work, struct fc_lport,
1339 retry_work.work);
1340
1341 mutex_lock(&lport->lp_mutex);
1342
1343 switch (lport->state) {
Joe Eykholtb1d9fd52009-07-29 17:04:22 -07001344 case LPORT_ST_DISABLED:
Robert Love42e9a922008-12-09 15:10:17 -08001345 WARN_ON(1);
1346 break;
Joe Eykholt22655ac2009-10-21 16:27:22 -07001347 case LPORT_ST_READY:
1348 WARN_ON(1);
1349 break;
1350 case LPORT_ST_RESET:
1351 break;
Robert Love42e9a922008-12-09 15:10:17 -08001352 case LPORT_ST_FLOGI:
1353 fc_lport_enter_flogi(lport);
1354 break;
1355 case LPORT_ST_DNS:
1356 fc_lport_enter_dns(lport);
1357 break;
Chris Leechc9c7bd72009-11-03 11:46:51 -08001358 case LPORT_ST_RNN_ID:
Chris Leech5baa17c2009-11-03 11:46:56 -08001359 case LPORT_ST_RSNN_NN:
Chris Leechc9866a52009-11-03 11:47:01 -08001360 case LPORT_ST_RSPN_ID:
Robert Love42e9a922008-12-09 15:10:17 -08001361 case LPORT_ST_RFT_ID:
Joe Eykholtab593b12009-11-03 11:49:27 -08001362 case LPORT_ST_RFF_ID:
Chris Leechc914f7d2009-11-03 11:47:12 -08001363 fc_lport_enter_ns(lport, lport->state);
Robert Love42e9a922008-12-09 15:10:17 -08001364 break;
1365 case LPORT_ST_SCR:
1366 fc_lport_enter_scr(lport);
1367 break;
1368 case LPORT_ST_LOGO:
1369 fc_lport_enter_logo(lport);
1370 break;
1371 }
1372
1373 mutex_unlock(&lport->lp_mutex);
1374}
1375
1376/**
Robert Love34f42a02009-02-27 10:55:45 -08001377 * fc_lport_logo_resp() - Handle response to LOGO request
Robert Love3a3b42b2009-11-03 11:47:39 -08001378 * @sp: The sequence that the LOGO was on
1379 * @fp: The LOGO frame
1380 * @lp_arg: The lport port that received the LOGO request
Robert Love42e9a922008-12-09 15:10:17 -08001381 *
1382 * Locking Note: This function will be called without the lport lock
Robert Love3a3b42b2009-11-03 11:47:39 -08001383 * held, but it will lock, call an _enter_* function or fc_lport_error()
Robert Love42e9a922008-12-09 15:10:17 -08001384 * and then unlock the lport.
1385 */
Chris Leech11b56182009-11-03 11:46:29 -08001386void fc_lport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
Robert Love3a3b42b2009-11-03 11:47:39 -08001387 void *lp_arg)
Robert Love42e9a922008-12-09 15:10:17 -08001388{
1389 struct fc_lport *lport = lp_arg;
1390 u8 op;
1391
Joe Eykholtf657d292009-08-25 14:03:21 -07001392 FC_LPORT_DBG(lport, "Received a LOGO %s\n", fc_els_resp_type(fp));
1393
Robert Love42e9a922008-12-09 15:10:17 -08001394 if (fp == ERR_PTR(-FC_EX_CLOSED))
1395 return;
1396
1397 mutex_lock(&lport->lp_mutex);
1398
Robert Love42e9a922008-12-09 15:10:17 -08001399 if (lport->state != LPORT_ST_LOGO) {
Robert Love74147052009-06-10 15:31:10 -07001400 FC_LPORT_DBG(lport, "Received a LOGO response, but in state "
1401 "%s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001402 if (IS_ERR(fp))
1403 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001404 goto out;
1405 }
1406
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001407 if (IS_ERR(fp)) {
1408 fc_lport_error(lport, fp);
1409 goto err;
1410 }
1411
Robert Love42e9a922008-12-09 15:10:17 -08001412 op = fc_frame_payload_op(fp);
1413 if (op == ELS_LS_ACC)
Joe Eykholt1190d922009-07-29 17:04:27 -07001414 fc_lport_enter_disabled(lport);
Robert Love42e9a922008-12-09 15:10:17 -08001415 else
1416 fc_lport_error(lport, fp);
1417
1418out:
1419 fc_frame_free(fp);
1420err:
1421 mutex_unlock(&lport->lp_mutex);
1422}
Chris Leech11b56182009-11-03 11:46:29 -08001423EXPORT_SYMBOL(fc_lport_logo_resp);
Robert Love42e9a922008-12-09 15:10:17 -08001424
1425/**
Robert Love34f42a02009-02-27 10:55:45 -08001426 * fc_rport_enter_logo() - Logout of the fabric
Robert Love3a3b42b2009-11-03 11:47:39 -08001427 * @lport: The local port to be logged out
Robert Love42e9a922008-12-09 15:10:17 -08001428 *
1429 * Locking Note: The lport lock is expected to be held before calling
1430 * this routine.
1431 */
1432static void fc_lport_enter_logo(struct fc_lport *lport)
1433{
1434 struct fc_frame *fp;
1435 struct fc_els_logo *logo;
1436
Robert Love74147052009-06-10 15:31:10 -07001437 FC_LPORT_DBG(lport, "Entered LOGO state from %s state\n",
1438 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001439
1440 fc_lport_state_enter(lport, LPORT_ST_LOGO);
Chris Leech8faecdd2009-11-03 11:46:19 -08001441 fc_vports_linkchange(lport);
Robert Love42e9a922008-12-09 15:10:17 -08001442
Robert Love42e9a922008-12-09 15:10:17 -08001443 fp = fc_frame_alloc(lport, sizeof(*logo));
1444 if (!fp) {
1445 fc_lport_error(lport, fp);
1446 return;
1447 }
1448
Joe Eykholta46f3272009-08-25 14:00:55 -07001449 if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp, ELS_LOGO,
Joe Eykholtb94f8952009-11-03 11:50:21 -08001450 fc_lport_logo_resp, lport,
1451 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001452 fc_lport_error(lport, NULL);
Robert Love42e9a922008-12-09 15:10:17 -08001453}
1454
1455/**
Robert Love34f42a02009-02-27 10:55:45 -08001456 * fc_lport_flogi_resp() - Handle response to FLOGI request
Robert Love3a3b42b2009-11-03 11:47:39 -08001457 * @sp: The sequence that the FLOGI was on
1458 * @fp: The FLOGI response frame
1459 * @lp_arg: The lport port that received the FLOGI response
Robert Love42e9a922008-12-09 15:10:17 -08001460 *
1461 * Locking Note: This function will be called without the lport lock
Robert Love3a3b42b2009-11-03 11:47:39 -08001462 * held, but it will lock, call an _enter_* function or fc_lport_error()
Robert Love42e9a922008-12-09 15:10:17 -08001463 * and then unlock the lport.
1464 */
Chris Leech11b56182009-11-03 11:46:29 -08001465void fc_lport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
Robert Love3a3b42b2009-11-03 11:47:39 -08001466 void *lp_arg)
Robert Love42e9a922008-12-09 15:10:17 -08001467{
1468 struct fc_lport *lport = lp_arg;
Robert Love42e9a922008-12-09 15:10:17 -08001469 struct fc_els_flogi *flp;
1470 u32 did;
1471 u16 csp_flags;
1472 unsigned int r_a_tov;
1473 unsigned int e_d_tov;
1474 u16 mfs;
1475
Joe Eykholtf657d292009-08-25 14:03:21 -07001476 FC_LPORT_DBG(lport, "Received a FLOGI %s\n", fc_els_resp_type(fp));
1477
Robert Love42e9a922008-12-09 15:10:17 -08001478 if (fp == ERR_PTR(-FC_EX_CLOSED))
1479 return;
1480
1481 mutex_lock(&lport->lp_mutex);
1482
Robert Love42e9a922008-12-09 15:10:17 -08001483 if (lport->state != LPORT_ST_FLOGI) {
Robert Love74147052009-06-10 15:31:10 -07001484 FC_LPORT_DBG(lport, "Received a FLOGI response, but in state "
1485 "%s\n", fc_lport_state(lport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001486 if (IS_ERR(fp))
1487 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001488 goto out;
1489 }
1490
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001491 if (IS_ERR(fp)) {
1492 fc_lport_error(lport, fp);
1493 goto err;
1494 }
1495
Joe Eykholt251748a2010-07-20 15:20:56 -07001496 did = fc_frame_did(fp);
Robert Love42e9a922008-12-09 15:10:17 -08001497 if (fc_frame_payload_op(fp) == ELS_LS_ACC && did != 0) {
Robert Love42e9a922008-12-09 15:10:17 -08001498 flp = fc_frame_payload_get(fp, sizeof(*flp));
1499 if (flp) {
1500 mfs = ntohs(flp->fl_csp.sp_bb_data) &
1501 FC_SP_BB_DATA_MASK;
1502 if (mfs >= FC_SP_MIN_MAX_PAYLOAD &&
1503 mfs < lport->mfs)
1504 lport->mfs = mfs;
1505 csp_flags = ntohs(flp->fl_csp.sp_features);
1506 r_a_tov = ntohl(flp->fl_csp.sp_r_a_tov);
1507 e_d_tov = ntohl(flp->fl_csp.sp_e_d_tov);
1508 if (csp_flags & FC_SP_FT_EDTR)
1509 e_d_tov /= 1000000;
Chris Leechdb36c062009-11-03 11:46:24 -08001510
1511 lport->npiv_enabled = !!(csp_flags & FC_SP_FT_NPIV_ACC);
1512
Robert Love42e9a922008-12-09 15:10:17 -08001513 if ((csp_flags & FC_SP_FT_FPORT) == 0) {
1514 if (e_d_tov > lport->e_d_tov)
1515 lport->e_d_tov = e_d_tov;
1516 lport->r_a_tov = 2 * e_d_tov;
Joe Eykholt093bb6a2009-11-03 11:49:05 -08001517 fc_lport_set_port_id(lport, did, fp);
Joe Eykholte6d8a1b2009-11-03 11:49:11 -08001518 printk(KERN_INFO "host%d: libfc: "
Chris Leechce8b5df2010-04-09 14:23:10 -07001519 "Port (%6.6x) entered "
Joe Eykholte6d8a1b2009-11-03 11:49:11 -08001520 "point-to-point mode\n",
1521 lport->host->host_no, did);
Joe Eykholt251748a2010-07-20 15:20:56 -07001522 fc_lport_ptp_setup(lport, fc_frame_sid(fp),
Robert Love42e9a922008-12-09 15:10:17 -08001523 get_unaligned_be64(
1524 &flp->fl_wwpn),
1525 get_unaligned_be64(
1526 &flp->fl_wwnn));
1527 } else {
1528 lport->e_d_tov = e_d_tov;
1529 lport->r_a_tov = r_a_tov;
1530 fc_host_fabric_name(lport->host) =
1531 get_unaligned_be64(&flp->fl_wwnn);
Joe Eykholt093bb6a2009-11-03 11:49:05 -08001532 fc_lport_set_port_id(lport, did, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001533 fc_lport_enter_dns(lport);
1534 }
1535 }
Robert Love42e9a922008-12-09 15:10:17 -08001536 } else {
Robert Love74147052009-06-10 15:31:10 -07001537 FC_LPORT_DBG(lport, "Bad FLOGI response\n");
Robert Love42e9a922008-12-09 15:10:17 -08001538 }
1539
1540out:
1541 fc_frame_free(fp);
1542err:
1543 mutex_unlock(&lport->lp_mutex);
1544}
Chris Leech11b56182009-11-03 11:46:29 -08001545EXPORT_SYMBOL(fc_lport_flogi_resp);
Robert Love42e9a922008-12-09 15:10:17 -08001546
1547/**
Robert Love34f42a02009-02-27 10:55:45 -08001548 * fc_rport_enter_flogi() - Send a FLOGI request to the fabric manager
Robert Love42e9a922008-12-09 15:10:17 -08001549 * @lport: Fibre Channel local port to be logged in to the fabric
1550 *
1551 * Locking Note: The lport lock is expected to be held before calling
1552 * this routine.
1553 */
1554void fc_lport_enter_flogi(struct fc_lport *lport)
1555{
1556 struct fc_frame *fp;
1557
Robert Love74147052009-06-10 15:31:10 -07001558 FC_LPORT_DBG(lport, "Entered FLOGI state from %s state\n",
1559 fc_lport_state(lport));
Robert Love42e9a922008-12-09 15:10:17 -08001560
1561 fc_lport_state_enter(lport, LPORT_ST_FLOGI);
1562
Joe Eykholt3726f352010-07-20 15:20:03 -07001563 if (lport->point_to_multipoint) {
1564 if (lport->port_id)
1565 fc_lport_enter_ready(lport);
1566 return;
1567 }
1568
Robert Love42e9a922008-12-09 15:10:17 -08001569 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
1570 if (!fp)
1571 return fc_lport_error(lport, fp);
1572
Chris Leechdb36c062009-11-03 11:46:24 -08001573 if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp,
1574 lport->vport ? ELS_FDISC : ELS_FLOGI,
Joe Eykholtb94f8952009-11-03 11:50:21 -08001575 fc_lport_flogi_resp, lport,
1576 lport->vport ? 2 * lport->r_a_tov :
1577 lport->e_d_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001578 fc_lport_error(lport, NULL);
Robert Love42e9a922008-12-09 15:10:17 -08001579}
1580
Robert Love3a3b42b2009-11-03 11:47:39 -08001581/**
1582 * fc_lport_config() - Configure a fc_lport
1583 * @lport: The local port to be configured
1584 */
Robert Love42e9a922008-12-09 15:10:17 -08001585int fc_lport_config(struct fc_lport *lport)
1586{
1587 INIT_DELAYED_WORK(&lport->retry_work, fc_lport_timeout);
1588 mutex_init(&lport->lp_mutex);
1589
Joe Eykholtb1d9fd52009-07-29 17:04:22 -07001590 fc_lport_state_enter(lport, LPORT_ST_DISABLED);
Robert Love42e9a922008-12-09 15:10:17 -08001591
1592 fc_lport_add_fc4_type(lport, FC_TYPE_FCP);
1593 fc_lport_add_fc4_type(lport, FC_TYPE_CT);
1594
1595 return 0;
1596}
1597EXPORT_SYMBOL(fc_lport_config);
1598
Robert Love3a3b42b2009-11-03 11:47:39 -08001599/**
1600 * fc_lport_init() - Initialize the lport layer for a local port
1601 * @lport: The local port to initialize the exchange layer for
1602 */
Robert Love42e9a922008-12-09 15:10:17 -08001603int fc_lport_init(struct fc_lport *lport)
1604{
1605 if (!lport->tt.lport_recv)
1606 lport->tt.lport_recv = fc_lport_recv_req;
1607
1608 if (!lport->tt.lport_reset)
1609 lport->tt.lport_reset = fc_lport_reset;
1610
1611 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
1612 fc_host_node_name(lport->host) = lport->wwnn;
1613 fc_host_port_name(lport->host) = lport->wwpn;
1614 fc_host_supported_classes(lport->host) = FC_COS_CLASS3;
1615 memset(fc_host_supported_fc4s(lport->host), 0,
1616 sizeof(fc_host_supported_fc4s(lport->host)));
1617 fc_host_supported_fc4s(lport->host)[2] = 1;
1618 fc_host_supported_fc4s(lport->host)[7] = 1;
1619
1620 /* This value is also unchanging */
1621 memset(fc_host_active_fc4s(lport->host), 0,
1622 sizeof(fc_host_active_fc4s(lport->host)));
1623 fc_host_active_fc4s(lport->host)[2] = 1;
1624 fc_host_active_fc4s(lport->host)[7] = 1;
1625 fc_host_maxframe_size(lport->host) = lport->mfs;
1626 fc_host_supported_speeds(lport->host) = 0;
1627 if (lport->link_supported_speeds & FC_PORTSPEED_1GBIT)
1628 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_1GBIT;
1629 if (lport->link_supported_speeds & FC_PORTSPEED_10GBIT)
1630 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_10GBIT;
1631
Robert Love42e9a922008-12-09 15:10:17 -08001632 return 0;
1633}
1634EXPORT_SYMBOL(fc_lport_init);
Steve Maa51ab392009-11-03 11:47:34 -08001635
1636/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001637 * fc_lport_bsg_resp() - The common response handler for FC Passthrough requests
1638 * @sp: The sequence for the FC Passthrough response
1639 * @fp: The response frame
1640 * @info_arg: The BSG info that the response is for
Steve Maa51ab392009-11-03 11:47:34 -08001641 */
1642static void fc_lport_bsg_resp(struct fc_seq *sp, struct fc_frame *fp,
1643 void *info_arg)
1644{
1645 struct fc_bsg_info *info = info_arg;
1646 struct fc_bsg_job *job = info->job;
1647 struct fc_lport *lport = info->lport;
1648 struct fc_frame_header *fh;
1649 size_t len;
1650 void *buf;
1651
1652 if (IS_ERR(fp)) {
1653 job->reply->result = (PTR_ERR(fp) == -FC_EX_CLOSED) ?
1654 -ECONNABORTED : -ETIMEDOUT;
1655 job->reply_len = sizeof(uint32_t);
1656 job->state_flags |= FC_RQST_STATE_DONE;
1657 job->job_done(job);
1658 kfree(info);
1659 return;
1660 }
1661
1662 mutex_lock(&lport->lp_mutex);
1663 fh = fc_frame_header_get(fp);
1664 len = fr_len(fp) - sizeof(*fh);
1665 buf = fc_frame_payload_get(fp, 0);
1666
1667 if (fr_sof(fp) == FC_SOF_I3 && !ntohs(fh->fh_seq_cnt)) {
1668 /* Get the response code from the first frame payload */
1669 unsigned short cmd = (info->rsp_code == FC_FS_ACC) ?
1670 ntohs(((struct fc_ct_hdr *)buf)->ct_cmd) :
1671 (unsigned short)fc_frame_payload_op(fp);
1672
1673 /* Save the reply status of the job */
1674 job->reply->reply_data.ctels_reply.status =
1675 (cmd == info->rsp_code) ?
1676 FC_CTELS_STATUS_OK : FC_CTELS_STATUS_REJECT;
1677 }
1678
1679 job->reply->reply_payload_rcv_len +=
1680 fc_copy_buffer_to_sglist(buf, len, info->sg, &info->nents,
1681 &info->offset, KM_BIO_SRC_IRQ, NULL);
1682
1683 if (fr_eof(fp) == FC_EOF_T &&
1684 (ntoh24(fh->fh_f_ctl) & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
1685 (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
1686 if (job->reply->reply_payload_rcv_len >
1687 job->reply_payload.payload_len)
1688 job->reply->reply_payload_rcv_len =
1689 job->reply_payload.payload_len;
1690 job->reply->result = 0;
1691 job->state_flags |= FC_RQST_STATE_DONE;
1692 job->job_done(job);
1693 kfree(info);
1694 }
1695 fc_frame_free(fp);
1696 mutex_unlock(&lport->lp_mutex);
1697}
1698
1699/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001700 * fc_lport_els_request() - Send ELS passthrough request
1701 * @job: The BSG Passthrough job
Steve Maa51ab392009-11-03 11:47:34 -08001702 * @lport: The local port sending the request
Robert Love3a3b42b2009-11-03 11:47:39 -08001703 * @did: The destination port id
Steve Maa51ab392009-11-03 11:47:34 -08001704 *
1705 * Locking Note: The lport lock is expected to be held before calling
1706 * this routine.
1707 */
1708static int fc_lport_els_request(struct fc_bsg_job *job,
1709 struct fc_lport *lport,
1710 u32 did, u32 tov)
1711{
1712 struct fc_bsg_info *info;
1713 struct fc_frame *fp;
1714 struct fc_frame_header *fh;
1715 char *pp;
1716 int len;
1717
Yi Zou70d919f2009-11-20 14:54:41 -08001718 fp = fc_frame_alloc(lport, job->request_payload.payload_len);
Steve Maa51ab392009-11-03 11:47:34 -08001719 if (!fp)
1720 return -ENOMEM;
1721
1722 len = job->request_payload.payload_len;
1723 pp = fc_frame_payload_get(fp, len);
1724
1725 sg_copy_to_buffer(job->request_payload.sg_list,
1726 job->request_payload.sg_cnt,
1727 pp, len);
1728
1729 fh = fc_frame_header_get(fp);
1730 fh->fh_r_ctl = FC_RCTL_ELS_REQ;
1731 hton24(fh->fh_d_id, did);
Robert Love7b2787e2010-05-07 15:18:41 -07001732 hton24(fh->fh_s_id, lport->port_id);
Steve Maa51ab392009-11-03 11:47:34 -08001733 fh->fh_type = FC_TYPE_ELS;
1734 hton24(fh->fh_f_ctl, FC_FC_FIRST_SEQ |
1735 FC_FC_END_SEQ | FC_FC_SEQ_INIT);
1736 fh->fh_cs_ctl = 0;
1737 fh->fh_df_ctl = 0;
1738 fh->fh_parm_offset = 0;
1739
1740 info = kzalloc(sizeof(struct fc_bsg_info), GFP_KERNEL);
1741 if (!info) {
1742 fc_frame_free(fp);
1743 return -ENOMEM;
1744 }
1745
1746 info->job = job;
1747 info->lport = lport;
1748 info->rsp_code = ELS_LS_ACC;
1749 info->nents = job->reply_payload.sg_cnt;
1750 info->sg = job->reply_payload.sg_list;
1751
1752 if (!lport->tt.exch_seq_send(lport, fp, fc_lport_bsg_resp,
1753 NULL, info, tov))
1754 return -ECOMM;
1755 return 0;
1756}
1757
1758/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001759 * fc_lport_ct_request() - Send CT Passthrough request
1760 * @job: The BSG Passthrough job
Steve Maa51ab392009-11-03 11:47:34 -08001761 * @lport: The local port sending the request
1762 * @did: The destination FC-ID
Robert Love3a3b42b2009-11-03 11:47:39 -08001763 * @tov: The timeout period to wait for the response
Steve Maa51ab392009-11-03 11:47:34 -08001764 *
1765 * Locking Note: The lport lock is expected to be held before calling
1766 * this routine.
1767 */
1768static int fc_lport_ct_request(struct fc_bsg_job *job,
1769 struct fc_lport *lport, u32 did, u32 tov)
1770{
1771 struct fc_bsg_info *info;
1772 struct fc_frame *fp;
1773 struct fc_frame_header *fh;
1774 struct fc_ct_req *ct;
1775 size_t len;
1776
1777 fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
1778 job->request_payload.payload_len);
1779 if (!fp)
1780 return -ENOMEM;
1781
1782 len = job->request_payload.payload_len;
1783 ct = fc_frame_payload_get(fp, len);
1784
1785 sg_copy_to_buffer(job->request_payload.sg_list,
1786 job->request_payload.sg_cnt,
1787 ct, len);
1788
1789 fh = fc_frame_header_get(fp);
1790 fh->fh_r_ctl = FC_RCTL_DD_UNSOL_CTL;
1791 hton24(fh->fh_d_id, did);
Robert Love7b2787e2010-05-07 15:18:41 -07001792 hton24(fh->fh_s_id, lport->port_id);
Steve Maa51ab392009-11-03 11:47:34 -08001793 fh->fh_type = FC_TYPE_CT;
1794 hton24(fh->fh_f_ctl, FC_FC_FIRST_SEQ |
1795 FC_FC_END_SEQ | FC_FC_SEQ_INIT);
1796 fh->fh_cs_ctl = 0;
1797 fh->fh_df_ctl = 0;
1798 fh->fh_parm_offset = 0;
1799
1800 info = kzalloc(sizeof(struct fc_bsg_info), GFP_KERNEL);
1801 if (!info) {
1802 fc_frame_free(fp);
1803 return -ENOMEM;
1804 }
1805
1806 info->job = job;
1807 info->lport = lport;
1808 info->rsp_code = FC_FS_ACC;
1809 info->nents = job->reply_payload.sg_cnt;
1810 info->sg = job->reply_payload.sg_list;
1811
1812 if (!lport->tt.exch_seq_send(lport, fp, fc_lport_bsg_resp,
1813 NULL, info, tov))
1814 return -ECOMM;
1815 return 0;
1816}
1817
1818/**
1819 * fc_lport_bsg_request() - The common entry point for sending
Robert Love3a3b42b2009-11-03 11:47:39 -08001820 * FC Passthrough requests
1821 * @job: The BSG passthrough job
Steve Maa51ab392009-11-03 11:47:34 -08001822 */
1823int fc_lport_bsg_request(struct fc_bsg_job *job)
1824{
1825 struct request *rsp = job->req->next_rq;
1826 struct Scsi_Host *shost = job->shost;
1827 struct fc_lport *lport = shost_priv(shost);
1828 struct fc_rport *rport;
1829 struct fc_rport_priv *rdata;
1830 int rc = -EINVAL;
1831 u32 did;
1832
1833 job->reply->reply_payload_rcv_len = 0;
Hugh Daschbachb248df32010-01-21 10:15:55 -08001834 if (rsp)
1835 rsp->resid_len = job->reply_payload.payload_len;
Steve Maa51ab392009-11-03 11:47:34 -08001836
1837 mutex_lock(&lport->lp_mutex);
1838
1839 switch (job->request->msgcode) {
1840 case FC_BSG_RPT_ELS:
1841 rport = job->rport;
1842 if (!rport)
1843 break;
1844
1845 rdata = rport->dd_data;
1846 rc = fc_lport_els_request(job, lport, rport->port_id,
1847 rdata->e_d_tov);
1848 break;
1849
1850 case FC_BSG_RPT_CT:
1851 rport = job->rport;
1852 if (!rport)
1853 break;
1854
1855 rdata = rport->dd_data;
1856 rc = fc_lport_ct_request(job, lport, rport->port_id,
1857 rdata->e_d_tov);
1858 break;
1859
1860 case FC_BSG_HST_CT:
1861 did = ntoh24(job->request->rqst_data.h_ct.port_id);
1862 if (did == FC_FID_DIR_SERV)
Robert Love3a3b42b2009-11-03 11:47:39 -08001863 rdata = lport->dns_rdata;
Steve Maa51ab392009-11-03 11:47:34 -08001864 else
1865 rdata = lport->tt.rport_lookup(lport, did);
1866
1867 if (!rdata)
1868 break;
1869
1870 rc = fc_lport_ct_request(job, lport, did, rdata->e_d_tov);
1871 break;
1872
1873 case FC_BSG_HST_ELS_NOLOGIN:
1874 did = ntoh24(job->request->rqst_data.h_els.port_id);
1875 rc = fc_lport_els_request(job, lport, did, lport->e_d_tov);
1876 break;
1877 }
1878
1879 mutex_unlock(&lport->lp_mutex);
1880 return rc;
1881}
1882EXPORT_SYMBOL(fc_lport_bsg_request);