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