xref: /openbmc/linux/drivers/scsi/bnx2i/bnx2i_hwi.c (revision f35cb48e)
1 /* bnx2i_hwi.c: QLogic NetXtreme II iSCSI driver.
2  *
3  * Copyright (c) 2006 - 2013 Broadcom Corporation
4  * Copyright (c) 2007, 2008 Red Hat, Inc.  All rights reserved.
5  * Copyright (c) 2007, 2008 Mike Christie
6  * Copyright (c) 2014, QLogic Corporation
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: Anil Veerabhadrappa (anilgv@broadcom.com)
13  * Previously Maintained by: Eddie Wai (eddie.wai@broadcom.com)
14  * Maintained by: QLogic-Storage-Upstream@qlogic.com
15  */
16 
17 #include <linux/gfp.h>
18 #include <scsi/scsi_tcq.h>
19 #include <scsi/libiscsi.h>
20 #include "bnx2i.h"
21 
22 DECLARE_PER_CPU(struct bnx2i_percpu_s, bnx2i_percpu);
23 
24 /**
25  * bnx2i_get_cid_num - get cid from ep
26  * @ep: 	endpoint pointer
27  *
28  * Only applicable to 57710 family of devices
29  */
30 static u32 bnx2i_get_cid_num(struct bnx2i_endpoint *ep)
31 {
32 	u32 cid;
33 
34 	if (test_bit(BNX2I_NX2_DEV_57710, &ep->hba->cnic_dev_type))
35 		cid = ep->ep_cid;
36 	else
37 		cid = GET_CID_NUM(ep->ep_cid);
38 	return cid;
39 }
40 
41 
42 /**
43  * bnx2i_adjust_qp_size - Adjust SQ/RQ/CQ size for 57710 device type
44  * @hba: 		Adapter for which adjustments is to be made
45  *
46  * Only applicable to 57710 family of devices
47  */
48 static void bnx2i_adjust_qp_size(struct bnx2i_hba *hba)
49 {
50 	u32 num_elements_per_pg;
51 
52 	if (test_bit(BNX2I_NX2_DEV_5706, &hba->cnic_dev_type) ||
53 	    test_bit(BNX2I_NX2_DEV_5708, &hba->cnic_dev_type) ||
54 	    test_bit(BNX2I_NX2_DEV_5709, &hba->cnic_dev_type)) {
55 		if (!is_power_of_2(hba->max_sqes))
56 			hba->max_sqes = rounddown_pow_of_two(hba->max_sqes);
57 
58 		if (!is_power_of_2(hba->max_rqes))
59 			hba->max_rqes = rounddown_pow_of_two(hba->max_rqes);
60 	}
61 
62 	/* Adjust each queue size if the user selection does not
63 	 * yield integral num of page buffers
64 	 */
65 	/* adjust SQ */
66 	num_elements_per_pg = CNIC_PAGE_SIZE / BNX2I_SQ_WQE_SIZE;
67 	if (hba->max_sqes < num_elements_per_pg)
68 		hba->max_sqes = num_elements_per_pg;
69 	else if (hba->max_sqes % num_elements_per_pg)
70 		hba->max_sqes = (hba->max_sqes + num_elements_per_pg - 1) &
71 				 ~(num_elements_per_pg - 1);
72 
73 	/* adjust CQ */
74 	num_elements_per_pg = CNIC_PAGE_SIZE / BNX2I_CQE_SIZE;
75 	if (hba->max_cqes < num_elements_per_pg)
76 		hba->max_cqes = num_elements_per_pg;
77 	else if (hba->max_cqes % num_elements_per_pg)
78 		hba->max_cqes = (hba->max_cqes + num_elements_per_pg - 1) &
79 				 ~(num_elements_per_pg - 1);
80 
81 	/* adjust RQ */
82 	num_elements_per_pg = CNIC_PAGE_SIZE / BNX2I_RQ_WQE_SIZE;
83 	if (hba->max_rqes < num_elements_per_pg)
84 		hba->max_rqes = num_elements_per_pg;
85 	else if (hba->max_rqes % num_elements_per_pg)
86 		hba->max_rqes = (hba->max_rqes + num_elements_per_pg - 1) &
87 				 ~(num_elements_per_pg - 1);
88 }
89 
90 
91 /**
92  * bnx2i_get_link_state - get network interface link state
93  * @hba:	adapter instance pointer
94  *
95  * updates adapter structure flag based on netdev state
96  */
97 static void bnx2i_get_link_state(struct bnx2i_hba *hba)
98 {
99 	if (test_bit(__LINK_STATE_NOCARRIER, &hba->netdev->state))
100 		set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
101 	else
102 		clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
103 }
104 
105 
106 /**
107  * bnx2i_iscsi_license_error - displays iscsi license related error message
108  * @hba:		adapter instance pointer
109  * @error_code:		error classification
110  *
111  * Puts out an error log when driver is unable to offload iscsi connection
112  *	due to license restrictions
113  */
114 static void bnx2i_iscsi_license_error(struct bnx2i_hba *hba, u32 error_code)
115 {
116 	if (error_code == ISCSI_KCQE_COMPLETION_STATUS_ISCSI_NOT_SUPPORTED)
117 		/* iSCSI offload not supported on this device */
118 		printk(KERN_ERR "bnx2i: iSCSI not supported, dev=%s\n",
119 				hba->netdev->name);
120 	if (error_code == ISCSI_KCQE_COMPLETION_STATUS_LOM_ISCSI_NOT_ENABLED)
121 		/* iSCSI offload not supported on this LOM device */
122 		printk(KERN_ERR "bnx2i: LOM is not enable to "
123 				"offload iSCSI connections, dev=%s\n",
124 				hba->netdev->name);
125 	set_bit(ADAPTER_STATE_INIT_FAILED, &hba->adapter_state);
126 }
127 
128 
129 /**
130  * bnx2i_arm_cq_event_coalescing - arms CQ to enable EQ notification
131  * @ep:		endpoint (transport identifier) structure
132  * @action:	action, ARM or DISARM. For now only ARM_CQE is used
133  *
134  * Arm'ing CQ will enable chip to generate global EQ events inorder to interrupt
135  *	the driver. EQ event is generated CQ index is hit or at least 1 CQ is
136  *	outstanding and on chip timer expires
137  */
138 int bnx2i_arm_cq_event_coalescing(struct bnx2i_endpoint *ep, u8 action)
139 {
140 	struct bnx2i_5771x_cq_db *cq_db;
141 	u16 cq_index;
142 	u16 next_index = 0;
143 	u32 num_active_cmds;
144 
145 	/* Coalesce CQ entries only on 10G devices */
146 	if (!test_bit(BNX2I_NX2_DEV_57710, &ep->hba->cnic_dev_type))
147 		return 0;
148 
149 	/* Do not update CQ DB multiple times before firmware writes
150 	 * '0xFFFF' to CQDB->SQN field. Deviation may cause spurious
151 	 * interrupts and other unwanted results
152 	 */
153 	cq_db = (struct bnx2i_5771x_cq_db *) ep->qp.cq_pgtbl_virt;
154 
155 	if (action != CNIC_ARM_CQE_FP)
156 		if (cq_db->sqn[0] && cq_db->sqn[0] != 0xFFFF)
157 			return 0;
158 
159 	if (action == CNIC_ARM_CQE || action == CNIC_ARM_CQE_FP) {
160 		num_active_cmds = atomic_read(&ep->num_active_cmds);
161 		if (num_active_cmds <= event_coal_min)
162 			next_index = 1;
163 		else {
164 			next_index = num_active_cmds >> ep->ec_shift;
165 			if (next_index > num_active_cmds - event_coal_min)
166 				next_index = num_active_cmds - event_coal_min;
167 		}
168 		if (!next_index)
169 			next_index = 1;
170 		cq_index = ep->qp.cqe_exp_seq_sn + next_index - 1;
171 		if (cq_index > ep->qp.cqe_size * 2)
172 			cq_index -= ep->qp.cqe_size * 2;
173 		if (!cq_index)
174 			cq_index = 1;
175 
176 		cq_db->sqn[0] = cq_index;
177 	}
178 	return next_index;
179 }
180 
181 
182 /**
183  * bnx2i_get_rq_buf - copy RQ buffer contents to driver buffer
184  * @conn:		iscsi connection on which RQ event occurred
185  * @ptr:		driver buffer to which RQ buffer contents is to
186  *			be copied
187  * @len:		length of valid data inside RQ buf
188  *
189  * Copies RQ buffer contents from shared (DMA'able) memory region to
190  *	driver buffer. RQ is used to DMA unsolicitated iscsi pdu's and
191  *	scsi sense info
192  */
193 void bnx2i_get_rq_buf(struct bnx2i_conn *bnx2i_conn, char *ptr, int len)
194 {
195 	if (!bnx2i_conn->ep->qp.rqe_left)
196 		return;
197 
198 	bnx2i_conn->ep->qp.rqe_left--;
199 	memcpy(ptr, (u8 *) bnx2i_conn->ep->qp.rq_cons_qe, len);
200 	if (bnx2i_conn->ep->qp.rq_cons_qe == bnx2i_conn->ep->qp.rq_last_qe) {
201 		bnx2i_conn->ep->qp.rq_cons_qe = bnx2i_conn->ep->qp.rq_first_qe;
202 		bnx2i_conn->ep->qp.rq_cons_idx = 0;
203 	} else {
204 		bnx2i_conn->ep->qp.rq_cons_qe++;
205 		bnx2i_conn->ep->qp.rq_cons_idx++;
206 	}
207 }
208 
209 
210 static void bnx2i_ring_577xx_doorbell(struct bnx2i_conn *conn)
211 {
212 	struct bnx2i_5771x_dbell dbell;
213 	u32 msg;
214 
215 	memset(&dbell, 0, sizeof(dbell));
216 	dbell.dbell.header = (B577XX_ISCSI_CONNECTION_TYPE <<
217 			      B577XX_DOORBELL_HDR_CONN_TYPE_SHIFT);
218 	msg = *((u32 *)&dbell);
219 	/* TODO : get doorbell register mapping */
220 	writel(cpu_to_le32(msg), conn->ep->qp.ctx_base);
221 }
222 
223 
224 /**
225  * bnx2i_put_rq_buf - Replenish RQ buffer, if required ring on chip doorbell
226  * @conn:	iscsi connection on which event to post
227  * @count:	number of RQ buffer being posted to chip
228  *
229  * No need to ring hardware doorbell for 57710 family of devices
230  */
231 void bnx2i_put_rq_buf(struct bnx2i_conn *bnx2i_conn, int count)
232 {
233 	struct bnx2i_5771x_sq_rq_db *rq_db;
234 	u16 hi_bit = (bnx2i_conn->ep->qp.rq_prod_idx & 0x8000);
235 	struct bnx2i_endpoint *ep = bnx2i_conn->ep;
236 
237 	ep->qp.rqe_left += count;
238 	ep->qp.rq_prod_idx &= 0x7FFF;
239 	ep->qp.rq_prod_idx += count;
240 
241 	if (ep->qp.rq_prod_idx > bnx2i_conn->hba->max_rqes) {
242 		ep->qp.rq_prod_idx %= bnx2i_conn->hba->max_rqes;
243 		if (!hi_bit)
244 			ep->qp.rq_prod_idx |= 0x8000;
245 	} else
246 		ep->qp.rq_prod_idx |= hi_bit;
247 
248 	if (test_bit(BNX2I_NX2_DEV_57710, &ep->hba->cnic_dev_type)) {
249 		rq_db = (struct bnx2i_5771x_sq_rq_db *) ep->qp.rq_pgtbl_virt;
250 		rq_db->prod_idx = ep->qp.rq_prod_idx;
251 		/* no need to ring hardware doorbell for 57710 */
252 	} else {
253 		writew(ep->qp.rq_prod_idx,
254 		       ep->qp.ctx_base + CNIC_RECV_DOORBELL);
255 	}
256 	mmiowb();
257 }
258 
259 
260 /**
261  * bnx2i_ring_sq_dbell - Ring SQ doorbell to wake-up the processing engine
262  * @conn: 		iscsi connection to which new SQ entries belong
263  * @count: 		number of SQ WQEs to post
264  *
265  * SQ DB is updated in host memory and TX Doorbell is rung for 57710 family
266  *	of devices. For 5706/5708/5709 new SQ WQE count is written into the
267  *	doorbell register
268  */
269 static void bnx2i_ring_sq_dbell(struct bnx2i_conn *bnx2i_conn, int count)
270 {
271 	struct bnx2i_5771x_sq_rq_db *sq_db;
272 	struct bnx2i_endpoint *ep = bnx2i_conn->ep;
273 
274 	atomic_inc(&ep->num_active_cmds);
275 	wmb();	/* flush SQ WQE memory before the doorbell is rung */
276 	if (test_bit(BNX2I_NX2_DEV_57710, &ep->hba->cnic_dev_type)) {
277 		sq_db = (struct bnx2i_5771x_sq_rq_db *) ep->qp.sq_pgtbl_virt;
278 		sq_db->prod_idx = ep->qp.sq_prod_idx;
279 		bnx2i_ring_577xx_doorbell(bnx2i_conn);
280 	} else
281 		writew(count, ep->qp.ctx_base + CNIC_SEND_DOORBELL);
282 
283 	mmiowb(); /* flush posted PCI writes */
284 }
285 
286 
287 /**
288  * bnx2i_ring_dbell_update_sq_params - update SQ driver parameters
289  * @conn:	iscsi connection to which new SQ entries belong
290  * @count:	number of SQ WQEs to post
291  *
292  * this routine will update SQ driver parameters and ring the doorbell
293  */
294 static void bnx2i_ring_dbell_update_sq_params(struct bnx2i_conn *bnx2i_conn,
295 					      int count)
296 {
297 	int tmp_cnt;
298 
299 	if (count == 1) {
300 		if (bnx2i_conn->ep->qp.sq_prod_qe ==
301 		    bnx2i_conn->ep->qp.sq_last_qe)
302 			bnx2i_conn->ep->qp.sq_prod_qe =
303 						bnx2i_conn->ep->qp.sq_first_qe;
304 		else
305 			bnx2i_conn->ep->qp.sq_prod_qe++;
306 	} else {
307 		if ((bnx2i_conn->ep->qp.sq_prod_qe + count) <=
308 		    bnx2i_conn->ep->qp.sq_last_qe)
309 			bnx2i_conn->ep->qp.sq_prod_qe += count;
310 		else {
311 			tmp_cnt = bnx2i_conn->ep->qp.sq_last_qe -
312 				bnx2i_conn->ep->qp.sq_prod_qe;
313 			bnx2i_conn->ep->qp.sq_prod_qe =
314 				&bnx2i_conn->ep->qp.sq_first_qe[count -
315 								(tmp_cnt + 1)];
316 		}
317 	}
318 	bnx2i_conn->ep->qp.sq_prod_idx += count;
319 	/* Ring the doorbell */
320 	bnx2i_ring_sq_dbell(bnx2i_conn, bnx2i_conn->ep->qp.sq_prod_idx);
321 }
322 
323 
324 /**
325  * bnx2i_send_iscsi_login - post iSCSI login request MP WQE to hardware
326  * @conn:	iscsi connection
327  * @cmd:	driver command structure which is requesting
328  *		a WQE to sent to chip for further processing
329  *
330  * prepare and post an iSCSI Login request WQE to CNIC firmware
331  */
332 int bnx2i_send_iscsi_login(struct bnx2i_conn *bnx2i_conn,
333 			   struct iscsi_task *task)
334 {
335 	struct bnx2i_login_request *login_wqe;
336 	struct iscsi_login_req *login_hdr;
337 	u32 dword;
338 
339 	login_hdr = (struct iscsi_login_req *)task->hdr;
340 	login_wqe = (struct bnx2i_login_request *)
341 						bnx2i_conn->ep->qp.sq_prod_qe;
342 
343 	login_wqe->op_code = login_hdr->opcode;
344 	login_wqe->op_attr = login_hdr->flags;
345 	login_wqe->version_max = login_hdr->max_version;
346 	login_wqe->version_min = login_hdr->min_version;
347 	login_wqe->data_length = ntoh24(login_hdr->dlength);
348 	login_wqe->isid_lo = *((u32 *) login_hdr->isid);
349 	login_wqe->isid_hi = *((u16 *) login_hdr->isid + 2);
350 	login_wqe->tsih = login_hdr->tsih;
351 	login_wqe->itt = task->itt |
352 		(ISCSI_TASK_TYPE_MPATH << ISCSI_LOGIN_REQUEST_TYPE_SHIFT);
353 	login_wqe->cid = login_hdr->cid;
354 
355 	login_wqe->cmd_sn = be32_to_cpu(login_hdr->cmdsn);
356 	login_wqe->exp_stat_sn = be32_to_cpu(login_hdr->exp_statsn);
357 	login_wqe->flags = ISCSI_LOGIN_REQUEST_UPDATE_EXP_STAT_SN;
358 
359 	login_wqe->resp_bd_list_addr_lo = (u32) bnx2i_conn->gen_pdu.resp_bd_dma;
360 	login_wqe->resp_bd_list_addr_hi =
361 		(u32) ((u64) bnx2i_conn->gen_pdu.resp_bd_dma >> 32);
362 
363 	dword = ((1 << ISCSI_LOGIN_REQUEST_NUM_RESP_BDS_SHIFT) |
364 		 (bnx2i_conn->gen_pdu.resp_buf_size <<
365 		  ISCSI_LOGIN_REQUEST_RESP_BUFFER_LENGTH_SHIFT));
366 	login_wqe->resp_buffer = dword;
367 	login_wqe->bd_list_addr_lo = (u32) bnx2i_conn->gen_pdu.req_bd_dma;
368 	login_wqe->bd_list_addr_hi =
369 		(u32) ((u64) bnx2i_conn->gen_pdu.req_bd_dma >> 32);
370 	login_wqe->num_bds = 1;
371 	login_wqe->cq_index = 0; /* CQ# used for completion, 5771x only */
372 
373 	bnx2i_ring_dbell_update_sq_params(bnx2i_conn, 1);
374 	return 0;
375 }
376 
377 /**
378  * bnx2i_send_iscsi_tmf - post iSCSI task management request MP WQE to hardware
379  * @conn:	iscsi connection
380  * @mtask:	driver command structure which is requesting
381  *		a WQE to sent to chip for further processing
382  *
383  * prepare and post an iSCSI Login request WQE to CNIC firmware
384  */
385 int bnx2i_send_iscsi_tmf(struct bnx2i_conn *bnx2i_conn,
386 			 struct iscsi_task *mtask)
387 {
388 	struct iscsi_conn *conn = bnx2i_conn->cls_conn->dd_data;
389 	struct iscsi_tm *tmfabort_hdr;
390 	struct scsi_cmnd *ref_sc;
391 	struct iscsi_task *ctask;
392 	struct bnx2i_tmf_request *tmfabort_wqe;
393 	u32 dword;
394 	u32 scsi_lun[2];
395 
396 	tmfabort_hdr = (struct iscsi_tm *)mtask->hdr;
397 	tmfabort_wqe = (struct bnx2i_tmf_request *)
398 						bnx2i_conn->ep->qp.sq_prod_qe;
399 
400 	tmfabort_wqe->op_code = tmfabort_hdr->opcode;
401 	tmfabort_wqe->op_attr = tmfabort_hdr->flags;
402 
403 	tmfabort_wqe->itt = (mtask->itt | (ISCSI_TASK_TYPE_MPATH << 14));
404 	tmfabort_wqe->reserved2 = 0;
405 	tmfabort_wqe->cmd_sn = be32_to_cpu(tmfabort_hdr->cmdsn);
406 
407 	switch (tmfabort_hdr->flags & ISCSI_FLAG_TM_FUNC_MASK) {
408 	case ISCSI_TM_FUNC_ABORT_TASK:
409 	case ISCSI_TM_FUNC_TASK_REASSIGN:
410 		ctask = iscsi_itt_to_task(conn, tmfabort_hdr->rtt);
411 		if (!ctask || !ctask->sc)
412 			/*
413 			 * the iscsi layer must have completed the cmd while
414 			 * was starting up.
415 			 *
416 			 * Note: In the case of a SCSI cmd timeout, the task's
417 			 *       sc is still active; hence ctask->sc != 0
418 			 *       In this case, the task must be aborted
419 			 */
420 			return 0;
421 
422 		ref_sc = ctask->sc;
423 		if (ref_sc->sc_data_direction == DMA_TO_DEVICE)
424 			dword = (ISCSI_TASK_TYPE_WRITE <<
425 				 ISCSI_CMD_REQUEST_TYPE_SHIFT);
426 		else
427 			dword = (ISCSI_TASK_TYPE_READ <<
428 				 ISCSI_CMD_REQUEST_TYPE_SHIFT);
429 		tmfabort_wqe->ref_itt = (dword |
430 					(tmfabort_hdr->rtt & ISCSI_ITT_MASK));
431 		break;
432 	default:
433 		tmfabort_wqe->ref_itt = RESERVED_ITT;
434 	}
435 	memcpy(scsi_lun, &tmfabort_hdr->lun, sizeof(struct scsi_lun));
436 	tmfabort_wqe->lun[0] = be32_to_cpu(scsi_lun[0]);
437 	tmfabort_wqe->lun[1] = be32_to_cpu(scsi_lun[1]);
438 
439 	tmfabort_wqe->ref_cmd_sn = be32_to_cpu(tmfabort_hdr->refcmdsn);
440 
441 	tmfabort_wqe->bd_list_addr_lo = (u32) bnx2i_conn->hba->mp_bd_dma;
442 	tmfabort_wqe->bd_list_addr_hi = (u32)
443 				((u64) bnx2i_conn->hba->mp_bd_dma >> 32);
444 	tmfabort_wqe->num_bds = 1;
445 	tmfabort_wqe->cq_index = 0; /* CQ# used for completion, 5771x only */
446 
447 	bnx2i_ring_dbell_update_sq_params(bnx2i_conn, 1);
448 	return 0;
449 }
450 
451 /**
452  * bnx2i_send_iscsi_text - post iSCSI text WQE to hardware
453  * @conn:	iscsi connection
454  * @mtask:	driver command structure which is requesting
455  *		a WQE to sent to chip for further processing
456  *
457  * prepare and post an iSCSI Text request WQE to CNIC firmware
458  */
459 int bnx2i_send_iscsi_text(struct bnx2i_conn *bnx2i_conn,
460 			  struct iscsi_task *mtask)
461 {
462 	struct bnx2i_text_request *text_wqe;
463 	struct iscsi_text *text_hdr;
464 	u32 dword;
465 
466 	text_hdr = (struct iscsi_text *)mtask->hdr;
467 	text_wqe = (struct bnx2i_text_request *) bnx2i_conn->ep->qp.sq_prod_qe;
468 
469 	memset(text_wqe, 0, sizeof(struct bnx2i_text_request));
470 
471 	text_wqe->op_code = text_hdr->opcode;
472 	text_wqe->op_attr = text_hdr->flags;
473 	text_wqe->data_length = ntoh24(text_hdr->dlength);
474 	text_wqe->itt = mtask->itt |
475 		(ISCSI_TASK_TYPE_MPATH << ISCSI_TEXT_REQUEST_TYPE_SHIFT);
476 	text_wqe->ttt = be32_to_cpu(text_hdr->ttt);
477 
478 	text_wqe->cmd_sn = be32_to_cpu(text_hdr->cmdsn);
479 
480 	text_wqe->resp_bd_list_addr_lo = (u32) bnx2i_conn->gen_pdu.resp_bd_dma;
481 	text_wqe->resp_bd_list_addr_hi =
482 			(u32) ((u64) bnx2i_conn->gen_pdu.resp_bd_dma >> 32);
483 
484 	dword = ((1 << ISCSI_TEXT_REQUEST_NUM_RESP_BDS_SHIFT) |
485 		 (bnx2i_conn->gen_pdu.resp_buf_size <<
486 		  ISCSI_TEXT_REQUEST_RESP_BUFFER_LENGTH_SHIFT));
487 	text_wqe->resp_buffer = dword;
488 	text_wqe->bd_list_addr_lo = (u32) bnx2i_conn->gen_pdu.req_bd_dma;
489 	text_wqe->bd_list_addr_hi =
490 			(u32) ((u64) bnx2i_conn->gen_pdu.req_bd_dma >> 32);
491 	text_wqe->num_bds = 1;
492 	text_wqe->cq_index = 0; /* CQ# used for completion, 5771x only */
493 
494 	bnx2i_ring_dbell_update_sq_params(bnx2i_conn, 1);
495 	return 0;
496 }
497 
498 
499 /**
500  * bnx2i_send_iscsi_scsicmd - post iSCSI scsicmd request WQE to hardware
501  * @conn:	iscsi connection
502  * @cmd:	driver command structure which is requesting
503  *		a WQE to sent to chip for further processing
504  *
505  * prepare and post an iSCSI SCSI-CMD request WQE to CNIC firmware
506  */
507 int bnx2i_send_iscsi_scsicmd(struct bnx2i_conn *bnx2i_conn,
508 			     struct bnx2i_cmd *cmd)
509 {
510 	struct bnx2i_cmd_request *scsi_cmd_wqe;
511 
512 	scsi_cmd_wqe = (struct bnx2i_cmd_request *)
513 						bnx2i_conn->ep->qp.sq_prod_qe;
514 	memcpy(scsi_cmd_wqe, &cmd->req, sizeof(struct bnx2i_cmd_request));
515 	scsi_cmd_wqe->cq_index = 0; /* CQ# used for completion, 5771x only */
516 
517 	bnx2i_ring_dbell_update_sq_params(bnx2i_conn, 1);
518 	return 0;
519 }
520 
521 /**
522  * bnx2i_send_iscsi_nopout - post iSCSI NOPOUT request WQE to hardware
523  * @conn:		iscsi connection
524  * @cmd:		driver command structure which is requesting
525  *			a WQE to sent to chip for further processing
526  * @datap:		payload buffer pointer
527  * @data_len:		payload data length
528  * @unsol:		indicated whether nopout pdu is unsolicited pdu or
529  *			in response to target's NOPIN w/ TTT != FFFFFFFF
530  *
531  * prepare and post a nopout request WQE to CNIC firmware
532  */
533 int bnx2i_send_iscsi_nopout(struct bnx2i_conn *bnx2i_conn,
534 			    struct iscsi_task *task,
535 			    char *datap, int data_len, int unsol)
536 {
537 	struct bnx2i_endpoint *ep = bnx2i_conn->ep;
538 	struct bnx2i_nop_out_request *nopout_wqe;
539 	struct iscsi_nopout *nopout_hdr;
540 
541 	nopout_hdr = (struct iscsi_nopout *)task->hdr;
542 	nopout_wqe = (struct bnx2i_nop_out_request *)ep->qp.sq_prod_qe;
543 
544 	memset(nopout_wqe, 0x00, sizeof(struct bnx2i_nop_out_request));
545 
546 	nopout_wqe->op_code = nopout_hdr->opcode;
547 	nopout_wqe->op_attr = ISCSI_FLAG_CMD_FINAL;
548 	memcpy(nopout_wqe->lun, &nopout_hdr->lun, 8);
549 
550 	/* 57710 requires LUN field to be swapped */
551 	if (test_bit(BNX2I_NX2_DEV_57710, &ep->hba->cnic_dev_type))
552 		swap(nopout_wqe->lun[0], nopout_wqe->lun[1]);
553 
554 	nopout_wqe->itt = ((u16)task->itt |
555 			   (ISCSI_TASK_TYPE_MPATH <<
556 			    ISCSI_TMF_REQUEST_TYPE_SHIFT));
557 	nopout_wqe->ttt = be32_to_cpu(nopout_hdr->ttt);
558 	nopout_wqe->flags = 0;
559 	if (!unsol)
560 		nopout_wqe->flags = ISCSI_NOP_OUT_REQUEST_LOCAL_COMPLETION;
561 	else if (nopout_hdr->itt == RESERVED_ITT)
562 		nopout_wqe->flags = ISCSI_NOP_OUT_REQUEST_LOCAL_COMPLETION;
563 
564 	nopout_wqe->cmd_sn = be32_to_cpu(nopout_hdr->cmdsn);
565 	nopout_wqe->data_length = data_len;
566 	if (data_len) {
567 		/* handle payload data, not required in first release */
568 		printk(KERN_ALERT "NOPOUT: WARNING!! payload len != 0\n");
569 	} else {
570 		nopout_wqe->bd_list_addr_lo = (u32)
571 					bnx2i_conn->hba->mp_bd_dma;
572 		nopout_wqe->bd_list_addr_hi =
573 			(u32) ((u64) bnx2i_conn->hba->mp_bd_dma >> 32);
574 		nopout_wqe->num_bds = 1;
575 	}
576 	nopout_wqe->cq_index = 0; /* CQ# used for completion, 5771x only */
577 
578 	bnx2i_ring_dbell_update_sq_params(bnx2i_conn, 1);
579 	return 0;
580 }
581 
582 
583 /**
584  * bnx2i_send_iscsi_logout - post iSCSI logout request WQE to hardware
585  * @conn:	iscsi connection
586  * @cmd:	driver command structure which is requesting
587  *		a WQE to sent to chip for further processing
588  *
589  * prepare and post logout request WQE to CNIC firmware
590  */
591 int bnx2i_send_iscsi_logout(struct bnx2i_conn *bnx2i_conn,
592 			    struct iscsi_task *task)
593 {
594 	struct bnx2i_logout_request *logout_wqe;
595 	struct iscsi_logout *logout_hdr;
596 
597 	logout_hdr = (struct iscsi_logout *)task->hdr;
598 
599 	logout_wqe = (struct bnx2i_logout_request *)
600 						bnx2i_conn->ep->qp.sq_prod_qe;
601 	memset(logout_wqe, 0x00, sizeof(struct bnx2i_logout_request));
602 
603 	logout_wqe->op_code = logout_hdr->opcode;
604 	logout_wqe->cmd_sn = be32_to_cpu(logout_hdr->cmdsn);
605 	logout_wqe->op_attr =
606 			logout_hdr->flags | ISCSI_LOGOUT_REQUEST_ALWAYS_ONE;
607 	logout_wqe->itt = ((u16)task->itt |
608 			   (ISCSI_TASK_TYPE_MPATH <<
609 			    ISCSI_LOGOUT_REQUEST_TYPE_SHIFT));
610 	logout_wqe->data_length = 0;
611 	logout_wqe->cid = 0;
612 
613 	logout_wqe->bd_list_addr_lo = (u32) bnx2i_conn->hba->mp_bd_dma;
614 	logout_wqe->bd_list_addr_hi = (u32)
615 				((u64) bnx2i_conn->hba->mp_bd_dma >> 32);
616 	logout_wqe->num_bds = 1;
617 	logout_wqe->cq_index = 0; /* CQ# used for completion, 5771x only */
618 
619 	bnx2i_conn->ep->state = EP_STATE_LOGOUT_SENT;
620 
621 	bnx2i_ring_dbell_update_sq_params(bnx2i_conn, 1);
622 	return 0;
623 }
624 
625 
626 /**
627  * bnx2i_update_iscsi_conn - post iSCSI logout request WQE to hardware
628  * @conn:	iscsi connection which requires iscsi parameter update
629  *
630  * sends down iSCSI Conn Update request to move iSCSI conn to FFP
631  */
632 void bnx2i_update_iscsi_conn(struct iscsi_conn *conn)
633 {
634 	struct bnx2i_conn *bnx2i_conn = conn->dd_data;
635 	struct bnx2i_hba *hba = bnx2i_conn->hba;
636 	struct kwqe *kwqe_arr[2];
637 	struct iscsi_kwqe_conn_update *update_wqe;
638 	struct iscsi_kwqe_conn_update conn_update_kwqe;
639 
640 	update_wqe = &conn_update_kwqe;
641 
642 	update_wqe->hdr.op_code = ISCSI_KWQE_OPCODE_UPDATE_CONN;
643 	update_wqe->hdr.flags =
644 		(ISCSI_KWQE_LAYER_CODE << ISCSI_KWQE_HEADER_LAYER_CODE_SHIFT);
645 
646 	/* 5771x requires conn context id to be passed as is */
647 	if (test_bit(BNX2I_NX2_DEV_57710, &bnx2i_conn->ep->hba->cnic_dev_type))
648 		update_wqe->context_id = bnx2i_conn->ep->ep_cid;
649 	else
650 		update_wqe->context_id = (bnx2i_conn->ep->ep_cid >> 7);
651 	update_wqe->conn_flags = 0;
652 	if (conn->hdrdgst_en)
653 		update_wqe->conn_flags |= ISCSI_KWQE_CONN_UPDATE_HEADER_DIGEST;
654 	if (conn->datadgst_en)
655 		update_wqe->conn_flags |= ISCSI_KWQE_CONN_UPDATE_DATA_DIGEST;
656 	if (conn->session->initial_r2t_en)
657 		update_wqe->conn_flags |= ISCSI_KWQE_CONN_UPDATE_INITIAL_R2T;
658 	if (conn->session->imm_data_en)
659 		update_wqe->conn_flags |= ISCSI_KWQE_CONN_UPDATE_IMMEDIATE_DATA;
660 
661 	update_wqe->max_send_pdu_length = conn->max_xmit_dlength;
662 	update_wqe->max_recv_pdu_length = conn->max_recv_dlength;
663 	update_wqe->first_burst_length = conn->session->first_burst;
664 	update_wqe->max_burst_length = conn->session->max_burst;
665 	update_wqe->exp_stat_sn = conn->exp_statsn;
666 	update_wqe->max_outstanding_r2ts = conn->session->max_r2t;
667 	update_wqe->session_error_recovery_level = conn->session->erl;
668 	iscsi_conn_printk(KERN_ALERT, conn,
669 			  "bnx2i: conn update - MBL 0x%x FBL 0x%x"
670 			  "MRDSL_I 0x%x MRDSL_T 0x%x \n",
671 			  update_wqe->max_burst_length,
672 			  update_wqe->first_burst_length,
673 			  update_wqe->max_recv_pdu_length,
674 			  update_wqe->max_send_pdu_length);
675 
676 	kwqe_arr[0] = (struct kwqe *) update_wqe;
677 	if (hba->cnic && hba->cnic->submit_kwqes)
678 		hba->cnic->submit_kwqes(hba->cnic, kwqe_arr, 1);
679 }
680 
681 
682 /**
683  * bnx2i_ep_ofld_timer - post iSCSI logout request WQE to hardware
684  * @data:	endpoint (transport handle) structure pointer
685  *
686  * routine to handle connection offload/destroy request timeout
687  */
688 void bnx2i_ep_ofld_timer(struct timer_list *t)
689 {
690 	struct bnx2i_endpoint *ep = from_timer(ep, t, ofld_timer);
691 
692 	if (ep->state == EP_STATE_OFLD_START) {
693 		printk(KERN_ALERT "ofld_timer: CONN_OFLD timeout\n");
694 		ep->state = EP_STATE_OFLD_FAILED;
695 	} else if (ep->state == EP_STATE_DISCONN_START) {
696 		printk(KERN_ALERT "ofld_timer: CONN_DISCON timeout\n");
697 		ep->state = EP_STATE_DISCONN_TIMEDOUT;
698 	} else if (ep->state == EP_STATE_CLEANUP_START) {
699 		printk(KERN_ALERT "ofld_timer: CONN_CLEANUP timeout\n");
700 		ep->state = EP_STATE_CLEANUP_FAILED;
701 	}
702 
703 	wake_up_interruptible(&ep->ofld_wait);
704 }
705 
706 
707 static int bnx2i_power_of2(u32 val)
708 {
709 	u32 power = 0;
710 	if (val & (val - 1))
711 		return power;
712 	val--;
713 	while (val) {
714 		val = val >> 1;
715 		power++;
716 	}
717 	return power;
718 }
719 
720 
721 /**
722  * bnx2i_send_cmd_cleanup_req - send iscsi cmd context clean-up request
723  * @hba:	adapter structure pointer
724  * @cmd:	driver command structure which is requesting
725  *		a WQE to sent to chip for further processing
726  *
727  * prepares and posts CONN_OFLD_REQ1/2 KWQE
728  */
729 void bnx2i_send_cmd_cleanup_req(struct bnx2i_hba *hba, struct bnx2i_cmd *cmd)
730 {
731 	struct bnx2i_cleanup_request *cmd_cleanup;
732 
733 	cmd_cleanup =
734 		(struct bnx2i_cleanup_request *)cmd->conn->ep->qp.sq_prod_qe;
735 	memset(cmd_cleanup, 0x00, sizeof(struct bnx2i_cleanup_request));
736 
737 	cmd_cleanup->op_code = ISCSI_OPCODE_CLEANUP_REQUEST;
738 	cmd_cleanup->itt = cmd->req.itt;
739 	cmd_cleanup->cq_index = 0; /* CQ# used for completion, 5771x only */
740 
741 	bnx2i_ring_dbell_update_sq_params(cmd->conn, 1);
742 }
743 
744 
745 /**
746  * bnx2i_send_conn_destroy - initiates iscsi connection teardown process
747  * @hba:	adapter structure pointer
748  * @ep:		endpoint (transport identifier) structure
749  *
750  * this routine prepares and posts CONN_OFLD_REQ1/2 KWQE to initiate
751  * 	iscsi connection context clean-up process
752  */
753 int bnx2i_send_conn_destroy(struct bnx2i_hba *hba, struct bnx2i_endpoint *ep)
754 {
755 	struct kwqe *kwqe_arr[2];
756 	struct iscsi_kwqe_conn_destroy conn_cleanup;
757 	int rc = -EINVAL;
758 
759 	memset(&conn_cleanup, 0x00, sizeof(struct iscsi_kwqe_conn_destroy));
760 
761 	conn_cleanup.hdr.op_code = ISCSI_KWQE_OPCODE_DESTROY_CONN;
762 	conn_cleanup.hdr.flags =
763 		(ISCSI_KWQE_LAYER_CODE << ISCSI_KWQE_HEADER_LAYER_CODE_SHIFT);
764 	/* 5771x requires conn context id to be passed as is */
765 	if (test_bit(BNX2I_NX2_DEV_57710, &ep->hba->cnic_dev_type))
766 		conn_cleanup.context_id = ep->ep_cid;
767 	else
768 		conn_cleanup.context_id = (ep->ep_cid >> 7);
769 
770 	conn_cleanup.reserved0 = (u16)ep->ep_iscsi_cid;
771 
772 	kwqe_arr[0] = (struct kwqe *) &conn_cleanup;
773 	if (hba->cnic && hba->cnic->submit_kwqes)
774 		rc = hba->cnic->submit_kwqes(hba->cnic, kwqe_arr, 1);
775 
776 	return rc;
777 }
778 
779 
780 /**
781  * bnx2i_570x_send_conn_ofld_req - initiates iscsi conn context setup process
782  * @hba: 		adapter structure pointer
783  * @ep: 		endpoint (transport identifier) structure
784  *
785  * 5706/5708/5709 specific - prepares and posts CONN_OFLD_REQ1/2 KWQE
786  */
787 static int bnx2i_570x_send_conn_ofld_req(struct bnx2i_hba *hba,
788 					 struct bnx2i_endpoint *ep)
789 {
790 	struct kwqe *kwqe_arr[2];
791 	struct iscsi_kwqe_conn_offload1 ofld_req1;
792 	struct iscsi_kwqe_conn_offload2 ofld_req2;
793 	dma_addr_t dma_addr;
794 	int num_kwqes = 2;
795 	u32 *ptbl;
796 	int rc = -EINVAL;
797 
798 	ofld_req1.hdr.op_code = ISCSI_KWQE_OPCODE_OFFLOAD_CONN1;
799 	ofld_req1.hdr.flags =
800 		(ISCSI_KWQE_LAYER_CODE << ISCSI_KWQE_HEADER_LAYER_CODE_SHIFT);
801 
802 	ofld_req1.iscsi_conn_id = (u16) ep->ep_iscsi_cid;
803 
804 	dma_addr = ep->qp.sq_pgtbl_phys;
805 	ofld_req1.sq_page_table_addr_lo = (u32) dma_addr;
806 	ofld_req1.sq_page_table_addr_hi = (u32) ((u64) dma_addr >> 32);
807 
808 	dma_addr = ep->qp.cq_pgtbl_phys;
809 	ofld_req1.cq_page_table_addr_lo = (u32) dma_addr;
810 	ofld_req1.cq_page_table_addr_hi = (u32) ((u64) dma_addr >> 32);
811 
812 	ofld_req2.hdr.op_code = ISCSI_KWQE_OPCODE_OFFLOAD_CONN2;
813 	ofld_req2.hdr.flags =
814 		(ISCSI_KWQE_LAYER_CODE << ISCSI_KWQE_HEADER_LAYER_CODE_SHIFT);
815 
816 	dma_addr = ep->qp.rq_pgtbl_phys;
817 	ofld_req2.rq_page_table_addr_lo = (u32) dma_addr;
818 	ofld_req2.rq_page_table_addr_hi = (u32) ((u64) dma_addr >> 32);
819 
820 	ptbl = (u32 *) ep->qp.sq_pgtbl_virt;
821 
822 	ofld_req2.sq_first_pte.hi = *ptbl++;
823 	ofld_req2.sq_first_pte.lo = *ptbl;
824 
825 	ptbl = (u32 *) ep->qp.cq_pgtbl_virt;
826 	ofld_req2.cq_first_pte.hi = *ptbl++;
827 	ofld_req2.cq_first_pte.lo = *ptbl;
828 
829 	kwqe_arr[0] = (struct kwqe *) &ofld_req1;
830 	kwqe_arr[1] = (struct kwqe *) &ofld_req2;
831 	ofld_req2.num_additional_wqes = 0;
832 
833 	if (hba->cnic && hba->cnic->submit_kwqes)
834 		rc = hba->cnic->submit_kwqes(hba->cnic, kwqe_arr, num_kwqes);
835 
836 	return rc;
837 }
838 
839 
840 /**
841  * bnx2i_5771x_send_conn_ofld_req - initiates iscsi connection context creation
842  * @hba: 		adapter structure pointer
843  * @ep: 		endpoint (transport identifier) structure
844  *
845  * 57710 specific - prepares and posts CONN_OFLD_REQ1/2 KWQE
846  */
847 static int bnx2i_5771x_send_conn_ofld_req(struct bnx2i_hba *hba,
848 					  struct bnx2i_endpoint *ep)
849 {
850 	struct kwqe *kwqe_arr[5];
851 	struct iscsi_kwqe_conn_offload1 ofld_req1;
852 	struct iscsi_kwqe_conn_offload2 ofld_req2;
853 	struct iscsi_kwqe_conn_offload3 ofld_req3[1];
854 	dma_addr_t dma_addr;
855 	int num_kwqes = 2;
856 	u32 *ptbl;
857 	int rc = -EINVAL;
858 
859 	ofld_req1.hdr.op_code = ISCSI_KWQE_OPCODE_OFFLOAD_CONN1;
860 	ofld_req1.hdr.flags =
861 		(ISCSI_KWQE_LAYER_CODE << ISCSI_KWQE_HEADER_LAYER_CODE_SHIFT);
862 
863 	ofld_req1.iscsi_conn_id = (u16) ep->ep_iscsi_cid;
864 
865 	dma_addr = ep->qp.sq_pgtbl_phys + ISCSI_SQ_DB_SIZE;
866 	ofld_req1.sq_page_table_addr_lo = (u32) dma_addr;
867 	ofld_req1.sq_page_table_addr_hi = (u32) ((u64) dma_addr >> 32);
868 
869 	dma_addr = ep->qp.cq_pgtbl_phys + ISCSI_CQ_DB_SIZE;
870 	ofld_req1.cq_page_table_addr_lo = (u32) dma_addr;
871 	ofld_req1.cq_page_table_addr_hi = (u32) ((u64) dma_addr >> 32);
872 
873 	ofld_req2.hdr.op_code = ISCSI_KWQE_OPCODE_OFFLOAD_CONN2;
874 	ofld_req2.hdr.flags =
875 		(ISCSI_KWQE_LAYER_CODE << ISCSI_KWQE_HEADER_LAYER_CODE_SHIFT);
876 
877 	dma_addr = ep->qp.rq_pgtbl_phys + ISCSI_RQ_DB_SIZE;
878 	ofld_req2.rq_page_table_addr_lo = (u32) dma_addr;
879 	ofld_req2.rq_page_table_addr_hi = (u32) ((u64) dma_addr >> 32);
880 
881 	ptbl = (u32 *)((u8 *)ep->qp.sq_pgtbl_virt + ISCSI_SQ_DB_SIZE);
882 	ofld_req2.sq_first_pte.hi = *ptbl++;
883 	ofld_req2.sq_first_pte.lo = *ptbl;
884 
885 	ptbl = (u32 *)((u8 *)ep->qp.cq_pgtbl_virt + ISCSI_CQ_DB_SIZE);
886 	ofld_req2.cq_first_pte.hi = *ptbl++;
887 	ofld_req2.cq_first_pte.lo = *ptbl;
888 
889 	kwqe_arr[0] = (struct kwqe *) &ofld_req1;
890 	kwqe_arr[1] = (struct kwqe *) &ofld_req2;
891 
892 	ofld_req2.num_additional_wqes = 1;
893 	memset(ofld_req3, 0x00, sizeof(ofld_req3[0]));
894 	ptbl = (u32 *)((u8 *)ep->qp.rq_pgtbl_virt + ISCSI_RQ_DB_SIZE);
895 	ofld_req3[0].qp_first_pte[0].hi = *ptbl++;
896 	ofld_req3[0].qp_first_pte[0].lo = *ptbl;
897 
898 	kwqe_arr[2] = (struct kwqe *) ofld_req3;
899 	/* need if we decide to go with multiple KCQE's per conn */
900 	num_kwqes += 1;
901 
902 	if (hba->cnic && hba->cnic->submit_kwqes)
903 		rc = hba->cnic->submit_kwqes(hba->cnic, kwqe_arr, num_kwqes);
904 
905 	return rc;
906 }
907 
908 /**
909  * bnx2i_send_conn_ofld_req - initiates iscsi connection context setup process
910  *
911  * @hba: 		adapter structure pointer
912  * @ep: 		endpoint (transport identifier) structure
913  *
914  * this routine prepares and posts CONN_OFLD_REQ1/2 KWQE
915  */
916 int bnx2i_send_conn_ofld_req(struct bnx2i_hba *hba, struct bnx2i_endpoint *ep)
917 {
918 	int rc;
919 
920 	if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type))
921 		rc = bnx2i_5771x_send_conn_ofld_req(hba, ep);
922 	else
923 		rc = bnx2i_570x_send_conn_ofld_req(hba, ep);
924 
925 	return rc;
926 }
927 
928 
929 /**
930  * setup_qp_page_tables - iscsi QP page table setup function
931  * @ep:		endpoint (transport identifier) structure
932  *
933  * Sets up page tables for SQ/RQ/CQ, 1G/sec (5706/5708/5709) devices requires
934  * 	64-bit address in big endian format. Whereas 10G/sec (57710) requires
935  * 	PT in little endian format
936  */
937 static void setup_qp_page_tables(struct bnx2i_endpoint *ep)
938 {
939 	int num_pages;
940 	u32 *ptbl;
941 	dma_addr_t page;
942 	int cnic_dev_10g;
943 
944 	if (test_bit(BNX2I_NX2_DEV_57710, &ep->hba->cnic_dev_type))
945 		cnic_dev_10g = 1;
946 	else
947 		cnic_dev_10g = 0;
948 
949 	/* SQ page table */
950 	memset(ep->qp.sq_pgtbl_virt, 0, ep->qp.sq_pgtbl_size);
951 	num_pages = ep->qp.sq_mem_size / CNIC_PAGE_SIZE;
952 	page = ep->qp.sq_phys;
953 
954 	if (cnic_dev_10g)
955 		ptbl = (u32 *)((u8 *)ep->qp.sq_pgtbl_virt + ISCSI_SQ_DB_SIZE);
956 	else
957 		ptbl = (u32 *) ep->qp.sq_pgtbl_virt;
958 	while (num_pages--) {
959 		if (cnic_dev_10g) {
960 			/* PTE is written in little endian format for 57710 */
961 			*ptbl = (u32) page;
962 			ptbl++;
963 			*ptbl = (u32) ((u64) page >> 32);
964 			ptbl++;
965 			page += CNIC_PAGE_SIZE;
966 		} else {
967 			/* PTE is written in big endian format for
968 			 * 5706/5708/5709 devices */
969 			*ptbl = (u32) ((u64) page >> 32);
970 			ptbl++;
971 			*ptbl = (u32) page;
972 			ptbl++;
973 			page += CNIC_PAGE_SIZE;
974 		}
975 	}
976 
977 	/* RQ page table */
978 	memset(ep->qp.rq_pgtbl_virt, 0, ep->qp.rq_pgtbl_size);
979 	num_pages = ep->qp.rq_mem_size / CNIC_PAGE_SIZE;
980 	page = ep->qp.rq_phys;
981 
982 	if (cnic_dev_10g)
983 		ptbl = (u32 *)((u8 *)ep->qp.rq_pgtbl_virt + ISCSI_RQ_DB_SIZE);
984 	else
985 		ptbl = (u32 *) ep->qp.rq_pgtbl_virt;
986 	while (num_pages--) {
987 		if (cnic_dev_10g) {
988 			/* PTE is written in little endian format for 57710 */
989 			*ptbl = (u32) page;
990 			ptbl++;
991 			*ptbl = (u32) ((u64) page >> 32);
992 			ptbl++;
993 			page += CNIC_PAGE_SIZE;
994 		} else {
995 			/* PTE is written in big endian format for
996 			 * 5706/5708/5709 devices */
997 			*ptbl = (u32) ((u64) page >> 32);
998 			ptbl++;
999 			*ptbl = (u32) page;
1000 			ptbl++;
1001 			page += CNIC_PAGE_SIZE;
1002 		}
1003 	}
1004 
1005 	/* CQ page table */
1006 	memset(ep->qp.cq_pgtbl_virt, 0, ep->qp.cq_pgtbl_size);
1007 	num_pages = ep->qp.cq_mem_size / CNIC_PAGE_SIZE;
1008 	page = ep->qp.cq_phys;
1009 
1010 	if (cnic_dev_10g)
1011 		ptbl = (u32 *)((u8 *)ep->qp.cq_pgtbl_virt + ISCSI_CQ_DB_SIZE);
1012 	else
1013 		ptbl = (u32 *) ep->qp.cq_pgtbl_virt;
1014 	while (num_pages--) {
1015 		if (cnic_dev_10g) {
1016 			/* PTE is written in little endian format for 57710 */
1017 			*ptbl = (u32) page;
1018 			ptbl++;
1019 			*ptbl = (u32) ((u64) page >> 32);
1020 			ptbl++;
1021 			page += CNIC_PAGE_SIZE;
1022 		} else {
1023 			/* PTE is written in big endian format for
1024 			 * 5706/5708/5709 devices */
1025 			*ptbl = (u32) ((u64) page >> 32);
1026 			ptbl++;
1027 			*ptbl = (u32) page;
1028 			ptbl++;
1029 			page += CNIC_PAGE_SIZE;
1030 		}
1031 	}
1032 }
1033 
1034 
1035 /**
1036  * bnx2i_alloc_qp_resc - allocates required resources for QP.
1037  * @hba:	adapter structure pointer
1038  * @ep:		endpoint (transport identifier) structure
1039  *
1040  * Allocate QP (transport layer for iSCSI connection) resources, DMA'able
1041  *	memory for SQ/RQ/CQ and page tables. EP structure elements such
1042  *	as producer/consumer indexes/pointers, queue sizes and page table
1043  *	contents are setup
1044  */
1045 int bnx2i_alloc_qp_resc(struct bnx2i_hba *hba, struct bnx2i_endpoint *ep)
1046 {
1047 	struct bnx2i_5771x_cq_db *cq_db;
1048 
1049 	ep->hba = hba;
1050 	ep->conn = NULL;
1051 	ep->ep_cid = ep->ep_iscsi_cid = ep->ep_pg_cid = 0;
1052 
1053 	/* Allocate page table memory for SQ which is page aligned */
1054 	ep->qp.sq_mem_size = hba->max_sqes * BNX2I_SQ_WQE_SIZE;
1055 	ep->qp.sq_mem_size =
1056 		(ep->qp.sq_mem_size + (CNIC_PAGE_SIZE - 1)) & CNIC_PAGE_MASK;
1057 	ep->qp.sq_pgtbl_size =
1058 		(ep->qp.sq_mem_size / CNIC_PAGE_SIZE) * sizeof(void *);
1059 	ep->qp.sq_pgtbl_size =
1060 		(ep->qp.sq_pgtbl_size + (CNIC_PAGE_SIZE - 1)) & CNIC_PAGE_MASK;
1061 
1062 	ep->qp.sq_pgtbl_virt =
1063 		dma_alloc_coherent(&hba->pcidev->dev, ep->qp.sq_pgtbl_size,
1064 				   &ep->qp.sq_pgtbl_phys, GFP_KERNEL);
1065 	if (!ep->qp.sq_pgtbl_virt) {
1066 		printk(KERN_ALERT "bnx2i: unable to alloc SQ PT mem (%d)\n",
1067 				  ep->qp.sq_pgtbl_size);
1068 		goto mem_alloc_err;
1069 	}
1070 
1071 	/* Allocate memory area for actual SQ element */
1072 	ep->qp.sq_virt =
1073 		dma_alloc_coherent(&hba->pcidev->dev, ep->qp.sq_mem_size,
1074 				   &ep->qp.sq_phys, GFP_KERNEL);
1075 	if (!ep->qp.sq_virt) {
1076 		printk(KERN_ALERT "bnx2i: unable to alloc SQ BD memory %d\n",
1077 				  ep->qp.sq_mem_size);
1078 		goto mem_alloc_err;
1079 	}
1080 
1081 	memset(ep->qp.sq_virt, 0x00, ep->qp.sq_mem_size);
1082 	ep->qp.sq_first_qe = ep->qp.sq_virt;
1083 	ep->qp.sq_prod_qe = ep->qp.sq_first_qe;
1084 	ep->qp.sq_cons_qe = ep->qp.sq_first_qe;
1085 	ep->qp.sq_last_qe = &ep->qp.sq_first_qe[hba->max_sqes - 1];
1086 	ep->qp.sq_prod_idx = 0;
1087 	ep->qp.sq_cons_idx = 0;
1088 	ep->qp.sqe_left = hba->max_sqes;
1089 
1090 	/* Allocate page table memory for CQ which is page aligned */
1091 	ep->qp.cq_mem_size = hba->max_cqes * BNX2I_CQE_SIZE;
1092 	ep->qp.cq_mem_size =
1093 		(ep->qp.cq_mem_size + (CNIC_PAGE_SIZE - 1)) & CNIC_PAGE_MASK;
1094 	ep->qp.cq_pgtbl_size =
1095 		(ep->qp.cq_mem_size / CNIC_PAGE_SIZE) * sizeof(void *);
1096 	ep->qp.cq_pgtbl_size =
1097 		(ep->qp.cq_pgtbl_size + (CNIC_PAGE_SIZE - 1)) & CNIC_PAGE_MASK;
1098 
1099 	ep->qp.cq_pgtbl_virt =
1100 		dma_alloc_coherent(&hba->pcidev->dev, ep->qp.cq_pgtbl_size,
1101 				   &ep->qp.cq_pgtbl_phys, GFP_KERNEL);
1102 	if (!ep->qp.cq_pgtbl_virt) {
1103 		printk(KERN_ALERT "bnx2i: unable to alloc CQ PT memory %d\n",
1104 				  ep->qp.cq_pgtbl_size);
1105 		goto mem_alloc_err;
1106 	}
1107 
1108 	/* Allocate memory area for actual CQ element */
1109 	ep->qp.cq_virt =
1110 		dma_alloc_coherent(&hba->pcidev->dev, ep->qp.cq_mem_size,
1111 				   &ep->qp.cq_phys, GFP_KERNEL);
1112 	if (!ep->qp.cq_virt) {
1113 		printk(KERN_ALERT "bnx2i: unable to alloc CQ BD memory %d\n",
1114 				  ep->qp.cq_mem_size);
1115 		goto mem_alloc_err;
1116 	}
1117 	memset(ep->qp.cq_virt, 0x00, ep->qp.cq_mem_size);
1118 
1119 	ep->qp.cq_first_qe = ep->qp.cq_virt;
1120 	ep->qp.cq_prod_qe = ep->qp.cq_first_qe;
1121 	ep->qp.cq_cons_qe = ep->qp.cq_first_qe;
1122 	ep->qp.cq_last_qe = &ep->qp.cq_first_qe[hba->max_cqes - 1];
1123 	ep->qp.cq_prod_idx = 0;
1124 	ep->qp.cq_cons_idx = 0;
1125 	ep->qp.cqe_left = hba->max_cqes;
1126 	ep->qp.cqe_exp_seq_sn = ISCSI_INITIAL_SN;
1127 	ep->qp.cqe_size = hba->max_cqes;
1128 
1129 	/* Invalidate all EQ CQE index, req only for 57710 */
1130 	cq_db = (struct bnx2i_5771x_cq_db *) ep->qp.cq_pgtbl_virt;
1131 	memset(cq_db->sqn, 0xFF, sizeof(cq_db->sqn[0]) * BNX2X_MAX_CQS);
1132 
1133 	/* Allocate page table memory for RQ which is page aligned */
1134 	ep->qp.rq_mem_size = hba->max_rqes * BNX2I_RQ_WQE_SIZE;
1135 	ep->qp.rq_mem_size =
1136 		(ep->qp.rq_mem_size + (CNIC_PAGE_SIZE - 1)) & CNIC_PAGE_MASK;
1137 	ep->qp.rq_pgtbl_size =
1138 		(ep->qp.rq_mem_size / CNIC_PAGE_SIZE) * sizeof(void *);
1139 	ep->qp.rq_pgtbl_size =
1140 		(ep->qp.rq_pgtbl_size + (CNIC_PAGE_SIZE - 1)) & CNIC_PAGE_MASK;
1141 
1142 	ep->qp.rq_pgtbl_virt =
1143 		dma_alloc_coherent(&hba->pcidev->dev, ep->qp.rq_pgtbl_size,
1144 				   &ep->qp.rq_pgtbl_phys, GFP_KERNEL);
1145 	if (!ep->qp.rq_pgtbl_virt) {
1146 		printk(KERN_ALERT "bnx2i: unable to alloc RQ PT mem %d\n",
1147 				  ep->qp.rq_pgtbl_size);
1148 		goto mem_alloc_err;
1149 	}
1150 
1151 	/* Allocate memory area for actual RQ element */
1152 	ep->qp.rq_virt =
1153 		dma_alloc_coherent(&hba->pcidev->dev, ep->qp.rq_mem_size,
1154 				   &ep->qp.rq_phys, GFP_KERNEL);
1155 	if (!ep->qp.rq_virt) {
1156 		printk(KERN_ALERT "bnx2i: unable to alloc RQ BD memory %d\n",
1157 				  ep->qp.rq_mem_size);
1158 		goto mem_alloc_err;
1159 	}
1160 
1161 	ep->qp.rq_first_qe = ep->qp.rq_virt;
1162 	ep->qp.rq_prod_qe = ep->qp.rq_first_qe;
1163 	ep->qp.rq_cons_qe = ep->qp.rq_first_qe;
1164 	ep->qp.rq_last_qe = &ep->qp.rq_first_qe[hba->max_rqes - 1];
1165 	ep->qp.rq_prod_idx = 0x8000;
1166 	ep->qp.rq_cons_idx = 0;
1167 	ep->qp.rqe_left = hba->max_rqes;
1168 
1169 	setup_qp_page_tables(ep);
1170 
1171 	return 0;
1172 
1173 mem_alloc_err:
1174 	bnx2i_free_qp_resc(hba, ep);
1175 	return -ENOMEM;
1176 }
1177 
1178 
1179 
1180 /**
1181  * bnx2i_free_qp_resc - free memory resources held by QP
1182  * @hba:	adapter structure pointer
1183  * @ep:	endpoint (transport identifier) structure
1184  *
1185  * Free QP resources - SQ/RQ/CQ memory and page tables.
1186  */
1187 void bnx2i_free_qp_resc(struct bnx2i_hba *hba, struct bnx2i_endpoint *ep)
1188 {
1189 	if (ep->qp.ctx_base) {
1190 		iounmap(ep->qp.ctx_base);
1191 		ep->qp.ctx_base = NULL;
1192 	}
1193 	/* Free SQ mem */
1194 	if (ep->qp.sq_pgtbl_virt) {
1195 		dma_free_coherent(&hba->pcidev->dev, ep->qp.sq_pgtbl_size,
1196 				  ep->qp.sq_pgtbl_virt, ep->qp.sq_pgtbl_phys);
1197 		ep->qp.sq_pgtbl_virt = NULL;
1198 		ep->qp.sq_pgtbl_phys = 0;
1199 	}
1200 	if (ep->qp.sq_virt) {
1201 		dma_free_coherent(&hba->pcidev->dev, ep->qp.sq_mem_size,
1202 				  ep->qp.sq_virt, ep->qp.sq_phys);
1203 		ep->qp.sq_virt = NULL;
1204 		ep->qp.sq_phys = 0;
1205 	}
1206 
1207 	/* Free RQ mem */
1208 	if (ep->qp.rq_pgtbl_virt) {
1209 		dma_free_coherent(&hba->pcidev->dev, ep->qp.rq_pgtbl_size,
1210 				  ep->qp.rq_pgtbl_virt, ep->qp.rq_pgtbl_phys);
1211 		ep->qp.rq_pgtbl_virt = NULL;
1212 		ep->qp.rq_pgtbl_phys = 0;
1213 	}
1214 	if (ep->qp.rq_virt) {
1215 		dma_free_coherent(&hba->pcidev->dev, ep->qp.rq_mem_size,
1216 				  ep->qp.rq_virt, ep->qp.rq_phys);
1217 		ep->qp.rq_virt = NULL;
1218 		ep->qp.rq_phys = 0;
1219 	}
1220 
1221 	/* Free CQ mem */
1222 	if (ep->qp.cq_pgtbl_virt) {
1223 		dma_free_coherent(&hba->pcidev->dev, ep->qp.cq_pgtbl_size,
1224 				  ep->qp.cq_pgtbl_virt, ep->qp.cq_pgtbl_phys);
1225 		ep->qp.cq_pgtbl_virt = NULL;
1226 		ep->qp.cq_pgtbl_phys = 0;
1227 	}
1228 	if (ep->qp.cq_virt) {
1229 		dma_free_coherent(&hba->pcidev->dev, ep->qp.cq_mem_size,
1230 				  ep->qp.cq_virt, ep->qp.cq_phys);
1231 		ep->qp.cq_virt = NULL;
1232 		ep->qp.cq_phys = 0;
1233 	}
1234 }
1235 
1236 
1237 /**
1238  * bnx2i_send_fw_iscsi_init_msg - initiates initial handshake with iscsi f/w
1239  * @hba:	adapter structure pointer
1240  *
1241  * Send down iscsi_init KWQEs which initiates the initial handshake with the f/w
1242  * 	This results in iSCSi support validation and on-chip context manager
1243  * 	initialization.  Firmware completes this handshake with a CQE carrying
1244  * 	the result of iscsi support validation. Parameter carried by
1245  * 	iscsi init request determines the number of offloaded connection and
1246  * 	tolerance level for iscsi protocol violation this hba/chip can support
1247  */
1248 int bnx2i_send_fw_iscsi_init_msg(struct bnx2i_hba *hba)
1249 {
1250 	struct kwqe *kwqe_arr[3];
1251 	struct iscsi_kwqe_init1 iscsi_init;
1252 	struct iscsi_kwqe_init2 iscsi_init2;
1253 	int rc = 0;
1254 	u64 mask64;
1255 
1256 	memset(&iscsi_init, 0x00, sizeof(struct iscsi_kwqe_init1));
1257 	memset(&iscsi_init2, 0x00, sizeof(struct iscsi_kwqe_init2));
1258 
1259 	bnx2i_adjust_qp_size(hba);
1260 
1261 	iscsi_init.flags =
1262 		(CNIC_PAGE_BITS - 8) << ISCSI_KWQE_INIT1_PAGE_SIZE_SHIFT;
1263 	if (en_tcp_dack)
1264 		iscsi_init.flags |= ISCSI_KWQE_INIT1_DELAYED_ACK_ENABLE;
1265 	iscsi_init.reserved0 = 0;
1266 	iscsi_init.num_cqs = 1;
1267 	iscsi_init.hdr.op_code = ISCSI_KWQE_OPCODE_INIT1;
1268 	iscsi_init.hdr.flags =
1269 		(ISCSI_KWQE_LAYER_CODE << ISCSI_KWQE_HEADER_LAYER_CODE_SHIFT);
1270 
1271 	iscsi_init.dummy_buffer_addr_lo = (u32) hba->dummy_buf_dma;
1272 	iscsi_init.dummy_buffer_addr_hi =
1273 		(u32) ((u64) hba->dummy_buf_dma >> 32);
1274 
1275 	hba->num_ccell = hba->max_sqes >> 1;
1276 	hba->ctx_ccell_tasks =
1277 			((hba->num_ccell & 0xFFFF) | (hba->max_sqes << 16));
1278 	iscsi_init.num_ccells_per_conn = hba->num_ccell;
1279 	iscsi_init.num_tasks_per_conn = hba->max_sqes;
1280 	iscsi_init.sq_wqes_per_page = CNIC_PAGE_SIZE / BNX2I_SQ_WQE_SIZE;
1281 	iscsi_init.sq_num_wqes = hba->max_sqes;
1282 	iscsi_init.cq_log_wqes_per_page =
1283 		(u8) bnx2i_power_of2(CNIC_PAGE_SIZE / BNX2I_CQE_SIZE);
1284 	iscsi_init.cq_num_wqes = hba->max_cqes;
1285 	iscsi_init.cq_num_pages = (hba->max_cqes * BNX2I_CQE_SIZE +
1286 				   (CNIC_PAGE_SIZE - 1)) / CNIC_PAGE_SIZE;
1287 	iscsi_init.sq_num_pages = (hba->max_sqes * BNX2I_SQ_WQE_SIZE +
1288 				   (CNIC_PAGE_SIZE - 1)) / CNIC_PAGE_SIZE;
1289 	iscsi_init.rq_buffer_size = BNX2I_RQ_WQE_SIZE;
1290 	iscsi_init.rq_num_wqes = hba->max_rqes;
1291 
1292 
1293 	iscsi_init2.hdr.op_code = ISCSI_KWQE_OPCODE_INIT2;
1294 	iscsi_init2.hdr.flags =
1295 		(ISCSI_KWQE_LAYER_CODE << ISCSI_KWQE_HEADER_LAYER_CODE_SHIFT);
1296 	iscsi_init2.max_cq_sqn = hba->max_cqes * 2 + 1;
1297 	mask64 = 0x0ULL;
1298 	mask64 |= (
1299 		/* CISCO MDS */
1300 		(1UL <<
1301 		  ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_TTT_NOT_RSRV) |
1302 		/* HP MSA1510i */
1303 		(1UL <<
1304 		  ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_EXP_DATASN) |
1305 		/* EMC */
1306 		(1ULL << ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_LUN));
1307 	if (error_mask1) {
1308 		iscsi_init2.error_bit_map[0] = error_mask1;
1309 		mask64 ^= (u32)(mask64);
1310 		mask64 |= error_mask1;
1311 	} else
1312 		iscsi_init2.error_bit_map[0] = (u32) mask64;
1313 
1314 	if (error_mask2) {
1315 		iscsi_init2.error_bit_map[1] = error_mask2;
1316 		mask64 &= 0xffffffff;
1317 		mask64 |= ((u64)error_mask2 << 32);
1318 	} else
1319 		iscsi_init2.error_bit_map[1] = (u32) (mask64 >> 32);
1320 
1321 	iscsi_error_mask = mask64;
1322 
1323 	kwqe_arr[0] = (struct kwqe *) &iscsi_init;
1324 	kwqe_arr[1] = (struct kwqe *) &iscsi_init2;
1325 
1326 	if (hba->cnic && hba->cnic->submit_kwqes)
1327 		rc = hba->cnic->submit_kwqes(hba->cnic, kwqe_arr, 2);
1328 	return rc;
1329 }
1330 
1331 
1332 /**
1333  * bnx2i_process_scsi_cmd_resp - this function handles scsi cmd completion.
1334  * @session:	iscsi session
1335  * @bnx2i_conn:	bnx2i connection
1336  * @cqe:	pointer to newly DMA'ed CQE entry for processing
1337  *
1338  * process SCSI CMD Response CQE & complete the request to SCSI-ML
1339  */
1340 int bnx2i_process_scsi_cmd_resp(struct iscsi_session *session,
1341 				struct bnx2i_conn *bnx2i_conn,
1342 				struct cqe *cqe)
1343 {
1344 	struct iscsi_conn *conn = bnx2i_conn->cls_conn->dd_data;
1345 	struct bnx2i_hba *hba = bnx2i_conn->hba;
1346 	struct bnx2i_cmd_response *resp_cqe;
1347 	struct bnx2i_cmd *bnx2i_cmd;
1348 	struct iscsi_task *task;
1349 	struct iscsi_scsi_rsp *hdr;
1350 	u32 datalen = 0;
1351 
1352 	resp_cqe = (struct bnx2i_cmd_response *)cqe;
1353 	spin_lock_bh(&session->back_lock);
1354 	task = iscsi_itt_to_task(conn,
1355 				 resp_cqe->itt & ISCSI_CMD_RESPONSE_INDEX);
1356 	if (!task)
1357 		goto fail;
1358 
1359 	bnx2i_cmd = task->dd_data;
1360 
1361 	if (bnx2i_cmd->req.op_attr & ISCSI_CMD_REQUEST_READ) {
1362 		conn->datain_pdus_cnt +=
1363 			resp_cqe->task_stat.read_stat.num_data_ins;
1364 		conn->rxdata_octets +=
1365 			bnx2i_cmd->req.total_data_transfer_length;
1366 		ADD_STATS_64(hba, rx_pdus,
1367 			     resp_cqe->task_stat.read_stat.num_data_ins);
1368 		ADD_STATS_64(hba, rx_bytes,
1369 			     bnx2i_cmd->req.total_data_transfer_length);
1370 	} else {
1371 		conn->dataout_pdus_cnt +=
1372 			resp_cqe->task_stat.write_stat.num_data_outs;
1373 		conn->r2t_pdus_cnt +=
1374 			resp_cqe->task_stat.write_stat.num_r2ts;
1375 		conn->txdata_octets +=
1376 			bnx2i_cmd->req.total_data_transfer_length;
1377 		ADD_STATS_64(hba, tx_pdus,
1378 			     resp_cqe->task_stat.write_stat.num_data_outs);
1379 		ADD_STATS_64(hba, tx_bytes,
1380 			     bnx2i_cmd->req.total_data_transfer_length);
1381 		ADD_STATS_64(hba, rx_pdus,
1382 			     resp_cqe->task_stat.write_stat.num_r2ts);
1383 	}
1384 	bnx2i_iscsi_unmap_sg_list(bnx2i_cmd);
1385 
1386 	hdr = (struct iscsi_scsi_rsp *)task->hdr;
1387 	resp_cqe = (struct bnx2i_cmd_response *)cqe;
1388 	hdr->opcode = resp_cqe->op_code;
1389 	hdr->max_cmdsn = cpu_to_be32(resp_cqe->max_cmd_sn);
1390 	hdr->exp_cmdsn = cpu_to_be32(resp_cqe->exp_cmd_sn);
1391 	hdr->response = resp_cqe->response;
1392 	hdr->cmd_status = resp_cqe->status;
1393 	hdr->flags = resp_cqe->response_flags;
1394 	hdr->residual_count = cpu_to_be32(resp_cqe->residual_count);
1395 
1396 	if (resp_cqe->op_code == ISCSI_OP_SCSI_DATA_IN)
1397 		goto done;
1398 
1399 	if (resp_cqe->status == SAM_STAT_CHECK_CONDITION) {
1400 		datalen = resp_cqe->data_length;
1401 		if (datalen < 2)
1402 			goto done;
1403 
1404 		if (datalen > BNX2I_RQ_WQE_SIZE) {
1405 			iscsi_conn_printk(KERN_ERR, conn,
1406 					  "sense data len %d > RQ sz\n",
1407 					  datalen);
1408 			datalen = BNX2I_RQ_WQE_SIZE;
1409 		} else if (datalen > ISCSI_DEF_MAX_RECV_SEG_LEN) {
1410 			iscsi_conn_printk(KERN_ERR, conn,
1411 					  "sense data len %d > conn data\n",
1412 					  datalen);
1413 			datalen = ISCSI_DEF_MAX_RECV_SEG_LEN;
1414 		}
1415 
1416 		bnx2i_get_rq_buf(bnx2i_cmd->conn, conn->data, datalen);
1417 		bnx2i_put_rq_buf(bnx2i_cmd->conn, 1);
1418 	}
1419 
1420 done:
1421 	__iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr,
1422 			     conn->data, datalen);
1423 fail:
1424 	spin_unlock_bh(&session->back_lock);
1425 	return 0;
1426 }
1427 
1428 
1429 /**
1430  * bnx2i_process_login_resp - this function handles iscsi login response
1431  * @session:		iscsi session pointer
1432  * @bnx2i_conn:		iscsi connection pointer
1433  * @cqe:		pointer to newly DMA'ed CQE entry for processing
1434  *
1435  * process Login Response CQE & complete it to open-iscsi user daemon
1436  */
1437 static int bnx2i_process_login_resp(struct iscsi_session *session,
1438 				    struct bnx2i_conn *bnx2i_conn,
1439 				    struct cqe *cqe)
1440 {
1441 	struct iscsi_conn *conn = bnx2i_conn->cls_conn->dd_data;
1442 	struct iscsi_task *task;
1443 	struct bnx2i_login_response *login;
1444 	struct iscsi_login_rsp *resp_hdr;
1445 	int pld_len;
1446 	int pad_len;
1447 
1448 	login = (struct bnx2i_login_response *) cqe;
1449 	spin_lock(&session->back_lock);
1450 	task = iscsi_itt_to_task(conn,
1451 				 login->itt & ISCSI_LOGIN_RESPONSE_INDEX);
1452 	if (!task)
1453 		goto done;
1454 
1455 	resp_hdr = (struct iscsi_login_rsp *) &bnx2i_conn->gen_pdu.resp_hdr;
1456 	memset(resp_hdr, 0, sizeof(struct iscsi_hdr));
1457 	resp_hdr->opcode = login->op_code;
1458 	resp_hdr->flags = login->response_flags;
1459 	resp_hdr->max_version = login->version_max;
1460 	resp_hdr->active_version = login->version_active;
1461 	resp_hdr->hlength = 0;
1462 
1463 	hton24(resp_hdr->dlength, login->data_length);
1464 	memcpy(resp_hdr->isid, &login->isid_lo, 6);
1465 	resp_hdr->tsih = cpu_to_be16(login->tsih);
1466 	resp_hdr->itt = task->hdr->itt;
1467 	resp_hdr->statsn = cpu_to_be32(login->stat_sn);
1468 	resp_hdr->exp_cmdsn = cpu_to_be32(login->exp_cmd_sn);
1469 	resp_hdr->max_cmdsn = cpu_to_be32(login->max_cmd_sn);
1470 	resp_hdr->status_class = login->status_class;
1471 	resp_hdr->status_detail = login->status_detail;
1472 	pld_len = login->data_length;
1473 	bnx2i_conn->gen_pdu.resp_wr_ptr =
1474 					bnx2i_conn->gen_pdu.resp_buf + pld_len;
1475 
1476 	pad_len = 0;
1477 	if (pld_len & 0x3)
1478 		pad_len = 4 - (pld_len % 4);
1479 
1480 	if (pad_len) {
1481 		int i = 0;
1482 		for (i = 0; i < pad_len; i++) {
1483 			bnx2i_conn->gen_pdu.resp_wr_ptr[0] = 0;
1484 			bnx2i_conn->gen_pdu.resp_wr_ptr++;
1485 		}
1486 	}
1487 
1488 	__iscsi_complete_pdu(conn, (struct iscsi_hdr *)resp_hdr,
1489 		bnx2i_conn->gen_pdu.resp_buf,
1490 		bnx2i_conn->gen_pdu.resp_wr_ptr - bnx2i_conn->gen_pdu.resp_buf);
1491 done:
1492 	spin_unlock(&session->back_lock);
1493 	return 0;
1494 }
1495 
1496 
1497 /**
1498  * bnx2i_process_text_resp - this function handles iscsi text response
1499  * @session:	iscsi session pointer
1500  * @bnx2i_conn:	iscsi connection pointer
1501  * @cqe:	pointer to newly DMA'ed CQE entry for processing
1502  *
1503  * process iSCSI Text Response CQE&  complete it to open-iscsi user daemon
1504  */
1505 static int bnx2i_process_text_resp(struct iscsi_session *session,
1506 				   struct bnx2i_conn *bnx2i_conn,
1507 				   struct cqe *cqe)
1508 {
1509 	struct iscsi_conn *conn = bnx2i_conn->cls_conn->dd_data;
1510 	struct iscsi_task *task;
1511 	struct bnx2i_text_response *text;
1512 	struct iscsi_text_rsp *resp_hdr;
1513 	int pld_len;
1514 	int pad_len;
1515 
1516 	text = (struct bnx2i_text_response *) cqe;
1517 	spin_lock(&session->back_lock);
1518 	task = iscsi_itt_to_task(conn, text->itt & ISCSI_LOGIN_RESPONSE_INDEX);
1519 	if (!task)
1520 		goto done;
1521 
1522 	resp_hdr = (struct iscsi_text_rsp *)&bnx2i_conn->gen_pdu.resp_hdr;
1523 	memset(resp_hdr, 0, sizeof(struct iscsi_hdr));
1524 	resp_hdr->opcode = text->op_code;
1525 	resp_hdr->flags = text->response_flags;
1526 	resp_hdr->hlength = 0;
1527 
1528 	hton24(resp_hdr->dlength, text->data_length);
1529 	resp_hdr->itt = task->hdr->itt;
1530 	resp_hdr->ttt = cpu_to_be32(text->ttt);
1531 	resp_hdr->statsn = task->hdr->exp_statsn;
1532 	resp_hdr->exp_cmdsn = cpu_to_be32(text->exp_cmd_sn);
1533 	resp_hdr->max_cmdsn = cpu_to_be32(text->max_cmd_sn);
1534 	pld_len = text->data_length;
1535 	bnx2i_conn->gen_pdu.resp_wr_ptr = bnx2i_conn->gen_pdu.resp_buf +
1536 					  pld_len;
1537 	pad_len = 0;
1538 	if (pld_len & 0x3)
1539 		pad_len = 4 - (pld_len % 4);
1540 
1541 	if (pad_len) {
1542 		int i = 0;
1543 		for (i = 0; i < pad_len; i++) {
1544 			bnx2i_conn->gen_pdu.resp_wr_ptr[0] = 0;
1545 			bnx2i_conn->gen_pdu.resp_wr_ptr++;
1546 		}
1547 	}
1548 	__iscsi_complete_pdu(conn, (struct iscsi_hdr *)resp_hdr,
1549 			     bnx2i_conn->gen_pdu.resp_buf,
1550 			     bnx2i_conn->gen_pdu.resp_wr_ptr -
1551 			     bnx2i_conn->gen_pdu.resp_buf);
1552 done:
1553 	spin_unlock(&session->back_lock);
1554 	return 0;
1555 }
1556 
1557 
1558 /**
1559  * bnx2i_process_tmf_resp - this function handles iscsi TMF response
1560  * @session:		iscsi session pointer
1561  * @bnx2i_conn:		iscsi connection pointer
1562  * @cqe:		pointer to newly DMA'ed CQE entry for processing
1563  *
1564  * process iSCSI TMF Response CQE and wake up the driver eh thread.
1565  */
1566 static int bnx2i_process_tmf_resp(struct iscsi_session *session,
1567 				  struct bnx2i_conn *bnx2i_conn,
1568 				  struct cqe *cqe)
1569 {
1570 	struct iscsi_conn *conn = bnx2i_conn->cls_conn->dd_data;
1571 	struct iscsi_task *task;
1572 	struct bnx2i_tmf_response *tmf_cqe;
1573 	struct iscsi_tm_rsp *resp_hdr;
1574 
1575 	tmf_cqe = (struct bnx2i_tmf_response *)cqe;
1576 	spin_lock(&session->back_lock);
1577 	task = iscsi_itt_to_task(conn,
1578 				 tmf_cqe->itt & ISCSI_TMF_RESPONSE_INDEX);
1579 	if (!task)
1580 		goto done;
1581 
1582 	resp_hdr = (struct iscsi_tm_rsp *) &bnx2i_conn->gen_pdu.resp_hdr;
1583 	memset(resp_hdr, 0, sizeof(struct iscsi_hdr));
1584 	resp_hdr->opcode = tmf_cqe->op_code;
1585 	resp_hdr->max_cmdsn = cpu_to_be32(tmf_cqe->max_cmd_sn);
1586 	resp_hdr->exp_cmdsn = cpu_to_be32(tmf_cqe->exp_cmd_sn);
1587 	resp_hdr->itt = task->hdr->itt;
1588 	resp_hdr->response = tmf_cqe->response;
1589 
1590 	__iscsi_complete_pdu(conn, (struct iscsi_hdr *)resp_hdr, NULL, 0);
1591 done:
1592 	spin_unlock(&session->back_lock);
1593 	return 0;
1594 }
1595 
1596 /**
1597  * bnx2i_process_logout_resp - this function handles iscsi logout response
1598  * @session:		iscsi session pointer
1599  * @bnx2i_conn:		iscsi connection pointer
1600  * @cqe:		pointer to newly DMA'ed CQE entry for processing
1601  *
1602  * process iSCSI Logout Response CQE & make function call to
1603  * notify the user daemon.
1604  */
1605 static int bnx2i_process_logout_resp(struct iscsi_session *session,
1606 				     struct bnx2i_conn *bnx2i_conn,
1607 				     struct cqe *cqe)
1608 {
1609 	struct iscsi_conn *conn = bnx2i_conn->cls_conn->dd_data;
1610 	struct iscsi_task *task;
1611 	struct bnx2i_logout_response *logout;
1612 	struct iscsi_logout_rsp *resp_hdr;
1613 
1614 	logout = (struct bnx2i_logout_response *) cqe;
1615 	spin_lock(&session->back_lock);
1616 	task = iscsi_itt_to_task(conn,
1617 				 logout->itt & ISCSI_LOGOUT_RESPONSE_INDEX);
1618 	if (!task)
1619 		goto done;
1620 
1621 	resp_hdr = (struct iscsi_logout_rsp *) &bnx2i_conn->gen_pdu.resp_hdr;
1622 	memset(resp_hdr, 0, sizeof(struct iscsi_hdr));
1623 	resp_hdr->opcode = logout->op_code;
1624 	resp_hdr->flags = logout->response;
1625 	resp_hdr->hlength = 0;
1626 
1627 	resp_hdr->itt = task->hdr->itt;
1628 	resp_hdr->statsn = task->hdr->exp_statsn;
1629 	resp_hdr->exp_cmdsn = cpu_to_be32(logout->exp_cmd_sn);
1630 	resp_hdr->max_cmdsn = cpu_to_be32(logout->max_cmd_sn);
1631 
1632 	resp_hdr->t2wait = cpu_to_be32(logout->time_to_wait);
1633 	resp_hdr->t2retain = cpu_to_be32(logout->time_to_retain);
1634 
1635 	__iscsi_complete_pdu(conn, (struct iscsi_hdr *)resp_hdr, NULL, 0);
1636 
1637 	bnx2i_conn->ep->state = EP_STATE_LOGOUT_RESP_RCVD;
1638 done:
1639 	spin_unlock(&session->back_lock);
1640 	return 0;
1641 }
1642 
1643 /**
1644  * bnx2i_process_nopin_local_cmpl - this function handles iscsi nopin CQE
1645  * @session:		iscsi session pointer
1646  * @bnx2i_conn:		iscsi connection pointer
1647  * @cqe:		pointer to newly DMA'ed CQE entry for processing
1648  *
1649  * process iSCSI NOPIN local completion CQE, frees IIT and command structures
1650  */
1651 static void bnx2i_process_nopin_local_cmpl(struct iscsi_session *session,
1652 					   struct bnx2i_conn *bnx2i_conn,
1653 					   struct cqe *cqe)
1654 {
1655 	struct iscsi_conn *conn = bnx2i_conn->cls_conn->dd_data;
1656 	struct bnx2i_nop_in_msg *nop_in;
1657 	struct iscsi_task *task;
1658 
1659 	nop_in = (struct bnx2i_nop_in_msg *)cqe;
1660 	spin_lock(&session->back_lock);
1661 	task = iscsi_itt_to_task(conn,
1662 				 nop_in->itt & ISCSI_NOP_IN_MSG_INDEX);
1663 	if (task)
1664 		__iscsi_put_task(task);
1665 	spin_unlock(&session->back_lock);
1666 }
1667 
1668 /**
1669  * bnx2i_unsol_pdu_adjust_rq - makes adjustments to RQ after unsol pdu is recvd
1670  * @conn:	iscsi connection
1671  *
1672  * Firmware advances RQ producer index for every unsolicited PDU even if
1673  *	payload data length is '0'. This function makes corresponding
1674  *	adjustments on the driver side to match this f/w behavior
1675  */
1676 static void bnx2i_unsol_pdu_adjust_rq(struct bnx2i_conn *bnx2i_conn)
1677 {
1678 	char dummy_rq_data[2];
1679 	bnx2i_get_rq_buf(bnx2i_conn, dummy_rq_data, 1);
1680 	bnx2i_put_rq_buf(bnx2i_conn, 1);
1681 }
1682 
1683 
1684 /**
1685  * bnx2i_process_nopin_mesg - this function handles iscsi nopin CQE
1686  * @session:		iscsi session pointer
1687  * @bnx2i_conn:		iscsi connection pointer
1688  * @cqe:		pointer to newly DMA'ed CQE entry for processing
1689  *
1690  * process iSCSI target's proactive iSCSI NOPIN request
1691  */
1692 static int bnx2i_process_nopin_mesg(struct iscsi_session *session,
1693 				     struct bnx2i_conn *bnx2i_conn,
1694 				     struct cqe *cqe)
1695 {
1696 	struct iscsi_conn *conn = bnx2i_conn->cls_conn->dd_data;
1697 	struct iscsi_task *task;
1698 	struct bnx2i_nop_in_msg *nop_in;
1699 	struct iscsi_nopin *hdr;
1700 	int tgt_async_nop = 0;
1701 
1702 	nop_in = (struct bnx2i_nop_in_msg *)cqe;
1703 
1704 	spin_lock(&session->back_lock);
1705 	hdr = (struct iscsi_nopin *)&bnx2i_conn->gen_pdu.resp_hdr;
1706 	memset(hdr, 0, sizeof(struct iscsi_hdr));
1707 	hdr->opcode = nop_in->op_code;
1708 	hdr->max_cmdsn = cpu_to_be32(nop_in->max_cmd_sn);
1709 	hdr->exp_cmdsn = cpu_to_be32(nop_in->exp_cmd_sn);
1710 	hdr->ttt = cpu_to_be32(nop_in->ttt);
1711 
1712 	if (nop_in->itt == (u16) RESERVED_ITT) {
1713 		bnx2i_unsol_pdu_adjust_rq(bnx2i_conn);
1714 		hdr->itt = RESERVED_ITT;
1715 		tgt_async_nop = 1;
1716 		goto done;
1717 	}
1718 
1719 	/* this is a response to one of our nop-outs */
1720 	task = iscsi_itt_to_task(conn,
1721 			 (itt_t) (nop_in->itt & ISCSI_NOP_IN_MSG_INDEX));
1722 	if (task) {
1723 		hdr->flags = ISCSI_FLAG_CMD_FINAL;
1724 		hdr->itt = task->hdr->itt;
1725 		hdr->ttt = cpu_to_be32(nop_in->ttt);
1726 		memcpy(&hdr->lun, nop_in->lun, 8);
1727 	}
1728 done:
1729 	__iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1730 	spin_unlock(&session->back_lock);
1731 
1732 	return tgt_async_nop;
1733 }
1734 
1735 
1736 /**
1737  * bnx2i_process_async_mesg - this function handles iscsi async message
1738  * @session:		iscsi session pointer
1739  * @bnx2i_conn:		iscsi connection pointer
1740  * @cqe:		pointer to newly DMA'ed CQE entry for processing
1741  *
1742  * process iSCSI ASYNC Message
1743  */
1744 static void bnx2i_process_async_mesg(struct iscsi_session *session,
1745 				     struct bnx2i_conn *bnx2i_conn,
1746 				     struct cqe *cqe)
1747 {
1748 	struct bnx2i_async_msg *async_cqe;
1749 	struct iscsi_async *resp_hdr;
1750 	u8 async_event;
1751 
1752 	bnx2i_unsol_pdu_adjust_rq(bnx2i_conn);
1753 
1754 	async_cqe = (struct bnx2i_async_msg *)cqe;
1755 	async_event = async_cqe->async_event;
1756 
1757 	if (async_event == ISCSI_ASYNC_MSG_SCSI_EVENT) {
1758 		iscsi_conn_printk(KERN_ALERT, bnx2i_conn->cls_conn->dd_data,
1759 				  "async: scsi events not supported\n");
1760 		return;
1761 	}
1762 
1763 	spin_lock(&session->back_lock);
1764 	resp_hdr = (struct iscsi_async *) &bnx2i_conn->gen_pdu.resp_hdr;
1765 	memset(resp_hdr, 0, sizeof(struct iscsi_hdr));
1766 	resp_hdr->opcode = async_cqe->op_code;
1767 	resp_hdr->flags = 0x80;
1768 
1769 	memcpy(&resp_hdr->lun, async_cqe->lun, 8);
1770 	resp_hdr->exp_cmdsn = cpu_to_be32(async_cqe->exp_cmd_sn);
1771 	resp_hdr->max_cmdsn = cpu_to_be32(async_cqe->max_cmd_sn);
1772 
1773 	resp_hdr->async_event = async_cqe->async_event;
1774 	resp_hdr->async_vcode = async_cqe->async_vcode;
1775 
1776 	resp_hdr->param1 = cpu_to_be16(async_cqe->param1);
1777 	resp_hdr->param2 = cpu_to_be16(async_cqe->param2);
1778 	resp_hdr->param3 = cpu_to_be16(async_cqe->param3);
1779 
1780 	__iscsi_complete_pdu(bnx2i_conn->cls_conn->dd_data,
1781 			     (struct iscsi_hdr *)resp_hdr, NULL, 0);
1782 	spin_unlock(&session->back_lock);
1783 }
1784 
1785 
1786 /**
1787  * bnx2i_process_reject_mesg - process iscsi reject pdu
1788  * @session:		iscsi session pointer
1789  * @bnx2i_conn:		iscsi connection pointer
1790  * @cqe:		pointer to newly DMA'ed CQE entry for processing
1791  *
1792  * process iSCSI REJECT message
1793  */
1794 static void bnx2i_process_reject_mesg(struct iscsi_session *session,
1795 				      struct bnx2i_conn *bnx2i_conn,
1796 				      struct cqe *cqe)
1797 {
1798 	struct iscsi_conn *conn = bnx2i_conn->cls_conn->dd_data;
1799 	struct bnx2i_reject_msg *reject;
1800 	struct iscsi_reject *hdr;
1801 
1802 	reject = (struct bnx2i_reject_msg *) cqe;
1803 	if (reject->data_length) {
1804 		bnx2i_get_rq_buf(bnx2i_conn, conn->data, reject->data_length);
1805 		bnx2i_put_rq_buf(bnx2i_conn, 1);
1806 	} else
1807 		bnx2i_unsol_pdu_adjust_rq(bnx2i_conn);
1808 
1809 	spin_lock(&session->back_lock);
1810 	hdr = (struct iscsi_reject *) &bnx2i_conn->gen_pdu.resp_hdr;
1811 	memset(hdr, 0, sizeof(struct iscsi_hdr));
1812 	hdr->opcode = reject->op_code;
1813 	hdr->reason = reject->reason;
1814 	hton24(hdr->dlength, reject->data_length);
1815 	hdr->max_cmdsn = cpu_to_be32(reject->max_cmd_sn);
1816 	hdr->exp_cmdsn = cpu_to_be32(reject->exp_cmd_sn);
1817 	hdr->ffffffff = cpu_to_be32(RESERVED_ITT);
1818 	__iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, conn->data,
1819 			     reject->data_length);
1820 	spin_unlock(&session->back_lock);
1821 }
1822 
1823 /**
1824  * bnx2i_process_cmd_cleanup_resp - process scsi command clean-up completion
1825  * @session:		iscsi session pointer
1826  * @bnx2i_conn:		iscsi connection pointer
1827  * @cqe:		pointer to newly DMA'ed CQE entry for processing
1828  *
1829  * process command cleanup response CQE during conn shutdown or error recovery
1830  */
1831 static void bnx2i_process_cmd_cleanup_resp(struct iscsi_session *session,
1832 					   struct bnx2i_conn *bnx2i_conn,
1833 					   struct cqe *cqe)
1834 {
1835 	struct bnx2i_cleanup_response *cmd_clean_rsp;
1836 	struct iscsi_conn *conn = bnx2i_conn->cls_conn->dd_data;
1837 	struct iscsi_task *task;
1838 
1839 	cmd_clean_rsp = (struct bnx2i_cleanup_response *)cqe;
1840 	spin_lock(&session->back_lock);
1841 	task = iscsi_itt_to_task(conn,
1842 			cmd_clean_rsp->itt & ISCSI_CLEANUP_RESPONSE_INDEX);
1843 	if (!task)
1844 		printk(KERN_ALERT "bnx2i: cmd clean ITT %x not active\n",
1845 			cmd_clean_rsp->itt & ISCSI_CLEANUP_RESPONSE_INDEX);
1846 	spin_unlock(&session->back_lock);
1847 	complete(&bnx2i_conn->cmd_cleanup_cmpl);
1848 }
1849 
1850 
1851 /**
1852  * bnx2i_percpu_io_thread - thread per cpu for ios
1853  *
1854  * @arg:	ptr to bnx2i_percpu_info structure
1855  */
1856 int bnx2i_percpu_io_thread(void *arg)
1857 {
1858 	struct bnx2i_percpu_s *p = arg;
1859 	struct bnx2i_work *work, *tmp;
1860 	LIST_HEAD(work_list);
1861 
1862 	set_user_nice(current, MIN_NICE);
1863 
1864 	while (!kthread_should_stop()) {
1865 		spin_lock_bh(&p->p_work_lock);
1866 		while (!list_empty(&p->work_list)) {
1867 			list_splice_init(&p->work_list, &work_list);
1868 			spin_unlock_bh(&p->p_work_lock);
1869 
1870 			list_for_each_entry_safe(work, tmp, &work_list, list) {
1871 				list_del_init(&work->list);
1872 				/* work allocated in the bh, freed here */
1873 				bnx2i_process_scsi_cmd_resp(work->session,
1874 							    work->bnx2i_conn,
1875 							    &work->cqe);
1876 				atomic_dec(&work->bnx2i_conn->work_cnt);
1877 				kfree(work);
1878 			}
1879 			spin_lock_bh(&p->p_work_lock);
1880 		}
1881 		set_current_state(TASK_INTERRUPTIBLE);
1882 		spin_unlock_bh(&p->p_work_lock);
1883 		schedule();
1884 	}
1885 	__set_current_state(TASK_RUNNING);
1886 
1887 	return 0;
1888 }
1889 
1890 
1891 /**
1892  * bnx2i_queue_scsi_cmd_resp - queue cmd completion to the percpu thread
1893  * @bnx2i_conn:		bnx2i connection
1894  *
1895  * this function is called by generic KCQ handler to queue all pending cmd
1896  * completion CQEs
1897  *
1898  * The implementation is to queue the cmd response based on the
1899  * last recorded command for the given connection.  The
1900  * cpu_id gets recorded upon task_xmit.  No out-of-order completion!
1901  */
1902 static int bnx2i_queue_scsi_cmd_resp(struct iscsi_session *session,
1903 				     struct bnx2i_conn *bnx2i_conn,
1904 				     struct bnx2i_nop_in_msg *cqe)
1905 {
1906 	struct bnx2i_work *bnx2i_work = NULL;
1907 	struct bnx2i_percpu_s *p = NULL;
1908 	struct iscsi_task *task;
1909 	struct scsi_cmnd *sc;
1910 	int rc = 0;
1911 	int cpu;
1912 
1913 	spin_lock(&session->back_lock);
1914 	task = iscsi_itt_to_task(bnx2i_conn->cls_conn->dd_data,
1915 				 cqe->itt & ISCSI_CMD_RESPONSE_INDEX);
1916 	if (!task || !task->sc) {
1917 		spin_unlock(&session->back_lock);
1918 		return -EINVAL;
1919 	}
1920 	sc = task->sc;
1921 
1922 	if (!blk_rq_cpu_valid(sc->request))
1923 		cpu = smp_processor_id();
1924 	else
1925 		cpu = sc->request->cpu;
1926 
1927 	spin_unlock(&session->back_lock);
1928 
1929 	p = &per_cpu(bnx2i_percpu, cpu);
1930 	spin_lock(&p->p_work_lock);
1931 	if (unlikely(!p->iothread)) {
1932 		rc = -EINVAL;
1933 		goto err;
1934 	}
1935 	/* Alloc and copy to the cqe */
1936 	bnx2i_work = kzalloc(sizeof(struct bnx2i_work), GFP_ATOMIC);
1937 	if (bnx2i_work) {
1938 		INIT_LIST_HEAD(&bnx2i_work->list);
1939 		bnx2i_work->session = session;
1940 		bnx2i_work->bnx2i_conn = bnx2i_conn;
1941 		memcpy(&bnx2i_work->cqe, cqe, sizeof(struct cqe));
1942 		list_add_tail(&bnx2i_work->list, &p->work_list);
1943 		atomic_inc(&bnx2i_conn->work_cnt);
1944 		wake_up_process(p->iothread);
1945 		spin_unlock(&p->p_work_lock);
1946 		goto done;
1947 	} else
1948 		rc = -ENOMEM;
1949 err:
1950 	spin_unlock(&p->p_work_lock);
1951 	bnx2i_process_scsi_cmd_resp(session, bnx2i_conn, (struct cqe *)cqe);
1952 done:
1953 	return rc;
1954 }
1955 
1956 
1957 /**
1958  * bnx2i_process_new_cqes - process newly DMA'ed CQE's
1959  * @bnx2i_conn:		bnx2i connection
1960  *
1961  * this function is called by generic KCQ handler to process all pending CQE's
1962  */
1963 static int bnx2i_process_new_cqes(struct bnx2i_conn *bnx2i_conn)
1964 {
1965 	struct iscsi_conn *conn = bnx2i_conn->cls_conn->dd_data;
1966 	struct iscsi_session *session = conn->session;
1967 	struct bnx2i_hba *hba = bnx2i_conn->hba;
1968 	struct qp_info *qp;
1969 	struct bnx2i_nop_in_msg *nopin;
1970 	int tgt_async_msg;
1971 	int cqe_cnt = 0;
1972 
1973 	if (bnx2i_conn->ep == NULL)
1974 		return 0;
1975 
1976 	qp = &bnx2i_conn->ep->qp;
1977 
1978 	if (!qp->cq_virt) {
1979 		printk(KERN_ALERT "bnx2i (%s): cq resr freed in bh execution!",
1980 		       hba->netdev->name);
1981 		goto out;
1982 	}
1983 	while (1) {
1984 		nopin = (struct bnx2i_nop_in_msg *) qp->cq_cons_qe;
1985 		if (nopin->cq_req_sn != qp->cqe_exp_seq_sn)
1986 			break;
1987 
1988 		if (unlikely(test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx))) {
1989 			if (nopin->op_code == ISCSI_OP_NOOP_IN &&
1990 			    nopin->itt == (u16) RESERVED_ITT) {
1991 				printk(KERN_ALERT "bnx2i: Unsolicited "
1992 				       "NOP-In detected for suspended "
1993 				       "connection dev=%s!\n",
1994 				       hba->netdev->name);
1995 				bnx2i_unsol_pdu_adjust_rq(bnx2i_conn);
1996 				goto cqe_out;
1997 			}
1998 			break;
1999 		}
2000 		tgt_async_msg = 0;
2001 
2002 		switch (nopin->op_code) {
2003 		case ISCSI_OP_SCSI_CMD_RSP:
2004 		case ISCSI_OP_SCSI_DATA_IN:
2005 			/* Run the kthread engine only for data cmds
2006 			   All other cmds will be completed in this bh! */
2007 			bnx2i_queue_scsi_cmd_resp(session, bnx2i_conn, nopin);
2008 			goto done;
2009 		case ISCSI_OP_LOGIN_RSP:
2010 			bnx2i_process_login_resp(session, bnx2i_conn,
2011 						 qp->cq_cons_qe);
2012 			break;
2013 		case ISCSI_OP_SCSI_TMFUNC_RSP:
2014 			bnx2i_process_tmf_resp(session, bnx2i_conn,
2015 					       qp->cq_cons_qe);
2016 			break;
2017 		case ISCSI_OP_TEXT_RSP:
2018 			bnx2i_process_text_resp(session, bnx2i_conn,
2019 						qp->cq_cons_qe);
2020 			break;
2021 		case ISCSI_OP_LOGOUT_RSP:
2022 			bnx2i_process_logout_resp(session, bnx2i_conn,
2023 						  qp->cq_cons_qe);
2024 			break;
2025 		case ISCSI_OP_NOOP_IN:
2026 			if (bnx2i_process_nopin_mesg(session, bnx2i_conn,
2027 						     qp->cq_cons_qe))
2028 				tgt_async_msg = 1;
2029 			break;
2030 		case ISCSI_OPCODE_NOPOUT_LOCAL_COMPLETION:
2031 			bnx2i_process_nopin_local_cmpl(session, bnx2i_conn,
2032 						       qp->cq_cons_qe);
2033 			break;
2034 		case ISCSI_OP_ASYNC_EVENT:
2035 			bnx2i_process_async_mesg(session, bnx2i_conn,
2036 						 qp->cq_cons_qe);
2037 			tgt_async_msg = 1;
2038 			break;
2039 		case ISCSI_OP_REJECT:
2040 			bnx2i_process_reject_mesg(session, bnx2i_conn,
2041 						  qp->cq_cons_qe);
2042 			break;
2043 		case ISCSI_OPCODE_CLEANUP_RESPONSE:
2044 			bnx2i_process_cmd_cleanup_resp(session, bnx2i_conn,
2045 						       qp->cq_cons_qe);
2046 			break;
2047 		default:
2048 			printk(KERN_ALERT "bnx2i: unknown opcode 0x%x\n",
2049 					  nopin->op_code);
2050 		}
2051 
2052 		ADD_STATS_64(hba, rx_pdus, 1);
2053 		ADD_STATS_64(hba, rx_bytes, nopin->data_length);
2054 done:
2055 		if (!tgt_async_msg) {
2056 			if (!atomic_read(&bnx2i_conn->ep->num_active_cmds))
2057 				printk(KERN_ALERT "bnx2i (%s): no active cmd! "
2058 				       "op 0x%x\n",
2059 				       hba->netdev->name,
2060 				       nopin->op_code);
2061 			else
2062 				atomic_dec(&bnx2i_conn->ep->num_active_cmds);
2063 		}
2064 cqe_out:
2065 		/* clear out in production version only, till beta keep opcode
2066 		 * field intact, will be helpful in debugging (context dump)
2067 		 * nopin->op_code = 0;
2068 		 */
2069 		cqe_cnt++;
2070 		qp->cqe_exp_seq_sn++;
2071 		if (qp->cqe_exp_seq_sn == (qp->cqe_size * 2 + 1))
2072 			qp->cqe_exp_seq_sn = ISCSI_INITIAL_SN;
2073 
2074 		if (qp->cq_cons_qe == qp->cq_last_qe) {
2075 			qp->cq_cons_qe = qp->cq_first_qe;
2076 			qp->cq_cons_idx = 0;
2077 		} else {
2078 			qp->cq_cons_qe++;
2079 			qp->cq_cons_idx++;
2080 		}
2081 	}
2082 out:
2083 	return cqe_cnt;
2084 }
2085 
2086 /**
2087  * bnx2i_fastpath_notification - process global event queue (KCQ)
2088  * @hba:		adapter structure pointer
2089  * @new_cqe_kcqe:	pointer to newly DMA'ed KCQE entry
2090  *
2091  * Fast path event notification handler, KCQ entry carries context id
2092  *	of the connection that has 1 or more pending CQ entries
2093  */
2094 static void bnx2i_fastpath_notification(struct bnx2i_hba *hba,
2095 					struct iscsi_kcqe *new_cqe_kcqe)
2096 {
2097 	struct bnx2i_conn *bnx2i_conn;
2098 	u32 iscsi_cid;
2099 	int nxt_idx;
2100 
2101 	iscsi_cid = new_cqe_kcqe->iscsi_conn_id;
2102 	bnx2i_conn = bnx2i_get_conn_from_id(hba, iscsi_cid);
2103 
2104 	if (!bnx2i_conn) {
2105 		printk(KERN_ALERT "cid #%x not valid\n", iscsi_cid);
2106 		return;
2107 	}
2108 	if (!bnx2i_conn->ep) {
2109 		printk(KERN_ALERT "cid #%x - ep not bound\n", iscsi_cid);
2110 		return;
2111 	}
2112 
2113 	bnx2i_process_new_cqes(bnx2i_conn);
2114 	nxt_idx = bnx2i_arm_cq_event_coalescing(bnx2i_conn->ep,
2115 						CNIC_ARM_CQE_FP);
2116 	if (nxt_idx && nxt_idx == bnx2i_process_new_cqes(bnx2i_conn))
2117 		bnx2i_arm_cq_event_coalescing(bnx2i_conn->ep, CNIC_ARM_CQE_FP);
2118 }
2119 
2120 
2121 /**
2122  * bnx2i_process_update_conn_cmpl - process iscsi conn update completion KCQE
2123  * @hba:		adapter structure pointer
2124  * @update_kcqe:	kcqe pointer
2125  *
2126  * CONN_UPDATE completion handler, this completes iSCSI connection FFP migration
2127  */
2128 static void bnx2i_process_update_conn_cmpl(struct bnx2i_hba *hba,
2129 					   struct iscsi_kcqe *update_kcqe)
2130 {
2131 	struct bnx2i_conn *conn;
2132 	u32 iscsi_cid;
2133 
2134 	iscsi_cid = update_kcqe->iscsi_conn_id;
2135 	conn = bnx2i_get_conn_from_id(hba, iscsi_cid);
2136 
2137 	if (!conn) {
2138 		printk(KERN_ALERT "conn_update: cid %x not valid\n", iscsi_cid);
2139 		return;
2140 	}
2141 	if (!conn->ep) {
2142 		printk(KERN_ALERT "cid %x does not have ep bound\n", iscsi_cid);
2143 		return;
2144 	}
2145 
2146 	if (update_kcqe->completion_status) {
2147 		printk(KERN_ALERT "request failed cid %x\n", iscsi_cid);
2148 		conn->ep->state = EP_STATE_ULP_UPDATE_FAILED;
2149 	} else
2150 		conn->ep->state = EP_STATE_ULP_UPDATE_COMPL;
2151 
2152 	wake_up_interruptible(&conn->ep->ofld_wait);
2153 }
2154 
2155 
2156 /**
2157  * bnx2i_recovery_que_add_conn - add connection to recovery queue
2158  * @hba:		adapter structure pointer
2159  * @bnx2i_conn:		iscsi connection
2160  *
2161  * Add connection to recovery queue and schedule adapter eh worker
2162  */
2163 static void bnx2i_recovery_que_add_conn(struct bnx2i_hba *hba,
2164 					struct bnx2i_conn *bnx2i_conn)
2165 {
2166 	iscsi_conn_failure(bnx2i_conn->cls_conn->dd_data,
2167 			   ISCSI_ERR_CONN_FAILED);
2168 }
2169 
2170 
2171 /**
2172  * bnx2i_process_tcp_error - process error notification on a given connection
2173  *
2174  * @hba: 		adapter structure pointer
2175  * @tcp_err: 		tcp error kcqe pointer
2176  *
2177  * handles tcp level error notifications from FW.
2178  */
2179 static void bnx2i_process_tcp_error(struct bnx2i_hba *hba,
2180 				    struct iscsi_kcqe *tcp_err)
2181 {
2182 	struct bnx2i_conn *bnx2i_conn;
2183 	u32 iscsi_cid;
2184 
2185 	iscsi_cid = tcp_err->iscsi_conn_id;
2186 	bnx2i_conn = bnx2i_get_conn_from_id(hba, iscsi_cid);
2187 
2188 	if (!bnx2i_conn) {
2189 		printk(KERN_ALERT "bnx2i - cid 0x%x not valid\n", iscsi_cid);
2190 		return;
2191 	}
2192 
2193 	printk(KERN_ALERT "bnx2i - cid 0x%x had TCP errors, error code 0x%x\n",
2194 			  iscsi_cid, tcp_err->completion_status);
2195 	bnx2i_recovery_que_add_conn(bnx2i_conn->hba, bnx2i_conn);
2196 }
2197 
2198 
2199 /**
2200  * bnx2i_process_iscsi_error - process error notification on a given connection
2201  * @hba:		adapter structure pointer
2202  * @iscsi_err:		iscsi error kcqe pointer
2203  *
2204  * handles iscsi error notifications from the FW. Firmware based in initial
2205  *	handshake classifies iscsi protocol / TCP rfc violation into either
2206  *	warning or error indications. If indication is of "Error" type, driver
2207  *	will initiate session recovery for that connection/session. For
2208  *	"Warning" type indication, driver will put out a system log message
2209  *	(there will be only one message for each type for the life of the
2210  *	session, this is to avoid un-necessarily overloading the system)
2211  */
2212 static void bnx2i_process_iscsi_error(struct bnx2i_hba *hba,
2213 				      struct iscsi_kcqe *iscsi_err)
2214 {
2215 	struct bnx2i_conn *bnx2i_conn;
2216 	u32 iscsi_cid;
2217 	char warn_notice[] = "iscsi_warning";
2218 	char error_notice[] = "iscsi_error";
2219 	char additional_notice[64];
2220 	char *message;
2221 	int need_recovery;
2222 	u64 err_mask64;
2223 
2224 	iscsi_cid = iscsi_err->iscsi_conn_id;
2225 	bnx2i_conn = bnx2i_get_conn_from_id(hba, iscsi_cid);
2226 	if (!bnx2i_conn) {
2227 		printk(KERN_ALERT "bnx2i - cid 0x%x not valid\n", iscsi_cid);
2228 		return;
2229 	}
2230 
2231 	err_mask64 = (0x1ULL << iscsi_err->completion_status);
2232 
2233 	if (err_mask64 & iscsi_error_mask) {
2234 		need_recovery = 0;
2235 		message = warn_notice;
2236 	} else {
2237 		need_recovery = 1;
2238 		message = error_notice;
2239 	}
2240 
2241 	switch (iscsi_err->completion_status) {
2242 	case ISCSI_KCQE_COMPLETION_STATUS_HDR_DIG_ERR:
2243 		strcpy(additional_notice, "hdr digest err");
2244 		break;
2245 	case ISCSI_KCQE_COMPLETION_STATUS_DATA_DIG_ERR:
2246 		strcpy(additional_notice, "data digest err");
2247 		break;
2248 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_OPCODE:
2249 		strcpy(additional_notice, "wrong opcode rcvd");
2250 		break;
2251 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_AHS_LEN:
2252 		strcpy(additional_notice, "AHS len > 0 rcvd");
2253 		break;
2254 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_ITT:
2255 		strcpy(additional_notice, "invalid ITT rcvd");
2256 		break;
2257 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_STATSN:
2258 		strcpy(additional_notice, "wrong StatSN rcvd");
2259 		break;
2260 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_EXP_DATASN:
2261 		strcpy(additional_notice, "wrong DataSN rcvd");
2262 		break;
2263 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_PEND_R2T:
2264 		strcpy(additional_notice, "pend R2T violation");
2265 		break;
2266 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_O_U_0:
2267 		strcpy(additional_notice, "ERL0, UO");
2268 		break;
2269 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_O_U_1:
2270 		strcpy(additional_notice, "ERL0, U1");
2271 		break;
2272 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_O_U_2:
2273 		strcpy(additional_notice, "ERL0, U2");
2274 		break;
2275 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_O_U_3:
2276 		strcpy(additional_notice, "ERL0, U3");
2277 		break;
2278 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_O_U_4:
2279 		strcpy(additional_notice, "ERL0, U4");
2280 		break;
2281 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_O_U_5:
2282 		strcpy(additional_notice, "ERL0, U5");
2283 		break;
2284 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_O_U_6:
2285 		strcpy(additional_notice, "ERL0, U6");
2286 		break;
2287 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_REMAIN_RCV_LEN:
2288 		strcpy(additional_notice, "invalid resi len");
2289 		break;
2290 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_MAX_RCV_PDU_LEN:
2291 		strcpy(additional_notice, "MRDSL violation");
2292 		break;
2293 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_F_BIT_ZERO:
2294 		strcpy(additional_notice, "F-bit not set");
2295 		break;
2296 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_TTT_NOT_RSRV:
2297 		strcpy(additional_notice, "invalid TTT");
2298 		break;
2299 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_DATASN:
2300 		strcpy(additional_notice, "invalid DataSN");
2301 		break;
2302 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_REMAIN_BURST_LEN:
2303 		strcpy(additional_notice, "burst len violation");
2304 		break;
2305 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_BUFFER_OFF:
2306 		strcpy(additional_notice, "buf offset violation");
2307 		break;
2308 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_LUN:
2309 		strcpy(additional_notice, "invalid LUN field");
2310 		break;
2311 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_R2TSN:
2312 		strcpy(additional_notice, "invalid R2TSN field");
2313 		break;
2314 #define BNX2I_ERR_DESIRED_DATA_TRNS_LEN_0 	\
2315 	ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_DESIRED_DATA_TRNS_LEN_0
2316 	case BNX2I_ERR_DESIRED_DATA_TRNS_LEN_0:
2317 		strcpy(additional_notice, "invalid cmd len1");
2318 		break;
2319 #define BNX2I_ERR_DESIRED_DATA_TRNS_LEN_1 	\
2320 	ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_DESIRED_DATA_TRNS_LEN_1
2321 	case BNX2I_ERR_DESIRED_DATA_TRNS_LEN_1:
2322 		strcpy(additional_notice, "invalid cmd len2");
2323 		break;
2324 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_PEND_R2T_EXCEED:
2325 		strcpy(additional_notice,
2326 		       "pend r2t exceeds MaxOutstandingR2T value");
2327 		break;
2328 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_TTT_IS_RSRV:
2329 		strcpy(additional_notice, "TTT is rsvd");
2330 		break;
2331 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_MAX_BURST_LEN:
2332 		strcpy(additional_notice, "MBL violation");
2333 		break;
2334 #define BNX2I_ERR_DATA_SEG_LEN_NOT_ZERO 	\
2335 	ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_DATA_SEG_LEN_NOT_ZERO
2336 	case BNX2I_ERR_DATA_SEG_LEN_NOT_ZERO:
2337 		strcpy(additional_notice, "data seg len != 0");
2338 		break;
2339 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_REJECT_PDU_LEN:
2340 		strcpy(additional_notice, "reject pdu len error");
2341 		break;
2342 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_ASYNC_PDU_LEN:
2343 		strcpy(additional_notice, "async pdu len error");
2344 		break;
2345 	case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_NOPIN_PDU_LEN:
2346 		strcpy(additional_notice, "nopin pdu len error");
2347 		break;
2348 #define BNX2_ERR_PEND_R2T_IN_CLEANUP			\
2349 	ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_PEND_R2T_IN_CLEANUP
2350 	case BNX2_ERR_PEND_R2T_IN_CLEANUP:
2351 		strcpy(additional_notice, "pend r2t in cleanup");
2352 		break;
2353 
2354 	case ISCI_KCQE_COMPLETION_STATUS_TCP_ERROR_IP_FRAGMENT:
2355 		strcpy(additional_notice, "IP fragments rcvd");
2356 		break;
2357 	case ISCI_KCQE_COMPLETION_STATUS_TCP_ERROR_IP_OPTIONS:
2358 		strcpy(additional_notice, "IP options error");
2359 		break;
2360 	case ISCI_KCQE_COMPLETION_STATUS_TCP_ERROR_URGENT_FLAG:
2361 		strcpy(additional_notice, "urgent flag error");
2362 		break;
2363 	default:
2364 		printk(KERN_ALERT "iscsi_err - unknown err %x\n",
2365 				  iscsi_err->completion_status);
2366 	}
2367 
2368 	if (need_recovery) {
2369 		iscsi_conn_printk(KERN_ALERT,
2370 				  bnx2i_conn->cls_conn->dd_data,
2371 				  "bnx2i: %s - %s\n",
2372 				  message, additional_notice);
2373 
2374 		iscsi_conn_printk(KERN_ALERT,
2375 				  bnx2i_conn->cls_conn->dd_data,
2376 				  "conn_err - hostno %d conn %p, "
2377 				  "iscsi_cid %x cid %x\n",
2378 				  bnx2i_conn->hba->shost->host_no,
2379 				  bnx2i_conn, bnx2i_conn->ep->ep_iscsi_cid,
2380 				  bnx2i_conn->ep->ep_cid);
2381 		bnx2i_recovery_que_add_conn(bnx2i_conn->hba, bnx2i_conn);
2382 	} else
2383 		if (!test_and_set_bit(iscsi_err->completion_status,
2384 				      (void *) &bnx2i_conn->violation_notified))
2385 			iscsi_conn_printk(KERN_ALERT,
2386 					  bnx2i_conn->cls_conn->dd_data,
2387 					  "bnx2i: %s - %s\n",
2388 					  message, additional_notice);
2389 }
2390 
2391 
2392 /**
2393  * bnx2i_process_conn_destroy_cmpl - process iscsi conn destroy completion
2394  * @hba:		adapter structure pointer
2395  * @conn_destroy:	conn destroy kcqe pointer
2396  *
2397  * handles connection destroy completion request.
2398  */
2399 static void bnx2i_process_conn_destroy_cmpl(struct bnx2i_hba *hba,
2400 					    struct iscsi_kcqe *conn_destroy)
2401 {
2402 	struct bnx2i_endpoint *ep;
2403 
2404 	ep = bnx2i_find_ep_in_destroy_list(hba, conn_destroy->iscsi_conn_id);
2405 	if (!ep) {
2406 		printk(KERN_ALERT "bnx2i_conn_destroy_cmpl: no pending "
2407 				  "offload request, unexpected completion\n");
2408 		return;
2409 	}
2410 
2411 	if (hba != ep->hba) {
2412 		printk(KERN_ALERT "conn destroy- error hba mis-match\n");
2413 		return;
2414 	}
2415 
2416 	if (conn_destroy->completion_status) {
2417 		printk(KERN_ALERT "conn_destroy_cmpl: op failed\n");
2418 		ep->state = EP_STATE_CLEANUP_FAILED;
2419 	} else
2420 		ep->state = EP_STATE_CLEANUP_CMPL;
2421 	wake_up_interruptible(&ep->ofld_wait);
2422 }
2423 
2424 
2425 /**
2426  * bnx2i_process_ofld_cmpl - process initial iscsi conn offload completion
2427  * @hba:		adapter structure pointer
2428  * @ofld_kcqe:		conn offload kcqe pointer
2429  *
2430  * handles initial connection offload completion, ep_connect() thread is
2431  *	woken-up to continue with LLP connect process
2432  */
2433 static void bnx2i_process_ofld_cmpl(struct bnx2i_hba *hba,
2434 				    struct iscsi_kcqe *ofld_kcqe)
2435 {
2436 	u32 cid_addr;
2437 	struct bnx2i_endpoint *ep;
2438 	u32 cid_num;
2439 
2440 	ep = bnx2i_find_ep_in_ofld_list(hba, ofld_kcqe->iscsi_conn_id);
2441 	if (!ep) {
2442 		printk(KERN_ALERT "ofld_cmpl: no pend offload request\n");
2443 		return;
2444 	}
2445 
2446 	if (hba != ep->hba) {
2447 		printk(KERN_ALERT "ofld_cmpl: error hba mis-match\n");
2448 		return;
2449 	}
2450 
2451 	if (ofld_kcqe->completion_status) {
2452 		ep->state = EP_STATE_OFLD_FAILED;
2453 		if (ofld_kcqe->completion_status ==
2454 		    ISCSI_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE)
2455 			printk(KERN_ALERT "bnx2i (%s): ofld1 cmpl - unable "
2456 				"to allocate iSCSI context resources\n",
2457 				hba->netdev->name);
2458 		else if (ofld_kcqe->completion_status ==
2459 			 ISCSI_KCQE_COMPLETION_STATUS_INVALID_OPCODE)
2460 			printk(KERN_ALERT "bnx2i (%s): ofld1 cmpl - invalid "
2461 				"opcode\n", hba->netdev->name);
2462 		else if (ofld_kcqe->completion_status ==
2463 			 ISCSI_KCQE_COMPLETION_STATUS_CID_BUSY)
2464 			/* error status code valid only for 5771x chipset */
2465 			ep->state = EP_STATE_OFLD_FAILED_CID_BUSY;
2466 		else
2467 			printk(KERN_ALERT "bnx2i (%s): ofld1 cmpl - invalid "
2468 				"error code %d\n", hba->netdev->name,
2469 				ofld_kcqe->completion_status);
2470 	} else {
2471 		ep->state = EP_STATE_OFLD_COMPL;
2472 		cid_addr = ofld_kcqe->iscsi_conn_context_id;
2473 		cid_num = bnx2i_get_cid_num(ep);
2474 		ep->ep_cid = cid_addr;
2475 		ep->qp.ctx_base = NULL;
2476 	}
2477 	wake_up_interruptible(&ep->ofld_wait);
2478 }
2479 
2480 /**
2481  * bnx2i_indicate_kcqe - process iscsi conn update completion KCQE
2482  * @hba:		adapter structure pointer
2483  * @update_kcqe:	kcqe pointer
2484  *
2485  * Generic KCQ event handler/dispatcher
2486  */
2487 static void bnx2i_indicate_kcqe(void *context, struct kcqe *kcqe[],
2488 				u32 num_cqe)
2489 {
2490 	struct bnx2i_hba *hba = context;
2491 	int i = 0;
2492 	struct iscsi_kcqe *ikcqe = NULL;
2493 
2494 	while (i < num_cqe) {
2495 		ikcqe = (struct iscsi_kcqe *) kcqe[i++];
2496 
2497 		if (ikcqe->op_code ==
2498 		    ISCSI_KCQE_OPCODE_CQ_EVENT_NOTIFICATION)
2499 			bnx2i_fastpath_notification(hba, ikcqe);
2500 		else if (ikcqe->op_code == ISCSI_KCQE_OPCODE_OFFLOAD_CONN)
2501 			bnx2i_process_ofld_cmpl(hba, ikcqe);
2502 		else if (ikcqe->op_code == ISCSI_KCQE_OPCODE_UPDATE_CONN)
2503 			bnx2i_process_update_conn_cmpl(hba, ikcqe);
2504 		else if (ikcqe->op_code == ISCSI_KCQE_OPCODE_INIT) {
2505 			if (ikcqe->completion_status !=
2506 			    ISCSI_KCQE_COMPLETION_STATUS_SUCCESS)
2507 				bnx2i_iscsi_license_error(hba, ikcqe->\
2508 							  completion_status);
2509 			else {
2510 				set_bit(ADAPTER_STATE_UP, &hba->adapter_state);
2511 				bnx2i_get_link_state(hba);
2512 				printk(KERN_INFO "bnx2i [%.2x:%.2x.%.2x]: "
2513 						 "ISCSI_INIT passed\n",
2514 						 (u8)hba->pcidev->bus->number,
2515 						 hba->pci_devno,
2516 						 (u8)hba->pci_func);
2517 
2518 
2519 			}
2520 		} else if (ikcqe->op_code == ISCSI_KCQE_OPCODE_DESTROY_CONN)
2521 			bnx2i_process_conn_destroy_cmpl(hba, ikcqe);
2522 		else if (ikcqe->op_code == ISCSI_KCQE_OPCODE_ISCSI_ERROR)
2523 			bnx2i_process_iscsi_error(hba, ikcqe);
2524 		else if (ikcqe->op_code == ISCSI_KCQE_OPCODE_TCP_ERROR)
2525 			bnx2i_process_tcp_error(hba, ikcqe);
2526 		else
2527 			printk(KERN_ALERT "bnx2i: unknown opcode 0x%x\n",
2528 					  ikcqe->op_code);
2529 	}
2530 }
2531 
2532 
2533 /**
2534  * bnx2i_indicate_netevent - Generic netdev event handler
2535  * @context:	adapter structure pointer
2536  * @event:	event type
2537  * @vlan_id:	vlans id - associated vlan id with this event
2538  *
2539  * Handles four netdev events, NETDEV_UP, NETDEV_DOWN,
2540  *	NETDEV_GOING_DOWN and NETDEV_CHANGE
2541  */
2542 static void bnx2i_indicate_netevent(void *context, unsigned long event,
2543 				    u16 vlan_id)
2544 {
2545 	struct bnx2i_hba *hba = context;
2546 
2547 	/* Ignore all netevent coming from vlans */
2548 	if (vlan_id != 0)
2549 		return;
2550 
2551 	switch (event) {
2552 	case NETDEV_UP:
2553 		if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
2554 			bnx2i_send_fw_iscsi_init_msg(hba);
2555 		break;
2556 	case NETDEV_DOWN:
2557 		clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
2558 		clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
2559 		break;
2560 	case NETDEV_GOING_DOWN:
2561 		set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
2562 		iscsi_host_for_each_session(hba->shost,
2563 					    bnx2i_drop_session);
2564 		break;
2565 	case NETDEV_CHANGE:
2566 		bnx2i_get_link_state(hba);
2567 		break;
2568 	default:
2569 		;
2570 	}
2571 }
2572 
2573 
2574 /**
2575  * bnx2i_cm_connect_cmpl - process iscsi conn establishment completion
2576  * @cm_sk: 		cnic sock structure pointer
2577  *
2578  * function callback exported via bnx2i - cnic driver interface to
2579  *	indicate completion of option-2 TCP connect request.
2580  */
2581 static void bnx2i_cm_connect_cmpl(struct cnic_sock *cm_sk)
2582 {
2583 	struct bnx2i_endpoint *ep = (struct bnx2i_endpoint *) cm_sk->context;
2584 
2585 	if (test_bit(ADAPTER_STATE_GOING_DOWN, &ep->hba->adapter_state))
2586 		ep->state = EP_STATE_CONNECT_FAILED;
2587 	else if (test_bit(SK_F_OFFLD_COMPLETE, &cm_sk->flags))
2588 		ep->state = EP_STATE_CONNECT_COMPL;
2589 	else
2590 		ep->state = EP_STATE_CONNECT_FAILED;
2591 
2592 	wake_up_interruptible(&ep->ofld_wait);
2593 }
2594 
2595 
2596 /**
2597  * bnx2i_cm_close_cmpl - process tcp conn close completion
2598  * @cm_sk:	cnic sock structure pointer
2599  *
2600  * function callback exported via bnx2i - cnic driver interface to
2601  *	indicate completion of option-2 graceful TCP connect shutdown
2602  */
2603 static void bnx2i_cm_close_cmpl(struct cnic_sock *cm_sk)
2604 {
2605 	struct bnx2i_endpoint *ep = (struct bnx2i_endpoint *) cm_sk->context;
2606 
2607 	ep->state = EP_STATE_DISCONN_COMPL;
2608 	wake_up_interruptible(&ep->ofld_wait);
2609 }
2610 
2611 
2612 /**
2613  * bnx2i_cm_abort_cmpl - process abortive tcp conn teardown completion
2614  * @cm_sk:	cnic sock structure pointer
2615  *
2616  * function callback exported via bnx2i - cnic driver interface to
2617  *	indicate completion of option-2 abortive TCP connect termination
2618  */
2619 static void bnx2i_cm_abort_cmpl(struct cnic_sock *cm_sk)
2620 {
2621 	struct bnx2i_endpoint *ep = (struct bnx2i_endpoint *) cm_sk->context;
2622 
2623 	ep->state = EP_STATE_DISCONN_COMPL;
2624 	wake_up_interruptible(&ep->ofld_wait);
2625 }
2626 
2627 
2628 /**
2629  * bnx2i_cm_remote_close - process received TCP FIN
2630  * @hba:		adapter structure pointer
2631  * @update_kcqe:	kcqe pointer
2632  *
2633  * function callback exported via bnx2i - cnic driver interface to indicate
2634  *	async TCP events such as FIN
2635  */
2636 static void bnx2i_cm_remote_close(struct cnic_sock *cm_sk)
2637 {
2638 	struct bnx2i_endpoint *ep = (struct bnx2i_endpoint *) cm_sk->context;
2639 
2640 	ep->state = EP_STATE_TCP_FIN_RCVD;
2641 	if (ep->conn)
2642 		bnx2i_recovery_que_add_conn(ep->hba, ep->conn);
2643 }
2644 
2645 /**
2646  * bnx2i_cm_remote_abort - process TCP RST and start conn cleanup
2647  * @hba:		adapter structure pointer
2648  * @update_kcqe:	kcqe pointer
2649  *
2650  * function callback exported via bnx2i - cnic driver interface to
2651  *	indicate async TCP events (RST) sent by the peer.
2652  */
2653 static void bnx2i_cm_remote_abort(struct cnic_sock *cm_sk)
2654 {
2655 	struct bnx2i_endpoint *ep = (struct bnx2i_endpoint *) cm_sk->context;
2656 	u32 old_state = ep->state;
2657 
2658 	ep->state = EP_STATE_TCP_RST_RCVD;
2659 	if (old_state == EP_STATE_DISCONN_START)
2660 		wake_up_interruptible(&ep->ofld_wait);
2661 	else
2662 		if (ep->conn)
2663 			bnx2i_recovery_que_add_conn(ep->hba, ep->conn);
2664 }
2665 
2666 
2667 static int bnx2i_send_nl_mesg(void *context, u32 msg_type,
2668 			      char *buf, u16 buflen)
2669 {
2670 	struct bnx2i_hba *hba = context;
2671 	int rc;
2672 
2673 	if (!hba)
2674 		return -ENODEV;
2675 
2676 	rc = iscsi_offload_mesg(hba->shost, &bnx2i_iscsi_transport,
2677 				msg_type, buf, buflen);
2678 	if (rc)
2679 		printk(KERN_ALERT "bnx2i: private nl message send error\n");
2680 
2681 	return rc;
2682 }
2683 
2684 
2685 /**
2686  * bnx2i_cnic_cb - global template of bnx2i - cnic driver interface structure
2687  *			carrying callback function pointers
2688  *
2689  */
2690 struct cnic_ulp_ops bnx2i_cnic_cb = {
2691 	.cnic_init = bnx2i_ulp_init,
2692 	.cnic_exit = bnx2i_ulp_exit,
2693 	.cnic_start = bnx2i_start,
2694 	.cnic_stop = bnx2i_stop,
2695 	.indicate_kcqes = bnx2i_indicate_kcqe,
2696 	.indicate_netevent = bnx2i_indicate_netevent,
2697 	.cm_connect_complete = bnx2i_cm_connect_cmpl,
2698 	.cm_close_complete = bnx2i_cm_close_cmpl,
2699 	.cm_abort_complete = bnx2i_cm_abort_cmpl,
2700 	.cm_remote_close = bnx2i_cm_remote_close,
2701 	.cm_remote_abort = bnx2i_cm_remote_abort,
2702 	.iscsi_nl_send_msg = bnx2i_send_nl_mesg,
2703 	.cnic_get_stats = bnx2i_get_stats,
2704 	.owner = THIS_MODULE
2705 };
2706 
2707 
2708 /**
2709  * bnx2i_map_ep_dbell_regs - map connection doorbell registers
2710  * @ep: bnx2i endpoint
2711  *
2712  * maps connection's SQ and RQ doorbell registers, 5706/5708/5709 hosts these
2713  *	register in BAR #0. Whereas in 57710 these register are accessed by
2714  *	mapping BAR #1
2715  */
2716 int bnx2i_map_ep_dbell_regs(struct bnx2i_endpoint *ep)
2717 {
2718 	u32 cid_num;
2719 	u32 reg_off;
2720 	u32 first_l4l5;
2721 	u32 ctx_sz;
2722 	u32 config2;
2723 	resource_size_t reg_base;
2724 
2725 	cid_num = bnx2i_get_cid_num(ep);
2726 
2727 	if (test_bit(BNX2I_NX2_DEV_57710, &ep->hba->cnic_dev_type)) {
2728 		reg_base = pci_resource_start(ep->hba->pcidev,
2729 					      BNX2X_DOORBELL_PCI_BAR);
2730 		reg_off = (1 << BNX2X_DB_SHIFT) * (cid_num & 0x1FFFF);
2731 		ep->qp.ctx_base = ioremap_nocache(reg_base + reg_off, 4);
2732 		goto arm_cq;
2733 	}
2734 
2735 	if ((test_bit(BNX2I_NX2_DEV_5709, &ep->hba->cnic_dev_type)) &&
2736 	    (ep->hba->mail_queue_access == BNX2I_MQ_BIN_MODE)) {
2737 		config2 = REG_RD(ep->hba, BNX2_MQ_CONFIG2);
2738 		first_l4l5 = config2 & BNX2_MQ_CONFIG2_FIRST_L4L5;
2739 		ctx_sz = (config2 & BNX2_MQ_CONFIG2_CONT_SZ) >> 3;
2740 		if (ctx_sz)
2741 			reg_off = CTX_OFFSET + MAX_CID_CNT * MB_KERNEL_CTX_SIZE
2742 				  + BNX2I_570X_PAGE_SIZE_DEFAULT *
2743 				  (((cid_num - first_l4l5) / ctx_sz) + 256);
2744 		else
2745 			reg_off = CTX_OFFSET + (MB_KERNEL_CTX_SIZE * cid_num);
2746 	} else
2747 		/* 5709 device in normal node and 5706/5708 devices */
2748 		reg_off = CTX_OFFSET + (MB_KERNEL_CTX_SIZE * cid_num);
2749 
2750 	ep->qp.ctx_base = ioremap_nocache(ep->hba->reg_base + reg_off,
2751 					  MB_KERNEL_CTX_SIZE);
2752 	if (!ep->qp.ctx_base)
2753 		return -ENOMEM;
2754 
2755 arm_cq:
2756 	bnx2i_arm_cq_event_coalescing(ep, CNIC_ARM_CQE);
2757 	return 0;
2758 }
2759