gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / scsi / libfc / fc_rport.c
blob773c45af9387096dcfbd6a1878d9ba8d88b7bf40
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;
131 size_t rport_priv_size = sizeof(*rdata);
133 lockdep_assert_held(&lport->disc.disc_mutex);
135 rdata = fc_rport_lookup(lport, port_id);
136 if (rdata)
137 return rdata;
139 if (lport->rport_priv_size > 0)
140 rport_priv_size = lport->rport_priv_size;
141 rdata = kzalloc(rport_priv_size, GFP_KERNEL);
142 if (!rdata)
143 return NULL;
145 rdata->ids.node_name = -1;
146 rdata->ids.port_name = -1;
147 rdata->ids.port_id = port_id;
148 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
150 kref_init(&rdata->kref);
151 mutex_init(&rdata->rp_mutex);
152 rdata->local_port = lport;
153 rdata->rp_state = RPORT_ST_INIT;
154 rdata->event = RPORT_EV_NONE;
155 rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
156 rdata->e_d_tov = lport->e_d_tov;
157 rdata->r_a_tov = lport->r_a_tov;
158 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
159 INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
160 INIT_WORK(&rdata->event_work, fc_rport_work);
161 if (port_id != FC_FID_DIR_SERV) {
162 rdata->lld_event_callback = lport->tt.rport_event_callback;
163 list_add_rcu(&rdata->peers, &lport->disc.rports);
165 return rdata;
167 EXPORT_SYMBOL(fc_rport_create);
170 * fc_rport_destroy() - Free a remote port after last reference is released
171 * @kref: The remote port's kref
173 void fc_rport_destroy(struct kref *kref)
175 struct fc_rport_priv *rdata;
177 rdata = container_of(kref, struct fc_rport_priv, kref);
178 kfree_rcu(rdata, rcu);
180 EXPORT_SYMBOL(fc_rport_destroy);
183 * fc_rport_state() - Return a string identifying the remote port's state
184 * @rdata: The remote port
186 static const char *fc_rport_state(struct fc_rport_priv *rdata)
188 const char *cp;
190 cp = fc_rport_state_names[rdata->rp_state];
191 if (!cp)
192 cp = "Unknown";
193 return cp;
197 * fc_set_rport_loss_tmo() - Set the remote port loss timeout
198 * @rport: The remote port that gets a new timeout value
199 * @timeout: The new timeout value (in seconds)
201 void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
203 if (timeout)
204 rport->dev_loss_tmo = timeout;
205 else
206 rport->dev_loss_tmo = 1;
208 EXPORT_SYMBOL(fc_set_rport_loss_tmo);
211 * fc_plogi_get_maxframe() - Get the maximum payload from the common service
212 * parameters in a FLOGI frame
213 * @flp: The FLOGI or PLOGI payload
214 * @maxval: The maximum frame size upper limit; this may be less than what
215 * is in the service parameters
217 static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
218 unsigned int maxval)
220 unsigned int mfs;
223 * Get max payload from the common service parameters and the
224 * class 3 receive data field size.
226 mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
227 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
228 maxval = mfs;
229 mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
230 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
231 maxval = mfs;
232 return maxval;
236 * fc_rport_state_enter() - Change the state of a remote port
237 * @rdata: The remote port whose state should change
238 * @new: The new state
240 static void fc_rport_state_enter(struct fc_rport_priv *rdata,
241 enum fc_rport_state new)
243 lockdep_assert_held(&rdata->rp_mutex);
245 if (rdata->rp_state != new)
246 rdata->retries = 0;
247 rdata->rp_state = new;
251 * fc_rport_work() - Handler for remote port events in the rport_event_queue
252 * @work: Handle to the remote port being dequeued
254 * Reference counting: drops kref on return
256 static void fc_rport_work(struct work_struct *work)
258 u32 port_id;
259 struct fc_rport_priv *rdata =
260 container_of(work, struct fc_rport_priv, event_work);
261 struct fc_rport_libfc_priv *rpriv;
262 enum fc_rport_event event;
263 struct fc_lport *lport = rdata->local_port;
264 struct fc_rport_operations *rport_ops;
265 struct fc_rport_identifiers ids;
266 struct fc_rport *rport;
267 struct fc4_prov *prov;
268 u8 type;
270 mutex_lock(&rdata->rp_mutex);
271 event = rdata->event;
272 rport_ops = rdata->ops;
273 rport = rdata->rport;
275 FC_RPORT_DBG(rdata, "work event %u\n", event);
277 switch (event) {
278 case RPORT_EV_READY:
279 ids = rdata->ids;
280 rdata->event = RPORT_EV_NONE;
281 rdata->major_retries = 0;
282 kref_get(&rdata->kref);
283 mutex_unlock(&rdata->rp_mutex);
285 if (!rport) {
286 FC_RPORT_DBG(rdata, "No rport!\n");
287 rport = fc_remote_port_add(lport->host, 0, &ids);
289 if (!rport) {
290 FC_RPORT_DBG(rdata, "Failed to add the rport\n");
291 fc_rport_logoff(rdata);
292 kref_put(&rdata->kref, fc_rport_destroy);
293 return;
295 mutex_lock(&rdata->rp_mutex);
296 if (rdata->rport)
297 FC_RPORT_DBG(rdata, "rport already allocated\n");
298 rdata->rport = rport;
299 rport->maxframe_size = rdata->maxframe_size;
300 rport->supported_classes = rdata->supported_classes;
302 rpriv = rport->dd_data;
303 rpriv->local_port = lport;
304 rpriv->rp_state = rdata->rp_state;
305 rpriv->flags = rdata->flags;
306 rpriv->e_d_tov = rdata->e_d_tov;
307 rpriv->r_a_tov = rdata->r_a_tov;
308 mutex_unlock(&rdata->rp_mutex);
310 if (rport_ops && rport_ops->event_callback) {
311 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
312 rport_ops->event_callback(lport, rdata, event);
314 if (rdata->lld_event_callback) {
315 FC_RPORT_DBG(rdata, "lld callback ev %d\n", event);
316 rdata->lld_event_callback(lport, rdata, event);
318 kref_put(&rdata->kref, fc_rport_destroy);
319 break;
321 case RPORT_EV_FAILED:
322 case RPORT_EV_LOGO:
323 case RPORT_EV_STOP:
324 if (rdata->prli_count) {
325 mutex_lock(&fc_prov_mutex);
326 for (type = 1; type < FC_FC4_PROV_SIZE; type++) {
327 prov = fc_passive_prov[type];
328 if (prov && prov->prlo)
329 prov->prlo(rdata);
331 mutex_unlock(&fc_prov_mutex);
333 port_id = rdata->ids.port_id;
334 mutex_unlock(&rdata->rp_mutex);
336 if (rport_ops && rport_ops->event_callback) {
337 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
338 rport_ops->event_callback(lport, rdata, event);
340 if (rdata->lld_event_callback) {
341 FC_RPORT_DBG(rdata, "lld callback ev %d\n", event);
342 rdata->lld_event_callback(lport, rdata, event);
344 if (cancel_delayed_work_sync(&rdata->retry_work))
345 kref_put(&rdata->kref, fc_rport_destroy);
348 * Reset any outstanding exchanges before freeing rport.
350 lport->tt.exch_mgr_reset(lport, 0, port_id);
351 lport->tt.exch_mgr_reset(lport, port_id, 0);
353 if (rport) {
354 rpriv = rport->dd_data;
355 rpriv->rp_state = RPORT_ST_DELETE;
356 mutex_lock(&rdata->rp_mutex);
357 rdata->rport = NULL;
358 mutex_unlock(&rdata->rp_mutex);
359 fc_remote_port_delete(rport);
362 mutex_lock(&rdata->rp_mutex);
363 if (rdata->rp_state == RPORT_ST_DELETE) {
364 if (port_id == FC_FID_DIR_SERV) {
365 rdata->event = RPORT_EV_NONE;
366 mutex_unlock(&rdata->rp_mutex);
367 kref_put(&rdata->kref, fc_rport_destroy);
368 } else if ((rdata->flags & FC_RP_STARTED) &&
369 rdata->major_retries <
370 lport->max_rport_retry_count) {
371 rdata->major_retries++;
372 rdata->event = RPORT_EV_NONE;
373 FC_RPORT_DBG(rdata, "work restart\n");
374 fc_rport_enter_flogi(rdata);
375 mutex_unlock(&rdata->rp_mutex);
376 } else {
377 mutex_unlock(&rdata->rp_mutex);
378 FC_RPORT_DBG(rdata, "work delete\n");
379 mutex_lock(&lport->disc.disc_mutex);
380 list_del_rcu(&rdata->peers);
381 mutex_unlock(&lport->disc.disc_mutex);
382 kref_put(&rdata->kref, fc_rport_destroy);
384 } else {
386 * Re-open for events. Reissue READY event if ready.
388 rdata->event = RPORT_EV_NONE;
389 if (rdata->rp_state == RPORT_ST_READY) {
390 FC_RPORT_DBG(rdata, "work reopen\n");
391 fc_rport_enter_ready(rdata);
393 mutex_unlock(&rdata->rp_mutex);
395 break;
397 default:
398 mutex_unlock(&rdata->rp_mutex);
399 break;
401 kref_put(&rdata->kref, fc_rport_destroy);
405 * fc_rport_login() - Start the remote port login state machine
406 * @rdata: The remote port to be logged in to
408 * Initiates the RP state machine. It is called from the LP module.
409 * This function will issue the following commands to the N_Port
410 * identified by the FC ID provided.
412 * - PLOGI
413 * - PRLI
414 * - RTV
416 * Locking Note: Called without the rport lock held. This
417 * function will hold the rport lock, call an _enter_*
418 * function and then unlock the rport.
420 * This indicates the intent to be logged into the remote port.
421 * If it appears we are already logged in, ADISC is used to verify
422 * the setup.
424 int fc_rport_login(struct fc_rport_priv *rdata)
426 mutex_lock(&rdata->rp_mutex);
428 if (rdata->flags & FC_RP_STARTED) {
429 FC_RPORT_DBG(rdata, "port already started\n");
430 mutex_unlock(&rdata->rp_mutex);
431 return 0;
434 rdata->flags |= FC_RP_STARTED;
435 switch (rdata->rp_state) {
436 case RPORT_ST_READY:
437 FC_RPORT_DBG(rdata, "ADISC port\n");
438 fc_rport_enter_adisc(rdata);
439 break;
440 case RPORT_ST_DELETE:
441 FC_RPORT_DBG(rdata, "Restart deleted port\n");
442 break;
443 case RPORT_ST_INIT:
444 FC_RPORT_DBG(rdata, "Login to port\n");
445 fc_rport_enter_flogi(rdata);
446 break;
447 default:
448 FC_RPORT_DBG(rdata, "Login in progress, state %s\n",
449 fc_rport_state(rdata));
450 break;
452 mutex_unlock(&rdata->rp_mutex);
454 return 0;
456 EXPORT_SYMBOL(fc_rport_login);
459 * fc_rport_enter_delete() - Schedule a remote port to be deleted
460 * @rdata: The remote port to be deleted
461 * @event: The event to report as the reason for deletion
463 * Allow state change into DELETE only once.
465 * Call queue_work only if there's no event already pending.
466 * Set the new event so that the old pending event will not occur.
467 * Since we have the mutex, even if fc_rport_work() is already started,
468 * it'll see the new event.
470 * Reference counting: does not modify kref
472 static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
473 enum fc_rport_event event)
475 lockdep_assert_held(&rdata->rp_mutex);
477 if (rdata->rp_state == RPORT_ST_DELETE)
478 return;
480 FC_RPORT_DBG(rdata, "Delete port\n");
482 fc_rport_state_enter(rdata, RPORT_ST_DELETE);
484 kref_get(&rdata->kref);
485 if (rdata->event == RPORT_EV_NONE &&
486 !queue_work(rport_event_queue, &rdata->event_work))
487 kref_put(&rdata->kref, fc_rport_destroy);
489 rdata->event = event;
493 * fc_rport_logoff() - Logoff and remove a remote port
494 * @rdata: The remote port to be logged off of
496 * Locking Note: Called without the rport lock held. This
497 * function will hold the rport lock, call an _enter_*
498 * function and then unlock the rport.
500 int fc_rport_logoff(struct fc_rport_priv *rdata)
502 struct fc_lport *lport = rdata->local_port;
503 u32 port_id = rdata->ids.port_id;
505 mutex_lock(&rdata->rp_mutex);
507 FC_RPORT_DBG(rdata, "Remove port\n");
509 rdata->flags &= ~FC_RP_STARTED;
510 if (rdata->rp_state == RPORT_ST_DELETE) {
511 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
512 goto out;
515 * FC-LS states:
516 * To explicitly Logout, the initiating Nx_Port shall terminate
517 * other open Sequences that it initiated with the destination
518 * Nx_Port prior to performing Logout.
520 lport->tt.exch_mgr_reset(lport, 0, port_id);
521 lport->tt.exch_mgr_reset(lport, port_id, 0);
523 fc_rport_enter_logo(rdata);
526 * Change the state to Delete so that we discard
527 * the response.
529 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
530 out:
531 mutex_unlock(&rdata->rp_mutex);
532 return 0;
534 EXPORT_SYMBOL(fc_rport_logoff);
537 * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state
538 * @rdata: The remote port that is ready
540 * Reference counting: schedules workqueue, does not modify kref
542 static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
544 lockdep_assert_held(&rdata->rp_mutex);
546 fc_rport_state_enter(rdata, RPORT_ST_READY);
548 FC_RPORT_DBG(rdata, "Port is Ready\n");
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, fc_rport_destroy);
555 rdata->event = RPORT_EV_READY;
559 * fc_rport_timeout() - Handler for the retry_work timer
560 * @work: Handle to the remote port that has timed out
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.
566 * Reference counting: Drops kref on return.
568 static void fc_rport_timeout(struct work_struct *work)
570 struct fc_rport_priv *rdata =
571 container_of(work, struct fc_rport_priv, retry_work.work);
573 mutex_lock(&rdata->rp_mutex);
574 FC_RPORT_DBG(rdata, "Port timeout, state %s\n", fc_rport_state(rdata));
576 switch (rdata->rp_state) {
577 case RPORT_ST_FLOGI:
578 fc_rport_enter_flogi(rdata);
579 break;
580 case RPORT_ST_PLOGI:
581 fc_rport_enter_plogi(rdata);
582 break;
583 case RPORT_ST_PRLI:
584 fc_rport_enter_prli(rdata);
585 break;
586 case RPORT_ST_RTV:
587 fc_rport_enter_rtv(rdata);
588 break;
589 case RPORT_ST_ADISC:
590 fc_rport_enter_adisc(rdata);
591 break;
592 case RPORT_ST_PLOGI_WAIT:
593 case RPORT_ST_READY:
594 case RPORT_ST_INIT:
595 case RPORT_ST_DELETE:
596 break;
599 mutex_unlock(&rdata->rp_mutex);
600 kref_put(&rdata->kref, fc_rport_destroy);
604 * fc_rport_error() - Error handler, called once retries have been exhausted
605 * @rdata: The remote port the error is happened on
606 * @err: The error code
608 * Reference counting: does not modify kref
610 static void fc_rport_error(struct fc_rport_priv *rdata, int err)
612 struct fc_lport *lport = rdata->local_port;
614 lockdep_assert_held(&rdata->rp_mutex);
616 FC_RPORT_DBG(rdata, "Error %d in state %s, retries %d\n",
617 -err, fc_rport_state(rdata), rdata->retries);
619 switch (rdata->rp_state) {
620 case RPORT_ST_FLOGI:
621 rdata->flags &= ~FC_RP_STARTED;
622 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
623 break;
624 case RPORT_ST_PLOGI:
625 if (lport->point_to_multipoint) {
626 rdata->flags &= ~FC_RP_STARTED;
627 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
628 } else
629 fc_rport_enter_logo(rdata);
630 break;
631 case RPORT_ST_RTV:
632 fc_rport_enter_ready(rdata);
633 break;
634 case RPORT_ST_PRLI:
635 fc_rport_enter_plogi(rdata);
636 break;
637 case RPORT_ST_ADISC:
638 fc_rport_enter_logo(rdata);
639 break;
640 case RPORT_ST_PLOGI_WAIT:
641 case RPORT_ST_DELETE:
642 case RPORT_ST_READY:
643 case RPORT_ST_INIT:
644 break;
649 * fc_rport_error_retry() - Handler for remote port state retries
650 * @rdata: The remote port whose state is to be retried
651 * @err: The error code
653 * If the error was an exchange timeout retry immediately,
654 * otherwise wait for E_D_TOV.
656 * Reference counting: increments kref when scheduling retry_work
658 static void fc_rport_error_retry(struct fc_rport_priv *rdata, int err)
660 unsigned long delay = msecs_to_jiffies(rdata->e_d_tov);
662 lockdep_assert_held(&rdata->rp_mutex);
664 /* make sure this isn't an FC_EX_CLOSED error, never retry those */
665 if (err == -FC_EX_CLOSED)
666 goto out;
668 if (rdata->retries < rdata->local_port->max_rport_retry_count) {
669 FC_RPORT_DBG(rdata, "Error %d in state %s, retrying\n",
670 err, fc_rport_state(rdata));
671 rdata->retries++;
672 /* no additional delay on exchange timeouts */
673 if (err == -FC_EX_TIMEOUT)
674 delay = 0;
675 kref_get(&rdata->kref);
676 if (!schedule_delayed_work(&rdata->retry_work, delay))
677 kref_put(&rdata->kref, fc_rport_destroy);
678 return;
681 out:
682 fc_rport_error(rdata, err);
686 * fc_rport_login_complete() - Handle parameters and completion of p-mp login.
687 * @rdata: The remote port which we logged into or which logged into us.
688 * @fp: The FLOGI or PLOGI request or response frame
690 * Returns non-zero error if a problem is detected with the frame.
691 * Does not free the frame.
693 * This is only used in point-to-multipoint mode for FIP currently.
695 static int fc_rport_login_complete(struct fc_rport_priv *rdata,
696 struct fc_frame *fp)
698 struct fc_lport *lport = rdata->local_port;
699 struct fc_els_flogi *flogi;
700 unsigned int e_d_tov;
701 u16 csp_flags;
703 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
704 if (!flogi)
705 return -EINVAL;
707 csp_flags = ntohs(flogi->fl_csp.sp_features);
709 if (fc_frame_payload_op(fp) == ELS_FLOGI) {
710 if (csp_flags & FC_SP_FT_FPORT) {
711 FC_RPORT_DBG(rdata, "Fabric bit set in FLOGI\n");
712 return -EINVAL;
714 } else {
717 * E_D_TOV is not valid on an incoming FLOGI request.
719 e_d_tov = ntohl(flogi->fl_csp.sp_e_d_tov);
720 if (csp_flags & FC_SP_FT_EDTR)
721 e_d_tov /= 1000000;
722 if (e_d_tov > rdata->e_d_tov)
723 rdata->e_d_tov = e_d_tov;
725 rdata->maxframe_size = fc_plogi_get_maxframe(flogi, lport->mfs);
726 return 0;
730 * fc_rport_flogi_resp() - Handle response to FLOGI request for p-mp mode
731 * @sp: The sequence that the FLOGI was on
732 * @fp: The FLOGI response frame
733 * @rp_arg: The remote port that received the FLOGI response
735 static void fc_rport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
736 void *rp_arg)
738 struct fc_rport_priv *rdata = rp_arg;
739 struct fc_lport *lport = rdata->local_port;
740 struct fc_els_flogi *flogi;
741 unsigned int r_a_tov;
742 u8 opcode;
743 int err = 0;
745 FC_RPORT_DBG(rdata, "Received a FLOGI %s\n",
746 IS_ERR(fp) ? "error" : fc_els_resp_type(fp));
748 if (fp == ERR_PTR(-FC_EX_CLOSED))
749 goto put;
751 mutex_lock(&rdata->rp_mutex);
753 if (rdata->rp_state != RPORT_ST_FLOGI) {
754 FC_RPORT_DBG(rdata, "Received a FLOGI response, but in state "
755 "%s\n", fc_rport_state(rdata));
756 if (IS_ERR(fp))
757 goto err;
758 goto out;
761 if (IS_ERR(fp)) {
762 fc_rport_error(rdata, PTR_ERR(fp));
763 goto err;
765 opcode = fc_frame_payload_op(fp);
766 if (opcode == ELS_LS_RJT) {
767 struct fc_els_ls_rjt *rjt;
769 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
770 FC_RPORT_DBG(rdata, "FLOGI ELS rejected, reason %x expl %x\n",
771 rjt->er_reason, rjt->er_explan);
772 err = -FC_EX_ELS_RJT;
773 goto bad;
774 } else if (opcode != ELS_LS_ACC) {
775 FC_RPORT_DBG(rdata, "FLOGI ELS invalid opcode %x\n", opcode);
776 err = -FC_EX_ELS_RJT;
777 goto bad;
779 if (fc_rport_login_complete(rdata, fp)) {
780 FC_RPORT_DBG(rdata, "FLOGI failed, no login\n");
781 err = -FC_EX_INV_LOGIN;
782 goto bad;
785 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
786 if (!flogi) {
787 err = -FC_EX_ALLOC_ERR;
788 goto bad;
790 r_a_tov = ntohl(flogi->fl_csp.sp_r_a_tov);
791 if (r_a_tov > rdata->r_a_tov)
792 rdata->r_a_tov = r_a_tov;
794 if (rdata->ids.port_name < lport->wwpn)
795 fc_rport_enter_plogi(rdata);
796 else
797 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
798 out:
799 fc_frame_free(fp);
800 err:
801 mutex_unlock(&rdata->rp_mutex);
802 put:
803 kref_put(&rdata->kref, fc_rport_destroy);
804 return;
805 bad:
806 FC_RPORT_DBG(rdata, "Bad FLOGI response\n");
807 fc_rport_error_retry(rdata, err);
808 goto out;
812 * fc_rport_enter_flogi() - Send a FLOGI request to the remote port for p-mp
813 * @rdata: The remote port to send a FLOGI to
815 * Reference counting: increments kref when sending ELS
817 static void fc_rport_enter_flogi(struct fc_rport_priv *rdata)
819 struct fc_lport *lport = rdata->local_port;
820 struct fc_frame *fp;
822 lockdep_assert_held(&rdata->rp_mutex);
824 if (!lport->point_to_multipoint)
825 return fc_rport_enter_plogi(rdata);
827 FC_RPORT_DBG(rdata, "Entered FLOGI state from %s state\n",
828 fc_rport_state(rdata));
830 fc_rport_state_enter(rdata, RPORT_ST_FLOGI);
832 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
833 if (!fp)
834 return fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
836 kref_get(&rdata->kref);
837 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_FLOGI,
838 fc_rport_flogi_resp, rdata,
839 2 * lport->r_a_tov)) {
840 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
841 kref_put(&rdata->kref, fc_rport_destroy);
846 * fc_rport_recv_flogi_req() - Handle Fabric Login (FLOGI) request in p-mp mode
847 * @lport: The local port that received the PLOGI request
848 * @rx_fp: The PLOGI request frame
850 * Reference counting: drops kref on return
852 static void fc_rport_recv_flogi_req(struct fc_lport *lport,
853 struct fc_frame *rx_fp)
855 struct fc_els_flogi *flp;
856 struct fc_rport_priv *rdata;
857 struct fc_frame *fp = rx_fp;
858 struct fc_seq_els_data rjt_data;
859 u32 sid;
861 sid = fc_frame_sid(fp);
863 FC_RPORT_ID_DBG(lport, sid, "Received FLOGI request\n");
865 if (!lport->point_to_multipoint) {
866 rjt_data.reason = ELS_RJT_UNSUP;
867 rjt_data.explan = ELS_EXPL_NONE;
868 goto reject;
871 flp = fc_frame_payload_get(fp, sizeof(*flp));
872 if (!flp) {
873 rjt_data.reason = ELS_RJT_LOGIC;
874 rjt_data.explan = ELS_EXPL_INV_LEN;
875 goto reject;
878 rdata = fc_rport_lookup(lport, sid);
879 if (!rdata) {
880 rjt_data.reason = ELS_RJT_FIP;
881 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
882 goto reject;
884 mutex_lock(&rdata->rp_mutex);
886 FC_RPORT_DBG(rdata, "Received FLOGI in %s state\n",
887 fc_rport_state(rdata));
889 switch (rdata->rp_state) {
890 case RPORT_ST_INIT:
892 * If received the FLOGI request on RPORT which is INIT state
893 * (means not transition to FLOGI either fc_rport timeout
894 * function didn;t trigger or this end hasn;t received
895 * beacon yet from other end. In that case only, allow RPORT
896 * state machine to continue, otherwise fall through which
897 * causes the code to send reject response.
898 * NOTE; Not checking for FIP->state such as VNMP_UP or
899 * VNMP_CLAIM because if FIP state is not one of those,
900 * RPORT wouldn;t have created and 'rport_lookup' would have
901 * failed anyway in that case.
903 break;
904 case RPORT_ST_DELETE:
905 mutex_unlock(&rdata->rp_mutex);
906 rjt_data.reason = ELS_RJT_FIP;
907 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
908 goto reject_put;
909 case RPORT_ST_FLOGI:
910 case RPORT_ST_PLOGI_WAIT:
911 case RPORT_ST_PLOGI:
912 break;
913 case RPORT_ST_PRLI:
914 case RPORT_ST_RTV:
915 case RPORT_ST_READY:
916 case RPORT_ST_ADISC:
918 * Set the remote port to be deleted and to then restart.
919 * This queues work to be sure exchanges are reset.
921 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
922 mutex_unlock(&rdata->rp_mutex);
923 rjt_data.reason = ELS_RJT_BUSY;
924 rjt_data.explan = ELS_EXPL_NONE;
925 goto reject_put;
927 if (fc_rport_login_complete(rdata, fp)) {
928 mutex_unlock(&rdata->rp_mutex);
929 rjt_data.reason = ELS_RJT_LOGIC;
930 rjt_data.explan = ELS_EXPL_NONE;
931 goto reject_put;
934 fp = fc_frame_alloc(lport, sizeof(*flp));
935 if (!fp)
936 goto out;
938 fc_flogi_fill(lport, fp);
939 flp = fc_frame_payload_get(fp, sizeof(*flp));
940 flp->fl_cmd = ELS_LS_ACC;
942 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
943 lport->tt.frame_send(lport, fp);
946 * Do not proceed with the state machine if our
947 * FLOGI has crossed with an FLOGI from the
948 * remote port; wait for the FLOGI response instead.
950 if (rdata->rp_state != RPORT_ST_FLOGI) {
951 if (rdata->ids.port_name < lport->wwpn)
952 fc_rport_enter_plogi(rdata);
953 else
954 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
956 out:
957 mutex_unlock(&rdata->rp_mutex);
958 kref_put(&rdata->kref, fc_rport_destroy);
959 fc_frame_free(rx_fp);
960 return;
962 reject_put:
963 kref_put(&rdata->kref, fc_rport_destroy);
964 reject:
965 fc_seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
966 fc_frame_free(rx_fp);
970 * fc_rport_plogi_resp() - Handler for ELS PLOGI responses
971 * @sp: The sequence the PLOGI is on
972 * @fp: The PLOGI response frame
973 * @rdata_arg: The remote port that sent the PLOGI response
975 * Locking Note: This function will be called without the rport lock
976 * held, but it will lock, call an _enter_* function or fc_rport_error
977 * and then unlock the rport.
979 static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
980 void *rdata_arg)
982 struct fc_rport_priv *rdata = rdata_arg;
983 struct fc_lport *lport = rdata->local_port;
984 struct fc_els_flogi *plp = NULL;
985 u16 csp_seq;
986 u16 cssp_seq;
987 u8 op;
989 FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
991 if (fp == ERR_PTR(-FC_EX_CLOSED))
992 goto put;
994 mutex_lock(&rdata->rp_mutex);
996 if (rdata->rp_state != RPORT_ST_PLOGI) {
997 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
998 "%s\n", fc_rport_state(rdata));
999 if (IS_ERR(fp))
1000 goto err;
1001 goto out;
1004 if (IS_ERR(fp)) {
1005 fc_rport_error_retry(rdata, PTR_ERR(fp));
1006 goto err;
1009 op = fc_frame_payload_op(fp);
1010 if (op == ELS_LS_ACC &&
1011 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
1012 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
1013 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
1015 /* save plogi response sp_features for further reference */
1016 rdata->sp_features = ntohs(plp->fl_csp.sp_features);
1018 if (lport->point_to_multipoint)
1019 fc_rport_login_complete(rdata, fp);
1020 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
1021 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
1022 if (cssp_seq < csp_seq)
1023 csp_seq = cssp_seq;
1024 rdata->max_seq = csp_seq;
1025 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
1026 fc_rport_enter_prli(rdata);
1027 } else {
1028 struct fc_els_ls_rjt *rjt;
1030 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1031 if (!rjt)
1032 FC_RPORT_DBG(rdata, "PLOGI bad response\n");
1033 else
1034 FC_RPORT_DBG(rdata, "PLOGI ELS rejected, reason %x expl %x\n",
1035 rjt->er_reason, rjt->er_explan);
1036 fc_rport_error_retry(rdata, -FC_EX_ELS_RJT);
1038 out:
1039 fc_frame_free(fp);
1040 err:
1041 mutex_unlock(&rdata->rp_mutex);
1042 put:
1043 kref_put(&rdata->kref, fc_rport_destroy);
1046 static bool
1047 fc_rport_compatible_roles(struct fc_lport *lport, struct fc_rport_priv *rdata)
1049 if (rdata->ids.roles == FC_PORT_ROLE_UNKNOWN)
1050 return true;
1051 if ((rdata->ids.roles & FC_PORT_ROLE_FCP_TARGET) &&
1052 (lport->service_params & FCP_SPPF_INIT_FCN))
1053 return true;
1054 if ((rdata->ids.roles & FC_PORT_ROLE_FCP_INITIATOR) &&
1055 (lport->service_params & FCP_SPPF_TARG_FCN))
1056 return true;
1057 return false;
1061 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
1062 * @rdata: The remote port to send a PLOGI to
1064 * Reference counting: increments kref when sending ELS
1066 static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
1068 struct fc_lport *lport = rdata->local_port;
1069 struct fc_frame *fp;
1071 lockdep_assert_held(&rdata->rp_mutex);
1073 if (!fc_rport_compatible_roles(lport, rdata)) {
1074 FC_RPORT_DBG(rdata, "PLOGI suppressed for incompatible role\n");
1075 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
1076 return;
1079 FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
1080 fc_rport_state(rdata));
1082 fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
1084 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
1085 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
1086 if (!fp) {
1087 FC_RPORT_DBG(rdata, "%s frame alloc failed\n", __func__);
1088 fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
1089 return;
1091 rdata->e_d_tov = lport->e_d_tov;
1093 kref_get(&rdata->kref);
1094 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
1095 fc_rport_plogi_resp, rdata,
1096 2 * lport->r_a_tov)) {
1097 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
1098 kref_put(&rdata->kref, fc_rport_destroy);
1103 * fc_rport_prli_resp() - Process Login (PRLI) response handler
1104 * @sp: The sequence the PRLI response was on
1105 * @fp: The PRLI response frame
1106 * @rdata_arg: The remote port that sent the PRLI response
1108 * Locking Note: This function will be called without the rport lock
1109 * held, but it will lock, call an _enter_* function or fc_rport_error
1110 * and then unlock the rport.
1112 static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
1113 void *rdata_arg)
1115 struct fc_rport_priv *rdata = rdata_arg;
1116 struct {
1117 struct fc_els_prli prli;
1118 struct fc_els_spp spp;
1119 } *pp;
1120 struct fc_els_spp temp_spp;
1121 struct fc_els_ls_rjt *rjt;
1122 struct fc4_prov *prov;
1123 u32 roles = FC_RPORT_ROLE_UNKNOWN;
1124 u32 fcp_parm = 0;
1125 u8 op;
1126 enum fc_els_spp_resp resp_code;
1128 FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
1130 if (fp == ERR_PTR(-FC_EX_CLOSED))
1131 goto put;
1133 mutex_lock(&rdata->rp_mutex);
1135 if (rdata->rp_state != RPORT_ST_PRLI) {
1136 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
1137 "%s\n", fc_rport_state(rdata));
1138 if (IS_ERR(fp))
1139 goto err;
1140 goto out;
1143 if (IS_ERR(fp)) {
1144 fc_rport_error_retry(rdata, PTR_ERR(fp));
1145 goto err;
1148 /* reinitialize remote port roles */
1149 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
1151 op = fc_frame_payload_op(fp);
1152 if (op == ELS_LS_ACC) {
1153 pp = fc_frame_payload_get(fp, sizeof(*pp));
1154 if (!pp) {
1155 fc_rport_error_retry(rdata, -FC_EX_SEQ_ERR);
1156 goto out;
1159 resp_code = (pp->spp.spp_flags & FC_SPP_RESP_MASK);
1160 FC_RPORT_DBG(rdata, "PRLI spp_flags = 0x%x spp_type 0x%x\n",
1161 pp->spp.spp_flags, pp->spp.spp_type);
1162 rdata->spp_type = pp->spp.spp_type;
1163 if (resp_code != FC_SPP_RESP_ACK) {
1164 if (resp_code == FC_SPP_RESP_CONF)
1165 fc_rport_error(rdata, -FC_EX_SEQ_ERR);
1166 else
1167 fc_rport_error_retry(rdata, -FC_EX_SEQ_ERR);
1168 goto out;
1170 if (pp->prli.prli_spp_len < sizeof(pp->spp)) {
1171 fc_rport_error_retry(rdata, -FC_EX_SEQ_ERR);
1172 goto out;
1175 fcp_parm = ntohl(pp->spp.spp_params);
1176 if (fcp_parm & FCP_SPPF_RETRY)
1177 rdata->flags |= FC_RP_FLAGS_RETRY;
1178 if (fcp_parm & FCP_SPPF_CONF_COMPL)
1179 rdata->flags |= FC_RP_FLAGS_CONF_REQ;
1182 * Call prli provider if we should act as a target
1184 prov = fc_passive_prov[rdata->spp_type];
1185 if (prov) {
1186 memset(&temp_spp, 0, sizeof(temp_spp));
1187 prov->prli(rdata, pp->prli.prli_spp_len,
1188 &pp->spp, &temp_spp);
1191 * Check if the image pair could be established
1193 if (rdata->spp_type != FC_TYPE_FCP ||
1194 !(pp->spp.spp_flags & FC_SPP_EST_IMG_PAIR)) {
1196 * Nope; we can't use this port as a target.
1198 fcp_parm &= ~FCP_SPPF_TARG_FCN;
1200 rdata->supported_classes = FC_COS_CLASS3;
1201 if (fcp_parm & FCP_SPPF_INIT_FCN)
1202 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1203 if (fcp_parm & FCP_SPPF_TARG_FCN)
1204 roles |= FC_RPORT_ROLE_FCP_TARGET;
1206 rdata->ids.roles = roles;
1207 fc_rport_enter_rtv(rdata);
1209 } else {
1210 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1211 if (!rjt)
1212 FC_RPORT_DBG(rdata, "PRLI bad response\n");
1213 else {
1214 FC_RPORT_DBG(rdata, "PRLI ELS rejected, reason %x expl %x\n",
1215 rjt->er_reason, rjt->er_explan);
1216 if (rjt->er_reason == ELS_RJT_UNAB &&
1217 rjt->er_explan == ELS_EXPL_PLOGI_REQD) {
1218 fc_rport_enter_plogi(rdata);
1219 goto out;
1222 fc_rport_error_retry(rdata, FC_EX_ELS_RJT);
1225 out:
1226 fc_frame_free(fp);
1227 err:
1228 mutex_unlock(&rdata->rp_mutex);
1229 put:
1230 kref_put(&rdata->kref, fc_rport_destroy);
1234 * fc_rport_enter_prli() - Send Process Login (PRLI) request
1235 * @rdata: The remote port to send the PRLI request to
1237 * Reference counting: increments kref when sending ELS
1239 static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
1241 struct fc_lport *lport = rdata->local_port;
1242 struct {
1243 struct fc_els_prli prli;
1244 struct fc_els_spp spp;
1245 } *pp;
1246 struct fc_frame *fp;
1247 struct fc4_prov *prov;
1249 lockdep_assert_held(&rdata->rp_mutex);
1252 * If the rport is one of the well known addresses
1253 * we skip PRLI and RTV and go straight to READY.
1255 if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
1256 fc_rport_enter_ready(rdata);
1257 return;
1261 * And if the local port does not support the initiator function
1262 * there's no need to send a PRLI, either.
1264 if (!(lport->service_params & FCP_SPPF_INIT_FCN)) {
1265 fc_rport_enter_ready(rdata);
1266 return;
1269 FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
1270 fc_rport_state(rdata));
1272 fc_rport_state_enter(rdata, RPORT_ST_PRLI);
1274 fp = fc_frame_alloc(lport, sizeof(*pp));
1275 if (!fp) {
1276 fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
1277 return;
1280 fc_prli_fill(lport, fp);
1282 prov = fc_passive_prov[FC_TYPE_FCP];
1283 if (prov) {
1284 pp = fc_frame_payload_get(fp, sizeof(*pp));
1285 prov->prli(rdata, sizeof(pp->spp), NULL, &pp->spp);
1288 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rdata->ids.port_id,
1289 fc_host_port_id(lport->host), FC_TYPE_ELS,
1290 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1292 kref_get(&rdata->kref);
1293 if (!fc_exch_seq_send(lport, fp, fc_rport_prli_resp,
1294 NULL, rdata, 2 * lport->r_a_tov)) {
1295 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
1296 kref_put(&rdata->kref, fc_rport_destroy);
1301 * fc_rport_rtv_resp() - Handler for Request Timeout Value (RTV) responses
1302 * @sp: The sequence the RTV was on
1303 * @fp: The RTV response frame
1304 * @rdata_arg: The remote port that sent the RTV response
1306 * Many targets don't seem to support this.
1308 * Locking Note: This function will be called without the rport lock
1309 * held, but it will lock, call an _enter_* function or fc_rport_error
1310 * and then unlock the rport.
1312 static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
1313 void *rdata_arg)
1315 struct fc_rport_priv *rdata = rdata_arg;
1316 u8 op;
1318 FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
1320 if (fp == ERR_PTR(-FC_EX_CLOSED))
1321 goto put;
1323 mutex_lock(&rdata->rp_mutex);
1325 if (rdata->rp_state != RPORT_ST_RTV) {
1326 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
1327 "%s\n", fc_rport_state(rdata));
1328 if (IS_ERR(fp))
1329 goto err;
1330 goto out;
1333 if (IS_ERR(fp)) {
1334 fc_rport_error(rdata, PTR_ERR(fp));
1335 goto err;
1338 op = fc_frame_payload_op(fp);
1339 if (op == ELS_LS_ACC) {
1340 struct fc_els_rtv_acc *rtv;
1341 u32 toq;
1342 u32 tov;
1344 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
1345 if (rtv) {
1346 toq = ntohl(rtv->rtv_toq);
1347 tov = ntohl(rtv->rtv_r_a_tov);
1348 if (tov == 0)
1349 tov = 1;
1350 if (tov > rdata->r_a_tov)
1351 rdata->r_a_tov = tov;
1352 tov = ntohl(rtv->rtv_e_d_tov);
1353 if (toq & FC_ELS_RTV_EDRES)
1354 tov /= 1000000;
1355 if (tov == 0)
1356 tov = 1;
1357 if (tov > rdata->e_d_tov)
1358 rdata->e_d_tov = tov;
1362 fc_rport_enter_ready(rdata);
1364 out:
1365 fc_frame_free(fp);
1366 err:
1367 mutex_unlock(&rdata->rp_mutex);
1368 put:
1369 kref_put(&rdata->kref, fc_rport_destroy);
1373 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
1374 * @rdata: The remote port to send the RTV request to
1376 * Reference counting: increments kref when sending ELS
1378 static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
1380 struct fc_frame *fp;
1381 struct fc_lport *lport = rdata->local_port;
1383 lockdep_assert_held(&rdata->rp_mutex);
1385 FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
1386 fc_rport_state(rdata));
1388 fc_rport_state_enter(rdata, RPORT_ST_RTV);
1390 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
1391 if (!fp) {
1392 fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
1393 return;
1396 kref_get(&rdata->kref);
1397 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
1398 fc_rport_rtv_resp, rdata,
1399 2 * lport->r_a_tov)) {
1400 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
1401 kref_put(&rdata->kref, fc_rport_destroy);
1406 * fc_rport_recv_rtv_req() - Handler for Read Timeout Value (RTV) requests
1407 * @rdata: The remote port that sent the RTV request
1408 * @in_fp: The RTV request frame
1410 static void fc_rport_recv_rtv_req(struct fc_rport_priv *rdata,
1411 struct fc_frame *in_fp)
1413 struct fc_lport *lport = rdata->local_port;
1414 struct fc_frame *fp;
1415 struct fc_els_rtv_acc *rtv;
1416 struct fc_seq_els_data rjt_data;
1418 lockdep_assert_held(&rdata->rp_mutex);
1419 lockdep_assert_held(&lport->lp_mutex);
1421 FC_RPORT_DBG(rdata, "Received RTV request\n");
1423 fp = fc_frame_alloc(lport, sizeof(*rtv));
1424 if (!fp) {
1425 rjt_data.reason = ELS_RJT_UNAB;
1426 rjt_data.explan = ELS_EXPL_INSUF_RES;
1427 fc_seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data);
1428 goto drop;
1430 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
1431 rtv->rtv_cmd = ELS_LS_ACC;
1432 rtv->rtv_r_a_tov = htonl(lport->r_a_tov);
1433 rtv->rtv_e_d_tov = htonl(lport->e_d_tov);
1434 rtv->rtv_toq = 0;
1435 fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
1436 lport->tt.frame_send(lport, fp);
1437 drop:
1438 fc_frame_free(in_fp);
1442 * fc_rport_logo_resp() - Handler for logout (LOGO) responses
1443 * @sp: The sequence the LOGO was on
1444 * @fp: The LOGO response frame
1445 * @lport_arg: The local port
1447 static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
1448 void *rdata_arg)
1450 struct fc_rport_priv *rdata = rdata_arg;
1451 struct fc_lport *lport = rdata->local_port;
1453 FC_RPORT_ID_DBG(lport, fc_seq_exch(sp)->did,
1454 "Received a LOGO %s\n", fc_els_resp_type(fp));
1455 if (!IS_ERR(fp))
1456 fc_frame_free(fp);
1457 kref_put(&rdata->kref, fc_rport_destroy);
1461 * fc_rport_enter_logo() - Send a logout (LOGO) request
1462 * @rdata: The remote port to send the LOGO request to
1464 * Reference counting: increments kref when sending ELS
1466 static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
1468 struct fc_lport *lport = rdata->local_port;
1469 struct fc_frame *fp;
1471 lockdep_assert_held(&rdata->rp_mutex);
1473 FC_RPORT_DBG(rdata, "Port sending LOGO from %s state\n",
1474 fc_rport_state(rdata));
1476 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
1477 if (!fp)
1478 return;
1479 kref_get(&rdata->kref);
1480 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
1481 fc_rport_logo_resp, rdata, 0))
1482 kref_put(&rdata->kref, fc_rport_destroy);
1486 * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
1487 * @sp: The sequence the ADISC response was on
1488 * @fp: The ADISC response frame
1489 * @rdata_arg: The remote port that sent the ADISC response
1491 * Locking Note: This function will be called without the rport lock
1492 * held, but it will lock, call an _enter_* function or fc_rport_error
1493 * and then unlock the rport.
1495 static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
1496 void *rdata_arg)
1498 struct fc_rport_priv *rdata = rdata_arg;
1499 struct fc_els_adisc *adisc;
1500 u8 op;
1502 FC_RPORT_DBG(rdata, "Received a ADISC response\n");
1504 if (fp == ERR_PTR(-FC_EX_CLOSED))
1505 goto put;
1507 mutex_lock(&rdata->rp_mutex);
1509 if (rdata->rp_state != RPORT_ST_ADISC) {
1510 FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
1511 fc_rport_state(rdata));
1512 if (IS_ERR(fp))
1513 goto err;
1514 goto out;
1517 if (IS_ERR(fp)) {
1518 fc_rport_error(rdata, PTR_ERR(fp));
1519 goto err;
1523 * If address verification failed. Consider us logged out of the rport.
1524 * Since the rport is still in discovery, we want to be
1525 * logged in, so go to PLOGI state. Otherwise, go back to READY.
1527 op = fc_frame_payload_op(fp);
1528 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1529 if (op != ELS_LS_ACC || !adisc ||
1530 ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
1531 get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
1532 get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
1533 FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
1534 fc_rport_enter_flogi(rdata);
1535 } else {
1536 FC_RPORT_DBG(rdata, "ADISC OK\n");
1537 fc_rport_enter_ready(rdata);
1539 out:
1540 fc_frame_free(fp);
1541 err:
1542 mutex_unlock(&rdata->rp_mutex);
1543 put:
1544 kref_put(&rdata->kref, fc_rport_destroy);
1548 * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
1549 * @rdata: The remote port to send the ADISC request to
1551 * Reference counting: increments kref when sending ELS
1553 static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
1555 struct fc_lport *lport = rdata->local_port;
1556 struct fc_frame *fp;
1558 lockdep_assert_held(&rdata->rp_mutex);
1560 FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
1561 fc_rport_state(rdata));
1563 fc_rport_state_enter(rdata, RPORT_ST_ADISC);
1565 fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
1566 if (!fp) {
1567 fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
1568 return;
1570 kref_get(&rdata->kref);
1571 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
1572 fc_rport_adisc_resp, rdata,
1573 2 * lport->r_a_tov)) {
1574 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
1575 kref_put(&rdata->kref, fc_rport_destroy);
1580 * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
1581 * @rdata: The remote port that sent the ADISC request
1582 * @in_fp: The ADISC request frame
1584 static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
1585 struct fc_frame *in_fp)
1587 struct fc_lport *lport = rdata->local_port;
1588 struct fc_frame *fp;
1589 struct fc_els_adisc *adisc;
1590 struct fc_seq_els_data rjt_data;
1592 lockdep_assert_held(&rdata->rp_mutex);
1593 lockdep_assert_held(&lport->lp_mutex);
1595 FC_RPORT_DBG(rdata, "Received ADISC request\n");
1597 adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
1598 if (!adisc) {
1599 rjt_data.reason = ELS_RJT_PROT;
1600 rjt_data.explan = ELS_EXPL_INV_LEN;
1601 fc_seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data);
1602 goto drop;
1605 fp = fc_frame_alloc(lport, sizeof(*adisc));
1606 if (!fp)
1607 goto drop;
1608 fc_adisc_fill(lport, fp);
1609 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1610 adisc->adisc_cmd = ELS_LS_ACC;
1611 fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
1612 lport->tt.frame_send(lport, fp);
1613 drop:
1614 fc_frame_free(in_fp);
1618 * fc_rport_recv_rls_req() - Handle received Read Link Status request
1619 * @rdata: The remote port that sent the RLS request
1620 * @rx_fp: The PRLI request frame
1622 static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
1623 struct fc_frame *rx_fp)
1626 struct fc_lport *lport = rdata->local_port;
1627 struct fc_frame *fp;
1628 struct fc_els_rls *rls;
1629 struct fc_els_rls_resp *rsp;
1630 struct fc_els_lesb *lesb;
1631 struct fc_seq_els_data rjt_data;
1632 struct fc_host_statistics *hst;
1634 lockdep_assert_held(&rdata->rp_mutex);
1636 FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
1637 fc_rport_state(rdata));
1639 rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
1640 if (!rls) {
1641 rjt_data.reason = ELS_RJT_PROT;
1642 rjt_data.explan = ELS_EXPL_INV_LEN;
1643 goto out_rjt;
1646 fp = fc_frame_alloc(lport, sizeof(*rsp));
1647 if (!fp) {
1648 rjt_data.reason = ELS_RJT_UNAB;
1649 rjt_data.explan = ELS_EXPL_INSUF_RES;
1650 goto out_rjt;
1653 rsp = fc_frame_payload_get(fp, sizeof(*rsp));
1654 memset(rsp, 0, sizeof(*rsp));
1655 rsp->rls_cmd = ELS_LS_ACC;
1656 lesb = &rsp->rls_lesb;
1657 if (lport->tt.get_lesb) {
1658 /* get LESB from LLD if it supports it */
1659 lport->tt.get_lesb(lport, lesb);
1660 } else {
1661 fc_get_host_stats(lport->host);
1662 hst = &lport->host_stats;
1663 lesb->lesb_link_fail = htonl(hst->link_failure_count);
1664 lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
1665 lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
1666 lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
1667 lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
1668 lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
1671 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1672 lport->tt.frame_send(lport, fp);
1673 goto out;
1675 out_rjt:
1676 fc_seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
1677 out:
1678 fc_frame_free(rx_fp);
1682 * fc_rport_recv_els_req() - Handler for validated ELS requests
1683 * @lport: The local port that received the ELS request
1684 * @fp: The ELS request frame
1686 * Handle incoming ELS requests that require port login.
1687 * The ELS opcode has already been validated by the caller.
1689 * Reference counting: does not modify kref
1691 static void fc_rport_recv_els_req(struct fc_lport *lport, struct fc_frame *fp)
1693 struct fc_rport_priv *rdata;
1694 struct fc_seq_els_data els_data;
1696 lockdep_assert_held(&lport->lp_mutex);
1698 rdata = fc_rport_lookup(lport, fc_frame_sid(fp));
1699 if (!rdata) {
1700 FC_RPORT_ID_DBG(lport, fc_frame_sid(fp),
1701 "Received ELS 0x%02x from non-logged-in port\n",
1702 fc_frame_payload_op(fp));
1703 goto reject;
1706 mutex_lock(&rdata->rp_mutex);
1708 switch (rdata->rp_state) {
1709 case RPORT_ST_PRLI:
1710 case RPORT_ST_RTV:
1711 case RPORT_ST_READY:
1712 case RPORT_ST_ADISC:
1713 break;
1714 case RPORT_ST_PLOGI:
1715 if (fc_frame_payload_op(fp) == ELS_PRLI) {
1716 FC_RPORT_DBG(rdata, "Reject ELS PRLI "
1717 "while in state %s\n",
1718 fc_rport_state(rdata));
1719 mutex_unlock(&rdata->rp_mutex);
1720 kref_put(&rdata->kref, fc_rport_destroy);
1721 goto busy;
1723 /* fall through */
1724 default:
1725 FC_RPORT_DBG(rdata,
1726 "Reject ELS 0x%02x while in state %s\n",
1727 fc_frame_payload_op(fp), fc_rport_state(rdata));
1728 mutex_unlock(&rdata->rp_mutex);
1729 kref_put(&rdata->kref, fc_rport_destroy);
1730 goto reject;
1733 switch (fc_frame_payload_op(fp)) {
1734 case ELS_PRLI:
1735 fc_rport_recv_prli_req(rdata, fp);
1736 break;
1737 case ELS_PRLO:
1738 fc_rport_recv_prlo_req(rdata, fp);
1739 break;
1740 case ELS_ADISC:
1741 fc_rport_recv_adisc_req(rdata, fp);
1742 break;
1743 case ELS_RRQ:
1744 fc_seq_els_rsp_send(fp, ELS_RRQ, NULL);
1745 fc_frame_free(fp);
1746 break;
1747 case ELS_REC:
1748 fc_seq_els_rsp_send(fp, ELS_REC, NULL);
1749 fc_frame_free(fp);
1750 break;
1751 case ELS_RLS:
1752 fc_rport_recv_rls_req(rdata, fp);
1753 break;
1754 case ELS_RTV:
1755 fc_rport_recv_rtv_req(rdata, fp);
1756 break;
1757 default:
1758 fc_frame_free(fp); /* can't happen */
1759 break;
1762 mutex_unlock(&rdata->rp_mutex);
1763 kref_put(&rdata->kref, fc_rport_destroy);
1764 return;
1766 reject:
1767 els_data.reason = ELS_RJT_UNAB;
1768 els_data.explan = ELS_EXPL_PLOGI_REQD;
1769 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1770 fc_frame_free(fp);
1771 return;
1773 busy:
1774 els_data.reason = ELS_RJT_BUSY;
1775 els_data.explan = ELS_EXPL_NONE;
1776 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1777 fc_frame_free(fp);
1778 return;
1782 * fc_rport_recv_req() - Handler for requests
1783 * @lport: The local port that received the request
1784 * @fp: The request frame
1786 * Reference counting: does not modify kref
1788 void fc_rport_recv_req(struct fc_lport *lport, struct fc_frame *fp)
1790 struct fc_seq_els_data els_data;
1792 lockdep_assert_held(&lport->lp_mutex);
1795 * Handle FLOGI, PLOGI and LOGO requests separately, since they
1796 * don't require prior login.
1797 * Check for unsupported opcodes first and reject them.
1798 * For some ops, it would be incorrect to reject with "PLOGI required".
1800 switch (fc_frame_payload_op(fp)) {
1801 case ELS_FLOGI:
1802 fc_rport_recv_flogi_req(lport, fp);
1803 break;
1804 case ELS_PLOGI:
1805 fc_rport_recv_plogi_req(lport, fp);
1806 break;
1807 case ELS_LOGO:
1808 fc_rport_recv_logo_req(lport, fp);
1809 break;
1810 case ELS_PRLI:
1811 case ELS_PRLO:
1812 case ELS_ADISC:
1813 case ELS_RRQ:
1814 case ELS_REC:
1815 case ELS_RLS:
1816 case ELS_RTV:
1817 fc_rport_recv_els_req(lport, fp);
1818 break;
1819 default:
1820 els_data.reason = ELS_RJT_UNSUP;
1821 els_data.explan = ELS_EXPL_NONE;
1822 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1823 fc_frame_free(fp);
1824 break;
1827 EXPORT_SYMBOL(fc_rport_recv_req);
1830 * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
1831 * @lport: The local port that received the PLOGI request
1832 * @rx_fp: The PLOGI request frame
1834 * Reference counting: increments kref on return
1836 static void fc_rport_recv_plogi_req(struct fc_lport *lport,
1837 struct fc_frame *rx_fp)
1839 struct fc_disc *disc;
1840 struct fc_rport_priv *rdata;
1841 struct fc_frame *fp = rx_fp;
1842 struct fc_els_flogi *pl;
1843 struct fc_seq_els_data rjt_data;
1844 u32 sid;
1846 lockdep_assert_held(&lport->lp_mutex);
1848 sid = fc_frame_sid(fp);
1850 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
1852 pl = fc_frame_payload_get(fp, sizeof(*pl));
1853 if (!pl) {
1854 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
1855 rjt_data.reason = ELS_RJT_PROT;
1856 rjt_data.explan = ELS_EXPL_INV_LEN;
1857 goto reject;
1860 disc = &lport->disc;
1861 mutex_lock(&disc->disc_mutex);
1862 rdata = fc_rport_create(lport, sid);
1863 if (!rdata) {
1864 mutex_unlock(&disc->disc_mutex);
1865 rjt_data.reason = ELS_RJT_UNAB;
1866 rjt_data.explan = ELS_EXPL_INSUF_RES;
1867 goto reject;
1870 mutex_lock(&rdata->rp_mutex);
1871 mutex_unlock(&disc->disc_mutex);
1873 rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1874 rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
1877 * If the rport was just created, possibly due to the incoming PLOGI,
1878 * set the state appropriately and accept the PLOGI.
1880 * If we had also sent a PLOGI, and if the received PLOGI is from a
1881 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1882 * "command already in progress".
1884 * XXX TBD: If the session was ready before, the PLOGI should result in
1885 * all outstanding exchanges being reset.
1887 switch (rdata->rp_state) {
1888 case RPORT_ST_INIT:
1889 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
1890 break;
1891 case RPORT_ST_PLOGI_WAIT:
1892 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI_WAIT state\n");
1893 break;
1894 case RPORT_ST_PLOGI:
1895 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1896 if (rdata->ids.port_name < lport->wwpn) {
1897 mutex_unlock(&rdata->rp_mutex);
1898 rjt_data.reason = ELS_RJT_INPROG;
1899 rjt_data.explan = ELS_EXPL_NONE;
1900 goto reject;
1902 break;
1903 case RPORT_ST_PRLI:
1904 case RPORT_ST_RTV:
1905 case RPORT_ST_READY:
1906 case RPORT_ST_ADISC:
1907 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
1908 "- ignored for now\n", rdata->rp_state);
1909 /* XXX TBD - should reset */
1910 break;
1911 case RPORT_ST_FLOGI:
1912 case RPORT_ST_DELETE:
1913 FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
1914 fc_rport_state(rdata));
1915 mutex_unlock(&rdata->rp_mutex);
1916 rjt_data.reason = ELS_RJT_BUSY;
1917 rjt_data.explan = ELS_EXPL_NONE;
1918 goto reject;
1920 if (!fc_rport_compatible_roles(lport, rdata)) {
1921 FC_RPORT_DBG(rdata, "Received PLOGI for incompatible role\n");
1922 mutex_unlock(&rdata->rp_mutex);
1923 rjt_data.reason = ELS_RJT_LOGIC;
1924 rjt_data.explan = ELS_EXPL_NONE;
1925 goto reject;
1929 * Get session payload size from incoming PLOGI.
1931 rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
1934 * Send LS_ACC. If this fails, the originator should retry.
1936 fp = fc_frame_alloc(lport, sizeof(*pl));
1937 if (!fp)
1938 goto out;
1940 fc_plogi_fill(lport, fp, ELS_LS_ACC);
1941 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1942 lport->tt.frame_send(lport, fp);
1943 fc_rport_enter_prli(rdata);
1944 out:
1945 mutex_unlock(&rdata->rp_mutex);
1946 fc_frame_free(rx_fp);
1947 return;
1949 reject:
1950 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
1951 fc_frame_free(fp);
1955 * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
1956 * @rdata: The remote port that sent the PRLI request
1957 * @rx_fp: The PRLI request frame
1959 static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
1960 struct fc_frame *rx_fp)
1962 struct fc_lport *lport = rdata->local_port;
1963 struct fc_frame *fp;
1964 struct {
1965 struct fc_els_prli prli;
1966 struct fc_els_spp spp;
1967 } *pp;
1968 struct fc_els_spp *rspp; /* request service param page */
1969 struct fc_els_spp *spp; /* response spp */
1970 unsigned int len;
1971 unsigned int plen;
1972 enum fc_els_spp_resp resp;
1973 struct fc_seq_els_data rjt_data;
1974 struct fc4_prov *prov;
1976 lockdep_assert_held(&rdata->rp_mutex);
1978 FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1979 fc_rport_state(rdata));
1981 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
1982 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1983 if (!pp)
1984 goto reject_len;
1985 plen = ntohs(pp->prli.prli_len);
1986 if ((plen % 4) != 0 || plen > len || plen < 16)
1987 goto reject_len;
1988 if (plen < len)
1989 len = plen;
1990 plen = pp->prli.prli_spp_len;
1991 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1992 plen > len || len < sizeof(*pp) || plen < 12)
1993 goto reject_len;
1994 rspp = &pp->spp;
1996 fp = fc_frame_alloc(lport, len);
1997 if (!fp) {
1998 rjt_data.reason = ELS_RJT_UNAB;
1999 rjt_data.explan = ELS_EXPL_INSUF_RES;
2000 goto reject;
2002 pp = fc_frame_payload_get(fp, len);
2003 WARN_ON(!pp);
2004 memset(pp, 0, len);
2005 pp->prli.prli_cmd = ELS_LS_ACC;
2006 pp->prli.prli_spp_len = plen;
2007 pp->prli.prli_len = htons(len);
2008 len -= sizeof(struct fc_els_prli);
2011 * Go through all the service parameter pages and build
2012 * response. If plen indicates longer SPP than standard,
2013 * use that. The entire response has been pre-cleared above.
2015 spp = &pp->spp;
2016 mutex_lock(&fc_prov_mutex);
2017 while (len >= plen) {
2018 rdata->spp_type = rspp->spp_type;
2019 spp->spp_type = rspp->spp_type;
2020 spp->spp_type_ext = rspp->spp_type_ext;
2021 resp = 0;
2023 if (rspp->spp_type < FC_FC4_PROV_SIZE) {
2024 enum fc_els_spp_resp active = 0, passive = 0;
2026 prov = fc_active_prov[rspp->spp_type];
2027 if (prov)
2028 active = prov->prli(rdata, plen, rspp, spp);
2029 prov = fc_passive_prov[rspp->spp_type];
2030 if (prov)
2031 passive = prov->prli(rdata, plen, rspp, spp);
2032 if (!active || passive == FC_SPP_RESP_ACK)
2033 resp = passive;
2034 else
2035 resp = active;
2036 FC_RPORT_DBG(rdata, "PRLI rspp type %x "
2037 "active %x passive %x\n",
2038 rspp->spp_type, active, passive);
2040 if (!resp) {
2041 if (spp->spp_flags & FC_SPP_EST_IMG_PAIR)
2042 resp |= FC_SPP_RESP_CONF;
2043 else
2044 resp |= FC_SPP_RESP_INVL;
2046 spp->spp_flags |= resp;
2047 len -= plen;
2048 rspp = (struct fc_els_spp *)((char *)rspp + plen);
2049 spp = (struct fc_els_spp *)((char *)spp + plen);
2051 mutex_unlock(&fc_prov_mutex);
2054 * Send LS_ACC. If this fails, the originator should retry.
2056 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
2057 lport->tt.frame_send(lport, fp);
2059 goto drop;
2061 reject_len:
2062 rjt_data.reason = ELS_RJT_PROT;
2063 rjt_data.explan = ELS_EXPL_INV_LEN;
2064 reject:
2065 fc_seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
2066 drop:
2067 fc_frame_free(rx_fp);
2071 * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
2072 * @rdata: The remote port that sent the PRLO request
2073 * @rx_fp: The PRLO request frame
2075 static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
2076 struct fc_frame *rx_fp)
2078 struct fc_lport *lport = rdata->local_port;
2079 struct fc_frame *fp;
2080 struct {
2081 struct fc_els_prlo prlo;
2082 struct fc_els_spp spp;
2083 } *pp;
2084 struct fc_els_spp *rspp; /* request service param page */
2085 struct fc_els_spp *spp; /* response spp */
2086 unsigned int len;
2087 unsigned int plen;
2088 struct fc_seq_els_data rjt_data;
2090 lockdep_assert_held(&rdata->rp_mutex);
2092 FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
2093 fc_rport_state(rdata));
2095 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
2096 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
2097 if (!pp)
2098 goto reject_len;
2099 plen = ntohs(pp->prlo.prlo_len);
2100 if (plen != 20)
2101 goto reject_len;
2102 if (plen < len)
2103 len = plen;
2105 rspp = &pp->spp;
2107 fp = fc_frame_alloc(lport, len);
2108 if (!fp) {
2109 rjt_data.reason = ELS_RJT_UNAB;
2110 rjt_data.explan = ELS_EXPL_INSUF_RES;
2111 goto reject;
2114 pp = fc_frame_payload_get(fp, len);
2115 WARN_ON(!pp);
2116 memset(pp, 0, len);
2117 pp->prlo.prlo_cmd = ELS_LS_ACC;
2118 pp->prlo.prlo_obs = 0x10;
2119 pp->prlo.prlo_len = htons(len);
2120 spp = &pp->spp;
2121 spp->spp_type = rspp->spp_type;
2122 spp->spp_type_ext = rspp->spp_type_ext;
2123 spp->spp_flags = FC_SPP_RESP_ACK;
2125 fc_rport_enter_prli(rdata);
2127 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
2128 lport->tt.frame_send(lport, fp);
2129 goto drop;
2131 reject_len:
2132 rjt_data.reason = ELS_RJT_PROT;
2133 rjt_data.explan = ELS_EXPL_INV_LEN;
2134 reject:
2135 fc_seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
2136 drop:
2137 fc_frame_free(rx_fp);
2141 * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
2142 * @lport: The local port that received the LOGO request
2143 * @fp: The LOGO request frame
2145 * Reference counting: drops kref on return
2147 static void fc_rport_recv_logo_req(struct fc_lport *lport, struct fc_frame *fp)
2149 struct fc_rport_priv *rdata;
2150 u32 sid;
2152 lockdep_assert_held(&lport->lp_mutex);
2154 fc_seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
2156 sid = fc_frame_sid(fp);
2158 rdata = fc_rport_lookup(lport, sid);
2159 if (rdata) {
2160 mutex_lock(&rdata->rp_mutex);
2161 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
2162 fc_rport_state(rdata));
2164 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
2165 mutex_unlock(&rdata->rp_mutex);
2166 kref_put(&rdata->kref, fc_rport_destroy);
2167 } else
2168 FC_RPORT_ID_DBG(lport, sid,
2169 "Received LOGO from non-logged-in port\n");
2170 fc_frame_free(fp);
2174 * fc_rport_flush_queue() - Flush the rport_event_queue
2176 void fc_rport_flush_queue(void)
2178 flush_workqueue(rport_event_queue);
2180 EXPORT_SYMBOL(fc_rport_flush_queue);
2183 * fc_rport_fcp_prli() - Handle incoming PRLI for the FCP initiator.
2184 * @rdata: remote port private
2185 * @spp_len: service parameter page length
2186 * @rspp: received service parameter page
2187 * @spp: response service parameter page
2189 * Returns the value for the response code to be placed in spp_flags;
2190 * Returns 0 if not an initiator.
2192 static int fc_rport_fcp_prli(struct fc_rport_priv *rdata, u32 spp_len,
2193 const struct fc_els_spp *rspp,
2194 struct fc_els_spp *spp)
2196 struct fc_lport *lport = rdata->local_port;
2197 u32 fcp_parm;
2199 fcp_parm = ntohl(rspp->spp_params);
2200 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
2201 if (fcp_parm & FCP_SPPF_INIT_FCN)
2202 rdata->ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2203 if (fcp_parm & FCP_SPPF_TARG_FCN)
2204 rdata->ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2205 if (fcp_parm & FCP_SPPF_RETRY)
2206 rdata->flags |= FC_RP_FLAGS_RETRY;
2207 rdata->supported_classes = FC_COS_CLASS3;
2209 if (!(lport->service_params & FCP_SPPF_INIT_FCN))
2210 return 0;
2212 spp->spp_flags |= rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
2215 * OR in our service parameters with other providers (target), if any.
2217 fcp_parm = ntohl(spp->spp_params);
2218 spp->spp_params = htonl(fcp_parm | lport->service_params);
2219 return FC_SPP_RESP_ACK;
2223 * FC-4 provider ops for FCP initiator.
2225 struct fc4_prov fc_rport_fcp_init = {
2226 .prli = fc_rport_fcp_prli,
2230 * fc_rport_t0_prli() - Handle incoming PRLI parameters for type 0
2231 * @rdata: remote port private
2232 * @spp_len: service parameter page length
2233 * @rspp: received service parameter page
2234 * @spp: response service parameter page
2236 static int fc_rport_t0_prli(struct fc_rport_priv *rdata, u32 spp_len,
2237 const struct fc_els_spp *rspp,
2238 struct fc_els_spp *spp)
2240 if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR)
2241 return FC_SPP_RESP_INVL;
2242 return FC_SPP_RESP_ACK;
2246 * FC-4 provider ops for type 0 service parameters.
2248 * This handles the special case of type 0 which is always successful
2249 * but doesn't do anything otherwise.
2251 struct fc4_prov fc_rport_t0_prov = {
2252 .prli = fc_rport_t0_prli,
2256 * fc_setup_rport() - Initialize the rport_event_queue
2258 int fc_setup_rport(void)
2260 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
2261 if (!rport_event_queue)
2262 return -ENOMEM;
2263 return 0;
2267 * fc_destroy_rport() - Destroy the rport_event_queue
2269 void fc_destroy_rport(void)
2271 destroy_workqueue(rport_event_queue);
2275 * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
2276 * @rport: The remote port whose I/O should be terminated
2278 void fc_rport_terminate_io(struct fc_rport *rport)
2280 struct fc_rport_libfc_priv *rpriv = rport->dd_data;
2281 struct fc_lport *lport = rpriv->local_port;
2283 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
2284 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
2286 EXPORT_SYMBOL(fc_rport_terminate_io);