]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/scsi/bnx2fc/bnx2fc_io.c
bnx2fc: Set ELS transfer length correctly for middle path commands.
[mirror_ubuntu-artful-kernel.git] / drivers / scsi / bnx2fc / bnx2fc_io.c
CommitLineData
de909d87 1/* bnx2fc_io.c: QLogic Linux FCoE offload driver.
853e2bd2
BG
2 * IO manager and SCSI IO processing.
3 *
97586090
CD
4 * Copyright (c) 2008-2013 Broadcom Corporation
5 * Copyright (c) 2014-2015 QLogic Corporation
853e2bd2
BG
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation.
10 *
11 * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
12 */
13
14#include "bnx2fc.h"
0ea5c275
BG
15
16#define RESERVE_FREE_LIST_INDEX num_possible_cpus()
17
853e2bd2
BG
18static int bnx2fc_split_bd(struct bnx2fc_cmd *io_req, u64 addr, int sg_len,
19 int bd_index);
20static int bnx2fc_map_sg(struct bnx2fc_cmd *io_req);
822f2903 21static int bnx2fc_build_bd_list_from_sg(struct bnx2fc_cmd *io_req);
853e2bd2
BG
22static void bnx2fc_unmap_sg_list(struct bnx2fc_cmd *io_req);
23static void bnx2fc_free_mp_resc(struct bnx2fc_cmd *io_req);
24static void bnx2fc_parse_fcp_rsp(struct bnx2fc_cmd *io_req,
25 struct fcoe_fcp_rsp_payload *fcp_rsp,
26 u8 num_rq);
27
28void bnx2fc_cmd_timer_set(struct bnx2fc_cmd *io_req,
29 unsigned int timer_msec)
30{
aea71a02 31 struct bnx2fc_interface *interface = io_req->port->priv;
853e2bd2 32
aea71a02
BPG
33 if (queue_delayed_work(interface->timer_work_queue,
34 &io_req->timeout_work,
35 msecs_to_jiffies(timer_msec)))
853e2bd2
BG
36 kref_get(&io_req->refcount);
37}
38
39static void bnx2fc_cmd_timeout(struct work_struct *work)
40{
41 struct bnx2fc_cmd *io_req = container_of(work, struct bnx2fc_cmd,
42 timeout_work.work);
43 struct fc_lport *lport;
44 struct fc_rport_priv *rdata;
45 u8 cmd_type = io_req->cmd_type;
46 struct bnx2fc_rport *tgt = io_req->tgt;
47 int logo_issued;
48 int rc;
49
50 BNX2FC_IO_DBG(io_req, "cmd_timeout, cmd_type = %d,"
51 "req_flags = %lx\n", cmd_type, io_req->req_flags);
52
53 spin_lock_bh(&tgt->tgt_lock);
54 if (test_and_clear_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags)) {
55 clear_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags);
56 /*
57 * ideally we should hold the io_req until RRQ complets,
58 * and release io_req from timeout hold.
59 */
60 spin_unlock_bh(&tgt->tgt_lock);
61 bnx2fc_send_rrq(io_req);
62 return;
63 }
64 if (test_and_clear_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags)) {
65 BNX2FC_IO_DBG(io_req, "IO ready for reuse now\n");
66 goto done;
67 }
68
69 switch (cmd_type) {
70 case BNX2FC_SCSI_CMD:
71 if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
72 &io_req->req_flags)) {
73 /* Handle eh_abort timeout */
74 BNX2FC_IO_DBG(io_req, "eh_abort timed out\n");
75 complete(&io_req->tm_done);
76 } else if (test_bit(BNX2FC_FLAG_ISSUE_ABTS,
77 &io_req->req_flags)) {
78 /* Handle internally generated ABTS timeout */
79 BNX2FC_IO_DBG(io_req, "ABTS timed out refcnt = %d\n",
80 io_req->refcount.refcount.counter);
81 if (!(test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
82 &io_req->req_flags))) {
83
84 lport = io_req->port->lport;
85 rdata = io_req->tgt->rdata;
86 logo_issued = test_and_set_bit(
87 BNX2FC_FLAG_EXPL_LOGO,
88 &tgt->flags);
89 kref_put(&io_req->refcount, bnx2fc_cmd_release);
90 spin_unlock_bh(&tgt->tgt_lock);
91
92 /* Explicitly logo the target */
93 if (!logo_issued) {
94 BNX2FC_IO_DBG(io_req, "Explicit "
95 "logo - tgt flags = 0x%lx\n",
96 tgt->flags);
97
98 mutex_lock(&lport->disc.disc_mutex);
99 lport->tt.rport_logoff(rdata);
100 mutex_unlock(&lport->disc.disc_mutex);
101 }
102 return;
103 }
104 } else {
105 /* Hanlde IO timeout */
106 BNX2FC_IO_DBG(io_req, "IO timed out. issue ABTS\n");
107 if (test_and_set_bit(BNX2FC_FLAG_IO_COMPL,
108 &io_req->req_flags)) {
109 BNX2FC_IO_DBG(io_req, "IO completed before "
110 " timer expiry\n");
111 goto done;
112 }
113
114 if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
115 &io_req->req_flags)) {
116 rc = bnx2fc_initiate_abts(io_req);
117 if (rc == SUCCESS)
118 goto done;
119 /*
120 * Explicitly logo the target if
121 * abts initiation fails
122 */
123 lport = io_req->port->lport;
124 rdata = io_req->tgt->rdata;
125 logo_issued = test_and_set_bit(
126 BNX2FC_FLAG_EXPL_LOGO,
127 &tgt->flags);
128 kref_put(&io_req->refcount, bnx2fc_cmd_release);
129 spin_unlock_bh(&tgt->tgt_lock);
130
131 if (!logo_issued) {
132 BNX2FC_IO_DBG(io_req, "Explicit "
133 "logo - tgt flags = 0x%lx\n",
134 tgt->flags);
135
136
137 mutex_lock(&lport->disc.disc_mutex);
138 lport->tt.rport_logoff(rdata);
139 mutex_unlock(&lport->disc.disc_mutex);
140 }
141 return;
142 } else {
143 BNX2FC_IO_DBG(io_req, "IO already in "
144 "ABTS processing\n");
145 }
146 }
147 break;
148 case BNX2FC_ELS:
149
150 if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags)) {
151 BNX2FC_IO_DBG(io_req, "ABTS for ELS timed out\n");
152
153 if (!test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
154 &io_req->req_flags)) {
155 lport = io_req->port->lport;
156 rdata = io_req->tgt->rdata;
157 logo_issued = test_and_set_bit(
158 BNX2FC_FLAG_EXPL_LOGO,
159 &tgt->flags);
160 kref_put(&io_req->refcount, bnx2fc_cmd_release);
161 spin_unlock_bh(&tgt->tgt_lock);
162
163 /* Explicitly logo the target */
164 if (!logo_issued) {
165 BNX2FC_IO_DBG(io_req, "Explicitly logo"
166 "(els)\n");
167 mutex_lock(&lport->disc.disc_mutex);
168 lport->tt.rport_logoff(rdata);
169 mutex_unlock(&lport->disc.disc_mutex);
170 }
171 return;
172 }
173 } else {
174 /*
175 * Handle ELS timeout.
176 * tgt_lock is used to sync compl path and timeout
177 * path. If els compl path is processing this IO, we
178 * have nothing to do here, just release the timer hold
179 */
180 BNX2FC_IO_DBG(io_req, "ELS timed out\n");
181 if (test_and_set_bit(BNX2FC_FLAG_ELS_DONE,
182 &io_req->req_flags))
183 goto done;
184
185 /* Indicate the cb_func that this ELS is timed out */
186 set_bit(BNX2FC_FLAG_ELS_TIMEOUT, &io_req->req_flags);
187
188 if ((io_req->cb_func) && (io_req->cb_arg)) {
189 io_req->cb_func(io_req->cb_arg);
190 io_req->cb_arg = NULL;
191 }
192 }
193 break;
194 default:
195 printk(KERN_ERR PFX "cmd_timeout: invalid cmd_type %d\n",
196 cmd_type);
197 break;
198 }
199
200done:
201 /* release the cmd that was held when timer was set */
202 kref_put(&io_req->refcount, bnx2fc_cmd_release);
203 spin_unlock_bh(&tgt->tgt_lock);
204}
205
206static void bnx2fc_scsi_done(struct bnx2fc_cmd *io_req, int err_code)
207{
208 /* Called with host lock held */
209 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
210
211 /*
212 * active_cmd_queue may have other command types as well,
213 * and during flush operation, we want to error back only
214 * scsi commands.
215 */
216 if (io_req->cmd_type != BNX2FC_SCSI_CMD)
217 return;
218
219 BNX2FC_IO_DBG(io_req, "scsi_done. err_code = 0x%x\n", err_code);
74446954
BPG
220 if (test_bit(BNX2FC_FLAG_CMD_LOST, &io_req->req_flags)) {
221 /* Do not call scsi done for this IO */
222 return;
223 }
224
853e2bd2
BG
225 bnx2fc_unmap_sg_list(io_req);
226 io_req->sc_cmd = NULL;
227 if (!sc_cmd) {
228 printk(KERN_ERR PFX "scsi_done - sc_cmd NULL. "
229 "IO(0x%x) already cleaned up\n",
230 io_req->xid);
231 return;
232 }
233 sc_cmd->result = err_code << 16;
234
235 BNX2FC_IO_DBG(io_req, "sc=%p, result=0x%x, retries=%d, allowed=%d\n",
236 sc_cmd, host_byte(sc_cmd->result), sc_cmd->retries,
237 sc_cmd->allowed);
238 scsi_set_resid(sc_cmd, scsi_bufflen(sc_cmd));
239 sc_cmd->SCp.ptr = NULL;
240 sc_cmd->scsi_done(sc_cmd);
241}
242
0eb43b4b 243struct bnx2fc_cmd_mgr *bnx2fc_cmd_mgr_alloc(struct bnx2fc_hba *hba)
853e2bd2
BG
244{
245 struct bnx2fc_cmd_mgr *cmgr;
246 struct io_bdt *bdt_info;
247 struct bnx2fc_cmd *io_req;
248 size_t len;
249 u32 mem_size;
250 u16 xid;
251 int i;
0ea5c275 252 int num_ios, num_pri_ios;
853e2bd2 253 size_t bd_tbl_sz;
0ea5c275 254 int arr_sz = num_possible_cpus() + 1;
0eb43b4b
BPG
255 u16 min_xid = BNX2FC_MIN_XID;
256 u16 max_xid = hba->max_xid;
853e2bd2
BG
257
258 if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN) {
259 printk(KERN_ERR PFX "cmd_mgr_alloc: Invalid min_xid 0x%x \
260 and max_xid 0x%x\n", min_xid, max_xid);
261 return NULL;
262 }
263 BNX2FC_MISC_DBG("min xid 0x%x, max xid 0x%x\n", min_xid, max_xid);
264
265 num_ios = max_xid - min_xid + 1;
266 len = (num_ios * (sizeof(struct bnx2fc_cmd *)));
267 len += sizeof(struct bnx2fc_cmd_mgr);
268
269 cmgr = kzalloc(len, GFP_KERNEL);
270 if (!cmgr) {
271 printk(KERN_ERR PFX "failed to alloc cmgr\n");
272 return NULL;
273 }
274
275 cmgr->free_list = kzalloc(sizeof(*cmgr->free_list) *
0ea5c275 276 arr_sz, GFP_KERNEL);
853e2bd2
BG
277 if (!cmgr->free_list) {
278 printk(KERN_ERR PFX "failed to alloc free_list\n");
279 goto mem_err;
280 }
281
282 cmgr->free_list_lock = kzalloc(sizeof(*cmgr->free_list_lock) *
0ea5c275 283 arr_sz, GFP_KERNEL);
853e2bd2
BG
284 if (!cmgr->free_list_lock) {
285 printk(KERN_ERR PFX "failed to alloc free_list_lock\n");
9172b763
ML
286 kfree(cmgr->free_list);
287 cmgr->free_list = NULL;
853e2bd2
BG
288 goto mem_err;
289 }
290
291 cmgr->hba = hba;
292 cmgr->cmds = (struct bnx2fc_cmd **)(cmgr + 1);
293
0ea5c275 294 for (i = 0; i < arr_sz; i++) {
853e2bd2
BG
295 INIT_LIST_HEAD(&cmgr->free_list[i]);
296 spin_lock_init(&cmgr->free_list_lock[i]);
297 }
298
0ea5c275
BG
299 /*
300 * Pre-allocated pool of bnx2fc_cmds.
301 * Last entry in the free list array is the free list
302 * of slow path requests.
303 */
853e2bd2 304 xid = BNX2FC_MIN_XID;
0eb43b4b 305 num_pri_ios = num_ios - hba->elstm_xids;
853e2bd2
BG
306 for (i = 0; i < num_ios; i++) {
307 io_req = kzalloc(sizeof(*io_req), GFP_KERNEL);
308
309 if (!io_req) {
310 printk(KERN_ERR PFX "failed to alloc io_req\n");
311 goto mem_err;
312 }
313
314 INIT_LIST_HEAD(&io_req->link);
315 INIT_DELAYED_WORK(&io_req->timeout_work, bnx2fc_cmd_timeout);
316
317 io_req->xid = xid++;
0ea5c275
BG
318 if (i < num_pri_ios)
319 list_add_tail(&io_req->link,
320 &cmgr->free_list[io_req->xid %
321 num_possible_cpus()]);
322 else
323 list_add_tail(&io_req->link,
324 &cmgr->free_list[num_possible_cpus()]);
853e2bd2
BG
325 io_req++;
326 }
327
328 /* Allocate pool of io_bdts - one for each bnx2fc_cmd */
329 mem_size = num_ios * sizeof(struct io_bdt *);
330 cmgr->io_bdt_pool = kmalloc(mem_size, GFP_KERNEL);
331 if (!cmgr->io_bdt_pool) {
332 printk(KERN_ERR PFX "failed to alloc io_bdt_pool\n");
333 goto mem_err;
334 }
335
336 mem_size = sizeof(struct io_bdt);
337 for (i = 0; i < num_ios; i++) {
338 cmgr->io_bdt_pool[i] = kmalloc(mem_size, GFP_KERNEL);
339 if (!cmgr->io_bdt_pool[i]) {
340 printk(KERN_ERR PFX "failed to alloc "
341 "io_bdt_pool[%d]\n", i);
342 goto mem_err;
343 }
344 }
345
346 /* Allocate an map fcoe_bdt_ctx structures */
347 bd_tbl_sz = BNX2FC_MAX_BDS_PER_CMD * sizeof(struct fcoe_bd_ctx);
348 for (i = 0; i < num_ios; i++) {
349 bdt_info = cmgr->io_bdt_pool[i];
350 bdt_info->bd_tbl = dma_alloc_coherent(&hba->pcidev->dev,
351 bd_tbl_sz,
352 &bdt_info->bd_tbl_dma,
353 GFP_KERNEL);
354 if (!bdt_info->bd_tbl) {
355 printk(KERN_ERR PFX "failed to alloc "
356 "bdt_tbl[%d]\n", i);
357 goto mem_err;
358 }
359 }
360
361 return cmgr;
362
363mem_err:
364 bnx2fc_cmd_mgr_free(cmgr);
365 return NULL;
366}
367
368void bnx2fc_cmd_mgr_free(struct bnx2fc_cmd_mgr *cmgr)
369{
370 struct io_bdt *bdt_info;
371 struct bnx2fc_hba *hba = cmgr->hba;
372 size_t bd_tbl_sz;
373 u16 min_xid = BNX2FC_MIN_XID;
0eb43b4b 374 u16 max_xid = hba->max_xid;
853e2bd2
BG
375 int num_ios;
376 int i;
377
378 num_ios = max_xid - min_xid + 1;
379
380 /* Free fcoe_bdt_ctx structures */
381 if (!cmgr->io_bdt_pool)
382 goto free_cmd_pool;
383
384 bd_tbl_sz = BNX2FC_MAX_BDS_PER_CMD * sizeof(struct fcoe_bd_ctx);
385 for (i = 0; i < num_ios; i++) {
386 bdt_info = cmgr->io_bdt_pool[i];
387 if (bdt_info->bd_tbl) {
388 dma_free_coherent(&hba->pcidev->dev, bd_tbl_sz,
389 bdt_info->bd_tbl,
390 bdt_info->bd_tbl_dma);
391 bdt_info->bd_tbl = NULL;
392 }
393 }
394
395 /* Destroy io_bdt pool */
396 for (i = 0; i < num_ios; i++) {
397 kfree(cmgr->io_bdt_pool[i]);
398 cmgr->io_bdt_pool[i] = NULL;
399 }
400
401 kfree(cmgr->io_bdt_pool);
402 cmgr->io_bdt_pool = NULL;
403
404free_cmd_pool:
405 kfree(cmgr->free_list_lock);
406
407 /* Destroy cmd pool */
408 if (!cmgr->free_list)
409 goto free_cmgr;
410
0ea5c275 411 for (i = 0; i < num_possible_cpus() + 1; i++) {
d71fb3bd 412 struct bnx2fc_cmd *tmp, *io_req;
853e2bd2 413
d71fb3bd
BPG
414 list_for_each_entry_safe(io_req, tmp,
415 &cmgr->free_list[i], link) {
853e2bd2
BG
416 list_del(&io_req->link);
417 kfree(io_req);
418 }
419 }
420 kfree(cmgr->free_list);
421free_cmgr:
422 /* Free command manager itself */
423 kfree(cmgr);
424}
425
426struct bnx2fc_cmd *bnx2fc_elstm_alloc(struct bnx2fc_rport *tgt, int type)
427{
428 struct fcoe_port *port = tgt->port;
aea71a02
BPG
429 struct bnx2fc_interface *interface = port->priv;
430 struct bnx2fc_cmd_mgr *cmd_mgr = interface->hba->cmd_mgr;
853e2bd2
BG
431 struct bnx2fc_cmd *io_req;
432 struct list_head *listp;
433 struct io_bdt *bd_tbl;
0ea5c275 434 int index = RESERVE_FREE_LIST_INDEX;
619c5cb6 435 u32 free_sqes;
853e2bd2
BG
436 u32 max_sqes;
437 u16 xid;
438
439 max_sqes = tgt->max_sqes;
440 switch (type) {
441 case BNX2FC_TASK_MGMT_CMD:
442 max_sqes = BNX2FC_TM_MAX_SQES;
443 break;
444 case BNX2FC_ELS:
445 max_sqes = BNX2FC_ELS_MAX_SQES;
446 break;
447 default:
448 break;
449 }
450
451 /*
452 * NOTE: Free list insertions and deletions are protected with
453 * cmgr lock
454 */
0ea5c275 455 spin_lock_bh(&cmd_mgr->free_list_lock[index]);
619c5cb6 456 free_sqes = atomic_read(&tgt->free_sqes);
0ea5c275 457 if ((list_empty(&(cmd_mgr->free_list[index]))) ||
619c5cb6
VZ
458 (tgt->num_active_ios.counter >= max_sqes) ||
459 (free_sqes + max_sqes <= BNX2FC_SQ_WQES_MAX)) {
853e2bd2
BG
460 BNX2FC_TGT_DBG(tgt, "No free els_tm cmds available "
461 "ios(%d):sqes(%d)\n",
462 tgt->num_active_ios.counter, tgt->max_sqes);
0ea5c275 463 if (list_empty(&(cmd_mgr->free_list[index])))
853e2bd2 464 printk(KERN_ERR PFX "elstm_alloc: list_empty\n");
0ea5c275 465 spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
853e2bd2
BG
466 return NULL;
467 }
468
469 listp = (struct list_head *)
0ea5c275 470 cmd_mgr->free_list[index].next;
853e2bd2
BG
471 list_del_init(listp);
472 io_req = (struct bnx2fc_cmd *) listp;
473 xid = io_req->xid;
474 cmd_mgr->cmds[xid] = io_req;
475 atomic_inc(&tgt->num_active_ios);
619c5cb6 476 atomic_dec(&tgt->free_sqes);
0ea5c275 477 spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
853e2bd2
BG
478
479 INIT_LIST_HEAD(&io_req->link);
480
481 io_req->port = port;
482 io_req->cmd_mgr = cmd_mgr;
483 io_req->req_flags = 0;
484 io_req->cmd_type = type;
485
486 /* Bind io_bdt for this io_req */
487 /* Have a static link between io_req and io_bdt_pool */
488 bd_tbl = io_req->bd_tbl = cmd_mgr->io_bdt_pool[xid];
489 bd_tbl->io_req = io_req;
490
491 /* Hold the io_req against deletion */
492 kref_init(&io_req->refcount);
493 return io_req;
494}
aea71a02
BPG
495
496struct bnx2fc_cmd *bnx2fc_cmd_alloc(struct bnx2fc_rport *tgt)
853e2bd2
BG
497{
498 struct fcoe_port *port = tgt->port;
aea71a02
BPG
499 struct bnx2fc_interface *interface = port->priv;
500 struct bnx2fc_cmd_mgr *cmd_mgr = interface->hba->cmd_mgr;
853e2bd2
BG
501 struct bnx2fc_cmd *io_req;
502 struct list_head *listp;
503 struct io_bdt *bd_tbl;
619c5cb6 504 u32 free_sqes;
853e2bd2
BG
505 u32 max_sqes;
506 u16 xid;
0ea5c275 507 int index = get_cpu();
853e2bd2
BG
508
509 max_sqes = BNX2FC_SCSI_MAX_SQES;
510 /*
511 * NOTE: Free list insertions and deletions are protected with
512 * cmgr lock
513 */
0ea5c275 514 spin_lock_bh(&cmd_mgr->free_list_lock[index]);
619c5cb6 515 free_sqes = atomic_read(&tgt->free_sqes);
0ea5c275 516 if ((list_empty(&cmd_mgr->free_list[index])) ||
619c5cb6
VZ
517 (tgt->num_active_ios.counter >= max_sqes) ||
518 (free_sqes + max_sqes <= BNX2FC_SQ_WQES_MAX)) {
0ea5c275
BG
519 spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
520 put_cpu();
853e2bd2
BG
521 return NULL;
522 }
523
524 listp = (struct list_head *)
0ea5c275 525 cmd_mgr->free_list[index].next;
853e2bd2
BG
526 list_del_init(listp);
527 io_req = (struct bnx2fc_cmd *) listp;
528 xid = io_req->xid;
529 cmd_mgr->cmds[xid] = io_req;
530 atomic_inc(&tgt->num_active_ios);
619c5cb6 531 atomic_dec(&tgt->free_sqes);
0ea5c275
BG
532 spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
533 put_cpu();
853e2bd2
BG
534
535 INIT_LIST_HEAD(&io_req->link);
536
537 io_req->port = port;
538 io_req->cmd_mgr = cmd_mgr;
539 io_req->req_flags = 0;
540
541 /* Bind io_bdt for this io_req */
542 /* Have a static link between io_req and io_bdt_pool */
543 bd_tbl = io_req->bd_tbl = cmd_mgr->io_bdt_pool[xid];
544 bd_tbl->io_req = io_req;
545
546 /* Hold the io_req against deletion */
547 kref_init(&io_req->refcount);
548 return io_req;
549}
550
551void bnx2fc_cmd_release(struct kref *ref)
552{
553 struct bnx2fc_cmd *io_req = container_of(ref,
554 struct bnx2fc_cmd, refcount);
555 struct bnx2fc_cmd_mgr *cmd_mgr = io_req->cmd_mgr;
0ea5c275
BG
556 int index;
557
558 if (io_req->cmd_type == BNX2FC_SCSI_CMD)
559 index = io_req->xid % num_possible_cpus();
560 else
561 index = RESERVE_FREE_LIST_INDEX;
853e2bd2 562
0ea5c275
BG
563
564 spin_lock_bh(&cmd_mgr->free_list_lock[index]);
853e2bd2
BG
565 if (io_req->cmd_type != BNX2FC_SCSI_CMD)
566 bnx2fc_free_mp_resc(io_req);
567 cmd_mgr->cmds[io_req->xid] = NULL;
568 /* Delete IO from retire queue */
569 list_del_init(&io_req->link);
570 /* Add it to the free list */
571 list_add(&io_req->link,
0ea5c275 572 &cmd_mgr->free_list[index]);
853e2bd2 573 atomic_dec(&io_req->tgt->num_active_ios);
0ea5c275
BG
574 spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
575
853e2bd2
BG
576}
577
578static void bnx2fc_free_mp_resc(struct bnx2fc_cmd *io_req)
579{
580 struct bnx2fc_mp_req *mp_req = &(io_req->mp_req);
aea71a02
BPG
581 struct bnx2fc_interface *interface = io_req->port->priv;
582 struct bnx2fc_hba *hba = interface->hba;
853e2bd2
BG
583 size_t sz = sizeof(struct fcoe_bd_ctx);
584
585 /* clear tm flags */
586 mp_req->tm_flags = 0;
587 if (mp_req->mp_req_bd) {
588 dma_free_coherent(&hba->pcidev->dev, sz,
589 mp_req->mp_req_bd,
590 mp_req->mp_req_bd_dma);
591 mp_req->mp_req_bd = NULL;
592 }
593 if (mp_req->mp_resp_bd) {
594 dma_free_coherent(&hba->pcidev->dev, sz,
595 mp_req->mp_resp_bd,
596 mp_req->mp_resp_bd_dma);
597 mp_req->mp_resp_bd = NULL;
598 }
599 if (mp_req->req_buf) {
be1fefc2 600 dma_free_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
853e2bd2
BG
601 mp_req->req_buf,
602 mp_req->req_buf_dma);
603 mp_req->req_buf = NULL;
604 }
605 if (mp_req->resp_buf) {
be1fefc2 606 dma_free_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
853e2bd2
BG
607 mp_req->resp_buf,
608 mp_req->resp_buf_dma);
609 mp_req->resp_buf = NULL;
610 }
611}
612
613int bnx2fc_init_mp_req(struct bnx2fc_cmd *io_req)
614{
615 struct bnx2fc_mp_req *mp_req;
616 struct fcoe_bd_ctx *mp_req_bd;
617 struct fcoe_bd_ctx *mp_resp_bd;
aea71a02
BPG
618 struct bnx2fc_interface *interface = io_req->port->priv;
619 struct bnx2fc_hba *hba = interface->hba;
853e2bd2
BG
620 dma_addr_t addr;
621 size_t sz;
622
623 mp_req = (struct bnx2fc_mp_req *)&(io_req->mp_req);
624 memset(mp_req, 0, sizeof(struct bnx2fc_mp_req));
625
1fffa199
CD
626 if (io_req->cmd_type != BNX2FC_ELS) {
627 mp_req->req_len = sizeof(struct fcp_cmnd);
628 io_req->data_xfer_len = mp_req->req_len;
629 } else
630 mp_req->req_len = io_req->data_xfer_len;
631
be1fefc2 632 mp_req->req_buf = dma_alloc_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
853e2bd2
BG
633 &mp_req->req_buf_dma,
634 GFP_ATOMIC);
635 if (!mp_req->req_buf) {
636 printk(KERN_ERR PFX "unable to alloc MP req buffer\n");
637 bnx2fc_free_mp_resc(io_req);
638 return FAILED;
639 }
640
be1fefc2 641 mp_req->resp_buf = dma_alloc_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
853e2bd2
BG
642 &mp_req->resp_buf_dma,
643 GFP_ATOMIC);
644 if (!mp_req->resp_buf) {
645 printk(KERN_ERR PFX "unable to alloc TM resp buffer\n");
646 bnx2fc_free_mp_resc(io_req);
647 return FAILED;
648 }
be1fefc2
MC
649 memset(mp_req->req_buf, 0, CNIC_PAGE_SIZE);
650 memset(mp_req->resp_buf, 0, CNIC_PAGE_SIZE);
853e2bd2
BG
651
652 /* Allocate and map mp_req_bd and mp_resp_bd */
653 sz = sizeof(struct fcoe_bd_ctx);
654 mp_req->mp_req_bd = dma_alloc_coherent(&hba->pcidev->dev, sz,
655 &mp_req->mp_req_bd_dma,
656 GFP_ATOMIC);
657 if (!mp_req->mp_req_bd) {
658 printk(KERN_ERR PFX "unable to alloc MP req bd\n");
659 bnx2fc_free_mp_resc(io_req);
660 return FAILED;
661 }
662 mp_req->mp_resp_bd = dma_alloc_coherent(&hba->pcidev->dev, sz,
663 &mp_req->mp_resp_bd_dma,
664 GFP_ATOMIC);
b0d5e15c 665 if (!mp_req->mp_resp_bd) {
853e2bd2
BG
666 printk(KERN_ERR PFX "unable to alloc MP resp bd\n");
667 bnx2fc_free_mp_resc(io_req);
668 return FAILED;
669 }
670 /* Fill bd table */
671 addr = mp_req->req_buf_dma;
672 mp_req_bd = mp_req->mp_req_bd;
673 mp_req_bd->buf_addr_lo = (u32)addr & 0xffffffff;
674 mp_req_bd->buf_addr_hi = (u32)((u64)addr >> 32);
be1fefc2 675 mp_req_bd->buf_len = CNIC_PAGE_SIZE;
853e2bd2
BG
676 mp_req_bd->flags = 0;
677
678 /*
679 * MP buffer is either a task mgmt command or an ELS.
680 * So the assumption is that it consumes a single bd
681 * entry in the bd table
682 */
683 mp_resp_bd = mp_req->mp_resp_bd;
684 addr = mp_req->resp_buf_dma;
685 mp_resp_bd->buf_addr_lo = (u32)addr & 0xffffffff;
686 mp_resp_bd->buf_addr_hi = (u32)((u64)addr >> 32);
be1fefc2 687 mp_resp_bd->buf_len = CNIC_PAGE_SIZE;
853e2bd2
BG
688 mp_resp_bd->flags = 0;
689
690 return SUCCESS;
691}
692
693static int bnx2fc_initiate_tmf(struct scsi_cmnd *sc_cmd, u8 tm_flags)
694{
695 struct fc_lport *lport;
33c7da05
JL
696 struct fc_rport *rport;
697 struct fc_rport_libfc_priv *rp;
853e2bd2 698 struct fcoe_port *port;
aea71a02 699 struct bnx2fc_interface *interface;
853e2bd2
BG
700 struct bnx2fc_rport *tgt;
701 struct bnx2fc_cmd *io_req;
702 struct bnx2fc_mp_req *tm_req;
703 struct fcoe_task_ctx_entry *task;
704 struct fcoe_task_ctx_entry *task_page;
705 struct Scsi_Host *host = sc_cmd->device->host;
706 struct fc_frame_header *fc_hdr;
707 struct fcp_cmnd *fcp_cmnd;
708 int task_idx, index;
709 int rc = SUCCESS;
710 u16 xid;
711 u32 sid, did;
712 unsigned long start = jiffies;
713
714 lport = shost_priv(host);
33c7da05 715 rport = starget_to_rport(scsi_target(sc_cmd->device));
853e2bd2 716 port = lport_priv(lport);
aea71a02 717 interface = port->priv;
853e2bd2
BG
718
719 if (rport == NULL) {
b2a554ff 720 printk(KERN_ERR PFX "device_reset: rport is NULL\n");
853e2bd2
BG
721 rc = FAILED;
722 goto tmf_err;
723 }
33c7da05 724 rp = rport->dd_data;
853e2bd2
BG
725
726 rc = fc_block_scsi_eh(sc_cmd);
727 if (rc)
728 return rc;
729
730 if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
731 printk(KERN_ERR PFX "device_reset: link is not ready\n");
732 rc = FAILED;
733 goto tmf_err;
734 }
735 /* rport and tgt are allocated together, so tgt should be non-NULL */
736 tgt = (struct bnx2fc_rport *)&rp[1];
737
738 if (!(test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
739 printk(KERN_ERR PFX "device_reset: tgt not offloaded\n");
740 rc = FAILED;
741 goto tmf_err;
742 }
743retry_tmf:
744 io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_TASK_MGMT_CMD);
745 if (!io_req) {
746 if (time_after(jiffies, start + HZ)) {
747 printk(KERN_ERR PFX "tmf: Failed TMF");
748 rc = FAILED;
749 goto tmf_err;
750 }
751 msleep(20);
752 goto retry_tmf;
753 }
754 /* Initialize rest of io_req fields */
755 io_req->sc_cmd = sc_cmd;
756 io_req->port = port;
757 io_req->tgt = tgt;
758
759 tm_req = (struct bnx2fc_mp_req *)&(io_req->mp_req);
760
761 rc = bnx2fc_init_mp_req(io_req);
762 if (rc == FAILED) {
763 printk(KERN_ERR PFX "Task mgmt MP request init failed\n");
bd4d5de8 764 spin_lock_bh(&tgt->tgt_lock);
853e2bd2 765 kref_put(&io_req->refcount, bnx2fc_cmd_release);
bd4d5de8 766 spin_unlock_bh(&tgt->tgt_lock);
853e2bd2
BG
767 goto tmf_err;
768 }
769
770 /* Set TM flags */
771 io_req->io_req_flags = 0;
772 tm_req->tm_flags = tm_flags;
773
774 /* Fill FCP_CMND */
775 bnx2fc_build_fcp_cmnd(io_req, (struct fcp_cmnd *)tm_req->req_buf);
776 fcp_cmnd = (struct fcp_cmnd *)tm_req->req_buf;
777 memset(fcp_cmnd->fc_cdb, 0, sc_cmd->cmd_len);
778 fcp_cmnd->fc_dl = 0;
779
780 /* Fill FC header */
781 fc_hdr = &(tm_req->req_fc_hdr);
782 sid = tgt->sid;
783 did = rport->port_id;
784 __fc_fill_fc_hdr(fc_hdr, FC_RCTL_DD_UNSOL_CMD, did, sid,
785 FC_TYPE_FCP, FC_FC_FIRST_SEQ | FC_FC_END_SEQ |
786 FC_FC_SEQ_INIT, 0);
787 /* Obtain exchange id */
788 xid = io_req->xid;
789
790 BNX2FC_TGT_DBG(tgt, "Initiate TMF - xid = 0x%x\n", xid);
791 task_idx = xid/BNX2FC_TASKS_PER_PAGE;
792 index = xid % BNX2FC_TASKS_PER_PAGE;
793
794 /* Initialize task context for this IO request */
aea71a02
BPG
795 task_page = (struct fcoe_task_ctx_entry *)
796 interface->hba->task_ctx[task_idx];
853e2bd2
BG
797 task = &(task_page[index]);
798 bnx2fc_init_mp_task(io_req, task);
799
800 sc_cmd->SCp.ptr = (char *)io_req;
801
802 /* Obtain free SQ entry */
803 spin_lock_bh(&tgt->tgt_lock);
804 bnx2fc_add_2_sq(tgt, xid);
805
806 /* Enqueue the io_req to active_tm_queue */
807 io_req->on_tmf_queue = 1;
808 list_add_tail(&io_req->link, &tgt->active_tm_queue);
809
810 init_completion(&io_req->tm_done);
811 io_req->wait_for_comp = 1;
812
813 /* Ring doorbell */
814 bnx2fc_ring_doorbell(tgt);
815 spin_unlock_bh(&tgt->tgt_lock);
816
817 rc = wait_for_completion_timeout(&io_req->tm_done,
818 BNX2FC_TM_TIMEOUT * HZ);
819 spin_lock_bh(&tgt->tgt_lock);
820
821 io_req->wait_for_comp = 0;
92886c9c 822 if (!(test_bit(BNX2FC_FLAG_TM_COMPL, &io_req->req_flags))) {
853e2bd2 823 set_bit(BNX2FC_FLAG_TM_TIMEOUT, &io_req->req_flags);
92886c9c
BPG
824 if (io_req->on_tmf_queue) {
825 list_del_init(&io_req->link);
826 io_req->on_tmf_queue = 0;
827 }
828 io_req->wait_for_comp = 1;
829 bnx2fc_initiate_cleanup(io_req);
830 spin_unlock_bh(&tgt->tgt_lock);
831 rc = wait_for_completion_timeout(&io_req->tm_done,
832 BNX2FC_FW_TIMEOUT);
833 spin_lock_bh(&tgt->tgt_lock);
834 io_req->wait_for_comp = 0;
835 if (!rc)
836 kref_put(&io_req->refcount, bnx2fc_cmd_release);
837 }
853e2bd2
BG
838
839 spin_unlock_bh(&tgt->tgt_lock);
840
841 if (!rc) {
b2a554ff 842 BNX2FC_TGT_DBG(tgt, "task mgmt command failed...\n");
853e2bd2
BG
843 rc = FAILED;
844 } else {
b2a554ff 845 BNX2FC_TGT_DBG(tgt, "task mgmt command success...\n");
853e2bd2
BG
846 rc = SUCCESS;
847 }
848tmf_err:
849 return rc;
850}
851
852int bnx2fc_initiate_abts(struct bnx2fc_cmd *io_req)
853{
854 struct fc_lport *lport;
855 struct bnx2fc_rport *tgt = io_req->tgt;
856 struct fc_rport *rport = tgt->rport;
857 struct fc_rport_priv *rdata = tgt->rdata;
aea71a02 858 struct bnx2fc_interface *interface;
853e2bd2
BG
859 struct fcoe_port *port;
860 struct bnx2fc_cmd *abts_io_req;
861 struct fcoe_task_ctx_entry *task;
862 struct fcoe_task_ctx_entry *task_page;
863 struct fc_frame_header *fc_hdr;
864 struct bnx2fc_mp_req *abts_req;
865 int task_idx, index;
866 u32 sid, did;
867 u16 xid;
868 int rc = SUCCESS;
869 u32 r_a_tov = rdata->r_a_tov;
870
871 /* called with tgt_lock held */
872 BNX2FC_IO_DBG(io_req, "Entered bnx2fc_initiate_abts\n");
873
874 port = io_req->port;
aea71a02 875 interface = port->priv;
853e2bd2
BG
876 lport = port->lport;
877
878 if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
879 printk(KERN_ERR PFX "initiate_abts: tgt not offloaded\n");
880 rc = FAILED;
881 goto abts_err;
882 }
883
884 if (rport == NULL) {
b2a554ff 885 printk(KERN_ERR PFX "initiate_abts: rport is NULL\n");
853e2bd2
BG
886 rc = FAILED;
887 goto abts_err;
888 }
889
890 if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
891 printk(KERN_ERR PFX "initiate_abts: link is not ready\n");
892 rc = FAILED;
893 goto abts_err;
894 }
895
896 abts_io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_ABTS);
897 if (!abts_io_req) {
898 printk(KERN_ERR PFX "abts: couldnt allocate cmd\n");
899 rc = FAILED;
900 goto abts_err;
901 }
902
903 /* Initialize rest of io_req fields */
904 abts_io_req->sc_cmd = NULL;
905 abts_io_req->port = port;
906 abts_io_req->tgt = tgt;
907 abts_io_req->data_xfer_len = 0; /* No data transfer for ABTS */
908
909 abts_req = (struct bnx2fc_mp_req *)&(abts_io_req->mp_req);
910 memset(abts_req, 0, sizeof(struct bnx2fc_mp_req));
911
912 /* Fill FC header */
913 fc_hdr = &(abts_req->req_fc_hdr);
914
915 /* Obtain oxid and rxid for the original exchange to be aborted */
916 fc_hdr->fh_ox_id = htons(io_req->xid);
619c5cb6 917 fc_hdr->fh_rx_id = htons(io_req->task->rxwr_txrd.var_ctx.rx_id);
853e2bd2
BG
918
919 sid = tgt->sid;
920 did = rport->port_id;
921
922 __fc_fill_fc_hdr(fc_hdr, FC_RCTL_BA_ABTS, did, sid,
923 FC_TYPE_BLS, FC_FC_FIRST_SEQ | FC_FC_END_SEQ |
924 FC_FC_SEQ_INIT, 0);
925
926 xid = abts_io_req->xid;
927 BNX2FC_IO_DBG(abts_io_req, "ABTS io_req\n");
928 task_idx = xid/BNX2FC_TASKS_PER_PAGE;
929 index = xid % BNX2FC_TASKS_PER_PAGE;
930
931 /* Initialize task context for this IO request */
aea71a02
BPG
932 task_page = (struct fcoe_task_ctx_entry *)
933 interface->hba->task_ctx[task_idx];
853e2bd2
BG
934 task = &(task_page[index]);
935 bnx2fc_init_mp_task(abts_io_req, task);
936
937 /*
938 * ABTS task is a temporary task that will be cleaned up
939 * irrespective of ABTS response. We need to start the timer
940 * for the original exchange, as the CQE is posted for the original
941 * IO request.
942 *
943 * Timer for ABTS is started only when it is originated by a
944 * TM request. For the ABTS issued as part of ULP timeout,
945 * scsi-ml maintains the timers.
946 */
947
948 /* if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags))*/
949 bnx2fc_cmd_timer_set(io_req, 2 * r_a_tov);
950
951 /* Obtain free SQ entry */
952 bnx2fc_add_2_sq(tgt, xid);
953
954 /* Ring doorbell */
955 bnx2fc_ring_doorbell(tgt);
956
957abts_err:
958 return rc;
959}
960
6c5a7ce4
BPG
961int bnx2fc_initiate_seq_cleanup(struct bnx2fc_cmd *orig_io_req, u32 offset,
962 enum fc_rctl r_ctl)
963{
964 struct fc_lport *lport;
965 struct bnx2fc_rport *tgt = orig_io_req->tgt;
966 struct bnx2fc_interface *interface;
967 struct fcoe_port *port;
968 struct bnx2fc_cmd *seq_clnp_req;
969 struct fcoe_task_ctx_entry *task;
970 struct fcoe_task_ctx_entry *task_page;
971 struct bnx2fc_els_cb_arg *cb_arg = NULL;
972 int task_idx, index;
973 u16 xid;
974 int rc = 0;
975
976 BNX2FC_IO_DBG(orig_io_req, "bnx2fc_initiate_seq_cleanup xid = 0x%x\n",
977 orig_io_req->xid);
978 kref_get(&orig_io_req->refcount);
979
980 port = orig_io_req->port;
981 interface = port->priv;
982 lport = port->lport;
983
984 cb_arg = kzalloc(sizeof(struct bnx2fc_els_cb_arg), GFP_ATOMIC);
985 if (!cb_arg) {
986 printk(KERN_ERR PFX "Unable to alloc cb_arg for seq clnup\n");
987 rc = -ENOMEM;
988 goto cleanup_err;
989 }
990
991 seq_clnp_req = bnx2fc_elstm_alloc(tgt, BNX2FC_SEQ_CLEANUP);
992 if (!seq_clnp_req) {
993 printk(KERN_ERR PFX "cleanup: couldnt allocate cmd\n");
994 rc = -ENOMEM;
995 kfree(cb_arg);
996 goto cleanup_err;
997 }
998 /* Initialize rest of io_req fields */
999 seq_clnp_req->sc_cmd = NULL;
1000 seq_clnp_req->port = port;
1001 seq_clnp_req->tgt = tgt;
1002 seq_clnp_req->data_xfer_len = 0; /* No data transfer for cleanup */
1003
1004 xid = seq_clnp_req->xid;
1005
1006 task_idx = xid/BNX2FC_TASKS_PER_PAGE;
1007 index = xid % BNX2FC_TASKS_PER_PAGE;
1008
1009 /* Initialize task context for this IO request */
1010 task_page = (struct fcoe_task_ctx_entry *)
1011 interface->hba->task_ctx[task_idx];
1012 task = &(task_page[index]);
1013 cb_arg->aborted_io_req = orig_io_req;
1014 cb_arg->io_req = seq_clnp_req;
1015 cb_arg->r_ctl = r_ctl;
1016 cb_arg->offset = offset;
1017 seq_clnp_req->cb_arg = cb_arg;
1018
1019 printk(KERN_ERR PFX "call init_seq_cleanup_task\n");
1020 bnx2fc_init_seq_cleanup_task(seq_clnp_req, task, orig_io_req, offset);
1021
1022 /* Obtain free SQ entry */
1023 bnx2fc_add_2_sq(tgt, xid);
1024
1025 /* Ring doorbell */
1026 bnx2fc_ring_doorbell(tgt);
1027cleanup_err:
1028 return rc;
1029}
1030
853e2bd2
BG
1031int bnx2fc_initiate_cleanup(struct bnx2fc_cmd *io_req)
1032{
1033 struct fc_lport *lport;
1034 struct bnx2fc_rport *tgt = io_req->tgt;
aea71a02 1035 struct bnx2fc_interface *interface;
853e2bd2
BG
1036 struct fcoe_port *port;
1037 struct bnx2fc_cmd *cleanup_io_req;
1038 struct fcoe_task_ctx_entry *task;
1039 struct fcoe_task_ctx_entry *task_page;
1040 int task_idx, index;
1041 u16 xid, orig_xid;
1042 int rc = 0;
1043
1044 /* ASSUMPTION: called with tgt_lock held */
1045 BNX2FC_IO_DBG(io_req, "Entered bnx2fc_initiate_cleanup\n");
1046
1047 port = io_req->port;
aea71a02 1048 interface = port->priv;
853e2bd2
BG
1049 lport = port->lport;
1050
1051 cleanup_io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_CLEANUP);
1052 if (!cleanup_io_req) {
1053 printk(KERN_ERR PFX "cleanup: couldnt allocate cmd\n");
1054 rc = -1;
1055 goto cleanup_err;
1056 }
1057
1058 /* Initialize rest of io_req fields */
1059 cleanup_io_req->sc_cmd = NULL;
1060 cleanup_io_req->port = port;
1061 cleanup_io_req->tgt = tgt;
1062 cleanup_io_req->data_xfer_len = 0; /* No data transfer for cleanup */
1063
1064 xid = cleanup_io_req->xid;
1065
1066 task_idx = xid/BNX2FC_TASKS_PER_PAGE;
1067 index = xid % BNX2FC_TASKS_PER_PAGE;
1068
1069 /* Initialize task context for this IO request */
aea71a02
BPG
1070 task_page = (struct fcoe_task_ctx_entry *)
1071 interface->hba->task_ctx[task_idx];
853e2bd2
BG
1072 task = &(task_page[index]);
1073 orig_xid = io_req->xid;
1074
1075 BNX2FC_IO_DBG(io_req, "CLEANUP io_req xid = 0x%x\n", xid);
1076
1077 bnx2fc_init_cleanup_task(cleanup_io_req, task, orig_xid);
1078
1079 /* Obtain free SQ entry */
1080 bnx2fc_add_2_sq(tgt, xid);
1081
1082 /* Ring doorbell */
1083 bnx2fc_ring_doorbell(tgt);
1084
1085cleanup_err:
1086 return rc;
1087}
1088
1089/**
1090 * bnx2fc_eh_target_reset: Reset a target
1091 *
1092 * @sc_cmd: SCSI command
1093 *
1094 * Set from SCSI host template to send task mgmt command to the target
1095 * and wait for the response
1096 */
1097int bnx2fc_eh_target_reset(struct scsi_cmnd *sc_cmd)
1098{
1099 return bnx2fc_initiate_tmf(sc_cmd, FCP_TMF_TGT_RESET);
1100}
1101
1102/**
1103 * bnx2fc_eh_device_reset - Reset a single LUN
1104 *
1105 * @sc_cmd: SCSI command
1106 *
1107 * Set from SCSI host template to send task mgmt command to the target
1108 * and wait for the response
1109 */
1110int bnx2fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
1111{
1112 return bnx2fc_initiate_tmf(sc_cmd, FCP_TMF_LUN_RESET);
1113}
1114
c1bb4f33
BPG
1115int bnx2fc_expl_logo(struct fc_lport *lport, struct bnx2fc_cmd *io_req)
1116{
1117 struct bnx2fc_rport *tgt = io_req->tgt;
1118 struct fc_rport_priv *rdata = tgt->rdata;
1119 int logo_issued;
1120 int rc = SUCCESS;
1121 int wait_cnt = 0;
1122
1123 BNX2FC_IO_DBG(io_req, "Expl logo - tgt flags = 0x%lx\n",
1124 tgt->flags);
1125 logo_issued = test_and_set_bit(BNX2FC_FLAG_EXPL_LOGO,
1126 &tgt->flags);
1127 io_req->wait_for_comp = 1;
1128 bnx2fc_initiate_cleanup(io_req);
1129
1130 spin_unlock_bh(&tgt->tgt_lock);
1131
1132 wait_for_completion(&io_req->tm_done);
1133
1134 io_req->wait_for_comp = 0;
1135 /*
1136 * release the reference taken in eh_abort to allow the
1137 * target to re-login after flushing IOs
1138 */
1139 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1140
1141 if (!logo_issued) {
1142 clear_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags);
1143 mutex_lock(&lport->disc.disc_mutex);
1144 lport->tt.rport_logoff(rdata);
1145 mutex_unlock(&lport->disc.disc_mutex);
1146 do {
1147 msleep(BNX2FC_RELOGIN_WAIT_TIME);
1148 if (wait_cnt++ > BNX2FC_RELOGIN_WAIT_CNT) {
1149 rc = FAILED;
1150 break;
1151 }
1152 } while (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags));
1153 }
1154 spin_lock_bh(&tgt->tgt_lock);
1155 return rc;
1156}
853e2bd2
BG
1157/**
1158 * bnx2fc_eh_abort - eh_abort_handler api to abort an outstanding
1159 * SCSI command
1160 *
1161 * @sc_cmd: SCSI_ML command pointer
1162 *
1163 * SCSI abort request handler
1164 */
1165int bnx2fc_eh_abort(struct scsi_cmnd *sc_cmd)
1166{
1167 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
1168 struct fc_rport_libfc_priv *rp = rport->dd_data;
1169 struct bnx2fc_cmd *io_req;
1170 struct fc_lport *lport;
1171 struct bnx2fc_rport *tgt;
1172 int rc = FAILED;
1173
1174
1175 rc = fc_block_scsi_eh(sc_cmd);
1176 if (rc)
1177 return rc;
1178
1179 lport = shost_priv(sc_cmd->device->host);
1180 if ((lport->state != LPORT_ST_READY) || !(lport->link_up)) {
b2a554ff 1181 printk(KERN_ERR PFX "eh_abort: link not ready\n");
853e2bd2
BG
1182 return rc;
1183 }
1184
1185 tgt = (struct bnx2fc_rport *)&rp[1];
1186
1187 BNX2FC_TGT_DBG(tgt, "Entered bnx2fc_eh_abort\n");
1188
1189 spin_lock_bh(&tgt->tgt_lock);
1190 io_req = (struct bnx2fc_cmd *)sc_cmd->SCp.ptr;
1191 if (!io_req) {
1192 /* Command might have just completed */
1193 printk(KERN_ERR PFX "eh_abort: io_req is NULL\n");
1194 spin_unlock_bh(&tgt->tgt_lock);
1195 return SUCCESS;
1196 }
1197 BNX2FC_IO_DBG(io_req, "eh_abort - refcnt = %d\n",
1198 io_req->refcount.refcount.counter);
1199
1200 /* Hold IO request across abort processing */
1201 kref_get(&io_req->refcount);
1202
1203 BUG_ON(tgt != io_req->tgt);
1204
1205 /* Remove the io_req from the active_q. */
1206 /*
1207 * Task Mgmt functions (LUN RESET & TGT RESET) will not
1208 * issue an ABTS on this particular IO req, as the
1209 * io_req is no longer in the active_q.
1210 */
1211 if (tgt->flush_in_prog) {
b2a554ff 1212 printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
853e2bd2
BG
1213 "flush in progress\n", io_req->xid);
1214 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1215 spin_unlock_bh(&tgt->tgt_lock);
1216 return SUCCESS;
1217 }
1218
1219 if (io_req->on_active_queue == 0) {
b2a554ff 1220 printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
853e2bd2
BG
1221 "not on active_q\n", io_req->xid);
1222 /*
1223 * This condition can happen only due to the FW bug,
1224 * where we do not receive cleanup response from
1225 * the FW. Handle this case gracefully by erroring
1226 * back the IO request to SCSI-ml
1227 */
1228 bnx2fc_scsi_done(io_req, DID_ABORT);
1229
1230 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1231 spin_unlock_bh(&tgt->tgt_lock);
1232 return SUCCESS;
1233 }
1234
1235 /*
1236 * Only eh_abort processing will remove the IO from
1237 * active_cmd_q before processing the request. this is
1238 * done to avoid race conditions between IOs aborted
1239 * as part of task management completion and eh_abort
1240 * processing
1241 */
1242 list_del_init(&io_req->link);
1243 io_req->on_active_queue = 0;
1244 /* Move IO req to retire queue */
1245 list_add_tail(&io_req->link, &tgt->io_retire_queue);
1246
1247 init_completion(&io_req->tm_done);
853e2bd2 1248
c1bb4f33 1249 if (test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags)) {
b2a554ff 1250 printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
853e2bd2 1251 "already in abts processing\n", io_req->xid);
99cc600c
BPG
1252 if (cancel_delayed_work(&io_req->timeout_work))
1253 kref_put(&io_req->refcount,
1254 bnx2fc_cmd_release); /* drop timer hold */
c1bb4f33 1255 rc = bnx2fc_expl_logo(lport, io_req);
21837896
EW
1256 /* This only occurs when an task abort was requested while ABTS
1257 is in progress. Setting the IO_CLEANUP flag will skip the
1258 RRQ process in the case when the fw generated SCSI_CMD cmpl
1259 was a result from the ABTS request rather than the CLEANUP
1260 request */
1261 set_bit(BNX2FC_FLAG_IO_CLEANUP, &io_req->req_flags);
c1bb4f33
BPG
1262 goto out;
1263 }
99cc600c 1264
c1bb4f33
BPG
1265 /* Cancel the current timer running on this io_req */
1266 if (cancel_delayed_work(&io_req->timeout_work))
1267 kref_put(&io_req->refcount,
1268 bnx2fc_cmd_release); /* drop timer hold */
1269 set_bit(BNX2FC_FLAG_EH_ABORT, &io_req->req_flags);
1270 io_req->wait_for_comp = 1;
1271 rc = bnx2fc_initiate_abts(io_req);
1272 if (rc == FAILED) {
1273 bnx2fc_initiate_cleanup(io_req);
99cc600c 1274 spin_unlock_bh(&tgt->tgt_lock);
99cc600c 1275 wait_for_completion(&io_req->tm_done);
99cc600c
BPG
1276 spin_lock_bh(&tgt->tgt_lock);
1277 io_req->wait_for_comp = 0;
c1bb4f33 1278 goto done;
853e2bd2
BG
1279 }
1280 spin_unlock_bh(&tgt->tgt_lock);
1281
1282 wait_for_completion(&io_req->tm_done);
1283
1284 spin_lock_bh(&tgt->tgt_lock);
1285 io_req->wait_for_comp = 0;
5d78f175
BPG
1286 if (test_bit(BNX2FC_FLAG_IO_COMPL, &io_req->req_flags)) {
1287 BNX2FC_IO_DBG(io_req, "IO completed in a different context\n");
1288 rc = SUCCESS;
1289 } else if (!(test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
1290 &io_req->req_flags))) {
853e2bd2
BG
1291 /* Let the scsi-ml try to recover this command */
1292 printk(KERN_ERR PFX "abort failed, xid = 0x%x\n",
1293 io_req->xid);
c1bb4f33
BPG
1294 rc = bnx2fc_expl_logo(lport, io_req);
1295 goto out;
853e2bd2
BG
1296 } else {
1297 /*
1298 * We come here even when there was a race condition
1299 * between timeout and abts completion, and abts
1300 * completion happens just in time.
1301 */
1302 BNX2FC_IO_DBG(io_req, "abort succeeded\n");
1303 rc = SUCCESS;
1304 bnx2fc_scsi_done(io_req, DID_ABORT);
1305 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1306 }
c1bb4f33 1307done:
853e2bd2
BG
1308 /* release the reference taken in eh_abort */
1309 kref_put(&io_req->refcount, bnx2fc_cmd_release);
c1bb4f33 1310out:
853e2bd2
BG
1311 spin_unlock_bh(&tgt->tgt_lock);
1312 return rc;
1313}
1314
6c5a7ce4
BPG
1315void bnx2fc_process_seq_cleanup_compl(struct bnx2fc_cmd *seq_clnp_req,
1316 struct fcoe_task_ctx_entry *task,
1317 u8 rx_state)
1318{
1319 struct bnx2fc_els_cb_arg *cb_arg = seq_clnp_req->cb_arg;
1320 struct bnx2fc_cmd *orig_io_req = cb_arg->aborted_io_req;
1321 u32 offset = cb_arg->offset;
1322 enum fc_rctl r_ctl = cb_arg->r_ctl;
1323 int rc = 0;
1324 struct bnx2fc_rport *tgt = orig_io_req->tgt;
1325
1326 BNX2FC_IO_DBG(orig_io_req, "Entered process_cleanup_compl xid = 0x%x"
1327 "cmd_type = %d\n",
1328 seq_clnp_req->xid, seq_clnp_req->cmd_type);
1329
1330 if (rx_state == FCOE_TASK_RX_STATE_IGNORED_SEQUENCE_CLEANUP) {
1331 printk(KERN_ERR PFX "seq cleanup ignored - xid = 0x%x\n",
1332 seq_clnp_req->xid);
1333 goto free_cb_arg;
1334 }
6c5a7ce4
BPG
1335
1336 spin_unlock_bh(&tgt->tgt_lock);
1337 rc = bnx2fc_send_srr(orig_io_req, offset, r_ctl);
1338 spin_lock_bh(&tgt->tgt_lock);
1339
1340 if (rc)
1341 printk(KERN_ERR PFX "clnup_compl: Unable to send SRR"
1342 " IO will abort\n");
1343 seq_clnp_req->cb_arg = NULL;
1344 kref_put(&orig_io_req->refcount, bnx2fc_cmd_release);
1345free_cb_arg:
1346 kfree(cb_arg);
1347 return;
1348}
1349
853e2bd2
BG
1350void bnx2fc_process_cleanup_compl(struct bnx2fc_cmd *io_req,
1351 struct fcoe_task_ctx_entry *task,
1352 u8 num_rq)
1353{
1354 BNX2FC_IO_DBG(io_req, "Entered process_cleanup_compl "
1355 "refcnt = %d, cmd_type = %d\n",
1356 io_req->refcount.refcount.counter, io_req->cmd_type);
1357 bnx2fc_scsi_done(io_req, DID_ERROR);
1358 kref_put(&io_req->refcount, bnx2fc_cmd_release);
99cc600c
BPG
1359 if (io_req->wait_for_comp)
1360 complete(&io_req->tm_done);
853e2bd2
BG
1361}
1362
1363void bnx2fc_process_abts_compl(struct bnx2fc_cmd *io_req,
1364 struct fcoe_task_ctx_entry *task,
1365 u8 num_rq)
1366{
1367 u32 r_ctl;
1368 u32 r_a_tov = FC_DEF_R_A_TOV;
1369 u8 issue_rrq = 0;
1370 struct bnx2fc_rport *tgt = io_req->tgt;
1371
1372 BNX2FC_IO_DBG(io_req, "Entered process_abts_compl xid = 0x%x"
1373 "refcnt = %d, cmd_type = %d\n",
1374 io_req->xid,
1375 io_req->refcount.refcount.counter, io_req->cmd_type);
1376
1377 if (test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
1378 &io_req->req_flags)) {
1379 BNX2FC_IO_DBG(io_req, "Timer context finished processing"
1380 " this io\n");
1381 return;
1382 }
1383
1384 /* Do not issue RRQ as this IO is already cleanedup */
1385 if (test_and_set_bit(BNX2FC_FLAG_IO_CLEANUP,
1386 &io_req->req_flags))
1387 goto io_compl;
1388
1389 /*
1390 * For ABTS issued due to SCSI eh_abort_handler, timeout
1391 * values are maintained by scsi-ml itself. Cancel timeout
1392 * in case ABTS issued as part of task management function
1393 * or due to FW error.
1394 */
1395 if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags))
1396 if (cancel_delayed_work(&io_req->timeout_work))
1397 kref_put(&io_req->refcount,
1398 bnx2fc_cmd_release); /* drop timer hold */
1399
619c5cb6 1400 r_ctl = (u8)task->rxwr_only.union_ctx.comp_info.abts_rsp.r_ctl;
853e2bd2
BG
1401
1402 switch (r_ctl) {
1403 case FC_RCTL_BA_ACC:
1404 /*
1405 * Dont release this cmd yet. It will be relesed
1406 * after we get RRQ response
1407 */
1408 BNX2FC_IO_DBG(io_req, "ABTS response - ACC Send RRQ\n");
1409 issue_rrq = 1;
1410 break;
1411
1412 case FC_RCTL_BA_RJT:
1413 BNX2FC_IO_DBG(io_req, "ABTS response - RJT\n");
1414 break;
1415 default:
1416 printk(KERN_ERR PFX "Unknown ABTS response\n");
1417 break;
1418 }
1419
1420 if (issue_rrq) {
1421 BNX2FC_IO_DBG(io_req, "Issue RRQ after R_A_TOV\n");
1422 set_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags);
1423 }
1424 set_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags);
1425 bnx2fc_cmd_timer_set(io_req, r_a_tov);
1426
1427io_compl:
1428 if (io_req->wait_for_comp) {
1429 if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
1430 &io_req->req_flags))
1431 complete(&io_req->tm_done);
1432 } else {
1433 /*
1434 * We end up here when ABTS is issued as
1435 * in asynchronous context, i.e., as part
1436 * of task management completion, or
1437 * when FW error is received or when the
1438 * ABTS is issued when the IO is timed
1439 * out.
1440 */
1441
1442 if (io_req->on_active_queue) {
1443 list_del_init(&io_req->link);
1444 io_req->on_active_queue = 0;
1445 /* Move IO req to retire queue */
1446 list_add_tail(&io_req->link, &tgt->io_retire_queue);
1447 }
1448 bnx2fc_scsi_done(io_req, DID_ERROR);
1449 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1450 }
1451}
1452
1453static void bnx2fc_lun_reset_cmpl(struct bnx2fc_cmd *io_req)
1454{
1455 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1456 struct bnx2fc_rport *tgt = io_req->tgt;
d71fb3bd 1457 struct bnx2fc_cmd *cmd, *tmp;
9cb78c16
HR
1458 u64 tm_lun = sc_cmd->device->lun;
1459 u64 lun;
853e2bd2 1460 int rc = 0;
853e2bd2
BG
1461
1462 /* called with tgt_lock held */
1463 BNX2FC_IO_DBG(io_req, "Entered bnx2fc_lun_reset_cmpl\n");
1464 /*
1465 * Walk thru the active_ios queue and ABORT the IO
1466 * that matches with the LUN that was reset
1467 */
d71fb3bd 1468 list_for_each_entry_safe(cmd, tmp, &tgt->active_cmd_queue, link) {
853e2bd2 1469 BNX2FC_TGT_DBG(tgt, "LUN RST cmpl: scan for pending IOs\n");
853e2bd2
BG
1470 lun = cmd->sc_cmd->device->lun;
1471 if (lun == tm_lun) {
1472 /* Initiate ABTS on this cmd */
1473 if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
1474 &cmd->req_flags)) {
1475 /* cancel the IO timeout */
1476 if (cancel_delayed_work(&io_req->timeout_work))
1477 kref_put(&io_req->refcount,
1478 bnx2fc_cmd_release);
1479 /* timer hold */
1480 rc = bnx2fc_initiate_abts(cmd);
25985edc 1481 /* abts shouldn't fail in this context */
853e2bd2
BG
1482 WARN_ON(rc != SUCCESS);
1483 } else
1484 printk(KERN_ERR PFX "lun_rst: abts already in"
1485 " progress for this IO 0x%x\n",
1486 cmd->xid);
1487 }
1488 }
1489}
1490
1491static void bnx2fc_tgt_reset_cmpl(struct bnx2fc_cmd *io_req)
1492{
1493 struct bnx2fc_rport *tgt = io_req->tgt;
d71fb3bd 1494 struct bnx2fc_cmd *cmd, *tmp;
853e2bd2
BG
1495 int rc = 0;
1496
1497 /* called with tgt_lock held */
1498 BNX2FC_IO_DBG(io_req, "Entered bnx2fc_tgt_reset_cmpl\n");
1499 /*
1500 * Walk thru the active_ios queue and ABORT the IO
1501 * that matches with the LUN that was reset
1502 */
d71fb3bd 1503 list_for_each_entry_safe(cmd, tmp, &tgt->active_cmd_queue, link) {
853e2bd2 1504 BNX2FC_TGT_DBG(tgt, "TGT RST cmpl: scan for pending IOs\n");
853e2bd2
BG
1505 /* Initiate ABTS */
1506 if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
1507 &cmd->req_flags)) {
1508 /* cancel the IO timeout */
1509 if (cancel_delayed_work(&io_req->timeout_work))
1510 kref_put(&io_req->refcount,
1511 bnx2fc_cmd_release); /* timer hold */
1512 rc = bnx2fc_initiate_abts(cmd);
25985edc 1513 /* abts shouldn't fail in this context */
853e2bd2
BG
1514 WARN_ON(rc != SUCCESS);
1515
1516 } else
1517 printk(KERN_ERR PFX "tgt_rst: abts already in progress"
1518 " for this IO 0x%x\n", cmd->xid);
1519 }
1520}
1521
1522void bnx2fc_process_tm_compl(struct bnx2fc_cmd *io_req,
1523 struct fcoe_task_ctx_entry *task, u8 num_rq)
1524{
1525 struct bnx2fc_mp_req *tm_req;
1526 struct fc_frame_header *fc_hdr;
1527 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1528 u64 *hdr;
1529 u64 *temp_hdr;
1530 void *rsp_buf;
1531
1532 /* Called with tgt_lock held */
1533 BNX2FC_IO_DBG(io_req, "Entered process_tm_compl\n");
1534
1535 if (!(test_bit(BNX2FC_FLAG_TM_TIMEOUT, &io_req->req_flags)))
1536 set_bit(BNX2FC_FLAG_TM_COMPL, &io_req->req_flags);
1537 else {
1538 /* TM has already timed out and we got
1539 * delayed completion. Ignore completion
1540 * processing.
1541 */
1542 return;
1543 }
1544
1545 tm_req = &(io_req->mp_req);
1546 fc_hdr = &(tm_req->resp_fc_hdr);
1547 hdr = (u64 *)fc_hdr;
1548 temp_hdr = (u64 *)
619c5cb6 1549 &task->rxwr_only.union_ctx.comp_info.mp_rsp.fc_hdr;
853e2bd2
BG
1550 hdr[0] = cpu_to_be64(temp_hdr[0]);
1551 hdr[1] = cpu_to_be64(temp_hdr[1]);
1552 hdr[2] = cpu_to_be64(temp_hdr[2]);
1553
619c5cb6
VZ
1554 tm_req->resp_len =
1555 task->rxwr_only.union_ctx.comp_info.mp_rsp.mp_payload_len;
853e2bd2
BG
1556
1557 rsp_buf = tm_req->resp_buf;
1558
1559 if (fc_hdr->fh_r_ctl == FC_RCTL_DD_CMD_STATUS) {
1560 bnx2fc_parse_fcp_rsp(io_req,
1561 (struct fcoe_fcp_rsp_payload *)
1562 rsp_buf, num_rq);
1563 if (io_req->fcp_rsp_code == 0) {
1564 /* TM successful */
1565 if (tm_req->tm_flags & FCP_TMF_LUN_RESET)
1566 bnx2fc_lun_reset_cmpl(io_req);
1567 else if (tm_req->tm_flags & FCP_TMF_TGT_RESET)
1568 bnx2fc_tgt_reset_cmpl(io_req);
1569 }
1570 } else {
1571 printk(KERN_ERR PFX "tmf's fc_hdr r_ctl = 0x%x\n",
1572 fc_hdr->fh_r_ctl);
1573 }
1574 if (!sc_cmd->SCp.ptr) {
b2a554ff 1575 printk(KERN_ERR PFX "tm_compl: SCp.ptr is NULL\n");
853e2bd2
BG
1576 return;
1577 }
1578 switch (io_req->fcp_status) {
1579 case FC_GOOD:
1580 if (io_req->cdb_status == 0) {
1581 /* Good IO completion */
1582 sc_cmd->result = DID_OK << 16;
1583 } else {
1584 /* Transport status is good, SCSI status not good */
1585 sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
1586 }
1587 if (io_req->fcp_resid)
1588 scsi_set_resid(sc_cmd, io_req->fcp_resid);
1589 break;
1590
1591 default:
1592 BNX2FC_IO_DBG(io_req, "process_tm_compl: fcp_status = %d\n",
1593 io_req->fcp_status);
1594 break;
1595 }
1596
1597 sc_cmd = io_req->sc_cmd;
1598 io_req->sc_cmd = NULL;
1599
1600 /* check if the io_req exists in tgt's tmf_q */
1601 if (io_req->on_tmf_queue) {
1602
1603 list_del_init(&io_req->link);
1604 io_req->on_tmf_queue = 0;
1605 } else {
1606
b2a554ff 1607 printk(KERN_ERR PFX "Command not on active_cmd_queue!\n");
853e2bd2
BG
1608 return;
1609 }
1610
1611 sc_cmd->SCp.ptr = NULL;
1612 sc_cmd->scsi_done(sc_cmd);
1613
1614 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1615 if (io_req->wait_for_comp) {
1616 BNX2FC_IO_DBG(io_req, "tm_compl - wake up the waiter\n");
1617 complete(&io_req->tm_done);
1618 }
1619}
1620
1621static int bnx2fc_split_bd(struct bnx2fc_cmd *io_req, u64 addr, int sg_len,
1622 int bd_index)
1623{
1624 struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
1625 int frag_size, sg_frags;
1626
1627 sg_frags = 0;
1628 while (sg_len) {
1629 if (sg_len >= BNX2FC_BD_SPLIT_SZ)
1630 frag_size = BNX2FC_BD_SPLIT_SZ;
1631 else
1632 frag_size = sg_len;
1633 bd[bd_index + sg_frags].buf_addr_lo = addr & 0xffffffff;
1634 bd[bd_index + sg_frags].buf_addr_hi = addr >> 32;
1635 bd[bd_index + sg_frags].buf_len = (u16)frag_size;
1636 bd[bd_index + sg_frags].flags = 0;
1637
1638 addr += (u64) frag_size;
1639 sg_frags++;
1640 sg_len -= frag_size;
1641 }
1642 return sg_frags;
1643
1644}
1645
1646static int bnx2fc_map_sg(struct bnx2fc_cmd *io_req)
1647{
3ce41ea1
BPG
1648 struct bnx2fc_interface *interface = io_req->port->priv;
1649 struct bnx2fc_hba *hba = interface->hba;
853e2bd2
BG
1650 struct scsi_cmnd *sc = io_req->sc_cmd;
1651 struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
1652 struct scatterlist *sg;
1653 int byte_count = 0;
1654 int sg_count = 0;
1655 int bd_count = 0;
1656 int sg_frags;
1657 unsigned int sg_len;
1658 u64 addr;
1659 int i;
1660
5565461e
CD
1661 /*
1662 * Use dma_map_sg directly to ensure we're using the correct
1663 * dev struct off of pcidev.
1664 */
3ce41ea1
BPG
1665 sg_count = dma_map_sg(&hba->pcidev->dev, scsi_sglist(sc),
1666 scsi_sg_count(sc), sc->sc_data_direction);
853e2bd2
BG
1667 scsi_for_each_sg(sc, sg, sg_count, i) {
1668 sg_len = sg_dma_len(sg);
1669 addr = sg_dma_address(sg);
1670 if (sg_len > BNX2FC_MAX_BD_LEN) {
1671 sg_frags = bnx2fc_split_bd(io_req, addr, sg_len,
1672 bd_count);
1673 } else {
1674
1675 sg_frags = 1;
1676 bd[bd_count].buf_addr_lo = addr & 0xffffffff;
1677 bd[bd_count].buf_addr_hi = addr >> 32;
1678 bd[bd_count].buf_len = (u16)sg_len;
1679 bd[bd_count].flags = 0;
1680 }
1681 bd_count += sg_frags;
1682 byte_count += sg_len;
1683 }
1684 if (byte_count != scsi_bufflen(sc))
1685 printk(KERN_ERR PFX "byte_count = %d != scsi_bufflen = %d, "
1686 "task_id = 0x%x\n", byte_count, scsi_bufflen(sc),
1687 io_req->xid);
1688 return bd_count;
1689}
1690
822f2903 1691static int bnx2fc_build_bd_list_from_sg(struct bnx2fc_cmd *io_req)
853e2bd2
BG
1692{
1693 struct scsi_cmnd *sc = io_req->sc_cmd;
1694 struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
1695 int bd_count;
1696
822f2903 1697 if (scsi_sg_count(sc)) {
853e2bd2 1698 bd_count = bnx2fc_map_sg(io_req);
822f2903
BPG
1699 if (bd_count == 0)
1700 return -ENOMEM;
1701 } else {
853e2bd2
BG
1702 bd_count = 0;
1703 bd[0].buf_addr_lo = bd[0].buf_addr_hi = 0;
1704 bd[0].buf_len = bd[0].flags = 0;
1705 }
1706 io_req->bd_tbl->bd_valid = bd_count;
822f2903
BPG
1707
1708 return 0;
853e2bd2
BG
1709}
1710
1711static void bnx2fc_unmap_sg_list(struct bnx2fc_cmd *io_req)
1712{
1713 struct scsi_cmnd *sc = io_req->sc_cmd;
5565461e
CD
1714 struct bnx2fc_interface *interface = io_req->port->priv;
1715 struct bnx2fc_hba *hba = interface->hba;
853e2bd2 1716
5565461e
CD
1717 /*
1718 * Use dma_unmap_sg directly to ensure we're using the correct
1719 * dev struct off of pcidev.
1720 */
1721 if (io_req->bd_tbl->bd_valid && sc && scsi_sg_count(sc)) {
1722 dma_unmap_sg(&hba->pcidev->dev, scsi_sglist(sc),
1723 scsi_sg_count(sc), sc->sc_data_direction);
853e2bd2
BG
1724 io_req->bd_tbl->bd_valid = 0;
1725 }
1726}
1727
1728void bnx2fc_build_fcp_cmnd(struct bnx2fc_cmd *io_req,
1729 struct fcp_cmnd *fcp_cmnd)
1730{
1731 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
853e2bd2
BG
1732
1733 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
1734
e35fa8c2 1735 int_to_scsilun(sc_cmd->device->lun, &fcp_cmnd->fc_lun);
853e2bd2
BG
1736
1737 fcp_cmnd->fc_dl = htonl(io_req->data_xfer_len);
1738 memcpy(fcp_cmnd->fc_cdb, sc_cmd->cmnd, sc_cmd->cmd_len);
1739
1740 fcp_cmnd->fc_cmdref = 0;
1741 fcp_cmnd->fc_pri_ta = 0;
1742 fcp_cmnd->fc_tm_flags = io_req->mp_req.tm_flags;
1743 fcp_cmnd->fc_flags = io_req->io_req_flags;
ea6bb33e 1744 fcp_cmnd->fc_pri_ta = FCP_PTA_SIMPLE;
853e2bd2
BG
1745}
1746
1747static void bnx2fc_parse_fcp_rsp(struct bnx2fc_cmd *io_req,
1748 struct fcoe_fcp_rsp_payload *fcp_rsp,
1749 u8 num_rq)
1750{
1751 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1752 struct bnx2fc_rport *tgt = io_req->tgt;
1753 u8 rsp_flags = fcp_rsp->fcp_flags.flags;
1754 u32 rq_buff_len = 0;
1755 int i;
1756 unsigned char *rq_data;
1757 unsigned char *dummy;
1758 int fcp_sns_len = 0;
1759 int fcp_rsp_len = 0;
1760
1761 io_req->fcp_status = FC_GOOD;
1762 io_req->fcp_resid = fcp_rsp->fcp_resid;
1763
1764 io_req->scsi_comp_flags = rsp_flags;
1765 CMD_SCSI_STATUS(sc_cmd) = io_req->cdb_status =
1766 fcp_rsp->scsi_status_code;
1767
1768 /* Fetch fcp_rsp_info and fcp_sns_info if available */
1769 if (num_rq) {
1770
1771 /*
1772 * We do not anticipate num_rq >1, as the linux defined
1773 * SCSI_SENSE_BUFFERSIZE is 96 bytes + 8 bytes of FCP_RSP_INFO
1774 * 256 bytes of single rq buffer is good enough to hold this.
1775 */
1776
1777 if (rsp_flags &
1778 FCOE_FCP_RSP_FLAGS_FCP_RSP_LEN_VALID) {
1779 fcp_rsp_len = rq_buff_len
1780 = fcp_rsp->fcp_rsp_len;
1781 }
1782
1783 if (rsp_flags &
1784 FCOE_FCP_RSP_FLAGS_FCP_SNS_LEN_VALID) {
1785 fcp_sns_len = fcp_rsp->fcp_sns_len;
1786 rq_buff_len += fcp_rsp->fcp_sns_len;
1787 }
1788
1789 io_req->fcp_rsp_len = fcp_rsp_len;
1790 io_req->fcp_sns_len = fcp_sns_len;
1791
1792 if (rq_buff_len > num_rq * BNX2FC_RQ_BUF_SZ) {
1793 /* Invalid sense sense length. */
b2a554ff 1794 printk(KERN_ERR PFX "invalid sns length %d\n",
853e2bd2
BG
1795 rq_buff_len);
1796 /* reset rq_buff_len */
1797 rq_buff_len = num_rq * BNX2FC_RQ_BUF_SZ;
1798 }
1799
1800 rq_data = bnx2fc_get_next_rqe(tgt, 1);
1801
1802 if (num_rq > 1) {
1803 /* We do not need extra sense data */
1804 for (i = 1; i < num_rq; i++)
1805 dummy = bnx2fc_get_next_rqe(tgt, 1);
1806 }
1807
1808 /* fetch fcp_rsp_code */
1809 if ((fcp_rsp_len == 4) || (fcp_rsp_len == 8)) {
1810 /* Only for task management function */
1811 io_req->fcp_rsp_code = rq_data[3];
1812 printk(KERN_ERR PFX "fcp_rsp_code = %d\n",
1813 io_req->fcp_rsp_code);
1814 }
1815
1816 /* fetch sense data */
1817 rq_data += fcp_rsp_len;
1818
1819 if (fcp_sns_len > SCSI_SENSE_BUFFERSIZE) {
1820 printk(KERN_ERR PFX "Truncating sense buffer\n");
1821 fcp_sns_len = SCSI_SENSE_BUFFERSIZE;
1822 }
1823
16da05b1 1824 memset(sc_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
853e2bd2
BG
1825 if (fcp_sns_len)
1826 memcpy(sc_cmd->sense_buffer, rq_data, fcp_sns_len);
1827
1828 /* return RQ entries */
1829 for (i = 0; i < num_rq; i++)
1830 bnx2fc_return_rqe(tgt, 1);
1831 }
1832}
1833
1834/**
1835 * bnx2fc_queuecommand - Queuecommand function of the scsi template
1836 *
1837 * @host: The Scsi_Host the command was issued to
1838 * @sc_cmd: struct scsi_cmnd to be executed
1839 *
1840 * This is the IO strategy routine, called by SCSI-ML
1841 **/
1842int bnx2fc_queuecommand(struct Scsi_Host *host,
1843 struct scsi_cmnd *sc_cmd)
1844{
1845 struct fc_lport *lport = shost_priv(host);
1846 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
1847 struct fc_rport_libfc_priv *rp = rport->dd_data;
1848 struct bnx2fc_rport *tgt;
1849 struct bnx2fc_cmd *io_req;
1850 int rc = 0;
1851 int rval;
1852
1853 rval = fc_remote_port_chkready(rport);
1854 if (rval) {
1855 sc_cmd->result = rval;
1856 sc_cmd->scsi_done(sc_cmd);
1857 return 0;
1858 }
1859
1860 if ((lport->state != LPORT_ST_READY) || !(lport->link_up)) {
1861 rc = SCSI_MLQUEUE_HOST_BUSY;
1862 goto exit_qcmd;
1863 }
1864
1865 /* rport and tgt are allocated together, so tgt should be non-NULL */
1866 tgt = (struct bnx2fc_rport *)&rp[1];
1867
1868 if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
1869 /*
1870 * Session is not offloaded yet. Let SCSI-ml retry
1871 * the command.
1872 */
1873 rc = SCSI_MLQUEUE_TARGET_BUSY;
1874 goto exit_qcmd;
1875 }
245a5754
EW
1876 if (tgt->retry_delay_timestamp) {
1877 if (time_after(jiffies, tgt->retry_delay_timestamp)) {
1878 tgt->retry_delay_timestamp = 0;
1879 } else {
1880 /* If retry_delay timer is active, flow off the ML */
1881 rc = SCSI_MLQUEUE_TARGET_BUSY;
1882 goto exit_qcmd;
1883 }
1884 }
dc6311dd
ML
1885
1886 spin_lock_bh(&tgt->tgt_lock);
1887
853e2bd2
BG
1888 io_req = bnx2fc_cmd_alloc(tgt);
1889 if (!io_req) {
1890 rc = SCSI_MLQUEUE_HOST_BUSY;
dc6311dd 1891 goto exit_qcmd_tgtlock;
853e2bd2
BG
1892 }
1893 io_req->sc_cmd = sc_cmd;
1894
1895 if (bnx2fc_post_io_req(tgt, io_req)) {
1896 printk(KERN_ERR PFX "Unable to post io_req\n");
1897 rc = SCSI_MLQUEUE_HOST_BUSY;
dc6311dd 1898 goto exit_qcmd_tgtlock;
853e2bd2 1899 }
dc6311dd
ML
1900
1901exit_qcmd_tgtlock:
1902 spin_unlock_bh(&tgt->tgt_lock);
853e2bd2
BG
1903exit_qcmd:
1904 return rc;
1905}
1906
1907void bnx2fc_process_scsi_cmd_compl(struct bnx2fc_cmd *io_req,
1908 struct fcoe_task_ctx_entry *task,
1909 u8 num_rq)
1910{
1911 struct fcoe_fcp_rsp_payload *fcp_rsp;
1912 struct bnx2fc_rport *tgt = io_req->tgt;
1913 struct scsi_cmnd *sc_cmd;
1914 struct Scsi_Host *host;
1915
1916
1917 /* scsi_cmd_cmpl is called with tgt lock held */
1918
1919 if (test_and_set_bit(BNX2FC_FLAG_IO_COMPL, &io_req->req_flags)) {
1920 /* we will not receive ABTS response for this IO */
1921 BNX2FC_IO_DBG(io_req, "Timer context finished processing "
1922 "this scsi cmd\n");
1923 }
1924
1925 /* Cancel the timeout_work, as we received IO completion */
1926 if (cancel_delayed_work(&io_req->timeout_work))
1927 kref_put(&io_req->refcount,
1928 bnx2fc_cmd_release); /* drop timer hold */
1929
1930 sc_cmd = io_req->sc_cmd;
1931 if (sc_cmd == NULL) {
1932 printk(KERN_ERR PFX "scsi_cmd_compl - sc_cmd is NULL\n");
1933 return;
1934 }
1935
1936 /* Fetch fcp_rsp from task context and perform cmd completion */
1937 fcp_rsp = (struct fcoe_fcp_rsp_payload *)
619c5cb6 1938 &(task->rxwr_only.union_ctx.comp_info.fcp_rsp.payload);
853e2bd2
BG
1939
1940 /* parse fcp_rsp and obtain sense data from RQ if available */
1941 bnx2fc_parse_fcp_rsp(io_req, fcp_rsp, num_rq);
1942
1943 host = sc_cmd->device->host;
1944 if (!sc_cmd->SCp.ptr) {
1945 printk(KERN_ERR PFX "SCp.ptr is NULL\n");
1946 return;
1947 }
853e2bd2
BG
1948
1949 if (io_req->on_active_queue) {
1950 list_del_init(&io_req->link);
1951 io_req->on_active_queue = 0;
1952 /* Move IO req to retire queue */
1953 list_add_tail(&io_req->link, &tgt->io_retire_queue);
1954 } else {
1955 /* This should not happen, but could have been pulled
1956 * by bnx2fc_flush_active_ios(), or during a race
1957 * between command abort and (late) completion.
1958 */
1959 BNX2FC_IO_DBG(io_req, "xid not on active_cmd_queue\n");
1960 if (io_req->wait_for_comp)
1961 if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
1962 &io_req->req_flags))
1963 complete(&io_req->tm_done);
1964 }
1965
1966 bnx2fc_unmap_sg_list(io_req);
b5a95fe7 1967 io_req->sc_cmd = NULL;
853e2bd2
BG
1968
1969 switch (io_req->fcp_status) {
1970 case FC_GOOD:
1971 if (io_req->cdb_status == 0) {
1972 /* Good IO completion */
1973 sc_cmd->result = DID_OK << 16;
1974 } else {
1975 /* Transport status is good, SCSI status not good */
1976 BNX2FC_IO_DBG(io_req, "scsi_cmpl: cdb_status = %d"
1977 " fcp_resid = 0x%x\n",
1978 io_req->cdb_status, io_req->fcp_resid);
1979 sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
245a5754
EW
1980
1981 if (io_req->cdb_status == SAM_STAT_TASK_SET_FULL ||
1982 io_req->cdb_status == SAM_STAT_BUSY) {
1983 /* Set the jiffies + retry_delay_timer * 100ms
1984 for the rport/tgt */
1985 tgt->retry_delay_timestamp = jiffies +
1986 fcp_rsp->retry_delay_timer * HZ / 10;
1987 }
1988
853e2bd2
BG
1989 }
1990 if (io_req->fcp_resid)
1991 scsi_set_resid(sc_cmd, io_req->fcp_resid);
1992 break;
1993 default:
b2a554ff 1994 printk(KERN_ERR PFX "scsi_cmd_compl: fcp_status = %d\n",
853e2bd2
BG
1995 io_req->fcp_status);
1996 break;
1997 }
1998 sc_cmd->SCp.ptr = NULL;
1999 sc_cmd->scsi_done(sc_cmd);
2000 kref_put(&io_req->refcount, bnx2fc_cmd_release);
2001}
2002
74446954 2003int bnx2fc_post_io_req(struct bnx2fc_rport *tgt,
853e2bd2
BG
2004 struct bnx2fc_cmd *io_req)
2005{
2006 struct fcoe_task_ctx_entry *task;
2007 struct fcoe_task_ctx_entry *task_page;
2008 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
2009 struct fcoe_port *port = tgt->port;
aea71a02
BPG
2010 struct bnx2fc_interface *interface = port->priv;
2011 struct bnx2fc_hba *hba = interface->hba;
853e2bd2 2012 struct fc_lport *lport = port->lport;
1bd49b48 2013 struct fc_stats *stats;
853e2bd2
BG
2014 int task_idx, index;
2015 u16 xid;
2016
dc6311dd
ML
2017 /* bnx2fc_post_io_req() is called with the tgt_lock held */
2018
853e2bd2
BG
2019 /* Initialize rest of io_req fields */
2020 io_req->cmd_type = BNX2FC_SCSI_CMD;
2021 io_req->port = port;
2022 io_req->tgt = tgt;
2023 io_req->data_xfer_len = scsi_bufflen(sc_cmd);
2024 sc_cmd->SCp.ptr = (char *)io_req;
2025
1bd49b48 2026 stats = per_cpu_ptr(lport->stats, get_cpu());
853e2bd2
BG
2027 if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
2028 io_req->io_req_flags = BNX2FC_READ;
2029 stats->InputRequests++;
2030 stats->InputBytes += io_req->data_xfer_len;
2031 } else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
2032 io_req->io_req_flags = BNX2FC_WRITE;
2033 stats->OutputRequests++;
2034 stats->OutputBytes += io_req->data_xfer_len;
2035 } else {
2036 io_req->io_req_flags = 0;
2037 stats->ControlRequests++;
2038 }
2039 put_cpu();
2040
2041 xid = io_req->xid;
2042
2043 /* Build buffer descriptor list for firmware from sg list */
822f2903
BPG
2044 if (bnx2fc_build_bd_list_from_sg(io_req)) {
2045 printk(KERN_ERR PFX "BD list creation failed\n");
822f2903 2046 kref_put(&io_req->refcount, bnx2fc_cmd_release);
822f2903
BPG
2047 return -EAGAIN;
2048 }
853e2bd2
BG
2049
2050 task_idx = xid / BNX2FC_TASKS_PER_PAGE;
2051 index = xid % BNX2FC_TASKS_PER_PAGE;
2052
2053 /* Initialize task context for this IO request */
2054 task_page = (struct fcoe_task_ctx_entry *) hba->task_ctx[task_idx];
2055 task = &(task_page[index]);
2056 bnx2fc_init_task(io_req, task);
2057
853e2bd2
BG
2058 if (tgt->flush_in_prog) {
2059 printk(KERN_ERR PFX "Flush in progress..Host Busy\n");
2060 kref_put(&io_req->refcount, bnx2fc_cmd_release);
853e2bd2
BG
2061 return -EAGAIN;
2062 }
2063
2064 if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
2065 printk(KERN_ERR PFX "Session not ready...post_io\n");
2066 kref_put(&io_req->refcount, bnx2fc_cmd_release);
853e2bd2
BG
2067 return -EAGAIN;
2068 }
2069
2070 /* Time IO req */
b252f4c7
BPG
2071 if (tgt->io_timeout)
2072 bnx2fc_cmd_timer_set(io_req, BNX2FC_IO_TIMEOUT);
853e2bd2
BG
2073 /* Obtain free SQ entry */
2074 bnx2fc_add_2_sq(tgt, xid);
2075
2076 /* Enqueue the io_req to active_cmd_queue */
2077
2078 io_req->on_active_queue = 1;
2079 /* move io_req from pending_queue to active_queue */
2080 list_add_tail(&io_req->link, &tgt->active_cmd_queue);
2081
2082 /* Ring doorbell */
2083 bnx2fc_ring_doorbell(tgt);
853e2bd2
BG
2084 return 0;
2085}