PageRenderTime 75ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/scsi/be2iscsi/be_main.c

https://bitbucket.org/cyanogenmod/android_kernel_asus_tf300t
C | 4461 lines | 3901 code | 471 blank | 89 comment | 425 complexity | 9f338297faf2b91c3eafe7613b4ab8e4 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  1. /**
  2. * Copyright (C) 2005 - 2011 Emulex
  3. * All rights reserved.
  4. *
  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.
  9. *
  10. * Written by: Jayamohan Kallickal (jayamohan.kallickal@emulex.com)
  11. *
  12. * Contact Information:
  13. * linux-drivers@emulex.com
  14. *
  15. * Emulex
  16. * 3333 Susan Street
  17. * Costa Mesa, CA 92626
  18. */
  19. #include <linux/reboot.h>
  20. #include <linux/delay.h>
  21. #include <linux/slab.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/blkdev.h>
  24. #include <linux/pci.h>
  25. #include <linux/string.h>
  26. #include <linux/kernel.h>
  27. #include <linux/semaphore.h>
  28. #include <linux/iscsi_boot_sysfs.h>
  29. #include <scsi/libiscsi.h>
  30. #include <scsi/scsi_transport_iscsi.h>
  31. #include <scsi/scsi_transport.h>
  32. #include <scsi/scsi_cmnd.h>
  33. #include <scsi/scsi_device.h>
  34. #include <scsi/scsi_host.h>
  35. #include <scsi/scsi.h>
  36. #include "be_main.h"
  37. #include "be_iscsi.h"
  38. #include "be_mgmt.h"
  39. static unsigned int be_iopoll_budget = 10;
  40. static unsigned int be_max_phys_size = 64;
  41. static unsigned int enable_msix = 1;
  42. static unsigned int gcrashmode = 0;
  43. static unsigned int num_hba = 0;
  44. MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
  45. MODULE_DESCRIPTION(DRV_DESC " " BUILD_STR);
  46. MODULE_AUTHOR("ServerEngines Corporation");
  47. MODULE_LICENSE("GPL");
  48. module_param(be_iopoll_budget, int, 0);
  49. module_param(enable_msix, int, 0);
  50. module_param(be_max_phys_size, uint, S_IRUGO);
  51. MODULE_PARM_DESC(be_max_phys_size, "Maximum Size (In Kilobytes) of physically"
  52. "contiguous memory that can be allocated."
  53. "Range is 16 - 128");
  54. static int beiscsi_slave_configure(struct scsi_device *sdev)
  55. {
  56. blk_queue_max_segment_size(sdev->request_queue, 65536);
  57. return 0;
  58. }
  59. static int beiscsi_eh_abort(struct scsi_cmnd *sc)
  60. {
  61. struct iscsi_cls_session *cls_session;
  62. struct iscsi_task *aborted_task = (struct iscsi_task *)sc->SCp.ptr;
  63. struct beiscsi_io_task *aborted_io_task;
  64. struct iscsi_conn *conn;
  65. struct beiscsi_conn *beiscsi_conn;
  66. struct beiscsi_hba *phba;
  67. struct iscsi_session *session;
  68. struct invalidate_command_table *inv_tbl;
  69. struct be_dma_mem nonemb_cmd;
  70. unsigned int cid, tag, num_invalidate;
  71. cls_session = starget_to_session(scsi_target(sc->device));
  72. session = cls_session->dd_data;
  73. spin_lock_bh(&session->lock);
  74. if (!aborted_task || !aborted_task->sc) {
  75. /* we raced */
  76. spin_unlock_bh(&session->lock);
  77. return SUCCESS;
  78. }
  79. aborted_io_task = aborted_task->dd_data;
  80. if (!aborted_io_task->scsi_cmnd) {
  81. /* raced or invalid command */
  82. spin_unlock_bh(&session->lock);
  83. return SUCCESS;
  84. }
  85. spin_unlock_bh(&session->lock);
  86. conn = aborted_task->conn;
  87. beiscsi_conn = conn->dd_data;
  88. phba = beiscsi_conn->phba;
  89. /* invalidate iocb */
  90. cid = beiscsi_conn->beiscsi_conn_cid;
  91. inv_tbl = phba->inv_tbl;
  92. memset(inv_tbl, 0x0, sizeof(*inv_tbl));
  93. inv_tbl->cid = cid;
  94. inv_tbl->icd = aborted_io_task->psgl_handle->sgl_index;
  95. num_invalidate = 1;
  96. nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
  97. sizeof(struct invalidate_commands_params_in),
  98. &nonemb_cmd.dma);
  99. if (nonemb_cmd.va == NULL) {
  100. SE_DEBUG(DBG_LVL_1,
  101. "Failed to allocate memory for"
  102. "mgmt_invalidate_icds\n");
  103. return FAILED;
  104. }
  105. nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
  106. tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
  107. cid, &nonemb_cmd);
  108. if (!tag) {
  109. shost_printk(KERN_WARNING, phba->shost,
  110. "mgmt_invalidate_icds could not be"
  111. " submitted\n");
  112. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  113. nonemb_cmd.va, nonemb_cmd.dma);
  114. return FAILED;
  115. } else {
  116. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  117. phba->ctrl.mcc_numtag[tag]);
  118. free_mcc_tag(&phba->ctrl, tag);
  119. }
  120. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  121. nonemb_cmd.va, nonemb_cmd.dma);
  122. return iscsi_eh_abort(sc);
  123. }
  124. static int beiscsi_eh_device_reset(struct scsi_cmnd *sc)
  125. {
  126. struct iscsi_task *abrt_task;
  127. struct beiscsi_io_task *abrt_io_task;
  128. struct iscsi_conn *conn;
  129. struct beiscsi_conn *beiscsi_conn;
  130. struct beiscsi_hba *phba;
  131. struct iscsi_session *session;
  132. struct iscsi_cls_session *cls_session;
  133. struct invalidate_command_table *inv_tbl;
  134. struct be_dma_mem nonemb_cmd;
  135. unsigned int cid, tag, i, num_invalidate;
  136. int rc = FAILED;
  137. /* invalidate iocbs */
  138. cls_session = starget_to_session(scsi_target(sc->device));
  139. session = cls_session->dd_data;
  140. spin_lock_bh(&session->lock);
  141. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
  142. goto unlock;
  143. conn = session->leadconn;
  144. beiscsi_conn = conn->dd_data;
  145. phba = beiscsi_conn->phba;
  146. cid = beiscsi_conn->beiscsi_conn_cid;
  147. inv_tbl = phba->inv_tbl;
  148. memset(inv_tbl, 0x0, sizeof(*inv_tbl) * BE2_CMDS_PER_CXN);
  149. num_invalidate = 0;
  150. for (i = 0; i < conn->session->cmds_max; i++) {
  151. abrt_task = conn->session->cmds[i];
  152. abrt_io_task = abrt_task->dd_data;
  153. if (!abrt_task->sc || abrt_task->state == ISCSI_TASK_FREE)
  154. continue;
  155. if (abrt_task->sc->device->lun != abrt_task->sc->device->lun)
  156. continue;
  157. inv_tbl->cid = cid;
  158. inv_tbl->icd = abrt_io_task->psgl_handle->sgl_index;
  159. num_invalidate++;
  160. inv_tbl++;
  161. }
  162. spin_unlock_bh(&session->lock);
  163. inv_tbl = phba->inv_tbl;
  164. nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
  165. sizeof(struct invalidate_commands_params_in),
  166. &nonemb_cmd.dma);
  167. if (nonemb_cmd.va == NULL) {
  168. SE_DEBUG(DBG_LVL_1,
  169. "Failed to allocate memory for"
  170. "mgmt_invalidate_icds\n");
  171. return FAILED;
  172. }
  173. nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
  174. memset(nonemb_cmd.va, 0, nonemb_cmd.size);
  175. tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
  176. cid, &nonemb_cmd);
  177. if (!tag) {
  178. shost_printk(KERN_WARNING, phba->shost,
  179. "mgmt_invalidate_icds could not be"
  180. " submitted\n");
  181. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  182. nonemb_cmd.va, nonemb_cmd.dma);
  183. return FAILED;
  184. } else {
  185. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  186. phba->ctrl.mcc_numtag[tag]);
  187. free_mcc_tag(&phba->ctrl, tag);
  188. }
  189. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  190. nonemb_cmd.va, nonemb_cmd.dma);
  191. return iscsi_eh_device_reset(sc);
  192. unlock:
  193. spin_unlock_bh(&session->lock);
  194. return rc;
  195. }
  196. static ssize_t beiscsi_show_boot_tgt_info(void *data, int type, char *buf)
  197. {
  198. struct beiscsi_hba *phba = data;
  199. struct mgmt_session_info *boot_sess = &phba->boot_sess;
  200. struct mgmt_conn_info *boot_conn = &boot_sess->conn_list[0];
  201. char *str = buf;
  202. int rc;
  203. switch (type) {
  204. case ISCSI_BOOT_TGT_NAME:
  205. rc = sprintf(buf, "%.*s\n",
  206. (int)strlen(boot_sess->target_name),
  207. (char *)&boot_sess->target_name);
  208. break;
  209. case ISCSI_BOOT_TGT_IP_ADDR:
  210. if (boot_conn->dest_ipaddr.ip_type == 0x1)
  211. rc = sprintf(buf, "%pI4\n",
  212. (char *)&boot_conn->dest_ipaddr.ip_address);
  213. else
  214. rc = sprintf(str, "%pI6\n",
  215. (char *)&boot_conn->dest_ipaddr.ip_address);
  216. break;
  217. case ISCSI_BOOT_TGT_PORT:
  218. rc = sprintf(str, "%d\n", boot_conn->dest_port);
  219. break;
  220. case ISCSI_BOOT_TGT_CHAP_NAME:
  221. rc = sprintf(str, "%.*s\n",
  222. boot_conn->negotiated_login_options.auth_data.chap.
  223. target_chap_name_length,
  224. (char *)&boot_conn->negotiated_login_options.
  225. auth_data.chap.target_chap_name);
  226. break;
  227. case ISCSI_BOOT_TGT_CHAP_SECRET:
  228. rc = sprintf(str, "%.*s\n",
  229. boot_conn->negotiated_login_options.auth_data.chap.
  230. target_secret_length,
  231. (char *)&boot_conn->negotiated_login_options.
  232. auth_data.chap.target_secret);
  233. break;
  234. case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  235. rc = sprintf(str, "%.*s\n",
  236. boot_conn->negotiated_login_options.auth_data.chap.
  237. intr_chap_name_length,
  238. (char *)&boot_conn->negotiated_login_options.
  239. auth_data.chap.intr_chap_name);
  240. break;
  241. case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  242. rc = sprintf(str, "%.*s\n",
  243. boot_conn->negotiated_login_options.auth_data.chap.
  244. intr_secret_length,
  245. (char *)&boot_conn->negotiated_login_options.
  246. auth_data.chap.intr_secret);
  247. break;
  248. case ISCSI_BOOT_TGT_FLAGS:
  249. rc = sprintf(str, "2\n");
  250. break;
  251. case ISCSI_BOOT_TGT_NIC_ASSOC:
  252. rc = sprintf(str, "0\n");
  253. break;
  254. default:
  255. rc = -ENOSYS;
  256. break;
  257. }
  258. return rc;
  259. }
  260. static ssize_t beiscsi_show_boot_ini_info(void *data, int type, char *buf)
  261. {
  262. struct beiscsi_hba *phba = data;
  263. char *str = buf;
  264. int rc;
  265. switch (type) {
  266. case ISCSI_BOOT_INI_INITIATOR_NAME:
  267. rc = sprintf(str, "%s\n", phba->boot_sess.initiator_iscsiname);
  268. break;
  269. default:
  270. rc = -ENOSYS;
  271. break;
  272. }
  273. return rc;
  274. }
  275. static ssize_t beiscsi_show_boot_eth_info(void *data, int type, char *buf)
  276. {
  277. struct beiscsi_hba *phba = data;
  278. char *str = buf;
  279. int rc;
  280. switch (type) {
  281. case ISCSI_BOOT_ETH_FLAGS:
  282. rc = sprintf(str, "2\n");
  283. break;
  284. case ISCSI_BOOT_ETH_INDEX:
  285. rc = sprintf(str, "0\n");
  286. break;
  287. case ISCSI_BOOT_ETH_MAC:
  288. rc = beiscsi_get_macaddr(buf, phba);
  289. if (rc < 0) {
  290. SE_DEBUG(DBG_LVL_1, "beiscsi_get_macaddr Failed\n");
  291. return rc;
  292. }
  293. break;
  294. default:
  295. rc = -ENOSYS;
  296. break;
  297. }
  298. return rc;
  299. }
  300. static mode_t beiscsi_tgt_get_attr_visibility(void *data, int type)
  301. {
  302. int rc;
  303. switch (type) {
  304. case ISCSI_BOOT_TGT_NAME:
  305. case ISCSI_BOOT_TGT_IP_ADDR:
  306. case ISCSI_BOOT_TGT_PORT:
  307. case ISCSI_BOOT_TGT_CHAP_NAME:
  308. case ISCSI_BOOT_TGT_CHAP_SECRET:
  309. case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  310. case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  311. case ISCSI_BOOT_TGT_NIC_ASSOC:
  312. case ISCSI_BOOT_TGT_FLAGS:
  313. rc = S_IRUGO;
  314. break;
  315. default:
  316. rc = 0;
  317. break;
  318. }
  319. return rc;
  320. }
  321. static mode_t beiscsi_ini_get_attr_visibility(void *data, int type)
  322. {
  323. int rc;
  324. switch (type) {
  325. case ISCSI_BOOT_INI_INITIATOR_NAME:
  326. rc = S_IRUGO;
  327. break;
  328. default:
  329. rc = 0;
  330. break;
  331. }
  332. return rc;
  333. }
  334. static mode_t beiscsi_eth_get_attr_visibility(void *data, int type)
  335. {
  336. int rc;
  337. switch (type) {
  338. case ISCSI_BOOT_ETH_FLAGS:
  339. case ISCSI_BOOT_ETH_MAC:
  340. case ISCSI_BOOT_ETH_INDEX:
  341. rc = S_IRUGO;
  342. break;
  343. default:
  344. rc = 0;
  345. break;
  346. }
  347. return rc;
  348. }
  349. /*------------------- PCI Driver operations and data ----------------- */
  350. static DEFINE_PCI_DEVICE_TABLE(beiscsi_pci_id_table) = {
  351. { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
  352. { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
  353. { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
  354. { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
  355. { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID3) },
  356. { 0 }
  357. };
  358. MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
  359. static struct scsi_host_template beiscsi_sht = {
  360. .module = THIS_MODULE,
  361. .name = "ServerEngines 10Gbe open-iscsi Initiator Driver",
  362. .proc_name = DRV_NAME,
  363. .queuecommand = iscsi_queuecommand,
  364. .change_queue_depth = iscsi_change_queue_depth,
  365. .slave_configure = beiscsi_slave_configure,
  366. .target_alloc = iscsi_target_alloc,
  367. .eh_abort_handler = beiscsi_eh_abort,
  368. .eh_device_reset_handler = beiscsi_eh_device_reset,
  369. .eh_target_reset_handler = iscsi_eh_session_reset,
  370. .sg_tablesize = BEISCSI_SGLIST_ELEMENTS,
  371. .can_queue = BE2_IO_DEPTH,
  372. .this_id = -1,
  373. .max_sectors = BEISCSI_MAX_SECTORS,
  374. .cmd_per_lun = BEISCSI_CMD_PER_LUN,
  375. .use_clustering = ENABLE_CLUSTERING,
  376. };
  377. static struct scsi_transport_template *beiscsi_scsi_transport;
  378. static struct beiscsi_hba *beiscsi_hba_alloc(struct pci_dev *pcidev)
  379. {
  380. struct beiscsi_hba *phba;
  381. struct Scsi_Host *shost;
  382. shost = iscsi_host_alloc(&beiscsi_sht, sizeof(*phba), 0);
  383. if (!shost) {
  384. dev_err(&pcidev->dev, "beiscsi_hba_alloc -"
  385. "iscsi_host_alloc failed\n");
  386. return NULL;
  387. }
  388. shost->dma_boundary = pcidev->dma_mask;
  389. shost->max_id = BE2_MAX_SESSIONS;
  390. shost->max_channel = 0;
  391. shost->max_cmd_len = BEISCSI_MAX_CMD_LEN;
  392. shost->max_lun = BEISCSI_NUM_MAX_LUN;
  393. shost->transportt = beiscsi_scsi_transport;
  394. phba = iscsi_host_priv(shost);
  395. memset(phba, 0, sizeof(*phba));
  396. phba->shost = shost;
  397. phba->pcidev = pci_dev_get(pcidev);
  398. pci_set_drvdata(pcidev, phba);
  399. if (iscsi_host_add(shost, &phba->pcidev->dev))
  400. goto free_devices;
  401. return phba;
  402. free_devices:
  403. pci_dev_put(phba->pcidev);
  404. iscsi_host_free(phba->shost);
  405. return NULL;
  406. }
  407. static void beiscsi_unmap_pci_function(struct beiscsi_hba *phba)
  408. {
  409. if (phba->csr_va) {
  410. iounmap(phba->csr_va);
  411. phba->csr_va = NULL;
  412. }
  413. if (phba->db_va) {
  414. iounmap(phba->db_va);
  415. phba->db_va = NULL;
  416. }
  417. if (phba->pci_va) {
  418. iounmap(phba->pci_va);
  419. phba->pci_va = NULL;
  420. }
  421. }
  422. static int beiscsi_map_pci_bars(struct beiscsi_hba *phba,
  423. struct pci_dev *pcidev)
  424. {
  425. u8 __iomem *addr;
  426. int pcicfg_reg;
  427. addr = ioremap_nocache(pci_resource_start(pcidev, 2),
  428. pci_resource_len(pcidev, 2));
  429. if (addr == NULL)
  430. return -ENOMEM;
  431. phba->ctrl.csr = addr;
  432. phba->csr_va = addr;
  433. phba->csr_pa.u.a64.address = pci_resource_start(pcidev, 2);
  434. addr = ioremap_nocache(pci_resource_start(pcidev, 4), 128 * 1024);
  435. if (addr == NULL)
  436. goto pci_map_err;
  437. phba->ctrl.db = addr;
  438. phba->db_va = addr;
  439. phba->db_pa.u.a64.address = pci_resource_start(pcidev, 4);
  440. if (phba->generation == BE_GEN2)
  441. pcicfg_reg = 1;
  442. else
  443. pcicfg_reg = 0;
  444. addr = ioremap_nocache(pci_resource_start(pcidev, pcicfg_reg),
  445. pci_resource_len(pcidev, pcicfg_reg));
  446. if (addr == NULL)
  447. goto pci_map_err;
  448. phba->ctrl.pcicfg = addr;
  449. phba->pci_va = addr;
  450. phba->pci_pa.u.a64.address = pci_resource_start(pcidev, pcicfg_reg);
  451. return 0;
  452. pci_map_err:
  453. beiscsi_unmap_pci_function(phba);
  454. return -ENOMEM;
  455. }
  456. static int beiscsi_enable_pci(struct pci_dev *pcidev)
  457. {
  458. int ret;
  459. ret = pci_enable_device(pcidev);
  460. if (ret) {
  461. dev_err(&pcidev->dev, "beiscsi_enable_pci - enable device "
  462. "failed. Returning -ENODEV\n");
  463. return ret;
  464. }
  465. pci_set_master(pcidev);
  466. if (pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(64))) {
  467. ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(32));
  468. if (ret) {
  469. dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
  470. pci_disable_device(pcidev);
  471. return ret;
  472. }
  473. }
  474. return 0;
  475. }
  476. static int be_ctrl_init(struct beiscsi_hba *phba, struct pci_dev *pdev)
  477. {
  478. struct be_ctrl_info *ctrl = &phba->ctrl;
  479. struct be_dma_mem *mbox_mem_alloc = &ctrl->mbox_mem_alloced;
  480. struct be_dma_mem *mbox_mem_align = &ctrl->mbox_mem;
  481. int status = 0;
  482. ctrl->pdev = pdev;
  483. status = beiscsi_map_pci_bars(phba, pdev);
  484. if (status)
  485. return status;
  486. mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
  487. mbox_mem_alloc->va = pci_alloc_consistent(pdev,
  488. mbox_mem_alloc->size,
  489. &mbox_mem_alloc->dma);
  490. if (!mbox_mem_alloc->va) {
  491. beiscsi_unmap_pci_function(phba);
  492. status = -ENOMEM;
  493. return status;
  494. }
  495. mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
  496. mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
  497. mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
  498. memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
  499. spin_lock_init(&ctrl->mbox_lock);
  500. spin_lock_init(&phba->ctrl.mcc_lock);
  501. spin_lock_init(&phba->ctrl.mcc_cq_lock);
  502. return status;
  503. }
  504. static void beiscsi_get_params(struct beiscsi_hba *phba)
  505. {
  506. phba->params.ios_per_ctrl = (phba->fw_config.iscsi_icd_count
  507. - (phba->fw_config.iscsi_cid_count
  508. + BE2_TMFS
  509. + BE2_NOPOUT_REQ));
  510. phba->params.cxns_per_ctrl = phba->fw_config.iscsi_cid_count;
  511. phba->params.asyncpdus_per_ctrl = phba->fw_config.iscsi_cid_count * 2;
  512. phba->params.icds_per_ctrl = phba->fw_config.iscsi_icd_count;
  513. phba->params.num_sge_per_io = BE2_SGE;
  514. phba->params.defpdu_hdr_sz = BE2_DEFPDU_HDR_SZ;
  515. phba->params.defpdu_data_sz = BE2_DEFPDU_DATA_SZ;
  516. phba->params.eq_timer = 64;
  517. phba->params.num_eq_entries =
  518. (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
  519. + BE2_TMFS) / 512) + 1) * 512;
  520. phba->params.num_eq_entries = (phba->params.num_eq_entries < 1024)
  521. ? 1024 : phba->params.num_eq_entries;
  522. SE_DEBUG(DBG_LVL_8, "phba->params.num_eq_entries=%d\n",
  523. phba->params.num_eq_entries);
  524. phba->params.num_cq_entries =
  525. (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
  526. + BE2_TMFS) / 512) + 1) * 512;
  527. phba->params.wrbs_per_cxn = 256;
  528. }
  529. static void hwi_ring_eq_db(struct beiscsi_hba *phba,
  530. unsigned int id, unsigned int clr_interrupt,
  531. unsigned int num_processed,
  532. unsigned char rearm, unsigned char event)
  533. {
  534. u32 val = 0;
  535. val |= id & DB_EQ_RING_ID_MASK;
  536. if (rearm)
  537. val |= 1 << DB_EQ_REARM_SHIFT;
  538. if (clr_interrupt)
  539. val |= 1 << DB_EQ_CLR_SHIFT;
  540. if (event)
  541. val |= 1 << DB_EQ_EVNT_SHIFT;
  542. val |= num_processed << DB_EQ_NUM_POPPED_SHIFT;
  543. iowrite32(val, phba->db_va + DB_EQ_OFFSET);
  544. }
  545. /**
  546. * be_isr_mcc - The isr routine of the driver.
  547. * @irq: Not used
  548. * @dev_id: Pointer to host adapter structure
  549. */
  550. static irqreturn_t be_isr_mcc(int irq, void *dev_id)
  551. {
  552. struct beiscsi_hba *phba;
  553. struct be_eq_entry *eqe = NULL;
  554. struct be_queue_info *eq;
  555. struct be_queue_info *mcc;
  556. unsigned int num_eq_processed;
  557. struct be_eq_obj *pbe_eq;
  558. unsigned long flags;
  559. pbe_eq = dev_id;
  560. eq = &pbe_eq->q;
  561. phba = pbe_eq->phba;
  562. mcc = &phba->ctrl.mcc_obj.cq;
  563. eqe = queue_tail_node(eq);
  564. if (!eqe)
  565. SE_DEBUG(DBG_LVL_1, "eqe is NULL\n");
  566. num_eq_processed = 0;
  567. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  568. & EQE_VALID_MASK) {
  569. if (((eqe->dw[offsetof(struct amap_eq_entry,
  570. resource_id) / 32] &
  571. EQE_RESID_MASK) >> 16) == mcc->id) {
  572. spin_lock_irqsave(&phba->isr_lock, flags);
  573. phba->todo_mcc_cq = 1;
  574. spin_unlock_irqrestore(&phba->isr_lock, flags);
  575. }
  576. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  577. queue_tail_inc(eq);
  578. eqe = queue_tail_node(eq);
  579. num_eq_processed++;
  580. }
  581. if (phba->todo_mcc_cq)
  582. queue_work(phba->wq, &phba->work_cqs);
  583. if (num_eq_processed)
  584. hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 1, 1);
  585. return IRQ_HANDLED;
  586. }
  587. /**
  588. * be_isr_msix - The isr routine of the driver.
  589. * @irq: Not used
  590. * @dev_id: Pointer to host adapter structure
  591. */
  592. static irqreturn_t be_isr_msix(int irq, void *dev_id)
  593. {
  594. struct beiscsi_hba *phba;
  595. struct be_eq_entry *eqe = NULL;
  596. struct be_queue_info *eq;
  597. struct be_queue_info *cq;
  598. unsigned int num_eq_processed;
  599. struct be_eq_obj *pbe_eq;
  600. unsigned long flags;
  601. pbe_eq = dev_id;
  602. eq = &pbe_eq->q;
  603. cq = pbe_eq->cq;
  604. eqe = queue_tail_node(eq);
  605. if (!eqe)
  606. SE_DEBUG(DBG_LVL_1, "eqe is NULL\n");
  607. phba = pbe_eq->phba;
  608. num_eq_processed = 0;
  609. if (blk_iopoll_enabled) {
  610. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  611. & EQE_VALID_MASK) {
  612. if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
  613. blk_iopoll_sched(&pbe_eq->iopoll);
  614. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  615. queue_tail_inc(eq);
  616. eqe = queue_tail_node(eq);
  617. num_eq_processed++;
  618. }
  619. if (num_eq_processed)
  620. hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 0, 1);
  621. return IRQ_HANDLED;
  622. } else {
  623. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  624. & EQE_VALID_MASK) {
  625. spin_lock_irqsave(&phba->isr_lock, flags);
  626. phba->todo_cq = 1;
  627. spin_unlock_irqrestore(&phba->isr_lock, flags);
  628. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  629. queue_tail_inc(eq);
  630. eqe = queue_tail_node(eq);
  631. num_eq_processed++;
  632. }
  633. if (phba->todo_cq)
  634. queue_work(phba->wq, &phba->work_cqs);
  635. if (num_eq_processed)
  636. hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 1, 1);
  637. return IRQ_HANDLED;
  638. }
  639. }
  640. /**
  641. * be_isr - The isr routine of the driver.
  642. * @irq: Not used
  643. * @dev_id: Pointer to host adapter structure
  644. */
  645. static irqreturn_t be_isr(int irq, void *dev_id)
  646. {
  647. struct beiscsi_hba *phba;
  648. struct hwi_controller *phwi_ctrlr;
  649. struct hwi_context_memory *phwi_context;
  650. struct be_eq_entry *eqe = NULL;
  651. struct be_queue_info *eq;
  652. struct be_queue_info *cq;
  653. struct be_queue_info *mcc;
  654. unsigned long flags, index;
  655. unsigned int num_mcceq_processed, num_ioeq_processed;
  656. struct be_ctrl_info *ctrl;
  657. struct be_eq_obj *pbe_eq;
  658. int isr;
  659. phba = dev_id;
  660. ctrl = &phba->ctrl;
  661. isr = ioread32(ctrl->csr + CEV_ISR0_OFFSET +
  662. (PCI_FUNC(ctrl->pdev->devfn) * CEV_ISR_SIZE));
  663. if (!isr)
  664. return IRQ_NONE;
  665. phwi_ctrlr = phba->phwi_ctrlr;
  666. phwi_context = phwi_ctrlr->phwi_ctxt;
  667. pbe_eq = &phwi_context->be_eq[0];
  668. eq = &phwi_context->be_eq[0].q;
  669. mcc = &phba->ctrl.mcc_obj.cq;
  670. index = 0;
  671. eqe = queue_tail_node(eq);
  672. if (!eqe)
  673. SE_DEBUG(DBG_LVL_1, "eqe is NULL\n");
  674. num_ioeq_processed = 0;
  675. num_mcceq_processed = 0;
  676. if (blk_iopoll_enabled) {
  677. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  678. & EQE_VALID_MASK) {
  679. if (((eqe->dw[offsetof(struct amap_eq_entry,
  680. resource_id) / 32] &
  681. EQE_RESID_MASK) >> 16) == mcc->id) {
  682. spin_lock_irqsave(&phba->isr_lock, flags);
  683. phba->todo_mcc_cq = 1;
  684. spin_unlock_irqrestore(&phba->isr_lock, flags);
  685. num_mcceq_processed++;
  686. } else {
  687. if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
  688. blk_iopoll_sched(&pbe_eq->iopoll);
  689. num_ioeq_processed++;
  690. }
  691. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  692. queue_tail_inc(eq);
  693. eqe = queue_tail_node(eq);
  694. }
  695. if (num_ioeq_processed || num_mcceq_processed) {
  696. if (phba->todo_mcc_cq)
  697. queue_work(phba->wq, &phba->work_cqs);
  698. if ((num_mcceq_processed) && (!num_ioeq_processed))
  699. hwi_ring_eq_db(phba, eq->id, 0,
  700. (num_ioeq_processed +
  701. num_mcceq_processed) , 1, 1);
  702. else
  703. hwi_ring_eq_db(phba, eq->id, 0,
  704. (num_ioeq_processed +
  705. num_mcceq_processed), 0, 1);
  706. return IRQ_HANDLED;
  707. } else
  708. return IRQ_NONE;
  709. } else {
  710. cq = &phwi_context->be_cq[0];
  711. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  712. & EQE_VALID_MASK) {
  713. if (((eqe->dw[offsetof(struct amap_eq_entry,
  714. resource_id) / 32] &
  715. EQE_RESID_MASK) >> 16) != cq->id) {
  716. spin_lock_irqsave(&phba->isr_lock, flags);
  717. phba->todo_mcc_cq = 1;
  718. spin_unlock_irqrestore(&phba->isr_lock, flags);
  719. } else {
  720. spin_lock_irqsave(&phba->isr_lock, flags);
  721. phba->todo_cq = 1;
  722. spin_unlock_irqrestore(&phba->isr_lock, flags);
  723. }
  724. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  725. queue_tail_inc(eq);
  726. eqe = queue_tail_node(eq);
  727. num_ioeq_processed++;
  728. }
  729. if (phba->todo_cq || phba->todo_mcc_cq)
  730. queue_work(phba->wq, &phba->work_cqs);
  731. if (num_ioeq_processed) {
  732. hwi_ring_eq_db(phba, eq->id, 0,
  733. num_ioeq_processed, 1, 1);
  734. return IRQ_HANDLED;
  735. } else
  736. return IRQ_NONE;
  737. }
  738. }
  739. static int beiscsi_init_irqs(struct beiscsi_hba *phba)
  740. {
  741. struct pci_dev *pcidev = phba->pcidev;
  742. struct hwi_controller *phwi_ctrlr;
  743. struct hwi_context_memory *phwi_context;
  744. int ret, msix_vec, i, j;
  745. char desc[32];
  746. phwi_ctrlr = phba->phwi_ctrlr;
  747. phwi_context = phwi_ctrlr->phwi_ctxt;
  748. if (phba->msix_enabled) {
  749. for (i = 0; i < phba->num_cpus; i++) {
  750. sprintf(desc, "beiscsi_msix_%04x", i);
  751. msix_vec = phba->msix_entries[i].vector;
  752. ret = request_irq(msix_vec, be_isr_msix, 0, desc,
  753. &phwi_context->be_eq[i]);
  754. if (ret) {
  755. shost_printk(KERN_ERR, phba->shost,
  756. "beiscsi_init_irqs-Failed to"
  757. "register msix for i = %d\n", i);
  758. if (!i)
  759. return ret;
  760. goto free_msix_irqs;
  761. }
  762. }
  763. msix_vec = phba->msix_entries[i].vector;
  764. ret = request_irq(msix_vec, be_isr_mcc, 0, "beiscsi_msix_mcc",
  765. &phwi_context->be_eq[i]);
  766. if (ret) {
  767. shost_printk(KERN_ERR, phba->shost, "beiscsi_init_irqs-"
  768. "Failed to register beiscsi_msix_mcc\n");
  769. i++;
  770. goto free_msix_irqs;
  771. }
  772. } else {
  773. ret = request_irq(pcidev->irq, be_isr, IRQF_SHARED,
  774. "beiscsi", phba);
  775. if (ret) {
  776. shost_printk(KERN_ERR, phba->shost, "beiscsi_init_irqs-"
  777. "Failed to register irq\\n");
  778. return ret;
  779. }
  780. }
  781. return 0;
  782. free_msix_irqs:
  783. for (j = i - 1; j == 0; j++)
  784. free_irq(msix_vec, &phwi_context->be_eq[j]);
  785. return ret;
  786. }
  787. static void hwi_ring_cq_db(struct beiscsi_hba *phba,
  788. unsigned int id, unsigned int num_processed,
  789. unsigned char rearm, unsigned char event)
  790. {
  791. u32 val = 0;
  792. val |= id & DB_CQ_RING_ID_MASK;
  793. if (rearm)
  794. val |= 1 << DB_CQ_REARM_SHIFT;
  795. val |= num_processed << DB_CQ_NUM_POPPED_SHIFT;
  796. iowrite32(val, phba->db_va + DB_CQ_OFFSET);
  797. }
  798. static unsigned int
  799. beiscsi_process_async_pdu(struct beiscsi_conn *beiscsi_conn,
  800. struct beiscsi_hba *phba,
  801. unsigned short cid,
  802. struct pdu_base *ppdu,
  803. unsigned long pdu_len,
  804. void *pbuffer, unsigned long buf_len)
  805. {
  806. struct iscsi_conn *conn = beiscsi_conn->conn;
  807. struct iscsi_session *session = conn->session;
  808. struct iscsi_task *task;
  809. struct beiscsi_io_task *io_task;
  810. struct iscsi_hdr *login_hdr;
  811. switch (ppdu->dw[offsetof(struct amap_pdu_base, opcode) / 32] &
  812. PDUBASE_OPCODE_MASK) {
  813. case ISCSI_OP_NOOP_IN:
  814. pbuffer = NULL;
  815. buf_len = 0;
  816. break;
  817. case ISCSI_OP_ASYNC_EVENT:
  818. break;
  819. case ISCSI_OP_REJECT:
  820. WARN_ON(!pbuffer);
  821. WARN_ON(!(buf_len == 48));
  822. SE_DEBUG(DBG_LVL_1, "In ISCSI_OP_REJECT\n");
  823. break;
  824. case ISCSI_OP_LOGIN_RSP:
  825. case ISCSI_OP_TEXT_RSP:
  826. task = conn->login_task;
  827. io_task = task->dd_data;
  828. login_hdr = (struct iscsi_hdr *)ppdu;
  829. login_hdr->itt = io_task->libiscsi_itt;
  830. break;
  831. default:
  832. shost_printk(KERN_WARNING, phba->shost,
  833. "Unrecognized opcode 0x%x in async msg\n",
  834. (ppdu->
  835. dw[offsetof(struct amap_pdu_base, opcode) / 32]
  836. & PDUBASE_OPCODE_MASK));
  837. return 1;
  838. }
  839. spin_lock_bh(&session->lock);
  840. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)ppdu, pbuffer, buf_len);
  841. spin_unlock_bh(&session->lock);
  842. return 0;
  843. }
  844. static struct sgl_handle *alloc_io_sgl_handle(struct beiscsi_hba *phba)
  845. {
  846. struct sgl_handle *psgl_handle;
  847. if (phba->io_sgl_hndl_avbl) {
  848. SE_DEBUG(DBG_LVL_8,
  849. "In alloc_io_sgl_handle,io_sgl_alloc_index=%d\n",
  850. phba->io_sgl_alloc_index);
  851. psgl_handle = phba->io_sgl_hndl_base[phba->
  852. io_sgl_alloc_index];
  853. phba->io_sgl_hndl_base[phba->io_sgl_alloc_index] = NULL;
  854. phba->io_sgl_hndl_avbl--;
  855. if (phba->io_sgl_alloc_index == (phba->params.
  856. ios_per_ctrl - 1))
  857. phba->io_sgl_alloc_index = 0;
  858. else
  859. phba->io_sgl_alloc_index++;
  860. } else
  861. psgl_handle = NULL;
  862. return psgl_handle;
  863. }
  864. static void
  865. free_io_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
  866. {
  867. SE_DEBUG(DBG_LVL_8, "In free_,io_sgl_free_index=%d\n",
  868. phba->io_sgl_free_index);
  869. if (phba->io_sgl_hndl_base[phba->io_sgl_free_index]) {
  870. /*
  871. * this can happen if clean_task is called on a task that
  872. * failed in xmit_task or alloc_pdu.
  873. */
  874. SE_DEBUG(DBG_LVL_8,
  875. "Double Free in IO SGL io_sgl_free_index=%d,"
  876. "value there=%p\n", phba->io_sgl_free_index,
  877. phba->io_sgl_hndl_base[phba->io_sgl_free_index]);
  878. return;
  879. }
  880. phba->io_sgl_hndl_base[phba->io_sgl_free_index] = psgl_handle;
  881. phba->io_sgl_hndl_avbl++;
  882. if (phba->io_sgl_free_index == (phba->params.ios_per_ctrl - 1))
  883. phba->io_sgl_free_index = 0;
  884. else
  885. phba->io_sgl_free_index++;
  886. }
  887. /**
  888. * alloc_wrb_handle - To allocate a wrb handle
  889. * @phba: The hba pointer
  890. * @cid: The cid to use for allocation
  891. *
  892. * This happens under session_lock until submission to chip
  893. */
  894. struct wrb_handle *alloc_wrb_handle(struct beiscsi_hba *phba, unsigned int cid)
  895. {
  896. struct hwi_wrb_context *pwrb_context;
  897. struct hwi_controller *phwi_ctrlr;
  898. struct wrb_handle *pwrb_handle, *pwrb_handle_tmp;
  899. phwi_ctrlr = phba->phwi_ctrlr;
  900. pwrb_context = &phwi_ctrlr->wrb_context[cid];
  901. if (pwrb_context->wrb_handles_available >= 2) {
  902. pwrb_handle = pwrb_context->pwrb_handle_base[
  903. pwrb_context->alloc_index];
  904. pwrb_context->wrb_handles_available--;
  905. if (pwrb_context->alloc_index ==
  906. (phba->params.wrbs_per_cxn - 1))
  907. pwrb_context->alloc_index = 0;
  908. else
  909. pwrb_context->alloc_index++;
  910. pwrb_handle_tmp = pwrb_context->pwrb_handle_base[
  911. pwrb_context->alloc_index];
  912. pwrb_handle->nxt_wrb_index = pwrb_handle_tmp->wrb_index;
  913. } else
  914. pwrb_handle = NULL;
  915. return pwrb_handle;
  916. }
  917. /**
  918. * free_wrb_handle - To free the wrb handle back to pool
  919. * @phba: The hba pointer
  920. * @pwrb_context: The context to free from
  921. * @pwrb_handle: The wrb_handle to free
  922. *
  923. * This happens under session_lock until submission to chip
  924. */
  925. static void
  926. free_wrb_handle(struct beiscsi_hba *phba, struct hwi_wrb_context *pwrb_context,
  927. struct wrb_handle *pwrb_handle)
  928. {
  929. pwrb_context->pwrb_handle_base[pwrb_context->free_index] = pwrb_handle;
  930. pwrb_context->wrb_handles_available++;
  931. if (pwrb_context->free_index == (phba->params.wrbs_per_cxn - 1))
  932. pwrb_context->free_index = 0;
  933. else
  934. pwrb_context->free_index++;
  935. SE_DEBUG(DBG_LVL_8,
  936. "FREE WRB: pwrb_handle=%p free_index=0x%x"
  937. "wrb_handles_available=%d\n",
  938. pwrb_handle, pwrb_context->free_index,
  939. pwrb_context->wrb_handles_available);
  940. }
  941. static struct sgl_handle *alloc_mgmt_sgl_handle(struct beiscsi_hba *phba)
  942. {
  943. struct sgl_handle *psgl_handle;
  944. if (phba->eh_sgl_hndl_avbl) {
  945. psgl_handle = phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index];
  946. phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index] = NULL;
  947. SE_DEBUG(DBG_LVL_8, "mgmt_sgl_alloc_index=%d=0x%x\n",
  948. phba->eh_sgl_alloc_index, phba->eh_sgl_alloc_index);
  949. phba->eh_sgl_hndl_avbl--;
  950. if (phba->eh_sgl_alloc_index ==
  951. (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl -
  952. 1))
  953. phba->eh_sgl_alloc_index = 0;
  954. else
  955. phba->eh_sgl_alloc_index++;
  956. } else
  957. psgl_handle = NULL;
  958. return psgl_handle;
  959. }
  960. void
  961. free_mgmt_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
  962. {
  963. SE_DEBUG(DBG_LVL_8, "In free_mgmt_sgl_handle,eh_sgl_free_index=%d\n",
  964. phba->eh_sgl_free_index);
  965. if (phba->eh_sgl_hndl_base[phba->eh_sgl_free_index]) {
  966. /*
  967. * this can happen if clean_task is called on a task that
  968. * failed in xmit_task or alloc_pdu.
  969. */
  970. SE_DEBUG(DBG_LVL_8,
  971. "Double Free in eh SGL ,eh_sgl_free_index=%d\n",
  972. phba->eh_sgl_free_index);
  973. return;
  974. }
  975. phba->eh_sgl_hndl_base[phba->eh_sgl_free_index] = psgl_handle;
  976. phba->eh_sgl_hndl_avbl++;
  977. if (phba->eh_sgl_free_index ==
  978. (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl - 1))
  979. phba->eh_sgl_free_index = 0;
  980. else
  981. phba->eh_sgl_free_index++;
  982. }
  983. static void
  984. be_complete_io(struct beiscsi_conn *beiscsi_conn,
  985. struct iscsi_task *task, struct sol_cqe *psol)
  986. {
  987. struct beiscsi_io_task *io_task = task->dd_data;
  988. struct be_status_bhs *sts_bhs =
  989. (struct be_status_bhs *)io_task->cmd_bhs;
  990. struct iscsi_conn *conn = beiscsi_conn->conn;
  991. unsigned int sense_len;
  992. unsigned char *sense;
  993. u32 resid = 0, exp_cmdsn, max_cmdsn;
  994. u8 rsp, status, flags;
  995. exp_cmdsn = (psol->
  996. dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
  997. & SOL_EXP_CMD_SN_MASK);
  998. max_cmdsn = ((psol->
  999. dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
  1000. & SOL_EXP_CMD_SN_MASK) +
  1001. ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
  1002. / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
  1003. rsp = ((psol->dw[offsetof(struct amap_sol_cqe, i_resp) / 32]
  1004. & SOL_RESP_MASK) >> 16);
  1005. status = ((psol->dw[offsetof(struct amap_sol_cqe, i_sts) / 32]
  1006. & SOL_STS_MASK) >> 8);
  1007. flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
  1008. & SOL_FLAGS_MASK) >> 24) | 0x80;
  1009. task->sc->result = (DID_OK << 16) | status;
  1010. if (rsp != ISCSI_STATUS_CMD_COMPLETED) {
  1011. task->sc->result = DID_ERROR << 16;
  1012. goto unmap;
  1013. }
  1014. /* bidi not initially supported */
  1015. if (flags & (ISCSI_FLAG_CMD_UNDERFLOW | ISCSI_FLAG_CMD_OVERFLOW)) {
  1016. resid = (psol->dw[offsetof(struct amap_sol_cqe, i_res_cnt) /
  1017. 32] & SOL_RES_CNT_MASK);
  1018. if (!status && (flags & ISCSI_FLAG_CMD_OVERFLOW))
  1019. task->sc->result = DID_ERROR << 16;
  1020. if (flags & ISCSI_FLAG_CMD_UNDERFLOW) {
  1021. scsi_set_resid(task->sc, resid);
  1022. if (!status && (scsi_bufflen(task->sc) - resid <
  1023. task->sc->underflow))
  1024. task->sc->result = DID_ERROR << 16;
  1025. }
  1026. }
  1027. if (status == SAM_STAT_CHECK_CONDITION) {
  1028. unsigned short *slen = (unsigned short *)sts_bhs->sense_info;
  1029. sense = sts_bhs->sense_info + sizeof(unsigned short);
  1030. sense_len = cpu_to_be16(*slen);
  1031. memcpy(task->sc->sense_buffer, sense,
  1032. min_t(u16, sense_len, SCSI_SENSE_BUFFERSIZE));
  1033. }
  1034. if (io_task->cmd_bhs->iscsi_hdr.flags & ISCSI_FLAG_CMD_READ) {
  1035. if (psol->dw[offsetof(struct amap_sol_cqe, i_res_cnt) / 32]
  1036. & SOL_RES_CNT_MASK)
  1037. conn->rxdata_octets += (psol->
  1038. dw[offsetof(struct amap_sol_cqe, i_res_cnt) / 32]
  1039. & SOL_RES_CNT_MASK);
  1040. }
  1041. unmap:
  1042. scsi_dma_unmap(io_task->scsi_cmnd);
  1043. iscsi_complete_scsi_task(task, exp_cmdsn, max_cmdsn);
  1044. }
  1045. static void
  1046. be_complete_logout(struct beiscsi_conn *beiscsi_conn,
  1047. struct iscsi_task *task, struct sol_cqe *psol)
  1048. {
  1049. struct iscsi_logout_rsp *hdr;
  1050. struct beiscsi_io_task *io_task = task->dd_data;
  1051. struct iscsi_conn *conn = beiscsi_conn->conn;
  1052. hdr = (struct iscsi_logout_rsp *)task->hdr;
  1053. hdr->opcode = ISCSI_OP_LOGOUT_RSP;
  1054. hdr->t2wait = 5;
  1055. hdr->t2retain = 0;
  1056. hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
  1057. & SOL_FLAGS_MASK) >> 24) | 0x80;
  1058. hdr->response = (psol->dw[offsetof(struct amap_sol_cqe, i_resp) /
  1059. 32] & SOL_RESP_MASK);
  1060. hdr->exp_cmdsn = cpu_to_be32(psol->
  1061. dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
  1062. & SOL_EXP_CMD_SN_MASK);
  1063. hdr->max_cmdsn = be32_to_cpu((psol->
  1064. dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
  1065. & SOL_EXP_CMD_SN_MASK) +
  1066. ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
  1067. / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
  1068. hdr->dlength[0] = 0;
  1069. hdr->dlength[1] = 0;
  1070. hdr->dlength[2] = 0;
  1071. hdr->hlength = 0;
  1072. hdr->itt = io_task->libiscsi_itt;
  1073. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
  1074. }
  1075. static void
  1076. be_complete_tmf(struct beiscsi_conn *beiscsi_conn,
  1077. struct iscsi_task *task, struct sol_cqe *psol)
  1078. {
  1079. struct iscsi_tm_rsp *hdr;
  1080. struct iscsi_conn *conn = beiscsi_conn->conn;
  1081. struct beiscsi_io_task *io_task = task->dd_data;
  1082. hdr = (struct iscsi_tm_rsp *)task->hdr;
  1083. hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
  1084. hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
  1085. & SOL_FLAGS_MASK) >> 24) | 0x80;
  1086. hdr->response = (psol->dw[offsetof(struct amap_sol_cqe, i_resp) /
  1087. 32] & SOL_RESP_MASK);
  1088. hdr->exp_cmdsn = cpu_to_be32(psol->dw[offsetof(struct amap_sol_cqe,
  1089. i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK);
  1090. hdr->max_cmdsn = be32_to_cpu((psol->dw[offsetof(struct amap_sol_cqe,
  1091. i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK) +
  1092. ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
  1093. / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
  1094. hdr->itt = io_task->libiscsi_itt;
  1095. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
  1096. }
  1097. static void
  1098. hwi_complete_drvr_msgs(struct beiscsi_conn *beiscsi_conn,
  1099. struct beiscsi_hba *phba, struct sol_cqe *psol)
  1100. {
  1101. struct hwi_wrb_context *pwrb_context;
  1102. struct wrb_handle *pwrb_handle = NULL;
  1103. struct hwi_controller *phwi_ctrlr;
  1104. struct iscsi_task *task;
  1105. struct beiscsi_io_task *io_task;
  1106. struct iscsi_conn *conn = beiscsi_conn->conn;
  1107. struct iscsi_session *session = conn->session;
  1108. phwi_ctrlr = phba->phwi_ctrlr;
  1109. pwrb_context = &phwi_ctrlr->wrb_context[((psol->
  1110. dw[offsetof(struct amap_sol_cqe, cid) / 32] &
  1111. SOL_CID_MASK) >> 6) -
  1112. phba->fw_config.iscsi_cid_start];
  1113. pwrb_handle = pwrb_context->pwrb_handle_basestd[((psol->
  1114. dw[offsetof(struct amap_sol_cqe, wrb_index) /
  1115. 32] & SOL_WRB_INDEX_MASK) >> 16)];
  1116. task = pwrb_handle->pio_handle;
  1117. io_task = task->dd_data;
  1118. spin_lock(&phba->mgmt_sgl_lock);
  1119. free_mgmt_sgl_handle(phba, io_task->psgl_handle);
  1120. spin_unlock(&phba->mgmt_sgl_lock);
  1121. spin_lock_bh(&session->lock);
  1122. free_wrb_handle(phba, pwrb_context, pwrb_handle);
  1123. spin_unlock_bh(&session->lock);
  1124. }
  1125. static void
  1126. be_complete_nopin_resp(struct beiscsi_conn *beiscsi_conn,
  1127. struct iscsi_task *task, struct sol_cqe *psol)
  1128. {
  1129. struct iscsi_nopin *hdr;
  1130. struct iscsi_conn *conn = beiscsi_conn->conn;
  1131. struct beiscsi_io_task *io_task = task->dd_data;
  1132. hdr = (struct iscsi_nopin *)task->hdr;
  1133. hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
  1134. & SOL_FLAGS_MASK) >> 24) | 0x80;
  1135. hdr->exp_cmdsn = cpu_to_be32(psol->dw[offsetof(struct amap_sol_cqe,
  1136. i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK);
  1137. hdr->max_cmdsn = be32_to_cpu((psol->dw[offsetof(struct amap_sol_cqe,
  1138. i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK) +
  1139. ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
  1140. / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
  1141. hdr->opcode = ISCSI_OP_NOOP_IN;
  1142. hdr->itt = io_task->libiscsi_itt;
  1143. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
  1144. }
  1145. static void hwi_complete_cmd(struct beiscsi_conn *beiscsi_conn,
  1146. struct beiscsi_hba *phba, struct sol_cqe *psol)
  1147. {
  1148. struct hwi_wrb_context *pwrb_context;
  1149. struct wrb_handle *pwrb_handle;
  1150. struct iscsi_wrb *pwrb = NULL;
  1151. struct hwi_controller *phwi_ctrlr;
  1152. struct iscsi_task *task;
  1153. unsigned int type;
  1154. struct iscsi_conn *conn = beiscsi_conn->conn;
  1155. struct iscsi_session *session = conn->session;
  1156. phwi_ctrlr = phba->phwi_ctrlr;
  1157. pwrb_context = &phwi_ctrlr->wrb_context[((psol->dw[offsetof
  1158. (struct amap_sol_cqe, cid) / 32]
  1159. & SOL_CID_MASK) >> 6) -
  1160. phba->fw_config.iscsi_cid_start];
  1161. pwrb_handle = pwrb_context->pwrb_handle_basestd[((psol->
  1162. dw[offsetof(struct amap_sol_cqe, wrb_index) /
  1163. 32] & SOL_WRB_INDEX_MASK) >> 16)];
  1164. task = pwrb_handle->pio_handle;
  1165. pwrb = pwrb_handle->pwrb;
  1166. type = (pwrb->dw[offsetof(struct amap_iscsi_wrb, type) / 32] &
  1167. WRB_TYPE_MASK) >> 28;
  1168. spin_lock_bh(&session->lock);
  1169. switch (type) {
  1170. case HWH_TYPE_IO:
  1171. case HWH_TYPE_IO_RD:
  1172. if ((task->hdr->opcode & ISCSI_OPCODE_MASK) ==
  1173. ISCSI_OP_NOOP_OUT)
  1174. be_complete_nopin_resp(beiscsi_conn, task, psol);
  1175. else
  1176. be_complete_io(beiscsi_conn, task, psol);
  1177. break;
  1178. case HWH_TYPE_LOGOUT:
  1179. if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
  1180. be_complete_logout(beiscsi_conn, task, psol);
  1181. else
  1182. be_complete_tmf(beiscsi_conn, task, psol);
  1183. break;
  1184. case HWH_TYPE_LOGIN:
  1185. SE_DEBUG(DBG_LVL_1,
  1186. "\t\t No HWH_TYPE_LOGIN Expected in hwi_complete_cmd"
  1187. "- Solicited path\n");
  1188. break;
  1189. case HWH_TYPE_NOP:
  1190. be_complete_nopin_resp(beiscsi_conn, task, psol);
  1191. break;
  1192. default:
  1193. shost_printk(KERN_WARNING, phba->shost,
  1194. "In hwi_complete_cmd, unknown type = %d"
  1195. "wrb_index 0x%x CID 0x%x\n", type,
  1196. ((psol->dw[offsetof(struct amap_iscsi_wrb,
  1197. type) / 32] & SOL_WRB_INDEX_MASK) >> 16),
  1198. ((psol->dw[offsetof(struct amap_sol_cqe,
  1199. cid) / 32] & SOL_CID_MASK) >> 6));
  1200. break;
  1201. }
  1202. spin_unlock_bh(&session->lock);
  1203. }
  1204. static struct list_head *hwi_get_async_busy_list(struct hwi_async_pdu_context
  1205. *pasync_ctx, unsigned int is_header,
  1206. unsigned int host_write_ptr)
  1207. {
  1208. if (is_header)
  1209. return &pasync_ctx->async_entry[host_write_ptr].
  1210. header_busy_list;
  1211. else
  1212. return &pasync_ctx->async_entry[host_write_ptr].data_busy_list;
  1213. }
  1214. static struct async_pdu_handle *
  1215. hwi_get_async_handle(struct beiscsi_hba *phba,
  1216. struct beiscsi_conn *beiscsi_conn,
  1217. struct hwi_async_pdu_context *pasync_ctx,
  1218. struct i_t_dpdu_cqe *pdpdu_cqe, unsigned int *pcq_index)
  1219. {
  1220. struct be_bus_address phys_addr;
  1221. struct list_head *pbusy_list;
  1222. struct async_pdu_handle *pasync_handle = NULL;
  1223. int buffer_len = 0;
  1224. unsigned char buffer_index = -1;
  1225. unsigned char is_header = 0;
  1226. phys_addr.u.a32.address_lo =
  1227. pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, db_addr_lo) / 32] -
  1228. ((pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, dpl) / 32]
  1229. & PDUCQE_DPL_MASK) >> 16);
  1230. phys_addr.u.a32.address_hi =
  1231. pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, db_addr_hi) / 32];
  1232. phys_addr.u.a64.address =
  1233. *((unsigned long long *)(&phys_addr.u.a64.address));
  1234. switch (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, code) / 32]
  1235. & PDUCQE_CODE_MASK) {
  1236. case UNSOL_HDR_NOTIFY:
  1237. is_header = 1;
  1238. pbusy_list = hwi_get_async_busy_list(pasync_ctx, 1,
  1239. (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
  1240. index) / 32] & PDUCQE_INDEX_MASK));
  1241. buffer_len = (unsigned int)(phys_addr.u.a64.address -
  1242. pasync_ctx->async_header.pa_base.u.a64.address);
  1243. buffer_index = buffer_len /
  1244. pasync_ctx->async_header.buffer_size;
  1245. break;
  1246. case UNSOL_DATA_NOTIFY:
  1247. pbusy_list = hwi_get_async_busy_list(pasync_ctx, 0, (pdpdu_cqe->
  1248. dw[offsetof(struct amap_i_t_dpdu_cqe,
  1249. index) / 32] & PDUCQE_INDEX_MASK));
  1250. buffer_len = (unsigned long)(phys_addr.u.a64.address -
  1251. pasync_ctx->async_data.pa_base.u.
  1252. a64.address);
  1253. buffer_index = buffer_len / pasync_ctx->async_data.buffer_size;
  1254. break;
  1255. default:
  1256. pbusy_list = NULL;
  1257. shost_printk(KERN_WARNING, phba->shost,
  1258. "Unexpected code=%d\n",
  1259. pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
  1260. code) / 32] & PDUCQE_CODE_MASK);
  1261. return NULL;
  1262. }
  1263. WARN_ON(!(buffer_index <= pasync_ctx->async_data.num_entries));
  1264. WARN_ON(list_empty(pbusy_list));
  1265. list_for_each_entry(pasync_handle, pbusy_list, link) {
  1266. WARN_ON(pasync_handle->consumed);
  1267. if (pasync_handle->index == buffer_index)
  1268. break;
  1269. }
  1270. WARN_ON(!pasync_handle);
  1271. pasync_handle->cri = (unsigned short)beiscsi_conn->beiscsi_conn_cid -
  1272. phba->fw_config.iscsi_cid_start;
  1273. pasync_handle->is_header = is_header;
  1274. pasync_handle->buffer_len = ((pdpdu_cqe->
  1275. dw[offsetof(struct amap_i_t_dpdu_cqe, dpl) / 32]
  1276. & PDUCQE_DPL_MASK) >> 16);
  1277. *pcq_index = (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
  1278. index) / 32] & PDUCQE_INDEX_MASK);
  1279. return pasync_handle;
  1280. }
  1281. static unsigned int
  1282. hwi_update_async_writables(struct hwi_async_pdu_context *pasync_ctx,
  1283. unsigned int is_header, unsigned int cq_index)
  1284. {
  1285. struct list_head *pbusy_list;
  1286. struct async_pdu_handle *pasync_handle;
  1287. unsigned int num_entries, writables = 0;
  1288. unsigned int *pep_read_ptr, *pwritables;
  1289. if (is_header) {
  1290. pep_read_ptr = &pasync_ctx->async_header.ep_read_ptr;
  1291. pwritables = &pasync_ctx->async_header.writables;
  1292. num_entries = pasync_ctx->async_header.num_entries;
  1293. } else {
  1294. pep_read_ptr = &pasync_ctx->async_data.ep_read_ptr;
  1295. pwritables = &pasync_ctx->async_data.writables;
  1296. num_entries = pasync_ctx->async_data.num_entries;
  1297. }
  1298. while ((*pep_read_ptr) != cq_index) {
  1299. (*pep_read_ptr)++;
  1300. *pep_read_ptr = (*pep_read_ptr) % num_entries;
  1301. pbusy_list = hwi_get_async_busy_list(pasync_ctx, is_header,
  1302. *pep_read_ptr);
  1303. if (writables == 0)
  1304. WARN_ON(list_empty(pbusy_list));
  1305. if (!list_empty(pbusy_list)) {
  1306. pasync_handle = list_entry(pbusy_list->next,
  1307. struct async_pdu_handle,
  1308. link);
  1309. WARN_ON(!pasync_handle);
  1310. pasync_handle->consumed = 1;
  1311. }
  1312. writables++;
  1313. }
  1314. if (!writables) {
  1315. SE_DEBUG(DBG_LVL_1,
  1316. "Duplicate notification received - index 0x%x!!\n",
  1317. cq_index);
  1318. WARN_ON(1);
  1319. }
  1320. *pwritables = *pwritables + writables;
  1321. return 0;
  1322. }
  1323. static unsigned int hwi_free_async_msg(struct beiscsi_hba *phba,
  1324. unsigned int cri)
  1325. {
  1326. struct hwi_controller *phwi_ctrlr;
  1327. struct hwi_async_pdu_context *pasync_ctx;
  1328. struct async_pdu_handle *pasync_handle, *tmp_handle;
  1329. struct list_head *plist;
  1330. unsigned int i = 0;
  1331. phwi_ctrlr = phba->phwi_ctrlr;
  1332. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
  1333. plist = &pasync_ctx->async_entry[cri].wait_queue.list;
  1334. list_for_each_entry_safe(pasync_handle, tmp_handle, plist, link) {
  1335. list_del(&pasync_handle->link);
  1336. if (i == 0) {
  1337. list_add_tail(&pasync_handle->link,
  1338. &pasync_ctx->async_header.free_list);
  1339. pasync_ctx->async_header.free_entries++;
  1340. i++;
  1341. } else {
  1342. list_add_tail(&pasync_handle->link,
  1343. &pasync_ctx->async_data.free_list);
  1344. pasync_ctx->async_data.free_entries++;
  1345. i++;
  1346. }
  1347. }
  1348. INIT_LIST_HEAD(&pasync_ctx->async_entry[cri].wait_queue.list);
  1349. pasync_ctx->async_entry[cri].wait_queue.hdr_received = 0;
  1350. pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
  1351. return 0;
  1352. }
  1353. static struct phys_addr *
  1354. hwi_get_ring_address(struct hwi_async_pdu_context *pasync_ctx,
  1355. unsigned int is_header, unsigned int host_write_ptr)
  1356. {
  1357. struct phys_addr *pasync_sge = NULL;
  1358. if (is_header)
  1359. pasync_sge = pasync_ctx->async_header.ring_base;
  1360. else
  1361. pasync_sge = pasync_ctx->async_data.ring_base;
  1362. return pasync_sge + host_write_ptr;
  1363. }
  1364. static void hwi_post_async_buffers(struct beiscsi_hba *phba,
  1365. unsigned int is_header)
  1366. {
  1367. struct hwi_controller *phwi_ctrlr;
  1368. struct hwi_async_pdu_context *pasync_ctx;
  1369. struct async_pdu_handle *pasync_handle;
  1370. struct list_head *pfree_link, *pbusy_list;
  1371. struct phys_addr *pasync_sge;
  1372. unsigned int ring_id, num_entries;
  1373. unsigned int host_write_num;
  1374. unsigned int writables;
  1375. unsigned int i = 0;
  1376. u32 doorbell = 0;
  1377. phwi_ctrlr = phba->phwi_ctrlr;
  1378. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
  1379. if (is_header) {
  1380. num_entries = pasync_ctx->async_header.num_entries;
  1381. writables = min(pasync_ctx->async_header.writables,
  1382. pasync_ctx->async_header.free_entries);
  1383. pfree_link = pasync_ctx->async_header.free_list.next;
  1384. host_write_num = pasync_ctx->async_header.host_write_ptr;
  1385. ring_id = phwi_ctrlr->default_pdu_hdr.id;
  1386. } else {
  1387. num_entries = pasync_ctx->async_data.num_entries;
  1388. writables = min(pasync_ctx->async_data.writables,
  1389. pasync_ctx->async_data.free_entries);
  1390. pfree_link = pasync_ctx->async_data.free_list.next;
  1391. host_write_num = pasync_ctx->async_data.host_write_ptr;
  1392. ring_id = phwi_ctrlr->default_pdu_data.id;
  1393. }
  1394. writables = (writables / 8) * 8;
  1395. if (writables) {
  1396. for (i = 0; i < writables; i++) {
  1397. pbusy_list =
  1398. hwi_get_async_busy_list(pasync_ctx, is_header,
  1399. host_write_num);
  1400. pasync_handle =
  1401. list_entry(pfree_link, struct async_pdu_handle,
  1402. link);
  1403. WARN_ON(!pasync_handle);
  1404. pasync_handle->consumed = 0;
  1405. pfree_link = pfree_link->next;
  1406. pasync_sge = hwi_get_ring_address(pasync_ctx,
  1407. is_header, host_write_num);
  1408. pasync_sge->hi = pasync_handle->pa.u.a32.address_lo;
  1409. pasync_sge->lo = pasync_handle->pa.u.a32.address_hi;
  1410. list_move(&pasync_handle->link, pbusy_list);
  1411. host_write_num++;
  1412. host_write_num = host_write_num % num_entries;
  1413. }
  1414. if (is_header) {
  1415. pasync_ctx->async_header.host_write_ptr =
  1416. host_write_num;
  1417. pasync_ctx->async_header.free_entries -= writables;
  1418. pasync_ctx->async_header.writables -= writables;
  1419. pasync_ctx->async_header.busy_entries += writables;
  1420. } else {
  1421. pasync_ctx->async_data.host_write_ptr = host_write_num;
  1422. pasync_ctx->async_data.free_entries -= writables;
  1423. pasync_ctx->async_data.writables -= writables;
  1424. pasync_ctx->async_data.busy_entries += writables;
  1425. }
  1426. doorbell |= ring_id & DB_DEF_PDU_RING_ID_MASK;
  1427. doorbell |= 1 << DB_DEF_PDU_REARM_SHIFT;
  1428. doorbell |= 0 << DB_DEF_PDU_EVENT_SHIFT;
  1429. doorbell |= (writables & DB_DEF_PDU_CQPROC_MASK)
  1430. << DB_DEF_PDU_CQPROC_SHIFT;
  1431. iowrite32(doorbell, phba->db_va + DB_RXULP0_OFFSET);
  1432. }
  1433. }
  1434. static void hwi_flush_default_pdu_buffer(struct beiscsi_hba *phba,
  1435. struct beiscsi_conn *beiscsi_conn,
  1436. struct i_t_dpdu_cqe *pdpdu_cqe)
  1437. {
  1438. struct hwi_controller *phwi_ctrlr;
  1439. struct hwi_async_pdu_context *pasync_ctx;
  1440. struct async_pdu_handle *pasync_handle = NULL;
  1441. unsigned int cq_index = -1;
  1442. phwi_ctrlr = phba->phwi_ctrlr;
  1443. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
  1444. pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
  1445. pdpdu_cqe, &cq_index);
  1446. BUG_ON(pasync_handle->is_header != 0);
  1447. if (pasync_handle->consumed == 0)
  1448. hwi_update_async_writables(pasync_ctx, pasync_handle->is_header,
  1449. cq_index);
  1450. hwi_free_async_msg(phba, pasync_handle->cri);
  1451. hwi_post_async_buffers(phba, pasync_handle->is_header);
  1452. }
  1453. static unsigned int
  1454. hwi_fwd_async_msg(struct beiscsi_conn *beiscsi_conn,
  1455. struct beiscsi_hba *phba,
  1456. struct hwi_async_pdu_context *pasync_ctx, unsigned short cri)
  1457. {
  1458. struct list_head *plist;
  1459. struct async_pdu_handle *pasync_handle;
  1460. void *phdr = NULL;
  1461. unsigned int hdr_len = 0, buf_len = 0;
  1462. unsigned int status, index = 0, offset = 0;
  1463. void *pfirst_buffer = NULL;
  1464. unsigned int num_buf = 0;
  1465. plist = &pasync_ctx->async_entry[cri].wait_queue.list;
  1466. list_for_each_entry(pasync_handle, plist, link) {
  1467. if (index == 0) {
  1468. phdr = pasync_handle->pbuffer;
  1469. hdr_len = pasync_handle->buffer_len;
  1470. } else {
  1471. buf_len = pasync_handle->buffer_len;
  1472. if (!num_buf) {
  1473. pfirst_buffer = pasync_handle->pbuffer;
  1474. num_buf++;
  1475. }
  1476. memcpy(pfirst_buffer + offset,
  1477. pasync_handle->pbuffer, buf_len);
  1478. offset = buf_len;
  1479. }
  1480. index++;
  1481. }
  1482. status = beiscsi_process_async_pdu(beiscsi_conn, phba,
  1483. (beiscsi_conn->beiscsi_conn_cid -
  1484. phba->fw_config.iscsi_cid_start),
  1485. phdr, hdr_len, pfirst_buffer,
  1486. buf_len);
  1487. if (status == 0)
  1488. hwi_free_async_msg(phba, cri);
  1489. return 0;
  1490. }
  1491. static unsigned int
  1492. hwi_gather_async_pdu(struct beiscsi_conn *beiscsi_conn,
  1493. struct beiscsi_hba *phba,
  1494. struct async_pdu_handle *pasync_handle)
  1495. {
  1496. struct hwi_async_pdu_context *pasync_ctx;
  1497. struct hwi_controller *phwi_ctrlr;
  1498. unsigned int bytes_needed = 0, status = 0;
  1499. unsigned short cri = pasync_handle->cri;
  1500. struct pdu_base *ppdu;
  1501. phwi_ctrlr = phba->phwi_ctrlr;
  1502. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
  1503. list_del(&pasync_handle->link);
  1504. if (pasync_handle->is_header) {
  1505. pasync_ctx->async_header.busy_entries--;
  1506. if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
  1507. hwi_free_async_msg(phba, cri);
  1508. BUG();
  1509. }
  1510. pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
  1511. pasync_ctx->async_entry[cri].wait_queue.hdr_received = 1;
  1512. pasync_ctx->async_entry[cri].wait_queue.hdr_len =
  1513. (unsigned short)pasync_handle->buffer_len;
  1514. list_add_tail(&pasync_handle->link,
  1515. &pasync_ctx->async_entry[cri].wait_queue.list);
  1516. ppdu = pasync_handle->pbuffer;
  1517. bytes_needed = ((((ppdu->dw[offsetof(struct amap_pdu_base,
  1518. data_len_hi) / 32] & PDUBASE_DATALENHI_MASK) << 8) &
  1519. 0xFFFF0000) | ((be16_to_cpu((ppdu->
  1520. dw[offsetof(struct amap_pdu_base, data_len_lo) / 32]
  1521. & PDUBASE_DATALENLO_MASK) >> 16)) & 0x0000FFFF));
  1522. if (status == 0) {
  1523. pasync_ctx->async_entry[cri].wait_queue.bytes_needed =
  1524. bytes_needed;
  1525. if (bytes_needed == 0)
  1526. status = hwi_fwd_async_msg(beiscsi_conn, phba,
  1527. pasync_ctx, cri);
  1528. }
  1529. } else {
  1530. pasync_ctx->async_data.busy_entries--;
  1531. if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
  1532. list_add_tail(&pasync_handle->link,
  1533. &pasync_ctx->async_entry[cri].wait_queue.
  1534. list);
  1535. pasync_ctx->async_entry[cri].wait_queue.
  1536. bytes_received +=
  1537. (unsigned short)pasync_handle->buffer_len;
  1538. if (pasync_ctx->async_entry[cri].wait_queue.
  1539. bytes_received >=
  1540. pasync_ctx->async_entry[cri].wait_queue.
  1541. bytes_needed)
  1542. status = hwi_fwd_async_msg(beiscsi_conn, phba,
  1543. pasync_ctx, cri);
  1544. }
  1545. }
  1546. return status;
  1547. }
  1548. static void hwi_process_default_pdu_ring(struct beiscsi_conn *beiscsi_conn,
  1549. struct beiscsi_hba *phba,
  1550. struct i_t_dpdu_cqe *pdpdu_cqe)
  1551. {
  1552. struct hwi_controller *phwi_ctrlr;
  1553. struct hwi_async_pdu_context *pasync_ctx;
  1554. struct async_pdu_handle *pasync_handle = NULL;
  1555. unsigned int cq_index = -1;
  1556. phwi_ctrlr = phba->phwi_ctrlr;
  1557. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
  1558. pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
  1559. pdpdu_cqe, &cq_index);
  1560. if (pasync_handle->consumed == 0)
  1561. hwi_update_async_writables(pasync_ctx, pasync_handle->is_header,
  1562. cq_index);
  1563. hwi_gather_async_pdu(beiscsi_conn, phba, pasync_handle);
  1564. hwi_post_async_buffers(phba, pasync_handle->is_header);
  1565. }
  1566. static void beiscsi_process_mcc_isr(struct beiscsi_hba *phba)
  1567. {
  1568. struct be_queue_info *mcc_cq;
  1569. struct be_mcc_compl *mcc_compl;
  1570. unsigned int num_processed = 0;
  1571. mcc_cq = &phba->ctrl.mcc_obj.cq;
  1572. mcc_compl = queue_tail_node(mcc_cq);
  1573. mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
  1574. while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
  1575. if (num_processed >= 32) {
  1576. hwi_ring_cq_db(phba, mcc_cq->id,
  1577. num_processed, 0, 0);
  1578. num_processed = 0;
  1579. }
  1580. if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
  1581. /* Interpret flags as an async trailer */
  1582. if (is_link_state_evt(mcc_compl->flags))
  1583. /* Interpret compl as a async link evt */
  1584. beiscsi_async_link_state_process(phba,
  1585. (struct be_async_event_link_state *) mcc_compl);
  1586. else
  1587. SE_DEBUG(DBG_LVL_1,
  1588. " Unsupported Async Event, flags"
  1589. " = 0x%08x\n", mcc_compl->flags);
  1590. } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
  1591. be_mcc_compl_process_isr(&phba->ctrl, mcc_compl);
  1592. atomic_dec(&phba->ctrl.mcc_obj.q.used);
  1593. }
  1594. mcc_compl->flags = 0;
  1595. queue_tail_inc(mcc_cq);
  1596. mcc_compl = queue_tail_node(mcc_cq);
  1597. mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
  1598. num_processed++;
  1599. }
  1600. if (num_processed > 0)
  1601. hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1, 0);
  1602. }
  1603. static unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq)
  1604. {
  1605. struct be_queue_info *cq;
  1606. struct sol_cqe *sol;
  1607. struct dmsg_cqe *dmsg;
  1608. unsigned int num_processed = 0;
  1609. unsigned int tot_nump = 0;
  1610. struct beiscsi_conn *beiscsi_conn;
  1611. struct beiscsi_endpoint *beiscsi_ep;
  1612. struct iscsi_endpoint *ep;
  1613. struct beiscsi_hba *phba;
  1614. cq = pbe_eq->cq;
  1615. sol = queue_tail_node(cq);
  1616. phba = pbe_eq->phba;
  1617. while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
  1618. CQE_VALID_MASK) {
  1619. be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
  1620. ep = phba->ep_array[(u32) ((sol->
  1621. dw[offsetof(struct amap_sol_cqe, cid) / 32] &
  1622. SOL_CID_MASK) >> 6) -
  1623. phba->fw_config.iscsi_cid_start];
  1624. beiscsi_ep = ep->dd_data;
  1625. beiscsi_conn = beiscsi_ep->conn;
  1626. if (num_processed >= 32) {
  1627. hwi_ring_cq_db(phba, cq->id,
  1628. num_processed, 0, 0);
  1629. tot_nump += num_processed;
  1630. num_processed = 0;
  1631. }
  1632. switch ((u32) sol->dw[offsetof(struct amap_sol_cqe, code) /
  1633. 32] & CQE_CODE_MASK) {
  1634. case SOL_CMD_COMPLETE:
  1635. hwi_complete_cmd(beiscsi_conn, phba, sol);
  1636. break;
  1637. case DRIVERMSG_NOTIFY:
  1638. SE_DEBUG(DBG_LVL_8, "Received DRIVERMSG_NOTIFY\n");
  1639. dmsg = (struct dmsg_cqe *)sol;
  1640. hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
  1641. break;
  1642. case UNSOL_HDR_NOTIFY:
  1643. SE_DEBUG(DBG_LVL_8, "Received UNSOL_HDR_ NOTIFY\n");
  1644. hwi_process_default_pdu_ring(beiscsi_conn, phba,
  1645. (struct i_t_dpdu_cqe *)sol);
  1646. break;
  1647. case UNSOL_DATA_NOTIFY:
  1648. SE_DEBUG(DBG_LVL_8, "Received UNSOL_DATA_NOTIFY\n");
  1649. hwi_process_default_pdu_ring(beiscsi_conn, phba,
  1650. (struct i_t_dpdu_cqe *)sol);
  1651. break;
  1652. case CXN_INVALIDATE_INDEX_NOTIFY:
  1653. case CMD_INVALIDATED_NOTIFY:
  1654. case CXN_INVALIDATE_NOTIFY:
  1655. SE_DEBUG(DBG_LVL_1,
  1656. "Ignoring CQ Error notification for cmd/cxn"
  1657. "invalidate\n");
  1658. break;
  1659. case SOL_CMD_KILLED_DATA_DIGEST_ERR:
  1660. case CMD_KILLED_INVALID_STATSN_RCVD:
  1661. case CMD_KILLED_INVALID_R2T_RCVD:
  1662. case CMD_CXN_KILLED_LUN_INVALID:
  1663. case CMD_CXN_KILLED_ICD_INVALID:
  1664. case CMD_CXN_KILLED_ITT_INVALID:
  1665. case CMD_CXN_KILLED_SEQ_OUTOFORDER:
  1666. case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
  1667. SE_DEBUG(DBG_LVL_1,
  1668. "CQ Error notification for cmd.. "
  1669. "code %d cid 0x%x\n",
  1670. sol->dw[offsetof(struct amap_sol_cqe, code) /
  1671. 32] & CQE_CODE_MASK,
  1672. (sol->dw[offsetof(struct amap_sol_cqe, cid) /
  1673. 32] & SOL_CID_MASK));
  1674. break;
  1675. case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
  1676. SE_DEBUG(DBG_LVL_1,
  1677. "Digest error on def pdu ring, dropping..\n");
  1678. hwi_flush_default_pdu_buffer(phba, beiscsi_conn,
  1679. (struct i_t_dpdu_cqe *) sol);
  1680. break;
  1681. case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
  1682. case CXN_KILLED_BURST_LEN_MISMATCH:
  1683. case CXN_KILLED_AHS_RCVD:
  1684. case CXN_KILLED_HDR_DIGEST_ERR:
  1685. case CXN_KILLED_UNKNOWN_HDR:
  1686. case CXN_KILLED_STALE_ITT_TTT_RCVD:
  1687. case CXN_KILLED_INVALID_ITT_TTT_RCVD:
  1688. case CXN_KILLED_TIMED_OUT:
  1689. case CXN_KILLED_FIN_RCVD:
  1690. case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
  1691. case CXN_KILLED_BAD_WRB_INDEX_ERROR:
  1692. case CXN_KILLED_OVER_RUN_RESIDUAL:
  1693. case CXN_KILLED_UNDER_RUN_RESIDUAL:
  1694. case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
  1695. SE_DEBUG(DBG_LVL_1, "CQ Error %d, reset CID "
  1696. "0x%x...\n",
  1697. sol->dw[offsetof(struct amap_sol_cqe, code) /
  1698. 32] & CQE_CODE_MASK,
  1699. (sol->dw[offsetof(struct amap_sol_cqe, cid) /
  1700. 32] & CQE_CID_MASK));
  1701. iscsi_conn_failure(beiscsi_conn->conn,
  1702. ISCSI_ERR_CONN_FAILED);
  1703. break;
  1704. case CXN_KILLED_RST_SENT:
  1705. case CXN_KILLED_RST_RCVD:
  1706. SE_DEBUG(DBG_LVL_1, "CQ Error %d, reset"
  1707. "received/sent on CID 0x%x...\n",
  1708. sol->dw[offsetof(struct amap_sol_cqe, code) /
  1709. 32] & CQE_CODE_MASK,
  1710. (sol->dw[offsetof(struct amap_sol_cqe, cid) /
  1711. 32] & CQE_CID_MASK));
  1712. iscsi_conn_failure(beiscsi_conn->conn,
  1713. ISCSI_ERR_CONN_FAILED);
  1714. break;
  1715. default:
  1716. SE_DEBUG(DBG_LVL_1, "CQ Error Invalid code= %d "
  1717. "received on CID 0x%x...\n",
  1718. sol->dw[offsetof(struct amap_sol_cqe, code) /
  1719. 32] & CQE_CODE_MASK,
  1720. (sol->dw[offsetof(struct amap_sol_cqe, cid) /
  1721. 32] & CQE_CID_MASK));
  1722. break;
  1723. }
  1724. AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
  1725. queue_tail_inc(cq);
  1726. sol = queue_tail_node(cq);
  1727. num_processed++;
  1728. }
  1729. if (num_processed > 0) {
  1730. tot_nump += num_processed;
  1731. hwi_ring_cq_db(phba, cq->id, num_processed, 1, 0);
  1732. }
  1733. return tot_nump;
  1734. }
  1735. void beiscsi_process_all_cqs(struct work_struct *work)
  1736. {
  1737. unsigned long flags;
  1738. struct hwi_controller *phwi_ctrlr;
  1739. struct hwi_context_memory *phwi_context;
  1740. struct be_eq_obj *pbe_eq;
  1741. struct beiscsi_hba *phba =
  1742. container_of(work, struct beiscsi_hba, work_cqs);
  1743. phwi_ctrlr = phba->phwi_ctrlr;
  1744. phwi_context = phwi_ctrlr->phwi_ctxt;
  1745. if (phba->msix_enabled)
  1746. pbe_eq = &phwi_context->be_eq[phba->num_cpus];
  1747. else
  1748. pbe_eq = &phwi_context->be_eq[0];
  1749. if (phba->todo_mcc_cq) {
  1750. spin_lock_irqsave(&phba->isr_lock, flags);
  1751. phba->todo_mcc_cq = 0;
  1752. spin_unlock_irqrestore(&phba->isr_lock, flags);
  1753. beiscsi_process_mcc_isr(phba);
  1754. }
  1755. if (phba->todo_cq) {
  1756. spin_lock_irqsave(&phba->isr_lock, flags);
  1757. phba->todo_cq = 0;
  1758. spin_unlock_irqrestore(&phba->isr_lock, flags);
  1759. beiscsi_process_cq(pbe_eq);
  1760. }
  1761. }
  1762. static int be_iopoll(struct blk_iopoll *iop, int budget)
  1763. {
  1764. static unsigned int ret;
  1765. struct beiscsi_hba *phba;
  1766. struct be_eq_obj *pbe_eq;
  1767. pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
  1768. ret = beiscsi_process_cq(pbe_eq);
  1769. if (ret < budget) {
  1770. phba = pbe_eq->phba;
  1771. blk_iopoll_complete(iop);
  1772. SE_DEBUG(DBG_LVL_8, "rearm pbe_eq->q.id =%d\n", pbe_eq->q.id);
  1773. hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
  1774. }
  1775. return ret;
  1776. }
  1777. static void
  1778. hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
  1779. unsigned int num_sg, struct beiscsi_io_task *io_task)
  1780. {
  1781. struct iscsi_sge *psgl;
  1782. unsigned int sg_len, index;
  1783. unsigned int sge_len = 0;
  1784. unsigned long long addr;
  1785. struct scatterlist *l_sg;
  1786. unsigned int offset;
  1787. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
  1788. io_task->bhs_pa.u.a32.address_lo);
  1789. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
  1790. io_task->bhs_pa.u.a32.address_hi);
  1791. l_sg = sg;
  1792. for (index = 0; (index < num_sg) && (index < 2); index++,
  1793. sg = sg_next(sg)) {
  1794. if (index == 0) {
  1795. sg_len = sg_dma_len(sg);
  1796. addr = (u64) sg_dma_address(sg);
  1797. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
  1798. ((u32)(addr & 0xFFFFFFFF)));
  1799. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
  1800. ((u32)(addr >> 32)));
  1801. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
  1802. sg_len);
  1803. sge_len = sg_len;
  1804. } else {
  1805. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
  1806. pwrb, sge_len);
  1807. sg_len = sg_dma_len(sg);
  1808. addr = (u64) sg_dma_address(sg);
  1809. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
  1810. ((u32)(addr & 0xFFFFFFFF)));
  1811. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
  1812. ((u32)(addr >> 32)));
  1813. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
  1814. sg_len);
  1815. }
  1816. }
  1817. psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
  1818. memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
  1819. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
  1820. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  1821. io_task->bhs_pa.u.a32.address_hi);
  1822. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  1823. io_task->bhs_pa.u.a32.address_lo);
  1824. if (num_sg == 1) {
  1825. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
  1826. 1);
  1827. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
  1828. 0);
  1829. } else if (num_sg == 2) {
  1830. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
  1831. 0);
  1832. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
  1833. 1);
  1834. } else {
  1835. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
  1836. 0);
  1837. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
  1838. 0);
  1839. }
  1840. sg = l_sg;
  1841. psgl++;
  1842. psgl++;
  1843. offset = 0;
  1844. for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
  1845. sg_len = sg_dma_len(sg);
  1846. addr = (u64) sg_dma_address(sg);
  1847. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  1848. (addr & 0xFFFFFFFF));
  1849. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  1850. (addr >> 32));
  1851. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
  1852. AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
  1853. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
  1854. offset += sg_len;
  1855. }
  1856. psgl--;
  1857. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
  1858. }
  1859. static void hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
  1860. {
  1861. struct iscsi_sge *psgl;
  1862. unsigned long long addr;
  1863. struct beiscsi_io_task *io_task = task->dd_data;
  1864. struct beiscsi_conn *beiscsi_conn = io_task->conn;
  1865. struct beiscsi_hba *phba = beiscsi_conn->phba;
  1866. io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
  1867. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
  1868. io_task->bhs_pa.u.a32.address_lo);
  1869. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
  1870. io_task->bhs_pa.u.a32.address_hi);
  1871. if (task->data) {
  1872. if (task->data_count) {
  1873. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
  1874. addr = (u64) pci_map_single(phba->pcidev,
  1875. task->data,
  1876. task->data_count, 1);
  1877. } else {
  1878. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
  1879. addr = 0;
  1880. }
  1881. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
  1882. ((u32)(addr & 0xFFFFFFFF)));
  1883. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
  1884. ((u32)(addr >> 32)));
  1885. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
  1886. task->data_count);
  1887. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
  1888. } else {
  1889. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
  1890. addr = 0;
  1891. }
  1892. psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
  1893. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
  1894. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  1895. io_task->bhs_pa.u.a32.address_hi);
  1896. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  1897. io_task->bhs_pa.u.a32.address_lo);
  1898. if (task->data) {
  1899. psgl++;
  1900. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
  1901. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
  1902. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
  1903. AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
  1904. AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
  1905. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
  1906. psgl++;
  1907. if (task->data) {
  1908. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  1909. ((u32)(addr & 0xFFFFFFFF)));
  1910. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  1911. ((u32)(addr >> 32)));
  1912. }
  1913. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
  1914. }
  1915. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
  1916. }
  1917. static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
  1918. {
  1919. unsigned int num_cq_pages, num_async_pdu_buf_pages;
  1920. unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
  1921. unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
  1922. num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
  1923. sizeof(struct sol_cqe));
  1924. num_async_pdu_buf_pages =
  1925. PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
  1926. phba->params.defpdu_hdr_sz);
  1927. num_async_pdu_buf_sgl_pages =
  1928. PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
  1929. sizeof(struct phys_addr));
  1930. num_async_pdu_data_pages =
  1931. PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
  1932. phba->params.defpdu_data_sz);
  1933. num_async_pdu_data_sgl_pages =
  1934. PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
  1935. sizeof(struct phys_addr));
  1936. phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
  1937. phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
  1938. BE_ISCSI_PDU_HEADER_SIZE;
  1939. phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
  1940. sizeof(struct hwi_context_memory);
  1941. phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
  1942. * (phba->params.wrbs_per_cxn)
  1943. * phba->params.cxns_per_ctrl;
  1944. wrb_sz_per_cxn = sizeof(struct wrb_handle) *
  1945. (phba->params.wrbs_per_cxn);
  1946. phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
  1947. phba->params.cxns_per_ctrl);
  1948. phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
  1949. phba->params.icds_per_ctrl;
  1950. phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
  1951. phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
  1952. phba->mem_req[HWI_MEM_ASYNC_HEADER_BUF] =
  1953. num_async_pdu_buf_pages * PAGE_SIZE;
  1954. phba->mem_req[HWI_MEM_ASYNC_DATA_BUF] =
  1955. num_async_pdu_data_pages * PAGE_SIZE;
  1956. phba->mem_req[HWI_MEM_ASYNC_HEADER_RING] =
  1957. num_async_pdu_buf_sgl_pages * PAGE_SIZE;
  1958. phba->mem_req[HWI_MEM_ASYNC_DATA_RING] =
  1959. num_async_pdu_data_sgl_pages * PAGE_SIZE;
  1960. phba->mem_req[HWI_MEM_ASYNC_HEADER_HANDLE] =
  1961. phba->params.asyncpdus_per_ctrl *
  1962. sizeof(struct async_pdu_handle);
  1963. phba->mem_req[HWI_MEM_ASYNC_DATA_HANDLE] =
  1964. phba->params.asyncpdus_per_ctrl *
  1965. sizeof(struct async_pdu_handle);
  1966. phba->mem_req[HWI_MEM_ASYNC_PDU_CONTEXT] =
  1967. sizeof(struct hwi_async_pdu_context) +
  1968. (phba->params.cxns_per_ctrl * sizeof(struct hwi_async_entry));
  1969. }
  1970. static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
  1971. {
  1972. struct be_mem_descriptor *mem_descr;
  1973. dma_addr_t bus_add;
  1974. struct mem_array *mem_arr, *mem_arr_orig;
  1975. unsigned int i, j, alloc_size, curr_alloc_size;
  1976. phba->phwi_ctrlr = kmalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
  1977. if (!phba->phwi_ctrlr)
  1978. return -ENOMEM;
  1979. phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
  1980. GFP_KERNEL);
  1981. if (!phba->init_mem) {
  1982. kfree(phba->phwi_ctrlr);
  1983. return -ENOMEM;
  1984. }
  1985. mem_arr_orig = kmalloc(sizeof(*mem_arr_orig) * BEISCSI_MAX_FRAGS_INIT,
  1986. GFP_KERNEL);
  1987. if (!mem_arr_orig) {
  1988. kfree(phba->init_mem);
  1989. kfree(phba->phwi_ctrlr);
  1990. return -ENOMEM;
  1991. }
  1992. mem_descr = phba->init_mem;
  1993. for (i = 0; i < SE_MEM_MAX; i++) {
  1994. j = 0;
  1995. mem_arr = mem_arr_orig;
  1996. alloc_size = phba->mem_req[i];
  1997. memset(mem_arr, 0, sizeof(struct mem_array) *
  1998. BEISCSI_MAX_FRAGS_INIT);
  1999. curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
  2000. do {
  2001. mem_arr->virtual_address = pci_alloc_consistent(
  2002. phba->pcidev,
  2003. curr_alloc_size,
  2004. &bus_add);
  2005. if (!mem_arr->virtual_address) {
  2006. if (curr_alloc_size <= BE_MIN_MEM_SIZE)
  2007. goto free_mem;
  2008. if (curr_alloc_size -
  2009. rounddown_pow_of_two(curr_alloc_size))
  2010. curr_alloc_size = rounddown_pow_of_two
  2011. (curr_alloc_size);
  2012. else
  2013. curr_alloc_size = curr_alloc_size / 2;
  2014. } else {
  2015. mem_arr->bus_address.u.
  2016. a64.address = (__u64) bus_add;
  2017. mem_arr->size = curr_alloc_size;
  2018. alloc_size -= curr_alloc_size;
  2019. curr_alloc_size = min(be_max_phys_size *
  2020. 1024, alloc_size);
  2021. j++;
  2022. mem_arr++;
  2023. }
  2024. } while (alloc_size);
  2025. mem_descr->num_elements = j;
  2026. mem_descr->size_in_bytes = phba->mem_req[i];
  2027. mem_descr->mem_array = kmalloc(sizeof(*mem_arr) * j,
  2028. GFP_KERNEL);
  2029. if (!mem_descr->mem_array)
  2030. goto free_mem;
  2031. memcpy(mem_descr->mem_array, mem_arr_orig,
  2032. sizeof(struct mem_array) * j);
  2033. mem_descr++;
  2034. }
  2035. kfree(mem_arr_orig);
  2036. return 0;
  2037. free_mem:
  2038. mem_descr->num_elements = j;
  2039. while ((i) || (j)) {
  2040. for (j = mem_descr->num_elements; j > 0; j--) {
  2041. pci_free_consistent(phba->pcidev,
  2042. mem_descr->mem_array[j - 1].size,
  2043. mem_descr->mem_array[j - 1].
  2044. virtual_address,
  2045. (unsigned long)mem_descr->
  2046. mem_array[j - 1].
  2047. bus_address.u.a64.address);
  2048. }
  2049. if (i) {
  2050. i--;
  2051. kfree(mem_descr->mem_array);
  2052. mem_descr--;
  2053. }
  2054. }
  2055. kfree(mem_arr_orig);
  2056. kfree(phba->init_mem);
  2057. kfree(phba->phwi_ctrlr);
  2058. return -ENOMEM;
  2059. }
  2060. static int beiscsi_get_memory(struct beiscsi_hba *phba)
  2061. {
  2062. beiscsi_find_mem_req(phba);
  2063. return beiscsi_alloc_mem(phba);
  2064. }
  2065. static void iscsi_init_global_templates(struct beiscsi_hba *phba)
  2066. {
  2067. struct pdu_data_out *pdata_out;
  2068. struct pdu_nop_out *pnop_out;
  2069. struct be_mem_descriptor *mem_descr;
  2070. mem_descr = phba->init_mem;
  2071. mem_descr += ISCSI_MEM_GLOBAL_HEADER;
  2072. pdata_out =
  2073. (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
  2074. memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
  2075. AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
  2076. IIOC_SCSI_DATA);
  2077. pnop_out =
  2078. (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
  2079. virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
  2080. memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
  2081. AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
  2082. AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
  2083. AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
  2084. }
  2085. static void beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
  2086. {
  2087. struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
  2088. struct wrb_handle *pwrb_handle;
  2089. struct hwi_controller *phwi_ctrlr;
  2090. struct hwi_wrb_context *pwrb_context;
  2091. struct iscsi_wrb *pwrb;
  2092. unsigned int num_cxn_wrbh;
  2093. unsigned int num_cxn_wrb, j, idx, index;
  2094. mem_descr_wrbh = phba->init_mem;
  2095. mem_descr_wrbh += HWI_MEM_WRBH;
  2096. mem_descr_wrb = phba->init_mem;
  2097. mem_descr_wrb += HWI_MEM_WRB;
  2098. idx = 0;
  2099. pwrb_handle = mem_descr_wrbh->mem_array[idx].virtual_address;
  2100. num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
  2101. ((sizeof(struct wrb_handle)) *
  2102. phba->params.wrbs_per_cxn));
  2103. phwi_ctrlr = phba->phwi_ctrlr;
  2104. for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
  2105. pwrb_context = &phwi_ctrlr->wrb_context[index];
  2106. pwrb_context->pwrb_handle_base =
  2107. kzalloc(sizeof(struct wrb_handle *) *
  2108. phba->params.wrbs_per_cxn, GFP_KERNEL);
  2109. pwrb_context->pwrb_handle_basestd =
  2110. kzalloc(sizeof(struct wrb_handle *) *
  2111. phba->params.wrbs_per_cxn, GFP_KERNEL);
  2112. if (num_cxn_wrbh) {
  2113. pwrb_context->alloc_index = 0;
  2114. pwrb_context->wrb_handles_available = 0;
  2115. for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
  2116. pwrb_context->pwrb_handle_base[j] = pwrb_handle;
  2117. pwrb_context->pwrb_handle_basestd[j] =
  2118. pwrb_handle;
  2119. pwrb_context->wrb_handles_available++;
  2120. pwrb_handle->wrb_index = j;
  2121. pwrb_handle++;
  2122. }
  2123. pwrb_context->free_index = 0;
  2124. num_cxn_wrbh--;
  2125. } else {
  2126. idx++;
  2127. pwrb_handle =
  2128. mem_descr_wrbh->mem_array[idx].virtual_address;
  2129. num_cxn_wrbh =
  2130. ((mem_descr_wrbh->mem_array[idx].size) /
  2131. ((sizeof(struct wrb_handle)) *
  2132. phba->params.wrbs_per_cxn));
  2133. pwrb_context->alloc_index = 0;
  2134. for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
  2135. pwrb_context->pwrb_handle_base[j] = pwrb_handle;
  2136. pwrb_context->pwrb_handle_basestd[j] =
  2137. pwrb_handle;
  2138. pwrb_context->wrb_handles_available++;
  2139. pwrb_handle->wrb_index = j;
  2140. pwrb_handle++;
  2141. }
  2142. pwrb_context->free_index = 0;
  2143. num_cxn_wrbh--;
  2144. }
  2145. }
  2146. idx = 0;
  2147. pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
  2148. num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
  2149. ((sizeof(struct iscsi_wrb) *
  2150. phba->params.wrbs_per_cxn));
  2151. for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
  2152. pwrb_context = &phwi_ctrlr->wrb_context[index];
  2153. if (num_cxn_wrb) {
  2154. for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
  2155. pwrb_handle = pwrb_context->pwrb_handle_base[j];
  2156. pwrb_handle->pwrb = pwrb;
  2157. pwrb++;
  2158. }
  2159. num_cxn_wrb--;
  2160. } else {
  2161. idx++;
  2162. pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
  2163. num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
  2164. ((sizeof(struct iscsi_wrb) *
  2165. phba->params.wrbs_per_cxn));
  2166. for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
  2167. pwrb_handle = pwrb_context->pwrb_handle_base[j];
  2168. pwrb_handle->pwrb = pwrb;
  2169. pwrb++;
  2170. }
  2171. num_cxn_wrb--;
  2172. }
  2173. }
  2174. }
  2175. static void hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
  2176. {
  2177. struct hwi_controller *phwi_ctrlr;
  2178. struct hba_parameters *p = &phba->params;
  2179. struct hwi_async_pdu_context *pasync_ctx;
  2180. struct async_pdu_handle *pasync_header_h, *pasync_data_h;
  2181. unsigned int index;
  2182. struct be_mem_descriptor *mem_descr;
  2183. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2184. mem_descr += HWI_MEM_ASYNC_PDU_CONTEXT;
  2185. phwi_ctrlr = phba->phwi_ctrlr;
  2186. phwi_ctrlr->phwi_ctxt->pasync_ctx = (struct hwi_async_pdu_context *)
  2187. mem_descr->mem_array[0].virtual_address;
  2188. pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx;
  2189. memset(pasync_ctx, 0, sizeof(*pasync_ctx));
  2190. pasync_ctx->async_header.num_entries = p->asyncpdus_per_ctrl;
  2191. pasync_ctx->async_header.buffer_size = p->defpdu_hdr_sz;
  2192. pasync_ctx->async_data.buffer_size = p->defpdu_data_sz;
  2193. pasync_ctx->async_data.num_entries = p->asyncpdus_per_ctrl;
  2194. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2195. mem_descr += HWI_MEM_ASYNC_HEADER_BUF;
  2196. if (mem_descr->mem_array[0].virtual_address) {
  2197. SE_DEBUG(DBG_LVL_8,
  2198. "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_HEADER_BUF"
  2199. "va=%p\n", mem_descr->mem_array[0].virtual_address);
  2200. } else
  2201. shost_printk(KERN_WARNING, phba->shost,
  2202. "No Virtual address\n");
  2203. pasync_ctx->async_header.va_base =
  2204. mem_descr->mem_array[0].virtual_address;
  2205. pasync_ctx->async_header.pa_base.u.a64.address =
  2206. mem_descr->mem_array[0].bus_address.u.a64.address;
  2207. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2208. mem_descr += HWI_MEM_ASYNC_HEADER_RING;
  2209. if (mem_descr->mem_array[0].virtual_address) {
  2210. SE_DEBUG(DBG_LVL_8,
  2211. "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_HEADER_RING"
  2212. "va=%p\n", mem_descr->mem_array[0].virtual_address);
  2213. } else
  2214. shost_printk(KERN_WARNING, phba->shost,
  2215. "No Virtual address\n");
  2216. pasync_ctx->async_header.ring_base =
  2217. mem_descr->mem_array[0].virtual_address;
  2218. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2219. mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE;
  2220. if (mem_descr->mem_array[0].virtual_address) {
  2221. SE_DEBUG(DBG_LVL_8,
  2222. "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_HEADER_HANDLE"
  2223. "va=%p\n", mem_descr->mem_array[0].virtual_address);
  2224. } else
  2225. shost_printk(KERN_WARNING, phba->shost,
  2226. "No Virtual address\n");
  2227. pasync_ctx->async_header.handle_base =
  2228. mem_descr->mem_array[0].virtual_address;
  2229. pasync_ctx->async_header.writables = 0;
  2230. INIT_LIST_HEAD(&pasync_ctx->async_header.free_list);
  2231. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2232. mem_descr += HWI_MEM_ASYNC_DATA_BUF;
  2233. if (mem_descr->mem_array[0].virtual_address) {
  2234. SE_DEBUG(DBG_LVL_8,
  2235. "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_DATA_BUF"
  2236. "va=%p\n", mem_descr->mem_array[0].virtual_address);
  2237. } else
  2238. shost_printk(KERN_WARNING, phba->shost,
  2239. "No Virtual address\n");
  2240. pasync_ctx->async_data.va_base =
  2241. mem_descr->mem_array[0].virtual_address;
  2242. pasync_ctx->async_data.pa_base.u.a64.address =
  2243. mem_descr->mem_array[0].bus_address.u.a64.address;
  2244. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2245. mem_descr += HWI_MEM_ASYNC_DATA_RING;
  2246. if (mem_descr->mem_array[0].virtual_address) {
  2247. SE_DEBUG(DBG_LVL_8,
  2248. "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_DATA_RING"
  2249. "va=%p\n", mem_descr->mem_array[0].virtual_address);
  2250. } else
  2251. shost_printk(KERN_WARNING, phba->shost,
  2252. "No Virtual address\n");
  2253. pasync_ctx->async_data.ring_base =
  2254. mem_descr->mem_array[0].virtual_address;
  2255. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2256. mem_descr += HWI_MEM_ASYNC_DATA_HANDLE;
  2257. if (!mem_descr->mem_array[0].virtual_address)
  2258. shost_printk(KERN_WARNING, phba->shost,
  2259. "No Virtual address\n");
  2260. pasync_ctx->async_data.handle_base =
  2261. mem_descr->mem_array[0].virtual_address;
  2262. pasync_ctx->async_data.writables = 0;
  2263. INIT_LIST_HEAD(&pasync_ctx->async_data.free_list);
  2264. pasync_header_h =
  2265. (struct async_pdu_handle *)pasync_ctx->async_header.handle_base;
  2266. pasync_data_h =
  2267. (struct async_pdu_handle *)pasync_ctx->async_data.handle_base;
  2268. for (index = 0; index < p->asyncpdus_per_ctrl; index++) {
  2269. pasync_header_h->cri = -1;
  2270. pasync_header_h->index = (char)index;
  2271. INIT_LIST_HEAD(&pasync_header_h->link);
  2272. pasync_header_h->pbuffer =
  2273. (void *)((unsigned long)
  2274. (pasync_ctx->async_header.va_base) +
  2275. (p->defpdu_hdr_sz * index));
  2276. pasync_header_h->pa.u.a64.address =
  2277. pasync_ctx->async_header.pa_base.u.a64.address +
  2278. (p->defpdu_hdr_sz * index);
  2279. list_add_tail(&pasync_header_h->link,
  2280. &pasync_ctx->async_header.free_list);
  2281. pasync_header_h++;
  2282. pasync_ctx->async_header.free_entries++;
  2283. pasync_ctx->async_header.writables++;
  2284. INIT_LIST_HEAD(&pasync_ctx->async_entry[index].wait_queue.list);
  2285. INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
  2286. header_busy_list);
  2287. pasync_data_h->cri = -1;
  2288. pasync_data_h->index = (char)index;
  2289. INIT_LIST_HEAD(&pasync_data_h->link);
  2290. pasync_data_h->pbuffer =
  2291. (void *)((unsigned long)
  2292. (pasync_ctx->async_data.va_base) +
  2293. (p->defpdu_data_sz * index));
  2294. pasync_data_h->pa.u.a64.address =
  2295. pasync_ctx->async_data.pa_base.u.a64.address +
  2296. (p->defpdu_data_sz * index);
  2297. list_add_tail(&pasync_data_h->link,
  2298. &pasync_ctx->async_data.free_list);
  2299. pasync_data_h++;
  2300. pasync_ctx->async_data.free_entries++;
  2301. pasync_ctx->async_data.writables++;
  2302. INIT_LIST_HEAD(&pasync_ctx->async_entry[index].data_busy_list);
  2303. }
  2304. pasync_ctx->async_header.host_write_ptr = 0;
  2305. pasync_ctx->async_header.ep_read_ptr = -1;
  2306. pasync_ctx->async_data.host_write_ptr = 0;
  2307. pasync_ctx->async_data.ep_read_ptr = -1;
  2308. }
  2309. static int
  2310. be_sgl_create_contiguous(void *virtual_address,
  2311. u64 physical_address, u32 length,
  2312. struct be_dma_mem *sgl)
  2313. {
  2314. WARN_ON(!virtual_address);
  2315. WARN_ON(!physical_address);
  2316. WARN_ON(!length > 0);
  2317. WARN_ON(!sgl);
  2318. sgl->va = virtual_address;
  2319. sgl->dma = (unsigned long)physical_address;
  2320. sgl->size = length;
  2321. return 0;
  2322. }
  2323. static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
  2324. {
  2325. memset(sgl, 0, sizeof(*sgl));
  2326. }
  2327. static void
  2328. hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
  2329. struct mem_array *pmem, struct be_dma_mem *sgl)
  2330. {
  2331. if (sgl->va)
  2332. be_sgl_destroy_contiguous(sgl);
  2333. be_sgl_create_contiguous(pmem->virtual_address,
  2334. pmem->bus_address.u.a64.address,
  2335. pmem->size, sgl);
  2336. }
  2337. static void
  2338. hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
  2339. struct mem_array *pmem, struct be_dma_mem *sgl)
  2340. {
  2341. if (sgl->va)
  2342. be_sgl_destroy_contiguous(sgl);
  2343. be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
  2344. pmem->bus_address.u.a64.address,
  2345. pmem->size, sgl);
  2346. }
  2347. static int be_fill_queue(struct be_queue_info *q,
  2348. u16 len, u16 entry_size, void *vaddress)
  2349. {
  2350. struct be_dma_mem *mem = &q->dma_mem;
  2351. memset(q, 0, sizeof(*q));
  2352. q->len = len;
  2353. q->entry_size = entry_size;
  2354. mem->size = len * entry_size;
  2355. mem->va = vaddress;
  2356. if (!mem->va)
  2357. return -ENOMEM;
  2358. memset(mem->va, 0, mem->size);
  2359. return 0;
  2360. }
  2361. static int beiscsi_create_eqs(struct beiscsi_hba *phba,
  2362. struct hwi_context_memory *phwi_context)
  2363. {
  2364. unsigned int i, num_eq_pages;
  2365. int ret, eq_for_mcc;
  2366. struct be_queue_info *eq;
  2367. struct be_dma_mem *mem;
  2368. void *eq_vaddress;
  2369. dma_addr_t paddr;
  2370. num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries * \
  2371. sizeof(struct be_eq_entry));
  2372. if (phba->msix_enabled)
  2373. eq_for_mcc = 1;
  2374. else
  2375. eq_for_mcc = 0;
  2376. for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
  2377. eq = &phwi_context->be_eq[i].q;
  2378. mem = &eq->dma_mem;
  2379. phwi_context->be_eq[i].phba = phba;
  2380. eq_vaddress = pci_alloc_consistent(phba->pcidev,
  2381. num_eq_pages * PAGE_SIZE,
  2382. &paddr);
  2383. if (!eq_vaddress)
  2384. goto create_eq_error;
  2385. mem->va = eq_vaddress;
  2386. ret = be_fill_queue(eq, phba->params.num_eq_entries,
  2387. sizeof(struct be_eq_entry), eq_vaddress);
  2388. if (ret) {
  2389. shost_printk(KERN_ERR, phba->shost,
  2390. "be_fill_queue Failed for EQ\n");
  2391. goto create_eq_error;
  2392. }
  2393. mem->dma = paddr;
  2394. ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
  2395. phwi_context->cur_eqd);
  2396. if (ret) {
  2397. shost_printk(KERN_ERR, phba->shost,
  2398. "beiscsi_cmd_eq_create"
  2399. "Failedfor EQ\n");
  2400. goto create_eq_error;
  2401. }
  2402. SE_DEBUG(DBG_LVL_8, "eqid = %d\n", phwi_context->be_eq[i].q.id);
  2403. }
  2404. return 0;
  2405. create_eq_error:
  2406. for (i = 0; i < (phba->num_cpus + 1); i++) {
  2407. eq = &phwi_context->be_eq[i].q;
  2408. mem = &eq->dma_mem;
  2409. if (mem->va)
  2410. pci_free_consistent(phba->pcidev, num_eq_pages
  2411. * PAGE_SIZE,
  2412. mem->va, mem->dma);
  2413. }
  2414. return ret;
  2415. }
  2416. static int beiscsi_create_cqs(struct beiscsi_hba *phba,
  2417. struct hwi_context_memory *phwi_context)
  2418. {
  2419. unsigned int i, num_cq_pages;
  2420. int ret;
  2421. struct be_queue_info *cq, *eq;
  2422. struct be_dma_mem *mem;
  2423. struct be_eq_obj *pbe_eq;
  2424. void *cq_vaddress;
  2425. dma_addr_t paddr;
  2426. num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
  2427. sizeof(struct sol_cqe));
  2428. for (i = 0; i < phba->num_cpus; i++) {
  2429. cq = &phwi_context->be_cq[i];
  2430. eq = &phwi_context->be_eq[i].q;
  2431. pbe_eq = &phwi_context->be_eq[i];
  2432. pbe_eq->cq = cq;
  2433. pbe_eq->phba = phba;
  2434. mem = &cq->dma_mem;
  2435. cq_vaddress = pci_alloc_consistent(phba->pcidev,
  2436. num_cq_pages * PAGE_SIZE,
  2437. &paddr);
  2438. if (!cq_vaddress)
  2439. goto create_cq_error;
  2440. ret = be_fill_queue(cq, phba->params.num_cq_entries,
  2441. sizeof(struct sol_cqe), cq_vaddress);
  2442. if (ret) {
  2443. shost_printk(KERN_ERR, phba->shost,
  2444. "be_fill_queue Failed for ISCSI CQ\n");
  2445. goto create_cq_error;
  2446. }
  2447. mem->dma = paddr;
  2448. ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
  2449. false, 0);
  2450. if (ret) {
  2451. shost_printk(KERN_ERR, phba->shost,
  2452. "beiscsi_cmd_eq_create"
  2453. "Failed for ISCSI CQ\n");
  2454. goto create_cq_error;
  2455. }
  2456. SE_DEBUG(DBG_LVL_8, "iscsi cq_id is %d for eq_id %d\n",
  2457. cq->id, eq->id);
  2458. SE_DEBUG(DBG_LVL_8, "ISCSI CQ CREATED\n");
  2459. }
  2460. return 0;
  2461. create_cq_error:
  2462. for (i = 0; i < phba->num_cpus; i++) {
  2463. cq = &phwi_context->be_cq[i];
  2464. mem = &cq->dma_mem;
  2465. if (mem->va)
  2466. pci_free_consistent(phba->pcidev, num_cq_pages
  2467. * PAGE_SIZE,
  2468. mem->va, mem->dma);
  2469. }
  2470. return ret;
  2471. }
  2472. static int
  2473. beiscsi_create_def_hdr(struct beiscsi_hba *phba,
  2474. struct hwi_context_memory *phwi_context,
  2475. struct hwi_controller *phwi_ctrlr,
  2476. unsigned int def_pdu_ring_sz)
  2477. {
  2478. unsigned int idx;
  2479. int ret;
  2480. struct be_queue_info *dq, *cq;
  2481. struct be_dma_mem *mem;
  2482. struct be_mem_descriptor *mem_descr;
  2483. void *dq_vaddress;
  2484. idx = 0;
  2485. dq = &phwi_context->be_def_hdrq;
  2486. cq = &phwi_context->be_cq[0];
  2487. mem = &dq->dma_mem;
  2488. mem_descr = phba->init_mem;
  2489. mem_descr += HWI_MEM_ASYNC_HEADER_RING;
  2490. dq_vaddress = mem_descr->mem_array[idx].virtual_address;
  2491. ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
  2492. sizeof(struct phys_addr),
  2493. sizeof(struct phys_addr), dq_vaddress);
  2494. if (ret) {
  2495. shost_printk(KERN_ERR, phba->shost,
  2496. "be_fill_queue Failed for DEF PDU HDR\n");
  2497. return ret;
  2498. }
  2499. mem->dma = (unsigned long)mem_descr->mem_array[idx].
  2500. bus_address.u.a64.address;
  2501. ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
  2502. def_pdu_ring_sz,
  2503. phba->params.defpdu_hdr_sz);
  2504. if (ret) {
  2505. shost_printk(KERN_ERR, phba->shost,
  2506. "be_cmd_create_default_pdu_queue Failed DEFHDR\n");
  2507. return ret;
  2508. }
  2509. phwi_ctrlr->default_pdu_hdr.id = phwi_context->be_def_hdrq.id;
  2510. SE_DEBUG(DBG_LVL_8, "iscsi def pdu id is %d\n",
  2511. phwi_context->be_def_hdrq.id);
  2512. hwi_post_async_buffers(phba, 1);
  2513. return 0;
  2514. }
  2515. static int
  2516. beiscsi_create_def_data(struct beiscsi_hba *phba,
  2517. struct hwi_context_memory *phwi_context,
  2518. struct hwi_controller *phwi_ctrlr,
  2519. unsigned int def_pdu_ring_sz)
  2520. {
  2521. unsigned int idx;
  2522. int ret;
  2523. struct be_queue_info *dataq, *cq;
  2524. struct be_dma_mem *mem;
  2525. struct be_mem_descriptor *mem_descr;
  2526. void *dq_vaddress;
  2527. idx = 0;
  2528. dataq = &phwi_context->be_def_dataq;
  2529. cq = &phwi_context->be_cq[0];
  2530. mem = &dataq->dma_mem;
  2531. mem_descr = phba->init_mem;
  2532. mem_descr += HWI_MEM_ASYNC_DATA_RING;
  2533. dq_vaddress = mem_descr->mem_array[idx].virtual_address;
  2534. ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
  2535. sizeof(struct phys_addr),
  2536. sizeof(struct phys_addr), dq_vaddress);
  2537. if (ret) {
  2538. shost_printk(KERN_ERR, phba->shost,
  2539. "be_fill_queue Failed for DEF PDU DATA\n");
  2540. return ret;
  2541. }
  2542. mem->dma = (unsigned long)mem_descr->mem_array[idx].
  2543. bus_address.u.a64.address;
  2544. ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
  2545. def_pdu_ring_sz,
  2546. phba->params.defpdu_data_sz);
  2547. if (ret) {
  2548. shost_printk(KERN_ERR, phba->shost,
  2549. "be_cmd_create_default_pdu_queue Failed"
  2550. " for DEF PDU DATA\n");
  2551. return ret;
  2552. }
  2553. phwi_ctrlr->default_pdu_data.id = phwi_context->be_def_dataq.id;
  2554. SE_DEBUG(DBG_LVL_8, "iscsi def data id is %d\n",
  2555. phwi_context->be_def_dataq.id);
  2556. hwi_post_async_buffers(phba, 0);
  2557. SE_DEBUG(DBG_LVL_8, "DEFAULT PDU DATA RING CREATED\n");
  2558. return 0;
  2559. }
  2560. static int
  2561. beiscsi_post_pages(struct beiscsi_hba *phba)
  2562. {
  2563. struct be_mem_descriptor *mem_descr;
  2564. struct mem_array *pm_arr;
  2565. unsigned int page_offset, i;
  2566. struct be_dma_mem sgl;
  2567. int status;
  2568. mem_descr = phba->init_mem;
  2569. mem_descr += HWI_MEM_SGE;
  2570. pm_arr = mem_descr->mem_array;
  2571. page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
  2572. phba->fw_config.iscsi_icd_start) / PAGE_SIZE;
  2573. for (i = 0; i < mem_descr->num_elements; i++) {
  2574. hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
  2575. status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
  2576. page_offset,
  2577. (pm_arr->size / PAGE_SIZE));
  2578. page_offset += pm_arr->size / PAGE_SIZE;
  2579. if (status != 0) {
  2580. shost_printk(KERN_ERR, phba->shost,
  2581. "post sgl failed.\n");
  2582. return status;
  2583. }
  2584. pm_arr++;
  2585. }
  2586. SE_DEBUG(DBG_LVL_8, "POSTED PAGES\n");
  2587. return 0;
  2588. }
  2589. static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
  2590. {
  2591. struct be_dma_mem *mem = &q->dma_mem;
  2592. if (mem->va)
  2593. pci_free_consistent(phba->pcidev, mem->size,
  2594. mem->va, mem->dma);
  2595. }
  2596. static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
  2597. u16 len, u16 entry_size)
  2598. {
  2599. struct be_dma_mem *mem = &q->dma_mem;
  2600. memset(q, 0, sizeof(*q));
  2601. q->len = len;
  2602. q->entry_size = entry_size;
  2603. mem->size = len * entry_size;
  2604. mem->va = pci_alloc_consistent(phba->pcidev, mem->size, &mem->dma);
  2605. if (!mem->va)
  2606. return -ENOMEM;
  2607. memset(mem->va, 0, mem->size);
  2608. return 0;
  2609. }
  2610. static int
  2611. beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
  2612. struct hwi_context_memory *phwi_context,
  2613. struct hwi_controller *phwi_ctrlr)
  2614. {
  2615. unsigned int wrb_mem_index, offset, size, num_wrb_rings;
  2616. u64 pa_addr_lo;
  2617. unsigned int idx, num, i;
  2618. struct mem_array *pwrb_arr;
  2619. void *wrb_vaddr;
  2620. struct be_dma_mem sgl;
  2621. struct be_mem_descriptor *mem_descr;
  2622. int status;
  2623. idx = 0;
  2624. mem_descr = phba->init_mem;
  2625. mem_descr += HWI_MEM_WRB;
  2626. pwrb_arr = kmalloc(sizeof(*pwrb_arr) * phba->params.cxns_per_ctrl,
  2627. GFP_KERNEL);
  2628. if (!pwrb_arr) {
  2629. shost_printk(KERN_ERR, phba->shost,
  2630. "Memory alloc failed in create wrb ring.\n");
  2631. return -ENOMEM;
  2632. }
  2633. wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
  2634. pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
  2635. num_wrb_rings = mem_descr->mem_array[idx].size /
  2636. (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
  2637. for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
  2638. if (num_wrb_rings) {
  2639. pwrb_arr[num].virtual_address = wrb_vaddr;
  2640. pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
  2641. pwrb_arr[num].size = phba->params.wrbs_per_cxn *
  2642. sizeof(struct iscsi_wrb);
  2643. wrb_vaddr += pwrb_arr[num].size;
  2644. pa_addr_lo += pwrb_arr[num].size;
  2645. num_wrb_rings--;
  2646. } else {
  2647. idx++;
  2648. wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
  2649. pa_addr_lo = mem_descr->mem_array[idx].\
  2650. bus_address.u.a64.address;
  2651. num_wrb_rings = mem_descr->mem_array[idx].size /
  2652. (phba->params.wrbs_per_cxn *
  2653. sizeof(struct iscsi_wrb));
  2654. pwrb_arr[num].virtual_address = wrb_vaddr;
  2655. pwrb_arr[num].bus_address.u.a64.address\
  2656. = pa_addr_lo;
  2657. pwrb_arr[num].size = phba->params.wrbs_per_cxn *
  2658. sizeof(struct iscsi_wrb);
  2659. wrb_vaddr += pwrb_arr[num].size;
  2660. pa_addr_lo += pwrb_arr[num].size;
  2661. num_wrb_rings--;
  2662. }
  2663. }
  2664. for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
  2665. wrb_mem_index = 0;
  2666. offset = 0;
  2667. size = 0;
  2668. hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
  2669. status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
  2670. &phwi_context->be_wrbq[i]);
  2671. if (status != 0) {
  2672. shost_printk(KERN_ERR, phba->shost,
  2673. "wrbq create failed.");
  2674. kfree(pwrb_arr);
  2675. return status;
  2676. }
  2677. phwi_ctrlr->wrb_context[i * 2].cid = phwi_context->be_wrbq[i].
  2678. id;
  2679. }
  2680. kfree(pwrb_arr);
  2681. return 0;
  2682. }
  2683. static void free_wrb_handles(struct beiscsi_hba *phba)
  2684. {
  2685. unsigned int index;
  2686. struct hwi_controller *phwi_ctrlr;
  2687. struct hwi_wrb_context *pwrb_context;
  2688. phwi_ctrlr = phba->phwi_ctrlr;
  2689. for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
  2690. pwrb_context = &phwi_ctrlr->wrb_context[index];
  2691. kfree(pwrb_context->pwrb_handle_base);
  2692. kfree(pwrb_context->pwrb_handle_basestd);
  2693. }
  2694. }
  2695. static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
  2696. {
  2697. struct be_queue_info *q;
  2698. struct be_ctrl_info *ctrl = &phba->ctrl;
  2699. q = &phba->ctrl.mcc_obj.q;
  2700. if (q->created)
  2701. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
  2702. be_queue_free(phba, q);
  2703. q = &phba->ctrl.mcc_obj.cq;
  2704. if (q->created)
  2705. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
  2706. be_queue_free(phba, q);
  2707. }
  2708. static void hwi_cleanup(struct beiscsi_hba *phba)
  2709. {
  2710. struct be_queue_info *q;
  2711. struct be_ctrl_info *ctrl = &phba->ctrl;
  2712. struct hwi_controller *phwi_ctrlr;
  2713. struct hwi_context_memory *phwi_context;
  2714. int i, eq_num;
  2715. phwi_ctrlr = phba->phwi_ctrlr;
  2716. phwi_context = phwi_ctrlr->phwi_ctxt;
  2717. for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
  2718. q = &phwi_context->be_wrbq[i];
  2719. if (q->created)
  2720. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
  2721. }
  2722. free_wrb_handles(phba);
  2723. q = &phwi_context->be_def_hdrq;
  2724. if (q->created)
  2725. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
  2726. q = &phwi_context->be_def_dataq;
  2727. if (q->created)
  2728. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
  2729. beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
  2730. for (i = 0; i < (phba->num_cpus); i++) {
  2731. q = &phwi_context->be_cq[i];
  2732. if (q->created)
  2733. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
  2734. }
  2735. if (phba->msix_enabled)
  2736. eq_num = 1;
  2737. else
  2738. eq_num = 0;
  2739. for (i = 0; i < (phba->num_cpus + eq_num); i++) {
  2740. q = &phwi_context->be_eq[i].q;
  2741. if (q->created)
  2742. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
  2743. }
  2744. be_mcc_queues_destroy(phba);
  2745. }
  2746. static int be_mcc_queues_create(struct beiscsi_hba *phba,
  2747. struct hwi_context_memory *phwi_context)
  2748. {
  2749. struct be_queue_info *q, *cq;
  2750. struct be_ctrl_info *ctrl = &phba->ctrl;
  2751. /* Alloc MCC compl queue */
  2752. cq = &phba->ctrl.mcc_obj.cq;
  2753. if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
  2754. sizeof(struct be_mcc_compl)))
  2755. goto err;
  2756. /* Ask BE to create MCC compl queue; */
  2757. if (phba->msix_enabled) {
  2758. if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq
  2759. [phba->num_cpus].q, false, true, 0))
  2760. goto mcc_cq_free;
  2761. } else {
  2762. if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
  2763. false, true, 0))
  2764. goto mcc_cq_free;
  2765. }
  2766. /* Alloc MCC queue */
  2767. q = &phba->ctrl.mcc_obj.q;
  2768. if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
  2769. goto mcc_cq_destroy;
  2770. /* Ask BE to create MCC queue */
  2771. if (beiscsi_cmd_mccq_create(phba, q, cq))
  2772. goto mcc_q_free;
  2773. return 0;
  2774. mcc_q_free:
  2775. be_queue_free(phba, q);
  2776. mcc_cq_destroy:
  2777. beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
  2778. mcc_cq_free:
  2779. be_queue_free(phba, cq);
  2780. err:
  2781. return -ENOMEM;
  2782. }
  2783. static int find_num_cpus(void)
  2784. {
  2785. int num_cpus = 0;
  2786. num_cpus = num_online_cpus();
  2787. if (num_cpus >= MAX_CPUS)
  2788. num_cpus = MAX_CPUS - 1;
  2789. SE_DEBUG(DBG_LVL_8, "num_cpus = %d\n", num_cpus);
  2790. return num_cpus;
  2791. }
  2792. static int hwi_init_port(struct beiscsi_hba *phba)
  2793. {
  2794. struct hwi_controller *phwi_ctrlr;
  2795. struct hwi_context_memory *phwi_context;
  2796. unsigned int def_pdu_ring_sz;
  2797. struct be_ctrl_info *ctrl = &phba->ctrl;
  2798. int status;
  2799. def_pdu_ring_sz =
  2800. phba->params.asyncpdus_per_ctrl * sizeof(struct phys_addr);
  2801. phwi_ctrlr = phba->phwi_ctrlr;
  2802. phwi_context = phwi_ctrlr->phwi_ctxt;
  2803. phwi_context->max_eqd = 0;
  2804. phwi_context->min_eqd = 0;
  2805. phwi_context->cur_eqd = 64;
  2806. be_cmd_fw_initialize(&phba->ctrl);
  2807. status = beiscsi_create_eqs(phba, phwi_context);
  2808. if (status != 0) {
  2809. shost_printk(KERN_ERR, phba->shost, "EQ not created\n");
  2810. goto error;
  2811. }
  2812. status = be_mcc_queues_create(phba, phwi_context);
  2813. if (status != 0)
  2814. goto error;
  2815. status = mgmt_check_supported_fw(ctrl, phba);
  2816. if (status != 0) {
  2817. shost_printk(KERN_ERR, phba->shost,
  2818. "Unsupported fw version\n");
  2819. goto error;
  2820. }
  2821. status = beiscsi_create_cqs(phba, phwi_context);
  2822. if (status != 0) {
  2823. shost_printk(KERN_ERR, phba->shost, "CQ not created\n");
  2824. goto error;
  2825. }
  2826. status = beiscsi_create_def_hdr(phba, phwi_context, phwi_ctrlr,
  2827. def_pdu_ring_sz);
  2828. if (status != 0) {
  2829. shost_printk(KERN_ERR, phba->shost,
  2830. "Default Header not created\n");
  2831. goto error;
  2832. }
  2833. status = beiscsi_create_def_data(phba, phwi_context,
  2834. phwi_ctrlr, def_pdu_ring_sz);
  2835. if (status != 0) {
  2836. shost_printk(KERN_ERR, phba->shost,
  2837. "Default Data not created\n");
  2838. goto error;
  2839. }
  2840. status = beiscsi_post_pages(phba);
  2841. if (status != 0) {
  2842. shost_printk(KERN_ERR, phba->shost, "Post SGL Pages Failed\n");
  2843. goto error;
  2844. }
  2845. status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
  2846. if (status != 0) {
  2847. shost_printk(KERN_ERR, phba->shost,
  2848. "WRB Rings not created\n");
  2849. goto error;
  2850. }
  2851. SE_DEBUG(DBG_LVL_8, "hwi_init_port success\n");
  2852. return 0;
  2853. error:
  2854. shost_printk(KERN_ERR, phba->shost, "hwi_init_port failed");
  2855. hwi_cleanup(phba);
  2856. return -ENOMEM;
  2857. }
  2858. static int hwi_init_controller(struct beiscsi_hba *phba)
  2859. {
  2860. struct hwi_controller *phwi_ctrlr;
  2861. phwi_ctrlr = phba->phwi_ctrlr;
  2862. if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
  2863. phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
  2864. init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
  2865. SE_DEBUG(DBG_LVL_8, " phwi_ctrlr->phwi_ctxt=%p\n",
  2866. phwi_ctrlr->phwi_ctxt);
  2867. } else {
  2868. shost_printk(KERN_ERR, phba->shost,
  2869. "HWI_MEM_ADDN_CONTEXT is more than one element."
  2870. "Failing to load\n");
  2871. return -ENOMEM;
  2872. }
  2873. iscsi_init_global_templates(phba);
  2874. beiscsi_init_wrb_handle(phba);
  2875. hwi_init_async_pdu_ctx(phba);
  2876. if (hwi_init_port(phba) != 0) {
  2877. shost_printk(KERN_ERR, phba->shost,
  2878. "hwi_init_controller failed\n");
  2879. return -ENOMEM;
  2880. }
  2881. return 0;
  2882. }
  2883. static void beiscsi_free_mem(struct beiscsi_hba *phba)
  2884. {
  2885. struct be_mem_descriptor *mem_descr;
  2886. int i, j;
  2887. mem_descr = phba->init_mem;
  2888. i = 0;
  2889. j = 0;
  2890. for (i = 0; i < SE_MEM_MAX; i++) {
  2891. for (j = mem_descr->num_elements; j > 0; j--) {
  2892. pci_free_consistent(phba->pcidev,
  2893. mem_descr->mem_array[j - 1].size,
  2894. mem_descr->mem_array[j - 1].virtual_address,
  2895. (unsigned long)mem_descr->mem_array[j - 1].
  2896. bus_address.u.a64.address);
  2897. }
  2898. kfree(mem_descr->mem_array);
  2899. mem_descr++;
  2900. }
  2901. kfree(phba->init_mem);
  2902. kfree(phba->phwi_ctrlr);
  2903. }
  2904. static int beiscsi_init_controller(struct beiscsi_hba *phba)
  2905. {
  2906. int ret = -ENOMEM;
  2907. ret = beiscsi_get_memory(phba);
  2908. if (ret < 0) {
  2909. shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe -"
  2910. "Failed in beiscsi_alloc_memory\n");
  2911. return ret;
  2912. }
  2913. ret = hwi_init_controller(phba);
  2914. if (ret)
  2915. goto free_init;
  2916. SE_DEBUG(DBG_LVL_8, "Return success from beiscsi_init_controller");
  2917. return 0;
  2918. free_init:
  2919. beiscsi_free_mem(phba);
  2920. return -ENOMEM;
  2921. }
  2922. static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
  2923. {
  2924. struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
  2925. struct sgl_handle *psgl_handle;
  2926. struct iscsi_sge *pfrag;
  2927. unsigned int arr_index, i, idx;
  2928. phba->io_sgl_hndl_avbl = 0;
  2929. phba->eh_sgl_hndl_avbl = 0;
  2930. mem_descr_sglh = phba->init_mem;
  2931. mem_descr_sglh += HWI_MEM_SGLH;
  2932. if (1 == mem_descr_sglh->num_elements) {
  2933. phba->io_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
  2934. phba->params.ios_per_ctrl,
  2935. GFP_KERNEL);
  2936. if (!phba->io_sgl_hndl_base) {
  2937. shost_printk(KERN_ERR, phba->shost,
  2938. "Mem Alloc Failed. Failing to load\n");
  2939. return -ENOMEM;
  2940. }
  2941. phba->eh_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
  2942. (phba->params.icds_per_ctrl -
  2943. phba->params.ios_per_ctrl),
  2944. GFP_KERNEL);
  2945. if (!phba->eh_sgl_hndl_base) {
  2946. kfree(phba->io_sgl_hndl_base);
  2947. shost_printk(KERN_ERR, phba->shost,
  2948. "Mem Alloc Failed. Failing to load\n");
  2949. return -ENOMEM;
  2950. }
  2951. } else {
  2952. shost_printk(KERN_ERR, phba->shost,
  2953. "HWI_MEM_SGLH is more than one element."
  2954. "Failing to load\n");
  2955. return -ENOMEM;
  2956. }
  2957. arr_index = 0;
  2958. idx = 0;
  2959. while (idx < mem_descr_sglh->num_elements) {
  2960. psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
  2961. for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
  2962. sizeof(struct sgl_handle)); i++) {
  2963. if (arr_index < phba->params.ios_per_ctrl) {
  2964. phba->io_sgl_hndl_base[arr_index] = psgl_handle;
  2965. phba->io_sgl_hndl_avbl++;
  2966. arr_index++;
  2967. } else {
  2968. phba->eh_sgl_hndl_base[arr_index -
  2969. phba->params.ios_per_ctrl] =
  2970. psgl_handle;
  2971. arr_index++;
  2972. phba->eh_sgl_hndl_avbl++;
  2973. }
  2974. psgl_handle++;
  2975. }
  2976. idx++;
  2977. }
  2978. SE_DEBUG(DBG_LVL_8,
  2979. "phba->io_sgl_hndl_avbl=%d"
  2980. "phba->eh_sgl_hndl_avbl=%d\n",
  2981. phba->io_sgl_hndl_avbl,
  2982. phba->eh_sgl_hndl_avbl);
  2983. mem_descr_sg = phba->init_mem;
  2984. mem_descr_sg += HWI_MEM_SGE;
  2985. SE_DEBUG(DBG_LVL_8, "\n mem_descr_sg->num_elements=%d\n",
  2986. mem_descr_sg->num_elements);
  2987. arr_index = 0;
  2988. idx = 0;
  2989. while (idx < mem_descr_sg->num_elements) {
  2990. pfrag = mem_descr_sg->mem_array[idx].virtual_address;
  2991. for (i = 0;
  2992. i < (mem_descr_sg->mem_array[idx].size) /
  2993. (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
  2994. i++) {
  2995. if (arr_index < phba->params.ios_per_ctrl)
  2996. psgl_handle = phba->io_sgl_hndl_base[arr_index];
  2997. else
  2998. psgl_handle = phba->eh_sgl_hndl_base[arr_index -
  2999. phba->params.ios_per_ctrl];
  3000. psgl_handle->pfrag = pfrag;
  3001. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
  3002. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
  3003. pfrag += phba->params.num_sge_per_io;
  3004. psgl_handle->sgl_index =
  3005. phba->fw_config.iscsi_icd_start + arr_index++;
  3006. }
  3007. idx++;
  3008. }
  3009. phba->io_sgl_free_index = 0;
  3010. phba->io_sgl_alloc_index = 0;
  3011. phba->eh_sgl_free_index = 0;
  3012. phba->eh_sgl_alloc_index = 0;
  3013. return 0;
  3014. }
  3015. static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
  3016. {
  3017. int i, new_cid;
  3018. phba->cid_array = kzalloc(sizeof(void *) * phba->params.cxns_per_ctrl,
  3019. GFP_KERNEL);
  3020. if (!phba->cid_array) {
  3021. shost_printk(KERN_ERR, phba->shost,
  3022. "Failed to allocate memory in "
  3023. "hba_setup_cid_tbls\n");
  3024. return -ENOMEM;
  3025. }
  3026. phba->ep_array = kzalloc(sizeof(struct iscsi_endpoint *) *
  3027. phba->params.cxns_per_ctrl * 2, GFP_KERNEL);
  3028. if (!phba->ep_array) {
  3029. shost_printk(KERN_ERR, phba->shost,
  3030. "Failed to allocate memory in "
  3031. "hba_setup_cid_tbls\n");
  3032. kfree(phba->cid_array);
  3033. return -ENOMEM;
  3034. }
  3035. new_cid = phba->fw_config.iscsi_cid_start;
  3036. for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
  3037. phba->cid_array[i] = new_cid;
  3038. new_cid += 2;
  3039. }
  3040. phba->avlbl_cids = phba->params.cxns_per_ctrl;
  3041. return 0;
  3042. }
  3043. static void hwi_enable_intr(struct beiscsi_hba *phba)
  3044. {
  3045. struct be_ctrl_info *ctrl = &phba->ctrl;
  3046. struct hwi_controller *phwi_ctrlr;
  3047. struct hwi_context_memory *phwi_context;
  3048. struct be_queue_info *eq;
  3049. u8 __iomem *addr;
  3050. u32 reg, i;
  3051. u32 enabled;
  3052. phwi_ctrlr = phba->phwi_ctrlr;
  3053. phwi_context = phwi_ctrlr->phwi_ctxt;
  3054. addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
  3055. PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
  3056. reg = ioread32(addr);
  3057. enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  3058. if (!enabled) {
  3059. reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  3060. SE_DEBUG(DBG_LVL_8, "reg =x%08x addr=%p\n", reg, addr);
  3061. iowrite32(reg, addr);
  3062. }
  3063. if (!phba->msix_enabled) {
  3064. eq = &phwi_context->be_eq[0].q;
  3065. SE_DEBUG(DBG_LVL_8, "eq->id=%d\n", eq->id);
  3066. hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
  3067. } else {
  3068. for (i = 0; i <= phba->num_cpus; i++) {
  3069. eq = &phwi_context->be_eq[i].q;
  3070. SE_DEBUG(DBG_LVL_8, "eq->id=%d\n", eq->id);
  3071. hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
  3072. }
  3073. }
  3074. }
  3075. static void hwi_disable_intr(struct beiscsi_hba *phba)
  3076. {
  3077. struct be_ctrl_info *ctrl = &phba->ctrl;
  3078. u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
  3079. u32 reg = ioread32(addr);
  3080. u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  3081. if (enabled) {
  3082. reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  3083. iowrite32(reg, addr);
  3084. } else
  3085. shost_printk(KERN_WARNING, phba->shost,
  3086. "In hwi_disable_intr, Already Disabled\n");
  3087. }
  3088. static int beiscsi_get_boot_info(struct beiscsi_hba *phba)
  3089. {
  3090. struct be_cmd_resp_get_boot_target *boot_resp;
  3091. struct be_cmd_resp_get_session *session_resp;
  3092. struct be_mcc_wrb *wrb;
  3093. struct be_dma_mem nonemb_cmd;
  3094. unsigned int tag, wrb_num;
  3095. unsigned short status, extd_status;
  3096. struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
  3097. int ret = -ENOMEM;
  3098. tag = beiscsi_get_boot_target(phba);
  3099. if (!tag) {
  3100. SE_DEBUG(DBG_LVL_1, "be_cmd_get_mac_addr Failed\n");
  3101. return -EAGAIN;
  3102. } else
  3103. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  3104. phba->ctrl.mcc_numtag[tag]);
  3105. wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
  3106. extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
  3107. status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
  3108. if (status || extd_status) {
  3109. SE_DEBUG(DBG_LVL_1, "be_cmd_get_mac_addr Failed"
  3110. " status = %d extd_status = %d\n",
  3111. status, extd_status);
  3112. free_mcc_tag(&phba->ctrl, tag);
  3113. return -EBUSY;
  3114. }
  3115. wrb = queue_get_wrb(mccq, wrb_num);
  3116. free_mcc_tag(&phba->ctrl, tag);
  3117. boot_resp = embedded_payload(wrb);
  3118. if (boot_resp->boot_session_handle < 0) {
  3119. shost_printk(KERN_INFO, phba->shost, "No Boot Session.\n");
  3120. return -ENXIO;
  3121. }
  3122. nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
  3123. sizeof(*session_resp),
  3124. &nonemb_cmd.dma);
  3125. if (nonemb_cmd.va == NULL) {
  3126. SE_DEBUG(DBG_LVL_1,
  3127. "Failed to allocate memory for"
  3128. "beiscsi_get_session_info\n");
  3129. return -ENOMEM;
  3130. }
  3131. memset(nonemb_cmd.va, 0, sizeof(*session_resp));
  3132. tag = beiscsi_get_session_info(phba,
  3133. boot_resp->boot_session_handle, &nonemb_cmd);
  3134. if (!tag) {
  3135. SE_DEBUG(DBG_LVL_1, "beiscsi_get_session_info"
  3136. " Failed\n");
  3137. goto boot_freemem;
  3138. } else
  3139. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  3140. phba->ctrl.mcc_numtag[tag]);
  3141. wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
  3142. extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
  3143. status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
  3144. if (status || extd_status) {
  3145. SE_DEBUG(DBG_LVL_1, "beiscsi_get_session_info Failed"
  3146. " status = %d extd_status = %d\n",
  3147. status, extd_status);
  3148. free_mcc_tag(&phba->ctrl, tag);
  3149. goto boot_freemem;
  3150. }
  3151. wrb = queue_get_wrb(mccq, wrb_num);
  3152. free_mcc_tag(&phba->ctrl, tag);
  3153. session_resp = nonemb_cmd.va ;
  3154. memcpy(&phba->boot_sess, &session_resp->session_info,
  3155. sizeof(struct mgmt_session_info));
  3156. ret = 0;
  3157. boot_freemem:
  3158. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  3159. nonemb_cmd.va, nonemb_cmd.dma);
  3160. return ret;
  3161. }
  3162. static void beiscsi_boot_release(void *data)
  3163. {
  3164. struct beiscsi_hba *phba = data;
  3165. scsi_host_put(phba->shost);
  3166. }
  3167. static int beiscsi_setup_boot_info(struct beiscsi_hba *phba)
  3168. {
  3169. struct iscsi_boot_kobj *boot_kobj;
  3170. /* get boot info using mgmt cmd */
  3171. if (beiscsi_get_boot_info(phba))
  3172. /* Try to see if we can carry on without this */
  3173. return 0;
  3174. phba->boot_kset = iscsi_boot_create_host_kset(phba->shost->host_no);
  3175. if (!phba->boot_kset)
  3176. return -ENOMEM;
  3177. /* get a ref because the show function will ref the phba */
  3178. if (!scsi_host_get(phba->shost))
  3179. goto free_kset;
  3180. boot_kobj = iscsi_boot_create_target(phba->boot_kset, 0, phba,
  3181. beiscsi_show_boot_tgt_info,
  3182. beiscsi_tgt_get_attr_visibility,
  3183. beiscsi_boot_release);
  3184. if (!boot_kobj)
  3185. goto put_shost;
  3186. if (!scsi_host_get(phba->shost))
  3187. goto free_kset;
  3188. boot_kobj = iscsi_boot_create_initiator(phba->boot_kset, 0, phba,
  3189. beiscsi_show_boot_ini_info,
  3190. beiscsi_ini_get_attr_visibility,
  3191. beiscsi_boot_release);
  3192. if (!boot_kobj)
  3193. goto put_shost;
  3194. if (!scsi_host_get(phba->shost))
  3195. goto free_kset;
  3196. boot_kobj = iscsi_boot_create_ethernet(phba->boot_kset, 0, phba,
  3197. beiscsi_show_boot_eth_info,
  3198. beiscsi_eth_get_attr_visibility,
  3199. beiscsi_boot_release);
  3200. if (!boot_kobj)
  3201. goto put_shost;
  3202. return 0;
  3203. put_shost:
  3204. scsi_host_put(phba->shost);
  3205. free_kset:
  3206. iscsi_boot_destroy_kset(phba->boot_kset);
  3207. return -ENOMEM;
  3208. }
  3209. static int beiscsi_init_port(struct beiscsi_hba *phba)
  3210. {
  3211. int ret;
  3212. ret = beiscsi_init_controller(phba);
  3213. if (ret < 0) {
  3214. shost_printk(KERN_ERR, phba->shost,
  3215. "beiscsi_dev_probe - Failed in"
  3216. "beiscsi_init_controller\n");
  3217. return ret;
  3218. }
  3219. ret = beiscsi_init_sgl_handle(phba);
  3220. if (ret < 0) {
  3221. shost_printk(KERN_ERR, phba->shost,
  3222. "beiscsi_dev_probe - Failed in"
  3223. "beiscsi_init_sgl_handle\n");
  3224. goto do_cleanup_ctrlr;
  3225. }
  3226. if (hba_setup_cid_tbls(phba)) {
  3227. shost_printk(KERN_ERR, phba->shost,
  3228. "Failed in hba_setup_cid_tbls\n");
  3229. kfree(phba->io_sgl_hndl_base);
  3230. kfree(phba->eh_sgl_hndl_base);
  3231. goto do_cleanup_ctrlr;
  3232. }
  3233. return ret;
  3234. do_cleanup_ctrlr:
  3235. hwi_cleanup(phba);
  3236. return ret;
  3237. }
  3238. static void hwi_purge_eq(struct beiscsi_hba *phba)
  3239. {
  3240. struct hwi_controller *phwi_ctrlr;
  3241. struct hwi_context_memory *phwi_context;
  3242. struct be_queue_info *eq;
  3243. struct be_eq_entry *eqe = NULL;
  3244. int i, eq_msix;
  3245. unsigned int num_processed;
  3246. phwi_ctrlr = phba->phwi_ctrlr;
  3247. phwi_context = phwi_ctrlr->phwi_ctxt;
  3248. if (phba->msix_enabled)
  3249. eq_msix = 1;
  3250. else
  3251. eq_msix = 0;
  3252. for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
  3253. eq = &phwi_context->be_eq[i].q;
  3254. eqe = queue_tail_node(eq);
  3255. num_processed = 0;
  3256. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  3257. & EQE_VALID_MASK) {
  3258. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  3259. queue_tail_inc(eq);
  3260. eqe = queue_tail_node(eq);
  3261. num_processed++;
  3262. }
  3263. if (num_processed)
  3264. hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
  3265. }
  3266. }
  3267. static void beiscsi_clean_port(struct beiscsi_hba *phba)
  3268. {
  3269. int mgmt_status;
  3270. mgmt_status = mgmt_epfw_cleanup(phba, CMD_CONNECTION_CHUTE_0);
  3271. if (mgmt_status)
  3272. shost_printk(KERN_WARNING, phba->shost,
  3273. "mgmt_epfw_cleanup FAILED\n");
  3274. hwi_purge_eq(phba);
  3275. hwi_cleanup(phba);
  3276. kfree(phba->io_sgl_hndl_base);
  3277. kfree(phba->eh_sgl_hndl_base);
  3278. kfree(phba->cid_array);
  3279. kfree(phba->ep_array);
  3280. }
  3281. void
  3282. beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
  3283. struct beiscsi_offload_params *params)
  3284. {
  3285. struct wrb_handle *pwrb_handle;
  3286. struct iscsi_target_context_update_wrb *pwrb = NULL;
  3287. struct be_mem_descriptor *mem_descr;
  3288. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3289. u32 doorbell = 0;
  3290. /*
  3291. * We can always use 0 here because it is reserved by libiscsi for
  3292. * login/startup related tasks.
  3293. */
  3294. pwrb_handle = alloc_wrb_handle(phba, (beiscsi_conn->beiscsi_conn_cid -
  3295. phba->fw_config.iscsi_cid_start));
  3296. pwrb = (struct iscsi_target_context_update_wrb *)pwrb_handle->pwrb;
  3297. memset(pwrb, 0, sizeof(*pwrb));
  3298. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
  3299. max_burst_length, pwrb, params->dw[offsetof
  3300. (struct amap_beiscsi_offload_params,
  3301. max_burst_length) / 32]);
  3302. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
  3303. max_send_data_segment_length, pwrb,
  3304. params->dw[offsetof(struct amap_beiscsi_offload_params,
  3305. max_send_data_segment_length) / 32]);
  3306. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
  3307. first_burst_length,
  3308. pwrb,
  3309. params->dw[offsetof(struct amap_beiscsi_offload_params,
  3310. first_burst_length) / 32]);
  3311. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, erl, pwrb,
  3312. (params->dw[offsetof(struct amap_beiscsi_offload_params,
  3313. erl) / 32] & OFFLD_PARAMS_ERL));
  3314. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, dde, pwrb,
  3315. (params->dw[offsetof(struct amap_beiscsi_offload_params,
  3316. dde) / 32] & OFFLD_PARAMS_DDE) >> 2);
  3317. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, hde, pwrb,
  3318. (params->dw[offsetof(struct amap_beiscsi_offload_params,
  3319. hde) / 32] & OFFLD_PARAMS_HDE) >> 3);
  3320. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ir2t, pwrb,
  3321. (params->dw[offsetof(struct amap_beiscsi_offload_params,
  3322. ir2t) / 32] & OFFLD_PARAMS_IR2T) >> 4);
  3323. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, imd, pwrb,
  3324. (params->dw[offsetof(struct amap_beiscsi_offload_params,
  3325. imd) / 32] & OFFLD_PARAMS_IMD) >> 5);
  3326. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, stat_sn,
  3327. pwrb,
  3328. (params->dw[offsetof(struct amap_beiscsi_offload_params,
  3329. exp_statsn) / 32] + 1));
  3330. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, type, pwrb,
  3331. 0x7);
  3332. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, wrb_idx,
  3333. pwrb, pwrb_handle->wrb_index);
  3334. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ptr2nextwrb,
  3335. pwrb, pwrb_handle->nxt_wrb_index);
  3336. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
  3337. session_state, pwrb, 0);
  3338. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, compltonack,
  3339. pwrb, 1);
  3340. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, notpredblq,
  3341. pwrb, 0);
  3342. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, mode, pwrb,
  3343. 0);
  3344. mem_descr = phba->init_mem;
  3345. mem_descr += ISCSI_MEM_GLOBAL_HEADER;
  3346. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
  3347. pad_buffer_addr_hi, pwrb,
  3348. mem_descr->mem_array[0].bus_address.u.a32.address_hi);
  3349. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
  3350. pad_buffer_addr_lo, pwrb,
  3351. mem_descr->mem_array[0].bus_address.u.a32.address_lo);
  3352. be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_target_context_update_wrb));
  3353. doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
  3354. doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
  3355. << DB_DEF_PDU_WRB_INDEX_SHIFT;
  3356. doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
  3357. iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
  3358. }
  3359. static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
  3360. int *index, int *age)
  3361. {
  3362. *index = (int)itt;
  3363. if (age)
  3364. *age = conn->session->age;
  3365. }
  3366. /**
  3367. * beiscsi_alloc_pdu - allocates pdu and related resources
  3368. * @task: libiscsi task
  3369. * @opcode: opcode of pdu for task
  3370. *
  3371. * This is called with the session lock held. It will allocate
  3372. * the wrb and sgl if needed for the command. And it will prep
  3373. * the pdu's itt. beiscsi_parse_pdu will later translate
  3374. * the pdu itt to the libiscsi task itt.
  3375. */
  3376. static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
  3377. {
  3378. struct beiscsi_io_task *io_task = task->dd_data;
  3379. struct iscsi_conn *conn = task->conn;
  3380. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  3381. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3382. struct hwi_wrb_context *pwrb_context;
  3383. struct hwi_controller *phwi_ctrlr;
  3384. itt_t itt;
  3385. struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
  3386. dma_addr_t paddr;
  3387. io_task->cmd_bhs = pci_pool_alloc(beiscsi_sess->bhs_pool,
  3388. GFP_ATOMIC, &paddr);
  3389. if (!io_task->cmd_bhs)
  3390. return -ENOMEM;
  3391. io_task->bhs_pa.u.a64.address = paddr;
  3392. io_task->libiscsi_itt = (itt_t)task->itt;
  3393. io_task->conn = beiscsi_conn;
  3394. task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
  3395. task->hdr_max = sizeof(struct be_cmd_bhs);
  3396. io_task->psgl_handle = NULL;
  3397. io_task->psgl_handle = NULL;
  3398. if (task->sc) {
  3399. spin_lock(&phba->io_sgl_lock);
  3400. io_task->psgl_handle = alloc_io_sgl_handle(phba);
  3401. spin_unlock(&phba->io_sgl_lock);
  3402. if (!io_task->psgl_handle)
  3403. goto free_hndls;
  3404. io_task->pwrb_handle = alloc_wrb_handle(phba,
  3405. beiscsi_conn->beiscsi_conn_cid -
  3406. phba->fw_config.iscsi_cid_start);
  3407. if (!io_task->pwrb_handle)
  3408. goto free_io_hndls;
  3409. } else {
  3410. io_task->scsi_cmnd = NULL;
  3411. if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
  3412. if (!beiscsi_conn->login_in_progress) {
  3413. spin_lock(&phba->mgmt_sgl_lock);
  3414. io_task->psgl_handle = (struct sgl_handle *)
  3415. alloc_mgmt_sgl_handle(phba);
  3416. spin_unlock(&phba->mgmt_sgl_lock);
  3417. if (!io_task->psgl_handle)
  3418. goto free_hndls;
  3419. beiscsi_conn->login_in_progress = 1;
  3420. beiscsi_conn->plogin_sgl_handle =
  3421. io_task->psgl_handle;
  3422. io_task->pwrb_handle =
  3423. alloc_wrb_handle(phba,
  3424. beiscsi_conn->beiscsi_conn_cid -
  3425. phba->fw_config.iscsi_cid_start);
  3426. if (!io_task->pwrb_handle)
  3427. goto free_io_hndls;
  3428. beiscsi_conn->plogin_wrb_handle =
  3429. io_task->pwrb_handle;
  3430. } else {
  3431. io_task->psgl_handle =
  3432. beiscsi_conn->plogin_sgl_handle;
  3433. io_task->pwrb_handle =
  3434. beiscsi_conn->plogin_wrb_handle;
  3435. }
  3436. } else {
  3437. spin_lock(&phba->mgmt_sgl_lock);
  3438. io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
  3439. spin_unlock(&phba->mgmt_sgl_lock);
  3440. if (!io_task->psgl_handle)
  3441. goto free_hndls;
  3442. io_task->pwrb_handle =
  3443. alloc_wrb_handle(phba,
  3444. beiscsi_conn->beiscsi_conn_cid -
  3445. phba->fw_config.iscsi_cid_start);
  3446. if (!io_task->pwrb_handle)
  3447. goto free_mgmt_hndls;
  3448. }
  3449. }
  3450. itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
  3451. wrb_index << 16) | (unsigned int)
  3452. (io_task->psgl_handle->sgl_index));
  3453. io_task->pwrb_handle->pio_handle = task;
  3454. io_task->cmd_bhs->iscsi_hdr.itt = itt;
  3455. return 0;
  3456. free_io_hndls:
  3457. spin_lock(&phba->io_sgl_lock);
  3458. free_io_sgl_handle(phba, io_task->psgl_handle);
  3459. spin_unlock(&phba->io_sgl_lock);
  3460. goto free_hndls;
  3461. free_mgmt_hndls:
  3462. spin_lock(&phba->mgmt_sgl_lock);
  3463. free_mgmt_sgl_handle(phba, io_task->psgl_handle);
  3464. spin_unlock(&phba->mgmt_sgl_lock);
  3465. free_hndls:
  3466. phwi_ctrlr = phba->phwi_ctrlr;
  3467. pwrb_context = &phwi_ctrlr->wrb_context[
  3468. beiscsi_conn->beiscsi_conn_cid -
  3469. phba->fw_config.iscsi_cid_start];
  3470. if (io_task->pwrb_handle)
  3471. free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
  3472. io_task->pwrb_handle = NULL;
  3473. pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
  3474. io_task->bhs_pa.u.a64.address);
  3475. SE_DEBUG(DBG_LVL_1, "Alloc of SGL_ICD Failed\n");
  3476. return -ENOMEM;
  3477. }
  3478. static void beiscsi_cleanup_task(struct iscsi_task *task)
  3479. {
  3480. struct beiscsi_io_task *io_task = task->dd_data;
  3481. struct iscsi_conn *conn = task->conn;
  3482. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  3483. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3484. struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
  3485. struct hwi_wrb_context *pwrb_context;
  3486. struct hwi_controller *phwi_ctrlr;
  3487. phwi_ctrlr = phba->phwi_ctrlr;
  3488. pwrb_context = &phwi_ctrlr->wrb_context[beiscsi_conn->beiscsi_conn_cid
  3489. - phba->fw_config.iscsi_cid_start];
  3490. if (io_task->pwrb_handle) {
  3491. free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
  3492. io_task->pwrb_handle = NULL;
  3493. }
  3494. if (io_task->cmd_bhs) {
  3495. pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
  3496. io_task->bhs_pa.u.a64.address);
  3497. }
  3498. if (task->sc) {
  3499. if (io_task->psgl_handle) {
  3500. spin_lock(&phba->io_sgl_lock);
  3501. free_io_sgl_handle(phba, io_task->psgl_handle);
  3502. spin_unlock(&phba->io_sgl_lock);
  3503. io_task->psgl_handle = NULL;
  3504. }
  3505. } else {
  3506. if (task->hdr &&
  3507. ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN))
  3508. return;
  3509. if (io_task->psgl_handle) {
  3510. spin_lock(&phba->mgmt_sgl_lock);
  3511. free_mgmt_sgl_handle(phba, io_task->psgl_handle);
  3512. spin_unlock(&phba->mgmt_sgl_lock);
  3513. io_task->psgl_handle = NULL;
  3514. }
  3515. }
  3516. }
  3517. static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
  3518. unsigned int num_sg, unsigned int xferlen,
  3519. unsigned int writedir)
  3520. {
  3521. struct beiscsi_io_task *io_task = task->dd_data;
  3522. struct iscsi_conn *conn = task->conn;
  3523. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  3524. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3525. struct iscsi_wrb *pwrb = NULL;
  3526. unsigned int doorbell = 0;
  3527. pwrb = io_task->pwrb_handle->pwrb;
  3528. io_task->cmd_bhs->iscsi_hdr.exp_statsn = 0;
  3529. io_task->bhs_len = sizeof(struct be_cmd_bhs);
  3530. if (writedir) {
  3531. memset(&io_task->cmd_bhs->iscsi_data_pdu, 0, 48);
  3532. AMAP_SET_BITS(struct amap_pdu_data_out, itt,
  3533. &io_task->cmd_bhs->iscsi_data_pdu,
  3534. (unsigned int)io_task->cmd_bhs->iscsi_hdr.itt);
  3535. AMAP_SET_BITS(struct amap_pdu_data_out, opcode,
  3536. &io_task->cmd_bhs->iscsi_data_pdu,
  3537. ISCSI_OPCODE_SCSI_DATA_OUT);
  3538. AMAP_SET_BITS(struct amap_pdu_data_out, final_bit,
  3539. &io_task->cmd_bhs->iscsi_data_pdu, 1);
  3540. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3541. INI_WR_CMD);
  3542. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
  3543. } else {
  3544. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3545. INI_RD_CMD);
  3546. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
  3547. }
  3548. memcpy(&io_task->cmd_bhs->iscsi_data_pdu.
  3549. dw[offsetof(struct amap_pdu_data_out, lun) / 32],
  3550. &io_task->cmd_bhs->iscsi_hdr.lun, sizeof(struct scsi_lun));
  3551. AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
  3552. cpu_to_be16(*(unsigned short *)&io_task->cmd_bhs->iscsi_hdr.lun));
  3553. AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
  3554. AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
  3555. io_task->pwrb_handle->wrb_index);
  3556. AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
  3557. be32_to_cpu(task->cmdsn));
  3558. AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
  3559. io_task->psgl_handle->sgl_index);
  3560. hwi_write_sgl(pwrb, sg, num_sg, io_task);
  3561. AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
  3562. io_task->pwrb_handle->nxt_wrb_index);
  3563. be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
  3564. doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
  3565. doorbell |= (io_task->pwrb_handle->wrb_index &
  3566. DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
  3567. doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
  3568. iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
  3569. return 0;
  3570. }
  3571. static int beiscsi_mtask(struct iscsi_task *task)
  3572. {
  3573. struct beiscsi_io_task *io_task = task->dd_data;
  3574. struct iscsi_conn *conn = task->conn;
  3575. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  3576. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3577. struct iscsi_wrb *pwrb = NULL;
  3578. unsigned int doorbell = 0;
  3579. unsigned int cid;
  3580. cid = beiscsi_conn->beiscsi_conn_cid;
  3581. pwrb = io_task->pwrb_handle->pwrb;
  3582. memset(pwrb, 0, sizeof(*pwrb));
  3583. AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
  3584. be32_to_cpu(task->cmdsn));
  3585. AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
  3586. io_task->pwrb_handle->wrb_index);
  3587. AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
  3588. io_task->psgl_handle->sgl_index);
  3589. switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
  3590. case ISCSI_OP_LOGIN:
  3591. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3592. TGT_DM_CMD);
  3593. AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
  3594. AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
  3595. hwi_write_buffer(pwrb, task);
  3596. break;
  3597. case ISCSI_OP_NOOP_OUT:
  3598. if (task->hdr->ttt != ISCSI_RESERVED_TAG) {
  3599. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3600. TGT_DM_CMD);
  3601. AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt,
  3602. pwrb, 0);
  3603. AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
  3604. } else {
  3605. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3606. INI_RD_CMD);
  3607. AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 1);
  3608. }
  3609. hwi_write_buffer(pwrb, task);
  3610. break;
  3611. case ISCSI_OP_TEXT:
  3612. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3613. TGT_DM_CMD);
  3614. AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
  3615. hwi_write_buffer(pwrb, task);
  3616. break;
  3617. case ISCSI_OP_SCSI_TMFUNC:
  3618. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3619. INI_TMF_CMD);
  3620. AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
  3621. hwi_write_buffer(pwrb, task);
  3622. break;
  3623. case ISCSI_OP_LOGOUT:
  3624. AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
  3625. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3626. HWH_TYPE_LOGOUT);
  3627. hwi_write_buffer(pwrb, task);
  3628. break;
  3629. default:
  3630. SE_DEBUG(DBG_LVL_1, "opcode =%d Not supported\n",
  3631. task->hdr->opcode & ISCSI_OPCODE_MASK);
  3632. return -EINVAL;
  3633. }
  3634. AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
  3635. task->data_count);
  3636. AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
  3637. io_task->pwrb_handle->nxt_wrb_index);
  3638. be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
  3639. doorbell |= cid & DB_WRB_POST_CID_MASK;
  3640. doorbell |= (io_task->pwrb_handle->wrb_index &
  3641. DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
  3642. doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
  3643. iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
  3644. return 0;
  3645. }
  3646. static int beiscsi_task_xmit(struct iscsi_task *task)
  3647. {
  3648. struct beiscsi_io_task *io_task = task->dd_data;
  3649. struct scsi_cmnd *sc = task->sc;
  3650. struct scatterlist *sg;
  3651. int num_sg;
  3652. unsigned int writedir = 0, xferlen = 0;
  3653. if (!sc)
  3654. return beiscsi_mtask(task);
  3655. io_task->scsi_cmnd = sc;
  3656. num_sg = scsi_dma_map(sc);
  3657. if (num_sg < 0) {
  3658. SE_DEBUG(DBG_LVL_1, " scsi_dma_map Failed\n")
  3659. return num_sg;
  3660. }
  3661. xferlen = scsi_bufflen(sc);
  3662. sg = scsi_sglist(sc);
  3663. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  3664. writedir = 1;
  3665. SE_DEBUG(DBG_LVL_4, "task->imm_count=0x%08x\n",
  3666. task->imm_count);
  3667. } else
  3668. writedir = 0;
  3669. return beiscsi_iotask(task, sg, num_sg, xferlen, writedir);
  3670. }
  3671. static void beiscsi_remove(struct pci_dev *pcidev)
  3672. {
  3673. struct beiscsi_hba *phba = NULL;
  3674. struct hwi_controller *phwi_ctrlr;
  3675. struct hwi_context_memory *phwi_context;
  3676. struct be_eq_obj *pbe_eq;
  3677. unsigned int i, msix_vec;
  3678. u8 *real_offset = 0;
  3679. u32 value = 0;
  3680. phba = (struct beiscsi_hba *)pci_get_drvdata(pcidev);
  3681. if (!phba) {
  3682. dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
  3683. return;
  3684. }
  3685. phwi_ctrlr = phba->phwi_ctrlr;
  3686. phwi_context = phwi_ctrlr->phwi_ctxt;
  3687. hwi_disable_intr(phba);
  3688. if (phba->msix_enabled) {
  3689. for (i = 0; i <= phba->num_cpus; i++) {
  3690. msix_vec = phba->msix_entries[i].vector;
  3691. free_irq(msix_vec, &phwi_context->be_eq[i]);
  3692. }
  3693. } else
  3694. if (phba->pcidev->irq)
  3695. free_irq(phba->pcidev->irq, phba);
  3696. pci_disable_msix(phba->pcidev);
  3697. destroy_workqueue(phba->wq);
  3698. if (blk_iopoll_enabled)
  3699. for (i = 0; i < phba->num_cpus; i++) {
  3700. pbe_eq = &phwi_context->be_eq[i];
  3701. blk_iopoll_disable(&pbe_eq->iopoll);
  3702. }
  3703. beiscsi_clean_port(phba);
  3704. beiscsi_free_mem(phba);
  3705. real_offset = (u8 *)phba->csr_va + MPU_EP_SEMAPHORE;
  3706. value = readl((void *)real_offset);
  3707. if (value & 0x00010000) {
  3708. value &= 0xfffeffff;
  3709. writel(value, (void *)real_offset);
  3710. }
  3711. beiscsi_unmap_pci_function(phba);
  3712. pci_free_consistent(phba->pcidev,
  3713. phba->ctrl.mbox_mem_alloced.size,
  3714. phba->ctrl.mbox_mem_alloced.va,
  3715. phba->ctrl.mbox_mem_alloced.dma);
  3716. iscsi_boot_destroy_kset(phba->boot_kset);
  3717. iscsi_host_remove(phba->shost);
  3718. pci_dev_put(phba->pcidev);
  3719. iscsi_host_free(phba->shost);
  3720. }
  3721. static void beiscsi_msix_enable(struct beiscsi_hba *phba)
  3722. {
  3723. int i, status;
  3724. for (i = 0; i <= phba->num_cpus; i++)
  3725. phba->msix_entries[i].entry = i;
  3726. status = pci_enable_msix(phba->pcidev, phba->msix_entries,
  3727. (phba->num_cpus + 1));
  3728. if (!status)
  3729. phba->msix_enabled = true;
  3730. return;
  3731. }
  3732. static int __devinit beiscsi_dev_probe(struct pci_dev *pcidev,
  3733. const struct pci_device_id *id)
  3734. {
  3735. struct beiscsi_hba *phba = NULL;
  3736. struct hwi_controller *phwi_ctrlr;
  3737. struct hwi_context_memory *phwi_context;
  3738. struct be_eq_obj *pbe_eq;
  3739. int ret, num_cpus, i;
  3740. u8 *real_offset = 0;
  3741. u32 value = 0;
  3742. ret = beiscsi_enable_pci(pcidev);
  3743. if (ret < 0) {
  3744. dev_err(&pcidev->dev, "beiscsi_dev_probe-"
  3745. " Failed to enable pci device\n");
  3746. return ret;
  3747. }
  3748. phba = beiscsi_hba_alloc(pcidev);
  3749. if (!phba) {
  3750. dev_err(&pcidev->dev, "beiscsi_dev_probe-"
  3751. " Failed in beiscsi_hba_alloc\n");
  3752. goto disable_pci;
  3753. }
  3754. switch (pcidev->device) {
  3755. case BE_DEVICE_ID1:
  3756. case OC_DEVICE_ID1:
  3757. case OC_DEVICE_ID2:
  3758. phba->generation = BE_GEN2;
  3759. break;
  3760. case BE_DEVICE_ID2:
  3761. case OC_DEVICE_ID3:
  3762. phba->generation = BE_GEN3;
  3763. break;
  3764. default:
  3765. phba->generation = 0;
  3766. }
  3767. if (enable_msix)
  3768. num_cpus = find_num_cpus();
  3769. else
  3770. num_cpus = 1;
  3771. phba->num_cpus = num_cpus;
  3772. SE_DEBUG(DBG_LVL_8, "num_cpus = %d\n", phba->num_cpus);
  3773. if (enable_msix)
  3774. beiscsi_msix_enable(phba);
  3775. ret = be_ctrl_init(phba, pcidev);
  3776. if (ret) {
  3777. shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
  3778. "Failed in be_ctrl_init\n");
  3779. goto hba_free;
  3780. }
  3781. if (!num_hba) {
  3782. real_offset = (u8 *)phba->csr_va + MPU_EP_SEMAPHORE;
  3783. value = readl((void *)real_offset);
  3784. if (value & 0x00010000) {
  3785. gcrashmode++;
  3786. shost_printk(KERN_ERR, phba->shost,
  3787. "Loading Driver in crashdump mode\n");
  3788. ret = beiscsi_pci_soft_reset(phba);
  3789. if (ret) {
  3790. shost_printk(KERN_ERR, phba->shost,
  3791. "Reset Failed. Aborting Crashdump\n");
  3792. goto hba_free;
  3793. }
  3794. ret = be_chk_reset_complete(phba);
  3795. if (ret) {
  3796. shost_printk(KERN_ERR, phba->shost,
  3797. "Failed to get out of reset."
  3798. "Aborting Crashdump\n");
  3799. goto hba_free;
  3800. }
  3801. } else {
  3802. value |= 0x00010000;
  3803. writel(value, (void *)real_offset);
  3804. num_hba++;
  3805. }
  3806. }
  3807. spin_lock_init(&phba->io_sgl_lock);
  3808. spin_lock_init(&phba->mgmt_sgl_lock);
  3809. spin_lock_init(&phba->isr_lock);
  3810. ret = mgmt_get_fw_config(&phba->ctrl, phba);
  3811. if (ret != 0) {
  3812. shost_printk(KERN_ERR, phba->shost,
  3813. "Error getting fw config\n");
  3814. goto free_port;
  3815. }
  3816. phba->shost->max_id = phba->fw_config.iscsi_cid_count;
  3817. beiscsi_get_params(phba);
  3818. phba->shost->can_queue = phba->params.ios_per_ctrl;
  3819. ret = beiscsi_init_port(phba);
  3820. if (ret < 0) {
  3821. shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
  3822. "Failed in beiscsi_init_port\n");
  3823. goto free_port;
  3824. }
  3825. for (i = 0; i < MAX_MCC_CMD ; i++) {
  3826. init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
  3827. phba->ctrl.mcc_tag[i] = i + 1;
  3828. phba->ctrl.mcc_numtag[i + 1] = 0;
  3829. phba->ctrl.mcc_tag_available++;
  3830. }
  3831. phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
  3832. snprintf(phba->wq_name, sizeof(phba->wq_name), "beiscsi_q_irq%u",
  3833. phba->shost->host_no);
  3834. phba->wq = alloc_workqueue(phba->wq_name, WQ_MEM_RECLAIM, 1);
  3835. if (!phba->wq) {
  3836. shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
  3837. "Failed to allocate work queue\n");
  3838. goto free_twq;
  3839. }
  3840. INIT_WORK(&phba->work_cqs, beiscsi_process_all_cqs);
  3841. phwi_ctrlr = phba->phwi_ctrlr;
  3842. phwi_context = phwi_ctrlr->phwi_ctxt;
  3843. if (blk_iopoll_enabled) {
  3844. for (i = 0; i < phba->num_cpus; i++) {
  3845. pbe_eq = &phwi_context->be_eq[i];
  3846. blk_iopoll_init(&pbe_eq->iopoll, be_iopoll_budget,
  3847. be_iopoll);
  3848. blk_iopoll_enable(&pbe_eq->iopoll);
  3849. }
  3850. }
  3851. ret = beiscsi_init_irqs(phba);
  3852. if (ret < 0) {
  3853. shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
  3854. "Failed to beiscsi_init_irqs\n");
  3855. goto free_blkenbld;
  3856. }
  3857. hwi_enable_intr(phba);
  3858. if (beiscsi_setup_boot_info(phba))
  3859. /*
  3860. * log error but continue, because we may not be using
  3861. * iscsi boot.
  3862. */
  3863. shost_printk(KERN_ERR, phba->shost, "Could not set up "
  3864. "iSCSI boot info.");
  3865. SE_DEBUG(DBG_LVL_8, "\n\n\n SUCCESS - DRIVER LOADED\n\n\n");
  3866. return 0;
  3867. free_blkenbld:
  3868. destroy_workqueue(phba->wq);
  3869. if (blk_iopoll_enabled)
  3870. for (i = 0; i < phba->num_cpus; i++) {
  3871. pbe_eq = &phwi_context->be_eq[i];
  3872. blk_iopoll_disable(&pbe_eq->iopoll);
  3873. }
  3874. free_twq:
  3875. beiscsi_clean_port(phba);
  3876. beiscsi_free_mem(phba);
  3877. free_port:
  3878. real_offset = (u8 *)phba->csr_va + MPU_EP_SEMAPHORE;
  3879. value = readl((void *)real_offset);
  3880. if (value & 0x00010000) {
  3881. value &= 0xfffeffff;
  3882. writel(value, (void *)real_offset);
  3883. }
  3884. pci_free_consistent(phba->pcidev,
  3885. phba->ctrl.mbox_mem_alloced.size,
  3886. phba->ctrl.mbox_mem_alloced.va,
  3887. phba->ctrl.mbox_mem_alloced.dma);
  3888. beiscsi_unmap_pci_function(phba);
  3889. hba_free:
  3890. if (phba->msix_enabled)
  3891. pci_disable_msix(phba->pcidev);
  3892. iscsi_host_remove(phba->shost);
  3893. pci_dev_put(phba->pcidev);
  3894. iscsi_host_free(phba->shost);
  3895. disable_pci:
  3896. pci_disable_device(pcidev);
  3897. return ret;
  3898. }
  3899. struct iscsi_transport beiscsi_iscsi_transport = {
  3900. .owner = THIS_MODULE,
  3901. .name = DRV_NAME,
  3902. .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
  3903. CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
  3904. .param_mask = ISCSI_MAX_RECV_DLENGTH |
  3905. ISCSI_MAX_XMIT_DLENGTH |
  3906. ISCSI_HDRDGST_EN |
  3907. ISCSI_DATADGST_EN |
  3908. ISCSI_INITIAL_R2T_EN |
  3909. ISCSI_MAX_R2T |
  3910. ISCSI_IMM_DATA_EN |
  3911. ISCSI_FIRST_BURST |
  3912. ISCSI_MAX_BURST |
  3913. ISCSI_PDU_INORDER_EN |
  3914. ISCSI_DATASEQ_INORDER_EN |
  3915. ISCSI_ERL |
  3916. ISCSI_CONN_PORT |
  3917. ISCSI_CONN_ADDRESS |
  3918. ISCSI_EXP_STATSN |
  3919. ISCSI_PERSISTENT_PORT |
  3920. ISCSI_PERSISTENT_ADDRESS |
  3921. ISCSI_TARGET_NAME | ISCSI_TPGT |
  3922. ISCSI_USERNAME | ISCSI_PASSWORD |
  3923. ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
  3924. ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
  3925. ISCSI_LU_RESET_TMO |
  3926. ISCSI_PING_TMO | ISCSI_RECV_TMO |
  3927. ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
  3928. .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
  3929. ISCSI_HOST_INITIATOR_NAME,
  3930. .create_session = beiscsi_session_create,
  3931. .destroy_session = beiscsi_session_destroy,
  3932. .create_conn = beiscsi_conn_create,
  3933. .bind_conn = beiscsi_conn_bind,
  3934. .destroy_conn = iscsi_conn_teardown,
  3935. .set_param = beiscsi_set_param,
  3936. .get_conn_param = iscsi_conn_get_param,
  3937. .get_session_param = iscsi_session_get_param,
  3938. .get_host_param = beiscsi_get_host_param,
  3939. .start_conn = beiscsi_conn_start,
  3940. .stop_conn = iscsi_conn_stop,
  3941. .send_pdu = iscsi_conn_send_pdu,
  3942. .xmit_task = beiscsi_task_xmit,
  3943. .cleanup_task = beiscsi_cleanup_task,
  3944. .alloc_pdu = beiscsi_alloc_pdu,
  3945. .parse_pdu_itt = beiscsi_parse_pdu,
  3946. .get_stats = beiscsi_conn_get_stats,
  3947. .get_ep_param = beiscsi_ep_get_param,
  3948. .ep_connect = beiscsi_ep_connect,
  3949. .ep_poll = beiscsi_ep_poll,
  3950. .ep_disconnect = beiscsi_ep_disconnect,
  3951. .session_recovery_timedout = iscsi_session_recovery_timedout,
  3952. };
  3953. static struct pci_driver beiscsi_pci_driver = {
  3954. .name = DRV_NAME,
  3955. .probe = beiscsi_dev_probe,
  3956. .remove = beiscsi_remove,
  3957. .id_table = beiscsi_pci_id_table
  3958. };
  3959. static int __init beiscsi_module_init(void)
  3960. {
  3961. int ret;
  3962. beiscsi_scsi_transport =
  3963. iscsi_register_transport(&beiscsi_iscsi_transport);
  3964. if (!beiscsi_scsi_transport) {
  3965. SE_DEBUG(DBG_LVL_1,
  3966. "beiscsi_module_init - Unable to register beiscsi"
  3967. "transport.\n");
  3968. return -ENOMEM;
  3969. }
  3970. SE_DEBUG(DBG_LVL_8, "In beiscsi_module_init, tt=%p\n",
  3971. &beiscsi_iscsi_transport);
  3972. ret = pci_register_driver(&beiscsi_pci_driver);
  3973. if (ret) {
  3974. SE_DEBUG(DBG_LVL_1,
  3975. "beiscsi_module_init - Unable to register"
  3976. "beiscsi pci driver.\n");
  3977. goto unregister_iscsi_transport;
  3978. }
  3979. return 0;
  3980. unregister_iscsi_transport:
  3981. iscsi_unregister_transport(&beiscsi_iscsi_transport);
  3982. return ret;
  3983. }
  3984. static void __exit beiscsi_module_exit(void)
  3985. {
  3986. pci_unregister_driver(&beiscsi_pci_driver);
  3987. iscsi_unregister_transport(&beiscsi_iscsi_transport);
  3988. }
  3989. module_init(beiscsi_module_init);
  3990. module_exit(beiscsi_module_exit);