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