init.c 16 KB

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