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