csio_init.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  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/aer.h>
  40. #include <linux/mm.h>
  41. #include <linux/notifier.h>
  42. #include <linux/kdebug.h>
  43. #include <linux/seq_file.h>
  44. #include <linux/debugfs.h>
  45. #include <linux/string.h>
  46. #include <linux/export.h>
  47. #include "csio_init.h"
  48. #include "csio_defs.h"
  49. #define CSIO_MIN_MEMPOOL_SZ 64
  50. static struct dentry *csio_debugfs_root;
  51. static struct scsi_transport_template *csio_fcoe_transport;
  52. static struct scsi_transport_template *csio_fcoe_transport_vport;
  53. /*
  54. * debugfs support
  55. */
  56. static ssize_t
  57. csio_mem_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  58. {
  59. loff_t pos = *ppos;
  60. loff_t avail = file_inode(file)->i_size;
  61. unsigned int mem = (uintptr_t)file->private_data & 3;
  62. struct csio_hw *hw = file->private_data - mem;
  63. if (pos < 0)
  64. return -EINVAL;
  65. if (pos >= avail)
  66. return 0;
  67. if (count > avail - pos)
  68. count = avail - pos;
  69. while (count) {
  70. size_t len;
  71. int ret, ofst;
  72. __be32 data[16];
  73. if (mem == MEM_MC)
  74. ret = hw->chip_ops->chip_mc_read(hw, 0, pos,
  75. data, NULL);
  76. else
  77. ret = hw->chip_ops->chip_edc_read(hw, mem, pos,
  78. data, NULL);
  79. if (ret)
  80. return ret;
  81. ofst = pos % sizeof(data);
  82. len = min(count, sizeof(data) - ofst);
  83. if (copy_to_user(buf, (u8 *)data + ofst, len))
  84. return -EFAULT;
  85. buf += len;
  86. pos += len;
  87. count -= len;
  88. }
  89. count = pos - *ppos;
  90. *ppos = pos;
  91. return count;
  92. }
  93. static const struct file_operations csio_mem_debugfs_fops = {
  94. .owner = THIS_MODULE,
  95. .open = simple_open,
  96. .read = csio_mem_read,
  97. .llseek = default_llseek,
  98. };
  99. void csio_add_debugfs_mem(struct csio_hw *hw, const char *name,
  100. unsigned int idx, unsigned int size_mb)
  101. {
  102. debugfs_create_file_size(name, S_IRUSR, hw->debugfs_root,
  103. (void *)hw + idx, &csio_mem_debugfs_fops,
  104. size_mb << 20);
  105. }
  106. static int csio_setup_debugfs(struct csio_hw *hw)
  107. {
  108. int i;
  109. if (IS_ERR_OR_NULL(hw->debugfs_root))
  110. return -1;
  111. i = csio_rd_reg32(hw, MA_TARGET_MEM_ENABLE_A);
  112. if (i & EDRAM0_ENABLE_F)
  113. csio_add_debugfs_mem(hw, "edc0", MEM_EDC0, 5);
  114. if (i & EDRAM1_ENABLE_F)
  115. csio_add_debugfs_mem(hw, "edc1", MEM_EDC1, 5);
  116. hw->chip_ops->chip_dfs_create_ext_mem(hw);
  117. return 0;
  118. }
  119. /*
  120. * csio_dfs_create - Creates and sets up per-hw debugfs.
  121. *
  122. */
  123. static int
  124. csio_dfs_create(struct csio_hw *hw)
  125. {
  126. if (csio_debugfs_root) {
  127. hw->debugfs_root = debugfs_create_dir(pci_name(hw->pdev),
  128. csio_debugfs_root);
  129. csio_setup_debugfs(hw);
  130. }
  131. return 0;
  132. }
  133. /*
  134. * csio_dfs_destroy - Destroys per-hw debugfs.
  135. */
  136. static void
  137. csio_dfs_destroy(struct csio_hw *hw)
  138. {
  139. debugfs_remove_recursive(hw->debugfs_root);
  140. }
  141. /*
  142. * csio_dfs_init - Debug filesystem initialization for the module.
  143. *
  144. */
  145. static void
  146. csio_dfs_init(void)
  147. {
  148. csio_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
  149. }
  150. /*
  151. * csio_dfs_exit - debugfs cleanup for the module.
  152. */
  153. static void
  154. csio_dfs_exit(void)
  155. {
  156. debugfs_remove(csio_debugfs_root);
  157. }
  158. /*
  159. * csio_pci_init - PCI initialization.
  160. * @pdev: PCI device.
  161. * @bars: Bitmask of bars to be requested.
  162. *
  163. * Initializes the PCI function by enabling MMIO, setting bus
  164. * mastership and setting DMA mask.
  165. */
  166. static int
  167. csio_pci_init(struct pci_dev *pdev, int *bars)
  168. {
  169. int rv = -ENODEV;
  170. *bars = pci_select_bars(pdev, IORESOURCE_MEM);
  171. if (pci_enable_device_mem(pdev))
  172. goto err;
  173. if (pci_request_selected_regions(pdev, *bars, KBUILD_MODNAME))
  174. goto err_disable_device;
  175. pci_set_master(pdev);
  176. pci_try_set_mwi(pdev);
  177. rv = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  178. if (rv)
  179. rv = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  180. if (rv) {
  181. rv = -ENODEV;
  182. dev_err(&pdev->dev, "No suitable DMA available.\n");
  183. goto err_release_regions;
  184. }
  185. return 0;
  186. err_release_regions:
  187. pci_release_selected_regions(pdev, *bars);
  188. err_disable_device:
  189. pci_disable_device(pdev);
  190. err:
  191. return rv;
  192. }
  193. /*
  194. * csio_pci_exit - PCI unitialization.
  195. * @pdev: PCI device.
  196. * @bars: Bars to be released.
  197. *
  198. */
  199. static void
  200. csio_pci_exit(struct pci_dev *pdev, int *bars)
  201. {
  202. pci_release_selected_regions(pdev, *bars);
  203. pci_disable_device(pdev);
  204. }
  205. /*
  206. * csio_hw_init_workers - Initialize the HW module's worker threads.
  207. * @hw: HW module.
  208. *
  209. */
  210. static void
  211. csio_hw_init_workers(struct csio_hw *hw)
  212. {
  213. INIT_WORK(&hw->evtq_work, csio_evtq_worker);
  214. }
  215. static void
  216. csio_hw_exit_workers(struct csio_hw *hw)
  217. {
  218. cancel_work_sync(&hw->evtq_work);
  219. }
  220. static int
  221. csio_create_queues(struct csio_hw *hw)
  222. {
  223. int i, j;
  224. struct csio_mgmtm *mgmtm = csio_hw_to_mgmtm(hw);
  225. int rv;
  226. struct csio_scsi_cpu_info *info;
  227. if (hw->flags & CSIO_HWF_Q_FW_ALLOCED)
  228. return 0;
  229. if (hw->intr_mode != CSIO_IM_MSIX) {
  230. rv = csio_wr_iq_create(hw, NULL, hw->intr_iq_idx,
  231. 0, hw->pport[0].portid, false, NULL);
  232. if (rv != 0) {
  233. csio_err(hw, " Forward Interrupt IQ failed!: %d\n", rv);
  234. return rv;
  235. }
  236. }
  237. /* FW event queue */
  238. rv = csio_wr_iq_create(hw, NULL, hw->fwevt_iq_idx,
  239. csio_get_fwevt_intr_idx(hw),
  240. hw->pport[0].portid, true, NULL);
  241. if (rv != 0) {
  242. csio_err(hw, "FW event IQ config failed!: %d\n", rv);
  243. return rv;
  244. }
  245. /* Create mgmt queue */
  246. rv = csio_wr_eq_create(hw, NULL, mgmtm->eq_idx,
  247. mgmtm->iq_idx, hw->pport[0].portid, NULL);
  248. if (rv != 0) {
  249. csio_err(hw, "Mgmt EQ create failed!: %d\n", rv);
  250. goto err;
  251. }
  252. /* Create SCSI queues */
  253. for (i = 0; i < hw->num_pports; i++) {
  254. info = &hw->scsi_cpu_info[i];
  255. for (j = 0; j < info->max_cpus; j++) {
  256. struct csio_scsi_qset *sqset = &hw->sqset[i][j];
  257. rv = csio_wr_iq_create(hw, NULL, sqset->iq_idx,
  258. sqset->intr_idx, i, false, NULL);
  259. if (rv != 0) {
  260. csio_err(hw,
  261. "SCSI module IQ config failed [%d][%d]:%d\n",
  262. i, j, rv);
  263. goto err;
  264. }
  265. rv = csio_wr_eq_create(hw, NULL, sqset->eq_idx,
  266. sqset->iq_idx, i, NULL);
  267. if (rv != 0) {
  268. csio_err(hw,
  269. "SCSI module EQ config failed [%d][%d]:%d\n",
  270. i, j, rv);
  271. goto err;
  272. }
  273. } /* for all CPUs */
  274. } /* For all ports */
  275. hw->flags |= CSIO_HWF_Q_FW_ALLOCED;
  276. return 0;
  277. err:
  278. csio_wr_destroy_queues(hw, true);
  279. return -EINVAL;
  280. }
  281. /*
  282. * csio_config_queues - Configure the DMA queues.
  283. * @hw: HW module.
  284. *
  285. * Allocates memory for queues are registers them with FW.
  286. */
  287. int
  288. csio_config_queues(struct csio_hw *hw)
  289. {
  290. int i, j, idx, k = 0;
  291. int rv;
  292. struct csio_scsi_qset *sqset;
  293. struct csio_mgmtm *mgmtm = csio_hw_to_mgmtm(hw);
  294. struct csio_scsi_qset *orig;
  295. struct csio_scsi_cpu_info *info;
  296. if (hw->flags & CSIO_HWF_Q_MEM_ALLOCED)
  297. return csio_create_queues(hw);
  298. /* Calculate number of SCSI queues for MSIX we would like */
  299. hw->num_scsi_msix_cpus = num_online_cpus();
  300. hw->num_sqsets = num_online_cpus() * hw->num_pports;
  301. if (hw->num_sqsets > CSIO_MAX_SCSI_QSETS) {
  302. hw->num_sqsets = CSIO_MAX_SCSI_QSETS;
  303. hw->num_scsi_msix_cpus = CSIO_MAX_SCSI_CPU;
  304. }
  305. /* Initialize max_cpus, may get reduced during msix allocations */
  306. for (i = 0; i < hw->num_pports; i++)
  307. hw->scsi_cpu_info[i].max_cpus = hw->num_scsi_msix_cpus;
  308. csio_dbg(hw, "nsqsets:%d scpus:%d\n",
  309. hw->num_sqsets, hw->num_scsi_msix_cpus);
  310. csio_intr_enable(hw);
  311. if (hw->intr_mode != CSIO_IM_MSIX) {
  312. /* Allocate Forward interrupt iq. */
  313. hw->intr_iq_idx = csio_wr_alloc_q(hw, CSIO_INTR_IQSIZE,
  314. CSIO_INTR_WRSIZE, CSIO_INGRESS,
  315. (void *)hw, 0, 0, NULL);
  316. if (hw->intr_iq_idx == -1) {
  317. csio_err(hw,
  318. "Forward interrupt queue creation failed\n");
  319. goto intr_disable;
  320. }
  321. }
  322. /* Allocate the FW evt queue */
  323. hw->fwevt_iq_idx = csio_wr_alloc_q(hw, CSIO_FWEVT_IQSIZE,
  324. CSIO_FWEVT_WRSIZE,
  325. CSIO_INGRESS, (void *)hw,
  326. CSIO_FWEVT_FLBUFS, 0,
  327. csio_fwevt_intx_handler);
  328. if (hw->fwevt_iq_idx == -1) {
  329. csio_err(hw, "FW evt queue creation failed\n");
  330. goto intr_disable;
  331. }
  332. /* Allocate the mgmt queue */
  333. mgmtm->eq_idx = csio_wr_alloc_q(hw, CSIO_MGMT_EQSIZE,
  334. CSIO_MGMT_EQ_WRSIZE,
  335. CSIO_EGRESS, (void *)hw, 0, 0, NULL);
  336. if (mgmtm->eq_idx == -1) {
  337. csio_err(hw, "Failed to alloc egress queue for mgmt module\n");
  338. goto intr_disable;
  339. }
  340. /* Use FW IQ for MGMT req completion */
  341. mgmtm->iq_idx = hw->fwevt_iq_idx;
  342. /* Allocate SCSI queues */
  343. for (i = 0; i < hw->num_pports; i++) {
  344. info = &hw->scsi_cpu_info[i];
  345. for (j = 0; j < hw->num_scsi_msix_cpus; j++) {
  346. sqset = &hw->sqset[i][j];
  347. if (j >= info->max_cpus) {
  348. k = j % info->max_cpus;
  349. orig = &hw->sqset[i][k];
  350. sqset->eq_idx = orig->eq_idx;
  351. sqset->iq_idx = orig->iq_idx;
  352. continue;
  353. }
  354. idx = csio_wr_alloc_q(hw, csio_scsi_eqsize, 0,
  355. CSIO_EGRESS, (void *)hw, 0, 0,
  356. NULL);
  357. if (idx == -1) {
  358. csio_err(hw, "EQ creation failed for idx:%d\n",
  359. idx);
  360. goto intr_disable;
  361. }
  362. sqset->eq_idx = idx;
  363. idx = csio_wr_alloc_q(hw, CSIO_SCSI_IQSIZE,
  364. CSIO_SCSI_IQ_WRSZ, CSIO_INGRESS,
  365. (void *)hw, 0, 0,
  366. csio_scsi_intx_handler);
  367. if (idx == -1) {
  368. csio_err(hw, "IQ creation failed for idx:%d\n",
  369. idx);
  370. goto intr_disable;
  371. }
  372. sqset->iq_idx = idx;
  373. } /* for all CPUs */
  374. } /* For all ports */
  375. hw->flags |= CSIO_HWF_Q_MEM_ALLOCED;
  376. rv = csio_create_queues(hw);
  377. if (rv != 0)
  378. goto intr_disable;
  379. /*
  380. * Now request IRQs for the vectors. In the event of a failure,
  381. * cleanup is handled internally by this function.
  382. */
  383. rv = csio_request_irqs(hw);
  384. if (rv != 0)
  385. return -EINVAL;
  386. return 0;
  387. intr_disable:
  388. csio_intr_disable(hw, false);
  389. return -EINVAL;
  390. }
  391. static int
  392. csio_resource_alloc(struct csio_hw *hw)
  393. {
  394. struct csio_wrm *wrm = csio_hw_to_wrm(hw);
  395. int rv = -ENOMEM;
  396. wrm->num_q = ((CSIO_MAX_SCSI_QSETS * 2) + CSIO_HW_NIQ +
  397. CSIO_HW_NEQ + CSIO_HW_NFLQ + CSIO_HW_NINTXQ);
  398. hw->mb_mempool = mempool_create_kmalloc_pool(CSIO_MIN_MEMPOOL_SZ,
  399. sizeof(struct csio_mb));
  400. if (!hw->mb_mempool)
  401. goto err;
  402. hw->rnode_mempool = mempool_create_kmalloc_pool(CSIO_MIN_MEMPOOL_SZ,
  403. sizeof(struct csio_rnode));
  404. if (!hw->rnode_mempool)
  405. goto err_free_mb_mempool;
  406. hw->scsi_dma_pool = dma_pool_create("csio_scsi_dma_pool",
  407. &hw->pdev->dev, CSIO_SCSI_RSP_LEN,
  408. 8, 0);
  409. if (!hw->scsi_dma_pool)
  410. goto err_free_rn_pool;
  411. return 0;
  412. err_free_rn_pool:
  413. mempool_destroy(hw->rnode_mempool);
  414. hw->rnode_mempool = NULL;
  415. err_free_mb_mempool:
  416. mempool_destroy(hw->mb_mempool);
  417. hw->mb_mempool = NULL;
  418. err:
  419. return rv;
  420. }
  421. static void
  422. csio_resource_free(struct csio_hw *hw)
  423. {
  424. dma_pool_destroy(hw->scsi_dma_pool);
  425. hw->scsi_dma_pool = NULL;
  426. mempool_destroy(hw->rnode_mempool);
  427. hw->rnode_mempool = NULL;
  428. mempool_destroy(hw->mb_mempool);
  429. hw->mb_mempool = NULL;
  430. }
  431. /*
  432. * csio_hw_alloc - Allocate and initialize the HW module.
  433. * @pdev: PCI device.
  434. *
  435. * Allocates HW structure, DMA, memory resources, maps BARS to
  436. * host memory and initializes HW module.
  437. */
  438. static struct csio_hw *csio_hw_alloc(struct pci_dev *pdev)
  439. {
  440. struct csio_hw *hw;
  441. hw = kzalloc(sizeof(struct csio_hw), GFP_KERNEL);
  442. if (!hw)
  443. goto err;
  444. hw->pdev = pdev;
  445. strncpy(hw->drv_version, CSIO_DRV_VERSION, 32);
  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. .driver = {
  1006. .owner = THIS_MODULE,
  1007. },
  1008. .id_table = csio_pci_tbl,
  1009. .probe = csio_probe_one,
  1010. .remove = csio_remove_one,
  1011. .err_handler = &csio_err_handler,
  1012. };
  1013. /*
  1014. * csio_init - Chelsio storage driver initialization function.
  1015. *
  1016. */
  1017. static int __init
  1018. csio_init(void)
  1019. {
  1020. int rv = -ENOMEM;
  1021. pr_info("%s %s\n", CSIO_DRV_DESC, CSIO_DRV_VERSION);
  1022. csio_dfs_init();
  1023. csio_fcoe_transport = fc_attach_transport(&csio_fc_transport_funcs);
  1024. if (!csio_fcoe_transport)
  1025. goto err;
  1026. csio_fcoe_transport_vport =
  1027. fc_attach_transport(&csio_fc_transport_vport_funcs);
  1028. if (!csio_fcoe_transport_vport)
  1029. goto err_vport;
  1030. rv = pci_register_driver(&csio_pci_driver);
  1031. if (rv)
  1032. goto err_pci;
  1033. return 0;
  1034. err_pci:
  1035. fc_release_transport(csio_fcoe_transport_vport);
  1036. err_vport:
  1037. fc_release_transport(csio_fcoe_transport);
  1038. err:
  1039. csio_dfs_exit();
  1040. return rv;
  1041. }
  1042. /*
  1043. * csio_exit - Chelsio storage driver uninitialization .
  1044. *
  1045. * Function that gets called in the unload path.
  1046. */
  1047. static void __exit
  1048. csio_exit(void)
  1049. {
  1050. pci_unregister_driver(&csio_pci_driver);
  1051. csio_dfs_exit();
  1052. fc_release_transport(csio_fcoe_transport_vport);
  1053. fc_release_transport(csio_fcoe_transport);
  1054. }
  1055. module_init(csio_init);
  1056. module_exit(csio_exit);
  1057. MODULE_AUTHOR(CSIO_DRV_AUTHOR);
  1058. MODULE_DESCRIPTION(CSIO_DRV_DESC);
  1059. MODULE_LICENSE("Dual BSD/GPL");
  1060. MODULE_DEVICE_TABLE(pci, csio_pci_tbl);
  1061. MODULE_VERSION(CSIO_DRV_VERSION);
  1062. MODULE_FIRMWARE(FW_FNAME_T5);
  1063. MODULE_FIRMWARE(FW_FNAME_T6);
  1064. MODULE_SOFTDEP("pre: cxgb4");