2 * This file is part of the Emulex Linux Device Driver for Enterprise iSCSI
3 * Host Bus Adapters. Refer to the README file included with this package
4 * for driver version and adapter compatibility.
6 * Copyright (c) 2018 Broadcom. All Rights Reserved.
7 * The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of version 2 of the GNU General Public License as published
11 * by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful. ALL EXPRESS
14 * OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
15 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
16 * OR NON-INFRINGEMENT, ARE DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH
17 * DISCLAIMERS ARE HELD TO BE LEGALLY INVALID.
18 * See the GNU General Public License for more details, a copy of which
19 * can be found in the file COPYING included with this package.
21 * Contact Information:
22 * linux-drivers@broadcom.com
26 #include <linux/reboot.h>
27 #include <linux/delay.h>
28 #include <linux/slab.h>
29 #include <linux/interrupt.h>
30 #include <linux/blkdev.h>
31 #include <linux/pci.h>
32 #include <linux/string.h>
33 #include <linux/kernel.h>
34 #include <linux/semaphore.h>
35 #include <linux/iscsi_boot_sysfs.h>
36 #include <linux/module.h>
37 #include <linux/bsg-lib.h>
38 #include <linux/irq_poll.h>
40 #include <scsi/libiscsi.h>
41 #include <scsi/scsi_bsg_iscsi.h>
42 #include <scsi/scsi_netlink.h>
43 #include <scsi/scsi_transport_iscsi.h>
44 #include <scsi/scsi_transport.h>
45 #include <scsi/scsi_cmnd.h>
46 #include <scsi/scsi_device.h>
47 #include <scsi/scsi_host.h>
48 #include <scsi/scsi.h>
54 static unsigned int be_iopoll_budget = 10;
55 static unsigned int be_max_phys_size = 64;
56 static unsigned int enable_msix = 1;
58 MODULE_DESCRIPTION(DRV_DESC " " BUILD_STR);
59 MODULE_VERSION(BUILD_STR);
60 MODULE_AUTHOR("Emulex Corporation");
61 MODULE_LICENSE("GPL");
62 module_param(be_iopoll_budget, int, 0);
63 module_param(enable_msix, int, 0);
64 module_param(be_max_phys_size, uint, S_IRUGO);
65 MODULE_PARM_DESC(be_max_phys_size,
66 "Maximum Size (In Kilobytes) of physically contiguous "
67 "memory that can be allocated. Range is 16 - 128");
69 #define beiscsi_disp_param(_name)\
71 beiscsi_##_name##_disp(struct device *dev,\
72 struct device_attribute *attrib, char *buf) \
74 struct Scsi_Host *shost = class_to_shost(dev);\
75 struct beiscsi_hba *phba = iscsi_host_priv(shost); \
76 return snprintf(buf, PAGE_SIZE, "%d\n",\
80 #define beiscsi_change_param(_name, _minval, _maxval, _defaval)\
82 beiscsi_##_name##_change(struct beiscsi_hba *phba, uint32_t val)\
84 if (val >= _minval && val <= _maxval) {\
85 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,\
86 "BA_%d : beiscsi_"#_name" updated "\
87 "from 0x%x ==> 0x%x\n",\
88 phba->attr_##_name, val); \
89 phba->attr_##_name = val;\
92 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT, \
93 "BA_%d beiscsi_"#_name" attribute "\
94 "cannot be updated to 0x%x, "\
95 "range allowed is ["#_minval" - "#_maxval"]\n", val);\
99 #define beiscsi_store_param(_name) \
101 beiscsi_##_name##_store(struct device *dev,\
102 struct device_attribute *attr, const char *buf,\
105 struct Scsi_Host *shost = class_to_shost(dev);\
106 struct beiscsi_hba *phba = iscsi_host_priv(shost);\
107 uint32_t param_val = 0;\
108 if (!isdigit(buf[0]))\
110 if (sscanf(buf, "%i", ¶m_val) != 1)\
112 if (beiscsi_##_name##_change(phba, param_val) == 0) \
118 #define beiscsi_init_param(_name, _minval, _maxval, _defval) \
120 beiscsi_##_name##_init(struct beiscsi_hba *phba, uint32_t val) \
122 if (val >= _minval && val <= _maxval) {\
123 phba->attr_##_name = val;\
126 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,\
127 "BA_%d beiscsi_"#_name" attribute " \
128 "cannot be updated to 0x%x, "\
129 "range allowed is ["#_minval" - "#_maxval"]\n", val);\
130 phba->attr_##_name = _defval;\
134 #define BEISCSI_RW_ATTR(_name, _minval, _maxval, _defval, _descp) \
135 static uint beiscsi_##_name = _defval;\
136 module_param(beiscsi_##_name, uint, S_IRUGO);\
137 MODULE_PARM_DESC(beiscsi_##_name, _descp);\
138 beiscsi_disp_param(_name)\
139 beiscsi_change_param(_name, _minval, _maxval, _defval)\
140 beiscsi_store_param(_name)\
141 beiscsi_init_param(_name, _minval, _maxval, _defval)\
142 DEVICE_ATTR(beiscsi_##_name, S_IRUGO | S_IWUSR,\
143 beiscsi_##_name##_disp, beiscsi_##_name##_store)
146 * When new log level added update MAX allowed value for log_enable
148 BEISCSI_RW_ATTR(log_enable, 0x00,
149 0xFF, 0x00, "Enable logging Bit Mask\n"
150 "\t\t\t\tInitialization Events : 0x01\n"
151 "\t\t\t\tMailbox Events : 0x02\n"
152 "\t\t\t\tMiscellaneous Events : 0x04\n"
153 "\t\t\t\tError Handling : 0x08\n"
154 "\t\t\t\tIO Path Events : 0x10\n"
155 "\t\t\t\tConfiguration Path : 0x20\n"
156 "\t\t\t\tiSCSI Protocol : 0x40\n");
158 DEVICE_ATTR(beiscsi_drvr_ver, S_IRUGO, beiscsi_drvr_ver_disp, NULL);
159 DEVICE_ATTR(beiscsi_adapter_family, S_IRUGO, beiscsi_adap_family_disp, NULL);
160 DEVICE_ATTR(beiscsi_fw_ver, S_IRUGO, beiscsi_fw_ver_disp, NULL);
161 DEVICE_ATTR(beiscsi_phys_port, S_IRUGO, beiscsi_phys_port_disp, NULL);
162 DEVICE_ATTR(beiscsi_active_session_count, S_IRUGO,
163 beiscsi_active_session_disp, NULL);
164 DEVICE_ATTR(beiscsi_free_session_count, S_IRUGO,
165 beiscsi_free_session_disp, NULL);
166 static struct device_attribute *beiscsi_attrs[] = {
167 &dev_attr_beiscsi_log_enable,
168 &dev_attr_beiscsi_drvr_ver,
169 &dev_attr_beiscsi_adapter_family,
170 &dev_attr_beiscsi_fw_ver,
171 &dev_attr_beiscsi_active_session_count,
172 &dev_attr_beiscsi_free_session_count,
173 &dev_attr_beiscsi_phys_port,
177 static char const *cqe_desc[] = {
180 "SOL_CMD_KILLED_DATA_DIGEST_ERR",
181 "CXN_KILLED_PDU_SIZE_EXCEEDS_DSL",
182 "CXN_KILLED_BURST_LEN_MISMATCH",
183 "CXN_KILLED_AHS_RCVD",
184 "CXN_KILLED_HDR_DIGEST_ERR",
185 "CXN_KILLED_UNKNOWN_HDR",
186 "CXN_KILLED_STALE_ITT_TTT_RCVD",
187 "CXN_KILLED_INVALID_ITT_TTT_RCVD",
188 "CXN_KILLED_RST_RCVD",
189 "CXN_KILLED_TIMED_OUT",
190 "CXN_KILLED_RST_SENT",
191 "CXN_KILLED_FIN_RCVD",
192 "CXN_KILLED_BAD_UNSOL_PDU_RCVD",
193 "CXN_KILLED_BAD_WRB_INDEX_ERROR",
194 "CXN_KILLED_OVER_RUN_RESIDUAL",
195 "CXN_KILLED_UNDER_RUN_RESIDUAL",
196 "CMD_KILLED_INVALID_STATSN_RCVD",
197 "CMD_KILLED_INVALID_R2T_RCVD",
198 "CMD_CXN_KILLED_LUN_INVALID",
199 "CMD_CXN_KILLED_ICD_INVALID",
200 "CMD_CXN_KILLED_ITT_INVALID",
201 "CMD_CXN_KILLED_SEQ_OUTOFORDER",
202 "CMD_CXN_KILLED_INVALID_DATASN_RCVD",
203 "CXN_INVALIDATE_NOTIFY",
204 "CXN_INVALIDATE_INDEX_NOTIFY",
205 "CMD_INVALIDATED_NOTIFY",
208 "UNSOL_DATA_DIGEST_ERROR_NOTIFY",
210 "CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN",
211 "SOL_CMD_KILLED_DIF_ERR",
212 "CXN_KILLED_SYN_RCVD",
213 "CXN_KILLED_IMM_DATA_RCVD"
216 static int beiscsi_eh_abort(struct scsi_cmnd *sc)
218 struct iscsi_task *abrt_task = (struct iscsi_task *)sc->SCp.ptr;
219 struct iscsi_cls_session *cls_session;
220 struct beiscsi_io_task *abrt_io_task;
221 struct beiscsi_conn *beiscsi_conn;
222 struct iscsi_session *session;
223 struct invldt_cmd_tbl inv_tbl;
224 struct beiscsi_hba *phba;
225 struct iscsi_conn *conn;
228 cls_session = starget_to_session(scsi_target(sc->device));
229 session = cls_session->dd_data;
231 /* check if we raced, task just got cleaned up under us */
232 spin_lock_bh(&session->back_lock);
233 if (!abrt_task || !abrt_task->sc) {
234 spin_unlock_bh(&session->back_lock);
237 /* get a task ref till FW processes the req for the ICD used */
238 __iscsi_get_task(abrt_task);
239 abrt_io_task = abrt_task->dd_data;
240 conn = abrt_task->conn;
241 beiscsi_conn = conn->dd_data;
242 phba = beiscsi_conn->phba;
243 /* mark WRB invalid which have been not processed by FW yet */
244 if (is_chip_be2_be3r(phba)) {
245 AMAP_SET_BITS(struct amap_iscsi_wrb, invld,
246 abrt_io_task->pwrb_handle->pwrb, 1);
248 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, invld,
249 abrt_io_task->pwrb_handle->pwrb, 1);
251 inv_tbl.cid = beiscsi_conn->beiscsi_conn_cid;
252 inv_tbl.icd = abrt_io_task->psgl_handle->sgl_index;
253 spin_unlock_bh(&session->back_lock);
255 rc = beiscsi_mgmt_invalidate_icds(phba, &inv_tbl, 1);
256 iscsi_put_task(abrt_task);
258 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
259 "BM_%d : sc %p invalidation failed %d\n",
264 return iscsi_eh_abort(sc);
267 static int beiscsi_eh_device_reset(struct scsi_cmnd *sc)
269 struct beiscsi_invldt_cmd_tbl {
270 struct invldt_cmd_tbl tbl[BE_INVLDT_CMD_TBL_SZ];
271 struct iscsi_task *task[BE_INVLDT_CMD_TBL_SZ];
273 struct iscsi_cls_session *cls_session;
274 struct beiscsi_conn *beiscsi_conn;
275 struct beiscsi_io_task *io_task;
276 struct iscsi_session *session;
277 struct beiscsi_hba *phba;
278 struct iscsi_conn *conn;
279 struct iscsi_task *task;
280 unsigned int i, nents;
283 cls_session = starget_to_session(scsi_target(sc->device));
284 session = cls_session->dd_data;
286 spin_lock_bh(&session->frwd_lock);
287 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) {
288 spin_unlock_bh(&session->frwd_lock);
292 conn = session->leadconn;
293 beiscsi_conn = conn->dd_data;
294 phba = beiscsi_conn->phba;
296 inv_tbl = kzalloc(sizeof(*inv_tbl), GFP_ATOMIC);
298 spin_unlock_bh(&session->frwd_lock);
299 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
300 "BM_%d : invldt_cmd_tbl alloc failed\n");
304 /* take back_lock to prevent task from getting cleaned up under us */
305 spin_lock(&session->back_lock);
306 for (i = 0; i < conn->session->cmds_max; i++) {
307 task = conn->session->cmds[i];
311 if (sc->device->lun != task->sc->device->lun)
314 * Can't fit in more cmds? Normally this won't happen b'coz
315 * BEISCSI_CMD_PER_LUN is same as BE_INVLDT_CMD_TBL_SZ.
317 if (nents == BE_INVLDT_CMD_TBL_SZ) {
322 /* get a task ref till FW processes the req for the ICD used */
323 __iscsi_get_task(task);
324 io_task = task->dd_data;
325 /* mark WRB invalid which have been not processed by FW yet */
326 if (is_chip_be2_be3r(phba)) {
327 AMAP_SET_BITS(struct amap_iscsi_wrb, invld,
328 io_task->pwrb_handle->pwrb, 1);
330 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, invld,
331 io_task->pwrb_handle->pwrb, 1);
334 inv_tbl->tbl[nents].cid = beiscsi_conn->beiscsi_conn_cid;
335 inv_tbl->tbl[nents].icd = io_task->psgl_handle->sgl_index;
336 inv_tbl->task[nents] = task;
339 spin_unlock(&session->back_lock);
340 spin_unlock_bh(&session->frwd_lock);
347 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
348 "BM_%d : number of cmds exceeds size of invalidation table\n");
353 if (beiscsi_mgmt_invalidate_icds(phba, &inv_tbl->tbl[0], nents)) {
354 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
355 "BM_%d : cid %u scmds invalidation failed\n",
356 beiscsi_conn->beiscsi_conn_cid);
361 for (i = 0; i < nents; i++)
362 iscsi_put_task(inv_tbl->task[i]);
366 rc = iscsi_eh_device_reset(sc);
370 /*------------------- PCI Driver operations and data ----------------- */
371 static const struct pci_device_id beiscsi_pci_id_table[] = {
372 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
373 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
374 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
375 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
376 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID3) },
377 { PCI_DEVICE(ELX_VENDOR_ID, OC_SKH_ID1) },
380 MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
383 static struct scsi_host_template beiscsi_sht = {
384 .module = THIS_MODULE,
385 .name = "Emulex 10Gbe open-iscsi Initiator Driver",
386 .proc_name = DRV_NAME,
387 .queuecommand = iscsi_queuecommand,
388 .change_queue_depth = scsi_change_queue_depth,
389 .target_alloc = iscsi_target_alloc,
390 .eh_timed_out = iscsi_eh_cmd_timed_out,
391 .eh_abort_handler = beiscsi_eh_abort,
392 .eh_device_reset_handler = beiscsi_eh_device_reset,
393 .eh_target_reset_handler = iscsi_eh_session_reset,
394 .shost_attrs = beiscsi_attrs,
395 .sg_tablesize = BEISCSI_SGLIST_ELEMENTS,
396 .can_queue = BE2_IO_DEPTH,
398 .max_sectors = BEISCSI_MAX_SECTORS,
399 .max_segment_size = 65536,
400 .cmd_per_lun = BEISCSI_CMD_PER_LUN,
401 .vendor_id = SCSI_NL_VID_TYPE_PCI | BE_VENDOR_ID,
402 .track_queue_depth = 1,
405 static struct scsi_transport_template *beiscsi_scsi_transport;
407 static struct beiscsi_hba *beiscsi_hba_alloc(struct pci_dev *pcidev)
409 struct beiscsi_hba *phba;
410 struct Scsi_Host *shost;
412 shost = iscsi_host_alloc(&beiscsi_sht, sizeof(*phba), 0);
414 dev_err(&pcidev->dev,
415 "beiscsi_hba_alloc - iscsi_host_alloc failed\n");
418 shost->max_id = BE2_MAX_SESSIONS - 1;
419 shost->max_channel = 0;
420 shost->max_cmd_len = BEISCSI_MAX_CMD_LEN;
421 shost->max_lun = BEISCSI_NUM_MAX_LUN;
422 shost->transportt = beiscsi_scsi_transport;
423 phba = iscsi_host_priv(shost);
424 memset(phba, 0, sizeof(*phba));
426 phba->pcidev = pci_dev_get(pcidev);
427 pci_set_drvdata(pcidev, phba);
428 phba->interface_handle = 0xFFFFFFFF;
433 static void beiscsi_unmap_pci_function(struct beiscsi_hba *phba)
436 iounmap(phba->csr_va);
440 iounmap(phba->db_va);
444 iounmap(phba->pci_va);
449 static int beiscsi_map_pci_bars(struct beiscsi_hba *phba,
450 struct pci_dev *pcidev)
455 addr = ioremap(pci_resource_start(pcidev, 2),
456 pci_resource_len(pcidev, 2));
459 phba->ctrl.csr = addr;
462 addr = ioremap(pci_resource_start(pcidev, 4), 128 * 1024);
465 phba->ctrl.db = addr;
468 if (phba->generation == BE_GEN2)
473 addr = ioremap(pci_resource_start(pcidev, pcicfg_reg),
474 pci_resource_len(pcidev, pcicfg_reg));
478 phba->ctrl.pcicfg = addr;
483 beiscsi_unmap_pci_function(phba);
487 static int beiscsi_enable_pci(struct pci_dev *pcidev)
491 ret = pci_enable_device(pcidev);
493 dev_err(&pcidev->dev,
494 "beiscsi_enable_pci - enable device failed\n");
498 ret = pci_request_regions(pcidev, DRV_NAME);
500 dev_err(&pcidev->dev,
501 "beiscsi_enable_pci - request region failed\n");
502 goto pci_dev_disable;
505 pci_set_master(pcidev);
506 ret = dma_set_mask_and_coherent(&pcidev->dev, DMA_BIT_MASK(64));
508 ret = dma_set_mask_and_coherent(&pcidev->dev, DMA_BIT_MASK(32));
510 dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
511 goto pci_region_release;
517 pci_release_regions(pcidev);
519 pci_disable_device(pcidev);
524 static int be_ctrl_init(struct beiscsi_hba *phba, struct pci_dev *pdev)
526 struct be_ctrl_info *ctrl = &phba->ctrl;
527 struct be_dma_mem *mbox_mem_alloc = &ctrl->mbox_mem_alloced;
528 struct be_dma_mem *mbox_mem_align = &ctrl->mbox_mem;
532 status = beiscsi_map_pci_bars(phba, pdev);
535 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
536 mbox_mem_alloc->va = dma_alloc_coherent(&pdev->dev,
537 mbox_mem_alloc->size, &mbox_mem_alloc->dma, GFP_KERNEL);
538 if (!mbox_mem_alloc->va) {
539 beiscsi_unmap_pci_function(phba);
543 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
544 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
545 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
546 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
547 mutex_init(&ctrl->mbox_lock);
548 spin_lock_init(&phba->ctrl.mcc_lock);
554 * beiscsi_get_params()- Set the config paramters
555 * @phba: ptr device priv structure
557 static void beiscsi_get_params(struct beiscsi_hba *phba)
559 uint32_t total_cid_count = 0;
560 uint32_t total_icd_count = 0;
563 total_cid_count = BEISCSI_GET_CID_COUNT(phba, BEISCSI_ULP0) +
564 BEISCSI_GET_CID_COUNT(phba, BEISCSI_ULP1);
566 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
567 uint32_t align_mask = 0;
568 uint32_t icd_post_per_page = 0;
569 uint32_t icd_count_unavailable = 0;
570 uint32_t icd_start = 0, icd_count = 0;
571 uint32_t icd_start_align = 0, icd_count_align = 0;
573 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
574 icd_start = phba->fw_config.iscsi_icd_start[ulp_num];
575 icd_count = phba->fw_config.iscsi_icd_count[ulp_num];
577 /* Get ICD count that can be posted on each page */
578 icd_post_per_page = (PAGE_SIZE / (BE2_SGE *
579 sizeof(struct iscsi_sge)));
580 align_mask = (icd_post_per_page - 1);
582 /* Check if icd_start is aligned ICD per page posting */
583 if (icd_start % icd_post_per_page) {
584 icd_start_align = ((icd_start +
588 iscsi_icd_start[ulp_num] =
592 icd_count_align = (icd_count & ~align_mask);
594 /* ICD discarded in the process of alignment */
596 icd_count_unavailable = ((icd_start_align -
601 /* Updated ICD count available */
602 phba->fw_config.iscsi_icd_count[ulp_num] = (icd_count -
603 icd_count_unavailable);
605 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
606 "BM_%d : Aligned ICD values\n"
607 "\t ICD Start : %d\n"
608 "\t ICD Count : %d\n"
609 "\t ICD Discarded : %d\n",
611 iscsi_icd_start[ulp_num],
613 iscsi_icd_count[ulp_num],
614 icd_count_unavailable);
619 total_icd_count = phba->fw_config.iscsi_icd_count[ulp_num];
620 phba->params.ios_per_ctrl = (total_icd_count -
622 BE2_TMFS + BE2_NOPOUT_REQ));
623 phba->params.cxns_per_ctrl = total_cid_count;
624 phba->params.icds_per_ctrl = total_icd_count;
625 phba->params.num_sge_per_io = BE2_SGE;
626 phba->params.defpdu_hdr_sz = BE2_DEFPDU_HDR_SZ;
627 phba->params.defpdu_data_sz = BE2_DEFPDU_DATA_SZ;
628 phba->params.num_eq_entries = 1024;
629 phba->params.num_cq_entries = 1024;
630 phba->params.wrbs_per_cxn = 256;
633 static void hwi_ring_eq_db(struct beiscsi_hba *phba,
634 unsigned int id, unsigned int clr_interrupt,
635 unsigned int num_processed,
636 unsigned char rearm, unsigned char event)
641 val |= 1 << DB_EQ_REARM_SHIFT;
643 val |= 1 << DB_EQ_CLR_SHIFT;
645 val |= 1 << DB_EQ_EVNT_SHIFT;
647 val |= num_processed << DB_EQ_NUM_POPPED_SHIFT;
648 /* Setting lower order EQ_ID Bits */
649 val |= (id & DB_EQ_RING_ID_LOW_MASK);
651 /* Setting Higher order EQ_ID Bits */
652 val |= (((id >> DB_EQ_HIGH_FEILD_SHIFT) &
653 DB_EQ_RING_ID_HIGH_MASK)
654 << DB_EQ_HIGH_SET_SHIFT);
656 iowrite32(val, phba->db_va + DB_EQ_OFFSET);
660 * be_isr_mcc - The isr routine of the driver.
662 * @dev_id: Pointer to host adapter structure
664 static irqreturn_t be_isr_mcc(int irq, void *dev_id)
666 struct beiscsi_hba *phba;
667 struct be_eq_entry *eqe;
668 struct be_queue_info *eq;
669 struct be_queue_info *mcc;
670 unsigned int mcc_events;
671 struct be_eq_obj *pbe_eq;
676 mcc = &phba->ctrl.mcc_obj.cq;
677 eqe = queue_tail_node(eq);
680 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
682 if (((eqe->dw[offsetof(struct amap_eq_entry,
684 EQE_RESID_MASK) >> 16) == mcc->id) {
687 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
689 eqe = queue_tail_node(eq);
693 queue_work(phba->wq, &pbe_eq->mcc_work);
694 hwi_ring_eq_db(phba, eq->id, 1, mcc_events, 1, 1);
700 * be_isr_msix - The isr routine of the driver.
702 * @dev_id: Pointer to host adapter structure
704 static irqreturn_t be_isr_msix(int irq, void *dev_id)
706 struct beiscsi_hba *phba;
707 struct be_queue_info *eq;
708 struct be_eq_obj *pbe_eq;
714 /* disable interrupt till iopoll completes */
715 hwi_ring_eq_db(phba, eq->id, 1, 0, 0, 1);
716 irq_poll_sched(&pbe_eq->iopoll);
722 * be_isr - The isr routine of the driver.
724 * @dev_id: Pointer to host adapter structure
726 static irqreturn_t be_isr(int irq, void *dev_id)
728 struct beiscsi_hba *phba;
729 struct hwi_controller *phwi_ctrlr;
730 struct hwi_context_memory *phwi_context;
731 struct be_eq_entry *eqe;
732 struct be_queue_info *eq;
733 struct be_queue_info *mcc;
734 unsigned int mcc_events, io_events;
735 struct be_ctrl_info *ctrl;
736 struct be_eq_obj *pbe_eq;
741 isr = ioread32(ctrl->csr + CEV_ISR0_OFFSET +
742 (PCI_FUNC(ctrl->pdev->devfn) * CEV_ISR_SIZE));
746 phwi_ctrlr = phba->phwi_ctrlr;
747 phwi_context = phwi_ctrlr->phwi_ctxt;
748 pbe_eq = &phwi_context->be_eq[0];
750 eq = &phwi_context->be_eq[0].q;
751 mcc = &phba->ctrl.mcc_obj.cq;
752 eqe = queue_tail_node(eq);
756 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
758 if (((eqe->dw[offsetof(struct amap_eq_entry,
759 resource_id) / 32] & EQE_RESID_MASK) >> 16) == mcc->id)
763 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
765 eqe = queue_tail_node(eq);
767 if (!io_events && !mcc_events)
770 /* no need to rearm if interrupt is only for IOs */
773 queue_work(phba->wq, &pbe_eq->mcc_work);
778 irq_poll_sched(&pbe_eq->iopoll);
779 hwi_ring_eq_db(phba, eq->id, 0, (io_events + mcc_events), rearm, 1);
783 static void beiscsi_free_irqs(struct beiscsi_hba *phba)
785 struct hwi_context_memory *phwi_context;
788 if (!phba->pcidev->msix_enabled) {
789 if (phba->pcidev->irq)
790 free_irq(phba->pcidev->irq, phba);
794 phwi_context = phba->phwi_ctrlr->phwi_ctxt;
795 for (i = 0; i <= phba->num_cpus; i++) {
796 free_irq(pci_irq_vector(phba->pcidev, i),
797 &phwi_context->be_eq[i]);
798 kfree(phba->msi_name[i]);
802 static int beiscsi_init_irqs(struct beiscsi_hba *phba)
804 struct pci_dev *pcidev = phba->pcidev;
805 struct hwi_controller *phwi_ctrlr;
806 struct hwi_context_memory *phwi_context;
809 phwi_ctrlr = phba->phwi_ctrlr;
810 phwi_context = phwi_ctrlr->phwi_ctxt;
812 if (pcidev->msix_enabled) {
813 for (i = 0; i < phba->num_cpus; i++) {
814 phba->msi_name[i] = kasprintf(GFP_KERNEL,
816 phba->shost->host_no, i);
817 if (!phba->msi_name[i]) {
822 ret = request_irq(pci_irq_vector(pcidev, i),
823 be_isr_msix, 0, phba->msi_name[i],
824 &phwi_context->be_eq[i]);
826 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
827 "BM_%d : %s-Failed to register msix for i = %d\n",
829 kfree(phba->msi_name[i]);
833 phba->msi_name[i] = kasprintf(GFP_KERNEL, "beiscsi_mcc_%02x",
834 phba->shost->host_no);
835 if (!phba->msi_name[i]) {
839 ret = request_irq(pci_irq_vector(pcidev, i), be_isr_mcc, 0,
840 phba->msi_name[i], &phwi_context->be_eq[i]);
842 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
843 "BM_%d : %s-Failed to register beiscsi_msix_mcc\n",
845 kfree(phba->msi_name[i]);
850 ret = request_irq(pcidev->irq, be_isr, IRQF_SHARED,
853 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
854 "BM_%d : %s-Failed to register irq\n",
861 for (j = i - 1; j >= 0; j--) {
862 free_irq(pci_irq_vector(pcidev, i), &phwi_context->be_eq[j]);
863 kfree(phba->msi_name[j]);
868 void hwi_ring_cq_db(struct beiscsi_hba *phba,
869 unsigned int id, unsigned int num_processed,
875 val |= 1 << DB_CQ_REARM_SHIFT;
877 val |= num_processed << DB_CQ_NUM_POPPED_SHIFT;
879 /* Setting lower order CQ_ID Bits */
880 val |= (id & DB_CQ_RING_ID_LOW_MASK);
882 /* Setting Higher order CQ_ID Bits */
883 val |= (((id >> DB_CQ_HIGH_FEILD_SHIFT) &
884 DB_CQ_RING_ID_HIGH_MASK)
885 << DB_CQ_HIGH_SET_SHIFT);
887 iowrite32(val, phba->db_va + DB_CQ_OFFSET);
890 static struct sgl_handle *alloc_io_sgl_handle(struct beiscsi_hba *phba)
892 struct sgl_handle *psgl_handle;
895 spin_lock_irqsave(&phba->io_sgl_lock, flags);
896 if (phba->io_sgl_hndl_avbl) {
897 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
898 "BM_%d : In alloc_io_sgl_handle,"
899 " io_sgl_alloc_index=%d\n",
900 phba->io_sgl_alloc_index);
902 psgl_handle = phba->io_sgl_hndl_base[phba->
904 phba->io_sgl_hndl_base[phba->io_sgl_alloc_index] = NULL;
905 phba->io_sgl_hndl_avbl--;
906 if (phba->io_sgl_alloc_index == (phba->params.
908 phba->io_sgl_alloc_index = 0;
910 phba->io_sgl_alloc_index++;
913 spin_unlock_irqrestore(&phba->io_sgl_lock, flags);
918 free_io_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
922 spin_lock_irqsave(&phba->io_sgl_lock, flags);
923 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
924 "BM_%d : In free_,io_sgl_free_index=%d\n",
925 phba->io_sgl_free_index);
927 if (phba->io_sgl_hndl_base[phba->io_sgl_free_index]) {
929 * this can happen if clean_task is called on a task that
930 * failed in xmit_task or alloc_pdu.
932 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
933 "BM_%d : Double Free in IO SGL io_sgl_free_index=%d, value there=%p\n",
934 phba->io_sgl_free_index,
935 phba->io_sgl_hndl_base[phba->io_sgl_free_index]);
936 spin_unlock_irqrestore(&phba->io_sgl_lock, flags);
939 phba->io_sgl_hndl_base[phba->io_sgl_free_index] = psgl_handle;
940 phba->io_sgl_hndl_avbl++;
941 if (phba->io_sgl_free_index == (phba->params.ios_per_ctrl - 1))
942 phba->io_sgl_free_index = 0;
944 phba->io_sgl_free_index++;
945 spin_unlock_irqrestore(&phba->io_sgl_lock, flags);
948 static inline struct wrb_handle *
949 beiscsi_get_wrb_handle(struct hwi_wrb_context *pwrb_context,
950 unsigned int wrbs_per_cxn)
952 struct wrb_handle *pwrb_handle;
955 spin_lock_irqsave(&pwrb_context->wrb_lock, flags);
956 if (!pwrb_context->wrb_handles_available) {
957 spin_unlock_irqrestore(&pwrb_context->wrb_lock, flags);
960 pwrb_handle = pwrb_context->pwrb_handle_base[pwrb_context->alloc_index];
961 pwrb_context->wrb_handles_available--;
962 if (pwrb_context->alloc_index == (wrbs_per_cxn - 1))
963 pwrb_context->alloc_index = 0;
965 pwrb_context->alloc_index++;
966 spin_unlock_irqrestore(&pwrb_context->wrb_lock, flags);
969 memset(pwrb_handle->pwrb, 0, sizeof(*pwrb_handle->pwrb));
975 * alloc_wrb_handle - To allocate a wrb handle
976 * @phba: The hba pointer
977 * @cid: The cid to use for allocation
978 * @pcontext: ptr to ptr to wrb context
980 * This happens under session_lock until submission to chip
982 struct wrb_handle *alloc_wrb_handle(struct beiscsi_hba *phba, unsigned int cid,
983 struct hwi_wrb_context **pcontext)
985 struct hwi_wrb_context *pwrb_context;
986 struct hwi_controller *phwi_ctrlr;
987 uint16_t cri_index = BE_GET_CRI_FROM_CID(cid);
989 phwi_ctrlr = phba->phwi_ctrlr;
990 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
991 /* return the context address */
992 *pcontext = pwrb_context;
993 return beiscsi_get_wrb_handle(pwrb_context, phba->params.wrbs_per_cxn);
997 beiscsi_put_wrb_handle(struct hwi_wrb_context *pwrb_context,
998 struct wrb_handle *pwrb_handle,
999 unsigned int wrbs_per_cxn)
1001 unsigned long flags;
1003 spin_lock_irqsave(&pwrb_context->wrb_lock, flags);
1004 pwrb_context->pwrb_handle_base[pwrb_context->free_index] = pwrb_handle;
1005 pwrb_context->wrb_handles_available++;
1006 if (pwrb_context->free_index == (wrbs_per_cxn - 1))
1007 pwrb_context->free_index = 0;
1009 pwrb_context->free_index++;
1010 pwrb_handle->pio_handle = NULL;
1011 spin_unlock_irqrestore(&pwrb_context->wrb_lock, flags);
1015 * free_wrb_handle - To free the wrb handle back to pool
1016 * @phba: The hba pointer
1017 * @pwrb_context: The context to free from
1018 * @pwrb_handle: The wrb_handle to free
1020 * This happens under session_lock until submission to chip
1023 free_wrb_handle(struct beiscsi_hba *phba, struct hwi_wrb_context *pwrb_context,
1024 struct wrb_handle *pwrb_handle)
1026 beiscsi_put_wrb_handle(pwrb_context,
1028 phba->params.wrbs_per_cxn);
1029 beiscsi_log(phba, KERN_INFO,
1030 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1031 "BM_%d : FREE WRB: pwrb_handle=%p free_index=0x%x "
1032 "wrb_handles_available=%d\n",
1033 pwrb_handle, pwrb_context->free_index,
1034 pwrb_context->wrb_handles_available);
1037 static struct sgl_handle *alloc_mgmt_sgl_handle(struct beiscsi_hba *phba)
1039 struct sgl_handle *psgl_handle;
1040 unsigned long flags;
1042 spin_lock_irqsave(&phba->mgmt_sgl_lock, flags);
1043 if (phba->eh_sgl_hndl_avbl) {
1044 psgl_handle = phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index];
1045 phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index] = NULL;
1046 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1047 "BM_%d : mgmt_sgl_alloc_index=%d=0x%x\n",
1048 phba->eh_sgl_alloc_index,
1049 phba->eh_sgl_alloc_index);
1051 phba->eh_sgl_hndl_avbl--;
1052 if (phba->eh_sgl_alloc_index ==
1053 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl -
1055 phba->eh_sgl_alloc_index = 0;
1057 phba->eh_sgl_alloc_index++;
1060 spin_unlock_irqrestore(&phba->mgmt_sgl_lock, flags);
1065 free_mgmt_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
1067 unsigned long flags;
1069 spin_lock_irqsave(&phba->mgmt_sgl_lock, flags);
1070 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1071 "BM_%d : In free_mgmt_sgl_handle,"
1072 "eh_sgl_free_index=%d\n",
1073 phba->eh_sgl_free_index);
1075 if (phba->eh_sgl_hndl_base[phba->eh_sgl_free_index]) {
1077 * this can happen if clean_task is called on a task that
1078 * failed in xmit_task or alloc_pdu.
1080 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
1081 "BM_%d : Double Free in eh SGL ,"
1082 "eh_sgl_free_index=%d\n",
1083 phba->eh_sgl_free_index);
1084 spin_unlock_irqrestore(&phba->mgmt_sgl_lock, flags);
1087 phba->eh_sgl_hndl_base[phba->eh_sgl_free_index] = psgl_handle;
1088 phba->eh_sgl_hndl_avbl++;
1089 if (phba->eh_sgl_free_index ==
1090 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl - 1))
1091 phba->eh_sgl_free_index = 0;
1093 phba->eh_sgl_free_index++;
1094 spin_unlock_irqrestore(&phba->mgmt_sgl_lock, flags);
1098 be_complete_io(struct beiscsi_conn *beiscsi_conn,
1099 struct iscsi_task *task,
1100 struct common_sol_cqe *csol_cqe)
1102 struct beiscsi_io_task *io_task = task->dd_data;
1103 struct be_status_bhs *sts_bhs =
1104 (struct be_status_bhs *)io_task->cmd_bhs;
1105 struct iscsi_conn *conn = beiscsi_conn->conn;
1106 unsigned char *sense;
1107 u32 resid = 0, exp_cmdsn, max_cmdsn;
1108 u8 rsp, status, flags;
1110 exp_cmdsn = csol_cqe->exp_cmdsn;
1111 max_cmdsn = (csol_cqe->exp_cmdsn +
1112 csol_cqe->cmd_wnd - 1);
1113 rsp = csol_cqe->i_resp;
1114 status = csol_cqe->i_sts;
1115 flags = csol_cqe->i_flags;
1116 resid = csol_cqe->res_cnt;
1119 if (io_task->scsi_cmnd) {
1120 scsi_dma_unmap(io_task->scsi_cmnd);
1121 io_task->scsi_cmnd = NULL;
1126 task->sc->result = (DID_OK << 16) | status;
1127 if (rsp != ISCSI_STATUS_CMD_COMPLETED) {
1128 task->sc->result = DID_ERROR << 16;
1132 /* bidi not initially supported */
1133 if (flags & (ISCSI_FLAG_CMD_UNDERFLOW | ISCSI_FLAG_CMD_OVERFLOW)) {
1134 if (!status && (flags & ISCSI_FLAG_CMD_OVERFLOW))
1135 task->sc->result = DID_ERROR << 16;
1137 if (flags & ISCSI_FLAG_CMD_UNDERFLOW) {
1138 scsi_set_resid(task->sc, resid);
1139 if (!status && (scsi_bufflen(task->sc) - resid <
1140 task->sc->underflow))
1141 task->sc->result = DID_ERROR << 16;
1145 if (status == SAM_STAT_CHECK_CONDITION) {
1147 unsigned short *slen = (unsigned short *)sts_bhs->sense_info;
1149 sense = sts_bhs->sense_info + sizeof(unsigned short);
1150 sense_len = be16_to_cpu(*slen);
1151 memcpy(task->sc->sense_buffer, sense,
1152 min_t(u16, sense_len, SCSI_SENSE_BUFFERSIZE));
1155 if (io_task->cmd_bhs->iscsi_hdr.flags & ISCSI_FLAG_CMD_READ)
1156 conn->rxdata_octets += resid;
1158 if (io_task->scsi_cmnd) {
1159 scsi_dma_unmap(io_task->scsi_cmnd);
1160 io_task->scsi_cmnd = NULL;
1162 iscsi_complete_scsi_task(task, exp_cmdsn, max_cmdsn);
1166 be_complete_logout(struct beiscsi_conn *beiscsi_conn,
1167 struct iscsi_task *task,
1168 struct common_sol_cqe *csol_cqe)
1170 struct iscsi_logout_rsp *hdr;
1171 struct beiscsi_io_task *io_task = task->dd_data;
1172 struct iscsi_conn *conn = beiscsi_conn->conn;
1174 hdr = (struct iscsi_logout_rsp *)task->hdr;
1175 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
1178 hdr->flags = csol_cqe->i_flags;
1179 hdr->response = csol_cqe->i_resp;
1180 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
1181 hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
1182 csol_cqe->cmd_wnd - 1);
1184 hdr->dlength[0] = 0;
1185 hdr->dlength[1] = 0;
1186 hdr->dlength[2] = 0;
1188 hdr->itt = io_task->libiscsi_itt;
1189 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1193 be_complete_tmf(struct beiscsi_conn *beiscsi_conn,
1194 struct iscsi_task *task,
1195 struct common_sol_cqe *csol_cqe)
1197 struct iscsi_tm_rsp *hdr;
1198 struct iscsi_conn *conn = beiscsi_conn->conn;
1199 struct beiscsi_io_task *io_task = task->dd_data;
1201 hdr = (struct iscsi_tm_rsp *)task->hdr;
1202 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
1203 hdr->flags = csol_cqe->i_flags;
1204 hdr->response = csol_cqe->i_resp;
1205 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
1206 hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
1207 csol_cqe->cmd_wnd - 1);
1209 hdr->itt = io_task->libiscsi_itt;
1210 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1214 hwi_complete_drvr_msgs(struct beiscsi_conn *beiscsi_conn,
1215 struct beiscsi_hba *phba, struct sol_cqe *psol)
1217 struct hwi_wrb_context *pwrb_context;
1218 uint16_t wrb_index, cid, cri_index;
1219 struct hwi_controller *phwi_ctrlr;
1220 struct wrb_handle *pwrb_handle;
1221 struct iscsi_session *session;
1222 struct iscsi_task *task;
1224 phwi_ctrlr = phba->phwi_ctrlr;
1225 if (is_chip_be2_be3r(phba)) {
1226 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1228 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1231 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1233 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1237 cri_index = BE_GET_CRI_FROM_CID(cid);
1238 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
1239 pwrb_handle = pwrb_context->pwrb_handle_basestd[wrb_index];
1240 session = beiscsi_conn->conn->session;
1241 spin_lock_bh(&session->back_lock);
1242 task = pwrb_handle->pio_handle;
1244 __iscsi_put_task(task);
1245 spin_unlock_bh(&session->back_lock);
1249 be_complete_nopin_resp(struct beiscsi_conn *beiscsi_conn,
1250 struct iscsi_task *task,
1251 struct common_sol_cqe *csol_cqe)
1253 struct iscsi_nopin *hdr;
1254 struct iscsi_conn *conn = beiscsi_conn->conn;
1255 struct beiscsi_io_task *io_task = task->dd_data;
1257 hdr = (struct iscsi_nopin *)task->hdr;
1258 hdr->flags = csol_cqe->i_flags;
1259 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
1260 hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
1261 csol_cqe->cmd_wnd - 1);
1263 hdr->opcode = ISCSI_OP_NOOP_IN;
1264 hdr->itt = io_task->libiscsi_itt;
1265 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1268 static void adapter_get_sol_cqe(struct beiscsi_hba *phba,
1269 struct sol_cqe *psol,
1270 struct common_sol_cqe *csol_cqe)
1272 if (is_chip_be2_be3r(phba)) {
1273 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe,
1274 i_exp_cmd_sn, psol);
1275 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe,
1277 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe,
1279 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe,
1281 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe,
1283 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1285 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe,
1287 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1289 csol_cqe->i_flags = AMAP_GET_BITS(struct amap_sol_cqe,
1292 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1293 i_exp_cmd_sn, psol);
1294 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1296 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1298 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1300 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1302 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1304 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1306 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1309 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1311 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1313 csol_cqe->i_flags = ISCSI_FLAG_CMD_UNDERFLOW;
1315 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1317 csol_cqe->i_flags |= ISCSI_FLAG_CMD_OVERFLOW;
1322 static void hwi_complete_cmd(struct beiscsi_conn *beiscsi_conn,
1323 struct beiscsi_hba *phba, struct sol_cqe *psol)
1325 struct iscsi_conn *conn = beiscsi_conn->conn;
1326 struct iscsi_session *session = conn->session;
1327 struct common_sol_cqe csol_cqe = {0};
1328 struct hwi_wrb_context *pwrb_context;
1329 struct hwi_controller *phwi_ctrlr;
1330 struct wrb_handle *pwrb_handle;
1331 struct iscsi_task *task;
1332 uint16_t cri_index = 0;
1335 phwi_ctrlr = phba->phwi_ctrlr;
1337 /* Copy the elements to a common structure */
1338 adapter_get_sol_cqe(phba, psol, &csol_cqe);
1340 cri_index = BE_GET_CRI_FROM_CID(csol_cqe.cid);
1341 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
1343 pwrb_handle = pwrb_context->pwrb_handle_basestd[
1344 csol_cqe.wrb_index];
1346 spin_lock_bh(&session->back_lock);
1347 task = pwrb_handle->pio_handle;
1349 spin_unlock_bh(&session->back_lock);
1352 type = ((struct beiscsi_io_task *)task->dd_data)->wrb_type;
1356 case HWH_TYPE_IO_RD:
1357 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) ==
1359 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
1361 be_complete_io(beiscsi_conn, task, &csol_cqe);
1364 case HWH_TYPE_LOGOUT:
1365 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
1366 be_complete_logout(beiscsi_conn, task, &csol_cqe);
1368 be_complete_tmf(beiscsi_conn, task, &csol_cqe);
1371 case HWH_TYPE_LOGIN:
1372 beiscsi_log(phba, KERN_ERR,
1373 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1374 "BM_%d :\t\t No HWH_TYPE_LOGIN Expected in"
1375 " %s- Solicited path\n", __func__);
1379 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
1383 beiscsi_log(phba, KERN_WARNING,
1384 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1385 "BM_%d : In %s, unknown type = %d "
1386 "wrb_index 0x%x CID 0x%x\n", __func__, type,
1392 spin_unlock_bh(&session->back_lock);
1396 * ASYNC PDUs include
1397 * a. Unsolicited NOP-In (target initiated NOP-In)
1401 * These headers arrive unprocessed by the EP firmware.
1402 * iSCSI layer processes them.
1405 beiscsi_complete_pdu(struct beiscsi_conn *beiscsi_conn,
1406 struct pdu_base *phdr, void *pdata, unsigned int dlen)
1408 struct beiscsi_hba *phba = beiscsi_conn->phba;
1409 struct iscsi_conn *conn = beiscsi_conn->conn;
1410 struct beiscsi_io_task *io_task;
1411 struct iscsi_hdr *login_hdr;
1412 struct iscsi_task *task;
1415 code = AMAP_GET_BITS(struct amap_pdu_base, opcode, phdr);
1417 case ISCSI_OP_NOOP_IN:
1421 case ISCSI_OP_ASYNC_EVENT:
1423 case ISCSI_OP_REJECT:
1425 WARN_ON(!(dlen == 48));
1426 beiscsi_log(phba, KERN_ERR,
1427 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1428 "BM_%d : In ISCSI_OP_REJECT\n");
1430 case ISCSI_OP_LOGIN_RSP:
1431 case ISCSI_OP_TEXT_RSP:
1432 task = conn->login_task;
1433 io_task = task->dd_data;
1434 login_hdr = (struct iscsi_hdr *)phdr;
1435 login_hdr->itt = io_task->libiscsi_itt;
1438 beiscsi_log(phba, KERN_WARNING,
1439 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1440 "BM_%d : unrecognized async PDU opcode 0x%x\n",
1444 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)phdr, pdata, dlen);
1449 beiscsi_hdl_put_handle(struct hd_async_context *pasync_ctx,
1450 struct hd_async_handle *pasync_handle)
1452 pasync_handle->is_final = 0;
1453 pasync_handle->buffer_len = 0;
1454 pasync_handle->in_use = 0;
1455 list_del_init(&pasync_handle->link);
1459 beiscsi_hdl_purge_handles(struct beiscsi_hba *phba,
1460 struct hd_async_context *pasync_ctx,
1463 struct hd_async_handle *pasync_handle, *tmp_handle;
1464 struct list_head *plist;
1466 plist = &pasync_ctx->async_entry[cri].wq.list;
1467 list_for_each_entry_safe(pasync_handle, tmp_handle, plist, link)
1468 beiscsi_hdl_put_handle(pasync_ctx, pasync_handle);
1470 INIT_LIST_HEAD(&pasync_ctx->async_entry[cri].wq.list);
1471 pasync_ctx->async_entry[cri].wq.hdr_len = 0;
1472 pasync_ctx->async_entry[cri].wq.bytes_received = 0;
1473 pasync_ctx->async_entry[cri].wq.bytes_needed = 0;
1476 static struct hd_async_handle *
1477 beiscsi_hdl_get_handle(struct beiscsi_conn *beiscsi_conn,
1478 struct hd_async_context *pasync_ctx,
1479 struct i_t_dpdu_cqe *pdpdu_cqe,
1482 struct beiscsi_hba *phba = beiscsi_conn->phba;
1483 struct hd_async_handle *pasync_handle;
1484 struct be_bus_address phys_addr;
1485 u16 cid, code, ci, cri;
1486 u8 final, error = 0;
1489 cid = beiscsi_conn->beiscsi_conn_cid;
1490 cri = BE_GET_ASYNC_CRI_FROM_CID(cid);
1492 * This function is invoked to get the right async_handle structure
1493 * from a given DEF PDU CQ entry.
1495 * - index in CQ entry gives the vertical index
1496 * - address in CQ entry is the offset where the DMA last ended
1497 * - final - no more notifications for this PDU
1499 if (is_chip_be2_be3r(phba)) {
1500 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1502 ci = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1504 final = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1507 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1509 ci = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1511 final = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1516 * DB addr Hi/Lo is same for BE and SKH.
1517 * Subtract the dataplacementlength to get to the base.
1519 phys_addr.u.a32.address_lo = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1520 db_addr_lo, pdpdu_cqe);
1521 phys_addr.u.a32.address_lo -= dpl;
1522 phys_addr.u.a32.address_hi = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1523 db_addr_hi, pdpdu_cqe);
1525 code = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe, code, pdpdu_cqe);
1527 case UNSOL_HDR_NOTIFY:
1528 pasync_handle = pasync_ctx->async_entry[ci].header;
1531 case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
1534 case UNSOL_DATA_NOTIFY:
1535 pasync_handle = pasync_ctx->async_entry[ci].data;
1537 /* called only for above codes */
1542 if (pasync_handle->pa.u.a64.address != phys_addr.u.a64.address ||
1543 pasync_handle->index != ci) {
1544 /* driver bug - if ci does not match async handle index */
1546 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
1547 "BM_%d : cid %u async PDU handle mismatch - addr in %cQE %llx at %u:addr in CQE %llx ci %u\n",
1548 cid, pasync_handle->is_header ? 'H' : 'D',
1549 pasync_handle->pa.u.a64.address,
1550 pasync_handle->index,
1551 phys_addr.u.a64.address, ci);
1552 /* FW has stale address - attempt continuing by dropping */
1556 * DEF PDU header and data buffers with errors should be simply
1557 * dropped as there are no consumers for it.
1560 beiscsi_hdl_put_handle(pasync_ctx, pasync_handle);
1564 if (pasync_handle->in_use || !list_empty(&pasync_handle->link)) {
1565 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
1566 "BM_%d : cid %d async PDU handle in use - code %d ci %d addr %llx\n",
1567 cid, code, ci, phys_addr.u.a64.address);
1568 beiscsi_hdl_purge_handles(phba, pasync_ctx, cri);
1571 list_del_init(&pasync_handle->link);
1573 * Each CID is associated with unique CRI.
1574 * ASYNC_CRI_FROM_CID mapping and CRI_FROM_CID are totaly different.
1576 pasync_handle->cri = cri;
1577 pasync_handle->is_final = final;
1578 pasync_handle->buffer_len = dpl;
1579 pasync_handle->in_use = 1;
1581 return pasync_handle;
1585 beiscsi_hdl_fwd_pdu(struct beiscsi_conn *beiscsi_conn,
1586 struct hd_async_context *pasync_ctx,
1589 struct iscsi_session *session = beiscsi_conn->conn->session;
1590 struct hd_async_handle *pasync_handle, *plast_handle;
1591 struct beiscsi_hba *phba = beiscsi_conn->phba;
1592 void *phdr = NULL, *pdata = NULL;
1593 u32 dlen = 0, status = 0;
1594 struct list_head *plist;
1596 plist = &pasync_ctx->async_entry[cri].wq.list;
1597 plast_handle = NULL;
1598 list_for_each_entry(pasync_handle, plist, link) {
1599 plast_handle = pasync_handle;
1600 /* get the header, the first entry */
1602 phdr = pasync_handle->pbuffer;
1605 /* use first buffer to collect all the data */
1607 pdata = pasync_handle->pbuffer;
1608 dlen = pasync_handle->buffer_len;
1611 if (!pasync_handle->buffer_len ||
1612 (dlen + pasync_handle->buffer_len) >
1613 pasync_ctx->async_data.buffer_size)
1615 memcpy(pdata + dlen, pasync_handle->pbuffer,
1616 pasync_handle->buffer_len);
1617 dlen += pasync_handle->buffer_len;
1620 if (!plast_handle->is_final) {
1621 /* last handle should have final PDU notification from FW */
1622 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
1623 "BM_%d : cid %u %p fwd async PDU opcode %x with last handle missing - HL%u:DN%u:DR%u\n",
1624 beiscsi_conn->beiscsi_conn_cid, plast_handle,
1625 AMAP_GET_BITS(struct amap_pdu_base, opcode, phdr),
1626 pasync_ctx->async_entry[cri].wq.hdr_len,
1627 pasync_ctx->async_entry[cri].wq.bytes_needed,
1628 pasync_ctx->async_entry[cri].wq.bytes_received);
1630 spin_lock_bh(&session->back_lock);
1631 status = beiscsi_complete_pdu(beiscsi_conn, phdr, pdata, dlen);
1632 spin_unlock_bh(&session->back_lock);
1633 beiscsi_hdl_purge_handles(phba, pasync_ctx, cri);
1638 beiscsi_hdl_gather_pdu(struct beiscsi_conn *beiscsi_conn,
1639 struct hd_async_context *pasync_ctx,
1640 struct hd_async_handle *pasync_handle)
1642 unsigned int bytes_needed = 0, status = 0;
1643 u16 cri = pasync_handle->cri;
1644 struct cri_wait_queue *wq;
1645 struct beiscsi_hba *phba;
1646 struct pdu_base *ppdu;
1649 phba = beiscsi_conn->phba;
1650 wq = &pasync_ctx->async_entry[cri].wq;
1651 if (pasync_handle->is_header) {
1652 /* check if PDU hdr is rcv'd when old hdr not completed */
1657 ppdu = pasync_handle->pbuffer;
1658 bytes_needed = AMAP_GET_BITS(struct amap_pdu_base,
1660 bytes_needed <<= 16;
1661 bytes_needed |= be16_to_cpu(AMAP_GET_BITS(struct amap_pdu_base,
1662 data_len_lo, ppdu));
1663 wq->hdr_len = pasync_handle->buffer_len;
1664 wq->bytes_received = 0;
1665 wq->bytes_needed = bytes_needed;
1666 list_add_tail(&pasync_handle->link, &wq->list);
1668 status = beiscsi_hdl_fwd_pdu(beiscsi_conn,
1671 /* check if data received has header and is needed */
1672 if (!wq->hdr_len || !wq->bytes_needed) {
1673 err = "header less";
1676 wq->bytes_received += pasync_handle->buffer_len;
1677 /* Something got overwritten? Better catch it here. */
1678 if (wq->bytes_received > wq->bytes_needed) {
1682 list_add_tail(&pasync_handle->link, &wq->list);
1683 if (wq->bytes_received == wq->bytes_needed)
1684 status = beiscsi_hdl_fwd_pdu(beiscsi_conn,
1690 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
1691 "BM_%d : cid %u async PDU %s - def-%c:HL%u:DN%u:DR%u\n",
1692 beiscsi_conn->beiscsi_conn_cid, err,
1693 pasync_handle->is_header ? 'H' : 'D',
1694 wq->hdr_len, wq->bytes_needed,
1695 pasync_handle->buffer_len);
1696 /* discard this handle */
1697 beiscsi_hdl_put_handle(pasync_ctx, pasync_handle);
1698 /* free all the other handles in cri_wait_queue */
1699 beiscsi_hdl_purge_handles(phba, pasync_ctx, cri);
1700 /* try continuing */
1705 beiscsi_hdq_post_handles(struct beiscsi_hba *phba,
1706 u8 header, u8 ulp_num, u16 nbuf)
1708 struct hd_async_handle *pasync_handle;
1709 struct hd_async_context *pasync_ctx;
1710 struct hwi_controller *phwi_ctrlr;
1711 struct phys_addr *pasync_sge;
1712 u32 ring_id, doorbell = 0;
1713 u32 doorbell_offset;
1716 phwi_ctrlr = phba->phwi_ctrlr;
1717 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr, ulp_num);
1719 pasync_sge = pasync_ctx->async_header.ring_base;
1720 pi = pasync_ctx->async_header.pi;
1721 ring_id = phwi_ctrlr->default_pdu_hdr[ulp_num].id;
1722 doorbell_offset = phwi_ctrlr->default_pdu_hdr[ulp_num].
1725 pasync_sge = pasync_ctx->async_data.ring_base;
1726 pi = pasync_ctx->async_data.pi;
1727 ring_id = phwi_ctrlr->default_pdu_data[ulp_num].id;
1728 doorbell_offset = phwi_ctrlr->default_pdu_data[ulp_num].
1732 for (prod = 0; prod < nbuf; prod++) {
1734 pasync_handle = pasync_ctx->async_entry[pi].header;
1736 pasync_handle = pasync_ctx->async_entry[pi].data;
1737 WARN_ON(pasync_handle->is_header != header);
1738 WARN_ON(pasync_handle->index != pi);
1739 /* setup the ring only once */
1740 if (nbuf == pasync_ctx->num_entries) {
1742 pasync_sge[pi].hi = pasync_handle->pa.u.a32.address_lo;
1743 pasync_sge[pi].lo = pasync_handle->pa.u.a32.address_hi;
1745 if (++pi == pasync_ctx->num_entries)
1750 pasync_ctx->async_header.pi = pi;
1752 pasync_ctx->async_data.pi = pi;
1754 doorbell |= ring_id & DB_DEF_PDU_RING_ID_MASK;
1755 doorbell |= 1 << DB_DEF_PDU_REARM_SHIFT;
1756 doorbell |= 0 << DB_DEF_PDU_EVENT_SHIFT;
1757 doorbell |= (prod & DB_DEF_PDU_CQPROC_MASK) << DB_DEF_PDU_CQPROC_SHIFT;
1758 iowrite32(doorbell, phba->db_va + doorbell_offset);
1762 beiscsi_hdq_process_compl(struct beiscsi_conn *beiscsi_conn,
1763 struct i_t_dpdu_cqe *pdpdu_cqe)
1765 struct beiscsi_hba *phba = beiscsi_conn->phba;
1766 struct hd_async_handle *pasync_handle = NULL;
1767 struct hd_async_context *pasync_ctx;
1768 struct hwi_controller *phwi_ctrlr;
1769 u8 ulp_num, consumed, header = 0;
1772 phwi_ctrlr = phba->phwi_ctrlr;
1773 cid_cri = BE_GET_CRI_FROM_CID(beiscsi_conn->beiscsi_conn_cid);
1774 ulp_num = BEISCSI_GET_ULP_FROM_CRI(phwi_ctrlr, cid_cri);
1775 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr, ulp_num);
1776 pasync_handle = beiscsi_hdl_get_handle(beiscsi_conn, pasync_ctx,
1777 pdpdu_cqe, &header);
1778 if (is_chip_be2_be3r(phba))
1779 consumed = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1780 num_cons, pdpdu_cqe);
1782 consumed = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1783 num_cons, pdpdu_cqe);
1785 beiscsi_hdl_gather_pdu(beiscsi_conn, pasync_ctx, pasync_handle);
1786 /* num_cons indicates number of 8 RQEs consumed */
1788 beiscsi_hdq_post_handles(phba, header, ulp_num, 8 * consumed);
1791 void beiscsi_process_mcc_cq(struct beiscsi_hba *phba)
1793 struct be_queue_info *mcc_cq;
1794 struct be_mcc_compl *mcc_compl;
1795 unsigned int num_processed = 0;
1797 mcc_cq = &phba->ctrl.mcc_obj.cq;
1798 mcc_compl = queue_tail_node(mcc_cq);
1799 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1800 while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
1801 if (beiscsi_hba_in_error(phba))
1804 if (num_processed >= 32) {
1805 hwi_ring_cq_db(phba, mcc_cq->id,
1809 if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
1810 beiscsi_process_async_event(phba, mcc_compl);
1811 } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
1812 beiscsi_process_mcc_compl(&phba->ctrl, mcc_compl);
1815 mcc_compl->flags = 0;
1816 queue_tail_inc(mcc_cq);
1817 mcc_compl = queue_tail_node(mcc_cq);
1818 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1822 if (num_processed > 0)
1823 hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1);
1826 static void beiscsi_mcc_work(struct work_struct *work)
1828 struct be_eq_obj *pbe_eq;
1829 struct beiscsi_hba *phba;
1831 pbe_eq = container_of(work, struct be_eq_obj, mcc_work);
1832 phba = pbe_eq->phba;
1833 beiscsi_process_mcc_cq(phba);
1834 /* rearm EQ for further interrupts */
1835 if (!beiscsi_hba_in_error(phba))
1836 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
1840 * beiscsi_process_cq()- Process the Completion Queue
1841 * @pbe_eq: Event Q on which the Completion has come
1842 * @budget: Max number of events to processed
1845 * Number of Completion Entries processed.
1847 unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq, int budget)
1849 struct be_queue_info *cq;
1850 struct sol_cqe *sol;
1851 unsigned int total = 0;
1852 unsigned int num_processed = 0;
1853 unsigned short code = 0, cid = 0;
1854 uint16_t cri_index = 0;
1855 struct beiscsi_conn *beiscsi_conn;
1856 struct beiscsi_endpoint *beiscsi_ep;
1857 struct iscsi_endpoint *ep;
1858 struct beiscsi_hba *phba;
1861 sol = queue_tail_node(cq);
1862 phba = pbe_eq->phba;
1864 while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
1866 if (beiscsi_hba_in_error(phba))
1869 be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
1871 code = (sol->dw[offsetof(struct amap_sol_cqe, code) / 32] &
1875 if (is_chip_be2_be3r(phba)) {
1876 cid = AMAP_GET_BITS(struct amap_sol_cqe, cid, sol);
1878 if ((code == DRIVERMSG_NOTIFY) ||
1879 (code == UNSOL_HDR_NOTIFY) ||
1880 (code == UNSOL_DATA_NOTIFY))
1881 cid = AMAP_GET_BITS(
1882 struct amap_i_t_dpdu_cqe_v2,
1885 cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1889 cri_index = BE_GET_CRI_FROM_CID(cid);
1890 ep = phba->ep_array[cri_index];
1893 /* connection has already been freed
1894 * just move on to next one
1896 beiscsi_log(phba, KERN_WARNING,
1898 "BM_%d : proc cqe of disconn ep: cid %d\n",
1903 beiscsi_ep = ep->dd_data;
1904 beiscsi_conn = beiscsi_ep->conn;
1907 if (num_processed == 32) {
1908 hwi_ring_cq_db(phba, cq->id, 32, 0);
1914 case SOL_CMD_COMPLETE:
1915 hwi_complete_cmd(beiscsi_conn, phba, sol);
1917 case DRIVERMSG_NOTIFY:
1918 beiscsi_log(phba, KERN_INFO,
1919 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1920 "BM_%d : Received %s[%d] on CID : %d\n",
1921 cqe_desc[code], code, cid);
1923 hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
1925 case UNSOL_HDR_NOTIFY:
1926 beiscsi_log(phba, KERN_INFO,
1927 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1928 "BM_%d : Received %s[%d] on CID : %d\n",
1929 cqe_desc[code], code, cid);
1931 spin_lock_bh(&phba->async_pdu_lock);
1932 beiscsi_hdq_process_compl(beiscsi_conn,
1933 (struct i_t_dpdu_cqe *)sol);
1934 spin_unlock_bh(&phba->async_pdu_lock);
1936 case UNSOL_DATA_NOTIFY:
1937 beiscsi_log(phba, KERN_INFO,
1938 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1939 "BM_%d : Received %s[%d] on CID : %d\n",
1940 cqe_desc[code], code, cid);
1942 spin_lock_bh(&phba->async_pdu_lock);
1943 beiscsi_hdq_process_compl(beiscsi_conn,
1944 (struct i_t_dpdu_cqe *)sol);
1945 spin_unlock_bh(&phba->async_pdu_lock);
1947 case CXN_INVALIDATE_INDEX_NOTIFY:
1948 case CMD_INVALIDATED_NOTIFY:
1949 case CXN_INVALIDATE_NOTIFY:
1950 beiscsi_log(phba, KERN_ERR,
1951 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1952 "BM_%d : Ignoring %s[%d] on CID : %d\n",
1953 cqe_desc[code], code, cid);
1955 case CXN_KILLED_HDR_DIGEST_ERR:
1956 case SOL_CMD_KILLED_DATA_DIGEST_ERR:
1957 beiscsi_log(phba, KERN_ERR,
1958 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1959 "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
1960 cqe_desc[code], code, cid);
1962 case CMD_KILLED_INVALID_STATSN_RCVD:
1963 case CMD_KILLED_INVALID_R2T_RCVD:
1964 case CMD_CXN_KILLED_LUN_INVALID:
1965 case CMD_CXN_KILLED_ICD_INVALID:
1966 case CMD_CXN_KILLED_ITT_INVALID:
1967 case CMD_CXN_KILLED_SEQ_OUTOFORDER:
1968 case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
1969 beiscsi_log(phba, KERN_ERR,
1970 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1971 "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
1972 cqe_desc[code], code, cid);
1974 case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
1975 beiscsi_log(phba, KERN_ERR,
1976 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1977 "BM_%d : Dropping %s[%d] on DPDU ring on CID : %d\n",
1978 cqe_desc[code], code, cid);
1979 spin_lock_bh(&phba->async_pdu_lock);
1980 /* driver consumes the entry and drops the contents */
1981 beiscsi_hdq_process_compl(beiscsi_conn,
1982 (struct i_t_dpdu_cqe *)sol);
1983 spin_unlock_bh(&phba->async_pdu_lock);
1985 case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
1986 case CXN_KILLED_BURST_LEN_MISMATCH:
1987 case CXN_KILLED_AHS_RCVD:
1988 case CXN_KILLED_UNKNOWN_HDR:
1989 case CXN_KILLED_STALE_ITT_TTT_RCVD:
1990 case CXN_KILLED_INVALID_ITT_TTT_RCVD:
1991 case CXN_KILLED_TIMED_OUT:
1992 case CXN_KILLED_FIN_RCVD:
1993 case CXN_KILLED_RST_SENT:
1994 case CXN_KILLED_RST_RCVD:
1995 case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
1996 case CXN_KILLED_BAD_WRB_INDEX_ERROR:
1997 case CXN_KILLED_OVER_RUN_RESIDUAL:
1998 case CXN_KILLED_UNDER_RUN_RESIDUAL:
1999 case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
2000 beiscsi_log(phba, KERN_ERR,
2001 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
2002 "BM_%d : Event %s[%d] received on CID : %d\n",
2003 cqe_desc[code], code, cid);
2005 iscsi_conn_failure(beiscsi_conn->conn,
2006 ISCSI_ERR_CONN_FAILED);
2009 beiscsi_log(phba, KERN_ERR,
2010 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
2011 "BM_%d : Invalid CQE Event Received Code : %d CID 0x%x...\n",
2017 AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
2019 sol = queue_tail_node(cq);
2021 if (total == budget)
2025 hwi_ring_cq_db(phba, cq->id, num_processed, 1);
2029 static int be_iopoll(struct irq_poll *iop, int budget)
2031 unsigned int ret, io_events;
2032 struct beiscsi_hba *phba;
2033 struct be_eq_obj *pbe_eq;
2034 struct be_eq_entry *eqe = NULL;
2035 struct be_queue_info *eq;
2037 pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
2038 phba = pbe_eq->phba;
2039 if (beiscsi_hba_in_error(phba)) {
2040 irq_poll_complete(iop);
2046 eqe = queue_tail_node(eq);
2047 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32] &
2049 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
2051 eqe = queue_tail_node(eq);
2054 hwi_ring_eq_db(phba, eq->id, 1, io_events, 0, 1);
2056 ret = beiscsi_process_cq(pbe_eq, budget);
2057 pbe_eq->cq_count += ret;
2059 irq_poll_complete(iop);
2060 beiscsi_log(phba, KERN_INFO,
2061 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
2062 "BM_%d : rearm pbe_eq->q.id =%d ret %d\n",
2064 if (!beiscsi_hba_in_error(phba))
2065 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
2071 hwi_write_sgl_v2(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2072 unsigned int num_sg, struct beiscsi_io_task *io_task)
2074 struct iscsi_sge *psgl;
2075 unsigned int sg_len, index;
2076 unsigned int sge_len = 0;
2077 unsigned long long addr;
2078 struct scatterlist *l_sg;
2079 unsigned int offset;
2081 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_lo, pwrb,
2082 io_task->bhs_pa.u.a32.address_lo);
2083 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_hi, pwrb,
2084 io_task->bhs_pa.u.a32.address_hi);
2087 for (index = 0; (index < num_sg) && (index < 2); index++,
2090 sg_len = sg_dma_len(sg);
2091 addr = (u64) sg_dma_address(sg);
2092 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2094 lower_32_bits(addr));
2095 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2097 upper_32_bits(addr));
2098 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2103 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_r2t_offset,
2105 sg_len = sg_dma_len(sg);
2106 addr = (u64) sg_dma_address(sg);
2107 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2109 lower_32_bits(addr));
2110 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2112 upper_32_bits(addr));
2113 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2118 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2119 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2121 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2123 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2124 io_task->bhs_pa.u.a32.address_hi);
2125 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2126 io_task->bhs_pa.u.a32.address_lo);
2129 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2131 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2133 } else if (num_sg == 2) {
2134 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2136 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2139 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2141 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2149 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
2150 sg_len = sg_dma_len(sg);
2151 addr = (u64) sg_dma_address(sg);
2152 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2153 lower_32_bits(addr));
2154 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2155 upper_32_bits(addr));
2156 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2157 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2158 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2162 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2166 hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2167 unsigned int num_sg, struct beiscsi_io_task *io_task)
2169 struct iscsi_sge *psgl;
2170 unsigned int sg_len, index;
2171 unsigned int sge_len = 0;
2172 unsigned long long addr;
2173 struct scatterlist *l_sg;
2174 unsigned int offset;
2176 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2177 io_task->bhs_pa.u.a32.address_lo);
2178 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2179 io_task->bhs_pa.u.a32.address_hi);
2182 for (index = 0; (index < num_sg) && (index < 2); index++,
2185 sg_len = sg_dma_len(sg);
2186 addr = (u64) sg_dma_address(sg);
2187 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
2188 ((u32)(addr & 0xFFFFFFFF)));
2189 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
2190 ((u32)(addr >> 32)));
2191 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2195 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
2197 sg_len = sg_dma_len(sg);
2198 addr = (u64) sg_dma_address(sg);
2199 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
2200 ((u32)(addr & 0xFFFFFFFF)));
2201 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
2202 ((u32)(addr >> 32)));
2203 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
2207 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2208 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2210 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2212 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2213 io_task->bhs_pa.u.a32.address_hi);
2214 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2215 io_task->bhs_pa.u.a32.address_lo);
2218 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2220 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2222 } else if (num_sg == 2) {
2223 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2225 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2228 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2230 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2237 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
2238 sg_len = sg_dma_len(sg);
2239 addr = (u64) sg_dma_address(sg);
2240 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2241 (addr & 0xFFFFFFFF));
2242 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2244 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2245 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2246 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2250 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2254 * hwi_write_buffer()- Populate the WRB with task info
2255 * @pwrb: ptr to the WRB entry
2256 * @task: iscsi task which is to be executed
2258 static int hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
2260 struct iscsi_sge *psgl;
2261 struct beiscsi_io_task *io_task = task->dd_data;
2262 struct beiscsi_conn *beiscsi_conn = io_task->conn;
2263 struct beiscsi_hba *phba = beiscsi_conn->phba;
2264 uint8_t dsp_value = 0;
2266 io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
2267 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2268 io_task->bhs_pa.u.a32.address_lo);
2269 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2270 io_task->bhs_pa.u.a32.address_hi);
2274 /* Check for the data_count */
2275 dsp_value = (task->data_count) ? 1 : 0;
2277 if (is_chip_be2_be3r(phba))
2278 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp,
2281 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp,
2284 /* Map addr only if there is data_count */
2286 io_task->mtask_addr = dma_map_single(&phba->pcidev->dev,
2290 if (dma_mapping_error(&phba->pcidev->dev,
2291 io_task->mtask_addr))
2293 io_task->mtask_data_count = task->data_count;
2295 io_task->mtask_addr = 0;
2297 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
2298 lower_32_bits(io_task->mtask_addr));
2299 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
2300 upper_32_bits(io_task->mtask_addr));
2301 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2304 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
2306 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
2307 io_task->mtask_addr = 0;
2310 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2312 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
2314 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2315 io_task->bhs_pa.u.a32.address_hi);
2316 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2317 io_task->bhs_pa.u.a32.address_lo);
2320 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
2321 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
2322 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
2323 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
2324 AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
2325 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2329 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2330 lower_32_bits(io_task->mtask_addr));
2331 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2332 upper_32_bits(io_task->mtask_addr));
2334 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
2336 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2341 * beiscsi_find_mem_req()- Find mem needed
2342 * @phba: ptr to HBA struct
2344 static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
2346 uint8_t mem_descr_index, ulp_num;
2347 unsigned int num_async_pdu_buf_pages;
2348 unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
2349 unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
2351 phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
2353 phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
2354 BE_ISCSI_PDU_HEADER_SIZE;
2355 phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
2356 sizeof(struct hwi_context_memory);
2359 phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
2360 * (phba->params.wrbs_per_cxn)
2361 * phba->params.cxns_per_ctrl;
2362 wrb_sz_per_cxn = sizeof(struct wrb_handle) *
2363 (phba->params.wrbs_per_cxn);
2364 phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
2365 phba->params.cxns_per_ctrl);
2367 phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
2368 phba->params.icds_per_ctrl;
2369 phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
2370 phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
2371 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
2372 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
2374 num_async_pdu_buf_sgl_pages =
2375 PAGES_REQUIRED(BEISCSI_ASYNC_HDQ_SIZE(
2377 sizeof(struct phys_addr));
2379 num_async_pdu_buf_pages =
2380 PAGES_REQUIRED(BEISCSI_ASYNC_HDQ_SIZE(
2382 phba->params.defpdu_hdr_sz);
2384 num_async_pdu_data_pages =
2385 PAGES_REQUIRED(BEISCSI_ASYNC_HDQ_SIZE(
2387 phba->params.defpdu_data_sz);
2389 num_async_pdu_data_sgl_pages =
2390 PAGES_REQUIRED(BEISCSI_ASYNC_HDQ_SIZE(
2392 sizeof(struct phys_addr));
2394 mem_descr_index = (HWI_MEM_TEMPLATE_HDR_ULP0 +
2395 (ulp_num * MEM_DESCR_OFFSET));
2396 phba->mem_req[mem_descr_index] =
2397 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
2398 BEISCSI_TEMPLATE_HDR_PER_CXN_SIZE;
2400 mem_descr_index = (HWI_MEM_ASYNC_HEADER_BUF_ULP0 +
2401 (ulp_num * MEM_DESCR_OFFSET));
2402 phba->mem_req[mem_descr_index] =
2403 num_async_pdu_buf_pages *
2406 mem_descr_index = (HWI_MEM_ASYNC_DATA_BUF_ULP0 +
2407 (ulp_num * MEM_DESCR_OFFSET));
2408 phba->mem_req[mem_descr_index] =
2409 num_async_pdu_data_pages *
2412 mem_descr_index = (HWI_MEM_ASYNC_HEADER_RING_ULP0 +
2413 (ulp_num * MEM_DESCR_OFFSET));
2414 phba->mem_req[mem_descr_index] =
2415 num_async_pdu_buf_sgl_pages *
2418 mem_descr_index = (HWI_MEM_ASYNC_DATA_RING_ULP0 +
2419 (ulp_num * MEM_DESCR_OFFSET));
2420 phba->mem_req[mem_descr_index] =
2421 num_async_pdu_data_sgl_pages *
2424 mem_descr_index = (HWI_MEM_ASYNC_HEADER_HANDLE_ULP0 +
2425 (ulp_num * MEM_DESCR_OFFSET));
2426 phba->mem_req[mem_descr_index] =
2427 BEISCSI_ASYNC_HDQ_SIZE(phba, ulp_num) *
2428 sizeof(struct hd_async_handle);
2430 mem_descr_index = (HWI_MEM_ASYNC_DATA_HANDLE_ULP0 +
2431 (ulp_num * MEM_DESCR_OFFSET));
2432 phba->mem_req[mem_descr_index] =
2433 BEISCSI_ASYNC_HDQ_SIZE(phba, ulp_num) *
2434 sizeof(struct hd_async_handle);
2436 mem_descr_index = (HWI_MEM_ASYNC_PDU_CONTEXT_ULP0 +
2437 (ulp_num * MEM_DESCR_OFFSET));
2438 phba->mem_req[mem_descr_index] =
2439 sizeof(struct hd_async_context) +
2440 (BEISCSI_ASYNC_HDQ_SIZE(phba, ulp_num) *
2441 sizeof(struct hd_async_entry));
2446 static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
2449 struct hwi_controller *phwi_ctrlr;
2450 struct be_mem_descriptor *mem_descr;
2451 struct mem_array *mem_arr, *mem_arr_orig;
2452 unsigned int i, j, alloc_size, curr_alloc_size;
2454 phba->phwi_ctrlr = kzalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
2455 if (!phba->phwi_ctrlr)
2458 /* Allocate memory for wrb_context */
2459 phwi_ctrlr = phba->phwi_ctrlr;
2460 phwi_ctrlr->wrb_context = kcalloc(phba->params.cxns_per_ctrl,
2461 sizeof(struct hwi_wrb_context),
2463 if (!phwi_ctrlr->wrb_context) {
2464 kfree(phba->phwi_ctrlr);
2468 phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
2470 if (!phba->init_mem) {
2471 kfree(phwi_ctrlr->wrb_context);
2472 kfree(phba->phwi_ctrlr);
2476 mem_arr_orig = kmalloc_array(BEISCSI_MAX_FRAGS_INIT,
2477 sizeof(*mem_arr_orig),
2479 if (!mem_arr_orig) {
2480 kfree(phba->init_mem);
2481 kfree(phwi_ctrlr->wrb_context);
2482 kfree(phba->phwi_ctrlr);
2486 mem_descr = phba->init_mem;
2487 for (i = 0; i < SE_MEM_MAX; i++) {
2488 if (!phba->mem_req[i]) {
2489 mem_descr->mem_array = NULL;
2495 mem_arr = mem_arr_orig;
2496 alloc_size = phba->mem_req[i];
2497 memset(mem_arr, 0, sizeof(struct mem_array) *
2498 BEISCSI_MAX_FRAGS_INIT);
2499 curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
2501 mem_arr->virtual_address =
2502 dma_alloc_coherent(&phba->pcidev->dev,
2503 curr_alloc_size, &bus_add, GFP_KERNEL);
2504 if (!mem_arr->virtual_address) {
2505 if (curr_alloc_size <= BE_MIN_MEM_SIZE)
2507 if (curr_alloc_size -
2508 rounddown_pow_of_two(curr_alloc_size))
2509 curr_alloc_size = rounddown_pow_of_two
2512 curr_alloc_size = curr_alloc_size / 2;
2514 mem_arr->bus_address.u.
2515 a64.address = (__u64) bus_add;
2516 mem_arr->size = curr_alloc_size;
2517 alloc_size -= curr_alloc_size;
2518 curr_alloc_size = min(be_max_phys_size *
2523 } while (alloc_size);
2524 mem_descr->num_elements = j;
2525 mem_descr->size_in_bytes = phba->mem_req[i];
2526 mem_descr->mem_array = kmalloc_array(j, sizeof(*mem_arr),
2528 if (!mem_descr->mem_array)
2531 memcpy(mem_descr->mem_array, mem_arr_orig,
2532 sizeof(struct mem_array) * j);
2535 kfree(mem_arr_orig);
2538 mem_descr->num_elements = j;
2539 while ((i) || (j)) {
2540 for (j = mem_descr->num_elements; j > 0; j--) {
2541 dma_free_coherent(&phba->pcidev->dev,
2542 mem_descr->mem_array[j - 1].size,
2543 mem_descr->mem_array[j - 1].
2545 (unsigned long)mem_descr->
2547 bus_address.u.a64.address);
2551 kfree(mem_descr->mem_array);
2555 kfree(mem_arr_orig);
2556 kfree(phba->init_mem);
2557 kfree(phba->phwi_ctrlr->wrb_context);
2558 kfree(phba->phwi_ctrlr);
2562 static int beiscsi_get_memory(struct beiscsi_hba *phba)
2564 beiscsi_find_mem_req(phba);
2565 return beiscsi_alloc_mem(phba);
2568 static void iscsi_init_global_templates(struct beiscsi_hba *phba)
2570 struct pdu_data_out *pdata_out;
2571 struct pdu_nop_out *pnop_out;
2572 struct be_mem_descriptor *mem_descr;
2574 mem_descr = phba->init_mem;
2575 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
2577 (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
2578 memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2580 AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
2584 (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
2585 virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
2587 memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2588 AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
2589 AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
2590 AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
2593 static int beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
2595 struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
2596 struct hwi_context_memory *phwi_ctxt;
2597 struct wrb_handle *pwrb_handle = NULL;
2598 struct hwi_controller *phwi_ctrlr;
2599 struct hwi_wrb_context *pwrb_context;
2600 struct iscsi_wrb *pwrb = NULL;
2601 unsigned int num_cxn_wrbh = 0;
2602 unsigned int num_cxn_wrb = 0, j, idx = 0, index;
2604 mem_descr_wrbh = phba->init_mem;
2605 mem_descr_wrbh += HWI_MEM_WRBH;
2607 mem_descr_wrb = phba->init_mem;
2608 mem_descr_wrb += HWI_MEM_WRB;
2609 phwi_ctrlr = phba->phwi_ctrlr;
2611 /* Allocate memory for WRBQ */
2612 phwi_ctxt = phwi_ctrlr->phwi_ctxt;
2613 phwi_ctxt->be_wrbq = kcalloc(phba->params.cxns_per_ctrl,
2614 sizeof(struct be_queue_info),
2616 if (!phwi_ctxt->be_wrbq) {
2617 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2618 "BM_%d : WRBQ Mem Alloc Failed\n");
2622 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
2623 pwrb_context = &phwi_ctrlr->wrb_context[index];
2624 pwrb_context->pwrb_handle_base =
2625 kcalloc(phba->params.wrbs_per_cxn,
2626 sizeof(struct wrb_handle *),
2628 if (!pwrb_context->pwrb_handle_base) {
2629 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2630 "BM_%d : Mem Alloc Failed. Failing to load\n");
2631 goto init_wrb_hndl_failed;
2633 pwrb_context->pwrb_handle_basestd =
2634 kcalloc(phba->params.wrbs_per_cxn,
2635 sizeof(struct wrb_handle *),
2637 if (!pwrb_context->pwrb_handle_basestd) {
2638 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2639 "BM_%d : Mem Alloc Failed. Failing to load\n");
2640 goto init_wrb_hndl_failed;
2642 if (!num_cxn_wrbh) {
2644 mem_descr_wrbh->mem_array[idx].virtual_address;
2645 num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
2646 ((sizeof(struct wrb_handle)) *
2647 phba->params.wrbs_per_cxn));
2650 pwrb_context->alloc_index = 0;
2651 pwrb_context->wrb_handles_available = 0;
2652 pwrb_context->free_index = 0;
2655 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2656 pwrb_context->pwrb_handle_base[j] = pwrb_handle;
2657 pwrb_context->pwrb_handle_basestd[j] =
2659 pwrb_context->wrb_handles_available++;
2660 pwrb_handle->wrb_index = j;
2665 spin_lock_init(&pwrb_context->wrb_lock);
2668 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
2669 pwrb_context = &phwi_ctrlr->wrb_context[index];
2671 pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
2672 num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
2673 ((sizeof(struct iscsi_wrb) *
2674 phba->params.wrbs_per_cxn));
2679 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2680 pwrb_handle = pwrb_context->pwrb_handle_base[j];
2681 pwrb_handle->pwrb = pwrb;
2688 init_wrb_hndl_failed:
2689 for (j = index; j > 0; j--) {
2690 pwrb_context = &phwi_ctrlr->wrb_context[j];
2691 kfree(pwrb_context->pwrb_handle_base);
2692 kfree(pwrb_context->pwrb_handle_basestd);
2697 static int hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
2700 struct hwi_controller *phwi_ctrlr;
2701 struct hba_parameters *p = &phba->params;
2702 struct hd_async_context *pasync_ctx;
2703 struct hd_async_handle *pasync_header_h, *pasync_data_h;
2704 unsigned int index, idx, num_per_mem, num_async_data;
2705 struct be_mem_descriptor *mem_descr;
2707 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
2708 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
2709 /* get async_ctx for each ULP */
2710 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2711 mem_descr += (HWI_MEM_ASYNC_PDU_CONTEXT_ULP0 +
2712 (ulp_num * MEM_DESCR_OFFSET));
2714 phwi_ctrlr = phba->phwi_ctrlr;
2715 phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num] =
2716 (struct hd_async_context *)
2717 mem_descr->mem_array[0].virtual_address;
2719 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num];
2720 memset(pasync_ctx, 0, sizeof(*pasync_ctx));
2722 pasync_ctx->async_entry =
2723 (struct hd_async_entry *)
2724 ((long unsigned int)pasync_ctx +
2725 sizeof(struct hd_async_context));
2727 pasync_ctx->num_entries = BEISCSI_ASYNC_HDQ_SIZE(phba,
2729 /* setup header buffers */
2730 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2731 mem_descr += HWI_MEM_ASYNC_HEADER_BUF_ULP0 +
2732 (ulp_num * MEM_DESCR_OFFSET);
2733 if (mem_descr->mem_array[0].virtual_address) {
2734 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2735 "BM_%d : hwi_init_async_pdu_ctx"
2736 " HWI_MEM_ASYNC_HEADER_BUF_ULP%d va=%p\n",
2738 mem_descr->mem_array[0].
2741 beiscsi_log(phba, KERN_WARNING,
2743 "BM_%d : No Virtual address for ULP : %d\n",
2746 pasync_ctx->async_header.pi = 0;
2747 pasync_ctx->async_header.buffer_size = p->defpdu_hdr_sz;
2748 pasync_ctx->async_header.va_base =
2749 mem_descr->mem_array[0].virtual_address;
2751 pasync_ctx->async_header.pa_base.u.a64.address =
2752 mem_descr->mem_array[0].
2753 bus_address.u.a64.address;
2755 /* setup header buffer sgls */
2756 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2757 mem_descr += HWI_MEM_ASYNC_HEADER_RING_ULP0 +
2758 (ulp_num * MEM_DESCR_OFFSET);
2759 if (mem_descr->mem_array[0].virtual_address) {
2760 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2761 "BM_%d : hwi_init_async_pdu_ctx"
2762 " HWI_MEM_ASYNC_HEADER_RING_ULP%d va=%p\n",
2764 mem_descr->mem_array[0].
2767 beiscsi_log(phba, KERN_WARNING,
2769 "BM_%d : No Virtual address for ULP : %d\n",
2772 pasync_ctx->async_header.ring_base =
2773 mem_descr->mem_array[0].virtual_address;
2775 /* setup header buffer handles */
2776 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2777 mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE_ULP0 +
2778 (ulp_num * MEM_DESCR_OFFSET);
2779 if (mem_descr->mem_array[0].virtual_address) {
2780 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2781 "BM_%d : hwi_init_async_pdu_ctx"
2782 " HWI_MEM_ASYNC_HEADER_HANDLE_ULP%d va=%p\n",
2784 mem_descr->mem_array[0].
2787 beiscsi_log(phba, KERN_WARNING,
2789 "BM_%d : No Virtual address for ULP : %d\n",
2792 pasync_ctx->async_header.handle_base =
2793 mem_descr->mem_array[0].virtual_address;
2795 /* setup data buffer sgls */
2796 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2797 mem_descr += HWI_MEM_ASYNC_DATA_RING_ULP0 +
2798 (ulp_num * MEM_DESCR_OFFSET);
2799 if (mem_descr->mem_array[0].virtual_address) {
2800 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2801 "BM_%d : hwi_init_async_pdu_ctx"
2802 " HWI_MEM_ASYNC_DATA_RING_ULP%d va=%p\n",
2804 mem_descr->mem_array[0].
2807 beiscsi_log(phba, KERN_WARNING,
2809 "BM_%d : No Virtual address for ULP : %d\n",
2812 pasync_ctx->async_data.ring_base =
2813 mem_descr->mem_array[0].virtual_address;
2815 /* setup data buffer handles */
2816 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2817 mem_descr += HWI_MEM_ASYNC_DATA_HANDLE_ULP0 +
2818 (ulp_num * MEM_DESCR_OFFSET);
2819 if (!mem_descr->mem_array[0].virtual_address)
2820 beiscsi_log(phba, KERN_WARNING,
2822 "BM_%d : No Virtual address for ULP : %d\n",
2825 pasync_ctx->async_data.handle_base =
2826 mem_descr->mem_array[0].virtual_address;
2829 (struct hd_async_handle *)
2830 pasync_ctx->async_header.handle_base;
2832 (struct hd_async_handle *)
2833 pasync_ctx->async_data.handle_base;
2835 /* setup data buffers */
2836 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2837 mem_descr += HWI_MEM_ASYNC_DATA_BUF_ULP0 +
2838 (ulp_num * MEM_DESCR_OFFSET);
2839 if (mem_descr->mem_array[0].virtual_address) {
2840 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2841 "BM_%d : hwi_init_async_pdu_ctx"
2842 " HWI_MEM_ASYNC_DATA_BUF_ULP%d va=%p\n",
2844 mem_descr->mem_array[0].
2847 beiscsi_log(phba, KERN_WARNING,
2849 "BM_%d : No Virtual address for ULP : %d\n",
2853 pasync_ctx->async_data.pi = 0;
2854 pasync_ctx->async_data.buffer_size = p->defpdu_data_sz;
2855 pasync_ctx->async_data.va_base =
2856 mem_descr->mem_array[idx].virtual_address;
2857 pasync_ctx->async_data.pa_base.u.a64.address =
2858 mem_descr->mem_array[idx].
2859 bus_address.u.a64.address;
2861 num_async_data = ((mem_descr->mem_array[idx].size) /
2862 phba->params.defpdu_data_sz);
2865 for (index = 0; index < BEISCSI_ASYNC_HDQ_SIZE
2866 (phba, ulp_num); index++) {
2867 pasync_header_h->cri = -1;
2868 pasync_header_h->is_header = 1;
2869 pasync_header_h->index = index;
2870 INIT_LIST_HEAD(&pasync_header_h->link);
2871 pasync_header_h->pbuffer =
2872 (void *)((unsigned long)
2874 async_header.va_base) +
2875 (p->defpdu_hdr_sz * index));
2877 pasync_header_h->pa.u.a64.address =
2878 pasync_ctx->async_header.pa_base.u.a64.
2879 address + (p->defpdu_hdr_sz * index);
2881 pasync_ctx->async_entry[index].header =
2884 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
2887 pasync_data_h->cri = -1;
2888 pasync_data_h->is_header = 0;
2889 pasync_data_h->index = index;
2890 INIT_LIST_HEAD(&pasync_data_h->link);
2892 if (!num_async_data) {
2895 pasync_ctx->async_data.va_base =
2896 mem_descr->mem_array[idx].
2898 pasync_ctx->async_data.pa_base.u.
2900 mem_descr->mem_array[idx].
2901 bus_address.u.a64.address;
2903 ((mem_descr->mem_array[idx].
2905 phba->params.defpdu_data_sz);
2907 pasync_data_h->pbuffer =
2908 (void *)((unsigned long)
2909 (pasync_ctx->async_data.va_base) +
2910 (p->defpdu_data_sz * num_per_mem));
2912 pasync_data_h->pa.u.a64.address =
2913 pasync_ctx->async_data.pa_base.u.a64.
2914 address + (p->defpdu_data_sz *
2919 pasync_ctx->async_entry[index].data =
2930 be_sgl_create_contiguous(void *virtual_address,
2931 u64 physical_address, u32 length,
2932 struct be_dma_mem *sgl)
2934 WARN_ON(!virtual_address);
2935 WARN_ON(!physical_address);
2939 sgl->va = virtual_address;
2940 sgl->dma = (unsigned long)physical_address;
2946 static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
2948 memset(sgl, 0, sizeof(*sgl));
2952 hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
2953 struct mem_array *pmem, struct be_dma_mem *sgl)
2956 be_sgl_destroy_contiguous(sgl);
2958 be_sgl_create_contiguous(pmem->virtual_address,
2959 pmem->bus_address.u.a64.address,
2964 hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
2965 struct mem_array *pmem, struct be_dma_mem *sgl)
2968 be_sgl_destroy_contiguous(sgl);
2970 be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
2971 pmem->bus_address.u.a64.address,
2975 static int be_fill_queue(struct be_queue_info *q,
2976 u16 len, u16 entry_size, void *vaddress)
2978 struct be_dma_mem *mem = &q->dma_mem;
2980 memset(q, 0, sizeof(*q));
2982 q->entry_size = entry_size;
2983 mem->size = len * entry_size;
2987 memset(mem->va, 0, mem->size);
2991 static int beiscsi_create_eqs(struct beiscsi_hba *phba,
2992 struct hwi_context_memory *phwi_context)
2994 int ret = -ENOMEM, eq_for_mcc;
2995 unsigned int i, num_eq_pages;
2996 struct be_queue_info *eq;
2997 struct be_dma_mem *mem;
3001 num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries *
3002 sizeof(struct be_eq_entry));
3004 if (phba->pcidev->msix_enabled)
3008 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
3009 eq = &phwi_context->be_eq[i].q;
3011 phwi_context->be_eq[i].phba = phba;
3012 eq_vaddress = dma_alloc_coherent(&phba->pcidev->dev,
3013 num_eq_pages * PAGE_SIZE,
3014 &paddr, GFP_KERNEL);
3017 goto create_eq_error;
3020 mem->va = eq_vaddress;
3021 ret = be_fill_queue(eq, phba->params.num_eq_entries,
3022 sizeof(struct be_eq_entry), eq_vaddress);
3024 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3025 "BM_%d : be_fill_queue Failed for EQ\n");
3026 goto create_eq_error;
3030 ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
3031 BEISCSI_EQ_DELAY_DEF);
3033 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3034 "BM_%d : beiscsi_cmd_eq_create Failed for EQ\n");
3035 goto create_eq_error;
3038 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3039 "BM_%d : eqid = %d\n",
3040 phwi_context->be_eq[i].q.id);
3045 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
3046 eq = &phwi_context->be_eq[i].q;
3049 dma_free_coherent(&phba->pcidev->dev, num_eq_pages
3056 static int beiscsi_create_cqs(struct beiscsi_hba *phba,
3057 struct hwi_context_memory *phwi_context)
3059 unsigned int i, num_cq_pages;
3060 struct be_queue_info *cq, *eq;
3061 struct be_dma_mem *mem;
3062 struct be_eq_obj *pbe_eq;
3067 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries *
3068 sizeof(struct sol_cqe));
3070 for (i = 0; i < phba->num_cpus; i++) {
3071 cq = &phwi_context->be_cq[i];
3072 eq = &phwi_context->be_eq[i].q;
3073 pbe_eq = &phwi_context->be_eq[i];
3075 pbe_eq->phba = phba;
3077 cq_vaddress = dma_alloc_coherent(&phba->pcidev->dev,
3078 num_cq_pages * PAGE_SIZE,
3079 &paddr, GFP_KERNEL);
3082 goto create_cq_error;
3085 ret = be_fill_queue(cq, phba->params.num_cq_entries,
3086 sizeof(struct sol_cqe), cq_vaddress);
3088 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3089 "BM_%d : be_fill_queue Failed for ISCSI CQ\n");
3090 goto create_cq_error;
3094 ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
3097 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3098 "BM_%d : beiscsi_cmd_eq_create Failed for ISCSI CQ\n");
3099 goto create_cq_error;
3101 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3102 "BM_%d : iscsi cq_id is %d for eq_id %d\n"
3103 "iSCSI CQ CREATED\n", cq->id, eq->id);
3108 for (i = 0; i < phba->num_cpus; i++) {
3109 cq = &phwi_context->be_cq[i];
3112 dma_free_coherent(&phba->pcidev->dev, num_cq_pages
3120 beiscsi_create_def_hdr(struct beiscsi_hba *phba,
3121 struct hwi_context_memory *phwi_context,
3122 struct hwi_controller *phwi_ctrlr,
3123 unsigned int def_pdu_ring_sz, uint8_t ulp_num)
3127 struct be_queue_info *dq, *cq;
3128 struct be_dma_mem *mem;
3129 struct be_mem_descriptor *mem_descr;
3133 dq = &phwi_context->be_def_hdrq[ulp_num];
3134 cq = &phwi_context->be_cq[0];
3136 mem_descr = phba->init_mem;
3137 mem_descr += HWI_MEM_ASYNC_HEADER_RING_ULP0 +
3138 (ulp_num * MEM_DESCR_OFFSET);
3139 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3140 ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
3141 sizeof(struct phys_addr),
3142 sizeof(struct phys_addr), dq_vaddress);
3144 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3145 "BM_%d : be_fill_queue Failed for DEF PDU HDR on ULP : %d\n",
3150 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3151 bus_address.u.a64.address;
3152 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
3154 phba->params.defpdu_hdr_sz,
3155 BEISCSI_DEFQ_HDR, ulp_num);
3157 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3158 "BM_%d : be_cmd_create_default_pdu_queue Failed DEFHDR on ULP : %d\n",
3164 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3165 "BM_%d : iscsi hdr def pdu id for ULP : %d is %d\n",
3167 phwi_context->be_def_hdrq[ulp_num].id);
3172 beiscsi_create_def_data(struct beiscsi_hba *phba,
3173 struct hwi_context_memory *phwi_context,
3174 struct hwi_controller *phwi_ctrlr,
3175 unsigned int def_pdu_ring_sz, uint8_t ulp_num)
3179 struct be_queue_info *dataq, *cq;
3180 struct be_dma_mem *mem;
3181 struct be_mem_descriptor *mem_descr;
3185 dataq = &phwi_context->be_def_dataq[ulp_num];
3186 cq = &phwi_context->be_cq[0];
3187 mem = &dataq->dma_mem;
3188 mem_descr = phba->init_mem;
3189 mem_descr += HWI_MEM_ASYNC_DATA_RING_ULP0 +
3190 (ulp_num * MEM_DESCR_OFFSET);
3191 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3192 ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
3193 sizeof(struct phys_addr),
3194 sizeof(struct phys_addr), dq_vaddress);
3196 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3197 "BM_%d : be_fill_queue Failed for DEF PDU "
3198 "DATA on ULP : %d\n",
3203 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3204 bus_address.u.a64.address;
3205 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
3207 phba->params.defpdu_data_sz,
3208 BEISCSI_DEFQ_DATA, ulp_num);
3210 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3211 "BM_%d be_cmd_create_default_pdu_queue"
3212 " Failed for DEF PDU DATA on ULP : %d\n",
3217 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3218 "BM_%d : iscsi def data id on ULP : %d is %d\n",
3220 phwi_context->be_def_dataq[ulp_num].id);
3222 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3223 "BM_%d : DEFAULT PDU DATA RING CREATED on ULP : %d\n",
3230 beiscsi_post_template_hdr(struct beiscsi_hba *phba)
3232 struct be_mem_descriptor *mem_descr;
3233 struct mem_array *pm_arr;
3234 struct be_dma_mem sgl;
3235 int status, ulp_num;
3237 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3238 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3239 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
3240 mem_descr += HWI_MEM_TEMPLATE_HDR_ULP0 +
3241 (ulp_num * MEM_DESCR_OFFSET);
3242 pm_arr = mem_descr->mem_array;
3244 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
3245 status = be_cmd_iscsi_post_template_hdr(
3249 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3250 "BM_%d : Post Template HDR Failed for "
3251 "ULP_%d\n", ulp_num);
3255 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3256 "BM_%d : Template HDR Pages Posted for "
3257 "ULP_%d\n", ulp_num);
3264 beiscsi_post_pages(struct beiscsi_hba *phba)
3266 struct be_mem_descriptor *mem_descr;
3267 struct mem_array *pm_arr;
3268 unsigned int page_offset, i;
3269 struct be_dma_mem sgl;
3270 int status, ulp_num = 0;
3272 mem_descr = phba->init_mem;
3273 mem_descr += HWI_MEM_SGE;
3274 pm_arr = mem_descr->mem_array;
3276 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3277 if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
3280 page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
3281 phba->fw_config.iscsi_icd_start[ulp_num]) / PAGE_SIZE;
3282 for (i = 0; i < mem_descr->num_elements; i++) {
3283 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
3284 status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
3286 (pm_arr->size / PAGE_SIZE));
3287 page_offset += pm_arr->size / PAGE_SIZE;
3289 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3290 "BM_%d : post sgl failed.\n");
3295 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3296 "BM_%d : POSTED PAGES\n");
3300 static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
3302 struct be_dma_mem *mem = &q->dma_mem;
3304 dma_free_coherent(&phba->pcidev->dev, mem->size,
3310 static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
3311 u16 len, u16 entry_size)
3313 struct be_dma_mem *mem = &q->dma_mem;
3315 memset(q, 0, sizeof(*q));
3317 q->entry_size = entry_size;
3318 mem->size = len * entry_size;
3319 mem->va = dma_alloc_coherent(&phba->pcidev->dev, mem->size, &mem->dma,
3327 beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
3328 struct hwi_context_memory *phwi_context,
3329 struct hwi_controller *phwi_ctrlr)
3331 unsigned int num_wrb_rings;
3333 unsigned int idx, num, i, ulp_num;
3334 struct mem_array *pwrb_arr;
3336 struct be_dma_mem sgl;
3337 struct be_mem_descriptor *mem_descr;
3338 struct hwi_wrb_context *pwrb_context;
3340 uint8_t ulp_count = 0, ulp_base_num = 0;
3341 uint16_t cid_count_ulp[BEISCSI_ULP_COUNT] = { 0 };
3344 mem_descr = phba->init_mem;
3345 mem_descr += HWI_MEM_WRB;
3346 pwrb_arr = kmalloc_array(phba->params.cxns_per_ctrl,
3350 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3351 "BM_%d : Memory alloc failed in create wrb ring.\n");
3354 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3355 pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
3356 num_wrb_rings = mem_descr->mem_array[idx].size /
3357 (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
3359 for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
3360 if (num_wrb_rings) {
3361 pwrb_arr[num].virtual_address = wrb_vaddr;
3362 pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
3363 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3364 sizeof(struct iscsi_wrb);
3365 wrb_vaddr += pwrb_arr[num].size;
3366 pa_addr_lo += pwrb_arr[num].size;
3370 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3371 pa_addr_lo = mem_descr->mem_array[idx].
3372 bus_address.u.a64.address;
3373 num_wrb_rings = mem_descr->mem_array[idx].size /
3374 (phba->params.wrbs_per_cxn *
3375 sizeof(struct iscsi_wrb));
3376 pwrb_arr[num].virtual_address = wrb_vaddr;
3377 pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
3378 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3379 sizeof(struct iscsi_wrb);
3380 wrb_vaddr += pwrb_arr[num].size;
3381 pa_addr_lo += pwrb_arr[num].size;
3386 /* Get the ULP Count */
3387 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3388 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3390 ulp_base_num = ulp_num;
3391 cid_count_ulp[ulp_num] =
3392 BEISCSI_GET_CID_COUNT(phba, ulp_num);
3395 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3396 if (ulp_count > 1) {
3397 ulp_base_num = (ulp_base_num + 1) % BEISCSI_ULP_COUNT;
3399 if (!cid_count_ulp[ulp_base_num])
3400 ulp_base_num = (ulp_base_num + 1) %
3403 cid_count_ulp[ulp_base_num]--;
3407 hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
3408 status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
3409 &phwi_context->be_wrbq[i],
3410 &phwi_ctrlr->wrb_context[i],
3413 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3414 "BM_%d : wrbq create failed.");
3418 pwrb_context = &phwi_ctrlr->wrb_context[i];
3419 BE_SET_CID_TO_CRI(i, pwrb_context->cid);
3425 static void free_wrb_handles(struct beiscsi_hba *phba)
3428 struct hwi_controller *phwi_ctrlr;
3429 struct hwi_wrb_context *pwrb_context;
3431 phwi_ctrlr = phba->phwi_ctrlr;
3432 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
3433 pwrb_context = &phwi_ctrlr->wrb_context[index];
3434 kfree(pwrb_context->pwrb_handle_base);
3435 kfree(pwrb_context->pwrb_handle_basestd);
3439 static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
3441 struct be_ctrl_info *ctrl = &phba->ctrl;
3442 struct be_dma_mem *ptag_mem;
3443 struct be_queue_info *q;
3446 q = &phba->ctrl.mcc_obj.q;
3447 for (i = 0; i < MAX_MCC_CMD; i++) {
3449 if (!test_bit(MCC_TAG_STATE_RUNNING,
3450 &ctrl->ptag_state[tag].tag_state))
3453 if (test_bit(MCC_TAG_STATE_TIMEOUT,
3454 &ctrl->ptag_state[tag].tag_state)) {
3455 ptag_mem = &ctrl->ptag_state[tag].tag_mem_state;
3456 if (ptag_mem->size) {
3457 dma_free_coherent(&ctrl->pdev->dev,
3466 * If MCC is still active and waiting then wake up the process.
3467 * We are here only because port is going offline. The process
3468 * sees that (BEISCSI_HBA_ONLINE is cleared) and EIO error is
3469 * returned for the operation and allocated memory cleaned up.
3471 if (waitqueue_active(&ctrl->mcc_wait[tag])) {
3472 ctrl->mcc_tag_status[tag] = MCC_STATUS_FAILED;
3473 ctrl->mcc_tag_status[tag] |= CQE_VALID_MASK;
3474 wake_up_interruptible(&ctrl->mcc_wait[tag]);
3476 * Control tag info gets reinitialized in enable
3477 * so wait for the process to clear running state.
3479 while (test_bit(MCC_TAG_STATE_RUNNING,
3480 &ctrl->ptag_state[tag].tag_state))
3481 schedule_timeout_uninterruptible(HZ);
3484 * For MCC with tag_states MCC_TAG_STATE_ASYNC and
3485 * MCC_TAG_STATE_IGNORE nothing needs to done.
3489 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
3490 be_queue_free(phba, q);
3493 q = &phba->ctrl.mcc_obj.cq;
3495 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3496 be_queue_free(phba, q);
3500 static int be_mcc_queues_create(struct beiscsi_hba *phba,
3501 struct hwi_context_memory *phwi_context)
3503 struct be_queue_info *q, *cq;
3504 struct be_ctrl_info *ctrl = &phba->ctrl;
3506 /* Alloc MCC compl queue */
3507 cq = &phba->ctrl.mcc_obj.cq;
3508 if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
3509 sizeof(struct be_mcc_compl)))
3511 /* Ask BE to create MCC compl queue; */
3512 if (phba->pcidev->msix_enabled) {
3513 if (beiscsi_cmd_cq_create(ctrl, cq,
3514 &phwi_context->be_eq[phba->num_cpus].q,
3518 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
3523 /* Alloc MCC queue */
3524 q = &phba->ctrl.mcc_obj.q;
3525 if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
3526 goto mcc_cq_destroy;
3528 /* Ask BE to create MCC queue */
3529 if (beiscsi_cmd_mccq_create(phba, q, cq))
3535 be_queue_free(phba, q);
3537 beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
3539 be_queue_free(phba, cq);
3544 static void be2iscsi_enable_msix(struct beiscsi_hba *phba)
3548 switch (phba->generation) {
3551 nvec = BEISCSI_MAX_NUM_CPUS + 1;
3554 nvec = phba->fw_config.eqid_count;
3561 /* if eqid_count == 1 fall back to INTX */
3562 if (enable_msix && nvec > 1) {
3563 struct irq_affinity desc = { .post_vectors = 1 };
3565 if (pci_alloc_irq_vectors_affinity(phba->pcidev, 2, nvec,
3566 PCI_IRQ_MSIX | PCI_IRQ_AFFINITY, &desc) < 0) {
3567 phba->num_cpus = nvec - 1;
3575 static void hwi_purge_eq(struct beiscsi_hba *phba)
3577 struct hwi_controller *phwi_ctrlr;
3578 struct hwi_context_memory *phwi_context;
3579 struct be_queue_info *eq;
3580 struct be_eq_entry *eqe = NULL;
3582 unsigned int num_processed;
3584 if (beiscsi_hba_in_error(phba))
3587 phwi_ctrlr = phba->phwi_ctrlr;
3588 phwi_context = phwi_ctrlr->phwi_ctxt;
3589 if (phba->pcidev->msix_enabled)
3594 for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
3595 eq = &phwi_context->be_eq[i].q;
3596 eqe = queue_tail_node(eq);
3598 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
3600 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
3602 eqe = queue_tail_node(eq);
3607 hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
3611 static void hwi_cleanup_port(struct beiscsi_hba *phba)
3613 struct be_queue_info *q;
3614 struct be_ctrl_info *ctrl = &phba->ctrl;
3615 struct hwi_controller *phwi_ctrlr;
3616 struct hwi_context_memory *phwi_context;
3617 int i, eq_for_mcc, ulp_num;
3619 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3620 if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
3621 beiscsi_cmd_iscsi_cleanup(phba, ulp_num);
3624 * Purge all EQ entries that may have been left out. This is to
3625 * workaround a problem we've seen occasionally where driver gets an
3626 * interrupt with EQ entry bit set after stopping the controller.
3630 phwi_ctrlr = phba->phwi_ctrlr;
3631 phwi_context = phwi_ctrlr->phwi_ctxt;
3633 be_cmd_iscsi_remove_template_hdr(ctrl);
3635 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3636 q = &phwi_context->be_wrbq[i];
3638 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
3640 kfree(phwi_context->be_wrbq);
3641 free_wrb_handles(phba);
3643 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3644 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3646 q = &phwi_context->be_def_hdrq[ulp_num];
3648 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3650 q = &phwi_context->be_def_dataq[ulp_num];
3652 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3656 beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
3658 for (i = 0; i < (phba->num_cpus); i++) {
3659 q = &phwi_context->be_cq[i];
3661 be_queue_free(phba, q);
3662 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3666 be_mcc_queues_destroy(phba);
3667 if (phba->pcidev->msix_enabled)
3671 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
3672 q = &phwi_context->be_eq[i].q;
3674 be_queue_free(phba, q);
3675 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
3678 /* this ensures complete FW cleanup */
3679 beiscsi_cmd_function_reset(phba);
3680 /* last communication, indicate driver is unloading */
3681 beiscsi_cmd_special_wrb(&phba->ctrl, 0);
3684 static int hwi_init_port(struct beiscsi_hba *phba)
3686 struct hwi_controller *phwi_ctrlr;
3687 struct hwi_context_memory *phwi_context;
3688 unsigned int def_pdu_ring_sz;
3689 struct be_ctrl_info *ctrl = &phba->ctrl;
3690 int status, ulp_num;
3693 phwi_ctrlr = phba->phwi_ctrlr;
3694 phwi_context = phwi_ctrlr->phwi_ctxt;
3695 /* set port optic state to unknown */
3696 phba->optic_state = 0xff;
3698 status = beiscsi_create_eqs(phba, phwi_context);
3700 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3701 "BM_%d : EQ not created\n");
3705 status = be_mcc_queues_create(phba, phwi_context);
3709 status = beiscsi_check_supported_fw(ctrl, phba);
3711 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3712 "BM_%d : Unsupported fw version\n");
3716 status = beiscsi_create_cqs(phba, phwi_context);
3718 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3719 "BM_%d : CQ not created\n");
3723 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3724 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3725 nbufs = phwi_context->pasync_ctx[ulp_num]->num_entries;
3726 def_pdu_ring_sz = nbufs * sizeof(struct phys_addr);
3728 status = beiscsi_create_def_hdr(phba, phwi_context,
3733 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3734 "BM_%d : Default Header not created for ULP : %d\n",
3739 status = beiscsi_create_def_data(phba, phwi_context,
3744 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3745 "BM_%d : Default Data not created for ULP : %d\n",
3750 * Now that the default PDU rings have been created,
3751 * let EP know about it.
3753 beiscsi_hdq_post_handles(phba, BEISCSI_DEFQ_HDR,
3755 beiscsi_hdq_post_handles(phba, BEISCSI_DEFQ_DATA,
3760 status = beiscsi_post_pages(phba);
3762 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3763 "BM_%d : Post SGL Pages Failed\n");
3767 status = beiscsi_post_template_hdr(phba);
3769 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3770 "BM_%d : Template HDR Posting for CXN Failed\n");
3773 status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
3775 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3776 "BM_%d : WRB Rings not created\n");
3780 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3781 uint16_t async_arr_idx = 0;
3783 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3785 struct hd_async_context *pasync_ctx;
3787 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(
3788 phwi_ctrlr, ulp_num);
3790 phba->params.cxns_per_ctrl; cri++) {
3791 if (ulp_num == BEISCSI_GET_ULP_FROM_CRI
3793 pasync_ctx->cid_to_async_cri_map[
3794 phwi_ctrlr->wrb_context[cri].cid] =
3800 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3801 "BM_%d : hwi_init_port success\n");
3805 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3806 "BM_%d : hwi_init_port failed");
3807 hwi_cleanup_port(phba);
3811 static int hwi_init_controller(struct beiscsi_hba *phba)
3813 struct hwi_controller *phwi_ctrlr;
3815 phwi_ctrlr = phba->phwi_ctrlr;
3816 if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
3817 phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
3818 init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
3819 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3820 "BM_%d : phwi_ctrlr->phwi_ctxt=%p\n",
3821 phwi_ctrlr->phwi_ctxt);
3823 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3824 "BM_%d : HWI_MEM_ADDN_CONTEXT is more "
3825 "than one element.Failing to load\n");
3829 iscsi_init_global_templates(phba);
3830 if (beiscsi_init_wrb_handle(phba))
3833 if (hwi_init_async_pdu_ctx(phba)) {
3834 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3835 "BM_%d : hwi_init_async_pdu_ctx failed\n");
3839 if (hwi_init_port(phba) != 0) {
3840 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3841 "BM_%d : hwi_init_controller failed\n");
3848 static void beiscsi_free_mem(struct beiscsi_hba *phba)
3850 struct be_mem_descriptor *mem_descr;
3853 mem_descr = phba->init_mem;
3854 for (i = 0; i < SE_MEM_MAX; i++) {
3855 for (j = mem_descr->num_elements; j > 0; j--) {
3856 dma_free_coherent(&phba->pcidev->dev,
3857 mem_descr->mem_array[j - 1].size,
3858 mem_descr->mem_array[j - 1].virtual_address,
3859 (unsigned long)mem_descr->mem_array[j - 1].
3860 bus_address.u.a64.address);
3863 kfree(mem_descr->mem_array);
3866 kfree(phba->init_mem);
3867 kfree(phba->phwi_ctrlr->wrb_context);
3868 kfree(phba->phwi_ctrlr);
3871 static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
3873 struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
3874 struct sgl_handle *psgl_handle;
3875 struct iscsi_sge *pfrag;
3876 unsigned int arr_index, i, idx;
3877 unsigned int ulp_icd_start, ulp_num = 0;
3879 phba->io_sgl_hndl_avbl = 0;
3880 phba->eh_sgl_hndl_avbl = 0;
3882 mem_descr_sglh = phba->init_mem;
3883 mem_descr_sglh += HWI_MEM_SGLH;
3884 if (1 == mem_descr_sglh->num_elements) {
3885 phba->io_sgl_hndl_base = kcalloc(phba->params.ios_per_ctrl,
3886 sizeof(struct sgl_handle *),
3888 if (!phba->io_sgl_hndl_base) {
3889 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3890 "BM_%d : Mem Alloc Failed. Failing to load\n");
3893 phba->eh_sgl_hndl_base =
3894 kcalloc(phba->params.icds_per_ctrl -
3895 phba->params.ios_per_ctrl,
3896 sizeof(struct sgl_handle *), GFP_KERNEL);
3897 if (!phba->eh_sgl_hndl_base) {
3898 kfree(phba->io_sgl_hndl_base);
3899 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3900 "BM_%d : Mem Alloc Failed. Failing to load\n");
3904 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3905 "BM_%d : HWI_MEM_SGLH is more than one element."
3906 "Failing to load\n");
3912 while (idx < mem_descr_sglh->num_elements) {
3913 psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
3915 for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
3916 sizeof(struct sgl_handle)); i++) {
3917 if (arr_index < phba->params.ios_per_ctrl) {
3918 phba->io_sgl_hndl_base[arr_index] = psgl_handle;
3919 phba->io_sgl_hndl_avbl++;
3922 phba->eh_sgl_hndl_base[arr_index -
3923 phba->params.ios_per_ctrl] =
3926 phba->eh_sgl_hndl_avbl++;
3932 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3933 "BM_%d : phba->io_sgl_hndl_avbl=%d "
3934 "phba->eh_sgl_hndl_avbl=%d\n",
3935 phba->io_sgl_hndl_avbl,
3936 phba->eh_sgl_hndl_avbl);
3938 mem_descr_sg = phba->init_mem;
3939 mem_descr_sg += HWI_MEM_SGE;
3940 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3941 "\n BM_%d : mem_descr_sg->num_elements=%d\n",
3942 mem_descr_sg->num_elements);
3944 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3945 if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
3948 ulp_icd_start = phba->fw_config.iscsi_icd_start[ulp_num];
3952 while (idx < mem_descr_sg->num_elements) {
3953 pfrag = mem_descr_sg->mem_array[idx].virtual_address;
3956 i < (mem_descr_sg->mem_array[idx].size) /
3957 (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
3959 if (arr_index < phba->params.ios_per_ctrl)
3960 psgl_handle = phba->io_sgl_hndl_base[arr_index];
3962 psgl_handle = phba->eh_sgl_hndl_base[arr_index -
3963 phba->params.ios_per_ctrl];
3964 psgl_handle->pfrag = pfrag;
3965 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
3966 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
3967 pfrag += phba->params.num_sge_per_io;
3968 psgl_handle->sgl_index = ulp_icd_start + arr_index++;
3972 phba->io_sgl_free_index = 0;
3973 phba->io_sgl_alloc_index = 0;
3974 phba->eh_sgl_free_index = 0;
3975 phba->eh_sgl_alloc_index = 0;
3979 static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
3982 uint16_t i, ulp_num;
3983 struct ulp_cid_info *ptr_cid_info = NULL;
3985 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3986 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
3987 ptr_cid_info = kzalloc(sizeof(struct ulp_cid_info),
3990 if (!ptr_cid_info) {
3995 /* Allocate memory for CID array */
3996 ptr_cid_info->cid_array =
3997 kcalloc(BEISCSI_GET_CID_COUNT(phba, ulp_num),
3998 sizeof(*ptr_cid_info->cid_array),
4000 if (!ptr_cid_info->cid_array) {
4001 kfree(ptr_cid_info);
4002 ptr_cid_info = NULL;
4007 ptr_cid_info->avlbl_cids = BEISCSI_GET_CID_COUNT(
4010 /* Save the cid_info_array ptr */
4011 phba->cid_array_info[ulp_num] = ptr_cid_info;
4014 phba->ep_array = kcalloc(phba->params.cxns_per_ctrl,
4015 sizeof(struct iscsi_endpoint *),
4017 if (!phba->ep_array) {
4023 phba->conn_table = kcalloc(phba->params.cxns_per_ctrl,
4024 sizeof(struct beiscsi_conn *),
4026 if (!phba->conn_table) {
4027 kfree(phba->ep_array);
4028 phba->ep_array = NULL;
4034 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
4035 ulp_num = phba->phwi_ctrlr->wrb_context[i].ulp_num;
4037 ptr_cid_info = phba->cid_array_info[ulp_num];
4038 ptr_cid_info->cid_array[ptr_cid_info->cid_alloc++] =
4039 phba->phwi_ctrlr->wrb_context[i].cid;
4043 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4044 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4045 ptr_cid_info = phba->cid_array_info[ulp_num];
4047 ptr_cid_info->cid_alloc = 0;
4048 ptr_cid_info->cid_free = 0;
4054 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4055 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4056 ptr_cid_info = phba->cid_array_info[ulp_num];
4059 kfree(ptr_cid_info->cid_array);
4060 kfree(ptr_cid_info);
4061 phba->cid_array_info[ulp_num] = NULL;
4069 static void hwi_enable_intr(struct beiscsi_hba *phba)
4071 struct be_ctrl_info *ctrl = &phba->ctrl;
4072 struct hwi_controller *phwi_ctrlr;
4073 struct hwi_context_memory *phwi_context;
4074 struct be_queue_info *eq;
4079 phwi_ctrlr = phba->phwi_ctrlr;
4080 phwi_context = phwi_ctrlr->phwi_ctxt;
4082 addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
4083 PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
4084 reg = ioread32(addr);
4086 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4088 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4089 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4090 "BM_%d : reg =x%08x addr=%p\n", reg, addr);
4091 iowrite32(reg, addr);
4094 if (!phba->pcidev->msix_enabled) {
4095 eq = &phwi_context->be_eq[0].q;
4096 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4097 "BM_%d : eq->id=%d\n", eq->id);
4099 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
4101 for (i = 0; i <= phba->num_cpus; i++) {
4102 eq = &phwi_context->be_eq[i].q;
4103 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4104 "BM_%d : eq->id=%d\n", eq->id);
4105 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
4110 static void hwi_disable_intr(struct beiscsi_hba *phba)
4112 struct be_ctrl_info *ctrl = &phba->ctrl;
4114 u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
4115 u32 reg = ioread32(addr);
4117 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4119 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4120 iowrite32(reg, addr);
4122 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
4123 "BM_%d : In hwi_disable_intr, Already Disabled\n");
4126 static int beiscsi_init_port(struct beiscsi_hba *phba)
4130 ret = hwi_init_controller(phba);
4132 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4133 "BM_%d : init controller failed\n");
4136 ret = beiscsi_init_sgl_handle(phba);
4138 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4139 "BM_%d : init sgl handles failed\n");
4143 ret = hba_setup_cid_tbls(phba);
4145 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4146 "BM_%d : setup CID table failed\n");
4147 kfree(phba->io_sgl_hndl_base);
4148 kfree(phba->eh_sgl_hndl_base);
4154 hwi_cleanup_port(phba);
4158 static void beiscsi_cleanup_port(struct beiscsi_hba *phba)
4160 struct ulp_cid_info *ptr_cid_info = NULL;
4163 kfree(phba->io_sgl_hndl_base);
4164 kfree(phba->eh_sgl_hndl_base);
4165 kfree(phba->ep_array);
4166 kfree(phba->conn_table);
4168 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4169 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4170 ptr_cid_info = phba->cid_array_info[ulp_num];
4173 kfree(ptr_cid_info->cid_array);
4174 kfree(ptr_cid_info);
4175 phba->cid_array_info[ulp_num] = NULL;
4182 * beiscsi_free_mgmt_task_handles()- Free driver CXN resources
4183 * @beiscsi_conn: ptr to the conn to be cleaned up
4184 * @task: ptr to iscsi_task resource to be freed.
4186 * Free driver mgmt resources binded to CXN.
4189 beiscsi_free_mgmt_task_handles(struct beiscsi_conn *beiscsi_conn,
4190 struct iscsi_task *task)
4192 struct beiscsi_io_task *io_task;
4193 struct beiscsi_hba *phba = beiscsi_conn->phba;
4194 struct hwi_wrb_context *pwrb_context;
4195 struct hwi_controller *phwi_ctrlr;
4196 uint16_t cri_index = BE_GET_CRI_FROM_CID(
4197 beiscsi_conn->beiscsi_conn_cid);
4199 phwi_ctrlr = phba->phwi_ctrlr;
4200 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
4202 io_task = task->dd_data;
4204 if (io_task->pwrb_handle) {
4205 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
4206 io_task->pwrb_handle = NULL;
4209 if (io_task->psgl_handle) {
4210 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
4211 io_task->psgl_handle = NULL;
4214 if (io_task->mtask_addr) {
4215 dma_unmap_single(&phba->pcidev->dev,
4216 io_task->mtask_addr,
4217 io_task->mtask_data_count,
4219 io_task->mtask_addr = 0;
4224 * beiscsi_cleanup_task()- Free driver resources of the task
4225 * @task: ptr to the iscsi task
4228 static void beiscsi_cleanup_task(struct iscsi_task *task)
4230 struct beiscsi_io_task *io_task = task->dd_data;
4231 struct iscsi_conn *conn = task->conn;
4232 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4233 struct beiscsi_hba *phba = beiscsi_conn->phba;
4234 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4235 struct hwi_wrb_context *pwrb_context;
4236 struct hwi_controller *phwi_ctrlr;
4237 uint16_t cri_index = BE_GET_CRI_FROM_CID(
4238 beiscsi_conn->beiscsi_conn_cid);
4240 phwi_ctrlr = phba->phwi_ctrlr;
4241 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
4243 if (io_task->cmd_bhs) {
4244 dma_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4245 io_task->bhs_pa.u.a64.address);
4246 io_task->cmd_bhs = NULL;
4251 if (io_task->pwrb_handle) {
4252 free_wrb_handle(phba, pwrb_context,
4253 io_task->pwrb_handle);
4254 io_task->pwrb_handle = NULL;
4257 if (io_task->psgl_handle) {
4258 free_io_sgl_handle(phba, io_task->psgl_handle);
4259 io_task->psgl_handle = NULL;
4262 if (io_task->scsi_cmnd) {
4263 if (io_task->num_sg)
4264 scsi_dma_unmap(io_task->scsi_cmnd);
4265 io_task->scsi_cmnd = NULL;
4268 if (!beiscsi_conn->login_in_progress)
4269 beiscsi_free_mgmt_task_handles(beiscsi_conn, task);
4274 beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
4275 struct beiscsi_offload_params *params)
4277 struct wrb_handle *pwrb_handle;
4278 struct hwi_wrb_context *pwrb_context = NULL;
4279 struct beiscsi_hba *phba = beiscsi_conn->phba;
4280 struct iscsi_task *task = beiscsi_conn->task;
4281 struct iscsi_session *session = task->conn->session;
4285 * We can always use 0 here because it is reserved by libiscsi for
4286 * login/startup related tasks.
4288 beiscsi_conn->login_in_progress = 0;
4289 spin_lock_bh(&session->back_lock);
4290 beiscsi_cleanup_task(task);
4291 spin_unlock_bh(&session->back_lock);
4293 pwrb_handle = alloc_wrb_handle(phba, beiscsi_conn->beiscsi_conn_cid,
4296 /* Check for the adapter family */
4297 if (is_chip_be2_be3r(phba))
4298 beiscsi_offload_cxn_v0(params, pwrb_handle,
4302 beiscsi_offload_cxn_v2(params, pwrb_handle,
4305 be_dws_le_to_cpu(pwrb_handle->pwrb,
4306 sizeof(struct iscsi_target_context_update_wrb));
4308 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
4309 doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
4310 << DB_DEF_PDU_WRB_INDEX_SHIFT;
4311 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4312 iowrite32(doorbell, phba->db_va +
4313 beiscsi_conn->doorbell_offset);
4316 * There is no completion for CONTEXT_UPDATE. The completion of next
4317 * WRB posted guarantees FW's processing and DMA'ing of it.
4318 * Use beiscsi_put_wrb_handle to put it back in the pool which makes
4319 * sure zero'ing or reuse of the WRB only after wrbs_per_cxn.
4321 beiscsi_put_wrb_handle(pwrb_context, pwrb_handle,
4322 phba->params.wrbs_per_cxn);
4323 beiscsi_log(phba, KERN_INFO,
4324 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4325 "BM_%d : put CONTEXT_UPDATE pwrb_handle=%p free_index=0x%x wrb_handles_available=%d\n",
4326 pwrb_handle, pwrb_context->free_index,
4327 pwrb_context->wrb_handles_available);
4330 static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
4331 int *index, int *age)
4335 *age = conn->session->age;
4339 * beiscsi_alloc_pdu - allocates pdu and related resources
4340 * @task: libiscsi task
4341 * @opcode: opcode of pdu for task
4343 * This is called with the session lock held. It will allocate
4344 * the wrb and sgl if needed for the command. And it will prep
4345 * the pdu's itt. beiscsi_parse_pdu will later translate
4346 * the pdu itt to the libiscsi task itt.
4348 static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
4350 struct beiscsi_io_task *io_task = task->dd_data;
4351 struct iscsi_conn *conn = task->conn;
4352 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4353 struct beiscsi_hba *phba = beiscsi_conn->phba;
4354 struct hwi_wrb_context *pwrb_context;
4355 struct hwi_controller *phwi_ctrlr;
4357 uint16_t cri_index = 0;
4358 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4361 io_task->cmd_bhs = dma_pool_alloc(beiscsi_sess->bhs_pool,
4362 GFP_ATOMIC, &paddr);
4363 if (!io_task->cmd_bhs)
4365 io_task->bhs_pa.u.a64.address = paddr;
4366 io_task->libiscsi_itt = (itt_t)task->itt;
4367 io_task->conn = beiscsi_conn;
4369 task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
4370 task->hdr_max = sizeof(struct be_cmd_bhs);
4371 io_task->psgl_handle = NULL;
4372 io_task->pwrb_handle = NULL;
4375 io_task->psgl_handle = alloc_io_sgl_handle(phba);
4376 if (!io_task->psgl_handle) {
4377 beiscsi_log(phba, KERN_ERR,
4378 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4379 "BM_%d : Alloc of IO_SGL_ICD Failed "
4380 "for the CID : %d\n",
4381 beiscsi_conn->beiscsi_conn_cid);
4384 io_task->pwrb_handle = alloc_wrb_handle(phba,
4385 beiscsi_conn->beiscsi_conn_cid,
4386 &io_task->pwrb_context);
4387 if (!io_task->pwrb_handle) {
4388 beiscsi_log(phba, KERN_ERR,
4389 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4390 "BM_%d : Alloc of WRB_HANDLE Failed "
4391 "for the CID : %d\n",
4392 beiscsi_conn->beiscsi_conn_cid);
4396 io_task->scsi_cmnd = NULL;
4397 if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
4398 beiscsi_conn->task = task;
4399 if (!beiscsi_conn->login_in_progress) {
4400 io_task->psgl_handle = (struct sgl_handle *)
4401 alloc_mgmt_sgl_handle(phba);
4402 if (!io_task->psgl_handle) {
4403 beiscsi_log(phba, KERN_ERR,
4406 "BM_%d : Alloc of MGMT_SGL_ICD Failed "
4407 "for the CID : %d\n",
4408 beiscsi_conn->beiscsi_conn_cid);
4412 beiscsi_conn->login_in_progress = 1;
4413 beiscsi_conn->plogin_sgl_handle =
4414 io_task->psgl_handle;
4415 io_task->pwrb_handle =
4416 alloc_wrb_handle(phba,
4417 beiscsi_conn->beiscsi_conn_cid,
4418 &io_task->pwrb_context);
4419 if (!io_task->pwrb_handle) {
4420 beiscsi_log(phba, KERN_ERR,
4423 "BM_%d : Alloc of WRB_HANDLE Failed "
4424 "for the CID : %d\n",
4425 beiscsi_conn->beiscsi_conn_cid);
4426 goto free_mgmt_hndls;
4428 beiscsi_conn->plogin_wrb_handle =
4429 io_task->pwrb_handle;
4432 io_task->psgl_handle =
4433 beiscsi_conn->plogin_sgl_handle;
4434 io_task->pwrb_handle =
4435 beiscsi_conn->plogin_wrb_handle;
4438 io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
4439 if (!io_task->psgl_handle) {
4440 beiscsi_log(phba, KERN_ERR,
4443 "BM_%d : Alloc of MGMT_SGL_ICD Failed "
4444 "for the CID : %d\n",
4445 beiscsi_conn->beiscsi_conn_cid);
4448 io_task->pwrb_handle =
4449 alloc_wrb_handle(phba,
4450 beiscsi_conn->beiscsi_conn_cid,
4451 &io_task->pwrb_context);
4452 if (!io_task->pwrb_handle) {
4453 beiscsi_log(phba, KERN_ERR,
4454 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4455 "BM_%d : Alloc of WRB_HANDLE Failed "
4456 "for the CID : %d\n",
4457 beiscsi_conn->beiscsi_conn_cid);
4458 goto free_mgmt_hndls;
4463 itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
4464 wrb_index << 16) | (unsigned int)
4465 (io_task->psgl_handle->sgl_index));
4466 io_task->pwrb_handle->pio_handle = task;
4468 io_task->cmd_bhs->iscsi_hdr.itt = itt;
4472 free_io_sgl_handle(phba, io_task->psgl_handle);
4475 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
4476 io_task->psgl_handle = NULL;
4478 phwi_ctrlr = phba->phwi_ctrlr;
4479 cri_index = BE_GET_CRI_FROM_CID(
4480 beiscsi_conn->beiscsi_conn_cid);
4481 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
4482 if (io_task->pwrb_handle)
4483 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
4484 io_task->pwrb_handle = NULL;
4485 dma_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4486 io_task->bhs_pa.u.a64.address);
4487 io_task->cmd_bhs = NULL;
4490 static int beiscsi_iotask_v2(struct iscsi_task *task, struct scatterlist *sg,
4491 unsigned int num_sg, unsigned int xferlen,
4492 unsigned int writedir)
4495 struct beiscsi_io_task *io_task = task->dd_data;
4496 struct iscsi_conn *conn = task->conn;
4497 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4498 struct beiscsi_hba *phba = beiscsi_conn->phba;
4499 struct iscsi_wrb *pwrb = NULL;
4500 unsigned int doorbell = 0;
4502 pwrb = io_task->pwrb_handle->pwrb;
4504 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4507 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4509 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 1);
4511 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4513 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 0);
4516 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb_v2,
4519 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, lun, pwrb,
4520 cpu_to_be16(*(unsigned short *)
4521 &io_task->cmd_bhs->iscsi_hdr.lun));
4522 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb, xferlen);
4523 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4524 io_task->pwrb_handle->wrb_index);
4525 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4526 be32_to_cpu(task->cmdsn));
4527 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4528 io_task->psgl_handle->sgl_index);
4530 hwi_write_sgl_v2(pwrb, sg, num_sg, io_task);
4531 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
4532 io_task->pwrb_handle->wrb_index);
4533 if (io_task->pwrb_context->plast_wrb)
4534 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb,
4535 io_task->pwrb_context->plast_wrb,
4536 io_task->pwrb_handle->wrb_index);
4537 io_task->pwrb_context->plast_wrb = pwrb;
4539 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4541 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
4542 doorbell |= (io_task->pwrb_handle->wrb_index &
4543 DB_DEF_PDU_WRB_INDEX_MASK) <<
4544 DB_DEF_PDU_WRB_INDEX_SHIFT;
4545 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4546 iowrite32(doorbell, phba->db_va +
4547 beiscsi_conn->doorbell_offset);
4551 static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
4552 unsigned int num_sg, unsigned int xferlen,
4553 unsigned int writedir)
4556 struct beiscsi_io_task *io_task = task->dd_data;
4557 struct iscsi_conn *conn = task->conn;
4558 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4559 struct beiscsi_hba *phba = beiscsi_conn->phba;
4560 struct iscsi_wrb *pwrb = NULL;
4561 unsigned int doorbell = 0;
4563 pwrb = io_task->pwrb_handle->pwrb;
4564 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4567 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4569 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
4571 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4573 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
4576 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb,
4579 AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
4580 cpu_to_be16(*(unsigned short *)
4581 &io_task->cmd_bhs->iscsi_hdr.lun));
4582 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
4583 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4584 io_task->pwrb_handle->wrb_index);
4585 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4586 be32_to_cpu(task->cmdsn));
4587 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4588 io_task->psgl_handle->sgl_index);
4590 hwi_write_sgl(pwrb, sg, num_sg, io_task);
4592 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
4593 io_task->pwrb_handle->wrb_index);
4594 if (io_task->pwrb_context->plast_wrb)
4595 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb,
4596 io_task->pwrb_context->plast_wrb,
4597 io_task->pwrb_handle->wrb_index);
4598 io_task->pwrb_context->plast_wrb = pwrb;
4600 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4602 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
4603 doorbell |= (io_task->pwrb_handle->wrb_index &
4604 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4605 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4607 iowrite32(doorbell, phba->db_va +
4608 beiscsi_conn->doorbell_offset);
4612 static int beiscsi_mtask(struct iscsi_task *task)
4614 struct beiscsi_io_task *io_task = task->dd_data;
4615 struct iscsi_conn *conn = task->conn;
4616 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4617 struct beiscsi_hba *phba = beiscsi_conn->phba;
4618 struct iscsi_wrb *pwrb = NULL;
4619 unsigned int doorbell = 0;
4621 unsigned int pwrb_typeoffset = 0;
4624 cid = beiscsi_conn->beiscsi_conn_cid;
4625 pwrb = io_task->pwrb_handle->pwrb;
4627 if (is_chip_be2_be3r(phba)) {
4628 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4629 be32_to_cpu(task->cmdsn));
4630 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4631 io_task->pwrb_handle->wrb_index);
4632 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4633 io_task->psgl_handle->sgl_index);
4634 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
4636 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
4637 io_task->pwrb_handle->wrb_index);
4638 if (io_task->pwrb_context->plast_wrb)
4639 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb,
4640 io_task->pwrb_context->plast_wrb,
4641 io_task->pwrb_handle->wrb_index);
4642 io_task->pwrb_context->plast_wrb = pwrb;
4644 pwrb_typeoffset = BE_WRB_TYPE_OFFSET;
4646 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4647 be32_to_cpu(task->cmdsn));
4648 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4649 io_task->pwrb_handle->wrb_index);
4650 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4651 io_task->psgl_handle->sgl_index);
4652 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb,
4654 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
4655 io_task->pwrb_handle->wrb_index);
4656 if (io_task->pwrb_context->plast_wrb)
4657 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb,
4658 io_task->pwrb_context->plast_wrb,
4659 io_task->pwrb_handle->wrb_index);
4660 io_task->pwrb_context->plast_wrb = pwrb;
4662 pwrb_typeoffset = SKH_WRB_TYPE_OFFSET;
4666 switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
4667 case ISCSI_OP_LOGIN:
4668 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
4669 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
4670 ret = hwi_write_buffer(pwrb, task);
4672 case ISCSI_OP_NOOP_OUT:
4673 if (task->hdr->ttt != ISCSI_RESERVED_TAG) {
4674 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
4675 if (is_chip_be2_be3r(phba))
4676 AMAP_SET_BITS(struct amap_iscsi_wrb,
4679 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
4682 ADAPTER_SET_WRB_TYPE(pwrb, INI_RD_CMD, pwrb_typeoffset);
4683 if (is_chip_be2_be3r(phba))
4684 AMAP_SET_BITS(struct amap_iscsi_wrb,
4687 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
4690 ret = hwi_write_buffer(pwrb, task);
4693 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
4694 ret = hwi_write_buffer(pwrb, task);
4696 case ISCSI_OP_SCSI_TMFUNC:
4697 ADAPTER_SET_WRB_TYPE(pwrb, INI_TMF_CMD, pwrb_typeoffset);
4698 ret = hwi_write_buffer(pwrb, task);
4700 case ISCSI_OP_LOGOUT:
4701 ADAPTER_SET_WRB_TYPE(pwrb, HWH_TYPE_LOGOUT, pwrb_typeoffset);
4702 ret = hwi_write_buffer(pwrb, task);
4706 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4707 "BM_%d : opcode =%d Not supported\n",
4708 task->hdr->opcode & ISCSI_OPCODE_MASK);
4716 /* Set the task type */
4717 io_task->wrb_type = (is_chip_be2_be3r(phba)) ?
4718 AMAP_GET_BITS(struct amap_iscsi_wrb, type, pwrb) :
4719 AMAP_GET_BITS(struct amap_iscsi_wrb_v2, type, pwrb);
4721 doorbell |= cid & DB_WRB_POST_CID_MASK;
4722 doorbell |= (io_task->pwrb_handle->wrb_index &
4723 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4724 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4725 iowrite32(doorbell, phba->db_va +
4726 beiscsi_conn->doorbell_offset);
4730 static int beiscsi_task_xmit(struct iscsi_task *task)
4732 struct beiscsi_io_task *io_task = task->dd_data;
4733 struct scsi_cmnd *sc = task->sc;
4734 struct beiscsi_hba *phba;
4735 struct scatterlist *sg;
4737 unsigned int writedir = 0, xferlen = 0;
4739 phba = io_task->conn->phba;
4741 * HBA in error includes BEISCSI_HBA_FW_TIMEOUT. IO path might be
4742 * operational if FW still gets heartbeat from EP FW. Is management
4743 * path really needed to continue further?
4745 if (!beiscsi_hba_is_online(phba))
4748 if (!io_task->conn->login_in_progress)
4749 task->hdr->exp_statsn = 0;
4752 return beiscsi_mtask(task);
4754 io_task->scsi_cmnd = sc;
4755 io_task->num_sg = 0;
4756 num_sg = scsi_dma_map(sc);
4758 beiscsi_log(phba, KERN_ERR,
4759 BEISCSI_LOG_IO | BEISCSI_LOG_ISCSI,
4760 "BM_%d : scsi_dma_map Failed "
4761 "Driver_ITT : 0x%x ITT : 0x%x Xferlen : 0x%x\n",
4762 be32_to_cpu(io_task->cmd_bhs->iscsi_hdr.itt),
4763 io_task->libiscsi_itt, scsi_bufflen(sc));
4768 * For scsi cmd task, check num_sg before unmapping in cleanup_task.
4769 * For management task, cleanup_task checks mtask_addr before unmapping.
4771 io_task->num_sg = num_sg;
4772 xferlen = scsi_bufflen(sc);
4773 sg = scsi_sglist(sc);
4774 if (sc->sc_data_direction == DMA_TO_DEVICE)
4779 return phba->iotask_fn(task, sg, num_sg, xferlen, writedir);
4783 * beiscsi_bsg_request - handle bsg request from ISCSI transport
4784 * @job: job to handle
4786 static int beiscsi_bsg_request(struct bsg_job *job)
4788 struct Scsi_Host *shost;
4789 struct beiscsi_hba *phba;
4790 struct iscsi_bsg_request *bsg_req = job->request;
4793 struct be_dma_mem nonemb_cmd;
4794 struct be_cmd_resp_hdr *resp;
4795 struct iscsi_bsg_reply *bsg_reply = job->reply;
4796 unsigned short status, extd_status;
4798 shost = iscsi_job_to_shost(job);
4799 phba = iscsi_host_priv(shost);
4801 if (!beiscsi_hba_is_online(phba)) {
4802 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
4803 "BM_%d : HBA in error 0x%lx\n", phba->state);
4807 switch (bsg_req->msgcode) {
4808 case ISCSI_BSG_HST_VENDOR:
4809 nonemb_cmd.va = dma_alloc_coherent(&phba->ctrl.pdev->dev,
4810 job->request_payload.payload_len,
4811 &nonemb_cmd.dma, GFP_KERNEL);
4812 if (nonemb_cmd.va == NULL) {
4813 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4814 "BM_%d : Failed to allocate memory for "
4815 "beiscsi_bsg_request\n");
4818 tag = mgmt_vendor_specific_fw_cmd(&phba->ctrl, phba, job,
4821 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4822 "BM_%d : MBX Tag Allocation Failed\n");
4824 dma_free_coherent(&phba->ctrl.pdev->dev, nonemb_cmd.size,
4825 nonemb_cmd.va, nonemb_cmd.dma);
4829 rc = wait_event_interruptible_timeout(
4830 phba->ctrl.mcc_wait[tag],
4831 phba->ctrl.mcc_tag_status[tag],
4833 BEISCSI_HOST_MBX_TIMEOUT));
4835 if (!test_bit(BEISCSI_HBA_ONLINE, &phba->state)) {
4836 clear_bit(MCC_TAG_STATE_RUNNING,
4837 &phba->ctrl.ptag_state[tag].tag_state);
4838 dma_free_coherent(&phba->ctrl.pdev->dev, nonemb_cmd.size,
4839 nonemb_cmd.va, nonemb_cmd.dma);
4842 extd_status = (phba->ctrl.mcc_tag_status[tag] &
4843 CQE_STATUS_ADDL_MASK) >> CQE_STATUS_ADDL_SHIFT;
4844 status = phba->ctrl.mcc_tag_status[tag] & CQE_STATUS_MASK;
4845 free_mcc_wrb(&phba->ctrl, tag);
4846 resp = (struct be_cmd_resp_hdr *)nonemb_cmd.va;
4847 sg_copy_from_buffer(job->reply_payload.sg_list,
4848 job->reply_payload.sg_cnt,
4849 nonemb_cmd.va, (resp->response_length
4851 bsg_reply->reply_payload_rcv_len = resp->response_length;
4852 bsg_reply->result = status;
4853 bsg_job_done(job, bsg_reply->result,
4854 bsg_reply->reply_payload_rcv_len);
4855 dma_free_coherent(&phba->ctrl.pdev->dev, nonemb_cmd.size,
4856 nonemb_cmd.va, nonemb_cmd.dma);
4857 if (status || extd_status) {
4858 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4859 "BM_%d : MBX Cmd Failed"
4860 " status = %d extd_status = %d\n",
4861 status, extd_status);
4870 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4871 "BM_%d : Unsupported bsg command: 0x%x\n",
4879 static void beiscsi_hba_attrs_init(struct beiscsi_hba *phba)
4881 /* Set the logging parameter */
4882 beiscsi_log_enable_init(phba, beiscsi_log_enable);
4885 void beiscsi_start_boot_work(struct beiscsi_hba *phba, unsigned int s_handle)
4887 if (phba->boot_struct.boot_kset)
4890 /* skip if boot work is already in progress */
4891 if (test_and_set_bit(BEISCSI_HBA_BOOT_WORK, &phba->state))
4894 phba->boot_struct.retry = 3;
4895 phba->boot_struct.tag = 0;
4896 phba->boot_struct.s_handle = s_handle;
4897 phba->boot_struct.action = BEISCSI_BOOT_GET_SHANDLE;
4898 schedule_work(&phba->boot_work);
4901 #define BEISCSI_SYSFS_ISCSI_BOOT_FLAGS 3
4903 * beiscsi_show_boot_tgt_info()
4904 * Boot flag info for iscsi-utilities
4905 * Bit 0 Block valid flag
4906 * Bit 1 Firmware booting selected
4908 static ssize_t beiscsi_show_boot_tgt_info(void *data, int type, char *buf)
4910 struct beiscsi_hba *phba = data;
4911 struct mgmt_session_info *boot_sess = &phba->boot_struct.boot_sess;
4912 struct mgmt_conn_info *boot_conn = &boot_sess->conn_list[0];
4917 case ISCSI_BOOT_TGT_NAME:
4918 rc = sprintf(buf, "%.*s\n",
4919 (int)strlen(boot_sess->target_name),
4920 (char *)&boot_sess->target_name);
4922 case ISCSI_BOOT_TGT_IP_ADDR:
4923 if (boot_conn->dest_ipaddr.ip_type == BEISCSI_IP_TYPE_V4)
4924 rc = sprintf(buf, "%pI4\n",
4925 (char *)&boot_conn->dest_ipaddr.addr);
4927 rc = sprintf(str, "%pI6\n",
4928 (char *)&boot_conn->dest_ipaddr.addr);
4930 case ISCSI_BOOT_TGT_PORT:
4931 rc = sprintf(str, "%d\n", boot_conn->dest_port);
4934 case ISCSI_BOOT_TGT_CHAP_NAME:
4935 rc = sprintf(str, "%.*s\n",
4936 boot_conn->negotiated_login_options.auth_data.chap.
4937 target_chap_name_length,
4938 (char *)&boot_conn->negotiated_login_options.
4939 auth_data.chap.target_chap_name);
4941 case ISCSI_BOOT_TGT_CHAP_SECRET:
4942 rc = sprintf(str, "%.*s\n",
4943 boot_conn->negotiated_login_options.auth_data.chap.
4944 target_secret_length,
4945 (char *)&boot_conn->negotiated_login_options.
4946 auth_data.chap.target_secret);
4948 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
4949 rc = sprintf(str, "%.*s\n",
4950 boot_conn->negotiated_login_options.auth_data.chap.
4951 intr_chap_name_length,
4952 (char *)&boot_conn->negotiated_login_options.
4953 auth_data.chap.intr_chap_name);
4955 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
4956 rc = sprintf(str, "%.*s\n",
4957 boot_conn->negotiated_login_options.auth_data.chap.
4959 (char *)&boot_conn->negotiated_login_options.
4960 auth_data.chap.intr_secret);
4962 case ISCSI_BOOT_TGT_FLAGS:
4963 rc = sprintf(str, "%d\n", BEISCSI_SYSFS_ISCSI_BOOT_FLAGS);
4965 case ISCSI_BOOT_TGT_NIC_ASSOC:
4966 rc = sprintf(str, "0\n");
4972 static ssize_t beiscsi_show_boot_ini_info(void *data, int type, char *buf)
4974 struct beiscsi_hba *phba = data;
4979 case ISCSI_BOOT_INI_INITIATOR_NAME:
4980 rc = sprintf(str, "%s\n",
4981 phba->boot_struct.boot_sess.initiator_iscsiname);
4987 static ssize_t beiscsi_show_boot_eth_info(void *data, int type, char *buf)
4989 struct beiscsi_hba *phba = data;
4994 case ISCSI_BOOT_ETH_FLAGS:
4995 rc = sprintf(str, "%d\n", BEISCSI_SYSFS_ISCSI_BOOT_FLAGS);
4997 case ISCSI_BOOT_ETH_INDEX:
4998 rc = sprintf(str, "0\n");
5000 case ISCSI_BOOT_ETH_MAC:
5001 rc = beiscsi_get_macaddr(str, phba);
5007 static umode_t beiscsi_tgt_get_attr_visibility(void *data, int type)
5012 case ISCSI_BOOT_TGT_NAME:
5013 case ISCSI_BOOT_TGT_IP_ADDR:
5014 case ISCSI_BOOT_TGT_PORT:
5015 case ISCSI_BOOT_TGT_CHAP_NAME:
5016 case ISCSI_BOOT_TGT_CHAP_SECRET:
5017 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
5018 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
5019 case ISCSI_BOOT_TGT_NIC_ASSOC:
5020 case ISCSI_BOOT_TGT_FLAGS:
5027 static umode_t beiscsi_ini_get_attr_visibility(void *data, int type)
5032 case ISCSI_BOOT_INI_INITIATOR_NAME:
5039 static umode_t beiscsi_eth_get_attr_visibility(void *data, int type)
5044 case ISCSI_BOOT_ETH_FLAGS:
5045 case ISCSI_BOOT_ETH_MAC:
5046 case ISCSI_BOOT_ETH_INDEX:
5053 static void beiscsi_boot_kobj_release(void *data)
5055 struct beiscsi_hba *phba = data;
5057 scsi_host_put(phba->shost);
5060 static int beiscsi_boot_create_kset(struct beiscsi_hba *phba)
5062 struct boot_struct *bs = &phba->boot_struct;
5063 struct iscsi_boot_kobj *boot_kobj;
5065 if (bs->boot_kset) {
5066 __beiscsi_log(phba, KERN_ERR,
5067 "BM_%d: boot_kset already created\n");
5071 bs->boot_kset = iscsi_boot_create_host_kset(phba->shost->host_no);
5072 if (!bs->boot_kset) {
5073 __beiscsi_log(phba, KERN_ERR,
5074 "BM_%d: boot_kset alloc failed\n");
5078 /* get shost ref because the show function will refer phba */
5079 if (!scsi_host_get(phba->shost))
5082 boot_kobj = iscsi_boot_create_target(bs->boot_kset, 0, phba,
5083 beiscsi_show_boot_tgt_info,
5084 beiscsi_tgt_get_attr_visibility,
5085 beiscsi_boot_kobj_release);
5089 if (!scsi_host_get(phba->shost))
5092 boot_kobj = iscsi_boot_create_initiator(bs->boot_kset, 0, phba,
5093 beiscsi_show_boot_ini_info,
5094 beiscsi_ini_get_attr_visibility,
5095 beiscsi_boot_kobj_release);
5099 if (!scsi_host_get(phba->shost))
5102 boot_kobj = iscsi_boot_create_ethernet(bs->boot_kset, 0, phba,
5103 beiscsi_show_boot_eth_info,
5104 beiscsi_eth_get_attr_visibility,
5105 beiscsi_boot_kobj_release);
5112 scsi_host_put(phba->shost);
5114 iscsi_boot_destroy_kset(bs->boot_kset);
5115 bs->boot_kset = NULL;
5119 static void beiscsi_boot_work(struct work_struct *work)
5121 struct beiscsi_hba *phba =
5122 container_of(work, struct beiscsi_hba, boot_work);
5123 struct boot_struct *bs = &phba->boot_struct;
5124 unsigned int tag = 0;
5126 if (!beiscsi_hba_is_online(phba))
5129 beiscsi_log(phba, KERN_INFO,
5130 BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX,
5131 "BM_%d : %s action %d\n",
5132 __func__, phba->boot_struct.action);
5134 switch (phba->boot_struct.action) {
5135 case BEISCSI_BOOT_REOPEN_SESS:
5136 tag = beiscsi_boot_reopen_sess(phba);
5138 case BEISCSI_BOOT_GET_SHANDLE:
5139 tag = __beiscsi_boot_get_shandle(phba, 1);
5141 case BEISCSI_BOOT_GET_SINFO:
5142 tag = beiscsi_boot_get_sinfo(phba);
5144 case BEISCSI_BOOT_LOGOUT_SESS:
5145 tag = beiscsi_boot_logout_sess(phba);
5147 case BEISCSI_BOOT_CREATE_KSET:
5148 beiscsi_boot_create_kset(phba);
5150 * updated boot_kset is made visible to all before
5151 * ending the boot work.
5154 clear_bit(BEISCSI_HBA_BOOT_WORK, &phba->state);
5159 schedule_work(&phba->boot_work);
5161 clear_bit(BEISCSI_HBA_BOOT_WORK, &phba->state);
5165 static void beiscsi_eqd_update_work(struct work_struct *work)
5167 struct hwi_context_memory *phwi_context;
5168 struct be_set_eqd set_eqd[MAX_CPUS];
5169 struct hwi_controller *phwi_ctrlr;
5170 struct be_eq_obj *pbe_eq;
5171 struct beiscsi_hba *phba;
5172 unsigned int pps, delta;
5173 struct be_aic_obj *aic;
5174 int eqd, i, num = 0;
5177 phba = container_of(work, struct beiscsi_hba, eqd_update.work);
5178 if (!beiscsi_hba_is_online(phba))
5181 phwi_ctrlr = phba->phwi_ctrlr;
5182 phwi_context = phwi_ctrlr->phwi_ctxt;
5184 for (i = 0; i <= phba->num_cpus; i++) {
5185 aic = &phba->aic_obj[i];
5186 pbe_eq = &phwi_context->be_eq[i];
5188 if (!aic->jiffies || time_before(now, aic->jiffies) ||
5189 pbe_eq->cq_count < aic->eq_prev) {
5191 aic->eq_prev = pbe_eq->cq_count;
5194 delta = jiffies_to_msecs(now - aic->jiffies);
5195 pps = (((u32)(pbe_eq->cq_count - aic->eq_prev) * 1000) / delta);
5196 eqd = (pps / 1500) << 2;
5200 eqd = min_t(u32, eqd, BEISCSI_EQ_DELAY_MAX);
5201 eqd = max_t(u32, eqd, BEISCSI_EQ_DELAY_MIN);
5204 aic->eq_prev = pbe_eq->cq_count;
5206 if (eqd != aic->prev_eqd) {
5207 set_eqd[num].delay_multiplier = (eqd * 65)/100;
5208 set_eqd[num].eq_id = pbe_eq->q.id;
5209 aic->prev_eqd = eqd;
5214 /* completion of this is ignored */
5215 beiscsi_modify_eq_delay(phba, set_eqd, num);
5217 schedule_delayed_work(&phba->eqd_update,
5218 msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
5221 static void beiscsi_hw_tpe_check(struct timer_list *t)
5223 struct beiscsi_hba *phba = from_timer(phba, t, hw_check);
5226 /* if not TPE, do nothing */
5227 if (!beiscsi_detect_tpe(phba))
5230 /* wait default 4000ms before recovering */
5232 if (phba->ue2rp > BEISCSI_UE_DETECT_INTERVAL)
5233 wait = phba->ue2rp - BEISCSI_UE_DETECT_INTERVAL;
5234 queue_delayed_work(phba->wq, &phba->recover_port,
5235 msecs_to_jiffies(wait));
5238 static void beiscsi_hw_health_check(struct timer_list *t)
5240 struct beiscsi_hba *phba = from_timer(phba, t, hw_check);
5242 beiscsi_detect_ue(phba);
5243 if (beiscsi_detect_ue(phba)) {
5244 __beiscsi_log(phba, KERN_ERR,
5245 "BM_%d : port in error: %lx\n", phba->state);
5246 /* sessions are no longer valid, so first fail the sessions */
5247 queue_work(phba->wq, &phba->sess_work);
5249 /* detect UER supported */
5250 if (!test_bit(BEISCSI_HBA_UER_SUPP, &phba->state))
5252 /* modify this timer to check TPE */
5253 phba->hw_check.function = beiscsi_hw_tpe_check;
5256 mod_timer(&phba->hw_check,
5257 jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
5261 * beiscsi_enable_port()- Enables the disabled port.
5262 * Only port resources freed in disable function are reallocated.
5263 * This is called in HBA error handling path.
5265 * @phba: Instance of driver private structure
5268 static int beiscsi_enable_port(struct beiscsi_hba *phba)
5270 struct hwi_context_memory *phwi_context;
5271 struct hwi_controller *phwi_ctrlr;
5272 struct be_eq_obj *pbe_eq;
5275 if (test_bit(BEISCSI_HBA_ONLINE, &phba->state)) {
5276 __beiscsi_log(phba, KERN_ERR,
5277 "BM_%d : %s : port is online %lx\n",
5278 __func__, phba->state);
5282 ret = beiscsi_init_sliport(phba);
5286 be2iscsi_enable_msix(phba);
5288 beiscsi_get_params(phba);
5289 beiscsi_set_host_data(phba);
5290 /* Re-enable UER. If different TPE occurs then it is recoverable. */
5291 beiscsi_set_uer_feature(phba);
5293 phba->shost->max_id = phba->params.cxns_per_ctrl - 1;
5294 phba->shost->can_queue = phba->params.ios_per_ctrl;
5295 ret = beiscsi_init_port(phba);
5297 __beiscsi_log(phba, KERN_ERR,
5298 "BM_%d : init port failed\n");
5302 for (i = 0; i < MAX_MCC_CMD; i++) {
5303 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
5304 phba->ctrl.mcc_tag[i] = i + 1;
5305 phba->ctrl.mcc_tag_status[i + 1] = 0;
5306 phba->ctrl.mcc_tag_available++;
5309 phwi_ctrlr = phba->phwi_ctrlr;
5310 phwi_context = phwi_ctrlr->phwi_ctxt;
5311 for (i = 0; i < phba->num_cpus; i++) {
5312 pbe_eq = &phwi_context->be_eq[i];
5313 irq_poll_init(&pbe_eq->iopoll, be_iopoll_budget, be_iopoll);
5316 i = (phba->pcidev->msix_enabled) ? i : 0;
5317 /* Work item for MCC handling */
5318 pbe_eq = &phwi_context->be_eq[i];
5319 INIT_WORK(&pbe_eq->mcc_work, beiscsi_mcc_work);
5321 ret = beiscsi_init_irqs(phba);
5323 __beiscsi_log(phba, KERN_ERR,
5324 "BM_%d : setup IRQs failed %d\n", ret);
5327 hwi_enable_intr(phba);
5328 /* port operational: clear all error bits */
5329 set_bit(BEISCSI_HBA_ONLINE, &phba->state);
5330 __beiscsi_log(phba, KERN_INFO,
5331 "BM_%d : port online: 0x%lx\n", phba->state);
5333 /* start hw_check timer and eqd_update work */
5334 schedule_delayed_work(&phba->eqd_update,
5335 msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
5338 * Timer function gets modified for TPE detection.
5339 * Always reinit to do health check first.
5341 phba->hw_check.function = beiscsi_hw_health_check;
5342 mod_timer(&phba->hw_check,
5343 jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
5347 for (i = 0; i < phba->num_cpus; i++) {
5348 pbe_eq = &phwi_context->be_eq[i];
5349 irq_poll_disable(&pbe_eq->iopoll);
5351 hwi_cleanup_port(phba);
5354 pci_free_irq_vectors(phba->pcidev);
5359 * beiscsi_disable_port()- Disable port and cleanup driver resources.
5360 * This is called in HBA error handling and driver removal.
5361 * @phba: Instance Priv structure
5362 * @unload: indicate driver is unloading
5364 * Free the OS and HW resources held by the driver
5366 static void beiscsi_disable_port(struct beiscsi_hba *phba, int unload)
5368 struct hwi_context_memory *phwi_context;
5369 struct hwi_controller *phwi_ctrlr;
5370 struct be_eq_obj *pbe_eq;
5373 if (!test_and_clear_bit(BEISCSI_HBA_ONLINE, &phba->state))
5376 phwi_ctrlr = phba->phwi_ctrlr;
5377 phwi_context = phwi_ctrlr->phwi_ctxt;
5378 hwi_disable_intr(phba);
5379 beiscsi_free_irqs(phba);
5380 pci_free_irq_vectors(phba->pcidev);
5382 for (i = 0; i < phba->num_cpus; i++) {
5383 pbe_eq = &phwi_context->be_eq[i];
5384 irq_poll_disable(&pbe_eq->iopoll);
5386 cancel_delayed_work_sync(&phba->eqd_update);
5387 cancel_work_sync(&phba->boot_work);
5388 /* WQ might be running cancel queued mcc_work if we are not exiting */
5389 if (!unload && beiscsi_hba_in_error(phba)) {
5390 pbe_eq = &phwi_context->be_eq[i];
5391 cancel_work_sync(&pbe_eq->mcc_work);
5393 hwi_cleanup_port(phba);
5394 beiscsi_cleanup_port(phba);
5397 static void beiscsi_sess_work(struct work_struct *work)
5399 struct beiscsi_hba *phba;
5401 phba = container_of(work, struct beiscsi_hba, sess_work);
5403 * This work gets scheduled only in case of HBA error.
5404 * Old sessions are gone so need to be re-established.
5405 * iscsi_session_failure needs process context hence this work.
5407 iscsi_host_for_each_session(phba->shost, beiscsi_session_fail);
5410 static void beiscsi_recover_port(struct work_struct *work)
5412 struct beiscsi_hba *phba;
5414 phba = container_of(work, struct beiscsi_hba, recover_port.work);
5415 beiscsi_disable_port(phba, 0);
5416 beiscsi_enable_port(phba);
5419 static pci_ers_result_t beiscsi_eeh_err_detected(struct pci_dev *pdev,
5420 pci_channel_state_t state)
5422 struct beiscsi_hba *phba = NULL;
5424 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
5425 set_bit(BEISCSI_HBA_PCI_ERR, &phba->state);
5427 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5428 "BM_%d : EEH error detected\n");
5430 /* first stop UE detection when PCI error detected */
5431 del_timer_sync(&phba->hw_check);
5432 cancel_delayed_work_sync(&phba->recover_port);
5434 /* sessions are no longer valid, so first fail the sessions */
5435 iscsi_host_for_each_session(phba->shost, beiscsi_session_fail);
5436 beiscsi_disable_port(phba, 0);
5438 if (state == pci_channel_io_perm_failure) {
5439 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5440 "BM_%d : EEH : State PERM Failure");
5441 return PCI_ERS_RESULT_DISCONNECT;
5444 pci_disable_device(pdev);
5446 /* The error could cause the FW to trigger a flash debug dump.
5447 * Resetting the card while flash dump is in progress
5448 * can cause it not to recover; wait for it to finish.
5449 * Wait only for first function as it is needed only once per
5452 if (pdev->devfn == 0)
5455 return PCI_ERS_RESULT_NEED_RESET;
5458 static pci_ers_result_t beiscsi_eeh_reset(struct pci_dev *pdev)
5460 struct beiscsi_hba *phba = NULL;
5463 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
5465 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5466 "BM_%d : EEH Reset\n");
5468 status = pci_enable_device(pdev);
5470 return PCI_ERS_RESULT_DISCONNECT;
5472 pci_set_master(pdev);
5473 pci_set_power_state(pdev, PCI_D0);
5474 pci_restore_state(pdev);
5476 status = beiscsi_check_fw_rdy(phba);
5478 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5479 "BM_%d : EEH Reset Completed\n");
5481 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5482 "BM_%d : EEH Reset Completion Failure\n");
5483 return PCI_ERS_RESULT_DISCONNECT;
5486 return PCI_ERS_RESULT_RECOVERED;
5489 static void beiscsi_eeh_resume(struct pci_dev *pdev)
5491 struct beiscsi_hba *phba;
5494 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
5495 pci_save_state(pdev);
5497 ret = beiscsi_enable_port(phba);
5499 __beiscsi_log(phba, KERN_ERR,
5500 "BM_%d : AER EEH resume failed\n");
5503 static int beiscsi_dev_probe(struct pci_dev *pcidev,
5504 const struct pci_device_id *id)
5506 struct hwi_context_memory *phwi_context;
5507 struct hwi_controller *phwi_ctrlr;
5508 struct beiscsi_hba *phba = NULL;
5509 struct be_eq_obj *pbe_eq;
5510 unsigned int s_handle;
5514 ret = beiscsi_enable_pci(pcidev);
5516 dev_err(&pcidev->dev,
5517 "beiscsi_dev_probe - Failed to enable pci device\n");
5521 phba = beiscsi_hba_alloc(pcidev);
5523 dev_err(&pcidev->dev,
5524 "beiscsi_dev_probe - Failed in beiscsi_hba_alloc\n");
5529 /* Enable EEH reporting */
5530 ret = pci_enable_pcie_error_reporting(pcidev);
5532 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5533 "BM_%d : PCIe Error Reporting "
5534 "Enabling Failed\n");
5536 pci_save_state(pcidev);
5538 /* Initialize Driver configuration Paramters */
5539 beiscsi_hba_attrs_init(phba);
5541 phba->mac_addr_set = false;
5543 switch (pcidev->device) {
5547 phba->generation = BE_GEN2;
5548 phba->iotask_fn = beiscsi_iotask;
5549 dev_warn(&pcidev->dev,
5550 "Obsolete/Unsupported BE2 Adapter Family\n");
5554 phba->generation = BE_GEN3;
5555 phba->iotask_fn = beiscsi_iotask;
5558 phba->generation = BE_GEN4;
5559 phba->iotask_fn = beiscsi_iotask_v2;
5562 phba->generation = 0;
5565 ret = be_ctrl_init(phba, pcidev);
5567 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5568 "BM_%d : be_ctrl_init failed\n");
5572 ret = beiscsi_init_sliport(phba);
5576 spin_lock_init(&phba->io_sgl_lock);
5577 spin_lock_init(&phba->mgmt_sgl_lock);
5578 spin_lock_init(&phba->async_pdu_lock);
5579 ret = beiscsi_get_fw_config(&phba->ctrl, phba);
5581 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5582 "BM_%d : Error getting fw config\n");
5585 beiscsi_get_port_name(&phba->ctrl, phba);
5586 beiscsi_get_params(phba);
5587 beiscsi_set_host_data(phba);
5588 beiscsi_set_uer_feature(phba);
5590 be2iscsi_enable_msix(phba);
5592 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
5593 "BM_%d : num_cpus = %d\n",
5596 phba->shost->max_id = phba->params.cxns_per_ctrl;
5597 phba->shost->can_queue = phba->params.ios_per_ctrl;
5598 ret = beiscsi_get_memory(phba);
5600 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5601 "BM_%d : alloc host mem failed\n");
5605 ret = beiscsi_init_port(phba);
5607 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5608 "BM_%d : init port failed\n");
5609 beiscsi_free_mem(phba);
5613 for (i = 0; i < MAX_MCC_CMD; i++) {
5614 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
5615 phba->ctrl.mcc_tag[i] = i + 1;
5616 phba->ctrl.mcc_tag_status[i + 1] = 0;
5617 phba->ctrl.mcc_tag_available++;
5618 memset(&phba->ctrl.ptag_state[i].tag_mem_state, 0,
5619 sizeof(struct be_dma_mem));
5622 phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
5624 snprintf(wq_name, sizeof(wq_name), "beiscsi_%02x_wq",
5625 phba->shost->host_no);
5626 phba->wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 1, wq_name);
5628 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5629 "BM_%d : beiscsi_dev_probe-"
5630 "Failed to allocate work queue\n");
5635 INIT_DELAYED_WORK(&phba->eqd_update, beiscsi_eqd_update_work);
5637 phwi_ctrlr = phba->phwi_ctrlr;
5638 phwi_context = phwi_ctrlr->phwi_ctxt;
5640 for (i = 0; i < phba->num_cpus; i++) {
5641 pbe_eq = &phwi_context->be_eq[i];
5642 irq_poll_init(&pbe_eq->iopoll, be_iopoll_budget, be_iopoll);
5645 i = (phba->pcidev->msix_enabled) ? i : 0;
5646 /* Work item for MCC handling */
5647 pbe_eq = &phwi_context->be_eq[i];
5648 INIT_WORK(&pbe_eq->mcc_work, beiscsi_mcc_work);
5650 ret = beiscsi_init_irqs(phba);
5652 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5653 "BM_%d : beiscsi_dev_probe-"
5654 "Failed to beiscsi_init_irqs\n");
5655 goto disable_iopoll;
5657 hwi_enable_intr(phba);
5659 ret = iscsi_host_add(phba->shost, &phba->pcidev->dev);
5663 /* set online bit after port is operational */
5664 set_bit(BEISCSI_HBA_ONLINE, &phba->state);
5665 __beiscsi_log(phba, KERN_INFO,
5666 "BM_%d : port online: 0x%lx\n", phba->state);
5668 INIT_WORK(&phba->boot_work, beiscsi_boot_work);
5669 ret = beiscsi_boot_get_shandle(phba, &s_handle);
5671 beiscsi_start_boot_work(phba, s_handle);
5673 * Set this bit after starting the work to let
5674 * probe handle it first.
5675 * ASYNC event can too schedule this work.
5677 set_bit(BEISCSI_HBA_BOOT_FOUND, &phba->state);
5680 beiscsi_iface_create_default(phba);
5681 schedule_delayed_work(&phba->eqd_update,
5682 msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
5684 INIT_WORK(&phba->sess_work, beiscsi_sess_work);
5685 INIT_DELAYED_WORK(&phba->recover_port, beiscsi_recover_port);
5687 * Start UE detection here. UE before this will cause stall in probe
5688 * and eventually fail the probe.
5690 timer_setup(&phba->hw_check, beiscsi_hw_health_check, 0);
5691 mod_timer(&phba->hw_check,
5692 jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
5693 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
5694 "\n\n\n BM_%d : SUCCESS - DRIVER LOADED\n\n\n");
5698 hwi_disable_intr(phba);
5699 beiscsi_free_irqs(phba);
5701 for (i = 0; i < phba->num_cpus; i++) {
5702 pbe_eq = &phwi_context->be_eq[i];
5703 irq_poll_disable(&pbe_eq->iopoll);
5705 destroy_workqueue(phba->wq);
5707 hwi_cleanup_port(phba);
5708 beiscsi_cleanup_port(phba);
5709 beiscsi_free_mem(phba);
5711 dma_free_coherent(&phba->pcidev->dev,
5712 phba->ctrl.mbox_mem_alloced.size,
5713 phba->ctrl.mbox_mem_alloced.va,
5714 phba->ctrl.mbox_mem_alloced.dma);
5715 beiscsi_unmap_pci_function(phba);
5717 pci_disable_msix(phba->pcidev);
5718 pci_dev_put(phba->pcidev);
5719 iscsi_host_free(phba->shost);
5720 pci_disable_pcie_error_reporting(pcidev);
5721 pci_set_drvdata(pcidev, NULL);
5723 pci_release_regions(pcidev);
5724 pci_disable_device(pcidev);
5728 static void beiscsi_remove(struct pci_dev *pcidev)
5730 struct beiscsi_hba *phba = NULL;
5732 phba = pci_get_drvdata(pcidev);
5734 dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
5738 /* first stop UE detection before unloading */
5739 del_timer_sync(&phba->hw_check);
5740 cancel_delayed_work_sync(&phba->recover_port);
5741 cancel_work_sync(&phba->sess_work);
5743 beiscsi_iface_destroy_default(phba);
5744 iscsi_host_remove(phba->shost);
5745 beiscsi_disable_port(phba, 1);
5747 /* after cancelling boot_work */
5748 iscsi_boot_destroy_kset(phba->boot_struct.boot_kset);
5750 /* free all resources */
5751 destroy_workqueue(phba->wq);
5752 beiscsi_free_mem(phba);
5755 beiscsi_unmap_pci_function(phba);
5756 dma_free_coherent(&phba->pcidev->dev,
5757 phba->ctrl.mbox_mem_alloced.size,
5758 phba->ctrl.mbox_mem_alloced.va,
5759 phba->ctrl.mbox_mem_alloced.dma);
5761 pci_dev_put(phba->pcidev);
5762 iscsi_host_free(phba->shost);
5763 pci_disable_pcie_error_reporting(pcidev);
5764 pci_set_drvdata(pcidev, NULL);
5765 pci_release_regions(pcidev);
5766 pci_disable_device(pcidev);
5770 static struct pci_error_handlers beiscsi_eeh_handlers = {
5771 .error_detected = beiscsi_eeh_err_detected,
5772 .slot_reset = beiscsi_eeh_reset,
5773 .resume = beiscsi_eeh_resume,
5776 struct iscsi_transport beiscsi_iscsi_transport = {
5777 .owner = THIS_MODULE,
5779 .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
5780 CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
5781 .create_session = beiscsi_session_create,
5782 .destroy_session = beiscsi_session_destroy,
5783 .create_conn = beiscsi_conn_create,
5784 .bind_conn = beiscsi_conn_bind,
5785 .unbind_conn = iscsi_conn_unbind,
5786 .destroy_conn = iscsi_conn_teardown,
5787 .attr_is_visible = beiscsi_attr_is_visible,
5788 .set_iface_param = beiscsi_iface_set_param,
5789 .get_iface_param = beiscsi_iface_get_param,
5790 .set_param = beiscsi_set_param,
5791 .get_conn_param = iscsi_conn_get_param,
5792 .get_session_param = iscsi_session_get_param,
5793 .get_host_param = beiscsi_get_host_param,
5794 .start_conn = beiscsi_conn_start,
5795 .stop_conn = iscsi_conn_stop,
5796 .send_pdu = iscsi_conn_send_pdu,
5797 .xmit_task = beiscsi_task_xmit,
5798 .cleanup_task = beiscsi_cleanup_task,
5799 .alloc_pdu = beiscsi_alloc_pdu,
5800 .parse_pdu_itt = beiscsi_parse_pdu,
5801 .get_stats = beiscsi_conn_get_stats,
5802 .get_ep_param = beiscsi_ep_get_param,
5803 .ep_connect = beiscsi_ep_connect,
5804 .ep_poll = beiscsi_ep_poll,
5805 .ep_disconnect = beiscsi_ep_disconnect,
5806 .session_recovery_timedout = iscsi_session_recovery_timedout,
5807 .bsg_request = beiscsi_bsg_request,
5810 static struct pci_driver beiscsi_pci_driver = {
5812 .probe = beiscsi_dev_probe,
5813 .remove = beiscsi_remove,
5814 .id_table = beiscsi_pci_id_table,
5815 .err_handler = &beiscsi_eeh_handlers
5818 static int __init beiscsi_module_init(void)
5822 beiscsi_scsi_transport =
5823 iscsi_register_transport(&beiscsi_iscsi_transport);
5824 if (!beiscsi_scsi_transport) {
5826 "beiscsi_module_init - Unable to register beiscsi transport.\n");
5829 printk(KERN_INFO "In beiscsi_module_init, tt=%p\n",
5830 &beiscsi_iscsi_transport);
5832 ret = pci_register_driver(&beiscsi_pci_driver);
5835 "beiscsi_module_init - Unable to register beiscsi pci driver.\n");
5836 goto unregister_iscsi_transport;
5840 unregister_iscsi_transport:
5841 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5845 static void __exit beiscsi_module_exit(void)
5847 pci_unregister_driver(&beiscsi_pci_driver);
5848 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5851 module_init(beiscsi_module_init);
5852 module_exit(beiscsi_module_exit);