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