blob: e33c5c7961a27ac93a450ded6cd0683f1e249225 [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
37 * more comments on the heirarchy.
38 *
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 Eykholt9fb9d322009-08-25 14:00:50 -070063static void fc_rport_enter_plogi(struct fc_rport_priv *);
64static void fc_rport_enter_prli(struct fc_rport_priv *);
65static void fc_rport_enter_rtv(struct fc_rport_priv *);
66static void fc_rport_enter_ready(struct fc_rport_priv *);
67static void fc_rport_enter_logo(struct fc_rport_priv *);
Joe Eykholt370c3bd2009-08-25 14:03:47 -070068static void fc_rport_enter_adisc(struct fc_rport_priv *);
Robert Love42e9a922008-12-09 15:10:17 -080069
Joe Eykholt3ac6f982009-08-25 14:03:26 -070070static void fc_rport_recv_plogi_req(struct fc_lport *,
Robert Love42e9a922008-12-09 15:10:17 -080071 struct fc_seq *, struct fc_frame *);
Joe Eykholt9fb9d322009-08-25 14:00:50 -070072static void fc_rport_recv_prli_req(struct fc_rport_priv *,
Robert Love42e9a922008-12-09 15:10:17 -080073 struct fc_seq *, struct fc_frame *);
Joe Eykholt9fb9d322009-08-25 14:00:50 -070074static void fc_rport_recv_prlo_req(struct fc_rport_priv *,
Robert Love42e9a922008-12-09 15:10:17 -080075 struct fc_seq *, struct fc_frame *);
Joe Eykholt83fe6a92009-08-25 14:03:31 -070076static void fc_rport_recv_logo_req(struct fc_lport *,
Robert Love42e9a922008-12-09 15:10:17 -080077 struct fc_seq *, struct fc_frame *);
78static void fc_rport_timeout(struct work_struct *);
Joe Eykholt9fb9d322009-08-25 14:00:50 -070079static void fc_rport_error(struct fc_rport_priv *, struct fc_frame *);
80static void fc_rport_error_retry(struct fc_rport_priv *, struct fc_frame *);
Robert Love42e9a922008-12-09 15:10:17 -080081static void fc_rport_work(struct work_struct *);
82
83static const char *fc_rport_state_names[] = {
Robert Love42e9a922008-12-09 15:10:17 -080084 [RPORT_ST_INIT] = "Init",
85 [RPORT_ST_PLOGI] = "PLOGI",
86 [RPORT_ST_PRLI] = "PRLI",
87 [RPORT_ST_RTV] = "RTV",
88 [RPORT_ST_READY] = "Ready",
89 [RPORT_ST_LOGO] = "LOGO",
Joe Eykholt370c3bd2009-08-25 14:03:47 -070090 [RPORT_ST_ADISC] = "ADISC",
Joe Eykholt14194052009-07-29 17:04:43 -070091 [RPORT_ST_DELETE] = "Delete",
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -070092 [RPORT_ST_RESTART] = "Restart",
Robert Love42e9a922008-12-09 15:10:17 -080093};
94
Joe Eykholt9e9d0452009-08-25 14:01:18 -070095/**
Robert Love3a3b42b2009-11-03 11:47:39 -080096 * fc_rport_lookup() - Lookup a remote port by port_id
97 * @lport: The local port to lookup the remote port on
98 * @port_id: The remote port ID to look up
Joe Eykholt8025b5d2009-08-25 14:02:06 -070099 */
100static struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport,
101 u32 port_id)
102{
103 struct fc_rport_priv *rdata;
104
105 list_for_each_entry(rdata, &lport->disc.rports, peers)
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700106 if (rdata->ids.port_id == port_id)
Joe Eykholt8025b5d2009-08-25 14:02:06 -0700107 return rdata;
108 return NULL;
109}
110
111/**
Robert Love9737e6a2009-08-25 14:02:59 -0700112 * fc_rport_create() - Create a new remote port
Robert Love3a3b42b2009-11-03 11:47:39 -0800113 * @lport: The local port this remote port will be associated with
114 * @ids: The identifiers for the new remote port
115 *
116 * The remote port will start in the INIT state.
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700117 *
Joe Eykholt48f00902009-08-25 14:01:50 -0700118 * Locking note: must be called with the disc_mutex held.
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700119 */
120static struct fc_rport_priv *fc_rport_create(struct fc_lport *lport,
Robert Love9737e6a2009-08-25 14:02:59 -0700121 u32 port_id)
Robert Love42e9a922008-12-09 15:10:17 -0800122{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700123 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800124
Robert Love9737e6a2009-08-25 14:02:59 -0700125 rdata = lport->tt.rport_lookup(lport, port_id);
Joe Eykholt19f97e32009-08-25 14:01:55 -0700126 if (rdata)
127 return rdata;
128
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700129 rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
130 if (!rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800131 return NULL;
132
Robert Love9737e6a2009-08-25 14:02:59 -0700133 rdata->ids.node_name = -1;
134 rdata->ids.port_name = -1;
135 rdata->ids.port_id = port_id;
136 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
137
Joe Eykholtf211fa52009-08-25 14:01:01 -0700138 kref_init(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800139 mutex_init(&rdata->rp_mutex);
Joe Eykholt795d86f2009-08-25 14:00:39 -0700140 rdata->local_port = lport;
Robert Love42e9a922008-12-09 15:10:17 -0800141 rdata->rp_state = RPORT_ST_INIT;
142 rdata->event = RPORT_EV_NONE;
143 rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
Joe Eykholt795d86f2009-08-25 14:00:39 -0700144 rdata->e_d_tov = lport->e_d_tov;
145 rdata->r_a_tov = lport->r_a_tov;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700146 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
Robert Love42e9a922008-12-09 15:10:17 -0800147 INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
148 INIT_WORK(&rdata->event_work, fc_rport_work);
Robert Love9737e6a2009-08-25 14:02:59 -0700149 if (port_id != FC_FID_DIR_SERV)
Joe Eykholt48f00902009-08-25 14:01:50 -0700150 list_add(&rdata->peers, &lport->disc.rports);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700151 return rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800152}
153
154/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800155 * fc_rport_destroy() - Free a remote port after last reference is released
156 * @kref: The remote port's kref
Joe Eykholtf211fa52009-08-25 14:01:01 -0700157 */
158static void fc_rport_destroy(struct kref *kref)
159{
160 struct fc_rport_priv *rdata;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700161
162 rdata = container_of(kref, struct fc_rport_priv, kref);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700163 kfree(rdata);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700164}
165
166/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800167 * fc_rport_state() - Return a string identifying the remote port's state
168 * @rdata: The remote port
Robert Love42e9a922008-12-09 15:10:17 -0800169 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700170static const char *fc_rport_state(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800171{
172 const char *cp;
Robert Love42e9a922008-12-09 15:10:17 -0800173
174 cp = fc_rport_state_names[rdata->rp_state];
175 if (!cp)
176 cp = "Unknown";
177 return cp;
178}
179
180/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800181 * fc_set_rport_loss_tmo() - Set the remote port loss timeout
182 * @rport: The remote port that gets a new timeout value
183 * @timeout: The new timeout value (in seconds)
Robert Love42e9a922008-12-09 15:10:17 -0800184 */
185void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
186{
187 if (timeout)
188 rport->dev_loss_tmo = timeout + 5;
189 else
190 rport->dev_loss_tmo = 30;
191}
192EXPORT_SYMBOL(fc_set_rport_loss_tmo);
193
194/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800195 * fc_plogi_get_maxframe() - Get the maximum payload from the common service
196 * parameters in a FLOGI frame
197 * @flp: The FLOGI payload
198 * @maxval: The maximum frame size upper limit; this may be less than what
199 * is in the service parameters
Robert Love42e9a922008-12-09 15:10:17 -0800200 */
Robert Loveb2ab99c2009-02-27 10:55:50 -0800201static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
202 unsigned int maxval)
Robert Love42e9a922008-12-09 15:10:17 -0800203{
204 unsigned int mfs;
205
206 /*
207 * Get max payload from the common service parameters and the
208 * class 3 receive data field size.
209 */
210 mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
211 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
212 maxval = mfs;
213 mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
214 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
215 maxval = mfs;
216 return maxval;
217}
218
219/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800220 * fc_rport_state_enter() - Change the state of a remote port
221 * @rdata: The remote port whose state should change
222 * @new: The new state
Robert Love42e9a922008-12-09 15:10:17 -0800223 *
224 * Locking Note: Called with the rport lock held
225 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700226static void fc_rport_state_enter(struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -0800227 enum fc_rport_state new)
228{
Robert Love42e9a922008-12-09 15:10:17 -0800229 if (rdata->rp_state != new)
230 rdata->retries = 0;
231 rdata->rp_state = new;
232}
233
Robert Love3a3b42b2009-11-03 11:47:39 -0800234/**
235 * fc_rport_work() - Handler for remote port events in the rport_event_queue
236 * @work: Handle to the remote port being dequeued
237 */
Robert Love42e9a922008-12-09 15:10:17 -0800238static void fc_rport_work(struct work_struct *work)
239{
Abhijeet Joglekar571f8242009-02-27 10:54:41 -0800240 u32 port_id;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700241 struct fc_rport_priv *rdata =
242 container_of(work, struct fc_rport_priv, event_work);
Robert Love3a3b42b2009-11-03 11:47:39 -0800243 struct fc_rport_libfc_priv *rpriv;
Robert Love42e9a922008-12-09 15:10:17 -0800244 enum fc_rport_event event;
Robert Love42e9a922008-12-09 15:10:17 -0800245 struct fc_lport *lport = rdata->local_port;
246 struct fc_rport_operations *rport_ops;
Joe Eykholt629f4422009-08-25 14:01:06 -0700247 struct fc_rport_identifiers ids;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700248 struct fc_rport *rport;
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700249 int restart = 0;
Robert Love42e9a922008-12-09 15:10:17 -0800250
251 mutex_lock(&rdata->rp_mutex);
252 event = rdata->event;
253 rport_ops = rdata->ops;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700254 rport = rdata->rport;
Robert Love42e9a922008-12-09 15:10:17 -0800255
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700256 FC_RPORT_DBG(rdata, "work event %u\n", event);
257
Joe Eykholt629f4422009-08-25 14:01:06 -0700258 switch (event) {
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700259 case RPORT_EV_READY:
Joe Eykholtf211fa52009-08-25 14:01:01 -0700260 ids = rdata->ids;
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700261 rdata->event = RPORT_EV_NONE;
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700262 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800263 mutex_unlock(&rdata->rp_mutex);
264
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700265 if (!rport)
266 rport = fc_remote_port_add(lport->host, 0, &ids);
267 if (!rport) {
268 FC_RPORT_DBG(rdata, "Failed to add the rport\n");
269 lport->tt.rport_logoff(rdata);
270 kref_put(&rdata->kref, lport->tt.rport_destroy);
271 return;
Robert Love42e9a922008-12-09 15:10:17 -0800272 }
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700273 mutex_lock(&rdata->rp_mutex);
274 if (rdata->rport)
275 FC_RPORT_DBG(rdata, "rport already allocated\n");
276 rdata->rport = rport;
277 rport->maxframe_size = rdata->maxframe_size;
278 rport->supported_classes = rdata->supported_classes;
279
Robert Love3a3b42b2009-11-03 11:47:39 -0800280 rpriv = rport->dd_data;
281 rpriv->local_port = lport;
282 rpriv->rp_state = rdata->rp_state;
283 rpriv->flags = rdata->flags;
284 rpriv->e_d_tov = rdata->e_d_tov;
285 rpriv->r_a_tov = rdata->r_a_tov;
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700286 mutex_unlock(&rdata->rp_mutex);
287
Joe Eykholt83455922009-08-25 14:02:01 -0700288 if (rport_ops && rport_ops->event_callback) {
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700289 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700290 rport_ops->event_callback(lport, rdata, event);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700291 }
292 kref_put(&rdata->kref, lport->tt.rport_destroy);
Joe Eykholt629f4422009-08-25 14:01:06 -0700293 break;
294
295 case RPORT_EV_FAILED:
296 case RPORT_EV_LOGO:
297 case RPORT_EV_STOP:
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700298 port_id = rdata->ids.port_id;
Robert Love42e9a922008-12-09 15:10:17 -0800299 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700300
Joe Eykholt48f00902009-08-25 14:01:50 -0700301 if (port_id != FC_FID_DIR_SERV) {
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700302 /*
303 * We must drop rp_mutex before taking disc_mutex.
304 * Re-evaluate state to allow for restart.
305 * A transition to RESTART state must only happen
306 * while disc_mutex is held and rdata is on the list.
307 */
Joe Eykholt48f00902009-08-25 14:01:50 -0700308 mutex_lock(&lport->disc.disc_mutex);
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700309 mutex_lock(&rdata->rp_mutex);
310 if (rdata->rp_state == RPORT_ST_RESTART)
311 restart = 1;
312 else
313 list_del(&rdata->peers);
Abhijeet Joglekar5543c722009-12-10 09:59:20 -0800314 rdata->event = RPORT_EV_NONE;
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700315 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt48f00902009-08-25 14:01:50 -0700316 mutex_unlock(&lport->disc.disc_mutex);
317 }
318
Joe Eykholt83455922009-08-25 14:02:01 -0700319 if (rport_ops && rport_ops->event_callback) {
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700320 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700321 rport_ops->event_callback(lport, rdata, event);
Abhijeet Joglekar571f8242009-02-27 10:54:41 -0800322 }
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700323 cancel_delayed_work_sync(&rdata->retry_work);
324
325 /*
326 * Reset any outstanding exchanges before freeing rport.
327 */
328 lport->tt.exch_mgr_reset(lport, 0, port_id);
329 lport->tt.exch_mgr_reset(lport, port_id, 0);
330
331 if (rport) {
Robert Love3a3b42b2009-11-03 11:47:39 -0800332 rpriv = rport->dd_data;
333 rpriv->rp_state = RPORT_ST_DELETE;
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700334 mutex_lock(&rdata->rp_mutex);
335 rdata->rport = NULL;
336 mutex_unlock(&rdata->rp_mutex);
337 fc_remote_port_delete(rport);
338 }
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700339 if (restart) {
340 mutex_lock(&rdata->rp_mutex);
341 FC_RPORT_DBG(rdata, "work restart\n");
342 fc_rport_enter_plogi(rdata);
343 mutex_unlock(&rdata->rp_mutex);
344 } else
345 kref_put(&rdata->kref, lport->tt.rport_destroy);
Joe Eykholt629f4422009-08-25 14:01:06 -0700346 break;
347
348 default:
Robert Love42e9a922008-12-09 15:10:17 -0800349 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt629f4422009-08-25 14:01:06 -0700350 break;
351 }
Robert Love42e9a922008-12-09 15:10:17 -0800352}
353
354/**
Robert Love34f42a02009-02-27 10:55:45 -0800355 * fc_rport_login() - Start the remote port login state machine
Robert Love3a3b42b2009-11-03 11:47:39 -0800356 * @rdata: The remote port to be logged in to
Robert Love42e9a922008-12-09 15:10:17 -0800357 *
358 * Locking Note: Called without the rport lock held. This
359 * function will hold the rport lock, call an _enter_*
360 * function and then unlock the rport.
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700361 *
362 * This indicates the intent to be logged into the remote port.
363 * If it appears we are already logged in, ADISC is used to verify
364 * the setup.
Robert Love42e9a922008-12-09 15:10:17 -0800365 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700366int fc_rport_login(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800367{
Robert Love42e9a922008-12-09 15:10:17 -0800368 mutex_lock(&rdata->rp_mutex);
369
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700370 switch (rdata->rp_state) {
371 case RPORT_ST_READY:
372 FC_RPORT_DBG(rdata, "ADISC port\n");
373 fc_rport_enter_adisc(rdata);
374 break;
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700375 case RPORT_ST_RESTART:
376 break;
377 case RPORT_ST_DELETE:
378 FC_RPORT_DBG(rdata, "Restart deleted port\n");
379 fc_rport_state_enter(rdata, RPORT_ST_RESTART);
380 break;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700381 default:
382 FC_RPORT_DBG(rdata, "Login to port\n");
383 fc_rport_enter_plogi(rdata);
384 break;
385 }
Robert Love42e9a922008-12-09 15:10:17 -0800386 mutex_unlock(&rdata->rp_mutex);
387
388 return 0;
389}
390
391/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800392 * fc_rport_enter_delete() - Schedule a remote port to be deleted
393 * @rdata: The remote port to be deleted
394 * @event: The event to report as the reason for deletion
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700395 *
396 * Locking Note: Called with the rport lock held.
397 *
398 * Allow state change into DELETE only once.
399 *
400 * Call queue_work only if there's no event already pending.
401 * Set the new event so that the old pending event will not occur.
402 * Since we have the mutex, even if fc_rport_work() is already started,
403 * it'll see the new event.
404 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700405static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700406 enum fc_rport_event event)
407{
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700408 if (rdata->rp_state == RPORT_ST_DELETE)
409 return;
410
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700411 FC_RPORT_DBG(rdata, "Delete port\n");
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700412
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700413 fc_rport_state_enter(rdata, RPORT_ST_DELETE);
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700414
415 if (rdata->event == RPORT_EV_NONE)
416 queue_work(rport_event_queue, &rdata->event_work);
417 rdata->event = event;
418}
419
420/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800421 * fc_rport_logoff() - Logoff and remove a remote port
422 * @rdata: The remote port to be logged off of
Robert Love42e9a922008-12-09 15:10:17 -0800423 *
424 * Locking Note: Called without the rport lock held. This
425 * function will hold the rport lock, call an _enter_*
426 * function and then unlock the rport.
427 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700428int fc_rport_logoff(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800429{
Robert Love42e9a922008-12-09 15:10:17 -0800430 mutex_lock(&rdata->rp_mutex);
431
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700432 FC_RPORT_DBG(rdata, "Remove port\n");
Robert Love42e9a922008-12-09 15:10:17 -0800433
Joe Eykholt14194052009-07-29 17:04:43 -0700434 if (rdata->rp_state == RPORT_ST_DELETE) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700435 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700436 goto out;
437 }
438
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700439 if (rdata->rp_state == RPORT_ST_RESTART)
440 FC_RPORT_DBG(rdata, "Port in Restart state, deleting\n");
441 else
442 fc_rport_enter_logo(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800443
444 /*
Joe Eykholt14194052009-07-29 17:04:43 -0700445 * Change the state to Delete so that we discard
Robert Love42e9a922008-12-09 15:10:17 -0800446 * the response.
447 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700448 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700449out:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700450 mutex_unlock(&rdata->rp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800451 return 0;
452}
453
454/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800455 * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state
456 * @rdata: The remote port that is ready
Robert Love42e9a922008-12-09 15:10:17 -0800457 *
458 * Locking Note: The rport lock is expected to be held before calling
459 * this routine.
460 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700461static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800462{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700463 fc_rport_state_enter(rdata, RPORT_ST_READY);
Robert Love42e9a922008-12-09 15:10:17 -0800464
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700465 FC_RPORT_DBG(rdata, "Port is Ready\n");
Robert Love42e9a922008-12-09 15:10:17 -0800466
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700467 if (rdata->event == RPORT_EV_NONE)
468 queue_work(rport_event_queue, &rdata->event_work);
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700469 rdata->event = RPORT_EV_READY;
Robert Love42e9a922008-12-09 15:10:17 -0800470}
471
472/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800473 * fc_rport_timeout() - Handler for the retry_work timer
474 * @work: Handle to the remote port that has timed out
Robert Love42e9a922008-12-09 15:10:17 -0800475 *
476 * Locking Note: Called without the rport lock held. This
477 * function will hold the rport lock, call an _enter_*
478 * function and then unlock the rport.
479 */
480static void fc_rport_timeout(struct work_struct *work)
481{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700482 struct fc_rport_priv *rdata =
483 container_of(work, struct fc_rport_priv, retry_work.work);
Robert Love42e9a922008-12-09 15:10:17 -0800484
485 mutex_lock(&rdata->rp_mutex);
486
487 switch (rdata->rp_state) {
488 case RPORT_ST_PLOGI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700489 fc_rport_enter_plogi(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800490 break;
491 case RPORT_ST_PRLI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700492 fc_rport_enter_prli(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800493 break;
494 case RPORT_ST_RTV:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700495 fc_rport_enter_rtv(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800496 break;
497 case RPORT_ST_LOGO:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700498 fc_rport_enter_logo(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800499 break;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700500 case RPORT_ST_ADISC:
501 fc_rport_enter_adisc(rdata);
502 break;
Robert Love42e9a922008-12-09 15:10:17 -0800503 case RPORT_ST_READY:
504 case RPORT_ST_INIT:
Joe Eykholt14194052009-07-29 17:04:43 -0700505 case RPORT_ST_DELETE:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700506 case RPORT_ST_RESTART:
Robert Love42e9a922008-12-09 15:10:17 -0800507 break;
508 }
509
510 mutex_unlock(&rdata->rp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800511}
512
513/**
Robert Love34f42a02009-02-27 10:55:45 -0800514 * fc_rport_error() - Error handler, called once retries have been exhausted
Robert Love3a3b42b2009-11-03 11:47:39 -0800515 * @rdata: The remote port the error is happened on
516 * @fp: The error code encapsulated in a frame pointer
Robert Love42e9a922008-12-09 15:10:17 -0800517 *
Robert Love42e9a922008-12-09 15:10:17 -0800518 * Locking Note: The rport lock is expected to be held before
519 * calling this routine
520 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700521static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -0800522{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700523 FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
Joe Eykholtcdbe6df2009-08-25 14:01:39 -0700524 IS_ERR(fp) ? -PTR_ERR(fp) : 0,
525 fc_rport_state(rdata), rdata->retries);
Robert Love42e9a922008-12-09 15:10:17 -0800526
Chris Leech6755db12009-02-27 10:55:02 -0800527 switch (rdata->rp_state) {
528 case RPORT_ST_PLOGI:
Chris Leech6755db12009-02-27 10:55:02 -0800529 case RPORT_ST_LOGO:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700530 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
Chris Leech6755db12009-02-27 10:55:02 -0800531 break;
532 case RPORT_ST_RTV:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700533 fc_rport_enter_ready(rdata);
Chris Leech6755db12009-02-27 10:55:02 -0800534 break;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700535 case RPORT_ST_PRLI:
536 case RPORT_ST_ADISC:
537 fc_rport_enter_logo(rdata);
538 break;
Joe Eykholt14194052009-07-29 17:04:43 -0700539 case RPORT_ST_DELETE:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700540 case RPORT_ST_RESTART:
Chris Leech6755db12009-02-27 10:55:02 -0800541 case RPORT_ST_READY:
542 case RPORT_ST_INIT:
543 break;
Robert Love42e9a922008-12-09 15:10:17 -0800544 }
545}
546
547/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800548 * fc_rport_error_retry() - Handler for remote port state retries
549 * @rdata: The remote port whose state is to be retried
550 * @fp: The error code encapsulated in a frame pointer
Chris Leech6755db12009-02-27 10:55:02 -0800551 *
552 * If the error was an exchange timeout retry immediately,
553 * otherwise wait for E_D_TOV.
554 *
555 * Locking Note: The rport lock is expected to be held before
556 * calling this routine
557 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700558static void fc_rport_error_retry(struct fc_rport_priv *rdata,
559 struct fc_frame *fp)
Chris Leech6755db12009-02-27 10:55:02 -0800560{
Chris Leech6755db12009-02-27 10:55:02 -0800561 unsigned long delay = FC_DEF_E_D_TOV;
562
563 /* make sure this isn't an FC_EX_CLOSED error, never retry those */
564 if (PTR_ERR(fp) == -FC_EX_CLOSED)
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700565 return fc_rport_error(rdata, fp);
Chris Leech6755db12009-02-27 10:55:02 -0800566
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700567 if (rdata->retries < rdata->local_port->max_rport_retry_count) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700568 FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
569 PTR_ERR(fp), fc_rport_state(rdata));
Chris Leech6755db12009-02-27 10:55:02 -0800570 rdata->retries++;
571 /* no additional delay on exchange timeouts */
572 if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
573 delay = 0;
Chris Leech6755db12009-02-27 10:55:02 -0800574 schedule_delayed_work(&rdata->retry_work, delay);
575 return;
576 }
577
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700578 return fc_rport_error(rdata, fp);
Chris Leech6755db12009-02-27 10:55:02 -0800579}
580
581/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800582 * fc_rport_plogi_recv_resp() - Handler for ELS PLOGI responses
583 * @sp: The sequence the PLOGI is on
584 * @fp: The PLOGI response frame
585 * @rdata_arg: The remote port that sent the PLOGI response
Robert Love42e9a922008-12-09 15:10:17 -0800586 *
587 * Locking Note: This function will be called without the rport lock
588 * held, but it will lock, call an _enter_* function or fc_rport_error
589 * and then unlock the rport.
590 */
591static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700592 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800593{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700594 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800595 struct fc_lport *lport = rdata->local_port;
Robert Lovea29e7642009-04-21 16:27:41 -0700596 struct fc_els_flogi *plp = NULL;
Robert Love42e9a922008-12-09 15:10:17 -0800597 unsigned int tov;
598 u16 csp_seq;
599 u16 cssp_seq;
600 u8 op;
601
602 mutex_lock(&rdata->rp_mutex);
603
Joe Eykholtf657d292009-08-25 14:03:21 -0700604 FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800605
606 if (rdata->rp_state != RPORT_ST_PLOGI) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700607 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
608 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700609 if (IS_ERR(fp))
610 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800611 goto out;
612 }
613
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700614 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700615 fc_rport_error_retry(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700616 goto err;
617 }
618
Robert Love42e9a922008-12-09 15:10:17 -0800619 op = fc_frame_payload_op(fp);
620 if (op == ELS_LS_ACC &&
621 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
Joe Eykholtf211fa52009-08-25 14:01:01 -0700622 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
623 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
Robert Love42e9a922008-12-09 15:10:17 -0800624
625 tov = ntohl(plp->fl_csp.sp_e_d_tov);
626 if (ntohs(plp->fl_csp.sp_features) & FC_SP_FT_EDTR)
Hugh Daschbach3b709152010-01-21 10:15:49 -0800627 tov /= 1000000;
Robert Love42e9a922008-12-09 15:10:17 -0800628 if (tov > rdata->e_d_tov)
629 rdata->e_d_tov = tov;
630 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
631 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
632 if (cssp_seq < csp_seq)
633 csp_seq = cssp_seq;
634 rdata->max_seq = csp_seq;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700635 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
Joe Eykholt3ac6f982009-08-25 14:03:26 -0700636 fc_rport_enter_prli(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800637 } else
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700638 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800639
640out:
641 fc_frame_free(fp);
642err:
643 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700644 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800645}
646
647/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800648 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
649 * @rdata: The remote port to send a PLOGI to
Robert Love42e9a922008-12-09 15:10:17 -0800650 *
651 * Locking Note: The rport lock is expected to be held before calling
652 * this routine.
653 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700654static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800655{
Robert Love42e9a922008-12-09 15:10:17 -0800656 struct fc_lport *lport = rdata->local_port;
657 struct fc_frame *fp;
658
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700659 FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
660 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800661
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700662 fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
Robert Love42e9a922008-12-09 15:10:17 -0800663
Joe Eykholtf211fa52009-08-25 14:01:01 -0700664 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
Robert Love42e9a922008-12-09 15:10:17 -0800665 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
666 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700667 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800668 return;
669 }
670 rdata->e_d_tov = lport->e_d_tov;
671
Joe Eykholtf211fa52009-08-25 14:01:01 -0700672 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
Joe Eykholtb94f8952009-11-03 11:50:21 -0800673 fc_rport_plogi_resp, rdata,
674 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -0700675 fc_rport_error_retry(rdata, NULL);
Robert Love42e9a922008-12-09 15:10:17 -0800676 else
Joe Eykholtf211fa52009-08-25 14:01:01 -0700677 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800678}
679
680/**
Robert Love34f42a02009-02-27 10:55:45 -0800681 * fc_rport_prli_resp() - Process Login (PRLI) response handler
Robert Love3a3b42b2009-11-03 11:47:39 -0800682 * @sp: The sequence the PRLI response was on
683 * @fp: The PRLI response frame
684 * @rdata_arg: The remote port that sent the PRLI response
Robert Love42e9a922008-12-09 15:10:17 -0800685 *
686 * Locking Note: This function will be called without the rport lock
687 * held, but it will lock, call an _enter_* function or fc_rport_error
688 * and then unlock the rport.
689 */
690static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700691 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800692{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700693 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800694 struct {
695 struct fc_els_prli prli;
696 struct fc_els_spp spp;
697 } *pp;
698 u32 roles = FC_RPORT_ROLE_UNKNOWN;
699 u32 fcp_parm = 0;
700 u8 op;
Bhanu Prakash Gollapudi618461c2010-06-11 16:43:54 -0700701 u8 resp_code = 0;
Robert Love42e9a922008-12-09 15:10:17 -0800702
703 mutex_lock(&rdata->rp_mutex);
704
Joe Eykholtf657d292009-08-25 14:03:21 -0700705 FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800706
707 if (rdata->rp_state != RPORT_ST_PRLI) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700708 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
709 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700710 if (IS_ERR(fp))
711 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800712 goto out;
713 }
714
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700715 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700716 fc_rport_error_retry(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700717 goto err;
718 }
719
Robert Love6bd054c2009-08-25 14:03:04 -0700720 /* reinitialize remote port roles */
721 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
722
Robert Love42e9a922008-12-09 15:10:17 -0800723 op = fc_frame_payload_op(fp);
724 if (op == ELS_LS_ACC) {
725 pp = fc_frame_payload_get(fp, sizeof(*pp));
Bhanu Prakash Gollapudi618461c2010-06-11 16:43:54 -0700726 if (!pp)
727 goto out;
728
729 resp_code = (pp->spp.spp_flags & FC_SPP_RESP_MASK);
730 FC_RPORT_DBG(rdata, "PRLI spp_flags = 0x%x\n",
731 pp->spp.spp_flags);
732 if (resp_code != FC_SPP_RESP_ACK) {
733 if (resp_code == FC_SPP_RESP_CONF)
734 fc_rport_error(rdata, fp);
735 else
736 fc_rport_error_retry(rdata, fp);
737 goto out;
Robert Love42e9a922008-12-09 15:10:17 -0800738 }
Bhanu Prakash Gollapudi618461c2010-06-11 16:43:54 -0700739 if (pp->prli.prli_spp_len < sizeof(pp->spp))
740 goto out;
741
742 fcp_parm = ntohl(pp->spp.spp_params);
743 if (fcp_parm & FCP_SPPF_RETRY)
744 rdata->flags |= FC_RP_FLAGS_RETRY;
Robert Love42e9a922008-12-09 15:10:17 -0800745
Joe Eykholtf211fa52009-08-25 14:01:01 -0700746 rdata->supported_classes = FC_COS_CLASS3;
Robert Love42e9a922008-12-09 15:10:17 -0800747 if (fcp_parm & FCP_SPPF_INIT_FCN)
748 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
749 if (fcp_parm & FCP_SPPF_TARG_FCN)
750 roles |= FC_RPORT_ROLE_FCP_TARGET;
751
Joe Eykholtf211fa52009-08-25 14:01:01 -0700752 rdata->ids.roles = roles;
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700753 fc_rport_enter_rtv(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800754
755 } else {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700756 FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
Bhanu Prakash Gollapudi292e40b2010-06-11 16:43:49 -0700757 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800758 }
759
760out:
761 fc_frame_free(fp);
762err:
763 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700764 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800765}
766
767/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800768 * fc_rport_logo_resp() - Handler for logout (LOGO) responses
769 * @sp: The sequence the LOGO was on
770 * @fp: The LOGO response frame
771 * @rdata_arg: The remote port that sent the LOGO response
Robert Love42e9a922008-12-09 15:10:17 -0800772 *
773 * Locking Note: This function will be called without the rport lock
774 * held, but it will lock, call an _enter_* function or fc_rport_error
775 * and then unlock the rport.
776 */
777static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700778 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800779{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700780 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800781 u8 op;
782
783 mutex_lock(&rdata->rp_mutex);
784
Joe Eykholtf657d292009-08-25 14:03:21 -0700785 FC_RPORT_DBG(rdata, "Received a LOGO %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800786
Robert Love42e9a922008-12-09 15:10:17 -0800787 if (rdata->rp_state != RPORT_ST_LOGO) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700788 FC_RPORT_DBG(rdata, "Received a LOGO response, but in state "
789 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700790 if (IS_ERR(fp))
791 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800792 goto out;
793 }
794
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700795 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700796 fc_rport_error_retry(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700797 goto err;
798 }
799
Robert Love42e9a922008-12-09 15:10:17 -0800800 op = fc_frame_payload_op(fp);
Joe Eykholt68a17502009-08-25 14:03:42 -0700801 if (op != ELS_LS_ACC)
802 FC_RPORT_DBG(rdata, "Bad ELS response op %x for LOGO command\n",
803 op);
804 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
Robert Love42e9a922008-12-09 15:10:17 -0800805
806out:
807 fc_frame_free(fp);
808err:
809 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700810 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800811}
812
813/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800814 * fc_rport_enter_prli() - Send Process Login (PRLI) request
815 * @rdata: The remote port to send the PRLI request to
Robert Love42e9a922008-12-09 15:10:17 -0800816 *
817 * Locking Note: The rport lock is expected to be held before calling
818 * this routine.
819 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700820static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800821{
Robert Love42e9a922008-12-09 15:10:17 -0800822 struct fc_lport *lport = rdata->local_port;
823 struct {
824 struct fc_els_prli prli;
825 struct fc_els_spp spp;
826 } *pp;
827 struct fc_frame *fp;
828
Joe Eykholt3ac6f982009-08-25 14:03:26 -0700829 /*
830 * If the rport is one of the well known addresses
831 * we skip PRLI and RTV and go straight to READY.
832 */
833 if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
834 fc_rport_enter_ready(rdata);
835 return;
836 }
837
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700838 FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
839 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800840
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700841 fc_rport_state_enter(rdata, RPORT_ST_PRLI);
Robert Love42e9a922008-12-09 15:10:17 -0800842
843 fp = fc_frame_alloc(lport, sizeof(*pp));
844 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700845 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800846 return;
847 }
848
Joe Eykholtf211fa52009-08-25 14:01:01 -0700849 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PRLI,
Joe Eykholtb94f8952009-11-03 11:50:21 -0800850 fc_rport_prli_resp, rdata,
851 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -0700852 fc_rport_error_retry(rdata, NULL);
Robert Love42e9a922008-12-09 15:10:17 -0800853 else
Joe Eykholtf211fa52009-08-25 14:01:01 -0700854 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800855}
856
857/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800858 * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses
859 * @sp: The sequence the RTV was on
860 * @fp: The RTV response frame
861 * @rdata_arg: The remote port that sent the RTV response
Robert Love42e9a922008-12-09 15:10:17 -0800862 *
863 * Many targets don't seem to support this.
864 *
865 * Locking Note: This function will be called without the rport lock
866 * held, but it will lock, call an _enter_* function or fc_rport_error
867 * and then unlock the rport.
868 */
869static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700870 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800871{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700872 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800873 u8 op;
874
875 mutex_lock(&rdata->rp_mutex);
876
Joe Eykholtf657d292009-08-25 14:03:21 -0700877 FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800878
879 if (rdata->rp_state != RPORT_ST_RTV) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700880 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
881 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700882 if (IS_ERR(fp))
883 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800884 goto out;
885 }
886
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700887 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700888 fc_rport_error(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700889 goto err;
890 }
891
Robert Love42e9a922008-12-09 15:10:17 -0800892 op = fc_frame_payload_op(fp);
893 if (op == ELS_LS_ACC) {
894 struct fc_els_rtv_acc *rtv;
895 u32 toq;
896 u32 tov;
897
898 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
899 if (rtv) {
900 toq = ntohl(rtv->rtv_toq);
901 tov = ntohl(rtv->rtv_r_a_tov);
902 if (tov == 0)
903 tov = 1;
904 rdata->r_a_tov = tov;
905 tov = ntohl(rtv->rtv_e_d_tov);
906 if (toq & FC_ELS_RTV_EDRES)
907 tov /= 1000000;
908 if (tov == 0)
909 tov = 1;
910 rdata->e_d_tov = tov;
911 }
912 }
913
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700914 fc_rport_enter_ready(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800915
916out:
917 fc_frame_free(fp);
918err:
919 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700920 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800921}
922
923/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800924 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
925 * @rdata: The remote port to send the RTV request to
Robert Love42e9a922008-12-09 15:10:17 -0800926 *
927 * Locking Note: The rport lock is expected to be held before calling
928 * this routine.
929 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700930static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800931{
932 struct fc_frame *fp;
Robert Love42e9a922008-12-09 15:10:17 -0800933 struct fc_lport *lport = rdata->local_port;
934
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700935 FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
936 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800937
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700938 fc_rport_state_enter(rdata, RPORT_ST_RTV);
Robert Love42e9a922008-12-09 15:10:17 -0800939
940 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
941 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700942 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800943 return;
944 }
945
Joe Eykholtf211fa52009-08-25 14:01:01 -0700946 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
Joe Eykholtb94f8952009-11-03 11:50:21 -0800947 fc_rport_rtv_resp, rdata,
948 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -0700949 fc_rport_error_retry(rdata, NULL);
Robert Love42e9a922008-12-09 15:10:17 -0800950 else
Joe Eykholtf211fa52009-08-25 14:01:01 -0700951 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800952}
953
954/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800955 * fc_rport_enter_logo() - Send a logout (LOGO) request
956 * @rdata: The remote port to send the LOGO request to
Robert Love42e9a922008-12-09 15:10:17 -0800957 *
958 * Locking Note: The rport lock is expected to be held before calling
959 * this routine.
960 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700961static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800962{
Robert Love42e9a922008-12-09 15:10:17 -0800963 struct fc_lport *lport = rdata->local_port;
964 struct fc_frame *fp;
965
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700966 FC_RPORT_DBG(rdata, "Port entered LOGO state from %s state\n",
967 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800968
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700969 fc_rport_state_enter(rdata, RPORT_ST_LOGO);
Robert Love42e9a922008-12-09 15:10:17 -0800970
971 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
972 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700973 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800974 return;
975 }
976
Joe Eykholtf211fa52009-08-25 14:01:01 -0700977 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
Joe Eykholtb94f8952009-11-03 11:50:21 -0800978 fc_rport_logo_resp, rdata,
979 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -0700980 fc_rport_error_retry(rdata, NULL);
Robert Love42e9a922008-12-09 15:10:17 -0800981 else
Joe Eykholtf211fa52009-08-25 14:01:01 -0700982 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800983}
984
Robert Love42e9a922008-12-09 15:10:17 -0800985/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800986 * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
987 * @sp: The sequence the ADISC response was on
988 * @fp: The ADISC response frame
989 * @rdata_arg: The remote port that sent the ADISC response
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700990 *
991 * Locking Note: This function will be called without the rport lock
992 * held, but it will lock, call an _enter_* function or fc_rport_error
993 * and then unlock the rport.
994 */
995static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
Robert Love3a3b42b2009-11-03 11:47:39 -0800996 void *rdata_arg)
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700997{
998 struct fc_rport_priv *rdata = rdata_arg;
999 struct fc_els_adisc *adisc;
1000 u8 op;
1001
1002 mutex_lock(&rdata->rp_mutex);
1003
1004 FC_RPORT_DBG(rdata, "Received a ADISC response\n");
1005
1006 if (rdata->rp_state != RPORT_ST_ADISC) {
1007 FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
1008 fc_rport_state(rdata));
1009 if (IS_ERR(fp))
1010 goto err;
1011 goto out;
1012 }
1013
1014 if (IS_ERR(fp)) {
1015 fc_rport_error(rdata, fp);
1016 goto err;
1017 }
1018
1019 /*
1020 * If address verification failed. Consider us logged out of the rport.
1021 * Since the rport is still in discovery, we want to be
1022 * logged in, so go to PLOGI state. Otherwise, go back to READY.
1023 */
1024 op = fc_frame_payload_op(fp);
1025 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1026 if (op != ELS_LS_ACC || !adisc ||
1027 ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
1028 get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
1029 get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
1030 FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
1031 fc_rport_enter_plogi(rdata);
1032 } else {
1033 FC_RPORT_DBG(rdata, "ADISC OK\n");
1034 fc_rport_enter_ready(rdata);
1035 }
1036out:
1037 fc_frame_free(fp);
1038err:
1039 mutex_unlock(&rdata->rp_mutex);
1040 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1041}
1042
1043/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001044 * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
1045 * @rdata: The remote port to send the ADISC request to
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001046 *
1047 * Locking Note: The rport lock is expected to be held before calling
1048 * this routine.
1049 */
1050static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
1051{
1052 struct fc_lport *lport = rdata->local_port;
1053 struct fc_frame *fp;
1054
1055 FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
1056 fc_rport_state(rdata));
1057
1058 fc_rport_state_enter(rdata, RPORT_ST_ADISC);
1059
1060 fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
1061 if (!fp) {
1062 fc_rport_error_retry(rdata, fp);
1063 return;
1064 }
1065 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
Joe Eykholtb94f8952009-11-03 11:50:21 -08001066 fc_rport_adisc_resp, rdata,
1067 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001068 fc_rport_error_retry(rdata, NULL);
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001069 else
1070 kref_get(&rdata->kref);
1071}
1072
1073/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001074 * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
1075 * @rdata: The remote port that sent the ADISC request
1076 * @sp: The sequence the ADISC request was on
1077 * @in_fp: The ADISC request frame
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001078 *
1079 * Locking Note: Called with the lport and rport locks held.
1080 */
1081static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
1082 struct fc_seq *sp, struct fc_frame *in_fp)
1083{
1084 struct fc_lport *lport = rdata->local_port;
1085 struct fc_frame *fp;
1086 struct fc_exch *ep = fc_seq_exch(sp);
1087 struct fc_els_adisc *adisc;
1088 struct fc_seq_els_data rjt_data;
1089 u32 f_ctl;
1090
1091 FC_RPORT_DBG(rdata, "Received ADISC request\n");
1092
1093 adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
1094 if (!adisc) {
1095 rjt_data.fp = NULL;
1096 rjt_data.reason = ELS_RJT_PROT;
1097 rjt_data.explan = ELS_EXPL_INV_LEN;
1098 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1099 goto drop;
1100 }
1101
1102 fp = fc_frame_alloc(lport, sizeof(*adisc));
1103 if (!fp)
1104 goto drop;
1105 fc_adisc_fill(lport, fp);
1106 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1107 adisc->adisc_cmd = ELS_LS_ACC;
1108 sp = lport->tt.seq_start_next(sp);
1109 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1110 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1111 FC_TYPE_ELS, f_ctl, 0);
1112 lport->tt.seq_send(lport, sp, fp);
1113drop:
1114 fc_frame_free(in_fp);
1115}
1116
1117/**
Yi Zou63e27fb2009-11-20 14:55:24 -08001118 * fc_rport_recv_rls_req() - Handle received Read Link Status request
1119 * @rdata: The remote port that sent the RLS request
1120 * @sp: The sequence that the RLS was on
1121 * @rx_fp: The PRLI request frame
1122 *
1123 * Locking Note: The rport lock is expected to be held before calling
1124 * this function.
1125 */
1126static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
1127 struct fc_seq *sp, struct fc_frame *rx_fp)
1128
1129{
1130 struct fc_lport *lport = rdata->local_port;
1131 struct fc_frame *fp;
1132 struct fc_exch *ep = fc_seq_exch(sp);
1133 struct fc_els_rls *rls;
1134 struct fc_els_rls_resp *rsp;
1135 struct fc_els_lesb *lesb;
1136 struct fc_seq_els_data rjt_data;
1137 struct fc_host_statistics *hst;
1138 u32 f_ctl;
1139
1140 FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
1141 fc_rport_state(rdata));
1142
1143 rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
1144 if (!rls) {
1145 rjt_data.reason = ELS_RJT_PROT;
1146 rjt_data.explan = ELS_EXPL_INV_LEN;
1147 goto out_rjt;
1148 }
1149
1150 fp = fc_frame_alloc(lport, sizeof(*rsp));
1151 if (!fp) {
1152 rjt_data.reason = ELS_RJT_UNAB;
1153 rjt_data.explan = ELS_EXPL_INSUF_RES;
1154 goto out_rjt;
1155 }
1156
1157 rsp = fc_frame_payload_get(fp, sizeof(*rsp));
1158 memset(rsp, 0, sizeof(*rsp));
1159 rsp->rls_cmd = ELS_LS_ACC;
1160 lesb = &rsp->rls_lesb;
1161 if (lport->tt.get_lesb) {
1162 /* get LESB from LLD if it supports it */
1163 lport->tt.get_lesb(lport, lesb);
1164 } else {
1165 fc_get_host_stats(lport->host);
1166 hst = &lport->host_stats;
1167 lesb->lesb_link_fail = htonl(hst->link_failure_count);
1168 lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
1169 lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
1170 lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
1171 lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
1172 lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
1173 }
1174
1175 sp = lport->tt.seq_start_next(sp);
1176 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
1177 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1178 FC_TYPE_ELS, f_ctl, 0);
1179 lport->tt.seq_send(lport, sp, fp);
1180 goto out;
1181
1182out_rjt:
1183 rjt_data.fp = NULL;
1184 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1185out:
1186 fc_frame_free(rx_fp);
1187}
1188
1189/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001190 * fc_rport_recv_els_req() - Handler for validated ELS requests
1191 * @lport: The local port that received the ELS request
1192 * @sp: The sequence that the ELS request was on
1193 * @fp: The ELS request frame
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001194 *
1195 * Handle incoming ELS requests that require port login.
1196 * The ELS opcode has already been validated by the caller.
Robert Love42e9a922008-12-09 15:10:17 -08001197 *
Joe Eykholt131203a2009-08-25 14:03:10 -07001198 * Locking Note: Called with the lport lock held.
Robert Love42e9a922008-12-09 15:10:17 -08001199 */
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001200static void fc_rport_recv_els_req(struct fc_lport *lport,
1201 struct fc_seq *sp, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -08001202{
Joe Eykholt131203a2009-08-25 14:03:10 -07001203 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -08001204 struct fc_frame_header *fh;
1205 struct fc_seq_els_data els_data;
Robert Love42e9a922008-12-09 15:10:17 -08001206
Robert Love42e9a922008-12-09 15:10:17 -08001207 els_data.fp = NULL;
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001208 els_data.reason = ELS_RJT_UNAB;
1209 els_data.explan = ELS_EXPL_PLOGI_REQD;
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001210
Robert Love42e9a922008-12-09 15:10:17 -08001211 fh = fc_frame_header_get(fp);
1212
Joe Eykholt25b37b92009-08-25 14:03:15 -07001213 mutex_lock(&lport->disc.disc_mutex);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001214 rdata = lport->tt.rport_lookup(lport, ntoh24(fh->fh_s_id));
Joe Eykholt131203a2009-08-25 14:03:10 -07001215 if (!rdata) {
Joe Eykholt25b37b92009-08-25 14:03:15 -07001216 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001217 goto reject;
Joe Eykholt131203a2009-08-25 14:03:10 -07001218 }
1219 mutex_lock(&rdata->rp_mutex);
Joe Eykholt25b37b92009-08-25 14:03:15 -07001220 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt131203a2009-08-25 14:03:10 -07001221
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001222 switch (rdata->rp_state) {
1223 case RPORT_ST_PRLI:
1224 case RPORT_ST_RTV:
1225 case RPORT_ST_READY:
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001226 case RPORT_ST_ADISC:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001227 break;
1228 default:
1229 mutex_unlock(&rdata->rp_mutex);
1230 goto reject;
1231 }
1232
1233 switch (fc_frame_payload_op(fp)) {
Joe Eykholt131203a2009-08-25 14:03:10 -07001234 case ELS_PRLI:
1235 fc_rport_recv_prli_req(rdata, sp, fp);
1236 break;
1237 case ELS_PRLO:
1238 fc_rport_recv_prlo_req(rdata, sp, fp);
1239 break;
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001240 case ELS_ADISC:
1241 fc_rport_recv_adisc_req(rdata, sp, fp);
1242 break;
Joe Eykholt131203a2009-08-25 14:03:10 -07001243 case ELS_RRQ:
1244 els_data.fp = fp;
1245 lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
1246 break;
1247 case ELS_REC:
1248 els_data.fp = fp;
1249 lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
1250 break;
Yi Zou63e27fb2009-11-20 14:55:24 -08001251 case ELS_RLS:
1252 fc_rport_recv_rls_req(rdata, sp, fp);
1253 break;
Joe Eykholt131203a2009-08-25 14:03:10 -07001254 default:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001255 fc_frame_free(fp); /* can't happen */
Joe Eykholt131203a2009-08-25 14:03:10 -07001256 break;
Robert Love42e9a922008-12-09 15:10:17 -08001257 }
1258
1259 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001260 return;
1261
1262reject:
1263 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
1264 fc_frame_free(fp);
1265}
1266
1267/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001268 * fc_rport_recv_req() - Handler for requests
1269 * @sp: The sequence the request was on
1270 * @fp: The request frame
1271 * @lport: The local port that received the request
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001272 *
1273 * Locking Note: Called with the lport lock held.
1274 */
1275void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
1276 struct fc_lport *lport)
1277{
1278 struct fc_seq_els_data els_data;
1279
1280 /*
1281 * Handle PLOGI and LOGO requests separately, since they
1282 * don't require prior login.
1283 * Check for unsupported opcodes first and reject them.
1284 * For some ops, it would be incorrect to reject with "PLOGI required".
1285 */
1286 switch (fc_frame_payload_op(fp)) {
1287 case ELS_PLOGI:
1288 fc_rport_recv_plogi_req(lport, sp, fp);
1289 break;
1290 case ELS_LOGO:
1291 fc_rport_recv_logo_req(lport, sp, fp);
1292 break;
1293 case ELS_PRLI:
1294 case ELS_PRLO:
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001295 case ELS_ADISC:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001296 case ELS_RRQ:
1297 case ELS_REC:
Yi Zou63e27fb2009-11-20 14:55:24 -08001298 case ELS_RLS:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001299 fc_rport_recv_els_req(lport, sp, fp);
1300 break;
1301 default:
1302 fc_frame_free(fp);
1303 els_data.fp = NULL;
1304 els_data.reason = ELS_RJT_UNSUP;
1305 els_data.explan = ELS_EXPL_NONE;
1306 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
1307 break;
1308 }
Robert Love42e9a922008-12-09 15:10:17 -08001309}
1310
1311/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001312 * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
1313 * @lport: The local port that received the PLOGI request
1314 * @sp: The sequence that the PLOGI request was on
1315 * @rx_fp: The PLOGI request frame
Robert Love42e9a922008-12-09 15:10:17 -08001316 *
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001317 * Locking Note: The rport lock is held before calling this function.
Robert Love42e9a922008-12-09 15:10:17 -08001318 */
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001319static void fc_rport_recv_plogi_req(struct fc_lport *lport,
Robert Love42e9a922008-12-09 15:10:17 -08001320 struct fc_seq *sp, struct fc_frame *rx_fp)
1321{
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001322 struct fc_disc *disc;
1323 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -08001324 struct fc_frame *fp = rx_fp;
1325 struct fc_exch *ep;
1326 struct fc_frame_header *fh;
1327 struct fc_els_flogi *pl;
1328 struct fc_seq_els_data rjt_data;
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001329 u32 sid, f_ctl;
1330
Robert Love42e9a922008-12-09 15:10:17 -08001331 rjt_data.fp = NULL;
Robert Love42e9a922008-12-09 15:10:17 -08001332 fh = fc_frame_header_get(fp);
Robert Love42e9a922008-12-09 15:10:17 -08001333 sid = ntoh24(fh->fh_s_id);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001334
1335 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
1336
Robert Love42e9a922008-12-09 15:10:17 -08001337 pl = fc_frame_payload_get(fp, sizeof(*pl));
1338 if (!pl) {
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001339 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
1340 rjt_data.reason = ELS_RJT_PROT;
1341 rjt_data.explan = ELS_EXPL_INV_LEN;
1342 goto reject;
Robert Love42e9a922008-12-09 15:10:17 -08001343 }
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001344
1345 disc = &lport->disc;
1346 mutex_lock(&disc->disc_mutex);
1347 rdata = lport->tt.rport_create(lport, sid);
1348 if (!rdata) {
1349 mutex_unlock(&disc->disc_mutex);
1350 rjt_data.reason = ELS_RJT_UNAB;
1351 rjt_data.explan = ELS_EXPL_INSUF_RES;
1352 goto reject;
1353 }
1354
1355 mutex_lock(&rdata->rp_mutex);
1356 mutex_unlock(&disc->disc_mutex);
1357
1358 rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1359 rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
Robert Love42e9a922008-12-09 15:10:17 -08001360
1361 /*
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001362 * If the rport was just created, possibly due to the incoming PLOGI,
Robert Love42e9a922008-12-09 15:10:17 -08001363 * set the state appropriately and accept the PLOGI.
1364 *
1365 * If we had also sent a PLOGI, and if the received PLOGI is from a
1366 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1367 * "command already in progress".
1368 *
1369 * XXX TBD: If the session was ready before, the PLOGI should result in
1370 * all outstanding exchanges being reset.
1371 */
1372 switch (rdata->rp_state) {
1373 case RPORT_ST_INIT:
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001374 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
Robert Love42e9a922008-12-09 15:10:17 -08001375 break;
1376 case RPORT_ST_PLOGI:
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001377 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1378 if (rdata->ids.port_name < lport->wwpn) {
1379 mutex_unlock(&rdata->rp_mutex);
1380 rjt_data.reason = ELS_RJT_INPROG;
1381 rjt_data.explan = ELS_EXPL_NONE;
1382 goto reject;
1383 }
Robert Love42e9a922008-12-09 15:10:17 -08001384 break;
1385 case RPORT_ST_PRLI:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001386 case RPORT_ST_RTV:
Robert Love42e9a922008-12-09 15:10:17 -08001387 case RPORT_ST_READY:
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001388 case RPORT_ST_ADISC:
1389 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
1390 "- ignored for now\n", rdata->rp_state);
1391 /* XXX TBD - should reset */
Robert Love42e9a922008-12-09 15:10:17 -08001392 break;
Joe Eykholt14194052009-07-29 17:04:43 -07001393 case RPORT_ST_DELETE:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001394 case RPORT_ST_LOGO:
1395 case RPORT_ST_RESTART:
1396 FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
1397 fc_rport_state(rdata));
1398 mutex_unlock(&rdata->rp_mutex);
1399 rjt_data.reason = ELS_RJT_BUSY;
1400 rjt_data.explan = ELS_EXPL_NONE;
1401 goto reject;
Robert Love42e9a922008-12-09 15:10:17 -08001402 }
1403
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001404 /*
1405 * Get session payload size from incoming PLOGI.
1406 */
1407 rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
1408 fc_frame_free(rx_fp);
Robert Love42e9a922008-12-09 15:10:17 -08001409
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001410 /*
1411 * Send LS_ACC. If this fails, the originator should retry.
1412 */
1413 sp = lport->tt.seq_start_next(sp);
1414 if (!sp)
1415 goto out;
1416 fp = fc_frame_alloc(lport, sizeof(*pl));
1417 if (!fp)
1418 goto out;
Robert Love42e9a922008-12-09 15:10:17 -08001419
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001420 fc_plogi_fill(lport, fp, ELS_LS_ACC);
1421 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1422 ep = fc_seq_exch(sp);
1423 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1424 FC_TYPE_ELS, f_ctl, 0);
1425 lport->tt.seq_send(lport, sp, fp);
1426 fc_rport_enter_prli(rdata);
1427out:
1428 mutex_unlock(&rdata->rp_mutex);
1429 return;
1430
1431reject:
1432 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1433 fc_frame_free(fp);
Robert Love42e9a922008-12-09 15:10:17 -08001434}
1435
1436/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001437 * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
1438 * @rdata: The remote port that sent the PRLI request
1439 * @sp: The sequence that the PRLI was on
1440 * @rx_fp: The PRLI request frame
Robert Love42e9a922008-12-09 15:10:17 -08001441 *
1442 * Locking Note: The rport lock is exected to be held before calling
1443 * this function.
1444 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001445static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -08001446 struct fc_seq *sp, struct fc_frame *rx_fp)
1447{
Robert Love42e9a922008-12-09 15:10:17 -08001448 struct fc_lport *lport = rdata->local_port;
1449 struct fc_exch *ep;
1450 struct fc_frame *fp;
1451 struct fc_frame_header *fh;
1452 struct {
1453 struct fc_els_prli prli;
1454 struct fc_els_spp spp;
1455 } *pp;
1456 struct fc_els_spp *rspp; /* request service param page */
1457 struct fc_els_spp *spp; /* response spp */
1458 unsigned int len;
1459 unsigned int plen;
Robert Love42e9a922008-12-09 15:10:17 -08001460 enum fc_els_spp_resp resp;
1461 struct fc_seq_els_data rjt_data;
1462 u32 f_ctl;
1463 u32 fcp_parm;
1464 u32 roles = FC_RPORT_ROLE_UNKNOWN;
Robert Love42e9a922008-12-09 15:10:17 -08001465
Joe Eykholta2f6a022010-03-12 16:07:36 -08001466 rjt_data.fp = NULL;
Robert Love42e9a922008-12-09 15:10:17 -08001467 fh = fc_frame_header_get(rx_fp);
1468
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001469 FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1470 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001471
Robert Love42e9a922008-12-09 15:10:17 -08001472 len = fr_len(rx_fp) - sizeof(*fh);
1473 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
Joe Eykholta2f6a022010-03-12 16:07:36 -08001474 if (!pp)
1475 goto reject_len;
1476 plen = ntohs(pp->prli.prli_len);
1477 if ((plen % 4) != 0 || plen > len || plen < 16)
1478 goto reject_len;
1479 if (plen < len)
1480 len = plen;
1481 plen = pp->prli.prli_spp_len;
1482 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1483 plen > len || len < sizeof(*pp) || plen < 12)
1484 goto reject_len;
1485 rspp = &pp->spp;
1486
1487 fp = fc_frame_alloc(lport, len);
1488 if (!fp) {
1489 rjt_data.reason = ELS_RJT_UNAB;
1490 rjt_data.explan = ELS_EXPL_INSUF_RES;
1491 goto reject;
Robert Love42e9a922008-12-09 15:10:17 -08001492 }
Joe Eykholta2f6a022010-03-12 16:07:36 -08001493 sp = lport->tt.seq_start_next(sp);
1494 WARN_ON(!sp);
1495 pp = fc_frame_payload_get(fp, len);
1496 WARN_ON(!pp);
1497 memset(pp, 0, len);
1498 pp->prli.prli_cmd = ELS_LS_ACC;
1499 pp->prli.prli_spp_len = plen;
1500 pp->prli.prli_len = htons(len);
1501 len -= sizeof(struct fc_els_prli);
Robert Love42e9a922008-12-09 15:10:17 -08001502
Joe Eykholta2f6a022010-03-12 16:07:36 -08001503 /* reinitialize remote port roles */
1504 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
Robert Love6bd054c2009-08-25 14:03:04 -07001505
Joe Eykholta2f6a022010-03-12 16:07:36 -08001506 /*
1507 * Go through all the service parameter pages and build
1508 * response. If plen indicates longer SPP than standard,
1509 * use that. The entire response has been pre-cleared above.
1510 */
1511 spp = &pp->spp;
1512 while (len >= plen) {
1513 spp->spp_type = rspp->spp_type;
1514 spp->spp_type_ext = rspp->spp_type_ext;
1515 spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
1516 resp = FC_SPP_RESP_ACK;
Robert Love42e9a922008-12-09 15:10:17 -08001517
Joe Eykholta2f6a022010-03-12 16:07:36 -08001518 switch (rspp->spp_type) {
1519 case 0: /* common to all FC-4 types */
Robert Love42e9a922008-12-09 15:10:17 -08001520 break;
Joe Eykholta2f6a022010-03-12 16:07:36 -08001521 case FC_TYPE_FCP:
1522 fcp_parm = ntohl(rspp->spp_params);
1523 if (fcp_parm & FCP_SPPF_RETRY)
1524 rdata->flags |= FC_RP_FLAGS_RETRY;
1525 rdata->supported_classes = FC_COS_CLASS3;
1526 if (fcp_parm & FCP_SPPF_INIT_FCN)
1527 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1528 if (fcp_parm & FCP_SPPF_TARG_FCN)
1529 roles |= FC_RPORT_ROLE_FCP_TARGET;
1530 rdata->ids.roles = roles;
1531
1532 spp->spp_params = htonl(lport->service_params);
Robert Love42e9a922008-12-09 15:10:17 -08001533 break;
1534 default:
Joe Eykholta2f6a022010-03-12 16:07:36 -08001535 resp = FC_SPP_RESP_INVL;
Robert Love42e9a922008-12-09 15:10:17 -08001536 break;
1537 }
Joe Eykholta2f6a022010-03-12 16:07:36 -08001538 spp->spp_flags |= resp;
1539 len -= plen;
1540 rspp = (struct fc_els_spp *)((char *)rspp + plen);
1541 spp = (struct fc_els_spp *)((char *)spp + plen);
Robert Love42e9a922008-12-09 15:10:17 -08001542 }
Joe Eykholta2f6a022010-03-12 16:07:36 -08001543
1544 /*
1545 * Send LS_ACC. If this fails, the originator should retry.
1546 */
1547 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1548 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1549 ep = fc_seq_exch(sp);
1550 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1551 FC_TYPE_ELS, f_ctl, 0);
1552 lport->tt.seq_send(lport, sp, fp);
1553
1554 switch (rdata->rp_state) {
1555 case RPORT_ST_PRLI:
1556 fc_rport_enter_ready(rdata);
1557 break;
1558 default:
1559 break;
1560 }
1561 goto drop;
1562
1563reject_len:
1564 rjt_data.reason = ELS_RJT_PROT;
1565 rjt_data.explan = ELS_EXPL_INV_LEN;
1566reject:
1567 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1568drop:
Robert Love42e9a922008-12-09 15:10:17 -08001569 fc_frame_free(rx_fp);
1570}
1571
1572/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001573 * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
1574 * @rdata: The remote port that sent the PRLO request
1575 * @sp: The sequence that the PRLO was on
1576 * @fp: The PRLO request frame
Robert Love42e9a922008-12-09 15:10:17 -08001577 *
1578 * Locking Note: The rport lock is exected to be held before calling
1579 * this function.
1580 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001581static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
1582 struct fc_seq *sp,
Robert Love42e9a922008-12-09 15:10:17 -08001583 struct fc_frame *fp)
1584{
Robert Love42e9a922008-12-09 15:10:17 -08001585 struct fc_lport *lport = rdata->local_port;
1586
1587 struct fc_frame_header *fh;
1588 struct fc_seq_els_data rjt_data;
1589
1590 fh = fc_frame_header_get(fp);
1591
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001592 FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
1593 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001594
1595 rjt_data.fp = NULL;
1596 rjt_data.reason = ELS_RJT_UNAB;
1597 rjt_data.explan = ELS_EXPL_NONE;
1598 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1599 fc_frame_free(fp);
1600}
1601
1602/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001603 * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
1604 * @lport: The local port that received the LOGO request
1605 * @sp: The sequence that the LOGO request was on
1606 * @fp: The LOGO request frame
Robert Love42e9a922008-12-09 15:10:17 -08001607 *
1608 * Locking Note: The rport lock is exected to be held before calling
1609 * this function.
1610 */
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001611static void fc_rport_recv_logo_req(struct fc_lport *lport,
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001612 struct fc_seq *sp,
Robert Love42e9a922008-12-09 15:10:17 -08001613 struct fc_frame *fp)
1614{
1615 struct fc_frame_header *fh;
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001616 struct fc_rport_priv *rdata;
1617 u32 sid;
Robert Love42e9a922008-12-09 15:10:17 -08001618
Joe Eykholtfeab4ae2009-08-25 14:03:36 -07001619 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
1620
Robert Love42e9a922008-12-09 15:10:17 -08001621 fh = fc_frame_header_get(fp);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001622 sid = ntoh24(fh->fh_s_id);
Robert Love42e9a922008-12-09 15:10:17 -08001623
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001624 mutex_lock(&lport->disc.disc_mutex);
1625 rdata = lport->tt.rport_lookup(lport, sid);
1626 if (rdata) {
1627 mutex_lock(&rdata->rp_mutex);
1628 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
1629 fc_rport_state(rdata));
Joe Eykholtfeab4ae2009-08-25 14:03:36 -07001630
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001631 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
1632
Joe Eykholtfeab4ae2009-08-25 14:03:36 -07001633 /*
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001634 * If the remote port was created due to discovery, set state
1635 * to log back in. It may have seen a stale RSCN about us.
Joe Eykholtfeab4ae2009-08-25 14:03:36 -07001636 */
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001637 if (rdata->disc_id)
1638 fc_rport_state_enter(rdata, RPORT_ST_RESTART);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001639 mutex_unlock(&rdata->rp_mutex);
1640 } else
1641 FC_RPORT_ID_DBG(lport, sid,
1642 "Received LOGO from non-logged-in port\n");
1643 mutex_unlock(&lport->disc.disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -08001644 fc_frame_free(fp);
1645}
1646
Robert Love3a3b42b2009-11-03 11:47:39 -08001647/**
1648 * fc_rport_flush_queue() - Flush the rport_event_queue
1649 */
Robert Love42e9a922008-12-09 15:10:17 -08001650static void fc_rport_flush_queue(void)
1651{
1652 flush_workqueue(rport_event_queue);
1653}
1654
Robert Love3a3b42b2009-11-03 11:47:39 -08001655/**
1656 * fc_rport_init() - Initialize the remote port layer for a local port
1657 * @lport: The local port to initialize the remote port layer for
1658 */
Robert Love42e9a922008-12-09 15:10:17 -08001659int fc_rport_init(struct fc_lport *lport)
1660{
Joe Eykholt8025b5d2009-08-25 14:02:06 -07001661 if (!lport->tt.rport_lookup)
1662 lport->tt.rport_lookup = fc_rport_lookup;
1663
Robert Love5101ff92009-02-27 10:55:18 -08001664 if (!lport->tt.rport_create)
Joe Eykholt9e9d0452009-08-25 14:01:18 -07001665 lport->tt.rport_create = fc_rport_create;
Robert Love5101ff92009-02-27 10:55:18 -08001666
Robert Love42e9a922008-12-09 15:10:17 -08001667 if (!lport->tt.rport_login)
1668 lport->tt.rport_login = fc_rport_login;
1669
1670 if (!lport->tt.rport_logoff)
1671 lport->tt.rport_logoff = fc_rport_logoff;
1672
1673 if (!lport->tt.rport_recv_req)
1674 lport->tt.rport_recv_req = fc_rport_recv_req;
1675
1676 if (!lport->tt.rport_flush_queue)
1677 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1678
Joe Eykholtf211fa52009-08-25 14:01:01 -07001679 if (!lport->tt.rport_destroy)
1680 lport->tt.rport_destroy = fc_rport_destroy;
1681
Robert Love42e9a922008-12-09 15:10:17 -08001682 return 0;
1683}
1684EXPORT_SYMBOL(fc_rport_init);
1685
Robert Love3a3b42b2009-11-03 11:47:39 -08001686/**
1687 * fc_setup_rport() - Initialize the rport_event_queue
1688 */
1689int fc_setup_rport()
Robert Love42e9a922008-12-09 15:10:17 -08001690{
1691 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
1692 if (!rport_event_queue)
1693 return -ENOMEM;
1694 return 0;
1695}
Robert Love42e9a922008-12-09 15:10:17 -08001696
Robert Love3a3b42b2009-11-03 11:47:39 -08001697/**
1698 * fc_destroy_rport() - Destroy the rport_event_queue
1699 */
1700void fc_destroy_rport()
Robert Love42e9a922008-12-09 15:10:17 -08001701{
1702 destroy_workqueue(rport_event_queue);
1703}
Robert Love42e9a922008-12-09 15:10:17 -08001704
Robert Love3a3b42b2009-11-03 11:47:39 -08001705/**
1706 * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
1707 * @rport: The remote port whose I/O should be terminated
1708 */
Robert Love42e9a922008-12-09 15:10:17 -08001709void fc_rport_terminate_io(struct fc_rport *rport)
1710{
Robert Love3a3b42b2009-11-03 11:47:39 -08001711 struct fc_rport_libfc_priv *rpriv = rport->dd_data;
1712 struct fc_lport *lport = rpriv->local_port;
Robert Love42e9a922008-12-09 15:10:17 -08001713
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -08001714 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
1715 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
Robert Love42e9a922008-12-09 15:10:17 -08001716}
1717EXPORT_SYMBOL(fc_rport_terminate_io);