task.c 47 KB

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