host.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 "isci.h"
  56. #include "scic_io_request.h"
  57. #include "scic_remote_device.h"
  58. #include "scic_port.h"
  59. #include "port.h"
  60. #include "request.h"
  61. #include "host.h"
  62. #include "probe_roms.h"
  63. #include "core/scic_sds_controller.h"
  64. irqreturn_t isci_msix_isr(int vec, void *data)
  65. {
  66. struct isci_host *ihost = data;
  67. struct scic_sds_controller *scic = ihost->core_controller;
  68. if (scic_sds_controller_isr(scic))
  69. tasklet_schedule(&ihost->completion_tasklet);
  70. return IRQ_HANDLED;
  71. }
  72. irqreturn_t isci_intx_isr(int vec, void *data)
  73. {
  74. struct pci_dev *pdev = data;
  75. struct isci_host *ihost;
  76. irqreturn_t ret = IRQ_NONE;
  77. int i;
  78. for_each_isci_host(i, ihost, pdev) {
  79. struct scic_sds_controller *scic = ihost->core_controller;
  80. if (scic_sds_controller_isr(scic)) {
  81. tasklet_schedule(&ihost->completion_tasklet);
  82. ret = IRQ_HANDLED;
  83. } else if (scic_sds_controller_error_isr(scic)) {
  84. spin_lock(&ihost->scic_lock);
  85. scic_sds_controller_error_handler(scic);
  86. spin_unlock(&ihost->scic_lock);
  87. ret = IRQ_HANDLED;
  88. }
  89. }
  90. return ret;
  91. }
  92. irqreturn_t isci_error_isr(int vec, void *data)
  93. {
  94. struct isci_host *ihost = data;
  95. struct scic_sds_controller *scic = ihost->core_controller;
  96. if (scic_sds_controller_error_isr(scic))
  97. scic_sds_controller_error_handler(scic);
  98. return IRQ_HANDLED;
  99. }
  100. /**
  101. * isci_host_start_complete() - This function is called by the core library,
  102. * through the ISCI Module, to indicate controller start status.
  103. * @isci_host: This parameter specifies the ISCI host object
  104. * @completion_status: This parameter specifies the completion status from the
  105. * core library.
  106. *
  107. */
  108. void isci_host_start_complete(struct isci_host *ihost, enum sci_status completion_status)
  109. {
  110. if (completion_status != SCI_SUCCESS)
  111. dev_info(&ihost->pdev->dev,
  112. "controller start timed out, continuing...\n");
  113. isci_host_change_state(ihost, isci_ready);
  114. clear_bit(IHOST_START_PENDING, &ihost->flags);
  115. wake_up(&ihost->eventq);
  116. }
  117. int isci_host_scan_finished(struct Scsi_Host *shost, unsigned long time)
  118. {
  119. struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
  120. if (test_bit(IHOST_START_PENDING, &ihost->flags))
  121. return 0;
  122. /* todo: use sas_flush_discovery once it is upstream */
  123. scsi_flush_work(shost);
  124. scsi_flush_work(shost);
  125. dev_dbg(&ihost->pdev->dev,
  126. "%s: ihost->status = %d, time = %ld\n",
  127. __func__, isci_host_get_state(ihost), time);
  128. return 1;
  129. }
  130. void isci_host_scan_start(struct Scsi_Host *shost)
  131. {
  132. struct isci_host *ihost = SHOST_TO_SAS_HA(shost)->lldd_ha;
  133. struct scic_sds_controller *scic = ihost->core_controller;
  134. unsigned long tmo = scic_controller_get_suggested_start_timeout(scic);
  135. set_bit(IHOST_START_PENDING, &ihost->flags);
  136. spin_lock_irq(&ihost->scic_lock);
  137. scic_controller_start(scic, tmo);
  138. scic_controller_enable_interrupts(scic);
  139. spin_unlock_irq(&ihost->scic_lock);
  140. }
  141. void isci_host_stop_complete(struct isci_host *ihost, enum sci_status completion_status)
  142. {
  143. isci_host_change_state(ihost, isci_stopped);
  144. scic_controller_disable_interrupts(ihost->core_controller);
  145. clear_bit(IHOST_STOP_PENDING, &ihost->flags);
  146. wake_up(&ihost->eventq);
  147. }
  148. /**
  149. * isci_host_completion_routine() - This function is the delayed service
  150. * routine that calls the sci core library's completion handler. It's
  151. * scheduled as a tasklet from the interrupt service routine when interrupts
  152. * in use, or set as the timeout function in polled mode.
  153. * @data: This parameter specifies the ISCI host object
  154. *
  155. */
  156. static void isci_host_completion_routine(unsigned long data)
  157. {
  158. struct isci_host *isci_host = (struct isci_host *)data;
  159. struct list_head completed_request_list;
  160. struct list_head errored_request_list;
  161. struct list_head *current_position;
  162. struct list_head *next_position;
  163. struct isci_request *request;
  164. struct isci_request *next_request;
  165. struct sas_task *task;
  166. INIT_LIST_HEAD(&completed_request_list);
  167. INIT_LIST_HEAD(&errored_request_list);
  168. spin_lock_irq(&isci_host->scic_lock);
  169. scic_sds_controller_completion_handler(isci_host->core_controller);
  170. /* Take the lists of completed I/Os from the host. */
  171. list_splice_init(&isci_host->requests_to_complete,
  172. &completed_request_list);
  173. /* Take the list of errored I/Os from the host. */
  174. list_splice_init(&isci_host->requests_to_errorback,
  175. &errored_request_list);
  176. spin_unlock_irq(&isci_host->scic_lock);
  177. /* Process any completions in the lists. */
  178. list_for_each_safe(current_position, next_position,
  179. &completed_request_list) {
  180. request = list_entry(current_position, struct isci_request,
  181. completed_node);
  182. task = isci_request_access_task(request);
  183. /* Normal notification (task_done) */
  184. dev_dbg(&isci_host->pdev->dev,
  185. "%s: Normal - request/task = %p/%p\n",
  186. __func__,
  187. request,
  188. task);
  189. /* Return the task to libsas */
  190. if (task != NULL) {
  191. task->lldd_task = NULL;
  192. if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  193. /* If the task is already in the abort path,
  194. * the task_done callback cannot be called.
  195. */
  196. task->task_done(task);
  197. }
  198. }
  199. /* Free the request object. */
  200. isci_request_free(isci_host, request);
  201. }
  202. list_for_each_entry_safe(request, next_request, &errored_request_list,
  203. completed_node) {
  204. task = isci_request_access_task(request);
  205. /* Use sas_task_abort */
  206. dev_warn(&isci_host->pdev->dev,
  207. "%s: Error - request/task = %p/%p\n",
  208. __func__,
  209. request,
  210. task);
  211. if (task != NULL) {
  212. /* Put the task into the abort path if it's not there
  213. * already.
  214. */
  215. if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED))
  216. sas_task_abort(task);
  217. } else {
  218. /* This is a case where the request has completed with a
  219. * status such that it needed further target servicing,
  220. * but the sas_task reference has already been removed
  221. * from the request. Since it was errored, it was not
  222. * being aborted, so there is nothing to do except free
  223. * it.
  224. */
  225. spin_lock_irq(&isci_host->scic_lock);
  226. /* Remove the request from the remote device's list
  227. * of pending requests.
  228. */
  229. list_del_init(&request->dev_node);
  230. spin_unlock_irq(&isci_host->scic_lock);
  231. /* Free the request object. */
  232. isci_request_free(isci_host, request);
  233. }
  234. }
  235. }
  236. void isci_host_deinit(struct isci_host *ihost)
  237. {
  238. struct scic_sds_controller *scic = ihost->core_controller;
  239. int i;
  240. isci_host_change_state(ihost, isci_stopping);
  241. for (i = 0; i < SCI_MAX_PORTS; i++) {
  242. struct isci_port *port = &ihost->isci_ports[i];
  243. struct isci_remote_device *idev, *d;
  244. list_for_each_entry_safe(idev, d, &port->remote_dev_list, node) {
  245. isci_remote_device_change_state(idev, isci_stopping);
  246. isci_remote_device_stop(ihost, idev);
  247. }
  248. }
  249. set_bit(IHOST_STOP_PENDING, &ihost->flags);
  250. spin_lock_irq(&ihost->scic_lock);
  251. scic_controller_stop(scic, SCIC_CONTROLLER_STOP_TIMEOUT);
  252. spin_unlock_irq(&ihost->scic_lock);
  253. wait_for_stop(ihost);
  254. scic_controller_reset(scic);
  255. isci_timer_list_destroy(ihost);
  256. }
  257. static void __iomem *scu_base(struct isci_host *isci_host)
  258. {
  259. struct pci_dev *pdev = isci_host->pdev;
  260. int id = isci_host->id;
  261. return pcim_iomap_table(pdev)[SCI_SCU_BAR * 2] + SCI_SCU_BAR_SIZE * id;
  262. }
  263. static void __iomem *smu_base(struct isci_host *isci_host)
  264. {
  265. struct pci_dev *pdev = isci_host->pdev;
  266. int id = isci_host->id;
  267. return pcim_iomap_table(pdev)[SCI_SMU_BAR * 2] + SCI_SMU_BAR_SIZE * id;
  268. }
  269. static void isci_user_parameters_get(
  270. struct isci_host *isci_host,
  271. union scic_user_parameters *scic_user_params)
  272. {
  273. struct scic_sds_user_parameters *u = &scic_user_params->sds1;
  274. int i;
  275. for (i = 0; i < SCI_MAX_PHYS; i++) {
  276. struct sci_phy_user_params *u_phy = &u->phys[i];
  277. u_phy->max_speed_generation = phy_gen;
  278. /* we are not exporting these for now */
  279. u_phy->align_insertion_frequency = 0x7f;
  280. u_phy->in_connection_align_insertion_frequency = 0xff;
  281. u_phy->notify_enable_spin_up_insertion_frequency = 0x33;
  282. }
  283. u->stp_inactivity_timeout = stp_inactive_to;
  284. u->ssp_inactivity_timeout = ssp_inactive_to;
  285. u->stp_max_occupancy_timeout = stp_max_occ_to;
  286. u->ssp_max_occupancy_timeout = ssp_max_occ_to;
  287. u->no_outbound_task_timeout = no_outbound_task_to;
  288. u->max_number_concurrent_device_spin_up = max_concurr_spinup;
  289. }
  290. int isci_host_init(struct isci_host *isci_host)
  291. {
  292. int err = 0, i;
  293. enum sci_status status;
  294. struct scic_sds_controller *controller;
  295. union scic_oem_parameters oem;
  296. union scic_user_parameters scic_user_params;
  297. struct isci_pci_info *pci_info = to_pci_info(isci_host->pdev);
  298. isci_timer_list_construct(isci_host);
  299. controller = scic_controller_alloc(&isci_host->pdev->dev);
  300. if (!controller) {
  301. dev_err(&isci_host->pdev->dev,
  302. "%s: failed (%d)\n",
  303. __func__,
  304. err);
  305. return -ENOMEM;
  306. }
  307. isci_host->core_controller = controller;
  308. sci_object_set_association(isci_host->core_controller, isci_host);
  309. spin_lock_init(&isci_host->state_lock);
  310. spin_lock_init(&isci_host->scic_lock);
  311. spin_lock_init(&isci_host->queue_lock);
  312. init_waitqueue_head(&isci_host->eventq);
  313. isci_host_change_state(isci_host, isci_starting);
  314. isci_host->can_queue = ISCI_CAN_QUEUE_VAL;
  315. status = scic_controller_construct(controller, scu_base(isci_host),
  316. smu_base(isci_host));
  317. if (status != SCI_SUCCESS) {
  318. dev_err(&isci_host->pdev->dev,
  319. "%s: scic_controller_construct failed - status = %x\n",
  320. __func__,
  321. status);
  322. return -ENODEV;
  323. }
  324. isci_host->sas_ha.dev = &isci_host->pdev->dev;
  325. isci_host->sas_ha.lldd_ha = isci_host;
  326. /*
  327. * grab initial values stored in the controller object for OEM and USER
  328. * parameters
  329. */
  330. isci_user_parameters_get(isci_host, &scic_user_params);
  331. status = scic_user_parameters_set(isci_host->core_controller,
  332. &scic_user_params);
  333. if (status != SCI_SUCCESS) {
  334. dev_warn(&isci_host->pdev->dev,
  335. "%s: scic_user_parameters_set failed\n",
  336. __func__);
  337. return -ENODEV;
  338. }
  339. scic_oem_parameters_get(controller, &oem);
  340. /* grab any OEM parameters specified in orom */
  341. if (pci_info->orom) {
  342. status = isci_parse_oem_parameters(&oem,
  343. pci_info->orom,
  344. isci_host->id);
  345. if (status != SCI_SUCCESS) {
  346. dev_warn(&isci_host->pdev->dev,
  347. "parsing firmware oem parameters failed\n");
  348. return -EINVAL;
  349. }
  350. }
  351. status = scic_oem_parameters_set(isci_host->core_controller, &oem);
  352. if (status != SCI_SUCCESS) {
  353. dev_warn(&isci_host->pdev->dev,
  354. "%s: scic_oem_parameters_set failed\n",
  355. __func__);
  356. return -ENODEV;
  357. }
  358. tasklet_init(&isci_host->completion_tasklet,
  359. isci_host_completion_routine, (unsigned long)isci_host);
  360. INIT_LIST_HEAD(&isci_host->requests_to_complete);
  361. INIT_LIST_HEAD(&isci_host->requests_to_errorback);
  362. spin_lock_irq(&isci_host->scic_lock);
  363. status = scic_controller_initialize(isci_host->core_controller);
  364. spin_unlock_irq(&isci_host->scic_lock);
  365. if (status != SCI_SUCCESS) {
  366. dev_warn(&isci_host->pdev->dev,
  367. "%s: scic_controller_initialize failed -"
  368. " status = 0x%x\n",
  369. __func__, status);
  370. return -ENODEV;
  371. }
  372. err = scic_controller_mem_init(isci_host->core_controller);
  373. if (err)
  374. return err;
  375. /*
  376. * keep the pool alloc size around, will use it for a bounds checking
  377. * when trying to convert virtual addresses to physical addresses
  378. */
  379. isci_host->dma_pool_alloc_size = sizeof(struct isci_request) +
  380. scic_io_request_get_object_size();
  381. isci_host->dma_pool = dmam_pool_create(DRV_NAME, &isci_host->pdev->dev,
  382. isci_host->dma_pool_alloc_size,
  383. SLAB_HWCACHE_ALIGN, 0);
  384. if (!isci_host->dma_pool)
  385. return -ENOMEM;
  386. for (i = 0; i < SCI_MAX_PORTS; i++)
  387. isci_port_init(&isci_host->isci_ports[i], isci_host, i);
  388. for (i = 0; i < SCI_MAX_PHYS; i++)
  389. isci_phy_init(&isci_host->phys[i], isci_host, i);
  390. for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
  391. struct isci_remote_device *idev = idev_by_id(isci_host, i);
  392. INIT_LIST_HEAD(&idev->reqs_in_process);
  393. INIT_LIST_HEAD(&idev->node);
  394. spin_lock_init(&idev->state_lock);
  395. }
  396. return 0;
  397. }