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