]> git.ipfire.org Git - thirdparty/linux.git/blame - drivers/scsi/libfc/fc_rport.c
[SCSI] libfc: add interface to allocate a sequence for incoming requests
[thirdparty/linux.git] / drivers / scsi / libfc / fc_rport.c
CommitLineData
42e9a92f
RL
1/*
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>
5a0e3ad6 50#include <linux/slab.h>
42e9a92f
RL
51#include <linux/rcupdate.h>
52#include <linux/timer.h>
53#include <linux/workqueue.h>
54#include <asm/unaligned.h>
55
56#include <scsi/libfc.h>
57#include <scsi/fc_encode.h>
58
8866a5d9
RL
59#include "fc_libfc.h"
60
42e9a92f
RL
61struct workqueue_struct *rport_event_queue;
62
a7b12a27 63static void fc_rport_enter_flogi(struct fc_rport_priv *);
9fb9d328
JE
64static void fc_rport_enter_plogi(struct fc_rport_priv *);
65static void fc_rport_enter_prli(struct fc_rport_priv *);
66static void fc_rport_enter_rtv(struct fc_rport_priv *);
67static void fc_rport_enter_ready(struct fc_rport_priv *);
68static void fc_rport_enter_logo(struct fc_rport_priv *);
370c3bd0 69static void fc_rport_enter_adisc(struct fc_rport_priv *);
42e9a92f 70
3ac6f98f 71static void fc_rport_recv_plogi_req(struct fc_lport *,
42e9a92f 72 struct fc_seq *, struct fc_frame *);
9fb9d328 73static void fc_rport_recv_prli_req(struct fc_rport_priv *,
42e9a92f 74 struct fc_seq *, struct fc_frame *);
9fb9d328 75static void fc_rport_recv_prlo_req(struct fc_rport_priv *,
42e9a92f 76 struct fc_seq *, struct fc_frame *);
83fe6a93 77static void fc_rport_recv_logo_req(struct fc_lport *,
42e9a92f
RL
78 struct fc_seq *, struct fc_frame *);
79static void fc_rport_timeout(struct work_struct *);
9fb9d328
JE
80static void fc_rport_error(struct fc_rport_priv *, struct fc_frame *);
81static void fc_rport_error_retry(struct fc_rport_priv *, struct fc_frame *);
42e9a92f
RL
82static void fc_rport_work(struct work_struct *);
83
84static const char *fc_rport_state_names[] = {
42e9a92f 85 [RPORT_ST_INIT] = "Init",
a7b12a27
JE
86 [RPORT_ST_FLOGI] = "FLOGI",
87 [RPORT_ST_PLOGI_WAIT] = "PLOGI_WAIT",
42e9a92f
RL
88 [RPORT_ST_PLOGI] = "PLOGI",
89 [RPORT_ST_PRLI] = "PRLI",
90 [RPORT_ST_RTV] = "RTV",
91 [RPORT_ST_READY] = "Ready",
370c3bd0 92 [RPORT_ST_ADISC] = "ADISC",
14194054 93 [RPORT_ST_DELETE] = "Delete",
42e9a92f
RL
94};
95
8025b5db 96/**
3a3b42bf
RL
97 * fc_rport_lookup() - Lookup a remote port by port_id
98 * @lport: The local port to lookup the remote port on
99 * @port_id: The remote port ID to look up
42e90414
JE
100 *
101 * The caller must hold either disc_mutex or rcu_read_lock().
8025b5db
JE
102 */
103static struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport,
104 u32 port_id)
105{
106 struct fc_rport_priv *rdata;
107
42e90414 108 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers)
b4a9c7ed 109 if (rdata->ids.port_id == port_id)
8025b5db
JE
110 return rdata;
111 return NULL;
112}
113
9e9d0452 114/**
9737e6a7 115 * fc_rport_create() - Create a new remote port
3a3b42bf
RL
116 * @lport: The local port this remote port will be associated with
117 * @ids: The identifiers for the new remote port
118 *
119 * The remote port will start in the INIT state.
9e9d0452 120 *
48f00902 121 * Locking note: must be called with the disc_mutex held.
9e9d0452
JE
122 */
123static struct fc_rport_priv *fc_rport_create(struct fc_lport *lport,
9737e6a7 124 u32 port_id)
42e9a92f 125{
ab28f1fd 126 struct fc_rport_priv *rdata;
42e9a92f 127
9737e6a7 128 rdata = lport->tt.rport_lookup(lport, port_id);
19f97e3c
JE
129 if (rdata)
130 return rdata;
131
f90377ab 132 rdata = kzalloc(sizeof(*rdata) + lport->rport_priv_size, GFP_KERNEL);
9e9d0452 133 if (!rdata)
42e9a92f
RL
134 return NULL;
135
9737e6a7
RL
136 rdata->ids.node_name = -1;
137 rdata->ids.port_name = -1;
138 rdata->ids.port_id = port_id;
139 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
140
f211fa51 141 kref_init(&rdata->kref);
42e9a92f 142 mutex_init(&rdata->rp_mutex);
795d86f5 143 rdata->local_port = lport;
42e9a92f
RL
144 rdata->rp_state = RPORT_ST_INIT;
145 rdata->event = RPORT_EV_NONE;
146 rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
795d86f5
JE
147 rdata->e_d_tov = lport->e_d_tov;
148 rdata->r_a_tov = lport->r_a_tov;
f211fa51 149 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
42e9a92f
RL
150 INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
151 INIT_WORK(&rdata->event_work, fc_rport_work);
9737e6a7 152 if (port_id != FC_FID_DIR_SERV)
42e90414 153 list_add_rcu(&rdata->peers, &lport->disc.rports);
9fb9d328 154 return rdata;
42e9a92f
RL
155}
156
42e90414
JE
157/**
158 * fc_rport_free_rcu() - Free a remote port
159 * @rcu: The rcu_head structure inside the remote port
160 */
161static void fc_rport_free_rcu(struct rcu_head *rcu)
162{
163 struct fc_rport_priv *rdata;
164
165 rdata = container_of(rcu, struct fc_rport_priv, rcu);
166 kfree(rdata);
167}
168
f211fa51 169/**
3a3b42bf
RL
170 * fc_rport_destroy() - Free a remote port after last reference is released
171 * @kref: The remote port's kref
f211fa51
JE
172 */
173static void fc_rport_destroy(struct kref *kref)
174{
175 struct fc_rport_priv *rdata;
f211fa51
JE
176
177 rdata = container_of(kref, struct fc_rport_priv, kref);
42e90414 178 call_rcu(&rdata->rcu, fc_rport_free_rcu);
f211fa51
JE
179}
180
42e9a92f 181/**
3a3b42bf
RL
182 * fc_rport_state() - Return a string identifying the remote port's state
183 * @rdata: The remote port
42e9a92f 184 */
9fb9d328 185static const char *fc_rport_state(struct fc_rport_priv *rdata)
42e9a92f
RL
186{
187 const char *cp;
42e9a92f
RL
188
189 cp = fc_rport_state_names[rdata->rp_state];
190 if (!cp)
191 cp = "Unknown";
192 return cp;
193}
194
195/**
3a3b42bf
RL
196 * fc_set_rport_loss_tmo() - Set the remote port loss timeout
197 * @rport: The remote port that gets a new timeout value
198 * @timeout: The new timeout value (in seconds)
42e9a92f
RL
199 */
200void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
201{
202 if (timeout)
203 rport->dev_loss_tmo = timeout + 5;
204 else
205 rport->dev_loss_tmo = 30;
206}
207EXPORT_SYMBOL(fc_set_rport_loss_tmo);
208
209/**
3a3b42bf
RL
210 * fc_plogi_get_maxframe() - Get the maximum payload from the common service
211 * parameters in a FLOGI frame
a7b12a27 212 * @flp: The FLOGI or PLOGI payload
3a3b42bf
RL
213 * @maxval: The maximum frame size upper limit; this may be less than what
214 * is in the service parameters
42e9a92f 215 */
b2ab99c9
RL
216static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
217 unsigned int maxval)
42e9a92f
RL
218{
219 unsigned int mfs;
220
221 /*
222 * Get max payload from the common service parameters and the
223 * class 3 receive data field size.
224 */
225 mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
226 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
227 maxval = mfs;
228 mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
229 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
230 maxval = mfs;
231 return maxval;
232}
233
234/**
3a3b42bf
RL
235 * fc_rport_state_enter() - Change the state of a remote port
236 * @rdata: The remote port whose state should change
237 * @new: The new state
42e9a92f
RL
238 *
239 * Locking Note: Called with the rport lock held
240 */
9fb9d328 241static void fc_rport_state_enter(struct fc_rport_priv *rdata,
42e9a92f
RL
242 enum fc_rport_state new)
243{
42e9a92f
RL
244 if (rdata->rp_state != new)
245 rdata->retries = 0;
246 rdata->rp_state = new;
247}
248
3a3b42bf
RL
249/**
250 * fc_rport_work() - Handler for remote port events in the rport_event_queue
251 * @work: Handle to the remote port being dequeued
252 */
42e9a92f
RL
253static void fc_rport_work(struct work_struct *work)
254{
571f824c 255 u32 port_id;
ab28f1fd
JE
256 struct fc_rport_priv *rdata =
257 container_of(work, struct fc_rport_priv, event_work);
3a3b42bf 258 struct fc_rport_libfc_priv *rpriv;
42e9a92f 259 enum fc_rport_event event;
42e9a92f
RL
260 struct fc_lport *lport = rdata->local_port;
261 struct fc_rport_operations *rport_ops;
629f4427 262 struct fc_rport_identifiers ids;
f211fa51 263 struct fc_rport *rport;
42e9a92f
RL
264
265 mutex_lock(&rdata->rp_mutex);
266 event = rdata->event;
267 rport_ops = rdata->ops;
f211fa51 268 rport = rdata->rport;
42e9a92f 269
9e9d0452
JE
270 FC_RPORT_DBG(rdata, "work event %u\n", event);
271
629f4427 272 switch (event) {
4c0f62b5 273 case RPORT_EV_READY:
f211fa51 274 ids = rdata->ids;
5f7ea3b7 275 rdata->event = RPORT_EV_NONE;
f034260d 276 rdata->major_retries = 0;
9e9d0452 277 kref_get(&rdata->kref);
42e9a92f
RL
278 mutex_unlock(&rdata->rp_mutex);
279
9e9d0452
JE
280 if (!rport)
281 rport = fc_remote_port_add(lport->host, 0, &ids);
282 if (!rport) {
283 FC_RPORT_DBG(rdata, "Failed to add the rport\n");
284 lport->tt.rport_logoff(rdata);
285 kref_put(&rdata->kref, lport->tt.rport_destroy);
286 return;
42e9a92f 287 }
9e9d0452
JE
288 mutex_lock(&rdata->rp_mutex);
289 if (rdata->rport)
290 FC_RPORT_DBG(rdata, "rport already allocated\n");
291 rdata->rport = rport;
292 rport->maxframe_size = rdata->maxframe_size;
293 rport->supported_classes = rdata->supported_classes;
294
3a3b42bf
RL
295 rpriv = rport->dd_data;
296 rpriv->local_port = lport;
297 rpriv->rp_state = rdata->rp_state;
298 rpriv->flags = rdata->flags;
299 rpriv->e_d_tov = rdata->e_d_tov;
300 rpriv->r_a_tov = rdata->r_a_tov;
9e9d0452
JE
301 mutex_unlock(&rdata->rp_mutex);
302
8345592b 303 if (rport_ops && rport_ops->event_callback) {
9e9d0452 304 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
9fb9d328 305 rport_ops->event_callback(lport, rdata, event);
9e9d0452
JE
306 }
307 kref_put(&rdata->kref, lport->tt.rport_destroy);
629f4427
JE
308 break;
309
310 case RPORT_EV_FAILED:
311 case RPORT_EV_LOGO:
312 case RPORT_EV_STOP:
9e9d0452 313 port_id = rdata->ids.port_id;
42e9a92f 314 mutex_unlock(&rdata->rp_mutex);
9e9d0452 315
8345592b 316 if (rport_ops && rport_ops->event_callback) {
9e9d0452 317 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
9fb9d328 318 rport_ops->event_callback(lport, rdata, event);
9e9d0452 319 }
201e5795 320 cancel_delayed_work_sync(&rdata->retry_work);
9e9d0452
JE
321
322 /*
323 * Reset any outstanding exchanges before freeing rport.
324 */
325 lport->tt.exch_mgr_reset(lport, 0, port_id);
326 lport->tt.exch_mgr_reset(lport, port_id, 0);
327
328 if (rport) {
3a3b42bf
RL
329 rpriv = rport->dd_data;
330 rpriv->rp_state = RPORT_ST_DELETE;
9e9d0452
JE
331 mutex_lock(&rdata->rp_mutex);
332 rdata->rport = NULL;
333 mutex_unlock(&rdata->rp_mutex);
42e9a92f 334 fc_remote_port_delete(rport);
571f824c 335 }
4b2164d4
JE
336
337 mutex_lock(&lport->disc.disc_mutex);
338 mutex_lock(&rdata->rp_mutex);
339 if (rdata->rp_state == RPORT_ST_DELETE) {
340 if (port_id == FC_FID_DIR_SERV) {
341 rdata->event = RPORT_EV_NONE;
342 mutex_unlock(&rdata->rp_mutex);
f034260d
JE
343 } else if ((rdata->flags & FC_RP_STARTED) &&
344 rdata->major_retries <
345 lport->max_rport_retry_count) {
346 rdata->major_retries++;
4b2164d4
JE
347 rdata->event = RPORT_EV_NONE;
348 FC_RPORT_DBG(rdata, "work restart\n");
a7b12a27 349 fc_rport_enter_flogi(rdata);
4b2164d4
JE
350 mutex_unlock(&rdata->rp_mutex);
351 } else {
352 FC_RPORT_DBG(rdata, "work delete\n");
42e90414 353 list_del_rcu(&rdata->peers);
4b2164d4
JE
354 mutex_unlock(&rdata->rp_mutex);
355 kref_put(&rdata->kref, lport->tt.rport_destroy);
356 }
357 } else {
358 /*
359 * Re-open for events. Reissue READY event if ready.
360 */
361 rdata->event = RPORT_EV_NONE;
362 if (rdata->rp_state == RPORT_ST_READY)
363 fc_rport_enter_ready(rdata);
b4a9c7ed 364 mutex_unlock(&rdata->rp_mutex);
4b2164d4
JE
365 }
366 mutex_unlock(&lport->disc.disc_mutex);
629f4427
JE
367 break;
368
369 default:
42e9a92f 370 mutex_unlock(&rdata->rp_mutex);
629f4427
JE
371 break;
372 }
42e9a92f
RL
373}
374
375/**
34f42a07 376 * fc_rport_login() - Start the remote port login state machine
3a3b42bf 377 * @rdata: The remote port to be logged in to
42e9a92f
RL
378 *
379 * Locking Note: Called without the rport lock held. This
380 * function will hold the rport lock, call an _enter_*
381 * function and then unlock the rport.
370c3bd0
JE
382 *
383 * This indicates the intent to be logged into the remote port.
384 * If it appears we are already logged in, ADISC is used to verify
385 * the setup.
42e9a92f 386 */
9fb9d328 387int fc_rport_login(struct fc_rport_priv *rdata)
42e9a92f 388{
42e9a92f
RL
389 mutex_lock(&rdata->rp_mutex);
390
4b2164d4 391 rdata->flags |= FC_RP_STARTED;
370c3bd0
JE
392 switch (rdata->rp_state) {
393 case RPORT_ST_READY:
394 FC_RPORT_DBG(rdata, "ADISC port\n");
395 fc_rport_enter_adisc(rdata);
396 break;
b4a9c7ed
JE
397 case RPORT_ST_DELETE:
398 FC_RPORT_DBG(rdata, "Restart deleted port\n");
b4a9c7ed 399 break;
370c3bd0
JE
400 default:
401 FC_RPORT_DBG(rdata, "Login to port\n");
a7b12a27 402 fc_rport_enter_flogi(rdata);
370c3bd0
JE
403 break;
404 }
42e9a92f
RL
405 mutex_unlock(&rdata->rp_mutex);
406
407 return 0;
408}
409
5f7ea3b7 410/**
3a3b42bf
RL
411 * fc_rport_enter_delete() - Schedule a remote port to be deleted
412 * @rdata: The remote port to be deleted
413 * @event: The event to report as the reason for deletion
5f7ea3b7
JE
414 *
415 * Locking Note: Called with the rport lock held.
416 *
417 * Allow state change into DELETE only once.
418 *
419 * Call queue_work only if there's no event already pending.
420 * Set the new event so that the old pending event will not occur.
421 * Since we have the mutex, even if fc_rport_work() is already started,
422 * it'll see the new event.
423 */
9fb9d328 424static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
5f7ea3b7
JE
425 enum fc_rport_event event)
426{
5f7ea3b7
JE
427 if (rdata->rp_state == RPORT_ST_DELETE)
428 return;
429
9fb9d328 430 FC_RPORT_DBG(rdata, "Delete port\n");
5f7ea3b7 431
9fb9d328 432 fc_rport_state_enter(rdata, RPORT_ST_DELETE);
5f7ea3b7
JE
433
434 if (rdata->event == RPORT_EV_NONE)
435 queue_work(rport_event_queue, &rdata->event_work);
436 rdata->event = event;
437}
438
42e9a92f 439/**
3a3b42bf
RL
440 * fc_rport_logoff() - Logoff and remove a remote port
441 * @rdata: The remote port to be logged off of
42e9a92f
RL
442 *
443 * Locking Note: Called without the rport lock held. This
444 * function will hold the rport lock, call an _enter_*
445 * function and then unlock the rport.
446 */
9fb9d328 447int fc_rport_logoff(struct fc_rport_priv *rdata)
42e9a92f 448{
42e9a92f
RL
449 mutex_lock(&rdata->rp_mutex);
450
9fb9d328 451 FC_RPORT_DBG(rdata, "Remove port\n");
42e9a92f 452
4b2164d4 453 rdata->flags &= ~FC_RP_STARTED;
14194054 454 if (rdata->rp_state == RPORT_ST_DELETE) {
9fb9d328 455 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
b4c6f546
AJ
456 goto out;
457 }
4b2164d4 458 fc_rport_enter_logo(rdata);
42e9a92f
RL
459
460 /*
14194054 461 * Change the state to Delete so that we discard
42e9a92f
RL
462 * the response.
463 */
9fb9d328 464 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
b4c6f546 465out:
b4a9c7ed 466 mutex_unlock(&rdata->rp_mutex);
42e9a92f
RL
467 return 0;
468}
469
470/**
3a3b42bf
RL
471 * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state
472 * @rdata: The remote port that is ready
42e9a92f
RL
473 *
474 * Locking Note: The rport lock is expected to be held before calling
475 * this routine.
476 */
9fb9d328 477static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
42e9a92f 478{
9fb9d328 479 fc_rport_state_enter(rdata, RPORT_ST_READY);
42e9a92f 480
9fb9d328 481 FC_RPORT_DBG(rdata, "Port is Ready\n");
42e9a92f 482
5f7ea3b7
JE
483 if (rdata->event == RPORT_EV_NONE)
484 queue_work(rport_event_queue, &rdata->event_work);
4c0f62b5 485 rdata->event = RPORT_EV_READY;
42e9a92f
RL
486}
487
488/**
3a3b42bf
RL
489 * fc_rport_timeout() - Handler for the retry_work timer
490 * @work: Handle to the remote port that has timed out
42e9a92f
RL
491 *
492 * Locking Note: Called without the rport lock held. This
493 * function will hold the rport lock, call an _enter_*
494 * function and then unlock the rport.
495 */
496static void fc_rport_timeout(struct work_struct *work)
497{
ab28f1fd
JE
498 struct fc_rport_priv *rdata =
499 container_of(work, struct fc_rport_priv, retry_work.work);
42e9a92f
RL
500
501 mutex_lock(&rdata->rp_mutex);
502
503 switch (rdata->rp_state) {
a7b12a27
JE
504 case RPORT_ST_FLOGI:
505 fc_rport_enter_flogi(rdata);
506 break;
42e9a92f 507 case RPORT_ST_PLOGI:
9fb9d328 508 fc_rport_enter_plogi(rdata);
42e9a92f
RL
509 break;
510 case RPORT_ST_PRLI:
9fb9d328 511 fc_rport_enter_prli(rdata);
42e9a92f
RL
512 break;
513 case RPORT_ST_RTV:
9fb9d328 514 fc_rport_enter_rtv(rdata);
42e9a92f 515 break;
370c3bd0
JE
516 case RPORT_ST_ADISC:
517 fc_rport_enter_adisc(rdata);
518 break;
a7b12a27 519 case RPORT_ST_PLOGI_WAIT:
42e9a92f
RL
520 case RPORT_ST_READY:
521 case RPORT_ST_INIT:
14194054 522 case RPORT_ST_DELETE:
42e9a92f
RL
523 break;
524 }
525
526 mutex_unlock(&rdata->rp_mutex);
42e9a92f
RL
527}
528
529/**
34f42a07 530 * fc_rport_error() - Error handler, called once retries have been exhausted
3a3b42bf
RL
531 * @rdata: The remote port the error is happened on
532 * @fp: The error code encapsulated in a frame pointer
42e9a92f 533 *
42e9a92f
RL
534 * Locking Note: The rport lock is expected to be held before
535 * calling this routine
536 */
9fb9d328 537static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
42e9a92f 538{
9fb9d328 539 FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
cdbe6dfe
JE
540 IS_ERR(fp) ? -PTR_ERR(fp) : 0,
541 fc_rport_state(rdata), rdata->retries);
42e9a92f 542
6755db1c 543 switch (rdata->rp_state) {
a7b12a27 544 case RPORT_ST_FLOGI:
6755db1c 545 case RPORT_ST_PLOGI:
4b2164d4 546 rdata->flags &= ~FC_RP_STARTED;
9fb9d328 547 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
6755db1c
CL
548 break;
549 case RPORT_ST_RTV:
9fb9d328 550 fc_rport_enter_ready(rdata);
6755db1c 551 break;
370c3bd0
JE
552 case RPORT_ST_PRLI:
553 case RPORT_ST_ADISC:
554 fc_rport_enter_logo(rdata);
555 break;
a7b12a27 556 case RPORT_ST_PLOGI_WAIT:
14194054 557 case RPORT_ST_DELETE:
6755db1c
CL
558 case RPORT_ST_READY:
559 case RPORT_ST_INIT:
560 break;
42e9a92f
RL
561 }
562}
563
6755db1c 564/**
3a3b42bf
RL
565 * fc_rport_error_retry() - Handler for remote port state retries
566 * @rdata: The remote port whose state is to be retried
567 * @fp: The error code encapsulated in a frame pointer
6755db1c
CL
568 *
569 * If the error was an exchange timeout retry immediately,
570 * otherwise wait for E_D_TOV.
571 *
572 * Locking Note: The rport lock is expected to be held before
573 * calling this routine
574 */
9fb9d328
JE
575static void fc_rport_error_retry(struct fc_rport_priv *rdata,
576 struct fc_frame *fp)
6755db1c 577{
6755db1c
CL
578 unsigned long delay = FC_DEF_E_D_TOV;
579
580 /* make sure this isn't an FC_EX_CLOSED error, never retry those */
581 if (PTR_ERR(fp) == -FC_EX_CLOSED)
9fb9d328 582 return fc_rport_error(rdata, fp);
6755db1c 583
a3666955 584 if (rdata->retries < rdata->local_port->max_rport_retry_count) {
9fb9d328
JE
585 FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
586 PTR_ERR(fp), fc_rport_state(rdata));
6755db1c
CL
587 rdata->retries++;
588 /* no additional delay on exchange timeouts */
589 if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
590 delay = 0;
6755db1c
CL
591 schedule_delayed_work(&rdata->retry_work, delay);
592 return;
593 }
594
9fb9d328 595 return fc_rport_error(rdata, fp);
6755db1c
CL
596}
597
42e9a92f 598/**
a7b12a27
JE
599 * fc_rport_login_complete() - Handle parameters and completion of p-mp login.
600 * @rdata: The remote port which we logged into or which logged into us.
601 * @fp: The FLOGI or PLOGI request or response frame
602 *
603 * Returns non-zero error if a problem is detected with the frame.
604 * Does not free the frame.
605 *
606 * This is only used in point-to-multipoint mode for FIP currently.
607 */
608static int fc_rport_login_complete(struct fc_rport_priv *rdata,
609 struct fc_frame *fp)
610{
611 struct fc_lport *lport = rdata->local_port;
612 struct fc_els_flogi *flogi;
613 unsigned int e_d_tov;
614 u16 csp_flags;
615
616 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
617 if (!flogi)
618 return -EINVAL;
619
620 csp_flags = ntohs(flogi->fl_csp.sp_features);
621
622 if (fc_frame_payload_op(fp) == ELS_FLOGI) {
623 if (csp_flags & FC_SP_FT_FPORT) {
624 FC_RPORT_DBG(rdata, "Fabric bit set in FLOGI\n");
625 return -EINVAL;
626 }
627 } else {
628
629 /*
630 * E_D_TOV is not valid on an incoming FLOGI request.
631 */
632 e_d_tov = ntohl(flogi->fl_csp.sp_e_d_tov);
633 if (csp_flags & FC_SP_FT_EDTR)
634 e_d_tov /= 1000000;
635 if (e_d_tov > rdata->e_d_tov)
636 rdata->e_d_tov = e_d_tov;
637 }
638 rdata->maxframe_size = fc_plogi_get_maxframe(flogi, lport->mfs);
639 return 0;
640}
641
642/**
643 * fc_rport_flogi_resp() - Handle response to FLOGI request for p-mp mode
644 * @sp: The sequence that the FLOGI was on
645 * @fp: The FLOGI response frame
646 * @rp_arg: The remote port that received the FLOGI response
647 */
648void fc_rport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
649 void *rp_arg)
650{
651 struct fc_rport_priv *rdata = rp_arg;
652 struct fc_lport *lport = rdata->local_port;
653 struct fc_els_flogi *flogi;
654 unsigned int r_a_tov;
655
656 FC_RPORT_DBG(rdata, "Received a FLOGI %s\n", fc_els_resp_type(fp));
657
658 if (fp == ERR_PTR(-FC_EX_CLOSED))
659 return;
660
661 mutex_lock(&rdata->rp_mutex);
662
663 if (rdata->rp_state != RPORT_ST_FLOGI) {
664 FC_RPORT_DBG(rdata, "Received a FLOGI response, but in state "
665 "%s\n", fc_rport_state(rdata));
666 if (IS_ERR(fp))
667 goto err;
668 goto out;
669 }
670
671 if (IS_ERR(fp)) {
672 fc_rport_error(rdata, fp);
673 goto err;
674 }
675
676 if (fc_frame_payload_op(fp) != ELS_LS_ACC)
677 goto bad;
678 if (fc_rport_login_complete(rdata, fp))
679 goto bad;
680
681 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
682 if (!flogi)
683 goto bad;
684 r_a_tov = ntohl(flogi->fl_csp.sp_r_a_tov);
685 if (r_a_tov > rdata->r_a_tov)
686 rdata->r_a_tov = r_a_tov;
687
688 if (rdata->ids.port_name < lport->wwpn)
689 fc_rport_enter_plogi(rdata);
690 else
691 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
692out:
693 fc_frame_free(fp);
694err:
695 mutex_unlock(&rdata->rp_mutex);
696 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
697 return;
698bad:
699 FC_RPORT_DBG(rdata, "Bad FLOGI response\n");
700 fc_rport_error_retry(rdata, fp);
701 goto out;
702}
703
704/**
705 * fc_rport_enter_flogi() - Send a FLOGI request to the remote port for p-mp
706 * @rdata: The remote port to send a FLOGI to
707 *
708 * Locking Note: The rport lock is expected to be held before calling
709 * this routine.
710 */
711static void fc_rport_enter_flogi(struct fc_rport_priv *rdata)
712{
713 struct fc_lport *lport = rdata->local_port;
714 struct fc_frame *fp;
715
716 if (!lport->point_to_multipoint)
717 return fc_rport_enter_plogi(rdata);
718
719 FC_RPORT_DBG(rdata, "Entered FLOGI state from %s state\n",
720 fc_rport_state(rdata));
721
722 fc_rport_state_enter(rdata, RPORT_ST_FLOGI);
723
724 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
725 if (!fp)
726 return fc_rport_error_retry(rdata, fp);
727
728 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_FLOGI,
729 fc_rport_flogi_resp, rdata,
730 2 * lport->r_a_tov))
731 fc_rport_error_retry(rdata, NULL);
732 else
733 kref_get(&rdata->kref);
734}
735
736/**
737 * fc_rport_recv_flogi_req() - Handle Fabric Login (FLOGI) request in p-mp mode
738 * @lport: The local port that received the PLOGI request
739 * @sp: The sequence that the PLOGI request was on
740 * @rx_fp: The PLOGI request frame
741 */
742static void fc_rport_recv_flogi_req(struct fc_lport *lport,
743 struct fc_seq *sp, struct fc_frame *rx_fp)
744{
745 struct fc_disc *disc;
746 struct fc_els_flogi *flp;
747 struct fc_rport_priv *rdata;
748 struct fc_frame *fp = rx_fp;
a7b12a27 749 struct fc_seq_els_data rjt_data;
24f089e2 750 u32 sid;
a7b12a27
JE
751
752 rjt_data.fp = NULL;
251748a9 753 sid = fc_frame_sid(fp);
a7b12a27
JE
754
755 FC_RPORT_ID_DBG(lport, sid, "Received FLOGI request\n");
756
757 disc = &lport->disc;
758 mutex_lock(&disc->disc_mutex);
759
760 if (!lport->point_to_multipoint) {
761 rjt_data.reason = ELS_RJT_UNSUP;
762 rjt_data.explan = ELS_EXPL_NONE;
763 goto reject;
764 }
765
766 flp = fc_frame_payload_get(fp, sizeof(*flp));
767 if (!flp) {
768 rjt_data.reason = ELS_RJT_LOGIC;
769 rjt_data.explan = ELS_EXPL_INV_LEN;
770 goto reject;
771 }
772
773 rdata = lport->tt.rport_lookup(lport, sid);
774 if (!rdata) {
775 rjt_data.reason = ELS_RJT_FIP;
776 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
777 goto reject;
778 }
779 mutex_lock(&rdata->rp_mutex);
780
781 FC_RPORT_DBG(rdata, "Received FLOGI in %s state\n",
782 fc_rport_state(rdata));
783
784 switch (rdata->rp_state) {
785 case RPORT_ST_INIT:
a7b12a27
JE
786 case RPORT_ST_DELETE:
787 mutex_unlock(&rdata->rp_mutex);
788 rjt_data.reason = ELS_RJT_FIP;
789 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
790 goto reject;
791 case RPORT_ST_FLOGI:
792 case RPORT_ST_PLOGI_WAIT:
793 case RPORT_ST_PLOGI:
794 break;
795 case RPORT_ST_PRLI:
796 case RPORT_ST_RTV:
797 case RPORT_ST_READY:
798 case RPORT_ST_ADISC:
799 /*
800 * Set the remote port to be deleted and to then restart.
801 * This queues work to be sure exchanges are reset.
802 */
803 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
804 mutex_unlock(&rdata->rp_mutex);
805 rjt_data.reason = ELS_RJT_BUSY;
806 rjt_data.explan = ELS_EXPL_NONE;
807 goto reject;
808 }
809 if (fc_rport_login_complete(rdata, fp)) {
810 mutex_unlock(&rdata->rp_mutex);
811 rjt_data.reason = ELS_RJT_LOGIC;
812 rjt_data.explan = ELS_EXPL_NONE;
813 goto reject;
814 }
a7b12a27
JE
815
816 fp = fc_frame_alloc(lport, sizeof(*flp));
817 if (!fp)
818 goto out;
819
820 sp = lport->tt.seq_start_next(sp);
821 fc_flogi_fill(lport, fp);
822 flp = fc_frame_payload_get(fp, sizeof(*flp));
823 flp->fl_cmd = ELS_LS_ACC;
824
24f089e2
JE
825 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
826 lport->tt.frame_send(lport, fp);
a7b12a27
JE
827
828 if (rdata->ids.port_name < lport->wwpn)
829 fc_rport_enter_plogi(rdata);
830 else
831 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
832out:
833 mutex_unlock(&rdata->rp_mutex);
834 mutex_unlock(&disc->disc_mutex);
24f089e2 835 fc_frame_free(rx_fp);
a7b12a27
JE
836 return;
837
838reject:
839 mutex_unlock(&disc->disc_mutex);
840 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
24f089e2 841 fc_frame_free(rx_fp);
a7b12a27
JE
842}
843
844/**
845 * fc_rport_plogi_resp() - Handler for ELS PLOGI responses
3a3b42bf
RL
846 * @sp: The sequence the PLOGI is on
847 * @fp: The PLOGI response frame
848 * @rdata_arg: The remote port that sent the PLOGI response
42e9a92f
RL
849 *
850 * Locking Note: This function will be called without the rport lock
851 * held, but it will lock, call an _enter_* function or fc_rport_error
852 * and then unlock the rport.
853 */
854static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
9fb9d328 855 void *rdata_arg)
42e9a92f 856{
9fb9d328 857 struct fc_rport_priv *rdata = rdata_arg;
42e9a92f 858 struct fc_lport *lport = rdata->local_port;
a29e7646 859 struct fc_els_flogi *plp = NULL;
42e9a92f
RL
860 u16 csp_seq;
861 u16 cssp_seq;
862 u8 op;
863
864 mutex_lock(&rdata->rp_mutex);
865
f657d299 866 FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
42e9a92f
RL
867
868 if (rdata->rp_state != RPORT_ST_PLOGI) {
9fb9d328
JE
869 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
870 "%s\n", fc_rport_state(rdata));
76f6804e
AJ
871 if (IS_ERR(fp))
872 goto err;
42e9a92f
RL
873 goto out;
874 }
875
76f6804e 876 if (IS_ERR(fp)) {
9fb9d328 877 fc_rport_error_retry(rdata, fp);
76f6804e
AJ
878 goto err;
879 }
880
42e9a92f
RL
881 op = fc_frame_payload_op(fp);
882 if (op == ELS_LS_ACC &&
883 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
f211fa51
JE
884 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
885 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
42e9a92f 886
a7b12a27
JE
887 if (lport->point_to_multipoint)
888 fc_rport_login_complete(rdata, fp);
42e9a92f
RL
889 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
890 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
891 if (cssp_seq < csp_seq)
892 csp_seq = cssp_seq;
893 rdata->max_seq = csp_seq;
f211fa51 894 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
3ac6f98f 895 fc_rport_enter_prli(rdata);
42e9a92f 896 } else
9fb9d328 897 fc_rport_error_retry(rdata, fp);
42e9a92f
RL
898
899out:
900 fc_frame_free(fp);
901err:
902 mutex_unlock(&rdata->rp_mutex);
f211fa51 903 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
42e9a92f
RL
904}
905
906/**
3a3b42bf
RL
907 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
908 * @rdata: The remote port to send a PLOGI to
42e9a92f
RL
909 *
910 * Locking Note: The rport lock is expected to be held before calling
911 * this routine.
912 */
9fb9d328 913static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
42e9a92f 914{
42e9a92f
RL
915 struct fc_lport *lport = rdata->local_port;
916 struct fc_frame *fp;
917
9fb9d328
JE
918 FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
919 fc_rport_state(rdata));
42e9a92f 920
9fb9d328 921 fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
42e9a92f 922
f211fa51 923 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
42e9a92f
RL
924 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
925 if (!fp) {
a7b12a27 926 FC_RPORT_DBG(rdata, "%s frame alloc failed\n", __func__);
9fb9d328 927 fc_rport_error_retry(rdata, fp);
42e9a92f
RL
928 return;
929 }
930 rdata->e_d_tov = lport->e_d_tov;
931
f211fa51 932 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
b94f8951
JE
933 fc_rport_plogi_resp, rdata,
934 2 * lport->r_a_tov))
8f550f93 935 fc_rport_error_retry(rdata, NULL);
42e9a92f 936 else
f211fa51 937 kref_get(&rdata->kref);
42e9a92f
RL
938}
939
940/**
34f42a07 941 * fc_rport_prli_resp() - Process Login (PRLI) response handler
3a3b42bf
RL
942 * @sp: The sequence the PRLI response was on
943 * @fp: The PRLI response frame
944 * @rdata_arg: The remote port that sent the PRLI response
42e9a92f
RL
945 *
946 * Locking Note: This function will be called without the rport lock
947 * held, but it will lock, call an _enter_* function or fc_rport_error
948 * and then unlock the rport.
949 */
950static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
9fb9d328 951 void *rdata_arg)
42e9a92f 952{
9fb9d328 953 struct fc_rport_priv *rdata = rdata_arg;
42e9a92f
RL
954 struct {
955 struct fc_els_prli prli;
956 struct fc_els_spp spp;
957 } *pp;
958 u32 roles = FC_RPORT_ROLE_UNKNOWN;
959 u32 fcp_parm = 0;
960 u8 op;
618461c0 961 u8 resp_code = 0;
42e9a92f
RL
962
963 mutex_lock(&rdata->rp_mutex);
964
f657d299 965 FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
42e9a92f
RL
966
967 if (rdata->rp_state != RPORT_ST_PRLI) {
9fb9d328
JE
968 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
969 "%s\n", fc_rport_state(rdata));
76f6804e
AJ
970 if (IS_ERR(fp))
971 goto err;
42e9a92f
RL
972 goto out;
973 }
974
76f6804e 975 if (IS_ERR(fp)) {
9fb9d328 976 fc_rport_error_retry(rdata, fp);
76f6804e
AJ
977 goto err;
978 }
979
6bd054cb
RL
980 /* reinitialize remote port roles */
981 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
982
42e9a92f
RL
983 op = fc_frame_payload_op(fp);
984 if (op == ELS_LS_ACC) {
985 pp = fc_frame_payload_get(fp, sizeof(*pp));
618461c0
BPG
986 if (!pp)
987 goto out;
988
989 resp_code = (pp->spp.spp_flags & FC_SPP_RESP_MASK);
990 FC_RPORT_DBG(rdata, "PRLI spp_flags = 0x%x\n",
991 pp->spp.spp_flags);
992 if (resp_code != FC_SPP_RESP_ACK) {
993 if (resp_code == FC_SPP_RESP_CONF)
994 fc_rport_error(rdata, fp);
995 else
996 fc_rport_error_retry(rdata, fp);
997 goto out;
42e9a92f 998 }
618461c0
BPG
999 if (pp->prli.prli_spp_len < sizeof(pp->spp))
1000 goto out;
1001
1002 fcp_parm = ntohl(pp->spp.spp_params);
1003 if (fcp_parm & FCP_SPPF_RETRY)
1004 rdata->flags |= FC_RP_FLAGS_RETRY;
42e9a92f 1005
f211fa51 1006 rdata->supported_classes = FC_COS_CLASS3;
42e9a92f
RL
1007 if (fcp_parm & FCP_SPPF_INIT_FCN)
1008 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1009 if (fcp_parm & FCP_SPPF_TARG_FCN)
1010 roles |= FC_RPORT_ROLE_FCP_TARGET;
1011
f211fa51 1012 rdata->ids.roles = roles;
9fb9d328 1013 fc_rport_enter_rtv(rdata);
42e9a92f
RL
1014
1015 } else {
9fb9d328 1016 FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
292e40b9 1017 fc_rport_error_retry(rdata, fp);
42e9a92f
RL
1018 }
1019
1020out:
1021 fc_frame_free(fp);
1022err:
1023 mutex_unlock(&rdata->rp_mutex);
f211fa51 1024 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
42e9a92f
RL
1025}
1026
42e9a92f 1027/**
3a3b42bf
RL
1028 * fc_rport_enter_prli() - Send Process Login (PRLI) request
1029 * @rdata: The remote port to send the PRLI request to
42e9a92f
RL
1030 *
1031 * Locking Note: The rport lock is expected to be held before calling
1032 * this routine.
1033 */
9fb9d328 1034static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
42e9a92f 1035{
42e9a92f
RL
1036 struct fc_lport *lport = rdata->local_port;
1037 struct {
1038 struct fc_els_prli prli;
1039 struct fc_els_spp spp;
1040 } *pp;
1041 struct fc_frame *fp;
1042
3ac6f98f
JE
1043 /*
1044 * If the rport is one of the well known addresses
1045 * we skip PRLI and RTV and go straight to READY.
1046 */
1047 if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
1048 fc_rport_enter_ready(rdata);
1049 return;
1050 }
1051
9fb9d328
JE
1052 FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
1053 fc_rport_state(rdata));
42e9a92f 1054
9fb9d328 1055 fc_rport_state_enter(rdata, RPORT_ST_PRLI);
42e9a92f
RL
1056
1057 fp = fc_frame_alloc(lport, sizeof(*pp));
1058 if (!fp) {
9fb9d328 1059 fc_rport_error_retry(rdata, fp);
42e9a92f
RL
1060 return;
1061 }
1062
f211fa51 1063 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PRLI,
b94f8951
JE
1064 fc_rport_prli_resp, rdata,
1065 2 * lport->r_a_tov))
8f550f93 1066 fc_rport_error_retry(rdata, NULL);
42e9a92f 1067 else
f211fa51 1068 kref_get(&rdata->kref);
42e9a92f
RL
1069}
1070
1071/**
3a3b42bf
RL
1072 * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses
1073 * @sp: The sequence the RTV was on
1074 * @fp: The RTV response frame
1075 * @rdata_arg: The remote port that sent the RTV response
42e9a92f
RL
1076 *
1077 * Many targets don't seem to support this.
1078 *
1079 * Locking Note: This function will be called without the rport lock
1080 * held, but it will lock, call an _enter_* function or fc_rport_error
1081 * and then unlock the rport.
1082 */
1083static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
9fb9d328 1084 void *rdata_arg)
42e9a92f 1085{
9fb9d328 1086 struct fc_rport_priv *rdata = rdata_arg;
42e9a92f
RL
1087 u8 op;
1088
1089 mutex_lock(&rdata->rp_mutex);
1090
f657d299 1091 FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
42e9a92f
RL
1092
1093 if (rdata->rp_state != RPORT_ST_RTV) {
9fb9d328
JE
1094 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
1095 "%s\n", fc_rport_state(rdata));
76f6804e
AJ
1096 if (IS_ERR(fp))
1097 goto err;
42e9a92f
RL
1098 goto out;
1099 }
1100
76f6804e 1101 if (IS_ERR(fp)) {
9fb9d328 1102 fc_rport_error(rdata, fp);
76f6804e
AJ
1103 goto err;
1104 }
1105
42e9a92f
RL
1106 op = fc_frame_payload_op(fp);
1107 if (op == ELS_LS_ACC) {
1108 struct fc_els_rtv_acc *rtv;
1109 u32 toq;
1110 u32 tov;
1111
1112 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
1113 if (rtv) {
1114 toq = ntohl(rtv->rtv_toq);
1115 tov = ntohl(rtv->rtv_r_a_tov);
1116 if (tov == 0)
1117 tov = 1;
1118 rdata->r_a_tov = tov;
1119 tov = ntohl(rtv->rtv_e_d_tov);
1120 if (toq & FC_ELS_RTV_EDRES)
1121 tov /= 1000000;
1122 if (tov == 0)
1123 tov = 1;
1124 rdata->e_d_tov = tov;
1125 }
1126 }
1127
9fb9d328 1128 fc_rport_enter_ready(rdata);
42e9a92f
RL
1129
1130out:
1131 fc_frame_free(fp);
1132err:
1133 mutex_unlock(&rdata->rp_mutex);
f211fa51 1134 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
42e9a92f
RL
1135}
1136
1137/**
3a3b42bf
RL
1138 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
1139 * @rdata: The remote port to send the RTV request to
42e9a92f
RL
1140 *
1141 * Locking Note: The rport lock is expected to be held before calling
1142 * this routine.
1143 */
9fb9d328 1144static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
42e9a92f
RL
1145{
1146 struct fc_frame *fp;
42e9a92f
RL
1147 struct fc_lport *lport = rdata->local_port;
1148
9fb9d328
JE
1149 FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
1150 fc_rport_state(rdata));
42e9a92f 1151
9fb9d328 1152 fc_rport_state_enter(rdata, RPORT_ST_RTV);
42e9a92f
RL
1153
1154 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
1155 if (!fp) {
9fb9d328 1156 fc_rport_error_retry(rdata, fp);
42e9a92f
RL
1157 return;
1158 }
1159
f211fa51 1160 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
b94f8951
JE
1161 fc_rport_rtv_resp, rdata,
1162 2 * lport->r_a_tov))
8f550f93 1163 fc_rport_error_retry(rdata, NULL);
42e9a92f 1164 else
f211fa51 1165 kref_get(&rdata->kref);
42e9a92f
RL
1166}
1167
079ecd8c
JE
1168/**
1169 * fc_rport_logo_resp() - Handler for logout (LOGO) responses
1170 * @sp: The sequence the LOGO was on
1171 * @fp: The LOGO response frame
1172 * @lport_arg: The local port
1173 */
1174static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
1175 void *lport_arg)
1176{
1177 struct fc_lport *lport = lport_arg;
1178
1179 FC_RPORT_ID_DBG(lport, fc_seq_exch(sp)->did,
1180 "Received a LOGO %s\n", fc_els_resp_type(fp));
1181 if (IS_ERR(fp))
1182 return;
1183 fc_frame_free(fp);
1184}
1185
42e9a92f 1186/**
3a3b42bf
RL
1187 * fc_rport_enter_logo() - Send a logout (LOGO) request
1188 * @rdata: The remote port to send the LOGO request to
42e9a92f
RL
1189 *
1190 * Locking Note: The rport lock is expected to be held before calling
1191 * this routine.
1192 */
9fb9d328 1193static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
42e9a92f 1194{
42e9a92f
RL
1195 struct fc_lport *lport = rdata->local_port;
1196 struct fc_frame *fp;
1197
079ecd8c 1198 FC_RPORT_DBG(rdata, "Port sending LOGO from %s state\n",
9fb9d328 1199 fc_rport_state(rdata));
42e9a92f 1200
42e9a92f 1201 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
079ecd8c 1202 if (!fp)
42e9a92f 1203 return;
079ecd8c
JE
1204 (void)lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
1205 fc_rport_logo_resp, lport, 0);
42e9a92f
RL
1206}
1207
370c3bd0 1208/**
3a3b42bf
RL
1209 * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
1210 * @sp: The sequence the ADISC response was on
1211 * @fp: The ADISC response frame
1212 * @rdata_arg: The remote port that sent the ADISC response
370c3bd0
JE
1213 *
1214 * Locking Note: This function will be called without the rport lock
1215 * held, but it will lock, call an _enter_* function or fc_rport_error
1216 * and then unlock the rport.
1217 */
1218static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
3a3b42bf 1219 void *rdata_arg)
370c3bd0
JE
1220{
1221 struct fc_rport_priv *rdata = rdata_arg;
1222 struct fc_els_adisc *adisc;
1223 u8 op;
1224
1225 mutex_lock(&rdata->rp_mutex);
1226
1227 FC_RPORT_DBG(rdata, "Received a ADISC response\n");
1228
1229 if (rdata->rp_state != RPORT_ST_ADISC) {
1230 FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
1231 fc_rport_state(rdata));
1232 if (IS_ERR(fp))
1233 goto err;
1234 goto out;
1235 }
1236
1237 if (IS_ERR(fp)) {
1238 fc_rport_error(rdata, fp);
1239 goto err;
1240 }
1241
1242 /*
1243 * If address verification failed. Consider us logged out of the rport.
1244 * Since the rport is still in discovery, we want to be
1245 * logged in, so go to PLOGI state. Otherwise, go back to READY.
1246 */
1247 op = fc_frame_payload_op(fp);
1248 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1249 if (op != ELS_LS_ACC || !adisc ||
1250 ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
1251 get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
1252 get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
1253 FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
a7b12a27 1254 fc_rport_enter_flogi(rdata);
370c3bd0
JE
1255 } else {
1256 FC_RPORT_DBG(rdata, "ADISC OK\n");
1257 fc_rport_enter_ready(rdata);
1258 }
1259out:
1260 fc_frame_free(fp);
1261err:
1262 mutex_unlock(&rdata->rp_mutex);
1263 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1264}
1265
1266/**
3a3b42bf
RL
1267 * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
1268 * @rdata: The remote port to send the ADISC request to
370c3bd0
JE
1269 *
1270 * Locking Note: The rport lock is expected to be held before calling
1271 * this routine.
1272 */
1273static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
1274{
1275 struct fc_lport *lport = rdata->local_port;
1276 struct fc_frame *fp;
1277
1278 FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
1279 fc_rport_state(rdata));
1280
1281 fc_rport_state_enter(rdata, RPORT_ST_ADISC);
1282
1283 fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
1284 if (!fp) {
1285 fc_rport_error_retry(rdata, fp);
1286 return;
1287 }
1288 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
b94f8951
JE
1289 fc_rport_adisc_resp, rdata,
1290 2 * lport->r_a_tov))
8f550f93 1291 fc_rport_error_retry(rdata, NULL);
370c3bd0
JE
1292 else
1293 kref_get(&rdata->kref);
1294}
1295
8abbe3a4 1296/**
3a3b42bf
RL
1297 * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
1298 * @rdata: The remote port that sent the ADISC request
1299 * @sp: The sequence the ADISC request was on
1300 * @in_fp: The ADISC request frame
8abbe3a4
JE
1301 *
1302 * Locking Note: Called with the lport and rport locks held.
1303 */
1304static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
1305 struct fc_seq *sp, struct fc_frame *in_fp)
1306{
1307 struct fc_lport *lport = rdata->local_port;
1308 struct fc_frame *fp;
8abbe3a4
JE
1309 struct fc_els_adisc *adisc;
1310 struct fc_seq_els_data rjt_data;
8abbe3a4
JE
1311
1312 FC_RPORT_DBG(rdata, "Received ADISC request\n");
1313
1314 adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
1315 if (!adisc) {
1316 rjt_data.fp = NULL;
1317 rjt_data.reason = ELS_RJT_PROT;
1318 rjt_data.explan = ELS_EXPL_INV_LEN;
1319 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1320 goto drop;
1321 }
1322
1323 fp = fc_frame_alloc(lport, sizeof(*adisc));
1324 if (!fp)
1325 goto drop;
1326 fc_adisc_fill(lport, fp);
1327 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1328 adisc->adisc_cmd = ELS_LS_ACC;
24f089e2
JE
1329 fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
1330 lport->tt.frame_send(lport, fp);
8abbe3a4
JE
1331drop:
1332 fc_frame_free(in_fp);
1333}
1334
63e27fb8
YZ
1335/**
1336 * fc_rport_recv_rls_req() - Handle received Read Link Status request
1337 * @rdata: The remote port that sent the RLS request
1338 * @sp: The sequence that the RLS was on
1339 * @rx_fp: The PRLI request frame
1340 *
1341 * Locking Note: The rport lock is expected to be held before calling
1342 * this function.
1343 */
1344static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
1345 struct fc_seq *sp, struct fc_frame *rx_fp)
1346
1347{
1348 struct fc_lport *lport = rdata->local_port;
1349 struct fc_frame *fp;
63e27fb8
YZ
1350 struct fc_els_rls *rls;
1351 struct fc_els_rls_resp *rsp;
1352 struct fc_els_lesb *lesb;
1353 struct fc_seq_els_data rjt_data;
1354 struct fc_host_statistics *hst;
63e27fb8
YZ
1355
1356 FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
1357 fc_rport_state(rdata));
1358
1359 rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
1360 if (!rls) {
1361 rjt_data.reason = ELS_RJT_PROT;
1362 rjt_data.explan = ELS_EXPL_INV_LEN;
1363 goto out_rjt;
1364 }
1365
1366 fp = fc_frame_alloc(lport, sizeof(*rsp));
1367 if (!fp) {
1368 rjt_data.reason = ELS_RJT_UNAB;
1369 rjt_data.explan = ELS_EXPL_INSUF_RES;
1370 goto out_rjt;
1371 }
1372
1373 rsp = fc_frame_payload_get(fp, sizeof(*rsp));
1374 memset(rsp, 0, sizeof(*rsp));
1375 rsp->rls_cmd = ELS_LS_ACC;
1376 lesb = &rsp->rls_lesb;
1377 if (lport->tt.get_lesb) {
1378 /* get LESB from LLD if it supports it */
1379 lport->tt.get_lesb(lport, lesb);
1380 } else {
1381 fc_get_host_stats(lport->host);
1382 hst = &lport->host_stats;
1383 lesb->lesb_link_fail = htonl(hst->link_failure_count);
1384 lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
1385 lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
1386 lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
1387 lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
1388 lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
1389 }
1390
24f089e2
JE
1391 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1392 lport->tt.frame_send(lport, fp);
63e27fb8
YZ
1393 goto out;
1394
1395out_rjt:
1396 rjt_data.fp = NULL;
1397 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1398out:
1399 fc_frame_free(rx_fp);
1400}
1401
42e9a92f 1402/**
3a3b42bf
RL
1403 * fc_rport_recv_els_req() - Handler for validated ELS requests
1404 * @lport: The local port that received the ELS request
1405 * @sp: The sequence that the ELS request was on
1406 * @fp: The ELS request frame
83fe6a93
JE
1407 *
1408 * Handle incoming ELS requests that require port login.
1409 * The ELS opcode has already been validated by the caller.
42e9a92f 1410 *
131203a1 1411 * Locking Note: Called with the lport lock held.
42e9a92f 1412 */
83fe6a93
JE
1413static void fc_rport_recv_els_req(struct fc_lport *lport,
1414 struct fc_seq *sp, struct fc_frame *fp)
42e9a92f 1415{
131203a1 1416 struct fc_rport_priv *rdata;
42e9a92f 1417 struct fc_seq_els_data els_data;
42e9a92f 1418
42e9a92f 1419 els_data.fp = NULL;
83fe6a93
JE
1420 els_data.reason = ELS_RJT_UNAB;
1421 els_data.explan = ELS_EXPL_PLOGI_REQD;
3ac6f98f 1422
25b37b98 1423 mutex_lock(&lport->disc.disc_mutex);
251748a9 1424 rdata = lport->tt.rport_lookup(lport, fc_frame_sid(fp));
131203a1 1425 if (!rdata) {
25b37b98 1426 mutex_unlock(&lport->disc.disc_mutex);
83fe6a93 1427 goto reject;
131203a1
JE
1428 }
1429 mutex_lock(&rdata->rp_mutex);
25b37b98 1430 mutex_unlock(&lport->disc.disc_mutex);
131203a1 1431
83fe6a93
JE
1432 switch (rdata->rp_state) {
1433 case RPORT_ST_PRLI:
1434 case RPORT_ST_RTV:
1435 case RPORT_ST_READY:
370c3bd0 1436 case RPORT_ST_ADISC:
83fe6a93
JE
1437 break;
1438 default:
1439 mutex_unlock(&rdata->rp_mutex);
1440 goto reject;
1441 }
1442
1443 switch (fc_frame_payload_op(fp)) {
131203a1
JE
1444 case ELS_PRLI:
1445 fc_rport_recv_prli_req(rdata, sp, fp);
1446 break;
1447 case ELS_PRLO:
1448 fc_rport_recv_prlo_req(rdata, sp, fp);
1449 break;
8abbe3a4
JE
1450 case ELS_ADISC:
1451 fc_rport_recv_adisc_req(rdata, sp, fp);
1452 break;
131203a1
JE
1453 case ELS_RRQ:
1454 els_data.fp = fp;
1455 lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
1456 break;
1457 case ELS_REC:
1458 els_data.fp = fp;
1459 lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
1460 break;
63e27fb8
YZ
1461 case ELS_RLS:
1462 fc_rport_recv_rls_req(rdata, sp, fp);
1463 break;
131203a1 1464 default:
83fe6a93 1465 fc_frame_free(fp); /* can't happen */
131203a1 1466 break;
42e9a92f
RL
1467 }
1468
1469 mutex_unlock(&rdata->rp_mutex);
83fe6a93
JE
1470 return;
1471
1472reject:
1473 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
1474 fc_frame_free(fp);
1475}
1476
1477/**
3a3b42bf
RL
1478 * fc_rport_recv_req() - Handler for requests
1479 * @sp: The sequence the request was on
1480 * @fp: The request frame
1481 * @lport: The local port that received the request
83fe6a93
JE
1482 *
1483 * Locking Note: Called with the lport lock held.
1484 */
1485void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
1486 struct fc_lport *lport)
1487{
1488 struct fc_seq_els_data els_data;
1489
1490 /*
a7b12a27 1491 * Handle FLOGI, PLOGI and LOGO requests separately, since they
83fe6a93
JE
1492 * don't require prior login.
1493 * Check for unsupported opcodes first and reject them.
1494 * For some ops, it would be incorrect to reject with "PLOGI required".
1495 */
1496 switch (fc_frame_payload_op(fp)) {
a7b12a27
JE
1497 case ELS_FLOGI:
1498 fc_rport_recv_flogi_req(lport, sp, fp);
1499 break;
83fe6a93
JE
1500 case ELS_PLOGI:
1501 fc_rport_recv_plogi_req(lport, sp, fp);
1502 break;
1503 case ELS_LOGO:
1504 fc_rport_recv_logo_req(lport, sp, fp);
1505 break;
1506 case ELS_PRLI:
1507 case ELS_PRLO:
8abbe3a4 1508 case ELS_ADISC:
83fe6a93
JE
1509 case ELS_RRQ:
1510 case ELS_REC:
63e27fb8 1511 case ELS_RLS:
83fe6a93
JE
1512 fc_rport_recv_els_req(lport, sp, fp);
1513 break;
1514 default:
1515 fc_frame_free(fp);
1516 els_data.fp = NULL;
1517 els_data.reason = ELS_RJT_UNSUP;
1518 els_data.explan = ELS_EXPL_NONE;
1519 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
1520 break;
1521 }
42e9a92f
RL
1522}
1523
1524/**
3a3b42bf
RL
1525 * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
1526 * @lport: The local port that received the PLOGI request
1527 * @sp: The sequence that the PLOGI request was on
1528 * @rx_fp: The PLOGI request frame
42e9a92f 1529 *
3ac6f98f 1530 * Locking Note: The rport lock is held before calling this function.
42e9a92f 1531 */
3ac6f98f 1532static void fc_rport_recv_plogi_req(struct fc_lport *lport,
42e9a92f
RL
1533 struct fc_seq *sp, struct fc_frame *rx_fp)
1534{
3ac6f98f
JE
1535 struct fc_disc *disc;
1536 struct fc_rport_priv *rdata;
42e9a92f 1537 struct fc_frame *fp = rx_fp;
42e9a92f
RL
1538 struct fc_els_flogi *pl;
1539 struct fc_seq_els_data rjt_data;
24f089e2 1540 u32 sid;
42e9a92f 1541
3ac6f98f 1542 rjt_data.fp = NULL;
251748a9 1543 sid = fc_frame_sid(fp);
42e9a92f 1544
3ac6f98f 1545 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
42e9a92f 1546
42e9a92f
RL
1547 pl = fc_frame_payload_get(fp, sizeof(*pl));
1548 if (!pl) {
3ac6f98f
JE
1549 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
1550 rjt_data.reason = ELS_RJT_PROT;
1551 rjt_data.explan = ELS_EXPL_INV_LEN;
1552 goto reject;
42e9a92f 1553 }
3ac6f98f
JE
1554
1555 disc = &lport->disc;
1556 mutex_lock(&disc->disc_mutex);
1557 rdata = lport->tt.rport_create(lport, sid);
1558 if (!rdata) {
1559 mutex_unlock(&disc->disc_mutex);
1560 rjt_data.reason = ELS_RJT_UNAB;
1561 rjt_data.explan = ELS_EXPL_INSUF_RES;
1562 goto reject;
1563 }
1564
1565 mutex_lock(&rdata->rp_mutex);
1566 mutex_unlock(&disc->disc_mutex);
1567
1568 rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1569 rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
42e9a92f
RL
1570
1571 /*
3ac6f98f 1572 * If the rport was just created, possibly due to the incoming PLOGI,
42e9a92f
RL
1573 * set the state appropriately and accept the PLOGI.
1574 *
1575 * If we had also sent a PLOGI, and if the received PLOGI is from a
1576 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1577 * "command already in progress".
1578 *
1579 * XXX TBD: If the session was ready before, the PLOGI should result in
1580 * all outstanding exchanges being reset.
1581 */
1582 switch (rdata->rp_state) {
1583 case RPORT_ST_INIT:
3ac6f98f 1584 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
42e9a92f 1585 break;
a7b12a27
JE
1586 case RPORT_ST_PLOGI_WAIT:
1587 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI_WAIT state\n");
1588 break;
42e9a92f 1589 case RPORT_ST_PLOGI:
3ac6f98f
JE
1590 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1591 if (rdata->ids.port_name < lport->wwpn) {
1592 mutex_unlock(&rdata->rp_mutex);
1593 rjt_data.reason = ELS_RJT_INPROG;
1594 rjt_data.explan = ELS_EXPL_NONE;
1595 goto reject;
1596 }
42e9a92f
RL
1597 break;
1598 case RPORT_ST_PRLI:
b4a9c7ed 1599 case RPORT_ST_RTV:
42e9a92f 1600 case RPORT_ST_READY:
370c3bd0
JE
1601 case RPORT_ST_ADISC:
1602 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
1603 "- ignored for now\n", rdata->rp_state);
1604 /* XXX TBD - should reset */
42e9a92f 1605 break;
a7b12a27 1606 case RPORT_ST_FLOGI:
14194054 1607 case RPORT_ST_DELETE:
b4a9c7ed
JE
1608 FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
1609 fc_rport_state(rdata));
1610 mutex_unlock(&rdata->rp_mutex);
1611 rjt_data.reason = ELS_RJT_BUSY;
1612 rjt_data.explan = ELS_EXPL_NONE;
1613 goto reject;
42e9a92f
RL
1614 }
1615
3ac6f98f
JE
1616 /*
1617 * Get session payload size from incoming PLOGI.
1618 */
1619 rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
3ac6f98f
JE
1620
1621 /*
1622 * Send LS_ACC. If this fails, the originator should retry.
1623 */
3ac6f98f
JE
1624 fp = fc_frame_alloc(lport, sizeof(*pl));
1625 if (!fp)
1626 goto out;
1627
1628 fc_plogi_fill(lport, fp, ELS_LS_ACC);
24f089e2
JE
1629 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1630 lport->tt.frame_send(lport, fp);
3ac6f98f
JE
1631 fc_rport_enter_prli(rdata);
1632out:
1633 mutex_unlock(&rdata->rp_mutex);
24f089e2 1634 fc_frame_free(rx_fp);
3ac6f98f
JE
1635 return;
1636
1637reject:
1638 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1639 fc_frame_free(fp);
42e9a92f
RL
1640}
1641
1642/**
3a3b42bf
RL
1643 * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
1644 * @rdata: The remote port that sent the PRLI request
1645 * @sp: The sequence that the PRLI was on
1646 * @rx_fp: The PRLI request frame
42e9a92f
RL
1647 *
1648 * Locking Note: The rport lock is exected to be held before calling
1649 * this function.
1650 */
9fb9d328 1651static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
42e9a92f
RL
1652 struct fc_seq *sp, struct fc_frame *rx_fp)
1653{
42e9a92f 1654 struct fc_lport *lport = rdata->local_port;
42e9a92f 1655 struct fc_frame *fp;
42e9a92f
RL
1656 struct {
1657 struct fc_els_prli prli;
1658 struct fc_els_spp spp;
1659 } *pp;
1660 struct fc_els_spp *rspp; /* request service param page */
1661 struct fc_els_spp *spp; /* response spp */
1662 unsigned int len;
1663 unsigned int plen;
42e9a92f
RL
1664 enum fc_els_spp_resp resp;
1665 struct fc_seq_els_data rjt_data;
42e9a92f
RL
1666 u32 fcp_parm;
1667 u32 roles = FC_RPORT_ROLE_UNKNOWN;
42e9a92f 1668
a2f6a024 1669 rjt_data.fp = NULL;
9fb9d328
JE
1670 FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1671 fc_rport_state(rdata));
42e9a92f 1672
251748a9 1673 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
42e9a92f 1674 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
a2f6a024
JE
1675 if (!pp)
1676 goto reject_len;
1677 plen = ntohs(pp->prli.prli_len);
1678 if ((plen % 4) != 0 || plen > len || plen < 16)
1679 goto reject_len;
1680 if (plen < len)
1681 len = plen;
1682 plen = pp->prli.prli_spp_len;
1683 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1684 plen > len || len < sizeof(*pp) || plen < 12)
1685 goto reject_len;
1686 rspp = &pp->spp;
1687
1688 fp = fc_frame_alloc(lport, len);
1689 if (!fp) {
1690 rjt_data.reason = ELS_RJT_UNAB;
1691 rjt_data.explan = ELS_EXPL_INSUF_RES;
1692 goto reject;
42e9a92f 1693 }
a2f6a024
JE
1694 pp = fc_frame_payload_get(fp, len);
1695 WARN_ON(!pp);
1696 memset(pp, 0, len);
1697 pp->prli.prli_cmd = ELS_LS_ACC;
1698 pp->prli.prli_spp_len = plen;
1699 pp->prli.prli_len = htons(len);
1700 len -= sizeof(struct fc_els_prli);
42e9a92f 1701
a2f6a024
JE
1702 /* reinitialize remote port roles */
1703 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
42e9a92f 1704
a2f6a024
JE
1705 /*
1706 * Go through all the service parameter pages and build
1707 * response. If plen indicates longer SPP than standard,
1708 * use that. The entire response has been pre-cleared above.
1709 */
1710 spp = &pp->spp;
1711 while (len >= plen) {
1712 spp->spp_type = rspp->spp_type;
1713 spp->spp_type_ext = rspp->spp_type_ext;
1714 spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
1715 resp = FC_SPP_RESP_ACK;
1716
1717 switch (rspp->spp_type) {
1718 case 0: /* common to all FC-4 types */
42e9a92f 1719 break;
a2f6a024
JE
1720 case FC_TYPE_FCP:
1721 fcp_parm = ntohl(rspp->spp_params);
1722 if (fcp_parm & FCP_SPPF_RETRY)
1723 rdata->flags |= FC_RP_FLAGS_RETRY;
1724 rdata->supported_classes = FC_COS_CLASS3;
1725 if (fcp_parm & FCP_SPPF_INIT_FCN)
1726 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1727 if (fcp_parm & FCP_SPPF_TARG_FCN)
1728 roles |= FC_RPORT_ROLE_FCP_TARGET;
1729 rdata->ids.roles = roles;
1730
1731 spp->spp_params = htonl(lport->service_params);
42e9a92f
RL
1732 break;
1733 default:
a2f6a024 1734 resp = FC_SPP_RESP_INVL;
42e9a92f
RL
1735 break;
1736 }
a2f6a024
JE
1737 spp->spp_flags |= resp;
1738 len -= plen;
1739 rspp = (struct fc_els_spp *)((char *)rspp + plen);
1740 spp = (struct fc_els_spp *)((char *)spp + plen);
1741 }
1742
1743 /*
1744 * Send LS_ACC. If this fails, the originator should retry.
1745 */
24f089e2
JE
1746 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1747 lport->tt.frame_send(lport, fp);
a2f6a024
JE
1748
1749 switch (rdata->rp_state) {
1750 case RPORT_ST_PRLI:
1751 fc_rport_enter_ready(rdata);
1752 break;
1753 default:
1754 break;
42e9a92f 1755 }
a2f6a024
JE
1756 goto drop;
1757
1758reject_len:
1759 rjt_data.reason = ELS_RJT_PROT;
1760 rjt_data.explan = ELS_EXPL_INV_LEN;
1761reject:
1762 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1763drop:
42e9a92f
RL
1764 fc_frame_free(rx_fp);
1765}
1766
1767/**
3a3b42bf
RL
1768 * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
1769 * @rdata: The remote port that sent the PRLO request
1770 * @sp: The sequence that the PRLO was on
f8fc6c2c 1771 * @rx_fp: The PRLO request frame
42e9a92f
RL
1772 *
1773 * Locking Note: The rport lock is exected to be held before calling
1774 * this function.
1775 */
9fb9d328
JE
1776static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
1777 struct fc_seq *sp,
f8fc6c2c 1778 struct fc_frame *rx_fp)
42e9a92f 1779{
42e9a92f 1780 struct fc_lport *lport = rdata->local_port;
f8fc6c2c
BPG
1781 struct fc_exch *ep;
1782 struct fc_frame *fp;
1783 struct {
1784 struct fc_els_prlo prlo;
1785 struct fc_els_spp spp;
1786 } *pp;
1787 struct fc_els_spp *rspp; /* request service param page */
1788 struct fc_els_spp *spp; /* response spp */
1789 unsigned int len;
1790 unsigned int plen;
42e9a92f
RL
1791 struct fc_seq_els_data rjt_data;
1792
f8fc6c2c 1793 rjt_data.fp = NULL;
42e9a92f 1794
9fb9d328
JE
1795 FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
1796 fc_rport_state(rdata));
42e9a92f 1797
251748a9 1798 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
f8fc6c2c
BPG
1799 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1800 if (!pp)
1801 goto reject_len;
1802 plen = ntohs(pp->prlo.prlo_len);
1803 if (plen != 20)
1804 goto reject_len;
1805 if (plen < len)
1806 len = plen;
1807
1808 rspp = &pp->spp;
1809
1810 fp = fc_frame_alloc(lport, len);
1811 if (!fp) {
1812 rjt_data.reason = ELS_RJT_UNAB;
1813 rjt_data.explan = ELS_EXPL_INSUF_RES;
1814 goto reject;
1815 }
1816
1817 sp = lport->tt.seq_start_next(sp);
1818 WARN_ON(!sp);
1819 pp = fc_frame_payload_get(fp, len);
1820 WARN_ON(!pp);
1821 memset(pp, 0, len);
1822 pp->prlo.prlo_cmd = ELS_LS_ACC;
1823 pp->prlo.prlo_obs = 0x10;
1824 pp->prlo.prlo_len = htons(len);
1825 spp = &pp->spp;
1826 spp->spp_type = rspp->spp_type;
1827 spp->spp_type_ext = rspp->spp_type_ext;
1828 spp->spp_flags = FC_SPP_RESP_ACK;
1829
1830 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
1831
f8fc6c2c
BPG
1832 ep = fc_seq_exch(sp);
1833 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
24f089e2 1834 FC_TYPE_ELS, FC_FCTL_RESP, 0);
f8fc6c2c
BPG
1835 lport->tt.seq_send(lport, sp, fp);
1836 goto drop;
1837
1838reject_len:
1839 rjt_data.reason = ELS_RJT_PROT;
1840 rjt_data.explan = ELS_EXPL_INV_LEN;
1841reject:
42e9a92f 1842 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
f8fc6c2c
BPG
1843drop:
1844 fc_frame_free(rx_fp);
42e9a92f
RL
1845}
1846
1847/**
3a3b42bf
RL
1848 * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
1849 * @lport: The local port that received the LOGO request
1850 * @sp: The sequence that the LOGO request was on
1851 * @fp: The LOGO request frame
42e9a92f
RL
1852 *
1853 * Locking Note: The rport lock is exected to be held before calling
1854 * this function.
1855 */
83fe6a93 1856static void fc_rport_recv_logo_req(struct fc_lport *lport,
9fb9d328 1857 struct fc_seq *sp,
42e9a92f
RL
1858 struct fc_frame *fp)
1859{
83fe6a93
JE
1860 struct fc_rport_priv *rdata;
1861 u32 sid;
42e9a92f 1862
feab4ae7
JE
1863 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
1864
251748a9 1865 sid = fc_frame_sid(fp);
42e9a92f 1866
83fe6a93
JE
1867 mutex_lock(&lport->disc.disc_mutex);
1868 rdata = lport->tt.rport_lookup(lport, sid);
1869 if (rdata) {
1870 mutex_lock(&rdata->rp_mutex);
1871 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
1872 fc_rport_state(rdata));
feab4ae7 1873
b4a9c7ed 1874 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
83fe6a93
JE
1875 mutex_unlock(&rdata->rp_mutex);
1876 } else
1877 FC_RPORT_ID_DBG(lport, sid,
1878 "Received LOGO from non-logged-in port\n");
1879 mutex_unlock(&lport->disc.disc_mutex);
42e9a92f
RL
1880 fc_frame_free(fp);
1881}
1882
3a3b42bf
RL
1883/**
1884 * fc_rport_flush_queue() - Flush the rport_event_queue
1885 */
42e9a92f
RL
1886static void fc_rport_flush_queue(void)
1887{
1888 flush_workqueue(rport_event_queue);
1889}
1890
3a3b42bf
RL
1891/**
1892 * fc_rport_init() - Initialize the remote port layer for a local port
1893 * @lport: The local port to initialize the remote port layer for
1894 */
42e9a92f
RL
1895int fc_rport_init(struct fc_lport *lport)
1896{
8025b5db
JE
1897 if (!lport->tt.rport_lookup)
1898 lport->tt.rport_lookup = fc_rport_lookup;
1899
5101ff99 1900 if (!lport->tt.rport_create)
9e9d0452 1901 lport->tt.rport_create = fc_rport_create;
5101ff99 1902
42e9a92f
RL
1903 if (!lport->tt.rport_login)
1904 lport->tt.rport_login = fc_rport_login;
1905
1906 if (!lport->tt.rport_logoff)
1907 lport->tt.rport_logoff = fc_rport_logoff;
1908
1909 if (!lport->tt.rport_recv_req)
1910 lport->tt.rport_recv_req = fc_rport_recv_req;
1911
1912 if (!lport->tt.rport_flush_queue)
1913 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1914
f211fa51
JE
1915 if (!lport->tt.rport_destroy)
1916 lport->tt.rport_destroy = fc_rport_destroy;
1917
42e9a92f
RL
1918 return 0;
1919}
1920EXPORT_SYMBOL(fc_rport_init);
1921
3a3b42bf
RL
1922/**
1923 * fc_setup_rport() - Initialize the rport_event_queue
1924 */
1925int fc_setup_rport()
42e9a92f
RL
1926{
1927 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
1928 if (!rport_event_queue)
1929 return -ENOMEM;
1930 return 0;
1931}
42e9a92f 1932
3a3b42bf
RL
1933/**
1934 * fc_destroy_rport() - Destroy the rport_event_queue
1935 */
1936void fc_destroy_rport()
42e9a92f
RL
1937{
1938 destroy_workqueue(rport_event_queue);
1939}
42e9a92f 1940
3a3b42bf
RL
1941/**
1942 * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
1943 * @rport: The remote port whose I/O should be terminated
1944 */
42e9a92f
RL
1945void fc_rport_terminate_io(struct fc_rport *rport)
1946{
3a3b42bf
RL
1947 struct fc_rport_libfc_priv *rpriv = rport->dd_data;
1948 struct fc_lport *lport = rpriv->local_port;
42e9a92f 1949
1f6ff364
AJ
1950 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
1951 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
42e9a92f
RL
1952}
1953EXPORT_SYMBOL(fc_rport_terminate_io);