sas_scsi_host.c 25 KB

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