sas_scsi_host.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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 sas_task *task = TO_SAS_TASK(cmd);
  199. /* At this point, we only get called following an actual abort
  200. * of the task, so we should be guaranteed not to be racing with
  201. * any completions from the LLD. Task is freed after this.
  202. */
  203. sas_end_task(cmd, task);
  204. /* now finish the command and move it on to the error
  205. * handler done list, this also takes it off the
  206. * error handler pending list.
  207. */
  208. scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
  209. }
  210. static void sas_eh_defer_cmd(struct scsi_cmnd *cmd)
  211. {
  212. struct domain_device *dev = cmd_to_domain_dev(cmd);
  213. struct sas_ha_struct *ha = dev->port->ha;
  214. struct sas_task *task = TO_SAS_TASK(cmd);
  215. if (!dev_is_sata(dev)) {
  216. sas_eh_finish_cmd(cmd);
  217. return;
  218. }
  219. /* report the timeout to libata */
  220. sas_end_task(cmd, task);
  221. list_move_tail(&cmd->eh_entry, &ha->eh_ata_q);
  222. }
  223. static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
  224. {
  225. struct scsi_cmnd *cmd, *n;
  226. list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
  227. if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
  228. cmd->device->lun == my_cmd->device->lun)
  229. sas_eh_defer_cmd(cmd);
  230. }
  231. }
  232. static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
  233. struct domain_device *dev)
  234. {
  235. struct scsi_cmnd *cmd, *n;
  236. list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
  237. struct domain_device *x = cmd_to_domain_dev(cmd);
  238. if (x == dev)
  239. sas_eh_finish_cmd(cmd);
  240. }
  241. }
  242. static void sas_scsi_clear_queue_port(struct list_head *error_q,
  243. struct asd_sas_port *port)
  244. {
  245. struct scsi_cmnd *cmd, *n;
  246. list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
  247. struct domain_device *dev = cmd_to_domain_dev(cmd);
  248. struct asd_sas_port *x = dev->port;
  249. if (x == port)
  250. sas_eh_finish_cmd(cmd);
  251. }
  252. }
  253. enum task_disposition {
  254. TASK_IS_DONE,
  255. TASK_IS_ABORTED,
  256. TASK_IS_AT_LU,
  257. TASK_IS_NOT_AT_LU,
  258. TASK_ABORT_FAILED,
  259. };
  260. static enum task_disposition sas_scsi_find_task(struct sas_task *task)
  261. {
  262. unsigned long flags;
  263. int i, res;
  264. struct sas_internal *si =
  265. to_sas_internal(task->dev->port->ha->core.shost->transportt);
  266. for (i = 0; i < 5; i++) {
  267. SAS_DPRINTK("%s: aborting task 0x%p\n", __func__, task);
  268. res = si->dft->lldd_abort_task(task);
  269. spin_lock_irqsave(&task->task_state_lock, flags);
  270. if (task->task_state_flags & SAS_TASK_STATE_DONE) {
  271. spin_unlock_irqrestore(&task->task_state_lock, flags);
  272. SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
  273. task);
  274. return TASK_IS_DONE;
  275. }
  276. spin_unlock_irqrestore(&task->task_state_lock, flags);
  277. if (res == TMF_RESP_FUNC_COMPLETE) {
  278. SAS_DPRINTK("%s: task 0x%p is aborted\n",
  279. __func__, task);
  280. return TASK_IS_ABORTED;
  281. } else if (si->dft->lldd_query_task) {
  282. SAS_DPRINTK("%s: querying task 0x%p\n",
  283. __func__, task);
  284. res = si->dft->lldd_query_task(task);
  285. switch (res) {
  286. case TMF_RESP_FUNC_SUCC:
  287. SAS_DPRINTK("%s: task 0x%p at LU\n",
  288. __func__, task);
  289. return TASK_IS_AT_LU;
  290. case TMF_RESP_FUNC_COMPLETE:
  291. SAS_DPRINTK("%s: task 0x%p not at LU\n",
  292. __func__, task);
  293. return TASK_IS_NOT_AT_LU;
  294. case TMF_RESP_FUNC_FAILED:
  295. SAS_DPRINTK("%s: task 0x%p failed to abort\n",
  296. __func__, task);
  297. return TASK_ABORT_FAILED;
  298. }
  299. }
  300. }
  301. return res;
  302. }
  303. static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
  304. {
  305. int res = TMF_RESP_FUNC_FAILED;
  306. struct scsi_lun lun;
  307. struct sas_internal *i =
  308. to_sas_internal(dev->port->ha->core.shost->transportt);
  309. int_to_scsilun(cmd->device->lun, &lun);
  310. SAS_DPRINTK("eh: device %llx LUN %llx has the task\n",
  311. SAS_ADDR(dev->sas_addr),
  312. cmd->device->lun);
  313. if (i->dft->lldd_abort_task_set)
  314. res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
  315. if (res == TMF_RESP_FUNC_FAILED) {
  316. if (i->dft->lldd_clear_task_set)
  317. res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
  318. }
  319. if (res == TMF_RESP_FUNC_FAILED) {
  320. if (i->dft->lldd_lu_reset)
  321. res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
  322. }
  323. return res;
  324. }
  325. static int sas_recover_I_T(struct domain_device *dev)
  326. {
  327. int res = TMF_RESP_FUNC_FAILED;
  328. struct sas_internal *i =
  329. to_sas_internal(dev->port->ha->core.shost->transportt);
  330. SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
  331. SAS_ADDR(dev->sas_addr));
  332. if (i->dft->lldd_I_T_nexus_reset)
  333. res = i->dft->lldd_I_T_nexus_reset(dev);
  334. return res;
  335. }
  336. /* take a reference on the last known good phy for this device */
  337. struct sas_phy *sas_get_local_phy(struct domain_device *dev)
  338. {
  339. struct sas_ha_struct *ha = dev->port->ha;
  340. struct sas_phy *phy;
  341. unsigned long flags;
  342. /* a published domain device always has a valid phy, it may be
  343. * stale, but it is never NULL
  344. */
  345. BUG_ON(!dev->phy);
  346. spin_lock_irqsave(&ha->phy_port_lock, flags);
  347. phy = dev->phy;
  348. get_device(&phy->dev);
  349. spin_unlock_irqrestore(&ha->phy_port_lock, flags);
  350. return phy;
  351. }
  352. EXPORT_SYMBOL_GPL(sas_get_local_phy);
  353. static void sas_wait_eh(struct domain_device *dev)
  354. {
  355. struct sas_ha_struct *ha = dev->port->ha;
  356. DEFINE_WAIT(wait);
  357. if (dev_is_sata(dev)) {
  358. ata_port_wait_eh(dev->sata_dev.ap);
  359. return;
  360. }
  361. retry:
  362. spin_lock_irq(&ha->lock);
  363. while (test_bit(SAS_DEV_EH_PENDING, &dev->state)) {
  364. prepare_to_wait(&ha->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
  365. spin_unlock_irq(&ha->lock);
  366. schedule();
  367. spin_lock_irq(&ha->lock);
  368. }
  369. finish_wait(&ha->eh_wait_q, &wait);
  370. spin_unlock_irq(&ha->lock);
  371. /* make sure SCSI EH is complete */
  372. if (scsi_host_in_recovery(ha->core.shost)) {
  373. msleep(10);
  374. goto retry;
  375. }
  376. }
  377. EXPORT_SYMBOL(sas_wait_eh);
  378. static int sas_queue_reset(struct domain_device *dev, int reset_type,
  379. u64 lun, int wait)
  380. {
  381. struct sas_ha_struct *ha = dev->port->ha;
  382. int scheduled = 0, tries = 100;
  383. /* ata: promote lun reset to bus reset */
  384. if (dev_is_sata(dev)) {
  385. sas_ata_schedule_reset(dev);
  386. if (wait)
  387. sas_ata_wait_eh(dev);
  388. return SUCCESS;
  389. }
  390. while (!scheduled && tries--) {
  391. spin_lock_irq(&ha->lock);
  392. if (!test_bit(SAS_DEV_EH_PENDING, &dev->state) &&
  393. !test_bit(reset_type, &dev->state)) {
  394. scheduled = 1;
  395. ha->eh_active++;
  396. list_add_tail(&dev->ssp_dev.eh_list_node, &ha->eh_dev_q);
  397. set_bit(SAS_DEV_EH_PENDING, &dev->state);
  398. set_bit(reset_type, &dev->state);
  399. int_to_scsilun(lun, &dev->ssp_dev.reset_lun);
  400. scsi_schedule_eh(ha->core.shost);
  401. }
  402. spin_unlock_irq(&ha->lock);
  403. if (wait)
  404. sas_wait_eh(dev);
  405. if (scheduled)
  406. return SUCCESS;
  407. }
  408. SAS_DPRINTK("%s reset of %s failed\n",
  409. reset_type == SAS_DEV_LU_RESET ? "LUN" : "Bus",
  410. dev_name(&dev->rphy->dev));
  411. return FAILED;
  412. }
  413. int sas_eh_abort_handler(struct scsi_cmnd *cmd)
  414. {
  415. int res;
  416. struct sas_task *task = TO_SAS_TASK(cmd);
  417. struct Scsi_Host *host = cmd->device->host;
  418. struct sas_internal *i = to_sas_internal(host->transportt);
  419. if (current != host->ehandler)
  420. return FAILED;
  421. if (!i->dft->lldd_abort_task)
  422. return FAILED;
  423. res = i->dft->lldd_abort_task(task);
  424. if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
  425. return SUCCESS;
  426. return FAILED;
  427. }
  428. EXPORT_SYMBOL_GPL(sas_eh_abort_handler);
  429. /* Attempt to send a LUN reset message to a device */
  430. int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
  431. {
  432. int res;
  433. struct scsi_lun lun;
  434. struct Scsi_Host *host = cmd->device->host;
  435. struct domain_device *dev = cmd_to_domain_dev(cmd);
  436. struct sas_internal *i = to_sas_internal(host->transportt);
  437. if (current != host->ehandler)
  438. return sas_queue_reset(dev, SAS_DEV_LU_RESET, cmd->device->lun, 0);
  439. int_to_scsilun(cmd->device->lun, &lun);
  440. if (!i->dft->lldd_lu_reset)
  441. return FAILED;
  442. res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
  443. if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
  444. return SUCCESS;
  445. return FAILED;
  446. }
  447. int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
  448. {
  449. int res;
  450. struct Scsi_Host *host = cmd->device->host;
  451. struct domain_device *dev = cmd_to_domain_dev(cmd);
  452. struct sas_internal *i = to_sas_internal(host->transportt);
  453. if (current != host->ehandler)
  454. return sas_queue_reset(dev, SAS_DEV_RESET, 0, 0);
  455. if (!i->dft->lldd_I_T_nexus_reset)
  456. return FAILED;
  457. res = i->dft->lldd_I_T_nexus_reset(dev);
  458. if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE ||
  459. res == -ENODEV)
  460. return SUCCESS;
  461. return FAILED;
  462. }
  463. /* Try to reset a device */
  464. static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
  465. {
  466. int res;
  467. struct Scsi_Host *shost = cmd->device->host;
  468. if (!shost->hostt->eh_device_reset_handler)
  469. goto try_bus_reset;
  470. res = shost->hostt->eh_device_reset_handler(cmd);
  471. if (res == SUCCESS)
  472. return res;
  473. try_bus_reset:
  474. if (shost->hostt->eh_bus_reset_handler)
  475. return shost->hostt->eh_bus_reset_handler(cmd);
  476. return FAILED;
  477. }
  478. static void sas_eh_handle_sas_errors(struct Scsi_Host *shost, struct list_head *work_q)
  479. {
  480. struct scsi_cmnd *cmd, *n;
  481. enum task_disposition res = TASK_IS_DONE;
  482. int tmf_resp, need_reset;
  483. struct sas_internal *i = to_sas_internal(shost->transportt);
  484. unsigned long flags;
  485. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  486. LIST_HEAD(done);
  487. /* clean out any commands that won the completion vs eh race */
  488. list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
  489. struct domain_device *dev = cmd_to_domain_dev(cmd);
  490. struct sas_task *task;
  491. spin_lock_irqsave(&dev->done_lock, flags);
  492. /* by this point the lldd has either observed
  493. * SAS_HA_FROZEN and is leaving the task alone, or has
  494. * won the race with eh and decided to complete it
  495. */
  496. task = TO_SAS_TASK(cmd);
  497. spin_unlock_irqrestore(&dev->done_lock, flags);
  498. if (!task)
  499. list_move_tail(&cmd->eh_entry, &done);
  500. }
  501. Again:
  502. list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
  503. struct sas_task *task = TO_SAS_TASK(cmd);
  504. list_del_init(&cmd->eh_entry);
  505. spin_lock_irqsave(&task->task_state_lock, flags);
  506. need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
  507. spin_unlock_irqrestore(&task->task_state_lock, flags);
  508. if (need_reset) {
  509. SAS_DPRINTK("%s: task 0x%p requests reset\n",
  510. __func__, task);
  511. goto reset;
  512. }
  513. SAS_DPRINTK("trying to find task 0x%p\n", task);
  514. res = sas_scsi_find_task(task);
  515. cmd->eh_eflags = 0;
  516. switch (res) {
  517. case TASK_IS_DONE:
  518. SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
  519. task);
  520. sas_eh_defer_cmd(cmd);
  521. continue;
  522. case TASK_IS_ABORTED:
  523. SAS_DPRINTK("%s: task 0x%p is aborted\n",
  524. __func__, task);
  525. sas_eh_defer_cmd(cmd);
  526. continue;
  527. case TASK_IS_AT_LU:
  528. SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
  529. reset:
  530. tmf_resp = sas_recover_lu(task->dev, cmd);
  531. if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
  532. SAS_DPRINTK("dev %016llx LU %llx is "
  533. "recovered\n",
  534. SAS_ADDR(task->dev),
  535. cmd->device->lun);
  536. sas_eh_defer_cmd(cmd);
  537. sas_scsi_clear_queue_lu(work_q, cmd);
  538. goto Again;
  539. }
  540. /* fallthrough */
  541. case TASK_IS_NOT_AT_LU:
  542. case TASK_ABORT_FAILED:
  543. SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
  544. task);
  545. tmf_resp = sas_recover_I_T(task->dev);
  546. if (tmf_resp == TMF_RESP_FUNC_COMPLETE ||
  547. tmf_resp == -ENODEV) {
  548. struct domain_device *dev = task->dev;
  549. SAS_DPRINTK("I_T %016llx recovered\n",
  550. SAS_ADDR(task->dev->sas_addr));
  551. sas_eh_finish_cmd(cmd);
  552. sas_scsi_clear_queue_I_T(work_q, dev);
  553. goto Again;
  554. }
  555. /* Hammer time :-) */
  556. try_to_reset_cmd_device(cmd);
  557. if (i->dft->lldd_clear_nexus_port) {
  558. struct asd_sas_port *port = task->dev->port;
  559. SAS_DPRINTK("clearing nexus for port:%d\n",
  560. port->id);
  561. res = i->dft->lldd_clear_nexus_port(port);
  562. if (res == TMF_RESP_FUNC_COMPLETE) {
  563. SAS_DPRINTK("clear nexus port:%d "
  564. "succeeded\n", port->id);
  565. sas_eh_finish_cmd(cmd);
  566. sas_scsi_clear_queue_port(work_q,
  567. port);
  568. goto Again;
  569. }
  570. }
  571. if (i->dft->lldd_clear_nexus_ha) {
  572. SAS_DPRINTK("clear nexus ha\n");
  573. res = i->dft->lldd_clear_nexus_ha(ha);
  574. if (res == TMF_RESP_FUNC_COMPLETE) {
  575. SAS_DPRINTK("clear nexus ha "
  576. "succeeded\n");
  577. sas_eh_finish_cmd(cmd);
  578. goto clear_q;
  579. }
  580. }
  581. /* If we are here -- this means that no amount
  582. * of effort could recover from errors. Quite
  583. * possibly the HA just disappeared.
  584. */
  585. SAS_DPRINTK("error from device %llx, LUN %llx "
  586. "couldn't be recovered in any way\n",
  587. SAS_ADDR(task->dev->sas_addr),
  588. cmd->device->lun);
  589. sas_eh_finish_cmd(cmd);
  590. goto clear_q;
  591. }
  592. }
  593. out:
  594. list_splice_tail(&done, work_q);
  595. list_splice_tail_init(&ha->eh_ata_q, work_q);
  596. return;
  597. clear_q:
  598. SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__);
  599. list_for_each_entry_safe(cmd, n, work_q, eh_entry)
  600. sas_eh_finish_cmd(cmd);
  601. goto out;
  602. }
  603. static void sas_eh_handle_resets(struct Scsi_Host *shost)
  604. {
  605. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  606. struct sas_internal *i = to_sas_internal(shost->transportt);
  607. /* handle directed resets to sas devices */
  608. spin_lock_irq(&ha->lock);
  609. while (!list_empty(&ha->eh_dev_q)) {
  610. struct domain_device *dev;
  611. struct ssp_device *ssp;
  612. ssp = list_entry(ha->eh_dev_q.next, typeof(*ssp), eh_list_node);
  613. list_del_init(&ssp->eh_list_node);
  614. dev = container_of(ssp, typeof(*dev), ssp_dev);
  615. kref_get(&dev->kref);
  616. WARN_ONCE(dev_is_sata(dev), "ssp reset to ata device?\n");
  617. spin_unlock_irq(&ha->lock);
  618. if (test_and_clear_bit(SAS_DEV_LU_RESET, &dev->state))
  619. i->dft->lldd_lu_reset(dev, ssp->reset_lun.scsi_lun);
  620. if (test_and_clear_bit(SAS_DEV_RESET, &dev->state))
  621. i->dft->lldd_I_T_nexus_reset(dev);
  622. sas_put_device(dev);
  623. spin_lock_irq(&ha->lock);
  624. clear_bit(SAS_DEV_EH_PENDING, &dev->state);
  625. ha->eh_active--;
  626. }
  627. spin_unlock_irq(&ha->lock);
  628. }
  629. void sas_scsi_recover_host(struct Scsi_Host *shost)
  630. {
  631. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  632. LIST_HEAD(eh_work_q);
  633. int tries = 0;
  634. bool retry;
  635. retry:
  636. tries++;
  637. retry = true;
  638. spin_lock_irq(shost->host_lock);
  639. list_splice_init(&shost->eh_cmd_q, &eh_work_q);
  640. spin_unlock_irq(shost->host_lock);
  641. SAS_DPRINTK("Enter %s busy: %d failed: %d\n",
  642. __func__, atomic_read(&shost->host_busy), shost->host_failed);
  643. /*
  644. * Deal with commands that still have SAS tasks (i.e. they didn't
  645. * complete via the normal sas_task completion mechanism),
  646. * SAS_HA_FROZEN gives eh dominion over all sas_task completion.
  647. */
  648. set_bit(SAS_HA_FROZEN, &ha->state);
  649. sas_eh_handle_sas_errors(shost, &eh_work_q);
  650. clear_bit(SAS_HA_FROZEN, &ha->state);
  651. if (list_empty(&eh_work_q))
  652. goto out;
  653. /*
  654. * Now deal with SCSI commands that completed ok but have a an error
  655. * code (and hopefully sense data) attached. This is roughly what
  656. * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
  657. * command we see here has no sas_task and is thus unknown to the HA.
  658. */
  659. sas_ata_eh(shost, &eh_work_q, &ha->eh_done_q);
  660. if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
  661. scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
  662. out:
  663. sas_eh_handle_resets(shost);
  664. /* now link into libata eh --- if we have any ata devices */
  665. sas_ata_strategy_handler(shost);
  666. scsi_eh_flush_done_q(&ha->eh_done_q);
  667. /* check if any new eh work was scheduled during the last run */
  668. spin_lock_irq(&ha->lock);
  669. if (ha->eh_active == 0) {
  670. shost->host_eh_scheduled = 0;
  671. retry = false;
  672. }
  673. spin_unlock_irq(&ha->lock);
  674. if (retry)
  675. goto retry;
  676. SAS_DPRINTK("--- Exit %s: busy: %d failed: %d tries: %d\n",
  677. __func__, atomic_read(&shost->host_busy),
  678. shost->host_failed, tries);
  679. }
  680. enum blk_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
  681. {
  682. scmd_dbg(cmd, "command %p timed out\n", cmd);
  683. return BLK_EH_NOT_HANDLED;
  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. struct sas_ha_struct *sas_ha;
  732. BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
  733. if (dev_is_sata(dev)) {
  734. ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
  735. return 0;
  736. }
  737. sas_ha = dev->port->ha;
  738. sas_read_port_mode_page(scsi_dev);
  739. if (scsi_dev->tagged_supported) {
  740. scsi_change_queue_depth(scsi_dev, SAS_DEF_QD);
  741. } else {
  742. SAS_DPRINTK("device %llx, LUN %llx doesn't support "
  743. "TCQ\n", SAS_ADDR(dev->sas_addr),
  744. scsi_dev->lun);
  745. scsi_change_queue_depth(scsi_dev, 1);
  746. }
  747. scsi_dev->allow_restart = 1;
  748. return 0;
  749. }
  750. int sas_change_queue_depth(struct scsi_device *sdev, int depth)
  751. {
  752. struct domain_device *dev = sdev_to_domain_dev(sdev);
  753. if (dev_is_sata(dev))
  754. return __ata_change_queue_depth(dev->sata_dev.ap, sdev, depth);
  755. if (!sdev->tagged_supported)
  756. depth = 1;
  757. return scsi_change_queue_depth(sdev, depth);
  758. }
  759. int sas_bios_param(struct scsi_device *scsi_dev,
  760. struct block_device *bdev,
  761. sector_t capacity, int *hsc)
  762. {
  763. hsc[0] = 255;
  764. hsc[1] = 63;
  765. sector_div(capacity, 255*63);
  766. hsc[2] = capacity;
  767. return 0;
  768. }
  769. /*
  770. * Tell an upper layer that it needs to initiate an abort for a given task.
  771. * This should only ever be called by an LLDD.
  772. */
  773. void sas_task_abort(struct sas_task *task)
  774. {
  775. struct scsi_cmnd *sc = task->uldd_task;
  776. /* Escape for libsas internal commands */
  777. if (!sc) {
  778. struct sas_task_slow *slow = task->slow_task;
  779. if (!slow)
  780. return;
  781. if (!del_timer(&slow->timer))
  782. return;
  783. slow->timer.function(slow->timer.data);
  784. return;
  785. }
  786. if (dev_is_sata(task->dev)) {
  787. sas_ata_task_abort(task);
  788. } else {
  789. struct request_queue *q = sc->device->request_queue;
  790. unsigned long flags;
  791. spin_lock_irqsave(q->queue_lock, flags);
  792. blk_abort_request(sc->request);
  793. spin_unlock_irqrestore(q->queue_lock, flags);
  794. }
  795. }
  796. void sas_target_destroy(struct scsi_target *starget)
  797. {
  798. struct domain_device *found_dev = starget->hostdata;
  799. if (!found_dev)
  800. return;
  801. starget->hostdata = NULL;
  802. sas_put_device(found_dev);
  803. }
  804. static void sas_parse_addr(u8 *sas_addr, const char *p)
  805. {
  806. int i;
  807. for (i = 0; i < SAS_ADDR_SIZE; i++) {
  808. u8 h, l;
  809. if (!*p)
  810. break;
  811. h = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
  812. p++;
  813. l = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
  814. p++;
  815. sas_addr[i] = (h<<4) | l;
  816. }
  817. }
  818. #define SAS_STRING_ADDR_SIZE 16
  819. int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
  820. {
  821. int res;
  822. const struct firmware *fw;
  823. res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
  824. if (res)
  825. return res;
  826. if (fw->size < SAS_STRING_ADDR_SIZE) {
  827. res = -ENODEV;
  828. goto out;
  829. }
  830. sas_parse_addr(addr, fw->data);
  831. out:
  832. release_firmware(fw);
  833. return res;
  834. }
  835. EXPORT_SYMBOL_GPL(sas_request_addr);
  836. EXPORT_SYMBOL_GPL(sas_queuecommand);
  837. EXPORT_SYMBOL_GPL(sas_target_alloc);
  838. EXPORT_SYMBOL_GPL(sas_slave_configure);
  839. EXPORT_SYMBOL_GPL(sas_change_queue_depth);
  840. EXPORT_SYMBOL_GPL(sas_bios_param);
  841. EXPORT_SYMBOL_GPL(sas_task_abort);
  842. EXPORT_SYMBOL_GPL(sas_phy_reset);
  843. EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
  844. EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
  845. EXPORT_SYMBOL_GPL(sas_target_destroy);
  846. EXPORT_SYMBOL_GPL(sas_ioctl);