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