blob: cbf6c9f233cae65eefe320feeeaef7bdeffd5c07 [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 *
94 * Locking note: this may be called without locks held, but
95 * is usually called from discovery with the disc_mutex held.
96 */
97static struct fc_rport_priv *fc_rport_create(struct fc_lport *lport,
98 struct fc_rport_identifiers *ids)
Robert Love42e9a922008-12-09 15:10:17 -080099{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700100 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800101
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700102 rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
103 if (!rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800104 return NULL;
105
Joe Eykholtf211fa52009-08-25 14:01:01 -0700106 rdata->ids = *ids;
107 kref_init(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800108 mutex_init(&rdata->rp_mutex);
Joe Eykholt795d86f2009-08-25 14:00:39 -0700109 rdata->local_port = lport;
Robert Love42e9a922008-12-09 15:10:17 -0800110 rdata->rp_state = RPORT_ST_INIT;
111 rdata->event = RPORT_EV_NONE;
112 rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
Joe Eykholt795d86f2009-08-25 14:00:39 -0700113 rdata->e_d_tov = lport->e_d_tov;
114 rdata->r_a_tov = lport->r_a_tov;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700115 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
Robert Love42e9a922008-12-09 15:10:17 -0800116 INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
117 INIT_WORK(&rdata->event_work, fc_rport_work);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700118 return rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800119}
120
121/**
Joe Eykholtf211fa52009-08-25 14:01:01 -0700122 * fc_rport_destroy() - free a remote port after last reference is released.
123 * @kref: pointer to kref inside struct fc_rport_priv
124 */
125static void fc_rport_destroy(struct kref *kref)
126{
127 struct fc_rport_priv *rdata;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700128
129 rdata = container_of(kref, struct fc_rport_priv, kref);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700130 kfree(rdata);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700131}
132
133/**
Robert Love34f42a02009-02-27 10:55:45 -0800134 * fc_rport_state() - return a string for the state the rport is in
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700135 * @rdata: remote port private data
Robert Love42e9a922008-12-09 15:10:17 -0800136 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700137static const char *fc_rport_state(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800138{
139 const char *cp;
Robert Love42e9a922008-12-09 15:10:17 -0800140
141 cp = fc_rport_state_names[rdata->rp_state];
142 if (!cp)
143 cp = "Unknown";
144 return cp;
145}
146
147/**
Robert Love34f42a02009-02-27 10:55:45 -0800148 * fc_set_rport_loss_tmo() - Set the remote port loss timeout in seconds.
Robert Love42e9a922008-12-09 15:10:17 -0800149 * @rport: Pointer to Fibre Channel remote port structure
150 * @timeout: timeout in seconds
151 */
152void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
153{
154 if (timeout)
155 rport->dev_loss_tmo = timeout + 5;
156 else
157 rport->dev_loss_tmo = 30;
158}
159EXPORT_SYMBOL(fc_set_rport_loss_tmo);
160
161/**
Robert Love34f42a02009-02-27 10:55:45 -0800162 * fc_plogi_get_maxframe() - Get max payload from the common service parameters
Robert Love42e9a922008-12-09 15:10:17 -0800163 * @flp: FLOGI payload structure
164 * @maxval: upper limit, may be less than what is in the service parameters
165 */
Robert Loveb2ab99c2009-02-27 10:55:50 -0800166static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
167 unsigned int maxval)
Robert Love42e9a922008-12-09 15:10:17 -0800168{
169 unsigned int mfs;
170
171 /*
172 * Get max payload from the common service parameters and the
173 * class 3 receive data field size.
174 */
175 mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
176 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
177 maxval = mfs;
178 mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
179 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
180 maxval = mfs;
181 return maxval;
182}
183
184/**
Robert Love34f42a02009-02-27 10:55:45 -0800185 * fc_rport_state_enter() - Change the rport's state
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700186 * @rdata: The rport whose state should change
Robert Love42e9a922008-12-09 15:10:17 -0800187 * @new: The new state of the rport
188 *
189 * Locking Note: Called with the rport lock held
190 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700191static void fc_rport_state_enter(struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -0800192 enum fc_rport_state new)
193{
Robert Love42e9a922008-12-09 15:10:17 -0800194 if (rdata->rp_state != new)
195 rdata->retries = 0;
196 rdata->rp_state = new;
197}
198
199static void fc_rport_work(struct work_struct *work)
200{
Abhijeet Joglekar571f8242009-02-27 10:54:41 -0800201 u32 port_id;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700202 struct fc_rport_priv *rdata =
203 container_of(work, struct fc_rport_priv, event_work);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700204 struct fc_rport_libfc_priv *rp;
Robert Love42e9a922008-12-09 15:10:17 -0800205 enum fc_rport_event event;
Robert Love42e9a922008-12-09 15:10:17 -0800206 struct fc_lport *lport = rdata->local_port;
207 struct fc_rport_operations *rport_ops;
Joe Eykholt629f4422009-08-25 14:01:06 -0700208 struct fc_rport_identifiers ids;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700209 struct fc_rport *rport;
Robert Love42e9a922008-12-09 15:10:17 -0800210
211 mutex_lock(&rdata->rp_mutex);
212 event = rdata->event;
213 rport_ops = rdata->ops;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700214 rport = rdata->rport;
Robert Love42e9a922008-12-09 15:10:17 -0800215
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700216 FC_RPORT_DBG(rdata, "work event %u\n", event);
217
Joe Eykholt629f4422009-08-25 14:01:06 -0700218 switch (event) {
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700219 case RPORT_EV_READY:
Joe Eykholtf211fa52009-08-25 14:01:01 -0700220 ids = rdata->ids;
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700221 rdata->event = RPORT_EV_NONE;
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700222 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800223 mutex_unlock(&rdata->rp_mutex);
224
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700225 if (!rport)
226 rport = fc_remote_port_add(lport->host, 0, &ids);
227 if (!rport) {
228 FC_RPORT_DBG(rdata, "Failed to add the rport\n");
229 lport->tt.rport_logoff(rdata);
230 kref_put(&rdata->kref, lport->tt.rport_destroy);
231 return;
Robert Love42e9a922008-12-09 15:10:17 -0800232 }
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700233 mutex_lock(&rdata->rp_mutex);
234 if (rdata->rport)
235 FC_RPORT_DBG(rdata, "rport already allocated\n");
236 rdata->rport = rport;
237 rport->maxframe_size = rdata->maxframe_size;
238 rport->supported_classes = rdata->supported_classes;
239
240 rp = rport->dd_data;
241 rp->local_port = lport;
242 rp->rp_state = rdata->rp_state;
243 rp->flags = rdata->flags;
244 rp->e_d_tov = rdata->e_d_tov;
245 rp->r_a_tov = rdata->r_a_tov;
246 mutex_unlock(&rdata->rp_mutex);
247
248 if (rport_ops->event_callback) {
249 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700250 rport_ops->event_callback(lport, rdata, event);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700251 }
252 kref_put(&rdata->kref, lport->tt.rport_destroy);
Joe Eykholt629f4422009-08-25 14:01:06 -0700253 break;
254
255 case RPORT_EV_FAILED:
256 case RPORT_EV_LOGO:
257 case RPORT_EV_STOP:
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700258 port_id = rdata->ids.port_id;
Robert Love42e9a922008-12-09 15:10:17 -0800259 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700260
261 if (rport_ops->event_callback) {
262 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700263 rport_ops->event_callback(lport, rdata, event);
Abhijeet Joglekar571f8242009-02-27 10:54:41 -0800264 }
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700265 cancel_delayed_work_sync(&rdata->retry_work);
266
267 /*
268 * Reset any outstanding exchanges before freeing rport.
269 */
270 lport->tt.exch_mgr_reset(lport, 0, port_id);
271 lport->tt.exch_mgr_reset(lport, port_id, 0);
272
273 if (rport) {
274 rp = rport->dd_data;
275 rp->rp_state = RPORT_ST_DELETE;
276 mutex_lock(&rdata->rp_mutex);
277 rdata->rport = NULL;
278 mutex_unlock(&rdata->rp_mutex);
279 fc_remote_port_delete(rport);
280 }
281 kref_put(&rdata->kref, lport->tt.rport_destroy);
Joe Eykholt629f4422009-08-25 14:01:06 -0700282 break;
283
284 default:
Robert Love42e9a922008-12-09 15:10:17 -0800285 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt629f4422009-08-25 14:01:06 -0700286 break;
287 }
Robert Love42e9a922008-12-09 15:10:17 -0800288}
289
290/**
Robert Love34f42a02009-02-27 10:55:45 -0800291 * fc_rport_login() - Start the remote port login state machine
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700292 * @rdata: private remote port
Robert Love42e9a922008-12-09 15:10:17 -0800293 *
294 * Locking Note: Called without the rport lock held. This
295 * function will hold the rport lock, call an _enter_*
296 * function and then unlock the rport.
297 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700298int fc_rport_login(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800299{
Robert Love42e9a922008-12-09 15:10:17 -0800300 mutex_lock(&rdata->rp_mutex);
301
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700302 FC_RPORT_DBG(rdata, "Login to port\n");
Robert Love42e9a922008-12-09 15:10:17 -0800303
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700304 fc_rport_enter_plogi(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800305
306 mutex_unlock(&rdata->rp_mutex);
307
308 return 0;
309}
310
311/**
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700312 * fc_rport_enter_delete() - schedule a remote port to be deleted.
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700313 * @rdata: private remote port
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700314 * @event: event to report as the reason for deletion
315 *
316 * Locking Note: Called with the rport lock held.
317 *
318 * Allow state change into DELETE only once.
319 *
320 * Call queue_work only if there's no event already pending.
321 * Set the new event so that the old pending event will not occur.
322 * Since we have the mutex, even if fc_rport_work() is already started,
323 * it'll see the new event.
324 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700325static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700326 enum fc_rport_event event)
327{
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700328 if (rdata->rp_state == RPORT_ST_DELETE)
329 return;
330
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700331 FC_RPORT_DBG(rdata, "Delete port\n");
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700332
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700333 fc_rport_state_enter(rdata, RPORT_ST_DELETE);
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700334
335 if (rdata->event == RPORT_EV_NONE)
336 queue_work(rport_event_queue, &rdata->event_work);
337 rdata->event = event;
338}
339
340/**
Robert Love34f42a02009-02-27 10:55:45 -0800341 * fc_rport_logoff() - Logoff and remove an rport
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700342 * @rdata: private remote port
Robert Love42e9a922008-12-09 15:10:17 -0800343 *
344 * Locking Note: Called without the rport lock held. This
345 * function will hold the rport lock, call an _enter_*
346 * function and then unlock the rport.
347 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700348int fc_rport_logoff(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800349{
Robert Love42e9a922008-12-09 15:10:17 -0800350 mutex_lock(&rdata->rp_mutex);
351
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700352 FC_RPORT_DBG(rdata, "Remove port\n");
Robert Love42e9a922008-12-09 15:10:17 -0800353
Joe Eykholt14194052009-07-29 17:04:43 -0700354 if (rdata->rp_state == RPORT_ST_DELETE) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700355 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700356 mutex_unlock(&rdata->rp_mutex);
357 goto out;
358 }
359
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700360 fc_rport_enter_logo(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800361
362 /*
Joe Eykholt14194052009-07-29 17:04:43 -0700363 * Change the state to Delete so that we discard
Robert Love42e9a922008-12-09 15:10:17 -0800364 * the response.
365 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700366 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
Robert Love42e9a922008-12-09 15:10:17 -0800367 mutex_unlock(&rdata->rp_mutex);
368
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700369out:
Robert Love42e9a922008-12-09 15:10:17 -0800370 return 0;
371}
372
373/**
Robert Love34f42a02009-02-27 10:55:45 -0800374 * fc_rport_enter_ready() - The rport is ready
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700375 * @rdata: private remote port
Robert Love42e9a922008-12-09 15:10:17 -0800376 *
377 * Locking Note: The rport lock is expected to be held before calling
378 * this routine.
379 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700380static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800381{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700382 fc_rport_state_enter(rdata, RPORT_ST_READY);
Robert Love42e9a922008-12-09 15:10:17 -0800383
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700384 FC_RPORT_DBG(rdata, "Port is Ready\n");
Robert Love42e9a922008-12-09 15:10:17 -0800385
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700386 if (rdata->event == RPORT_EV_NONE)
387 queue_work(rport_event_queue, &rdata->event_work);
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700388 rdata->event = RPORT_EV_READY;
Robert Love42e9a922008-12-09 15:10:17 -0800389}
390
391/**
Robert Love34f42a02009-02-27 10:55:45 -0800392 * fc_rport_timeout() - Handler for the retry_work timer.
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700393 * @work: The work struct of the fc_rport_priv
Robert Love42e9a922008-12-09 15:10:17 -0800394 *
395 * Locking Note: Called without the rport lock held. This
396 * function will hold the rport lock, call an _enter_*
397 * function and then unlock the rport.
398 */
399static void fc_rport_timeout(struct work_struct *work)
400{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700401 struct fc_rport_priv *rdata =
402 container_of(work, struct fc_rport_priv, retry_work.work);
Robert Love42e9a922008-12-09 15:10:17 -0800403
404 mutex_lock(&rdata->rp_mutex);
405
406 switch (rdata->rp_state) {
407 case RPORT_ST_PLOGI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700408 fc_rport_enter_plogi(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800409 break;
410 case RPORT_ST_PRLI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700411 fc_rport_enter_prli(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800412 break;
413 case RPORT_ST_RTV:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700414 fc_rport_enter_rtv(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800415 break;
416 case RPORT_ST_LOGO:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700417 fc_rport_enter_logo(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800418 break;
419 case RPORT_ST_READY:
420 case RPORT_ST_INIT:
Joe Eykholt14194052009-07-29 17:04:43 -0700421 case RPORT_ST_DELETE:
Robert Love42e9a922008-12-09 15:10:17 -0800422 break;
423 }
424
425 mutex_unlock(&rdata->rp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800426}
427
428/**
Robert Love34f42a02009-02-27 10:55:45 -0800429 * fc_rport_error() - Error handler, called once retries have been exhausted
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700430 * @rdata: private remote port
Robert Love42e9a922008-12-09 15:10:17 -0800431 * @fp: The frame pointer
432 *
Robert Love42e9a922008-12-09 15:10:17 -0800433 * Locking Note: The rport lock is expected to be held before
434 * calling this routine
435 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700436static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -0800437{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700438 FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
439 PTR_ERR(fp), fc_rport_state(rdata), rdata->retries);
Robert Love42e9a922008-12-09 15:10:17 -0800440
Chris Leech6755db12009-02-27 10:55:02 -0800441 switch (rdata->rp_state) {
442 case RPORT_ST_PLOGI:
443 case RPORT_ST_PRLI:
444 case RPORT_ST_LOGO:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700445 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
Chris Leech6755db12009-02-27 10:55:02 -0800446 break;
447 case RPORT_ST_RTV:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700448 fc_rport_enter_ready(rdata);
Chris Leech6755db12009-02-27 10:55:02 -0800449 break;
Joe Eykholt14194052009-07-29 17:04:43 -0700450 case RPORT_ST_DELETE:
Chris Leech6755db12009-02-27 10:55:02 -0800451 case RPORT_ST_READY:
452 case RPORT_ST_INIT:
453 break;
Robert Love42e9a922008-12-09 15:10:17 -0800454 }
455}
456
457/**
Robert Love34f42a02009-02-27 10:55:45 -0800458 * fc_rport_error_retry() - Error handler when retries are desired
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700459 * @rdata: private remote port data
Chris Leech6755db12009-02-27 10:55:02 -0800460 * @fp: The frame pointer
461 *
462 * If the error was an exchange timeout retry immediately,
463 * otherwise wait for E_D_TOV.
464 *
465 * Locking Note: The rport lock is expected to be held before
466 * calling this routine
467 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700468static void fc_rport_error_retry(struct fc_rport_priv *rdata,
469 struct fc_frame *fp)
Chris Leech6755db12009-02-27 10:55:02 -0800470{
Chris Leech6755db12009-02-27 10:55:02 -0800471 unsigned long delay = FC_DEF_E_D_TOV;
472
473 /* make sure this isn't an FC_EX_CLOSED error, never retry those */
474 if (PTR_ERR(fp) == -FC_EX_CLOSED)
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700475 return fc_rport_error(rdata, fp);
Chris Leech6755db12009-02-27 10:55:02 -0800476
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700477 if (rdata->retries < rdata->local_port->max_rport_retry_count) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700478 FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
479 PTR_ERR(fp), fc_rport_state(rdata));
Chris Leech6755db12009-02-27 10:55:02 -0800480 rdata->retries++;
481 /* no additional delay on exchange timeouts */
482 if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
483 delay = 0;
Chris Leech6755db12009-02-27 10:55:02 -0800484 schedule_delayed_work(&rdata->retry_work, delay);
485 return;
486 }
487
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700488 return fc_rport_error(rdata, fp);
Chris Leech6755db12009-02-27 10:55:02 -0800489}
490
491/**
Robert Love34f42a02009-02-27 10:55:45 -0800492 * fc_rport_plogi_recv_resp() - Handle incoming ELS PLOGI response
Robert Love42e9a922008-12-09 15:10:17 -0800493 * @sp: current sequence in the PLOGI exchange
494 * @fp: response frame
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700495 * @rdata_arg: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800496 *
497 * Locking Note: This function will be called without the rport lock
498 * held, but it will lock, call an _enter_* function or fc_rport_error
499 * and then unlock the rport.
500 */
501static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700502 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800503{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700504 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800505 struct fc_lport *lport = rdata->local_port;
Robert Lovea29e7642009-04-21 16:27:41 -0700506 struct fc_els_flogi *plp = NULL;
Robert Love42e9a922008-12-09 15:10:17 -0800507 unsigned int tov;
508 u16 csp_seq;
509 u16 cssp_seq;
510 u8 op;
511
512 mutex_lock(&rdata->rp_mutex);
513
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700514 FC_RPORT_DBG(rdata, "Received a PLOGI response\n");
Robert Love42e9a922008-12-09 15:10:17 -0800515
516 if (rdata->rp_state != RPORT_ST_PLOGI) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700517 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
518 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700519 if (IS_ERR(fp))
520 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800521 goto out;
522 }
523
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700524 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700525 fc_rport_error_retry(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700526 goto err;
527 }
528
Robert Love42e9a922008-12-09 15:10:17 -0800529 op = fc_frame_payload_op(fp);
530 if (op == ELS_LS_ACC &&
531 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
Joe Eykholtf211fa52009-08-25 14:01:01 -0700532 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
533 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
Robert Love42e9a922008-12-09 15:10:17 -0800534
535 tov = ntohl(plp->fl_csp.sp_e_d_tov);
536 if (ntohs(plp->fl_csp.sp_features) & FC_SP_FT_EDTR)
537 tov /= 1000;
538 if (tov > rdata->e_d_tov)
539 rdata->e_d_tov = tov;
540 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
541 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
542 if (cssp_seq < csp_seq)
543 csp_seq = cssp_seq;
544 rdata->max_seq = csp_seq;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700545 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
Robert Love42e9a922008-12-09 15:10:17 -0800546
547 /*
548 * If the rport is one of the well known addresses
549 * we skip PRLI and RTV and go straight to READY.
550 */
Joe Eykholtf211fa52009-08-25 14:01:01 -0700551 if (rdata->ids.port_id >= FC_FID_DOM_MGR)
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700552 fc_rport_enter_ready(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800553 else
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700554 fc_rport_enter_prli(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800555 } else
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700556 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800557
558out:
559 fc_frame_free(fp);
560err:
561 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700562 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800563}
564
565/**
Robert Love34f42a02009-02-27 10:55:45 -0800566 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request to peer
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700567 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800568 *
569 * Locking Note: The rport lock is expected to be held before calling
570 * this routine.
571 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700572static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800573{
Robert Love42e9a922008-12-09 15:10:17 -0800574 struct fc_lport *lport = rdata->local_port;
575 struct fc_frame *fp;
576
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700577 FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
578 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800579
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700580 fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
Robert Love42e9a922008-12-09 15:10:17 -0800581
Joe Eykholtf211fa52009-08-25 14:01:01 -0700582 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
Robert Love42e9a922008-12-09 15:10:17 -0800583 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
584 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700585 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800586 return;
587 }
588 rdata->e_d_tov = lport->e_d_tov;
589
Joe Eykholtf211fa52009-08-25 14:01:01 -0700590 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700591 fc_rport_plogi_resp, rdata, lport->e_d_tov))
592 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800593 else
Joe Eykholtf211fa52009-08-25 14:01:01 -0700594 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800595}
596
597/**
Robert Love34f42a02009-02-27 10:55:45 -0800598 * fc_rport_prli_resp() - Process Login (PRLI) response handler
Robert Love42e9a922008-12-09 15:10:17 -0800599 * @sp: current sequence in the PRLI exchange
600 * @fp: response frame
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700601 * @rdata_arg: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800602 *
603 * Locking Note: This function will be called without the rport lock
604 * held, but it will lock, call an _enter_* function or fc_rport_error
605 * and then unlock the rport.
606 */
607static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700608 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800609{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700610 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800611 struct {
612 struct fc_els_prli prli;
613 struct fc_els_spp spp;
614 } *pp;
615 u32 roles = FC_RPORT_ROLE_UNKNOWN;
616 u32 fcp_parm = 0;
617 u8 op;
618
619 mutex_lock(&rdata->rp_mutex);
620
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700621 FC_RPORT_DBG(rdata, "Received a PRLI response\n");
Robert Love42e9a922008-12-09 15:10:17 -0800622
623 if (rdata->rp_state != RPORT_ST_PRLI) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700624 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
625 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700626 if (IS_ERR(fp))
627 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800628 goto out;
629 }
630
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700631 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700632 fc_rport_error_retry(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700633 goto err;
634 }
635
Robert Love42e9a922008-12-09 15:10:17 -0800636 op = fc_frame_payload_op(fp);
637 if (op == ELS_LS_ACC) {
638 pp = fc_frame_payload_get(fp, sizeof(*pp));
639 if (pp && pp->prli.prli_spp_len >= sizeof(pp->spp)) {
640 fcp_parm = ntohl(pp->spp.spp_params);
641 if (fcp_parm & FCP_SPPF_RETRY)
642 rdata->flags |= FC_RP_FLAGS_RETRY;
643 }
644
Joe Eykholtf211fa52009-08-25 14:01:01 -0700645 rdata->supported_classes = FC_COS_CLASS3;
Robert Love42e9a922008-12-09 15:10:17 -0800646 if (fcp_parm & FCP_SPPF_INIT_FCN)
647 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
648 if (fcp_parm & FCP_SPPF_TARG_FCN)
649 roles |= FC_RPORT_ROLE_FCP_TARGET;
650
Joe Eykholtf211fa52009-08-25 14:01:01 -0700651 rdata->ids.roles = roles;
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700652 fc_rport_enter_rtv(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800653
654 } else {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700655 FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
656 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
Robert Love42e9a922008-12-09 15:10:17 -0800657 }
658
659out:
660 fc_frame_free(fp);
661err:
662 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700663 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800664}
665
666/**
Robert Love34f42a02009-02-27 10:55:45 -0800667 * fc_rport_logo_resp() - Logout (LOGO) response handler
Robert Love42e9a922008-12-09 15:10:17 -0800668 * @sp: current sequence in the LOGO exchange
669 * @fp: response frame
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700670 * @rdata_arg: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800671 *
672 * Locking Note: This function will be called without the rport lock
673 * held, but it will lock, call an _enter_* function or fc_rport_error
674 * and then unlock the rport.
675 */
676static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700677 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800678{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700679 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800680 u8 op;
681
682 mutex_lock(&rdata->rp_mutex);
683
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700684 FC_RPORT_DBG(rdata, "Received a LOGO response\n");
Robert Love42e9a922008-12-09 15:10:17 -0800685
Robert Love42e9a922008-12-09 15:10:17 -0800686 if (rdata->rp_state != RPORT_ST_LOGO) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700687 FC_RPORT_DBG(rdata, "Received a LOGO response, but in state "
688 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700689 if (IS_ERR(fp))
690 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800691 goto out;
692 }
693
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700694 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700695 fc_rport_error_retry(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700696 goto err;
697 }
698
Robert Love42e9a922008-12-09 15:10:17 -0800699 op = fc_frame_payload_op(fp);
700 if (op == ELS_LS_ACC) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700701 fc_rport_enter_rtv(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800702 } else {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700703 FC_RPORT_DBG(rdata, "Bad ELS response for LOGO command\n");
704 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
Robert Love42e9a922008-12-09 15:10:17 -0800705 }
706
707out:
708 fc_frame_free(fp);
709err:
710 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700711 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800712}
713
714/**
Robert Love34f42a02009-02-27 10:55:45 -0800715 * fc_rport_enter_prli() - Send Process Login (PRLI) request to peer
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700716 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800717 *
718 * Locking Note: The rport lock is expected to be held before calling
719 * this routine.
720 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700721static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800722{
Robert Love42e9a922008-12-09 15:10:17 -0800723 struct fc_lport *lport = rdata->local_port;
724 struct {
725 struct fc_els_prli prli;
726 struct fc_els_spp spp;
727 } *pp;
728 struct fc_frame *fp;
729
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700730 FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
731 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800732
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700733 fc_rport_state_enter(rdata, RPORT_ST_PRLI);
Robert Love42e9a922008-12-09 15:10:17 -0800734
735 fp = fc_frame_alloc(lport, sizeof(*pp));
736 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700737 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800738 return;
739 }
740
Joe Eykholtf211fa52009-08-25 14:01:01 -0700741 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PRLI,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700742 fc_rport_prli_resp, rdata, lport->e_d_tov))
743 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800744 else
Joe Eykholtf211fa52009-08-25 14:01:01 -0700745 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800746}
747
748/**
Robert Love34f42a02009-02-27 10:55:45 -0800749 * fc_rport_els_rtv_resp() - Request Timeout Value response handler
Robert Love42e9a922008-12-09 15:10:17 -0800750 * @sp: current sequence in the RTV exchange
751 * @fp: response frame
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700752 * @rdata_arg: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800753 *
754 * Many targets don't seem to support this.
755 *
756 * Locking Note: This function will be called without the rport lock
757 * held, but it will lock, call an _enter_* function or fc_rport_error
758 * and then unlock the rport.
759 */
760static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700761 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800762{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700763 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800764 u8 op;
765
766 mutex_lock(&rdata->rp_mutex);
767
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700768 FC_RPORT_DBG(rdata, "Received a RTV response\n");
Robert Love42e9a922008-12-09 15:10:17 -0800769
770 if (rdata->rp_state != RPORT_ST_RTV) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700771 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
772 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700773 if (IS_ERR(fp))
774 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800775 goto out;
776 }
777
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700778 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700779 fc_rport_error(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700780 goto err;
781 }
782
Robert Love42e9a922008-12-09 15:10:17 -0800783 op = fc_frame_payload_op(fp);
784 if (op == ELS_LS_ACC) {
785 struct fc_els_rtv_acc *rtv;
786 u32 toq;
787 u32 tov;
788
789 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
790 if (rtv) {
791 toq = ntohl(rtv->rtv_toq);
792 tov = ntohl(rtv->rtv_r_a_tov);
793 if (tov == 0)
794 tov = 1;
795 rdata->r_a_tov = tov;
796 tov = ntohl(rtv->rtv_e_d_tov);
797 if (toq & FC_ELS_RTV_EDRES)
798 tov /= 1000000;
799 if (tov == 0)
800 tov = 1;
801 rdata->e_d_tov = tov;
802 }
803 }
804
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700805 fc_rport_enter_ready(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800806
807out:
808 fc_frame_free(fp);
809err:
810 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700811 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800812}
813
814/**
Robert Love34f42a02009-02-27 10:55:45 -0800815 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request to peer
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700816 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800817 *
818 * Locking Note: The rport lock is expected to be held before calling
819 * this routine.
820 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700821static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800822{
823 struct fc_frame *fp;
Robert Love42e9a922008-12-09 15:10:17 -0800824 struct fc_lport *lport = rdata->local_port;
825
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700826 FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
827 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800828
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700829 fc_rport_state_enter(rdata, RPORT_ST_RTV);
Robert Love42e9a922008-12-09 15:10:17 -0800830
831 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
832 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700833 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800834 return;
835 }
836
Joe Eykholtf211fa52009-08-25 14:01:01 -0700837 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700838 fc_rport_rtv_resp, rdata, lport->e_d_tov))
839 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800840 else
Joe Eykholtf211fa52009-08-25 14:01:01 -0700841 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800842}
843
844/**
Robert Love34f42a02009-02-27 10:55:45 -0800845 * fc_rport_enter_logo() - Send Logout (LOGO) request to peer
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700846 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800847 *
848 * Locking Note: The rport lock is expected to be held before calling
849 * this routine.
850 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700851static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800852{
Robert Love42e9a922008-12-09 15:10:17 -0800853 struct fc_lport *lport = rdata->local_port;
854 struct fc_frame *fp;
855
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700856 FC_RPORT_DBG(rdata, "Port entered LOGO state from %s state\n",
857 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800858
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700859 fc_rport_state_enter(rdata, RPORT_ST_LOGO);
Robert Love42e9a922008-12-09 15:10:17 -0800860
861 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
862 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700863 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800864 return;
865 }
866
Joe Eykholtf211fa52009-08-25 14:01:01 -0700867 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700868 fc_rport_logo_resp, rdata, lport->e_d_tov))
869 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800870 else
Joe Eykholtf211fa52009-08-25 14:01:01 -0700871 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800872}
873
874
875/**
Robert Love34f42a02009-02-27 10:55:45 -0800876 * fc_rport_recv_req() - Receive a request from a rport
Robert Love42e9a922008-12-09 15:10:17 -0800877 * @sp: current sequence in the PLOGI exchange
878 * @fp: response frame
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700879 * @rdata_arg: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800880 *
881 * Locking Note: Called without the rport lock held. This
882 * function will hold the rport lock, call an _enter_*
883 * function and then unlock the rport.
884 */
885void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700886 struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800887{
Robert Love42e9a922008-12-09 15:10:17 -0800888 struct fc_lport *lport = rdata->local_port;
889
890 struct fc_frame_header *fh;
891 struct fc_seq_els_data els_data;
892 u8 op;
893
894 mutex_lock(&rdata->rp_mutex);
895
896 els_data.fp = NULL;
897 els_data.explan = ELS_EXPL_NONE;
898 els_data.reason = ELS_RJT_NONE;
899
900 fh = fc_frame_header_get(fp);
901
902 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ && fh->fh_type == FC_TYPE_ELS) {
903 op = fc_frame_payload_op(fp);
904 switch (op) {
905 case ELS_PLOGI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700906 fc_rport_recv_plogi_req(rdata, sp, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800907 break;
908 case ELS_PRLI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700909 fc_rport_recv_prli_req(rdata, sp, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800910 break;
911 case ELS_PRLO:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700912 fc_rport_recv_prlo_req(rdata, sp, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800913 break;
914 case ELS_LOGO:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700915 fc_rport_recv_logo_req(rdata, sp, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800916 break;
917 case ELS_RRQ:
918 els_data.fp = fp;
919 lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
920 break;
921 case ELS_REC:
922 els_data.fp = fp;
923 lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
924 break;
925 default:
926 els_data.reason = ELS_RJT_UNSUP;
927 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
928 break;
929 }
930 }
931
932 mutex_unlock(&rdata->rp_mutex);
933}
934
935/**
Robert Love34f42a02009-02-27 10:55:45 -0800936 * fc_rport_recv_plogi_req() - Handle incoming Port Login (PLOGI) request
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700937 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800938 * @sp: current sequence in the PLOGI exchange
939 * @fp: PLOGI request frame
940 *
941 * Locking Note: The rport lock is exected to be held before calling
942 * this function.
943 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700944static void fc_rport_recv_plogi_req(struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -0800945 struct fc_seq *sp, struct fc_frame *rx_fp)
946{
Robert Love42e9a922008-12-09 15:10:17 -0800947 struct fc_lport *lport = rdata->local_port;
948 struct fc_frame *fp = rx_fp;
949 struct fc_exch *ep;
950 struct fc_frame_header *fh;
951 struct fc_els_flogi *pl;
952 struct fc_seq_els_data rjt_data;
953 u32 sid;
954 u64 wwpn;
955 u64 wwnn;
956 enum fc_els_rjt_reason reject = 0;
957 u32 f_ctl;
958 rjt_data.fp = NULL;
959
960 fh = fc_frame_header_get(fp);
961
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700962 FC_RPORT_DBG(rdata, "Received PLOGI request while in state %s\n",
963 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800964
965 sid = ntoh24(fh->fh_s_id);
966 pl = fc_frame_payload_get(fp, sizeof(*pl));
967 if (!pl) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700968 FC_RPORT_DBG(rdata, "Received PLOGI too short\n");
Robert Love42e9a922008-12-09 15:10:17 -0800969 WARN_ON(1);
970 /* XXX TBD: send reject? */
971 fc_frame_free(fp);
972 return;
973 }
974 wwpn = get_unaligned_be64(&pl->fl_wwpn);
975 wwnn = get_unaligned_be64(&pl->fl_wwnn);
976
977 /*
978 * If the session was just created, possibly due to the incoming PLOGI,
979 * set the state appropriately and accept the PLOGI.
980 *
981 * If we had also sent a PLOGI, and if the received PLOGI is from a
982 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
983 * "command already in progress".
984 *
985 * XXX TBD: If the session was ready before, the PLOGI should result in
986 * all outstanding exchanges being reset.
987 */
988 switch (rdata->rp_state) {
989 case RPORT_ST_INIT:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700990 FC_RPORT_DBG(rdata, "Received PLOGI, wwpn %llx state INIT "
Robert Love74147052009-06-10 15:31:10 -0700991 "- reject\n", (unsigned long long)wwpn);
Robert Love42e9a922008-12-09 15:10:17 -0800992 reject = ELS_RJT_UNSUP;
993 break;
994 case RPORT_ST_PLOGI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700995 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state %d\n",
Robert Love74147052009-06-10 15:31:10 -0700996 rdata->rp_state);
Robert Love42e9a922008-12-09 15:10:17 -0800997 if (wwpn < lport->wwpn)
998 reject = ELS_RJT_INPROG;
999 break;
1000 case RPORT_ST_PRLI:
1001 case RPORT_ST_READY:
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001002 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
Robert Love74147052009-06-10 15:31:10 -07001003 "- ignored for now\n", rdata->rp_state);
Robert Love42e9a922008-12-09 15:10:17 -08001004 /* XXX TBD - should reset */
1005 break;
Joe Eykholt14194052009-07-29 17:04:43 -07001006 case RPORT_ST_DELETE:
Robert Love42e9a922008-12-09 15:10:17 -08001007 default:
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001008 FC_RPORT_DBG(rdata, "Received PLOGI in unexpected "
Robert Love74147052009-06-10 15:31:10 -07001009 "state %d\n", rdata->rp_state);
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -07001010 fc_frame_free(fp);
1011 return;
Robert Love42e9a922008-12-09 15:10:17 -08001012 break;
1013 }
1014
1015 if (reject) {
1016 rjt_data.reason = reject;
1017 rjt_data.explan = ELS_EXPL_NONE;
1018 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1019 fc_frame_free(fp);
1020 } else {
1021 fp = fc_frame_alloc(lport, sizeof(*pl));
1022 if (fp == NULL) {
1023 fp = rx_fp;
1024 rjt_data.reason = ELS_RJT_UNAB;
1025 rjt_data.explan = ELS_EXPL_NONE;
1026 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1027 fc_frame_free(fp);
1028 } else {
1029 sp = lport->tt.seq_start_next(sp);
1030 WARN_ON(!sp);
Joe Eykholtf211fa52009-08-25 14:01:01 -07001031 rdata->ids.port_name = wwpn;
1032 rdata->ids.node_name = wwnn;
Robert Love42e9a922008-12-09 15:10:17 -08001033
1034 /*
1035 * Get session payload size from incoming PLOGI.
1036 */
Joe Eykholtf211fa52009-08-25 14:01:01 -07001037 rdata->maxframe_size =
Robert Love42e9a922008-12-09 15:10:17 -08001038 fc_plogi_get_maxframe(pl, lport->mfs);
1039 fc_frame_free(rx_fp);
1040 fc_plogi_fill(lport, fp, ELS_LS_ACC);
1041
1042 /*
1043 * Send LS_ACC. If this fails,
1044 * the originator should retry.
1045 */
1046 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1047 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1048 ep = fc_seq_exch(sp);
1049 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1050 FC_TYPE_ELS, f_ctl, 0);
1051 lport->tt.seq_send(lport, sp, fp);
1052 if (rdata->rp_state == RPORT_ST_PLOGI)
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001053 fc_rport_enter_prli(rdata);
Robert Love42e9a922008-12-09 15:10:17 -08001054 }
1055 }
1056}
1057
1058/**
Robert Love34f42a02009-02-27 10:55:45 -08001059 * fc_rport_recv_prli_req() - Handle incoming Process Login (PRLI) request
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001060 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -08001061 * @sp: current sequence in the PRLI exchange
1062 * @fp: PRLI request frame
1063 *
1064 * Locking Note: The rport lock is exected to be held before calling
1065 * this function.
1066 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001067static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -08001068 struct fc_seq *sp, struct fc_frame *rx_fp)
1069{
Robert Love42e9a922008-12-09 15:10:17 -08001070 struct fc_lport *lport = rdata->local_port;
1071 struct fc_exch *ep;
1072 struct fc_frame *fp;
1073 struct fc_frame_header *fh;
1074 struct {
1075 struct fc_els_prli prli;
1076 struct fc_els_spp spp;
1077 } *pp;
1078 struct fc_els_spp *rspp; /* request service param page */
1079 struct fc_els_spp *spp; /* response spp */
1080 unsigned int len;
1081 unsigned int plen;
1082 enum fc_els_rjt_reason reason = ELS_RJT_UNAB;
1083 enum fc_els_rjt_explan explan = ELS_EXPL_NONE;
1084 enum fc_els_spp_resp resp;
1085 struct fc_seq_els_data rjt_data;
1086 u32 f_ctl;
1087 u32 fcp_parm;
1088 u32 roles = FC_RPORT_ROLE_UNKNOWN;
1089 rjt_data.fp = NULL;
1090
1091 fh = fc_frame_header_get(rx_fp);
1092
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001093 FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1094 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001095
1096 switch (rdata->rp_state) {
1097 case RPORT_ST_PRLI:
1098 case RPORT_ST_READY:
1099 reason = ELS_RJT_NONE;
1100 break;
1101 default:
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -07001102 fc_frame_free(rx_fp);
1103 return;
Robert Love42e9a922008-12-09 15:10:17 -08001104 break;
1105 }
1106 len = fr_len(rx_fp) - sizeof(*fh);
1107 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1108 if (pp == NULL) {
1109 reason = ELS_RJT_PROT;
1110 explan = ELS_EXPL_INV_LEN;
1111 } else {
1112 plen = ntohs(pp->prli.prli_len);
1113 if ((plen % 4) != 0 || plen > len) {
1114 reason = ELS_RJT_PROT;
1115 explan = ELS_EXPL_INV_LEN;
1116 } else if (plen < len) {
1117 len = plen;
1118 }
1119 plen = pp->prli.prli_spp_len;
1120 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1121 plen > len || len < sizeof(*pp)) {
1122 reason = ELS_RJT_PROT;
1123 explan = ELS_EXPL_INV_LEN;
1124 }
1125 rspp = &pp->spp;
1126 }
1127 if (reason != ELS_RJT_NONE ||
1128 (fp = fc_frame_alloc(lport, len)) == NULL) {
1129 rjt_data.reason = reason;
1130 rjt_data.explan = explan;
1131 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1132 } else {
1133 sp = lport->tt.seq_start_next(sp);
1134 WARN_ON(!sp);
1135 pp = fc_frame_payload_get(fp, len);
1136 WARN_ON(!pp);
1137 memset(pp, 0, len);
1138 pp->prli.prli_cmd = ELS_LS_ACC;
1139 pp->prli.prli_spp_len = plen;
1140 pp->prli.prli_len = htons(len);
1141 len -= sizeof(struct fc_els_prli);
1142
1143 /*
1144 * Go through all the service parameter pages and build
1145 * response. If plen indicates longer SPP than standard,
1146 * use that. The entire response has been pre-cleared above.
1147 */
1148 spp = &pp->spp;
1149 while (len >= plen) {
1150 spp->spp_type = rspp->spp_type;
1151 spp->spp_type_ext = rspp->spp_type_ext;
1152 spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
1153 resp = FC_SPP_RESP_ACK;
1154 if (rspp->spp_flags & FC_SPP_RPA_VAL)
1155 resp = FC_SPP_RESP_NO_PA;
1156 switch (rspp->spp_type) {
1157 case 0: /* common to all FC-4 types */
1158 break;
1159 case FC_TYPE_FCP:
1160 fcp_parm = ntohl(rspp->spp_params);
1161 if (fcp_parm * FCP_SPPF_RETRY)
1162 rdata->flags |= FC_RP_FLAGS_RETRY;
Joe Eykholtf211fa52009-08-25 14:01:01 -07001163 rdata->supported_classes = FC_COS_CLASS3;
Robert Love42e9a922008-12-09 15:10:17 -08001164 if (fcp_parm & FCP_SPPF_INIT_FCN)
1165 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1166 if (fcp_parm & FCP_SPPF_TARG_FCN)
1167 roles |= FC_RPORT_ROLE_FCP_TARGET;
Joe Eykholtf211fa52009-08-25 14:01:01 -07001168 rdata->ids.roles = roles;
Robert Love42e9a922008-12-09 15:10:17 -08001169
1170 spp->spp_params =
1171 htonl(lport->service_params);
1172 break;
1173 default:
1174 resp = FC_SPP_RESP_INVL;
1175 break;
1176 }
1177 spp->spp_flags |= resp;
1178 len -= plen;
1179 rspp = (struct fc_els_spp *)((char *)rspp + plen);
1180 spp = (struct fc_els_spp *)((char *)spp + plen);
1181 }
1182
1183 /*
1184 * Send LS_ACC. If this fails, the originator should retry.
1185 */
1186 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1187 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1188 ep = fc_seq_exch(sp);
1189 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1190 FC_TYPE_ELS, f_ctl, 0);
1191 lport->tt.seq_send(lport, sp, fp);
1192
1193 /*
1194 * Get lock and re-check state.
1195 */
1196 switch (rdata->rp_state) {
1197 case RPORT_ST_PRLI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001198 fc_rport_enter_ready(rdata);
Robert Love42e9a922008-12-09 15:10:17 -08001199 break;
1200 case RPORT_ST_READY:
1201 break;
1202 default:
1203 break;
1204 }
1205 }
1206 fc_frame_free(rx_fp);
1207}
1208
1209/**
Robert Love34f42a02009-02-27 10:55:45 -08001210 * fc_rport_recv_prlo_req() - Handle incoming Process Logout (PRLO) request
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001211 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -08001212 * @sp: current sequence in the PRLO exchange
1213 * @fp: PRLO request frame
1214 *
1215 * Locking Note: The rport lock is exected to be held before calling
1216 * this function.
1217 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001218static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
1219 struct fc_seq *sp,
Robert Love42e9a922008-12-09 15:10:17 -08001220 struct fc_frame *fp)
1221{
Robert Love42e9a922008-12-09 15:10:17 -08001222 struct fc_lport *lport = rdata->local_port;
1223
1224 struct fc_frame_header *fh;
1225 struct fc_seq_els_data rjt_data;
1226
1227 fh = fc_frame_header_get(fp);
1228
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001229 FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
1230 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001231
Joe Eykholt14194052009-07-29 17:04:43 -07001232 if (rdata->rp_state == RPORT_ST_DELETE) {
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -07001233 fc_frame_free(fp);
1234 return;
1235 }
1236
Robert Love42e9a922008-12-09 15:10:17 -08001237 rjt_data.fp = NULL;
1238 rjt_data.reason = ELS_RJT_UNAB;
1239 rjt_data.explan = ELS_EXPL_NONE;
1240 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1241 fc_frame_free(fp);
1242}
1243
1244/**
Robert Love34f42a02009-02-27 10:55:45 -08001245 * fc_rport_recv_logo_req() - Handle incoming Logout (LOGO) request
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001246 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -08001247 * @sp: current sequence in the LOGO exchange
1248 * @fp: LOGO request frame
1249 *
1250 * Locking Note: The rport lock is exected to be held before calling
1251 * this function.
1252 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001253static void fc_rport_recv_logo_req(struct fc_rport_priv *rdata,
1254 struct fc_seq *sp,
Robert Love42e9a922008-12-09 15:10:17 -08001255 struct fc_frame *fp)
1256{
1257 struct fc_frame_header *fh;
Robert Love42e9a922008-12-09 15:10:17 -08001258 struct fc_lport *lport = rdata->local_port;
1259
1260 fh = fc_frame_header_get(fp);
1261
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001262 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
1263 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001264
Joe Eykholt14194052009-07-29 17:04:43 -07001265 if (rdata->rp_state == RPORT_ST_DELETE) {
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -07001266 fc_frame_free(fp);
1267 return;
1268 }
1269
Joe Eykholt00fea932009-08-25 14:01:23 -07001270 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
Robert Love42e9a922008-12-09 15:10:17 -08001271
1272 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
1273 fc_frame_free(fp);
1274}
1275
1276static void fc_rport_flush_queue(void)
1277{
1278 flush_workqueue(rport_event_queue);
1279}
1280
Robert Love42e9a922008-12-09 15:10:17 -08001281int fc_rport_init(struct fc_lport *lport)
1282{
Robert Love5101ff92009-02-27 10:55:18 -08001283 if (!lport->tt.rport_create)
Joe Eykholt9e9d0452009-08-25 14:01:18 -07001284 lport->tt.rport_create = fc_rport_create;
Robert Love5101ff92009-02-27 10:55:18 -08001285
Robert Love42e9a922008-12-09 15:10:17 -08001286 if (!lport->tt.rport_login)
1287 lport->tt.rport_login = fc_rport_login;
1288
1289 if (!lport->tt.rport_logoff)
1290 lport->tt.rport_logoff = fc_rport_logoff;
1291
1292 if (!lport->tt.rport_recv_req)
1293 lport->tt.rport_recv_req = fc_rport_recv_req;
1294
1295 if (!lport->tt.rport_flush_queue)
1296 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1297
Joe Eykholtf211fa52009-08-25 14:01:01 -07001298 if (!lport->tt.rport_destroy)
1299 lport->tt.rport_destroy = fc_rport_destroy;
1300
Robert Love42e9a922008-12-09 15:10:17 -08001301 return 0;
1302}
1303EXPORT_SYMBOL(fc_rport_init);
1304
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001305int fc_setup_rport(void)
Robert Love42e9a922008-12-09 15:10:17 -08001306{
1307 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
1308 if (!rport_event_queue)
1309 return -ENOMEM;
1310 return 0;
1311}
1312EXPORT_SYMBOL(fc_setup_rport);
1313
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001314void fc_destroy_rport(void)
Robert Love42e9a922008-12-09 15:10:17 -08001315{
1316 destroy_workqueue(rport_event_queue);
1317}
1318EXPORT_SYMBOL(fc_destroy_rport);
1319
1320void fc_rport_terminate_io(struct fc_rport *rport)
1321{
Joe Eykholtab28f1f2009-08-25 14:00:34 -07001322 struct fc_rport_libfc_priv *rp = rport->dd_data;
1323 struct fc_lport *lport = rp->local_port;
Robert Love42e9a922008-12-09 15:10:17 -08001324
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -08001325 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
1326 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
Robert Love42e9a922008-12-09 15:10:17 -08001327}
1328EXPORT_SYMBOL(fc_rport_terminate_io);