host.c 16 KB

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