task.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677
  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/completion.h>
  56. #include <linux/irqflags.h>
  57. #include "sas.h"
  58. #include <scsi/libsas.h>
  59. #include "remote_device.h"
  60. #include "remote_node_context.h"
  61. #include "isci.h"
  62. #include "request.h"
  63. #include "task.h"
  64. #include "host.h"
  65. /**
  66. * isci_task_refuse() - complete the request to the upper layer driver in
  67. * the case where an I/O needs to be completed back in the submit path.
  68. * @ihost: host on which the the request was queued
  69. * @task: request to complete
  70. * @response: response code for the completed task.
  71. * @status: status code for the completed task.
  72. *
  73. */
  74. static void isci_task_refuse(struct isci_host *ihost, struct sas_task *task,
  75. enum service_response response,
  76. enum exec_status status)
  77. {
  78. enum isci_completion_selection disposition;
  79. disposition = isci_perform_normal_io_completion;
  80. disposition = isci_task_set_completion_status(task, response, status,
  81. disposition);
  82. /* Tasks aborted specifically by a call to the lldd_abort_task
  83. * function should not be completed to the host in the regular path.
  84. */
  85. switch (disposition) {
  86. case isci_perform_normal_io_completion:
  87. /* Normal notification (task_done) */
  88. dev_dbg(&ihost->pdev->dev,
  89. "%s: Normal - task = %p, response=%d, "
  90. "status=%d\n",
  91. __func__, task, response, status);
  92. task->lldd_task = NULL;
  93. isci_execpath_callback(ihost, task, task->task_done);
  94. break;
  95. case isci_perform_aborted_io_completion:
  96. /*
  97. * No notification because this request is already in the
  98. * abort path.
  99. */
  100. dev_dbg(&ihost->pdev->dev,
  101. "%s: Aborted - task = %p, response=%d, "
  102. "status=%d\n",
  103. __func__, task, response, status);
  104. break;
  105. case isci_perform_error_io_completion:
  106. /* Use sas_task_abort */
  107. dev_dbg(&ihost->pdev->dev,
  108. "%s: Error - task = %p, response=%d, "
  109. "status=%d\n",
  110. __func__, task, response, status);
  111. isci_execpath_callback(ihost, task, sas_task_abort);
  112. break;
  113. default:
  114. dev_dbg(&ihost->pdev->dev,
  115. "%s: isci task notification default case!",
  116. __func__);
  117. sas_task_abort(task);
  118. break;
  119. }
  120. }
  121. #define for_each_sas_task(num, task) \
  122. for (; num > 0; num--,\
  123. task = list_entry(task->list.next, struct sas_task, list))
  124. static inline int isci_device_io_ready(struct isci_remote_device *idev,
  125. struct sas_task *task)
  126. {
  127. return idev ? test_bit(IDEV_IO_READY, &idev->flags) ||
  128. (test_bit(IDEV_IO_NCQERROR, &idev->flags) &&
  129. isci_task_is_ncq_recovery(task))
  130. : 0;
  131. }
  132. /**
  133. * isci_task_execute_task() - This function is one of the SAS Domain Template
  134. * functions. This function is called by libsas to send a task down to
  135. * hardware.
  136. * @task: This parameter specifies the SAS task to send.
  137. * @num: This parameter specifies the number of tasks to queue.
  138. * @gfp_flags: This parameter specifies the context of this call.
  139. *
  140. * status, zero indicates success.
  141. */
  142. int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags)
  143. {
  144. struct isci_host *ihost = dev_to_ihost(task->dev);
  145. struct isci_remote_device *idev;
  146. unsigned long flags;
  147. bool io_ready;
  148. u16 tag;
  149. dev_dbg(&ihost->pdev->dev, "%s: num=%d\n", __func__, num);
  150. for_each_sas_task(num, task) {
  151. enum sci_status status = SCI_FAILURE;
  152. spin_lock_irqsave(&ihost->scic_lock, flags);
  153. idev = isci_lookup_device(task->dev);
  154. io_ready = isci_device_io_ready(idev, task);
  155. tag = isci_alloc_tag(ihost);
  156. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  157. dev_dbg(&ihost->pdev->dev,
  158. "task: %p, num: %d dev: %p idev: %p:%#lx cmd = %p\n",
  159. task, num, task->dev, idev, idev ? idev->flags : 0,
  160. task->uldd_task);
  161. if (!idev) {
  162. isci_task_refuse(ihost, task, SAS_TASK_UNDELIVERED,
  163. SAS_DEVICE_UNKNOWN);
  164. } else if (!io_ready || tag == SCI_CONTROLLER_INVALID_IO_TAG) {
  165. /* Indicate QUEUE_FULL so that the scsi midlayer
  166. * retries.
  167. */
  168. isci_task_refuse(ihost, task, SAS_TASK_COMPLETE,
  169. SAS_QUEUE_FULL);
  170. } else {
  171. /* There is a device and it's ready for I/O. */
  172. spin_lock_irqsave(&task->task_state_lock, flags);
  173. if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
  174. /* The I/O was aborted. */
  175. spin_unlock_irqrestore(&task->task_state_lock,
  176. flags);
  177. isci_task_refuse(ihost, task,
  178. SAS_TASK_UNDELIVERED,
  179. SAM_STAT_TASK_ABORTED);
  180. } else {
  181. task->task_state_flags |= SAS_TASK_AT_INITIATOR;
  182. spin_unlock_irqrestore(&task->task_state_lock, flags);
  183. /* build and send the request. */
  184. status = isci_request_execute(ihost, idev, task, tag);
  185. if (status != SCI_SUCCESS) {
  186. spin_lock_irqsave(&task->task_state_lock, flags);
  187. /* Did not really start this command. */
  188. task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
  189. spin_unlock_irqrestore(&task->task_state_lock, flags);
  190. /* Indicate QUEUE_FULL so that the scsi
  191. * midlayer retries. if the request
  192. * failed for remote device reasons,
  193. * it gets returned as
  194. * SAS_TASK_UNDELIVERED next time
  195. * through.
  196. */
  197. isci_task_refuse(ihost, task,
  198. SAS_TASK_COMPLETE,
  199. SAS_QUEUE_FULL);
  200. }
  201. }
  202. }
  203. if (status != SCI_SUCCESS && tag != SCI_CONTROLLER_INVALID_IO_TAG) {
  204. spin_lock_irqsave(&ihost->scic_lock, flags);
  205. /* command never hit the device, so just free
  206. * the tci and skip the sequence increment
  207. */
  208. isci_tci_free(ihost, ISCI_TAG_TCI(tag));
  209. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  210. }
  211. isci_put_device(idev);
  212. }
  213. return 0;
  214. }
  215. static enum sci_status isci_sata_management_task_request_build(struct isci_request *ireq)
  216. {
  217. struct isci_tmf *isci_tmf;
  218. enum sci_status status;
  219. if (tmf_task != ireq->ttype)
  220. return SCI_FAILURE;
  221. isci_tmf = isci_request_access_tmf(ireq);
  222. switch (isci_tmf->tmf_code) {
  223. case isci_tmf_sata_srst_high:
  224. case isci_tmf_sata_srst_low: {
  225. struct host_to_dev_fis *fis = &ireq->stp.cmd;
  226. memset(fis, 0, sizeof(*fis));
  227. fis->fis_type = 0x27;
  228. fis->flags &= ~0x80;
  229. fis->flags &= 0xF0;
  230. if (isci_tmf->tmf_code == isci_tmf_sata_srst_high)
  231. fis->control |= ATA_SRST;
  232. else
  233. fis->control &= ~ATA_SRST;
  234. break;
  235. }
  236. /* other management commnd go here... */
  237. default:
  238. return SCI_FAILURE;
  239. }
  240. /* core builds the protocol specific request
  241. * based on the h2d fis.
  242. */
  243. status = sci_task_request_construct_sata(ireq);
  244. return status;
  245. }
  246. static struct isci_request *isci_task_request_build(struct isci_host *ihost,
  247. struct isci_remote_device *idev,
  248. u16 tag, struct isci_tmf *isci_tmf)
  249. {
  250. enum sci_status status = SCI_FAILURE;
  251. struct isci_request *ireq = NULL;
  252. struct domain_device *dev;
  253. dev_dbg(&ihost->pdev->dev,
  254. "%s: isci_tmf = %p\n", __func__, isci_tmf);
  255. dev = idev->domain_dev;
  256. /* do common allocation and init of request object. */
  257. ireq = isci_tmf_request_from_tag(ihost, isci_tmf, tag);
  258. if (!ireq)
  259. return NULL;
  260. /* let the core do it's construct. */
  261. status = sci_task_request_construct(ihost, idev, tag,
  262. ireq);
  263. if (status != SCI_SUCCESS) {
  264. dev_warn(&ihost->pdev->dev,
  265. "%s: sci_task_request_construct failed - "
  266. "status = 0x%x\n",
  267. __func__,
  268. status);
  269. return NULL;
  270. }
  271. /* XXX convert to get this from task->tproto like other drivers */
  272. if (dev->dev_type == SAS_END_DEV) {
  273. isci_tmf->proto = SAS_PROTOCOL_SSP;
  274. status = sci_task_request_construct_ssp(ireq);
  275. if (status != SCI_SUCCESS)
  276. return NULL;
  277. }
  278. if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
  279. isci_tmf->proto = SAS_PROTOCOL_SATA;
  280. status = isci_sata_management_task_request_build(ireq);
  281. if (status != SCI_SUCCESS)
  282. return NULL;
  283. }
  284. return ireq;
  285. }
  286. static int isci_task_execute_tmf(struct isci_host *ihost,
  287. struct isci_remote_device *idev,
  288. struct isci_tmf *tmf, unsigned long timeout_ms)
  289. {
  290. DECLARE_COMPLETION_ONSTACK(completion);
  291. enum sci_task_status status = SCI_TASK_FAILURE;
  292. struct isci_request *ireq;
  293. int ret = TMF_RESP_FUNC_FAILED;
  294. unsigned long flags;
  295. unsigned long timeleft;
  296. u16 tag;
  297. spin_lock_irqsave(&ihost->scic_lock, flags);
  298. tag = isci_alloc_tag(ihost);
  299. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  300. if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
  301. return ret;
  302. /* sanity check, return TMF_RESP_FUNC_FAILED
  303. * if the device is not there and ready.
  304. */
  305. if (!idev ||
  306. (!test_bit(IDEV_IO_READY, &idev->flags) &&
  307. !test_bit(IDEV_IO_NCQERROR, &idev->flags))) {
  308. dev_dbg(&ihost->pdev->dev,
  309. "%s: idev = %p not ready (%#lx)\n",
  310. __func__,
  311. idev, idev ? idev->flags : 0);
  312. goto err_tci;
  313. } else
  314. dev_dbg(&ihost->pdev->dev,
  315. "%s: idev = %p\n",
  316. __func__, idev);
  317. /* Assign the pointer to the TMF's completion kernel wait structure. */
  318. tmf->complete = &completion;
  319. ireq = isci_task_request_build(ihost, idev, tag, tmf);
  320. if (!ireq)
  321. goto err_tci;
  322. spin_lock_irqsave(&ihost->scic_lock, flags);
  323. /* start the TMF io. */
  324. status = sci_controller_start_task(ihost, idev, ireq);
  325. if (status != SCI_TASK_SUCCESS) {
  326. dev_dbg(&ihost->pdev->dev,
  327. "%s: start_io failed - status = 0x%x, request = %p\n",
  328. __func__,
  329. status,
  330. ireq);
  331. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  332. goto err_tci;
  333. }
  334. if (tmf->cb_state_func != NULL)
  335. tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);
  336. isci_request_change_state(ireq, started);
  337. /* add the request to the remote device request list. */
  338. list_add(&ireq->dev_node, &idev->reqs_in_process);
  339. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  340. /* Wait for the TMF to complete, or a timeout. */
  341. timeleft = wait_for_completion_timeout(&completion,
  342. msecs_to_jiffies(timeout_ms));
  343. if (timeleft == 0) {
  344. spin_lock_irqsave(&ihost->scic_lock, flags);
  345. if (tmf->cb_state_func != NULL)
  346. tmf->cb_state_func(isci_tmf_timed_out, tmf, tmf->cb_data);
  347. sci_controller_terminate_request(ihost,
  348. idev,
  349. ireq);
  350. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  351. wait_for_completion(tmf->complete);
  352. }
  353. isci_print_tmf(tmf);
  354. if (tmf->status == SCI_SUCCESS)
  355. ret = TMF_RESP_FUNC_COMPLETE;
  356. else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
  357. dev_dbg(&ihost->pdev->dev,
  358. "%s: tmf.status == "
  359. "SCI_FAILURE_IO_RESPONSE_VALID\n",
  360. __func__);
  361. ret = TMF_RESP_FUNC_COMPLETE;
  362. }
  363. /* Else - leave the default "failed" status alone. */
  364. dev_dbg(&ihost->pdev->dev,
  365. "%s: completed request = %p\n",
  366. __func__,
  367. ireq);
  368. return ret;
  369. err_tci:
  370. spin_lock_irqsave(&ihost->scic_lock, flags);
  371. isci_tci_free(ihost, ISCI_TAG_TCI(tag));
  372. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  373. return ret;
  374. }
  375. static void isci_task_build_tmf(struct isci_tmf *tmf,
  376. enum isci_tmf_function_codes code,
  377. void (*tmf_sent_cb)(enum isci_tmf_cb_state,
  378. struct isci_tmf *,
  379. void *),
  380. void *cb_data)
  381. {
  382. memset(tmf, 0, sizeof(*tmf));
  383. tmf->tmf_code = code;
  384. tmf->cb_state_func = tmf_sent_cb;
  385. tmf->cb_data = cb_data;
  386. }
  387. static void isci_task_build_abort_task_tmf(struct isci_tmf *tmf,
  388. enum isci_tmf_function_codes code,
  389. void (*tmf_sent_cb)(enum isci_tmf_cb_state,
  390. struct isci_tmf *,
  391. void *),
  392. struct isci_request *old_request)
  393. {
  394. isci_task_build_tmf(tmf, code, tmf_sent_cb, old_request);
  395. tmf->io_tag = old_request->io_tag;
  396. }
  397. /**
  398. * isci_task_validate_request_to_abort() - This function checks the given I/O
  399. * against the "started" state. If the request is still "started", it's
  400. * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
  401. * BEFORE CALLING THIS FUNCTION.
  402. * @isci_request: This parameter specifies the request object to control.
  403. * @isci_host: This parameter specifies the ISCI host object
  404. * @isci_device: This is the device to which the request is pending.
  405. * @aborted_io_completion: This is a completion structure that will be added to
  406. * the request in case it is changed to aborting; this completion is
  407. * triggered when the request is fully completed.
  408. *
  409. * Either "started" on successful change of the task status to "aborted", or
  410. * "unallocated" if the task cannot be controlled.
  411. */
  412. static enum isci_request_status isci_task_validate_request_to_abort(
  413. struct isci_request *isci_request,
  414. struct isci_host *isci_host,
  415. struct isci_remote_device *isci_device,
  416. struct completion *aborted_io_completion)
  417. {
  418. enum isci_request_status old_state = unallocated;
  419. /* Only abort the task if it's in the
  420. * device's request_in_process list
  421. */
  422. if (isci_request && !list_empty(&isci_request->dev_node)) {
  423. old_state = isci_request_change_started_to_aborted(
  424. isci_request, aborted_io_completion);
  425. }
  426. return old_state;
  427. }
  428. /**
  429. * isci_request_cleanup_completed_loiterer() - This function will take care of
  430. * the final cleanup on any request which has been explicitly terminated.
  431. * @isci_host: This parameter specifies the ISCI host object
  432. * @isci_device: This is the device to which the request is pending.
  433. * @isci_request: This parameter specifies the terminated request object.
  434. * @task: This parameter is the libsas I/O request.
  435. */
  436. static void isci_request_cleanup_completed_loiterer(
  437. struct isci_host *isci_host,
  438. struct isci_remote_device *isci_device,
  439. struct isci_request *isci_request,
  440. struct sas_task *task)
  441. {
  442. unsigned long flags;
  443. dev_dbg(&isci_host->pdev->dev,
  444. "%s: isci_device=%p, request=%p, task=%p\n",
  445. __func__, isci_device, isci_request, task);
  446. if (task != NULL) {
  447. spin_lock_irqsave(&task->task_state_lock, flags);
  448. task->lldd_task = NULL;
  449. task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
  450. isci_set_task_doneflags(task);
  451. /* If this task is not in the abort path, call task_done. */
  452. if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  453. spin_unlock_irqrestore(&task->task_state_lock, flags);
  454. task->task_done(task);
  455. } else
  456. spin_unlock_irqrestore(&task->task_state_lock, flags);
  457. }
  458. if (isci_request != NULL) {
  459. spin_lock_irqsave(&isci_host->scic_lock, flags);
  460. list_del_init(&isci_request->dev_node);
  461. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  462. }
  463. }
  464. /**
  465. * isci_terminate_request_core() - This function will terminate the given
  466. * request, and wait for it to complete. This function must only be called
  467. * from a thread that can wait. Note that the request is terminated and
  468. * completed (back to the host, if started there).
  469. * @ihost: This SCU.
  470. * @idev: The target.
  471. * @isci_request: The I/O request to be terminated.
  472. *
  473. */
  474. static void isci_terminate_request_core(struct isci_host *ihost,
  475. struct isci_remote_device *idev,
  476. struct isci_request *isci_request)
  477. {
  478. enum sci_status status = SCI_SUCCESS;
  479. bool was_terminated = false;
  480. bool needs_cleanup_handling = false;
  481. enum isci_request_status request_status;
  482. unsigned long flags;
  483. unsigned long termination_completed = 1;
  484. struct completion *io_request_completion;
  485. struct sas_task *task;
  486. dev_dbg(&ihost->pdev->dev,
  487. "%s: device = %p; request = %p\n",
  488. __func__, idev, isci_request);
  489. spin_lock_irqsave(&ihost->scic_lock, flags);
  490. io_request_completion = isci_request->io_request_completion;
  491. task = (isci_request->ttype == io_task)
  492. ? isci_request_access_task(isci_request)
  493. : NULL;
  494. /* Note that we are not going to control
  495. * the target to abort the request.
  496. */
  497. set_bit(IREQ_COMPLETE_IN_TARGET, &isci_request->flags);
  498. /* Make sure the request wasn't just sitting around signalling
  499. * device condition (if the request handle is NULL, then the
  500. * request completed but needed additional handling here).
  501. */
  502. if (!test_bit(IREQ_TERMINATED, &isci_request->flags)) {
  503. was_terminated = true;
  504. needs_cleanup_handling = true;
  505. status = sci_controller_terminate_request(ihost,
  506. idev,
  507. isci_request);
  508. }
  509. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  510. /*
  511. * The only time the request to terminate will
  512. * fail is when the io request is completed and
  513. * being aborted.
  514. */
  515. if (status != SCI_SUCCESS) {
  516. dev_dbg(&ihost->pdev->dev,
  517. "%s: sci_controller_terminate_request"
  518. " returned = 0x%x\n",
  519. __func__, status);
  520. isci_request->io_request_completion = NULL;
  521. } else {
  522. if (was_terminated) {
  523. dev_dbg(&ihost->pdev->dev,
  524. "%s: before completion wait (%p/%p)\n",
  525. __func__, isci_request, io_request_completion);
  526. /* Wait here for the request to complete. */
  527. #define TERMINATION_TIMEOUT_MSEC 500
  528. termination_completed
  529. = wait_for_completion_timeout(
  530. io_request_completion,
  531. msecs_to_jiffies(TERMINATION_TIMEOUT_MSEC));
  532. if (!termination_completed) {
  533. /* The request to terminate has timed out. */
  534. spin_lock_irqsave(&ihost->scic_lock,
  535. flags);
  536. /* Check for state changes. */
  537. if (!test_bit(IREQ_TERMINATED, &isci_request->flags)) {
  538. /* The best we can do is to have the
  539. * request die a silent death if it
  540. * ever really completes.
  541. *
  542. * Set the request state to "dead",
  543. * and clear the task pointer so that
  544. * an actual completion event callback
  545. * doesn't do anything.
  546. */
  547. isci_request->status = dead;
  548. isci_request->io_request_completion
  549. = NULL;
  550. if (isci_request->ttype == io_task) {
  551. /* Break links with the
  552. * sas_task.
  553. */
  554. isci_request->ttype_ptr.io_task_ptr
  555. = NULL;
  556. }
  557. } else
  558. termination_completed = 1;
  559. spin_unlock_irqrestore(&ihost->scic_lock,
  560. flags);
  561. if (!termination_completed) {
  562. dev_dbg(&ihost->pdev->dev,
  563. "%s: *** Timeout waiting for "
  564. "termination(%p/%p)\n",
  565. __func__, io_request_completion,
  566. isci_request);
  567. /* The request can no longer be referenced
  568. * safely since it may go away if the
  569. * termination every really does complete.
  570. */
  571. isci_request = NULL;
  572. }
  573. }
  574. if (termination_completed)
  575. dev_dbg(&ihost->pdev->dev,
  576. "%s: after completion wait (%p/%p)\n",
  577. __func__, isci_request, io_request_completion);
  578. }
  579. if (termination_completed) {
  580. isci_request->io_request_completion = NULL;
  581. /* Peek at the status of the request. This will tell
  582. * us if there was special handling on the request such that it
  583. * needs to be detached and freed here.
  584. */
  585. spin_lock_irqsave(&isci_request->state_lock, flags);
  586. request_status = isci_request->status;
  587. if ((isci_request->ttype == io_task) /* TMFs are in their own thread */
  588. && ((request_status == aborted)
  589. || (request_status == aborting)
  590. || (request_status == terminating)
  591. || (request_status == completed)
  592. || (request_status == dead)
  593. )
  594. ) {
  595. /* The completion routine won't free a request in
  596. * the aborted/aborting/etc. states, so we do
  597. * it here.
  598. */
  599. needs_cleanup_handling = true;
  600. }
  601. spin_unlock_irqrestore(&isci_request->state_lock, flags);
  602. }
  603. if (needs_cleanup_handling)
  604. isci_request_cleanup_completed_loiterer(
  605. ihost, idev, isci_request, task);
  606. }
  607. }
  608. /**
  609. * isci_terminate_pending_requests() - This function will change the all of the
  610. * requests on the given device's state to "aborting", will terminate the
  611. * requests, and wait for them to complete. This function must only be
  612. * called from a thread that can wait. Note that the requests are all
  613. * terminated and completed (back to the host, if started there).
  614. * @isci_host: This parameter specifies SCU.
  615. * @idev: This parameter specifies the target.
  616. *
  617. */
  618. void isci_terminate_pending_requests(struct isci_host *ihost,
  619. struct isci_remote_device *idev)
  620. {
  621. struct completion request_completion;
  622. enum isci_request_status old_state;
  623. unsigned long flags;
  624. LIST_HEAD(list);
  625. spin_lock_irqsave(&ihost->scic_lock, flags);
  626. list_splice_init(&idev->reqs_in_process, &list);
  627. /* assumes that isci_terminate_request_core deletes from the list */
  628. while (!list_empty(&list)) {
  629. struct isci_request *ireq = list_entry(list.next, typeof(*ireq), dev_node);
  630. /* Change state to "terminating" if it is currently
  631. * "started".
  632. */
  633. old_state = isci_request_change_started_to_newstate(ireq,
  634. &request_completion,
  635. terminating);
  636. switch (old_state) {
  637. case started:
  638. case completed:
  639. case aborting:
  640. break;
  641. default:
  642. /* termination in progress, or otherwise dispositioned.
  643. * We know the request was on 'list' so should be safe
  644. * to move it back to reqs_in_process
  645. */
  646. list_move(&ireq->dev_node, &idev->reqs_in_process);
  647. ireq = NULL;
  648. break;
  649. }
  650. if (!ireq)
  651. continue;
  652. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  653. init_completion(&request_completion);
  654. dev_dbg(&ihost->pdev->dev,
  655. "%s: idev=%p request=%p; task=%p old_state=%d\n",
  656. __func__, idev, ireq,
  657. ireq->ttype == io_task ? isci_request_access_task(ireq) : NULL,
  658. old_state);
  659. /* If the old_state is started:
  660. * This request was not already being aborted. If it had been,
  661. * then the aborting I/O (ie. the TMF request) would not be in
  662. * the aborting state, and thus would be terminated here. Note
  663. * that since the TMF completion's call to the kernel function
  664. * "complete()" does not happen until the pending I/O request
  665. * terminate fully completes, we do not have to implement a
  666. * special wait here for already aborting requests - the
  667. * termination of the TMF request will force the request
  668. * to finish it's already started terminate.
  669. *
  670. * If old_state == completed:
  671. * This request completed from the SCU hardware perspective
  672. * and now just needs cleaning up in terms of freeing the
  673. * request and potentially calling up to libsas.
  674. *
  675. * If old_state == aborting:
  676. * This request has already gone through a TMF timeout, but may
  677. * not have been terminated; needs cleaning up at least.
  678. */
  679. isci_terminate_request_core(ihost, idev, ireq);
  680. spin_lock_irqsave(&ihost->scic_lock, flags);
  681. }
  682. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  683. }
  684. /**
  685. * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
  686. * Template functions.
  687. * @lun: This parameter specifies the lun to be reset.
  688. *
  689. * status, zero indicates success.
  690. */
  691. static int isci_task_send_lu_reset_sas(
  692. struct isci_host *isci_host,
  693. struct isci_remote_device *isci_device,
  694. u8 *lun)
  695. {
  696. struct isci_tmf tmf;
  697. int ret = TMF_RESP_FUNC_FAILED;
  698. dev_dbg(&isci_host->pdev->dev,
  699. "%s: isci_host = %p, isci_device = %p\n",
  700. __func__, isci_host, isci_device);
  701. /* Send the LUN reset to the target. By the time the call returns,
  702. * the TMF has fully exected in the target (in which case the return
  703. * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
  704. * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
  705. */
  706. isci_task_build_tmf(&tmf, isci_tmf_ssp_lun_reset, NULL, NULL);
  707. #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
  708. ret = isci_task_execute_tmf(isci_host, isci_device, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
  709. if (ret == TMF_RESP_FUNC_COMPLETE)
  710. dev_dbg(&isci_host->pdev->dev,
  711. "%s: %p: TMF_LU_RESET passed\n",
  712. __func__, isci_device);
  713. else
  714. dev_dbg(&isci_host->pdev->dev,
  715. "%s: %p: TMF_LU_RESET failed (%x)\n",
  716. __func__, isci_device, ret);
  717. return ret;
  718. }
  719. static int isci_task_send_lu_reset_sata(struct isci_host *ihost,
  720. struct isci_remote_device *idev, u8 *lun)
  721. {
  722. int ret = TMF_RESP_FUNC_FAILED;
  723. struct isci_tmf tmf;
  724. /* Send the soft reset to the target */
  725. #define ISCI_SRST_TIMEOUT_MS 25000 /* 25 second timeout. */
  726. isci_task_build_tmf(&tmf, isci_tmf_sata_srst_high, NULL, NULL);
  727. ret = isci_task_execute_tmf(ihost, idev, &tmf, ISCI_SRST_TIMEOUT_MS);
  728. if (ret != TMF_RESP_FUNC_COMPLETE) {
  729. dev_dbg(&ihost->pdev->dev,
  730. "%s: Assert SRST failed (%p) = %x",
  731. __func__, idev, ret);
  732. /* Return the failure so that the LUN reset is escalated
  733. * to a target reset.
  734. */
  735. }
  736. return ret;
  737. }
  738. /**
  739. * isci_task_lu_reset() - This function is one of the SAS Domain Template
  740. * functions. This is one of the Task Management functoins called by libsas,
  741. * to reset the given lun. Note the assumption that while this call is
  742. * executing, no I/O will be sent by the host to the device.
  743. * @lun: This parameter specifies the lun to be reset.
  744. *
  745. * status, zero indicates success.
  746. */
  747. int isci_task_lu_reset(struct domain_device *domain_device, u8 *lun)
  748. {
  749. struct isci_host *isci_host = dev_to_ihost(domain_device);
  750. struct isci_remote_device *isci_device;
  751. unsigned long flags;
  752. int ret;
  753. spin_lock_irqsave(&isci_host->scic_lock, flags);
  754. isci_device = isci_lookup_device(domain_device);
  755. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  756. dev_dbg(&isci_host->pdev->dev,
  757. "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
  758. __func__, domain_device, isci_host, isci_device);
  759. if (isci_device)
  760. set_bit(IDEV_EH, &isci_device->flags);
  761. /* If there is a device reset pending on any request in the
  762. * device's list, fail this LUN reset request in order to
  763. * escalate to the device reset.
  764. */
  765. if (!isci_device ||
  766. isci_device_is_reset_pending(isci_host, isci_device)) {
  767. dev_dbg(&isci_host->pdev->dev,
  768. "%s: No dev (%p), or "
  769. "RESET PENDING: domain_device=%p\n",
  770. __func__, isci_device, domain_device);
  771. ret = TMF_RESP_FUNC_FAILED;
  772. goto out;
  773. }
  774. /* Send the task management part of the reset. */
  775. if (sas_protocol_ata(domain_device->tproto)) {
  776. ret = isci_task_send_lu_reset_sata(isci_host, isci_device, lun);
  777. } else
  778. ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
  779. /* If the LUN reset worked, all the I/O can now be terminated. */
  780. if (ret == TMF_RESP_FUNC_COMPLETE)
  781. /* Terminate all I/O now. */
  782. isci_terminate_pending_requests(isci_host,
  783. isci_device);
  784. out:
  785. isci_put_device(isci_device);
  786. return ret;
  787. }
  788. /* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
  789. int isci_task_clear_nexus_port(struct asd_sas_port *port)
  790. {
  791. return TMF_RESP_FUNC_FAILED;
  792. }
  793. int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
  794. {
  795. return TMF_RESP_FUNC_FAILED;
  796. }
  797. /* Task Management Functions. Must be called from process context. */
  798. /**
  799. * isci_abort_task_process_cb() - This is a helper function for the abort task
  800. * TMF command. It manages the request state with respect to the successful
  801. * transmission / completion of the abort task request.
  802. * @cb_state: This parameter specifies when this function was called - after
  803. * the TMF request has been started and after it has timed-out.
  804. * @tmf: This parameter specifies the TMF in progress.
  805. *
  806. *
  807. */
  808. static void isci_abort_task_process_cb(
  809. enum isci_tmf_cb_state cb_state,
  810. struct isci_tmf *tmf,
  811. void *cb_data)
  812. {
  813. struct isci_request *old_request;
  814. old_request = (struct isci_request *)cb_data;
  815. dev_dbg(&old_request->isci_host->pdev->dev,
  816. "%s: tmf=%p, old_request=%p\n",
  817. __func__, tmf, old_request);
  818. switch (cb_state) {
  819. case isci_tmf_started:
  820. /* The TMF has been started. Nothing to do here, since the
  821. * request state was already set to "aborted" by the abort
  822. * task function.
  823. */
  824. if ((old_request->status != aborted)
  825. && (old_request->status != completed))
  826. dev_dbg(&old_request->isci_host->pdev->dev,
  827. "%s: Bad request status (%d): tmf=%p, old_request=%p\n",
  828. __func__, old_request->status, tmf, old_request);
  829. break;
  830. case isci_tmf_timed_out:
  831. /* Set the task's state to "aborting", since the abort task
  832. * function thread set it to "aborted" (above) in anticipation
  833. * of the task management request working correctly. Since the
  834. * timeout has now fired, the TMF request failed. We set the
  835. * state such that the request completion will indicate the
  836. * device is no longer present.
  837. */
  838. isci_request_change_state(old_request, aborting);
  839. break;
  840. default:
  841. dev_dbg(&old_request->isci_host->pdev->dev,
  842. "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
  843. __func__, cb_state, tmf, old_request);
  844. break;
  845. }
  846. }
  847. /**
  848. * isci_task_abort_task() - This function is one of the SAS Domain Template
  849. * functions. This function is called by libsas to abort a specified task.
  850. * @task: This parameter specifies the SAS task to abort.
  851. *
  852. * status, zero indicates success.
  853. */
  854. int isci_task_abort_task(struct sas_task *task)
  855. {
  856. struct isci_host *isci_host = dev_to_ihost(task->dev);
  857. DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
  858. struct isci_request *old_request = NULL;
  859. enum isci_request_status old_state;
  860. struct isci_remote_device *isci_device = NULL;
  861. struct isci_tmf tmf;
  862. int ret = TMF_RESP_FUNC_FAILED;
  863. unsigned long flags;
  864. bool any_dev_reset = false;
  865. /* Get the isci_request reference from the task. Note that
  866. * this check does not depend on the pending request list
  867. * in the device, because tasks driving resets may land here
  868. * after completion in the core.
  869. */
  870. spin_lock_irqsave(&isci_host->scic_lock, flags);
  871. spin_lock(&task->task_state_lock);
  872. old_request = task->lldd_task;
  873. /* If task is already done, the request isn't valid */
  874. if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
  875. (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
  876. old_request)
  877. isci_device = isci_lookup_device(task->dev);
  878. spin_unlock(&task->task_state_lock);
  879. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  880. dev_dbg(&isci_host->pdev->dev,
  881. "%s: task = %p\n", __func__, task);
  882. if (!isci_device || !old_request)
  883. goto out;
  884. set_bit(IDEV_EH, &isci_device->flags);
  885. /* This version of the driver will fail abort requests for
  886. * SATA/STP. Failing the abort request this way will cause the
  887. * SCSI error handler thread to escalate to LUN reset
  888. */
  889. if (sas_protocol_ata(task->task_proto)) {
  890. dev_dbg(&isci_host->pdev->dev,
  891. " task %p is for a STP/SATA device;"
  892. " returning TMF_RESP_FUNC_FAILED\n"
  893. " to cause a LUN reset...\n", task);
  894. goto out;
  895. }
  896. dev_dbg(&isci_host->pdev->dev,
  897. "%s: old_request == %p\n", __func__, old_request);
  898. any_dev_reset = isci_device_is_reset_pending(isci_host, isci_device);
  899. spin_lock_irqsave(&task->task_state_lock, flags);
  900. any_dev_reset = any_dev_reset || (task->task_state_flags & SAS_TASK_NEED_DEV_RESET);
  901. /* If the extraction of the request reference from the task
  902. * failed, then the request has been completed (or if there is a
  903. * pending reset then this abort request function must be failed
  904. * in order to escalate to the target reset).
  905. */
  906. if ((old_request == NULL) || any_dev_reset) {
  907. /* If the device reset task flag is set, fail the task
  908. * management request. Otherwise, the original request
  909. * has completed.
  910. */
  911. if (any_dev_reset) {
  912. /* Turn off the task's DONE to make sure this
  913. * task is escalated to a target reset.
  914. */
  915. task->task_state_flags &= ~SAS_TASK_STATE_DONE;
  916. /* Make the reset happen as soon as possible. */
  917. task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
  918. spin_unlock_irqrestore(&task->task_state_lock, flags);
  919. /* Fail the task management request in order to
  920. * escalate to the target reset.
  921. */
  922. ret = TMF_RESP_FUNC_FAILED;
  923. dev_dbg(&isci_host->pdev->dev,
  924. "%s: Failing task abort in order to "
  925. "escalate to target reset because\n"
  926. "SAS_TASK_NEED_DEV_RESET is set for "
  927. "task %p on dev %p\n",
  928. __func__, task, isci_device);
  929. } else {
  930. /* The request has already completed and there
  931. * is nothing to do here other than to set the task
  932. * done bit, and indicate that the task abort function
  933. * was sucessful.
  934. */
  935. isci_set_task_doneflags(task);
  936. spin_unlock_irqrestore(&task->task_state_lock, flags);
  937. ret = TMF_RESP_FUNC_COMPLETE;
  938. dev_dbg(&isci_host->pdev->dev,
  939. "%s: abort task not needed for %p\n",
  940. __func__, task);
  941. }
  942. goto out;
  943. } else {
  944. spin_unlock_irqrestore(&task->task_state_lock, flags);
  945. }
  946. spin_lock_irqsave(&isci_host->scic_lock, flags);
  947. /* Check the request status and change to "aborted" if currently
  948. * "starting"; if true then set the I/O kernel completion
  949. * struct that will be triggered when the request completes.
  950. */
  951. old_state = isci_task_validate_request_to_abort(
  952. old_request, isci_host, isci_device,
  953. &aborted_io_completion);
  954. if ((old_state != started) &&
  955. (old_state != completed) &&
  956. (old_state != aborting)) {
  957. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  958. /* The request was already being handled by someone else (because
  959. * they got to set the state away from started).
  960. */
  961. dev_dbg(&isci_host->pdev->dev,
  962. "%s: device = %p; old_request %p already being aborted\n",
  963. __func__,
  964. isci_device, old_request);
  965. ret = TMF_RESP_FUNC_COMPLETE;
  966. goto out;
  967. }
  968. if (task->task_proto == SAS_PROTOCOL_SMP ||
  969. test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags)) {
  970. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  971. dev_dbg(&isci_host->pdev->dev,
  972. "%s: SMP request (%d)"
  973. " or complete_in_target (%d), thus no TMF\n",
  974. __func__, (task->task_proto == SAS_PROTOCOL_SMP),
  975. test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags));
  976. /* Set the state on the task. */
  977. isci_task_all_done(task);
  978. ret = TMF_RESP_FUNC_COMPLETE;
  979. /* Stopping and SMP devices are not sent a TMF, and are not
  980. * reset, but the outstanding I/O request is terminated below.
  981. */
  982. } else {
  983. /* Fill in the tmf stucture */
  984. isci_task_build_abort_task_tmf(&tmf, isci_tmf_ssp_task_abort,
  985. isci_abort_task_process_cb,
  986. old_request);
  987. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  988. #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* half second timeout. */
  989. ret = isci_task_execute_tmf(isci_host, isci_device, &tmf,
  990. ISCI_ABORT_TASK_TIMEOUT_MS);
  991. if (ret != TMF_RESP_FUNC_COMPLETE)
  992. dev_dbg(&isci_host->pdev->dev,
  993. "%s: isci_task_send_tmf failed\n",
  994. __func__);
  995. }
  996. if (ret == TMF_RESP_FUNC_COMPLETE) {
  997. set_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags);
  998. /* Clean up the request on our side, and wait for the aborted
  999. * I/O to complete.
  1000. */
  1001. isci_terminate_request_core(isci_host, isci_device, old_request);
  1002. }
  1003. /* Make sure we do not leave a reference to aborted_io_completion */
  1004. old_request->io_request_completion = NULL;
  1005. out:
  1006. isci_put_device(isci_device);
  1007. return ret;
  1008. }
  1009. /**
  1010. * isci_task_abort_task_set() - This function is one of the SAS Domain Template
  1011. * functions. This is one of the Task Management functoins called by libsas,
  1012. * to abort all task for the given lun.
  1013. * @d_device: This parameter specifies the domain device associated with this
  1014. * request.
  1015. * @lun: This parameter specifies the lun associated with this request.
  1016. *
  1017. * status, zero indicates success.
  1018. */
  1019. int isci_task_abort_task_set(
  1020. struct domain_device *d_device,
  1021. u8 *lun)
  1022. {
  1023. return TMF_RESP_FUNC_FAILED;
  1024. }
  1025. /**
  1026. * isci_task_clear_aca() - This function is one of the SAS Domain Template
  1027. * functions. This is one of the Task Management functoins called by libsas.
  1028. * @d_device: This parameter specifies the domain device associated with this
  1029. * request.
  1030. * @lun: This parameter specifies the lun associated with this request.
  1031. *
  1032. * status, zero indicates success.
  1033. */
  1034. int isci_task_clear_aca(
  1035. struct domain_device *d_device,
  1036. u8 *lun)
  1037. {
  1038. return TMF_RESP_FUNC_FAILED;
  1039. }
  1040. /**
  1041. * isci_task_clear_task_set() - This function is one of the SAS Domain Template
  1042. * functions. This is one of the Task Management functoins called by libsas.
  1043. * @d_device: This parameter specifies the domain device associated with this
  1044. * request.
  1045. * @lun: This parameter specifies the lun associated with this request.
  1046. *
  1047. * status, zero indicates success.
  1048. */
  1049. int isci_task_clear_task_set(
  1050. struct domain_device *d_device,
  1051. u8 *lun)
  1052. {
  1053. return TMF_RESP_FUNC_FAILED;
  1054. }
  1055. /**
  1056. * isci_task_query_task() - This function is implemented to cause libsas to
  1057. * correctly escalate the failed abort to a LUN or target reset (this is
  1058. * because sas_scsi_find_task libsas function does not correctly interpret
  1059. * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
  1060. * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
  1061. * returned, libsas will turn this into a target reset
  1062. * @task: This parameter specifies the sas task being queried.
  1063. * @lun: This parameter specifies the lun associated with this request.
  1064. *
  1065. * status, zero indicates success.
  1066. */
  1067. int isci_task_query_task(
  1068. struct sas_task *task)
  1069. {
  1070. /* See if there is a pending device reset for this device. */
  1071. if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
  1072. return TMF_RESP_FUNC_FAILED;
  1073. else
  1074. return TMF_RESP_FUNC_SUCC;
  1075. }
  1076. /*
  1077. * isci_task_request_complete() - This function is called by the sci core when
  1078. * an task request completes.
  1079. * @ihost: This parameter specifies the ISCI host object
  1080. * @ireq: This parameter is the completed isci_request object.
  1081. * @completion_status: This parameter specifies the completion status from the
  1082. * sci core.
  1083. *
  1084. * none.
  1085. */
  1086. void
  1087. isci_task_request_complete(struct isci_host *ihost,
  1088. struct isci_request *ireq,
  1089. enum sci_task_status completion_status)
  1090. {
  1091. struct isci_tmf *tmf = isci_request_access_tmf(ireq);
  1092. struct completion *tmf_complete;
  1093. dev_dbg(&ihost->pdev->dev,
  1094. "%s: request = %p, status=%d\n",
  1095. __func__, ireq, completion_status);
  1096. isci_request_change_state(ireq, completed);
  1097. tmf->status = completion_status;
  1098. set_bit(IREQ_COMPLETE_IN_TARGET, &ireq->flags);
  1099. if (tmf->proto == SAS_PROTOCOL_SSP) {
  1100. memcpy(&tmf->resp.resp_iu,
  1101. &ireq->ssp.rsp,
  1102. SSP_RESP_IU_MAX_SIZE);
  1103. } else if (tmf->proto == SAS_PROTOCOL_SATA) {
  1104. memcpy(&tmf->resp.d2h_fis,
  1105. &ireq->stp.rsp,
  1106. sizeof(struct dev_to_host_fis));
  1107. }
  1108. /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
  1109. tmf_complete = tmf->complete;
  1110. sci_controller_complete_io(ihost, ireq->target_device, ireq);
  1111. /* set the 'terminated' flag handle to make sure it cannot be terminated
  1112. * or completed again.
  1113. */
  1114. set_bit(IREQ_TERMINATED, &ireq->flags);
  1115. isci_request_change_state(ireq, unallocated);
  1116. list_del_init(&ireq->dev_node);
  1117. /* The task management part completes last. */
  1118. complete(tmf_complete);
  1119. }
  1120. static void isci_smp_task_timedout(unsigned long _task)
  1121. {
  1122. struct sas_task *task = (void *) _task;
  1123. unsigned long flags;
  1124. spin_lock_irqsave(&task->task_state_lock, flags);
  1125. if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
  1126. task->task_state_flags |= SAS_TASK_STATE_ABORTED;
  1127. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1128. complete(&task->completion);
  1129. }
  1130. static void isci_smp_task_done(struct sas_task *task)
  1131. {
  1132. if (!del_timer(&task->timer))
  1133. return;
  1134. complete(&task->completion);
  1135. }
  1136. static struct sas_task *isci_alloc_task(void)
  1137. {
  1138. struct sas_task *task = kzalloc(sizeof(*task), GFP_KERNEL);
  1139. if (task) {
  1140. INIT_LIST_HEAD(&task->list);
  1141. spin_lock_init(&task->task_state_lock);
  1142. task->task_state_flags = SAS_TASK_STATE_PENDING;
  1143. init_timer(&task->timer);
  1144. init_completion(&task->completion);
  1145. }
  1146. return task;
  1147. }
  1148. static void isci_free_task(struct isci_host *ihost, struct sas_task *task)
  1149. {
  1150. if (task) {
  1151. BUG_ON(!list_empty(&task->list));
  1152. kfree(task);
  1153. }
  1154. }
  1155. static int isci_smp_execute_task(struct isci_host *ihost,
  1156. struct domain_device *dev, void *req,
  1157. int req_size, void *resp, int resp_size)
  1158. {
  1159. int res, retry;
  1160. struct sas_task *task = NULL;
  1161. for (retry = 0; retry < 3; retry++) {
  1162. task = isci_alloc_task();
  1163. if (!task)
  1164. return -ENOMEM;
  1165. task->dev = dev;
  1166. task->task_proto = dev->tproto;
  1167. sg_init_one(&task->smp_task.smp_req, req, req_size);
  1168. sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
  1169. task->task_done = isci_smp_task_done;
  1170. task->timer.data = (unsigned long) task;
  1171. task->timer.function = isci_smp_task_timedout;
  1172. task->timer.expires = jiffies + 10*HZ;
  1173. add_timer(&task->timer);
  1174. res = isci_task_execute_task(task, 1, GFP_KERNEL);
  1175. if (res) {
  1176. del_timer(&task->timer);
  1177. dev_dbg(&ihost->pdev->dev,
  1178. "%s: executing SMP task failed:%d\n",
  1179. __func__, res);
  1180. goto ex_err;
  1181. }
  1182. wait_for_completion(&task->completion);
  1183. res = -ECOMM;
  1184. if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  1185. dev_dbg(&ihost->pdev->dev,
  1186. "%s: smp task timed out or aborted\n",
  1187. __func__);
  1188. isci_task_abort_task(task);
  1189. if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
  1190. dev_dbg(&ihost->pdev->dev,
  1191. "%s: SMP task aborted and not done\n",
  1192. __func__);
  1193. goto ex_err;
  1194. }
  1195. }
  1196. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  1197. task->task_status.stat == SAM_STAT_GOOD) {
  1198. res = 0;
  1199. break;
  1200. }
  1201. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  1202. task->task_status.stat == SAS_DATA_UNDERRUN) {
  1203. /* no error, but return the number of bytes of
  1204. * underrun */
  1205. res = task->task_status.residual;
  1206. break;
  1207. }
  1208. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  1209. task->task_status.stat == SAS_DATA_OVERRUN) {
  1210. res = -EMSGSIZE;
  1211. break;
  1212. } else {
  1213. dev_dbg(&ihost->pdev->dev,
  1214. "%s: task to dev %016llx response: 0x%x "
  1215. "status 0x%x\n", __func__,
  1216. SAS_ADDR(dev->sas_addr),
  1217. task->task_status.resp,
  1218. task->task_status.stat);
  1219. isci_free_task(ihost, task);
  1220. task = NULL;
  1221. }
  1222. }
  1223. ex_err:
  1224. BUG_ON(retry == 3 && task != NULL);
  1225. isci_free_task(ihost, task);
  1226. return res;
  1227. }
  1228. #define DISCOVER_REQ_SIZE 16
  1229. #define DISCOVER_RESP_SIZE 56
  1230. int isci_smp_get_phy_attached_dev_type(struct isci_host *ihost,
  1231. struct domain_device *dev,
  1232. int phy_id, int *adt)
  1233. {
  1234. struct smp_resp *disc_resp;
  1235. u8 *disc_req;
  1236. int res;
  1237. disc_resp = kzalloc(DISCOVER_RESP_SIZE, GFP_KERNEL);
  1238. if (!disc_resp)
  1239. return -ENOMEM;
  1240. disc_req = kzalloc(DISCOVER_REQ_SIZE, GFP_KERNEL);
  1241. if (disc_req) {
  1242. disc_req[0] = SMP_REQUEST;
  1243. disc_req[1] = SMP_DISCOVER;
  1244. disc_req[9] = phy_id;
  1245. } else {
  1246. kfree(disc_resp);
  1247. return -ENOMEM;
  1248. }
  1249. res = isci_smp_execute_task(ihost, dev, disc_req, DISCOVER_REQ_SIZE,
  1250. disc_resp, DISCOVER_RESP_SIZE);
  1251. if (!res) {
  1252. if (disc_resp->result != SMP_RESP_FUNC_ACC)
  1253. res = disc_resp->result;
  1254. else
  1255. *adt = disc_resp->disc.attached_dev_type;
  1256. }
  1257. kfree(disc_req);
  1258. kfree(disc_resp);
  1259. return res;
  1260. }
  1261. static void isci_wait_for_smp_phy_reset(struct isci_remote_device *idev, int phy_num)
  1262. {
  1263. struct domain_device *dev = idev->domain_dev;
  1264. struct isci_port *iport = idev->isci_port;
  1265. struct isci_host *ihost = iport->isci_host;
  1266. int res, iteration = 0, attached_device_type;
  1267. #define STP_WAIT_MSECS 25000
  1268. unsigned long tmo = msecs_to_jiffies(STP_WAIT_MSECS);
  1269. unsigned long deadline = jiffies + tmo;
  1270. enum {
  1271. SMP_PHYWAIT_PHYDOWN,
  1272. SMP_PHYWAIT_PHYUP,
  1273. SMP_PHYWAIT_DONE
  1274. } phy_state = SMP_PHYWAIT_PHYDOWN;
  1275. /* While there is time, wait for the phy to go away and come back */
  1276. while (time_is_after_jiffies(deadline) && phy_state != SMP_PHYWAIT_DONE) {
  1277. int event = atomic_read(&iport->event);
  1278. ++iteration;
  1279. tmo = wait_event_timeout(ihost->eventq,
  1280. event != atomic_read(&iport->event) ||
  1281. !test_bit(IPORT_BCN_BLOCKED, &iport->flags),
  1282. tmo);
  1283. /* link down, stop polling */
  1284. if (!test_bit(IPORT_BCN_BLOCKED, &iport->flags))
  1285. break;
  1286. dev_dbg(&ihost->pdev->dev,
  1287. "%s: iport %p, iteration %d,"
  1288. " phase %d: time_remaining %lu, bcns = %d\n",
  1289. __func__, iport, iteration, phy_state,
  1290. tmo, test_bit(IPORT_BCN_PENDING, &iport->flags));
  1291. res = isci_smp_get_phy_attached_dev_type(ihost, dev, phy_num,
  1292. &attached_device_type);
  1293. tmo = deadline - jiffies;
  1294. if (res) {
  1295. dev_dbg(&ihost->pdev->dev,
  1296. "%s: iteration %d, phase %d:"
  1297. " SMP error=%d, time_remaining=%lu\n",
  1298. __func__, iteration, phy_state, res, tmo);
  1299. break;
  1300. }
  1301. dev_dbg(&ihost->pdev->dev,
  1302. "%s: iport %p, iteration %d,"
  1303. " phase %d: time_remaining %lu, bcns = %d, "
  1304. "attdevtype = %x\n",
  1305. __func__, iport, iteration, phy_state,
  1306. tmo, test_bit(IPORT_BCN_PENDING, &iport->flags),
  1307. attached_device_type);
  1308. switch (phy_state) {
  1309. case SMP_PHYWAIT_PHYDOWN:
  1310. /* Has the device gone away? */
  1311. if (!attached_device_type)
  1312. phy_state = SMP_PHYWAIT_PHYUP;
  1313. break;
  1314. case SMP_PHYWAIT_PHYUP:
  1315. /* Has the device come back? */
  1316. if (attached_device_type)
  1317. phy_state = SMP_PHYWAIT_DONE;
  1318. break;
  1319. case SMP_PHYWAIT_DONE:
  1320. break;
  1321. }
  1322. }
  1323. dev_dbg(&ihost->pdev->dev, "%s: done\n", __func__);
  1324. }
  1325. static int isci_reset_device(struct isci_host *ihost,
  1326. struct isci_remote_device *idev)
  1327. {
  1328. struct sas_phy *phy = sas_find_local_phy(idev->domain_dev);
  1329. struct isci_port *iport = idev->isci_port;
  1330. enum sci_status status;
  1331. unsigned long flags;
  1332. int rc;
  1333. dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
  1334. spin_lock_irqsave(&ihost->scic_lock, flags);
  1335. status = sci_remote_device_reset(idev);
  1336. if (status != SCI_SUCCESS) {
  1337. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1338. dev_dbg(&ihost->pdev->dev,
  1339. "%s: sci_remote_device_reset(%p) returned %d!\n",
  1340. __func__, idev, status);
  1341. return TMF_RESP_FUNC_FAILED;
  1342. }
  1343. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1344. /* Make sure all pending requests are able to be fully terminated. */
  1345. isci_device_clear_reset_pending(ihost, idev);
  1346. /* If this is a device on an expander, disable BCN processing. */
  1347. if (!scsi_is_sas_phy_local(phy))
  1348. set_bit(IPORT_BCN_BLOCKED, &iport->flags);
  1349. rc = sas_phy_reset(phy, true);
  1350. /* Terminate in-progress I/O now. */
  1351. isci_remote_device_nuke_requests(ihost, idev);
  1352. /* Since all pending TCs have been cleaned, resume the RNC. */
  1353. spin_lock_irqsave(&ihost->scic_lock, flags);
  1354. status = sci_remote_device_reset_complete(idev);
  1355. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1356. /* If this is a device on an expander, bring the phy back up. */
  1357. if (!scsi_is_sas_phy_local(phy)) {
  1358. /* A phy reset will cause the device to go away then reappear.
  1359. * Since libsas will take action on incoming BCNs (eg. remove
  1360. * a device going through an SMP phy-control driven reset),
  1361. * we need to wait until the phy comes back up before letting
  1362. * discovery proceed in libsas.
  1363. */
  1364. isci_wait_for_smp_phy_reset(idev, phy->number);
  1365. spin_lock_irqsave(&ihost->scic_lock, flags);
  1366. isci_port_bcn_enable(ihost, idev->isci_port);
  1367. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1368. }
  1369. if (status != SCI_SUCCESS) {
  1370. dev_dbg(&ihost->pdev->dev,
  1371. "%s: sci_remote_device_reset_complete(%p) "
  1372. "returned %d!\n", __func__, idev, status);
  1373. }
  1374. dev_dbg(&ihost->pdev->dev, "%s: idev %p complete.\n", __func__, idev);
  1375. return rc;
  1376. }
  1377. int isci_task_I_T_nexus_reset(struct domain_device *dev)
  1378. {
  1379. struct isci_host *ihost = dev_to_ihost(dev);
  1380. struct isci_remote_device *idev;
  1381. unsigned long flags;
  1382. int ret;
  1383. spin_lock_irqsave(&ihost->scic_lock, flags);
  1384. idev = isci_lookup_device(dev);
  1385. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1386. if (!idev || !test_bit(IDEV_EH, &idev->flags)) {
  1387. ret = TMF_RESP_FUNC_COMPLETE;
  1388. goto out;
  1389. }
  1390. ret = isci_reset_device(ihost, idev);
  1391. out:
  1392. isci_put_device(idev);
  1393. return ret;
  1394. }
  1395. int isci_bus_reset_handler(struct scsi_cmnd *cmd)
  1396. {
  1397. struct domain_device *dev = sdev_to_domain_dev(cmd->device);
  1398. struct isci_host *ihost = dev_to_ihost(dev);
  1399. struct isci_remote_device *idev;
  1400. unsigned long flags;
  1401. int ret;
  1402. spin_lock_irqsave(&ihost->scic_lock, flags);
  1403. idev = isci_lookup_device(dev);
  1404. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1405. if (!idev) {
  1406. ret = TMF_RESP_FUNC_COMPLETE;
  1407. goto out;
  1408. }
  1409. ret = isci_reset_device(ihost, idev);
  1410. out:
  1411. isci_put_device(idev);
  1412. return ret;
  1413. }