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