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