]> git.ipfire.org Git - thirdparty/linux.git/blame - drivers/scsi/libfc/fc_rport.c
[SCSI] fcoe: adds src and dest mac address checking for fcoe frames
[thirdparty/linux.git] / drivers / scsi / libfc / fc_rport.c
CommitLineData
42e9a92f
RL
1/*
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>
5a0e3ad6 50#include <linux/slab.h>
42e9a92f
RL
51#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
8866a5d9
RL
59#include "fc_libfc.h"
60
42e9a92f
RL
61struct workqueue_struct *rport_event_queue;
62
9fb9d328
JE
63static 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 *);
370c3bd0 68static void fc_rport_enter_adisc(struct fc_rport_priv *);
42e9a92f 69
3ac6f98f 70static void fc_rport_recv_plogi_req(struct fc_lport *,
42e9a92f 71 struct fc_seq *, struct fc_frame *);
9fb9d328 72static void fc_rport_recv_prli_req(struct fc_rport_priv *,
42e9a92f 73 struct fc_seq *, struct fc_frame *);
9fb9d328 74static void fc_rport_recv_prlo_req(struct fc_rport_priv *,
42e9a92f 75 struct fc_seq *, struct fc_frame *);
83fe6a93 76static void fc_rport_recv_logo_req(struct fc_lport *,
42e9a92f
RL
77 struct fc_seq *, struct fc_frame *);
78static void fc_rport_timeout(struct work_struct *);
9fb9d328
JE
79static void fc_rport_error(struct fc_rport_priv *, struct fc_frame *);
80static void fc_rport_error_retry(struct fc_rport_priv *, struct fc_frame *);
42e9a92f
RL
81static void fc_rport_work(struct work_struct *);
82
83static const char *fc_rport_state_names[] = {
42e9a92f
RL
84 [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",
370c3bd0 90 [RPORT_ST_ADISC] = "ADISC",
14194054 91 [RPORT_ST_DELETE] = "Delete",
42e9a92f
RL
92};
93
8025b5db 94/**
3a3b42bf
RL
95 * 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
8025b5db
JE
98 */
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)
b4a9c7ed 105 if (rdata->ids.port_id == port_id)
8025b5db
JE
106 return rdata;
107 return NULL;
108}
109
9e9d0452 110/**
9737e6a7 111 * fc_rport_create() - Create a new remote port
3a3b42bf
RL
112 * @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.
9e9d0452 116 *
48f00902 117 * Locking note: must be called with the disc_mutex held.
9e9d0452
JE
118 */
119static struct fc_rport_priv *fc_rport_create(struct fc_lport *lport,
9737e6a7 120 u32 port_id)
42e9a92f 121{
ab28f1fd 122 struct fc_rport_priv *rdata;
42e9a92f 123
9737e6a7 124 rdata = lport->tt.rport_lookup(lport, port_id);
19f97e3c
JE
125 if (rdata)
126 return rdata;
127
9e9d0452
JE
128 rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
129 if (!rdata)
42e9a92f
RL
130 return NULL;
131
9737e6a7
RL
132 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
f211fa51 137 kref_init(&rdata->kref);
42e9a92f 138 mutex_init(&rdata->rp_mutex);
795d86f5 139 rdata->local_port = lport;
42e9a92f
RL
140 rdata->rp_state = RPORT_ST_INIT;
141 rdata->event = RPORT_EV_NONE;
142 rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
795d86f5
JE
143 rdata->e_d_tov = lport->e_d_tov;
144 rdata->r_a_tov = lport->r_a_tov;
f211fa51 145 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
42e9a92f
RL
146 INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
147 INIT_WORK(&rdata->event_work, fc_rport_work);
9737e6a7 148 if (port_id != FC_FID_DIR_SERV)
48f00902 149 list_add(&rdata->peers, &lport->disc.rports);
9fb9d328 150 return rdata;
42e9a92f
RL
151}
152
f211fa51 153/**
3a3b42bf
RL
154 * fc_rport_destroy() - Free a remote port after last reference is released
155 * @kref: The remote port's kref
f211fa51
JE
156 */
157static void fc_rport_destroy(struct kref *kref)
158{
159 struct fc_rport_priv *rdata;
f211fa51
JE
160
161 rdata = container_of(kref, struct fc_rport_priv, kref);
9e9d0452 162 kfree(rdata);
f211fa51
JE
163}
164
42e9a92f 165/**
3a3b42bf
RL
166 * fc_rport_state() - Return a string identifying the remote port's state
167 * @rdata: The remote port
42e9a92f 168 */
9fb9d328 169static const char *fc_rport_state(struct fc_rport_priv *rdata)
42e9a92f
RL
170{
171 const char *cp;
42e9a92f
RL
172
173 cp = fc_rport_state_names[rdata->rp_state];
174 if (!cp)
175 cp = "Unknown";
176 return cp;
177}
178
179/**
3a3b42bf
RL
180 * 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)
42e9a92f
RL
183 */
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/**
3a3b42bf
RL
194 * 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
42e9a92f 199 */
b2ab99c9
RL
200static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
201 unsigned int maxval)
42e9a92f
RL
202{
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/**
3a3b42bf
RL
219 * 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
42e9a92f
RL
222 *
223 * Locking Note: Called with the rport lock held
224 */
9fb9d328 225static void fc_rport_state_enter(struct fc_rport_priv *rdata,
42e9a92f
RL
226 enum fc_rport_state new)
227{
42e9a92f
RL
228 if (rdata->rp_state != new)
229 rdata->retries = 0;
230 rdata->rp_state = new;
231}
232
3a3b42bf
RL
233/**
234 * fc_rport_work() - Handler for remote port events in the rport_event_queue
235 * @work: Handle to the remote port being dequeued
236 */
42e9a92f
RL
237static void fc_rport_work(struct work_struct *work)
238{
571f824c 239 u32 port_id;
ab28f1fd
JE
240 struct fc_rport_priv *rdata =
241 container_of(work, struct fc_rport_priv, event_work);
3a3b42bf 242 struct fc_rport_libfc_priv *rpriv;
42e9a92f 243 enum fc_rport_event event;
42e9a92f
RL
244 struct fc_lport *lport = rdata->local_port;
245 struct fc_rport_operations *rport_ops;
629f4427 246 struct fc_rport_identifiers ids;
f211fa51 247 struct fc_rport *rport;
42e9a92f
RL
248
249 mutex_lock(&rdata->rp_mutex);
250 event = rdata->event;
251 rport_ops = rdata->ops;
f211fa51 252 rport = rdata->rport;
42e9a92f 253
9e9d0452
JE
254 FC_RPORT_DBG(rdata, "work event %u\n", event);
255
629f4427 256 switch (event) {
4c0f62b5 257 case RPORT_EV_READY:
f211fa51 258 ids = rdata->ids;
5f7ea3b7 259 rdata->event = RPORT_EV_NONE;
f034260d 260 rdata->major_retries = 0;
9e9d0452 261 kref_get(&rdata->kref);
42e9a92f
RL
262 mutex_unlock(&rdata->rp_mutex);
263
9e9d0452
JE
264 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;
42e9a92f 271 }
9e9d0452
JE
272 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
3a3b42bf
RL
279 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;
9e9d0452
JE
285 mutex_unlock(&rdata->rp_mutex);
286
8345592b 287 if (rport_ops && rport_ops->event_callback) {
9e9d0452 288 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
9fb9d328 289 rport_ops->event_callback(lport, rdata, event);
9e9d0452
JE
290 }
291 kref_put(&rdata->kref, lport->tt.rport_destroy);
629f4427
JE
292 break;
293
294 case RPORT_EV_FAILED:
295 case RPORT_EV_LOGO:
296 case RPORT_EV_STOP:
9e9d0452 297 port_id = rdata->ids.port_id;
42e9a92f 298 mutex_unlock(&rdata->rp_mutex);
9e9d0452 299
8345592b 300 if (rport_ops && rport_ops->event_callback) {
9e9d0452 301 FC_RPORT_DBG(rdata, "callback ev %d\n", event);
9fb9d328 302 rport_ops->event_callback(lport, rdata, event);
9e9d0452 303 }
201e5795 304 cancel_delayed_work_sync(&rdata->retry_work);
9e9d0452
JE
305
306 /*
307 * Reset any outstanding exchanges before freeing rport.
308 */
309 lport->tt.exch_mgr_reset(lport, 0, port_id);
310 lport->tt.exch_mgr_reset(lport, port_id, 0);
311
312 if (rport) {
3a3b42bf
RL
313 rpriv = rport->dd_data;
314 rpriv->rp_state = RPORT_ST_DELETE;
9e9d0452
JE
315 mutex_lock(&rdata->rp_mutex);
316 rdata->rport = NULL;
317 mutex_unlock(&rdata->rp_mutex);
42e9a92f 318 fc_remote_port_delete(rport);
571f824c 319 }
4b2164d4
JE
320
321 mutex_lock(&lport->disc.disc_mutex);
322 mutex_lock(&rdata->rp_mutex);
323 if (rdata->rp_state == RPORT_ST_DELETE) {
324 if (port_id == FC_FID_DIR_SERV) {
325 rdata->event = RPORT_EV_NONE;
326 mutex_unlock(&rdata->rp_mutex);
f034260d
JE
327 } else if ((rdata->flags & FC_RP_STARTED) &&
328 rdata->major_retries <
329 lport->max_rport_retry_count) {
330 rdata->major_retries++;
4b2164d4
JE
331 rdata->event = RPORT_EV_NONE;
332 FC_RPORT_DBG(rdata, "work restart\n");
333 fc_rport_enter_plogi(rdata);
334 mutex_unlock(&rdata->rp_mutex);
335 } else {
336 FC_RPORT_DBG(rdata, "work delete\n");
337 list_del(&rdata->peers);
338 mutex_unlock(&rdata->rp_mutex);
339 kref_put(&rdata->kref, lport->tt.rport_destroy);
340 }
341 } else {
342 /*
343 * Re-open for events. Reissue READY event if ready.
344 */
345 rdata->event = RPORT_EV_NONE;
346 if (rdata->rp_state == RPORT_ST_READY)
347 fc_rport_enter_ready(rdata);
b4a9c7ed 348 mutex_unlock(&rdata->rp_mutex);
4b2164d4
JE
349 }
350 mutex_unlock(&lport->disc.disc_mutex);
629f4427
JE
351 break;
352
353 default:
42e9a92f 354 mutex_unlock(&rdata->rp_mutex);
629f4427
JE
355 break;
356 }
42e9a92f
RL
357}
358
359/**
34f42a07 360 * fc_rport_login() - Start the remote port login state machine
3a3b42bf 361 * @rdata: The remote port to be logged in to
42e9a92f
RL
362 *
363 * Locking Note: Called without the rport lock held. This
364 * function will hold the rport lock, call an _enter_*
365 * function and then unlock the rport.
370c3bd0
JE
366 *
367 * This indicates the intent to be logged into the remote port.
368 * If it appears we are already logged in, ADISC is used to verify
369 * the setup.
42e9a92f 370 */
9fb9d328 371int fc_rport_login(struct fc_rport_priv *rdata)
42e9a92f 372{
42e9a92f
RL
373 mutex_lock(&rdata->rp_mutex);
374
4b2164d4 375 rdata->flags |= FC_RP_STARTED;
370c3bd0
JE
376 switch (rdata->rp_state) {
377 case RPORT_ST_READY:
378 FC_RPORT_DBG(rdata, "ADISC port\n");
379 fc_rport_enter_adisc(rdata);
380 break;
b4a9c7ed
JE
381 case RPORT_ST_DELETE:
382 FC_RPORT_DBG(rdata, "Restart deleted port\n");
b4a9c7ed 383 break;
370c3bd0
JE
384 default:
385 FC_RPORT_DBG(rdata, "Login to port\n");
386 fc_rport_enter_plogi(rdata);
387 break;
388 }
42e9a92f
RL
389 mutex_unlock(&rdata->rp_mutex);
390
391 return 0;
392}
393
5f7ea3b7 394/**
3a3b42bf
RL
395 * fc_rport_enter_delete() - Schedule a remote port to be deleted
396 * @rdata: The remote port to be deleted
397 * @event: The event to report as the reason for deletion
5f7ea3b7
JE
398 *
399 * Locking Note: Called with the rport lock held.
400 *
401 * Allow state change into DELETE only once.
402 *
403 * Call queue_work only if there's no event already pending.
404 * Set the new event so that the old pending event will not occur.
405 * Since we have the mutex, even if fc_rport_work() is already started,
406 * it'll see the new event.
407 */
9fb9d328 408static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
5f7ea3b7
JE
409 enum fc_rport_event event)
410{
5f7ea3b7
JE
411 if (rdata->rp_state == RPORT_ST_DELETE)
412 return;
413
9fb9d328 414 FC_RPORT_DBG(rdata, "Delete port\n");
5f7ea3b7 415
9fb9d328 416 fc_rport_state_enter(rdata, RPORT_ST_DELETE);
5f7ea3b7
JE
417
418 if (rdata->event == RPORT_EV_NONE)
419 queue_work(rport_event_queue, &rdata->event_work);
420 rdata->event = event;
421}
422
42e9a92f 423/**
3a3b42bf
RL
424 * fc_rport_logoff() - Logoff and remove a remote port
425 * @rdata: The remote port to be logged off of
42e9a92f
RL
426 *
427 * Locking Note: Called without the rport lock held. This
428 * function will hold the rport lock, call an _enter_*
429 * function and then unlock the rport.
430 */
9fb9d328 431int fc_rport_logoff(struct fc_rport_priv *rdata)
42e9a92f 432{
42e9a92f
RL
433 mutex_lock(&rdata->rp_mutex);
434
9fb9d328 435 FC_RPORT_DBG(rdata, "Remove port\n");
42e9a92f 436
4b2164d4 437 rdata->flags &= ~FC_RP_STARTED;
14194054 438 if (rdata->rp_state == RPORT_ST_DELETE) {
9fb9d328 439 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
b4c6f546
AJ
440 goto out;
441 }
4b2164d4 442 fc_rport_enter_logo(rdata);
42e9a92f
RL
443
444 /*
14194054 445 * Change the state to Delete so that we discard
42e9a92f
RL
446 * the response.
447 */
9fb9d328 448 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
b4c6f546 449out:
b4a9c7ed 450 mutex_unlock(&rdata->rp_mutex);
42e9a92f
RL
451 return 0;
452}
453
454/**
3a3b42bf
RL
455 * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state
456 * @rdata: The remote port that is ready
42e9a92f
RL
457 *
458 * Locking Note: The rport lock is expected to be held before calling
459 * this routine.
460 */
9fb9d328 461static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
42e9a92f 462{
9fb9d328 463 fc_rport_state_enter(rdata, RPORT_ST_READY);
42e9a92f 464
9fb9d328 465 FC_RPORT_DBG(rdata, "Port is Ready\n");
42e9a92f 466
5f7ea3b7
JE
467 if (rdata->event == RPORT_EV_NONE)
468 queue_work(rport_event_queue, &rdata->event_work);
4c0f62b5 469 rdata->event = RPORT_EV_READY;
42e9a92f
RL
470}
471
472/**
3a3b42bf
RL
473 * fc_rport_timeout() - Handler for the retry_work timer
474 * @work: Handle to the remote port that has timed out
42e9a92f
RL
475 *
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{
ab28f1fd
JE
482 struct fc_rport_priv *rdata =
483 container_of(work, struct fc_rport_priv, retry_work.work);
42e9a92f
RL
484
485 mutex_lock(&rdata->rp_mutex);
486
487 switch (rdata->rp_state) {
488 case RPORT_ST_PLOGI:
9fb9d328 489 fc_rport_enter_plogi(rdata);
42e9a92f
RL
490 break;
491 case RPORT_ST_PRLI:
9fb9d328 492 fc_rport_enter_prli(rdata);
42e9a92f
RL
493 break;
494 case RPORT_ST_RTV:
9fb9d328 495 fc_rport_enter_rtv(rdata);
42e9a92f
RL
496 break;
497 case RPORT_ST_LOGO:
9fb9d328 498 fc_rport_enter_logo(rdata);
42e9a92f 499 break;
370c3bd0
JE
500 case RPORT_ST_ADISC:
501 fc_rport_enter_adisc(rdata);
502 break;
42e9a92f
RL
503 case RPORT_ST_READY:
504 case RPORT_ST_INIT:
14194054 505 case RPORT_ST_DELETE:
42e9a92f
RL
506 break;
507 }
508
509 mutex_unlock(&rdata->rp_mutex);
42e9a92f
RL
510}
511
512/**
34f42a07 513 * fc_rport_error() - Error handler, called once retries have been exhausted
3a3b42bf
RL
514 * @rdata: The remote port the error is happened on
515 * @fp: The error code encapsulated in a frame pointer
42e9a92f 516 *
42e9a92f
RL
517 * Locking Note: The rport lock is expected to be held before
518 * calling this routine
519 */
9fb9d328 520static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
42e9a92f 521{
9fb9d328 522 FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
cdbe6dfe
JE
523 IS_ERR(fp) ? -PTR_ERR(fp) : 0,
524 fc_rport_state(rdata), rdata->retries);
42e9a92f 525
6755db1c
CL
526 switch (rdata->rp_state) {
527 case RPORT_ST_PLOGI:
6755db1c 528 case RPORT_ST_LOGO:
4b2164d4 529 rdata->flags &= ~FC_RP_STARTED;
9fb9d328 530 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
6755db1c
CL
531 break;
532 case RPORT_ST_RTV:
9fb9d328 533 fc_rport_enter_ready(rdata);
6755db1c 534 break;
370c3bd0
JE
535 case RPORT_ST_PRLI:
536 case RPORT_ST_ADISC:
537 fc_rport_enter_logo(rdata);
538 break;
14194054 539 case RPORT_ST_DELETE:
6755db1c
CL
540 case RPORT_ST_READY:
541 case RPORT_ST_INIT:
542 break;
42e9a92f
RL
543 }
544}
545
6755db1c 546/**
3a3b42bf
RL
547 * 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
6755db1c
CL
550 *
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 */
9fb9d328
JE
557static void fc_rport_error_retry(struct fc_rport_priv *rdata,
558 struct fc_frame *fp)
6755db1c 559{
6755db1c
CL
560 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)
9fb9d328 564 return fc_rport_error(rdata, fp);
6755db1c 565
a3666955 566 if (rdata->retries < rdata->local_port->max_rport_retry_count) {
9fb9d328
JE
567 FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
568 PTR_ERR(fp), fc_rport_state(rdata));
6755db1c
CL
569 rdata->retries++;
570 /* no additional delay on exchange timeouts */
571 if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
572 delay = 0;
6755db1c
CL
573 schedule_delayed_work(&rdata->retry_work, delay);
574 return;
575 }
576
9fb9d328 577 return fc_rport_error(rdata, fp);
6755db1c
CL
578}
579
42e9a92f 580/**
3a3b42bf
RL
581 * 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
42e9a92f
RL
585 *
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,
9fb9d328 591 void *rdata_arg)
42e9a92f 592{
9fb9d328 593 struct fc_rport_priv *rdata = rdata_arg;
42e9a92f 594 struct fc_lport *lport = rdata->local_port;
a29e7646 595 struct fc_els_flogi *plp = NULL;
42e9a92f
RL
596 unsigned int tov;
597 u16 csp_seq;
598 u16 cssp_seq;
599 u8 op;
600
601 mutex_lock(&rdata->rp_mutex);
602
f657d299 603 FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
42e9a92f
RL
604
605 if (rdata->rp_state != RPORT_ST_PLOGI) {
9fb9d328
JE
606 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
607 "%s\n", fc_rport_state(rdata));
76f6804e
AJ
608 if (IS_ERR(fp))
609 goto err;
42e9a92f
RL
610 goto out;
611 }
612
76f6804e 613 if (IS_ERR(fp)) {
9fb9d328 614 fc_rport_error_retry(rdata, fp);
76f6804e
AJ
615 goto err;
616 }
617
42e9a92f
RL
618 op = fc_frame_payload_op(fp);
619 if (op == ELS_LS_ACC &&
620 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
f211fa51
JE
621 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
622 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
42e9a92f
RL
623
624 tov = ntohl(plp->fl_csp.sp_e_d_tov);
625 if (ntohs(plp->fl_csp.sp_features) & FC_SP_FT_EDTR)
3b709150 626 tov /= 1000000;
42e9a92f
RL
627 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;
f211fa51 634 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
3ac6f98f 635 fc_rport_enter_prli(rdata);
42e9a92f 636 } else
9fb9d328 637 fc_rport_error_retry(rdata, fp);
42e9a92f
RL
638
639out:
640 fc_frame_free(fp);
641err:
642 mutex_unlock(&rdata->rp_mutex);
f211fa51 643 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
42e9a92f
RL
644}
645
646/**
3a3b42bf
RL
647 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
648 * @rdata: The remote port to send a PLOGI to
42e9a92f
RL
649 *
650 * Locking Note: The rport lock is expected to be held before calling
651 * this routine.
652 */
9fb9d328 653static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
42e9a92f 654{
42e9a92f
RL
655 struct fc_lport *lport = rdata->local_port;
656 struct fc_frame *fp;
657
9fb9d328
JE
658 FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
659 fc_rport_state(rdata));
42e9a92f 660
9fb9d328 661 fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
42e9a92f 662
f211fa51 663 rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
42e9a92f
RL
664 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
665 if (!fp) {
9fb9d328 666 fc_rport_error_retry(rdata, fp);
42e9a92f
RL
667 return;
668 }
669 rdata->e_d_tov = lport->e_d_tov;
670
f211fa51 671 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
b94f8951
JE
672 fc_rport_plogi_resp, rdata,
673 2 * lport->r_a_tov))
8f550f93 674 fc_rport_error_retry(rdata, NULL);
42e9a92f 675 else
f211fa51 676 kref_get(&rdata->kref);
42e9a92f
RL
677}
678
679/**
34f42a07 680 * fc_rport_prli_resp() - Process Login (PRLI) response handler
3a3b42bf
RL
681 * @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
42e9a92f
RL
684 *
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,
9fb9d328 690 void *rdata_arg)
42e9a92f 691{
9fb9d328 692 struct fc_rport_priv *rdata = rdata_arg;
42e9a92f
RL
693 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;
618461c0 700 u8 resp_code = 0;
42e9a92f
RL
701
702 mutex_lock(&rdata->rp_mutex);
703
f657d299 704 FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
42e9a92f
RL
705
706 if (rdata->rp_state != RPORT_ST_PRLI) {
9fb9d328
JE
707 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
708 "%s\n", fc_rport_state(rdata));
76f6804e
AJ
709 if (IS_ERR(fp))
710 goto err;
42e9a92f
RL
711 goto out;
712 }
713
76f6804e 714 if (IS_ERR(fp)) {
9fb9d328 715 fc_rport_error_retry(rdata, fp);
76f6804e
AJ
716 goto err;
717 }
718
6bd054cb
RL
719 /* reinitialize remote port roles */
720 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
721
42e9a92f
RL
722 op = fc_frame_payload_op(fp);
723 if (op == ELS_LS_ACC) {
724 pp = fc_frame_payload_get(fp, sizeof(*pp));
618461c0
BPG
725 if (!pp)
726 goto out;
727
728 resp_code = (pp->spp.spp_flags & FC_SPP_RESP_MASK);
729 FC_RPORT_DBG(rdata, "PRLI spp_flags = 0x%x\n",
730 pp->spp.spp_flags);
731 if (resp_code != FC_SPP_RESP_ACK) {
732 if (resp_code == FC_SPP_RESP_CONF)
733 fc_rport_error(rdata, fp);
734 else
735 fc_rport_error_retry(rdata, fp);
736 goto out;
42e9a92f 737 }
618461c0
BPG
738 if (pp->prli.prli_spp_len < sizeof(pp->spp))
739 goto out;
740
741 fcp_parm = ntohl(pp->spp.spp_params);
742 if (fcp_parm & FCP_SPPF_RETRY)
743 rdata->flags |= FC_RP_FLAGS_RETRY;
42e9a92f 744
f211fa51 745 rdata->supported_classes = FC_COS_CLASS3;
42e9a92f
RL
746 if (fcp_parm & FCP_SPPF_INIT_FCN)
747 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
748 if (fcp_parm & FCP_SPPF_TARG_FCN)
749 roles |= FC_RPORT_ROLE_FCP_TARGET;
750
f211fa51 751 rdata->ids.roles = roles;
9fb9d328 752 fc_rport_enter_rtv(rdata);
42e9a92f
RL
753
754 } else {
9fb9d328 755 FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
292e40b9 756 fc_rport_error_retry(rdata, fp);
42e9a92f
RL
757 }
758
759out:
760 fc_frame_free(fp);
761err:
762 mutex_unlock(&rdata->rp_mutex);
f211fa51 763 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
42e9a92f
RL
764}
765
766/**
3a3b42bf
RL
767 * fc_rport_logo_resp() - Handler for logout (LOGO) responses
768 * @sp: The sequence the LOGO was on
769 * @fp: The LOGO response frame
770 * @rdata_arg: The remote port that sent the LOGO response
42e9a92f
RL
771 *
772 * Locking Note: This function will be called without the rport lock
773 * held, but it will lock, call an _enter_* function or fc_rport_error
774 * and then unlock the rport.
775 */
776static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
9fb9d328 777 void *rdata_arg)
42e9a92f 778{
9fb9d328 779 struct fc_rport_priv *rdata = rdata_arg;
42e9a92f
RL
780 u8 op;
781
782 mutex_lock(&rdata->rp_mutex);
783
f657d299 784 FC_RPORT_DBG(rdata, "Received a LOGO %s\n", fc_els_resp_type(fp));
42e9a92f 785
42e9a92f 786 if (rdata->rp_state != RPORT_ST_LOGO) {
9fb9d328
JE
787 FC_RPORT_DBG(rdata, "Received a LOGO response, but in state "
788 "%s\n", fc_rport_state(rdata));
76f6804e
AJ
789 if (IS_ERR(fp))
790 goto err;
42e9a92f
RL
791 goto out;
792 }
793
76f6804e 794 if (IS_ERR(fp)) {
9fb9d328 795 fc_rport_error_retry(rdata, fp);
76f6804e
AJ
796 goto err;
797 }
798
42e9a92f 799 op = fc_frame_payload_op(fp);
68a1750b
JE
800 if (op != ELS_LS_ACC)
801 FC_RPORT_DBG(rdata, "Bad ELS response op %x for LOGO command\n",
802 op);
803 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
42e9a92f
RL
804
805out:
806 fc_frame_free(fp);
807err:
808 mutex_unlock(&rdata->rp_mutex);
f211fa51 809 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
42e9a92f
RL
810}
811
812/**
3a3b42bf
RL
813 * fc_rport_enter_prli() - Send Process Login (PRLI) request
814 * @rdata: The remote port to send the PRLI request to
42e9a92f
RL
815 *
816 * Locking Note: The rport lock is expected to be held before calling
817 * this routine.
818 */
9fb9d328 819static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
42e9a92f 820{
42e9a92f
RL
821 struct fc_lport *lport = rdata->local_port;
822 struct {
823 struct fc_els_prli prli;
824 struct fc_els_spp spp;
825 } *pp;
826 struct fc_frame *fp;
827
3ac6f98f
JE
828 /*
829 * If the rport is one of the well known addresses
830 * we skip PRLI and RTV and go straight to READY.
831 */
832 if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
833 fc_rport_enter_ready(rdata);
834 return;
835 }
836
9fb9d328
JE
837 FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
838 fc_rport_state(rdata));
42e9a92f 839
9fb9d328 840 fc_rport_state_enter(rdata, RPORT_ST_PRLI);
42e9a92f
RL
841
842 fp = fc_frame_alloc(lport, sizeof(*pp));
843 if (!fp) {
9fb9d328 844 fc_rport_error_retry(rdata, fp);
42e9a92f
RL
845 return;
846 }
847
f211fa51 848 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PRLI,
b94f8951
JE
849 fc_rport_prli_resp, rdata,
850 2 * lport->r_a_tov))
8f550f93 851 fc_rport_error_retry(rdata, NULL);
42e9a92f 852 else
f211fa51 853 kref_get(&rdata->kref);
42e9a92f
RL
854}
855
856/**
3a3b42bf
RL
857 * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses
858 * @sp: The sequence the RTV was on
859 * @fp: The RTV response frame
860 * @rdata_arg: The remote port that sent the RTV response
42e9a92f
RL
861 *
862 * Many targets don't seem to support this.
863 *
864 * Locking Note: This function will be called without the rport lock
865 * held, but it will lock, call an _enter_* function or fc_rport_error
866 * and then unlock the rport.
867 */
868static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
9fb9d328 869 void *rdata_arg)
42e9a92f 870{
9fb9d328 871 struct fc_rport_priv *rdata = rdata_arg;
42e9a92f
RL
872 u8 op;
873
874 mutex_lock(&rdata->rp_mutex);
875
f657d299 876 FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
42e9a92f
RL
877
878 if (rdata->rp_state != RPORT_ST_RTV) {
9fb9d328
JE
879 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
880 "%s\n", fc_rport_state(rdata));
76f6804e
AJ
881 if (IS_ERR(fp))
882 goto err;
42e9a92f
RL
883 goto out;
884 }
885
76f6804e 886 if (IS_ERR(fp)) {
9fb9d328 887 fc_rport_error(rdata, fp);
76f6804e
AJ
888 goto err;
889 }
890
42e9a92f
RL
891 op = fc_frame_payload_op(fp);
892 if (op == ELS_LS_ACC) {
893 struct fc_els_rtv_acc *rtv;
894 u32 toq;
895 u32 tov;
896
897 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
898 if (rtv) {
899 toq = ntohl(rtv->rtv_toq);
900 tov = ntohl(rtv->rtv_r_a_tov);
901 if (tov == 0)
902 tov = 1;
903 rdata->r_a_tov = tov;
904 tov = ntohl(rtv->rtv_e_d_tov);
905 if (toq & FC_ELS_RTV_EDRES)
906 tov /= 1000000;
907 if (tov == 0)
908 tov = 1;
909 rdata->e_d_tov = tov;
910 }
911 }
912
9fb9d328 913 fc_rport_enter_ready(rdata);
42e9a92f
RL
914
915out:
916 fc_frame_free(fp);
917err:
918 mutex_unlock(&rdata->rp_mutex);
f211fa51 919 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
42e9a92f
RL
920}
921
922/**
3a3b42bf
RL
923 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
924 * @rdata: The remote port to send the RTV request to
42e9a92f
RL
925 *
926 * Locking Note: The rport lock is expected to be held before calling
927 * this routine.
928 */
9fb9d328 929static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
42e9a92f
RL
930{
931 struct fc_frame *fp;
42e9a92f
RL
932 struct fc_lport *lport = rdata->local_port;
933
9fb9d328
JE
934 FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
935 fc_rport_state(rdata));
42e9a92f 936
9fb9d328 937 fc_rport_state_enter(rdata, RPORT_ST_RTV);
42e9a92f
RL
938
939 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
940 if (!fp) {
9fb9d328 941 fc_rport_error_retry(rdata, fp);
42e9a92f
RL
942 return;
943 }
944
f211fa51 945 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
b94f8951
JE
946 fc_rport_rtv_resp, rdata,
947 2 * lport->r_a_tov))
8f550f93 948 fc_rport_error_retry(rdata, NULL);
42e9a92f 949 else
f211fa51 950 kref_get(&rdata->kref);
42e9a92f
RL
951}
952
953/**
3a3b42bf
RL
954 * fc_rport_enter_logo() - Send a logout (LOGO) request
955 * @rdata: The remote port to send the LOGO request to
42e9a92f
RL
956 *
957 * Locking Note: The rport lock is expected to be held before calling
958 * this routine.
959 */
9fb9d328 960static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
42e9a92f 961{
42e9a92f
RL
962 struct fc_lport *lport = rdata->local_port;
963 struct fc_frame *fp;
964
9fb9d328
JE
965 FC_RPORT_DBG(rdata, "Port entered LOGO state from %s state\n",
966 fc_rport_state(rdata));
42e9a92f 967
9fb9d328 968 fc_rport_state_enter(rdata, RPORT_ST_LOGO);
42e9a92f
RL
969
970 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
971 if (!fp) {
9fb9d328 972 fc_rport_error_retry(rdata, fp);
42e9a92f
RL
973 return;
974 }
975
f211fa51 976 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
b94f8951
JE
977 fc_rport_logo_resp, rdata,
978 2 * lport->r_a_tov))
8f550f93 979 fc_rport_error_retry(rdata, NULL);
42e9a92f 980 else
f211fa51 981 kref_get(&rdata->kref);
42e9a92f
RL
982}
983
370c3bd0 984/**
3a3b42bf
RL
985 * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
986 * @sp: The sequence the ADISC response was on
987 * @fp: The ADISC response frame
988 * @rdata_arg: The remote port that sent the ADISC response
370c3bd0
JE
989 *
990 * Locking Note: This function will be called without the rport lock
991 * held, but it will lock, call an _enter_* function or fc_rport_error
992 * and then unlock the rport.
993 */
994static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
3a3b42bf 995 void *rdata_arg)
370c3bd0
JE
996{
997 struct fc_rport_priv *rdata = rdata_arg;
998 struct fc_els_adisc *adisc;
999 u8 op;
1000
1001 mutex_lock(&rdata->rp_mutex);
1002
1003 FC_RPORT_DBG(rdata, "Received a ADISC response\n");
1004
1005 if (rdata->rp_state != RPORT_ST_ADISC) {
1006 FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
1007 fc_rport_state(rdata));
1008 if (IS_ERR(fp))
1009 goto err;
1010 goto out;
1011 }
1012
1013 if (IS_ERR(fp)) {
1014 fc_rport_error(rdata, fp);
1015 goto err;
1016 }
1017
1018 /*
1019 * If address verification failed. Consider us logged out of the rport.
1020 * Since the rport is still in discovery, we want to be
1021 * logged in, so go to PLOGI state. Otherwise, go back to READY.
1022 */
1023 op = fc_frame_payload_op(fp);
1024 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1025 if (op != ELS_LS_ACC || !adisc ||
1026 ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
1027 get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
1028 get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
1029 FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
1030 fc_rport_enter_plogi(rdata);
1031 } else {
1032 FC_RPORT_DBG(rdata, "ADISC OK\n");
1033 fc_rport_enter_ready(rdata);
1034 }
1035out:
1036 fc_frame_free(fp);
1037err:
1038 mutex_unlock(&rdata->rp_mutex);
1039 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1040}
1041
1042/**
3a3b42bf
RL
1043 * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
1044 * @rdata: The remote port to send the ADISC request to
370c3bd0
JE
1045 *
1046 * Locking Note: The rport lock is expected to be held before calling
1047 * this routine.
1048 */
1049static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
1050{
1051 struct fc_lport *lport = rdata->local_port;
1052 struct fc_frame *fp;
1053
1054 FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
1055 fc_rport_state(rdata));
1056
1057 fc_rport_state_enter(rdata, RPORT_ST_ADISC);
1058
1059 fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
1060 if (!fp) {
1061 fc_rport_error_retry(rdata, fp);
1062 return;
1063 }
1064 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
b94f8951
JE
1065 fc_rport_adisc_resp, rdata,
1066 2 * lport->r_a_tov))
8f550f93 1067 fc_rport_error_retry(rdata, NULL);
370c3bd0
JE
1068 else
1069 kref_get(&rdata->kref);
1070}
1071
8abbe3a4 1072/**
3a3b42bf
RL
1073 * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
1074 * @rdata: The remote port that sent the ADISC request
1075 * @sp: The sequence the ADISC request was on
1076 * @in_fp: The ADISC request frame
8abbe3a4
JE
1077 *
1078 * Locking Note: Called with the lport and rport locks held.
1079 */
1080static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
1081 struct fc_seq *sp, struct fc_frame *in_fp)
1082{
1083 struct fc_lport *lport = rdata->local_port;
1084 struct fc_frame *fp;
1085 struct fc_exch *ep = fc_seq_exch(sp);
1086 struct fc_els_adisc *adisc;
1087 struct fc_seq_els_data rjt_data;
1088 u32 f_ctl;
1089
1090 FC_RPORT_DBG(rdata, "Received ADISC request\n");
1091
1092 adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
1093 if (!adisc) {
1094 rjt_data.fp = NULL;
1095 rjt_data.reason = ELS_RJT_PROT;
1096 rjt_data.explan = ELS_EXPL_INV_LEN;
1097 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1098 goto drop;
1099 }
1100
1101 fp = fc_frame_alloc(lport, sizeof(*adisc));
1102 if (!fp)
1103 goto drop;
1104 fc_adisc_fill(lport, fp);
1105 adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1106 adisc->adisc_cmd = ELS_LS_ACC;
1107 sp = lport->tt.seq_start_next(sp);
1108 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1109 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1110 FC_TYPE_ELS, f_ctl, 0);
1111 lport->tt.seq_send(lport, sp, fp);
1112drop:
1113 fc_frame_free(in_fp);
1114}
1115
63e27fb8
YZ
1116/**
1117 * fc_rport_recv_rls_req() - Handle received Read Link Status request
1118 * @rdata: The remote port that sent the RLS request
1119 * @sp: The sequence that the RLS was on
1120 * @rx_fp: The PRLI request frame
1121 *
1122 * Locking Note: The rport lock is expected to be held before calling
1123 * this function.
1124 */
1125static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
1126 struct fc_seq *sp, struct fc_frame *rx_fp)
1127
1128{
1129 struct fc_lport *lport = rdata->local_port;
1130 struct fc_frame *fp;
1131 struct fc_exch *ep = fc_seq_exch(sp);
1132 struct fc_els_rls *rls;
1133 struct fc_els_rls_resp *rsp;
1134 struct fc_els_lesb *lesb;
1135 struct fc_seq_els_data rjt_data;
1136 struct fc_host_statistics *hst;
1137 u32 f_ctl;
1138
1139 FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
1140 fc_rport_state(rdata));
1141
1142 rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
1143 if (!rls) {
1144 rjt_data.reason = ELS_RJT_PROT;
1145 rjt_data.explan = ELS_EXPL_INV_LEN;
1146 goto out_rjt;
1147 }
1148
1149 fp = fc_frame_alloc(lport, sizeof(*rsp));
1150 if (!fp) {
1151 rjt_data.reason = ELS_RJT_UNAB;
1152 rjt_data.explan = ELS_EXPL_INSUF_RES;
1153 goto out_rjt;
1154 }
1155
1156 rsp = fc_frame_payload_get(fp, sizeof(*rsp));
1157 memset(rsp, 0, sizeof(*rsp));
1158 rsp->rls_cmd = ELS_LS_ACC;
1159 lesb = &rsp->rls_lesb;
1160 if (lport->tt.get_lesb) {
1161 /* get LESB from LLD if it supports it */
1162 lport->tt.get_lesb(lport, lesb);
1163 } else {
1164 fc_get_host_stats(lport->host);
1165 hst = &lport->host_stats;
1166 lesb->lesb_link_fail = htonl(hst->link_failure_count);
1167 lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
1168 lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
1169 lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
1170 lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
1171 lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
1172 }
1173
1174 sp = lport->tt.seq_start_next(sp);
1175 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
1176 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1177 FC_TYPE_ELS, f_ctl, 0);
1178 lport->tt.seq_send(lport, sp, fp);
1179 goto out;
1180
1181out_rjt:
1182 rjt_data.fp = NULL;
1183 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1184out:
1185 fc_frame_free(rx_fp);
1186}
1187
42e9a92f 1188/**
3a3b42bf
RL
1189 * fc_rport_recv_els_req() - Handler for validated ELS requests
1190 * @lport: The local port that received the ELS request
1191 * @sp: The sequence that the ELS request was on
1192 * @fp: The ELS request frame
83fe6a93
JE
1193 *
1194 * Handle incoming ELS requests that require port login.
1195 * The ELS opcode has already been validated by the caller.
42e9a92f 1196 *
131203a1 1197 * Locking Note: Called with the lport lock held.
42e9a92f 1198 */
83fe6a93
JE
1199static void fc_rport_recv_els_req(struct fc_lport *lport,
1200 struct fc_seq *sp, struct fc_frame *fp)
42e9a92f 1201{
131203a1 1202 struct fc_rport_priv *rdata;
42e9a92f
RL
1203 struct fc_frame_header *fh;
1204 struct fc_seq_els_data els_data;
42e9a92f 1205
42e9a92f 1206 els_data.fp = NULL;
83fe6a93
JE
1207 els_data.reason = ELS_RJT_UNAB;
1208 els_data.explan = ELS_EXPL_PLOGI_REQD;
3ac6f98f 1209
42e9a92f
RL
1210 fh = fc_frame_header_get(fp);
1211
25b37b98 1212 mutex_lock(&lport->disc.disc_mutex);
83fe6a93 1213 rdata = lport->tt.rport_lookup(lport, ntoh24(fh->fh_s_id));
131203a1 1214 if (!rdata) {
25b37b98 1215 mutex_unlock(&lport->disc.disc_mutex);
83fe6a93 1216 goto reject;
131203a1
JE
1217 }
1218 mutex_lock(&rdata->rp_mutex);
25b37b98 1219 mutex_unlock(&lport->disc.disc_mutex);
131203a1 1220
83fe6a93
JE
1221 switch (rdata->rp_state) {
1222 case RPORT_ST_PRLI:
1223 case RPORT_ST_RTV:
1224 case RPORT_ST_READY:
370c3bd0 1225 case RPORT_ST_ADISC:
83fe6a93
JE
1226 break;
1227 default:
1228 mutex_unlock(&rdata->rp_mutex);
1229 goto reject;
1230 }
1231
1232 switch (fc_frame_payload_op(fp)) {
131203a1
JE
1233 case ELS_PRLI:
1234 fc_rport_recv_prli_req(rdata, sp, fp);
1235 break;
1236 case ELS_PRLO:
1237 fc_rport_recv_prlo_req(rdata, sp, fp);
1238 break;
8abbe3a4
JE
1239 case ELS_ADISC:
1240 fc_rport_recv_adisc_req(rdata, sp, fp);
1241 break;
131203a1
JE
1242 case ELS_RRQ:
1243 els_data.fp = fp;
1244 lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
1245 break;
1246 case ELS_REC:
1247 els_data.fp = fp;
1248 lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
1249 break;
63e27fb8
YZ
1250 case ELS_RLS:
1251 fc_rport_recv_rls_req(rdata, sp, fp);
1252 break;
131203a1 1253 default:
83fe6a93 1254 fc_frame_free(fp); /* can't happen */
131203a1 1255 break;
42e9a92f
RL
1256 }
1257
1258 mutex_unlock(&rdata->rp_mutex);
83fe6a93
JE
1259 return;
1260
1261reject:
1262 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
1263 fc_frame_free(fp);
1264}
1265
1266/**
3a3b42bf
RL
1267 * fc_rport_recv_req() - Handler for requests
1268 * @sp: The sequence the request was on
1269 * @fp: The request frame
1270 * @lport: The local port that received the request
83fe6a93
JE
1271 *
1272 * Locking Note: Called with the lport lock held.
1273 */
1274void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
1275 struct fc_lport *lport)
1276{
1277 struct fc_seq_els_data els_data;
1278
1279 /*
1280 * Handle PLOGI and LOGO requests separately, since they
1281 * don't require prior login.
1282 * Check for unsupported opcodes first and reject them.
1283 * For some ops, it would be incorrect to reject with "PLOGI required".
1284 */
1285 switch (fc_frame_payload_op(fp)) {
1286 case ELS_PLOGI:
1287 fc_rport_recv_plogi_req(lport, sp, fp);
1288 break;
1289 case ELS_LOGO:
1290 fc_rport_recv_logo_req(lport, sp, fp);
1291 break;
1292 case ELS_PRLI:
1293 case ELS_PRLO:
8abbe3a4 1294 case ELS_ADISC:
83fe6a93
JE
1295 case ELS_RRQ:
1296 case ELS_REC:
63e27fb8 1297 case ELS_RLS:
83fe6a93
JE
1298 fc_rport_recv_els_req(lport, sp, fp);
1299 break;
1300 default:
1301 fc_frame_free(fp);
1302 els_data.fp = NULL;
1303 els_data.reason = ELS_RJT_UNSUP;
1304 els_data.explan = ELS_EXPL_NONE;
1305 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
1306 break;
1307 }
42e9a92f
RL
1308}
1309
1310/**
3a3b42bf
RL
1311 * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
1312 * @lport: The local port that received the PLOGI request
1313 * @sp: The sequence that the PLOGI request was on
1314 * @rx_fp: The PLOGI request frame
42e9a92f 1315 *
3ac6f98f 1316 * Locking Note: The rport lock is held before calling this function.
42e9a92f 1317 */
3ac6f98f 1318static void fc_rport_recv_plogi_req(struct fc_lport *lport,
42e9a92f
RL
1319 struct fc_seq *sp, struct fc_frame *rx_fp)
1320{
3ac6f98f
JE
1321 struct fc_disc *disc;
1322 struct fc_rport_priv *rdata;
42e9a92f
RL
1323 struct fc_frame *fp = rx_fp;
1324 struct fc_exch *ep;
1325 struct fc_frame_header *fh;
1326 struct fc_els_flogi *pl;
1327 struct fc_seq_els_data rjt_data;
3ac6f98f 1328 u32 sid, f_ctl;
42e9a92f 1329
3ac6f98f 1330 rjt_data.fp = NULL;
42e9a92f 1331 fh = fc_frame_header_get(fp);
3ac6f98f 1332 sid = ntoh24(fh->fh_s_id);
42e9a92f 1333
3ac6f98f 1334 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
42e9a92f 1335
42e9a92f
RL
1336 pl = fc_frame_payload_get(fp, sizeof(*pl));
1337 if (!pl) {
3ac6f98f
JE
1338 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
1339 rjt_data.reason = ELS_RJT_PROT;
1340 rjt_data.explan = ELS_EXPL_INV_LEN;
1341 goto reject;
42e9a92f 1342 }
3ac6f98f
JE
1343
1344 disc = &lport->disc;
1345 mutex_lock(&disc->disc_mutex);
1346 rdata = lport->tt.rport_create(lport, sid);
1347 if (!rdata) {
1348 mutex_unlock(&disc->disc_mutex);
1349 rjt_data.reason = ELS_RJT_UNAB;
1350 rjt_data.explan = ELS_EXPL_INSUF_RES;
1351 goto reject;
1352 }
1353
1354 mutex_lock(&rdata->rp_mutex);
1355 mutex_unlock(&disc->disc_mutex);
1356
1357 rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1358 rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
42e9a92f
RL
1359
1360 /*
3ac6f98f 1361 * If the rport was just created, possibly due to the incoming PLOGI,
42e9a92f
RL
1362 * set the state appropriately and accept the PLOGI.
1363 *
1364 * If we had also sent a PLOGI, and if the received PLOGI is from a
1365 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1366 * "command already in progress".
1367 *
1368 * XXX TBD: If the session was ready before, the PLOGI should result in
1369 * all outstanding exchanges being reset.
1370 */
1371 switch (rdata->rp_state) {
1372 case RPORT_ST_INIT:
3ac6f98f 1373 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
42e9a92f
RL
1374 break;
1375 case RPORT_ST_PLOGI:
3ac6f98f
JE
1376 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1377 if (rdata->ids.port_name < lport->wwpn) {
1378 mutex_unlock(&rdata->rp_mutex);
1379 rjt_data.reason = ELS_RJT_INPROG;
1380 rjt_data.explan = ELS_EXPL_NONE;
1381 goto reject;
1382 }
42e9a92f
RL
1383 break;
1384 case RPORT_ST_PRLI:
b4a9c7ed 1385 case RPORT_ST_RTV:
42e9a92f 1386 case RPORT_ST_READY:
370c3bd0
JE
1387 case RPORT_ST_ADISC:
1388 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
1389 "- ignored for now\n", rdata->rp_state);
1390 /* XXX TBD - should reset */
42e9a92f 1391 break;
14194054 1392 case RPORT_ST_DELETE:
b4a9c7ed 1393 case RPORT_ST_LOGO:
b4a9c7ed
JE
1394 FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
1395 fc_rport_state(rdata));
1396 mutex_unlock(&rdata->rp_mutex);
1397 rjt_data.reason = ELS_RJT_BUSY;
1398 rjt_data.explan = ELS_EXPL_NONE;
1399 goto reject;
42e9a92f
RL
1400 }
1401
3ac6f98f
JE
1402 /*
1403 * Get session payload size from incoming PLOGI.
1404 */
1405 rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
1406 fc_frame_free(rx_fp);
1407
1408 /*
1409 * Send LS_ACC. If this fails, the originator should retry.
1410 */
1411 sp = lport->tt.seq_start_next(sp);
1412 if (!sp)
1413 goto out;
1414 fp = fc_frame_alloc(lport, sizeof(*pl));
1415 if (!fp)
1416 goto out;
1417
1418 fc_plogi_fill(lport, fp, ELS_LS_ACC);
1419 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1420 ep = fc_seq_exch(sp);
1421 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1422 FC_TYPE_ELS, f_ctl, 0);
1423 lport->tt.seq_send(lport, sp, fp);
1424 fc_rport_enter_prli(rdata);
1425out:
1426 mutex_unlock(&rdata->rp_mutex);
1427 return;
1428
1429reject:
1430 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1431 fc_frame_free(fp);
42e9a92f
RL
1432}
1433
1434/**
3a3b42bf
RL
1435 * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
1436 * @rdata: The remote port that sent the PRLI request
1437 * @sp: The sequence that the PRLI was on
1438 * @rx_fp: The PRLI request frame
42e9a92f
RL
1439 *
1440 * Locking Note: The rport lock is exected to be held before calling
1441 * this function.
1442 */
9fb9d328 1443static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
42e9a92f
RL
1444 struct fc_seq *sp, struct fc_frame *rx_fp)
1445{
42e9a92f
RL
1446 struct fc_lport *lport = rdata->local_port;
1447 struct fc_exch *ep;
1448 struct fc_frame *fp;
1449 struct fc_frame_header *fh;
1450 struct {
1451 struct fc_els_prli prli;
1452 struct fc_els_spp spp;
1453 } *pp;
1454 struct fc_els_spp *rspp; /* request service param page */
1455 struct fc_els_spp *spp; /* response spp */
1456 unsigned int len;
1457 unsigned int plen;
42e9a92f
RL
1458 enum fc_els_spp_resp resp;
1459 struct fc_seq_els_data rjt_data;
1460 u32 f_ctl;
1461 u32 fcp_parm;
1462 u32 roles = FC_RPORT_ROLE_UNKNOWN;
42e9a92f 1463
a2f6a024 1464 rjt_data.fp = NULL;
42e9a92f
RL
1465 fh = fc_frame_header_get(rx_fp);
1466
9fb9d328
JE
1467 FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1468 fc_rport_state(rdata));
42e9a92f 1469
42e9a92f
RL
1470 len = fr_len(rx_fp) - sizeof(*fh);
1471 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
a2f6a024
JE
1472 if (!pp)
1473 goto reject_len;
1474 plen = ntohs(pp->prli.prli_len);
1475 if ((plen % 4) != 0 || plen > len || plen < 16)
1476 goto reject_len;
1477 if (plen < len)
1478 len = plen;
1479 plen = pp->prli.prli_spp_len;
1480 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1481 plen > len || len < sizeof(*pp) || plen < 12)
1482 goto reject_len;
1483 rspp = &pp->spp;
1484
1485 fp = fc_frame_alloc(lport, len);
1486 if (!fp) {
1487 rjt_data.reason = ELS_RJT_UNAB;
1488 rjt_data.explan = ELS_EXPL_INSUF_RES;
1489 goto reject;
42e9a92f 1490 }
a2f6a024
JE
1491 sp = lport->tt.seq_start_next(sp);
1492 WARN_ON(!sp);
1493 pp = fc_frame_payload_get(fp, len);
1494 WARN_ON(!pp);
1495 memset(pp, 0, len);
1496 pp->prli.prli_cmd = ELS_LS_ACC;
1497 pp->prli.prli_spp_len = plen;
1498 pp->prli.prli_len = htons(len);
1499 len -= sizeof(struct fc_els_prli);
42e9a92f 1500
a2f6a024
JE
1501 /* reinitialize remote port roles */
1502 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
42e9a92f 1503
a2f6a024
JE
1504 /*
1505 * Go through all the service parameter pages and build
1506 * response. If plen indicates longer SPP than standard,
1507 * use that. The entire response has been pre-cleared above.
1508 */
1509 spp = &pp->spp;
1510 while (len >= plen) {
1511 spp->spp_type = rspp->spp_type;
1512 spp->spp_type_ext = rspp->spp_type_ext;
1513 spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
1514 resp = FC_SPP_RESP_ACK;
1515
1516 switch (rspp->spp_type) {
1517 case 0: /* common to all FC-4 types */
42e9a92f 1518 break;
a2f6a024
JE
1519 case FC_TYPE_FCP:
1520 fcp_parm = ntohl(rspp->spp_params);
1521 if (fcp_parm & FCP_SPPF_RETRY)
1522 rdata->flags |= FC_RP_FLAGS_RETRY;
1523 rdata->supported_classes = FC_COS_CLASS3;
1524 if (fcp_parm & FCP_SPPF_INIT_FCN)
1525 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1526 if (fcp_parm & FCP_SPPF_TARG_FCN)
1527 roles |= FC_RPORT_ROLE_FCP_TARGET;
1528 rdata->ids.roles = roles;
1529
1530 spp->spp_params = htonl(lport->service_params);
42e9a92f
RL
1531 break;
1532 default:
a2f6a024 1533 resp = FC_SPP_RESP_INVL;
42e9a92f
RL
1534 break;
1535 }
a2f6a024
JE
1536 spp->spp_flags |= resp;
1537 len -= plen;
1538 rspp = (struct fc_els_spp *)((char *)rspp + plen);
1539 spp = (struct fc_els_spp *)((char *)spp + plen);
1540 }
1541
1542 /*
1543 * Send LS_ACC. If this fails, the originator should retry.
1544 */
1545 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1546 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1547 ep = fc_seq_exch(sp);
1548 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1549 FC_TYPE_ELS, f_ctl, 0);
1550 lport->tt.seq_send(lport, sp, fp);
1551
1552 switch (rdata->rp_state) {
1553 case RPORT_ST_PRLI:
1554 fc_rport_enter_ready(rdata);
1555 break;
1556 default:
1557 break;
42e9a92f 1558 }
a2f6a024
JE
1559 goto drop;
1560
1561reject_len:
1562 rjt_data.reason = ELS_RJT_PROT;
1563 rjt_data.explan = ELS_EXPL_INV_LEN;
1564reject:
1565 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1566drop:
42e9a92f
RL
1567 fc_frame_free(rx_fp);
1568}
1569
1570/**
3a3b42bf
RL
1571 * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
1572 * @rdata: The remote port that sent the PRLO request
1573 * @sp: The sequence that the PRLO was on
f8fc6c2c 1574 * @rx_fp: The PRLO request frame
42e9a92f
RL
1575 *
1576 * Locking Note: The rport lock is exected to be held before calling
1577 * this function.
1578 */
9fb9d328
JE
1579static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
1580 struct fc_seq *sp,
f8fc6c2c 1581 struct fc_frame *rx_fp)
42e9a92f 1582{
42e9a92f 1583 struct fc_lport *lport = rdata->local_port;
42e9a92f 1584 struct fc_frame_header *fh;
f8fc6c2c
BPG
1585 struct fc_exch *ep;
1586 struct fc_frame *fp;
1587 struct {
1588 struct fc_els_prlo prlo;
1589 struct fc_els_spp spp;
1590 } *pp;
1591 struct fc_els_spp *rspp; /* request service param page */
1592 struct fc_els_spp *spp; /* response spp */
1593 unsigned int len;
1594 unsigned int plen;
1595 u32 f_ctl;
42e9a92f
RL
1596 struct fc_seq_els_data rjt_data;
1597
f8fc6c2c
BPG
1598 rjt_data.fp = NULL;
1599 fh = fc_frame_header_get(rx_fp);
42e9a92f 1600
9fb9d328
JE
1601 FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
1602 fc_rport_state(rdata));
42e9a92f 1603
f8fc6c2c
BPG
1604 len = fr_len(rx_fp) - sizeof(*fh);
1605 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1606 if (!pp)
1607 goto reject_len;
1608 plen = ntohs(pp->prlo.prlo_len);
1609 if (plen != 20)
1610 goto reject_len;
1611 if (plen < len)
1612 len = plen;
1613
1614 rspp = &pp->spp;
1615
1616 fp = fc_frame_alloc(lport, len);
1617 if (!fp) {
1618 rjt_data.reason = ELS_RJT_UNAB;
1619 rjt_data.explan = ELS_EXPL_INSUF_RES;
1620 goto reject;
1621 }
1622
1623 sp = lport->tt.seq_start_next(sp);
1624 WARN_ON(!sp);
1625 pp = fc_frame_payload_get(fp, len);
1626 WARN_ON(!pp);
1627 memset(pp, 0, len);
1628 pp->prlo.prlo_cmd = ELS_LS_ACC;
1629 pp->prlo.prlo_obs = 0x10;
1630 pp->prlo.prlo_len = htons(len);
1631 spp = &pp->spp;
1632 spp->spp_type = rspp->spp_type;
1633 spp->spp_type_ext = rspp->spp_type_ext;
1634 spp->spp_flags = FC_SPP_RESP_ACK;
1635
1636 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
1637
1638 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1639 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1640 ep = fc_seq_exch(sp);
1641 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1642 FC_TYPE_ELS, f_ctl, 0);
1643 lport->tt.seq_send(lport, sp, fp);
1644 goto drop;
1645
1646reject_len:
1647 rjt_data.reason = ELS_RJT_PROT;
1648 rjt_data.explan = ELS_EXPL_INV_LEN;
1649reject:
42e9a92f 1650 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
f8fc6c2c
BPG
1651drop:
1652 fc_frame_free(rx_fp);
42e9a92f
RL
1653}
1654
1655/**
3a3b42bf
RL
1656 * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
1657 * @lport: The local port that received the LOGO request
1658 * @sp: The sequence that the LOGO request was on
1659 * @fp: The LOGO request frame
42e9a92f
RL
1660 *
1661 * Locking Note: The rport lock is exected to be held before calling
1662 * this function.
1663 */
83fe6a93 1664static void fc_rport_recv_logo_req(struct fc_lport *lport,
9fb9d328 1665 struct fc_seq *sp,
42e9a92f
RL
1666 struct fc_frame *fp)
1667{
1668 struct fc_frame_header *fh;
83fe6a93
JE
1669 struct fc_rport_priv *rdata;
1670 u32 sid;
42e9a92f 1671
feab4ae7
JE
1672 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
1673
42e9a92f 1674 fh = fc_frame_header_get(fp);
83fe6a93 1675 sid = ntoh24(fh->fh_s_id);
42e9a92f 1676
83fe6a93
JE
1677 mutex_lock(&lport->disc.disc_mutex);
1678 rdata = lport->tt.rport_lookup(lport, sid);
1679 if (rdata) {
1680 mutex_lock(&rdata->rp_mutex);
1681 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
1682 fc_rport_state(rdata));
feab4ae7 1683
b4a9c7ed 1684 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
83fe6a93
JE
1685 mutex_unlock(&rdata->rp_mutex);
1686 } else
1687 FC_RPORT_ID_DBG(lport, sid,
1688 "Received LOGO from non-logged-in port\n");
1689 mutex_unlock(&lport->disc.disc_mutex);
42e9a92f
RL
1690 fc_frame_free(fp);
1691}
1692
3a3b42bf
RL
1693/**
1694 * fc_rport_flush_queue() - Flush the rport_event_queue
1695 */
42e9a92f
RL
1696static void fc_rport_flush_queue(void)
1697{
1698 flush_workqueue(rport_event_queue);
1699}
1700
3a3b42bf
RL
1701/**
1702 * fc_rport_init() - Initialize the remote port layer for a local port
1703 * @lport: The local port to initialize the remote port layer for
1704 */
42e9a92f
RL
1705int fc_rport_init(struct fc_lport *lport)
1706{
8025b5db
JE
1707 if (!lport->tt.rport_lookup)
1708 lport->tt.rport_lookup = fc_rport_lookup;
1709
5101ff99 1710 if (!lport->tt.rport_create)
9e9d0452 1711 lport->tt.rport_create = fc_rport_create;
5101ff99 1712
42e9a92f
RL
1713 if (!lport->tt.rport_login)
1714 lport->tt.rport_login = fc_rport_login;
1715
1716 if (!lport->tt.rport_logoff)
1717 lport->tt.rport_logoff = fc_rport_logoff;
1718
1719 if (!lport->tt.rport_recv_req)
1720 lport->tt.rport_recv_req = fc_rport_recv_req;
1721
1722 if (!lport->tt.rport_flush_queue)
1723 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1724
f211fa51
JE
1725 if (!lport->tt.rport_destroy)
1726 lport->tt.rport_destroy = fc_rport_destroy;
1727
42e9a92f
RL
1728 return 0;
1729}
1730EXPORT_SYMBOL(fc_rport_init);
1731
3a3b42bf
RL
1732/**
1733 * fc_setup_rport() - Initialize the rport_event_queue
1734 */
1735int fc_setup_rport()
42e9a92f
RL
1736{
1737 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
1738 if (!rport_event_queue)
1739 return -ENOMEM;
1740 return 0;
1741}
42e9a92f 1742
3a3b42bf
RL
1743/**
1744 * fc_destroy_rport() - Destroy the rport_event_queue
1745 */
1746void fc_destroy_rport()
42e9a92f
RL
1747{
1748 destroy_workqueue(rport_event_queue);
1749}
42e9a92f 1750
3a3b42bf
RL
1751/**
1752 * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
1753 * @rport: The remote port whose I/O should be terminated
1754 */
42e9a92f
RL
1755void fc_rport_terminate_io(struct fc_rport *rport)
1756{
3a3b42bf
RL
1757 struct fc_rport_libfc_priv *rpriv = rport->dd_data;
1758 struct fc_lport *lport = rpriv->local_port;
42e9a92f 1759
1f6ff364
AJ
1760 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
1761 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
42e9a92f
RL
1762}
1763EXPORT_SYMBOL(fc_rport_terminate_io);