remote_device.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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. * @idev: This parameter specifies the remote device
  240. *
  241. */
  242. void isci_remote_device_ready(struct isci_remote_device *idev)
  243. {
  244. struct isci_host *ihost = idev->isci_port->isci_host;
  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(
  260. struct isci_remote_device *isci_device,
  261. u32 reason_code)
  262. {
  263. dev_dbg(&isci_device->isci_port->isci_host->pdev->dev,
  264. "%s: isci_device = %p\n", __func__, isci_device);
  265. if (reason_code == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
  266. isci_remote_device_change_state(isci_device, isci_stopping);
  267. else
  268. /* device ready is actually a "not ready for io" state. */
  269. isci_remote_device_change_state(isci_device, isci_ready);
  270. }
  271. /**
  272. * isci_remote_device_stop_complete() - This function is called by the scic
  273. * when the remote device stop has completed. We mark the isci device as not
  274. * ready and remove the isci remote device.
  275. * @isci_host: This parameter specifies the isci host object.
  276. * @isci_device: This parameter specifies the remote device.
  277. * @status: This parameter specifies status of the completion.
  278. *
  279. */
  280. void isci_remote_device_stop_complete(
  281. struct isci_host *isci_host,
  282. struct isci_remote_device *isci_device,
  283. enum sci_status status)
  284. {
  285. dev_dbg(&isci_host->pdev->dev,
  286. "%s: complete isci_device = %p, status = 0x%x\n",
  287. __func__,
  288. isci_device,
  289. status);
  290. isci_remote_device_change_state(isci_device, isci_stopped);
  291. /* after stop, we can tear down resources. */
  292. isci_remote_device_deconstruct(isci_host, isci_device);
  293. }
  294. /**
  295. * isci_remote_device_start_complete() - This function is called by the scic
  296. * when the remote device start has completed
  297. * @isci_host: This parameter specifies the isci host object.
  298. * @isci_device: This parameter specifies the remote device.
  299. * @status: This parameter specifies status of the completion.
  300. *
  301. */
  302. void isci_remote_device_start_complete(
  303. struct isci_host *isci_host,
  304. struct isci_remote_device *isci_device,
  305. enum sci_status status)
  306. {
  307. }
  308. /**
  309. * isci_remote_device_stop() - This function is called internally to stop the
  310. * remote device.
  311. * @isci_host: This parameter specifies the isci host object.
  312. * @isci_device: This parameter specifies the remote device.
  313. *
  314. * The status of the scic request to stop.
  315. */
  316. enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
  317. {
  318. enum sci_status status;
  319. unsigned long flags;
  320. dev_dbg(&ihost->pdev->dev,
  321. "%s: isci_device = %p\n", __func__, idev);
  322. isci_remote_device_change_state(idev, isci_stopping);
  323. set_bit(IDEV_STOP_PENDING, &idev->flags);
  324. spin_lock_irqsave(&ihost->scic_lock, flags);
  325. status = scic_remote_device_stop(to_sci_dev(idev), 50);
  326. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  327. /* Wait for the stop complete callback. */
  328. if (status == SCI_SUCCESS) {
  329. wait_for_device_stop(ihost, idev);
  330. clear_bit(IDEV_ALLOCATED, &idev->flags);
  331. }
  332. dev_dbg(&ihost->pdev->dev,
  333. "%s: idev = %p - after completion wait\n",
  334. __func__, idev);
  335. return status;
  336. }
  337. /**
  338. * isci_remote_device_gone() - This function is called by libsas when a domain
  339. * device is removed.
  340. * @domain_device: This parameter specifies the libsas domain device.
  341. *
  342. */
  343. void isci_remote_device_gone(struct domain_device *dev)
  344. {
  345. struct isci_host *ihost = dev->port->ha->lldd_ha;
  346. struct isci_remote_device *idev = dev->lldd_dev;
  347. dev_dbg(&ihost->pdev->dev,
  348. "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
  349. __func__, dev, idev, idev->isci_port);
  350. isci_remote_device_stop(ihost, idev);
  351. }
  352. /**
  353. * isci_remote_device_found() - This function is called by libsas when a remote
  354. * device is discovered. A remote device object is created and started. the
  355. * function then sleeps until the sci core device started message is
  356. * received.
  357. * @domain_device: This parameter specifies the libsas domain device.
  358. *
  359. * status, zero indicates success.
  360. */
  361. int isci_remote_device_found(struct domain_device *domain_dev)
  362. {
  363. struct isci_host *isci_host;
  364. struct isci_port *isci_port;
  365. struct isci_phy *isci_phy;
  366. struct asd_sas_port *sas_port;
  367. struct asd_sas_phy *sas_phy;
  368. struct isci_remote_device *isci_device;
  369. enum sci_status status;
  370. isci_host = isci_host_from_sas_ha(domain_dev->port->ha);
  371. dev_dbg(&isci_host->pdev->dev,
  372. "%s: domain_device = %p\n", __func__, domain_dev);
  373. wait_for_start(isci_host);
  374. sas_port = domain_dev->port;
  375. sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
  376. port_phy_el);
  377. isci_phy = to_isci_phy(sas_phy);
  378. isci_port = isci_phy->isci_port;
  379. /* we are being called for a device on this port,
  380. * so it has to come up eventually
  381. */
  382. wait_for_completion(&isci_port->start_complete);
  383. if ((isci_stopping == isci_port_get_state(isci_port)) ||
  384. (isci_stopped == isci_port_get_state(isci_port)))
  385. return -ENODEV;
  386. isci_device = isci_remote_device_alloc(isci_host, isci_port);
  387. if (!isci_device)
  388. return -ENODEV;
  389. INIT_LIST_HEAD(&isci_device->node);
  390. domain_dev->lldd_dev = isci_device;
  391. isci_device->domain_dev = domain_dev;
  392. isci_device->isci_port = isci_port;
  393. isci_remote_device_change_state(isci_device, isci_starting);
  394. spin_lock_irq(&isci_host->scic_lock);
  395. list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
  396. set_bit(IDEV_START_PENDING, &isci_device->flags);
  397. status = isci_remote_device_construct(isci_port, isci_device);
  398. spin_unlock_irq(&isci_host->scic_lock);
  399. dev_dbg(&isci_host->pdev->dev,
  400. "%s: isci_device = %p\n",
  401. __func__, isci_device);
  402. if (status != SCI_SUCCESS) {
  403. spin_lock_irq(&isci_host->scic_lock);
  404. isci_remote_device_deconstruct(
  405. isci_host,
  406. isci_device
  407. );
  408. spin_unlock_irq(&isci_host->scic_lock);
  409. return -ENODEV;
  410. }
  411. /* wait for the device ready callback. */
  412. wait_for_device_start(isci_host, isci_device);
  413. return 0;
  414. }
  415. /**
  416. * isci_device_is_reset_pending() - This function will check if there is any
  417. * pending reset condition on the device.
  418. * @request: This parameter is the isci_device object.
  419. *
  420. * true if there is a reset pending for the device.
  421. */
  422. bool isci_device_is_reset_pending(
  423. struct isci_host *isci_host,
  424. struct isci_remote_device *isci_device)
  425. {
  426. struct isci_request *isci_request;
  427. struct isci_request *tmp_req;
  428. bool reset_is_pending = false;
  429. unsigned long flags;
  430. dev_dbg(&isci_host->pdev->dev,
  431. "%s: isci_device = %p\n", __func__, isci_device);
  432. spin_lock_irqsave(&isci_host->scic_lock, flags);
  433. /* Check for reset on all pending requests. */
  434. list_for_each_entry_safe(isci_request, tmp_req,
  435. &isci_device->reqs_in_process, dev_node) {
  436. dev_dbg(&isci_host->pdev->dev,
  437. "%s: isci_device = %p request = %p\n",
  438. __func__, isci_device, isci_request);
  439. if (isci_request->ttype == io_task) {
  440. unsigned long flags;
  441. struct sas_task *task = isci_request_access_task(
  442. isci_request);
  443. spin_lock_irqsave(&task->task_state_lock, flags);
  444. if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
  445. reset_is_pending = true;
  446. spin_unlock_irqrestore(&task->task_state_lock, flags);
  447. }
  448. }
  449. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  450. dev_dbg(&isci_host->pdev->dev,
  451. "%s: isci_device = %p reset_is_pending = %d\n",
  452. __func__, isci_device, reset_is_pending);
  453. return reset_is_pending;
  454. }
  455. /**
  456. * isci_device_clear_reset_pending() - This function will clear if any pending
  457. * reset condition flags on the device.
  458. * @request: This parameter is the isci_device object.
  459. *
  460. * true if there is a reset pending for the device.
  461. */
  462. void isci_device_clear_reset_pending(struct isci_remote_device *isci_device)
  463. {
  464. struct isci_request *isci_request;
  465. struct isci_request *tmp_req;
  466. struct isci_host *isci_host = NULL;
  467. unsigned long flags = 0;
  468. /* FIXME more port gone confusion, and this time it makes the
  469. * locking "fun"
  470. */
  471. if (isci_device->isci_port != NULL)
  472. isci_host = isci_device->isci_port->isci_host;
  473. /*
  474. * FIXME when the isci_host gets sorted out
  475. * use dev_dbg()
  476. */
  477. pr_debug("%s: isci_device=%p, isci_host=%p\n",
  478. __func__, isci_device, isci_host);
  479. if (isci_host != NULL)
  480. spin_lock_irqsave(&isci_host->scic_lock, flags);
  481. else
  482. pr_err("%s: isci_device %p; isci_host == NULL!\n",
  483. __func__, isci_device);
  484. /* Clear reset pending on all pending requests. */
  485. list_for_each_entry_safe(isci_request, tmp_req,
  486. &isci_device->reqs_in_process, dev_node) {
  487. /*
  488. * FIXME when the conditional spinlock is gone
  489. * change to dev_dbg()
  490. */
  491. pr_debug("%s: isci_device = %p request = %p\n",
  492. __func__, isci_device, isci_request);
  493. if (isci_request->ttype == io_task) {
  494. unsigned long flags2;
  495. struct sas_task *task = isci_request_access_task(
  496. isci_request);
  497. spin_lock_irqsave(&task->task_state_lock, flags2);
  498. task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
  499. spin_unlock_irqrestore(&task->task_state_lock, flags2);
  500. }
  501. }
  502. if (isci_host != NULL)
  503. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  504. }
  505. /**
  506. * isci_remote_device_change_state() - This function gets the status of the
  507. * remote_device object.
  508. * @isci_device: This parameter points to the isci_remote_device object
  509. *
  510. * status of the object as a isci_status enum.
  511. */
  512. void isci_remote_device_change_state(
  513. struct isci_remote_device *isci_device,
  514. enum isci_status status)
  515. {
  516. unsigned long flags;
  517. spin_lock_irqsave(&isci_device->state_lock, flags);
  518. isci_device->status = status;
  519. spin_unlock_irqrestore(&isci_device->state_lock, flags);
  520. }