2 * Copyright (C) 2005 - 2015 Emulex
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation. The full GNU General
8 * Public License is included in this distribution in the file called COPYING.
10 * Written by: Jayamohan Kallickal (jayamohan.kallickal@avagotech.com)
12 * Contact Information:
13 * linux-drivers@avagotech.com
17 * Costa Mesa, CA 92626
20 #include <linux/reboot.h>
21 #include <linux/delay.h>
22 #include <linux/slab.h>
23 #include <linux/interrupt.h>
24 #include <linux/blkdev.h>
25 #include <linux/pci.h>
26 #include <linux/string.h>
27 #include <linux/kernel.h>
28 #include <linux/semaphore.h>
29 #include <linux/iscsi_boot_sysfs.h>
30 #include <linux/module.h>
31 #include <linux/bsg-lib.h>
32 #include <linux/irq_poll.h>
34 #include <scsi/libiscsi.h>
35 #include <scsi/scsi_bsg_iscsi.h>
36 #include <scsi/scsi_netlink.h>
37 #include <scsi/scsi_transport_iscsi.h>
38 #include <scsi/scsi_transport.h>
39 #include <scsi/scsi_cmnd.h>
40 #include <scsi/scsi_device.h>
41 #include <scsi/scsi_host.h>
42 #include <scsi/scsi.h>
48 static unsigned int be_iopoll_budget = 10;
49 static unsigned int be_max_phys_size = 64;
50 static unsigned int enable_msix = 1;
52 MODULE_DESCRIPTION(DRV_DESC " " BUILD_STR);
53 MODULE_VERSION(BUILD_STR);
54 MODULE_AUTHOR("Emulex Corporation");
55 MODULE_LICENSE("GPL");
56 module_param(be_iopoll_budget, int, 0);
57 module_param(enable_msix, int, 0);
58 module_param(be_max_phys_size, uint, S_IRUGO);
59 MODULE_PARM_DESC(be_max_phys_size,
60 "Maximum Size (In Kilobytes) of physically contiguous "
61 "memory that can be allocated. Range is 16 - 128");
63 #define beiscsi_disp_param(_name)\
65 beiscsi_##_name##_disp(struct device *dev,\
66 struct device_attribute *attrib, char *buf) \
68 struct Scsi_Host *shost = class_to_shost(dev);\
69 struct beiscsi_hba *phba = iscsi_host_priv(shost); \
70 uint32_t param_val = 0; \
71 param_val = phba->attr_##_name;\
72 return snprintf(buf, PAGE_SIZE, "%d\n",\
76 #define beiscsi_change_param(_name, _minval, _maxval, _defaval)\
78 beiscsi_##_name##_change(struct beiscsi_hba *phba, uint32_t val)\
80 if (val >= _minval && val <= _maxval) {\
81 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,\
82 "BA_%d : beiscsi_"#_name" updated "\
83 "from 0x%x ==> 0x%x\n",\
84 phba->attr_##_name, val); \
85 phba->attr_##_name = val;\
88 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT, \
89 "BA_%d beiscsi_"#_name" attribute "\
90 "cannot be updated to 0x%x, "\
91 "range allowed is ["#_minval" - "#_maxval"]\n", val);\
95 #define beiscsi_store_param(_name) \
97 beiscsi_##_name##_store(struct device *dev,\
98 struct device_attribute *attr, const char *buf,\
101 struct Scsi_Host *shost = class_to_shost(dev);\
102 struct beiscsi_hba *phba = iscsi_host_priv(shost);\
103 uint32_t param_val = 0;\
104 if (!isdigit(buf[0]))\
106 if (sscanf(buf, "%i", ¶m_val) != 1)\
108 if (beiscsi_##_name##_change(phba, param_val) == 0) \
114 #define beiscsi_init_param(_name, _minval, _maxval, _defval) \
116 beiscsi_##_name##_init(struct beiscsi_hba *phba, uint32_t val) \
118 if (val >= _minval && val <= _maxval) {\
119 phba->attr_##_name = val;\
122 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,\
123 "BA_%d beiscsi_"#_name" attribute " \
124 "cannot be updated to 0x%x, "\
125 "range allowed is ["#_minval" - "#_maxval"]\n", val);\
126 phba->attr_##_name = _defval;\
130 #define BEISCSI_RW_ATTR(_name, _minval, _maxval, _defval, _descp) \
131 static uint beiscsi_##_name = _defval;\
132 module_param(beiscsi_##_name, uint, S_IRUGO);\
133 MODULE_PARM_DESC(beiscsi_##_name, _descp);\
134 beiscsi_disp_param(_name)\
135 beiscsi_change_param(_name, _minval, _maxval, _defval)\
136 beiscsi_store_param(_name)\
137 beiscsi_init_param(_name, _minval, _maxval, _defval)\
138 DEVICE_ATTR(beiscsi_##_name, S_IRUGO | S_IWUSR,\
139 beiscsi_##_name##_disp, beiscsi_##_name##_store)
142 * When new log level added update the
143 * the MAX allowed value for log_enable
145 BEISCSI_RW_ATTR(log_enable, 0x00,
146 0xFF, 0x00, "Enable logging Bit Mask\n"
147 "\t\t\t\tInitialization Events : 0x01\n"
148 "\t\t\t\tMailbox Events : 0x02\n"
149 "\t\t\t\tMiscellaneous Events : 0x04\n"
150 "\t\t\t\tError Handling : 0x08\n"
151 "\t\t\t\tIO Path Events : 0x10\n"
152 "\t\t\t\tConfiguration Path : 0x20\n"
153 "\t\t\t\tiSCSI Protocol : 0x40\n");
155 DEVICE_ATTR(beiscsi_drvr_ver, S_IRUGO, beiscsi_drvr_ver_disp, NULL);
156 DEVICE_ATTR(beiscsi_adapter_family, S_IRUGO, beiscsi_adap_family_disp, NULL);
157 DEVICE_ATTR(beiscsi_fw_ver, S_IRUGO, beiscsi_fw_ver_disp, NULL);
158 DEVICE_ATTR(beiscsi_phys_port, S_IRUGO, beiscsi_phys_port_disp, NULL);
159 DEVICE_ATTR(beiscsi_active_session_count, S_IRUGO,
160 beiscsi_active_session_disp, NULL);
161 DEVICE_ATTR(beiscsi_free_session_count, S_IRUGO,
162 beiscsi_free_session_disp, NULL);
163 struct device_attribute *beiscsi_attrs[] = {
164 &dev_attr_beiscsi_log_enable,
165 &dev_attr_beiscsi_drvr_ver,
166 &dev_attr_beiscsi_adapter_family,
167 &dev_attr_beiscsi_fw_ver,
168 &dev_attr_beiscsi_active_session_count,
169 &dev_attr_beiscsi_free_session_count,
170 &dev_attr_beiscsi_phys_port,
174 static char const *cqe_desc[] = {
177 "SOL_CMD_KILLED_DATA_DIGEST_ERR",
178 "CXN_KILLED_PDU_SIZE_EXCEEDS_DSL",
179 "CXN_KILLED_BURST_LEN_MISMATCH",
180 "CXN_KILLED_AHS_RCVD",
181 "CXN_KILLED_HDR_DIGEST_ERR",
182 "CXN_KILLED_UNKNOWN_HDR",
183 "CXN_KILLED_STALE_ITT_TTT_RCVD",
184 "CXN_KILLED_INVALID_ITT_TTT_RCVD",
185 "CXN_KILLED_RST_RCVD",
186 "CXN_KILLED_TIMED_OUT",
187 "CXN_KILLED_RST_SENT",
188 "CXN_KILLED_FIN_RCVD",
189 "CXN_KILLED_BAD_UNSOL_PDU_RCVD",
190 "CXN_KILLED_BAD_WRB_INDEX_ERROR",
191 "CXN_KILLED_OVER_RUN_RESIDUAL",
192 "CXN_KILLED_UNDER_RUN_RESIDUAL",
193 "CMD_KILLED_INVALID_STATSN_RCVD",
194 "CMD_KILLED_INVALID_R2T_RCVD",
195 "CMD_CXN_KILLED_LUN_INVALID",
196 "CMD_CXN_KILLED_ICD_INVALID",
197 "CMD_CXN_KILLED_ITT_INVALID",
198 "CMD_CXN_KILLED_SEQ_OUTOFORDER",
199 "CMD_CXN_KILLED_INVALID_DATASN_RCVD",
200 "CXN_INVALIDATE_NOTIFY",
201 "CXN_INVALIDATE_INDEX_NOTIFY",
202 "CMD_INVALIDATED_NOTIFY",
205 "UNSOL_DATA_DIGEST_ERROR_NOTIFY",
207 "CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN",
208 "SOL_CMD_KILLED_DIF_ERR",
209 "CXN_KILLED_SYN_RCVD",
210 "CXN_KILLED_IMM_DATA_RCVD"
213 static int beiscsi_slave_configure(struct scsi_device *sdev)
215 blk_queue_max_segment_size(sdev->request_queue, 65536);
219 static int beiscsi_eh_abort(struct scsi_cmnd *sc)
221 struct iscsi_cls_session *cls_session;
222 struct iscsi_task *aborted_task = (struct iscsi_task *)sc->SCp.ptr;
223 struct beiscsi_io_task *aborted_io_task;
224 struct iscsi_conn *conn;
225 struct beiscsi_conn *beiscsi_conn;
226 struct beiscsi_hba *phba;
227 struct iscsi_session *session;
228 struct invalidate_command_table *inv_tbl;
229 struct be_dma_mem nonemb_cmd;
230 unsigned int cid, tag, num_invalidate;
233 cls_session = starget_to_session(scsi_target(sc->device));
234 session = cls_session->dd_data;
236 spin_lock_bh(&session->frwd_lock);
237 if (!aborted_task || !aborted_task->sc) {
239 spin_unlock_bh(&session->frwd_lock);
243 aborted_io_task = aborted_task->dd_data;
244 if (!aborted_io_task->scsi_cmnd) {
245 /* raced or invalid command */
246 spin_unlock_bh(&session->frwd_lock);
249 spin_unlock_bh(&session->frwd_lock);
250 /* Invalidate WRB Posted for this Task */
251 AMAP_SET_BITS(struct amap_iscsi_wrb, invld,
252 aborted_io_task->pwrb_handle->pwrb,
255 conn = aborted_task->conn;
256 beiscsi_conn = conn->dd_data;
257 phba = beiscsi_conn->phba;
259 /* invalidate iocb */
260 cid = beiscsi_conn->beiscsi_conn_cid;
261 inv_tbl = phba->inv_tbl;
262 memset(inv_tbl, 0x0, sizeof(*inv_tbl));
264 inv_tbl->icd = aborted_io_task->psgl_handle->sgl_index;
266 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
267 sizeof(struct invalidate_commands_params_in),
269 if (nonemb_cmd.va == NULL) {
270 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
271 "BM_%d : Failed to allocate memory for"
272 "mgmt_invalidate_icds\n");
275 nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
277 tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
280 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
281 "BM_%d : mgmt_invalidate_icds could not be"
283 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
284 nonemb_cmd.va, nonemb_cmd.dma);
289 rc = beiscsi_mccq_compl_wait(phba, tag, NULL, &nonemb_cmd);
291 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
292 nonemb_cmd.va, nonemb_cmd.dma);
294 return iscsi_eh_abort(sc);
297 static int beiscsi_eh_device_reset(struct scsi_cmnd *sc)
299 struct iscsi_task *abrt_task;
300 struct beiscsi_io_task *abrt_io_task;
301 struct iscsi_conn *conn;
302 struct beiscsi_conn *beiscsi_conn;
303 struct beiscsi_hba *phba;
304 struct iscsi_session *session;
305 struct iscsi_cls_session *cls_session;
306 struct invalidate_command_table *inv_tbl;
307 struct be_dma_mem nonemb_cmd;
308 unsigned int cid, tag, i, num_invalidate;
311 /* invalidate iocbs */
312 cls_session = starget_to_session(scsi_target(sc->device));
313 session = cls_session->dd_data;
314 spin_lock_bh(&session->frwd_lock);
315 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) {
316 spin_unlock_bh(&session->frwd_lock);
319 conn = session->leadconn;
320 beiscsi_conn = conn->dd_data;
321 phba = beiscsi_conn->phba;
322 cid = beiscsi_conn->beiscsi_conn_cid;
323 inv_tbl = phba->inv_tbl;
324 memset(inv_tbl, 0x0, sizeof(*inv_tbl) * BE2_CMDS_PER_CXN);
326 for (i = 0; i < conn->session->cmds_max; i++) {
327 abrt_task = conn->session->cmds[i];
328 abrt_io_task = abrt_task->dd_data;
329 if (!abrt_task->sc || abrt_task->state == ISCSI_TASK_FREE)
332 if (sc->device->lun != abrt_task->sc->device->lun)
335 /* Invalidate WRB Posted for this Task */
336 AMAP_SET_BITS(struct amap_iscsi_wrb, invld,
337 abrt_io_task->pwrb_handle->pwrb,
341 inv_tbl->icd = abrt_io_task->psgl_handle->sgl_index;
345 spin_unlock_bh(&session->frwd_lock);
346 inv_tbl = phba->inv_tbl;
348 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
349 sizeof(struct invalidate_commands_params_in),
351 if (nonemb_cmd.va == NULL) {
352 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
353 "BM_%d : Failed to allocate memory for"
354 "mgmt_invalidate_icds\n");
357 nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
358 memset(nonemb_cmd.va, 0, nonemb_cmd.size);
359 tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
362 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
363 "BM_%d : mgmt_invalidate_icds could not be"
365 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
366 nonemb_cmd.va, nonemb_cmd.dma);
370 rc = beiscsi_mccq_compl_wait(phba, tag, NULL, &nonemb_cmd);
372 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
373 nonemb_cmd.va, nonemb_cmd.dma);
374 return iscsi_eh_device_reset(sc);
377 /*------------------- PCI Driver operations and data ----------------- */
378 static const struct pci_device_id beiscsi_pci_id_table[] = {
379 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
380 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
381 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
382 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
383 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID3) },
384 { PCI_DEVICE(ELX_VENDOR_ID, OC_SKH_ID1) },
387 MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
390 static struct scsi_host_template beiscsi_sht = {
391 .module = THIS_MODULE,
392 .name = "Emulex 10Gbe open-iscsi Initiator Driver",
393 .proc_name = DRV_NAME,
394 .queuecommand = iscsi_queuecommand,
395 .change_queue_depth = scsi_change_queue_depth,
396 .slave_configure = beiscsi_slave_configure,
397 .target_alloc = iscsi_target_alloc,
398 .eh_abort_handler = beiscsi_eh_abort,
399 .eh_device_reset_handler = beiscsi_eh_device_reset,
400 .eh_target_reset_handler = iscsi_eh_session_reset,
401 .shost_attrs = beiscsi_attrs,
402 .sg_tablesize = BEISCSI_SGLIST_ELEMENTS,
403 .can_queue = BE2_IO_DEPTH,
405 .max_sectors = BEISCSI_MAX_SECTORS,
406 .cmd_per_lun = BEISCSI_CMD_PER_LUN,
407 .use_clustering = ENABLE_CLUSTERING,
408 .vendor_id = SCSI_NL_VID_TYPE_PCI | BE_VENDOR_ID,
409 .track_queue_depth = 1,
412 static struct scsi_transport_template *beiscsi_scsi_transport;
414 static struct beiscsi_hba *beiscsi_hba_alloc(struct pci_dev *pcidev)
416 struct beiscsi_hba *phba;
417 struct Scsi_Host *shost;
419 shost = iscsi_host_alloc(&beiscsi_sht, sizeof(*phba), 0);
421 dev_err(&pcidev->dev,
422 "beiscsi_hba_alloc - iscsi_host_alloc failed\n");
425 shost->max_id = BE2_MAX_SESSIONS;
426 shost->max_channel = 0;
427 shost->max_cmd_len = BEISCSI_MAX_CMD_LEN;
428 shost->max_lun = BEISCSI_NUM_MAX_LUN;
429 shost->transportt = beiscsi_scsi_transport;
430 phba = iscsi_host_priv(shost);
431 memset(phba, 0, sizeof(*phba));
433 phba->pcidev = pci_dev_get(pcidev);
434 pci_set_drvdata(pcidev, phba);
435 phba->interface_handle = 0xFFFFFFFF;
440 static void beiscsi_unmap_pci_function(struct beiscsi_hba *phba)
443 iounmap(phba->csr_va);
447 iounmap(phba->db_va);
451 iounmap(phba->pci_va);
456 static int beiscsi_map_pci_bars(struct beiscsi_hba *phba,
457 struct pci_dev *pcidev)
462 addr = ioremap_nocache(pci_resource_start(pcidev, 2),
463 pci_resource_len(pcidev, 2));
466 phba->ctrl.csr = addr;
468 phba->csr_pa.u.a64.address = pci_resource_start(pcidev, 2);
470 addr = ioremap_nocache(pci_resource_start(pcidev, 4), 128 * 1024);
473 phba->ctrl.db = addr;
475 phba->db_pa.u.a64.address = pci_resource_start(pcidev, 4);
477 if (phba->generation == BE_GEN2)
482 addr = ioremap_nocache(pci_resource_start(pcidev, pcicfg_reg),
483 pci_resource_len(pcidev, pcicfg_reg));
487 phba->ctrl.pcicfg = addr;
489 phba->pci_pa.u.a64.address = pci_resource_start(pcidev, pcicfg_reg);
493 beiscsi_unmap_pci_function(phba);
497 static int beiscsi_enable_pci(struct pci_dev *pcidev)
501 ret = pci_enable_device(pcidev);
503 dev_err(&pcidev->dev,
504 "beiscsi_enable_pci - enable device failed\n");
508 ret = pci_request_regions(pcidev, DRV_NAME);
510 dev_err(&pcidev->dev,
511 "beiscsi_enable_pci - request region failed\n");
512 goto pci_dev_disable;
515 pci_set_master(pcidev);
516 ret = pci_set_dma_mask(pcidev, DMA_BIT_MASK(64));
518 ret = pci_set_dma_mask(pcidev, DMA_BIT_MASK(32));
520 dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
521 goto pci_region_release;
523 ret = pci_set_consistent_dma_mask(pcidev,
527 ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(64));
529 dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
530 goto pci_region_release;
536 pci_release_regions(pcidev);
538 pci_disable_device(pcidev);
543 static int be_ctrl_init(struct beiscsi_hba *phba, struct pci_dev *pdev)
545 struct be_ctrl_info *ctrl = &phba->ctrl;
546 struct be_dma_mem *mbox_mem_alloc = &ctrl->mbox_mem_alloced;
547 struct be_dma_mem *mbox_mem_align = &ctrl->mbox_mem;
551 status = beiscsi_map_pci_bars(phba, pdev);
554 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
555 mbox_mem_alloc->va = pci_alloc_consistent(pdev,
556 mbox_mem_alloc->size,
557 &mbox_mem_alloc->dma);
558 if (!mbox_mem_alloc->va) {
559 beiscsi_unmap_pci_function(phba);
563 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
564 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
565 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
566 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
567 mutex_init(&ctrl->mbox_lock);
568 spin_lock_init(&phba->ctrl.mcc_lock);
574 * beiscsi_get_params()- Set the config paramters
575 * @phba: ptr device priv structure
577 static void beiscsi_get_params(struct beiscsi_hba *phba)
579 uint32_t total_cid_count = 0;
580 uint32_t total_icd_count = 0;
583 total_cid_count = BEISCSI_GET_CID_COUNT(phba, BEISCSI_ULP0) +
584 BEISCSI_GET_CID_COUNT(phba, BEISCSI_ULP1);
586 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
587 uint32_t align_mask = 0;
588 uint32_t icd_post_per_page = 0;
589 uint32_t icd_count_unavailable = 0;
590 uint32_t icd_start = 0, icd_count = 0;
591 uint32_t icd_start_align = 0, icd_count_align = 0;
593 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
594 icd_start = phba->fw_config.iscsi_icd_start[ulp_num];
595 icd_count = phba->fw_config.iscsi_icd_count[ulp_num];
597 /* Get ICD count that can be posted on each page */
598 icd_post_per_page = (PAGE_SIZE / (BE2_SGE *
599 sizeof(struct iscsi_sge)));
600 align_mask = (icd_post_per_page - 1);
602 /* Check if icd_start is aligned ICD per page posting */
603 if (icd_start % icd_post_per_page) {
604 icd_start_align = ((icd_start +
608 iscsi_icd_start[ulp_num] =
612 icd_count_align = (icd_count & ~align_mask);
614 /* ICD discarded in the process of alignment */
616 icd_count_unavailable = ((icd_start_align -
621 /* Updated ICD count available */
622 phba->fw_config.iscsi_icd_count[ulp_num] = (icd_count -
623 icd_count_unavailable);
625 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
626 "BM_%d : Aligned ICD values\n"
627 "\t ICD Start : %d\n"
628 "\t ICD Count : %d\n"
629 "\t ICD Discarded : %d\n",
631 iscsi_icd_start[ulp_num],
633 iscsi_icd_count[ulp_num],
634 icd_count_unavailable);
639 total_icd_count = phba->fw_config.iscsi_icd_count[ulp_num];
640 phba->params.ios_per_ctrl = (total_icd_count -
642 BE2_TMFS + BE2_NOPOUT_REQ));
643 phba->params.cxns_per_ctrl = total_cid_count;
644 phba->params.asyncpdus_per_ctrl = total_cid_count;
645 phba->params.icds_per_ctrl = total_icd_count;
646 phba->params.num_sge_per_io = BE2_SGE;
647 phba->params.defpdu_hdr_sz = BE2_DEFPDU_HDR_SZ;
648 phba->params.defpdu_data_sz = BE2_DEFPDU_DATA_SZ;
649 phba->params.eq_timer = 64;
650 phba->params.num_eq_entries = 1024;
651 phba->params.num_cq_entries = 1024;
652 phba->params.wrbs_per_cxn = 256;
655 static void hwi_ring_eq_db(struct beiscsi_hba *phba,
656 unsigned int id, unsigned int clr_interrupt,
657 unsigned int num_processed,
658 unsigned char rearm, unsigned char event)
663 val |= 1 << DB_EQ_REARM_SHIFT;
665 val |= 1 << DB_EQ_CLR_SHIFT;
667 val |= 1 << DB_EQ_EVNT_SHIFT;
669 val |= num_processed << DB_EQ_NUM_POPPED_SHIFT;
670 /* Setting lower order EQ_ID Bits */
671 val |= (id & DB_EQ_RING_ID_LOW_MASK);
673 /* Setting Higher order EQ_ID Bits */
674 val |= (((id >> DB_EQ_HIGH_FEILD_SHIFT) &
675 DB_EQ_RING_ID_HIGH_MASK)
676 << DB_EQ_HIGH_SET_SHIFT);
678 iowrite32(val, phba->db_va + DB_EQ_OFFSET);
682 * be_isr_mcc - The isr routine of the driver.
684 * @dev_id: Pointer to host adapter structure
686 static irqreturn_t be_isr_mcc(int irq, void *dev_id)
688 struct beiscsi_hba *phba;
689 struct be_eq_entry *eqe;
690 struct be_queue_info *eq;
691 struct be_queue_info *mcc;
692 unsigned int mcc_events;
693 struct be_eq_obj *pbe_eq;
698 mcc = &phba->ctrl.mcc_obj.cq;
699 eqe = queue_tail_node(eq);
702 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
704 if (((eqe->dw[offsetof(struct amap_eq_entry,
706 EQE_RESID_MASK) >> 16) == mcc->id) {
709 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
711 eqe = queue_tail_node(eq);
715 queue_work(phba->wq, &pbe_eq->mcc_work);
716 hwi_ring_eq_db(phba, eq->id, 1, mcc_events, 1, 1);
722 * be_isr_msix - The isr routine of the driver.
724 * @dev_id: Pointer to host adapter structure
726 static irqreturn_t be_isr_msix(int irq, void *dev_id)
728 struct beiscsi_hba *phba;
729 struct be_queue_info *eq;
730 struct be_eq_obj *pbe_eq;
736 /* disable interrupt till iopoll completes */
737 hwi_ring_eq_db(phba, eq->id, 1, 0, 0, 1);
738 irq_poll_sched(&pbe_eq->iopoll);
744 * be_isr - The isr routine of the driver.
746 * @dev_id: Pointer to host adapter structure
748 static irqreturn_t be_isr(int irq, void *dev_id)
750 struct beiscsi_hba *phba;
751 struct hwi_controller *phwi_ctrlr;
752 struct hwi_context_memory *phwi_context;
753 struct be_eq_entry *eqe;
754 struct be_queue_info *eq;
755 struct be_queue_info *mcc;
756 unsigned int mcc_events, io_events;
757 struct be_ctrl_info *ctrl;
758 struct be_eq_obj *pbe_eq;
763 isr = ioread32(ctrl->csr + CEV_ISR0_OFFSET +
764 (PCI_FUNC(ctrl->pdev->devfn) * CEV_ISR_SIZE));
768 phwi_ctrlr = phba->phwi_ctrlr;
769 phwi_context = phwi_ctrlr->phwi_ctxt;
770 pbe_eq = &phwi_context->be_eq[0];
772 eq = &phwi_context->be_eq[0].q;
773 mcc = &phba->ctrl.mcc_obj.cq;
774 eqe = queue_tail_node(eq);
778 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
780 if (((eqe->dw[offsetof(struct amap_eq_entry,
781 resource_id) / 32] & EQE_RESID_MASK) >> 16) == mcc->id)
785 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
787 eqe = queue_tail_node(eq);
789 if (!io_events && !mcc_events)
792 /* no need to rearm if interrupt is only for IOs */
795 queue_work(phba->wq, &pbe_eq->mcc_work);
800 irq_poll_sched(&pbe_eq->iopoll);
801 hwi_ring_eq_db(phba, eq->id, 0, (io_events + mcc_events), rearm, 1);
806 static int beiscsi_init_irqs(struct beiscsi_hba *phba)
808 struct pci_dev *pcidev = phba->pcidev;
809 struct hwi_controller *phwi_ctrlr;
810 struct hwi_context_memory *phwi_context;
811 int ret, msix_vec, i, j;
813 phwi_ctrlr = phba->phwi_ctrlr;
814 phwi_context = phwi_ctrlr->phwi_ctxt;
816 if (phba->msix_enabled) {
817 for (i = 0; i < phba->num_cpus; i++) {
818 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME,
820 if (!phba->msi_name[i]) {
825 sprintf(phba->msi_name[i], "beiscsi_%02x_%02x",
826 phba->shost->host_no, i);
827 msix_vec = phba->msix_entries[i].vector;
828 ret = request_irq(msix_vec, be_isr_msix, 0,
830 &phwi_context->be_eq[i]);
832 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
833 "BM_%d : beiscsi_init_irqs-Failed to"
834 "register msix for i = %d\n",
836 kfree(phba->msi_name[i]);
840 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME, GFP_KERNEL);
841 if (!phba->msi_name[i]) {
845 sprintf(phba->msi_name[i], "beiscsi_mcc_%02x",
846 phba->shost->host_no);
847 msix_vec = phba->msix_entries[i].vector;
848 ret = request_irq(msix_vec, be_isr_mcc, 0, phba->msi_name[i],
849 &phwi_context->be_eq[i]);
851 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT ,
852 "BM_%d : beiscsi_init_irqs-"
853 "Failed to register beiscsi_msix_mcc\n");
854 kfree(phba->msi_name[i]);
859 ret = request_irq(pcidev->irq, be_isr, IRQF_SHARED,
862 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
863 "BM_%d : beiscsi_init_irqs-"
864 "Failed to register irq\\n");
870 for (j = i - 1; j >= 0; j--) {
871 kfree(phba->msi_name[j]);
872 msix_vec = phba->msix_entries[j].vector;
873 free_irq(msix_vec, &phwi_context->be_eq[j]);
878 void hwi_ring_cq_db(struct beiscsi_hba *phba,
879 unsigned int id, unsigned int num_processed,
885 val |= 1 << DB_CQ_REARM_SHIFT;
887 val |= num_processed << DB_CQ_NUM_POPPED_SHIFT;
889 /* Setting lower order CQ_ID Bits */
890 val |= (id & DB_CQ_RING_ID_LOW_MASK);
892 /* Setting Higher order CQ_ID Bits */
893 val |= (((id >> DB_CQ_HIGH_FEILD_SHIFT) &
894 DB_CQ_RING_ID_HIGH_MASK)
895 << DB_CQ_HIGH_SET_SHIFT);
897 iowrite32(val, phba->db_va + DB_CQ_OFFSET);
901 beiscsi_process_async_pdu(struct beiscsi_conn *beiscsi_conn,
902 struct beiscsi_hba *phba,
903 struct pdu_base *ppdu,
904 unsigned long pdu_len,
905 void *pbuffer, unsigned long buf_len)
907 struct iscsi_conn *conn = beiscsi_conn->conn;
908 struct iscsi_session *session = conn->session;
909 struct iscsi_task *task;
910 struct beiscsi_io_task *io_task;
911 struct iscsi_hdr *login_hdr;
913 switch (ppdu->dw[offsetof(struct amap_pdu_base, opcode) / 32] &
914 PDUBASE_OPCODE_MASK) {
915 case ISCSI_OP_NOOP_IN:
919 case ISCSI_OP_ASYNC_EVENT:
921 case ISCSI_OP_REJECT:
923 WARN_ON(!(buf_len == 48));
924 beiscsi_log(phba, KERN_ERR,
925 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
926 "BM_%d : In ISCSI_OP_REJECT\n");
928 case ISCSI_OP_LOGIN_RSP:
929 case ISCSI_OP_TEXT_RSP:
930 task = conn->login_task;
931 io_task = task->dd_data;
932 login_hdr = (struct iscsi_hdr *)ppdu;
933 login_hdr->itt = io_task->libiscsi_itt;
936 beiscsi_log(phba, KERN_WARNING,
937 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
938 "BM_%d : Unrecognized opcode 0x%x in async msg\n",
940 dw[offsetof(struct amap_pdu_base, opcode) / 32]
941 & PDUBASE_OPCODE_MASK));
945 spin_lock_bh(&session->back_lock);
946 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)ppdu, pbuffer, buf_len);
947 spin_unlock_bh(&session->back_lock);
951 static struct sgl_handle *alloc_io_sgl_handle(struct beiscsi_hba *phba)
953 struct sgl_handle *psgl_handle;
955 spin_lock_bh(&phba->io_sgl_lock);
956 if (phba->io_sgl_hndl_avbl) {
957 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
958 "BM_%d : In alloc_io_sgl_handle,"
959 " io_sgl_alloc_index=%d\n",
960 phba->io_sgl_alloc_index);
962 psgl_handle = phba->io_sgl_hndl_base[phba->
964 phba->io_sgl_hndl_base[phba->io_sgl_alloc_index] = NULL;
965 phba->io_sgl_hndl_avbl--;
966 if (phba->io_sgl_alloc_index == (phba->params.
968 phba->io_sgl_alloc_index = 0;
970 phba->io_sgl_alloc_index++;
973 spin_unlock_bh(&phba->io_sgl_lock);
978 free_io_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
980 spin_lock_bh(&phba->io_sgl_lock);
981 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
982 "BM_%d : In free_,io_sgl_free_index=%d\n",
983 phba->io_sgl_free_index);
985 if (phba->io_sgl_hndl_base[phba->io_sgl_free_index]) {
987 * this can happen if clean_task is called on a task that
988 * failed in xmit_task or alloc_pdu.
990 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
991 "BM_%d : Double Free in IO SGL io_sgl_free_index=%d,"
992 "value there=%p\n", phba->io_sgl_free_index,
993 phba->io_sgl_hndl_base
994 [phba->io_sgl_free_index]);
995 spin_unlock_bh(&phba->io_sgl_lock);
998 phba->io_sgl_hndl_base[phba->io_sgl_free_index] = psgl_handle;
999 phba->io_sgl_hndl_avbl++;
1000 if (phba->io_sgl_free_index == (phba->params.ios_per_ctrl - 1))
1001 phba->io_sgl_free_index = 0;
1003 phba->io_sgl_free_index++;
1004 spin_unlock_bh(&phba->io_sgl_lock);
1007 static inline struct wrb_handle *
1008 beiscsi_get_wrb_handle(struct hwi_wrb_context *pwrb_context,
1009 unsigned int wrbs_per_cxn)
1011 struct wrb_handle *pwrb_handle;
1013 spin_lock_bh(&pwrb_context->wrb_lock);
1014 pwrb_handle = pwrb_context->pwrb_handle_base[pwrb_context->alloc_index];
1015 pwrb_context->wrb_handles_available--;
1016 if (pwrb_context->alloc_index == (wrbs_per_cxn - 1))
1017 pwrb_context->alloc_index = 0;
1019 pwrb_context->alloc_index++;
1020 spin_unlock_bh(&pwrb_context->wrb_lock);
1026 * alloc_wrb_handle - To allocate a wrb handle
1027 * @phba: The hba pointer
1028 * @cid: The cid to use for allocation
1029 * @pwrb_context: ptr to ptr to wrb context
1031 * This happens under session_lock until submission to chip
1033 struct wrb_handle *alloc_wrb_handle(struct beiscsi_hba *phba, unsigned int cid,
1034 struct hwi_wrb_context **pcontext)
1036 struct hwi_wrb_context *pwrb_context;
1037 struct hwi_controller *phwi_ctrlr;
1038 uint16_t cri_index = BE_GET_CRI_FROM_CID(cid);
1040 phwi_ctrlr = phba->phwi_ctrlr;
1041 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
1042 /* return the context address */
1043 *pcontext = pwrb_context;
1044 return beiscsi_get_wrb_handle(pwrb_context, phba->params.wrbs_per_cxn);
1048 beiscsi_put_wrb_handle(struct hwi_wrb_context *pwrb_context,
1049 struct wrb_handle *pwrb_handle,
1050 unsigned int wrbs_per_cxn)
1052 spin_lock_bh(&pwrb_context->wrb_lock);
1053 pwrb_context->pwrb_handle_base[pwrb_context->free_index] = pwrb_handle;
1054 pwrb_context->wrb_handles_available++;
1055 if (pwrb_context->free_index == (wrbs_per_cxn - 1))
1056 pwrb_context->free_index = 0;
1058 pwrb_context->free_index++;
1059 spin_unlock_bh(&pwrb_context->wrb_lock);
1063 * free_wrb_handle - To free the wrb handle back to pool
1064 * @phba: The hba pointer
1065 * @pwrb_context: The context to free from
1066 * @pwrb_handle: The wrb_handle to free
1068 * This happens under session_lock until submission to chip
1071 free_wrb_handle(struct beiscsi_hba *phba, struct hwi_wrb_context *pwrb_context,
1072 struct wrb_handle *pwrb_handle)
1074 beiscsi_put_wrb_handle(pwrb_context,
1076 phba->params.wrbs_per_cxn);
1077 beiscsi_log(phba, KERN_INFO,
1078 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1079 "BM_%d : FREE WRB: pwrb_handle=%p free_index=0x%x"
1080 "wrb_handles_available=%d\n",
1081 pwrb_handle, pwrb_context->free_index,
1082 pwrb_context->wrb_handles_available);
1085 static struct sgl_handle *alloc_mgmt_sgl_handle(struct beiscsi_hba *phba)
1087 struct sgl_handle *psgl_handle;
1089 spin_lock_bh(&phba->mgmt_sgl_lock);
1090 if (phba->eh_sgl_hndl_avbl) {
1091 psgl_handle = phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index];
1092 phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index] = NULL;
1093 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1094 "BM_%d : mgmt_sgl_alloc_index=%d=0x%x\n",
1095 phba->eh_sgl_alloc_index,
1096 phba->eh_sgl_alloc_index);
1098 phba->eh_sgl_hndl_avbl--;
1099 if (phba->eh_sgl_alloc_index ==
1100 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl -
1102 phba->eh_sgl_alloc_index = 0;
1104 phba->eh_sgl_alloc_index++;
1107 spin_unlock_bh(&phba->mgmt_sgl_lock);
1112 free_mgmt_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
1114 spin_lock_bh(&phba->mgmt_sgl_lock);
1115 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1116 "BM_%d : In free_mgmt_sgl_handle,"
1117 "eh_sgl_free_index=%d\n",
1118 phba->eh_sgl_free_index);
1120 if (phba->eh_sgl_hndl_base[phba->eh_sgl_free_index]) {
1122 * this can happen if clean_task is called on a task that
1123 * failed in xmit_task or alloc_pdu.
1125 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
1126 "BM_%d : Double Free in eh SGL ,"
1127 "eh_sgl_free_index=%d\n",
1128 phba->eh_sgl_free_index);
1129 spin_unlock_bh(&phba->mgmt_sgl_lock);
1132 phba->eh_sgl_hndl_base[phba->eh_sgl_free_index] = psgl_handle;
1133 phba->eh_sgl_hndl_avbl++;
1134 if (phba->eh_sgl_free_index ==
1135 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl - 1))
1136 phba->eh_sgl_free_index = 0;
1138 phba->eh_sgl_free_index++;
1139 spin_unlock_bh(&phba->mgmt_sgl_lock);
1143 be_complete_io(struct beiscsi_conn *beiscsi_conn,
1144 struct iscsi_task *task,
1145 struct common_sol_cqe *csol_cqe)
1147 struct beiscsi_io_task *io_task = task->dd_data;
1148 struct be_status_bhs *sts_bhs =
1149 (struct be_status_bhs *)io_task->cmd_bhs;
1150 struct iscsi_conn *conn = beiscsi_conn->conn;
1151 unsigned char *sense;
1152 u32 resid = 0, exp_cmdsn, max_cmdsn;
1153 u8 rsp, status, flags;
1155 exp_cmdsn = csol_cqe->exp_cmdsn;
1156 max_cmdsn = (csol_cqe->exp_cmdsn +
1157 csol_cqe->cmd_wnd - 1);
1158 rsp = csol_cqe->i_resp;
1159 status = csol_cqe->i_sts;
1160 flags = csol_cqe->i_flags;
1161 resid = csol_cqe->res_cnt;
1164 if (io_task->scsi_cmnd) {
1165 scsi_dma_unmap(io_task->scsi_cmnd);
1166 io_task->scsi_cmnd = NULL;
1171 task->sc->result = (DID_OK << 16) | status;
1172 if (rsp != ISCSI_STATUS_CMD_COMPLETED) {
1173 task->sc->result = DID_ERROR << 16;
1177 /* bidi not initially supported */
1178 if (flags & (ISCSI_FLAG_CMD_UNDERFLOW | ISCSI_FLAG_CMD_OVERFLOW)) {
1179 if (!status && (flags & ISCSI_FLAG_CMD_OVERFLOW))
1180 task->sc->result = DID_ERROR << 16;
1182 if (flags & ISCSI_FLAG_CMD_UNDERFLOW) {
1183 scsi_set_resid(task->sc, resid);
1184 if (!status && (scsi_bufflen(task->sc) - resid <
1185 task->sc->underflow))
1186 task->sc->result = DID_ERROR << 16;
1190 if (status == SAM_STAT_CHECK_CONDITION) {
1192 unsigned short *slen = (unsigned short *)sts_bhs->sense_info;
1194 sense = sts_bhs->sense_info + sizeof(unsigned short);
1195 sense_len = be16_to_cpu(*slen);
1196 memcpy(task->sc->sense_buffer, sense,
1197 min_t(u16, sense_len, SCSI_SENSE_BUFFERSIZE));
1200 if (io_task->cmd_bhs->iscsi_hdr.flags & ISCSI_FLAG_CMD_READ)
1201 conn->rxdata_octets += resid;
1203 if (io_task->scsi_cmnd) {
1204 scsi_dma_unmap(io_task->scsi_cmnd);
1205 io_task->scsi_cmnd = NULL;
1207 iscsi_complete_scsi_task(task, exp_cmdsn, max_cmdsn);
1211 be_complete_logout(struct beiscsi_conn *beiscsi_conn,
1212 struct iscsi_task *task,
1213 struct common_sol_cqe *csol_cqe)
1215 struct iscsi_logout_rsp *hdr;
1216 struct beiscsi_io_task *io_task = task->dd_data;
1217 struct iscsi_conn *conn = beiscsi_conn->conn;
1219 hdr = (struct iscsi_logout_rsp *)task->hdr;
1220 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
1223 hdr->flags = csol_cqe->i_flags;
1224 hdr->response = csol_cqe->i_resp;
1225 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
1226 hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
1227 csol_cqe->cmd_wnd - 1);
1229 hdr->dlength[0] = 0;
1230 hdr->dlength[1] = 0;
1231 hdr->dlength[2] = 0;
1233 hdr->itt = io_task->libiscsi_itt;
1234 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1238 be_complete_tmf(struct beiscsi_conn *beiscsi_conn,
1239 struct iscsi_task *task,
1240 struct common_sol_cqe *csol_cqe)
1242 struct iscsi_tm_rsp *hdr;
1243 struct iscsi_conn *conn = beiscsi_conn->conn;
1244 struct beiscsi_io_task *io_task = task->dd_data;
1246 hdr = (struct iscsi_tm_rsp *)task->hdr;
1247 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
1248 hdr->flags = csol_cqe->i_flags;
1249 hdr->response = csol_cqe->i_resp;
1250 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
1251 hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
1252 csol_cqe->cmd_wnd - 1);
1254 hdr->itt = io_task->libiscsi_itt;
1255 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1259 hwi_complete_drvr_msgs(struct beiscsi_conn *beiscsi_conn,
1260 struct beiscsi_hba *phba, struct sol_cqe *psol)
1262 struct hwi_wrb_context *pwrb_context;
1263 struct wrb_handle *pwrb_handle = NULL;
1264 struct hwi_controller *phwi_ctrlr;
1265 struct iscsi_task *task;
1266 struct beiscsi_io_task *io_task;
1267 uint16_t wrb_index, cid, cri_index;
1269 phwi_ctrlr = phba->phwi_ctrlr;
1270 if (is_chip_be2_be3r(phba)) {
1271 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1273 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1276 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1278 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1282 cri_index = BE_GET_CRI_FROM_CID(cid);
1283 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
1284 pwrb_handle = pwrb_context->pwrb_handle_basestd[wrb_index];
1285 task = pwrb_handle->pio_handle;
1287 io_task = task->dd_data;
1288 memset(io_task->pwrb_handle->pwrb, 0, sizeof(struct iscsi_wrb));
1289 iscsi_put_task(task);
1293 be_complete_nopin_resp(struct beiscsi_conn *beiscsi_conn,
1294 struct iscsi_task *task,
1295 struct common_sol_cqe *csol_cqe)
1297 struct iscsi_nopin *hdr;
1298 struct iscsi_conn *conn = beiscsi_conn->conn;
1299 struct beiscsi_io_task *io_task = task->dd_data;
1301 hdr = (struct iscsi_nopin *)task->hdr;
1302 hdr->flags = csol_cqe->i_flags;
1303 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
1304 hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
1305 csol_cqe->cmd_wnd - 1);
1307 hdr->opcode = ISCSI_OP_NOOP_IN;
1308 hdr->itt = io_task->libiscsi_itt;
1309 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1312 static void adapter_get_sol_cqe(struct beiscsi_hba *phba,
1313 struct sol_cqe *psol,
1314 struct common_sol_cqe *csol_cqe)
1316 if (is_chip_be2_be3r(phba)) {
1317 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe,
1318 i_exp_cmd_sn, psol);
1319 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe,
1321 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe,
1323 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe,
1325 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe,
1327 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1329 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe,
1331 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1333 csol_cqe->i_flags = AMAP_GET_BITS(struct amap_sol_cqe,
1336 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1337 i_exp_cmd_sn, psol);
1338 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1340 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1342 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1344 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1346 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1348 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1350 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1353 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1355 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1357 csol_cqe->i_flags = ISCSI_FLAG_CMD_UNDERFLOW;
1359 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1361 csol_cqe->i_flags |= ISCSI_FLAG_CMD_OVERFLOW;
1366 static void hwi_complete_cmd(struct beiscsi_conn *beiscsi_conn,
1367 struct beiscsi_hba *phba, struct sol_cqe *psol)
1369 struct hwi_wrb_context *pwrb_context;
1370 struct wrb_handle *pwrb_handle;
1371 struct iscsi_wrb *pwrb = NULL;
1372 struct hwi_controller *phwi_ctrlr;
1373 struct iscsi_task *task;
1375 struct iscsi_conn *conn = beiscsi_conn->conn;
1376 struct iscsi_session *session = conn->session;
1377 struct common_sol_cqe csol_cqe = {0};
1378 uint16_t cri_index = 0;
1380 phwi_ctrlr = phba->phwi_ctrlr;
1382 /* Copy the elements to a common structure */
1383 adapter_get_sol_cqe(phba, psol, &csol_cqe);
1385 cri_index = BE_GET_CRI_FROM_CID(csol_cqe.cid);
1386 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
1388 pwrb_handle = pwrb_context->pwrb_handle_basestd[
1389 csol_cqe.wrb_index];
1391 task = pwrb_handle->pio_handle;
1392 pwrb = pwrb_handle->pwrb;
1393 type = ((struct beiscsi_io_task *)task->dd_data)->wrb_type;
1395 spin_lock_bh(&session->back_lock);
1398 case HWH_TYPE_IO_RD:
1399 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) ==
1401 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
1403 be_complete_io(beiscsi_conn, task, &csol_cqe);
1406 case HWH_TYPE_LOGOUT:
1407 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
1408 be_complete_logout(beiscsi_conn, task, &csol_cqe);
1410 be_complete_tmf(beiscsi_conn, task, &csol_cqe);
1413 case HWH_TYPE_LOGIN:
1414 beiscsi_log(phba, KERN_ERR,
1415 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1416 "BM_%d :\t\t No HWH_TYPE_LOGIN Expected in"
1417 " hwi_complete_cmd- Solicited path\n");
1421 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
1425 beiscsi_log(phba, KERN_WARNING,
1426 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1427 "BM_%d : In hwi_complete_cmd, unknown type = %d"
1428 "wrb_index 0x%x CID 0x%x\n", type,
1434 spin_unlock_bh(&session->back_lock);
1437 static struct list_head *hwi_get_async_busy_list(struct hwi_async_pdu_context
1438 *pasync_ctx, unsigned int is_header,
1439 unsigned int host_write_ptr)
1442 return &pasync_ctx->async_entry[host_write_ptr].
1445 return &pasync_ctx->async_entry[host_write_ptr].data_busy_list;
1448 static struct async_pdu_handle *
1449 hwi_get_async_handle(struct beiscsi_hba *phba,
1450 struct beiscsi_conn *beiscsi_conn,
1451 struct hwi_async_pdu_context *pasync_ctx,
1452 struct i_t_dpdu_cqe *pdpdu_cqe, unsigned int *pcq_index)
1454 struct be_bus_address phys_addr;
1455 struct list_head *pbusy_list;
1456 struct async_pdu_handle *pasync_handle = NULL;
1457 unsigned char is_header = 0;
1458 unsigned int index, dpl;
1460 if (is_chip_be2_be3r(phba)) {
1461 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1463 index = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1466 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1468 index = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1472 phys_addr.u.a32.address_lo =
1473 (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1474 db_addr_lo) / 32] - dpl);
1475 phys_addr.u.a32.address_hi =
1476 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1479 phys_addr.u.a64.address =
1480 *((unsigned long long *)(&phys_addr.u.a64.address));
1482 switch (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, code) / 32]
1483 & PDUCQE_CODE_MASK) {
1484 case UNSOL_HDR_NOTIFY:
1487 pbusy_list = hwi_get_async_busy_list(pasync_ctx,
1490 case UNSOL_DATA_NOTIFY:
1491 pbusy_list = hwi_get_async_busy_list(pasync_ctx,
1496 beiscsi_log(phba, KERN_WARNING,
1497 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1498 "BM_%d : Unexpected code=%d\n",
1499 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1500 code) / 32] & PDUCQE_CODE_MASK);
1504 WARN_ON(list_empty(pbusy_list));
1505 list_for_each_entry(pasync_handle, pbusy_list, link) {
1506 if (pasync_handle->pa.u.a64.address == phys_addr.u.a64.address)
1510 WARN_ON(!pasync_handle);
1512 pasync_handle->cri = BE_GET_ASYNC_CRI_FROM_CID(
1513 beiscsi_conn->beiscsi_conn_cid);
1514 pasync_handle->is_header = is_header;
1515 pasync_handle->buffer_len = dpl;
1518 return pasync_handle;
1522 hwi_update_async_writables(struct beiscsi_hba *phba,
1523 struct hwi_async_pdu_context *pasync_ctx,
1524 unsigned int is_header, unsigned int cq_index)
1526 struct list_head *pbusy_list;
1527 struct async_pdu_handle *pasync_handle;
1528 unsigned int num_entries, writables = 0;
1529 unsigned int *pep_read_ptr, *pwritables;
1531 num_entries = pasync_ctx->num_entries;
1533 pep_read_ptr = &pasync_ctx->async_header.ep_read_ptr;
1534 pwritables = &pasync_ctx->async_header.writables;
1536 pep_read_ptr = &pasync_ctx->async_data.ep_read_ptr;
1537 pwritables = &pasync_ctx->async_data.writables;
1540 while ((*pep_read_ptr) != cq_index) {
1542 *pep_read_ptr = (*pep_read_ptr) % num_entries;
1544 pbusy_list = hwi_get_async_busy_list(pasync_ctx, is_header,
1547 WARN_ON(list_empty(pbusy_list));
1549 if (!list_empty(pbusy_list)) {
1550 pasync_handle = list_entry(pbusy_list->next,
1551 struct async_pdu_handle,
1553 WARN_ON(!pasync_handle);
1554 pasync_handle->consumed = 1;
1561 beiscsi_log(phba, KERN_ERR,
1562 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1563 "BM_%d : Duplicate notification received - index 0x%x!!\n",
1568 *pwritables = *pwritables + writables;
1572 static void hwi_free_async_msg(struct beiscsi_hba *phba,
1573 struct hwi_async_pdu_context *pasync_ctx,
1576 struct async_pdu_handle *pasync_handle, *tmp_handle;
1577 struct list_head *plist;
1579 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1580 list_for_each_entry_safe(pasync_handle, tmp_handle, plist, link) {
1581 list_del(&pasync_handle->link);
1583 if (pasync_handle->is_header) {
1584 list_add_tail(&pasync_handle->link,
1585 &pasync_ctx->async_header.free_list);
1586 pasync_ctx->async_header.free_entries++;
1588 list_add_tail(&pasync_handle->link,
1589 &pasync_ctx->async_data.free_list);
1590 pasync_ctx->async_data.free_entries++;
1594 INIT_LIST_HEAD(&pasync_ctx->async_entry[cri].wait_queue.list);
1595 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 0;
1596 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
1599 static struct phys_addr *
1600 hwi_get_ring_address(struct hwi_async_pdu_context *pasync_ctx,
1601 unsigned int is_header, unsigned int host_write_ptr)
1603 struct phys_addr *pasync_sge = NULL;
1606 pasync_sge = pasync_ctx->async_header.ring_base;
1608 pasync_sge = pasync_ctx->async_data.ring_base;
1610 return pasync_sge + host_write_ptr;
1613 static void hwi_post_async_buffers(struct beiscsi_hba *phba,
1614 unsigned int is_header, uint8_t ulp_num)
1616 struct hwi_controller *phwi_ctrlr;
1617 struct hwi_async_pdu_context *pasync_ctx;
1618 struct async_pdu_handle *pasync_handle;
1619 struct list_head *pfree_link, *pbusy_list;
1620 struct phys_addr *pasync_sge;
1621 unsigned int ring_id, num_entries;
1622 unsigned int host_write_num, doorbell_offset;
1623 unsigned int writables;
1627 phwi_ctrlr = phba->phwi_ctrlr;
1628 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr, ulp_num);
1629 num_entries = pasync_ctx->num_entries;
1632 writables = min(pasync_ctx->async_header.writables,
1633 pasync_ctx->async_header.free_entries);
1634 pfree_link = pasync_ctx->async_header.free_list.next;
1635 host_write_num = pasync_ctx->async_header.host_write_ptr;
1636 ring_id = phwi_ctrlr->default_pdu_hdr[ulp_num].id;
1637 doorbell_offset = phwi_ctrlr->default_pdu_hdr[ulp_num].
1640 writables = min(pasync_ctx->async_data.writables,
1641 pasync_ctx->async_data.free_entries);
1642 pfree_link = pasync_ctx->async_data.free_list.next;
1643 host_write_num = pasync_ctx->async_data.host_write_ptr;
1644 ring_id = phwi_ctrlr->default_pdu_data[ulp_num].id;
1645 doorbell_offset = phwi_ctrlr->default_pdu_data[ulp_num].
1649 writables = (writables / 8) * 8;
1651 for (i = 0; i < writables; i++) {
1653 hwi_get_async_busy_list(pasync_ctx, is_header,
1656 list_entry(pfree_link, struct async_pdu_handle,
1658 WARN_ON(!pasync_handle);
1659 pasync_handle->consumed = 0;
1661 pfree_link = pfree_link->next;
1663 pasync_sge = hwi_get_ring_address(pasync_ctx,
1664 is_header, host_write_num);
1666 pasync_sge->hi = pasync_handle->pa.u.a32.address_lo;
1667 pasync_sge->lo = pasync_handle->pa.u.a32.address_hi;
1669 list_move(&pasync_handle->link, pbusy_list);
1672 host_write_num = host_write_num % num_entries;
1676 pasync_ctx->async_header.host_write_ptr =
1678 pasync_ctx->async_header.free_entries -= writables;
1679 pasync_ctx->async_header.writables -= writables;
1680 pasync_ctx->async_header.busy_entries += writables;
1682 pasync_ctx->async_data.host_write_ptr = host_write_num;
1683 pasync_ctx->async_data.free_entries -= writables;
1684 pasync_ctx->async_data.writables -= writables;
1685 pasync_ctx->async_data.busy_entries += writables;
1688 doorbell |= ring_id & DB_DEF_PDU_RING_ID_MASK;
1689 doorbell |= 1 << DB_DEF_PDU_REARM_SHIFT;
1690 doorbell |= 0 << DB_DEF_PDU_EVENT_SHIFT;
1691 doorbell |= (writables & DB_DEF_PDU_CQPROC_MASK)
1692 << DB_DEF_PDU_CQPROC_SHIFT;
1694 iowrite32(doorbell, phba->db_va + doorbell_offset);
1698 static void hwi_flush_default_pdu_buffer(struct beiscsi_hba *phba,
1699 struct beiscsi_conn *beiscsi_conn,
1700 struct i_t_dpdu_cqe *pdpdu_cqe)
1702 struct hwi_controller *phwi_ctrlr;
1703 struct hwi_async_pdu_context *pasync_ctx;
1704 struct async_pdu_handle *pasync_handle = NULL;
1705 unsigned int cq_index = -1;
1706 uint16_t cri_index = BE_GET_CRI_FROM_CID(
1707 beiscsi_conn->beiscsi_conn_cid);
1709 phwi_ctrlr = phba->phwi_ctrlr;
1710 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr,
1711 BEISCSI_GET_ULP_FROM_CRI(phwi_ctrlr,
1714 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1715 pdpdu_cqe, &cq_index);
1716 BUG_ON(pasync_handle->is_header != 0);
1717 if (pasync_handle->consumed == 0)
1718 hwi_update_async_writables(phba, pasync_ctx,
1719 pasync_handle->is_header, cq_index);
1721 hwi_free_async_msg(phba, pasync_ctx, pasync_handle->cri);
1722 hwi_post_async_buffers(phba, pasync_handle->is_header,
1723 BEISCSI_GET_ULP_FROM_CRI(phwi_ctrlr,
1728 hwi_fwd_async_msg(struct beiscsi_conn *beiscsi_conn,
1729 struct beiscsi_hba *phba,
1730 struct hwi_async_pdu_context *pasync_ctx, unsigned short cri)
1732 struct list_head *plist;
1733 struct async_pdu_handle *pasync_handle;
1735 unsigned int hdr_len = 0, buf_len = 0;
1736 unsigned int status, index = 0, offset = 0;
1737 void *pfirst_buffer = NULL;
1738 unsigned int num_buf = 0;
1740 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1742 list_for_each_entry(pasync_handle, plist, link) {
1744 phdr = pasync_handle->pbuffer;
1745 hdr_len = pasync_handle->buffer_len;
1747 buf_len = pasync_handle->buffer_len;
1749 pfirst_buffer = pasync_handle->pbuffer;
1752 memcpy(pfirst_buffer + offset,
1753 pasync_handle->pbuffer, buf_len);
1759 status = beiscsi_process_async_pdu(beiscsi_conn, phba,
1760 phdr, hdr_len, pfirst_buffer,
1763 hwi_free_async_msg(phba, pasync_ctx, cri);
1768 hwi_gather_async_pdu(struct beiscsi_conn *beiscsi_conn,
1769 struct beiscsi_hba *phba,
1770 struct async_pdu_handle *pasync_handle)
1772 struct hwi_async_pdu_context *pasync_ctx;
1773 struct hwi_controller *phwi_ctrlr;
1774 unsigned int bytes_needed = 0, status = 0;
1775 unsigned short cri = pasync_handle->cri;
1776 struct pdu_base *ppdu;
1778 phwi_ctrlr = phba->phwi_ctrlr;
1779 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr,
1780 BEISCSI_GET_ULP_FROM_CRI(phwi_ctrlr,
1781 BE_GET_CRI_FROM_CID(beiscsi_conn->
1782 beiscsi_conn_cid)));
1784 list_del(&pasync_handle->link);
1785 if (pasync_handle->is_header) {
1786 pasync_ctx->async_header.busy_entries--;
1787 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1788 hwi_free_async_msg(phba, pasync_ctx, cri);
1792 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
1793 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 1;
1794 pasync_ctx->async_entry[cri].wait_queue.hdr_len =
1795 (unsigned short)pasync_handle->buffer_len;
1796 list_add_tail(&pasync_handle->link,
1797 &pasync_ctx->async_entry[cri].wait_queue.list);
1799 ppdu = pasync_handle->pbuffer;
1800 bytes_needed = ((((ppdu->dw[offsetof(struct amap_pdu_base,
1801 data_len_hi) / 32] & PDUBASE_DATALENHI_MASK) << 8) &
1802 0xFFFF0000) | ((be16_to_cpu((ppdu->
1803 dw[offsetof(struct amap_pdu_base, data_len_lo) / 32]
1804 & PDUBASE_DATALENLO_MASK) >> 16)) & 0x0000FFFF));
1807 pasync_ctx->async_entry[cri].wait_queue.bytes_needed =
1810 if (bytes_needed == 0)
1811 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1815 pasync_ctx->async_data.busy_entries--;
1816 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1817 list_add_tail(&pasync_handle->link,
1818 &pasync_ctx->async_entry[cri].wait_queue.
1820 pasync_ctx->async_entry[cri].wait_queue.
1822 (unsigned short)pasync_handle->buffer_len;
1824 if (pasync_ctx->async_entry[cri].wait_queue.
1826 pasync_ctx->async_entry[cri].wait_queue.
1828 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1835 static void hwi_process_default_pdu_ring(struct beiscsi_conn *beiscsi_conn,
1836 struct beiscsi_hba *phba,
1837 struct i_t_dpdu_cqe *pdpdu_cqe)
1839 struct hwi_controller *phwi_ctrlr;
1840 struct hwi_async_pdu_context *pasync_ctx;
1841 struct async_pdu_handle *pasync_handle = NULL;
1842 unsigned int cq_index = -1;
1843 uint16_t cri_index = BE_GET_CRI_FROM_CID(
1844 beiscsi_conn->beiscsi_conn_cid);
1846 phwi_ctrlr = phba->phwi_ctrlr;
1847 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr,
1848 BEISCSI_GET_ULP_FROM_CRI(phwi_ctrlr,
1851 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1852 pdpdu_cqe, &cq_index);
1854 if (pasync_handle->consumed == 0)
1855 hwi_update_async_writables(phba, pasync_ctx,
1856 pasync_handle->is_header, cq_index);
1858 hwi_gather_async_pdu(beiscsi_conn, phba, pasync_handle);
1859 hwi_post_async_buffers(phba, pasync_handle->is_header,
1860 BEISCSI_GET_ULP_FROM_CRI(
1861 phwi_ctrlr, cri_index));
1864 void beiscsi_process_mcc_cq(struct beiscsi_hba *phba)
1866 struct be_queue_info *mcc_cq;
1867 struct be_mcc_compl *mcc_compl;
1868 unsigned int num_processed = 0;
1870 mcc_cq = &phba->ctrl.mcc_obj.cq;
1871 mcc_compl = queue_tail_node(mcc_cq);
1872 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1873 while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
1874 if (beiscsi_hba_in_error(phba))
1877 if (num_processed >= 32) {
1878 hwi_ring_cq_db(phba, mcc_cq->id,
1882 if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
1883 beiscsi_process_async_event(phba, mcc_compl);
1884 } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
1885 beiscsi_process_mcc_compl(&phba->ctrl, mcc_compl);
1888 mcc_compl->flags = 0;
1889 queue_tail_inc(mcc_cq);
1890 mcc_compl = queue_tail_node(mcc_cq);
1891 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1895 if (num_processed > 0)
1896 hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1);
1899 static void beiscsi_mcc_work(struct work_struct *work)
1901 struct be_eq_obj *pbe_eq;
1902 struct beiscsi_hba *phba;
1904 pbe_eq = container_of(work, struct be_eq_obj, mcc_work);
1905 phba = pbe_eq->phba;
1906 beiscsi_process_mcc_cq(phba);
1907 /* rearm EQ for further interrupts */
1908 if (!beiscsi_hba_in_error(phba))
1909 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
1913 * beiscsi_process_cq()- Process the Completion Queue
1914 * @pbe_eq: Event Q on which the Completion has come
1915 * @budget: Max number of events to processed
1918 * Number of Completion Entries processed.
1920 unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq, int budget)
1922 struct be_queue_info *cq;
1923 struct sol_cqe *sol;
1924 struct dmsg_cqe *dmsg;
1925 unsigned int total = 0;
1926 unsigned int num_processed = 0;
1927 unsigned short code = 0, cid = 0;
1928 uint16_t cri_index = 0;
1929 struct beiscsi_conn *beiscsi_conn;
1930 struct beiscsi_endpoint *beiscsi_ep;
1931 struct iscsi_endpoint *ep;
1932 struct beiscsi_hba *phba;
1935 sol = queue_tail_node(cq);
1936 phba = pbe_eq->phba;
1938 while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
1940 if (beiscsi_hba_in_error(phba))
1943 be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
1945 code = (sol->dw[offsetof(struct amap_sol_cqe, code) /
1946 32] & CQE_CODE_MASK);
1949 if (is_chip_be2_be3r(phba)) {
1950 cid = AMAP_GET_BITS(struct amap_sol_cqe, cid, sol);
1952 if ((code == DRIVERMSG_NOTIFY) ||
1953 (code == UNSOL_HDR_NOTIFY) ||
1954 (code == UNSOL_DATA_NOTIFY))
1955 cid = AMAP_GET_BITS(
1956 struct amap_i_t_dpdu_cqe_v2,
1959 cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1963 cri_index = BE_GET_CRI_FROM_CID(cid);
1964 ep = phba->ep_array[cri_index];
1967 /* connection has already been freed
1968 * just move on to next one
1970 beiscsi_log(phba, KERN_WARNING,
1972 "BM_%d : proc cqe of disconn ep: cid %d\n",
1977 beiscsi_ep = ep->dd_data;
1978 beiscsi_conn = beiscsi_ep->conn;
1981 if (num_processed == 32) {
1982 hwi_ring_cq_db(phba, cq->id, 32, 0);
1988 case SOL_CMD_COMPLETE:
1989 hwi_complete_cmd(beiscsi_conn, phba, sol);
1991 case DRIVERMSG_NOTIFY:
1992 beiscsi_log(phba, KERN_INFO,
1993 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1994 "BM_%d : Received %s[%d] on CID : %d\n",
1995 cqe_desc[code], code, cid);
1997 dmsg = (struct dmsg_cqe *)sol;
1998 hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
2000 case UNSOL_HDR_NOTIFY:
2001 beiscsi_log(phba, KERN_INFO,
2002 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
2003 "BM_%d : Received %s[%d] on CID : %d\n",
2004 cqe_desc[code], code, cid);
2006 spin_lock_bh(&phba->async_pdu_lock);
2007 hwi_process_default_pdu_ring(beiscsi_conn, phba,
2008 (struct i_t_dpdu_cqe *)sol);
2009 spin_unlock_bh(&phba->async_pdu_lock);
2011 case UNSOL_DATA_NOTIFY:
2012 beiscsi_log(phba, KERN_INFO,
2013 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
2014 "BM_%d : Received %s[%d] on CID : %d\n",
2015 cqe_desc[code], code, cid);
2017 spin_lock_bh(&phba->async_pdu_lock);
2018 hwi_process_default_pdu_ring(beiscsi_conn, phba,
2019 (struct i_t_dpdu_cqe *)sol);
2020 spin_unlock_bh(&phba->async_pdu_lock);
2022 case CXN_INVALIDATE_INDEX_NOTIFY:
2023 case CMD_INVALIDATED_NOTIFY:
2024 case CXN_INVALIDATE_NOTIFY:
2025 beiscsi_log(phba, KERN_ERR,
2026 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
2027 "BM_%d : Ignoring %s[%d] on CID : %d\n",
2028 cqe_desc[code], code, cid);
2030 case CXN_KILLED_HDR_DIGEST_ERR:
2031 case SOL_CMD_KILLED_DATA_DIGEST_ERR:
2032 beiscsi_log(phba, KERN_ERR,
2033 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
2034 "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
2035 cqe_desc[code], code, cid);
2037 case CMD_KILLED_INVALID_STATSN_RCVD:
2038 case CMD_KILLED_INVALID_R2T_RCVD:
2039 case CMD_CXN_KILLED_LUN_INVALID:
2040 case CMD_CXN_KILLED_ICD_INVALID:
2041 case CMD_CXN_KILLED_ITT_INVALID:
2042 case CMD_CXN_KILLED_SEQ_OUTOFORDER:
2043 case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
2044 beiscsi_log(phba, KERN_ERR,
2045 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
2046 "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
2047 cqe_desc[code], code, cid);
2049 case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
2050 beiscsi_log(phba, KERN_ERR,
2051 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
2052 "BM_%d : Dropping %s[%d] on DPDU ring on CID : %d\n",
2053 cqe_desc[code], code, cid);
2054 spin_lock_bh(&phba->async_pdu_lock);
2055 hwi_flush_default_pdu_buffer(phba, beiscsi_conn,
2056 (struct i_t_dpdu_cqe *) sol);
2057 spin_unlock_bh(&phba->async_pdu_lock);
2059 case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
2060 case CXN_KILLED_BURST_LEN_MISMATCH:
2061 case CXN_KILLED_AHS_RCVD:
2062 case CXN_KILLED_UNKNOWN_HDR:
2063 case CXN_KILLED_STALE_ITT_TTT_RCVD:
2064 case CXN_KILLED_INVALID_ITT_TTT_RCVD:
2065 case CXN_KILLED_TIMED_OUT:
2066 case CXN_KILLED_FIN_RCVD:
2067 case CXN_KILLED_RST_SENT:
2068 case CXN_KILLED_RST_RCVD:
2069 case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
2070 case CXN_KILLED_BAD_WRB_INDEX_ERROR:
2071 case CXN_KILLED_OVER_RUN_RESIDUAL:
2072 case CXN_KILLED_UNDER_RUN_RESIDUAL:
2073 case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
2074 beiscsi_log(phba, KERN_ERR,
2075 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
2076 "BM_%d : Event %s[%d] received on CID : %d\n",
2077 cqe_desc[code], code, cid);
2079 iscsi_conn_failure(beiscsi_conn->conn,
2080 ISCSI_ERR_CONN_FAILED);
2083 beiscsi_log(phba, KERN_ERR,
2084 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
2085 "BM_%d : Invalid CQE Event Received Code : %d"
2092 AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
2094 sol = queue_tail_node(cq);
2096 if (total == budget)
2100 hwi_ring_cq_db(phba, cq->id, num_processed, 1);
2104 static int be_iopoll(struct irq_poll *iop, int budget)
2106 unsigned int ret, io_events;
2107 struct beiscsi_hba *phba;
2108 struct be_eq_obj *pbe_eq;
2109 struct be_eq_entry *eqe = NULL;
2110 struct be_queue_info *eq;
2112 pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
2113 phba = pbe_eq->phba;
2114 if (beiscsi_hba_in_error(phba)) {
2115 irq_poll_complete(iop);
2121 eqe = queue_tail_node(eq);
2122 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32] &
2124 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
2126 eqe = queue_tail_node(eq);
2129 hwi_ring_eq_db(phba, eq->id, 1, io_events, 0, 1);
2131 ret = beiscsi_process_cq(pbe_eq, budget);
2132 pbe_eq->cq_count += ret;
2134 irq_poll_complete(iop);
2135 beiscsi_log(phba, KERN_INFO,
2136 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
2137 "BM_%d : rearm pbe_eq->q.id =%d ret %d\n",
2139 if (!beiscsi_hba_in_error(phba))
2140 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
2146 hwi_write_sgl_v2(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2147 unsigned int num_sg, struct beiscsi_io_task *io_task)
2149 struct iscsi_sge *psgl;
2150 unsigned int sg_len, index;
2151 unsigned int sge_len = 0;
2152 unsigned long long addr;
2153 struct scatterlist *l_sg;
2154 unsigned int offset;
2156 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_lo, pwrb,
2157 io_task->bhs_pa.u.a32.address_lo);
2158 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_hi, pwrb,
2159 io_task->bhs_pa.u.a32.address_hi);
2162 for (index = 0; (index < num_sg) && (index < 2); index++,
2165 sg_len = sg_dma_len(sg);
2166 addr = (u64) sg_dma_address(sg);
2167 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2169 lower_32_bits(addr));
2170 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2172 upper_32_bits(addr));
2173 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2178 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_r2t_offset,
2180 sg_len = sg_dma_len(sg);
2181 addr = (u64) sg_dma_address(sg);
2182 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2184 lower_32_bits(addr));
2185 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2187 upper_32_bits(addr));
2188 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2193 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2194 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2196 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2198 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2199 io_task->bhs_pa.u.a32.address_hi);
2200 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2201 io_task->bhs_pa.u.a32.address_lo);
2204 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2206 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2208 } else if (num_sg == 2) {
2209 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2211 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2214 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2216 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2224 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
2225 sg_len = sg_dma_len(sg);
2226 addr = (u64) sg_dma_address(sg);
2227 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2228 lower_32_bits(addr));
2229 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2230 upper_32_bits(addr));
2231 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2232 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2233 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2237 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2241 hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2242 unsigned int num_sg, struct beiscsi_io_task *io_task)
2244 struct iscsi_sge *psgl;
2245 unsigned int sg_len, index;
2246 unsigned int sge_len = 0;
2247 unsigned long long addr;
2248 struct scatterlist *l_sg;
2249 unsigned int offset;
2251 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2252 io_task->bhs_pa.u.a32.address_lo);
2253 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2254 io_task->bhs_pa.u.a32.address_hi);
2257 for (index = 0; (index < num_sg) && (index < 2); index++,
2260 sg_len = sg_dma_len(sg);
2261 addr = (u64) sg_dma_address(sg);
2262 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
2263 ((u32)(addr & 0xFFFFFFFF)));
2264 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
2265 ((u32)(addr >> 32)));
2266 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2270 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
2272 sg_len = sg_dma_len(sg);
2273 addr = (u64) sg_dma_address(sg);
2274 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
2275 ((u32)(addr & 0xFFFFFFFF)));
2276 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
2277 ((u32)(addr >> 32)));
2278 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
2282 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2283 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2285 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2287 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2288 io_task->bhs_pa.u.a32.address_hi);
2289 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2290 io_task->bhs_pa.u.a32.address_lo);
2293 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2295 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2297 } else if (num_sg == 2) {
2298 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2300 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2303 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2305 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2312 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
2313 sg_len = sg_dma_len(sg);
2314 addr = (u64) sg_dma_address(sg);
2315 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2316 (addr & 0xFFFFFFFF));
2317 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2319 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2320 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2321 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2325 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2329 * hwi_write_buffer()- Populate the WRB with task info
2330 * @pwrb: ptr to the WRB entry
2331 * @task: iscsi task which is to be executed
2333 static int hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
2335 struct iscsi_sge *psgl;
2336 struct beiscsi_io_task *io_task = task->dd_data;
2337 struct beiscsi_conn *beiscsi_conn = io_task->conn;
2338 struct beiscsi_hba *phba = beiscsi_conn->phba;
2339 uint8_t dsp_value = 0;
2341 io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
2342 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2343 io_task->bhs_pa.u.a32.address_lo);
2344 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2345 io_task->bhs_pa.u.a32.address_hi);
2349 /* Check for the data_count */
2350 dsp_value = (task->data_count) ? 1 : 0;
2352 if (is_chip_be2_be3r(phba))
2353 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp,
2356 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp,
2359 /* Map addr only if there is data_count */
2361 io_task->mtask_addr = pci_map_single(phba->pcidev,
2365 if (pci_dma_mapping_error(phba->pcidev,
2366 io_task->mtask_addr))
2368 io_task->mtask_data_count = task->data_count;
2370 io_task->mtask_addr = 0;
2372 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
2373 lower_32_bits(io_task->mtask_addr));
2374 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
2375 upper_32_bits(io_task->mtask_addr));
2376 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2379 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
2381 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
2382 io_task->mtask_addr = 0;
2385 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2387 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
2389 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2390 io_task->bhs_pa.u.a32.address_hi);
2391 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2392 io_task->bhs_pa.u.a32.address_lo);
2395 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
2396 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
2397 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
2398 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
2399 AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
2400 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2404 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2405 lower_32_bits(io_task->mtask_addr));
2406 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2407 upper_32_bits(io_task->mtask_addr));
2409 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
2411 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2416 * beiscsi_find_mem_req()- Find mem needed
2417 * @phba: ptr to HBA struct
2419 static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
2421 uint8_t mem_descr_index, ulp_num;
2422 unsigned int num_cq_pages, num_async_pdu_buf_pages;
2423 unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
2424 unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
2426 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
2427 sizeof(struct sol_cqe));
2429 phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
2431 phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
2432 BE_ISCSI_PDU_HEADER_SIZE;
2433 phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
2434 sizeof(struct hwi_context_memory);
2437 phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
2438 * (phba->params.wrbs_per_cxn)
2439 * phba->params.cxns_per_ctrl;
2440 wrb_sz_per_cxn = sizeof(struct wrb_handle) *
2441 (phba->params.wrbs_per_cxn);
2442 phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
2443 phba->params.cxns_per_ctrl);
2445 phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
2446 phba->params.icds_per_ctrl;
2447 phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
2448 phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
2449 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
2450 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
2452 num_async_pdu_buf_sgl_pages =
2453 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2455 sizeof(struct phys_addr));
2457 num_async_pdu_buf_pages =
2458 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2460 phba->params.defpdu_hdr_sz);
2462 num_async_pdu_data_pages =
2463 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2465 phba->params.defpdu_data_sz);
2467 num_async_pdu_data_sgl_pages =
2468 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2470 sizeof(struct phys_addr));
2472 mem_descr_index = (HWI_MEM_TEMPLATE_HDR_ULP0 +
2473 (ulp_num * MEM_DESCR_OFFSET));
2474 phba->mem_req[mem_descr_index] =
2475 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
2476 BEISCSI_TEMPLATE_HDR_PER_CXN_SIZE;
2478 mem_descr_index = (HWI_MEM_ASYNC_HEADER_BUF_ULP0 +
2479 (ulp_num * MEM_DESCR_OFFSET));
2480 phba->mem_req[mem_descr_index] =
2481 num_async_pdu_buf_pages *
2484 mem_descr_index = (HWI_MEM_ASYNC_DATA_BUF_ULP0 +
2485 (ulp_num * MEM_DESCR_OFFSET));
2486 phba->mem_req[mem_descr_index] =
2487 num_async_pdu_data_pages *
2490 mem_descr_index = (HWI_MEM_ASYNC_HEADER_RING_ULP0 +
2491 (ulp_num * MEM_DESCR_OFFSET));
2492 phba->mem_req[mem_descr_index] =
2493 num_async_pdu_buf_sgl_pages *
2496 mem_descr_index = (HWI_MEM_ASYNC_DATA_RING_ULP0 +
2497 (ulp_num * MEM_DESCR_OFFSET));
2498 phba->mem_req[mem_descr_index] =
2499 num_async_pdu_data_sgl_pages *
2502 mem_descr_index = (HWI_MEM_ASYNC_HEADER_HANDLE_ULP0 +
2503 (ulp_num * MEM_DESCR_OFFSET));
2504 phba->mem_req[mem_descr_index] =
2505 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
2506 sizeof(struct async_pdu_handle);
2508 mem_descr_index = (HWI_MEM_ASYNC_DATA_HANDLE_ULP0 +
2509 (ulp_num * MEM_DESCR_OFFSET));
2510 phba->mem_req[mem_descr_index] =
2511 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
2512 sizeof(struct async_pdu_handle);
2514 mem_descr_index = (HWI_MEM_ASYNC_PDU_CONTEXT_ULP0 +
2515 (ulp_num * MEM_DESCR_OFFSET));
2516 phba->mem_req[mem_descr_index] =
2517 sizeof(struct hwi_async_pdu_context) +
2518 (BEISCSI_GET_CID_COUNT(phba, ulp_num) *
2519 sizeof(struct hwi_async_entry));
2524 static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
2527 struct hwi_controller *phwi_ctrlr;
2528 struct be_mem_descriptor *mem_descr;
2529 struct mem_array *mem_arr, *mem_arr_orig;
2530 unsigned int i, j, alloc_size, curr_alloc_size;
2532 phba->phwi_ctrlr = kzalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
2533 if (!phba->phwi_ctrlr)
2536 /* Allocate memory for wrb_context */
2537 phwi_ctrlr = phba->phwi_ctrlr;
2538 phwi_ctrlr->wrb_context = kzalloc(sizeof(struct hwi_wrb_context) *
2539 phba->params.cxns_per_ctrl,
2541 if (!phwi_ctrlr->wrb_context) {
2542 kfree(phba->phwi_ctrlr);
2546 phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
2548 if (!phba->init_mem) {
2549 kfree(phwi_ctrlr->wrb_context);
2550 kfree(phba->phwi_ctrlr);
2554 mem_arr_orig = kmalloc(sizeof(*mem_arr_orig) * BEISCSI_MAX_FRAGS_INIT,
2556 if (!mem_arr_orig) {
2557 kfree(phba->init_mem);
2558 kfree(phwi_ctrlr->wrb_context);
2559 kfree(phba->phwi_ctrlr);
2563 mem_descr = phba->init_mem;
2564 for (i = 0; i < SE_MEM_MAX; i++) {
2565 if (!phba->mem_req[i]) {
2566 mem_descr->mem_array = NULL;
2572 mem_arr = mem_arr_orig;
2573 alloc_size = phba->mem_req[i];
2574 memset(mem_arr, 0, sizeof(struct mem_array) *
2575 BEISCSI_MAX_FRAGS_INIT);
2576 curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
2578 mem_arr->virtual_address = pci_alloc_consistent(
2582 if (!mem_arr->virtual_address) {
2583 if (curr_alloc_size <= BE_MIN_MEM_SIZE)
2585 if (curr_alloc_size -
2586 rounddown_pow_of_two(curr_alloc_size))
2587 curr_alloc_size = rounddown_pow_of_two
2590 curr_alloc_size = curr_alloc_size / 2;
2592 mem_arr->bus_address.u.
2593 a64.address = (__u64) bus_add;
2594 mem_arr->size = curr_alloc_size;
2595 alloc_size -= curr_alloc_size;
2596 curr_alloc_size = min(be_max_phys_size *
2601 } while (alloc_size);
2602 mem_descr->num_elements = j;
2603 mem_descr->size_in_bytes = phba->mem_req[i];
2604 mem_descr->mem_array = kmalloc(sizeof(*mem_arr) * j,
2606 if (!mem_descr->mem_array)
2609 memcpy(mem_descr->mem_array, mem_arr_orig,
2610 sizeof(struct mem_array) * j);
2613 kfree(mem_arr_orig);
2616 mem_descr->num_elements = j;
2617 while ((i) || (j)) {
2618 for (j = mem_descr->num_elements; j > 0; j--) {
2619 pci_free_consistent(phba->pcidev,
2620 mem_descr->mem_array[j - 1].size,
2621 mem_descr->mem_array[j - 1].
2623 (unsigned long)mem_descr->
2625 bus_address.u.a64.address);
2629 kfree(mem_descr->mem_array);
2633 kfree(mem_arr_orig);
2634 kfree(phba->init_mem);
2635 kfree(phba->phwi_ctrlr->wrb_context);
2636 kfree(phba->phwi_ctrlr);
2640 static int beiscsi_get_memory(struct beiscsi_hba *phba)
2642 beiscsi_find_mem_req(phba);
2643 return beiscsi_alloc_mem(phba);
2646 static void iscsi_init_global_templates(struct beiscsi_hba *phba)
2648 struct pdu_data_out *pdata_out;
2649 struct pdu_nop_out *pnop_out;
2650 struct be_mem_descriptor *mem_descr;
2652 mem_descr = phba->init_mem;
2653 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
2655 (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
2656 memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2658 AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
2662 (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
2663 virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
2665 memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2666 AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
2667 AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
2668 AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
2671 static int beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
2673 struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
2674 struct hwi_context_memory *phwi_ctxt;
2675 struct wrb_handle *pwrb_handle = NULL;
2676 struct hwi_controller *phwi_ctrlr;
2677 struct hwi_wrb_context *pwrb_context;
2678 struct iscsi_wrb *pwrb = NULL;
2679 unsigned int num_cxn_wrbh = 0;
2680 unsigned int num_cxn_wrb = 0, j, idx = 0, index;
2682 mem_descr_wrbh = phba->init_mem;
2683 mem_descr_wrbh += HWI_MEM_WRBH;
2685 mem_descr_wrb = phba->init_mem;
2686 mem_descr_wrb += HWI_MEM_WRB;
2687 phwi_ctrlr = phba->phwi_ctrlr;
2689 /* Allocate memory for WRBQ */
2690 phwi_ctxt = phwi_ctrlr->phwi_ctxt;
2691 phwi_ctxt->be_wrbq = kzalloc(sizeof(struct be_queue_info) *
2692 phba->params.cxns_per_ctrl,
2694 if (!phwi_ctxt->be_wrbq) {
2695 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2696 "BM_%d : WRBQ Mem Alloc Failed\n");
2700 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
2701 pwrb_context = &phwi_ctrlr->wrb_context[index];
2702 pwrb_context->pwrb_handle_base =
2703 kzalloc(sizeof(struct wrb_handle *) *
2704 phba->params.wrbs_per_cxn, GFP_KERNEL);
2705 if (!pwrb_context->pwrb_handle_base) {
2706 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2707 "BM_%d : Mem Alloc Failed. Failing to load\n");
2708 goto init_wrb_hndl_failed;
2710 pwrb_context->pwrb_handle_basestd =
2711 kzalloc(sizeof(struct wrb_handle *) *
2712 phba->params.wrbs_per_cxn, GFP_KERNEL);
2713 if (!pwrb_context->pwrb_handle_basestd) {
2714 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2715 "BM_%d : Mem Alloc Failed. Failing to load\n");
2716 goto init_wrb_hndl_failed;
2718 if (!num_cxn_wrbh) {
2720 mem_descr_wrbh->mem_array[idx].virtual_address;
2721 num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
2722 ((sizeof(struct wrb_handle)) *
2723 phba->params.wrbs_per_cxn));
2726 pwrb_context->alloc_index = 0;
2727 pwrb_context->wrb_handles_available = 0;
2728 pwrb_context->free_index = 0;
2731 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2732 pwrb_context->pwrb_handle_base[j] = pwrb_handle;
2733 pwrb_context->pwrb_handle_basestd[j] =
2735 pwrb_context->wrb_handles_available++;
2736 pwrb_handle->wrb_index = j;
2741 spin_lock_init(&pwrb_context->wrb_lock);
2744 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
2745 pwrb_context = &phwi_ctrlr->wrb_context[index];
2747 pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
2748 num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
2749 ((sizeof(struct iscsi_wrb) *
2750 phba->params.wrbs_per_cxn));
2755 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2756 pwrb_handle = pwrb_context->pwrb_handle_base[j];
2757 pwrb_handle->pwrb = pwrb;
2764 init_wrb_hndl_failed:
2765 for (j = index; j > 0; j--) {
2766 pwrb_context = &phwi_ctrlr->wrb_context[j];
2767 kfree(pwrb_context->pwrb_handle_base);
2768 kfree(pwrb_context->pwrb_handle_basestd);
2773 static int hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
2776 struct hwi_controller *phwi_ctrlr;
2777 struct hba_parameters *p = &phba->params;
2778 struct hwi_async_pdu_context *pasync_ctx;
2779 struct async_pdu_handle *pasync_header_h, *pasync_data_h;
2780 unsigned int index, idx, num_per_mem, num_async_data;
2781 struct be_mem_descriptor *mem_descr;
2783 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
2784 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
2786 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2787 mem_descr += (HWI_MEM_ASYNC_PDU_CONTEXT_ULP0 +
2788 (ulp_num * MEM_DESCR_OFFSET));
2790 phwi_ctrlr = phba->phwi_ctrlr;
2791 phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num] =
2792 (struct hwi_async_pdu_context *)
2793 mem_descr->mem_array[0].virtual_address;
2795 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num];
2796 memset(pasync_ctx, 0, sizeof(*pasync_ctx));
2798 pasync_ctx->async_entry =
2799 (struct hwi_async_entry *)
2800 ((long unsigned int)pasync_ctx +
2801 sizeof(struct hwi_async_pdu_context));
2803 pasync_ctx->num_entries = BEISCSI_GET_CID_COUNT(phba,
2805 pasync_ctx->buffer_size = p->defpdu_hdr_sz;
2807 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2808 mem_descr += HWI_MEM_ASYNC_HEADER_BUF_ULP0 +
2809 (ulp_num * MEM_DESCR_OFFSET);
2810 if (mem_descr->mem_array[0].virtual_address) {
2811 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2812 "BM_%d : hwi_init_async_pdu_ctx"
2813 " HWI_MEM_ASYNC_HEADER_BUF_ULP%d va=%p\n",
2815 mem_descr->mem_array[0].
2818 beiscsi_log(phba, KERN_WARNING,
2820 "BM_%d : No Virtual address for ULP : %d\n",
2823 pasync_ctx->async_header.va_base =
2824 mem_descr->mem_array[0].virtual_address;
2826 pasync_ctx->async_header.pa_base.u.a64.address =
2827 mem_descr->mem_array[0].
2828 bus_address.u.a64.address;
2830 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2831 mem_descr += HWI_MEM_ASYNC_HEADER_RING_ULP0 +
2832 (ulp_num * MEM_DESCR_OFFSET);
2833 if (mem_descr->mem_array[0].virtual_address) {
2834 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2835 "BM_%d : hwi_init_async_pdu_ctx"
2836 " HWI_MEM_ASYNC_HEADER_RING_ULP%d va=%p\n",
2838 mem_descr->mem_array[0].
2841 beiscsi_log(phba, KERN_WARNING,
2843 "BM_%d : No Virtual address for ULP : %d\n",
2846 pasync_ctx->async_header.ring_base =
2847 mem_descr->mem_array[0].virtual_address;
2849 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2850 mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE_ULP0 +
2851 (ulp_num * MEM_DESCR_OFFSET);
2852 if (mem_descr->mem_array[0].virtual_address) {
2853 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2854 "BM_%d : hwi_init_async_pdu_ctx"
2855 " HWI_MEM_ASYNC_HEADER_HANDLE_ULP%d va=%p\n",
2857 mem_descr->mem_array[0].
2860 beiscsi_log(phba, KERN_WARNING,
2862 "BM_%d : No Virtual address for ULP : %d\n",
2865 pasync_ctx->async_header.handle_base =
2866 mem_descr->mem_array[0].virtual_address;
2867 pasync_ctx->async_header.writables = 0;
2868 INIT_LIST_HEAD(&pasync_ctx->async_header.free_list);
2870 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2871 mem_descr += HWI_MEM_ASYNC_DATA_RING_ULP0 +
2872 (ulp_num * MEM_DESCR_OFFSET);
2873 if (mem_descr->mem_array[0].virtual_address) {
2874 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2875 "BM_%d : hwi_init_async_pdu_ctx"
2876 " HWI_MEM_ASYNC_DATA_RING_ULP%d va=%p\n",
2878 mem_descr->mem_array[0].
2881 beiscsi_log(phba, KERN_WARNING,
2883 "BM_%d : No Virtual address for ULP : %d\n",
2886 pasync_ctx->async_data.ring_base =
2887 mem_descr->mem_array[0].virtual_address;
2889 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2890 mem_descr += HWI_MEM_ASYNC_DATA_HANDLE_ULP0 +
2891 (ulp_num * MEM_DESCR_OFFSET);
2892 if (!mem_descr->mem_array[0].virtual_address)
2893 beiscsi_log(phba, KERN_WARNING,
2895 "BM_%d : No Virtual address for ULP : %d\n",
2898 pasync_ctx->async_data.handle_base =
2899 mem_descr->mem_array[0].virtual_address;
2900 pasync_ctx->async_data.writables = 0;
2901 INIT_LIST_HEAD(&pasync_ctx->async_data.free_list);
2904 (struct async_pdu_handle *)
2905 pasync_ctx->async_header.handle_base;
2907 (struct async_pdu_handle *)
2908 pasync_ctx->async_data.handle_base;
2910 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2911 mem_descr += HWI_MEM_ASYNC_DATA_BUF_ULP0 +
2912 (ulp_num * MEM_DESCR_OFFSET);
2913 if (mem_descr->mem_array[0].virtual_address) {
2914 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2915 "BM_%d : hwi_init_async_pdu_ctx"
2916 " HWI_MEM_ASYNC_DATA_BUF_ULP%d va=%p\n",
2918 mem_descr->mem_array[0].
2921 beiscsi_log(phba, KERN_WARNING,
2923 "BM_%d : No Virtual address for ULP : %d\n",
2927 pasync_ctx->async_data.va_base =
2928 mem_descr->mem_array[idx].virtual_address;
2929 pasync_ctx->async_data.pa_base.u.a64.address =
2930 mem_descr->mem_array[idx].
2931 bus_address.u.a64.address;
2933 num_async_data = ((mem_descr->mem_array[idx].size) /
2934 phba->params.defpdu_data_sz);
2937 for (index = 0; index < BEISCSI_GET_CID_COUNT
2938 (phba, ulp_num); index++) {
2939 pasync_header_h->cri = -1;
2940 pasync_header_h->index = (char)index;
2941 INIT_LIST_HEAD(&pasync_header_h->link);
2942 pasync_header_h->pbuffer =
2943 (void *)((unsigned long)
2945 async_header.va_base) +
2946 (p->defpdu_hdr_sz * index));
2948 pasync_header_h->pa.u.a64.address =
2949 pasync_ctx->async_header.pa_base.u.a64.
2950 address + (p->defpdu_hdr_sz * index);
2952 list_add_tail(&pasync_header_h->link,
2953 &pasync_ctx->async_header.
2956 pasync_ctx->async_header.free_entries++;
2957 pasync_ctx->async_header.writables++;
2959 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
2961 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
2963 pasync_data_h->cri = -1;
2964 pasync_data_h->index = (char)index;
2965 INIT_LIST_HEAD(&pasync_data_h->link);
2967 if (!num_async_data) {
2970 pasync_ctx->async_data.va_base =
2971 mem_descr->mem_array[idx].
2973 pasync_ctx->async_data.pa_base.u.
2975 mem_descr->mem_array[idx].
2976 bus_address.u.a64.address;
2978 ((mem_descr->mem_array[idx].
2980 phba->params.defpdu_data_sz);
2982 pasync_data_h->pbuffer =
2983 (void *)((unsigned long)
2984 (pasync_ctx->async_data.va_base) +
2985 (p->defpdu_data_sz * num_per_mem));
2987 pasync_data_h->pa.u.a64.address =
2988 pasync_ctx->async_data.pa_base.u.a64.
2989 address + (p->defpdu_data_sz *
2994 list_add_tail(&pasync_data_h->link,
2995 &pasync_ctx->async_data.
2998 pasync_ctx->async_data.free_entries++;
2999 pasync_ctx->async_data.writables++;
3001 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
3005 pasync_ctx->async_header.host_write_ptr = 0;
3006 pasync_ctx->async_header.ep_read_ptr = -1;
3007 pasync_ctx->async_data.host_write_ptr = 0;
3008 pasync_ctx->async_data.ep_read_ptr = -1;
3016 be_sgl_create_contiguous(void *virtual_address,
3017 u64 physical_address, u32 length,
3018 struct be_dma_mem *sgl)
3020 WARN_ON(!virtual_address);
3021 WARN_ON(!physical_address);
3025 sgl->va = virtual_address;
3026 sgl->dma = (unsigned long)physical_address;
3032 static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
3034 memset(sgl, 0, sizeof(*sgl));
3038 hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
3039 struct mem_array *pmem, struct be_dma_mem *sgl)
3042 be_sgl_destroy_contiguous(sgl);
3044 be_sgl_create_contiguous(pmem->virtual_address,
3045 pmem->bus_address.u.a64.address,
3050 hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
3051 struct mem_array *pmem, struct be_dma_mem *sgl)
3054 be_sgl_destroy_contiguous(sgl);
3056 be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
3057 pmem->bus_address.u.a64.address,
3061 static int be_fill_queue(struct be_queue_info *q,
3062 u16 len, u16 entry_size, void *vaddress)
3064 struct be_dma_mem *mem = &q->dma_mem;
3066 memset(q, 0, sizeof(*q));
3068 q->entry_size = entry_size;
3069 mem->size = len * entry_size;
3073 memset(mem->va, 0, mem->size);
3077 static int beiscsi_create_eqs(struct beiscsi_hba *phba,
3078 struct hwi_context_memory *phwi_context)
3080 unsigned int i, num_eq_pages;
3081 int ret = 0, eq_for_mcc;
3082 struct be_queue_info *eq;
3083 struct be_dma_mem *mem;
3087 num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries * \
3088 sizeof(struct be_eq_entry));
3090 if (phba->msix_enabled)
3094 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
3095 eq = &phwi_context->be_eq[i].q;
3097 phwi_context->be_eq[i].phba = phba;
3098 eq_vaddress = pci_alloc_consistent(phba->pcidev,
3099 num_eq_pages * PAGE_SIZE,
3102 goto create_eq_error;
3104 mem->va = eq_vaddress;
3105 ret = be_fill_queue(eq, phba->params.num_eq_entries,
3106 sizeof(struct be_eq_entry), eq_vaddress);
3108 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3109 "BM_%d : be_fill_queue Failed for EQ\n");
3110 goto create_eq_error;
3114 ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
3115 phwi_context->cur_eqd);
3117 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3118 "BM_%d : beiscsi_cmd_eq_create"
3120 goto create_eq_error;
3123 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3124 "BM_%d : eqid = %d\n",
3125 phwi_context->be_eq[i].q.id);
3129 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
3130 eq = &phwi_context->be_eq[i].q;
3133 pci_free_consistent(phba->pcidev, num_eq_pages
3140 static int beiscsi_create_cqs(struct beiscsi_hba *phba,
3141 struct hwi_context_memory *phwi_context)
3143 unsigned int i, num_cq_pages;
3145 struct be_queue_info *cq, *eq;
3146 struct be_dma_mem *mem;
3147 struct be_eq_obj *pbe_eq;
3151 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
3152 sizeof(struct sol_cqe));
3154 for (i = 0; i < phba->num_cpus; i++) {
3155 cq = &phwi_context->be_cq[i];
3156 eq = &phwi_context->be_eq[i].q;
3157 pbe_eq = &phwi_context->be_eq[i];
3159 pbe_eq->phba = phba;
3161 cq_vaddress = pci_alloc_consistent(phba->pcidev,
3162 num_cq_pages * PAGE_SIZE,
3165 goto create_cq_error;
3166 ret = be_fill_queue(cq, phba->params.num_cq_entries,
3167 sizeof(struct sol_cqe), cq_vaddress);
3169 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3170 "BM_%d : be_fill_queue Failed "
3172 goto create_cq_error;
3176 ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
3179 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3180 "BM_%d : beiscsi_cmd_eq_create"
3181 "Failed for ISCSI CQ\n");
3182 goto create_cq_error;
3184 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3185 "BM_%d : iscsi cq_id is %d for eq_id %d\n"
3186 "iSCSI CQ CREATED\n", cq->id, eq->id);
3191 for (i = 0; i < phba->num_cpus; i++) {
3192 cq = &phwi_context->be_cq[i];
3195 pci_free_consistent(phba->pcidev, num_cq_pages
3204 beiscsi_create_def_hdr(struct beiscsi_hba *phba,
3205 struct hwi_context_memory *phwi_context,
3206 struct hwi_controller *phwi_ctrlr,
3207 unsigned int def_pdu_ring_sz, uint8_t ulp_num)
3211 struct be_queue_info *dq, *cq;
3212 struct be_dma_mem *mem;
3213 struct be_mem_descriptor *mem_descr;
3217 dq = &phwi_context->be_def_hdrq[ulp_num];
3218 cq = &phwi_context->be_cq[0];
3220 mem_descr = phba->init_mem;
3221 mem_descr += HWI_MEM_ASYNC_HEADER_RING_ULP0 +
3222 (ulp_num * MEM_DESCR_OFFSET);
3223 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3224 ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
3225 sizeof(struct phys_addr),
3226 sizeof(struct phys_addr), dq_vaddress);
3228 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3229 "BM_%d : be_fill_queue Failed for DEF PDU HDR on ULP : %d\n",
3234 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3235 bus_address.u.a64.address;
3236 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
3238 phba->params.defpdu_hdr_sz,
3239 BEISCSI_DEFQ_HDR, ulp_num);
3241 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3242 "BM_%d : be_cmd_create_default_pdu_queue Failed DEFHDR on ULP : %d\n",
3248 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3249 "BM_%d : iscsi hdr def pdu id for ULP : %d is %d\n",
3251 phwi_context->be_def_hdrq[ulp_num].id);
3252 hwi_post_async_buffers(phba, BEISCSI_DEFQ_HDR, ulp_num);
3257 beiscsi_create_def_data(struct beiscsi_hba *phba,
3258 struct hwi_context_memory *phwi_context,
3259 struct hwi_controller *phwi_ctrlr,
3260 unsigned int def_pdu_ring_sz, uint8_t ulp_num)
3264 struct be_queue_info *dataq, *cq;
3265 struct be_dma_mem *mem;
3266 struct be_mem_descriptor *mem_descr;
3270 dataq = &phwi_context->be_def_dataq[ulp_num];
3271 cq = &phwi_context->be_cq[0];
3272 mem = &dataq->dma_mem;
3273 mem_descr = phba->init_mem;
3274 mem_descr += HWI_MEM_ASYNC_DATA_RING_ULP0 +
3275 (ulp_num * MEM_DESCR_OFFSET);
3276 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3277 ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
3278 sizeof(struct phys_addr),
3279 sizeof(struct phys_addr), dq_vaddress);
3281 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3282 "BM_%d : be_fill_queue Failed for DEF PDU "
3283 "DATA on ULP : %d\n",
3288 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3289 bus_address.u.a64.address;
3290 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
3292 phba->params.defpdu_data_sz,
3293 BEISCSI_DEFQ_DATA, ulp_num);
3295 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3296 "BM_%d be_cmd_create_default_pdu_queue"
3297 " Failed for DEF PDU DATA on ULP : %d\n",
3302 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3303 "BM_%d : iscsi def data id on ULP : %d is %d\n",
3305 phwi_context->be_def_dataq[ulp_num].id);
3307 hwi_post_async_buffers(phba, BEISCSI_DEFQ_DATA, ulp_num);
3308 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3309 "BM_%d : DEFAULT PDU DATA RING CREATED"
3310 "on ULP : %d\n", ulp_num);
3317 beiscsi_post_template_hdr(struct beiscsi_hba *phba)
3319 struct be_mem_descriptor *mem_descr;
3320 struct mem_array *pm_arr;
3321 struct be_dma_mem sgl;
3322 int status, ulp_num;
3324 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3325 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3326 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
3327 mem_descr += HWI_MEM_TEMPLATE_HDR_ULP0 +
3328 (ulp_num * MEM_DESCR_OFFSET);
3329 pm_arr = mem_descr->mem_array;
3331 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
3332 status = be_cmd_iscsi_post_template_hdr(
3336 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3337 "BM_%d : Post Template HDR Failed for"
3338 "ULP_%d\n", ulp_num);
3342 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3343 "BM_%d : Template HDR Pages Posted for"
3344 "ULP_%d\n", ulp_num);
3351 beiscsi_post_pages(struct beiscsi_hba *phba)
3353 struct be_mem_descriptor *mem_descr;
3354 struct mem_array *pm_arr;
3355 unsigned int page_offset, i;
3356 struct be_dma_mem sgl;
3357 int status, ulp_num = 0;
3359 mem_descr = phba->init_mem;
3360 mem_descr += HWI_MEM_SGE;
3361 pm_arr = mem_descr->mem_array;
3363 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3364 if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
3367 page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
3368 phba->fw_config.iscsi_icd_start[ulp_num]) / PAGE_SIZE;
3369 for (i = 0; i < mem_descr->num_elements; i++) {
3370 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
3371 status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
3373 (pm_arr->size / PAGE_SIZE));
3374 page_offset += pm_arr->size / PAGE_SIZE;
3376 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3377 "BM_%d : post sgl failed.\n");
3382 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3383 "BM_%d : POSTED PAGES\n");
3387 static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
3389 struct be_dma_mem *mem = &q->dma_mem;
3391 pci_free_consistent(phba->pcidev, mem->size,
3397 static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
3398 u16 len, u16 entry_size)
3400 struct be_dma_mem *mem = &q->dma_mem;
3402 memset(q, 0, sizeof(*q));
3404 q->entry_size = entry_size;
3405 mem->size = len * entry_size;
3406 mem->va = pci_zalloc_consistent(phba->pcidev, mem->size, &mem->dma);
3413 beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
3414 struct hwi_context_memory *phwi_context,
3415 struct hwi_controller *phwi_ctrlr)
3417 unsigned int wrb_mem_index, offset, size, num_wrb_rings;
3419 unsigned int idx, num, i, ulp_num;
3420 struct mem_array *pwrb_arr;
3422 struct be_dma_mem sgl;
3423 struct be_mem_descriptor *mem_descr;
3424 struct hwi_wrb_context *pwrb_context;
3426 uint8_t ulp_count = 0, ulp_base_num = 0;
3427 uint16_t cid_count_ulp[BEISCSI_ULP_COUNT] = { 0 };
3430 mem_descr = phba->init_mem;
3431 mem_descr += HWI_MEM_WRB;
3432 pwrb_arr = kmalloc(sizeof(*pwrb_arr) * phba->params.cxns_per_ctrl,
3435 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3436 "BM_%d : Memory alloc failed in create wrb ring.\n");
3439 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3440 pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
3441 num_wrb_rings = mem_descr->mem_array[idx].size /
3442 (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
3444 for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
3445 if (num_wrb_rings) {
3446 pwrb_arr[num].virtual_address = wrb_vaddr;
3447 pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
3448 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3449 sizeof(struct iscsi_wrb);
3450 wrb_vaddr += pwrb_arr[num].size;
3451 pa_addr_lo += pwrb_arr[num].size;
3455 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3456 pa_addr_lo = mem_descr->mem_array[idx].\
3457 bus_address.u.a64.address;
3458 num_wrb_rings = mem_descr->mem_array[idx].size /
3459 (phba->params.wrbs_per_cxn *
3460 sizeof(struct iscsi_wrb));
3461 pwrb_arr[num].virtual_address = wrb_vaddr;
3462 pwrb_arr[num].bus_address.u.a64.address\
3464 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3465 sizeof(struct iscsi_wrb);
3466 wrb_vaddr += pwrb_arr[num].size;
3467 pa_addr_lo += pwrb_arr[num].size;
3472 /* Get the ULP Count */
3473 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3474 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3476 ulp_base_num = ulp_num;
3477 cid_count_ulp[ulp_num] =
3478 BEISCSI_GET_CID_COUNT(phba, ulp_num);
3481 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3486 if (ulp_count > 1) {
3487 ulp_base_num = (ulp_base_num + 1) % BEISCSI_ULP_COUNT;
3489 if (!cid_count_ulp[ulp_base_num])
3490 ulp_base_num = (ulp_base_num + 1) %
3493 cid_count_ulp[ulp_base_num]--;
3497 hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
3498 status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
3499 &phwi_context->be_wrbq[i],
3500 &phwi_ctrlr->wrb_context[i],
3503 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3504 "BM_%d : wrbq create failed.");
3508 pwrb_context = &phwi_ctrlr->wrb_context[i];
3509 BE_SET_CID_TO_CRI(i, pwrb_context->cid);
3515 static void free_wrb_handles(struct beiscsi_hba *phba)
3518 struct hwi_controller *phwi_ctrlr;
3519 struct hwi_wrb_context *pwrb_context;
3521 phwi_ctrlr = phba->phwi_ctrlr;
3522 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
3523 pwrb_context = &phwi_ctrlr->wrb_context[index];
3524 kfree(pwrb_context->pwrb_handle_base);
3525 kfree(pwrb_context->pwrb_handle_basestd);
3529 static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
3531 struct be_queue_info *q;
3532 struct be_ctrl_info *ctrl = &phba->ctrl;
3534 q = &phba->ctrl.mcc_obj.q;
3536 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
3537 be_queue_free(phba, q);
3540 q = &phba->ctrl.mcc_obj.cq;
3542 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3543 be_queue_free(phba, q);
3547 static void hwi_cleanup(struct beiscsi_hba *phba)
3549 struct be_queue_info *q;
3550 struct be_ctrl_info *ctrl = &phba->ctrl;
3551 struct hwi_controller *phwi_ctrlr;
3552 struct hwi_context_memory *phwi_context;
3553 struct hwi_async_pdu_context *pasync_ctx;
3554 int i, eq_for_mcc, ulp_num;
3556 phwi_ctrlr = phba->phwi_ctrlr;
3557 phwi_context = phwi_ctrlr->phwi_ctxt;
3559 be_cmd_iscsi_remove_template_hdr(ctrl);
3561 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3562 q = &phwi_context->be_wrbq[i];
3564 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
3566 kfree(phwi_context->be_wrbq);
3567 free_wrb_handles(phba);
3569 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3570 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3572 q = &phwi_context->be_def_hdrq[ulp_num];
3574 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3576 q = &phwi_context->be_def_dataq[ulp_num];
3578 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3580 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num];
3584 beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
3586 for (i = 0; i < (phba->num_cpus); i++) {
3587 q = &phwi_context->be_cq[i];
3589 be_queue_free(phba, q);
3590 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3594 be_mcc_queues_destroy(phba);
3595 if (phba->msix_enabled)
3599 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
3600 q = &phwi_context->be_eq[i].q;
3602 be_queue_free(phba, q);
3603 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
3606 be_cmd_fw_uninit(ctrl);
3609 static int be_mcc_queues_create(struct beiscsi_hba *phba,
3610 struct hwi_context_memory *phwi_context)
3612 struct be_queue_info *q, *cq;
3613 struct be_ctrl_info *ctrl = &phba->ctrl;
3615 /* Alloc MCC compl queue */
3616 cq = &phba->ctrl.mcc_obj.cq;
3617 if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
3618 sizeof(struct be_mcc_compl)))
3620 /* Ask BE to create MCC compl queue; */
3621 if (phba->msix_enabled) {
3622 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq
3623 [phba->num_cpus].q, false, true, 0))
3626 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
3631 /* Alloc MCC queue */
3632 q = &phba->ctrl.mcc_obj.q;
3633 if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
3634 goto mcc_cq_destroy;
3636 /* Ask BE to create MCC queue */
3637 if (beiscsi_cmd_mccq_create(phba, q, cq))
3643 be_queue_free(phba, q);
3645 beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
3647 be_queue_free(phba, cq);
3653 * find_num_cpus()- Get the CPU online count
3654 * @phba: ptr to priv structure
3656 * CPU count is used for creating EQ.
3658 static void find_num_cpus(struct beiscsi_hba *phba)
3662 num_cpus = num_online_cpus();
3664 switch (phba->generation) {
3667 phba->num_cpus = (num_cpus > BEISCSI_MAX_NUM_CPUS) ?
3668 BEISCSI_MAX_NUM_CPUS : num_cpus;
3672 * If eqid_count == 1 fall back to
3675 if (phba->fw_config.eqid_count == 1) {
3682 (num_cpus > (phba->fw_config.eqid_count - 1)) ?
3683 (phba->fw_config.eqid_count - 1) : num_cpus;
3690 static int hwi_init_port(struct beiscsi_hba *phba)
3692 struct hwi_controller *phwi_ctrlr;
3693 struct hwi_context_memory *phwi_context;
3694 unsigned int def_pdu_ring_sz;
3695 struct be_ctrl_info *ctrl = &phba->ctrl;
3696 int status, ulp_num;
3698 phwi_ctrlr = phba->phwi_ctrlr;
3699 phwi_context = phwi_ctrlr->phwi_ctxt;
3700 phwi_context->max_eqd = 128;
3701 phwi_context->min_eqd = 0;
3702 phwi_context->cur_eqd = 0;
3703 be_cmd_fw_initialize(&phba->ctrl);
3704 /* set optic state to unknown */
3705 phba->optic_state = 0xff;
3707 status = beiscsi_create_eqs(phba, phwi_context);
3709 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3710 "BM_%d : EQ not created\n");
3714 status = be_mcc_queues_create(phba, phwi_context);
3718 status = mgmt_check_supported_fw(ctrl, phba);
3720 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3721 "BM_%d : Unsupported fw version\n");
3725 status = beiscsi_create_cqs(phba, phwi_context);
3727 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3728 "BM_%d : CQ not created\n");
3732 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3733 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3736 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
3737 sizeof(struct phys_addr);
3739 status = beiscsi_create_def_hdr(phba, phwi_context,
3744 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3745 "BM_%d : Default Header not created for ULP : %d\n",
3750 status = beiscsi_create_def_data(phba, phwi_context,
3755 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3756 "BM_%d : Default Data not created for ULP : %d\n",
3763 status = beiscsi_post_pages(phba);
3765 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3766 "BM_%d : Post SGL Pages Failed\n");
3770 status = beiscsi_post_template_hdr(phba);
3772 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3773 "BM_%d : Template HDR Posting for CXN Failed\n");
3776 status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
3778 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3779 "BM_%d : WRB Rings not created\n");
3783 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3784 uint16_t async_arr_idx = 0;
3786 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3788 struct hwi_async_pdu_context *pasync_ctx;
3790 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(
3791 phwi_ctrlr, ulp_num);
3793 phba->params.cxns_per_ctrl; cri++) {
3794 if (ulp_num == BEISCSI_GET_ULP_FROM_CRI
3796 pasync_ctx->cid_to_async_cri_map[
3797 phwi_ctrlr->wrb_context[cri].cid] =
3803 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3804 "BM_%d : hwi_init_port success\n");
3808 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3809 "BM_%d : hwi_init_port failed");
3814 static int hwi_init_controller(struct beiscsi_hba *phba)
3816 struct hwi_controller *phwi_ctrlr;
3818 phwi_ctrlr = phba->phwi_ctrlr;
3819 if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
3820 phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
3821 init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
3822 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3823 "BM_%d : phwi_ctrlr->phwi_ctxt=%p\n",
3824 phwi_ctrlr->phwi_ctxt);
3826 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3827 "BM_%d : HWI_MEM_ADDN_CONTEXT is more "
3828 "than one element.Failing to load\n");
3832 iscsi_init_global_templates(phba);
3833 if (beiscsi_init_wrb_handle(phba))
3836 if (hwi_init_async_pdu_ctx(phba)) {
3837 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3838 "BM_%d : hwi_init_async_pdu_ctx failed\n");
3842 if (hwi_init_port(phba) != 0) {
3843 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3844 "BM_%d : hwi_init_controller failed\n");
3851 static void beiscsi_free_mem(struct beiscsi_hba *phba)
3853 struct be_mem_descriptor *mem_descr;
3856 mem_descr = phba->init_mem;
3859 for (i = 0; i < SE_MEM_MAX; i++) {
3860 for (j = mem_descr->num_elements; j > 0; j--) {
3861 pci_free_consistent(phba->pcidev,
3862 mem_descr->mem_array[j - 1].size,
3863 mem_descr->mem_array[j - 1].virtual_address,
3864 (unsigned long)mem_descr->mem_array[j - 1].
3865 bus_address.u.a64.address);
3868 kfree(mem_descr->mem_array);
3871 kfree(phba->init_mem);
3872 kfree(phba->phwi_ctrlr->wrb_context);
3873 kfree(phba->phwi_ctrlr);
3876 static int beiscsi_init_controller(struct beiscsi_hba *phba)
3880 ret = beiscsi_get_memory(phba);
3882 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3883 "BM_%d : beiscsi_dev_probe -"
3884 "Failed in beiscsi_alloc_memory\n");
3888 ret = hwi_init_controller(phba);
3891 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3892 "BM_%d : Return success from beiscsi_init_controller");
3897 beiscsi_free_mem(phba);
3901 static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
3903 struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
3904 struct sgl_handle *psgl_handle;
3905 struct iscsi_sge *pfrag;
3906 unsigned int arr_index, i, idx;
3907 unsigned int ulp_icd_start, ulp_num = 0;
3909 phba->io_sgl_hndl_avbl = 0;
3910 phba->eh_sgl_hndl_avbl = 0;
3912 mem_descr_sglh = phba->init_mem;
3913 mem_descr_sglh += HWI_MEM_SGLH;
3914 if (1 == mem_descr_sglh->num_elements) {
3915 phba->io_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3916 phba->params.ios_per_ctrl,
3918 if (!phba->io_sgl_hndl_base) {
3919 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3920 "BM_%d : Mem Alloc Failed. Failing to load\n");
3923 phba->eh_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3924 (phba->params.icds_per_ctrl -
3925 phba->params.ios_per_ctrl),
3927 if (!phba->eh_sgl_hndl_base) {
3928 kfree(phba->io_sgl_hndl_base);
3929 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3930 "BM_%d : Mem Alloc Failed. Failing to load\n");
3934 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3935 "BM_%d : HWI_MEM_SGLH is more than one element."
3936 "Failing to load\n");
3942 while (idx < mem_descr_sglh->num_elements) {
3943 psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
3945 for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
3946 sizeof(struct sgl_handle)); i++) {
3947 if (arr_index < phba->params.ios_per_ctrl) {
3948 phba->io_sgl_hndl_base[arr_index] = psgl_handle;
3949 phba->io_sgl_hndl_avbl++;
3952 phba->eh_sgl_hndl_base[arr_index -
3953 phba->params.ios_per_ctrl] =
3956 phba->eh_sgl_hndl_avbl++;
3962 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3963 "BM_%d : phba->io_sgl_hndl_avbl=%d"
3964 "phba->eh_sgl_hndl_avbl=%d\n",
3965 phba->io_sgl_hndl_avbl,
3966 phba->eh_sgl_hndl_avbl);
3968 mem_descr_sg = phba->init_mem;
3969 mem_descr_sg += HWI_MEM_SGE;
3970 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3971 "\n BM_%d : mem_descr_sg->num_elements=%d\n",
3972 mem_descr_sg->num_elements);
3974 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3975 if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
3978 ulp_icd_start = phba->fw_config.iscsi_icd_start[ulp_num];
3982 while (idx < mem_descr_sg->num_elements) {
3983 pfrag = mem_descr_sg->mem_array[idx].virtual_address;
3986 i < (mem_descr_sg->mem_array[idx].size) /
3987 (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
3989 if (arr_index < phba->params.ios_per_ctrl)
3990 psgl_handle = phba->io_sgl_hndl_base[arr_index];
3992 psgl_handle = phba->eh_sgl_hndl_base[arr_index -
3993 phba->params.ios_per_ctrl];
3994 psgl_handle->pfrag = pfrag;
3995 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
3996 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
3997 pfrag += phba->params.num_sge_per_io;
3998 psgl_handle->sgl_index = ulp_icd_start + arr_index++;
4002 phba->io_sgl_free_index = 0;
4003 phba->io_sgl_alloc_index = 0;
4004 phba->eh_sgl_free_index = 0;
4005 phba->eh_sgl_alloc_index = 0;
4009 static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
4012 uint16_t i, ulp_num;
4013 struct ulp_cid_info *ptr_cid_info = NULL;
4015 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4016 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4017 ptr_cid_info = kzalloc(sizeof(struct ulp_cid_info),
4020 if (!ptr_cid_info) {
4021 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4022 "BM_%d : Failed to allocate memory"
4023 "for ULP_CID_INFO for ULP : %d\n",
4030 /* Allocate memory for CID array */
4031 ptr_cid_info->cid_array = kzalloc(sizeof(void *) *
4032 BEISCSI_GET_CID_COUNT(phba,
4033 ulp_num), GFP_KERNEL);
4034 if (!ptr_cid_info->cid_array) {
4035 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4036 "BM_%d : Failed to allocate memory"
4037 "for CID_ARRAY for ULP : %d\n",
4039 kfree(ptr_cid_info);
4040 ptr_cid_info = NULL;
4045 ptr_cid_info->avlbl_cids = BEISCSI_GET_CID_COUNT(
4048 /* Save the cid_info_array ptr */
4049 phba->cid_array_info[ulp_num] = ptr_cid_info;
4052 phba->ep_array = kzalloc(sizeof(struct iscsi_endpoint *) *
4053 phba->params.cxns_per_ctrl, GFP_KERNEL);
4054 if (!phba->ep_array) {
4055 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4056 "BM_%d : Failed to allocate memory in "
4057 "hba_setup_cid_tbls\n");
4063 phba->conn_table = kzalloc(sizeof(struct beiscsi_conn *) *
4064 phba->params.cxns_per_ctrl, GFP_KERNEL);
4065 if (!phba->conn_table) {
4066 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4067 "BM_%d : Failed to allocate memory in"
4068 "hba_setup_cid_tbls\n");
4070 kfree(phba->ep_array);
4071 phba->ep_array = NULL;
4077 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
4078 ulp_num = phba->phwi_ctrlr->wrb_context[i].ulp_num;
4080 ptr_cid_info = phba->cid_array_info[ulp_num];
4081 ptr_cid_info->cid_array[ptr_cid_info->cid_alloc++] =
4082 phba->phwi_ctrlr->wrb_context[i].cid;
4086 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4087 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4088 ptr_cid_info = phba->cid_array_info[ulp_num];
4090 ptr_cid_info->cid_alloc = 0;
4091 ptr_cid_info->cid_free = 0;
4097 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4098 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4099 ptr_cid_info = phba->cid_array_info[ulp_num];
4102 kfree(ptr_cid_info->cid_array);
4103 kfree(ptr_cid_info);
4104 phba->cid_array_info[ulp_num] = NULL;
4112 static void hwi_enable_intr(struct beiscsi_hba *phba)
4114 struct be_ctrl_info *ctrl = &phba->ctrl;
4115 struct hwi_controller *phwi_ctrlr;
4116 struct hwi_context_memory *phwi_context;
4117 struct be_queue_info *eq;
4122 phwi_ctrlr = phba->phwi_ctrlr;
4123 phwi_context = phwi_ctrlr->phwi_ctxt;
4125 addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
4126 PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
4127 reg = ioread32(addr);
4129 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4131 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4132 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4133 "BM_%d : reg =x%08x addr=%p\n", reg, addr);
4134 iowrite32(reg, addr);
4137 if (!phba->msix_enabled) {
4138 eq = &phwi_context->be_eq[0].q;
4139 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4140 "BM_%d : eq->id=%d\n", eq->id);
4142 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
4144 for (i = 0; i <= phba->num_cpus; i++) {
4145 eq = &phwi_context->be_eq[i].q;
4146 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4147 "BM_%d : eq->id=%d\n", eq->id);
4148 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
4153 static void hwi_disable_intr(struct beiscsi_hba *phba)
4155 struct be_ctrl_info *ctrl = &phba->ctrl;
4157 u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
4158 u32 reg = ioread32(addr);
4160 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4162 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4163 iowrite32(reg, addr);
4165 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
4166 "BM_%d : In hwi_disable_intr, Already Disabled\n");
4169 static int beiscsi_init_port(struct beiscsi_hba *phba)
4173 ret = beiscsi_init_controller(phba);
4175 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4176 "BM_%d : beiscsi_dev_probe - Failed in"
4177 "beiscsi_init_controller\n");
4180 ret = beiscsi_init_sgl_handle(phba);
4182 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4183 "BM_%d : beiscsi_dev_probe - Failed in"
4184 "beiscsi_init_sgl_handle\n");
4185 goto do_cleanup_ctrlr;
4188 if (hba_setup_cid_tbls(phba)) {
4189 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4190 "BM_%d : Failed in hba_setup_cid_tbls\n");
4191 kfree(phba->io_sgl_hndl_base);
4192 kfree(phba->eh_sgl_hndl_base);
4193 goto do_cleanup_ctrlr;
4203 static void hwi_purge_eq(struct beiscsi_hba *phba)
4205 struct hwi_controller *phwi_ctrlr;
4206 struct hwi_context_memory *phwi_context;
4207 struct be_queue_info *eq;
4208 struct be_eq_entry *eqe = NULL;
4210 unsigned int num_processed;
4212 phwi_ctrlr = phba->phwi_ctrlr;
4213 phwi_context = phwi_ctrlr->phwi_ctxt;
4214 if (phba->msix_enabled)
4219 for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
4220 eq = &phwi_context->be_eq[i].q;
4221 eqe = queue_tail_node(eq);
4223 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
4225 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
4227 eqe = queue_tail_node(eq);
4232 hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
4236 static void beiscsi_clean_port(struct beiscsi_hba *phba)
4238 int mgmt_status, ulp_num;
4239 struct ulp_cid_info *ptr_cid_info = NULL;
4241 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4242 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4243 mgmt_status = mgmt_epfw_cleanup(phba, ulp_num);
4245 beiscsi_log(phba, KERN_WARNING,
4247 "BM_%d : mgmt_epfw_cleanup FAILED"
4248 " for ULP_%d\n", ulp_num);
4254 kfree(phba->io_sgl_hndl_base);
4255 kfree(phba->eh_sgl_hndl_base);
4256 kfree(phba->ep_array);
4257 kfree(phba->conn_table);
4259 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4260 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4261 ptr_cid_info = phba->cid_array_info[ulp_num];
4264 kfree(ptr_cid_info->cid_array);
4265 kfree(ptr_cid_info);
4266 phba->cid_array_info[ulp_num] = NULL;
4274 * beiscsi_free_mgmt_task_handles()- Free driver CXN resources
4275 * @beiscsi_conn: ptr to the conn to be cleaned up
4276 * @task: ptr to iscsi_task resource to be freed.
4278 * Free driver mgmt resources binded to CXN.
4281 beiscsi_free_mgmt_task_handles(struct beiscsi_conn *beiscsi_conn,
4282 struct iscsi_task *task)
4284 struct beiscsi_io_task *io_task;
4285 struct beiscsi_hba *phba = beiscsi_conn->phba;
4286 struct hwi_wrb_context *pwrb_context;
4287 struct hwi_controller *phwi_ctrlr;
4288 uint16_t cri_index = BE_GET_CRI_FROM_CID(
4289 beiscsi_conn->beiscsi_conn_cid);
4291 phwi_ctrlr = phba->phwi_ctrlr;
4292 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
4294 io_task = task->dd_data;
4296 if (io_task->pwrb_handle) {
4297 memset(io_task->pwrb_handle->pwrb, 0,
4298 sizeof(struct iscsi_wrb));
4299 free_wrb_handle(phba, pwrb_context,
4300 io_task->pwrb_handle);
4301 io_task->pwrb_handle = NULL;
4304 if (io_task->psgl_handle) {
4305 free_mgmt_sgl_handle(phba,
4306 io_task->psgl_handle);
4307 io_task->psgl_handle = NULL;
4310 if (io_task->mtask_addr) {
4311 pci_unmap_single(phba->pcidev,
4312 io_task->mtask_addr,
4313 io_task->mtask_data_count,
4315 io_task->mtask_addr = 0;
4320 * beiscsi_cleanup_task()- Free driver resources of the task
4321 * @task: ptr to the iscsi task
4324 static void beiscsi_cleanup_task(struct iscsi_task *task)
4326 struct beiscsi_io_task *io_task = task->dd_data;
4327 struct iscsi_conn *conn = task->conn;
4328 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4329 struct beiscsi_hba *phba = beiscsi_conn->phba;
4330 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4331 struct hwi_wrb_context *pwrb_context;
4332 struct hwi_controller *phwi_ctrlr;
4333 uint16_t cri_index = BE_GET_CRI_FROM_CID(
4334 beiscsi_conn->beiscsi_conn_cid);
4336 phwi_ctrlr = phba->phwi_ctrlr;
4337 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
4339 if (io_task->cmd_bhs) {
4340 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4341 io_task->bhs_pa.u.a64.address);
4342 io_task->cmd_bhs = NULL;
4346 if (io_task->pwrb_handle) {
4347 free_wrb_handle(phba, pwrb_context,
4348 io_task->pwrb_handle);
4349 io_task->pwrb_handle = NULL;
4352 if (io_task->psgl_handle) {
4353 free_io_sgl_handle(phba, io_task->psgl_handle);
4354 io_task->psgl_handle = NULL;
4357 if (io_task->scsi_cmnd) {
4358 if (io_task->num_sg)
4359 scsi_dma_unmap(io_task->scsi_cmnd);
4360 io_task->scsi_cmnd = NULL;
4363 if (!beiscsi_conn->login_in_progress)
4364 beiscsi_free_mgmt_task_handles(beiscsi_conn, task);
4369 beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
4370 struct beiscsi_offload_params *params)
4372 struct wrb_handle *pwrb_handle;
4373 struct hwi_wrb_context *pwrb_context = NULL;
4374 struct beiscsi_hba *phba = beiscsi_conn->phba;
4375 struct iscsi_task *task = beiscsi_conn->task;
4376 struct iscsi_session *session = task->conn->session;
4380 * We can always use 0 here because it is reserved by libiscsi for
4381 * login/startup related tasks.
4383 beiscsi_conn->login_in_progress = 0;
4384 spin_lock_bh(&session->back_lock);
4385 beiscsi_cleanup_task(task);
4386 spin_unlock_bh(&session->back_lock);
4388 pwrb_handle = alloc_wrb_handle(phba, beiscsi_conn->beiscsi_conn_cid,
4391 /* Check for the adapter family */
4392 if (is_chip_be2_be3r(phba))
4393 beiscsi_offload_cxn_v0(params, pwrb_handle,
4397 beiscsi_offload_cxn_v2(params, pwrb_handle,
4400 be_dws_le_to_cpu(pwrb_handle->pwrb,
4401 sizeof(struct iscsi_target_context_update_wrb));
4403 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
4404 doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
4405 << DB_DEF_PDU_WRB_INDEX_SHIFT;
4406 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4407 iowrite32(doorbell, phba->db_va +
4408 beiscsi_conn->doorbell_offset);
4411 * There is no completion for CONTEXT_UPDATE. The completion of next
4412 * WRB posted guarantees FW's processing and DMA'ing of it.
4413 * Use beiscsi_put_wrb_handle to put it back in the pool which makes
4414 * sure zero'ing or reuse of the WRB only after wrbs_per_cxn.
4416 beiscsi_put_wrb_handle(pwrb_context, pwrb_handle,
4417 phba->params.wrbs_per_cxn);
4418 beiscsi_log(phba, KERN_INFO,
4419 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4420 "BM_%d : put CONTEXT_UPDATE pwrb_handle=%p free_index=0x%x wrb_handles_available=%d\n",
4421 pwrb_handle, pwrb_context->free_index,
4422 pwrb_context->wrb_handles_available);
4425 static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
4426 int *index, int *age)
4430 *age = conn->session->age;
4434 * beiscsi_alloc_pdu - allocates pdu and related resources
4435 * @task: libiscsi task
4436 * @opcode: opcode of pdu for task
4438 * This is called with the session lock held. It will allocate
4439 * the wrb and sgl if needed for the command. And it will prep
4440 * the pdu's itt. beiscsi_parse_pdu will later translate
4441 * the pdu itt to the libiscsi task itt.
4443 static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
4445 struct beiscsi_io_task *io_task = task->dd_data;
4446 struct iscsi_conn *conn = task->conn;
4447 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4448 struct beiscsi_hba *phba = beiscsi_conn->phba;
4449 struct hwi_wrb_context *pwrb_context;
4450 struct hwi_controller *phwi_ctrlr;
4452 uint16_t cri_index = 0;
4453 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4456 io_task->cmd_bhs = pci_pool_alloc(beiscsi_sess->bhs_pool,
4457 GFP_ATOMIC, &paddr);
4458 if (!io_task->cmd_bhs)
4460 io_task->bhs_pa.u.a64.address = paddr;
4461 io_task->libiscsi_itt = (itt_t)task->itt;
4462 io_task->conn = beiscsi_conn;
4464 task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
4465 task->hdr_max = sizeof(struct be_cmd_bhs);
4466 io_task->psgl_handle = NULL;
4467 io_task->pwrb_handle = NULL;
4470 io_task->psgl_handle = alloc_io_sgl_handle(phba);
4471 if (!io_task->psgl_handle) {
4472 beiscsi_log(phba, KERN_ERR,
4473 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4474 "BM_%d : Alloc of IO_SGL_ICD Failed"
4475 "for the CID : %d\n",
4476 beiscsi_conn->beiscsi_conn_cid);
4479 io_task->pwrb_handle = alloc_wrb_handle(phba,
4480 beiscsi_conn->beiscsi_conn_cid,
4481 &io_task->pwrb_context);
4482 if (!io_task->pwrb_handle) {
4483 beiscsi_log(phba, KERN_ERR,
4484 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4485 "BM_%d : Alloc of WRB_HANDLE Failed"
4486 "for the CID : %d\n",
4487 beiscsi_conn->beiscsi_conn_cid);
4491 io_task->scsi_cmnd = NULL;
4492 if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
4493 beiscsi_conn->task = task;
4494 if (!beiscsi_conn->login_in_progress) {
4495 io_task->psgl_handle = (struct sgl_handle *)
4496 alloc_mgmt_sgl_handle(phba);
4497 if (!io_task->psgl_handle) {
4498 beiscsi_log(phba, KERN_ERR,
4501 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4502 "for the CID : %d\n",
4508 beiscsi_conn->login_in_progress = 1;
4509 beiscsi_conn->plogin_sgl_handle =
4510 io_task->psgl_handle;
4511 io_task->pwrb_handle =
4512 alloc_wrb_handle(phba,
4513 beiscsi_conn->beiscsi_conn_cid,
4514 &io_task->pwrb_context);
4515 if (!io_task->pwrb_handle) {
4516 beiscsi_log(phba, KERN_ERR,
4519 "BM_%d : Alloc of WRB_HANDLE Failed"
4520 "for the CID : %d\n",
4523 goto free_mgmt_hndls;
4525 beiscsi_conn->plogin_wrb_handle =
4526 io_task->pwrb_handle;
4529 io_task->psgl_handle =
4530 beiscsi_conn->plogin_sgl_handle;
4531 io_task->pwrb_handle =
4532 beiscsi_conn->plogin_wrb_handle;
4535 io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
4536 if (!io_task->psgl_handle) {
4537 beiscsi_log(phba, KERN_ERR,
4540 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4541 "for the CID : %d\n",
4546 io_task->pwrb_handle =
4547 alloc_wrb_handle(phba,
4548 beiscsi_conn->beiscsi_conn_cid,
4549 &io_task->pwrb_context);
4550 if (!io_task->pwrb_handle) {
4551 beiscsi_log(phba, KERN_ERR,
4552 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4553 "BM_%d : Alloc of WRB_HANDLE Failed"
4554 "for the CID : %d\n",
4555 beiscsi_conn->beiscsi_conn_cid);
4556 goto free_mgmt_hndls;
4561 itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
4562 wrb_index << 16) | (unsigned int)
4563 (io_task->psgl_handle->sgl_index));
4564 io_task->pwrb_handle->pio_handle = task;
4566 io_task->cmd_bhs->iscsi_hdr.itt = itt;
4570 free_io_sgl_handle(phba, io_task->psgl_handle);
4573 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
4574 io_task->psgl_handle = NULL;
4576 phwi_ctrlr = phba->phwi_ctrlr;
4577 cri_index = BE_GET_CRI_FROM_CID(
4578 beiscsi_conn->beiscsi_conn_cid);
4579 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
4580 if (io_task->pwrb_handle)
4581 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
4582 io_task->pwrb_handle = NULL;
4583 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4584 io_task->bhs_pa.u.a64.address);
4585 io_task->cmd_bhs = NULL;
4588 int beiscsi_iotask_v2(struct iscsi_task *task, struct scatterlist *sg,
4589 unsigned int num_sg, unsigned int xferlen,
4590 unsigned int writedir)
4593 struct beiscsi_io_task *io_task = task->dd_data;
4594 struct iscsi_conn *conn = task->conn;
4595 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4596 struct beiscsi_hba *phba = beiscsi_conn->phba;
4597 struct iscsi_wrb *pwrb = NULL;
4598 unsigned int doorbell = 0;
4600 pwrb = io_task->pwrb_handle->pwrb;
4602 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4605 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4607 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 1);
4609 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4611 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 0);
4614 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb_v2,
4617 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, lun, pwrb,
4618 cpu_to_be16(*(unsigned short *)
4619 &io_task->cmd_bhs->iscsi_hdr.lun));
4620 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb, xferlen);
4621 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4622 io_task->pwrb_handle->wrb_index);
4623 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4624 be32_to_cpu(task->cmdsn));
4625 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4626 io_task->psgl_handle->sgl_index);
4628 hwi_write_sgl_v2(pwrb, sg, num_sg, io_task);
4629 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
4630 io_task->pwrb_handle->wrb_index);
4631 if (io_task->pwrb_context->plast_wrb)
4632 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb,
4633 io_task->pwrb_context->plast_wrb,
4634 io_task->pwrb_handle->wrb_index);
4635 io_task->pwrb_context->plast_wrb = pwrb;
4637 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4639 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
4640 doorbell |= (io_task->pwrb_handle->wrb_index &
4641 DB_DEF_PDU_WRB_INDEX_MASK) <<
4642 DB_DEF_PDU_WRB_INDEX_SHIFT;
4643 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4644 iowrite32(doorbell, phba->db_va +
4645 beiscsi_conn->doorbell_offset);
4649 static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
4650 unsigned int num_sg, unsigned int xferlen,
4651 unsigned int writedir)
4654 struct beiscsi_io_task *io_task = task->dd_data;
4655 struct iscsi_conn *conn = task->conn;
4656 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4657 struct beiscsi_hba *phba = beiscsi_conn->phba;
4658 struct iscsi_wrb *pwrb = NULL;
4659 unsigned int doorbell = 0;
4661 pwrb = io_task->pwrb_handle->pwrb;
4662 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4665 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4667 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
4669 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4671 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
4674 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb,
4677 AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
4678 cpu_to_be16(*(unsigned short *)
4679 &io_task->cmd_bhs->iscsi_hdr.lun));
4680 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
4681 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4682 io_task->pwrb_handle->wrb_index);
4683 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4684 be32_to_cpu(task->cmdsn));
4685 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4686 io_task->psgl_handle->sgl_index);
4688 hwi_write_sgl(pwrb, sg, num_sg, io_task);
4690 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
4691 io_task->pwrb_handle->wrb_index);
4692 if (io_task->pwrb_context->plast_wrb)
4693 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb,
4694 io_task->pwrb_context->plast_wrb,
4695 io_task->pwrb_handle->wrb_index);
4696 io_task->pwrb_context->plast_wrb = pwrb;
4698 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4700 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
4701 doorbell |= (io_task->pwrb_handle->wrb_index &
4702 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4703 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4705 iowrite32(doorbell, phba->db_va +
4706 beiscsi_conn->doorbell_offset);
4710 static int beiscsi_mtask(struct iscsi_task *task)
4712 struct beiscsi_io_task *io_task = task->dd_data;
4713 struct iscsi_conn *conn = task->conn;
4714 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4715 struct beiscsi_hba *phba = beiscsi_conn->phba;
4716 struct iscsi_wrb *pwrb = NULL;
4717 unsigned int doorbell = 0;
4719 unsigned int pwrb_typeoffset = 0;
4722 cid = beiscsi_conn->beiscsi_conn_cid;
4723 pwrb = io_task->pwrb_handle->pwrb;
4724 memset(pwrb, 0, sizeof(*pwrb));
4726 if (is_chip_be2_be3r(phba)) {
4727 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4728 be32_to_cpu(task->cmdsn));
4729 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4730 io_task->pwrb_handle->wrb_index);
4731 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4732 io_task->psgl_handle->sgl_index);
4733 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
4735 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
4736 io_task->pwrb_handle->wrb_index);
4737 if (io_task->pwrb_context->plast_wrb)
4738 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb,
4739 io_task->pwrb_context->plast_wrb,
4740 io_task->pwrb_handle->wrb_index);
4741 io_task->pwrb_context->plast_wrb = pwrb;
4743 pwrb_typeoffset = BE_WRB_TYPE_OFFSET;
4745 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4746 be32_to_cpu(task->cmdsn));
4747 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4748 io_task->pwrb_handle->wrb_index);
4749 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4750 io_task->psgl_handle->sgl_index);
4751 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb,
4753 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
4754 io_task->pwrb_handle->wrb_index);
4755 if (io_task->pwrb_context->plast_wrb)
4756 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb,
4757 io_task->pwrb_context->plast_wrb,
4758 io_task->pwrb_handle->wrb_index);
4759 io_task->pwrb_context->plast_wrb = pwrb;
4761 pwrb_typeoffset = SKH_WRB_TYPE_OFFSET;
4765 switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
4766 case ISCSI_OP_LOGIN:
4767 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
4768 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
4769 ret = hwi_write_buffer(pwrb, task);
4771 case ISCSI_OP_NOOP_OUT:
4772 if (task->hdr->ttt != ISCSI_RESERVED_TAG) {
4773 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
4774 if (is_chip_be2_be3r(phba))
4775 AMAP_SET_BITS(struct amap_iscsi_wrb,
4778 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
4781 ADAPTER_SET_WRB_TYPE(pwrb, INI_RD_CMD, pwrb_typeoffset);
4782 if (is_chip_be2_be3r(phba))
4783 AMAP_SET_BITS(struct amap_iscsi_wrb,
4786 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
4789 ret = hwi_write_buffer(pwrb, task);
4792 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
4793 ret = hwi_write_buffer(pwrb, task);
4795 case ISCSI_OP_SCSI_TMFUNC:
4796 ADAPTER_SET_WRB_TYPE(pwrb, INI_TMF_CMD, pwrb_typeoffset);
4797 ret = hwi_write_buffer(pwrb, task);
4799 case ISCSI_OP_LOGOUT:
4800 ADAPTER_SET_WRB_TYPE(pwrb, HWH_TYPE_LOGOUT, pwrb_typeoffset);
4801 ret = hwi_write_buffer(pwrb, task);
4805 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4806 "BM_%d : opcode =%d Not supported\n",
4807 task->hdr->opcode & ISCSI_OPCODE_MASK);
4815 /* Set the task type */
4816 io_task->wrb_type = (is_chip_be2_be3r(phba)) ?
4817 AMAP_GET_BITS(struct amap_iscsi_wrb, type, pwrb) :
4818 AMAP_GET_BITS(struct amap_iscsi_wrb_v2, type, pwrb);
4820 doorbell |= cid & DB_WRB_POST_CID_MASK;
4821 doorbell |= (io_task->pwrb_handle->wrb_index &
4822 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4823 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4824 iowrite32(doorbell, phba->db_va +
4825 beiscsi_conn->doorbell_offset);
4829 static int beiscsi_task_xmit(struct iscsi_task *task)
4831 struct beiscsi_io_task *io_task = task->dd_data;
4832 struct scsi_cmnd *sc = task->sc;
4833 struct beiscsi_hba *phba;
4834 struct scatterlist *sg;
4836 unsigned int writedir = 0, xferlen = 0;
4838 phba = io_task->conn->phba;
4840 * HBA in error includes BEISCSI_HBA_FW_TIMEOUT. IO path might be
4841 * operational if FW still gets heartbeat from EP FW. Is management
4842 * path really needed to continue further?
4844 if (beiscsi_hba_in_error(phba))
4847 if (!io_task->conn->login_in_progress)
4848 task->hdr->exp_statsn = 0;
4851 return beiscsi_mtask(task);
4853 io_task->scsi_cmnd = sc;
4854 io_task->num_sg = 0;
4855 num_sg = scsi_dma_map(sc);
4857 beiscsi_log(phba, KERN_ERR,
4858 BEISCSI_LOG_IO | BEISCSI_LOG_ISCSI,
4859 "BM_%d : scsi_dma_map Failed "
4860 "Driver_ITT : 0x%x ITT : 0x%x Xferlen : 0x%x\n",
4861 be32_to_cpu(io_task->cmd_bhs->iscsi_hdr.itt),
4862 io_task->libiscsi_itt, scsi_bufflen(sc));
4867 * For scsi cmd task, check num_sg before unmapping in cleanup_task.
4868 * For management task, cleanup_task checks mtask_addr before unmapping.
4870 io_task->num_sg = num_sg;
4871 xferlen = scsi_bufflen(sc);
4872 sg = scsi_sglist(sc);
4873 if (sc->sc_data_direction == DMA_TO_DEVICE)
4878 return phba->iotask_fn(task, sg, num_sg, xferlen, writedir);
4882 * beiscsi_bsg_request - handle bsg request from ISCSI transport
4883 * @job: job to handle
4885 static int beiscsi_bsg_request(struct bsg_job *job)
4887 struct Scsi_Host *shost;
4888 struct beiscsi_hba *phba;
4889 struct iscsi_bsg_request *bsg_req = job->request;
4892 struct be_dma_mem nonemb_cmd;
4893 struct be_cmd_resp_hdr *resp;
4894 struct iscsi_bsg_reply *bsg_reply = job->reply;
4895 unsigned short status, extd_status;
4897 shost = iscsi_job_to_shost(job);
4898 phba = iscsi_host_priv(shost);
4900 if (beiscsi_hba_in_error(phba)) {
4901 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
4902 "BM_%d : HBA in error 0x%lx\n", phba->state);
4906 switch (bsg_req->msgcode) {
4907 case ISCSI_BSG_HST_VENDOR:
4908 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
4909 job->request_payload.payload_len,
4911 if (nonemb_cmd.va == NULL) {
4912 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4913 "BM_%d : Failed to allocate memory for "
4914 "beiscsi_bsg_request\n");
4917 tag = mgmt_vendor_specific_fw_cmd(&phba->ctrl, phba, job,
4920 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4921 "BM_%d : MBX Tag Allocation Failed\n");
4923 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4924 nonemb_cmd.va, nonemb_cmd.dma);
4928 rc = wait_event_interruptible_timeout(
4929 phba->ctrl.mcc_wait[tag],
4930 phba->ctrl.mcc_tag_status[tag],
4932 BEISCSI_HOST_MBX_TIMEOUT));
4933 extd_status = (phba->ctrl.mcc_tag_status[tag] &
4934 CQE_STATUS_ADDL_MASK) >> CQE_STATUS_ADDL_SHIFT;
4935 status = phba->ctrl.mcc_tag_status[tag] & CQE_STATUS_MASK;
4936 free_mcc_wrb(&phba->ctrl, tag);
4937 resp = (struct be_cmd_resp_hdr *)nonemb_cmd.va;
4938 sg_copy_from_buffer(job->reply_payload.sg_list,
4939 job->reply_payload.sg_cnt,
4940 nonemb_cmd.va, (resp->response_length
4942 bsg_reply->reply_payload_rcv_len = resp->response_length;
4943 bsg_reply->result = status;
4944 bsg_job_done(job, bsg_reply->result,
4945 bsg_reply->reply_payload_rcv_len);
4946 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4947 nonemb_cmd.va, nonemb_cmd.dma);
4948 if (status || extd_status) {
4949 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4950 "BM_%d : MBX Cmd Failed"
4951 " status = %d extd_status = %d\n",
4952 status, extd_status);
4961 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4962 "BM_%d : Unsupported bsg command: 0x%x\n",
4970 void beiscsi_hba_attrs_init(struct beiscsi_hba *phba)
4972 /* Set the logging parameter */
4973 beiscsi_log_enable_init(phba, beiscsi_log_enable);
4977 * beiscsi_quiesce()- Cleanup Driver resources
4978 * @phba: Instance Priv structure
4980 * Free the OS and HW resources held by the driver
4982 static void beiscsi_quiesce(struct beiscsi_hba *phba)
4984 struct hwi_controller *phwi_ctrlr;
4985 struct hwi_context_memory *phwi_context;
4986 struct be_eq_obj *pbe_eq;
4987 unsigned int i, msix_vec;
4989 phwi_ctrlr = phba->phwi_ctrlr;
4990 phwi_context = phwi_ctrlr->phwi_ctxt;
4991 hwi_disable_intr(phba);
4992 if (phba->msix_enabled) {
4993 for (i = 0; i <= phba->num_cpus; i++) {
4994 msix_vec = phba->msix_entries[i].vector;
4995 free_irq(msix_vec, &phwi_context->be_eq[i]);
4996 kfree(phba->msi_name[i]);
4999 if (phba->pcidev->irq)
5000 free_irq(phba->pcidev->irq, phba);
5001 pci_disable_msix(phba->pcidev);
5002 cancel_delayed_work_sync(&phba->eqd_update);
5003 cancel_work_sync(&phba->boot_work);
5004 del_timer_sync(&phba->hw_check);
5006 for (i = 0; i < phba->num_cpus; i++) {
5007 pbe_eq = &phwi_context->be_eq[i];
5008 irq_poll_disable(&pbe_eq->iopoll);
5011 /* PCI_ERR is set then check if driver is not unloading */
5012 if (test_bit(BEISCSI_HBA_RUNNING, &phba->state) &&
5013 test_bit(BEISCSI_HBA_PCI_ERR, &phba->state)) {
5018 destroy_workqueue(phba->wq);
5019 beiscsi_clean_port(phba);
5020 beiscsi_free_mem(phba);
5022 beiscsi_unmap_pci_function(phba);
5023 pci_free_consistent(phba->pcidev,
5024 phba->ctrl.mbox_mem_alloced.size,
5025 phba->ctrl.mbox_mem_alloced.va,
5026 phba->ctrl.mbox_mem_alloced.dma);
5029 static void beiscsi_remove(struct pci_dev *pcidev)
5031 struct beiscsi_hba *phba = NULL;
5033 phba = pci_get_drvdata(pcidev);
5035 dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
5039 clear_bit(BEISCSI_HBA_RUNNING, &phba->state);
5040 beiscsi_iface_destroy_default(phba);
5041 iscsi_host_remove(phba->shost);
5042 beiscsi_quiesce(phba);
5043 /* after cancelling boot_work */
5044 iscsi_boot_destroy_kset(phba->boot_struct.boot_kset);
5045 pci_dev_put(phba->pcidev);
5046 iscsi_host_free(phba->shost);
5047 pci_disable_pcie_error_reporting(pcidev);
5048 pci_set_drvdata(pcidev, NULL);
5049 pci_release_regions(pcidev);
5050 pci_disable_device(pcidev);
5053 static void beiscsi_msix_enable(struct beiscsi_hba *phba)
5057 for (i = 0; i <= phba->num_cpus; i++)
5058 phba->msix_entries[i].entry = i;
5060 status = pci_enable_msix_range(phba->pcidev, phba->msix_entries,
5061 phba->num_cpus + 1, phba->num_cpus + 1);
5063 phba->msix_enabled = true;
5068 void beiscsi_start_boot_work(struct beiscsi_hba *phba, unsigned int s_handle)
5070 if (phba->boot_struct.boot_kset)
5073 /* skip if boot work is already in progress */
5074 if (test_and_set_bit(BEISCSI_HBA_BOOT_WORK, &phba->state))
5077 phba->boot_struct.retry = 3;
5078 phba->boot_struct.tag = 0;
5079 phba->boot_struct.s_handle = s_handle;
5080 phba->boot_struct.action = BEISCSI_BOOT_GET_SHANDLE;
5081 schedule_work(&phba->boot_work);
5084 static ssize_t beiscsi_show_boot_tgt_info(void *data, int type, char *buf)
5086 struct beiscsi_hba *phba = data;
5087 struct mgmt_session_info *boot_sess = &phba->boot_struct.boot_sess;
5088 struct mgmt_conn_info *boot_conn = &boot_sess->conn_list[0];
5093 case ISCSI_BOOT_TGT_NAME:
5094 rc = sprintf(buf, "%.*s\n",
5095 (int)strlen(boot_sess->target_name),
5096 (char *)&boot_sess->target_name);
5098 case ISCSI_BOOT_TGT_IP_ADDR:
5099 if (boot_conn->dest_ipaddr.ip_type == BEISCSI_IP_TYPE_V4)
5100 rc = sprintf(buf, "%pI4\n",
5101 (char *)&boot_conn->dest_ipaddr.addr);
5103 rc = sprintf(str, "%pI6\n",
5104 (char *)&boot_conn->dest_ipaddr.addr);
5106 case ISCSI_BOOT_TGT_PORT:
5107 rc = sprintf(str, "%d\n", boot_conn->dest_port);
5110 case ISCSI_BOOT_TGT_CHAP_NAME:
5111 rc = sprintf(str, "%.*s\n",
5112 boot_conn->negotiated_login_options.auth_data.chap.
5113 target_chap_name_length,
5114 (char *)&boot_conn->negotiated_login_options.
5115 auth_data.chap.target_chap_name);
5117 case ISCSI_BOOT_TGT_CHAP_SECRET:
5118 rc = sprintf(str, "%.*s\n",
5119 boot_conn->negotiated_login_options.auth_data.chap.
5120 target_secret_length,
5121 (char *)&boot_conn->negotiated_login_options.
5122 auth_data.chap.target_secret);
5124 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
5125 rc = sprintf(str, "%.*s\n",
5126 boot_conn->negotiated_login_options.auth_data.chap.
5127 intr_chap_name_length,
5128 (char *)&boot_conn->negotiated_login_options.
5129 auth_data.chap.intr_chap_name);
5131 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
5132 rc = sprintf(str, "%.*s\n",
5133 boot_conn->negotiated_login_options.auth_data.chap.
5135 (char *)&boot_conn->negotiated_login_options.
5136 auth_data.chap.intr_secret);
5138 case ISCSI_BOOT_TGT_FLAGS:
5139 rc = sprintf(str, "2\n");
5141 case ISCSI_BOOT_TGT_NIC_ASSOC:
5142 rc = sprintf(str, "0\n");
5148 static ssize_t beiscsi_show_boot_ini_info(void *data, int type, char *buf)
5150 struct beiscsi_hba *phba = data;
5155 case ISCSI_BOOT_INI_INITIATOR_NAME:
5156 rc = sprintf(str, "%s\n",
5157 phba->boot_struct.boot_sess.initiator_iscsiname);
5163 static ssize_t beiscsi_show_boot_eth_info(void *data, int type, char *buf)
5165 struct beiscsi_hba *phba = data;
5170 case ISCSI_BOOT_ETH_FLAGS:
5171 rc = sprintf(str, "2\n");
5173 case ISCSI_BOOT_ETH_INDEX:
5174 rc = sprintf(str, "0\n");
5176 case ISCSI_BOOT_ETH_MAC:
5177 rc = beiscsi_get_macaddr(str, phba);
5184 static umode_t beiscsi_tgt_get_attr_visibility(void *data, int type)
5189 case ISCSI_BOOT_TGT_NAME:
5190 case ISCSI_BOOT_TGT_IP_ADDR:
5191 case ISCSI_BOOT_TGT_PORT:
5192 case ISCSI_BOOT_TGT_CHAP_NAME:
5193 case ISCSI_BOOT_TGT_CHAP_SECRET:
5194 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
5195 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
5196 case ISCSI_BOOT_TGT_NIC_ASSOC:
5197 case ISCSI_BOOT_TGT_FLAGS:
5204 static umode_t beiscsi_ini_get_attr_visibility(void *data, int type)
5209 case ISCSI_BOOT_INI_INITIATOR_NAME:
5217 static umode_t beiscsi_eth_get_attr_visibility(void *data, int type)
5222 case ISCSI_BOOT_ETH_FLAGS:
5223 case ISCSI_BOOT_ETH_MAC:
5224 case ISCSI_BOOT_ETH_INDEX:
5231 static void beiscsi_boot_kobj_release(void *data)
5233 struct beiscsi_hba *phba = data;
5235 scsi_host_put(phba->shost);
5238 static int beiscsi_boot_create_kset(struct beiscsi_hba *phba)
5240 struct boot_struct *bs = &phba->boot_struct;
5241 struct iscsi_boot_kobj *boot_kobj;
5243 if (bs->boot_kset) {
5244 __beiscsi_log(phba, KERN_ERR,
5245 "BM_%d: boot_kset already created\n");
5249 bs->boot_kset = iscsi_boot_create_host_kset(phba->shost->host_no);
5250 if (!bs->boot_kset) {
5251 __beiscsi_log(phba, KERN_ERR,
5252 "BM_%d: boot_kset alloc failed\n");
5256 /* get shost ref because the show function will refer phba */
5257 if (!scsi_host_get(phba->shost))
5260 boot_kobj = iscsi_boot_create_target(bs->boot_kset, 0, phba,
5261 beiscsi_show_boot_tgt_info,
5262 beiscsi_tgt_get_attr_visibility,
5263 beiscsi_boot_kobj_release);
5267 if (!scsi_host_get(phba->shost))
5270 boot_kobj = iscsi_boot_create_initiator(bs->boot_kset, 0, phba,
5271 beiscsi_show_boot_ini_info,
5272 beiscsi_ini_get_attr_visibility,
5273 beiscsi_boot_kobj_release);
5277 if (!scsi_host_get(phba->shost))
5280 boot_kobj = iscsi_boot_create_ethernet(bs->boot_kset, 0, phba,
5281 beiscsi_show_boot_eth_info,
5282 beiscsi_eth_get_attr_visibility,
5283 beiscsi_boot_kobj_release);
5290 scsi_host_put(phba->shost);
5292 iscsi_boot_destroy_kset(bs->boot_kset);
5293 bs->boot_kset = NULL;
5297 static void beiscsi_boot_work(struct work_struct *work)
5299 struct beiscsi_hba *phba =
5300 container_of(work, struct beiscsi_hba, boot_work);
5301 struct boot_struct *bs = &phba->boot_struct;
5302 unsigned int tag = 0;
5304 if (beiscsi_hba_in_error(phba))
5307 beiscsi_log(phba, KERN_INFO,
5308 BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX,
5309 "BM_%d : %s action %d\n",
5310 __func__, phba->boot_struct.action);
5312 switch (phba->boot_struct.action) {
5313 case BEISCSI_BOOT_REOPEN_SESS:
5314 tag = beiscsi_boot_reopen_sess(phba);
5316 case BEISCSI_BOOT_GET_SHANDLE:
5317 tag = __beiscsi_boot_get_shandle(phba, 1);
5319 case BEISCSI_BOOT_GET_SINFO:
5320 tag = beiscsi_boot_get_sinfo(phba);
5322 case BEISCSI_BOOT_LOGOUT_SESS:
5323 tag = beiscsi_boot_logout_sess(phba);
5325 case BEISCSI_BOOT_CREATE_KSET:
5326 beiscsi_boot_create_kset(phba);
5328 * updated boot_kset is made visible to all before
5329 * ending the boot work.
5332 clear_bit(BEISCSI_HBA_BOOT_WORK, &phba->state);
5337 schedule_work(&phba->boot_work);
5339 clear_bit(BEISCSI_HBA_BOOT_WORK, &phba->state);
5343 static void beiscsi_hw_health_check(unsigned long ptr)
5345 struct beiscsi_hba *phba;
5347 phba = (struct beiscsi_hba *)ptr;
5348 beiscsi_ue_detect(phba);
5349 if (test_bit(BEISCSI_HBA_IN_UE, &phba->state))
5352 mod_timer(&phba->hw_check,
5353 jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
5356 static void beiscsi_eqd_update_work(struct work_struct *work)
5358 struct hwi_context_memory *phwi_context;
5359 struct be_set_eqd set_eqd[MAX_CPUS];
5360 struct hwi_controller *phwi_ctrlr;
5361 struct be_eq_obj *pbe_eq;
5362 struct beiscsi_hba *phba;
5363 unsigned int pps, delta;
5364 struct be_aic_obj *aic;
5365 int eqd, i, num = 0;
5368 phba = container_of(work, struct beiscsi_hba, eqd_update.work);
5369 if (beiscsi_hba_in_error(phba))
5372 phwi_ctrlr = phba->phwi_ctrlr;
5373 phwi_context = phwi_ctrlr->phwi_ctxt;
5375 for (i = 0; i <= phba->num_cpus; i++) {
5376 aic = &phba->aic_obj[i];
5377 pbe_eq = &phwi_context->be_eq[i];
5379 if (!aic->jiffies || time_before(now, aic->jiffies) ||
5380 pbe_eq->cq_count < aic->eq_prev) {
5382 aic->eq_prev = pbe_eq->cq_count;
5385 delta = jiffies_to_msecs(now - aic->jiffies);
5386 pps = (((u32)(pbe_eq->cq_count - aic->eq_prev) * 1000) / delta);
5387 eqd = (pps / 1500) << 2;
5391 eqd = min_t(u32, eqd, phwi_context->max_eqd);
5392 eqd = max_t(u32, eqd, phwi_context->min_eqd);
5395 aic->eq_prev = pbe_eq->cq_count;
5397 if (eqd != aic->prev_eqd) {
5398 set_eqd[num].delay_multiplier = (eqd * 65)/100;
5399 set_eqd[num].eq_id = pbe_eq->q.id;
5400 aic->prev_eqd = eqd;
5405 /* completion of this is ignored */
5406 beiscsi_modify_eq_delay(phba, set_eqd, num);
5408 schedule_delayed_work(&phba->eqd_update,
5409 msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
5413 static pci_ers_result_t beiscsi_eeh_err_detected(struct pci_dev *pdev,
5414 pci_channel_state_t state)
5416 struct beiscsi_hba *phba = NULL;
5418 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
5419 set_bit(BEISCSI_HBA_PCI_ERR, &phba->state);
5421 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5422 "BM_%d : EEH error detected\n");
5424 beiscsi_quiesce(phba);
5426 if (state == pci_channel_io_perm_failure) {
5427 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5428 "BM_%d : EEH : State PERM Failure");
5429 return PCI_ERS_RESULT_DISCONNECT;
5432 pci_disable_device(pdev);
5434 /* The error could cause the FW to trigger a flash debug dump.
5435 * Resetting the card while flash dump is in progress
5436 * can cause it not to recover; wait for it to finish.
5437 * Wait only for first function as it is needed only once per
5440 if (pdev->devfn == 0)
5443 return PCI_ERS_RESULT_NEED_RESET;
5446 static pci_ers_result_t beiscsi_eeh_reset(struct pci_dev *pdev)
5448 struct beiscsi_hba *phba = NULL;
5451 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
5453 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5454 "BM_%d : EEH Reset\n");
5456 status = pci_enable_device(pdev);
5458 return PCI_ERS_RESULT_DISCONNECT;
5460 pci_set_master(pdev);
5461 pci_set_power_state(pdev, PCI_D0);
5462 pci_restore_state(pdev);
5464 /* Wait for the CHIP Reset to complete */
5465 status = be_chk_reset_complete(phba);
5467 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5468 "BM_%d : EEH Reset Completed\n");
5470 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5471 "BM_%d : EEH Reset Completion Failure\n");
5472 return PCI_ERS_RESULT_DISCONNECT;
5475 pci_cleanup_aer_uncorrect_error_status(pdev);
5476 return PCI_ERS_RESULT_RECOVERED;
5479 static void beiscsi_eeh_resume(struct pci_dev *pdev)
5482 struct be_eq_obj *pbe_eq;
5483 struct beiscsi_hba *phba = NULL;
5484 struct hwi_controller *phwi_ctrlr;
5485 struct hwi_context_memory *phwi_context;
5487 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
5488 pci_save_state(pdev);
5491 find_num_cpus(phba);
5496 beiscsi_msix_enable(phba);
5497 if (!phba->msix_enabled)
5501 ret = beiscsi_cmd_reset_function(phba);
5503 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5504 "BM_%d : Reset Failed\n");
5508 ret = be_chk_reset_complete(phba);
5510 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5511 "BM_%d : Failed to get out of reset.\n");
5515 beiscsi_get_params(phba);
5516 phba->shost->max_id = phba->params.cxns_per_ctrl;
5517 phba->shost->can_queue = phba->params.ios_per_ctrl;
5518 ret = hwi_init_controller(phba);
5520 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5521 "BM_%d : beiscsi_eeh_resume -"
5522 "Failed to initialize beiscsi_hba.\n");
5526 for (i = 0; i < MAX_MCC_CMD; i++) {
5527 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
5528 phba->ctrl.mcc_tag[i] = i + 1;
5529 phba->ctrl.mcc_tag_status[i + 1] = 0;
5530 phba->ctrl.mcc_tag_available++;
5533 phwi_ctrlr = phba->phwi_ctrlr;
5534 phwi_context = phwi_ctrlr->phwi_ctxt;
5536 for (i = 0; i < phba->num_cpus; i++) {
5537 pbe_eq = &phwi_context->be_eq[i];
5538 irq_poll_init(&pbe_eq->iopoll, be_iopoll_budget,
5542 i = (phba->msix_enabled) ? i : 0;
5543 /* Work item for MCC handling */
5544 pbe_eq = &phwi_context->be_eq[i];
5545 INIT_WORK(&pbe_eq->mcc_work, beiscsi_mcc_work);
5547 ret = beiscsi_init_irqs(phba);
5549 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5550 "BM_%d : beiscsi_eeh_resume - "
5551 "Failed to beiscsi_init_irqs\n");
5555 hwi_enable_intr(phba);
5556 clear_bit(BEISCSI_HBA_PCI_ERR, &phba->state);
5558 /* start hw_check timer and eqd_update work */
5559 schedule_delayed_work(&phba->eqd_update,
5560 msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
5561 mod_timer(&phba->hw_check,
5562 jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
5565 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5566 "BM_%d : AER EEH Resume Failed\n");
5569 static int beiscsi_dev_probe(struct pci_dev *pcidev,
5570 const struct pci_device_id *id)
5572 struct beiscsi_hba *phba = NULL;
5573 struct hwi_controller *phwi_ctrlr;
5574 struct hwi_context_memory *phwi_context;
5575 struct be_eq_obj *pbe_eq;
5576 unsigned int s_handle;
5579 ret = beiscsi_enable_pci(pcidev);
5581 dev_err(&pcidev->dev,
5582 "beiscsi_dev_probe - Failed to enable pci device\n");
5586 phba = beiscsi_hba_alloc(pcidev);
5588 dev_err(&pcidev->dev,
5589 "beiscsi_dev_probe - Failed in beiscsi_hba_alloc\n");
5593 /* Enable EEH reporting */
5594 ret = pci_enable_pcie_error_reporting(pcidev);
5596 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5597 "BM_%d : PCIe Error Reporting "
5598 "Enabling Failed\n");
5600 pci_save_state(pcidev);
5602 /* Initialize Driver configuration Paramters */
5603 beiscsi_hba_attrs_init(phba);
5605 phba->mac_addr_set = false;
5607 switch (pcidev->device) {
5611 phba->generation = BE_GEN2;
5612 phba->iotask_fn = beiscsi_iotask;
5616 phba->generation = BE_GEN3;
5617 phba->iotask_fn = beiscsi_iotask;
5620 phba->generation = BE_GEN4;
5621 phba->iotask_fn = beiscsi_iotask_v2;
5624 phba->generation = 0;
5627 ret = be_ctrl_init(phba, pcidev);
5629 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5630 "BM_%d : beiscsi_dev_probe-"
5631 "Failed in be_ctrl_init\n");
5635 set_bit(BEISCSI_HBA_RUNNING, &phba->state);
5637 * FUNCTION_RESET should clean up any stale info in FW for this fn
5639 ret = beiscsi_cmd_reset_function(phba);
5641 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5642 "BM_%d : Reset Failed\n");
5645 ret = be_chk_reset_complete(phba);
5647 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5648 "BM_%d : Failed to get out of reset.\n");
5652 spin_lock_init(&phba->io_sgl_lock);
5653 spin_lock_init(&phba->mgmt_sgl_lock);
5654 spin_lock_init(&phba->async_pdu_lock);
5655 ret = mgmt_get_fw_config(&phba->ctrl, phba);
5657 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5658 "BM_%d : Error getting fw config\n");
5661 mgmt_get_port_name(&phba->ctrl, phba);
5662 beiscsi_get_params(phba);
5663 beiscsi_set_uer_feature(phba);
5666 find_num_cpus(phba);
5670 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
5671 "BM_%d : num_cpus = %d\n",
5675 beiscsi_msix_enable(phba);
5676 if (!phba->msix_enabled)
5680 phba->shost->max_id = phba->params.cxns_per_ctrl;
5681 phba->shost->can_queue = phba->params.ios_per_ctrl;
5682 ret = beiscsi_init_port(phba);
5684 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5685 "BM_%d : beiscsi_dev_probe-"
5686 "Failed in beiscsi_init_port\n");
5690 for (i = 0; i < MAX_MCC_CMD; i++) {
5691 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
5692 phba->ctrl.mcc_tag[i] = i + 1;
5693 phba->ctrl.mcc_tag_status[i + 1] = 0;
5694 phba->ctrl.mcc_tag_available++;
5695 memset(&phba->ctrl.ptag_state[i].tag_mem_state, 0,
5696 sizeof(struct be_dma_mem));
5699 phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
5701 snprintf(phba->wq_name, sizeof(phba->wq_name), "beiscsi_%02x_wq",
5702 phba->shost->host_no);
5703 phba->wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 1, phba->wq_name);
5705 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5706 "BM_%d : beiscsi_dev_probe-"
5707 "Failed to allocate work queue\n");
5711 INIT_DELAYED_WORK(&phba->eqd_update, beiscsi_eqd_update_work);
5713 phwi_ctrlr = phba->phwi_ctrlr;
5714 phwi_context = phwi_ctrlr->phwi_ctxt;
5716 for (i = 0; i < phba->num_cpus; i++) {
5717 pbe_eq = &phwi_context->be_eq[i];
5718 irq_poll_init(&pbe_eq->iopoll, be_iopoll_budget,
5722 i = (phba->msix_enabled) ? i : 0;
5723 /* Work item for MCC handling */
5724 pbe_eq = &phwi_context->be_eq[i];
5725 INIT_WORK(&pbe_eq->mcc_work, beiscsi_mcc_work);
5727 ret = beiscsi_init_irqs(phba);
5729 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5730 "BM_%d : beiscsi_dev_probe-"
5731 "Failed to beiscsi_init_irqs\n");
5734 hwi_enable_intr(phba);
5736 if (iscsi_host_add(phba->shost, &phba->pcidev->dev))
5739 INIT_WORK(&phba->boot_work, beiscsi_boot_work);
5740 ret = beiscsi_boot_get_shandle(phba, &s_handle);
5742 beiscsi_start_boot_work(phba, s_handle);
5744 * Set this bit after starting the work to let
5745 * probe handle it first.
5746 * ASYNC event can too schedule this work.
5748 set_bit(BEISCSI_HBA_BOOT_FOUND, &phba->state);
5751 beiscsi_iface_create_default(phba);
5752 schedule_delayed_work(&phba->eqd_update,
5753 msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
5755 * Start UE detection here. UE before this will cause stall in probe
5756 * and eventually fail the probe.
5758 init_timer(&phba->hw_check);
5759 phba->hw_check.function = beiscsi_hw_health_check;
5760 phba->hw_check.data = (unsigned long)phba;
5761 mod_timer(&phba->hw_check,
5762 jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
5764 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
5765 "\n\n\n BM_%d : SUCCESS - DRIVER LOADED\n\n\n");
5769 destroy_workqueue(phba->wq);
5770 for (i = 0; i < phba->num_cpus; i++) {
5771 pbe_eq = &phwi_context->be_eq[i];
5772 irq_poll_disable(&pbe_eq->iopoll);
5775 beiscsi_clean_port(phba);
5776 beiscsi_free_mem(phba);
5778 pci_free_consistent(phba->pcidev,
5779 phba->ctrl.mbox_mem_alloced.size,
5780 phba->ctrl.mbox_mem_alloced.va,
5781 phba->ctrl.mbox_mem_alloced.dma);
5782 beiscsi_unmap_pci_function(phba);
5784 if (phba->msix_enabled)
5785 pci_disable_msix(phba->pcidev);
5786 pci_dev_put(phba->pcidev);
5787 iscsi_host_free(phba->shost);
5788 pci_set_drvdata(pcidev, NULL);
5790 pci_release_regions(pcidev);
5791 pci_disable_device(pcidev);
5795 static struct pci_error_handlers beiscsi_eeh_handlers = {
5796 .error_detected = beiscsi_eeh_err_detected,
5797 .slot_reset = beiscsi_eeh_reset,
5798 .resume = beiscsi_eeh_resume,
5801 struct iscsi_transport beiscsi_iscsi_transport = {
5802 .owner = THIS_MODULE,
5804 .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
5805 CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
5806 .create_session = beiscsi_session_create,
5807 .destroy_session = beiscsi_session_destroy,
5808 .create_conn = beiscsi_conn_create,
5809 .bind_conn = beiscsi_conn_bind,
5810 .destroy_conn = iscsi_conn_teardown,
5811 .attr_is_visible = beiscsi_attr_is_visible,
5812 .set_iface_param = beiscsi_iface_set_param,
5813 .get_iface_param = beiscsi_iface_get_param,
5814 .set_param = beiscsi_set_param,
5815 .get_conn_param = iscsi_conn_get_param,
5816 .get_session_param = iscsi_session_get_param,
5817 .get_host_param = beiscsi_get_host_param,
5818 .start_conn = beiscsi_conn_start,
5819 .stop_conn = iscsi_conn_stop,
5820 .send_pdu = iscsi_conn_send_pdu,
5821 .xmit_task = beiscsi_task_xmit,
5822 .cleanup_task = beiscsi_cleanup_task,
5823 .alloc_pdu = beiscsi_alloc_pdu,
5824 .parse_pdu_itt = beiscsi_parse_pdu,
5825 .get_stats = beiscsi_conn_get_stats,
5826 .get_ep_param = beiscsi_ep_get_param,
5827 .ep_connect = beiscsi_ep_connect,
5828 .ep_poll = beiscsi_ep_poll,
5829 .ep_disconnect = beiscsi_ep_disconnect,
5830 .session_recovery_timedout = iscsi_session_recovery_timedout,
5831 .bsg_request = beiscsi_bsg_request,
5834 static struct pci_driver beiscsi_pci_driver = {
5836 .probe = beiscsi_dev_probe,
5837 .remove = beiscsi_remove,
5838 .id_table = beiscsi_pci_id_table,
5839 .err_handler = &beiscsi_eeh_handlers
5843 static int __init beiscsi_module_init(void)
5847 beiscsi_scsi_transport =
5848 iscsi_register_transport(&beiscsi_iscsi_transport);
5849 if (!beiscsi_scsi_transport) {
5851 "beiscsi_module_init - Unable to register beiscsi transport.\n");
5854 printk(KERN_INFO "In beiscsi_module_init, tt=%p\n",
5855 &beiscsi_iscsi_transport);
5857 ret = pci_register_driver(&beiscsi_pci_driver);
5860 "beiscsi_module_init - Unable to register beiscsi pci driver.\n");
5861 goto unregister_iscsi_transport;
5865 unregister_iscsi_transport:
5866 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5870 static void __exit beiscsi_module_exit(void)
5872 pci_unregister_driver(&beiscsi_pci_driver);
5873 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5876 module_init(beiscsi_module_init);
5877 module_exit(beiscsi_module_exit);