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