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