task.c 50 KB

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