aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/scsi/libfc
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/scsi/libfc')
-rw-r--r--drivers/scsi/libfc/Makefile4
-rw-r--r--drivers/scsi/libfc/fc_disc.c86
-rw-r--r--drivers/scsi/libfc/fc_elsct.c79
-rw-r--r--drivers/scsi/libfc/fc_exch.c934
-rw-r--r--drivers/scsi/libfc/fc_fcp.c1071
-rw-r--r--drivers/scsi/libfc/fc_frame.c14
-rw-r--r--drivers/scsi/libfc/fc_libfc.c134
-rw-r--r--drivers/scsi/libfc/fc_libfc.h112
-rw-r--r--drivers/scsi/libfc/fc_lport.c864
-rw-r--r--drivers/scsi/libfc/fc_npiv.c161
-rw-r--r--drivers/scsi/libfc/fc_rport.c413
11 files changed, 2569 insertions, 1303 deletions
diff --git a/drivers/scsi/libfc/Makefile b/drivers/scsi/libfc/Makefile
index 55f982de3a9a..4bb23ac86a5c 100644
--- a/drivers/scsi/libfc/Makefile
+++ b/drivers/scsi/libfc/Makefile
@@ -3,10 +3,12 @@
3obj-$(CONFIG_LIBFC) += libfc.o 3obj-$(CONFIG_LIBFC) += libfc.o
4 4
5libfc-objs := \ 5libfc-objs := \
6 fc_libfc.o \
6 fc_disc.o \ 7 fc_disc.o \
7 fc_exch.o \ 8 fc_exch.o \
8 fc_elsct.o \ 9 fc_elsct.o \
9 fc_frame.o \ 10 fc_frame.o \
10 fc_lport.o \ 11 fc_lport.o \
11 fc_rport.o \ 12 fc_rport.o \
12 fc_fcp.o 13 fc_fcp.o \
14 fc_npiv.o
diff --git a/drivers/scsi/libfc/fc_disc.c b/drivers/scsi/libfc/fc_disc.c
index c48799e9dd8e..1087a7f18e84 100644
--- a/drivers/scsi/libfc/fc_disc.c
+++ b/drivers/scsi/libfc/fc_disc.c
@@ -33,6 +33,7 @@
33 */ 33 */
34 34
35#include <linux/timer.h> 35#include <linux/timer.h>
36#include <linux/slab.h>
36#include <linux/err.h> 37#include <linux/err.h>
37#include <asm/unaligned.h> 38#include <asm/unaligned.h>
38 39
@@ -40,6 +41,8 @@
40 41
41#include <scsi/libfc.h> 42#include <scsi/libfc.h>
42 43
44#include "fc_libfc.h"
45
43#define FC_DISC_RETRY_LIMIT 3 /* max retries */ 46#define FC_DISC_RETRY_LIMIT 3 /* max retries */
44#define FC_DISC_RETRY_DELAY 500UL /* (msecs) delay */ 47#define FC_DISC_RETRY_DELAY 500UL /* (msecs) delay */
45 48
@@ -51,8 +54,8 @@ static int fc_disc_single(struct fc_lport *, struct fc_disc_port *);
51static void fc_disc_restart(struct fc_disc *); 54static void fc_disc_restart(struct fc_disc *);
52 55
53/** 56/**
54 * fc_disc_stop_rports() - delete all the remote ports associated with the lport 57 * fc_disc_stop_rports() - Delete all the remote ports associated with the lport
55 * @disc: The discovery job to stop rports on 58 * @disc: The discovery job to stop remote ports on
56 * 59 *
57 * Locking Note: This function expects that the lport mutex is locked before 60 * Locking Note: This function expects that the lport mutex is locked before
58 * calling it. 61 * calling it.
@@ -72,9 +75,9 @@ void fc_disc_stop_rports(struct fc_disc *disc)
72 75
73/** 76/**
74 * fc_disc_recv_rscn_req() - Handle Registered State Change Notification (RSCN) 77 * fc_disc_recv_rscn_req() - Handle Registered State Change Notification (RSCN)
75 * @sp: Current sequence of the RSCN exchange 78 * @sp: The sequence of the RSCN exchange
76 * @fp: RSCN Frame 79 * @fp: The RSCN frame
77 * @lport: Fibre Channel host port instance 80 * @lport: The local port that the request will be sent on
78 * 81 *
79 * Locking Note: This function expects that the disc_mutex is locked 82 * Locking Note: This function expects that the disc_mutex is locked
80 * before it is called. 83 * before it is called.
@@ -183,9 +186,9 @@ reject:
183 186
184/** 187/**
185 * fc_disc_recv_req() - Handle incoming requests 188 * fc_disc_recv_req() - Handle incoming requests
186 * @sp: Current sequence of the request exchange 189 * @sp: The sequence of the request exchange
187 * @fp: The frame 190 * @fp: The request frame
188 * @lport: The FC local port 191 * @lport: The local port receiving the request
189 * 192 *
190 * Locking Note: This function is called from the EM and will lock 193 * Locking Note: This function is called from the EM and will lock
191 * the disc_mutex before calling the handler for the 194 * the disc_mutex before calling the handler for the
@@ -213,7 +216,7 @@ static void fc_disc_recv_req(struct fc_seq *sp, struct fc_frame *fp,
213 216
214/** 217/**
215 * fc_disc_restart() - Restart discovery 218 * fc_disc_restart() - Restart discovery
216 * @lport: FC discovery context 219 * @disc: The discovery object to be restarted
217 * 220 *
218 * Locking Note: This function expects that the disc mutex 221 * Locking Note: This function expects that the disc mutex
219 * is already locked. 222 * is already locked.
@@ -240,9 +243,9 @@ static void fc_disc_restart(struct fc_disc *disc)
240} 243}
241 244
242/** 245/**
243 * fc_disc_start() - Fibre Channel Target discovery 246 * fc_disc_start() - Start discovery on a local port
244 * @lport: FC local port 247 * @lport: The local port to have discovery started on
245 * @disc_callback: function to be called when discovery is complete 248 * @disc_callback: Callback function to be called when discovery is complete
246 */ 249 */
247static void fc_disc_start(void (*disc_callback)(struct fc_lport *, 250static void fc_disc_start(void (*disc_callback)(struct fc_lport *,
248 enum fc_disc_event), 251 enum fc_disc_event),
@@ -263,8 +266,8 @@ static void fc_disc_start(void (*disc_callback)(struct fc_lport *,
263 266
264/** 267/**
265 * fc_disc_done() - Discovery has been completed 268 * fc_disc_done() - Discovery has been completed
266 * @disc: FC discovery context 269 * @disc: The discovery context
267 * @event: discovery completion status 270 * @event: The discovery completion status
268 * 271 *
269 * Locking Note: This function expects that the disc mutex is locked before 272 * Locking Note: This function expects that the disc mutex is locked before
270 * it is called. The discovery callback is then made with the lock released, 273 * it is called. The discovery callback is then made with the lock released,
@@ -284,8 +287,8 @@ static void fc_disc_done(struct fc_disc *disc, enum fc_disc_event event)
284 } 287 }
285 288
286 /* 289 /*
287 * Go through all remote ports. If they were found in the latest 290 * Go through all remote ports. If they were found in the latest
288 * discovery, reverify or log them in. Otherwise, log them out. 291 * discovery, reverify or log them in. Otherwise, log them out.
289 * Skip ports which were never discovered. These are the dNS port 292 * Skip ports which were never discovered. These are the dNS port
290 * and ports which were created by PLOGI. 293 * and ports which were created by PLOGI.
291 */ 294 */
@@ -305,8 +308,8 @@ static void fc_disc_done(struct fc_disc *disc, enum fc_disc_event event)
305 308
306/** 309/**
307 * fc_disc_error() - Handle error on dNS request 310 * fc_disc_error() - Handle error on dNS request
308 * @disc: FC discovery context 311 * @disc: The discovery context
309 * @fp: The frame pointer 312 * @fp: The error code encoded as a frame pointer
310 */ 313 */
311static void fc_disc_error(struct fc_disc *disc, struct fc_frame *fp) 314static void fc_disc_error(struct fc_disc *disc, struct fc_frame *fp)
312{ 315{
@@ -342,7 +345,7 @@ static void fc_disc_error(struct fc_disc *disc, struct fc_frame *fp)
342 345
343/** 346/**
344 * fc_disc_gpn_ft_req() - Send Get Port Names by FC-4 type (GPN_FT) request 347 * fc_disc_gpn_ft_req() - Send Get Port Names by FC-4 type (GPN_FT) request
345 * @lport: FC discovery context 348 * @lport: The discovery context
346 * 349 *
347 * Locking Note: This function expects that the disc_mutex is locked 350 * Locking Note: This function expects that the disc_mutex is locked
348 * before it is called. 351 * before it is called.
@@ -368,17 +371,17 @@ static void fc_disc_gpn_ft_req(struct fc_disc *disc)
368 if (lport->tt.elsct_send(lport, 0, fp, 371 if (lport->tt.elsct_send(lport, 0, fp,
369 FC_NS_GPN_FT, 372 FC_NS_GPN_FT,
370 fc_disc_gpn_ft_resp, 373 fc_disc_gpn_ft_resp,
371 disc, lport->e_d_tov)) 374 disc, 3 * lport->r_a_tov))
372 return; 375 return;
373err: 376err:
374 fc_disc_error(disc, fp); 377 fc_disc_error(disc, NULL);
375} 378}
376 379
377/** 380/**
378 * fc_disc_gpn_ft_parse() - Parse the body of the dNS GPN_FT response. 381 * fc_disc_gpn_ft_parse() - Parse the body of the dNS GPN_FT response.
379 * @lport: Fibre Channel host port instance 382 * @lport: The local port the GPN_FT was received on
380 * @buf: GPN_FT response buffer 383 * @buf: The GPN_FT response buffer
381 * @len: size of response buffer 384 * @len: The size of response buffer
382 * 385 *
383 * Goes through the list of IDs and names resulting from a request. 386 * Goes through the list of IDs and names resulting from a request.
384 */ 387 */
@@ -477,10 +480,8 @@ static int fc_disc_gpn_ft_parse(struct fc_disc *disc, void *buf, size_t len)
477} 480}
478 481
479/** 482/**
480 * fc_disc_timeout() - Retry handler for the disc component 483 * fc_disc_timeout() - Handler for discovery timeouts
481 * @work: Structure holding disc obj that needs retry discovery 484 * @work: Structure holding discovery context that needs to retry discovery
482 *
483 * Handle retry of memory allocation for remote ports.
484 */ 485 */
485static void fc_disc_timeout(struct work_struct *work) 486static void fc_disc_timeout(struct work_struct *work)
486{ 487{
@@ -494,9 +495,9 @@ static void fc_disc_timeout(struct work_struct *work)
494 495
495/** 496/**
496 * fc_disc_gpn_ft_resp() - Handle a response frame from Get Port Names (GPN_FT) 497 * fc_disc_gpn_ft_resp() - Handle a response frame from Get Port Names (GPN_FT)
497 * @sp: Current sequence of GPN_FT exchange 498 * @sp: The sequence that the GPN_FT response was received on
498 * @fp: response frame 499 * @fp: The GPN_FT response frame
499 * @lp_arg: Fibre Channel host port instance 500 * @lp_arg: The discovery context
500 * 501 *
501 * Locking Note: This function is called without disc mutex held, and 502 * Locking Note: This function is called without disc mutex held, and
502 * should do all its processing with the mutex held 503 * should do all its processing with the mutex held
@@ -567,9 +568,9 @@ static void fc_disc_gpn_ft_resp(struct fc_seq *sp, struct fc_frame *fp,
567 568
568/** 569/**
569 * fc_disc_gpn_id_resp() - Handle a response frame from Get Port Names (GPN_ID) 570 * fc_disc_gpn_id_resp() - Handle a response frame from Get Port Names (GPN_ID)
570 * @sp: exchange sequence 571 * @sp: The sequence the GPN_ID is on
571 * @fp: response frame 572 * @fp: The response frame
572 * @rdata_arg: remote port private data 573 * @rdata_arg: The remote port that sent the GPN_ID response
573 * 574 *
574 * Locking Note: This function is called without disc mutex held. 575 * Locking Note: This function is called without disc mutex held.
575 */ 576 */
@@ -637,7 +638,7 @@ out:
637 638
638/** 639/**
639 * fc_disc_gpn_id_req() - Send Get Port Names by ID (GPN_ID) request 640 * fc_disc_gpn_id_req() - Send Get Port Names by ID (GPN_ID) request
640 * @lport: local port 641 * @lport: The local port to initiate discovery on
641 * @rdata: remote port private data 642 * @rdata: remote port private data
642 * 643 *
643 * Locking Note: This function expects that the disc_mutex is locked 644 * Locking Note: This function expects that the disc_mutex is locked
@@ -654,7 +655,8 @@ static int fc_disc_gpn_id_req(struct fc_lport *lport,
654 if (!fp) 655 if (!fp)
655 return -ENOMEM; 656 return -ENOMEM;
656 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, FC_NS_GPN_ID, 657 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, FC_NS_GPN_ID,
657 fc_disc_gpn_id_resp, rdata, lport->e_d_tov)) 658 fc_disc_gpn_id_resp, rdata,
659 3 * lport->r_a_tov))
658 return -ENOMEM; 660 return -ENOMEM;
659 kref_get(&rdata->kref); 661 kref_get(&rdata->kref);
660 return 0; 662 return 0;
@@ -662,8 +664,8 @@ static int fc_disc_gpn_id_req(struct fc_lport *lport,
662 664
663/** 665/**
664 * fc_disc_single() - Discover the directory information for a single target 666 * fc_disc_single() - Discover the directory information for a single target
665 * @lport: local port 667 * @lport: The local port the remote port is associated with
666 * @dp: The port to rediscover 668 * @dp: The port to rediscover
667 * 669 *
668 * Locking Note: This function expects that the disc_mutex is locked 670 * Locking Note: This function expects that the disc_mutex is locked
669 * before it is called. 671 * before it is called.
@@ -681,7 +683,7 @@ static int fc_disc_single(struct fc_lport *lport, struct fc_disc_port *dp)
681 683
682/** 684/**
683 * fc_disc_stop() - Stop discovery for a given lport 685 * fc_disc_stop() - Stop discovery for a given lport
684 * @lport: The lport that discovery should stop for 686 * @lport: The local port that discovery should stop on
685 */ 687 */
686void fc_disc_stop(struct fc_lport *lport) 688void fc_disc_stop(struct fc_lport *lport)
687{ 689{
@@ -695,7 +697,7 @@ void fc_disc_stop(struct fc_lport *lport)
695 697
696/** 698/**
697 * fc_disc_stop_final() - Stop discovery for a given lport 699 * fc_disc_stop_final() - Stop discovery for a given lport
698 * @lport: The lport that discovery should stop for 700 * @lport: The lport that discovery should stop on
699 * 701 *
700 * This function will block until discovery has been 702 * This function will block until discovery has been
701 * completely stopped and all rports have been deleted. 703 * completely stopped and all rports have been deleted.
@@ -707,8 +709,8 @@ void fc_disc_stop_final(struct fc_lport *lport)
707} 709}
708 710
709/** 711/**
710 * fc_disc_init() - Initialize the discovery block 712 * fc_disc_init() - Initialize the discovery layer for a local port
711 * @lport: FC local port 713 * @lport: The local port that needs the discovery layer to be initialized
712 */ 714 */
713int fc_disc_init(struct fc_lport *lport) 715int fc_disc_init(struct fc_lport *lport)
714{ 716{
diff --git a/drivers/scsi/libfc/fc_elsct.c b/drivers/scsi/libfc/fc_elsct.c
index 5cfa68732e9d..53748724f2c5 100644
--- a/drivers/scsi/libfc/fc_elsct.c
+++ b/drivers/scsi/libfc/fc_elsct.c
@@ -28,17 +28,22 @@
28#include <scsi/libfc.h> 28#include <scsi/libfc.h>
29#include <scsi/fc_encode.h> 29#include <scsi/fc_encode.h>
30 30
31/* 31/**
32 * fc_elsct_send - sends ELS/CT frame 32 * fc_elsct_send() - Send an ELS or CT frame
33 * @lport: The local port to send the frame on
34 * @did: The destination ID for the frame
35 * @fp: The frame to be sent
36 * @op: The operational code
37 * @resp: The callback routine when the response is received
38 * @arg: The argument to pass to the response callback routine
39 * @timer_msec: The timeout period for the frame (in msecs)
33 */ 40 */
34static struct fc_seq *fc_elsct_send(struct fc_lport *lport, 41struct fc_seq *fc_elsct_send(struct fc_lport *lport, u32 did,
35 u32 did, 42 struct fc_frame *fp, unsigned int op,
36 struct fc_frame *fp, 43 void (*resp)(struct fc_seq *,
37 unsigned int op, 44 struct fc_frame *,
38 void (*resp)(struct fc_seq *, 45 void *),
39 struct fc_frame *fp, 46 void *arg, u32 timer_msec)
40 void *arg),
41 void *arg, u32 timer_msec)
42{ 47{
43 enum fc_rctl r_ctl; 48 enum fc_rctl r_ctl;
44 enum fc_fh_type fh_type; 49 enum fc_fh_type fh_type;
@@ -53,15 +58,22 @@ static struct fc_seq *fc_elsct_send(struct fc_lport *lport,
53 did = FC_FID_DIR_SERV; 58 did = FC_FID_DIR_SERV;
54 } 59 }
55 60
56 if (rc) 61 if (rc) {
62 fc_frame_free(fp);
57 return NULL; 63 return NULL;
64 }
58 65
59 fc_fill_fc_hdr(fp, r_ctl, did, fc_host_port_id(lport->host), fh_type, 66 fc_fill_fc_hdr(fp, r_ctl, did, fc_host_port_id(lport->host), fh_type,
60 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0); 67 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
61 68
62 return lport->tt.exch_seq_send(lport, fp, resp, NULL, arg, timer_msec); 69 return lport->tt.exch_seq_send(lport, fp, resp, NULL, arg, timer_msec);
63} 70}
71EXPORT_SYMBOL(fc_elsct_send);
64 72
73/**
74 * fc_elsct_init() - Initialize the ELS/CT layer
75 * @lport: The local port to initialize the ELS/CT layer for
76 */
65int fc_elsct_init(struct fc_lport *lport) 77int fc_elsct_init(struct fc_lport *lport)
66{ 78{
67 if (!lport->tt.elsct_send) 79 if (!lport->tt.elsct_send)
@@ -72,12 +84,15 @@ int fc_elsct_init(struct fc_lport *lport)
72EXPORT_SYMBOL(fc_elsct_init); 84EXPORT_SYMBOL(fc_elsct_init);
73 85
74/** 86/**
75 * fc_els_resp_type() - return string describing ELS response for debug. 87 * fc_els_resp_type() - Return a string describing the ELS response
76 * @fp: frame pointer with possible error code. 88 * @fp: The frame pointer or possible error code
77 */ 89 */
78const char *fc_els_resp_type(struct fc_frame *fp) 90const char *fc_els_resp_type(struct fc_frame *fp)
79{ 91{
80 const char *msg; 92 const char *msg;
93 struct fc_frame_header *fh;
94 struct fc_ct_hdr *ct;
95
81 if (IS_ERR(fp)) { 96 if (IS_ERR(fp)) {
82 switch (-PTR_ERR(fp)) { 97 switch (-PTR_ERR(fp)) {
83 case FC_NO_ERR: 98 case FC_NO_ERR:
@@ -94,15 +109,41 @@ const char *fc_els_resp_type(struct fc_frame *fp)
94 break; 109 break;
95 } 110 }
96 } else { 111 } else {
97 switch (fc_frame_payload_op(fp)) { 112 fh = fc_frame_header_get(fp);
98 case ELS_LS_ACC: 113 switch (fh->fh_type) {
99 msg = "accept"; 114 case FC_TYPE_ELS:
115 switch (fc_frame_payload_op(fp)) {
116 case ELS_LS_ACC:
117 msg = "accept";
118 break;
119 case ELS_LS_RJT:
120 msg = "reject";
121 break;
122 default:
123 msg = "response unknown ELS";
124 break;
125 }
100 break; 126 break;
101 case ELS_LS_RJT: 127 case FC_TYPE_CT:
102 msg = "reject"; 128 ct = fc_frame_payload_get(fp, sizeof(*ct));
129 if (ct) {
130 switch (ntohs(ct->ct_cmd)) {
131 case FC_FS_ACC:
132 msg = "CT accept";
133 break;
134 case FC_FS_RJT:
135 msg = "CT reject";
136 break;
137 default:
138 msg = "response unknown CT";
139 break;
140 }
141 } else {
142 msg = "short CT response";
143 }
103 break; 144 break;
104 default: 145 default:
105 msg = "response unknown ELS"; 146 msg = "response not ELS or CT";
106 break; 147 break;
107 } 148 }
108 } 149 }
diff --git a/drivers/scsi/libfc/fc_exch.c b/drivers/scsi/libfc/fc_exch.c
index c1c15748220c..e5df0d4db67e 100644
--- a/drivers/scsi/libfc/fc_exch.c
+++ b/drivers/scsi/libfc/fc_exch.c
@@ -24,7 +24,7 @@
24 */ 24 */
25 25
26#include <linux/timer.h> 26#include <linux/timer.h>
27#include <linux/gfp.h> 27#include <linux/slab.h>
28#include <linux/err.h> 28#include <linux/err.h>
29 29
30#include <scsi/fc/fc_fc2.h> 30#include <scsi/fc/fc_fc2.h>
@@ -32,10 +32,13 @@
32#include <scsi/libfc.h> 32#include <scsi/libfc.h>
33#include <scsi/fc_encode.h> 33#include <scsi/fc_encode.h>
34 34
35#include "fc_libfc.h"
36
35u16 fc_cpu_mask; /* cpu mask for possible cpus */ 37u16 fc_cpu_mask; /* cpu mask for possible cpus */
36EXPORT_SYMBOL(fc_cpu_mask); 38EXPORT_SYMBOL(fc_cpu_mask);
37static u16 fc_cpu_order; /* 2's power to represent total possible cpus */ 39static u16 fc_cpu_order; /* 2's power to represent total possible cpus */
38static struct kmem_cache *fc_em_cachep; /* cache for exchanges */ 40static struct kmem_cache *fc_em_cachep; /* cache for exchanges */
41struct workqueue_struct *fc_exch_workqueue;
39 42
40/* 43/*
41 * Structure and function definitions for managing Fibre Channel Exchanges 44 * Structure and function definitions for managing Fibre Channel Exchanges
@@ -50,35 +53,46 @@ static struct kmem_cache *fc_em_cachep; /* cache for exchanges */
50 * fc_seq holds the state for an individual sequence. 53 * fc_seq holds the state for an individual sequence.
51 */ 54 */
52 55
53/* 56/**
54 * Per cpu exchange pool 57 * struct fc_exch_pool - Per cpu exchange pool
58 * @next_index: Next possible free exchange index
59 * @total_exches: Total allocated exchanges
60 * @lock: Exch pool lock
61 * @ex_list: List of exchanges
55 * 62 *
56 * This structure manages per cpu exchanges in array of exchange pointers. 63 * This structure manages per cpu exchanges in array of exchange pointers.
57 * This array is allocated followed by struct fc_exch_pool memory for 64 * This array is allocated followed by struct fc_exch_pool memory for
58 * assigned range of exchanges to per cpu pool. 65 * assigned range of exchanges to per cpu pool.
59 */ 66 */
60struct fc_exch_pool { 67struct fc_exch_pool {
61 u16 next_index; /* next possible free exchange index */ 68 u16 next_index;
62 u16 total_exches; /* total allocated exchanges */ 69 u16 total_exches;
63 spinlock_t lock; /* exch pool lock */ 70 spinlock_t lock;
64 struct list_head ex_list; /* allocated exchanges list */ 71 struct list_head ex_list;
65}; 72};
66 73
67/* 74/**
68 * Exchange manager. 75 * struct fc_exch_mgr - The Exchange Manager (EM).
76 * @class: Default class for new sequences
77 * @kref: Reference counter
78 * @min_xid: Minimum exchange ID
79 * @max_xid: Maximum exchange ID
80 * @ep_pool: Reserved exchange pointers
81 * @pool_max_index: Max exch array index in exch pool
82 * @pool: Per cpu exch pool
83 * @stats: Statistics structure
69 * 84 *
70 * This structure is the center for creating exchanges and sequences. 85 * This structure is the center for creating exchanges and sequences.
71 * It manages the allocation of exchange IDs. 86 * It manages the allocation of exchange IDs.
72 */ 87 */
73struct fc_exch_mgr { 88struct fc_exch_mgr {
74 enum fc_class class; /* default class for sequences */ 89 enum fc_class class;
75 struct kref kref; /* exchange mgr reference count */ 90 struct kref kref;
76 u16 min_xid; /* min exchange ID */ 91 u16 min_xid;
77 u16 max_xid; /* max exchange ID */ 92 u16 max_xid;
78 struct list_head ex_list; /* allocated exchanges list */ 93 mempool_t *ep_pool;
79 mempool_t *ep_pool; /* reserve ep's */ 94 u16 pool_max_index;
80 u16 pool_max_index; /* max exch array index in exch pool */ 95 struct fc_exch_pool *pool;
81 struct fc_exch_pool *pool; /* per cpu exch pool */
82 96
83 /* 97 /*
84 * currently exchange mgr stats are updated but not used. 98 * currently exchange mgr stats are updated but not used.
@@ -96,6 +110,18 @@ struct fc_exch_mgr {
96}; 110};
97#define fc_seq_exch(sp) container_of(sp, struct fc_exch, seq) 111#define fc_seq_exch(sp) container_of(sp, struct fc_exch, seq)
98 112
113/**
114 * struct fc_exch_mgr_anchor - primary structure for list of EMs
115 * @ema_list: Exchange Manager Anchor list
116 * @mp: Exchange Manager associated with this anchor
117 * @match: Routine to determine if this anchor's EM should be used
118 *
119 * When walking the list of anchors the match routine will be called
120 * for each anchor to determine if that EM should be used. The last
121 * anchor in the list will always match to handle any exchanges not
122 * handled by other EMs. The non-default EMs would be added to the
123 * anchor list by HW that provides FCoE offloads.
124 */
99struct fc_exch_mgr_anchor { 125struct fc_exch_mgr_anchor {
100 struct list_head ema_list; 126 struct list_head ema_list;
101 struct fc_exch_mgr *mp; 127 struct fc_exch_mgr *mp;
@@ -108,7 +134,6 @@ static void fc_seq_ls_rjt(struct fc_seq *, enum fc_els_rjt_reason,
108 enum fc_els_rjt_explan); 134 enum fc_els_rjt_explan);
109static void fc_exch_els_rec(struct fc_seq *, struct fc_frame *); 135static void fc_exch_els_rec(struct fc_seq *, struct fc_frame *);
110static void fc_exch_els_rrq(struct fc_seq *, struct fc_frame *); 136static void fc_exch_els_rrq(struct fc_seq *, struct fc_frame *);
111static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp);
112 137
113/* 138/*
114 * Internal implementation notes. 139 * Internal implementation notes.
@@ -196,6 +221,15 @@ static char *fc_exch_rctl_names[] = FC_RCTL_NAMES_INIT;
196 221
197#define FC_TABLE_SIZE(x) (sizeof(x) / sizeof(x[0])) 222#define FC_TABLE_SIZE(x) (sizeof(x) / sizeof(x[0]))
198 223
224/**
225 * fc_exch_name_lookup() - Lookup name by opcode
226 * @op: Opcode to be looked up
227 * @table: Opcode/name table
228 * @max_index: Index not to be exceeded
229 *
230 * This routine is used to determine a human-readable string identifying
231 * a R_CTL opcode.
232 */
199static inline const char *fc_exch_name_lookup(unsigned int op, char **table, 233static inline const char *fc_exch_name_lookup(unsigned int op, char **table,
200 unsigned int max_index) 234 unsigned int max_index)
201{ 235{
@@ -208,25 +242,34 @@ static inline const char *fc_exch_name_lookup(unsigned int op, char **table,
208 return name; 242 return name;
209} 243}
210 244
245/**
246 * fc_exch_rctl_name() - Wrapper routine for fc_exch_name_lookup()
247 * @op: The opcode to be looked up
248 */
211static const char *fc_exch_rctl_name(unsigned int op) 249static const char *fc_exch_rctl_name(unsigned int op)
212{ 250{
213 return fc_exch_name_lookup(op, fc_exch_rctl_names, 251 return fc_exch_name_lookup(op, fc_exch_rctl_names,
214 FC_TABLE_SIZE(fc_exch_rctl_names)); 252 FC_TABLE_SIZE(fc_exch_rctl_names));
215} 253}
216 254
217/* 255/**
218 * Hold an exchange - keep it from being freed. 256 * fc_exch_hold() - Increment an exchange's reference count
257 * @ep: Echange to be held
219 */ 258 */
220static void fc_exch_hold(struct fc_exch *ep) 259static inline void fc_exch_hold(struct fc_exch *ep)
221{ 260{
222 atomic_inc(&ep->ex_refcnt); 261 atomic_inc(&ep->ex_refcnt);
223} 262}
224 263
225/* 264/**
226 * setup fc hdr by initializing few more FC header fields and sof/eof. 265 * fc_exch_setup_hdr() - Initialize a FC header by initializing some fields
227 * Initialized fields by this func: 266 * and determine SOF and EOF.
228 * - fh_ox_id, fh_rx_id, fh_seq_id, fh_seq_cnt 267 * @ep: The exchange to that will use the header
229 * - sof and eof 268 * @fp: The frame whose header is to be modified
269 * @f_ctl: F_CTL bits that will be used for the frame header
270 *
271 * The fields initialized by this routine are: fh_ox_id, fh_rx_id,
272 * fh_seq_id, fh_seq_cnt and the SOF and EOF.
230 */ 273 */
231static void fc_exch_setup_hdr(struct fc_exch *ep, struct fc_frame *fp, 274static void fc_exch_setup_hdr(struct fc_exch *ep, struct fc_frame *fp,
232 u32 f_ctl) 275 u32 f_ctl)
@@ -243,7 +286,7 @@ static void fc_exch_setup_hdr(struct fc_exch *ep, struct fc_frame *fp,
243 if (fc_sof_needs_ack(ep->class)) 286 if (fc_sof_needs_ack(ep->class))
244 fr_eof(fp) = FC_EOF_N; 287 fr_eof(fp) = FC_EOF_N;
245 /* 288 /*
246 * Form f_ctl. 289 * From F_CTL.
247 * The number of fill bytes to make the length a 4-byte 290 * The number of fill bytes to make the length a 4-byte
248 * multiple is the low order 2-bits of the f_ctl. 291 * multiple is the low order 2-bits of the f_ctl.
249 * The fill itself will have been cleared by the frame 292 * The fill itself will have been cleared by the frame
@@ -273,10 +316,12 @@ static void fc_exch_setup_hdr(struct fc_exch *ep, struct fc_frame *fp,
273 fh->fh_seq_cnt = htons(ep->seq.cnt); 316 fh->fh_seq_cnt = htons(ep->seq.cnt);
274} 317}
275 318
276 319/**
277/* 320 * fc_exch_release() - Decrement an exchange's reference count
278 * Release a reference to an exchange. 321 * @ep: Exchange to be released
279 * If the refcnt goes to zero and the exchange is complete, it is freed. 322 *
323 * If the reference count reaches zero and the exchange is complete,
324 * it is freed.
280 */ 325 */
281static void fc_exch_release(struct fc_exch *ep) 326static void fc_exch_release(struct fc_exch *ep)
282{ 327{
@@ -291,6 +336,10 @@ static void fc_exch_release(struct fc_exch *ep)
291 } 336 }
292} 337}
293 338
339/**
340 * fc_exch_done_locked() - Complete an exchange with the exchange lock held
341 * @ep: The exchange that is complete
342 */
294static int fc_exch_done_locked(struct fc_exch *ep) 343static int fc_exch_done_locked(struct fc_exch *ep)
295{ 344{
296 int rc = 1; 345 int rc = 1;
@@ -315,6 +364,15 @@ static int fc_exch_done_locked(struct fc_exch *ep)
315 return rc; 364 return rc;
316} 365}
317 366
367/**
368 * fc_exch_ptr_get() - Return an exchange from an exchange pool
369 * @pool: Exchange Pool to get an exchange from
370 * @index: Index of the exchange within the pool
371 *
372 * Use the index to get an exchange from within an exchange pool. exches
373 * will point to an array of exchange pointers. The index will select
374 * the exchange within the array.
375 */
318static inline struct fc_exch *fc_exch_ptr_get(struct fc_exch_pool *pool, 376static inline struct fc_exch *fc_exch_ptr_get(struct fc_exch_pool *pool,
319 u16 index) 377 u16 index)
320{ 378{
@@ -322,12 +380,22 @@ static inline struct fc_exch *fc_exch_ptr_get(struct fc_exch_pool *pool,
322 return exches[index]; 380 return exches[index];
323} 381}
324 382
383/**
384 * fc_exch_ptr_set() - Assign an exchange to a slot in an exchange pool
385 * @pool: The pool to assign the exchange to
386 * @index: The index in the pool where the exchange will be assigned
387 * @ep: The exchange to assign to the pool
388 */
325static inline void fc_exch_ptr_set(struct fc_exch_pool *pool, u16 index, 389static inline void fc_exch_ptr_set(struct fc_exch_pool *pool, u16 index,
326 struct fc_exch *ep) 390 struct fc_exch *ep)
327{ 391{
328 ((struct fc_exch **)(pool + 1))[index] = ep; 392 ((struct fc_exch **)(pool + 1))[index] = ep;
329} 393}
330 394
395/**
396 * fc_exch_delete() - Delete an exchange
397 * @ep: The exchange to be deleted
398 */
331static void fc_exch_delete(struct fc_exch *ep) 399static void fc_exch_delete(struct fc_exch *ep)
332{ 400{
333 struct fc_exch_pool *pool; 401 struct fc_exch_pool *pool;
@@ -343,8 +411,14 @@ static void fc_exch_delete(struct fc_exch *ep)
343 fc_exch_release(ep); /* drop hold for exch in mp */ 411 fc_exch_release(ep); /* drop hold for exch in mp */
344} 412}
345 413
346/* 414/**
347 * Internal version of fc_exch_timer_set - used with lock held. 415 * fc_exch_timer_set_locked() - Start a timer for an exchange w/ the
416 * the exchange lock held
417 * @ep: The exchange whose timer will start
418 * @timer_msec: The timeout period
419 *
420 * Used for upper level protocols to time out the exchange.
421 * The timer is cancelled when it fires or when the exchange completes.
348 */ 422 */
349static inline void fc_exch_timer_set_locked(struct fc_exch *ep, 423static inline void fc_exch_timer_set_locked(struct fc_exch *ep,
350 unsigned int timer_msec) 424 unsigned int timer_msec)
@@ -354,17 +428,15 @@ static inline void fc_exch_timer_set_locked(struct fc_exch *ep,
354 428
355 FC_EXCH_DBG(ep, "Exchange timer armed\n"); 429 FC_EXCH_DBG(ep, "Exchange timer armed\n");
356 430
357 if (schedule_delayed_work(&ep->timeout_work, 431 if (queue_delayed_work(fc_exch_workqueue, &ep->timeout_work,
358 msecs_to_jiffies(timer_msec))) 432 msecs_to_jiffies(timer_msec)))
359 fc_exch_hold(ep); /* hold for timer */ 433 fc_exch_hold(ep); /* hold for timer */
360} 434}
361 435
362/* 436/**
363 * Set timer for an exchange. 437 * fc_exch_timer_set() - Lock the exchange and set the timer
364 * The time is a minimum delay in milliseconds until the timer fires. 438 * @ep: The exchange whose timer will start
365 * Used for upper level protocols to time out the exchange. 439 * @timer_msec: The timeout period
366 * The timer is cancelled when it fires or when the exchange completes.
367 * Returns non-zero if a timer couldn't be allocated.
368 */ 440 */
369static void fc_exch_timer_set(struct fc_exch *ep, unsigned int timer_msec) 441static void fc_exch_timer_set(struct fc_exch *ep, unsigned int timer_msec)
370{ 442{
@@ -373,7 +445,115 @@ static void fc_exch_timer_set(struct fc_exch *ep, unsigned int timer_msec)
373 spin_unlock_bh(&ep->ex_lock); 445 spin_unlock_bh(&ep->ex_lock);
374} 446}
375 447
376int fc_seq_exch_abort(const struct fc_seq *req_sp, unsigned int timer_msec) 448/**
449 * fc_seq_send() - Send a frame using existing sequence/exchange pair
450 * @lport: The local port that the exchange will be sent on
451 * @sp: The sequence to be sent
452 * @fp: The frame to be sent on the exchange
453 */
454static int fc_seq_send(struct fc_lport *lport, struct fc_seq *sp,
455 struct fc_frame *fp)
456{
457 struct fc_exch *ep;
458 struct fc_frame_header *fh = fc_frame_header_get(fp);
459 int error;
460 u32 f_ctl;
461
462 ep = fc_seq_exch(sp);
463 WARN_ON((ep->esb_stat & ESB_ST_SEQ_INIT) != ESB_ST_SEQ_INIT);
464
465 f_ctl = ntoh24(fh->fh_f_ctl);
466 fc_exch_setup_hdr(ep, fp, f_ctl);
467
468 /*
469 * update sequence count if this frame is carrying
470 * multiple FC frames when sequence offload is enabled
471 * by LLD.
472 */
473 if (fr_max_payload(fp))
474 sp->cnt += DIV_ROUND_UP((fr_len(fp) - sizeof(*fh)),
475 fr_max_payload(fp));
476 else
477 sp->cnt++;
478
479 /*
480 * Send the frame.
481 */
482 error = lport->tt.frame_send(lport, fp);
483
484 /*
485 * Update the exchange and sequence flags,
486 * assuming all frames for the sequence have been sent.
487 * We can only be called to send once for each sequence.
488 */
489 spin_lock_bh(&ep->ex_lock);
490 ep->f_ctl = f_ctl & ~FC_FC_FIRST_SEQ; /* not first seq */
491 if (f_ctl & (FC_FC_END_SEQ | FC_FC_SEQ_INIT))
492 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
493 spin_unlock_bh(&ep->ex_lock);
494 return error;
495}
496
497/**
498 * fc_seq_alloc() - Allocate a sequence for a given exchange
499 * @ep: The exchange to allocate a new sequence for
500 * @seq_id: The sequence ID to be used
501 *
502 * We don't support multiple originated sequences on the same exchange.
503 * By implication, any previously originated sequence on this exchange
504 * is complete, and we reallocate the same sequence.
505 */
506static struct fc_seq *fc_seq_alloc(struct fc_exch *ep, u8 seq_id)
507{
508 struct fc_seq *sp;
509
510 sp = &ep->seq;
511 sp->ssb_stat = 0;
512 sp->cnt = 0;
513 sp->id = seq_id;
514 return sp;
515}
516
517/**
518 * fc_seq_start_next_locked() - Allocate a new sequence on the same
519 * exchange as the supplied sequence
520 * @sp: The sequence/exchange to get a new sequence for
521 */
522static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp)
523{
524 struct fc_exch *ep = fc_seq_exch(sp);
525
526 sp = fc_seq_alloc(ep, ep->seq_id++);
527 FC_EXCH_DBG(ep, "f_ctl %6x seq %2x\n",
528 ep->f_ctl, sp->id);
529 return sp;
530}
531
532/**
533 * fc_seq_start_next() - Lock the exchange and get a new sequence
534 * for a given sequence/exchange pair
535 * @sp: The sequence/exchange to get a new exchange for
536 */
537static struct fc_seq *fc_seq_start_next(struct fc_seq *sp)
538{
539 struct fc_exch *ep = fc_seq_exch(sp);
540
541 spin_lock_bh(&ep->ex_lock);
542 sp = fc_seq_start_next_locked(sp);
543 spin_unlock_bh(&ep->ex_lock);
544
545 return sp;
546}
547
548/**
549 * fc_seq_exch_abort() - Abort an exchange and sequence
550 * @req_sp: The sequence to be aborted
551 * @timer_msec: The period of time to wait before aborting
552 *
553 * Generally called because of a timeout or an abort from the upper layer.
554 */
555static int fc_seq_exch_abort(const struct fc_seq *req_sp,
556 unsigned int timer_msec)
377{ 557{
378 struct fc_seq *sp; 558 struct fc_seq *sp;
379 struct fc_exch *ep; 559 struct fc_exch *ep;
@@ -422,11 +602,10 @@ int fc_seq_exch_abort(const struct fc_seq *req_sp, unsigned int timer_msec)
422 error = -ENOBUFS; 602 error = -ENOBUFS;
423 return error; 603 return error;
424} 604}
425EXPORT_SYMBOL(fc_seq_exch_abort);
426 605
427/* 606/**
428 * Exchange timeout - handle exchange timer expiration. 607 * fc_exch_timeout() - Handle exchange timer expiration
429 * The timer will have been cancelled before this is called. 608 * @work: The work_struct identifying the exchange that timed out
430 */ 609 */
431static void fc_exch_timeout(struct work_struct *work) 610static void fc_exch_timeout(struct work_struct *work)
432{ 611{
@@ -474,28 +653,10 @@ done:
474 fc_exch_release(ep); 653 fc_exch_release(ep);
475} 654}
476 655
477/*
478 * Allocate a sequence.
479 *
480 * We don't support multiple originated sequences on the same exchange.
481 * By implication, any previously originated sequence on this exchange
482 * is complete, and we reallocate the same sequence.
483 */
484static struct fc_seq *fc_seq_alloc(struct fc_exch *ep, u8 seq_id)
485{
486 struct fc_seq *sp;
487
488 sp = &ep->seq;
489 sp->ssb_stat = 0;
490 sp->cnt = 0;
491 sp->id = seq_id;
492 return sp;
493}
494
495/** 656/**
496 * fc_exch_em_alloc() - allocate an exchange from a specified EM. 657 * fc_exch_em_alloc() - Allocate an exchange from a specified EM.
497 * @lport: ptr to the local port 658 * @lport: The local port that the exchange is for
498 * @mp: ptr to the exchange manager 659 * @mp: The exchange manager that will allocate the exchange
499 * 660 *
500 * Returns pointer to allocated fc_exch with exch lock held. 661 * Returns pointer to allocated fc_exch with exch lock held.
501 */ 662 */
@@ -563,16 +724,18 @@ err:
563} 724}
564 725
565/** 726/**
566 * fc_exch_alloc() - allocate an exchange. 727 * fc_exch_alloc() - Allocate an exchange from an EM on a
567 * @lport: ptr to the local port 728 * local port's list of EMs.
568 * @fp: ptr to the FC frame 729 * @lport: The local port that will own the exchange
730 * @fp: The FC frame that the exchange will be for
569 * 731 *
570 * This function walks the list of the exchange manager(EM) 732 * This function walks the list of exchange manager(EM)
571 * anchors to select a EM for new exchange allocation. The 733 * anchors to select an EM for a new exchange allocation. The
572 * EM is selected having either a NULL match function pointer 734 * EM is selected when a NULL match function pointer is encountered
573 * or call to match function returning true. 735 * or when a call to a match function returns true.
574 */ 736 */
575struct fc_exch *fc_exch_alloc(struct fc_lport *lport, struct fc_frame *fp) 737static struct fc_exch *fc_exch_alloc(struct fc_lport *lport,
738 struct fc_frame *fp)
576{ 739{
577 struct fc_exch_mgr_anchor *ema; 740 struct fc_exch_mgr_anchor *ema;
578 struct fc_exch *ep; 741 struct fc_exch *ep;
@@ -586,10 +749,11 @@ struct fc_exch *fc_exch_alloc(struct fc_lport *lport, struct fc_frame *fp)
586 } 749 }
587 return NULL; 750 return NULL;
588} 751}
589EXPORT_SYMBOL(fc_exch_alloc);
590 752
591/* 753/**
592 * Lookup and hold an exchange. 754 * fc_exch_find() - Lookup and hold an exchange
755 * @mp: The exchange manager to lookup the exchange from
756 * @xid: The XID of the exchange to look up
593 */ 757 */
594static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid) 758static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid)
595{ 759{
@@ -609,7 +773,13 @@ static struct fc_exch *fc_exch_find(struct fc_exch_mgr *mp, u16 xid)
609 return ep; 773 return ep;
610} 774}
611 775
612void fc_exch_done(struct fc_seq *sp) 776
777/**
778 * fc_exch_done() - Indicate that an exchange/sequence tuple is complete and
779 * the memory allocated for the related objects may be freed.
780 * @sp: The sequence that has completed
781 */
782static void fc_exch_done(struct fc_seq *sp)
613{ 783{
614 struct fc_exch *ep = fc_seq_exch(sp); 784 struct fc_exch *ep = fc_seq_exch(sp);
615 int rc; 785 int rc;
@@ -620,10 +790,13 @@ void fc_exch_done(struct fc_seq *sp)
620 if (!rc) 790 if (!rc)
621 fc_exch_delete(ep); 791 fc_exch_delete(ep);
622} 792}
623EXPORT_SYMBOL(fc_exch_done);
624 793
625/* 794/**
626 * Allocate a new exchange as responder. 795 * fc_exch_resp() - Allocate a new exchange for a response frame
796 * @lport: The local port that the exchange was for
797 * @mp: The exchange manager to allocate the exchange from
798 * @fp: The response frame
799 *
627 * Sets the responder ID in the frame header. 800 * Sets the responder ID in the frame header.
628 */ 801 */
629static struct fc_exch *fc_exch_resp(struct fc_lport *lport, 802static struct fc_exch *fc_exch_resp(struct fc_lport *lport,
@@ -664,8 +837,13 @@ static struct fc_exch *fc_exch_resp(struct fc_lport *lport,
664 return ep; 837 return ep;
665} 838}
666 839
667/* 840/**
668 * Find a sequence for receive where the other end is originating the sequence. 841 * fc_seq_lookup_recip() - Find a sequence where the other end
842 * originated the sequence
843 * @lport: The local port that the frame was sent to
844 * @mp: The Exchange Manager to lookup the exchange from
845 * @fp: The frame associated with the sequence we're looking for
846 *
669 * If fc_pf_rjt_reason is FC_RJT_NONE then this function will have a hold 847 * If fc_pf_rjt_reason is FC_RJT_NONE then this function will have a hold
670 * on the ep that should be released by the caller. 848 * on the ep that should be released by the caller.
671 */ 849 */
@@ -771,10 +949,12 @@ rel:
771 return reject; 949 return reject;
772} 950}
773 951
774/* 952/**
775 * Find the sequence for a frame being received. 953 * fc_seq_lookup_orig() - Find a sequence where this end
776 * We originated the sequence, so it should be found. 954 * originated the sequence
777 * We may or may not have originated the exchange. 955 * @mp: The Exchange Manager to lookup the exchange from
956 * @fp: The frame associated with the sequence we're looking for
957 *
778 * Does not hold the sequence for the caller. 958 * Does not hold the sequence for the caller.
779 */ 959 */
780static struct fc_seq *fc_seq_lookup_orig(struct fc_exch_mgr *mp, 960static struct fc_seq *fc_seq_lookup_orig(struct fc_exch_mgr *mp,
@@ -806,8 +986,12 @@ static struct fc_seq *fc_seq_lookup_orig(struct fc_exch_mgr *mp,
806 return sp; 986 return sp;
807} 987}
808 988
809/* 989/**
810 * Set addresses for an exchange. 990 * fc_exch_set_addr() - Set the source and destination IDs for an exchange
991 * @ep: The exchange to set the addresses for
992 * @orig_id: The originator's ID
993 * @resp_id: The responder's ID
994 *
811 * Note this must be done before the first sequence of the exchange is sent. 995 * Note this must be done before the first sequence of the exchange is sent.
812 */ 996 */
813static void fc_exch_set_addr(struct fc_exch *ep, 997static void fc_exch_set_addr(struct fc_exch *ep,
@@ -823,76 +1007,15 @@ static void fc_exch_set_addr(struct fc_exch *ep,
823 } 1007 }
824} 1008}
825 1009
826static struct fc_seq *fc_seq_start_next_locked(struct fc_seq *sp) 1010/**
827{ 1011 * fc_seq_els_rsp_send() - Send an ELS response using infomation from
828 struct fc_exch *ep = fc_seq_exch(sp); 1012 * the existing sequence/exchange.
829 1013 * @sp: The sequence/exchange to get information from
830 sp = fc_seq_alloc(ep, ep->seq_id++); 1014 * @els_cmd: The ELS command to be sent
831 FC_EXCH_DBG(ep, "f_ctl %6x seq %2x\n", 1015 * @els_data: The ELS data to be sent
832 ep->f_ctl, sp->id);
833 return sp;
834}
835/*
836 * Allocate a new sequence on the same exchange as the supplied sequence.
837 * This will never return NULL.
838 */ 1016 */
839struct fc_seq *fc_seq_start_next(struct fc_seq *sp) 1017static void fc_seq_els_rsp_send(struct fc_seq *sp, enum fc_els_cmd els_cmd,
840{ 1018 struct fc_seq_els_data *els_data)
841 struct fc_exch *ep = fc_seq_exch(sp);
842
843 spin_lock_bh(&ep->ex_lock);
844 sp = fc_seq_start_next_locked(sp);
845 spin_unlock_bh(&ep->ex_lock);
846
847 return sp;
848}
849EXPORT_SYMBOL(fc_seq_start_next);
850
851int fc_seq_send(struct fc_lport *lp, struct fc_seq *sp, struct fc_frame *fp)
852{
853 struct fc_exch *ep;
854 struct fc_frame_header *fh = fc_frame_header_get(fp);
855 int error;
856 u32 f_ctl;
857
858 ep = fc_seq_exch(sp);
859 WARN_ON((ep->esb_stat & ESB_ST_SEQ_INIT) != ESB_ST_SEQ_INIT);
860
861 f_ctl = ntoh24(fh->fh_f_ctl);
862 fc_exch_setup_hdr(ep, fp, f_ctl);
863
864 /*
865 * update sequence count if this frame is carrying
866 * multiple FC frames when sequence offload is enabled
867 * by LLD.
868 */
869 if (fr_max_payload(fp))
870 sp->cnt += DIV_ROUND_UP((fr_len(fp) - sizeof(*fh)),
871 fr_max_payload(fp));
872 else
873 sp->cnt++;
874
875 /*
876 * Send the frame.
877 */
878 error = lp->tt.frame_send(lp, fp);
879
880 /*
881 * Update the exchange and sequence flags,
882 * assuming all frames for the sequence have been sent.
883 * We can only be called to send once for each sequence.
884 */
885 spin_lock_bh(&ep->ex_lock);
886 ep->f_ctl = f_ctl & ~FC_FC_FIRST_SEQ; /* not first seq */
887 if (f_ctl & (FC_FC_END_SEQ | FC_FC_SEQ_INIT))
888 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
889 spin_unlock_bh(&ep->ex_lock);
890 return error;
891}
892EXPORT_SYMBOL(fc_seq_send);
893
894void fc_seq_els_rsp_send(struct fc_seq *sp, enum fc_els_cmd els_cmd,
895 struct fc_seq_els_data *els_data)
896{ 1019{
897 switch (els_cmd) { 1020 switch (els_cmd) {
898 case ELS_LS_RJT: 1021 case ELS_LS_RJT:
@@ -911,10 +1034,13 @@ void fc_seq_els_rsp_send(struct fc_seq *sp, enum fc_els_cmd els_cmd,
911 FC_EXCH_DBG(fc_seq_exch(sp), "Invalid ELS CMD:%x\n", els_cmd); 1034 FC_EXCH_DBG(fc_seq_exch(sp), "Invalid ELS CMD:%x\n", els_cmd);
912 } 1035 }
913} 1036}
914EXPORT_SYMBOL(fc_seq_els_rsp_send);
915 1037
916/* 1038/**
917 * Send a sequence, which is also the last sequence in the exchange. 1039 * fc_seq_send_last() - Send a sequence that is the last in the exchange
1040 * @sp: The sequence that is to be sent
1041 * @fp: The frame that will be sent on the sequence
1042 * @rctl: The R_CTL information to be sent
1043 * @fh_type: The frame header type
918 */ 1044 */
919static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp, 1045static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp,
920 enum fc_rctl rctl, enum fc_fh_type fh_type) 1046 enum fc_rctl rctl, enum fc_fh_type fh_type)
@@ -928,9 +1054,12 @@ static void fc_seq_send_last(struct fc_seq *sp, struct fc_frame *fp,
928 fc_seq_send(ep->lp, sp, fp); 1054 fc_seq_send(ep->lp, sp, fp);
929} 1055}
930 1056
931/* 1057/**
1058 * fc_seq_send_ack() - Send an acknowledgement that we've received a frame
1059 * @sp: The sequence to send the ACK on
1060 * @rx_fp: The received frame that is being acknoledged
1061 *
932 * Send ACK_1 (or equiv.) indicating we received something. 1062 * Send ACK_1 (or equiv.) indicating we received something.
933 * The frame we're acking is supplied.
934 */ 1063 */
935static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp) 1064static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp)
936{ 1065{
@@ -938,14 +1067,14 @@ static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp)
938 struct fc_frame_header *rx_fh; 1067 struct fc_frame_header *rx_fh;
939 struct fc_frame_header *fh; 1068 struct fc_frame_header *fh;
940 struct fc_exch *ep = fc_seq_exch(sp); 1069 struct fc_exch *ep = fc_seq_exch(sp);
941 struct fc_lport *lp = ep->lp; 1070 struct fc_lport *lport = ep->lp;
942 unsigned int f_ctl; 1071 unsigned int f_ctl;
943 1072
944 /* 1073 /*
945 * Don't send ACKs for class 3. 1074 * Don't send ACKs for class 3.
946 */ 1075 */
947 if (fc_sof_needs_ack(fr_sof(rx_fp))) { 1076 if (fc_sof_needs_ack(fr_sof(rx_fp))) {
948 fp = fc_frame_alloc(lp, 0); 1077 fp = fc_frame_alloc(lport, 0);
949 if (!fp) 1078 if (!fp)
950 return; 1079 return;
951 1080
@@ -980,12 +1109,16 @@ static void fc_seq_send_ack(struct fc_seq *sp, const struct fc_frame *rx_fp)
980 else 1109 else
981 fr_eof(fp) = FC_EOF_N; 1110 fr_eof(fp) = FC_EOF_N;
982 1111
983 (void) lp->tt.frame_send(lp, fp); 1112 lport->tt.frame_send(lport, fp);
984 } 1113 }
985} 1114}
986 1115
987/* 1116/**
988 * Send BLS Reject. 1117 * fc_exch_send_ba_rjt() - Send BLS Reject
1118 * @rx_fp: The frame being rejected
1119 * @reason: The reason the frame is being rejected
1120 * @explan: The explaination for the rejection
1121 *
989 * This is for rejecting BA_ABTS only. 1122 * This is for rejecting BA_ABTS only.
990 */ 1123 */
991static void fc_exch_send_ba_rjt(struct fc_frame *rx_fp, 1124static void fc_exch_send_ba_rjt(struct fc_frame *rx_fp,
@@ -996,11 +1129,11 @@ static void fc_exch_send_ba_rjt(struct fc_frame *rx_fp,
996 struct fc_frame_header *rx_fh; 1129 struct fc_frame_header *rx_fh;
997 struct fc_frame_header *fh; 1130 struct fc_frame_header *fh;
998 struct fc_ba_rjt *rp; 1131 struct fc_ba_rjt *rp;
999 struct fc_lport *lp; 1132 struct fc_lport *lport;
1000 unsigned int f_ctl; 1133 unsigned int f_ctl;
1001 1134
1002 lp = fr_dev(rx_fp); 1135 lport = fr_dev(rx_fp);
1003 fp = fc_frame_alloc(lp, sizeof(*rp)); 1136 fp = fc_frame_alloc(lport, sizeof(*rp));
1004 if (!fp) 1137 if (!fp)
1005 return; 1138 return;
1006 fh = fc_frame_header_get(fp); 1139 fh = fc_frame_header_get(fp);
@@ -1045,13 +1178,17 @@ static void fc_exch_send_ba_rjt(struct fc_frame *rx_fp,
1045 if (fc_sof_needs_ack(fr_sof(fp))) 1178 if (fc_sof_needs_ack(fr_sof(fp)))
1046 fr_eof(fp) = FC_EOF_N; 1179 fr_eof(fp) = FC_EOF_N;
1047 1180
1048 (void) lp->tt.frame_send(lp, fp); 1181 lport->tt.frame_send(lport, fp);
1049} 1182}
1050 1183
1051/* 1184/**
1052 * Handle an incoming ABTS. This would be for target mode usually, 1185 * fc_exch_recv_abts() - Handle an incoming ABTS
1053 * but could be due to lost FCP transfer ready, confirm or RRQ. 1186 * @ep: The exchange the abort was on
1054 * We always handle this as an exchange abort, ignoring the parameter. 1187 * @rx_fp: The ABTS frame
1188 *
1189 * This would be for target mode usually, but could be due to lost
1190 * FCP transfer ready, confirm or RRQ. We always handle this as an
1191 * exchange abort, ignoring the parameter.
1055 */ 1192 */
1056static void fc_exch_recv_abts(struct fc_exch *ep, struct fc_frame *rx_fp) 1193static void fc_exch_recv_abts(struct fc_exch *ep, struct fc_frame *rx_fp)
1057{ 1194{
@@ -1100,10 +1237,14 @@ free:
1100 fc_frame_free(rx_fp); 1237 fc_frame_free(rx_fp);
1101} 1238}
1102 1239
1103/* 1240/**
1104 * Handle receive where the other end is originating the sequence. 1241 * fc_exch_recv_req() - Handler for an incoming request where is other
1242 * end is originating the sequence
1243 * @lport: The local port that received the request
1244 * @mp: The EM that the exchange is on
1245 * @fp: The request frame
1105 */ 1246 */
1106static void fc_exch_recv_req(struct fc_lport *lp, struct fc_exch_mgr *mp, 1247static void fc_exch_recv_req(struct fc_lport *lport, struct fc_exch_mgr *mp,
1107 struct fc_frame *fp) 1248 struct fc_frame *fp)
1108{ 1249{
1109 struct fc_frame_header *fh = fc_frame_header_get(fp); 1250 struct fc_frame_header *fh = fc_frame_header_get(fp);
@@ -1114,8 +1255,17 @@ static void fc_exch_recv_req(struct fc_lport *lp, struct fc_exch_mgr *mp,
1114 u32 f_ctl; 1255 u32 f_ctl;
1115 enum fc_pf_rjt_reason reject; 1256 enum fc_pf_rjt_reason reject;
1116 1257
1258 /* We can have the wrong fc_lport at this point with NPIV, which is a
1259 * problem now that we know a new exchange needs to be allocated
1260 */
1261 lport = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
1262 if (!lport) {
1263 fc_frame_free(fp);
1264 return;
1265 }
1266
1117 fr_seq(fp) = NULL; 1267 fr_seq(fp) = NULL;
1118 reject = fc_seq_lookup_recip(lp, mp, fp); 1268 reject = fc_seq_lookup_recip(lport, mp, fp);
1119 if (reject == FC_RJT_NONE) { 1269 if (reject == FC_RJT_NONE) {
1120 sp = fr_seq(fp); /* sequence will be held */ 1270 sp = fr_seq(fp); /* sequence will be held */
1121 ep = fc_seq_exch(sp); 1271 ep = fc_seq_exch(sp);
@@ -1138,17 +1288,21 @@ static void fc_exch_recv_req(struct fc_lport *lp, struct fc_exch_mgr *mp,
1138 if (ep->resp) 1288 if (ep->resp)
1139 ep->resp(sp, fp, ep->arg); 1289 ep->resp(sp, fp, ep->arg);
1140 else 1290 else
1141 lp->tt.lport_recv(lp, sp, fp); 1291 lport->tt.lport_recv(lport, sp, fp);
1142 fc_exch_release(ep); /* release from lookup */ 1292 fc_exch_release(ep); /* release from lookup */
1143 } else { 1293 } else {
1144 FC_LPORT_DBG(lp, "exch/seq lookup failed: reject %x\n", reject); 1294 FC_LPORT_DBG(lport, "exch/seq lookup failed: reject %x\n",
1295 reject);
1145 fc_frame_free(fp); 1296 fc_frame_free(fp);
1146 } 1297 }
1147} 1298}
1148 1299
1149/* 1300/**
1150 * Handle receive where the other end is originating the sequence in 1301 * fc_exch_recv_seq_resp() - Handler for an incoming response where the other
1151 * response to our exchange. 1302 * end is the originator of the sequence that is a
1303 * response to our initial exchange
1304 * @mp: The EM that the exchange is on
1305 * @fp: The response frame
1152 */ 1306 */
1153static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp) 1307static void fc_exch_recv_seq_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
1154{ 1308{
@@ -1239,8 +1393,11 @@ out:
1239 fc_frame_free(fp); 1393 fc_frame_free(fp);
1240} 1394}
1241 1395
1242/* 1396/**
1243 * Handle receive for a sequence where other end is responding to our sequence. 1397 * fc_exch_recv_resp() - Handler for a sequence where other end is
1398 * responding to our sequence
1399 * @mp: The EM that the exchange is on
1400 * @fp: The response frame
1244 */ 1401 */
1245static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp) 1402static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
1246{ 1403{
@@ -1256,9 +1413,13 @@ static void fc_exch_recv_resp(struct fc_exch_mgr *mp, struct fc_frame *fp)
1256 fc_frame_free(fp); 1413 fc_frame_free(fp);
1257} 1414}
1258 1415
1259/* 1416/**
1260 * Handle the response to an ABTS for exchange or sequence. 1417 * fc_exch_abts_resp() - Handler for a response to an ABT
1261 * This can be BA_ACC or BA_RJT. 1418 * @ep: The exchange that the frame is on
1419 * @fp: The response frame
1420 *
1421 * This response would be to an ABTS cancelling an exchange or sequence.
1422 * The response can be either BA_ACC or BA_RJT
1262 */ 1423 */
1263static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp) 1424static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp)
1264{ 1425{
@@ -1333,9 +1494,12 @@ static void fc_exch_abts_resp(struct fc_exch *ep, struct fc_frame *fp)
1333 1494
1334} 1495}
1335 1496
1336/* 1497/**
1337 * Receive BLS sequence. 1498 * fc_exch_recv_bls() - Handler for a BLS sequence
1338 * This is always a sequence initiated by the remote side. 1499 * @mp: The EM that the exchange is on
1500 * @fp: The request frame
1501 *
1502 * The BLS frame is always a sequence initiated by the remote side.
1339 * We may be either the originator or recipient of the exchange. 1503 * We may be either the originator or recipient of the exchange.
1340 */ 1504 */
1341static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp) 1505static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp)
@@ -1392,8 +1556,10 @@ static void fc_exch_recv_bls(struct fc_exch_mgr *mp, struct fc_frame *fp)
1392 fc_exch_release(ep); /* release hold taken by fc_exch_find */ 1556 fc_exch_release(ep); /* release hold taken by fc_exch_find */
1393} 1557}
1394 1558
1395/* 1559/**
1396 * Accept sequence with LS_ACC. 1560 * fc_seq_ls_acc() - Accept sequence with LS_ACC
1561 * @req_sp: The request sequence
1562 *
1397 * If this fails due to allocation or transmit congestion, assume the 1563 * If this fails due to allocation or transmit congestion, assume the
1398 * originator will repeat the sequence. 1564 * originator will repeat the sequence.
1399 */ 1565 */
@@ -1413,8 +1579,12 @@ static void fc_seq_ls_acc(struct fc_seq *req_sp)
1413 } 1579 }
1414} 1580}
1415 1581
1416/* 1582/**
1417 * Reject sequence with ELS LS_RJT. 1583 * fc_seq_ls_rjt() - Reject a sequence with ELS LS_RJT
1584 * @req_sp: The request sequence
1585 * @reason: The reason the sequence is being rejected
1586 * @explan: The explaination for the rejection
1587 *
1418 * If this fails due to allocation or transmit congestion, assume the 1588 * If this fails due to allocation or transmit congestion, assume the
1419 * originator will repeat the sequence. 1589 * originator will repeat the sequence.
1420 */ 1590 */
@@ -1437,6 +1607,10 @@ static void fc_seq_ls_rjt(struct fc_seq *req_sp, enum fc_els_rjt_reason reason,
1437 } 1607 }
1438} 1608}
1439 1609
1610/**
1611 * fc_exch_reset() - Reset an exchange
1612 * @ep: The exchange to be reset
1613 */
1440static void fc_exch_reset(struct fc_exch *ep) 1614static void fc_exch_reset(struct fc_exch *ep)
1441{ 1615{
1442 struct fc_seq *sp; 1616 struct fc_seq *sp;
@@ -1446,12 +1620,6 @@ static void fc_exch_reset(struct fc_exch *ep)
1446 1620
1447 spin_lock_bh(&ep->ex_lock); 1621 spin_lock_bh(&ep->ex_lock);
1448 ep->state |= FC_EX_RST_CLEANUP; 1622 ep->state |= FC_EX_RST_CLEANUP;
1449 /*
1450 * we really want to call del_timer_sync, but cannot due
1451 * to the lport calling with the lport lock held (some resp
1452 * functions can also grab the lport lock which could cause
1453 * a deadlock).
1454 */
1455 if (cancel_delayed_work(&ep->timeout_work)) 1623 if (cancel_delayed_work(&ep->timeout_work))
1456 atomic_dec(&ep->ex_refcnt); /* drop hold for timer */ 1624 atomic_dec(&ep->ex_refcnt); /* drop hold for timer */
1457 resp = ep->resp; 1625 resp = ep->resp;
@@ -1471,16 +1639,16 @@ static void fc_exch_reset(struct fc_exch *ep)
1471} 1639}
1472 1640
1473/** 1641/**
1474 * fc_exch_pool_reset() - Resets an per cpu exches pool. 1642 * fc_exch_pool_reset() - Reset a per cpu exchange pool
1475 * @lport: ptr to the local port 1643 * @lport: The local port that the exchange pool is on
1476 * @pool: ptr to the per cpu exches pool 1644 * @pool: The exchange pool to be reset
1477 * @sid: source FC ID 1645 * @sid: The source ID
1478 * @did: destination FC ID 1646 * @did: The destination ID
1479 * 1647 *
1480 * Resets an per cpu exches pool, releasing its all sequences 1648 * Resets a per cpu exches pool, releasing all of its sequences
1481 * and exchanges. If sid is non-zero, then reset only exchanges 1649 * and exchanges. If sid is non-zero then reset only exchanges
1482 * we sourced from that FID. If did is non-zero, reset only 1650 * we sourced from the local port's FID. If did is non-zero then
1483 * exchanges destined to that FID. 1651 * only reset exchanges destined for the local port's FID.
1484 */ 1652 */
1485static void fc_exch_pool_reset(struct fc_lport *lport, 1653static void fc_exch_pool_reset(struct fc_lport *lport,
1486 struct fc_exch_pool *pool, 1654 struct fc_exch_pool *pool,
@@ -1514,15 +1682,15 @@ restart:
1514} 1682}
1515 1683
1516/** 1684/**
1517 * fc_exch_mgr_reset() - Resets all EMs of a lport 1685 * fc_exch_mgr_reset() - Reset all EMs of a local port
1518 * @lport: ptr to the local port 1686 * @lport: The local port whose EMs are to be reset
1519 * @sid: source FC ID 1687 * @sid: The source ID
1520 * @did: destination FC ID 1688 * @did: The destination ID
1521 * 1689 *
1522 * Reset all EMs of a lport, releasing its all sequences and 1690 * Reset all EMs associated with a given local port. Release all
1523 * exchanges. If sid is non-zero, then reset only exchanges 1691 * sequences and exchanges. If sid is non-zero then reset only the
1524 * we sourced from that FID. If did is non-zero, reset only 1692 * exchanges sent from the local port's FID. If did is non-zero then
1525 * exchanges destined to that FID. 1693 * reset only exchanges destined for the local port's FID.
1526 */ 1694 */
1527void fc_exch_mgr_reset(struct fc_lport *lport, u32 sid, u32 did) 1695void fc_exch_mgr_reset(struct fc_lport *lport, u32 sid, u32 did)
1528{ 1696{
@@ -1538,8 +1706,11 @@ void fc_exch_mgr_reset(struct fc_lport *lport, u32 sid, u32 did)
1538} 1706}
1539EXPORT_SYMBOL(fc_exch_mgr_reset); 1707EXPORT_SYMBOL(fc_exch_mgr_reset);
1540 1708
1541/* 1709/**
1542 * Handle incoming ELS REC - Read Exchange Concise. 1710 * fc_exch_els_rec() - Handler for ELS REC (Read Exchange Concise) requests
1711 * @sp: The sequence the REC is on
1712 * @rfp: The REC frame
1713 *
1543 * Note that the requesting port may be different than the S_ID in the request. 1714 * Note that the requesting port may be different than the S_ID in the request.
1544 */ 1715 */
1545static void fc_exch_els_rec(struct fc_seq *sp, struct fc_frame *rfp) 1716static void fc_exch_els_rec(struct fc_seq *sp, struct fc_frame *rfp)
@@ -1621,10 +1792,11 @@ reject:
1621 fc_frame_free(rfp); 1792 fc_frame_free(rfp);
1622} 1793}
1623 1794
1624/* 1795/**
1625 * Handle response from RRQ. 1796 * fc_exch_rrq_resp() - Handler for RRQ responses
1626 * Not much to do here, really. 1797 * @sp: The sequence that the RRQ is on
1627 * Should report errors. 1798 * @fp: The RRQ frame
1799 * @arg: The exchange that the RRQ is on
1628 * 1800 *
1629 * TODO: fix error handler. 1801 * TODO: fix error handler.
1630 */ 1802 */
@@ -1664,21 +1836,99 @@ cleanup:
1664 fc_exch_release(aborted_ep); 1836 fc_exch_release(aborted_ep);
1665} 1837}
1666 1838
1667/* 1839
1668 * Send ELS RRQ - Reinstate Recovery Qualifier. 1840/**
1841 * fc_exch_seq_send() - Send a frame using a new exchange and sequence
1842 * @lport: The local port to send the frame on
1843 * @fp: The frame to be sent
1844 * @resp: The response handler for this request
1845 * @destructor: The destructor for the exchange
1846 * @arg: The argument to be passed to the response handler
1847 * @timer_msec: The timeout period for the exchange
1848 *
1849 * The frame pointer with some of the header's fields must be
1850 * filled before calling this routine, those fields are:
1851 *
1852 * - routing control
1853 * - FC port did
1854 * - FC port sid
1855 * - FC header type
1856 * - frame control
1857 * - parameter or relative offset
1858 */
1859static struct fc_seq *fc_exch_seq_send(struct fc_lport *lport,
1860 struct fc_frame *fp,
1861 void (*resp)(struct fc_seq *,
1862 struct fc_frame *fp,
1863 void *arg),
1864 void (*destructor)(struct fc_seq *,
1865 void *),
1866 void *arg, u32 timer_msec)
1867{
1868 struct fc_exch *ep;
1869 struct fc_seq *sp = NULL;
1870 struct fc_frame_header *fh;
1871 int rc = 1;
1872
1873 ep = fc_exch_alloc(lport, fp);
1874 if (!ep) {
1875 fc_frame_free(fp);
1876 return NULL;
1877 }
1878 ep->esb_stat |= ESB_ST_SEQ_INIT;
1879 fh = fc_frame_header_get(fp);
1880 fc_exch_set_addr(ep, ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id));
1881 ep->resp = resp;
1882 ep->destructor = destructor;
1883 ep->arg = arg;
1884 ep->r_a_tov = FC_DEF_R_A_TOV;
1885 ep->lp = lport;
1886 sp = &ep->seq;
1887
1888 ep->fh_type = fh->fh_type; /* save for possbile timeout handling */
1889 ep->f_ctl = ntoh24(fh->fh_f_ctl);
1890 fc_exch_setup_hdr(ep, fp, ep->f_ctl);
1891 sp->cnt++;
1892
1893 if (ep->xid <= lport->lro_xid && fh->fh_r_ctl == FC_RCTL_DD_UNSOL_CMD)
1894 fc_fcp_ddp_setup(fr_fsp(fp), ep->xid);
1895
1896 if (unlikely(lport->tt.frame_send(lport, fp)))
1897 goto err;
1898
1899 if (timer_msec)
1900 fc_exch_timer_set_locked(ep, timer_msec);
1901 ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not first seq */
1902
1903 if (ep->f_ctl & FC_FC_SEQ_INIT)
1904 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
1905 spin_unlock_bh(&ep->ex_lock);
1906 return sp;
1907err:
1908 rc = fc_exch_done_locked(ep);
1909 spin_unlock_bh(&ep->ex_lock);
1910 if (!rc)
1911 fc_exch_delete(ep);
1912 return NULL;
1913}
1914
1915/**
1916 * fc_exch_rrq() - Send an ELS RRQ (Reinstate Recovery Qualifier) command
1917 * @ep: The exchange to send the RRQ on
1918 *
1669 * This tells the remote port to stop blocking the use of 1919 * This tells the remote port to stop blocking the use of
1670 * the exchange and the seq_cnt range. 1920 * the exchange and the seq_cnt range.
1671 */ 1921 */
1672static void fc_exch_rrq(struct fc_exch *ep) 1922static void fc_exch_rrq(struct fc_exch *ep)
1673{ 1923{
1674 struct fc_lport *lp; 1924 struct fc_lport *lport;
1675 struct fc_els_rrq *rrq; 1925 struct fc_els_rrq *rrq;
1676 struct fc_frame *fp; 1926 struct fc_frame *fp;
1677 u32 did; 1927 u32 did;
1678 1928
1679 lp = ep->lp; 1929 lport = ep->lp;
1680 1930
1681 fp = fc_frame_alloc(lp, sizeof(*rrq)); 1931 fp = fc_frame_alloc(lport, sizeof(*rrq));
1682 if (!fp) 1932 if (!fp)
1683 goto retry; 1933 goto retry;
1684 1934
@@ -1694,10 +1944,11 @@ static void fc_exch_rrq(struct fc_exch *ep)
1694 did = ep->sid; 1944 did = ep->sid;
1695 1945
1696 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, did, 1946 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, did,
1697 fc_host_port_id(lp->host), FC_TYPE_ELS, 1947 fc_host_port_id(lport->host), FC_TYPE_ELS,
1698 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0); 1948 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1699 1949
1700 if (fc_exch_seq_send(lp, fp, fc_exch_rrq_resp, NULL, ep, lp->e_d_tov)) 1950 if (fc_exch_seq_send(lport, fp, fc_exch_rrq_resp, NULL, ep,
1951 lport->e_d_tov))
1701 return; 1952 return;
1702 1953
1703retry: 1954retry:
@@ -1714,12 +1965,14 @@ retry:
1714} 1965}
1715 1966
1716 1967
1717/* 1968/**
1718 * Handle incoming ELS RRQ - Reset Recovery Qualifier. 1969 * fc_exch_els_rrq() - Handler for ELS RRQ (Reset Recovery Qualifier) requests
1970 * @sp: The sequence that the RRQ is on
1971 * @fp: The RRQ frame
1719 */ 1972 */
1720static void fc_exch_els_rrq(struct fc_seq *sp, struct fc_frame *fp) 1973static void fc_exch_els_rrq(struct fc_seq *sp, struct fc_frame *fp)
1721{ 1974{
1722 struct fc_exch *ep; /* request or subject exchange */ 1975 struct fc_exch *ep = NULL; /* request or subject exchange */
1723 struct fc_els_rrq *rp; 1976 struct fc_els_rrq *rp;
1724 u32 sid; 1977 u32 sid;
1725 u16 xid; 1978 u16 xid;
@@ -1769,17 +2022,24 @@ static void fc_exch_els_rrq(struct fc_seq *sp, struct fc_frame *fp)
1769 * Send LS_ACC. 2022 * Send LS_ACC.
1770 */ 2023 */
1771 fc_seq_ls_acc(sp); 2024 fc_seq_ls_acc(sp);
1772 fc_frame_free(fp); 2025 goto out;
1773 return;
1774 2026
1775unlock_reject: 2027unlock_reject:
1776 spin_unlock_bh(&ep->ex_lock); 2028 spin_unlock_bh(&ep->ex_lock);
1777 fc_exch_release(ep); /* drop hold from fc_exch_find */
1778reject: 2029reject:
1779 fc_seq_ls_rjt(sp, ELS_RJT_LOGIC, explan); 2030 fc_seq_ls_rjt(sp, ELS_RJT_LOGIC, explan);
2031out:
1780 fc_frame_free(fp); 2032 fc_frame_free(fp);
2033 if (ep)
2034 fc_exch_release(ep); /* drop hold from fc_exch_find */
1781} 2035}
1782 2036
2037/**
2038 * fc_exch_mgr_add() - Add an exchange manager to a local port's list of EMs
2039 * @lport: The local port to add the exchange manager to
2040 * @mp: The exchange manager to be added to the local port
2041 * @match: The match routine that indicates when this EM should be used
2042 */
1783struct fc_exch_mgr_anchor *fc_exch_mgr_add(struct fc_lport *lport, 2043struct fc_exch_mgr_anchor *fc_exch_mgr_add(struct fc_lport *lport,
1784 struct fc_exch_mgr *mp, 2044 struct fc_exch_mgr *mp,
1785 bool (*match)(struct fc_frame *)) 2045 bool (*match)(struct fc_frame *))
@@ -1799,6 +2059,10 @@ struct fc_exch_mgr_anchor *fc_exch_mgr_add(struct fc_lport *lport,
1799} 2059}
1800EXPORT_SYMBOL(fc_exch_mgr_add); 2060EXPORT_SYMBOL(fc_exch_mgr_add);
1801 2061
2062/**
2063 * fc_exch_mgr_destroy() - Destroy an exchange manager
2064 * @kref: The reference to the EM to be destroyed
2065 */
1802static void fc_exch_mgr_destroy(struct kref *kref) 2066static void fc_exch_mgr_destroy(struct kref *kref)
1803{ 2067{
1804 struct fc_exch_mgr *mp = container_of(kref, struct fc_exch_mgr, kref); 2068 struct fc_exch_mgr *mp = container_of(kref, struct fc_exch_mgr, kref);
@@ -1808,6 +2072,10 @@ static void fc_exch_mgr_destroy(struct kref *kref)
1808 kfree(mp); 2072 kfree(mp);
1809} 2073}
1810 2074
2075/**
2076 * fc_exch_mgr_del() - Delete an EM from a local port's list
2077 * @ema: The exchange manager anchor identifying the EM to be deleted
2078 */
1811void fc_exch_mgr_del(struct fc_exch_mgr_anchor *ema) 2079void fc_exch_mgr_del(struct fc_exch_mgr_anchor *ema)
1812{ 2080{
1813 /* remove EM anchor from EM anchors list */ 2081 /* remove EM anchor from EM anchors list */
@@ -1817,7 +2085,35 @@ void fc_exch_mgr_del(struct fc_exch_mgr_anchor *ema)
1817} 2085}
1818EXPORT_SYMBOL(fc_exch_mgr_del); 2086EXPORT_SYMBOL(fc_exch_mgr_del);
1819 2087
1820struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lp, 2088/**
2089 * fc_exch_mgr_list_clone() - Share all exchange manager objects
2090 * @src: Source lport to clone exchange managers from
2091 * @dst: New lport that takes references to all the exchange managers
2092 */
2093int fc_exch_mgr_list_clone(struct fc_lport *src, struct fc_lport *dst)
2094{
2095 struct fc_exch_mgr_anchor *ema, *tmp;
2096
2097 list_for_each_entry(ema, &src->ema_list, ema_list) {
2098 if (!fc_exch_mgr_add(dst, ema->mp, ema->match))
2099 goto err;
2100 }
2101 return 0;
2102err:
2103 list_for_each_entry_safe(ema, tmp, &dst->ema_list, ema_list)
2104 fc_exch_mgr_del(ema);
2105 return -ENOMEM;
2106}
2107
2108/**
2109 * fc_exch_mgr_alloc() - Allocate an exchange manager
2110 * @lport: The local port that the new EM will be associated with
2111 * @class: The default FC class for new exchanges
2112 * @min_xid: The minimum XID for exchanges from the new EM
2113 * @max_xid: The maximum XID for exchanges from the new EM
2114 * @match: The match routine for the new EM
2115 */
2116struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lport,
1821 enum fc_class class, 2117 enum fc_class class,
1822 u16 min_xid, u16 max_xid, 2118 u16 min_xid, u16 max_xid,
1823 bool (*match)(struct fc_frame *)) 2119 bool (*match)(struct fc_frame *))
@@ -1830,7 +2126,7 @@ struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lp,
1830 2126
1831 if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN || 2127 if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN ||
1832 (min_xid & fc_cpu_mask) != 0) { 2128 (min_xid & fc_cpu_mask) != 0) {
1833 FC_LPORT_DBG(lp, "Invalid min_xid 0x:%x and max_xid 0x:%x\n", 2129 FC_LPORT_DBG(lport, "Invalid min_xid 0x:%x and max_xid 0x:%x\n",
1834 min_xid, max_xid); 2130 min_xid, max_xid);
1835 return NULL; 2131 return NULL;
1836 } 2132 }
@@ -1873,7 +2169,7 @@ struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lp,
1873 } 2169 }
1874 2170
1875 kref_init(&mp->kref); 2171 kref_init(&mp->kref);
1876 if (!fc_exch_mgr_add(lp, mp, match)) { 2172 if (!fc_exch_mgr_add(lport, mp, match)) {
1877 free_percpu(mp->pool); 2173 free_percpu(mp->pool);
1878 goto free_mempool; 2174 goto free_mempool;
1879 } 2175 }
@@ -1894,76 +2190,26 @@ free_mp:
1894} 2190}
1895EXPORT_SYMBOL(fc_exch_mgr_alloc); 2191EXPORT_SYMBOL(fc_exch_mgr_alloc);
1896 2192
2193/**
2194 * fc_exch_mgr_free() - Free all exchange managers on a local port
2195 * @lport: The local port whose EMs are to be freed
2196 */
1897void fc_exch_mgr_free(struct fc_lport *lport) 2197void fc_exch_mgr_free(struct fc_lport *lport)
1898{ 2198{
1899 struct fc_exch_mgr_anchor *ema, *next; 2199 struct fc_exch_mgr_anchor *ema, *next;
1900 2200
2201 flush_workqueue(fc_exch_workqueue);
1901 list_for_each_entry_safe(ema, next, &lport->ema_list, ema_list) 2202 list_for_each_entry_safe(ema, next, &lport->ema_list, ema_list)
1902 fc_exch_mgr_del(ema); 2203 fc_exch_mgr_del(ema);
1903} 2204}
1904EXPORT_SYMBOL(fc_exch_mgr_free); 2205EXPORT_SYMBOL(fc_exch_mgr_free);
1905 2206
1906 2207/**
1907struct fc_seq *fc_exch_seq_send(struct fc_lport *lp, 2208 * fc_exch_recv() - Handler for received frames
1908 struct fc_frame *fp, 2209 * @lport: The local port the frame was received on
1909 void (*resp)(struct fc_seq *, 2210 * @fp: The received frame
1910 struct fc_frame *fp,
1911 void *arg),
1912 void (*destructor)(struct fc_seq *, void *),
1913 void *arg, u32 timer_msec)
1914{
1915 struct fc_exch *ep;
1916 struct fc_seq *sp = NULL;
1917 struct fc_frame_header *fh;
1918 int rc = 1;
1919
1920 ep = fc_exch_alloc(lp, fp);
1921 if (!ep) {
1922 fc_frame_free(fp);
1923 return NULL;
1924 }
1925 ep->esb_stat |= ESB_ST_SEQ_INIT;
1926 fh = fc_frame_header_get(fp);
1927 fc_exch_set_addr(ep, ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id));
1928 ep->resp = resp;
1929 ep->destructor = destructor;
1930 ep->arg = arg;
1931 ep->r_a_tov = FC_DEF_R_A_TOV;
1932 ep->lp = lp;
1933 sp = &ep->seq;
1934
1935 ep->fh_type = fh->fh_type; /* save for possbile timeout handling */
1936 ep->f_ctl = ntoh24(fh->fh_f_ctl);
1937 fc_exch_setup_hdr(ep, fp, ep->f_ctl);
1938 sp->cnt++;
1939
1940 if (ep->xid <= lp->lro_xid)
1941 fc_fcp_ddp_setup(fr_fsp(fp), ep->xid);
1942
1943 if (unlikely(lp->tt.frame_send(lp, fp)))
1944 goto err;
1945
1946 if (timer_msec)
1947 fc_exch_timer_set_locked(ep, timer_msec);
1948 ep->f_ctl &= ~FC_FC_FIRST_SEQ; /* not first seq */
1949
1950 if (ep->f_ctl & FC_FC_SEQ_INIT)
1951 ep->esb_stat &= ~ESB_ST_SEQ_INIT;
1952 spin_unlock_bh(&ep->ex_lock);
1953 return sp;
1954err:
1955 rc = fc_exch_done_locked(ep);
1956 spin_unlock_bh(&ep->ex_lock);
1957 if (!rc)
1958 fc_exch_delete(ep);
1959 return NULL;
1960}
1961EXPORT_SYMBOL(fc_exch_seq_send);
1962
1963/*
1964 * Receive a frame
1965 */ 2211 */
1966void fc_exch_recv(struct fc_lport *lp, struct fc_frame *fp) 2212void fc_exch_recv(struct fc_lport *lport, struct fc_frame *fp)
1967{ 2213{
1968 struct fc_frame_header *fh = fc_frame_header_get(fp); 2214 struct fc_frame_header *fh = fc_frame_header_get(fp);
1969 struct fc_exch_mgr_anchor *ema; 2215 struct fc_exch_mgr_anchor *ema;
@@ -1971,8 +2217,8 @@ void fc_exch_recv(struct fc_lport *lp, struct fc_frame *fp)
1971 u16 oxid; 2217 u16 oxid;
1972 2218
1973 /* lport lock ? */ 2219 /* lport lock ? */
1974 if (!lp || lp->state == LPORT_ST_DISABLED) { 2220 if (!lport || lport->state == LPORT_ST_DISABLED) {
1975 FC_LPORT_DBG(lp, "Receiving frames for an lport that " 2221 FC_LPORT_DBG(lport, "Receiving frames for an lport that "
1976 "has not been initialized correctly\n"); 2222 "has not been initialized correctly\n");
1977 fc_frame_free(fp); 2223 fc_frame_free(fp);
1978 return; 2224 return;
@@ -1981,7 +2227,7 @@ void fc_exch_recv(struct fc_lport *lp, struct fc_frame *fp)
1981 f_ctl = ntoh24(fh->fh_f_ctl); 2227 f_ctl = ntoh24(fh->fh_f_ctl);
1982 oxid = ntohs(fh->fh_ox_id); 2228 oxid = ntohs(fh->fh_ox_id);
1983 if (f_ctl & FC_FC_EX_CTX) { 2229 if (f_ctl & FC_FC_EX_CTX) {
1984 list_for_each_entry(ema, &lp->ema_list, ema_list) { 2230 list_for_each_entry(ema, &lport->ema_list, ema_list) {
1985 if ((oxid >= ema->mp->min_xid) && 2231 if ((oxid >= ema->mp->min_xid) &&
1986 (oxid <= ema->mp->max_xid)) { 2232 (oxid <= ema->mp->max_xid)) {
1987 found = 1; 2233 found = 1;
@@ -1990,13 +2236,13 @@ void fc_exch_recv(struct fc_lport *lp, struct fc_frame *fp)
1990 } 2236 }
1991 2237
1992 if (!found) { 2238 if (!found) {
1993 FC_LPORT_DBG(lp, "Received response for out " 2239 FC_LPORT_DBG(lport, "Received response for out "
1994 "of range oxid:%hx\n", oxid); 2240 "of range oxid:%hx\n", oxid);
1995 fc_frame_free(fp); 2241 fc_frame_free(fp);
1996 return; 2242 return;
1997 } 2243 }
1998 } else 2244 } else
1999 ema = list_entry(lp->ema_list.prev, typeof(*ema), ema_list); 2245 ema = list_entry(lport->ema_list.prev, typeof(*ema), ema_list);
2000 2246
2001 /* 2247 /*
2002 * If frame is marked invalid, just drop it. 2248 * If frame is marked invalid, just drop it.
@@ -2015,37 +2261,56 @@ void fc_exch_recv(struct fc_lport *lp, struct fc_frame *fp)
2015 else if (f_ctl & FC_FC_SEQ_CTX) 2261 else if (f_ctl & FC_FC_SEQ_CTX)
2016 fc_exch_recv_resp(ema->mp, fp); 2262 fc_exch_recv_resp(ema->mp, fp);
2017 else 2263 else
2018 fc_exch_recv_req(lp, ema->mp, fp); 2264 fc_exch_recv_req(lport, ema->mp, fp);
2019 break; 2265 break;
2020 default: 2266 default:
2021 FC_LPORT_DBG(lp, "dropping invalid frame (eof %x)", fr_eof(fp)); 2267 FC_LPORT_DBG(lport, "dropping invalid frame (eof %x)",
2268 fr_eof(fp));
2022 fc_frame_free(fp); 2269 fc_frame_free(fp);
2023 } 2270 }
2024} 2271}
2025EXPORT_SYMBOL(fc_exch_recv); 2272EXPORT_SYMBOL(fc_exch_recv);
2026 2273
2027int fc_exch_init(struct fc_lport *lp) 2274/**
2275 * fc_exch_init() - Initialize the exchange layer for a local port
2276 * @lport: The local port to initialize the exchange layer for
2277 */
2278int fc_exch_init(struct fc_lport *lport)
2028{ 2279{
2029 if (!lp->tt.seq_start_next) 2280 if (!lport->tt.seq_start_next)
2030 lp->tt.seq_start_next = fc_seq_start_next; 2281 lport->tt.seq_start_next = fc_seq_start_next;
2031 2282
2032 if (!lp->tt.exch_seq_send) 2283 if (!lport->tt.exch_seq_send)
2033 lp->tt.exch_seq_send = fc_exch_seq_send; 2284 lport->tt.exch_seq_send = fc_exch_seq_send;
2034 2285
2035 if (!lp->tt.seq_send) 2286 if (!lport->tt.seq_send)
2036 lp->tt.seq_send = fc_seq_send; 2287 lport->tt.seq_send = fc_seq_send;
2037 2288
2038 if (!lp->tt.seq_els_rsp_send) 2289 if (!lport->tt.seq_els_rsp_send)
2039 lp->tt.seq_els_rsp_send = fc_seq_els_rsp_send; 2290 lport->tt.seq_els_rsp_send = fc_seq_els_rsp_send;
2040 2291
2041 if (!lp->tt.exch_done) 2292 if (!lport->tt.exch_done)
2042 lp->tt.exch_done = fc_exch_done; 2293 lport->tt.exch_done = fc_exch_done;
2043 2294
2044 if (!lp->tt.exch_mgr_reset) 2295 if (!lport->tt.exch_mgr_reset)
2045 lp->tt.exch_mgr_reset = fc_exch_mgr_reset; 2296 lport->tt.exch_mgr_reset = fc_exch_mgr_reset;
2046 2297
2047 if (!lp->tt.seq_exch_abort) 2298 if (!lport->tt.seq_exch_abort)
2048 lp->tt.seq_exch_abort = fc_seq_exch_abort; 2299 lport->tt.seq_exch_abort = fc_seq_exch_abort;
2300
2301 return 0;
2302}
2303EXPORT_SYMBOL(fc_exch_init);
2304
2305/**
2306 * fc_setup_exch_mgr() - Setup an exchange manager
2307 */
2308int fc_setup_exch_mgr()
2309{
2310 fc_em_cachep = kmem_cache_create("libfc_em", sizeof(struct fc_exch),
2311 0, SLAB_HWCACHE_ALIGN, NULL);
2312 if (!fc_em_cachep)
2313 return -ENOMEM;
2049 2314
2050 /* 2315 /*
2051 * Initialize fc_cpu_mask and fc_cpu_order. The 2316 * Initialize fc_cpu_mask and fc_cpu_order. The
@@ -2069,20 +2334,17 @@ int fc_exch_init(struct fc_lport *lp)
2069 } 2334 }
2070 fc_cpu_mask--; 2335 fc_cpu_mask--;
2071 2336
2072 return 0; 2337 fc_exch_workqueue = create_singlethread_workqueue("fc_exch_workqueue");
2073} 2338 if (!fc_exch_workqueue)
2074EXPORT_SYMBOL(fc_exch_init);
2075
2076int fc_setup_exch_mgr(void)
2077{
2078 fc_em_cachep = kmem_cache_create("libfc_em", sizeof(struct fc_exch),
2079 0, SLAB_HWCACHE_ALIGN, NULL);
2080 if (!fc_em_cachep)
2081 return -ENOMEM; 2339 return -ENOMEM;
2082 return 0; 2340 return 0;
2083} 2341}
2084 2342
2085void fc_destroy_exch_mgr(void) 2343/**
2344 * fc_destroy_exch_mgr() - Destroy an exchange manager
2345 */
2346void fc_destroy_exch_mgr()
2086{ 2347{
2348 destroy_workqueue(fc_exch_workqueue);
2087 kmem_cache_destroy(fc_em_cachep); 2349 kmem_cache_destroy(fc_em_cachep);
2088} 2350}
diff --git a/drivers/scsi/libfc/fc_fcp.c b/drivers/scsi/libfc/fc_fcp.c
index 59a4408b27b5..17396c708b08 100644
--- a/drivers/scsi/libfc/fc_fcp.c
+++ b/drivers/scsi/libfc/fc_fcp.c
@@ -27,6 +27,7 @@
27#include <linux/scatterlist.h> 27#include <linux/scatterlist.h>
28#include <linux/err.h> 28#include <linux/err.h>
29#include <linux/crc32.h> 29#include <linux/crc32.h>
30#include <linux/slab.h>
30 31
31#include <scsi/scsi_tcq.h> 32#include <scsi/scsi_tcq.h>
32#include <scsi/scsi.h> 33#include <scsi/scsi.h>
@@ -39,26 +40,19 @@
39#include <scsi/libfc.h> 40#include <scsi/libfc.h>
40#include <scsi/fc_encode.h> 41#include <scsi/fc_encode.h>
41 42
42MODULE_AUTHOR("Open-FCoE.org"); 43#include "fc_libfc.h"
43MODULE_DESCRIPTION("libfc");
44MODULE_LICENSE("GPL v2");
45 44
46unsigned int fc_debug_logging; 45struct kmem_cache *scsi_pkt_cachep;
47module_param_named(debug_logging, fc_debug_logging, int, S_IRUGO|S_IWUSR);
48MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
49
50static struct kmem_cache *scsi_pkt_cachep;
51 46
52/* SRB state definitions */ 47/* SRB state definitions */
53#define FC_SRB_FREE 0 /* cmd is free */ 48#define FC_SRB_FREE 0 /* cmd is free */
54#define FC_SRB_CMD_SENT (1 << 0) /* cmd has been sent */ 49#define FC_SRB_CMD_SENT (1 << 0) /* cmd has been sent */
55#define FC_SRB_RCV_STATUS (1 << 1) /* response has arrived */ 50#define FC_SRB_RCV_STATUS (1 << 1) /* response has arrived */
56#define FC_SRB_ABORT_PENDING (1 << 2) /* cmd abort sent to device */ 51#define FC_SRB_ABORT_PENDING (1 << 2) /* cmd abort sent to device */
57#define FC_SRB_ABORTED (1 << 3) /* abort acknowleged */ 52#define FC_SRB_ABORTED (1 << 3) /* abort acknowledged */
58#define FC_SRB_DISCONTIG (1 << 4) /* non-sequential data recvd */ 53#define FC_SRB_DISCONTIG (1 << 4) /* non-sequential data recvd */
59#define FC_SRB_COMPL (1 << 5) /* fc_io_compl has been run */ 54#define FC_SRB_COMPL (1 << 5) /* fc_io_compl has been run */
60#define FC_SRB_FCP_PROCESSING_TMO (1 << 6) /* timer function processing */ 55#define FC_SRB_FCP_PROCESSING_TMO (1 << 6) /* timer function processing */
61#define FC_SRB_NOMEM (1 << 7) /* dropped to out of mem */
62 56
63#define FC_SRB_READ (1 << 1) 57#define FC_SRB_READ (1 << 1)
64#define FC_SRB_WRITE (1 << 0) 58#define FC_SRB_WRITE (1 << 0)
@@ -73,10 +67,22 @@ static struct kmem_cache *scsi_pkt_cachep;
73#define CMD_SCSI_STATUS(Cmnd) ((Cmnd)->SCp.Status) 67#define CMD_SCSI_STATUS(Cmnd) ((Cmnd)->SCp.Status)
74#define CMD_RESID_LEN(Cmnd) ((Cmnd)->SCp.buffers_residual) 68#define CMD_RESID_LEN(Cmnd) ((Cmnd)->SCp.buffers_residual)
75 69
70/**
71 * struct fc_fcp_internal - FCP layer internal data
72 * @scsi_pkt_pool: Memory pool to draw FCP packets from
73 * @scsi_queue_lock: Protects the scsi_pkt_queue
74 * @scsi_pkt_queue: Current FCP packets
75 * @last_can_queue_ramp_down_time: ramp down time
76 * @last_can_queue_ramp_up_time: ramp up time
77 * @max_can_queue: max can_queue size
78 */
76struct fc_fcp_internal { 79struct fc_fcp_internal {
77 mempool_t *scsi_pkt_pool; 80 mempool_t *scsi_pkt_pool;
78 struct list_head scsi_pkt_queue; 81 spinlock_t scsi_queue_lock;
79 u8 throttled; 82 struct list_head scsi_pkt_queue;
83 unsigned long last_can_queue_ramp_down_time;
84 unsigned long last_can_queue_ramp_up_time;
85 int max_can_queue;
80}; 86};
81 87
82#define fc_get_scsi_internal(x) ((struct fc_fcp_internal *)(x)->scsi_priv) 88#define fc_get_scsi_internal(x) ((struct fc_fcp_internal *)(x)->scsi_priv)
@@ -90,9 +96,9 @@ static void fc_fcp_recv(struct fc_seq *, struct fc_frame *, void *);
90static void fc_fcp_resp(struct fc_fcp_pkt *, struct fc_frame *); 96static void fc_fcp_resp(struct fc_fcp_pkt *, struct fc_frame *);
91static void fc_fcp_complete_locked(struct fc_fcp_pkt *); 97static void fc_fcp_complete_locked(struct fc_fcp_pkt *);
92static void fc_tm_done(struct fc_seq *, struct fc_frame *, void *); 98static void fc_tm_done(struct fc_seq *, struct fc_frame *, void *);
93static void fc_fcp_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp); 99static void fc_fcp_error(struct fc_fcp_pkt *, struct fc_frame *);
94static void fc_timeout_error(struct fc_fcp_pkt *); 100static void fc_timeout_error(struct fc_fcp_pkt *);
95static void fc_fcp_timeout(unsigned long data); 101static void fc_fcp_timeout(unsigned long);
96static void fc_fcp_rec(struct fc_fcp_pkt *); 102static void fc_fcp_rec(struct fc_fcp_pkt *);
97static void fc_fcp_rec_error(struct fc_fcp_pkt *, struct fc_frame *); 103static void fc_fcp_rec_error(struct fc_fcp_pkt *, struct fc_frame *);
98static void fc_fcp_rec_resp(struct fc_seq *, struct fc_frame *, void *); 104static void fc_fcp_rec_resp(struct fc_seq *, struct fc_frame *, void *);
@@ -124,6 +130,7 @@ static void fc_fcp_srr_error(struct fc_fcp_pkt *, struct fc_frame *);
124#define FC_SCSI_TM_TOV (10 * HZ) 130#define FC_SCSI_TM_TOV (10 * HZ)
125#define FC_SCSI_REC_TOV (2 * HZ) 131#define FC_SCSI_REC_TOV (2 * HZ)
126#define FC_HOST_RESET_TIMEOUT (30 * HZ) 132#define FC_HOST_RESET_TIMEOUT (30 * HZ)
133#define FC_CAN_QUEUE_PERIOD (60 * HZ)
127 134
128#define FC_MAX_ERROR_CNT 5 135#define FC_MAX_ERROR_CNT 5
129#define FC_MAX_RECOV_RETRY 3 136#define FC_MAX_RECOV_RETRY 3
@@ -131,23 +138,22 @@ static void fc_fcp_srr_error(struct fc_fcp_pkt *, struct fc_frame *);
131#define FC_FCP_DFLT_QUEUE_DEPTH 32 138#define FC_FCP_DFLT_QUEUE_DEPTH 32
132 139
133/** 140/**
134 * fc_fcp_pkt_alloc - allocation routine for scsi_pkt packet 141 * fc_fcp_pkt_alloc() - Allocate a fcp_pkt
135 * @lp: fc lport struct 142 * @lport: The local port that the FCP packet is for
136 * @gfp: gfp flags for allocation 143 * @gfp: GFP flags for allocation
137 * 144 *
138 * This is used by upper layer scsi driver. 145 * Return value: fcp_pkt structure or null on allocation failure.
139 * Return Value : scsi_pkt structure or null on allocation failure. 146 * Context: Can be called from process context, no lock is required.
140 * Context : call from process context. no locking required.
141 */ 147 */
142static struct fc_fcp_pkt *fc_fcp_pkt_alloc(struct fc_lport *lp, gfp_t gfp) 148static struct fc_fcp_pkt *fc_fcp_pkt_alloc(struct fc_lport *lport, gfp_t gfp)
143{ 149{
144 struct fc_fcp_internal *si = fc_get_scsi_internal(lp); 150 struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
145 struct fc_fcp_pkt *fsp; 151 struct fc_fcp_pkt *fsp;
146 152
147 fsp = mempool_alloc(si->scsi_pkt_pool, gfp); 153 fsp = mempool_alloc(si->scsi_pkt_pool, gfp);
148 if (fsp) { 154 if (fsp) {
149 memset(fsp, 0, sizeof(*fsp)); 155 memset(fsp, 0, sizeof(*fsp));
150 fsp->lp = lp; 156 fsp->lp = lport;
151 atomic_set(&fsp->ref_cnt, 1); 157 atomic_set(&fsp->ref_cnt, 1);
152 init_timer(&fsp->timer); 158 init_timer(&fsp->timer);
153 INIT_LIST_HEAD(&fsp->list); 159 INIT_LIST_HEAD(&fsp->list);
@@ -157,12 +163,11 @@ static struct fc_fcp_pkt *fc_fcp_pkt_alloc(struct fc_lport *lp, gfp_t gfp)
157} 163}
158 164
159/** 165/**
160 * fc_fcp_pkt_release() - release hold on scsi_pkt packet 166 * fc_fcp_pkt_release() - Release hold on a fcp_pkt
161 * @fsp: fcp packet struct 167 * @fsp: The FCP packet to be released
162 * 168 *
163 * This is used by upper layer scsi driver. 169 * Context: Can be called from process or interrupt context,
164 * Context : call from process and interrupt context. 170 * no lock is required.
165 * no locking required
166 */ 171 */
167static void fc_fcp_pkt_release(struct fc_fcp_pkt *fsp) 172static void fc_fcp_pkt_release(struct fc_fcp_pkt *fsp)
168{ 173{
@@ -173,20 +178,25 @@ static void fc_fcp_pkt_release(struct fc_fcp_pkt *fsp)
173 } 178 }
174} 179}
175 180
181/**
182 * fc_fcp_pkt_hold() - Hold a fcp_pkt
183 * @fsp: The FCP packet to be held
184 */
176static void fc_fcp_pkt_hold(struct fc_fcp_pkt *fsp) 185static void fc_fcp_pkt_hold(struct fc_fcp_pkt *fsp)
177{ 186{
178 atomic_inc(&fsp->ref_cnt); 187 atomic_inc(&fsp->ref_cnt);
179} 188}
180 189
181/** 190/**
182 * fc_fcp_pkt_destory() - release hold on scsi_pkt packet 191 * fc_fcp_pkt_destory() - Release hold on a fcp_pkt
183 * @seq: exchange sequence 192 * @seq: The sequence that the FCP packet is on (required by destructor API)
184 * @fsp: fcp packet struct 193 * @fsp: The FCP packet to be released
194 *
195 * This routine is called by a destructor callback in the exch_seq_send()
196 * routine of the libfc Transport Template. The 'struct fc_seq' is a required
197 * argument even though it is not used by this routine.
185 * 198 *
186 * Release hold on scsi_pkt packet set to keep scsi_pkt 199 * Context: No locking required.
187 * till EM layer exch resource is not freed.
188 * Context : called from from EM layer.
189 * no locking required
190 */ 200 */
191static void fc_fcp_pkt_destroy(struct fc_seq *seq, void *fsp) 201static void fc_fcp_pkt_destroy(struct fc_seq *seq, void *fsp)
192{ 202{
@@ -194,10 +204,10 @@ static void fc_fcp_pkt_destroy(struct fc_seq *seq, void *fsp)
194} 204}
195 205
196/** 206/**
197 * fc_fcp_lock_pkt() - lock a packet and get a ref to it. 207 * fc_fcp_lock_pkt() - Lock a fcp_pkt and increase its reference count
198 * @fsp: fcp packet 208 * @fsp: The FCP packet to be locked and incremented
199 * 209 *
200 * We should only return error if we return a command to scsi-ml before 210 * We should only return error if we return a command to SCSI-ml before
201 * getting a response. This can happen in cases where we send a abort, but 211 * getting a response. This can happen in cases where we send a abort, but
202 * do not wait for the response and the abort and command can be passing 212 * do not wait for the response and the abort and command can be passing
203 * each other on the wire/network-layer. 213 * each other on the wire/network-layer.
@@ -222,18 +232,33 @@ static inline int fc_fcp_lock_pkt(struct fc_fcp_pkt *fsp)
222 return 0; 232 return 0;
223} 233}
224 234
235/**
236 * fc_fcp_unlock_pkt() - Release a fcp_pkt's lock and decrement its
237 * reference count
238 * @fsp: The FCP packet to be unlocked and decremented
239 */
225static inline void fc_fcp_unlock_pkt(struct fc_fcp_pkt *fsp) 240static inline void fc_fcp_unlock_pkt(struct fc_fcp_pkt *fsp)
226{ 241{
227 spin_unlock_bh(&fsp->scsi_pkt_lock); 242 spin_unlock_bh(&fsp->scsi_pkt_lock);
228 fc_fcp_pkt_release(fsp); 243 fc_fcp_pkt_release(fsp);
229} 244}
230 245
246/**
247 * fc_fcp_timer_set() - Start a timer for a fcp_pkt
248 * @fsp: The FCP packet to start a timer for
249 * @delay: The timeout period for the timer
250 */
231static void fc_fcp_timer_set(struct fc_fcp_pkt *fsp, unsigned long delay) 251static void fc_fcp_timer_set(struct fc_fcp_pkt *fsp, unsigned long delay)
232{ 252{
233 if (!(fsp->state & FC_SRB_COMPL)) 253 if (!(fsp->state & FC_SRB_COMPL))
234 mod_timer(&fsp->timer, jiffies + delay); 254 mod_timer(&fsp->timer, jiffies + delay);
235} 255}
236 256
257/**
258 * fc_fcp_send_abort() - Send an abort for exchanges associated with a
259 * fcp_pkt
260 * @fsp: The FCP packet to abort exchanges on
261 */
237static int fc_fcp_send_abort(struct fc_fcp_pkt *fsp) 262static int fc_fcp_send_abort(struct fc_fcp_pkt *fsp)
238{ 263{
239 if (!fsp->seq_ptr) 264 if (!fsp->seq_ptr)
@@ -243,9 +268,14 @@ static int fc_fcp_send_abort(struct fc_fcp_pkt *fsp)
243 return fsp->lp->tt.seq_exch_abort(fsp->seq_ptr, 0); 268 return fsp->lp->tt.seq_exch_abort(fsp->seq_ptr, 0);
244} 269}
245 270
246/* 271/**
247 * Retry command. 272 * fc_fcp_retry_cmd() - Retry a fcp_pkt
248 * An abort isn't needed. 273 * @fsp: The FCP packet to be retried
274 *
275 * Sets the status code to be FC_ERROR and then calls
276 * fc_fcp_complete_locked() which in turn calls fc_io_compl().
277 * fc_io_compl() will notify the SCSI-ml that the I/O is done.
278 * The SCSI-ml will retry the command.
249 */ 279 */
250static void fc_fcp_retry_cmd(struct fc_fcp_pkt *fsp) 280static void fc_fcp_retry_cmd(struct fc_fcp_pkt *fsp)
251{ 281{
@@ -260,64 +290,145 @@ static void fc_fcp_retry_cmd(struct fc_fcp_pkt *fsp)
260 fc_fcp_complete_locked(fsp); 290 fc_fcp_complete_locked(fsp);
261} 291}
262 292
263/* 293/**
264 * fc_fcp_ddp_setup - calls to LLD's ddp_setup to set up DDP 294 * fc_fcp_ddp_setup() - Calls a LLD's ddp_setup routine to set up DDP context
265 * transfer for a read I/O indicated by the fc_fcp_pkt. 295 * @fsp: The FCP packet that will manage the DDP frames
266 * @fsp: ptr to the fc_fcp_pkt 296 * @xid: The XID that will be used for the DDP exchange
267 *
268 * This is called in exch_seq_send() when we have a newly allocated
269 * exchange with a valid exchange id to setup ddp.
270 *
271 * returns: none
272 */ 297 */
273void fc_fcp_ddp_setup(struct fc_fcp_pkt *fsp, u16 xid) 298void fc_fcp_ddp_setup(struct fc_fcp_pkt *fsp, u16 xid)
274{ 299{
275 struct fc_lport *lp; 300 struct fc_lport *lport;
276
277 if (!fsp)
278 return;
279 301
280 lp = fsp->lp; 302 lport = fsp->lp;
281 if ((fsp->req_flags & FC_SRB_READ) && 303 if ((fsp->req_flags & FC_SRB_READ) &&
282 (lp->lro_enabled) && (lp->tt.ddp_setup)) { 304 (lport->lro_enabled) && (lport->tt.ddp_setup)) {
283 if (lp->tt.ddp_setup(lp, xid, scsi_sglist(fsp->cmd), 305 if (lport->tt.ddp_setup(lport, xid, scsi_sglist(fsp->cmd),
284 scsi_sg_count(fsp->cmd))) 306 scsi_sg_count(fsp->cmd)))
285 fsp->xfer_ddp = xid; 307 fsp->xfer_ddp = xid;
286 } 308 }
287} 309}
288EXPORT_SYMBOL(fc_fcp_ddp_setup);
289 310
290/* 311/**
291 * fc_fcp_ddp_done - calls to LLD's ddp_done to release any 312 * fc_fcp_ddp_done() - Calls a LLD's ddp_done routine to release any
292 * DDP related resources for this I/O if it is initialized 313 * DDP related resources for a fcp_pkt
293 * as a ddp transfer 314 * @fsp: The FCP packet that DDP had been used on
294 * @fsp: ptr to the fc_fcp_pkt
295 *
296 * returns: none
297 */ 315 */
298static void fc_fcp_ddp_done(struct fc_fcp_pkt *fsp) 316static void fc_fcp_ddp_done(struct fc_fcp_pkt *fsp)
299{ 317{
300 struct fc_lport *lp; 318 struct fc_lport *lport;
301 319
302 if (!fsp) 320 if (!fsp)
303 return; 321 return;
304 322
305 lp = fsp->lp; 323 if (fsp->xfer_ddp == FC_XID_UNKNOWN)
306 if (fsp->xfer_ddp && lp->tt.ddp_done) { 324 return;
307 fsp->xfer_len = lp->tt.ddp_done(lp, fsp->xfer_ddp); 325
308 fsp->xfer_ddp = 0; 326 lport = fsp->lp;
327 if (lport->tt.ddp_done) {
328 fsp->xfer_len = lport->tt.ddp_done(lport, fsp->xfer_ddp);
329 fsp->xfer_ddp = FC_XID_UNKNOWN;
330 }
331}
332
333/**
334 * fc_fcp_can_queue_ramp_up() - increases can_queue
335 * @lport: lport to ramp up can_queue
336 *
337 * Locking notes: Called with Scsi_Host lock held
338 */
339static void fc_fcp_can_queue_ramp_up(struct fc_lport *lport)
340{
341 struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
342 int can_queue;
343
344 if (si->last_can_queue_ramp_up_time &&
345 (time_before(jiffies, si->last_can_queue_ramp_up_time +
346 FC_CAN_QUEUE_PERIOD)))
347 return;
348
349 if (time_before(jiffies, si->last_can_queue_ramp_down_time +
350 FC_CAN_QUEUE_PERIOD))
351 return;
352
353 si->last_can_queue_ramp_up_time = jiffies;
354
355 can_queue = lport->host->can_queue << 1;
356 if (can_queue >= si->max_can_queue) {
357 can_queue = si->max_can_queue;
358 si->last_can_queue_ramp_down_time = 0;
309 } 359 }
360 lport->host->can_queue = can_queue;
361 shost_printk(KERN_ERR, lport->host, "libfc: increased "
362 "can_queue to %d.\n", can_queue);
310} 363}
311 364
365/**
366 * fc_fcp_can_queue_ramp_down() - reduces can_queue
367 * @lport: lport to reduce can_queue
368 *
369 * If we are getting memory allocation failures, then we may
370 * be trying to execute too many commands. We let the running
371 * commands complete or timeout, then try again with a reduced
372 * can_queue. Eventually we will hit the point where we run
373 * on all reserved structs.
374 *
375 * Locking notes: Called with Scsi_Host lock held
376 */
377static void fc_fcp_can_queue_ramp_down(struct fc_lport *lport)
378{
379 struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
380 int can_queue;
381
382 if (si->last_can_queue_ramp_down_time &&
383 (time_before(jiffies, si->last_can_queue_ramp_down_time +
384 FC_CAN_QUEUE_PERIOD)))
385 return;
386
387 si->last_can_queue_ramp_down_time = jiffies;
388
389 can_queue = lport->host->can_queue;
390 can_queue >>= 1;
391 if (!can_queue)
392 can_queue = 1;
393 lport->host->can_queue = can_queue;
394 shost_printk(KERN_ERR, lport->host, "libfc: Could not allocate frame.\n"
395 "Reducing can_queue to %d.\n", can_queue);
396}
312 397
313/* 398/*
314 * Receive SCSI data from target. 399 * fc_fcp_frame_alloc() - Allocates fc_frame structure and buffer.
315 * Called after receiving solicited data. 400 * @lport: fc lport struct
401 * @len: payload length
402 *
403 * Allocates fc_frame structure and buffer but if fails to allocate
404 * then reduce can_queue.
405 */
406static inline struct fc_frame *fc_fcp_frame_alloc(struct fc_lport *lport,
407 size_t len)
408{
409 struct fc_frame *fp;
410 unsigned long flags;
411
412 fp = fc_frame_alloc(lport, len);
413 if (likely(fp))
414 return fp;
415
416 /* error case */
417 spin_lock_irqsave(lport->host->host_lock, flags);
418 fc_fcp_can_queue_ramp_down(lport);
419 spin_unlock_irqrestore(lport->host->host_lock, flags);
420 return NULL;
421}
422
423/**
424 * fc_fcp_recv_data() - Handler for receiving SCSI-FCP data from a target
425 * @fsp: The FCP packet the data is on
426 * @fp: The data frame
316 */ 427 */
317static void fc_fcp_recv_data(struct fc_fcp_pkt *fsp, struct fc_frame *fp) 428static void fc_fcp_recv_data(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
318{ 429{
319 struct scsi_cmnd *sc = fsp->cmd; 430 struct scsi_cmnd *sc = fsp->cmd;
320 struct fc_lport *lp = fsp->lp; 431 struct fc_lport *lport = fsp->lp;
321 struct fcoe_dev_stats *stats; 432 struct fcoe_dev_stats *stats;
322 struct fc_frame_header *fh; 433 struct fc_frame_header *fh;
323 size_t start_offset; 434 size_t start_offset;
@@ -327,7 +438,7 @@ static void fc_fcp_recv_data(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
327 size_t len; 438 size_t len;
328 void *buf; 439 void *buf;
329 struct scatterlist *sg; 440 struct scatterlist *sg;
330 size_t remaining; 441 u32 nents;
331 442
332 fh = fc_frame_header_get(fp); 443 fh = fc_frame_header_get(fp);
333 offset = ntohl(fh->fh_parm_offset); 444 offset = ntohl(fh->fh_parm_offset);
@@ -351,65 +462,29 @@ static void fc_fcp_recv_data(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
351 if (offset != fsp->xfer_len) 462 if (offset != fsp->xfer_len)
352 fsp->state |= FC_SRB_DISCONTIG; 463 fsp->state |= FC_SRB_DISCONTIG;
353 464
354 crc = 0;
355 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED)
356 crc = crc32(~0, (u8 *) fh, sizeof(*fh));
357
358 sg = scsi_sglist(sc); 465 sg = scsi_sglist(sc);
359 remaining = len; 466 nents = scsi_sg_count(sc);
360
361 while (remaining > 0 && sg) {
362 size_t off;
363 void *page_addr;
364 size_t sg_bytes;
365
366 if (offset >= sg->length) {
367 offset -= sg->length;
368 sg = sg_next(sg);
369 continue;
370 }
371 sg_bytes = min(remaining, sg->length - offset);
372
373 /*
374 * The scatterlist item may be bigger than PAGE_SIZE,
375 * but we are limited to mapping PAGE_SIZE at a time.
376 */
377 off = offset + sg->offset;
378 sg_bytes = min(sg_bytes, (size_t)
379 (PAGE_SIZE - (off & ~PAGE_MASK)));
380 page_addr = kmap_atomic(sg_page(sg) + (off >> PAGE_SHIFT),
381 KM_SOFTIRQ0);
382 if (!page_addr)
383 break; /* XXX panic? */
384
385 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED)
386 crc = crc32(crc, buf, sg_bytes);
387 memcpy((char *)page_addr + (off & ~PAGE_MASK), buf,
388 sg_bytes);
389
390 kunmap_atomic(page_addr, KM_SOFTIRQ0);
391 buf += sg_bytes;
392 offset += sg_bytes;
393 remaining -= sg_bytes;
394 copy_len += sg_bytes;
395 }
396 467
397 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) { 468 if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED)) {
469 copy_len = fc_copy_buffer_to_sglist(buf, len, sg, &nents,
470 &offset, KM_SOFTIRQ0, NULL);
471 } else {
472 crc = crc32(~0, (u8 *) fh, sizeof(*fh));
473 copy_len = fc_copy_buffer_to_sglist(buf, len, sg, &nents,
474 &offset, KM_SOFTIRQ0, &crc);
398 buf = fc_frame_payload_get(fp, 0); 475 buf = fc_frame_payload_get(fp, 0);
399 if (len % 4) { 476 if (len % 4)
400 crc = crc32(crc, buf + len, 4 - (len % 4)); 477 crc = crc32(crc, buf + len, 4 - (len % 4));
401 len += 4 - (len % 4);
402 }
403 478
404 if (~crc != le32_to_cpu(fr_crc(fp))) { 479 if (~crc != le32_to_cpu(fr_crc(fp))) {
405crc_err: 480crc_err:
406 stats = fc_lport_get_stats(lp); 481 stats = fc_lport_get_stats(lport);
407 stats->ErrorFrames++; 482 stats->ErrorFrames++;
408 /* FIXME - per cpu count, not total count! */ 483 /* FIXME - per cpu count, not total count! */
409 if (stats->InvalidCRCCount++ < 5) 484 if (stats->InvalidCRCCount++ < 5)
410 printk(KERN_WARNING "libfc: CRC error on data " 485 printk(KERN_WARNING "libfc: CRC error on data "
411 "frame for port (%6x)\n", 486 "frame for port (%6x)\n",
412 fc_host_port_id(lp->host)); 487 fc_host_port_id(lport->host));
413 /* 488 /*
414 * Assume the frame is total garbage. 489 * Assume the frame is total garbage.
415 * We may have copied it over the good part 490 * We may have copied it over the good part
@@ -437,18 +512,17 @@ crc_err:
437} 512}
438 513
439/** 514/**
440 * fc_fcp_send_data() - Send SCSI data to target. 515 * fc_fcp_send_data() - Send SCSI data to a target
441 * @fsp: ptr to fc_fcp_pkt 516 * @fsp: The FCP packet the data is on
442 * @sp: ptr to this sequence 517 * @sp: The sequence the data is to be sent on
443 * @offset: starting offset for this data request 518 * @offset: The starting offset for this data request
444 * @seq_blen: the burst length for this data request 519 * @seq_blen: The burst length for this data request
445 * 520 *
446 * Called after receiving a Transfer Ready data descriptor. 521 * Called after receiving a Transfer Ready data descriptor.
447 * if LLD is capable of seq offload then send down seq_blen 522 * If the LLD is capable of sequence offload then send down the
448 * size of data in single frame, otherwise send multiple FC 523 * seq_blen amount of data in single frame, otherwise send
449 * frames of max FC frame payload supported by target port. 524 * multiple frames of the maximum frame payload supported by
450 * 525 * the target port.
451 * Returns : 0 for success.
452 */ 526 */
453static int fc_fcp_send_data(struct fc_fcp_pkt *fsp, struct fc_seq *seq, 527static int fc_fcp_send_data(struct fc_fcp_pkt *fsp, struct fc_seq *seq,
454 size_t offset, size_t seq_blen) 528 size_t offset, size_t seq_blen)
@@ -457,16 +531,18 @@ static int fc_fcp_send_data(struct fc_fcp_pkt *fsp, struct fc_seq *seq,
457 struct scsi_cmnd *sc; 531 struct scsi_cmnd *sc;
458 struct scatterlist *sg; 532 struct scatterlist *sg;
459 struct fc_frame *fp = NULL; 533 struct fc_frame *fp = NULL;
460 struct fc_lport *lp = fsp->lp; 534 struct fc_lport *lport = fsp->lp;
535 struct page *page;
461 size_t remaining; 536 size_t remaining;
462 size_t t_blen; 537 size_t t_blen;
463 size_t tlen; 538 size_t tlen;
464 size_t sg_bytes; 539 size_t sg_bytes;
465 size_t frame_offset, fh_parm_offset; 540 size_t frame_offset, fh_parm_offset;
541 size_t off;
466 int error; 542 int error;
467 void *data = NULL; 543 void *data = NULL;
468 void *page_addr; 544 void *page_addr;
469 int using_sg = lp->sg_supp; 545 int using_sg = lport->sg_supp;
470 u32 f_ctl; 546 u32 f_ctl;
471 547
472 WARN_ON(seq_blen <= 0); 548 WARN_ON(seq_blen <= 0);
@@ -488,10 +564,10 @@ static int fc_fcp_send_data(struct fc_fcp_pkt *fsp, struct fc_seq *seq,
488 * to max FC frame payload previously set in fsp->max_payload. 564 * to max FC frame payload previously set in fsp->max_payload.
489 */ 565 */
490 t_blen = fsp->max_payload; 566 t_blen = fsp->max_payload;
491 if (lp->seq_offload) { 567 if (lport->seq_offload) {
492 t_blen = min(seq_blen, (size_t)lp->lso_max); 568 t_blen = min(seq_blen, (size_t)lport->lso_max);
493 FC_FCP_DBG(fsp, "fsp=%p:lso:blen=%zx lso_max=0x%x t_blen=%zx\n", 569 FC_FCP_DBG(fsp, "fsp=%p:lso:blen=%zx lso_max=0x%x t_blen=%zx\n",
494 fsp, seq_blen, lp->lso_max, t_blen); 570 fsp, seq_blen, lport->lso_max, t_blen);
495 } 571 }
496 572
497 WARN_ON(t_blen < FC_MIN_MAX_PAYLOAD); 573 WARN_ON(t_blen < FC_MIN_MAX_PAYLOAD);
@@ -503,7 +579,7 @@ static int fc_fcp_send_data(struct fc_fcp_pkt *fsp, struct fc_seq *seq,
503 remaining = seq_blen; 579 remaining = seq_blen;
504 fh_parm_offset = frame_offset = offset; 580 fh_parm_offset = frame_offset = offset;
505 tlen = 0; 581 tlen = 0;
506 seq = lp->tt.seq_start_next(seq); 582 seq = lport->tt.seq_start_next(seq);
507 f_ctl = FC_FC_REL_OFF; 583 f_ctl = FC_FC_REL_OFF;
508 WARN_ON(!seq); 584 WARN_ON(!seq);
509 585
@@ -525,43 +601,34 @@ static int fc_fcp_send_data(struct fc_fcp_pkt *fsp, struct fc_seq *seq,
525 */ 601 */
526 if (tlen % 4) 602 if (tlen % 4)
527 using_sg = 0; 603 using_sg = 0;
528 if (using_sg) { 604 fp = fc_frame_alloc(lport, using_sg ? 0 : tlen);
529 fp = _fc_frame_alloc(lp, 0); 605 if (!fp)
530 if (!fp) 606 return -ENOMEM;
531 return -ENOMEM;
532 } else {
533 fp = fc_frame_alloc(lp, tlen);
534 if (!fp)
535 return -ENOMEM;
536 607
537 data = (void *)(fr_hdr(fp)) + 608 data = fc_frame_header_get(fp) + 1;
538 sizeof(struct fc_frame_header);
539 }
540 fh_parm_offset = frame_offset; 609 fh_parm_offset = frame_offset;
541 fr_max_payload(fp) = fsp->max_payload; 610 fr_max_payload(fp) = fsp->max_payload;
542 } 611 }
612
613 off = offset + sg->offset;
543 sg_bytes = min(tlen, sg->length - offset); 614 sg_bytes = min(tlen, sg->length - offset);
615 sg_bytes = min(sg_bytes,
616 (size_t) (PAGE_SIZE - (off & ~PAGE_MASK)));
617 page = sg_page(sg) + (off >> PAGE_SHIFT);
544 if (using_sg) { 618 if (using_sg) {
545 get_page(sg_page(sg)); 619 get_page(page);
546 skb_fill_page_desc(fp_skb(fp), 620 skb_fill_page_desc(fp_skb(fp),
547 skb_shinfo(fp_skb(fp))->nr_frags, 621 skb_shinfo(fp_skb(fp))->nr_frags,
548 sg_page(sg), sg->offset + offset, 622 page, off & ~PAGE_MASK, sg_bytes);
549 sg_bytes);
550 fp_skb(fp)->data_len += sg_bytes; 623 fp_skb(fp)->data_len += sg_bytes;
551 fr_len(fp) += sg_bytes; 624 fr_len(fp) += sg_bytes;
552 fp_skb(fp)->truesize += PAGE_SIZE; 625 fp_skb(fp)->truesize += PAGE_SIZE;
553 } else { 626 } else {
554 size_t off = offset + sg->offset;
555
556 /* 627 /*
557 * The scatterlist item may be bigger than PAGE_SIZE, 628 * The scatterlist item may be bigger than PAGE_SIZE,
558 * but we must not cross pages inside the kmap. 629 * but we must not cross pages inside the kmap.
559 */ 630 */
560 sg_bytes = min(sg_bytes, (size_t) (PAGE_SIZE - 631 page_addr = kmap_atomic(page, KM_SOFTIRQ0);
561 (off & ~PAGE_MASK)));
562 page_addr = kmap_atomic(sg_page(sg) +
563 (off >> PAGE_SHIFT),
564 KM_SOFTIRQ0);
565 memcpy(data, (char *)page_addr + (off & ~PAGE_MASK), 632 memcpy(data, (char *)page_addr + (off & ~PAGE_MASK),
566 sg_bytes); 633 sg_bytes);
567 kunmap_atomic(page_addr, KM_SOFTIRQ0); 634 kunmap_atomic(page_addr, KM_SOFTIRQ0);
@@ -572,7 +639,8 @@ static int fc_fcp_send_data(struct fc_fcp_pkt *fsp, struct fc_seq *seq,
572 tlen -= sg_bytes; 639 tlen -= sg_bytes;
573 remaining -= sg_bytes; 640 remaining -= sg_bytes;
574 641
575 if (tlen) 642 if ((skb_shinfo(fp_skb(fp))->nr_frags < FC_FRAME_SG_LEN) &&
643 (tlen))
576 continue; 644 continue;
577 645
578 /* 646 /*
@@ -589,7 +657,7 @@ static int fc_fcp_send_data(struct fc_fcp_pkt *fsp, struct fc_seq *seq,
589 /* 657 /*
590 * send fragment using for a sequence. 658 * send fragment using for a sequence.
591 */ 659 */
592 error = lp->tt.seq_send(lp, seq, fp); 660 error = lport->tt.seq_send(lport, seq, fp);
593 if (error) { 661 if (error) {
594 WARN_ON(1); /* send error should be rare */ 662 WARN_ON(1); /* send error should be rare */
595 fc_fcp_retry_cmd(fsp); 663 fc_fcp_retry_cmd(fsp);
@@ -601,6 +669,11 @@ static int fc_fcp_send_data(struct fc_fcp_pkt *fsp, struct fc_seq *seq,
601 return 0; 669 return 0;
602} 670}
603 671
672/**
673 * fc_fcp_abts_resp() - Send an ABTS response
674 * @fsp: The FCP packet that is being aborted
675 * @fp: The response frame
676 */
604static void fc_fcp_abts_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp) 677static void fc_fcp_abts_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
605{ 678{
606 int ba_done = 1; 679 int ba_done = 1;
@@ -637,46 +710,13 @@ static void fc_fcp_abts_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
637} 710}
638 711
639/** 712/**
640 * fc_fcp_reduce_can_queue() - drop can_queue 713 * fc_fcp_recv() - Reveive an FCP frame
641 * @lp: lport to drop queueing for
642 *
643 * If we are getting memory allocation failures, then we may
644 * be trying to execute too many commands. We let the running
645 * commands complete or timeout, then try again with a reduced
646 * can_queue. Eventually we will hit the point where we run
647 * on all reserved structs.
648 */
649static void fc_fcp_reduce_can_queue(struct fc_lport *lp)
650{
651 struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
652 unsigned long flags;
653 int can_queue;
654
655 spin_lock_irqsave(lp->host->host_lock, flags);
656 if (si->throttled)
657 goto done;
658 si->throttled = 1;
659
660 can_queue = lp->host->can_queue;
661 can_queue >>= 1;
662 if (!can_queue)
663 can_queue = 1;
664 lp->host->can_queue = can_queue;
665 shost_printk(KERN_ERR, lp->host, "libfc: Could not allocate frame.\n"
666 "Reducing can_queue to %d.\n", can_queue);
667done:
668 spin_unlock_irqrestore(lp->host->host_lock, flags);
669}
670
671/**
672 * fc_fcp_recv() - Reveive FCP frames
673 * @seq: The sequence the frame is on 714 * @seq: The sequence the frame is on
674 * @fp: The FC frame 715 * @fp: The received frame
675 * @arg: The related FCP packet 716 * @arg: The related FCP packet
676 * 717 *
677 * Return : None 718 * Context: Called from Soft IRQ context. Can not be called
678 * Context : called from Soft IRQ context 719 * holding the FCP packet list lock.
679 * can not called holding list lock
680 */ 720 */
681static void fc_fcp_recv(struct fc_seq *seq, struct fc_frame *fp, void *arg) 721static void fc_fcp_recv(struct fc_seq *seq, struct fc_frame *fp, void *arg)
682{ 722{
@@ -687,8 +727,10 @@ static void fc_fcp_recv(struct fc_seq *seq, struct fc_frame *fp, void *arg)
687 u8 r_ctl; 727 u8 r_ctl;
688 int rc = 0; 728 int rc = 0;
689 729
690 if (IS_ERR(fp)) 730 if (IS_ERR(fp)) {
691 goto errout; 731 fc_fcp_error(fsp, fp);
732 return;
733 }
692 734
693 fh = fc_frame_header_get(fp); 735 fh = fc_frame_header_get(fp);
694 r_ctl = fh->fh_r_ctl; 736 r_ctl = fh->fh_r_ctl;
@@ -721,8 +763,6 @@ static void fc_fcp_recv(struct fc_seq *seq, struct fc_frame *fp, void *arg)
721 (size_t) ntohl(dd->ft_burst_len)); 763 (size_t) ntohl(dd->ft_burst_len));
722 if (!rc) 764 if (!rc)
723 seq->rec_data = fsp->xfer_len; 765 seq->rec_data = fsp->xfer_len;
724 else if (rc == -ENOMEM)
725 fsp->state |= FC_SRB_NOMEM;
726 } else if (r_ctl == FC_RCTL_DD_SOL_DATA) { 766 } else if (r_ctl == FC_RCTL_DD_SOL_DATA) {
727 /* 767 /*
728 * received a DATA frame 768 * received a DATA frame
@@ -742,13 +782,13 @@ unlock:
742 fc_fcp_unlock_pkt(fsp); 782 fc_fcp_unlock_pkt(fsp);
743out: 783out:
744 fc_frame_free(fp); 784 fc_frame_free(fp);
745errout:
746 if (IS_ERR(fp))
747 fc_fcp_error(fsp, fp);
748 else if (rc == -ENOMEM)
749 fc_fcp_reduce_can_queue(lport);
750} 785}
751 786
787/**
788 * fc_fcp_resp() - Handler for FCP responses
789 * @fsp: The FCP packet the response is for
790 * @fp: The response frame
791 */
752static void fc_fcp_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp) 792static void fc_fcp_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
753{ 793{
754 struct fc_frame_header *fh; 794 struct fc_frame_header *fh;
@@ -862,15 +902,16 @@ err:
862} 902}
863 903
864/** 904/**
865 * fc_fcp_complete_locked() - complete processing of a fcp packet 905 * fc_fcp_complete_locked() - Complete processing of a fcp_pkt with the
866 * @fsp: fcp packet 906 * fcp_pkt lock held
907 * @fsp: The FCP packet to be completed
867 * 908 *
868 * This function may sleep if a timer is pending. The packet lock must be 909 * This function may sleep if a timer is pending. The packet lock must be
869 * held, and the host lock must not be held. 910 * held, and the host lock must not be held.
870 */ 911 */
871static void fc_fcp_complete_locked(struct fc_fcp_pkt *fsp) 912static void fc_fcp_complete_locked(struct fc_fcp_pkt *fsp)
872{ 913{
873 struct fc_lport *lp = fsp->lp; 914 struct fc_lport *lport = fsp->lp;
874 struct fc_seq *seq; 915 struct fc_seq *seq;
875 struct fc_exch *ep; 916 struct fc_exch *ep;
876 u32 f_ctl; 917 u32 f_ctl;
@@ -901,8 +942,8 @@ static void fc_fcp_complete_locked(struct fc_fcp_pkt *fsp)
901 struct fc_frame *conf_frame; 942 struct fc_frame *conf_frame;
902 struct fc_seq *csp; 943 struct fc_seq *csp;
903 944
904 csp = lp->tt.seq_start_next(seq); 945 csp = lport->tt.seq_start_next(seq);
905 conf_frame = fc_frame_alloc(fsp->lp, 0); 946 conf_frame = fc_fcp_frame_alloc(fsp->lp, 0);
906 if (conf_frame) { 947 if (conf_frame) {
907 f_ctl = FC_FC_SEQ_INIT; 948 f_ctl = FC_FC_SEQ_INIT;
908 f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ; 949 f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
@@ -910,43 +951,48 @@ static void fc_fcp_complete_locked(struct fc_fcp_pkt *fsp)
910 fc_fill_fc_hdr(conf_frame, FC_RCTL_DD_SOL_CTL, 951 fc_fill_fc_hdr(conf_frame, FC_RCTL_DD_SOL_CTL,
911 ep->did, ep->sid, 952 ep->did, ep->sid,
912 FC_TYPE_FCP, f_ctl, 0); 953 FC_TYPE_FCP, f_ctl, 0);
913 lp->tt.seq_send(lp, csp, conf_frame); 954 lport->tt.seq_send(lport, csp, conf_frame);
914 } 955 }
915 } 956 }
916 lp->tt.exch_done(seq); 957 lport->tt.exch_done(seq);
917 } 958 }
918 fc_io_compl(fsp); 959 fc_io_compl(fsp);
919} 960}
920 961
962/**
963 * fc_fcp_cleanup_cmd() - Cancel the active exchange on a fcp_pkt
964 * @fsp: The FCP packet whose exchanges should be canceled
965 * @error: The reason for the cancellation
966 */
921static void fc_fcp_cleanup_cmd(struct fc_fcp_pkt *fsp, int error) 967static void fc_fcp_cleanup_cmd(struct fc_fcp_pkt *fsp, int error)
922{ 968{
923 struct fc_lport *lp = fsp->lp; 969 struct fc_lport *lport = fsp->lp;
924 970
925 if (fsp->seq_ptr) { 971 if (fsp->seq_ptr) {
926 lp->tt.exch_done(fsp->seq_ptr); 972 lport->tt.exch_done(fsp->seq_ptr);
927 fsp->seq_ptr = NULL; 973 fsp->seq_ptr = NULL;
928 } 974 }
929 fsp->status_code = error; 975 fsp->status_code = error;
930} 976}
931 977
932/** 978/**
933 * fc_fcp_cleanup_each_cmd() - Cleanup active commads 979 * fc_fcp_cleanup_each_cmd() - Cancel all exchanges on a local port
934 * @lp: logical port 980 * @lport: The local port whose exchanges should be canceled
935 * @id: target id 981 * @id: The target's ID
936 * @lun: lun 982 * @lun: The LUN
937 * @error: fsp status code 983 * @error: The reason for cancellation
938 * 984 *
939 * If lun or id is -1, they are ignored. 985 * If lun or id is -1, they are ignored.
940 */ 986 */
941static void fc_fcp_cleanup_each_cmd(struct fc_lport *lp, unsigned int id, 987static void fc_fcp_cleanup_each_cmd(struct fc_lport *lport, unsigned int id,
942 unsigned int lun, int error) 988 unsigned int lun, int error)
943{ 989{
944 struct fc_fcp_internal *si = fc_get_scsi_internal(lp); 990 struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
945 struct fc_fcp_pkt *fsp; 991 struct fc_fcp_pkt *fsp;
946 struct scsi_cmnd *sc_cmd; 992 struct scsi_cmnd *sc_cmd;
947 unsigned long flags; 993 unsigned long flags;
948 994
949 spin_lock_irqsave(lp->host->host_lock, flags); 995 spin_lock_irqsave(&si->scsi_queue_lock, flags);
950restart: 996restart:
951 list_for_each_entry(fsp, &si->scsi_pkt_queue, list) { 997 list_for_each_entry(fsp, &si->scsi_pkt_queue, list) {
952 sc_cmd = fsp->cmd; 998 sc_cmd = fsp->cmd;
@@ -957,7 +1003,7 @@ restart:
957 continue; 1003 continue;
958 1004
959 fc_fcp_pkt_hold(fsp); 1005 fc_fcp_pkt_hold(fsp);
960 spin_unlock_irqrestore(lp->host->host_lock, flags); 1006 spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
961 1007
962 if (!fc_fcp_lock_pkt(fsp)) { 1008 if (!fc_fcp_lock_pkt(fsp)) {
963 fc_fcp_cleanup_cmd(fsp, error); 1009 fc_fcp_cleanup_cmd(fsp, error);
@@ -966,35 +1012,37 @@ restart:
966 } 1012 }
967 1013
968 fc_fcp_pkt_release(fsp); 1014 fc_fcp_pkt_release(fsp);
969 spin_lock_irqsave(lp->host->host_lock, flags); 1015 spin_lock_irqsave(&si->scsi_queue_lock, flags);
970 /* 1016 /*
971 * while we dropped the lock multiple pkts could 1017 * while we dropped the lock multiple pkts could
972 * have been released, so we have to start over. 1018 * have been released, so we have to start over.
973 */ 1019 */
974 goto restart; 1020 goto restart;
975 } 1021 }
976 spin_unlock_irqrestore(lp->host->host_lock, flags); 1022 spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
977} 1023}
978 1024
979static void fc_fcp_abort_io(struct fc_lport *lp) 1025/**
1026 * fc_fcp_abort_io() - Abort all FCP-SCSI exchanges on a local port
1027 * @lport: The local port whose exchanges are to be aborted
1028 */
1029static void fc_fcp_abort_io(struct fc_lport *lport)
980{ 1030{
981 fc_fcp_cleanup_each_cmd(lp, -1, -1, FC_HRD_ERROR); 1031 fc_fcp_cleanup_each_cmd(lport, -1, -1, FC_HRD_ERROR);
982} 1032}
983 1033
984/** 1034/**
985 * fc_fcp_pkt_send() - send a fcp packet to the lower level. 1035 * fc_fcp_pkt_send() - Send a fcp_pkt
986 * @lp: fc lport 1036 * @lport: The local port to send the FCP packet on
987 * @fsp: fc packet. 1037 * @fsp: The FCP packet to send
988 * 1038 *
989 * This is called by upper layer protocol. 1039 * Return: Zero for success and -1 for failure
990 * Return : zero for success and -1 for failure 1040 * Locks: Called without locks held
991 * Context : called from queuecommand which can be called from process
992 * or scsi soft irq.
993 * Locks : called with the host lock and irqs disabled.
994 */ 1041 */
995static int fc_fcp_pkt_send(struct fc_lport *lp, struct fc_fcp_pkt *fsp) 1042static int fc_fcp_pkt_send(struct fc_lport *lport, struct fc_fcp_pkt *fsp)
996{ 1043{
997 struct fc_fcp_internal *si = fc_get_scsi_internal(lp); 1044 struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
1045 unsigned long flags;
998 int rc; 1046 int rc;
999 1047
1000 fsp->cmd->SCp.ptr = (char *)fsp; 1048 fsp->cmd->SCp.ptr = (char *)fsp;
@@ -1004,18 +1052,27 @@ static int fc_fcp_pkt_send(struct fc_lport *lp, struct fc_fcp_pkt *fsp)
1004 int_to_scsilun(fsp->cmd->device->lun, 1052 int_to_scsilun(fsp->cmd->device->lun,
1005 (struct scsi_lun *)fsp->cdb_cmd.fc_lun); 1053 (struct scsi_lun *)fsp->cdb_cmd.fc_lun);
1006 memcpy(fsp->cdb_cmd.fc_cdb, fsp->cmd->cmnd, fsp->cmd->cmd_len); 1054 memcpy(fsp->cdb_cmd.fc_cdb, fsp->cmd->cmnd, fsp->cmd->cmd_len);
1007 list_add_tail(&fsp->list, &si->scsi_pkt_queue);
1008 1055
1009 spin_unlock_irq(lp->host->host_lock); 1056 spin_lock_irqsave(&si->scsi_queue_lock, flags);
1010 rc = lp->tt.fcp_cmd_send(lp, fsp, fc_fcp_recv); 1057 list_add_tail(&fsp->list, &si->scsi_pkt_queue);
1011 spin_lock_irq(lp->host->host_lock); 1058 spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
1012 if (rc) 1059 rc = lport->tt.fcp_cmd_send(lport, fsp, fc_fcp_recv);
1060 if (unlikely(rc)) {
1061 spin_lock_irqsave(&si->scsi_queue_lock, flags);
1013 list_del(&fsp->list); 1062 list_del(&fsp->list);
1063 spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
1064 }
1014 1065
1015 return rc; 1066 return rc;
1016} 1067}
1017 1068
1018static int fc_fcp_cmd_send(struct fc_lport *lp, struct fc_fcp_pkt *fsp, 1069/**
1070 * fc_fcp_cmd_send() - Send a FCP command
1071 * @lport: The local port to send the command on
1072 * @fsp: The FCP packet the command is on
1073 * @resp: The handler for the response
1074 */
1075static int fc_fcp_cmd_send(struct fc_lport *lport, struct fc_fcp_pkt *fsp,
1019 void (*resp)(struct fc_seq *, 1076 void (*resp)(struct fc_seq *,
1020 struct fc_frame *fp, 1077 struct fc_frame *fp,
1021 void *arg)) 1078 void *arg))
@@ -1023,14 +1080,14 @@ static int fc_fcp_cmd_send(struct fc_lport *lp, struct fc_fcp_pkt *fsp,
1023 struct fc_frame *fp; 1080 struct fc_frame *fp;
1024 struct fc_seq *seq; 1081 struct fc_seq *seq;
1025 struct fc_rport *rport; 1082 struct fc_rport *rport;
1026 struct fc_rport_libfc_priv *rp; 1083 struct fc_rport_libfc_priv *rpriv;
1027 const size_t len = sizeof(fsp->cdb_cmd); 1084 const size_t len = sizeof(fsp->cdb_cmd);
1028 int rc = 0; 1085 int rc = 0;
1029 1086
1030 if (fc_fcp_lock_pkt(fsp)) 1087 if (fc_fcp_lock_pkt(fsp))
1031 return 0; 1088 return 0;
1032 1089
1033 fp = fc_frame_alloc(lp, sizeof(fsp->cdb_cmd)); 1090 fp = fc_fcp_frame_alloc(lport, sizeof(fsp->cdb_cmd));
1034 if (!fp) { 1091 if (!fp) {
1035 rc = -1; 1092 rc = -1;
1036 goto unlock; 1093 goto unlock;
@@ -1040,15 +1097,15 @@ static int fc_fcp_cmd_send(struct fc_lport *lp, struct fc_fcp_pkt *fsp,
1040 fr_fsp(fp) = fsp; 1097 fr_fsp(fp) = fsp;
1041 rport = fsp->rport; 1098 rport = fsp->rport;
1042 fsp->max_payload = rport->maxframe_size; 1099 fsp->max_payload = rport->maxframe_size;
1043 rp = rport->dd_data; 1100 rpriv = rport->dd_data;
1044 1101
1045 fc_fill_fc_hdr(fp, FC_RCTL_DD_UNSOL_CMD, rport->port_id, 1102 fc_fill_fc_hdr(fp, FC_RCTL_DD_UNSOL_CMD, rport->port_id,
1046 fc_host_port_id(rp->local_port->host), FC_TYPE_FCP, 1103 fc_host_port_id(rpriv->local_port->host), FC_TYPE_FCP,
1047 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0); 1104 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1048 1105
1049 seq = lp->tt.exch_seq_send(lp, fp, resp, fc_fcp_pkt_destroy, fsp, 0); 1106 seq = lport->tt.exch_seq_send(lport, fp, resp, fc_fcp_pkt_destroy,
1107 fsp, 0);
1050 if (!seq) { 1108 if (!seq) {
1051 fc_frame_free(fp);
1052 rc = -1; 1109 rc = -1;
1053 goto unlock; 1110 goto unlock;
1054 } 1111 }
@@ -1065,8 +1122,10 @@ unlock:
1065 return rc; 1122 return rc;
1066} 1123}
1067 1124
1068/* 1125/**
1069 * transport error handler 1126 * fc_fcp_error() - Handler for FCP layer errors
1127 * @fsp: The FCP packet the error is on
1128 * @fp: The frame that has errored
1070 */ 1129 */
1071static void fc_fcp_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp) 1130static void fc_fcp_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
1072{ 1131{
@@ -1091,11 +1150,13 @@ unlock:
1091 fc_fcp_unlock_pkt(fsp); 1150 fc_fcp_unlock_pkt(fsp);
1092} 1151}
1093 1152
1094/* 1153/**
1095 * Scsi abort handler- calls to send an abort 1154 * fc_fcp_pkt_abort() - Abort a fcp_pkt
1096 * and then wait for abort completion 1155 * @fsp: The FCP packet to abort on
1156 *
1157 * Called to send an abort and then wait for abort completion
1097 */ 1158 */
1098static int fc_fcp_pkt_abort(struct fc_lport *lp, struct fc_fcp_pkt *fsp) 1159static int fc_fcp_pkt_abort(struct fc_fcp_pkt *fsp)
1099{ 1160{
1100 int rc = FAILED; 1161 int rc = FAILED;
1101 1162
@@ -1122,14 +1183,15 @@ static int fc_fcp_pkt_abort(struct fc_lport *lp, struct fc_fcp_pkt *fsp)
1122 return rc; 1183 return rc;
1123} 1184}
1124 1185
1125/* 1186/**
1126 * Retry LUN reset after resource allocation failed. 1187 * fc_lun_reset_send() - Send LUN reset command
1188 * @data: The FCP packet that identifies the LUN to be reset
1127 */ 1189 */
1128static void fc_lun_reset_send(unsigned long data) 1190static void fc_lun_reset_send(unsigned long data)
1129{ 1191{
1130 struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data; 1192 struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
1131 struct fc_lport *lp = fsp->lp; 1193 struct fc_lport *lport = fsp->lp;
1132 if (lp->tt.fcp_cmd_send(lp, fsp, fc_tm_done)) { 1194 if (lport->tt.fcp_cmd_send(lport, fsp, fc_tm_done)) {
1133 if (fsp->recov_retry++ >= FC_MAX_RECOV_RETRY) 1195 if (fsp->recov_retry++ >= FC_MAX_RECOV_RETRY)
1134 return; 1196 return;
1135 if (fc_fcp_lock_pkt(fsp)) 1197 if (fc_fcp_lock_pkt(fsp))
@@ -1140,11 +1202,15 @@ static void fc_lun_reset_send(unsigned long data)
1140 } 1202 }
1141} 1203}
1142 1204
1143/* 1205/**
1144 * Scsi device reset handler- send a LUN RESET to the device 1206 * fc_lun_reset() - Send a LUN RESET command to a device
1145 * and wait for reset reply 1207 * and wait for the reply
1208 * @lport: The local port to sent the comand on
1209 * @fsp: The FCP packet that identifies the LUN to be reset
1210 * @id: The SCSI command ID
1211 * @lun: The LUN ID to be reset
1146 */ 1212 */
1147static int fc_lun_reset(struct fc_lport *lp, struct fc_fcp_pkt *fsp, 1213static int fc_lun_reset(struct fc_lport *lport, struct fc_fcp_pkt *fsp,
1148 unsigned int id, unsigned int lun) 1214 unsigned int id, unsigned int lun)
1149{ 1215{
1150 int rc; 1216 int rc;
@@ -1172,14 +1238,14 @@ static int fc_lun_reset(struct fc_lport *lp, struct fc_fcp_pkt *fsp,
1172 1238
1173 spin_lock_bh(&fsp->scsi_pkt_lock); 1239 spin_lock_bh(&fsp->scsi_pkt_lock);
1174 if (fsp->seq_ptr) { 1240 if (fsp->seq_ptr) {
1175 lp->tt.exch_done(fsp->seq_ptr); 1241 lport->tt.exch_done(fsp->seq_ptr);
1176 fsp->seq_ptr = NULL; 1242 fsp->seq_ptr = NULL;
1177 } 1243 }
1178 fsp->wait_for_comp = 0; 1244 fsp->wait_for_comp = 0;
1179 spin_unlock_bh(&fsp->scsi_pkt_lock); 1245 spin_unlock_bh(&fsp->scsi_pkt_lock);
1180 1246
1181 if (!rc) { 1247 if (!rc) {
1182 FC_SCSI_DBG(lp, "lun reset failed\n"); 1248 FC_SCSI_DBG(lport, "lun reset failed\n");
1183 return FAILED; 1249 return FAILED;
1184 } 1250 }
1185 1251
@@ -1187,13 +1253,16 @@ static int fc_lun_reset(struct fc_lport *lp, struct fc_fcp_pkt *fsp,
1187 if (fsp->cdb_status != FCP_TMF_CMPL) 1253 if (fsp->cdb_status != FCP_TMF_CMPL)
1188 return FAILED; 1254 return FAILED;
1189 1255
1190 FC_SCSI_DBG(lp, "lun reset to lun %u completed\n", lun); 1256 FC_SCSI_DBG(lport, "lun reset to lun %u completed\n", lun);
1191 fc_fcp_cleanup_each_cmd(lp, id, lun, FC_CMD_ABORTED); 1257 fc_fcp_cleanup_each_cmd(lport, id, lun, FC_CMD_ABORTED);
1192 return SUCCESS; 1258 return SUCCESS;
1193} 1259}
1194 1260
1195/* 1261/**
1196 * Task Managment response handler 1262 * fc_tm_done() - Task Managment response handler
1263 * @seq: The sequence that the response is on
1264 * @fp: The response frame
1265 * @arg: The FCP packet the response is for
1197 */ 1266 */
1198static void fc_tm_done(struct fc_seq *seq, struct fc_frame *fp, void *arg) 1267static void fc_tm_done(struct fc_seq *seq, struct fc_frame *fp, void *arg)
1199{ 1268{
@@ -1230,34 +1299,31 @@ static void fc_tm_done(struct fc_seq *seq, struct fc_frame *fp, void *arg)
1230 fc_fcp_unlock_pkt(fsp); 1299 fc_fcp_unlock_pkt(fsp);
1231} 1300}
1232 1301
1233static void fc_fcp_cleanup(struct fc_lport *lp) 1302/**
1303 * fc_fcp_cleanup() - Cleanup all FCP exchanges on a local port
1304 * @lport: The local port to be cleaned up
1305 */
1306static void fc_fcp_cleanup(struct fc_lport *lport)
1234{ 1307{
1235 fc_fcp_cleanup_each_cmd(lp, -1, -1, FC_ERROR); 1308 fc_fcp_cleanup_each_cmd(lport, -1, -1, FC_ERROR);
1236} 1309}
1237 1310
1238/* 1311/**
1239 * fc_fcp_timeout: called by OS timer function. 1312 * fc_fcp_timeout() - Handler for fcp_pkt timeouts
1240 * 1313 * @data: The FCP packet that has timed out
1241 * The timer has been inactivated and must be reactivated if desired
1242 * using fc_fcp_timer_set().
1243 *
1244 * Algorithm:
1245 *
1246 * If REC is supported, just issue it, and return. The REC exchange will
1247 * complete or time out, and recovery can continue at that point.
1248 *
1249 * Otherwise, if the response has been received without all the data,
1250 * it has been ER_TIMEOUT since the response was received.
1251 * 1314 *
1252 * If the response has not been received, 1315 * If REC is supported then just issue it and return. The REC exchange will
1253 * we see if data was received recently. If it has been, we continue waiting, 1316 * complete or time out and recovery can continue at that point. Otherwise,
1254 * otherwise, we abort the command. 1317 * if the response has been received without all the data it has been
1318 * ER_TIMEOUT since the response was received. If the response has not been
1319 * received we see if data was received recently. If it has been then we
1320 * continue waiting, otherwise, we abort the command.
1255 */ 1321 */
1256static void fc_fcp_timeout(unsigned long data) 1322static void fc_fcp_timeout(unsigned long data)
1257{ 1323{
1258 struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data; 1324 struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
1259 struct fc_rport *rport = fsp->rport; 1325 struct fc_rport *rport = fsp->rport;
1260 struct fc_rport_libfc_priv *rp = rport->dd_data; 1326 struct fc_rport_libfc_priv *rpriv = rport->dd_data;
1261 1327
1262 if (fc_fcp_lock_pkt(fsp)) 1328 if (fc_fcp_lock_pkt(fsp))
1263 return; 1329 return;
@@ -1267,7 +1333,7 @@ static void fc_fcp_timeout(unsigned long data)
1267 1333
1268 fsp->state |= FC_SRB_FCP_PROCESSING_TMO; 1334 fsp->state |= FC_SRB_FCP_PROCESSING_TMO;
1269 1335
1270 if (rp->flags & FC_RP_FLAGS_REC_SUPPORTED) 1336 if (rpriv->flags & FC_RP_FLAGS_REC_SUPPORTED)
1271 fc_fcp_rec(fsp); 1337 fc_fcp_rec(fsp);
1272 else if (time_after_eq(fsp->last_pkt_time + (FC_SCSI_ER_TIMEOUT / 2), 1338 else if (time_after_eq(fsp->last_pkt_time + (FC_SCSI_ER_TIMEOUT / 2),
1273 jiffies)) 1339 jiffies))
@@ -1281,39 +1347,40 @@ unlock:
1281 fc_fcp_unlock_pkt(fsp); 1347 fc_fcp_unlock_pkt(fsp);
1282} 1348}
1283 1349
1284/* 1350/**
1285 * Send a REC ELS request 1351 * fc_fcp_rec() - Send a REC ELS request
1352 * @fsp: The FCP packet to send the REC request on
1286 */ 1353 */
1287static void fc_fcp_rec(struct fc_fcp_pkt *fsp) 1354static void fc_fcp_rec(struct fc_fcp_pkt *fsp)
1288{ 1355{
1289 struct fc_lport *lp; 1356 struct fc_lport *lport;
1290 struct fc_frame *fp; 1357 struct fc_frame *fp;
1291 struct fc_rport *rport; 1358 struct fc_rport *rport;
1292 struct fc_rport_libfc_priv *rp; 1359 struct fc_rport_libfc_priv *rpriv;
1293 1360
1294 lp = fsp->lp; 1361 lport = fsp->lp;
1295 rport = fsp->rport; 1362 rport = fsp->rport;
1296 rp = rport->dd_data; 1363 rpriv = rport->dd_data;
1297 if (!fsp->seq_ptr || rp->rp_state != RPORT_ST_READY) { 1364 if (!fsp->seq_ptr || rpriv->rp_state != RPORT_ST_READY) {
1298 fsp->status_code = FC_HRD_ERROR; 1365 fsp->status_code = FC_HRD_ERROR;
1299 fsp->io_status = 0; 1366 fsp->io_status = 0;
1300 fc_fcp_complete_locked(fsp); 1367 fc_fcp_complete_locked(fsp);
1301 return; 1368 return;
1302 } 1369 }
1303 fp = fc_frame_alloc(lp, sizeof(struct fc_els_rec)); 1370 fp = fc_fcp_frame_alloc(lport, sizeof(struct fc_els_rec));
1304 if (!fp) 1371 if (!fp)
1305 goto retry; 1372 goto retry;
1306 1373
1307 fr_seq(fp) = fsp->seq_ptr; 1374 fr_seq(fp) = fsp->seq_ptr;
1308 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rport->port_id, 1375 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rport->port_id,
1309 fc_host_port_id(rp->local_port->host), FC_TYPE_ELS, 1376 fc_host_port_id(rpriv->local_port->host), FC_TYPE_ELS,
1310 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0); 1377 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1311 if (lp->tt.elsct_send(lp, rport->port_id, fp, ELS_REC, fc_fcp_rec_resp, 1378 if (lport->tt.elsct_send(lport, rport->port_id, fp, ELS_REC,
1312 fsp, jiffies_to_msecs(FC_SCSI_REC_TOV))) { 1379 fc_fcp_rec_resp, fsp,
1380 jiffies_to_msecs(FC_SCSI_REC_TOV))) {
1313 fc_fcp_pkt_hold(fsp); /* hold while REC outstanding */ 1381 fc_fcp_pkt_hold(fsp); /* hold while REC outstanding */
1314 return; 1382 return;
1315 } 1383 }
1316 fc_frame_free(fp);
1317retry: 1384retry:
1318 if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY) 1385 if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
1319 fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV); 1386 fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
@@ -1321,12 +1388,16 @@ retry:
1321 fc_timeout_error(fsp); 1388 fc_timeout_error(fsp);
1322} 1389}
1323 1390
1324/* 1391/**
1325 * Receive handler for REC ELS frame 1392 * fc_fcp_rec_resp() - Handler for REC ELS responses
1326 * if it is a reject then let the scsi layer to handle 1393 * @seq: The sequence the response is on
1327 * the timeout. if it is a LS_ACC then if the io was not completed 1394 * @fp: The response frame
1328 * then set the timeout and return otherwise complete the exchange 1395 * @arg: The FCP packet the response is on
1329 * and tell the scsi layer to restart the I/O. 1396 *
1397 * If the response is a reject then the scsi layer will handle
1398 * the timeout. If the response is a LS_ACC then if the I/O was not completed
1399 * set the timeout and return. If the I/O was completed then complete the
1400 * exchange and tell the SCSI layer.
1330 */ 1401 */
1331static void fc_fcp_rec_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg) 1402static void fc_fcp_rec_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
1332{ 1403{
@@ -1338,7 +1409,7 @@ static void fc_fcp_rec_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
1338 u32 offset; 1409 u32 offset;
1339 enum dma_data_direction data_dir; 1410 enum dma_data_direction data_dir;
1340 enum fc_rctl r_ctl; 1411 enum fc_rctl r_ctl;
1341 struct fc_rport_libfc_priv *rp; 1412 struct fc_rport_libfc_priv *rpriv;
1342 1413
1343 if (IS_ERR(fp)) { 1414 if (IS_ERR(fp)) {
1344 fc_fcp_rec_error(fsp, fp); 1415 fc_fcp_rec_error(fsp, fp);
@@ -1361,13 +1432,13 @@ static void fc_fcp_rec_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
1361 /* fall through */ 1432 /* fall through */
1362 case ELS_RJT_UNSUP: 1433 case ELS_RJT_UNSUP:
1363 FC_FCP_DBG(fsp, "device does not support REC\n"); 1434 FC_FCP_DBG(fsp, "device does not support REC\n");
1364 rp = fsp->rport->dd_data; 1435 rpriv = fsp->rport->dd_data;
1365 /* 1436 /*
1366 * if we do not spport RECs or got some bogus 1437 * if we do not spport RECs or got some bogus
1367 * reason then resetup timer so we check for 1438 * reason then resetup timer so we check for
1368 * making progress. 1439 * making progress.
1369 */ 1440 */
1370 rp->flags &= ~FC_RP_FLAGS_REC_SUPPORTED; 1441 rpriv->flags &= ~FC_RP_FLAGS_REC_SUPPORTED;
1371 fc_fcp_timer_set(fsp, FC_SCSI_ER_TIMEOUT); 1442 fc_fcp_timer_set(fsp, FC_SCSI_ER_TIMEOUT);
1372 break; 1443 break;
1373 case ELS_RJT_LOGIC: 1444 case ELS_RJT_LOGIC:
@@ -1464,8 +1535,10 @@ out:
1464 fc_frame_free(fp); 1535 fc_frame_free(fp);
1465} 1536}
1466 1537
1467/* 1538/**
1468 * Handle error response or timeout for REC exchange. 1539 * fc_fcp_rec_error() - Handler for REC errors
1540 * @fsp: The FCP packet the error is on
1541 * @fp: The REC frame
1469 */ 1542 */
1470static void fc_fcp_rec_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp) 1543static void fc_fcp_rec_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
1471{ 1544{
@@ -1504,10 +1577,9 @@ out:
1504 fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */ 1577 fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
1505} 1578}
1506 1579
1507/* 1580/**
1508 * Time out error routine: 1581 * fc_timeout_error() - Handler for fcp_pkt timeouts
1509 * abort's the I/O close the exchange and 1582 * @fsp: The FCP packt that has timed out
1510 * send completion notification to scsi layer
1511 */ 1583 */
1512static void fc_timeout_error(struct fc_fcp_pkt *fsp) 1584static void fc_timeout_error(struct fc_fcp_pkt *fsp)
1513{ 1585{
@@ -1521,16 +1593,18 @@ static void fc_timeout_error(struct fc_fcp_pkt *fsp)
1521 fc_fcp_send_abort(fsp); 1593 fc_fcp_send_abort(fsp);
1522} 1594}
1523 1595
1524/* 1596/**
1525 * Sequence retransmission request. 1597 * fc_fcp_srr() - Send a SRR request (Sequence Retransmission Request)
1598 * @fsp: The FCP packet the SRR is to be sent on
1599 * @r_ctl: The R_CTL field for the SRR request
1526 * This is called after receiving status but insufficient data, or 1600 * This is called after receiving status but insufficient data, or
1527 * when expecting status but the request has timed out. 1601 * when expecting status but the request has timed out.
1528 */ 1602 */
1529static void fc_fcp_srr(struct fc_fcp_pkt *fsp, enum fc_rctl r_ctl, u32 offset) 1603static void fc_fcp_srr(struct fc_fcp_pkt *fsp, enum fc_rctl r_ctl, u32 offset)
1530{ 1604{
1531 struct fc_lport *lp = fsp->lp; 1605 struct fc_lport *lport = fsp->lp;
1532 struct fc_rport *rport; 1606 struct fc_rport *rport;
1533 struct fc_rport_libfc_priv *rp; 1607 struct fc_rport_libfc_priv *rpriv;
1534 struct fc_exch *ep = fc_seq_exch(fsp->seq_ptr); 1608 struct fc_exch *ep = fc_seq_exch(fsp->seq_ptr);
1535 struct fc_seq *seq; 1609 struct fc_seq *seq;
1536 struct fcp_srr *srr; 1610 struct fcp_srr *srr;
@@ -1538,12 +1612,13 @@ static void fc_fcp_srr(struct fc_fcp_pkt *fsp, enum fc_rctl r_ctl, u32 offset)
1538 u8 cdb_op; 1612 u8 cdb_op;
1539 1613
1540 rport = fsp->rport; 1614 rport = fsp->rport;
1541 rp = rport->dd_data; 1615 rpriv = rport->dd_data;
1542 cdb_op = fsp->cdb_cmd.fc_cdb[0]; 1616 cdb_op = fsp->cdb_cmd.fc_cdb[0];
1543 1617
1544 if (!(rp->flags & FC_RP_FLAGS_RETRY) || rp->rp_state != RPORT_ST_READY) 1618 if (!(rpriv->flags & FC_RP_FLAGS_RETRY) ||
1619 rpriv->rp_state != RPORT_ST_READY)
1545 goto retry; /* shouldn't happen */ 1620 goto retry; /* shouldn't happen */
1546 fp = fc_frame_alloc(lp, sizeof(*srr)); 1621 fp = fc_fcp_frame_alloc(lport, sizeof(*srr));
1547 if (!fp) 1622 if (!fp)
1548 goto retry; 1623 goto retry;
1549 1624
@@ -1556,15 +1631,14 @@ static void fc_fcp_srr(struct fc_fcp_pkt *fsp, enum fc_rctl r_ctl, u32 offset)
1556 srr->srr_rel_off = htonl(offset); 1631 srr->srr_rel_off = htonl(offset);
1557 1632
1558 fc_fill_fc_hdr(fp, FC_RCTL_ELS4_REQ, rport->port_id, 1633 fc_fill_fc_hdr(fp, FC_RCTL_ELS4_REQ, rport->port_id,
1559 fc_host_port_id(rp->local_port->host), FC_TYPE_FCP, 1634 fc_host_port_id(rpriv->local_port->host), FC_TYPE_FCP,
1560 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0); 1635 FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1561 1636
1562 seq = lp->tt.exch_seq_send(lp, fp, fc_fcp_srr_resp, NULL, 1637 seq = lport->tt.exch_seq_send(lport, fp, fc_fcp_srr_resp, NULL,
1563 fsp, jiffies_to_msecs(FC_SCSI_REC_TOV)); 1638 fsp, jiffies_to_msecs(FC_SCSI_REC_TOV));
1564 if (!seq) { 1639 if (!seq)
1565 fc_frame_free(fp);
1566 goto retry; 1640 goto retry;
1567 } 1641
1568 fsp->recov_seq = seq; 1642 fsp->recov_seq = seq;
1569 fsp->xfer_len = offset; 1643 fsp->xfer_len = offset;
1570 fsp->xfer_contig_end = offset; 1644 fsp->xfer_contig_end = offset;
@@ -1575,8 +1649,11 @@ retry:
1575 fc_fcp_retry_cmd(fsp); 1649 fc_fcp_retry_cmd(fsp);
1576} 1650}
1577 1651
1578/* 1652/**
1579 * Handle response from SRR. 1653 * fc_fcp_srr_resp() - Handler for SRR response
1654 * @seq: The sequence the SRR is on
1655 * @fp: The SRR frame
1656 * @arg: The FCP packet the SRR is on
1580 */ 1657 */
1581static void fc_fcp_srr_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg) 1658static void fc_fcp_srr_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
1582{ 1659{
@@ -1622,6 +1699,11 @@ out:
1622 fc_fcp_pkt_release(fsp); /* drop hold for outstanding SRR */ 1699 fc_fcp_pkt_release(fsp); /* drop hold for outstanding SRR */
1623} 1700}
1624 1701
1702/**
1703 * fc_fcp_srr_error() - Handler for SRR errors
1704 * @fsp: The FCP packet that the SRR error is on
1705 * @fp: The SRR frame
1706 */
1625static void fc_fcp_srr_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp) 1707static void fc_fcp_srr_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
1626{ 1708{
1627 if (fc_fcp_lock_pkt(fsp)) 1709 if (fc_fcp_lock_pkt(fsp))
@@ -1646,31 +1728,37 @@ out:
1646 fc_fcp_pkt_release(fsp); /* drop hold for outstanding SRR */ 1728 fc_fcp_pkt_release(fsp); /* drop hold for outstanding SRR */
1647} 1729}
1648 1730
1649static inline int fc_fcp_lport_queue_ready(struct fc_lport *lp) 1731/**
1732 * fc_fcp_lport_queue_ready() - Determine if the lport and it's queue is ready
1733 * @lport: The local port to be checked
1734 */
1735static inline int fc_fcp_lport_queue_ready(struct fc_lport *lport)
1650{ 1736{
1651 /* lock ? */ 1737 /* lock ? */
1652 return (lp->state == LPORT_ST_READY) && lp->link_up && !lp->qfull; 1738 return (lport->state == LPORT_ST_READY) &&
1739 lport->link_up && !lport->qfull;
1653} 1740}
1654 1741
1655/** 1742/**
1656 * fc_queuecommand - The queuecommand function of the scsi template 1743 * fc_queuecommand() - The queuecommand function of the SCSI template
1657 * @cmd: struct scsi_cmnd to be executed 1744 * @cmd: The scsi_cmnd to be executed
1658 * @done: Callback function to be called when cmd is completed 1745 * @done: The callback function to be called when the scsi_cmnd is complete
1659 * 1746 *
1660 * this is the i/o strategy routine, called by the scsi layer 1747 * This is the i/o strategy routine, called by the SCSI layer. This routine
1661 * this routine is called with holding the host_lock. 1748 * is called with the host_lock held.
1662 */ 1749 */
1663int fc_queuecommand(struct scsi_cmnd *sc_cmd, void (*done)(struct scsi_cmnd *)) 1750int fc_queuecommand(struct scsi_cmnd *sc_cmd, void (*done)(struct scsi_cmnd *))
1664{ 1751{
1665 struct fc_lport *lp; 1752 struct fc_lport *lport;
1666 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device)); 1753 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
1667 struct fc_fcp_pkt *fsp; 1754 struct fc_fcp_pkt *fsp;
1668 struct fc_rport_libfc_priv *rp; 1755 struct fc_rport_libfc_priv *rpriv;
1669 int rval; 1756 int rval;
1670 int rc = 0; 1757 int rc = 0;
1671 struct fcoe_dev_stats *stats; 1758 struct fcoe_dev_stats *stats;
1672 1759
1673 lp = shost_priv(sc_cmd->device->host); 1760 lport = shost_priv(sc_cmd->device->host);
1761 spin_unlock_irq(lport->host->host_lock);
1674 1762
1675 rval = fc_remote_port_chkready(rport); 1763 rval = fc_remote_port_chkready(rport);
1676 if (rval) { 1764 if (rval) {
@@ -1689,14 +1777,16 @@ int fc_queuecommand(struct scsi_cmnd *sc_cmd, void (*done)(struct scsi_cmnd *))
1689 goto out; 1777 goto out;
1690 } 1778 }
1691 1779
1692 rp = rport->dd_data; 1780 rpriv = rport->dd_data;
1693 1781
1694 if (!fc_fcp_lport_queue_ready(lp)) { 1782 if (!fc_fcp_lport_queue_ready(lport)) {
1783 if (lport->qfull)
1784 fc_fcp_can_queue_ramp_down(lport);
1695 rc = SCSI_MLQUEUE_HOST_BUSY; 1785 rc = SCSI_MLQUEUE_HOST_BUSY;
1696 goto out; 1786 goto out;
1697 } 1787 }
1698 1788
1699 fsp = fc_fcp_pkt_alloc(lp, GFP_ATOMIC); 1789 fsp = fc_fcp_pkt_alloc(lport, GFP_ATOMIC);
1700 if (fsp == NULL) { 1790 if (fsp == NULL) {
1701 rc = SCSI_MLQUEUE_HOST_BUSY; 1791 rc = SCSI_MLQUEUE_HOST_BUSY;
1702 goto out; 1792 goto out;
@@ -1706,8 +1796,9 @@ int fc_queuecommand(struct scsi_cmnd *sc_cmd, void (*done)(struct scsi_cmnd *))
1706 * build the libfc request pkt 1796 * build the libfc request pkt
1707 */ 1797 */
1708 fsp->cmd = sc_cmd; /* save the cmd */ 1798 fsp->cmd = sc_cmd; /* save the cmd */
1709 fsp->lp = lp; /* save the softc ptr */ 1799 fsp->lp = lport; /* save the softc ptr */
1710 fsp->rport = rport; /* set the remote port ptr */ 1800 fsp->rport = rport; /* set the remote port ptr */
1801 fsp->xfer_ddp = FC_XID_UNKNOWN;
1711 sc_cmd->scsi_done = done; 1802 sc_cmd->scsi_done = done;
1712 1803
1713 /* 1804 /*
@@ -1719,7 +1810,7 @@ int fc_queuecommand(struct scsi_cmnd *sc_cmd, void (*done)(struct scsi_cmnd *))
1719 /* 1810 /*
1720 * setup the data direction 1811 * setup the data direction
1721 */ 1812 */
1722 stats = fc_lport_get_stats(lp); 1813 stats = fc_lport_get_stats(lport);
1723 if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) { 1814 if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
1724 fsp->req_flags = FC_SRB_READ; 1815 fsp->req_flags = FC_SRB_READ;
1725 stats->InputRequests++; 1816 stats->InputRequests++;
@@ -1733,7 +1824,7 @@ int fc_queuecommand(struct scsi_cmnd *sc_cmd, void (*done)(struct scsi_cmnd *))
1733 stats->ControlRequests++; 1824 stats->ControlRequests++;
1734 } 1825 }
1735 1826
1736 fsp->tgt_flags = rp->flags; 1827 fsp->tgt_flags = rpriv->flags;
1737 1828
1738 init_timer(&fsp->timer); 1829 init_timer(&fsp->timer);
1739 fsp->timer.data = (unsigned long)fsp; 1830 fsp->timer.data = (unsigned long)fsp;
@@ -1743,30 +1834,30 @@ int fc_queuecommand(struct scsi_cmnd *sc_cmd, void (*done)(struct scsi_cmnd *))
1743 * if we get -1 return then put the request in the pending 1834 * if we get -1 return then put the request in the pending
1744 * queue. 1835 * queue.
1745 */ 1836 */
1746 rval = fc_fcp_pkt_send(lp, fsp); 1837 rval = fc_fcp_pkt_send(lport, fsp);
1747 if (rval != 0) { 1838 if (rval != 0) {
1748 fsp->state = FC_SRB_FREE; 1839 fsp->state = FC_SRB_FREE;
1749 fc_fcp_pkt_release(fsp); 1840 fc_fcp_pkt_release(fsp);
1750 rc = SCSI_MLQUEUE_HOST_BUSY; 1841 rc = SCSI_MLQUEUE_HOST_BUSY;
1751 } 1842 }
1752out: 1843out:
1844 spin_lock_irq(lport->host->host_lock);
1753 return rc; 1845 return rc;
1754} 1846}
1755EXPORT_SYMBOL(fc_queuecommand); 1847EXPORT_SYMBOL(fc_queuecommand);
1756 1848
1757/** 1849/**
1758 * fc_io_compl() - Handle responses for completed commands 1850 * fc_io_compl() - Handle responses for completed commands
1759 * @fsp: scsi packet 1851 * @fsp: The FCP packet that is complete
1760 *
1761 * Translates a error to a Linux SCSI error.
1762 * 1852 *
1853 * Translates fcp_pkt errors to a Linux SCSI errors.
1763 * The fcp packet lock must be held when calling. 1854 * The fcp packet lock must be held when calling.
1764 */ 1855 */
1765static void fc_io_compl(struct fc_fcp_pkt *fsp) 1856static void fc_io_compl(struct fc_fcp_pkt *fsp)
1766{ 1857{
1767 struct fc_fcp_internal *si; 1858 struct fc_fcp_internal *si;
1768 struct scsi_cmnd *sc_cmd; 1859 struct scsi_cmnd *sc_cmd;
1769 struct fc_lport *lp; 1860 struct fc_lport *lport;
1770 unsigned long flags; 1861 unsigned long flags;
1771 1862
1772 /* release outstanding ddp context */ 1863 /* release outstanding ddp context */
@@ -1779,30 +1870,23 @@ static void fc_io_compl(struct fc_fcp_pkt *fsp)
1779 spin_lock_bh(&fsp->scsi_pkt_lock); 1870 spin_lock_bh(&fsp->scsi_pkt_lock);
1780 } 1871 }
1781 1872
1782 lp = fsp->lp; 1873 lport = fsp->lp;
1783 si = fc_get_scsi_internal(lp); 1874 si = fc_get_scsi_internal(lport);
1784 spin_lock_irqsave(lp->host->host_lock, flags); 1875 if (!fsp->cmd)
1785 if (!fsp->cmd) {
1786 spin_unlock_irqrestore(lp->host->host_lock, flags);
1787 return; 1876 return;
1788 }
1789 1877
1790 /* 1878 /*
1791 * if a command timed out while we had to try and throttle IO 1879 * if can_queue ramp down is done then try can_queue ramp up
1792 * and it is now getting cleaned up, then we are about to 1880 * since commands are completing now.
1793 * try again so clear the throttled flag incase we get more
1794 * time outs.
1795 */ 1881 */
1796 if (si->throttled && fsp->state & FC_SRB_NOMEM) 1882 if (si->last_can_queue_ramp_down_time)
1797 si->throttled = 0; 1883 fc_fcp_can_queue_ramp_up(lport);
1798 1884
1799 sc_cmd = fsp->cmd; 1885 sc_cmd = fsp->cmd;
1800 fsp->cmd = NULL; 1886 fsp->cmd = NULL;
1801 1887
1802 if (!sc_cmd->SCp.ptr) { 1888 if (!sc_cmd->SCp.ptr)
1803 spin_unlock_irqrestore(lp->host->host_lock, flags);
1804 return; 1889 return;
1805 }
1806 1890
1807 CMD_SCSI_STATUS(sc_cmd) = fsp->cdb_status; 1891 CMD_SCSI_STATUS(sc_cmd) = fsp->cdb_status;
1808 switch (fsp->status_code) { 1892 switch (fsp->status_code) {
@@ -1814,21 +1898,6 @@ static void fc_io_compl(struct fc_fcp_pkt *fsp)
1814 sc_cmd->result = DID_OK << 16; 1898 sc_cmd->result = DID_OK << 16;
1815 if (fsp->scsi_resid) 1899 if (fsp->scsi_resid)
1816 CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid; 1900 CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
1817 } else if (fsp->cdb_status == QUEUE_FULL) {
1818 struct scsi_device *tmp_sdev;
1819 struct scsi_device *sdev = sc_cmd->device;
1820
1821 shost_for_each_device(tmp_sdev, sdev->host) {
1822 if (tmp_sdev->id != sdev->id)
1823 continue;
1824
1825 if (tmp_sdev->queue_depth > 1) {
1826 scsi_track_queue_full(tmp_sdev,
1827 tmp_sdev->
1828 queue_depth - 1);
1829 }
1830 }
1831 sc_cmd->result = (DID_OK << 16) | fsp->cdb_status;
1832 } else { 1901 } else {
1833 /* 1902 /*
1834 * transport level I/O was ok but scsi 1903 * transport level I/O was ok but scsi
@@ -1846,7 +1915,8 @@ static void fc_io_compl(struct fc_fcp_pkt *fsp)
1846 * scsi status is good but transport level 1915 * scsi status is good but transport level
1847 * underrun. 1916 * underrun.
1848 */ 1917 */
1849 sc_cmd->result = DID_OK << 16; 1918 sc_cmd->result = (fsp->state & FC_SRB_RCV_STATUS ?
1919 DID_OK : DID_ERROR) << 16;
1850 } else { 1920 } else {
1851 /* 1921 /*
1852 * scsi got underrun, this is an error 1922 * scsi got underrun, this is an error
@@ -1878,63 +1948,46 @@ static void fc_io_compl(struct fc_fcp_pkt *fsp)
1878 break; 1948 break;
1879 } 1949 }
1880 1950
1951 spin_lock_irqsave(&si->scsi_queue_lock, flags);
1881 list_del(&fsp->list); 1952 list_del(&fsp->list);
1953 spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
1882 sc_cmd->SCp.ptr = NULL; 1954 sc_cmd->SCp.ptr = NULL;
1883 sc_cmd->scsi_done(sc_cmd); 1955 sc_cmd->scsi_done(sc_cmd);
1884 spin_unlock_irqrestore(lp->host->host_lock, flags);
1885 1956
1886 /* release ref from initial allocation in queue command */ 1957 /* release ref from initial allocation in queue command */
1887 fc_fcp_pkt_release(fsp); 1958 fc_fcp_pkt_release(fsp);
1888} 1959}
1889 1960
1890/** 1961/**
1891 * fc_fcp_complete() - complete processing of a fcp packet
1892 * @fsp: fcp packet
1893 *
1894 * This function may sleep if a fsp timer is pending.
1895 * The host lock must not be held by caller.
1896 */
1897void fc_fcp_complete(struct fc_fcp_pkt *fsp)
1898{
1899 if (fc_fcp_lock_pkt(fsp))
1900 return;
1901
1902 fc_fcp_complete_locked(fsp);
1903 fc_fcp_unlock_pkt(fsp);
1904}
1905EXPORT_SYMBOL(fc_fcp_complete);
1906
1907/**
1908 * fc_eh_abort() - Abort a command 1962 * fc_eh_abort() - Abort a command
1909 * @sc_cmd: scsi command to abort 1963 * @sc_cmd: The SCSI command to abort
1910 * 1964 *
1911 * From scsi host template. 1965 * From SCSI host template.
1912 * send ABTS to the target device and wait for the response 1966 * Send an ABTS to the target device and wait for the response.
1913 * sc_cmd is the pointer to the command to be aborted.
1914 */ 1967 */
1915int fc_eh_abort(struct scsi_cmnd *sc_cmd) 1968int fc_eh_abort(struct scsi_cmnd *sc_cmd)
1916{ 1969{
1917 struct fc_fcp_pkt *fsp; 1970 struct fc_fcp_pkt *fsp;
1918 struct fc_lport *lp; 1971 struct fc_lport *lport;
1919 int rc = FAILED; 1972 int rc = FAILED;
1920 unsigned long flags; 1973 unsigned long flags;
1921 1974
1922 lp = shost_priv(sc_cmd->device->host); 1975 lport = shost_priv(sc_cmd->device->host);
1923 if (lp->state != LPORT_ST_READY) 1976 if (lport->state != LPORT_ST_READY)
1924 return rc; 1977 return rc;
1925 else if (!lp->link_up) 1978 else if (!lport->link_up)
1926 return rc; 1979 return rc;
1927 1980
1928 spin_lock_irqsave(lp->host->host_lock, flags); 1981 spin_lock_irqsave(lport->host->host_lock, flags);
1929 fsp = CMD_SP(sc_cmd); 1982 fsp = CMD_SP(sc_cmd);
1930 if (!fsp) { 1983 if (!fsp) {
1931 /* command completed while scsi eh was setting up */ 1984 /* command completed while scsi eh was setting up */
1932 spin_unlock_irqrestore(lp->host->host_lock, flags); 1985 spin_unlock_irqrestore(lport->host->host_lock, flags);
1933 return SUCCESS; 1986 return SUCCESS;
1934 } 1987 }
1935 /* grab a ref so the fsp and sc_cmd cannot be relased from under us */ 1988 /* grab a ref so the fsp and sc_cmd cannot be relased from under us */
1936 fc_fcp_pkt_hold(fsp); 1989 fc_fcp_pkt_hold(fsp);
1937 spin_unlock_irqrestore(lp->host->host_lock, flags); 1990 spin_unlock_irqrestore(lport->host->host_lock, flags);
1938 1991
1939 if (fc_fcp_lock_pkt(fsp)) { 1992 if (fc_fcp_lock_pkt(fsp)) {
1940 /* completed while we were waiting for timer to be deleted */ 1993 /* completed while we were waiting for timer to be deleted */
@@ -1942,7 +1995,7 @@ int fc_eh_abort(struct scsi_cmnd *sc_cmd)
1942 goto release_pkt; 1995 goto release_pkt;
1943 } 1996 }
1944 1997
1945 rc = fc_fcp_pkt_abort(lp, fsp); 1998 rc = fc_fcp_pkt_abort(fsp);
1946 fc_fcp_unlock_pkt(fsp); 1999 fc_fcp_unlock_pkt(fsp);
1947 2000
1948release_pkt: 2001release_pkt:
@@ -1952,37 +2005,34 @@ release_pkt:
1952EXPORT_SYMBOL(fc_eh_abort); 2005EXPORT_SYMBOL(fc_eh_abort);
1953 2006
1954/** 2007/**
1955 * fc_eh_device_reset() Reset a single LUN 2008 * fc_eh_device_reset() - Reset a single LUN
1956 * @sc_cmd: scsi command 2009 * @sc_cmd: The SCSI command which identifies the device whose
2010 * LUN is to be reset
1957 * 2011 *
1958 * Set from scsi host template to send tm cmd to the target and wait for the 2012 * Set from SCSI host template.
1959 * response.
1960 */ 2013 */
1961int fc_eh_device_reset(struct scsi_cmnd *sc_cmd) 2014int fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
1962{ 2015{
1963 struct fc_lport *lp; 2016 struct fc_lport *lport;
1964 struct fc_fcp_pkt *fsp; 2017 struct fc_fcp_pkt *fsp;
1965 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device)); 2018 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
1966 int rc = FAILED; 2019 int rc = FAILED;
1967 struct fc_rport_libfc_priv *rp;
1968 int rval; 2020 int rval;
1969 2021
1970 rval = fc_remote_port_chkready(rport); 2022 rval = fc_remote_port_chkready(rport);
1971 if (rval) 2023 if (rval)
1972 goto out; 2024 goto out;
1973 2025
1974 rp = rport->dd_data; 2026 lport = shost_priv(sc_cmd->device->host);
1975 lp = shost_priv(sc_cmd->device->host);
1976 2027
1977 if (lp->state != LPORT_ST_READY) 2028 if (lport->state != LPORT_ST_READY)
1978 return rc; 2029 return rc;
1979 2030
1980 FC_SCSI_DBG(lp, "Resetting rport (%6x)\n", rport->port_id); 2031 FC_SCSI_DBG(lport, "Resetting rport (%6x)\n", rport->port_id);
1981 2032
1982 fsp = fc_fcp_pkt_alloc(lp, GFP_NOIO); 2033 fsp = fc_fcp_pkt_alloc(lport, GFP_NOIO);
1983 if (fsp == NULL) { 2034 if (fsp == NULL) {
1984 printk(KERN_WARNING "libfc: could not allocate scsi_pkt\n"); 2035 printk(KERN_WARNING "libfc: could not allocate scsi_pkt\n");
1985 sc_cmd->result = DID_NO_CONNECT << 16;
1986 goto out; 2036 goto out;
1987 } 2037 }
1988 2038
@@ -1991,13 +2041,13 @@ int fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
1991 * the sc passed in is not setup for execution like when sent 2041 * the sc passed in is not setup for execution like when sent
1992 * through the queuecommand callout. 2042 * through the queuecommand callout.
1993 */ 2043 */
1994 fsp->lp = lp; /* save the softc ptr */ 2044 fsp->lp = lport; /* save the softc ptr */
1995 fsp->rport = rport; /* set the remote port ptr */ 2045 fsp->rport = rport; /* set the remote port ptr */
1996 2046
1997 /* 2047 /*
1998 * flush outstanding commands 2048 * flush outstanding commands
1999 */ 2049 */
2000 rc = fc_lun_reset(lp, fsp, scmd_id(sc_cmd), sc_cmd->device->lun); 2050 rc = fc_lun_reset(lport, fsp, scmd_id(sc_cmd), sc_cmd->device->lun);
2001 fsp->state = FC_SRB_FREE; 2051 fsp->state = FC_SRB_FREE;
2002 fc_fcp_pkt_release(fsp); 2052 fc_fcp_pkt_release(fsp);
2003 2053
@@ -2007,38 +2057,39 @@ out:
2007EXPORT_SYMBOL(fc_eh_device_reset); 2057EXPORT_SYMBOL(fc_eh_device_reset);
2008 2058
2009/** 2059/**
2010 * fc_eh_host_reset() - The reset function will reset the ports on the host. 2060 * fc_eh_host_reset() - Reset a Scsi_Host.
2011 * @sc_cmd: scsi command 2061 * @sc_cmd: The SCSI command that identifies the SCSI host to be reset
2012 */ 2062 */
2013int fc_eh_host_reset(struct scsi_cmnd *sc_cmd) 2063int fc_eh_host_reset(struct scsi_cmnd *sc_cmd)
2014{ 2064{
2015 struct Scsi_Host *shost = sc_cmd->device->host; 2065 struct Scsi_Host *shost = sc_cmd->device->host;
2016 struct fc_lport *lp = shost_priv(shost); 2066 struct fc_lport *lport = shost_priv(shost);
2017 unsigned long wait_tmo; 2067 unsigned long wait_tmo;
2018 2068
2019 FC_SCSI_DBG(lp, "Resetting host\n"); 2069 FC_SCSI_DBG(lport, "Resetting host\n");
2020 2070
2021 lp->tt.lport_reset(lp); 2071 lport->tt.lport_reset(lport);
2022 wait_tmo = jiffies + FC_HOST_RESET_TIMEOUT; 2072 wait_tmo = jiffies + FC_HOST_RESET_TIMEOUT;
2023 while (!fc_fcp_lport_queue_ready(lp) && time_before(jiffies, wait_tmo)) 2073 while (!fc_fcp_lport_queue_ready(lport) && time_before(jiffies,
2074 wait_tmo))
2024 msleep(1000); 2075 msleep(1000);
2025 2076
2026 if (fc_fcp_lport_queue_ready(lp)) { 2077 if (fc_fcp_lport_queue_ready(lport)) {
2027 shost_printk(KERN_INFO, shost, "libfc: Host reset succeeded " 2078 shost_printk(KERN_INFO, shost, "libfc: Host reset succeeded "
2028 "on port (%6x)\n", fc_host_port_id(lp->host)); 2079 "on port (%6x)\n", fc_host_port_id(lport->host));
2029 return SUCCESS; 2080 return SUCCESS;
2030 } else { 2081 } else {
2031 shost_printk(KERN_INFO, shost, "libfc: Host reset failed, " 2082 shost_printk(KERN_INFO, shost, "libfc: Host reset failed, "
2032 "port (%6x) is not ready.\n", 2083 "port (%6x) is not ready.\n",
2033 fc_host_port_id(lp->host)); 2084 fc_host_port_id(lport->host));
2034 return FAILED; 2085 return FAILED;
2035 } 2086 }
2036} 2087}
2037EXPORT_SYMBOL(fc_eh_host_reset); 2088EXPORT_SYMBOL(fc_eh_host_reset);
2038 2089
2039/** 2090/**
2040 * fc_slave_alloc() - configure queue depth 2091 * fc_slave_alloc() - Configure the queue depth of a Scsi_Host
2041 * @sdev: scsi device 2092 * @sdev: The SCSI device that identifies the SCSI host
2042 * 2093 *
2043 * Configures queue depth based on host's cmd_per_len. If not set 2094 * Configures queue depth based on host's cmd_per_len. If not set
2044 * then we use the libfc default. 2095 * then we use the libfc default.
@@ -2046,29 +2097,50 @@ EXPORT_SYMBOL(fc_eh_host_reset);
2046int fc_slave_alloc(struct scsi_device *sdev) 2097int fc_slave_alloc(struct scsi_device *sdev)
2047{ 2098{
2048 struct fc_rport *rport = starget_to_rport(scsi_target(sdev)); 2099 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2049 int queue_depth;
2050 2100
2051 if (!rport || fc_remote_port_chkready(rport)) 2101 if (!rport || fc_remote_port_chkready(rport))
2052 return -ENXIO; 2102 return -ENXIO;
2053 2103
2054 if (sdev->tagged_supported) { 2104 if (sdev->tagged_supported)
2055 if (sdev->host->hostt->cmd_per_lun) 2105 scsi_activate_tcq(sdev, FC_FCP_DFLT_QUEUE_DEPTH);
2056 queue_depth = sdev->host->hostt->cmd_per_lun; 2106 else
2057 else 2107 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev),
2058 queue_depth = FC_FCP_DFLT_QUEUE_DEPTH; 2108 FC_FCP_DFLT_QUEUE_DEPTH);
2059 scsi_activate_tcq(sdev, queue_depth); 2109
2060 }
2061 return 0; 2110 return 0;
2062} 2111}
2063EXPORT_SYMBOL(fc_slave_alloc); 2112EXPORT_SYMBOL(fc_slave_alloc);
2064 2113
2065int fc_change_queue_depth(struct scsi_device *sdev, int qdepth) 2114/**
2115 * fc_change_queue_depth() - Change a device's queue depth
2116 * @sdev: The SCSI device whose queue depth is to change
2117 * @qdepth: The new queue depth
2118 * @reason: The resason for the change
2119 */
2120int fc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
2066{ 2121{
2067 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth); 2122 switch (reason) {
2123 case SCSI_QDEPTH_DEFAULT:
2124 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
2125 break;
2126 case SCSI_QDEPTH_QFULL:
2127 scsi_track_queue_full(sdev, qdepth);
2128 break;
2129 case SCSI_QDEPTH_RAMP_UP:
2130 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
2131 break;
2132 default:
2133 return -EOPNOTSUPP;
2134 }
2068 return sdev->queue_depth; 2135 return sdev->queue_depth;
2069} 2136}
2070EXPORT_SYMBOL(fc_change_queue_depth); 2137EXPORT_SYMBOL(fc_change_queue_depth);
2071 2138
2139/**
2140 * fc_change_queue_type() - Change a device's queue type
2141 * @sdev: The SCSI device whose queue depth is to change
2142 * @tag_type: Identifier for queue type
2143 */
2072int fc_change_queue_type(struct scsi_device *sdev, int tag_type) 2144int fc_change_queue_type(struct scsi_device *sdev, int tag_type)
2073{ 2145{
2074 if (sdev->tagged_supported) { 2146 if (sdev->tagged_supported) {
@@ -2084,39 +2156,71 @@ int fc_change_queue_type(struct scsi_device *sdev, int tag_type)
2084} 2156}
2085EXPORT_SYMBOL(fc_change_queue_type); 2157EXPORT_SYMBOL(fc_change_queue_type);
2086 2158
2087void fc_fcp_destroy(struct fc_lport *lp) 2159/**
2160 * fc_fcp_destory() - Tear down the FCP layer for a given local port
2161 * @lport: The local port that no longer needs the FCP layer
2162 */
2163void fc_fcp_destroy(struct fc_lport *lport)
2088{ 2164{
2089 struct fc_fcp_internal *si = fc_get_scsi_internal(lp); 2165 struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
2090 2166
2091 if (!list_empty(&si->scsi_pkt_queue)) 2167 if (!list_empty(&si->scsi_pkt_queue))
2092 printk(KERN_ERR "libfc: Leaked SCSI packets when destroying " 2168 printk(KERN_ERR "libfc: Leaked SCSI packets when destroying "
2093 "port (%6x)\n", fc_host_port_id(lp->host)); 2169 "port (%6x)\n", fc_host_port_id(lport->host));
2094 2170
2095 mempool_destroy(si->scsi_pkt_pool); 2171 mempool_destroy(si->scsi_pkt_pool);
2096 kfree(si); 2172 kfree(si);
2097 lp->scsi_priv = NULL; 2173 lport->scsi_priv = NULL;
2098} 2174}
2099EXPORT_SYMBOL(fc_fcp_destroy); 2175EXPORT_SYMBOL(fc_fcp_destroy);
2100 2176
2101int fc_fcp_init(struct fc_lport *lp) 2177int fc_setup_fcp()
2178{
2179 int rc = 0;
2180
2181 scsi_pkt_cachep = kmem_cache_create("libfc_fcp_pkt",
2182 sizeof(struct fc_fcp_pkt),
2183 0, SLAB_HWCACHE_ALIGN, NULL);
2184 if (!scsi_pkt_cachep) {
2185 printk(KERN_ERR "libfc: Unable to allocate SRB cache, "
2186 "module load failed!");
2187 rc = -ENOMEM;
2188 }
2189
2190 return rc;
2191}
2192
2193void fc_destroy_fcp()
2194{
2195 if (scsi_pkt_cachep)
2196 kmem_cache_destroy(scsi_pkt_cachep);
2197}
2198
2199/**
2200 * fc_fcp_init() - Initialize the FCP layer for a local port
2201 * @lport: The local port to initialize the exchange layer for
2202 */
2203int fc_fcp_init(struct fc_lport *lport)
2102{ 2204{
2103 int rc; 2205 int rc;
2104 struct fc_fcp_internal *si; 2206 struct fc_fcp_internal *si;
2105 2207
2106 if (!lp->tt.fcp_cmd_send) 2208 if (!lport->tt.fcp_cmd_send)
2107 lp->tt.fcp_cmd_send = fc_fcp_cmd_send; 2209 lport->tt.fcp_cmd_send = fc_fcp_cmd_send;
2108 2210
2109 if (!lp->tt.fcp_cleanup) 2211 if (!lport->tt.fcp_cleanup)
2110 lp->tt.fcp_cleanup = fc_fcp_cleanup; 2212 lport->tt.fcp_cleanup = fc_fcp_cleanup;
2111 2213
2112 if (!lp->tt.fcp_abort_io) 2214 if (!lport->tt.fcp_abort_io)
2113 lp->tt.fcp_abort_io = fc_fcp_abort_io; 2215 lport->tt.fcp_abort_io = fc_fcp_abort_io;
2114 2216
2115 si = kzalloc(sizeof(struct fc_fcp_internal), GFP_KERNEL); 2217 si = kzalloc(sizeof(struct fc_fcp_internal), GFP_KERNEL);
2116 if (!si) 2218 if (!si)
2117 return -ENOMEM; 2219 return -ENOMEM;
2118 lp->scsi_priv = si; 2220 lport->scsi_priv = si;
2221 si->max_can_queue = lport->host->can_queue;
2119 INIT_LIST_HEAD(&si->scsi_pkt_queue); 2222 INIT_LIST_HEAD(&si->scsi_pkt_queue);
2223 spin_lock_init(&si->scsi_queue_lock);
2120 2224
2121 si->scsi_pkt_pool = mempool_create_slab_pool(2, scsi_pkt_cachep); 2225 si->scsi_pkt_pool = mempool_create_slab_pool(2, scsi_pkt_cachep);
2122 if (!si->scsi_pkt_pool) { 2226 if (!si->scsi_pkt_pool) {
@@ -2130,42 +2234,3 @@ free_internal:
2130 return rc; 2234 return rc;
2131} 2235}
2132EXPORT_SYMBOL(fc_fcp_init); 2236EXPORT_SYMBOL(fc_fcp_init);
2133
2134static int __init libfc_init(void)
2135{
2136 int rc;
2137
2138 scsi_pkt_cachep = kmem_cache_create("libfc_fcp_pkt",
2139 sizeof(struct fc_fcp_pkt),
2140 0, SLAB_HWCACHE_ALIGN, NULL);
2141 if (scsi_pkt_cachep == NULL) {
2142 printk(KERN_ERR "libfc: Unable to allocate SRB cache, "
2143 "module load failed!");
2144 return -ENOMEM;
2145 }
2146
2147 rc = fc_setup_exch_mgr();
2148 if (rc)
2149 goto destroy_pkt_cache;
2150
2151 rc = fc_setup_rport();
2152 if (rc)
2153 goto destroy_em;
2154
2155 return rc;
2156destroy_em:
2157 fc_destroy_exch_mgr();
2158destroy_pkt_cache:
2159 kmem_cache_destroy(scsi_pkt_cachep);
2160 return rc;
2161}
2162
2163static void __exit libfc_exit(void)
2164{
2165 kmem_cache_destroy(scsi_pkt_cachep);
2166 fc_destroy_exch_mgr();
2167 fc_destroy_rport();
2168}
2169
2170module_init(libfc_init);
2171module_exit(libfc_exit);
diff --git a/drivers/scsi/libfc/fc_frame.c b/drivers/scsi/libfc/fc_frame.c
index 63fe00cfe667..981329a17c48 100644
--- a/drivers/scsi/libfc/fc_frame.c
+++ b/drivers/scsi/libfc/fc_frame.c
@@ -24,6 +24,7 @@
24#include <linux/kernel.h> 24#include <linux/kernel.h>
25#include <linux/skbuff.h> 25#include <linux/skbuff.h>
26#include <linux/crc32.h> 26#include <linux/crc32.h>
27#include <linux/gfp.h>
27 28
28#include <scsi/fc_frame.h> 29#include <scsi/fc_frame.h>
29 30
@@ -51,24 +52,24 @@ EXPORT_SYMBOL(fc_frame_crc_check);
51 * Allocate a frame intended to be sent via fcoe_xmit. 52 * Allocate a frame intended to be sent via fcoe_xmit.
52 * Get an sk_buff for the frame and set the length. 53 * Get an sk_buff for the frame and set the length.
53 */ 54 */
54struct fc_frame *__fc_frame_alloc(size_t len) 55struct fc_frame *_fc_frame_alloc(size_t len)
55{ 56{
56 struct fc_frame *fp; 57 struct fc_frame *fp;
57 struct sk_buff *skb; 58 struct sk_buff *skb;
58 59
59 WARN_ON((len % sizeof(u32)) != 0); 60 WARN_ON((len % sizeof(u32)) != 0);
60 len += sizeof(struct fc_frame_header); 61 len += sizeof(struct fc_frame_header);
61 skb = dev_alloc_skb(len + FC_FRAME_HEADROOM + FC_FRAME_TAILROOM); 62 skb = alloc_skb_fclone(len + FC_FRAME_HEADROOM + FC_FRAME_TAILROOM +
63 NET_SKB_PAD, GFP_ATOMIC);
62 if (!skb) 64 if (!skb)
63 return NULL; 65 return NULL;
66 skb_reserve(skb, NET_SKB_PAD + FC_FRAME_HEADROOM);
64 fp = (struct fc_frame *) skb; 67 fp = (struct fc_frame *) skb;
65 fc_frame_init(fp); 68 fc_frame_init(fp);
66 skb_reserve(skb, FC_FRAME_HEADROOM);
67 skb_put(skb, len); 69 skb_put(skb, len);
68 return fp; 70 return fp;
69} 71}
70EXPORT_SYMBOL(__fc_frame_alloc); 72EXPORT_SYMBOL(_fc_frame_alloc);
71
72 73
73struct fc_frame *fc_frame_alloc_fill(struct fc_lport *lp, size_t payload_len) 74struct fc_frame *fc_frame_alloc_fill(struct fc_lport *lp, size_t payload_len)
74{ 75{
@@ -78,7 +79,7 @@ struct fc_frame *fc_frame_alloc_fill(struct fc_lport *lp, size_t payload_len)
78 fill = payload_len % 4; 79 fill = payload_len % 4;
79 if (fill != 0) 80 if (fill != 0)
80 fill = 4 - fill; 81 fill = 4 - fill;
81 fp = __fc_frame_alloc(payload_len + fill); 82 fp = _fc_frame_alloc(payload_len + fill);
82 if (fp) { 83 if (fp) {
83 memset((char *) fr_hdr(fp) + payload_len, 0, fill); 84 memset((char *) fr_hdr(fp) + payload_len, 0, fill);
84 /* trim is OK, we just allocated it so there are no fragments */ 85 /* trim is OK, we just allocated it so there are no fragments */
@@ -87,3 +88,4 @@ struct fc_frame *fc_frame_alloc_fill(struct fc_lport *lp, size_t payload_len)
87 } 88 }
88 return fp; 89 return fp;
89} 90}
91EXPORT_SYMBOL(fc_frame_alloc_fill);
diff --git a/drivers/scsi/libfc/fc_libfc.c b/drivers/scsi/libfc/fc_libfc.c
new file mode 100644
index 000000000000..39f4b6ab04b4
--- /dev/null
+++ b/drivers/scsi/libfc/fc_libfc.c
@@ -0,0 +1,134 @@
1/*
2 * Copyright(c) 2009 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#include <linux/kernel.h>
21#include <linux/types.h>
22#include <linux/scatterlist.h>
23#include <linux/crc32.h>
24
25#include <scsi/libfc.h>
26
27#include "fc_libfc.h"
28
29MODULE_AUTHOR("Open-FCoE.org");
30MODULE_DESCRIPTION("libfc");
31MODULE_LICENSE("GPL v2");
32
33unsigned int fc_debug_logging;
34module_param_named(debug_logging, fc_debug_logging, int, S_IRUGO|S_IWUSR);
35MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
36
37/**
38 * libfc_init() - Initialize libfc.ko
39 */
40static int __init libfc_init(void)
41{
42 int rc = 0;
43
44 rc = fc_setup_fcp();
45 if (rc)
46 return rc;
47
48 rc = fc_setup_exch_mgr();
49 if (rc)
50 goto destroy_pkt_cache;
51
52 rc = fc_setup_rport();
53 if (rc)
54 goto destroy_em;
55
56 return rc;
57destroy_em:
58 fc_destroy_exch_mgr();
59destroy_pkt_cache:
60 fc_destroy_fcp();
61 return rc;
62}
63module_init(libfc_init);
64
65/**
66 * libfc_exit() - Tear down libfc.ko
67 */
68static void __exit libfc_exit(void)
69{
70 fc_destroy_fcp();
71 fc_destroy_exch_mgr();
72 fc_destroy_rport();
73}
74module_exit(libfc_exit);
75
76/**
77 * fc_copy_buffer_to_sglist() - This routine copies the data of a buffer
78 * into a scatter-gather list (SG list).
79 *
80 * @buf: pointer to the data buffer.
81 * @len: the byte-length of the data buffer.
82 * @sg: pointer to the pointer of the SG list.
83 * @nents: pointer to the remaining number of entries in the SG list.
84 * @offset: pointer to the current offset in the SG list.
85 * @km_type: dedicated page table slot type for kmap_atomic.
86 * @crc: pointer to the 32-bit crc value.
87 * If crc is NULL, CRC is not calculated.
88 */
89u32 fc_copy_buffer_to_sglist(void *buf, size_t len,
90 struct scatterlist *sg,
91 u32 *nents, size_t *offset,
92 enum km_type km_type, u32 *crc)
93{
94 size_t remaining = len;
95 u32 copy_len = 0;
96
97 while (remaining > 0 && sg) {
98 size_t off, sg_bytes;
99 void *page_addr;
100
101 if (*offset >= sg->length) {
102 /*
103 * Check for end and drop resources
104 * from the last iteration.
105 */
106 if (!(*nents))
107 break;
108 --(*nents);
109 *offset -= sg->length;
110 sg = sg_next(sg);
111 continue;
112 }
113 sg_bytes = min(remaining, sg->length - *offset);
114
115 /*
116 * The scatterlist item may be bigger than PAGE_SIZE,
117 * but we are limited to mapping PAGE_SIZE at a time.
118 */
119 off = *offset + sg->offset;
120 sg_bytes = min(sg_bytes,
121 (size_t)(PAGE_SIZE - (off & ~PAGE_MASK)));
122 page_addr = kmap_atomic(sg_page(sg) + (off >> PAGE_SHIFT),
123 km_type);
124 if (crc)
125 *crc = crc32(*crc, buf, sg_bytes);
126 memcpy((char *)page_addr + (off & ~PAGE_MASK), buf, sg_bytes);
127 kunmap_atomic(page_addr, km_type);
128 buf += sg_bytes;
129 *offset += sg_bytes;
130 remaining -= sg_bytes;
131 copy_len += sg_bytes;
132 }
133 return copy_len;
134}
diff --git a/drivers/scsi/libfc/fc_libfc.h b/drivers/scsi/libfc/fc_libfc.h
new file mode 100644
index 000000000000..741fd5c72e13
--- /dev/null
+++ b/drivers/scsi/libfc/fc_libfc.h
@@ -0,0 +1,112 @@
1/*
2 * Copyright(c) 2009 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#ifndef _FC_LIBFC_H_
21#define _FC_LIBFC_H_
22
23#define FC_LIBFC_LOGGING 0x01 /* General logging, not categorized */
24#define FC_LPORT_LOGGING 0x02 /* lport layer logging */
25#define FC_DISC_LOGGING 0x04 /* discovery layer logging */
26#define FC_RPORT_LOGGING 0x08 /* rport layer logging */
27#define FC_FCP_LOGGING 0x10 /* I/O path logging */
28#define FC_EM_LOGGING 0x20 /* Exchange Manager logging */
29#define FC_EXCH_LOGGING 0x40 /* Exchange/Sequence logging */
30#define FC_SCSI_LOGGING 0x80 /* SCSI logging (mostly error handling) */
31
32extern unsigned int fc_debug_logging;
33
34#define FC_CHECK_LOGGING(LEVEL, CMD) \
35 do { \
36 if (unlikely(fc_debug_logging & LEVEL)) \
37 do { \
38 CMD; \
39 } while (0); \
40 } while (0)
41
42#define FC_LIBFC_DBG(fmt, args...) \
43 FC_CHECK_LOGGING(FC_LIBFC_LOGGING, \
44 printk(KERN_INFO "libfc: " fmt, ##args))
45
46#define FC_LPORT_DBG(lport, fmt, args...) \
47 FC_CHECK_LOGGING(FC_LPORT_LOGGING, \
48 printk(KERN_INFO "host%u: lport %6x: " fmt, \
49 (lport)->host->host_no, \
50 fc_host_port_id((lport)->host), ##args))
51
52#define FC_DISC_DBG(disc, fmt, args...) \
53 FC_CHECK_LOGGING(FC_DISC_LOGGING, \
54 printk(KERN_INFO "host%u: disc: " fmt, \
55 (disc)->lport->host->host_no, \
56 ##args))
57
58#define FC_RPORT_ID_DBG(lport, port_id, fmt, args...) \
59 FC_CHECK_LOGGING(FC_RPORT_LOGGING, \
60 printk(KERN_INFO "host%u: rport %6x: " fmt, \
61 (lport)->host->host_no, \
62 (port_id), ##args))
63
64#define FC_RPORT_DBG(rdata, fmt, args...) \
65 FC_RPORT_ID_DBG((rdata)->local_port, (rdata)->ids.port_id, fmt, ##args)
66
67#define FC_FCP_DBG(pkt, fmt, args...) \
68 FC_CHECK_LOGGING(FC_FCP_LOGGING, \
69 printk(KERN_INFO "host%u: fcp: %6x: " fmt, \
70 (pkt)->lp->host->host_no, \
71 pkt->rport->port_id, ##args))
72
73#define FC_EXCH_DBG(exch, fmt, args...) \
74 FC_CHECK_LOGGING(FC_EXCH_LOGGING, \
75 printk(KERN_INFO "host%u: xid %4x: " fmt, \
76 (exch)->lp->host->host_no, \
77 exch->xid, ##args))
78
79#define FC_SCSI_DBG(lport, fmt, args...) \
80 FC_CHECK_LOGGING(FC_SCSI_LOGGING, \
81 printk(KERN_INFO "host%u: scsi: " fmt, \
82 (lport)->host->host_no, ##args))
83
84/*
85 * Set up direct-data placement for this I/O request
86 */
87void fc_fcp_ddp_setup(struct fc_fcp_pkt *fsp, u16 xid);
88
89/*
90 * Module setup functions
91 */
92int fc_setup_exch_mgr(void);
93void fc_destroy_exch_mgr(void);
94int fc_setup_rport(void);
95void fc_destroy_rport(void);
96int fc_setup_fcp(void);
97void fc_destroy_fcp(void);
98
99/*
100 * Internal libfc functions
101 */
102const char *fc_els_resp_type(struct fc_frame *);
103
104/*
105 * Copies a buffer into an sg list
106 */
107u32 fc_copy_buffer_to_sglist(void *buf, size_t len,
108 struct scatterlist *sg,
109 u32 *nents, size_t *offset,
110 enum km_type km_type, u32 *crc);
111
112#endif /* _FC_LIBFC_H_ */
diff --git a/drivers/scsi/libfc/fc_lport.c b/drivers/scsi/libfc/fc_lport.c
index bd2f77197447..d126ecfff704 100644
--- a/drivers/scsi/libfc/fc_lport.c
+++ b/drivers/scsi/libfc/fc_lport.c
@@ -56,7 +56,7 @@
56 * at the same time. 56 * at the same time.
57 * 57 *
58 * When discovery succeeds or fails a callback is made to the lport as 58 * When discovery succeeds or fails a callback is made to the lport as
59 * notification. Currently, succesful discovery causes the lport to take no 59 * notification. Currently, successful discovery causes the lport to take no
60 * action. A failure will cause the lport to reset. There is likely a circular 60 * action. A failure will cause the lport to reset. There is likely a circular
61 * locking problem with this implementation. 61 * locking problem with this implementation.
62 */ 62 */
@@ -88,12 +88,16 @@
88 */ 88 */
89 89
90#include <linux/timer.h> 90#include <linux/timer.h>
91#include <linux/slab.h>
91#include <asm/unaligned.h> 92#include <asm/unaligned.h>
92 93
93#include <scsi/fc/fc_gs.h> 94#include <scsi/fc/fc_gs.h>
94 95
95#include <scsi/libfc.h> 96#include <scsi/libfc.h>
96#include <scsi/fc_encode.h> 97#include <scsi/fc_encode.h>
98#include <linux/scatterlist.h>
99
100#include "fc_libfc.h"
97 101
98/* Fabric IDs to use for point-to-point mode, chosen on whims. */ 102/* Fabric IDs to use for point-to-point mode, chosen on whims. */
99#define FC_LOCAL_PTP_FID_LO 0x010101 103#define FC_LOCAL_PTP_FID_LO 0x010101
@@ -106,8 +110,7 @@ static void fc_lport_error(struct fc_lport *, struct fc_frame *);
106static void fc_lport_enter_reset(struct fc_lport *); 110static void fc_lport_enter_reset(struct fc_lport *);
107static void fc_lport_enter_flogi(struct fc_lport *); 111static void fc_lport_enter_flogi(struct fc_lport *);
108static void fc_lport_enter_dns(struct fc_lport *); 112static void fc_lport_enter_dns(struct fc_lport *);
109static void fc_lport_enter_rpn_id(struct fc_lport *); 113static void fc_lport_enter_ns(struct fc_lport *, enum fc_lport_state);
110static void fc_lport_enter_rft_id(struct fc_lport *);
111static void fc_lport_enter_scr(struct fc_lport *); 114static void fc_lport_enter_scr(struct fc_lport *);
112static void fc_lport_enter_ready(struct fc_lport *); 115static void fc_lport_enter_ready(struct fc_lport *);
113static void fc_lport_enter_logo(struct fc_lport *); 116static void fc_lport_enter_logo(struct fc_lport *);
@@ -116,14 +119,40 @@ static const char *fc_lport_state_names[] = {
116 [LPORT_ST_DISABLED] = "disabled", 119 [LPORT_ST_DISABLED] = "disabled",
117 [LPORT_ST_FLOGI] = "FLOGI", 120 [LPORT_ST_FLOGI] = "FLOGI",
118 [LPORT_ST_DNS] = "dNS", 121 [LPORT_ST_DNS] = "dNS",
119 [LPORT_ST_RPN_ID] = "RPN_ID", 122 [LPORT_ST_RNN_ID] = "RNN_ID",
123 [LPORT_ST_RSNN_NN] = "RSNN_NN",
124 [LPORT_ST_RSPN_ID] = "RSPN_ID",
120 [LPORT_ST_RFT_ID] = "RFT_ID", 125 [LPORT_ST_RFT_ID] = "RFT_ID",
126 [LPORT_ST_RFF_ID] = "RFF_ID",
121 [LPORT_ST_SCR] = "SCR", 127 [LPORT_ST_SCR] = "SCR",
122 [LPORT_ST_READY] = "Ready", 128 [LPORT_ST_READY] = "Ready",
123 [LPORT_ST_LOGO] = "LOGO", 129 [LPORT_ST_LOGO] = "LOGO",
124 [LPORT_ST_RESET] = "reset", 130 [LPORT_ST_RESET] = "reset",
125}; 131};
126 132
133/**
134 * struct fc_bsg_info - FC Passthrough managemet structure
135 * @job: The passthrough job
136 * @lport: The local port to pass through a command
137 * @rsp_code: The expected response code
138 * @sg: job->reply_payload.sg_list
139 * @nents: job->reply_payload.sg_cnt
140 * @offset: The offset into the response data
141 */
142struct fc_bsg_info {
143 struct fc_bsg_job *job;
144 struct fc_lport *lport;
145 u16 rsp_code;
146 struct scatterlist *sg;
147 u32 nents;
148 size_t offset;
149};
150
151/**
152 * fc_frame_drop() - Dummy frame handler
153 * @lport: The local port the frame was received on
154 * @fp: The received frame
155 */
127static int fc_frame_drop(struct fc_lport *lport, struct fc_frame *fp) 156static int fc_frame_drop(struct fc_lport *lport, struct fc_frame *fp)
128{ 157{
129 fc_frame_free(fp); 158 fc_frame_free(fp);
@@ -150,8 +179,8 @@ static void fc_lport_rport_callback(struct fc_lport *lport,
150 switch (event) { 179 switch (event) {
151 case RPORT_EV_READY: 180 case RPORT_EV_READY:
152 if (lport->state == LPORT_ST_DNS) { 181 if (lport->state == LPORT_ST_DNS) {
153 lport->dns_rp = rdata; 182 lport->dns_rdata = rdata;
154 fc_lport_enter_rpn_id(lport); 183 fc_lport_enter_ns(lport, LPORT_ST_RNN_ID);
155 } else { 184 } else {
156 FC_LPORT_DBG(lport, "Received an READY event " 185 FC_LPORT_DBG(lport, "Received an READY event "
157 "on port (%6x) for the directory " 186 "on port (%6x) for the directory "
@@ -165,7 +194,7 @@ static void fc_lport_rport_callback(struct fc_lport *lport,
165 case RPORT_EV_LOGO: 194 case RPORT_EV_LOGO:
166 case RPORT_EV_FAILED: 195 case RPORT_EV_FAILED:
167 case RPORT_EV_STOP: 196 case RPORT_EV_STOP:
168 lport->dns_rp = NULL; 197 lport->dns_rdata = NULL;
169 break; 198 break;
170 case RPORT_EV_NONE: 199 case RPORT_EV_NONE:
171 break; 200 break;
@@ -189,8 +218,8 @@ static const char *fc_lport_state(struct fc_lport *lport)
189 218
190/** 219/**
191 * fc_lport_ptp_setup() - Create an rport for point-to-point mode 220 * fc_lport_ptp_setup() - Create an rport for point-to-point mode
192 * @lport: The lport to attach the ptp rport to 221 * @lport: The lport to attach the ptp rport to
193 * @fid: The FID of the ptp rport 222 * @remote_fid: The FID of the ptp rport
194 * @remote_wwpn: The WWPN of the ptp rport 223 * @remote_wwpn: The WWPN of the ptp rport
195 * @remote_wwnn: The WWNN of the ptp rport 224 * @remote_wwnn: The WWNN of the ptp rport
196 */ 225 */
@@ -199,18 +228,22 @@ static void fc_lport_ptp_setup(struct fc_lport *lport,
199 u64 remote_wwnn) 228 u64 remote_wwnn)
200{ 229{
201 mutex_lock(&lport->disc.disc_mutex); 230 mutex_lock(&lport->disc.disc_mutex);
202 if (lport->ptp_rp) 231 if (lport->ptp_rdata)
203 lport->tt.rport_logoff(lport->ptp_rp); 232 lport->tt.rport_logoff(lport->ptp_rdata);
204 lport->ptp_rp = lport->tt.rport_create(lport, remote_fid); 233 lport->ptp_rdata = lport->tt.rport_create(lport, remote_fid);
205 lport->ptp_rp->ids.port_name = remote_wwpn; 234 lport->ptp_rdata->ids.port_name = remote_wwpn;
206 lport->ptp_rp->ids.node_name = remote_wwnn; 235 lport->ptp_rdata->ids.node_name = remote_wwnn;
207 mutex_unlock(&lport->disc.disc_mutex); 236 mutex_unlock(&lport->disc.disc_mutex);
208 237
209 lport->tt.rport_login(lport->ptp_rp); 238 lport->tt.rport_login(lport->ptp_rdata);
210 239
211 fc_lport_enter_ready(lport); 240 fc_lport_enter_ready(lport);
212} 241}
213 242
243/**
244 * fc_get_host_port_type() - Return the port type of the given Scsi_Host
245 * @shost: The SCSI host whose port type is to be determined
246 */
214void fc_get_host_port_type(struct Scsi_Host *shost) 247void fc_get_host_port_type(struct Scsi_Host *shost)
215{ 248{
216 /* TODO - currently just NPORT */ 249 /* TODO - currently just NPORT */
@@ -218,17 +251,33 @@ void fc_get_host_port_type(struct Scsi_Host *shost)
218} 251}
219EXPORT_SYMBOL(fc_get_host_port_type); 252EXPORT_SYMBOL(fc_get_host_port_type);
220 253
254/**
255 * fc_get_host_port_state() - Return the port state of the given Scsi_Host
256 * @shost: The SCSI host whose port state is to be determined
257 */
221void fc_get_host_port_state(struct Scsi_Host *shost) 258void fc_get_host_port_state(struct Scsi_Host *shost)
222{ 259{
223 struct fc_lport *lp = shost_priv(shost); 260 struct fc_lport *lport = shost_priv(shost);
224 261
225 if (lp->link_up) 262 mutex_lock(&lport->lp_mutex);
226 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE; 263 if (!lport->link_up)
264 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
227 else 265 else
228 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE; 266 switch (lport->state) {
267 case LPORT_ST_READY:
268 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
269 break;
270 default:
271 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
272 }
273 mutex_unlock(&lport->lp_mutex);
229} 274}
230EXPORT_SYMBOL(fc_get_host_port_state); 275EXPORT_SYMBOL(fc_get_host_port_state);
231 276
277/**
278 * fc_get_host_speed() - Return the speed of the given Scsi_Host
279 * @shost: The SCSI host whose port speed is to be determined
280 */
232void fc_get_host_speed(struct Scsi_Host *shost) 281void fc_get_host_speed(struct Scsi_Host *shost)
233{ 282{
234 struct fc_lport *lport = shost_priv(shost); 283 struct fc_lport *lport = shost_priv(shost);
@@ -237,24 +286,28 @@ void fc_get_host_speed(struct Scsi_Host *shost)
237} 286}
238EXPORT_SYMBOL(fc_get_host_speed); 287EXPORT_SYMBOL(fc_get_host_speed);
239 288
289/**
290 * fc_get_host_stats() - Return the Scsi_Host's statistics
291 * @shost: The SCSI host whose statistics are to be returned
292 */
240struct fc_host_statistics *fc_get_host_stats(struct Scsi_Host *shost) 293struct fc_host_statistics *fc_get_host_stats(struct Scsi_Host *shost)
241{ 294{
242 struct fc_host_statistics *fcoe_stats; 295 struct fc_host_statistics *fcoe_stats;
243 struct fc_lport *lp = shost_priv(shost); 296 struct fc_lport *lport = shost_priv(shost);
244 struct timespec v0, v1; 297 struct timespec v0, v1;
245 unsigned int cpu; 298 unsigned int cpu;
246 299
247 fcoe_stats = &lp->host_stats; 300 fcoe_stats = &lport->host_stats;
248 memset(fcoe_stats, 0, sizeof(struct fc_host_statistics)); 301 memset(fcoe_stats, 0, sizeof(struct fc_host_statistics));
249 302
250 jiffies_to_timespec(jiffies, &v0); 303 jiffies_to_timespec(jiffies, &v0);
251 jiffies_to_timespec(lp->boot_time, &v1); 304 jiffies_to_timespec(lport->boot_time, &v1);
252 fcoe_stats->seconds_since_last_reset = (v0.tv_sec - v1.tv_sec); 305 fcoe_stats->seconds_since_last_reset = (v0.tv_sec - v1.tv_sec);
253 306
254 for_each_possible_cpu(cpu) { 307 for_each_possible_cpu(cpu) {
255 struct fcoe_dev_stats *stats; 308 struct fcoe_dev_stats *stats;
256 309
257 stats = per_cpu_ptr(lp->dev_stats, cpu); 310 stats = per_cpu_ptr(lport->dev_stats, cpu);
258 311
259 fcoe_stats->tx_frames += stats->TxFrames; 312 fcoe_stats->tx_frames += stats->TxFrames;
260 fcoe_stats->tx_words += stats->TxWords; 313 fcoe_stats->tx_words += stats->TxWords;
@@ -279,12 +332,15 @@ struct fc_host_statistics *fc_get_host_stats(struct Scsi_Host *shost)
279} 332}
280EXPORT_SYMBOL(fc_get_host_stats); 333EXPORT_SYMBOL(fc_get_host_stats);
281 334
282/* 335/**
283 * Fill in FLOGI command for request. 336 * fc_lport_flogi_fill() - Fill in FLOGI command for request
337 * @lport: The local port the FLOGI is for
338 * @flogi: The FLOGI command
339 * @op: The opcode
284 */ 340 */
285static void 341static void fc_lport_flogi_fill(struct fc_lport *lport,
286fc_lport_flogi_fill(struct fc_lport *lport, struct fc_els_flogi *flogi, 342 struct fc_els_flogi *flogi,
287 unsigned int op) 343 unsigned int op)
288{ 344{
289 struct fc_els_csp *sp; 345 struct fc_els_csp *sp;
290 struct fc_els_cssp *cp; 346 struct fc_els_cssp *cp;
@@ -312,8 +368,10 @@ fc_lport_flogi_fill(struct fc_lport *lport, struct fc_els_flogi *flogi,
312 } 368 }
313} 369}
314 370
315/* 371/**
316 * Add a supported FC-4 type. 372 * fc_lport_add_fc4_type() - Add a supported FC-4 type to a local port
373 * @lport: The local port to add a new FC-4 type to
374 * @type: The new FC-4 type
317 */ 375 */
318static void fc_lport_add_fc4_type(struct fc_lport *lport, enum fc_fh_type type) 376static void fc_lport_add_fc4_type(struct fc_lport *lport, enum fc_fh_type type)
319{ 377{
@@ -325,11 +383,11 @@ static void fc_lport_add_fc4_type(struct fc_lport *lport, enum fc_fh_type type)
325 383
326/** 384/**
327 * fc_lport_recv_rlir_req() - Handle received Registered Link Incident Report. 385 * fc_lport_recv_rlir_req() - Handle received Registered Link Incident Report.
386 * @sp: The sequence in the RLIR exchange
387 * @fp: The RLIR request frame
328 * @lport: Fibre Channel local port recieving the RLIR 388 * @lport: Fibre Channel local port recieving the RLIR
329 * @sp: current sequence in the RLIR exchange
330 * @fp: RLIR request frame
331 * 389 *
332 * Locking Note: The lport lock is exected to be held before calling 390 * Locking Note: The lport lock is expected to be held before calling
333 * this function. 391 * this function.
334 */ 392 */
335static void fc_lport_recv_rlir_req(struct fc_seq *sp, struct fc_frame *fp, 393static void fc_lport_recv_rlir_req(struct fc_seq *sp, struct fc_frame *fp,
@@ -344,11 +402,11 @@ static void fc_lport_recv_rlir_req(struct fc_seq *sp, struct fc_frame *fp,
344 402
345/** 403/**
346 * fc_lport_recv_echo_req() - Handle received ECHO request 404 * fc_lport_recv_echo_req() - Handle received ECHO request
347 * @lport: Fibre Channel local port recieving the ECHO 405 * @sp: The sequence in the ECHO exchange
348 * @sp: current sequence in the ECHO exchange 406 * @fp: ECHO request frame
349 * @fp: ECHO request frame 407 * @lport: The local port recieving the ECHO
350 * 408 *
351 * Locking Note: The lport lock is exected to be held before calling 409 * Locking Note: The lport lock is expected to be held before calling
352 * this function. 410 * this function.
353 */ 411 */
354static void fc_lport_recv_echo_req(struct fc_seq *sp, struct fc_frame *in_fp, 412static void fc_lport_recv_echo_req(struct fc_seq *sp, struct fc_frame *in_fp,
@@ -361,7 +419,7 @@ static void fc_lport_recv_echo_req(struct fc_seq *sp, struct fc_frame *in_fp,
361 void *dp; 419 void *dp;
362 u32 f_ctl; 420 u32 f_ctl;
363 421
364 FC_LPORT_DBG(lport, "Received RLIR request while in state %s\n", 422 FC_LPORT_DBG(lport, "Received ECHO request while in state %s\n",
365 fc_lport_state(lport)); 423 fc_lport_state(lport));
366 424
367 len = fr_len(in_fp) - sizeof(struct fc_frame_header); 425 len = fr_len(in_fp) - sizeof(struct fc_frame_header);
@@ -374,7 +432,7 @@ static void fc_lport_recv_echo_req(struct fc_seq *sp, struct fc_frame *in_fp,
374 if (fp) { 432 if (fp) {
375 dp = fc_frame_payload_get(fp, len); 433 dp = fc_frame_payload_get(fp, len);
376 memcpy(dp, pp, len); 434 memcpy(dp, pp, len);
377 *((u32 *)dp) = htonl(ELS_LS_ACC << 24); 435 *((__be32 *)dp) = htonl(ELS_LS_ACC << 24);
378 sp = lport->tt.seq_start_next(sp); 436 sp = lport->tt.seq_start_next(sp);
379 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ; 437 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
380 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid, 438 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
@@ -385,12 +443,12 @@ static void fc_lport_recv_echo_req(struct fc_seq *sp, struct fc_frame *in_fp,
385} 443}
386 444
387/** 445/**
388 * fc_lport_recv_echo_req() - Handle received Request Node ID data request 446 * fc_lport_recv_rnid_req() - Handle received Request Node ID data request
389 * @lport: Fibre Channel local port recieving the RNID 447 * @sp: The sequence in the RNID exchange
390 * @sp: current sequence in the RNID exchange 448 * @fp: The RNID request frame
391 * @fp: RNID request frame 449 * @lport: The local port recieving the RNID
392 * 450 *
393 * Locking Note: The lport lock is exected to be held before calling 451 * Locking Note: The lport lock is expected to be held before calling
394 * this function. 452 * this function.
395 */ 453 */
396static void fc_lport_recv_rnid_req(struct fc_seq *sp, struct fc_frame *in_fp, 454static void fc_lport_recv_rnid_req(struct fc_seq *sp, struct fc_frame *in_fp,
@@ -453,9 +511,9 @@ static void fc_lport_recv_rnid_req(struct fc_seq *sp, struct fc_frame *in_fp,
453 511
454/** 512/**
455 * fc_lport_recv_logo_req() - Handle received fabric LOGO request 513 * fc_lport_recv_logo_req() - Handle received fabric LOGO request
456 * @lport: Fibre Channel local port recieving the LOGO 514 * @sp: The sequence in the LOGO exchange
457 * @sp: current sequence in the LOGO exchange 515 * @fp: The LOGO request frame
458 * @fp: LOGO request frame 516 * @lport: The local port recieving the LOGO
459 * 517 *
460 * Locking Note: The lport lock is exected to be held before calling 518 * Locking Note: The lport lock is exected to be held before calling
461 * this function. 519 * this function.
@@ -470,7 +528,7 @@ static void fc_lport_recv_logo_req(struct fc_seq *sp, struct fc_frame *fp,
470 528
471/** 529/**
472 * fc_fabric_login() - Start the lport state machine 530 * fc_fabric_login() - Start the lport state machine
473 * @lport: The lport that should log into the fabric 531 * @lport: The local port that should log into the fabric
474 * 532 *
475 * Locking Note: This function should not be called 533 * Locking Note: This function should not be called
476 * with the lport lock held. 534 * with the lport lock held.
@@ -480,7 +538,9 @@ int fc_fabric_login(struct fc_lport *lport)
480 int rc = -1; 538 int rc = -1;
481 539
482 mutex_lock(&lport->lp_mutex); 540 mutex_lock(&lport->lp_mutex);
483 if (lport->state == LPORT_ST_DISABLED) { 541 if (lport->state == LPORT_ST_DISABLED ||
542 lport->state == LPORT_ST_LOGO) {
543 fc_lport_state_enter(lport, LPORT_ST_RESET);
484 fc_lport_enter_reset(lport); 544 fc_lport_enter_reset(lport);
485 rc = 0; 545 rc = 0;
486 } 546 }
@@ -491,47 +551,69 @@ int fc_fabric_login(struct fc_lport *lport)
491EXPORT_SYMBOL(fc_fabric_login); 551EXPORT_SYMBOL(fc_fabric_login);
492 552
493/** 553/**
494 * fc_linkup() - Handler for transport linkup events 554 * __fc_linkup() - Handler for transport linkup events
495 * @lport: The lport whose link is up 555 * @lport: The lport whose link is up
556 *
557 * Locking: must be called with the lp_mutex held
496 */ 558 */
497void fc_linkup(struct fc_lport *lport) 559void __fc_linkup(struct fc_lport *lport)
498{ 560{
499 printk(KERN_INFO "libfc: Link up on port (%6x)\n",
500 fc_host_port_id(lport->host));
501
502 mutex_lock(&lport->lp_mutex);
503 if (!lport->link_up) { 561 if (!lport->link_up) {
504 lport->link_up = 1; 562 lport->link_up = 1;
505 563
506 if (lport->state == LPORT_ST_RESET) 564 if (lport->state == LPORT_ST_RESET)
507 fc_lport_enter_flogi(lport); 565 fc_lport_enter_flogi(lport);
508 } 566 }
567}
568
569/**
570 * fc_linkup() - Handler for transport linkup events
571 * @lport: The local port whose link is up
572 */
573void fc_linkup(struct fc_lport *lport)
574{
575 printk(KERN_INFO "host%d: libfc: Link up on port (%6x)\n",
576 lport->host->host_no, fc_host_port_id(lport->host));
577
578 mutex_lock(&lport->lp_mutex);
579 __fc_linkup(lport);
509 mutex_unlock(&lport->lp_mutex); 580 mutex_unlock(&lport->lp_mutex);
510} 581}
511EXPORT_SYMBOL(fc_linkup); 582EXPORT_SYMBOL(fc_linkup);
512 583
513/** 584/**
514 * fc_linkdown() - Handler for transport linkdown events 585 * __fc_linkdown() - Handler for transport linkdown events
515 * @lport: The lport whose link is down 586 * @lport: The lport whose link is down
587 *
588 * Locking: must be called with the lp_mutex held
516 */ 589 */
517void fc_linkdown(struct fc_lport *lport) 590void __fc_linkdown(struct fc_lport *lport)
518{ 591{
519 mutex_lock(&lport->lp_mutex);
520 printk(KERN_INFO "libfc: Link down on port (%6x)\n",
521 fc_host_port_id(lport->host));
522
523 if (lport->link_up) { 592 if (lport->link_up) {
524 lport->link_up = 0; 593 lport->link_up = 0;
525 fc_lport_enter_reset(lport); 594 fc_lport_enter_reset(lport);
526 lport->tt.fcp_cleanup(lport); 595 lport->tt.fcp_cleanup(lport);
527 } 596 }
597}
598
599/**
600 * fc_linkdown() - Handler for transport linkdown events
601 * @lport: The local port whose link is down
602 */
603void fc_linkdown(struct fc_lport *lport)
604{
605 printk(KERN_INFO "host%d: libfc: Link down on port (%6x)\n",
606 lport->host->host_no, fc_host_port_id(lport->host));
607
608 mutex_lock(&lport->lp_mutex);
609 __fc_linkdown(lport);
528 mutex_unlock(&lport->lp_mutex); 610 mutex_unlock(&lport->lp_mutex);
529} 611}
530EXPORT_SYMBOL(fc_linkdown); 612EXPORT_SYMBOL(fc_linkdown);
531 613
532/** 614/**
533 * fc_fabric_logoff() - Logout of the fabric 615 * fc_fabric_logoff() - Logout of the fabric
534 * @lport: fc_lport pointer to logoff the fabric 616 * @lport: The local port to logoff the fabric
535 * 617 *
536 * Return value: 618 * Return value:
537 * 0 for success, -1 for failure 619 * 0 for success, -1 for failure
@@ -540,8 +622,8 @@ int fc_fabric_logoff(struct fc_lport *lport)
540{ 622{
541 lport->tt.disc_stop_final(lport); 623 lport->tt.disc_stop_final(lport);
542 mutex_lock(&lport->lp_mutex); 624 mutex_lock(&lport->lp_mutex);
543 if (lport->dns_rp) 625 if (lport->dns_rdata)
544 lport->tt.rport_logoff(lport->dns_rp); 626 lport->tt.rport_logoff(lport->dns_rdata);
545 mutex_unlock(&lport->lp_mutex); 627 mutex_unlock(&lport->lp_mutex);
546 lport->tt.rport_flush_queue(); 628 lport->tt.rport_flush_queue();
547 mutex_lock(&lport->lp_mutex); 629 mutex_lock(&lport->lp_mutex);
@@ -553,11 +635,9 @@ int fc_fabric_logoff(struct fc_lport *lport)
553EXPORT_SYMBOL(fc_fabric_logoff); 635EXPORT_SYMBOL(fc_fabric_logoff);
554 636
555/** 637/**
556 * fc_lport_destroy() - unregister a fc_lport 638 * fc_lport_destroy() - Unregister a fc_lport
557 * @lport: fc_lport pointer to unregister 639 * @lport: The local port to unregister
558 * 640 *
559 * Return value:
560 * None
561 * Note: 641 * Note:
562 * exit routine for fc_lport instance 642 * exit routine for fc_lport instance
563 * clean-up all the allocated memory 643 * clean-up all the allocated memory
@@ -580,13 +660,9 @@ int fc_lport_destroy(struct fc_lport *lport)
580EXPORT_SYMBOL(fc_lport_destroy); 660EXPORT_SYMBOL(fc_lport_destroy);
581 661
582/** 662/**
583 * fc_set_mfs() - sets up the mfs for the corresponding fc_lport 663 * fc_set_mfs() - Set the maximum frame size for a local port
584 * @lport: fc_lport pointer to unregister 664 * @lport: The local port to set the MFS for
585 * @mfs: the new mfs for fc_lport 665 * @mfs: The new MFS
586 *
587 * Set mfs for the given fc_lport to the new mfs.
588 *
589 * Return: 0 for success
590 */ 666 */
591int fc_set_mfs(struct fc_lport *lport, u32 mfs) 667int fc_set_mfs(struct fc_lport *lport, u32 mfs)
592{ 668{
@@ -617,7 +693,7 @@ EXPORT_SYMBOL(fc_set_mfs);
617 693
618/** 694/**
619 * fc_lport_disc_callback() - Callback for discovery events 695 * fc_lport_disc_callback() - Callback for discovery events
620 * @lport: FC local port 696 * @lport: The local port receiving the event
621 * @event: The discovery event 697 * @event: The discovery event
622 */ 698 */
623void fc_lport_disc_callback(struct fc_lport *lport, enum fc_disc_event event) 699void fc_lport_disc_callback(struct fc_lport *lport, enum fc_disc_event event)
@@ -627,8 +703,9 @@ void fc_lport_disc_callback(struct fc_lport *lport, enum fc_disc_event event)
627 FC_LPORT_DBG(lport, "Discovery succeeded\n"); 703 FC_LPORT_DBG(lport, "Discovery succeeded\n");
628 break; 704 break;
629 case DISC_EV_FAILED: 705 case DISC_EV_FAILED:
630 printk(KERN_ERR "libfc: Discovery failed for port (%6x)\n", 706 printk(KERN_ERR "host%d: libfc: "
631 fc_host_port_id(lport->host)); 707 "Discovery failed for port (%6x)\n",
708 lport->host->host_no, fc_host_port_id(lport->host));
632 mutex_lock(&lport->lp_mutex); 709 mutex_lock(&lport->lp_mutex);
633 fc_lport_enter_reset(lport); 710 fc_lport_enter_reset(lport);
634 mutex_unlock(&lport->lp_mutex); 711 mutex_unlock(&lport->lp_mutex);
@@ -641,7 +718,7 @@ void fc_lport_disc_callback(struct fc_lport *lport, enum fc_disc_event event)
641 718
642/** 719/**
643 * fc_rport_enter_ready() - Enter the ready state and start discovery 720 * fc_rport_enter_ready() - Enter the ready state and start discovery
644 * @lport: Fibre Channel local port that is ready 721 * @lport: The local port that is ready
645 * 722 *
646 * Locking Note: The lport lock is expected to be held before calling 723 * Locking Note: The lport lock is expected to be held before calling
647 * this routine. 724 * this routine.
@@ -652,22 +729,46 @@ static void fc_lport_enter_ready(struct fc_lport *lport)
652 fc_lport_state(lport)); 729 fc_lport_state(lport));
653 730
654 fc_lport_state_enter(lport, LPORT_ST_READY); 731 fc_lport_state_enter(lport, LPORT_ST_READY);
732 if (lport->vport)
733 fc_vport_set_state(lport->vport, FC_VPORT_ACTIVE);
734 fc_vports_linkchange(lport);
655 735
656 if (!lport->ptp_rp) 736 if (!lport->ptp_rdata)
657 lport->tt.disc_start(fc_lport_disc_callback, lport); 737 lport->tt.disc_start(fc_lport_disc_callback, lport);
658} 738}
659 739
660/** 740/**
741 * fc_lport_set_port_id() - set the local port Port ID
742 * @lport: The local port which will have its Port ID set.
743 * @port_id: The new port ID.
744 * @fp: The frame containing the incoming request, or NULL.
745 *
746 * Locking Note: The lport lock is expected to be held before calling
747 * this function.
748 */
749static void fc_lport_set_port_id(struct fc_lport *lport, u32 port_id,
750 struct fc_frame *fp)
751{
752 if (port_id)
753 printk(KERN_INFO "host%d: Assigned Port ID %6x\n",
754 lport->host->host_no, port_id);
755
756 fc_host_port_id(lport->host) = port_id;
757 if (lport->tt.lport_set_port_id)
758 lport->tt.lport_set_port_id(lport, port_id, fp);
759}
760
761/**
661 * fc_lport_recv_flogi_req() - Receive a FLOGI request 762 * fc_lport_recv_flogi_req() - Receive a FLOGI request
662 * @sp_in: The sequence the FLOGI is on 763 * @sp_in: The sequence the FLOGI is on
663 * @rx_fp: The frame the FLOGI is in 764 * @rx_fp: The FLOGI frame
664 * @lport: The lport that recieved the request 765 * @lport: The local port that recieved the request
665 * 766 *
666 * A received FLOGI request indicates a point-to-point connection. 767 * A received FLOGI request indicates a point-to-point connection.
667 * Accept it with the common service parameters indicating our N port. 768 * Accept it with the common service parameters indicating our N port.
668 * Set up to do a PLOGI if we have the higher-number WWPN. 769 * Set up to do a PLOGI if we have the higher-number WWPN.
669 * 770 *
670 * Locking Note: The lport lock is exected to be held before calling 771 * Locking Note: The lport lock is expected to be held before calling
671 * this function. 772 * this function.
672 */ 773 */
673static void fc_lport_recv_flogi_req(struct fc_seq *sp_in, 774static void fc_lport_recv_flogi_req(struct fc_seq *sp_in,
@@ -695,8 +796,9 @@ static void fc_lport_recv_flogi_req(struct fc_seq *sp_in,
695 goto out; 796 goto out;
696 remote_wwpn = get_unaligned_be64(&flp->fl_wwpn); 797 remote_wwpn = get_unaligned_be64(&flp->fl_wwpn);
697 if (remote_wwpn == lport->wwpn) { 798 if (remote_wwpn == lport->wwpn) {
698 printk(KERN_WARNING "libfc: Received FLOGI from port " 799 printk(KERN_WARNING "host%d: libfc: Received FLOGI from port "
699 "with same WWPN %llx\n", remote_wwpn); 800 "with same WWPN %llx\n",
801 lport->host->host_no, remote_wwpn);
700 goto out; 802 goto out;
701 } 803 }
702 FC_LPORT_DBG(lport, "FLOGI from port WWPN %llx\n", remote_wwpn); 804 FC_LPORT_DBG(lport, "FLOGI from port WWPN %llx\n", remote_wwpn);
@@ -715,7 +817,7 @@ static void fc_lport_recv_flogi_req(struct fc_seq *sp_in,
715 remote_fid = FC_LOCAL_PTP_FID_HI; 817 remote_fid = FC_LOCAL_PTP_FID_HI;
716 } 818 }
717 819
718 fc_host_port_id(lport->host) = local_fid; 820 fc_lport_set_port_id(lport, local_fid, rx_fp);
719 821
720 fp = fc_frame_alloc(lport, sizeof(*flp)); 822 fp = fc_frame_alloc(lport, sizeof(*flp));
721 if (fp) { 823 if (fp) {
@@ -747,9 +849,9 @@ out:
747 849
748/** 850/**
749 * fc_lport_recv_req() - The generic lport request handler 851 * fc_lport_recv_req() - The generic lport request handler
750 * @lport: The lport that received the request 852 * @lport: The local port that received the request
751 * @sp: The sequence the request is on 853 * @sp: The sequence the request is on
752 * @fp: The frame the request is in 854 * @fp: The request frame
753 * 855 *
754 * This function will see if the lport handles the request or 856 * This function will see if the lport handles the request or
755 * if an rport should handle the request. 857 * if an rport should handle the request.
@@ -817,8 +919,8 @@ static void fc_lport_recv_req(struct fc_lport *lport, struct fc_seq *sp,
817} 919}
818 920
819/** 921/**
820 * fc_lport_reset() - Reset an lport 922 * fc_lport_reset() - Reset a local port
821 * @lport: The lport which should be reset 923 * @lport: The local port which should be reset
822 * 924 *
823 * Locking Note: This functions should not be called with the 925 * Locking Note: This functions should not be called with the
824 * lport lock held. 926 * lport lock held.
@@ -834,29 +936,31 @@ int fc_lport_reset(struct fc_lport *lport)
834EXPORT_SYMBOL(fc_lport_reset); 936EXPORT_SYMBOL(fc_lport_reset);
835 937
836/** 938/**
837 * fc_lport_reset_locked() - Reset the local port 939 * fc_lport_reset_locked() - Reset the local port w/ the lport lock held
838 * @lport: Fibre Channel local port to be reset 940 * @lport: The local port to be reset
839 * 941 *
840 * Locking Note: The lport lock is expected to be held before calling 942 * Locking Note: The lport lock is expected to be held before calling
841 * this routine. 943 * this routine.
842 */ 944 */
843static void fc_lport_reset_locked(struct fc_lport *lport) 945static void fc_lport_reset_locked(struct fc_lport *lport)
844{ 946{
845 if (lport->dns_rp) 947 if (lport->dns_rdata)
846 lport->tt.rport_logoff(lport->dns_rp); 948 lport->tt.rport_logoff(lport->dns_rdata);
847 949
848 lport->ptp_rp = NULL; 950 lport->ptp_rdata = NULL;
849 951
850 lport->tt.disc_stop(lport); 952 lport->tt.disc_stop(lport);
851 953
852 lport->tt.exch_mgr_reset(lport, 0, 0); 954 lport->tt.exch_mgr_reset(lport, 0, 0);
853 fc_host_fabric_name(lport->host) = 0; 955 fc_host_fabric_name(lport->host) = 0;
854 fc_host_port_id(lport->host) = 0; 956
957 if (fc_host_port_id(lport->host))
958 fc_lport_set_port_id(lport, 0, NULL);
855} 959}
856 960
857/** 961/**
858 * fc_lport_enter_reset() - Reset the local port 962 * fc_lport_enter_reset() - Reset the local port
859 * @lport: Fibre Channel local port to be reset 963 * @lport: The local port to be reset
860 * 964 *
861 * Locking Note: The lport lock is expected to be held before calling 965 * Locking Note: The lport lock is expected to be held before calling
862 * this routine. 966 * this routine.
@@ -866,15 +970,25 @@ static void fc_lport_enter_reset(struct fc_lport *lport)
866 FC_LPORT_DBG(lport, "Entered RESET state from %s state\n", 970 FC_LPORT_DBG(lport, "Entered RESET state from %s state\n",
867 fc_lport_state(lport)); 971 fc_lport_state(lport));
868 972
973 if (lport->state == LPORT_ST_DISABLED || lport->state == LPORT_ST_LOGO)
974 return;
975
976 if (lport->vport) {
977 if (lport->link_up)
978 fc_vport_set_state(lport->vport, FC_VPORT_INITIALIZING);
979 else
980 fc_vport_set_state(lport->vport, FC_VPORT_LINKDOWN);
981 }
869 fc_lport_state_enter(lport, LPORT_ST_RESET); 982 fc_lport_state_enter(lport, LPORT_ST_RESET);
983 fc_vports_linkchange(lport);
870 fc_lport_reset_locked(lport); 984 fc_lport_reset_locked(lport);
871 if (lport->link_up) 985 if (lport->link_up)
872 fc_lport_enter_flogi(lport); 986 fc_lport_enter_flogi(lport);
873} 987}
874 988
875/** 989/**
876 * fc_lport_enter_disabled() - disable the local port 990 * fc_lport_enter_disabled() - Disable the local port
877 * @lport: Fibre Channel local port to be reset 991 * @lport: The local port to be reset
878 * 992 *
879 * Locking Note: The lport lock is expected to be held before calling 993 * Locking Note: The lport lock is expected to be held before calling
880 * this routine. 994 * this routine.
@@ -885,13 +999,14 @@ static void fc_lport_enter_disabled(struct fc_lport *lport)
885 fc_lport_state(lport)); 999 fc_lport_state(lport));
886 1000
887 fc_lport_state_enter(lport, LPORT_ST_DISABLED); 1001 fc_lport_state_enter(lport, LPORT_ST_DISABLED);
1002 fc_vports_linkchange(lport);
888 fc_lport_reset_locked(lport); 1003 fc_lport_reset_locked(lport);
889} 1004}
890 1005
891/** 1006/**
892 * fc_lport_error() - Handler for any errors 1007 * fc_lport_error() - Handler for any errors
893 * @lport: The fc_lport object 1008 * @lport: The local port that the error was on
894 * @fp: The frame pointer 1009 * @fp: The error code encoded in a frame pointer
895 * 1010 *
896 * If the error was caused by a resource allocation failure 1011 * If the error was caused by a resource allocation failure
897 * then wait for half a second and retry, otherwise retry 1012 * then wait for half a second and retry, otherwise retry
@@ -922,8 +1037,11 @@ static void fc_lport_error(struct fc_lport *lport, struct fc_frame *fp)
922 case LPORT_ST_DISABLED: 1037 case LPORT_ST_DISABLED:
923 case LPORT_ST_READY: 1038 case LPORT_ST_READY:
924 case LPORT_ST_RESET: 1039 case LPORT_ST_RESET:
925 case LPORT_ST_RPN_ID: 1040 case LPORT_ST_RNN_ID:
1041 case LPORT_ST_RSNN_NN:
1042 case LPORT_ST_RSPN_ID:
926 case LPORT_ST_RFT_ID: 1043 case LPORT_ST_RFT_ID:
1044 case LPORT_ST_RFF_ID:
927 case LPORT_ST_SCR: 1045 case LPORT_ST_SCR:
928 case LPORT_ST_DNS: 1046 case LPORT_ST_DNS:
929 case LPORT_ST_FLOGI: 1047 case LPORT_ST_FLOGI:
@@ -936,33 +1054,33 @@ static void fc_lport_error(struct fc_lport *lport, struct fc_frame *fp)
936} 1054}
937 1055
938/** 1056/**
939 * fc_lport_rft_id_resp() - Handle response to Register Fibre 1057 * fc_lport_ns_resp() - Handle response to a name server
940 * Channel Types by ID (RPN_ID) request 1058 * registration exchange
941 * @sp: current sequence in RPN_ID exchange 1059 * @sp: current sequence in exchange
942 * @fp: response frame 1060 * @fp: response frame
943 * @lp_arg: Fibre Channel host port instance 1061 * @lp_arg: Fibre Channel host port instance
944 * 1062 *
945 * Locking Note: This function will be called without the lport lock 1063 * Locking Note: This function will be called without the lport lock
946 * held, but it will lock, call an _enter_* function or fc_lport_error 1064 * held, but it will lock, call an _enter_* function or fc_lport_error()
947 * and then unlock the lport. 1065 * and then unlock the lport.
948 */ 1066 */
949static void fc_lport_rft_id_resp(struct fc_seq *sp, struct fc_frame *fp, 1067static void fc_lport_ns_resp(struct fc_seq *sp, struct fc_frame *fp,
950 void *lp_arg) 1068 void *lp_arg)
951{ 1069{
952 struct fc_lport *lport = lp_arg; 1070 struct fc_lport *lport = lp_arg;
953 struct fc_frame_header *fh; 1071 struct fc_frame_header *fh;
954 struct fc_ct_hdr *ct; 1072 struct fc_ct_hdr *ct;
955 1073
956 FC_LPORT_DBG(lport, "Received a RFT_ID %s\n", fc_els_resp_type(fp)); 1074 FC_LPORT_DBG(lport, "Received a ns %s\n", fc_els_resp_type(fp));
957 1075
958 if (fp == ERR_PTR(-FC_EX_CLOSED)) 1076 if (fp == ERR_PTR(-FC_EX_CLOSED))
959 return; 1077 return;
960 1078
961 mutex_lock(&lport->lp_mutex); 1079 mutex_lock(&lport->lp_mutex);
962 1080
963 if (lport->state != LPORT_ST_RFT_ID) { 1081 if (lport->state < LPORT_ST_RNN_ID || lport->state > LPORT_ST_RFF_ID) {
964 FC_LPORT_DBG(lport, "Received a RFT_ID response, but in state " 1082 FC_LPORT_DBG(lport, "Received a name server response, "
965 "%s\n", fc_lport_state(lport)); 1083 "but in state %s\n", fc_lport_state(lport));
966 if (IS_ERR(fp)) 1084 if (IS_ERR(fp))
967 goto err; 1085 goto err;
968 goto out; 1086 goto out;
@@ -980,63 +1098,28 @@ static void fc_lport_rft_id_resp(struct fc_seq *sp, struct fc_frame *fp,
980 ct->ct_fs_type == FC_FST_DIR && 1098 ct->ct_fs_type == FC_FST_DIR &&
981 ct->ct_fs_subtype == FC_NS_SUBTYPE && 1099 ct->ct_fs_subtype == FC_NS_SUBTYPE &&
982 ntohs(ct->ct_cmd) == FC_FS_ACC) 1100 ntohs(ct->ct_cmd) == FC_FS_ACC)
983 fc_lport_enter_scr(lport); 1101 switch (lport->state) {
984 else 1102 case LPORT_ST_RNN_ID:
985 fc_lport_error(lport, fp); 1103 fc_lport_enter_ns(lport, LPORT_ST_RSNN_NN);
986out: 1104 break;
987 fc_frame_free(fp); 1105 case LPORT_ST_RSNN_NN:
988err: 1106 fc_lport_enter_ns(lport, LPORT_ST_RSPN_ID);
989 mutex_unlock(&lport->lp_mutex); 1107 break;
990} 1108 case LPORT_ST_RSPN_ID:
991 1109 fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
992/** 1110 break;
993 * fc_lport_rpn_id_resp() - Handle response to Register Port 1111 case LPORT_ST_RFT_ID:
994 * Name by ID (RPN_ID) request 1112 fc_lport_enter_ns(lport, LPORT_ST_RFF_ID);
995 * @sp: current sequence in RPN_ID exchange 1113 break;
996 * @fp: response frame 1114 case LPORT_ST_RFF_ID:
997 * @lp_arg: Fibre Channel host port instance 1115 fc_lport_enter_scr(lport);
998 * 1116 break;
999 * Locking Note: This function will be called without the lport lock 1117 default:
1000 * held, but it will lock, call an _enter_* function or fc_lport_error 1118 /* should have already been caught by state checks */
1001 * and then unlock the lport. 1119 break;
1002 */ 1120 }
1003static void fc_lport_rpn_id_resp(struct fc_seq *sp, struct fc_frame *fp,
1004 void *lp_arg)
1005{
1006 struct fc_lport *lport = lp_arg;
1007 struct fc_frame_header *fh;
1008 struct fc_ct_hdr *ct;
1009
1010 FC_LPORT_DBG(lport, "Received a RPN_ID %s\n", fc_els_resp_type(fp));
1011
1012 if (fp == ERR_PTR(-FC_EX_CLOSED))
1013 return;
1014
1015 mutex_lock(&lport->lp_mutex);
1016
1017 if (lport->state != LPORT_ST_RPN_ID) {
1018 FC_LPORT_DBG(lport, "Received a RPN_ID response, but in state "
1019 "%s\n", fc_lport_state(lport));
1020 if (IS_ERR(fp))
1021 goto err;
1022 goto out;
1023 }
1024
1025 if (IS_ERR(fp)) {
1026 fc_lport_error(lport, fp);
1027 goto err;
1028 }
1029
1030 fh = fc_frame_header_get(fp);
1031 ct = fc_frame_payload_get(fp, sizeof(*ct));
1032 if (fh && ct && fh->fh_type == FC_TYPE_CT &&
1033 ct->ct_fs_type == FC_FST_DIR &&
1034 ct->ct_fs_subtype == FC_NS_SUBTYPE &&
1035 ntohs(ct->ct_cmd) == FC_FS_ACC)
1036 fc_lport_enter_rft_id(lport);
1037 else 1121 else
1038 fc_lport_error(lport, fp); 1122 fc_lport_error(lport, fp);
1039
1040out: 1123out:
1041 fc_frame_free(fp); 1124 fc_frame_free(fp);
1042err: 1125err:
@@ -1045,8 +1128,8 @@ err:
1045 1128
1046/** 1129/**
1047 * fc_lport_scr_resp() - Handle response to State Change Register (SCR) request 1130 * fc_lport_scr_resp() - Handle response to State Change Register (SCR) request
1048 * @sp: current sequence in SCR exchange 1131 * @sp: current sequence in SCR exchange
1049 * @fp: response frame 1132 * @fp: response frame
1050 * @lp_arg: Fibre Channel lport port instance that sent the registration request 1133 * @lp_arg: Fibre Channel lport port instance that sent the registration request
1051 * 1134 *
1052 * Locking Note: This function will be called without the lport lock 1135 * Locking Note: This function will be called without the lport lock
@@ -1092,8 +1175,8 @@ err:
1092} 1175}
1093 1176
1094/** 1177/**
1095 * fc_lport_enter_scr() - Send a State Change Register (SCR) request 1178 * fc_lport_enter_scr() - Send a SCR (State Change Register) request
1096 * @lport: Fibre Channel local port to register for state changes 1179 * @lport: The local port to register for state changes
1097 * 1180 *
1098 * Locking Note: The lport lock is expected to be held before calling 1181 * Locking Note: The lport lock is expected to be held before calling
1099 * this routine. 1182 * this routine.
@@ -1114,78 +1197,74 @@ static void fc_lport_enter_scr(struct fc_lport *lport)
1114 } 1197 }
1115 1198
1116 if (!lport->tt.elsct_send(lport, FC_FID_FCTRL, fp, ELS_SCR, 1199 if (!lport->tt.elsct_send(lport, FC_FID_FCTRL, fp, ELS_SCR,
1117 fc_lport_scr_resp, lport, lport->e_d_tov)) 1200 fc_lport_scr_resp, lport,
1118 fc_lport_error(lport, fp); 1201 2 * lport->r_a_tov))
1202 fc_lport_error(lport, NULL);
1119} 1203}
1120 1204
1121/** 1205/**
1122 * fc_lport_enter_rft_id() - Register FC4-types with the name server 1206 * fc_lport_enter_ns() - register some object with the name server
1123 * @lport: Fibre Channel local port to register 1207 * @lport: Fibre Channel local port to register
1124 * 1208 *
1125 * Locking Note: The lport lock is expected to be held before calling 1209 * Locking Note: The lport lock is expected to be held before calling
1126 * this routine. 1210 * this routine.
1127 */ 1211 */
1128static void fc_lport_enter_rft_id(struct fc_lport *lport) 1212static void fc_lport_enter_ns(struct fc_lport *lport, enum fc_lport_state state)
1129{ 1213{
1130 struct fc_frame *fp; 1214 struct fc_frame *fp;
1131 struct fc_ns_fts *lps; 1215 enum fc_ns_req cmd;
1132 int i; 1216 int size = sizeof(struct fc_ct_hdr);
1217 size_t len;
1133 1218
1134 FC_LPORT_DBG(lport, "Entered RFT_ID state from %s state\n", 1219 FC_LPORT_DBG(lport, "Entered %s state from %s state\n",
1220 fc_lport_state_names[state],
1135 fc_lport_state(lport)); 1221 fc_lport_state(lport));
1136 1222
1137 fc_lport_state_enter(lport, LPORT_ST_RFT_ID); 1223 fc_lport_state_enter(lport, state);
1138 1224
1139 lps = &lport->fcts; 1225 switch (state) {
1140 i = sizeof(lps->ff_type_map) / sizeof(lps->ff_type_map[0]); 1226 case LPORT_ST_RNN_ID:
1141 while (--i >= 0) 1227 cmd = FC_NS_RNN_ID;
1142 if (ntohl(lps->ff_type_map[i]) != 0) 1228 size += sizeof(struct fc_ns_rn_id);
1143 break; 1229 break;
1144 if (i < 0) { 1230 case LPORT_ST_RSNN_NN:
1145 /* nothing to register, move on to SCR */ 1231 len = strnlen(fc_host_symbolic_name(lport->host), 255);
1146 fc_lport_enter_scr(lport); 1232 /* if there is no symbolic name, skip to RFT_ID */
1147 return; 1233 if (!len)
1148 } 1234 return fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
1149 1235 cmd = FC_NS_RSNN_NN;
1150 fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) + 1236 size += sizeof(struct fc_ns_rsnn) + len;
1151 sizeof(struct fc_ns_rft)); 1237 break;
1152 if (!fp) { 1238 case LPORT_ST_RSPN_ID:
1153 fc_lport_error(lport, fp); 1239 len = strnlen(fc_host_symbolic_name(lport->host), 255);
1240 /* if there is no symbolic name, skip to RFT_ID */
1241 if (!len)
1242 return fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
1243 cmd = FC_NS_RSPN_ID;
1244 size += sizeof(struct fc_ns_rspn) + len;
1245 break;
1246 case LPORT_ST_RFT_ID:
1247 cmd = FC_NS_RFT_ID;
1248 size += sizeof(struct fc_ns_rft);
1249 break;
1250 case LPORT_ST_RFF_ID:
1251 cmd = FC_NS_RFF_ID;
1252 size += sizeof(struct fc_ns_rff_id);
1253 break;
1254 default:
1255 fc_lport_error(lport, NULL);
1154 return; 1256 return;
1155 } 1257 }
1156 1258
1157 if (!lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RFT_ID, 1259 fp = fc_frame_alloc(lport, size);
1158 fc_lport_rft_id_resp,
1159 lport, lport->e_d_tov))
1160 fc_lport_error(lport, fp);
1161}
1162
1163/**
1164 * fc_rport_enter_rft_id() - Register port name with the name server
1165 * @lport: Fibre Channel local port to register
1166 *
1167 * Locking Note: The lport lock is expected to be held before calling
1168 * this routine.
1169 */
1170static void fc_lport_enter_rpn_id(struct fc_lport *lport)
1171{
1172 struct fc_frame *fp;
1173
1174 FC_LPORT_DBG(lport, "Entered RPN_ID state from %s state\n",
1175 fc_lport_state(lport));
1176
1177 fc_lport_state_enter(lport, LPORT_ST_RPN_ID);
1178
1179 fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
1180 sizeof(struct fc_ns_rn_id));
1181 if (!fp) { 1260 if (!fp) {
1182 fc_lport_error(lport, fp); 1261 fc_lport_error(lport, fp);
1183 return; 1262 return;
1184 } 1263 }
1185 1264
1186 if (!lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RPN_ID, 1265 if (!lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, cmd,
1187 fc_lport_rpn_id_resp, 1266 fc_lport_ns_resp,
1188 lport, lport->e_d_tov)) 1267 lport, 3 * lport->r_a_tov))
1189 fc_lport_error(lport, fp); 1268 fc_lport_error(lport, fp);
1190} 1269}
1191 1270
@@ -1194,8 +1273,8 @@ static struct fc_rport_operations fc_lport_rport_ops = {
1194}; 1273};
1195 1274
1196/** 1275/**
1197 * fc_rport_enter_dns() - Create a rport to the name server 1276 * fc_rport_enter_dns() - Create a fc_rport for the name server
1198 * @lport: Fibre Channel local port requesting a rport for the name server 1277 * @lport: The local port requesting a remote port for the name server
1199 * 1278 *
1200 * Locking Note: The lport lock is expected to be held before calling 1279 * Locking Note: The lport lock is expected to be held before calling
1201 * this routine. 1280 * this routine.
@@ -1224,8 +1303,8 @@ err:
1224} 1303}
1225 1304
1226/** 1305/**
1227 * fc_lport_timeout() - Handler for the retry_work timer. 1306 * fc_lport_timeout() - Handler for the retry_work timer
1228 * @work: The work struct of the fc_lport 1307 * @work: The work struct of the local port
1229 */ 1308 */
1230static void fc_lport_timeout(struct work_struct *work) 1309static void fc_lport_timeout(struct work_struct *work)
1231{ 1310{
@@ -1237,21 +1316,25 @@ static void fc_lport_timeout(struct work_struct *work)
1237 1316
1238 switch (lport->state) { 1317 switch (lport->state) {
1239 case LPORT_ST_DISABLED: 1318 case LPORT_ST_DISABLED:
1319 WARN_ON(1);
1320 break;
1240 case LPORT_ST_READY: 1321 case LPORT_ST_READY:
1241 case LPORT_ST_RESET:
1242 WARN_ON(1); 1322 WARN_ON(1);
1243 break; 1323 break;
1324 case LPORT_ST_RESET:
1325 break;
1244 case LPORT_ST_FLOGI: 1326 case LPORT_ST_FLOGI:
1245 fc_lport_enter_flogi(lport); 1327 fc_lport_enter_flogi(lport);
1246 break; 1328 break;
1247 case LPORT_ST_DNS: 1329 case LPORT_ST_DNS:
1248 fc_lport_enter_dns(lport); 1330 fc_lport_enter_dns(lport);
1249 break; 1331 break;
1250 case LPORT_ST_RPN_ID: 1332 case LPORT_ST_RNN_ID:
1251 fc_lport_enter_rpn_id(lport); 1333 case LPORT_ST_RSNN_NN:
1252 break; 1334 case LPORT_ST_RSPN_ID:
1253 case LPORT_ST_RFT_ID: 1335 case LPORT_ST_RFT_ID:
1254 fc_lport_enter_rft_id(lport); 1336 case LPORT_ST_RFF_ID:
1337 fc_lport_enter_ns(lport, lport->state);
1255 break; 1338 break;
1256 case LPORT_ST_SCR: 1339 case LPORT_ST_SCR:
1257 fc_lport_enter_scr(lport); 1340 fc_lport_enter_scr(lport);
@@ -1266,16 +1349,16 @@ static void fc_lport_timeout(struct work_struct *work)
1266 1349
1267/** 1350/**
1268 * fc_lport_logo_resp() - Handle response to LOGO request 1351 * fc_lport_logo_resp() - Handle response to LOGO request
1269 * @sp: current sequence in LOGO exchange 1352 * @sp: The sequence that the LOGO was on
1270 * @fp: response frame 1353 * @fp: The LOGO frame
1271 * @lp_arg: Fibre Channel lport port instance that sent the LOGO request 1354 * @lp_arg: The lport port that received the LOGO request
1272 * 1355 *
1273 * Locking Note: This function will be called without the lport lock 1356 * Locking Note: This function will be called without the lport lock
1274 * held, but it will lock, call an _enter_* function or fc_lport_error 1357 * held, but it will lock, call an _enter_* function or fc_lport_error()
1275 * and then unlock the lport. 1358 * and then unlock the lport.
1276 */ 1359 */
1277static void fc_lport_logo_resp(struct fc_seq *sp, struct fc_frame *fp, 1360void fc_lport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
1278 void *lp_arg) 1361 void *lp_arg)
1279{ 1362{
1280 struct fc_lport *lport = lp_arg; 1363 struct fc_lport *lport = lp_arg;
1281 u8 op; 1364 u8 op;
@@ -1311,10 +1394,11 @@ out:
1311err: 1394err:
1312 mutex_unlock(&lport->lp_mutex); 1395 mutex_unlock(&lport->lp_mutex);
1313} 1396}
1397EXPORT_SYMBOL(fc_lport_logo_resp);
1314 1398
1315/** 1399/**
1316 * fc_rport_enter_logo() - Logout of the fabric 1400 * fc_rport_enter_logo() - Logout of the fabric
1317 * @lport: Fibre Channel local port to be logged out 1401 * @lport: The local port to be logged out
1318 * 1402 *
1319 * Locking Note: The lport lock is expected to be held before calling 1403 * Locking Note: The lport lock is expected to be held before calling
1320 * this routine. 1404 * this routine.
@@ -1328,6 +1412,7 @@ static void fc_lport_enter_logo(struct fc_lport *lport)
1328 fc_lport_state(lport)); 1412 fc_lport_state(lport));
1329 1413
1330 fc_lport_state_enter(lport, LPORT_ST_LOGO); 1414 fc_lport_state_enter(lport, LPORT_ST_LOGO);
1415 fc_vports_linkchange(lport);
1331 1416
1332 fp = fc_frame_alloc(lport, sizeof(*logo)); 1417 fp = fc_frame_alloc(lport, sizeof(*logo));
1333 if (!fp) { 1418 if (!fp) {
@@ -1336,22 +1421,23 @@ static void fc_lport_enter_logo(struct fc_lport *lport)
1336 } 1421 }
1337 1422
1338 if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp, ELS_LOGO, 1423 if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp, ELS_LOGO,
1339 fc_lport_logo_resp, lport, lport->e_d_tov)) 1424 fc_lport_logo_resp, lport,
1340 fc_lport_error(lport, fp); 1425 2 * lport->r_a_tov))
1426 fc_lport_error(lport, NULL);
1341} 1427}
1342 1428
1343/** 1429/**
1344 * fc_lport_flogi_resp() - Handle response to FLOGI request 1430 * fc_lport_flogi_resp() - Handle response to FLOGI request
1345 * @sp: current sequence in FLOGI exchange 1431 * @sp: The sequence that the FLOGI was on
1346 * @fp: response frame 1432 * @fp: The FLOGI response frame
1347 * @lp_arg: Fibre Channel lport port instance that sent the FLOGI request 1433 * @lp_arg: The lport port that received the FLOGI response
1348 * 1434 *
1349 * Locking Note: This function will be called without the lport lock 1435 * Locking Note: This function will be called without the lport lock
1350 * held, but it will lock, call an _enter_* function or fc_lport_error 1436 * held, but it will lock, call an _enter_* function or fc_lport_error()
1351 * and then unlock the lport. 1437 * and then unlock the lport.
1352 */ 1438 */
1353static void fc_lport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp, 1439void fc_lport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
1354 void *lp_arg) 1440 void *lp_arg)
1355{ 1441{
1356 struct fc_lport *lport = lp_arg; 1442 struct fc_lport *lport = lp_arg;
1357 struct fc_frame_header *fh; 1443 struct fc_frame_header *fh;
@@ -1385,11 +1471,6 @@ static void fc_lport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
1385 fh = fc_frame_header_get(fp); 1471 fh = fc_frame_header_get(fp);
1386 did = ntoh24(fh->fh_d_id); 1472 did = ntoh24(fh->fh_d_id);
1387 if (fc_frame_payload_op(fp) == ELS_LS_ACC && did != 0) { 1473 if (fc_frame_payload_op(fp) == ELS_LS_ACC && did != 0) {
1388
1389 printk(KERN_INFO "libfc: Assigned FID (%6x) in FLOGI response\n",
1390 did);
1391 fc_host_port_id(lport->host) = did;
1392
1393 flp = fc_frame_payload_get(fp, sizeof(*flp)); 1474 flp = fc_frame_payload_get(fp, sizeof(*flp));
1394 if (flp) { 1475 if (flp) {
1395 mfs = ntohs(flp->fl_csp.sp_bb_data) & 1476 mfs = ntohs(flp->fl_csp.sp_bb_data) &
@@ -1402,12 +1483,18 @@ static void fc_lport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
1402 e_d_tov = ntohl(flp->fl_csp.sp_e_d_tov); 1483 e_d_tov = ntohl(flp->fl_csp.sp_e_d_tov);
1403 if (csp_flags & FC_SP_FT_EDTR) 1484 if (csp_flags & FC_SP_FT_EDTR)
1404 e_d_tov /= 1000000; 1485 e_d_tov /= 1000000;
1486
1487 lport->npiv_enabled = !!(csp_flags & FC_SP_FT_NPIV_ACC);
1488
1405 if ((csp_flags & FC_SP_FT_FPORT) == 0) { 1489 if ((csp_flags & FC_SP_FT_FPORT) == 0) {
1406 if (e_d_tov > lport->e_d_tov) 1490 if (e_d_tov > lport->e_d_tov)
1407 lport->e_d_tov = e_d_tov; 1491 lport->e_d_tov = e_d_tov;
1408 lport->r_a_tov = 2 * e_d_tov; 1492 lport->r_a_tov = 2 * e_d_tov;
1409 printk(KERN_INFO "libfc: Port (%6x) entered " 1493 fc_lport_set_port_id(lport, did, fp);
1410 "point to point mode\n", did); 1494 printk(KERN_INFO "host%d: libfc: "
1495 "Port (%6x) entered "
1496 "point-to-point mode\n",
1497 lport->host->host_no, did);
1411 fc_lport_ptp_setup(lport, ntoh24(fh->fh_s_id), 1498 fc_lport_ptp_setup(lport, ntoh24(fh->fh_s_id),
1412 get_unaligned_be64( 1499 get_unaligned_be64(
1413 &flp->fl_wwpn), 1500 &flp->fl_wwpn),
@@ -1418,6 +1505,7 @@ static void fc_lport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
1418 lport->r_a_tov = r_a_tov; 1505 lport->r_a_tov = r_a_tov;
1419 fc_host_fabric_name(lport->host) = 1506 fc_host_fabric_name(lport->host) =
1420 get_unaligned_be64(&flp->fl_wwnn); 1507 get_unaligned_be64(&flp->fl_wwnn);
1508 fc_lport_set_port_id(lport, did, fp);
1421 fc_lport_enter_dns(lport); 1509 fc_lport_enter_dns(lport);
1422 } 1510 }
1423 } 1511 }
@@ -1430,6 +1518,7 @@ out:
1430err: 1518err:
1431 mutex_unlock(&lport->lp_mutex); 1519 mutex_unlock(&lport->lp_mutex);
1432} 1520}
1521EXPORT_SYMBOL(fc_lport_flogi_resp);
1433 1522
1434/** 1523/**
1435 * fc_rport_enter_flogi() - Send a FLOGI request to the fabric manager 1524 * fc_rport_enter_flogi() - Send a FLOGI request to the fabric manager
@@ -1451,12 +1540,18 @@ void fc_lport_enter_flogi(struct fc_lport *lport)
1451 if (!fp) 1540 if (!fp)
1452 return fc_lport_error(lport, fp); 1541 return fc_lport_error(lport, fp);
1453 1542
1454 if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp, ELS_FLOGI, 1543 if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp,
1455 fc_lport_flogi_resp, lport, lport->e_d_tov)) 1544 lport->vport ? ELS_FDISC : ELS_FLOGI,
1456 fc_lport_error(lport, fp); 1545 fc_lport_flogi_resp, lport,
1546 lport->vport ? 2 * lport->r_a_tov :
1547 lport->e_d_tov))
1548 fc_lport_error(lport, NULL);
1457} 1549}
1458 1550
1459/* Configure a fc_lport */ 1551/**
1552 * fc_lport_config() - Configure a fc_lport
1553 * @lport: The local port to be configured
1554 */
1460int fc_lport_config(struct fc_lport *lport) 1555int fc_lport_config(struct fc_lport *lport)
1461{ 1556{
1462 INIT_DELAYED_WORK(&lport->retry_work, fc_lport_timeout); 1557 INIT_DELAYED_WORK(&lport->retry_work, fc_lport_timeout);
@@ -1471,6 +1566,10 @@ int fc_lport_config(struct fc_lport *lport)
1471} 1566}
1472EXPORT_SYMBOL(fc_lport_config); 1567EXPORT_SYMBOL(fc_lport_config);
1473 1568
1569/**
1570 * fc_lport_init() - Initialize the lport layer for a local port
1571 * @lport: The local port to initialize the exchange layer for
1572 */
1474int fc_lport_init(struct fc_lport *lport) 1573int fc_lport_init(struct fc_lport *lport)
1475{ 1574{
1476 if (!lport->tt.lport_recv) 1575 if (!lport->tt.lport_recv)
@@ -1500,7 +1599,254 @@ int fc_lport_init(struct fc_lport *lport)
1500 if (lport->link_supported_speeds & FC_PORTSPEED_10GBIT) 1599 if (lport->link_supported_speeds & FC_PORTSPEED_10GBIT)
1501 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_10GBIT; 1600 fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_10GBIT;
1502 1601
1503 INIT_LIST_HEAD(&lport->ema_list);
1504 return 0; 1602 return 0;
1505} 1603}
1506EXPORT_SYMBOL(fc_lport_init); 1604EXPORT_SYMBOL(fc_lport_init);
1605
1606/**
1607 * fc_lport_bsg_resp() - The common response handler for FC Passthrough requests
1608 * @sp: The sequence for the FC Passthrough response
1609 * @fp: The response frame
1610 * @info_arg: The BSG info that the response is for
1611 */
1612static void fc_lport_bsg_resp(struct fc_seq *sp, struct fc_frame *fp,
1613 void *info_arg)
1614{
1615 struct fc_bsg_info *info = info_arg;
1616 struct fc_bsg_job *job = info->job;
1617 struct fc_lport *lport = info->lport;
1618 struct fc_frame_header *fh;
1619 size_t len;
1620 void *buf;
1621
1622 if (IS_ERR(fp)) {
1623 job->reply->result = (PTR_ERR(fp) == -FC_EX_CLOSED) ?
1624 -ECONNABORTED : -ETIMEDOUT;
1625 job->reply_len = sizeof(uint32_t);
1626 job->state_flags |= FC_RQST_STATE_DONE;
1627 job->job_done(job);
1628 kfree(info);
1629 return;
1630 }
1631
1632 mutex_lock(&lport->lp_mutex);
1633 fh = fc_frame_header_get(fp);
1634 len = fr_len(fp) - sizeof(*fh);
1635 buf = fc_frame_payload_get(fp, 0);
1636
1637 if (fr_sof(fp) == FC_SOF_I3 && !ntohs(fh->fh_seq_cnt)) {
1638 /* Get the response code from the first frame payload */
1639 unsigned short cmd = (info->rsp_code == FC_FS_ACC) ?
1640 ntohs(((struct fc_ct_hdr *)buf)->ct_cmd) :
1641 (unsigned short)fc_frame_payload_op(fp);
1642
1643 /* Save the reply status of the job */
1644 job->reply->reply_data.ctels_reply.status =
1645 (cmd == info->rsp_code) ?
1646 FC_CTELS_STATUS_OK : FC_CTELS_STATUS_REJECT;
1647 }
1648
1649 job->reply->reply_payload_rcv_len +=
1650 fc_copy_buffer_to_sglist(buf, len, info->sg, &info->nents,
1651 &info->offset, KM_BIO_SRC_IRQ, NULL);
1652
1653 if (fr_eof(fp) == FC_EOF_T &&
1654 (ntoh24(fh->fh_f_ctl) & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
1655 (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
1656 if (job->reply->reply_payload_rcv_len >
1657 job->reply_payload.payload_len)
1658 job->reply->reply_payload_rcv_len =
1659 job->reply_payload.payload_len;
1660 job->reply->result = 0;
1661 job->state_flags |= FC_RQST_STATE_DONE;
1662 job->job_done(job);
1663 kfree(info);
1664 }
1665 fc_frame_free(fp);
1666 mutex_unlock(&lport->lp_mutex);
1667}
1668
1669/**
1670 * fc_lport_els_request() - Send ELS passthrough request
1671 * @job: The BSG Passthrough job
1672 * @lport: The local port sending the request
1673 * @did: The destination port id
1674 *
1675 * Locking Note: The lport lock is expected to be held before calling
1676 * this routine.
1677 */
1678static int fc_lport_els_request(struct fc_bsg_job *job,
1679 struct fc_lport *lport,
1680 u32 did, u32 tov)
1681{
1682 struct fc_bsg_info *info;
1683 struct fc_frame *fp;
1684 struct fc_frame_header *fh;
1685 char *pp;
1686 int len;
1687
1688 fp = fc_frame_alloc(lport, job->request_payload.payload_len);
1689 if (!fp)
1690 return -ENOMEM;
1691
1692 len = job->request_payload.payload_len;
1693 pp = fc_frame_payload_get(fp, len);
1694
1695 sg_copy_to_buffer(job->request_payload.sg_list,
1696 job->request_payload.sg_cnt,
1697 pp, len);
1698
1699 fh = fc_frame_header_get(fp);
1700 fh->fh_r_ctl = FC_RCTL_ELS_REQ;
1701 hton24(fh->fh_d_id, did);
1702 hton24(fh->fh_s_id, fc_host_port_id(lport->host));
1703 fh->fh_type = FC_TYPE_ELS;
1704 hton24(fh->fh_f_ctl, FC_FC_FIRST_SEQ |
1705 FC_FC_END_SEQ | FC_FC_SEQ_INIT);
1706 fh->fh_cs_ctl = 0;
1707 fh->fh_df_ctl = 0;
1708 fh->fh_parm_offset = 0;
1709
1710 info = kzalloc(sizeof(struct fc_bsg_info), GFP_KERNEL);
1711 if (!info) {
1712 fc_frame_free(fp);
1713 return -ENOMEM;
1714 }
1715
1716 info->job = job;
1717 info->lport = lport;
1718 info->rsp_code = ELS_LS_ACC;
1719 info->nents = job->reply_payload.sg_cnt;
1720 info->sg = job->reply_payload.sg_list;
1721
1722 if (!lport->tt.exch_seq_send(lport, fp, fc_lport_bsg_resp,
1723 NULL, info, tov))
1724 return -ECOMM;
1725 return 0;
1726}
1727
1728/**
1729 * fc_lport_ct_request() - Send CT Passthrough request
1730 * @job: The BSG Passthrough job
1731 * @lport: The local port sending the request
1732 * @did: The destination FC-ID
1733 * @tov: The timeout period to wait for the response
1734 *
1735 * Locking Note: The lport lock is expected to be held before calling
1736 * this routine.
1737 */
1738static int fc_lport_ct_request(struct fc_bsg_job *job,
1739 struct fc_lport *lport, u32 did, u32 tov)
1740{
1741 struct fc_bsg_info *info;
1742 struct fc_frame *fp;
1743 struct fc_frame_header *fh;
1744 struct fc_ct_req *ct;
1745 size_t len;
1746
1747 fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
1748 job->request_payload.payload_len);
1749 if (!fp)
1750 return -ENOMEM;
1751
1752 len = job->request_payload.payload_len;
1753 ct = fc_frame_payload_get(fp, len);
1754
1755 sg_copy_to_buffer(job->request_payload.sg_list,
1756 job->request_payload.sg_cnt,
1757 ct, len);
1758
1759 fh = fc_frame_header_get(fp);
1760 fh->fh_r_ctl = FC_RCTL_DD_UNSOL_CTL;
1761 hton24(fh->fh_d_id, did);
1762 hton24(fh->fh_s_id, fc_host_port_id(lport->host));
1763 fh->fh_type = FC_TYPE_CT;
1764 hton24(fh->fh_f_ctl, FC_FC_FIRST_SEQ |
1765 FC_FC_END_SEQ | FC_FC_SEQ_INIT);
1766 fh->fh_cs_ctl = 0;
1767 fh->fh_df_ctl = 0;
1768 fh->fh_parm_offset = 0;
1769
1770 info = kzalloc(sizeof(struct fc_bsg_info), GFP_KERNEL);
1771 if (!info) {
1772 fc_frame_free(fp);
1773 return -ENOMEM;
1774 }
1775
1776 info->job = job;
1777 info->lport = lport;
1778 info->rsp_code = FC_FS_ACC;
1779 info->nents = job->reply_payload.sg_cnt;
1780 info->sg = job->reply_payload.sg_list;
1781
1782 if (!lport->tt.exch_seq_send(lport, fp, fc_lport_bsg_resp,
1783 NULL, info, tov))
1784 return -ECOMM;
1785 return 0;
1786}
1787
1788/**
1789 * fc_lport_bsg_request() - The common entry point for sending
1790 * FC Passthrough requests
1791 * @job: The BSG passthrough job
1792 */
1793int fc_lport_bsg_request(struct fc_bsg_job *job)
1794{
1795 struct request *rsp = job->req->next_rq;
1796 struct Scsi_Host *shost = job->shost;
1797 struct fc_lport *lport = shost_priv(shost);
1798 struct fc_rport *rport;
1799 struct fc_rport_priv *rdata;
1800 int rc = -EINVAL;
1801 u32 did;
1802
1803 job->reply->reply_payload_rcv_len = 0;
1804 if (rsp)
1805 rsp->resid_len = job->reply_payload.payload_len;
1806
1807 mutex_lock(&lport->lp_mutex);
1808
1809 switch (job->request->msgcode) {
1810 case FC_BSG_RPT_ELS:
1811 rport = job->rport;
1812 if (!rport)
1813 break;
1814
1815 rdata = rport->dd_data;
1816 rc = fc_lport_els_request(job, lport, rport->port_id,
1817 rdata->e_d_tov);
1818 break;
1819
1820 case FC_BSG_RPT_CT:
1821 rport = job->rport;
1822 if (!rport)
1823 break;
1824
1825 rdata = rport->dd_data;
1826 rc = fc_lport_ct_request(job, lport, rport->port_id,
1827 rdata->e_d_tov);
1828 break;
1829
1830 case FC_BSG_HST_CT:
1831 did = ntoh24(job->request->rqst_data.h_ct.port_id);
1832 if (did == FC_FID_DIR_SERV)
1833 rdata = lport->dns_rdata;
1834 else
1835 rdata = lport->tt.rport_lookup(lport, did);
1836
1837 if (!rdata)
1838 break;
1839
1840 rc = fc_lport_ct_request(job, lport, did, rdata->e_d_tov);
1841 break;
1842
1843 case FC_BSG_HST_ELS_NOLOGIN:
1844 did = ntoh24(job->request->rqst_data.h_els.port_id);
1845 rc = fc_lport_els_request(job, lport, did, lport->e_d_tov);
1846 break;
1847 }
1848
1849 mutex_unlock(&lport->lp_mutex);
1850 return rc;
1851}
1852EXPORT_SYMBOL(fc_lport_bsg_request);
diff --git a/drivers/scsi/libfc/fc_npiv.c b/drivers/scsi/libfc/fc_npiv.c
new file mode 100644
index 000000000000..c68f6c7341c2
--- /dev/null
+++ b/drivers/scsi/libfc/fc_npiv.c
@@ -0,0 +1,161 @@
1/*
2 * Copyright(c) 2009 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 * NPIV VN_Port helper functions for libfc
22 */
23
24#include <scsi/libfc.h>
25
26/**
27 * fc_vport_create() - Create a new NPIV vport instance
28 * @vport: fc_vport structure from scsi_transport_fc
29 * @privsize: driver private data size to allocate along with the Scsi_Host
30 */
31
32struct fc_lport *libfc_vport_create(struct fc_vport *vport, int privsize)
33{
34 struct Scsi_Host *shost = vport_to_shost(vport);
35 struct fc_lport *n_port = shost_priv(shost);
36 struct fc_lport *vn_port;
37
38 vn_port = libfc_host_alloc(shost->hostt, privsize);
39 if (!vn_port)
40 goto err_out;
41 if (fc_exch_mgr_list_clone(n_port, vn_port))
42 goto err_put;
43
44 vn_port->vport = vport;
45 vport->dd_data = vn_port;
46
47 mutex_lock(&n_port->lp_mutex);
48 list_add_tail(&vn_port->list, &n_port->vports);
49 mutex_unlock(&n_port->lp_mutex);
50
51 return vn_port;
52
53err_put:
54 scsi_host_put(vn_port->host);
55err_out:
56 return NULL;
57}
58EXPORT_SYMBOL(libfc_vport_create);
59
60/**
61 * fc_vport_id_lookup() - find NPIV lport that matches a given fabric ID
62 * @n_port: Top level N_Port which may have multiple NPIV VN_Ports
63 * @port_id: Fabric ID to find a match for
64 *
65 * Returns: matching lport pointer or NULL if there is no match
66 */
67struct fc_lport *fc_vport_id_lookup(struct fc_lport *n_port, u32 port_id)
68{
69 struct fc_lport *lport = NULL;
70 struct fc_lport *vn_port;
71
72 if (fc_host_port_id(n_port->host) == port_id)
73 return n_port;
74
75 mutex_lock(&n_port->lp_mutex);
76 list_for_each_entry(vn_port, &n_port->vports, list) {
77 if (fc_host_port_id(vn_port->host) == port_id) {
78 lport = vn_port;
79 break;
80 }
81 }
82 mutex_unlock(&n_port->lp_mutex);
83
84 return lport;
85}
86
87/*
88 * When setting the link state of vports during an lport state change, it's
89 * necessary to hold the lp_mutex of both the N_Port and the VN_Port.
90 * This tells the lockdep engine to treat the nested locking of the VN_Port
91 * as a different lock class.
92 */
93enum libfc_lport_mutex_class {
94 LPORT_MUTEX_NORMAL = 0,
95 LPORT_MUTEX_VN_PORT = 1,
96};
97
98/**
99 * __fc_vport_setlink() - update link and status on a VN_Port
100 * @n_port: parent N_Port
101 * @vn_port: VN_Port to update
102 *
103 * Locking: must be called with both the N_Port and VN_Port lp_mutex held
104 */
105static void __fc_vport_setlink(struct fc_lport *n_port,
106 struct fc_lport *vn_port)
107{
108 struct fc_vport *vport = vn_port->vport;
109
110 if (vn_port->state == LPORT_ST_DISABLED)
111 return;
112
113 if (n_port->state == LPORT_ST_READY) {
114 if (n_port->npiv_enabled) {
115 fc_vport_set_state(vport, FC_VPORT_INITIALIZING);
116 __fc_linkup(vn_port);
117 } else {
118 fc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
119 __fc_linkdown(vn_port);
120 }
121 } else {
122 fc_vport_set_state(vport, FC_VPORT_LINKDOWN);
123 __fc_linkdown(vn_port);
124 }
125}
126
127/**
128 * fc_vport_setlink() - update link and status on a VN_Port
129 * @vn_port: virtual port to update
130 */
131void fc_vport_setlink(struct fc_lport *vn_port)
132{
133 struct fc_vport *vport = vn_port->vport;
134 struct Scsi_Host *shost = vport_to_shost(vport);
135 struct fc_lport *n_port = shost_priv(shost);
136
137 mutex_lock(&n_port->lp_mutex);
138 mutex_lock_nested(&vn_port->lp_mutex, LPORT_MUTEX_VN_PORT);
139 __fc_vport_setlink(n_port, vn_port);
140 mutex_unlock(&vn_port->lp_mutex);
141 mutex_unlock(&n_port->lp_mutex);
142}
143EXPORT_SYMBOL(fc_vport_setlink);
144
145/**
146 * fc_vports_linkchange() - change the link state of all vports
147 * @n_port: Parent N_Port that has changed state
148 *
149 * Locking: called with the n_port lp_mutex held
150 */
151void fc_vports_linkchange(struct fc_lport *n_port)
152{
153 struct fc_lport *vn_port;
154
155 list_for_each_entry(vn_port, &n_port->vports, list) {
156 mutex_lock_nested(&vn_port->lp_mutex, LPORT_MUTEX_VN_PORT);
157 __fc_vport_setlink(n_port, vn_port);
158 mutex_unlock(&vn_port->lp_mutex);
159 }
160}
161
diff --git a/drivers/scsi/libfc/fc_rport.c b/drivers/scsi/libfc/fc_rport.c
index 03ea6748e7ee..b37d0ff28b35 100644
--- a/drivers/scsi/libfc/fc_rport.c
+++ b/drivers/scsi/libfc/fc_rport.c
@@ -47,6 +47,7 @@
47#include <linux/kernel.h> 47#include <linux/kernel.h>
48#include <linux/spinlock.h> 48#include <linux/spinlock.h>
49#include <linux/interrupt.h> 49#include <linux/interrupt.h>
50#include <linux/slab.h>
50#include <linux/rcupdate.h> 51#include <linux/rcupdate.h>
51#include <linux/timer.h> 52#include <linux/timer.h>
52#include <linux/workqueue.h> 53#include <linux/workqueue.h>
@@ -55,6 +56,8 @@
55#include <scsi/libfc.h> 56#include <scsi/libfc.h>
56#include <scsi/fc_encode.h> 57#include <scsi/fc_encode.h>
57 58
59#include "fc_libfc.h"
60
58struct workqueue_struct *rport_event_queue; 61struct workqueue_struct *rport_event_queue;
59 62
60static void fc_rport_enter_plogi(struct fc_rport_priv *); 63static void fc_rport_enter_plogi(struct fc_rport_priv *);
@@ -86,12 +89,13 @@ static const char *fc_rport_state_names[] = {
86 [RPORT_ST_LOGO] = "LOGO", 89 [RPORT_ST_LOGO] = "LOGO",
87 [RPORT_ST_ADISC] = "ADISC", 90 [RPORT_ST_ADISC] = "ADISC",
88 [RPORT_ST_DELETE] = "Delete", 91 [RPORT_ST_DELETE] = "Delete",
92 [RPORT_ST_RESTART] = "Restart",
89}; 93};
90 94
91/** 95/**
92 * fc_rport_lookup() - lookup a remote port by port_id 96 * fc_rport_lookup() - Lookup a remote port by port_id
93 * @lport: Fibre Channel host port instance 97 * @lport: The local port to lookup the remote port on
94 * @port_id: remote port port_id to match 98 * @port_id: The remote port ID to look up
95 */ 99 */
96static struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport, 100static struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport,
97 u32 port_id) 101 u32 port_id)
@@ -99,16 +103,17 @@ static struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport,
99 struct fc_rport_priv *rdata; 103 struct fc_rport_priv *rdata;
100 104
101 list_for_each_entry(rdata, &lport->disc.rports, peers) 105 list_for_each_entry(rdata, &lport->disc.rports, peers)
102 if (rdata->ids.port_id == port_id && 106 if (rdata->ids.port_id == port_id)
103 rdata->rp_state != RPORT_ST_DELETE)
104 return rdata; 107 return rdata;
105 return NULL; 108 return NULL;
106} 109}
107 110
108/** 111/**
109 * fc_rport_create() - Create a new remote port 112 * fc_rport_create() - Create a new remote port
110 * @lport: The local port that the new remote port is for 113 * @lport: The local port this remote port will be associated with
111 * @port_id: The port ID for the new remote port 114 * @ids: The identifiers for the new remote port
115 *
116 * The remote port will start in the INIT state.
112 * 117 *
113 * Locking note: must be called with the disc_mutex held. 118 * Locking note: must be called with the disc_mutex held.
114 */ 119 */
@@ -147,8 +152,8 @@ static struct fc_rport_priv *fc_rport_create(struct fc_lport *lport,
147} 152}
148 153
149/** 154/**
150 * fc_rport_destroy() - free a remote port after last reference is released. 155 * fc_rport_destroy() - Free a remote port after last reference is released
151 * @kref: pointer to kref inside struct fc_rport_priv 156 * @kref: The remote port's kref
152 */ 157 */
153static void fc_rport_destroy(struct kref *kref) 158static void fc_rport_destroy(struct kref *kref)
154{ 159{
@@ -159,8 +164,8 @@ static void fc_rport_destroy(struct kref *kref)
159} 164}
160 165
161/** 166/**
162 * fc_rport_state() - return a string for the state the rport is in 167 * fc_rport_state() - Return a string identifying the remote port's state
163 * @rdata: remote port private data 168 * @rdata: The remote port
164 */ 169 */
165static const char *fc_rport_state(struct fc_rport_priv *rdata) 170static const char *fc_rport_state(struct fc_rport_priv *rdata)
166{ 171{
@@ -173,9 +178,9 @@ static const char *fc_rport_state(struct fc_rport_priv *rdata)
173} 178}
174 179
175/** 180/**
176 * fc_set_rport_loss_tmo() - Set the remote port loss timeout in seconds. 181 * fc_set_rport_loss_tmo() - Set the remote port loss timeout
177 * @rport: Pointer to Fibre Channel remote port structure 182 * @rport: The remote port that gets a new timeout value
178 * @timeout: timeout in seconds 183 * @timeout: The new timeout value (in seconds)
179 */ 184 */
180void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout) 185void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
181{ 186{
@@ -187,9 +192,11 @@ void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
187EXPORT_SYMBOL(fc_set_rport_loss_tmo); 192EXPORT_SYMBOL(fc_set_rport_loss_tmo);
188 193
189/** 194/**
190 * fc_plogi_get_maxframe() - Get max payload from the common service parameters 195 * fc_plogi_get_maxframe() - Get the maximum payload from the common service
191 * @flp: FLOGI payload structure 196 * parameters in a FLOGI frame
192 * @maxval: upper limit, may be less than what is in the service parameters 197 * @flp: The FLOGI payload
198 * @maxval: The maximum frame size upper limit; this may be less than what
199 * is in the service parameters
193 */ 200 */
194static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp, 201static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
195 unsigned int maxval) 202 unsigned int maxval)
@@ -210,9 +217,9 @@ static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
210} 217}
211 218
212/** 219/**
213 * fc_rport_state_enter() - Change the rport's state 220 * fc_rport_state_enter() - Change the state of a remote port
214 * @rdata: The rport whose state should change 221 * @rdata: The remote port whose state should change
215 * @new: The new state of the rport 222 * @new: The new state
216 * 223 *
217 * Locking Note: Called with the rport lock held 224 * Locking Note: Called with the rport lock held
218 */ 225 */
@@ -224,17 +231,22 @@ static void fc_rport_state_enter(struct fc_rport_priv *rdata,
224 rdata->rp_state = new; 231 rdata->rp_state = new;
225} 232}
226 233
234/**
235 * fc_rport_work() - Handler for remote port events in the rport_event_queue
236 * @work: Handle to the remote port being dequeued
237 */
227static void fc_rport_work(struct work_struct *work) 238static void fc_rport_work(struct work_struct *work)
228{ 239{
229 u32 port_id; 240 u32 port_id;
230 struct fc_rport_priv *rdata = 241 struct fc_rport_priv *rdata =
231 container_of(work, struct fc_rport_priv, event_work); 242 container_of(work, struct fc_rport_priv, event_work);
232 struct fc_rport_libfc_priv *rp; 243 struct fc_rport_libfc_priv *rpriv;
233 enum fc_rport_event event; 244 enum fc_rport_event event;
234 struct fc_lport *lport = rdata->local_port; 245 struct fc_lport *lport = rdata->local_port;
235 struct fc_rport_operations *rport_ops; 246 struct fc_rport_operations *rport_ops;
236 struct fc_rport_identifiers ids; 247 struct fc_rport_identifiers ids;
237 struct fc_rport *rport; 248 struct fc_rport *rport;
249 int restart = 0;
238 250
239 mutex_lock(&rdata->rp_mutex); 251 mutex_lock(&rdata->rp_mutex);
240 event = rdata->event; 252 event = rdata->event;
@@ -265,12 +277,12 @@ static void fc_rport_work(struct work_struct *work)
265 rport->maxframe_size = rdata->maxframe_size; 277 rport->maxframe_size = rdata->maxframe_size;
266 rport->supported_classes = rdata->supported_classes; 278 rport->supported_classes = rdata->supported_classes;
267 279
268 rp = rport->dd_data; 280 rpriv = rport->dd_data;
269 rp->local_port = lport; 281 rpriv->local_port = lport;
270 rp->rp_state = rdata->rp_state; 282 rpriv->rp_state = rdata->rp_state;
271 rp->flags = rdata->flags; 283 rpriv->flags = rdata->flags;
272 rp->e_d_tov = rdata->e_d_tov; 284 rpriv->e_d_tov = rdata->e_d_tov;
273 rp->r_a_tov = rdata->r_a_tov; 285 rpriv->r_a_tov = rdata->r_a_tov;
274 mutex_unlock(&rdata->rp_mutex); 286 mutex_unlock(&rdata->rp_mutex);
275 287
276 if (rport_ops && rport_ops->event_callback) { 288 if (rport_ops && rport_ops->event_callback) {
@@ -287,8 +299,20 @@ static void fc_rport_work(struct work_struct *work)
287 mutex_unlock(&rdata->rp_mutex); 299 mutex_unlock(&rdata->rp_mutex);
288 300
289 if (port_id != FC_FID_DIR_SERV) { 301 if (port_id != FC_FID_DIR_SERV) {
302 /*
303 * We must drop rp_mutex before taking disc_mutex.
304 * Re-evaluate state to allow for restart.
305 * A transition to RESTART state must only happen
306 * while disc_mutex is held and rdata is on the list.
307 */
290 mutex_lock(&lport->disc.disc_mutex); 308 mutex_lock(&lport->disc.disc_mutex);
291 list_del(&rdata->peers); 309 mutex_lock(&rdata->rp_mutex);
310 if (rdata->rp_state == RPORT_ST_RESTART)
311 restart = 1;
312 else
313 list_del(&rdata->peers);
314 rdata->event = RPORT_EV_NONE;
315 mutex_unlock(&rdata->rp_mutex);
292 mutex_unlock(&lport->disc.disc_mutex); 316 mutex_unlock(&lport->disc.disc_mutex);
293 } 317 }
294 318
@@ -305,14 +329,20 @@ static void fc_rport_work(struct work_struct *work)
305 lport->tt.exch_mgr_reset(lport, port_id, 0); 329 lport->tt.exch_mgr_reset(lport, port_id, 0);
306 330
307 if (rport) { 331 if (rport) {
308 rp = rport->dd_data; 332 rpriv = rport->dd_data;
309 rp->rp_state = RPORT_ST_DELETE; 333 rpriv->rp_state = RPORT_ST_DELETE;
310 mutex_lock(&rdata->rp_mutex); 334 mutex_lock(&rdata->rp_mutex);
311 rdata->rport = NULL; 335 rdata->rport = NULL;
312 mutex_unlock(&rdata->rp_mutex); 336 mutex_unlock(&rdata->rp_mutex);
313 fc_remote_port_delete(rport); 337 fc_remote_port_delete(rport);
314 } 338 }
315 kref_put(&rdata->kref, lport->tt.rport_destroy); 339 if (restart) {
340 mutex_lock(&rdata->rp_mutex);
341 FC_RPORT_DBG(rdata, "work restart\n");
342 fc_rport_enter_plogi(rdata);
343 mutex_unlock(&rdata->rp_mutex);
344 } else
345 kref_put(&rdata->kref, lport->tt.rport_destroy);
316 break; 346 break;
317 347
318 default: 348 default:
@@ -323,7 +353,7 @@ static void fc_rport_work(struct work_struct *work)
323 353
324/** 354/**
325 * fc_rport_login() - Start the remote port login state machine 355 * fc_rport_login() - Start the remote port login state machine
326 * @rdata: private remote port 356 * @rdata: The remote port to be logged in to
327 * 357 *
328 * Locking Note: Called without the rport lock held. This 358 * Locking Note: Called without the rport lock held. This
329 * function will hold the rport lock, call an _enter_* 359 * function will hold the rport lock, call an _enter_*
@@ -342,6 +372,12 @@ int fc_rport_login(struct fc_rport_priv *rdata)
342 FC_RPORT_DBG(rdata, "ADISC port\n"); 372 FC_RPORT_DBG(rdata, "ADISC port\n");
343 fc_rport_enter_adisc(rdata); 373 fc_rport_enter_adisc(rdata);
344 break; 374 break;
375 case RPORT_ST_RESTART:
376 break;
377 case RPORT_ST_DELETE:
378 FC_RPORT_DBG(rdata, "Restart deleted port\n");
379 fc_rport_state_enter(rdata, RPORT_ST_RESTART);
380 break;
345 default: 381 default:
346 FC_RPORT_DBG(rdata, "Login to port\n"); 382 FC_RPORT_DBG(rdata, "Login to port\n");
347 fc_rport_enter_plogi(rdata); 383 fc_rport_enter_plogi(rdata);
@@ -353,9 +389,9 @@ int fc_rport_login(struct fc_rport_priv *rdata)
353} 389}
354 390
355/** 391/**
356 * fc_rport_enter_delete() - schedule a remote port to be deleted. 392 * fc_rport_enter_delete() - Schedule a remote port to be deleted
357 * @rdata: private remote port 393 * @rdata: The remote port to be deleted
358 * @event: event to report as the reason for deletion 394 * @event: The event to report as the reason for deletion
359 * 395 *
360 * Locking Note: Called with the rport lock held. 396 * Locking Note: Called with the rport lock held.
361 * 397 *
@@ -382,8 +418,8 @@ static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
382} 418}
383 419
384/** 420/**
385 * fc_rport_logoff() - Logoff and remove an rport 421 * fc_rport_logoff() - Logoff and remove a remote port
386 * @rdata: private remote port 422 * @rdata: The remote port to be logged off of
387 * 423 *
388 * Locking Note: Called without the rport lock held. This 424 * Locking Note: Called without the rport lock held. This
389 * function will hold the rport lock, call an _enter_* 425 * function will hold the rport lock, call an _enter_*
@@ -397,26 +433,27 @@ int fc_rport_logoff(struct fc_rport_priv *rdata)
397 433
398 if (rdata->rp_state == RPORT_ST_DELETE) { 434 if (rdata->rp_state == RPORT_ST_DELETE) {
399 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n"); 435 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
400 mutex_unlock(&rdata->rp_mutex);
401 goto out; 436 goto out;
402 } 437 }
403 438
404 fc_rport_enter_logo(rdata); 439 if (rdata->rp_state == RPORT_ST_RESTART)
440 FC_RPORT_DBG(rdata, "Port in Restart state, deleting\n");
441 else
442 fc_rport_enter_logo(rdata);
405 443
406 /* 444 /*
407 * Change the state to Delete so that we discard 445 * Change the state to Delete so that we discard
408 * the response. 446 * the response.
409 */ 447 */
410 fc_rport_enter_delete(rdata, RPORT_EV_STOP); 448 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
411 mutex_unlock(&rdata->rp_mutex);
412
413out: 449out:
450 mutex_unlock(&rdata->rp_mutex);
414 return 0; 451 return 0;
415} 452}
416 453
417/** 454/**
418 * fc_rport_enter_ready() - The rport is ready 455 * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state
419 * @rdata: private remote port 456 * @rdata: The remote port that is ready
420 * 457 *
421 * Locking Note: The rport lock is expected to be held before calling 458 * Locking Note: The rport lock is expected to be held before calling
422 * this routine. 459 * this routine.
@@ -433,8 +470,8 @@ static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
433} 470}
434 471
435/** 472/**
436 * fc_rport_timeout() - Handler for the retry_work timer. 473 * fc_rport_timeout() - Handler for the retry_work timer
437 * @work: The work struct of the fc_rport_priv 474 * @work: Handle to the remote port that has timed out
438 * 475 *
439 * Locking Note: Called without the rport lock held. This 476 * Locking Note: Called without the rport lock held. This
440 * function will hold the rport lock, call an _enter_* 477 * function will hold the rport lock, call an _enter_*
@@ -466,6 +503,7 @@ static void fc_rport_timeout(struct work_struct *work)
466 case RPORT_ST_READY: 503 case RPORT_ST_READY:
467 case RPORT_ST_INIT: 504 case RPORT_ST_INIT:
468 case RPORT_ST_DELETE: 505 case RPORT_ST_DELETE:
506 case RPORT_ST_RESTART:
469 break; 507 break;
470 } 508 }
471 509
@@ -474,8 +512,8 @@ static void fc_rport_timeout(struct work_struct *work)
474 512
475/** 513/**
476 * fc_rport_error() - Error handler, called once retries have been exhausted 514 * fc_rport_error() - Error handler, called once retries have been exhausted
477 * @rdata: private remote port 515 * @rdata: The remote port the error is happened on
478 * @fp: The frame pointer 516 * @fp: The error code encapsulated in a frame pointer
479 * 517 *
480 * Locking Note: The rport lock is expected to be held before 518 * Locking Note: The rport lock is expected to be held before
481 * calling this routine 519 * calling this routine
@@ -499,6 +537,7 @@ static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
499 fc_rport_enter_logo(rdata); 537 fc_rport_enter_logo(rdata);
500 break; 538 break;
501 case RPORT_ST_DELETE: 539 case RPORT_ST_DELETE:
540 case RPORT_ST_RESTART:
502 case RPORT_ST_READY: 541 case RPORT_ST_READY:
503 case RPORT_ST_INIT: 542 case RPORT_ST_INIT:
504 break; 543 break;
@@ -506,9 +545,9 @@ static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
506} 545}
507 546
508/** 547/**
509 * fc_rport_error_retry() - Error handler when retries are desired 548 * fc_rport_error_retry() - Handler for remote port state retries
510 * @rdata: private remote port data 549 * @rdata: The remote port whose state is to be retried
511 * @fp: The frame pointer 550 * @fp: The error code encapsulated in a frame pointer
512 * 551 *
513 * If the error was an exchange timeout retry immediately, 552 * If the error was an exchange timeout retry immediately,
514 * otherwise wait for E_D_TOV. 553 * otherwise wait for E_D_TOV.
@@ -540,10 +579,10 @@ static void fc_rport_error_retry(struct fc_rport_priv *rdata,
540} 579}
541 580
542/** 581/**
543 * fc_rport_plogi_recv_resp() - Handle incoming ELS PLOGI response 582 * fc_rport_plogi_recv_resp() - Handler for ELS PLOGI responses
544 * @sp: current sequence in the PLOGI exchange 583 * @sp: The sequence the PLOGI is on
545 * @fp: response frame 584 * @fp: The PLOGI response frame
546 * @rdata_arg: private remote port data 585 * @rdata_arg: The remote port that sent the PLOGI response
547 * 586 *
548 * Locking Note: This function will be called without the rport lock 587 * Locking Note: This function will be called without the rport lock
549 * held, but it will lock, call an _enter_* function or fc_rport_error 588 * held, but it will lock, call an _enter_* function or fc_rport_error
@@ -585,7 +624,7 @@ static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
585 624
586 tov = ntohl(plp->fl_csp.sp_e_d_tov); 625 tov = ntohl(plp->fl_csp.sp_e_d_tov);
587 if (ntohs(plp->fl_csp.sp_features) & FC_SP_FT_EDTR) 626 if (ntohs(plp->fl_csp.sp_features) & FC_SP_FT_EDTR)
588 tov /= 1000; 627 tov /= 1000000;
589 if (tov > rdata->e_d_tov) 628 if (tov > rdata->e_d_tov)
590 rdata->e_d_tov = tov; 629 rdata->e_d_tov = tov;
591 csp_seq = ntohs(plp->fl_csp.sp_tot_seq); 630 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
@@ -606,8 +645,8 @@ err:
606} 645}
607 646
608/** 647/**
609 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request to peer 648 * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
610 * @rdata: private remote port data 649 * @rdata: The remote port to send a PLOGI to
611 * 650 *
612 * Locking Note: The rport lock is expected to be held before calling 651 * Locking Note: The rport lock is expected to be held before calling
613 * this routine. 652 * this routine.
@@ -631,17 +670,18 @@ static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
631 rdata->e_d_tov = lport->e_d_tov; 670 rdata->e_d_tov = lport->e_d_tov;
632 671
633 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI, 672 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
634 fc_rport_plogi_resp, rdata, lport->e_d_tov)) 673 fc_rport_plogi_resp, rdata,
635 fc_rport_error_retry(rdata, fp); 674 2 * lport->r_a_tov))
675 fc_rport_error_retry(rdata, NULL);
636 else 676 else
637 kref_get(&rdata->kref); 677 kref_get(&rdata->kref);
638} 678}
639 679
640/** 680/**
641 * fc_rport_prli_resp() - Process Login (PRLI) response handler 681 * fc_rport_prli_resp() - Process Login (PRLI) response handler
642 * @sp: current sequence in the PRLI exchange 682 * @sp: The sequence the PRLI response was on
643 * @fp: response frame 683 * @fp: The PRLI response frame
644 * @rdata_arg: private remote port data 684 * @rdata_arg: The remote port that sent the PRLI response
645 * 685 *
646 * Locking Note: This function will be called without the rport lock 686 * Locking Note: This function will be called without the rport lock
647 * held, but it will lock, call an _enter_* function or fc_rport_error 687 * held, but it will lock, call an _enter_* function or fc_rport_error
@@ -710,10 +750,10 @@ err:
710} 750}
711 751
712/** 752/**
713 * fc_rport_logo_resp() - Logout (LOGO) response handler 753 * fc_rport_logo_resp() - Handler for logout (LOGO) responses
714 * @sp: current sequence in the LOGO exchange 754 * @sp: The sequence the LOGO was on
715 * @fp: response frame 755 * @fp: The LOGO response frame
716 * @rdata_arg: private remote port data 756 * @rdata_arg: The remote port that sent the LOGO response
717 * 757 *
718 * Locking Note: This function will be called without the rport lock 758 * Locking Note: This function will be called without the rport lock
719 * held, but it will lock, call an _enter_* function or fc_rport_error 759 * held, but it will lock, call an _enter_* function or fc_rport_error
@@ -756,8 +796,8 @@ err:
756} 796}
757 797
758/** 798/**
759 * fc_rport_enter_prli() - Send Process Login (PRLI) request to peer 799 * fc_rport_enter_prli() - Send Process Login (PRLI) request
760 * @rdata: private remote port data 800 * @rdata: The remote port to send the PRLI request to
761 * 801 *
762 * Locking Note: The rport lock is expected to be held before calling 802 * Locking Note: The rport lock is expected to be held before calling
763 * this routine. 803 * this routine.
@@ -792,17 +832,18 @@ static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
792 } 832 }
793 833
794 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PRLI, 834 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PRLI,
795 fc_rport_prli_resp, rdata, lport->e_d_tov)) 835 fc_rport_prli_resp, rdata,
796 fc_rport_error_retry(rdata, fp); 836 2 * lport->r_a_tov))
837 fc_rport_error_retry(rdata, NULL);
797 else 838 else
798 kref_get(&rdata->kref); 839 kref_get(&rdata->kref);
799} 840}
800 841
801/** 842/**
802 * fc_rport_els_rtv_resp() - Request Timeout Value response handler 843 * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses
803 * @sp: current sequence in the RTV exchange 844 * @sp: The sequence the RTV was on
804 * @fp: response frame 845 * @fp: The RTV response frame
805 * @rdata_arg: private remote port data 846 * @rdata_arg: The remote port that sent the RTV response
806 * 847 *
807 * Many targets don't seem to support this. 848 * Many targets don't seem to support this.
808 * 849 *
@@ -865,8 +906,8 @@ err:
865} 906}
866 907
867/** 908/**
868 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request to peer 909 * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
869 * @rdata: private remote port data 910 * @rdata: The remote port to send the RTV request to
870 * 911 *
871 * Locking Note: The rport lock is expected to be held before calling 912 * Locking Note: The rport lock is expected to be held before calling
872 * this routine. 913 * this routine.
@@ -888,15 +929,16 @@ static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
888 } 929 }
889 930
890 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV, 931 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
891 fc_rport_rtv_resp, rdata, lport->e_d_tov)) 932 fc_rport_rtv_resp, rdata,
892 fc_rport_error_retry(rdata, fp); 933 2 * lport->r_a_tov))
934 fc_rport_error_retry(rdata, NULL);
893 else 935 else
894 kref_get(&rdata->kref); 936 kref_get(&rdata->kref);
895} 937}
896 938
897/** 939/**
898 * fc_rport_enter_logo() - Send Logout (LOGO) request to peer 940 * fc_rport_enter_logo() - Send a logout (LOGO) request
899 * @rdata: private remote port data 941 * @rdata: The remote port to send the LOGO request to
900 * 942 *
901 * Locking Note: The rport lock is expected to be held before calling 943 * Locking Note: The rport lock is expected to be held before calling
902 * this routine. 944 * this routine.
@@ -918,24 +960,25 @@ static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
918 } 960 }
919 961
920 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO, 962 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
921 fc_rport_logo_resp, rdata, lport->e_d_tov)) 963 fc_rport_logo_resp, rdata,
922 fc_rport_error_retry(rdata, fp); 964 2 * lport->r_a_tov))
965 fc_rport_error_retry(rdata, NULL);
923 else 966 else
924 kref_get(&rdata->kref); 967 kref_get(&rdata->kref);
925} 968}
926 969
927/** 970/**
928 * fc_rport_els_adisc_resp() - Address Discovery response handler 971 * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
929 * @sp: current sequence in the ADISC exchange 972 * @sp: The sequence the ADISC response was on
930 * @fp: response frame 973 * @fp: The ADISC response frame
931 * @rdata_arg: remote port private. 974 * @rdata_arg: The remote port that sent the ADISC response
932 * 975 *
933 * Locking Note: This function will be called without the rport lock 976 * Locking Note: This function will be called without the rport lock
934 * held, but it will lock, call an _enter_* function or fc_rport_error 977 * held, but it will lock, call an _enter_* function or fc_rport_error
935 * and then unlock the rport. 978 * and then unlock the rport.
936 */ 979 */
937static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp, 980static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
938 void *rdata_arg) 981 void *rdata_arg)
939{ 982{
940 struct fc_rport_priv *rdata = rdata_arg; 983 struct fc_rport_priv *rdata = rdata_arg;
941 struct fc_els_adisc *adisc; 984 struct fc_els_adisc *adisc;
@@ -983,8 +1026,8 @@ err:
983} 1026}
984 1027
985/** 1028/**
986 * fc_rport_enter_adisc() - Send Address Discover (ADISC) request to peer 1029 * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
987 * @rdata: remote port private data 1030 * @rdata: The remote port to send the ADISC request to
988 * 1031 *
989 * Locking Note: The rport lock is expected to be held before calling 1032 * Locking Note: The rport lock is expected to be held before calling
990 * this routine. 1033 * this routine.
@@ -1005,17 +1048,18 @@ static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
1005 return; 1048 return;
1006 } 1049 }
1007 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC, 1050 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
1008 fc_rport_adisc_resp, rdata, lport->e_d_tov)) 1051 fc_rport_adisc_resp, rdata,
1009 fc_rport_error_retry(rdata, fp); 1052 2 * lport->r_a_tov))
1053 fc_rport_error_retry(rdata, NULL);
1010 else 1054 else
1011 kref_get(&rdata->kref); 1055 kref_get(&rdata->kref);
1012} 1056}
1013 1057
1014/** 1058/**
1015 * fc_rport_recv_adisc_req() - Handle incoming Address Discovery (ADISC) Request 1059 * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
1016 * @rdata: remote port private 1060 * @rdata: The remote port that sent the ADISC request
1017 * @sp: current sequence in the ADISC exchange 1061 * @sp: The sequence the ADISC request was on
1018 * @in_fp: ADISC request frame 1062 * @in_fp: The ADISC request frame
1019 * 1063 *
1020 * Locking Note: Called with the lport and rport locks held. 1064 * Locking Note: Called with the lport and rport locks held.
1021 */ 1065 */
@@ -1056,10 +1100,82 @@ drop:
1056} 1100}
1057 1101
1058/** 1102/**
1059 * fc_rport_recv_els_req() - handle a validated ELS request. 1103 * fc_rport_recv_rls_req() - Handle received Read Link Status request
1060 * @lport: Fibre Channel local port 1104 * @rdata: The remote port that sent the RLS request
1061 * @sp: current sequence in the PLOGI exchange 1105 * @sp: The sequence that the RLS was on
1062 * @fp: response frame 1106 * @rx_fp: The PRLI request frame
1107 *
1108 * Locking Note: The rport lock is expected to be held before calling
1109 * this function.
1110 */
1111static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
1112 struct fc_seq *sp, struct fc_frame *rx_fp)
1113
1114{
1115 struct fc_lport *lport = rdata->local_port;
1116 struct fc_frame *fp;
1117 struct fc_exch *ep = fc_seq_exch(sp);
1118 struct fc_els_rls *rls;
1119 struct fc_els_rls_resp *rsp;
1120 struct fc_els_lesb *lesb;
1121 struct fc_seq_els_data rjt_data;
1122 struct fc_host_statistics *hst;
1123 u32 f_ctl;
1124
1125 FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
1126 fc_rport_state(rdata));
1127
1128 rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
1129 if (!rls) {
1130 rjt_data.reason = ELS_RJT_PROT;
1131 rjt_data.explan = ELS_EXPL_INV_LEN;
1132 goto out_rjt;
1133 }
1134
1135 fp = fc_frame_alloc(lport, sizeof(*rsp));
1136 if (!fp) {
1137 rjt_data.reason = ELS_RJT_UNAB;
1138 rjt_data.explan = ELS_EXPL_INSUF_RES;
1139 goto out_rjt;
1140 }
1141
1142 rsp = fc_frame_payload_get(fp, sizeof(*rsp));
1143 memset(rsp, 0, sizeof(*rsp));
1144 rsp->rls_cmd = ELS_LS_ACC;
1145 lesb = &rsp->rls_lesb;
1146 if (lport->tt.get_lesb) {
1147 /* get LESB from LLD if it supports it */
1148 lport->tt.get_lesb(lport, lesb);
1149 } else {
1150 fc_get_host_stats(lport->host);
1151 hst = &lport->host_stats;
1152 lesb->lesb_link_fail = htonl(hst->link_failure_count);
1153 lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
1154 lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
1155 lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
1156 lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
1157 lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
1158 }
1159
1160 sp = lport->tt.seq_start_next(sp);
1161 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
1162 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1163 FC_TYPE_ELS, f_ctl, 0);
1164 lport->tt.seq_send(lport, sp, fp);
1165 goto out;
1166
1167out_rjt:
1168 rjt_data.fp = NULL;
1169 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1170out:
1171 fc_frame_free(rx_fp);
1172}
1173
1174/**
1175 * fc_rport_recv_els_req() - Handler for validated ELS requests
1176 * @lport: The local port that received the ELS request
1177 * @sp: The sequence that the ELS request was on
1178 * @fp: The ELS request frame
1063 * 1179 *
1064 * Handle incoming ELS requests that require port login. 1180 * Handle incoming ELS requests that require port login.
1065 * The ELS opcode has already been validated by the caller. 1181 * The ELS opcode has already been validated by the caller.
@@ -1117,6 +1233,9 @@ static void fc_rport_recv_els_req(struct fc_lport *lport,
1117 els_data.fp = fp; 1233 els_data.fp = fp;
1118 lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data); 1234 lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
1119 break; 1235 break;
1236 case ELS_RLS:
1237 fc_rport_recv_rls_req(rdata, sp, fp);
1238 break;
1120 default: 1239 default:
1121 fc_frame_free(fp); /* can't happen */ 1240 fc_frame_free(fp); /* can't happen */
1122 break; 1241 break;
@@ -1131,10 +1250,10 @@ reject:
1131} 1250}
1132 1251
1133/** 1252/**
1134 * fc_rport_recv_req() - Handle a received ELS request from a rport 1253 * fc_rport_recv_req() - Handler for requests
1135 * @sp: current sequence in the PLOGI exchange 1254 * @sp: The sequence the request was on
1136 * @fp: response frame 1255 * @fp: The request frame
1137 * @lport: Fibre Channel local port 1256 * @lport: The local port that received the request
1138 * 1257 *
1139 * Locking Note: Called with the lport lock held. 1258 * Locking Note: Called with the lport lock held.
1140 */ 1259 */
@@ -1161,6 +1280,7 @@ void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
1161 case ELS_ADISC: 1280 case ELS_ADISC:
1162 case ELS_RRQ: 1281 case ELS_RRQ:
1163 case ELS_REC: 1282 case ELS_REC:
1283 case ELS_RLS:
1164 fc_rport_recv_els_req(lport, sp, fp); 1284 fc_rport_recv_els_req(lport, sp, fp);
1165 break; 1285 break;
1166 default: 1286 default:
@@ -1174,10 +1294,10 @@ void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
1174} 1294}
1175 1295
1176/** 1296/**
1177 * fc_rport_recv_plogi_req() - Handle incoming Port Login (PLOGI) request 1297 * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
1178 * @lport: local port 1298 * @lport: The local port that received the PLOGI request
1179 * @sp: current sequence in the PLOGI exchange 1299 * @sp: The sequence that the PLOGI request was on
1180 * @fp: PLOGI request frame 1300 * @rx_fp: The PLOGI request frame
1181 * 1301 *
1182 * Locking Note: The rport lock is held before calling this function. 1302 * Locking Note: The rport lock is held before calling this function.
1183 */ 1303 */
@@ -1248,6 +1368,7 @@ static void fc_rport_recv_plogi_req(struct fc_lport *lport,
1248 } 1368 }
1249 break; 1369 break;
1250 case RPORT_ST_PRLI: 1370 case RPORT_ST_PRLI:
1371 case RPORT_ST_RTV:
1251 case RPORT_ST_READY: 1372 case RPORT_ST_READY:
1252 case RPORT_ST_ADISC: 1373 case RPORT_ST_ADISC:
1253 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d " 1374 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
@@ -1255,11 +1376,14 @@ static void fc_rport_recv_plogi_req(struct fc_lport *lport,
1255 /* XXX TBD - should reset */ 1376 /* XXX TBD - should reset */
1256 break; 1377 break;
1257 case RPORT_ST_DELETE: 1378 case RPORT_ST_DELETE:
1258 default: 1379 case RPORT_ST_LOGO:
1259 FC_RPORT_DBG(rdata, "Received PLOGI in unexpected state %d\n", 1380 case RPORT_ST_RESTART:
1260 rdata->rp_state); 1381 FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
1261 fc_frame_free(rx_fp); 1382 fc_rport_state(rdata));
1262 goto out; 1383 mutex_unlock(&rdata->rp_mutex);
1384 rjt_data.reason = ELS_RJT_BUSY;
1385 rjt_data.explan = ELS_EXPL_NONE;
1386 goto reject;
1263 } 1387 }
1264 1388
1265 /* 1389 /*
@@ -1295,10 +1419,10 @@ reject:
1295} 1419}
1296 1420
1297/** 1421/**
1298 * fc_rport_recv_prli_req() - Handle incoming Process Login (PRLI) request 1422 * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
1299 * @rdata: private remote port data 1423 * @rdata: The remote port that sent the PRLI request
1300 * @sp: current sequence in the PRLI exchange 1424 * @sp: The sequence that the PRLI was on
1301 * @fp: PRLI request frame 1425 * @rx_fp: The PRLI request frame
1302 * 1426 *
1303 * Locking Note: The rport lock is exected to be held before calling 1427 * Locking Note: The rport lock is exected to be held before calling
1304 * this function. 1428 * this function.
@@ -1402,7 +1526,7 @@ static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
1402 break; 1526 break;
1403 case FC_TYPE_FCP: 1527 case FC_TYPE_FCP:
1404 fcp_parm = ntohl(rspp->spp_params); 1528 fcp_parm = ntohl(rspp->spp_params);
1405 if (fcp_parm * FCP_SPPF_RETRY) 1529 if (fcp_parm & FCP_SPPF_RETRY)
1406 rdata->flags |= FC_RP_FLAGS_RETRY; 1530 rdata->flags |= FC_RP_FLAGS_RETRY;
1407 rdata->supported_classes = FC_COS_CLASS3; 1531 rdata->supported_classes = FC_COS_CLASS3;
1408 if (fcp_parm & FCP_SPPF_INIT_FCN) 1532 if (fcp_parm & FCP_SPPF_INIT_FCN)
@@ -1452,10 +1576,10 @@ static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
1452} 1576}
1453 1577
1454/** 1578/**
1455 * fc_rport_recv_prlo_req() - Handle incoming Process Logout (PRLO) request 1579 * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
1456 * @rdata: private remote port data 1580 * @rdata: The remote port that sent the PRLO request
1457 * @sp: current sequence in the PRLO exchange 1581 * @sp: The sequence that the PRLO was on
1458 * @fp: PRLO request frame 1582 * @fp: The PRLO request frame
1459 * 1583 *
1460 * Locking Note: The rport lock is exected to be held before calling 1584 * Locking Note: The rport lock is exected to be held before calling
1461 * this function. 1585 * this function.
@@ -1482,10 +1606,10 @@ static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
1482} 1606}
1483 1607
1484/** 1608/**
1485 * fc_rport_recv_logo_req() - Handle incoming Logout (LOGO) request 1609 * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
1486 * @lport: local port. 1610 * @lport: The local port that received the LOGO request
1487 * @sp: current sequence in the LOGO exchange 1611 * @sp: The sequence that the LOGO request was on
1488 * @fp: LOGO request frame 1612 * @fp: The LOGO request frame
1489 * 1613 *
1490 * Locking Note: The rport lock is exected to be held before calling 1614 * Locking Note: The rport lock is exected to be held before calling
1491 * this function. 1615 * this function.
@@ -1510,14 +1634,14 @@ static void fc_rport_recv_logo_req(struct fc_lport *lport,
1510 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n", 1634 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
1511 fc_rport_state(rdata)); 1635 fc_rport_state(rdata));
1512 1636
1637 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
1638
1513 /* 1639 /*
1514 * If the remote port was created due to discovery, 1640 * If the remote port was created due to discovery, set state
1515 * log back in. It may have seen a stale RSCN about us. 1641 * to log back in. It may have seen a stale RSCN about us.
1516 */ 1642 */
1517 if (rdata->rp_state != RPORT_ST_DELETE && rdata->disc_id) 1643 if (rdata->disc_id)
1518 fc_rport_enter_plogi(rdata); 1644 fc_rport_state_enter(rdata, RPORT_ST_RESTART);
1519 else
1520 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
1521 mutex_unlock(&rdata->rp_mutex); 1645 mutex_unlock(&rdata->rp_mutex);
1522 } else 1646 } else
1523 FC_RPORT_ID_DBG(lport, sid, 1647 FC_RPORT_ID_DBG(lport, sid,
@@ -1526,11 +1650,18 @@ static void fc_rport_recv_logo_req(struct fc_lport *lport,
1526 fc_frame_free(fp); 1650 fc_frame_free(fp);
1527} 1651}
1528 1652
1653/**
1654 * fc_rport_flush_queue() - Flush the rport_event_queue
1655 */
1529static void fc_rport_flush_queue(void) 1656static void fc_rport_flush_queue(void)
1530{ 1657{
1531 flush_workqueue(rport_event_queue); 1658 flush_workqueue(rport_event_queue);
1532} 1659}
1533 1660
1661/**
1662 * fc_rport_init() - Initialize the remote port layer for a local port
1663 * @lport: The local port to initialize the remote port layer for
1664 */
1534int fc_rport_init(struct fc_lport *lport) 1665int fc_rport_init(struct fc_lport *lport)
1535{ 1666{
1536 if (!lport->tt.rport_lookup) 1667 if (!lport->tt.rport_lookup)
@@ -1558,25 +1689,33 @@ int fc_rport_init(struct fc_lport *lport)
1558} 1689}
1559EXPORT_SYMBOL(fc_rport_init); 1690EXPORT_SYMBOL(fc_rport_init);
1560 1691
1561int fc_setup_rport(void) 1692/**
1693 * fc_setup_rport() - Initialize the rport_event_queue
1694 */
1695int fc_setup_rport()
1562{ 1696{
1563 rport_event_queue = create_singlethread_workqueue("fc_rport_eq"); 1697 rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
1564 if (!rport_event_queue) 1698 if (!rport_event_queue)
1565 return -ENOMEM; 1699 return -ENOMEM;
1566 return 0; 1700 return 0;
1567} 1701}
1568EXPORT_SYMBOL(fc_setup_rport);
1569 1702
1570void fc_destroy_rport(void) 1703/**
1704 * fc_destroy_rport() - Destroy the rport_event_queue
1705 */
1706void fc_destroy_rport()
1571{ 1707{
1572 destroy_workqueue(rport_event_queue); 1708 destroy_workqueue(rport_event_queue);
1573} 1709}
1574EXPORT_SYMBOL(fc_destroy_rport);
1575 1710
1711/**
1712 * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
1713 * @rport: The remote port whose I/O should be terminated
1714 */
1576void fc_rport_terminate_io(struct fc_rport *rport) 1715void fc_rport_terminate_io(struct fc_rport *rport)
1577{ 1716{
1578 struct fc_rport_libfc_priv *rp = rport->dd_data; 1717 struct fc_rport_libfc_priv *rpriv = rport->dd_data;
1579 struct fc_lport *lport = rp->local_port; 1718 struct fc_lport *lport = rpriv->local_port;
1580 1719
1581 lport->tt.exch_mgr_reset(lport, 0, rport->port_id); 1720 lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
1582 lport->tt.exch_mgr_reset(lport, rport->port_id, 0); 1721 lport->tt.exch_mgr_reset(lport, rport->port_id, 0);