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