]> git.ipfire.org Git - thirdparty/linux.git/blame - drivers/scsi/libfc/fc_rport.c
scsi: libfc: Implement RTV responder
[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;
618461c0 1129 u8 resp_code = 0;
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);
1161 FC_RPORT_DBG(rdata, "PRLI spp_flags = 0x%x\n",
1162 pp->spp.spp_flags);
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
925cedae
JE
1180 prov = fc_passive_prov[FC_TYPE_FCP];
1181 if (prov) {
1182 memset(&temp_spp, 0, sizeof(temp_spp));
1183 prov->prli(rdata, pp->prli.prli_spp_len,
1184 &pp->spp, &temp_spp);
1185 }
1186
f211fa51 1187 rdata->supported_classes = FC_COS_CLASS3;
42e9a92f
RL
1188 if (fcp_parm & FCP_SPPF_INIT_FCN)
1189 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1190 if (fcp_parm & FCP_SPPF_TARG_FCN)
1191 roles |= FC_RPORT_ROLE_FCP_TARGET;
1192
f211fa51 1193 rdata->ids.roles = roles;
9fb9d328 1194 fc_rport_enter_rtv(rdata);
42e9a92f
RL
1195
1196 } else {
57d3ec7e
HR
1197 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1198 FC_RPORT_DBG(rdata, "PRLI ELS rejected, reason %x expl %x\n",
1199 rjt->er_reason, rjt->er_explan);
9f9504a7 1200 fc_rport_error_retry(rdata, FC_EX_ELS_RJT);
42e9a92f
RL
1201 }
1202
1203out:
1204 fc_frame_free(fp);
1205err:
1206 mutex_unlock(&rdata->rp_mutex);
785141c6 1207put:
f211fa51 1208 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
42e9a92f
RL
1209}
1210
42e9a92f 1211/**
3a3b42bf
RL
1212 * fc_rport_enter_prli() - Send Process Login (PRLI) request
1213 * @rdata: The remote port to send the PRLI request to
42e9a92f
RL
1214 *
1215 * Locking Note: The rport lock is expected to be held before calling
1216 * this routine.
4d2095cc
HR
1217 *
1218 * Reference counting: increments kref when sending ELS
42e9a92f 1219 */
9fb9d328 1220static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
42e9a92f 1221{
42e9a92f
RL
1222 struct fc_lport *lport = rdata->local_port;
1223 struct {
1224 struct fc_els_prli prli;
1225 struct fc_els_spp spp;
1226 } *pp;
1227 struct fc_frame *fp;
925cedae 1228 struct fc4_prov *prov;
42e9a92f 1229
3ac6f98f
JE
1230 /*
1231 * If the rport is one of the well known addresses
1232 * we skip PRLI and RTV and go straight to READY.
1233 */
1234 if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
1235 fc_rport_enter_ready(rdata);
1236 return;
1237 }
1238
9fb9d328
JE
1239 FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
1240 fc_rport_state(rdata));
42e9a92f 1241
9fb9d328 1242 fc_rport_state_enter(rdata, RPORT_ST_PRLI);
42e9a92f
RL
1243
1244 fp = fc_frame_alloc(lport, sizeof(*pp));
1245 if (!fp) {
9f9504a7 1246 fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
42e9a92f
RL
1247 return;
1248 }
1249
925cedae
JE
1250 fc_prli_fill(lport, fp);
1251
1252 prov = fc_passive_prov[FC_TYPE_FCP];
1253 if (prov) {
1254 pp = fc_frame_payload_get(fp, sizeof(*pp));
1255 prov->prli(rdata, sizeof(pp->spp), NULL, &pp->spp);
1256 }
1257
1258 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rdata->ids.port_id,
1259 fc_host_port_id(lport->host), FC_TYPE_ELS,
1260 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1261
4d2095cc 1262 kref_get(&rdata->kref);
925cedae 1263 if (!lport->tt.exch_seq_send(lport, fp, fc_rport_prli_resp,
4d2095cc 1264 NULL, rdata, 2 * lport->r_a_tov)) {
9f9504a7 1265 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
4d2095cc
HR
1266 kref_put(&rdata->kref, lport->tt.rport_destroy);
1267 }
42e9a92f
RL
1268}
1269
1270/**
7c5a51b8 1271 * fc_rport_rtv_resp() - Handler for Request Timeout Value (RTV) responses
3a3b42bf
RL
1272 * @sp: The sequence the RTV was on
1273 * @fp: The RTV response frame
1274 * @rdata_arg: The remote port that sent the RTV response
42e9a92f
RL
1275 *
1276 * Many targets don't seem to support this.
1277 *
1278 * Locking Note: This function will be called without the rport lock
1279 * held, but it will lock, call an _enter_* function or fc_rport_error
1280 * and then unlock the rport.
1281 */
1282static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
9fb9d328 1283 void *rdata_arg)
42e9a92f 1284{
9fb9d328 1285 struct fc_rport_priv *rdata = rdata_arg;
42e9a92f
RL
1286 u8 op;
1287
f657d299 1288 FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
42e9a92f 1289
785141c6
CD
1290 if (fp == ERR_PTR(-FC_EX_CLOSED))
1291 goto put;
1292
1293 mutex_lock(&rdata->rp_mutex);
1294
42e9a92f 1295 if (rdata->rp_state != RPORT_ST_RTV) {
9fb9d328
JE
1296 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
1297 "%s\n", fc_rport_state(rdata));
76f6804e
AJ
1298 if (IS_ERR(fp))
1299 goto err;
42e9a92f
RL
1300 goto out;
1301 }
1302
76f6804e 1303 if (IS_ERR(fp)) {
9f9504a7 1304 fc_rport_error(rdata, PTR_ERR(fp));
76f6804e
AJ
1305 goto err;
1306 }
1307
42e9a92f
RL
1308 op = fc_frame_payload_op(fp);
1309 if (op == ELS_LS_ACC) {
1310 struct fc_els_rtv_acc *rtv;
1311 u32 toq;
1312 u32 tov;
1313
1314 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
1315 if (rtv) {
1316 toq = ntohl(rtv->rtv_toq);
1317 tov = ntohl(rtv->rtv_r_a_tov);
1318 if (tov == 0)
1319 tov = 1;
76e72ad1
HR
1320 if (tov > rdata->r_a_tov)
1321 rdata->r_a_tov = tov;
42e9a92f
RL
1322 tov = ntohl(rtv->rtv_e_d_tov);
1323 if (toq & FC_ELS_RTV_EDRES)
1324 tov /= 1000000;
1325 if (tov == 0)
1326 tov = 1;
76e72ad1
HR
1327 if (tov > rdata->e_d_tov)
1328 rdata->e_d_tov = tov;
42e9a92f
RL
1329 }
1330 }
1331
9fb9d328 1332 fc_rport_enter_ready(rdata);
42e9a92f
RL
1333
1334out:
1335 fc_frame_free(fp);
1336err:
1337 mutex_unlock(&rdata->rp_mutex);
785141c6 1338put:
f211fa51 1339 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
42e9a92f
RL
1340}
1341
1342/**
3a3b42bf
RL
1343 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
1344 * @rdata: The remote port to send the RTV request to
42e9a92f
RL
1345 *
1346 * Locking Note: The rport lock is expected to be held before calling
1347 * this routine.
4d2095cc
HR
1348 *
1349 * Reference counting: increments kref when sending ELS
42e9a92f 1350 */
9fb9d328 1351static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
42e9a92f
RL
1352{
1353 struct fc_frame *fp;
42e9a92f
RL
1354 struct fc_lport *lport = rdata->local_port;
1355
9fb9d328
JE
1356 FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
1357 fc_rport_state(rdata));
42e9a92f 1358
9fb9d328 1359 fc_rport_state_enter(rdata, RPORT_ST_RTV);
42e9a92f
RL
1360
1361 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
1362 if (!fp) {
9f9504a7 1363 fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
42e9a92f
RL
1364 return;
1365 }
1366
4d2095cc 1367 kref_get(&rdata->kref);
f211fa51 1368 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
b94f8951 1369 fc_rport_rtv_resp, rdata,
4d2095cc 1370 2 * lport->r_a_tov)) {
9f9504a7 1371 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
4d2095cc
HR
1372 kref_put(&rdata->kref, lport->tt.rport_destroy);
1373 }
42e9a92f
RL
1374}
1375
7c5a51b8
HR
1376/**
1377 * fc_rport_recv_rtv_req() - Handler for Read Timeout Value (RTV) requests
1378 * @rdata: The remote port that sent the RTV request
1379 * @in_fp: The RTV request frame
1380 *
1381 * Locking Note: Called with the lport and rport locks held.
1382 */
1383static void fc_rport_recv_rtv_req(struct fc_rport_priv *rdata,
1384 struct fc_frame *in_fp)
1385{
1386 struct fc_lport *lport = rdata->local_port;
1387 struct fc_frame *fp;
1388 struct fc_els_rtv_acc *rtv;
1389 struct fc_seq_els_data rjt_data;
1390
1391 FC_RPORT_DBG(rdata, "Received RTV request\n");
1392
1393 fp = fc_frame_alloc(lport, sizeof(*rtv));
1394 if (!fp) {
1395 rjt_data.reason = ELS_RJT_UNAB;
1396 rjt_data.reason = ELS_EXPL_INSUF_RES;
1397 lport->tt.seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data);
1398 goto drop;
1399 }
1400 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
1401 rtv->rtv_cmd = ELS_LS_ACC;
1402 rtv->rtv_r_a_tov = htonl(lport->r_a_tov);
1403 rtv->rtv_e_d_tov = htonl(lport->e_d_tov);
1404 rtv->rtv_toq = 0;
1405 fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
1406 lport->tt.frame_send(lport, fp);
1407drop:
1408 fc_frame_free(in_fp);
1409}
1410
079ecd8c
JE
1411/**
1412 * fc_rport_logo_resp() - Handler for logout (LOGO) responses
1413 * @sp: The sequence the LOGO was on
1414 * @fp: The LOGO response frame
1415 * @lport_arg: The local port
1416 */
1417static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
4d2095cc 1418 void *rdata_arg)
079ecd8c 1419{
4d2095cc
HR
1420 struct fc_rport_priv *rdata = rdata_arg;
1421 struct fc_lport *lport = rdata->local_port;
079ecd8c
JE
1422
1423 FC_RPORT_ID_DBG(lport, fc_seq_exch(sp)->did,
1424 "Received a LOGO %s\n", fc_els_resp_type(fp));
4d2095cc
HR
1425 if (!IS_ERR(fp))
1426 fc_frame_free(fp);
1427 kref_put(&rdata->kref, lport->tt.rport_destroy);
079ecd8c
JE
1428}
1429
42e9a92f 1430/**
3a3b42bf
RL
1431 * fc_rport_enter_logo() - Send a logout (LOGO) request
1432 * @rdata: The remote port to send the LOGO request to
42e9a92f
RL
1433 *
1434 * Locking Note: The rport lock is expected to be held before calling
1435 * this routine.
4d2095cc
HR
1436 *
1437 * Reference counting: increments kref when sending ELS
42e9a92f 1438 */
9fb9d328 1439static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
42e9a92f 1440{
42e9a92f
RL
1441 struct fc_lport *lport = rdata->local_port;
1442 struct fc_frame *fp;
1443
079ecd8c 1444 FC_RPORT_DBG(rdata, "Port sending LOGO from %s state\n",
9fb9d328 1445 fc_rport_state(rdata));
42e9a92f 1446
42e9a92f 1447 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
079ecd8c 1448 if (!fp)
42e9a92f 1449 return;
4d2095cc
HR
1450 kref_get(&rdata->kref);
1451 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
1452 fc_rport_logo_resp, rdata, 0))
1453 kref_put(&rdata->kref, lport->tt.rport_destroy);
42e9a92f
RL
1454}
1455
370c3bd0 1456/**
3a3b42bf
RL
1457 * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
1458 * @sp: The sequence the ADISC response was on
1459 * @fp: The ADISC response frame
1460 * @rdata_arg: The remote port that sent the ADISC response
370c3bd0
JE
1461 *
1462 * Locking Note: This function will be called without the rport lock
1463 * held, but it will lock, call an _enter_* function or fc_rport_error
1464 * and then unlock the rport.
1465 */
1466static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
3a3b42bf 1467 void *rdata_arg)
370c3bd0
JE
1468{
1469 struct fc_rport_priv *rdata = rdata_arg;
1470 struct fc_els_adisc *adisc;
1471 u8 op;
1472
370c3bd0
JE
1473 FC_RPORT_DBG(rdata, "Received a ADISC response\n");
1474
785141c6
CD
1475 if (fp == ERR_PTR(-FC_EX_CLOSED))
1476 goto put;
1477
1478 mutex_lock(&rdata->rp_mutex);
1479
370c3bd0
JE
1480 if (rdata->rp_state != RPORT_ST_ADISC) {
1481 FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
1482 fc_rport_state(rdata));
1483 if (IS_ERR(fp))
1484 goto err;
1485 goto out;
1486 }
1487
1488 if (IS_ERR(fp)) {
9f9504a7 1489 fc_rport_error(rdata, PTR_ERR(fp));
370c3bd0
JE
1490 goto err;
1491 }
1492
1493 /*
1494 * If address verification failed. Consider us logged out of the rport.
1495 * Since the rport is still in discovery, we want to be
1496 * logged in, so go to PLOGI state. Otherwise, go back to READY.
1497 */
1498 op = fc_frame_payload_op(fp);
1499 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1500 if (op != ELS_LS_ACC || !adisc ||
1501 ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
1502 get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
1503 get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
1504 FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
a7b12a27 1505 fc_rport_enter_flogi(rdata);
370c3bd0
JE
1506 } else {
1507 FC_RPORT_DBG(rdata, "ADISC OK\n");
1508 fc_rport_enter_ready(rdata);
1509 }
1510out:
1511 fc_frame_free(fp);
1512err:
1513 mutex_unlock(&rdata->rp_mutex);
785141c6 1514put:
370c3bd0
JE
1515 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1516}
1517
1518/**
3a3b42bf
RL
1519 * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
1520 * @rdata: The remote port to send the ADISC request to
370c3bd0
JE
1521 *
1522 * Locking Note: The rport lock is expected to be held before calling
1523 * this routine.
4d2095cc
HR
1524 *
1525 * Reference counting: increments kref when sending ELS
370c3bd0
JE
1526 */
1527static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
1528{
1529 struct fc_lport *lport = rdata->local_port;
1530 struct fc_frame *fp;
1531
1532 FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
1533 fc_rport_state(rdata));
1534
1535 fc_rport_state_enter(rdata, RPORT_ST_ADISC);
1536
1537 fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
1538 if (!fp) {
9f9504a7 1539 fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
370c3bd0
JE
1540 return;
1541 }
4d2095cc 1542 kref_get(&rdata->kref);
370c3bd0 1543 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
b94f8951 1544 fc_rport_adisc_resp, rdata,
4d2095cc 1545 2 * lport->r_a_tov)) {
9f9504a7 1546 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
4d2095cc
HR
1547 kref_put(&rdata->kref, lport->tt.rport_destroy);
1548 }
370c3bd0
JE
1549}
1550
8abbe3a4 1551/**
3a3b42bf
RL
1552 * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
1553 * @rdata: The remote port that sent the ADISC request
3a3b42bf 1554 * @in_fp: The ADISC request frame
8abbe3a4
JE
1555 *
1556 * Locking Note: Called with the lport and rport locks held.
1557 */
1558static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
92261156 1559 struct fc_frame *in_fp)
8abbe3a4
JE
1560{
1561 struct fc_lport *lport = rdata->local_port;
1562 struct fc_frame *fp;
8abbe3a4
JE
1563 struct fc_els_adisc *adisc;
1564 struct fc_seq_els_data rjt_data;
8abbe3a4
JE
1565
1566 FC_RPORT_DBG(rdata, "Received ADISC request\n");
1567
1568 adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
1569 if (!adisc) {
8abbe3a4
JE
1570 rjt_data.reason = ELS_RJT_PROT;
1571 rjt_data.explan = ELS_EXPL_INV_LEN;
92261156 1572 lport->tt.seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data);
8abbe3a4
JE
1573 goto drop;
1574 }
1575
1576 fp = fc_frame_alloc(lport, sizeof(*adisc));
1577 if (!fp)
1578 goto drop;
1579 fc_adisc_fill(lport, fp);
1580 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1581 adisc->adisc_cmd = ELS_LS_ACC;
24f089e2
JE
1582 fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
1583 lport->tt.frame_send(lport, fp);
8abbe3a4
JE
1584drop:
1585 fc_frame_free(in_fp);
1586}
1587
63e27fb8
YZ
1588/**
1589 * fc_rport_recv_rls_req() - Handle received Read Link Status request
1590 * @rdata: The remote port that sent the RLS request
63e27fb8
YZ
1591 * @rx_fp: The PRLI request frame
1592 *
1593 * Locking Note: The rport lock is expected to be held before calling
1594 * this function.
1595 */
1596static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
92261156 1597 struct fc_frame *rx_fp)
63e27fb8
YZ
1598
1599{
1600 struct fc_lport *lport = rdata->local_port;
1601 struct fc_frame *fp;
63e27fb8
YZ
1602 struct fc_els_rls *rls;
1603 struct fc_els_rls_resp *rsp;
1604 struct fc_els_lesb *lesb;
1605 struct fc_seq_els_data rjt_data;
1606 struct fc_host_statistics *hst;
63e27fb8
YZ
1607
1608 FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
1609 fc_rport_state(rdata));
1610
1611 rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
1612 if (!rls) {
1613 rjt_data.reason = ELS_RJT_PROT;
1614 rjt_data.explan = ELS_EXPL_INV_LEN;
1615 goto out_rjt;
1616 }
1617
1618 fp = fc_frame_alloc(lport, sizeof(*rsp));
1619 if (!fp) {
1620 rjt_data.reason = ELS_RJT_UNAB;
1621 rjt_data.explan = ELS_EXPL_INSUF_RES;
1622 goto out_rjt;
1623 }
1624
1625 rsp = fc_frame_payload_get(fp, sizeof(*rsp));
1626 memset(rsp, 0, sizeof(*rsp));
1627 rsp->rls_cmd = ELS_LS_ACC;
1628 lesb = &rsp->rls_lesb;
1629 if (lport->tt.get_lesb) {
1630 /* get LESB from LLD if it supports it */
1631 lport->tt.get_lesb(lport, lesb);
1632 } else {
1633 fc_get_host_stats(lport->host);
1634 hst = &lport->host_stats;
1635 lesb->lesb_link_fail = htonl(hst->link_failure_count);
1636 lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
1637 lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
1638 lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
1639 lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
1640 lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
1641 }
1642
24f089e2
JE
1643 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1644 lport->tt.frame_send(lport, fp);
63e27fb8
YZ
1645 goto out;
1646
1647out_rjt:
92261156 1648 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
63e27fb8
YZ
1649out:
1650 fc_frame_free(rx_fp);
1651}
1652
42e9a92f 1653/**
3a3b42bf
RL
1654 * fc_rport_recv_els_req() - Handler for validated ELS requests
1655 * @lport: The local port that received the ELS request
3a3b42bf 1656 * @fp: The ELS request frame
83fe6a93
JE
1657 *
1658 * Handle incoming ELS requests that require port login.
1659 * The ELS opcode has already been validated by the caller.
42e9a92f 1660 *
131203a1 1661 * Locking Note: Called with the lport lock held.
4d2095cc
HR
1662 *
1663 * Reference counting: does not modify kref
42e9a92f 1664 */
92261156 1665static void fc_rport_recv_els_req(struct fc_lport *lport, struct fc_frame *fp)
42e9a92f 1666{
131203a1 1667 struct fc_rport_priv *rdata;
42e9a92f 1668 struct fc_seq_els_data els_data;
42e9a92f 1669
251748a9 1670 rdata = lport->tt.rport_lookup(lport, fc_frame_sid(fp));
57d3ec7e
HR
1671 if (!rdata) {
1672 FC_RPORT_ID_DBG(lport, fc_frame_sid(fp),
1673 "Received ELS 0x%02x from non-logged-in port\n",
1674 fc_frame_payload_op(fp));
83fe6a93 1675 goto reject;
57d3ec7e 1676 }
baa6719f 1677
131203a1
JE
1678 mutex_lock(&rdata->rp_mutex);
1679
83fe6a93
JE
1680 switch (rdata->rp_state) {
1681 case RPORT_ST_PRLI:
1682 case RPORT_ST_RTV:
1683 case RPORT_ST_READY:
370c3bd0 1684 case RPORT_ST_ADISC:
83fe6a93
JE
1685 break;
1686 default:
57d3ec7e
HR
1687 FC_RPORT_DBG(rdata,
1688 "Reject ELS 0x%02x while in state %s\n",
1689 fc_frame_payload_op(fp), fc_rport_state(rdata));
83fe6a93 1690 mutex_unlock(&rdata->rp_mutex);
baa6719f 1691 kref_put(&rdata->kref, lport->tt.rport_destroy);
83fe6a93
JE
1692 goto reject;
1693 }
1694
1695 switch (fc_frame_payload_op(fp)) {
131203a1 1696 case ELS_PRLI:
92261156 1697 fc_rport_recv_prli_req(rdata, fp);
131203a1
JE
1698 break;
1699 case ELS_PRLO:
92261156 1700 fc_rport_recv_prlo_req(rdata, fp);
131203a1 1701 break;
8abbe3a4 1702 case ELS_ADISC:
92261156 1703 fc_rport_recv_adisc_req(rdata, fp);
8abbe3a4 1704 break;
131203a1 1705 case ELS_RRQ:
92261156
JE
1706 lport->tt.seq_els_rsp_send(fp, ELS_RRQ, NULL);
1707 fc_frame_free(fp);
131203a1
JE
1708 break;
1709 case ELS_REC:
92261156
JE
1710 lport->tt.seq_els_rsp_send(fp, ELS_REC, NULL);
1711 fc_frame_free(fp);
131203a1 1712 break;
63e27fb8 1713 case ELS_RLS:
92261156 1714 fc_rport_recv_rls_req(rdata, fp);
63e27fb8 1715 break;
7c5a51b8
HR
1716 case ELS_RTV:
1717 fc_rport_recv_rtv_req(rdata, fp);
1718 break;
131203a1 1719 default:
83fe6a93 1720 fc_frame_free(fp); /* can't happen */
131203a1 1721 break;
42e9a92f
RL
1722 }
1723
1724 mutex_unlock(&rdata->rp_mutex);
baa6719f 1725 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
83fe6a93
JE
1726 return;
1727
1728reject:
92261156
JE
1729 els_data.reason = ELS_RJT_UNAB;
1730 els_data.explan = ELS_EXPL_PLOGI_REQD;
1731 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
83fe6a93
JE
1732 fc_frame_free(fp);
1733}
1734
1735/**
3a3b42bf 1736 * fc_rport_recv_req() - Handler for requests
3a3b42bf 1737 * @lport: The local port that received the request
92261156 1738 * @fp: The request frame
83fe6a93
JE
1739 *
1740 * Locking Note: Called with the lport lock held.
4d2095cc
HR
1741 *
1742 * Reference counting: does not modify kref
83fe6a93 1743 */
c6b21c93 1744static void fc_rport_recv_req(struct fc_lport *lport, struct fc_frame *fp)
83fe6a93
JE
1745{
1746 struct fc_seq_els_data els_data;
1747
1748 /*
a7b12a27 1749 * Handle FLOGI, PLOGI and LOGO requests separately, since they
83fe6a93
JE
1750 * don't require prior login.
1751 * Check for unsupported opcodes first and reject them.
1752 * For some ops, it would be incorrect to reject with "PLOGI required".
1753 */
1754 switch (fc_frame_payload_op(fp)) {
a7b12a27 1755 case ELS_FLOGI:
92261156 1756 fc_rport_recv_flogi_req(lport, fp);
a7b12a27 1757 break;
83fe6a93 1758 case ELS_PLOGI:
92261156 1759 fc_rport_recv_plogi_req(lport, fp);
83fe6a93
JE
1760 break;
1761 case ELS_LOGO:
92261156 1762 fc_rport_recv_logo_req(lport, fp);
83fe6a93
JE
1763 break;
1764 case ELS_PRLI:
1765 case ELS_PRLO:
8abbe3a4 1766 case ELS_ADISC:
83fe6a93
JE
1767 case ELS_RRQ:
1768 case ELS_REC:
63e27fb8 1769 case ELS_RLS:
7c5a51b8 1770 case ELS_RTV:
92261156 1771 fc_rport_recv_els_req(lport, fp);
83fe6a93
JE
1772 break;
1773 default:
83fe6a93
JE
1774 els_data.reason = ELS_RJT_UNSUP;
1775 els_data.explan = ELS_EXPL_NONE;
92261156
JE
1776 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1777 fc_frame_free(fp);
83fe6a93
JE
1778 break;
1779 }
42e9a92f
RL
1780}
1781
1782/**
3a3b42bf
RL
1783 * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
1784 * @lport: The local port that received the PLOGI request
3a3b42bf 1785 * @rx_fp: The PLOGI request frame
42e9a92f 1786 *
3ac6f98f 1787 * Locking Note: The rport lock is held before calling this function.
4d2095cc
HR
1788 *
1789 * Reference counting: increments kref on return
42e9a92f 1790 */
3ac6f98f 1791static void fc_rport_recv_plogi_req(struct fc_lport *lport,
92261156 1792 struct fc_frame *rx_fp)
42e9a92f 1793{
3ac6f98f
JE
1794 struct fc_disc *disc;
1795 struct fc_rport_priv *rdata;
42e9a92f 1796 struct fc_frame *fp = rx_fp;
42e9a92f
RL
1797 struct fc_els_flogi *pl;
1798 struct fc_seq_els_data rjt_data;
24f089e2 1799 u32 sid;
42e9a92f 1800
251748a9 1801 sid = fc_frame_sid(fp);
42e9a92f 1802
3ac6f98f 1803 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
42e9a92f 1804
42e9a92f
RL
1805 pl = fc_frame_payload_get(fp, sizeof(*pl));
1806 if (!pl) {
3ac6f98f
JE
1807 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
1808 rjt_data.reason = ELS_RJT_PROT;
1809 rjt_data.explan = ELS_EXPL_INV_LEN;
1810 goto reject;
42e9a92f 1811 }
3ac6f98f
JE
1812
1813 disc = &lport->disc;
1814 mutex_lock(&disc->disc_mutex);
1815 rdata = lport->tt.rport_create(lport, sid);
1816 if (!rdata) {
1817 mutex_unlock(&disc->disc_mutex);
1818 rjt_data.reason = ELS_RJT_UNAB;
1819 rjt_data.explan = ELS_EXPL_INSUF_RES;
1820 goto reject;
1821 }
1822
1823 mutex_lock(&rdata->rp_mutex);
1824 mutex_unlock(&disc->disc_mutex);
1825
1826 rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1827 rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
42e9a92f
RL
1828
1829 /*
3ac6f98f 1830 * If the rport was just created, possibly due to the incoming PLOGI,
42e9a92f
RL
1831 * set the state appropriately and accept the PLOGI.
1832 *
1833 * If we had also sent a PLOGI, and if the received PLOGI is from a
1834 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1835 * "command already in progress".
1836 *
1837 * XXX TBD: If the session was ready before, the PLOGI should result in
1838 * all outstanding exchanges being reset.
1839 */
1840 switch (rdata->rp_state) {
1841 case RPORT_ST_INIT:
3ac6f98f 1842 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
42e9a92f 1843 break;
a7b12a27
JE
1844 case RPORT_ST_PLOGI_WAIT:
1845 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI_WAIT state\n");
1846 break;
42e9a92f 1847 case RPORT_ST_PLOGI:
3ac6f98f
JE
1848 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1849 if (rdata->ids.port_name < lport->wwpn) {
1850 mutex_unlock(&rdata->rp_mutex);
1851 rjt_data.reason = ELS_RJT_INPROG;
1852 rjt_data.explan = ELS_EXPL_NONE;
1853 goto reject;
1854 }
42e9a92f
RL
1855 break;
1856 case RPORT_ST_PRLI:
b4a9c7ed 1857 case RPORT_ST_RTV:
42e9a92f 1858 case RPORT_ST_READY:
370c3bd0
JE
1859 case RPORT_ST_ADISC:
1860 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
1861 "- ignored for now\n", rdata->rp_state);
1862 /* XXX TBD - should reset */
42e9a92f 1863 break;
a7b12a27 1864 case RPORT_ST_FLOGI:
14194054 1865 case RPORT_ST_DELETE:
b4a9c7ed
JE
1866 FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
1867 fc_rport_state(rdata));
1868 mutex_unlock(&rdata->rp_mutex);
1869 rjt_data.reason = ELS_RJT_BUSY;
1870 rjt_data.explan = ELS_EXPL_NONE;
1871 goto reject;
42e9a92f 1872 }
e0335f67
MR
1873 if (!fc_rport_compatible_roles(lport, rdata)) {
1874 FC_RPORT_DBG(rdata, "Received PLOGI for incompatible role\n");
1875 mutex_unlock(&rdata->rp_mutex);
1876 rjt_data.reason = ELS_RJT_LOGIC;
1877 rjt_data.explan = ELS_EXPL_NONE;
1878 goto reject;
1879 }
42e9a92f 1880
3ac6f98f
JE
1881 /*
1882 * Get session payload size from incoming PLOGI.
1883 */
1884 rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
3ac6f98f
JE
1885
1886 /*
1887 * Send LS_ACC. If this fails, the originator should retry.
1888 */
3ac6f98f
JE
1889 fp = fc_frame_alloc(lport, sizeof(*pl));
1890 if (!fp)
1891 goto out;
1892
1893 fc_plogi_fill(lport, fp, ELS_LS_ACC);
24f089e2
JE
1894 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1895 lport->tt.frame_send(lport, fp);
3ac6f98f
JE
1896 fc_rport_enter_prli(rdata);
1897out:
1898 mutex_unlock(&rdata->rp_mutex);
24f089e2 1899 fc_frame_free(rx_fp);
3ac6f98f
JE
1900 return;
1901
1902reject:
92261156 1903 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
3ac6f98f 1904 fc_frame_free(fp);
42e9a92f
RL
1905}
1906
1907/**
3a3b42bf
RL
1908 * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
1909 * @rdata: The remote port that sent the PRLI request
3a3b42bf 1910 * @rx_fp: The PRLI request frame
42e9a92f 1911 *
c1d45424 1912 * Locking Note: The rport lock is expected to be held before calling
42e9a92f
RL
1913 * this function.
1914 */
9fb9d328 1915static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
92261156 1916 struct fc_frame *rx_fp)
42e9a92f 1917{
42e9a92f 1918 struct fc_lport *lport = rdata->local_port;
42e9a92f 1919 struct fc_frame *fp;
42e9a92f
RL
1920 struct {
1921 struct fc_els_prli prli;
1922 struct fc_els_spp spp;
1923 } *pp;
1924 struct fc_els_spp *rspp; /* request service param page */
1925 struct fc_els_spp *spp; /* response spp */
1926 unsigned int len;
1927 unsigned int plen;
42e9a92f 1928 enum fc_els_spp_resp resp;
96ad8464 1929 enum fc_els_spp_resp passive;
42e9a92f 1930 struct fc_seq_els_data rjt_data;
96ad8464 1931 struct fc4_prov *prov;
42e9a92f 1932
9fb9d328
JE
1933 FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1934 fc_rport_state(rdata));
42e9a92f 1935
251748a9 1936 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
42e9a92f 1937 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
a2f6a024
JE
1938 if (!pp)
1939 goto reject_len;
1940 plen = ntohs(pp->prli.prli_len);
1941 if ((plen % 4) != 0 || plen > len || plen < 16)
1942 goto reject_len;
1943 if (plen < len)
1944 len = plen;
1945 plen = pp->prli.prli_spp_len;
1946 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1947 plen > len || len < sizeof(*pp) || plen < 12)
1948 goto reject_len;
1949 rspp = &pp->spp;
1950
1951 fp = fc_frame_alloc(lport, len);
1952 if (!fp) {
1953 rjt_data.reason = ELS_RJT_UNAB;
1954 rjt_data.explan = ELS_EXPL_INSUF_RES;
1955 goto reject;
42e9a92f 1956 }
a2f6a024
JE
1957 pp = fc_frame_payload_get(fp, len);
1958 WARN_ON(!pp);
1959 memset(pp, 0, len);
1960 pp->prli.prli_cmd = ELS_LS_ACC;
1961 pp->prli.prli_spp_len = plen;
1962 pp->prli.prli_len = htons(len);
1963 len -= sizeof(struct fc_els_prli);
42e9a92f 1964
a2f6a024
JE
1965 /*
1966 * Go through all the service parameter pages and build
1967 * response. If plen indicates longer SPP than standard,
1968 * use that. The entire response has been pre-cleared above.
1969 */
1970 spp = &pp->spp;
96ad8464 1971 mutex_lock(&fc_prov_mutex);
a2f6a024 1972 while (len >= plen) {
75a2792d 1973 rdata->spp_type = rspp->spp_type;
a2f6a024
JE
1974 spp->spp_type = rspp->spp_type;
1975 spp->spp_type_ext = rspp->spp_type_ext;
96ad8464
JE
1976 resp = 0;
1977
1978 if (rspp->spp_type < FC_FC4_PROV_SIZE) {
1979 prov = fc_active_prov[rspp->spp_type];
1980 if (prov)
1981 resp = prov->prli(rdata, plen, rspp, spp);
1982 prov = fc_passive_prov[rspp->spp_type];
1983 if (prov) {
1984 passive = prov->prli(rdata, plen, rspp, spp);
1985 if (!resp || passive == FC_SPP_RESP_ACK)
1986 resp = passive;
1987 }
1988 }
1989 if (!resp) {
1990 if (spp->spp_flags & FC_SPP_EST_IMG_PAIR)
1991 resp |= FC_SPP_RESP_CONF;
1992 else
1993 resp |= FC_SPP_RESP_INVL;
42e9a92f 1994 }
a2f6a024
JE
1995 spp->spp_flags |= resp;
1996 len -= plen;
1997 rspp = (struct fc_els_spp *)((char *)rspp + plen);
1998 spp = (struct fc_els_spp *)((char *)spp + plen);
1999 }
96ad8464 2000 mutex_unlock(&fc_prov_mutex);
a2f6a024
JE
2001
2002 /*
2003 * Send LS_ACC. If this fails, the originator should retry.
2004 */
24f089e2
JE
2005 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
2006 lport->tt.frame_send(lport, fp);
a2f6a024
JE
2007
2008 switch (rdata->rp_state) {
2009 case RPORT_ST_PRLI:
2010 fc_rport_enter_ready(rdata);
2011 break;
2012 default:
2013 break;
42e9a92f 2014 }
a2f6a024
JE
2015 goto drop;
2016
2017reject_len:
2018 rjt_data.reason = ELS_RJT_PROT;
2019 rjt_data.explan = ELS_EXPL_INV_LEN;
2020reject:
92261156 2021 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
a2f6a024 2022drop:
42e9a92f
RL
2023 fc_frame_free(rx_fp);
2024}
2025
2026/**
3a3b42bf
RL
2027 * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
2028 * @rdata: The remote port that sent the PRLO request
f8fc6c2c 2029 * @rx_fp: The PRLO request frame
42e9a92f 2030 *
c1d45424 2031 * Locking Note: The rport lock is expected to be held before calling
42e9a92f
RL
2032 * this function.
2033 */
9fb9d328 2034static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
f8fc6c2c 2035 struct fc_frame *rx_fp)
42e9a92f 2036{
42e9a92f 2037 struct fc_lport *lport = rdata->local_port;
f8fc6c2c
BPG
2038 struct fc_frame *fp;
2039 struct {
2040 struct fc_els_prlo prlo;
2041 struct fc_els_spp spp;
2042 } *pp;
2043 struct fc_els_spp *rspp; /* request service param page */
2044 struct fc_els_spp *spp; /* response spp */
2045 unsigned int len;
2046 unsigned int plen;
42e9a92f
RL
2047 struct fc_seq_els_data rjt_data;
2048
9fb9d328
JE
2049 FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
2050 fc_rport_state(rdata));
42e9a92f 2051
251748a9 2052 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
f8fc6c2c
BPG
2053 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
2054 if (!pp)
2055 goto reject_len;
2056 plen = ntohs(pp->prlo.prlo_len);
2057 if (plen != 20)
2058 goto reject_len;
2059 if (plen < len)
2060 len = plen;
2061
2062 rspp = &pp->spp;
2063
2064 fp = fc_frame_alloc(lport, len);
2065 if (!fp) {
2066 rjt_data.reason = ELS_RJT_UNAB;
2067 rjt_data.explan = ELS_EXPL_INSUF_RES;
2068 goto reject;
2069 }
2070
f8fc6c2c
BPG
2071 pp = fc_frame_payload_get(fp, len);
2072 WARN_ON(!pp);
2073 memset(pp, 0, len);
2074 pp->prlo.prlo_cmd = ELS_LS_ACC;
2075 pp->prlo.prlo_obs = 0x10;
2076 pp->prlo.prlo_len = htons(len);
2077 spp = &pp->spp;
2078 spp->spp_type = rspp->spp_type;
2079 spp->spp_type_ext = rspp->spp_type_ext;
2080 spp->spp_flags = FC_SPP_RESP_ACK;
2081
166f310b 2082 fc_rport_enter_prli(rdata);
f8fc6c2c 2083
92261156
JE
2084 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
2085 lport->tt.frame_send(lport, fp);
f8fc6c2c
BPG
2086 goto drop;
2087
2088reject_len:
2089 rjt_data.reason = ELS_RJT_PROT;
2090 rjt_data.explan = ELS_EXPL_INV_LEN;
2091reject:
92261156 2092 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
f8fc6c2c
BPG
2093drop:
2094 fc_frame_free(rx_fp);
42e9a92f
RL
2095}
2096
2097/**
3a3b42bf
RL
2098 * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
2099 * @lport: The local port that received the LOGO request
3a3b42bf 2100 * @fp: The LOGO request frame
42e9a92f 2101 *
c1d45424 2102 * Locking Note: The rport lock is expected to be held before calling
42e9a92f 2103 * this function.
4d2095cc
HR
2104 *
2105 * Reference counting: drops kref on return
42e9a92f 2106 */
92261156 2107static void fc_rport_recv_logo_req(struct fc_lport *lport, struct fc_frame *fp)
42e9a92f 2108{
83fe6a93
JE
2109 struct fc_rport_priv *rdata;
2110 u32 sid;
42e9a92f 2111
92261156 2112 lport->tt.seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
feab4ae7 2113
251748a9 2114 sid = fc_frame_sid(fp);
42e9a92f 2115
83fe6a93
JE
2116 rdata = lport->tt.rport_lookup(lport, sid);
2117 if (rdata) {
2118 mutex_lock(&rdata->rp_mutex);
2119 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
2120 fc_rport_state(rdata));
feab4ae7 2121
649eb869 2122 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
83fe6a93 2123 mutex_unlock(&rdata->rp_mutex);
baa6719f 2124 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
83fe6a93
JE
2125 } else
2126 FC_RPORT_ID_DBG(lport, sid,
2127 "Received LOGO from non-logged-in port\n");
42e9a92f
RL
2128 fc_frame_free(fp);
2129}
2130
3a3b42bf
RL
2131/**
2132 * fc_rport_flush_queue() - Flush the rport_event_queue
2133 */
42e9a92f
RL
2134static void fc_rport_flush_queue(void)
2135{
2136 flush_workqueue(rport_event_queue);
2137}
2138
3a3b42bf
RL
2139/**
2140 * fc_rport_init() - Initialize the remote port layer for a local port
2141 * @lport: The local port to initialize the remote port layer for
2142 */
42e9a92f
RL
2143int fc_rport_init(struct fc_lport *lport)
2144{
8025b5db
JE
2145 if (!lport->tt.rport_lookup)
2146 lport->tt.rport_lookup = fc_rport_lookup;
2147
5101ff99 2148 if (!lport->tt.rport_create)
9e9d0452 2149 lport->tt.rport_create = fc_rport_create;
5101ff99 2150
42e9a92f
RL
2151 if (!lport->tt.rport_login)
2152 lport->tt.rport_login = fc_rport_login;
2153
2154 if (!lport->tt.rport_logoff)
2155 lport->tt.rport_logoff = fc_rport_logoff;
2156
2157 if (!lport->tt.rport_recv_req)
2158 lport->tt.rport_recv_req = fc_rport_recv_req;
2159
2160 if (!lport->tt.rport_flush_queue)
2161 lport->tt.rport_flush_queue = fc_rport_flush_queue;
2162
f211fa51
JE
2163 if (!lport->tt.rport_destroy)
2164 lport->tt.rport_destroy = fc_rport_destroy;
2165
42e9a92f
RL
2166 return 0;
2167}
2168EXPORT_SYMBOL(fc_rport_init);
2169
96ad8464
JE
2170/**
2171 * fc_rport_fcp_prli() - Handle incoming PRLI for the FCP initiator.
2172 * @rdata: remote port private
2173 * @spp_len: service parameter page length
2174 * @rspp: received service parameter page
2175 * @spp: response service parameter page
2176 *
2177 * Returns the value for the response code to be placed in spp_flags;
2178 * Returns 0 if not an initiator.
2179 */
2180static int fc_rport_fcp_prli(struct fc_rport_priv *rdata, u32 spp_len,
2181 const struct fc_els_spp *rspp,
2182 struct fc_els_spp *spp)
2183{
2184 struct fc_lport *lport = rdata->local_port;
2185 u32 fcp_parm;
2186
2187 fcp_parm = ntohl(rspp->spp_params);
2188 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
2189 if (fcp_parm & FCP_SPPF_INIT_FCN)
2190 rdata->ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2191 if (fcp_parm & FCP_SPPF_TARG_FCN)
2192 rdata->ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2193 if (fcp_parm & FCP_SPPF_RETRY)
2194 rdata->flags |= FC_RP_FLAGS_RETRY;
2195 rdata->supported_classes = FC_COS_CLASS3;
2196
732bdb9d 2197 if (!(lport->service_params & FCP_SPPF_INIT_FCN))
96ad8464
JE
2198 return 0;
2199
2200 spp->spp_flags |= rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
2201
2202 /*
2203 * OR in our service parameters with other providers (target), if any.
2204 */
2205 fcp_parm = ntohl(spp->spp_params);
2206 spp->spp_params = htonl(fcp_parm | lport->service_params);
2207 return FC_SPP_RESP_ACK;
2208}
2209
2210/*
2211 * FC-4 provider ops for FCP initiator.
2212 */
2213struct fc4_prov fc_rport_fcp_init = {
2214 .prli = fc_rport_fcp_prli,
2215};
2216
2217/**
2218 * fc_rport_t0_prli() - Handle incoming PRLI parameters for type 0
2219 * @rdata: remote port private
2220 * @spp_len: service parameter page length
2221 * @rspp: received service parameter page
2222 * @spp: response service parameter page
2223 */
2224static int fc_rport_t0_prli(struct fc_rport_priv *rdata, u32 spp_len,
2225 const struct fc_els_spp *rspp,
2226 struct fc_els_spp *spp)
2227{
2228 if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR)
2229 return FC_SPP_RESP_INVL;
2230 return FC_SPP_RESP_ACK;
2231}
2232
2233/*
2234 * FC-4 provider ops for type 0 service parameters.
2235 *
2236 * This handles the special case of type 0 which is always successful
2237 * but doesn't do anything otherwise.
2238 */
2239struct fc4_prov fc_rport_t0_prov = {
2240 .prli = fc_rport_t0_prli,
2241};
2242
3a3b42bf
RL
2243/**
2244 * fc_setup_rport() - Initialize the rport_event_queue
2245 */
55204909 2246int fc_setup_rport(void)
42e9a92f
RL
2247{
2248 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
2249 if (!rport_event_queue)
2250 return -ENOMEM;
2251 return 0;
2252}
42e9a92f 2253
3a3b42bf
RL
2254/**
2255 * fc_destroy_rport() - Destroy the rport_event_queue
2256 */
55204909 2257void fc_destroy_rport(void)
42e9a92f
RL
2258{
2259 destroy_workqueue(rport_event_queue);
2260}
42e9a92f 2261
3a3b42bf
RL
2262/**
2263 * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
2264 * @rport: The remote port whose I/O should be terminated
2265 */
42e9a92f
RL
2266void fc_rport_terminate_io(struct fc_rport *rport)
2267{
3a3b42bf
RL
2268 struct fc_rport_libfc_priv *rpriv = rport->dd_data;
2269 struct fc_lport *lport = rpriv->local_port;
42e9a92f 2270
1f6ff364
AJ
2271 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
2272 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
42e9a92f
RL
2273}
2274EXPORT_SYMBOL(fc_rport_terminate_io);