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