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