blob: b44c3136eb5181311f12f982fa1ab77b5e95a5f5 [file] [log] [blame]
Robert Love42e9a922008-12-09 15:10:17 -08001/*
2 * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
16 *
17 * Maintained at www.Open-FCoE.org
18 */
19
20/*
21 * RPORT GENERAL INFO
22 *
23 * This file contains all processing regarding fc_rports. It contains the
24 * rport state machine and does all rport interaction with the transport class.
25 * There should be no other places in libfc that interact directly with the
26 * transport class in regards to adding and deleting rports.
27 *
28 * fc_rport's represent N_Port's within the fabric.
29 */
30
31/*
32 * RPORT LOCKING
33 *
34 * The rport should never hold the rport mutex and then attempt to acquire
35 * either the lport or disc mutexes. The rport's mutex is considered lesser
36 * than both the lport's mutex and the disc mutex. Refer to fc_lport.c for
Uwe Kleine-König732bee72010-06-11 12:16:59 +020037 * more comments on the hierarchy.
Robert Love42e9a922008-12-09 15:10:17 -080038 *
39 * The locking strategy is similar to the lport's strategy. The lock protects
40 * the rport's states and is held and released by the entry points to the rport
41 * block. All _enter_* functions correspond to rport states and expect the rport
42 * mutex to be locked before calling them. This means that rports only handle
43 * one request or response at a time, since they're not critical for the I/O
44 * path this potential over-use of the mutex is acceptable.
45 */
46
Hannes Reinecke4d2095c2016-09-30 11:01:14 +020047/*
48 * RPORT REFERENCE COUNTING
49 *
50 * A rport reference should be taken when:
51 * - an rport is allocated
52 * - a workqueue item is scheduled
53 * - an ELS request is send
54 * The reference should be dropped when:
55 * - the workqueue function has finished
56 * - the ELS response is handled
57 * - an rport is removed
58 */
59
Robert Love42e9a922008-12-09 15:10:17 -080060#include <linux/kernel.h>
61#include <linux/spinlock.h>
62#include <linux/interrupt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090063#include <linux/slab.h>
Robert Love42e9a922008-12-09 15:10:17 -080064#include <linux/rcupdate.h>
65#include <linux/timer.h>
66#include <linux/workqueue.h>
Paul Gortmaker09703662011-05-27 09:37:25 -040067#include <linux/export.h>
Ingo Molnarb2d09102017-02-04 01:27:20 +010068#include <linux/rculist.h>
69
Robert Love42e9a922008-12-09 15:10:17 -080070#include <asm/unaligned.h>
71
72#include <scsi/libfc.h>
73#include <scsi/fc_encode.h>
74
Robert Love8866a5d2009-11-03 11:45:58 -080075#include "fc_libfc.h"
76
Randy Dunlap55204902011-01-28 16:03:57 -080077static struct workqueue_struct *rport_event_queue;
Robert Love42e9a922008-12-09 15:10:17 -080078
Joe Eykholta7b12a22010-07-20 15:20:08 -070079static void fc_rport_enter_flogi(struct fc_rport_priv *);
Joe Eykholt9fb9d322009-08-25 14:00:50 -070080static void fc_rport_enter_plogi(struct fc_rport_priv *);
81static void fc_rport_enter_prli(struct fc_rport_priv *);
82static void fc_rport_enter_rtv(struct fc_rport_priv *);
83static void fc_rport_enter_ready(struct fc_rport_priv *);
84static void fc_rport_enter_logo(struct fc_rport_priv *);
Joe Eykholt370c3bd2009-08-25 14:03:47 -070085static void fc_rport_enter_adisc(struct fc_rport_priv *);
Robert Love42e9a922008-12-09 15:10:17 -080086
Joe Eykholt922611562010-07-20 15:21:12 -070087static void fc_rport_recv_plogi_req(struct fc_lport *, struct fc_frame *);
88static void fc_rport_recv_prli_req(struct fc_rport_priv *, struct fc_frame *);
89static void fc_rport_recv_prlo_req(struct fc_rport_priv *, struct fc_frame *);
90static void fc_rport_recv_logo_req(struct fc_lport *, struct fc_frame *);
Robert Love42e9a922008-12-09 15:10:17 -080091static void fc_rport_timeout(struct work_struct *);
Hannes Reinecke9f9504a2016-10-13 15:10:44 +020092static void fc_rport_error(struct fc_rport_priv *, int);
93static void fc_rport_error_retry(struct fc_rport_priv *, int);
Robert Love42e9a922008-12-09 15:10:17 -080094static void fc_rport_work(struct work_struct *);
95
96static const char *fc_rport_state_names[] = {
Robert Love42e9a922008-12-09 15:10:17 -080097 [RPORT_ST_INIT] = "Init",
Joe Eykholta7b12a22010-07-20 15:20:08 -070098 [RPORT_ST_FLOGI] = "FLOGI",
99 [RPORT_ST_PLOGI_WAIT] = "PLOGI_WAIT",
Robert Love42e9a922008-12-09 15:10:17 -0800100 [RPORT_ST_PLOGI] = "PLOGI",
101 [RPORT_ST_PRLI] = "PRLI",
102 [RPORT_ST_RTV] = "RTV",
103 [RPORT_ST_READY] = "Ready",
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700104 [RPORT_ST_ADISC] = "ADISC",
Joe Eykholt14194052009-07-29 17:04:43 -0700105 [RPORT_ST_DELETE] = "Delete",
Robert Love42e9a922008-12-09 15:10:17 -0800106};
107
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700108/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800109 * fc_rport_lookup() - Lookup a remote port by port_id
110 * @lport: The local port to lookup the remote port on
111 * @port_id: The remote port ID to look up
Joe Eykholt42e90412010-07-20 15:19:37 -0700112 *
Hannes Reineckebaa67192016-05-24 08:11:58 +0200113 * The reference count of the fc_rport_priv structure is
114 * increased by one.
Joe Eykholt8025b5d2009-08-25 14:02:06 -0700115 */
Hannes Reineckee87b7772016-10-18 10:01:40 +0200116struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport,
117 u32 port_id)
Joe Eykholt8025b5d2009-08-25 14:02:06 -0700118{
Hannes Reineckebaa67192016-05-24 08:11:58 +0200119 struct fc_rport_priv *rdata = NULL, *tmp_rdata;
Joe Eykholt8025b5d2009-08-25 14:02:06 -0700120
Hannes Reineckebaa67192016-05-24 08:11:58 +0200121 rcu_read_lock();
122 list_for_each_entry_rcu(tmp_rdata, &lport->disc.rports, peers)
123 if (tmp_rdata->ids.port_id == port_id &&
124 kref_get_unless_zero(&tmp_rdata->kref)) {
125 rdata = tmp_rdata;
126 break;
127 }
128 rcu_read_unlock();
129 return rdata;
Joe Eykholt8025b5d2009-08-25 14:02:06 -0700130}
Hannes Reineckee87b7772016-10-18 10:01:40 +0200131EXPORT_SYMBOL(fc_rport_lookup);
Joe Eykholt8025b5d2009-08-25 14:02:06 -0700132
133/**
Robert Love9737e6a2009-08-25 14:02:59 -0700134 * fc_rport_create() - Create a new remote port
Robert Love3a3b42b2009-11-03 11:47:39 -0800135 * @lport: The local port this remote port will be associated with
136 * @ids: The identifiers for the new remote port
137 *
138 * The remote port will start in the INIT state.
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700139 *
Joe Eykholt48f00902009-08-25 14:01:50 -0700140 * Locking note: must be called with the disc_mutex held.
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700141 */
Hannes Reinecke25800642016-10-18 10:01:42 +0200142struct fc_rport_priv *fc_rport_create(struct fc_lport *lport, u32 port_id)
Robert Love42e9a922008-12-09 15:10:17 -0800143{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700144 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800145
Hannes Reineckee87b7772016-10-18 10:01:40 +0200146 rdata = fc_rport_lookup(lport, port_id);
Joe Eykholt19f97e32009-08-25 14:01:55 -0700147 if (rdata)
148 return rdata;
149
Joe Eykholtf90377a2010-07-20 15:19:42 -0700150 rdata = kzalloc(sizeof(*rdata) + lport->rport_priv_size, GFP_KERNEL);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700151 if (!rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800152 return NULL;
153
Robert Love9737e6a2009-08-25 14:02:59 -0700154 rdata->ids.node_name = -1;
155 rdata->ids.port_name = -1;
156 rdata->ids.port_id = port_id;
157 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
158
Joe Eykholtf211fa52009-08-25 14:01:01 -0700159 kref_init(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800160 mutex_init(&rdata->rp_mutex);
Joe Eykholt795d86f2009-08-25 14:00:39 -0700161 rdata->local_port = lport;
Robert Love42e9a922008-12-09 15:10:17 -0800162 rdata->rp_state = RPORT_ST_INIT;
163 rdata->event = RPORT_EV_NONE;
164 rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
Joe Eykholt795d86f2009-08-25 14:00:39 -0700165 rdata->e_d_tov = lport->e_d_tov;
166 rdata->r_a_tov = lport->r_a_tov;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700167 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
Robert Love42e9a922008-12-09 15:10:17 -0800168 INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
169 INIT_WORK(&rdata->event_work, fc_rport_work);
Bhanu Prakash Gollapudi75a27922011-01-28 16:05:27 -0800170 if (port_id != FC_FID_DIR_SERV) {
171 rdata->lld_event_callback = lport->tt.rport_event_callback;
Joe Eykholt42e90412010-07-20 15:19:37 -0700172 list_add_rcu(&rdata->peers, &lport->disc.rports);
Bhanu Prakash Gollapudi75a27922011-01-28 16:05:27 -0800173 }
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700174 return rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800175}
Hannes Reinecke25800642016-10-18 10:01:42 +0200176EXPORT_SYMBOL(fc_rport_create);
Robert Love42e9a922008-12-09 15:10:17 -0800177
178/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800179 * fc_rport_destroy() - Free a remote port after last reference is released
180 * @kref: The remote port's kref
Joe Eykholtf211fa52009-08-25 14:01:01 -0700181 */
Hannes Reinecke944ef962016-10-18 10:01:39 +0200182void fc_rport_destroy(struct kref *kref)
Joe Eykholtf211fa52009-08-25 14:01:01 -0700183{
184 struct fc_rport_priv *rdata;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700185
186 rdata = container_of(kref, struct fc_rport_priv, kref);
Lai Jiangshan8497a242011-03-18 11:41:14 +0800187 kfree_rcu(rdata, rcu);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700188}
Hannes Reinecke944ef962016-10-18 10:01:39 +0200189EXPORT_SYMBOL(fc_rport_destroy);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700190
191/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800192 * fc_rport_state() - Return a string identifying the remote port's state
193 * @rdata: The remote port
Robert Love42e9a922008-12-09 15:10:17 -0800194 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700195static const char *fc_rport_state(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800196{
197 const char *cp;
Robert Love42e9a922008-12-09 15:10:17 -0800198
199 cp = fc_rport_state_names[rdata->rp_state];
200 if (!cp)
201 cp = "Unknown";
202 return cp;
203}
204
205/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800206 * fc_set_rport_loss_tmo() - Set the remote port loss timeout
207 * @rport: The remote port that gets a new timeout value
208 * @timeout: The new timeout value (in seconds)
Robert Love42e9a922008-12-09 15:10:17 -0800209 */
210void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
211{
212 if (timeout)
Mike Christie73b43762010-10-08 17:12:10 -0700213 rport->dev_loss_tmo = timeout;
Robert Love42e9a922008-12-09 15:10:17 -0800214 else
Mike Christie73b43762010-10-08 17:12:10 -0700215 rport->dev_loss_tmo = 1;
Robert Love42e9a922008-12-09 15:10:17 -0800216}
217EXPORT_SYMBOL(fc_set_rport_loss_tmo);
218
219/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800220 * fc_plogi_get_maxframe() - Get the maximum payload from the common service
221 * parameters in a FLOGI frame
Joe Eykholta7b12a22010-07-20 15:20:08 -0700222 * @flp: The FLOGI or PLOGI payload
Robert Love3a3b42b2009-11-03 11:47:39 -0800223 * @maxval: The maximum frame size upper limit; this may be less than what
224 * is in the service parameters
Robert Love42e9a922008-12-09 15:10:17 -0800225 */
Robert Loveb2ab99c2009-02-27 10:55:50 -0800226static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
227 unsigned int maxval)
Robert Love42e9a922008-12-09 15:10:17 -0800228{
229 unsigned int mfs;
230
231 /*
232 * Get max payload from the common service parameters and the
233 * class 3 receive data field size.
234 */
235 mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
236 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
237 maxval = mfs;
238 mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
239 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
240 maxval = mfs;
241 return maxval;
242}
243
244/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800245 * fc_rport_state_enter() - Change the state of a remote port
246 * @rdata: The remote port whose state should change
247 * @new: The new state
Robert Love42e9a922008-12-09 15:10:17 -0800248 *
249 * Locking Note: Called with the rport lock held
250 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700251static void fc_rport_state_enter(struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -0800252 enum fc_rport_state new)
253{
Robert Love42e9a922008-12-09 15:10:17 -0800254 if (rdata->rp_state != new)
255 rdata->retries = 0;
256 rdata->rp_state = new;
257}
258
Robert Love3a3b42b2009-11-03 11:47:39 -0800259/**
260 * fc_rport_work() - Handler for remote port events in the rport_event_queue
261 * @work: Handle to the remote port being dequeued
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200262 *
263 * Reference counting: drops kref on return
Robert Love3a3b42b2009-11-03 11:47:39 -0800264 */
Robert Love42e9a922008-12-09 15:10:17 -0800265static void fc_rport_work(struct work_struct *work)
266{
Abhijeet Joglekar571f8242009-02-27 10:54:41 -0800267 u32 port_id;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700268 struct fc_rport_priv *rdata =
269 container_of(work, struct fc_rport_priv, event_work);
Robert Love3a3b42b2009-11-03 11:47:39 -0800270 struct fc_rport_libfc_priv *rpriv;
Robert Love42e9a922008-12-09 15:10:17 -0800271 enum fc_rport_event event;
Robert Love42e9a922008-12-09 15:10:17 -0800272 struct fc_lport *lport = rdata->local_port;
273 struct fc_rport_operations *rport_ops;
Joe Eykholt629f4422009-08-25 14:01:06 -0700274 struct fc_rport_identifiers ids;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700275 struct fc_rport *rport;
Joe Eykholt96ad8462011-01-28 16:04:02 -0800276 struct fc4_prov *prov;
277 u8 type;
Robert Love42e9a922008-12-09 15:10:17 -0800278
279 mutex_lock(&rdata->rp_mutex);
280 event = rdata->event;
281 rport_ops = rdata->ops;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700282 rport = rdata->rport;
Robert Love42e9a922008-12-09 15:10:17 -0800283
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700284 FC_RPORT_DBG(rdata, "work event %u\n", event);
285
Joe Eykholt629f4422009-08-25 14:01:06 -0700286 switch (event) {
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700287 case RPORT_EV_READY:
Joe Eykholtf211fa52009-08-25 14:01:01 -0700288 ids = rdata->ids;
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700289 rdata->event = RPORT_EV_NONE;
Joe Eykholtf0342602010-06-11 16:44:57 -0700290 rdata->major_retries = 0;
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700291 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800292 mutex_unlock(&rdata->rp_mutex);
293
Hannes Reinecke57d3ec7e2016-10-13 15:10:37 +0200294 if (!rport) {
295 FC_RPORT_DBG(rdata, "No rport!\n");
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700296 rport = fc_remote_port_add(lport->host, 0, &ids);
Hannes Reinecke57d3ec7e2016-10-13 15:10:37 +0200297 }
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700298 if (!rport) {
299 FC_RPORT_DBG(rdata, "Failed to add the rport\n");
Hannes Reineckec96c7922016-10-18 10:01:44 +0200300 fc_rport_logoff(rdata);
Hannes Reinecke944ef962016-10-18 10:01:39 +0200301 kref_put(&rdata->kref, fc_rport_destroy);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700302 return;
Robert Love42e9a922008-12-09 15:10:17 -0800303 }
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700304 mutex_lock(&rdata->rp_mutex);
305 if (rdata->rport)
306 FC_RPORT_DBG(rdata, "rport already allocated\n");
307 rdata->rport = rport;
308 rport->maxframe_size = rdata->maxframe_size;
309 rport->supported_classes = rdata->supported_classes;
310
Robert Love3a3b42b2009-11-03 11:47:39 -0800311 rpriv = rport->dd_data;
312 rpriv->local_port = lport;
313 rpriv->rp_state = rdata->rp_state;
314 rpriv->flags = rdata->flags;
315 rpriv->e_d_tov = rdata->e_d_tov;
316 rpriv->r_a_tov = rdata->r_a_tov;
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700317 mutex_unlock(&rdata->rp_mutex);
318
Joe Eykholt83455922009-08-25 14:02:01 -0700319 if (rport_ops && rport_ops->event_callback) {
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700320 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700321 rport_ops->event_callback(lport, rdata, event);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700322 }
Bhanu Prakash Gollapudi75a27922011-01-28 16:05:27 -0800323 if (rdata->lld_event_callback) {
324 FC_RPORT_DBG(rdata, "lld callback ev %d\n", event);
325 rdata->lld_event_callback(lport, rdata, event);
326 }
Hannes Reinecke944ef962016-10-18 10:01:39 +0200327 kref_put(&rdata->kref, fc_rport_destroy);
Joe Eykholt629f4422009-08-25 14:01:06 -0700328 break;
329
330 case RPORT_EV_FAILED:
331 case RPORT_EV_LOGO:
332 case RPORT_EV_STOP:
Joe Eykholt96ad8462011-01-28 16:04:02 -0800333 if (rdata->prli_count) {
334 mutex_lock(&fc_prov_mutex);
335 for (type = 1; type < FC_FC4_PROV_SIZE; type++) {
336 prov = fc_passive_prov[type];
337 if (prov && prov->prlo)
338 prov->prlo(rdata);
339 }
340 mutex_unlock(&fc_prov_mutex);
341 }
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700342 port_id = rdata->ids.port_id;
Robert Love42e9a922008-12-09 15:10:17 -0800343 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700344
Joe Eykholt83455922009-08-25 14:02:01 -0700345 if (rport_ops && rport_ops->event_callback) {
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700346 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700347 rport_ops->event_callback(lport, rdata, event);
Abhijeet Joglekar571f8242009-02-27 10:54:41 -0800348 }
Bhanu Prakash Gollapudi75a27922011-01-28 16:05:27 -0800349 if (rdata->lld_event_callback) {
350 FC_RPORT_DBG(rdata, "lld callback ev %d\n", event);
351 rdata->lld_event_callback(lport, rdata, event);
352 }
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200353 if (cancel_delayed_work_sync(&rdata->retry_work))
Hannes Reinecke944ef962016-10-18 10:01:39 +0200354 kref_put(&rdata->kref, fc_rport_destroy);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700355
356 /*
357 * Reset any outstanding exchanges before freeing rport.
358 */
359 lport->tt.exch_mgr_reset(lport, 0, port_id);
360 lport->tt.exch_mgr_reset(lport, port_id, 0);
361
362 if (rport) {
Robert Love3a3b42b2009-11-03 11:47:39 -0800363 rpriv = rport->dd_data;
364 rpriv->rp_state = RPORT_ST_DELETE;
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700365 mutex_lock(&rdata->rp_mutex);
366 rdata->rport = NULL;
367 mutex_unlock(&rdata->rp_mutex);
368 fc_remote_port_delete(rport);
369 }
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700370
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700371 mutex_lock(&rdata->rp_mutex);
372 if (rdata->rp_state == RPORT_ST_DELETE) {
373 if (port_id == FC_FID_DIR_SERV) {
374 rdata->event = RPORT_EV_NONE;
375 mutex_unlock(&rdata->rp_mutex);
Hannes Reinecke944ef962016-10-18 10:01:39 +0200376 kref_put(&rdata->kref, fc_rport_destroy);
Joe Eykholtf0342602010-06-11 16:44:57 -0700377 } else if ((rdata->flags & FC_RP_STARTED) &&
378 rdata->major_retries <
379 lport->max_rport_retry_count) {
380 rdata->major_retries++;
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700381 rdata->event = RPORT_EV_NONE;
382 FC_RPORT_DBG(rdata, "work restart\n");
Joe Eykholta7b12a22010-07-20 15:20:08 -0700383 fc_rport_enter_flogi(rdata);
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700384 mutex_unlock(&rdata->rp_mutex);
385 } else {
386 FC_RPORT_DBG(rdata, "work delete\n");
Hannes Reineckea407c592016-09-30 11:01:15 +0200387 mutex_lock(&lport->disc.disc_mutex);
Joe Eykholt42e90412010-07-20 15:19:37 -0700388 list_del_rcu(&rdata->peers);
Hannes Reineckea407c592016-09-30 11:01:15 +0200389 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700390 mutex_unlock(&rdata->rp_mutex);
Hannes Reinecke944ef962016-10-18 10:01:39 +0200391 kref_put(&rdata->kref, fc_rport_destroy);
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700392 }
393 } else {
394 /*
395 * Re-open for events. Reissue READY event if ready.
396 */
397 rdata->event = RPORT_EV_NONE;
Hannes Reinecke57d3ec7e2016-10-13 15:10:37 +0200398 if (rdata->rp_state == RPORT_ST_READY) {
399 FC_RPORT_DBG(rdata, "work reopen\n");
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700400 fc_rport_enter_ready(rdata);
Hannes Reinecke57d3ec7e2016-10-13 15:10:37 +0200401 }
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700402 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700403 }
Joe Eykholt629f4422009-08-25 14:01:06 -0700404 break;
405
406 default:
Robert Love42e9a922008-12-09 15:10:17 -0800407 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt629f4422009-08-25 14:01:06 -0700408 break;
409 }
Hannes Reinecke944ef962016-10-18 10:01:39 +0200410 kref_put(&rdata->kref, fc_rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800411}
412
413/**
Robert Love34f42a02009-02-27 10:55:45 -0800414 * fc_rport_login() - Start the remote port login state machine
Robert Love3a3b42b2009-11-03 11:47:39 -0800415 * @rdata: The remote port to be logged in to
Robert Love42e9a922008-12-09 15:10:17 -0800416 *
Hannes Reinecke05d7d3b2016-10-18 10:01:43 +0200417 * Initiates the RP state machine. It is called from the LP module.
418 * This function will issue the following commands to the N_Port
419 * identified by the FC ID provided.
420 *
421 * - PLOGI
422 * - PRLI
423 * - RTV
424 *
Robert Love42e9a922008-12-09 15:10:17 -0800425 * Locking Note: Called without the rport lock held. This
426 * function will hold the rport lock, call an _enter_*
427 * function and then unlock the rport.
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700428 *
429 * This indicates the intent to be logged into the remote port.
430 * If it appears we are already logged in, ADISC is used to verify
431 * the setup.
Robert Love42e9a922008-12-09 15:10:17 -0800432 */
Hannes Reinecke05d7d3b2016-10-18 10:01:43 +0200433int fc_rport_login(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800434{
Robert Love42e9a922008-12-09 15:10:17 -0800435 mutex_lock(&rdata->rp_mutex);
436
Hannes Reinecke06ee2572016-09-30 11:01:18 +0200437 if (rdata->flags & FC_RP_STARTED) {
438 FC_RPORT_DBG(rdata, "port already started\n");
439 mutex_unlock(&rdata->rp_mutex);
440 return 0;
441 }
442
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700443 rdata->flags |= FC_RP_STARTED;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700444 switch (rdata->rp_state) {
445 case RPORT_ST_READY:
446 FC_RPORT_DBG(rdata, "ADISC port\n");
447 fc_rport_enter_adisc(rdata);
448 break;
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700449 case RPORT_ST_DELETE:
450 FC_RPORT_DBG(rdata, "Restart deleted port\n");
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700451 break;
Hannes Reineckee5a20002016-09-30 11:01:17 +0200452 case RPORT_ST_INIT:
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700453 FC_RPORT_DBG(rdata, "Login to port\n");
Joe Eykholta7b12a22010-07-20 15:20:08 -0700454 fc_rport_enter_flogi(rdata);
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700455 break;
Hannes Reineckee5a20002016-09-30 11:01:17 +0200456 default:
457 FC_RPORT_DBG(rdata, "Login in progress, state %s\n",
458 fc_rport_state(rdata));
459 break;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700460 }
Robert Love42e9a922008-12-09 15:10:17 -0800461 mutex_unlock(&rdata->rp_mutex);
462
463 return 0;
464}
Hannes Reinecke05d7d3b2016-10-18 10:01:43 +0200465EXPORT_SYMBOL(fc_rport_login);
Robert Love42e9a922008-12-09 15:10:17 -0800466
467/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800468 * fc_rport_enter_delete() - Schedule a remote port to be deleted
469 * @rdata: The remote port to be deleted
470 * @event: The event to report as the reason for deletion
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700471 *
472 * Locking Note: Called with the rport lock held.
473 *
474 * Allow state change into DELETE only once.
475 *
476 * Call queue_work only if there's no event already pending.
477 * Set the new event so that the old pending event will not occur.
478 * Since we have the mutex, even if fc_rport_work() is already started,
479 * it'll see the new event.
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200480 *
481 * Reference counting: does not modify kref
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700482 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700483static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700484 enum fc_rport_event event)
485{
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700486 if (rdata->rp_state == RPORT_ST_DELETE)
487 return;
488
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700489 FC_RPORT_DBG(rdata, "Delete port\n");
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700490
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700491 fc_rport_state_enter(rdata, RPORT_ST_DELETE);
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700492
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200493 kref_get(&rdata->kref);
494 if (rdata->event == RPORT_EV_NONE &&
495 !queue_work(rport_event_queue, &rdata->event_work))
Hannes Reinecke944ef962016-10-18 10:01:39 +0200496 kref_put(&rdata->kref, fc_rport_destroy);
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200497
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700498 rdata->event = event;
499}
500
501/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800502 * fc_rport_logoff() - Logoff and remove a remote port
503 * @rdata: The remote port to be logged off of
Robert Love42e9a922008-12-09 15:10:17 -0800504 *
505 * Locking Note: Called without the rport lock held. This
506 * function will hold the rport lock, call an _enter_*
507 * function and then unlock the rport.
508 */
Hannes Reineckec96c7922016-10-18 10:01:44 +0200509int fc_rport_logoff(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800510{
Hannes Reinecke649eb862016-08-05 14:55:02 +0200511 struct fc_lport *lport = rdata->local_port;
512 u32 port_id = rdata->ids.port_id;
513
Robert Love42e9a922008-12-09 15:10:17 -0800514 mutex_lock(&rdata->rp_mutex);
515
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700516 FC_RPORT_DBG(rdata, "Remove port\n");
Robert Love42e9a922008-12-09 15:10:17 -0800517
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700518 rdata->flags &= ~FC_RP_STARTED;
Joe Eykholt14194052009-07-29 17:04:43 -0700519 if (rdata->rp_state == RPORT_ST_DELETE) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700520 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700521 goto out;
522 }
Hannes Reinecke649eb862016-08-05 14:55:02 +0200523 /*
524 * FC-LS states:
525 * To explicitly Logout, the initiating Nx_Port shall terminate
526 * other open Sequences that it initiated with the destination
527 * Nx_Port prior to performing Logout.
528 */
529 lport->tt.exch_mgr_reset(lport, 0, port_id);
530 lport->tt.exch_mgr_reset(lport, port_id, 0);
531
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700532 fc_rport_enter_logo(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800533
534 /*
Joe Eykholt14194052009-07-29 17:04:43 -0700535 * Change the state to Delete so that we discard
Robert Love42e9a922008-12-09 15:10:17 -0800536 * the response.
537 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700538 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700539out:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700540 mutex_unlock(&rdata->rp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800541 return 0;
542}
Hannes Reineckec96c7922016-10-18 10:01:44 +0200543EXPORT_SYMBOL(fc_rport_logoff);
Robert Love42e9a922008-12-09 15:10:17 -0800544
545/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800546 * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state
547 * @rdata: The remote port that is ready
Robert Love42e9a922008-12-09 15:10:17 -0800548 *
549 * Locking Note: The rport lock is expected to be held before calling
550 * this routine.
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200551 *
552 * Reference counting: schedules workqueue, does not modify kref
Robert Love42e9a922008-12-09 15:10:17 -0800553 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700554static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800555{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700556 fc_rport_state_enter(rdata, RPORT_ST_READY);
Robert Love42e9a922008-12-09 15:10:17 -0800557
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700558 FC_RPORT_DBG(rdata, "Port is Ready\n");
Robert Love42e9a922008-12-09 15:10:17 -0800559
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200560 kref_get(&rdata->kref);
561 if (rdata->event == RPORT_EV_NONE &&
562 !queue_work(rport_event_queue, &rdata->event_work))
Hannes Reinecke944ef962016-10-18 10:01:39 +0200563 kref_put(&rdata->kref, fc_rport_destroy);
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200564
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700565 rdata->event = RPORT_EV_READY;
Robert Love42e9a922008-12-09 15:10:17 -0800566}
567
568/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800569 * fc_rport_timeout() - Handler for the retry_work timer
570 * @work: Handle to the remote port that has timed out
Robert Love42e9a922008-12-09 15:10:17 -0800571 *
572 * Locking Note: Called without the rport lock held. This
573 * function will hold the rport lock, call an _enter_*
574 * function and then unlock the rport.
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200575 *
576 * Reference counting: Drops kref on return.
Robert Love42e9a922008-12-09 15:10:17 -0800577 */
578static void fc_rport_timeout(struct work_struct *work)
579{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700580 struct fc_rport_priv *rdata =
581 container_of(work, struct fc_rport_priv, retry_work.work);
Robert Love42e9a922008-12-09 15:10:17 -0800582
583 mutex_lock(&rdata->rp_mutex);
Hannes Reinecke57d3ec7e2016-10-13 15:10:37 +0200584 FC_RPORT_DBG(rdata, "Port timeout, state %s\n", fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800585
586 switch (rdata->rp_state) {
Joe Eykholta7b12a22010-07-20 15:20:08 -0700587 case RPORT_ST_FLOGI:
588 fc_rport_enter_flogi(rdata);
589 break;
Robert Love42e9a922008-12-09 15:10:17 -0800590 case RPORT_ST_PLOGI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700591 fc_rport_enter_plogi(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800592 break;
593 case RPORT_ST_PRLI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700594 fc_rport_enter_prli(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800595 break;
596 case RPORT_ST_RTV:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700597 fc_rport_enter_rtv(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800598 break;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700599 case RPORT_ST_ADISC:
600 fc_rport_enter_adisc(rdata);
601 break;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700602 case RPORT_ST_PLOGI_WAIT:
Robert Love42e9a922008-12-09 15:10:17 -0800603 case RPORT_ST_READY:
604 case RPORT_ST_INIT:
Joe Eykholt14194052009-07-29 17:04:43 -0700605 case RPORT_ST_DELETE:
Robert Love42e9a922008-12-09 15:10:17 -0800606 break;
607 }
608
609 mutex_unlock(&rdata->rp_mutex);
Hannes Reinecke944ef962016-10-18 10:01:39 +0200610 kref_put(&rdata->kref, fc_rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800611}
612
613/**
Robert Love34f42a02009-02-27 10:55:45 -0800614 * fc_rport_error() - Error handler, called once retries have been exhausted
Robert Love3a3b42b2009-11-03 11:47:39 -0800615 * @rdata: The remote port the error is happened on
Hannes Reinecke9f9504a2016-10-13 15:10:44 +0200616 * @err: The error code
Robert Love42e9a922008-12-09 15:10:17 -0800617 *
Robert Love42e9a922008-12-09 15:10:17 -0800618 * Locking Note: The rport lock is expected to be held before
619 * calling this routine
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200620 *
621 * Reference counting: does not modify kref
Robert Love42e9a922008-12-09 15:10:17 -0800622 */
Hannes Reinecke9f9504a2016-10-13 15:10:44 +0200623static void fc_rport_error(struct fc_rport_priv *rdata, int err)
Robert Love42e9a922008-12-09 15:10:17 -0800624{
Hannes Reinecked3919662016-08-05 14:55:01 +0200625 struct fc_lport *lport = rdata->local_port;
626
Hannes Reinecke9f9504a2016-10-13 15:10:44 +0200627 FC_RPORT_DBG(rdata, "Error %d in state %s, retries %d\n",
628 -err, fc_rport_state(rdata), rdata->retries);
Robert Love42e9a922008-12-09 15:10:17 -0800629
Chris Leech6755db12009-02-27 10:55:02 -0800630 switch (rdata->rp_state) {
Joe Eykholta7b12a22010-07-20 15:20:08 -0700631 case RPORT_ST_FLOGI:
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700632 rdata->flags &= ~FC_RP_STARTED;
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700633 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
Chris Leech6755db12009-02-27 10:55:02 -0800634 break;
Hannes Reinecked3919662016-08-05 14:55:01 +0200635 case RPORT_ST_PLOGI:
636 if (lport->point_to_multipoint) {
637 rdata->flags &= ~FC_RP_STARTED;
638 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
639 } else
640 fc_rport_enter_logo(rdata);
641 break;
Chris Leech6755db12009-02-27 10:55:02 -0800642 case RPORT_ST_RTV:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700643 fc_rport_enter_ready(rdata);
Chris Leech6755db12009-02-27 10:55:02 -0800644 break;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700645 case RPORT_ST_PRLI:
646 case RPORT_ST_ADISC:
647 fc_rport_enter_logo(rdata);
648 break;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700649 case RPORT_ST_PLOGI_WAIT:
Joe Eykholt14194052009-07-29 17:04:43 -0700650 case RPORT_ST_DELETE:
Chris Leech6755db12009-02-27 10:55:02 -0800651 case RPORT_ST_READY:
652 case RPORT_ST_INIT:
653 break;
Robert Love42e9a922008-12-09 15:10:17 -0800654 }
655}
656
657/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800658 * fc_rport_error_retry() - Handler for remote port state retries
659 * @rdata: The remote port whose state is to be retried
Hannes Reinecke9f9504a2016-10-13 15:10:44 +0200660 * @err: The error code
Chris Leech6755db12009-02-27 10:55:02 -0800661 *
662 * If the error was an exchange timeout retry immediately,
663 * otherwise wait for E_D_TOV.
664 *
665 * Locking Note: The rport lock is expected to be held before
666 * calling this routine
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200667 *
668 * Reference counting: increments kref when scheduling retry_work
Chris Leech6755db12009-02-27 10:55:02 -0800669 */
Hannes Reinecke9f9504a2016-10-13 15:10:44 +0200670static void fc_rport_error_retry(struct fc_rport_priv *rdata, int err)
Chris Leech6755db12009-02-27 10:55:02 -0800671{
Hannes Reineckea50cc9e2016-10-13 15:10:40 +0200672 unsigned long delay = msecs_to_jiffies(rdata->e_d_tov);
Chris Leech6755db12009-02-27 10:55:02 -0800673
674 /* make sure this isn't an FC_EX_CLOSED error, never retry those */
Hannes Reinecke9f9504a2016-10-13 15:10:44 +0200675 if (err == -FC_EX_CLOSED)
Hillf Danton28a4af12011-01-28 16:03:26 -0800676 goto out;
Chris Leech6755db12009-02-27 10:55:02 -0800677
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700678 if (rdata->retries < rdata->local_port->max_rport_retry_count) {
Hannes Reinecke9f9504a2016-10-13 15:10:44 +0200679 FC_RPORT_DBG(rdata, "Error %d in state %s, retrying\n",
680 err, fc_rport_state(rdata));
Chris Leech6755db12009-02-27 10:55:02 -0800681 rdata->retries++;
682 /* no additional delay on exchange timeouts */
Hannes Reinecke9f9504a2016-10-13 15:10:44 +0200683 if (err == -FC_EX_TIMEOUT)
Chris Leech6755db12009-02-27 10:55:02 -0800684 delay = 0;
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200685 kref_get(&rdata->kref);
686 if (!schedule_delayed_work(&rdata->retry_work, delay))
Hannes Reinecke944ef962016-10-18 10:01:39 +0200687 kref_put(&rdata->kref, fc_rport_destroy);
Chris Leech6755db12009-02-27 10:55:02 -0800688 return;
689 }
690
Hillf Danton28a4af12011-01-28 16:03:26 -0800691out:
Hannes Reinecke9f9504a2016-10-13 15:10:44 +0200692 fc_rport_error(rdata, err);
Chris Leech6755db12009-02-27 10:55:02 -0800693}
694
695/**
Joe Eykholta7b12a22010-07-20 15:20:08 -0700696 * fc_rport_login_complete() - Handle parameters and completion of p-mp login.
697 * @rdata: The remote port which we logged into or which logged into us.
698 * @fp: The FLOGI or PLOGI request or response frame
699 *
700 * Returns non-zero error if a problem is detected with the frame.
701 * Does not free the frame.
702 *
703 * This is only used in point-to-multipoint mode for FIP currently.
704 */
705static int fc_rport_login_complete(struct fc_rport_priv *rdata,
706 struct fc_frame *fp)
707{
708 struct fc_lport *lport = rdata->local_port;
709 struct fc_els_flogi *flogi;
710 unsigned int e_d_tov;
711 u16 csp_flags;
712
713 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
714 if (!flogi)
715 return -EINVAL;
716
717 csp_flags = ntohs(flogi->fl_csp.sp_features);
718
719 if (fc_frame_payload_op(fp) == ELS_FLOGI) {
720 if (csp_flags & FC_SP_FT_FPORT) {
721 FC_RPORT_DBG(rdata, "Fabric bit set in FLOGI\n");
722 return -EINVAL;
723 }
724 } else {
725
726 /*
727 * E_D_TOV is not valid on an incoming FLOGI request.
728 */
729 e_d_tov = ntohl(flogi->fl_csp.sp_e_d_tov);
730 if (csp_flags & FC_SP_FT_EDTR)
731 e_d_tov /= 1000000;
732 if (e_d_tov > rdata->e_d_tov)
733 rdata->e_d_tov = e_d_tov;
734 }
735 rdata->maxframe_size = fc_plogi_get_maxframe(flogi, lport->mfs);
736 return 0;
737}
738
739/**
740 * fc_rport_flogi_resp() - Handle response to FLOGI request for p-mp mode
741 * @sp: The sequence that the FLOGI was on
742 * @fp: The FLOGI response frame
743 * @rp_arg: The remote port that received the FLOGI response
744 */
Bart Van Asschec6b21c92012-01-13 17:26:20 -0800745static void fc_rport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
746 void *rp_arg)
Joe Eykholta7b12a22010-07-20 15:20:08 -0700747{
748 struct fc_rport_priv *rdata = rp_arg;
749 struct fc_lport *lport = rdata->local_port;
750 struct fc_els_flogi *flogi;
751 unsigned int r_a_tov;
Hannes Reinecke9f9504a2016-10-13 15:10:44 +0200752 u8 opcode;
753 int err = 0;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700754
Hannes Reinecke9f9504a2016-10-13 15:10:44 +0200755 FC_RPORT_DBG(rdata, "Received a FLOGI %s\n",
756 IS_ERR(fp) ? "error" : fc_els_resp_type(fp));
Joe Eykholta7b12a22010-07-20 15:20:08 -0700757
758 if (fp == ERR_PTR(-FC_EX_CLOSED))
Hillf Danton0e9e3d32010-11-30 16:19:04 -0800759 goto put;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700760
761 mutex_lock(&rdata->rp_mutex);
762
763 if (rdata->rp_state != RPORT_ST_FLOGI) {
764 FC_RPORT_DBG(rdata, "Received a FLOGI response, but in state "
765 "%s\n", fc_rport_state(rdata));
766 if (IS_ERR(fp))
767 goto err;
768 goto out;
769 }
770
771 if (IS_ERR(fp)) {
Hannes Reinecke9f9504a2016-10-13 15:10:44 +0200772 fc_rport_error(rdata, PTR_ERR(fp));
Joe Eykholta7b12a22010-07-20 15:20:08 -0700773 goto err;
774 }
Hannes Reinecke9f9504a2016-10-13 15:10:44 +0200775 opcode = fc_frame_payload_op(fp);
776 if (opcode == ELS_LS_RJT) {
777 struct fc_els_ls_rjt *rjt;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700778
Hannes Reinecke9f9504a2016-10-13 15:10:44 +0200779 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
780 FC_RPORT_DBG(rdata, "FLOGI ELS rejected, reason %x expl %x\n",
781 rjt->er_reason, rjt->er_explan);
782 err = -FC_EX_ELS_RJT;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700783 goto bad;
Hannes Reinecke9f9504a2016-10-13 15:10:44 +0200784 } else if (opcode != ELS_LS_ACC) {
785 FC_RPORT_DBG(rdata, "FLOGI ELS invalid opcode %x\n", opcode);
786 err = -FC_EX_ELS_RJT;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700787 goto bad;
Hannes Reinecke9f9504a2016-10-13 15:10:44 +0200788 }
789 if (fc_rport_login_complete(rdata, fp)) {
790 FC_RPORT_DBG(rdata, "FLOGI failed, no login\n");
791 err = -FC_EX_INV_LOGIN;
792 goto bad;
793 }
Joe Eykholta7b12a22010-07-20 15:20:08 -0700794
795 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
Hannes Reineckef89b8d62016-09-30 11:01:19 +0200796 if (!flogi) {
Hannes Reinecke9f9504a2016-10-13 15:10:44 +0200797 err = -FC_EX_ALLOC_ERR;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700798 goto bad;
Hannes Reineckef89b8d62016-09-30 11:01:19 +0200799 }
Joe Eykholta7b12a22010-07-20 15:20:08 -0700800 r_a_tov = ntohl(flogi->fl_csp.sp_r_a_tov);
801 if (r_a_tov > rdata->r_a_tov)
802 rdata->r_a_tov = r_a_tov;
803
804 if (rdata->ids.port_name < lport->wwpn)
805 fc_rport_enter_plogi(rdata);
806 else
807 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
808out:
809 fc_frame_free(fp);
810err:
811 mutex_unlock(&rdata->rp_mutex);
Hillf Danton0e9e3d32010-11-30 16:19:04 -0800812put:
Hannes Reinecke944ef962016-10-18 10:01:39 +0200813 kref_put(&rdata->kref, fc_rport_destroy);
Joe Eykholta7b12a22010-07-20 15:20:08 -0700814 return;
815bad:
Hannes Reinecke9f9504a2016-10-13 15:10:44 +0200816 FC_RPORT_DBG(rdata, "Bad FLOGI response\n");
817 fc_rport_error_retry(rdata, err);
Joe Eykholta7b12a22010-07-20 15:20:08 -0700818 goto out;
819}
820
821/**
822 * fc_rport_enter_flogi() - Send a FLOGI request to the remote port for p-mp
823 * @rdata: The remote port to send a FLOGI to
824 *
825 * Locking Note: The rport lock is expected to be held before calling
826 * this routine.
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200827 *
828 * Reference counting: increments kref when sending ELS
Joe Eykholta7b12a22010-07-20 15:20:08 -0700829 */
830static void fc_rport_enter_flogi(struct fc_rport_priv *rdata)
831{
832 struct fc_lport *lport = rdata->local_port;
833 struct fc_frame *fp;
834
835 if (!lport->point_to_multipoint)
836 return fc_rport_enter_plogi(rdata);
837
838 FC_RPORT_DBG(rdata, "Entered FLOGI state from %s state\n",
839 fc_rport_state(rdata));
840
841 fc_rport_state_enter(rdata, RPORT_ST_FLOGI);
842
843 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
844 if (!fp)
Hannes Reinecke9f9504a2016-10-13 15:10:44 +0200845 return fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
Joe Eykholta7b12a22010-07-20 15:20:08 -0700846
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200847 kref_get(&rdata->kref);
Joe Eykholta7b12a22010-07-20 15:20:08 -0700848 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_FLOGI,
849 fc_rport_flogi_resp, rdata,
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200850 2 * lport->r_a_tov)) {
Hannes Reinecke9f9504a2016-10-13 15:10:44 +0200851 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
Hannes Reinecke944ef962016-10-18 10:01:39 +0200852 kref_put(&rdata->kref, fc_rport_destroy);
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200853 }
Joe Eykholta7b12a22010-07-20 15:20:08 -0700854}
855
856/**
857 * fc_rport_recv_flogi_req() - Handle Fabric Login (FLOGI) request in p-mp mode
858 * @lport: The local port that received the PLOGI request
Joe Eykholta7b12a22010-07-20 15:20:08 -0700859 * @rx_fp: The PLOGI request frame
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200860 *
861 * Reference counting: drops kref on return
Joe Eykholta7b12a22010-07-20 15:20:08 -0700862 */
863static void fc_rport_recv_flogi_req(struct fc_lport *lport,
Joe Eykholt922611562010-07-20 15:21:12 -0700864 struct fc_frame *rx_fp)
Joe Eykholta7b12a22010-07-20 15:20:08 -0700865{
866 struct fc_disc *disc;
867 struct fc_els_flogi *flp;
868 struct fc_rport_priv *rdata;
869 struct fc_frame *fp = rx_fp;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700870 struct fc_seq_els_data rjt_data;
Joe Eykholt24f089e2010-07-20 15:21:01 -0700871 u32 sid;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700872
Joe Eykholt251748a2010-07-20 15:20:56 -0700873 sid = fc_frame_sid(fp);
Joe Eykholta7b12a22010-07-20 15:20:08 -0700874
875 FC_RPORT_ID_DBG(lport, sid, "Received FLOGI request\n");
876
877 disc = &lport->disc;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700878 if (!lport->point_to_multipoint) {
879 rjt_data.reason = ELS_RJT_UNSUP;
880 rjt_data.explan = ELS_EXPL_NONE;
881 goto reject;
882 }
883
884 flp = fc_frame_payload_get(fp, sizeof(*flp));
885 if (!flp) {
886 rjt_data.reason = ELS_RJT_LOGIC;
887 rjt_data.explan = ELS_EXPL_INV_LEN;
888 goto reject;
889 }
890
Hannes Reineckee87b7772016-10-18 10:01:40 +0200891 rdata = fc_rport_lookup(lport, sid);
Joe Eykholta7b12a22010-07-20 15:20:08 -0700892 if (!rdata) {
893 rjt_data.reason = ELS_RJT_FIP;
894 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
895 goto reject;
896 }
897 mutex_lock(&rdata->rp_mutex);
898
899 FC_RPORT_DBG(rdata, "Received FLOGI in %s state\n",
900 fc_rport_state(rdata));
901
902 switch (rdata->rp_state) {
903 case RPORT_ST_INIT:
Kiran Patil48058482011-06-20 16:58:59 -0700904 /*
905 * If received the FLOGI request on RPORT which is INIT state
906 * (means not transition to FLOGI either fc_rport timeout
907 * function didn;t trigger or this end hasn;t received
908 * beacon yet from other end. In that case only, allow RPORT
909 * state machine to continue, otherwise fall through which
910 * causes the code to send reject response.
911 * NOTE; Not checking for FIP->state such as VNMP_UP or
912 * VNMP_CLAIM because if FIP state is not one of those,
913 * RPORT wouldn;t have created and 'rport_lookup' would have
914 * failed anyway in that case.
915 */
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200916 break;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700917 case RPORT_ST_DELETE:
918 mutex_unlock(&rdata->rp_mutex);
919 rjt_data.reason = ELS_RJT_FIP;
920 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
Hannes Reineckebaa67192016-05-24 08:11:58 +0200921 goto reject_put;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700922 case RPORT_ST_FLOGI:
923 case RPORT_ST_PLOGI_WAIT:
924 case RPORT_ST_PLOGI:
925 break;
926 case RPORT_ST_PRLI:
927 case RPORT_ST_RTV:
928 case RPORT_ST_READY:
929 case RPORT_ST_ADISC:
930 /*
931 * Set the remote port to be deleted and to then restart.
932 * This queues work to be sure exchanges are reset.
933 */
934 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
935 mutex_unlock(&rdata->rp_mutex);
936 rjt_data.reason = ELS_RJT_BUSY;
937 rjt_data.explan = ELS_EXPL_NONE;
Hannes Reineckebaa67192016-05-24 08:11:58 +0200938 goto reject_put;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700939 }
940 if (fc_rport_login_complete(rdata, fp)) {
941 mutex_unlock(&rdata->rp_mutex);
942 rjt_data.reason = ELS_RJT_LOGIC;
943 rjt_data.explan = ELS_EXPL_NONE;
Hannes Reineckebaa67192016-05-24 08:11:58 +0200944 goto reject_put;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700945 }
Joe Eykholta7b12a22010-07-20 15:20:08 -0700946
947 fp = fc_frame_alloc(lport, sizeof(*flp));
948 if (!fp)
949 goto out;
950
Joe Eykholta7b12a22010-07-20 15:20:08 -0700951 fc_flogi_fill(lport, fp);
952 flp = fc_frame_payload_get(fp, sizeof(*flp));
953 flp->fl_cmd = ELS_LS_ACC;
954
Joe Eykholt24f089e2010-07-20 15:21:01 -0700955 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
956 lport->tt.frame_send(lport, fp);
Joe Eykholta7b12a22010-07-20 15:20:08 -0700957
Hannes Reineckef89b8d62016-09-30 11:01:19 +0200958 /*
959 * Do not proceed with the state machine if our
960 * FLOGI has crossed with an FLOGI from the
961 * remote port; wait for the FLOGI response instead.
962 */
963 if (rdata->rp_state != RPORT_ST_FLOGI) {
964 if (rdata->ids.port_name < lport->wwpn)
965 fc_rport_enter_plogi(rdata);
966 else
967 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
968 }
Joe Eykholta7b12a22010-07-20 15:20:08 -0700969out:
970 mutex_unlock(&rdata->rp_mutex);
Hannes Reinecke944ef962016-10-18 10:01:39 +0200971 kref_put(&rdata->kref, fc_rport_destroy);
Joe Eykholt24f089e2010-07-20 15:21:01 -0700972 fc_frame_free(rx_fp);
Joe Eykholta7b12a22010-07-20 15:20:08 -0700973 return;
974
Hannes Reineckebaa67192016-05-24 08:11:58 +0200975reject_put:
Hannes Reinecke944ef962016-10-18 10:01:39 +0200976 kref_put(&rdata->kref, fc_rport_destroy);
Joe Eykholta7b12a22010-07-20 15:20:08 -0700977reject:
Hannes Reinecke7ab24dd2016-10-18 10:01:35 +0200978 fc_seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
Joe Eykholt24f089e2010-07-20 15:21:01 -0700979 fc_frame_free(rx_fp);
Joe Eykholta7b12a22010-07-20 15:20:08 -0700980}
981
982/**
983 * fc_rport_plogi_resp() - Handler for ELS PLOGI responses
Robert Love3a3b42b2009-11-03 11:47:39 -0800984 * @sp: The sequence the PLOGI is on
985 * @fp: The PLOGI response frame
986 * @rdata_arg: The remote port that sent the PLOGI response
Robert Love42e9a922008-12-09 15:10:17 -0800987 *
988 * Locking Note: This function will be called without the rport lock
989 * held, but it will lock, call an _enter_* function or fc_rport_error
990 * and then unlock the rport.
991 */
992static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700993 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800994{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700995 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800996 struct fc_lport *lport = rdata->local_port;
Robert Lovea29e7642009-04-21 16:27:41 -0700997 struct fc_els_flogi *plp = NULL;
Robert Love42e9a922008-12-09 15:10:17 -0800998 u16 csp_seq;
999 u16 cssp_seq;
1000 u8 op;
1001
Joe Eykholtf657d292009-08-25 14:03:21 -07001002 FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -08001003
Chad Dupuis785141c2016-09-30 11:01:16 +02001004 if (fp == ERR_PTR(-FC_EX_CLOSED))
1005 goto put;
1006
1007 mutex_lock(&rdata->rp_mutex);
1008
Robert Love42e9a922008-12-09 15:10:17 -08001009 if (rdata->rp_state != RPORT_ST_PLOGI) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001010 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
1011 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001012 if (IS_ERR(fp))
1013 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001014 goto out;
1015 }
1016
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001017 if (IS_ERR(fp)) {
Hannes Reinecke9f9504a2016-10-13 15:10:44 +02001018 fc_rport_error_retry(rdata, PTR_ERR(fp));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001019 goto err;
1020 }
1021
Robert Love42e9a922008-12-09 15:10:17 -08001022 op = fc_frame_payload_op(fp);
1023 if (op == ELS_LS_ACC &&
1024 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
Joe Eykholtf211fa52009-08-25 14:01:01 -07001025 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
1026 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
Robert Love42e9a922008-12-09 15:10:17 -08001027
Bhanu Prakash Gollapudi75a27922011-01-28 16:05:27 -08001028 /* save plogi response sp_features for further reference */
1029 rdata->sp_features = ntohs(plp->fl_csp.sp_features);
1030
Joe Eykholta7b12a22010-07-20 15:20:08 -07001031 if (lport->point_to_multipoint)
1032 fc_rport_login_complete(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001033 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
1034 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
1035 if (cssp_seq < csp_seq)
1036 csp_seq = cssp_seq;
1037 rdata->max_seq = csp_seq;
Joe Eykholtf211fa52009-08-25 14:01:01 -07001038 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001039 fc_rport_enter_prli(rdata);
Hannes Reinecke9f9504a2016-10-13 15:10:44 +02001040 } else {
1041 struct fc_els_ls_rjt *rjt;
Robert Love42e9a922008-12-09 15:10:17 -08001042
Hannes Reinecke9f9504a2016-10-13 15:10:44 +02001043 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1044 FC_RPORT_DBG(rdata, "PLOGI ELS rejected, reason %x expl %x\n",
1045 rjt->er_reason, rjt->er_explan);
1046 fc_rport_error_retry(rdata, -FC_EX_ELS_RJT);
1047 }
Robert Love42e9a922008-12-09 15:10:17 -08001048out:
1049 fc_frame_free(fp);
1050err:
1051 mutex_unlock(&rdata->rp_mutex);
Chad Dupuis785141c2016-09-30 11:01:16 +02001052put:
Hannes Reinecke944ef962016-10-18 10:01:39 +02001053 kref_put(&rdata->kref, fc_rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -08001054}
1055
Mark Rustade0335f62013-05-18 04:01:36 +00001056static bool
1057fc_rport_compatible_roles(struct fc_lport *lport, struct fc_rport_priv *rdata)
1058{
1059 if (rdata->ids.roles == FC_PORT_ROLE_UNKNOWN)
1060 return true;
1061 if ((rdata->ids.roles & FC_PORT_ROLE_FCP_TARGET) &&
1062 (lport->service_params & FCP_SPPF_INIT_FCN))
1063 return true;
1064 if ((rdata->ids.roles & FC_PORT_ROLE_FCP_INITIATOR) &&
1065 (lport->service_params & FCP_SPPF_TARG_FCN))
1066 return true;
1067 return false;
1068}
1069
Robert Love42e9a922008-12-09 15:10:17 -08001070/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001071 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
1072 * @rdata: The remote port to send a PLOGI to
Robert Love42e9a922008-12-09 15:10:17 -08001073 *
1074 * Locking Note: The rport lock is expected to be held before calling
1075 * this routine.
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001076 *
1077 * Reference counting: increments kref when sending ELS
Robert Love42e9a922008-12-09 15:10:17 -08001078 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001079static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -08001080{
Robert Love42e9a922008-12-09 15:10:17 -08001081 struct fc_lport *lport = rdata->local_port;
1082 struct fc_frame *fp;
1083
Mark Rustade0335f62013-05-18 04:01:36 +00001084 if (!fc_rport_compatible_roles(lport, rdata)) {
1085 FC_RPORT_DBG(rdata, "PLOGI suppressed for incompatible role\n");
1086 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
1087 return;
1088 }
1089
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001090 FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
1091 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001092
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001093 fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
Robert Love42e9a922008-12-09 15:10:17 -08001094
Joe Eykholtf211fa52009-08-25 14:01:01 -07001095 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
Robert Love42e9a922008-12-09 15:10:17 -08001096 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
1097 if (!fp) {
Joe Eykholta7b12a22010-07-20 15:20:08 -07001098 FC_RPORT_DBG(rdata, "%s frame alloc failed\n", __func__);
Hannes Reinecke9f9504a2016-10-13 15:10:44 +02001099 fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
Robert Love42e9a922008-12-09 15:10:17 -08001100 return;
1101 }
1102 rdata->e_d_tov = lport->e_d_tov;
1103
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001104 kref_get(&rdata->kref);
Joe Eykholtf211fa52009-08-25 14:01:01 -07001105 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
Joe Eykholtb94f8952009-11-03 11:50:21 -08001106 fc_rport_plogi_resp, rdata,
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001107 2 * lport->r_a_tov)) {
Hannes Reinecke9f9504a2016-10-13 15:10:44 +02001108 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
Hannes Reinecke944ef962016-10-18 10:01:39 +02001109 kref_put(&rdata->kref, fc_rport_destroy);
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001110 }
Robert Love42e9a922008-12-09 15:10:17 -08001111}
1112
1113/**
Robert Love34f42a02009-02-27 10:55:45 -08001114 * fc_rport_prli_resp() - Process Login (PRLI) response handler
Robert Love3a3b42b2009-11-03 11:47:39 -08001115 * @sp: The sequence the PRLI response was on
1116 * @fp: The PRLI response frame
1117 * @rdata_arg: The remote port that sent the PRLI response
Robert Love42e9a922008-12-09 15:10:17 -08001118 *
1119 * Locking Note: This function will be called without the rport lock
1120 * held, but it will lock, call an _enter_* function or fc_rport_error
1121 * and then unlock the rport.
1122 */
1123static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001124 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -08001125{
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001126 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -08001127 struct {
1128 struct fc_els_prli prli;
1129 struct fc_els_spp spp;
1130 } *pp;
Joe Eykholt925ceda2011-01-28 16:04:23 -08001131 struct fc_els_spp temp_spp;
Hannes Reinecke57d3ec7e2016-10-13 15:10:37 +02001132 struct fc_els_ls_rjt *rjt;
Joe Eykholt925ceda2011-01-28 16:04:23 -08001133 struct fc4_prov *prov;
Robert Love42e9a922008-12-09 15:10:17 -08001134 u32 roles = FC_RPORT_ROLE_UNKNOWN;
1135 u32 fcp_parm = 0;
1136 u8 op;
Hannes Reinecke386b97b2016-10-13 15:10:46 +02001137 enum fc_els_spp_resp resp_code;
Robert Love42e9a922008-12-09 15:10:17 -08001138
Joe Eykholtf657d292009-08-25 14:03:21 -07001139 FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -08001140
Chad Dupuis785141c2016-09-30 11:01:16 +02001141 if (fp == ERR_PTR(-FC_EX_CLOSED))
1142 goto put;
1143
1144 mutex_lock(&rdata->rp_mutex);
1145
Robert Love42e9a922008-12-09 15:10:17 -08001146 if (rdata->rp_state != RPORT_ST_PRLI) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001147 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
1148 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001149 if (IS_ERR(fp))
1150 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001151 goto out;
1152 }
1153
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001154 if (IS_ERR(fp)) {
Hannes Reinecke9f9504a2016-10-13 15:10:44 +02001155 fc_rport_error_retry(rdata, PTR_ERR(fp));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001156 goto err;
1157 }
1158
Robert Love6bd054c2009-08-25 14:03:04 -07001159 /* reinitialize remote port roles */
1160 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
1161
Robert Love42e9a922008-12-09 15:10:17 -08001162 op = fc_frame_payload_op(fp);
1163 if (op == ELS_LS_ACC) {
1164 pp = fc_frame_payload_get(fp, sizeof(*pp));
Bhanu Prakash Gollapudi618461c2010-06-11 16:43:54 -07001165 if (!pp)
1166 goto out;
1167
1168 resp_code = (pp->spp.spp_flags & FC_SPP_RESP_MASK);
Hannes Reinecke386b97b2016-10-13 15:10:46 +02001169 FC_RPORT_DBG(rdata, "PRLI spp_flags = 0x%x spp_type 0x%x\n",
1170 pp->spp.spp_flags, pp->spp.spp_type);
Bhanu Prakash Gollapudi75a27922011-01-28 16:05:27 -08001171 rdata->spp_type = pp->spp.spp_type;
Bhanu Prakash Gollapudi618461c2010-06-11 16:43:54 -07001172 if (resp_code != FC_SPP_RESP_ACK) {
1173 if (resp_code == FC_SPP_RESP_CONF)
Hannes Reinecke9f9504a2016-10-13 15:10:44 +02001174 fc_rport_error(rdata, -FC_EX_SEQ_ERR);
Bhanu Prakash Gollapudi618461c2010-06-11 16:43:54 -07001175 else
Hannes Reinecke9f9504a2016-10-13 15:10:44 +02001176 fc_rport_error_retry(rdata, -FC_EX_SEQ_ERR);
Bhanu Prakash Gollapudi618461c2010-06-11 16:43:54 -07001177 goto out;
Robert Love42e9a922008-12-09 15:10:17 -08001178 }
Bhanu Prakash Gollapudi618461c2010-06-11 16:43:54 -07001179 if (pp->prli.prli_spp_len < sizeof(pp->spp))
1180 goto out;
1181
1182 fcp_parm = ntohl(pp->spp.spp_params);
1183 if (fcp_parm & FCP_SPPF_RETRY)
1184 rdata->flags |= FC_RP_FLAGS_RETRY;
Bhanu Prakash Gollapudi75a27922011-01-28 16:05:27 -08001185 if (fcp_parm & FCP_SPPF_CONF_COMPL)
1186 rdata->flags |= FC_RP_FLAGS_CONF_REQ;
Robert Love42e9a922008-12-09 15:10:17 -08001187
Hannes Reinecke386b97b2016-10-13 15:10:46 +02001188 /*
1189 * Call prli provider if we should act as a target
1190 */
1191 prov = fc_passive_prov[rdata->spp_type];
Joe Eykholt925ceda2011-01-28 16:04:23 -08001192 if (prov) {
1193 memset(&temp_spp, 0, sizeof(temp_spp));
1194 prov->prli(rdata, pp->prli.prli_spp_len,
1195 &pp->spp, &temp_spp);
1196 }
Hannes Reinecke386b97b2016-10-13 15:10:46 +02001197 /*
1198 * Check if the image pair could be established
1199 */
1200 if (rdata->spp_type != FC_TYPE_FCP ||
Hannes Reinecke386b97b2016-10-13 15:10:46 +02001201 !(pp->spp.spp_flags & FC_SPP_EST_IMG_PAIR)) {
1202 /*
1203 * Nope; we can't use this port as a target.
1204 */
1205 fcp_parm &= ~FCP_SPPF_TARG_FCN;
1206 }
Joe Eykholtf211fa52009-08-25 14:01:01 -07001207 rdata->supported_classes = FC_COS_CLASS3;
Robert Love42e9a922008-12-09 15:10:17 -08001208 if (fcp_parm & FCP_SPPF_INIT_FCN)
1209 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1210 if (fcp_parm & FCP_SPPF_TARG_FCN)
1211 roles |= FC_RPORT_ROLE_FCP_TARGET;
1212
Joe Eykholtf211fa52009-08-25 14:01:01 -07001213 rdata->ids.roles = roles;
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001214 fc_rport_enter_rtv(rdata);
Robert Love42e9a922008-12-09 15:10:17 -08001215
1216 } else {
Hannes Reinecke57d3ec7e2016-10-13 15:10:37 +02001217 rjt = fc_frame_payload_get(fp, sizeof(*rjt));
1218 FC_RPORT_DBG(rdata, "PRLI ELS rejected, reason %x expl %x\n",
1219 rjt->er_reason, rjt->er_explan);
Hannes Reinecke9f9504a2016-10-13 15:10:44 +02001220 fc_rport_error_retry(rdata, FC_EX_ELS_RJT);
Robert Love42e9a922008-12-09 15:10:17 -08001221 }
1222
1223out:
1224 fc_frame_free(fp);
1225err:
1226 mutex_unlock(&rdata->rp_mutex);
Chad Dupuis785141c2016-09-30 11:01:16 +02001227put:
Hannes Reinecke944ef962016-10-18 10:01:39 +02001228 kref_put(&rdata->kref, fc_rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -08001229}
1230
1231/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001232 * fc_rport_enter_prli() - Send Process Login (PRLI) request
1233 * @rdata: The remote port to send the PRLI request to
Robert Love42e9a922008-12-09 15:10:17 -08001234 *
1235 * Locking Note: The rport lock is expected to be held before calling
1236 * this routine.
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001237 *
1238 * Reference counting: increments kref when sending ELS
Robert Love42e9a922008-12-09 15:10:17 -08001239 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001240static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -08001241{
Robert Love42e9a922008-12-09 15:10:17 -08001242 struct fc_lport *lport = rdata->local_port;
1243 struct {
1244 struct fc_els_prli prli;
1245 struct fc_els_spp spp;
1246 } *pp;
1247 struct fc_frame *fp;
Joe Eykholt925ceda2011-01-28 16:04:23 -08001248 struct fc4_prov *prov;
Robert Love42e9a922008-12-09 15:10:17 -08001249
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001250 /*
1251 * If the rport is one of the well known addresses
1252 * we skip PRLI and RTV and go straight to READY.
1253 */
1254 if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
1255 fc_rport_enter_ready(rdata);
1256 return;
1257 }
1258
Hannes Reinecke386b97b2016-10-13 15:10:46 +02001259 /*
1260 * And if the local port does not support the initiator function
1261 * there's no need to send a PRLI, either.
1262 */
1263 if (!(lport->service_params & FCP_SPPF_INIT_FCN)) {
1264 fc_rport_enter_ready(rdata);
1265 return;
1266 }
1267
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001268 FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
1269 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001270
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001271 fc_rport_state_enter(rdata, RPORT_ST_PRLI);
Robert Love42e9a922008-12-09 15:10:17 -08001272
1273 fp = fc_frame_alloc(lport, sizeof(*pp));
1274 if (!fp) {
Hannes Reinecke9f9504a2016-10-13 15:10:44 +02001275 fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
Robert Love42e9a922008-12-09 15:10:17 -08001276 return;
1277 }
1278
Joe Eykholt925ceda2011-01-28 16:04:23 -08001279 fc_prli_fill(lport, fp);
1280
1281 prov = fc_passive_prov[FC_TYPE_FCP];
1282 if (prov) {
1283 pp = fc_frame_payload_get(fp, sizeof(*pp));
1284 prov->prli(rdata, sizeof(pp->spp), NULL, &pp->spp);
1285 }
1286
1287 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rdata->ids.port_id,
1288 fc_host_port_id(lport->host), FC_TYPE_ELS,
1289 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1290
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001291 kref_get(&rdata->kref);
Hannes Reinecke3afd2d12016-10-18 10:01:38 +02001292 if (!fc_exch_seq_send(lport, fp, fc_rport_prli_resp,
1293 NULL, rdata, 2 * lport->r_a_tov)) {
Hannes Reinecke9f9504a2016-10-13 15:10:44 +02001294 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
Hannes Reinecke944ef962016-10-18 10:01:39 +02001295 kref_put(&rdata->kref, fc_rport_destroy);
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001296 }
Robert Love42e9a922008-12-09 15:10:17 -08001297}
1298
1299/**
Hannes Reinecke7c5a51b2016-10-13 15:10:45 +02001300 * fc_rport_rtv_resp() - Handler for Request Timeout Value (RTV) responses
Robert Love3a3b42b2009-11-03 11:47:39 -08001301 * @sp: The sequence the RTV was on
1302 * @fp: The RTV response frame
1303 * @rdata_arg: The remote port that sent the RTV response
Robert Love42e9a922008-12-09 15:10:17 -08001304 *
1305 * Many targets don't seem to support this.
1306 *
1307 * Locking Note: This function will be called without the rport lock
1308 * held, but it will lock, call an _enter_* function or fc_rport_error
1309 * and then unlock the rport.
1310 */
1311static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001312 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -08001313{
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001314 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -08001315 u8 op;
1316
Joe Eykholtf657d292009-08-25 14:03:21 -07001317 FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -08001318
Chad Dupuis785141c2016-09-30 11:01:16 +02001319 if (fp == ERR_PTR(-FC_EX_CLOSED))
1320 goto put;
1321
1322 mutex_lock(&rdata->rp_mutex);
1323
Robert Love42e9a922008-12-09 15:10:17 -08001324 if (rdata->rp_state != RPORT_ST_RTV) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001325 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
1326 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001327 if (IS_ERR(fp))
1328 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001329 goto out;
1330 }
1331
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001332 if (IS_ERR(fp)) {
Hannes Reinecke9f9504a2016-10-13 15:10:44 +02001333 fc_rport_error(rdata, PTR_ERR(fp));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001334 goto err;
1335 }
1336
Robert Love42e9a922008-12-09 15:10:17 -08001337 op = fc_frame_payload_op(fp);
1338 if (op == ELS_LS_ACC) {
1339 struct fc_els_rtv_acc *rtv;
1340 u32 toq;
1341 u32 tov;
1342
1343 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
1344 if (rtv) {
1345 toq = ntohl(rtv->rtv_toq);
1346 tov = ntohl(rtv->rtv_r_a_tov);
1347 if (tov == 0)
1348 tov = 1;
Hannes Reinecke76e72ad2016-10-13 15:10:41 +02001349 if (tov > rdata->r_a_tov)
1350 rdata->r_a_tov = tov;
Robert Love42e9a922008-12-09 15:10:17 -08001351 tov = ntohl(rtv->rtv_e_d_tov);
1352 if (toq & FC_ELS_RTV_EDRES)
1353 tov /= 1000000;
1354 if (tov == 0)
1355 tov = 1;
Hannes Reinecke76e72ad2016-10-13 15:10:41 +02001356 if (tov > rdata->e_d_tov)
1357 rdata->e_d_tov = tov;
Robert Love42e9a922008-12-09 15:10:17 -08001358 }
1359 }
1360
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001361 fc_rport_enter_ready(rdata);
Robert Love42e9a922008-12-09 15:10:17 -08001362
1363out:
1364 fc_frame_free(fp);
1365err:
1366 mutex_unlock(&rdata->rp_mutex);
Chad Dupuis785141c2016-09-30 11:01:16 +02001367put:
Hannes Reinecke944ef962016-10-18 10:01:39 +02001368 kref_put(&rdata->kref, fc_rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -08001369}
1370
1371/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001372 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
1373 * @rdata: The remote port to send the RTV request to
Robert Love42e9a922008-12-09 15:10:17 -08001374 *
1375 * Locking Note: The rport lock is expected to be held before calling
1376 * this routine.
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001377 *
1378 * Reference counting: increments kref when sending ELS
Robert Love42e9a922008-12-09 15:10:17 -08001379 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001380static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -08001381{
1382 struct fc_frame *fp;
Robert Love42e9a922008-12-09 15:10:17 -08001383 struct fc_lport *lport = rdata->local_port;
1384
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001385 FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
1386 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001387
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001388 fc_rport_state_enter(rdata, RPORT_ST_RTV);
Robert Love42e9a922008-12-09 15:10:17 -08001389
1390 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
1391 if (!fp) {
Hannes Reinecke9f9504a2016-10-13 15:10:44 +02001392 fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
Robert Love42e9a922008-12-09 15:10:17 -08001393 return;
1394 }
1395
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001396 kref_get(&rdata->kref);
Joe Eykholtf211fa52009-08-25 14:01:01 -07001397 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
Joe Eykholtb94f8952009-11-03 11:50:21 -08001398 fc_rport_rtv_resp, rdata,
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001399 2 * lport->r_a_tov)) {
Hannes Reinecke9f9504a2016-10-13 15:10:44 +02001400 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
Hannes Reinecke944ef962016-10-18 10:01:39 +02001401 kref_put(&rdata->kref, fc_rport_destroy);
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001402 }
Robert Love42e9a922008-12-09 15:10:17 -08001403}
1404
1405/**
Hannes Reinecke7c5a51b2016-10-13 15:10:45 +02001406 * fc_rport_recv_rtv_req() - Handler for Read Timeout Value (RTV) requests
1407 * @rdata: The remote port that sent the RTV request
1408 * @in_fp: The RTV request frame
1409 *
1410 * Locking Note: Called with the lport and rport locks held.
1411 */
1412static void fc_rport_recv_rtv_req(struct fc_rport_priv *rdata,
1413 struct fc_frame *in_fp)
1414{
1415 struct fc_lport *lport = rdata->local_port;
1416 struct fc_frame *fp;
1417 struct fc_els_rtv_acc *rtv;
1418 struct fc_seq_els_data rjt_data;
1419
1420 FC_RPORT_DBG(rdata, "Received RTV request\n");
1421
1422 fp = fc_frame_alloc(lport, sizeof(*rtv));
1423 if (!fp) {
1424 rjt_data.reason = ELS_RJT_UNAB;
1425 rjt_data.reason = ELS_EXPL_INSUF_RES;
Hannes Reinecke7ab24dd2016-10-18 10:01:35 +02001426 fc_seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data);
Hannes Reinecke7c5a51b2016-10-13 15:10:45 +02001427 goto drop;
1428 }
1429 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
1430 rtv->rtv_cmd = ELS_LS_ACC;
1431 rtv->rtv_r_a_tov = htonl(lport->r_a_tov);
1432 rtv->rtv_e_d_tov = htonl(lport->e_d_tov);
1433 rtv->rtv_toq = 0;
1434 fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
1435 lport->tt.frame_send(lport, fp);
1436drop:
1437 fc_frame_free(in_fp);
1438}
1439
1440/**
Joe Eykholt079ecd82010-07-20 15:20:51 -07001441 * fc_rport_logo_resp() - Handler for logout (LOGO) responses
1442 * @sp: The sequence the LOGO was on
1443 * @fp: The LOGO response frame
1444 * @lport_arg: The local port
1445 */
1446static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001447 void *rdata_arg)
Joe Eykholt079ecd82010-07-20 15:20:51 -07001448{
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001449 struct fc_rport_priv *rdata = rdata_arg;
1450 struct fc_lport *lport = rdata->local_port;
Joe Eykholt079ecd82010-07-20 15:20:51 -07001451
1452 FC_RPORT_ID_DBG(lport, fc_seq_exch(sp)->did,
1453 "Received a LOGO %s\n", fc_els_resp_type(fp));
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001454 if (!IS_ERR(fp))
1455 fc_frame_free(fp);
Hannes Reinecke944ef962016-10-18 10:01:39 +02001456 kref_put(&rdata->kref, fc_rport_destroy);
Joe Eykholt079ecd82010-07-20 15:20:51 -07001457}
1458
1459/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001460 * fc_rport_enter_logo() - Send a logout (LOGO) request
1461 * @rdata: The remote port to send the LOGO request to
Robert Love42e9a922008-12-09 15:10:17 -08001462 *
1463 * Locking Note: The rport lock is expected to be held before calling
1464 * this routine.
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001465 *
1466 * Reference counting: increments kref when sending ELS
Robert Love42e9a922008-12-09 15:10:17 -08001467 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001468static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -08001469{
Robert Love42e9a922008-12-09 15:10:17 -08001470 struct fc_lport *lport = rdata->local_port;
1471 struct fc_frame *fp;
1472
Joe Eykholt079ecd82010-07-20 15:20:51 -07001473 FC_RPORT_DBG(rdata, "Port sending LOGO from %s state\n",
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001474 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001475
Robert Love42e9a922008-12-09 15:10:17 -08001476 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
Joe Eykholt079ecd82010-07-20 15:20:51 -07001477 if (!fp)
Robert Love42e9a922008-12-09 15:10:17 -08001478 return;
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001479 kref_get(&rdata->kref);
1480 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
1481 fc_rport_logo_resp, rdata, 0))
Hannes Reinecke944ef962016-10-18 10:01:39 +02001482 kref_put(&rdata->kref, fc_rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -08001483}
1484
Robert Love42e9a922008-12-09 15:10:17 -08001485/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001486 * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
1487 * @sp: The sequence the ADISC response was on
1488 * @fp: The ADISC response frame
1489 * @rdata_arg: The remote port that sent the ADISC response
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001490 *
1491 * Locking Note: This function will be called without the rport lock
1492 * held, but it will lock, call an _enter_* function or fc_rport_error
1493 * and then unlock the rport.
1494 */
1495static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
Robert Love3a3b42b2009-11-03 11:47:39 -08001496 void *rdata_arg)
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001497{
1498 struct fc_rport_priv *rdata = rdata_arg;
1499 struct fc_els_adisc *adisc;
1500 u8 op;
1501
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001502 FC_RPORT_DBG(rdata, "Received a ADISC response\n");
1503
Chad Dupuis785141c2016-09-30 11:01:16 +02001504 if (fp == ERR_PTR(-FC_EX_CLOSED))
1505 goto put;
1506
1507 mutex_lock(&rdata->rp_mutex);
1508
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001509 if (rdata->rp_state != RPORT_ST_ADISC) {
1510 FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
1511 fc_rport_state(rdata));
1512 if (IS_ERR(fp))
1513 goto err;
1514 goto out;
1515 }
1516
1517 if (IS_ERR(fp)) {
Hannes Reinecke9f9504a2016-10-13 15:10:44 +02001518 fc_rport_error(rdata, PTR_ERR(fp));
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001519 goto err;
1520 }
1521
1522 /*
1523 * If address verification failed. Consider us logged out of the rport.
1524 * Since the rport is still in discovery, we want to be
1525 * logged in, so go to PLOGI state. Otherwise, go back to READY.
1526 */
1527 op = fc_frame_payload_op(fp);
1528 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1529 if (op != ELS_LS_ACC || !adisc ||
1530 ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
1531 get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
1532 get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
1533 FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
Joe Eykholta7b12a22010-07-20 15:20:08 -07001534 fc_rport_enter_flogi(rdata);
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001535 } else {
1536 FC_RPORT_DBG(rdata, "ADISC OK\n");
1537 fc_rport_enter_ready(rdata);
1538 }
1539out:
1540 fc_frame_free(fp);
1541err:
1542 mutex_unlock(&rdata->rp_mutex);
Chad Dupuis785141c2016-09-30 11:01:16 +02001543put:
Hannes Reinecke944ef962016-10-18 10:01:39 +02001544 kref_put(&rdata->kref, fc_rport_destroy);
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001545}
1546
1547/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001548 * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
1549 * @rdata: The remote port to send the ADISC request to
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001550 *
1551 * Locking Note: The rport lock is expected to be held before calling
1552 * this routine.
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001553 *
1554 * Reference counting: increments kref when sending ELS
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001555 */
1556static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
1557{
1558 struct fc_lport *lport = rdata->local_port;
1559 struct fc_frame *fp;
1560
1561 FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
1562 fc_rport_state(rdata));
1563
1564 fc_rport_state_enter(rdata, RPORT_ST_ADISC);
1565
1566 fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
1567 if (!fp) {
Hannes Reinecke9f9504a2016-10-13 15:10:44 +02001568 fc_rport_error_retry(rdata, -FC_EX_ALLOC_ERR);
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001569 return;
1570 }
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001571 kref_get(&rdata->kref);
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001572 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
Joe Eykholtb94f8952009-11-03 11:50:21 -08001573 fc_rport_adisc_resp, rdata,
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001574 2 * lport->r_a_tov)) {
Hannes Reinecke9f9504a2016-10-13 15:10:44 +02001575 fc_rport_error_retry(rdata, -FC_EX_XMIT_ERR);
Hannes Reinecke944ef962016-10-18 10:01:39 +02001576 kref_put(&rdata->kref, fc_rport_destroy);
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001577 }
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001578}
1579
1580/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001581 * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
1582 * @rdata: The remote port that sent the ADISC request
Robert Love3a3b42b2009-11-03 11:47:39 -08001583 * @in_fp: The ADISC request frame
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001584 *
1585 * Locking Note: Called with the lport and rport locks held.
1586 */
1587static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
Joe Eykholt922611562010-07-20 15:21:12 -07001588 struct fc_frame *in_fp)
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001589{
1590 struct fc_lport *lport = rdata->local_port;
1591 struct fc_frame *fp;
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001592 struct fc_els_adisc *adisc;
1593 struct fc_seq_els_data rjt_data;
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001594
1595 FC_RPORT_DBG(rdata, "Received ADISC request\n");
1596
1597 adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
1598 if (!adisc) {
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001599 rjt_data.reason = ELS_RJT_PROT;
1600 rjt_data.explan = ELS_EXPL_INV_LEN;
Hannes Reinecke7ab24dd2016-10-18 10:01:35 +02001601 fc_seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data);
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001602 goto drop;
1603 }
1604
1605 fp = fc_frame_alloc(lport, sizeof(*adisc));
1606 if (!fp)
1607 goto drop;
1608 fc_adisc_fill(lport, fp);
1609 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1610 adisc->adisc_cmd = ELS_LS_ACC;
Joe Eykholt24f089e2010-07-20 15:21:01 -07001611 fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
1612 lport->tt.frame_send(lport, fp);
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001613drop:
1614 fc_frame_free(in_fp);
1615}
1616
1617/**
Yi Zou63e27fb2009-11-20 14:55:24 -08001618 * fc_rport_recv_rls_req() - Handle received Read Link Status request
1619 * @rdata: The remote port that sent the RLS request
Yi Zou63e27fb2009-11-20 14:55:24 -08001620 * @rx_fp: The PRLI request frame
1621 *
1622 * Locking Note: The rport lock is expected to be held before calling
1623 * this function.
1624 */
1625static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
Joe Eykholt922611562010-07-20 15:21:12 -07001626 struct fc_frame *rx_fp)
Yi Zou63e27fb2009-11-20 14:55:24 -08001627
1628{
1629 struct fc_lport *lport = rdata->local_port;
1630 struct fc_frame *fp;
Yi Zou63e27fb2009-11-20 14:55:24 -08001631 struct fc_els_rls *rls;
1632 struct fc_els_rls_resp *rsp;
1633 struct fc_els_lesb *lesb;
1634 struct fc_seq_els_data rjt_data;
1635 struct fc_host_statistics *hst;
Yi Zou63e27fb2009-11-20 14:55:24 -08001636
1637 FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
1638 fc_rport_state(rdata));
1639
1640 rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
1641 if (!rls) {
1642 rjt_data.reason = ELS_RJT_PROT;
1643 rjt_data.explan = ELS_EXPL_INV_LEN;
1644 goto out_rjt;
1645 }
1646
1647 fp = fc_frame_alloc(lport, sizeof(*rsp));
1648 if (!fp) {
1649 rjt_data.reason = ELS_RJT_UNAB;
1650 rjt_data.explan = ELS_EXPL_INSUF_RES;
1651 goto out_rjt;
1652 }
1653
1654 rsp = fc_frame_payload_get(fp, sizeof(*rsp));
1655 memset(rsp, 0, sizeof(*rsp));
1656 rsp->rls_cmd = ELS_LS_ACC;
1657 lesb = &rsp->rls_lesb;
1658 if (lport->tt.get_lesb) {
1659 /* get LESB from LLD if it supports it */
1660 lport->tt.get_lesb(lport, lesb);
1661 } else {
1662 fc_get_host_stats(lport->host);
1663 hst = &lport->host_stats;
1664 lesb->lesb_link_fail = htonl(hst->link_failure_count);
1665 lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
1666 lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
1667 lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
1668 lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
1669 lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
1670 }
1671
Joe Eykholt24f089e2010-07-20 15:21:01 -07001672 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1673 lport->tt.frame_send(lport, fp);
Yi Zou63e27fb2009-11-20 14:55:24 -08001674 goto out;
1675
1676out_rjt:
Hannes Reinecke7ab24dd2016-10-18 10:01:35 +02001677 fc_seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
Yi Zou63e27fb2009-11-20 14:55:24 -08001678out:
1679 fc_frame_free(rx_fp);
1680}
1681
1682/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001683 * fc_rport_recv_els_req() - Handler for validated ELS requests
1684 * @lport: The local port that received the ELS request
Robert Love3a3b42b2009-11-03 11:47:39 -08001685 * @fp: The ELS request frame
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001686 *
1687 * Handle incoming ELS requests that require port login.
1688 * The ELS opcode has already been validated by the caller.
Robert Love42e9a922008-12-09 15:10:17 -08001689 *
Joe Eykholt131203a2009-08-25 14:03:10 -07001690 * Locking Note: Called with the lport lock held.
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001691 *
1692 * Reference counting: does not modify kref
Robert Love42e9a922008-12-09 15:10:17 -08001693 */
Joe Eykholt922611562010-07-20 15:21:12 -07001694static void fc_rport_recv_els_req(struct fc_lport *lport, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -08001695{
Joe Eykholt131203a2009-08-25 14:03:10 -07001696 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -08001697 struct fc_seq_els_data els_data;
Robert Love42e9a922008-12-09 15:10:17 -08001698
Hannes Reineckee87b7772016-10-18 10:01:40 +02001699 rdata = fc_rport_lookup(lport, fc_frame_sid(fp));
Hannes Reinecke57d3ec7e2016-10-13 15:10:37 +02001700 if (!rdata) {
1701 FC_RPORT_ID_DBG(lport, fc_frame_sid(fp),
1702 "Received ELS 0x%02x from non-logged-in port\n",
1703 fc_frame_payload_op(fp));
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001704 goto reject;
Hannes Reinecke57d3ec7e2016-10-13 15:10:37 +02001705 }
Hannes Reineckebaa67192016-05-24 08:11:58 +02001706
Joe Eykholt131203a2009-08-25 14:03:10 -07001707 mutex_lock(&rdata->rp_mutex);
1708
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001709 switch (rdata->rp_state) {
1710 case RPORT_ST_PRLI:
1711 case RPORT_ST_RTV:
1712 case RPORT_ST_READY:
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001713 case RPORT_ST_ADISC:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001714 break;
Hannes Reinecke8acf1b52016-10-13 15:10:47 +02001715 case RPORT_ST_PLOGI:
1716 if (fc_frame_payload_op(fp) == ELS_PRLI) {
1717 FC_RPORT_DBG(rdata, "Reject ELS PRLI "
1718 "while in state %s\n",
1719 fc_rport_state(rdata));
1720 mutex_unlock(&rdata->rp_mutex);
Hannes Reinecke944ef962016-10-18 10:01:39 +02001721 kref_put(&rdata->kref, fc_rport_destroy);
Hannes Reinecke8acf1b52016-10-13 15:10:47 +02001722 goto busy;
1723 }
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001724 default:
Hannes Reinecke57d3ec7e2016-10-13 15:10:37 +02001725 FC_RPORT_DBG(rdata,
1726 "Reject ELS 0x%02x while in state %s\n",
1727 fc_frame_payload_op(fp), fc_rport_state(rdata));
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001728 mutex_unlock(&rdata->rp_mutex);
Hannes Reinecke944ef962016-10-18 10:01:39 +02001729 kref_put(&rdata->kref, fc_rport_destroy);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001730 goto reject;
1731 }
1732
1733 switch (fc_frame_payload_op(fp)) {
Joe Eykholt131203a2009-08-25 14:03:10 -07001734 case ELS_PRLI:
Joe Eykholt922611562010-07-20 15:21:12 -07001735 fc_rport_recv_prli_req(rdata, fp);
Joe Eykholt131203a2009-08-25 14:03:10 -07001736 break;
1737 case ELS_PRLO:
Joe Eykholt922611562010-07-20 15:21:12 -07001738 fc_rport_recv_prlo_req(rdata, fp);
Joe Eykholt131203a2009-08-25 14:03:10 -07001739 break;
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001740 case ELS_ADISC:
Joe Eykholt922611562010-07-20 15:21:12 -07001741 fc_rport_recv_adisc_req(rdata, fp);
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001742 break;
Joe Eykholt131203a2009-08-25 14:03:10 -07001743 case ELS_RRQ:
Hannes Reinecke7ab24dd2016-10-18 10:01:35 +02001744 fc_seq_els_rsp_send(fp, ELS_RRQ, NULL);
Joe Eykholt922611562010-07-20 15:21:12 -07001745 fc_frame_free(fp);
Joe Eykholt131203a2009-08-25 14:03:10 -07001746 break;
1747 case ELS_REC:
Hannes Reinecke7ab24dd2016-10-18 10:01:35 +02001748 fc_seq_els_rsp_send(fp, ELS_REC, NULL);
Joe Eykholt922611562010-07-20 15:21:12 -07001749 fc_frame_free(fp);
Joe Eykholt131203a2009-08-25 14:03:10 -07001750 break;
Yi Zou63e27fb2009-11-20 14:55:24 -08001751 case ELS_RLS:
Joe Eykholt922611562010-07-20 15:21:12 -07001752 fc_rport_recv_rls_req(rdata, fp);
Yi Zou63e27fb2009-11-20 14:55:24 -08001753 break;
Hannes Reinecke7c5a51b2016-10-13 15:10:45 +02001754 case ELS_RTV:
1755 fc_rport_recv_rtv_req(rdata, fp);
1756 break;
Joe Eykholt131203a2009-08-25 14:03:10 -07001757 default:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001758 fc_frame_free(fp); /* can't happen */
Joe Eykholt131203a2009-08-25 14:03:10 -07001759 break;
Robert Love42e9a922008-12-09 15:10:17 -08001760 }
1761
1762 mutex_unlock(&rdata->rp_mutex);
Hannes Reinecke944ef962016-10-18 10:01:39 +02001763 kref_put(&rdata->kref, fc_rport_destroy);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001764 return;
1765
1766reject:
Joe Eykholt922611562010-07-20 15:21:12 -07001767 els_data.reason = ELS_RJT_UNAB;
1768 els_data.explan = ELS_EXPL_PLOGI_REQD;
Hannes Reinecke7ab24dd2016-10-18 10:01:35 +02001769 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001770 fc_frame_free(fp);
Hannes Reinecke8acf1b52016-10-13 15:10:47 +02001771 return;
1772
1773busy:
1774 els_data.reason = ELS_RJT_BUSY;
1775 els_data.explan = ELS_EXPL_NONE;
Hannes Reinecke7ab24dd2016-10-18 10:01:35 +02001776 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
Hannes Reinecke8acf1b52016-10-13 15:10:47 +02001777 fc_frame_free(fp);
1778 return;
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001779}
1780
1781/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001782 * fc_rport_recv_req() - Handler for requests
Robert Love3a3b42b2009-11-03 11:47:39 -08001783 * @lport: The local port that received the request
Joe Eykholt922611562010-07-20 15:21:12 -07001784 * @fp: The request frame
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001785 *
1786 * Locking Note: Called with the lport lock held.
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001787 *
1788 * Reference counting: does not modify kref
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001789 */
Hannes Reineckee76ee652016-10-18 10:01:45 +02001790void fc_rport_recv_req(struct fc_lport *lport, struct fc_frame *fp)
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001791{
1792 struct fc_seq_els_data els_data;
1793
1794 /*
Joe Eykholta7b12a22010-07-20 15:20:08 -07001795 * Handle FLOGI, PLOGI and LOGO requests separately, since they
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001796 * don't require prior login.
1797 * Check for unsupported opcodes first and reject them.
1798 * For some ops, it would be incorrect to reject with "PLOGI required".
1799 */
1800 switch (fc_frame_payload_op(fp)) {
Joe Eykholta7b12a22010-07-20 15:20:08 -07001801 case ELS_FLOGI:
Joe Eykholt922611562010-07-20 15:21:12 -07001802 fc_rport_recv_flogi_req(lport, fp);
Joe Eykholta7b12a22010-07-20 15:20:08 -07001803 break;
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001804 case ELS_PLOGI:
Joe Eykholt922611562010-07-20 15:21:12 -07001805 fc_rport_recv_plogi_req(lport, fp);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001806 break;
1807 case ELS_LOGO:
Joe Eykholt922611562010-07-20 15:21:12 -07001808 fc_rport_recv_logo_req(lport, fp);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001809 break;
1810 case ELS_PRLI:
1811 case ELS_PRLO:
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001812 case ELS_ADISC:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001813 case ELS_RRQ:
1814 case ELS_REC:
Yi Zou63e27fb2009-11-20 14:55:24 -08001815 case ELS_RLS:
Hannes Reinecke7c5a51b2016-10-13 15:10:45 +02001816 case ELS_RTV:
Joe Eykholt922611562010-07-20 15:21:12 -07001817 fc_rport_recv_els_req(lport, fp);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001818 break;
1819 default:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001820 els_data.reason = ELS_RJT_UNSUP;
1821 els_data.explan = ELS_EXPL_NONE;
Hannes Reinecke7ab24dd2016-10-18 10:01:35 +02001822 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
Joe Eykholt922611562010-07-20 15:21:12 -07001823 fc_frame_free(fp);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001824 break;
1825 }
Robert Love42e9a922008-12-09 15:10:17 -08001826}
Hannes Reineckee76ee652016-10-18 10:01:45 +02001827EXPORT_SYMBOL(fc_rport_recv_req);
Robert Love42e9a922008-12-09 15:10:17 -08001828
1829/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001830 * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
1831 * @lport: The local port that received the PLOGI request
Robert Love3a3b42b2009-11-03 11:47:39 -08001832 * @rx_fp: The PLOGI request frame
Robert Love42e9a922008-12-09 15:10:17 -08001833 *
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001834 * Locking Note: The rport lock is held before calling this function.
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001835 *
1836 * Reference counting: increments kref on return
Robert Love42e9a922008-12-09 15:10:17 -08001837 */
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001838static void fc_rport_recv_plogi_req(struct fc_lport *lport,
Joe Eykholt922611562010-07-20 15:21:12 -07001839 struct fc_frame *rx_fp)
Robert Love42e9a922008-12-09 15:10:17 -08001840{
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001841 struct fc_disc *disc;
1842 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -08001843 struct fc_frame *fp = rx_fp;
Robert Love42e9a922008-12-09 15:10:17 -08001844 struct fc_els_flogi *pl;
1845 struct fc_seq_els_data rjt_data;
Joe Eykholt24f089e2010-07-20 15:21:01 -07001846 u32 sid;
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001847
Joe Eykholt251748a2010-07-20 15:20:56 -07001848 sid = fc_frame_sid(fp);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001849
1850 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
1851
Robert Love42e9a922008-12-09 15:10:17 -08001852 pl = fc_frame_payload_get(fp, sizeof(*pl));
1853 if (!pl) {
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001854 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
1855 rjt_data.reason = ELS_RJT_PROT;
1856 rjt_data.explan = ELS_EXPL_INV_LEN;
1857 goto reject;
Robert Love42e9a922008-12-09 15:10:17 -08001858 }
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001859
1860 disc = &lport->disc;
1861 mutex_lock(&disc->disc_mutex);
Hannes Reinecke25800642016-10-18 10:01:42 +02001862 rdata = fc_rport_create(lport, sid);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001863 if (!rdata) {
1864 mutex_unlock(&disc->disc_mutex);
1865 rjt_data.reason = ELS_RJT_UNAB;
1866 rjt_data.explan = ELS_EXPL_INSUF_RES;
1867 goto reject;
1868 }
1869
1870 mutex_lock(&rdata->rp_mutex);
1871 mutex_unlock(&disc->disc_mutex);
1872
1873 rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1874 rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
Robert Love42e9a922008-12-09 15:10:17 -08001875
1876 /*
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001877 * If the rport was just created, possibly due to the incoming PLOGI,
Robert Love42e9a922008-12-09 15:10:17 -08001878 * set the state appropriately and accept the PLOGI.
1879 *
1880 * If we had also sent a PLOGI, and if the received PLOGI is from a
1881 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1882 * "command already in progress".
1883 *
1884 * XXX TBD: If the session was ready before, the PLOGI should result in
1885 * all outstanding exchanges being reset.
1886 */
1887 switch (rdata->rp_state) {
1888 case RPORT_ST_INIT:
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001889 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
Robert Love42e9a922008-12-09 15:10:17 -08001890 break;
Joe Eykholta7b12a22010-07-20 15:20:08 -07001891 case RPORT_ST_PLOGI_WAIT:
1892 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI_WAIT state\n");
1893 break;
Robert Love42e9a922008-12-09 15:10:17 -08001894 case RPORT_ST_PLOGI:
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001895 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1896 if (rdata->ids.port_name < lport->wwpn) {
1897 mutex_unlock(&rdata->rp_mutex);
1898 rjt_data.reason = ELS_RJT_INPROG;
1899 rjt_data.explan = ELS_EXPL_NONE;
1900 goto reject;
1901 }
Robert Love42e9a922008-12-09 15:10:17 -08001902 break;
1903 case RPORT_ST_PRLI:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001904 case RPORT_ST_RTV:
Robert Love42e9a922008-12-09 15:10:17 -08001905 case RPORT_ST_READY:
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001906 case RPORT_ST_ADISC:
1907 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
1908 "- ignored for now\n", rdata->rp_state);
1909 /* XXX TBD - should reset */
Robert Love42e9a922008-12-09 15:10:17 -08001910 break;
Joe Eykholta7b12a22010-07-20 15:20:08 -07001911 case RPORT_ST_FLOGI:
Joe Eykholt14194052009-07-29 17:04:43 -07001912 case RPORT_ST_DELETE:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001913 FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
1914 fc_rport_state(rdata));
1915 mutex_unlock(&rdata->rp_mutex);
1916 rjt_data.reason = ELS_RJT_BUSY;
1917 rjt_data.explan = ELS_EXPL_NONE;
1918 goto reject;
Robert Love42e9a922008-12-09 15:10:17 -08001919 }
Mark Rustade0335f62013-05-18 04:01:36 +00001920 if (!fc_rport_compatible_roles(lport, rdata)) {
1921 FC_RPORT_DBG(rdata, "Received PLOGI for incompatible role\n");
1922 mutex_unlock(&rdata->rp_mutex);
1923 rjt_data.reason = ELS_RJT_LOGIC;
1924 rjt_data.explan = ELS_EXPL_NONE;
1925 goto reject;
1926 }
Robert Love42e9a922008-12-09 15:10:17 -08001927
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001928 /*
1929 * Get session payload size from incoming PLOGI.
1930 */
1931 rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
Robert Love42e9a922008-12-09 15:10:17 -08001932
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001933 /*
1934 * Send LS_ACC. If this fails, the originator should retry.
1935 */
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001936 fp = fc_frame_alloc(lport, sizeof(*pl));
1937 if (!fp)
1938 goto out;
Robert Love42e9a922008-12-09 15:10:17 -08001939
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001940 fc_plogi_fill(lport, fp, ELS_LS_ACC);
Joe Eykholt24f089e2010-07-20 15:21:01 -07001941 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1942 lport->tt.frame_send(lport, fp);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001943 fc_rport_enter_prli(rdata);
1944out:
1945 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt24f089e2010-07-20 15:21:01 -07001946 fc_frame_free(rx_fp);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001947 return;
1948
1949reject:
Hannes Reinecke7ab24dd2016-10-18 10:01:35 +02001950 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001951 fc_frame_free(fp);
Robert Love42e9a922008-12-09 15:10:17 -08001952}
1953
1954/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001955 * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
1956 * @rdata: The remote port that sent the PRLI request
Robert Love3a3b42b2009-11-03 11:47:39 -08001957 * @rx_fp: The PRLI request frame
Robert Love42e9a922008-12-09 15:10:17 -08001958 *
Bart Van Asschec1d45422013-08-14 15:31:52 +00001959 * Locking Note: The rport lock is expected to be held before calling
Robert Love42e9a922008-12-09 15:10:17 -08001960 * this function.
1961 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001962static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
Joe Eykholt922611562010-07-20 15:21:12 -07001963 struct fc_frame *rx_fp)
Robert Love42e9a922008-12-09 15:10:17 -08001964{
Robert Love42e9a922008-12-09 15:10:17 -08001965 struct fc_lport *lport = rdata->local_port;
Robert Love42e9a922008-12-09 15:10:17 -08001966 struct fc_frame *fp;
Robert Love42e9a922008-12-09 15:10:17 -08001967 struct {
1968 struct fc_els_prli prli;
1969 struct fc_els_spp spp;
1970 } *pp;
1971 struct fc_els_spp *rspp; /* request service param page */
1972 struct fc_els_spp *spp; /* response spp */
1973 unsigned int len;
1974 unsigned int plen;
Robert Love42e9a922008-12-09 15:10:17 -08001975 enum fc_els_spp_resp resp;
1976 struct fc_seq_els_data rjt_data;
Joe Eykholt96ad8462011-01-28 16:04:02 -08001977 struct fc4_prov *prov;
Robert Love42e9a922008-12-09 15:10:17 -08001978
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001979 FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1980 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001981
Joe Eykholt251748a2010-07-20 15:20:56 -07001982 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
Robert Love42e9a922008-12-09 15:10:17 -08001983 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
Joe Eykholta2f6a022010-03-12 16:07:36 -08001984 if (!pp)
1985 goto reject_len;
1986 plen = ntohs(pp->prli.prli_len);
1987 if ((plen % 4) != 0 || plen > len || plen < 16)
1988 goto reject_len;
1989 if (plen < len)
1990 len = plen;
1991 plen = pp->prli.prli_spp_len;
1992 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1993 plen > len || len < sizeof(*pp) || plen < 12)
1994 goto reject_len;
1995 rspp = &pp->spp;
1996
1997 fp = fc_frame_alloc(lport, len);
1998 if (!fp) {
1999 rjt_data.reason = ELS_RJT_UNAB;
2000 rjt_data.explan = ELS_EXPL_INSUF_RES;
2001 goto reject;
Robert Love42e9a922008-12-09 15:10:17 -08002002 }
Joe Eykholta2f6a022010-03-12 16:07:36 -08002003 pp = fc_frame_payload_get(fp, len);
2004 WARN_ON(!pp);
2005 memset(pp, 0, len);
2006 pp->prli.prli_cmd = ELS_LS_ACC;
2007 pp->prli.prli_spp_len = plen;
2008 pp->prli.prli_len = htons(len);
2009 len -= sizeof(struct fc_els_prli);
Robert Love42e9a922008-12-09 15:10:17 -08002010
Joe Eykholta2f6a022010-03-12 16:07:36 -08002011 /*
2012 * Go through all the service parameter pages and build
2013 * response. If plen indicates longer SPP than standard,
2014 * use that. The entire response has been pre-cleared above.
2015 */
2016 spp = &pp->spp;
Joe Eykholt96ad8462011-01-28 16:04:02 -08002017 mutex_lock(&fc_prov_mutex);
Joe Eykholta2f6a022010-03-12 16:07:36 -08002018 while (len >= plen) {
Bhanu Prakash Gollapudi75a27922011-01-28 16:05:27 -08002019 rdata->spp_type = rspp->spp_type;
Joe Eykholta2f6a022010-03-12 16:07:36 -08002020 spp->spp_type = rspp->spp_type;
2021 spp->spp_type_ext = rspp->spp_type_ext;
Joe Eykholt96ad8462011-01-28 16:04:02 -08002022 resp = 0;
Robert Love42e9a922008-12-09 15:10:17 -08002023
Joe Eykholt96ad8462011-01-28 16:04:02 -08002024 if (rspp->spp_type < FC_FC4_PROV_SIZE) {
Hannes Reinecke386b97b2016-10-13 15:10:46 +02002025 enum fc_els_spp_resp active = 0, passive = 0;
2026
Joe Eykholt96ad8462011-01-28 16:04:02 -08002027 prov = fc_active_prov[rspp->spp_type];
2028 if (prov)
Hannes Reinecke386b97b2016-10-13 15:10:46 +02002029 active = prov->prli(rdata, plen, rspp, spp);
Joe Eykholt96ad8462011-01-28 16:04:02 -08002030 prov = fc_passive_prov[rspp->spp_type];
Hannes Reinecke386b97b2016-10-13 15:10:46 +02002031 if (prov)
Joe Eykholt96ad8462011-01-28 16:04:02 -08002032 passive = prov->prli(rdata, plen, rspp, spp);
Hannes Reinecke386b97b2016-10-13 15:10:46 +02002033 if (!active || passive == FC_SPP_RESP_ACK)
2034 resp = passive;
2035 else
2036 resp = active;
2037 FC_RPORT_DBG(rdata, "PRLI rspp type %x "
2038 "active %x passive %x\n",
2039 rspp->spp_type, active, passive);
Joe Eykholt96ad8462011-01-28 16:04:02 -08002040 }
2041 if (!resp) {
2042 if (spp->spp_flags & FC_SPP_EST_IMG_PAIR)
2043 resp |= FC_SPP_RESP_CONF;
2044 else
2045 resp |= FC_SPP_RESP_INVL;
Robert Love42e9a922008-12-09 15:10:17 -08002046 }
Joe Eykholta2f6a022010-03-12 16:07:36 -08002047 spp->spp_flags |= resp;
2048 len -= plen;
2049 rspp = (struct fc_els_spp *)((char *)rspp + plen);
2050 spp = (struct fc_els_spp *)((char *)spp + plen);
Robert Love42e9a922008-12-09 15:10:17 -08002051 }
Joe Eykholt96ad8462011-01-28 16:04:02 -08002052 mutex_unlock(&fc_prov_mutex);
Joe Eykholta2f6a022010-03-12 16:07:36 -08002053
2054 /*
2055 * Send LS_ACC. If this fails, the originator should retry.
2056 */
Joe Eykholt24f089e2010-07-20 15:21:01 -07002057 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
2058 lport->tt.frame_send(lport, fp);
Joe Eykholta2f6a022010-03-12 16:07:36 -08002059
Joe Eykholta2f6a022010-03-12 16:07:36 -08002060 goto drop;
2061
2062reject_len:
2063 rjt_data.reason = ELS_RJT_PROT;
2064 rjt_data.explan = ELS_EXPL_INV_LEN;
2065reject:
Hannes Reinecke7ab24dd2016-10-18 10:01:35 +02002066 fc_seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
Joe Eykholta2f6a022010-03-12 16:07:36 -08002067drop:
Robert Love42e9a922008-12-09 15:10:17 -08002068 fc_frame_free(rx_fp);
2069}
2070
2071/**
Robert Love3a3b42b2009-11-03 11:47:39 -08002072 * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
2073 * @rdata: The remote port that sent the PRLO request
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07002074 * @rx_fp: The PRLO request frame
Robert Love42e9a922008-12-09 15:10:17 -08002075 *
Bart Van Asschec1d45422013-08-14 15:31:52 +00002076 * Locking Note: The rport lock is expected to be held before calling
Robert Love42e9a922008-12-09 15:10:17 -08002077 * this function.
2078 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07002079static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07002080 struct fc_frame *rx_fp)
Robert Love42e9a922008-12-09 15:10:17 -08002081{
Robert Love42e9a922008-12-09 15:10:17 -08002082 struct fc_lport *lport = rdata->local_port;
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07002083 struct fc_frame *fp;
2084 struct {
2085 struct fc_els_prlo prlo;
2086 struct fc_els_spp spp;
2087 } *pp;
2088 struct fc_els_spp *rspp; /* request service param page */
2089 struct fc_els_spp *spp; /* response spp */
2090 unsigned int len;
2091 unsigned int plen;
Robert Love42e9a922008-12-09 15:10:17 -08002092 struct fc_seq_els_data rjt_data;
2093
Joe Eykholt9fb9d322009-08-25 14:00:50 -07002094 FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
2095 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08002096
Joe Eykholt251748a2010-07-20 15:20:56 -07002097 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07002098 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
2099 if (!pp)
2100 goto reject_len;
2101 plen = ntohs(pp->prlo.prlo_len);
2102 if (plen != 20)
2103 goto reject_len;
2104 if (plen < len)
2105 len = plen;
2106
2107 rspp = &pp->spp;
2108
2109 fp = fc_frame_alloc(lport, len);
2110 if (!fp) {
2111 rjt_data.reason = ELS_RJT_UNAB;
2112 rjt_data.explan = ELS_EXPL_INSUF_RES;
2113 goto reject;
2114 }
2115
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07002116 pp = fc_frame_payload_get(fp, len);
2117 WARN_ON(!pp);
2118 memset(pp, 0, len);
2119 pp->prlo.prlo_cmd = ELS_LS_ACC;
2120 pp->prlo.prlo_obs = 0x10;
2121 pp->prlo.prlo_len = htons(len);
2122 spp = &pp->spp;
2123 spp->spp_type = rspp->spp_type;
2124 spp->spp_type_ext = rspp->spp_type_ext;
2125 spp->spp_flags = FC_SPP_RESP_ACK;
2126
Hannes Reinecke166f3102016-08-05 14:55:00 +02002127 fc_rport_enter_prli(rdata);
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07002128
Joe Eykholt922611562010-07-20 15:21:12 -07002129 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
2130 lport->tt.frame_send(lport, fp);
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07002131 goto drop;
2132
2133reject_len:
2134 rjt_data.reason = ELS_RJT_PROT;
2135 rjt_data.explan = ELS_EXPL_INV_LEN;
2136reject:
Hannes Reinecke7ab24dd2016-10-18 10:01:35 +02002137 fc_seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07002138drop:
2139 fc_frame_free(rx_fp);
Robert Love42e9a922008-12-09 15:10:17 -08002140}
2141
2142/**
Robert Love3a3b42b2009-11-03 11:47:39 -08002143 * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
2144 * @lport: The local port that received the LOGO request
Robert Love3a3b42b2009-11-03 11:47:39 -08002145 * @fp: The LOGO request frame
Robert Love42e9a922008-12-09 15:10:17 -08002146 *
Bart Van Asschec1d45422013-08-14 15:31:52 +00002147 * Locking Note: The rport lock is expected to be held before calling
Robert Love42e9a922008-12-09 15:10:17 -08002148 * this function.
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02002149 *
2150 * Reference counting: drops kref on return
Robert Love42e9a922008-12-09 15:10:17 -08002151 */
Joe Eykholt922611562010-07-20 15:21:12 -07002152static void fc_rport_recv_logo_req(struct fc_lport *lport, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -08002153{
Joe Eykholt83fe6a92009-08-25 14:03:31 -07002154 struct fc_rport_priv *rdata;
2155 u32 sid;
Robert Love42e9a922008-12-09 15:10:17 -08002156
Hannes Reinecke7ab24dd2016-10-18 10:01:35 +02002157 fc_seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
Joe Eykholtfeab4ae2009-08-25 14:03:36 -07002158
Joe Eykholt251748a2010-07-20 15:20:56 -07002159 sid = fc_frame_sid(fp);
Robert Love42e9a922008-12-09 15:10:17 -08002160
Hannes Reineckee87b7772016-10-18 10:01:40 +02002161 rdata = fc_rport_lookup(lport, sid);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07002162 if (rdata) {
2163 mutex_lock(&rdata->rp_mutex);
2164 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
2165 fc_rport_state(rdata));
Joe Eykholtfeab4ae2009-08-25 14:03:36 -07002166
Hannes Reinecke649eb862016-08-05 14:55:02 +02002167 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07002168 mutex_unlock(&rdata->rp_mutex);
Hannes Reinecke944ef962016-10-18 10:01:39 +02002169 kref_put(&rdata->kref, fc_rport_destroy);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07002170 } else
2171 FC_RPORT_ID_DBG(lport, sid,
2172 "Received LOGO from non-logged-in port\n");
Robert Love42e9a922008-12-09 15:10:17 -08002173 fc_frame_free(fp);
2174}
2175
Robert Love3a3b42b2009-11-03 11:47:39 -08002176/**
2177 * fc_rport_flush_queue() - Flush the rport_event_queue
2178 */
Hannes Reinecke5922a952016-10-18 10:01:46 +02002179void fc_rport_flush_queue(void)
Robert Love42e9a922008-12-09 15:10:17 -08002180{
2181 flush_workqueue(rport_event_queue);
2182}
Hannes Reinecke5922a952016-10-18 10:01:46 +02002183EXPORT_SYMBOL(fc_rport_flush_queue);
Robert Love42e9a922008-12-09 15:10:17 -08002184
Robert Love3a3b42b2009-11-03 11:47:39 -08002185/**
Joe Eykholt96ad8462011-01-28 16:04:02 -08002186 * fc_rport_fcp_prli() - Handle incoming PRLI for the FCP initiator.
2187 * @rdata: remote port private
2188 * @spp_len: service parameter page length
2189 * @rspp: received service parameter page
2190 * @spp: response service parameter page
2191 *
2192 * Returns the value for the response code to be placed in spp_flags;
2193 * Returns 0 if not an initiator.
2194 */
2195static int fc_rport_fcp_prli(struct fc_rport_priv *rdata, u32 spp_len,
2196 const struct fc_els_spp *rspp,
2197 struct fc_els_spp *spp)
2198{
2199 struct fc_lport *lport = rdata->local_port;
2200 u32 fcp_parm;
2201
2202 fcp_parm = ntohl(rspp->spp_params);
2203 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
2204 if (fcp_parm & FCP_SPPF_INIT_FCN)
2205 rdata->ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2206 if (fcp_parm & FCP_SPPF_TARG_FCN)
2207 rdata->ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2208 if (fcp_parm & FCP_SPPF_RETRY)
2209 rdata->flags |= FC_RP_FLAGS_RETRY;
2210 rdata->supported_classes = FC_COS_CLASS3;
2211
Mark Rustad732bdb92013-03-20 07:17:55 +00002212 if (!(lport->service_params & FCP_SPPF_INIT_FCN))
Joe Eykholt96ad8462011-01-28 16:04:02 -08002213 return 0;
2214
2215 spp->spp_flags |= rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
2216
2217 /*
2218 * OR in our service parameters with other providers (target), if any.
2219 */
2220 fcp_parm = ntohl(spp->spp_params);
2221 spp->spp_params = htonl(fcp_parm | lport->service_params);
2222 return FC_SPP_RESP_ACK;
2223}
2224
2225/*
2226 * FC-4 provider ops for FCP initiator.
2227 */
2228struct fc4_prov fc_rport_fcp_init = {
2229 .prli = fc_rport_fcp_prli,
2230};
2231
2232/**
2233 * fc_rport_t0_prli() - Handle incoming PRLI parameters for type 0
2234 * @rdata: remote port private
2235 * @spp_len: service parameter page length
2236 * @rspp: received service parameter page
2237 * @spp: response service parameter page
2238 */
2239static int fc_rport_t0_prli(struct fc_rport_priv *rdata, u32 spp_len,
2240 const struct fc_els_spp *rspp,
2241 struct fc_els_spp *spp)
2242{
2243 if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR)
2244 return FC_SPP_RESP_INVL;
2245 return FC_SPP_RESP_ACK;
2246}
2247
2248/*
2249 * FC-4 provider ops for type 0 service parameters.
2250 *
2251 * This handles the special case of type 0 which is always successful
2252 * but doesn't do anything otherwise.
2253 */
2254struct fc4_prov fc_rport_t0_prov = {
2255 .prli = fc_rport_t0_prli,
2256};
2257
2258/**
Robert Love3a3b42b2009-11-03 11:47:39 -08002259 * fc_setup_rport() - Initialize the rport_event_queue
2260 */
Randy Dunlap55204902011-01-28 16:03:57 -08002261int fc_setup_rport(void)
Robert Love42e9a922008-12-09 15:10:17 -08002262{
2263 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
2264 if (!rport_event_queue)
2265 return -ENOMEM;
2266 return 0;
2267}
Robert Love42e9a922008-12-09 15:10:17 -08002268
Robert Love3a3b42b2009-11-03 11:47:39 -08002269/**
2270 * fc_destroy_rport() - Destroy the rport_event_queue
2271 */
Randy Dunlap55204902011-01-28 16:03:57 -08002272void fc_destroy_rport(void)
Robert Love42e9a922008-12-09 15:10:17 -08002273{
2274 destroy_workqueue(rport_event_queue);
2275}
Robert Love42e9a922008-12-09 15:10:17 -08002276
Robert Love3a3b42b2009-11-03 11:47:39 -08002277/**
2278 * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
2279 * @rport: The remote port whose I/O should be terminated
2280 */
Robert Love42e9a922008-12-09 15:10:17 -08002281void fc_rport_terminate_io(struct fc_rport *rport)
2282{
Robert Love3a3b42b2009-11-03 11:47:39 -08002283 struct fc_rport_libfc_priv *rpriv = rport->dd_data;
2284 struct fc_lport *lport = rpriv->local_port;
Robert Love42e9a922008-12-09 15:10:17 -08002285
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -08002286 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
2287 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
Robert Love42e9a922008-12-09 15:10:17 -08002288}
2289EXPORT_SYMBOL(fc_rport_terminate_io);