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