blob: 123493166824fb4f24b4b7070a93572263d5ea1e [file] [log] [blame]
Robert Love42e9a922008-12-09 15:10:17 -08001/*
2 * Copyright(c) 2007 - 2008 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 * RPORT GENERAL INFO
22 *
23 * This file contains all processing regarding fc_rports. It contains the
24 * rport state machine and does all rport interaction with the transport class.
25 * There should be no other places in libfc that interact directly with the
26 * transport class in regards to adding and deleting rports.
27 *
28 * fc_rport's represent N_Port's within the fabric.
29 */
30
31/*
32 * RPORT LOCKING
33 *
34 * The rport should never hold the rport mutex and then attempt to acquire
35 * either the lport or disc mutexes. The rport's mutex is considered lesser
36 * than both the lport's mutex and the disc mutex. Refer to fc_lport.c for
37 * more comments on the heirarchy.
38 *
39 * The locking strategy is similar to the lport's strategy. The lock protects
40 * the rport's states and is held and released by the entry points to the rport
41 * block. All _enter_* functions correspond to rport states and expect the rport
42 * mutex to be locked before calling them. This means that rports only handle
43 * one request or response at a time, since they're not critical for the I/O
44 * path this potential over-use of the mutex is acceptable.
45 */
46
47#include <linux/kernel.h>
48#include <linux/spinlock.h>
49#include <linux/interrupt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Robert Love42e9a922008-12-09 15:10:17 -080051#include <linux/rcupdate.h>
52#include <linux/timer.h>
53#include <linux/workqueue.h>
54#include <asm/unaligned.h>
55
56#include <scsi/libfc.h>
57#include <scsi/fc_encode.h>
58
Robert Love8866a5d2009-11-03 11:45:58 -080059#include "fc_libfc.h"
60
Robert Love42e9a922008-12-09 15:10:17 -080061struct workqueue_struct *rport_event_queue;
62
Joe Eykholta7b12a22010-07-20 15:20:08 -070063static void fc_rport_enter_flogi(struct fc_rport_priv *);
Joe Eykholt9fb9d322009-08-25 14:00:50 -070064static void fc_rport_enter_plogi(struct fc_rport_priv *);
65static void fc_rport_enter_prli(struct fc_rport_priv *);
66static void fc_rport_enter_rtv(struct fc_rport_priv *);
67static void fc_rport_enter_ready(struct fc_rport_priv *);
68static void fc_rport_enter_logo(struct fc_rport_priv *);
Joe Eykholt370c3bd2009-08-25 14:03:47 -070069static void fc_rport_enter_adisc(struct fc_rport_priv *);
Robert Love42e9a922008-12-09 15:10:17 -080070
Joe Eykholt3ac6f982009-08-25 14:03:26 -070071static void fc_rport_recv_plogi_req(struct fc_lport *,
Robert Love42e9a922008-12-09 15:10:17 -080072 struct fc_seq *, struct fc_frame *);
Joe Eykholt9fb9d322009-08-25 14:00:50 -070073static void fc_rport_recv_prli_req(struct fc_rport_priv *,
Robert Love42e9a922008-12-09 15:10:17 -080074 struct fc_seq *, struct fc_frame *);
Joe Eykholt9fb9d322009-08-25 14:00:50 -070075static void fc_rport_recv_prlo_req(struct fc_rport_priv *,
Robert Love42e9a922008-12-09 15:10:17 -080076 struct fc_seq *, struct fc_frame *);
Joe Eykholt83fe6a92009-08-25 14:03:31 -070077static void fc_rport_recv_logo_req(struct fc_lport *,
Robert Love42e9a922008-12-09 15:10:17 -080078 struct fc_seq *, struct fc_frame *);
79static void fc_rport_timeout(struct work_struct *);
Joe Eykholt9fb9d322009-08-25 14:00:50 -070080static void fc_rport_error(struct fc_rport_priv *, struct fc_frame *);
81static void fc_rport_error_retry(struct fc_rport_priv *, struct fc_frame *);
Robert Love42e9a922008-12-09 15:10:17 -080082static void fc_rport_work(struct work_struct *);
83
84static const char *fc_rport_state_names[] = {
Robert Love42e9a922008-12-09 15:10:17 -080085 [RPORT_ST_INIT] = "Init",
Joe Eykholta7b12a22010-07-20 15:20:08 -070086 [RPORT_ST_FLOGI] = "FLOGI",
87 [RPORT_ST_PLOGI_WAIT] = "PLOGI_WAIT",
Robert Love42e9a922008-12-09 15:10:17 -080088 [RPORT_ST_PLOGI] = "PLOGI",
89 [RPORT_ST_PRLI] = "PRLI",
90 [RPORT_ST_RTV] = "RTV",
91 [RPORT_ST_READY] = "Ready",
Joe Eykholt370c3bd2009-08-25 14:03:47 -070092 [RPORT_ST_ADISC] = "ADISC",
Joe Eykholt14194052009-07-29 17:04:43 -070093 [RPORT_ST_DELETE] = "Delete",
Robert Love42e9a922008-12-09 15:10:17 -080094};
95
Joe Eykholt9e9d0452009-08-25 14:01:18 -070096/**
Robert Love3a3b42b2009-11-03 11:47:39 -080097 * fc_rport_lookup() - Lookup a remote port by port_id
98 * @lport: The local port to lookup the remote port on
99 * @port_id: The remote port ID to look up
Joe Eykholt42e90412010-07-20 15:19:37 -0700100 *
101 * The caller must hold either disc_mutex or rcu_read_lock().
Joe Eykholt8025b5d2009-08-25 14:02:06 -0700102 */
103static struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport,
104 u32 port_id)
105{
106 struct fc_rport_priv *rdata;
107
Joe Eykholt42e90412010-07-20 15:19:37 -0700108 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers)
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700109 if (rdata->ids.port_id == port_id)
Joe Eykholt8025b5d2009-08-25 14:02:06 -0700110 return rdata;
111 return NULL;
112}
113
114/**
Robert Love9737e6a2009-08-25 14:02:59 -0700115 * fc_rport_create() - Create a new remote port
Robert Love3a3b42b2009-11-03 11:47:39 -0800116 * @lport: The local port this remote port will be associated with
117 * @ids: The identifiers for the new remote port
118 *
119 * The remote port will start in the INIT state.
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700120 *
Joe Eykholt48f00902009-08-25 14:01:50 -0700121 * Locking note: must be called with the disc_mutex held.
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700122 */
123static struct fc_rport_priv *fc_rport_create(struct fc_lport *lport,
Robert Love9737e6a2009-08-25 14:02:59 -0700124 u32 port_id)
Robert Love42e9a922008-12-09 15:10:17 -0800125{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700126 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800127
Robert Love9737e6a2009-08-25 14:02:59 -0700128 rdata = lport->tt.rport_lookup(lport, port_id);
Joe Eykholt19f97e32009-08-25 14:01:55 -0700129 if (rdata)
130 return rdata;
131
Joe Eykholtf90377a2010-07-20 15:19:42 -0700132 rdata = kzalloc(sizeof(*rdata) + lport->rport_priv_size, GFP_KERNEL);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700133 if (!rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800134 return NULL;
135
Robert Love9737e6a2009-08-25 14:02:59 -0700136 rdata->ids.node_name = -1;
137 rdata->ids.port_name = -1;
138 rdata->ids.port_id = port_id;
139 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
140
Joe Eykholtf211fa52009-08-25 14:01:01 -0700141 kref_init(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800142 mutex_init(&rdata->rp_mutex);
Joe Eykholt795d86f2009-08-25 14:00:39 -0700143 rdata->local_port = lport;
Robert Love42e9a922008-12-09 15:10:17 -0800144 rdata->rp_state = RPORT_ST_INIT;
145 rdata->event = RPORT_EV_NONE;
146 rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
Joe Eykholt795d86f2009-08-25 14:00:39 -0700147 rdata->e_d_tov = lport->e_d_tov;
148 rdata->r_a_tov = lport->r_a_tov;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700149 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
Robert Love42e9a922008-12-09 15:10:17 -0800150 INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
151 INIT_WORK(&rdata->event_work, fc_rport_work);
Robert Love9737e6a2009-08-25 14:02:59 -0700152 if (port_id != FC_FID_DIR_SERV)
Joe Eykholt42e90412010-07-20 15:19:37 -0700153 list_add_rcu(&rdata->peers, &lport->disc.rports);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700154 return rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800155}
156
157/**
Joe Eykholt42e90412010-07-20 15:19:37 -0700158 * fc_rport_free_rcu() - Free a remote port
159 * @rcu: The rcu_head structure inside the remote port
160 */
161static void fc_rport_free_rcu(struct rcu_head *rcu)
162{
163 struct fc_rport_priv *rdata;
164
165 rdata = container_of(rcu, struct fc_rport_priv, rcu);
166 kfree(rdata);
167}
168
169/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800170 * fc_rport_destroy() - Free a remote port after last reference is released
171 * @kref: The remote port's kref
Joe Eykholtf211fa52009-08-25 14:01:01 -0700172 */
173static void fc_rport_destroy(struct kref *kref)
174{
175 struct fc_rport_priv *rdata;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700176
177 rdata = container_of(kref, struct fc_rport_priv, kref);
Joe Eykholt42e90412010-07-20 15:19:37 -0700178 call_rcu(&rdata->rcu, fc_rport_free_rcu);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700179}
180
181/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800182 * fc_rport_state() - Return a string identifying the remote port's state
183 * @rdata: The remote port
Robert Love42e9a922008-12-09 15:10:17 -0800184 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700185static const char *fc_rport_state(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800186{
187 const char *cp;
Robert Love42e9a922008-12-09 15:10:17 -0800188
189 cp = fc_rport_state_names[rdata->rp_state];
190 if (!cp)
191 cp = "Unknown";
192 return cp;
193}
194
195/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800196 * fc_set_rport_loss_tmo() - Set the remote port loss timeout
197 * @rport: The remote port that gets a new timeout value
198 * @timeout: The new timeout value (in seconds)
Robert Love42e9a922008-12-09 15:10:17 -0800199 */
200void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
201{
202 if (timeout)
203 rport->dev_loss_tmo = timeout + 5;
204 else
205 rport->dev_loss_tmo = 30;
206}
207EXPORT_SYMBOL(fc_set_rport_loss_tmo);
208
209/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800210 * fc_plogi_get_maxframe() - Get the maximum payload from the common service
211 * parameters in a FLOGI frame
Joe Eykholta7b12a22010-07-20 15:20:08 -0700212 * @flp: The FLOGI or PLOGI payload
Robert Love3a3b42b2009-11-03 11:47:39 -0800213 * @maxval: The maximum frame size upper limit; this may be less than what
214 * is in the service parameters
Robert Love42e9a922008-12-09 15:10:17 -0800215 */
Robert Loveb2ab99c2009-02-27 10:55:50 -0800216static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
217 unsigned int maxval)
Robert Love42e9a922008-12-09 15:10:17 -0800218{
219 unsigned int mfs;
220
221 /*
222 * Get max payload from the common service parameters and the
223 * class 3 receive data field size.
224 */
225 mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
226 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
227 maxval = mfs;
228 mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
229 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
230 maxval = mfs;
231 return maxval;
232}
233
234/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800235 * fc_rport_state_enter() - Change the state of a remote port
236 * @rdata: The remote port whose state should change
237 * @new: The new state
Robert Love42e9a922008-12-09 15:10:17 -0800238 *
239 * Locking Note: Called with the rport lock held
240 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700241static void fc_rport_state_enter(struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -0800242 enum fc_rport_state new)
243{
Robert Love42e9a922008-12-09 15:10:17 -0800244 if (rdata->rp_state != new)
245 rdata->retries = 0;
246 rdata->rp_state = new;
247}
248
Robert Love3a3b42b2009-11-03 11:47:39 -0800249/**
250 * fc_rport_work() - Handler for remote port events in the rport_event_queue
251 * @work: Handle to the remote port being dequeued
252 */
Robert Love42e9a922008-12-09 15:10:17 -0800253static void fc_rport_work(struct work_struct *work)
254{
Abhijeet Joglekar571f8242009-02-27 10:54:41 -0800255 u32 port_id;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700256 struct fc_rport_priv *rdata =
257 container_of(work, struct fc_rport_priv, event_work);
Robert Love3a3b42b2009-11-03 11:47:39 -0800258 struct fc_rport_libfc_priv *rpriv;
Robert Love42e9a922008-12-09 15:10:17 -0800259 enum fc_rport_event event;
Robert Love42e9a922008-12-09 15:10:17 -0800260 struct fc_lport *lport = rdata->local_port;
261 struct fc_rport_operations *rport_ops;
Joe Eykholt629f4422009-08-25 14:01:06 -0700262 struct fc_rport_identifiers ids;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700263 struct fc_rport *rport;
Robert Love42e9a922008-12-09 15:10:17 -0800264
265 mutex_lock(&rdata->rp_mutex);
266 event = rdata->event;
267 rport_ops = rdata->ops;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700268 rport = rdata->rport;
Robert Love42e9a922008-12-09 15:10:17 -0800269
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700270 FC_RPORT_DBG(rdata, "work event %u\n", event);
271
Joe Eykholt629f4422009-08-25 14:01:06 -0700272 switch (event) {
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700273 case RPORT_EV_READY:
Joe Eykholtf211fa52009-08-25 14:01:01 -0700274 ids = rdata->ids;
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700275 rdata->event = RPORT_EV_NONE;
Joe Eykholtf0342602010-06-11 16:44:57 -0700276 rdata->major_retries = 0;
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700277 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800278 mutex_unlock(&rdata->rp_mutex);
279
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700280 if (!rport)
281 rport = fc_remote_port_add(lport->host, 0, &ids);
282 if (!rport) {
283 FC_RPORT_DBG(rdata, "Failed to add the rport\n");
284 lport->tt.rport_logoff(rdata);
285 kref_put(&rdata->kref, lport->tt.rport_destroy);
286 return;
Robert Love42e9a922008-12-09 15:10:17 -0800287 }
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700288 mutex_lock(&rdata->rp_mutex);
289 if (rdata->rport)
290 FC_RPORT_DBG(rdata, "rport already allocated\n");
291 rdata->rport = rport;
292 rport->maxframe_size = rdata->maxframe_size;
293 rport->supported_classes = rdata->supported_classes;
294
Robert Love3a3b42b2009-11-03 11:47:39 -0800295 rpriv = rport->dd_data;
296 rpriv->local_port = lport;
297 rpriv->rp_state = rdata->rp_state;
298 rpriv->flags = rdata->flags;
299 rpriv->e_d_tov = rdata->e_d_tov;
300 rpriv->r_a_tov = rdata->r_a_tov;
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700301 mutex_unlock(&rdata->rp_mutex);
302
Joe Eykholt83455922009-08-25 14:02:01 -0700303 if (rport_ops && rport_ops->event_callback) {
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700304 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700305 rport_ops->event_callback(lport, rdata, event);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700306 }
307 kref_put(&rdata->kref, lport->tt.rport_destroy);
Joe Eykholt629f4422009-08-25 14:01:06 -0700308 break;
309
310 case RPORT_EV_FAILED:
311 case RPORT_EV_LOGO:
312 case RPORT_EV_STOP:
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700313 port_id = rdata->ids.port_id;
Robert Love42e9a922008-12-09 15:10:17 -0800314 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700315
Joe Eykholt83455922009-08-25 14:02:01 -0700316 if (rport_ops && rport_ops->event_callback) {
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700317 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700318 rport_ops->event_callback(lport, rdata, event);
Abhijeet Joglekar571f8242009-02-27 10:54:41 -0800319 }
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700320 cancel_delayed_work_sync(&rdata->retry_work);
321
322 /*
323 * Reset any outstanding exchanges before freeing rport.
324 */
325 lport->tt.exch_mgr_reset(lport, 0, port_id);
326 lport->tt.exch_mgr_reset(lport, port_id, 0);
327
328 if (rport) {
Robert Love3a3b42b2009-11-03 11:47:39 -0800329 rpriv = rport->dd_data;
330 rpriv->rp_state = RPORT_ST_DELETE;
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700331 mutex_lock(&rdata->rp_mutex);
332 rdata->rport = NULL;
333 mutex_unlock(&rdata->rp_mutex);
334 fc_remote_port_delete(rport);
335 }
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700336
337 mutex_lock(&lport->disc.disc_mutex);
338 mutex_lock(&rdata->rp_mutex);
339 if (rdata->rp_state == RPORT_ST_DELETE) {
340 if (port_id == FC_FID_DIR_SERV) {
341 rdata->event = RPORT_EV_NONE;
342 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf0342602010-06-11 16:44:57 -0700343 } else if ((rdata->flags & FC_RP_STARTED) &&
344 rdata->major_retries <
345 lport->max_rport_retry_count) {
346 rdata->major_retries++;
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700347 rdata->event = RPORT_EV_NONE;
348 FC_RPORT_DBG(rdata, "work restart\n");
Joe Eykholta7b12a22010-07-20 15:20:08 -0700349 fc_rport_enter_flogi(rdata);
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700350 mutex_unlock(&rdata->rp_mutex);
351 } else {
352 FC_RPORT_DBG(rdata, "work delete\n");
Joe Eykholt42e90412010-07-20 15:19:37 -0700353 list_del_rcu(&rdata->peers);
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700354 mutex_unlock(&rdata->rp_mutex);
355 kref_put(&rdata->kref, lport->tt.rport_destroy);
356 }
357 } else {
358 /*
359 * Re-open for events. Reissue READY event if ready.
360 */
361 rdata->event = RPORT_EV_NONE;
362 if (rdata->rp_state == RPORT_ST_READY)
363 fc_rport_enter_ready(rdata);
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700364 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700365 }
366 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt629f4422009-08-25 14:01:06 -0700367 break;
368
369 default:
Robert Love42e9a922008-12-09 15:10:17 -0800370 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt629f4422009-08-25 14:01:06 -0700371 break;
372 }
Robert Love42e9a922008-12-09 15:10:17 -0800373}
374
375/**
Robert Love34f42a02009-02-27 10:55:45 -0800376 * fc_rport_login() - Start the remote port login state machine
Robert Love3a3b42b2009-11-03 11:47:39 -0800377 * @rdata: The remote port to be logged in to
Robert Love42e9a922008-12-09 15:10:17 -0800378 *
379 * Locking Note: Called without the rport lock held. This
380 * function will hold the rport lock, call an _enter_*
381 * function and then unlock the rport.
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700382 *
383 * This indicates the intent to be logged into the remote port.
384 * If it appears we are already logged in, ADISC is used to verify
385 * the setup.
Robert Love42e9a922008-12-09 15:10:17 -0800386 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700387int fc_rport_login(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800388{
Robert Love42e9a922008-12-09 15:10:17 -0800389 mutex_lock(&rdata->rp_mutex);
390
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700391 rdata->flags |= FC_RP_STARTED;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700392 switch (rdata->rp_state) {
393 case RPORT_ST_READY:
394 FC_RPORT_DBG(rdata, "ADISC port\n");
395 fc_rport_enter_adisc(rdata);
396 break;
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700397 case RPORT_ST_DELETE:
398 FC_RPORT_DBG(rdata, "Restart deleted port\n");
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700399 break;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700400 default:
401 FC_RPORT_DBG(rdata, "Login to port\n");
Joe Eykholta7b12a22010-07-20 15:20:08 -0700402 fc_rport_enter_flogi(rdata);
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700403 break;
404 }
Robert Love42e9a922008-12-09 15:10:17 -0800405 mutex_unlock(&rdata->rp_mutex);
406
407 return 0;
408}
409
410/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800411 * fc_rport_enter_delete() - Schedule a remote port to be deleted
412 * @rdata: The remote port to be deleted
413 * @event: The event to report as the reason for deletion
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700414 *
415 * Locking Note: Called with the rport lock held.
416 *
417 * Allow state change into DELETE only once.
418 *
419 * Call queue_work only if there's no event already pending.
420 * Set the new event so that the old pending event will not occur.
421 * Since we have the mutex, even if fc_rport_work() is already started,
422 * it'll see the new event.
423 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700424static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700425 enum fc_rport_event event)
426{
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700427 if (rdata->rp_state == RPORT_ST_DELETE)
428 return;
429
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700430 FC_RPORT_DBG(rdata, "Delete port\n");
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700431
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700432 fc_rport_state_enter(rdata, RPORT_ST_DELETE);
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700433
434 if (rdata->event == RPORT_EV_NONE)
435 queue_work(rport_event_queue, &rdata->event_work);
436 rdata->event = event;
437}
438
439/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800440 * fc_rport_logoff() - Logoff and remove a remote port
441 * @rdata: The remote port to be logged off of
Robert Love42e9a922008-12-09 15:10:17 -0800442 *
443 * Locking Note: Called without the rport lock held. This
444 * function will hold the rport lock, call an _enter_*
445 * function and then unlock the rport.
446 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700447int fc_rport_logoff(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800448{
Robert Love42e9a922008-12-09 15:10:17 -0800449 mutex_lock(&rdata->rp_mutex);
450
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700451 FC_RPORT_DBG(rdata, "Remove port\n");
Robert Love42e9a922008-12-09 15:10:17 -0800452
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700453 rdata->flags &= ~FC_RP_STARTED;
Joe Eykholt14194052009-07-29 17:04:43 -0700454 if (rdata->rp_state == RPORT_ST_DELETE) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700455 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700456 goto out;
457 }
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700458 fc_rport_enter_logo(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800459
460 /*
Joe Eykholt14194052009-07-29 17:04:43 -0700461 * Change the state to Delete so that we discard
Robert Love42e9a922008-12-09 15:10:17 -0800462 * the response.
463 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700464 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700465out:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700466 mutex_unlock(&rdata->rp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800467 return 0;
468}
469
470/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800471 * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state
472 * @rdata: The remote port that is ready
Robert Love42e9a922008-12-09 15:10:17 -0800473 *
474 * Locking Note: The rport lock is expected to be held before calling
475 * this routine.
476 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700477static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800478{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700479 fc_rport_state_enter(rdata, RPORT_ST_READY);
Robert Love42e9a922008-12-09 15:10:17 -0800480
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700481 FC_RPORT_DBG(rdata, "Port is Ready\n");
Robert Love42e9a922008-12-09 15:10:17 -0800482
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700483 if (rdata->event == RPORT_EV_NONE)
484 queue_work(rport_event_queue, &rdata->event_work);
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700485 rdata->event = RPORT_EV_READY;
Robert Love42e9a922008-12-09 15:10:17 -0800486}
487
488/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800489 * fc_rport_timeout() - Handler for the retry_work timer
490 * @work: Handle to the remote port that has timed out
Robert Love42e9a922008-12-09 15:10:17 -0800491 *
492 * Locking Note: Called without the rport lock held. This
493 * function will hold the rport lock, call an _enter_*
494 * function and then unlock the rport.
495 */
496static void fc_rport_timeout(struct work_struct *work)
497{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700498 struct fc_rport_priv *rdata =
499 container_of(work, struct fc_rport_priv, retry_work.work);
Robert Love42e9a922008-12-09 15:10:17 -0800500
501 mutex_lock(&rdata->rp_mutex);
502
503 switch (rdata->rp_state) {
Joe Eykholta7b12a22010-07-20 15:20:08 -0700504 case RPORT_ST_FLOGI:
505 fc_rport_enter_flogi(rdata);
506 break;
Robert Love42e9a922008-12-09 15:10:17 -0800507 case RPORT_ST_PLOGI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700508 fc_rport_enter_plogi(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800509 break;
510 case RPORT_ST_PRLI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700511 fc_rport_enter_prli(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800512 break;
513 case RPORT_ST_RTV:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700514 fc_rport_enter_rtv(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800515 break;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700516 case RPORT_ST_ADISC:
517 fc_rport_enter_adisc(rdata);
518 break;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700519 case RPORT_ST_PLOGI_WAIT:
Robert Love42e9a922008-12-09 15:10:17 -0800520 case RPORT_ST_READY:
521 case RPORT_ST_INIT:
Joe Eykholt14194052009-07-29 17:04:43 -0700522 case RPORT_ST_DELETE:
Robert Love42e9a922008-12-09 15:10:17 -0800523 break;
524 }
525
526 mutex_unlock(&rdata->rp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800527}
528
529/**
Robert Love34f42a02009-02-27 10:55:45 -0800530 * fc_rport_error() - Error handler, called once retries have been exhausted
Robert Love3a3b42b2009-11-03 11:47:39 -0800531 * @rdata: The remote port the error is happened on
532 * @fp: The error code encapsulated in a frame pointer
Robert Love42e9a922008-12-09 15:10:17 -0800533 *
Robert Love42e9a922008-12-09 15:10:17 -0800534 * Locking Note: The rport lock is expected to be held before
535 * calling this routine
536 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700537static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -0800538{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700539 FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
Joe Eykholtcdbe6df2009-08-25 14:01:39 -0700540 IS_ERR(fp) ? -PTR_ERR(fp) : 0,
541 fc_rport_state(rdata), rdata->retries);
Robert Love42e9a922008-12-09 15:10:17 -0800542
Chris Leech6755db12009-02-27 10:55:02 -0800543 switch (rdata->rp_state) {
Joe Eykholta7b12a22010-07-20 15:20:08 -0700544 case RPORT_ST_FLOGI:
Chris Leech6755db12009-02-27 10:55:02 -0800545 case RPORT_ST_PLOGI:
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700546 rdata->flags &= ~FC_RP_STARTED;
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700547 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
Chris Leech6755db12009-02-27 10:55:02 -0800548 break;
549 case RPORT_ST_RTV:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700550 fc_rport_enter_ready(rdata);
Chris Leech6755db12009-02-27 10:55:02 -0800551 break;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700552 case RPORT_ST_PRLI:
553 case RPORT_ST_ADISC:
554 fc_rport_enter_logo(rdata);
555 break;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700556 case RPORT_ST_PLOGI_WAIT:
Joe Eykholt14194052009-07-29 17:04:43 -0700557 case RPORT_ST_DELETE:
Chris Leech6755db12009-02-27 10:55:02 -0800558 case RPORT_ST_READY:
559 case RPORT_ST_INIT:
560 break;
Robert Love42e9a922008-12-09 15:10:17 -0800561 }
562}
563
564/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800565 * fc_rport_error_retry() - Handler for remote port state retries
566 * @rdata: The remote port whose state is to be retried
567 * @fp: The error code encapsulated in a frame pointer
Chris Leech6755db12009-02-27 10:55:02 -0800568 *
569 * If the error was an exchange timeout retry immediately,
570 * otherwise wait for E_D_TOV.
571 *
572 * Locking Note: The rport lock is expected to be held before
573 * calling this routine
574 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700575static void fc_rport_error_retry(struct fc_rport_priv *rdata,
576 struct fc_frame *fp)
Chris Leech6755db12009-02-27 10:55:02 -0800577{
Chris Leech6755db12009-02-27 10:55:02 -0800578 unsigned long delay = FC_DEF_E_D_TOV;
579
580 /* make sure this isn't an FC_EX_CLOSED error, never retry those */
581 if (PTR_ERR(fp) == -FC_EX_CLOSED)
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700582 return fc_rport_error(rdata, fp);
Chris Leech6755db12009-02-27 10:55:02 -0800583
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700584 if (rdata->retries < rdata->local_port->max_rport_retry_count) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700585 FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
586 PTR_ERR(fp), fc_rport_state(rdata));
Chris Leech6755db12009-02-27 10:55:02 -0800587 rdata->retries++;
588 /* no additional delay on exchange timeouts */
589 if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
590 delay = 0;
Chris Leech6755db12009-02-27 10:55:02 -0800591 schedule_delayed_work(&rdata->retry_work, delay);
592 return;
593 }
594
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700595 return fc_rport_error(rdata, fp);
Chris Leech6755db12009-02-27 10:55:02 -0800596}
597
598/**
Joe Eykholta7b12a22010-07-20 15:20:08 -0700599 * fc_rport_login_complete() - Handle parameters and completion of p-mp login.
600 * @rdata: The remote port which we logged into or which logged into us.
601 * @fp: The FLOGI or PLOGI request or response frame
602 *
603 * Returns non-zero error if a problem is detected with the frame.
604 * Does not free the frame.
605 *
606 * This is only used in point-to-multipoint mode for FIP currently.
607 */
608static int fc_rport_login_complete(struct fc_rport_priv *rdata,
609 struct fc_frame *fp)
610{
611 struct fc_lport *lport = rdata->local_port;
612 struct fc_els_flogi *flogi;
613 unsigned int e_d_tov;
614 u16 csp_flags;
615
616 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
617 if (!flogi)
618 return -EINVAL;
619
620 csp_flags = ntohs(flogi->fl_csp.sp_features);
621
622 if (fc_frame_payload_op(fp) == ELS_FLOGI) {
623 if (csp_flags & FC_SP_FT_FPORT) {
624 FC_RPORT_DBG(rdata, "Fabric bit set in FLOGI\n");
625 return -EINVAL;
626 }
627 } else {
628
629 /*
630 * E_D_TOV is not valid on an incoming FLOGI request.
631 */
632 e_d_tov = ntohl(flogi->fl_csp.sp_e_d_tov);
633 if (csp_flags & FC_SP_FT_EDTR)
634 e_d_tov /= 1000000;
635 if (e_d_tov > rdata->e_d_tov)
636 rdata->e_d_tov = e_d_tov;
637 }
638 rdata->maxframe_size = fc_plogi_get_maxframe(flogi, lport->mfs);
639 return 0;
640}
641
642/**
643 * fc_rport_flogi_resp() - Handle response to FLOGI request for p-mp mode
644 * @sp: The sequence that the FLOGI was on
645 * @fp: The FLOGI response frame
646 * @rp_arg: The remote port that received the FLOGI response
647 */
648void fc_rport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
649 void *rp_arg)
650{
651 struct fc_rport_priv *rdata = rp_arg;
652 struct fc_lport *lport = rdata->local_port;
653 struct fc_els_flogi *flogi;
654 unsigned int r_a_tov;
655
656 FC_RPORT_DBG(rdata, "Received a FLOGI %s\n", fc_els_resp_type(fp));
657
658 if (fp == ERR_PTR(-FC_EX_CLOSED))
659 return;
660
661 mutex_lock(&rdata->rp_mutex);
662
663 if (rdata->rp_state != RPORT_ST_FLOGI) {
664 FC_RPORT_DBG(rdata, "Received a FLOGI response, but in state "
665 "%s\n", fc_rport_state(rdata));
666 if (IS_ERR(fp))
667 goto err;
668 goto out;
669 }
670
671 if (IS_ERR(fp)) {
672 fc_rport_error(rdata, fp);
673 goto err;
674 }
675
676 if (fc_frame_payload_op(fp) != ELS_LS_ACC)
677 goto bad;
678 if (fc_rport_login_complete(rdata, fp))
679 goto bad;
680
681 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
682 if (!flogi)
683 goto bad;
684 r_a_tov = ntohl(flogi->fl_csp.sp_r_a_tov);
685 if (r_a_tov > rdata->r_a_tov)
686 rdata->r_a_tov = r_a_tov;
687
688 if (rdata->ids.port_name < lport->wwpn)
689 fc_rport_enter_plogi(rdata);
690 else
691 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
692out:
693 fc_frame_free(fp);
694err:
695 mutex_unlock(&rdata->rp_mutex);
696 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
697 return;
698bad:
699 FC_RPORT_DBG(rdata, "Bad FLOGI response\n");
700 fc_rport_error_retry(rdata, fp);
701 goto out;
702}
703
704/**
705 * fc_rport_enter_flogi() - Send a FLOGI request to the remote port for p-mp
706 * @rdata: The remote port to send a FLOGI to
707 *
708 * Locking Note: The rport lock is expected to be held before calling
709 * this routine.
710 */
711static void fc_rport_enter_flogi(struct fc_rport_priv *rdata)
712{
713 struct fc_lport *lport = rdata->local_port;
714 struct fc_frame *fp;
715
716 if (!lport->point_to_multipoint)
717 return fc_rport_enter_plogi(rdata);
718
719 FC_RPORT_DBG(rdata, "Entered FLOGI state from %s state\n",
720 fc_rport_state(rdata));
721
722 fc_rport_state_enter(rdata, RPORT_ST_FLOGI);
723
724 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
725 if (!fp)
726 return fc_rport_error_retry(rdata, fp);
727
728 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_FLOGI,
729 fc_rport_flogi_resp, rdata,
730 2 * lport->r_a_tov))
731 fc_rport_error_retry(rdata, NULL);
732 else
733 kref_get(&rdata->kref);
734}
735
736/**
737 * fc_rport_recv_flogi_req() - Handle Fabric Login (FLOGI) request in p-mp mode
738 * @lport: The local port that received the PLOGI request
739 * @sp: The sequence that the PLOGI request was on
740 * @rx_fp: The PLOGI request frame
741 */
742static void fc_rport_recv_flogi_req(struct fc_lport *lport,
743 struct fc_seq *sp, struct fc_frame *rx_fp)
744{
745 struct fc_disc *disc;
746 struct fc_els_flogi *flp;
747 struct fc_rport_priv *rdata;
748 struct fc_frame *fp = rx_fp;
749 struct fc_exch *ep;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700750 struct fc_seq_els_data rjt_data;
751 u32 sid, f_ctl;
752
753 rjt_data.fp = NULL;
Joe Eykholt251748a2010-07-20 15:20:56 -0700754 sid = fc_frame_sid(fp);
Joe Eykholta7b12a22010-07-20 15:20:08 -0700755
756 FC_RPORT_ID_DBG(lport, sid, "Received FLOGI request\n");
757
758 disc = &lport->disc;
759 mutex_lock(&disc->disc_mutex);
760
761 if (!lport->point_to_multipoint) {
762 rjt_data.reason = ELS_RJT_UNSUP;
763 rjt_data.explan = ELS_EXPL_NONE;
764 goto reject;
765 }
766
767 flp = fc_frame_payload_get(fp, sizeof(*flp));
768 if (!flp) {
769 rjt_data.reason = ELS_RJT_LOGIC;
770 rjt_data.explan = ELS_EXPL_INV_LEN;
771 goto reject;
772 }
773
774 rdata = lport->tt.rport_lookup(lport, sid);
775 if (!rdata) {
776 rjt_data.reason = ELS_RJT_FIP;
777 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
778 goto reject;
779 }
780 mutex_lock(&rdata->rp_mutex);
781
782 FC_RPORT_DBG(rdata, "Received FLOGI in %s state\n",
783 fc_rport_state(rdata));
784
785 switch (rdata->rp_state) {
786 case RPORT_ST_INIT:
Joe Eykholta7b12a22010-07-20 15:20:08 -0700787 case RPORT_ST_DELETE:
788 mutex_unlock(&rdata->rp_mutex);
789 rjt_data.reason = ELS_RJT_FIP;
790 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
791 goto reject;
792 case RPORT_ST_FLOGI:
793 case RPORT_ST_PLOGI_WAIT:
794 case RPORT_ST_PLOGI:
795 break;
796 case RPORT_ST_PRLI:
797 case RPORT_ST_RTV:
798 case RPORT_ST_READY:
799 case RPORT_ST_ADISC:
800 /*
801 * Set the remote port to be deleted and to then restart.
802 * This queues work to be sure exchanges are reset.
803 */
804 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
805 mutex_unlock(&rdata->rp_mutex);
806 rjt_data.reason = ELS_RJT_BUSY;
807 rjt_data.explan = ELS_EXPL_NONE;
808 goto reject;
809 }
810 if (fc_rport_login_complete(rdata, fp)) {
811 mutex_unlock(&rdata->rp_mutex);
812 rjt_data.reason = ELS_RJT_LOGIC;
813 rjt_data.explan = ELS_EXPL_NONE;
814 goto reject;
815 }
816 fc_frame_free(rx_fp);
817
818 fp = fc_frame_alloc(lport, sizeof(*flp));
819 if (!fp)
820 goto out;
821
822 sp = lport->tt.seq_start_next(sp);
823 fc_flogi_fill(lport, fp);
824 flp = fc_frame_payload_get(fp, sizeof(*flp));
825 flp->fl_cmd = ELS_LS_ACC;
826
827 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
828 ep = fc_seq_exch(sp);
829 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
830 FC_TYPE_ELS, f_ctl, 0);
831 lport->tt.seq_send(lport, sp, fp);
832
833 if (rdata->ids.port_name < lport->wwpn)
834 fc_rport_enter_plogi(rdata);
835 else
836 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
837out:
838 mutex_unlock(&rdata->rp_mutex);
839 mutex_unlock(&disc->disc_mutex);
840 return;
841
842reject:
843 mutex_unlock(&disc->disc_mutex);
844 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
845 fc_frame_free(fp);
846}
847
848/**
849 * fc_rport_plogi_resp() - Handler for ELS PLOGI responses
Robert Love3a3b42b2009-11-03 11:47:39 -0800850 * @sp: The sequence the PLOGI is on
851 * @fp: The PLOGI response frame
852 * @rdata_arg: The remote port that sent the PLOGI response
Robert Love42e9a922008-12-09 15:10:17 -0800853 *
854 * Locking Note: This function will be called without the rport lock
855 * held, but it will lock, call an _enter_* function or fc_rport_error
856 * and then unlock the rport.
857 */
858static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700859 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800860{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700861 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800862 struct fc_lport *lport = rdata->local_port;
Robert Lovea29e7642009-04-21 16:27:41 -0700863 struct fc_els_flogi *plp = NULL;
Robert Love42e9a922008-12-09 15:10:17 -0800864 u16 csp_seq;
865 u16 cssp_seq;
866 u8 op;
867
868 mutex_lock(&rdata->rp_mutex);
869
Joe Eykholtf657d292009-08-25 14:03:21 -0700870 FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800871
872 if (rdata->rp_state != RPORT_ST_PLOGI) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700873 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
874 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700875 if (IS_ERR(fp))
876 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800877 goto out;
878 }
879
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700880 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700881 fc_rport_error_retry(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700882 goto err;
883 }
884
Robert Love42e9a922008-12-09 15:10:17 -0800885 op = fc_frame_payload_op(fp);
886 if (op == ELS_LS_ACC &&
887 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
Joe Eykholtf211fa52009-08-25 14:01:01 -0700888 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
889 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
Robert Love42e9a922008-12-09 15:10:17 -0800890
Joe Eykholta7b12a22010-07-20 15:20:08 -0700891 if (lport->point_to_multipoint)
892 fc_rport_login_complete(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800893 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
894 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
895 if (cssp_seq < csp_seq)
896 csp_seq = cssp_seq;
897 rdata->max_seq = csp_seq;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700898 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
Joe Eykholt3ac6f982009-08-25 14:03:26 -0700899 fc_rport_enter_prli(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800900 } else
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700901 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800902
903out:
904 fc_frame_free(fp);
905err:
906 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700907 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800908}
909
910/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800911 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
912 * @rdata: The remote port to send a PLOGI to
Robert Love42e9a922008-12-09 15:10:17 -0800913 *
914 * Locking Note: The rport lock is expected to be held before calling
915 * this routine.
916 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700917static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800918{
Robert Love42e9a922008-12-09 15:10:17 -0800919 struct fc_lport *lport = rdata->local_port;
920 struct fc_frame *fp;
921
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700922 FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
923 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800924
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700925 fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
Robert Love42e9a922008-12-09 15:10:17 -0800926
Joe Eykholtf211fa52009-08-25 14:01:01 -0700927 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
Robert Love42e9a922008-12-09 15:10:17 -0800928 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
929 if (!fp) {
Joe Eykholta7b12a22010-07-20 15:20:08 -0700930 FC_RPORT_DBG(rdata, "%s frame alloc failed\n", __func__);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700931 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800932 return;
933 }
934 rdata->e_d_tov = lport->e_d_tov;
935
Joe Eykholtf211fa52009-08-25 14:01:01 -0700936 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
Joe Eykholtb94f8952009-11-03 11:50:21 -0800937 fc_rport_plogi_resp, rdata,
938 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -0700939 fc_rport_error_retry(rdata, NULL);
Robert Love42e9a922008-12-09 15:10:17 -0800940 else
Joe Eykholtf211fa52009-08-25 14:01:01 -0700941 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800942}
943
944/**
Robert Love34f42a02009-02-27 10:55:45 -0800945 * fc_rport_prli_resp() - Process Login (PRLI) response handler
Robert Love3a3b42b2009-11-03 11:47:39 -0800946 * @sp: The sequence the PRLI response was on
947 * @fp: The PRLI response frame
948 * @rdata_arg: The remote port that sent the PRLI response
Robert Love42e9a922008-12-09 15:10:17 -0800949 *
950 * Locking Note: This function will be called without the rport lock
951 * held, but it will lock, call an _enter_* function or fc_rport_error
952 * and then unlock the rport.
953 */
954static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700955 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800956{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700957 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800958 struct {
959 struct fc_els_prli prli;
960 struct fc_els_spp spp;
961 } *pp;
962 u32 roles = FC_RPORT_ROLE_UNKNOWN;
963 u32 fcp_parm = 0;
964 u8 op;
Bhanu Prakash Gollapudi618461c2010-06-11 16:43:54 -0700965 u8 resp_code = 0;
Robert Love42e9a922008-12-09 15:10:17 -0800966
967 mutex_lock(&rdata->rp_mutex);
968
Joe Eykholtf657d292009-08-25 14:03:21 -0700969 FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800970
971 if (rdata->rp_state != RPORT_ST_PRLI) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700972 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
973 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700974 if (IS_ERR(fp))
975 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800976 goto out;
977 }
978
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700979 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700980 fc_rport_error_retry(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700981 goto err;
982 }
983
Robert Love6bd054c2009-08-25 14:03:04 -0700984 /* reinitialize remote port roles */
985 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
986
Robert Love42e9a922008-12-09 15:10:17 -0800987 op = fc_frame_payload_op(fp);
988 if (op == ELS_LS_ACC) {
989 pp = fc_frame_payload_get(fp, sizeof(*pp));
Bhanu Prakash Gollapudi618461c2010-06-11 16:43:54 -0700990 if (!pp)
991 goto out;
992
993 resp_code = (pp->spp.spp_flags & FC_SPP_RESP_MASK);
994 FC_RPORT_DBG(rdata, "PRLI spp_flags = 0x%x\n",
995 pp->spp.spp_flags);
996 if (resp_code != FC_SPP_RESP_ACK) {
997 if (resp_code == FC_SPP_RESP_CONF)
998 fc_rport_error(rdata, fp);
999 else
1000 fc_rport_error_retry(rdata, fp);
1001 goto out;
Robert Love42e9a922008-12-09 15:10:17 -08001002 }
Bhanu Prakash Gollapudi618461c2010-06-11 16:43:54 -07001003 if (pp->prli.prli_spp_len < sizeof(pp->spp))
1004 goto out;
1005
1006 fcp_parm = ntohl(pp->spp.spp_params);
1007 if (fcp_parm & FCP_SPPF_RETRY)
1008 rdata->flags |= FC_RP_FLAGS_RETRY;
Robert Love42e9a922008-12-09 15:10:17 -08001009
Joe Eykholtf211fa52009-08-25 14:01:01 -07001010 rdata->supported_classes = FC_COS_CLASS3;
Robert Love42e9a922008-12-09 15:10:17 -08001011 if (fcp_parm & FCP_SPPF_INIT_FCN)
1012 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1013 if (fcp_parm & FCP_SPPF_TARG_FCN)
1014 roles |= FC_RPORT_ROLE_FCP_TARGET;
1015
Joe Eykholtf211fa52009-08-25 14:01:01 -07001016 rdata->ids.roles = roles;
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001017 fc_rport_enter_rtv(rdata);
Robert Love42e9a922008-12-09 15:10:17 -08001018
1019 } else {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001020 FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
Bhanu Prakash Gollapudi292e40b2010-06-11 16:43:49 -07001021 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001022 }
1023
1024out:
1025 fc_frame_free(fp);
1026err:
1027 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -07001028 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -08001029}
1030
1031/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001032 * fc_rport_enter_prli() - Send Process Login (PRLI) request
1033 * @rdata: The remote port to send the PRLI request to
Robert Love42e9a922008-12-09 15:10:17 -08001034 *
1035 * Locking Note: The rport lock is expected to be held before calling
1036 * this routine.
1037 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001038static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -08001039{
Robert Love42e9a922008-12-09 15:10:17 -08001040 struct fc_lport *lport = rdata->local_port;
1041 struct {
1042 struct fc_els_prli prli;
1043 struct fc_els_spp spp;
1044 } *pp;
1045 struct fc_frame *fp;
1046
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001047 /*
1048 * If the rport is one of the well known addresses
1049 * we skip PRLI and RTV and go straight to READY.
1050 */
1051 if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
1052 fc_rport_enter_ready(rdata);
1053 return;
1054 }
1055
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001056 FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
1057 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001058
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001059 fc_rport_state_enter(rdata, RPORT_ST_PRLI);
Robert Love42e9a922008-12-09 15:10:17 -08001060
1061 fp = fc_frame_alloc(lport, sizeof(*pp));
1062 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001063 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001064 return;
1065 }
1066
Joe Eykholtf211fa52009-08-25 14:01:01 -07001067 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PRLI,
Joe Eykholtb94f8952009-11-03 11:50:21 -08001068 fc_rport_prli_resp, rdata,
1069 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001070 fc_rport_error_retry(rdata, NULL);
Robert Love42e9a922008-12-09 15:10:17 -08001071 else
Joe Eykholtf211fa52009-08-25 14:01:01 -07001072 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -08001073}
1074
1075/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001076 * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses
1077 * @sp: The sequence the RTV was on
1078 * @fp: The RTV response frame
1079 * @rdata_arg: The remote port that sent the RTV response
Robert Love42e9a922008-12-09 15:10:17 -08001080 *
1081 * Many targets don't seem to support this.
1082 *
1083 * Locking Note: This function will be called without the rport lock
1084 * held, but it will lock, call an _enter_* function or fc_rport_error
1085 * and then unlock the rport.
1086 */
1087static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001088 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -08001089{
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001090 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -08001091 u8 op;
1092
1093 mutex_lock(&rdata->rp_mutex);
1094
Joe Eykholtf657d292009-08-25 14:03:21 -07001095 FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -08001096
1097 if (rdata->rp_state != RPORT_ST_RTV) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001098 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
1099 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001100 if (IS_ERR(fp))
1101 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001102 goto out;
1103 }
1104
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001105 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001106 fc_rport_error(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001107 goto err;
1108 }
1109
Robert Love42e9a922008-12-09 15:10:17 -08001110 op = fc_frame_payload_op(fp);
1111 if (op == ELS_LS_ACC) {
1112 struct fc_els_rtv_acc *rtv;
1113 u32 toq;
1114 u32 tov;
1115
1116 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
1117 if (rtv) {
1118 toq = ntohl(rtv->rtv_toq);
1119 tov = ntohl(rtv->rtv_r_a_tov);
1120 if (tov == 0)
1121 tov = 1;
1122 rdata->r_a_tov = tov;
1123 tov = ntohl(rtv->rtv_e_d_tov);
1124 if (toq & FC_ELS_RTV_EDRES)
1125 tov /= 1000000;
1126 if (tov == 0)
1127 tov = 1;
1128 rdata->e_d_tov = tov;
1129 }
1130 }
1131
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001132 fc_rport_enter_ready(rdata);
Robert Love42e9a922008-12-09 15:10:17 -08001133
1134out:
1135 fc_frame_free(fp);
1136err:
1137 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -07001138 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -08001139}
1140
1141/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001142 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
1143 * @rdata: The remote port to send the RTV request to
Robert Love42e9a922008-12-09 15:10:17 -08001144 *
1145 * Locking Note: The rport lock is expected to be held before calling
1146 * this routine.
1147 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001148static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -08001149{
1150 struct fc_frame *fp;
Robert Love42e9a922008-12-09 15:10:17 -08001151 struct fc_lport *lport = rdata->local_port;
1152
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001153 FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
1154 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001155
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001156 fc_rport_state_enter(rdata, RPORT_ST_RTV);
Robert Love42e9a922008-12-09 15:10:17 -08001157
1158 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
1159 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001160 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001161 return;
1162 }
1163
Joe Eykholtf211fa52009-08-25 14:01:01 -07001164 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
Joe Eykholtb94f8952009-11-03 11:50:21 -08001165 fc_rport_rtv_resp, rdata,
1166 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001167 fc_rport_error_retry(rdata, NULL);
Robert Love42e9a922008-12-09 15:10:17 -08001168 else
Joe Eykholtf211fa52009-08-25 14:01:01 -07001169 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -08001170}
1171
1172/**
Joe Eykholt079ecd82010-07-20 15:20:51 -07001173 * fc_rport_logo_resp() - Handler for logout (LOGO) responses
1174 * @sp: The sequence the LOGO was on
1175 * @fp: The LOGO response frame
1176 * @lport_arg: The local port
1177 */
1178static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
1179 void *lport_arg)
1180{
1181 struct fc_lport *lport = lport_arg;
1182
1183 FC_RPORT_ID_DBG(lport, fc_seq_exch(sp)->did,
1184 "Received a LOGO %s\n", fc_els_resp_type(fp));
1185 if (IS_ERR(fp))
1186 return;
1187 fc_frame_free(fp);
1188}
1189
1190/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001191 * fc_rport_enter_logo() - Send a logout (LOGO) request
1192 * @rdata: The remote port to send the LOGO request to
Robert Love42e9a922008-12-09 15:10:17 -08001193 *
1194 * Locking Note: The rport lock is expected to be held before calling
1195 * this routine.
1196 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001197static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -08001198{
Robert Love42e9a922008-12-09 15:10:17 -08001199 struct fc_lport *lport = rdata->local_port;
1200 struct fc_frame *fp;
1201
Joe Eykholt079ecd82010-07-20 15:20:51 -07001202 FC_RPORT_DBG(rdata, "Port sending LOGO from %s state\n",
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001203 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001204
Robert Love42e9a922008-12-09 15:10:17 -08001205 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
Joe Eykholt079ecd82010-07-20 15:20:51 -07001206 if (!fp)
Robert Love42e9a922008-12-09 15:10:17 -08001207 return;
Joe Eykholt079ecd82010-07-20 15:20:51 -07001208 (void)lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
1209 fc_rport_logo_resp, lport, 0);
Robert Love42e9a922008-12-09 15:10:17 -08001210}
1211
Robert Love42e9a922008-12-09 15:10:17 -08001212/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001213 * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
1214 * @sp: The sequence the ADISC response was on
1215 * @fp: The ADISC response frame
1216 * @rdata_arg: The remote port that sent the ADISC response
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001217 *
1218 * Locking Note: This function will be called without the rport lock
1219 * held, but it will lock, call an _enter_* function or fc_rport_error
1220 * and then unlock the rport.
1221 */
1222static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
Robert Love3a3b42b2009-11-03 11:47:39 -08001223 void *rdata_arg)
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001224{
1225 struct fc_rport_priv *rdata = rdata_arg;
1226 struct fc_els_adisc *adisc;
1227 u8 op;
1228
1229 mutex_lock(&rdata->rp_mutex);
1230
1231 FC_RPORT_DBG(rdata, "Received a ADISC response\n");
1232
1233 if (rdata->rp_state != RPORT_ST_ADISC) {
1234 FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
1235 fc_rport_state(rdata));
1236 if (IS_ERR(fp))
1237 goto err;
1238 goto out;
1239 }
1240
1241 if (IS_ERR(fp)) {
1242 fc_rport_error(rdata, fp);
1243 goto err;
1244 }
1245
1246 /*
1247 * If address verification failed. Consider us logged out of the rport.
1248 * Since the rport is still in discovery, we want to be
1249 * logged in, so go to PLOGI state. Otherwise, go back to READY.
1250 */
1251 op = fc_frame_payload_op(fp);
1252 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1253 if (op != ELS_LS_ACC || !adisc ||
1254 ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
1255 get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
1256 get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
1257 FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
Joe Eykholta7b12a22010-07-20 15:20:08 -07001258 fc_rport_enter_flogi(rdata);
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001259 } else {
1260 FC_RPORT_DBG(rdata, "ADISC OK\n");
1261 fc_rport_enter_ready(rdata);
1262 }
1263out:
1264 fc_frame_free(fp);
1265err:
1266 mutex_unlock(&rdata->rp_mutex);
1267 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1268}
1269
1270/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001271 * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
1272 * @rdata: The remote port to send the ADISC request to
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001273 *
1274 * Locking Note: The rport lock is expected to be held before calling
1275 * this routine.
1276 */
1277static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
1278{
1279 struct fc_lport *lport = rdata->local_port;
1280 struct fc_frame *fp;
1281
1282 FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
1283 fc_rport_state(rdata));
1284
1285 fc_rport_state_enter(rdata, RPORT_ST_ADISC);
1286
1287 fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
1288 if (!fp) {
1289 fc_rport_error_retry(rdata, fp);
1290 return;
1291 }
1292 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
Joe Eykholtb94f8952009-11-03 11:50:21 -08001293 fc_rport_adisc_resp, rdata,
1294 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001295 fc_rport_error_retry(rdata, NULL);
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001296 else
1297 kref_get(&rdata->kref);
1298}
1299
1300/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001301 * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
1302 * @rdata: The remote port that sent the ADISC request
1303 * @sp: The sequence the ADISC request was on
1304 * @in_fp: The ADISC request frame
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001305 *
1306 * Locking Note: Called with the lport and rport locks held.
1307 */
1308static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
1309 struct fc_seq *sp, struct fc_frame *in_fp)
1310{
1311 struct fc_lport *lport = rdata->local_port;
1312 struct fc_frame *fp;
1313 struct fc_exch *ep = fc_seq_exch(sp);
1314 struct fc_els_adisc *adisc;
1315 struct fc_seq_els_data rjt_data;
1316 u32 f_ctl;
1317
1318 FC_RPORT_DBG(rdata, "Received ADISC request\n");
1319
1320 adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
1321 if (!adisc) {
1322 rjt_data.fp = NULL;
1323 rjt_data.reason = ELS_RJT_PROT;
1324 rjt_data.explan = ELS_EXPL_INV_LEN;
1325 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1326 goto drop;
1327 }
1328
1329 fp = fc_frame_alloc(lport, sizeof(*adisc));
1330 if (!fp)
1331 goto drop;
1332 fc_adisc_fill(lport, fp);
1333 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1334 adisc->adisc_cmd = ELS_LS_ACC;
1335 sp = lport->tt.seq_start_next(sp);
1336 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1337 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1338 FC_TYPE_ELS, f_ctl, 0);
1339 lport->tt.seq_send(lport, sp, fp);
1340drop:
1341 fc_frame_free(in_fp);
1342}
1343
1344/**
Yi Zou63e27fb2009-11-20 14:55:24 -08001345 * fc_rport_recv_rls_req() - Handle received Read Link Status request
1346 * @rdata: The remote port that sent the RLS request
1347 * @sp: The sequence that the RLS was on
1348 * @rx_fp: The PRLI request frame
1349 *
1350 * Locking Note: The rport lock is expected to be held before calling
1351 * this function.
1352 */
1353static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
1354 struct fc_seq *sp, struct fc_frame *rx_fp)
1355
1356{
1357 struct fc_lport *lport = rdata->local_port;
1358 struct fc_frame *fp;
1359 struct fc_exch *ep = fc_seq_exch(sp);
1360 struct fc_els_rls *rls;
1361 struct fc_els_rls_resp *rsp;
1362 struct fc_els_lesb *lesb;
1363 struct fc_seq_els_data rjt_data;
1364 struct fc_host_statistics *hst;
1365 u32 f_ctl;
1366
1367 FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
1368 fc_rport_state(rdata));
1369
1370 rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
1371 if (!rls) {
1372 rjt_data.reason = ELS_RJT_PROT;
1373 rjt_data.explan = ELS_EXPL_INV_LEN;
1374 goto out_rjt;
1375 }
1376
1377 fp = fc_frame_alloc(lport, sizeof(*rsp));
1378 if (!fp) {
1379 rjt_data.reason = ELS_RJT_UNAB;
1380 rjt_data.explan = ELS_EXPL_INSUF_RES;
1381 goto out_rjt;
1382 }
1383
1384 rsp = fc_frame_payload_get(fp, sizeof(*rsp));
1385 memset(rsp, 0, sizeof(*rsp));
1386 rsp->rls_cmd = ELS_LS_ACC;
1387 lesb = &rsp->rls_lesb;
1388 if (lport->tt.get_lesb) {
1389 /* get LESB from LLD if it supports it */
1390 lport->tt.get_lesb(lport, lesb);
1391 } else {
1392 fc_get_host_stats(lport->host);
1393 hst = &lport->host_stats;
1394 lesb->lesb_link_fail = htonl(hst->link_failure_count);
1395 lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
1396 lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
1397 lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
1398 lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
1399 lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
1400 }
1401
1402 sp = lport->tt.seq_start_next(sp);
1403 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
1404 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1405 FC_TYPE_ELS, f_ctl, 0);
1406 lport->tt.seq_send(lport, sp, fp);
1407 goto out;
1408
1409out_rjt:
1410 rjt_data.fp = NULL;
1411 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1412out:
1413 fc_frame_free(rx_fp);
1414}
1415
1416/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001417 * fc_rport_recv_els_req() - Handler for validated ELS requests
1418 * @lport: The local port that received the ELS request
1419 * @sp: The sequence that the ELS request was on
1420 * @fp: The ELS request frame
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001421 *
1422 * Handle incoming ELS requests that require port login.
1423 * The ELS opcode has already been validated by the caller.
Robert Love42e9a922008-12-09 15:10:17 -08001424 *
Joe Eykholt131203a2009-08-25 14:03:10 -07001425 * Locking Note: Called with the lport lock held.
Robert Love42e9a922008-12-09 15:10:17 -08001426 */
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001427static void fc_rport_recv_els_req(struct fc_lport *lport,
1428 struct fc_seq *sp, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -08001429{
Joe Eykholt131203a2009-08-25 14:03:10 -07001430 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -08001431 struct fc_seq_els_data els_data;
Robert Love42e9a922008-12-09 15:10:17 -08001432
Robert Love42e9a922008-12-09 15:10:17 -08001433 els_data.fp = NULL;
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001434 els_data.reason = ELS_RJT_UNAB;
1435 els_data.explan = ELS_EXPL_PLOGI_REQD;
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001436
Joe Eykholt25b37b92009-08-25 14:03:15 -07001437 mutex_lock(&lport->disc.disc_mutex);
Joe Eykholt251748a2010-07-20 15:20:56 -07001438 rdata = lport->tt.rport_lookup(lport, fc_frame_sid(fp));
Joe Eykholt131203a2009-08-25 14:03:10 -07001439 if (!rdata) {
Joe Eykholt25b37b92009-08-25 14:03:15 -07001440 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001441 goto reject;
Joe Eykholt131203a2009-08-25 14:03:10 -07001442 }
1443 mutex_lock(&rdata->rp_mutex);
Joe Eykholt25b37b92009-08-25 14:03:15 -07001444 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt131203a2009-08-25 14:03:10 -07001445
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001446 switch (rdata->rp_state) {
1447 case RPORT_ST_PRLI:
1448 case RPORT_ST_RTV:
1449 case RPORT_ST_READY:
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001450 case RPORT_ST_ADISC:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001451 break;
1452 default:
1453 mutex_unlock(&rdata->rp_mutex);
1454 goto reject;
1455 }
1456
1457 switch (fc_frame_payload_op(fp)) {
Joe Eykholt131203a2009-08-25 14:03:10 -07001458 case ELS_PRLI:
1459 fc_rport_recv_prli_req(rdata, sp, fp);
1460 break;
1461 case ELS_PRLO:
1462 fc_rport_recv_prlo_req(rdata, sp, fp);
1463 break;
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001464 case ELS_ADISC:
1465 fc_rport_recv_adisc_req(rdata, sp, fp);
1466 break;
Joe Eykholt131203a2009-08-25 14:03:10 -07001467 case ELS_RRQ:
1468 els_data.fp = fp;
1469 lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
1470 break;
1471 case ELS_REC:
1472 els_data.fp = fp;
1473 lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
1474 break;
Yi Zou63e27fb2009-11-20 14:55:24 -08001475 case ELS_RLS:
1476 fc_rport_recv_rls_req(rdata, sp, fp);
1477 break;
Joe Eykholt131203a2009-08-25 14:03:10 -07001478 default:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001479 fc_frame_free(fp); /* can't happen */
Joe Eykholt131203a2009-08-25 14:03:10 -07001480 break;
Robert Love42e9a922008-12-09 15:10:17 -08001481 }
1482
1483 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001484 return;
1485
1486reject:
1487 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
1488 fc_frame_free(fp);
1489}
1490
1491/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001492 * fc_rport_recv_req() - Handler for requests
1493 * @sp: The sequence the request was on
1494 * @fp: The request frame
1495 * @lport: The local port that received the request
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001496 *
1497 * Locking Note: Called with the lport lock held.
1498 */
1499void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
1500 struct fc_lport *lport)
1501{
1502 struct fc_seq_els_data els_data;
1503
1504 /*
Joe Eykholta7b12a22010-07-20 15:20:08 -07001505 * Handle FLOGI, PLOGI and LOGO requests separately, since they
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001506 * don't require prior login.
1507 * Check for unsupported opcodes first and reject them.
1508 * For some ops, it would be incorrect to reject with "PLOGI required".
1509 */
1510 switch (fc_frame_payload_op(fp)) {
Joe Eykholta7b12a22010-07-20 15:20:08 -07001511 case ELS_FLOGI:
1512 fc_rport_recv_flogi_req(lport, sp, fp);
1513 break;
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001514 case ELS_PLOGI:
1515 fc_rport_recv_plogi_req(lport, sp, fp);
1516 break;
1517 case ELS_LOGO:
1518 fc_rport_recv_logo_req(lport, sp, fp);
1519 break;
1520 case ELS_PRLI:
1521 case ELS_PRLO:
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001522 case ELS_ADISC:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001523 case ELS_RRQ:
1524 case ELS_REC:
Yi Zou63e27fb2009-11-20 14:55:24 -08001525 case ELS_RLS:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001526 fc_rport_recv_els_req(lport, sp, fp);
1527 break;
1528 default:
1529 fc_frame_free(fp);
1530 els_data.fp = NULL;
1531 els_data.reason = ELS_RJT_UNSUP;
1532 els_data.explan = ELS_EXPL_NONE;
1533 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
1534 break;
1535 }
Robert Love42e9a922008-12-09 15:10:17 -08001536}
1537
1538/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001539 * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
1540 * @lport: The local port that received the PLOGI request
1541 * @sp: The sequence that the PLOGI request was on
1542 * @rx_fp: The PLOGI request frame
Robert Love42e9a922008-12-09 15:10:17 -08001543 *
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001544 * Locking Note: The rport lock is held before calling this function.
Robert Love42e9a922008-12-09 15:10:17 -08001545 */
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001546static void fc_rport_recv_plogi_req(struct fc_lport *lport,
Robert Love42e9a922008-12-09 15:10:17 -08001547 struct fc_seq *sp, struct fc_frame *rx_fp)
1548{
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001549 struct fc_disc *disc;
1550 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -08001551 struct fc_frame *fp = rx_fp;
1552 struct fc_exch *ep;
Robert Love42e9a922008-12-09 15:10:17 -08001553 struct fc_els_flogi *pl;
1554 struct fc_seq_els_data rjt_data;
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001555 u32 sid, f_ctl;
1556
Robert Love42e9a922008-12-09 15:10:17 -08001557 rjt_data.fp = NULL;
Joe Eykholt251748a2010-07-20 15:20:56 -07001558 sid = fc_frame_sid(fp);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001559
1560 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
1561
Robert Love42e9a922008-12-09 15:10:17 -08001562 pl = fc_frame_payload_get(fp, sizeof(*pl));
1563 if (!pl) {
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001564 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
1565 rjt_data.reason = ELS_RJT_PROT;
1566 rjt_data.explan = ELS_EXPL_INV_LEN;
1567 goto reject;
Robert Love42e9a922008-12-09 15:10:17 -08001568 }
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001569
1570 disc = &lport->disc;
1571 mutex_lock(&disc->disc_mutex);
1572 rdata = lport->tt.rport_create(lport, sid);
1573 if (!rdata) {
1574 mutex_unlock(&disc->disc_mutex);
1575 rjt_data.reason = ELS_RJT_UNAB;
1576 rjt_data.explan = ELS_EXPL_INSUF_RES;
1577 goto reject;
1578 }
1579
1580 mutex_lock(&rdata->rp_mutex);
1581 mutex_unlock(&disc->disc_mutex);
1582
1583 rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1584 rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
Robert Love42e9a922008-12-09 15:10:17 -08001585
1586 /*
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001587 * If the rport was just created, possibly due to the incoming PLOGI,
Robert Love42e9a922008-12-09 15:10:17 -08001588 * set the state appropriately and accept the PLOGI.
1589 *
1590 * If we had also sent a PLOGI, and if the received PLOGI is from a
1591 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1592 * "command already in progress".
1593 *
1594 * XXX TBD: If the session was ready before, the PLOGI should result in
1595 * all outstanding exchanges being reset.
1596 */
1597 switch (rdata->rp_state) {
1598 case RPORT_ST_INIT:
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001599 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
Robert Love42e9a922008-12-09 15:10:17 -08001600 break;
Joe Eykholta7b12a22010-07-20 15:20:08 -07001601 case RPORT_ST_PLOGI_WAIT:
1602 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI_WAIT state\n");
1603 break;
Robert Love42e9a922008-12-09 15:10:17 -08001604 case RPORT_ST_PLOGI:
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001605 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1606 if (rdata->ids.port_name < lport->wwpn) {
1607 mutex_unlock(&rdata->rp_mutex);
1608 rjt_data.reason = ELS_RJT_INPROG;
1609 rjt_data.explan = ELS_EXPL_NONE;
1610 goto reject;
1611 }
Robert Love42e9a922008-12-09 15:10:17 -08001612 break;
1613 case RPORT_ST_PRLI:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001614 case RPORT_ST_RTV:
Robert Love42e9a922008-12-09 15:10:17 -08001615 case RPORT_ST_READY:
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001616 case RPORT_ST_ADISC:
1617 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
1618 "- ignored for now\n", rdata->rp_state);
1619 /* XXX TBD - should reset */
Robert Love42e9a922008-12-09 15:10:17 -08001620 break;
Joe Eykholta7b12a22010-07-20 15:20:08 -07001621 case RPORT_ST_FLOGI:
Joe Eykholt14194052009-07-29 17:04:43 -07001622 case RPORT_ST_DELETE:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001623 FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
1624 fc_rport_state(rdata));
1625 mutex_unlock(&rdata->rp_mutex);
1626 rjt_data.reason = ELS_RJT_BUSY;
1627 rjt_data.explan = ELS_EXPL_NONE;
1628 goto reject;
Robert Love42e9a922008-12-09 15:10:17 -08001629 }
1630
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001631 /*
1632 * Get session payload size from incoming PLOGI.
1633 */
1634 rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
1635 fc_frame_free(rx_fp);
Robert Love42e9a922008-12-09 15:10:17 -08001636
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001637 /*
1638 * Send LS_ACC. If this fails, the originator should retry.
1639 */
1640 sp = lport->tt.seq_start_next(sp);
1641 if (!sp)
1642 goto out;
1643 fp = fc_frame_alloc(lport, sizeof(*pl));
1644 if (!fp)
1645 goto out;
Robert Love42e9a922008-12-09 15:10:17 -08001646
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001647 fc_plogi_fill(lport, fp, ELS_LS_ACC);
1648 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1649 ep = fc_seq_exch(sp);
1650 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1651 FC_TYPE_ELS, f_ctl, 0);
1652 lport->tt.seq_send(lport, sp, fp);
1653 fc_rport_enter_prli(rdata);
1654out:
1655 mutex_unlock(&rdata->rp_mutex);
1656 return;
1657
1658reject:
1659 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1660 fc_frame_free(fp);
Robert Love42e9a922008-12-09 15:10:17 -08001661}
1662
1663/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001664 * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
1665 * @rdata: The remote port that sent the PRLI request
1666 * @sp: The sequence that the PRLI was on
1667 * @rx_fp: The PRLI request frame
Robert Love42e9a922008-12-09 15:10:17 -08001668 *
1669 * Locking Note: The rport lock is exected to be held before calling
1670 * this function.
1671 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001672static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -08001673 struct fc_seq *sp, struct fc_frame *rx_fp)
1674{
Robert Love42e9a922008-12-09 15:10:17 -08001675 struct fc_lport *lport = rdata->local_port;
1676 struct fc_exch *ep;
1677 struct fc_frame *fp;
Robert Love42e9a922008-12-09 15:10:17 -08001678 struct {
1679 struct fc_els_prli prli;
1680 struct fc_els_spp spp;
1681 } *pp;
1682 struct fc_els_spp *rspp; /* request service param page */
1683 struct fc_els_spp *spp; /* response spp */
1684 unsigned int len;
1685 unsigned int plen;
Robert Love42e9a922008-12-09 15:10:17 -08001686 enum fc_els_spp_resp resp;
1687 struct fc_seq_els_data rjt_data;
1688 u32 f_ctl;
1689 u32 fcp_parm;
1690 u32 roles = FC_RPORT_ROLE_UNKNOWN;
Robert Love42e9a922008-12-09 15:10:17 -08001691
Joe Eykholta2f6a022010-03-12 16:07:36 -08001692 rjt_data.fp = NULL;
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001693 FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1694 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001695
Joe Eykholt251748a2010-07-20 15:20:56 -07001696 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
Robert Love42e9a922008-12-09 15:10:17 -08001697 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
Joe Eykholta2f6a022010-03-12 16:07:36 -08001698 if (!pp)
1699 goto reject_len;
1700 plen = ntohs(pp->prli.prli_len);
1701 if ((plen % 4) != 0 || plen > len || plen < 16)
1702 goto reject_len;
1703 if (plen < len)
1704 len = plen;
1705 plen = pp->prli.prli_spp_len;
1706 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1707 plen > len || len < sizeof(*pp) || plen < 12)
1708 goto reject_len;
1709 rspp = &pp->spp;
1710
1711 fp = fc_frame_alloc(lport, len);
1712 if (!fp) {
1713 rjt_data.reason = ELS_RJT_UNAB;
1714 rjt_data.explan = ELS_EXPL_INSUF_RES;
1715 goto reject;
Robert Love42e9a922008-12-09 15:10:17 -08001716 }
Joe Eykholta2f6a022010-03-12 16:07:36 -08001717 sp = lport->tt.seq_start_next(sp);
1718 WARN_ON(!sp);
1719 pp = fc_frame_payload_get(fp, len);
1720 WARN_ON(!pp);
1721 memset(pp, 0, len);
1722 pp->prli.prli_cmd = ELS_LS_ACC;
1723 pp->prli.prli_spp_len = plen;
1724 pp->prli.prli_len = htons(len);
1725 len -= sizeof(struct fc_els_prli);
Robert Love42e9a922008-12-09 15:10:17 -08001726
Joe Eykholta2f6a022010-03-12 16:07:36 -08001727 /* reinitialize remote port roles */
1728 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
Robert Love6bd054c2009-08-25 14:03:04 -07001729
Joe Eykholta2f6a022010-03-12 16:07:36 -08001730 /*
1731 * Go through all the service parameter pages and build
1732 * response. If plen indicates longer SPP than standard,
1733 * use that. The entire response has been pre-cleared above.
1734 */
1735 spp = &pp->spp;
1736 while (len >= plen) {
1737 spp->spp_type = rspp->spp_type;
1738 spp->spp_type_ext = rspp->spp_type_ext;
1739 spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
1740 resp = FC_SPP_RESP_ACK;
Robert Love42e9a922008-12-09 15:10:17 -08001741
Joe Eykholta2f6a022010-03-12 16:07:36 -08001742 switch (rspp->spp_type) {
1743 case 0: /* common to all FC-4 types */
Robert Love42e9a922008-12-09 15:10:17 -08001744 break;
Joe Eykholta2f6a022010-03-12 16:07:36 -08001745 case FC_TYPE_FCP:
1746 fcp_parm = ntohl(rspp->spp_params);
1747 if (fcp_parm & FCP_SPPF_RETRY)
1748 rdata->flags |= FC_RP_FLAGS_RETRY;
1749 rdata->supported_classes = FC_COS_CLASS3;
1750 if (fcp_parm & FCP_SPPF_INIT_FCN)
1751 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1752 if (fcp_parm & FCP_SPPF_TARG_FCN)
1753 roles |= FC_RPORT_ROLE_FCP_TARGET;
1754 rdata->ids.roles = roles;
1755
1756 spp->spp_params = htonl(lport->service_params);
Robert Love42e9a922008-12-09 15:10:17 -08001757 break;
1758 default:
Joe Eykholta2f6a022010-03-12 16:07:36 -08001759 resp = FC_SPP_RESP_INVL;
Robert Love42e9a922008-12-09 15:10:17 -08001760 break;
1761 }
Joe Eykholta2f6a022010-03-12 16:07:36 -08001762 spp->spp_flags |= resp;
1763 len -= plen;
1764 rspp = (struct fc_els_spp *)((char *)rspp + plen);
1765 spp = (struct fc_els_spp *)((char *)spp + plen);
Robert Love42e9a922008-12-09 15:10:17 -08001766 }
Joe Eykholta2f6a022010-03-12 16:07:36 -08001767
1768 /*
1769 * Send LS_ACC. If this fails, the originator should retry.
1770 */
1771 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1772 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1773 ep = fc_seq_exch(sp);
1774 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1775 FC_TYPE_ELS, f_ctl, 0);
1776 lport->tt.seq_send(lport, sp, fp);
1777
1778 switch (rdata->rp_state) {
1779 case RPORT_ST_PRLI:
1780 fc_rport_enter_ready(rdata);
1781 break;
1782 default:
1783 break;
1784 }
1785 goto drop;
1786
1787reject_len:
1788 rjt_data.reason = ELS_RJT_PROT;
1789 rjt_data.explan = ELS_EXPL_INV_LEN;
1790reject:
1791 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1792drop:
Robert Love42e9a922008-12-09 15:10:17 -08001793 fc_frame_free(rx_fp);
1794}
1795
1796/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001797 * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
1798 * @rdata: The remote port that sent the PRLO request
1799 * @sp: The sequence that the PRLO was on
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001800 * @rx_fp: The PRLO request frame
Robert Love42e9a922008-12-09 15:10:17 -08001801 *
1802 * Locking Note: The rport lock is exected to be held before calling
1803 * this function.
1804 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001805static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
1806 struct fc_seq *sp,
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001807 struct fc_frame *rx_fp)
Robert Love42e9a922008-12-09 15:10:17 -08001808{
Robert Love42e9a922008-12-09 15:10:17 -08001809 struct fc_lport *lport = rdata->local_port;
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001810 struct fc_exch *ep;
1811 struct fc_frame *fp;
1812 struct {
1813 struct fc_els_prlo prlo;
1814 struct fc_els_spp spp;
1815 } *pp;
1816 struct fc_els_spp *rspp; /* request service param page */
1817 struct fc_els_spp *spp; /* response spp */
1818 unsigned int len;
1819 unsigned int plen;
1820 u32 f_ctl;
Robert Love42e9a922008-12-09 15:10:17 -08001821 struct fc_seq_els_data rjt_data;
1822
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001823 rjt_data.fp = NULL;
Robert Love42e9a922008-12-09 15:10:17 -08001824
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001825 FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
1826 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001827
Joe Eykholt251748a2010-07-20 15:20:56 -07001828 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001829 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1830 if (!pp)
1831 goto reject_len;
1832 plen = ntohs(pp->prlo.prlo_len);
1833 if (plen != 20)
1834 goto reject_len;
1835 if (plen < len)
1836 len = plen;
1837
1838 rspp = &pp->spp;
1839
1840 fp = fc_frame_alloc(lport, len);
1841 if (!fp) {
1842 rjt_data.reason = ELS_RJT_UNAB;
1843 rjt_data.explan = ELS_EXPL_INSUF_RES;
1844 goto reject;
1845 }
1846
1847 sp = lport->tt.seq_start_next(sp);
1848 WARN_ON(!sp);
1849 pp = fc_frame_payload_get(fp, len);
1850 WARN_ON(!pp);
1851 memset(pp, 0, len);
1852 pp->prlo.prlo_cmd = ELS_LS_ACC;
1853 pp->prlo.prlo_obs = 0x10;
1854 pp->prlo.prlo_len = htons(len);
1855 spp = &pp->spp;
1856 spp->spp_type = rspp->spp_type;
1857 spp->spp_type_ext = rspp->spp_type_ext;
1858 spp->spp_flags = FC_SPP_RESP_ACK;
1859
1860 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
1861
1862 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1863 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1864 ep = fc_seq_exch(sp);
1865 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1866 FC_TYPE_ELS, f_ctl, 0);
1867 lport->tt.seq_send(lport, sp, fp);
1868 goto drop;
1869
1870reject_len:
1871 rjt_data.reason = ELS_RJT_PROT;
1872 rjt_data.explan = ELS_EXPL_INV_LEN;
1873reject:
Robert Love42e9a922008-12-09 15:10:17 -08001874 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001875drop:
1876 fc_frame_free(rx_fp);
Robert Love42e9a922008-12-09 15:10:17 -08001877}
1878
1879/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001880 * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
1881 * @lport: The local port that received the LOGO request
1882 * @sp: The sequence that the LOGO request was on
1883 * @fp: The LOGO request frame
Robert Love42e9a922008-12-09 15:10:17 -08001884 *
1885 * Locking Note: The rport lock is exected to be held before calling
1886 * this function.
1887 */
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001888static void fc_rport_recv_logo_req(struct fc_lport *lport,
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001889 struct fc_seq *sp,
Robert Love42e9a922008-12-09 15:10:17 -08001890 struct fc_frame *fp)
1891{
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001892 struct fc_rport_priv *rdata;
1893 u32 sid;
Robert Love42e9a922008-12-09 15:10:17 -08001894
Joe Eykholtfeab4ae2009-08-25 14:03:36 -07001895 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
1896
Joe Eykholt251748a2010-07-20 15:20:56 -07001897 sid = fc_frame_sid(fp);
Robert Love42e9a922008-12-09 15:10:17 -08001898
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001899 mutex_lock(&lport->disc.disc_mutex);
1900 rdata = lport->tt.rport_lookup(lport, sid);
1901 if (rdata) {
1902 mutex_lock(&rdata->rp_mutex);
1903 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
1904 fc_rport_state(rdata));
Joe Eykholtfeab4ae2009-08-25 14:03:36 -07001905
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001906 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001907 mutex_unlock(&rdata->rp_mutex);
1908 } else
1909 FC_RPORT_ID_DBG(lport, sid,
1910 "Received LOGO from non-logged-in port\n");
1911 mutex_unlock(&lport->disc.disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -08001912 fc_frame_free(fp);
1913}
1914
Robert Love3a3b42b2009-11-03 11:47:39 -08001915/**
1916 * fc_rport_flush_queue() - Flush the rport_event_queue
1917 */
Robert Love42e9a922008-12-09 15:10:17 -08001918static void fc_rport_flush_queue(void)
1919{
1920 flush_workqueue(rport_event_queue);
1921}
1922
Robert Love3a3b42b2009-11-03 11:47:39 -08001923/**
1924 * fc_rport_init() - Initialize the remote port layer for a local port
1925 * @lport: The local port to initialize the remote port layer for
1926 */
Robert Love42e9a922008-12-09 15:10:17 -08001927int fc_rport_init(struct fc_lport *lport)
1928{
Joe Eykholt8025b5d2009-08-25 14:02:06 -07001929 if (!lport->tt.rport_lookup)
1930 lport->tt.rport_lookup = fc_rport_lookup;
1931
Robert Love5101ff92009-02-27 10:55:18 -08001932 if (!lport->tt.rport_create)
Joe Eykholt9e9d0452009-08-25 14:01:18 -07001933 lport->tt.rport_create = fc_rport_create;
Robert Love5101ff92009-02-27 10:55:18 -08001934
Robert Love42e9a922008-12-09 15:10:17 -08001935 if (!lport->tt.rport_login)
1936 lport->tt.rport_login = fc_rport_login;
1937
1938 if (!lport->tt.rport_logoff)
1939 lport->tt.rport_logoff = fc_rport_logoff;
1940
1941 if (!lport->tt.rport_recv_req)
1942 lport->tt.rport_recv_req = fc_rport_recv_req;
1943
1944 if (!lport->tt.rport_flush_queue)
1945 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1946
Joe Eykholtf211fa52009-08-25 14:01:01 -07001947 if (!lport->tt.rport_destroy)
1948 lport->tt.rport_destroy = fc_rport_destroy;
1949
Robert Love42e9a922008-12-09 15:10:17 -08001950 return 0;
1951}
1952EXPORT_SYMBOL(fc_rport_init);
1953
Robert Love3a3b42b2009-11-03 11:47:39 -08001954/**
1955 * fc_setup_rport() - Initialize the rport_event_queue
1956 */
1957int fc_setup_rport()
Robert Love42e9a922008-12-09 15:10:17 -08001958{
1959 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
1960 if (!rport_event_queue)
1961 return -ENOMEM;
1962 return 0;
1963}
Robert Love42e9a922008-12-09 15:10:17 -08001964
Robert Love3a3b42b2009-11-03 11:47:39 -08001965/**
1966 * fc_destroy_rport() - Destroy the rport_event_queue
1967 */
1968void fc_destroy_rport()
Robert Love42e9a922008-12-09 15:10:17 -08001969{
1970 destroy_workqueue(rport_event_queue);
1971}
Robert Love42e9a922008-12-09 15:10:17 -08001972
Robert Love3a3b42b2009-11-03 11:47:39 -08001973/**
1974 * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
1975 * @rport: The remote port whose I/O should be terminated
1976 */
Robert Love42e9a922008-12-09 15:10:17 -08001977void fc_rport_terminate_io(struct fc_rport *rport)
1978{
Robert Love3a3b42b2009-11-03 11:47:39 -08001979 struct fc_rport_libfc_priv *rpriv = rport->dd_data;
1980 struct fc_lport *lport = rpriv->local_port;
Robert Love42e9a922008-12-09 15:10:17 -08001981
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -08001982 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
1983 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
Robert Love42e9a922008-12-09 15:10:17 -08001984}
1985EXPORT_SYMBOL(fc_rport_terminate_io);