task.c 48 KB

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