blob: 6b569732f89295ae7823fcd7adde056a35c81bb0 [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 Eykholt9fb9d322009-08-25 14:00:50 -070063static void fc_rport_enter_plogi(struct fc_rport_priv *);
64static void fc_rport_enter_prli(struct fc_rport_priv *);
65static void fc_rport_enter_rtv(struct fc_rport_priv *);
66static void fc_rport_enter_ready(struct fc_rport_priv *);
67static void fc_rport_enter_logo(struct fc_rport_priv *);
Joe Eykholt370c3bd2009-08-25 14:03:47 -070068static void fc_rport_enter_adisc(struct fc_rport_priv *);
Robert Love42e9a922008-12-09 15:10:17 -080069
Joe Eykholt3ac6f982009-08-25 14:03:26 -070070static void fc_rport_recv_plogi_req(struct fc_lport *,
Robert Love42e9a922008-12-09 15:10:17 -080071 struct fc_seq *, struct fc_frame *);
Joe Eykholt9fb9d322009-08-25 14:00:50 -070072static void fc_rport_recv_prli_req(struct fc_rport_priv *,
Robert Love42e9a922008-12-09 15:10:17 -080073 struct fc_seq *, struct fc_frame *);
Joe Eykholt9fb9d322009-08-25 14:00:50 -070074static void fc_rport_recv_prlo_req(struct fc_rport_priv *,
Robert Love42e9a922008-12-09 15:10:17 -080075 struct fc_seq *, struct fc_frame *);
Joe Eykholt83fe6a92009-08-25 14:03:31 -070076static void fc_rport_recv_logo_req(struct fc_lport *,
Robert Love42e9a922008-12-09 15:10:17 -080077 struct fc_seq *, struct fc_frame *);
78static void fc_rport_timeout(struct work_struct *);
Joe Eykholt9fb9d322009-08-25 14:00:50 -070079static void fc_rport_error(struct fc_rport_priv *, struct fc_frame *);
80static void fc_rport_error_retry(struct fc_rport_priv *, struct fc_frame *);
Robert Love42e9a922008-12-09 15:10:17 -080081static void fc_rport_work(struct work_struct *);
82
83static const char *fc_rport_state_names[] = {
Robert Love42e9a922008-12-09 15:10:17 -080084 [RPORT_ST_INIT] = "Init",
85 [RPORT_ST_PLOGI] = "PLOGI",
86 [RPORT_ST_PRLI] = "PRLI",
87 [RPORT_ST_RTV] = "RTV",
88 [RPORT_ST_READY] = "Ready",
89 [RPORT_ST_LOGO] = "LOGO",
Joe Eykholt370c3bd2009-08-25 14:03:47 -070090 [RPORT_ST_ADISC] = "ADISC",
Joe Eykholt14194052009-07-29 17:04:43 -070091 [RPORT_ST_DELETE] = "Delete",
Robert Love42e9a922008-12-09 15:10:17 -080092};
93
Joe Eykholt9e9d0452009-08-25 14:01:18 -070094/**
Robert Love3a3b42b2009-11-03 11:47:39 -080095 * fc_rport_lookup() - Lookup a remote port by port_id
96 * @lport: The local port to lookup the remote port on
97 * @port_id: The remote port ID to look up
Joe Eykholt42e90412010-07-20 15:19:37 -070098 *
99 * The caller must hold either disc_mutex or rcu_read_lock().
Joe Eykholt8025b5d2009-08-25 14:02:06 -0700100 */
101static struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport,
102 u32 port_id)
103{
104 struct fc_rport_priv *rdata;
105
Joe Eykholt42e90412010-07-20 15:19:37 -0700106 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers)
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700107 if (rdata->ids.port_id == port_id)
Joe Eykholt8025b5d2009-08-25 14:02:06 -0700108 return rdata;
109 return NULL;
110}
111
112/**
Robert Love9737e6a2009-08-25 14:02:59 -0700113 * fc_rport_create() - Create a new remote port
Robert Love3a3b42b2009-11-03 11:47:39 -0800114 * @lport: The local port this remote port will be associated with
115 * @ids: The identifiers for the new remote port
116 *
117 * The remote port will start in the INIT state.
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700118 *
Joe Eykholt48f00902009-08-25 14:01:50 -0700119 * Locking note: must be called with the disc_mutex held.
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700120 */
121static struct fc_rport_priv *fc_rport_create(struct fc_lport *lport,
Robert Love9737e6a2009-08-25 14:02:59 -0700122 u32 port_id)
Robert Love42e9a922008-12-09 15:10:17 -0800123{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700124 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800125
Robert Love9737e6a2009-08-25 14:02:59 -0700126 rdata = lport->tt.rport_lookup(lport, port_id);
Joe Eykholt19f97e32009-08-25 14:01:55 -0700127 if (rdata)
128 return rdata;
129
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700130 rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
131 if (!rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800132 return NULL;
133
Robert Love9737e6a2009-08-25 14:02:59 -0700134 rdata->ids.node_name = -1;
135 rdata->ids.port_name = -1;
136 rdata->ids.port_id = port_id;
137 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
138
Joe Eykholtf211fa52009-08-25 14:01:01 -0700139 kref_init(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800140 mutex_init(&rdata->rp_mutex);
Joe Eykholt795d86f2009-08-25 14:00:39 -0700141 rdata->local_port = lport;
Robert Love42e9a922008-12-09 15:10:17 -0800142 rdata->rp_state = RPORT_ST_INIT;
143 rdata->event = RPORT_EV_NONE;
144 rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
Joe Eykholt795d86f2009-08-25 14:00:39 -0700145 rdata->e_d_tov = lport->e_d_tov;
146 rdata->r_a_tov = lport->r_a_tov;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700147 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
Robert Love42e9a922008-12-09 15:10:17 -0800148 INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
149 INIT_WORK(&rdata->event_work, fc_rport_work);
Robert Love9737e6a2009-08-25 14:02:59 -0700150 if (port_id != FC_FID_DIR_SERV)
Joe Eykholt42e90412010-07-20 15:19:37 -0700151 list_add_rcu(&rdata->peers, &lport->disc.rports);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700152 return rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800153}
154
155/**
Joe Eykholt42e90412010-07-20 15:19:37 -0700156 * fc_rport_free_rcu() - Free a remote port
157 * @rcu: The rcu_head structure inside the remote port
158 */
159static void fc_rport_free_rcu(struct rcu_head *rcu)
160{
161 struct fc_rport_priv *rdata;
162
163 rdata = container_of(rcu, struct fc_rport_priv, rcu);
164 kfree(rdata);
165}
166
167/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800168 * fc_rport_destroy() - Free a remote port after last reference is released
169 * @kref: The remote port's kref
Joe Eykholtf211fa52009-08-25 14:01:01 -0700170 */
171static void fc_rport_destroy(struct kref *kref)
172{
173 struct fc_rport_priv *rdata;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700174
175 rdata = container_of(kref, struct fc_rport_priv, kref);
Joe Eykholt42e90412010-07-20 15:19:37 -0700176 call_rcu(&rdata->rcu, fc_rport_free_rcu);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700177}
178
179/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800180 * fc_rport_state() - Return a string identifying the remote port's state
181 * @rdata: The remote port
Robert Love42e9a922008-12-09 15:10:17 -0800182 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700183static const char *fc_rport_state(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800184{
185 const char *cp;
Robert Love42e9a922008-12-09 15:10:17 -0800186
187 cp = fc_rport_state_names[rdata->rp_state];
188 if (!cp)
189 cp = "Unknown";
190 return cp;
191}
192
193/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800194 * fc_set_rport_loss_tmo() - Set the remote port loss timeout
195 * @rport: The remote port that gets a new timeout value
196 * @timeout: The new timeout value (in seconds)
Robert Love42e9a922008-12-09 15:10:17 -0800197 */
198void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
199{
200 if (timeout)
201 rport->dev_loss_tmo = timeout + 5;
202 else
203 rport->dev_loss_tmo = 30;
204}
205EXPORT_SYMBOL(fc_set_rport_loss_tmo);
206
207/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800208 * fc_plogi_get_maxframe() - Get the maximum payload from the common service
209 * parameters in a FLOGI frame
210 * @flp: The FLOGI payload
211 * @maxval: The maximum frame size upper limit; this may be less than what
212 * is in the service parameters
Robert Love42e9a922008-12-09 15:10:17 -0800213 */
Robert Loveb2ab99c2009-02-27 10:55:50 -0800214static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
215 unsigned int maxval)
Robert Love42e9a922008-12-09 15:10:17 -0800216{
217 unsigned int mfs;
218
219 /*
220 * Get max payload from the common service parameters and the
221 * class 3 receive data field size.
222 */
223 mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
224 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
225 maxval = mfs;
226 mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
227 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
228 maxval = mfs;
229 return maxval;
230}
231
232/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800233 * fc_rport_state_enter() - Change the state of a remote port
234 * @rdata: The remote port whose state should change
235 * @new: The new state
Robert Love42e9a922008-12-09 15:10:17 -0800236 *
237 * Locking Note: Called with the rport lock held
238 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700239static void fc_rport_state_enter(struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -0800240 enum fc_rport_state new)
241{
Robert Love42e9a922008-12-09 15:10:17 -0800242 if (rdata->rp_state != new)
243 rdata->retries = 0;
244 rdata->rp_state = new;
245}
246
Robert Love3a3b42b2009-11-03 11:47:39 -0800247/**
248 * fc_rport_work() - Handler for remote port events in the rport_event_queue
249 * @work: Handle to the remote port being dequeued
250 */
Robert Love42e9a922008-12-09 15:10:17 -0800251static void fc_rport_work(struct work_struct *work)
252{
Abhijeet Joglekar571f8242009-02-27 10:54:41 -0800253 u32 port_id;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700254 struct fc_rport_priv *rdata =
255 container_of(work, struct fc_rport_priv, event_work);
Robert Love3a3b42b2009-11-03 11:47:39 -0800256 struct fc_rport_libfc_priv *rpriv;
Robert Love42e9a922008-12-09 15:10:17 -0800257 enum fc_rport_event event;
Robert Love42e9a922008-12-09 15:10:17 -0800258 struct fc_lport *lport = rdata->local_port;
259 struct fc_rport_operations *rport_ops;
Joe Eykholt629f4422009-08-25 14:01:06 -0700260 struct fc_rport_identifiers ids;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700261 struct fc_rport *rport;
Robert Love42e9a922008-12-09 15:10:17 -0800262
263 mutex_lock(&rdata->rp_mutex);
264 event = rdata->event;
265 rport_ops = rdata->ops;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700266 rport = rdata->rport;
Robert Love42e9a922008-12-09 15:10:17 -0800267
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700268 FC_RPORT_DBG(rdata, "work event %u\n", event);
269
Joe Eykholt629f4422009-08-25 14:01:06 -0700270 switch (event) {
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700271 case RPORT_EV_READY:
Joe Eykholtf211fa52009-08-25 14:01:01 -0700272 ids = rdata->ids;
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700273 rdata->event = RPORT_EV_NONE;
Joe Eykholtf0342602010-06-11 16:44:57 -0700274 rdata->major_retries = 0;
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700275 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800276 mutex_unlock(&rdata->rp_mutex);
277
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700278 if (!rport)
279 rport = fc_remote_port_add(lport->host, 0, &ids);
280 if (!rport) {
281 FC_RPORT_DBG(rdata, "Failed to add the rport\n");
282 lport->tt.rport_logoff(rdata);
283 kref_put(&rdata->kref, lport->tt.rport_destroy);
284 return;
Robert Love42e9a922008-12-09 15:10:17 -0800285 }
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700286 mutex_lock(&rdata->rp_mutex);
287 if (rdata->rport)
288 FC_RPORT_DBG(rdata, "rport already allocated\n");
289 rdata->rport = rport;
290 rport->maxframe_size = rdata->maxframe_size;
291 rport->supported_classes = rdata->supported_classes;
292
Robert Love3a3b42b2009-11-03 11:47:39 -0800293 rpriv = rport->dd_data;
294 rpriv->local_port = lport;
295 rpriv->rp_state = rdata->rp_state;
296 rpriv->flags = rdata->flags;
297 rpriv->e_d_tov = rdata->e_d_tov;
298 rpriv->r_a_tov = rdata->r_a_tov;
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700299 mutex_unlock(&rdata->rp_mutex);
300
Joe Eykholt83455922009-08-25 14:02:01 -0700301 if (rport_ops && rport_ops->event_callback) {
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700302 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700303 rport_ops->event_callback(lport, rdata, event);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700304 }
305 kref_put(&rdata->kref, lport->tt.rport_destroy);
Joe Eykholt629f4422009-08-25 14:01:06 -0700306 break;
307
308 case RPORT_EV_FAILED:
309 case RPORT_EV_LOGO:
310 case RPORT_EV_STOP:
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700311 port_id = rdata->ids.port_id;
Robert Love42e9a922008-12-09 15:10:17 -0800312 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700313
Joe Eykholt83455922009-08-25 14:02:01 -0700314 if (rport_ops && rport_ops->event_callback) {
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700315 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700316 rport_ops->event_callback(lport, rdata, event);
Abhijeet Joglekar571f8242009-02-27 10:54:41 -0800317 }
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700318 cancel_delayed_work_sync(&rdata->retry_work);
319
320 /*
321 * Reset any outstanding exchanges before freeing rport.
322 */
323 lport->tt.exch_mgr_reset(lport, 0, port_id);
324 lport->tt.exch_mgr_reset(lport, port_id, 0);
325
326 if (rport) {
Robert Love3a3b42b2009-11-03 11:47:39 -0800327 rpriv = rport->dd_data;
328 rpriv->rp_state = RPORT_ST_DELETE;
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700329 mutex_lock(&rdata->rp_mutex);
330 rdata->rport = NULL;
331 mutex_unlock(&rdata->rp_mutex);
332 fc_remote_port_delete(rport);
333 }
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700334
335 mutex_lock(&lport->disc.disc_mutex);
336 mutex_lock(&rdata->rp_mutex);
337 if (rdata->rp_state == RPORT_ST_DELETE) {
338 if (port_id == FC_FID_DIR_SERV) {
339 rdata->event = RPORT_EV_NONE;
340 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf0342602010-06-11 16:44:57 -0700341 } else if ((rdata->flags & FC_RP_STARTED) &&
342 rdata->major_retries <
343 lport->max_rport_retry_count) {
344 rdata->major_retries++;
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700345 rdata->event = RPORT_EV_NONE;
346 FC_RPORT_DBG(rdata, "work restart\n");
347 fc_rport_enter_plogi(rdata);
348 mutex_unlock(&rdata->rp_mutex);
349 } else {
350 FC_RPORT_DBG(rdata, "work delete\n");
Joe Eykholt42e90412010-07-20 15:19:37 -0700351 list_del_rcu(&rdata->peers);
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700352 mutex_unlock(&rdata->rp_mutex);
353 kref_put(&rdata->kref, lport->tt.rport_destroy);
354 }
355 } else {
356 /*
357 * Re-open for events. Reissue READY event if ready.
358 */
359 rdata->event = RPORT_EV_NONE;
360 if (rdata->rp_state == RPORT_ST_READY)
361 fc_rport_enter_ready(rdata);
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700362 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700363 }
364 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt629f4422009-08-25 14:01:06 -0700365 break;
366
367 default:
Robert Love42e9a922008-12-09 15:10:17 -0800368 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt629f4422009-08-25 14:01:06 -0700369 break;
370 }
Robert Love42e9a922008-12-09 15:10:17 -0800371}
372
373/**
Robert Love34f42a02009-02-27 10:55:45 -0800374 * fc_rport_login() - Start the remote port login state machine
Robert Love3a3b42b2009-11-03 11:47:39 -0800375 * @rdata: The remote port to be logged in to
Robert Love42e9a922008-12-09 15:10:17 -0800376 *
377 * Locking Note: Called without the rport lock held. This
378 * function will hold the rport lock, call an _enter_*
379 * function and then unlock the rport.
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700380 *
381 * This indicates the intent to be logged into the remote port.
382 * If it appears we are already logged in, ADISC is used to verify
383 * the setup.
Robert Love42e9a922008-12-09 15:10:17 -0800384 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700385int fc_rport_login(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800386{
Robert Love42e9a922008-12-09 15:10:17 -0800387 mutex_lock(&rdata->rp_mutex);
388
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700389 rdata->flags |= FC_RP_STARTED;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700390 switch (rdata->rp_state) {
391 case RPORT_ST_READY:
392 FC_RPORT_DBG(rdata, "ADISC port\n");
393 fc_rport_enter_adisc(rdata);
394 break;
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700395 case RPORT_ST_DELETE:
396 FC_RPORT_DBG(rdata, "Restart deleted port\n");
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700397 break;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700398 default:
399 FC_RPORT_DBG(rdata, "Login to port\n");
400 fc_rport_enter_plogi(rdata);
401 break;
402 }
Robert Love42e9a922008-12-09 15:10:17 -0800403 mutex_unlock(&rdata->rp_mutex);
404
405 return 0;
406}
407
408/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800409 * fc_rport_enter_delete() - Schedule a remote port to be deleted
410 * @rdata: The remote port to be deleted
411 * @event: The event to report as the reason for deletion
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700412 *
413 * Locking Note: Called with the rport lock held.
414 *
415 * Allow state change into DELETE only once.
416 *
417 * Call queue_work only if there's no event already pending.
418 * Set the new event so that the old pending event will not occur.
419 * Since we have the mutex, even if fc_rport_work() is already started,
420 * it'll see the new event.
421 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700422static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700423 enum fc_rport_event event)
424{
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700425 if (rdata->rp_state == RPORT_ST_DELETE)
426 return;
427
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700428 FC_RPORT_DBG(rdata, "Delete port\n");
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700429
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700430 fc_rport_state_enter(rdata, RPORT_ST_DELETE);
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700431
432 if (rdata->event == RPORT_EV_NONE)
433 queue_work(rport_event_queue, &rdata->event_work);
434 rdata->event = event;
435}
436
437/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800438 * fc_rport_logoff() - Logoff and remove a remote port
439 * @rdata: The remote port to be logged off of
Robert Love42e9a922008-12-09 15:10:17 -0800440 *
441 * Locking Note: Called without the rport lock held. This
442 * function will hold the rport lock, call an _enter_*
443 * function and then unlock the rport.
444 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700445int fc_rport_logoff(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800446{
Robert Love42e9a922008-12-09 15:10:17 -0800447 mutex_lock(&rdata->rp_mutex);
448
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700449 FC_RPORT_DBG(rdata, "Remove port\n");
Robert Love42e9a922008-12-09 15:10:17 -0800450
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700451 rdata->flags &= ~FC_RP_STARTED;
Joe Eykholt14194052009-07-29 17:04:43 -0700452 if (rdata->rp_state == RPORT_ST_DELETE) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700453 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700454 goto out;
455 }
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700456 fc_rport_enter_logo(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800457
458 /*
Joe Eykholt14194052009-07-29 17:04:43 -0700459 * Change the state to Delete so that we discard
Robert Love42e9a922008-12-09 15:10:17 -0800460 * the response.
461 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700462 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700463out:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700464 mutex_unlock(&rdata->rp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800465 return 0;
466}
467
468/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800469 * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state
470 * @rdata: The remote port that is ready
Robert Love42e9a922008-12-09 15:10:17 -0800471 *
472 * Locking Note: The rport lock is expected to be held before calling
473 * this routine.
474 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700475static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800476{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700477 fc_rport_state_enter(rdata, RPORT_ST_READY);
Robert Love42e9a922008-12-09 15:10:17 -0800478
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700479 FC_RPORT_DBG(rdata, "Port is Ready\n");
Robert Love42e9a922008-12-09 15:10:17 -0800480
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700481 if (rdata->event == RPORT_EV_NONE)
482 queue_work(rport_event_queue, &rdata->event_work);
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700483 rdata->event = RPORT_EV_READY;
Robert Love42e9a922008-12-09 15:10:17 -0800484}
485
486/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800487 * fc_rport_timeout() - Handler for the retry_work timer
488 * @work: Handle to the remote port that has timed out
Robert Love42e9a922008-12-09 15:10:17 -0800489 *
490 * Locking Note: Called without the rport lock held. This
491 * function will hold the rport lock, call an _enter_*
492 * function and then unlock the rport.
493 */
494static void fc_rport_timeout(struct work_struct *work)
495{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700496 struct fc_rport_priv *rdata =
497 container_of(work, struct fc_rport_priv, retry_work.work);
Robert Love42e9a922008-12-09 15:10:17 -0800498
499 mutex_lock(&rdata->rp_mutex);
500
501 switch (rdata->rp_state) {
502 case RPORT_ST_PLOGI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700503 fc_rport_enter_plogi(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800504 break;
505 case RPORT_ST_PRLI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700506 fc_rport_enter_prli(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800507 break;
508 case RPORT_ST_RTV:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700509 fc_rport_enter_rtv(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800510 break;
511 case RPORT_ST_LOGO:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700512 fc_rport_enter_logo(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800513 break;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700514 case RPORT_ST_ADISC:
515 fc_rport_enter_adisc(rdata);
516 break;
Robert Love42e9a922008-12-09 15:10:17 -0800517 case RPORT_ST_READY:
518 case RPORT_ST_INIT:
Joe Eykholt14194052009-07-29 17:04:43 -0700519 case RPORT_ST_DELETE:
Robert Love42e9a922008-12-09 15:10:17 -0800520 break;
521 }
522
523 mutex_unlock(&rdata->rp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800524}
525
526/**
Robert Love34f42a02009-02-27 10:55:45 -0800527 * fc_rport_error() - Error handler, called once retries have been exhausted
Robert Love3a3b42b2009-11-03 11:47:39 -0800528 * @rdata: The remote port the error is happened on
529 * @fp: The error code encapsulated in a frame pointer
Robert Love42e9a922008-12-09 15:10:17 -0800530 *
Robert Love42e9a922008-12-09 15:10:17 -0800531 * Locking Note: The rport lock is expected to be held before
532 * calling this routine
533 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700534static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -0800535{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700536 FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
Joe Eykholtcdbe6df2009-08-25 14:01:39 -0700537 IS_ERR(fp) ? -PTR_ERR(fp) : 0,
538 fc_rport_state(rdata), rdata->retries);
Robert Love42e9a922008-12-09 15:10:17 -0800539
Chris Leech6755db12009-02-27 10:55:02 -0800540 switch (rdata->rp_state) {
541 case RPORT_ST_PLOGI:
Chris Leech6755db12009-02-27 10:55:02 -0800542 case RPORT_ST_LOGO:
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700543 rdata->flags &= ~FC_RP_STARTED;
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700544 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
Chris Leech6755db12009-02-27 10:55:02 -0800545 break;
546 case RPORT_ST_RTV:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700547 fc_rport_enter_ready(rdata);
Chris Leech6755db12009-02-27 10:55:02 -0800548 break;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700549 case RPORT_ST_PRLI:
550 case RPORT_ST_ADISC:
551 fc_rport_enter_logo(rdata);
552 break;
Joe Eykholt14194052009-07-29 17:04:43 -0700553 case RPORT_ST_DELETE:
Chris Leech6755db12009-02-27 10:55:02 -0800554 case RPORT_ST_READY:
555 case RPORT_ST_INIT:
556 break;
Robert Love42e9a922008-12-09 15:10:17 -0800557 }
558}
559
560/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800561 * fc_rport_error_retry() - Handler for remote port state retries
562 * @rdata: The remote port whose state is to be retried
563 * @fp: The error code encapsulated in a frame pointer
Chris Leech6755db12009-02-27 10:55:02 -0800564 *
565 * If the error was an exchange timeout retry immediately,
566 * otherwise wait for E_D_TOV.
567 *
568 * Locking Note: The rport lock is expected to be held before
569 * calling this routine
570 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700571static void fc_rport_error_retry(struct fc_rport_priv *rdata,
572 struct fc_frame *fp)
Chris Leech6755db12009-02-27 10:55:02 -0800573{
Chris Leech6755db12009-02-27 10:55:02 -0800574 unsigned long delay = FC_DEF_E_D_TOV;
575
576 /* make sure this isn't an FC_EX_CLOSED error, never retry those */
577 if (PTR_ERR(fp) == -FC_EX_CLOSED)
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700578 return fc_rport_error(rdata, fp);
Chris Leech6755db12009-02-27 10:55:02 -0800579
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700580 if (rdata->retries < rdata->local_port->max_rport_retry_count) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700581 FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
582 PTR_ERR(fp), fc_rport_state(rdata));
Chris Leech6755db12009-02-27 10:55:02 -0800583 rdata->retries++;
584 /* no additional delay on exchange timeouts */
585 if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
586 delay = 0;
Chris Leech6755db12009-02-27 10:55:02 -0800587 schedule_delayed_work(&rdata->retry_work, delay);
588 return;
589 }
590
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700591 return fc_rport_error(rdata, fp);
Chris Leech6755db12009-02-27 10:55:02 -0800592}
593
594/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800595 * fc_rport_plogi_recv_resp() - Handler for ELS PLOGI responses
596 * @sp: The sequence the PLOGI is on
597 * @fp: The PLOGI response frame
598 * @rdata_arg: The remote port that sent the PLOGI response
Robert Love42e9a922008-12-09 15:10:17 -0800599 *
600 * Locking Note: This function will be called without the rport lock
601 * held, but it will lock, call an _enter_* function or fc_rport_error
602 * and then unlock the rport.
603 */
604static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700605 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800606{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700607 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800608 struct fc_lport *lport = rdata->local_port;
Robert Lovea29e7642009-04-21 16:27:41 -0700609 struct fc_els_flogi *plp = NULL;
Robert Love42e9a922008-12-09 15:10:17 -0800610 unsigned int tov;
611 u16 csp_seq;
612 u16 cssp_seq;
613 u8 op;
614
615 mutex_lock(&rdata->rp_mutex);
616
Joe Eykholtf657d292009-08-25 14:03:21 -0700617 FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800618
619 if (rdata->rp_state != RPORT_ST_PLOGI) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700620 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
621 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700622 if (IS_ERR(fp))
623 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800624 goto out;
625 }
626
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700627 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700628 fc_rport_error_retry(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700629 goto err;
630 }
631
Robert Love42e9a922008-12-09 15:10:17 -0800632 op = fc_frame_payload_op(fp);
633 if (op == ELS_LS_ACC &&
634 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
Joe Eykholtf211fa52009-08-25 14:01:01 -0700635 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
636 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
Robert Love42e9a922008-12-09 15:10:17 -0800637
638 tov = ntohl(plp->fl_csp.sp_e_d_tov);
639 if (ntohs(plp->fl_csp.sp_features) & FC_SP_FT_EDTR)
Hugh Daschbach3b709152010-01-21 10:15:49 -0800640 tov /= 1000000;
Robert Love42e9a922008-12-09 15:10:17 -0800641 if (tov > rdata->e_d_tov)
642 rdata->e_d_tov = tov;
643 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
644 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
645 if (cssp_seq < csp_seq)
646 csp_seq = cssp_seq;
647 rdata->max_seq = csp_seq;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700648 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
Joe Eykholt3ac6f982009-08-25 14:03:26 -0700649 fc_rport_enter_prli(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800650 } else
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700651 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800652
653out:
654 fc_frame_free(fp);
655err:
656 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700657 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800658}
659
660/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800661 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
662 * @rdata: The remote port to send a PLOGI to
Robert Love42e9a922008-12-09 15:10:17 -0800663 *
664 * Locking Note: The rport lock is expected to be held before calling
665 * this routine.
666 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700667static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800668{
Robert Love42e9a922008-12-09 15:10:17 -0800669 struct fc_lport *lport = rdata->local_port;
670 struct fc_frame *fp;
671
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700672 FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
673 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800674
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700675 fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
Robert Love42e9a922008-12-09 15:10:17 -0800676
Joe Eykholtf211fa52009-08-25 14:01:01 -0700677 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
Robert Love42e9a922008-12-09 15:10:17 -0800678 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
679 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700680 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800681 return;
682 }
683 rdata->e_d_tov = lport->e_d_tov;
684
Joe Eykholtf211fa52009-08-25 14:01:01 -0700685 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
Joe Eykholtb94f8952009-11-03 11:50:21 -0800686 fc_rport_plogi_resp, rdata,
687 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -0700688 fc_rport_error_retry(rdata, NULL);
Robert Love42e9a922008-12-09 15:10:17 -0800689 else
Joe Eykholtf211fa52009-08-25 14:01:01 -0700690 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800691}
692
693/**
Robert Love34f42a02009-02-27 10:55:45 -0800694 * fc_rport_prli_resp() - Process Login (PRLI) response handler
Robert Love3a3b42b2009-11-03 11:47:39 -0800695 * @sp: The sequence the PRLI response was on
696 * @fp: The PRLI response frame
697 * @rdata_arg: The remote port that sent the PRLI response
Robert Love42e9a922008-12-09 15:10:17 -0800698 *
699 * Locking Note: This function will be called without the rport lock
700 * held, but it will lock, call an _enter_* function or fc_rport_error
701 * and then unlock the rport.
702 */
703static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700704 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800705{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700706 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800707 struct {
708 struct fc_els_prli prli;
709 struct fc_els_spp spp;
710 } *pp;
711 u32 roles = FC_RPORT_ROLE_UNKNOWN;
712 u32 fcp_parm = 0;
713 u8 op;
Bhanu Prakash Gollapudi618461c2010-06-11 16:43:54 -0700714 u8 resp_code = 0;
Robert Love42e9a922008-12-09 15:10:17 -0800715
716 mutex_lock(&rdata->rp_mutex);
717
Joe Eykholtf657d292009-08-25 14:03:21 -0700718 FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800719
720 if (rdata->rp_state != RPORT_ST_PRLI) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700721 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
722 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700723 if (IS_ERR(fp))
724 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800725 goto out;
726 }
727
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700728 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700729 fc_rport_error_retry(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700730 goto err;
731 }
732
Robert Love6bd054c2009-08-25 14:03:04 -0700733 /* reinitialize remote port roles */
734 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
735
Robert Love42e9a922008-12-09 15:10:17 -0800736 op = fc_frame_payload_op(fp);
737 if (op == ELS_LS_ACC) {
738 pp = fc_frame_payload_get(fp, sizeof(*pp));
Bhanu Prakash Gollapudi618461c2010-06-11 16:43:54 -0700739 if (!pp)
740 goto out;
741
742 resp_code = (pp->spp.spp_flags & FC_SPP_RESP_MASK);
743 FC_RPORT_DBG(rdata, "PRLI spp_flags = 0x%x\n",
744 pp->spp.spp_flags);
745 if (resp_code != FC_SPP_RESP_ACK) {
746 if (resp_code == FC_SPP_RESP_CONF)
747 fc_rport_error(rdata, fp);
748 else
749 fc_rport_error_retry(rdata, fp);
750 goto out;
Robert Love42e9a922008-12-09 15:10:17 -0800751 }
Bhanu Prakash Gollapudi618461c2010-06-11 16:43:54 -0700752 if (pp->prli.prli_spp_len < sizeof(pp->spp))
753 goto out;
754
755 fcp_parm = ntohl(pp->spp.spp_params);
756 if (fcp_parm & FCP_SPPF_RETRY)
757 rdata->flags |= FC_RP_FLAGS_RETRY;
Robert Love42e9a922008-12-09 15:10:17 -0800758
Joe Eykholtf211fa52009-08-25 14:01:01 -0700759 rdata->supported_classes = FC_COS_CLASS3;
Robert Love42e9a922008-12-09 15:10:17 -0800760 if (fcp_parm & FCP_SPPF_INIT_FCN)
761 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
762 if (fcp_parm & FCP_SPPF_TARG_FCN)
763 roles |= FC_RPORT_ROLE_FCP_TARGET;
764
Joe Eykholtf211fa52009-08-25 14:01:01 -0700765 rdata->ids.roles = roles;
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700766 fc_rport_enter_rtv(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800767
768 } else {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700769 FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
Bhanu Prakash Gollapudi292e40b2010-06-11 16:43:49 -0700770 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800771 }
772
773out:
774 fc_frame_free(fp);
775err:
776 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700777 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800778}
779
780/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800781 * fc_rport_logo_resp() - Handler for logout (LOGO) responses
782 * @sp: The sequence the LOGO was on
783 * @fp: The LOGO response frame
784 * @rdata_arg: The remote port that sent the LOGO response
Robert Love42e9a922008-12-09 15:10:17 -0800785 *
786 * Locking Note: This function will be called without the rport lock
787 * held, but it will lock, call an _enter_* function or fc_rport_error
788 * and then unlock the rport.
789 */
790static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700791 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800792{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700793 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800794 u8 op;
795
796 mutex_lock(&rdata->rp_mutex);
797
Joe Eykholtf657d292009-08-25 14:03:21 -0700798 FC_RPORT_DBG(rdata, "Received a LOGO %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800799
Robert Love42e9a922008-12-09 15:10:17 -0800800 if (rdata->rp_state != RPORT_ST_LOGO) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700801 FC_RPORT_DBG(rdata, "Received a LOGO response, but in state "
802 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700803 if (IS_ERR(fp))
804 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800805 goto out;
806 }
807
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700808 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700809 fc_rport_error_retry(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700810 goto err;
811 }
812
Robert Love42e9a922008-12-09 15:10:17 -0800813 op = fc_frame_payload_op(fp);
Joe Eykholt68a17502009-08-25 14:03:42 -0700814 if (op != ELS_LS_ACC)
815 FC_RPORT_DBG(rdata, "Bad ELS response op %x for LOGO command\n",
816 op);
817 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
Robert Love42e9a922008-12-09 15:10:17 -0800818
819out:
820 fc_frame_free(fp);
821err:
822 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700823 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800824}
825
826/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800827 * fc_rport_enter_prli() - Send Process Login (PRLI) request
828 * @rdata: The remote port to send the PRLI request to
Robert Love42e9a922008-12-09 15:10:17 -0800829 *
830 * Locking Note: The rport lock is expected to be held before calling
831 * this routine.
832 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700833static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800834{
Robert Love42e9a922008-12-09 15:10:17 -0800835 struct fc_lport *lport = rdata->local_port;
836 struct {
837 struct fc_els_prli prli;
838 struct fc_els_spp spp;
839 } *pp;
840 struct fc_frame *fp;
841
Joe Eykholt3ac6f982009-08-25 14:03:26 -0700842 /*
843 * If the rport is one of the well known addresses
844 * we skip PRLI and RTV and go straight to READY.
845 */
846 if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
847 fc_rport_enter_ready(rdata);
848 return;
849 }
850
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700851 FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
852 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800853
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700854 fc_rport_state_enter(rdata, RPORT_ST_PRLI);
Robert Love42e9a922008-12-09 15:10:17 -0800855
856 fp = fc_frame_alloc(lport, sizeof(*pp));
857 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700858 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800859 return;
860 }
861
Joe Eykholtf211fa52009-08-25 14:01:01 -0700862 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PRLI,
Joe Eykholtb94f8952009-11-03 11:50:21 -0800863 fc_rport_prli_resp, rdata,
864 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -0700865 fc_rport_error_retry(rdata, NULL);
Robert Love42e9a922008-12-09 15:10:17 -0800866 else
Joe Eykholtf211fa52009-08-25 14:01:01 -0700867 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800868}
869
870/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800871 * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses
872 * @sp: The sequence the RTV was on
873 * @fp: The RTV response frame
874 * @rdata_arg: The remote port that sent the RTV response
Robert Love42e9a922008-12-09 15:10:17 -0800875 *
876 * Many targets don't seem to support this.
877 *
878 * Locking Note: This function will be called without the rport lock
879 * held, but it will lock, call an _enter_* function or fc_rport_error
880 * and then unlock the rport.
881 */
882static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700883 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800884{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700885 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800886 u8 op;
887
888 mutex_lock(&rdata->rp_mutex);
889
Joe Eykholtf657d292009-08-25 14:03:21 -0700890 FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800891
892 if (rdata->rp_state != RPORT_ST_RTV) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700893 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
894 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700895 if (IS_ERR(fp))
896 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800897 goto out;
898 }
899
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700900 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700901 fc_rport_error(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700902 goto err;
903 }
904
Robert Love42e9a922008-12-09 15:10:17 -0800905 op = fc_frame_payload_op(fp);
906 if (op == ELS_LS_ACC) {
907 struct fc_els_rtv_acc *rtv;
908 u32 toq;
909 u32 tov;
910
911 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
912 if (rtv) {
913 toq = ntohl(rtv->rtv_toq);
914 tov = ntohl(rtv->rtv_r_a_tov);
915 if (tov == 0)
916 tov = 1;
917 rdata->r_a_tov = tov;
918 tov = ntohl(rtv->rtv_e_d_tov);
919 if (toq & FC_ELS_RTV_EDRES)
920 tov /= 1000000;
921 if (tov == 0)
922 tov = 1;
923 rdata->e_d_tov = tov;
924 }
925 }
926
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700927 fc_rport_enter_ready(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800928
929out:
930 fc_frame_free(fp);
931err:
932 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700933 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800934}
935
936/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800937 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
938 * @rdata: The remote port to send the RTV request to
Robert Love42e9a922008-12-09 15:10:17 -0800939 *
940 * Locking Note: The rport lock is expected to be held before calling
941 * this routine.
942 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700943static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800944{
945 struct fc_frame *fp;
Robert Love42e9a922008-12-09 15:10:17 -0800946 struct fc_lport *lport = rdata->local_port;
947
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700948 FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
949 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800950
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700951 fc_rport_state_enter(rdata, RPORT_ST_RTV);
Robert Love42e9a922008-12-09 15:10:17 -0800952
953 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
954 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700955 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800956 return;
957 }
958
Joe Eykholtf211fa52009-08-25 14:01:01 -0700959 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
Joe Eykholtb94f8952009-11-03 11:50:21 -0800960 fc_rport_rtv_resp, rdata,
961 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -0700962 fc_rport_error_retry(rdata, NULL);
Robert Love42e9a922008-12-09 15:10:17 -0800963 else
Joe Eykholtf211fa52009-08-25 14:01:01 -0700964 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800965}
966
967/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800968 * fc_rport_enter_logo() - Send a logout (LOGO) request
969 * @rdata: The remote port to send the LOGO request to
Robert Love42e9a922008-12-09 15:10:17 -0800970 *
971 * Locking Note: The rport lock is expected to be held before calling
972 * this routine.
973 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700974static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800975{
Robert Love42e9a922008-12-09 15:10:17 -0800976 struct fc_lport *lport = rdata->local_port;
977 struct fc_frame *fp;
978
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700979 FC_RPORT_DBG(rdata, "Port entered LOGO state from %s state\n",
980 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800981
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700982 fc_rport_state_enter(rdata, RPORT_ST_LOGO);
Robert Love42e9a922008-12-09 15:10:17 -0800983
984 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
985 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700986 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800987 return;
988 }
989
Joe Eykholtf211fa52009-08-25 14:01:01 -0700990 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
Joe Eykholtb94f8952009-11-03 11:50:21 -0800991 fc_rport_logo_resp, rdata,
992 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -0700993 fc_rport_error_retry(rdata, NULL);
Robert Love42e9a922008-12-09 15:10:17 -0800994 else
Joe Eykholtf211fa52009-08-25 14:01:01 -0700995 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800996}
997
Robert Love42e9a922008-12-09 15:10:17 -0800998/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800999 * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
1000 * @sp: The sequence the ADISC response was on
1001 * @fp: The ADISC response frame
1002 * @rdata_arg: The remote port that sent the ADISC response
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001003 *
1004 * Locking Note: This function will be called without the rport lock
1005 * held, but it will lock, call an _enter_* function or fc_rport_error
1006 * and then unlock the rport.
1007 */
1008static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
Robert Love3a3b42b2009-11-03 11:47:39 -08001009 void *rdata_arg)
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001010{
1011 struct fc_rport_priv *rdata = rdata_arg;
1012 struct fc_els_adisc *adisc;
1013 u8 op;
1014
1015 mutex_lock(&rdata->rp_mutex);
1016
1017 FC_RPORT_DBG(rdata, "Received a ADISC response\n");
1018
1019 if (rdata->rp_state != RPORT_ST_ADISC) {
1020 FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
1021 fc_rport_state(rdata));
1022 if (IS_ERR(fp))
1023 goto err;
1024 goto out;
1025 }
1026
1027 if (IS_ERR(fp)) {
1028 fc_rport_error(rdata, fp);
1029 goto err;
1030 }
1031
1032 /*
1033 * If address verification failed. Consider us logged out of the rport.
1034 * Since the rport is still in discovery, we want to be
1035 * logged in, so go to PLOGI state. Otherwise, go back to READY.
1036 */
1037 op = fc_frame_payload_op(fp);
1038 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1039 if (op != ELS_LS_ACC || !adisc ||
1040 ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
1041 get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
1042 get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
1043 FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
1044 fc_rport_enter_plogi(rdata);
1045 } else {
1046 FC_RPORT_DBG(rdata, "ADISC OK\n");
1047 fc_rport_enter_ready(rdata);
1048 }
1049out:
1050 fc_frame_free(fp);
1051err:
1052 mutex_unlock(&rdata->rp_mutex);
1053 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1054}
1055
1056/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001057 * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
1058 * @rdata: The remote port to send the ADISC request to
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001059 *
1060 * Locking Note: The rport lock is expected to be held before calling
1061 * this routine.
1062 */
1063static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
1064{
1065 struct fc_lport *lport = rdata->local_port;
1066 struct fc_frame *fp;
1067
1068 FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
1069 fc_rport_state(rdata));
1070
1071 fc_rport_state_enter(rdata, RPORT_ST_ADISC);
1072
1073 fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
1074 if (!fp) {
1075 fc_rport_error_retry(rdata, fp);
1076 return;
1077 }
1078 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
Joe Eykholtb94f8952009-11-03 11:50:21 -08001079 fc_rport_adisc_resp, rdata,
1080 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001081 fc_rport_error_retry(rdata, NULL);
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001082 else
1083 kref_get(&rdata->kref);
1084}
1085
1086/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001087 * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
1088 * @rdata: The remote port that sent the ADISC request
1089 * @sp: The sequence the ADISC request was on
1090 * @in_fp: The ADISC request frame
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001091 *
1092 * Locking Note: Called with the lport and rport locks held.
1093 */
1094static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
1095 struct fc_seq *sp, struct fc_frame *in_fp)
1096{
1097 struct fc_lport *lport = rdata->local_port;
1098 struct fc_frame *fp;
1099 struct fc_exch *ep = fc_seq_exch(sp);
1100 struct fc_els_adisc *adisc;
1101 struct fc_seq_els_data rjt_data;
1102 u32 f_ctl;
1103
1104 FC_RPORT_DBG(rdata, "Received ADISC request\n");
1105
1106 adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
1107 if (!adisc) {
1108 rjt_data.fp = NULL;
1109 rjt_data.reason = ELS_RJT_PROT;
1110 rjt_data.explan = ELS_EXPL_INV_LEN;
1111 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1112 goto drop;
1113 }
1114
1115 fp = fc_frame_alloc(lport, sizeof(*adisc));
1116 if (!fp)
1117 goto drop;
1118 fc_adisc_fill(lport, fp);
1119 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1120 adisc->adisc_cmd = ELS_LS_ACC;
1121 sp = lport->tt.seq_start_next(sp);
1122 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1123 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1124 FC_TYPE_ELS, f_ctl, 0);
1125 lport->tt.seq_send(lport, sp, fp);
1126drop:
1127 fc_frame_free(in_fp);
1128}
1129
1130/**
Yi Zou63e27fb2009-11-20 14:55:24 -08001131 * fc_rport_recv_rls_req() - Handle received Read Link Status request
1132 * @rdata: The remote port that sent the RLS request
1133 * @sp: The sequence that the RLS was on
1134 * @rx_fp: The PRLI request frame
1135 *
1136 * Locking Note: The rport lock is expected to be held before calling
1137 * this function.
1138 */
1139static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
1140 struct fc_seq *sp, struct fc_frame *rx_fp)
1141
1142{
1143 struct fc_lport *lport = rdata->local_port;
1144 struct fc_frame *fp;
1145 struct fc_exch *ep = fc_seq_exch(sp);
1146 struct fc_els_rls *rls;
1147 struct fc_els_rls_resp *rsp;
1148 struct fc_els_lesb *lesb;
1149 struct fc_seq_els_data rjt_data;
1150 struct fc_host_statistics *hst;
1151 u32 f_ctl;
1152
1153 FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
1154 fc_rport_state(rdata));
1155
1156 rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
1157 if (!rls) {
1158 rjt_data.reason = ELS_RJT_PROT;
1159 rjt_data.explan = ELS_EXPL_INV_LEN;
1160 goto out_rjt;
1161 }
1162
1163 fp = fc_frame_alloc(lport, sizeof(*rsp));
1164 if (!fp) {
1165 rjt_data.reason = ELS_RJT_UNAB;
1166 rjt_data.explan = ELS_EXPL_INSUF_RES;
1167 goto out_rjt;
1168 }
1169
1170 rsp = fc_frame_payload_get(fp, sizeof(*rsp));
1171 memset(rsp, 0, sizeof(*rsp));
1172 rsp->rls_cmd = ELS_LS_ACC;
1173 lesb = &rsp->rls_lesb;
1174 if (lport->tt.get_lesb) {
1175 /* get LESB from LLD if it supports it */
1176 lport->tt.get_lesb(lport, lesb);
1177 } else {
1178 fc_get_host_stats(lport->host);
1179 hst = &lport->host_stats;
1180 lesb->lesb_link_fail = htonl(hst->link_failure_count);
1181 lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
1182 lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
1183 lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
1184 lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
1185 lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
1186 }
1187
1188 sp = lport->tt.seq_start_next(sp);
1189 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
1190 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1191 FC_TYPE_ELS, f_ctl, 0);
1192 lport->tt.seq_send(lport, sp, fp);
1193 goto out;
1194
1195out_rjt:
1196 rjt_data.fp = NULL;
1197 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1198out:
1199 fc_frame_free(rx_fp);
1200}
1201
1202/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001203 * fc_rport_recv_els_req() - Handler for validated ELS requests
1204 * @lport: The local port that received the ELS request
1205 * @sp: The sequence that the ELS request was on
1206 * @fp: The ELS request frame
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001207 *
1208 * Handle incoming ELS requests that require port login.
1209 * The ELS opcode has already been validated by the caller.
Robert Love42e9a922008-12-09 15:10:17 -08001210 *
Joe Eykholt131203a2009-08-25 14:03:10 -07001211 * Locking Note: Called with the lport lock held.
Robert Love42e9a922008-12-09 15:10:17 -08001212 */
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001213static void fc_rport_recv_els_req(struct fc_lport *lport,
1214 struct fc_seq *sp, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -08001215{
Joe Eykholt131203a2009-08-25 14:03:10 -07001216 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -08001217 struct fc_frame_header *fh;
1218 struct fc_seq_els_data els_data;
Robert Love42e9a922008-12-09 15:10:17 -08001219
Robert Love42e9a922008-12-09 15:10:17 -08001220 els_data.fp = NULL;
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001221 els_data.reason = ELS_RJT_UNAB;
1222 els_data.explan = ELS_EXPL_PLOGI_REQD;
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001223
Robert Love42e9a922008-12-09 15:10:17 -08001224 fh = fc_frame_header_get(fp);
1225
Joe Eykholt25b37b92009-08-25 14:03:15 -07001226 mutex_lock(&lport->disc.disc_mutex);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001227 rdata = lport->tt.rport_lookup(lport, ntoh24(fh->fh_s_id));
Joe Eykholt131203a2009-08-25 14:03:10 -07001228 if (!rdata) {
Joe Eykholt25b37b92009-08-25 14:03:15 -07001229 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001230 goto reject;
Joe Eykholt131203a2009-08-25 14:03:10 -07001231 }
1232 mutex_lock(&rdata->rp_mutex);
Joe Eykholt25b37b92009-08-25 14:03:15 -07001233 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt131203a2009-08-25 14:03:10 -07001234
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001235 switch (rdata->rp_state) {
1236 case RPORT_ST_PRLI:
1237 case RPORT_ST_RTV:
1238 case RPORT_ST_READY:
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001239 case RPORT_ST_ADISC:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001240 break;
1241 default:
1242 mutex_unlock(&rdata->rp_mutex);
1243 goto reject;
1244 }
1245
1246 switch (fc_frame_payload_op(fp)) {
Joe Eykholt131203a2009-08-25 14:03:10 -07001247 case ELS_PRLI:
1248 fc_rport_recv_prli_req(rdata, sp, fp);
1249 break;
1250 case ELS_PRLO:
1251 fc_rport_recv_prlo_req(rdata, sp, fp);
1252 break;
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001253 case ELS_ADISC:
1254 fc_rport_recv_adisc_req(rdata, sp, fp);
1255 break;
Joe Eykholt131203a2009-08-25 14:03:10 -07001256 case ELS_RRQ:
1257 els_data.fp = fp;
1258 lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
1259 break;
1260 case ELS_REC:
1261 els_data.fp = fp;
1262 lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
1263 break;
Yi Zou63e27fb2009-11-20 14:55:24 -08001264 case ELS_RLS:
1265 fc_rport_recv_rls_req(rdata, sp, fp);
1266 break;
Joe Eykholt131203a2009-08-25 14:03:10 -07001267 default:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001268 fc_frame_free(fp); /* can't happen */
Joe Eykholt131203a2009-08-25 14:03:10 -07001269 break;
Robert Love42e9a922008-12-09 15:10:17 -08001270 }
1271
1272 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001273 return;
1274
1275reject:
1276 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
1277 fc_frame_free(fp);
1278}
1279
1280/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001281 * fc_rport_recv_req() - Handler for requests
1282 * @sp: The sequence the request was on
1283 * @fp: The request frame
1284 * @lport: The local port that received the request
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001285 *
1286 * Locking Note: Called with the lport lock held.
1287 */
1288void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
1289 struct fc_lport *lport)
1290{
1291 struct fc_seq_els_data els_data;
1292
1293 /*
1294 * Handle PLOGI and LOGO requests separately, since they
1295 * don't require prior login.
1296 * Check for unsupported opcodes first and reject them.
1297 * For some ops, it would be incorrect to reject with "PLOGI required".
1298 */
1299 switch (fc_frame_payload_op(fp)) {
1300 case ELS_PLOGI:
1301 fc_rport_recv_plogi_req(lport, sp, fp);
1302 break;
1303 case ELS_LOGO:
1304 fc_rport_recv_logo_req(lport, sp, fp);
1305 break;
1306 case ELS_PRLI:
1307 case ELS_PRLO:
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001308 case ELS_ADISC:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001309 case ELS_RRQ:
1310 case ELS_REC:
Yi Zou63e27fb2009-11-20 14:55:24 -08001311 case ELS_RLS:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001312 fc_rport_recv_els_req(lport, sp, fp);
1313 break;
1314 default:
1315 fc_frame_free(fp);
1316 els_data.fp = NULL;
1317 els_data.reason = ELS_RJT_UNSUP;
1318 els_data.explan = ELS_EXPL_NONE;
1319 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
1320 break;
1321 }
Robert Love42e9a922008-12-09 15:10:17 -08001322}
1323
1324/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001325 * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
1326 * @lport: The local port that received the PLOGI request
1327 * @sp: The sequence that the PLOGI request was on
1328 * @rx_fp: The PLOGI request frame
Robert Love42e9a922008-12-09 15:10:17 -08001329 *
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001330 * Locking Note: The rport lock is held before calling this function.
Robert Love42e9a922008-12-09 15:10:17 -08001331 */
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001332static void fc_rport_recv_plogi_req(struct fc_lport *lport,
Robert Love42e9a922008-12-09 15:10:17 -08001333 struct fc_seq *sp, struct fc_frame *rx_fp)
1334{
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001335 struct fc_disc *disc;
1336 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -08001337 struct fc_frame *fp = rx_fp;
1338 struct fc_exch *ep;
1339 struct fc_frame_header *fh;
1340 struct fc_els_flogi *pl;
1341 struct fc_seq_els_data rjt_data;
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001342 u32 sid, f_ctl;
1343
Robert Love42e9a922008-12-09 15:10:17 -08001344 rjt_data.fp = NULL;
Robert Love42e9a922008-12-09 15:10:17 -08001345 fh = fc_frame_header_get(fp);
Robert Love42e9a922008-12-09 15:10:17 -08001346 sid = ntoh24(fh->fh_s_id);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001347
1348 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
1349
Robert Love42e9a922008-12-09 15:10:17 -08001350 pl = fc_frame_payload_get(fp, sizeof(*pl));
1351 if (!pl) {
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001352 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
1353 rjt_data.reason = ELS_RJT_PROT;
1354 rjt_data.explan = ELS_EXPL_INV_LEN;
1355 goto reject;
Robert Love42e9a922008-12-09 15:10:17 -08001356 }
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001357
1358 disc = &lport->disc;
1359 mutex_lock(&disc->disc_mutex);
1360 rdata = lport->tt.rport_create(lport, sid);
1361 if (!rdata) {
1362 mutex_unlock(&disc->disc_mutex);
1363 rjt_data.reason = ELS_RJT_UNAB;
1364 rjt_data.explan = ELS_EXPL_INSUF_RES;
1365 goto reject;
1366 }
1367
1368 mutex_lock(&rdata->rp_mutex);
1369 mutex_unlock(&disc->disc_mutex);
1370
1371 rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1372 rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
Robert Love42e9a922008-12-09 15:10:17 -08001373
1374 /*
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001375 * If the rport was just created, possibly due to the incoming PLOGI,
Robert Love42e9a922008-12-09 15:10:17 -08001376 * set the state appropriately and accept the PLOGI.
1377 *
1378 * If we had also sent a PLOGI, and if the received PLOGI is from a
1379 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1380 * "command already in progress".
1381 *
1382 * XXX TBD: If the session was ready before, the PLOGI should result in
1383 * all outstanding exchanges being reset.
1384 */
1385 switch (rdata->rp_state) {
1386 case RPORT_ST_INIT:
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001387 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
Robert Love42e9a922008-12-09 15:10:17 -08001388 break;
1389 case RPORT_ST_PLOGI:
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001390 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1391 if (rdata->ids.port_name < lport->wwpn) {
1392 mutex_unlock(&rdata->rp_mutex);
1393 rjt_data.reason = ELS_RJT_INPROG;
1394 rjt_data.explan = ELS_EXPL_NONE;
1395 goto reject;
1396 }
Robert Love42e9a922008-12-09 15:10:17 -08001397 break;
1398 case RPORT_ST_PRLI:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001399 case RPORT_ST_RTV:
Robert Love42e9a922008-12-09 15:10:17 -08001400 case RPORT_ST_READY:
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001401 case RPORT_ST_ADISC:
1402 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
1403 "- ignored for now\n", rdata->rp_state);
1404 /* XXX TBD - should reset */
Robert Love42e9a922008-12-09 15:10:17 -08001405 break;
Joe Eykholt14194052009-07-29 17:04:43 -07001406 case RPORT_ST_DELETE:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001407 case RPORT_ST_LOGO:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001408 FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
1409 fc_rport_state(rdata));
1410 mutex_unlock(&rdata->rp_mutex);
1411 rjt_data.reason = ELS_RJT_BUSY;
1412 rjt_data.explan = ELS_EXPL_NONE;
1413 goto reject;
Robert Love42e9a922008-12-09 15:10:17 -08001414 }
1415
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001416 /*
1417 * Get session payload size from incoming PLOGI.
1418 */
1419 rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
1420 fc_frame_free(rx_fp);
Robert Love42e9a922008-12-09 15:10:17 -08001421
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001422 /*
1423 * Send LS_ACC. If this fails, the originator should retry.
1424 */
1425 sp = lport->tt.seq_start_next(sp);
1426 if (!sp)
1427 goto out;
1428 fp = fc_frame_alloc(lport, sizeof(*pl));
1429 if (!fp)
1430 goto out;
Robert Love42e9a922008-12-09 15:10:17 -08001431
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001432 fc_plogi_fill(lport, fp, ELS_LS_ACC);
1433 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1434 ep = fc_seq_exch(sp);
1435 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1436 FC_TYPE_ELS, f_ctl, 0);
1437 lport->tt.seq_send(lport, sp, fp);
1438 fc_rport_enter_prli(rdata);
1439out:
1440 mutex_unlock(&rdata->rp_mutex);
1441 return;
1442
1443reject:
1444 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1445 fc_frame_free(fp);
Robert Love42e9a922008-12-09 15:10:17 -08001446}
1447
1448/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001449 * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
1450 * @rdata: The remote port that sent the PRLI request
1451 * @sp: The sequence that the PRLI was on
1452 * @rx_fp: The PRLI request frame
Robert Love42e9a922008-12-09 15:10:17 -08001453 *
1454 * Locking Note: The rport lock is exected to be held before calling
1455 * this function.
1456 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001457static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -08001458 struct fc_seq *sp, struct fc_frame *rx_fp)
1459{
Robert Love42e9a922008-12-09 15:10:17 -08001460 struct fc_lport *lport = rdata->local_port;
1461 struct fc_exch *ep;
1462 struct fc_frame *fp;
1463 struct fc_frame_header *fh;
1464 struct {
1465 struct fc_els_prli prli;
1466 struct fc_els_spp spp;
1467 } *pp;
1468 struct fc_els_spp *rspp; /* request service param page */
1469 struct fc_els_spp *spp; /* response spp */
1470 unsigned int len;
1471 unsigned int plen;
Robert Love42e9a922008-12-09 15:10:17 -08001472 enum fc_els_spp_resp resp;
1473 struct fc_seq_els_data rjt_data;
1474 u32 f_ctl;
1475 u32 fcp_parm;
1476 u32 roles = FC_RPORT_ROLE_UNKNOWN;
Robert Love42e9a922008-12-09 15:10:17 -08001477
Joe Eykholta2f6a022010-03-12 16:07:36 -08001478 rjt_data.fp = NULL;
Robert Love42e9a922008-12-09 15:10:17 -08001479 fh = fc_frame_header_get(rx_fp);
1480
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001481 FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1482 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001483
Robert Love42e9a922008-12-09 15:10:17 -08001484 len = fr_len(rx_fp) - sizeof(*fh);
1485 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
Joe Eykholta2f6a022010-03-12 16:07:36 -08001486 if (!pp)
1487 goto reject_len;
1488 plen = ntohs(pp->prli.prli_len);
1489 if ((plen % 4) != 0 || plen > len || plen < 16)
1490 goto reject_len;
1491 if (plen < len)
1492 len = plen;
1493 plen = pp->prli.prli_spp_len;
1494 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1495 plen > len || len < sizeof(*pp) || plen < 12)
1496 goto reject_len;
1497 rspp = &pp->spp;
1498
1499 fp = fc_frame_alloc(lport, len);
1500 if (!fp) {
1501 rjt_data.reason = ELS_RJT_UNAB;
1502 rjt_data.explan = ELS_EXPL_INSUF_RES;
1503 goto reject;
Robert Love42e9a922008-12-09 15:10:17 -08001504 }
Joe Eykholta2f6a022010-03-12 16:07:36 -08001505 sp = lport->tt.seq_start_next(sp);
1506 WARN_ON(!sp);
1507 pp = fc_frame_payload_get(fp, len);
1508 WARN_ON(!pp);
1509 memset(pp, 0, len);
1510 pp->prli.prli_cmd = ELS_LS_ACC;
1511 pp->prli.prli_spp_len = plen;
1512 pp->prli.prli_len = htons(len);
1513 len -= sizeof(struct fc_els_prli);
Robert Love42e9a922008-12-09 15:10:17 -08001514
Joe Eykholta2f6a022010-03-12 16:07:36 -08001515 /* reinitialize remote port roles */
1516 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
Robert Love6bd054c2009-08-25 14:03:04 -07001517
Joe Eykholta2f6a022010-03-12 16:07:36 -08001518 /*
1519 * Go through all the service parameter pages and build
1520 * response. If plen indicates longer SPP than standard,
1521 * use that. The entire response has been pre-cleared above.
1522 */
1523 spp = &pp->spp;
1524 while (len >= plen) {
1525 spp->spp_type = rspp->spp_type;
1526 spp->spp_type_ext = rspp->spp_type_ext;
1527 spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
1528 resp = FC_SPP_RESP_ACK;
Robert Love42e9a922008-12-09 15:10:17 -08001529
Joe Eykholta2f6a022010-03-12 16:07:36 -08001530 switch (rspp->spp_type) {
1531 case 0: /* common to all FC-4 types */
Robert Love42e9a922008-12-09 15:10:17 -08001532 break;
Joe Eykholta2f6a022010-03-12 16:07:36 -08001533 case FC_TYPE_FCP:
1534 fcp_parm = ntohl(rspp->spp_params);
1535 if (fcp_parm & FCP_SPPF_RETRY)
1536 rdata->flags |= FC_RP_FLAGS_RETRY;
1537 rdata->supported_classes = FC_COS_CLASS3;
1538 if (fcp_parm & FCP_SPPF_INIT_FCN)
1539 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1540 if (fcp_parm & FCP_SPPF_TARG_FCN)
1541 roles |= FC_RPORT_ROLE_FCP_TARGET;
1542 rdata->ids.roles = roles;
1543
1544 spp->spp_params = htonl(lport->service_params);
Robert Love42e9a922008-12-09 15:10:17 -08001545 break;
1546 default:
Joe Eykholta2f6a022010-03-12 16:07:36 -08001547 resp = FC_SPP_RESP_INVL;
Robert Love42e9a922008-12-09 15:10:17 -08001548 break;
1549 }
Joe Eykholta2f6a022010-03-12 16:07:36 -08001550 spp->spp_flags |= resp;
1551 len -= plen;
1552 rspp = (struct fc_els_spp *)((char *)rspp + plen);
1553 spp = (struct fc_els_spp *)((char *)spp + plen);
Robert Love42e9a922008-12-09 15:10:17 -08001554 }
Joe Eykholta2f6a022010-03-12 16:07:36 -08001555
1556 /*
1557 * Send LS_ACC. If this fails, the originator should retry.
1558 */
1559 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1560 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1561 ep = fc_seq_exch(sp);
1562 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1563 FC_TYPE_ELS, f_ctl, 0);
1564 lport->tt.seq_send(lport, sp, fp);
1565
1566 switch (rdata->rp_state) {
1567 case RPORT_ST_PRLI:
1568 fc_rport_enter_ready(rdata);
1569 break;
1570 default:
1571 break;
1572 }
1573 goto drop;
1574
1575reject_len:
1576 rjt_data.reason = ELS_RJT_PROT;
1577 rjt_data.explan = ELS_EXPL_INV_LEN;
1578reject:
1579 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1580drop:
Robert Love42e9a922008-12-09 15:10:17 -08001581 fc_frame_free(rx_fp);
1582}
1583
1584/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001585 * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
1586 * @rdata: The remote port that sent the PRLO request
1587 * @sp: The sequence that the PRLO was on
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001588 * @rx_fp: The PRLO request frame
Robert Love42e9a922008-12-09 15:10:17 -08001589 *
1590 * Locking Note: The rport lock is exected to be held before calling
1591 * this function.
1592 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001593static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
1594 struct fc_seq *sp,
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001595 struct fc_frame *rx_fp)
Robert Love42e9a922008-12-09 15:10:17 -08001596{
Robert Love42e9a922008-12-09 15:10:17 -08001597 struct fc_lport *lport = rdata->local_port;
Robert Love42e9a922008-12-09 15:10:17 -08001598 struct fc_frame_header *fh;
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001599 struct fc_exch *ep;
1600 struct fc_frame *fp;
1601 struct {
1602 struct fc_els_prlo prlo;
1603 struct fc_els_spp spp;
1604 } *pp;
1605 struct fc_els_spp *rspp; /* request service param page */
1606 struct fc_els_spp *spp; /* response spp */
1607 unsigned int len;
1608 unsigned int plen;
1609 u32 f_ctl;
Robert Love42e9a922008-12-09 15:10:17 -08001610 struct fc_seq_els_data rjt_data;
1611
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001612 rjt_data.fp = NULL;
1613 fh = fc_frame_header_get(rx_fp);
Robert Love42e9a922008-12-09 15:10:17 -08001614
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001615 FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
1616 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001617
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001618 len = fr_len(rx_fp) - sizeof(*fh);
1619 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1620 if (!pp)
1621 goto reject_len;
1622 plen = ntohs(pp->prlo.prlo_len);
1623 if (plen != 20)
1624 goto reject_len;
1625 if (plen < len)
1626 len = plen;
1627
1628 rspp = &pp->spp;
1629
1630 fp = fc_frame_alloc(lport, len);
1631 if (!fp) {
1632 rjt_data.reason = ELS_RJT_UNAB;
1633 rjt_data.explan = ELS_EXPL_INSUF_RES;
1634 goto reject;
1635 }
1636
1637 sp = lport->tt.seq_start_next(sp);
1638 WARN_ON(!sp);
1639 pp = fc_frame_payload_get(fp, len);
1640 WARN_ON(!pp);
1641 memset(pp, 0, len);
1642 pp->prlo.prlo_cmd = ELS_LS_ACC;
1643 pp->prlo.prlo_obs = 0x10;
1644 pp->prlo.prlo_len = htons(len);
1645 spp = &pp->spp;
1646 spp->spp_type = rspp->spp_type;
1647 spp->spp_type_ext = rspp->spp_type_ext;
1648 spp->spp_flags = FC_SPP_RESP_ACK;
1649
1650 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
1651
1652 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1653 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1654 ep = fc_seq_exch(sp);
1655 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1656 FC_TYPE_ELS, f_ctl, 0);
1657 lport->tt.seq_send(lport, sp, fp);
1658 goto drop;
1659
1660reject_len:
1661 rjt_data.reason = ELS_RJT_PROT;
1662 rjt_data.explan = ELS_EXPL_INV_LEN;
1663reject:
Robert Love42e9a922008-12-09 15:10:17 -08001664 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001665drop:
1666 fc_frame_free(rx_fp);
Robert Love42e9a922008-12-09 15:10:17 -08001667}
1668
1669/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001670 * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
1671 * @lport: The local port that received the LOGO request
1672 * @sp: The sequence that the LOGO request was on
1673 * @fp: The LOGO request frame
Robert Love42e9a922008-12-09 15:10:17 -08001674 *
1675 * Locking Note: The rport lock is exected to be held before calling
1676 * this function.
1677 */
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001678static void fc_rport_recv_logo_req(struct fc_lport *lport,
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001679 struct fc_seq *sp,
Robert Love42e9a922008-12-09 15:10:17 -08001680 struct fc_frame *fp)
1681{
1682 struct fc_frame_header *fh;
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001683 struct fc_rport_priv *rdata;
1684 u32 sid;
Robert Love42e9a922008-12-09 15:10:17 -08001685
Joe Eykholtfeab4ae2009-08-25 14:03:36 -07001686 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
1687
Robert Love42e9a922008-12-09 15:10:17 -08001688 fh = fc_frame_header_get(fp);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001689 sid = ntoh24(fh->fh_s_id);
Robert Love42e9a922008-12-09 15:10:17 -08001690
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001691 mutex_lock(&lport->disc.disc_mutex);
1692 rdata = lport->tt.rport_lookup(lport, sid);
1693 if (rdata) {
1694 mutex_lock(&rdata->rp_mutex);
1695 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
1696 fc_rport_state(rdata));
Joe Eykholtfeab4ae2009-08-25 14:03:36 -07001697
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001698 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001699 mutex_unlock(&rdata->rp_mutex);
1700 } else
1701 FC_RPORT_ID_DBG(lport, sid,
1702 "Received LOGO from non-logged-in port\n");
1703 mutex_unlock(&lport->disc.disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -08001704 fc_frame_free(fp);
1705}
1706
Robert Love3a3b42b2009-11-03 11:47:39 -08001707/**
1708 * fc_rport_flush_queue() - Flush the rport_event_queue
1709 */
Robert Love42e9a922008-12-09 15:10:17 -08001710static void fc_rport_flush_queue(void)
1711{
1712 flush_workqueue(rport_event_queue);
1713}
1714
Robert Love3a3b42b2009-11-03 11:47:39 -08001715/**
1716 * fc_rport_init() - Initialize the remote port layer for a local port
1717 * @lport: The local port to initialize the remote port layer for
1718 */
Robert Love42e9a922008-12-09 15:10:17 -08001719int fc_rport_init(struct fc_lport *lport)
1720{
Joe Eykholt8025b5d2009-08-25 14:02:06 -07001721 if (!lport->tt.rport_lookup)
1722 lport->tt.rport_lookup = fc_rport_lookup;
1723
Robert Love5101ff92009-02-27 10:55:18 -08001724 if (!lport->tt.rport_create)
Joe Eykholt9e9d0452009-08-25 14:01:18 -07001725 lport->tt.rport_create = fc_rport_create;
Robert Love5101ff92009-02-27 10:55:18 -08001726
Robert Love42e9a922008-12-09 15:10:17 -08001727 if (!lport->tt.rport_login)
1728 lport->tt.rport_login = fc_rport_login;
1729
1730 if (!lport->tt.rport_logoff)
1731 lport->tt.rport_logoff = fc_rport_logoff;
1732
1733 if (!lport->tt.rport_recv_req)
1734 lport->tt.rport_recv_req = fc_rport_recv_req;
1735
1736 if (!lport->tt.rport_flush_queue)
1737 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1738
Joe Eykholtf211fa52009-08-25 14:01:01 -07001739 if (!lport->tt.rport_destroy)
1740 lport->tt.rport_destroy = fc_rport_destroy;
1741
Robert Love42e9a922008-12-09 15:10:17 -08001742 return 0;
1743}
1744EXPORT_SYMBOL(fc_rport_init);
1745
Robert Love3a3b42b2009-11-03 11:47:39 -08001746/**
1747 * fc_setup_rport() - Initialize the rport_event_queue
1748 */
1749int fc_setup_rport()
Robert Love42e9a922008-12-09 15:10:17 -08001750{
1751 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
1752 if (!rport_event_queue)
1753 return -ENOMEM;
1754 return 0;
1755}
Robert Love42e9a922008-12-09 15:10:17 -08001756
Robert Love3a3b42b2009-11-03 11:47:39 -08001757/**
1758 * fc_destroy_rport() - Destroy the rport_event_queue
1759 */
1760void fc_destroy_rport()
Robert Love42e9a922008-12-09 15:10:17 -08001761{
1762 destroy_workqueue(rport_event_queue);
1763}
Robert Love42e9a922008-12-09 15:10:17 -08001764
Robert Love3a3b42b2009-11-03 11:47:39 -08001765/**
1766 * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
1767 * @rport: The remote port whose I/O should be terminated
1768 */
Robert Love42e9a922008-12-09 15:10:17 -08001769void fc_rport_terminate_io(struct fc_rport *rport)
1770{
Robert Love3a3b42b2009-11-03 11:47:39 -08001771 struct fc_rport_libfc_priv *rpriv = rport->dd_data;
1772 struct fc_lport *lport = rpriv->local_port;
Robert Love42e9a922008-12-09 15:10:17 -08001773
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -08001774 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
1775 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
Robert Love42e9a922008-12-09 15:10:17 -08001776}
1777EXPORT_SYMBOL(fc_rport_terminate_io);