csio_init.c 29 KB

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