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