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