task.c 47 KB

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