sas_scsi_host.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. /*
  2. * Serial Attached SCSI (SAS) class SCSI Host glue.
  3. *
  4. * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
  5. * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
  6. *
  7. * This file is licensed under GPLv2.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  22. * USA
  23. *
  24. */
  25. #include <linux/kthread.h>
  26. #include <linux/firmware.h>
  27. #include <linux/export.h>
  28. #include <linux/ctype.h>
  29. #include "sas_internal.h"
  30. #include <scsi/scsi_host.h>
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_tcq.h>
  33. #include <scsi/scsi.h>
  34. #include <scsi/scsi_eh.h>
  35. #include <scsi/scsi_transport.h>
  36. #include <scsi/scsi_transport_sas.h>
  37. #include <scsi/sas_ata.h>
  38. #include "../scsi_sas_internal.h"
  39. #include "../scsi_transport_api.h"
  40. #include "../scsi_priv.h"
  41. #include <linux/err.h>
  42. #include <linux/blkdev.h>
  43. #include <linux/freezer.h>
  44. #include <linux/gfp.h>
  45. #include <linux/scatterlist.h>
  46. #include <linux/libata.h>
  47. /* record final status and free the task */
  48. static void sas_end_task(struct scsi_cmnd *sc, struct sas_task *task)
  49. {
  50. struct task_status_struct *ts = &task->task_status;
  51. int hs = 0, stat = 0;
  52. if (ts->resp == SAS_TASK_UNDELIVERED) {
  53. /* transport error */
  54. hs = DID_NO_CONNECT;
  55. } else { /* ts->resp == SAS_TASK_COMPLETE */
  56. /* task delivered, what happened afterwards? */
  57. switch (ts->stat) {
  58. case SAS_DEV_NO_RESPONSE:
  59. case SAS_INTERRUPTED:
  60. case SAS_PHY_DOWN:
  61. case SAS_NAK_R_ERR:
  62. case SAS_OPEN_TO:
  63. hs = DID_NO_CONNECT;
  64. break;
  65. case SAS_DATA_UNDERRUN:
  66. scsi_set_resid(sc, ts->residual);
  67. if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow)
  68. hs = DID_ERROR;
  69. break;
  70. case SAS_DATA_OVERRUN:
  71. hs = DID_ERROR;
  72. break;
  73. case SAS_QUEUE_FULL:
  74. hs = DID_SOFT_ERROR; /* retry */
  75. break;
  76. case SAS_DEVICE_UNKNOWN:
  77. hs = DID_BAD_TARGET;
  78. break;
  79. case SAS_SG_ERR:
  80. hs = DID_PARITY;
  81. break;
  82. case SAS_OPEN_REJECT:
  83. if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
  84. hs = DID_SOFT_ERROR; /* retry */
  85. else
  86. hs = DID_ERROR;
  87. break;
  88. case SAS_PROTO_RESPONSE:
  89. SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
  90. "task; please report this\n",
  91. task->dev->port->ha->sas_ha_name);
  92. break;
  93. case SAS_ABORTED_TASK:
  94. hs = DID_ABORT;
  95. break;
  96. case SAM_STAT_CHECK_CONDITION:
  97. memcpy(sc->sense_buffer, ts->buf,
  98. min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
  99. stat = SAM_STAT_CHECK_CONDITION;
  100. break;
  101. default:
  102. stat = ts->stat;
  103. break;
  104. }
  105. }
  106. sc->result = (hs << 16) | stat;
  107. ASSIGN_SAS_TASK(sc, NULL);
  108. sas_free_task(task);
  109. }
  110. static void sas_scsi_task_done(struct sas_task *task)
  111. {
  112. struct scsi_cmnd *sc = task->uldd_task;
  113. struct domain_device *dev = task->dev;
  114. struct sas_ha_struct *ha = dev->port->ha;
  115. unsigned long flags;
  116. spin_lock_irqsave(&dev->done_lock, flags);
  117. if (test_bit(SAS_HA_FROZEN, &ha->state))
  118. task = NULL;
  119. else
  120. ASSIGN_SAS_TASK(sc, NULL);
  121. spin_unlock_irqrestore(&dev->done_lock, flags);
  122. if (unlikely(!task)) {
  123. /* task will be completed by the error handler */
  124. SAS_DPRINTK("task done but aborted\n");
  125. return;
  126. }
  127. if (unlikely(!sc)) {
  128. SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
  129. sas_free_task(task);
  130. return;
  131. }
  132. sas_end_task(sc, task);
  133. sc->scsi_done(sc);
  134. }
  135. static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
  136. struct domain_device *dev,
  137. gfp_t gfp_flags)
  138. {
  139. struct sas_task *task = sas_alloc_task(gfp_flags);
  140. struct scsi_lun lun;
  141. if (!task)
  142. return NULL;
  143. task->uldd_task = cmd;
  144. ASSIGN_SAS_TASK(cmd, task);
  145. task->dev = dev;
  146. task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
  147. task->ssp_task.retry_count = 1;
  148. int_to_scsilun(cmd->device->lun, &lun);
  149. memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
  150. task->ssp_task.task_attr = TASK_ATTR_SIMPLE;
  151. task->ssp_task.cmd = cmd;
  152. task->scatter = scsi_sglist(cmd);
  153. task->num_scatter = scsi_sg_count(cmd);
  154. task->total_xfer_len = scsi_bufflen(cmd);
  155. task->data_dir = cmd->sc_data_direction;
  156. task->task_done = sas_scsi_task_done;
  157. return task;
  158. }
  159. int sas_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
  160. {
  161. struct sas_internal *i = to_sas_internal(host->transportt);
  162. struct domain_device *dev = cmd_to_domain_dev(cmd);
  163. struct sas_task *task;
  164. int res = 0;
  165. /* If the device fell off, no sense in issuing commands */
  166. if (test_bit(SAS_DEV_GONE, &dev->state)) {
  167. cmd->result = DID_BAD_TARGET << 16;
  168. goto out_done;
  169. }
  170. if (dev_is_sata(dev)) {
  171. spin_lock_irq(dev->sata_dev.ap->lock);
  172. res = ata_sas_queuecmd(cmd, dev->sata_dev.ap);
  173. spin_unlock_irq(dev->sata_dev.ap->lock);
  174. return res;
  175. }
  176. task = sas_create_task(cmd, dev, GFP_ATOMIC);
  177. if (!task)
  178. return SCSI_MLQUEUE_HOST_BUSY;
  179. res = i->dft->lldd_execute_task(task, GFP_ATOMIC);
  180. if (res)
  181. goto out_free_task;
  182. return 0;
  183. out_free_task:
  184. SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
  185. ASSIGN_SAS_TASK(cmd, NULL);
  186. sas_free_task(task);
  187. if (res == -SAS_QUEUE_FULL)
  188. cmd->result = DID_SOFT_ERROR << 16; /* retry */
  189. else
  190. cmd->result = DID_ERROR << 16;
  191. out_done:
  192. cmd->scsi_done(cmd);
  193. return 0;
  194. }
  195. static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
  196. {
  197. struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
  198. struct domain_device *dev = cmd_to_domain_dev(cmd);
  199. struct sas_task *task = TO_SAS_TASK(cmd);
  200. /* At this point, we only get called following an actual abort
  201. * of the task, so we should be guaranteed not to be racing with
  202. * any completions from the LLD. Task is freed after this.
  203. */
  204. sas_end_task(cmd, task);
  205. if (dev_is_sata(dev)) {
  206. /* defer commands to libata so that libata EH can
  207. * handle ata qcs correctly
  208. */
  209. list_move_tail(&cmd->eh_entry, &sas_ha->eh_ata_q);
  210. return;
  211. }
  212. /* now finish the command and move it on to the error
  213. * handler done list, this also takes it off the
  214. * error handler pending list.
  215. */
  216. scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
  217. }
  218. static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
  219. {
  220. struct scsi_cmnd *cmd, *n;
  221. list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
  222. if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
  223. cmd->device->lun == my_cmd->device->lun)
  224. sas_eh_finish_cmd(cmd);
  225. }
  226. }
  227. static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
  228. struct domain_device *dev)
  229. {
  230. struct scsi_cmnd *cmd, *n;
  231. list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
  232. struct domain_device *x = cmd_to_domain_dev(cmd);
  233. if (x == dev)
  234. sas_eh_finish_cmd(cmd);
  235. }
  236. }
  237. static void sas_scsi_clear_queue_port(struct list_head *error_q,
  238. struct asd_sas_port *port)
  239. {
  240. struct scsi_cmnd *cmd, *n;
  241. list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
  242. struct domain_device *dev = cmd_to_domain_dev(cmd);
  243. struct asd_sas_port *x = dev->port;
  244. if (x == port)
  245. sas_eh_finish_cmd(cmd);
  246. }
  247. }
  248. enum task_disposition {
  249. TASK_IS_DONE,
  250. TASK_IS_ABORTED,
  251. TASK_IS_AT_LU,
  252. TASK_IS_NOT_AT_LU,
  253. TASK_ABORT_FAILED,
  254. };
  255. static enum task_disposition sas_scsi_find_task(struct sas_task *task)
  256. {
  257. unsigned long flags;
  258. int i, res;
  259. struct sas_internal *si =
  260. to_sas_internal(task->dev->port->ha->core.shost->transportt);
  261. for (i = 0; i < 5; i++) {
  262. SAS_DPRINTK("%s: aborting task 0x%p\n", __func__, task);
  263. res = si->dft->lldd_abort_task(task);
  264. spin_lock_irqsave(&task->task_state_lock, flags);
  265. if (task->task_state_flags & SAS_TASK_STATE_DONE) {
  266. spin_unlock_irqrestore(&task->task_state_lock, flags);
  267. SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
  268. task);
  269. return TASK_IS_DONE;
  270. }
  271. spin_unlock_irqrestore(&task->task_state_lock, flags);
  272. if (res == TMF_RESP_FUNC_COMPLETE) {
  273. SAS_DPRINTK("%s: task 0x%p is aborted\n",
  274. __func__, task);
  275. return TASK_IS_ABORTED;
  276. } else if (si->dft->lldd_query_task) {
  277. SAS_DPRINTK("%s: querying task 0x%p\n",
  278. __func__, task);
  279. res = si->dft->lldd_query_task(task);
  280. switch (res) {
  281. case TMF_RESP_FUNC_SUCC:
  282. SAS_DPRINTK("%s: task 0x%p at LU\n",
  283. __func__, task);
  284. return TASK_IS_AT_LU;
  285. case TMF_RESP_FUNC_COMPLETE:
  286. SAS_DPRINTK("%s: task 0x%p not at LU\n",
  287. __func__, task);
  288. return TASK_IS_NOT_AT_LU;
  289. case TMF_RESP_FUNC_FAILED:
  290. SAS_DPRINTK("%s: task 0x%p failed to abort\n",
  291. __func__, task);
  292. return TASK_ABORT_FAILED;
  293. }
  294. }
  295. }
  296. return res;
  297. }
  298. static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
  299. {
  300. int res = TMF_RESP_FUNC_FAILED;
  301. struct scsi_lun lun;
  302. struct sas_internal *i =
  303. to_sas_internal(dev->port->ha->core.shost->transportt);
  304. int_to_scsilun(cmd->device->lun, &lun);
  305. SAS_DPRINTK("eh: device %llx LUN %llx has the task\n",
  306. SAS_ADDR(dev->sas_addr),
  307. cmd->device->lun);
  308. if (i->dft->lldd_abort_task_set)
  309. res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
  310. if (res == TMF_RESP_FUNC_FAILED) {
  311. if (i->dft->lldd_clear_task_set)
  312. res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
  313. }
  314. if (res == TMF_RESP_FUNC_FAILED) {
  315. if (i->dft->lldd_lu_reset)
  316. res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
  317. }
  318. return res;
  319. }
  320. static int sas_recover_I_T(struct domain_device *dev)
  321. {
  322. int res = TMF_RESP_FUNC_FAILED;
  323. struct sas_internal *i =
  324. to_sas_internal(dev->port->ha->core.shost->transportt);
  325. SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
  326. SAS_ADDR(dev->sas_addr));
  327. if (i->dft->lldd_I_T_nexus_reset)
  328. res = i->dft->lldd_I_T_nexus_reset(dev);
  329. return res;
  330. }
  331. /* take a reference on the last known good phy for this device */
  332. struct sas_phy *sas_get_local_phy(struct domain_device *dev)
  333. {
  334. struct sas_ha_struct *ha = dev->port->ha;
  335. struct sas_phy *phy;
  336. unsigned long flags;
  337. /* a published domain device always has a valid phy, it may be
  338. * stale, but it is never NULL
  339. */
  340. BUG_ON(!dev->phy);
  341. spin_lock_irqsave(&ha->phy_port_lock, flags);
  342. phy = dev->phy;
  343. get_device(&phy->dev);
  344. spin_unlock_irqrestore(&ha->phy_port_lock, flags);
  345. return phy;
  346. }
  347. EXPORT_SYMBOL_GPL(sas_get_local_phy);
  348. static void sas_wait_eh(struct domain_device *dev)
  349. {
  350. struct sas_ha_struct *ha = dev->port->ha;
  351. DEFINE_WAIT(wait);
  352. if (dev_is_sata(dev)) {
  353. ata_port_wait_eh(dev->sata_dev.ap);
  354. return;
  355. }
  356. retry:
  357. spin_lock_irq(&ha->lock);
  358. while (test_bit(SAS_DEV_EH_PENDING, &dev->state)) {
  359. prepare_to_wait(&ha->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
  360. spin_unlock_irq(&ha->lock);
  361. schedule();
  362. spin_lock_irq(&ha->lock);
  363. }
  364. finish_wait(&ha->eh_wait_q, &wait);
  365. spin_unlock_irq(&ha->lock);
  366. /* make sure SCSI EH is complete */
  367. if (scsi_host_in_recovery(ha->core.shost)) {
  368. msleep(10);
  369. goto retry;
  370. }
  371. }
  372. EXPORT_SYMBOL(sas_wait_eh);
  373. static int sas_queue_reset(struct domain_device *dev, int reset_type,
  374. u64 lun, int wait)
  375. {
  376. struct sas_ha_struct *ha = dev->port->ha;
  377. int scheduled = 0, tries = 100;
  378. /* ata: promote lun reset to bus reset */
  379. if (dev_is_sata(dev)) {
  380. sas_ata_schedule_reset(dev);
  381. if (wait)
  382. sas_ata_wait_eh(dev);
  383. return SUCCESS;
  384. }
  385. while (!scheduled && tries--) {
  386. spin_lock_irq(&ha->lock);
  387. if (!test_bit(SAS_DEV_EH_PENDING, &dev->state) &&
  388. !test_bit(reset_type, &dev->state)) {
  389. scheduled = 1;
  390. ha->eh_active++;
  391. list_add_tail(&dev->ssp_dev.eh_list_node, &ha->eh_dev_q);
  392. set_bit(SAS_DEV_EH_PENDING, &dev->state);
  393. set_bit(reset_type, &dev->state);
  394. int_to_scsilun(lun, &dev->ssp_dev.reset_lun);
  395. scsi_schedule_eh(ha->core.shost);
  396. }
  397. spin_unlock_irq(&ha->lock);
  398. if (wait)
  399. sas_wait_eh(dev);
  400. if (scheduled)
  401. return SUCCESS;
  402. }
  403. SAS_DPRINTK("%s reset of %s failed\n",
  404. reset_type == SAS_DEV_LU_RESET ? "LUN" : "Bus",
  405. dev_name(&dev->rphy->dev));
  406. return FAILED;
  407. }
  408. int sas_eh_abort_handler(struct scsi_cmnd *cmd)
  409. {
  410. int res;
  411. struct sas_task *task = TO_SAS_TASK(cmd);
  412. struct Scsi_Host *host = cmd->device->host;
  413. struct sas_internal *i = to_sas_internal(host->transportt);
  414. if (current != host->ehandler)
  415. return FAILED;
  416. if (!i->dft->lldd_abort_task)
  417. return FAILED;
  418. res = i->dft->lldd_abort_task(task);
  419. if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
  420. return SUCCESS;
  421. return FAILED;
  422. }
  423. EXPORT_SYMBOL_GPL(sas_eh_abort_handler);
  424. /* Attempt to send a LUN reset message to a device */
  425. int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
  426. {
  427. int res;
  428. struct scsi_lun lun;
  429. struct Scsi_Host *host = cmd->device->host;
  430. struct domain_device *dev = cmd_to_domain_dev(cmd);
  431. struct sas_internal *i = to_sas_internal(host->transportt);
  432. if (current != host->ehandler)
  433. return sas_queue_reset(dev, SAS_DEV_LU_RESET, cmd->device->lun, 0);
  434. int_to_scsilun(cmd->device->lun, &lun);
  435. if (!i->dft->lldd_lu_reset)
  436. return FAILED;
  437. res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
  438. if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
  439. return SUCCESS;
  440. return FAILED;
  441. }
  442. int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
  443. {
  444. int res;
  445. struct Scsi_Host *host = cmd->device->host;
  446. struct domain_device *dev = cmd_to_domain_dev(cmd);
  447. struct sas_internal *i = to_sas_internal(host->transportt);
  448. if (current != host->ehandler)
  449. return sas_queue_reset(dev, SAS_DEV_RESET, 0, 0);
  450. if (!i->dft->lldd_I_T_nexus_reset)
  451. return FAILED;
  452. res = i->dft->lldd_I_T_nexus_reset(dev);
  453. if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE ||
  454. res == -ENODEV)
  455. return SUCCESS;
  456. return FAILED;
  457. }
  458. /* Try to reset a device */
  459. static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
  460. {
  461. int res;
  462. struct Scsi_Host *shost = cmd->device->host;
  463. if (!shost->hostt->eh_device_reset_handler)
  464. goto try_bus_reset;
  465. res = shost->hostt->eh_device_reset_handler(cmd);
  466. if (res == SUCCESS)
  467. return res;
  468. try_bus_reset:
  469. if (shost->hostt->eh_bus_reset_handler)
  470. return shost->hostt->eh_bus_reset_handler(cmd);
  471. return FAILED;
  472. }
  473. static void sas_eh_handle_sas_errors(struct Scsi_Host *shost, struct list_head *work_q)
  474. {
  475. struct scsi_cmnd *cmd, *n;
  476. enum task_disposition res = TASK_IS_DONE;
  477. int tmf_resp, need_reset;
  478. struct sas_internal *i = to_sas_internal(shost->transportt);
  479. unsigned long flags;
  480. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  481. LIST_HEAD(done);
  482. /* clean out any commands that won the completion vs eh race */
  483. list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
  484. struct domain_device *dev = cmd_to_domain_dev(cmd);
  485. struct sas_task *task;
  486. spin_lock_irqsave(&dev->done_lock, flags);
  487. /* by this point the lldd has either observed
  488. * SAS_HA_FROZEN and is leaving the task alone, or has
  489. * won the race with eh and decided to complete it
  490. */
  491. task = TO_SAS_TASK(cmd);
  492. spin_unlock_irqrestore(&dev->done_lock, flags);
  493. if (!task)
  494. list_move_tail(&cmd->eh_entry, &done);
  495. }
  496. Again:
  497. list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
  498. struct sas_task *task = TO_SAS_TASK(cmd);
  499. list_del_init(&cmd->eh_entry);
  500. spin_lock_irqsave(&task->task_state_lock, flags);
  501. need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
  502. spin_unlock_irqrestore(&task->task_state_lock, flags);
  503. if (need_reset) {
  504. SAS_DPRINTK("%s: task 0x%p requests reset\n",
  505. __func__, task);
  506. goto reset;
  507. }
  508. SAS_DPRINTK("trying to find task 0x%p\n", task);
  509. res = sas_scsi_find_task(task);
  510. cmd->eh_eflags = 0;
  511. switch (res) {
  512. case TASK_IS_DONE:
  513. SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
  514. task);
  515. sas_eh_finish_cmd(cmd);
  516. continue;
  517. case TASK_IS_ABORTED:
  518. SAS_DPRINTK("%s: task 0x%p is aborted\n",
  519. __func__, task);
  520. sas_eh_finish_cmd(cmd);
  521. continue;
  522. case TASK_IS_AT_LU:
  523. SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
  524. reset:
  525. tmf_resp = sas_recover_lu(task->dev, cmd);
  526. if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
  527. SAS_DPRINTK("dev %016llx LU %llx is "
  528. "recovered\n",
  529. SAS_ADDR(task->dev),
  530. cmd->device->lun);
  531. sas_eh_finish_cmd(cmd);
  532. sas_scsi_clear_queue_lu(work_q, cmd);
  533. goto Again;
  534. }
  535. /* fallthrough */
  536. case TASK_IS_NOT_AT_LU:
  537. case TASK_ABORT_FAILED:
  538. SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
  539. task);
  540. tmf_resp = sas_recover_I_T(task->dev);
  541. if (tmf_resp == TMF_RESP_FUNC_COMPLETE ||
  542. tmf_resp == -ENODEV) {
  543. struct domain_device *dev = task->dev;
  544. SAS_DPRINTK("I_T %016llx recovered\n",
  545. SAS_ADDR(task->dev->sas_addr));
  546. sas_eh_finish_cmd(cmd);
  547. sas_scsi_clear_queue_I_T(work_q, dev);
  548. goto Again;
  549. }
  550. /* Hammer time :-) */
  551. try_to_reset_cmd_device(cmd);
  552. if (i->dft->lldd_clear_nexus_port) {
  553. struct asd_sas_port *port = task->dev->port;
  554. SAS_DPRINTK("clearing nexus for port:%d\n",
  555. port->id);
  556. res = i->dft->lldd_clear_nexus_port(port);
  557. if (res == TMF_RESP_FUNC_COMPLETE) {
  558. SAS_DPRINTK("clear nexus port:%d "
  559. "succeeded\n", port->id);
  560. sas_eh_finish_cmd(cmd);
  561. sas_scsi_clear_queue_port(work_q,
  562. port);
  563. goto Again;
  564. }
  565. }
  566. if (i->dft->lldd_clear_nexus_ha) {
  567. SAS_DPRINTK("clear nexus ha\n");
  568. res = i->dft->lldd_clear_nexus_ha(ha);
  569. if (res == TMF_RESP_FUNC_COMPLETE) {
  570. SAS_DPRINTK("clear nexus ha "
  571. "succeeded\n");
  572. sas_eh_finish_cmd(cmd);
  573. goto clear_q;
  574. }
  575. }
  576. /* If we are here -- this means that no amount
  577. * of effort could recover from errors. Quite
  578. * possibly the HA just disappeared.
  579. */
  580. SAS_DPRINTK("error from device %llx, LUN %llx "
  581. "couldn't be recovered in any way\n",
  582. SAS_ADDR(task->dev->sas_addr),
  583. cmd->device->lun);
  584. sas_eh_finish_cmd(cmd);
  585. goto clear_q;
  586. }
  587. }
  588. out:
  589. list_splice_tail(&done, work_q);
  590. list_splice_tail_init(&ha->eh_ata_q, work_q);
  591. return;
  592. clear_q:
  593. SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__);
  594. list_for_each_entry_safe(cmd, n, work_q, eh_entry)
  595. sas_eh_finish_cmd(cmd);
  596. goto out;
  597. }
  598. static void sas_eh_handle_resets(struct Scsi_Host *shost)
  599. {
  600. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  601. struct sas_internal *i = to_sas_internal(shost->transportt);
  602. /* handle directed resets to sas devices */
  603. spin_lock_irq(&ha->lock);
  604. while (!list_empty(&ha->eh_dev_q)) {
  605. struct domain_device *dev;
  606. struct ssp_device *ssp;
  607. ssp = list_entry(ha->eh_dev_q.next, typeof(*ssp), eh_list_node);
  608. list_del_init(&ssp->eh_list_node);
  609. dev = container_of(ssp, typeof(*dev), ssp_dev);
  610. kref_get(&dev->kref);
  611. WARN_ONCE(dev_is_sata(dev), "ssp reset to ata device?\n");
  612. spin_unlock_irq(&ha->lock);
  613. if (test_and_clear_bit(SAS_DEV_LU_RESET, &dev->state))
  614. i->dft->lldd_lu_reset(dev, ssp->reset_lun.scsi_lun);
  615. if (test_and_clear_bit(SAS_DEV_RESET, &dev->state))
  616. i->dft->lldd_I_T_nexus_reset(dev);
  617. sas_put_device(dev);
  618. spin_lock_irq(&ha->lock);
  619. clear_bit(SAS_DEV_EH_PENDING, &dev->state);
  620. ha->eh_active--;
  621. }
  622. spin_unlock_irq(&ha->lock);
  623. }
  624. void sas_scsi_recover_host(struct Scsi_Host *shost)
  625. {
  626. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  627. LIST_HEAD(eh_work_q);
  628. int tries = 0;
  629. bool retry;
  630. retry:
  631. tries++;
  632. retry = true;
  633. spin_lock_irq(shost->host_lock);
  634. list_splice_init(&shost->eh_cmd_q, &eh_work_q);
  635. spin_unlock_irq(shost->host_lock);
  636. SAS_DPRINTK("Enter %s busy: %d failed: %d\n",
  637. __func__, atomic_read(&shost->host_busy), shost->host_failed);
  638. /*
  639. * Deal with commands that still have SAS tasks (i.e. they didn't
  640. * complete via the normal sas_task completion mechanism),
  641. * SAS_HA_FROZEN gives eh dominion over all sas_task completion.
  642. */
  643. set_bit(SAS_HA_FROZEN, &ha->state);
  644. sas_eh_handle_sas_errors(shost, &eh_work_q);
  645. clear_bit(SAS_HA_FROZEN, &ha->state);
  646. if (list_empty(&eh_work_q))
  647. goto out;
  648. /*
  649. * Now deal with SCSI commands that completed ok but have a an error
  650. * code (and hopefully sense data) attached. This is roughly what
  651. * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
  652. * command we see here has no sas_task and is thus unknown to the HA.
  653. */
  654. sas_ata_eh(shost, &eh_work_q, &ha->eh_done_q);
  655. if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
  656. scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
  657. out:
  658. sas_eh_handle_resets(shost);
  659. /* now link into libata eh --- if we have any ata devices */
  660. sas_ata_strategy_handler(shost);
  661. scsi_eh_flush_done_q(&ha->eh_done_q);
  662. /* check if any new eh work was scheduled during the last run */
  663. spin_lock_irq(&ha->lock);
  664. if (ha->eh_active == 0) {
  665. shost->host_eh_scheduled = 0;
  666. retry = false;
  667. }
  668. spin_unlock_irq(&ha->lock);
  669. if (retry)
  670. goto retry;
  671. SAS_DPRINTK("--- Exit %s: busy: %d failed: %d tries: %d\n",
  672. __func__, atomic_read(&shost->host_busy),
  673. shost->host_failed, tries);
  674. }
  675. enum blk_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
  676. {
  677. scmd_dbg(cmd, "command %p timed out\n", cmd);
  678. return BLK_EH_NOT_HANDLED;
  679. }
  680. int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
  681. {
  682. struct domain_device *dev = sdev_to_domain_dev(sdev);
  683. if (dev_is_sata(dev))
  684. return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
  685. return -EINVAL;
  686. }
  687. struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
  688. {
  689. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
  690. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  691. struct domain_device *found_dev = NULL;
  692. int i;
  693. unsigned long flags;
  694. spin_lock_irqsave(&ha->phy_port_lock, flags);
  695. for (i = 0; i < ha->num_phys; i++) {
  696. struct asd_sas_port *port = ha->sas_port[i];
  697. struct domain_device *dev;
  698. spin_lock(&port->dev_list_lock);
  699. list_for_each_entry(dev, &port->dev_list, dev_list_node) {
  700. if (rphy == dev->rphy) {
  701. found_dev = dev;
  702. spin_unlock(&port->dev_list_lock);
  703. goto found;
  704. }
  705. }
  706. spin_unlock(&port->dev_list_lock);
  707. }
  708. found:
  709. spin_unlock_irqrestore(&ha->phy_port_lock, flags);
  710. return found_dev;
  711. }
  712. int sas_target_alloc(struct scsi_target *starget)
  713. {
  714. struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
  715. struct domain_device *found_dev = sas_find_dev_by_rphy(rphy);
  716. if (!found_dev)
  717. return -ENODEV;
  718. kref_get(&found_dev->kref);
  719. starget->hostdata = found_dev;
  720. return 0;
  721. }
  722. #define SAS_DEF_QD 256
  723. int sas_slave_configure(struct scsi_device *scsi_dev)
  724. {
  725. struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
  726. struct sas_ha_struct *sas_ha;
  727. BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
  728. if (dev_is_sata(dev)) {
  729. ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
  730. return 0;
  731. }
  732. sas_ha = dev->port->ha;
  733. sas_read_port_mode_page(scsi_dev);
  734. if (scsi_dev->tagged_supported) {
  735. scsi_change_queue_depth(scsi_dev, SAS_DEF_QD);
  736. } else {
  737. SAS_DPRINTK("device %llx, LUN %llx doesn't support "
  738. "TCQ\n", SAS_ADDR(dev->sas_addr),
  739. scsi_dev->lun);
  740. scsi_change_queue_depth(scsi_dev, 1);
  741. }
  742. scsi_dev->allow_restart = 1;
  743. return 0;
  744. }
  745. int sas_change_queue_depth(struct scsi_device *sdev, int depth)
  746. {
  747. struct domain_device *dev = sdev_to_domain_dev(sdev);
  748. if (dev_is_sata(dev))
  749. return __ata_change_queue_depth(dev->sata_dev.ap, sdev, depth);
  750. if (!sdev->tagged_supported)
  751. depth = 1;
  752. return scsi_change_queue_depth(sdev, depth);
  753. }
  754. int sas_bios_param(struct scsi_device *scsi_dev,
  755. struct block_device *bdev,
  756. sector_t capacity, int *hsc)
  757. {
  758. hsc[0] = 255;
  759. hsc[1] = 63;
  760. sector_div(capacity, 255*63);
  761. hsc[2] = capacity;
  762. return 0;
  763. }
  764. /*
  765. * Tell an upper layer that it needs to initiate an abort for a given task.
  766. * This should only ever be called by an LLDD.
  767. */
  768. void sas_task_abort(struct sas_task *task)
  769. {
  770. struct scsi_cmnd *sc = task->uldd_task;
  771. /* Escape for libsas internal commands */
  772. if (!sc) {
  773. struct sas_task_slow *slow = task->slow_task;
  774. if (!slow)
  775. return;
  776. if (!del_timer(&slow->timer))
  777. return;
  778. slow->timer.function(slow->timer.data);
  779. return;
  780. }
  781. if (dev_is_sata(task->dev)) {
  782. sas_ata_task_abort(task);
  783. } else {
  784. struct request_queue *q = sc->device->request_queue;
  785. unsigned long flags;
  786. spin_lock_irqsave(q->queue_lock, flags);
  787. blk_abort_request(sc->request);
  788. spin_unlock_irqrestore(q->queue_lock, flags);
  789. }
  790. }
  791. void sas_target_destroy(struct scsi_target *starget)
  792. {
  793. struct domain_device *found_dev = starget->hostdata;
  794. if (!found_dev)
  795. return;
  796. starget->hostdata = NULL;
  797. sas_put_device(found_dev);
  798. }
  799. static void sas_parse_addr(u8 *sas_addr, const char *p)
  800. {
  801. int i;
  802. for (i = 0; i < SAS_ADDR_SIZE; i++) {
  803. u8 h, l;
  804. if (!*p)
  805. break;
  806. h = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
  807. p++;
  808. l = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
  809. p++;
  810. sas_addr[i] = (h<<4) | l;
  811. }
  812. }
  813. #define SAS_STRING_ADDR_SIZE 16
  814. int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
  815. {
  816. int res;
  817. const struct firmware *fw;
  818. res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
  819. if (res)
  820. return res;
  821. if (fw->size < SAS_STRING_ADDR_SIZE) {
  822. res = -ENODEV;
  823. goto out;
  824. }
  825. sas_parse_addr(addr, fw->data);
  826. out:
  827. release_firmware(fw);
  828. return res;
  829. }
  830. EXPORT_SYMBOL_GPL(sas_request_addr);
  831. EXPORT_SYMBOL_GPL(sas_queuecommand);
  832. EXPORT_SYMBOL_GPL(sas_target_alloc);
  833. EXPORT_SYMBOL_GPL(sas_slave_configure);
  834. EXPORT_SYMBOL_GPL(sas_change_queue_depth);
  835. EXPORT_SYMBOL_GPL(sas_bios_param);
  836. EXPORT_SYMBOL_GPL(sas_task_abort);
  837. EXPORT_SYMBOL_GPL(sas_phy_reset);
  838. EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
  839. EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
  840. EXPORT_SYMBOL_GPL(sas_target_destroy);
  841. EXPORT_SYMBOL_GPL(sas_ioctl);