csio_init.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  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_pci_pool = pci_pool_create("csio_scsi_pci_pool", hw->pdev,
  413. CSIO_SCSI_RSP_LEN, 8, 0);
  414. if (!hw->scsi_pci_pool)
  415. goto err_free_rn_pool;
  416. return 0;
  417. err_free_rn_pool:
  418. mempool_destroy(hw->rnode_mempool);
  419. hw->rnode_mempool = NULL;
  420. err_free_mb_mempool:
  421. mempool_destroy(hw->mb_mempool);
  422. hw->mb_mempool = NULL;
  423. err:
  424. return rv;
  425. }
  426. static void
  427. csio_resource_free(struct csio_hw *hw)
  428. {
  429. pci_pool_destroy(hw->scsi_pci_pool);
  430. hw->scsi_pci_pool = NULL;
  431. mempool_destroy(hw->rnode_mempool);
  432. hw->rnode_mempool = NULL;
  433. mempool_destroy(hw->mb_mempool);
  434. hw->mb_mempool = NULL;
  435. }
  436. /*
  437. * csio_hw_alloc - Allocate and initialize the HW module.
  438. * @pdev: PCI device.
  439. *
  440. * Allocates HW structure, DMA, memory resources, maps BARS to
  441. * host memory and initializes HW module.
  442. */
  443. static struct csio_hw *csio_hw_alloc(struct pci_dev *pdev)
  444. {
  445. struct csio_hw *hw;
  446. hw = kzalloc(sizeof(struct csio_hw), GFP_KERNEL);
  447. if (!hw)
  448. goto err;
  449. hw->pdev = pdev;
  450. strncpy(hw->drv_version, CSIO_DRV_VERSION, 32);
  451. /* memory pool/DMA pool allocation */
  452. if (csio_resource_alloc(hw))
  453. goto err_free_hw;
  454. /* Get the start address of registers from BAR 0 */
  455. hw->regstart = ioremap_nocache(pci_resource_start(pdev, 0),
  456. pci_resource_len(pdev, 0));
  457. if (!hw->regstart) {
  458. csio_err(hw, "Could not map BAR 0, regstart = %p\n",
  459. hw->regstart);
  460. goto err_resource_free;
  461. }
  462. csio_hw_init_workers(hw);
  463. if (csio_hw_init(hw))
  464. goto err_unmap_bar;
  465. csio_dfs_create(hw);
  466. csio_dbg(hw, "hw:%p\n", hw);
  467. return hw;
  468. err_unmap_bar:
  469. csio_hw_exit_workers(hw);
  470. iounmap(hw->regstart);
  471. err_resource_free:
  472. csio_resource_free(hw);
  473. err_free_hw:
  474. kfree(hw);
  475. err:
  476. return NULL;
  477. }
  478. /*
  479. * csio_hw_free - Uninitialize and free the HW module.
  480. * @hw: The HW module
  481. *
  482. * Disable interrupts, uninit the HW module, free resources, free hw.
  483. */
  484. static void
  485. csio_hw_free(struct csio_hw *hw)
  486. {
  487. csio_intr_disable(hw, true);
  488. csio_hw_exit_workers(hw);
  489. csio_hw_exit(hw);
  490. iounmap(hw->regstart);
  491. csio_dfs_destroy(hw);
  492. csio_resource_free(hw);
  493. kfree(hw);
  494. }
  495. /**
  496. * csio_shost_init - Create and initialize the lnode module.
  497. * @hw: The HW module.
  498. * @dev: The device associated with this invocation.
  499. * @probe: Called from probe context or not?
  500. * @os_pln: Parent lnode if any.
  501. *
  502. * Allocates lnode structure via scsi_host_alloc, initializes
  503. * shost, initializes lnode module and registers with SCSI ML
  504. * via scsi_host_add. This function is shared between physical and
  505. * virtual node ports.
  506. */
  507. struct csio_lnode *
  508. csio_shost_init(struct csio_hw *hw, struct device *dev,
  509. bool probe, struct csio_lnode *pln)
  510. {
  511. struct Scsi_Host *shost = NULL;
  512. struct csio_lnode *ln;
  513. csio_fcoe_shost_template.cmd_per_lun = csio_lun_qdepth;
  514. csio_fcoe_shost_vport_template.cmd_per_lun = csio_lun_qdepth;
  515. /*
  516. * hw->pdev is the physical port's PCI dev structure,
  517. * which will be different from the NPIV dev structure.
  518. */
  519. if (dev == &hw->pdev->dev)
  520. shost = scsi_host_alloc(
  521. &csio_fcoe_shost_template,
  522. sizeof(struct csio_lnode));
  523. else
  524. shost = scsi_host_alloc(
  525. &csio_fcoe_shost_vport_template,
  526. sizeof(struct csio_lnode));
  527. if (!shost)
  528. goto err;
  529. ln = shost_priv(shost);
  530. memset(ln, 0, sizeof(struct csio_lnode));
  531. /* Link common lnode to this lnode */
  532. ln->dev_num = (shost->host_no << 16);
  533. shost->can_queue = CSIO_MAX_QUEUE;
  534. shost->this_id = -1;
  535. shost->unique_id = shost->host_no;
  536. shost->max_cmd_len = 16; /* Max CDB length supported */
  537. shost->max_id = min_t(uint32_t, csio_fcoe_rnodes,
  538. hw->fres_info.max_ssns);
  539. shost->max_lun = CSIO_MAX_LUN;
  540. if (dev == &hw->pdev->dev)
  541. shost->transportt = csio_fcoe_transport;
  542. else
  543. shost->transportt = csio_fcoe_transport_vport;
  544. /* root lnode */
  545. if (!hw->rln)
  546. hw->rln = ln;
  547. /* Other initialization here: Common, Transport specific */
  548. if (csio_lnode_init(ln, hw, pln))
  549. goto err_shost_put;
  550. if (scsi_add_host(shost, dev))
  551. goto err_lnode_exit;
  552. return ln;
  553. err_lnode_exit:
  554. csio_lnode_exit(ln);
  555. err_shost_put:
  556. scsi_host_put(shost);
  557. err:
  558. return NULL;
  559. }
  560. /**
  561. * csio_shost_exit - De-instantiate the shost.
  562. * @ln: The lnode module corresponding to the shost.
  563. *
  564. */
  565. void
  566. csio_shost_exit(struct csio_lnode *ln)
  567. {
  568. struct Scsi_Host *shost = csio_ln_to_shost(ln);
  569. struct csio_hw *hw = csio_lnode_to_hw(ln);
  570. /* Inform transport */
  571. fc_remove_host(shost);
  572. /* Inform SCSI ML */
  573. scsi_remove_host(shost);
  574. /* Flush all the events, so that any rnode removal events
  575. * already queued are all handled, before we remove the lnode.
  576. */
  577. spin_lock_irq(&hw->lock);
  578. csio_evtq_flush(hw);
  579. spin_unlock_irq(&hw->lock);
  580. csio_lnode_exit(ln);
  581. scsi_host_put(shost);
  582. }
  583. struct csio_lnode *
  584. csio_lnode_alloc(struct csio_hw *hw)
  585. {
  586. return csio_shost_init(hw, &hw->pdev->dev, false, NULL);
  587. }
  588. void
  589. csio_lnodes_block_request(struct csio_hw *hw)
  590. {
  591. struct Scsi_Host *shost;
  592. struct csio_lnode *sln;
  593. struct csio_lnode *ln;
  594. struct list_head *cur_ln, *cur_cln;
  595. struct csio_lnode **lnode_list;
  596. int cur_cnt = 0, ii;
  597. lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
  598. GFP_KERNEL);
  599. if (!lnode_list) {
  600. csio_err(hw, "Failed to allocate lnodes_list");
  601. return;
  602. }
  603. spin_lock_irq(&hw->lock);
  604. /* Traverse sibling lnodes */
  605. list_for_each(cur_ln, &hw->sln_head) {
  606. sln = (struct csio_lnode *) cur_ln;
  607. lnode_list[cur_cnt++] = sln;
  608. /* Traverse children lnodes */
  609. list_for_each(cur_cln, &sln->cln_head)
  610. lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
  611. }
  612. spin_unlock_irq(&hw->lock);
  613. for (ii = 0; ii < cur_cnt; ii++) {
  614. csio_dbg(hw, "Blocking IOs on lnode: %p\n", lnode_list[ii]);
  615. ln = lnode_list[ii];
  616. shost = csio_ln_to_shost(ln);
  617. scsi_block_requests(shost);
  618. }
  619. kfree(lnode_list);
  620. }
  621. void
  622. csio_lnodes_unblock_request(struct csio_hw *hw)
  623. {
  624. struct csio_lnode *ln;
  625. struct Scsi_Host *shost;
  626. struct csio_lnode *sln;
  627. struct list_head *cur_ln, *cur_cln;
  628. struct csio_lnode **lnode_list;
  629. int cur_cnt = 0, ii;
  630. lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
  631. GFP_KERNEL);
  632. if (!lnode_list) {
  633. csio_err(hw, "Failed to allocate lnodes_list");
  634. return;
  635. }
  636. spin_lock_irq(&hw->lock);
  637. /* Traverse sibling lnodes */
  638. list_for_each(cur_ln, &hw->sln_head) {
  639. sln = (struct csio_lnode *) cur_ln;
  640. lnode_list[cur_cnt++] = sln;
  641. /* Traverse children lnodes */
  642. list_for_each(cur_cln, &sln->cln_head)
  643. lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
  644. }
  645. spin_unlock_irq(&hw->lock);
  646. for (ii = 0; ii < cur_cnt; ii++) {
  647. csio_dbg(hw, "unblocking IOs on lnode: %p\n", lnode_list[ii]);
  648. ln = lnode_list[ii];
  649. shost = csio_ln_to_shost(ln);
  650. scsi_unblock_requests(shost);
  651. }
  652. kfree(lnode_list);
  653. }
  654. void
  655. csio_lnodes_block_by_port(struct csio_hw *hw, uint8_t portid)
  656. {
  657. struct csio_lnode *ln;
  658. struct Scsi_Host *shost;
  659. struct csio_lnode *sln;
  660. struct list_head *cur_ln, *cur_cln;
  661. struct csio_lnode **lnode_list;
  662. int cur_cnt = 0, ii;
  663. lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
  664. GFP_KERNEL);
  665. if (!lnode_list) {
  666. csio_err(hw, "Failed to allocate lnodes_list");
  667. return;
  668. }
  669. spin_lock_irq(&hw->lock);
  670. /* Traverse sibling lnodes */
  671. list_for_each(cur_ln, &hw->sln_head) {
  672. sln = (struct csio_lnode *) cur_ln;
  673. if (sln->portid != portid)
  674. continue;
  675. lnode_list[cur_cnt++] = sln;
  676. /* Traverse children lnodes */
  677. list_for_each(cur_cln, &sln->cln_head)
  678. lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
  679. }
  680. spin_unlock_irq(&hw->lock);
  681. for (ii = 0; ii < cur_cnt; ii++) {
  682. csio_dbg(hw, "Blocking IOs on lnode: %p\n", lnode_list[ii]);
  683. ln = lnode_list[ii];
  684. shost = csio_ln_to_shost(ln);
  685. scsi_block_requests(shost);
  686. }
  687. kfree(lnode_list);
  688. }
  689. void
  690. csio_lnodes_unblock_by_port(struct csio_hw *hw, uint8_t portid)
  691. {
  692. struct csio_lnode *ln;
  693. struct Scsi_Host *shost;
  694. struct csio_lnode *sln;
  695. struct list_head *cur_ln, *cur_cln;
  696. struct csio_lnode **lnode_list;
  697. int cur_cnt = 0, ii;
  698. lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
  699. GFP_KERNEL);
  700. if (!lnode_list) {
  701. csio_err(hw, "Failed to allocate lnodes_list");
  702. return;
  703. }
  704. spin_lock_irq(&hw->lock);
  705. /* Traverse sibling lnodes */
  706. list_for_each(cur_ln, &hw->sln_head) {
  707. sln = (struct csio_lnode *) cur_ln;
  708. if (sln->portid != portid)
  709. continue;
  710. lnode_list[cur_cnt++] = sln;
  711. /* Traverse children lnodes */
  712. list_for_each(cur_cln, &sln->cln_head)
  713. lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
  714. }
  715. spin_unlock_irq(&hw->lock);
  716. for (ii = 0; ii < cur_cnt; ii++) {
  717. csio_dbg(hw, "unblocking IOs on lnode: %p\n", lnode_list[ii]);
  718. ln = lnode_list[ii];
  719. shost = csio_ln_to_shost(ln);
  720. scsi_unblock_requests(shost);
  721. }
  722. kfree(lnode_list);
  723. }
  724. void
  725. csio_lnodes_exit(struct csio_hw *hw, bool npiv)
  726. {
  727. struct csio_lnode *sln;
  728. struct csio_lnode *ln;
  729. struct list_head *cur_ln, *cur_cln;
  730. struct csio_lnode **lnode_list;
  731. int cur_cnt = 0, ii;
  732. lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
  733. GFP_KERNEL);
  734. if (!lnode_list) {
  735. csio_err(hw, "lnodes_exit: Failed to allocate lnodes_list.\n");
  736. return;
  737. }
  738. /* Get all child lnodes(NPIV ports) */
  739. spin_lock_irq(&hw->lock);
  740. list_for_each(cur_ln, &hw->sln_head) {
  741. sln = (struct csio_lnode *) cur_ln;
  742. /* Traverse children lnodes */
  743. list_for_each(cur_cln, &sln->cln_head)
  744. lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
  745. }
  746. spin_unlock_irq(&hw->lock);
  747. /* Delete NPIV lnodes */
  748. for (ii = 0; ii < cur_cnt; ii++) {
  749. csio_dbg(hw, "Deleting child lnode: %p\n", lnode_list[ii]);
  750. ln = lnode_list[ii];
  751. fc_vport_terminate(ln->fc_vport);
  752. }
  753. /* Delete only npiv lnodes */
  754. if (npiv)
  755. goto free_lnodes;
  756. cur_cnt = 0;
  757. /* Get all physical lnodes */
  758. spin_lock_irq(&hw->lock);
  759. /* Traverse sibling lnodes */
  760. list_for_each(cur_ln, &hw->sln_head) {
  761. sln = (struct csio_lnode *) cur_ln;
  762. lnode_list[cur_cnt++] = sln;
  763. }
  764. spin_unlock_irq(&hw->lock);
  765. /* Delete physical lnodes */
  766. for (ii = 0; ii < cur_cnt; ii++) {
  767. csio_dbg(hw, "Deleting parent lnode: %p\n", lnode_list[ii]);
  768. csio_shost_exit(lnode_list[ii]);
  769. }
  770. free_lnodes:
  771. kfree(lnode_list);
  772. }
  773. /*
  774. * csio_lnode_init_post: Set lnode attributes after starting HW.
  775. * @ln: lnode.
  776. *
  777. */
  778. static void
  779. csio_lnode_init_post(struct csio_lnode *ln)
  780. {
  781. struct Scsi_Host *shost = csio_ln_to_shost(ln);
  782. csio_fchost_attr_init(ln);
  783. scsi_scan_host(shost);
  784. }
  785. /*
  786. * csio_probe_one - Instantiate this function.
  787. * @pdev: PCI device
  788. * @id: Device ID
  789. *
  790. * This is the .probe() callback of the driver. This function:
  791. * - Initializes the PCI function by enabling MMIO, setting bus
  792. * mastership and setting DMA mask.
  793. * - Allocates HW structure, DMA, memory resources, maps BARS to
  794. * host memory and initializes HW module.
  795. * - Allocates lnode structure via scsi_host_alloc, initializes
  796. * shost, initialized lnode module and registers with SCSI ML
  797. * via scsi_host_add.
  798. * - Enables interrupts, and starts the chip by kicking off the
  799. * HW state machine.
  800. * - Once hardware is ready, initiated scan of the host via
  801. * scsi_scan_host.
  802. */
  803. static int csio_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  804. {
  805. int rv;
  806. int bars;
  807. int i;
  808. struct csio_hw *hw;
  809. struct csio_lnode *ln;
  810. /* probe only T5 cards */
  811. if (!csio_is_t5((pdev->device & CSIO_HW_CHIP_MASK)))
  812. return -ENODEV;
  813. rv = csio_pci_init(pdev, &bars);
  814. if (rv)
  815. goto err;
  816. hw = csio_hw_alloc(pdev);
  817. if (!hw) {
  818. rv = -ENODEV;
  819. goto err_pci_exit;
  820. }
  821. pci_set_drvdata(pdev, hw);
  822. if (csio_hw_start(hw) != 0) {
  823. dev_err(&pdev->dev,
  824. "Failed to start FW, continuing in debug mode.\n");
  825. return 0;
  826. }
  827. sprintf(hw->fwrev_str, "%u.%u.%u.%u\n",
  828. FW_HDR_FW_VER_MAJOR_G(hw->fwrev),
  829. FW_HDR_FW_VER_MINOR_G(hw->fwrev),
  830. FW_HDR_FW_VER_MICRO_G(hw->fwrev),
  831. FW_HDR_FW_VER_BUILD_G(hw->fwrev));
  832. for (i = 0; i < hw->num_pports; i++) {
  833. ln = csio_shost_init(hw, &pdev->dev, true, NULL);
  834. if (!ln) {
  835. rv = -ENODEV;
  836. break;
  837. }
  838. /* Initialize portid */
  839. ln->portid = hw->pport[i].portid;
  840. spin_lock_irq(&hw->lock);
  841. if (csio_lnode_start(ln) != 0)
  842. rv = -ENODEV;
  843. spin_unlock_irq(&hw->lock);
  844. if (rv)
  845. break;
  846. csio_lnode_init_post(ln);
  847. }
  848. if (rv)
  849. goto err_lnode_exit;
  850. return 0;
  851. err_lnode_exit:
  852. csio_lnodes_block_request(hw);
  853. spin_lock_irq(&hw->lock);
  854. csio_hw_stop(hw);
  855. spin_unlock_irq(&hw->lock);
  856. csio_lnodes_unblock_request(hw);
  857. csio_lnodes_exit(hw, 0);
  858. csio_hw_free(hw);
  859. err_pci_exit:
  860. csio_pci_exit(pdev, &bars);
  861. err:
  862. dev_err(&pdev->dev, "probe of device failed: %d\n", rv);
  863. return rv;
  864. }
  865. /*
  866. * csio_remove_one - Remove one instance of the driver at this PCI function.
  867. * @pdev: PCI device
  868. *
  869. * Used during hotplug operation.
  870. */
  871. static void csio_remove_one(struct pci_dev *pdev)
  872. {
  873. struct csio_hw *hw = pci_get_drvdata(pdev);
  874. int bars = pci_select_bars(pdev, IORESOURCE_MEM);
  875. csio_lnodes_block_request(hw);
  876. spin_lock_irq(&hw->lock);
  877. /* Stops lnode, Rnode s/m
  878. * Quiesce IOs.
  879. * All sessions with remote ports are unregistered.
  880. */
  881. csio_hw_stop(hw);
  882. spin_unlock_irq(&hw->lock);
  883. csio_lnodes_unblock_request(hw);
  884. csio_lnodes_exit(hw, 0);
  885. csio_hw_free(hw);
  886. csio_pci_exit(pdev, &bars);
  887. }
  888. /*
  889. * csio_pci_error_detected - PCI error was detected
  890. * @pdev: PCI device
  891. *
  892. */
  893. static pci_ers_result_t
  894. csio_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  895. {
  896. struct csio_hw *hw = pci_get_drvdata(pdev);
  897. csio_lnodes_block_request(hw);
  898. spin_lock_irq(&hw->lock);
  899. /* Post PCI error detected evt to HW s/m
  900. * HW s/m handles this evt by quiescing IOs, unregisters rports
  901. * and finally takes the device to offline.
  902. */
  903. csio_post_event(&hw->sm, CSIO_HWE_PCIERR_DETECTED);
  904. spin_unlock_irq(&hw->lock);
  905. csio_lnodes_unblock_request(hw);
  906. csio_lnodes_exit(hw, 0);
  907. csio_intr_disable(hw, true);
  908. pci_disable_device(pdev);
  909. return state == pci_channel_io_perm_failure ?
  910. PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
  911. }
  912. /*
  913. * csio_pci_slot_reset - PCI slot has been reset.
  914. * @pdev: PCI device
  915. *
  916. */
  917. static pci_ers_result_t
  918. csio_pci_slot_reset(struct pci_dev *pdev)
  919. {
  920. struct csio_hw *hw = pci_get_drvdata(pdev);
  921. int ready;
  922. if (pci_enable_device(pdev)) {
  923. dev_err(&pdev->dev, "cannot re-enable device in slot reset\n");
  924. return PCI_ERS_RESULT_DISCONNECT;
  925. }
  926. pci_set_master(pdev);
  927. pci_restore_state(pdev);
  928. pci_save_state(pdev);
  929. pci_cleanup_aer_uncorrect_error_status(pdev);
  930. /* Bring HW s/m to ready state.
  931. * but don't resume IOs.
  932. */
  933. spin_lock_irq(&hw->lock);
  934. csio_post_event(&hw->sm, CSIO_HWE_PCIERR_SLOT_RESET);
  935. ready = csio_is_hw_ready(hw);
  936. spin_unlock_irq(&hw->lock);
  937. if (ready) {
  938. return PCI_ERS_RESULT_RECOVERED;
  939. } else {
  940. dev_err(&pdev->dev, "Can't initialize HW when in slot reset\n");
  941. return PCI_ERS_RESULT_DISCONNECT;
  942. }
  943. }
  944. /*
  945. * csio_pci_resume - Resume normal operations
  946. * @pdev: PCI device
  947. *
  948. */
  949. static void
  950. csio_pci_resume(struct pci_dev *pdev)
  951. {
  952. struct csio_hw *hw = pci_get_drvdata(pdev);
  953. struct csio_lnode *ln;
  954. int rv = 0;
  955. int i;
  956. /* Bring the LINK UP and Resume IO */
  957. for (i = 0; i < hw->num_pports; i++) {
  958. ln = csio_shost_init(hw, &pdev->dev, true, NULL);
  959. if (!ln) {
  960. rv = -ENODEV;
  961. break;
  962. }
  963. /* Initialize portid */
  964. ln->portid = hw->pport[i].portid;
  965. spin_lock_irq(&hw->lock);
  966. if (csio_lnode_start(ln) != 0)
  967. rv = -ENODEV;
  968. spin_unlock_irq(&hw->lock);
  969. if (rv)
  970. break;
  971. csio_lnode_init_post(ln);
  972. }
  973. if (rv)
  974. goto err_resume_exit;
  975. return;
  976. err_resume_exit:
  977. csio_lnodes_block_request(hw);
  978. spin_lock_irq(&hw->lock);
  979. csio_hw_stop(hw);
  980. spin_unlock_irq(&hw->lock);
  981. csio_lnodes_unblock_request(hw);
  982. csio_lnodes_exit(hw, 0);
  983. csio_hw_free(hw);
  984. dev_err(&pdev->dev, "resume of device failed: %d\n", rv);
  985. }
  986. static struct pci_error_handlers csio_err_handler = {
  987. .error_detected = csio_pci_error_detected,
  988. .slot_reset = csio_pci_slot_reset,
  989. .resume = csio_pci_resume,
  990. };
  991. /*
  992. * Macros needed to support the PCI Device ID Table ...
  993. */
  994. #define CH_PCI_DEVICE_ID_TABLE_DEFINE_BEGIN \
  995. static const struct pci_device_id csio_pci_tbl[] = {
  996. /* Define for FCoE uses PF6 */
  997. #define CH_PCI_DEVICE_ID_FUNCTION 0x6
  998. #define CH_PCI_ID_TABLE_ENTRY(devid) \
  999. { PCI_VDEVICE(CHELSIO, (devid)), 0 }
  1000. #define CH_PCI_DEVICE_ID_TABLE_DEFINE_END { 0, } }
  1001. #include "t4_pci_id_tbl.h"
  1002. static struct pci_driver csio_pci_driver = {
  1003. .name = KBUILD_MODNAME,
  1004. .driver = {
  1005. .owner = THIS_MODULE,
  1006. },
  1007. .id_table = csio_pci_tbl,
  1008. .probe = csio_probe_one,
  1009. .remove = csio_remove_one,
  1010. .err_handler = &csio_err_handler,
  1011. };
  1012. /*
  1013. * csio_init - Chelsio storage driver initialization function.
  1014. *
  1015. */
  1016. static int __init
  1017. csio_init(void)
  1018. {
  1019. int rv = -ENOMEM;
  1020. pr_info("%s %s\n", CSIO_DRV_DESC, CSIO_DRV_VERSION);
  1021. csio_dfs_init();
  1022. csio_fcoe_transport = fc_attach_transport(&csio_fc_transport_funcs);
  1023. if (!csio_fcoe_transport)
  1024. goto err;
  1025. csio_fcoe_transport_vport =
  1026. fc_attach_transport(&csio_fc_transport_vport_funcs);
  1027. if (!csio_fcoe_transport_vport)
  1028. goto err_vport;
  1029. rv = pci_register_driver(&csio_pci_driver);
  1030. if (rv)
  1031. goto err_pci;
  1032. return 0;
  1033. err_pci:
  1034. fc_release_transport(csio_fcoe_transport_vport);
  1035. err_vport:
  1036. fc_release_transport(csio_fcoe_transport);
  1037. err:
  1038. csio_dfs_exit();
  1039. return rv;
  1040. }
  1041. /*
  1042. * csio_exit - Chelsio storage driver uninitialization .
  1043. *
  1044. * Function that gets called in the unload path.
  1045. */
  1046. static void __exit
  1047. csio_exit(void)
  1048. {
  1049. pci_unregister_driver(&csio_pci_driver);
  1050. csio_dfs_exit();
  1051. fc_release_transport(csio_fcoe_transport_vport);
  1052. fc_release_transport(csio_fcoe_transport);
  1053. }
  1054. module_init(csio_init);
  1055. module_exit(csio_exit);
  1056. MODULE_AUTHOR(CSIO_DRV_AUTHOR);
  1057. MODULE_DESCRIPTION(CSIO_DRV_DESC);
  1058. MODULE_LICENSE(CSIO_DRV_LICENSE);
  1059. MODULE_DEVICE_TABLE(pci, csio_pci_tbl);
  1060. MODULE_VERSION(CSIO_DRV_VERSION);
  1061. /*(DEBLOBBED)*/