init.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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 "sci_controller_constants.h"
  64. #include "scic_remote_device.h"
  65. #include "sci_environment.h"
  66. #include "probe_roms.h"
  67. static struct scsi_transport_template *isci_transport_template;
  68. static DEFINE_PCI_DEVICE_TABLE(isci_id_table) = {
  69. { PCI_VDEVICE(INTEL, 0x1D61),},
  70. { PCI_VDEVICE(INTEL, 0x1D63),},
  71. { PCI_VDEVICE(INTEL, 0x1D65),},
  72. { PCI_VDEVICE(INTEL, 0x1D67),},
  73. { PCI_VDEVICE(INTEL, 0x1D69),},
  74. { PCI_VDEVICE(INTEL, 0x1D6B),},
  75. { PCI_VDEVICE(INTEL, 0x1D60),},
  76. { PCI_VDEVICE(INTEL, 0x1D62),},
  77. { PCI_VDEVICE(INTEL, 0x1D64),},
  78. { PCI_VDEVICE(INTEL, 0x1D66),},
  79. { PCI_VDEVICE(INTEL, 0x1D68),},
  80. { PCI_VDEVICE(INTEL, 0x1D6A),},
  81. {}
  82. };
  83. struct isci_firmware *isci_firmware;
  84. static int __devinit isci_pci_probe(
  85. struct pci_dev *pdev,
  86. const struct pci_device_id *device_id_p);
  87. static void __devexit isci_pci_remove(struct pci_dev *pdev);
  88. MODULE_DEVICE_TABLE(pci, isci_id_table);
  89. static struct pci_driver isci_pci_driver = {
  90. .name = DRV_NAME,
  91. .id_table = isci_id_table,
  92. .probe = isci_pci_probe,
  93. .remove = __devexit_p(isci_pci_remove),
  94. };
  95. /* linux isci specific settings */
  96. #if defined(CONFIG_PBG_HBA_A0)
  97. int isci_si_rev = ISCI_SI_REVA0;
  98. #elif defined(CONFIG_PBG_HBA_A2)
  99. int isci_si_rev = ISCI_SI_REVA2;
  100. #else
  101. int isci_si_rev = ISCI_SI_REVB0;
  102. #endif
  103. module_param(isci_si_rev, int, S_IRUGO | S_IWUSR);
  104. MODULE_PARM_DESC(isci_si_rev, "override default si rev (0: A0 1: A2 2: B0)");
  105. static struct scsi_host_template isci_sht = {
  106. .module = THIS_MODULE,
  107. .name = DRV_NAME,
  108. .proc_name = DRV_NAME,
  109. .queuecommand = sas_queuecommand,
  110. .target_alloc = sas_target_alloc,
  111. .slave_configure = sas_slave_configure,
  112. .slave_destroy = sas_slave_destroy,
  113. .scan_finished = isci_host_scan_finished,
  114. .scan_start = isci_host_scan_start,
  115. .change_queue_depth = sas_change_queue_depth,
  116. .change_queue_type = sas_change_queue_type,
  117. .bios_param = sas_bios_param,
  118. .can_queue = ISCI_CAN_QUEUE_VAL,
  119. .cmd_per_lun = 1,
  120. .this_id = -1,
  121. .sg_tablesize = SG_ALL,
  122. .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
  123. .use_clustering = ENABLE_CLUSTERING,
  124. .eh_device_reset_handler = sas_eh_device_reset_handler,
  125. .eh_bus_reset_handler = isci_bus_reset_handler,
  126. .slave_alloc = sas_slave_alloc,
  127. .target_destroy = sas_target_destroy,
  128. .ioctl = sas_ioctl,
  129. };
  130. static struct sas_domain_function_template isci_transport_ops = {
  131. /* The class calls these to notify the LLDD of an event. */
  132. .lldd_port_formed = isci_port_formed,
  133. .lldd_port_deformed = isci_port_deformed,
  134. /* The class calls these when a device is found or gone. */
  135. .lldd_dev_found = isci_remote_device_found,
  136. .lldd_dev_gone = isci_remote_device_gone,
  137. .lldd_execute_task = isci_task_execute_task,
  138. /* Task Management Functions. Must be called from process context. */
  139. .lldd_abort_task = isci_task_abort_task,
  140. .lldd_abort_task_set = isci_task_abort_task_set,
  141. .lldd_clear_aca = isci_task_clear_aca,
  142. .lldd_clear_task_set = isci_task_clear_task_set,
  143. .lldd_I_T_nexus_reset = isci_task_I_T_nexus_reset,
  144. .lldd_lu_reset = isci_task_lu_reset,
  145. .lldd_query_task = isci_task_query_task,
  146. /* Port and Adapter management */
  147. .lldd_clear_nexus_port = isci_task_clear_nexus_port,
  148. .lldd_clear_nexus_ha = isci_task_clear_nexus_ha,
  149. /* Phy management */
  150. .lldd_control_phy = isci_phy_control,
  151. };
  152. /******************************************************************************
  153. * P R O T E C T E D M E T H O D S
  154. ******************************************************************************/
  155. /**
  156. * isci_register_sas_ha() - This method initializes various lldd
  157. * specific members of the sas_ha struct and calls the libsas
  158. * sas_register_ha() function.
  159. * @isci_host: This parameter specifies the lldd specific wrapper for the
  160. * libsas sas_ha struct.
  161. *
  162. * This method returns an error code indicating sucess or failure. The user
  163. * should check for possible memory allocation error return otherwise, a zero
  164. * indicates success.
  165. */
  166. static int isci_register_sas_ha(struct isci_host *isci_host)
  167. {
  168. int i;
  169. struct sas_ha_struct *sas_ha = &(isci_host->sas_ha);
  170. struct asd_sas_phy **sas_phys;
  171. struct asd_sas_port **sas_ports;
  172. sas_phys = devm_kzalloc(&isci_host->pdev->dev,
  173. SCI_MAX_PHYS * sizeof(void *),
  174. GFP_KERNEL);
  175. if (!sas_phys)
  176. return -ENOMEM;
  177. sas_ports = devm_kzalloc(&isci_host->pdev->dev,
  178. SCI_MAX_PORTS * sizeof(void *),
  179. GFP_KERNEL);
  180. if (!sas_ports)
  181. return -ENOMEM;
  182. /*----------------- Libsas Initialization Stuff----------------------
  183. * Set various fields in the sas_ha struct:
  184. */
  185. sas_ha->sas_ha_name = DRV_NAME;
  186. sas_ha->lldd_module = THIS_MODULE;
  187. sas_ha->sas_addr = &isci_host->phys[0].sas_addr[0];
  188. /* set the array of phy and port structs. */
  189. for (i = 0; i < SCI_MAX_PHYS; i++) {
  190. sas_phys[i] = &(isci_host->phys[i].sas_phy);
  191. sas_ports[i] = &(isci_host->sas_ports[i]);
  192. }
  193. sas_ha->sas_phy = sas_phys;
  194. sas_ha->sas_port = sas_ports;
  195. sas_ha->num_phys = SCI_MAX_PHYS;
  196. sas_ha->lldd_queue_size = ISCI_CAN_QUEUE_VAL;
  197. sas_ha->lldd_max_execute_num = 1;
  198. sas_ha->strict_wide_ports = 1;
  199. sas_register_ha(sas_ha);
  200. return 0;
  201. }
  202. static ssize_t isci_show_id(struct device *dev, struct device_attribute *attr, char *buf)
  203. {
  204. struct Scsi_Host *shost = container_of(dev, typeof(*shost), shost_dev);
  205. struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
  206. struct isci_host *ihost = container_of(sas_ha, typeof(*ihost), sas_ha);
  207. return snprintf(buf, PAGE_SIZE, "%d\n", ihost->id);
  208. }
  209. static DEVICE_ATTR(isci_id, S_IRUGO, isci_show_id, NULL);
  210. static void isci_unregister(struct isci_host *isci_host)
  211. {
  212. struct Scsi_Host *shost;
  213. if (!isci_host)
  214. return;
  215. shost = isci_host->shost;
  216. device_remove_file(&shost->shost_dev, &dev_attr_isci_id);
  217. sas_unregister_ha(&isci_host->sas_ha);
  218. sas_remove_host(isci_host->shost);
  219. scsi_remove_host(isci_host->shost);
  220. scsi_host_put(isci_host->shost);
  221. }
  222. static int __devinit isci_pci_init(struct pci_dev *pdev)
  223. {
  224. int err, bar_num, bar_mask;
  225. void __iomem * const *iomap;
  226. err = pcim_enable_device(pdev);
  227. if (err) {
  228. dev_err(&pdev->dev,
  229. "failed enable PCI device %s!\n",
  230. pci_name(pdev));
  231. return err;
  232. }
  233. for (bar_num = 0; bar_num < SCI_PCI_BAR_COUNT; bar_num++)
  234. bar_mask |= 1 << (bar_num * 2);
  235. err = pcim_iomap_regions(pdev, bar_mask, DRV_NAME);
  236. if (err)
  237. return err;
  238. iomap = pcim_iomap_table(pdev);
  239. if (!iomap)
  240. return -ENOMEM;
  241. pci_set_master(pdev);
  242. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  243. if (err) {
  244. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  245. if (err)
  246. return err;
  247. }
  248. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  249. if (err) {
  250. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  251. if (err)
  252. return err;
  253. }
  254. return 0;
  255. }
  256. static int num_controllers(struct pci_dev *pdev)
  257. {
  258. /* bar size alone can tell us if we are running with a dual controller
  259. * part, no need to trust revision ids that might be under broken firmware
  260. * control
  261. */
  262. resource_size_t scu_bar_size = pci_resource_len(pdev, SCI_SCU_BAR*2);
  263. resource_size_t smu_bar_size = pci_resource_len(pdev, SCI_SMU_BAR*2);
  264. if (scu_bar_size >= SCI_SCU_BAR_SIZE*SCI_MAX_CONTROLLERS &&
  265. smu_bar_size >= SCI_SMU_BAR_SIZE*SCI_MAX_CONTROLLERS)
  266. return SCI_MAX_CONTROLLERS;
  267. else
  268. return 1;
  269. }
  270. static int isci_setup_interrupts(struct pci_dev *pdev)
  271. {
  272. int err, i, num_msix;
  273. struct isci_pci_info *pci_info = to_pci_info(pdev);
  274. /*
  275. * Determine the number of vectors associated with this
  276. * PCI function.
  277. */
  278. num_msix = num_controllers(pdev) * SCI_NUM_MSI_X_INT;
  279. for (i = 0; i < num_msix; i++)
  280. pci_info->msix_entries[i].entry = i;
  281. err = pci_enable_msix(pdev, pci_info->msix_entries, num_msix);
  282. if (err)
  283. goto intx;
  284. for (i = 0; i < num_msix; i++) {
  285. int id = i / SCI_NUM_MSI_X_INT;
  286. struct msix_entry *msix = &pci_info->msix_entries[i];
  287. struct isci_host *isci_host = pci_info->hosts[id];
  288. irq_handler_t isr;
  289. /* odd numbered vectors are error interrupts */
  290. if (i & 1)
  291. isr = isci_error_isr;
  292. else
  293. isr = isci_msix_isr;
  294. BUG_ON(!isci_host);
  295. err = devm_request_irq(&pdev->dev, msix->vector, isr, 0,
  296. DRV_NAME"-msix", isci_host);
  297. if (!err)
  298. continue;
  299. dev_info(&pdev->dev, "msix setup failed falling back to intx\n");
  300. while (i--) {
  301. id = i / SCI_NUM_MSI_X_INT;
  302. isci_host = pci_info->hosts[id];
  303. msix = &pci_info->msix_entries[i];
  304. devm_free_irq(&pdev->dev, msix->vector, isci_host);
  305. }
  306. pci_disable_msix(pdev);
  307. goto intx;
  308. }
  309. return 0;
  310. intx:
  311. err = devm_request_irq(&pdev->dev, pdev->irq, isci_intx_isr,
  312. IRQF_SHARED, DRV_NAME"-intx", pdev);
  313. return err;
  314. }
  315. static struct isci_host *isci_host_alloc(struct pci_dev *pdev, int id)
  316. {
  317. struct isci_host *isci_host;
  318. struct Scsi_Host *shost;
  319. int err;
  320. isci_host = devm_kzalloc(&pdev->dev, sizeof(*isci_host) +
  321. SCI_MAX_REMOTE_DEVICES *
  322. (sizeof(struct isci_remote_device) +
  323. scic_remote_device_get_object_size()), GFP_KERNEL);
  324. if (!isci_host)
  325. return NULL;
  326. isci_host->pdev = pdev;
  327. isci_host->id = id;
  328. shost = scsi_host_alloc(&isci_sht, sizeof(void *));
  329. if (!shost)
  330. return NULL;
  331. isci_host->shost = shost;
  332. err = isci_host_init(isci_host);
  333. if (err)
  334. goto err_shost;
  335. SHOST_TO_SAS_HA(shost) = &isci_host->sas_ha;
  336. isci_host->sas_ha.core.shost = shost;
  337. shost->transportt = isci_transport_template;
  338. shost->max_id = ~0;
  339. shost->max_lun = ~0;
  340. shost->max_cmd_len = MAX_COMMAND_SIZE;
  341. err = scsi_add_host(shost, &pdev->dev);
  342. if (err)
  343. goto err_shost;
  344. err = isci_register_sas_ha(isci_host);
  345. if (err)
  346. goto err_shost_remove;
  347. err = device_create_file(&shost->shost_dev, &dev_attr_isci_id);
  348. if (err)
  349. goto err_unregister_ha;
  350. return isci_host;
  351. err_unregister_ha:
  352. sas_unregister_ha(&(isci_host->sas_ha));
  353. err_shost_remove:
  354. scsi_remove_host(shost);
  355. err_shost:
  356. scsi_host_put(shost);
  357. return NULL;
  358. }
  359. static void check_si_rev(struct pci_dev *pdev)
  360. {
  361. if (num_controllers(pdev) > 1)
  362. isci_si_rev = ISCI_SI_REVB0;
  363. else {
  364. switch (pdev->revision) {
  365. case 0:
  366. case 1:
  367. /* if the id is ambiguous don't update isci_si_rev */
  368. break;
  369. case 3:
  370. isci_si_rev = ISCI_SI_REVA2;
  371. break;
  372. default:
  373. case 4:
  374. isci_si_rev = ISCI_SI_REVB0;
  375. break;
  376. }
  377. }
  378. dev_info(&pdev->dev, "driver configured for %s silicon (rev: %d)\n",
  379. isci_si_rev == ISCI_SI_REVA0 ? "A0" :
  380. isci_si_rev == ISCI_SI_REVA2 ? "A2" : "B0", pdev->revision);
  381. }
  382. static int __devinit isci_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  383. {
  384. struct isci_pci_info *pci_info;
  385. int err, i;
  386. struct isci_host *isci_host;
  387. const struct firmware *fw = NULL;
  388. struct isci_orom *orom;
  389. char *source = "(platform)";
  390. check_si_rev(pdev);
  391. pci_info = devm_kzalloc(&pdev->dev, sizeof(*pci_info), GFP_KERNEL);
  392. if (!pci_info)
  393. return -ENOMEM;
  394. pci_set_drvdata(pdev, pci_info);
  395. if (efi_enabled)
  396. orom = isci_get_efi_var(pdev);
  397. else
  398. orom = isci_request_oprom(pdev);
  399. if (!orom) {
  400. source = "(firmware)";
  401. orom = isci_request_firmware(pdev, fw);
  402. if (!orom) {
  403. /* TODO convert this to WARN_TAINT_ONCE once the
  404. * orom/efi parameter support is widely available
  405. */
  406. dev_warn(&pdev->dev,
  407. "Loading user firmware failed, using default "
  408. "values\n");
  409. dev_warn(&pdev->dev,
  410. "Default OEM configuration being used: 4 "
  411. "narrow ports, and default SAS Addresses\n");
  412. }
  413. }
  414. if (orom)
  415. dev_info(&pdev->dev,
  416. "OEM SAS parameters (version: %u.%u) loaded %s\n",
  417. (orom->hdr.version & 0xf0) >> 4,
  418. (orom->hdr.version & 0xf), source);
  419. pci_info->orom = orom;
  420. err = isci_pci_init(pdev);
  421. if (err)
  422. return err;
  423. for (i = 0; i < num_controllers(pdev); i++) {
  424. struct isci_host *h = isci_host_alloc(pdev, i);
  425. if (!h) {
  426. err = -ENOMEM;
  427. goto err_host_alloc;
  428. }
  429. pci_info->hosts[i] = h;
  430. }
  431. err = isci_setup_interrupts(pdev);
  432. if (err)
  433. goto err_host_alloc;
  434. for_each_isci_host(i, isci_host, pdev)
  435. scsi_scan_host(isci_host->shost);
  436. return 0;
  437. err_host_alloc:
  438. for_each_isci_host(i, isci_host, pdev)
  439. isci_unregister(isci_host);
  440. return err;
  441. }
  442. static void __devexit isci_pci_remove(struct pci_dev *pdev)
  443. {
  444. struct isci_host *isci_host;
  445. int i;
  446. for_each_isci_host(i, isci_host, pdev) {
  447. isci_unregister(isci_host);
  448. isci_host_deinit(isci_host);
  449. scic_controller_disable_interrupts(isci_host->core_controller);
  450. }
  451. }
  452. static __init int isci_init(void)
  453. {
  454. int err;
  455. pr_info("%s: Intel(R) C600 SAS Controller Driver\n", DRV_NAME);
  456. isci_transport_template = sas_domain_attach_transport(&isci_transport_ops);
  457. if (!isci_transport_template)
  458. return -ENOMEM;
  459. err = pci_register_driver(&isci_pci_driver);
  460. if (err)
  461. sas_release_transport(isci_transport_template);
  462. return err;
  463. }
  464. static __exit void isci_exit(void)
  465. {
  466. pci_unregister_driver(&isci_pci_driver);
  467. sas_release_transport(isci_transport_template);
  468. }
  469. MODULE_LICENSE("Dual BSD/GPL");
  470. MODULE_FIRMWARE(ISCI_FW_NAME);
  471. module_init(isci_init);
  472. module_exit(isci_exit);