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