xref: /openbmc/linux/drivers/scsi/libfc/fc_rport.c (revision b94f8951)
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 heirarchy.
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 #include <linux/kernel.h>
48 #include <linux/spinlock.h>
49 #include <linux/interrupt.h>
50 #include <linux/rcupdate.h>
51 #include <linux/timer.h>
52 #include <linux/workqueue.h>
53 #include <asm/unaligned.h>
54 
55 #include <scsi/libfc.h>
56 #include <scsi/fc_encode.h>
57 
58 #include "fc_libfc.h"
59 
60 struct workqueue_struct *rport_event_queue;
61 
62 static void fc_rport_enter_plogi(struct fc_rport_priv *);
63 static void fc_rport_enter_prli(struct fc_rport_priv *);
64 static void fc_rport_enter_rtv(struct fc_rport_priv *);
65 static void fc_rport_enter_ready(struct fc_rport_priv *);
66 static void fc_rport_enter_logo(struct fc_rport_priv *);
67 static void fc_rport_enter_adisc(struct fc_rport_priv *);
68 
69 static void fc_rport_recv_plogi_req(struct fc_lport *,
70 				    struct fc_seq *, struct fc_frame *);
71 static void fc_rport_recv_prli_req(struct fc_rport_priv *,
72 				   struct fc_seq *, struct fc_frame *);
73 static void fc_rport_recv_prlo_req(struct fc_rport_priv *,
74 				   struct fc_seq *, struct fc_frame *);
75 static void fc_rport_recv_logo_req(struct fc_lport *,
76 				   struct fc_seq *, struct fc_frame *);
77 static void fc_rport_timeout(struct work_struct *);
78 static void fc_rport_error(struct fc_rport_priv *, struct fc_frame *);
79 static void fc_rport_error_retry(struct fc_rport_priv *, struct fc_frame *);
80 static void fc_rport_work(struct work_struct *);
81 
82 static const char *fc_rport_state_names[] = {
83 	[RPORT_ST_INIT] = "Init",
84 	[RPORT_ST_PLOGI] = "PLOGI",
85 	[RPORT_ST_PRLI] = "PRLI",
86 	[RPORT_ST_RTV] = "RTV",
87 	[RPORT_ST_READY] = "Ready",
88 	[RPORT_ST_LOGO] = "LOGO",
89 	[RPORT_ST_ADISC] = "ADISC",
90 	[RPORT_ST_DELETE] = "Delete",
91 	[RPORT_ST_RESTART] = "Restart",
92 };
93 
94 /**
95  * fc_rport_lookup() - Lookup a remote port by port_id
96  * @lport:   The local port to lookup the remote port on
97  * @port_id: The remote port ID to look up
98  */
99 static struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport,
100 					     u32 port_id)
101 {
102 	struct fc_rport_priv *rdata;
103 
104 	list_for_each_entry(rdata, &lport->disc.rports, peers)
105 		if (rdata->ids.port_id == port_id)
106 			return rdata;
107 	return NULL;
108 }
109 
110 /**
111  * fc_rport_create() - Create a new remote port
112  * @lport: The local port this remote port will be associated with
113  * @ids:   The identifiers for the new remote port
114  *
115  * The remote port will start in the INIT state.
116  *
117  * Locking note:  must be called with the disc_mutex held.
118  */
119 static struct fc_rport_priv *fc_rport_create(struct fc_lport *lport,
120 					     u32 port_id)
121 {
122 	struct fc_rport_priv *rdata;
123 
124 	rdata = lport->tt.rport_lookup(lport, port_id);
125 	if (rdata)
126 		return rdata;
127 
128 	rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
129 	if (!rdata)
130 		return NULL;
131 
132 	rdata->ids.node_name = -1;
133 	rdata->ids.port_name = -1;
134 	rdata->ids.port_id = port_id;
135 	rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
136 
137 	kref_init(&rdata->kref);
138 	mutex_init(&rdata->rp_mutex);
139 	rdata->local_port = lport;
140 	rdata->rp_state = RPORT_ST_INIT;
141 	rdata->event = RPORT_EV_NONE;
142 	rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
143 	rdata->e_d_tov = lport->e_d_tov;
144 	rdata->r_a_tov = lport->r_a_tov;
145 	rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
146 	INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
147 	INIT_WORK(&rdata->event_work, fc_rport_work);
148 	if (port_id != FC_FID_DIR_SERV)
149 		list_add(&rdata->peers, &lport->disc.rports);
150 	return rdata;
151 }
152 
153 /**
154  * fc_rport_destroy() - Free a remote port after last reference is released
155  * @kref: The remote port's kref
156  */
157 static void fc_rport_destroy(struct kref *kref)
158 {
159 	struct fc_rport_priv *rdata;
160 
161 	rdata = container_of(kref, struct fc_rport_priv, kref);
162 	kfree(rdata);
163 }
164 
165 /**
166  * fc_rport_state() - Return a string identifying the remote port's state
167  * @rdata: The remote port
168  */
169 static const char *fc_rport_state(struct fc_rport_priv *rdata)
170 {
171 	const char *cp;
172 
173 	cp = fc_rport_state_names[rdata->rp_state];
174 	if (!cp)
175 		cp = "Unknown";
176 	return cp;
177 }
178 
179 /**
180  * fc_set_rport_loss_tmo() - Set the remote port loss timeout
181  * @rport:   The remote port that gets a new timeout value
182  * @timeout: The new timeout value (in seconds)
183  */
184 void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
185 {
186 	if (timeout)
187 		rport->dev_loss_tmo = timeout + 5;
188 	else
189 		rport->dev_loss_tmo = 30;
190 }
191 EXPORT_SYMBOL(fc_set_rport_loss_tmo);
192 
193 /**
194  * fc_plogi_get_maxframe() - Get the maximum payload from the common service
195  *			     parameters in a FLOGI frame
196  * @flp:    The FLOGI payload
197  * @maxval: The maximum frame size upper limit; this may be less than what
198  *	    is in the service parameters
199  */
200 static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
201 					  unsigned int maxval)
202 {
203 	unsigned int mfs;
204 
205 	/*
206 	 * Get max payload from the common service parameters and the
207 	 * class 3 receive data field size.
208 	 */
209 	mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
210 	if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
211 		maxval = mfs;
212 	mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
213 	if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
214 		maxval = mfs;
215 	return maxval;
216 }
217 
218 /**
219  * fc_rport_state_enter() - Change the state of a remote port
220  * @rdata: The remote port whose state should change
221  * @new:   The new state
222  *
223  * Locking Note: Called with the rport lock held
224  */
225 static void fc_rport_state_enter(struct fc_rport_priv *rdata,
226 				 enum fc_rport_state new)
227 {
228 	if (rdata->rp_state != new)
229 		rdata->retries = 0;
230 	rdata->rp_state = new;
231 }
232 
233 /**
234  * fc_rport_work() - Handler for remote port events in the rport_event_queue
235  * @work: Handle to the remote port being dequeued
236  */
237 static void fc_rport_work(struct work_struct *work)
238 {
239 	u32 port_id;
240 	struct fc_rport_priv *rdata =
241 		container_of(work, struct fc_rport_priv, event_work);
242 	struct fc_rport_libfc_priv *rpriv;
243 	enum fc_rport_event event;
244 	struct fc_lport *lport = rdata->local_port;
245 	struct fc_rport_operations *rport_ops;
246 	struct fc_rport_identifiers ids;
247 	struct fc_rport *rport;
248 	int restart = 0;
249 
250 	mutex_lock(&rdata->rp_mutex);
251 	event = rdata->event;
252 	rport_ops = rdata->ops;
253 	rport = rdata->rport;
254 
255 	FC_RPORT_DBG(rdata, "work event %u\n", event);
256 
257 	switch (event) {
258 	case RPORT_EV_READY:
259 		ids = rdata->ids;
260 		rdata->event = RPORT_EV_NONE;
261 		kref_get(&rdata->kref);
262 		mutex_unlock(&rdata->rp_mutex);
263 
264 		if (!rport)
265 			rport = fc_remote_port_add(lport->host, 0, &ids);
266 		if (!rport) {
267 			FC_RPORT_DBG(rdata, "Failed to add the rport\n");
268 			lport->tt.rport_logoff(rdata);
269 			kref_put(&rdata->kref, lport->tt.rport_destroy);
270 			return;
271 		}
272 		mutex_lock(&rdata->rp_mutex);
273 		if (rdata->rport)
274 			FC_RPORT_DBG(rdata, "rport already allocated\n");
275 		rdata->rport = rport;
276 		rport->maxframe_size = rdata->maxframe_size;
277 		rport->supported_classes = rdata->supported_classes;
278 
279 		rpriv = rport->dd_data;
280 		rpriv->local_port = lport;
281 		rpriv->rp_state = rdata->rp_state;
282 		rpriv->flags = rdata->flags;
283 		rpriv->e_d_tov = rdata->e_d_tov;
284 		rpriv->r_a_tov = rdata->r_a_tov;
285 		mutex_unlock(&rdata->rp_mutex);
286 
287 		if (rport_ops && rport_ops->event_callback) {
288 			FC_RPORT_DBG(rdata, "callback ev %d\n", event);
289 			rport_ops->event_callback(lport, rdata, event);
290 		}
291 		kref_put(&rdata->kref, lport->tt.rport_destroy);
292 		break;
293 
294 	case RPORT_EV_FAILED:
295 	case RPORT_EV_LOGO:
296 	case RPORT_EV_STOP:
297 		port_id = rdata->ids.port_id;
298 		mutex_unlock(&rdata->rp_mutex);
299 
300 		if (port_id != FC_FID_DIR_SERV) {
301 			/*
302 			 * We must drop rp_mutex before taking disc_mutex.
303 			 * Re-evaluate state to allow for restart.
304 			 * A transition to RESTART state must only happen
305 			 * while disc_mutex is held and rdata is on the list.
306 			 */
307 			mutex_lock(&lport->disc.disc_mutex);
308 			mutex_lock(&rdata->rp_mutex);
309 			if (rdata->rp_state == RPORT_ST_RESTART)
310 				restart = 1;
311 			else
312 				list_del(&rdata->peers);
313 			mutex_unlock(&rdata->rp_mutex);
314 			mutex_unlock(&lport->disc.disc_mutex);
315 		}
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 		cancel_delayed_work_sync(&rdata->retry_work);
322 
323 		/*
324 		 * Reset any outstanding exchanges before freeing rport.
325 		 */
326 		lport->tt.exch_mgr_reset(lport, 0, port_id);
327 		lport->tt.exch_mgr_reset(lport, port_id, 0);
328 
329 		if (rport) {
330 			rpriv = rport->dd_data;
331 			rpriv->rp_state = RPORT_ST_DELETE;
332 			mutex_lock(&rdata->rp_mutex);
333 			rdata->rport = NULL;
334 			mutex_unlock(&rdata->rp_mutex);
335 			fc_remote_port_delete(rport);
336 		}
337 		if (restart) {
338 			mutex_lock(&rdata->rp_mutex);
339 			FC_RPORT_DBG(rdata, "work restart\n");
340 			fc_rport_enter_plogi(rdata);
341 			mutex_unlock(&rdata->rp_mutex);
342 		} else
343 			kref_put(&rdata->kref, lport->tt.rport_destroy);
344 		break;
345 
346 	default:
347 		mutex_unlock(&rdata->rp_mutex);
348 		break;
349 	}
350 }
351 
352 /**
353  * fc_rport_login() - Start the remote port login state machine
354  * @rdata: The remote port to be logged in to
355  *
356  * Locking Note: Called without the rport lock held. This
357  * function will hold the rport lock, call an _enter_*
358  * function and then unlock the rport.
359  *
360  * This indicates the intent to be logged into the remote port.
361  * If it appears we are already logged in, ADISC is used to verify
362  * the setup.
363  */
364 int fc_rport_login(struct fc_rport_priv *rdata)
365 {
366 	mutex_lock(&rdata->rp_mutex);
367 
368 	switch (rdata->rp_state) {
369 	case RPORT_ST_READY:
370 		FC_RPORT_DBG(rdata, "ADISC port\n");
371 		fc_rport_enter_adisc(rdata);
372 		break;
373 	case RPORT_ST_RESTART:
374 		break;
375 	case RPORT_ST_DELETE:
376 		FC_RPORT_DBG(rdata, "Restart deleted port\n");
377 		fc_rport_state_enter(rdata, RPORT_ST_RESTART);
378 		break;
379 	default:
380 		FC_RPORT_DBG(rdata, "Login to port\n");
381 		fc_rport_enter_plogi(rdata);
382 		break;
383 	}
384 	mutex_unlock(&rdata->rp_mutex);
385 
386 	return 0;
387 }
388 
389 /**
390  * fc_rport_enter_delete() - Schedule a remote port to be deleted
391  * @rdata: The remote port to be deleted
392  * @event: The event to report as the reason for deletion
393  *
394  * Locking Note: Called with the rport lock held.
395  *
396  * Allow state change into DELETE only once.
397  *
398  * Call queue_work only if there's no event already pending.
399  * Set the new event so that the old pending event will not occur.
400  * Since we have the mutex, even if fc_rport_work() is already started,
401  * it'll see the new event.
402  */
403 static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
404 				  enum fc_rport_event event)
405 {
406 	if (rdata->rp_state == RPORT_ST_DELETE)
407 		return;
408 
409 	FC_RPORT_DBG(rdata, "Delete port\n");
410 
411 	fc_rport_state_enter(rdata, RPORT_ST_DELETE);
412 
413 	if (rdata->event == RPORT_EV_NONE)
414 		queue_work(rport_event_queue, &rdata->event_work);
415 	rdata->event = event;
416 }
417 
418 /**
419  * fc_rport_logoff() - Logoff and remove a remote port
420  * @rdata: The remote port to be logged off of
421  *
422  * Locking Note: Called without the rport lock held. This
423  * function will hold the rport lock, call an _enter_*
424  * function and then unlock the rport.
425  */
426 int fc_rport_logoff(struct fc_rport_priv *rdata)
427 {
428 	mutex_lock(&rdata->rp_mutex);
429 
430 	FC_RPORT_DBG(rdata, "Remove port\n");
431 
432 	if (rdata->rp_state == RPORT_ST_DELETE) {
433 		FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
434 		goto out;
435 	}
436 
437 	if (rdata->rp_state == RPORT_ST_RESTART)
438 		FC_RPORT_DBG(rdata, "Port in Restart state, deleting\n");
439 	else
440 		fc_rport_enter_logo(rdata);
441 
442 	/*
443 	 * Change the state to Delete so that we discard
444 	 * the response.
445 	 */
446 	fc_rport_enter_delete(rdata, RPORT_EV_STOP);
447 out:
448 	mutex_unlock(&rdata->rp_mutex);
449 	return 0;
450 }
451 
452 /**
453  * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state
454  * @rdata: The remote port that is ready
455  *
456  * Locking Note: The rport lock is expected to be held before calling
457  * this routine.
458  */
459 static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
460 {
461 	fc_rport_state_enter(rdata, RPORT_ST_READY);
462 
463 	FC_RPORT_DBG(rdata, "Port is Ready\n");
464 
465 	if (rdata->event == RPORT_EV_NONE)
466 		queue_work(rport_event_queue, &rdata->event_work);
467 	rdata->event = RPORT_EV_READY;
468 }
469 
470 /**
471  * fc_rport_timeout() - Handler for the retry_work timer
472  * @work: Handle to the remote port that has timed out
473  *
474  * Locking Note: Called without the rport lock held. This
475  * function will hold the rport lock, call an _enter_*
476  * function and then unlock the rport.
477  */
478 static void fc_rport_timeout(struct work_struct *work)
479 {
480 	struct fc_rport_priv *rdata =
481 		container_of(work, struct fc_rport_priv, retry_work.work);
482 
483 	mutex_lock(&rdata->rp_mutex);
484 
485 	switch (rdata->rp_state) {
486 	case RPORT_ST_PLOGI:
487 		fc_rport_enter_plogi(rdata);
488 		break;
489 	case RPORT_ST_PRLI:
490 		fc_rport_enter_prli(rdata);
491 		break;
492 	case RPORT_ST_RTV:
493 		fc_rport_enter_rtv(rdata);
494 		break;
495 	case RPORT_ST_LOGO:
496 		fc_rport_enter_logo(rdata);
497 		break;
498 	case RPORT_ST_ADISC:
499 		fc_rport_enter_adisc(rdata);
500 		break;
501 	case RPORT_ST_READY:
502 	case RPORT_ST_INIT:
503 	case RPORT_ST_DELETE:
504 	case RPORT_ST_RESTART:
505 		break;
506 	}
507 
508 	mutex_unlock(&rdata->rp_mutex);
509 }
510 
511 /**
512  * fc_rport_error() - Error handler, called once retries have been exhausted
513  * @rdata: The remote port the error is happened on
514  * @fp:	   The error code encapsulated in a frame pointer
515  *
516  * Locking Note: The rport lock is expected to be held before
517  * calling this routine
518  */
519 static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
520 {
521 	FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
522 		     IS_ERR(fp) ? -PTR_ERR(fp) : 0,
523 		     fc_rport_state(rdata), rdata->retries);
524 
525 	switch (rdata->rp_state) {
526 	case RPORT_ST_PLOGI:
527 	case RPORT_ST_LOGO:
528 		fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
529 		break;
530 	case RPORT_ST_RTV:
531 		fc_rport_enter_ready(rdata);
532 		break;
533 	case RPORT_ST_PRLI:
534 	case RPORT_ST_ADISC:
535 		fc_rport_enter_logo(rdata);
536 		break;
537 	case RPORT_ST_DELETE:
538 	case RPORT_ST_RESTART:
539 	case RPORT_ST_READY:
540 	case RPORT_ST_INIT:
541 		break;
542 	}
543 }
544 
545 /**
546  * fc_rport_error_retry() - Handler for remote port state retries
547  * @rdata: The remote port whose state is to be retried
548  * @fp:	   The error code encapsulated in a frame pointer
549  *
550  * If the error was an exchange timeout retry immediately,
551  * otherwise wait for E_D_TOV.
552  *
553  * Locking Note: The rport lock is expected to be held before
554  * calling this routine
555  */
556 static void fc_rport_error_retry(struct fc_rport_priv *rdata,
557 				 struct fc_frame *fp)
558 {
559 	unsigned long delay = FC_DEF_E_D_TOV;
560 
561 	/* make sure this isn't an FC_EX_CLOSED error, never retry those */
562 	if (PTR_ERR(fp) == -FC_EX_CLOSED)
563 		return fc_rport_error(rdata, fp);
564 
565 	if (rdata->retries < rdata->local_port->max_rport_retry_count) {
566 		FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
567 			     PTR_ERR(fp), fc_rport_state(rdata));
568 		rdata->retries++;
569 		/* no additional delay on exchange timeouts */
570 		if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
571 			delay = 0;
572 		schedule_delayed_work(&rdata->retry_work, delay);
573 		return;
574 	}
575 
576 	return fc_rport_error(rdata, fp);
577 }
578 
579 /**
580  * fc_rport_plogi_recv_resp() - Handler for ELS PLOGI responses
581  * @sp:	       The sequence the PLOGI is on
582  * @fp:	       The PLOGI response frame
583  * @rdata_arg: The remote port that sent the PLOGI response
584  *
585  * Locking Note: This function will be called without the rport lock
586  * held, but it will lock, call an _enter_* function or fc_rport_error
587  * and then unlock the rport.
588  */
589 static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
590 				void *rdata_arg)
591 {
592 	struct fc_rport_priv *rdata = rdata_arg;
593 	struct fc_lport *lport = rdata->local_port;
594 	struct fc_els_flogi *plp = NULL;
595 	unsigned int tov;
596 	u16 csp_seq;
597 	u16 cssp_seq;
598 	u8 op;
599 
600 	mutex_lock(&rdata->rp_mutex);
601 
602 	FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
603 
604 	if (rdata->rp_state != RPORT_ST_PLOGI) {
605 		FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
606 			     "%s\n", fc_rport_state(rdata));
607 		if (IS_ERR(fp))
608 			goto err;
609 		goto out;
610 	}
611 
612 	if (IS_ERR(fp)) {
613 		fc_rport_error_retry(rdata, fp);
614 		goto err;
615 	}
616 
617 	op = fc_frame_payload_op(fp);
618 	if (op == ELS_LS_ACC &&
619 	    (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
620 		rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
621 		rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
622 
623 		tov = ntohl(plp->fl_csp.sp_e_d_tov);
624 		if (ntohs(plp->fl_csp.sp_features) & FC_SP_FT_EDTR)
625 			tov /= 1000;
626 		if (tov > rdata->e_d_tov)
627 			rdata->e_d_tov = tov;
628 		csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
629 		cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
630 		if (cssp_seq < csp_seq)
631 			csp_seq = cssp_seq;
632 		rdata->max_seq = csp_seq;
633 		rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
634 		fc_rport_enter_prli(rdata);
635 	} else
636 		fc_rport_error_retry(rdata, fp);
637 
638 out:
639 	fc_frame_free(fp);
640 err:
641 	mutex_unlock(&rdata->rp_mutex);
642 	kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
643 }
644 
645 /**
646  * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
647  * @rdata: The remote port to send a PLOGI to
648  *
649  * Locking Note: The rport lock is expected to be held before calling
650  * this routine.
651  */
652 static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
653 {
654 	struct fc_lport *lport = rdata->local_port;
655 	struct fc_frame *fp;
656 
657 	FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
658 		     fc_rport_state(rdata));
659 
660 	fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
661 
662 	rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
663 	fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
664 	if (!fp) {
665 		fc_rport_error_retry(rdata, fp);
666 		return;
667 	}
668 	rdata->e_d_tov = lport->e_d_tov;
669 
670 	if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
671 				  fc_rport_plogi_resp, rdata,
672 				  2 * lport->r_a_tov))
673 		fc_rport_error_retry(rdata, NULL);
674 	else
675 		kref_get(&rdata->kref);
676 }
677 
678 /**
679  * fc_rport_prli_resp() - Process Login (PRLI) response handler
680  * @sp:	       The sequence the PRLI response was on
681  * @fp:	       The PRLI response frame
682  * @rdata_arg: The remote port that sent the PRLI response
683  *
684  * Locking Note: This function will be called without the rport lock
685  * held, but it will lock, call an _enter_* function or fc_rport_error
686  * and then unlock the rport.
687  */
688 static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
689 			       void *rdata_arg)
690 {
691 	struct fc_rport_priv *rdata = rdata_arg;
692 	struct {
693 		struct fc_els_prli prli;
694 		struct fc_els_spp spp;
695 	} *pp;
696 	u32 roles = FC_RPORT_ROLE_UNKNOWN;
697 	u32 fcp_parm = 0;
698 	u8 op;
699 
700 	mutex_lock(&rdata->rp_mutex);
701 
702 	FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
703 
704 	if (rdata->rp_state != RPORT_ST_PRLI) {
705 		FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
706 			     "%s\n", fc_rport_state(rdata));
707 		if (IS_ERR(fp))
708 			goto err;
709 		goto out;
710 	}
711 
712 	if (IS_ERR(fp)) {
713 		fc_rport_error_retry(rdata, fp);
714 		goto err;
715 	}
716 
717 	/* reinitialize remote port roles */
718 	rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
719 
720 	op = fc_frame_payload_op(fp);
721 	if (op == ELS_LS_ACC) {
722 		pp = fc_frame_payload_get(fp, sizeof(*pp));
723 		if (pp && pp->prli.prli_spp_len >= sizeof(pp->spp)) {
724 			fcp_parm = ntohl(pp->spp.spp_params);
725 			if (fcp_parm & FCP_SPPF_RETRY)
726 				rdata->flags |= FC_RP_FLAGS_RETRY;
727 		}
728 
729 		rdata->supported_classes = FC_COS_CLASS3;
730 		if (fcp_parm & FCP_SPPF_INIT_FCN)
731 			roles |= FC_RPORT_ROLE_FCP_INITIATOR;
732 		if (fcp_parm & FCP_SPPF_TARG_FCN)
733 			roles |= FC_RPORT_ROLE_FCP_TARGET;
734 
735 		rdata->ids.roles = roles;
736 		fc_rport_enter_rtv(rdata);
737 
738 	} else {
739 		FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
740 		fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
741 	}
742 
743 out:
744 	fc_frame_free(fp);
745 err:
746 	mutex_unlock(&rdata->rp_mutex);
747 	kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
748 }
749 
750 /**
751  * fc_rport_logo_resp() - Handler for logout (LOGO) responses
752  * @sp:	       The sequence the LOGO was on
753  * @fp:	       The LOGO response frame
754  * @rdata_arg: The remote port that sent the LOGO response
755  *
756  * Locking Note: This function will be called without the rport lock
757  * held, but it will lock, call an _enter_* function or fc_rport_error
758  * and then unlock the rport.
759  */
760 static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
761 			       void *rdata_arg)
762 {
763 	struct fc_rport_priv *rdata = rdata_arg;
764 	u8 op;
765 
766 	mutex_lock(&rdata->rp_mutex);
767 
768 	FC_RPORT_DBG(rdata, "Received a LOGO %s\n", fc_els_resp_type(fp));
769 
770 	if (rdata->rp_state != RPORT_ST_LOGO) {
771 		FC_RPORT_DBG(rdata, "Received a LOGO response, but in state "
772 			     "%s\n", fc_rport_state(rdata));
773 		if (IS_ERR(fp))
774 			goto err;
775 		goto out;
776 	}
777 
778 	if (IS_ERR(fp)) {
779 		fc_rport_error_retry(rdata, fp);
780 		goto err;
781 	}
782 
783 	op = fc_frame_payload_op(fp);
784 	if (op != ELS_LS_ACC)
785 		FC_RPORT_DBG(rdata, "Bad ELS response op %x for LOGO command\n",
786 			     op);
787 	fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
788 
789 out:
790 	fc_frame_free(fp);
791 err:
792 	mutex_unlock(&rdata->rp_mutex);
793 	kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
794 }
795 
796 /**
797  * fc_rport_enter_prli() - Send Process Login (PRLI) request
798  * @rdata: The remote port to send the PRLI request to
799  *
800  * Locking Note: The rport lock is expected to be held before calling
801  * this routine.
802  */
803 static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
804 {
805 	struct fc_lport *lport = rdata->local_port;
806 	struct {
807 		struct fc_els_prli prli;
808 		struct fc_els_spp spp;
809 	} *pp;
810 	struct fc_frame *fp;
811 
812 	/*
813 	 * If the rport is one of the well known addresses
814 	 * we skip PRLI and RTV and go straight to READY.
815 	 */
816 	if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
817 		fc_rport_enter_ready(rdata);
818 		return;
819 	}
820 
821 	FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
822 		     fc_rport_state(rdata));
823 
824 	fc_rport_state_enter(rdata, RPORT_ST_PRLI);
825 
826 	fp = fc_frame_alloc(lport, sizeof(*pp));
827 	if (!fp) {
828 		fc_rport_error_retry(rdata, fp);
829 		return;
830 	}
831 
832 	if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PRLI,
833 				  fc_rport_prli_resp, rdata,
834 				  2 * lport->r_a_tov))
835 		fc_rport_error_retry(rdata, NULL);
836 	else
837 		kref_get(&rdata->kref);
838 }
839 
840 /**
841  * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses
842  * @sp:	       The sequence the RTV was on
843  * @fp:	       The RTV response frame
844  * @rdata_arg: The remote port that sent the RTV response
845  *
846  * Many targets don't seem to support this.
847  *
848  * Locking Note: This function will be called without the rport lock
849  * held, but it will lock, call an _enter_* function or fc_rport_error
850  * and then unlock the rport.
851  */
852 static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
853 			      void *rdata_arg)
854 {
855 	struct fc_rport_priv *rdata = rdata_arg;
856 	u8 op;
857 
858 	mutex_lock(&rdata->rp_mutex);
859 
860 	FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
861 
862 	if (rdata->rp_state != RPORT_ST_RTV) {
863 		FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
864 			     "%s\n", fc_rport_state(rdata));
865 		if (IS_ERR(fp))
866 			goto err;
867 		goto out;
868 	}
869 
870 	if (IS_ERR(fp)) {
871 		fc_rport_error(rdata, fp);
872 		goto err;
873 	}
874 
875 	op = fc_frame_payload_op(fp);
876 	if (op == ELS_LS_ACC) {
877 		struct fc_els_rtv_acc *rtv;
878 		u32 toq;
879 		u32 tov;
880 
881 		rtv = fc_frame_payload_get(fp, sizeof(*rtv));
882 		if (rtv) {
883 			toq = ntohl(rtv->rtv_toq);
884 			tov = ntohl(rtv->rtv_r_a_tov);
885 			if (tov == 0)
886 				tov = 1;
887 			rdata->r_a_tov = tov;
888 			tov = ntohl(rtv->rtv_e_d_tov);
889 			if (toq & FC_ELS_RTV_EDRES)
890 				tov /= 1000000;
891 			if (tov == 0)
892 				tov = 1;
893 			rdata->e_d_tov = tov;
894 		}
895 	}
896 
897 	fc_rport_enter_ready(rdata);
898 
899 out:
900 	fc_frame_free(fp);
901 err:
902 	mutex_unlock(&rdata->rp_mutex);
903 	kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
904 }
905 
906 /**
907  * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
908  * @rdata: The remote port to send the RTV request to
909  *
910  * Locking Note: The rport lock is expected to be held before calling
911  * this routine.
912  */
913 static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
914 {
915 	struct fc_frame *fp;
916 	struct fc_lport *lport = rdata->local_port;
917 
918 	FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
919 		     fc_rport_state(rdata));
920 
921 	fc_rport_state_enter(rdata, RPORT_ST_RTV);
922 
923 	fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
924 	if (!fp) {
925 		fc_rport_error_retry(rdata, fp);
926 		return;
927 	}
928 
929 	if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
930 				  fc_rport_rtv_resp, rdata,
931 				  2 * lport->r_a_tov))
932 		fc_rport_error_retry(rdata, NULL);
933 	else
934 		kref_get(&rdata->kref);
935 }
936 
937 /**
938  * fc_rport_enter_logo() - Send a logout (LOGO) request
939  * @rdata: The remote port to send the LOGO request to
940  *
941  * Locking Note: The rport lock is expected to be held before calling
942  * this routine.
943  */
944 static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
945 {
946 	struct fc_lport *lport = rdata->local_port;
947 	struct fc_frame *fp;
948 
949 	FC_RPORT_DBG(rdata, "Port entered LOGO state from %s state\n",
950 		     fc_rport_state(rdata));
951 
952 	fc_rport_state_enter(rdata, RPORT_ST_LOGO);
953 
954 	fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
955 	if (!fp) {
956 		fc_rport_error_retry(rdata, fp);
957 		return;
958 	}
959 
960 	if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
961 				  fc_rport_logo_resp, rdata,
962 				  2 * lport->r_a_tov))
963 		fc_rport_error_retry(rdata, NULL);
964 	else
965 		kref_get(&rdata->kref);
966 }
967 
968 /**
969  * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
970  * @sp:	       The sequence the ADISC response was on
971  * @fp:	       The ADISC response frame
972  * @rdata_arg: The remote port that sent the ADISC response
973  *
974  * Locking Note: This function will be called without the rport lock
975  * held, but it will lock, call an _enter_* function or fc_rport_error
976  * and then unlock the rport.
977  */
978 static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
979 				void *rdata_arg)
980 {
981 	struct fc_rport_priv *rdata = rdata_arg;
982 	struct fc_els_adisc *adisc;
983 	u8 op;
984 
985 	mutex_lock(&rdata->rp_mutex);
986 
987 	FC_RPORT_DBG(rdata, "Received a ADISC response\n");
988 
989 	if (rdata->rp_state != RPORT_ST_ADISC) {
990 		FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
991 			     fc_rport_state(rdata));
992 		if (IS_ERR(fp))
993 			goto err;
994 		goto out;
995 	}
996 
997 	if (IS_ERR(fp)) {
998 		fc_rport_error(rdata, fp);
999 		goto err;
1000 	}
1001 
1002 	/*
1003 	 * If address verification failed.  Consider us logged out of the rport.
1004 	 * Since the rport is still in discovery, we want to be
1005 	 * logged in, so go to PLOGI state.  Otherwise, go back to READY.
1006 	 */
1007 	op = fc_frame_payload_op(fp);
1008 	adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1009 	if (op != ELS_LS_ACC || !adisc ||
1010 	    ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
1011 	    get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
1012 	    get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
1013 		FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
1014 		fc_rport_enter_plogi(rdata);
1015 	} else {
1016 		FC_RPORT_DBG(rdata, "ADISC OK\n");
1017 		fc_rport_enter_ready(rdata);
1018 	}
1019 out:
1020 	fc_frame_free(fp);
1021 err:
1022 	mutex_unlock(&rdata->rp_mutex);
1023 	kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1024 }
1025 
1026 /**
1027  * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
1028  * @rdata: The remote port to send the ADISC request to
1029  *
1030  * Locking Note: The rport lock is expected to be held before calling
1031  * this routine.
1032  */
1033 static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
1034 {
1035 	struct fc_lport *lport = rdata->local_port;
1036 	struct fc_frame *fp;
1037 
1038 	FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
1039 		     fc_rport_state(rdata));
1040 
1041 	fc_rport_state_enter(rdata, RPORT_ST_ADISC);
1042 
1043 	fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
1044 	if (!fp) {
1045 		fc_rport_error_retry(rdata, fp);
1046 		return;
1047 	}
1048 	if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
1049 				  fc_rport_adisc_resp, rdata,
1050 				  2 * lport->r_a_tov))
1051 		fc_rport_error_retry(rdata, NULL);
1052 	else
1053 		kref_get(&rdata->kref);
1054 }
1055 
1056 /**
1057  * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
1058  * @rdata: The remote port that sent the ADISC request
1059  * @sp:	   The sequence the ADISC request was on
1060  * @in_fp: The ADISC request frame
1061  *
1062  * Locking Note:  Called with the lport and rport locks held.
1063  */
1064 static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
1065 				    struct fc_seq *sp, struct fc_frame *in_fp)
1066 {
1067 	struct fc_lport *lport = rdata->local_port;
1068 	struct fc_frame *fp;
1069 	struct fc_exch *ep = fc_seq_exch(sp);
1070 	struct fc_els_adisc *adisc;
1071 	struct fc_seq_els_data rjt_data;
1072 	u32 f_ctl;
1073 
1074 	FC_RPORT_DBG(rdata, "Received ADISC request\n");
1075 
1076 	adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
1077 	if (!adisc) {
1078 		rjt_data.fp = NULL;
1079 		rjt_data.reason = ELS_RJT_PROT;
1080 		rjt_data.explan = ELS_EXPL_INV_LEN;
1081 		lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1082 		goto drop;
1083 	}
1084 
1085 	fp = fc_frame_alloc(lport, sizeof(*adisc));
1086 	if (!fp)
1087 		goto drop;
1088 	fc_adisc_fill(lport, fp);
1089 	adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1090 	adisc->adisc_cmd = ELS_LS_ACC;
1091 	sp = lport->tt.seq_start_next(sp);
1092 	f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1093 	fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1094 		       FC_TYPE_ELS, f_ctl, 0);
1095 	lport->tt.seq_send(lport, sp, fp);
1096 drop:
1097 	fc_frame_free(in_fp);
1098 }
1099 
1100 /**
1101  * fc_rport_recv_els_req() - Handler for validated ELS requests
1102  * @lport: The local port that received the ELS request
1103  * @sp:	   The sequence that the ELS request was on
1104  * @fp:	   The ELS request frame
1105  *
1106  * Handle incoming ELS requests that require port login.
1107  * The ELS opcode has already been validated by the caller.
1108  *
1109  * Locking Note: Called with the lport lock held.
1110  */
1111 static void fc_rport_recv_els_req(struct fc_lport *lport,
1112 				  struct fc_seq *sp, struct fc_frame *fp)
1113 {
1114 	struct fc_rport_priv *rdata;
1115 	struct fc_frame_header *fh;
1116 	struct fc_seq_els_data els_data;
1117 
1118 	els_data.fp = NULL;
1119 	els_data.reason = ELS_RJT_UNAB;
1120 	els_data.explan = ELS_EXPL_PLOGI_REQD;
1121 
1122 	fh = fc_frame_header_get(fp);
1123 
1124 	mutex_lock(&lport->disc.disc_mutex);
1125 	rdata = lport->tt.rport_lookup(lport, ntoh24(fh->fh_s_id));
1126 	if (!rdata) {
1127 		mutex_unlock(&lport->disc.disc_mutex);
1128 		goto reject;
1129 	}
1130 	mutex_lock(&rdata->rp_mutex);
1131 	mutex_unlock(&lport->disc.disc_mutex);
1132 
1133 	switch (rdata->rp_state) {
1134 	case RPORT_ST_PRLI:
1135 	case RPORT_ST_RTV:
1136 	case RPORT_ST_READY:
1137 	case RPORT_ST_ADISC:
1138 		break;
1139 	default:
1140 		mutex_unlock(&rdata->rp_mutex);
1141 		goto reject;
1142 	}
1143 
1144 	switch (fc_frame_payload_op(fp)) {
1145 	case ELS_PRLI:
1146 		fc_rport_recv_prli_req(rdata, sp, fp);
1147 		break;
1148 	case ELS_PRLO:
1149 		fc_rport_recv_prlo_req(rdata, sp, fp);
1150 		break;
1151 	case ELS_ADISC:
1152 		fc_rport_recv_adisc_req(rdata, sp, fp);
1153 		break;
1154 	case ELS_RRQ:
1155 		els_data.fp = fp;
1156 		lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
1157 		break;
1158 	case ELS_REC:
1159 		els_data.fp = fp;
1160 		lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
1161 		break;
1162 	default:
1163 		fc_frame_free(fp);	/* can't happen */
1164 		break;
1165 	}
1166 
1167 	mutex_unlock(&rdata->rp_mutex);
1168 	return;
1169 
1170 reject:
1171 	lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
1172 	fc_frame_free(fp);
1173 }
1174 
1175 /**
1176  * fc_rport_recv_req() - Handler for requests
1177  * @sp:	   The sequence the request was on
1178  * @fp:	   The request frame
1179  * @lport: The local port that received the request
1180  *
1181  * Locking Note: Called with the lport lock held.
1182  */
1183 void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
1184 		       struct fc_lport *lport)
1185 {
1186 	struct fc_seq_els_data els_data;
1187 
1188 	/*
1189 	 * Handle PLOGI and LOGO requests separately, since they
1190 	 * don't require prior login.
1191 	 * Check for unsupported opcodes first and reject them.
1192 	 * For some ops, it would be incorrect to reject with "PLOGI required".
1193 	 */
1194 	switch (fc_frame_payload_op(fp)) {
1195 	case ELS_PLOGI:
1196 		fc_rport_recv_plogi_req(lport, sp, fp);
1197 		break;
1198 	case ELS_LOGO:
1199 		fc_rport_recv_logo_req(lport, sp, fp);
1200 		break;
1201 	case ELS_PRLI:
1202 	case ELS_PRLO:
1203 	case ELS_ADISC:
1204 	case ELS_RRQ:
1205 	case ELS_REC:
1206 		fc_rport_recv_els_req(lport, sp, fp);
1207 		break;
1208 	default:
1209 		fc_frame_free(fp);
1210 		els_data.fp = NULL;
1211 		els_data.reason = ELS_RJT_UNSUP;
1212 		els_data.explan = ELS_EXPL_NONE;
1213 		lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
1214 		break;
1215 	}
1216 }
1217 
1218 /**
1219  * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
1220  * @lport: The local port that received the PLOGI request
1221  * @sp:	   The sequence that the PLOGI request was on
1222  * @rx_fp: The PLOGI request frame
1223  *
1224  * Locking Note: The rport lock is held before calling this function.
1225  */
1226 static void fc_rport_recv_plogi_req(struct fc_lport *lport,
1227 				    struct fc_seq *sp, struct fc_frame *rx_fp)
1228 {
1229 	struct fc_disc *disc;
1230 	struct fc_rport_priv *rdata;
1231 	struct fc_frame *fp = rx_fp;
1232 	struct fc_exch *ep;
1233 	struct fc_frame_header *fh;
1234 	struct fc_els_flogi *pl;
1235 	struct fc_seq_els_data rjt_data;
1236 	u32 sid, f_ctl;
1237 
1238 	rjt_data.fp = NULL;
1239 	fh = fc_frame_header_get(fp);
1240 	sid = ntoh24(fh->fh_s_id);
1241 
1242 	FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
1243 
1244 	pl = fc_frame_payload_get(fp, sizeof(*pl));
1245 	if (!pl) {
1246 		FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
1247 		rjt_data.reason = ELS_RJT_PROT;
1248 		rjt_data.explan = ELS_EXPL_INV_LEN;
1249 		goto reject;
1250 	}
1251 
1252 	disc = &lport->disc;
1253 	mutex_lock(&disc->disc_mutex);
1254 	rdata = lport->tt.rport_create(lport, sid);
1255 	if (!rdata) {
1256 		mutex_unlock(&disc->disc_mutex);
1257 		rjt_data.reason = ELS_RJT_UNAB;
1258 		rjt_data.explan = ELS_EXPL_INSUF_RES;
1259 		goto reject;
1260 	}
1261 
1262 	mutex_lock(&rdata->rp_mutex);
1263 	mutex_unlock(&disc->disc_mutex);
1264 
1265 	rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1266 	rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
1267 
1268 	/*
1269 	 * If the rport was just created, possibly due to the incoming PLOGI,
1270 	 * set the state appropriately and accept the PLOGI.
1271 	 *
1272 	 * If we had also sent a PLOGI, and if the received PLOGI is from a
1273 	 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1274 	 * "command already in progress".
1275 	 *
1276 	 * XXX TBD: If the session was ready before, the PLOGI should result in
1277 	 * all outstanding exchanges being reset.
1278 	 */
1279 	switch (rdata->rp_state) {
1280 	case RPORT_ST_INIT:
1281 		FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
1282 		break;
1283 	case RPORT_ST_PLOGI:
1284 		FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1285 		if (rdata->ids.port_name < lport->wwpn) {
1286 			mutex_unlock(&rdata->rp_mutex);
1287 			rjt_data.reason = ELS_RJT_INPROG;
1288 			rjt_data.explan = ELS_EXPL_NONE;
1289 			goto reject;
1290 		}
1291 		break;
1292 	case RPORT_ST_PRLI:
1293 	case RPORT_ST_RTV:
1294 	case RPORT_ST_READY:
1295 	case RPORT_ST_ADISC:
1296 		FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
1297 			     "- ignored for now\n", rdata->rp_state);
1298 		/* XXX TBD - should reset */
1299 		break;
1300 	case RPORT_ST_DELETE:
1301 	case RPORT_ST_LOGO:
1302 	case RPORT_ST_RESTART:
1303 		FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
1304 			     fc_rport_state(rdata));
1305 		mutex_unlock(&rdata->rp_mutex);
1306 		rjt_data.reason = ELS_RJT_BUSY;
1307 		rjt_data.explan = ELS_EXPL_NONE;
1308 		goto reject;
1309 	}
1310 
1311 	/*
1312 	 * Get session payload size from incoming PLOGI.
1313 	 */
1314 	rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
1315 	fc_frame_free(rx_fp);
1316 
1317 	/*
1318 	 * Send LS_ACC.	 If this fails, the originator should retry.
1319 	 */
1320 	sp = lport->tt.seq_start_next(sp);
1321 	if (!sp)
1322 		goto out;
1323 	fp = fc_frame_alloc(lport, sizeof(*pl));
1324 	if (!fp)
1325 		goto out;
1326 
1327 	fc_plogi_fill(lport, fp, ELS_LS_ACC);
1328 	f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1329 	ep = fc_seq_exch(sp);
1330 	fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1331 		       FC_TYPE_ELS, f_ctl, 0);
1332 	lport->tt.seq_send(lport, sp, fp);
1333 	fc_rport_enter_prli(rdata);
1334 out:
1335 	mutex_unlock(&rdata->rp_mutex);
1336 	return;
1337 
1338 reject:
1339 	lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1340 	fc_frame_free(fp);
1341 }
1342 
1343 /**
1344  * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
1345  * @rdata: The remote port that sent the PRLI request
1346  * @sp:	   The sequence that the PRLI was on
1347  * @rx_fp: The PRLI request frame
1348  *
1349  * Locking Note: The rport lock is exected to be held before calling
1350  * this function.
1351  */
1352 static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
1353 				   struct fc_seq *sp, struct fc_frame *rx_fp)
1354 {
1355 	struct fc_lport *lport = rdata->local_port;
1356 	struct fc_exch *ep;
1357 	struct fc_frame *fp;
1358 	struct fc_frame_header *fh;
1359 	struct {
1360 		struct fc_els_prli prli;
1361 		struct fc_els_spp spp;
1362 	} *pp;
1363 	struct fc_els_spp *rspp;	/* request service param page */
1364 	struct fc_els_spp *spp;	/* response spp */
1365 	unsigned int len;
1366 	unsigned int plen;
1367 	enum fc_els_rjt_reason reason = ELS_RJT_UNAB;
1368 	enum fc_els_rjt_explan explan = ELS_EXPL_NONE;
1369 	enum fc_els_spp_resp resp;
1370 	struct fc_seq_els_data rjt_data;
1371 	u32 f_ctl;
1372 	u32 fcp_parm;
1373 	u32 roles = FC_RPORT_ROLE_UNKNOWN;
1374 	rjt_data.fp = NULL;
1375 
1376 	fh = fc_frame_header_get(rx_fp);
1377 
1378 	FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1379 		     fc_rport_state(rdata));
1380 
1381 	switch (rdata->rp_state) {
1382 	case RPORT_ST_PRLI:
1383 	case RPORT_ST_RTV:
1384 	case RPORT_ST_READY:
1385 	case RPORT_ST_ADISC:
1386 		reason = ELS_RJT_NONE;
1387 		break;
1388 	default:
1389 		fc_frame_free(rx_fp);
1390 		return;
1391 		break;
1392 	}
1393 	len = fr_len(rx_fp) - sizeof(*fh);
1394 	pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1395 	if (pp == NULL) {
1396 		reason = ELS_RJT_PROT;
1397 		explan = ELS_EXPL_INV_LEN;
1398 	} else {
1399 		plen = ntohs(pp->prli.prli_len);
1400 		if ((plen % 4) != 0 || plen > len) {
1401 			reason = ELS_RJT_PROT;
1402 			explan = ELS_EXPL_INV_LEN;
1403 		} else if (plen < len) {
1404 			len = plen;
1405 		}
1406 		plen = pp->prli.prli_spp_len;
1407 		if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1408 		    plen > len || len < sizeof(*pp)) {
1409 			reason = ELS_RJT_PROT;
1410 			explan = ELS_EXPL_INV_LEN;
1411 		}
1412 		rspp = &pp->spp;
1413 	}
1414 	if (reason != ELS_RJT_NONE ||
1415 	    (fp = fc_frame_alloc(lport, len)) == NULL) {
1416 		rjt_data.reason = reason;
1417 		rjt_data.explan = explan;
1418 		lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1419 	} else {
1420 		sp = lport->tt.seq_start_next(sp);
1421 		WARN_ON(!sp);
1422 		pp = fc_frame_payload_get(fp, len);
1423 		WARN_ON(!pp);
1424 		memset(pp, 0, len);
1425 		pp->prli.prli_cmd = ELS_LS_ACC;
1426 		pp->prli.prli_spp_len = plen;
1427 		pp->prli.prli_len = htons(len);
1428 		len -= sizeof(struct fc_els_prli);
1429 
1430 		/* reinitialize remote port roles */
1431 		rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
1432 
1433 		/*
1434 		 * Go through all the service parameter pages and build
1435 		 * response.  If plen indicates longer SPP than standard,
1436 		 * use that.  The entire response has been pre-cleared above.
1437 		 */
1438 		spp = &pp->spp;
1439 		while (len >= plen) {
1440 			spp->spp_type = rspp->spp_type;
1441 			spp->spp_type_ext = rspp->spp_type_ext;
1442 			spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
1443 			resp = FC_SPP_RESP_ACK;
1444 			if (rspp->spp_flags & FC_SPP_RPA_VAL)
1445 				resp = FC_SPP_RESP_NO_PA;
1446 			switch (rspp->spp_type) {
1447 			case 0:	/* common to all FC-4 types */
1448 				break;
1449 			case FC_TYPE_FCP:
1450 				fcp_parm = ntohl(rspp->spp_params);
1451 				if (fcp_parm & FCP_SPPF_RETRY)
1452 					rdata->flags |= FC_RP_FLAGS_RETRY;
1453 				rdata->supported_classes = FC_COS_CLASS3;
1454 				if (fcp_parm & FCP_SPPF_INIT_FCN)
1455 					roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1456 				if (fcp_parm & FCP_SPPF_TARG_FCN)
1457 					roles |= FC_RPORT_ROLE_FCP_TARGET;
1458 				rdata->ids.roles = roles;
1459 
1460 				spp->spp_params =
1461 					htonl(lport->service_params);
1462 				break;
1463 			default:
1464 				resp = FC_SPP_RESP_INVL;
1465 				break;
1466 			}
1467 			spp->spp_flags |= resp;
1468 			len -= plen;
1469 			rspp = (struct fc_els_spp *)((char *)rspp + plen);
1470 			spp = (struct fc_els_spp *)((char *)spp + plen);
1471 		}
1472 
1473 		/*
1474 		 * Send LS_ACC.	 If this fails, the originator should retry.
1475 		 */
1476 		f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1477 		f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1478 		ep = fc_seq_exch(sp);
1479 		fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1480 			       FC_TYPE_ELS, f_ctl, 0);
1481 		lport->tt.seq_send(lport, sp, fp);
1482 
1483 		/*
1484 		 * Get lock and re-check state.
1485 		 */
1486 		switch (rdata->rp_state) {
1487 		case RPORT_ST_PRLI:
1488 			fc_rport_enter_ready(rdata);
1489 			break;
1490 		case RPORT_ST_READY:
1491 		case RPORT_ST_ADISC:
1492 			break;
1493 		default:
1494 			break;
1495 		}
1496 	}
1497 	fc_frame_free(rx_fp);
1498 }
1499 
1500 /**
1501  * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
1502  * @rdata: The remote port that sent the PRLO request
1503  * @sp:	   The sequence that the PRLO was on
1504  * @fp:	   The PRLO request frame
1505  *
1506  * Locking Note: The rport lock is exected to be held before calling
1507  * this function.
1508  */
1509 static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
1510 				   struct fc_seq *sp,
1511 				   struct fc_frame *fp)
1512 {
1513 	struct fc_lport *lport = rdata->local_port;
1514 
1515 	struct fc_frame_header *fh;
1516 	struct fc_seq_els_data rjt_data;
1517 
1518 	fh = fc_frame_header_get(fp);
1519 
1520 	FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
1521 		     fc_rport_state(rdata));
1522 
1523 	rjt_data.fp = NULL;
1524 	rjt_data.reason = ELS_RJT_UNAB;
1525 	rjt_data.explan = ELS_EXPL_NONE;
1526 	lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1527 	fc_frame_free(fp);
1528 }
1529 
1530 /**
1531  * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
1532  * @lport: The local port that received the LOGO request
1533  * @sp:	   The sequence that the LOGO request was on
1534  * @fp:	   The LOGO request frame
1535  *
1536  * Locking Note: The rport lock is exected to be held before calling
1537  * this function.
1538  */
1539 static void fc_rport_recv_logo_req(struct fc_lport *lport,
1540 				   struct fc_seq *sp,
1541 				   struct fc_frame *fp)
1542 {
1543 	struct fc_frame_header *fh;
1544 	struct fc_rport_priv *rdata;
1545 	u32 sid;
1546 
1547 	lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
1548 
1549 	fh = fc_frame_header_get(fp);
1550 	sid = ntoh24(fh->fh_s_id);
1551 
1552 	mutex_lock(&lport->disc.disc_mutex);
1553 	rdata = lport->tt.rport_lookup(lport, sid);
1554 	if (rdata) {
1555 		mutex_lock(&rdata->rp_mutex);
1556 		FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
1557 			     fc_rport_state(rdata));
1558 
1559 		fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
1560 
1561 		/*
1562 		 * If the remote port was created due to discovery, set state
1563 		 * to log back in.  It may have seen a stale RSCN about us.
1564 		 */
1565 		if (rdata->disc_id)
1566 			fc_rport_state_enter(rdata, RPORT_ST_RESTART);
1567 		mutex_unlock(&rdata->rp_mutex);
1568 	} else
1569 		FC_RPORT_ID_DBG(lport, sid,
1570 				"Received LOGO from non-logged-in port\n");
1571 	mutex_unlock(&lport->disc.disc_mutex);
1572 	fc_frame_free(fp);
1573 }
1574 
1575 /**
1576  * fc_rport_flush_queue() - Flush the rport_event_queue
1577  */
1578 static void fc_rport_flush_queue(void)
1579 {
1580 	flush_workqueue(rport_event_queue);
1581 }
1582 
1583 /**
1584  * fc_rport_init() - Initialize the remote port layer for a local port
1585  * @lport: The local port to initialize the remote port layer for
1586  */
1587 int fc_rport_init(struct fc_lport *lport)
1588 {
1589 	if (!lport->tt.rport_lookup)
1590 		lport->tt.rport_lookup = fc_rport_lookup;
1591 
1592 	if (!lport->tt.rport_create)
1593 		lport->tt.rport_create = fc_rport_create;
1594 
1595 	if (!lport->tt.rport_login)
1596 		lport->tt.rport_login = fc_rport_login;
1597 
1598 	if (!lport->tt.rport_logoff)
1599 		lport->tt.rport_logoff = fc_rport_logoff;
1600 
1601 	if (!lport->tt.rport_recv_req)
1602 		lport->tt.rport_recv_req = fc_rport_recv_req;
1603 
1604 	if (!lport->tt.rport_flush_queue)
1605 		lport->tt.rport_flush_queue = fc_rport_flush_queue;
1606 
1607 	if (!lport->tt.rport_destroy)
1608 		lport->tt.rport_destroy = fc_rport_destroy;
1609 
1610 	return 0;
1611 }
1612 EXPORT_SYMBOL(fc_rport_init);
1613 
1614 /**
1615  * fc_setup_rport() - Initialize the rport_event_queue
1616  */
1617 int fc_setup_rport()
1618 {
1619 	rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
1620 	if (!rport_event_queue)
1621 		return -ENOMEM;
1622 	return 0;
1623 }
1624 
1625 /**
1626  * fc_destroy_rport() - Destroy the rport_event_queue
1627  */
1628 void fc_destroy_rport()
1629 {
1630 	destroy_workqueue(rport_event_queue);
1631 }
1632 
1633 /**
1634  * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
1635  * @rport: The remote port whose I/O should be terminated
1636  */
1637 void fc_rport_terminate_io(struct fc_rport *rport)
1638 {
1639 	struct fc_rport_libfc_priv *rpriv = rport->dd_data;
1640 	struct fc_lport *lport = rpriv->local_port;
1641 
1642 	lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
1643 	lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
1644 }
1645 EXPORT_SYMBOL(fc_rport_terminate_io);
1646