blob: 72a7183fdd06d8243b8f42ca824461aba74a34b2 [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>
Robert Love42e9a922008-12-09 15:10:17 -080068#include <asm/unaligned.h>
69
70#include <scsi/libfc.h>
71#include <scsi/fc_encode.h>
72
Robert Love8866a5d2009-11-03 11:45:58 -080073#include "fc_libfc.h"
74
Randy Dunlap55204902011-01-28 16:03:57 -080075static struct workqueue_struct *rport_event_queue;
Robert Love42e9a922008-12-09 15:10:17 -080076
Joe Eykholta7b12a22010-07-20 15:20:08 -070077static void fc_rport_enter_flogi(struct fc_rport_priv *);
Joe Eykholt9fb9d322009-08-25 14:00:50 -070078static void fc_rport_enter_plogi(struct fc_rport_priv *);
79static void fc_rport_enter_prli(struct fc_rport_priv *);
80static void fc_rport_enter_rtv(struct fc_rport_priv *);
81static void fc_rport_enter_ready(struct fc_rport_priv *);
82static void fc_rport_enter_logo(struct fc_rport_priv *);
Joe Eykholt370c3bd2009-08-25 14:03:47 -070083static void fc_rport_enter_adisc(struct fc_rport_priv *);
Robert Love42e9a922008-12-09 15:10:17 -080084
Joe Eykholt922611562010-07-20 15:21:12 -070085static void fc_rport_recv_plogi_req(struct fc_lport *, struct fc_frame *);
86static void fc_rport_recv_prli_req(struct fc_rport_priv *, struct fc_frame *);
87static void fc_rport_recv_prlo_req(struct fc_rport_priv *, struct fc_frame *);
88static void fc_rport_recv_logo_req(struct fc_lport *, struct fc_frame *);
Robert Love42e9a922008-12-09 15:10:17 -080089static void fc_rport_timeout(struct work_struct *);
Joe Eykholt9fb9d322009-08-25 14:00:50 -070090static void fc_rport_error(struct fc_rport_priv *, struct fc_frame *);
91static void fc_rport_error_retry(struct fc_rport_priv *, struct fc_frame *);
Robert Love42e9a922008-12-09 15:10:17 -080092static void fc_rport_work(struct work_struct *);
93
94static const char *fc_rport_state_names[] = {
Robert Love42e9a922008-12-09 15:10:17 -080095 [RPORT_ST_INIT] = "Init",
Joe Eykholta7b12a22010-07-20 15:20:08 -070096 [RPORT_ST_FLOGI] = "FLOGI",
97 [RPORT_ST_PLOGI_WAIT] = "PLOGI_WAIT",
Robert Love42e9a922008-12-09 15:10:17 -080098 [RPORT_ST_PLOGI] = "PLOGI",
99 [RPORT_ST_PRLI] = "PRLI",
100 [RPORT_ST_RTV] = "RTV",
101 [RPORT_ST_READY] = "Ready",
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700102 [RPORT_ST_ADISC] = "ADISC",
Joe Eykholt14194052009-07-29 17:04:43 -0700103 [RPORT_ST_DELETE] = "Delete",
Robert Love42e9a922008-12-09 15:10:17 -0800104};
105
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700106/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800107 * fc_rport_lookup() - Lookup a remote port by port_id
108 * @lport: The local port to lookup the remote port on
109 * @port_id: The remote port ID to look up
Joe Eykholt42e90412010-07-20 15:19:37 -0700110 *
Hannes Reineckebaa67192016-05-24 08:11:58 +0200111 * The reference count of the fc_rport_priv structure is
112 * increased by one.
Joe Eykholt8025b5d2009-08-25 14:02:06 -0700113 */
114static struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport,
115 u32 port_id)
116{
Hannes Reineckebaa67192016-05-24 08:11:58 +0200117 struct fc_rport_priv *rdata = NULL, *tmp_rdata;
Joe Eykholt8025b5d2009-08-25 14:02:06 -0700118
Hannes Reineckebaa67192016-05-24 08:11:58 +0200119 rcu_read_lock();
120 list_for_each_entry_rcu(tmp_rdata, &lport->disc.rports, peers)
121 if (tmp_rdata->ids.port_id == port_id &&
122 kref_get_unless_zero(&tmp_rdata->kref)) {
123 rdata = tmp_rdata;
124 break;
125 }
126 rcu_read_unlock();
127 return rdata;
Joe Eykholt8025b5d2009-08-25 14:02:06 -0700128}
129
130/**
Robert Love9737e6a2009-08-25 14:02:59 -0700131 * fc_rport_create() - Create a new remote port
Robert Love3a3b42b2009-11-03 11:47:39 -0800132 * @lport: The local port this remote port will be associated with
133 * @ids: The identifiers for the new remote port
134 *
135 * The remote port will start in the INIT state.
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700136 *
Joe Eykholt48f00902009-08-25 14:01:50 -0700137 * Locking note: must be called with the disc_mutex held.
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700138 */
139static struct fc_rport_priv *fc_rport_create(struct fc_lport *lport,
Robert Love9737e6a2009-08-25 14:02:59 -0700140 u32 port_id)
Robert Love42e9a922008-12-09 15:10:17 -0800141{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700142 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800143
Robert Love9737e6a2009-08-25 14:02:59 -0700144 rdata = lport->tt.rport_lookup(lport, port_id);
Joe Eykholt19f97e32009-08-25 14:01:55 -0700145 if (rdata)
146 return rdata;
147
Joe Eykholtf90377a2010-07-20 15:19:42 -0700148 rdata = kzalloc(sizeof(*rdata) + lport->rport_priv_size, GFP_KERNEL);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700149 if (!rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800150 return NULL;
151
Robert Love9737e6a2009-08-25 14:02:59 -0700152 rdata->ids.node_name = -1;
153 rdata->ids.port_name = -1;
154 rdata->ids.port_id = port_id;
155 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
156
Joe Eykholtf211fa52009-08-25 14:01:01 -0700157 kref_init(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800158 mutex_init(&rdata->rp_mutex);
Joe Eykholt795d86f2009-08-25 14:00:39 -0700159 rdata->local_port = lport;
Robert Love42e9a922008-12-09 15:10:17 -0800160 rdata->rp_state = RPORT_ST_INIT;
161 rdata->event = RPORT_EV_NONE;
162 rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
Joe Eykholt795d86f2009-08-25 14:00:39 -0700163 rdata->e_d_tov = lport->e_d_tov;
164 rdata->r_a_tov = lport->r_a_tov;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700165 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
Robert Love42e9a922008-12-09 15:10:17 -0800166 INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
167 INIT_WORK(&rdata->event_work, fc_rport_work);
Bhanu Prakash Gollapudi75a27922011-01-28 16:05:27 -0800168 if (port_id != FC_FID_DIR_SERV) {
169 rdata->lld_event_callback = lport->tt.rport_event_callback;
Joe Eykholt42e90412010-07-20 15:19:37 -0700170 list_add_rcu(&rdata->peers, &lport->disc.rports);
Bhanu Prakash Gollapudi75a27922011-01-28 16:05:27 -0800171 }
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700172 return rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800173}
174
175/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800176 * fc_rport_destroy() - Free a remote port after last reference is released
177 * @kref: The remote port's kref
Joe Eykholtf211fa52009-08-25 14:01:01 -0700178 */
179static void fc_rport_destroy(struct kref *kref)
180{
181 struct fc_rport_priv *rdata;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700182
183 rdata = container_of(kref, struct fc_rport_priv, kref);
Lai Jiangshan8497a242011-03-18 11:41:14 +0800184 kfree_rcu(rdata, rcu);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700185}
186
187/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800188 * fc_rport_state() - Return a string identifying the remote port's state
189 * @rdata: The remote port
Robert Love42e9a922008-12-09 15:10:17 -0800190 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700191static const char *fc_rport_state(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800192{
193 const char *cp;
Robert Love42e9a922008-12-09 15:10:17 -0800194
195 cp = fc_rport_state_names[rdata->rp_state];
196 if (!cp)
197 cp = "Unknown";
198 return cp;
199}
200
201/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800202 * fc_set_rport_loss_tmo() - Set the remote port loss timeout
203 * @rport: The remote port that gets a new timeout value
204 * @timeout: The new timeout value (in seconds)
Robert Love42e9a922008-12-09 15:10:17 -0800205 */
206void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
207{
208 if (timeout)
Mike Christie73b43762010-10-08 17:12:10 -0700209 rport->dev_loss_tmo = timeout;
Robert Love42e9a922008-12-09 15:10:17 -0800210 else
Mike Christie73b43762010-10-08 17:12:10 -0700211 rport->dev_loss_tmo = 1;
Robert Love42e9a922008-12-09 15:10:17 -0800212}
213EXPORT_SYMBOL(fc_set_rport_loss_tmo);
214
215/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800216 * fc_plogi_get_maxframe() - Get the maximum payload from the common service
217 * parameters in a FLOGI frame
Joe Eykholta7b12a22010-07-20 15:20:08 -0700218 * @flp: The FLOGI or PLOGI payload
Robert Love3a3b42b2009-11-03 11:47:39 -0800219 * @maxval: The maximum frame size upper limit; this may be less than what
220 * is in the service parameters
Robert Love42e9a922008-12-09 15:10:17 -0800221 */
Robert Loveb2ab99c2009-02-27 10:55:50 -0800222static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
223 unsigned int maxval)
Robert Love42e9a922008-12-09 15:10:17 -0800224{
225 unsigned int mfs;
226
227 /*
228 * Get max payload from the common service parameters and the
229 * class 3 receive data field size.
230 */
231 mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
232 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
233 maxval = mfs;
234 mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
235 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
236 maxval = mfs;
237 return maxval;
238}
239
240/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800241 * fc_rport_state_enter() - Change the state of a remote port
242 * @rdata: The remote port whose state should change
243 * @new: The new state
Robert Love42e9a922008-12-09 15:10:17 -0800244 *
245 * Locking Note: Called with the rport lock held
246 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700247static void fc_rport_state_enter(struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -0800248 enum fc_rport_state new)
249{
Robert Love42e9a922008-12-09 15:10:17 -0800250 if (rdata->rp_state != new)
251 rdata->retries = 0;
252 rdata->rp_state = new;
253}
254
Robert Love3a3b42b2009-11-03 11:47:39 -0800255/**
256 * fc_rport_work() - Handler for remote port events in the rport_event_queue
257 * @work: Handle to the remote port being dequeued
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200258 *
259 * Reference counting: drops kref on return
Robert Love3a3b42b2009-11-03 11:47:39 -0800260 */
Robert Love42e9a922008-12-09 15:10:17 -0800261static void fc_rport_work(struct work_struct *work)
262{
Abhijeet Joglekar571f8242009-02-27 10:54:41 -0800263 u32 port_id;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700264 struct fc_rport_priv *rdata =
265 container_of(work, struct fc_rport_priv, event_work);
Robert Love3a3b42b2009-11-03 11:47:39 -0800266 struct fc_rport_libfc_priv *rpriv;
Robert Love42e9a922008-12-09 15:10:17 -0800267 enum fc_rport_event event;
Robert Love42e9a922008-12-09 15:10:17 -0800268 struct fc_lport *lport = rdata->local_port;
269 struct fc_rport_operations *rport_ops;
Joe Eykholt629f4422009-08-25 14:01:06 -0700270 struct fc_rport_identifiers ids;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700271 struct fc_rport *rport;
Joe Eykholt96ad8462011-01-28 16:04:02 -0800272 struct fc4_prov *prov;
273 u8 type;
Robert Love42e9a922008-12-09 15:10:17 -0800274
275 mutex_lock(&rdata->rp_mutex);
276 event = rdata->event;
277 rport_ops = rdata->ops;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700278 rport = rdata->rport;
Robert Love42e9a922008-12-09 15:10:17 -0800279
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700280 FC_RPORT_DBG(rdata, "work event %u\n", event);
281
Joe Eykholt629f4422009-08-25 14:01:06 -0700282 switch (event) {
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700283 case RPORT_EV_READY:
Joe Eykholtf211fa52009-08-25 14:01:01 -0700284 ids = rdata->ids;
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700285 rdata->event = RPORT_EV_NONE;
Joe Eykholtf0342602010-06-11 16:44:57 -0700286 rdata->major_retries = 0;
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700287 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800288 mutex_unlock(&rdata->rp_mutex);
289
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700290 if (!rport)
291 rport = fc_remote_port_add(lport->host, 0, &ids);
292 if (!rport) {
293 FC_RPORT_DBG(rdata, "Failed to add the rport\n");
294 lport->tt.rport_logoff(rdata);
295 kref_put(&rdata->kref, lport->tt.rport_destroy);
296 return;
Robert Love42e9a922008-12-09 15:10:17 -0800297 }
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700298 mutex_lock(&rdata->rp_mutex);
299 if (rdata->rport)
300 FC_RPORT_DBG(rdata, "rport already allocated\n");
301 rdata->rport = rport;
302 rport->maxframe_size = rdata->maxframe_size;
303 rport->supported_classes = rdata->supported_classes;
304
Robert Love3a3b42b2009-11-03 11:47:39 -0800305 rpriv = rport->dd_data;
306 rpriv->local_port = lport;
307 rpriv->rp_state = rdata->rp_state;
308 rpriv->flags = rdata->flags;
309 rpriv->e_d_tov = rdata->e_d_tov;
310 rpriv->r_a_tov = rdata->r_a_tov;
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700311 mutex_unlock(&rdata->rp_mutex);
312
Joe Eykholt83455922009-08-25 14:02:01 -0700313 if (rport_ops && rport_ops->event_callback) {
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700314 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700315 rport_ops->event_callback(lport, rdata, event);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700316 }
Bhanu Prakash Gollapudi75a27922011-01-28 16:05:27 -0800317 if (rdata->lld_event_callback) {
318 FC_RPORT_DBG(rdata, "lld callback ev %d\n", event);
319 rdata->lld_event_callback(lport, rdata, event);
320 }
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700321 kref_put(&rdata->kref, lport->tt.rport_destroy);
Joe Eykholt629f4422009-08-25 14:01:06 -0700322 break;
323
324 case RPORT_EV_FAILED:
325 case RPORT_EV_LOGO:
326 case RPORT_EV_STOP:
Joe Eykholt96ad8462011-01-28 16:04:02 -0800327 if (rdata->prli_count) {
328 mutex_lock(&fc_prov_mutex);
329 for (type = 1; type < FC_FC4_PROV_SIZE; type++) {
330 prov = fc_passive_prov[type];
331 if (prov && prov->prlo)
332 prov->prlo(rdata);
333 }
334 mutex_unlock(&fc_prov_mutex);
335 }
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700336 port_id = rdata->ids.port_id;
Robert Love42e9a922008-12-09 15:10:17 -0800337 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700338
Joe Eykholt83455922009-08-25 14:02:01 -0700339 if (rport_ops && rport_ops->event_callback) {
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700340 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700341 rport_ops->event_callback(lport, rdata, event);
Abhijeet Joglekar571f8242009-02-27 10:54:41 -0800342 }
Bhanu Prakash Gollapudi75a27922011-01-28 16:05:27 -0800343 if (rdata->lld_event_callback) {
344 FC_RPORT_DBG(rdata, "lld callback ev %d\n", event);
345 rdata->lld_event_callback(lport, rdata, event);
346 }
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200347 if (cancel_delayed_work_sync(&rdata->retry_work))
348 kref_put(&rdata->kref, lport->tt.rport_destroy);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700349
350 /*
351 * Reset any outstanding exchanges before freeing rport.
352 */
353 lport->tt.exch_mgr_reset(lport, 0, port_id);
354 lport->tt.exch_mgr_reset(lport, port_id, 0);
355
356 if (rport) {
Robert Love3a3b42b2009-11-03 11:47:39 -0800357 rpriv = rport->dd_data;
358 rpriv->rp_state = RPORT_ST_DELETE;
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700359 mutex_lock(&rdata->rp_mutex);
360 rdata->rport = NULL;
361 mutex_unlock(&rdata->rp_mutex);
362 fc_remote_port_delete(rport);
363 }
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700364
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700365 mutex_lock(&rdata->rp_mutex);
366 if (rdata->rp_state == RPORT_ST_DELETE) {
367 if (port_id == FC_FID_DIR_SERV) {
368 rdata->event = RPORT_EV_NONE;
369 mutex_unlock(&rdata->rp_mutex);
Parikh, Neeravfe5e3f12011-02-25 15:02:51 -0800370 kref_put(&rdata->kref, lport->tt.rport_destroy);
Joe Eykholtf0342602010-06-11 16:44:57 -0700371 } else if ((rdata->flags & FC_RP_STARTED) &&
372 rdata->major_retries <
373 lport->max_rport_retry_count) {
374 rdata->major_retries++;
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700375 rdata->event = RPORT_EV_NONE;
376 FC_RPORT_DBG(rdata, "work restart\n");
Joe Eykholta7b12a22010-07-20 15:20:08 -0700377 fc_rport_enter_flogi(rdata);
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700378 mutex_unlock(&rdata->rp_mutex);
379 } else {
380 FC_RPORT_DBG(rdata, "work delete\n");
Hannes Reineckea407c592016-09-30 11:01:15 +0200381 mutex_lock(&lport->disc.disc_mutex);
Joe Eykholt42e90412010-07-20 15:19:37 -0700382 list_del_rcu(&rdata->peers);
Hannes Reineckea407c592016-09-30 11:01:15 +0200383 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700384 mutex_unlock(&rdata->rp_mutex);
385 kref_put(&rdata->kref, lport->tt.rport_destroy);
386 }
387 } else {
388 /*
389 * Re-open for events. Reissue READY event if ready.
390 */
391 rdata->event = RPORT_EV_NONE;
392 if (rdata->rp_state == RPORT_ST_READY)
393 fc_rport_enter_ready(rdata);
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700394 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700395 }
Joe Eykholt629f4422009-08-25 14:01:06 -0700396 break;
397
398 default:
Robert Love42e9a922008-12-09 15:10:17 -0800399 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt629f4422009-08-25 14:01:06 -0700400 break;
401 }
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200402 kref_put(&rdata->kref, lport->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800403}
404
405/**
Robert Love34f42a02009-02-27 10:55:45 -0800406 * fc_rport_login() - Start the remote port login state machine
Robert Love3a3b42b2009-11-03 11:47:39 -0800407 * @rdata: The remote port to be logged in to
Robert Love42e9a922008-12-09 15:10:17 -0800408 *
409 * Locking Note: Called without the rport lock held. This
410 * function will hold the rport lock, call an _enter_*
411 * function and then unlock the rport.
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700412 *
413 * This indicates the intent to be logged into the remote port.
414 * If it appears we are already logged in, ADISC is used to verify
415 * the setup.
Robert Love42e9a922008-12-09 15:10:17 -0800416 */
Bart Van Asschec6b21c92012-01-13 17:26:20 -0800417static int fc_rport_login(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800418{
Robert Love42e9a922008-12-09 15:10:17 -0800419 mutex_lock(&rdata->rp_mutex);
420
Hannes Reinecke06ee2572016-09-30 11:01:18 +0200421 if (rdata->flags & FC_RP_STARTED) {
422 FC_RPORT_DBG(rdata, "port already started\n");
423 mutex_unlock(&rdata->rp_mutex);
424 return 0;
425 }
426
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700427 rdata->flags |= FC_RP_STARTED;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700428 switch (rdata->rp_state) {
429 case RPORT_ST_READY:
430 FC_RPORT_DBG(rdata, "ADISC port\n");
431 fc_rport_enter_adisc(rdata);
432 break;
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700433 case RPORT_ST_DELETE:
434 FC_RPORT_DBG(rdata, "Restart deleted port\n");
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700435 break;
Hannes Reineckee5a20002016-09-30 11:01:17 +0200436 case RPORT_ST_INIT:
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700437 FC_RPORT_DBG(rdata, "Login to port\n");
Joe Eykholta7b12a22010-07-20 15:20:08 -0700438 fc_rport_enter_flogi(rdata);
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700439 break;
Hannes Reineckee5a20002016-09-30 11:01:17 +0200440 default:
441 FC_RPORT_DBG(rdata, "Login in progress, state %s\n",
442 fc_rport_state(rdata));
443 break;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700444 }
Robert Love42e9a922008-12-09 15:10:17 -0800445 mutex_unlock(&rdata->rp_mutex);
446
447 return 0;
448}
449
450/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800451 * fc_rport_enter_delete() - Schedule a remote port to be deleted
452 * @rdata: The remote port to be deleted
453 * @event: The event to report as the reason for deletion
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700454 *
455 * Locking Note: Called with the rport lock held.
456 *
457 * Allow state change into DELETE only once.
458 *
459 * Call queue_work only if there's no event already pending.
460 * Set the new event so that the old pending event will not occur.
461 * Since we have the mutex, even if fc_rport_work() is already started,
462 * it'll see the new event.
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200463 *
464 * Reference counting: does not modify kref
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700465 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700466static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700467 enum fc_rport_event event)
468{
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200469 struct fc_lport *lport = rdata->local_port;
470
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700471 if (rdata->rp_state == RPORT_ST_DELETE)
472 return;
473
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700474 FC_RPORT_DBG(rdata, "Delete port\n");
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700475
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700476 fc_rport_state_enter(rdata, RPORT_ST_DELETE);
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700477
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200478 kref_get(&rdata->kref);
479 if (rdata->event == RPORT_EV_NONE &&
480 !queue_work(rport_event_queue, &rdata->event_work))
481 kref_put(&rdata->kref, lport->tt.rport_destroy);
482
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700483 rdata->event = event;
484}
485
486/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800487 * fc_rport_logoff() - Logoff and remove a remote port
488 * @rdata: The remote port to be logged off of
Robert Love42e9a922008-12-09 15:10:17 -0800489 *
490 * Locking Note: Called without the rport lock held. This
491 * function will hold the rport lock, call an _enter_*
492 * function and then unlock the rport.
493 */
Bart Van Asschec6b21c92012-01-13 17:26:20 -0800494static int fc_rport_logoff(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800495{
Hannes Reinecke649eb862016-08-05 14:55:02 +0200496 struct fc_lport *lport = rdata->local_port;
497 u32 port_id = rdata->ids.port_id;
498
Robert Love42e9a922008-12-09 15:10:17 -0800499 mutex_lock(&rdata->rp_mutex);
500
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700501 FC_RPORT_DBG(rdata, "Remove port\n");
Robert Love42e9a922008-12-09 15:10:17 -0800502
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700503 rdata->flags &= ~FC_RP_STARTED;
Joe Eykholt14194052009-07-29 17:04:43 -0700504 if (rdata->rp_state == RPORT_ST_DELETE) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700505 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700506 goto out;
507 }
Hannes Reinecke649eb862016-08-05 14:55:02 +0200508 /*
509 * FC-LS states:
510 * To explicitly Logout, the initiating Nx_Port shall terminate
511 * other open Sequences that it initiated with the destination
512 * Nx_Port prior to performing Logout.
513 */
514 lport->tt.exch_mgr_reset(lport, 0, port_id);
515 lport->tt.exch_mgr_reset(lport, port_id, 0);
516
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700517 fc_rport_enter_logo(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800518
519 /*
Joe Eykholt14194052009-07-29 17:04:43 -0700520 * Change the state to Delete so that we discard
Robert Love42e9a922008-12-09 15:10:17 -0800521 * the response.
522 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700523 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700524out:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700525 mutex_unlock(&rdata->rp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800526 return 0;
527}
528
529/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800530 * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state
531 * @rdata: The remote port that is ready
Robert Love42e9a922008-12-09 15:10:17 -0800532 *
533 * Locking Note: The rport lock is expected to be held before calling
534 * this routine.
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200535 *
536 * Reference counting: schedules workqueue, does not modify kref
Robert Love42e9a922008-12-09 15:10:17 -0800537 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700538static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800539{
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200540 struct fc_lport *lport = rdata->local_port;
541
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700542 fc_rport_state_enter(rdata, RPORT_ST_READY);
Robert Love42e9a922008-12-09 15:10:17 -0800543
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700544 FC_RPORT_DBG(rdata, "Port is Ready\n");
Robert Love42e9a922008-12-09 15:10:17 -0800545
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200546 kref_get(&rdata->kref);
547 if (rdata->event == RPORT_EV_NONE &&
548 !queue_work(rport_event_queue, &rdata->event_work))
549 kref_put(&rdata->kref, lport->tt.rport_destroy);
550
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700551 rdata->event = RPORT_EV_READY;
Robert Love42e9a922008-12-09 15:10:17 -0800552}
553
554/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800555 * fc_rport_timeout() - Handler for the retry_work timer
556 * @work: Handle to the remote port that has timed out
Robert Love42e9a922008-12-09 15:10:17 -0800557 *
558 * Locking Note: Called without the rport lock held. This
559 * function will hold the rport lock, call an _enter_*
560 * function and then unlock the rport.
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200561 *
562 * Reference counting: Drops kref on return.
Robert Love42e9a922008-12-09 15:10:17 -0800563 */
564static void fc_rport_timeout(struct work_struct *work)
565{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700566 struct fc_rport_priv *rdata =
567 container_of(work, struct fc_rport_priv, retry_work.work);
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200568 struct fc_lport *lport = rdata->local_port;
Robert Love42e9a922008-12-09 15:10:17 -0800569
570 mutex_lock(&rdata->rp_mutex);
571
572 switch (rdata->rp_state) {
Joe Eykholta7b12a22010-07-20 15:20:08 -0700573 case RPORT_ST_FLOGI:
574 fc_rport_enter_flogi(rdata);
575 break;
Robert Love42e9a922008-12-09 15:10:17 -0800576 case RPORT_ST_PLOGI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700577 fc_rport_enter_plogi(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800578 break;
579 case RPORT_ST_PRLI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700580 fc_rport_enter_prli(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800581 break;
582 case RPORT_ST_RTV:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700583 fc_rport_enter_rtv(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800584 break;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700585 case RPORT_ST_ADISC:
586 fc_rport_enter_adisc(rdata);
587 break;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700588 case RPORT_ST_PLOGI_WAIT:
Robert Love42e9a922008-12-09 15:10:17 -0800589 case RPORT_ST_READY:
590 case RPORT_ST_INIT:
Joe Eykholt14194052009-07-29 17:04:43 -0700591 case RPORT_ST_DELETE:
Robert Love42e9a922008-12-09 15:10:17 -0800592 break;
593 }
594
595 mutex_unlock(&rdata->rp_mutex);
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200596 kref_put(&rdata->kref, lport->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800597}
598
599/**
Robert Love34f42a02009-02-27 10:55:45 -0800600 * fc_rport_error() - Error handler, called once retries have been exhausted
Robert Love3a3b42b2009-11-03 11:47:39 -0800601 * @rdata: The remote port the error is happened on
602 * @fp: The error code encapsulated in a frame pointer
Robert Love42e9a922008-12-09 15:10:17 -0800603 *
Robert Love42e9a922008-12-09 15:10:17 -0800604 * Locking Note: The rport lock is expected to be held before
605 * calling this routine
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200606 *
607 * Reference counting: does not modify kref
Robert Love42e9a922008-12-09 15:10:17 -0800608 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700609static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -0800610{
Hannes Reinecked3919662016-08-05 14:55:01 +0200611 struct fc_lport *lport = rdata->local_port;
612
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700613 FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
Joe Eykholtcdbe6df2009-08-25 14:01:39 -0700614 IS_ERR(fp) ? -PTR_ERR(fp) : 0,
615 fc_rport_state(rdata), rdata->retries);
Robert Love42e9a922008-12-09 15:10:17 -0800616
Chris Leech6755db12009-02-27 10:55:02 -0800617 switch (rdata->rp_state) {
Joe Eykholta7b12a22010-07-20 15:20:08 -0700618 case RPORT_ST_FLOGI:
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700619 rdata->flags &= ~FC_RP_STARTED;
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700620 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
Chris Leech6755db12009-02-27 10:55:02 -0800621 break;
Hannes Reinecked3919662016-08-05 14:55:01 +0200622 case RPORT_ST_PLOGI:
623 if (lport->point_to_multipoint) {
624 rdata->flags &= ~FC_RP_STARTED;
625 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
626 } else
627 fc_rport_enter_logo(rdata);
628 break;
Chris Leech6755db12009-02-27 10:55:02 -0800629 case RPORT_ST_RTV:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700630 fc_rport_enter_ready(rdata);
Chris Leech6755db12009-02-27 10:55:02 -0800631 break;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700632 case RPORT_ST_PRLI:
633 case RPORT_ST_ADISC:
634 fc_rport_enter_logo(rdata);
635 break;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700636 case RPORT_ST_PLOGI_WAIT:
Joe Eykholt14194052009-07-29 17:04:43 -0700637 case RPORT_ST_DELETE:
Chris Leech6755db12009-02-27 10:55:02 -0800638 case RPORT_ST_READY:
639 case RPORT_ST_INIT:
640 break;
Robert Love42e9a922008-12-09 15:10:17 -0800641 }
642}
643
644/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800645 * fc_rport_error_retry() - Handler for remote port state retries
646 * @rdata: The remote port whose state is to be retried
647 * @fp: The error code encapsulated in a frame pointer
Chris Leech6755db12009-02-27 10:55:02 -0800648 *
649 * If the error was an exchange timeout retry immediately,
650 * otherwise wait for E_D_TOV.
651 *
652 * Locking Note: The rport lock is expected to be held before
653 * calling this routine
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200654 *
655 * Reference counting: increments kref when scheduling retry_work
Chris Leech6755db12009-02-27 10:55:02 -0800656 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700657static void fc_rport_error_retry(struct fc_rport_priv *rdata,
658 struct fc_frame *fp)
Chris Leech6755db12009-02-27 10:55:02 -0800659{
Krishna Mohana5860692013-02-13 16:33:04 -0800660 unsigned long delay = msecs_to_jiffies(FC_DEF_E_D_TOV);
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200661 struct fc_lport *lport = rdata->local_port;
Chris Leech6755db12009-02-27 10:55:02 -0800662
663 /* make sure this isn't an FC_EX_CLOSED error, never retry those */
664 if (PTR_ERR(fp) == -FC_EX_CLOSED)
Hillf Danton28a4af12011-01-28 16:03:26 -0800665 goto out;
Chris Leech6755db12009-02-27 10:55:02 -0800666
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700667 if (rdata->retries < rdata->local_port->max_rport_retry_count) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700668 FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
669 PTR_ERR(fp), fc_rport_state(rdata));
Chris Leech6755db12009-02-27 10:55:02 -0800670 rdata->retries++;
671 /* no additional delay on exchange timeouts */
672 if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
673 delay = 0;
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200674 kref_get(&rdata->kref);
675 if (!schedule_delayed_work(&rdata->retry_work, delay))
676 kref_put(&rdata->kref, lport->tt.rport_destroy);
Chris Leech6755db12009-02-27 10:55:02 -0800677 return;
678 }
679
Hillf Danton28a4af12011-01-28 16:03:26 -0800680out:
681 fc_rport_error(rdata, fp);
Chris Leech6755db12009-02-27 10:55:02 -0800682}
683
684/**
Joe Eykholta7b12a22010-07-20 15:20:08 -0700685 * fc_rport_login_complete() - Handle parameters and completion of p-mp login.
686 * @rdata: The remote port which we logged into or which logged into us.
687 * @fp: The FLOGI or PLOGI request or response frame
688 *
689 * Returns non-zero error if a problem is detected with the frame.
690 * Does not free the frame.
691 *
692 * This is only used in point-to-multipoint mode for FIP currently.
693 */
694static int fc_rport_login_complete(struct fc_rport_priv *rdata,
695 struct fc_frame *fp)
696{
697 struct fc_lport *lport = rdata->local_port;
698 struct fc_els_flogi *flogi;
699 unsigned int e_d_tov;
700 u16 csp_flags;
701
702 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
703 if (!flogi)
704 return -EINVAL;
705
706 csp_flags = ntohs(flogi->fl_csp.sp_features);
707
708 if (fc_frame_payload_op(fp) == ELS_FLOGI) {
709 if (csp_flags & FC_SP_FT_FPORT) {
710 FC_RPORT_DBG(rdata, "Fabric bit set in FLOGI\n");
711 return -EINVAL;
712 }
713 } else {
714
715 /*
716 * E_D_TOV is not valid on an incoming FLOGI request.
717 */
718 e_d_tov = ntohl(flogi->fl_csp.sp_e_d_tov);
719 if (csp_flags & FC_SP_FT_EDTR)
720 e_d_tov /= 1000000;
721 if (e_d_tov > rdata->e_d_tov)
722 rdata->e_d_tov = e_d_tov;
723 }
724 rdata->maxframe_size = fc_plogi_get_maxframe(flogi, lport->mfs);
725 return 0;
726}
727
728/**
729 * fc_rport_flogi_resp() - Handle response to FLOGI request for p-mp mode
730 * @sp: The sequence that the FLOGI was on
731 * @fp: The FLOGI response frame
732 * @rp_arg: The remote port that received the FLOGI response
733 */
Bart Van Asschec6b21c92012-01-13 17:26:20 -0800734static void fc_rport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
735 void *rp_arg)
Joe Eykholta7b12a22010-07-20 15:20:08 -0700736{
737 struct fc_rport_priv *rdata = rp_arg;
738 struct fc_lport *lport = rdata->local_port;
739 struct fc_els_flogi *flogi;
740 unsigned int r_a_tov;
741
742 FC_RPORT_DBG(rdata, "Received a FLOGI %s\n", fc_els_resp_type(fp));
743
744 if (fp == ERR_PTR(-FC_EX_CLOSED))
Hillf Danton0e9e3d32010-11-30 16:19:04 -0800745 goto put;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700746
747 mutex_lock(&rdata->rp_mutex);
748
749 if (rdata->rp_state != RPORT_ST_FLOGI) {
750 FC_RPORT_DBG(rdata, "Received a FLOGI response, but in state "
751 "%s\n", fc_rport_state(rdata));
752 if (IS_ERR(fp))
753 goto err;
754 goto out;
755 }
756
757 if (IS_ERR(fp)) {
758 fc_rport_error(rdata, fp);
759 goto err;
760 }
761
762 if (fc_frame_payload_op(fp) != ELS_LS_ACC)
763 goto bad;
764 if (fc_rport_login_complete(rdata, fp))
765 goto bad;
766
767 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
768 if (!flogi)
769 goto bad;
770 r_a_tov = ntohl(flogi->fl_csp.sp_r_a_tov);
771 if (r_a_tov > rdata->r_a_tov)
772 rdata->r_a_tov = r_a_tov;
773
774 if (rdata->ids.port_name < lport->wwpn)
775 fc_rport_enter_plogi(rdata);
776 else
777 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
778out:
779 fc_frame_free(fp);
780err:
781 mutex_unlock(&rdata->rp_mutex);
Hillf Danton0e9e3d32010-11-30 16:19:04 -0800782put:
Hannes Reineckebaa67192016-05-24 08:11:58 +0200783 kref_put(&rdata->kref, lport->tt.rport_destroy);
Joe Eykholta7b12a22010-07-20 15:20:08 -0700784 return;
785bad:
786 FC_RPORT_DBG(rdata, "Bad FLOGI response\n");
787 fc_rport_error_retry(rdata, fp);
788 goto out;
789}
790
791/**
792 * fc_rport_enter_flogi() - Send a FLOGI request to the remote port for p-mp
793 * @rdata: The remote port to send a FLOGI to
794 *
795 * Locking Note: The rport lock is expected to be held before calling
796 * this routine.
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200797 *
798 * Reference counting: increments kref when sending ELS
Joe Eykholta7b12a22010-07-20 15:20:08 -0700799 */
800static void fc_rport_enter_flogi(struct fc_rport_priv *rdata)
801{
802 struct fc_lport *lport = rdata->local_port;
803 struct fc_frame *fp;
804
805 if (!lport->point_to_multipoint)
806 return fc_rport_enter_plogi(rdata);
807
808 FC_RPORT_DBG(rdata, "Entered FLOGI state from %s state\n",
809 fc_rport_state(rdata));
810
811 fc_rport_state_enter(rdata, RPORT_ST_FLOGI);
812
813 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
814 if (!fp)
815 return fc_rport_error_retry(rdata, fp);
816
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200817 kref_get(&rdata->kref);
Joe Eykholta7b12a22010-07-20 15:20:08 -0700818 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_FLOGI,
819 fc_rport_flogi_resp, rdata,
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200820 2 * lport->r_a_tov)) {
Joe Eykholta7b12a22010-07-20 15:20:08 -0700821 fc_rport_error_retry(rdata, NULL);
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200822 kref_put(&rdata->kref, lport->tt.rport_destroy);
823 }
Joe Eykholta7b12a22010-07-20 15:20:08 -0700824}
825
826/**
827 * fc_rport_recv_flogi_req() - Handle Fabric Login (FLOGI) request in p-mp mode
828 * @lport: The local port that received the PLOGI request
Joe Eykholta7b12a22010-07-20 15:20:08 -0700829 * @rx_fp: The PLOGI request frame
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200830 *
831 * Reference counting: drops kref on return
Joe Eykholta7b12a22010-07-20 15:20:08 -0700832 */
833static void fc_rport_recv_flogi_req(struct fc_lport *lport,
Joe Eykholt922611562010-07-20 15:21:12 -0700834 struct fc_frame *rx_fp)
Joe Eykholta7b12a22010-07-20 15:20:08 -0700835{
836 struct fc_disc *disc;
837 struct fc_els_flogi *flp;
838 struct fc_rport_priv *rdata;
839 struct fc_frame *fp = rx_fp;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700840 struct fc_seq_els_data rjt_data;
Joe Eykholt24f089e2010-07-20 15:21:01 -0700841 u32 sid;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700842
Joe Eykholt251748a2010-07-20 15:20:56 -0700843 sid = fc_frame_sid(fp);
Joe Eykholta7b12a22010-07-20 15:20:08 -0700844
845 FC_RPORT_ID_DBG(lport, sid, "Received FLOGI request\n");
846
847 disc = &lport->disc;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700848 if (!lport->point_to_multipoint) {
849 rjt_data.reason = ELS_RJT_UNSUP;
850 rjt_data.explan = ELS_EXPL_NONE;
851 goto reject;
852 }
853
854 flp = fc_frame_payload_get(fp, sizeof(*flp));
855 if (!flp) {
856 rjt_data.reason = ELS_RJT_LOGIC;
857 rjt_data.explan = ELS_EXPL_INV_LEN;
858 goto reject;
859 }
860
861 rdata = lport->tt.rport_lookup(lport, sid);
862 if (!rdata) {
863 rjt_data.reason = ELS_RJT_FIP;
864 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
865 goto reject;
866 }
867 mutex_lock(&rdata->rp_mutex);
868
869 FC_RPORT_DBG(rdata, "Received FLOGI in %s state\n",
870 fc_rport_state(rdata));
871
872 switch (rdata->rp_state) {
873 case RPORT_ST_INIT:
Kiran Patil48058482011-06-20 16:58:59 -0700874 /*
875 * If received the FLOGI request on RPORT which is INIT state
876 * (means not transition to FLOGI either fc_rport timeout
877 * function didn;t trigger or this end hasn;t received
878 * beacon yet from other end. In that case only, allow RPORT
879 * state machine to continue, otherwise fall through which
880 * causes the code to send reject response.
881 * NOTE; Not checking for FIP->state such as VNMP_UP or
882 * VNMP_CLAIM because if FIP state is not one of those,
883 * RPORT wouldn;t have created and 'rport_lookup' would have
884 * failed anyway in that case.
885 */
Hannes Reinecke4d2095c2016-09-30 11:01:14 +0200886 break;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700887 case RPORT_ST_DELETE:
888 mutex_unlock(&rdata->rp_mutex);
889 rjt_data.reason = ELS_RJT_FIP;
890 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
Hannes Reineckebaa67192016-05-24 08:11:58 +0200891 goto reject_put;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700892 case RPORT_ST_FLOGI:
893 case RPORT_ST_PLOGI_WAIT:
894 case RPORT_ST_PLOGI:
895 break;
896 case RPORT_ST_PRLI:
897 case RPORT_ST_RTV:
898 case RPORT_ST_READY:
899 case RPORT_ST_ADISC:
900 /*
901 * Set the remote port to be deleted and to then restart.
902 * This queues work to be sure exchanges are reset.
903 */
904 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
905 mutex_unlock(&rdata->rp_mutex);
906 rjt_data.reason = ELS_RJT_BUSY;
907 rjt_data.explan = ELS_EXPL_NONE;
Hannes Reineckebaa67192016-05-24 08:11:58 +0200908 goto reject_put;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700909 }
910 if (fc_rport_login_complete(rdata, fp)) {
911 mutex_unlock(&rdata->rp_mutex);
912 rjt_data.reason = ELS_RJT_LOGIC;
913 rjt_data.explan = ELS_EXPL_NONE;
Hannes Reineckebaa67192016-05-24 08:11:58 +0200914 goto reject_put;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700915 }
Joe Eykholta7b12a22010-07-20 15:20:08 -0700916
917 fp = fc_frame_alloc(lport, sizeof(*flp));
918 if (!fp)
919 goto out;
920
Joe Eykholta7b12a22010-07-20 15:20:08 -0700921 fc_flogi_fill(lport, fp);
922 flp = fc_frame_payload_get(fp, sizeof(*flp));
923 flp->fl_cmd = ELS_LS_ACC;
924
Joe Eykholt24f089e2010-07-20 15:21:01 -0700925 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
926 lport->tt.frame_send(lport, fp);
Joe Eykholta7b12a22010-07-20 15:20:08 -0700927
928 if (rdata->ids.port_name < lport->wwpn)
929 fc_rport_enter_plogi(rdata);
930 else
931 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
932out:
933 mutex_unlock(&rdata->rp_mutex);
Hannes Reineckebaa67192016-05-24 08:11:58 +0200934 kref_put(&rdata->kref, lport->tt.rport_destroy);
Joe Eykholt24f089e2010-07-20 15:21:01 -0700935 fc_frame_free(rx_fp);
Joe Eykholta7b12a22010-07-20 15:20:08 -0700936 return;
937
Hannes Reineckebaa67192016-05-24 08:11:58 +0200938reject_put:
939 kref_put(&rdata->kref, lport->tt.rport_destroy);
Joe Eykholta7b12a22010-07-20 15:20:08 -0700940reject:
Joe Eykholt922611562010-07-20 15:21:12 -0700941 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
Joe Eykholt24f089e2010-07-20 15:21:01 -0700942 fc_frame_free(rx_fp);
Joe Eykholta7b12a22010-07-20 15:20:08 -0700943}
944
945/**
946 * fc_rport_plogi_resp() - Handler for ELS PLOGI responses
Robert Love3a3b42b2009-11-03 11:47:39 -0800947 * @sp: The sequence the PLOGI is on
948 * @fp: The PLOGI response frame
949 * @rdata_arg: The remote port that sent the PLOGI response
Robert Love42e9a922008-12-09 15:10:17 -0800950 *
951 * Locking Note: This function will be called without the rport lock
952 * held, but it will lock, call an _enter_* function or fc_rport_error
953 * and then unlock the rport.
954 */
955static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700956 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800957{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700958 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800959 struct fc_lport *lport = rdata->local_port;
Robert Lovea29e7642009-04-21 16:27:41 -0700960 struct fc_els_flogi *plp = NULL;
Robert Love42e9a922008-12-09 15:10:17 -0800961 u16 csp_seq;
962 u16 cssp_seq;
963 u8 op;
964
Joe Eykholtf657d292009-08-25 14:03:21 -0700965 FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800966
Chad Dupuis785141c2016-09-30 11:01:16 +0200967 if (fp == ERR_PTR(-FC_EX_CLOSED))
968 goto put;
969
970 mutex_lock(&rdata->rp_mutex);
971
Robert Love42e9a922008-12-09 15:10:17 -0800972 if (rdata->rp_state != RPORT_ST_PLOGI) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700973 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
974 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700975 if (IS_ERR(fp))
976 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800977 goto out;
978 }
979
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700980 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700981 fc_rport_error_retry(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700982 goto err;
983 }
984
Robert Love42e9a922008-12-09 15:10:17 -0800985 op = fc_frame_payload_op(fp);
986 if (op == ELS_LS_ACC &&
987 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
Joe Eykholtf211fa52009-08-25 14:01:01 -0700988 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
989 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
Robert Love42e9a922008-12-09 15:10:17 -0800990
Bhanu Prakash Gollapudi75a27922011-01-28 16:05:27 -0800991 /* save plogi response sp_features for further reference */
992 rdata->sp_features = ntohs(plp->fl_csp.sp_features);
993
Joe Eykholta7b12a22010-07-20 15:20:08 -0700994 if (lport->point_to_multipoint)
995 fc_rport_login_complete(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800996 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
997 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
998 if (cssp_seq < csp_seq)
999 csp_seq = cssp_seq;
1000 rdata->max_seq = csp_seq;
Joe Eykholtf211fa52009-08-25 14:01:01 -07001001 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001002 fc_rport_enter_prli(rdata);
Robert Love42e9a922008-12-09 15:10:17 -08001003 } else
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001004 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001005
1006out:
1007 fc_frame_free(fp);
1008err:
1009 mutex_unlock(&rdata->rp_mutex);
Chad Dupuis785141c2016-09-30 11:01:16 +02001010put:
Hannes Reineckebaa67192016-05-24 08:11:58 +02001011 kref_put(&rdata->kref, lport->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -08001012}
1013
Mark Rustade0335f62013-05-18 04:01:36 +00001014static bool
1015fc_rport_compatible_roles(struct fc_lport *lport, struct fc_rport_priv *rdata)
1016{
1017 if (rdata->ids.roles == FC_PORT_ROLE_UNKNOWN)
1018 return true;
1019 if ((rdata->ids.roles & FC_PORT_ROLE_FCP_TARGET) &&
1020 (lport->service_params & FCP_SPPF_INIT_FCN))
1021 return true;
1022 if ((rdata->ids.roles & FC_PORT_ROLE_FCP_INITIATOR) &&
1023 (lport->service_params & FCP_SPPF_TARG_FCN))
1024 return true;
1025 return false;
1026}
1027
Robert Love42e9a922008-12-09 15:10:17 -08001028/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001029 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
1030 * @rdata: The remote port to send a PLOGI to
Robert Love42e9a922008-12-09 15:10:17 -08001031 *
1032 * Locking Note: The rport lock is expected to be held before calling
1033 * this routine.
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001034 *
1035 * Reference counting: increments kref when sending ELS
Robert Love42e9a922008-12-09 15:10:17 -08001036 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001037static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -08001038{
Robert Love42e9a922008-12-09 15:10:17 -08001039 struct fc_lport *lport = rdata->local_port;
1040 struct fc_frame *fp;
1041
Mark Rustade0335f62013-05-18 04:01:36 +00001042 if (!fc_rport_compatible_roles(lport, rdata)) {
1043 FC_RPORT_DBG(rdata, "PLOGI suppressed for incompatible role\n");
1044 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
1045 return;
1046 }
1047
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001048 FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
1049 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001050
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001051 fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
Robert Love42e9a922008-12-09 15:10:17 -08001052
Joe Eykholtf211fa52009-08-25 14:01:01 -07001053 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
Robert Love42e9a922008-12-09 15:10:17 -08001054 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
1055 if (!fp) {
Joe Eykholta7b12a22010-07-20 15:20:08 -07001056 FC_RPORT_DBG(rdata, "%s frame alloc failed\n", __func__);
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001057 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001058 return;
1059 }
1060 rdata->e_d_tov = lport->e_d_tov;
1061
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001062 kref_get(&rdata->kref);
Joe Eykholtf211fa52009-08-25 14:01:01 -07001063 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
Joe Eykholtb94f8952009-11-03 11:50:21 -08001064 fc_rport_plogi_resp, rdata,
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001065 2 * lport->r_a_tov)) {
Chris Leech8f550f92009-10-21 16:28:09 -07001066 fc_rport_error_retry(rdata, NULL);
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001067 kref_put(&rdata->kref, lport->tt.rport_destroy);
1068 }
Robert Love42e9a922008-12-09 15:10:17 -08001069}
1070
1071/**
Robert Love34f42a02009-02-27 10:55:45 -08001072 * fc_rport_prli_resp() - Process Login (PRLI) response handler
Robert Love3a3b42b2009-11-03 11:47:39 -08001073 * @sp: The sequence the PRLI response was on
1074 * @fp: The PRLI response frame
1075 * @rdata_arg: The remote port that sent the PRLI response
Robert Love42e9a922008-12-09 15:10:17 -08001076 *
1077 * Locking Note: This function will be called without the rport lock
1078 * held, but it will lock, call an _enter_* function or fc_rport_error
1079 * and then unlock the rport.
1080 */
1081static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001082 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -08001083{
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001084 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -08001085 struct {
1086 struct fc_els_prli prli;
1087 struct fc_els_spp spp;
1088 } *pp;
Joe Eykholt925ceda2011-01-28 16:04:23 -08001089 struct fc_els_spp temp_spp;
1090 struct fc4_prov *prov;
Robert Love42e9a922008-12-09 15:10:17 -08001091 u32 roles = FC_RPORT_ROLE_UNKNOWN;
1092 u32 fcp_parm = 0;
1093 u8 op;
Bhanu Prakash Gollapudi618461c2010-06-11 16:43:54 -07001094 u8 resp_code = 0;
Robert Love42e9a922008-12-09 15:10:17 -08001095
Joe Eykholtf657d292009-08-25 14:03:21 -07001096 FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -08001097
Chad Dupuis785141c2016-09-30 11:01:16 +02001098 if (fp == ERR_PTR(-FC_EX_CLOSED))
1099 goto put;
1100
1101 mutex_lock(&rdata->rp_mutex);
1102
Robert Love42e9a922008-12-09 15:10:17 -08001103 if (rdata->rp_state != RPORT_ST_PRLI) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001104 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
1105 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001106 if (IS_ERR(fp))
1107 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001108 goto out;
1109 }
1110
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001111 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001112 fc_rport_error_retry(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001113 goto err;
1114 }
1115
Robert Love6bd054c2009-08-25 14:03:04 -07001116 /* reinitialize remote port roles */
1117 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
1118
Robert Love42e9a922008-12-09 15:10:17 -08001119 op = fc_frame_payload_op(fp);
1120 if (op == ELS_LS_ACC) {
1121 pp = fc_frame_payload_get(fp, sizeof(*pp));
Bhanu Prakash Gollapudi618461c2010-06-11 16:43:54 -07001122 if (!pp)
1123 goto out;
1124
1125 resp_code = (pp->spp.spp_flags & FC_SPP_RESP_MASK);
1126 FC_RPORT_DBG(rdata, "PRLI spp_flags = 0x%x\n",
1127 pp->spp.spp_flags);
Bhanu Prakash Gollapudi75a27922011-01-28 16:05:27 -08001128 rdata->spp_type = pp->spp.spp_type;
Bhanu Prakash Gollapudi618461c2010-06-11 16:43:54 -07001129 if (resp_code != FC_SPP_RESP_ACK) {
1130 if (resp_code == FC_SPP_RESP_CONF)
1131 fc_rport_error(rdata, fp);
1132 else
1133 fc_rport_error_retry(rdata, fp);
1134 goto out;
Robert Love42e9a922008-12-09 15:10:17 -08001135 }
Bhanu Prakash Gollapudi618461c2010-06-11 16:43:54 -07001136 if (pp->prli.prli_spp_len < sizeof(pp->spp))
1137 goto out;
1138
1139 fcp_parm = ntohl(pp->spp.spp_params);
1140 if (fcp_parm & FCP_SPPF_RETRY)
1141 rdata->flags |= FC_RP_FLAGS_RETRY;
Bhanu Prakash Gollapudi75a27922011-01-28 16:05:27 -08001142 if (fcp_parm & FCP_SPPF_CONF_COMPL)
1143 rdata->flags |= FC_RP_FLAGS_CONF_REQ;
Robert Love42e9a922008-12-09 15:10:17 -08001144
Joe Eykholt925ceda2011-01-28 16:04:23 -08001145 prov = fc_passive_prov[FC_TYPE_FCP];
1146 if (prov) {
1147 memset(&temp_spp, 0, sizeof(temp_spp));
1148 prov->prli(rdata, pp->prli.prli_spp_len,
1149 &pp->spp, &temp_spp);
1150 }
1151
Joe Eykholtf211fa52009-08-25 14:01:01 -07001152 rdata->supported_classes = FC_COS_CLASS3;
Robert Love42e9a922008-12-09 15:10:17 -08001153 if (fcp_parm & FCP_SPPF_INIT_FCN)
1154 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1155 if (fcp_parm & FCP_SPPF_TARG_FCN)
1156 roles |= FC_RPORT_ROLE_FCP_TARGET;
1157
Joe Eykholtf211fa52009-08-25 14:01:01 -07001158 rdata->ids.roles = roles;
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001159 fc_rport_enter_rtv(rdata);
Robert Love42e9a922008-12-09 15:10:17 -08001160
1161 } else {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001162 FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
Bhanu Prakash Gollapudi292e40b2010-06-11 16:43:49 -07001163 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001164 }
1165
1166out:
1167 fc_frame_free(fp);
1168err:
1169 mutex_unlock(&rdata->rp_mutex);
Chad Dupuis785141c2016-09-30 11:01:16 +02001170put:
Joe Eykholtf211fa52009-08-25 14:01:01 -07001171 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -08001172}
1173
1174/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001175 * fc_rport_enter_prli() - Send Process Login (PRLI) request
1176 * @rdata: The remote port to send the PRLI request to
Robert Love42e9a922008-12-09 15:10:17 -08001177 *
1178 * Locking Note: The rport lock is expected to be held before calling
1179 * this routine.
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001180 *
1181 * Reference counting: increments kref when sending ELS
Robert Love42e9a922008-12-09 15:10:17 -08001182 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001183static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -08001184{
Robert Love42e9a922008-12-09 15:10:17 -08001185 struct fc_lport *lport = rdata->local_port;
1186 struct {
1187 struct fc_els_prli prli;
1188 struct fc_els_spp spp;
1189 } *pp;
1190 struct fc_frame *fp;
Joe Eykholt925ceda2011-01-28 16:04:23 -08001191 struct fc4_prov *prov;
Robert Love42e9a922008-12-09 15:10:17 -08001192
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001193 /*
1194 * If the rport is one of the well known addresses
1195 * we skip PRLI and RTV and go straight to READY.
1196 */
1197 if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
1198 fc_rport_enter_ready(rdata);
1199 return;
1200 }
1201
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001202 FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
1203 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001204
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001205 fc_rport_state_enter(rdata, RPORT_ST_PRLI);
Robert Love42e9a922008-12-09 15:10:17 -08001206
1207 fp = fc_frame_alloc(lport, sizeof(*pp));
1208 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001209 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001210 return;
1211 }
1212
Joe Eykholt925ceda2011-01-28 16:04:23 -08001213 fc_prli_fill(lport, fp);
1214
1215 prov = fc_passive_prov[FC_TYPE_FCP];
1216 if (prov) {
1217 pp = fc_frame_payload_get(fp, sizeof(*pp));
1218 prov->prli(rdata, sizeof(pp->spp), NULL, &pp->spp);
1219 }
1220
1221 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rdata->ids.port_id,
1222 fc_host_port_id(lport->host), FC_TYPE_ELS,
1223 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1224
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001225 kref_get(&rdata->kref);
Joe Eykholt925ceda2011-01-28 16:04:23 -08001226 if (!lport->tt.exch_seq_send(lport, fp, fc_rport_prli_resp,
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001227 NULL, rdata, 2 * lport->r_a_tov)) {
Chris Leech8f550f92009-10-21 16:28:09 -07001228 fc_rport_error_retry(rdata, NULL);
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001229 kref_put(&rdata->kref, lport->tt.rport_destroy);
1230 }
Robert Love42e9a922008-12-09 15:10:17 -08001231}
1232
1233/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001234 * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses
1235 * @sp: The sequence the RTV was on
1236 * @fp: The RTV response frame
1237 * @rdata_arg: The remote port that sent the RTV response
Robert Love42e9a922008-12-09 15:10:17 -08001238 *
1239 * Many targets don't seem to support this.
1240 *
1241 * Locking Note: This function will be called without the rport lock
1242 * held, but it will lock, call an _enter_* function or fc_rport_error
1243 * and then unlock the rport.
1244 */
1245static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001246 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -08001247{
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001248 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -08001249 u8 op;
1250
Joe Eykholtf657d292009-08-25 14:03:21 -07001251 FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -08001252
Chad Dupuis785141c2016-09-30 11:01:16 +02001253 if (fp == ERR_PTR(-FC_EX_CLOSED))
1254 goto put;
1255
1256 mutex_lock(&rdata->rp_mutex);
1257
Robert Love42e9a922008-12-09 15:10:17 -08001258 if (rdata->rp_state != RPORT_ST_RTV) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001259 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
1260 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001261 if (IS_ERR(fp))
1262 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001263 goto out;
1264 }
1265
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001266 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001267 fc_rport_error(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001268 goto err;
1269 }
1270
Robert Love42e9a922008-12-09 15:10:17 -08001271 op = fc_frame_payload_op(fp);
1272 if (op == ELS_LS_ACC) {
1273 struct fc_els_rtv_acc *rtv;
1274 u32 toq;
1275 u32 tov;
1276
1277 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
1278 if (rtv) {
1279 toq = ntohl(rtv->rtv_toq);
1280 tov = ntohl(rtv->rtv_r_a_tov);
1281 if (tov == 0)
1282 tov = 1;
1283 rdata->r_a_tov = tov;
1284 tov = ntohl(rtv->rtv_e_d_tov);
1285 if (toq & FC_ELS_RTV_EDRES)
1286 tov /= 1000000;
1287 if (tov == 0)
1288 tov = 1;
1289 rdata->e_d_tov = tov;
1290 }
1291 }
1292
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001293 fc_rport_enter_ready(rdata);
Robert Love42e9a922008-12-09 15:10:17 -08001294
1295out:
1296 fc_frame_free(fp);
1297err:
1298 mutex_unlock(&rdata->rp_mutex);
Chad Dupuis785141c2016-09-30 11:01:16 +02001299put:
Joe Eykholtf211fa52009-08-25 14:01:01 -07001300 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -08001301}
1302
1303/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001304 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
1305 * @rdata: The remote port to send the RTV request to
Robert Love42e9a922008-12-09 15:10:17 -08001306 *
1307 * Locking Note: The rport lock is expected to be held before calling
1308 * this routine.
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001309 *
1310 * Reference counting: increments kref when sending ELS
Robert Love42e9a922008-12-09 15:10:17 -08001311 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001312static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -08001313{
1314 struct fc_frame *fp;
Robert Love42e9a922008-12-09 15:10:17 -08001315 struct fc_lport *lport = rdata->local_port;
1316
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001317 FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
1318 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001319
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001320 fc_rport_state_enter(rdata, RPORT_ST_RTV);
Robert Love42e9a922008-12-09 15:10:17 -08001321
1322 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
1323 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001324 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001325 return;
1326 }
1327
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001328 kref_get(&rdata->kref);
Joe Eykholtf211fa52009-08-25 14:01:01 -07001329 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
Joe Eykholtb94f8952009-11-03 11:50:21 -08001330 fc_rport_rtv_resp, rdata,
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001331 2 * lport->r_a_tov)) {
Chris Leech8f550f92009-10-21 16:28:09 -07001332 fc_rport_error_retry(rdata, NULL);
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001333 kref_put(&rdata->kref, lport->tt.rport_destroy);
1334 }
Robert Love42e9a922008-12-09 15:10:17 -08001335}
1336
1337/**
Joe Eykholt079ecd82010-07-20 15:20:51 -07001338 * fc_rport_logo_resp() - Handler for logout (LOGO) responses
1339 * @sp: The sequence the LOGO was on
1340 * @fp: The LOGO response frame
1341 * @lport_arg: The local port
1342 */
1343static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001344 void *rdata_arg)
Joe Eykholt079ecd82010-07-20 15:20:51 -07001345{
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001346 struct fc_rport_priv *rdata = rdata_arg;
1347 struct fc_lport *lport = rdata->local_port;
Joe Eykholt079ecd82010-07-20 15:20:51 -07001348
1349 FC_RPORT_ID_DBG(lport, fc_seq_exch(sp)->did,
1350 "Received a LOGO %s\n", fc_els_resp_type(fp));
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001351 if (!IS_ERR(fp))
1352 fc_frame_free(fp);
1353 kref_put(&rdata->kref, lport->tt.rport_destroy);
Joe Eykholt079ecd82010-07-20 15:20:51 -07001354}
1355
1356/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001357 * fc_rport_enter_logo() - Send a logout (LOGO) request
1358 * @rdata: The remote port to send the LOGO request to
Robert Love42e9a922008-12-09 15:10:17 -08001359 *
1360 * Locking Note: The rport lock is expected to be held before calling
1361 * this routine.
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001362 *
1363 * Reference counting: increments kref when sending ELS
Robert Love42e9a922008-12-09 15:10:17 -08001364 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001365static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -08001366{
Robert Love42e9a922008-12-09 15:10:17 -08001367 struct fc_lport *lport = rdata->local_port;
1368 struct fc_frame *fp;
1369
Joe Eykholt079ecd82010-07-20 15:20:51 -07001370 FC_RPORT_DBG(rdata, "Port sending LOGO from %s state\n",
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001371 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001372
Robert Love42e9a922008-12-09 15:10:17 -08001373 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
Joe Eykholt079ecd82010-07-20 15:20:51 -07001374 if (!fp)
Robert Love42e9a922008-12-09 15:10:17 -08001375 return;
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001376 kref_get(&rdata->kref);
1377 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
1378 fc_rport_logo_resp, rdata, 0))
1379 kref_put(&rdata->kref, lport->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -08001380}
1381
Robert Love42e9a922008-12-09 15:10:17 -08001382/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001383 * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
1384 * @sp: The sequence the ADISC response was on
1385 * @fp: The ADISC response frame
1386 * @rdata_arg: The remote port that sent the ADISC response
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001387 *
1388 * Locking Note: This function will be called without the rport lock
1389 * held, but it will lock, call an _enter_* function or fc_rport_error
1390 * and then unlock the rport.
1391 */
1392static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
Robert Love3a3b42b2009-11-03 11:47:39 -08001393 void *rdata_arg)
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001394{
1395 struct fc_rport_priv *rdata = rdata_arg;
1396 struct fc_els_adisc *adisc;
1397 u8 op;
1398
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001399 FC_RPORT_DBG(rdata, "Received a ADISC response\n");
1400
Chad Dupuis785141c2016-09-30 11:01:16 +02001401 if (fp == ERR_PTR(-FC_EX_CLOSED))
1402 goto put;
1403
1404 mutex_lock(&rdata->rp_mutex);
1405
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001406 if (rdata->rp_state != RPORT_ST_ADISC) {
1407 FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
1408 fc_rport_state(rdata));
1409 if (IS_ERR(fp))
1410 goto err;
1411 goto out;
1412 }
1413
1414 if (IS_ERR(fp)) {
1415 fc_rport_error(rdata, fp);
1416 goto err;
1417 }
1418
1419 /*
1420 * If address verification failed. Consider us logged out of the rport.
1421 * Since the rport is still in discovery, we want to be
1422 * logged in, so go to PLOGI state. Otherwise, go back to READY.
1423 */
1424 op = fc_frame_payload_op(fp);
1425 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1426 if (op != ELS_LS_ACC || !adisc ||
1427 ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
1428 get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
1429 get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
1430 FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
Joe Eykholta7b12a22010-07-20 15:20:08 -07001431 fc_rport_enter_flogi(rdata);
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001432 } else {
1433 FC_RPORT_DBG(rdata, "ADISC OK\n");
1434 fc_rport_enter_ready(rdata);
1435 }
1436out:
1437 fc_frame_free(fp);
1438err:
1439 mutex_unlock(&rdata->rp_mutex);
Chad Dupuis785141c2016-09-30 11:01:16 +02001440put:
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001441 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1442}
1443
1444/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001445 * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
1446 * @rdata: The remote port to send the ADISC request to
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001447 *
1448 * Locking Note: The rport lock is expected to be held before calling
1449 * this routine.
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001450 *
1451 * Reference counting: increments kref when sending ELS
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001452 */
1453static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
1454{
1455 struct fc_lport *lport = rdata->local_port;
1456 struct fc_frame *fp;
1457
1458 FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
1459 fc_rport_state(rdata));
1460
1461 fc_rport_state_enter(rdata, RPORT_ST_ADISC);
1462
1463 fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
1464 if (!fp) {
1465 fc_rport_error_retry(rdata, fp);
1466 return;
1467 }
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001468 kref_get(&rdata->kref);
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001469 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
Joe Eykholtb94f8952009-11-03 11:50:21 -08001470 fc_rport_adisc_resp, rdata,
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001471 2 * lport->r_a_tov)) {
Chris Leech8f550f92009-10-21 16:28:09 -07001472 fc_rport_error_retry(rdata, NULL);
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001473 kref_put(&rdata->kref, lport->tt.rport_destroy);
1474 }
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001475}
1476
1477/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001478 * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
1479 * @rdata: The remote port that sent the ADISC request
Robert Love3a3b42b2009-11-03 11:47:39 -08001480 * @in_fp: The ADISC request frame
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001481 *
1482 * Locking Note: Called with the lport and rport locks held.
1483 */
1484static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
Joe Eykholt922611562010-07-20 15:21:12 -07001485 struct fc_frame *in_fp)
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001486{
1487 struct fc_lport *lport = rdata->local_port;
1488 struct fc_frame *fp;
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001489 struct fc_els_adisc *adisc;
1490 struct fc_seq_els_data rjt_data;
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001491
1492 FC_RPORT_DBG(rdata, "Received ADISC request\n");
1493
1494 adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
1495 if (!adisc) {
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001496 rjt_data.reason = ELS_RJT_PROT;
1497 rjt_data.explan = ELS_EXPL_INV_LEN;
Joe Eykholt922611562010-07-20 15:21:12 -07001498 lport->tt.seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data);
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001499 goto drop;
1500 }
1501
1502 fp = fc_frame_alloc(lport, sizeof(*adisc));
1503 if (!fp)
1504 goto drop;
1505 fc_adisc_fill(lport, fp);
1506 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1507 adisc->adisc_cmd = ELS_LS_ACC;
Joe Eykholt24f089e2010-07-20 15:21:01 -07001508 fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
1509 lport->tt.frame_send(lport, fp);
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001510drop:
1511 fc_frame_free(in_fp);
1512}
1513
1514/**
Yi Zou63e27fb2009-11-20 14:55:24 -08001515 * fc_rport_recv_rls_req() - Handle received Read Link Status request
1516 * @rdata: The remote port that sent the RLS request
Yi Zou63e27fb2009-11-20 14:55:24 -08001517 * @rx_fp: The PRLI request frame
1518 *
1519 * Locking Note: The rport lock is expected to be held before calling
1520 * this function.
1521 */
1522static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
Joe Eykholt922611562010-07-20 15:21:12 -07001523 struct fc_frame *rx_fp)
Yi Zou63e27fb2009-11-20 14:55:24 -08001524
1525{
1526 struct fc_lport *lport = rdata->local_port;
1527 struct fc_frame *fp;
Yi Zou63e27fb2009-11-20 14:55:24 -08001528 struct fc_els_rls *rls;
1529 struct fc_els_rls_resp *rsp;
1530 struct fc_els_lesb *lesb;
1531 struct fc_seq_els_data rjt_data;
1532 struct fc_host_statistics *hst;
Yi Zou63e27fb2009-11-20 14:55:24 -08001533
1534 FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
1535 fc_rport_state(rdata));
1536
1537 rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
1538 if (!rls) {
1539 rjt_data.reason = ELS_RJT_PROT;
1540 rjt_data.explan = ELS_EXPL_INV_LEN;
1541 goto out_rjt;
1542 }
1543
1544 fp = fc_frame_alloc(lport, sizeof(*rsp));
1545 if (!fp) {
1546 rjt_data.reason = ELS_RJT_UNAB;
1547 rjt_data.explan = ELS_EXPL_INSUF_RES;
1548 goto out_rjt;
1549 }
1550
1551 rsp = fc_frame_payload_get(fp, sizeof(*rsp));
1552 memset(rsp, 0, sizeof(*rsp));
1553 rsp->rls_cmd = ELS_LS_ACC;
1554 lesb = &rsp->rls_lesb;
1555 if (lport->tt.get_lesb) {
1556 /* get LESB from LLD if it supports it */
1557 lport->tt.get_lesb(lport, lesb);
1558 } else {
1559 fc_get_host_stats(lport->host);
1560 hst = &lport->host_stats;
1561 lesb->lesb_link_fail = htonl(hst->link_failure_count);
1562 lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
1563 lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
1564 lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
1565 lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
1566 lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
1567 }
1568
Joe Eykholt24f089e2010-07-20 15:21:01 -07001569 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1570 lport->tt.frame_send(lport, fp);
Yi Zou63e27fb2009-11-20 14:55:24 -08001571 goto out;
1572
1573out_rjt:
Joe Eykholt922611562010-07-20 15:21:12 -07001574 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
Yi Zou63e27fb2009-11-20 14:55:24 -08001575out:
1576 fc_frame_free(rx_fp);
1577}
1578
1579/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001580 * fc_rport_recv_els_req() - Handler for validated ELS requests
1581 * @lport: The local port that received the ELS request
Robert Love3a3b42b2009-11-03 11:47:39 -08001582 * @fp: The ELS request frame
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001583 *
1584 * Handle incoming ELS requests that require port login.
1585 * The ELS opcode has already been validated by the caller.
Robert Love42e9a922008-12-09 15:10:17 -08001586 *
Joe Eykholt131203a2009-08-25 14:03:10 -07001587 * Locking Note: Called with the lport lock held.
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001588 *
1589 * Reference counting: does not modify kref
Robert Love42e9a922008-12-09 15:10:17 -08001590 */
Joe Eykholt922611562010-07-20 15:21:12 -07001591static void fc_rport_recv_els_req(struct fc_lport *lport, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -08001592{
Joe Eykholt131203a2009-08-25 14:03:10 -07001593 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -08001594 struct fc_seq_els_data els_data;
Robert Love42e9a922008-12-09 15:10:17 -08001595
Joe Eykholt251748a2010-07-20 15:20:56 -07001596 rdata = lport->tt.rport_lookup(lport, fc_frame_sid(fp));
Hannes Reineckebaa67192016-05-24 08:11:58 +02001597 if (!rdata)
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001598 goto reject;
Hannes Reineckebaa67192016-05-24 08:11:58 +02001599
Joe Eykholt131203a2009-08-25 14:03:10 -07001600 mutex_lock(&rdata->rp_mutex);
1601
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001602 switch (rdata->rp_state) {
1603 case RPORT_ST_PRLI:
1604 case RPORT_ST_RTV:
1605 case RPORT_ST_READY:
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001606 case RPORT_ST_ADISC:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001607 break;
1608 default:
1609 mutex_unlock(&rdata->rp_mutex);
Hannes Reineckebaa67192016-05-24 08:11:58 +02001610 kref_put(&rdata->kref, lport->tt.rport_destroy);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001611 goto reject;
1612 }
1613
1614 switch (fc_frame_payload_op(fp)) {
Joe Eykholt131203a2009-08-25 14:03:10 -07001615 case ELS_PRLI:
Joe Eykholt922611562010-07-20 15:21:12 -07001616 fc_rport_recv_prli_req(rdata, fp);
Joe Eykholt131203a2009-08-25 14:03:10 -07001617 break;
1618 case ELS_PRLO:
Joe Eykholt922611562010-07-20 15:21:12 -07001619 fc_rport_recv_prlo_req(rdata, fp);
Joe Eykholt131203a2009-08-25 14:03:10 -07001620 break;
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001621 case ELS_ADISC:
Joe Eykholt922611562010-07-20 15:21:12 -07001622 fc_rport_recv_adisc_req(rdata, fp);
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001623 break;
Joe Eykholt131203a2009-08-25 14:03:10 -07001624 case ELS_RRQ:
Joe Eykholt922611562010-07-20 15:21:12 -07001625 lport->tt.seq_els_rsp_send(fp, ELS_RRQ, NULL);
1626 fc_frame_free(fp);
Joe Eykholt131203a2009-08-25 14:03:10 -07001627 break;
1628 case ELS_REC:
Joe Eykholt922611562010-07-20 15:21:12 -07001629 lport->tt.seq_els_rsp_send(fp, ELS_REC, NULL);
1630 fc_frame_free(fp);
Joe Eykholt131203a2009-08-25 14:03:10 -07001631 break;
Yi Zou63e27fb2009-11-20 14:55:24 -08001632 case ELS_RLS:
Joe Eykholt922611562010-07-20 15:21:12 -07001633 fc_rport_recv_rls_req(rdata, fp);
Yi Zou63e27fb2009-11-20 14:55:24 -08001634 break;
Joe Eykholt131203a2009-08-25 14:03:10 -07001635 default:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001636 fc_frame_free(fp); /* can't happen */
Joe Eykholt131203a2009-08-25 14:03:10 -07001637 break;
Robert Love42e9a922008-12-09 15:10:17 -08001638 }
1639
1640 mutex_unlock(&rdata->rp_mutex);
Hannes Reineckebaa67192016-05-24 08:11:58 +02001641 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001642 return;
1643
1644reject:
Joe Eykholt922611562010-07-20 15:21:12 -07001645 els_data.reason = ELS_RJT_UNAB;
1646 els_data.explan = ELS_EXPL_PLOGI_REQD;
1647 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001648 fc_frame_free(fp);
1649}
1650
1651/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001652 * fc_rport_recv_req() - Handler for requests
Robert Love3a3b42b2009-11-03 11:47:39 -08001653 * @lport: The local port that received the request
Joe Eykholt922611562010-07-20 15:21:12 -07001654 * @fp: The request frame
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001655 *
1656 * Locking Note: Called with the lport lock held.
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001657 *
1658 * Reference counting: does not modify kref
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001659 */
Bart Van Asschec6b21c92012-01-13 17:26:20 -08001660static void fc_rport_recv_req(struct fc_lport *lport, struct fc_frame *fp)
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001661{
1662 struct fc_seq_els_data els_data;
1663
1664 /*
Joe Eykholta7b12a22010-07-20 15:20:08 -07001665 * Handle FLOGI, PLOGI and LOGO requests separately, since they
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001666 * don't require prior login.
1667 * Check for unsupported opcodes first and reject them.
1668 * For some ops, it would be incorrect to reject with "PLOGI required".
1669 */
1670 switch (fc_frame_payload_op(fp)) {
Joe Eykholta7b12a22010-07-20 15:20:08 -07001671 case ELS_FLOGI:
Joe Eykholt922611562010-07-20 15:21:12 -07001672 fc_rport_recv_flogi_req(lport, fp);
Joe Eykholta7b12a22010-07-20 15:20:08 -07001673 break;
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001674 case ELS_PLOGI:
Joe Eykholt922611562010-07-20 15:21:12 -07001675 fc_rport_recv_plogi_req(lport, fp);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001676 break;
1677 case ELS_LOGO:
Joe Eykholt922611562010-07-20 15:21:12 -07001678 fc_rport_recv_logo_req(lport, fp);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001679 break;
1680 case ELS_PRLI:
1681 case ELS_PRLO:
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001682 case ELS_ADISC:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001683 case ELS_RRQ:
1684 case ELS_REC:
Yi Zou63e27fb2009-11-20 14:55:24 -08001685 case ELS_RLS:
Joe Eykholt922611562010-07-20 15:21:12 -07001686 fc_rport_recv_els_req(lport, fp);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001687 break;
1688 default:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001689 els_data.reason = ELS_RJT_UNSUP;
1690 els_data.explan = ELS_EXPL_NONE;
Joe Eykholt922611562010-07-20 15:21:12 -07001691 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1692 fc_frame_free(fp);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001693 break;
1694 }
Robert Love42e9a922008-12-09 15:10:17 -08001695}
1696
1697/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001698 * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
1699 * @lport: The local port that received the PLOGI request
Robert Love3a3b42b2009-11-03 11:47:39 -08001700 * @rx_fp: The PLOGI request frame
Robert Love42e9a922008-12-09 15:10:17 -08001701 *
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001702 * Locking Note: The rport lock is held before calling this function.
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02001703 *
1704 * Reference counting: increments kref on return
Robert Love42e9a922008-12-09 15:10:17 -08001705 */
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001706static void fc_rport_recv_plogi_req(struct fc_lport *lport,
Joe Eykholt922611562010-07-20 15:21:12 -07001707 struct fc_frame *rx_fp)
Robert Love42e9a922008-12-09 15:10:17 -08001708{
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001709 struct fc_disc *disc;
1710 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -08001711 struct fc_frame *fp = rx_fp;
Robert Love42e9a922008-12-09 15:10:17 -08001712 struct fc_els_flogi *pl;
1713 struct fc_seq_els_data rjt_data;
Joe Eykholt24f089e2010-07-20 15:21:01 -07001714 u32 sid;
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001715
Joe Eykholt251748a2010-07-20 15:20:56 -07001716 sid = fc_frame_sid(fp);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001717
1718 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
1719
Robert Love42e9a922008-12-09 15:10:17 -08001720 pl = fc_frame_payload_get(fp, sizeof(*pl));
1721 if (!pl) {
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001722 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
1723 rjt_data.reason = ELS_RJT_PROT;
1724 rjt_data.explan = ELS_EXPL_INV_LEN;
1725 goto reject;
Robert Love42e9a922008-12-09 15:10:17 -08001726 }
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001727
1728 disc = &lport->disc;
1729 mutex_lock(&disc->disc_mutex);
1730 rdata = lport->tt.rport_create(lport, sid);
1731 if (!rdata) {
1732 mutex_unlock(&disc->disc_mutex);
1733 rjt_data.reason = ELS_RJT_UNAB;
1734 rjt_data.explan = ELS_EXPL_INSUF_RES;
1735 goto reject;
1736 }
1737
1738 mutex_lock(&rdata->rp_mutex);
1739 mutex_unlock(&disc->disc_mutex);
1740
1741 rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1742 rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
Robert Love42e9a922008-12-09 15:10:17 -08001743
1744 /*
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001745 * If the rport was just created, possibly due to the incoming PLOGI,
Robert Love42e9a922008-12-09 15:10:17 -08001746 * set the state appropriately and accept the PLOGI.
1747 *
1748 * If we had also sent a PLOGI, and if the received PLOGI is from a
1749 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1750 * "command already in progress".
1751 *
1752 * XXX TBD: If the session was ready before, the PLOGI should result in
1753 * all outstanding exchanges being reset.
1754 */
1755 switch (rdata->rp_state) {
1756 case RPORT_ST_INIT:
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001757 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
Robert Love42e9a922008-12-09 15:10:17 -08001758 break;
Joe Eykholta7b12a22010-07-20 15:20:08 -07001759 case RPORT_ST_PLOGI_WAIT:
1760 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI_WAIT state\n");
1761 break;
Robert Love42e9a922008-12-09 15:10:17 -08001762 case RPORT_ST_PLOGI:
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001763 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1764 if (rdata->ids.port_name < lport->wwpn) {
1765 mutex_unlock(&rdata->rp_mutex);
1766 rjt_data.reason = ELS_RJT_INPROG;
1767 rjt_data.explan = ELS_EXPL_NONE;
1768 goto reject;
1769 }
Robert Love42e9a922008-12-09 15:10:17 -08001770 break;
1771 case RPORT_ST_PRLI:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001772 case RPORT_ST_RTV:
Robert Love42e9a922008-12-09 15:10:17 -08001773 case RPORT_ST_READY:
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001774 case RPORT_ST_ADISC:
1775 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
1776 "- ignored for now\n", rdata->rp_state);
1777 /* XXX TBD - should reset */
Robert Love42e9a922008-12-09 15:10:17 -08001778 break;
Joe Eykholta7b12a22010-07-20 15:20:08 -07001779 case RPORT_ST_FLOGI:
Joe Eykholt14194052009-07-29 17:04:43 -07001780 case RPORT_ST_DELETE:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001781 FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
1782 fc_rport_state(rdata));
1783 mutex_unlock(&rdata->rp_mutex);
1784 rjt_data.reason = ELS_RJT_BUSY;
1785 rjt_data.explan = ELS_EXPL_NONE;
1786 goto reject;
Robert Love42e9a922008-12-09 15:10:17 -08001787 }
Mark Rustade0335f62013-05-18 04:01:36 +00001788 if (!fc_rport_compatible_roles(lport, rdata)) {
1789 FC_RPORT_DBG(rdata, "Received PLOGI for incompatible role\n");
1790 mutex_unlock(&rdata->rp_mutex);
1791 rjt_data.reason = ELS_RJT_LOGIC;
1792 rjt_data.explan = ELS_EXPL_NONE;
1793 goto reject;
1794 }
Robert Love42e9a922008-12-09 15:10:17 -08001795
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001796 /*
1797 * Get session payload size from incoming PLOGI.
1798 */
1799 rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
Robert Love42e9a922008-12-09 15:10:17 -08001800
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001801 /*
1802 * Send LS_ACC. If this fails, the originator should retry.
1803 */
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001804 fp = fc_frame_alloc(lport, sizeof(*pl));
1805 if (!fp)
1806 goto out;
Robert Love42e9a922008-12-09 15:10:17 -08001807
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001808 fc_plogi_fill(lport, fp, ELS_LS_ACC);
Joe Eykholt24f089e2010-07-20 15:21:01 -07001809 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1810 lport->tt.frame_send(lport, fp);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001811 fc_rport_enter_prli(rdata);
1812out:
1813 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt24f089e2010-07-20 15:21:01 -07001814 fc_frame_free(rx_fp);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001815 return;
1816
1817reject:
Joe Eykholt922611562010-07-20 15:21:12 -07001818 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001819 fc_frame_free(fp);
Robert Love42e9a922008-12-09 15:10:17 -08001820}
1821
1822/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001823 * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
1824 * @rdata: The remote port that sent the PRLI request
Robert Love3a3b42b2009-11-03 11:47:39 -08001825 * @rx_fp: The PRLI request frame
Robert Love42e9a922008-12-09 15:10:17 -08001826 *
Bart Van Asschec1d45422013-08-14 15:31:52 +00001827 * Locking Note: The rport lock is expected to be held before calling
Robert Love42e9a922008-12-09 15:10:17 -08001828 * this function.
1829 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001830static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
Joe Eykholt922611562010-07-20 15:21:12 -07001831 struct fc_frame *rx_fp)
Robert Love42e9a922008-12-09 15:10:17 -08001832{
Robert Love42e9a922008-12-09 15:10:17 -08001833 struct fc_lport *lport = rdata->local_port;
Robert Love42e9a922008-12-09 15:10:17 -08001834 struct fc_frame *fp;
Robert Love42e9a922008-12-09 15:10:17 -08001835 struct {
1836 struct fc_els_prli prli;
1837 struct fc_els_spp spp;
1838 } *pp;
1839 struct fc_els_spp *rspp; /* request service param page */
1840 struct fc_els_spp *spp; /* response spp */
1841 unsigned int len;
1842 unsigned int plen;
Robert Love42e9a922008-12-09 15:10:17 -08001843 enum fc_els_spp_resp resp;
Joe Eykholt96ad8462011-01-28 16:04:02 -08001844 enum fc_els_spp_resp passive;
Robert Love42e9a922008-12-09 15:10:17 -08001845 struct fc_seq_els_data rjt_data;
Joe Eykholt96ad8462011-01-28 16:04:02 -08001846 struct fc4_prov *prov;
Robert Love42e9a922008-12-09 15:10:17 -08001847
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001848 FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1849 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001850
Joe Eykholt251748a2010-07-20 15:20:56 -07001851 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
Robert Love42e9a922008-12-09 15:10:17 -08001852 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
Joe Eykholta2f6a022010-03-12 16:07:36 -08001853 if (!pp)
1854 goto reject_len;
1855 plen = ntohs(pp->prli.prli_len);
1856 if ((plen % 4) != 0 || plen > len || plen < 16)
1857 goto reject_len;
1858 if (plen < len)
1859 len = plen;
1860 plen = pp->prli.prli_spp_len;
1861 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1862 plen > len || len < sizeof(*pp) || plen < 12)
1863 goto reject_len;
1864 rspp = &pp->spp;
1865
1866 fp = fc_frame_alloc(lport, len);
1867 if (!fp) {
1868 rjt_data.reason = ELS_RJT_UNAB;
1869 rjt_data.explan = ELS_EXPL_INSUF_RES;
1870 goto reject;
Robert Love42e9a922008-12-09 15:10:17 -08001871 }
Joe Eykholta2f6a022010-03-12 16:07:36 -08001872 pp = fc_frame_payload_get(fp, len);
1873 WARN_ON(!pp);
1874 memset(pp, 0, len);
1875 pp->prli.prli_cmd = ELS_LS_ACC;
1876 pp->prli.prli_spp_len = plen;
1877 pp->prli.prli_len = htons(len);
1878 len -= sizeof(struct fc_els_prli);
Robert Love42e9a922008-12-09 15:10:17 -08001879
Joe Eykholta2f6a022010-03-12 16:07:36 -08001880 /*
1881 * Go through all the service parameter pages and build
1882 * response. If plen indicates longer SPP than standard,
1883 * use that. The entire response has been pre-cleared above.
1884 */
1885 spp = &pp->spp;
Joe Eykholt96ad8462011-01-28 16:04:02 -08001886 mutex_lock(&fc_prov_mutex);
Joe Eykholta2f6a022010-03-12 16:07:36 -08001887 while (len >= plen) {
Bhanu Prakash Gollapudi75a27922011-01-28 16:05:27 -08001888 rdata->spp_type = rspp->spp_type;
Joe Eykholta2f6a022010-03-12 16:07:36 -08001889 spp->spp_type = rspp->spp_type;
1890 spp->spp_type_ext = rspp->spp_type_ext;
Joe Eykholt96ad8462011-01-28 16:04:02 -08001891 resp = 0;
Robert Love42e9a922008-12-09 15:10:17 -08001892
Joe Eykholt96ad8462011-01-28 16:04:02 -08001893 if (rspp->spp_type < FC_FC4_PROV_SIZE) {
1894 prov = fc_active_prov[rspp->spp_type];
1895 if (prov)
1896 resp = prov->prli(rdata, plen, rspp, spp);
1897 prov = fc_passive_prov[rspp->spp_type];
1898 if (prov) {
1899 passive = prov->prli(rdata, plen, rspp, spp);
1900 if (!resp || passive == FC_SPP_RESP_ACK)
1901 resp = passive;
1902 }
1903 }
1904 if (!resp) {
1905 if (spp->spp_flags & FC_SPP_EST_IMG_PAIR)
1906 resp |= FC_SPP_RESP_CONF;
1907 else
1908 resp |= FC_SPP_RESP_INVL;
Robert Love42e9a922008-12-09 15:10:17 -08001909 }
Joe Eykholta2f6a022010-03-12 16:07:36 -08001910 spp->spp_flags |= resp;
1911 len -= plen;
1912 rspp = (struct fc_els_spp *)((char *)rspp + plen);
1913 spp = (struct fc_els_spp *)((char *)spp + plen);
Robert Love42e9a922008-12-09 15:10:17 -08001914 }
Joe Eykholt96ad8462011-01-28 16:04:02 -08001915 mutex_unlock(&fc_prov_mutex);
Joe Eykholta2f6a022010-03-12 16:07:36 -08001916
1917 /*
1918 * Send LS_ACC. If this fails, the originator should retry.
1919 */
Joe Eykholt24f089e2010-07-20 15:21:01 -07001920 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1921 lport->tt.frame_send(lport, fp);
Joe Eykholta2f6a022010-03-12 16:07:36 -08001922
1923 switch (rdata->rp_state) {
1924 case RPORT_ST_PRLI:
1925 fc_rport_enter_ready(rdata);
1926 break;
1927 default:
1928 break;
1929 }
1930 goto drop;
1931
1932reject_len:
1933 rjt_data.reason = ELS_RJT_PROT;
1934 rjt_data.explan = ELS_EXPL_INV_LEN;
1935reject:
Joe Eykholt922611562010-07-20 15:21:12 -07001936 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
Joe Eykholta2f6a022010-03-12 16:07:36 -08001937drop:
Robert Love42e9a922008-12-09 15:10:17 -08001938 fc_frame_free(rx_fp);
1939}
1940
1941/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001942 * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
1943 * @rdata: The remote port that sent the PRLO request
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001944 * @rx_fp: The PRLO request frame
Robert Love42e9a922008-12-09 15:10:17 -08001945 *
Bart Van Asschec1d45422013-08-14 15:31:52 +00001946 * Locking Note: The rport lock is expected to be held before calling
Robert Love42e9a922008-12-09 15:10:17 -08001947 * this function.
1948 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001949static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001950 struct fc_frame *rx_fp)
Robert Love42e9a922008-12-09 15:10:17 -08001951{
Robert Love42e9a922008-12-09 15:10:17 -08001952 struct fc_lport *lport = rdata->local_port;
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001953 struct fc_frame *fp;
1954 struct {
1955 struct fc_els_prlo prlo;
1956 struct fc_els_spp spp;
1957 } *pp;
1958 struct fc_els_spp *rspp; /* request service param page */
1959 struct fc_els_spp *spp; /* response spp */
1960 unsigned int len;
1961 unsigned int plen;
Robert Love42e9a922008-12-09 15:10:17 -08001962 struct fc_seq_els_data rjt_data;
1963
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001964 FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
1965 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001966
Joe Eykholt251748a2010-07-20 15:20:56 -07001967 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001968 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1969 if (!pp)
1970 goto reject_len;
1971 plen = ntohs(pp->prlo.prlo_len);
1972 if (plen != 20)
1973 goto reject_len;
1974 if (plen < len)
1975 len = plen;
1976
1977 rspp = &pp->spp;
1978
1979 fp = fc_frame_alloc(lport, len);
1980 if (!fp) {
1981 rjt_data.reason = ELS_RJT_UNAB;
1982 rjt_data.explan = ELS_EXPL_INSUF_RES;
1983 goto reject;
1984 }
1985
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001986 pp = fc_frame_payload_get(fp, len);
1987 WARN_ON(!pp);
1988 memset(pp, 0, len);
1989 pp->prlo.prlo_cmd = ELS_LS_ACC;
1990 pp->prlo.prlo_obs = 0x10;
1991 pp->prlo.prlo_len = htons(len);
1992 spp = &pp->spp;
1993 spp->spp_type = rspp->spp_type;
1994 spp->spp_type_ext = rspp->spp_type_ext;
1995 spp->spp_flags = FC_SPP_RESP_ACK;
1996
Hannes Reinecke166f3102016-08-05 14:55:00 +02001997 fc_rport_enter_prli(rdata);
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001998
Joe Eykholt922611562010-07-20 15:21:12 -07001999 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
2000 lport->tt.frame_send(lport, fp);
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07002001 goto drop;
2002
2003reject_len:
2004 rjt_data.reason = ELS_RJT_PROT;
2005 rjt_data.explan = ELS_EXPL_INV_LEN;
2006reject:
Joe Eykholt922611562010-07-20 15:21:12 -07002007 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07002008drop:
2009 fc_frame_free(rx_fp);
Robert Love42e9a922008-12-09 15:10:17 -08002010}
2011
2012/**
Robert Love3a3b42b2009-11-03 11:47:39 -08002013 * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
2014 * @lport: The local port that received the LOGO request
Robert Love3a3b42b2009-11-03 11:47:39 -08002015 * @fp: The LOGO request frame
Robert Love42e9a922008-12-09 15:10:17 -08002016 *
Bart Van Asschec1d45422013-08-14 15:31:52 +00002017 * Locking Note: The rport lock is expected to be held before calling
Robert Love42e9a922008-12-09 15:10:17 -08002018 * this function.
Hannes Reinecke4d2095c2016-09-30 11:01:14 +02002019 *
2020 * Reference counting: drops kref on return
Robert Love42e9a922008-12-09 15:10:17 -08002021 */
Joe Eykholt922611562010-07-20 15:21:12 -07002022static void fc_rport_recv_logo_req(struct fc_lport *lport, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -08002023{
Joe Eykholt83fe6a92009-08-25 14:03:31 -07002024 struct fc_rport_priv *rdata;
2025 u32 sid;
Robert Love42e9a922008-12-09 15:10:17 -08002026
Joe Eykholt922611562010-07-20 15:21:12 -07002027 lport->tt.seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
Joe Eykholtfeab4ae2009-08-25 14:03:36 -07002028
Joe Eykholt251748a2010-07-20 15:20:56 -07002029 sid = fc_frame_sid(fp);
Robert Love42e9a922008-12-09 15:10:17 -08002030
Joe Eykholt83fe6a92009-08-25 14:03:31 -07002031 rdata = lport->tt.rport_lookup(lport, sid);
2032 if (rdata) {
2033 mutex_lock(&rdata->rp_mutex);
2034 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
2035 fc_rport_state(rdata));
Joe Eykholtfeab4ae2009-08-25 14:03:36 -07002036
Hannes Reinecke649eb862016-08-05 14:55:02 +02002037 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07002038 mutex_unlock(&rdata->rp_mutex);
Hannes Reineckebaa67192016-05-24 08:11:58 +02002039 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07002040 } else
2041 FC_RPORT_ID_DBG(lport, sid,
2042 "Received LOGO from non-logged-in port\n");
Robert Love42e9a922008-12-09 15:10:17 -08002043 fc_frame_free(fp);
2044}
2045
Robert Love3a3b42b2009-11-03 11:47:39 -08002046/**
2047 * fc_rport_flush_queue() - Flush the rport_event_queue
2048 */
Robert Love42e9a922008-12-09 15:10:17 -08002049static void fc_rport_flush_queue(void)
2050{
2051 flush_workqueue(rport_event_queue);
2052}
2053
Robert Love3a3b42b2009-11-03 11:47:39 -08002054/**
2055 * fc_rport_init() - Initialize the remote port layer for a local port
2056 * @lport: The local port to initialize the remote port layer for
2057 */
Robert Love42e9a922008-12-09 15:10:17 -08002058int fc_rport_init(struct fc_lport *lport)
2059{
Joe Eykholt8025b5d2009-08-25 14:02:06 -07002060 if (!lport->tt.rport_lookup)
2061 lport->tt.rport_lookup = fc_rport_lookup;
2062
Robert Love5101ff92009-02-27 10:55:18 -08002063 if (!lport->tt.rport_create)
Joe Eykholt9e9d0452009-08-25 14:01:18 -07002064 lport->tt.rport_create = fc_rport_create;
Robert Love5101ff92009-02-27 10:55:18 -08002065
Robert Love42e9a922008-12-09 15:10:17 -08002066 if (!lport->tt.rport_login)
2067 lport->tt.rport_login = fc_rport_login;
2068
2069 if (!lport->tt.rport_logoff)
2070 lport->tt.rport_logoff = fc_rport_logoff;
2071
2072 if (!lport->tt.rport_recv_req)
2073 lport->tt.rport_recv_req = fc_rport_recv_req;
2074
2075 if (!lport->tt.rport_flush_queue)
2076 lport->tt.rport_flush_queue = fc_rport_flush_queue;
2077
Joe Eykholtf211fa52009-08-25 14:01:01 -07002078 if (!lport->tt.rport_destroy)
2079 lport->tt.rport_destroy = fc_rport_destroy;
2080
Robert Love42e9a922008-12-09 15:10:17 -08002081 return 0;
2082}
2083EXPORT_SYMBOL(fc_rport_init);
2084
Robert Love3a3b42b2009-11-03 11:47:39 -08002085/**
Joe Eykholt96ad8462011-01-28 16:04:02 -08002086 * fc_rport_fcp_prli() - Handle incoming PRLI for the FCP initiator.
2087 * @rdata: remote port private
2088 * @spp_len: service parameter page length
2089 * @rspp: received service parameter page
2090 * @spp: response service parameter page
2091 *
2092 * Returns the value for the response code to be placed in spp_flags;
2093 * Returns 0 if not an initiator.
2094 */
2095static int fc_rport_fcp_prli(struct fc_rport_priv *rdata, u32 spp_len,
2096 const struct fc_els_spp *rspp,
2097 struct fc_els_spp *spp)
2098{
2099 struct fc_lport *lport = rdata->local_port;
2100 u32 fcp_parm;
2101
2102 fcp_parm = ntohl(rspp->spp_params);
2103 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
2104 if (fcp_parm & FCP_SPPF_INIT_FCN)
2105 rdata->ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2106 if (fcp_parm & FCP_SPPF_TARG_FCN)
2107 rdata->ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2108 if (fcp_parm & FCP_SPPF_RETRY)
2109 rdata->flags |= FC_RP_FLAGS_RETRY;
2110 rdata->supported_classes = FC_COS_CLASS3;
2111
Mark Rustad732bdb92013-03-20 07:17:55 +00002112 if (!(lport->service_params & FCP_SPPF_INIT_FCN))
Joe Eykholt96ad8462011-01-28 16:04:02 -08002113 return 0;
2114
2115 spp->spp_flags |= rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
2116
2117 /*
2118 * OR in our service parameters with other providers (target), if any.
2119 */
2120 fcp_parm = ntohl(spp->spp_params);
2121 spp->spp_params = htonl(fcp_parm | lport->service_params);
2122 return FC_SPP_RESP_ACK;
2123}
2124
2125/*
2126 * FC-4 provider ops for FCP initiator.
2127 */
2128struct fc4_prov fc_rport_fcp_init = {
2129 .prli = fc_rport_fcp_prli,
2130};
2131
2132/**
2133 * fc_rport_t0_prli() - Handle incoming PRLI parameters for type 0
2134 * @rdata: remote port private
2135 * @spp_len: service parameter page length
2136 * @rspp: received service parameter page
2137 * @spp: response service parameter page
2138 */
2139static int fc_rport_t0_prli(struct fc_rport_priv *rdata, u32 spp_len,
2140 const struct fc_els_spp *rspp,
2141 struct fc_els_spp *spp)
2142{
2143 if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR)
2144 return FC_SPP_RESP_INVL;
2145 return FC_SPP_RESP_ACK;
2146}
2147
2148/*
2149 * FC-4 provider ops for type 0 service parameters.
2150 *
2151 * This handles the special case of type 0 which is always successful
2152 * but doesn't do anything otherwise.
2153 */
2154struct fc4_prov fc_rport_t0_prov = {
2155 .prli = fc_rport_t0_prli,
2156};
2157
2158/**
Robert Love3a3b42b2009-11-03 11:47:39 -08002159 * fc_setup_rport() - Initialize the rport_event_queue
2160 */
Randy Dunlap55204902011-01-28 16:03:57 -08002161int fc_setup_rport(void)
Robert Love42e9a922008-12-09 15:10:17 -08002162{
2163 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
2164 if (!rport_event_queue)
2165 return -ENOMEM;
2166 return 0;
2167}
Robert Love42e9a922008-12-09 15:10:17 -08002168
Robert Love3a3b42b2009-11-03 11:47:39 -08002169/**
2170 * fc_destroy_rport() - Destroy the rport_event_queue
2171 */
Randy Dunlap55204902011-01-28 16:03:57 -08002172void fc_destroy_rport(void)
Robert Love42e9a922008-12-09 15:10:17 -08002173{
2174 destroy_workqueue(rport_event_queue);
2175}
Robert Love42e9a922008-12-09 15:10:17 -08002176
Robert Love3a3b42b2009-11-03 11:47:39 -08002177/**
2178 * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
2179 * @rport: The remote port whose I/O should be terminated
2180 */
Robert Love42e9a922008-12-09 15:10:17 -08002181void fc_rport_terminate_io(struct fc_rport *rport)
2182{
Robert Love3a3b42b2009-11-03 11:47:39 -08002183 struct fc_rport_libfc_priv *rpriv = rport->dd_data;
2184 struct fc_lport *lport = rpriv->local_port;
Robert Love42e9a922008-12-09 15:10:17 -08002185
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -08002186 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
2187 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
Robert Love42e9a922008-12-09 15:10:17 -08002188}
2189EXPORT_SYMBOL(fc_rport_terminate_io);