init.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  23. *
  24. * BSD LICENSE
  25. *
  26. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  27. * All rights reserved.
  28. *
  29. * Redistribution and use in source and binary forms, with or without
  30. * modification, are permitted provided that the following conditions
  31. * are met:
  32. *
  33. * * Redistributions of source code must retain the above copyright
  34. * notice, this list of conditions and the following disclaimer.
  35. * * Redistributions in binary form must reproduce the above copyright
  36. * notice, this list of conditions and the following disclaimer in
  37. * the documentation and/or other materials provided with the
  38. * distribution.
  39. * * Neither the name of Intel Corporation nor the names of its
  40. * contributors may be used to endorse or promote products derived
  41. * from this software without specific prior written permission.
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  44. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  45. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  46. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  47. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  48. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  49. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  50. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  51. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  52. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  53. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  54. */
  55. #include <linux/kernel.h>
  56. #include <linux/init.h>
  57. #include <linux/module.h>
  58. #include <linux/firmware.h>
  59. #include <linux/efi.h>
  60. #include <asm/string.h>
  61. #include "isci.h"
  62. #include "task.h"
  63. #include "probe_roms.h"
  64. static struct scsi_transport_template *isci_transport_template;
  65. static DEFINE_PCI_DEVICE_TABLE(isci_id_table) = {
  66. { PCI_VDEVICE(INTEL, 0x1D61),},
  67. { PCI_VDEVICE(INTEL, 0x1D63),},
  68. { PCI_VDEVICE(INTEL, 0x1D65),},
  69. { PCI_VDEVICE(INTEL, 0x1D67),},
  70. { PCI_VDEVICE(INTEL, 0x1D69),},
  71. { PCI_VDEVICE(INTEL, 0x1D6B),},
  72. { PCI_VDEVICE(INTEL, 0x1D60),},
  73. { PCI_VDEVICE(INTEL, 0x1D62),},
  74. { PCI_VDEVICE(INTEL, 0x1D64),},
  75. { PCI_VDEVICE(INTEL, 0x1D66),},
  76. { PCI_VDEVICE(INTEL, 0x1D68),},
  77. { PCI_VDEVICE(INTEL, 0x1D6A),},
  78. {}
  79. };
  80. MODULE_DEVICE_TABLE(pci, isci_id_table);
  81. /* linux isci specific settings */
  82. unsigned char no_outbound_task_to = 20;
  83. module_param(no_outbound_task_to, byte, 0);
  84. MODULE_PARM_DESC(no_outbound_task_to, "No Outbound Task Timeout (1us incr)");
  85. u16 ssp_max_occ_to = 20;
  86. module_param(ssp_max_occ_to, ushort, 0);
  87. MODULE_PARM_DESC(ssp_max_occ_to, "SSP Max occupancy timeout (100us incr)");
  88. u16 stp_max_occ_to = 5;
  89. module_param(stp_max_occ_to, ushort, 0);
  90. MODULE_PARM_DESC(stp_max_occ_to, "STP Max occupancy timeout (100us incr)");
  91. u16 ssp_inactive_to = 5;
  92. module_param(ssp_inactive_to, ushort, 0);
  93. MODULE_PARM_DESC(ssp_inactive_to, "SSP inactivity timeout (100us incr)");
  94. u16 stp_inactive_to = 5;
  95. module_param(stp_inactive_to, ushort, 0);
  96. MODULE_PARM_DESC(stp_inactive_to, "STP inactivity timeout (100us incr)");
  97. unsigned char phy_gen = 3;
  98. module_param(phy_gen, byte, 0);
  99. MODULE_PARM_DESC(phy_gen, "PHY generation (1: 1.5Gbps 2: 3.0Gbps 3: 6.0Gbps)");
  100. unsigned char max_concurr_spinup = 1;
  101. module_param(max_concurr_spinup, byte, 0);
  102. MODULE_PARM_DESC(max_concurr_spinup, "Max concurrent device spinup");
  103. static struct scsi_host_template isci_sht = {
  104. .module = THIS_MODULE,
  105. .name = DRV_NAME,
  106. .proc_name = DRV_NAME,
  107. .queuecommand = sas_queuecommand,
  108. .target_alloc = sas_target_alloc,
  109. .slave_configure = sas_slave_configure,
  110. .slave_destroy = sas_slave_destroy,
  111. .scan_finished = isci_host_scan_finished,
  112. .scan_start = isci_host_scan_start,
  113. .change_queue_depth = sas_change_queue_depth,
  114. .change_queue_type = sas_change_queue_type,
  115. .bios_param = sas_bios_param,
  116. .can_queue = ISCI_CAN_QUEUE_VAL,
  117. .cmd_per_lun = 1,
  118. .this_id = -1,
  119. .sg_tablesize = SG_ALL,
  120. .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
  121. .use_clustering = ENABLE_CLUSTERING,
  122. .eh_device_reset_handler = sas_eh_device_reset_handler,
  123. .eh_bus_reset_handler = isci_bus_reset_handler,
  124. .slave_alloc = sas_slave_alloc,
  125. .target_destroy = sas_target_destroy,
  126. .ioctl = sas_ioctl,
  127. };
  128. static struct sas_domain_function_template isci_transport_ops = {
  129. /* The class calls these to notify the LLDD of an event. */
  130. .lldd_port_formed = isci_port_formed,
  131. .lldd_port_deformed = isci_port_deformed,
  132. /* The class calls these when a device is found or gone. */
  133. .lldd_dev_found = isci_remote_device_found,
  134. .lldd_dev_gone = isci_remote_device_gone,
  135. .lldd_execute_task = isci_task_execute_task,
  136. /* Task Management Functions. Must be called from process context. */
  137. .lldd_abort_task = isci_task_abort_task,
  138. .lldd_abort_task_set = isci_task_abort_task_set,
  139. .lldd_clear_aca = isci_task_clear_aca,
  140. .lldd_clear_task_set = isci_task_clear_task_set,
  141. .lldd_I_T_nexus_reset = isci_task_I_T_nexus_reset,
  142. .lldd_lu_reset = isci_task_lu_reset,
  143. .lldd_query_task = isci_task_query_task,
  144. /* Port and Adapter management */
  145. .lldd_clear_nexus_port = isci_task_clear_nexus_port,
  146. .lldd_clear_nexus_ha = isci_task_clear_nexus_ha,
  147. /* Phy management */
  148. .lldd_control_phy = isci_phy_control,
  149. };
  150. /******************************************************************************
  151. * P R O T E C T E D M E T H O D S
  152. ******************************************************************************/
  153. /**
  154. * isci_register_sas_ha() - This method initializes various lldd
  155. * specific members of the sas_ha struct and calls the libsas
  156. * sas_register_ha() function.
  157. * @isci_host: This parameter specifies the lldd specific wrapper for the
  158. * libsas sas_ha struct.
  159. *
  160. * This method returns an error code indicating sucess or failure. The user
  161. * should check for possible memory allocation error return otherwise, a zero
  162. * indicates success.
  163. */
  164. static int isci_register_sas_ha(struct isci_host *isci_host)
  165. {
  166. int i;
  167. struct sas_ha_struct *sas_ha = &(isci_host->sas_ha);
  168. struct asd_sas_phy **sas_phys;
  169. struct asd_sas_port **sas_ports;
  170. sas_phys = devm_kzalloc(&isci_host->pdev->dev,
  171. SCI_MAX_PHYS * sizeof(void *),
  172. GFP_KERNEL);
  173. if (!sas_phys)
  174. return -ENOMEM;
  175. sas_ports = devm_kzalloc(&isci_host->pdev->dev,
  176. SCI_MAX_PORTS * sizeof(void *),
  177. GFP_KERNEL);
  178. if (!sas_ports)
  179. return -ENOMEM;
  180. /*----------------- Libsas Initialization Stuff----------------------
  181. * Set various fields in the sas_ha struct:
  182. */
  183. sas_ha->sas_ha_name = DRV_NAME;
  184. sas_ha->lldd_module = THIS_MODULE;
  185. sas_ha->sas_addr = &isci_host->phys[0].sas_addr[0];
  186. /* set the array of phy and port structs. */
  187. for (i = 0; i < SCI_MAX_PHYS; i++) {
  188. sas_phys[i] = &isci_host->phys[i].sas_phy;
  189. sas_ports[i] = &isci_host->ports[i].sas_port;
  190. }
  191. sas_ha->sas_phy = sas_phys;
  192. sas_ha->sas_port = sas_ports;
  193. sas_ha->num_phys = SCI_MAX_PHYS;
  194. sas_ha->lldd_queue_size = ISCI_CAN_QUEUE_VAL;
  195. sas_ha->lldd_max_execute_num = 1;
  196. sas_ha->strict_wide_ports = 1;
  197. sas_register_ha(sas_ha);
  198. return 0;
  199. }
  200. static ssize_t isci_show_id(struct device *dev, struct device_attribute *attr, char *buf)
  201. {
  202. struct Scsi_Host *shost = container_of(dev, typeof(*shost), shost_dev);
  203. struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
  204. struct isci_host *ihost = container_of(sas_ha, typeof(*ihost), sas_ha);
  205. return snprintf(buf, PAGE_SIZE, "%d\n", ihost->id);
  206. }
  207. static DEVICE_ATTR(isci_id, S_IRUGO, isci_show_id, NULL);
  208. static void isci_unregister(struct isci_host *isci_host)
  209. {
  210. struct Scsi_Host *shost;
  211. if (!isci_host)
  212. return;
  213. shost = isci_host->shost;
  214. device_remove_file(&shost->shost_dev, &dev_attr_isci_id);
  215. sas_unregister_ha(&isci_host->sas_ha);
  216. sas_remove_host(isci_host->shost);
  217. scsi_remove_host(isci_host->shost);
  218. scsi_host_put(isci_host->shost);
  219. }
  220. static int __devinit isci_pci_init(struct pci_dev *pdev)
  221. {
  222. int err, bar_num, bar_mask = 0;
  223. void __iomem * const *iomap;
  224. err = pcim_enable_device(pdev);
  225. if (err) {
  226. dev_err(&pdev->dev,
  227. "failed enable PCI device %s!\n",
  228. pci_name(pdev));
  229. return err;
  230. }
  231. for (bar_num = 0; bar_num < SCI_PCI_BAR_COUNT; bar_num++)
  232. bar_mask |= 1 << (bar_num * 2);
  233. err = pcim_iomap_regions(pdev, bar_mask, DRV_NAME);
  234. if (err)
  235. return err;
  236. iomap = pcim_iomap_table(pdev);
  237. if (!iomap)
  238. return -ENOMEM;
  239. pci_set_master(pdev);
  240. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  241. if (err) {
  242. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  243. if (err)
  244. return err;
  245. }
  246. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  247. if (err) {
  248. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  249. if (err)
  250. return err;
  251. }
  252. return 0;
  253. }
  254. static int num_controllers(struct pci_dev *pdev)
  255. {
  256. /* bar size alone can tell us if we are running with a dual controller
  257. * part, no need to trust revision ids that might be under broken firmware
  258. * control
  259. */
  260. resource_size_t scu_bar_size = pci_resource_len(pdev, SCI_SCU_BAR*2);
  261. resource_size_t smu_bar_size = pci_resource_len(pdev, SCI_SMU_BAR*2);
  262. if (scu_bar_size >= SCI_SCU_BAR_SIZE*SCI_MAX_CONTROLLERS &&
  263. smu_bar_size >= SCI_SMU_BAR_SIZE*SCI_MAX_CONTROLLERS)
  264. return SCI_MAX_CONTROLLERS;
  265. else
  266. return 1;
  267. }
  268. static int isci_setup_interrupts(struct pci_dev *pdev)
  269. {
  270. int err, i, num_msix;
  271. struct isci_host *ihost;
  272. struct isci_pci_info *pci_info = to_pci_info(pdev);
  273. /*
  274. * Determine the number of vectors associated with this
  275. * PCI function.
  276. */
  277. num_msix = num_controllers(pdev) * SCI_NUM_MSI_X_INT;
  278. for (i = 0; i < num_msix; i++)
  279. pci_info->msix_entries[i].entry = i;
  280. err = pci_enable_msix(pdev, pci_info->msix_entries, num_msix);
  281. if (err)
  282. goto intx;
  283. for (i = 0; i < num_msix; i++) {
  284. int id = i / SCI_NUM_MSI_X_INT;
  285. struct msix_entry *msix = &pci_info->msix_entries[i];
  286. irq_handler_t isr;
  287. ihost = pci_info->hosts[id];
  288. /* odd numbered vectors are error interrupts */
  289. if (i & 1)
  290. isr = isci_error_isr;
  291. else
  292. isr = isci_msix_isr;
  293. err = devm_request_irq(&pdev->dev, msix->vector, isr, 0,
  294. DRV_NAME"-msix", ihost);
  295. if (!err)
  296. continue;
  297. dev_info(&pdev->dev, "msix setup failed falling back to intx\n");
  298. while (i--) {
  299. id = i / SCI_NUM_MSI_X_INT;
  300. ihost = pci_info->hosts[id];
  301. msix = &pci_info->msix_entries[i];
  302. devm_free_irq(&pdev->dev, msix->vector, ihost);
  303. }
  304. pci_disable_msix(pdev);
  305. goto intx;
  306. }
  307. return 0;
  308. intx:
  309. for_each_isci_host(i, ihost, pdev) {
  310. err = devm_request_irq(&pdev->dev, pdev->irq, isci_intx_isr,
  311. IRQF_SHARED, DRV_NAME"-intx", ihost);
  312. if (err)
  313. break;
  314. }
  315. return err;
  316. }
  317. static struct isci_host *isci_host_alloc(struct pci_dev *pdev, int id)
  318. {
  319. struct isci_host *isci_host;
  320. struct Scsi_Host *shost;
  321. int err;
  322. isci_host = devm_kzalloc(&pdev->dev, sizeof(*isci_host), GFP_KERNEL);
  323. if (!isci_host)
  324. return NULL;
  325. isci_host->pdev = pdev;
  326. isci_host->id = id;
  327. shost = scsi_host_alloc(&isci_sht, sizeof(void *));
  328. if (!shost)
  329. return NULL;
  330. isci_host->shost = shost;
  331. err = isci_host_init(isci_host);
  332. if (err)
  333. goto err_shost;
  334. SHOST_TO_SAS_HA(shost) = &isci_host->sas_ha;
  335. isci_host->sas_ha.core.shost = shost;
  336. shost->transportt = isci_transport_template;
  337. shost->max_id = ~0;
  338. shost->max_lun = ~0;
  339. shost->max_cmd_len = MAX_COMMAND_SIZE;
  340. err = scsi_add_host(shost, &pdev->dev);
  341. if (err)
  342. goto err_shost;
  343. err = isci_register_sas_ha(isci_host);
  344. if (err)
  345. goto err_shost_remove;
  346. err = device_create_file(&shost->shost_dev, &dev_attr_isci_id);
  347. if (err)
  348. goto err_unregister_ha;
  349. return isci_host;
  350. err_unregister_ha:
  351. sas_unregister_ha(&(isci_host->sas_ha));
  352. err_shost_remove:
  353. scsi_remove_host(shost);
  354. err_shost:
  355. scsi_host_put(shost);
  356. return NULL;
  357. }
  358. static int __devinit isci_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  359. {
  360. struct isci_pci_info *pci_info;
  361. int err, i;
  362. struct isci_host *isci_host;
  363. const struct firmware *fw = NULL;
  364. struct isci_orom *orom = NULL;
  365. char *source = "(platform)";
  366. dev_info(&pdev->dev, "driver configured for rev: %d silicon\n",
  367. pdev->revision);
  368. pci_info = devm_kzalloc(&pdev->dev, sizeof(*pci_info), GFP_KERNEL);
  369. if (!pci_info)
  370. return -ENOMEM;
  371. pci_set_drvdata(pdev, pci_info);
  372. if (efi_enabled)
  373. orom = isci_get_efi_var(pdev);
  374. if (!orom)
  375. orom = isci_request_oprom(pdev);
  376. for (i = 0; orom && i < ARRAY_SIZE(orom->ctrl); i++) {
  377. if (sci_oem_parameters_validate(&orom->ctrl[i])) {
  378. dev_warn(&pdev->dev,
  379. "[%d]: invalid oem parameters detected, falling back to firmware\n", i);
  380. devm_kfree(&pdev->dev, orom);
  381. orom = NULL;
  382. break;
  383. }
  384. }
  385. if (!orom) {
  386. source = "(firmware)";
  387. orom = isci_request_firmware(pdev, fw);
  388. if (!orom) {
  389. /* TODO convert this to WARN_TAINT_ONCE once the
  390. * orom/efi parameter support is widely available
  391. */
  392. dev_warn(&pdev->dev,
  393. "Loading user firmware failed, using default "
  394. "values\n");
  395. dev_warn(&pdev->dev,
  396. "Default OEM configuration being used: 4 "
  397. "narrow ports, and default SAS Addresses\n");
  398. }
  399. }
  400. if (orom)
  401. dev_info(&pdev->dev,
  402. "OEM SAS parameters (version: %u.%u) loaded %s\n",
  403. (orom->hdr.version & 0xf0) >> 4,
  404. (orom->hdr.version & 0xf), source);
  405. pci_info->orom = orom;
  406. err = isci_pci_init(pdev);
  407. if (err)
  408. return err;
  409. for (i = 0; i < num_controllers(pdev); i++) {
  410. struct isci_host *h = isci_host_alloc(pdev, i);
  411. if (!h) {
  412. err = -ENOMEM;
  413. goto err_host_alloc;
  414. }
  415. pci_info->hosts[i] = h;
  416. }
  417. err = isci_setup_interrupts(pdev);
  418. if (err)
  419. goto err_host_alloc;
  420. for_each_isci_host(i, isci_host, pdev)
  421. scsi_scan_host(isci_host->shost);
  422. return 0;
  423. err_host_alloc:
  424. for_each_isci_host(i, isci_host, pdev)
  425. isci_unregister(isci_host);
  426. return err;
  427. }
  428. static void __devexit isci_pci_remove(struct pci_dev *pdev)
  429. {
  430. struct isci_host *ihost;
  431. int i;
  432. for_each_isci_host(i, ihost, pdev) {
  433. isci_unregister(ihost);
  434. isci_host_deinit(ihost);
  435. sci_controller_disable_interrupts(ihost);
  436. }
  437. }
  438. static struct pci_driver isci_pci_driver = {
  439. .name = DRV_NAME,
  440. .id_table = isci_id_table,
  441. .probe = isci_pci_probe,
  442. .remove = __devexit_p(isci_pci_remove),
  443. };
  444. static __init int isci_init(void)
  445. {
  446. int err;
  447. pr_info("%s: Intel(R) C600 SAS Controller Driver\n", DRV_NAME);
  448. isci_transport_template = sas_domain_attach_transport(&isci_transport_ops);
  449. if (!isci_transport_template)
  450. return -ENOMEM;
  451. err = pci_register_driver(&isci_pci_driver);
  452. if (err)
  453. sas_release_transport(isci_transport_template);
  454. return err;
  455. }
  456. static __exit void isci_exit(void)
  457. {
  458. pci_unregister_driver(&isci_pci_driver);
  459. sas_release_transport(isci_transport_template);
  460. }
  461. MODULE_LICENSE("Dual BSD/GPL");
  462. MODULE_FIRMWARE(ISCI_FW_NAME);
  463. module_init(isci_init);
  464. module_exit(isci_exit);