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(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(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 static ssize_t beiscsi_show_boot_tgt_info(void *data, int type, char *buf)
379 struct beiscsi_hba *phba = data;
380 struct mgmt_session_info *boot_sess = &phba->boot_sess;
381 struct mgmt_conn_info *boot_conn = &boot_sess->conn_list[0];
386 case ISCSI_BOOT_TGT_NAME:
387 rc = sprintf(buf, "%.*s\n",
388 (int)strlen(boot_sess->target_name),
389 (char *)&boot_sess->target_name);
391 case ISCSI_BOOT_TGT_IP_ADDR:
392 if (boot_conn->dest_ipaddr.ip_type == 0x1)
393 rc = sprintf(buf, "%pI4\n",
394 (char *)&boot_conn->dest_ipaddr.addr);
396 rc = sprintf(str, "%pI6\n",
397 (char *)&boot_conn->dest_ipaddr.addr);
399 case ISCSI_BOOT_TGT_PORT:
400 rc = sprintf(str, "%d\n", boot_conn->dest_port);
403 case ISCSI_BOOT_TGT_CHAP_NAME:
404 rc = sprintf(str, "%.*s\n",
405 boot_conn->negotiated_login_options.auth_data.chap.
406 target_chap_name_length,
407 (char *)&boot_conn->negotiated_login_options.
408 auth_data.chap.target_chap_name);
410 case ISCSI_BOOT_TGT_CHAP_SECRET:
411 rc = sprintf(str, "%.*s\n",
412 boot_conn->negotiated_login_options.auth_data.chap.
413 target_secret_length,
414 (char *)&boot_conn->negotiated_login_options.
415 auth_data.chap.target_secret);
417 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
418 rc = sprintf(str, "%.*s\n",
419 boot_conn->negotiated_login_options.auth_data.chap.
420 intr_chap_name_length,
421 (char *)&boot_conn->negotiated_login_options.
422 auth_data.chap.intr_chap_name);
424 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
425 rc = sprintf(str, "%.*s\n",
426 boot_conn->negotiated_login_options.auth_data.chap.
428 (char *)&boot_conn->negotiated_login_options.
429 auth_data.chap.intr_secret);
431 case ISCSI_BOOT_TGT_FLAGS:
432 rc = sprintf(str, "2\n");
434 case ISCSI_BOOT_TGT_NIC_ASSOC:
435 rc = sprintf(str, "0\n");
444 static ssize_t beiscsi_show_boot_ini_info(void *data, int type, char *buf)
446 struct beiscsi_hba *phba = data;
451 case ISCSI_BOOT_INI_INITIATOR_NAME:
452 rc = sprintf(str, "%s\n", phba->boot_sess.initiator_iscsiname);
461 static ssize_t beiscsi_show_boot_eth_info(void *data, int type, char *buf)
463 struct beiscsi_hba *phba = data;
468 case ISCSI_BOOT_ETH_FLAGS:
469 rc = sprintf(str, "2\n");
471 case ISCSI_BOOT_ETH_INDEX:
472 rc = sprintf(str, "0\n");
474 case ISCSI_BOOT_ETH_MAC:
475 rc = beiscsi_get_macaddr(str, phba);
485 static umode_t beiscsi_tgt_get_attr_visibility(void *data, int type)
490 case ISCSI_BOOT_TGT_NAME:
491 case ISCSI_BOOT_TGT_IP_ADDR:
492 case ISCSI_BOOT_TGT_PORT:
493 case ISCSI_BOOT_TGT_CHAP_NAME:
494 case ISCSI_BOOT_TGT_CHAP_SECRET:
495 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
496 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
497 case ISCSI_BOOT_TGT_NIC_ASSOC:
498 case ISCSI_BOOT_TGT_FLAGS:
508 static umode_t beiscsi_ini_get_attr_visibility(void *data, int type)
513 case ISCSI_BOOT_INI_INITIATOR_NAME:
524 static umode_t beiscsi_eth_get_attr_visibility(void *data, int type)
529 case ISCSI_BOOT_ETH_FLAGS:
530 case ISCSI_BOOT_ETH_MAC:
531 case ISCSI_BOOT_ETH_INDEX:
541 /*------------------- PCI Driver operations and data ----------------- */
542 static const struct pci_device_id beiscsi_pci_id_table[] = {
543 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
544 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
545 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
546 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
547 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID3) },
548 { PCI_DEVICE(ELX_VENDOR_ID, OC_SKH_ID1) },
551 MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
554 static struct scsi_host_template beiscsi_sht = {
555 .module = THIS_MODULE,
556 .name = "Emulex 10Gbe open-iscsi Initiator Driver",
557 .proc_name = DRV_NAME,
558 .queuecommand = iscsi_queuecommand,
559 .change_queue_depth = scsi_change_queue_depth,
560 .slave_configure = beiscsi_slave_configure,
561 .target_alloc = iscsi_target_alloc,
562 .eh_abort_handler = beiscsi_eh_abort,
563 .eh_device_reset_handler = beiscsi_eh_device_reset,
564 .eh_target_reset_handler = iscsi_eh_session_reset,
565 .shost_attrs = beiscsi_attrs,
566 .sg_tablesize = BEISCSI_SGLIST_ELEMENTS,
567 .can_queue = BE2_IO_DEPTH,
569 .max_sectors = BEISCSI_MAX_SECTORS,
570 .cmd_per_lun = BEISCSI_CMD_PER_LUN,
571 .use_clustering = ENABLE_CLUSTERING,
572 .vendor_id = SCSI_NL_VID_TYPE_PCI | BE_VENDOR_ID,
573 .track_queue_depth = 1,
576 static struct scsi_transport_template *beiscsi_scsi_transport;
578 static struct beiscsi_hba *beiscsi_hba_alloc(struct pci_dev *pcidev)
580 struct beiscsi_hba *phba;
581 struct Scsi_Host *shost;
583 shost = iscsi_host_alloc(&beiscsi_sht, sizeof(*phba), 0);
585 dev_err(&pcidev->dev,
586 "beiscsi_hba_alloc - iscsi_host_alloc failed\n");
589 shost->max_id = BE2_MAX_SESSIONS;
590 shost->max_channel = 0;
591 shost->max_cmd_len = BEISCSI_MAX_CMD_LEN;
592 shost->max_lun = BEISCSI_NUM_MAX_LUN;
593 shost->transportt = beiscsi_scsi_transport;
594 phba = iscsi_host_priv(shost);
595 memset(phba, 0, sizeof(*phba));
597 phba->pcidev = pci_dev_get(pcidev);
598 pci_set_drvdata(pcidev, phba);
599 phba->interface_handle = 0xFFFFFFFF;
604 static void beiscsi_unmap_pci_function(struct beiscsi_hba *phba)
607 iounmap(phba->csr_va);
611 iounmap(phba->db_va);
615 iounmap(phba->pci_va);
620 static int beiscsi_map_pci_bars(struct beiscsi_hba *phba,
621 struct pci_dev *pcidev)
626 addr = ioremap_nocache(pci_resource_start(pcidev, 2),
627 pci_resource_len(pcidev, 2));
630 phba->ctrl.csr = addr;
632 phba->csr_pa.u.a64.address = pci_resource_start(pcidev, 2);
634 addr = ioremap_nocache(pci_resource_start(pcidev, 4), 128 * 1024);
637 phba->ctrl.db = addr;
639 phba->db_pa.u.a64.address = pci_resource_start(pcidev, 4);
641 if (phba->generation == BE_GEN2)
646 addr = ioremap_nocache(pci_resource_start(pcidev, pcicfg_reg),
647 pci_resource_len(pcidev, pcicfg_reg));
651 phba->ctrl.pcicfg = addr;
653 phba->pci_pa.u.a64.address = pci_resource_start(pcidev, pcicfg_reg);
657 beiscsi_unmap_pci_function(phba);
661 static int beiscsi_enable_pci(struct pci_dev *pcidev)
665 ret = pci_enable_device(pcidev);
667 dev_err(&pcidev->dev,
668 "beiscsi_enable_pci - enable device failed\n");
672 ret = pci_request_regions(pcidev, DRV_NAME);
674 dev_err(&pcidev->dev,
675 "beiscsi_enable_pci - request region failed\n");
676 goto pci_dev_disable;
679 pci_set_master(pcidev);
680 ret = pci_set_dma_mask(pcidev, DMA_BIT_MASK(64));
682 ret = pci_set_dma_mask(pcidev, DMA_BIT_MASK(32));
684 dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
685 goto pci_region_release;
687 ret = pci_set_consistent_dma_mask(pcidev,
691 ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(64));
693 dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
694 goto pci_region_release;
700 pci_release_regions(pcidev);
702 pci_disable_device(pcidev);
707 static int be_ctrl_init(struct beiscsi_hba *phba, struct pci_dev *pdev)
709 struct be_ctrl_info *ctrl = &phba->ctrl;
710 struct be_dma_mem *mbox_mem_alloc = &ctrl->mbox_mem_alloced;
711 struct be_dma_mem *mbox_mem_align = &ctrl->mbox_mem;
715 status = beiscsi_map_pci_bars(phba, pdev);
718 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
719 mbox_mem_alloc->va = pci_alloc_consistent(pdev,
720 mbox_mem_alloc->size,
721 &mbox_mem_alloc->dma);
722 if (!mbox_mem_alloc->va) {
723 beiscsi_unmap_pci_function(phba);
727 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
728 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
729 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
730 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
731 mutex_init(&ctrl->mbox_lock);
732 spin_lock_init(&phba->ctrl.mcc_lock);
738 * beiscsi_get_params()- Set the config paramters
739 * @phba: ptr device priv structure
741 static void beiscsi_get_params(struct beiscsi_hba *phba)
743 uint32_t total_cid_count = 0;
744 uint32_t total_icd_count = 0;
747 total_cid_count = BEISCSI_GET_CID_COUNT(phba, BEISCSI_ULP0) +
748 BEISCSI_GET_CID_COUNT(phba, BEISCSI_ULP1);
750 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
751 uint32_t align_mask = 0;
752 uint32_t icd_post_per_page = 0;
753 uint32_t icd_count_unavailable = 0;
754 uint32_t icd_start = 0, icd_count = 0;
755 uint32_t icd_start_align = 0, icd_count_align = 0;
757 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
758 icd_start = phba->fw_config.iscsi_icd_start[ulp_num];
759 icd_count = phba->fw_config.iscsi_icd_count[ulp_num];
761 /* Get ICD count that can be posted on each page */
762 icd_post_per_page = (PAGE_SIZE / (BE2_SGE *
763 sizeof(struct iscsi_sge)));
764 align_mask = (icd_post_per_page - 1);
766 /* Check if icd_start is aligned ICD per page posting */
767 if (icd_start % icd_post_per_page) {
768 icd_start_align = ((icd_start +
772 iscsi_icd_start[ulp_num] =
776 icd_count_align = (icd_count & ~align_mask);
778 /* ICD discarded in the process of alignment */
780 icd_count_unavailable = ((icd_start_align -
785 /* Updated ICD count available */
786 phba->fw_config.iscsi_icd_count[ulp_num] = (icd_count -
787 icd_count_unavailable);
789 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
790 "BM_%d : Aligned ICD values\n"
791 "\t ICD Start : %d\n"
792 "\t ICD Count : %d\n"
793 "\t ICD Discarded : %d\n",
795 iscsi_icd_start[ulp_num],
797 iscsi_icd_count[ulp_num],
798 icd_count_unavailable);
803 total_icd_count = phba->fw_config.iscsi_icd_count[ulp_num];
804 phba->params.ios_per_ctrl = (total_icd_count -
806 BE2_TMFS + BE2_NOPOUT_REQ));
807 phba->params.cxns_per_ctrl = total_cid_count;
808 phba->params.asyncpdus_per_ctrl = total_cid_count;
809 phba->params.icds_per_ctrl = total_icd_count;
810 phba->params.num_sge_per_io = BE2_SGE;
811 phba->params.defpdu_hdr_sz = BE2_DEFPDU_HDR_SZ;
812 phba->params.defpdu_data_sz = BE2_DEFPDU_DATA_SZ;
813 phba->params.eq_timer = 64;
814 phba->params.num_eq_entries = 1024;
815 phba->params.num_cq_entries = 1024;
816 phba->params.wrbs_per_cxn = 256;
819 static void hwi_ring_eq_db(struct beiscsi_hba *phba,
820 unsigned int id, unsigned int clr_interrupt,
821 unsigned int num_processed,
822 unsigned char rearm, unsigned char event)
827 val |= 1 << DB_EQ_REARM_SHIFT;
829 val |= 1 << DB_EQ_CLR_SHIFT;
831 val |= 1 << DB_EQ_EVNT_SHIFT;
833 val |= num_processed << DB_EQ_NUM_POPPED_SHIFT;
834 /* Setting lower order EQ_ID Bits */
835 val |= (id & DB_EQ_RING_ID_LOW_MASK);
837 /* Setting Higher order EQ_ID Bits */
838 val |= (((id >> DB_EQ_HIGH_FEILD_SHIFT) &
839 DB_EQ_RING_ID_HIGH_MASK)
840 << DB_EQ_HIGH_SET_SHIFT);
842 iowrite32(val, phba->db_va + DB_EQ_OFFSET);
846 * be_isr_mcc - The isr routine of the driver.
848 * @dev_id: Pointer to host adapter structure
850 static irqreturn_t be_isr_mcc(int irq, void *dev_id)
852 struct beiscsi_hba *phba;
853 struct be_eq_entry *eqe = NULL;
854 struct be_queue_info *eq;
855 struct be_queue_info *mcc;
856 unsigned int num_eq_processed;
857 struct be_eq_obj *pbe_eq;
863 mcc = &phba->ctrl.mcc_obj.cq;
864 eqe = queue_tail_node(eq);
866 num_eq_processed = 0;
868 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
870 if (((eqe->dw[offsetof(struct amap_eq_entry,
872 EQE_RESID_MASK) >> 16) == mcc->id) {
873 spin_lock_irqsave(&phba->isr_lock, flags);
874 pbe_eq->todo_mcc_cq = true;
875 spin_unlock_irqrestore(&phba->isr_lock, flags);
877 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
879 eqe = queue_tail_node(eq);
882 if (pbe_eq->todo_mcc_cq)
883 queue_work(phba->wq, &pbe_eq->work_cqs);
884 if (num_eq_processed)
885 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 1, 1);
891 * be_isr_msix - The isr routine of the driver.
893 * @dev_id: Pointer to host adapter structure
895 static irqreturn_t be_isr_msix(int irq, void *dev_id)
897 struct beiscsi_hba *phba;
898 struct be_queue_info *eq;
899 struct be_eq_obj *pbe_eq;
906 /* disable interrupt till iopoll completes */
907 hwi_ring_eq_db(phba, eq->id, 1, 0, 0, 1);
908 irq_poll_sched(&pbe_eq->iopoll);
914 * be_isr - The isr routine of the driver.
916 * @dev_id: Pointer to host adapter structure
918 static irqreturn_t be_isr(int irq, void *dev_id)
920 struct beiscsi_hba *phba;
921 struct hwi_controller *phwi_ctrlr;
922 struct hwi_context_memory *phwi_context;
923 struct be_eq_entry *eqe = NULL;
924 struct be_queue_info *eq;
925 struct be_queue_info *mcc;
926 unsigned long flags, index;
927 unsigned int num_mcceq_processed, num_ioeq_processed;
928 struct be_ctrl_info *ctrl;
929 struct be_eq_obj *pbe_eq;
934 isr = ioread32(ctrl->csr + CEV_ISR0_OFFSET +
935 (PCI_FUNC(ctrl->pdev->devfn) * CEV_ISR_SIZE));
939 phwi_ctrlr = phba->phwi_ctrlr;
940 phwi_context = phwi_ctrlr->phwi_ctxt;
941 pbe_eq = &phwi_context->be_eq[0];
943 eq = &phwi_context->be_eq[0].q;
944 mcc = &phba->ctrl.mcc_obj.cq;
946 eqe = queue_tail_node(eq);
948 num_ioeq_processed = 0;
949 num_mcceq_processed = 0;
950 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
952 if (((eqe->dw[offsetof(struct amap_eq_entry,
954 EQE_RESID_MASK) >> 16) == mcc->id) {
955 spin_lock_irqsave(&phba->isr_lock, flags);
956 pbe_eq->todo_mcc_cq = true;
957 spin_unlock_irqrestore(&phba->isr_lock, flags);
958 num_mcceq_processed++;
960 irq_poll_sched(&pbe_eq->iopoll);
961 num_ioeq_processed++;
963 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
965 eqe = queue_tail_node(eq);
967 if (num_ioeq_processed || num_mcceq_processed) {
968 if (pbe_eq->todo_mcc_cq)
969 queue_work(phba->wq, &pbe_eq->work_cqs);
971 if ((num_mcceq_processed) && (!num_ioeq_processed))
972 hwi_ring_eq_db(phba, eq->id, 0,
973 (num_ioeq_processed +
974 num_mcceq_processed) , 1, 1);
976 hwi_ring_eq_db(phba, eq->id, 0,
977 (num_ioeq_processed +
978 num_mcceq_processed), 0, 1);
986 static int beiscsi_init_irqs(struct beiscsi_hba *phba)
988 struct pci_dev *pcidev = phba->pcidev;
989 struct hwi_controller *phwi_ctrlr;
990 struct hwi_context_memory *phwi_context;
991 int ret, msix_vec, i, j;
993 phwi_ctrlr = phba->phwi_ctrlr;
994 phwi_context = phwi_ctrlr->phwi_ctxt;
996 if (phba->msix_enabled) {
997 for (i = 0; i < phba->num_cpus; i++) {
998 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME,
1000 if (!phba->msi_name[i]) {
1002 goto free_msix_irqs;
1005 sprintf(phba->msi_name[i], "beiscsi_%02x_%02x",
1006 phba->shost->host_no, i);
1007 msix_vec = phba->msix_entries[i].vector;
1008 ret = request_irq(msix_vec, be_isr_msix, 0,
1010 &phwi_context->be_eq[i]);
1012 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
1013 "BM_%d : beiscsi_init_irqs-Failed to"
1014 "register msix for i = %d\n",
1016 kfree(phba->msi_name[i]);
1017 goto free_msix_irqs;
1020 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME, GFP_KERNEL);
1021 if (!phba->msi_name[i]) {
1023 goto free_msix_irqs;
1025 sprintf(phba->msi_name[i], "beiscsi_mcc_%02x",
1026 phba->shost->host_no);
1027 msix_vec = phba->msix_entries[i].vector;
1028 ret = request_irq(msix_vec, be_isr_mcc, 0, phba->msi_name[i],
1029 &phwi_context->be_eq[i]);
1031 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT ,
1032 "BM_%d : beiscsi_init_irqs-"
1033 "Failed to register beiscsi_msix_mcc\n");
1034 kfree(phba->msi_name[i]);
1035 goto free_msix_irqs;
1039 ret = request_irq(pcidev->irq, be_isr, IRQF_SHARED,
1042 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
1043 "BM_%d : beiscsi_init_irqs-"
1044 "Failed to register irq\\n");
1050 for (j = i - 1; j >= 0; j--) {
1051 kfree(phba->msi_name[j]);
1052 msix_vec = phba->msix_entries[j].vector;
1053 free_irq(msix_vec, &phwi_context->be_eq[j]);
1058 void hwi_ring_cq_db(struct beiscsi_hba *phba,
1059 unsigned int id, unsigned int num_processed,
1060 unsigned char rearm)
1065 val |= 1 << DB_CQ_REARM_SHIFT;
1067 val |= num_processed << DB_CQ_NUM_POPPED_SHIFT;
1069 /* Setting lower order CQ_ID Bits */
1070 val |= (id & DB_CQ_RING_ID_LOW_MASK);
1072 /* Setting Higher order CQ_ID Bits */
1073 val |= (((id >> DB_CQ_HIGH_FEILD_SHIFT) &
1074 DB_CQ_RING_ID_HIGH_MASK)
1075 << DB_CQ_HIGH_SET_SHIFT);
1077 iowrite32(val, phba->db_va + DB_CQ_OFFSET);
1081 beiscsi_process_async_pdu(struct beiscsi_conn *beiscsi_conn,
1082 struct beiscsi_hba *phba,
1083 struct pdu_base *ppdu,
1084 unsigned long pdu_len,
1085 void *pbuffer, unsigned long buf_len)
1087 struct iscsi_conn *conn = beiscsi_conn->conn;
1088 struct iscsi_session *session = conn->session;
1089 struct iscsi_task *task;
1090 struct beiscsi_io_task *io_task;
1091 struct iscsi_hdr *login_hdr;
1093 switch (ppdu->dw[offsetof(struct amap_pdu_base, opcode) / 32] &
1094 PDUBASE_OPCODE_MASK) {
1095 case ISCSI_OP_NOOP_IN:
1099 case ISCSI_OP_ASYNC_EVENT:
1101 case ISCSI_OP_REJECT:
1103 WARN_ON(!(buf_len == 48));
1104 beiscsi_log(phba, KERN_ERR,
1105 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1106 "BM_%d : In ISCSI_OP_REJECT\n");
1108 case ISCSI_OP_LOGIN_RSP:
1109 case ISCSI_OP_TEXT_RSP:
1110 task = conn->login_task;
1111 io_task = task->dd_data;
1112 login_hdr = (struct iscsi_hdr *)ppdu;
1113 login_hdr->itt = io_task->libiscsi_itt;
1116 beiscsi_log(phba, KERN_WARNING,
1117 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1118 "BM_%d : Unrecognized opcode 0x%x in async msg\n",
1120 dw[offsetof(struct amap_pdu_base, opcode) / 32]
1121 & PDUBASE_OPCODE_MASK));
1125 spin_lock_bh(&session->back_lock);
1126 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)ppdu, pbuffer, buf_len);
1127 spin_unlock_bh(&session->back_lock);
1131 static struct sgl_handle *alloc_io_sgl_handle(struct beiscsi_hba *phba)
1133 struct sgl_handle *psgl_handle;
1135 if (phba->io_sgl_hndl_avbl) {
1136 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
1137 "BM_%d : In alloc_io_sgl_handle,"
1138 " io_sgl_alloc_index=%d\n",
1139 phba->io_sgl_alloc_index);
1141 psgl_handle = phba->io_sgl_hndl_base[phba->
1142 io_sgl_alloc_index];
1143 phba->io_sgl_hndl_base[phba->io_sgl_alloc_index] = NULL;
1144 phba->io_sgl_hndl_avbl--;
1145 if (phba->io_sgl_alloc_index == (phba->params.
1147 phba->io_sgl_alloc_index = 0;
1149 phba->io_sgl_alloc_index++;
1156 free_io_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
1158 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
1159 "BM_%d : In free_,io_sgl_free_index=%d\n",
1160 phba->io_sgl_free_index);
1162 if (phba->io_sgl_hndl_base[phba->io_sgl_free_index]) {
1164 * this can happen if clean_task is called on a task that
1165 * failed in xmit_task or alloc_pdu.
1167 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
1168 "BM_%d : Double Free in IO SGL io_sgl_free_index=%d,"
1169 "value there=%p\n", phba->io_sgl_free_index,
1170 phba->io_sgl_hndl_base
1171 [phba->io_sgl_free_index]);
1174 phba->io_sgl_hndl_base[phba->io_sgl_free_index] = psgl_handle;
1175 phba->io_sgl_hndl_avbl++;
1176 if (phba->io_sgl_free_index == (phba->params.ios_per_ctrl - 1))
1177 phba->io_sgl_free_index = 0;
1179 phba->io_sgl_free_index++;
1182 static inline struct wrb_handle *
1183 beiscsi_get_wrb_handle(struct hwi_wrb_context *pwrb_context,
1184 unsigned int wrbs_per_cxn)
1186 struct wrb_handle *pwrb_handle;
1188 pwrb_handle = pwrb_context->pwrb_handle_base[pwrb_context->alloc_index];
1189 pwrb_context->wrb_handles_available--;
1190 if (pwrb_context->alloc_index == (wrbs_per_cxn - 1))
1191 pwrb_context->alloc_index = 0;
1193 pwrb_context->alloc_index++;
1199 * alloc_wrb_handle - To allocate a wrb handle
1200 * @phba: The hba pointer
1201 * @cid: The cid to use for allocation
1202 * @pwrb_context: ptr to ptr to wrb context
1204 * This happens under session_lock until submission to chip
1206 struct wrb_handle *alloc_wrb_handle(struct beiscsi_hba *phba, unsigned int cid,
1207 struct hwi_wrb_context **pcontext)
1209 struct hwi_wrb_context *pwrb_context;
1210 struct hwi_controller *phwi_ctrlr;
1211 uint16_t cri_index = BE_GET_CRI_FROM_CID(cid);
1213 phwi_ctrlr = phba->phwi_ctrlr;
1214 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
1215 /* return the context address */
1216 *pcontext = pwrb_context;
1217 return beiscsi_get_wrb_handle(pwrb_context, phba->params.wrbs_per_cxn);
1221 beiscsi_put_wrb_handle(struct hwi_wrb_context *pwrb_context,
1222 struct wrb_handle *pwrb_handle,
1223 unsigned int wrbs_per_cxn)
1225 pwrb_context->pwrb_handle_base[pwrb_context->free_index] = pwrb_handle;
1226 pwrb_context->wrb_handles_available++;
1227 if (pwrb_context->free_index == (wrbs_per_cxn - 1))
1228 pwrb_context->free_index = 0;
1230 pwrb_context->free_index++;
1234 * free_wrb_handle - To free the wrb handle back to pool
1235 * @phba: The hba pointer
1236 * @pwrb_context: The context to free from
1237 * @pwrb_handle: The wrb_handle to free
1239 * This happens under session_lock until submission to chip
1242 free_wrb_handle(struct beiscsi_hba *phba, struct hwi_wrb_context *pwrb_context,
1243 struct wrb_handle *pwrb_handle)
1245 beiscsi_put_wrb_handle(pwrb_context,
1247 phba->params.wrbs_per_cxn);
1248 beiscsi_log(phba, KERN_INFO,
1249 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1250 "BM_%d : FREE WRB: pwrb_handle=%p free_index=0x%x"
1251 "wrb_handles_available=%d\n",
1252 pwrb_handle, pwrb_context->free_index,
1253 pwrb_context->wrb_handles_available);
1256 static struct sgl_handle *alloc_mgmt_sgl_handle(struct beiscsi_hba *phba)
1258 struct sgl_handle *psgl_handle;
1260 if (phba->eh_sgl_hndl_avbl) {
1261 psgl_handle = phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index];
1262 phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index] = NULL;
1263 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1264 "BM_%d : mgmt_sgl_alloc_index=%d=0x%x\n",
1265 phba->eh_sgl_alloc_index,
1266 phba->eh_sgl_alloc_index);
1268 phba->eh_sgl_hndl_avbl--;
1269 if (phba->eh_sgl_alloc_index ==
1270 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl -
1272 phba->eh_sgl_alloc_index = 0;
1274 phba->eh_sgl_alloc_index++;
1281 free_mgmt_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
1284 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1285 "BM_%d : In free_mgmt_sgl_handle,"
1286 "eh_sgl_free_index=%d\n",
1287 phba->eh_sgl_free_index);
1289 if (phba->eh_sgl_hndl_base[phba->eh_sgl_free_index]) {
1291 * this can happen if clean_task is called on a task that
1292 * failed in xmit_task or alloc_pdu.
1294 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
1295 "BM_%d : Double Free in eh SGL ,"
1296 "eh_sgl_free_index=%d\n",
1297 phba->eh_sgl_free_index);
1300 phba->eh_sgl_hndl_base[phba->eh_sgl_free_index] = psgl_handle;
1301 phba->eh_sgl_hndl_avbl++;
1302 if (phba->eh_sgl_free_index ==
1303 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl - 1))
1304 phba->eh_sgl_free_index = 0;
1306 phba->eh_sgl_free_index++;
1310 be_complete_io(struct beiscsi_conn *beiscsi_conn,
1311 struct iscsi_task *task,
1312 struct common_sol_cqe *csol_cqe)
1314 struct beiscsi_io_task *io_task = task->dd_data;
1315 struct be_status_bhs *sts_bhs =
1316 (struct be_status_bhs *)io_task->cmd_bhs;
1317 struct iscsi_conn *conn = beiscsi_conn->conn;
1318 unsigned char *sense;
1319 u32 resid = 0, exp_cmdsn, max_cmdsn;
1320 u8 rsp, status, flags;
1322 exp_cmdsn = csol_cqe->exp_cmdsn;
1323 max_cmdsn = (csol_cqe->exp_cmdsn +
1324 csol_cqe->cmd_wnd - 1);
1325 rsp = csol_cqe->i_resp;
1326 status = csol_cqe->i_sts;
1327 flags = csol_cqe->i_flags;
1328 resid = csol_cqe->res_cnt;
1331 if (io_task->scsi_cmnd) {
1332 scsi_dma_unmap(io_task->scsi_cmnd);
1333 io_task->scsi_cmnd = NULL;
1338 task->sc->result = (DID_OK << 16) | status;
1339 if (rsp != ISCSI_STATUS_CMD_COMPLETED) {
1340 task->sc->result = DID_ERROR << 16;
1344 /* bidi not initially supported */
1345 if (flags & (ISCSI_FLAG_CMD_UNDERFLOW | ISCSI_FLAG_CMD_OVERFLOW)) {
1346 if (!status && (flags & ISCSI_FLAG_CMD_OVERFLOW))
1347 task->sc->result = DID_ERROR << 16;
1349 if (flags & ISCSI_FLAG_CMD_UNDERFLOW) {
1350 scsi_set_resid(task->sc, resid);
1351 if (!status && (scsi_bufflen(task->sc) - resid <
1352 task->sc->underflow))
1353 task->sc->result = DID_ERROR << 16;
1357 if (status == SAM_STAT_CHECK_CONDITION) {
1359 unsigned short *slen = (unsigned short *)sts_bhs->sense_info;
1361 sense = sts_bhs->sense_info + sizeof(unsigned short);
1362 sense_len = be16_to_cpu(*slen);
1363 memcpy(task->sc->sense_buffer, sense,
1364 min_t(u16, sense_len, SCSI_SENSE_BUFFERSIZE));
1367 if (io_task->cmd_bhs->iscsi_hdr.flags & ISCSI_FLAG_CMD_READ)
1368 conn->rxdata_octets += resid;
1370 if (io_task->scsi_cmnd) {
1371 scsi_dma_unmap(io_task->scsi_cmnd);
1372 io_task->scsi_cmnd = NULL;
1374 iscsi_complete_scsi_task(task, exp_cmdsn, max_cmdsn);
1378 be_complete_logout(struct beiscsi_conn *beiscsi_conn,
1379 struct iscsi_task *task,
1380 struct common_sol_cqe *csol_cqe)
1382 struct iscsi_logout_rsp *hdr;
1383 struct beiscsi_io_task *io_task = task->dd_data;
1384 struct iscsi_conn *conn = beiscsi_conn->conn;
1386 hdr = (struct iscsi_logout_rsp *)task->hdr;
1387 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
1390 hdr->flags = csol_cqe->i_flags;
1391 hdr->response = csol_cqe->i_resp;
1392 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
1393 hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
1394 csol_cqe->cmd_wnd - 1);
1396 hdr->dlength[0] = 0;
1397 hdr->dlength[1] = 0;
1398 hdr->dlength[2] = 0;
1400 hdr->itt = io_task->libiscsi_itt;
1401 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1405 be_complete_tmf(struct beiscsi_conn *beiscsi_conn,
1406 struct iscsi_task *task,
1407 struct common_sol_cqe *csol_cqe)
1409 struct iscsi_tm_rsp *hdr;
1410 struct iscsi_conn *conn = beiscsi_conn->conn;
1411 struct beiscsi_io_task *io_task = task->dd_data;
1413 hdr = (struct iscsi_tm_rsp *)task->hdr;
1414 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
1415 hdr->flags = csol_cqe->i_flags;
1416 hdr->response = csol_cqe->i_resp;
1417 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
1418 hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
1419 csol_cqe->cmd_wnd - 1);
1421 hdr->itt = io_task->libiscsi_itt;
1422 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1426 hwi_complete_drvr_msgs(struct beiscsi_conn *beiscsi_conn,
1427 struct beiscsi_hba *phba, struct sol_cqe *psol)
1429 struct hwi_wrb_context *pwrb_context;
1430 struct wrb_handle *pwrb_handle = NULL;
1431 struct hwi_controller *phwi_ctrlr;
1432 struct iscsi_task *task;
1433 struct beiscsi_io_task *io_task;
1434 uint16_t wrb_index, cid, cri_index;
1436 phwi_ctrlr = phba->phwi_ctrlr;
1437 if (is_chip_be2_be3r(phba)) {
1438 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1440 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1443 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1445 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1449 cri_index = BE_GET_CRI_FROM_CID(cid);
1450 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
1451 pwrb_handle = pwrb_context->pwrb_handle_basestd[wrb_index];
1452 task = pwrb_handle->pio_handle;
1454 io_task = task->dd_data;
1455 memset(io_task->pwrb_handle->pwrb, 0, sizeof(struct iscsi_wrb));
1456 iscsi_put_task(task);
1460 be_complete_nopin_resp(struct beiscsi_conn *beiscsi_conn,
1461 struct iscsi_task *task,
1462 struct common_sol_cqe *csol_cqe)
1464 struct iscsi_nopin *hdr;
1465 struct iscsi_conn *conn = beiscsi_conn->conn;
1466 struct beiscsi_io_task *io_task = task->dd_data;
1468 hdr = (struct iscsi_nopin *)task->hdr;
1469 hdr->flags = csol_cqe->i_flags;
1470 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
1471 hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
1472 csol_cqe->cmd_wnd - 1);
1474 hdr->opcode = ISCSI_OP_NOOP_IN;
1475 hdr->itt = io_task->libiscsi_itt;
1476 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1479 static void adapter_get_sol_cqe(struct beiscsi_hba *phba,
1480 struct sol_cqe *psol,
1481 struct common_sol_cqe *csol_cqe)
1483 if (is_chip_be2_be3r(phba)) {
1484 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe,
1485 i_exp_cmd_sn, psol);
1486 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe,
1488 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe,
1490 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe,
1492 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe,
1494 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1496 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe,
1498 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1500 csol_cqe->i_flags = AMAP_GET_BITS(struct amap_sol_cqe,
1503 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1504 i_exp_cmd_sn, psol);
1505 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1507 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1509 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1511 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1513 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1515 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1517 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1520 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1522 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1524 csol_cqe->i_flags = ISCSI_FLAG_CMD_UNDERFLOW;
1526 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1528 csol_cqe->i_flags |= ISCSI_FLAG_CMD_OVERFLOW;
1533 static void hwi_complete_cmd(struct beiscsi_conn *beiscsi_conn,
1534 struct beiscsi_hba *phba, struct sol_cqe *psol)
1536 struct hwi_wrb_context *pwrb_context;
1537 struct wrb_handle *pwrb_handle;
1538 struct iscsi_wrb *pwrb = NULL;
1539 struct hwi_controller *phwi_ctrlr;
1540 struct iscsi_task *task;
1542 struct iscsi_conn *conn = beiscsi_conn->conn;
1543 struct iscsi_session *session = conn->session;
1544 struct common_sol_cqe csol_cqe = {0};
1545 uint16_t cri_index = 0;
1547 phwi_ctrlr = phba->phwi_ctrlr;
1549 /* Copy the elements to a common structure */
1550 adapter_get_sol_cqe(phba, psol, &csol_cqe);
1552 cri_index = BE_GET_CRI_FROM_CID(csol_cqe.cid);
1553 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
1555 pwrb_handle = pwrb_context->pwrb_handle_basestd[
1556 csol_cqe.wrb_index];
1558 task = pwrb_handle->pio_handle;
1559 pwrb = pwrb_handle->pwrb;
1560 type = ((struct beiscsi_io_task *)task->dd_data)->wrb_type;
1562 spin_lock_bh(&session->back_lock);
1565 case HWH_TYPE_IO_RD:
1566 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) ==
1568 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
1570 be_complete_io(beiscsi_conn, task, &csol_cqe);
1573 case HWH_TYPE_LOGOUT:
1574 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
1575 be_complete_logout(beiscsi_conn, task, &csol_cqe);
1577 be_complete_tmf(beiscsi_conn, task, &csol_cqe);
1580 case HWH_TYPE_LOGIN:
1581 beiscsi_log(phba, KERN_ERR,
1582 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1583 "BM_%d :\t\t No HWH_TYPE_LOGIN Expected in"
1584 " hwi_complete_cmd- Solicited path\n");
1588 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
1592 beiscsi_log(phba, KERN_WARNING,
1593 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1594 "BM_%d : In hwi_complete_cmd, unknown type = %d"
1595 "wrb_index 0x%x CID 0x%x\n", type,
1601 spin_unlock_bh(&session->back_lock);
1604 static struct list_head *hwi_get_async_busy_list(struct hwi_async_pdu_context
1605 *pasync_ctx, unsigned int is_header,
1606 unsigned int host_write_ptr)
1609 return &pasync_ctx->async_entry[host_write_ptr].
1612 return &pasync_ctx->async_entry[host_write_ptr].data_busy_list;
1615 static struct async_pdu_handle *
1616 hwi_get_async_handle(struct beiscsi_hba *phba,
1617 struct beiscsi_conn *beiscsi_conn,
1618 struct hwi_async_pdu_context *pasync_ctx,
1619 struct i_t_dpdu_cqe *pdpdu_cqe, unsigned int *pcq_index)
1621 struct be_bus_address phys_addr;
1622 struct list_head *pbusy_list;
1623 struct async_pdu_handle *pasync_handle = NULL;
1624 unsigned char is_header = 0;
1625 unsigned int index, dpl;
1627 if (is_chip_be2_be3r(phba)) {
1628 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1630 index = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1633 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1635 index = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1639 phys_addr.u.a32.address_lo =
1640 (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1641 db_addr_lo) / 32] - dpl);
1642 phys_addr.u.a32.address_hi =
1643 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1646 phys_addr.u.a64.address =
1647 *((unsigned long long *)(&phys_addr.u.a64.address));
1649 switch (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, code) / 32]
1650 & PDUCQE_CODE_MASK) {
1651 case UNSOL_HDR_NOTIFY:
1654 pbusy_list = hwi_get_async_busy_list(pasync_ctx,
1657 case UNSOL_DATA_NOTIFY:
1658 pbusy_list = hwi_get_async_busy_list(pasync_ctx,
1663 beiscsi_log(phba, KERN_WARNING,
1664 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1665 "BM_%d : Unexpected code=%d\n",
1666 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1667 code) / 32] & PDUCQE_CODE_MASK);
1671 WARN_ON(list_empty(pbusy_list));
1672 list_for_each_entry(pasync_handle, pbusy_list, link) {
1673 if (pasync_handle->pa.u.a64.address == phys_addr.u.a64.address)
1677 WARN_ON(!pasync_handle);
1679 pasync_handle->cri = BE_GET_ASYNC_CRI_FROM_CID(
1680 beiscsi_conn->beiscsi_conn_cid);
1681 pasync_handle->is_header = is_header;
1682 pasync_handle->buffer_len = dpl;
1685 return pasync_handle;
1689 hwi_update_async_writables(struct beiscsi_hba *phba,
1690 struct hwi_async_pdu_context *pasync_ctx,
1691 unsigned int is_header, unsigned int cq_index)
1693 struct list_head *pbusy_list;
1694 struct async_pdu_handle *pasync_handle;
1695 unsigned int num_entries, writables = 0;
1696 unsigned int *pep_read_ptr, *pwritables;
1698 num_entries = pasync_ctx->num_entries;
1700 pep_read_ptr = &pasync_ctx->async_header.ep_read_ptr;
1701 pwritables = &pasync_ctx->async_header.writables;
1703 pep_read_ptr = &pasync_ctx->async_data.ep_read_ptr;
1704 pwritables = &pasync_ctx->async_data.writables;
1707 while ((*pep_read_ptr) != cq_index) {
1709 *pep_read_ptr = (*pep_read_ptr) % num_entries;
1711 pbusy_list = hwi_get_async_busy_list(pasync_ctx, is_header,
1714 WARN_ON(list_empty(pbusy_list));
1716 if (!list_empty(pbusy_list)) {
1717 pasync_handle = list_entry(pbusy_list->next,
1718 struct async_pdu_handle,
1720 WARN_ON(!pasync_handle);
1721 pasync_handle->consumed = 1;
1728 beiscsi_log(phba, KERN_ERR,
1729 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1730 "BM_%d : Duplicate notification received - index 0x%x!!\n",
1735 *pwritables = *pwritables + writables;
1739 static void hwi_free_async_msg(struct beiscsi_hba *phba,
1740 struct hwi_async_pdu_context *pasync_ctx,
1743 struct async_pdu_handle *pasync_handle, *tmp_handle;
1744 struct list_head *plist;
1746 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1747 list_for_each_entry_safe(pasync_handle, tmp_handle, plist, link) {
1748 list_del(&pasync_handle->link);
1750 if (pasync_handle->is_header) {
1751 list_add_tail(&pasync_handle->link,
1752 &pasync_ctx->async_header.free_list);
1753 pasync_ctx->async_header.free_entries++;
1755 list_add_tail(&pasync_handle->link,
1756 &pasync_ctx->async_data.free_list);
1757 pasync_ctx->async_data.free_entries++;
1761 INIT_LIST_HEAD(&pasync_ctx->async_entry[cri].wait_queue.list);
1762 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 0;
1763 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
1766 static struct phys_addr *
1767 hwi_get_ring_address(struct hwi_async_pdu_context *pasync_ctx,
1768 unsigned int is_header, unsigned int host_write_ptr)
1770 struct phys_addr *pasync_sge = NULL;
1773 pasync_sge = pasync_ctx->async_header.ring_base;
1775 pasync_sge = pasync_ctx->async_data.ring_base;
1777 return pasync_sge + host_write_ptr;
1780 static void hwi_post_async_buffers(struct beiscsi_hba *phba,
1781 unsigned int is_header, uint8_t ulp_num)
1783 struct hwi_controller *phwi_ctrlr;
1784 struct hwi_async_pdu_context *pasync_ctx;
1785 struct async_pdu_handle *pasync_handle;
1786 struct list_head *pfree_link, *pbusy_list;
1787 struct phys_addr *pasync_sge;
1788 unsigned int ring_id, num_entries;
1789 unsigned int host_write_num, doorbell_offset;
1790 unsigned int writables;
1794 phwi_ctrlr = phba->phwi_ctrlr;
1795 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr, ulp_num);
1796 num_entries = pasync_ctx->num_entries;
1799 writables = min(pasync_ctx->async_header.writables,
1800 pasync_ctx->async_header.free_entries);
1801 pfree_link = pasync_ctx->async_header.free_list.next;
1802 host_write_num = pasync_ctx->async_header.host_write_ptr;
1803 ring_id = phwi_ctrlr->default_pdu_hdr[ulp_num].id;
1804 doorbell_offset = phwi_ctrlr->default_pdu_hdr[ulp_num].
1807 writables = min(pasync_ctx->async_data.writables,
1808 pasync_ctx->async_data.free_entries);
1809 pfree_link = pasync_ctx->async_data.free_list.next;
1810 host_write_num = pasync_ctx->async_data.host_write_ptr;
1811 ring_id = phwi_ctrlr->default_pdu_data[ulp_num].id;
1812 doorbell_offset = phwi_ctrlr->default_pdu_data[ulp_num].
1816 writables = (writables / 8) * 8;
1818 for (i = 0; i < writables; i++) {
1820 hwi_get_async_busy_list(pasync_ctx, is_header,
1823 list_entry(pfree_link, struct async_pdu_handle,
1825 WARN_ON(!pasync_handle);
1826 pasync_handle->consumed = 0;
1828 pfree_link = pfree_link->next;
1830 pasync_sge = hwi_get_ring_address(pasync_ctx,
1831 is_header, host_write_num);
1833 pasync_sge->hi = pasync_handle->pa.u.a32.address_lo;
1834 pasync_sge->lo = pasync_handle->pa.u.a32.address_hi;
1836 list_move(&pasync_handle->link, pbusy_list);
1839 host_write_num = host_write_num % num_entries;
1843 pasync_ctx->async_header.host_write_ptr =
1845 pasync_ctx->async_header.free_entries -= writables;
1846 pasync_ctx->async_header.writables -= writables;
1847 pasync_ctx->async_header.busy_entries += writables;
1849 pasync_ctx->async_data.host_write_ptr = host_write_num;
1850 pasync_ctx->async_data.free_entries -= writables;
1851 pasync_ctx->async_data.writables -= writables;
1852 pasync_ctx->async_data.busy_entries += writables;
1855 doorbell |= ring_id & DB_DEF_PDU_RING_ID_MASK;
1856 doorbell |= 1 << DB_DEF_PDU_REARM_SHIFT;
1857 doorbell |= 0 << DB_DEF_PDU_EVENT_SHIFT;
1858 doorbell |= (writables & DB_DEF_PDU_CQPROC_MASK)
1859 << DB_DEF_PDU_CQPROC_SHIFT;
1861 iowrite32(doorbell, phba->db_va + doorbell_offset);
1865 static void hwi_flush_default_pdu_buffer(struct beiscsi_hba *phba,
1866 struct beiscsi_conn *beiscsi_conn,
1867 struct i_t_dpdu_cqe *pdpdu_cqe)
1869 struct hwi_controller *phwi_ctrlr;
1870 struct hwi_async_pdu_context *pasync_ctx;
1871 struct async_pdu_handle *pasync_handle = NULL;
1872 unsigned int cq_index = -1;
1873 uint16_t cri_index = BE_GET_CRI_FROM_CID(
1874 beiscsi_conn->beiscsi_conn_cid);
1876 phwi_ctrlr = phba->phwi_ctrlr;
1877 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr,
1878 BEISCSI_GET_ULP_FROM_CRI(phwi_ctrlr,
1881 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1882 pdpdu_cqe, &cq_index);
1883 BUG_ON(pasync_handle->is_header != 0);
1884 if (pasync_handle->consumed == 0)
1885 hwi_update_async_writables(phba, pasync_ctx,
1886 pasync_handle->is_header, cq_index);
1888 hwi_free_async_msg(phba, pasync_ctx, pasync_handle->cri);
1889 hwi_post_async_buffers(phba, pasync_handle->is_header,
1890 BEISCSI_GET_ULP_FROM_CRI(phwi_ctrlr,
1895 hwi_fwd_async_msg(struct beiscsi_conn *beiscsi_conn,
1896 struct beiscsi_hba *phba,
1897 struct hwi_async_pdu_context *pasync_ctx, unsigned short cri)
1899 struct list_head *plist;
1900 struct async_pdu_handle *pasync_handle;
1902 unsigned int hdr_len = 0, buf_len = 0;
1903 unsigned int status, index = 0, offset = 0;
1904 void *pfirst_buffer = NULL;
1905 unsigned int num_buf = 0;
1907 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1909 list_for_each_entry(pasync_handle, plist, link) {
1911 phdr = pasync_handle->pbuffer;
1912 hdr_len = pasync_handle->buffer_len;
1914 buf_len = pasync_handle->buffer_len;
1916 pfirst_buffer = pasync_handle->pbuffer;
1919 memcpy(pfirst_buffer + offset,
1920 pasync_handle->pbuffer, buf_len);
1926 status = beiscsi_process_async_pdu(beiscsi_conn, phba,
1927 phdr, hdr_len, pfirst_buffer,
1930 hwi_free_async_msg(phba, pasync_ctx, cri);
1935 hwi_gather_async_pdu(struct beiscsi_conn *beiscsi_conn,
1936 struct beiscsi_hba *phba,
1937 struct async_pdu_handle *pasync_handle)
1939 struct hwi_async_pdu_context *pasync_ctx;
1940 struct hwi_controller *phwi_ctrlr;
1941 unsigned int bytes_needed = 0, status = 0;
1942 unsigned short cri = pasync_handle->cri;
1943 struct pdu_base *ppdu;
1945 phwi_ctrlr = phba->phwi_ctrlr;
1946 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr,
1947 BEISCSI_GET_ULP_FROM_CRI(phwi_ctrlr,
1948 BE_GET_CRI_FROM_CID(beiscsi_conn->
1949 beiscsi_conn_cid)));
1951 list_del(&pasync_handle->link);
1952 if (pasync_handle->is_header) {
1953 pasync_ctx->async_header.busy_entries--;
1954 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1955 hwi_free_async_msg(phba, pasync_ctx, cri);
1959 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
1960 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 1;
1961 pasync_ctx->async_entry[cri].wait_queue.hdr_len =
1962 (unsigned short)pasync_handle->buffer_len;
1963 list_add_tail(&pasync_handle->link,
1964 &pasync_ctx->async_entry[cri].wait_queue.list);
1966 ppdu = pasync_handle->pbuffer;
1967 bytes_needed = ((((ppdu->dw[offsetof(struct amap_pdu_base,
1968 data_len_hi) / 32] & PDUBASE_DATALENHI_MASK) << 8) &
1969 0xFFFF0000) | ((be16_to_cpu((ppdu->
1970 dw[offsetof(struct amap_pdu_base, data_len_lo) / 32]
1971 & PDUBASE_DATALENLO_MASK) >> 16)) & 0x0000FFFF));
1974 pasync_ctx->async_entry[cri].wait_queue.bytes_needed =
1977 if (bytes_needed == 0)
1978 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1982 pasync_ctx->async_data.busy_entries--;
1983 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1984 list_add_tail(&pasync_handle->link,
1985 &pasync_ctx->async_entry[cri].wait_queue.
1987 pasync_ctx->async_entry[cri].wait_queue.
1989 (unsigned short)pasync_handle->buffer_len;
1991 if (pasync_ctx->async_entry[cri].wait_queue.
1993 pasync_ctx->async_entry[cri].wait_queue.
1995 status = hwi_fwd_async_msg(beiscsi_conn, phba,
2002 static void hwi_process_default_pdu_ring(struct beiscsi_conn *beiscsi_conn,
2003 struct beiscsi_hba *phba,
2004 struct i_t_dpdu_cqe *pdpdu_cqe)
2006 struct hwi_controller *phwi_ctrlr;
2007 struct hwi_async_pdu_context *pasync_ctx;
2008 struct async_pdu_handle *pasync_handle = NULL;
2009 unsigned int cq_index = -1;
2010 uint16_t cri_index = BE_GET_CRI_FROM_CID(
2011 beiscsi_conn->beiscsi_conn_cid);
2013 phwi_ctrlr = phba->phwi_ctrlr;
2014 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr,
2015 BEISCSI_GET_ULP_FROM_CRI(phwi_ctrlr,
2018 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
2019 pdpdu_cqe, &cq_index);
2021 if (pasync_handle->consumed == 0)
2022 hwi_update_async_writables(phba, pasync_ctx,
2023 pasync_handle->is_header, cq_index);
2025 hwi_gather_async_pdu(beiscsi_conn, phba, pasync_handle);
2026 hwi_post_async_buffers(phba, pasync_handle->is_header,
2027 BEISCSI_GET_ULP_FROM_CRI(
2028 phwi_ctrlr, cri_index));
2031 static void beiscsi_process_mcc_isr(struct beiscsi_hba *phba)
2033 struct be_queue_info *mcc_cq;
2034 struct be_mcc_compl *mcc_compl;
2035 unsigned int num_processed = 0;
2037 mcc_cq = &phba->ctrl.mcc_obj.cq;
2038 mcc_compl = queue_tail_node(mcc_cq);
2039 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
2040 while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
2042 if (num_processed >= 32) {
2043 hwi_ring_cq_db(phba, mcc_cq->id,
2047 if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
2048 beiscsi_process_async_event(phba, mcc_compl);
2049 } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
2050 be_mcc_compl_process_isr(&phba->ctrl, mcc_compl);
2051 atomic_dec(&phba->ctrl.mcc_obj.q.used);
2054 mcc_compl->flags = 0;
2055 queue_tail_inc(mcc_cq);
2056 mcc_compl = queue_tail_node(mcc_cq);
2057 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
2061 if (num_processed > 0)
2062 hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1);
2067 * beiscsi_process_cq()- Process the Completion Queue
2068 * @pbe_eq: Event Q on which the Completion has come
2069 * @budget: Max number of events to processed
2072 * Number of Completion Entries processed.
2074 unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq, int budget)
2076 struct be_queue_info *cq;
2077 struct sol_cqe *sol;
2078 struct dmsg_cqe *dmsg;
2079 unsigned int total = 0;
2080 unsigned int num_processed = 0;
2081 unsigned short code = 0, cid = 0;
2082 uint16_t cri_index = 0;
2083 struct beiscsi_conn *beiscsi_conn;
2084 struct beiscsi_endpoint *beiscsi_ep;
2085 struct iscsi_endpoint *ep;
2086 struct beiscsi_hba *phba;
2089 sol = queue_tail_node(cq);
2090 phba = pbe_eq->phba;
2092 while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
2094 be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
2096 code = (sol->dw[offsetof(struct amap_sol_cqe, code) /
2097 32] & CQE_CODE_MASK);
2100 if (is_chip_be2_be3r(phba)) {
2101 cid = AMAP_GET_BITS(struct amap_sol_cqe, cid, sol);
2103 if ((code == DRIVERMSG_NOTIFY) ||
2104 (code == UNSOL_HDR_NOTIFY) ||
2105 (code == UNSOL_DATA_NOTIFY))
2106 cid = AMAP_GET_BITS(
2107 struct amap_i_t_dpdu_cqe_v2,
2110 cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
2114 cri_index = BE_GET_CRI_FROM_CID(cid);
2115 ep = phba->ep_array[cri_index];
2118 /* connection has already been freed
2119 * just move on to next one
2121 beiscsi_log(phba, KERN_WARNING,
2123 "BM_%d : proc cqe of disconn ep: cid %d\n",
2128 beiscsi_ep = ep->dd_data;
2129 beiscsi_conn = beiscsi_ep->conn;
2132 if (num_processed == 32) {
2133 hwi_ring_cq_db(phba, cq->id, 32, 0);
2139 case SOL_CMD_COMPLETE:
2140 hwi_complete_cmd(beiscsi_conn, phba, sol);
2142 case DRIVERMSG_NOTIFY:
2143 beiscsi_log(phba, KERN_INFO,
2144 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
2145 "BM_%d : Received %s[%d] on CID : %d\n",
2146 cqe_desc[code], code, cid);
2148 dmsg = (struct dmsg_cqe *)sol;
2149 hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
2151 case UNSOL_HDR_NOTIFY:
2152 beiscsi_log(phba, KERN_INFO,
2153 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
2154 "BM_%d : Received %s[%d] on CID : %d\n",
2155 cqe_desc[code], code, cid);
2157 spin_lock_bh(&phba->async_pdu_lock);
2158 hwi_process_default_pdu_ring(beiscsi_conn, phba,
2159 (struct i_t_dpdu_cqe *)sol);
2160 spin_unlock_bh(&phba->async_pdu_lock);
2162 case UNSOL_DATA_NOTIFY:
2163 beiscsi_log(phba, KERN_INFO,
2164 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
2165 "BM_%d : Received %s[%d] on CID : %d\n",
2166 cqe_desc[code], code, cid);
2168 spin_lock_bh(&phba->async_pdu_lock);
2169 hwi_process_default_pdu_ring(beiscsi_conn, phba,
2170 (struct i_t_dpdu_cqe *)sol);
2171 spin_unlock_bh(&phba->async_pdu_lock);
2173 case CXN_INVALIDATE_INDEX_NOTIFY:
2174 case CMD_INVALIDATED_NOTIFY:
2175 case CXN_INVALIDATE_NOTIFY:
2176 beiscsi_log(phba, KERN_ERR,
2177 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
2178 "BM_%d : Ignoring %s[%d] on CID : %d\n",
2179 cqe_desc[code], code, cid);
2181 case CXN_KILLED_HDR_DIGEST_ERR:
2182 case SOL_CMD_KILLED_DATA_DIGEST_ERR:
2183 beiscsi_log(phba, KERN_ERR,
2184 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
2185 "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
2186 cqe_desc[code], code, cid);
2188 case CMD_KILLED_INVALID_STATSN_RCVD:
2189 case CMD_KILLED_INVALID_R2T_RCVD:
2190 case CMD_CXN_KILLED_LUN_INVALID:
2191 case CMD_CXN_KILLED_ICD_INVALID:
2192 case CMD_CXN_KILLED_ITT_INVALID:
2193 case CMD_CXN_KILLED_SEQ_OUTOFORDER:
2194 case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
2195 beiscsi_log(phba, KERN_ERR,
2196 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
2197 "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
2198 cqe_desc[code], code, cid);
2200 case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
2201 beiscsi_log(phba, KERN_ERR,
2202 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
2203 "BM_%d : Dropping %s[%d] on DPDU ring on CID : %d\n",
2204 cqe_desc[code], code, cid);
2205 spin_lock_bh(&phba->async_pdu_lock);
2206 hwi_flush_default_pdu_buffer(phba, beiscsi_conn,
2207 (struct i_t_dpdu_cqe *) sol);
2208 spin_unlock_bh(&phba->async_pdu_lock);
2210 case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
2211 case CXN_KILLED_BURST_LEN_MISMATCH:
2212 case CXN_KILLED_AHS_RCVD:
2213 case CXN_KILLED_UNKNOWN_HDR:
2214 case CXN_KILLED_STALE_ITT_TTT_RCVD:
2215 case CXN_KILLED_INVALID_ITT_TTT_RCVD:
2216 case CXN_KILLED_TIMED_OUT:
2217 case CXN_KILLED_FIN_RCVD:
2218 case CXN_KILLED_RST_SENT:
2219 case CXN_KILLED_RST_RCVD:
2220 case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
2221 case CXN_KILLED_BAD_WRB_INDEX_ERROR:
2222 case CXN_KILLED_OVER_RUN_RESIDUAL:
2223 case CXN_KILLED_UNDER_RUN_RESIDUAL:
2224 case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
2225 beiscsi_log(phba, KERN_ERR,
2226 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
2227 "BM_%d : Event %s[%d] received on CID : %d\n",
2228 cqe_desc[code], code, cid);
2230 iscsi_conn_failure(beiscsi_conn->conn,
2231 ISCSI_ERR_CONN_FAILED);
2234 beiscsi_log(phba, KERN_ERR,
2235 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
2236 "BM_%d : Invalid CQE Event Received Code : %d"
2243 AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
2245 sol = queue_tail_node(cq);
2247 if (total == budget)
2251 hwi_ring_cq_db(phba, cq->id, num_processed, 1);
2255 void beiscsi_process_all_cqs(struct work_struct *work)
2257 unsigned long flags;
2258 struct hwi_controller *phwi_ctrlr;
2259 struct hwi_context_memory *phwi_context;
2260 struct beiscsi_hba *phba;
2261 struct be_eq_obj *pbe_eq =
2262 container_of(work, struct be_eq_obj, work_cqs);
2264 phba = pbe_eq->phba;
2265 phwi_ctrlr = phba->phwi_ctrlr;
2266 phwi_context = phwi_ctrlr->phwi_ctxt;
2268 if (pbe_eq->todo_mcc_cq) {
2269 spin_lock_irqsave(&phba->isr_lock, flags);
2270 pbe_eq->todo_mcc_cq = false;
2271 spin_unlock_irqrestore(&phba->isr_lock, flags);
2272 beiscsi_process_mcc_isr(phba);
2275 if (pbe_eq->todo_cq) {
2276 spin_lock_irqsave(&phba->isr_lock, flags);
2277 pbe_eq->todo_cq = false;
2278 spin_unlock_irqrestore(&phba->isr_lock, flags);
2279 beiscsi_process_cq(pbe_eq, BE2_MAX_NUM_CQ_PROC);
2282 /* rearm EQ for further interrupts */
2283 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
2286 static int be_iopoll(struct irq_poll *iop, int budget)
2288 unsigned int ret, num_eq_processed;
2289 struct beiscsi_hba *phba;
2290 struct be_eq_obj *pbe_eq;
2291 struct be_eq_entry *eqe = NULL;
2292 struct be_queue_info *eq;
2294 num_eq_processed = 0;
2295 pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
2296 phba = pbe_eq->phba;
2298 eqe = queue_tail_node(eq);
2300 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32] &
2302 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
2304 eqe = queue_tail_node(eq);
2308 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 0, 1);
2310 ret = beiscsi_process_cq(pbe_eq, budget);
2311 pbe_eq->cq_count += ret;
2313 irq_poll_complete(iop);
2314 beiscsi_log(phba, KERN_INFO,
2315 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
2316 "BM_%d : rearm pbe_eq->q.id =%d ret %d\n",
2318 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
2324 hwi_write_sgl_v2(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2325 unsigned int num_sg, struct beiscsi_io_task *io_task)
2327 struct iscsi_sge *psgl;
2328 unsigned int sg_len, index;
2329 unsigned int sge_len = 0;
2330 unsigned long long addr;
2331 struct scatterlist *l_sg;
2332 unsigned int offset;
2334 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_lo, pwrb,
2335 io_task->bhs_pa.u.a32.address_lo);
2336 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_hi, pwrb,
2337 io_task->bhs_pa.u.a32.address_hi);
2340 for (index = 0; (index < num_sg) && (index < 2); index++,
2343 sg_len = sg_dma_len(sg);
2344 addr = (u64) sg_dma_address(sg);
2345 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2347 lower_32_bits(addr));
2348 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2350 upper_32_bits(addr));
2351 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2356 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_r2t_offset,
2358 sg_len = sg_dma_len(sg);
2359 addr = (u64) sg_dma_address(sg);
2360 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2362 lower_32_bits(addr));
2363 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2365 upper_32_bits(addr));
2366 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2371 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2372 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2374 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2376 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2377 io_task->bhs_pa.u.a32.address_hi);
2378 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2379 io_task->bhs_pa.u.a32.address_lo);
2382 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2384 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2386 } else if (num_sg == 2) {
2387 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2389 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2392 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2394 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2402 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
2403 sg_len = sg_dma_len(sg);
2404 addr = (u64) sg_dma_address(sg);
2405 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2406 lower_32_bits(addr));
2407 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2408 upper_32_bits(addr));
2409 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2410 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2411 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2415 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2419 hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2420 unsigned int num_sg, struct beiscsi_io_task *io_task)
2422 struct iscsi_sge *psgl;
2423 unsigned int sg_len, index;
2424 unsigned int sge_len = 0;
2425 unsigned long long addr;
2426 struct scatterlist *l_sg;
2427 unsigned int offset;
2429 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2430 io_task->bhs_pa.u.a32.address_lo);
2431 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2432 io_task->bhs_pa.u.a32.address_hi);
2435 for (index = 0; (index < num_sg) && (index < 2); index++,
2438 sg_len = sg_dma_len(sg);
2439 addr = (u64) sg_dma_address(sg);
2440 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
2441 ((u32)(addr & 0xFFFFFFFF)));
2442 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
2443 ((u32)(addr >> 32)));
2444 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2448 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
2450 sg_len = sg_dma_len(sg);
2451 addr = (u64) sg_dma_address(sg);
2452 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
2453 ((u32)(addr & 0xFFFFFFFF)));
2454 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
2455 ((u32)(addr >> 32)));
2456 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
2460 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2461 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2463 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2465 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2466 io_task->bhs_pa.u.a32.address_hi);
2467 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2468 io_task->bhs_pa.u.a32.address_lo);
2471 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2473 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2475 } else if (num_sg == 2) {
2476 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2478 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2481 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2483 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2490 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
2491 sg_len = sg_dma_len(sg);
2492 addr = (u64) sg_dma_address(sg);
2493 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2494 (addr & 0xFFFFFFFF));
2495 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2497 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2498 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2499 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2503 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2507 * hwi_write_buffer()- Populate the WRB with task info
2508 * @pwrb: ptr to the WRB entry
2509 * @task: iscsi task which is to be executed
2511 static int hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
2513 struct iscsi_sge *psgl;
2514 struct beiscsi_io_task *io_task = task->dd_data;
2515 struct beiscsi_conn *beiscsi_conn = io_task->conn;
2516 struct beiscsi_hba *phba = beiscsi_conn->phba;
2517 uint8_t dsp_value = 0;
2519 io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
2520 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2521 io_task->bhs_pa.u.a32.address_lo);
2522 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2523 io_task->bhs_pa.u.a32.address_hi);
2527 /* Check for the data_count */
2528 dsp_value = (task->data_count) ? 1 : 0;
2530 if (is_chip_be2_be3r(phba))
2531 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp,
2534 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp,
2537 /* Map addr only if there is data_count */
2539 io_task->mtask_addr = pci_map_single(phba->pcidev,
2543 if (pci_dma_mapping_error(phba->pcidev,
2544 io_task->mtask_addr))
2546 io_task->mtask_data_count = task->data_count;
2548 io_task->mtask_addr = 0;
2550 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
2551 lower_32_bits(io_task->mtask_addr));
2552 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
2553 upper_32_bits(io_task->mtask_addr));
2554 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2557 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
2559 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
2560 io_task->mtask_addr = 0;
2563 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2565 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
2567 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2568 io_task->bhs_pa.u.a32.address_hi);
2569 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2570 io_task->bhs_pa.u.a32.address_lo);
2573 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
2574 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
2575 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
2576 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
2577 AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
2578 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2582 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2583 lower_32_bits(io_task->mtask_addr));
2584 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2585 upper_32_bits(io_task->mtask_addr));
2587 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
2589 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2594 * beiscsi_find_mem_req()- Find mem needed
2595 * @phba: ptr to HBA struct
2597 static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
2599 uint8_t mem_descr_index, ulp_num;
2600 unsigned int num_cq_pages, num_async_pdu_buf_pages;
2601 unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
2602 unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
2604 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
2605 sizeof(struct sol_cqe));
2607 phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
2609 phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
2610 BE_ISCSI_PDU_HEADER_SIZE;
2611 phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
2612 sizeof(struct hwi_context_memory);
2615 phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
2616 * (phba->params.wrbs_per_cxn)
2617 * phba->params.cxns_per_ctrl;
2618 wrb_sz_per_cxn = sizeof(struct wrb_handle) *
2619 (phba->params.wrbs_per_cxn);
2620 phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
2621 phba->params.cxns_per_ctrl);
2623 phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
2624 phba->params.icds_per_ctrl;
2625 phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
2626 phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
2627 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
2628 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
2630 num_async_pdu_buf_sgl_pages =
2631 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2633 sizeof(struct phys_addr));
2635 num_async_pdu_buf_pages =
2636 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2638 phba->params.defpdu_hdr_sz);
2640 num_async_pdu_data_pages =
2641 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2643 phba->params.defpdu_data_sz);
2645 num_async_pdu_data_sgl_pages =
2646 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2648 sizeof(struct phys_addr));
2650 mem_descr_index = (HWI_MEM_TEMPLATE_HDR_ULP0 +
2651 (ulp_num * MEM_DESCR_OFFSET));
2652 phba->mem_req[mem_descr_index] =
2653 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
2654 BEISCSI_TEMPLATE_HDR_PER_CXN_SIZE;
2656 mem_descr_index = (HWI_MEM_ASYNC_HEADER_BUF_ULP0 +
2657 (ulp_num * MEM_DESCR_OFFSET));
2658 phba->mem_req[mem_descr_index] =
2659 num_async_pdu_buf_pages *
2662 mem_descr_index = (HWI_MEM_ASYNC_DATA_BUF_ULP0 +
2663 (ulp_num * MEM_DESCR_OFFSET));
2664 phba->mem_req[mem_descr_index] =
2665 num_async_pdu_data_pages *
2668 mem_descr_index = (HWI_MEM_ASYNC_HEADER_RING_ULP0 +
2669 (ulp_num * MEM_DESCR_OFFSET));
2670 phba->mem_req[mem_descr_index] =
2671 num_async_pdu_buf_sgl_pages *
2674 mem_descr_index = (HWI_MEM_ASYNC_DATA_RING_ULP0 +
2675 (ulp_num * MEM_DESCR_OFFSET));
2676 phba->mem_req[mem_descr_index] =
2677 num_async_pdu_data_sgl_pages *
2680 mem_descr_index = (HWI_MEM_ASYNC_HEADER_HANDLE_ULP0 +
2681 (ulp_num * MEM_DESCR_OFFSET));
2682 phba->mem_req[mem_descr_index] =
2683 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
2684 sizeof(struct async_pdu_handle);
2686 mem_descr_index = (HWI_MEM_ASYNC_DATA_HANDLE_ULP0 +
2687 (ulp_num * MEM_DESCR_OFFSET));
2688 phba->mem_req[mem_descr_index] =
2689 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
2690 sizeof(struct async_pdu_handle);
2692 mem_descr_index = (HWI_MEM_ASYNC_PDU_CONTEXT_ULP0 +
2693 (ulp_num * MEM_DESCR_OFFSET));
2694 phba->mem_req[mem_descr_index] =
2695 sizeof(struct hwi_async_pdu_context) +
2696 (BEISCSI_GET_CID_COUNT(phba, ulp_num) *
2697 sizeof(struct hwi_async_entry));
2702 static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
2705 struct hwi_controller *phwi_ctrlr;
2706 struct be_mem_descriptor *mem_descr;
2707 struct mem_array *mem_arr, *mem_arr_orig;
2708 unsigned int i, j, alloc_size, curr_alloc_size;
2710 phba->phwi_ctrlr = kzalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
2711 if (!phba->phwi_ctrlr)
2714 /* Allocate memory for wrb_context */
2715 phwi_ctrlr = phba->phwi_ctrlr;
2716 phwi_ctrlr->wrb_context = kzalloc(sizeof(struct hwi_wrb_context) *
2717 phba->params.cxns_per_ctrl,
2719 if (!phwi_ctrlr->wrb_context)
2722 phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
2724 if (!phba->init_mem) {
2725 kfree(phwi_ctrlr->wrb_context);
2726 kfree(phba->phwi_ctrlr);
2730 mem_arr_orig = kmalloc(sizeof(*mem_arr_orig) * BEISCSI_MAX_FRAGS_INIT,
2732 if (!mem_arr_orig) {
2733 kfree(phba->init_mem);
2734 kfree(phwi_ctrlr->wrb_context);
2735 kfree(phba->phwi_ctrlr);
2739 mem_descr = phba->init_mem;
2740 for (i = 0; i < SE_MEM_MAX; i++) {
2741 if (!phba->mem_req[i]) {
2742 mem_descr->mem_array = NULL;
2748 mem_arr = mem_arr_orig;
2749 alloc_size = phba->mem_req[i];
2750 memset(mem_arr, 0, sizeof(struct mem_array) *
2751 BEISCSI_MAX_FRAGS_INIT);
2752 curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
2754 mem_arr->virtual_address = pci_alloc_consistent(
2758 if (!mem_arr->virtual_address) {
2759 if (curr_alloc_size <= BE_MIN_MEM_SIZE)
2761 if (curr_alloc_size -
2762 rounddown_pow_of_two(curr_alloc_size))
2763 curr_alloc_size = rounddown_pow_of_two
2766 curr_alloc_size = curr_alloc_size / 2;
2768 mem_arr->bus_address.u.
2769 a64.address = (__u64) bus_add;
2770 mem_arr->size = curr_alloc_size;
2771 alloc_size -= curr_alloc_size;
2772 curr_alloc_size = min(be_max_phys_size *
2777 } while (alloc_size);
2778 mem_descr->num_elements = j;
2779 mem_descr->size_in_bytes = phba->mem_req[i];
2780 mem_descr->mem_array = kmalloc(sizeof(*mem_arr) * j,
2782 if (!mem_descr->mem_array)
2785 memcpy(mem_descr->mem_array, mem_arr_orig,
2786 sizeof(struct mem_array) * j);
2789 kfree(mem_arr_orig);
2792 mem_descr->num_elements = j;
2793 while ((i) || (j)) {
2794 for (j = mem_descr->num_elements; j > 0; j--) {
2795 pci_free_consistent(phba->pcidev,
2796 mem_descr->mem_array[j - 1].size,
2797 mem_descr->mem_array[j - 1].
2799 (unsigned long)mem_descr->
2801 bus_address.u.a64.address);
2805 kfree(mem_descr->mem_array);
2809 kfree(mem_arr_orig);
2810 kfree(phba->init_mem);
2811 kfree(phba->phwi_ctrlr->wrb_context);
2812 kfree(phba->phwi_ctrlr);
2816 static int beiscsi_get_memory(struct beiscsi_hba *phba)
2818 beiscsi_find_mem_req(phba);
2819 return beiscsi_alloc_mem(phba);
2822 static void iscsi_init_global_templates(struct beiscsi_hba *phba)
2824 struct pdu_data_out *pdata_out;
2825 struct pdu_nop_out *pnop_out;
2826 struct be_mem_descriptor *mem_descr;
2828 mem_descr = phba->init_mem;
2829 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
2831 (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
2832 memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2834 AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
2838 (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
2839 virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
2841 memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2842 AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
2843 AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
2844 AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
2847 static int beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
2849 struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
2850 struct hwi_context_memory *phwi_ctxt;
2851 struct wrb_handle *pwrb_handle = NULL;
2852 struct hwi_controller *phwi_ctrlr;
2853 struct hwi_wrb_context *pwrb_context;
2854 struct iscsi_wrb *pwrb = NULL;
2855 unsigned int num_cxn_wrbh = 0;
2856 unsigned int num_cxn_wrb = 0, j, idx = 0, index;
2858 mem_descr_wrbh = phba->init_mem;
2859 mem_descr_wrbh += HWI_MEM_WRBH;
2861 mem_descr_wrb = phba->init_mem;
2862 mem_descr_wrb += HWI_MEM_WRB;
2863 phwi_ctrlr = phba->phwi_ctrlr;
2865 /* Allocate memory for WRBQ */
2866 phwi_ctxt = phwi_ctrlr->phwi_ctxt;
2867 phwi_ctxt->be_wrbq = kzalloc(sizeof(struct be_queue_info) *
2868 phba->params.cxns_per_ctrl,
2870 if (!phwi_ctxt->be_wrbq) {
2871 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2872 "BM_%d : WRBQ Mem Alloc Failed\n");
2876 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
2877 pwrb_context = &phwi_ctrlr->wrb_context[index];
2878 pwrb_context->pwrb_handle_base =
2879 kzalloc(sizeof(struct wrb_handle *) *
2880 phba->params.wrbs_per_cxn, GFP_KERNEL);
2881 if (!pwrb_context->pwrb_handle_base) {
2882 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2883 "BM_%d : Mem Alloc Failed. Failing to load\n");
2884 goto init_wrb_hndl_failed;
2886 pwrb_context->pwrb_handle_basestd =
2887 kzalloc(sizeof(struct wrb_handle *) *
2888 phba->params.wrbs_per_cxn, GFP_KERNEL);
2889 if (!pwrb_context->pwrb_handle_basestd) {
2890 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2891 "BM_%d : Mem Alloc Failed. Failing to load\n");
2892 goto init_wrb_hndl_failed;
2894 if (!num_cxn_wrbh) {
2896 mem_descr_wrbh->mem_array[idx].virtual_address;
2897 num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
2898 ((sizeof(struct wrb_handle)) *
2899 phba->params.wrbs_per_cxn));
2902 pwrb_context->alloc_index = 0;
2903 pwrb_context->wrb_handles_available = 0;
2904 pwrb_context->free_index = 0;
2907 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2908 pwrb_context->pwrb_handle_base[j] = pwrb_handle;
2909 pwrb_context->pwrb_handle_basestd[j] =
2911 pwrb_context->wrb_handles_available++;
2912 pwrb_handle->wrb_index = j;
2919 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
2920 pwrb_context = &phwi_ctrlr->wrb_context[index];
2922 pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
2923 num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
2924 ((sizeof(struct iscsi_wrb) *
2925 phba->params.wrbs_per_cxn));
2930 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2931 pwrb_handle = pwrb_context->pwrb_handle_base[j];
2932 pwrb_handle->pwrb = pwrb;
2939 init_wrb_hndl_failed:
2940 for (j = index; j > 0; j--) {
2941 pwrb_context = &phwi_ctrlr->wrb_context[j];
2942 kfree(pwrb_context->pwrb_handle_base);
2943 kfree(pwrb_context->pwrb_handle_basestd);
2948 static int hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
2951 struct hwi_controller *phwi_ctrlr;
2952 struct hba_parameters *p = &phba->params;
2953 struct hwi_async_pdu_context *pasync_ctx;
2954 struct async_pdu_handle *pasync_header_h, *pasync_data_h;
2955 unsigned int index, idx, num_per_mem, num_async_data;
2956 struct be_mem_descriptor *mem_descr;
2958 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
2959 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
2961 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2962 mem_descr += (HWI_MEM_ASYNC_PDU_CONTEXT_ULP0 +
2963 (ulp_num * MEM_DESCR_OFFSET));
2965 phwi_ctrlr = phba->phwi_ctrlr;
2966 phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num] =
2967 (struct hwi_async_pdu_context *)
2968 mem_descr->mem_array[0].virtual_address;
2970 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num];
2971 memset(pasync_ctx, 0, sizeof(*pasync_ctx));
2973 pasync_ctx->async_entry =
2974 (struct hwi_async_entry *)
2975 ((long unsigned int)pasync_ctx +
2976 sizeof(struct hwi_async_pdu_context));
2978 pasync_ctx->num_entries = BEISCSI_GET_CID_COUNT(phba,
2980 pasync_ctx->buffer_size = p->defpdu_hdr_sz;
2982 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2983 mem_descr += HWI_MEM_ASYNC_HEADER_BUF_ULP0 +
2984 (ulp_num * MEM_DESCR_OFFSET);
2985 if (mem_descr->mem_array[0].virtual_address) {
2986 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2987 "BM_%d : hwi_init_async_pdu_ctx"
2988 " HWI_MEM_ASYNC_HEADER_BUF_ULP%d va=%p\n",
2990 mem_descr->mem_array[0].
2993 beiscsi_log(phba, KERN_WARNING,
2995 "BM_%d : No Virtual address for ULP : %d\n",
2998 pasync_ctx->async_header.va_base =
2999 mem_descr->mem_array[0].virtual_address;
3001 pasync_ctx->async_header.pa_base.u.a64.address =
3002 mem_descr->mem_array[0].
3003 bus_address.u.a64.address;
3005 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
3006 mem_descr += HWI_MEM_ASYNC_HEADER_RING_ULP0 +
3007 (ulp_num * MEM_DESCR_OFFSET);
3008 if (mem_descr->mem_array[0].virtual_address) {
3009 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3010 "BM_%d : hwi_init_async_pdu_ctx"
3011 " HWI_MEM_ASYNC_HEADER_RING_ULP%d va=%p\n",
3013 mem_descr->mem_array[0].
3016 beiscsi_log(phba, KERN_WARNING,
3018 "BM_%d : No Virtual address for ULP : %d\n",
3021 pasync_ctx->async_header.ring_base =
3022 mem_descr->mem_array[0].virtual_address;
3024 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
3025 mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE_ULP0 +
3026 (ulp_num * MEM_DESCR_OFFSET);
3027 if (mem_descr->mem_array[0].virtual_address) {
3028 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3029 "BM_%d : hwi_init_async_pdu_ctx"
3030 " HWI_MEM_ASYNC_HEADER_HANDLE_ULP%d va=%p\n",
3032 mem_descr->mem_array[0].
3035 beiscsi_log(phba, KERN_WARNING,
3037 "BM_%d : No Virtual address for ULP : %d\n",
3040 pasync_ctx->async_header.handle_base =
3041 mem_descr->mem_array[0].virtual_address;
3042 pasync_ctx->async_header.writables = 0;
3043 INIT_LIST_HEAD(&pasync_ctx->async_header.free_list);
3045 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
3046 mem_descr += HWI_MEM_ASYNC_DATA_RING_ULP0 +
3047 (ulp_num * MEM_DESCR_OFFSET);
3048 if (mem_descr->mem_array[0].virtual_address) {
3049 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3050 "BM_%d : hwi_init_async_pdu_ctx"
3051 " HWI_MEM_ASYNC_DATA_RING_ULP%d va=%p\n",
3053 mem_descr->mem_array[0].
3056 beiscsi_log(phba, KERN_WARNING,
3058 "BM_%d : No Virtual address for ULP : %d\n",
3061 pasync_ctx->async_data.ring_base =
3062 mem_descr->mem_array[0].virtual_address;
3064 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
3065 mem_descr += HWI_MEM_ASYNC_DATA_HANDLE_ULP0 +
3066 (ulp_num * MEM_DESCR_OFFSET);
3067 if (!mem_descr->mem_array[0].virtual_address)
3068 beiscsi_log(phba, KERN_WARNING,
3070 "BM_%d : No Virtual address for ULP : %d\n",
3073 pasync_ctx->async_data.handle_base =
3074 mem_descr->mem_array[0].virtual_address;
3075 pasync_ctx->async_data.writables = 0;
3076 INIT_LIST_HEAD(&pasync_ctx->async_data.free_list);
3079 (struct async_pdu_handle *)
3080 pasync_ctx->async_header.handle_base;
3082 (struct async_pdu_handle *)
3083 pasync_ctx->async_data.handle_base;
3085 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
3086 mem_descr += HWI_MEM_ASYNC_DATA_BUF_ULP0 +
3087 (ulp_num * MEM_DESCR_OFFSET);
3088 if (mem_descr->mem_array[0].virtual_address) {
3089 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3090 "BM_%d : hwi_init_async_pdu_ctx"
3091 " HWI_MEM_ASYNC_DATA_BUF_ULP%d va=%p\n",
3093 mem_descr->mem_array[0].
3096 beiscsi_log(phba, KERN_WARNING,
3098 "BM_%d : No Virtual address for ULP : %d\n",
3102 pasync_ctx->async_data.va_base =
3103 mem_descr->mem_array[idx].virtual_address;
3104 pasync_ctx->async_data.pa_base.u.a64.address =
3105 mem_descr->mem_array[idx].
3106 bus_address.u.a64.address;
3108 num_async_data = ((mem_descr->mem_array[idx].size) /
3109 phba->params.defpdu_data_sz);
3112 for (index = 0; index < BEISCSI_GET_CID_COUNT
3113 (phba, ulp_num); index++) {
3114 pasync_header_h->cri = -1;
3115 pasync_header_h->index = (char)index;
3116 INIT_LIST_HEAD(&pasync_header_h->link);
3117 pasync_header_h->pbuffer =
3118 (void *)((unsigned long)
3120 async_header.va_base) +
3121 (p->defpdu_hdr_sz * index));
3123 pasync_header_h->pa.u.a64.address =
3124 pasync_ctx->async_header.pa_base.u.a64.
3125 address + (p->defpdu_hdr_sz * index);
3127 list_add_tail(&pasync_header_h->link,
3128 &pasync_ctx->async_header.
3131 pasync_ctx->async_header.free_entries++;
3132 pasync_ctx->async_header.writables++;
3134 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
3136 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
3138 pasync_data_h->cri = -1;
3139 pasync_data_h->index = (char)index;
3140 INIT_LIST_HEAD(&pasync_data_h->link);
3142 if (!num_async_data) {
3145 pasync_ctx->async_data.va_base =
3146 mem_descr->mem_array[idx].
3148 pasync_ctx->async_data.pa_base.u.
3150 mem_descr->mem_array[idx].
3151 bus_address.u.a64.address;
3153 ((mem_descr->mem_array[idx].
3155 phba->params.defpdu_data_sz);
3157 pasync_data_h->pbuffer =
3158 (void *)((unsigned long)
3159 (pasync_ctx->async_data.va_base) +
3160 (p->defpdu_data_sz * num_per_mem));
3162 pasync_data_h->pa.u.a64.address =
3163 pasync_ctx->async_data.pa_base.u.a64.
3164 address + (p->defpdu_data_sz *
3169 list_add_tail(&pasync_data_h->link,
3170 &pasync_ctx->async_data.
3173 pasync_ctx->async_data.free_entries++;
3174 pasync_ctx->async_data.writables++;
3176 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
3180 pasync_ctx->async_header.host_write_ptr = 0;
3181 pasync_ctx->async_header.ep_read_ptr = -1;
3182 pasync_ctx->async_data.host_write_ptr = 0;
3183 pasync_ctx->async_data.ep_read_ptr = -1;
3191 be_sgl_create_contiguous(void *virtual_address,
3192 u64 physical_address, u32 length,
3193 struct be_dma_mem *sgl)
3195 WARN_ON(!virtual_address);
3196 WARN_ON(!physical_address);
3200 sgl->va = virtual_address;
3201 sgl->dma = (unsigned long)physical_address;
3207 static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
3209 memset(sgl, 0, sizeof(*sgl));
3213 hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
3214 struct mem_array *pmem, struct be_dma_mem *sgl)
3217 be_sgl_destroy_contiguous(sgl);
3219 be_sgl_create_contiguous(pmem->virtual_address,
3220 pmem->bus_address.u.a64.address,
3225 hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
3226 struct mem_array *pmem, struct be_dma_mem *sgl)
3229 be_sgl_destroy_contiguous(sgl);
3231 be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
3232 pmem->bus_address.u.a64.address,
3236 static int be_fill_queue(struct be_queue_info *q,
3237 u16 len, u16 entry_size, void *vaddress)
3239 struct be_dma_mem *mem = &q->dma_mem;
3241 memset(q, 0, sizeof(*q));
3243 q->entry_size = entry_size;
3244 mem->size = len * entry_size;
3248 memset(mem->va, 0, mem->size);
3252 static int beiscsi_create_eqs(struct beiscsi_hba *phba,
3253 struct hwi_context_memory *phwi_context)
3255 unsigned int i, num_eq_pages;
3256 int ret = 0, eq_for_mcc;
3257 struct be_queue_info *eq;
3258 struct be_dma_mem *mem;
3262 num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries * \
3263 sizeof(struct be_eq_entry));
3265 if (phba->msix_enabled)
3269 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
3270 eq = &phwi_context->be_eq[i].q;
3272 phwi_context->be_eq[i].phba = phba;
3273 eq_vaddress = pci_alloc_consistent(phba->pcidev,
3274 num_eq_pages * PAGE_SIZE,
3277 goto create_eq_error;
3279 mem->va = eq_vaddress;
3280 ret = be_fill_queue(eq, phba->params.num_eq_entries,
3281 sizeof(struct be_eq_entry), eq_vaddress);
3283 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3284 "BM_%d : be_fill_queue Failed for EQ\n");
3285 goto create_eq_error;
3289 ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
3290 phwi_context->cur_eqd);
3292 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3293 "BM_%d : beiscsi_cmd_eq_create"
3295 goto create_eq_error;
3298 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3299 "BM_%d : eqid = %d\n",
3300 phwi_context->be_eq[i].q.id);
3304 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
3305 eq = &phwi_context->be_eq[i].q;
3308 pci_free_consistent(phba->pcidev, num_eq_pages
3315 static int beiscsi_create_cqs(struct beiscsi_hba *phba,
3316 struct hwi_context_memory *phwi_context)
3318 unsigned int i, num_cq_pages;
3320 struct be_queue_info *cq, *eq;
3321 struct be_dma_mem *mem;
3322 struct be_eq_obj *pbe_eq;
3326 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
3327 sizeof(struct sol_cqe));
3329 for (i = 0; i < phba->num_cpus; i++) {
3330 cq = &phwi_context->be_cq[i];
3331 eq = &phwi_context->be_eq[i].q;
3332 pbe_eq = &phwi_context->be_eq[i];
3334 pbe_eq->phba = phba;
3336 cq_vaddress = pci_alloc_consistent(phba->pcidev,
3337 num_cq_pages * PAGE_SIZE,
3340 goto create_cq_error;
3341 ret = be_fill_queue(cq, phba->params.num_cq_entries,
3342 sizeof(struct sol_cqe), cq_vaddress);
3344 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3345 "BM_%d : be_fill_queue Failed "
3347 goto create_cq_error;
3351 ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
3354 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3355 "BM_%d : beiscsi_cmd_eq_create"
3356 "Failed for ISCSI CQ\n");
3357 goto create_cq_error;
3359 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3360 "BM_%d : iscsi cq_id is %d for eq_id %d\n"
3361 "iSCSI CQ CREATED\n", cq->id, eq->id);
3366 for (i = 0; i < phba->num_cpus; i++) {
3367 cq = &phwi_context->be_cq[i];
3370 pci_free_consistent(phba->pcidev, num_cq_pages
3379 beiscsi_create_def_hdr(struct beiscsi_hba *phba,
3380 struct hwi_context_memory *phwi_context,
3381 struct hwi_controller *phwi_ctrlr,
3382 unsigned int def_pdu_ring_sz, uint8_t ulp_num)
3386 struct be_queue_info *dq, *cq;
3387 struct be_dma_mem *mem;
3388 struct be_mem_descriptor *mem_descr;
3392 dq = &phwi_context->be_def_hdrq[ulp_num];
3393 cq = &phwi_context->be_cq[0];
3395 mem_descr = phba->init_mem;
3396 mem_descr += HWI_MEM_ASYNC_HEADER_RING_ULP0 +
3397 (ulp_num * MEM_DESCR_OFFSET);
3398 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3399 ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
3400 sizeof(struct phys_addr),
3401 sizeof(struct phys_addr), dq_vaddress);
3403 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3404 "BM_%d : be_fill_queue Failed for DEF PDU HDR on ULP : %d\n",
3409 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3410 bus_address.u.a64.address;
3411 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
3413 phba->params.defpdu_hdr_sz,
3414 BEISCSI_DEFQ_HDR, ulp_num);
3416 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3417 "BM_%d : be_cmd_create_default_pdu_queue Failed DEFHDR on ULP : %d\n",
3423 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3424 "BM_%d : iscsi hdr def pdu id for ULP : %d is %d\n",
3426 phwi_context->be_def_hdrq[ulp_num].id);
3427 hwi_post_async_buffers(phba, BEISCSI_DEFQ_HDR, ulp_num);
3432 beiscsi_create_def_data(struct beiscsi_hba *phba,
3433 struct hwi_context_memory *phwi_context,
3434 struct hwi_controller *phwi_ctrlr,
3435 unsigned int def_pdu_ring_sz, uint8_t ulp_num)
3439 struct be_queue_info *dataq, *cq;
3440 struct be_dma_mem *mem;
3441 struct be_mem_descriptor *mem_descr;
3445 dataq = &phwi_context->be_def_dataq[ulp_num];
3446 cq = &phwi_context->be_cq[0];
3447 mem = &dataq->dma_mem;
3448 mem_descr = phba->init_mem;
3449 mem_descr += HWI_MEM_ASYNC_DATA_RING_ULP0 +
3450 (ulp_num * MEM_DESCR_OFFSET);
3451 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3452 ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
3453 sizeof(struct phys_addr),
3454 sizeof(struct phys_addr), dq_vaddress);
3456 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3457 "BM_%d : be_fill_queue Failed for DEF PDU "
3458 "DATA on ULP : %d\n",
3463 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3464 bus_address.u.a64.address;
3465 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
3467 phba->params.defpdu_data_sz,
3468 BEISCSI_DEFQ_DATA, ulp_num);
3470 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3471 "BM_%d be_cmd_create_default_pdu_queue"
3472 " Failed for DEF PDU DATA on ULP : %d\n",
3477 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3478 "BM_%d : iscsi def data id on ULP : %d is %d\n",
3480 phwi_context->be_def_dataq[ulp_num].id);
3482 hwi_post_async_buffers(phba, BEISCSI_DEFQ_DATA, ulp_num);
3483 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3484 "BM_%d : DEFAULT PDU DATA RING CREATED"
3485 "on ULP : %d\n", ulp_num);
3492 beiscsi_post_template_hdr(struct beiscsi_hba *phba)
3494 struct be_mem_descriptor *mem_descr;
3495 struct mem_array *pm_arr;
3496 struct be_dma_mem sgl;
3497 int status, ulp_num;
3499 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3500 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3501 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
3502 mem_descr += HWI_MEM_TEMPLATE_HDR_ULP0 +
3503 (ulp_num * MEM_DESCR_OFFSET);
3504 pm_arr = mem_descr->mem_array;
3506 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
3507 status = be_cmd_iscsi_post_template_hdr(
3511 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3512 "BM_%d : Post Template HDR Failed for"
3513 "ULP_%d\n", ulp_num);
3517 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3518 "BM_%d : Template HDR Pages Posted for"
3519 "ULP_%d\n", ulp_num);
3526 beiscsi_post_pages(struct beiscsi_hba *phba)
3528 struct be_mem_descriptor *mem_descr;
3529 struct mem_array *pm_arr;
3530 unsigned int page_offset, i;
3531 struct be_dma_mem sgl;
3532 int status, ulp_num = 0;
3534 mem_descr = phba->init_mem;
3535 mem_descr += HWI_MEM_SGE;
3536 pm_arr = mem_descr->mem_array;
3538 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3539 if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
3542 page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
3543 phba->fw_config.iscsi_icd_start[ulp_num]) / PAGE_SIZE;
3544 for (i = 0; i < mem_descr->num_elements; i++) {
3545 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
3546 status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
3548 (pm_arr->size / PAGE_SIZE));
3549 page_offset += pm_arr->size / PAGE_SIZE;
3551 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3552 "BM_%d : post sgl failed.\n");
3557 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3558 "BM_%d : POSTED PAGES\n");
3562 static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
3564 struct be_dma_mem *mem = &q->dma_mem;
3566 pci_free_consistent(phba->pcidev, mem->size,
3572 static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
3573 u16 len, u16 entry_size)
3575 struct be_dma_mem *mem = &q->dma_mem;
3577 memset(q, 0, sizeof(*q));
3579 q->entry_size = entry_size;
3580 mem->size = len * entry_size;
3581 mem->va = pci_zalloc_consistent(phba->pcidev, mem->size, &mem->dma);
3588 beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
3589 struct hwi_context_memory *phwi_context,
3590 struct hwi_controller *phwi_ctrlr)
3592 unsigned int wrb_mem_index, offset, size, num_wrb_rings;
3594 unsigned int idx, num, i, ulp_num;
3595 struct mem_array *pwrb_arr;
3597 struct be_dma_mem sgl;
3598 struct be_mem_descriptor *mem_descr;
3599 struct hwi_wrb_context *pwrb_context;
3601 uint8_t ulp_count = 0, ulp_base_num = 0;
3602 uint16_t cid_count_ulp[BEISCSI_ULP_COUNT] = { 0 };
3605 mem_descr = phba->init_mem;
3606 mem_descr += HWI_MEM_WRB;
3607 pwrb_arr = kmalloc(sizeof(*pwrb_arr) * phba->params.cxns_per_ctrl,
3610 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3611 "BM_%d : Memory alloc failed in create wrb ring.\n");
3614 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3615 pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
3616 num_wrb_rings = mem_descr->mem_array[idx].size /
3617 (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
3619 for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
3620 if (num_wrb_rings) {
3621 pwrb_arr[num].virtual_address = wrb_vaddr;
3622 pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
3623 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3624 sizeof(struct iscsi_wrb);
3625 wrb_vaddr += pwrb_arr[num].size;
3626 pa_addr_lo += pwrb_arr[num].size;
3630 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3631 pa_addr_lo = mem_descr->mem_array[idx].\
3632 bus_address.u.a64.address;
3633 num_wrb_rings = mem_descr->mem_array[idx].size /
3634 (phba->params.wrbs_per_cxn *
3635 sizeof(struct iscsi_wrb));
3636 pwrb_arr[num].virtual_address = wrb_vaddr;
3637 pwrb_arr[num].bus_address.u.a64.address\
3639 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3640 sizeof(struct iscsi_wrb);
3641 wrb_vaddr += pwrb_arr[num].size;
3642 pa_addr_lo += pwrb_arr[num].size;
3647 /* Get the ULP Count */
3648 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3649 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3651 ulp_base_num = ulp_num;
3652 cid_count_ulp[ulp_num] =
3653 BEISCSI_GET_CID_COUNT(phba, ulp_num);
3656 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3661 if (ulp_count > 1) {
3662 ulp_base_num = (ulp_base_num + 1) % BEISCSI_ULP_COUNT;
3664 if (!cid_count_ulp[ulp_base_num])
3665 ulp_base_num = (ulp_base_num + 1) %
3668 cid_count_ulp[ulp_base_num]--;
3672 hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
3673 status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
3674 &phwi_context->be_wrbq[i],
3675 &phwi_ctrlr->wrb_context[i],
3678 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3679 "BM_%d : wrbq create failed.");
3683 pwrb_context = &phwi_ctrlr->wrb_context[i];
3684 BE_SET_CID_TO_CRI(i, pwrb_context->cid);
3690 static void free_wrb_handles(struct beiscsi_hba *phba)
3693 struct hwi_controller *phwi_ctrlr;
3694 struct hwi_wrb_context *pwrb_context;
3696 phwi_ctrlr = phba->phwi_ctrlr;
3697 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
3698 pwrb_context = &phwi_ctrlr->wrb_context[index];
3699 kfree(pwrb_context->pwrb_handle_base);
3700 kfree(pwrb_context->pwrb_handle_basestd);
3704 static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
3706 struct be_queue_info *q;
3707 struct be_ctrl_info *ctrl = &phba->ctrl;
3709 q = &phba->ctrl.mcc_obj.q;
3711 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
3712 be_queue_free(phba, q);
3715 q = &phba->ctrl.mcc_obj.cq;
3717 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3718 be_queue_free(phba, q);
3722 static void hwi_cleanup(struct beiscsi_hba *phba)
3724 struct be_queue_info *q;
3725 struct be_ctrl_info *ctrl = &phba->ctrl;
3726 struct hwi_controller *phwi_ctrlr;
3727 struct hwi_context_memory *phwi_context;
3728 struct hwi_async_pdu_context *pasync_ctx;
3729 int i, eq_for_mcc, ulp_num;
3731 phwi_ctrlr = phba->phwi_ctrlr;
3732 phwi_context = phwi_ctrlr->phwi_ctxt;
3734 be_cmd_iscsi_remove_template_hdr(ctrl);
3736 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3737 q = &phwi_context->be_wrbq[i];
3739 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
3741 kfree(phwi_context->be_wrbq);
3742 free_wrb_handles(phba);
3744 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3745 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3747 q = &phwi_context->be_def_hdrq[ulp_num];
3749 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3751 q = &phwi_context->be_def_dataq[ulp_num];
3753 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3755 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num];
3759 beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
3761 for (i = 0; i < (phba->num_cpus); i++) {
3762 q = &phwi_context->be_cq[i];
3764 be_queue_free(phba, q);
3765 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3769 be_mcc_queues_destroy(phba);
3770 if (phba->msix_enabled)
3774 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
3775 q = &phwi_context->be_eq[i].q;
3777 be_queue_free(phba, q);
3778 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
3781 be_cmd_fw_uninit(ctrl);
3784 static int be_mcc_queues_create(struct beiscsi_hba *phba,
3785 struct hwi_context_memory *phwi_context)
3787 struct be_queue_info *q, *cq;
3788 struct be_ctrl_info *ctrl = &phba->ctrl;
3790 /* Alloc MCC compl queue */
3791 cq = &phba->ctrl.mcc_obj.cq;
3792 if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
3793 sizeof(struct be_mcc_compl)))
3795 /* Ask BE to create MCC compl queue; */
3796 if (phba->msix_enabled) {
3797 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq
3798 [phba->num_cpus].q, false, true, 0))
3801 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
3806 /* Alloc MCC queue */
3807 q = &phba->ctrl.mcc_obj.q;
3808 if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
3809 goto mcc_cq_destroy;
3811 /* Ask BE to create MCC queue */
3812 if (beiscsi_cmd_mccq_create(phba, q, cq))
3818 be_queue_free(phba, q);
3820 beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
3822 be_queue_free(phba, cq);
3828 * find_num_cpus()- Get the CPU online count
3829 * @phba: ptr to priv structure
3831 * CPU count is used for creating EQ.
3833 static void find_num_cpus(struct beiscsi_hba *phba)
3837 num_cpus = num_online_cpus();
3839 switch (phba->generation) {
3842 phba->num_cpus = (num_cpus > BEISCSI_MAX_NUM_CPUS) ?
3843 BEISCSI_MAX_NUM_CPUS : num_cpus;
3847 * If eqid_count == 1 fall back to
3850 if (phba->fw_config.eqid_count == 1) {
3857 (num_cpus > (phba->fw_config.eqid_count - 1)) ?
3858 (phba->fw_config.eqid_count - 1) : num_cpus;
3865 static int hwi_init_port(struct beiscsi_hba *phba)
3867 struct hwi_controller *phwi_ctrlr;
3868 struct hwi_context_memory *phwi_context;
3869 unsigned int def_pdu_ring_sz;
3870 struct be_ctrl_info *ctrl = &phba->ctrl;
3871 int status, ulp_num;
3873 phwi_ctrlr = phba->phwi_ctrlr;
3874 phwi_context = phwi_ctrlr->phwi_ctxt;
3875 phwi_context->max_eqd = 128;
3876 phwi_context->min_eqd = 0;
3877 phwi_context->cur_eqd = 0;
3878 be_cmd_fw_initialize(&phba->ctrl);
3879 /* set optic state to unknown */
3880 phba->optic_state = 0xff;
3882 status = beiscsi_create_eqs(phba, phwi_context);
3884 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3885 "BM_%d : EQ not created\n");
3889 status = be_mcc_queues_create(phba, phwi_context);
3893 status = mgmt_check_supported_fw(ctrl, phba);
3895 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3896 "BM_%d : Unsupported fw version\n");
3900 status = beiscsi_create_cqs(phba, phwi_context);
3902 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3903 "BM_%d : CQ not created\n");
3907 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3908 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3911 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
3912 sizeof(struct phys_addr);
3914 status = beiscsi_create_def_hdr(phba, phwi_context,
3919 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3920 "BM_%d : Default Header not created for ULP : %d\n",
3925 status = beiscsi_create_def_data(phba, phwi_context,
3930 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3931 "BM_%d : Default Data not created for ULP : %d\n",
3938 status = beiscsi_post_pages(phba);
3940 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3941 "BM_%d : Post SGL Pages Failed\n");
3945 status = beiscsi_post_template_hdr(phba);
3947 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3948 "BM_%d : Template HDR Posting for CXN Failed\n");
3951 status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
3953 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3954 "BM_%d : WRB Rings not created\n");
3958 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3959 uint16_t async_arr_idx = 0;
3961 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3963 struct hwi_async_pdu_context *pasync_ctx;
3965 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(
3966 phwi_ctrlr, ulp_num);
3968 phba->params.cxns_per_ctrl; cri++) {
3969 if (ulp_num == BEISCSI_GET_ULP_FROM_CRI
3971 pasync_ctx->cid_to_async_cri_map[
3972 phwi_ctrlr->wrb_context[cri].cid] =
3978 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3979 "BM_%d : hwi_init_port success\n");
3983 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3984 "BM_%d : hwi_init_port failed");
3989 static int hwi_init_controller(struct beiscsi_hba *phba)
3991 struct hwi_controller *phwi_ctrlr;
3993 phwi_ctrlr = phba->phwi_ctrlr;
3994 if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
3995 phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
3996 init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
3997 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3998 "BM_%d : phwi_ctrlr->phwi_ctxt=%p\n",
3999 phwi_ctrlr->phwi_ctxt);
4001 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4002 "BM_%d : HWI_MEM_ADDN_CONTEXT is more "
4003 "than one element.Failing to load\n");
4007 iscsi_init_global_templates(phba);
4008 if (beiscsi_init_wrb_handle(phba))
4011 if (hwi_init_async_pdu_ctx(phba)) {
4012 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4013 "BM_%d : hwi_init_async_pdu_ctx failed\n");
4017 if (hwi_init_port(phba) != 0) {
4018 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4019 "BM_%d : hwi_init_controller failed\n");
4026 static void beiscsi_free_mem(struct beiscsi_hba *phba)
4028 struct be_mem_descriptor *mem_descr;
4031 mem_descr = phba->init_mem;
4034 for (i = 0; i < SE_MEM_MAX; i++) {
4035 for (j = mem_descr->num_elements; j > 0; j--) {
4036 pci_free_consistent(phba->pcidev,
4037 mem_descr->mem_array[j - 1].size,
4038 mem_descr->mem_array[j - 1].virtual_address,
4039 (unsigned long)mem_descr->mem_array[j - 1].
4040 bus_address.u.a64.address);
4043 kfree(mem_descr->mem_array);
4046 kfree(phba->init_mem);
4047 kfree(phba->phwi_ctrlr->wrb_context);
4048 kfree(phba->phwi_ctrlr);
4051 static int beiscsi_init_controller(struct beiscsi_hba *phba)
4055 ret = beiscsi_get_memory(phba);
4057 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4058 "BM_%d : beiscsi_dev_probe -"
4059 "Failed in beiscsi_alloc_memory\n");
4063 ret = hwi_init_controller(phba);
4066 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4067 "BM_%d : Return success from beiscsi_init_controller");
4072 beiscsi_free_mem(phba);
4076 static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
4078 struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
4079 struct sgl_handle *psgl_handle;
4080 struct iscsi_sge *pfrag;
4081 unsigned int arr_index, i, idx;
4082 unsigned int ulp_icd_start, ulp_num = 0;
4084 phba->io_sgl_hndl_avbl = 0;
4085 phba->eh_sgl_hndl_avbl = 0;
4087 mem_descr_sglh = phba->init_mem;
4088 mem_descr_sglh += HWI_MEM_SGLH;
4089 if (1 == mem_descr_sglh->num_elements) {
4090 phba->io_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
4091 phba->params.ios_per_ctrl,
4093 if (!phba->io_sgl_hndl_base) {
4094 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4095 "BM_%d : Mem Alloc Failed. Failing to load\n");
4098 phba->eh_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
4099 (phba->params.icds_per_ctrl -
4100 phba->params.ios_per_ctrl),
4102 if (!phba->eh_sgl_hndl_base) {
4103 kfree(phba->io_sgl_hndl_base);
4104 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4105 "BM_%d : Mem Alloc Failed. Failing to load\n");
4109 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4110 "BM_%d : HWI_MEM_SGLH is more than one element."
4111 "Failing to load\n");
4117 while (idx < mem_descr_sglh->num_elements) {
4118 psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
4120 for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
4121 sizeof(struct sgl_handle)); i++) {
4122 if (arr_index < phba->params.ios_per_ctrl) {
4123 phba->io_sgl_hndl_base[arr_index] = psgl_handle;
4124 phba->io_sgl_hndl_avbl++;
4127 phba->eh_sgl_hndl_base[arr_index -
4128 phba->params.ios_per_ctrl] =
4131 phba->eh_sgl_hndl_avbl++;
4137 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4138 "BM_%d : phba->io_sgl_hndl_avbl=%d"
4139 "phba->eh_sgl_hndl_avbl=%d\n",
4140 phba->io_sgl_hndl_avbl,
4141 phba->eh_sgl_hndl_avbl);
4143 mem_descr_sg = phba->init_mem;
4144 mem_descr_sg += HWI_MEM_SGE;
4145 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4146 "\n BM_%d : mem_descr_sg->num_elements=%d\n",
4147 mem_descr_sg->num_elements);
4149 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
4150 if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
4153 ulp_icd_start = phba->fw_config.iscsi_icd_start[ulp_num];
4157 while (idx < mem_descr_sg->num_elements) {
4158 pfrag = mem_descr_sg->mem_array[idx].virtual_address;
4161 i < (mem_descr_sg->mem_array[idx].size) /
4162 (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
4164 if (arr_index < phba->params.ios_per_ctrl)
4165 psgl_handle = phba->io_sgl_hndl_base[arr_index];
4167 psgl_handle = phba->eh_sgl_hndl_base[arr_index -
4168 phba->params.ios_per_ctrl];
4169 psgl_handle->pfrag = pfrag;
4170 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
4171 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
4172 pfrag += phba->params.num_sge_per_io;
4173 psgl_handle->sgl_index = ulp_icd_start + arr_index++;
4177 phba->io_sgl_free_index = 0;
4178 phba->io_sgl_alloc_index = 0;
4179 phba->eh_sgl_free_index = 0;
4180 phba->eh_sgl_alloc_index = 0;
4184 static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
4187 uint16_t i, ulp_num;
4188 struct ulp_cid_info *ptr_cid_info = NULL;
4190 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4191 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4192 ptr_cid_info = kzalloc(sizeof(struct ulp_cid_info),
4195 if (!ptr_cid_info) {
4196 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4197 "BM_%d : Failed to allocate memory"
4198 "for ULP_CID_INFO for ULP : %d\n",
4205 /* Allocate memory for CID array */
4206 ptr_cid_info->cid_array = kzalloc(sizeof(void *) *
4207 BEISCSI_GET_CID_COUNT(phba,
4208 ulp_num), GFP_KERNEL);
4209 if (!ptr_cid_info->cid_array) {
4210 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4211 "BM_%d : Failed to allocate memory"
4212 "for CID_ARRAY for ULP : %d\n",
4214 kfree(ptr_cid_info);
4215 ptr_cid_info = NULL;
4220 ptr_cid_info->avlbl_cids = BEISCSI_GET_CID_COUNT(
4223 /* Save the cid_info_array ptr */
4224 phba->cid_array_info[ulp_num] = ptr_cid_info;
4227 phba->ep_array = kzalloc(sizeof(struct iscsi_endpoint *) *
4228 phba->params.cxns_per_ctrl, GFP_KERNEL);
4229 if (!phba->ep_array) {
4230 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4231 "BM_%d : Failed to allocate memory in "
4232 "hba_setup_cid_tbls\n");
4238 phba->conn_table = kzalloc(sizeof(struct beiscsi_conn *) *
4239 phba->params.cxns_per_ctrl, GFP_KERNEL);
4240 if (!phba->conn_table) {
4241 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4242 "BM_%d : Failed to allocate memory in"
4243 "hba_setup_cid_tbls\n");
4245 kfree(phba->ep_array);
4246 phba->ep_array = NULL;
4252 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
4253 ulp_num = phba->phwi_ctrlr->wrb_context[i].ulp_num;
4255 ptr_cid_info = phba->cid_array_info[ulp_num];
4256 ptr_cid_info->cid_array[ptr_cid_info->cid_alloc++] =
4257 phba->phwi_ctrlr->wrb_context[i].cid;
4261 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4262 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4263 ptr_cid_info = phba->cid_array_info[ulp_num];
4265 ptr_cid_info->cid_alloc = 0;
4266 ptr_cid_info->cid_free = 0;
4272 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4273 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4274 ptr_cid_info = phba->cid_array_info[ulp_num];
4277 kfree(ptr_cid_info->cid_array);
4278 kfree(ptr_cid_info);
4279 phba->cid_array_info[ulp_num] = NULL;
4287 static void hwi_enable_intr(struct beiscsi_hba *phba)
4289 struct be_ctrl_info *ctrl = &phba->ctrl;
4290 struct hwi_controller *phwi_ctrlr;
4291 struct hwi_context_memory *phwi_context;
4292 struct be_queue_info *eq;
4297 phwi_ctrlr = phba->phwi_ctrlr;
4298 phwi_context = phwi_ctrlr->phwi_ctxt;
4300 addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
4301 PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
4302 reg = ioread32(addr);
4304 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4306 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4307 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4308 "BM_%d : reg =x%08x addr=%p\n", reg, addr);
4309 iowrite32(reg, addr);
4312 if (!phba->msix_enabled) {
4313 eq = &phwi_context->be_eq[0].q;
4314 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4315 "BM_%d : eq->id=%d\n", eq->id);
4317 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
4319 for (i = 0; i <= phba->num_cpus; i++) {
4320 eq = &phwi_context->be_eq[i].q;
4321 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4322 "BM_%d : eq->id=%d\n", eq->id);
4323 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
4328 static void hwi_disable_intr(struct beiscsi_hba *phba)
4330 struct be_ctrl_info *ctrl = &phba->ctrl;
4332 u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
4333 u32 reg = ioread32(addr);
4335 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4337 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4338 iowrite32(reg, addr);
4340 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
4341 "BM_%d : In hwi_disable_intr, Already Disabled\n");
4345 * beiscsi_get_boot_info()- Get the boot session info
4346 * @phba: The device priv structure instance
4348 * Get the boot target info and store in driver priv structure
4352 * Failure: Non-Zero Value
4354 static int beiscsi_get_boot_info(struct beiscsi_hba *phba)
4356 struct be_cmd_get_session_resp *session_resp;
4357 struct be_dma_mem nonemb_cmd;
4359 unsigned int s_handle;
4362 /* Get the session handle of the boot target */
4363 ret = be_mgmt_get_boot_shandle(phba, &s_handle);
4365 beiscsi_log(phba, KERN_ERR,
4366 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
4367 "BM_%d : No boot session\n");
4376 nonemb_cmd.va = pci_zalloc_consistent(phba->ctrl.pdev,
4377 sizeof(*session_resp),
4379 if (nonemb_cmd.va == NULL) {
4380 beiscsi_log(phba, KERN_ERR,
4381 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
4382 "BM_%d : Failed to allocate memory for"
4383 "beiscsi_get_session_info\n");
4388 tag = mgmt_get_session_info(phba, s_handle,
4391 beiscsi_log(phba, KERN_ERR,
4392 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
4393 "BM_%d : beiscsi_get_session_info"
4399 ret = beiscsi_mccq_compl(phba, tag, NULL, &nonemb_cmd);
4401 beiscsi_log(phba, KERN_ERR,
4402 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
4403 "BM_%d : beiscsi_get_session_info Failed");
4411 session_resp = nonemb_cmd.va ;
4413 memcpy(&phba->boot_sess, &session_resp->session_info,
4414 sizeof(struct mgmt_session_info));
4416 beiscsi_logout_fw_sess(phba,
4417 phba->boot_sess.session_handle);
4421 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4422 nonemb_cmd.va, nonemb_cmd.dma);
4426 static void beiscsi_boot_release(void *data)
4428 struct beiscsi_hba *phba = data;
4430 scsi_host_put(phba->shost);
4433 static int beiscsi_setup_boot_info(struct beiscsi_hba *phba)
4435 struct iscsi_boot_kobj *boot_kobj;
4437 /* it has been created previously */
4438 if (phba->boot_kset)
4441 /* get boot info using mgmt cmd */
4442 if (beiscsi_get_boot_info(phba))
4443 /* Try to see if we can carry on without this */
4446 phba->boot_kset = iscsi_boot_create_host_kset(phba->shost->host_no);
4447 if (!phba->boot_kset)
4450 /* get a ref because the show function will ref the phba */
4451 if (!scsi_host_get(phba->shost))
4453 boot_kobj = iscsi_boot_create_target(phba->boot_kset, 0, phba,
4454 beiscsi_show_boot_tgt_info,
4455 beiscsi_tgt_get_attr_visibility,
4456 beiscsi_boot_release);
4460 if (!scsi_host_get(phba->shost))
4462 boot_kobj = iscsi_boot_create_initiator(phba->boot_kset, 0, phba,
4463 beiscsi_show_boot_ini_info,
4464 beiscsi_ini_get_attr_visibility,
4465 beiscsi_boot_release);
4469 if (!scsi_host_get(phba->shost))
4471 boot_kobj = iscsi_boot_create_ethernet(phba->boot_kset, 0, phba,
4472 beiscsi_show_boot_eth_info,
4473 beiscsi_eth_get_attr_visibility,
4474 beiscsi_boot_release);
4480 scsi_host_put(phba->shost);
4482 iscsi_boot_destroy_kset(phba->boot_kset);
4486 static int beiscsi_init_port(struct beiscsi_hba *phba)
4490 ret = beiscsi_init_controller(phba);
4492 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4493 "BM_%d : beiscsi_dev_probe - Failed in"
4494 "beiscsi_init_controller\n");
4497 ret = beiscsi_init_sgl_handle(phba);
4499 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4500 "BM_%d : beiscsi_dev_probe - Failed in"
4501 "beiscsi_init_sgl_handle\n");
4502 goto do_cleanup_ctrlr;
4505 if (hba_setup_cid_tbls(phba)) {
4506 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4507 "BM_%d : Failed in hba_setup_cid_tbls\n");
4508 kfree(phba->io_sgl_hndl_base);
4509 kfree(phba->eh_sgl_hndl_base);
4510 goto do_cleanup_ctrlr;
4520 static void hwi_purge_eq(struct beiscsi_hba *phba)
4522 struct hwi_controller *phwi_ctrlr;
4523 struct hwi_context_memory *phwi_context;
4524 struct be_queue_info *eq;
4525 struct be_eq_entry *eqe = NULL;
4527 unsigned int num_processed;
4529 phwi_ctrlr = phba->phwi_ctrlr;
4530 phwi_context = phwi_ctrlr->phwi_ctxt;
4531 if (phba->msix_enabled)
4536 for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
4537 eq = &phwi_context->be_eq[i].q;
4538 eqe = queue_tail_node(eq);
4540 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
4542 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
4544 eqe = queue_tail_node(eq);
4549 hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
4553 static void beiscsi_clean_port(struct beiscsi_hba *phba)
4555 int mgmt_status, ulp_num;
4556 struct ulp_cid_info *ptr_cid_info = NULL;
4558 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4559 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4560 mgmt_status = mgmt_epfw_cleanup(phba, ulp_num);
4562 beiscsi_log(phba, KERN_WARNING,
4564 "BM_%d : mgmt_epfw_cleanup FAILED"
4565 " for ULP_%d\n", ulp_num);
4571 kfree(phba->io_sgl_hndl_base);
4572 kfree(phba->eh_sgl_hndl_base);
4573 kfree(phba->ep_array);
4574 kfree(phba->conn_table);
4576 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4577 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4578 ptr_cid_info = phba->cid_array_info[ulp_num];
4581 kfree(ptr_cid_info->cid_array);
4582 kfree(ptr_cid_info);
4583 phba->cid_array_info[ulp_num] = NULL;
4591 * beiscsi_free_mgmt_task_handles()- Free driver CXN resources
4592 * @beiscsi_conn: ptr to the conn to be cleaned up
4593 * @task: ptr to iscsi_task resource to be freed.
4595 * Free driver mgmt resources binded to CXN.
4598 beiscsi_free_mgmt_task_handles(struct beiscsi_conn *beiscsi_conn,
4599 struct iscsi_task *task)
4601 struct beiscsi_io_task *io_task;
4602 struct beiscsi_hba *phba = beiscsi_conn->phba;
4603 struct hwi_wrb_context *pwrb_context;
4604 struct hwi_controller *phwi_ctrlr;
4605 uint16_t cri_index = BE_GET_CRI_FROM_CID(
4606 beiscsi_conn->beiscsi_conn_cid);
4608 phwi_ctrlr = phba->phwi_ctrlr;
4609 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
4611 io_task = task->dd_data;
4613 if (io_task->pwrb_handle) {
4614 memset(io_task->pwrb_handle->pwrb, 0,
4615 sizeof(struct iscsi_wrb));
4616 free_wrb_handle(phba, pwrb_context,
4617 io_task->pwrb_handle);
4618 io_task->pwrb_handle = NULL;
4621 if (io_task->psgl_handle) {
4622 spin_lock_bh(&phba->mgmt_sgl_lock);
4623 free_mgmt_sgl_handle(phba,
4624 io_task->psgl_handle);
4625 io_task->psgl_handle = NULL;
4626 spin_unlock_bh(&phba->mgmt_sgl_lock);
4629 if (io_task->mtask_addr) {
4630 pci_unmap_single(phba->pcidev,
4631 io_task->mtask_addr,
4632 io_task->mtask_data_count,
4634 io_task->mtask_addr = 0;
4639 * beiscsi_cleanup_task()- Free driver resources of the task
4640 * @task: ptr to the iscsi task
4643 static void beiscsi_cleanup_task(struct iscsi_task *task)
4645 struct beiscsi_io_task *io_task = task->dd_data;
4646 struct iscsi_conn *conn = task->conn;
4647 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4648 struct beiscsi_hba *phba = beiscsi_conn->phba;
4649 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4650 struct hwi_wrb_context *pwrb_context;
4651 struct hwi_controller *phwi_ctrlr;
4652 uint16_t cri_index = BE_GET_CRI_FROM_CID(
4653 beiscsi_conn->beiscsi_conn_cid);
4655 phwi_ctrlr = phba->phwi_ctrlr;
4656 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
4658 if (io_task->cmd_bhs) {
4659 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4660 io_task->bhs_pa.u.a64.address);
4661 io_task->cmd_bhs = NULL;
4665 if (io_task->pwrb_handle) {
4666 free_wrb_handle(phba, pwrb_context,
4667 io_task->pwrb_handle);
4668 io_task->pwrb_handle = NULL;
4671 if (io_task->psgl_handle) {
4672 spin_lock(&phba->io_sgl_lock);
4673 free_io_sgl_handle(phba, io_task->psgl_handle);
4674 spin_unlock(&phba->io_sgl_lock);
4675 io_task->psgl_handle = NULL;
4678 if (io_task->scsi_cmnd) {
4679 scsi_dma_unmap(io_task->scsi_cmnd);
4680 io_task->scsi_cmnd = NULL;
4683 if (!beiscsi_conn->login_in_progress)
4684 beiscsi_free_mgmt_task_handles(beiscsi_conn, task);
4689 beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
4690 struct beiscsi_offload_params *params)
4692 struct wrb_handle *pwrb_handle;
4693 struct hwi_wrb_context *pwrb_context = NULL;
4694 struct beiscsi_hba *phba = beiscsi_conn->phba;
4695 struct iscsi_task *task = beiscsi_conn->task;
4696 struct iscsi_session *session = task->conn->session;
4700 * We can always use 0 here because it is reserved by libiscsi for
4701 * login/startup related tasks.
4703 beiscsi_conn->login_in_progress = 0;
4704 spin_lock_bh(&session->back_lock);
4705 beiscsi_cleanup_task(task);
4706 spin_unlock_bh(&session->back_lock);
4708 pwrb_handle = alloc_wrb_handle(phba, beiscsi_conn->beiscsi_conn_cid,
4711 /* Check for the adapter family */
4712 if (is_chip_be2_be3r(phba))
4713 beiscsi_offload_cxn_v0(params, pwrb_handle,
4717 beiscsi_offload_cxn_v2(params, pwrb_handle,
4720 be_dws_le_to_cpu(pwrb_handle->pwrb,
4721 sizeof(struct iscsi_target_context_update_wrb));
4723 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
4724 doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
4725 << DB_DEF_PDU_WRB_INDEX_SHIFT;
4726 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4727 iowrite32(doorbell, phba->db_va +
4728 beiscsi_conn->doorbell_offset);
4731 * There is no completion for CONTEXT_UPDATE. The completion of next
4732 * WRB posted guarantees FW's processing and DMA'ing of it.
4733 * Use beiscsi_put_wrb_handle to put it back in the pool which makes
4734 * sure zero'ing or reuse of the WRB only after wrbs_per_cxn.
4736 beiscsi_put_wrb_handle(pwrb_context, pwrb_handle,
4737 phba->params.wrbs_per_cxn);
4738 beiscsi_log(phba, KERN_INFO,
4739 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4740 "BM_%d : put CONTEXT_UPDATE pwrb_handle=%p free_index=0x%x wrb_handles_available=%d\n",
4741 pwrb_handle, pwrb_context->free_index,
4742 pwrb_context->wrb_handles_available);
4745 static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
4746 int *index, int *age)
4750 *age = conn->session->age;
4754 * beiscsi_alloc_pdu - allocates pdu and related resources
4755 * @task: libiscsi task
4756 * @opcode: opcode of pdu for task
4758 * This is called with the session lock held. It will allocate
4759 * the wrb and sgl if needed for the command. And it will prep
4760 * the pdu's itt. beiscsi_parse_pdu will later translate
4761 * the pdu itt to the libiscsi task itt.
4763 static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
4765 struct beiscsi_io_task *io_task = task->dd_data;
4766 struct iscsi_conn *conn = task->conn;
4767 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4768 struct beiscsi_hba *phba = beiscsi_conn->phba;
4769 struct hwi_wrb_context *pwrb_context;
4770 struct hwi_controller *phwi_ctrlr;
4772 uint16_t cri_index = 0;
4773 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4776 io_task->cmd_bhs = pci_pool_alloc(beiscsi_sess->bhs_pool,
4777 GFP_ATOMIC, &paddr);
4778 if (!io_task->cmd_bhs)
4780 io_task->bhs_pa.u.a64.address = paddr;
4781 io_task->libiscsi_itt = (itt_t)task->itt;
4782 io_task->conn = beiscsi_conn;
4784 task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
4785 task->hdr_max = sizeof(struct be_cmd_bhs);
4786 io_task->psgl_handle = NULL;
4787 io_task->pwrb_handle = NULL;
4790 spin_lock(&phba->io_sgl_lock);
4791 io_task->psgl_handle = alloc_io_sgl_handle(phba);
4792 spin_unlock(&phba->io_sgl_lock);
4793 if (!io_task->psgl_handle) {
4794 beiscsi_log(phba, KERN_ERR,
4795 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4796 "BM_%d : Alloc of IO_SGL_ICD Failed"
4797 "for the CID : %d\n",
4798 beiscsi_conn->beiscsi_conn_cid);
4801 io_task->pwrb_handle = alloc_wrb_handle(phba,
4802 beiscsi_conn->beiscsi_conn_cid,
4803 &io_task->pwrb_context);
4804 if (!io_task->pwrb_handle) {
4805 beiscsi_log(phba, KERN_ERR,
4806 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4807 "BM_%d : Alloc of WRB_HANDLE Failed"
4808 "for the CID : %d\n",
4809 beiscsi_conn->beiscsi_conn_cid);
4813 io_task->scsi_cmnd = NULL;
4814 if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
4815 beiscsi_conn->task = task;
4816 if (!beiscsi_conn->login_in_progress) {
4817 spin_lock(&phba->mgmt_sgl_lock);
4818 io_task->psgl_handle = (struct sgl_handle *)
4819 alloc_mgmt_sgl_handle(phba);
4820 spin_unlock(&phba->mgmt_sgl_lock);
4821 if (!io_task->psgl_handle) {
4822 beiscsi_log(phba, KERN_ERR,
4825 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4826 "for the CID : %d\n",
4832 beiscsi_conn->login_in_progress = 1;
4833 beiscsi_conn->plogin_sgl_handle =
4834 io_task->psgl_handle;
4835 io_task->pwrb_handle =
4836 alloc_wrb_handle(phba,
4837 beiscsi_conn->beiscsi_conn_cid,
4838 &io_task->pwrb_context);
4839 if (!io_task->pwrb_handle) {
4840 beiscsi_log(phba, KERN_ERR,
4843 "BM_%d : Alloc of WRB_HANDLE Failed"
4844 "for the CID : %d\n",
4847 goto free_mgmt_hndls;
4849 beiscsi_conn->plogin_wrb_handle =
4850 io_task->pwrb_handle;
4853 io_task->psgl_handle =
4854 beiscsi_conn->plogin_sgl_handle;
4855 io_task->pwrb_handle =
4856 beiscsi_conn->plogin_wrb_handle;
4859 spin_lock(&phba->mgmt_sgl_lock);
4860 io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
4861 spin_unlock(&phba->mgmt_sgl_lock);
4862 if (!io_task->psgl_handle) {
4863 beiscsi_log(phba, KERN_ERR,
4866 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4867 "for the CID : %d\n",
4872 io_task->pwrb_handle =
4873 alloc_wrb_handle(phba,
4874 beiscsi_conn->beiscsi_conn_cid,
4875 &io_task->pwrb_context);
4876 if (!io_task->pwrb_handle) {
4877 beiscsi_log(phba, KERN_ERR,
4878 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4879 "BM_%d : Alloc of WRB_HANDLE Failed"
4880 "for the CID : %d\n",
4881 beiscsi_conn->beiscsi_conn_cid);
4882 goto free_mgmt_hndls;
4887 itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
4888 wrb_index << 16) | (unsigned int)
4889 (io_task->psgl_handle->sgl_index));
4890 io_task->pwrb_handle->pio_handle = task;
4892 io_task->cmd_bhs->iscsi_hdr.itt = itt;
4896 spin_lock(&phba->io_sgl_lock);
4897 free_io_sgl_handle(phba, io_task->psgl_handle);
4898 spin_unlock(&phba->io_sgl_lock);
4901 spin_lock(&phba->mgmt_sgl_lock);
4902 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
4903 io_task->psgl_handle = NULL;
4904 spin_unlock(&phba->mgmt_sgl_lock);
4906 phwi_ctrlr = phba->phwi_ctrlr;
4907 cri_index = BE_GET_CRI_FROM_CID(
4908 beiscsi_conn->beiscsi_conn_cid);
4909 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
4910 if (io_task->pwrb_handle)
4911 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
4912 io_task->pwrb_handle = NULL;
4913 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4914 io_task->bhs_pa.u.a64.address);
4915 io_task->cmd_bhs = NULL;
4918 int beiscsi_iotask_v2(struct iscsi_task *task, struct scatterlist *sg,
4919 unsigned int num_sg, unsigned int xferlen,
4920 unsigned int writedir)
4923 struct beiscsi_io_task *io_task = task->dd_data;
4924 struct iscsi_conn *conn = task->conn;
4925 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4926 struct beiscsi_hba *phba = beiscsi_conn->phba;
4927 struct iscsi_wrb *pwrb = NULL;
4928 unsigned int doorbell = 0;
4930 pwrb = io_task->pwrb_handle->pwrb;
4932 io_task->cmd_bhs->iscsi_hdr.exp_statsn = 0;
4933 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4936 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4938 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 1);
4940 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4942 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 0);
4945 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb_v2,
4948 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, lun, pwrb,
4949 cpu_to_be16(*(unsigned short *)
4950 &io_task->cmd_bhs->iscsi_hdr.lun));
4951 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb, xferlen);
4952 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4953 io_task->pwrb_handle->wrb_index);
4954 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4955 be32_to_cpu(task->cmdsn));
4956 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4957 io_task->psgl_handle->sgl_index);
4959 hwi_write_sgl_v2(pwrb, sg, num_sg, io_task);
4960 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
4961 io_task->pwrb_handle->wrb_index);
4962 if (io_task->pwrb_context->plast_wrb)
4963 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb,
4964 io_task->pwrb_context->plast_wrb,
4965 io_task->pwrb_handle->wrb_index);
4966 io_task->pwrb_context->plast_wrb = pwrb;
4968 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4970 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
4971 doorbell |= (io_task->pwrb_handle->wrb_index &
4972 DB_DEF_PDU_WRB_INDEX_MASK) <<
4973 DB_DEF_PDU_WRB_INDEX_SHIFT;
4974 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4975 iowrite32(doorbell, phba->db_va +
4976 beiscsi_conn->doorbell_offset);
4980 static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
4981 unsigned int num_sg, unsigned int xferlen,
4982 unsigned int writedir)
4985 struct beiscsi_io_task *io_task = task->dd_data;
4986 struct iscsi_conn *conn = task->conn;
4987 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4988 struct beiscsi_hba *phba = beiscsi_conn->phba;
4989 struct iscsi_wrb *pwrb = NULL;
4990 unsigned int doorbell = 0;
4992 pwrb = io_task->pwrb_handle->pwrb;
4993 io_task->cmd_bhs->iscsi_hdr.exp_statsn = 0;
4994 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4997 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4999 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
5001 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
5003 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
5006 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb,
5009 AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
5010 cpu_to_be16(*(unsigned short *)
5011 &io_task->cmd_bhs->iscsi_hdr.lun));
5012 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
5013 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
5014 io_task->pwrb_handle->wrb_index);
5015 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
5016 be32_to_cpu(task->cmdsn));
5017 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
5018 io_task->psgl_handle->sgl_index);
5020 hwi_write_sgl(pwrb, sg, num_sg, io_task);
5022 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
5023 io_task->pwrb_handle->wrb_index);
5024 if (io_task->pwrb_context->plast_wrb)
5025 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb,
5026 io_task->pwrb_context->plast_wrb,
5027 io_task->pwrb_handle->wrb_index);
5028 io_task->pwrb_context->plast_wrb = pwrb;
5030 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
5032 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
5033 doorbell |= (io_task->pwrb_handle->wrb_index &
5034 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
5035 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
5037 iowrite32(doorbell, phba->db_va +
5038 beiscsi_conn->doorbell_offset);
5042 static int beiscsi_mtask(struct iscsi_task *task)
5044 struct beiscsi_io_task *io_task = task->dd_data;
5045 struct iscsi_conn *conn = task->conn;
5046 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
5047 struct beiscsi_hba *phba = beiscsi_conn->phba;
5048 struct iscsi_wrb *pwrb = NULL;
5049 unsigned int doorbell = 0;
5051 unsigned int pwrb_typeoffset = 0;
5054 cid = beiscsi_conn->beiscsi_conn_cid;
5055 pwrb = io_task->pwrb_handle->pwrb;
5056 memset(pwrb, 0, sizeof(*pwrb));
5058 if (is_chip_be2_be3r(phba)) {
5059 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
5060 be32_to_cpu(task->cmdsn));
5061 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
5062 io_task->pwrb_handle->wrb_index);
5063 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
5064 io_task->psgl_handle->sgl_index);
5065 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
5067 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
5068 io_task->pwrb_handle->wrb_index);
5069 if (io_task->pwrb_context->plast_wrb)
5070 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb,
5071 io_task->pwrb_context->plast_wrb,
5072 io_task->pwrb_handle->wrb_index);
5073 io_task->pwrb_context->plast_wrb = pwrb;
5075 pwrb_typeoffset = BE_WRB_TYPE_OFFSET;
5077 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
5078 be32_to_cpu(task->cmdsn));
5079 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
5080 io_task->pwrb_handle->wrb_index);
5081 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
5082 io_task->psgl_handle->sgl_index);
5083 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb,
5085 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
5086 io_task->pwrb_handle->wrb_index);
5087 if (io_task->pwrb_context->plast_wrb)
5088 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb,
5089 io_task->pwrb_context->plast_wrb,
5090 io_task->pwrb_handle->wrb_index);
5091 io_task->pwrb_context->plast_wrb = pwrb;
5093 pwrb_typeoffset = SKH_WRB_TYPE_OFFSET;
5097 switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
5098 case ISCSI_OP_LOGIN:
5099 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
5100 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
5101 ret = hwi_write_buffer(pwrb, task);
5103 case ISCSI_OP_NOOP_OUT:
5104 if (task->hdr->ttt != ISCSI_RESERVED_TAG) {
5105 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
5106 if (is_chip_be2_be3r(phba))
5107 AMAP_SET_BITS(struct amap_iscsi_wrb,
5110 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
5113 ADAPTER_SET_WRB_TYPE(pwrb, INI_RD_CMD, pwrb_typeoffset);
5114 if (is_chip_be2_be3r(phba))
5115 AMAP_SET_BITS(struct amap_iscsi_wrb,
5118 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
5121 ret = hwi_write_buffer(pwrb, task);
5124 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
5125 ret = hwi_write_buffer(pwrb, task);
5127 case ISCSI_OP_SCSI_TMFUNC:
5128 ADAPTER_SET_WRB_TYPE(pwrb, INI_TMF_CMD, pwrb_typeoffset);
5129 ret = hwi_write_buffer(pwrb, task);
5131 case ISCSI_OP_LOGOUT:
5132 ADAPTER_SET_WRB_TYPE(pwrb, HWH_TYPE_LOGOUT, pwrb_typeoffset);
5133 ret = hwi_write_buffer(pwrb, task);
5137 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
5138 "BM_%d : opcode =%d Not supported\n",
5139 task->hdr->opcode & ISCSI_OPCODE_MASK);
5147 /* Set the task type */
5148 io_task->wrb_type = (is_chip_be2_be3r(phba)) ?
5149 AMAP_GET_BITS(struct amap_iscsi_wrb, type, pwrb) :
5150 AMAP_GET_BITS(struct amap_iscsi_wrb_v2, type, pwrb);
5152 doorbell |= cid & DB_WRB_POST_CID_MASK;
5153 doorbell |= (io_task->pwrb_handle->wrb_index &
5154 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
5155 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
5156 iowrite32(doorbell, phba->db_va +
5157 beiscsi_conn->doorbell_offset);
5161 static int beiscsi_task_xmit(struct iscsi_task *task)
5163 struct beiscsi_io_task *io_task = task->dd_data;
5164 struct scsi_cmnd *sc = task->sc;
5165 struct beiscsi_hba *phba = NULL;
5166 struct scatterlist *sg;
5168 unsigned int writedir = 0, xferlen = 0;
5170 phba = ((struct beiscsi_conn *)task->conn->dd_data)->phba;
5173 return beiscsi_mtask(task);
5175 io_task->scsi_cmnd = sc;
5176 num_sg = scsi_dma_map(sc);
5178 struct iscsi_conn *conn = task->conn;
5179 struct beiscsi_hba *phba = NULL;
5181 phba = ((struct beiscsi_conn *)conn->dd_data)->phba;
5182 beiscsi_log(phba, KERN_ERR,
5183 BEISCSI_LOG_IO | BEISCSI_LOG_ISCSI,
5184 "BM_%d : scsi_dma_map Failed "
5185 "Driver_ITT : 0x%x ITT : 0x%x Xferlen : 0x%x\n",
5186 be32_to_cpu(io_task->cmd_bhs->iscsi_hdr.itt),
5187 io_task->libiscsi_itt, scsi_bufflen(sc));
5191 xferlen = scsi_bufflen(sc);
5192 sg = scsi_sglist(sc);
5193 if (sc->sc_data_direction == DMA_TO_DEVICE)
5198 return phba->iotask_fn(task, sg, num_sg, xferlen, writedir);
5202 * beiscsi_bsg_request - handle bsg request from ISCSI transport
5203 * @job: job to handle
5205 static int beiscsi_bsg_request(struct bsg_job *job)
5207 struct Scsi_Host *shost;
5208 struct beiscsi_hba *phba;
5209 struct iscsi_bsg_request *bsg_req = job->request;
5212 struct be_dma_mem nonemb_cmd;
5213 struct be_cmd_resp_hdr *resp;
5214 struct iscsi_bsg_reply *bsg_reply = job->reply;
5215 unsigned short status, extd_status;
5217 shost = iscsi_job_to_shost(job);
5218 phba = iscsi_host_priv(shost);
5220 switch (bsg_req->msgcode) {
5221 case ISCSI_BSG_HST_VENDOR:
5222 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
5223 job->request_payload.payload_len,
5225 if (nonemb_cmd.va == NULL) {
5226 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
5227 "BM_%d : Failed to allocate memory for "
5228 "beiscsi_bsg_request\n");
5231 tag = mgmt_vendor_specific_fw_cmd(&phba->ctrl, phba, job,
5234 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
5235 "BM_%d : MBX Tag Allocation Failed\n");
5237 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
5238 nonemb_cmd.va, nonemb_cmd.dma);
5242 rc = wait_event_interruptible_timeout(
5243 phba->ctrl.mcc_wait[tag],
5244 phba->ctrl.mcc_numtag[tag],
5246 BEISCSI_HOST_MBX_TIMEOUT));
5247 extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
5248 status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
5249 free_mcc_tag(&phba->ctrl, tag);
5250 resp = (struct be_cmd_resp_hdr *)nonemb_cmd.va;
5251 sg_copy_from_buffer(job->reply_payload.sg_list,
5252 job->reply_payload.sg_cnt,
5253 nonemb_cmd.va, (resp->response_length
5255 bsg_reply->reply_payload_rcv_len = resp->response_length;
5256 bsg_reply->result = status;
5257 bsg_job_done(job, bsg_reply->result,
5258 bsg_reply->reply_payload_rcv_len);
5259 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
5260 nonemb_cmd.va, nonemb_cmd.dma);
5261 if (status || extd_status) {
5262 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
5263 "BM_%d : MBX Cmd Failed"
5264 " status = %d extd_status = %d\n",
5265 status, extd_status);
5274 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
5275 "BM_%d : Unsupported bsg command: 0x%x\n",
5283 void beiscsi_hba_attrs_init(struct beiscsi_hba *phba)
5285 /* Set the logging parameter */
5286 beiscsi_log_enable_init(phba, beiscsi_log_enable);
5290 * beiscsi_quiesce()- Cleanup Driver resources
5291 * @phba: Instance Priv structure
5292 * @unload_state:i Clean or EEH unload state
5294 * Free the OS and HW resources held by the driver
5296 static void beiscsi_quiesce(struct beiscsi_hba *phba,
5297 uint32_t unload_state)
5299 struct hwi_controller *phwi_ctrlr;
5300 struct hwi_context_memory *phwi_context;
5301 struct be_eq_obj *pbe_eq;
5302 unsigned int i, msix_vec;
5304 phwi_ctrlr = phba->phwi_ctrlr;
5305 phwi_context = phwi_ctrlr->phwi_ctxt;
5306 hwi_disable_intr(phba);
5307 if (phba->msix_enabled) {
5308 for (i = 0; i <= phba->num_cpus; i++) {
5309 msix_vec = phba->msix_entries[i].vector;
5310 synchronize_irq(msix_vec);
5311 free_irq(msix_vec, &phwi_context->be_eq[i]);
5312 kfree(phba->msi_name[i]);
5315 if (phba->pcidev->irq) {
5316 synchronize_irq(phba->pcidev->irq);
5317 free_irq(phba->pcidev->irq, phba);
5319 pci_disable_msix(phba->pcidev);
5320 cancel_delayed_work_sync(&phba->beiscsi_hw_check_task);
5322 for (i = 0; i < phba->num_cpus; i++) {
5323 pbe_eq = &phwi_context->be_eq[i];
5324 irq_poll_disable(&pbe_eq->iopoll);
5327 if (unload_state == BEISCSI_CLEAN_UNLOAD) {
5328 destroy_workqueue(phba->wq);
5329 beiscsi_clean_port(phba);
5330 beiscsi_free_mem(phba);
5332 beiscsi_unmap_pci_function(phba);
5333 pci_free_consistent(phba->pcidev,
5334 phba->ctrl.mbox_mem_alloced.size,
5335 phba->ctrl.mbox_mem_alloced.va,
5336 phba->ctrl.mbox_mem_alloced.dma);
5344 static void beiscsi_remove(struct pci_dev *pcidev)
5346 struct beiscsi_hba *phba = NULL;
5348 phba = pci_get_drvdata(pcidev);
5350 dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
5354 beiscsi_destroy_def_ifaces(phba);
5355 iscsi_boot_destroy_kset(phba->boot_kset);
5356 iscsi_host_remove(phba->shost);
5357 beiscsi_quiesce(phba, BEISCSI_CLEAN_UNLOAD);
5358 pci_dev_put(phba->pcidev);
5359 iscsi_host_free(phba->shost);
5360 pci_disable_pcie_error_reporting(pcidev);
5361 pci_set_drvdata(pcidev, NULL);
5362 pci_release_regions(pcidev);
5363 pci_disable_device(pcidev);
5366 static void beiscsi_msix_enable(struct beiscsi_hba *phba)
5370 for (i = 0; i <= phba->num_cpus; i++)
5371 phba->msix_entries[i].entry = i;
5373 status = pci_enable_msix_range(phba->pcidev, phba->msix_entries,
5374 phba->num_cpus + 1, phba->num_cpus + 1);
5376 phba->msix_enabled = true;
5381 static void be_eqd_update(struct beiscsi_hba *phba)
5383 struct be_set_eqd set_eqd[MAX_CPUS];
5384 struct be_aic_obj *aic;
5385 struct be_eq_obj *pbe_eq;
5386 struct hwi_controller *phwi_ctrlr;
5387 struct hwi_context_memory *phwi_context;
5388 int eqd, i, num = 0;
5393 phwi_ctrlr = phba->phwi_ctrlr;
5394 phwi_context = phwi_ctrlr->phwi_ctxt;
5396 for (i = 0; i <= phba->num_cpus; i++) {
5397 aic = &phba->aic_obj[i];
5398 pbe_eq = &phwi_context->be_eq[i];
5400 if (!aic->jiffs || time_before(now, aic->jiffs) ||
5401 pbe_eq->cq_count < aic->eq_prev) {
5403 aic->eq_prev = pbe_eq->cq_count;
5406 delta = jiffies_to_msecs(now - aic->jiffs);
5407 pps = (((u32)(pbe_eq->cq_count - aic->eq_prev) * 1000) / delta);
5408 eqd = (pps / 1500) << 2;
5412 eqd = min_t(u32, eqd, phwi_context->max_eqd);
5413 eqd = max_t(u32, eqd, phwi_context->min_eqd);
5416 aic->eq_prev = pbe_eq->cq_count;
5418 if (eqd != aic->prev_eqd) {
5419 set_eqd[num].delay_multiplier = (eqd * 65)/100;
5420 set_eqd[num].eq_id = pbe_eq->q.id;
5421 aic->prev_eqd = eqd;
5426 tag = be_cmd_modify_eq_delay(phba, set_eqd, num);
5428 beiscsi_mccq_compl(phba, tag, NULL, NULL);
5432 static void be_check_boot_session(struct beiscsi_hba *phba)
5434 if (beiscsi_setup_boot_info(phba))
5435 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5436 "BM_%d : Could not set up "
5437 "iSCSI boot info on async event.\n");
5441 * beiscsi_hw_health_check()- Check adapter health
5442 * @work: work item to check HW health
5444 * Check if adapter in an unrecoverable state or not.
5447 beiscsi_hw_health_check(struct work_struct *work)
5449 struct beiscsi_hba *phba =
5450 container_of(work, struct beiscsi_hba,
5451 beiscsi_hw_check_task.work);
5453 be_eqd_update(phba);
5455 if (phba->state & BE_ADAPTER_CHECK_BOOT) {
5456 if ((phba->get_boot > 0) && (!phba->boot_kset)) {
5458 if (!(phba->get_boot % BE_GET_BOOT_TO))
5459 be_check_boot_session(phba);
5461 phba->state &= ~BE_ADAPTER_CHECK_BOOT;
5466 beiscsi_ue_detect(phba);
5468 schedule_delayed_work(&phba->beiscsi_hw_check_task,
5469 msecs_to_jiffies(1000));
5473 static pci_ers_result_t beiscsi_eeh_err_detected(struct pci_dev *pdev,
5474 pci_channel_state_t state)
5476 struct beiscsi_hba *phba = NULL;
5478 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
5479 phba->state |= BE_ADAPTER_PCI_ERR;
5481 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5482 "BM_%d : EEH error detected\n");
5484 beiscsi_quiesce(phba, BEISCSI_EEH_UNLOAD);
5486 if (state == pci_channel_io_perm_failure) {
5487 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5488 "BM_%d : EEH : State PERM Failure");
5489 return PCI_ERS_RESULT_DISCONNECT;
5492 pci_disable_device(pdev);
5494 /* The error could cause the FW to trigger a flash debug dump.
5495 * Resetting the card while flash dump is in progress
5496 * can cause it not to recover; wait for it to finish.
5497 * Wait only for first function as it is needed only once per
5500 if (pdev->devfn == 0)
5503 return PCI_ERS_RESULT_NEED_RESET;
5506 static pci_ers_result_t beiscsi_eeh_reset(struct pci_dev *pdev)
5508 struct beiscsi_hba *phba = NULL;
5511 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
5513 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5514 "BM_%d : EEH Reset\n");
5516 status = pci_enable_device(pdev);
5518 return PCI_ERS_RESULT_DISCONNECT;
5520 pci_set_master(pdev);
5521 pci_set_power_state(pdev, PCI_D0);
5522 pci_restore_state(pdev);
5524 /* Wait for the CHIP Reset to complete */
5525 status = be_chk_reset_complete(phba);
5527 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5528 "BM_%d : EEH Reset Completed\n");
5530 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5531 "BM_%d : EEH Reset Completion Failure\n");
5532 return PCI_ERS_RESULT_DISCONNECT;
5535 pci_cleanup_aer_uncorrect_error_status(pdev);
5536 return PCI_ERS_RESULT_RECOVERED;
5539 static void beiscsi_eeh_resume(struct pci_dev *pdev)
5542 struct be_eq_obj *pbe_eq;
5543 struct beiscsi_hba *phba = NULL;
5544 struct hwi_controller *phwi_ctrlr;
5545 struct hwi_context_memory *phwi_context;
5547 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
5548 pci_save_state(pdev);
5551 find_num_cpus(phba);
5556 beiscsi_msix_enable(phba);
5557 if (!phba->msix_enabled)
5561 ret = beiscsi_cmd_reset_function(phba);
5563 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5564 "BM_%d : Reset Failed\n");
5568 ret = be_chk_reset_complete(phba);
5570 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5571 "BM_%d : Failed to get out of reset.\n");
5575 beiscsi_get_params(phba);
5576 phba->shost->max_id = phba->params.cxns_per_ctrl;
5577 phba->shost->can_queue = phba->params.ios_per_ctrl;
5578 ret = hwi_init_controller(phba);
5580 for (i = 0; i < MAX_MCC_CMD; i++) {
5581 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
5582 phba->ctrl.mcc_tag[i] = i + 1;
5583 phba->ctrl.mcc_numtag[i + 1] = 0;
5584 phba->ctrl.mcc_tag_available++;
5587 phwi_ctrlr = phba->phwi_ctrlr;
5588 phwi_context = phwi_ctrlr->phwi_ctxt;
5590 for (i = 0; i < phba->num_cpus; i++) {
5591 pbe_eq = &phwi_context->be_eq[i];
5592 irq_poll_init(&pbe_eq->iopoll, be_iopoll_budget,
5596 i = (phba->msix_enabled) ? i : 0;
5597 /* Work item for MCC handling */
5598 pbe_eq = &phwi_context->be_eq[i];
5599 INIT_WORK(&pbe_eq->work_cqs, beiscsi_process_all_cqs);
5601 ret = beiscsi_init_irqs(phba);
5603 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5604 "BM_%d : beiscsi_eeh_resume - "
5605 "Failed to beiscsi_init_irqs\n");
5609 hwi_enable_intr(phba);
5610 phba->state &= ~BE_ADAPTER_PCI_ERR;
5614 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5615 "BM_%d : AER EEH Resume Failed\n");
5618 static int beiscsi_dev_probe(struct pci_dev *pcidev,
5619 const struct pci_device_id *id)
5621 struct beiscsi_hba *phba = NULL;
5622 struct hwi_controller *phwi_ctrlr;
5623 struct hwi_context_memory *phwi_context;
5624 struct be_eq_obj *pbe_eq;
5627 ret = beiscsi_enable_pci(pcidev);
5629 dev_err(&pcidev->dev,
5630 "beiscsi_dev_probe - Failed to enable pci device\n");
5634 phba = beiscsi_hba_alloc(pcidev);
5636 dev_err(&pcidev->dev,
5637 "beiscsi_dev_probe - Failed in beiscsi_hba_alloc\n");
5641 /* Enable EEH reporting */
5642 ret = pci_enable_pcie_error_reporting(pcidev);
5644 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5645 "BM_%d : PCIe Error Reporting "
5646 "Enabling Failed\n");
5648 pci_save_state(pcidev);
5650 /* Initialize Driver configuration Paramters */
5651 beiscsi_hba_attrs_init(phba);
5653 phba->fw_timeout = false;
5654 phba->mac_addr_set = false;
5657 switch (pcidev->device) {
5661 phba->generation = BE_GEN2;
5662 phba->iotask_fn = beiscsi_iotask;
5666 phba->generation = BE_GEN3;
5667 phba->iotask_fn = beiscsi_iotask;
5670 phba->generation = BE_GEN4;
5671 phba->iotask_fn = beiscsi_iotask_v2;
5674 phba->generation = 0;
5677 ret = be_ctrl_init(phba, pcidev);
5679 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5680 "BM_%d : beiscsi_dev_probe-"
5681 "Failed in be_ctrl_init\n");
5686 * FUNCTION_RESET should clean up any stale info in FW for this fn
5688 ret = beiscsi_cmd_reset_function(phba);
5690 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5691 "BM_%d : Reset Failed\n");
5694 ret = be_chk_reset_complete(phba);
5696 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5697 "BM_%d : Failed to get out of reset.\n");
5701 spin_lock_init(&phba->io_sgl_lock);
5702 spin_lock_init(&phba->mgmt_sgl_lock);
5703 spin_lock_init(&phba->isr_lock);
5704 spin_lock_init(&phba->async_pdu_lock);
5705 ret = mgmt_get_fw_config(&phba->ctrl, phba);
5707 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5708 "BM_%d : Error getting fw config\n");
5711 mgmt_get_port_name(&phba->ctrl, phba);
5712 beiscsi_get_params(phba);
5715 find_num_cpus(phba);
5719 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
5720 "BM_%d : num_cpus = %d\n",
5724 beiscsi_msix_enable(phba);
5725 if (!phba->msix_enabled)
5729 phba->shost->max_id = phba->params.cxns_per_ctrl;
5730 phba->shost->can_queue = phba->params.ios_per_ctrl;
5731 ret = beiscsi_init_port(phba);
5733 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5734 "BM_%d : beiscsi_dev_probe-"
5735 "Failed in beiscsi_init_port\n");
5739 for (i = 0; i < MAX_MCC_CMD; i++) {
5740 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
5741 phba->ctrl.mcc_tag[i] = i + 1;
5742 phba->ctrl.mcc_numtag[i + 1] = 0;
5743 phba->ctrl.mcc_tag_available++;
5744 memset(&phba->ctrl.ptag_state[i].tag_mem_state, 0,
5745 sizeof(struct be_dma_mem));
5748 phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
5750 snprintf(phba->wq_name, sizeof(phba->wq_name), "beiscsi_%02x_wq",
5751 phba->shost->host_no);
5752 phba->wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 1, phba->wq_name);
5754 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5755 "BM_%d : beiscsi_dev_probe-"
5756 "Failed to allocate work queue\n");
5760 INIT_DELAYED_WORK(&phba->beiscsi_hw_check_task,
5761 beiscsi_hw_health_check);
5763 phwi_ctrlr = phba->phwi_ctrlr;
5764 phwi_context = phwi_ctrlr->phwi_ctxt;
5766 for (i = 0; i < phba->num_cpus; i++) {
5767 pbe_eq = &phwi_context->be_eq[i];
5768 irq_poll_init(&pbe_eq->iopoll, be_iopoll_budget,
5772 i = (phba->msix_enabled) ? i : 0;
5773 /* Work item for MCC handling */
5774 pbe_eq = &phwi_context->be_eq[i];
5775 INIT_WORK(&pbe_eq->work_cqs, beiscsi_process_all_cqs);
5777 ret = beiscsi_init_irqs(phba);
5779 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5780 "BM_%d : beiscsi_dev_probe-"
5781 "Failed to beiscsi_init_irqs\n");
5784 hwi_enable_intr(phba);
5786 if (iscsi_host_add(phba->shost, &phba->pcidev->dev))
5789 if (beiscsi_setup_boot_info(phba))
5791 * log error but continue, because we may not be using
5794 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5795 "BM_%d : Could not set up "
5796 "iSCSI boot info.\n");
5798 beiscsi_create_def_ifaces(phba);
5799 schedule_delayed_work(&phba->beiscsi_hw_check_task,
5800 msecs_to_jiffies(1000));
5802 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
5803 "\n\n\n BM_%d : SUCCESS - DRIVER LOADED\n\n\n");
5807 destroy_workqueue(phba->wq);
5808 for (i = 0; i < phba->num_cpus; i++) {
5809 pbe_eq = &phwi_context->be_eq[i];
5810 irq_poll_disable(&pbe_eq->iopoll);
5813 beiscsi_clean_port(phba);
5814 beiscsi_free_mem(phba);
5816 pci_free_consistent(phba->pcidev,
5817 phba->ctrl.mbox_mem_alloced.size,
5818 phba->ctrl.mbox_mem_alloced.va,
5819 phba->ctrl.mbox_mem_alloced.dma);
5820 beiscsi_unmap_pci_function(phba);
5822 if (phba->msix_enabled)
5823 pci_disable_msix(phba->pcidev);
5824 pci_dev_put(phba->pcidev);
5825 iscsi_host_free(phba->shost);
5826 pci_set_drvdata(pcidev, NULL);
5828 pci_release_regions(pcidev);
5829 pci_disable_device(pcidev);
5833 static struct pci_error_handlers beiscsi_eeh_handlers = {
5834 .error_detected = beiscsi_eeh_err_detected,
5835 .slot_reset = beiscsi_eeh_reset,
5836 .resume = beiscsi_eeh_resume,
5839 struct iscsi_transport beiscsi_iscsi_transport = {
5840 .owner = THIS_MODULE,
5842 .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
5843 CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
5844 .create_session = beiscsi_session_create,
5845 .destroy_session = beiscsi_session_destroy,
5846 .create_conn = beiscsi_conn_create,
5847 .bind_conn = beiscsi_conn_bind,
5848 .destroy_conn = iscsi_conn_teardown,
5849 .attr_is_visible = be2iscsi_attr_is_visible,
5850 .set_iface_param = be2iscsi_iface_set_param,
5851 .get_iface_param = be2iscsi_iface_get_param,
5852 .set_param = beiscsi_set_param,
5853 .get_conn_param = iscsi_conn_get_param,
5854 .get_session_param = iscsi_session_get_param,
5855 .get_host_param = beiscsi_get_host_param,
5856 .start_conn = beiscsi_conn_start,
5857 .stop_conn = iscsi_conn_stop,
5858 .send_pdu = iscsi_conn_send_pdu,
5859 .xmit_task = beiscsi_task_xmit,
5860 .cleanup_task = beiscsi_cleanup_task,
5861 .alloc_pdu = beiscsi_alloc_pdu,
5862 .parse_pdu_itt = beiscsi_parse_pdu,
5863 .get_stats = beiscsi_conn_get_stats,
5864 .get_ep_param = beiscsi_ep_get_param,
5865 .ep_connect = beiscsi_ep_connect,
5866 .ep_poll = beiscsi_ep_poll,
5867 .ep_disconnect = beiscsi_ep_disconnect,
5868 .session_recovery_timedout = iscsi_session_recovery_timedout,
5869 .bsg_request = beiscsi_bsg_request,
5872 static struct pci_driver beiscsi_pci_driver = {
5874 .probe = beiscsi_dev_probe,
5875 .remove = beiscsi_remove,
5876 .id_table = beiscsi_pci_id_table,
5877 .err_handler = &beiscsi_eeh_handlers
5881 static int __init beiscsi_module_init(void)
5885 beiscsi_scsi_transport =
5886 iscsi_register_transport(&beiscsi_iscsi_transport);
5887 if (!beiscsi_scsi_transport) {
5889 "beiscsi_module_init - Unable to register beiscsi transport.\n");
5892 printk(KERN_INFO "In beiscsi_module_init, tt=%p\n",
5893 &beiscsi_iscsi_transport);
5895 ret = pci_register_driver(&beiscsi_pci_driver);
5898 "beiscsi_module_init - Unable to register beiscsi pci driver.\n");
5899 goto unregister_iscsi_transport;
5903 unregister_iscsi_transport:
5904 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5908 static void __exit beiscsi_module_exit(void)
5910 pci_unregister_driver(&beiscsi_pci_driver);
5911 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5914 module_init(beiscsi_module_init);
5915 module_exit(beiscsi_module_exit);