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