blob: 97923bb07765e60cc43c121666c0e9b19a4734fb [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>
50#include <linux/rcupdate.h>
51#include <linux/timer.h>
52#include <linux/workqueue.h>
53#include <asm/unaligned.h>
54
55#include <scsi/libfc.h>
56#include <scsi/fc_encode.h>
57
Robert Love8866a5d2009-11-03 11:45:58 -080058#include "fc_libfc.h"
59
Robert Love42e9a922008-12-09 15:10:17 -080060struct workqueue_struct *rport_event_queue;
61
Joe Eykholt9fb9d322009-08-25 14:00:50 -070062static void fc_rport_enter_plogi(struct fc_rport_priv *);
63static void fc_rport_enter_prli(struct fc_rport_priv *);
64static void fc_rport_enter_rtv(struct fc_rport_priv *);
65static void fc_rport_enter_ready(struct fc_rport_priv *);
66static void fc_rport_enter_logo(struct fc_rport_priv *);
Joe Eykholt370c3bd2009-08-25 14:03:47 -070067static void fc_rport_enter_adisc(struct fc_rport_priv *);
Robert Love42e9a922008-12-09 15:10:17 -080068
Joe Eykholt3ac6f982009-08-25 14:03:26 -070069static void fc_rport_recv_plogi_req(struct fc_lport *,
Robert Love42e9a922008-12-09 15:10:17 -080070 struct fc_seq *, struct fc_frame *);
Joe Eykholt9fb9d322009-08-25 14:00:50 -070071static void fc_rport_recv_prli_req(struct fc_rport_priv *,
Robert Love42e9a922008-12-09 15:10:17 -080072 struct fc_seq *, struct fc_frame *);
Joe Eykholt9fb9d322009-08-25 14:00:50 -070073static void fc_rport_recv_prlo_req(struct fc_rport_priv *,
Robert Love42e9a922008-12-09 15:10:17 -080074 struct fc_seq *, struct fc_frame *);
Joe Eykholt83fe6a92009-08-25 14:03:31 -070075static void fc_rport_recv_logo_req(struct fc_lport *,
Robert Love42e9a922008-12-09 15:10:17 -080076 struct fc_seq *, struct fc_frame *);
77static void fc_rport_timeout(struct work_struct *);
Joe Eykholt9fb9d322009-08-25 14:00:50 -070078static void fc_rport_error(struct fc_rport_priv *, struct fc_frame *);
79static void fc_rport_error_retry(struct fc_rport_priv *, struct fc_frame *);
Robert Love42e9a922008-12-09 15:10:17 -080080static void fc_rport_work(struct work_struct *);
81
82static const char *fc_rport_state_names[] = {
Robert Love42e9a922008-12-09 15:10:17 -080083 [RPORT_ST_INIT] = "Init",
84 [RPORT_ST_PLOGI] = "PLOGI",
85 [RPORT_ST_PRLI] = "PRLI",
86 [RPORT_ST_RTV] = "RTV",
87 [RPORT_ST_READY] = "Ready",
88 [RPORT_ST_LOGO] = "LOGO",
Joe Eykholt370c3bd2009-08-25 14:03:47 -070089 [RPORT_ST_ADISC] = "ADISC",
Joe Eykholt14194052009-07-29 17:04:43 -070090 [RPORT_ST_DELETE] = "Delete",
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -070091 [RPORT_ST_RESTART] = "Restart",
Robert Love42e9a922008-12-09 15:10:17 -080092};
93
Joe Eykholt9e9d0452009-08-25 14:01:18 -070094/**
Robert Love3a3b42b2009-11-03 11:47:39 -080095 * fc_rport_lookup() - Lookup a remote port by port_id
96 * @lport: The local port to lookup the remote port on
97 * @port_id: The remote port ID to look up
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
104 list_for_each_entry(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 Eykholt9e9d0452009-08-25 14:01:18 -0700128 rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
129 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 Eykholt48f00902009-08-25 14:01:50 -0700149 list_add(&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/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800154 * fc_rport_destroy() - Free a remote port after last reference is released
155 * @kref: The remote port's kref
Joe Eykholtf211fa52009-08-25 14:01:01 -0700156 */
157static void fc_rport_destroy(struct kref *kref)
158{
159 struct fc_rport_priv *rdata;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700160
161 rdata = container_of(kref, struct fc_rport_priv, kref);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700162 kfree(rdata);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700163}
164
165/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800166 * fc_rport_state() - Return a string identifying the remote port's state
167 * @rdata: The remote port
Robert Love42e9a922008-12-09 15:10:17 -0800168 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700169static const char *fc_rport_state(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800170{
171 const char *cp;
Robert Love42e9a922008-12-09 15:10:17 -0800172
173 cp = fc_rport_state_names[rdata->rp_state];
174 if (!cp)
175 cp = "Unknown";
176 return cp;
177}
178
179/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800180 * fc_set_rport_loss_tmo() - Set the remote port loss timeout
181 * @rport: The remote port that gets a new timeout value
182 * @timeout: The new timeout value (in seconds)
Robert Love42e9a922008-12-09 15:10:17 -0800183 */
184void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
185{
186 if (timeout)
187 rport->dev_loss_tmo = timeout + 5;
188 else
189 rport->dev_loss_tmo = 30;
190}
191EXPORT_SYMBOL(fc_set_rport_loss_tmo);
192
193/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800194 * fc_plogi_get_maxframe() - Get the maximum payload from the common service
195 * parameters in a FLOGI frame
196 * @flp: The FLOGI payload
197 * @maxval: The maximum frame size upper limit; this may be less than what
198 * is in the service parameters
Robert Love42e9a922008-12-09 15:10:17 -0800199 */
Robert Loveb2ab99c2009-02-27 10:55:50 -0800200static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
201 unsigned int maxval)
Robert Love42e9a922008-12-09 15:10:17 -0800202{
203 unsigned int mfs;
204
205 /*
206 * Get max payload from the common service parameters and the
207 * class 3 receive data field size.
208 */
209 mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
210 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
211 maxval = mfs;
212 mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
213 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
214 maxval = mfs;
215 return maxval;
216}
217
218/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800219 * fc_rport_state_enter() - Change the state of a remote port
220 * @rdata: The remote port whose state should change
221 * @new: The new state
Robert Love42e9a922008-12-09 15:10:17 -0800222 *
223 * Locking Note: Called with the rport lock held
224 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700225static void fc_rport_state_enter(struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -0800226 enum fc_rport_state new)
227{
Robert Love42e9a922008-12-09 15:10:17 -0800228 if (rdata->rp_state != new)
229 rdata->retries = 0;
230 rdata->rp_state = new;
231}
232
Robert Love3a3b42b2009-11-03 11:47:39 -0800233/**
234 * fc_rport_work() - Handler for remote port events in the rport_event_queue
235 * @work: Handle to the remote port being dequeued
236 */
Robert Love42e9a922008-12-09 15:10:17 -0800237static void fc_rport_work(struct work_struct *work)
238{
Abhijeet Joglekar571f8242009-02-27 10:54:41 -0800239 u32 port_id;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700240 struct fc_rport_priv *rdata =
241 container_of(work, struct fc_rport_priv, event_work);
Robert Love3a3b42b2009-11-03 11:47:39 -0800242 struct fc_rport_libfc_priv *rpriv;
Robert Love42e9a922008-12-09 15:10:17 -0800243 enum fc_rport_event event;
Robert Love42e9a922008-12-09 15:10:17 -0800244 struct fc_lport *lport = rdata->local_port;
245 struct fc_rport_operations *rport_ops;
Joe Eykholt629f4422009-08-25 14:01:06 -0700246 struct fc_rport_identifiers ids;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700247 struct fc_rport *rport;
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700248 int restart = 0;
Robert Love42e9a922008-12-09 15:10:17 -0800249
250 mutex_lock(&rdata->rp_mutex);
251 event = rdata->event;
252 rport_ops = rdata->ops;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700253 rport = rdata->rport;
Robert Love42e9a922008-12-09 15:10:17 -0800254
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700255 FC_RPORT_DBG(rdata, "work event %u\n", event);
256
Joe Eykholt629f4422009-08-25 14:01:06 -0700257 switch (event) {
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700258 case RPORT_EV_READY:
Joe Eykholtf211fa52009-08-25 14:01:01 -0700259 ids = rdata->ids;
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700260 rdata->event = RPORT_EV_NONE;
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700261 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800262 mutex_unlock(&rdata->rp_mutex);
263
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700264 if (!rport)
265 rport = fc_remote_port_add(lport->host, 0, &ids);
266 if (!rport) {
267 FC_RPORT_DBG(rdata, "Failed to add the rport\n");
268 lport->tt.rport_logoff(rdata);
269 kref_put(&rdata->kref, lport->tt.rport_destroy);
270 return;
Robert Love42e9a922008-12-09 15:10:17 -0800271 }
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700272 mutex_lock(&rdata->rp_mutex);
273 if (rdata->rport)
274 FC_RPORT_DBG(rdata, "rport already allocated\n");
275 rdata->rport = rport;
276 rport->maxframe_size = rdata->maxframe_size;
277 rport->supported_classes = rdata->supported_classes;
278
Robert Love3a3b42b2009-11-03 11:47:39 -0800279 rpriv = rport->dd_data;
280 rpriv->local_port = lport;
281 rpriv->rp_state = rdata->rp_state;
282 rpriv->flags = rdata->flags;
283 rpriv->e_d_tov = rdata->e_d_tov;
284 rpriv->r_a_tov = rdata->r_a_tov;
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700285 mutex_unlock(&rdata->rp_mutex);
286
Joe Eykholt83455922009-08-25 14:02:01 -0700287 if (rport_ops && rport_ops->event_callback) {
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700288 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700289 rport_ops->event_callback(lport, rdata, event);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700290 }
291 kref_put(&rdata->kref, lport->tt.rport_destroy);
Joe Eykholt629f4422009-08-25 14:01:06 -0700292 break;
293
294 case RPORT_EV_FAILED:
295 case RPORT_EV_LOGO:
296 case RPORT_EV_STOP:
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700297 port_id = rdata->ids.port_id;
Robert Love42e9a922008-12-09 15:10:17 -0800298 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700299
Joe Eykholt48f00902009-08-25 14:01:50 -0700300 if (port_id != FC_FID_DIR_SERV) {
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700301 /*
302 * We must drop rp_mutex before taking disc_mutex.
303 * Re-evaluate state to allow for restart.
304 * A transition to RESTART state must only happen
305 * while disc_mutex is held and rdata is on the list.
306 */
Joe Eykholt48f00902009-08-25 14:01:50 -0700307 mutex_lock(&lport->disc.disc_mutex);
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700308 mutex_lock(&rdata->rp_mutex);
309 if (rdata->rp_state == RPORT_ST_RESTART)
310 restart = 1;
311 else
312 list_del(&rdata->peers);
Abhijeet Joglekar5543c722009-12-10 09:59:20 -0800313 rdata->event = RPORT_EV_NONE;
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700314 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt48f00902009-08-25 14:01:50 -0700315 mutex_unlock(&lport->disc.disc_mutex);
316 }
317
Joe Eykholt83455922009-08-25 14:02:01 -0700318 if (rport_ops && rport_ops->event_callback) {
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700319 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700320 rport_ops->event_callback(lport, rdata, event);
Abhijeet Joglekar571f8242009-02-27 10:54:41 -0800321 }
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700322 cancel_delayed_work_sync(&rdata->retry_work);
323
324 /*
325 * Reset any outstanding exchanges before freeing rport.
326 */
327 lport->tt.exch_mgr_reset(lport, 0, port_id);
328 lport->tt.exch_mgr_reset(lport, port_id, 0);
329
330 if (rport) {
Robert Love3a3b42b2009-11-03 11:47:39 -0800331 rpriv = rport->dd_data;
332 rpriv->rp_state = RPORT_ST_DELETE;
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700333 mutex_lock(&rdata->rp_mutex);
334 rdata->rport = NULL;
335 mutex_unlock(&rdata->rp_mutex);
336 fc_remote_port_delete(rport);
337 }
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700338 if (restart) {
339 mutex_lock(&rdata->rp_mutex);
340 FC_RPORT_DBG(rdata, "work restart\n");
341 fc_rport_enter_plogi(rdata);
342 mutex_unlock(&rdata->rp_mutex);
343 } else
344 kref_put(&rdata->kref, lport->tt.rport_destroy);
Joe Eykholt629f4422009-08-25 14:01:06 -0700345 break;
346
347 default:
Robert Love42e9a922008-12-09 15:10:17 -0800348 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt629f4422009-08-25 14:01:06 -0700349 break;
350 }
Robert Love42e9a922008-12-09 15:10:17 -0800351}
352
353/**
Robert Love34f42a02009-02-27 10:55:45 -0800354 * fc_rport_login() - Start the remote port login state machine
Robert Love3a3b42b2009-11-03 11:47:39 -0800355 * @rdata: The remote port to be logged in to
Robert Love42e9a922008-12-09 15:10:17 -0800356 *
357 * Locking Note: Called without the rport lock held. This
358 * function will hold the rport lock, call an _enter_*
359 * function and then unlock the rport.
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700360 *
361 * This indicates the intent to be logged into the remote port.
362 * If it appears we are already logged in, ADISC is used to verify
363 * the setup.
Robert Love42e9a922008-12-09 15:10:17 -0800364 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700365int fc_rport_login(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800366{
Robert Love42e9a922008-12-09 15:10:17 -0800367 mutex_lock(&rdata->rp_mutex);
368
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700369 switch (rdata->rp_state) {
370 case RPORT_ST_READY:
371 FC_RPORT_DBG(rdata, "ADISC port\n");
372 fc_rport_enter_adisc(rdata);
373 break;
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700374 case RPORT_ST_RESTART:
375 break;
376 case RPORT_ST_DELETE:
377 FC_RPORT_DBG(rdata, "Restart deleted port\n");
378 fc_rport_state_enter(rdata, RPORT_ST_RESTART);
379 break;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700380 default:
381 FC_RPORT_DBG(rdata, "Login to port\n");
382 fc_rport_enter_plogi(rdata);
383 break;
384 }
Robert Love42e9a922008-12-09 15:10:17 -0800385 mutex_unlock(&rdata->rp_mutex);
386
387 return 0;
388}
389
390/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800391 * fc_rport_enter_delete() - Schedule a remote port to be deleted
392 * @rdata: The remote port to be deleted
393 * @event: The event to report as the reason for deletion
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700394 *
395 * Locking Note: Called with the rport lock held.
396 *
397 * Allow state change into DELETE only once.
398 *
399 * Call queue_work only if there's no event already pending.
400 * Set the new event so that the old pending event will not occur.
401 * Since we have the mutex, even if fc_rport_work() is already started,
402 * it'll see the new event.
403 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700404static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700405 enum fc_rport_event event)
406{
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700407 if (rdata->rp_state == RPORT_ST_DELETE)
408 return;
409
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700410 FC_RPORT_DBG(rdata, "Delete port\n");
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700411
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700412 fc_rport_state_enter(rdata, RPORT_ST_DELETE);
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700413
414 if (rdata->event == RPORT_EV_NONE)
415 queue_work(rport_event_queue, &rdata->event_work);
416 rdata->event = event;
417}
418
419/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800420 * fc_rport_logoff() - Logoff and remove a remote port
421 * @rdata: The remote port to be logged off of
Robert Love42e9a922008-12-09 15:10:17 -0800422 *
423 * Locking Note: Called without the rport lock held. This
424 * function will hold the rport lock, call an _enter_*
425 * function and then unlock the rport.
426 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700427int fc_rport_logoff(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800428{
Robert Love42e9a922008-12-09 15:10:17 -0800429 mutex_lock(&rdata->rp_mutex);
430
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700431 FC_RPORT_DBG(rdata, "Remove port\n");
Robert Love42e9a922008-12-09 15:10:17 -0800432
Joe Eykholt14194052009-07-29 17:04:43 -0700433 if (rdata->rp_state == RPORT_ST_DELETE) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700434 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700435 goto out;
436 }
437
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700438 if (rdata->rp_state == RPORT_ST_RESTART)
439 FC_RPORT_DBG(rdata, "Port in Restart state, deleting\n");
440 else
441 fc_rport_enter_logo(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800442
443 /*
Joe Eykholt14194052009-07-29 17:04:43 -0700444 * Change the state to Delete so that we discard
Robert Love42e9a922008-12-09 15:10:17 -0800445 * the response.
446 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700447 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700448out:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700449 mutex_unlock(&rdata->rp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800450 return 0;
451}
452
453/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800454 * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state
455 * @rdata: The remote port that is ready
Robert Love42e9a922008-12-09 15:10:17 -0800456 *
457 * Locking Note: The rport lock is expected to be held before calling
458 * this routine.
459 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700460static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800461{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700462 fc_rport_state_enter(rdata, RPORT_ST_READY);
Robert Love42e9a922008-12-09 15:10:17 -0800463
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700464 FC_RPORT_DBG(rdata, "Port is Ready\n");
Robert Love42e9a922008-12-09 15:10:17 -0800465
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700466 if (rdata->event == RPORT_EV_NONE)
467 queue_work(rport_event_queue, &rdata->event_work);
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700468 rdata->event = RPORT_EV_READY;
Robert Love42e9a922008-12-09 15:10:17 -0800469}
470
471/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800472 * fc_rport_timeout() - Handler for the retry_work timer
473 * @work: Handle to the remote port that has timed out
Robert Love42e9a922008-12-09 15:10:17 -0800474 *
475 * Locking Note: Called without the rport lock held. This
476 * function will hold the rport lock, call an _enter_*
477 * function and then unlock the rport.
478 */
479static void fc_rport_timeout(struct work_struct *work)
480{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700481 struct fc_rport_priv *rdata =
482 container_of(work, struct fc_rport_priv, retry_work.work);
Robert Love42e9a922008-12-09 15:10:17 -0800483
484 mutex_lock(&rdata->rp_mutex);
485
486 switch (rdata->rp_state) {
487 case RPORT_ST_PLOGI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700488 fc_rport_enter_plogi(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800489 break;
490 case RPORT_ST_PRLI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700491 fc_rport_enter_prli(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800492 break;
493 case RPORT_ST_RTV:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700494 fc_rport_enter_rtv(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800495 break;
496 case RPORT_ST_LOGO:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700497 fc_rport_enter_logo(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800498 break;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700499 case RPORT_ST_ADISC:
500 fc_rport_enter_adisc(rdata);
501 break;
Robert Love42e9a922008-12-09 15:10:17 -0800502 case RPORT_ST_READY:
503 case RPORT_ST_INIT:
Joe Eykholt14194052009-07-29 17:04:43 -0700504 case RPORT_ST_DELETE:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700505 case RPORT_ST_RESTART:
Robert Love42e9a922008-12-09 15:10:17 -0800506 break;
507 }
508
509 mutex_unlock(&rdata->rp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800510}
511
512/**
Robert Love34f42a02009-02-27 10:55:45 -0800513 * fc_rport_error() - Error handler, called once retries have been exhausted
Robert Love3a3b42b2009-11-03 11:47:39 -0800514 * @rdata: The remote port the error is happened on
515 * @fp: The error code encapsulated in a frame pointer
Robert Love42e9a922008-12-09 15:10:17 -0800516 *
Robert Love42e9a922008-12-09 15:10:17 -0800517 * Locking Note: The rport lock is expected to be held before
518 * calling this routine
519 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700520static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -0800521{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700522 FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
Joe Eykholtcdbe6df2009-08-25 14:01:39 -0700523 IS_ERR(fp) ? -PTR_ERR(fp) : 0,
524 fc_rport_state(rdata), rdata->retries);
Robert Love42e9a922008-12-09 15:10:17 -0800525
Chris Leech6755db12009-02-27 10:55:02 -0800526 switch (rdata->rp_state) {
527 case RPORT_ST_PLOGI:
Chris Leech6755db12009-02-27 10:55:02 -0800528 case RPORT_ST_LOGO:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700529 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
Chris Leech6755db12009-02-27 10:55:02 -0800530 break;
531 case RPORT_ST_RTV:
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700532 fc_rport_enter_ready(rdata);
Chris Leech6755db12009-02-27 10:55:02 -0800533 break;
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700534 case RPORT_ST_PRLI:
535 case RPORT_ST_ADISC:
536 fc_rport_enter_logo(rdata);
537 break;
Joe Eykholt14194052009-07-29 17:04:43 -0700538 case RPORT_ST_DELETE:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -0700539 case RPORT_ST_RESTART:
Chris Leech6755db12009-02-27 10:55:02 -0800540 case RPORT_ST_READY:
541 case RPORT_ST_INIT:
542 break;
Robert Love42e9a922008-12-09 15:10:17 -0800543 }
544}
545
546/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800547 * fc_rport_error_retry() - Handler for remote port state retries
548 * @rdata: The remote port whose state is to be retried
549 * @fp: The error code encapsulated in a frame pointer
Chris Leech6755db12009-02-27 10:55:02 -0800550 *
551 * If the error was an exchange timeout retry immediately,
552 * otherwise wait for E_D_TOV.
553 *
554 * Locking Note: The rport lock is expected to be held before
555 * calling this routine
556 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700557static void fc_rport_error_retry(struct fc_rport_priv *rdata,
558 struct fc_frame *fp)
Chris Leech6755db12009-02-27 10:55:02 -0800559{
Chris Leech6755db12009-02-27 10:55:02 -0800560 unsigned long delay = FC_DEF_E_D_TOV;
561
562 /* make sure this isn't an FC_EX_CLOSED error, never retry those */
563 if (PTR_ERR(fp) == -FC_EX_CLOSED)
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700564 return fc_rport_error(rdata, fp);
Chris Leech6755db12009-02-27 10:55:02 -0800565
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700566 if (rdata->retries < rdata->local_port->max_rport_retry_count) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700567 FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
568 PTR_ERR(fp), fc_rport_state(rdata));
Chris Leech6755db12009-02-27 10:55:02 -0800569 rdata->retries++;
570 /* no additional delay on exchange timeouts */
571 if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
572 delay = 0;
Chris Leech6755db12009-02-27 10:55:02 -0800573 schedule_delayed_work(&rdata->retry_work, delay);
574 return;
575 }
576
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700577 return fc_rport_error(rdata, fp);
Chris Leech6755db12009-02-27 10:55:02 -0800578}
579
580/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800581 * fc_rport_plogi_recv_resp() - Handler for ELS PLOGI responses
582 * @sp: The sequence the PLOGI is on
583 * @fp: The PLOGI response frame
584 * @rdata_arg: The remote port that sent the PLOGI response
Robert Love42e9a922008-12-09 15:10:17 -0800585 *
586 * Locking Note: This function will be called without the rport lock
587 * held, but it will lock, call an _enter_* function or fc_rport_error
588 * and then unlock the rport.
589 */
590static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700591 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800592{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700593 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800594 struct fc_lport *lport = rdata->local_port;
Robert Lovea29e7642009-04-21 16:27:41 -0700595 struct fc_els_flogi *plp = NULL;
Robert Love42e9a922008-12-09 15:10:17 -0800596 unsigned int tov;
597 u16 csp_seq;
598 u16 cssp_seq;
599 u8 op;
600
601 mutex_lock(&rdata->rp_mutex);
602
Joe Eykholtf657d292009-08-25 14:03:21 -0700603 FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800604
605 if (rdata->rp_state != RPORT_ST_PLOGI) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700606 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
607 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700608 if (IS_ERR(fp))
609 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800610 goto out;
611 }
612
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700613 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700614 fc_rport_error_retry(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700615 goto err;
616 }
617
Robert Love42e9a922008-12-09 15:10:17 -0800618 op = fc_frame_payload_op(fp);
619 if (op == ELS_LS_ACC &&
620 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
Joe Eykholtf211fa52009-08-25 14:01:01 -0700621 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
622 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
Robert Love42e9a922008-12-09 15:10:17 -0800623
624 tov = ntohl(plp->fl_csp.sp_e_d_tov);
625 if (ntohs(plp->fl_csp.sp_features) & FC_SP_FT_EDTR)
Hugh Daschbach3b709152010-01-21 10:15:49 -0800626 tov /= 1000000;
Robert Love42e9a922008-12-09 15:10:17 -0800627 if (tov > rdata->e_d_tov)
628 rdata->e_d_tov = tov;
629 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
630 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
631 if (cssp_seq < csp_seq)
632 csp_seq = cssp_seq;
633 rdata->max_seq = csp_seq;
Joe Eykholtf211fa52009-08-25 14:01:01 -0700634 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
Joe Eykholt3ac6f982009-08-25 14:03:26 -0700635 fc_rport_enter_prli(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800636 } else
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700637 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800638
639out:
640 fc_frame_free(fp);
641err:
642 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700643 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800644}
645
646/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800647 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
648 * @rdata: The remote port to send a PLOGI to
Robert Love42e9a922008-12-09 15:10:17 -0800649 *
650 * Locking Note: The rport lock is expected to be held before calling
651 * this routine.
652 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700653static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800654{
Robert Love42e9a922008-12-09 15:10:17 -0800655 struct fc_lport *lport = rdata->local_port;
656 struct fc_frame *fp;
657
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700658 FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
659 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800660
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700661 fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
Robert Love42e9a922008-12-09 15:10:17 -0800662
Joe Eykholtf211fa52009-08-25 14:01:01 -0700663 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
Robert Love42e9a922008-12-09 15:10:17 -0800664 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
665 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700666 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800667 return;
668 }
669 rdata->e_d_tov = lport->e_d_tov;
670
Joe Eykholtf211fa52009-08-25 14:01:01 -0700671 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
Joe Eykholtb94f8952009-11-03 11:50:21 -0800672 fc_rport_plogi_resp, rdata,
673 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -0700674 fc_rport_error_retry(rdata, NULL);
Robert Love42e9a922008-12-09 15:10:17 -0800675 else
Joe Eykholtf211fa52009-08-25 14:01:01 -0700676 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800677}
678
679/**
Robert Love34f42a02009-02-27 10:55:45 -0800680 * fc_rport_prli_resp() - Process Login (PRLI) response handler
Robert Love3a3b42b2009-11-03 11:47:39 -0800681 * @sp: The sequence the PRLI response was on
682 * @fp: The PRLI response frame
683 * @rdata_arg: The remote port that sent the PRLI response
Robert Love42e9a922008-12-09 15:10:17 -0800684 *
685 * Locking Note: This function will be called without the rport lock
686 * held, but it will lock, call an _enter_* function or fc_rport_error
687 * and then unlock the rport.
688 */
689static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700690 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800691{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700692 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800693 struct {
694 struct fc_els_prli prli;
695 struct fc_els_spp spp;
696 } *pp;
697 u32 roles = FC_RPORT_ROLE_UNKNOWN;
698 u32 fcp_parm = 0;
699 u8 op;
700
701 mutex_lock(&rdata->rp_mutex);
702
Joe Eykholtf657d292009-08-25 14:03:21 -0700703 FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800704
705 if (rdata->rp_state != RPORT_ST_PRLI) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700706 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
707 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700708 if (IS_ERR(fp))
709 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800710 goto out;
711 }
712
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700713 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700714 fc_rport_error_retry(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700715 goto err;
716 }
717
Robert Love6bd054c2009-08-25 14:03:04 -0700718 /* reinitialize remote port roles */
719 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
720
Robert Love42e9a922008-12-09 15:10:17 -0800721 op = fc_frame_payload_op(fp);
722 if (op == ELS_LS_ACC) {
723 pp = fc_frame_payload_get(fp, sizeof(*pp));
724 if (pp && pp->prli.prli_spp_len >= sizeof(pp->spp)) {
725 fcp_parm = ntohl(pp->spp.spp_params);
726 if (fcp_parm & FCP_SPPF_RETRY)
727 rdata->flags |= FC_RP_FLAGS_RETRY;
728 }
729
Joe Eykholtf211fa52009-08-25 14:01:01 -0700730 rdata->supported_classes = FC_COS_CLASS3;
Robert Love42e9a922008-12-09 15:10:17 -0800731 if (fcp_parm & FCP_SPPF_INIT_FCN)
732 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
733 if (fcp_parm & FCP_SPPF_TARG_FCN)
734 roles |= FC_RPORT_ROLE_FCP_TARGET;
735
Joe Eykholtf211fa52009-08-25 14:01:01 -0700736 rdata->ids.roles = roles;
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700737 fc_rport_enter_rtv(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800738
739 } else {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700740 FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
741 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
Robert Love42e9a922008-12-09 15:10:17 -0800742 }
743
744out:
745 fc_frame_free(fp);
746err:
747 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700748 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800749}
750
751/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800752 * fc_rport_logo_resp() - Handler for logout (LOGO) responses
753 * @sp: The sequence the LOGO was on
754 * @fp: The LOGO response frame
755 * @rdata_arg: The remote port that sent the LOGO response
Robert Love42e9a922008-12-09 15:10:17 -0800756 *
757 * Locking Note: This function will be called without the rport lock
758 * held, but it will lock, call an _enter_* function or fc_rport_error
759 * and then unlock the rport.
760 */
761static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700762 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800763{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700764 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800765 u8 op;
766
767 mutex_lock(&rdata->rp_mutex);
768
Joe Eykholtf657d292009-08-25 14:03:21 -0700769 FC_RPORT_DBG(rdata, "Received a LOGO %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800770
Robert Love42e9a922008-12-09 15:10:17 -0800771 if (rdata->rp_state != RPORT_ST_LOGO) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700772 FC_RPORT_DBG(rdata, "Received a LOGO response, but in state "
773 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700774 if (IS_ERR(fp))
775 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800776 goto out;
777 }
778
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700779 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700780 fc_rport_error_retry(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700781 goto err;
782 }
783
Robert Love42e9a922008-12-09 15:10:17 -0800784 op = fc_frame_payload_op(fp);
Joe Eykholt68a17502009-08-25 14:03:42 -0700785 if (op != ELS_LS_ACC)
786 FC_RPORT_DBG(rdata, "Bad ELS response op %x for LOGO command\n",
787 op);
788 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
Robert Love42e9a922008-12-09 15:10:17 -0800789
790out:
791 fc_frame_free(fp);
792err:
793 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700794 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800795}
796
797/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800798 * fc_rport_enter_prli() - Send Process Login (PRLI) request
799 * @rdata: The remote port to send the PRLI request to
Robert Love42e9a922008-12-09 15:10:17 -0800800 *
801 * Locking Note: The rport lock is expected to be held before calling
802 * this routine.
803 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700804static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800805{
Robert Love42e9a922008-12-09 15:10:17 -0800806 struct fc_lport *lport = rdata->local_port;
807 struct {
808 struct fc_els_prli prli;
809 struct fc_els_spp spp;
810 } *pp;
811 struct fc_frame *fp;
812
Joe Eykholt3ac6f982009-08-25 14:03:26 -0700813 /*
814 * If the rport is one of the well known addresses
815 * we skip PRLI and RTV and go straight to READY.
816 */
817 if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
818 fc_rport_enter_ready(rdata);
819 return;
820 }
821
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700822 FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
823 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800824
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700825 fc_rport_state_enter(rdata, RPORT_ST_PRLI);
Robert Love42e9a922008-12-09 15:10:17 -0800826
827 fp = fc_frame_alloc(lport, sizeof(*pp));
828 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700829 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800830 return;
831 }
832
Joe Eykholtf211fa52009-08-25 14:01:01 -0700833 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PRLI,
Joe Eykholtb94f8952009-11-03 11:50:21 -0800834 fc_rport_prli_resp, rdata,
835 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -0700836 fc_rport_error_retry(rdata, NULL);
Robert Love42e9a922008-12-09 15:10:17 -0800837 else
Joe Eykholtf211fa52009-08-25 14:01:01 -0700838 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800839}
840
841/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800842 * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses
843 * @sp: The sequence the RTV was on
844 * @fp: The RTV response frame
845 * @rdata_arg: The remote port that sent the RTV response
Robert Love42e9a922008-12-09 15:10:17 -0800846 *
847 * Many targets don't seem to support this.
848 *
849 * Locking Note: This function will be called without the rport lock
850 * held, but it will lock, call an _enter_* function or fc_rport_error
851 * and then unlock the rport.
852 */
853static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700854 void *rdata_arg)
Robert Love42e9a922008-12-09 15:10:17 -0800855{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700856 struct fc_rport_priv *rdata = rdata_arg;
Robert Love42e9a922008-12-09 15:10:17 -0800857 u8 op;
858
859 mutex_lock(&rdata->rp_mutex);
860
Joe Eykholtf657d292009-08-25 14:03:21 -0700861 FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800862
863 if (rdata->rp_state != RPORT_ST_RTV) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700864 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
865 "%s\n", fc_rport_state(rdata));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700866 if (IS_ERR(fp))
867 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800868 goto out;
869 }
870
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700871 if (IS_ERR(fp)) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700872 fc_rport_error(rdata, fp);
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700873 goto err;
874 }
875
Robert Love42e9a922008-12-09 15:10:17 -0800876 op = fc_frame_payload_op(fp);
877 if (op == ELS_LS_ACC) {
878 struct fc_els_rtv_acc *rtv;
879 u32 toq;
880 u32 tov;
881
882 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
883 if (rtv) {
884 toq = ntohl(rtv->rtv_toq);
885 tov = ntohl(rtv->rtv_r_a_tov);
886 if (tov == 0)
887 tov = 1;
888 rdata->r_a_tov = tov;
889 tov = ntohl(rtv->rtv_e_d_tov);
890 if (toq & FC_ELS_RTV_EDRES)
891 tov /= 1000000;
892 if (tov == 0)
893 tov = 1;
894 rdata->e_d_tov = tov;
895 }
896 }
897
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700898 fc_rport_enter_ready(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800899
900out:
901 fc_frame_free(fp);
902err:
903 mutex_unlock(&rdata->rp_mutex);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700904 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800905}
906
907/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800908 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
909 * @rdata: The remote port to send the RTV request to
Robert Love42e9a922008-12-09 15:10:17 -0800910 *
911 * Locking Note: The rport lock is expected to be held before calling
912 * this routine.
913 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700914static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800915{
916 struct fc_frame *fp;
Robert Love42e9a922008-12-09 15:10:17 -0800917 struct fc_lport *lport = rdata->local_port;
918
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700919 FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
920 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800921
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700922 fc_rport_state_enter(rdata, RPORT_ST_RTV);
Robert Love42e9a922008-12-09 15:10:17 -0800923
924 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
925 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700926 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800927 return;
928 }
929
Joe Eykholtf211fa52009-08-25 14:01:01 -0700930 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
Joe Eykholtb94f8952009-11-03 11:50:21 -0800931 fc_rport_rtv_resp, rdata,
932 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -0700933 fc_rport_error_retry(rdata, NULL);
Robert Love42e9a922008-12-09 15:10:17 -0800934 else
Joe Eykholtf211fa52009-08-25 14:01:01 -0700935 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800936}
937
938/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800939 * fc_rport_enter_logo() - Send a logout (LOGO) request
940 * @rdata: The remote port to send the LOGO request to
Robert Love42e9a922008-12-09 15:10:17 -0800941 *
942 * Locking Note: The rport lock is expected to be held before calling
943 * this routine.
944 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700945static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800946{
Robert Love42e9a922008-12-09 15:10:17 -0800947 struct fc_lport *lport = rdata->local_port;
948 struct fc_frame *fp;
949
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700950 FC_RPORT_DBG(rdata, "Port entered LOGO state from %s state\n",
951 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -0800952
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700953 fc_rport_state_enter(rdata, RPORT_ST_LOGO);
Robert Love42e9a922008-12-09 15:10:17 -0800954
955 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
956 if (!fp) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700957 fc_rport_error_retry(rdata, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800958 return;
959 }
960
Joe Eykholtf211fa52009-08-25 14:01:01 -0700961 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
Joe Eykholtb94f8952009-11-03 11:50:21 -0800962 fc_rport_logo_resp, rdata,
963 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -0700964 fc_rport_error_retry(rdata, NULL);
Robert Love42e9a922008-12-09 15:10:17 -0800965 else
Joe Eykholtf211fa52009-08-25 14:01:01 -0700966 kref_get(&rdata->kref);
Robert Love42e9a922008-12-09 15:10:17 -0800967}
968
Robert Love42e9a922008-12-09 15:10:17 -0800969/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800970 * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
971 * @sp: The sequence the ADISC response was on
972 * @fp: The ADISC response frame
973 * @rdata_arg: The remote port that sent the ADISC response
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700974 *
975 * Locking Note: This function will be called without the rport lock
976 * held, but it will lock, call an _enter_* function or fc_rport_error
977 * and then unlock the rport.
978 */
979static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
Robert Love3a3b42b2009-11-03 11:47:39 -0800980 void *rdata_arg)
Joe Eykholt370c3bd2009-08-25 14:03:47 -0700981{
982 struct fc_rport_priv *rdata = rdata_arg;
983 struct fc_els_adisc *adisc;
984 u8 op;
985
986 mutex_lock(&rdata->rp_mutex);
987
988 FC_RPORT_DBG(rdata, "Received a ADISC response\n");
989
990 if (rdata->rp_state != RPORT_ST_ADISC) {
991 FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
992 fc_rport_state(rdata));
993 if (IS_ERR(fp))
994 goto err;
995 goto out;
996 }
997
998 if (IS_ERR(fp)) {
999 fc_rport_error(rdata, fp);
1000 goto err;
1001 }
1002
1003 /*
1004 * If address verification failed. Consider us logged out of the rport.
1005 * Since the rport is still in discovery, we want to be
1006 * logged in, so go to PLOGI state. Otherwise, go back to READY.
1007 */
1008 op = fc_frame_payload_op(fp);
1009 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1010 if (op != ELS_LS_ACC || !adisc ||
1011 ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
1012 get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
1013 get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
1014 FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
1015 fc_rport_enter_plogi(rdata);
1016 } else {
1017 FC_RPORT_DBG(rdata, "ADISC OK\n");
1018 fc_rport_enter_ready(rdata);
1019 }
1020out:
1021 fc_frame_free(fp);
1022err:
1023 mutex_unlock(&rdata->rp_mutex);
1024 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1025}
1026
1027/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001028 * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
1029 * @rdata: The remote port to send the ADISC request to
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001030 *
1031 * Locking Note: The rport lock is expected to be held before calling
1032 * this routine.
1033 */
1034static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
1035{
1036 struct fc_lport *lport = rdata->local_port;
1037 struct fc_frame *fp;
1038
1039 FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
1040 fc_rport_state(rdata));
1041
1042 fc_rport_state_enter(rdata, RPORT_ST_ADISC);
1043
1044 fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
1045 if (!fp) {
1046 fc_rport_error_retry(rdata, fp);
1047 return;
1048 }
1049 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
Joe Eykholtb94f8952009-11-03 11:50:21 -08001050 fc_rport_adisc_resp, rdata,
1051 2 * lport->r_a_tov))
Chris Leech8f550f92009-10-21 16:28:09 -07001052 fc_rport_error_retry(rdata, NULL);
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001053 else
1054 kref_get(&rdata->kref);
1055}
1056
1057/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001058 * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
1059 * @rdata: The remote port that sent the ADISC request
1060 * @sp: The sequence the ADISC request was on
1061 * @in_fp: The ADISC request frame
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001062 *
1063 * Locking Note: Called with the lport and rport locks held.
1064 */
1065static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
1066 struct fc_seq *sp, struct fc_frame *in_fp)
1067{
1068 struct fc_lport *lport = rdata->local_port;
1069 struct fc_frame *fp;
1070 struct fc_exch *ep = fc_seq_exch(sp);
1071 struct fc_els_adisc *adisc;
1072 struct fc_seq_els_data rjt_data;
1073 u32 f_ctl;
1074
1075 FC_RPORT_DBG(rdata, "Received ADISC request\n");
1076
1077 adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
1078 if (!adisc) {
1079 rjt_data.fp = NULL;
1080 rjt_data.reason = ELS_RJT_PROT;
1081 rjt_data.explan = ELS_EXPL_INV_LEN;
1082 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1083 goto drop;
1084 }
1085
1086 fp = fc_frame_alloc(lport, sizeof(*adisc));
1087 if (!fp)
1088 goto drop;
1089 fc_adisc_fill(lport, fp);
1090 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1091 adisc->adisc_cmd = ELS_LS_ACC;
1092 sp = lport->tt.seq_start_next(sp);
1093 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1094 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1095 FC_TYPE_ELS, f_ctl, 0);
1096 lport->tt.seq_send(lport, sp, fp);
1097drop:
1098 fc_frame_free(in_fp);
1099}
1100
1101/**
Yi Zou63e27fb2009-11-20 14:55:24 -08001102 * fc_rport_recv_rls_req() - Handle received Read Link Status request
1103 * @rdata: The remote port that sent the RLS request
1104 * @sp: The sequence that the RLS was on
1105 * @rx_fp: The PRLI request frame
1106 *
1107 * Locking Note: The rport lock is expected to be held before calling
1108 * this function.
1109 */
1110static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
1111 struct fc_seq *sp, struct fc_frame *rx_fp)
1112
1113{
1114 struct fc_lport *lport = rdata->local_port;
1115 struct fc_frame *fp;
1116 struct fc_exch *ep = fc_seq_exch(sp);
1117 struct fc_els_rls *rls;
1118 struct fc_els_rls_resp *rsp;
1119 struct fc_els_lesb *lesb;
1120 struct fc_seq_els_data rjt_data;
1121 struct fc_host_statistics *hst;
1122 u32 f_ctl;
1123
1124 FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
1125 fc_rport_state(rdata));
1126
1127 rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
1128 if (!rls) {
1129 rjt_data.reason = ELS_RJT_PROT;
1130 rjt_data.explan = ELS_EXPL_INV_LEN;
1131 goto out_rjt;
1132 }
1133
1134 fp = fc_frame_alloc(lport, sizeof(*rsp));
1135 if (!fp) {
1136 rjt_data.reason = ELS_RJT_UNAB;
1137 rjt_data.explan = ELS_EXPL_INSUF_RES;
1138 goto out_rjt;
1139 }
1140
1141 rsp = fc_frame_payload_get(fp, sizeof(*rsp));
1142 memset(rsp, 0, sizeof(*rsp));
1143 rsp->rls_cmd = ELS_LS_ACC;
1144 lesb = &rsp->rls_lesb;
1145 if (lport->tt.get_lesb) {
1146 /* get LESB from LLD if it supports it */
1147 lport->tt.get_lesb(lport, lesb);
1148 } else {
1149 fc_get_host_stats(lport->host);
1150 hst = &lport->host_stats;
1151 lesb->lesb_link_fail = htonl(hst->link_failure_count);
1152 lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
1153 lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
1154 lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
1155 lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
1156 lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
1157 }
1158
1159 sp = lport->tt.seq_start_next(sp);
1160 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
1161 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1162 FC_TYPE_ELS, f_ctl, 0);
1163 lport->tt.seq_send(lport, sp, fp);
1164 goto out;
1165
1166out_rjt:
1167 rjt_data.fp = NULL;
1168 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1169out:
1170 fc_frame_free(rx_fp);
1171}
1172
1173/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001174 * fc_rport_recv_els_req() - Handler for validated ELS requests
1175 * @lport: The local port that received the ELS request
1176 * @sp: The sequence that the ELS request was on
1177 * @fp: The ELS request frame
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001178 *
1179 * Handle incoming ELS requests that require port login.
1180 * The ELS opcode has already been validated by the caller.
Robert Love42e9a922008-12-09 15:10:17 -08001181 *
Joe Eykholt131203a2009-08-25 14:03:10 -07001182 * Locking Note: Called with the lport lock held.
Robert Love42e9a922008-12-09 15:10:17 -08001183 */
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001184static void fc_rport_recv_els_req(struct fc_lport *lport,
1185 struct fc_seq *sp, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -08001186{
Joe Eykholt131203a2009-08-25 14:03:10 -07001187 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -08001188 struct fc_frame_header *fh;
1189 struct fc_seq_els_data els_data;
Robert Love42e9a922008-12-09 15:10:17 -08001190
Robert Love42e9a922008-12-09 15:10:17 -08001191 els_data.fp = NULL;
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001192 els_data.reason = ELS_RJT_UNAB;
1193 els_data.explan = ELS_EXPL_PLOGI_REQD;
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001194
Robert Love42e9a922008-12-09 15:10:17 -08001195 fh = fc_frame_header_get(fp);
1196
Joe Eykholt25b37b92009-08-25 14:03:15 -07001197 mutex_lock(&lport->disc.disc_mutex);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001198 rdata = lport->tt.rport_lookup(lport, ntoh24(fh->fh_s_id));
Joe Eykholt131203a2009-08-25 14:03:10 -07001199 if (!rdata) {
Joe Eykholt25b37b92009-08-25 14:03:15 -07001200 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001201 goto reject;
Joe Eykholt131203a2009-08-25 14:03:10 -07001202 }
1203 mutex_lock(&rdata->rp_mutex);
Joe Eykholt25b37b92009-08-25 14:03:15 -07001204 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt131203a2009-08-25 14:03:10 -07001205
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001206 switch (rdata->rp_state) {
1207 case RPORT_ST_PRLI:
1208 case RPORT_ST_RTV:
1209 case RPORT_ST_READY:
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001210 case RPORT_ST_ADISC:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001211 break;
1212 default:
1213 mutex_unlock(&rdata->rp_mutex);
1214 goto reject;
1215 }
1216
1217 switch (fc_frame_payload_op(fp)) {
Joe Eykholt131203a2009-08-25 14:03:10 -07001218 case ELS_PRLI:
1219 fc_rport_recv_prli_req(rdata, sp, fp);
1220 break;
1221 case ELS_PRLO:
1222 fc_rport_recv_prlo_req(rdata, sp, fp);
1223 break;
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001224 case ELS_ADISC:
1225 fc_rport_recv_adisc_req(rdata, sp, fp);
1226 break;
Joe Eykholt131203a2009-08-25 14:03:10 -07001227 case ELS_RRQ:
1228 els_data.fp = fp;
1229 lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
1230 break;
1231 case ELS_REC:
1232 els_data.fp = fp;
1233 lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
1234 break;
Yi Zou63e27fb2009-11-20 14:55:24 -08001235 case ELS_RLS:
1236 fc_rport_recv_rls_req(rdata, sp, fp);
1237 break;
Joe Eykholt131203a2009-08-25 14:03:10 -07001238 default:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001239 fc_frame_free(fp); /* can't happen */
Joe Eykholt131203a2009-08-25 14:03:10 -07001240 break;
Robert Love42e9a922008-12-09 15:10:17 -08001241 }
1242
1243 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001244 return;
1245
1246reject:
1247 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
1248 fc_frame_free(fp);
1249}
1250
1251/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001252 * fc_rport_recv_req() - Handler for requests
1253 * @sp: The sequence the request was on
1254 * @fp: The request frame
1255 * @lport: The local port that received the request
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001256 *
1257 * Locking Note: Called with the lport lock held.
1258 */
1259void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
1260 struct fc_lport *lport)
1261{
1262 struct fc_seq_els_data els_data;
1263
1264 /*
1265 * Handle PLOGI and LOGO requests separately, since they
1266 * don't require prior login.
1267 * Check for unsupported opcodes first and reject them.
1268 * For some ops, it would be incorrect to reject with "PLOGI required".
1269 */
1270 switch (fc_frame_payload_op(fp)) {
1271 case ELS_PLOGI:
1272 fc_rport_recv_plogi_req(lport, sp, fp);
1273 break;
1274 case ELS_LOGO:
1275 fc_rport_recv_logo_req(lport, sp, fp);
1276 break;
1277 case ELS_PRLI:
1278 case ELS_PRLO:
Joe Eykholt8abbe3a2009-08-25 14:03:52 -07001279 case ELS_ADISC:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001280 case ELS_RRQ:
1281 case ELS_REC:
Yi Zou63e27fb2009-11-20 14:55:24 -08001282 case ELS_RLS:
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001283 fc_rport_recv_els_req(lport, sp, fp);
1284 break;
1285 default:
1286 fc_frame_free(fp);
1287 els_data.fp = NULL;
1288 els_data.reason = ELS_RJT_UNSUP;
1289 els_data.explan = ELS_EXPL_NONE;
1290 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
1291 break;
1292 }
Robert Love42e9a922008-12-09 15:10:17 -08001293}
1294
1295/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001296 * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
1297 * @lport: The local port that received the PLOGI request
1298 * @sp: The sequence that the PLOGI request was on
1299 * @rx_fp: The PLOGI request frame
Robert Love42e9a922008-12-09 15:10:17 -08001300 *
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001301 * Locking Note: The rport lock is held before calling this function.
Robert Love42e9a922008-12-09 15:10:17 -08001302 */
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001303static void fc_rport_recv_plogi_req(struct fc_lport *lport,
Robert Love42e9a922008-12-09 15:10:17 -08001304 struct fc_seq *sp, struct fc_frame *rx_fp)
1305{
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001306 struct fc_disc *disc;
1307 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -08001308 struct fc_frame *fp = rx_fp;
1309 struct fc_exch *ep;
1310 struct fc_frame_header *fh;
1311 struct fc_els_flogi *pl;
1312 struct fc_seq_els_data rjt_data;
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001313 u32 sid, f_ctl;
1314
Robert Love42e9a922008-12-09 15:10:17 -08001315 rjt_data.fp = NULL;
Robert Love42e9a922008-12-09 15:10:17 -08001316 fh = fc_frame_header_get(fp);
Robert Love42e9a922008-12-09 15:10:17 -08001317 sid = ntoh24(fh->fh_s_id);
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001318
1319 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
1320
Robert Love42e9a922008-12-09 15:10:17 -08001321 pl = fc_frame_payload_get(fp, sizeof(*pl));
1322 if (!pl) {
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001323 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
1324 rjt_data.reason = ELS_RJT_PROT;
1325 rjt_data.explan = ELS_EXPL_INV_LEN;
1326 goto reject;
Robert Love42e9a922008-12-09 15:10:17 -08001327 }
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001328
1329 disc = &lport->disc;
1330 mutex_lock(&disc->disc_mutex);
1331 rdata = lport->tt.rport_create(lport, sid);
1332 if (!rdata) {
1333 mutex_unlock(&disc->disc_mutex);
1334 rjt_data.reason = ELS_RJT_UNAB;
1335 rjt_data.explan = ELS_EXPL_INSUF_RES;
1336 goto reject;
1337 }
1338
1339 mutex_lock(&rdata->rp_mutex);
1340 mutex_unlock(&disc->disc_mutex);
1341
1342 rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1343 rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
Robert Love42e9a922008-12-09 15:10:17 -08001344
1345 /*
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001346 * If the rport was just created, possibly due to the incoming PLOGI,
Robert Love42e9a922008-12-09 15:10:17 -08001347 * set the state appropriately and accept the PLOGI.
1348 *
1349 * If we had also sent a PLOGI, and if the received PLOGI is from a
1350 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1351 * "command already in progress".
1352 *
1353 * XXX TBD: If the session was ready before, the PLOGI should result in
1354 * all outstanding exchanges being reset.
1355 */
1356 switch (rdata->rp_state) {
1357 case RPORT_ST_INIT:
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001358 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
Robert Love42e9a922008-12-09 15:10:17 -08001359 break;
1360 case RPORT_ST_PLOGI:
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001361 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1362 if (rdata->ids.port_name < lport->wwpn) {
1363 mutex_unlock(&rdata->rp_mutex);
1364 rjt_data.reason = ELS_RJT_INPROG;
1365 rjt_data.explan = ELS_EXPL_NONE;
1366 goto reject;
1367 }
Robert Love42e9a922008-12-09 15:10:17 -08001368 break;
1369 case RPORT_ST_PRLI:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001370 case RPORT_ST_RTV:
Robert Love42e9a922008-12-09 15:10:17 -08001371 case RPORT_ST_READY:
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001372 case RPORT_ST_ADISC:
1373 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
1374 "- ignored for now\n", rdata->rp_state);
1375 /* XXX TBD - should reset */
Robert Love42e9a922008-12-09 15:10:17 -08001376 break;
Joe Eykholt14194052009-07-29 17:04:43 -07001377 case RPORT_ST_DELETE:
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001378 case RPORT_ST_LOGO:
1379 case RPORT_ST_RESTART:
1380 FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
1381 fc_rport_state(rdata));
1382 mutex_unlock(&rdata->rp_mutex);
1383 rjt_data.reason = ELS_RJT_BUSY;
1384 rjt_data.explan = ELS_EXPL_NONE;
1385 goto reject;
Robert Love42e9a922008-12-09 15:10:17 -08001386 }
1387
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001388 /*
1389 * Get session payload size from incoming PLOGI.
1390 */
1391 rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
1392 fc_frame_free(rx_fp);
Robert Love42e9a922008-12-09 15:10:17 -08001393
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001394 /*
1395 * Send LS_ACC. If this fails, the originator should retry.
1396 */
1397 sp = lport->tt.seq_start_next(sp);
1398 if (!sp)
1399 goto out;
1400 fp = fc_frame_alloc(lport, sizeof(*pl));
1401 if (!fp)
1402 goto out;
Robert Love42e9a922008-12-09 15:10:17 -08001403
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001404 fc_plogi_fill(lport, fp, ELS_LS_ACC);
1405 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1406 ep = fc_seq_exch(sp);
1407 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1408 FC_TYPE_ELS, f_ctl, 0);
1409 lport->tt.seq_send(lport, sp, fp);
1410 fc_rport_enter_prli(rdata);
1411out:
1412 mutex_unlock(&rdata->rp_mutex);
1413 return;
1414
1415reject:
1416 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1417 fc_frame_free(fp);
Robert Love42e9a922008-12-09 15:10:17 -08001418}
1419
1420/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001421 * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
1422 * @rdata: The remote port that sent the PRLI request
1423 * @sp: The sequence that the PRLI was on
1424 * @rx_fp: The PRLI request frame
Robert Love42e9a922008-12-09 15:10:17 -08001425 *
1426 * Locking Note: The rport lock is exected to be held before calling
1427 * this function.
1428 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001429static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -08001430 struct fc_seq *sp, struct fc_frame *rx_fp)
1431{
Robert Love42e9a922008-12-09 15:10:17 -08001432 struct fc_lport *lport = rdata->local_port;
1433 struct fc_exch *ep;
1434 struct fc_frame *fp;
1435 struct fc_frame_header *fh;
1436 struct {
1437 struct fc_els_prli prli;
1438 struct fc_els_spp spp;
1439 } *pp;
1440 struct fc_els_spp *rspp; /* request service param page */
1441 struct fc_els_spp *spp; /* response spp */
1442 unsigned int len;
1443 unsigned int plen;
1444 enum fc_els_rjt_reason reason = ELS_RJT_UNAB;
1445 enum fc_els_rjt_explan explan = ELS_EXPL_NONE;
1446 enum fc_els_spp_resp resp;
1447 struct fc_seq_els_data rjt_data;
1448 u32 f_ctl;
1449 u32 fcp_parm;
1450 u32 roles = FC_RPORT_ROLE_UNKNOWN;
1451 rjt_data.fp = NULL;
1452
1453 fh = fc_frame_header_get(rx_fp);
1454
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001455 FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1456 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001457
1458 switch (rdata->rp_state) {
1459 case RPORT_ST_PRLI:
Joe Eykholt3ac6f982009-08-25 14:03:26 -07001460 case RPORT_ST_RTV:
Robert Love42e9a922008-12-09 15:10:17 -08001461 case RPORT_ST_READY:
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001462 case RPORT_ST_ADISC:
Robert Love42e9a922008-12-09 15:10:17 -08001463 reason = ELS_RJT_NONE;
1464 break;
1465 default:
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -07001466 fc_frame_free(rx_fp);
1467 return;
Robert Love42e9a922008-12-09 15:10:17 -08001468 break;
1469 }
1470 len = fr_len(rx_fp) - sizeof(*fh);
1471 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1472 if (pp == NULL) {
1473 reason = ELS_RJT_PROT;
1474 explan = ELS_EXPL_INV_LEN;
1475 } else {
1476 plen = ntohs(pp->prli.prli_len);
1477 if ((plen % 4) != 0 || plen > len) {
1478 reason = ELS_RJT_PROT;
1479 explan = ELS_EXPL_INV_LEN;
1480 } else if (plen < len) {
1481 len = plen;
1482 }
1483 plen = pp->prli.prli_spp_len;
1484 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1485 plen > len || len < sizeof(*pp)) {
1486 reason = ELS_RJT_PROT;
1487 explan = ELS_EXPL_INV_LEN;
1488 }
1489 rspp = &pp->spp;
1490 }
1491 if (reason != ELS_RJT_NONE ||
1492 (fp = fc_frame_alloc(lport, len)) == NULL) {
1493 rjt_data.reason = reason;
1494 rjt_data.explan = explan;
1495 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1496 } else {
1497 sp = lport->tt.seq_start_next(sp);
1498 WARN_ON(!sp);
1499 pp = fc_frame_payload_get(fp, len);
1500 WARN_ON(!pp);
1501 memset(pp, 0, len);
1502 pp->prli.prli_cmd = ELS_LS_ACC;
1503 pp->prli.prli_spp_len = plen;
1504 pp->prli.prli_len = htons(len);
1505 len -= sizeof(struct fc_els_prli);
1506
Robert Love6bd054c2009-08-25 14:03:04 -07001507 /* reinitialize remote port roles */
1508 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
1509
Robert Love42e9a922008-12-09 15:10:17 -08001510 /*
1511 * Go through all the service parameter pages and build
1512 * response. If plen indicates longer SPP than standard,
1513 * use that. The entire response has been pre-cleared above.
1514 */
1515 spp = &pp->spp;
1516 while (len >= plen) {
1517 spp->spp_type = rspp->spp_type;
1518 spp->spp_type_ext = rspp->spp_type_ext;
1519 spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
1520 resp = FC_SPP_RESP_ACK;
1521 if (rspp->spp_flags & FC_SPP_RPA_VAL)
1522 resp = FC_SPP_RESP_NO_PA;
1523 switch (rspp->spp_type) {
1524 case 0: /* common to all FC-4 types */
1525 break;
1526 case FC_TYPE_FCP:
1527 fcp_parm = ntohl(rspp->spp_params);
Joe Eykholt85b58932009-10-21 16:26:45 -07001528 if (fcp_parm & FCP_SPPF_RETRY)
Robert Love42e9a922008-12-09 15:10:17 -08001529 rdata->flags |= FC_RP_FLAGS_RETRY;
Joe Eykholtf211fa52009-08-25 14:01:01 -07001530 rdata->supported_classes = FC_COS_CLASS3;
Robert Love42e9a922008-12-09 15:10:17 -08001531 if (fcp_parm & FCP_SPPF_INIT_FCN)
1532 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1533 if (fcp_parm & FCP_SPPF_TARG_FCN)
1534 roles |= FC_RPORT_ROLE_FCP_TARGET;
Joe Eykholtf211fa52009-08-25 14:01:01 -07001535 rdata->ids.roles = roles;
Robert Love42e9a922008-12-09 15:10:17 -08001536
1537 spp->spp_params =
1538 htonl(lport->service_params);
1539 break;
1540 default:
1541 resp = FC_SPP_RESP_INVL;
1542 break;
1543 }
1544 spp->spp_flags |= resp;
1545 len -= plen;
1546 rspp = (struct fc_els_spp *)((char *)rspp + plen);
1547 spp = (struct fc_els_spp *)((char *)spp + plen);
1548 }
1549
1550 /*
1551 * Send LS_ACC. If this fails, the originator should retry.
1552 */
1553 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1554 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1555 ep = fc_seq_exch(sp);
1556 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1557 FC_TYPE_ELS, f_ctl, 0);
1558 lport->tt.seq_send(lport, sp, fp);
1559
1560 /*
1561 * Get lock and re-check state.
1562 */
1563 switch (rdata->rp_state) {
1564 case RPORT_ST_PRLI:
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001565 fc_rport_enter_ready(rdata);
Robert Love42e9a922008-12-09 15:10:17 -08001566 break;
1567 case RPORT_ST_READY:
Joe Eykholt370c3bd2009-08-25 14:03:47 -07001568 case RPORT_ST_ADISC:
Robert Love42e9a922008-12-09 15:10:17 -08001569 break;
1570 default:
1571 break;
1572 }
1573 }
1574 fc_frame_free(rx_fp);
1575}
1576
1577/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001578 * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
1579 * @rdata: The remote port that sent the PRLO request
1580 * @sp: The sequence that the PRLO was on
1581 * @fp: The PRLO request frame
Robert Love42e9a922008-12-09 15:10:17 -08001582 *
1583 * Locking Note: The rport lock is exected to be held before calling
1584 * this function.
1585 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001586static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
1587 struct fc_seq *sp,
Robert Love42e9a922008-12-09 15:10:17 -08001588 struct fc_frame *fp)
1589{
Robert Love42e9a922008-12-09 15:10:17 -08001590 struct fc_lport *lport = rdata->local_port;
1591
1592 struct fc_frame_header *fh;
1593 struct fc_seq_els_data rjt_data;
1594
1595 fh = fc_frame_header_get(fp);
1596
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001597 FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
1598 fc_rport_state(rdata));
Robert Love42e9a922008-12-09 15:10:17 -08001599
1600 rjt_data.fp = NULL;
1601 rjt_data.reason = ELS_RJT_UNAB;
1602 rjt_data.explan = ELS_EXPL_NONE;
1603 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1604 fc_frame_free(fp);
1605}
1606
1607/**
Robert Love3a3b42b2009-11-03 11:47:39 -08001608 * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
1609 * @lport: The local port that received the LOGO request
1610 * @sp: The sequence that the LOGO request was on
1611 * @fp: The LOGO request frame
Robert Love42e9a922008-12-09 15:10:17 -08001612 *
1613 * Locking Note: The rport lock is exected to be held before calling
1614 * this function.
1615 */
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001616static void fc_rport_recv_logo_req(struct fc_lport *lport,
Joe Eykholt9fb9d322009-08-25 14:00:50 -07001617 struct fc_seq *sp,
Robert Love42e9a922008-12-09 15:10:17 -08001618 struct fc_frame *fp)
1619{
1620 struct fc_frame_header *fh;
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001621 struct fc_rport_priv *rdata;
1622 u32 sid;
Robert Love42e9a922008-12-09 15:10:17 -08001623
Joe Eykholtfeab4ae2009-08-25 14:03:36 -07001624 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
1625
Robert Love42e9a922008-12-09 15:10:17 -08001626 fh = fc_frame_header_get(fp);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001627 sid = ntoh24(fh->fh_s_id);
Robert Love42e9a922008-12-09 15:10:17 -08001628
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001629 mutex_lock(&lport->disc.disc_mutex);
1630 rdata = lport->tt.rport_lookup(lport, sid);
1631 if (rdata) {
1632 mutex_lock(&rdata->rp_mutex);
1633 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
1634 fc_rport_state(rdata));
Joe Eykholtfeab4ae2009-08-25 14:03:36 -07001635
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001636 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
1637
Joe Eykholtfeab4ae2009-08-25 14:03:36 -07001638 /*
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001639 * If the remote port was created due to discovery, set state
1640 * to log back in. It may have seen a stale RSCN about us.
Joe Eykholtfeab4ae2009-08-25 14:03:36 -07001641 */
Joe Eykholtb4a9c7e2009-10-21 16:28:30 -07001642 if (rdata->disc_id)
1643 fc_rport_state_enter(rdata, RPORT_ST_RESTART);
Joe Eykholt83fe6a92009-08-25 14:03:31 -07001644 mutex_unlock(&rdata->rp_mutex);
1645 } else
1646 FC_RPORT_ID_DBG(lport, sid,
1647 "Received LOGO from non-logged-in port\n");
1648 mutex_unlock(&lport->disc.disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -08001649 fc_frame_free(fp);
1650}
1651
Robert Love3a3b42b2009-11-03 11:47:39 -08001652/**
1653 * fc_rport_flush_queue() - Flush the rport_event_queue
1654 */
Robert Love42e9a922008-12-09 15:10:17 -08001655static void fc_rport_flush_queue(void)
1656{
1657 flush_workqueue(rport_event_queue);
1658}
1659
Robert Love3a3b42b2009-11-03 11:47:39 -08001660/**
1661 * fc_rport_init() - Initialize the remote port layer for a local port
1662 * @lport: The local port to initialize the remote port layer for
1663 */
Robert Love42e9a922008-12-09 15:10:17 -08001664int fc_rport_init(struct fc_lport *lport)
1665{
Joe Eykholt8025b5d2009-08-25 14:02:06 -07001666 if (!lport->tt.rport_lookup)
1667 lport->tt.rport_lookup = fc_rport_lookup;
1668
Robert Love5101ff92009-02-27 10:55:18 -08001669 if (!lport->tt.rport_create)
Joe Eykholt9e9d0452009-08-25 14:01:18 -07001670 lport->tt.rport_create = fc_rport_create;
Robert Love5101ff92009-02-27 10:55:18 -08001671
Robert Love42e9a922008-12-09 15:10:17 -08001672 if (!lport->tt.rport_login)
1673 lport->tt.rport_login = fc_rport_login;
1674
1675 if (!lport->tt.rport_logoff)
1676 lport->tt.rport_logoff = fc_rport_logoff;
1677
1678 if (!lport->tt.rport_recv_req)
1679 lport->tt.rport_recv_req = fc_rport_recv_req;
1680
1681 if (!lport->tt.rport_flush_queue)
1682 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1683
Joe Eykholtf211fa52009-08-25 14:01:01 -07001684 if (!lport->tt.rport_destroy)
1685 lport->tt.rport_destroy = fc_rport_destroy;
1686
Robert Love42e9a922008-12-09 15:10:17 -08001687 return 0;
1688}
1689EXPORT_SYMBOL(fc_rport_init);
1690
Robert Love3a3b42b2009-11-03 11:47:39 -08001691/**
1692 * fc_setup_rport() - Initialize the rport_event_queue
1693 */
1694int fc_setup_rport()
Robert Love42e9a922008-12-09 15:10:17 -08001695{
1696 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
1697 if (!rport_event_queue)
1698 return -ENOMEM;
1699 return 0;
1700}
Robert Love42e9a922008-12-09 15:10:17 -08001701
Robert Love3a3b42b2009-11-03 11:47:39 -08001702/**
1703 * fc_destroy_rport() - Destroy the rport_event_queue
1704 */
1705void fc_destroy_rport()
Robert Love42e9a922008-12-09 15:10:17 -08001706{
1707 destroy_workqueue(rport_event_queue);
1708}
Robert Love42e9a922008-12-09 15:10:17 -08001709
Robert Love3a3b42b2009-11-03 11:47:39 -08001710/**
1711 * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
1712 * @rport: The remote port whose I/O should be terminated
1713 */
Robert Love42e9a922008-12-09 15:10:17 -08001714void fc_rport_terminate_io(struct fc_rport *rport)
1715{
Robert Love3a3b42b2009-11-03 11:47:39 -08001716 struct fc_rport_libfc_priv *rpriv = rport->dd_data;
1717 struct fc_lport *lport = rpriv->local_port;
Robert Love42e9a922008-12-09 15:10:17 -08001718
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -08001719 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
1720 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
Robert Love42e9a922008-12-09 15:10:17 -08001721}
1722EXPORT_SYMBOL(fc_rport_terminate_io);