csio_init.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  1. /*
  2. * This file is part of the Chelsio FCoE driver for Linux.
  3. *
  4. * Copyright (c) 2008-2012 Chelsio Communications, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  35. #include <linux/kernel.h>
  36. #include <linux/module.h>
  37. #include <linux/init.h>
  38. #include <linux/pci.h>
  39. #include <linux/mm.h>
  40. #include <linux/notifier.h>
  41. #include <linux/kdebug.h>
  42. #include <linux/seq_file.h>
  43. #include <linux/debugfs.h>
  44. #include <linux/string.h>
  45. #include <linux/export.h>
  46. #include "csio_init.h"
  47. #include "csio_defs.h"
  48. #define CSIO_MIN_MEMPOOL_SZ 64
  49. static struct dentry *csio_debugfs_root;
  50. static struct scsi_transport_template *csio_fcoe_transport;
  51. static struct scsi_transport_template *csio_fcoe_transport_vport;
  52. /*
  53. * debugfs support
  54. */
  55. static ssize_t
  56. csio_mem_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  57. {
  58. loff_t pos = *ppos;
  59. loff_t avail = file_inode(file)->i_size;
  60. unsigned int mem = (uintptr_t)file->private_data & 3;
  61. struct csio_hw *hw = file->private_data - mem;
  62. if (pos < 0)
  63. return -EINVAL;
  64. if (pos >= avail)
  65. return 0;
  66. if (count > avail - pos)
  67. count = avail - pos;
  68. while (count) {
  69. size_t len;
  70. int ret, ofst;
  71. __be32 data[16];
  72. if (mem == MEM_MC)
  73. ret = hw->chip_ops->chip_mc_read(hw, 0, pos,
  74. data, NULL);
  75. else
  76. ret = hw->chip_ops->chip_edc_read(hw, mem, pos,
  77. data, NULL);
  78. if (ret)
  79. return ret;
  80. ofst = pos % sizeof(data);
  81. len = min(count, sizeof(data) - ofst);
  82. if (copy_to_user(buf, (u8 *)data + ofst, len))
  83. return -EFAULT;
  84. buf += len;
  85. pos += len;
  86. count -= len;
  87. }
  88. count = pos - *ppos;
  89. *ppos = pos;
  90. return count;
  91. }
  92. static const struct file_operations csio_mem_debugfs_fops = {
  93. .owner = THIS_MODULE,
  94. .open = simple_open,
  95. .read = csio_mem_read,
  96. .llseek = default_llseek,
  97. };
  98. void csio_add_debugfs_mem(struct csio_hw *hw, const char *name,
  99. unsigned int idx, unsigned int size_mb)
  100. {
  101. debugfs_create_file_size(name, S_IRUSR, hw->debugfs_root,
  102. (void *)hw + idx, &csio_mem_debugfs_fops,
  103. size_mb << 20);
  104. }
  105. static int csio_setup_debugfs(struct csio_hw *hw)
  106. {
  107. int i;
  108. if (IS_ERR_OR_NULL(hw->debugfs_root))
  109. return -1;
  110. i = csio_rd_reg32(hw, MA_TARGET_MEM_ENABLE_A);
  111. if (i & EDRAM0_ENABLE_F)
  112. csio_add_debugfs_mem(hw, "edc0", MEM_EDC0, 5);
  113. if (i & EDRAM1_ENABLE_F)
  114. csio_add_debugfs_mem(hw, "edc1", MEM_EDC1, 5);
  115. hw->chip_ops->chip_dfs_create_ext_mem(hw);
  116. return 0;
  117. }
  118. /*
  119. * csio_dfs_create - Creates and sets up per-hw debugfs.
  120. *
  121. */
  122. static int
  123. csio_dfs_create(struct csio_hw *hw)
  124. {
  125. if (csio_debugfs_root) {
  126. hw->debugfs_root = debugfs_create_dir(pci_name(hw->pdev),
  127. csio_debugfs_root);
  128. csio_setup_debugfs(hw);
  129. }
  130. return 0;
  131. }
  132. /*
  133. * csio_dfs_destroy - Destroys per-hw debugfs.
  134. */
  135. static void
  136. csio_dfs_destroy(struct csio_hw *hw)
  137. {
  138. debugfs_remove_recursive(hw->debugfs_root);
  139. }
  140. /*
  141. * csio_dfs_init - Debug filesystem initialization for the module.
  142. *
  143. */
  144. static void
  145. csio_dfs_init(void)
  146. {
  147. csio_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
  148. }
  149. /*
  150. * csio_dfs_exit - debugfs cleanup for the module.
  151. */
  152. static void
  153. csio_dfs_exit(void)
  154. {
  155. debugfs_remove(csio_debugfs_root);
  156. }
  157. /*
  158. * csio_pci_init - PCI initialization.
  159. * @pdev: PCI device.
  160. * @bars: Bitmask of bars to be requested.
  161. *
  162. * Initializes the PCI function by enabling MMIO, setting bus
  163. * mastership and setting DMA mask.
  164. */
  165. static int
  166. csio_pci_init(struct pci_dev *pdev, int *bars)
  167. {
  168. int rv = -ENODEV;
  169. *bars = pci_select_bars(pdev, IORESOURCE_MEM);
  170. if (pci_enable_device_mem(pdev))
  171. goto err;
  172. if (pci_request_selected_regions(pdev, *bars, KBUILD_MODNAME))
  173. goto err_disable_device;
  174. pci_set_master(pdev);
  175. pci_try_set_mwi(pdev);
  176. rv = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  177. if (rv)
  178. rv = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  179. if (rv) {
  180. rv = -ENODEV;
  181. dev_err(&pdev->dev, "No suitable DMA available.\n");
  182. goto err_release_regions;
  183. }
  184. return 0;
  185. err_release_regions:
  186. pci_release_selected_regions(pdev, *bars);
  187. err_disable_device:
  188. pci_disable_device(pdev);
  189. err:
  190. return rv;
  191. }
  192. /*
  193. * csio_pci_exit - PCI unitialization.
  194. * @pdev: PCI device.
  195. * @bars: Bars to be released.
  196. *
  197. */
  198. static void
  199. csio_pci_exit(struct pci_dev *pdev, int *bars)
  200. {
  201. pci_release_selected_regions(pdev, *bars);
  202. pci_disable_device(pdev);
  203. }
  204. /*
  205. * csio_hw_init_workers - Initialize the HW module's worker threads.
  206. * @hw: HW module.
  207. *
  208. */
  209. static void
  210. csio_hw_init_workers(struct csio_hw *hw)
  211. {
  212. INIT_WORK(&hw->evtq_work, csio_evtq_worker);
  213. }
  214. static void
  215. csio_hw_exit_workers(struct csio_hw *hw)
  216. {
  217. cancel_work_sync(&hw->evtq_work);
  218. }
  219. static int
  220. csio_create_queues(struct csio_hw *hw)
  221. {
  222. int i, j;
  223. struct csio_mgmtm *mgmtm = csio_hw_to_mgmtm(hw);
  224. int rv;
  225. struct csio_scsi_cpu_info *info;
  226. if (hw->flags & CSIO_HWF_Q_FW_ALLOCED)
  227. return 0;
  228. if (hw->intr_mode != CSIO_IM_MSIX) {
  229. rv = csio_wr_iq_create(hw, NULL, hw->intr_iq_idx,
  230. 0, hw->pport[0].portid, false, NULL);
  231. if (rv != 0) {
  232. csio_err(hw, " Forward Interrupt IQ failed!: %d\n", rv);
  233. return rv;
  234. }
  235. }
  236. /* FW event queue */
  237. rv = csio_wr_iq_create(hw, NULL, hw->fwevt_iq_idx,
  238. csio_get_fwevt_intr_idx(hw),
  239. hw->pport[0].portid, true, NULL);
  240. if (rv != 0) {
  241. csio_err(hw, "FW event IQ config failed!: %d\n", rv);
  242. return rv;
  243. }
  244. /* Create mgmt queue */
  245. rv = csio_wr_eq_create(hw, NULL, mgmtm->eq_idx,
  246. mgmtm->iq_idx, hw->pport[0].portid, NULL);
  247. if (rv != 0) {
  248. csio_err(hw, "Mgmt EQ create failed!: %d\n", rv);
  249. goto err;
  250. }
  251. /* Create SCSI queues */
  252. for (i = 0; i < hw->num_pports; i++) {
  253. info = &hw->scsi_cpu_info[i];
  254. for (j = 0; j < info->max_cpus; j++) {
  255. struct csio_scsi_qset *sqset = &hw->sqset[i][j];
  256. rv = csio_wr_iq_create(hw, NULL, sqset->iq_idx,
  257. sqset->intr_idx, i, false, NULL);
  258. if (rv != 0) {
  259. csio_err(hw,
  260. "SCSI module IQ config failed [%d][%d]:%d\n",
  261. i, j, rv);
  262. goto err;
  263. }
  264. rv = csio_wr_eq_create(hw, NULL, sqset->eq_idx,
  265. sqset->iq_idx, i, NULL);
  266. if (rv != 0) {
  267. csio_err(hw,
  268. "SCSI module EQ config failed [%d][%d]:%d\n",
  269. i, j, rv);
  270. goto err;
  271. }
  272. } /* for all CPUs */
  273. } /* For all ports */
  274. hw->flags |= CSIO_HWF_Q_FW_ALLOCED;
  275. return 0;
  276. err:
  277. csio_wr_destroy_queues(hw, true);
  278. return -EINVAL;
  279. }
  280. /*
  281. * csio_config_queues - Configure the DMA queues.
  282. * @hw: HW module.
  283. *
  284. * Allocates memory for queues are registers them with FW.
  285. */
  286. int
  287. csio_config_queues(struct csio_hw *hw)
  288. {
  289. int i, j, idx, k = 0;
  290. int rv;
  291. struct csio_scsi_qset *sqset;
  292. struct csio_mgmtm *mgmtm = csio_hw_to_mgmtm(hw);
  293. struct csio_scsi_qset *orig;
  294. struct csio_scsi_cpu_info *info;
  295. if (hw->flags & CSIO_HWF_Q_MEM_ALLOCED)
  296. return csio_create_queues(hw);
  297. /* Calculate number of SCSI queues for MSIX we would like */
  298. hw->num_scsi_msix_cpus = num_online_cpus();
  299. hw->num_sqsets = num_online_cpus() * hw->num_pports;
  300. if (hw->num_sqsets > CSIO_MAX_SCSI_QSETS) {
  301. hw->num_sqsets = CSIO_MAX_SCSI_QSETS;
  302. hw->num_scsi_msix_cpus = CSIO_MAX_SCSI_CPU;
  303. }
  304. /* Initialize max_cpus, may get reduced during msix allocations */
  305. for (i = 0; i < hw->num_pports; i++)
  306. hw->scsi_cpu_info[i].max_cpus = hw->num_scsi_msix_cpus;
  307. csio_dbg(hw, "nsqsets:%d scpus:%d\n",
  308. hw->num_sqsets, hw->num_scsi_msix_cpus);
  309. csio_intr_enable(hw);
  310. if (hw->intr_mode != CSIO_IM_MSIX) {
  311. /* Allocate Forward interrupt iq. */
  312. hw->intr_iq_idx = csio_wr_alloc_q(hw, CSIO_INTR_IQSIZE,
  313. CSIO_INTR_WRSIZE, CSIO_INGRESS,
  314. (void *)hw, 0, 0, NULL);
  315. if (hw->intr_iq_idx == -1) {
  316. csio_err(hw,
  317. "Forward interrupt queue creation failed\n");
  318. goto intr_disable;
  319. }
  320. }
  321. /* Allocate the FW evt queue */
  322. hw->fwevt_iq_idx = csio_wr_alloc_q(hw, CSIO_FWEVT_IQSIZE,
  323. CSIO_FWEVT_WRSIZE,
  324. CSIO_INGRESS, (void *)hw,
  325. CSIO_FWEVT_FLBUFS, 0,
  326. csio_fwevt_intx_handler);
  327. if (hw->fwevt_iq_idx == -1) {
  328. csio_err(hw, "FW evt queue creation failed\n");
  329. goto intr_disable;
  330. }
  331. /* Allocate the mgmt queue */
  332. mgmtm->eq_idx = csio_wr_alloc_q(hw, CSIO_MGMT_EQSIZE,
  333. CSIO_MGMT_EQ_WRSIZE,
  334. CSIO_EGRESS, (void *)hw, 0, 0, NULL);
  335. if (mgmtm->eq_idx == -1) {
  336. csio_err(hw, "Failed to alloc egress queue for mgmt module\n");
  337. goto intr_disable;
  338. }
  339. /* Use FW IQ for MGMT req completion */
  340. mgmtm->iq_idx = hw->fwevt_iq_idx;
  341. /* Allocate SCSI queues */
  342. for (i = 0; i < hw->num_pports; i++) {
  343. info = &hw->scsi_cpu_info[i];
  344. for (j = 0; j < hw->num_scsi_msix_cpus; j++) {
  345. sqset = &hw->sqset[i][j];
  346. if (j >= info->max_cpus) {
  347. k = j % info->max_cpus;
  348. orig = &hw->sqset[i][k];
  349. sqset->eq_idx = orig->eq_idx;
  350. sqset->iq_idx = orig->iq_idx;
  351. continue;
  352. }
  353. idx = csio_wr_alloc_q(hw, csio_scsi_eqsize, 0,
  354. CSIO_EGRESS, (void *)hw, 0, 0,
  355. NULL);
  356. if (idx == -1) {
  357. csio_err(hw, "EQ creation failed for idx:%d\n",
  358. idx);
  359. goto intr_disable;
  360. }
  361. sqset->eq_idx = idx;
  362. idx = csio_wr_alloc_q(hw, CSIO_SCSI_IQSIZE,
  363. CSIO_SCSI_IQ_WRSZ, CSIO_INGRESS,
  364. (void *)hw, 0, 0,
  365. csio_scsi_intx_handler);
  366. if (idx == -1) {
  367. csio_err(hw, "IQ creation failed for idx:%d\n",
  368. idx);
  369. goto intr_disable;
  370. }
  371. sqset->iq_idx = idx;
  372. } /* for all CPUs */
  373. } /* For all ports */
  374. hw->flags |= CSIO_HWF_Q_MEM_ALLOCED;
  375. rv = csio_create_queues(hw);
  376. if (rv != 0)
  377. goto intr_disable;
  378. /*
  379. * Now request IRQs for the vectors. In the event of a failure,
  380. * cleanup is handled internally by this function.
  381. */
  382. rv = csio_request_irqs(hw);
  383. if (rv != 0)
  384. return -EINVAL;
  385. return 0;
  386. intr_disable:
  387. csio_intr_disable(hw, false);
  388. return -EINVAL;
  389. }
  390. static int
  391. csio_resource_alloc(struct csio_hw *hw)
  392. {
  393. struct csio_wrm *wrm = csio_hw_to_wrm(hw);
  394. int rv = -ENOMEM;
  395. wrm->num_q = ((CSIO_MAX_SCSI_QSETS * 2) + CSIO_HW_NIQ +
  396. CSIO_HW_NEQ + CSIO_HW_NFLQ + CSIO_HW_NINTXQ);
  397. hw->mb_mempool = mempool_create_kmalloc_pool(CSIO_MIN_MEMPOOL_SZ,
  398. sizeof(struct csio_mb));
  399. if (!hw->mb_mempool)
  400. goto err;
  401. hw->rnode_mempool = mempool_create_kmalloc_pool(CSIO_MIN_MEMPOOL_SZ,
  402. sizeof(struct csio_rnode));
  403. if (!hw->rnode_mempool)
  404. goto err_free_mb_mempool;
  405. hw->scsi_dma_pool = dma_pool_create("csio_scsi_dma_pool",
  406. &hw->pdev->dev, CSIO_SCSI_RSP_LEN,
  407. 8, 0);
  408. if (!hw->scsi_dma_pool)
  409. goto err_free_rn_pool;
  410. return 0;
  411. err_free_rn_pool:
  412. mempool_destroy(hw->rnode_mempool);
  413. hw->rnode_mempool = NULL;
  414. err_free_mb_mempool:
  415. mempool_destroy(hw->mb_mempool);
  416. hw->mb_mempool = NULL;
  417. err:
  418. return rv;
  419. }
  420. static void
  421. csio_resource_free(struct csio_hw *hw)
  422. {
  423. dma_pool_destroy(hw->scsi_dma_pool);
  424. hw->scsi_dma_pool = NULL;
  425. mempool_destroy(hw->rnode_mempool);
  426. hw->rnode_mempool = NULL;
  427. mempool_destroy(hw->mb_mempool);
  428. hw->mb_mempool = NULL;
  429. }
  430. /*
  431. * csio_hw_alloc - Allocate and initialize the HW module.
  432. * @pdev: PCI device.
  433. *
  434. * Allocates HW structure, DMA, memory resources, maps BARS to
  435. * host memory and initializes HW module.
  436. */
  437. static struct csio_hw *csio_hw_alloc(struct pci_dev *pdev)
  438. {
  439. struct csio_hw *hw;
  440. hw = kzalloc(sizeof(struct csio_hw), GFP_KERNEL);
  441. if (!hw)
  442. goto err;
  443. hw->pdev = pdev;
  444. strscpy(hw->drv_version, CSIO_DRV_VERSION,
  445. sizeof(hw->drv_version));
  446. /* memory pool/DMA pool allocation */
  447. if (csio_resource_alloc(hw))
  448. goto err_free_hw;
  449. /* Get the start address of registers from BAR 0 */
  450. hw->regstart = ioremap(pci_resource_start(pdev, 0),
  451. pci_resource_len(pdev, 0));
  452. if (!hw->regstart) {
  453. csio_err(hw, "Could not map BAR 0, regstart = %p\n",
  454. hw->regstart);
  455. goto err_resource_free;
  456. }
  457. csio_hw_init_workers(hw);
  458. if (csio_hw_init(hw))
  459. goto err_unmap_bar;
  460. csio_dfs_create(hw);
  461. csio_dbg(hw, "hw:%p\n", hw);
  462. return hw;
  463. err_unmap_bar:
  464. csio_hw_exit_workers(hw);
  465. iounmap(hw->regstart);
  466. err_resource_free:
  467. csio_resource_free(hw);
  468. err_free_hw:
  469. kfree(hw);
  470. err:
  471. return NULL;
  472. }
  473. /*
  474. * csio_hw_free - Uninitialize and free the HW module.
  475. * @hw: The HW module
  476. *
  477. * Disable interrupts, uninit the HW module, free resources, free hw.
  478. */
  479. static void
  480. csio_hw_free(struct csio_hw *hw)
  481. {
  482. csio_intr_disable(hw, true);
  483. csio_hw_exit_workers(hw);
  484. csio_hw_exit(hw);
  485. iounmap(hw->regstart);
  486. csio_dfs_destroy(hw);
  487. csio_resource_free(hw);
  488. kfree(hw);
  489. }
  490. /**
  491. * csio_shost_init - Create and initialize the lnode module.
  492. * @hw: The HW module.
  493. * @dev: The device associated with this invocation.
  494. * @probe: Called from probe context or not?
  495. * @pln: Parent lnode if any.
  496. *
  497. * Allocates lnode structure via scsi_host_alloc, initializes
  498. * shost, initializes lnode module and registers with SCSI ML
  499. * via scsi_host_add. This function is shared between physical and
  500. * virtual node ports.
  501. */
  502. struct csio_lnode *
  503. csio_shost_init(struct csio_hw *hw, struct device *dev,
  504. bool probe, struct csio_lnode *pln)
  505. {
  506. struct Scsi_Host *shost = NULL;
  507. struct csio_lnode *ln;
  508. csio_fcoe_shost_template.cmd_per_lun = csio_lun_qdepth;
  509. csio_fcoe_shost_vport_template.cmd_per_lun = csio_lun_qdepth;
  510. /*
  511. * hw->pdev is the physical port's PCI dev structure,
  512. * which will be different from the NPIV dev structure.
  513. */
  514. if (dev == &hw->pdev->dev)
  515. shost = scsi_host_alloc(
  516. &csio_fcoe_shost_template,
  517. sizeof(struct csio_lnode));
  518. else
  519. shost = scsi_host_alloc(
  520. &csio_fcoe_shost_vport_template,
  521. sizeof(struct csio_lnode));
  522. if (!shost)
  523. goto err;
  524. ln = shost_priv(shost);
  525. memset(ln, 0, sizeof(struct csio_lnode));
  526. /* Link common lnode to this lnode */
  527. ln->dev_num = (shost->host_no << 16);
  528. shost->can_queue = CSIO_MAX_QUEUE;
  529. shost->this_id = -1;
  530. shost->unique_id = shost->host_no;
  531. shost->max_cmd_len = 16; /* Max CDB length supported */
  532. shost->max_id = min_t(uint32_t, csio_fcoe_rnodes,
  533. hw->fres_info.max_ssns);
  534. shost->max_lun = CSIO_MAX_LUN;
  535. if (dev == &hw->pdev->dev)
  536. shost->transportt = csio_fcoe_transport;
  537. else
  538. shost->transportt = csio_fcoe_transport_vport;
  539. /* root lnode */
  540. if (!hw->rln)
  541. hw->rln = ln;
  542. /* Other initialization here: Common, Transport specific */
  543. if (csio_lnode_init(ln, hw, pln))
  544. goto err_shost_put;
  545. if (scsi_add_host_with_dma(shost, dev, &hw->pdev->dev))
  546. goto err_lnode_exit;
  547. return ln;
  548. err_lnode_exit:
  549. csio_lnode_exit(ln);
  550. err_shost_put:
  551. scsi_host_put(shost);
  552. err:
  553. return NULL;
  554. }
  555. /**
  556. * csio_shost_exit - De-instantiate the shost.
  557. * @ln: The lnode module corresponding to the shost.
  558. *
  559. */
  560. void
  561. csio_shost_exit(struct csio_lnode *ln)
  562. {
  563. struct Scsi_Host *shost = csio_ln_to_shost(ln);
  564. struct csio_hw *hw = csio_lnode_to_hw(ln);
  565. /* Inform transport */
  566. fc_remove_host(shost);
  567. /* Inform SCSI ML */
  568. scsi_remove_host(shost);
  569. /* Flush all the events, so that any rnode removal events
  570. * already queued are all handled, before we remove the lnode.
  571. */
  572. spin_lock_irq(&hw->lock);
  573. csio_evtq_flush(hw);
  574. spin_unlock_irq(&hw->lock);
  575. csio_lnode_exit(ln);
  576. scsi_host_put(shost);
  577. }
  578. struct csio_lnode *
  579. csio_lnode_alloc(struct csio_hw *hw)
  580. {
  581. return csio_shost_init(hw, &hw->pdev->dev, false, NULL);
  582. }
  583. void
  584. csio_lnodes_block_request(struct csio_hw *hw)
  585. {
  586. struct Scsi_Host *shost;
  587. struct csio_lnode *sln;
  588. struct csio_lnode *ln;
  589. struct list_head *cur_ln, *cur_cln;
  590. struct csio_lnode **lnode_list;
  591. int cur_cnt = 0, ii;
  592. lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
  593. GFP_KERNEL);
  594. if (!lnode_list) {
  595. csio_err(hw, "Failed to allocate lnodes_list");
  596. return;
  597. }
  598. spin_lock_irq(&hw->lock);
  599. /* Traverse sibling lnodes */
  600. list_for_each(cur_ln, &hw->sln_head) {
  601. sln = (struct csio_lnode *) cur_ln;
  602. lnode_list[cur_cnt++] = sln;
  603. /* Traverse children lnodes */
  604. list_for_each(cur_cln, &sln->cln_head)
  605. lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
  606. }
  607. spin_unlock_irq(&hw->lock);
  608. for (ii = 0; ii < cur_cnt; ii++) {
  609. csio_dbg(hw, "Blocking IOs on lnode: %p\n", lnode_list[ii]);
  610. ln = lnode_list[ii];
  611. shost = csio_ln_to_shost(ln);
  612. scsi_block_requests(shost);
  613. }
  614. kfree(lnode_list);
  615. }
  616. void
  617. csio_lnodes_unblock_request(struct csio_hw *hw)
  618. {
  619. struct csio_lnode *ln;
  620. struct Scsi_Host *shost;
  621. struct csio_lnode *sln;
  622. struct list_head *cur_ln, *cur_cln;
  623. struct csio_lnode **lnode_list;
  624. int cur_cnt = 0, ii;
  625. lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
  626. GFP_KERNEL);
  627. if (!lnode_list) {
  628. csio_err(hw, "Failed to allocate lnodes_list");
  629. return;
  630. }
  631. spin_lock_irq(&hw->lock);
  632. /* Traverse sibling lnodes */
  633. list_for_each(cur_ln, &hw->sln_head) {
  634. sln = (struct csio_lnode *) cur_ln;
  635. lnode_list[cur_cnt++] = sln;
  636. /* Traverse children lnodes */
  637. list_for_each(cur_cln, &sln->cln_head)
  638. lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
  639. }
  640. spin_unlock_irq(&hw->lock);
  641. for (ii = 0; ii < cur_cnt; ii++) {
  642. csio_dbg(hw, "unblocking IOs on lnode: %p\n", lnode_list[ii]);
  643. ln = lnode_list[ii];
  644. shost = csio_ln_to_shost(ln);
  645. scsi_unblock_requests(shost);
  646. }
  647. kfree(lnode_list);
  648. }
  649. void
  650. csio_lnodes_block_by_port(struct csio_hw *hw, uint8_t portid)
  651. {
  652. struct csio_lnode *ln;
  653. struct Scsi_Host *shost;
  654. struct csio_lnode *sln;
  655. struct list_head *cur_ln, *cur_cln;
  656. struct csio_lnode **lnode_list;
  657. int cur_cnt = 0, ii;
  658. lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
  659. GFP_KERNEL);
  660. if (!lnode_list) {
  661. csio_err(hw, "Failed to allocate lnodes_list");
  662. return;
  663. }
  664. spin_lock_irq(&hw->lock);
  665. /* Traverse sibling lnodes */
  666. list_for_each(cur_ln, &hw->sln_head) {
  667. sln = (struct csio_lnode *) cur_ln;
  668. if (sln->portid != portid)
  669. continue;
  670. lnode_list[cur_cnt++] = sln;
  671. /* Traverse children lnodes */
  672. list_for_each(cur_cln, &sln->cln_head)
  673. lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
  674. }
  675. spin_unlock_irq(&hw->lock);
  676. for (ii = 0; ii < cur_cnt; ii++) {
  677. csio_dbg(hw, "Blocking IOs on lnode: %p\n", lnode_list[ii]);
  678. ln = lnode_list[ii];
  679. shost = csio_ln_to_shost(ln);
  680. scsi_block_requests(shost);
  681. }
  682. kfree(lnode_list);
  683. }
  684. void
  685. csio_lnodes_unblock_by_port(struct csio_hw *hw, uint8_t portid)
  686. {
  687. struct csio_lnode *ln;
  688. struct Scsi_Host *shost;
  689. struct csio_lnode *sln;
  690. struct list_head *cur_ln, *cur_cln;
  691. struct csio_lnode **lnode_list;
  692. int cur_cnt = 0, ii;
  693. lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
  694. GFP_KERNEL);
  695. if (!lnode_list) {
  696. csio_err(hw, "Failed to allocate lnodes_list");
  697. return;
  698. }
  699. spin_lock_irq(&hw->lock);
  700. /* Traverse sibling lnodes */
  701. list_for_each(cur_ln, &hw->sln_head) {
  702. sln = (struct csio_lnode *) cur_ln;
  703. if (sln->portid != portid)
  704. continue;
  705. lnode_list[cur_cnt++] = sln;
  706. /* Traverse children lnodes */
  707. list_for_each(cur_cln, &sln->cln_head)
  708. lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
  709. }
  710. spin_unlock_irq(&hw->lock);
  711. for (ii = 0; ii < cur_cnt; ii++) {
  712. csio_dbg(hw, "unblocking IOs on lnode: %p\n", lnode_list[ii]);
  713. ln = lnode_list[ii];
  714. shost = csio_ln_to_shost(ln);
  715. scsi_unblock_requests(shost);
  716. }
  717. kfree(lnode_list);
  718. }
  719. void
  720. csio_lnodes_exit(struct csio_hw *hw, bool npiv)
  721. {
  722. struct csio_lnode *sln;
  723. struct csio_lnode *ln;
  724. struct list_head *cur_ln, *cur_cln;
  725. struct csio_lnode **lnode_list;
  726. int cur_cnt = 0, ii;
  727. lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
  728. GFP_KERNEL);
  729. if (!lnode_list) {
  730. csio_err(hw, "lnodes_exit: Failed to allocate lnodes_list.\n");
  731. return;
  732. }
  733. /* Get all child lnodes(NPIV ports) */
  734. spin_lock_irq(&hw->lock);
  735. list_for_each(cur_ln, &hw->sln_head) {
  736. sln = (struct csio_lnode *) cur_ln;
  737. /* Traverse children lnodes */
  738. list_for_each(cur_cln, &sln->cln_head)
  739. lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
  740. }
  741. spin_unlock_irq(&hw->lock);
  742. /* Delete NPIV lnodes */
  743. for (ii = 0; ii < cur_cnt; ii++) {
  744. csio_dbg(hw, "Deleting child lnode: %p\n", lnode_list[ii]);
  745. ln = lnode_list[ii];
  746. fc_vport_terminate(ln->fc_vport);
  747. }
  748. /* Delete only npiv lnodes */
  749. if (npiv)
  750. goto free_lnodes;
  751. cur_cnt = 0;
  752. /* Get all physical lnodes */
  753. spin_lock_irq(&hw->lock);
  754. /* Traverse sibling lnodes */
  755. list_for_each(cur_ln, &hw->sln_head) {
  756. sln = (struct csio_lnode *) cur_ln;
  757. lnode_list[cur_cnt++] = sln;
  758. }
  759. spin_unlock_irq(&hw->lock);
  760. /* Delete physical lnodes */
  761. for (ii = 0; ii < cur_cnt; ii++) {
  762. csio_dbg(hw, "Deleting parent lnode: %p\n", lnode_list[ii]);
  763. csio_shost_exit(lnode_list[ii]);
  764. }
  765. free_lnodes:
  766. kfree(lnode_list);
  767. }
  768. /*
  769. * csio_lnode_init_post: Set lnode attributes after starting HW.
  770. * @ln: lnode.
  771. *
  772. */
  773. static void
  774. csio_lnode_init_post(struct csio_lnode *ln)
  775. {
  776. struct Scsi_Host *shost = csio_ln_to_shost(ln);
  777. csio_fchost_attr_init(ln);
  778. scsi_scan_host(shost);
  779. }
  780. /*
  781. * csio_probe_one - Instantiate this function.
  782. * @pdev: PCI device
  783. * @id: Device ID
  784. *
  785. * This is the .probe() callback of the driver. This function:
  786. * - Initializes the PCI function by enabling MMIO, setting bus
  787. * mastership and setting DMA mask.
  788. * - Allocates HW structure, DMA, memory resources, maps BARS to
  789. * host memory and initializes HW module.
  790. * - Allocates lnode structure via scsi_host_alloc, initializes
  791. * shost, initialized lnode module and registers with SCSI ML
  792. * via scsi_host_add.
  793. * - Enables interrupts, and starts the chip by kicking off the
  794. * HW state machine.
  795. * - Once hardware is ready, initiated scan of the host via
  796. * scsi_scan_host.
  797. */
  798. static int csio_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  799. {
  800. int rv;
  801. int bars;
  802. int i;
  803. struct csio_hw *hw;
  804. struct csio_lnode *ln;
  805. /* probe only T5 and T6 cards */
  806. if (!csio_is_t5((pdev->device & CSIO_HW_CHIP_MASK)) &&
  807. !csio_is_t6((pdev->device & CSIO_HW_CHIP_MASK)))
  808. return -ENODEV;
  809. rv = csio_pci_init(pdev, &bars);
  810. if (rv)
  811. goto err;
  812. hw = csio_hw_alloc(pdev);
  813. if (!hw) {
  814. rv = -ENODEV;
  815. goto err_pci_exit;
  816. }
  817. if (!pcie_relaxed_ordering_enabled(pdev))
  818. hw->flags |= CSIO_HWF_ROOT_NO_RELAXED_ORDERING;
  819. pci_set_drvdata(pdev, hw);
  820. rv = csio_hw_start(hw);
  821. if (rv) {
  822. if (rv == -EINVAL) {
  823. dev_err(&pdev->dev,
  824. "Failed to start FW, continuing in debug mode.\n");
  825. return 0;
  826. }
  827. goto err_lnode_exit;
  828. }
  829. sprintf(hw->fwrev_str, "%u.%u.%u.%u\n",
  830. FW_HDR_FW_VER_MAJOR_G(hw->fwrev),
  831. FW_HDR_FW_VER_MINOR_G(hw->fwrev),
  832. FW_HDR_FW_VER_MICRO_G(hw->fwrev),
  833. FW_HDR_FW_VER_BUILD_G(hw->fwrev));
  834. for (i = 0; i < hw->num_pports; i++) {
  835. ln = csio_shost_init(hw, &pdev->dev, true, NULL);
  836. if (!ln) {
  837. rv = -ENODEV;
  838. break;
  839. }
  840. /* Initialize portid */
  841. ln->portid = hw->pport[i].portid;
  842. spin_lock_irq(&hw->lock);
  843. if (csio_lnode_start(ln) != 0)
  844. rv = -ENODEV;
  845. spin_unlock_irq(&hw->lock);
  846. if (rv)
  847. break;
  848. csio_lnode_init_post(ln);
  849. }
  850. if (rv)
  851. goto err_lnode_exit;
  852. return 0;
  853. err_lnode_exit:
  854. csio_lnodes_block_request(hw);
  855. spin_lock_irq(&hw->lock);
  856. csio_hw_stop(hw);
  857. spin_unlock_irq(&hw->lock);
  858. csio_lnodes_unblock_request(hw);
  859. csio_lnodes_exit(hw, 0);
  860. csio_hw_free(hw);
  861. err_pci_exit:
  862. csio_pci_exit(pdev, &bars);
  863. err:
  864. dev_err(&pdev->dev, "probe of device failed: %d\n", rv);
  865. return rv;
  866. }
  867. /*
  868. * csio_remove_one - Remove one instance of the driver at this PCI function.
  869. * @pdev: PCI device
  870. *
  871. * Used during hotplug operation.
  872. */
  873. static void csio_remove_one(struct pci_dev *pdev)
  874. {
  875. struct csio_hw *hw = pci_get_drvdata(pdev);
  876. int bars = pci_select_bars(pdev, IORESOURCE_MEM);
  877. csio_lnodes_block_request(hw);
  878. spin_lock_irq(&hw->lock);
  879. /* Stops lnode, Rnode s/m
  880. * Quiesce IOs.
  881. * All sessions with remote ports are unregistered.
  882. */
  883. csio_hw_stop(hw);
  884. spin_unlock_irq(&hw->lock);
  885. csio_lnodes_unblock_request(hw);
  886. csio_lnodes_exit(hw, 0);
  887. csio_hw_free(hw);
  888. csio_pci_exit(pdev, &bars);
  889. }
  890. /*
  891. * csio_pci_error_detected - PCI error was detected
  892. * @pdev: PCI device
  893. *
  894. */
  895. static pci_ers_result_t
  896. csio_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  897. {
  898. struct csio_hw *hw = pci_get_drvdata(pdev);
  899. csio_lnodes_block_request(hw);
  900. spin_lock_irq(&hw->lock);
  901. /* Post PCI error detected evt to HW s/m
  902. * HW s/m handles this evt by quiescing IOs, unregisters rports
  903. * and finally takes the device to offline.
  904. */
  905. csio_post_event(&hw->sm, CSIO_HWE_PCIERR_DETECTED);
  906. spin_unlock_irq(&hw->lock);
  907. csio_lnodes_unblock_request(hw);
  908. csio_lnodes_exit(hw, 0);
  909. csio_intr_disable(hw, true);
  910. pci_disable_device(pdev);
  911. return state == pci_channel_io_perm_failure ?
  912. PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
  913. }
  914. /*
  915. * csio_pci_slot_reset - PCI slot has been reset.
  916. * @pdev: PCI device
  917. *
  918. */
  919. static pci_ers_result_t
  920. csio_pci_slot_reset(struct pci_dev *pdev)
  921. {
  922. struct csio_hw *hw = pci_get_drvdata(pdev);
  923. int ready;
  924. if (pci_enable_device(pdev)) {
  925. dev_err(&pdev->dev, "cannot re-enable device in slot reset\n");
  926. return PCI_ERS_RESULT_DISCONNECT;
  927. }
  928. pci_set_master(pdev);
  929. pci_restore_state(pdev);
  930. pci_save_state(pdev);
  931. /* Bring HW s/m to ready state.
  932. * but don't resume IOs.
  933. */
  934. spin_lock_irq(&hw->lock);
  935. csio_post_event(&hw->sm, CSIO_HWE_PCIERR_SLOT_RESET);
  936. ready = csio_is_hw_ready(hw);
  937. spin_unlock_irq(&hw->lock);
  938. if (ready) {
  939. return PCI_ERS_RESULT_RECOVERED;
  940. } else {
  941. dev_err(&pdev->dev, "Can't initialize HW when in slot reset\n");
  942. return PCI_ERS_RESULT_DISCONNECT;
  943. }
  944. }
  945. /*
  946. * csio_pci_resume - Resume normal operations
  947. * @pdev: PCI device
  948. *
  949. */
  950. static void
  951. csio_pci_resume(struct pci_dev *pdev)
  952. {
  953. struct csio_hw *hw = pci_get_drvdata(pdev);
  954. struct csio_lnode *ln;
  955. int rv = 0;
  956. int i;
  957. /* Bring the LINK UP and Resume IO */
  958. for (i = 0; i < hw->num_pports; i++) {
  959. ln = csio_shost_init(hw, &pdev->dev, true, NULL);
  960. if (!ln) {
  961. rv = -ENODEV;
  962. break;
  963. }
  964. /* Initialize portid */
  965. ln->portid = hw->pport[i].portid;
  966. spin_lock_irq(&hw->lock);
  967. if (csio_lnode_start(ln) != 0)
  968. rv = -ENODEV;
  969. spin_unlock_irq(&hw->lock);
  970. if (rv)
  971. break;
  972. csio_lnode_init_post(ln);
  973. }
  974. if (rv)
  975. goto err_resume_exit;
  976. return;
  977. err_resume_exit:
  978. csio_lnodes_block_request(hw);
  979. spin_lock_irq(&hw->lock);
  980. csio_hw_stop(hw);
  981. spin_unlock_irq(&hw->lock);
  982. csio_lnodes_unblock_request(hw);
  983. csio_lnodes_exit(hw, 0);
  984. csio_hw_free(hw);
  985. dev_err(&pdev->dev, "resume of device failed: %d\n", rv);
  986. }
  987. static struct pci_error_handlers csio_err_handler = {
  988. .error_detected = csio_pci_error_detected,
  989. .slot_reset = csio_pci_slot_reset,
  990. .resume = csio_pci_resume,
  991. };
  992. /*
  993. * Macros needed to support the PCI Device ID Table ...
  994. */
  995. #define CH_PCI_DEVICE_ID_TABLE_DEFINE_BEGIN \
  996. static const struct pci_device_id csio_pci_tbl[] = {
  997. /* Define for FCoE uses PF6 */
  998. #define CH_PCI_DEVICE_ID_FUNCTION 0x6
  999. #define CH_PCI_ID_TABLE_ENTRY(devid) \
  1000. { PCI_VDEVICE(CHELSIO, (devid)), 0 }
  1001. #define CH_PCI_DEVICE_ID_TABLE_DEFINE_END { 0, } }
  1002. #include "t4_pci_id_tbl.h"
  1003. static struct pci_driver csio_pci_driver = {
  1004. .name = KBUILD_MODNAME,
  1005. .id_table = csio_pci_tbl,
  1006. .probe = csio_probe_one,
  1007. .remove = csio_remove_one,
  1008. .err_handler = &csio_err_handler,
  1009. };
  1010. /*
  1011. * csio_init - Chelsio storage driver initialization function.
  1012. *
  1013. */
  1014. static int __init
  1015. csio_init(void)
  1016. {
  1017. int rv = -ENOMEM;
  1018. pr_info("%s %s\n", CSIO_DRV_DESC, CSIO_DRV_VERSION);
  1019. csio_dfs_init();
  1020. csio_fcoe_transport = fc_attach_transport(&csio_fc_transport_funcs);
  1021. if (!csio_fcoe_transport)
  1022. goto err;
  1023. csio_fcoe_transport_vport =
  1024. fc_attach_transport(&csio_fc_transport_vport_funcs);
  1025. if (!csio_fcoe_transport_vport)
  1026. goto err_vport;
  1027. rv = pci_register_driver(&csio_pci_driver);
  1028. if (rv)
  1029. goto err_pci;
  1030. return 0;
  1031. err_pci:
  1032. fc_release_transport(csio_fcoe_transport_vport);
  1033. err_vport:
  1034. fc_release_transport(csio_fcoe_transport);
  1035. err:
  1036. csio_dfs_exit();
  1037. return rv;
  1038. }
  1039. /*
  1040. * csio_exit - Chelsio storage driver uninitialization .
  1041. *
  1042. * Function that gets called in the unload path.
  1043. */
  1044. static void __exit
  1045. csio_exit(void)
  1046. {
  1047. pci_unregister_driver(&csio_pci_driver);
  1048. csio_dfs_exit();
  1049. fc_release_transport(csio_fcoe_transport_vport);
  1050. fc_release_transport(csio_fcoe_transport);
  1051. }
  1052. module_init(csio_init);
  1053. module_exit(csio_exit);
  1054. MODULE_AUTHOR(CSIO_DRV_AUTHOR);
  1055. MODULE_DESCRIPTION(CSIO_DRV_DESC);
  1056. MODULE_LICENSE("Dual BSD/GPL");
  1057. MODULE_DEVICE_TABLE(pci, csio_pci_tbl);
  1058. MODULE_VERSION(CSIO_DRV_VERSION);
  1059. MODULE_FIRMWARE(FW_FNAME_T5);
  1060. MODULE_FIRMWARE(FW_FNAME_T6);
  1061. MODULE_SOFTDEP("pre: cxgb4");