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