blob: b5bc8724e1a07520c7ec4b7c3c79b5ff55d6fdc3 [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 Eykholt3ac6f982009-08-25 14:03:26 -070066static void fc_rport_recv_plogi_req(struct fc_lport *,
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 Eykholt83fe6a92009-08-25 14:03:31 -070072static void fc_rport_recv_logo_req(struct fc_lport *,
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/**
Joe Eykholt8025b5d2009-08-25 14:02:06 -070090 * fc_rport_lookup() - lookup a remote port by port_id
91 * @lport: Fibre Channel host port instance
92 * @port_id: remote port port_id to match
93 */
94static struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport,
95 u32 port_id)
96{
97 struct fc_rport_priv *rdata;
98
99 list_for_each_entry(rdata, &lport->disc.rports, peers)
100 if (rdata->ids.port_id == port_id &&
101 rdata->rp_state != RPORT_ST_DELETE)
102 return rdata;
103 return NULL;
104}
105
106/**
Robert Love9737e6a2009-08-25 14:02:59 -0700107 * fc_rport_create() - Create a new remote port
108 * @lport: The local port that the new remote port is for
109 * @port_id: The port ID for the new remote port
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700110 *
Joe Eykholt48f00902009-08-25 14:01:50 -0700111 * Locking note: must be called with the disc_mutex held.
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700112 */
113static struct fc_rport_priv *fc_rport_create(struct fc_lport *lport,
Robert Love9737e6a2009-08-25 14:02:59 -0700114 u32 port_id)
Robert Love42e9a922008-12-09 15:10:17 -0800115{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700116 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800117
Robert Love9737e6a2009-08-25 14:02:59 -0700118 rdata = lport->tt.rport_lookup(lport, port_id);
Joe Eykholt19f97e32009-08-25 14:01:55 -0700119 if (rdata)
120 return rdata;
121
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700122 rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
123 if (!rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800124 return NULL;
125
Robert Love9737e6a2009-08-25 14:02:59 -0700126 rdata->ids.node_name = -1;
127 rdata->ids.port_name = -1;
128 rdata->ids.port_id = port_id;
129 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
130
Joe Eykholtf211fa52009-08-25 14:01:01 -0700131 kref_init(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800132 mutex_init(&rdata->rp_mutex);
Joe Eykholt795d86f2009-08-25 14:00:39 -0700133 rdata->local_port = lport;
Robert Love42e9a922008-12-09 15:10:17 -0800134 rdata->rp_state = RPORT_ST_INIT;
135 rdata->event = RPORT_EV_NONE;
136 rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
Joe Eykholt795d86f2009-08-25 14:00:39 -0700137 rdata->e_d_tov = lport->e_d_tov;
138 rdata->r_a_tov = lport->r_a_tov;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700139 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
Robert Love42e9a922008-12-09 15:10:17 -0800140 INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
141 INIT_WORK(&rdata->event_work, fc_rport_work);
Robert Love9737e6a2009-08-25 14:02:59 -0700142 if (port_id != FC_FID_DIR_SERV)
Joe Eykholt48f00902009-08-25 14:01:50 -0700143 list_add(&rdata->peers, &lport->disc.rports);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700144 return rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800145}
146
147/**
Joe Eykholtf211fa52009-08-25 14:01:01 -0700148 * fc_rport_destroy() - free a remote port after last reference is released.
149 * @kref: pointer to kref inside struct fc_rport_priv
150 */
151static void fc_rport_destroy(struct kref *kref)
152{
153 struct fc_rport_priv *rdata;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700154
155 rdata = container_of(kref, struct fc_rport_priv, kref);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700156 kfree(rdata);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700157}
158
159/**
Robert Love34f42a02009-02-27 10:55:45 -0800160 * fc_rport_state() - return a string for the state the rport is in
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700161 * @rdata: remote port private data
Robert Love42e9a922008-12-09 15:10:17 -0800162 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700163static const char *fc_rport_state(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800164{
165 const char *cp;
Robert Love42e9a922008-12-09 15:10:17 -0800166
167 cp = fc_rport_state_names[rdata->rp_state];
168 if (!cp)
169 cp = "Unknown";
170 return cp;
171}
172
173/**
Robert Love34f42a02009-02-27 10:55:45 -0800174 * fc_set_rport_loss_tmo() - Set the remote port loss timeout in seconds.
Robert Love42e9a922008-12-09 15:10:17 -0800175 * @rport: Pointer to Fibre Channel remote port structure
176 * @timeout: timeout in seconds
177 */
178void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
179{
180 if (timeout)
181 rport->dev_loss_tmo = timeout + 5;
182 else
183 rport->dev_loss_tmo = 30;
184}
185EXPORT_SYMBOL(fc_set_rport_loss_tmo);
186
187/**
Robert Love34f42a02009-02-27 10:55:45 -0800188 * fc_plogi_get_maxframe() - Get max payload from the common service parameters
Robert Love42e9a922008-12-09 15:10:17 -0800189 * @flp: FLOGI payload structure
190 * @maxval: upper limit, may be less than what is in the service parameters
191 */
Robert Loveb2ab99c2009-02-27 10:55:50 -0800192static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
193 unsigned int maxval)
Robert Love42e9a922008-12-09 15:10:17 -0800194{
195 unsigned int mfs;
196
197 /*
198 * Get max payload from the common service parameters and the
199 * class 3 receive data field size.
200 */
201 mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
202 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
203 maxval = mfs;
204 mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
205 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
206 maxval = mfs;
207 return maxval;
208}
209
210/**
Robert Love34f42a02009-02-27 10:55:45 -0800211 * fc_rport_state_enter() - Change the rport's state
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700212 * @rdata: The rport whose state should change
Robert Love42e9a922008-12-09 15:10:17 -0800213 * @new: The new state of the rport
214 *
215 * Locking Note: Called with the rport lock held
216 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700217static void fc_rport_state_enter(struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -0800218 enum fc_rport_state new)
219{
Robert Love42e9a922008-12-09 15:10:17 -0800220 if (rdata->rp_state != new)
221 rdata->retries = 0;
222 rdata->rp_state = new;
223}
224
225static void fc_rport_work(struct work_struct *work)
226{
Abhijeet Joglekar571f8242009-02-27 10:54:41 -0800227 u32 port_id;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700228 struct fc_rport_priv *rdata =
229 container_of(work, struct fc_rport_priv, event_work);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700230 struct fc_rport_libfc_priv *rp;
Robert Love42e9a922008-12-09 15:10:17 -0800231 enum fc_rport_event event;
Robert Love42e9a922008-12-09 15:10:17 -0800232 struct fc_lport *lport = rdata->local_port;
233 struct fc_rport_operations *rport_ops;
Joe Eykholt629f4422009-08-25 14:01:06 -0700234 struct fc_rport_identifiers ids;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700235 struct fc_rport *rport;
Robert Love42e9a922008-12-09 15:10:17 -0800236
237 mutex_lock(&rdata->rp_mutex);
238 event = rdata->event;
239 rport_ops = rdata->ops;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700240 rport = rdata->rport;
Robert Love42e9a922008-12-09 15:10:17 -0800241
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700242 FC_RPORT_DBG(rdata, "work event %u\n", event);
243
Joe Eykholt629f4422009-08-25 14:01:06 -0700244 switch (event) {
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700245 case RPORT_EV_READY:
Joe Eykholtf211fa52009-08-25 14:01:01 -0700246 ids = rdata->ids;
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700247 rdata->event = RPORT_EV_NONE;
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700248 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800249 mutex_unlock(&rdata->rp_mutex);
250
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700251 if (!rport)
252 rport = fc_remote_port_add(lport->host, 0, &ids);
253 if (!rport) {
254 FC_RPORT_DBG(rdata, "Failed to add the rport\n");
255 lport->tt.rport_logoff(rdata);
256 kref_put(&rdata->kref, lport->tt.rport_destroy);
257 return;
Robert Love42e9a922008-12-09 15:10:17 -0800258 }
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700259 mutex_lock(&rdata->rp_mutex);
260 if (rdata->rport)
261 FC_RPORT_DBG(rdata, "rport already allocated\n");
262 rdata->rport = rport;
263 rport->maxframe_size = rdata->maxframe_size;
264 rport->supported_classes = rdata->supported_classes;
265
266 rp = rport->dd_data;
267 rp->local_port = lport;
268 rp->rp_state = rdata->rp_state;
269 rp->flags = rdata->flags;
270 rp->e_d_tov = rdata->e_d_tov;
271 rp->r_a_tov = rdata->r_a_tov;
272 mutex_unlock(&rdata->rp_mutex);
273
Joe Eykholt83455922009-08-25 14:02:01 -0700274 if (rport_ops && rport_ops->event_callback) {
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700275 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700276 rport_ops->event_callback(lport, rdata, event);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700277 }
278 kref_put(&rdata->kref, lport->tt.rport_destroy);
Joe Eykholt629f4422009-08-25 14:01:06 -0700279 break;
280
281 case RPORT_EV_FAILED:
282 case RPORT_EV_LOGO:
283 case RPORT_EV_STOP:
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700284 port_id = rdata->ids.port_id;
Robert Love42e9a922008-12-09 15:10:17 -0800285 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700286
Joe Eykholt48f00902009-08-25 14:01:50 -0700287 if (port_id != FC_FID_DIR_SERV) {
288 mutex_lock(&lport->disc.disc_mutex);
289 list_del(&rdata->peers);
290 mutex_unlock(&lport->disc.disc_mutex);
291 }
292
Joe Eykholt83455922009-08-25 14:02:01 -0700293 if (rport_ops && rport_ops->event_callback) {
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700294 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700295 rport_ops->event_callback(lport, rdata, event);
Abhijeet Joglekar571f8242009-02-27 10:54:41 -0800296 }
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700297 cancel_delayed_work_sync(&rdata->retry_work);
298
299 /*
300 * Reset any outstanding exchanges before freeing rport.
301 */
302 lport->tt.exch_mgr_reset(lport, 0, port_id);
303 lport->tt.exch_mgr_reset(lport, port_id, 0);
304
305 if (rport) {
306 rp = rport->dd_data;
307 rp->rp_state = RPORT_ST_DELETE;
308 mutex_lock(&rdata->rp_mutex);
309 rdata->rport = NULL;
310 mutex_unlock(&rdata->rp_mutex);
311 fc_remote_port_delete(rport);
312 }
313 kref_put(&rdata->kref, lport->tt.rport_destroy);
Joe Eykholt629f4422009-08-25 14:01:06 -0700314 break;
315
316 default:
Robert Love42e9a922008-12-09 15:10:17 -0800317 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt629f4422009-08-25 14:01:06 -0700318 break;
319 }
Robert Love42e9a922008-12-09 15:10:17 -0800320}
321
322/**
Robert Love34f42a02009-02-27 10:55:45 -0800323 * fc_rport_login() - Start the remote port login state machine
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700324 * @rdata: private remote port
Robert Love42e9a922008-12-09 15:10:17 -0800325 *
326 * Locking Note: Called without the rport lock held. This
327 * function will hold the rport lock, call an _enter_*
328 * function and then unlock the rport.
329 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700330int fc_rport_login(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800331{
Robert Love42e9a922008-12-09 15:10:17 -0800332 mutex_lock(&rdata->rp_mutex);
333
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700334 FC_RPORT_DBG(rdata, "Login to port\n");
Robert Love42e9a922008-12-09 15:10:17 -0800335
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700336 fc_rport_enter_plogi(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800337
338 mutex_unlock(&rdata->rp_mutex);
339
340 return 0;
341}
342
343/**
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700344 * fc_rport_enter_delete() - schedule a remote port to be deleted.
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700345 * @rdata: private remote port
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700346 * @event: event to report as the reason for deletion
347 *
348 * Locking Note: Called with the rport lock held.
349 *
350 * Allow state change into DELETE only once.
351 *
352 * Call queue_work only if there's no event already pending.
353 * Set the new event so that the old pending event will not occur.
354 * Since we have the mutex, even if fc_rport_work() is already started,
355 * it'll see the new event.
356 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700357static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700358 enum fc_rport_event event)
359{
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700360 if (rdata->rp_state == RPORT_ST_DELETE)
361 return;
362
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700363 FC_RPORT_DBG(rdata, "Delete port\n");
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700364
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700365 fc_rport_state_enter(rdata, RPORT_ST_DELETE);
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700366
367 if (rdata->event == RPORT_EV_NONE)
368 queue_work(rport_event_queue, &rdata->event_work);
369 rdata->event = event;
370}
371
372/**
Robert Love34f42a02009-02-27 10:55:45 -0800373 * fc_rport_logoff() - Logoff and remove an rport
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700374 * @rdata: private remote port
Robert Love42e9a922008-12-09 15:10:17 -0800375 *
376 * Locking Note: Called without the rport lock held. This
377 * function will hold the rport lock, call an _enter_*
378 * function and then unlock the rport.
379 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700380int fc_rport_logoff(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800381{
Robert Love42e9a922008-12-09 15:10:17 -0800382 mutex_lock(&rdata->rp_mutex);
383
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700384 FC_RPORT_DBG(rdata, "Remove port\n");
Robert Love42e9a922008-12-09 15:10:17 -0800385
Joe Eykholt14194052009-07-29 17:04:43 -0700386 if (rdata->rp_state == RPORT_ST_DELETE) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700387 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700388 mutex_unlock(&rdata->rp_mutex);
389 goto out;
390 }
391
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700392 fc_rport_enter_logo(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800393
394 /*
Joe Eykholt14194052009-07-29 17:04:43 -0700395 * Change the state to Delete so that we discard
Robert Love42e9a922008-12-09 15:10:17 -0800396 * the response.
397 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700398 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
Robert Love42e9a922008-12-09 15:10:17 -0800399 mutex_unlock(&rdata->rp_mutex);
400
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700401out:
Robert Love42e9a922008-12-09 15:10:17 -0800402 return 0;
403}
404
405/**
Robert Love34f42a02009-02-27 10:55:45 -0800406 * fc_rport_enter_ready() - The rport is ready
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700407 * @rdata: private remote port
Robert Love42e9a922008-12-09 15:10:17 -0800408 *
409 * Locking Note: The rport lock is expected to be held before calling
410 * this routine.
411 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700412static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800413{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700414 fc_rport_state_enter(rdata, RPORT_ST_READY);
Robert Love42e9a922008-12-09 15:10:17 -0800415
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700416 FC_RPORT_DBG(rdata, "Port is Ready\n");
Robert Love42e9a922008-12-09 15:10:17 -0800417
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700418 if (rdata->event == RPORT_EV_NONE)
419 queue_work(rport_event_queue, &rdata->event_work);
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700420 rdata->event = RPORT_EV_READY;
Robert Love42e9a922008-12-09 15:10:17 -0800421}
422
423/**
Robert Love34f42a02009-02-27 10:55:45 -0800424 * fc_rport_timeout() - Handler for the retry_work timer.
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700425 * @work: The work struct of the fc_rport_priv
Robert Love42e9a922008-12-09 15:10:17 -0800426 *
427 * Locking Note: Called without the rport lock held. This
428 * function will hold the rport lock, call an _enter_*
429 * function and then unlock the rport.
430 */
431static void fc_rport_timeout(struct work_struct *work)
432{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700433 struct fc_rport_priv *rdata =
434 container_of(work, struct fc_rport_priv, retry_work.work);
Robert Love42e9a922008-12-09 15:10:17 -0800435
436 mutex_lock(&rdata->rp_mutex);
437
438 switch (rdata->rp_state) {
439 case RPORT_ST_PLOGI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700440 fc_rport_enter_plogi(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800441 break;
442 case RPORT_ST_PRLI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700443 fc_rport_enter_prli(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800444 break;
445 case RPORT_ST_RTV:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700446 fc_rport_enter_rtv(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800447 break;
448 case RPORT_ST_LOGO:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700449 fc_rport_enter_logo(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800450 break;
451 case RPORT_ST_READY:
452 case RPORT_ST_INIT:
Joe Eykholt14194052009-07-29 17:04:43 -0700453 case RPORT_ST_DELETE:
Robert Love42e9a922008-12-09 15:10:17 -0800454 break;
455 }
456
457 mutex_unlock(&rdata->rp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800458}
459
460/**
Robert Love34f42a02009-02-27 10:55:45 -0800461 * fc_rport_error() - Error handler, called once retries have been exhausted
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700462 * @rdata: private remote port
Robert Love42e9a922008-12-09 15:10:17 -0800463 * @fp: The frame pointer
464 *
Robert Love42e9a922008-12-09 15:10:17 -0800465 * 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(struct fc_rport_priv *rdata, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -0800469{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700470 FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
Joe Eykholtcdbe6df2009-08-25 14:01:39 -0700471 IS_ERR(fp) ? -PTR_ERR(fp) : 0,
472 fc_rport_state(rdata), rdata->retries);
Robert Love42e9a922008-12-09 15:10:17 -0800473
Chris Leech6755db12009-02-27 10:55:02 -0800474 switch (rdata->rp_state) {
475 case RPORT_ST_PLOGI:
476 case RPORT_ST_PRLI:
477 case RPORT_ST_LOGO:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700478 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
Chris Leech6755db12009-02-27 10:55:02 -0800479 break;
480 case RPORT_ST_RTV:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700481 fc_rport_enter_ready(rdata);
Chris Leech6755db12009-02-27 10:55:02 -0800482 break;
Joe Eykholt14194052009-07-29 17:04:43 -0700483 case RPORT_ST_DELETE:
Chris Leech6755db12009-02-27 10:55:02 -0800484 case RPORT_ST_READY:
485 case RPORT_ST_INIT:
486 break;
Robert Love42e9a922008-12-09 15:10:17 -0800487 }
488}
489
490/**
Robert Love34f42a02009-02-27 10:55:45 -0800491 * fc_rport_error_retry() - Error handler when retries are desired
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700492 * @rdata: private remote port data
Chris Leech6755db12009-02-27 10:55:02 -0800493 * @fp: The frame pointer
494 *
495 * If the error was an exchange timeout retry immediately,
496 * otherwise wait for E_D_TOV.
497 *
498 * Locking Note: The rport lock is expected to be held before
499 * calling this routine
500 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700501static void fc_rport_error_retry(struct fc_rport_priv *rdata,
502 struct fc_frame *fp)
Chris Leech6755db12009-02-27 10:55:02 -0800503{
Chris Leech6755db12009-02-27 10:55:02 -0800504 unsigned long delay = FC_DEF_E_D_TOV;
505
506 /* make sure this isn't an FC_EX_CLOSED error, never retry those */
507 if (PTR_ERR(fp) == -FC_EX_CLOSED)
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700508 return fc_rport_error(rdata, fp);
Chris Leech6755db12009-02-27 10:55:02 -0800509
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700510 if (rdata->retries < rdata->local_port->max_rport_retry_count) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700511 FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
512 PTR_ERR(fp), fc_rport_state(rdata));
Chris Leech6755db12009-02-27 10:55:02 -0800513 rdata->retries++;
514 /* no additional delay on exchange timeouts */
515 if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
516 delay = 0;
Chris Leech6755db12009-02-27 10:55:02 -0800517 schedule_delayed_work(&rdata->retry_work, delay);
518 return;
519 }
520
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700521 return fc_rport_error(rdata, fp);
Chris Leech6755db12009-02-27 10:55:02 -0800522}
523
524/**
Robert Love34f42a02009-02-27 10:55:45 -0800525 * fc_rport_plogi_recv_resp() - Handle incoming ELS PLOGI response
Robert Love42e9a922008-12-09 15:10:17 -0800526 * @sp: current sequence in the PLOGI exchange
527 * @fp: response frame
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700528 * @rdata_arg: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800529 *
530 * Locking Note: This function will be called without the rport lock
531 * held, but it will lock, call an _enter_* function or fc_rport_error
532 * and then unlock the rport.
533 */
534static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700535 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800536{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700537 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800538 struct fc_lport *lport = rdata->local_port;
Robert Lovea29e7642009-04-21 16:27:41 -0700539 struct fc_els_flogi *plp = NULL;
Robert Love42e9a922008-12-09 15:10:17 -0800540 unsigned int tov;
541 u16 csp_seq;
542 u16 cssp_seq;
543 u8 op;
544
545 mutex_lock(&rdata->rp_mutex);
546
Joe Eykholtf657d292009-08-25 14:03:21 -0700547 FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800548
549 if (rdata->rp_state != RPORT_ST_PLOGI) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700550 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
551 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700552 if (IS_ERR(fp))
553 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800554 goto out;
555 }
556
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700557 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700558 fc_rport_error_retry(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700559 goto err;
560 }
561
Robert Love42e9a922008-12-09 15:10:17 -0800562 op = fc_frame_payload_op(fp);
563 if (op == ELS_LS_ACC &&
564 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
Joe Eykholtf211fa52009-08-25 14:01:01 -0700565 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
566 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
Robert Love42e9a922008-12-09 15:10:17 -0800567
568 tov = ntohl(plp->fl_csp.sp_e_d_tov);
569 if (ntohs(plp->fl_csp.sp_features) & FC_SP_FT_EDTR)
570 tov /= 1000;
571 if (tov > rdata->e_d_tov)
572 rdata->e_d_tov = tov;
573 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
574 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
575 if (cssp_seq < csp_seq)
576 csp_seq = cssp_seq;
577 rdata->max_seq = csp_seq;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700578 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
Joe Eykholt3ac6f982009-08-25 14:03:26 -0700579 fc_rport_enter_prli(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800580 } else
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700581 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800582
583out:
584 fc_frame_free(fp);
585err:
586 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700587 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800588}
589
590/**
Robert Love34f42a02009-02-27 10:55:45 -0800591 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request to peer
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700592 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800593 *
594 * Locking Note: The rport lock is expected to be held before calling
595 * this routine.
596 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700597static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800598{
Robert Love42e9a922008-12-09 15:10:17 -0800599 struct fc_lport *lport = rdata->local_port;
600 struct fc_frame *fp;
601
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700602 FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
603 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800604
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700605 fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
Robert Love42e9a922008-12-09 15:10:17 -0800606
Joe Eykholtf211fa52009-08-25 14:01:01 -0700607 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
Robert Love42e9a922008-12-09 15:10:17 -0800608 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
609 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700610 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800611 return;
612 }
613 rdata->e_d_tov = lport->e_d_tov;
614
Joe Eykholtf211fa52009-08-25 14:01:01 -0700615 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700616 fc_rport_plogi_resp, rdata, lport->e_d_tov))
617 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800618 else
Joe Eykholtf211fa52009-08-25 14:01:01 -0700619 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800620}
621
622/**
Robert Love34f42a02009-02-27 10:55:45 -0800623 * fc_rport_prli_resp() - Process Login (PRLI) response handler
Robert Love42e9a922008-12-09 15:10:17 -0800624 * @sp: current sequence in the PRLI exchange
625 * @fp: response frame
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700626 * @rdata_arg: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800627 *
628 * Locking Note: This function will be called without the rport lock
629 * held, but it will lock, call an _enter_* function or fc_rport_error
630 * and then unlock the rport.
631 */
632static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700633 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800634{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700635 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800636 struct {
637 struct fc_els_prli prli;
638 struct fc_els_spp spp;
639 } *pp;
640 u32 roles = FC_RPORT_ROLE_UNKNOWN;
641 u32 fcp_parm = 0;
642 u8 op;
643
644 mutex_lock(&rdata->rp_mutex);
645
Joe Eykholtf657d292009-08-25 14:03:21 -0700646 FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800647
648 if (rdata->rp_state != RPORT_ST_PRLI) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700649 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
650 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700651 if (IS_ERR(fp))
652 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800653 goto out;
654 }
655
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700656 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700657 fc_rport_error_retry(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700658 goto err;
659 }
660
Robert Love6bd054c2009-08-25 14:03:04 -0700661 /* reinitialize remote port roles */
662 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
663
Robert Love42e9a922008-12-09 15:10:17 -0800664 op = fc_frame_payload_op(fp);
665 if (op == ELS_LS_ACC) {
666 pp = fc_frame_payload_get(fp, sizeof(*pp));
667 if (pp && pp->prli.prli_spp_len >= sizeof(pp->spp)) {
668 fcp_parm = ntohl(pp->spp.spp_params);
669 if (fcp_parm & FCP_SPPF_RETRY)
670 rdata->flags |= FC_RP_FLAGS_RETRY;
671 }
672
Joe Eykholtf211fa52009-08-25 14:01:01 -0700673 rdata->supported_classes = FC_COS_CLASS3;
Robert Love42e9a922008-12-09 15:10:17 -0800674 if (fcp_parm & FCP_SPPF_INIT_FCN)
675 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
676 if (fcp_parm & FCP_SPPF_TARG_FCN)
677 roles |= FC_RPORT_ROLE_FCP_TARGET;
678
Joe Eykholtf211fa52009-08-25 14:01:01 -0700679 rdata->ids.roles = roles;
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700680 fc_rport_enter_rtv(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800681
682 } else {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700683 FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
684 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
Robert Love42e9a922008-12-09 15:10:17 -0800685 }
686
687out:
688 fc_frame_free(fp);
689err:
690 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700691 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800692}
693
694/**
Robert Love34f42a02009-02-27 10:55:45 -0800695 * fc_rport_logo_resp() - Logout (LOGO) response handler
Robert Love42e9a922008-12-09 15:10:17 -0800696 * @sp: current sequence in the LOGO exchange
697 * @fp: response frame
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700698 * @rdata_arg: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800699 *
700 * Locking Note: This function will be called without the rport lock
701 * held, but it will lock, call an _enter_* function or fc_rport_error
702 * and then unlock the rport.
703 */
704static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700705 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800706{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700707 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800708 u8 op;
709
710 mutex_lock(&rdata->rp_mutex);
711
Joe Eykholtf657d292009-08-25 14:03:21 -0700712 FC_RPORT_DBG(rdata, "Received a LOGO %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800713
Robert Love42e9a922008-12-09 15:10:17 -0800714 if (rdata->rp_state != RPORT_ST_LOGO) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700715 FC_RPORT_DBG(rdata, "Received a LOGO response, but in state "
716 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700717 if (IS_ERR(fp))
718 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800719 goto out;
720 }
721
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700722 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700723 fc_rport_error_retry(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700724 goto err;
725 }
726
Robert Love42e9a922008-12-09 15:10:17 -0800727 op = fc_frame_payload_op(fp);
Joe Eykholt68a17502009-08-25 14:03:42 -0700728 if (op != ELS_LS_ACC)
729 FC_RPORT_DBG(rdata, "Bad ELS response op %x for LOGO command\n",
730 op);
731 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
Robert Love42e9a922008-12-09 15:10:17 -0800732
733out:
734 fc_frame_free(fp);
735err:
736 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700737 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800738}
739
740/**
Robert Love34f42a02009-02-27 10:55:45 -0800741 * fc_rport_enter_prli() - Send Process Login (PRLI) request to peer
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700742 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800743 *
744 * Locking Note: The rport lock is expected to be held before calling
745 * this routine.
746 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700747static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800748{
Robert Love42e9a922008-12-09 15:10:17 -0800749 struct fc_lport *lport = rdata->local_port;
750 struct {
751 struct fc_els_prli prli;
752 struct fc_els_spp spp;
753 } *pp;
754 struct fc_frame *fp;
755
Joe Eykholt3ac6f982009-08-25 14:03:26 -0700756 /*
757 * If the rport is one of the well known addresses
758 * we skip PRLI and RTV and go straight to READY.
759 */
760 if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
761 fc_rport_enter_ready(rdata);
762 return;
763 }
764
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700765 FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
766 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800767
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700768 fc_rport_state_enter(rdata, RPORT_ST_PRLI);
Robert Love42e9a922008-12-09 15:10:17 -0800769
770 fp = fc_frame_alloc(lport, sizeof(*pp));
771 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700772 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800773 return;
774 }
775
Joe Eykholtf211fa52009-08-25 14:01:01 -0700776 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PRLI,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700777 fc_rport_prli_resp, rdata, lport->e_d_tov))
778 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800779 else
Joe Eykholtf211fa52009-08-25 14:01:01 -0700780 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800781}
782
783/**
Robert Love34f42a02009-02-27 10:55:45 -0800784 * fc_rport_els_rtv_resp() - Request Timeout Value response handler
Robert Love42e9a922008-12-09 15:10:17 -0800785 * @sp: current sequence in the RTV exchange
786 * @fp: response frame
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700787 * @rdata_arg: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800788 *
789 * Many targets don't seem to support this.
790 *
791 * Locking Note: This function will be called without the rport lock
792 * held, but it will lock, call an _enter_* function or fc_rport_error
793 * and then unlock the rport.
794 */
795static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700796 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800797{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700798 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800799 u8 op;
800
801 mutex_lock(&rdata->rp_mutex);
802
Joe Eykholtf657d292009-08-25 14:03:21 -0700803 FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800804
805 if (rdata->rp_state != RPORT_ST_RTV) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700806 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
807 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700808 if (IS_ERR(fp))
809 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800810 goto out;
811 }
812
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700813 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700814 fc_rport_error(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700815 goto err;
816 }
817
Robert Love42e9a922008-12-09 15:10:17 -0800818 op = fc_frame_payload_op(fp);
819 if (op == ELS_LS_ACC) {
820 struct fc_els_rtv_acc *rtv;
821 u32 toq;
822 u32 tov;
823
824 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
825 if (rtv) {
826 toq = ntohl(rtv->rtv_toq);
827 tov = ntohl(rtv->rtv_r_a_tov);
828 if (tov == 0)
829 tov = 1;
830 rdata->r_a_tov = tov;
831 tov = ntohl(rtv->rtv_e_d_tov);
832 if (toq & FC_ELS_RTV_EDRES)
833 tov /= 1000000;
834 if (tov == 0)
835 tov = 1;
836 rdata->e_d_tov = tov;
837 }
838 }
839
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700840 fc_rport_enter_ready(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800841
842out:
843 fc_frame_free(fp);
844err:
845 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700846 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800847}
848
849/**
Robert Love34f42a02009-02-27 10:55:45 -0800850 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request to peer
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700851 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800852 *
853 * Locking Note: The rport lock is expected to be held before calling
854 * this routine.
855 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700856static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800857{
858 struct fc_frame *fp;
Robert Love42e9a922008-12-09 15:10:17 -0800859 struct fc_lport *lport = rdata->local_port;
860
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700861 FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
862 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800863
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700864 fc_rport_state_enter(rdata, RPORT_ST_RTV);
Robert Love42e9a922008-12-09 15:10:17 -0800865
866 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
867 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700868 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800869 return;
870 }
871
Joe Eykholtf211fa52009-08-25 14:01:01 -0700872 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700873 fc_rport_rtv_resp, rdata, lport->e_d_tov))
874 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800875 else
Joe Eykholtf211fa52009-08-25 14:01:01 -0700876 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800877}
878
879/**
Robert Love34f42a02009-02-27 10:55:45 -0800880 * fc_rport_enter_logo() - Send Logout (LOGO) request to peer
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700881 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800882 *
883 * Locking Note: The rport lock is expected to be held before calling
884 * this routine.
885 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700886static void fc_rport_enter_logo(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 struct fc_frame *fp;
890
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700891 FC_RPORT_DBG(rdata, "Port entered LOGO state from %s state\n",
892 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800893
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700894 fc_rport_state_enter(rdata, RPORT_ST_LOGO);
Robert Love42e9a922008-12-09 15:10:17 -0800895
896 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
897 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700898 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800899 return;
900 }
901
Joe Eykholtf211fa52009-08-25 14:01:01 -0700902 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700903 fc_rport_logo_resp, rdata, lport->e_d_tov))
904 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800905 else
Joe Eykholtf211fa52009-08-25 14:01:01 -0700906 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800907}
908
Robert Love42e9a922008-12-09 15:10:17 -0800909/**
Joe Eykholt83fe6a92009-08-25 14:03:31 -0700910 * fc_rport_recv_els_req() - handle a validated ELS request.
911 * @lport: Fibre Channel local port
Robert Love42e9a922008-12-09 15:10:17 -0800912 * @sp: current sequence in the PLOGI exchange
913 * @fp: response frame
Joe Eykholt83fe6a92009-08-25 14:03:31 -0700914 *
915 * Handle incoming ELS requests that require port login.
916 * The ELS opcode has already been validated by the caller.
Robert Love42e9a922008-12-09 15:10:17 -0800917 *
Joe Eykholt131203a2009-08-25 14:03:10 -0700918 * Locking Note: Called with the lport lock held.
Robert Love42e9a922008-12-09 15:10:17 -0800919 */
Joe Eykholt83fe6a92009-08-25 14:03:31 -0700920static void fc_rport_recv_els_req(struct fc_lport *lport,
921 struct fc_seq *sp, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -0800922{
Joe Eykholt131203a2009-08-25 14:03:10 -0700923 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800924 struct fc_frame_header *fh;
925 struct fc_seq_els_data els_data;
Robert Love42e9a922008-12-09 15:10:17 -0800926
Robert Love42e9a922008-12-09 15:10:17 -0800927 els_data.fp = NULL;
Joe Eykholt83fe6a92009-08-25 14:03:31 -0700928 els_data.reason = ELS_RJT_UNAB;
929 els_data.explan = ELS_EXPL_PLOGI_REQD;
Joe Eykholt3ac6f982009-08-25 14:03:26 -0700930
Robert Love42e9a922008-12-09 15:10:17 -0800931 fh = fc_frame_header_get(fp);
932
Joe Eykholt25b37b92009-08-25 14:03:15 -0700933 mutex_lock(&lport->disc.disc_mutex);
Joe Eykholt83fe6a92009-08-25 14:03:31 -0700934 rdata = lport->tt.rport_lookup(lport, ntoh24(fh->fh_s_id));
Joe Eykholt131203a2009-08-25 14:03:10 -0700935 if (!rdata) {
Joe Eykholt25b37b92009-08-25 14:03:15 -0700936 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt83fe6a92009-08-25 14:03:31 -0700937 goto reject;
Joe Eykholt131203a2009-08-25 14:03:10 -0700938 }
939 mutex_lock(&rdata->rp_mutex);
Joe Eykholt25b37b92009-08-25 14:03:15 -0700940 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt131203a2009-08-25 14:03:10 -0700941
Joe Eykholt83fe6a92009-08-25 14:03:31 -0700942 switch (rdata->rp_state) {
943 case RPORT_ST_PRLI:
944 case RPORT_ST_RTV:
945 case RPORT_ST_READY:
946 break;
947 default:
948 mutex_unlock(&rdata->rp_mutex);
949 goto reject;
950 }
951
952 switch (fc_frame_payload_op(fp)) {
Joe Eykholt131203a2009-08-25 14:03:10 -0700953 case ELS_PRLI:
954 fc_rport_recv_prli_req(rdata, sp, fp);
955 break;
956 case ELS_PRLO:
957 fc_rport_recv_prlo_req(rdata, sp, fp);
958 break;
Joe Eykholt131203a2009-08-25 14:03:10 -0700959 case ELS_RRQ:
960 els_data.fp = fp;
961 lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
962 break;
963 case ELS_REC:
964 els_data.fp = fp;
965 lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
966 break;
967 default:
Joe Eykholt83fe6a92009-08-25 14:03:31 -0700968 fc_frame_free(fp); /* can't happen */
Joe Eykholt131203a2009-08-25 14:03:10 -0700969 break;
Robert Love42e9a922008-12-09 15:10:17 -0800970 }
971
972 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt83fe6a92009-08-25 14:03:31 -0700973 return;
974
975reject:
976 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
977 fc_frame_free(fp);
978}
979
980/**
981 * fc_rport_recv_req() - Handle a received ELS request from a rport
982 * @sp: current sequence in the PLOGI exchange
983 * @fp: response frame
984 * @lport: Fibre Channel local port
985 *
986 * Locking Note: Called with the lport lock held.
987 */
988void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
989 struct fc_lport *lport)
990{
991 struct fc_seq_els_data els_data;
992
993 /*
994 * Handle PLOGI and LOGO requests separately, since they
995 * don't require prior login.
996 * Check for unsupported opcodes first and reject them.
997 * For some ops, it would be incorrect to reject with "PLOGI required".
998 */
999 switch (fc_frame_payload_op(fp)) {
1000 case ELS_PLOGI:
1001 fc_rport_recv_plogi_req(lport, sp, fp);
1002 break;
1003 case ELS_LOGO:
1004 fc_rport_recv_logo_req(lport, sp, fp);
1005 break;
1006 case ELS_PRLI:
1007 case ELS_PRLO:
1008 case ELS_RRQ:
1009 case ELS_REC:
1010 fc_rport_recv_els_req(lport, sp, fp);
1011 break;
1012 default:
1013 fc_frame_free(fp);
1014 els_data.fp = NULL;
1015 els_data.reason = ELS_RJT_UNSUP;
1016 els_data.explan = ELS_EXPL_NONE;
1017 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
1018 break;
1019 }
Robert Love42e9a922008-12-09 15:10:17 -08001020}
1021
1022/**
Robert Love34f42a02009-02-27 10:55:45 -08001023 * fc_rport_recv_plogi_req() - Handle incoming Port Login (PLOGI) request
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001024 * @lport: local port
Robert Love42e9a922008-12-09 15:10:17 -08001025 * @sp: current sequence in the PLOGI exchange
1026 * @fp: PLOGI request frame
1027 *
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001028 * Locking Note: The rport lock is held before calling this function.
Robert Love42e9a922008-12-09 15:10:17 -08001029 */
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001030static void fc_rport_recv_plogi_req(struct fc_lport *lport,
Robert Love42e9a922008-12-09 15:10:17 -08001031 struct fc_seq *sp, struct fc_frame *rx_fp)
1032{
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001033 struct fc_disc *disc;
1034 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -08001035 struct fc_frame *fp = rx_fp;
1036 struct fc_exch *ep;
1037 struct fc_frame_header *fh;
1038 struct fc_els_flogi *pl;
1039 struct fc_seq_els_data rjt_data;
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001040 u32 sid, f_ctl;
1041
Robert Love42e9a922008-12-09 15:10:17 -08001042 rjt_data.fp = NULL;
Robert Love42e9a922008-12-09 15:10:17 -08001043 fh = fc_frame_header_get(fp);
Robert Love42e9a922008-12-09 15:10:17 -08001044 sid = ntoh24(fh->fh_s_id);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001045
1046 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
1047
Robert Love42e9a922008-12-09 15:10:17 -08001048 pl = fc_frame_payload_get(fp, sizeof(*pl));
1049 if (!pl) {
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001050 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
1051 rjt_data.reason = ELS_RJT_PROT;
1052 rjt_data.explan = ELS_EXPL_INV_LEN;
1053 goto reject;
Robert Love42e9a922008-12-09 15:10:17 -08001054 }
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001055
1056 disc = &lport->disc;
1057 mutex_lock(&disc->disc_mutex);
1058 rdata = lport->tt.rport_create(lport, sid);
1059 if (!rdata) {
1060 mutex_unlock(&disc->disc_mutex);
1061 rjt_data.reason = ELS_RJT_UNAB;
1062 rjt_data.explan = ELS_EXPL_INSUF_RES;
1063 goto reject;
1064 }
1065
1066 mutex_lock(&rdata->rp_mutex);
1067 mutex_unlock(&disc->disc_mutex);
1068
1069 rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1070 rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
Robert Love42e9a922008-12-09 15:10:17 -08001071
1072 /*
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001073 * If the rport was just created, possibly due to the incoming PLOGI,
Robert Love42e9a922008-12-09 15:10:17 -08001074 * set the state appropriately and accept the PLOGI.
1075 *
1076 * If we had also sent a PLOGI, and if the received PLOGI is from a
1077 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1078 * "command already in progress".
1079 *
1080 * XXX TBD: If the session was ready before, the PLOGI should result in
1081 * all outstanding exchanges being reset.
1082 */
1083 switch (rdata->rp_state) {
1084 case RPORT_ST_INIT:
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001085 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
Robert Love42e9a922008-12-09 15:10:17 -08001086 break;
1087 case RPORT_ST_PLOGI:
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001088 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1089 if (rdata->ids.port_name < lport->wwpn) {
1090 mutex_unlock(&rdata->rp_mutex);
1091 rjt_data.reason = ELS_RJT_INPROG;
1092 rjt_data.explan = ELS_EXPL_NONE;
1093 goto reject;
1094 }
Robert Love42e9a922008-12-09 15:10:17 -08001095 break;
1096 case RPORT_ST_PRLI:
1097 case RPORT_ST_READY:
Robert Love42e9a922008-12-09 15:10:17 -08001098 break;
Joe Eykholt14194052009-07-29 17:04:43 -07001099 case RPORT_ST_DELETE:
Robert Love42e9a922008-12-09 15:10:17 -08001100 default:
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001101 FC_RPORT_DBG(rdata, "Received PLOGI in unexpected state %d\n",
1102 rdata->rp_state);
1103 fc_frame_free(rx_fp);
1104 goto out;
Robert Love42e9a922008-12-09 15:10:17 -08001105 }
1106
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001107 /*
1108 * Get session payload size from incoming PLOGI.
1109 */
1110 rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
1111 fc_frame_free(rx_fp);
Robert Love42e9a922008-12-09 15:10:17 -08001112
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001113 /*
1114 * Send LS_ACC. If this fails, the originator should retry.
1115 */
1116 sp = lport->tt.seq_start_next(sp);
1117 if (!sp)
1118 goto out;
1119 fp = fc_frame_alloc(lport, sizeof(*pl));
1120 if (!fp)
1121 goto out;
Robert Love42e9a922008-12-09 15:10:17 -08001122
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001123 fc_plogi_fill(lport, fp, ELS_LS_ACC);
1124 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1125 ep = fc_seq_exch(sp);
1126 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1127 FC_TYPE_ELS, f_ctl, 0);
1128 lport->tt.seq_send(lport, sp, fp);
1129 fc_rport_enter_prli(rdata);
1130out:
1131 mutex_unlock(&rdata->rp_mutex);
1132 return;
1133
1134reject:
1135 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1136 fc_frame_free(fp);
Robert Love42e9a922008-12-09 15:10:17 -08001137}
1138
1139/**
Robert Love34f42a02009-02-27 10:55:45 -08001140 * fc_rport_recv_prli_req() - Handle incoming Process Login (PRLI) request
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001141 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -08001142 * @sp: current sequence in the PRLI exchange
1143 * @fp: PRLI request frame
1144 *
1145 * Locking Note: The rport lock is exected to be held before calling
1146 * this function.
1147 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001148static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -08001149 struct fc_seq *sp, struct fc_frame *rx_fp)
1150{
Robert Love42e9a922008-12-09 15:10:17 -08001151 struct fc_lport *lport = rdata->local_port;
1152 struct fc_exch *ep;
1153 struct fc_frame *fp;
1154 struct fc_frame_header *fh;
1155 struct {
1156 struct fc_els_prli prli;
1157 struct fc_els_spp spp;
1158 } *pp;
1159 struct fc_els_spp *rspp; /* request service param page */
1160 struct fc_els_spp *spp; /* response spp */
1161 unsigned int len;
1162 unsigned int plen;
1163 enum fc_els_rjt_reason reason = ELS_RJT_UNAB;
1164 enum fc_els_rjt_explan explan = ELS_EXPL_NONE;
1165 enum fc_els_spp_resp resp;
1166 struct fc_seq_els_data rjt_data;
1167 u32 f_ctl;
1168 u32 fcp_parm;
1169 u32 roles = FC_RPORT_ROLE_UNKNOWN;
1170 rjt_data.fp = NULL;
1171
1172 fh = fc_frame_header_get(rx_fp);
1173
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001174 FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1175 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001176
1177 switch (rdata->rp_state) {
1178 case RPORT_ST_PRLI:
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001179 case RPORT_ST_RTV:
Robert Love42e9a922008-12-09 15:10:17 -08001180 case RPORT_ST_READY:
1181 reason = ELS_RJT_NONE;
1182 break;
1183 default:
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -07001184 fc_frame_free(rx_fp);
1185 return;
Robert Love42e9a922008-12-09 15:10:17 -08001186 break;
1187 }
1188 len = fr_len(rx_fp) - sizeof(*fh);
1189 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1190 if (pp == NULL) {
1191 reason = ELS_RJT_PROT;
1192 explan = ELS_EXPL_INV_LEN;
1193 } else {
1194 plen = ntohs(pp->prli.prli_len);
1195 if ((plen % 4) != 0 || plen > len) {
1196 reason = ELS_RJT_PROT;
1197 explan = ELS_EXPL_INV_LEN;
1198 } else if (plen < len) {
1199 len = plen;
1200 }
1201 plen = pp->prli.prli_spp_len;
1202 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1203 plen > len || len < sizeof(*pp)) {
1204 reason = ELS_RJT_PROT;
1205 explan = ELS_EXPL_INV_LEN;
1206 }
1207 rspp = &pp->spp;
1208 }
1209 if (reason != ELS_RJT_NONE ||
1210 (fp = fc_frame_alloc(lport, len)) == NULL) {
1211 rjt_data.reason = reason;
1212 rjt_data.explan = explan;
1213 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1214 } else {
1215 sp = lport->tt.seq_start_next(sp);
1216 WARN_ON(!sp);
1217 pp = fc_frame_payload_get(fp, len);
1218 WARN_ON(!pp);
1219 memset(pp, 0, len);
1220 pp->prli.prli_cmd = ELS_LS_ACC;
1221 pp->prli.prli_spp_len = plen;
1222 pp->prli.prli_len = htons(len);
1223 len -= sizeof(struct fc_els_prli);
1224
Robert Love6bd054c2009-08-25 14:03:04 -07001225 /* reinitialize remote port roles */
1226 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
1227
Robert Love42e9a922008-12-09 15:10:17 -08001228 /*
1229 * Go through all the service parameter pages and build
1230 * response. If plen indicates longer SPP than standard,
1231 * use that. The entire response has been pre-cleared above.
1232 */
1233 spp = &pp->spp;
1234 while (len >= plen) {
1235 spp->spp_type = rspp->spp_type;
1236 spp->spp_type_ext = rspp->spp_type_ext;
1237 spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
1238 resp = FC_SPP_RESP_ACK;
1239 if (rspp->spp_flags & FC_SPP_RPA_VAL)
1240 resp = FC_SPP_RESP_NO_PA;
1241 switch (rspp->spp_type) {
1242 case 0: /* common to all FC-4 types */
1243 break;
1244 case FC_TYPE_FCP:
1245 fcp_parm = ntohl(rspp->spp_params);
1246 if (fcp_parm * FCP_SPPF_RETRY)
1247 rdata->flags |= FC_RP_FLAGS_RETRY;
Joe Eykholtf211fa52009-08-25 14:01:01 -07001248 rdata->supported_classes = FC_COS_CLASS3;
Robert Love42e9a922008-12-09 15:10:17 -08001249 if (fcp_parm & FCP_SPPF_INIT_FCN)
1250 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1251 if (fcp_parm & FCP_SPPF_TARG_FCN)
1252 roles |= FC_RPORT_ROLE_FCP_TARGET;
Joe Eykholtf211fa52009-08-25 14:01:01 -07001253 rdata->ids.roles = roles;
Robert Love42e9a922008-12-09 15:10:17 -08001254
1255 spp->spp_params =
1256 htonl(lport->service_params);
1257 break;
1258 default:
1259 resp = FC_SPP_RESP_INVL;
1260 break;
1261 }
1262 spp->spp_flags |= resp;
1263 len -= plen;
1264 rspp = (struct fc_els_spp *)((char *)rspp + plen);
1265 spp = (struct fc_els_spp *)((char *)spp + plen);
1266 }
1267
1268 /*
1269 * Send LS_ACC. If this fails, the originator should retry.
1270 */
1271 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1272 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1273 ep = fc_seq_exch(sp);
1274 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1275 FC_TYPE_ELS, f_ctl, 0);
1276 lport->tt.seq_send(lport, sp, fp);
1277
1278 /*
1279 * Get lock and re-check state.
1280 */
1281 switch (rdata->rp_state) {
1282 case RPORT_ST_PRLI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001283 fc_rport_enter_ready(rdata);
Robert Love42e9a922008-12-09 15:10:17 -08001284 break;
1285 case RPORT_ST_READY:
1286 break;
1287 default:
1288 break;
1289 }
1290 }
1291 fc_frame_free(rx_fp);
1292}
1293
1294/**
Robert Love34f42a02009-02-27 10:55:45 -08001295 * fc_rport_recv_prlo_req() - Handle incoming Process Logout (PRLO) request
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001296 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -08001297 * @sp: current sequence in the PRLO exchange
1298 * @fp: PRLO request frame
1299 *
1300 * Locking Note: The rport lock is exected to be held before calling
1301 * this function.
1302 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001303static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
1304 struct fc_seq *sp,
Robert Love42e9a922008-12-09 15:10:17 -08001305 struct fc_frame *fp)
1306{
Robert Love42e9a922008-12-09 15:10:17 -08001307 struct fc_lport *lport = rdata->local_port;
1308
1309 struct fc_frame_header *fh;
1310 struct fc_seq_els_data rjt_data;
1311
1312 fh = fc_frame_header_get(fp);
1313
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001314 FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
1315 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001316
1317 rjt_data.fp = NULL;
1318 rjt_data.reason = ELS_RJT_UNAB;
1319 rjt_data.explan = ELS_EXPL_NONE;
1320 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1321 fc_frame_free(fp);
1322}
1323
1324/**
Robert Love34f42a02009-02-27 10:55:45 -08001325 * fc_rport_recv_logo_req() - Handle incoming Logout (LOGO) request
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001326 * @lport: local port.
Robert Love42e9a922008-12-09 15:10:17 -08001327 * @sp: current sequence in the LOGO exchange
1328 * @fp: LOGO request frame
1329 *
1330 * Locking Note: The rport lock is exected to be held before calling
1331 * this function.
1332 */
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001333static void fc_rport_recv_logo_req(struct fc_lport *lport,
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001334 struct fc_seq *sp,
Robert Love42e9a922008-12-09 15:10:17 -08001335 struct fc_frame *fp)
1336{
1337 struct fc_frame_header *fh;
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001338 struct fc_rport_priv *rdata;
1339 u32 sid;
Robert Love42e9a922008-12-09 15:10:17 -08001340
Joe Eykholtfeab4ae2009-08-25 14:03:36 -07001341 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
1342
Robert Love42e9a922008-12-09 15:10:17 -08001343 fh = fc_frame_header_get(fp);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001344 sid = ntoh24(fh->fh_s_id);
Robert Love42e9a922008-12-09 15:10:17 -08001345
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001346 mutex_lock(&lport->disc.disc_mutex);
1347 rdata = lport->tt.rport_lookup(lport, sid);
1348 if (rdata) {
1349 mutex_lock(&rdata->rp_mutex);
1350 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
1351 fc_rport_state(rdata));
Joe Eykholtfeab4ae2009-08-25 14:03:36 -07001352
1353 /*
1354 * If the remote port was created due to discovery,
1355 * log back in. It may have seen a stale RSCN about us.
1356 */
1357 if (rdata->rp_state != RPORT_ST_DELETE && rdata->disc_id)
1358 fc_rport_enter_plogi(rdata);
1359 else
1360 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001361 mutex_unlock(&rdata->rp_mutex);
1362 } else
1363 FC_RPORT_ID_DBG(lport, sid,
1364 "Received LOGO from non-logged-in port\n");
1365 mutex_unlock(&lport->disc.disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -08001366 fc_frame_free(fp);
1367}
1368
1369static void fc_rport_flush_queue(void)
1370{
1371 flush_workqueue(rport_event_queue);
1372}
1373
Robert Love42e9a922008-12-09 15:10:17 -08001374int fc_rport_init(struct fc_lport *lport)
1375{
Joe Eykholt8025b5d2009-08-25 14:02:06 -07001376 if (!lport->tt.rport_lookup)
1377 lport->tt.rport_lookup = fc_rport_lookup;
1378
Robert Love5101ff92009-02-27 10:55:18 -08001379 if (!lport->tt.rport_create)
Joe Eykholt9e9d0452009-08-25 14:01:18 -07001380 lport->tt.rport_create = fc_rport_create;
Robert Love5101ff92009-02-27 10:55:18 -08001381
Robert Love42e9a922008-12-09 15:10:17 -08001382 if (!lport->tt.rport_login)
1383 lport->tt.rport_login = fc_rport_login;
1384
1385 if (!lport->tt.rport_logoff)
1386 lport->tt.rport_logoff = fc_rport_logoff;
1387
1388 if (!lport->tt.rport_recv_req)
1389 lport->tt.rport_recv_req = fc_rport_recv_req;
1390
1391 if (!lport->tt.rport_flush_queue)
1392 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1393
Joe Eykholtf211fa52009-08-25 14:01:01 -07001394 if (!lport->tt.rport_destroy)
1395 lport->tt.rport_destroy = fc_rport_destroy;
1396
Robert Love42e9a922008-12-09 15:10:17 -08001397 return 0;
1398}
1399EXPORT_SYMBOL(fc_rport_init);
1400
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001401int fc_setup_rport(void)
Robert Love42e9a922008-12-09 15:10:17 -08001402{
1403 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
1404 if (!rport_event_queue)
1405 return -ENOMEM;
1406 return 0;
1407}
1408EXPORT_SYMBOL(fc_setup_rport);
1409
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001410void fc_destroy_rport(void)
Robert Love42e9a922008-12-09 15:10:17 -08001411{
1412 destroy_workqueue(rport_event_queue);
1413}
1414EXPORT_SYMBOL(fc_destroy_rport);
1415
1416void fc_rport_terminate_io(struct fc_rport *rport)
1417{
Joe Eykholtab28f1f2009-08-25 14:00:34 -07001418 struct fc_rport_libfc_priv *rp = rport->dd_data;
1419 struct fc_lport *lport = rp->local_port;
Robert Love42e9a922008-12-09 15:10:17 -08001420
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -08001421 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
1422 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
Robert Love42e9a922008-12-09 15:10:17 -08001423}
1424EXPORT_SYMBOL(fc_rport_terminate_io);