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