sas_scsi_host.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  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. list_del_init(&task->list);
  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. list_del_init(&task->list);
  131. sas_free_task(task);
  132. return;
  133. }
  134. sas_end_task(sc, task);
  135. sc->scsi_done(sc);
  136. }
  137. static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
  138. struct domain_device *dev,
  139. gfp_t gfp_flags)
  140. {
  141. struct sas_task *task = sas_alloc_task(gfp_flags);
  142. struct scsi_lun lun;
  143. if (!task)
  144. return NULL;
  145. task->uldd_task = cmd;
  146. ASSIGN_SAS_TASK(cmd, task);
  147. task->dev = dev;
  148. task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
  149. task->ssp_task.retry_count = 1;
  150. int_to_scsilun(cmd->device->lun, &lun);
  151. memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
  152. task->ssp_task.task_attr = TASK_ATTR_SIMPLE;
  153. memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
  154. task->scatter = scsi_sglist(cmd);
  155. task->num_scatter = scsi_sg_count(cmd);
  156. task->total_xfer_len = scsi_bufflen(cmd);
  157. task->data_dir = cmd->sc_data_direction;
  158. task->task_done = sas_scsi_task_done;
  159. return task;
  160. }
  161. int sas_queue_up(struct sas_task *task)
  162. {
  163. struct sas_ha_struct *sas_ha = task->dev->port->ha;
  164. struct scsi_core *core = &sas_ha->core;
  165. unsigned long flags;
  166. LIST_HEAD(list);
  167. spin_lock_irqsave(&core->task_queue_lock, flags);
  168. if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
  169. spin_unlock_irqrestore(&core->task_queue_lock, flags);
  170. return -SAS_QUEUE_FULL;
  171. }
  172. list_add_tail(&task->list, &core->task_queue);
  173. core->task_queue_size += 1;
  174. spin_unlock_irqrestore(&core->task_queue_lock, flags);
  175. wake_up_process(core->queue_thread);
  176. return 0;
  177. }
  178. int sas_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
  179. {
  180. struct sas_internal *i = to_sas_internal(host->transportt);
  181. struct domain_device *dev = cmd_to_domain_dev(cmd);
  182. struct sas_ha_struct *sas_ha = dev->port->ha;
  183. struct sas_task *task;
  184. int res = 0;
  185. /* If the device fell off, no sense in issuing commands */
  186. if (test_bit(SAS_DEV_GONE, &dev->state)) {
  187. cmd->result = DID_BAD_TARGET << 16;
  188. goto out_done;
  189. }
  190. if (dev_is_sata(dev)) {
  191. spin_lock_irq(dev->sata_dev.ap->lock);
  192. res = ata_sas_queuecmd(cmd, dev->sata_dev.ap);
  193. spin_unlock_irq(dev->sata_dev.ap->lock);
  194. return res;
  195. }
  196. task = sas_create_task(cmd, dev, GFP_ATOMIC);
  197. if (!task)
  198. return SCSI_MLQUEUE_HOST_BUSY;
  199. /* Queue up, Direct Mode or Task Collector Mode. */
  200. if (sas_ha->lldd_max_execute_num < 2)
  201. res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
  202. else
  203. res = sas_queue_up(task);
  204. if (res)
  205. goto out_free_task;
  206. return 0;
  207. out_free_task:
  208. SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
  209. ASSIGN_SAS_TASK(cmd, NULL);
  210. sas_free_task(task);
  211. if (res == -SAS_QUEUE_FULL)
  212. cmd->result = DID_SOFT_ERROR << 16; /* retry */
  213. else
  214. cmd->result = DID_ERROR << 16;
  215. out_done:
  216. cmd->scsi_done(cmd);
  217. return 0;
  218. }
  219. static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
  220. {
  221. struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
  222. struct sas_task *task = TO_SAS_TASK(cmd);
  223. /* At this point, we only get called following an actual abort
  224. * of the task, so we should be guaranteed not to be racing with
  225. * any completions from the LLD. Task is freed after this.
  226. */
  227. sas_end_task(cmd, task);
  228. /* now finish the command and move it on to the error
  229. * handler done list, this also takes it off the
  230. * error handler pending list.
  231. */
  232. scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
  233. }
  234. static void sas_eh_defer_cmd(struct scsi_cmnd *cmd)
  235. {
  236. struct domain_device *dev = cmd_to_domain_dev(cmd);
  237. struct sas_ha_struct *ha = dev->port->ha;
  238. struct sas_task *task = TO_SAS_TASK(cmd);
  239. if (!dev_is_sata(dev)) {
  240. sas_eh_finish_cmd(cmd);
  241. return;
  242. }
  243. /* report the timeout to libata */
  244. sas_end_task(cmd, task);
  245. list_move_tail(&cmd->eh_entry, &ha->eh_ata_q);
  246. }
  247. static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
  248. {
  249. struct scsi_cmnd *cmd, *n;
  250. list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
  251. if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
  252. cmd->device->lun == my_cmd->device->lun)
  253. sas_eh_defer_cmd(cmd);
  254. }
  255. }
  256. static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
  257. struct domain_device *dev)
  258. {
  259. struct scsi_cmnd *cmd, *n;
  260. list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
  261. struct domain_device *x = cmd_to_domain_dev(cmd);
  262. if (x == dev)
  263. sas_eh_finish_cmd(cmd);
  264. }
  265. }
  266. static void sas_scsi_clear_queue_port(struct list_head *error_q,
  267. struct asd_sas_port *port)
  268. {
  269. struct scsi_cmnd *cmd, *n;
  270. list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
  271. struct domain_device *dev = cmd_to_domain_dev(cmd);
  272. struct asd_sas_port *x = dev->port;
  273. if (x == port)
  274. sas_eh_finish_cmd(cmd);
  275. }
  276. }
  277. enum task_disposition {
  278. TASK_IS_DONE,
  279. TASK_IS_ABORTED,
  280. TASK_IS_AT_LU,
  281. TASK_IS_NOT_AT_HA,
  282. TASK_IS_NOT_AT_LU,
  283. TASK_ABORT_FAILED,
  284. };
  285. static enum task_disposition sas_scsi_find_task(struct sas_task *task)
  286. {
  287. struct sas_ha_struct *ha = task->dev->port->ha;
  288. unsigned long flags;
  289. int i, res;
  290. struct sas_internal *si =
  291. to_sas_internal(task->dev->port->ha->core.shost->transportt);
  292. if (ha->lldd_max_execute_num > 1) {
  293. struct scsi_core *core = &ha->core;
  294. struct sas_task *t, *n;
  295. mutex_lock(&core->task_queue_flush);
  296. spin_lock_irqsave(&core->task_queue_lock, flags);
  297. list_for_each_entry_safe(t, n, &core->task_queue, list)
  298. if (task == t) {
  299. list_del_init(&t->list);
  300. break;
  301. }
  302. spin_unlock_irqrestore(&core->task_queue_lock, flags);
  303. mutex_unlock(&core->task_queue_flush);
  304. if (task == t)
  305. return TASK_IS_NOT_AT_HA;
  306. }
  307. for (i = 0; i < 5; i++) {
  308. SAS_DPRINTK("%s: aborting task 0x%p\n", __func__, task);
  309. res = si->dft->lldd_abort_task(task);
  310. spin_lock_irqsave(&task->task_state_lock, flags);
  311. if (task->task_state_flags & SAS_TASK_STATE_DONE) {
  312. spin_unlock_irqrestore(&task->task_state_lock, flags);
  313. SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
  314. task);
  315. return TASK_IS_DONE;
  316. }
  317. spin_unlock_irqrestore(&task->task_state_lock, flags);
  318. if (res == TMF_RESP_FUNC_COMPLETE) {
  319. SAS_DPRINTK("%s: task 0x%p is aborted\n",
  320. __func__, task);
  321. return TASK_IS_ABORTED;
  322. } else if (si->dft->lldd_query_task) {
  323. SAS_DPRINTK("%s: querying task 0x%p\n",
  324. __func__, task);
  325. res = si->dft->lldd_query_task(task);
  326. switch (res) {
  327. case TMF_RESP_FUNC_SUCC:
  328. SAS_DPRINTK("%s: task 0x%p at LU\n",
  329. __func__, task);
  330. return TASK_IS_AT_LU;
  331. case TMF_RESP_FUNC_COMPLETE:
  332. SAS_DPRINTK("%s: task 0x%p not at LU\n",
  333. __func__, task);
  334. return TASK_IS_NOT_AT_LU;
  335. case TMF_RESP_FUNC_FAILED:
  336. SAS_DPRINTK("%s: task 0x%p failed to abort\n",
  337. __func__, task);
  338. return TASK_ABORT_FAILED;
  339. }
  340. }
  341. }
  342. return res;
  343. }
  344. static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
  345. {
  346. int res = TMF_RESP_FUNC_FAILED;
  347. struct scsi_lun lun;
  348. struct sas_internal *i =
  349. to_sas_internal(dev->port->ha->core.shost->transportt);
  350. int_to_scsilun(cmd->device->lun, &lun);
  351. SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
  352. SAS_ADDR(dev->sas_addr),
  353. cmd->device->lun);
  354. if (i->dft->lldd_abort_task_set)
  355. res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
  356. if (res == TMF_RESP_FUNC_FAILED) {
  357. if (i->dft->lldd_clear_task_set)
  358. res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
  359. }
  360. if (res == TMF_RESP_FUNC_FAILED) {
  361. if (i->dft->lldd_lu_reset)
  362. res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
  363. }
  364. return res;
  365. }
  366. static int sas_recover_I_T(struct domain_device *dev)
  367. {
  368. int res = TMF_RESP_FUNC_FAILED;
  369. struct sas_internal *i =
  370. to_sas_internal(dev->port->ha->core.shost->transportt);
  371. SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
  372. SAS_ADDR(dev->sas_addr));
  373. if (i->dft->lldd_I_T_nexus_reset)
  374. res = i->dft->lldd_I_T_nexus_reset(dev);
  375. return res;
  376. }
  377. /* take a reference on the last known good phy for this device */
  378. struct sas_phy *sas_get_local_phy(struct domain_device *dev)
  379. {
  380. struct sas_ha_struct *ha = dev->port->ha;
  381. struct sas_phy *phy;
  382. unsigned long flags;
  383. /* a published domain device always has a valid phy, it may be
  384. * stale, but it is never NULL
  385. */
  386. BUG_ON(!dev->phy);
  387. spin_lock_irqsave(&ha->phy_port_lock, flags);
  388. phy = dev->phy;
  389. get_device(&phy->dev);
  390. spin_unlock_irqrestore(&ha->phy_port_lock, flags);
  391. return phy;
  392. }
  393. EXPORT_SYMBOL_GPL(sas_get_local_phy);
  394. static void sas_wait_eh(struct domain_device *dev)
  395. {
  396. struct sas_ha_struct *ha = dev->port->ha;
  397. DEFINE_WAIT(wait);
  398. if (dev_is_sata(dev)) {
  399. ata_port_wait_eh(dev->sata_dev.ap);
  400. return;
  401. }
  402. retry:
  403. spin_lock_irq(&ha->lock);
  404. while (test_bit(SAS_DEV_EH_PENDING, &dev->state)) {
  405. prepare_to_wait(&ha->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
  406. spin_unlock_irq(&ha->lock);
  407. schedule();
  408. spin_lock_irq(&ha->lock);
  409. }
  410. finish_wait(&ha->eh_wait_q, &wait);
  411. spin_unlock_irq(&ha->lock);
  412. /* make sure SCSI EH is complete */
  413. if (scsi_host_in_recovery(ha->core.shost)) {
  414. msleep(10);
  415. goto retry;
  416. }
  417. }
  418. EXPORT_SYMBOL(sas_wait_eh);
  419. static int sas_queue_reset(struct domain_device *dev, int reset_type, int lun, int wait)
  420. {
  421. struct sas_ha_struct *ha = dev->port->ha;
  422. int scheduled = 0, tries = 100;
  423. /* ata: promote lun reset to bus reset */
  424. if (dev_is_sata(dev)) {
  425. sas_ata_schedule_reset(dev);
  426. if (wait)
  427. sas_ata_wait_eh(dev);
  428. return SUCCESS;
  429. }
  430. while (!scheduled && tries--) {
  431. spin_lock_irq(&ha->lock);
  432. if (!test_bit(SAS_DEV_EH_PENDING, &dev->state) &&
  433. !test_bit(reset_type, &dev->state)) {
  434. scheduled = 1;
  435. ha->eh_active++;
  436. list_add_tail(&dev->ssp_dev.eh_list_node, &ha->eh_dev_q);
  437. set_bit(SAS_DEV_EH_PENDING, &dev->state);
  438. set_bit(reset_type, &dev->state);
  439. int_to_scsilun(lun, &dev->ssp_dev.reset_lun);
  440. scsi_schedule_eh(ha->core.shost);
  441. }
  442. spin_unlock_irq(&ha->lock);
  443. if (wait)
  444. sas_wait_eh(dev);
  445. if (scheduled)
  446. return SUCCESS;
  447. }
  448. SAS_DPRINTK("%s reset of %s failed\n",
  449. reset_type == SAS_DEV_LU_RESET ? "LUN" : "Bus",
  450. dev_name(&dev->rphy->dev));
  451. return FAILED;
  452. }
  453. /* Attempt to send a LUN reset message to a device */
  454. int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
  455. {
  456. int res;
  457. struct scsi_lun lun;
  458. struct Scsi_Host *host = cmd->device->host;
  459. struct domain_device *dev = cmd_to_domain_dev(cmd);
  460. struct sas_internal *i = to_sas_internal(host->transportt);
  461. if (current != host->ehandler)
  462. return sas_queue_reset(dev, SAS_DEV_LU_RESET, cmd->device->lun, 0);
  463. int_to_scsilun(cmd->device->lun, &lun);
  464. if (!i->dft->lldd_lu_reset)
  465. return FAILED;
  466. res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
  467. if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
  468. return SUCCESS;
  469. return FAILED;
  470. }
  471. /* Attempt to send a phy (bus) reset */
  472. int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
  473. {
  474. struct domain_device *dev = cmd_to_domain_dev(cmd);
  475. struct sas_phy *phy = sas_get_local_phy(dev);
  476. struct Scsi_Host *host = cmd->device->host;
  477. int res;
  478. if (current != host->ehandler)
  479. return sas_queue_reset(dev, SAS_DEV_RESET, 0, 0);
  480. res = sas_phy_reset(phy, 1);
  481. if (res)
  482. SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
  483. kobject_name(&phy->dev.kobj),
  484. res);
  485. sas_put_local_phy(phy);
  486. if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
  487. return SUCCESS;
  488. return FAILED;
  489. }
  490. /* Try to reset a device */
  491. static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
  492. {
  493. int res;
  494. struct Scsi_Host *shost = cmd->device->host;
  495. if (!shost->hostt->eh_device_reset_handler)
  496. goto try_bus_reset;
  497. res = shost->hostt->eh_device_reset_handler(cmd);
  498. if (res == SUCCESS)
  499. return res;
  500. try_bus_reset:
  501. if (shost->hostt->eh_bus_reset_handler)
  502. return shost->hostt->eh_bus_reset_handler(cmd);
  503. return FAILED;
  504. }
  505. static void sas_eh_handle_sas_errors(struct Scsi_Host *shost, struct list_head *work_q)
  506. {
  507. struct scsi_cmnd *cmd, *n;
  508. enum task_disposition res = TASK_IS_DONE;
  509. int tmf_resp, need_reset;
  510. struct sas_internal *i = to_sas_internal(shost->transportt);
  511. unsigned long flags;
  512. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  513. LIST_HEAD(done);
  514. /* clean out any commands that won the completion vs eh race */
  515. list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
  516. struct domain_device *dev = cmd_to_domain_dev(cmd);
  517. struct sas_task *task;
  518. spin_lock_irqsave(&dev->done_lock, flags);
  519. /* by this point the lldd has either observed
  520. * SAS_HA_FROZEN and is leaving the task alone, or has
  521. * won the race with eh and decided to complete it
  522. */
  523. task = TO_SAS_TASK(cmd);
  524. spin_unlock_irqrestore(&dev->done_lock, flags);
  525. if (!task)
  526. list_move_tail(&cmd->eh_entry, &done);
  527. }
  528. Again:
  529. list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
  530. struct sas_task *task = TO_SAS_TASK(cmd);
  531. list_del_init(&cmd->eh_entry);
  532. spin_lock_irqsave(&task->task_state_lock, flags);
  533. need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
  534. spin_unlock_irqrestore(&task->task_state_lock, flags);
  535. if (need_reset) {
  536. SAS_DPRINTK("%s: task 0x%p requests reset\n",
  537. __func__, task);
  538. goto reset;
  539. }
  540. SAS_DPRINTK("trying to find task 0x%p\n", task);
  541. res = sas_scsi_find_task(task);
  542. cmd->eh_eflags = 0;
  543. switch (res) {
  544. case TASK_IS_NOT_AT_HA:
  545. SAS_DPRINTK("%s: task 0x%p is not at ha: %s\n",
  546. __func__, task,
  547. cmd->retries ? "retry" : "aborted");
  548. if (cmd->retries)
  549. cmd->retries--;
  550. sas_eh_finish_cmd(cmd);
  551. continue;
  552. case TASK_IS_DONE:
  553. SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
  554. task);
  555. sas_eh_defer_cmd(cmd);
  556. continue;
  557. case TASK_IS_ABORTED:
  558. SAS_DPRINTK("%s: task 0x%p is aborted\n",
  559. __func__, task);
  560. sas_eh_defer_cmd(cmd);
  561. continue;
  562. case TASK_IS_AT_LU:
  563. SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
  564. reset:
  565. tmf_resp = sas_recover_lu(task->dev, cmd);
  566. if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
  567. SAS_DPRINTK("dev %016llx LU %x is "
  568. "recovered\n",
  569. SAS_ADDR(task->dev),
  570. cmd->device->lun);
  571. sas_eh_defer_cmd(cmd);
  572. sas_scsi_clear_queue_lu(work_q, cmd);
  573. goto Again;
  574. }
  575. /* fallthrough */
  576. case TASK_IS_NOT_AT_LU:
  577. case TASK_ABORT_FAILED:
  578. SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
  579. task);
  580. tmf_resp = sas_recover_I_T(task->dev);
  581. if (tmf_resp == TMF_RESP_FUNC_COMPLETE ||
  582. tmf_resp == -ENODEV) {
  583. struct domain_device *dev = task->dev;
  584. SAS_DPRINTK("I_T %016llx recovered\n",
  585. SAS_ADDR(task->dev->sas_addr));
  586. sas_eh_finish_cmd(cmd);
  587. sas_scsi_clear_queue_I_T(work_q, dev);
  588. goto Again;
  589. }
  590. /* Hammer time :-) */
  591. try_to_reset_cmd_device(cmd);
  592. if (i->dft->lldd_clear_nexus_port) {
  593. struct asd_sas_port *port = task->dev->port;
  594. SAS_DPRINTK("clearing nexus for port:%d\n",
  595. port->id);
  596. res = i->dft->lldd_clear_nexus_port(port);
  597. if (res == TMF_RESP_FUNC_COMPLETE) {
  598. SAS_DPRINTK("clear nexus port:%d "
  599. "succeeded\n", port->id);
  600. sas_eh_finish_cmd(cmd);
  601. sas_scsi_clear_queue_port(work_q,
  602. port);
  603. goto Again;
  604. }
  605. }
  606. if (i->dft->lldd_clear_nexus_ha) {
  607. SAS_DPRINTK("clear nexus ha\n");
  608. res = i->dft->lldd_clear_nexus_ha(ha);
  609. if (res == TMF_RESP_FUNC_COMPLETE) {
  610. SAS_DPRINTK("clear nexus ha "
  611. "succeeded\n");
  612. sas_eh_finish_cmd(cmd);
  613. goto clear_q;
  614. }
  615. }
  616. /* If we are here -- this means that no amount
  617. * of effort could recover from errors. Quite
  618. * possibly the HA just disappeared.
  619. */
  620. SAS_DPRINTK("error from device %llx, LUN %x "
  621. "couldn't be recovered in any way\n",
  622. SAS_ADDR(task->dev->sas_addr),
  623. cmd->device->lun);
  624. sas_eh_finish_cmd(cmd);
  625. goto clear_q;
  626. }
  627. }
  628. out:
  629. list_splice_tail(&done, work_q);
  630. list_splice_tail_init(&ha->eh_ata_q, work_q);
  631. return;
  632. clear_q:
  633. SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__);
  634. list_for_each_entry_safe(cmd, n, work_q, eh_entry)
  635. sas_eh_finish_cmd(cmd);
  636. goto out;
  637. }
  638. static void sas_eh_handle_resets(struct Scsi_Host *shost)
  639. {
  640. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  641. struct sas_internal *i = to_sas_internal(shost->transportt);
  642. /* handle directed resets to sas devices */
  643. spin_lock_irq(&ha->lock);
  644. while (!list_empty(&ha->eh_dev_q)) {
  645. struct domain_device *dev;
  646. struct ssp_device *ssp;
  647. ssp = list_entry(ha->eh_dev_q.next, typeof(*ssp), eh_list_node);
  648. list_del_init(&ssp->eh_list_node);
  649. dev = container_of(ssp, typeof(*dev), ssp_dev);
  650. kref_get(&dev->kref);
  651. WARN_ONCE(dev_is_sata(dev), "ssp reset to ata device?\n");
  652. spin_unlock_irq(&ha->lock);
  653. if (test_and_clear_bit(SAS_DEV_LU_RESET, &dev->state))
  654. i->dft->lldd_lu_reset(dev, ssp->reset_lun.scsi_lun);
  655. if (test_and_clear_bit(SAS_DEV_RESET, &dev->state))
  656. i->dft->lldd_I_T_nexus_reset(dev);
  657. sas_put_device(dev);
  658. spin_lock_irq(&ha->lock);
  659. clear_bit(SAS_DEV_EH_PENDING, &dev->state);
  660. ha->eh_active--;
  661. }
  662. spin_unlock_irq(&ha->lock);
  663. }
  664. void sas_scsi_recover_host(struct Scsi_Host *shost)
  665. {
  666. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  667. LIST_HEAD(eh_work_q);
  668. int tries = 0;
  669. bool retry;
  670. retry:
  671. tries++;
  672. retry = true;
  673. spin_lock_irq(shost->host_lock);
  674. list_splice_init(&shost->eh_cmd_q, &eh_work_q);
  675. spin_unlock_irq(shost->host_lock);
  676. SAS_DPRINTK("Enter %s busy: %d failed: %d\n",
  677. __func__, shost->host_busy, shost->host_failed);
  678. /*
  679. * Deal with commands that still have SAS tasks (i.e. they didn't
  680. * complete via the normal sas_task completion mechanism),
  681. * SAS_HA_FROZEN gives eh dominion over all sas_task completion.
  682. */
  683. set_bit(SAS_HA_FROZEN, &ha->state);
  684. sas_eh_handle_sas_errors(shost, &eh_work_q);
  685. clear_bit(SAS_HA_FROZEN, &ha->state);
  686. if (list_empty(&eh_work_q))
  687. goto out;
  688. /*
  689. * Now deal with SCSI commands that completed ok but have a an error
  690. * code (and hopefully sense data) attached. This is roughly what
  691. * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
  692. * command we see here has no sas_task and is thus unknown to the HA.
  693. */
  694. sas_ata_eh(shost, &eh_work_q, &ha->eh_done_q);
  695. if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
  696. scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
  697. out:
  698. if (ha->lldd_max_execute_num > 1)
  699. wake_up_process(ha->core.queue_thread);
  700. sas_eh_handle_resets(shost);
  701. /* now link into libata eh --- if we have any ata devices */
  702. sas_ata_strategy_handler(shost);
  703. scsi_eh_flush_done_q(&ha->eh_done_q);
  704. /* check if any new eh work was scheduled during the last run */
  705. spin_lock_irq(&ha->lock);
  706. if (ha->eh_active == 0) {
  707. shost->host_eh_scheduled = 0;
  708. retry = false;
  709. }
  710. spin_unlock_irq(&ha->lock);
  711. if (retry)
  712. goto retry;
  713. SAS_DPRINTK("--- Exit %s: busy: %d failed: %d tries: %d\n",
  714. __func__, shost->host_busy, shost->host_failed, tries);
  715. }
  716. enum blk_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
  717. {
  718. scmd_printk(KERN_DEBUG, cmd, "command %p timed out\n", cmd);
  719. return BLK_EH_NOT_HANDLED;
  720. }
  721. int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
  722. {
  723. struct domain_device *dev = sdev_to_domain_dev(sdev);
  724. if (dev_is_sata(dev))
  725. return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
  726. return -EINVAL;
  727. }
  728. struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
  729. {
  730. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
  731. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  732. struct domain_device *found_dev = NULL;
  733. int i;
  734. unsigned long flags;
  735. spin_lock_irqsave(&ha->phy_port_lock, flags);
  736. for (i = 0; i < ha->num_phys; i++) {
  737. struct asd_sas_port *port = ha->sas_port[i];
  738. struct domain_device *dev;
  739. spin_lock(&port->dev_list_lock);
  740. list_for_each_entry(dev, &port->dev_list, dev_list_node) {
  741. if (rphy == dev->rphy) {
  742. found_dev = dev;
  743. spin_unlock(&port->dev_list_lock);
  744. goto found;
  745. }
  746. }
  747. spin_unlock(&port->dev_list_lock);
  748. }
  749. found:
  750. spin_unlock_irqrestore(&ha->phy_port_lock, flags);
  751. return found_dev;
  752. }
  753. int sas_target_alloc(struct scsi_target *starget)
  754. {
  755. struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
  756. struct domain_device *found_dev = sas_find_dev_by_rphy(rphy);
  757. if (!found_dev)
  758. return -ENODEV;
  759. kref_get(&found_dev->kref);
  760. starget->hostdata = found_dev;
  761. return 0;
  762. }
  763. #define SAS_DEF_QD 256
  764. int sas_slave_configure(struct scsi_device *scsi_dev)
  765. {
  766. struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
  767. struct sas_ha_struct *sas_ha;
  768. BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
  769. if (dev_is_sata(dev)) {
  770. ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
  771. return 0;
  772. }
  773. sas_ha = dev->port->ha;
  774. sas_read_port_mode_page(scsi_dev);
  775. if (scsi_dev->tagged_supported) {
  776. scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
  777. scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
  778. } else {
  779. SAS_DPRINTK("device %llx, LUN %x doesn't support "
  780. "TCQ\n", SAS_ADDR(dev->sas_addr),
  781. scsi_dev->lun);
  782. scsi_dev->tagged_supported = 0;
  783. scsi_set_tag_type(scsi_dev, 0);
  784. scsi_deactivate_tcq(scsi_dev, 1);
  785. }
  786. scsi_dev->allow_restart = 1;
  787. return 0;
  788. }
  789. int sas_change_queue_depth(struct scsi_device *sdev, int depth, int reason)
  790. {
  791. struct domain_device *dev = sdev_to_domain_dev(sdev);
  792. if (dev_is_sata(dev))
  793. return __ata_change_queue_depth(dev->sata_dev.ap, sdev, depth,
  794. reason);
  795. switch (reason) {
  796. case SCSI_QDEPTH_DEFAULT:
  797. case SCSI_QDEPTH_RAMP_UP:
  798. if (!sdev->tagged_supported)
  799. depth = 1;
  800. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  801. break;
  802. case SCSI_QDEPTH_QFULL:
  803. scsi_track_queue_full(sdev, depth);
  804. break;
  805. default:
  806. return -EOPNOTSUPP;
  807. }
  808. return depth;
  809. }
  810. int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
  811. {
  812. struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
  813. if (dev_is_sata(dev))
  814. return -EINVAL;
  815. if (!scsi_dev->tagged_supported)
  816. return 0;
  817. scsi_deactivate_tcq(scsi_dev, 1);
  818. scsi_set_tag_type(scsi_dev, qt);
  819. scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
  820. return qt;
  821. }
  822. int sas_bios_param(struct scsi_device *scsi_dev,
  823. struct block_device *bdev,
  824. sector_t capacity, int *hsc)
  825. {
  826. hsc[0] = 255;
  827. hsc[1] = 63;
  828. sector_div(capacity, 255*63);
  829. hsc[2] = capacity;
  830. return 0;
  831. }
  832. /* ---------- Task Collector Thread implementation ---------- */
  833. static void sas_queue(struct sas_ha_struct *sas_ha)
  834. {
  835. struct scsi_core *core = &sas_ha->core;
  836. unsigned long flags;
  837. LIST_HEAD(q);
  838. int can_queue;
  839. int res;
  840. struct sas_internal *i = to_sas_internal(core->shost->transportt);
  841. mutex_lock(&core->task_queue_flush);
  842. spin_lock_irqsave(&core->task_queue_lock, flags);
  843. while (!kthread_should_stop() &&
  844. !list_empty(&core->task_queue) &&
  845. !test_bit(SAS_HA_FROZEN, &sas_ha->state)) {
  846. can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
  847. if (can_queue >= 0) {
  848. can_queue = core->task_queue_size;
  849. list_splice_init(&core->task_queue, &q);
  850. } else {
  851. struct list_head *a, *n;
  852. can_queue = sas_ha->lldd_queue_size;
  853. list_for_each_safe(a, n, &core->task_queue) {
  854. list_move_tail(a, &q);
  855. if (--can_queue == 0)
  856. break;
  857. }
  858. can_queue = sas_ha->lldd_queue_size;
  859. }
  860. core->task_queue_size -= can_queue;
  861. spin_unlock_irqrestore(&core->task_queue_lock, flags);
  862. {
  863. struct sas_task *task = list_entry(q.next,
  864. struct sas_task,
  865. list);
  866. list_del_init(&q);
  867. res = i->dft->lldd_execute_task(task, can_queue,
  868. GFP_KERNEL);
  869. if (unlikely(res))
  870. __list_add(&q, task->list.prev, &task->list);
  871. }
  872. spin_lock_irqsave(&core->task_queue_lock, flags);
  873. if (res) {
  874. list_splice_init(&q, &core->task_queue); /*at head*/
  875. core->task_queue_size += can_queue;
  876. }
  877. }
  878. spin_unlock_irqrestore(&core->task_queue_lock, flags);
  879. mutex_unlock(&core->task_queue_flush);
  880. }
  881. /**
  882. * sas_queue_thread -- The Task Collector thread
  883. * @_sas_ha: pointer to struct sas_ha
  884. */
  885. static int sas_queue_thread(void *_sas_ha)
  886. {
  887. struct sas_ha_struct *sas_ha = _sas_ha;
  888. while (1) {
  889. set_current_state(TASK_INTERRUPTIBLE);
  890. schedule();
  891. sas_queue(sas_ha);
  892. if (kthread_should_stop())
  893. break;
  894. }
  895. return 0;
  896. }
  897. int sas_init_queue(struct sas_ha_struct *sas_ha)
  898. {
  899. struct scsi_core *core = &sas_ha->core;
  900. spin_lock_init(&core->task_queue_lock);
  901. mutex_init(&core->task_queue_flush);
  902. core->task_queue_size = 0;
  903. INIT_LIST_HEAD(&core->task_queue);
  904. core->queue_thread = kthread_run(sas_queue_thread, sas_ha,
  905. "sas_queue_%d", core->shost->host_no);
  906. if (IS_ERR(core->queue_thread))
  907. return PTR_ERR(core->queue_thread);
  908. return 0;
  909. }
  910. void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
  911. {
  912. unsigned long flags;
  913. struct scsi_core *core = &sas_ha->core;
  914. struct sas_task *task, *n;
  915. kthread_stop(core->queue_thread);
  916. if (!list_empty(&core->task_queue))
  917. SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
  918. SAS_ADDR(sas_ha->sas_addr));
  919. spin_lock_irqsave(&core->task_queue_lock, flags);
  920. list_for_each_entry_safe(task, n, &core->task_queue, list) {
  921. struct scsi_cmnd *cmd = task->uldd_task;
  922. list_del_init(&task->list);
  923. ASSIGN_SAS_TASK(cmd, NULL);
  924. sas_free_task(task);
  925. cmd->result = DID_ABORT << 16;
  926. cmd->scsi_done(cmd);
  927. }
  928. spin_unlock_irqrestore(&core->task_queue_lock, flags);
  929. }
  930. /*
  931. * Tell an upper layer that it needs to initiate an abort for a given task.
  932. * This should only ever be called by an LLDD.
  933. */
  934. void sas_task_abort(struct sas_task *task)
  935. {
  936. struct scsi_cmnd *sc = task->uldd_task;
  937. /* Escape for libsas internal commands */
  938. if (!sc) {
  939. if (!del_timer(&task->timer))
  940. return;
  941. task->timer.function(task->timer.data);
  942. return;
  943. }
  944. if (dev_is_sata(task->dev)) {
  945. sas_ata_task_abort(task);
  946. } else {
  947. struct request_queue *q = sc->device->request_queue;
  948. unsigned long flags;
  949. spin_lock_irqsave(q->queue_lock, flags);
  950. blk_abort_request(sc->request);
  951. spin_unlock_irqrestore(q->queue_lock, flags);
  952. }
  953. }
  954. void sas_target_destroy(struct scsi_target *starget)
  955. {
  956. struct domain_device *found_dev = starget->hostdata;
  957. if (!found_dev)
  958. return;
  959. starget->hostdata = NULL;
  960. sas_put_device(found_dev);
  961. }
  962. static void sas_parse_addr(u8 *sas_addr, const char *p)
  963. {
  964. int i;
  965. for (i = 0; i < SAS_ADDR_SIZE; i++) {
  966. u8 h, l;
  967. if (!*p)
  968. break;
  969. h = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
  970. p++;
  971. l = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
  972. p++;
  973. sas_addr[i] = (h<<4) | l;
  974. }
  975. }
  976. #define SAS_STRING_ADDR_SIZE 16
  977. int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
  978. {
  979. int res;
  980. const struct firmware *fw;
  981. res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
  982. if (res)
  983. return res;
  984. if (fw->size < SAS_STRING_ADDR_SIZE) {
  985. res = -ENODEV;
  986. goto out;
  987. }
  988. sas_parse_addr(addr, fw->data);
  989. out:
  990. release_firmware(fw);
  991. return res;
  992. }
  993. EXPORT_SYMBOL_GPL(sas_request_addr);
  994. EXPORT_SYMBOL_GPL(sas_queuecommand);
  995. EXPORT_SYMBOL_GPL(sas_target_alloc);
  996. EXPORT_SYMBOL_GPL(sas_slave_configure);
  997. EXPORT_SYMBOL_GPL(sas_change_queue_depth);
  998. EXPORT_SYMBOL_GPL(sas_change_queue_type);
  999. EXPORT_SYMBOL_GPL(sas_bios_param);
  1000. EXPORT_SYMBOL_GPL(sas_task_abort);
  1001. EXPORT_SYMBOL_GPL(sas_phy_reset);
  1002. EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
  1003. EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
  1004. EXPORT_SYMBOL_GPL(sas_target_destroy);
  1005. EXPORT_SYMBOL_GPL(sas_ioctl);