/drivers/scsi/isci/init.c

http://github.com/mirrors/linux · C · 793 lines · 533 code · 148 blank · 112 comment · 60 complexity · d7ba38eecd2184b4693205d841e59a8c MD5 · raw file

  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 <scsi/scsi_host.h>
  62. #include "host.h"
  63. #include "isci.h"
  64. #include "task.h"
  65. #include "probe_roms.h"
  66. #define MAJ 1
  67. #define MIN 2
  68. #define BUILD 0
  69. #define DRV_VERSION __stringify(MAJ) "." __stringify(MIN) "." \
  70. __stringify(BUILD)
  71. MODULE_VERSION(DRV_VERSION);
  72. static struct scsi_transport_template *isci_transport_template;
  73. static const struct pci_device_id isci_id_table[] = {
  74. { PCI_VDEVICE(INTEL, 0x1D61),},
  75. { PCI_VDEVICE(INTEL, 0x1D63),},
  76. { PCI_VDEVICE(INTEL, 0x1D65),},
  77. { PCI_VDEVICE(INTEL, 0x1D67),},
  78. { PCI_VDEVICE(INTEL, 0x1D69),},
  79. { PCI_VDEVICE(INTEL, 0x1D6B),},
  80. { PCI_VDEVICE(INTEL, 0x1D60),},
  81. { PCI_VDEVICE(INTEL, 0x1D62),},
  82. { PCI_VDEVICE(INTEL, 0x1D64),},
  83. { PCI_VDEVICE(INTEL, 0x1D66),},
  84. { PCI_VDEVICE(INTEL, 0x1D68),},
  85. { PCI_VDEVICE(INTEL, 0x1D6A),},
  86. {}
  87. };
  88. MODULE_DEVICE_TABLE(pci, isci_id_table);
  89. /* linux isci specific settings */
  90. unsigned char no_outbound_task_to = 2;
  91. module_param(no_outbound_task_to, byte, 0);
  92. MODULE_PARM_DESC(no_outbound_task_to, "No Outbound Task Timeout (1us incr)");
  93. u16 ssp_max_occ_to = 20;
  94. module_param(ssp_max_occ_to, ushort, 0);
  95. MODULE_PARM_DESC(ssp_max_occ_to, "SSP Max occupancy timeout (100us incr)");
  96. u16 stp_max_occ_to = 5;
  97. module_param(stp_max_occ_to, ushort, 0);
  98. MODULE_PARM_DESC(stp_max_occ_to, "STP Max occupancy timeout (100us incr)");
  99. u16 ssp_inactive_to = 5;
  100. module_param(ssp_inactive_to, ushort, 0);
  101. MODULE_PARM_DESC(ssp_inactive_to, "SSP inactivity timeout (100us incr)");
  102. u16 stp_inactive_to = 5;
  103. module_param(stp_inactive_to, ushort, 0);
  104. MODULE_PARM_DESC(stp_inactive_to, "STP inactivity timeout (100us incr)");
  105. unsigned char phy_gen = SCIC_SDS_PARM_GEN2_SPEED;
  106. module_param(phy_gen, byte, 0);
  107. MODULE_PARM_DESC(phy_gen, "PHY generation (1: 1.5Gbps 2: 3.0Gbps 3: 6.0Gbps)");
  108. unsigned char max_concurr_spinup;
  109. module_param(max_concurr_spinup, byte, 0);
  110. MODULE_PARM_DESC(max_concurr_spinup, "Max concurrent device spinup");
  111. uint cable_selection_override = CABLE_OVERRIDE_DISABLED;
  112. module_param(cable_selection_override, uint, 0);
  113. MODULE_PARM_DESC(cable_selection_override,
  114. "This field indicates length of the SAS/SATA cable between "
  115. "host and device. If any bits > 15 are set (default) "
  116. "indicates \"use platform defaults\"");
  117. static ssize_t isci_show_id(struct device *dev, struct device_attribute *attr, char *buf)
  118. {
  119. struct Scsi_Host *shost = container_of(dev, typeof(*shost), shost_dev);
  120. struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
  121. struct isci_host *ihost = container_of(sas_ha, typeof(*ihost), sas_ha);
  122. return snprintf(buf, PAGE_SIZE, "%d\n", ihost->id);
  123. }
  124. static DEVICE_ATTR(isci_id, S_IRUGO, isci_show_id, NULL);
  125. struct device_attribute *isci_host_attrs[] = {
  126. &dev_attr_isci_id,
  127. NULL
  128. };
  129. static struct scsi_host_template isci_sht = {
  130. .module = THIS_MODULE,
  131. .name = DRV_NAME,
  132. .proc_name = DRV_NAME,
  133. .queuecommand = sas_queuecommand,
  134. .target_alloc = sas_target_alloc,
  135. .slave_configure = sas_slave_configure,
  136. .scan_finished = isci_host_scan_finished,
  137. .scan_start = isci_host_start,
  138. .change_queue_depth = sas_change_queue_depth,
  139. .bios_param = sas_bios_param,
  140. .can_queue = ISCI_CAN_QUEUE_VAL,
  141. .this_id = -1,
  142. .sg_tablesize = SG_ALL,
  143. .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
  144. .eh_abort_handler = sas_eh_abort_handler,
  145. .eh_device_reset_handler = sas_eh_device_reset_handler,
  146. .eh_target_reset_handler = sas_eh_target_reset_handler,
  147. .target_destroy = sas_target_destroy,
  148. .ioctl = sas_ioctl,
  149. #ifdef CONFIG_COMPAT
  150. .compat_ioctl = sas_ioctl,
  151. #endif
  152. .shost_attrs = isci_host_attrs,
  153. .track_queue_depth = 1,
  154. };
  155. static struct sas_domain_function_template isci_transport_ops = {
  156. /* The class calls these to notify the LLDD of an event. */
  157. .lldd_port_formed = isci_port_formed,
  158. .lldd_port_deformed = isci_port_deformed,
  159. /* The class calls these when a device is found or gone. */
  160. .lldd_dev_found = isci_remote_device_found,
  161. .lldd_dev_gone = isci_remote_device_gone,
  162. .lldd_execute_task = isci_task_execute_task,
  163. /* Task Management Functions. Must be called from process context. */
  164. .lldd_abort_task = isci_task_abort_task,
  165. .lldd_abort_task_set = isci_task_abort_task_set,
  166. .lldd_clear_aca = isci_task_clear_aca,
  167. .lldd_clear_task_set = isci_task_clear_task_set,
  168. .lldd_I_T_nexus_reset = isci_task_I_T_nexus_reset,
  169. .lldd_lu_reset = isci_task_lu_reset,
  170. .lldd_query_task = isci_task_query_task,
  171. /* ata recovery called from ata-eh */
  172. .lldd_ata_check_ready = isci_ata_check_ready,
  173. /* Port and Adapter management */
  174. .lldd_clear_nexus_port = isci_task_clear_nexus_port,
  175. .lldd_clear_nexus_ha = isci_task_clear_nexus_ha,
  176. /* Phy management */
  177. .lldd_control_phy = isci_phy_control,
  178. /* GPIO support */
  179. .lldd_write_gpio = isci_gpio_write,
  180. };
  181. /******************************************************************************
  182. * P R O T E C T E D M E T H O D S
  183. ******************************************************************************/
  184. /**
  185. * isci_register_sas_ha() - This method initializes various lldd
  186. * specific members of the sas_ha struct and calls the libsas
  187. * sas_register_ha() function.
  188. * @isci_host: This parameter specifies the lldd specific wrapper for the
  189. * libsas sas_ha struct.
  190. *
  191. * This method returns an error code indicating success or failure. The user
  192. * should check for possible memory allocation error return otherwise, a zero
  193. * indicates success.
  194. */
  195. static int isci_register_sas_ha(struct isci_host *isci_host)
  196. {
  197. int i;
  198. struct sas_ha_struct *sas_ha = &(isci_host->sas_ha);
  199. struct asd_sas_phy **sas_phys;
  200. struct asd_sas_port **sas_ports;
  201. sas_phys = devm_kcalloc(&isci_host->pdev->dev,
  202. SCI_MAX_PHYS, sizeof(void *),
  203. GFP_KERNEL);
  204. if (!sas_phys)
  205. return -ENOMEM;
  206. sas_ports = devm_kcalloc(&isci_host->pdev->dev,
  207. SCI_MAX_PORTS, sizeof(void *),
  208. GFP_KERNEL);
  209. if (!sas_ports)
  210. return -ENOMEM;
  211. sas_ha->sas_ha_name = DRV_NAME;
  212. sas_ha->lldd_module = THIS_MODULE;
  213. sas_ha->sas_addr = &isci_host->phys[0].sas_addr[0];
  214. for (i = 0; i < SCI_MAX_PHYS; i++) {
  215. sas_phys[i] = &isci_host->phys[i].sas_phy;
  216. sas_ports[i] = &isci_host->sas_ports[i];
  217. }
  218. sas_ha->sas_phy = sas_phys;
  219. sas_ha->sas_port = sas_ports;
  220. sas_ha->num_phys = SCI_MAX_PHYS;
  221. sas_ha->strict_wide_ports = 1;
  222. sas_register_ha(sas_ha);
  223. return 0;
  224. }
  225. static void isci_unregister(struct isci_host *isci_host)
  226. {
  227. struct Scsi_Host *shost;
  228. if (!isci_host)
  229. return;
  230. shost = to_shost(isci_host);
  231. sas_unregister_ha(&isci_host->sas_ha);
  232. sas_remove_host(shost);
  233. scsi_host_put(shost);
  234. }
  235. static int isci_pci_init(struct pci_dev *pdev)
  236. {
  237. int err, bar_num, bar_mask = 0;
  238. void __iomem * const *iomap;
  239. err = pcim_enable_device(pdev);
  240. if (err) {
  241. dev_err(&pdev->dev,
  242. "failed enable PCI device %s!\n",
  243. pci_name(pdev));
  244. return err;
  245. }
  246. for (bar_num = 0; bar_num < SCI_PCI_BAR_COUNT; bar_num++)
  247. bar_mask |= 1 << (bar_num * 2);
  248. err = pcim_iomap_regions(pdev, bar_mask, DRV_NAME);
  249. if (err)
  250. return err;
  251. iomap = pcim_iomap_table(pdev);
  252. if (!iomap)
  253. return -ENOMEM;
  254. pci_set_master(pdev);
  255. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  256. if (err)
  257. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  258. return err;
  259. }
  260. static int num_controllers(struct pci_dev *pdev)
  261. {
  262. /* bar size alone can tell us if we are running with a dual controller
  263. * part, no need to trust revision ids that might be under broken firmware
  264. * control
  265. */
  266. resource_size_t scu_bar_size = pci_resource_len(pdev, SCI_SCU_BAR*2);
  267. resource_size_t smu_bar_size = pci_resource_len(pdev, SCI_SMU_BAR*2);
  268. if (scu_bar_size >= SCI_SCU_BAR_SIZE*SCI_MAX_CONTROLLERS &&
  269. smu_bar_size >= SCI_SMU_BAR_SIZE*SCI_MAX_CONTROLLERS)
  270. return SCI_MAX_CONTROLLERS;
  271. else
  272. return 1;
  273. }
  274. static int isci_setup_interrupts(struct pci_dev *pdev)
  275. {
  276. int err, i, num_msix;
  277. struct isci_host *ihost;
  278. struct isci_pci_info *pci_info = to_pci_info(pdev);
  279. /*
  280. * Determine the number of vectors associated with this
  281. * PCI function.
  282. */
  283. num_msix = num_controllers(pdev) * SCI_NUM_MSI_X_INT;
  284. err = pci_alloc_irq_vectors(pdev, num_msix, num_msix, PCI_IRQ_MSIX);
  285. if (err < 0)
  286. goto intx;
  287. for (i = 0; i < num_msix; i++) {
  288. int id = i / SCI_NUM_MSI_X_INT;
  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, pci_irq_vector(pdev, i),
  297. isr, 0, 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. devm_free_irq(&pdev->dev, pci_irq_vector(pdev, i),
  305. ihost);
  306. }
  307. pci_free_irq_vectors(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, pci_irq_vector(pdev, 0),
  314. isci_intx_isr, IRQF_SHARED, DRV_NAME"-intx",
  315. ihost);
  316. if (err)
  317. break;
  318. }
  319. return err;
  320. }
  321. static void isci_user_parameters_get(struct sci_user_parameters *u)
  322. {
  323. int i;
  324. for (i = 0; i < SCI_MAX_PHYS; i++) {
  325. struct sci_phy_user_params *u_phy = &u->phys[i];
  326. u_phy->max_speed_generation = phy_gen;
  327. /* we are not exporting these for now */
  328. u_phy->align_insertion_frequency = 0x7f;
  329. u_phy->in_connection_align_insertion_frequency = 0xff;
  330. u_phy->notify_enable_spin_up_insertion_frequency = 0x33;
  331. }
  332. u->stp_inactivity_timeout = stp_inactive_to;
  333. u->ssp_inactivity_timeout = ssp_inactive_to;
  334. u->stp_max_occupancy_timeout = stp_max_occ_to;
  335. u->ssp_max_occupancy_timeout = ssp_max_occ_to;
  336. u->no_outbound_task_timeout = no_outbound_task_to;
  337. u->max_concurr_spinup = max_concurr_spinup;
  338. }
  339. static enum sci_status sci_user_parameters_set(struct isci_host *ihost,
  340. struct sci_user_parameters *sci_parms)
  341. {
  342. u16 index;
  343. /*
  344. * Validate the user parameters. If they are not legal, then
  345. * return a failure.
  346. */
  347. for (index = 0; index < SCI_MAX_PHYS; index++) {
  348. struct sci_phy_user_params *u;
  349. u = &sci_parms->phys[index];
  350. if (!((u->max_speed_generation <= SCIC_SDS_PARM_MAX_SPEED) &&
  351. (u->max_speed_generation > SCIC_SDS_PARM_NO_SPEED)))
  352. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  353. if ((u->in_connection_align_insertion_frequency < 3) ||
  354. (u->align_insertion_frequency == 0) ||
  355. (u->notify_enable_spin_up_insertion_frequency == 0))
  356. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  357. }
  358. if ((sci_parms->stp_inactivity_timeout == 0) ||
  359. (sci_parms->ssp_inactivity_timeout == 0) ||
  360. (sci_parms->stp_max_occupancy_timeout == 0) ||
  361. (sci_parms->ssp_max_occupancy_timeout == 0) ||
  362. (sci_parms->no_outbound_task_timeout == 0))
  363. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  364. memcpy(&ihost->user_parameters, sci_parms, sizeof(*sci_parms));
  365. return SCI_SUCCESS;
  366. }
  367. static void sci_oem_defaults(struct isci_host *ihost)
  368. {
  369. /* these defaults are overridden by the platform / firmware */
  370. struct sci_user_parameters *user = &ihost->user_parameters;
  371. struct sci_oem_params *oem = &ihost->oem_parameters;
  372. int i;
  373. /* Default to APC mode. */
  374. oem->controller.mode_type = SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE;
  375. /* Default to APC mode. */
  376. oem->controller.max_concurr_spin_up = 1;
  377. /* Default to no SSC operation. */
  378. oem->controller.do_enable_ssc = false;
  379. /* Default to short cables on all phys. */
  380. oem->controller.cable_selection_mask = 0;
  381. /* Initialize all of the port parameter information to narrow ports. */
  382. for (i = 0; i < SCI_MAX_PORTS; i++)
  383. oem->ports[i].phy_mask = 0;
  384. /* Initialize all of the phy parameter information. */
  385. for (i = 0; i < SCI_MAX_PHYS; i++) {
  386. /* Default to 3G (i.e. Gen 2). */
  387. user->phys[i].max_speed_generation = SCIC_SDS_PARM_GEN2_SPEED;
  388. /* the frequencies cannot be 0 */
  389. user->phys[i].align_insertion_frequency = 0x7f;
  390. user->phys[i].in_connection_align_insertion_frequency = 0xff;
  391. user->phys[i].notify_enable_spin_up_insertion_frequency = 0x33;
  392. /* Previous Vitesse based expanders had a arbitration issue that
  393. * is worked around by having the upper 32-bits of SAS address
  394. * with a value greater then the Vitesse company identifier.
  395. * Hence, usage of 0x5FCFFFFF.
  396. */
  397. oem->phys[i].sas_address.low = 0x1 + ihost->id;
  398. oem->phys[i].sas_address.high = 0x5FCFFFFF;
  399. }
  400. user->stp_inactivity_timeout = 5;
  401. user->ssp_inactivity_timeout = 5;
  402. user->stp_max_occupancy_timeout = 5;
  403. user->ssp_max_occupancy_timeout = 20;
  404. user->no_outbound_task_timeout = 2;
  405. }
  406. static struct isci_host *isci_host_alloc(struct pci_dev *pdev, int id)
  407. {
  408. struct isci_orom *orom = to_pci_info(pdev)->orom;
  409. struct sci_user_parameters sci_user_params;
  410. u8 oem_version = ISCI_ROM_VER_1_0;
  411. struct isci_host *ihost;
  412. struct Scsi_Host *shost;
  413. int err, i;
  414. ihost = devm_kzalloc(&pdev->dev, sizeof(*ihost), GFP_KERNEL);
  415. if (!ihost)
  416. return NULL;
  417. ihost->pdev = pdev;
  418. ihost->id = id;
  419. spin_lock_init(&ihost->scic_lock);
  420. init_waitqueue_head(&ihost->eventq);
  421. ihost->sas_ha.dev = &ihost->pdev->dev;
  422. ihost->sas_ha.lldd_ha = ihost;
  423. tasklet_init(&ihost->completion_tasklet,
  424. isci_host_completion_routine, (unsigned long)ihost);
  425. /* validate module parameters */
  426. /* TODO: kill struct sci_user_parameters and reference directly */
  427. sci_oem_defaults(ihost);
  428. isci_user_parameters_get(&sci_user_params);
  429. if (sci_user_parameters_set(ihost, &sci_user_params)) {
  430. dev_warn(&pdev->dev,
  431. "%s: sci_user_parameters_set failed\n", __func__);
  432. return NULL;
  433. }
  434. /* sanity check platform (or 'firmware') oem parameters */
  435. if (orom) {
  436. if (id < 0 || id >= SCI_MAX_CONTROLLERS || id > orom->hdr.num_elements) {
  437. dev_warn(&pdev->dev, "parsing firmware oem parameters failed\n");
  438. return NULL;
  439. }
  440. ihost->oem_parameters = orom->ctrl[id];
  441. oem_version = orom->hdr.version;
  442. }
  443. /* validate oem parameters (platform, firmware, or built-in defaults) */
  444. if (sci_oem_parameters_validate(&ihost->oem_parameters, oem_version)) {
  445. dev_warn(&pdev->dev, "oem parameter validation failed\n");
  446. return NULL;
  447. }
  448. for (i = 0; i < SCI_MAX_PORTS; i++) {
  449. struct isci_port *iport = &ihost->ports[i];
  450. INIT_LIST_HEAD(&iport->remote_dev_list);
  451. iport->isci_host = ihost;
  452. }
  453. for (i = 0; i < SCI_MAX_PHYS; i++)
  454. isci_phy_init(&ihost->phys[i], ihost, i);
  455. for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
  456. struct isci_remote_device *idev = &ihost->devices[i];
  457. INIT_LIST_HEAD(&idev->node);
  458. }
  459. shost = scsi_host_alloc(&isci_sht, sizeof(void *));
  460. if (!shost)
  461. return NULL;
  462. dev_info(&pdev->dev, "%sSCU controller %d: phy 3-0 cables: "
  463. "{%s, %s, %s, %s}\n",
  464. (is_cable_select_overridden() ? "* " : ""), ihost->id,
  465. lookup_cable_names(decode_cable_selection(ihost, 3)),
  466. lookup_cable_names(decode_cable_selection(ihost, 2)),
  467. lookup_cable_names(decode_cable_selection(ihost, 1)),
  468. lookup_cable_names(decode_cable_selection(ihost, 0)));
  469. err = isci_host_init(ihost);
  470. if (err)
  471. goto err_shost;
  472. SHOST_TO_SAS_HA(shost) = &ihost->sas_ha;
  473. ihost->sas_ha.core.shost = shost;
  474. shost->transportt = isci_transport_template;
  475. shost->max_id = ~0;
  476. shost->max_lun = ~0;
  477. shost->max_cmd_len = MAX_COMMAND_SIZE;
  478. /* turn on DIF support */
  479. scsi_host_set_prot(shost,
  480. SHOST_DIF_TYPE1_PROTECTION |
  481. SHOST_DIF_TYPE2_PROTECTION |
  482. SHOST_DIF_TYPE3_PROTECTION);
  483. scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
  484. err = scsi_add_host(shost, &pdev->dev);
  485. if (err)
  486. goto err_shost;
  487. err = isci_register_sas_ha(ihost);
  488. if (err)
  489. goto err_shost_remove;
  490. return ihost;
  491. err_shost_remove:
  492. scsi_remove_host(shost);
  493. err_shost:
  494. scsi_host_put(shost);
  495. return NULL;
  496. }
  497. static int isci_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  498. {
  499. struct isci_pci_info *pci_info;
  500. int err, i;
  501. struct isci_host *isci_host;
  502. const struct firmware *fw = NULL;
  503. struct isci_orom *orom = NULL;
  504. char *source = "(platform)";
  505. dev_info(&pdev->dev, "driver configured for rev: %d silicon\n",
  506. pdev->revision);
  507. pci_info = devm_kzalloc(&pdev->dev, sizeof(*pci_info), GFP_KERNEL);
  508. if (!pci_info)
  509. return -ENOMEM;
  510. pci_set_drvdata(pdev, pci_info);
  511. if (efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE))
  512. orom = isci_get_efi_var(pdev);
  513. if (!orom)
  514. orom = isci_request_oprom(pdev);
  515. for (i = 0; orom && i < num_controllers(pdev); i++) {
  516. if (sci_oem_parameters_validate(&orom->ctrl[i],
  517. orom->hdr.version)) {
  518. dev_warn(&pdev->dev,
  519. "[%d]: invalid oem parameters detected, falling back to firmware\n", i);
  520. orom = NULL;
  521. break;
  522. }
  523. }
  524. if (!orom) {
  525. source = "(firmware)";
  526. orom = isci_request_firmware(pdev, fw);
  527. if (!orom) {
  528. /* TODO convert this to WARN_TAINT_ONCE once the
  529. * orom/efi parameter support is widely available
  530. */
  531. dev_warn(&pdev->dev,
  532. "Loading user firmware failed, using default "
  533. "values\n");
  534. dev_warn(&pdev->dev,
  535. "Default OEM configuration being used: 4 "
  536. "narrow ports, and default SAS Addresses\n");
  537. }
  538. }
  539. if (orom)
  540. dev_info(&pdev->dev,
  541. "OEM SAS parameters (version: %u.%u) loaded %s\n",
  542. (orom->hdr.version & 0xf0) >> 4,
  543. (orom->hdr.version & 0xf), source);
  544. pci_info->orom = orom;
  545. err = isci_pci_init(pdev);
  546. if (err)
  547. return err;
  548. for (i = 0; i < num_controllers(pdev); i++) {
  549. struct isci_host *h = isci_host_alloc(pdev, i);
  550. if (!h) {
  551. err = -ENOMEM;
  552. goto err_host_alloc;
  553. }
  554. pci_info->hosts[i] = h;
  555. }
  556. err = isci_setup_interrupts(pdev);
  557. if (err)
  558. goto err_host_alloc;
  559. for_each_isci_host(i, isci_host, pdev)
  560. scsi_scan_host(to_shost(isci_host));
  561. return 0;
  562. err_host_alloc:
  563. for_each_isci_host(i, isci_host, pdev)
  564. isci_unregister(isci_host);
  565. return err;
  566. }
  567. static void isci_pci_remove(struct pci_dev *pdev)
  568. {
  569. struct isci_host *ihost;
  570. int i;
  571. for_each_isci_host(i, ihost, pdev) {
  572. wait_for_start(ihost);
  573. isci_unregister(ihost);
  574. isci_host_deinit(ihost);
  575. }
  576. }
  577. #ifdef CONFIG_PM_SLEEP
  578. static int isci_suspend(struct device *dev)
  579. {
  580. struct pci_dev *pdev = to_pci_dev(dev);
  581. struct isci_host *ihost;
  582. int i;
  583. for_each_isci_host(i, ihost, pdev) {
  584. sas_suspend_ha(&ihost->sas_ha);
  585. isci_host_deinit(ihost);
  586. }
  587. pci_save_state(pdev);
  588. pci_disable_device(pdev);
  589. pci_set_power_state(pdev, PCI_D3hot);
  590. return 0;
  591. }
  592. static int isci_resume(struct device *dev)
  593. {
  594. struct pci_dev *pdev = to_pci_dev(dev);
  595. struct isci_host *ihost;
  596. int rc, i;
  597. pci_set_power_state(pdev, PCI_D0);
  598. pci_restore_state(pdev);
  599. rc = pcim_enable_device(pdev);
  600. if (rc) {
  601. dev_err(&pdev->dev,
  602. "enabling device failure after resume(%d)\n", rc);
  603. return rc;
  604. }
  605. pci_set_master(pdev);
  606. for_each_isci_host(i, ihost, pdev) {
  607. sas_prep_resume_ha(&ihost->sas_ha);
  608. isci_host_init(ihost);
  609. isci_host_start(ihost->sas_ha.core.shost);
  610. wait_for_start(ihost);
  611. sas_resume_ha(&ihost->sas_ha);
  612. }
  613. return 0;
  614. }
  615. #endif
  616. static SIMPLE_DEV_PM_OPS(isci_pm_ops, isci_suspend, isci_resume);
  617. static struct pci_driver isci_pci_driver = {
  618. .name = DRV_NAME,
  619. .id_table = isci_id_table,
  620. .probe = isci_pci_probe,
  621. .remove = isci_pci_remove,
  622. .driver.pm = &isci_pm_ops,
  623. };
  624. static __init int isci_init(void)
  625. {
  626. int err;
  627. pr_info("%s: Intel(R) C600 SAS Controller Driver - version %s\n",
  628. DRV_NAME, DRV_VERSION);
  629. isci_transport_template = sas_domain_attach_transport(&isci_transport_ops);
  630. if (!isci_transport_template)
  631. return -ENOMEM;
  632. err = pci_register_driver(&isci_pci_driver);
  633. if (err)
  634. sas_release_transport(isci_transport_template);
  635. return err;
  636. }
  637. static __exit void isci_exit(void)
  638. {
  639. pci_unregister_driver(&isci_pci_driver);
  640. sas_release_transport(isci_transport_template);
  641. }
  642. MODULE_LICENSE("Dual BSD/GPL");
  643. MODULE_FIRMWARE(ISCI_FW_NAME);
  644. module_init(isci_init);
  645. module_exit(isci_exit);