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