blob: 9ded6123ff6ddc358ad68606302b707b7eec5424 [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
47#include <linux/kernel.h>
48#include <linux/spinlock.h>
49#include <linux/interrupt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Robert Love42e9a922008-12-09 15:10:17 -080051#include <linux/rcupdate.h>
52#include <linux/timer.h>
53#include <linux/workqueue.h>
54#include <asm/unaligned.h>
55
56#include <scsi/libfc.h>
57#include <scsi/fc_encode.h>
58
Robert Love8866a5d2009-11-03 11:45:58 -080059#include "fc_libfc.h"
60
Randy Dunlap55204902011-01-28 16:03:57 -080061static struct workqueue_struct *rport_event_queue;
Robert Love42e9a922008-12-09 15:10:17 -080062
Joe Eykholta7b12a22010-07-20 15:20:08 -070063static void fc_rport_enter_flogi(struct fc_rport_priv *);
Joe Eykholt9fb9d322009-08-25 14:00:50 -070064static void fc_rport_enter_plogi(struct fc_rport_priv *);
65static void fc_rport_enter_prli(struct fc_rport_priv *);
66static void fc_rport_enter_rtv(struct fc_rport_priv *);
67static void fc_rport_enter_ready(struct fc_rport_priv *);
68static void fc_rport_enter_logo(struct fc_rport_priv *);
Joe Eykholt370c3bd2009-08-25 14:03:47 -070069static void fc_rport_enter_adisc(struct fc_rport_priv *);
Robert Love42e9a922008-12-09 15:10:17 -080070
Joe Eykholt922611562010-07-20 15:21:12 -070071static void fc_rport_recv_plogi_req(struct fc_lport *, struct fc_frame *);
72static void fc_rport_recv_prli_req(struct fc_rport_priv *, struct fc_frame *);
73static void fc_rport_recv_prlo_req(struct fc_rport_priv *, struct fc_frame *);
74static void fc_rport_recv_logo_req(struct fc_lport *, struct fc_frame *);
Robert Love42e9a922008-12-09 15:10:17 -080075static void fc_rport_timeout(struct work_struct *);
Joe Eykholt9fb9d322009-08-25 14:00:50 -070076static void fc_rport_error(struct fc_rport_priv *, struct fc_frame *);
77static void fc_rport_error_retry(struct fc_rport_priv *, struct fc_frame *);
Robert Love42e9a922008-12-09 15:10:17 -080078static void fc_rport_work(struct work_struct *);
79
80static const char *fc_rport_state_names[] = {
Robert Love42e9a922008-12-09 15:10:17 -080081 [RPORT_ST_INIT] = "Init",
Joe Eykholta7b12a22010-07-20 15:20:08 -070082 [RPORT_ST_FLOGI] = "FLOGI",
83 [RPORT_ST_PLOGI_WAIT] = "PLOGI_WAIT",
Robert Love42e9a922008-12-09 15:10:17 -080084 [RPORT_ST_PLOGI] = "PLOGI",
85 [RPORT_ST_PRLI] = "PRLI",
86 [RPORT_ST_RTV] = "RTV",
87 [RPORT_ST_READY] = "Ready",
Joe Eykholt370c3bd2009-08-25 14:03:47 -070088 [RPORT_ST_ADISC] = "ADISC",
Joe Eykholt14194052009-07-29 17:04:43 -070089 [RPORT_ST_DELETE] = "Delete",
Robert Love42e9a922008-12-09 15:10:17 -080090};
91
Joe Eykholt9e9d0452009-08-25 14:01:18 -070092/**
Robert Love3a3b42b2009-11-03 11:47:39 -080093 * fc_rport_lookup() - Lookup a remote port by port_id
94 * @lport: The local port to lookup the remote port on
95 * @port_id: The remote port ID to look up
Joe Eykholt42e90412010-07-20 15:19:37 -070096 *
97 * The caller must hold either disc_mutex or rcu_read_lock().
Joe Eykholt8025b5d2009-08-25 14:02:06 -070098 */
99static struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport,
100 u32 port_id)
101{
102 struct fc_rport_priv *rdata;
103
Joe Eykholt42e90412010-07-20 15:19:37 -0700104 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers)
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700105 if (rdata->ids.port_id == port_id)
Joe Eykholt8025b5d2009-08-25 14:02:06 -0700106 return rdata;
107 return NULL;
108}
109
110/**
Robert Love9737e6a2009-08-25 14:02:59 -0700111 * fc_rport_create() - Create a new remote port
Robert Love3a3b42b2009-11-03 11:47:39 -0800112 * @lport: The local port this remote port will be associated with
113 * @ids: The identifiers for the new remote port
114 *
115 * The remote port will start in the INIT state.
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700116 *
Joe Eykholt48f00902009-08-25 14:01:50 -0700117 * Locking note: must be called with the disc_mutex held.
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700118 */
119static struct fc_rport_priv *fc_rport_create(struct fc_lport *lport,
Robert Love9737e6a2009-08-25 14:02:59 -0700120 u32 port_id)
Robert Love42e9a922008-12-09 15:10:17 -0800121{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700122 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800123
Robert Love9737e6a2009-08-25 14:02:59 -0700124 rdata = lport->tt.rport_lookup(lport, port_id);
Joe Eykholt19f97e32009-08-25 14:01:55 -0700125 if (rdata)
126 return rdata;
127
Joe Eykholtf90377a2010-07-20 15:19:42 -0700128 rdata = kzalloc(sizeof(*rdata) + lport->rport_priv_size, GFP_KERNEL);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700129 if (!rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800130 return NULL;
131
Robert Love9737e6a2009-08-25 14:02:59 -0700132 rdata->ids.node_name = -1;
133 rdata->ids.port_name = -1;
134 rdata->ids.port_id = port_id;
135 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
136
Joe Eykholtf211fa52009-08-25 14:01:01 -0700137 kref_init(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800138 mutex_init(&rdata->rp_mutex);
Joe Eykholt795d86f2009-08-25 14:00:39 -0700139 rdata->local_port = lport;
Robert Love42e9a922008-12-09 15:10:17 -0800140 rdata->rp_state = RPORT_ST_INIT;
141 rdata->event = RPORT_EV_NONE;
142 rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
Joe Eykholt795d86f2009-08-25 14:00:39 -0700143 rdata->e_d_tov = lport->e_d_tov;
144 rdata->r_a_tov = lport->r_a_tov;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700145 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
Robert Love42e9a922008-12-09 15:10:17 -0800146 INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
147 INIT_WORK(&rdata->event_work, fc_rport_work);
Robert Love9737e6a2009-08-25 14:02:59 -0700148 if (port_id != FC_FID_DIR_SERV)
Joe Eykholt42e90412010-07-20 15:19:37 -0700149 list_add_rcu(&rdata->peers, &lport->disc.rports);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700150 return rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800151}
152
153/**
Joe Eykholt42e90412010-07-20 15:19:37 -0700154 * fc_rport_free_rcu() - Free a remote port
155 * @rcu: The rcu_head structure inside the remote port
156 */
157static void fc_rport_free_rcu(struct rcu_head *rcu)
158{
159 struct fc_rport_priv *rdata;
160
161 rdata = container_of(rcu, struct fc_rport_priv, rcu);
162 kfree(rdata);
163}
164
165/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800166 * fc_rport_destroy() - Free a remote port after last reference is released
167 * @kref: The remote port's kref
Joe Eykholtf211fa52009-08-25 14:01:01 -0700168 */
169static void fc_rport_destroy(struct kref *kref)
170{
171 struct fc_rport_priv *rdata;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700172
173 rdata = container_of(kref, struct fc_rport_priv, kref);
Joe Eykholt42e90412010-07-20 15:19:37 -0700174 call_rcu(&rdata->rcu, fc_rport_free_rcu);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700175}
176
177/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800178 * fc_rport_state() - Return a string identifying the remote port's state
179 * @rdata: The remote port
Robert Love42e9a922008-12-09 15:10:17 -0800180 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700181static const char *fc_rport_state(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800182{
183 const char *cp;
Robert Love42e9a922008-12-09 15:10:17 -0800184
185 cp = fc_rport_state_names[rdata->rp_state];
186 if (!cp)
187 cp = "Unknown";
188 return cp;
189}
190
191/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800192 * fc_set_rport_loss_tmo() - Set the remote port loss timeout
193 * @rport: The remote port that gets a new timeout value
194 * @timeout: The new timeout value (in seconds)
Robert Love42e9a922008-12-09 15:10:17 -0800195 */
196void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
197{
198 if (timeout)
Mike Christie73b43762010-10-08 17:12:10 -0700199 rport->dev_loss_tmo = timeout;
Robert Love42e9a922008-12-09 15:10:17 -0800200 else
Mike Christie73b43762010-10-08 17:12:10 -0700201 rport->dev_loss_tmo = 1;
Robert Love42e9a922008-12-09 15:10:17 -0800202}
203EXPORT_SYMBOL(fc_set_rport_loss_tmo);
204
205/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800206 * fc_plogi_get_maxframe() - Get the maximum payload from the common service
207 * parameters in a FLOGI frame
Joe Eykholta7b12a22010-07-20 15:20:08 -0700208 * @flp: The FLOGI or PLOGI payload
Robert Love3a3b42b2009-11-03 11:47:39 -0800209 * @maxval: The maximum frame size upper limit; this may be less than what
210 * is in the service parameters
Robert Love42e9a922008-12-09 15:10:17 -0800211 */
Robert Loveb2ab99c2009-02-27 10:55:50 -0800212static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
213 unsigned int maxval)
Robert Love42e9a922008-12-09 15:10:17 -0800214{
215 unsigned int mfs;
216
217 /*
218 * Get max payload from the common service parameters and the
219 * class 3 receive data field size.
220 */
221 mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
222 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
223 maxval = mfs;
224 mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
225 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
226 maxval = mfs;
227 return maxval;
228}
229
230/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800231 * fc_rport_state_enter() - Change the state of a remote port
232 * @rdata: The remote port whose state should change
233 * @new: The new state
Robert Love42e9a922008-12-09 15:10:17 -0800234 *
235 * Locking Note: Called with the rport lock held
236 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700237static void fc_rport_state_enter(struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -0800238 enum fc_rport_state new)
239{
Robert Love42e9a922008-12-09 15:10:17 -0800240 if (rdata->rp_state != new)
241 rdata->retries = 0;
242 rdata->rp_state = new;
243}
244
Robert Love3a3b42b2009-11-03 11:47:39 -0800245/**
246 * fc_rport_work() - Handler for remote port events in the rport_event_queue
247 * @work: Handle to the remote port being dequeued
248 */
Robert Love42e9a922008-12-09 15:10:17 -0800249static void fc_rport_work(struct work_struct *work)
250{
Abhijeet Joglekar571f8242009-02-27 10:54:41 -0800251 u32 port_id;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700252 struct fc_rport_priv *rdata =
253 container_of(work, struct fc_rport_priv, event_work);
Robert Love3a3b42b2009-11-03 11:47:39 -0800254 struct fc_rport_libfc_priv *rpriv;
Robert Love42e9a922008-12-09 15:10:17 -0800255 enum fc_rport_event event;
Robert Love42e9a922008-12-09 15:10:17 -0800256 struct fc_lport *lport = rdata->local_port;
257 struct fc_rport_operations *rport_ops;
Joe Eykholt629f4422009-08-25 14:01:06 -0700258 struct fc_rport_identifiers ids;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700259 struct fc_rport *rport;
Joe Eykholt96ad8462011-01-28 16:04:02 -0800260 struct fc4_prov *prov;
261 u8 type;
Robert Love42e9a922008-12-09 15:10:17 -0800262
263 mutex_lock(&rdata->rp_mutex);
264 event = rdata->event;
265 rport_ops = rdata->ops;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700266 rport = rdata->rport;
Robert Love42e9a922008-12-09 15:10:17 -0800267
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700268 FC_RPORT_DBG(rdata, "work event %u\n", event);
269
Joe Eykholt629f4422009-08-25 14:01:06 -0700270 switch (event) {
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700271 case RPORT_EV_READY:
Joe Eykholtf211fa52009-08-25 14:01:01 -0700272 ids = rdata->ids;
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700273 rdata->event = RPORT_EV_NONE;
Joe Eykholtf0342602010-06-11 16:44:57 -0700274 rdata->major_retries = 0;
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700275 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800276 mutex_unlock(&rdata->rp_mutex);
277
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700278 if (!rport)
279 rport = fc_remote_port_add(lport->host, 0, &ids);
280 if (!rport) {
281 FC_RPORT_DBG(rdata, "Failed to add the rport\n");
282 lport->tt.rport_logoff(rdata);
283 kref_put(&rdata->kref, lport->tt.rport_destroy);
284 return;
Robert Love42e9a922008-12-09 15:10:17 -0800285 }
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700286 mutex_lock(&rdata->rp_mutex);
287 if (rdata->rport)
288 FC_RPORT_DBG(rdata, "rport already allocated\n");
289 rdata->rport = rport;
290 rport->maxframe_size = rdata->maxframe_size;
291 rport->supported_classes = rdata->supported_classes;
292
Robert Love3a3b42b2009-11-03 11:47:39 -0800293 rpriv = rport->dd_data;
294 rpriv->local_port = lport;
295 rpriv->rp_state = rdata->rp_state;
296 rpriv->flags = rdata->flags;
297 rpriv->e_d_tov = rdata->e_d_tov;
298 rpriv->r_a_tov = rdata->r_a_tov;
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700299 mutex_unlock(&rdata->rp_mutex);
300
Joe Eykholt83455922009-08-25 14:02:01 -0700301 if (rport_ops && rport_ops->event_callback) {
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700302 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700303 rport_ops->event_callback(lport, rdata, event);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700304 }
305 kref_put(&rdata->kref, lport->tt.rport_destroy);
Joe Eykholt629f4422009-08-25 14:01:06 -0700306 break;
307
308 case RPORT_EV_FAILED:
309 case RPORT_EV_LOGO:
310 case RPORT_EV_STOP:
Joe Eykholt96ad8462011-01-28 16:04:02 -0800311 if (rdata->prli_count) {
312 mutex_lock(&fc_prov_mutex);
313 for (type = 1; type < FC_FC4_PROV_SIZE; type++) {
314 prov = fc_passive_prov[type];
315 if (prov && prov->prlo)
316 prov->prlo(rdata);
317 }
318 mutex_unlock(&fc_prov_mutex);
319 }
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700320 port_id = rdata->ids.port_id;
Robert Love42e9a922008-12-09 15:10:17 -0800321 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700322
Joe Eykholt83455922009-08-25 14:02:01 -0700323 if (rport_ops && rport_ops->event_callback) {
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700324 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700325 rport_ops->event_callback(lport, rdata, event);
Abhijeet Joglekar571f8242009-02-27 10:54:41 -0800326 }
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700327 cancel_delayed_work_sync(&rdata->retry_work);
328
329 /*
330 * Reset any outstanding exchanges before freeing rport.
331 */
332 lport->tt.exch_mgr_reset(lport, 0, port_id);
333 lport->tt.exch_mgr_reset(lport, port_id, 0);
334
335 if (rport) {
Robert Love3a3b42b2009-11-03 11:47:39 -0800336 rpriv = rport->dd_data;
337 rpriv->rp_state = RPORT_ST_DELETE;
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700338 mutex_lock(&rdata->rp_mutex);
339 rdata->rport = NULL;
340 mutex_unlock(&rdata->rp_mutex);
341 fc_remote_port_delete(rport);
342 }
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700343
344 mutex_lock(&lport->disc.disc_mutex);
345 mutex_lock(&rdata->rp_mutex);
346 if (rdata->rp_state == RPORT_ST_DELETE) {
347 if (port_id == FC_FID_DIR_SERV) {
348 rdata->event = RPORT_EV_NONE;
349 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf0342602010-06-11 16:44:57 -0700350 } else if ((rdata->flags & FC_RP_STARTED) &&
351 rdata->major_retries <
352 lport->max_rport_retry_count) {
353 rdata->major_retries++;
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700354 rdata->event = RPORT_EV_NONE;
355 FC_RPORT_DBG(rdata, "work restart\n");
Joe Eykholta7b12a22010-07-20 15:20:08 -0700356 fc_rport_enter_flogi(rdata);
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700357 mutex_unlock(&rdata->rp_mutex);
358 } else {
359 FC_RPORT_DBG(rdata, "work delete\n");
Joe Eykholt42e90412010-07-20 15:19:37 -0700360 list_del_rcu(&rdata->peers);
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700361 mutex_unlock(&rdata->rp_mutex);
362 kref_put(&rdata->kref, lport->tt.rport_destroy);
363 }
364 } else {
365 /*
366 * Re-open for events. Reissue READY event if ready.
367 */
368 rdata->event = RPORT_EV_NONE;
369 if (rdata->rp_state == RPORT_ST_READY)
370 fc_rport_enter_ready(rdata);
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700371 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700372 }
373 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt629f4422009-08-25 14:01:06 -0700374 break;
375
376 default:
Robert Love42e9a922008-12-09 15:10:17 -0800377 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt629f4422009-08-25 14:01:06 -0700378 break;
379 }
Robert Love42e9a922008-12-09 15:10:17 -0800380}
381
382/**
Robert Love34f42a02009-02-27 10:55:45 -0800383 * fc_rport_login() - Start the remote port login state machine
Robert Love3a3b42b2009-11-03 11:47:39 -0800384 * @rdata: The remote port to be logged in to
Robert Love42e9a922008-12-09 15:10:17 -0800385 *
386 * Locking Note: Called without the rport lock held. This
387 * function will hold the rport lock, call an _enter_*
388 * function and then unlock the rport.
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700389 *
390 * This indicates the intent to be logged into the remote port.
391 * If it appears we are already logged in, ADISC is used to verify
392 * the setup.
Robert Love42e9a922008-12-09 15:10:17 -0800393 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700394int fc_rport_login(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800395{
Robert Love42e9a922008-12-09 15:10:17 -0800396 mutex_lock(&rdata->rp_mutex);
397
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700398 rdata->flags |= FC_RP_STARTED;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700399 switch (rdata->rp_state) {
400 case RPORT_ST_READY:
401 FC_RPORT_DBG(rdata, "ADISC port\n");
402 fc_rport_enter_adisc(rdata);
403 break;
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700404 case RPORT_ST_DELETE:
405 FC_RPORT_DBG(rdata, "Restart deleted port\n");
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700406 break;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700407 default:
408 FC_RPORT_DBG(rdata, "Login to port\n");
Joe Eykholta7b12a22010-07-20 15:20:08 -0700409 fc_rport_enter_flogi(rdata);
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700410 break;
411 }
Robert Love42e9a922008-12-09 15:10:17 -0800412 mutex_unlock(&rdata->rp_mutex);
413
414 return 0;
415}
416
417/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800418 * fc_rport_enter_delete() - Schedule a remote port to be deleted
419 * @rdata: The remote port to be deleted
420 * @event: The event to report as the reason for deletion
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700421 *
422 * Locking Note: Called with the rport lock held.
423 *
424 * Allow state change into DELETE only once.
425 *
426 * Call queue_work only if there's no event already pending.
427 * Set the new event so that the old pending event will not occur.
428 * Since we have the mutex, even if fc_rport_work() is already started,
429 * it'll see the new event.
430 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700431static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700432 enum fc_rport_event event)
433{
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700434 if (rdata->rp_state == RPORT_ST_DELETE)
435 return;
436
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700437 FC_RPORT_DBG(rdata, "Delete port\n");
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700438
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700439 fc_rport_state_enter(rdata, RPORT_ST_DELETE);
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700440
441 if (rdata->event == RPORT_EV_NONE)
442 queue_work(rport_event_queue, &rdata->event_work);
443 rdata->event = event;
444}
445
446/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800447 * fc_rport_logoff() - Logoff and remove a remote port
448 * @rdata: The remote port to be logged off of
Robert Love42e9a922008-12-09 15:10:17 -0800449 *
450 * Locking Note: Called without the rport lock held. This
451 * function will hold the rport lock, call an _enter_*
452 * function and then unlock the rport.
453 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700454int fc_rport_logoff(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800455{
Robert Love42e9a922008-12-09 15:10:17 -0800456 mutex_lock(&rdata->rp_mutex);
457
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700458 FC_RPORT_DBG(rdata, "Remove port\n");
Robert Love42e9a922008-12-09 15:10:17 -0800459
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700460 rdata->flags &= ~FC_RP_STARTED;
Joe Eykholt14194052009-07-29 17:04:43 -0700461 if (rdata->rp_state == RPORT_ST_DELETE) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700462 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700463 goto out;
464 }
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700465 fc_rport_enter_logo(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800466
467 /*
Joe Eykholt14194052009-07-29 17:04:43 -0700468 * Change the state to Delete so that we discard
Robert Love42e9a922008-12-09 15:10:17 -0800469 * the response.
470 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700471 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700472out:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700473 mutex_unlock(&rdata->rp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800474 return 0;
475}
476
477/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800478 * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state
479 * @rdata: The remote port that is ready
Robert Love42e9a922008-12-09 15:10:17 -0800480 *
481 * Locking Note: The rport lock is expected to be held before calling
482 * this routine.
483 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700484static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800485{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700486 fc_rport_state_enter(rdata, RPORT_ST_READY);
Robert Love42e9a922008-12-09 15:10:17 -0800487
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700488 FC_RPORT_DBG(rdata, "Port is Ready\n");
Robert Love42e9a922008-12-09 15:10:17 -0800489
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700490 if (rdata->event == RPORT_EV_NONE)
491 queue_work(rport_event_queue, &rdata->event_work);
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700492 rdata->event = RPORT_EV_READY;
Robert Love42e9a922008-12-09 15:10:17 -0800493}
494
495/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800496 * fc_rport_timeout() - Handler for the retry_work timer
497 * @work: Handle to the remote port that has timed out
Robert Love42e9a922008-12-09 15:10:17 -0800498 *
499 * Locking Note: Called without the rport lock held. This
500 * function will hold the rport lock, call an _enter_*
501 * function and then unlock the rport.
502 */
503static void fc_rport_timeout(struct work_struct *work)
504{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700505 struct fc_rport_priv *rdata =
506 container_of(work, struct fc_rport_priv, retry_work.work);
Robert Love42e9a922008-12-09 15:10:17 -0800507
508 mutex_lock(&rdata->rp_mutex);
509
510 switch (rdata->rp_state) {
Joe Eykholta7b12a22010-07-20 15:20:08 -0700511 case RPORT_ST_FLOGI:
512 fc_rport_enter_flogi(rdata);
513 break;
Robert Love42e9a922008-12-09 15:10:17 -0800514 case RPORT_ST_PLOGI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700515 fc_rport_enter_plogi(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800516 break;
517 case RPORT_ST_PRLI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700518 fc_rport_enter_prli(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800519 break;
520 case RPORT_ST_RTV:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700521 fc_rport_enter_rtv(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800522 break;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700523 case RPORT_ST_ADISC:
524 fc_rport_enter_adisc(rdata);
525 break;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700526 case RPORT_ST_PLOGI_WAIT:
Robert Love42e9a922008-12-09 15:10:17 -0800527 case RPORT_ST_READY:
528 case RPORT_ST_INIT:
Joe Eykholt14194052009-07-29 17:04:43 -0700529 case RPORT_ST_DELETE:
Robert Love42e9a922008-12-09 15:10:17 -0800530 break;
531 }
532
533 mutex_unlock(&rdata->rp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800534}
535
536/**
Robert Love34f42a02009-02-27 10:55:45 -0800537 * fc_rport_error() - Error handler, called once retries have been exhausted
Robert Love3a3b42b2009-11-03 11:47:39 -0800538 * @rdata: The remote port the error is happened on
539 * @fp: The error code encapsulated in a frame pointer
Robert Love42e9a922008-12-09 15:10:17 -0800540 *
Robert Love42e9a922008-12-09 15:10:17 -0800541 * Locking Note: The rport lock is expected to be held before
542 * calling this routine
543 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700544static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -0800545{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700546 FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
Joe Eykholtcdbe6df2009-08-25 14:01:39 -0700547 IS_ERR(fp) ? -PTR_ERR(fp) : 0,
548 fc_rport_state(rdata), rdata->retries);
Robert Love42e9a922008-12-09 15:10:17 -0800549
Chris Leech6755db12009-02-27 10:55:02 -0800550 switch (rdata->rp_state) {
Joe Eykholta7b12a22010-07-20 15:20:08 -0700551 case RPORT_ST_FLOGI:
Chris Leech6755db12009-02-27 10:55:02 -0800552 case RPORT_ST_PLOGI:
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700553 rdata->flags &= ~FC_RP_STARTED;
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700554 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
Chris Leech6755db12009-02-27 10:55:02 -0800555 break;
556 case RPORT_ST_RTV:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700557 fc_rport_enter_ready(rdata);
Chris Leech6755db12009-02-27 10:55:02 -0800558 break;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700559 case RPORT_ST_PRLI:
560 case RPORT_ST_ADISC:
561 fc_rport_enter_logo(rdata);
562 break;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700563 case RPORT_ST_PLOGI_WAIT:
Joe Eykholt14194052009-07-29 17:04:43 -0700564 case RPORT_ST_DELETE:
Chris Leech6755db12009-02-27 10:55:02 -0800565 case RPORT_ST_READY:
566 case RPORT_ST_INIT:
567 break;
Robert Love42e9a922008-12-09 15:10:17 -0800568 }
569}
570
571/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800572 * fc_rport_error_retry() - Handler for remote port state retries
573 * @rdata: The remote port whose state is to be retried
574 * @fp: The error code encapsulated in a frame pointer
Chris Leech6755db12009-02-27 10:55:02 -0800575 *
576 * If the error was an exchange timeout retry immediately,
577 * otherwise wait for E_D_TOV.
578 *
579 * Locking Note: The rport lock is expected to be held before
580 * calling this routine
581 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700582static void fc_rport_error_retry(struct fc_rport_priv *rdata,
583 struct fc_frame *fp)
Chris Leech6755db12009-02-27 10:55:02 -0800584{
Chris Leech6755db12009-02-27 10:55:02 -0800585 unsigned long delay = FC_DEF_E_D_TOV;
586
587 /* make sure this isn't an FC_EX_CLOSED error, never retry those */
588 if (PTR_ERR(fp) == -FC_EX_CLOSED)
Hillf Danton28a4af12011-01-28 16:03:26 -0800589 goto out;
Chris Leech6755db12009-02-27 10:55:02 -0800590
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700591 if (rdata->retries < rdata->local_port->max_rport_retry_count) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700592 FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
593 PTR_ERR(fp), fc_rport_state(rdata));
Chris Leech6755db12009-02-27 10:55:02 -0800594 rdata->retries++;
595 /* no additional delay on exchange timeouts */
596 if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
597 delay = 0;
Chris Leech6755db12009-02-27 10:55:02 -0800598 schedule_delayed_work(&rdata->retry_work, delay);
599 return;
600 }
601
Hillf Danton28a4af12011-01-28 16:03:26 -0800602out:
603 fc_rport_error(rdata, fp);
Chris Leech6755db12009-02-27 10:55:02 -0800604}
605
606/**
Joe Eykholta7b12a22010-07-20 15:20:08 -0700607 * fc_rport_login_complete() - Handle parameters and completion of p-mp login.
608 * @rdata: The remote port which we logged into or which logged into us.
609 * @fp: The FLOGI or PLOGI request or response frame
610 *
611 * Returns non-zero error if a problem is detected with the frame.
612 * Does not free the frame.
613 *
614 * This is only used in point-to-multipoint mode for FIP currently.
615 */
616static int fc_rport_login_complete(struct fc_rport_priv *rdata,
617 struct fc_frame *fp)
618{
619 struct fc_lport *lport = rdata->local_port;
620 struct fc_els_flogi *flogi;
621 unsigned int e_d_tov;
622 u16 csp_flags;
623
624 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
625 if (!flogi)
626 return -EINVAL;
627
628 csp_flags = ntohs(flogi->fl_csp.sp_features);
629
630 if (fc_frame_payload_op(fp) == ELS_FLOGI) {
631 if (csp_flags & FC_SP_FT_FPORT) {
632 FC_RPORT_DBG(rdata, "Fabric bit set in FLOGI\n");
633 return -EINVAL;
634 }
635 } else {
636
637 /*
638 * E_D_TOV is not valid on an incoming FLOGI request.
639 */
640 e_d_tov = ntohl(flogi->fl_csp.sp_e_d_tov);
641 if (csp_flags & FC_SP_FT_EDTR)
642 e_d_tov /= 1000000;
643 if (e_d_tov > rdata->e_d_tov)
644 rdata->e_d_tov = e_d_tov;
645 }
646 rdata->maxframe_size = fc_plogi_get_maxframe(flogi, lport->mfs);
647 return 0;
648}
649
650/**
651 * fc_rport_flogi_resp() - Handle response to FLOGI request for p-mp mode
652 * @sp: The sequence that the FLOGI was on
653 * @fp: The FLOGI response frame
654 * @rp_arg: The remote port that received the FLOGI response
655 */
656void fc_rport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
657 void *rp_arg)
658{
659 struct fc_rport_priv *rdata = rp_arg;
660 struct fc_lport *lport = rdata->local_port;
661 struct fc_els_flogi *flogi;
662 unsigned int r_a_tov;
663
664 FC_RPORT_DBG(rdata, "Received a FLOGI %s\n", fc_els_resp_type(fp));
665
666 if (fp == ERR_PTR(-FC_EX_CLOSED))
Hillf Danton0e9e3d32010-11-30 16:19:04 -0800667 goto put;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700668
669 mutex_lock(&rdata->rp_mutex);
670
671 if (rdata->rp_state != RPORT_ST_FLOGI) {
672 FC_RPORT_DBG(rdata, "Received a FLOGI response, but in state "
673 "%s\n", fc_rport_state(rdata));
674 if (IS_ERR(fp))
675 goto err;
676 goto out;
677 }
678
679 if (IS_ERR(fp)) {
680 fc_rport_error(rdata, fp);
681 goto err;
682 }
683
684 if (fc_frame_payload_op(fp) != ELS_LS_ACC)
685 goto bad;
686 if (fc_rport_login_complete(rdata, fp))
687 goto bad;
688
689 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
690 if (!flogi)
691 goto bad;
692 r_a_tov = ntohl(flogi->fl_csp.sp_r_a_tov);
693 if (r_a_tov > rdata->r_a_tov)
694 rdata->r_a_tov = r_a_tov;
695
696 if (rdata->ids.port_name < lport->wwpn)
697 fc_rport_enter_plogi(rdata);
698 else
699 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
700out:
701 fc_frame_free(fp);
702err:
703 mutex_unlock(&rdata->rp_mutex);
Hillf Danton0e9e3d32010-11-30 16:19:04 -0800704put:
Joe Eykholta7b12a22010-07-20 15:20:08 -0700705 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
706 return;
707bad:
708 FC_RPORT_DBG(rdata, "Bad FLOGI response\n");
709 fc_rport_error_retry(rdata, fp);
710 goto out;
711}
712
713/**
714 * fc_rport_enter_flogi() - Send a FLOGI request to the remote port for p-mp
715 * @rdata: The remote port to send a FLOGI to
716 *
717 * Locking Note: The rport lock is expected to be held before calling
718 * this routine.
719 */
720static void fc_rport_enter_flogi(struct fc_rport_priv *rdata)
721{
722 struct fc_lport *lport = rdata->local_port;
723 struct fc_frame *fp;
724
725 if (!lport->point_to_multipoint)
726 return fc_rport_enter_plogi(rdata);
727
728 FC_RPORT_DBG(rdata, "Entered FLOGI state from %s state\n",
729 fc_rport_state(rdata));
730
731 fc_rport_state_enter(rdata, RPORT_ST_FLOGI);
732
733 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
734 if (!fp)
735 return fc_rport_error_retry(rdata, fp);
736
737 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_FLOGI,
738 fc_rport_flogi_resp, rdata,
739 2 * lport->r_a_tov))
740 fc_rport_error_retry(rdata, NULL);
741 else
742 kref_get(&rdata->kref);
743}
744
745/**
746 * fc_rport_recv_flogi_req() - Handle Fabric Login (FLOGI) request in p-mp mode
747 * @lport: The local port that received the PLOGI request
Joe Eykholta7b12a22010-07-20 15:20:08 -0700748 * @rx_fp: The PLOGI request frame
749 */
750static void fc_rport_recv_flogi_req(struct fc_lport *lport,
Joe Eykholt922611562010-07-20 15:21:12 -0700751 struct fc_frame *rx_fp)
Joe Eykholta7b12a22010-07-20 15:20:08 -0700752{
753 struct fc_disc *disc;
754 struct fc_els_flogi *flp;
755 struct fc_rport_priv *rdata;
756 struct fc_frame *fp = rx_fp;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700757 struct fc_seq_els_data rjt_data;
Joe Eykholt24f089e2010-07-20 15:21:01 -0700758 u32 sid;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700759
Joe Eykholt251748a2010-07-20 15:20:56 -0700760 sid = fc_frame_sid(fp);
Joe Eykholta7b12a22010-07-20 15:20:08 -0700761
762 FC_RPORT_ID_DBG(lport, sid, "Received FLOGI request\n");
763
764 disc = &lport->disc;
765 mutex_lock(&disc->disc_mutex);
766
767 if (!lport->point_to_multipoint) {
768 rjt_data.reason = ELS_RJT_UNSUP;
769 rjt_data.explan = ELS_EXPL_NONE;
770 goto reject;
771 }
772
773 flp = fc_frame_payload_get(fp, sizeof(*flp));
774 if (!flp) {
775 rjt_data.reason = ELS_RJT_LOGIC;
776 rjt_data.explan = ELS_EXPL_INV_LEN;
777 goto reject;
778 }
779
780 rdata = lport->tt.rport_lookup(lport, sid);
781 if (!rdata) {
782 rjt_data.reason = ELS_RJT_FIP;
783 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
784 goto reject;
785 }
786 mutex_lock(&rdata->rp_mutex);
787
788 FC_RPORT_DBG(rdata, "Received FLOGI in %s state\n",
789 fc_rport_state(rdata));
790
791 switch (rdata->rp_state) {
792 case RPORT_ST_INIT:
Joe Eykholta7b12a22010-07-20 15:20:08 -0700793 case RPORT_ST_DELETE:
794 mutex_unlock(&rdata->rp_mutex);
795 rjt_data.reason = ELS_RJT_FIP;
796 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
797 goto reject;
798 case RPORT_ST_FLOGI:
799 case RPORT_ST_PLOGI_WAIT:
800 case RPORT_ST_PLOGI:
801 break;
802 case RPORT_ST_PRLI:
803 case RPORT_ST_RTV:
804 case RPORT_ST_READY:
805 case RPORT_ST_ADISC:
806 /*
807 * Set the remote port to be deleted and to then restart.
808 * This queues work to be sure exchanges are reset.
809 */
810 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
811 mutex_unlock(&rdata->rp_mutex);
812 rjt_data.reason = ELS_RJT_BUSY;
813 rjt_data.explan = ELS_EXPL_NONE;
814 goto reject;
815 }
816 if (fc_rport_login_complete(rdata, fp)) {
817 mutex_unlock(&rdata->rp_mutex);
818 rjt_data.reason = ELS_RJT_LOGIC;
819 rjt_data.explan = ELS_EXPL_NONE;
820 goto reject;
821 }
Joe Eykholta7b12a22010-07-20 15:20:08 -0700822
823 fp = fc_frame_alloc(lport, sizeof(*flp));
824 if (!fp)
825 goto out;
826
Joe Eykholta7b12a22010-07-20 15:20:08 -0700827 fc_flogi_fill(lport, fp);
828 flp = fc_frame_payload_get(fp, sizeof(*flp));
829 flp->fl_cmd = ELS_LS_ACC;
830
Joe Eykholt24f089e2010-07-20 15:21:01 -0700831 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
832 lport->tt.frame_send(lport, fp);
Joe Eykholta7b12a22010-07-20 15:20:08 -0700833
834 if (rdata->ids.port_name < lport->wwpn)
835 fc_rport_enter_plogi(rdata);
836 else
837 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
838out:
839 mutex_unlock(&rdata->rp_mutex);
840 mutex_unlock(&disc->disc_mutex);
Joe Eykholt24f089e2010-07-20 15:21:01 -0700841 fc_frame_free(rx_fp);
Joe Eykholta7b12a22010-07-20 15:20:08 -0700842 return;
843
844reject:
845 mutex_unlock(&disc->disc_mutex);
Joe Eykholt922611562010-07-20 15:21:12 -0700846 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
Joe Eykholt24f089e2010-07-20 15:21:01 -0700847 fc_frame_free(rx_fp);
Joe Eykholta7b12a22010-07-20 15:20:08 -0700848}
849
850/**
851 * fc_rport_plogi_resp() - Handler for ELS PLOGI responses
Robert Love3a3b42b2009-11-03 11:47:39 -0800852 * @sp: The sequence the PLOGI is on
853 * @fp: The PLOGI response frame
854 * @rdata_arg: The remote port that sent the PLOGI response
Robert Love42e9a922008-12-09 15:10:17 -0800855 *
856 * Locking Note: This function will be called without the rport lock
857 * held, but it will lock, call an _enter_* function or fc_rport_error
858 * and then unlock the rport.
859 */
860static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700861 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800862{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700863 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800864 struct fc_lport *lport = rdata->local_port;
Robert Lovea29e7642009-04-21 16:27:41 -0700865 struct fc_els_flogi *plp = NULL;
Robert Love42e9a922008-12-09 15:10:17 -0800866 u16 csp_seq;
867 u16 cssp_seq;
868 u8 op;
869
870 mutex_lock(&rdata->rp_mutex);
871
Joe Eykholtf657d292009-08-25 14:03:21 -0700872 FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800873
874 if (rdata->rp_state != RPORT_ST_PLOGI) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700875 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
876 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700877 if (IS_ERR(fp))
878 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800879 goto out;
880 }
881
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700882 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700883 fc_rport_error_retry(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700884 goto err;
885 }
886
Robert Love42e9a922008-12-09 15:10:17 -0800887 op = fc_frame_payload_op(fp);
888 if (op == ELS_LS_ACC &&
889 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
Joe Eykholtf211fa52009-08-25 14:01:01 -0700890 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
891 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
Robert Love42e9a922008-12-09 15:10:17 -0800892
Joe Eykholta7b12a22010-07-20 15:20:08 -0700893 if (lport->point_to_multipoint)
894 fc_rport_login_complete(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800895 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
896 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
897 if (cssp_seq < csp_seq)
898 csp_seq = cssp_seq;
899 rdata->max_seq = csp_seq;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700900 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
Joe Eykholt3ac6f982009-08-25 14:03:26 -0700901 fc_rport_enter_prli(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800902 } else
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700903 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800904
905out:
906 fc_frame_free(fp);
907err:
908 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700909 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800910}
911
912/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800913 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
914 * @rdata: The remote port to send a PLOGI to
Robert Love42e9a922008-12-09 15:10:17 -0800915 *
916 * Locking Note: The rport lock is expected to be held before calling
917 * this routine.
918 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700919static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800920{
Robert Love42e9a922008-12-09 15:10:17 -0800921 struct fc_lport *lport = rdata->local_port;
922 struct fc_frame *fp;
923
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700924 FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
925 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800926
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700927 fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
Robert Love42e9a922008-12-09 15:10:17 -0800928
Joe Eykholtf211fa52009-08-25 14:01:01 -0700929 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
Robert Love42e9a922008-12-09 15:10:17 -0800930 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
931 if (!fp) {
Joe Eykholta7b12a22010-07-20 15:20:08 -0700932 FC_RPORT_DBG(rdata, "%s frame alloc failed\n", __func__);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700933 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800934 return;
935 }
936 rdata->e_d_tov = lport->e_d_tov;
937
Joe Eykholtf211fa52009-08-25 14:01:01 -0700938 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
Joe Eykholtb94f8952009-11-03 11:50:21 -0800939 fc_rport_plogi_resp, rdata,
940 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -0700941 fc_rport_error_retry(rdata, NULL);
Robert Love42e9a922008-12-09 15:10:17 -0800942 else
Joe Eykholtf211fa52009-08-25 14:01:01 -0700943 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800944}
945
946/**
Robert Love34f42a02009-02-27 10:55:45 -0800947 * fc_rport_prli_resp() - Process Login (PRLI) response handler
Robert Love3a3b42b2009-11-03 11:47:39 -0800948 * @sp: The sequence the PRLI response was on
949 * @fp: The PRLI response frame
950 * @rdata_arg: The remote port that sent the PRLI response
Robert Love42e9a922008-12-09 15:10:17 -0800951 *
952 * Locking Note: This function will be called without the rport lock
953 * held, but it will lock, call an _enter_* function or fc_rport_error
954 * and then unlock the rport.
955 */
956static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700957 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800958{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700959 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800960 struct {
961 struct fc_els_prli prli;
962 struct fc_els_spp spp;
963 } *pp;
Joe Eykholt925ceda2011-01-28 16:04:23 -0800964 struct fc_els_spp temp_spp;
965 struct fc4_prov *prov;
Robert Love42e9a922008-12-09 15:10:17 -0800966 u32 roles = FC_RPORT_ROLE_UNKNOWN;
967 u32 fcp_parm = 0;
968 u8 op;
Bhanu Prakash Gollapudi618461c2010-06-11 16:43:54 -0700969 u8 resp_code = 0;
Robert Love42e9a922008-12-09 15:10:17 -0800970
971 mutex_lock(&rdata->rp_mutex);
972
Joe Eykholtf657d292009-08-25 14:03:21 -0700973 FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800974
975 if (rdata->rp_state != RPORT_ST_PRLI) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700976 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
977 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700978 if (IS_ERR(fp))
979 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800980 goto out;
981 }
982
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700983 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700984 fc_rport_error_retry(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700985 goto err;
986 }
987
Robert Love6bd054c2009-08-25 14:03:04 -0700988 /* reinitialize remote port roles */
989 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
990
Robert Love42e9a922008-12-09 15:10:17 -0800991 op = fc_frame_payload_op(fp);
992 if (op == ELS_LS_ACC) {
993 pp = fc_frame_payload_get(fp, sizeof(*pp));
Bhanu Prakash Gollapudi618461c2010-06-11 16:43:54 -0700994 if (!pp)
995 goto out;
996
997 resp_code = (pp->spp.spp_flags & FC_SPP_RESP_MASK);
998 FC_RPORT_DBG(rdata, "PRLI spp_flags = 0x%x\n",
999 pp->spp.spp_flags);
1000 if (resp_code != FC_SPP_RESP_ACK) {
1001 if (resp_code == FC_SPP_RESP_CONF)
1002 fc_rport_error(rdata, fp);
1003 else
1004 fc_rport_error_retry(rdata, fp);
1005 goto out;
Robert Love42e9a922008-12-09 15:10:17 -08001006 }
Bhanu Prakash Gollapudi618461c2010-06-11 16:43:54 -07001007 if (pp->prli.prli_spp_len < sizeof(pp->spp))
1008 goto out;
1009
1010 fcp_parm = ntohl(pp->spp.spp_params);
1011 if (fcp_parm & FCP_SPPF_RETRY)
1012 rdata->flags |= FC_RP_FLAGS_RETRY;
Robert Love42e9a922008-12-09 15:10:17 -08001013
Joe Eykholt925ceda2011-01-28 16:04:23 -08001014 prov = fc_passive_prov[FC_TYPE_FCP];
1015 if (prov) {
1016 memset(&temp_spp, 0, sizeof(temp_spp));
1017 prov->prli(rdata, pp->prli.prli_spp_len,
1018 &pp->spp, &temp_spp);
1019 }
1020
Joe Eykholtf211fa52009-08-25 14:01:01 -07001021 rdata->supported_classes = FC_COS_CLASS3;
Robert Love42e9a922008-12-09 15:10:17 -08001022 if (fcp_parm & FCP_SPPF_INIT_FCN)
1023 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1024 if (fcp_parm & FCP_SPPF_TARG_FCN)
1025 roles |= FC_RPORT_ROLE_FCP_TARGET;
1026
Joe Eykholtf211fa52009-08-25 14:01:01 -07001027 rdata->ids.roles = roles;
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001028 fc_rport_enter_rtv(rdata);
Robert Love42e9a922008-12-09 15:10:17 -08001029
1030 } else {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001031 FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
Bhanu Prakash Gollapudi292e40b2010-06-11 16:43:49 -07001032 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001033 }
1034
1035out:
1036 fc_frame_free(fp);
1037err:
1038 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -07001039 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -08001040}
1041
1042/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001043 * fc_rport_enter_prli() - Send Process Login (PRLI) request
1044 * @rdata: The remote port to send the PRLI request to
Robert Love42e9a922008-12-09 15:10:17 -08001045 *
1046 * Locking Note: The rport lock is expected to be held before calling
1047 * this routine.
1048 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001049static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -08001050{
Robert Love42e9a922008-12-09 15:10:17 -08001051 struct fc_lport *lport = rdata->local_port;
1052 struct {
1053 struct fc_els_prli prli;
1054 struct fc_els_spp spp;
1055 } *pp;
1056 struct fc_frame *fp;
Joe Eykholt925ceda2011-01-28 16:04:23 -08001057 struct fc4_prov *prov;
Robert Love42e9a922008-12-09 15:10:17 -08001058
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001059 /*
1060 * If the rport is one of the well known addresses
1061 * we skip PRLI and RTV and go straight to READY.
1062 */
1063 if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
1064 fc_rport_enter_ready(rdata);
1065 return;
1066 }
1067
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001068 FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
1069 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001070
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001071 fc_rport_state_enter(rdata, RPORT_ST_PRLI);
Robert Love42e9a922008-12-09 15:10:17 -08001072
1073 fp = fc_frame_alloc(lport, sizeof(*pp));
1074 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001075 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001076 return;
1077 }
1078
Joe Eykholt925ceda2011-01-28 16:04:23 -08001079 fc_prli_fill(lport, fp);
1080
1081 prov = fc_passive_prov[FC_TYPE_FCP];
1082 if (prov) {
1083 pp = fc_frame_payload_get(fp, sizeof(*pp));
1084 prov->prli(rdata, sizeof(pp->spp), NULL, &pp->spp);
1085 }
1086
1087 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rdata->ids.port_id,
1088 fc_host_port_id(lport->host), FC_TYPE_ELS,
1089 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1090
1091 if (!lport->tt.exch_seq_send(lport, fp, fc_rport_prli_resp,
1092 NULL, rdata, 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001093 fc_rport_error_retry(rdata, NULL);
Robert Love42e9a922008-12-09 15:10:17 -08001094 else
Joe Eykholtf211fa52009-08-25 14:01:01 -07001095 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -08001096}
1097
1098/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001099 * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses
1100 * @sp: The sequence the RTV was on
1101 * @fp: The RTV response frame
1102 * @rdata_arg: The remote port that sent the RTV response
Robert Love42e9a922008-12-09 15:10:17 -08001103 *
1104 * Many targets don't seem to support this.
1105 *
1106 * Locking Note: This function will be called without the rport lock
1107 * held, but it will lock, call an _enter_* function or fc_rport_error
1108 * and then unlock the rport.
1109 */
1110static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001111 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -08001112{
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001113 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -08001114 u8 op;
1115
1116 mutex_lock(&rdata->rp_mutex);
1117
Joe Eykholtf657d292009-08-25 14:03:21 -07001118 FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -08001119
1120 if (rdata->rp_state != RPORT_ST_RTV) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001121 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
1122 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001123 if (IS_ERR(fp))
1124 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001125 goto out;
1126 }
1127
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001128 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001129 fc_rport_error(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001130 goto err;
1131 }
1132
Robert Love42e9a922008-12-09 15:10:17 -08001133 op = fc_frame_payload_op(fp);
1134 if (op == ELS_LS_ACC) {
1135 struct fc_els_rtv_acc *rtv;
1136 u32 toq;
1137 u32 tov;
1138
1139 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
1140 if (rtv) {
1141 toq = ntohl(rtv->rtv_toq);
1142 tov = ntohl(rtv->rtv_r_a_tov);
1143 if (tov == 0)
1144 tov = 1;
1145 rdata->r_a_tov = tov;
1146 tov = ntohl(rtv->rtv_e_d_tov);
1147 if (toq & FC_ELS_RTV_EDRES)
1148 tov /= 1000000;
1149 if (tov == 0)
1150 tov = 1;
1151 rdata->e_d_tov = tov;
1152 }
1153 }
1154
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001155 fc_rport_enter_ready(rdata);
Robert Love42e9a922008-12-09 15:10:17 -08001156
1157out:
1158 fc_frame_free(fp);
1159err:
1160 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -07001161 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -08001162}
1163
1164/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001165 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
1166 * @rdata: The remote port to send the RTV request to
Robert Love42e9a922008-12-09 15:10:17 -08001167 *
1168 * Locking Note: The rport lock is expected to be held before calling
1169 * this routine.
1170 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001171static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -08001172{
1173 struct fc_frame *fp;
Robert Love42e9a922008-12-09 15:10:17 -08001174 struct fc_lport *lport = rdata->local_port;
1175
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001176 FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
1177 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001178
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001179 fc_rport_state_enter(rdata, RPORT_ST_RTV);
Robert Love42e9a922008-12-09 15:10:17 -08001180
1181 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
1182 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001183 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001184 return;
1185 }
1186
Joe Eykholtf211fa52009-08-25 14:01:01 -07001187 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
Joe Eykholtb94f8952009-11-03 11:50:21 -08001188 fc_rport_rtv_resp, rdata,
1189 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001190 fc_rport_error_retry(rdata, NULL);
Robert Love42e9a922008-12-09 15:10:17 -08001191 else
Joe Eykholtf211fa52009-08-25 14:01:01 -07001192 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -08001193}
1194
1195/**
Joe Eykholt079ecd82010-07-20 15:20:51 -07001196 * fc_rport_logo_resp() - Handler for logout (LOGO) responses
1197 * @sp: The sequence the LOGO was on
1198 * @fp: The LOGO response frame
1199 * @lport_arg: The local port
1200 */
1201static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
1202 void *lport_arg)
1203{
1204 struct fc_lport *lport = lport_arg;
1205
1206 FC_RPORT_ID_DBG(lport, fc_seq_exch(sp)->did,
1207 "Received a LOGO %s\n", fc_els_resp_type(fp));
1208 if (IS_ERR(fp))
1209 return;
1210 fc_frame_free(fp);
1211}
1212
1213/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001214 * fc_rport_enter_logo() - Send a logout (LOGO) request
1215 * @rdata: The remote port to send the LOGO request to
Robert Love42e9a922008-12-09 15:10:17 -08001216 *
1217 * Locking Note: The rport lock is expected to be held before calling
1218 * this routine.
1219 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001220static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -08001221{
Robert Love42e9a922008-12-09 15:10:17 -08001222 struct fc_lport *lport = rdata->local_port;
1223 struct fc_frame *fp;
1224
Joe Eykholt079ecd82010-07-20 15:20:51 -07001225 FC_RPORT_DBG(rdata, "Port sending LOGO from %s state\n",
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001226 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001227
Robert Love42e9a922008-12-09 15:10:17 -08001228 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
Joe Eykholt079ecd82010-07-20 15:20:51 -07001229 if (!fp)
Robert Love42e9a922008-12-09 15:10:17 -08001230 return;
Joe Eykholt079ecd82010-07-20 15:20:51 -07001231 (void)lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
1232 fc_rport_logo_resp, lport, 0);
Robert Love42e9a922008-12-09 15:10:17 -08001233}
1234
Robert Love42e9a922008-12-09 15:10:17 -08001235/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001236 * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
1237 * @sp: The sequence the ADISC response was on
1238 * @fp: The ADISC response frame
1239 * @rdata_arg: The remote port that sent the ADISC response
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001240 *
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_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
Robert Love3a3b42b2009-11-03 11:47:39 -08001246 void *rdata_arg)
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001247{
1248 struct fc_rport_priv *rdata = rdata_arg;
1249 struct fc_els_adisc *adisc;
1250 u8 op;
1251
1252 mutex_lock(&rdata->rp_mutex);
1253
1254 FC_RPORT_DBG(rdata, "Received a ADISC response\n");
1255
1256 if (rdata->rp_state != RPORT_ST_ADISC) {
1257 FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
1258 fc_rport_state(rdata));
1259 if (IS_ERR(fp))
1260 goto err;
1261 goto out;
1262 }
1263
1264 if (IS_ERR(fp)) {
1265 fc_rport_error(rdata, fp);
1266 goto err;
1267 }
1268
1269 /*
1270 * If address verification failed. Consider us logged out of the rport.
1271 * Since the rport is still in discovery, we want to be
1272 * logged in, so go to PLOGI state. Otherwise, go back to READY.
1273 */
1274 op = fc_frame_payload_op(fp);
1275 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1276 if (op != ELS_LS_ACC || !adisc ||
1277 ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
1278 get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
1279 get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
1280 FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
Joe Eykholta7b12a22010-07-20 15:20:08 -07001281 fc_rport_enter_flogi(rdata);
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001282 } else {
1283 FC_RPORT_DBG(rdata, "ADISC OK\n");
1284 fc_rport_enter_ready(rdata);
1285 }
1286out:
1287 fc_frame_free(fp);
1288err:
1289 mutex_unlock(&rdata->rp_mutex);
1290 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1291}
1292
1293/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001294 * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
1295 * @rdata: The remote port to send the ADISC request to
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001296 *
1297 * Locking Note: The rport lock is expected to be held before calling
1298 * this routine.
1299 */
1300static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
1301{
1302 struct fc_lport *lport = rdata->local_port;
1303 struct fc_frame *fp;
1304
1305 FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
1306 fc_rport_state(rdata));
1307
1308 fc_rport_state_enter(rdata, RPORT_ST_ADISC);
1309
1310 fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
1311 if (!fp) {
1312 fc_rport_error_retry(rdata, fp);
1313 return;
1314 }
1315 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
Joe Eykholtb94f8952009-11-03 11:50:21 -08001316 fc_rport_adisc_resp, rdata,
1317 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001318 fc_rport_error_retry(rdata, NULL);
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001319 else
1320 kref_get(&rdata->kref);
1321}
1322
1323/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001324 * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
1325 * @rdata: The remote port that sent the ADISC request
Robert Love3a3b42b2009-11-03 11:47:39 -08001326 * @in_fp: The ADISC request frame
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001327 *
1328 * Locking Note: Called with the lport and rport locks held.
1329 */
1330static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
Joe Eykholt922611562010-07-20 15:21:12 -07001331 struct fc_frame *in_fp)
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001332{
1333 struct fc_lport *lport = rdata->local_port;
1334 struct fc_frame *fp;
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001335 struct fc_els_adisc *adisc;
1336 struct fc_seq_els_data rjt_data;
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001337
1338 FC_RPORT_DBG(rdata, "Received ADISC request\n");
1339
1340 adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
1341 if (!adisc) {
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001342 rjt_data.reason = ELS_RJT_PROT;
1343 rjt_data.explan = ELS_EXPL_INV_LEN;
Joe Eykholt922611562010-07-20 15:21:12 -07001344 lport->tt.seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data);
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001345 goto drop;
1346 }
1347
1348 fp = fc_frame_alloc(lport, sizeof(*adisc));
1349 if (!fp)
1350 goto drop;
1351 fc_adisc_fill(lport, fp);
1352 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1353 adisc->adisc_cmd = ELS_LS_ACC;
Joe Eykholt24f089e2010-07-20 15:21:01 -07001354 fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
1355 lport->tt.frame_send(lport, fp);
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001356drop:
1357 fc_frame_free(in_fp);
1358}
1359
1360/**
Yi Zou63e27fb2009-11-20 14:55:24 -08001361 * fc_rport_recv_rls_req() - Handle received Read Link Status request
1362 * @rdata: The remote port that sent the RLS request
Yi Zou63e27fb2009-11-20 14:55:24 -08001363 * @rx_fp: The PRLI request frame
1364 *
1365 * Locking Note: The rport lock is expected to be held before calling
1366 * this function.
1367 */
1368static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
Joe Eykholt922611562010-07-20 15:21:12 -07001369 struct fc_frame *rx_fp)
Yi Zou63e27fb2009-11-20 14:55:24 -08001370
1371{
1372 struct fc_lport *lport = rdata->local_port;
1373 struct fc_frame *fp;
Yi Zou63e27fb2009-11-20 14:55:24 -08001374 struct fc_els_rls *rls;
1375 struct fc_els_rls_resp *rsp;
1376 struct fc_els_lesb *lesb;
1377 struct fc_seq_els_data rjt_data;
1378 struct fc_host_statistics *hst;
Yi Zou63e27fb2009-11-20 14:55:24 -08001379
1380 FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
1381 fc_rport_state(rdata));
1382
1383 rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
1384 if (!rls) {
1385 rjt_data.reason = ELS_RJT_PROT;
1386 rjt_data.explan = ELS_EXPL_INV_LEN;
1387 goto out_rjt;
1388 }
1389
1390 fp = fc_frame_alloc(lport, sizeof(*rsp));
1391 if (!fp) {
1392 rjt_data.reason = ELS_RJT_UNAB;
1393 rjt_data.explan = ELS_EXPL_INSUF_RES;
1394 goto out_rjt;
1395 }
1396
1397 rsp = fc_frame_payload_get(fp, sizeof(*rsp));
1398 memset(rsp, 0, sizeof(*rsp));
1399 rsp->rls_cmd = ELS_LS_ACC;
1400 lesb = &rsp->rls_lesb;
1401 if (lport->tt.get_lesb) {
1402 /* get LESB from LLD if it supports it */
1403 lport->tt.get_lesb(lport, lesb);
1404 } else {
1405 fc_get_host_stats(lport->host);
1406 hst = &lport->host_stats;
1407 lesb->lesb_link_fail = htonl(hst->link_failure_count);
1408 lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
1409 lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
1410 lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
1411 lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
1412 lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
1413 }
1414
Joe Eykholt24f089e2010-07-20 15:21:01 -07001415 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1416 lport->tt.frame_send(lport, fp);
Yi Zou63e27fb2009-11-20 14:55:24 -08001417 goto out;
1418
1419out_rjt:
Joe Eykholt922611562010-07-20 15:21:12 -07001420 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
Yi Zou63e27fb2009-11-20 14:55:24 -08001421out:
1422 fc_frame_free(rx_fp);
1423}
1424
1425/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001426 * fc_rport_recv_els_req() - Handler for validated ELS requests
1427 * @lport: The local port that received the ELS request
Robert Love3a3b42b2009-11-03 11:47:39 -08001428 * @fp: The ELS request frame
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001429 *
1430 * Handle incoming ELS requests that require port login.
1431 * The ELS opcode has already been validated by the caller.
Robert Love42e9a922008-12-09 15:10:17 -08001432 *
Joe Eykholt131203a2009-08-25 14:03:10 -07001433 * Locking Note: Called with the lport lock held.
Robert Love42e9a922008-12-09 15:10:17 -08001434 */
Joe Eykholt922611562010-07-20 15:21:12 -07001435static void fc_rport_recv_els_req(struct fc_lport *lport, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -08001436{
Joe Eykholt131203a2009-08-25 14:03:10 -07001437 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -08001438 struct fc_seq_els_data els_data;
Robert Love42e9a922008-12-09 15:10:17 -08001439
Joe Eykholt25b37b92009-08-25 14:03:15 -07001440 mutex_lock(&lport->disc.disc_mutex);
Joe Eykholt251748a2010-07-20 15:20:56 -07001441 rdata = lport->tt.rport_lookup(lport, fc_frame_sid(fp));
Joe Eykholt131203a2009-08-25 14:03:10 -07001442 if (!rdata) {
Joe Eykholt25b37b92009-08-25 14:03:15 -07001443 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001444 goto reject;
Joe Eykholt131203a2009-08-25 14:03:10 -07001445 }
1446 mutex_lock(&rdata->rp_mutex);
Joe Eykholt25b37b92009-08-25 14:03:15 -07001447 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt131203a2009-08-25 14:03:10 -07001448
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001449 switch (rdata->rp_state) {
1450 case RPORT_ST_PRLI:
1451 case RPORT_ST_RTV:
1452 case RPORT_ST_READY:
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001453 case RPORT_ST_ADISC:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001454 break;
1455 default:
1456 mutex_unlock(&rdata->rp_mutex);
1457 goto reject;
1458 }
1459
1460 switch (fc_frame_payload_op(fp)) {
Joe Eykholt131203a2009-08-25 14:03:10 -07001461 case ELS_PRLI:
Joe Eykholt922611562010-07-20 15:21:12 -07001462 fc_rport_recv_prli_req(rdata, fp);
Joe Eykholt131203a2009-08-25 14:03:10 -07001463 break;
1464 case ELS_PRLO:
Joe Eykholt922611562010-07-20 15:21:12 -07001465 fc_rport_recv_prlo_req(rdata, fp);
Joe Eykholt131203a2009-08-25 14:03:10 -07001466 break;
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001467 case ELS_ADISC:
Joe Eykholt922611562010-07-20 15:21:12 -07001468 fc_rport_recv_adisc_req(rdata, fp);
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001469 break;
Joe Eykholt131203a2009-08-25 14:03:10 -07001470 case ELS_RRQ:
Joe Eykholt922611562010-07-20 15:21:12 -07001471 lport->tt.seq_els_rsp_send(fp, ELS_RRQ, NULL);
1472 fc_frame_free(fp);
Joe Eykholt131203a2009-08-25 14:03:10 -07001473 break;
1474 case ELS_REC:
Joe Eykholt922611562010-07-20 15:21:12 -07001475 lport->tt.seq_els_rsp_send(fp, ELS_REC, NULL);
1476 fc_frame_free(fp);
Joe Eykholt131203a2009-08-25 14:03:10 -07001477 break;
Yi Zou63e27fb2009-11-20 14:55:24 -08001478 case ELS_RLS:
Joe Eykholt922611562010-07-20 15:21:12 -07001479 fc_rport_recv_rls_req(rdata, fp);
Yi Zou63e27fb2009-11-20 14:55:24 -08001480 break;
Joe Eykholt131203a2009-08-25 14:03:10 -07001481 default:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001482 fc_frame_free(fp); /* can't happen */
Joe Eykholt131203a2009-08-25 14:03:10 -07001483 break;
Robert Love42e9a922008-12-09 15:10:17 -08001484 }
1485
1486 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001487 return;
1488
1489reject:
Joe Eykholt922611562010-07-20 15:21:12 -07001490 els_data.reason = ELS_RJT_UNAB;
1491 els_data.explan = ELS_EXPL_PLOGI_REQD;
1492 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001493 fc_frame_free(fp);
1494}
1495
1496/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001497 * fc_rport_recv_req() - Handler for requests
Robert Love3a3b42b2009-11-03 11:47:39 -08001498 * @lport: The local port that received the request
Joe Eykholt922611562010-07-20 15:21:12 -07001499 * @fp: The request frame
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001500 *
1501 * Locking Note: Called with the lport lock held.
1502 */
Joe Eykholt922611562010-07-20 15:21:12 -07001503void fc_rport_recv_req(struct fc_lport *lport, struct fc_frame *fp)
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001504{
1505 struct fc_seq_els_data els_data;
1506
1507 /*
Joe Eykholta7b12a22010-07-20 15:20:08 -07001508 * Handle FLOGI, PLOGI and LOGO requests separately, since they
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001509 * don't require prior login.
1510 * Check for unsupported opcodes first and reject them.
1511 * For some ops, it would be incorrect to reject with "PLOGI required".
1512 */
1513 switch (fc_frame_payload_op(fp)) {
Joe Eykholta7b12a22010-07-20 15:20:08 -07001514 case ELS_FLOGI:
Joe Eykholt922611562010-07-20 15:21:12 -07001515 fc_rport_recv_flogi_req(lport, fp);
Joe Eykholta7b12a22010-07-20 15:20:08 -07001516 break;
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001517 case ELS_PLOGI:
Joe Eykholt922611562010-07-20 15:21:12 -07001518 fc_rport_recv_plogi_req(lport, fp);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001519 break;
1520 case ELS_LOGO:
Joe Eykholt922611562010-07-20 15:21:12 -07001521 fc_rport_recv_logo_req(lport, fp);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001522 break;
1523 case ELS_PRLI:
1524 case ELS_PRLO:
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001525 case ELS_ADISC:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001526 case ELS_RRQ:
1527 case ELS_REC:
Yi Zou63e27fb2009-11-20 14:55:24 -08001528 case ELS_RLS:
Joe Eykholt922611562010-07-20 15:21:12 -07001529 fc_rport_recv_els_req(lport, fp);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001530 break;
1531 default:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001532 els_data.reason = ELS_RJT_UNSUP;
1533 els_data.explan = ELS_EXPL_NONE;
Joe Eykholt922611562010-07-20 15:21:12 -07001534 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1535 fc_frame_free(fp);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001536 break;
1537 }
Robert Love42e9a922008-12-09 15:10:17 -08001538}
1539
1540/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001541 * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
1542 * @lport: The local port that received the PLOGI request
Robert Love3a3b42b2009-11-03 11:47:39 -08001543 * @rx_fp: The PLOGI request frame
Robert Love42e9a922008-12-09 15:10:17 -08001544 *
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001545 * Locking Note: The rport lock is held before calling this function.
Robert Love42e9a922008-12-09 15:10:17 -08001546 */
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001547static void fc_rport_recv_plogi_req(struct fc_lport *lport,
Joe Eykholt922611562010-07-20 15:21:12 -07001548 struct fc_frame *rx_fp)
Robert Love42e9a922008-12-09 15:10:17 -08001549{
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001550 struct fc_disc *disc;
1551 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -08001552 struct fc_frame *fp = rx_fp;
Robert Love42e9a922008-12-09 15:10:17 -08001553 struct fc_els_flogi *pl;
1554 struct fc_seq_els_data rjt_data;
Joe Eykholt24f089e2010-07-20 15:21:01 -07001555 u32 sid;
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001556
Joe Eykholt251748a2010-07-20 15:20:56 -07001557 sid = fc_frame_sid(fp);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001558
1559 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
1560
Robert Love42e9a922008-12-09 15:10:17 -08001561 pl = fc_frame_payload_get(fp, sizeof(*pl));
1562 if (!pl) {
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001563 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
1564 rjt_data.reason = ELS_RJT_PROT;
1565 rjt_data.explan = ELS_EXPL_INV_LEN;
1566 goto reject;
Robert Love42e9a922008-12-09 15:10:17 -08001567 }
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001568
1569 disc = &lport->disc;
1570 mutex_lock(&disc->disc_mutex);
1571 rdata = lport->tt.rport_create(lport, sid);
1572 if (!rdata) {
1573 mutex_unlock(&disc->disc_mutex);
1574 rjt_data.reason = ELS_RJT_UNAB;
1575 rjt_data.explan = ELS_EXPL_INSUF_RES;
1576 goto reject;
1577 }
1578
1579 mutex_lock(&rdata->rp_mutex);
1580 mutex_unlock(&disc->disc_mutex);
1581
1582 rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1583 rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
Robert Love42e9a922008-12-09 15:10:17 -08001584
1585 /*
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001586 * If the rport was just created, possibly due to the incoming PLOGI,
Robert Love42e9a922008-12-09 15:10:17 -08001587 * set the state appropriately and accept the PLOGI.
1588 *
1589 * If we had also sent a PLOGI, and if the received PLOGI is from a
1590 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1591 * "command already in progress".
1592 *
1593 * XXX TBD: If the session was ready before, the PLOGI should result in
1594 * all outstanding exchanges being reset.
1595 */
1596 switch (rdata->rp_state) {
1597 case RPORT_ST_INIT:
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001598 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
Robert Love42e9a922008-12-09 15:10:17 -08001599 break;
Joe Eykholta7b12a22010-07-20 15:20:08 -07001600 case RPORT_ST_PLOGI_WAIT:
1601 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI_WAIT state\n");
1602 break;
Robert Love42e9a922008-12-09 15:10:17 -08001603 case RPORT_ST_PLOGI:
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001604 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1605 if (rdata->ids.port_name < lport->wwpn) {
1606 mutex_unlock(&rdata->rp_mutex);
1607 rjt_data.reason = ELS_RJT_INPROG;
1608 rjt_data.explan = ELS_EXPL_NONE;
1609 goto reject;
1610 }
Robert Love42e9a922008-12-09 15:10:17 -08001611 break;
1612 case RPORT_ST_PRLI:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001613 case RPORT_ST_RTV:
Robert Love42e9a922008-12-09 15:10:17 -08001614 case RPORT_ST_READY:
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001615 case RPORT_ST_ADISC:
1616 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
1617 "- ignored for now\n", rdata->rp_state);
1618 /* XXX TBD - should reset */
Robert Love42e9a922008-12-09 15:10:17 -08001619 break;
Joe Eykholta7b12a22010-07-20 15:20:08 -07001620 case RPORT_ST_FLOGI:
Joe Eykholt14194052009-07-29 17:04:43 -07001621 case RPORT_ST_DELETE:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001622 FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
1623 fc_rport_state(rdata));
1624 mutex_unlock(&rdata->rp_mutex);
1625 rjt_data.reason = ELS_RJT_BUSY;
1626 rjt_data.explan = ELS_EXPL_NONE;
1627 goto reject;
Robert Love42e9a922008-12-09 15:10:17 -08001628 }
1629
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001630 /*
1631 * Get session payload size from incoming PLOGI.
1632 */
1633 rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
Robert Love42e9a922008-12-09 15:10:17 -08001634
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001635 /*
1636 * Send LS_ACC. If this fails, the originator should retry.
1637 */
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001638 fp = fc_frame_alloc(lport, sizeof(*pl));
1639 if (!fp)
1640 goto out;
Robert Love42e9a922008-12-09 15:10:17 -08001641
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001642 fc_plogi_fill(lport, fp, ELS_LS_ACC);
Joe Eykholt24f089e2010-07-20 15:21:01 -07001643 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1644 lport->tt.frame_send(lport, fp);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001645 fc_rport_enter_prli(rdata);
1646out:
1647 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt24f089e2010-07-20 15:21:01 -07001648 fc_frame_free(rx_fp);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001649 return;
1650
1651reject:
Joe Eykholt922611562010-07-20 15:21:12 -07001652 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001653 fc_frame_free(fp);
Robert Love42e9a922008-12-09 15:10:17 -08001654}
1655
1656/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001657 * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
1658 * @rdata: The remote port that sent the PRLI request
Robert Love3a3b42b2009-11-03 11:47:39 -08001659 * @rx_fp: The PRLI request frame
Robert Love42e9a922008-12-09 15:10:17 -08001660 *
1661 * Locking Note: The rport lock is exected to be held before calling
1662 * this function.
1663 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001664static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
Joe Eykholt922611562010-07-20 15:21:12 -07001665 struct fc_frame *rx_fp)
Robert Love42e9a922008-12-09 15:10:17 -08001666{
Robert Love42e9a922008-12-09 15:10:17 -08001667 struct fc_lport *lport = rdata->local_port;
Robert Love42e9a922008-12-09 15:10:17 -08001668 struct fc_frame *fp;
Robert Love42e9a922008-12-09 15:10:17 -08001669 struct {
1670 struct fc_els_prli prli;
1671 struct fc_els_spp spp;
1672 } *pp;
1673 struct fc_els_spp *rspp; /* request service param page */
1674 struct fc_els_spp *spp; /* response spp */
1675 unsigned int len;
1676 unsigned int plen;
Robert Love42e9a922008-12-09 15:10:17 -08001677 enum fc_els_spp_resp resp;
Joe Eykholt96ad8462011-01-28 16:04:02 -08001678 enum fc_els_spp_resp passive;
Robert Love42e9a922008-12-09 15:10:17 -08001679 struct fc_seq_els_data rjt_data;
Joe Eykholt96ad8462011-01-28 16:04:02 -08001680 struct fc4_prov *prov;
Robert Love42e9a922008-12-09 15:10:17 -08001681
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001682 FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1683 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001684
Joe Eykholt251748a2010-07-20 15:20:56 -07001685 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
Robert Love42e9a922008-12-09 15:10:17 -08001686 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
Joe Eykholta2f6a022010-03-12 16:07:36 -08001687 if (!pp)
1688 goto reject_len;
1689 plen = ntohs(pp->prli.prli_len);
1690 if ((plen % 4) != 0 || plen > len || plen < 16)
1691 goto reject_len;
1692 if (plen < len)
1693 len = plen;
1694 plen = pp->prli.prli_spp_len;
1695 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1696 plen > len || len < sizeof(*pp) || plen < 12)
1697 goto reject_len;
1698 rspp = &pp->spp;
1699
1700 fp = fc_frame_alloc(lport, len);
1701 if (!fp) {
1702 rjt_data.reason = ELS_RJT_UNAB;
1703 rjt_data.explan = ELS_EXPL_INSUF_RES;
1704 goto reject;
Robert Love42e9a922008-12-09 15:10:17 -08001705 }
Joe Eykholta2f6a022010-03-12 16:07:36 -08001706 pp = fc_frame_payload_get(fp, len);
1707 WARN_ON(!pp);
1708 memset(pp, 0, len);
1709 pp->prli.prli_cmd = ELS_LS_ACC;
1710 pp->prli.prli_spp_len = plen;
1711 pp->prli.prli_len = htons(len);
1712 len -= sizeof(struct fc_els_prli);
Robert Love42e9a922008-12-09 15:10:17 -08001713
Joe Eykholta2f6a022010-03-12 16:07:36 -08001714 /*
1715 * Go through all the service parameter pages and build
1716 * response. If plen indicates longer SPP than standard,
1717 * use that. The entire response has been pre-cleared above.
1718 */
1719 spp = &pp->spp;
Joe Eykholt96ad8462011-01-28 16:04:02 -08001720 mutex_lock(&fc_prov_mutex);
Joe Eykholta2f6a022010-03-12 16:07:36 -08001721 while (len >= plen) {
1722 spp->spp_type = rspp->spp_type;
1723 spp->spp_type_ext = rspp->spp_type_ext;
Joe Eykholt96ad8462011-01-28 16:04:02 -08001724 resp = 0;
Robert Love42e9a922008-12-09 15:10:17 -08001725
Joe Eykholt96ad8462011-01-28 16:04:02 -08001726 if (rspp->spp_type < FC_FC4_PROV_SIZE) {
1727 prov = fc_active_prov[rspp->spp_type];
1728 if (prov)
1729 resp = prov->prli(rdata, plen, rspp, spp);
1730 prov = fc_passive_prov[rspp->spp_type];
1731 if (prov) {
1732 passive = prov->prli(rdata, plen, rspp, spp);
1733 if (!resp || passive == FC_SPP_RESP_ACK)
1734 resp = passive;
1735 }
1736 }
1737 if (!resp) {
1738 if (spp->spp_flags & FC_SPP_EST_IMG_PAIR)
1739 resp |= FC_SPP_RESP_CONF;
1740 else
1741 resp |= FC_SPP_RESP_INVL;
Robert Love42e9a922008-12-09 15:10:17 -08001742 }
Joe Eykholta2f6a022010-03-12 16:07:36 -08001743 spp->spp_flags |= resp;
1744 len -= plen;
1745 rspp = (struct fc_els_spp *)((char *)rspp + plen);
1746 spp = (struct fc_els_spp *)((char *)spp + plen);
Robert Love42e9a922008-12-09 15:10:17 -08001747 }
Joe Eykholt96ad8462011-01-28 16:04:02 -08001748 mutex_unlock(&fc_prov_mutex);
Joe Eykholta2f6a022010-03-12 16:07:36 -08001749
1750 /*
1751 * Send LS_ACC. If this fails, the originator should retry.
1752 */
Joe Eykholt24f089e2010-07-20 15:21:01 -07001753 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1754 lport->tt.frame_send(lport, fp);
Joe Eykholta2f6a022010-03-12 16:07:36 -08001755
1756 switch (rdata->rp_state) {
1757 case RPORT_ST_PRLI:
1758 fc_rport_enter_ready(rdata);
1759 break;
1760 default:
1761 break;
1762 }
1763 goto drop;
1764
1765reject_len:
1766 rjt_data.reason = ELS_RJT_PROT;
1767 rjt_data.explan = ELS_EXPL_INV_LEN;
1768reject:
Joe Eykholt922611562010-07-20 15:21:12 -07001769 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
Joe Eykholta2f6a022010-03-12 16:07:36 -08001770drop:
Robert Love42e9a922008-12-09 15:10:17 -08001771 fc_frame_free(rx_fp);
1772}
1773
1774/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001775 * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
1776 * @rdata: The remote port that sent the PRLO request
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001777 * @rx_fp: The PRLO request frame
Robert Love42e9a922008-12-09 15:10:17 -08001778 *
1779 * Locking Note: The rport lock is exected to be held before calling
1780 * this function.
1781 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001782static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001783 struct fc_frame *rx_fp)
Robert Love42e9a922008-12-09 15:10:17 -08001784{
Robert Love42e9a922008-12-09 15:10:17 -08001785 struct fc_lport *lport = rdata->local_port;
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001786 struct fc_frame *fp;
1787 struct {
1788 struct fc_els_prlo prlo;
1789 struct fc_els_spp spp;
1790 } *pp;
1791 struct fc_els_spp *rspp; /* request service param page */
1792 struct fc_els_spp *spp; /* response spp */
1793 unsigned int len;
1794 unsigned int plen;
Robert Love42e9a922008-12-09 15:10:17 -08001795 struct fc_seq_els_data rjt_data;
1796
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001797 FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
1798 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001799
Joe Eykholt251748a2010-07-20 15:20:56 -07001800 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001801 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1802 if (!pp)
1803 goto reject_len;
1804 plen = ntohs(pp->prlo.prlo_len);
1805 if (plen != 20)
1806 goto reject_len;
1807 if (plen < len)
1808 len = plen;
1809
1810 rspp = &pp->spp;
1811
1812 fp = fc_frame_alloc(lport, len);
1813 if (!fp) {
1814 rjt_data.reason = ELS_RJT_UNAB;
1815 rjt_data.explan = ELS_EXPL_INSUF_RES;
1816 goto reject;
1817 }
1818
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001819 pp = fc_frame_payload_get(fp, len);
1820 WARN_ON(!pp);
1821 memset(pp, 0, len);
1822 pp->prlo.prlo_cmd = ELS_LS_ACC;
1823 pp->prlo.prlo_obs = 0x10;
1824 pp->prlo.prlo_len = htons(len);
1825 spp = &pp->spp;
1826 spp->spp_type = rspp->spp_type;
1827 spp->spp_type_ext = rspp->spp_type_ext;
1828 spp->spp_flags = FC_SPP_RESP_ACK;
1829
1830 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
1831
Joe Eykholt922611562010-07-20 15:21:12 -07001832 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1833 lport->tt.frame_send(lport, fp);
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001834 goto drop;
1835
1836reject_len:
1837 rjt_data.reason = ELS_RJT_PROT;
1838 rjt_data.explan = ELS_EXPL_INV_LEN;
1839reject:
Joe Eykholt922611562010-07-20 15:21:12 -07001840 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001841drop:
1842 fc_frame_free(rx_fp);
Robert Love42e9a922008-12-09 15:10:17 -08001843}
1844
1845/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001846 * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
1847 * @lport: The local port that received the LOGO request
Robert Love3a3b42b2009-11-03 11:47:39 -08001848 * @fp: The LOGO request frame
Robert Love42e9a922008-12-09 15:10:17 -08001849 *
1850 * Locking Note: The rport lock is exected to be held before calling
1851 * this function.
1852 */
Joe Eykholt922611562010-07-20 15:21:12 -07001853static void fc_rport_recv_logo_req(struct fc_lport *lport, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -08001854{
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001855 struct fc_rport_priv *rdata;
1856 u32 sid;
Robert Love42e9a922008-12-09 15:10:17 -08001857
Joe Eykholt922611562010-07-20 15:21:12 -07001858 lport->tt.seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
Joe Eykholtfeab4ae2009-08-25 14:03:36 -07001859
Joe Eykholt251748a2010-07-20 15:20:56 -07001860 sid = fc_frame_sid(fp);
Robert Love42e9a922008-12-09 15:10:17 -08001861
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001862 mutex_lock(&lport->disc.disc_mutex);
1863 rdata = lport->tt.rport_lookup(lport, sid);
1864 if (rdata) {
1865 mutex_lock(&rdata->rp_mutex);
1866 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
1867 fc_rport_state(rdata));
Joe Eykholtfeab4ae2009-08-25 14:03:36 -07001868
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001869 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001870 mutex_unlock(&rdata->rp_mutex);
1871 } else
1872 FC_RPORT_ID_DBG(lport, sid,
1873 "Received LOGO from non-logged-in port\n");
1874 mutex_unlock(&lport->disc.disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -08001875 fc_frame_free(fp);
1876}
1877
Robert Love3a3b42b2009-11-03 11:47:39 -08001878/**
1879 * fc_rport_flush_queue() - Flush the rport_event_queue
1880 */
Robert Love42e9a922008-12-09 15:10:17 -08001881static void fc_rport_flush_queue(void)
1882{
1883 flush_workqueue(rport_event_queue);
1884}
1885
Robert Love3a3b42b2009-11-03 11:47:39 -08001886/**
1887 * fc_rport_init() - Initialize the remote port layer for a local port
1888 * @lport: The local port to initialize the remote port layer for
1889 */
Robert Love42e9a922008-12-09 15:10:17 -08001890int fc_rport_init(struct fc_lport *lport)
1891{
Joe Eykholt8025b5d2009-08-25 14:02:06 -07001892 if (!lport->tt.rport_lookup)
1893 lport->tt.rport_lookup = fc_rport_lookup;
1894
Robert Love5101ff92009-02-27 10:55:18 -08001895 if (!lport->tt.rport_create)
Joe Eykholt9e9d0452009-08-25 14:01:18 -07001896 lport->tt.rport_create = fc_rport_create;
Robert Love5101ff92009-02-27 10:55:18 -08001897
Robert Love42e9a922008-12-09 15:10:17 -08001898 if (!lport->tt.rport_login)
1899 lport->tt.rport_login = fc_rport_login;
1900
1901 if (!lport->tt.rport_logoff)
1902 lport->tt.rport_logoff = fc_rport_logoff;
1903
1904 if (!lport->tt.rport_recv_req)
1905 lport->tt.rport_recv_req = fc_rport_recv_req;
1906
1907 if (!lport->tt.rport_flush_queue)
1908 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1909
Joe Eykholtf211fa52009-08-25 14:01:01 -07001910 if (!lport->tt.rport_destroy)
1911 lport->tt.rport_destroy = fc_rport_destroy;
1912
Robert Love42e9a922008-12-09 15:10:17 -08001913 return 0;
1914}
1915EXPORT_SYMBOL(fc_rport_init);
1916
Robert Love3a3b42b2009-11-03 11:47:39 -08001917/**
Joe Eykholt96ad8462011-01-28 16:04:02 -08001918 * fc_rport_fcp_prli() - Handle incoming PRLI for the FCP initiator.
1919 * @rdata: remote port private
1920 * @spp_len: service parameter page length
1921 * @rspp: received service parameter page
1922 * @spp: response service parameter page
1923 *
1924 * Returns the value for the response code to be placed in spp_flags;
1925 * Returns 0 if not an initiator.
1926 */
1927static int fc_rport_fcp_prli(struct fc_rport_priv *rdata, u32 spp_len,
1928 const struct fc_els_spp *rspp,
1929 struct fc_els_spp *spp)
1930{
1931 struct fc_lport *lport = rdata->local_port;
1932 u32 fcp_parm;
1933
1934 fcp_parm = ntohl(rspp->spp_params);
1935 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
1936 if (fcp_parm & FCP_SPPF_INIT_FCN)
1937 rdata->ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1938 if (fcp_parm & FCP_SPPF_TARG_FCN)
1939 rdata->ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1940 if (fcp_parm & FCP_SPPF_RETRY)
1941 rdata->flags |= FC_RP_FLAGS_RETRY;
1942 rdata->supported_classes = FC_COS_CLASS3;
1943
1944 if (!(lport->service_params & FC_RPORT_ROLE_FCP_INITIATOR))
1945 return 0;
1946
1947 spp->spp_flags |= rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
1948
1949 /*
1950 * OR in our service parameters with other providers (target), if any.
1951 */
1952 fcp_parm = ntohl(spp->spp_params);
1953 spp->spp_params = htonl(fcp_parm | lport->service_params);
1954 return FC_SPP_RESP_ACK;
1955}
1956
1957/*
1958 * FC-4 provider ops for FCP initiator.
1959 */
1960struct fc4_prov fc_rport_fcp_init = {
1961 .prli = fc_rport_fcp_prli,
1962};
1963
1964/**
1965 * fc_rport_t0_prli() - Handle incoming PRLI parameters for type 0
1966 * @rdata: remote port private
1967 * @spp_len: service parameter page length
1968 * @rspp: received service parameter page
1969 * @spp: response service parameter page
1970 */
1971static int fc_rport_t0_prli(struct fc_rport_priv *rdata, u32 spp_len,
1972 const struct fc_els_spp *rspp,
1973 struct fc_els_spp *spp)
1974{
1975 if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR)
1976 return FC_SPP_RESP_INVL;
1977 return FC_SPP_RESP_ACK;
1978}
1979
1980/*
1981 * FC-4 provider ops for type 0 service parameters.
1982 *
1983 * This handles the special case of type 0 which is always successful
1984 * but doesn't do anything otherwise.
1985 */
1986struct fc4_prov fc_rport_t0_prov = {
1987 .prli = fc_rport_t0_prli,
1988};
1989
1990/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001991 * fc_setup_rport() - Initialize the rport_event_queue
1992 */
Randy Dunlap55204902011-01-28 16:03:57 -08001993int fc_setup_rport(void)
Robert Love42e9a922008-12-09 15:10:17 -08001994{
1995 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
1996 if (!rport_event_queue)
1997 return -ENOMEM;
1998 return 0;
1999}
Robert Love42e9a922008-12-09 15:10:17 -08002000
Robert Love3a3b42b2009-11-03 11:47:39 -08002001/**
2002 * fc_destroy_rport() - Destroy the rport_event_queue
2003 */
Randy Dunlap55204902011-01-28 16:03:57 -08002004void fc_destroy_rport(void)
Robert Love42e9a922008-12-09 15:10:17 -08002005{
2006 destroy_workqueue(rport_event_queue);
2007}
Robert Love42e9a922008-12-09 15:10:17 -08002008
Robert Love3a3b42b2009-11-03 11:47:39 -08002009/**
2010 * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
2011 * @rport: The remote port whose I/O should be terminated
2012 */
Robert Love42e9a922008-12-09 15:10:17 -08002013void fc_rport_terminate_io(struct fc_rport *rport)
2014{
Robert Love3a3b42b2009-11-03 11:47:39 -08002015 struct fc_rport_libfc_priv *rpriv = rport->dd_data;
2016 struct fc_lport *lport = rpriv->local_port;
Robert Love42e9a922008-12-09 15:10:17 -08002017
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -08002018 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
2019 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
Robert Love42e9a922008-12-09 15:10:17 -08002020}
2021EXPORT_SYMBOL(fc_rport_terminate_io);