lkdtm: Add Control Flow Integrity test
[linux/fpc-iii.git] / drivers / scsi / libfc / fc_rport.c
blobe0f3852fdad1543e8d668f828d2555bc78afab3a
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved.
5 * Maintained at www.Open-FCoE.org
6 */
8 /*
9 * RPORT GENERAL INFO
11 * This file contains all processing regarding fc_rports. It contains the
12 * rport state machine and does all rport interaction with the transport class.
13 * There should be no other places in libfc that interact directly with the
14 * transport class in regards to adding and deleting rports.
16 * fc_rport's represent N_Port's within the fabric.
20 * RPORT LOCKING
22 * The rport should never hold the rport mutex and then attempt to acquire
23 * either the lport or disc mutexes. The rport's mutex is considered lesser
24 * than both the lport's mutex and the disc mutex. Refer to fc_lport.c for
25 * more comments on the hierarchy.
27 * The locking strategy is similar to the lport's strategy. The lock protects
28 * the rport's states and is held and released by the entry points to the rport
29 * block. All _enter_* functions correspond to rport states and expect the rport
30 * mutex to be locked before calling them. This means that rports only handle
31 * one request or response at a time, since they're not critical for the I/O
32 * path this potential over-use of the mutex is acceptable.
36 * RPORT REFERENCE COUNTING
38 * A rport reference should be taken when:
39 * - an rport is allocated
40 * - a workqueue item is scheduled
41 * - an ELS request is send
42 * The reference should be dropped when:
43 * - the workqueue function has finished
44 * - the ELS response is handled
45 * - an rport is removed
48 #include <linux/kernel.h>
49 #include <linux/spinlock.h>
50 #include <linux/interrupt.h>
51 #include <linux/slab.h>
52 #include <linux/rcupdate.h>
53 #include <linux/timer.h>
54 #include <linux/workqueue.h>
55 #include <linux/export.h>
56 #include <linux/rculist.h>
58 #include <asm/unaligned.h>
60 #include <scsi/libfc.h>
61 #include <scsi/fc_encode.h>
63 #include "fc_libfc.h"
65 static struct workqueue_struct *rport_event_queue;
67 static void fc_rport_enter_flogi(struct fc_rport_priv *);
68 static void fc_rport_enter_plogi(struct fc_rport_priv *);
69 static void fc_rport_enter_prli(struct fc_rport_priv *);
70 static void fc_rport_enter_rtv(struct fc_rport_priv *);
71 static void fc_rport_enter_ready(struct fc_rport_priv *);
72 static void fc_rport_enter_logo(struct fc_rport_priv *);
73 static void fc_rport_enter_adisc(struct fc_rport_priv *);
75 static void fc_rport_recv_plogi_req(struct fc_lport *, struct fc_frame *);
76 static void fc_rport_recv_prli_req(struct fc_rport_priv *, struct fc_frame *);
77 static void fc_rport_recv_prlo_req(struct fc_rport_priv *, struct fc_frame *);
78 static void fc_rport_recv_logo_req(struct fc_lport *, struct fc_frame *);
79 static void fc_rport_timeout(struct work_struct *);
80 static void fc_rport_error(struct fc_rport_priv *, int);
81 static void fc_rport_error_retry(struct fc_rport_priv *, int);
82 static void fc_rport_work(struct work_struct *);
84 static const char *fc_rport_state_names[] = {
85 [RPORT_ST_INIT] = "Init",
86 [RPORT_ST_FLOGI] = "FLOGI",
87 [RPORT_ST_PLOGI_WAIT] = "PLOGI_WAIT",
88 [RPORT_ST_PLOGI] = "PLOGI",
89 [RPORT_ST_PRLI] = "PRLI",
90 [RPORT_ST_RTV] = "RTV",
91 [RPORT_ST_READY] = "Ready",
92 [RPORT_ST_ADISC] = "ADISC",
93 [RPORT_ST_DELETE] = "Delete",
96 /**
97 * fc_rport_lookup() - Lookup a remote port by port_id
98 * @lport: The local port to lookup the remote port on
99 * @port_id: The remote port ID to look up
101 * The reference count of the fc_rport_priv structure is
102 * increased by one.
104 struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport,
105 u32 port_id)
107 struct fc_rport_priv *rdata = NULL, *tmp_rdata;
109 rcu_read_lock();
110 list_for_each_entry_rcu(tmp_rdata, &lport->disc.rports, peers)
111 if (tmp_rdata->ids.port_id == port_id &&
112 kref_get_unless_zero(&tmp_rdata->kref)) {
113 rdata = tmp_rdata;
114 break;
116 rcu_read_unlock();
117 return rdata;
119 EXPORT_SYMBOL(fc_rport_lookup);
122 * fc_rport_create() - Create a new remote port
123 * @lport: The local port this remote port will be associated with
124 * @ids: The identifiers for the new remote port
126 * The remote port will start in the INIT state.
128 struct fc_rport_priv *fc_rport_create(struct fc_lport *lport, u32 port_id)
130 struct fc_rport_priv *rdata;
132 lockdep_assert_held(&lport->disc.disc_mutex);
134 rdata = fc_rport_lookup(lport, port_id);
135 if (rdata)
136 return rdata;
138 rdata = kzalloc(sizeof(*rdata) + lport->rport_priv_size, GFP_KERNEL);
139 if (!rdata)
140 return NULL;
142 rdata->ids.node_name = -1;
143 rdata->ids.port_name = -1;
144 rdata->ids.port_id = port_id;
145 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
147 kref_init(&rdata->kref);
148 mutex_init(&rdata->rp_mutex);
149 rdata->local_port = lport;
150 rdata->rp_state = RPORT_ST_INIT;
151 rdata->event = RPORT_EV_NONE;
152 rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
153 rdata->e_d_tov = lport->e_d_tov;
154 rdata->r_a_tov = lport->r_a_tov;
155 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
156 INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
157 INIT_WORK(&rdata->event_work, fc_rport_work);
158 if (port_id != FC_FID_DIR_SERV) {
159 rdata->lld_event_callback = lport->tt.rport_event_callback;
160 list_add_rcu(&rdata->peers, &lport->disc.rports);
162 return rdata;
164 EXPORT_SYMBOL(fc_rport_create);
167 * fc_rport_destroy() - Free a remote port after last reference is released
168 * @kref: The remote port's kref
170 void fc_rport_destroy(struct kref *kref)
172 struct fc_rport_priv *rdata;
174 rdata = container_of(kref, struct fc_rport_priv, kref);
175 kfree_rcu(rdata, rcu);
177 EXPORT_SYMBOL(fc_rport_destroy);
180 * fc_rport_state() - Return a string identifying the remote port's state
181 * @rdata: The remote port
183 static const char *fc_rport_state(struct fc_rport_priv *rdata)
185 const char *cp;
187 cp = fc_rport_state_names[rdata->rp_state];
188 if (!cp)
189 cp = "Unknown";
190 return cp;
194 * fc_set_rport_loss_tmo() - Set the remote port loss timeout
195 * @rport: The remote port that gets a new timeout value
196 * @timeout: The new timeout value (in seconds)
198 void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
200 if (timeout)
201 rport->dev_loss_tmo = timeout;
202 else
203 rport->dev_loss_tmo = 1;
205 EXPORT_SYMBOL(fc_set_rport_loss_tmo);
208 * fc_plogi_get_maxframe() - Get the maximum payload from the common service
209 * parameters in a FLOGI frame
210 * @flp: The FLOGI or PLOGI payload
211 * @maxval: The maximum frame size upper limit; this may be less than what
212 * is in the service parameters
214 static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
215 unsigned int maxval)
217 unsigned int mfs;
220 * Get max payload from the common service parameters and the
221 * class 3 receive data field size.
223 mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
224 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
225 maxval = mfs;
226 mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
227 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
228 maxval = mfs;
229 return maxval;
233 * fc_rport_state_enter() - Change the state of a remote port
234 * @rdata: The remote port whose state should change
235 * @new: The new state
237 static void fc_rport_state_enter(struct fc_rport_priv *rdata,
238 enum fc_rport_state new)
240 lockdep_assert_held(&rdata->rp_mutex);
242 if (rdata->rp_state != new)
243 rdata->retries = 0;
244 rdata->rp_state = new;
248 * fc_rport_work() - Handler for remote port events in the rport_event_queue
249 * @work: Handle to the remote port being dequeued
251 * Reference counting: drops kref on return
253 static void fc_rport_work(struct work_struct *work)
255 u32 port_id;
256 struct fc_rport_priv *rdata =
257 container_of(work, struct fc_rport_priv, event_work);
258 struct fc_rport_libfc_priv *rpriv;
259 enum fc_rport_event event;
260 struct fc_lport *lport = rdata->local_port;
261 struct fc_rport_operations *rport_ops;
262 struct fc_rport_identifiers ids;
263 struct fc_rport *rport;
264 struct fc4_prov *prov;
265 u8 type;
267 mutex_lock(&rdata->rp_mutex);
268 event = rdata->event;
269 rport_ops = rdata->ops;
270 rport = rdata->rport;
272 FC_RPORT_DBG(rdata, "work event %u\n", event);
274 switch (event) {
275 case RPORT_EV_READY:
276 ids = rdata->ids;
277 rdata->event = RPORT_EV_NONE;
278 rdata->major_retries = 0;
279 kref_get(&rdata->kref);
280 mutex_unlock(&rdata->rp_mutex);
282 if (!rport) {
283 FC_RPORT_DBG(rdata, "No rport!\n");
284 rport = fc_remote_port_add(lport->host, 0, &ids);
286 if (!rport) {
287 FC_RPORT_DBG(rdata, "Failed to add the rport\n");
288 fc_rport_logoff(rdata);
289 kref_put(&rdata->kref, fc_rport_destroy);
290 return;
292 mutex_lock(&rdata->rp_mutex);
293 if (rdata->rport)
294 FC_RPORT_DBG(rdata, "rport already allocated\n");
295 rdata->rport = rport;
296 rport->maxframe_size = rdata->maxframe_size;
297 rport->supported_classes = rdata->supported_classes;
299 rpriv = rport->dd_data;
300 rpriv->local_port = lport;
301 rpriv->rp_state = rdata->rp_state;
302 rpriv->flags = rdata->flags;
303 rpriv->e_d_tov = rdata->e_d_tov;
304 rpriv->r_a_tov = rdata->r_a_tov;
305 mutex_unlock(&rdata->rp_mutex);
307 if (rport_ops && rport_ops->event_callback) {
308 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
309 rport_ops->event_callback(lport, rdata, event);
311 if (rdata->lld_event_callback) {
312 FC_RPORT_DBG(rdata, "lld callback ev %d\n", event);
313 rdata->lld_event_callback(lport, rdata, event);
315 kref_put(&rdata->kref, fc_rport_destroy);
316 break;
318 case RPORT_EV_FAILED:
319 case RPORT_EV_LOGO:
320 case RPORT_EV_STOP:
321 if (rdata->prli_count) {
322 mutex_lock(&fc_prov_mutex);
323 for (type = 1; type < FC_FC4_PROV_SIZE; type++) {
324 prov = fc_passive_prov[type];
325 if (prov && prov->prlo)
326 prov->prlo(rdata);
328 mutex_unlock(&fc_prov_mutex);
330 port_id = rdata->ids.port_id;
331 mutex_unlock(&rdata->rp_mutex);
333 if (rport_ops && rport_ops->event_callback) {
334 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
335 rport_ops->event_callback(lport, rdata, event);
337 if (rdata->lld_event_callback) {
338 FC_RPORT_DBG(rdata, "lld callback ev %d\n", event);
339 rdata->lld_event_callback(lport, rdata, event);
341 if (cancel_delayed_work_sync(&rdata->retry_work))
342 kref_put(&rdata->kref, fc_rport_destroy);
345 * Reset any outstanding exchanges before freeing rport.
347 lport->tt.exch_mgr_reset(lport, 0, port_id);
348 lport->tt.exch_mgr_reset(lport, port_id, 0);
350 if (rport) {
351 rpriv = rport->dd_data;
352 rpriv->rp_state = RPORT_ST_DELETE;
353 mutex_lock(&rdata->rp_mutex);
354 rdata->rport = NULL;
355 mutex_unlock(&rdata->rp_mutex);
356 fc_remote_port_delete(rport);
359 mutex_lock(&rdata->rp_mutex);
360 if (rdata->rp_state == RPORT_ST_DELETE) {
361 if (port_id == FC_FID_DIR_SERV) {
362 rdata->event = RPORT_EV_NONE;
363 mutex_unlock(&rdata->rp_mutex);
364 kref_put(&rdata->kref, fc_rport_destroy);
365 } else if ((rdata->flags & FC_RP_STARTED) &&
366 rdata->major_retries <
367 lport->max_rport_retry_count) {
368 rdata->major_retries++;
369 rdata->event = RPORT_EV_NONE;
370 FC_RPORT_DBG(rdata, "work restart\n");
371 fc_rport_enter_flogi(rdata);
372 mutex_unlock(&rdata->rp_mutex);
373 } else {
374 mutex_unlock(&rdata->rp_mutex);
375 FC_RPORT_DBG(rdata, "work delete\n");
376 mutex_lock(&lport->disc.disc_mutex);
377 list_del_rcu(&rdata->peers);
378 mutex_unlock(&lport->disc.disc_mutex);
379 kref_put(&rdata->kref, fc_rport_destroy);
381 } else {
383 * Re-open for events. Reissue READY event if ready.
385 rdata->event = RPORT_EV_NONE;
386 if (rdata->rp_state == RPORT_ST_READY) {
387 FC_RPORT_DBG(rdata, "work reopen\n");
388 fc_rport_enter_ready(rdata);
390 mutex_unlock(&rdata->rp_mutex);
392 break;
394 default:
395 mutex_unlock(&rdata->rp_mutex);
396 break;
398 kref_put(&rdata->kref, fc_rport_destroy);
402 * fc_rport_login() - Start the remote port login state machine
403 * @rdata: The remote port to be logged in to
405 * Initiates the RP state machine. It is called from the LP module.
406 * This function will issue the following commands to the N_Port
407 * identified by the FC ID provided.
409 * - PLOGI
410 * - PRLI
411 * - RTV
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.
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.
421 int fc_rport_login(struct fc_rport_priv *rdata)
423 mutex_lock(&rdata->rp_mutex);
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;
431 rdata->flags |= FC_RP_STARTED;
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;
437 case RPORT_ST_DELETE:
438 FC_RPORT_DBG(rdata, "Restart deleted port\n");
439 break;
440 case RPORT_ST_INIT:
441 FC_RPORT_DBG(rdata, "Login to port\n");
442 fc_rport_enter_flogi(rdata);
443 break;
444 default:
445 FC_RPORT_DBG(rdata, "Login in progress, state %s\n",
446 fc_rport_state(rdata));
447 break;
449 mutex_unlock(&rdata->rp_mutex);
451 return 0;
453 EXPORT_SYMBOL(fc_rport_login);
456 * fc_rport_enter_delete() - Schedule a remote port to be deleted
457 * @rdata: The remote port to be deleted
458 * @event: The event to report as the reason for deletion
460 * Allow state change into DELETE only once.
462 * Call queue_work only if there's no event already pending.
463 * Set the new event so that the old pending event will not occur.
464 * Since we have the mutex, even if fc_rport_work() is already started,
465 * it'll see the new event.
467 * Reference counting: does not modify kref
469 static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
470 enum fc_rport_event event)
472 lockdep_assert_held(&rdata->rp_mutex);
474 if (rdata->rp_state == RPORT_ST_DELETE)
475 return;
477 FC_RPORT_DBG(rdata, "Delete port\n");
479 fc_rport_state_enter(rdata, RPORT_ST_DELETE);
481 kref_get(&rdata->kref);
482 if (rdata->event == RPORT_EV_NONE &&
483 !queue_work(rport_event_queue, &rdata->event_work))
484 kref_put(&rdata->kref, fc_rport_destroy);
486 rdata->event = event;
490 * fc_rport_logoff() - Logoff and remove a remote port
491 * @rdata: The remote port to be logged off of
493 * Locking Note: Called without the rport lock held. This
494 * function will hold the rport lock, call an _enter_*
495 * function and then unlock the rport.
497 int fc_rport_logoff(struct fc_rport_priv *rdata)
499 struct fc_lport *lport = rdata->local_port;
500 u32 port_id = rdata->ids.port_id;
502 mutex_lock(&rdata->rp_mutex);
504 FC_RPORT_DBG(rdata, "Remove port\n");
506 rdata->flags &= ~FC_RP_STARTED;
507 if (rdata->rp_state == RPORT_ST_DELETE) {
508 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
509 goto out;
512 * FC-LS states:
513 * To explicitly Logout, the initiating Nx_Port shall terminate
514 * other open Sequences that it initiated with the destination
515 * Nx_Port prior to performing Logout.
517 lport->tt.exch_mgr_reset(lport, 0, port_id);
518 lport->tt.exch_mgr_reset(lport, port_id, 0);
520 fc_rport_enter_logo(rdata);
523 * Change the state to Delete so that we discard
524 * the response.
526 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
527 out:
528 mutex_unlock(&rdata->rp_mutex);
529 return 0;
531 EXPORT_SYMBOL(fc_rport_logoff);
534 * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state
535 * @rdata: The remote port that is ready
537 * Reference counting: schedules workqueue, does not modify kref
539 static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
541 lockdep_assert_held(&rdata->rp_mutex);
543 fc_rport_state_enter(rdata, RPORT_ST_READY);
545 FC_RPORT_DBG(rdata, "Port is Ready\n");
547 kref_get(&rdata->kref);
548 if (rdata->event == RPORT_EV_NONE &&
549 !queue_work(rport_event_queue, &rdata->event_work))
550 kref_put(&rdata->kref, fc_rport_destroy);
552 rdata->event = RPORT_EV_READY;
556 * fc_rport_timeout() - Handler for the retry_work timer
557 * @work: Handle to the remote port that has timed out
559 * Locking Note: Called without the rport lock held. This
560 * function will hold the rport lock, call an _enter_*
561 * function and then unlock the rport.
563 * Reference counting: Drops kref on return.
565 static void fc_rport_timeout(struct work_struct *work)
567 struct fc_rport_priv *rdata =
568 container_of(work, struct fc_rport_priv, retry_work.work);
570 mutex_lock(&rdata->rp_mutex);
571 FC_RPORT_DBG(rdata, "Port timeout, state %s\n", fc_rport_state(rdata));
573 switch (rdata->rp_state) {
574 case RPORT_ST_FLOGI:
575 fc_rport_enter_flogi(rdata);
576 break;
577 case RPORT_ST_PLOGI:
578 fc_rport_enter_plogi(rdata);
579 break;
580 case RPORT_ST_PRLI:
581 fc_rport_enter_prli(rdata);
582 break;
583 case RPORT_ST_RTV:
584 fc_rport_enter_rtv(rdata);
585 break;
586 case RPORT_ST_ADISC:
587 fc_rport_enter_adisc(rdata);
588 break;
589 case RPORT_ST_PLOGI_WAIT:
590 case RPORT_ST_READY:
591 case RPORT_ST_INIT:
592 case RPORT_ST_DELETE:
593 break;
596 mutex_unlock(&rdata->rp_mutex);
597 kref_put(&rdata->kref, fc_rport_destroy);
601 * fc_rport_error() - Error handler, called once retries have been exhausted
602 * @rdata: The remote port the error is happened on
603 * @err: The error code
605 * Reference counting: does not modify kref
607 static void fc_rport_error(struct fc_rport_priv *rdata, int err)
609 struct fc_lport *lport = rdata->local_port;
611 lockdep_assert_held(&rdata->rp_mutex);
613 FC_RPORT_DBG(rdata, "Error %d in state %s, retries %d\n",
614 -err, fc_rport_state(rdata), rdata->retries);
616 switch (rdata->rp_state) {
617 case RPORT_ST_FLOGI:
618 rdata->flags &= ~FC_RP_STARTED;
619 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
620 break;
621 case RPORT_ST_PLOGI:
622 if (lport->point_to_multipoint) {
623 rdata->flags &= ~FC_RP_STARTED;
624 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
625 } else
626 fc_rport_enter_logo(rdata);
627 break;
628 case RPORT_ST_RTV:
629 fc_rport_enter_ready(rdata);
630 break;
631 case RPORT_ST_PRLI:
632 case RPORT_ST_ADISC:
633 fc_rport_enter_logo(rdata);
634 break;
635 case RPORT_ST_PLOGI_WAIT:
636 case RPORT_ST_DELETE:
637 case RPORT_ST_READY:
638 case RPORT_ST_INIT:
639 break;
644 * fc_rport_error_retry() - Handler for remote port state retries
645 * @rdata: The remote port whose state is to be retried
646 * @err: The error code
648 * If the error was an exchange timeout retry immediately,
649 * otherwise wait for E_D_TOV.
651 * Reference counting: increments kref when scheduling retry_work
653 static void fc_rport_error_retry(struct fc_rport_priv *rdata, int err)
655 unsigned long delay = msecs_to_jiffies(rdata->e_d_tov);
657 lockdep_assert_held(&rdata->rp_mutex);
659 /* make sure this isn't an FC_EX_CLOSED error, never retry those */
660 if (err == -FC_EX_CLOSED)
661 goto out;
663 if (rdata->retries < rdata->local_port->max_rport_retry_count) {
664 FC_RPORT_DBG(rdata, "Error %d in state %s, retrying\n",
665 err, fc_rport_state(rdata));
666 rdata->retries++;
667 /* no additional delay on exchange timeouts */
668 if (err == -FC_EX_TIMEOUT)
669 delay = 0;
670 kref_get(&rdata->kref);
671 if (!schedule_delayed_work(&rdata->retry_work, delay))
672 kref_put(&rdata->kref, fc_rport_destroy);
673 return;
676 out:
677 fc_rport_error(rdata, err);
681 * fc_rport_login_complete() - Handle parameters and completion of p-mp login.
682 * @rdata: The remote port which we logged into or which logged into us.
683 * @fp: The FLOGI or PLOGI request or response frame
685 * Returns non-zero error if a problem is detected with the frame.
686 * Does not free the frame.
688 * This is only used in point-to-multipoint mode for FIP currently.
690 static int fc_rport_login_complete(struct fc_rport_priv *rdata,
691 struct fc_frame *fp)
693 struct fc_lport *lport = rdata->local_port;
694 struct fc_els_flogi *flogi;
695 unsigned int e_d_tov;
696 u16 csp_flags;
698 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
699 if (!flogi)
700 return -EINVAL;
702 csp_flags = ntohs(flogi->fl_csp.sp_features);
704 if (fc_frame_payload_op(fp) == ELS_FLOGI) {
705 if (csp_flags & FC_SP_FT_FPORT) {
706 FC_RPORT_DBG(rdata, "Fabric bit set in FLOGI\n");
707 return -EINVAL;
709 } else {
712 * E_D_TOV is not valid on an incoming FLOGI request.
714 e_d_tov = ntohl(flogi->fl_csp.sp_e_d_tov);
715 if (csp_flags & FC_SP_FT_EDTR)
716 e_d_tov /= 1000000;
717 if (e_d_tov > rdata->e_d_tov)
718 rdata->e_d_tov = e_d_tov;
720 rdata->maxframe_size = fc_plogi_get_maxframe(flogi, lport->mfs);
721 return 0;
725 * fc_rport_flogi_resp() - Handle response to FLOGI request for p-mp mode
726 * @sp: The sequence that the FLOGI was on
727 * @fp: The FLOGI response frame
728 * @rp_arg: The remote port that received the FLOGI response
730 static void fc_rport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
731 void *rp_arg)
733 struct fc_rport_priv *rdata = rp_arg;
734 struct fc_lport *lport = rdata->local_port;
735 struct fc_els_flogi *flogi;
736 unsigned int r_a_tov;
737 u8 opcode;
738 int err = 0;
740 FC_RPORT_DBG(rdata, "Received a FLOGI %s\n",
741 IS_ERR(fp) ? "error" : fc_els_resp_type(fp));
743 if (fp == ERR_PTR(-FC_EX_CLOSED))
744 goto put;
746 mutex_lock(&rdata->rp_mutex);
748 if (rdata->rp_state != RPORT_ST_FLOGI) {
749 FC_RPORT_DBG(rdata, "Received a FLOGI response, but in state "
750 "%s\n", fc_rport_state(rdata));
751 if (IS_ERR(fp))
752 goto err;
753 goto out;
756 if (IS_ERR(fp)) {
757 fc_rport_error(rdata, PTR_ERR(fp));
758 goto err;
760 opcode = fc_frame_payload_op(fp);
761 if (opcode == ELS_LS_RJT) {
762 struct fc_els_ls_rjt *rjt;
764 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
765 FC_RPORT_DBG(rdata, "FLOGI ELS rejected, reason %x expl %x\n",
766 rjt->er_reason, rjt->er_explan);
767 err = -FC_EX_ELS_RJT;
768 goto bad;
769 } else if (opcode != ELS_LS_ACC) {
770 FC_RPORT_DBG(rdata, "FLOGI ELS invalid opcode %x\n", opcode);
771 err = -FC_EX_ELS_RJT;
772 goto bad;
774 if (fc_rport_login_complete(rdata, fp)) {
775 FC_RPORT_DBG(rdata, "FLOGI failed, no login\n");
776 err = -FC_EX_INV_LOGIN;
777 goto bad;
780 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
781 if (!flogi) {
782 err = -FC_EX_ALLOC_ERR;
783 goto bad;
785 r_a_tov = ntohl(flogi->fl_csp.sp_r_a_tov);
786 if (r_a_tov > rdata->r_a_tov)
787 rdata->r_a_tov = r_a_tov;
789 if (rdata->ids.port_name < lport->wwpn)
790 fc_rport_enter_plogi(rdata);
791 else
792 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
793 out:
794 fc_frame_free(fp);
795 err:
796 mutex_unlock(&rdata->rp_mutex);
797 put:
798 kref_put(&rdata->kref, fc_rport_destroy);
799 return;
800 bad:
801 FC_RPORT_DBG(rdata, "Bad FLOGI response\n");
802 fc_rport_error_retry(rdata, err);
803 goto out;
807 * fc_rport_enter_flogi() - Send a FLOGI request to the remote port for p-mp
808 * @rdata: The remote port to send a FLOGI to
810 * Reference counting: increments kref when sending ELS
812 static void fc_rport_enter_flogi(struct fc_rport_priv *rdata)
814 struct fc_lport *lport = rdata->local_port;
815 struct fc_frame *fp;
817 lockdep_assert_held(&rdata->rp_mutex);
819 if (!lport->point_to_multipoint)
820 return fc_rport_enter_plogi(rdata);
822 FC_RPORT_DBG(rdata, "Entered FLOGI state from %s state\n",
823 fc_rport_state(rdata));
825 fc_rport_state_enter(rdata, RPORT_ST_FLOGI);
827 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
828 if (!fp)
829 return fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
831 kref_get(&rdata->kref);
832 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_FLOGI,
833 fc_rport_flogi_resp, rdata,
834 2 * lport->r_a_tov)) {
835 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
836 kref_put(&rdata->kref, fc_rport_destroy);
841 * fc_rport_recv_flogi_req() - Handle Fabric Login (FLOGI) request in p-mp mode
842 * @lport: The local port that received the PLOGI request
843 * @rx_fp: The PLOGI request frame
845 * Reference counting: drops kref on return
847 static void fc_rport_recv_flogi_req(struct fc_lport *lport,
848 struct fc_frame *rx_fp)
850 struct fc_els_flogi *flp;
851 struct fc_rport_priv *rdata;
852 struct fc_frame *fp = rx_fp;
853 struct fc_seq_els_data rjt_data;
854 u32 sid;
856 sid = fc_frame_sid(fp);
858 FC_RPORT_ID_DBG(lport, sid, "Received FLOGI request\n");
860 if (!lport->point_to_multipoint) {
861 rjt_data.reason = ELS_RJT_UNSUP;
862 rjt_data.explan = ELS_EXPL_NONE;
863 goto reject;
866 flp = fc_frame_payload_get(fp, sizeof(*flp));
867 if (!flp) {
868 rjt_data.reason = ELS_RJT_LOGIC;
869 rjt_data.explan = ELS_EXPL_INV_LEN;
870 goto reject;
873 rdata = fc_rport_lookup(lport, sid);
874 if (!rdata) {
875 rjt_data.reason = ELS_RJT_FIP;
876 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
877 goto reject;
879 mutex_lock(&rdata->rp_mutex);
881 FC_RPORT_DBG(rdata, "Received FLOGI in %s state\n",
882 fc_rport_state(rdata));
884 switch (rdata->rp_state) {
885 case RPORT_ST_INIT:
887 * If received the FLOGI request on RPORT which is INIT state
888 * (means not transition to FLOGI either fc_rport timeout
889 * function didn;t trigger or this end hasn;t received
890 * beacon yet from other end. In that case only, allow RPORT
891 * state machine to continue, otherwise fall through which
892 * causes the code to send reject response.
893 * NOTE; Not checking for FIP->state such as VNMP_UP or
894 * VNMP_CLAIM because if FIP state is not one of those,
895 * RPORT wouldn;t have created and 'rport_lookup' would have
896 * failed anyway in that case.
898 break;
899 case RPORT_ST_DELETE:
900 mutex_unlock(&rdata->rp_mutex);
901 rjt_data.reason = ELS_RJT_FIP;
902 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
903 goto reject_put;
904 case RPORT_ST_FLOGI:
905 case RPORT_ST_PLOGI_WAIT:
906 case RPORT_ST_PLOGI:
907 break;
908 case RPORT_ST_PRLI:
909 case RPORT_ST_RTV:
910 case RPORT_ST_READY:
911 case RPORT_ST_ADISC:
913 * Set the remote port to be deleted and to then restart.
914 * This queues work to be sure exchanges are reset.
916 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
917 mutex_unlock(&rdata->rp_mutex);
918 rjt_data.reason = ELS_RJT_BUSY;
919 rjt_data.explan = ELS_EXPL_NONE;
920 goto reject_put;
922 if (fc_rport_login_complete(rdata, fp)) {
923 mutex_unlock(&rdata->rp_mutex);
924 rjt_data.reason = ELS_RJT_LOGIC;
925 rjt_data.explan = ELS_EXPL_NONE;
926 goto reject_put;
929 fp = fc_frame_alloc(lport, sizeof(*flp));
930 if (!fp)
931 goto out;
933 fc_flogi_fill(lport, fp);
934 flp = fc_frame_payload_get(fp, sizeof(*flp));
935 flp->fl_cmd = ELS_LS_ACC;
937 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
938 lport->tt.frame_send(lport, fp);
941 * Do not proceed with the state machine if our
942 * FLOGI has crossed with an FLOGI from the
943 * remote port; wait for the FLOGI response instead.
945 if (rdata->rp_state != RPORT_ST_FLOGI) {
946 if (rdata->ids.port_name < lport->wwpn)
947 fc_rport_enter_plogi(rdata);
948 else
949 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
951 out:
952 mutex_unlock(&rdata->rp_mutex);
953 kref_put(&rdata->kref, fc_rport_destroy);
954 fc_frame_free(rx_fp);
955 return;
957 reject_put:
958 kref_put(&rdata->kref, fc_rport_destroy);
959 reject:
960 fc_seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
961 fc_frame_free(rx_fp);
965 * fc_rport_plogi_resp() - Handler for ELS PLOGI responses
966 * @sp: The sequence the PLOGI is on
967 * @fp: The PLOGI response frame
968 * @rdata_arg: The remote port that sent the PLOGI response
970 * Locking Note: This function will be called without the rport lock
971 * held, but it will lock, call an _enter_* function or fc_rport_error
972 * and then unlock the rport.
974 static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
975 void *rdata_arg)
977 struct fc_rport_priv *rdata = rdata_arg;
978 struct fc_lport *lport = rdata->local_port;
979 struct fc_els_flogi *plp = NULL;
980 u16 csp_seq;
981 u16 cssp_seq;
982 u8 op;
984 FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
986 if (fp == ERR_PTR(-FC_EX_CLOSED))
987 goto put;
989 mutex_lock(&rdata->rp_mutex);
991 if (rdata->rp_state != RPORT_ST_PLOGI) {
992 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
993 "%s\n", fc_rport_state(rdata));
994 if (IS_ERR(fp))
995 goto err;
996 goto out;
999 if (IS_ERR(fp)) {
1000 fc_rport_error_retry(rdata, PTR_ERR(fp));
1001 goto err;
1004 op = fc_frame_payload_op(fp);
1005 if (op == ELS_LS_ACC &&
1006 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
1007 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
1008 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
1010 /* save plogi response sp_features for further reference */
1011 rdata->sp_features = ntohs(plp->fl_csp.sp_features);
1013 if (lport->point_to_multipoint)
1014 fc_rport_login_complete(rdata, fp);
1015 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
1016 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
1017 if (cssp_seq < csp_seq)
1018 csp_seq = cssp_seq;
1019 rdata->max_seq = csp_seq;
1020 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
1021 fc_rport_enter_prli(rdata);
1022 } else {
1023 struct fc_els_ls_rjt *rjt;
1025 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1026 if (!rjt)
1027 FC_RPORT_DBG(rdata, "PLOGI bad response\n");
1028 else
1029 FC_RPORT_DBG(rdata, "PLOGI ELS rejected, reason %x expl %x\n",
1030 rjt->er_reason, rjt->er_explan);
1031 fc_rport_error_retry(rdata, -FC_EX_ELS_RJT);
1033 out:
1034 fc_frame_free(fp);
1035 err:
1036 mutex_unlock(&rdata->rp_mutex);
1037 put:
1038 kref_put(&rdata->kref, fc_rport_destroy);
1041 static bool
1042 fc_rport_compatible_roles(struct fc_lport *lport, struct fc_rport_priv *rdata)
1044 if (rdata->ids.roles == FC_PORT_ROLE_UNKNOWN)
1045 return true;
1046 if ((rdata->ids.roles & FC_PORT_ROLE_FCP_TARGET) &&
1047 (lport->service_params & FCP_SPPF_INIT_FCN))
1048 return true;
1049 if ((rdata->ids.roles & FC_PORT_ROLE_FCP_INITIATOR) &&
1050 (lport->service_params & FCP_SPPF_TARG_FCN))
1051 return true;
1052 return false;
1056 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
1057 * @rdata: The remote port to send a PLOGI to
1059 * Reference counting: increments kref when sending ELS
1061 static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
1063 struct fc_lport *lport = rdata->local_port;
1064 struct fc_frame *fp;
1066 lockdep_assert_held(&rdata->rp_mutex);
1068 if (!fc_rport_compatible_roles(lport, rdata)) {
1069 FC_RPORT_DBG(rdata, "PLOGI suppressed for incompatible role\n");
1070 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
1071 return;
1074 FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
1075 fc_rport_state(rdata));
1077 fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
1079 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
1080 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
1081 if (!fp) {
1082 FC_RPORT_DBG(rdata, "%s frame alloc failed\n", __func__);
1083 fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
1084 return;
1086 rdata->e_d_tov = lport->e_d_tov;
1088 kref_get(&rdata->kref);
1089 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
1090 fc_rport_plogi_resp, rdata,
1091 2 * lport->r_a_tov)) {
1092 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
1093 kref_put(&rdata->kref, fc_rport_destroy);
1098 * fc_rport_prli_resp() - Process Login (PRLI) response handler
1099 * @sp: The sequence the PRLI response was on
1100 * @fp: The PRLI response frame
1101 * @rdata_arg: The remote port that sent the PRLI response
1103 * Locking Note: This function will be called without the rport lock
1104 * held, but it will lock, call an _enter_* function or fc_rport_error
1105 * and then unlock the rport.
1107 static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
1108 void *rdata_arg)
1110 struct fc_rport_priv *rdata = rdata_arg;
1111 struct {
1112 struct fc_els_prli prli;
1113 struct fc_els_spp spp;
1114 } *pp;
1115 struct fc_els_spp temp_spp;
1116 struct fc_els_ls_rjt *rjt;
1117 struct fc4_prov *prov;
1118 u32 roles = FC_RPORT_ROLE_UNKNOWN;
1119 u32 fcp_parm = 0;
1120 u8 op;
1121 enum fc_els_spp_resp resp_code;
1123 FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
1125 if (fp == ERR_PTR(-FC_EX_CLOSED))
1126 goto put;
1128 mutex_lock(&rdata->rp_mutex);
1130 if (rdata->rp_state != RPORT_ST_PRLI) {
1131 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
1132 "%s\n", fc_rport_state(rdata));
1133 if (IS_ERR(fp))
1134 goto err;
1135 goto out;
1138 if (IS_ERR(fp)) {
1139 fc_rport_error_retry(rdata, PTR_ERR(fp));
1140 goto err;
1143 /* reinitialize remote port roles */
1144 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
1146 op = fc_frame_payload_op(fp);
1147 if (op == ELS_LS_ACC) {
1148 pp = fc_frame_payload_get(fp, sizeof(*pp));
1149 if (!pp) {
1150 fc_rport_error_retry(rdata, -FC_EX_SEQ_ERR);
1151 goto out;
1154 resp_code = (pp->spp.spp_flags & FC_SPP_RESP_MASK);
1155 FC_RPORT_DBG(rdata, "PRLI spp_flags = 0x%x spp_type 0x%x\n",
1156 pp->spp.spp_flags, pp->spp.spp_type);
1157 rdata->spp_type = pp->spp.spp_type;
1158 if (resp_code != FC_SPP_RESP_ACK) {
1159 if (resp_code == FC_SPP_RESP_CONF)
1160 fc_rport_error(rdata, -FC_EX_SEQ_ERR);
1161 else
1162 fc_rport_error_retry(rdata, -FC_EX_SEQ_ERR);
1163 goto out;
1165 if (pp->prli.prli_spp_len < sizeof(pp->spp)) {
1166 fc_rport_error_retry(rdata, -FC_EX_SEQ_ERR);
1167 goto out;
1170 fcp_parm = ntohl(pp->spp.spp_params);
1171 if (fcp_parm & FCP_SPPF_RETRY)
1172 rdata->flags |= FC_RP_FLAGS_RETRY;
1173 if (fcp_parm & FCP_SPPF_CONF_COMPL)
1174 rdata->flags |= FC_RP_FLAGS_CONF_REQ;
1177 * Call prli provider if we should act as a target
1179 prov = fc_passive_prov[rdata->spp_type];
1180 if (prov) {
1181 memset(&temp_spp, 0, sizeof(temp_spp));
1182 prov->prli(rdata, pp->prli.prli_spp_len,
1183 &pp->spp, &temp_spp);
1186 * Check if the image pair could be established
1188 if (rdata->spp_type != FC_TYPE_FCP ||
1189 !(pp->spp.spp_flags & FC_SPP_EST_IMG_PAIR)) {
1191 * Nope; we can't use this port as a target.
1193 fcp_parm &= ~FCP_SPPF_TARG_FCN;
1195 rdata->supported_classes = FC_COS_CLASS3;
1196 if (fcp_parm & FCP_SPPF_INIT_FCN)
1197 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1198 if (fcp_parm & FCP_SPPF_TARG_FCN)
1199 roles |= FC_RPORT_ROLE_FCP_TARGET;
1201 rdata->ids.roles = roles;
1202 fc_rport_enter_rtv(rdata);
1204 } else {
1205 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1206 if (!rjt)
1207 FC_RPORT_DBG(rdata, "PRLI bad response\n");
1208 else
1209 FC_RPORT_DBG(rdata, "PRLI ELS rejected, reason %x expl %x\n",
1210 rjt->er_reason, rjt->er_explan);
1211 fc_rport_error_retry(rdata, FC_EX_ELS_RJT);
1214 out:
1215 fc_frame_free(fp);
1216 err:
1217 mutex_unlock(&rdata->rp_mutex);
1218 put:
1219 kref_put(&rdata->kref, fc_rport_destroy);
1223 * fc_rport_enter_prli() - Send Process Login (PRLI) request
1224 * @rdata: The remote port to send the PRLI request to
1226 * Reference counting: increments kref when sending ELS
1228 static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
1230 struct fc_lport *lport = rdata->local_port;
1231 struct {
1232 struct fc_els_prli prli;
1233 struct fc_els_spp spp;
1234 } *pp;
1235 struct fc_frame *fp;
1236 struct fc4_prov *prov;
1238 lockdep_assert_held(&rdata->rp_mutex);
1241 * If the rport is one of the well known addresses
1242 * we skip PRLI and RTV and go straight to READY.
1244 if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
1245 fc_rport_enter_ready(rdata);
1246 return;
1250 * And if the local port does not support the initiator function
1251 * there's no need to send a PRLI, either.
1253 if (!(lport->service_params & FCP_SPPF_INIT_FCN)) {
1254 fc_rport_enter_ready(rdata);
1255 return;
1258 FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
1259 fc_rport_state(rdata));
1261 fc_rport_state_enter(rdata, RPORT_ST_PRLI);
1263 fp = fc_frame_alloc(lport, sizeof(*pp));
1264 if (!fp) {
1265 fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
1266 return;
1269 fc_prli_fill(lport, fp);
1271 prov = fc_passive_prov[FC_TYPE_FCP];
1272 if (prov) {
1273 pp = fc_frame_payload_get(fp, sizeof(*pp));
1274 prov->prli(rdata, sizeof(pp->spp), NULL, &pp->spp);
1277 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rdata->ids.port_id,
1278 fc_host_port_id(lport->host), FC_TYPE_ELS,
1279 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1281 kref_get(&rdata->kref);
1282 if (!fc_exch_seq_send(lport, fp, fc_rport_prli_resp,
1283 NULL, rdata, 2 * lport->r_a_tov)) {
1284 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
1285 kref_put(&rdata->kref, fc_rport_destroy);
1290 * fc_rport_rtv_resp() - Handler for Request Timeout Value (RTV) responses
1291 * @sp: The sequence the RTV was on
1292 * @fp: The RTV response frame
1293 * @rdata_arg: The remote port that sent the RTV response
1295 * Many targets don't seem to support this.
1297 * Locking Note: This function will be called without the rport lock
1298 * held, but it will lock, call an _enter_* function or fc_rport_error
1299 * and then unlock the rport.
1301 static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
1302 void *rdata_arg)
1304 struct fc_rport_priv *rdata = rdata_arg;
1305 u8 op;
1307 FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
1309 if (fp == ERR_PTR(-FC_EX_CLOSED))
1310 goto put;
1312 mutex_lock(&rdata->rp_mutex);
1314 if (rdata->rp_state != RPORT_ST_RTV) {
1315 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
1316 "%s\n", fc_rport_state(rdata));
1317 if (IS_ERR(fp))
1318 goto err;
1319 goto out;
1322 if (IS_ERR(fp)) {
1323 fc_rport_error(rdata, PTR_ERR(fp));
1324 goto err;
1327 op = fc_frame_payload_op(fp);
1328 if (op == ELS_LS_ACC) {
1329 struct fc_els_rtv_acc *rtv;
1330 u32 toq;
1331 u32 tov;
1333 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
1334 if (rtv) {
1335 toq = ntohl(rtv->rtv_toq);
1336 tov = ntohl(rtv->rtv_r_a_tov);
1337 if (tov == 0)
1338 tov = 1;
1339 if (tov > rdata->r_a_tov)
1340 rdata->r_a_tov = tov;
1341 tov = ntohl(rtv->rtv_e_d_tov);
1342 if (toq & FC_ELS_RTV_EDRES)
1343 tov /= 1000000;
1344 if (tov == 0)
1345 tov = 1;
1346 if (tov > rdata->e_d_tov)
1347 rdata->e_d_tov = tov;
1351 fc_rport_enter_ready(rdata);
1353 out:
1354 fc_frame_free(fp);
1355 err:
1356 mutex_unlock(&rdata->rp_mutex);
1357 put:
1358 kref_put(&rdata->kref, fc_rport_destroy);
1362 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
1363 * @rdata: The remote port to send the RTV request to
1365 * Reference counting: increments kref when sending ELS
1367 static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
1369 struct fc_frame *fp;
1370 struct fc_lport *lport = rdata->local_port;
1372 lockdep_assert_held(&rdata->rp_mutex);
1374 FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
1375 fc_rport_state(rdata));
1377 fc_rport_state_enter(rdata, RPORT_ST_RTV);
1379 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
1380 if (!fp) {
1381 fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
1382 return;
1385 kref_get(&rdata->kref);
1386 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
1387 fc_rport_rtv_resp, rdata,
1388 2 * lport->r_a_tov)) {
1389 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
1390 kref_put(&rdata->kref, fc_rport_destroy);
1395 * fc_rport_recv_rtv_req() - Handler for Read Timeout Value (RTV) requests
1396 * @rdata: The remote port that sent the RTV request
1397 * @in_fp: The RTV request frame
1399 static void fc_rport_recv_rtv_req(struct fc_rport_priv *rdata,
1400 struct fc_frame *in_fp)
1402 struct fc_lport *lport = rdata->local_port;
1403 struct fc_frame *fp;
1404 struct fc_els_rtv_acc *rtv;
1405 struct fc_seq_els_data rjt_data;
1407 lockdep_assert_held(&rdata->rp_mutex);
1408 lockdep_assert_held(&lport->lp_mutex);
1410 FC_RPORT_DBG(rdata, "Received RTV request\n");
1412 fp = fc_frame_alloc(lport, sizeof(*rtv));
1413 if (!fp) {
1414 rjt_data.reason = ELS_RJT_UNAB;
1415 rjt_data.explan = ELS_EXPL_INSUF_RES;
1416 fc_seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data);
1417 goto drop;
1419 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
1420 rtv->rtv_cmd = ELS_LS_ACC;
1421 rtv->rtv_r_a_tov = htonl(lport->r_a_tov);
1422 rtv->rtv_e_d_tov = htonl(lport->e_d_tov);
1423 rtv->rtv_toq = 0;
1424 fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
1425 lport->tt.frame_send(lport, fp);
1426 drop:
1427 fc_frame_free(in_fp);
1431 * fc_rport_logo_resp() - Handler for logout (LOGO) responses
1432 * @sp: The sequence the LOGO was on
1433 * @fp: The LOGO response frame
1434 * @lport_arg: The local port
1436 static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
1437 void *rdata_arg)
1439 struct fc_rport_priv *rdata = rdata_arg;
1440 struct fc_lport *lport = rdata->local_port;
1442 FC_RPORT_ID_DBG(lport, fc_seq_exch(sp)->did,
1443 "Received a LOGO %s\n", fc_els_resp_type(fp));
1444 if (!IS_ERR(fp))
1445 fc_frame_free(fp);
1446 kref_put(&rdata->kref, fc_rport_destroy);
1450 * fc_rport_enter_logo() - Send a logout (LOGO) request
1451 * @rdata: The remote port to send the LOGO request to
1453 * Reference counting: increments kref when sending ELS
1455 static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
1457 struct fc_lport *lport = rdata->local_port;
1458 struct fc_frame *fp;
1460 lockdep_assert_held(&rdata->rp_mutex);
1462 FC_RPORT_DBG(rdata, "Port sending LOGO from %s state\n",
1463 fc_rport_state(rdata));
1465 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
1466 if (!fp)
1467 return;
1468 kref_get(&rdata->kref);
1469 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
1470 fc_rport_logo_resp, rdata, 0))
1471 kref_put(&rdata->kref, fc_rport_destroy);
1475 * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
1476 * @sp: The sequence the ADISC response was on
1477 * @fp: The ADISC response frame
1478 * @rdata_arg: The remote port that sent the ADISC response
1480 * Locking Note: This function will be called without the rport lock
1481 * held, but it will lock, call an _enter_* function or fc_rport_error
1482 * and then unlock the rport.
1484 static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
1485 void *rdata_arg)
1487 struct fc_rport_priv *rdata = rdata_arg;
1488 struct fc_els_adisc *adisc;
1489 u8 op;
1491 FC_RPORT_DBG(rdata, "Received a ADISC response\n");
1493 if (fp == ERR_PTR(-FC_EX_CLOSED))
1494 goto put;
1496 mutex_lock(&rdata->rp_mutex);
1498 if (rdata->rp_state != RPORT_ST_ADISC) {
1499 FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
1500 fc_rport_state(rdata));
1501 if (IS_ERR(fp))
1502 goto err;
1503 goto out;
1506 if (IS_ERR(fp)) {
1507 fc_rport_error(rdata, PTR_ERR(fp));
1508 goto err;
1512 * If address verification failed. Consider us logged out of the rport.
1513 * Since the rport is still in discovery, we want to be
1514 * logged in, so go to PLOGI state. Otherwise, go back to READY.
1516 op = fc_frame_payload_op(fp);
1517 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1518 if (op != ELS_LS_ACC || !adisc ||
1519 ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
1520 get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
1521 get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
1522 FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
1523 fc_rport_enter_flogi(rdata);
1524 } else {
1525 FC_RPORT_DBG(rdata, "ADISC OK\n");
1526 fc_rport_enter_ready(rdata);
1528 out:
1529 fc_frame_free(fp);
1530 err:
1531 mutex_unlock(&rdata->rp_mutex);
1532 put:
1533 kref_put(&rdata->kref, fc_rport_destroy);
1537 * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
1538 * @rdata: The remote port to send the ADISC request to
1540 * Reference counting: increments kref when sending ELS
1542 static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
1544 struct fc_lport *lport = rdata->local_port;
1545 struct fc_frame *fp;
1547 lockdep_assert_held(&rdata->rp_mutex);
1549 FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
1550 fc_rport_state(rdata));
1552 fc_rport_state_enter(rdata, RPORT_ST_ADISC);
1554 fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
1555 if (!fp) {
1556 fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
1557 return;
1559 kref_get(&rdata->kref);
1560 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
1561 fc_rport_adisc_resp, rdata,
1562 2 * lport->r_a_tov)) {
1563 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
1564 kref_put(&rdata->kref, fc_rport_destroy);
1569 * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
1570 * @rdata: The remote port that sent the ADISC request
1571 * @in_fp: The ADISC request frame
1573 static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
1574 struct fc_frame *in_fp)
1576 struct fc_lport *lport = rdata->local_port;
1577 struct fc_frame *fp;
1578 struct fc_els_adisc *adisc;
1579 struct fc_seq_els_data rjt_data;
1581 lockdep_assert_held(&rdata->rp_mutex);
1582 lockdep_assert_held(&lport->lp_mutex);
1584 FC_RPORT_DBG(rdata, "Received ADISC request\n");
1586 adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
1587 if (!adisc) {
1588 rjt_data.reason = ELS_RJT_PROT;
1589 rjt_data.explan = ELS_EXPL_INV_LEN;
1590 fc_seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data);
1591 goto drop;
1594 fp = fc_frame_alloc(lport, sizeof(*adisc));
1595 if (!fp)
1596 goto drop;
1597 fc_adisc_fill(lport, fp);
1598 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1599 adisc->adisc_cmd = ELS_LS_ACC;
1600 fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
1601 lport->tt.frame_send(lport, fp);
1602 drop:
1603 fc_frame_free(in_fp);
1607 * fc_rport_recv_rls_req() - Handle received Read Link Status request
1608 * @rdata: The remote port that sent the RLS request
1609 * @rx_fp: The PRLI request frame
1611 static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
1612 struct fc_frame *rx_fp)
1615 struct fc_lport *lport = rdata->local_port;
1616 struct fc_frame *fp;
1617 struct fc_els_rls *rls;
1618 struct fc_els_rls_resp *rsp;
1619 struct fc_els_lesb *lesb;
1620 struct fc_seq_els_data rjt_data;
1621 struct fc_host_statistics *hst;
1623 lockdep_assert_held(&rdata->rp_mutex);
1625 FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
1626 fc_rport_state(rdata));
1628 rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
1629 if (!rls) {
1630 rjt_data.reason = ELS_RJT_PROT;
1631 rjt_data.explan = ELS_EXPL_INV_LEN;
1632 goto out_rjt;
1635 fp = fc_frame_alloc(lport, sizeof(*rsp));
1636 if (!fp) {
1637 rjt_data.reason = ELS_RJT_UNAB;
1638 rjt_data.explan = ELS_EXPL_INSUF_RES;
1639 goto out_rjt;
1642 rsp = fc_frame_payload_get(fp, sizeof(*rsp));
1643 memset(rsp, 0, sizeof(*rsp));
1644 rsp->rls_cmd = ELS_LS_ACC;
1645 lesb = &rsp->rls_lesb;
1646 if (lport->tt.get_lesb) {
1647 /* get LESB from LLD if it supports it */
1648 lport->tt.get_lesb(lport, lesb);
1649 } else {
1650 fc_get_host_stats(lport->host);
1651 hst = &lport->host_stats;
1652 lesb->lesb_link_fail = htonl(hst->link_failure_count);
1653 lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
1654 lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
1655 lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
1656 lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
1657 lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
1660 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1661 lport->tt.frame_send(lport, fp);
1662 goto out;
1664 out_rjt:
1665 fc_seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
1666 out:
1667 fc_frame_free(rx_fp);
1671 * fc_rport_recv_els_req() - Handler for validated ELS requests
1672 * @lport: The local port that received the ELS request
1673 * @fp: The ELS request frame
1675 * Handle incoming ELS requests that require port login.
1676 * The ELS opcode has already been validated by the caller.
1678 * Reference counting: does not modify kref
1680 static void fc_rport_recv_els_req(struct fc_lport *lport, struct fc_frame *fp)
1682 struct fc_rport_priv *rdata;
1683 struct fc_seq_els_data els_data;
1685 lockdep_assert_held(&lport->lp_mutex);
1687 rdata = fc_rport_lookup(lport, fc_frame_sid(fp));
1688 if (!rdata) {
1689 FC_RPORT_ID_DBG(lport, fc_frame_sid(fp),
1690 "Received ELS 0x%02x from non-logged-in port\n",
1691 fc_frame_payload_op(fp));
1692 goto reject;
1695 mutex_lock(&rdata->rp_mutex);
1697 switch (rdata->rp_state) {
1698 case RPORT_ST_PRLI:
1699 case RPORT_ST_RTV:
1700 case RPORT_ST_READY:
1701 case RPORT_ST_ADISC:
1702 break;
1703 case RPORT_ST_PLOGI:
1704 if (fc_frame_payload_op(fp) == ELS_PRLI) {
1705 FC_RPORT_DBG(rdata, "Reject ELS PRLI "
1706 "while in state %s\n",
1707 fc_rport_state(rdata));
1708 mutex_unlock(&rdata->rp_mutex);
1709 kref_put(&rdata->kref, fc_rport_destroy);
1710 goto busy;
1712 /* fall through */
1713 default:
1714 FC_RPORT_DBG(rdata,
1715 "Reject ELS 0x%02x while in state %s\n",
1716 fc_frame_payload_op(fp), fc_rport_state(rdata));
1717 mutex_unlock(&rdata->rp_mutex);
1718 kref_put(&rdata->kref, fc_rport_destroy);
1719 goto reject;
1722 switch (fc_frame_payload_op(fp)) {
1723 case ELS_PRLI:
1724 fc_rport_recv_prli_req(rdata, fp);
1725 break;
1726 case ELS_PRLO:
1727 fc_rport_recv_prlo_req(rdata, fp);
1728 break;
1729 case ELS_ADISC:
1730 fc_rport_recv_adisc_req(rdata, fp);
1731 break;
1732 case ELS_RRQ:
1733 fc_seq_els_rsp_send(fp, ELS_RRQ, NULL);
1734 fc_frame_free(fp);
1735 break;
1736 case ELS_REC:
1737 fc_seq_els_rsp_send(fp, ELS_REC, NULL);
1738 fc_frame_free(fp);
1739 break;
1740 case ELS_RLS:
1741 fc_rport_recv_rls_req(rdata, fp);
1742 break;
1743 case ELS_RTV:
1744 fc_rport_recv_rtv_req(rdata, fp);
1745 break;
1746 default:
1747 fc_frame_free(fp); /* can't happen */
1748 break;
1751 mutex_unlock(&rdata->rp_mutex);
1752 kref_put(&rdata->kref, fc_rport_destroy);
1753 return;
1755 reject:
1756 els_data.reason = ELS_RJT_UNAB;
1757 els_data.explan = ELS_EXPL_PLOGI_REQD;
1758 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1759 fc_frame_free(fp);
1760 return;
1762 busy:
1763 els_data.reason = ELS_RJT_BUSY;
1764 els_data.explan = ELS_EXPL_NONE;
1765 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1766 fc_frame_free(fp);
1767 return;
1771 * fc_rport_recv_req() - Handler for requests
1772 * @lport: The local port that received the request
1773 * @fp: The request frame
1775 * Reference counting: does not modify kref
1777 void fc_rport_recv_req(struct fc_lport *lport, struct fc_frame *fp)
1779 struct fc_seq_els_data els_data;
1781 lockdep_assert_held(&lport->lp_mutex);
1784 * Handle FLOGI, PLOGI and LOGO requests separately, since they
1785 * don't require prior login.
1786 * Check for unsupported opcodes first and reject them.
1787 * For some ops, it would be incorrect to reject with "PLOGI required".
1789 switch (fc_frame_payload_op(fp)) {
1790 case ELS_FLOGI:
1791 fc_rport_recv_flogi_req(lport, fp);
1792 break;
1793 case ELS_PLOGI:
1794 fc_rport_recv_plogi_req(lport, fp);
1795 break;
1796 case ELS_LOGO:
1797 fc_rport_recv_logo_req(lport, fp);
1798 break;
1799 case ELS_PRLI:
1800 case ELS_PRLO:
1801 case ELS_ADISC:
1802 case ELS_RRQ:
1803 case ELS_REC:
1804 case ELS_RLS:
1805 case ELS_RTV:
1806 fc_rport_recv_els_req(lport, fp);
1807 break;
1808 default:
1809 els_data.reason = ELS_RJT_UNSUP;
1810 els_data.explan = ELS_EXPL_NONE;
1811 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1812 fc_frame_free(fp);
1813 break;
1816 EXPORT_SYMBOL(fc_rport_recv_req);
1819 * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
1820 * @lport: The local port that received the PLOGI request
1821 * @rx_fp: The PLOGI request frame
1823 * Reference counting: increments kref on return
1825 static void fc_rport_recv_plogi_req(struct fc_lport *lport,
1826 struct fc_frame *rx_fp)
1828 struct fc_disc *disc;
1829 struct fc_rport_priv *rdata;
1830 struct fc_frame *fp = rx_fp;
1831 struct fc_els_flogi *pl;
1832 struct fc_seq_els_data rjt_data;
1833 u32 sid;
1835 lockdep_assert_held(&lport->lp_mutex);
1837 sid = fc_frame_sid(fp);
1839 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
1841 pl = fc_frame_payload_get(fp, sizeof(*pl));
1842 if (!pl) {
1843 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
1844 rjt_data.reason = ELS_RJT_PROT;
1845 rjt_data.explan = ELS_EXPL_INV_LEN;
1846 goto reject;
1849 disc = &lport->disc;
1850 mutex_lock(&disc->disc_mutex);
1851 rdata = fc_rport_create(lport, sid);
1852 if (!rdata) {
1853 mutex_unlock(&disc->disc_mutex);
1854 rjt_data.reason = ELS_RJT_UNAB;
1855 rjt_data.explan = ELS_EXPL_INSUF_RES;
1856 goto reject;
1859 mutex_lock(&rdata->rp_mutex);
1860 mutex_unlock(&disc->disc_mutex);
1862 rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1863 rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
1866 * If the rport was just created, possibly due to the incoming PLOGI,
1867 * set the state appropriately and accept the PLOGI.
1869 * If we had also sent a PLOGI, and if the received PLOGI is from a
1870 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1871 * "command already in progress".
1873 * XXX TBD: If the session was ready before, the PLOGI should result in
1874 * all outstanding exchanges being reset.
1876 switch (rdata->rp_state) {
1877 case RPORT_ST_INIT:
1878 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
1879 break;
1880 case RPORT_ST_PLOGI_WAIT:
1881 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI_WAIT state\n");
1882 break;
1883 case RPORT_ST_PLOGI:
1884 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1885 if (rdata->ids.port_name < lport->wwpn) {
1886 mutex_unlock(&rdata->rp_mutex);
1887 rjt_data.reason = ELS_RJT_INPROG;
1888 rjt_data.explan = ELS_EXPL_NONE;
1889 goto reject;
1891 break;
1892 case RPORT_ST_PRLI:
1893 case RPORT_ST_RTV:
1894 case RPORT_ST_READY:
1895 case RPORT_ST_ADISC:
1896 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
1897 "- ignored for now\n", rdata->rp_state);
1898 /* XXX TBD - should reset */
1899 break;
1900 case RPORT_ST_FLOGI:
1901 case RPORT_ST_DELETE:
1902 FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
1903 fc_rport_state(rdata));
1904 mutex_unlock(&rdata->rp_mutex);
1905 rjt_data.reason = ELS_RJT_BUSY;
1906 rjt_data.explan = ELS_EXPL_NONE;
1907 goto reject;
1909 if (!fc_rport_compatible_roles(lport, rdata)) {
1910 FC_RPORT_DBG(rdata, "Received PLOGI for incompatible role\n");
1911 mutex_unlock(&rdata->rp_mutex);
1912 rjt_data.reason = ELS_RJT_LOGIC;
1913 rjt_data.explan = ELS_EXPL_NONE;
1914 goto reject;
1918 * Get session payload size from incoming PLOGI.
1920 rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
1923 * Send LS_ACC. If this fails, the originator should retry.
1925 fp = fc_frame_alloc(lport, sizeof(*pl));
1926 if (!fp)
1927 goto out;
1929 fc_plogi_fill(lport, fp, ELS_LS_ACC);
1930 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1931 lport->tt.frame_send(lport, fp);
1932 fc_rport_enter_prli(rdata);
1933 out:
1934 mutex_unlock(&rdata->rp_mutex);
1935 fc_frame_free(rx_fp);
1936 return;
1938 reject:
1939 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
1940 fc_frame_free(fp);
1944 * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
1945 * @rdata: The remote port that sent the PRLI request
1946 * @rx_fp: The PRLI request frame
1948 static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
1949 struct fc_frame *rx_fp)
1951 struct fc_lport *lport = rdata->local_port;
1952 struct fc_frame *fp;
1953 struct {
1954 struct fc_els_prli prli;
1955 struct fc_els_spp spp;
1956 } *pp;
1957 struct fc_els_spp *rspp; /* request service param page */
1958 struct fc_els_spp *spp; /* response spp */
1959 unsigned int len;
1960 unsigned int plen;
1961 enum fc_els_spp_resp resp;
1962 struct fc_seq_els_data rjt_data;
1963 struct fc4_prov *prov;
1965 lockdep_assert_held(&rdata->rp_mutex);
1967 FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1968 fc_rport_state(rdata));
1970 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
1971 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1972 if (!pp)
1973 goto reject_len;
1974 plen = ntohs(pp->prli.prli_len);
1975 if ((plen % 4) != 0 || plen > len || plen < 16)
1976 goto reject_len;
1977 if (plen < len)
1978 len = plen;
1979 plen = pp->prli.prli_spp_len;
1980 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1981 plen > len || len < sizeof(*pp) || plen < 12)
1982 goto reject_len;
1983 rspp = &pp->spp;
1985 fp = fc_frame_alloc(lport, len);
1986 if (!fp) {
1987 rjt_data.reason = ELS_RJT_UNAB;
1988 rjt_data.explan = ELS_EXPL_INSUF_RES;
1989 goto reject;
1991 pp = fc_frame_payload_get(fp, len);
1992 WARN_ON(!pp);
1993 memset(pp, 0, len);
1994 pp->prli.prli_cmd = ELS_LS_ACC;
1995 pp->prli.prli_spp_len = plen;
1996 pp->prli.prli_len = htons(len);
1997 len -= sizeof(struct fc_els_prli);
2000 * Go through all the service parameter pages and build
2001 * response. If plen indicates longer SPP than standard,
2002 * use that. The entire response has been pre-cleared above.
2004 spp = &pp->spp;
2005 mutex_lock(&fc_prov_mutex);
2006 while (len >= plen) {
2007 rdata->spp_type = rspp->spp_type;
2008 spp->spp_type = rspp->spp_type;
2009 spp->spp_type_ext = rspp->spp_type_ext;
2010 resp = 0;
2012 if (rspp->spp_type < FC_FC4_PROV_SIZE) {
2013 enum fc_els_spp_resp active = 0, passive = 0;
2015 prov = fc_active_prov[rspp->spp_type];
2016 if (prov)
2017 active = prov->prli(rdata, plen, rspp, spp);
2018 prov = fc_passive_prov[rspp->spp_type];
2019 if (prov)
2020 passive = prov->prli(rdata, plen, rspp, spp);
2021 if (!active || passive == FC_SPP_RESP_ACK)
2022 resp = passive;
2023 else
2024 resp = active;
2025 FC_RPORT_DBG(rdata, "PRLI rspp type %x "
2026 "active %x passive %x\n",
2027 rspp->spp_type, active, passive);
2029 if (!resp) {
2030 if (spp->spp_flags & FC_SPP_EST_IMG_PAIR)
2031 resp |= FC_SPP_RESP_CONF;
2032 else
2033 resp |= FC_SPP_RESP_INVL;
2035 spp->spp_flags |= resp;
2036 len -= plen;
2037 rspp = (struct fc_els_spp *)((char *)rspp + plen);
2038 spp = (struct fc_els_spp *)((char *)spp + plen);
2040 mutex_unlock(&fc_prov_mutex);
2043 * Send LS_ACC. If this fails, the originator should retry.
2045 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
2046 lport->tt.frame_send(lport, fp);
2048 goto drop;
2050 reject_len:
2051 rjt_data.reason = ELS_RJT_PROT;
2052 rjt_data.explan = ELS_EXPL_INV_LEN;
2053 reject:
2054 fc_seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
2055 drop:
2056 fc_frame_free(rx_fp);
2060 * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
2061 * @rdata: The remote port that sent the PRLO request
2062 * @rx_fp: The PRLO request frame
2064 static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
2065 struct fc_frame *rx_fp)
2067 struct fc_lport *lport = rdata->local_port;
2068 struct fc_frame *fp;
2069 struct {
2070 struct fc_els_prlo prlo;
2071 struct fc_els_spp spp;
2072 } *pp;
2073 struct fc_els_spp *rspp; /* request service param page */
2074 struct fc_els_spp *spp; /* response spp */
2075 unsigned int len;
2076 unsigned int plen;
2077 struct fc_seq_els_data rjt_data;
2079 lockdep_assert_held(&rdata->rp_mutex);
2081 FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
2082 fc_rport_state(rdata));
2084 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
2085 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
2086 if (!pp)
2087 goto reject_len;
2088 plen = ntohs(pp->prlo.prlo_len);
2089 if (plen != 20)
2090 goto reject_len;
2091 if (plen < len)
2092 len = plen;
2094 rspp = &pp->spp;
2096 fp = fc_frame_alloc(lport, len);
2097 if (!fp) {
2098 rjt_data.reason = ELS_RJT_UNAB;
2099 rjt_data.explan = ELS_EXPL_INSUF_RES;
2100 goto reject;
2103 pp = fc_frame_payload_get(fp, len);
2104 WARN_ON(!pp);
2105 memset(pp, 0, len);
2106 pp->prlo.prlo_cmd = ELS_LS_ACC;
2107 pp->prlo.prlo_obs = 0x10;
2108 pp->prlo.prlo_len = htons(len);
2109 spp = &pp->spp;
2110 spp->spp_type = rspp->spp_type;
2111 spp->spp_type_ext = rspp->spp_type_ext;
2112 spp->spp_flags = FC_SPP_RESP_ACK;
2114 fc_rport_enter_prli(rdata);
2116 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
2117 lport->tt.frame_send(lport, fp);
2118 goto drop;
2120 reject_len:
2121 rjt_data.reason = ELS_RJT_PROT;
2122 rjt_data.explan = ELS_EXPL_INV_LEN;
2123 reject:
2124 fc_seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
2125 drop:
2126 fc_frame_free(rx_fp);
2130 * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
2131 * @lport: The local port that received the LOGO request
2132 * @fp: The LOGO request frame
2134 * Reference counting: drops kref on return
2136 static void fc_rport_recv_logo_req(struct fc_lport *lport, struct fc_frame *fp)
2138 struct fc_rport_priv *rdata;
2139 u32 sid;
2141 lockdep_assert_held(&lport->lp_mutex);
2143 fc_seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
2145 sid = fc_frame_sid(fp);
2147 rdata = fc_rport_lookup(lport, sid);
2148 if (rdata) {
2149 mutex_lock(&rdata->rp_mutex);
2150 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
2151 fc_rport_state(rdata));
2153 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
2154 mutex_unlock(&rdata->rp_mutex);
2155 kref_put(&rdata->kref, fc_rport_destroy);
2156 } else
2157 FC_RPORT_ID_DBG(lport, sid,
2158 "Received LOGO from non-logged-in port\n");
2159 fc_frame_free(fp);
2163 * fc_rport_flush_queue() - Flush the rport_event_queue
2165 void fc_rport_flush_queue(void)
2167 flush_workqueue(rport_event_queue);
2169 EXPORT_SYMBOL(fc_rport_flush_queue);
2172 * fc_rport_fcp_prli() - Handle incoming PRLI for the FCP initiator.
2173 * @rdata: remote port private
2174 * @spp_len: service parameter page length
2175 * @rspp: received service parameter page
2176 * @spp: response service parameter page
2178 * Returns the value for the response code to be placed in spp_flags;
2179 * Returns 0 if not an initiator.
2181 static int fc_rport_fcp_prli(struct fc_rport_priv *rdata, u32 spp_len,
2182 const struct fc_els_spp *rspp,
2183 struct fc_els_spp *spp)
2185 struct fc_lport *lport = rdata->local_port;
2186 u32 fcp_parm;
2188 fcp_parm = ntohl(rspp->spp_params);
2189 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
2190 if (fcp_parm & FCP_SPPF_INIT_FCN)
2191 rdata->ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2192 if (fcp_parm & FCP_SPPF_TARG_FCN)
2193 rdata->ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2194 if (fcp_parm & FCP_SPPF_RETRY)
2195 rdata->flags |= FC_RP_FLAGS_RETRY;
2196 rdata->supported_classes = FC_COS_CLASS3;
2198 if (!(lport->service_params & FCP_SPPF_INIT_FCN))
2199 return 0;
2201 spp->spp_flags |= rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
2204 * OR in our service parameters with other providers (target), if any.
2206 fcp_parm = ntohl(spp->spp_params);
2207 spp->spp_params = htonl(fcp_parm | lport->service_params);
2208 return FC_SPP_RESP_ACK;
2212 * FC-4 provider ops for FCP initiator.
2214 struct fc4_prov fc_rport_fcp_init = {
2215 .prli = fc_rport_fcp_prli,
2219 * fc_rport_t0_prli() - Handle incoming PRLI parameters for type 0
2220 * @rdata: remote port private
2221 * @spp_len: service parameter page length
2222 * @rspp: received service parameter page
2223 * @spp: response service parameter page
2225 static int fc_rport_t0_prli(struct fc_rport_priv *rdata, u32 spp_len,
2226 const struct fc_els_spp *rspp,
2227 struct fc_els_spp *spp)
2229 if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR)
2230 return FC_SPP_RESP_INVL;
2231 return FC_SPP_RESP_ACK;
2235 * FC-4 provider ops for type 0 service parameters.
2237 * This handles the special case of type 0 which is always successful
2238 * but doesn't do anything otherwise.
2240 struct fc4_prov fc_rport_t0_prov = {
2241 .prli = fc_rport_t0_prli,
2245 * fc_setup_rport() - Initialize the rport_event_queue
2247 int fc_setup_rport(void)
2249 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
2250 if (!rport_event_queue)
2251 return -ENOMEM;
2252 return 0;
2256 * fc_destroy_rport() - Destroy the rport_event_queue
2258 void fc_destroy_rport(void)
2260 destroy_workqueue(rport_event_queue);
2264 * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
2265 * @rport: The remote port whose I/O should be terminated
2267 void fc_rport_terminate_io(struct fc_rport *rport)
2269 struct fc_rport_libfc_priv *rpriv = rport->dd_data;
2270 struct fc_lport *lport = rpriv->local_port;
2272 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
2273 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
2275 EXPORT_SYMBOL(fc_rport_terminate_io);