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