blob: 406049c13a0e508b3ee2659b4b36791940e887ce [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>
50#include <linux/rcupdate.h>
51#include <linux/timer.h>
52#include <linux/workqueue.h>
53#include <asm/unaligned.h>
54
55#include <scsi/libfc.h>
56#include <scsi/fc_encode.h>
57
Robert Love42e9a922008-12-09 15:10:17 -080058struct workqueue_struct *rport_event_queue;
59
Joe Eykholt9fb9d322009-08-25 14:00:50 -070060static void fc_rport_enter_plogi(struct fc_rport_priv *);
61static void fc_rport_enter_prli(struct fc_rport_priv *);
62static void fc_rport_enter_rtv(struct fc_rport_priv *);
63static void fc_rport_enter_ready(struct fc_rport_priv *);
64static void fc_rport_enter_logo(struct fc_rport_priv *);
Robert Love42e9a922008-12-09 15:10:17 -080065
Joe Eykholt9fb9d322009-08-25 14:00:50 -070066static void fc_rport_recv_plogi_req(struct fc_rport_priv *,
Robert Love42e9a922008-12-09 15:10:17 -080067 struct fc_seq *, struct fc_frame *);
Joe Eykholt9fb9d322009-08-25 14:00:50 -070068static void fc_rport_recv_prli_req(struct fc_rport_priv *,
Robert Love42e9a922008-12-09 15:10:17 -080069 struct fc_seq *, struct fc_frame *);
Joe Eykholt9fb9d322009-08-25 14:00:50 -070070static void fc_rport_recv_prlo_req(struct fc_rport_priv *,
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_logo_req(struct fc_rport_priv *,
Robert Love42e9a922008-12-09 15:10:17 -080073 struct fc_seq *, struct fc_frame *);
74static void fc_rport_timeout(struct work_struct *);
Joe Eykholt9fb9d322009-08-25 14:00:50 -070075static void fc_rport_error(struct fc_rport_priv *, struct fc_frame *);
76static void fc_rport_error_retry(struct fc_rport_priv *, struct fc_frame *);
Robert Love42e9a922008-12-09 15:10:17 -080077static void fc_rport_work(struct work_struct *);
78
79static const char *fc_rport_state_names[] = {
Robert Love42e9a922008-12-09 15:10:17 -080080 [RPORT_ST_INIT] = "Init",
81 [RPORT_ST_PLOGI] = "PLOGI",
82 [RPORT_ST_PRLI] = "PRLI",
83 [RPORT_ST_RTV] = "RTV",
84 [RPORT_ST_READY] = "Ready",
85 [RPORT_ST_LOGO] = "LOGO",
Joe Eykholt14194052009-07-29 17:04:43 -070086 [RPORT_ST_DELETE] = "Delete",
Robert Love42e9a922008-12-09 15:10:17 -080087};
88
Joe Eykholt9e9d0452009-08-25 14:01:18 -070089/**
90 * fc_rport_create() - create remote port in INIT state.
91 * @lport: local port.
92 * @ids: remote port identifiers.
93 *
Joe Eykholt48f00902009-08-25 14:01:50 -070094 * Locking note: must be called with the disc_mutex held.
Joe Eykholt9e9d0452009-08-25 14:01:18 -070095 */
96static struct fc_rport_priv *fc_rport_create(struct fc_lport *lport,
97 struct fc_rport_identifiers *ids)
Robert Love42e9a922008-12-09 15:10:17 -080098{
Joe Eykholtab28f1f2009-08-25 14:00:34 -070099 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800100
Joe Eykholt19f97e32009-08-25 14:01:55 -0700101 rdata = lport->tt.rport_lookup(lport, ids->port_id);
102 if (rdata)
103 return rdata;
104
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700105 rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
106 if (!rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800107 return NULL;
108
Joe Eykholtf211fa52009-08-25 14:01:01 -0700109 rdata->ids = *ids;
110 kref_init(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800111 mutex_init(&rdata->rp_mutex);
Joe Eykholt795d86f2009-08-25 14:00:39 -0700112 rdata->local_port = lport;
Robert Love42e9a922008-12-09 15:10:17 -0800113 rdata->rp_state = RPORT_ST_INIT;
114 rdata->event = RPORT_EV_NONE;
115 rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
Joe Eykholt795d86f2009-08-25 14:00:39 -0700116 rdata->e_d_tov = lport->e_d_tov;
117 rdata->r_a_tov = lport->r_a_tov;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700118 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
Robert Love42e9a922008-12-09 15:10:17 -0800119 INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
120 INIT_WORK(&rdata->event_work, fc_rport_work);
Joe Eykholt48f00902009-08-25 14:01:50 -0700121 if (ids->port_id != FC_FID_DIR_SERV)
122 list_add(&rdata->peers, &lport->disc.rports);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700123 return rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800124}
125
126/**
Joe Eykholtf211fa52009-08-25 14:01:01 -0700127 * fc_rport_destroy() - free a remote port after last reference is released.
128 * @kref: pointer to kref inside struct fc_rport_priv
129 */
130static void fc_rport_destroy(struct kref *kref)
131{
132 struct fc_rport_priv *rdata;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700133
134 rdata = container_of(kref, struct fc_rport_priv, kref);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700135 kfree(rdata);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700136}
137
138/**
Robert Love34f42a02009-02-27 10:55:45 -0800139 * fc_rport_state() - return a string for the state the rport is in
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700140 * @rdata: remote port private data
Robert Love42e9a922008-12-09 15:10:17 -0800141 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700142static const char *fc_rport_state(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800143{
144 const char *cp;
Robert Love42e9a922008-12-09 15:10:17 -0800145
146 cp = fc_rport_state_names[rdata->rp_state];
147 if (!cp)
148 cp = "Unknown";
149 return cp;
150}
151
152/**
Robert Love34f42a02009-02-27 10:55:45 -0800153 * fc_set_rport_loss_tmo() - Set the remote port loss timeout in seconds.
Robert Love42e9a922008-12-09 15:10:17 -0800154 * @rport: Pointer to Fibre Channel remote port structure
155 * @timeout: timeout in seconds
156 */
157void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
158{
159 if (timeout)
160 rport->dev_loss_tmo = timeout + 5;
161 else
162 rport->dev_loss_tmo = 30;
163}
164EXPORT_SYMBOL(fc_set_rport_loss_tmo);
165
166/**
Robert Love34f42a02009-02-27 10:55:45 -0800167 * fc_plogi_get_maxframe() - Get max payload from the common service parameters
Robert Love42e9a922008-12-09 15:10:17 -0800168 * @flp: FLOGI payload structure
169 * @maxval: upper limit, may be less than what is in the service parameters
170 */
Robert Loveb2ab99c2009-02-27 10:55:50 -0800171static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
172 unsigned int maxval)
Robert Love42e9a922008-12-09 15:10:17 -0800173{
174 unsigned int mfs;
175
176 /*
177 * Get max payload from the common service parameters and the
178 * class 3 receive data field size.
179 */
180 mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
181 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
182 maxval = mfs;
183 mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
184 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
185 maxval = mfs;
186 return maxval;
187}
188
189/**
Robert Love34f42a02009-02-27 10:55:45 -0800190 * fc_rport_state_enter() - Change the rport's state
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700191 * @rdata: The rport whose state should change
Robert Love42e9a922008-12-09 15:10:17 -0800192 * @new: The new state of the rport
193 *
194 * Locking Note: Called with the rport lock held
195 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700196static void fc_rport_state_enter(struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -0800197 enum fc_rport_state new)
198{
Robert Love42e9a922008-12-09 15:10:17 -0800199 if (rdata->rp_state != new)
200 rdata->retries = 0;
201 rdata->rp_state = new;
202}
203
204static void fc_rport_work(struct work_struct *work)
205{
Abhijeet Joglekar571f8242009-02-27 10:54:41 -0800206 u32 port_id;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700207 struct fc_rport_priv *rdata =
208 container_of(work, struct fc_rport_priv, event_work);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700209 struct fc_rport_libfc_priv *rp;
Robert Love42e9a922008-12-09 15:10:17 -0800210 enum fc_rport_event event;
Robert Love42e9a922008-12-09 15:10:17 -0800211 struct fc_lport *lport = rdata->local_port;
212 struct fc_rport_operations *rport_ops;
Joe Eykholt629f4422009-08-25 14:01:06 -0700213 struct fc_rport_identifiers ids;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700214 struct fc_rport *rport;
Robert Love42e9a922008-12-09 15:10:17 -0800215
216 mutex_lock(&rdata->rp_mutex);
217 event = rdata->event;
218 rport_ops = rdata->ops;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700219 rport = rdata->rport;
Robert Love42e9a922008-12-09 15:10:17 -0800220
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700221 FC_RPORT_DBG(rdata, "work event %u\n", event);
222
Joe Eykholt629f4422009-08-25 14:01:06 -0700223 switch (event) {
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700224 case RPORT_EV_READY:
Joe Eykholtf211fa52009-08-25 14:01:01 -0700225 ids = rdata->ids;
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700226 rdata->event = RPORT_EV_NONE;
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700227 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800228 mutex_unlock(&rdata->rp_mutex);
229
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700230 if (!rport)
231 rport = fc_remote_port_add(lport->host, 0, &ids);
232 if (!rport) {
233 FC_RPORT_DBG(rdata, "Failed to add the rport\n");
234 lport->tt.rport_logoff(rdata);
235 kref_put(&rdata->kref, lport->tt.rport_destroy);
236 return;
Robert Love42e9a922008-12-09 15:10:17 -0800237 }
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700238 mutex_lock(&rdata->rp_mutex);
239 if (rdata->rport)
240 FC_RPORT_DBG(rdata, "rport already allocated\n");
241 rdata->rport = rport;
242 rport->maxframe_size = rdata->maxframe_size;
243 rport->supported_classes = rdata->supported_classes;
244
245 rp = rport->dd_data;
246 rp->local_port = lport;
247 rp->rp_state = rdata->rp_state;
248 rp->flags = rdata->flags;
249 rp->e_d_tov = rdata->e_d_tov;
250 rp->r_a_tov = rdata->r_a_tov;
251 mutex_unlock(&rdata->rp_mutex);
252
Joe Eykholt83455922009-08-25 14:02:01 -0700253 if (rport_ops && rport_ops->event_callback) {
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700254 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700255 rport_ops->event_callback(lport, rdata, event);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700256 }
257 kref_put(&rdata->kref, lport->tt.rport_destroy);
Joe Eykholt629f4422009-08-25 14:01:06 -0700258 break;
259
260 case RPORT_EV_FAILED:
261 case RPORT_EV_LOGO:
262 case RPORT_EV_STOP:
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700263 port_id = rdata->ids.port_id;
Robert Love42e9a922008-12-09 15:10:17 -0800264 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700265
Joe Eykholt48f00902009-08-25 14:01:50 -0700266 if (port_id != FC_FID_DIR_SERV) {
267 mutex_lock(&lport->disc.disc_mutex);
268 list_del(&rdata->peers);
269 mutex_unlock(&lport->disc.disc_mutex);
270 }
271
Joe Eykholt83455922009-08-25 14:02:01 -0700272 if (rport_ops && rport_ops->event_callback) {
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700273 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700274 rport_ops->event_callback(lport, rdata, event);
Abhijeet Joglekar571f8242009-02-27 10:54:41 -0800275 }
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700276 cancel_delayed_work_sync(&rdata->retry_work);
277
278 /*
279 * Reset any outstanding exchanges before freeing rport.
280 */
281 lport->tt.exch_mgr_reset(lport, 0, port_id);
282 lport->tt.exch_mgr_reset(lport, port_id, 0);
283
284 if (rport) {
285 rp = rport->dd_data;
286 rp->rp_state = RPORT_ST_DELETE;
287 mutex_lock(&rdata->rp_mutex);
288 rdata->rport = NULL;
289 mutex_unlock(&rdata->rp_mutex);
290 fc_remote_port_delete(rport);
291 }
292 kref_put(&rdata->kref, lport->tt.rport_destroy);
Joe Eykholt629f4422009-08-25 14:01:06 -0700293 break;
294
295 default:
Robert Love42e9a922008-12-09 15:10:17 -0800296 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt629f4422009-08-25 14:01:06 -0700297 break;
298 }
Robert Love42e9a922008-12-09 15:10:17 -0800299}
300
301/**
Robert Love34f42a02009-02-27 10:55:45 -0800302 * fc_rport_login() - Start the remote port login state machine
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700303 * @rdata: private remote port
Robert Love42e9a922008-12-09 15:10:17 -0800304 *
305 * Locking Note: Called without the rport lock held. This
306 * function will hold the rport lock, call an _enter_*
307 * function and then unlock the rport.
308 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700309int fc_rport_login(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800310{
Robert Love42e9a922008-12-09 15:10:17 -0800311 mutex_lock(&rdata->rp_mutex);
312
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700313 FC_RPORT_DBG(rdata, "Login to port\n");
Robert Love42e9a922008-12-09 15:10:17 -0800314
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700315 fc_rport_enter_plogi(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800316
317 mutex_unlock(&rdata->rp_mutex);
318
319 return 0;
320}
321
322/**
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700323 * fc_rport_enter_delete() - schedule a remote port to be deleted.
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700324 * @rdata: private remote port
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700325 * @event: event to report as the reason for deletion
326 *
327 * Locking Note: Called with the rport lock held.
328 *
329 * Allow state change into DELETE only once.
330 *
331 * Call queue_work only if there's no event already pending.
332 * Set the new event so that the old pending event will not occur.
333 * Since we have the mutex, even if fc_rport_work() is already started,
334 * it'll see the new event.
335 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700336static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700337 enum fc_rport_event event)
338{
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700339 if (rdata->rp_state == RPORT_ST_DELETE)
340 return;
341
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700342 FC_RPORT_DBG(rdata, "Delete port\n");
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700343
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700344 fc_rport_state_enter(rdata, RPORT_ST_DELETE);
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700345
346 if (rdata->event == RPORT_EV_NONE)
347 queue_work(rport_event_queue, &rdata->event_work);
348 rdata->event = event;
349}
350
351/**
Robert Love34f42a02009-02-27 10:55:45 -0800352 * fc_rport_logoff() - Logoff and remove an rport
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700353 * @rdata: private remote port
Robert Love42e9a922008-12-09 15:10:17 -0800354 *
355 * Locking Note: Called without the rport lock held. This
356 * function will hold the rport lock, call an _enter_*
357 * function and then unlock the rport.
358 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700359int fc_rport_logoff(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800360{
Robert Love42e9a922008-12-09 15:10:17 -0800361 mutex_lock(&rdata->rp_mutex);
362
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700363 FC_RPORT_DBG(rdata, "Remove port\n");
Robert Love42e9a922008-12-09 15:10:17 -0800364
Joe Eykholt14194052009-07-29 17:04:43 -0700365 if (rdata->rp_state == RPORT_ST_DELETE) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700366 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700367 mutex_unlock(&rdata->rp_mutex);
368 goto out;
369 }
370
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700371 fc_rport_enter_logo(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800372
373 /*
Joe Eykholt14194052009-07-29 17:04:43 -0700374 * Change the state to Delete so that we discard
Robert Love42e9a922008-12-09 15:10:17 -0800375 * the response.
376 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700377 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
Robert Love42e9a922008-12-09 15:10:17 -0800378 mutex_unlock(&rdata->rp_mutex);
379
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700380out:
Robert Love42e9a922008-12-09 15:10:17 -0800381 return 0;
382}
383
384/**
Robert Love34f42a02009-02-27 10:55:45 -0800385 * fc_rport_enter_ready() - The rport is ready
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700386 * @rdata: private remote port
Robert Love42e9a922008-12-09 15:10:17 -0800387 *
388 * Locking Note: The rport lock is expected to be held before calling
389 * this routine.
390 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700391static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800392{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700393 fc_rport_state_enter(rdata, RPORT_ST_READY);
Robert Love42e9a922008-12-09 15:10:17 -0800394
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700395 FC_RPORT_DBG(rdata, "Port is Ready\n");
Robert Love42e9a922008-12-09 15:10:17 -0800396
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700397 if (rdata->event == RPORT_EV_NONE)
398 queue_work(rport_event_queue, &rdata->event_work);
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700399 rdata->event = RPORT_EV_READY;
Robert Love42e9a922008-12-09 15:10:17 -0800400}
401
402/**
Robert Love34f42a02009-02-27 10:55:45 -0800403 * fc_rport_timeout() - Handler for the retry_work timer.
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700404 * @work: The work struct of the fc_rport_priv
Robert Love42e9a922008-12-09 15:10:17 -0800405 *
406 * Locking Note: Called without the rport lock held. This
407 * function will hold the rport lock, call an _enter_*
408 * function and then unlock the rport.
409 */
410static void fc_rport_timeout(struct work_struct *work)
411{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700412 struct fc_rport_priv *rdata =
413 container_of(work, struct fc_rport_priv, retry_work.work);
Robert Love42e9a922008-12-09 15:10:17 -0800414
415 mutex_lock(&rdata->rp_mutex);
416
417 switch (rdata->rp_state) {
418 case RPORT_ST_PLOGI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700419 fc_rport_enter_plogi(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800420 break;
421 case RPORT_ST_PRLI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700422 fc_rport_enter_prli(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800423 break;
424 case RPORT_ST_RTV:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700425 fc_rport_enter_rtv(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800426 break;
427 case RPORT_ST_LOGO:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700428 fc_rport_enter_logo(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800429 break;
430 case RPORT_ST_READY:
431 case RPORT_ST_INIT:
Joe Eykholt14194052009-07-29 17:04:43 -0700432 case RPORT_ST_DELETE:
Robert Love42e9a922008-12-09 15:10:17 -0800433 break;
434 }
435
436 mutex_unlock(&rdata->rp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800437}
438
439/**
Robert Love34f42a02009-02-27 10:55:45 -0800440 * fc_rport_error() - Error handler, called once retries have been exhausted
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700441 * @rdata: private remote port
Robert Love42e9a922008-12-09 15:10:17 -0800442 * @fp: The frame pointer
443 *
Robert Love42e9a922008-12-09 15:10:17 -0800444 * Locking Note: The rport lock is expected to be held before
445 * calling this routine
446 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700447static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -0800448{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700449 FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
Joe Eykholtcdbe6df2009-08-25 14:01:39 -0700450 IS_ERR(fp) ? -PTR_ERR(fp) : 0,
451 fc_rport_state(rdata), rdata->retries);
Robert Love42e9a922008-12-09 15:10:17 -0800452
Chris Leech6755db12009-02-27 10:55:02 -0800453 switch (rdata->rp_state) {
454 case RPORT_ST_PLOGI:
455 case RPORT_ST_PRLI:
456 case RPORT_ST_LOGO:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700457 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
Chris Leech6755db12009-02-27 10:55:02 -0800458 break;
459 case RPORT_ST_RTV:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700460 fc_rport_enter_ready(rdata);
Chris Leech6755db12009-02-27 10:55:02 -0800461 break;
Joe Eykholt14194052009-07-29 17:04:43 -0700462 case RPORT_ST_DELETE:
Chris Leech6755db12009-02-27 10:55:02 -0800463 case RPORT_ST_READY:
464 case RPORT_ST_INIT:
465 break;
Robert Love42e9a922008-12-09 15:10:17 -0800466 }
467}
468
469/**
Robert Love34f42a02009-02-27 10:55:45 -0800470 * fc_rport_error_retry() - Error handler when retries are desired
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700471 * @rdata: private remote port data
Chris Leech6755db12009-02-27 10:55:02 -0800472 * @fp: The frame pointer
473 *
474 * If the error was an exchange timeout retry immediately,
475 * otherwise wait for E_D_TOV.
476 *
477 * Locking Note: The rport lock is expected to be held before
478 * calling this routine
479 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700480static void fc_rport_error_retry(struct fc_rport_priv *rdata,
481 struct fc_frame *fp)
Chris Leech6755db12009-02-27 10:55:02 -0800482{
Chris Leech6755db12009-02-27 10:55:02 -0800483 unsigned long delay = FC_DEF_E_D_TOV;
484
485 /* make sure this isn't an FC_EX_CLOSED error, never retry those */
486 if (PTR_ERR(fp) == -FC_EX_CLOSED)
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700487 return fc_rport_error(rdata, fp);
Chris Leech6755db12009-02-27 10:55:02 -0800488
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700489 if (rdata->retries < rdata->local_port->max_rport_retry_count) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700490 FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
491 PTR_ERR(fp), fc_rport_state(rdata));
Chris Leech6755db12009-02-27 10:55:02 -0800492 rdata->retries++;
493 /* no additional delay on exchange timeouts */
494 if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
495 delay = 0;
Chris Leech6755db12009-02-27 10:55:02 -0800496 schedule_delayed_work(&rdata->retry_work, delay);
497 return;
498 }
499
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700500 return fc_rport_error(rdata, fp);
Chris Leech6755db12009-02-27 10:55:02 -0800501}
502
503/**
Robert Love34f42a02009-02-27 10:55:45 -0800504 * fc_rport_plogi_recv_resp() - Handle incoming ELS PLOGI response
Robert Love42e9a922008-12-09 15:10:17 -0800505 * @sp: current sequence in the PLOGI exchange
506 * @fp: response frame
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700507 * @rdata_arg: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800508 *
509 * Locking Note: This function will be called without the rport lock
510 * held, but it will lock, call an _enter_* function or fc_rport_error
511 * and then unlock the rport.
512 */
513static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700514 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800515{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700516 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800517 struct fc_lport *lport = rdata->local_port;
Robert Lovea29e7642009-04-21 16:27:41 -0700518 struct fc_els_flogi *plp = NULL;
Robert Love42e9a922008-12-09 15:10:17 -0800519 unsigned int tov;
520 u16 csp_seq;
521 u16 cssp_seq;
522 u8 op;
523
524 mutex_lock(&rdata->rp_mutex);
525
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700526 FC_RPORT_DBG(rdata, "Received a PLOGI response\n");
Robert Love42e9a922008-12-09 15:10:17 -0800527
528 if (rdata->rp_state != RPORT_ST_PLOGI) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700529 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
530 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700531 if (IS_ERR(fp))
532 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800533 goto out;
534 }
535
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700536 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700537 fc_rport_error_retry(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700538 goto err;
539 }
540
Robert Love42e9a922008-12-09 15:10:17 -0800541 op = fc_frame_payload_op(fp);
542 if (op == ELS_LS_ACC &&
543 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
Joe Eykholtf211fa52009-08-25 14:01:01 -0700544 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
545 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
Robert Love42e9a922008-12-09 15:10:17 -0800546
547 tov = ntohl(plp->fl_csp.sp_e_d_tov);
548 if (ntohs(plp->fl_csp.sp_features) & FC_SP_FT_EDTR)
549 tov /= 1000;
550 if (tov > rdata->e_d_tov)
551 rdata->e_d_tov = tov;
552 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
553 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
554 if (cssp_seq < csp_seq)
555 csp_seq = cssp_seq;
556 rdata->max_seq = csp_seq;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700557 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
Robert Love42e9a922008-12-09 15:10:17 -0800558
559 /*
560 * If the rport is one of the well known addresses
561 * we skip PRLI and RTV and go straight to READY.
562 */
Joe Eykholtf211fa52009-08-25 14:01:01 -0700563 if (rdata->ids.port_id >= FC_FID_DOM_MGR)
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700564 fc_rport_enter_ready(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800565 else
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700566 fc_rport_enter_prli(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800567 } else
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700568 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800569
570out:
571 fc_frame_free(fp);
572err:
573 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700574 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800575}
576
577/**
Robert Love34f42a02009-02-27 10:55:45 -0800578 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request to peer
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700579 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800580 *
581 * Locking Note: The rport lock is expected to be held before calling
582 * this routine.
583 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700584static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800585{
Robert Love42e9a922008-12-09 15:10:17 -0800586 struct fc_lport *lport = rdata->local_port;
587 struct fc_frame *fp;
588
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700589 FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
590 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800591
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700592 fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
Robert Love42e9a922008-12-09 15:10:17 -0800593
Joe Eykholtf211fa52009-08-25 14:01:01 -0700594 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
Robert Love42e9a922008-12-09 15:10:17 -0800595 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
596 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700597 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800598 return;
599 }
600 rdata->e_d_tov = lport->e_d_tov;
601
Joe Eykholtf211fa52009-08-25 14:01:01 -0700602 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700603 fc_rport_plogi_resp, rdata, lport->e_d_tov))
604 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800605 else
Joe Eykholtf211fa52009-08-25 14:01:01 -0700606 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800607}
608
609/**
Robert Love34f42a02009-02-27 10:55:45 -0800610 * fc_rport_prli_resp() - Process Login (PRLI) response handler
Robert Love42e9a922008-12-09 15:10:17 -0800611 * @sp: current sequence in the PRLI exchange
612 * @fp: response frame
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700613 * @rdata_arg: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800614 *
615 * Locking Note: This function will be called without the rport lock
616 * held, but it will lock, call an _enter_* function or fc_rport_error
617 * and then unlock the rport.
618 */
619static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700620 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800621{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700622 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800623 struct {
624 struct fc_els_prli prli;
625 struct fc_els_spp spp;
626 } *pp;
627 u32 roles = FC_RPORT_ROLE_UNKNOWN;
628 u32 fcp_parm = 0;
629 u8 op;
630
631 mutex_lock(&rdata->rp_mutex);
632
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700633 FC_RPORT_DBG(rdata, "Received a PRLI response\n");
Robert Love42e9a922008-12-09 15:10:17 -0800634
635 if (rdata->rp_state != RPORT_ST_PRLI) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700636 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
637 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700638 if (IS_ERR(fp))
639 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800640 goto out;
641 }
642
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700643 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700644 fc_rport_error_retry(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700645 goto err;
646 }
647
Robert Love42e9a922008-12-09 15:10:17 -0800648 op = fc_frame_payload_op(fp);
649 if (op == ELS_LS_ACC) {
650 pp = fc_frame_payload_get(fp, sizeof(*pp));
651 if (pp && pp->prli.prli_spp_len >= sizeof(pp->spp)) {
652 fcp_parm = ntohl(pp->spp.spp_params);
653 if (fcp_parm & FCP_SPPF_RETRY)
654 rdata->flags |= FC_RP_FLAGS_RETRY;
655 }
656
Joe Eykholtf211fa52009-08-25 14:01:01 -0700657 rdata->supported_classes = FC_COS_CLASS3;
Robert Love42e9a922008-12-09 15:10:17 -0800658 if (fcp_parm & FCP_SPPF_INIT_FCN)
659 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
660 if (fcp_parm & FCP_SPPF_TARG_FCN)
661 roles |= FC_RPORT_ROLE_FCP_TARGET;
662
Joe Eykholtf211fa52009-08-25 14:01:01 -0700663 rdata->ids.roles = roles;
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700664 fc_rport_enter_rtv(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800665
666 } else {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700667 FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
668 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
Robert Love42e9a922008-12-09 15:10:17 -0800669 }
670
671out:
672 fc_frame_free(fp);
673err:
674 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700675 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800676}
677
678/**
Robert Love34f42a02009-02-27 10:55:45 -0800679 * fc_rport_logo_resp() - Logout (LOGO) response handler
Robert Love42e9a922008-12-09 15:10:17 -0800680 * @sp: current sequence in the LOGO exchange
681 * @fp: response frame
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700682 * @rdata_arg: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800683 *
684 * Locking Note: This function will be called without the rport lock
685 * held, but it will lock, call an _enter_* function or fc_rport_error
686 * and then unlock the rport.
687 */
688static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700689 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800690{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700691 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800692 u8 op;
693
694 mutex_lock(&rdata->rp_mutex);
695
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700696 FC_RPORT_DBG(rdata, "Received a LOGO response\n");
Robert Love42e9a922008-12-09 15:10:17 -0800697
Robert Love42e9a922008-12-09 15:10:17 -0800698 if (rdata->rp_state != RPORT_ST_LOGO) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700699 FC_RPORT_DBG(rdata, "Received a LOGO response, but in state "
700 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700701 if (IS_ERR(fp))
702 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800703 goto out;
704 }
705
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700706 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700707 fc_rport_error_retry(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700708 goto err;
709 }
710
Robert Love42e9a922008-12-09 15:10:17 -0800711 op = fc_frame_payload_op(fp);
712 if (op == ELS_LS_ACC) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700713 fc_rport_enter_rtv(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800714 } else {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700715 FC_RPORT_DBG(rdata, "Bad ELS response for LOGO command\n");
716 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
Robert Love42e9a922008-12-09 15:10:17 -0800717 }
718
719out:
720 fc_frame_free(fp);
721err:
722 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700723 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800724}
725
726/**
Robert Love34f42a02009-02-27 10:55:45 -0800727 * fc_rport_enter_prli() - Send Process Login (PRLI) request to peer
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700728 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800729 *
730 * Locking Note: The rport lock is expected to be held before calling
731 * this routine.
732 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700733static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800734{
Robert Love42e9a922008-12-09 15:10:17 -0800735 struct fc_lport *lport = rdata->local_port;
736 struct {
737 struct fc_els_prli prli;
738 struct fc_els_spp spp;
739 } *pp;
740 struct fc_frame *fp;
741
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700742 FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
743 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800744
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700745 fc_rport_state_enter(rdata, RPORT_ST_PRLI);
Robert Love42e9a922008-12-09 15:10:17 -0800746
747 fp = fc_frame_alloc(lport, sizeof(*pp));
748 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700749 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800750 return;
751 }
752
Joe Eykholtf211fa52009-08-25 14:01:01 -0700753 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PRLI,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700754 fc_rport_prli_resp, rdata, lport->e_d_tov))
755 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800756 else
Joe Eykholtf211fa52009-08-25 14:01:01 -0700757 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800758}
759
760/**
Robert Love34f42a02009-02-27 10:55:45 -0800761 * fc_rport_els_rtv_resp() - Request Timeout Value response handler
Robert Love42e9a922008-12-09 15:10:17 -0800762 * @sp: current sequence in the RTV exchange
763 * @fp: response frame
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700764 * @rdata_arg: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800765 *
766 * Many targets don't seem to support this.
767 *
768 * Locking Note: This function will be called without the rport lock
769 * held, but it will lock, call an _enter_* function or fc_rport_error
770 * and then unlock the rport.
771 */
772static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700773 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800774{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700775 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800776 u8 op;
777
778 mutex_lock(&rdata->rp_mutex);
779
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700780 FC_RPORT_DBG(rdata, "Received a RTV response\n");
Robert Love42e9a922008-12-09 15:10:17 -0800781
782 if (rdata->rp_state != RPORT_ST_RTV) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700783 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
784 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700785 if (IS_ERR(fp))
786 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800787 goto out;
788 }
789
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700790 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700791 fc_rport_error(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700792 goto err;
793 }
794
Robert Love42e9a922008-12-09 15:10:17 -0800795 op = fc_frame_payload_op(fp);
796 if (op == ELS_LS_ACC) {
797 struct fc_els_rtv_acc *rtv;
798 u32 toq;
799 u32 tov;
800
801 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
802 if (rtv) {
803 toq = ntohl(rtv->rtv_toq);
804 tov = ntohl(rtv->rtv_r_a_tov);
805 if (tov == 0)
806 tov = 1;
807 rdata->r_a_tov = tov;
808 tov = ntohl(rtv->rtv_e_d_tov);
809 if (toq & FC_ELS_RTV_EDRES)
810 tov /= 1000000;
811 if (tov == 0)
812 tov = 1;
813 rdata->e_d_tov = tov;
814 }
815 }
816
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700817 fc_rport_enter_ready(rdata);
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 Love34f42a02009-02-27 10:55:45 -0800827 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request to peer
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700828 * @rdata: private remote port data
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_rtv(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800834{
835 struct fc_frame *fp;
Robert Love42e9a922008-12-09 15:10:17 -0800836 struct fc_lport *lport = rdata->local_port;
837
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700838 FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
839 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800840
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700841 fc_rport_state_enter(rdata, RPORT_ST_RTV);
Robert Love42e9a922008-12-09 15:10:17 -0800842
843 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
844 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700845 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800846 return;
847 }
848
Joe Eykholtf211fa52009-08-25 14:01:01 -0700849 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700850 fc_rport_rtv_resp, rdata, lport->e_d_tov))
851 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800852 else
Joe Eykholtf211fa52009-08-25 14:01:01 -0700853 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800854}
855
856/**
Robert Love34f42a02009-02-27 10:55:45 -0800857 * fc_rport_enter_logo() - Send Logout (LOGO) request to peer
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700858 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800859 *
860 * Locking Note: The rport lock is expected to be held before calling
861 * this routine.
862 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700863static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800864{
Robert Love42e9a922008-12-09 15:10:17 -0800865 struct fc_lport *lport = rdata->local_port;
866 struct fc_frame *fp;
867
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700868 FC_RPORT_DBG(rdata, "Port entered LOGO state from %s state\n",
869 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800870
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700871 fc_rport_state_enter(rdata, RPORT_ST_LOGO);
Robert Love42e9a922008-12-09 15:10:17 -0800872
873 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
874 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700875 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800876 return;
877 }
878
Joe Eykholtf211fa52009-08-25 14:01:01 -0700879 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700880 fc_rport_logo_resp, rdata, lport->e_d_tov))
881 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800882 else
Joe Eykholtf211fa52009-08-25 14:01:01 -0700883 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800884}
885
886
887/**
Robert Love34f42a02009-02-27 10:55:45 -0800888 * fc_rport_recv_req() - Receive a request from a rport
Robert Love42e9a922008-12-09 15:10:17 -0800889 * @sp: current sequence in the PLOGI exchange
890 * @fp: response frame
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700891 * @rdata_arg: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800892 *
893 * Locking Note: Called without the rport lock held. This
894 * function will hold the rport lock, call an _enter_*
895 * function and then unlock the rport.
896 */
897void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700898 struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800899{
Robert Love42e9a922008-12-09 15:10:17 -0800900 struct fc_lport *lport = rdata->local_port;
901
902 struct fc_frame_header *fh;
903 struct fc_seq_els_data els_data;
904 u8 op;
905
906 mutex_lock(&rdata->rp_mutex);
907
908 els_data.fp = NULL;
909 els_data.explan = ELS_EXPL_NONE;
910 els_data.reason = ELS_RJT_NONE;
911
912 fh = fc_frame_header_get(fp);
913
914 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ && fh->fh_type == FC_TYPE_ELS) {
915 op = fc_frame_payload_op(fp);
916 switch (op) {
917 case ELS_PLOGI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700918 fc_rport_recv_plogi_req(rdata, sp, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800919 break;
920 case ELS_PRLI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700921 fc_rport_recv_prli_req(rdata, sp, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800922 break;
923 case ELS_PRLO:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700924 fc_rport_recv_prlo_req(rdata, sp, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800925 break;
926 case ELS_LOGO:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700927 fc_rport_recv_logo_req(rdata, sp, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800928 break;
929 case ELS_RRQ:
930 els_data.fp = fp;
931 lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
932 break;
933 case ELS_REC:
934 els_data.fp = fp;
935 lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
936 break;
937 default:
938 els_data.reason = ELS_RJT_UNSUP;
939 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
940 break;
941 }
942 }
943
944 mutex_unlock(&rdata->rp_mutex);
945}
946
947/**
Robert Love34f42a02009-02-27 10:55:45 -0800948 * fc_rport_recv_plogi_req() - Handle incoming Port Login (PLOGI) request
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700949 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800950 * @sp: current sequence in the PLOGI exchange
951 * @fp: PLOGI request frame
952 *
953 * Locking Note: The rport lock is exected to be held before calling
954 * this function.
955 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700956static void fc_rport_recv_plogi_req(struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -0800957 struct fc_seq *sp, struct fc_frame *rx_fp)
958{
Robert Love42e9a922008-12-09 15:10:17 -0800959 struct fc_lport *lport = rdata->local_port;
960 struct fc_frame *fp = rx_fp;
961 struct fc_exch *ep;
962 struct fc_frame_header *fh;
963 struct fc_els_flogi *pl;
964 struct fc_seq_els_data rjt_data;
965 u32 sid;
966 u64 wwpn;
967 u64 wwnn;
968 enum fc_els_rjt_reason reject = 0;
969 u32 f_ctl;
970 rjt_data.fp = NULL;
971
972 fh = fc_frame_header_get(fp);
973
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700974 FC_RPORT_DBG(rdata, "Received PLOGI request while in state %s\n",
975 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800976
977 sid = ntoh24(fh->fh_s_id);
978 pl = fc_frame_payload_get(fp, sizeof(*pl));
979 if (!pl) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700980 FC_RPORT_DBG(rdata, "Received PLOGI too short\n");
Robert Love42e9a922008-12-09 15:10:17 -0800981 WARN_ON(1);
982 /* XXX TBD: send reject? */
983 fc_frame_free(fp);
984 return;
985 }
986 wwpn = get_unaligned_be64(&pl->fl_wwpn);
987 wwnn = get_unaligned_be64(&pl->fl_wwnn);
988
989 /*
990 * If the session was just created, possibly due to the incoming PLOGI,
991 * set the state appropriately and accept the PLOGI.
992 *
993 * If we had also sent a PLOGI, and if the received PLOGI is from a
994 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
995 * "command already in progress".
996 *
997 * XXX TBD: If the session was ready before, the PLOGI should result in
998 * all outstanding exchanges being reset.
999 */
1000 switch (rdata->rp_state) {
1001 case RPORT_ST_INIT:
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001002 FC_RPORT_DBG(rdata, "Received PLOGI, wwpn %llx state INIT "
Robert Love74147052009-06-10 15:31:10 -07001003 "- reject\n", (unsigned long long)wwpn);
Robert Love42e9a922008-12-09 15:10:17 -08001004 reject = ELS_RJT_UNSUP;
1005 break;
1006 case RPORT_ST_PLOGI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001007 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state %d\n",
Robert Love74147052009-06-10 15:31:10 -07001008 rdata->rp_state);
Robert Love42e9a922008-12-09 15:10:17 -08001009 if (wwpn < lport->wwpn)
1010 reject = ELS_RJT_INPROG;
1011 break;
1012 case RPORT_ST_PRLI:
1013 case RPORT_ST_READY:
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001014 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
Robert Love74147052009-06-10 15:31:10 -07001015 "- ignored for now\n", rdata->rp_state);
Robert Love42e9a922008-12-09 15:10:17 -08001016 /* XXX TBD - should reset */
1017 break;
Joe Eykholt14194052009-07-29 17:04:43 -07001018 case RPORT_ST_DELETE:
Robert Love42e9a922008-12-09 15:10:17 -08001019 default:
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001020 FC_RPORT_DBG(rdata, "Received PLOGI in unexpected "
Robert Love74147052009-06-10 15:31:10 -07001021 "state %d\n", rdata->rp_state);
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -07001022 fc_frame_free(fp);
1023 return;
Robert Love42e9a922008-12-09 15:10:17 -08001024 break;
1025 }
1026
1027 if (reject) {
1028 rjt_data.reason = reject;
1029 rjt_data.explan = ELS_EXPL_NONE;
1030 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1031 fc_frame_free(fp);
1032 } else {
1033 fp = fc_frame_alloc(lport, sizeof(*pl));
1034 if (fp == NULL) {
1035 fp = rx_fp;
1036 rjt_data.reason = ELS_RJT_UNAB;
1037 rjt_data.explan = ELS_EXPL_NONE;
1038 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1039 fc_frame_free(fp);
1040 } else {
1041 sp = lport->tt.seq_start_next(sp);
1042 WARN_ON(!sp);
Joe Eykholtf211fa52009-08-25 14:01:01 -07001043 rdata->ids.port_name = wwpn;
1044 rdata->ids.node_name = wwnn;
Robert Love42e9a922008-12-09 15:10:17 -08001045
1046 /*
1047 * Get session payload size from incoming PLOGI.
1048 */
Joe Eykholtf211fa52009-08-25 14:01:01 -07001049 rdata->maxframe_size =
Robert Love42e9a922008-12-09 15:10:17 -08001050 fc_plogi_get_maxframe(pl, lport->mfs);
1051 fc_frame_free(rx_fp);
1052 fc_plogi_fill(lport, fp, ELS_LS_ACC);
1053
1054 /*
1055 * Send LS_ACC. If this fails,
1056 * the originator should retry.
1057 */
1058 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1059 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1060 ep = fc_seq_exch(sp);
1061 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1062 FC_TYPE_ELS, f_ctl, 0);
1063 lport->tt.seq_send(lport, sp, fp);
1064 if (rdata->rp_state == RPORT_ST_PLOGI)
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001065 fc_rport_enter_prli(rdata);
Robert Love42e9a922008-12-09 15:10:17 -08001066 }
1067 }
1068}
1069
1070/**
Robert Love34f42a02009-02-27 10:55:45 -08001071 * fc_rport_recv_prli_req() - Handle incoming Process Login (PRLI) request
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001072 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -08001073 * @sp: current sequence in the PRLI exchange
1074 * @fp: PRLI request frame
1075 *
1076 * Locking Note: The rport lock is exected to be held before calling
1077 * this function.
1078 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001079static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -08001080 struct fc_seq *sp, struct fc_frame *rx_fp)
1081{
Robert Love42e9a922008-12-09 15:10:17 -08001082 struct fc_lport *lport = rdata->local_port;
1083 struct fc_exch *ep;
1084 struct fc_frame *fp;
1085 struct fc_frame_header *fh;
1086 struct {
1087 struct fc_els_prli prli;
1088 struct fc_els_spp spp;
1089 } *pp;
1090 struct fc_els_spp *rspp; /* request service param page */
1091 struct fc_els_spp *spp; /* response spp */
1092 unsigned int len;
1093 unsigned int plen;
1094 enum fc_els_rjt_reason reason = ELS_RJT_UNAB;
1095 enum fc_els_rjt_explan explan = ELS_EXPL_NONE;
1096 enum fc_els_spp_resp resp;
1097 struct fc_seq_els_data rjt_data;
1098 u32 f_ctl;
1099 u32 fcp_parm;
1100 u32 roles = FC_RPORT_ROLE_UNKNOWN;
1101 rjt_data.fp = NULL;
1102
1103 fh = fc_frame_header_get(rx_fp);
1104
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001105 FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1106 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001107
1108 switch (rdata->rp_state) {
1109 case RPORT_ST_PRLI:
1110 case RPORT_ST_READY:
1111 reason = ELS_RJT_NONE;
1112 break;
1113 default:
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -07001114 fc_frame_free(rx_fp);
1115 return;
Robert Love42e9a922008-12-09 15:10:17 -08001116 break;
1117 }
1118 len = fr_len(rx_fp) - sizeof(*fh);
1119 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1120 if (pp == NULL) {
1121 reason = ELS_RJT_PROT;
1122 explan = ELS_EXPL_INV_LEN;
1123 } else {
1124 plen = ntohs(pp->prli.prli_len);
1125 if ((plen % 4) != 0 || plen > len) {
1126 reason = ELS_RJT_PROT;
1127 explan = ELS_EXPL_INV_LEN;
1128 } else if (plen < len) {
1129 len = plen;
1130 }
1131 plen = pp->prli.prli_spp_len;
1132 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1133 plen > len || len < sizeof(*pp)) {
1134 reason = ELS_RJT_PROT;
1135 explan = ELS_EXPL_INV_LEN;
1136 }
1137 rspp = &pp->spp;
1138 }
1139 if (reason != ELS_RJT_NONE ||
1140 (fp = fc_frame_alloc(lport, len)) == NULL) {
1141 rjt_data.reason = reason;
1142 rjt_data.explan = explan;
1143 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1144 } else {
1145 sp = lport->tt.seq_start_next(sp);
1146 WARN_ON(!sp);
1147 pp = fc_frame_payload_get(fp, len);
1148 WARN_ON(!pp);
1149 memset(pp, 0, len);
1150 pp->prli.prli_cmd = ELS_LS_ACC;
1151 pp->prli.prli_spp_len = plen;
1152 pp->prli.prli_len = htons(len);
1153 len -= sizeof(struct fc_els_prli);
1154
1155 /*
1156 * Go through all the service parameter pages and build
1157 * response. If plen indicates longer SPP than standard,
1158 * use that. The entire response has been pre-cleared above.
1159 */
1160 spp = &pp->spp;
1161 while (len >= plen) {
1162 spp->spp_type = rspp->spp_type;
1163 spp->spp_type_ext = rspp->spp_type_ext;
1164 spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
1165 resp = FC_SPP_RESP_ACK;
1166 if (rspp->spp_flags & FC_SPP_RPA_VAL)
1167 resp = FC_SPP_RESP_NO_PA;
1168 switch (rspp->spp_type) {
1169 case 0: /* common to all FC-4 types */
1170 break;
1171 case FC_TYPE_FCP:
1172 fcp_parm = ntohl(rspp->spp_params);
1173 if (fcp_parm * FCP_SPPF_RETRY)
1174 rdata->flags |= FC_RP_FLAGS_RETRY;
Joe Eykholtf211fa52009-08-25 14:01:01 -07001175 rdata->supported_classes = FC_COS_CLASS3;
Robert Love42e9a922008-12-09 15:10:17 -08001176 if (fcp_parm & FCP_SPPF_INIT_FCN)
1177 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1178 if (fcp_parm & FCP_SPPF_TARG_FCN)
1179 roles |= FC_RPORT_ROLE_FCP_TARGET;
Joe Eykholtf211fa52009-08-25 14:01:01 -07001180 rdata->ids.roles = roles;
Robert Love42e9a922008-12-09 15:10:17 -08001181
1182 spp->spp_params =
1183 htonl(lport->service_params);
1184 break;
1185 default:
1186 resp = FC_SPP_RESP_INVL;
1187 break;
1188 }
1189 spp->spp_flags |= resp;
1190 len -= plen;
1191 rspp = (struct fc_els_spp *)((char *)rspp + plen);
1192 spp = (struct fc_els_spp *)((char *)spp + plen);
1193 }
1194
1195 /*
1196 * Send LS_ACC. If this fails, the originator should retry.
1197 */
1198 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1199 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1200 ep = fc_seq_exch(sp);
1201 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1202 FC_TYPE_ELS, f_ctl, 0);
1203 lport->tt.seq_send(lport, sp, fp);
1204
1205 /*
1206 * Get lock and re-check state.
1207 */
1208 switch (rdata->rp_state) {
1209 case RPORT_ST_PRLI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001210 fc_rport_enter_ready(rdata);
Robert Love42e9a922008-12-09 15:10:17 -08001211 break;
1212 case RPORT_ST_READY:
1213 break;
1214 default:
1215 break;
1216 }
1217 }
1218 fc_frame_free(rx_fp);
1219}
1220
1221/**
Robert Love34f42a02009-02-27 10:55:45 -08001222 * fc_rport_recv_prlo_req() - Handle incoming Process Logout (PRLO) request
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001223 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -08001224 * @sp: current sequence in the PRLO exchange
1225 * @fp: PRLO request frame
1226 *
1227 * Locking Note: The rport lock is exected to be held before calling
1228 * this function.
1229 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001230static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
1231 struct fc_seq *sp,
Robert Love42e9a922008-12-09 15:10:17 -08001232 struct fc_frame *fp)
1233{
Robert Love42e9a922008-12-09 15:10:17 -08001234 struct fc_lport *lport = rdata->local_port;
1235
1236 struct fc_frame_header *fh;
1237 struct fc_seq_els_data rjt_data;
1238
1239 fh = fc_frame_header_get(fp);
1240
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001241 FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
1242 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001243
Joe Eykholt14194052009-07-29 17:04:43 -07001244 if (rdata->rp_state == RPORT_ST_DELETE) {
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -07001245 fc_frame_free(fp);
1246 return;
1247 }
1248
Robert Love42e9a922008-12-09 15:10:17 -08001249 rjt_data.fp = NULL;
1250 rjt_data.reason = ELS_RJT_UNAB;
1251 rjt_data.explan = ELS_EXPL_NONE;
1252 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1253 fc_frame_free(fp);
1254}
1255
1256/**
Robert Love34f42a02009-02-27 10:55:45 -08001257 * fc_rport_recv_logo_req() - Handle incoming Logout (LOGO) request
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001258 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -08001259 * @sp: current sequence in the LOGO exchange
1260 * @fp: LOGO request frame
1261 *
1262 * Locking Note: The rport lock is exected to be held before calling
1263 * this function.
1264 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001265static void fc_rport_recv_logo_req(struct fc_rport_priv *rdata,
1266 struct fc_seq *sp,
Robert Love42e9a922008-12-09 15:10:17 -08001267 struct fc_frame *fp)
1268{
1269 struct fc_frame_header *fh;
Robert Love42e9a922008-12-09 15:10:17 -08001270 struct fc_lport *lport = rdata->local_port;
1271
1272 fh = fc_frame_header_get(fp);
1273
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001274 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
1275 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001276
Joe Eykholt14194052009-07-29 17:04:43 -07001277 if (rdata->rp_state == RPORT_ST_DELETE) {
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -07001278 fc_frame_free(fp);
1279 return;
1280 }
1281
Joe Eykholt00fea932009-08-25 14:01:23 -07001282 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
Robert Love42e9a922008-12-09 15:10:17 -08001283
1284 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
1285 fc_frame_free(fp);
1286}
1287
1288static void fc_rport_flush_queue(void)
1289{
1290 flush_workqueue(rport_event_queue);
1291}
1292
Robert Love42e9a922008-12-09 15:10:17 -08001293int fc_rport_init(struct fc_lport *lport)
1294{
Robert Love5101ff92009-02-27 10:55:18 -08001295 if (!lport->tt.rport_create)
Joe Eykholt9e9d0452009-08-25 14:01:18 -07001296 lport->tt.rport_create = fc_rport_create;
Robert Love5101ff92009-02-27 10:55:18 -08001297
Robert Love42e9a922008-12-09 15:10:17 -08001298 if (!lport->tt.rport_login)
1299 lport->tt.rport_login = fc_rport_login;
1300
1301 if (!lport->tt.rport_logoff)
1302 lport->tt.rport_logoff = fc_rport_logoff;
1303
1304 if (!lport->tt.rport_recv_req)
1305 lport->tt.rport_recv_req = fc_rport_recv_req;
1306
1307 if (!lport->tt.rport_flush_queue)
1308 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1309
Joe Eykholtf211fa52009-08-25 14:01:01 -07001310 if (!lport->tt.rport_destroy)
1311 lport->tt.rport_destroy = fc_rport_destroy;
1312
Robert Love42e9a922008-12-09 15:10:17 -08001313 return 0;
1314}
1315EXPORT_SYMBOL(fc_rport_init);
1316
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001317int fc_setup_rport(void)
Robert Love42e9a922008-12-09 15:10:17 -08001318{
1319 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
1320 if (!rport_event_queue)
1321 return -ENOMEM;
1322 return 0;
1323}
1324EXPORT_SYMBOL(fc_setup_rport);
1325
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001326void fc_destroy_rport(void)
Robert Love42e9a922008-12-09 15:10:17 -08001327{
1328 destroy_workqueue(rport_event_queue);
1329}
1330EXPORT_SYMBOL(fc_destroy_rport);
1331
1332void fc_rport_terminate_io(struct fc_rport *rport)
1333{
Joe Eykholtab28f1f2009-08-25 14:00:34 -07001334 struct fc_rport_libfc_priv *rp = rport->dd_data;
1335 struct fc_lport *lport = rp->local_port;
Robert Love42e9a922008-12-09 15:10:17 -08001336
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -08001337 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
1338 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
Robert Love42e9a922008-12-09 15:10:17 -08001339}
1340EXPORT_SYMBOL(fc_rport_terminate_io);