task.c 47 KB

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