blob: a7175adab32dd4278967a0a49e276ed09f8d6a1b [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
Robert Love42e9a922008-12-09 15:10:17 -080061struct workqueue_struct *rport_event_queue;
62
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;
Robert Love42e9a922008-12-09 15:10:17 -0800260
261 mutex_lock(&rdata->rp_mutex);
262 event = rdata->event;
263 rport_ops = rdata->ops;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700264 rport = rdata->rport;
Robert Love42e9a922008-12-09 15:10:17 -0800265
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700266 FC_RPORT_DBG(rdata, "work event %u\n", event);
267
Joe Eykholt629f4422009-08-25 14:01:06 -0700268 switch (event) {
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700269 case RPORT_EV_READY:
Joe Eykholtf211fa52009-08-25 14:01:01 -0700270 ids = rdata->ids;
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700271 rdata->event = RPORT_EV_NONE;
Joe Eykholtf0342602010-06-11 16:44:57 -0700272 rdata->major_retries = 0;
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700273 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800274 mutex_unlock(&rdata->rp_mutex);
275
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700276 if (!rport)
277 rport = fc_remote_port_add(lport->host, 0, &ids);
278 if (!rport) {
279 FC_RPORT_DBG(rdata, "Failed to add the rport\n");
280 lport->tt.rport_logoff(rdata);
281 kref_put(&rdata->kref, lport->tt.rport_destroy);
282 return;
Robert Love42e9a922008-12-09 15:10:17 -0800283 }
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700284 mutex_lock(&rdata->rp_mutex);
285 if (rdata->rport)
286 FC_RPORT_DBG(rdata, "rport already allocated\n");
287 rdata->rport = rport;
288 rport->maxframe_size = rdata->maxframe_size;
289 rport->supported_classes = rdata->supported_classes;
290
Robert Love3a3b42b2009-11-03 11:47:39 -0800291 rpriv = rport->dd_data;
292 rpriv->local_port = lport;
293 rpriv->rp_state = rdata->rp_state;
294 rpriv->flags = rdata->flags;
295 rpriv->e_d_tov = rdata->e_d_tov;
296 rpriv->r_a_tov = rdata->r_a_tov;
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700297 mutex_unlock(&rdata->rp_mutex);
298
Joe Eykholt83455922009-08-25 14:02:01 -0700299 if (rport_ops && rport_ops->event_callback) {
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700300 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700301 rport_ops->event_callback(lport, rdata, event);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700302 }
303 kref_put(&rdata->kref, lport->tt.rport_destroy);
Joe Eykholt629f4422009-08-25 14:01:06 -0700304 break;
305
306 case RPORT_EV_FAILED:
307 case RPORT_EV_LOGO:
308 case RPORT_EV_STOP:
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700309 port_id = rdata->ids.port_id;
Robert Love42e9a922008-12-09 15:10:17 -0800310 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700311
Joe Eykholt83455922009-08-25 14:02:01 -0700312 if (rport_ops && rport_ops->event_callback) {
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700313 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700314 rport_ops->event_callback(lport, rdata, event);
Abhijeet Joglekar571f8242009-02-27 10:54:41 -0800315 }
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700316 cancel_delayed_work_sync(&rdata->retry_work);
317
318 /*
319 * Reset any outstanding exchanges before freeing rport.
320 */
321 lport->tt.exch_mgr_reset(lport, 0, port_id);
322 lport->tt.exch_mgr_reset(lport, port_id, 0);
323
324 if (rport) {
Robert Love3a3b42b2009-11-03 11:47:39 -0800325 rpriv = rport->dd_data;
326 rpriv->rp_state = RPORT_ST_DELETE;
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700327 mutex_lock(&rdata->rp_mutex);
328 rdata->rport = NULL;
329 mutex_unlock(&rdata->rp_mutex);
330 fc_remote_port_delete(rport);
331 }
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700332
333 mutex_lock(&lport->disc.disc_mutex);
334 mutex_lock(&rdata->rp_mutex);
335 if (rdata->rp_state == RPORT_ST_DELETE) {
336 if (port_id == FC_FID_DIR_SERV) {
337 rdata->event = RPORT_EV_NONE;
338 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf0342602010-06-11 16:44:57 -0700339 } else if ((rdata->flags & FC_RP_STARTED) &&
340 rdata->major_retries <
341 lport->max_rport_retry_count) {
342 rdata->major_retries++;
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700343 rdata->event = RPORT_EV_NONE;
344 FC_RPORT_DBG(rdata, "work restart\n");
Joe Eykholta7b12a22010-07-20 15:20:08 -0700345 fc_rport_enter_flogi(rdata);
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700346 mutex_unlock(&rdata->rp_mutex);
347 } else {
348 FC_RPORT_DBG(rdata, "work delete\n");
Joe Eykholt42e90412010-07-20 15:19:37 -0700349 list_del_rcu(&rdata->peers);
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700350 mutex_unlock(&rdata->rp_mutex);
351 kref_put(&rdata->kref, lport->tt.rport_destroy);
352 }
353 } else {
354 /*
355 * Re-open for events. Reissue READY event if ready.
356 */
357 rdata->event = RPORT_EV_NONE;
358 if (rdata->rp_state == RPORT_ST_READY)
359 fc_rport_enter_ready(rdata);
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700360 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700361 }
362 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt629f4422009-08-25 14:01:06 -0700363 break;
364
365 default:
Robert Love42e9a922008-12-09 15:10:17 -0800366 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt629f4422009-08-25 14:01:06 -0700367 break;
368 }
Robert Love42e9a922008-12-09 15:10:17 -0800369}
370
371/**
Robert Love34f42a02009-02-27 10:55:45 -0800372 * fc_rport_login() - Start the remote port login state machine
Robert Love3a3b42b2009-11-03 11:47:39 -0800373 * @rdata: The remote port to be logged in to
Robert Love42e9a922008-12-09 15:10:17 -0800374 *
375 * Locking Note: Called without the rport lock held. This
376 * function will hold the rport lock, call an _enter_*
377 * function and then unlock the rport.
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700378 *
379 * This indicates the intent to be logged into the remote port.
380 * If it appears we are already logged in, ADISC is used to verify
381 * the setup.
Robert Love42e9a922008-12-09 15:10:17 -0800382 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700383int fc_rport_login(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800384{
Robert Love42e9a922008-12-09 15:10:17 -0800385 mutex_lock(&rdata->rp_mutex);
386
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700387 rdata->flags |= FC_RP_STARTED;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700388 switch (rdata->rp_state) {
389 case RPORT_ST_READY:
390 FC_RPORT_DBG(rdata, "ADISC port\n");
391 fc_rport_enter_adisc(rdata);
392 break;
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700393 case RPORT_ST_DELETE:
394 FC_RPORT_DBG(rdata, "Restart deleted port\n");
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700395 break;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700396 default:
397 FC_RPORT_DBG(rdata, "Login to port\n");
Joe Eykholta7b12a22010-07-20 15:20:08 -0700398 fc_rport_enter_flogi(rdata);
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700399 break;
400 }
Robert Love42e9a922008-12-09 15:10:17 -0800401 mutex_unlock(&rdata->rp_mutex);
402
403 return 0;
404}
405
406/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800407 * fc_rport_enter_delete() - Schedule a remote port to be deleted
408 * @rdata: The remote port to be deleted
409 * @event: The event to report as the reason for deletion
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700410 *
411 * Locking Note: Called with the rport lock held.
412 *
413 * Allow state change into DELETE only once.
414 *
415 * Call queue_work only if there's no event already pending.
416 * Set the new event so that the old pending event will not occur.
417 * Since we have the mutex, even if fc_rport_work() is already started,
418 * it'll see the new event.
419 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700420static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700421 enum fc_rport_event event)
422{
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700423 if (rdata->rp_state == RPORT_ST_DELETE)
424 return;
425
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700426 FC_RPORT_DBG(rdata, "Delete port\n");
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700427
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700428 fc_rport_state_enter(rdata, RPORT_ST_DELETE);
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700429
430 if (rdata->event == RPORT_EV_NONE)
431 queue_work(rport_event_queue, &rdata->event_work);
432 rdata->event = event;
433}
434
435/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800436 * fc_rport_logoff() - Logoff and remove a remote port
437 * @rdata: The remote port to be logged off of
Robert Love42e9a922008-12-09 15:10:17 -0800438 *
439 * Locking Note: Called without the rport lock held. This
440 * function will hold the rport lock, call an _enter_*
441 * function and then unlock the rport.
442 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700443int fc_rport_logoff(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800444{
Robert Love42e9a922008-12-09 15:10:17 -0800445 mutex_lock(&rdata->rp_mutex);
446
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700447 FC_RPORT_DBG(rdata, "Remove port\n");
Robert Love42e9a922008-12-09 15:10:17 -0800448
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700449 rdata->flags &= ~FC_RP_STARTED;
Joe Eykholt14194052009-07-29 17:04:43 -0700450 if (rdata->rp_state == RPORT_ST_DELETE) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700451 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700452 goto out;
453 }
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700454 fc_rport_enter_logo(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800455
456 /*
Joe Eykholt14194052009-07-29 17:04:43 -0700457 * Change the state to Delete so that we discard
Robert Love42e9a922008-12-09 15:10:17 -0800458 * the response.
459 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700460 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700461out:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700462 mutex_unlock(&rdata->rp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800463 return 0;
464}
465
466/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800467 * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state
468 * @rdata: The remote port that is ready
Robert Love42e9a922008-12-09 15:10:17 -0800469 *
470 * Locking Note: The rport lock is expected to be held before calling
471 * this routine.
472 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700473static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800474{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700475 fc_rport_state_enter(rdata, RPORT_ST_READY);
Robert Love42e9a922008-12-09 15:10:17 -0800476
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700477 FC_RPORT_DBG(rdata, "Port is Ready\n");
Robert Love42e9a922008-12-09 15:10:17 -0800478
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700479 if (rdata->event == RPORT_EV_NONE)
480 queue_work(rport_event_queue, &rdata->event_work);
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700481 rdata->event = RPORT_EV_READY;
Robert Love42e9a922008-12-09 15:10:17 -0800482}
483
484/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800485 * fc_rport_timeout() - Handler for the retry_work timer
486 * @work: Handle to the remote port that has timed out
Robert Love42e9a922008-12-09 15:10:17 -0800487 *
488 * Locking Note: Called without the rport lock held. This
489 * function will hold the rport lock, call an _enter_*
490 * function and then unlock the rport.
491 */
492static void fc_rport_timeout(struct work_struct *work)
493{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700494 struct fc_rport_priv *rdata =
495 container_of(work, struct fc_rport_priv, retry_work.work);
Robert Love42e9a922008-12-09 15:10:17 -0800496
497 mutex_lock(&rdata->rp_mutex);
498
499 switch (rdata->rp_state) {
Joe Eykholta7b12a22010-07-20 15:20:08 -0700500 case RPORT_ST_FLOGI:
501 fc_rport_enter_flogi(rdata);
502 break;
Robert Love42e9a922008-12-09 15:10:17 -0800503 case RPORT_ST_PLOGI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700504 fc_rport_enter_plogi(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800505 break;
506 case RPORT_ST_PRLI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700507 fc_rport_enter_prli(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800508 break;
509 case RPORT_ST_RTV:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700510 fc_rport_enter_rtv(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800511 break;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700512 case RPORT_ST_ADISC:
513 fc_rport_enter_adisc(rdata);
514 break;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700515 case RPORT_ST_PLOGI_WAIT:
Robert Love42e9a922008-12-09 15:10:17 -0800516 case RPORT_ST_READY:
517 case RPORT_ST_INIT:
Joe Eykholt14194052009-07-29 17:04:43 -0700518 case RPORT_ST_DELETE:
Robert Love42e9a922008-12-09 15:10:17 -0800519 break;
520 }
521
522 mutex_unlock(&rdata->rp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800523}
524
525/**
Robert Love34f42a02009-02-27 10:55:45 -0800526 * fc_rport_error() - Error handler, called once retries have been exhausted
Robert Love3a3b42b2009-11-03 11:47:39 -0800527 * @rdata: The remote port the error is happened on
528 * @fp: The error code encapsulated in a frame pointer
Robert Love42e9a922008-12-09 15:10:17 -0800529 *
Robert Love42e9a922008-12-09 15:10:17 -0800530 * Locking Note: The rport lock is expected to be held before
531 * calling this routine
532 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700533static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -0800534{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700535 FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
Joe Eykholtcdbe6df2009-08-25 14:01:39 -0700536 IS_ERR(fp) ? -PTR_ERR(fp) : 0,
537 fc_rport_state(rdata), rdata->retries);
Robert Love42e9a922008-12-09 15:10:17 -0800538
Chris Leech6755db12009-02-27 10:55:02 -0800539 switch (rdata->rp_state) {
Joe Eykholta7b12a22010-07-20 15:20:08 -0700540 case RPORT_ST_FLOGI:
Chris Leech6755db12009-02-27 10:55:02 -0800541 case RPORT_ST_PLOGI:
Joe Eykholt4b2164d2010-06-11 16:44:51 -0700542 rdata->flags &= ~FC_RP_STARTED;
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700543 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
Chris Leech6755db12009-02-27 10:55:02 -0800544 break;
545 case RPORT_ST_RTV:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700546 fc_rport_enter_ready(rdata);
Chris Leech6755db12009-02-27 10:55:02 -0800547 break;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700548 case RPORT_ST_PRLI:
549 case RPORT_ST_ADISC:
550 fc_rport_enter_logo(rdata);
551 break;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700552 case RPORT_ST_PLOGI_WAIT:
Joe Eykholt14194052009-07-29 17:04:43 -0700553 case RPORT_ST_DELETE:
Chris Leech6755db12009-02-27 10:55:02 -0800554 case RPORT_ST_READY:
555 case RPORT_ST_INIT:
556 break;
Robert Love42e9a922008-12-09 15:10:17 -0800557 }
558}
559
560/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800561 * fc_rport_error_retry() - Handler for remote port state retries
562 * @rdata: The remote port whose state is to be retried
563 * @fp: The error code encapsulated in a frame pointer
Chris Leech6755db12009-02-27 10:55:02 -0800564 *
565 * If the error was an exchange timeout retry immediately,
566 * otherwise wait for E_D_TOV.
567 *
568 * Locking Note: The rport lock is expected to be held before
569 * calling this routine
570 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700571static void fc_rport_error_retry(struct fc_rport_priv *rdata,
572 struct fc_frame *fp)
Chris Leech6755db12009-02-27 10:55:02 -0800573{
Chris Leech6755db12009-02-27 10:55:02 -0800574 unsigned long delay = FC_DEF_E_D_TOV;
575
576 /* make sure this isn't an FC_EX_CLOSED error, never retry those */
577 if (PTR_ERR(fp) == -FC_EX_CLOSED)
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700578 return fc_rport_error(rdata, fp);
Chris Leech6755db12009-02-27 10:55:02 -0800579
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700580 if (rdata->retries < rdata->local_port->max_rport_retry_count) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700581 FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
582 PTR_ERR(fp), fc_rport_state(rdata));
Chris Leech6755db12009-02-27 10:55:02 -0800583 rdata->retries++;
584 /* no additional delay on exchange timeouts */
585 if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
586 delay = 0;
Chris Leech6755db12009-02-27 10:55:02 -0800587 schedule_delayed_work(&rdata->retry_work, delay);
588 return;
589 }
590
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700591 return fc_rport_error(rdata, fp);
Chris Leech6755db12009-02-27 10:55:02 -0800592}
593
594/**
Joe Eykholta7b12a22010-07-20 15:20:08 -0700595 * fc_rport_login_complete() - Handle parameters and completion of p-mp login.
596 * @rdata: The remote port which we logged into or which logged into us.
597 * @fp: The FLOGI or PLOGI request or response frame
598 *
599 * Returns non-zero error if a problem is detected with the frame.
600 * Does not free the frame.
601 *
602 * This is only used in point-to-multipoint mode for FIP currently.
603 */
604static int fc_rport_login_complete(struct fc_rport_priv *rdata,
605 struct fc_frame *fp)
606{
607 struct fc_lport *lport = rdata->local_port;
608 struct fc_els_flogi *flogi;
609 unsigned int e_d_tov;
610 u16 csp_flags;
611
612 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
613 if (!flogi)
614 return -EINVAL;
615
616 csp_flags = ntohs(flogi->fl_csp.sp_features);
617
618 if (fc_frame_payload_op(fp) == ELS_FLOGI) {
619 if (csp_flags & FC_SP_FT_FPORT) {
620 FC_RPORT_DBG(rdata, "Fabric bit set in FLOGI\n");
621 return -EINVAL;
622 }
623 } else {
624
625 /*
626 * E_D_TOV is not valid on an incoming FLOGI request.
627 */
628 e_d_tov = ntohl(flogi->fl_csp.sp_e_d_tov);
629 if (csp_flags & FC_SP_FT_EDTR)
630 e_d_tov /= 1000000;
631 if (e_d_tov > rdata->e_d_tov)
632 rdata->e_d_tov = e_d_tov;
633 }
634 rdata->maxframe_size = fc_plogi_get_maxframe(flogi, lport->mfs);
635 return 0;
636}
637
638/**
639 * fc_rport_flogi_resp() - Handle response to FLOGI request for p-mp mode
640 * @sp: The sequence that the FLOGI was on
641 * @fp: The FLOGI response frame
642 * @rp_arg: The remote port that received the FLOGI response
643 */
644void fc_rport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
645 void *rp_arg)
646{
647 struct fc_rport_priv *rdata = rp_arg;
648 struct fc_lport *lport = rdata->local_port;
649 struct fc_els_flogi *flogi;
650 unsigned int r_a_tov;
651
652 FC_RPORT_DBG(rdata, "Received a FLOGI %s\n", fc_els_resp_type(fp));
653
654 if (fp == ERR_PTR(-FC_EX_CLOSED))
Hillf Danton0e9e3d32010-11-30 16:19:04 -0800655 goto put;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700656
657 mutex_lock(&rdata->rp_mutex);
658
659 if (rdata->rp_state != RPORT_ST_FLOGI) {
660 FC_RPORT_DBG(rdata, "Received a FLOGI response, but in state "
661 "%s\n", fc_rport_state(rdata));
662 if (IS_ERR(fp))
663 goto err;
664 goto out;
665 }
666
667 if (IS_ERR(fp)) {
668 fc_rport_error(rdata, fp);
669 goto err;
670 }
671
672 if (fc_frame_payload_op(fp) != ELS_LS_ACC)
673 goto bad;
674 if (fc_rport_login_complete(rdata, fp))
675 goto bad;
676
677 flogi = fc_frame_payload_get(fp, sizeof(*flogi));
678 if (!flogi)
679 goto bad;
680 r_a_tov = ntohl(flogi->fl_csp.sp_r_a_tov);
681 if (r_a_tov > rdata->r_a_tov)
682 rdata->r_a_tov = r_a_tov;
683
684 if (rdata->ids.port_name < lport->wwpn)
685 fc_rport_enter_plogi(rdata);
686 else
687 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
688out:
689 fc_frame_free(fp);
690err:
691 mutex_unlock(&rdata->rp_mutex);
Hillf Danton0e9e3d32010-11-30 16:19:04 -0800692put:
Joe Eykholta7b12a22010-07-20 15:20:08 -0700693 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
694 return;
695bad:
696 FC_RPORT_DBG(rdata, "Bad FLOGI response\n");
697 fc_rport_error_retry(rdata, fp);
698 goto out;
699}
700
701/**
702 * fc_rport_enter_flogi() - Send a FLOGI request to the remote port for p-mp
703 * @rdata: The remote port to send a FLOGI to
704 *
705 * Locking Note: The rport lock is expected to be held before calling
706 * this routine.
707 */
708static void fc_rport_enter_flogi(struct fc_rport_priv *rdata)
709{
710 struct fc_lport *lport = rdata->local_port;
711 struct fc_frame *fp;
712
713 if (!lport->point_to_multipoint)
714 return fc_rport_enter_plogi(rdata);
715
716 FC_RPORT_DBG(rdata, "Entered FLOGI state from %s state\n",
717 fc_rport_state(rdata));
718
719 fc_rport_state_enter(rdata, RPORT_ST_FLOGI);
720
721 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
722 if (!fp)
723 return fc_rport_error_retry(rdata, fp);
724
725 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_FLOGI,
726 fc_rport_flogi_resp, rdata,
727 2 * lport->r_a_tov))
728 fc_rport_error_retry(rdata, NULL);
729 else
730 kref_get(&rdata->kref);
731}
732
733/**
734 * fc_rport_recv_flogi_req() - Handle Fabric Login (FLOGI) request in p-mp mode
735 * @lport: The local port that received the PLOGI request
Joe Eykholta7b12a22010-07-20 15:20:08 -0700736 * @rx_fp: The PLOGI request frame
737 */
738static void fc_rport_recv_flogi_req(struct fc_lport *lport,
Joe Eykholt922611562010-07-20 15:21:12 -0700739 struct fc_frame *rx_fp)
Joe Eykholta7b12a22010-07-20 15:20:08 -0700740{
741 struct fc_disc *disc;
742 struct fc_els_flogi *flp;
743 struct fc_rport_priv *rdata;
744 struct fc_frame *fp = rx_fp;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700745 struct fc_seq_els_data rjt_data;
Joe Eykholt24f089e2010-07-20 15:21:01 -0700746 u32 sid;
Joe Eykholta7b12a22010-07-20 15:20:08 -0700747
Joe Eykholt251748a2010-07-20 15:20:56 -0700748 sid = fc_frame_sid(fp);
Joe Eykholta7b12a22010-07-20 15:20:08 -0700749
750 FC_RPORT_ID_DBG(lport, sid, "Received FLOGI request\n");
751
752 disc = &lport->disc;
753 mutex_lock(&disc->disc_mutex);
754
755 if (!lport->point_to_multipoint) {
756 rjt_data.reason = ELS_RJT_UNSUP;
757 rjt_data.explan = ELS_EXPL_NONE;
758 goto reject;
759 }
760
761 flp = fc_frame_payload_get(fp, sizeof(*flp));
762 if (!flp) {
763 rjt_data.reason = ELS_RJT_LOGIC;
764 rjt_data.explan = ELS_EXPL_INV_LEN;
765 goto reject;
766 }
767
768 rdata = lport->tt.rport_lookup(lport, sid);
769 if (!rdata) {
770 rjt_data.reason = ELS_RJT_FIP;
771 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
772 goto reject;
773 }
774 mutex_lock(&rdata->rp_mutex);
775
776 FC_RPORT_DBG(rdata, "Received FLOGI in %s state\n",
777 fc_rport_state(rdata));
778
779 switch (rdata->rp_state) {
780 case RPORT_ST_INIT:
Joe Eykholta7b12a22010-07-20 15:20:08 -0700781 case RPORT_ST_DELETE:
782 mutex_unlock(&rdata->rp_mutex);
783 rjt_data.reason = ELS_RJT_FIP;
784 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
785 goto reject;
786 case RPORT_ST_FLOGI:
787 case RPORT_ST_PLOGI_WAIT:
788 case RPORT_ST_PLOGI:
789 break;
790 case RPORT_ST_PRLI:
791 case RPORT_ST_RTV:
792 case RPORT_ST_READY:
793 case RPORT_ST_ADISC:
794 /*
795 * Set the remote port to be deleted and to then restart.
796 * This queues work to be sure exchanges are reset.
797 */
798 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
799 mutex_unlock(&rdata->rp_mutex);
800 rjt_data.reason = ELS_RJT_BUSY;
801 rjt_data.explan = ELS_EXPL_NONE;
802 goto reject;
803 }
804 if (fc_rport_login_complete(rdata, fp)) {
805 mutex_unlock(&rdata->rp_mutex);
806 rjt_data.reason = ELS_RJT_LOGIC;
807 rjt_data.explan = ELS_EXPL_NONE;
808 goto reject;
809 }
Joe Eykholta7b12a22010-07-20 15:20:08 -0700810
811 fp = fc_frame_alloc(lport, sizeof(*flp));
812 if (!fp)
813 goto out;
814
Joe Eykholta7b12a22010-07-20 15:20:08 -0700815 fc_flogi_fill(lport, fp);
816 flp = fc_frame_payload_get(fp, sizeof(*flp));
817 flp->fl_cmd = ELS_LS_ACC;
818
Joe Eykholt24f089e2010-07-20 15:21:01 -0700819 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
820 lport->tt.frame_send(lport, fp);
Joe Eykholta7b12a22010-07-20 15:20:08 -0700821
822 if (rdata->ids.port_name < lport->wwpn)
823 fc_rport_enter_plogi(rdata);
824 else
825 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
826out:
827 mutex_unlock(&rdata->rp_mutex);
828 mutex_unlock(&disc->disc_mutex);
Joe Eykholt24f089e2010-07-20 15:21:01 -0700829 fc_frame_free(rx_fp);
Joe Eykholta7b12a22010-07-20 15:20:08 -0700830 return;
831
832reject:
833 mutex_unlock(&disc->disc_mutex);
Joe Eykholt922611562010-07-20 15:21:12 -0700834 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
Joe Eykholt24f089e2010-07-20 15:21:01 -0700835 fc_frame_free(rx_fp);
Joe Eykholta7b12a22010-07-20 15:20:08 -0700836}
837
838/**
839 * fc_rport_plogi_resp() - Handler for ELS PLOGI responses
Robert Love3a3b42b2009-11-03 11:47:39 -0800840 * @sp: The sequence the PLOGI is on
841 * @fp: The PLOGI response frame
842 * @rdata_arg: The remote port that sent the PLOGI response
Robert Love42e9a922008-12-09 15:10:17 -0800843 *
844 * Locking Note: This function will be called without the rport lock
845 * held, but it will lock, call an _enter_* function or fc_rport_error
846 * and then unlock the rport.
847 */
848static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700849 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800850{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700851 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800852 struct fc_lport *lport = rdata->local_port;
Robert Lovea29e7642009-04-21 16:27:41 -0700853 struct fc_els_flogi *plp = NULL;
Robert Love42e9a922008-12-09 15:10:17 -0800854 u16 csp_seq;
855 u16 cssp_seq;
856 u8 op;
857
858 mutex_lock(&rdata->rp_mutex);
859
Joe Eykholtf657d292009-08-25 14:03:21 -0700860 FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800861
862 if (rdata->rp_state != RPORT_ST_PLOGI) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700863 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
864 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700865 if (IS_ERR(fp))
866 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800867 goto out;
868 }
869
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700870 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700871 fc_rport_error_retry(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700872 goto err;
873 }
874
Robert Love42e9a922008-12-09 15:10:17 -0800875 op = fc_frame_payload_op(fp);
876 if (op == ELS_LS_ACC &&
877 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
Joe Eykholtf211fa52009-08-25 14:01:01 -0700878 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
879 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
Robert Love42e9a922008-12-09 15:10:17 -0800880
Joe Eykholta7b12a22010-07-20 15:20:08 -0700881 if (lport->point_to_multipoint)
882 fc_rport_login_complete(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800883 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
884 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
885 if (cssp_seq < csp_seq)
886 csp_seq = cssp_seq;
887 rdata->max_seq = csp_seq;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700888 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
Joe Eykholt3ac6f982009-08-25 14:03:26 -0700889 fc_rport_enter_prli(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800890 } else
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700891 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800892
893out:
894 fc_frame_free(fp);
895err:
896 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700897 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800898}
899
900/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800901 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
902 * @rdata: The remote port to send a PLOGI to
Robert Love42e9a922008-12-09 15:10:17 -0800903 *
904 * Locking Note: The rport lock is expected to be held before calling
905 * this routine.
906 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700907static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800908{
Robert Love42e9a922008-12-09 15:10:17 -0800909 struct fc_lport *lport = rdata->local_port;
910 struct fc_frame *fp;
911
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700912 FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
913 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800914
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700915 fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
Robert Love42e9a922008-12-09 15:10:17 -0800916
Joe Eykholtf211fa52009-08-25 14:01:01 -0700917 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
Robert Love42e9a922008-12-09 15:10:17 -0800918 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
919 if (!fp) {
Joe Eykholta7b12a22010-07-20 15:20:08 -0700920 FC_RPORT_DBG(rdata, "%s frame alloc failed\n", __func__);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700921 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800922 return;
923 }
924 rdata->e_d_tov = lport->e_d_tov;
925
Joe Eykholtf211fa52009-08-25 14:01:01 -0700926 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
Joe Eykholtb94f8952009-11-03 11:50:21 -0800927 fc_rport_plogi_resp, rdata,
928 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -0700929 fc_rport_error_retry(rdata, NULL);
Robert Love42e9a922008-12-09 15:10:17 -0800930 else
Joe Eykholtf211fa52009-08-25 14:01:01 -0700931 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800932}
933
934/**
Robert Love34f42a02009-02-27 10:55:45 -0800935 * fc_rport_prli_resp() - Process Login (PRLI) response handler
Robert Love3a3b42b2009-11-03 11:47:39 -0800936 * @sp: The sequence the PRLI response was on
937 * @fp: The PRLI response frame
938 * @rdata_arg: The remote port that sent the PRLI response
Robert Love42e9a922008-12-09 15:10:17 -0800939 *
940 * Locking Note: This function will be called without the rport lock
941 * held, but it will lock, call an _enter_* function or fc_rport_error
942 * and then unlock the rport.
943 */
944static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700945 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800946{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700947 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800948 struct {
949 struct fc_els_prli prli;
950 struct fc_els_spp spp;
951 } *pp;
952 u32 roles = FC_RPORT_ROLE_UNKNOWN;
953 u32 fcp_parm = 0;
954 u8 op;
Bhanu Prakash Gollapudi618461c2010-06-11 16:43:54 -0700955 u8 resp_code = 0;
Robert Love42e9a922008-12-09 15:10:17 -0800956
957 mutex_lock(&rdata->rp_mutex);
958
Joe Eykholtf657d292009-08-25 14:03:21 -0700959 FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800960
961 if (rdata->rp_state != RPORT_ST_PRLI) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700962 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
963 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700964 if (IS_ERR(fp))
965 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800966 goto out;
967 }
968
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700969 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700970 fc_rport_error_retry(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700971 goto err;
972 }
973
Robert Love6bd054c2009-08-25 14:03:04 -0700974 /* reinitialize remote port roles */
975 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
976
Robert Love42e9a922008-12-09 15:10:17 -0800977 op = fc_frame_payload_op(fp);
978 if (op == ELS_LS_ACC) {
979 pp = fc_frame_payload_get(fp, sizeof(*pp));
Bhanu Prakash Gollapudi618461c2010-06-11 16:43:54 -0700980 if (!pp)
981 goto out;
982
983 resp_code = (pp->spp.spp_flags & FC_SPP_RESP_MASK);
984 FC_RPORT_DBG(rdata, "PRLI spp_flags = 0x%x\n",
985 pp->spp.spp_flags);
986 if (resp_code != FC_SPP_RESP_ACK) {
987 if (resp_code == FC_SPP_RESP_CONF)
988 fc_rport_error(rdata, fp);
989 else
990 fc_rport_error_retry(rdata, fp);
991 goto out;
Robert Love42e9a922008-12-09 15:10:17 -0800992 }
Bhanu Prakash Gollapudi618461c2010-06-11 16:43:54 -0700993 if (pp->prli.prli_spp_len < sizeof(pp->spp))
994 goto out;
995
996 fcp_parm = ntohl(pp->spp.spp_params);
997 if (fcp_parm & FCP_SPPF_RETRY)
998 rdata->flags |= FC_RP_FLAGS_RETRY;
Robert Love42e9a922008-12-09 15:10:17 -0800999
Joe Eykholtf211fa52009-08-25 14:01:01 -07001000 rdata->supported_classes = FC_COS_CLASS3;
Robert Love42e9a922008-12-09 15:10:17 -08001001 if (fcp_parm & FCP_SPPF_INIT_FCN)
1002 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1003 if (fcp_parm & FCP_SPPF_TARG_FCN)
1004 roles |= FC_RPORT_ROLE_FCP_TARGET;
1005
Joe Eykholtf211fa52009-08-25 14:01:01 -07001006 rdata->ids.roles = roles;
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001007 fc_rport_enter_rtv(rdata);
Robert Love42e9a922008-12-09 15:10:17 -08001008
1009 } else {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001010 FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
Bhanu Prakash Gollapudi292e40b2010-06-11 16:43:49 -07001011 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001012 }
1013
1014out:
1015 fc_frame_free(fp);
1016err:
1017 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -07001018 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -08001019}
1020
1021/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001022 * fc_rport_enter_prli() - Send Process Login (PRLI) request
1023 * @rdata: The remote port to send the PRLI request to
Robert Love42e9a922008-12-09 15:10:17 -08001024 *
1025 * Locking Note: The rport lock is expected to be held before calling
1026 * this routine.
1027 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001028static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -08001029{
Robert Love42e9a922008-12-09 15:10:17 -08001030 struct fc_lport *lport = rdata->local_port;
1031 struct {
1032 struct fc_els_prli prli;
1033 struct fc_els_spp spp;
1034 } *pp;
1035 struct fc_frame *fp;
1036
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001037 /*
1038 * If the rport is one of the well known addresses
1039 * we skip PRLI and RTV and go straight to READY.
1040 */
1041 if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
1042 fc_rport_enter_ready(rdata);
1043 return;
1044 }
1045
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001046 FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
1047 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001048
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001049 fc_rport_state_enter(rdata, RPORT_ST_PRLI);
Robert Love42e9a922008-12-09 15:10:17 -08001050
1051 fp = fc_frame_alloc(lport, sizeof(*pp));
1052 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001053 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001054 return;
1055 }
1056
Joe Eykholtf211fa52009-08-25 14:01:01 -07001057 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PRLI,
Joe Eykholtb94f8952009-11-03 11:50:21 -08001058 fc_rport_prli_resp, rdata,
1059 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001060 fc_rport_error_retry(rdata, NULL);
Robert Love42e9a922008-12-09 15:10:17 -08001061 else
Joe Eykholtf211fa52009-08-25 14:01:01 -07001062 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -08001063}
1064
1065/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001066 * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses
1067 * @sp: The sequence the RTV was on
1068 * @fp: The RTV response frame
1069 * @rdata_arg: The remote port that sent the RTV response
Robert Love42e9a922008-12-09 15:10:17 -08001070 *
1071 * Many targets don't seem to support this.
1072 *
1073 * Locking Note: This function will be called without the rport lock
1074 * held, but it will lock, call an _enter_* function or fc_rport_error
1075 * and then unlock the rport.
1076 */
1077static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001078 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -08001079{
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001080 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -08001081 u8 op;
1082
1083 mutex_lock(&rdata->rp_mutex);
1084
Joe Eykholtf657d292009-08-25 14:03:21 -07001085 FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -08001086
1087 if (rdata->rp_state != RPORT_ST_RTV) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001088 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
1089 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001090 if (IS_ERR(fp))
1091 goto err;
Robert Love42e9a922008-12-09 15:10:17 -08001092 goto out;
1093 }
1094
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001095 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001096 fc_rport_error(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -07001097 goto err;
1098 }
1099
Robert Love42e9a922008-12-09 15:10:17 -08001100 op = fc_frame_payload_op(fp);
1101 if (op == ELS_LS_ACC) {
1102 struct fc_els_rtv_acc *rtv;
1103 u32 toq;
1104 u32 tov;
1105
1106 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
1107 if (rtv) {
1108 toq = ntohl(rtv->rtv_toq);
1109 tov = ntohl(rtv->rtv_r_a_tov);
1110 if (tov == 0)
1111 tov = 1;
1112 rdata->r_a_tov = tov;
1113 tov = ntohl(rtv->rtv_e_d_tov);
1114 if (toq & FC_ELS_RTV_EDRES)
1115 tov /= 1000000;
1116 if (tov == 0)
1117 tov = 1;
1118 rdata->e_d_tov = tov;
1119 }
1120 }
1121
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001122 fc_rport_enter_ready(rdata);
Robert Love42e9a922008-12-09 15:10:17 -08001123
1124out:
1125 fc_frame_free(fp);
1126err:
1127 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -07001128 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -08001129}
1130
1131/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001132 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
1133 * @rdata: The remote port to send the RTV request to
Robert Love42e9a922008-12-09 15:10:17 -08001134 *
1135 * Locking Note: The rport lock is expected to be held before calling
1136 * this routine.
1137 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001138static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -08001139{
1140 struct fc_frame *fp;
Robert Love42e9a922008-12-09 15:10:17 -08001141 struct fc_lport *lport = rdata->local_port;
1142
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001143 FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
1144 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001145
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001146 fc_rport_state_enter(rdata, RPORT_ST_RTV);
Robert Love42e9a922008-12-09 15:10:17 -08001147
1148 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
1149 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001150 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -08001151 return;
1152 }
1153
Joe Eykholtf211fa52009-08-25 14:01:01 -07001154 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
Joe Eykholtb94f8952009-11-03 11:50:21 -08001155 fc_rport_rtv_resp, rdata,
1156 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001157 fc_rport_error_retry(rdata, NULL);
Robert Love42e9a922008-12-09 15:10:17 -08001158 else
Joe Eykholtf211fa52009-08-25 14:01:01 -07001159 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -08001160}
1161
1162/**
Joe Eykholt079ecd82010-07-20 15:20:51 -07001163 * fc_rport_logo_resp() - Handler for logout (LOGO) responses
1164 * @sp: The sequence the LOGO was on
1165 * @fp: The LOGO response frame
1166 * @lport_arg: The local port
1167 */
1168static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
1169 void *lport_arg)
1170{
1171 struct fc_lport *lport = lport_arg;
1172
1173 FC_RPORT_ID_DBG(lport, fc_seq_exch(sp)->did,
1174 "Received a LOGO %s\n", fc_els_resp_type(fp));
1175 if (IS_ERR(fp))
1176 return;
1177 fc_frame_free(fp);
1178}
1179
1180/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001181 * fc_rport_enter_logo() - Send a logout (LOGO) request
1182 * @rdata: The remote port to send the LOGO request to
Robert Love42e9a922008-12-09 15:10:17 -08001183 *
1184 * Locking Note: The rport lock is expected to be held before calling
1185 * this routine.
1186 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001187static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -08001188{
Robert Love42e9a922008-12-09 15:10:17 -08001189 struct fc_lport *lport = rdata->local_port;
1190 struct fc_frame *fp;
1191
Joe Eykholt079ecd82010-07-20 15:20:51 -07001192 FC_RPORT_DBG(rdata, "Port sending LOGO from %s state\n",
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001193 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001194
Robert Love42e9a922008-12-09 15:10:17 -08001195 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
Joe Eykholt079ecd82010-07-20 15:20:51 -07001196 if (!fp)
Robert Love42e9a922008-12-09 15:10:17 -08001197 return;
Joe Eykholt079ecd82010-07-20 15:20:51 -07001198 (void)lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
1199 fc_rport_logo_resp, lport, 0);
Robert Love42e9a922008-12-09 15:10:17 -08001200}
1201
Robert Love42e9a922008-12-09 15:10:17 -08001202/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001203 * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
1204 * @sp: The sequence the ADISC response was on
1205 * @fp: The ADISC response frame
1206 * @rdata_arg: The remote port that sent the ADISC response
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001207 *
1208 * Locking Note: This function will be called without the rport lock
1209 * held, but it will lock, call an _enter_* function or fc_rport_error
1210 * and then unlock the rport.
1211 */
1212static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
Robert Love3a3b42b2009-11-03 11:47:39 -08001213 void *rdata_arg)
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001214{
1215 struct fc_rport_priv *rdata = rdata_arg;
1216 struct fc_els_adisc *adisc;
1217 u8 op;
1218
1219 mutex_lock(&rdata->rp_mutex);
1220
1221 FC_RPORT_DBG(rdata, "Received a ADISC response\n");
1222
1223 if (rdata->rp_state != RPORT_ST_ADISC) {
1224 FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
1225 fc_rport_state(rdata));
1226 if (IS_ERR(fp))
1227 goto err;
1228 goto out;
1229 }
1230
1231 if (IS_ERR(fp)) {
1232 fc_rport_error(rdata, fp);
1233 goto err;
1234 }
1235
1236 /*
1237 * If address verification failed. Consider us logged out of the rport.
1238 * Since the rport is still in discovery, we want to be
1239 * logged in, so go to PLOGI state. Otherwise, go back to READY.
1240 */
1241 op = fc_frame_payload_op(fp);
1242 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1243 if (op != ELS_LS_ACC || !adisc ||
1244 ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
1245 get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
1246 get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
1247 FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
Joe Eykholta7b12a22010-07-20 15:20:08 -07001248 fc_rport_enter_flogi(rdata);
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001249 } else {
1250 FC_RPORT_DBG(rdata, "ADISC OK\n");
1251 fc_rport_enter_ready(rdata);
1252 }
1253out:
1254 fc_frame_free(fp);
1255err:
1256 mutex_unlock(&rdata->rp_mutex);
1257 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1258}
1259
1260/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001261 * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
1262 * @rdata: The remote port to send the ADISC request to
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001263 *
1264 * Locking Note: The rport lock is expected to be held before calling
1265 * this routine.
1266 */
1267static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
1268{
1269 struct fc_lport *lport = rdata->local_port;
1270 struct fc_frame *fp;
1271
1272 FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
1273 fc_rport_state(rdata));
1274
1275 fc_rport_state_enter(rdata, RPORT_ST_ADISC);
1276
1277 fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
1278 if (!fp) {
1279 fc_rport_error_retry(rdata, fp);
1280 return;
1281 }
1282 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
Joe Eykholtb94f8952009-11-03 11:50:21 -08001283 fc_rport_adisc_resp, rdata,
1284 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001285 fc_rport_error_retry(rdata, NULL);
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001286 else
1287 kref_get(&rdata->kref);
1288}
1289
1290/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001291 * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
1292 * @rdata: The remote port that sent the ADISC request
Robert Love3a3b42b2009-11-03 11:47:39 -08001293 * @in_fp: The ADISC request frame
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001294 *
1295 * Locking Note: Called with the lport and rport locks held.
1296 */
1297static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
Joe Eykholt922611562010-07-20 15:21:12 -07001298 struct fc_frame *in_fp)
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001299{
1300 struct fc_lport *lport = rdata->local_port;
1301 struct fc_frame *fp;
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001302 struct fc_els_adisc *adisc;
1303 struct fc_seq_els_data rjt_data;
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001304
1305 FC_RPORT_DBG(rdata, "Received ADISC request\n");
1306
1307 adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
1308 if (!adisc) {
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001309 rjt_data.reason = ELS_RJT_PROT;
1310 rjt_data.explan = ELS_EXPL_INV_LEN;
Joe Eykholt922611562010-07-20 15:21:12 -07001311 lport->tt.seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data);
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001312 goto drop;
1313 }
1314
1315 fp = fc_frame_alloc(lport, sizeof(*adisc));
1316 if (!fp)
1317 goto drop;
1318 fc_adisc_fill(lport, fp);
1319 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1320 adisc->adisc_cmd = ELS_LS_ACC;
Joe Eykholt24f089e2010-07-20 15:21:01 -07001321 fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
1322 lport->tt.frame_send(lport, fp);
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001323drop:
1324 fc_frame_free(in_fp);
1325}
1326
1327/**
Yi Zou63e27fb2009-11-20 14:55:24 -08001328 * fc_rport_recv_rls_req() - Handle received Read Link Status request
1329 * @rdata: The remote port that sent the RLS request
Yi Zou63e27fb2009-11-20 14:55:24 -08001330 * @rx_fp: The PRLI request frame
1331 *
1332 * Locking Note: The rport lock is expected to be held before calling
1333 * this function.
1334 */
1335static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
Joe Eykholt922611562010-07-20 15:21:12 -07001336 struct fc_frame *rx_fp)
Yi Zou63e27fb2009-11-20 14:55:24 -08001337
1338{
1339 struct fc_lport *lport = rdata->local_port;
1340 struct fc_frame *fp;
Yi Zou63e27fb2009-11-20 14:55:24 -08001341 struct fc_els_rls *rls;
1342 struct fc_els_rls_resp *rsp;
1343 struct fc_els_lesb *lesb;
1344 struct fc_seq_els_data rjt_data;
1345 struct fc_host_statistics *hst;
Yi Zou63e27fb2009-11-20 14:55:24 -08001346
1347 FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
1348 fc_rport_state(rdata));
1349
1350 rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
1351 if (!rls) {
1352 rjt_data.reason = ELS_RJT_PROT;
1353 rjt_data.explan = ELS_EXPL_INV_LEN;
1354 goto out_rjt;
1355 }
1356
1357 fp = fc_frame_alloc(lport, sizeof(*rsp));
1358 if (!fp) {
1359 rjt_data.reason = ELS_RJT_UNAB;
1360 rjt_data.explan = ELS_EXPL_INSUF_RES;
1361 goto out_rjt;
1362 }
1363
1364 rsp = fc_frame_payload_get(fp, sizeof(*rsp));
1365 memset(rsp, 0, sizeof(*rsp));
1366 rsp->rls_cmd = ELS_LS_ACC;
1367 lesb = &rsp->rls_lesb;
1368 if (lport->tt.get_lesb) {
1369 /* get LESB from LLD if it supports it */
1370 lport->tt.get_lesb(lport, lesb);
1371 } else {
1372 fc_get_host_stats(lport->host);
1373 hst = &lport->host_stats;
1374 lesb->lesb_link_fail = htonl(hst->link_failure_count);
1375 lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
1376 lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
1377 lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
1378 lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
1379 lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
1380 }
1381
Joe Eykholt24f089e2010-07-20 15:21:01 -07001382 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1383 lport->tt.frame_send(lport, fp);
Yi Zou63e27fb2009-11-20 14:55:24 -08001384 goto out;
1385
1386out_rjt:
Joe Eykholt922611562010-07-20 15:21:12 -07001387 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
Yi Zou63e27fb2009-11-20 14:55:24 -08001388out:
1389 fc_frame_free(rx_fp);
1390}
1391
1392/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001393 * fc_rport_recv_els_req() - Handler for validated ELS requests
1394 * @lport: The local port that received the ELS request
Robert Love3a3b42b2009-11-03 11:47:39 -08001395 * @fp: The ELS request frame
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001396 *
1397 * Handle incoming ELS requests that require port login.
1398 * The ELS opcode has already been validated by the caller.
Robert Love42e9a922008-12-09 15:10:17 -08001399 *
Joe Eykholt131203a2009-08-25 14:03:10 -07001400 * Locking Note: Called with the lport lock held.
Robert Love42e9a922008-12-09 15:10:17 -08001401 */
Joe Eykholt922611562010-07-20 15:21:12 -07001402static void fc_rport_recv_els_req(struct fc_lport *lport, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -08001403{
Joe Eykholt131203a2009-08-25 14:03:10 -07001404 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -08001405 struct fc_seq_els_data els_data;
Robert Love42e9a922008-12-09 15:10:17 -08001406
Joe Eykholt25b37b92009-08-25 14:03:15 -07001407 mutex_lock(&lport->disc.disc_mutex);
Joe Eykholt251748a2010-07-20 15:20:56 -07001408 rdata = lport->tt.rport_lookup(lport, fc_frame_sid(fp));
Joe Eykholt131203a2009-08-25 14:03:10 -07001409 if (!rdata) {
Joe Eykholt25b37b92009-08-25 14:03:15 -07001410 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001411 goto reject;
Joe Eykholt131203a2009-08-25 14:03:10 -07001412 }
1413 mutex_lock(&rdata->rp_mutex);
Joe Eykholt25b37b92009-08-25 14:03:15 -07001414 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt131203a2009-08-25 14:03:10 -07001415
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001416 switch (rdata->rp_state) {
1417 case RPORT_ST_PRLI:
1418 case RPORT_ST_RTV:
1419 case RPORT_ST_READY:
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001420 case RPORT_ST_ADISC:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001421 break;
1422 default:
1423 mutex_unlock(&rdata->rp_mutex);
1424 goto reject;
1425 }
1426
1427 switch (fc_frame_payload_op(fp)) {
Joe Eykholt131203a2009-08-25 14:03:10 -07001428 case ELS_PRLI:
Joe Eykholt922611562010-07-20 15:21:12 -07001429 fc_rport_recv_prli_req(rdata, fp);
Joe Eykholt131203a2009-08-25 14:03:10 -07001430 break;
1431 case ELS_PRLO:
Joe Eykholt922611562010-07-20 15:21:12 -07001432 fc_rport_recv_prlo_req(rdata, fp);
Joe Eykholt131203a2009-08-25 14:03:10 -07001433 break;
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001434 case ELS_ADISC:
Joe Eykholt922611562010-07-20 15:21:12 -07001435 fc_rport_recv_adisc_req(rdata, fp);
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001436 break;
Joe Eykholt131203a2009-08-25 14:03:10 -07001437 case ELS_RRQ:
Joe Eykholt922611562010-07-20 15:21:12 -07001438 lport->tt.seq_els_rsp_send(fp, ELS_RRQ, NULL);
1439 fc_frame_free(fp);
Joe Eykholt131203a2009-08-25 14:03:10 -07001440 break;
1441 case ELS_REC:
Joe Eykholt922611562010-07-20 15:21:12 -07001442 lport->tt.seq_els_rsp_send(fp, ELS_REC, NULL);
1443 fc_frame_free(fp);
Joe Eykholt131203a2009-08-25 14:03:10 -07001444 break;
Yi Zou63e27fb2009-11-20 14:55:24 -08001445 case ELS_RLS:
Joe Eykholt922611562010-07-20 15:21:12 -07001446 fc_rport_recv_rls_req(rdata, fp);
Yi Zou63e27fb2009-11-20 14:55:24 -08001447 break;
Joe Eykholt131203a2009-08-25 14:03:10 -07001448 default:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001449 fc_frame_free(fp); /* can't happen */
Joe Eykholt131203a2009-08-25 14:03:10 -07001450 break;
Robert Love42e9a922008-12-09 15:10:17 -08001451 }
1452
1453 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001454 return;
1455
1456reject:
Joe Eykholt922611562010-07-20 15:21:12 -07001457 els_data.reason = ELS_RJT_UNAB;
1458 els_data.explan = ELS_EXPL_PLOGI_REQD;
1459 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001460 fc_frame_free(fp);
1461}
1462
1463/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001464 * fc_rport_recv_req() - Handler for requests
Robert Love3a3b42b2009-11-03 11:47:39 -08001465 * @lport: The local port that received the request
Joe Eykholt922611562010-07-20 15:21:12 -07001466 * @fp: The request frame
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001467 *
1468 * Locking Note: Called with the lport lock held.
1469 */
Joe Eykholt922611562010-07-20 15:21:12 -07001470void fc_rport_recv_req(struct fc_lport *lport, struct fc_frame *fp)
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001471{
1472 struct fc_seq_els_data els_data;
1473
1474 /*
Joe Eykholta7b12a22010-07-20 15:20:08 -07001475 * Handle FLOGI, PLOGI and LOGO requests separately, since they
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001476 * don't require prior login.
1477 * Check for unsupported opcodes first and reject them.
1478 * For some ops, it would be incorrect to reject with "PLOGI required".
1479 */
1480 switch (fc_frame_payload_op(fp)) {
Joe Eykholta7b12a22010-07-20 15:20:08 -07001481 case ELS_FLOGI:
Joe Eykholt922611562010-07-20 15:21:12 -07001482 fc_rport_recv_flogi_req(lport, fp);
Joe Eykholta7b12a22010-07-20 15:20:08 -07001483 break;
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001484 case ELS_PLOGI:
Joe Eykholt922611562010-07-20 15:21:12 -07001485 fc_rport_recv_plogi_req(lport, fp);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001486 break;
1487 case ELS_LOGO:
Joe Eykholt922611562010-07-20 15:21:12 -07001488 fc_rport_recv_logo_req(lport, fp);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001489 break;
1490 case ELS_PRLI:
1491 case ELS_PRLO:
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001492 case ELS_ADISC:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001493 case ELS_RRQ:
1494 case ELS_REC:
Yi Zou63e27fb2009-11-20 14:55:24 -08001495 case ELS_RLS:
Joe Eykholt922611562010-07-20 15:21:12 -07001496 fc_rport_recv_els_req(lport, fp);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001497 break;
1498 default:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001499 els_data.reason = ELS_RJT_UNSUP;
1500 els_data.explan = ELS_EXPL_NONE;
Joe Eykholt922611562010-07-20 15:21:12 -07001501 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1502 fc_frame_free(fp);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001503 break;
1504 }
Robert Love42e9a922008-12-09 15:10:17 -08001505}
1506
1507/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001508 * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
1509 * @lport: The local port that received the PLOGI request
Robert Love3a3b42b2009-11-03 11:47:39 -08001510 * @rx_fp: The PLOGI request frame
Robert Love42e9a922008-12-09 15:10:17 -08001511 *
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001512 * Locking Note: The rport lock is held before calling this function.
Robert Love42e9a922008-12-09 15:10:17 -08001513 */
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001514static void fc_rport_recv_plogi_req(struct fc_lport *lport,
Joe Eykholt922611562010-07-20 15:21:12 -07001515 struct fc_frame *rx_fp)
Robert Love42e9a922008-12-09 15:10:17 -08001516{
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001517 struct fc_disc *disc;
1518 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -08001519 struct fc_frame *fp = rx_fp;
Robert Love42e9a922008-12-09 15:10:17 -08001520 struct fc_els_flogi *pl;
1521 struct fc_seq_els_data rjt_data;
Joe Eykholt24f089e2010-07-20 15:21:01 -07001522 u32 sid;
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001523
Joe Eykholt251748a2010-07-20 15:20:56 -07001524 sid = fc_frame_sid(fp);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001525
1526 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
1527
Robert Love42e9a922008-12-09 15:10:17 -08001528 pl = fc_frame_payload_get(fp, sizeof(*pl));
1529 if (!pl) {
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001530 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
1531 rjt_data.reason = ELS_RJT_PROT;
1532 rjt_data.explan = ELS_EXPL_INV_LEN;
1533 goto reject;
Robert Love42e9a922008-12-09 15:10:17 -08001534 }
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001535
1536 disc = &lport->disc;
1537 mutex_lock(&disc->disc_mutex);
1538 rdata = lport->tt.rport_create(lport, sid);
1539 if (!rdata) {
1540 mutex_unlock(&disc->disc_mutex);
1541 rjt_data.reason = ELS_RJT_UNAB;
1542 rjt_data.explan = ELS_EXPL_INSUF_RES;
1543 goto reject;
1544 }
1545
1546 mutex_lock(&rdata->rp_mutex);
1547 mutex_unlock(&disc->disc_mutex);
1548
1549 rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1550 rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
Robert Love42e9a922008-12-09 15:10:17 -08001551
1552 /*
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001553 * If the rport was just created, possibly due to the incoming PLOGI,
Robert Love42e9a922008-12-09 15:10:17 -08001554 * set the state appropriately and accept the PLOGI.
1555 *
1556 * If we had also sent a PLOGI, and if the received PLOGI is from a
1557 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1558 * "command already in progress".
1559 *
1560 * XXX TBD: If the session was ready before, the PLOGI should result in
1561 * all outstanding exchanges being reset.
1562 */
1563 switch (rdata->rp_state) {
1564 case RPORT_ST_INIT:
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001565 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
Robert Love42e9a922008-12-09 15:10:17 -08001566 break;
Joe Eykholta7b12a22010-07-20 15:20:08 -07001567 case RPORT_ST_PLOGI_WAIT:
1568 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI_WAIT state\n");
1569 break;
Robert Love42e9a922008-12-09 15:10:17 -08001570 case RPORT_ST_PLOGI:
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001571 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1572 if (rdata->ids.port_name < lport->wwpn) {
1573 mutex_unlock(&rdata->rp_mutex);
1574 rjt_data.reason = ELS_RJT_INPROG;
1575 rjt_data.explan = ELS_EXPL_NONE;
1576 goto reject;
1577 }
Robert Love42e9a922008-12-09 15:10:17 -08001578 break;
1579 case RPORT_ST_PRLI:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001580 case RPORT_ST_RTV:
Robert Love42e9a922008-12-09 15:10:17 -08001581 case RPORT_ST_READY:
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001582 case RPORT_ST_ADISC:
1583 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
1584 "- ignored for now\n", rdata->rp_state);
1585 /* XXX TBD - should reset */
Robert Love42e9a922008-12-09 15:10:17 -08001586 break;
Joe Eykholta7b12a22010-07-20 15:20:08 -07001587 case RPORT_ST_FLOGI:
Joe Eykholt14194052009-07-29 17:04:43 -07001588 case RPORT_ST_DELETE:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001589 FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
1590 fc_rport_state(rdata));
1591 mutex_unlock(&rdata->rp_mutex);
1592 rjt_data.reason = ELS_RJT_BUSY;
1593 rjt_data.explan = ELS_EXPL_NONE;
1594 goto reject;
Robert Love42e9a922008-12-09 15:10:17 -08001595 }
1596
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001597 /*
1598 * Get session payload size from incoming PLOGI.
1599 */
1600 rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
Robert Love42e9a922008-12-09 15:10:17 -08001601
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001602 /*
1603 * Send LS_ACC. If this fails, the originator should retry.
1604 */
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001605 fp = fc_frame_alloc(lport, sizeof(*pl));
1606 if (!fp)
1607 goto out;
Robert Love42e9a922008-12-09 15:10:17 -08001608
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001609 fc_plogi_fill(lport, fp, ELS_LS_ACC);
Joe Eykholt24f089e2010-07-20 15:21:01 -07001610 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1611 lport->tt.frame_send(lport, fp);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001612 fc_rport_enter_prli(rdata);
1613out:
1614 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt24f089e2010-07-20 15:21:01 -07001615 fc_frame_free(rx_fp);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001616 return;
1617
1618reject:
Joe Eykholt922611562010-07-20 15:21:12 -07001619 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001620 fc_frame_free(fp);
Robert Love42e9a922008-12-09 15:10:17 -08001621}
1622
1623/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001624 * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
1625 * @rdata: The remote port that sent the PRLI request
Robert Love3a3b42b2009-11-03 11:47:39 -08001626 * @rx_fp: The PRLI request frame
Robert Love42e9a922008-12-09 15:10:17 -08001627 *
1628 * Locking Note: The rport lock is exected to be held before calling
1629 * this function.
1630 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001631static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
Joe Eykholt922611562010-07-20 15:21:12 -07001632 struct fc_frame *rx_fp)
Robert Love42e9a922008-12-09 15:10:17 -08001633{
Robert Love42e9a922008-12-09 15:10:17 -08001634 struct fc_lport *lport = rdata->local_port;
Robert Love42e9a922008-12-09 15:10:17 -08001635 struct fc_frame *fp;
Robert Love42e9a922008-12-09 15:10:17 -08001636 struct {
1637 struct fc_els_prli prli;
1638 struct fc_els_spp spp;
1639 } *pp;
1640 struct fc_els_spp *rspp; /* request service param page */
1641 struct fc_els_spp *spp; /* response spp */
1642 unsigned int len;
1643 unsigned int plen;
Robert Love42e9a922008-12-09 15:10:17 -08001644 enum fc_els_spp_resp resp;
1645 struct fc_seq_els_data rjt_data;
Robert Love42e9a922008-12-09 15:10:17 -08001646 u32 fcp_parm;
1647 u32 roles = FC_RPORT_ROLE_UNKNOWN;
Robert Love42e9a922008-12-09 15:10:17 -08001648
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001649 FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1650 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001651
Joe Eykholt251748a2010-07-20 15:20:56 -07001652 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
Robert Love42e9a922008-12-09 15:10:17 -08001653 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
Joe Eykholta2f6a022010-03-12 16:07:36 -08001654 if (!pp)
1655 goto reject_len;
1656 plen = ntohs(pp->prli.prli_len);
1657 if ((plen % 4) != 0 || plen > len || plen < 16)
1658 goto reject_len;
1659 if (plen < len)
1660 len = plen;
1661 plen = pp->prli.prli_spp_len;
1662 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1663 plen > len || len < sizeof(*pp) || plen < 12)
1664 goto reject_len;
1665 rspp = &pp->spp;
1666
1667 fp = fc_frame_alloc(lport, len);
1668 if (!fp) {
1669 rjt_data.reason = ELS_RJT_UNAB;
1670 rjt_data.explan = ELS_EXPL_INSUF_RES;
1671 goto reject;
Robert Love42e9a922008-12-09 15:10:17 -08001672 }
Joe Eykholta2f6a022010-03-12 16:07:36 -08001673 pp = fc_frame_payload_get(fp, len);
1674 WARN_ON(!pp);
1675 memset(pp, 0, len);
1676 pp->prli.prli_cmd = ELS_LS_ACC;
1677 pp->prli.prli_spp_len = plen;
1678 pp->prli.prli_len = htons(len);
1679 len -= sizeof(struct fc_els_prli);
Robert Love42e9a922008-12-09 15:10:17 -08001680
Joe Eykholta2f6a022010-03-12 16:07:36 -08001681 /* reinitialize remote port roles */
1682 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
Robert Love6bd054c2009-08-25 14:03:04 -07001683
Joe Eykholta2f6a022010-03-12 16:07:36 -08001684 /*
1685 * Go through all the service parameter pages and build
1686 * response. If plen indicates longer SPP than standard,
1687 * use that. The entire response has been pre-cleared above.
1688 */
1689 spp = &pp->spp;
1690 while (len >= plen) {
1691 spp->spp_type = rspp->spp_type;
1692 spp->spp_type_ext = rspp->spp_type_ext;
1693 spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
1694 resp = FC_SPP_RESP_ACK;
Robert Love42e9a922008-12-09 15:10:17 -08001695
Joe Eykholta2f6a022010-03-12 16:07:36 -08001696 switch (rspp->spp_type) {
1697 case 0: /* common to all FC-4 types */
Robert Love42e9a922008-12-09 15:10:17 -08001698 break;
Joe Eykholta2f6a022010-03-12 16:07:36 -08001699 case FC_TYPE_FCP:
1700 fcp_parm = ntohl(rspp->spp_params);
1701 if (fcp_parm & FCP_SPPF_RETRY)
1702 rdata->flags |= FC_RP_FLAGS_RETRY;
1703 rdata->supported_classes = FC_COS_CLASS3;
1704 if (fcp_parm & FCP_SPPF_INIT_FCN)
1705 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1706 if (fcp_parm & FCP_SPPF_TARG_FCN)
1707 roles |= FC_RPORT_ROLE_FCP_TARGET;
1708 rdata->ids.roles = roles;
1709
1710 spp->spp_params = htonl(lport->service_params);
Robert Love42e9a922008-12-09 15:10:17 -08001711 break;
1712 default:
Joe Eykholta2f6a022010-03-12 16:07:36 -08001713 resp = FC_SPP_RESP_INVL;
Robert Love42e9a922008-12-09 15:10:17 -08001714 break;
1715 }
Joe Eykholta2f6a022010-03-12 16:07:36 -08001716 spp->spp_flags |= resp;
1717 len -= plen;
1718 rspp = (struct fc_els_spp *)((char *)rspp + plen);
1719 spp = (struct fc_els_spp *)((char *)spp + plen);
Robert Love42e9a922008-12-09 15:10:17 -08001720 }
Joe Eykholta2f6a022010-03-12 16:07:36 -08001721
1722 /*
1723 * Send LS_ACC. If this fails, the originator should retry.
1724 */
Joe Eykholt24f089e2010-07-20 15:21:01 -07001725 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1726 lport->tt.frame_send(lport, fp);
Joe Eykholta2f6a022010-03-12 16:07:36 -08001727
1728 switch (rdata->rp_state) {
1729 case RPORT_ST_PRLI:
1730 fc_rport_enter_ready(rdata);
1731 break;
1732 default:
1733 break;
1734 }
1735 goto drop;
1736
1737reject_len:
1738 rjt_data.reason = ELS_RJT_PROT;
1739 rjt_data.explan = ELS_EXPL_INV_LEN;
1740reject:
Joe Eykholt922611562010-07-20 15:21:12 -07001741 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
Joe Eykholta2f6a022010-03-12 16:07:36 -08001742drop:
Robert Love42e9a922008-12-09 15:10:17 -08001743 fc_frame_free(rx_fp);
1744}
1745
1746/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001747 * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
1748 * @rdata: The remote port that sent the PRLO request
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001749 * @rx_fp: The PRLO request frame
Robert Love42e9a922008-12-09 15:10:17 -08001750 *
1751 * Locking Note: The rport lock is exected to be held before calling
1752 * this function.
1753 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001754static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001755 struct fc_frame *rx_fp)
Robert Love42e9a922008-12-09 15:10:17 -08001756{
Robert Love42e9a922008-12-09 15:10:17 -08001757 struct fc_lport *lport = rdata->local_port;
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001758 struct fc_frame *fp;
1759 struct {
1760 struct fc_els_prlo prlo;
1761 struct fc_els_spp spp;
1762 } *pp;
1763 struct fc_els_spp *rspp; /* request service param page */
1764 struct fc_els_spp *spp; /* response spp */
1765 unsigned int len;
1766 unsigned int plen;
Robert Love42e9a922008-12-09 15:10:17 -08001767 struct fc_seq_els_data rjt_data;
1768
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001769 FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
1770 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001771
Joe Eykholt251748a2010-07-20 15:20:56 -07001772 len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001773 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1774 if (!pp)
1775 goto reject_len;
1776 plen = ntohs(pp->prlo.prlo_len);
1777 if (plen != 20)
1778 goto reject_len;
1779 if (plen < len)
1780 len = plen;
1781
1782 rspp = &pp->spp;
1783
1784 fp = fc_frame_alloc(lport, len);
1785 if (!fp) {
1786 rjt_data.reason = ELS_RJT_UNAB;
1787 rjt_data.explan = ELS_EXPL_INSUF_RES;
1788 goto reject;
1789 }
1790
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001791 pp = fc_frame_payload_get(fp, len);
1792 WARN_ON(!pp);
1793 memset(pp, 0, len);
1794 pp->prlo.prlo_cmd = ELS_LS_ACC;
1795 pp->prlo.prlo_obs = 0x10;
1796 pp->prlo.prlo_len = htons(len);
1797 spp = &pp->spp;
1798 spp->spp_type = rspp->spp_type;
1799 spp->spp_type_ext = rspp->spp_type_ext;
1800 spp->spp_flags = FC_SPP_RESP_ACK;
1801
1802 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
1803
Joe Eykholt922611562010-07-20 15:21:12 -07001804 fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1805 lport->tt.frame_send(lport, fp);
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001806 goto drop;
1807
1808reject_len:
1809 rjt_data.reason = ELS_RJT_PROT;
1810 rjt_data.explan = ELS_EXPL_INV_LEN;
1811reject:
Joe Eykholt922611562010-07-20 15:21:12 -07001812 lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
Bhanu Prakash Gollapudif8fc6c22010-06-11 16:44:04 -07001813drop:
1814 fc_frame_free(rx_fp);
Robert Love42e9a922008-12-09 15:10:17 -08001815}
1816
1817/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001818 * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
1819 * @lport: The local port that received the LOGO request
Robert Love3a3b42b2009-11-03 11:47:39 -08001820 * @fp: The LOGO request frame
Robert Love42e9a922008-12-09 15:10:17 -08001821 *
1822 * Locking Note: The rport lock is exected to be held before calling
1823 * this function.
1824 */
Joe Eykholt922611562010-07-20 15:21:12 -07001825static void fc_rport_recv_logo_req(struct fc_lport *lport, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -08001826{
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001827 struct fc_rport_priv *rdata;
1828 u32 sid;
Robert Love42e9a922008-12-09 15:10:17 -08001829
Joe Eykholt922611562010-07-20 15:21:12 -07001830 lport->tt.seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
Joe Eykholtfeab4ae2009-08-25 14:03:36 -07001831
Joe Eykholt251748a2010-07-20 15:20:56 -07001832 sid = fc_frame_sid(fp);
Robert Love42e9a922008-12-09 15:10:17 -08001833
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001834 mutex_lock(&lport->disc.disc_mutex);
1835 rdata = lport->tt.rport_lookup(lport, sid);
1836 if (rdata) {
1837 mutex_lock(&rdata->rp_mutex);
1838 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
1839 fc_rport_state(rdata));
Joe Eykholtfeab4ae2009-08-25 14:03:36 -07001840
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001841 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001842 mutex_unlock(&rdata->rp_mutex);
1843 } else
1844 FC_RPORT_ID_DBG(lport, sid,
1845 "Received LOGO from non-logged-in port\n");
1846 mutex_unlock(&lport->disc.disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -08001847 fc_frame_free(fp);
1848}
1849
Robert Love3a3b42b2009-11-03 11:47:39 -08001850/**
1851 * fc_rport_flush_queue() - Flush the rport_event_queue
1852 */
Robert Love42e9a922008-12-09 15:10:17 -08001853static void fc_rport_flush_queue(void)
1854{
1855 flush_workqueue(rport_event_queue);
1856}
1857
Robert Love3a3b42b2009-11-03 11:47:39 -08001858/**
1859 * fc_rport_init() - Initialize the remote port layer for a local port
1860 * @lport: The local port to initialize the remote port layer for
1861 */
Robert Love42e9a922008-12-09 15:10:17 -08001862int fc_rport_init(struct fc_lport *lport)
1863{
Joe Eykholt8025b5d2009-08-25 14:02:06 -07001864 if (!lport->tt.rport_lookup)
1865 lport->tt.rport_lookup = fc_rport_lookup;
1866
Robert Love5101ff92009-02-27 10:55:18 -08001867 if (!lport->tt.rport_create)
Joe Eykholt9e9d0452009-08-25 14:01:18 -07001868 lport->tt.rport_create = fc_rport_create;
Robert Love5101ff92009-02-27 10:55:18 -08001869
Robert Love42e9a922008-12-09 15:10:17 -08001870 if (!lport->tt.rport_login)
1871 lport->tt.rport_login = fc_rport_login;
1872
1873 if (!lport->tt.rport_logoff)
1874 lport->tt.rport_logoff = fc_rport_logoff;
1875
1876 if (!lport->tt.rport_recv_req)
1877 lport->tt.rport_recv_req = fc_rport_recv_req;
1878
1879 if (!lport->tt.rport_flush_queue)
1880 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1881
Joe Eykholtf211fa52009-08-25 14:01:01 -07001882 if (!lport->tt.rport_destroy)
1883 lport->tt.rport_destroy = fc_rport_destroy;
1884
Robert Love42e9a922008-12-09 15:10:17 -08001885 return 0;
1886}
1887EXPORT_SYMBOL(fc_rport_init);
1888
Robert Love3a3b42b2009-11-03 11:47:39 -08001889/**
1890 * fc_setup_rport() - Initialize the rport_event_queue
1891 */
1892int fc_setup_rport()
Robert Love42e9a922008-12-09 15:10:17 -08001893{
1894 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
1895 if (!rport_event_queue)
1896 return -ENOMEM;
1897 return 0;
1898}
Robert Love42e9a922008-12-09 15:10:17 -08001899
Robert Love3a3b42b2009-11-03 11:47:39 -08001900/**
1901 * fc_destroy_rport() - Destroy the rport_event_queue
1902 */
1903void fc_destroy_rport()
Robert Love42e9a922008-12-09 15:10:17 -08001904{
1905 destroy_workqueue(rport_event_queue);
1906}
Robert Love42e9a922008-12-09 15:10:17 -08001907
Robert Love3a3b42b2009-11-03 11:47:39 -08001908/**
1909 * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
1910 * @rport: The remote port whose I/O should be terminated
1911 */
Robert Love42e9a922008-12-09 15:10:17 -08001912void fc_rport_terminate_io(struct fc_rport *rport)
1913{
Robert Love3a3b42b2009-11-03 11:47:39 -08001914 struct fc_rport_libfc_priv *rpriv = rport->dd_data;
1915 struct fc_lport *lport = rpriv->local_port;
Robert Love42e9a922008-12-09 15:10:17 -08001916
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -08001917 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
1918 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
Robert Love42e9a922008-12-09 15:10:17 -08001919}
1920EXPORT_SYMBOL(fc_rport_terminate_io);