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