init.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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. .use_clustering = ENABLE_CLUSTERING,
  145. .eh_abort_handler = sas_eh_abort_handler,
  146. .eh_device_reset_handler = sas_eh_device_reset_handler,
  147. .eh_bus_reset_handler = sas_eh_bus_reset_handler,
  148. .target_destroy = sas_target_destroy,
  149. .ioctl = sas_ioctl,
  150. .shost_attrs = isci_host_attrs,
  151. .use_blk_tags = 1,
  152. .track_queue_depth = 1,
  153. };
  154. static struct sas_domain_function_template isci_transport_ops = {
  155. /* The class calls these to notify the LLDD of an event. */
  156. .lldd_port_formed = isci_port_formed,
  157. .lldd_port_deformed = isci_port_deformed,
  158. /* The class calls these when a device is found or gone. */
  159. .lldd_dev_found = isci_remote_device_found,
  160. .lldd_dev_gone = isci_remote_device_gone,
  161. .lldd_execute_task = isci_task_execute_task,
  162. /* Task Management Functions. Must be called from process context. */
  163. .lldd_abort_task = isci_task_abort_task,
  164. .lldd_abort_task_set = isci_task_abort_task_set,
  165. .lldd_clear_aca = isci_task_clear_aca,
  166. .lldd_clear_task_set = isci_task_clear_task_set,
  167. .lldd_I_T_nexus_reset = isci_task_I_T_nexus_reset,
  168. .lldd_lu_reset = isci_task_lu_reset,
  169. .lldd_query_task = isci_task_query_task,
  170. /* ata recovery called from ata-eh */
  171. .lldd_ata_check_ready = isci_ata_check_ready,
  172. /* Port and Adapter management */
  173. .lldd_clear_nexus_port = isci_task_clear_nexus_port,
  174. .lldd_clear_nexus_ha = isci_task_clear_nexus_ha,
  175. /* Phy management */
  176. .lldd_control_phy = isci_phy_control,
  177. /* GPIO support */
  178. .lldd_write_gpio = isci_gpio_write,
  179. };
  180. /******************************************************************************
  181. * P R O T E C T E D M E T H O D S
  182. ******************************************************************************/
  183. /**
  184. * isci_register_sas_ha() - This method initializes various lldd
  185. * specific members of the sas_ha struct and calls the libsas
  186. * sas_register_ha() function.
  187. * @isci_host: This parameter specifies the lldd specific wrapper for the
  188. * libsas sas_ha struct.
  189. *
  190. * This method returns an error code indicating success or failure. The user
  191. * should check for possible memory allocation error return otherwise, a zero
  192. * indicates success.
  193. */
  194. static int isci_register_sas_ha(struct isci_host *isci_host)
  195. {
  196. int i;
  197. struct sas_ha_struct *sas_ha = &(isci_host->sas_ha);
  198. struct asd_sas_phy **sas_phys;
  199. struct asd_sas_port **sas_ports;
  200. sas_phys = devm_kzalloc(&isci_host->pdev->dev,
  201. SCI_MAX_PHYS * sizeof(void *),
  202. GFP_KERNEL);
  203. if (!sas_phys)
  204. return -ENOMEM;
  205. sas_ports = devm_kzalloc(&isci_host->pdev->dev,
  206. SCI_MAX_PORTS * sizeof(void *),
  207. GFP_KERNEL);
  208. if (!sas_ports)
  209. return -ENOMEM;
  210. sas_ha->sas_ha_name = DRV_NAME;
  211. sas_ha->lldd_module = THIS_MODULE;
  212. sas_ha->sas_addr = &isci_host->phys[0].sas_addr[0];
  213. for (i = 0; i < SCI_MAX_PHYS; i++) {
  214. sas_phys[i] = &isci_host->phys[i].sas_phy;
  215. sas_ports[i] = &isci_host->sas_ports[i];
  216. }
  217. sas_ha->sas_phy = sas_phys;
  218. sas_ha->sas_port = sas_ports;
  219. sas_ha->num_phys = SCI_MAX_PHYS;
  220. sas_ha->strict_wide_ports = 1;
  221. sas_register_ha(sas_ha);
  222. return 0;
  223. }
  224. static void isci_unregister(struct isci_host *isci_host)
  225. {
  226. struct Scsi_Host *shost;
  227. if (!isci_host)
  228. return;
  229. sas_unregister_ha(&isci_host->sas_ha);
  230. shost = to_shost(isci_host);
  231. sas_remove_host(shost);
  232. scsi_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 = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  256. if (err) {
  257. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  258. if (err)
  259. return err;
  260. }
  261. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  262. if (err) {
  263. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  264. if (err)
  265. return err;
  266. }
  267. return 0;
  268. }
  269. static int num_controllers(struct pci_dev *pdev)
  270. {
  271. /* bar size alone can tell us if we are running with a dual controller
  272. * part, no need to trust revision ids that might be under broken firmware
  273. * control
  274. */
  275. resource_size_t scu_bar_size = pci_resource_len(pdev, SCI_SCU_BAR*2);
  276. resource_size_t smu_bar_size = pci_resource_len(pdev, SCI_SMU_BAR*2);
  277. if (scu_bar_size >= SCI_SCU_BAR_SIZE*SCI_MAX_CONTROLLERS &&
  278. smu_bar_size >= SCI_SMU_BAR_SIZE*SCI_MAX_CONTROLLERS)
  279. return SCI_MAX_CONTROLLERS;
  280. else
  281. return 1;
  282. }
  283. static int isci_setup_interrupts(struct pci_dev *pdev)
  284. {
  285. int err, i, num_msix;
  286. struct isci_host *ihost;
  287. struct isci_pci_info *pci_info = to_pci_info(pdev);
  288. /*
  289. * Determine the number of vectors associated with this
  290. * PCI function.
  291. */
  292. num_msix = num_controllers(pdev) * SCI_NUM_MSI_X_INT;
  293. for (i = 0; i < num_msix; i++)
  294. pci_info->msix_entries[i].entry = i;
  295. err = pci_enable_msix_exact(pdev, pci_info->msix_entries, num_msix);
  296. if (err)
  297. goto intx;
  298. for (i = 0; i < num_msix; i++) {
  299. int id = i / SCI_NUM_MSI_X_INT;
  300. struct msix_entry *msix = &pci_info->msix_entries[i];
  301. irq_handler_t isr;
  302. ihost = pci_info->hosts[id];
  303. /* odd numbered vectors are error interrupts */
  304. if (i & 1)
  305. isr = isci_error_isr;
  306. else
  307. isr = isci_msix_isr;
  308. err = devm_request_irq(&pdev->dev, msix->vector, isr, 0,
  309. DRV_NAME"-msix", ihost);
  310. if (!err)
  311. continue;
  312. dev_info(&pdev->dev, "msix setup failed falling back to intx\n");
  313. while (i--) {
  314. id = i / SCI_NUM_MSI_X_INT;
  315. ihost = pci_info->hosts[id];
  316. msix = &pci_info->msix_entries[i];
  317. devm_free_irq(&pdev->dev, msix->vector, ihost);
  318. }
  319. pci_disable_msix(pdev);
  320. goto intx;
  321. }
  322. return 0;
  323. intx:
  324. for_each_isci_host(i, ihost, pdev) {
  325. err = devm_request_irq(&pdev->dev, pdev->irq, isci_intx_isr,
  326. IRQF_SHARED, DRV_NAME"-intx", ihost);
  327. if (err)
  328. break;
  329. }
  330. return err;
  331. }
  332. static void isci_user_parameters_get(struct sci_user_parameters *u)
  333. {
  334. int i;
  335. for (i = 0; i < SCI_MAX_PHYS; i++) {
  336. struct sci_phy_user_params *u_phy = &u->phys[i];
  337. u_phy->max_speed_generation = phy_gen;
  338. /* we are not exporting these for now */
  339. u_phy->align_insertion_frequency = 0x7f;
  340. u_phy->in_connection_align_insertion_frequency = 0xff;
  341. u_phy->notify_enable_spin_up_insertion_frequency = 0x33;
  342. }
  343. u->stp_inactivity_timeout = stp_inactive_to;
  344. u->ssp_inactivity_timeout = ssp_inactive_to;
  345. u->stp_max_occupancy_timeout = stp_max_occ_to;
  346. u->ssp_max_occupancy_timeout = ssp_max_occ_to;
  347. u->no_outbound_task_timeout = no_outbound_task_to;
  348. u->max_concurr_spinup = max_concurr_spinup;
  349. }
  350. static enum sci_status sci_user_parameters_set(struct isci_host *ihost,
  351. struct sci_user_parameters *sci_parms)
  352. {
  353. u16 index;
  354. /*
  355. * Validate the user parameters. If they are not legal, then
  356. * return a failure.
  357. */
  358. for (index = 0; index < SCI_MAX_PHYS; index++) {
  359. struct sci_phy_user_params *u;
  360. u = &sci_parms->phys[index];
  361. if (!((u->max_speed_generation <= SCIC_SDS_PARM_MAX_SPEED) &&
  362. (u->max_speed_generation > SCIC_SDS_PARM_NO_SPEED)))
  363. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  364. if (u->in_connection_align_insertion_frequency < 3)
  365. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  366. if ((u->in_connection_align_insertion_frequency < 3) ||
  367. (u->align_insertion_frequency == 0) ||
  368. (u->notify_enable_spin_up_insertion_frequency == 0))
  369. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  370. }
  371. if ((sci_parms->stp_inactivity_timeout == 0) ||
  372. (sci_parms->ssp_inactivity_timeout == 0) ||
  373. (sci_parms->stp_max_occupancy_timeout == 0) ||
  374. (sci_parms->ssp_max_occupancy_timeout == 0) ||
  375. (sci_parms->no_outbound_task_timeout == 0))
  376. return SCI_FAILURE_INVALID_PARAMETER_VALUE;
  377. memcpy(&ihost->user_parameters, sci_parms, sizeof(*sci_parms));
  378. return SCI_SUCCESS;
  379. }
  380. static void sci_oem_defaults(struct isci_host *ihost)
  381. {
  382. /* these defaults are overridden by the platform / firmware */
  383. struct sci_user_parameters *user = &ihost->user_parameters;
  384. struct sci_oem_params *oem = &ihost->oem_parameters;
  385. int i;
  386. /* Default to APC mode. */
  387. oem->controller.mode_type = SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE;
  388. /* Default to APC mode. */
  389. oem->controller.max_concurr_spin_up = 1;
  390. /* Default to no SSC operation. */
  391. oem->controller.do_enable_ssc = false;
  392. /* Default to short cables on all phys. */
  393. oem->controller.cable_selection_mask = 0;
  394. /* Initialize all of the port parameter information to narrow ports. */
  395. for (i = 0; i < SCI_MAX_PORTS; i++)
  396. oem->ports[i].phy_mask = 0;
  397. /* Initialize all of the phy parameter information. */
  398. for (i = 0; i < SCI_MAX_PHYS; i++) {
  399. /* Default to 3G (i.e. Gen 2). */
  400. user->phys[i].max_speed_generation = SCIC_SDS_PARM_GEN2_SPEED;
  401. /* the frequencies cannot be 0 */
  402. user->phys[i].align_insertion_frequency = 0x7f;
  403. user->phys[i].in_connection_align_insertion_frequency = 0xff;
  404. user->phys[i].notify_enable_spin_up_insertion_frequency = 0x33;
  405. /* Previous Vitesse based expanders had a arbitration issue that
  406. * is worked around by having the upper 32-bits of SAS address
  407. * with a value greater then the Vitesse company identifier.
  408. * Hence, usage of 0x5FCFFFFF.
  409. */
  410. oem->phys[i].sas_address.low = 0x1 + ihost->id;
  411. oem->phys[i].sas_address.high = 0x5FCFFFFF;
  412. }
  413. user->stp_inactivity_timeout = 5;
  414. user->ssp_inactivity_timeout = 5;
  415. user->stp_max_occupancy_timeout = 5;
  416. user->ssp_max_occupancy_timeout = 20;
  417. user->no_outbound_task_timeout = 2;
  418. }
  419. static struct isci_host *isci_host_alloc(struct pci_dev *pdev, int id)
  420. {
  421. struct isci_orom *orom = to_pci_info(pdev)->orom;
  422. struct sci_user_parameters sci_user_params;
  423. u8 oem_version = ISCI_ROM_VER_1_0;
  424. struct isci_host *ihost;
  425. struct Scsi_Host *shost;
  426. int err, i;
  427. ihost = devm_kzalloc(&pdev->dev, sizeof(*ihost), GFP_KERNEL);
  428. if (!ihost)
  429. return NULL;
  430. ihost->pdev = pdev;
  431. ihost->id = id;
  432. spin_lock_init(&ihost->scic_lock);
  433. init_waitqueue_head(&ihost->eventq);
  434. ihost->sas_ha.dev = &ihost->pdev->dev;
  435. ihost->sas_ha.lldd_ha = ihost;
  436. tasklet_init(&ihost->completion_tasklet,
  437. isci_host_completion_routine, (unsigned long)ihost);
  438. /* validate module parameters */
  439. /* TODO: kill struct sci_user_parameters and reference directly */
  440. sci_oem_defaults(ihost);
  441. isci_user_parameters_get(&sci_user_params);
  442. if (sci_user_parameters_set(ihost, &sci_user_params)) {
  443. dev_warn(&pdev->dev,
  444. "%s: sci_user_parameters_set failed\n", __func__);
  445. return NULL;
  446. }
  447. /* sanity check platform (or 'firmware') oem parameters */
  448. if (orom) {
  449. if (id < 0 || id >= SCI_MAX_CONTROLLERS || id > orom->hdr.num_elements) {
  450. dev_warn(&pdev->dev, "parsing firmware oem parameters failed\n");
  451. return NULL;
  452. }
  453. ihost->oem_parameters = orom->ctrl[id];
  454. oem_version = orom->hdr.version;
  455. }
  456. /* validate oem parameters (platform, firmware, or built-in defaults) */
  457. if (sci_oem_parameters_validate(&ihost->oem_parameters, oem_version)) {
  458. dev_warn(&pdev->dev, "oem parameter validation failed\n");
  459. return NULL;
  460. }
  461. for (i = 0; i < SCI_MAX_PORTS; i++) {
  462. struct isci_port *iport = &ihost->ports[i];
  463. INIT_LIST_HEAD(&iport->remote_dev_list);
  464. iport->isci_host = ihost;
  465. }
  466. for (i = 0; i < SCI_MAX_PHYS; i++)
  467. isci_phy_init(&ihost->phys[i], ihost, i);
  468. for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
  469. struct isci_remote_device *idev = &ihost->devices[i];
  470. INIT_LIST_HEAD(&idev->node);
  471. }
  472. shost = scsi_host_alloc(&isci_sht, sizeof(void *));
  473. if (!shost)
  474. return NULL;
  475. dev_info(&pdev->dev, "%sSCU controller %d: phy 3-0 cables: "
  476. "{%s, %s, %s, %s}\n",
  477. (is_cable_select_overridden() ? "* " : ""), ihost->id,
  478. lookup_cable_names(decode_cable_selection(ihost, 3)),
  479. lookup_cable_names(decode_cable_selection(ihost, 2)),
  480. lookup_cable_names(decode_cable_selection(ihost, 1)),
  481. lookup_cable_names(decode_cable_selection(ihost, 0)));
  482. err = isci_host_init(ihost);
  483. if (err)
  484. goto err_shost;
  485. SHOST_TO_SAS_HA(shost) = &ihost->sas_ha;
  486. ihost->sas_ha.core.shost = shost;
  487. shost->transportt = isci_transport_template;
  488. shost->max_id = ~0;
  489. shost->max_lun = ~0;
  490. shost->max_cmd_len = MAX_COMMAND_SIZE;
  491. err = scsi_add_host(shost, &pdev->dev);
  492. if (err)
  493. goto err_shost;
  494. err = isci_register_sas_ha(ihost);
  495. if (err)
  496. goto err_shost_remove;
  497. return ihost;
  498. err_shost_remove:
  499. scsi_remove_host(shost);
  500. err_shost:
  501. scsi_host_put(shost);
  502. return NULL;
  503. }
  504. static int isci_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  505. {
  506. struct isci_pci_info *pci_info;
  507. int err, i;
  508. struct isci_host *isci_host;
  509. const struct firmware *fw = NULL;
  510. struct isci_orom *orom = NULL;
  511. char *source = "(platform)";
  512. dev_info(&pdev->dev, "driver configured for rev: %d silicon\n",
  513. pdev->revision);
  514. pci_info = devm_kzalloc(&pdev->dev, sizeof(*pci_info), GFP_KERNEL);
  515. if (!pci_info)
  516. return -ENOMEM;
  517. pci_set_drvdata(pdev, pci_info);
  518. if (efi_enabled(EFI_RUNTIME_SERVICES))
  519. orom = isci_get_efi_var(pdev);
  520. if (!orom)
  521. orom = isci_request_oprom(pdev);
  522. for (i = 0; orom && i < num_controllers(pdev); i++) {
  523. if (sci_oem_parameters_validate(&orom->ctrl[i],
  524. orom->hdr.version)) {
  525. dev_warn(&pdev->dev,
  526. "[%d]: invalid oem parameters detected, falling back to firmware\n", i);
  527. orom = NULL;
  528. break;
  529. }
  530. }
  531. if (!orom) {
  532. source = "(firmware)";
  533. orom = isci_request_firmware(pdev, fw);
  534. if (!orom) {
  535. /* TODO convert this to WARN_TAINT_ONCE once the
  536. * orom/efi parameter support is widely available
  537. */
  538. dev_warn(&pdev->dev,
  539. "Loading user firmware failed, using default "
  540. "values\n");
  541. dev_warn(&pdev->dev,
  542. "Default OEM configuration being used: 4 "
  543. "narrow ports, and default SAS Addresses\n");
  544. }
  545. }
  546. if (orom)
  547. dev_info(&pdev->dev,
  548. "OEM SAS parameters (version: %u.%u) loaded %s\n",
  549. (orom->hdr.version & 0xf0) >> 4,
  550. (orom->hdr.version & 0xf), source);
  551. pci_info->orom = orom;
  552. err = isci_pci_init(pdev);
  553. if (err)
  554. return err;
  555. for (i = 0; i < num_controllers(pdev); i++) {
  556. struct isci_host *h = isci_host_alloc(pdev, i);
  557. if (!h) {
  558. err = -ENOMEM;
  559. goto err_host_alloc;
  560. }
  561. pci_info->hosts[i] = h;
  562. /* turn on DIF support */
  563. scsi_host_set_prot(to_shost(h),
  564. SHOST_DIF_TYPE1_PROTECTION |
  565. SHOST_DIF_TYPE2_PROTECTION |
  566. SHOST_DIF_TYPE3_PROTECTION);
  567. scsi_host_set_guard(to_shost(h), SHOST_DIX_GUARD_CRC);
  568. }
  569. err = isci_setup_interrupts(pdev);
  570. if (err)
  571. goto err_host_alloc;
  572. for_each_isci_host(i, isci_host, pdev)
  573. scsi_scan_host(to_shost(isci_host));
  574. return 0;
  575. err_host_alloc:
  576. for_each_isci_host(i, isci_host, pdev)
  577. isci_unregister(isci_host);
  578. return err;
  579. }
  580. static void isci_pci_remove(struct pci_dev *pdev)
  581. {
  582. struct isci_host *ihost;
  583. int i;
  584. for_each_isci_host(i, ihost, pdev) {
  585. wait_for_start(ihost);
  586. isci_unregister(ihost);
  587. isci_host_deinit(ihost);
  588. }
  589. }
  590. #ifdef CONFIG_PM_SLEEP
  591. static int isci_suspend(struct device *dev)
  592. {
  593. struct pci_dev *pdev = to_pci_dev(dev);
  594. struct isci_host *ihost;
  595. int i;
  596. for_each_isci_host(i, ihost, pdev) {
  597. sas_suspend_ha(&ihost->sas_ha);
  598. isci_host_deinit(ihost);
  599. }
  600. pci_save_state(pdev);
  601. pci_disable_device(pdev);
  602. pci_set_power_state(pdev, PCI_D3hot);
  603. return 0;
  604. }
  605. static int isci_resume(struct device *dev)
  606. {
  607. struct pci_dev *pdev = to_pci_dev(dev);
  608. struct isci_host *ihost;
  609. int rc, i;
  610. pci_set_power_state(pdev, PCI_D0);
  611. pci_restore_state(pdev);
  612. rc = pcim_enable_device(pdev);
  613. if (rc) {
  614. dev_err(&pdev->dev,
  615. "enabling device failure after resume(%d)\n", rc);
  616. return rc;
  617. }
  618. pci_set_master(pdev);
  619. for_each_isci_host(i, ihost, pdev) {
  620. sas_prep_resume_ha(&ihost->sas_ha);
  621. isci_host_init(ihost);
  622. isci_host_start(ihost->sas_ha.core.shost);
  623. wait_for_start(ihost);
  624. sas_resume_ha(&ihost->sas_ha);
  625. }
  626. return 0;
  627. }
  628. #endif
  629. static SIMPLE_DEV_PM_OPS(isci_pm_ops, isci_suspend, isci_resume);
  630. static struct pci_driver isci_pci_driver = {
  631. .name = DRV_NAME,
  632. .id_table = isci_id_table,
  633. .probe = isci_pci_probe,
  634. .remove = isci_pci_remove,
  635. .driver.pm = &isci_pm_ops,
  636. };
  637. static __init int isci_init(void)
  638. {
  639. int err;
  640. pr_info("%s: Intel(R) C600 SAS Controller Driver - version %s\n",
  641. DRV_NAME, DRV_VERSION);
  642. isci_transport_template = sas_domain_attach_transport(&isci_transport_ops);
  643. if (!isci_transport_template)
  644. return -ENOMEM;
  645. err = pci_register_driver(&isci_pci_driver);
  646. if (err)
  647. sas_release_transport(isci_transport_template);
  648. return err;
  649. }
  650. static __exit void isci_exit(void)
  651. {
  652. pci_unregister_driver(&isci_pci_driver);
  653. sas_release_transport(isci_transport_template);
  654. }
  655. MODULE_LICENSE("Dual BSD/GPL");
  656. MODULE_FIRMWARE(ISCI_FW_NAME);
  657. module_init(isci_init);
  658. module_exit(isci_exit);