blob: a92954c1f42fdba97caeca53e2d5991b19adb94d [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 Eykholt92261152010-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 Eykholt92261152010-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 Eykholt92261152010-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;
964 u32 roles = FC_RPORT_ROLE_UNKNOWN;
965 u32 fcp_parm = 0;
966 u8 op;
Bhanu Prakash Gollapudi618461c2010-06-11 16:43:54 -0700967 u8 resp_code = 0;
Robert Love42e9a922008-12-09 15:10:17 -0800968
969 mutex_lock(&rdata->rp_mutex);
970
Joe Eykholtf657d292009-08-25 14:03:21 -0700971 FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800972
973 if (rdata->rp_state != RPORT_ST_PRLI) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700974 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
975 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700976 if (IS_ERR(fp))
977 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800978 goto out;
979 }
980
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700981 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700982 fc_rport_error_retry(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700983 goto err;
984 }
985
Robert Love6bd054c2009-08-25 14:03:04 -0700986 /* reinitialize remote port roles */
987 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
988
Robert Love42e9a922008-12-09 15:10:17 -0800989 op = fc_frame_payload_op(fp);
990 if (op == ELS_LS_ACC) {
991 pp = fc_frame_payload_get(fp, sizeof(*pp));
Bhanu Prakash Gollapudi618461c2010-06-11 16:43:54 -0700992 if (!pp)
993 goto out;
994
995 resp_code = (pp->spp.spp_flags & FC_SPP_RESP_MASK);
996 FC_RPORT_DBG(rdata, "PRLI spp_flags = 0x%x\n",
997 pp->spp.spp_flags);
998 if (resp_code != FC_SPP_RESP_ACK) {
999 if (resp_code == FC_SPP_RESP_CONF)
1000 fc_rport_error(rdata, fp);
1001 else
1002 fc_rport_error_retry(rdata, fp);
1003 goto out;
Robert Love42e9a922008-12-09 15:10:17 -08001004 }
Bhanu Prakash Gollapudi618461c2010-06-11 16:43:54 -07001005 if (pp->prli.prli_spp_len < sizeof(pp->spp))
1006 goto out;
1007
1008 fcp_parm = ntohl(pp->spp.spp_params);
1009 if (fcp_parm & FCP_SPPF_RETRY)
1010 rdata->flags |= FC_RP_FLAGS_RETRY;
Robert Love42e9a922008-12-09 15:10:17 -08001011
Joe Eykholtf211fa52009-08-25 14:01:01 -07001012 rdata->supported_classes = FC_COS_CLASS3;
Robert Love42e9a922008-12-09 15:10:17 -08001013 if (fcp_parm & FCP_SPPF_INIT_FCN)
1014 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1015 if (fcp_parm & FCP_SPPF_TARG_FCN)
1016 roles |= FC_RPORT_ROLE_FCP_TARGET;
1017
Joe Eykholtf211fa52009-08-25 14:01:01 -07001018 rdata->ids.roles = roles;
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001019 fc_rport_enter_rtv(rdata);
Robert Love42e9a922008-12-09 15:10:17 -08001020
1021 } else {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001022 FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
Bhanu Prakash Gollapudi292e40b2010-06-11 16:43:49 -07001023 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001024 }
1025
1026out:
1027 fc_frame_free(fp);
1028err:
1029 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -07001030 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -08001031}
1032
1033/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001034 * fc_rport_enter_prli() - Send Process Login (PRLI) request
1035 * @rdata: The remote port to send the PRLI request to
Robert Love42e9a922008-12-09 15:10:17 -08001036 *
1037 * Locking Note: The rport lock is expected to be held before calling
1038 * this routine.
1039 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001040static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -08001041{
Robert Love42e9a922008-12-09 15:10:17 -08001042 struct fc_lport *lport = rdata->local_port;
1043 struct {
1044 struct fc_els_prli prli;
1045 struct fc_els_spp spp;
1046 } *pp;
1047 struct fc_frame *fp;
1048
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001049 /*
1050 * If the rport is one of the well known addresses
1051 * we skip PRLI and RTV and go straight to READY.
1052 */
1053 if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
1054 fc_rport_enter_ready(rdata);
1055 return;
1056 }
1057
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001058 FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
1059 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001060
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001061 fc_rport_state_enter(rdata, RPORT_ST_PRLI);
Robert Love42e9a922008-12-09 15:10:17 -08001062
1063 fp = fc_frame_alloc(lport, sizeof(*pp));
1064 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001065 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001066 return;
1067 }
1068
Joe Eykholtf211fa52009-08-25 14:01:01 -07001069 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PRLI,
Joe Eykholtb94f8952009-11-03 11:50:21 -08001070 fc_rport_prli_resp, rdata,
1071 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001072 fc_rport_error_retry(rdata, NULL);
Robert Love42e9a922008-12-09 15:10:17 -08001073 else
Joe Eykholtf211fa52009-08-25 14:01:01 -07001074 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -08001075}
1076
1077/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001078 * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses
1079 * @sp: The sequence the RTV was on
1080 * @fp: The RTV response frame
1081 * @rdata_arg: The remote port that sent the RTV response
Robert Love42e9a922008-12-09 15:10:17 -08001082 *
1083 * Many targets don't seem to support this.
1084 *
1085 * Locking Note: This function will be called without the rport lock
1086 * held, but it will lock, call an _enter_* function or fc_rport_error
1087 * and then unlock the rport.
1088 */
1089static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001090 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -08001091{
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001092 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -08001093 u8 op;
1094
1095 mutex_lock(&rdata->rp_mutex);
1096
Joe Eykholtf657d292009-08-25 14:03:21 -07001097 FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -08001098
1099 if (rdata->rp_state != RPORT_ST_RTV) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001100 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
1101 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001102 if (IS_ERR(fp))
1103 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001104 goto out;
1105 }
1106
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001107 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001108 fc_rport_error(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001109 goto err;
1110 }
1111
Robert Love42e9a922008-12-09 15:10:17 -08001112 op = fc_frame_payload_op(fp);
1113 if (op == ELS_LS_ACC) {
1114 struct fc_els_rtv_acc *rtv;
1115 u32 toq;
1116 u32 tov;
1117
1118 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
1119 if (rtv) {
1120 toq = ntohl(rtv->rtv_toq);
1121 tov = ntohl(rtv->rtv_r_a_tov);
1122 if (tov == 0)
1123 tov = 1;
1124 rdata->r_a_tov = tov;
1125 tov = ntohl(rtv->rtv_e_d_tov);
1126 if (toq & FC_ELS_RTV_EDRES)
1127 tov /= 1000000;
1128 if (tov == 0)
1129 tov = 1;
1130 rdata->e_d_tov = tov;
1131 }
1132 }
1133
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001134 fc_rport_enter_ready(rdata);
Robert Love42e9a922008-12-09 15:10:17 -08001135
1136out:
1137 fc_frame_free(fp);
1138err:
1139 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -07001140 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -08001141}
1142
1143/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001144 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
1145 * @rdata: The remote port to send the RTV request to
Robert Love42e9a922008-12-09 15:10:17 -08001146 *
1147 * Locking Note: The rport lock is expected to be held before calling
1148 * this routine.
1149 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001150static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -08001151{
1152 struct fc_frame *fp;
Robert Love42e9a922008-12-09 15:10:17 -08001153 struct fc_lport *lport = rdata->local_port;
1154
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001155 FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
1156 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001157
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001158 fc_rport_state_enter(rdata, RPORT_ST_RTV);
Robert Love42e9a922008-12-09 15:10:17 -08001159
1160 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
1161 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001162 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001163 return;
1164 }
1165
Joe Eykholtf211fa52009-08-25 14:01:01 -07001166 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
Joe Eykholtb94f8952009-11-03 11:50:21 -08001167 fc_rport_rtv_resp, rdata,
1168 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001169 fc_rport_error_retry(rdata, NULL);
Robert Love42e9a922008-12-09 15:10:17 -08001170 else
Joe Eykholtf211fa52009-08-25 14:01:01 -07001171 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -08001172}
1173
1174/**
Joe Eykholt079ecd82010-07-20 15:20:51 -07001175 * fc_rport_logo_resp() - Handler for logout (LOGO) responses
1176 * @sp: The sequence the LOGO was on
1177 * @fp: The LOGO response frame
1178 * @lport_arg: The local port
1179 */
1180static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
1181 void *lport_arg)
1182{
1183 struct fc_lport *lport = lport_arg;
1184
1185 FC_RPORT_ID_DBG(lport, fc_seq_exch(sp)->did,
1186 "Received a LOGO %s\n", fc_els_resp_type(fp));
1187 if (IS_ERR(fp))
1188 return;
1189 fc_frame_free(fp);
1190}
1191
1192/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001193 * fc_rport_enter_logo() - Send a logout (LOGO) request
1194 * @rdata: The remote port to send the LOGO request to
Robert Love42e9a922008-12-09 15:10:17 -08001195 *
1196 * Locking Note: The rport lock is expected to be held before calling
1197 * this routine.
1198 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001199static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -08001200{
Robert Love42e9a922008-12-09 15:10:17 -08001201 struct fc_lport *lport = rdata->local_port;
1202 struct fc_frame *fp;
1203
Joe Eykholt079ecd82010-07-20 15:20:51 -07001204 FC_RPORT_DBG(rdata, "Port sending LOGO from %s state\n",
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001205 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001206
Robert Love42e9a922008-12-09 15:10:17 -08001207 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
Joe Eykholt079ecd82010-07-20 15:20:51 -07001208 if (!fp)
Robert Love42e9a922008-12-09 15:10:17 -08001209 return;
Joe Eykholt079ecd82010-07-20 15:20:51 -07001210 (void)lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
1211 fc_rport_logo_resp, lport, 0);
Robert Love42e9a922008-12-09 15:10:17 -08001212}
1213
Robert Love42e9a922008-12-09 15:10:17 -08001214/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001215 * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
1216 * @sp: The sequence the ADISC response was on
1217 * @fp: The ADISC response frame
1218 * @rdata_arg: The remote port that sent the ADISC response
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001219 *
1220 * Locking Note: This function will be called without the rport lock
1221 * held, but it will lock, call an _enter_* function or fc_rport_error
1222 * and then unlock the rport.
1223 */
1224static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
Robert Love3a3b42b2009-11-03 11:47:39 -08001225 void *rdata_arg)
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001226{
1227 struct fc_rport_priv *rdata = rdata_arg;
1228 struct fc_els_adisc *adisc;
1229 u8 op;
1230
1231 mutex_lock(&rdata->rp_mutex);
1232
1233 FC_RPORT_DBG(rdata, "Received a ADISC response\n");
1234
1235 if (rdata->rp_state != RPORT_ST_ADISC) {
1236 FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
1237 fc_rport_state(rdata));
1238 if (IS_ERR(fp))
1239 goto err;
1240 goto out;
1241 }
1242
1243 if (IS_ERR(fp)) {
1244 fc_rport_error(rdata, fp);
1245 goto err;
1246 }
1247
1248 /*
1249 * If address verification failed. Consider us logged out of the rport.
1250 * Since the rport is still in discovery, we want to be
1251 * logged in, so go to PLOGI state. Otherwise, go back to READY.
1252 */
1253 op = fc_frame_payload_op(fp);
1254 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1255 if (op != ELS_LS_ACC || !adisc ||
1256 ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
1257 get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
1258 get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
1259 FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
Joe Eykholta7b12a22010-07-20 15:20:08 -07001260 fc_rport_enter_flogi(rdata);
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001261 } else {
1262 FC_RPORT_DBG(rdata, "ADISC OK\n");
1263 fc_rport_enter_ready(rdata);
1264 }
1265out:
1266 fc_frame_free(fp);
1267err:
1268 mutex_unlock(&rdata->rp_mutex);
1269 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1270}
1271
1272/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001273 * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
1274 * @rdata: The remote port to send the ADISC request to
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001275 *
1276 * Locking Note: The rport lock is expected to be held before calling
1277 * this routine.
1278 */
1279static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
1280{
1281 struct fc_lport *lport = rdata->local_port;
1282 struct fc_frame *fp;
1283
1284 FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
1285 fc_rport_state(rdata));
1286
1287 fc_rport_state_enter(rdata, RPORT_ST_ADISC);
1288
1289 fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
1290 if (!fp) {
1291 fc_rport_error_retry(rdata, fp);
1292 return;
1293 }
1294 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
Joe Eykholtb94f8952009-11-03 11:50:21 -08001295 fc_rport_adisc_resp, rdata,
1296 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001297 fc_rport_error_retry(rdata, NULL);
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001298 else
1299 kref_get(&rdata->kref);
1300}
1301
1302/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001303 * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
1304 * @rdata: The remote port that sent the ADISC request
Robert Love3a3b42b2009-11-03 11:47:39 -08001305 * @in_fp: The ADISC request frame
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001306 *
1307 * Locking Note: Called with the lport and rport locks held.
1308 */
1309static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
Joe Eykholt92261152010-07-20 15:21:12 -07001310 struct fc_frame *in_fp)
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001311{
1312 struct fc_lport *lport = rdata->local_port;
1313 struct fc_frame *fp;
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001314 struct fc_els_adisc *adisc;
1315 struct fc_seq_els_data rjt_data;
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001316
1317 FC_RPORT_DBG(rdata, "Received ADISC request\n");
1318
1319 adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
1320 if (!adisc) {
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001321 rjt_data.reason = ELS_RJT_PROT;
1322 rjt_data.explan = ELS_EXPL_INV_LEN;
Joe Eykholt92261152010-07-20 15:21:12 -07001323 lport->tt.seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data);
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001324 goto drop;
1325 }
1326
1327 fp = fc_frame_alloc(lport, sizeof(*adisc));
1328 if (!fp)
1329 goto drop;
1330 fc_adisc_fill(lport, fp);
1331 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1332 adisc->adisc_cmd = ELS_LS_ACC;
Joe Eykholt24f089e2010-07-20 15:21:01 -07001333 fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
1334 lport->tt.frame_send(lport, fp);
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001335drop:
1336 fc_frame_free(in_fp);
1337}
1338
1339/**
Yi Zou63e27fb2009-11-20 14:55:24 -08001340 * fc_rport_recv_rls_req() - Handle received Read Link Status request
1341 * @rdata: The remote port that sent the RLS request
Yi Zou63e27fb2009-11-20 14:55:24 -08001342 * @rx_fp: The PRLI request frame
1343 *
1344 * Locking Note: The rport lock is expected to be held before calling
1345 * this function.
1346 */
1347static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
Joe Eykholt92261152010-07-20 15:21:12 -07001348 struct fc_frame *rx_fp)
Yi Zou63e27fb2009-11-20 14:55:24 -08001349
1350{
1351 struct fc_lport *lport = rdata->local_port;
1352 struct fc_frame *fp;
Yi Zou63e27fb2009-11-20 14:55:24 -08001353 struct fc_els_rls *rls;
1354 struct fc_els_rls_resp *rsp;
1355 struct fc_els_lesb *lesb;
1356 struct fc_seq_els_data rjt_data;
1357 struct fc_host_statistics *hst;
Yi Zou63e27fb2009-11-20 14:55:24 -08001358
1359 FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
1360 fc_rport_state(rdata));
1361
1362 rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
1363 if (!rls) {
1364 rjt_data.reason = ELS_RJT_PROT;
1365 rjt_data.explan = ELS_EXPL_INV_LEN;
1366 goto out_rjt;
1367 }
1368
1369 fp = fc_frame_alloc(lport, sizeof(*rsp));
1370 if (!fp) {
1371 rjt_data.reason = ELS_RJT_UNAB;
1372 rjt_data.explan = ELS_EXPL_INSUF_RES;
1373 goto out_rjt;
1374 }
1375
1376 rsp = fc_frame_payload_get(fp, sizeof(*rsp));
1377 memset(rsp, 0, sizeof(*rsp));
1378 rsp->rls_cmd = ELS_LS_ACC;
1379 lesb = &rsp->rls_lesb;
1380 if (lport->tt.get_lesb) {
1381 /* get LESB from LLD if it supports it */
1382 lport->tt.get_lesb(lport, lesb);
1383 } else {
1384 fc_get_host_stats(lport->host);
1385 hst = &lport->host_stats;
1386 lesb->lesb_link_fail = htonl(hst->link_failure_count);
1387 lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
1388 lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
1389 lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
1390 lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
1391 lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
1392 }
1393
Joe Eykholt24f089e2010-07-20 15:21:01 -07001394 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1395 lport->tt.frame_send(lport, fp);
Yi Zou63e27fb2009-11-20 14:55:24 -08001396 goto out;
1397
1398out_rjt:
Joe Eykholt92261152010-07-20 15:21:12 -07001399 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
Yi Zou63e27fb2009-11-20 14:55:24 -08001400out:
1401 fc_frame_free(rx_fp);
1402}
1403
1404/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001405 * fc_rport_recv_els_req() - Handler for validated ELS requests
1406 * @lport: The local port that received the ELS request
Robert Love3a3b42b2009-11-03 11:47:39 -08001407 * @fp: The ELS request frame
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001408 *
1409 * Handle incoming ELS requests that require port login.
1410 * The ELS opcode has already been validated by the caller.
Robert Love42e9a922008-12-09 15:10:17 -08001411 *
Joe Eykholt131203a2009-08-25 14:03:10 -07001412 * Locking Note: Called with the lport lock held.
Robert Love42e9a922008-12-09 15:10:17 -08001413 */
Joe Eykholt92261152010-07-20 15:21:12 -07001414static void fc_rport_recv_els_req(struct fc_lport *lport, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -08001415{
Joe Eykholt131203a2009-08-25 14:03:10 -07001416 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -08001417 struct fc_seq_els_data els_data;
Robert Love42e9a922008-12-09 15:10:17 -08001418
Joe Eykholt25b37b92009-08-25 14:03:15 -07001419 mutex_lock(&lport->disc.disc_mutex);
Joe Eykholt251748a2010-07-20 15:20:56 -07001420 rdata = lport->tt.rport_lookup(lport, fc_frame_sid(fp));
Joe Eykholt131203a2009-08-25 14:03:10 -07001421 if (!rdata) {
Joe Eykholt25b37b92009-08-25 14:03:15 -07001422 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001423 goto reject;
Joe Eykholt131203a2009-08-25 14:03:10 -07001424 }
1425 mutex_lock(&rdata->rp_mutex);
Joe Eykholt25b37b92009-08-25 14:03:15 -07001426 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt131203a2009-08-25 14:03:10 -07001427
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001428 switch (rdata->rp_state) {
1429 case RPORT_ST_PRLI:
1430 case RPORT_ST_RTV:
1431 case RPORT_ST_READY:
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001432 case RPORT_ST_ADISC:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001433 break;
1434 default:
1435 mutex_unlock(&rdata->rp_mutex);
1436 goto reject;
1437 }
1438
1439 switch (fc_frame_payload_op(fp)) {
Joe Eykholt131203a2009-08-25 14:03:10 -07001440 case ELS_PRLI:
Joe Eykholt92261152010-07-20 15:21:12 -07001441 fc_rport_recv_prli_req(rdata, fp);
Joe Eykholt131203a2009-08-25 14:03:10 -07001442 break;
1443 case ELS_PRLO:
Joe Eykholt92261152010-07-20 15:21:12 -07001444 fc_rport_recv_prlo_req(rdata, fp);
Joe Eykholt131203a2009-08-25 14:03:10 -07001445 break;
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001446 case ELS_ADISC:
Joe Eykholt92261152010-07-20 15:21:12 -07001447 fc_rport_recv_adisc_req(rdata, fp);
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001448 break;
Joe Eykholt131203a2009-08-25 14:03:10 -07001449 case ELS_RRQ:
Joe Eykholt92261152010-07-20 15:21:12 -07001450 lport->tt.seq_els_rsp_send(fp, ELS_RRQ, NULL);
1451 fc_frame_free(fp);
Joe Eykholt131203a2009-08-25 14:03:10 -07001452 break;
1453 case ELS_REC:
Joe Eykholt92261152010-07-20 15:21:12 -07001454 lport->tt.seq_els_rsp_send(fp, ELS_REC, NULL);
1455 fc_frame_free(fp);
Joe Eykholt131203a2009-08-25 14:03:10 -07001456 break;
Yi Zou63e27fb2009-11-20 14:55:24 -08001457 case ELS_RLS:
Joe Eykholt92261152010-07-20 15:21:12 -07001458 fc_rport_recv_rls_req(rdata, fp);
Yi Zou63e27fb2009-11-20 14:55:24 -08001459 break;
Joe Eykholt131203a2009-08-25 14:03:10 -07001460 default:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001461 fc_frame_free(fp); /* can't happen */
Joe Eykholt131203a2009-08-25 14:03:10 -07001462 break;
Robert Love42e9a922008-12-09 15:10:17 -08001463 }
1464
1465 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001466 return;
1467
1468reject:
Joe Eykholt92261152010-07-20 15:21:12 -07001469 els_data.reason = ELS_RJT_UNAB;
1470 els_data.explan = ELS_EXPL_PLOGI_REQD;
1471 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001472 fc_frame_free(fp);
1473}
1474
1475/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001476 * fc_rport_recv_req() - Handler for requests
Robert Love3a3b42b2009-11-03 11:47:39 -08001477 * @lport: The local port that received the request
Joe Eykholt92261152010-07-20 15:21:12 -07001478 * @fp: The request frame
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001479 *
1480 * Locking Note: Called with the lport lock held.
1481 */
Joe Eykholt92261152010-07-20 15:21:12 -07001482void fc_rport_recv_req(struct fc_lport *lport, struct fc_frame *fp)
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001483{
1484 struct fc_seq_els_data els_data;
1485
1486 /*
Joe Eykholta7b12a22010-07-20 15:20:08 -07001487 * Handle FLOGI, PLOGI and LOGO requests separately, since they
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001488 * don't require prior login.
1489 * Check for unsupported opcodes first and reject them.
1490 * For some ops, it would be incorrect to reject with "PLOGI required".
1491 */
1492 switch (fc_frame_payload_op(fp)) {
Joe Eykholta7b12a22010-07-20 15:20:08 -07001493 case ELS_FLOGI:
Joe Eykholt92261152010-07-20 15:21:12 -07001494 fc_rport_recv_flogi_req(lport, fp);
Joe Eykholta7b12a22010-07-20 15:20:08 -07001495 break;
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001496 case ELS_PLOGI:
Joe Eykholt92261152010-07-20 15:21:12 -07001497 fc_rport_recv_plogi_req(lport, fp);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001498 break;
1499 case ELS_LOGO:
Joe Eykholt92261152010-07-20 15:21:12 -07001500 fc_rport_recv_logo_req(lport, fp);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001501 break;
1502 case ELS_PRLI:
1503 case ELS_PRLO:
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001504 case ELS_ADISC:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001505 case ELS_RRQ:
1506 case ELS_REC:
Yi Zou63e27fb2009-11-20 14:55:24 -08001507 case ELS_RLS:
Joe Eykholt92261152010-07-20 15:21:12 -07001508 fc_rport_recv_els_req(lport, fp);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001509 break;
1510 default:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001511 els_data.reason = ELS_RJT_UNSUP;
1512 els_data.explan = ELS_EXPL_NONE;
Joe Eykholt92261152010-07-20 15:21:12 -07001513 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1514 fc_frame_free(fp);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001515 break;
1516 }
Robert Love42e9a922008-12-09 15:10:17 -08001517}
1518
1519/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001520 * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
1521 * @lport: The local port that received the PLOGI request
Robert Love3a3b42b2009-11-03 11:47:39 -08001522 * @rx_fp: The PLOGI request frame
Robert Love42e9a922008-12-09 15:10:17 -08001523 *
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001524 * Locking Note: The rport lock is held before calling this function.
Robert Love42e9a922008-12-09 15:10:17 -08001525 */
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001526static void fc_rport_recv_plogi_req(struct fc_lport *lport,
Joe Eykholt92261152010-07-20 15:21:12 -07001527 struct fc_frame *rx_fp)
Robert Love42e9a922008-12-09 15:10:17 -08001528{
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001529 struct fc_disc *disc;
1530 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -08001531 struct fc_frame *fp = rx_fp;
Robert Love42e9a922008-12-09 15:10:17 -08001532 struct fc_els_flogi *pl;
1533 struct fc_seq_els_data rjt_data;
Joe Eykholt24f089e2010-07-20 15:21:01 -07001534 u32 sid;
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001535
Joe Eykholt251748a2010-07-20 15:20:56 -07001536 sid = fc_frame_sid(fp);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001537
1538 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
1539
Robert Love42e9a922008-12-09 15:10:17 -08001540 pl = fc_frame_payload_get(fp, sizeof(*pl));
1541 if (!pl) {
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001542 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
1543 rjt_data.reason = ELS_RJT_PROT;
1544 rjt_data.explan = ELS_EXPL_INV_LEN;
1545 goto reject;
Robert Love42e9a922008-12-09 15:10:17 -08001546 }
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001547
1548 disc = &lport->disc;
1549 mutex_lock(&disc->disc_mutex);
1550 rdata = lport->tt.rport_create(lport, sid);
1551 if (!rdata) {
1552 mutex_unlock(&disc->disc_mutex);
1553 rjt_data.reason = ELS_RJT_UNAB;
1554 rjt_data.explan = ELS_EXPL_INSUF_RES;
1555 goto reject;
1556 }
1557
1558 mutex_lock(&rdata->rp_mutex);
1559 mutex_unlock(&disc->disc_mutex);
1560
1561 rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1562 rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
Robert Love42e9a922008-12-09 15:10:17 -08001563
1564 /*
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001565 * If the rport was just created, possibly due to the incoming PLOGI,
Robert Love42e9a922008-12-09 15:10:17 -08001566 * set the state appropriately and accept the PLOGI.
1567 *
1568 * If we had also sent a PLOGI, and if the received PLOGI is from a
1569 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1570 * "command already in progress".
1571 *
1572 * XXX TBD: If the session was ready before, the PLOGI should result in
1573 * all outstanding exchanges being reset.
1574 */
1575 switch (rdata->rp_state) {
1576 case RPORT_ST_INIT:
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001577 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
Robert Love42e9a922008-12-09 15:10:17 -08001578 break;
Joe Eykholta7b12a22010-07-20 15:20:08 -07001579 case RPORT_ST_PLOGI_WAIT:
1580 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI_WAIT state\n");
1581 break;
Robert Love42e9a922008-12-09 15:10:17 -08001582 case RPORT_ST_PLOGI:
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001583 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1584 if (rdata->ids.port_name < lport->wwpn) {
1585 mutex_unlock(&rdata->rp_mutex);
1586 rjt_data.reason = ELS_RJT_INPROG;
1587 rjt_data.explan = ELS_EXPL_NONE;
1588 goto reject;
1589 }
Robert Love42e9a922008-12-09 15:10:17 -08001590 break;
1591 case RPORT_ST_PRLI:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001592 case RPORT_ST_RTV:
Robert Love42e9a922008-12-09 15:10:17 -08001593 case RPORT_ST_READY:
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001594 case RPORT_ST_ADISC:
1595 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
1596 "- ignored for now\n", rdata->rp_state);
1597 /* XXX TBD - should reset */
Robert Love42e9a922008-12-09 15:10:17 -08001598 break;
Joe Eykholta7b12a22010-07-20 15:20:08 -07001599 case RPORT_ST_FLOGI:
Joe Eykholt14194052009-07-29 17:04:43 -07001600 case RPORT_ST_DELETE:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001601 FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
1602 fc_rport_state(rdata));
1603 mutex_unlock(&rdata->rp_mutex);
1604 rjt_data.reason = ELS_RJT_BUSY;
1605 rjt_data.explan = ELS_EXPL_NONE;
1606 goto reject;
Robert Love42e9a922008-12-09 15:10:17 -08001607 }
1608
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001609 /*
1610 * Get session payload size from incoming PLOGI.
1611 */
1612 rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
Robert Love42e9a922008-12-09 15:10:17 -08001613
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001614 /*
1615 * Send LS_ACC. If this fails, the originator should retry.
1616 */
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001617 fp = fc_frame_alloc(lport, sizeof(*pl));
1618 if (!fp)
1619 goto out;
Robert Love42e9a922008-12-09 15:10:17 -08001620
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001621 fc_plogi_fill(lport, fp, ELS_LS_ACC);
Joe Eykholt24f089e2010-07-20 15:21:01 -07001622 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1623 lport->tt.frame_send(lport, fp);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001624 fc_rport_enter_prli(rdata);
1625out:
1626 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt24f089e2010-07-20 15:21:01 -07001627 fc_frame_free(rx_fp);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001628 return;
1629
1630reject:
Joe Eykholt92261152010-07-20 15:21:12 -07001631 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001632 fc_frame_free(fp);
Robert Love42e9a922008-12-09 15:10:17 -08001633}
1634
1635/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001636 * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
1637 * @rdata: The remote port that sent the PRLI request
Robert Love3a3b42b2009-11-03 11:47:39 -08001638 * @rx_fp: The PRLI request frame
Robert Love42e9a922008-12-09 15:10:17 -08001639 *
1640 * Locking Note: The rport lock is exected to be held before calling
1641 * this function.
1642 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001643static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
Joe Eykholt92261152010-07-20 15:21:12 -07001644 struct fc_frame *rx_fp)
Robert Love42e9a922008-12-09 15:10:17 -08001645{
Robert Love42e9a922008-12-09 15:10:17 -08001646 struct fc_lport *lport = rdata->local_port;
Robert Love42e9a922008-12-09 15:10:17 -08001647 struct fc_frame *fp;
Robert Love42e9a922008-12-09 15:10:17 -08001648 struct {
1649 struct fc_els_prli prli;
1650 struct fc_els_spp spp;
1651 } *pp;
1652 struct fc_els_spp *rspp; /* request service param page */
1653 struct fc_els_spp *spp; /* response spp */
1654 unsigned int len;
1655 unsigned int plen;
Robert Love42e9a922008-12-09 15:10:17 -08001656 enum fc_els_spp_resp resp;
Joe Eykholt96ad8462011-01-28 16:04:02 -08001657 enum fc_els_spp_resp passive;
Robert Love42e9a922008-12-09 15:10:17 -08001658 struct fc_seq_els_data rjt_data;
Joe Eykholt96ad8462011-01-28 16:04:02 -08001659 struct fc4_prov *prov;
Robert Love42e9a922008-12-09 15:10:17 -08001660
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001661 FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1662 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001663
Joe Eykholt251748a2010-07-20 15:20:56 -07001664 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
Robert Love42e9a922008-12-09 15:10:17 -08001665 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
Joe Eykholta2f6a022010-03-12 16:07:36 -08001666 if (!pp)
1667 goto reject_len;
1668 plen = ntohs(pp->prli.prli_len);
1669 if ((plen % 4) != 0 || plen > len || plen < 16)
1670 goto reject_len;
1671 if (plen < len)
1672 len = plen;
1673 plen = pp->prli.prli_spp_len;
1674 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1675 plen > len || len < sizeof(*pp) || plen < 12)
1676 goto reject_len;
1677 rspp = &pp->spp;
1678
1679 fp = fc_frame_alloc(lport, len);
1680 if (!fp) {
1681 rjt_data.reason = ELS_RJT_UNAB;
1682 rjt_data.explan = ELS_EXPL_INSUF_RES;
1683 goto reject;
Robert Love42e9a922008-12-09 15:10:17 -08001684 }
Joe Eykholta2f6a022010-03-12 16:07:36 -08001685 pp = fc_frame_payload_get(fp, len);
1686 WARN_ON(!pp);
1687 memset(pp, 0, len);
1688 pp->prli.prli_cmd = ELS_LS_ACC;
1689 pp->prli.prli_spp_len = plen;
1690 pp->prli.prli_len = htons(len);
1691 len -= sizeof(struct fc_els_prli);
Robert Love42e9a922008-12-09 15:10:17 -08001692
Joe Eykholta2f6a022010-03-12 16:07:36 -08001693 /*
1694 * Go through all the service parameter pages and build
1695 * response. If plen indicates longer SPP than standard,
1696 * use that. The entire response has been pre-cleared above.
1697 */
1698 spp = &pp->spp;
Joe Eykholt96ad8462011-01-28 16:04:02 -08001699 mutex_lock(&fc_prov_mutex);
Joe Eykholta2f6a022010-03-12 16:07:36 -08001700 while (len >= plen) {
1701 spp->spp_type = rspp->spp_type;
1702 spp->spp_type_ext = rspp->spp_type_ext;
Joe Eykholt96ad8462011-01-28 16:04:02 -08001703 resp = 0;
Robert Love42e9a922008-12-09 15:10:17 -08001704
Joe Eykholt96ad8462011-01-28 16:04:02 -08001705 if (rspp->spp_type < FC_FC4_PROV_SIZE) {
1706 prov = fc_active_prov[rspp->spp_type];
1707 if (prov)
1708 resp = prov->prli(rdata, plen, rspp, spp);
1709 prov = fc_passive_prov[rspp->spp_type];
1710 if (prov) {
1711 passive = prov->prli(rdata, plen, rspp, spp);
1712 if (!resp || passive == FC_SPP_RESP_ACK)
1713 resp = passive;
1714 }
1715 }
1716 if (!resp) {
1717 if (spp->spp_flags & FC_SPP_EST_IMG_PAIR)
1718 resp |= FC_SPP_RESP_CONF;
1719 else
1720 resp |= FC_SPP_RESP_INVL;
Robert Love42e9a922008-12-09 15:10:17 -08001721 }
Joe Eykholta2f6a022010-03-12 16:07:36 -08001722 spp->spp_flags |= resp;
1723 len -= plen;
1724 rspp = (struct fc_els_spp *)((char *)rspp + plen);
1725 spp = (struct fc_els_spp *)((char *)spp + plen);
Robert Love42e9a922008-12-09 15:10:17 -08001726 }
Joe Eykholt96ad8462011-01-28 16:04:02 -08001727 mutex_unlock(&fc_prov_mutex);
Joe Eykholta2f6a022010-03-12 16:07:36 -08001728
1729 /*
1730 * Send LS_ACC. If this fails, the originator should retry.
1731 */
Joe Eykholt24f089e2010-07-20 15:21:01 -07001732 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1733 lport->tt.frame_send(lport, fp);
Joe Eykholta2f6a022010-03-12 16:07:36 -08001734
1735 switch (rdata->rp_state) {
1736 case RPORT_ST_PRLI:
1737 fc_rport_enter_ready(rdata);
1738 break;
1739 default:
1740 break;
1741 }
1742 goto drop;
1743
1744reject_len:
1745 rjt_data.reason = ELS_RJT_PROT;
1746 rjt_data.explan = ELS_EXPL_INV_LEN;
1747reject:
Joe Eykholt92261152010-07-20 15:21:12 -07001748 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
Joe Eykholta2f6a022010-03-12 16:07:36 -08001749drop:
Robert Love42e9a922008-12-09 15:10:17 -08001750 fc_frame_free(rx_fp);
1751}
1752
1753/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001754 * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
1755 * @rdata: The remote port that sent the PRLO request
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001756 * @rx_fp: The PRLO request frame
Robert Love42e9a922008-12-09 15:10:17 -08001757 *
1758 * Locking Note: The rport lock is exected to be held before calling
1759 * this function.
1760 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001761static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001762 struct fc_frame *rx_fp)
Robert Love42e9a922008-12-09 15:10:17 -08001763{
Robert Love42e9a922008-12-09 15:10:17 -08001764 struct fc_lport *lport = rdata->local_port;
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001765 struct fc_frame *fp;
1766 struct {
1767 struct fc_els_prlo prlo;
1768 struct fc_els_spp spp;
1769 } *pp;
1770 struct fc_els_spp *rspp; /* request service param page */
1771 struct fc_els_spp *spp; /* response spp */
1772 unsigned int len;
1773 unsigned int plen;
Robert Love42e9a922008-12-09 15:10:17 -08001774 struct fc_seq_els_data rjt_data;
1775
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001776 FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
1777 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001778
Joe Eykholt251748a2010-07-20 15:20:56 -07001779 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001780 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1781 if (!pp)
1782 goto reject_len;
1783 plen = ntohs(pp->prlo.prlo_len);
1784 if (plen != 20)
1785 goto reject_len;
1786 if (plen < len)
1787 len = plen;
1788
1789 rspp = &pp->spp;
1790
1791 fp = fc_frame_alloc(lport, len);
1792 if (!fp) {
1793 rjt_data.reason = ELS_RJT_UNAB;
1794 rjt_data.explan = ELS_EXPL_INSUF_RES;
1795 goto reject;
1796 }
1797
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001798 pp = fc_frame_payload_get(fp, len);
1799 WARN_ON(!pp);
1800 memset(pp, 0, len);
1801 pp->prlo.prlo_cmd = ELS_LS_ACC;
1802 pp->prlo.prlo_obs = 0x10;
1803 pp->prlo.prlo_len = htons(len);
1804 spp = &pp->spp;
1805 spp->spp_type = rspp->spp_type;
1806 spp->spp_type_ext = rspp->spp_type_ext;
1807 spp->spp_flags = FC_SPP_RESP_ACK;
1808
1809 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
1810
Joe Eykholt92261152010-07-20 15:21:12 -07001811 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1812 lport->tt.frame_send(lport, fp);
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001813 goto drop;
1814
1815reject_len:
1816 rjt_data.reason = ELS_RJT_PROT;
1817 rjt_data.explan = ELS_EXPL_INV_LEN;
1818reject:
Joe Eykholt92261152010-07-20 15:21:12 -07001819 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001820drop:
1821 fc_frame_free(rx_fp);
Robert Love42e9a922008-12-09 15:10:17 -08001822}
1823
1824/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001825 * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
1826 * @lport: The local port that received the LOGO request
Robert Love3a3b42b2009-11-03 11:47:39 -08001827 * @fp: The LOGO request frame
Robert Love42e9a922008-12-09 15:10:17 -08001828 *
1829 * Locking Note: The rport lock is exected to be held before calling
1830 * this function.
1831 */
Joe Eykholt92261152010-07-20 15:21:12 -07001832static void fc_rport_recv_logo_req(struct fc_lport *lport, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -08001833{
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001834 struct fc_rport_priv *rdata;
1835 u32 sid;
Robert Love42e9a922008-12-09 15:10:17 -08001836
Joe Eykholt92261152010-07-20 15:21:12 -07001837 lport->tt.seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
Joe Eykholtfeab4ae2009-08-25 14:03:36 -07001838
Joe Eykholt251748a2010-07-20 15:20:56 -07001839 sid = fc_frame_sid(fp);
Robert Love42e9a922008-12-09 15:10:17 -08001840
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001841 mutex_lock(&lport->disc.disc_mutex);
1842 rdata = lport->tt.rport_lookup(lport, sid);
1843 if (rdata) {
1844 mutex_lock(&rdata->rp_mutex);
1845 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
1846 fc_rport_state(rdata));
Joe Eykholtfeab4ae2009-08-25 14:03:36 -07001847
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001848 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001849 mutex_unlock(&rdata->rp_mutex);
1850 } else
1851 FC_RPORT_ID_DBG(lport, sid,
1852 "Received LOGO from non-logged-in port\n");
1853 mutex_unlock(&lport->disc.disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -08001854 fc_frame_free(fp);
1855}
1856
Robert Love3a3b42b2009-11-03 11:47:39 -08001857/**
1858 * fc_rport_flush_queue() - Flush the rport_event_queue
1859 */
Robert Love42e9a922008-12-09 15:10:17 -08001860static void fc_rport_flush_queue(void)
1861{
1862 flush_workqueue(rport_event_queue);
1863}
1864
Robert Love3a3b42b2009-11-03 11:47:39 -08001865/**
1866 * fc_rport_init() - Initialize the remote port layer for a local port
1867 * @lport: The local port to initialize the remote port layer for
1868 */
Robert Love42e9a922008-12-09 15:10:17 -08001869int fc_rport_init(struct fc_lport *lport)
1870{
Joe Eykholt8025b5d2009-08-25 14:02:06 -07001871 if (!lport->tt.rport_lookup)
1872 lport->tt.rport_lookup = fc_rport_lookup;
1873
Robert Love5101ff92009-02-27 10:55:18 -08001874 if (!lport->tt.rport_create)
Joe Eykholt9e9d0452009-08-25 14:01:18 -07001875 lport->tt.rport_create = fc_rport_create;
Robert Love5101ff92009-02-27 10:55:18 -08001876
Robert Love42e9a922008-12-09 15:10:17 -08001877 if (!lport->tt.rport_login)
1878 lport->tt.rport_login = fc_rport_login;
1879
1880 if (!lport->tt.rport_logoff)
1881 lport->tt.rport_logoff = fc_rport_logoff;
1882
1883 if (!lport->tt.rport_recv_req)
1884 lport->tt.rport_recv_req = fc_rport_recv_req;
1885
1886 if (!lport->tt.rport_flush_queue)
1887 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1888
Joe Eykholtf211fa52009-08-25 14:01:01 -07001889 if (!lport->tt.rport_destroy)
1890 lport->tt.rport_destroy = fc_rport_destroy;
1891
Robert Love42e9a922008-12-09 15:10:17 -08001892 return 0;
1893}
1894EXPORT_SYMBOL(fc_rport_init);
1895
Robert Love3a3b42b2009-11-03 11:47:39 -08001896/**
Joe Eykholt96ad8462011-01-28 16:04:02 -08001897 * fc_rport_fcp_prli() - Handle incoming PRLI for the FCP initiator.
1898 * @rdata: remote port private
1899 * @spp_len: service parameter page length
1900 * @rspp: received service parameter page
1901 * @spp: response service parameter page
1902 *
1903 * Returns the value for the response code to be placed in spp_flags;
1904 * Returns 0 if not an initiator.
1905 */
1906static int fc_rport_fcp_prli(struct fc_rport_priv *rdata, u32 spp_len,
1907 const struct fc_els_spp *rspp,
1908 struct fc_els_spp *spp)
1909{
1910 struct fc_lport *lport = rdata->local_port;
1911 u32 fcp_parm;
1912
1913 fcp_parm = ntohl(rspp->spp_params);
1914 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
1915 if (fcp_parm & FCP_SPPF_INIT_FCN)
1916 rdata->ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1917 if (fcp_parm & FCP_SPPF_TARG_FCN)
1918 rdata->ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1919 if (fcp_parm & FCP_SPPF_RETRY)
1920 rdata->flags |= FC_RP_FLAGS_RETRY;
1921 rdata->supported_classes = FC_COS_CLASS3;
1922
1923 if (!(lport->service_params & FC_RPORT_ROLE_FCP_INITIATOR))
1924 return 0;
1925
1926 spp->spp_flags |= rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
1927
1928 /*
1929 * OR in our service parameters with other providers (target), if any.
1930 */
1931 fcp_parm = ntohl(spp->spp_params);
1932 spp->spp_params = htonl(fcp_parm | lport->service_params);
1933 return FC_SPP_RESP_ACK;
1934}
1935
1936/*
1937 * FC-4 provider ops for FCP initiator.
1938 */
1939struct fc4_prov fc_rport_fcp_init = {
1940 .prli = fc_rport_fcp_prli,
1941};
1942
1943/**
1944 * fc_rport_t0_prli() - Handle incoming PRLI parameters for type 0
1945 * @rdata: remote port private
1946 * @spp_len: service parameter page length
1947 * @rspp: received service parameter page
1948 * @spp: response service parameter page
1949 */
1950static int fc_rport_t0_prli(struct fc_rport_priv *rdata, u32 spp_len,
1951 const struct fc_els_spp *rspp,
1952 struct fc_els_spp *spp)
1953{
1954 if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR)
1955 return FC_SPP_RESP_INVL;
1956 return FC_SPP_RESP_ACK;
1957}
1958
1959/*
1960 * FC-4 provider ops for type 0 service parameters.
1961 *
1962 * This handles the special case of type 0 which is always successful
1963 * but doesn't do anything otherwise.
1964 */
1965struct fc4_prov fc_rport_t0_prov = {
1966 .prli = fc_rport_t0_prli,
1967};
1968
1969/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001970 * fc_setup_rport() - Initialize the rport_event_queue
1971 */
Randy Dunlap55204902011-01-28 16:03:57 -08001972int fc_setup_rport(void)
Robert Love42e9a922008-12-09 15:10:17 -08001973{
1974 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
1975 if (!rport_event_queue)
1976 return -ENOMEM;
1977 return 0;
1978}
Robert Love42e9a922008-12-09 15:10:17 -08001979
Robert Love3a3b42b2009-11-03 11:47:39 -08001980/**
1981 * fc_destroy_rport() - Destroy the rport_event_queue
1982 */
Randy Dunlap55204902011-01-28 16:03:57 -08001983void fc_destroy_rport(void)
Robert Love42e9a922008-12-09 15:10:17 -08001984{
1985 destroy_workqueue(rport_event_queue);
1986}
Robert Love42e9a922008-12-09 15:10:17 -08001987
Robert Love3a3b42b2009-11-03 11:47:39 -08001988/**
1989 * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
1990 * @rport: The remote port whose I/O should be terminated
1991 */
Robert Love42e9a922008-12-09 15:10:17 -08001992void fc_rport_terminate_io(struct fc_rport *rport)
1993{
Robert Love3a3b42b2009-11-03 11:47:39 -08001994 struct fc_rport_libfc_priv *rpriv = rport->dd_data;
1995 struct fc_lport *lport = rpriv->local_port;
Robert Love42e9a922008-12-09 15:10:17 -08001996
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -08001997 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
1998 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
Robert Love42e9a922008-12-09 15:10:17 -08001999}
2000EXPORT_SYMBOL(fc_rport_terminate_io);