remote_device.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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_phy.h"
  59. #include "scic_port.h"
  60. #include "port.h"
  61. #include "remote_device.h"
  62. #include "request.h"
  63. #include "task.h"
  64. /**
  65. * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
  66. * @ihost: This parameter specifies the isci host object.
  67. * @idev: This parameter specifies the remote device to be freed.
  68. *
  69. */
  70. static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
  71. {
  72. dev_dbg(&ihost->pdev->dev,
  73. "%s: isci_device = %p\n", __func__, idev);
  74. /* There should not be any outstanding io's. All paths to
  75. * here should go through isci_remote_device_nuke_requests.
  76. * If we hit this condition, we will need a way to complete
  77. * io requests in process */
  78. while (!list_empty(&idev->reqs_in_process)) {
  79. dev_err(&ihost->pdev->dev,
  80. "%s: ** request list not empty! **\n", __func__);
  81. BUG();
  82. }
  83. scic_remote_device_destruct(to_sci_dev(idev));
  84. idev->domain_dev->lldd_dev = NULL;
  85. idev->domain_dev = NULL;
  86. idev->isci_port = NULL;
  87. list_del_init(&idev->node);
  88. clear_bit(IDEV_START_PENDING, &idev->flags);
  89. clear_bit(IDEV_STOP_PENDING, &idev->flags);
  90. wake_up(&ihost->eventq);
  91. }
  92. /**
  93. * isci_remote_device_construct() - This function calls the scic remote device
  94. * construct and start functions, it waits on the remote device start
  95. * completion.
  96. * @port: This parameter specifies the isci port with the remote device.
  97. * @isci_device: This parameter specifies the isci remote device
  98. *
  99. * status from the scic calls, the caller to this function should clean up
  100. * resources as appropriate.
  101. */
  102. static enum sci_status isci_remote_device_construct(
  103. struct isci_port *port,
  104. struct isci_remote_device *isci_device)
  105. {
  106. enum sci_status status = SCI_SUCCESS;
  107. /* let the core do it's common constuction. */
  108. scic_remote_device_construct(port->sci_port_handle,
  109. to_sci_dev(isci_device));
  110. /* let the core do it's device specific constuction. */
  111. if (isci_device->domain_dev->parent &&
  112. (isci_device->domain_dev->parent->dev_type == EDGE_DEV)) {
  113. int i;
  114. /* struct smp_response_discover discover_response; */
  115. struct discover_resp discover_response;
  116. struct domain_device *parent =
  117. isci_device->domain_dev->parent;
  118. struct expander_device *parent_ex = &parent->ex_dev;
  119. for (i = 0; i < parent_ex->num_phys; i++) {
  120. struct ex_phy *phy = &parent_ex->ex_phy[i];
  121. if ((phy->phy_state == PHY_VACANT) ||
  122. (phy->phy_state == PHY_NOT_PRESENT))
  123. continue;
  124. if (SAS_ADDR(phy->attached_sas_addr)
  125. == SAS_ADDR(isci_device->domain_dev->sas_addr)) {
  126. discover_response.attached_dev_type
  127. = phy->attached_dev_type;
  128. discover_response.linkrate
  129. = phy->linkrate;
  130. discover_response.attached_sata_host
  131. = phy->attached_sata_host;
  132. discover_response.attached_sata_dev
  133. = phy->attached_sata_dev;
  134. discover_response.attached_sata_ps
  135. = phy->attached_sata_ps;
  136. discover_response.iproto
  137. = phy->attached_iproto >> 1;
  138. discover_response.tproto
  139. = phy->attached_tproto >> 1;
  140. memcpy(
  141. discover_response.attached_sas_addr,
  142. phy->attached_sas_addr,
  143. SAS_ADDR_SIZE
  144. );
  145. discover_response.attached_phy_id
  146. = phy->attached_phy_id;
  147. discover_response.change_count
  148. = phy->phy_change_count;
  149. discover_response.routing_attr
  150. = phy->routing_attr;
  151. discover_response.hmin_linkrate
  152. = phy->phy->minimum_linkrate_hw;
  153. discover_response.hmax_linkrate
  154. = phy->phy->maximum_linkrate_hw;
  155. discover_response.pmin_linkrate
  156. = phy->phy->minimum_linkrate;
  157. discover_response.pmax_linkrate
  158. = phy->phy->maximum_linkrate;
  159. }
  160. }
  161. dev_dbg(&port->isci_host->pdev->dev,
  162. "%s: parent->dev_type = EDGE_DEV\n",
  163. __func__);
  164. status = scic_remote_device_ea_construct(to_sci_dev(isci_device),
  165. (struct smp_response_discover *)&discover_response);
  166. } else
  167. status = scic_remote_device_da_construct(to_sci_dev(isci_device));
  168. if (status != SCI_SUCCESS) {
  169. dev_dbg(&port->isci_host->pdev->dev,
  170. "%s: scic_remote_device_da_construct failed - "
  171. "isci_device = %p\n",
  172. __func__,
  173. isci_device);
  174. return status;
  175. }
  176. sci_object_set_association(to_sci_dev(isci_device), isci_device);
  177. BUG_ON(port->isci_host == NULL);
  178. /* start the device. */
  179. status = scic_remote_device_start(to_sci_dev(isci_device),
  180. ISCI_REMOTE_DEVICE_START_TIMEOUT);
  181. if (status != SCI_SUCCESS) {
  182. dev_warn(&port->isci_host->pdev->dev,
  183. "%s: scic_remote_device_start failed\n",
  184. __func__);
  185. return status;
  186. }
  187. return status;
  188. }
  189. /**
  190. * isci_remote_device_nuke_requests() - This function terminates all requests
  191. * for a given remote device.
  192. * @isci_device: This parameter specifies the remote device
  193. *
  194. */
  195. void isci_remote_device_nuke_requests(
  196. struct isci_remote_device *isci_device)
  197. {
  198. DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
  199. struct isci_host *isci_host;
  200. isci_host = isci_device->isci_port->isci_host;
  201. dev_dbg(&isci_host->pdev->dev,
  202. "%s: isci_device = %p\n", __func__, isci_device);
  203. /* Cleanup all requests pending for this device. */
  204. isci_terminate_pending_requests(isci_host, isci_device, terminating);
  205. dev_dbg(&isci_host->pdev->dev,
  206. "%s: isci_device = %p, done\n", __func__, isci_device);
  207. }
  208. /**
  209. * This function builds the isci_remote_device when a libsas dev_found message
  210. * is received.
  211. * @isci_host: This parameter specifies the isci host object.
  212. * @port: This parameter specifies the isci_port conected to this device.
  213. *
  214. * pointer to new isci_remote_device.
  215. */
  216. static struct isci_remote_device *
  217. isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
  218. {
  219. struct isci_remote_device *idev;
  220. int i;
  221. for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
  222. idev = idev_by_id(ihost, i);
  223. if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
  224. break;
  225. }
  226. if (i >= SCI_MAX_REMOTE_DEVICES) {
  227. dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
  228. return NULL;
  229. }
  230. BUG_ON(!list_empty(&idev->reqs_in_process));
  231. BUG_ON(!list_empty(&idev->node));
  232. isci_remote_device_change_state(idev, isci_freed);
  233. return idev;
  234. }
  235. /**
  236. * isci_remote_device_ready() - This function is called by the scic when the
  237. * remote device is ready. We mark the isci device as ready and signal the
  238. * waiting proccess.
  239. * @ihost: our valid isci_host
  240. * @idev: remote device
  241. *
  242. */
  243. void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
  244. {
  245. dev_dbg(&ihost->pdev->dev,
  246. "%s: idev = %p\n", __func__, idev);
  247. isci_remote_device_change_state(idev, isci_ready_for_io);
  248. if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
  249. wake_up(&ihost->eventq);
  250. }
  251. /**
  252. * isci_remote_device_not_ready() - This function is called by the scic when
  253. * the remote device is not ready. We mark the isci device as ready (not
  254. * "ready_for_io") and signal the waiting proccess.
  255. * @isci_host: This parameter specifies the isci host object.
  256. * @isci_device: This parameter specifies the remote device
  257. *
  258. */
  259. void isci_remote_device_not_ready(struct isci_host *ihost,
  260. struct isci_remote_device *idev, u32 reason)
  261. {
  262. dev_dbg(&ihost->pdev->dev,
  263. "%s: isci_device = %p\n", __func__, idev);
  264. if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
  265. isci_remote_device_change_state(idev, isci_stopping);
  266. else
  267. /* device ready is actually a "not ready for io" state. */
  268. isci_remote_device_change_state(idev, isci_ready);
  269. }
  270. /**
  271. * isci_remote_device_stop_complete() - This function is called by the scic
  272. * when the remote device stop has completed. We mark the isci device as not
  273. * ready and remove the isci remote device.
  274. * @isci_host: This parameter specifies the isci host object.
  275. * @isci_device: This parameter specifies the remote device.
  276. * @status: This parameter specifies status of the completion.
  277. *
  278. */
  279. void isci_remote_device_stop_complete(
  280. struct isci_host *isci_host,
  281. struct isci_remote_device *isci_device,
  282. enum sci_status status)
  283. {
  284. dev_dbg(&isci_host->pdev->dev,
  285. "%s: complete isci_device = %p, status = 0x%x\n",
  286. __func__,
  287. isci_device,
  288. status);
  289. isci_remote_device_change_state(isci_device, isci_stopped);
  290. /* after stop, we can tear down resources. */
  291. isci_remote_device_deconstruct(isci_host, isci_device);
  292. }
  293. /**
  294. * isci_remote_device_start_complete() - This function is called by the scic
  295. * when the remote device start has completed
  296. * @isci_host: This parameter specifies the isci host object.
  297. * @isci_device: This parameter specifies the remote device.
  298. * @status: This parameter specifies status of the completion.
  299. *
  300. */
  301. void isci_remote_device_start_complete(
  302. struct isci_host *isci_host,
  303. struct isci_remote_device *isci_device,
  304. enum sci_status status)
  305. {
  306. }
  307. /**
  308. * isci_remote_device_stop() - This function is called internally to stop the
  309. * remote device.
  310. * @isci_host: This parameter specifies the isci host object.
  311. * @isci_device: This parameter specifies the remote device.
  312. *
  313. * The status of the scic request to stop.
  314. */
  315. enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
  316. {
  317. enum sci_status status;
  318. unsigned long flags;
  319. dev_dbg(&ihost->pdev->dev,
  320. "%s: isci_device = %p\n", __func__, idev);
  321. isci_remote_device_change_state(idev, isci_stopping);
  322. /* Kill all outstanding requests. */
  323. isci_remote_device_nuke_requests(idev);
  324. set_bit(IDEV_STOP_PENDING, &idev->flags);
  325. spin_lock_irqsave(&ihost->scic_lock, flags);
  326. status = scic_remote_device_stop(to_sci_dev(idev), 50);
  327. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  328. /* Wait for the stop complete callback. */
  329. if (status == SCI_SUCCESS) {
  330. wait_for_device_stop(ihost, idev);
  331. clear_bit(IDEV_ALLOCATED, &idev->flags);
  332. }
  333. dev_dbg(&ihost->pdev->dev,
  334. "%s: idev = %p - after completion wait\n",
  335. __func__, idev);
  336. return status;
  337. }
  338. /**
  339. * isci_remote_device_gone() - This function is called by libsas when a domain
  340. * device is removed.
  341. * @domain_device: This parameter specifies the libsas domain device.
  342. *
  343. */
  344. void isci_remote_device_gone(struct domain_device *dev)
  345. {
  346. struct isci_host *ihost = dev->port->ha->lldd_ha;
  347. struct isci_remote_device *idev = dev->lldd_dev;
  348. dev_dbg(&ihost->pdev->dev,
  349. "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
  350. __func__, dev, idev, idev->isci_port);
  351. isci_remote_device_stop(ihost, idev);
  352. }
  353. /**
  354. * isci_remote_device_found() - This function is called by libsas when a remote
  355. * device is discovered. A remote device object is created and started. the
  356. * function then sleeps until the sci core device started message is
  357. * received.
  358. * @domain_device: This parameter specifies the libsas domain device.
  359. *
  360. * status, zero indicates success.
  361. */
  362. int isci_remote_device_found(struct domain_device *domain_dev)
  363. {
  364. struct isci_host *isci_host;
  365. struct isci_port *isci_port;
  366. struct isci_phy *isci_phy;
  367. struct asd_sas_port *sas_port;
  368. struct asd_sas_phy *sas_phy;
  369. struct isci_remote_device *isci_device;
  370. enum sci_status status;
  371. isci_host = isci_host_from_sas_ha(domain_dev->port->ha);
  372. dev_dbg(&isci_host->pdev->dev,
  373. "%s: domain_device = %p\n", __func__, domain_dev);
  374. wait_for_start(isci_host);
  375. sas_port = domain_dev->port;
  376. sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
  377. port_phy_el);
  378. isci_phy = to_isci_phy(sas_phy);
  379. isci_port = isci_phy->isci_port;
  380. /* we are being called for a device on this port,
  381. * so it has to come up eventually
  382. */
  383. wait_for_completion(&isci_port->start_complete);
  384. if ((isci_stopping == isci_port_get_state(isci_port)) ||
  385. (isci_stopped == isci_port_get_state(isci_port)))
  386. return -ENODEV;
  387. isci_device = isci_remote_device_alloc(isci_host, isci_port);
  388. if (!isci_device)
  389. return -ENODEV;
  390. INIT_LIST_HEAD(&isci_device->node);
  391. domain_dev->lldd_dev = isci_device;
  392. isci_device->domain_dev = domain_dev;
  393. isci_device->isci_port = isci_port;
  394. isci_remote_device_change_state(isci_device, isci_starting);
  395. spin_lock_irq(&isci_host->scic_lock);
  396. list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
  397. set_bit(IDEV_START_PENDING, &isci_device->flags);
  398. status = isci_remote_device_construct(isci_port, isci_device);
  399. spin_unlock_irq(&isci_host->scic_lock);
  400. dev_dbg(&isci_host->pdev->dev,
  401. "%s: isci_device = %p\n",
  402. __func__, isci_device);
  403. if (status != SCI_SUCCESS) {
  404. spin_lock_irq(&isci_host->scic_lock);
  405. isci_remote_device_deconstruct(
  406. isci_host,
  407. isci_device
  408. );
  409. spin_unlock_irq(&isci_host->scic_lock);
  410. return -ENODEV;
  411. }
  412. /* wait for the device ready callback. */
  413. wait_for_device_start(isci_host, isci_device);
  414. return 0;
  415. }
  416. /**
  417. * isci_device_is_reset_pending() - This function will check if there is any
  418. * pending reset condition on the device.
  419. * @request: This parameter is the isci_device object.
  420. *
  421. * true if there is a reset pending for the device.
  422. */
  423. bool isci_device_is_reset_pending(
  424. struct isci_host *isci_host,
  425. struct isci_remote_device *isci_device)
  426. {
  427. struct isci_request *isci_request;
  428. struct isci_request *tmp_req;
  429. bool reset_is_pending = false;
  430. unsigned long flags;
  431. dev_dbg(&isci_host->pdev->dev,
  432. "%s: isci_device = %p\n", __func__, isci_device);
  433. spin_lock_irqsave(&isci_host->scic_lock, flags);
  434. /* Check for reset on all pending requests. */
  435. list_for_each_entry_safe(isci_request, tmp_req,
  436. &isci_device->reqs_in_process, dev_node) {
  437. dev_dbg(&isci_host->pdev->dev,
  438. "%s: isci_device = %p request = %p\n",
  439. __func__, isci_device, isci_request);
  440. if (isci_request->ttype == io_task) {
  441. unsigned long flags;
  442. struct sas_task *task = isci_request_access_task(
  443. isci_request);
  444. spin_lock_irqsave(&task->task_state_lock, flags);
  445. if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
  446. reset_is_pending = true;
  447. spin_unlock_irqrestore(&task->task_state_lock, flags);
  448. }
  449. }
  450. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  451. dev_dbg(&isci_host->pdev->dev,
  452. "%s: isci_device = %p reset_is_pending = %d\n",
  453. __func__, isci_device, reset_is_pending);
  454. return reset_is_pending;
  455. }
  456. /**
  457. * isci_device_clear_reset_pending() - This function will clear if any pending
  458. * reset condition flags on the device.
  459. * @request: This parameter is the isci_device object.
  460. *
  461. * true if there is a reset pending for the device.
  462. */
  463. void isci_device_clear_reset_pending(struct isci_remote_device *isci_device)
  464. {
  465. struct isci_request *isci_request;
  466. struct isci_request *tmp_req;
  467. struct isci_host *isci_host = NULL;
  468. unsigned long flags = 0;
  469. /* FIXME more port gone confusion, and this time it makes the
  470. * locking "fun"
  471. */
  472. if (isci_device->isci_port != NULL)
  473. isci_host = isci_device->isci_port->isci_host;
  474. /*
  475. * FIXME when the isci_host gets sorted out
  476. * use dev_dbg()
  477. */
  478. pr_debug("%s: isci_device=%p, isci_host=%p\n",
  479. __func__, isci_device, isci_host);
  480. if (isci_host != NULL)
  481. spin_lock_irqsave(&isci_host->scic_lock, flags);
  482. else
  483. pr_err("%s: isci_device %p; isci_host == NULL!\n",
  484. __func__, isci_device);
  485. /* Clear reset pending on all pending requests. */
  486. list_for_each_entry_safe(isci_request, tmp_req,
  487. &isci_device->reqs_in_process, dev_node) {
  488. /*
  489. * FIXME when the conditional spinlock is gone
  490. * change to dev_dbg()
  491. */
  492. pr_debug("%s: isci_device = %p request = %p\n",
  493. __func__, isci_device, isci_request);
  494. if (isci_request->ttype == io_task) {
  495. unsigned long flags2;
  496. struct sas_task *task = isci_request_access_task(
  497. isci_request);
  498. spin_lock_irqsave(&task->task_state_lock, flags2);
  499. task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
  500. spin_unlock_irqrestore(&task->task_state_lock, flags2);
  501. }
  502. }
  503. if (isci_host != NULL)
  504. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  505. }
  506. /**
  507. * isci_remote_device_change_state() - This function gets the status of the
  508. * remote_device object.
  509. * @isci_device: This parameter points to the isci_remote_device object
  510. *
  511. * status of the object as a isci_status enum.
  512. */
  513. void isci_remote_device_change_state(
  514. struct isci_remote_device *isci_device,
  515. enum isci_status status)
  516. {
  517. unsigned long flags;
  518. spin_lock_irqsave(&isci_device->state_lock, flags);
  519. isci_device->status = status;
  520. spin_unlock_irqrestore(&isci_device->state_lock, flags);
  521. }