[SCSI] libfc: A modular Fibre Channel library
[linux-2.6-block.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>
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
58static int fc_rport_debug;
59
60#define FC_DEBUG_RPORT(fmt...) \
61 do { \
62 if (fc_rport_debug) \
63 FC_DBG(fmt); \
64 } while (0)
65
66struct workqueue_struct *rport_event_queue;
67
68static void fc_rport_enter_plogi(struct fc_rport *);
69static void fc_rport_enter_prli(struct fc_rport *);
70static void fc_rport_enter_rtv(struct fc_rport *);
71static void fc_rport_enter_ready(struct fc_rport *);
72static void fc_rport_enter_logo(struct fc_rport *);
73
74static void fc_rport_recv_plogi_req(struct fc_rport *,
75 struct fc_seq *, struct fc_frame *);
76static void fc_rport_recv_prli_req(struct fc_rport *,
77 struct fc_seq *, struct fc_frame *);
78static void fc_rport_recv_prlo_req(struct fc_rport *,
79 struct fc_seq *, struct fc_frame *);
80static void fc_rport_recv_logo_req(struct fc_rport *,
81 struct fc_seq *, struct fc_frame *);
82static void fc_rport_timeout(struct work_struct *);
83static void fc_rport_error(struct fc_rport *, struct fc_frame *);
84static void fc_rport_work(struct work_struct *);
85
86static const char *fc_rport_state_names[] = {
87 [RPORT_ST_NONE] = "None",
88 [RPORT_ST_INIT] = "Init",
89 [RPORT_ST_PLOGI] = "PLOGI",
90 [RPORT_ST_PRLI] = "PRLI",
91 [RPORT_ST_RTV] = "RTV",
92 [RPORT_ST_READY] = "Ready",
93 [RPORT_ST_LOGO] = "LOGO",
94};
95
96static void fc_rport_rogue_destroy(struct device *dev)
97{
98 struct fc_rport *rport = dev_to_rport(dev);
99 FC_DEBUG_RPORT("Destroying rogue rport (%6x)\n", rport->port_id);
100 kfree(rport);
101}
102
103struct fc_rport *fc_rport_rogue_create(struct fc_disc_port *dp)
104{
105 struct fc_rport *rport;
106 struct fc_rport_libfc_priv *rdata;
107 rport = kzalloc(sizeof(*rport) + sizeof(*rdata), GFP_KERNEL);
108
109 if (!rport)
110 return NULL;
111
112 rdata = RPORT_TO_PRIV(rport);
113
114 rport->dd_data = rdata;
115 rport->port_id = dp->ids.port_id;
116 rport->port_name = dp->ids.port_name;
117 rport->node_name = dp->ids.node_name;
118 rport->roles = dp->ids.roles;
119 rport->maxframe_size = FC_MIN_MAX_PAYLOAD;
120 /*
121 * Note: all this libfc rogue rport code will be removed for
122 * upstream so it fine that this is really ugly and hacky right now.
123 */
124 device_initialize(&rport->dev);
125 rport->dev.release = fc_rport_rogue_destroy;
126
127 mutex_init(&rdata->rp_mutex);
128 rdata->local_port = dp->lp;
129 rdata->trans_state = FC_PORTSTATE_ROGUE;
130 rdata->rp_state = RPORT_ST_INIT;
131 rdata->event = RPORT_EV_NONE;
132 rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
133 rdata->ops = NULL;
134 rdata->e_d_tov = dp->lp->e_d_tov;
135 rdata->r_a_tov = dp->lp->r_a_tov;
136 INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
137 INIT_WORK(&rdata->event_work, fc_rport_work);
138 /*
139 * For good measure, but not necessary as we should only
140 * add REAL rport to the lport list.
141 */
142 INIT_LIST_HEAD(&rdata->peers);
143
144 return rport;
145}
146
147/**
148 * fc_rport_state - return a string for the state the rport is in
149 * @rport: The rport whose state we want to get a string for
150 */
151static const char *fc_rport_state(struct fc_rport *rport)
152{
153 const char *cp;
154 struct fc_rport_libfc_priv *rdata = rport->dd_data;
155
156 cp = fc_rport_state_names[rdata->rp_state];
157 if (!cp)
158 cp = "Unknown";
159 return cp;
160}
161
162/**
163 * fc_set_rport_loss_tmo - Set the remote port loss timeout in seconds.
164 * @rport: Pointer to Fibre Channel remote port structure
165 * @timeout: timeout in seconds
166 */
167void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
168{
169 if (timeout)
170 rport->dev_loss_tmo = timeout + 5;
171 else
172 rport->dev_loss_tmo = 30;
173}
174EXPORT_SYMBOL(fc_set_rport_loss_tmo);
175
176/**
177 * fc_plogi_get_maxframe - Get max payload from the common service parameters
178 * @flp: FLOGI payload structure
179 * @maxval: upper limit, may be less than what is in the service parameters
180 */
181static unsigned int
182fc_plogi_get_maxframe(struct fc_els_flogi *flp, unsigned int maxval)
183{
184 unsigned int mfs;
185
186 /*
187 * Get max payload from the common service parameters and the
188 * class 3 receive data field size.
189 */
190 mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
191 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
192 maxval = mfs;
193 mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
194 if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
195 maxval = mfs;
196 return maxval;
197}
198
199/**
200 * fc_rport_state_enter - Change the rport's state
201 * @rport: The rport whose state should change
202 * @new: The new state of the rport
203 *
204 * Locking Note: Called with the rport lock held
205 */
206static void fc_rport_state_enter(struct fc_rport *rport,
207 enum fc_rport_state new)
208{
209 struct fc_rport_libfc_priv *rdata = rport->dd_data;
210 if (rdata->rp_state != new)
211 rdata->retries = 0;
212 rdata->rp_state = new;
213}
214
215static void fc_rport_work(struct work_struct *work)
216{
217 struct fc_rport_libfc_priv *rdata =
218 container_of(work, struct fc_rport_libfc_priv, event_work);
219 enum fc_rport_event event;
220 enum fc_rport_trans_state trans_state;
221 struct fc_lport *lport = rdata->local_port;
222 struct fc_rport_operations *rport_ops;
223 struct fc_rport *rport = PRIV_TO_RPORT(rdata);
224
225 mutex_lock(&rdata->rp_mutex);
226 event = rdata->event;
227 rport_ops = rdata->ops;
228
229 if (event == RPORT_EV_CREATED) {
230 struct fc_rport *new_rport;
231 struct fc_rport_libfc_priv *new_rdata;
232 struct fc_rport_identifiers ids;
233
234 ids.port_id = rport->port_id;
235 ids.roles = rport->roles;
236 ids.port_name = rport->port_name;
237 ids.node_name = rport->node_name;
238
239 mutex_unlock(&rdata->rp_mutex);
240
241 new_rport = fc_remote_port_add(lport->host, 0, &ids);
242 if (new_rport) {
243 /*
244 * Switch from the rogue rport to the rport
245 * returned by the FC class.
246 */
247 new_rport->maxframe_size = rport->maxframe_size;
248
249 new_rdata = new_rport->dd_data;
250 new_rdata->e_d_tov = rdata->e_d_tov;
251 new_rdata->r_a_tov = rdata->r_a_tov;
252 new_rdata->ops = rdata->ops;
253 new_rdata->local_port = rdata->local_port;
254 new_rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
255 new_rdata->trans_state = FC_PORTSTATE_REAL;
256 mutex_init(&new_rdata->rp_mutex);
257 INIT_DELAYED_WORK(&new_rdata->retry_work,
258 fc_rport_timeout);
259 INIT_LIST_HEAD(&new_rdata->peers);
260 INIT_WORK(&new_rdata->event_work, fc_rport_work);
261
262 fc_rport_state_enter(new_rport, RPORT_ST_READY);
263 } else {
264 FC_DBG("Failed to create the rport for port "
265 "(%6x).\n", ids.port_id);
266 event = RPORT_EV_FAILED;
267 }
268 put_device(&rport->dev);
269 rport = new_rport;
270 rdata = new_rport->dd_data;
271 if (rport_ops->event_callback)
272 rport_ops->event_callback(lport, rport, event);
273 } else if ((event == RPORT_EV_FAILED) ||
274 (event == RPORT_EV_LOGO) ||
275 (event == RPORT_EV_STOP)) {
276 trans_state = rdata->trans_state;
277 mutex_unlock(&rdata->rp_mutex);
278 if (rport_ops->event_callback)
279 rport_ops->event_callback(lport, rport, event);
280 if (trans_state == FC_PORTSTATE_ROGUE)
281 put_device(&rport->dev);
282 else
283 fc_remote_port_delete(rport);
284 } else
285 mutex_unlock(&rdata->rp_mutex);
286}
287
288/**
289 * fc_rport_login - Start the remote port login state machine
290 * @rport: Fibre Channel remote port
291 *
292 * Locking Note: Called without the rport lock held. This
293 * function will hold the rport lock, call an _enter_*
294 * function and then unlock the rport.
295 */
296int fc_rport_login(struct fc_rport *rport)
297{
298 struct fc_rport_libfc_priv *rdata = rport->dd_data;
299
300 mutex_lock(&rdata->rp_mutex);
301
302 FC_DEBUG_RPORT("Login to port (%6x)\n", rport->port_id);
303
304 fc_rport_enter_plogi(rport);
305
306 mutex_unlock(&rdata->rp_mutex);
307
308 return 0;
309}
310
311/**
312 * fc_rport_logoff - Logoff and remove an rport
313 * @rport: Fibre Channel remote port to be removed
314 *
315 * Locking Note: Called without the rport lock held. This
316 * function will hold the rport lock, call an _enter_*
317 * function and then unlock the rport.
318 */
319int fc_rport_logoff(struct fc_rport *rport)
320{
321 struct fc_rport_libfc_priv *rdata = rport->dd_data;
322
323 mutex_lock(&rdata->rp_mutex);
324
325 FC_DEBUG_RPORT("Remove port (%6x)\n", rport->port_id);
326
327 fc_rport_enter_logo(rport);
328
329 /*
330 * Change the state to NONE so that we discard
331 * the response.
332 */
333 fc_rport_state_enter(rport, RPORT_ST_NONE);
334
335 mutex_unlock(&rdata->rp_mutex);
336
337 cancel_delayed_work_sync(&rdata->retry_work);
338
339 mutex_lock(&rdata->rp_mutex);
340
341 rdata->event = RPORT_EV_STOP;
342 queue_work(rport_event_queue, &rdata->event_work);
343
344 mutex_unlock(&rdata->rp_mutex);
345
346 return 0;
347}
348
349/**
350 * fc_rport_enter_ready - The rport is ready
351 * @rport: Fibre Channel remote port that is ready
352 *
353 * Locking Note: The rport lock is expected to be held before calling
354 * this routine.
355 */
356static void fc_rport_enter_ready(struct fc_rport *rport)
357{
358 struct fc_rport_libfc_priv *rdata = rport->dd_data;
359
360 fc_rport_state_enter(rport, RPORT_ST_READY);
361
362 FC_DEBUG_RPORT("Port (%6x) is Ready\n", rport->port_id);
363
364 rdata->event = RPORT_EV_CREATED;
365 queue_work(rport_event_queue, &rdata->event_work);
366}
367
368/**
369 * fc_rport_timeout - Handler for the retry_work timer.
370 * @work: The work struct of the fc_rport_libfc_priv
371 *
372 * Locking Note: Called without the rport lock held. This
373 * function will hold the rport lock, call an _enter_*
374 * function and then unlock the rport.
375 */
376static void fc_rport_timeout(struct work_struct *work)
377{
378 struct fc_rport_libfc_priv *rdata =
379 container_of(work, struct fc_rport_libfc_priv, retry_work.work);
380 struct fc_rport *rport = PRIV_TO_RPORT(rdata);
381
382 mutex_lock(&rdata->rp_mutex);
383
384 switch (rdata->rp_state) {
385 case RPORT_ST_PLOGI:
386 fc_rport_enter_plogi(rport);
387 break;
388 case RPORT_ST_PRLI:
389 fc_rport_enter_prli(rport);
390 break;
391 case RPORT_ST_RTV:
392 fc_rport_enter_rtv(rport);
393 break;
394 case RPORT_ST_LOGO:
395 fc_rport_enter_logo(rport);
396 break;
397 case RPORT_ST_READY:
398 case RPORT_ST_INIT:
399 case RPORT_ST_NONE:
400 break;
401 }
402
403 mutex_unlock(&rdata->rp_mutex);
404 put_device(&rport->dev);
405}
406
407/**
408 * fc_rport_error - Handler for any errors
409 * @rport: The fc_rport object
410 * @fp: The frame pointer
411 *
412 * If the error was caused by a resource allocation failure
413 * then wait for half a second and retry, otherwise retry
414 * immediately.
415 *
416 * Locking Note: The rport lock is expected to be held before
417 * calling this routine
418 */
419static void fc_rport_error(struct fc_rport *rport, struct fc_frame *fp)
420{
421 struct fc_rport_libfc_priv *rdata = rport->dd_data;
422 unsigned long delay = 0;
423
424 FC_DEBUG_RPORT("Error %ld in state %s, retries %d\n",
425 PTR_ERR(fp), fc_rport_state(rport), rdata->retries);
426
427 if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
428 /*
429 * Memory allocation failure, or the exchange timed out.
430 * Retry after delay
431 */
432 if (rdata->retries < rdata->local_port->max_retry_count) {
433 rdata->retries++;
434 if (!fp)
435 delay = msecs_to_jiffies(500);
436 get_device(&rport->dev);
437 schedule_delayed_work(&rdata->retry_work, delay);
438 } else {
439 switch (rdata->rp_state) {
440 case RPORT_ST_PLOGI:
441 case RPORT_ST_PRLI:
442 case RPORT_ST_LOGO:
443 rdata->event = RPORT_EV_FAILED;
444 queue_work(rport_event_queue,
445 &rdata->event_work);
446 break;
447 case RPORT_ST_RTV:
448 fc_rport_enter_ready(rport);
449 break;
450 case RPORT_ST_NONE:
451 case RPORT_ST_READY:
452 case RPORT_ST_INIT:
453 break;
454 }
455 }
456 }
457}
458
459/**
460 * fc_rport_plogi_recv_resp - Handle incoming ELS PLOGI response
461 * @sp: current sequence in the PLOGI exchange
462 * @fp: response frame
463 * @rp_arg: Fibre Channel remote port
464 *
465 * Locking Note: This function will be called without the rport lock
466 * held, but it will lock, call an _enter_* function or fc_rport_error
467 * and then unlock the rport.
468 */
469static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
470 void *rp_arg)
471{
472 struct fc_rport *rport = rp_arg;
473 struct fc_rport_libfc_priv *rdata = rport->dd_data;
474 struct fc_lport *lport = rdata->local_port;
475 struct fc_els_flogi *plp;
476 unsigned int tov;
477 u16 csp_seq;
478 u16 cssp_seq;
479 u8 op;
480
481 mutex_lock(&rdata->rp_mutex);
482
483 FC_DEBUG_RPORT("Received a PLOGI response from port (%6x)\n",
484 rport->port_id);
485
486 if (rdata->rp_state != RPORT_ST_PLOGI) {
487 FC_DBG("Received a PLOGI response, but in state %s\n",
488 fc_rport_state(rport));
489 goto out;
490 }
491
492 if (IS_ERR(fp)) {
493 fc_rport_error(rport, fp);
494 goto err;
495 }
496
497 op = fc_frame_payload_op(fp);
498 if (op == ELS_LS_ACC &&
499 (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
500 rport->port_name = get_unaligned_be64(&plp->fl_wwpn);
501 rport->node_name = get_unaligned_be64(&plp->fl_wwnn);
502
503 tov = ntohl(plp->fl_csp.sp_e_d_tov);
504 if (ntohs(plp->fl_csp.sp_features) & FC_SP_FT_EDTR)
505 tov /= 1000;
506 if (tov > rdata->e_d_tov)
507 rdata->e_d_tov = tov;
508 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
509 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
510 if (cssp_seq < csp_seq)
511 csp_seq = cssp_seq;
512 rdata->max_seq = csp_seq;
513 rport->maxframe_size =
514 fc_plogi_get_maxframe(plp, lport->mfs);
515
516 /*
517 * If the rport is one of the well known addresses
518 * we skip PRLI and RTV and go straight to READY.
519 */
520 if (rport->port_id >= FC_FID_DOM_MGR)
521 fc_rport_enter_ready(rport);
522 else
523 fc_rport_enter_prli(rport);
524 } else
525 fc_rport_error(rport, fp);
526
527out:
528 fc_frame_free(fp);
529err:
530 mutex_unlock(&rdata->rp_mutex);
531 put_device(&rport->dev);
532}
533
534/**
535 * fc_rport_enter_plogi - Send Port Login (PLOGI) request to peer
536 * @rport: Fibre Channel remote port to send PLOGI to
537 *
538 * Locking Note: The rport lock is expected to be held before calling
539 * this routine.
540 */
541static void fc_rport_enter_plogi(struct fc_rport *rport)
542{
543 struct fc_rport_libfc_priv *rdata = rport->dd_data;
544 struct fc_lport *lport = rdata->local_port;
545 struct fc_frame *fp;
546
547 FC_DEBUG_RPORT("Port (%6x) entered PLOGI state from %s state\n",
548 rport->port_id, fc_rport_state(rport));
549
550 fc_rport_state_enter(rport, RPORT_ST_PLOGI);
551
552 rport->maxframe_size = FC_MIN_MAX_PAYLOAD;
553 fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
554 if (!fp) {
555 fc_rport_error(rport, fp);
556 return;
557 }
558 rdata->e_d_tov = lport->e_d_tov;
559
560 if (!lport->tt.elsct_send(lport, rport, fp, ELS_PLOGI,
561 fc_rport_plogi_resp, rport, lport->e_d_tov))
562 fc_rport_error(rport, fp);
563 else
564 get_device(&rport->dev);
565}
566
567/**
568 * fc_rport_prli_resp - Process Login (PRLI) response handler
569 * @sp: current sequence in the PRLI exchange
570 * @fp: response frame
571 * @rp_arg: Fibre Channel remote port
572 *
573 * Locking Note: This function will be called without the rport lock
574 * held, but it will lock, call an _enter_* function or fc_rport_error
575 * and then unlock the rport.
576 */
577static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
578 void *rp_arg)
579{
580 struct fc_rport *rport = rp_arg;
581 struct fc_rport_libfc_priv *rdata = rport->dd_data;
582 struct {
583 struct fc_els_prli prli;
584 struct fc_els_spp spp;
585 } *pp;
586 u32 roles = FC_RPORT_ROLE_UNKNOWN;
587 u32 fcp_parm = 0;
588 u8 op;
589
590 mutex_lock(&rdata->rp_mutex);
591
592 FC_DEBUG_RPORT("Received a PRLI response from port (%6x)\n",
593 rport->port_id);
594
595 if (rdata->rp_state != RPORT_ST_PRLI) {
596 FC_DBG("Received a PRLI response, but in state %s\n",
597 fc_rport_state(rport));
598 goto out;
599 }
600
601 if (IS_ERR(fp)) {
602 fc_rport_error(rport, fp);
603 goto err;
604 }
605
606 op = fc_frame_payload_op(fp);
607 if (op == ELS_LS_ACC) {
608 pp = fc_frame_payload_get(fp, sizeof(*pp));
609 if (pp && pp->prli.prli_spp_len >= sizeof(pp->spp)) {
610 fcp_parm = ntohl(pp->spp.spp_params);
611 if (fcp_parm & FCP_SPPF_RETRY)
612 rdata->flags |= FC_RP_FLAGS_RETRY;
613 }
614
615 rport->supported_classes = FC_COS_CLASS3;
616 if (fcp_parm & FCP_SPPF_INIT_FCN)
617 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
618 if (fcp_parm & FCP_SPPF_TARG_FCN)
619 roles |= FC_RPORT_ROLE_FCP_TARGET;
620
621 rport->roles = roles;
622 fc_rport_enter_rtv(rport);
623
624 } else {
625 FC_DBG("Bad ELS response\n");
626 rdata->event = RPORT_EV_FAILED;
627 queue_work(rport_event_queue, &rdata->event_work);
628 }
629
630out:
631 fc_frame_free(fp);
632err:
633 mutex_unlock(&rdata->rp_mutex);
634 put_device(&rport->dev);
635}
636
637/**
638 * fc_rport_logo_resp - Logout (LOGO) response handler
639 * @sp: current sequence in the LOGO exchange
640 * @fp: response frame
641 * @rp_arg: Fibre Channel remote port
642 *
643 * Locking Note: This function will be called without the rport lock
644 * held, but it will lock, call an _enter_* function or fc_rport_error
645 * and then unlock the rport.
646 */
647static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
648 void *rp_arg)
649{
650 struct fc_rport *rport = rp_arg;
651 struct fc_rport_libfc_priv *rdata = rport->dd_data;
652 u8 op;
653
654 mutex_lock(&rdata->rp_mutex);
655
656 FC_DEBUG_RPORT("Received a LOGO response from port (%6x)\n",
657 rport->port_id);
658
659 if (IS_ERR(fp)) {
660 fc_rport_error(rport, fp);
661 goto err;
662 }
663
664 if (rdata->rp_state != RPORT_ST_LOGO) {
665 FC_DEBUG_RPORT("Received a LOGO response, but in state %s\n",
666 fc_rport_state(rport));
667 goto out;
668 }
669
670 op = fc_frame_payload_op(fp);
671 if (op == ELS_LS_ACC) {
672 fc_rport_enter_rtv(rport);
673 } else {
674 FC_DBG("Bad ELS response\n");
675 rdata->event = RPORT_EV_LOGO;
676 queue_work(rport_event_queue, &rdata->event_work);
677 }
678
679out:
680 fc_frame_free(fp);
681err:
682 mutex_unlock(&rdata->rp_mutex);
683 put_device(&rport->dev);
684}
685
686/**
687 * fc_rport_enter_prli - Send Process Login (PRLI) request to peer
688 * @rport: Fibre Channel remote port to send PRLI to
689 *
690 * Locking Note: The rport lock is expected to be held before calling
691 * this routine.
692 */
693static void fc_rport_enter_prli(struct fc_rport *rport)
694{
695 struct fc_rport_libfc_priv *rdata = rport->dd_data;
696 struct fc_lport *lport = rdata->local_port;
697 struct {
698 struct fc_els_prli prli;
699 struct fc_els_spp spp;
700 } *pp;
701 struct fc_frame *fp;
702
703 FC_DEBUG_RPORT("Port (%6x) entered PRLI state from %s state\n",
704 rport->port_id, fc_rport_state(rport));
705
706 fc_rport_state_enter(rport, RPORT_ST_PRLI);
707
708 fp = fc_frame_alloc(lport, sizeof(*pp));
709 if (!fp) {
710 fc_rport_error(rport, fp);
711 return;
712 }
713
714 if (!lport->tt.elsct_send(lport, rport, fp, ELS_PRLI,
715 fc_rport_prli_resp, rport, lport->e_d_tov))
716 fc_rport_error(rport, fp);
717 else
718 get_device(&rport->dev);
719}
720
721/**
722 * fc_rport_els_rtv_resp - Request Timeout Value response handler
723 * @sp: current sequence in the RTV exchange
724 * @fp: response frame
725 * @rp_arg: Fibre Channel remote port
726 *
727 * Many targets don't seem to support this.
728 *
729 * Locking Note: This function will be called without the rport lock
730 * held, but it will lock, call an _enter_* function or fc_rport_error
731 * and then unlock the rport.
732 */
733static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
734 void *rp_arg)
735{
736 struct fc_rport *rport = rp_arg;
737 struct fc_rport_libfc_priv *rdata = rport->dd_data;
738 u8 op;
739
740 mutex_lock(&rdata->rp_mutex);
741
742 FC_DEBUG_RPORT("Received a RTV response from port (%6x)\n",
743 rport->port_id);
744
745 if (rdata->rp_state != RPORT_ST_RTV) {
746 FC_DBG("Received a RTV response, but in state %s\n",
747 fc_rport_state(rport));
748 goto out;
749 }
750
751 if (IS_ERR(fp)) {
752 fc_rport_error(rport, fp);
753 goto err;
754 }
755
756 op = fc_frame_payload_op(fp);
757 if (op == ELS_LS_ACC) {
758 struct fc_els_rtv_acc *rtv;
759 u32 toq;
760 u32 tov;
761
762 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
763 if (rtv) {
764 toq = ntohl(rtv->rtv_toq);
765 tov = ntohl(rtv->rtv_r_a_tov);
766 if (tov == 0)
767 tov = 1;
768 rdata->r_a_tov = tov;
769 tov = ntohl(rtv->rtv_e_d_tov);
770 if (toq & FC_ELS_RTV_EDRES)
771 tov /= 1000000;
772 if (tov == 0)
773 tov = 1;
774 rdata->e_d_tov = tov;
775 }
776 }
777
778 fc_rport_enter_ready(rport);
779
780out:
781 fc_frame_free(fp);
782err:
783 mutex_unlock(&rdata->rp_mutex);
784 put_device(&rport->dev);
785}
786
787/**
788 * fc_rport_enter_rtv - Send Request Timeout Value (RTV) request to peer
789 * @rport: Fibre Channel remote port to send RTV to
790 *
791 * Locking Note: The rport lock is expected to be held before calling
792 * this routine.
793 */
794static void fc_rport_enter_rtv(struct fc_rport *rport)
795{
796 struct fc_frame *fp;
797 struct fc_rport_libfc_priv *rdata = rport->dd_data;
798 struct fc_lport *lport = rdata->local_port;
799
800 FC_DEBUG_RPORT("Port (%6x) entered RTV state from %s state\n",
801 rport->port_id, fc_rport_state(rport));
802
803 fc_rport_state_enter(rport, RPORT_ST_RTV);
804
805 fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
806 if (!fp) {
807 fc_rport_error(rport, fp);
808 return;
809 }
810
811 if (!lport->tt.elsct_send(lport, rport, fp, ELS_RTV,
812 fc_rport_rtv_resp, rport, lport->e_d_tov))
813 fc_rport_error(rport, fp);
814 else
815 get_device(&rport->dev);
816}
817
818/**
819 * fc_rport_enter_logo - Send Logout (LOGO) request to peer
820 * @rport: Fibre Channel remote port to send LOGO to
821 *
822 * Locking Note: The rport lock is expected to be held before calling
823 * this routine.
824 */
825static void fc_rport_enter_logo(struct fc_rport *rport)
826{
827 struct fc_rport_libfc_priv *rdata = rport->dd_data;
828 struct fc_lport *lport = rdata->local_port;
829 struct fc_frame *fp;
830
831 FC_DEBUG_RPORT("Port (%6x) entered LOGO state from %s state\n",
832 rport->port_id, fc_rport_state(rport));
833
834 fc_rport_state_enter(rport, RPORT_ST_LOGO);
835
836 fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
837 if (!fp) {
838 fc_rport_error(rport, fp);
839 return;
840 }
841
842 if (!lport->tt.elsct_send(lport, rport, fp, ELS_LOGO,
843 fc_rport_logo_resp, rport, lport->e_d_tov))
844 fc_rport_error(rport, fp);
845 else
846 get_device(&rport->dev);
847}
848
849
850/**
851 * fc_rport_recv_req - Receive a request from a rport
852 * @sp: current sequence in the PLOGI exchange
853 * @fp: response frame
854 * @rp_arg: Fibre Channel remote port
855 *
856 * Locking Note: Called without the rport lock held. This
857 * function will hold the rport lock, call an _enter_*
858 * function and then unlock the rport.
859 */
860void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
861 struct fc_rport *rport)
862{
863 struct fc_rport_libfc_priv *rdata = rport->dd_data;
864 struct fc_lport *lport = rdata->local_port;
865
866 struct fc_frame_header *fh;
867 struct fc_seq_els_data els_data;
868 u8 op;
869
870 mutex_lock(&rdata->rp_mutex);
871
872 els_data.fp = NULL;
873 els_data.explan = ELS_EXPL_NONE;
874 els_data.reason = ELS_RJT_NONE;
875
876 fh = fc_frame_header_get(fp);
877
878 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ && fh->fh_type == FC_TYPE_ELS) {
879 op = fc_frame_payload_op(fp);
880 switch (op) {
881 case ELS_PLOGI:
882 fc_rport_recv_plogi_req(rport, sp, fp);
883 break;
884 case ELS_PRLI:
885 fc_rport_recv_prli_req(rport, sp, fp);
886 break;
887 case ELS_PRLO:
888 fc_rport_recv_prlo_req(rport, sp, fp);
889 break;
890 case ELS_LOGO:
891 fc_rport_recv_logo_req(rport, sp, fp);
892 break;
893 case ELS_RRQ:
894 els_data.fp = fp;
895 lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
896 break;
897 case ELS_REC:
898 els_data.fp = fp;
899 lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
900 break;
901 default:
902 els_data.reason = ELS_RJT_UNSUP;
903 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
904 break;
905 }
906 }
907
908 mutex_unlock(&rdata->rp_mutex);
909}
910
911/**
912 * fc_rport_recv_plogi_req - Handle incoming Port Login (PLOGI) request
913 * @rport: Fibre Channel remote port that initiated PLOGI
914 * @sp: current sequence in the PLOGI exchange
915 * @fp: PLOGI request frame
916 *
917 * Locking Note: The rport lock is exected to be held before calling
918 * this function.
919 */
920static void fc_rport_recv_plogi_req(struct fc_rport *rport,
921 struct fc_seq *sp, struct fc_frame *rx_fp)
922{
923 struct fc_rport_libfc_priv *rdata = rport->dd_data;
924 struct fc_lport *lport = rdata->local_port;
925 struct fc_frame *fp = rx_fp;
926 struct fc_exch *ep;
927 struct fc_frame_header *fh;
928 struct fc_els_flogi *pl;
929 struct fc_seq_els_data rjt_data;
930 u32 sid;
931 u64 wwpn;
932 u64 wwnn;
933 enum fc_els_rjt_reason reject = 0;
934 u32 f_ctl;
935 rjt_data.fp = NULL;
936
937 fh = fc_frame_header_get(fp);
938
939 FC_DEBUG_RPORT("Received PLOGI request from port (%6x) "
940 "while in state %s\n", ntoh24(fh->fh_s_id),
941 fc_rport_state(rport));
942
943 sid = ntoh24(fh->fh_s_id);
944 pl = fc_frame_payload_get(fp, sizeof(*pl));
945 if (!pl) {
946 FC_DBG("incoming PLOGI from %x too short\n", sid);
947 WARN_ON(1);
948 /* XXX TBD: send reject? */
949 fc_frame_free(fp);
950 return;
951 }
952 wwpn = get_unaligned_be64(&pl->fl_wwpn);
953 wwnn = get_unaligned_be64(&pl->fl_wwnn);
954
955 /*
956 * If the session was just created, possibly due to the incoming PLOGI,
957 * set the state appropriately and accept the PLOGI.
958 *
959 * If we had also sent a PLOGI, and if the received PLOGI is from a
960 * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
961 * "command already in progress".
962 *
963 * XXX TBD: If the session was ready before, the PLOGI should result in
964 * all outstanding exchanges being reset.
965 */
966 switch (rdata->rp_state) {
967 case RPORT_ST_INIT:
968 FC_DEBUG_RPORT("incoming PLOGI from %6x wwpn %llx state INIT "
969 "- reject\n", sid, wwpn);
970 reject = ELS_RJT_UNSUP;
971 break;
972 case RPORT_ST_PLOGI:
973 FC_DEBUG_RPORT("incoming PLOGI from %x in PLOGI state %d\n",
974 sid, rdata->rp_state);
975 if (wwpn < lport->wwpn)
976 reject = ELS_RJT_INPROG;
977 break;
978 case RPORT_ST_PRLI:
979 case RPORT_ST_READY:
980 FC_DEBUG_RPORT("incoming PLOGI from %x in logged-in state %d "
981 "- ignored for now\n", sid, rdata->rp_state);
982 /* XXX TBD - should reset */
983 break;
984 case RPORT_ST_NONE:
985 default:
986 FC_DEBUG_RPORT("incoming PLOGI from %x in unexpected "
987 "state %d\n", sid, rdata->rp_state);
988 break;
989 }
990
991 if (reject) {
992 rjt_data.reason = reject;
993 rjt_data.explan = ELS_EXPL_NONE;
994 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
995 fc_frame_free(fp);
996 } else {
997 fp = fc_frame_alloc(lport, sizeof(*pl));
998 if (fp == NULL) {
999 fp = rx_fp;
1000 rjt_data.reason = ELS_RJT_UNAB;
1001 rjt_data.explan = ELS_EXPL_NONE;
1002 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1003 fc_frame_free(fp);
1004 } else {
1005 sp = lport->tt.seq_start_next(sp);
1006 WARN_ON(!sp);
1007 fc_rport_set_name(rport, wwpn, wwnn);
1008
1009 /*
1010 * Get session payload size from incoming PLOGI.
1011 */
1012 rport->maxframe_size =
1013 fc_plogi_get_maxframe(pl, lport->mfs);
1014 fc_frame_free(rx_fp);
1015 fc_plogi_fill(lport, fp, ELS_LS_ACC);
1016
1017 /*
1018 * Send LS_ACC. If this fails,
1019 * the originator should retry.
1020 */
1021 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1022 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1023 ep = fc_seq_exch(sp);
1024 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1025 FC_TYPE_ELS, f_ctl, 0);
1026 lport->tt.seq_send(lport, sp, fp);
1027 if (rdata->rp_state == RPORT_ST_PLOGI)
1028 fc_rport_enter_prli(rport);
1029 }
1030 }
1031}
1032
1033/**
1034 * fc_rport_recv_prli_req - Handle incoming Process Login (PRLI) request
1035 * @rport: Fibre Channel remote port that initiated PRLI
1036 * @sp: current sequence in the PRLI exchange
1037 * @fp: PRLI request frame
1038 *
1039 * Locking Note: The rport lock is exected to be held before calling
1040 * this function.
1041 */
1042static void fc_rport_recv_prli_req(struct fc_rport *rport,
1043 struct fc_seq *sp, struct fc_frame *rx_fp)
1044{
1045 struct fc_rport_libfc_priv *rdata = rport->dd_data;
1046 struct fc_lport *lport = rdata->local_port;
1047 struct fc_exch *ep;
1048 struct fc_frame *fp;
1049 struct fc_frame_header *fh;
1050 struct {
1051 struct fc_els_prli prli;
1052 struct fc_els_spp spp;
1053 } *pp;
1054 struct fc_els_spp *rspp; /* request service param page */
1055 struct fc_els_spp *spp; /* response spp */
1056 unsigned int len;
1057 unsigned int plen;
1058 enum fc_els_rjt_reason reason = ELS_RJT_UNAB;
1059 enum fc_els_rjt_explan explan = ELS_EXPL_NONE;
1060 enum fc_els_spp_resp resp;
1061 struct fc_seq_els_data rjt_data;
1062 u32 f_ctl;
1063 u32 fcp_parm;
1064 u32 roles = FC_RPORT_ROLE_UNKNOWN;
1065 rjt_data.fp = NULL;
1066
1067 fh = fc_frame_header_get(rx_fp);
1068
1069 FC_DEBUG_RPORT("Received PRLI request from port (%6x) "
1070 "while in state %s\n", ntoh24(fh->fh_s_id),
1071 fc_rport_state(rport));
1072
1073 switch (rdata->rp_state) {
1074 case RPORT_ST_PRLI:
1075 case RPORT_ST_READY:
1076 reason = ELS_RJT_NONE;
1077 break;
1078 default:
1079 break;
1080 }
1081 len = fr_len(rx_fp) - sizeof(*fh);
1082 pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1083 if (pp == NULL) {
1084 reason = ELS_RJT_PROT;
1085 explan = ELS_EXPL_INV_LEN;
1086 } else {
1087 plen = ntohs(pp->prli.prli_len);
1088 if ((plen % 4) != 0 || plen > len) {
1089 reason = ELS_RJT_PROT;
1090 explan = ELS_EXPL_INV_LEN;
1091 } else if (plen < len) {
1092 len = plen;
1093 }
1094 plen = pp->prli.prli_spp_len;
1095 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1096 plen > len || len < sizeof(*pp)) {
1097 reason = ELS_RJT_PROT;
1098 explan = ELS_EXPL_INV_LEN;
1099 }
1100 rspp = &pp->spp;
1101 }
1102 if (reason != ELS_RJT_NONE ||
1103 (fp = fc_frame_alloc(lport, len)) == NULL) {
1104 rjt_data.reason = reason;
1105 rjt_data.explan = explan;
1106 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1107 } else {
1108 sp = lport->tt.seq_start_next(sp);
1109 WARN_ON(!sp);
1110 pp = fc_frame_payload_get(fp, len);
1111 WARN_ON(!pp);
1112 memset(pp, 0, len);
1113 pp->prli.prli_cmd = ELS_LS_ACC;
1114 pp->prli.prli_spp_len = plen;
1115 pp->prli.prli_len = htons(len);
1116 len -= sizeof(struct fc_els_prli);
1117
1118 /*
1119 * Go through all the service parameter pages and build
1120 * response. If plen indicates longer SPP than standard,
1121 * use that. The entire response has been pre-cleared above.
1122 */
1123 spp = &pp->spp;
1124 while (len >= plen) {
1125 spp->spp_type = rspp->spp_type;
1126 spp->spp_type_ext = rspp->spp_type_ext;
1127 spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
1128 resp = FC_SPP_RESP_ACK;
1129 if (rspp->spp_flags & FC_SPP_RPA_VAL)
1130 resp = FC_SPP_RESP_NO_PA;
1131 switch (rspp->spp_type) {
1132 case 0: /* common to all FC-4 types */
1133 break;
1134 case FC_TYPE_FCP:
1135 fcp_parm = ntohl(rspp->spp_params);
1136 if (fcp_parm * FCP_SPPF_RETRY)
1137 rdata->flags |= FC_RP_FLAGS_RETRY;
1138 rport->supported_classes = FC_COS_CLASS3;
1139 if (fcp_parm & FCP_SPPF_INIT_FCN)
1140 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1141 if (fcp_parm & FCP_SPPF_TARG_FCN)
1142 roles |= FC_RPORT_ROLE_FCP_TARGET;
1143 rport->roles = roles;
1144
1145 spp->spp_params =
1146 htonl(lport->service_params);
1147 break;
1148 default:
1149 resp = FC_SPP_RESP_INVL;
1150 break;
1151 }
1152 spp->spp_flags |= resp;
1153 len -= plen;
1154 rspp = (struct fc_els_spp *)((char *)rspp + plen);
1155 spp = (struct fc_els_spp *)((char *)spp + plen);
1156 }
1157
1158 /*
1159 * Send LS_ACC. If this fails, the originator should retry.
1160 */
1161 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1162 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1163 ep = fc_seq_exch(sp);
1164 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1165 FC_TYPE_ELS, f_ctl, 0);
1166 lport->tt.seq_send(lport, sp, fp);
1167
1168 /*
1169 * Get lock and re-check state.
1170 */
1171 switch (rdata->rp_state) {
1172 case RPORT_ST_PRLI:
1173 fc_rport_enter_ready(rport);
1174 break;
1175 case RPORT_ST_READY:
1176 break;
1177 default:
1178 break;
1179 }
1180 }
1181 fc_frame_free(rx_fp);
1182}
1183
1184/**
1185 * fc_rport_recv_prlo_req - Handle incoming Process Logout (PRLO) request
1186 * @rport: Fibre Channel remote port that initiated PRLO
1187 * @sp: current sequence in the PRLO exchange
1188 * @fp: PRLO request frame
1189 *
1190 * Locking Note: The rport lock is exected to be held before calling
1191 * this function.
1192 */
1193static void fc_rport_recv_prlo_req(struct fc_rport *rport, struct fc_seq *sp,
1194 struct fc_frame *fp)
1195{
1196 struct fc_rport_libfc_priv *rdata = rport->dd_data;
1197 struct fc_lport *lport = rdata->local_port;
1198
1199 struct fc_frame_header *fh;
1200 struct fc_seq_els_data rjt_data;
1201
1202 fh = fc_frame_header_get(fp);
1203
1204 FC_DEBUG_RPORT("Received PRLO request from port (%6x) "
1205 "while in state %s\n", ntoh24(fh->fh_s_id),
1206 fc_rport_state(rport));
1207
1208 rjt_data.fp = NULL;
1209 rjt_data.reason = ELS_RJT_UNAB;
1210 rjt_data.explan = ELS_EXPL_NONE;
1211 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1212 fc_frame_free(fp);
1213}
1214
1215/**
1216 * fc_rport_recv_logo_req - Handle incoming Logout (LOGO) request
1217 * @rport: Fibre Channel remote port that initiated LOGO
1218 * @sp: current sequence in the LOGO exchange
1219 * @fp: LOGO request frame
1220 *
1221 * Locking Note: The rport lock is exected to be held before calling
1222 * this function.
1223 */
1224static void fc_rport_recv_logo_req(struct fc_rport *rport, struct fc_seq *sp,
1225 struct fc_frame *fp)
1226{
1227 struct fc_frame_header *fh;
1228 struct fc_rport_libfc_priv *rdata = rport->dd_data;
1229 struct fc_lport *lport = rdata->local_port;
1230
1231 fh = fc_frame_header_get(fp);
1232
1233 FC_DEBUG_RPORT("Received LOGO request from port (%6x) "
1234 "while in state %s\n", ntoh24(fh->fh_s_id),
1235 fc_rport_state(rport));
1236
1237 rdata->event = RPORT_EV_LOGO;
1238 queue_work(rport_event_queue, &rdata->event_work);
1239
1240 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
1241 fc_frame_free(fp);
1242}
1243
1244static void fc_rport_flush_queue(void)
1245{
1246 flush_workqueue(rport_event_queue);
1247}
1248
1249
1250int fc_rport_init(struct fc_lport *lport)
1251{
1252 if (!lport->tt.rport_login)
1253 lport->tt.rport_login = fc_rport_login;
1254
1255 if (!lport->tt.rport_logoff)
1256 lport->tt.rport_logoff = fc_rport_logoff;
1257
1258 if (!lport->tt.rport_recv_req)
1259 lport->tt.rport_recv_req = fc_rport_recv_req;
1260
1261 if (!lport->tt.rport_flush_queue)
1262 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1263
1264 return 0;
1265}
1266EXPORT_SYMBOL(fc_rport_init);
1267
1268int fc_setup_rport()
1269{
1270 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
1271 if (!rport_event_queue)
1272 return -ENOMEM;
1273 return 0;
1274}
1275EXPORT_SYMBOL(fc_setup_rport);
1276
1277void fc_destroy_rport()
1278{
1279 destroy_workqueue(rport_event_queue);
1280}
1281EXPORT_SYMBOL(fc_destroy_rport);
1282
1283void fc_rport_terminate_io(struct fc_rport *rport)
1284{
1285 struct fc_rport_libfc_priv *rdata = rport->dd_data;
1286 struct fc_lport *lport = rdata->local_port;
1287
1288 lport->tt.exch_mgr_reset(lport->emp, 0, rport->port_id);
1289 lport->tt.exch_mgr_reset(lport->emp, rport->port_id, 0);
1290}
1291EXPORT_SYMBOL(fc_rport_terminate_io);