blob: 5f8f437e76b335c78a894fab7d9d3887c5fba2bc [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 Love42e9a922008-12-09 15:10:17 -080058struct workqueue_struct *rport_event_queue;
59
60static void fc_rport_enter_plogi(struct fc_rport *);
61static void fc_rport_enter_prli(struct fc_rport *);
62static void fc_rport_enter_rtv(struct fc_rport *);
63static void fc_rport_enter_ready(struct fc_rport *);
64static void fc_rport_enter_logo(struct fc_rport *);
65
66static void fc_rport_recv_plogi_req(struct fc_rport *,
67 struct fc_seq *, struct fc_frame *);
68static void fc_rport_recv_prli_req(struct fc_rport *,
69 struct fc_seq *, struct fc_frame *);
70static void fc_rport_recv_prlo_req(struct fc_rport *,
71 struct fc_seq *, struct fc_frame *);
72static void fc_rport_recv_logo_req(struct fc_rport *,
73 struct fc_seq *, struct fc_frame *);
74static void fc_rport_timeout(struct work_struct *);
75static void fc_rport_error(struct fc_rport *, struct fc_frame *);
Chris Leech6755db12009-02-27 10:55:02 -080076static void fc_rport_error_retry(struct fc_rport *, struct fc_frame *);
Robert Love42e9a922008-12-09 15:10:17 -080077static void fc_rport_work(struct work_struct *);
78
79static const char *fc_rport_state_names[] = {
Robert Love42e9a922008-12-09 15:10:17 -080080 [RPORT_ST_INIT] = "Init",
81 [RPORT_ST_PLOGI] = "PLOGI",
82 [RPORT_ST_PRLI] = "PRLI",
83 [RPORT_ST_RTV] = "RTV",
84 [RPORT_ST_READY] = "Ready",
85 [RPORT_ST_LOGO] = "LOGO",
Joe Eykholt14194052009-07-29 17:04:43 -070086 [RPORT_ST_DELETE] = "Delete",
Robert Love42e9a922008-12-09 15:10:17 -080087};
88
89static void fc_rport_rogue_destroy(struct device *dev)
90{
91 struct fc_rport *rport = dev_to_rport(dev);
Robert Love74147052009-06-10 15:31:10 -070092 FC_RPORT_DBG(rport, "Destroying rogue rport\n");
Robert Love42e9a922008-12-09 15:10:17 -080093 kfree(rport);
94}
95
96struct fc_rport *fc_rport_rogue_create(struct fc_disc_port *dp)
97{
98 struct fc_rport *rport;
Joe Eykholtab28f1f2009-08-25 14:00:34 -070099 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800100 rport = kzalloc(sizeof(*rport) + sizeof(*rdata), GFP_KERNEL);
101
102 if (!rport)
103 return NULL;
104
105 rdata = RPORT_TO_PRIV(rport);
106
107 rport->dd_data = rdata;
108 rport->port_id = dp->ids.port_id;
109 rport->port_name = dp->ids.port_name;
110 rport->node_name = dp->ids.node_name;
111 rport->roles = dp->ids.roles;
112 rport->maxframe_size = FC_MIN_MAX_PAYLOAD;
113 /*
114 * Note: all this libfc rogue rport code will be removed for
115 * upstream so it fine that this is really ugly and hacky right now.
116 */
117 device_initialize(&rport->dev);
118 rport->dev.release = fc_rport_rogue_destroy;
119
120 mutex_init(&rdata->rp_mutex);
121 rdata->local_port = dp->lp;
122 rdata->trans_state = FC_PORTSTATE_ROGUE;
123 rdata->rp_state = RPORT_ST_INIT;
124 rdata->event = RPORT_EV_NONE;
125 rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
126 rdata->ops = NULL;
127 rdata->e_d_tov = dp->lp->e_d_tov;
128 rdata->r_a_tov = dp->lp->r_a_tov;
129 INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
130 INIT_WORK(&rdata->event_work, fc_rport_work);
131 /*
132 * For good measure, but not necessary as we should only
133 * add REAL rport to the lport list.
134 */
135 INIT_LIST_HEAD(&rdata->peers);
136
137 return rport;
138}
139
140/**
Robert Love34f42a02009-02-27 10:55:45 -0800141 * fc_rport_state() - return a string for the state the rport is in
Robert Love42e9a922008-12-09 15:10:17 -0800142 * @rport: The rport whose state we want to get a string for
143 */
144static const char *fc_rport_state(struct fc_rport *rport)
145{
146 const char *cp;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700147 struct fc_rport_priv *rdata = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -0800148
149 cp = fc_rport_state_names[rdata->rp_state];
150 if (!cp)
151 cp = "Unknown";
152 return cp;
153}
154
155/**
Robert Love34f42a02009-02-27 10:55:45 -0800156 * fc_set_rport_loss_tmo() - Set the remote port loss timeout in seconds.
Robert Love42e9a922008-12-09 15:10:17 -0800157 * @rport: Pointer to Fibre Channel remote port structure
158 * @timeout: timeout in seconds
159 */
160void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
161{
162 if (timeout)
163 rport->dev_loss_tmo = timeout + 5;
164 else
165 rport->dev_loss_tmo = 30;
166}
167EXPORT_SYMBOL(fc_set_rport_loss_tmo);
168
169/**
Robert Love34f42a02009-02-27 10:55:45 -0800170 * fc_plogi_get_maxframe() - Get max payload from the common service parameters
Robert Love42e9a922008-12-09 15:10:17 -0800171 * @flp: FLOGI payload structure
172 * @maxval: upper limit, may be less than what is in the service parameters
173 */
Robert Loveb2ab99c2009-02-27 10:55:50 -0800174static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
175 unsigned int maxval)
Robert Love42e9a922008-12-09 15:10:17 -0800176{
177 unsigned int mfs;
178
179 /*
180 * Get max payload from the common service parameters and the
181 * class 3 receive data field size.
182 */
183 mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
184 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
185 maxval = mfs;
186 mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
187 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
188 maxval = mfs;
189 return maxval;
190}
191
192/**
Robert Love34f42a02009-02-27 10:55:45 -0800193 * fc_rport_state_enter() - Change the rport's state
Robert Love42e9a922008-12-09 15:10:17 -0800194 * @rport: The rport whose state should change
195 * @new: The new state of the rport
196 *
197 * Locking Note: Called with the rport lock held
198 */
199static void fc_rport_state_enter(struct fc_rport *rport,
200 enum fc_rport_state new)
201{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700202 struct fc_rport_priv *rdata = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -0800203 if (rdata->rp_state != new)
204 rdata->retries = 0;
205 rdata->rp_state = new;
206}
207
208static void fc_rport_work(struct work_struct *work)
209{
Abhijeet Joglekar571f8242009-02-27 10:54:41 -0800210 u32 port_id;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700211 struct fc_rport_priv *rdata =
212 container_of(work, struct fc_rport_priv, event_work);
Robert Love42e9a922008-12-09 15:10:17 -0800213 enum fc_rport_event event;
214 enum fc_rport_trans_state trans_state;
215 struct fc_lport *lport = rdata->local_port;
216 struct fc_rport_operations *rport_ops;
217 struct fc_rport *rport = PRIV_TO_RPORT(rdata);
218
219 mutex_lock(&rdata->rp_mutex);
220 event = rdata->event;
221 rport_ops = rdata->ops;
222
223 if (event == RPORT_EV_CREATED) {
224 struct fc_rport *new_rport;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700225 struct fc_rport_priv *new_rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800226 struct fc_rport_identifiers ids;
227
228 ids.port_id = rport->port_id;
229 ids.roles = rport->roles;
230 ids.port_name = rport->port_name;
231 ids.node_name = rport->node_name;
232
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700233 rdata->event = RPORT_EV_NONE;
Robert Love42e9a922008-12-09 15:10:17 -0800234 mutex_unlock(&rdata->rp_mutex);
235
236 new_rport = fc_remote_port_add(lport->host, 0, &ids);
237 if (new_rport) {
238 /*
239 * Switch from the rogue rport to the rport
240 * returned by the FC class.
241 */
242 new_rport->maxframe_size = rport->maxframe_size;
243
244 new_rdata = new_rport->dd_data;
245 new_rdata->e_d_tov = rdata->e_d_tov;
246 new_rdata->r_a_tov = rdata->r_a_tov;
247 new_rdata->ops = rdata->ops;
248 new_rdata->local_port = rdata->local_port;
249 new_rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
250 new_rdata->trans_state = FC_PORTSTATE_REAL;
251 mutex_init(&new_rdata->rp_mutex);
252 INIT_DELAYED_WORK(&new_rdata->retry_work,
253 fc_rport_timeout);
254 INIT_LIST_HEAD(&new_rdata->peers);
255 INIT_WORK(&new_rdata->event_work, fc_rport_work);
256
257 fc_rport_state_enter(new_rport, RPORT_ST_READY);
258 } else {
Robert Love74147052009-06-10 15:31:10 -0700259 printk(KERN_WARNING "libfc: Failed to allocate "
260 " memory for rport (%6x)\n", ids.port_id);
Robert Love42e9a922008-12-09 15:10:17 -0800261 event = RPORT_EV_FAILED;
262 }
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700263 if (rport->port_id != FC_FID_DIR_SERV)
264 if (rport_ops->event_callback)
265 rport_ops->event_callback(lport, rport,
266 RPORT_EV_FAILED);
Robert Love42e9a922008-12-09 15:10:17 -0800267 put_device(&rport->dev);
268 rport = new_rport;
269 rdata = new_rport->dd_data;
270 if (rport_ops->event_callback)
271 rport_ops->event_callback(lport, rport, event);
272 } else if ((event == RPORT_EV_FAILED) ||
273 (event == RPORT_EV_LOGO) ||
274 (event == RPORT_EV_STOP)) {
275 trans_state = rdata->trans_state;
276 mutex_unlock(&rdata->rp_mutex);
277 if (rport_ops->event_callback)
278 rport_ops->event_callback(lport, rport, event);
Joe Eykholt201e5792009-07-29 17:04:54 -0700279 cancel_delayed_work_sync(&rdata->retry_work);
Robert Love42e9a922008-12-09 15:10:17 -0800280 if (trans_state == FC_PORTSTATE_ROGUE)
281 put_device(&rport->dev);
Abhijeet Joglekar571f8242009-02-27 10:54:41 -0800282 else {
283 port_id = rport->port_id;
Robert Love42e9a922008-12-09 15:10:17 -0800284 fc_remote_port_delete(rport);
Abhijeet Joglekar571f8242009-02-27 10:54:41 -0800285 lport->tt.exch_mgr_reset(lport, 0, port_id);
286 lport->tt.exch_mgr_reset(lport, port_id, 0);
287 }
Robert Love42e9a922008-12-09 15:10:17 -0800288 } else
289 mutex_unlock(&rdata->rp_mutex);
290}
291
292/**
Robert Love34f42a02009-02-27 10:55:45 -0800293 * fc_rport_login() - Start the remote port login state machine
Robert Love42e9a922008-12-09 15:10:17 -0800294 * @rport: Fibre Channel remote port
295 *
296 * Locking Note: Called without the rport lock held. This
297 * function will hold the rport lock, call an _enter_*
298 * function and then unlock the rport.
299 */
300int fc_rport_login(struct fc_rport *rport)
301{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700302 struct fc_rport_priv *rdata = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -0800303
304 mutex_lock(&rdata->rp_mutex);
305
Robert Love74147052009-06-10 15:31:10 -0700306 FC_RPORT_DBG(rport, "Login to port\n");
Robert Love42e9a922008-12-09 15:10:17 -0800307
308 fc_rport_enter_plogi(rport);
309
310 mutex_unlock(&rdata->rp_mutex);
311
312 return 0;
313}
314
315/**
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700316 * fc_rport_enter_delete() - schedule a remote port to be deleted.
317 * @rport: Fibre Channel remote port
318 * @event: event to report as the reason for deletion
319 *
320 * Locking Note: Called with the rport lock held.
321 *
322 * Allow state change into DELETE only once.
323 *
324 * Call queue_work only if there's no event already pending.
325 * Set the new event so that the old pending event will not occur.
326 * Since we have the mutex, even if fc_rport_work() is already started,
327 * it'll see the new event.
328 */
329static void fc_rport_enter_delete(struct fc_rport *rport,
330 enum fc_rport_event event)
331{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700332 struct fc_rport_priv *rdata = rport->dd_data;
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700333
334 if (rdata->rp_state == RPORT_ST_DELETE)
335 return;
336
337 FC_RPORT_DBG(rport, "Delete port\n");
338
339 fc_rport_state_enter(rport, RPORT_ST_DELETE);
340
341 if (rdata->event == RPORT_EV_NONE)
342 queue_work(rport_event_queue, &rdata->event_work);
343 rdata->event = event;
344}
345
346/**
Robert Love34f42a02009-02-27 10:55:45 -0800347 * fc_rport_logoff() - Logoff and remove an rport
Robert Love42e9a922008-12-09 15:10:17 -0800348 * @rport: Fibre Channel remote port to be removed
349 *
350 * Locking Note: Called without the rport lock held. This
351 * function will hold the rport lock, call an _enter_*
352 * function and then unlock the rport.
353 */
354int fc_rport_logoff(struct fc_rport *rport)
355{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700356 struct fc_rport_priv *rdata = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -0800357
358 mutex_lock(&rdata->rp_mutex);
359
Robert Love74147052009-06-10 15:31:10 -0700360 FC_RPORT_DBG(rport, "Remove port\n");
Robert Love42e9a922008-12-09 15:10:17 -0800361
Joe Eykholt14194052009-07-29 17:04:43 -0700362 if (rdata->rp_state == RPORT_ST_DELETE) {
363 FC_RPORT_DBG(rport, "Port in Delete state, not removing\n");
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700364 mutex_unlock(&rdata->rp_mutex);
365 goto out;
366 }
367
Robert Love42e9a922008-12-09 15:10:17 -0800368 fc_rport_enter_logo(rport);
369
370 /*
Joe Eykholt14194052009-07-29 17:04:43 -0700371 * Change the state to Delete so that we discard
Robert Love42e9a922008-12-09 15:10:17 -0800372 * the response.
373 */
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700374 fc_rport_enter_delete(rport, RPORT_EV_STOP);
Robert Love42e9a922008-12-09 15:10:17 -0800375 mutex_unlock(&rdata->rp_mutex);
376
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700377out:
Robert Love42e9a922008-12-09 15:10:17 -0800378 return 0;
379}
380
381/**
Robert Love34f42a02009-02-27 10:55:45 -0800382 * fc_rport_enter_ready() - The rport is ready
Robert Love42e9a922008-12-09 15:10:17 -0800383 * @rport: Fibre Channel remote port that is ready
384 *
385 * Locking Note: The rport lock is expected to be held before calling
386 * this routine.
387 */
388static void fc_rport_enter_ready(struct fc_rport *rport)
389{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700390 struct fc_rport_priv *rdata = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -0800391
392 fc_rport_state_enter(rport, RPORT_ST_READY);
393
Robert Love74147052009-06-10 15:31:10 -0700394 FC_RPORT_DBG(rport, "Port is Ready\n");
Robert Love42e9a922008-12-09 15:10:17 -0800395
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700396 if (rdata->event == RPORT_EV_NONE)
397 queue_work(rport_event_queue, &rdata->event_work);
Robert Love42e9a922008-12-09 15:10:17 -0800398 rdata->event = RPORT_EV_CREATED;
Robert Love42e9a922008-12-09 15:10:17 -0800399}
400
401/**
Robert Love34f42a02009-02-27 10:55:45 -0800402 * fc_rport_timeout() - Handler for the retry_work timer.
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700403 * @work: The work struct of the fc_rport_priv
Robert Love42e9a922008-12-09 15:10:17 -0800404 *
405 * Locking Note: Called without the rport lock held. This
406 * function will hold the rport lock, call an _enter_*
407 * function and then unlock the rport.
408 */
409static void fc_rport_timeout(struct work_struct *work)
410{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700411 struct fc_rport_priv *rdata =
412 container_of(work, struct fc_rport_priv, retry_work.work);
Robert Love42e9a922008-12-09 15:10:17 -0800413 struct fc_rport *rport = PRIV_TO_RPORT(rdata);
414
415 mutex_lock(&rdata->rp_mutex);
416
417 switch (rdata->rp_state) {
418 case RPORT_ST_PLOGI:
419 fc_rport_enter_plogi(rport);
420 break;
421 case RPORT_ST_PRLI:
422 fc_rport_enter_prli(rport);
423 break;
424 case RPORT_ST_RTV:
425 fc_rport_enter_rtv(rport);
426 break;
427 case RPORT_ST_LOGO:
428 fc_rport_enter_logo(rport);
429 break;
430 case RPORT_ST_READY:
431 case RPORT_ST_INIT:
Joe Eykholt14194052009-07-29 17:04:43 -0700432 case RPORT_ST_DELETE:
Robert Love42e9a922008-12-09 15:10:17 -0800433 break;
434 }
435
436 mutex_unlock(&rdata->rp_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800437}
438
439/**
Robert Love34f42a02009-02-27 10:55:45 -0800440 * fc_rport_error() - Error handler, called once retries have been exhausted
Robert Love42e9a922008-12-09 15:10:17 -0800441 * @rport: The fc_rport object
442 * @fp: The frame pointer
443 *
Robert Love42e9a922008-12-09 15:10:17 -0800444 * Locking Note: The rport lock is expected to be held before
445 * calling this routine
446 */
447static void fc_rport_error(struct fc_rport *rport, struct fc_frame *fp)
448{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700449 struct fc_rport_priv *rdata = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -0800450
Robert Love74147052009-06-10 15:31:10 -0700451 FC_RPORT_DBG(rport, "Error %ld in state %s, retries %d\n",
452 PTR_ERR(fp), fc_rport_state(rport), rdata->retries);
Robert Love42e9a922008-12-09 15:10:17 -0800453
Chris Leech6755db12009-02-27 10:55:02 -0800454 switch (rdata->rp_state) {
455 case RPORT_ST_PLOGI:
456 case RPORT_ST_PRLI:
457 case RPORT_ST_LOGO:
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700458 fc_rport_enter_delete(rport, RPORT_EV_FAILED);
Chris Leech6755db12009-02-27 10:55:02 -0800459 break;
460 case RPORT_ST_RTV:
461 fc_rport_enter_ready(rport);
462 break;
Joe Eykholt14194052009-07-29 17:04:43 -0700463 case RPORT_ST_DELETE:
Chris Leech6755db12009-02-27 10:55:02 -0800464 case RPORT_ST_READY:
465 case RPORT_ST_INIT:
466 break;
Robert Love42e9a922008-12-09 15:10:17 -0800467 }
468}
469
470/**
Robert Love34f42a02009-02-27 10:55:45 -0800471 * fc_rport_error_retry() - Error handler when retries are desired
Chris Leech6755db12009-02-27 10:55:02 -0800472 * @rport: The fc_rport object
473 * @fp: The frame pointer
474 *
475 * If the error was an exchange timeout retry immediately,
476 * otherwise wait for E_D_TOV.
477 *
478 * Locking Note: The rport lock is expected to be held before
479 * calling this routine
480 */
481static void fc_rport_error_retry(struct fc_rport *rport, struct fc_frame *fp)
482{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700483 struct fc_rport_priv *rdata = rport->dd_data;
Chris Leech6755db12009-02-27 10:55:02 -0800484 unsigned long delay = FC_DEF_E_D_TOV;
485
486 /* make sure this isn't an FC_EX_CLOSED error, never retry those */
487 if (PTR_ERR(fp) == -FC_EX_CLOSED)
488 return fc_rport_error(rport, fp);
489
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700490 if (rdata->retries < rdata->local_port->max_rport_retry_count) {
Robert Love74147052009-06-10 15:31:10 -0700491 FC_RPORT_DBG(rport, "Error %ld in state %s, retrying\n",
492 PTR_ERR(fp), fc_rport_state(rport));
Chris Leech6755db12009-02-27 10:55:02 -0800493 rdata->retries++;
494 /* no additional delay on exchange timeouts */
495 if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
496 delay = 0;
Chris Leech6755db12009-02-27 10:55:02 -0800497 schedule_delayed_work(&rdata->retry_work, delay);
498 return;
499 }
500
501 return fc_rport_error(rport, fp);
502}
503
504/**
Robert Love34f42a02009-02-27 10:55:45 -0800505 * fc_rport_plogi_recv_resp() - Handle incoming ELS PLOGI response
Robert Love42e9a922008-12-09 15:10:17 -0800506 * @sp: current sequence in the PLOGI exchange
507 * @fp: response frame
508 * @rp_arg: Fibre Channel remote port
509 *
510 * Locking Note: This function will be called without the rport lock
511 * held, but it will lock, call an _enter_* function or fc_rport_error
512 * and then unlock the rport.
513 */
514static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
515 void *rp_arg)
516{
517 struct fc_rport *rport = rp_arg;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700518 struct fc_rport_priv *rdata = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -0800519 struct fc_lport *lport = rdata->local_port;
Robert Lovea29e7642009-04-21 16:27:41 -0700520 struct fc_els_flogi *plp = NULL;
Robert Love42e9a922008-12-09 15:10:17 -0800521 unsigned int tov;
522 u16 csp_seq;
523 u16 cssp_seq;
524 u8 op;
525
526 mutex_lock(&rdata->rp_mutex);
527
Robert Love74147052009-06-10 15:31:10 -0700528 FC_RPORT_DBG(rport, "Received a PLOGI response\n");
Robert Love42e9a922008-12-09 15:10:17 -0800529
530 if (rdata->rp_state != RPORT_ST_PLOGI) {
Robert Love74147052009-06-10 15:31:10 -0700531 FC_RPORT_DBG(rport, "Received a PLOGI response, but in state "
532 "%s\n", fc_rport_state(rport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700533 if (IS_ERR(fp))
534 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800535 goto out;
536 }
537
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700538 if (IS_ERR(fp)) {
539 fc_rport_error_retry(rport, fp);
540 goto err;
541 }
542
Robert Love42e9a922008-12-09 15:10:17 -0800543 op = fc_frame_payload_op(fp);
544 if (op == ELS_LS_ACC &&
545 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
546 rport->port_name = get_unaligned_be64(&plp->fl_wwpn);
547 rport->node_name = get_unaligned_be64(&plp->fl_wwnn);
548
549 tov = ntohl(plp->fl_csp.sp_e_d_tov);
550 if (ntohs(plp->fl_csp.sp_features) & FC_SP_FT_EDTR)
551 tov /= 1000;
552 if (tov > rdata->e_d_tov)
553 rdata->e_d_tov = tov;
554 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
555 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
556 if (cssp_seq < csp_seq)
557 csp_seq = cssp_seq;
558 rdata->max_seq = csp_seq;
559 rport->maxframe_size =
560 fc_plogi_get_maxframe(plp, lport->mfs);
561
562 /*
563 * If the rport is one of the well known addresses
564 * we skip PRLI and RTV and go straight to READY.
565 */
566 if (rport->port_id >= FC_FID_DOM_MGR)
567 fc_rport_enter_ready(rport);
568 else
569 fc_rport_enter_prli(rport);
570 } else
Chris Leech6755db12009-02-27 10:55:02 -0800571 fc_rport_error_retry(rport, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800572
573out:
574 fc_frame_free(fp);
575err:
576 mutex_unlock(&rdata->rp_mutex);
577 put_device(&rport->dev);
578}
579
580/**
Robert Love34f42a02009-02-27 10:55:45 -0800581 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request to peer
Robert Love42e9a922008-12-09 15:10:17 -0800582 * @rport: Fibre Channel remote port to send PLOGI to
583 *
584 * Locking Note: The rport lock is expected to be held before calling
585 * this routine.
586 */
587static void fc_rport_enter_plogi(struct fc_rport *rport)
588{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700589 struct fc_rport_priv *rdata = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -0800590 struct fc_lport *lport = rdata->local_port;
591 struct fc_frame *fp;
592
Robert Love74147052009-06-10 15:31:10 -0700593 FC_RPORT_DBG(rport, "Port entered PLOGI state from %s state\n",
594 fc_rport_state(rport));
Robert Love42e9a922008-12-09 15:10:17 -0800595
596 fc_rport_state_enter(rport, RPORT_ST_PLOGI);
597
598 rport->maxframe_size = FC_MIN_MAX_PAYLOAD;
599 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
600 if (!fp) {
Chris Leech6755db12009-02-27 10:55:02 -0800601 fc_rport_error_retry(rport, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800602 return;
603 }
604 rdata->e_d_tov = lport->e_d_tov;
605
606 if (!lport->tt.elsct_send(lport, rport, fp, ELS_PLOGI,
607 fc_rport_plogi_resp, rport, lport->e_d_tov))
Chris Leech6755db12009-02-27 10:55:02 -0800608 fc_rport_error_retry(rport, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800609 else
610 get_device(&rport->dev);
611}
612
613/**
Robert Love34f42a02009-02-27 10:55:45 -0800614 * fc_rport_prli_resp() - Process Login (PRLI) response handler
Robert Love42e9a922008-12-09 15:10:17 -0800615 * @sp: current sequence in the PRLI exchange
616 * @fp: response frame
617 * @rp_arg: Fibre Channel remote port
618 *
619 * Locking Note: This function will be called without the rport lock
620 * held, but it will lock, call an _enter_* function or fc_rport_error
621 * and then unlock the rport.
622 */
623static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
624 void *rp_arg)
625{
626 struct fc_rport *rport = rp_arg;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700627 struct fc_rport_priv *rdata = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -0800628 struct {
629 struct fc_els_prli prli;
630 struct fc_els_spp spp;
631 } *pp;
632 u32 roles = FC_RPORT_ROLE_UNKNOWN;
633 u32 fcp_parm = 0;
634 u8 op;
635
636 mutex_lock(&rdata->rp_mutex);
637
Robert Love74147052009-06-10 15:31:10 -0700638 FC_RPORT_DBG(rport, "Received a PRLI response\n");
Robert Love42e9a922008-12-09 15:10:17 -0800639
640 if (rdata->rp_state != RPORT_ST_PRLI) {
Robert Love74147052009-06-10 15:31:10 -0700641 FC_RPORT_DBG(rport, "Received a PRLI response, but in state "
642 "%s\n", fc_rport_state(rport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700643 if (IS_ERR(fp))
644 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800645 goto out;
646 }
647
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700648 if (IS_ERR(fp)) {
649 fc_rport_error_retry(rport, fp);
650 goto err;
651 }
652
Robert Love42e9a922008-12-09 15:10:17 -0800653 op = fc_frame_payload_op(fp);
654 if (op == ELS_LS_ACC) {
655 pp = fc_frame_payload_get(fp, sizeof(*pp));
656 if (pp && pp->prli.prli_spp_len >= sizeof(pp->spp)) {
657 fcp_parm = ntohl(pp->spp.spp_params);
658 if (fcp_parm & FCP_SPPF_RETRY)
659 rdata->flags |= FC_RP_FLAGS_RETRY;
660 }
661
662 rport->supported_classes = FC_COS_CLASS3;
663 if (fcp_parm & FCP_SPPF_INIT_FCN)
664 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
665 if (fcp_parm & FCP_SPPF_TARG_FCN)
666 roles |= FC_RPORT_ROLE_FCP_TARGET;
667
668 rport->roles = roles;
669 fc_rport_enter_rtv(rport);
670
671 } else {
Robert Love74147052009-06-10 15:31:10 -0700672 FC_RPORT_DBG(rport, "Bad ELS response for PRLI command\n");
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700673 fc_rport_enter_delete(rport, RPORT_EV_FAILED);
Robert Love42e9a922008-12-09 15:10:17 -0800674 }
675
676out:
677 fc_frame_free(fp);
678err:
679 mutex_unlock(&rdata->rp_mutex);
680 put_device(&rport->dev);
681}
682
683/**
Robert Love34f42a02009-02-27 10:55:45 -0800684 * fc_rport_logo_resp() - Logout (LOGO) response handler
Robert Love42e9a922008-12-09 15:10:17 -0800685 * @sp: current sequence in the LOGO exchange
686 * @fp: response frame
687 * @rp_arg: Fibre Channel remote port
688 *
689 * Locking Note: This function will be called without the rport lock
690 * held, but it will lock, call an _enter_* function or fc_rport_error
691 * and then unlock the rport.
692 */
693static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
694 void *rp_arg)
695{
696 struct fc_rport *rport = rp_arg;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700697 struct fc_rport_priv *rdata = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -0800698 u8 op;
699
700 mutex_lock(&rdata->rp_mutex);
701
Robert Love74147052009-06-10 15:31:10 -0700702 FC_RPORT_DBG(rport, "Received a LOGO response\n");
Robert Love42e9a922008-12-09 15:10:17 -0800703
Robert Love42e9a922008-12-09 15:10:17 -0800704 if (rdata->rp_state != RPORT_ST_LOGO) {
Robert Love74147052009-06-10 15:31:10 -0700705 FC_RPORT_DBG(rport, "Received a LOGO response, but in state "
706 "%s\n", fc_rport_state(rport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700707 if (IS_ERR(fp))
708 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800709 goto out;
710 }
711
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700712 if (IS_ERR(fp)) {
713 fc_rport_error_retry(rport, fp);
714 goto err;
715 }
716
Robert Love42e9a922008-12-09 15:10:17 -0800717 op = fc_frame_payload_op(fp);
718 if (op == ELS_LS_ACC) {
719 fc_rport_enter_rtv(rport);
720 } else {
Robert Love74147052009-06-10 15:31:10 -0700721 FC_RPORT_DBG(rport, "Bad ELS response for LOGO command\n");
Joe Eykholt5f7ea3b2009-07-29 17:04:49 -0700722 fc_rport_enter_delete(rport, RPORT_EV_LOGO);
Robert Love42e9a922008-12-09 15:10:17 -0800723 }
724
725out:
726 fc_frame_free(fp);
727err:
728 mutex_unlock(&rdata->rp_mutex);
729 put_device(&rport->dev);
730}
731
732/**
Robert Love34f42a02009-02-27 10:55:45 -0800733 * fc_rport_enter_prli() - Send Process Login (PRLI) request to peer
Robert Love42e9a922008-12-09 15:10:17 -0800734 * @rport: Fibre Channel remote port to send PRLI to
735 *
736 * Locking Note: The rport lock is expected to be held before calling
737 * this routine.
738 */
739static void fc_rport_enter_prli(struct fc_rport *rport)
740{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700741 struct fc_rport_priv *rdata = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -0800742 struct fc_lport *lport = rdata->local_port;
743 struct {
744 struct fc_els_prli prli;
745 struct fc_els_spp spp;
746 } *pp;
747 struct fc_frame *fp;
748
Robert Love74147052009-06-10 15:31:10 -0700749 FC_RPORT_DBG(rport, "Port entered PRLI state from %s state\n",
750 fc_rport_state(rport));
Robert Love42e9a922008-12-09 15:10:17 -0800751
752 fc_rport_state_enter(rport, RPORT_ST_PRLI);
753
754 fp = fc_frame_alloc(lport, sizeof(*pp));
755 if (!fp) {
Chris Leech6755db12009-02-27 10:55:02 -0800756 fc_rport_error_retry(rport, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800757 return;
758 }
759
760 if (!lport->tt.elsct_send(lport, rport, fp, ELS_PRLI,
761 fc_rport_prli_resp, rport, lport->e_d_tov))
Chris Leech6755db12009-02-27 10:55:02 -0800762 fc_rport_error_retry(rport, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800763 else
764 get_device(&rport->dev);
765}
766
767/**
Robert Love34f42a02009-02-27 10:55:45 -0800768 * fc_rport_els_rtv_resp() - Request Timeout Value response handler
Robert Love42e9a922008-12-09 15:10:17 -0800769 * @sp: current sequence in the RTV exchange
770 * @fp: response frame
771 * @rp_arg: Fibre Channel remote port
772 *
773 * Many targets don't seem to support this.
774 *
775 * Locking Note: This function will be called without the rport lock
776 * held, but it will lock, call an _enter_* function or fc_rport_error
777 * and then unlock the rport.
778 */
779static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
780 void *rp_arg)
781{
782 struct fc_rport *rport = rp_arg;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700783 struct fc_rport_priv *rdata = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -0800784 u8 op;
785
786 mutex_lock(&rdata->rp_mutex);
787
Robert Love74147052009-06-10 15:31:10 -0700788 FC_RPORT_DBG(rport, "Received a RTV response\n");
Robert Love42e9a922008-12-09 15:10:17 -0800789
790 if (rdata->rp_state != RPORT_ST_RTV) {
Robert Love74147052009-06-10 15:31:10 -0700791 FC_RPORT_DBG(rport, "Received a RTV response, but in state "
792 "%s\n", fc_rport_state(rport));
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700793 if (IS_ERR(fp))
794 goto err;
Robert Love42e9a922008-12-09 15:10:17 -0800795 goto out;
796 }
797
Abhijeet Joglekar76f68042009-04-21 16:26:58 -0700798 if (IS_ERR(fp)) {
799 fc_rport_error(rport, fp);
800 goto err;
801 }
802
Robert Love42e9a922008-12-09 15:10:17 -0800803 op = fc_frame_payload_op(fp);
804 if (op == ELS_LS_ACC) {
805 struct fc_els_rtv_acc *rtv;
806 u32 toq;
807 u32 tov;
808
809 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
810 if (rtv) {
811 toq = ntohl(rtv->rtv_toq);
812 tov = ntohl(rtv->rtv_r_a_tov);
813 if (tov == 0)
814 tov = 1;
815 rdata->r_a_tov = tov;
816 tov = ntohl(rtv->rtv_e_d_tov);
817 if (toq & FC_ELS_RTV_EDRES)
818 tov /= 1000000;
819 if (tov == 0)
820 tov = 1;
821 rdata->e_d_tov = tov;
822 }
823 }
824
825 fc_rport_enter_ready(rport);
826
827out:
828 fc_frame_free(fp);
829err:
830 mutex_unlock(&rdata->rp_mutex);
831 put_device(&rport->dev);
832}
833
834/**
Robert Love34f42a02009-02-27 10:55:45 -0800835 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request to peer
Robert Love42e9a922008-12-09 15:10:17 -0800836 * @rport: Fibre Channel remote port to send RTV to
837 *
838 * Locking Note: The rport lock is expected to be held before calling
839 * this routine.
840 */
841static void fc_rport_enter_rtv(struct fc_rport *rport)
842{
843 struct fc_frame *fp;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700844 struct fc_rport_priv *rdata = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -0800845 struct fc_lport *lport = rdata->local_port;
846
Robert Love74147052009-06-10 15:31:10 -0700847 FC_RPORT_DBG(rport, "Port entered RTV state from %s state\n",
848 fc_rport_state(rport));
Robert Love42e9a922008-12-09 15:10:17 -0800849
850 fc_rport_state_enter(rport, RPORT_ST_RTV);
851
852 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
853 if (!fp) {
Chris Leech6755db12009-02-27 10:55:02 -0800854 fc_rport_error_retry(rport, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800855 return;
856 }
857
858 if (!lport->tt.elsct_send(lport, rport, fp, ELS_RTV,
859 fc_rport_rtv_resp, rport, lport->e_d_tov))
Chris Leech6755db12009-02-27 10:55:02 -0800860 fc_rport_error_retry(rport, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800861 else
862 get_device(&rport->dev);
863}
864
865/**
Robert Love34f42a02009-02-27 10:55:45 -0800866 * fc_rport_enter_logo() - Send Logout (LOGO) request to peer
Robert Love42e9a922008-12-09 15:10:17 -0800867 * @rport: Fibre Channel remote port to send LOGO to
868 *
869 * Locking Note: The rport lock is expected to be held before calling
870 * this routine.
871 */
872static void fc_rport_enter_logo(struct fc_rport *rport)
873{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700874 struct fc_rport_priv *rdata = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -0800875 struct fc_lport *lport = rdata->local_port;
876 struct fc_frame *fp;
877
Robert Love74147052009-06-10 15:31:10 -0700878 FC_RPORT_DBG(rport, "Port entered LOGO state from %s state\n",
879 fc_rport_state(rport));
Robert Love42e9a922008-12-09 15:10:17 -0800880
881 fc_rport_state_enter(rport, RPORT_ST_LOGO);
882
883 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
884 if (!fp) {
Chris Leech6755db12009-02-27 10:55:02 -0800885 fc_rport_error_retry(rport, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800886 return;
887 }
888
889 if (!lport->tt.elsct_send(lport, rport, fp, ELS_LOGO,
890 fc_rport_logo_resp, rport, lport->e_d_tov))
Chris Leech6755db12009-02-27 10:55:02 -0800891 fc_rport_error_retry(rport, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800892 else
893 get_device(&rport->dev);
894}
895
896
897/**
Robert Love34f42a02009-02-27 10:55:45 -0800898 * fc_rport_recv_req() - Receive a request from a rport
Robert Love42e9a922008-12-09 15:10:17 -0800899 * @sp: current sequence in the PLOGI exchange
900 * @fp: response frame
901 * @rp_arg: Fibre Channel remote port
902 *
903 * Locking Note: Called without the rport lock held. This
904 * function will hold the rport lock, call an _enter_*
905 * function and then unlock the rport.
906 */
907void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
908 struct fc_rport *rport)
909{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700910 struct fc_rport_priv *rdata = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -0800911 struct fc_lport *lport = rdata->local_port;
912
913 struct fc_frame_header *fh;
914 struct fc_seq_els_data els_data;
915 u8 op;
916
917 mutex_lock(&rdata->rp_mutex);
918
919 els_data.fp = NULL;
920 els_data.explan = ELS_EXPL_NONE;
921 els_data.reason = ELS_RJT_NONE;
922
923 fh = fc_frame_header_get(fp);
924
925 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ && fh->fh_type == FC_TYPE_ELS) {
926 op = fc_frame_payload_op(fp);
927 switch (op) {
928 case ELS_PLOGI:
929 fc_rport_recv_plogi_req(rport, sp, fp);
930 break;
931 case ELS_PRLI:
932 fc_rport_recv_prli_req(rport, sp, fp);
933 break;
934 case ELS_PRLO:
935 fc_rport_recv_prlo_req(rport, sp, fp);
936 break;
937 case ELS_LOGO:
938 fc_rport_recv_logo_req(rport, sp, fp);
939 break;
940 case ELS_RRQ:
941 els_data.fp = fp;
942 lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
943 break;
944 case ELS_REC:
945 els_data.fp = fp;
946 lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
947 break;
948 default:
949 els_data.reason = ELS_RJT_UNSUP;
950 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
951 break;
952 }
953 }
954
955 mutex_unlock(&rdata->rp_mutex);
956}
957
958/**
Robert Love34f42a02009-02-27 10:55:45 -0800959 * fc_rport_recv_plogi_req() - Handle incoming Port Login (PLOGI) request
Robert Love42e9a922008-12-09 15:10:17 -0800960 * @rport: Fibre Channel remote port that initiated PLOGI
961 * @sp: current sequence in the PLOGI exchange
962 * @fp: PLOGI request frame
963 *
964 * Locking Note: The rport lock is exected to be held before calling
965 * this function.
966 */
967static void fc_rport_recv_plogi_req(struct fc_rport *rport,
968 struct fc_seq *sp, struct fc_frame *rx_fp)
969{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700970 struct fc_rport_priv *rdata = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -0800971 struct fc_lport *lport = rdata->local_port;
972 struct fc_frame *fp = rx_fp;
973 struct fc_exch *ep;
974 struct fc_frame_header *fh;
975 struct fc_els_flogi *pl;
976 struct fc_seq_els_data rjt_data;
977 u32 sid;
978 u64 wwpn;
979 u64 wwnn;
980 enum fc_els_rjt_reason reject = 0;
981 u32 f_ctl;
982 rjt_data.fp = NULL;
983
984 fh = fc_frame_header_get(fp);
985
Robert Love74147052009-06-10 15:31:10 -0700986 FC_RPORT_DBG(rport, "Received PLOGI request while in state %s\n",
987 fc_rport_state(rport));
Robert Love42e9a922008-12-09 15:10:17 -0800988
989 sid = ntoh24(fh->fh_s_id);
990 pl = fc_frame_payload_get(fp, sizeof(*pl));
991 if (!pl) {
Robert Love74147052009-06-10 15:31:10 -0700992 FC_RPORT_DBG(rport, "Received PLOGI too short\n");
Robert Love42e9a922008-12-09 15:10:17 -0800993 WARN_ON(1);
994 /* XXX TBD: send reject? */
995 fc_frame_free(fp);
996 return;
997 }
998 wwpn = get_unaligned_be64(&pl->fl_wwpn);
999 wwnn = get_unaligned_be64(&pl->fl_wwnn);
1000
1001 /*
1002 * If the session was just created, possibly due to the incoming PLOGI,
1003 * set the state appropriately and accept the PLOGI.
1004 *
1005 * If we had also sent a PLOGI, and if the received PLOGI is from a
1006 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1007 * "command already in progress".
1008 *
1009 * XXX TBD: If the session was ready before, the PLOGI should result in
1010 * all outstanding exchanges being reset.
1011 */
1012 switch (rdata->rp_state) {
1013 case RPORT_ST_INIT:
Robert Love74147052009-06-10 15:31:10 -07001014 FC_RPORT_DBG(rport, "Received PLOGI, wwpn %llx state INIT "
1015 "- reject\n", (unsigned long long)wwpn);
Robert Love42e9a922008-12-09 15:10:17 -08001016 reject = ELS_RJT_UNSUP;
1017 break;
1018 case RPORT_ST_PLOGI:
Robert Love74147052009-06-10 15:31:10 -07001019 FC_RPORT_DBG(rport, "Received PLOGI in PLOGI state %d\n",
1020 rdata->rp_state);
Robert Love42e9a922008-12-09 15:10:17 -08001021 if (wwpn < lport->wwpn)
1022 reject = ELS_RJT_INPROG;
1023 break;
1024 case RPORT_ST_PRLI:
1025 case RPORT_ST_READY:
Robert Love74147052009-06-10 15:31:10 -07001026 FC_RPORT_DBG(rport, "Received PLOGI in logged-in state %d "
1027 "- ignored for now\n", rdata->rp_state);
Robert Love42e9a922008-12-09 15:10:17 -08001028 /* XXX TBD - should reset */
1029 break;
Joe Eykholt14194052009-07-29 17:04:43 -07001030 case RPORT_ST_DELETE:
Robert Love42e9a922008-12-09 15:10:17 -08001031 default:
Robert Love74147052009-06-10 15:31:10 -07001032 FC_RPORT_DBG(rport, "Received PLOGI in unexpected "
1033 "state %d\n", rdata->rp_state);
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -07001034 fc_frame_free(fp);
1035 return;
Robert Love42e9a922008-12-09 15:10:17 -08001036 break;
1037 }
1038
1039 if (reject) {
1040 rjt_data.reason = reject;
1041 rjt_data.explan = ELS_EXPL_NONE;
1042 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1043 fc_frame_free(fp);
1044 } else {
1045 fp = fc_frame_alloc(lport, sizeof(*pl));
1046 if (fp == NULL) {
1047 fp = rx_fp;
1048 rjt_data.reason = ELS_RJT_UNAB;
1049 rjt_data.explan = ELS_EXPL_NONE;
1050 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1051 fc_frame_free(fp);
1052 } else {
1053 sp = lport->tt.seq_start_next(sp);
1054 WARN_ON(!sp);
1055 fc_rport_set_name(rport, wwpn, wwnn);
1056
1057 /*
1058 * Get session payload size from incoming PLOGI.
1059 */
1060 rport->maxframe_size =
1061 fc_plogi_get_maxframe(pl, lport->mfs);
1062 fc_frame_free(rx_fp);
1063 fc_plogi_fill(lport, fp, ELS_LS_ACC);
1064
1065 /*
1066 * Send LS_ACC. If this fails,
1067 * the originator should retry.
1068 */
1069 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1070 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1071 ep = fc_seq_exch(sp);
1072 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1073 FC_TYPE_ELS, f_ctl, 0);
1074 lport->tt.seq_send(lport, sp, fp);
1075 if (rdata->rp_state == RPORT_ST_PLOGI)
1076 fc_rport_enter_prli(rport);
1077 }
1078 }
1079}
1080
1081/**
Robert Love34f42a02009-02-27 10:55:45 -08001082 * fc_rport_recv_prli_req() - Handle incoming Process Login (PRLI) request
Robert Love42e9a922008-12-09 15:10:17 -08001083 * @rport: Fibre Channel remote port that initiated PRLI
1084 * @sp: current sequence in the PRLI exchange
1085 * @fp: PRLI request frame
1086 *
1087 * Locking Note: The rport lock is exected to be held before calling
1088 * this function.
1089 */
1090static void fc_rport_recv_prli_req(struct fc_rport *rport,
1091 struct fc_seq *sp, struct fc_frame *rx_fp)
1092{
Joe Eykholtab28f1f2009-08-25 14:00:34 -07001093 struct fc_rport_priv *rdata = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -08001094 struct fc_lport *lport = rdata->local_port;
1095 struct fc_exch *ep;
1096 struct fc_frame *fp;
1097 struct fc_frame_header *fh;
1098 struct {
1099 struct fc_els_prli prli;
1100 struct fc_els_spp spp;
1101 } *pp;
1102 struct fc_els_spp *rspp; /* request service param page */
1103 struct fc_els_spp *spp; /* response spp */
1104 unsigned int len;
1105 unsigned int plen;
1106 enum fc_els_rjt_reason reason = ELS_RJT_UNAB;
1107 enum fc_els_rjt_explan explan = ELS_EXPL_NONE;
1108 enum fc_els_spp_resp resp;
1109 struct fc_seq_els_data rjt_data;
1110 u32 f_ctl;
1111 u32 fcp_parm;
1112 u32 roles = FC_RPORT_ROLE_UNKNOWN;
1113 rjt_data.fp = NULL;
1114
1115 fh = fc_frame_header_get(rx_fp);
1116
Robert Love74147052009-06-10 15:31:10 -07001117 FC_RPORT_DBG(rport, "Received PRLI request while in state %s\n",
1118 fc_rport_state(rport));
Robert Love42e9a922008-12-09 15:10:17 -08001119
1120 switch (rdata->rp_state) {
1121 case RPORT_ST_PRLI:
1122 case RPORT_ST_READY:
1123 reason = ELS_RJT_NONE;
1124 break;
1125 default:
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -07001126 fc_frame_free(rx_fp);
1127 return;
Robert Love42e9a922008-12-09 15:10:17 -08001128 break;
1129 }
1130 len = fr_len(rx_fp) - sizeof(*fh);
1131 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1132 if (pp == NULL) {
1133 reason = ELS_RJT_PROT;
1134 explan = ELS_EXPL_INV_LEN;
1135 } else {
1136 plen = ntohs(pp->prli.prli_len);
1137 if ((plen % 4) != 0 || plen > len) {
1138 reason = ELS_RJT_PROT;
1139 explan = ELS_EXPL_INV_LEN;
1140 } else if (plen < len) {
1141 len = plen;
1142 }
1143 plen = pp->prli.prli_spp_len;
1144 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1145 plen > len || len < sizeof(*pp)) {
1146 reason = ELS_RJT_PROT;
1147 explan = ELS_EXPL_INV_LEN;
1148 }
1149 rspp = &pp->spp;
1150 }
1151 if (reason != ELS_RJT_NONE ||
1152 (fp = fc_frame_alloc(lport, len)) == NULL) {
1153 rjt_data.reason = reason;
1154 rjt_data.explan = explan;
1155 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1156 } else {
1157 sp = lport->tt.seq_start_next(sp);
1158 WARN_ON(!sp);
1159 pp = fc_frame_payload_get(fp, len);
1160 WARN_ON(!pp);
1161 memset(pp, 0, len);
1162 pp->prli.prli_cmd = ELS_LS_ACC;
1163 pp->prli.prli_spp_len = plen;
1164 pp->prli.prli_len = htons(len);
1165 len -= sizeof(struct fc_els_prli);
1166
1167 /*
1168 * Go through all the service parameter pages and build
1169 * response. If plen indicates longer SPP than standard,
1170 * use that. The entire response has been pre-cleared above.
1171 */
1172 spp = &pp->spp;
1173 while (len >= plen) {
1174 spp->spp_type = rspp->spp_type;
1175 spp->spp_type_ext = rspp->spp_type_ext;
1176 spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
1177 resp = FC_SPP_RESP_ACK;
1178 if (rspp->spp_flags & FC_SPP_RPA_VAL)
1179 resp = FC_SPP_RESP_NO_PA;
1180 switch (rspp->spp_type) {
1181 case 0: /* common to all FC-4 types */
1182 break;
1183 case FC_TYPE_FCP:
1184 fcp_parm = ntohl(rspp->spp_params);
1185 if (fcp_parm * FCP_SPPF_RETRY)
1186 rdata->flags |= FC_RP_FLAGS_RETRY;
1187 rport->supported_classes = FC_COS_CLASS3;
1188 if (fcp_parm & FCP_SPPF_INIT_FCN)
1189 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1190 if (fcp_parm & FCP_SPPF_TARG_FCN)
1191 roles |= FC_RPORT_ROLE_FCP_TARGET;
1192 rport->roles = roles;
1193
1194 spp->spp_params =
1195 htonl(lport->service_params);
1196 break;
1197 default:
1198 resp = FC_SPP_RESP_INVL;
1199 break;
1200 }
1201 spp->spp_flags |= resp;
1202 len -= plen;
1203 rspp = (struct fc_els_spp *)((char *)rspp + plen);
1204 spp = (struct fc_els_spp *)((char *)spp + plen);
1205 }
1206
1207 /*
1208 * Send LS_ACC. If this fails, the originator should retry.
1209 */
1210 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1211 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1212 ep = fc_seq_exch(sp);
1213 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1214 FC_TYPE_ELS, f_ctl, 0);
1215 lport->tt.seq_send(lport, sp, fp);
1216
1217 /*
1218 * Get lock and re-check state.
1219 */
1220 switch (rdata->rp_state) {
1221 case RPORT_ST_PRLI:
1222 fc_rport_enter_ready(rport);
1223 break;
1224 case RPORT_ST_READY:
1225 break;
1226 default:
1227 break;
1228 }
1229 }
1230 fc_frame_free(rx_fp);
1231}
1232
1233/**
Robert Love34f42a02009-02-27 10:55:45 -08001234 * fc_rport_recv_prlo_req() - Handle incoming Process Logout (PRLO) request
Robert Love42e9a922008-12-09 15:10:17 -08001235 * @rport: Fibre Channel remote port that initiated PRLO
1236 * @sp: current sequence in the PRLO exchange
1237 * @fp: PRLO request frame
1238 *
1239 * Locking Note: The rport lock is exected to be held before calling
1240 * this function.
1241 */
1242static void fc_rport_recv_prlo_req(struct fc_rport *rport, struct fc_seq *sp,
1243 struct fc_frame *fp)
1244{
Joe Eykholtab28f1f2009-08-25 14:00:34 -07001245 struct fc_rport_priv *rdata = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -08001246 struct fc_lport *lport = rdata->local_port;
1247
1248 struct fc_frame_header *fh;
1249 struct fc_seq_els_data rjt_data;
1250
1251 fh = fc_frame_header_get(fp);
1252
Robert Love74147052009-06-10 15:31:10 -07001253 FC_RPORT_DBG(rport, "Received PRLO request while in state %s\n",
1254 fc_rport_state(rport));
Robert Love42e9a922008-12-09 15:10:17 -08001255
Joe Eykholt14194052009-07-29 17:04:43 -07001256 if (rdata->rp_state == RPORT_ST_DELETE) {
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -07001257 fc_frame_free(fp);
1258 return;
1259 }
1260
Robert Love42e9a922008-12-09 15:10:17 -08001261 rjt_data.fp = NULL;
1262 rjt_data.reason = ELS_RJT_UNAB;
1263 rjt_data.explan = ELS_EXPL_NONE;
1264 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1265 fc_frame_free(fp);
1266}
1267
1268/**
Robert Love34f42a02009-02-27 10:55:45 -08001269 * fc_rport_recv_logo_req() - Handle incoming Logout (LOGO) request
Robert Love42e9a922008-12-09 15:10:17 -08001270 * @rport: Fibre Channel remote port that initiated LOGO
1271 * @sp: current sequence in the LOGO exchange
1272 * @fp: LOGO request frame
1273 *
1274 * Locking Note: The rport lock is exected to be held before calling
1275 * this function.
1276 */
1277static void fc_rport_recv_logo_req(struct fc_rport *rport, struct fc_seq *sp,
1278 struct fc_frame *fp)
1279{
1280 struct fc_frame_header *fh;
Joe Eykholtab28f1f2009-08-25 14:00:34 -07001281 struct fc_rport_priv *rdata = rport->dd_data;
Robert Love42e9a922008-12-09 15:10:17 -08001282 struct fc_lport *lport = rdata->local_port;
1283
1284 fh = fc_frame_header_get(fp);
1285
Robert Love74147052009-06-10 15:31:10 -07001286 FC_RPORT_DBG(rport, "Received LOGO request while in state %s\n",
1287 fc_rport_state(rport));
Robert Love42e9a922008-12-09 15:10:17 -08001288
Joe Eykholt14194052009-07-29 17:04:43 -07001289 if (rdata->rp_state == RPORT_ST_DELETE) {
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -07001290 fc_frame_free(fp);
1291 return;
1292 }
1293
Robert Love42e9a922008-12-09 15:10:17 -08001294 rdata->event = RPORT_EV_LOGO;
Joe Eykholt14194052009-07-29 17:04:43 -07001295 fc_rport_state_enter(rport, RPORT_ST_DELETE);
Robert Love42e9a922008-12-09 15:10:17 -08001296 queue_work(rport_event_queue, &rdata->event_work);
1297
1298 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
1299 fc_frame_free(fp);
1300}
1301
1302static void fc_rport_flush_queue(void)
1303{
1304 flush_workqueue(rport_event_queue);
1305}
1306
Robert Love42e9a922008-12-09 15:10:17 -08001307int fc_rport_init(struct fc_lport *lport)
1308{
Robert Love5101ff92009-02-27 10:55:18 -08001309 if (!lport->tt.rport_create)
1310 lport->tt.rport_create = fc_rport_rogue_create;
1311
Robert Love42e9a922008-12-09 15:10:17 -08001312 if (!lport->tt.rport_login)
1313 lport->tt.rport_login = fc_rport_login;
1314
1315 if (!lport->tt.rport_logoff)
1316 lport->tt.rport_logoff = fc_rport_logoff;
1317
1318 if (!lport->tt.rport_recv_req)
1319 lport->tt.rport_recv_req = fc_rport_recv_req;
1320
1321 if (!lport->tt.rport_flush_queue)
1322 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1323
1324 return 0;
1325}
1326EXPORT_SYMBOL(fc_rport_init);
1327
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001328int fc_setup_rport(void)
Robert Love42e9a922008-12-09 15:10:17 -08001329{
1330 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
1331 if (!rport_event_queue)
1332 return -ENOMEM;
1333 return 0;
1334}
1335EXPORT_SYMBOL(fc_setup_rport);
1336
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001337void fc_destroy_rport(void)
Robert Love42e9a922008-12-09 15:10:17 -08001338{
1339 destroy_workqueue(rport_event_queue);
1340}
1341EXPORT_SYMBOL(fc_destroy_rport);
1342
1343void fc_rport_terminate_io(struct fc_rport *rport)
1344{
Joe Eykholtab28f1f2009-08-25 14:00:34 -07001345 struct fc_rport_libfc_priv *rp = rport->dd_data;
1346 struct fc_lport *lport = rp->local_port;
Robert Love42e9a922008-12-09 15:10:17 -08001347
Abhijeet Joglekar1f6ff362009-02-27 10:54:35 -08001348 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
1349 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
Robert Love42e9a922008-12-09 15:10:17 -08001350}
1351EXPORT_SYMBOL(fc_rport_terminate_io);