sas_scsi_host.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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 domain_device *dev = cmd_to_domain_dev(cmd);
  223. struct sas_task *task = TO_SAS_TASK(cmd);
  224. /* At this point, we only get called following an actual abort
  225. * of the task, so we should be guaranteed not to be racing with
  226. * any completions from the LLD. Task is freed after this.
  227. */
  228. sas_end_task(cmd, task);
  229. if (dev_is_sata(dev)) {
  230. /* defer commands to libata so that libata EH can
  231. * handle ata qcs correctly
  232. */
  233. list_move_tail(&cmd->eh_entry, &sas_ha->eh_ata_q);
  234. return;
  235. }
  236. /* now finish the command and move it on to the error
  237. * handler done list, this also takes it off the
  238. * error handler pending list.
  239. */
  240. scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
  241. }
  242. static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
  243. {
  244. struct scsi_cmnd *cmd, *n;
  245. list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
  246. if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
  247. cmd->device->lun == my_cmd->device->lun)
  248. sas_eh_finish_cmd(cmd);
  249. }
  250. }
  251. static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
  252. struct domain_device *dev)
  253. {
  254. struct scsi_cmnd *cmd, *n;
  255. list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
  256. struct domain_device *x = cmd_to_domain_dev(cmd);
  257. if (x == dev)
  258. sas_eh_finish_cmd(cmd);
  259. }
  260. }
  261. static void sas_scsi_clear_queue_port(struct list_head *error_q,
  262. struct asd_sas_port *port)
  263. {
  264. struct scsi_cmnd *cmd, *n;
  265. list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
  266. struct domain_device *dev = cmd_to_domain_dev(cmd);
  267. struct asd_sas_port *x = dev->port;
  268. if (x == port)
  269. sas_eh_finish_cmd(cmd);
  270. }
  271. }
  272. enum task_disposition {
  273. TASK_IS_DONE,
  274. TASK_IS_ABORTED,
  275. TASK_IS_AT_LU,
  276. TASK_IS_NOT_AT_HA,
  277. TASK_IS_NOT_AT_LU,
  278. TASK_ABORT_FAILED,
  279. };
  280. static enum task_disposition sas_scsi_find_task(struct sas_task *task)
  281. {
  282. struct sas_ha_struct *ha = task->dev->port->ha;
  283. unsigned long flags;
  284. int i, res;
  285. struct sas_internal *si =
  286. to_sas_internal(task->dev->port->ha->core.shost->transportt);
  287. if (ha->lldd_max_execute_num > 1) {
  288. struct scsi_core *core = &ha->core;
  289. struct sas_task *t, *n;
  290. mutex_lock(&core->task_queue_flush);
  291. spin_lock_irqsave(&core->task_queue_lock, flags);
  292. list_for_each_entry_safe(t, n, &core->task_queue, list)
  293. if (task == t) {
  294. list_del_init(&t->list);
  295. break;
  296. }
  297. spin_unlock_irqrestore(&core->task_queue_lock, flags);
  298. mutex_unlock(&core->task_queue_flush);
  299. if (task == t)
  300. return TASK_IS_NOT_AT_HA;
  301. }
  302. for (i = 0; i < 5; i++) {
  303. SAS_DPRINTK("%s: aborting task 0x%p\n", __func__, task);
  304. res = si->dft->lldd_abort_task(task);
  305. spin_lock_irqsave(&task->task_state_lock, flags);
  306. if (task->task_state_flags & SAS_TASK_STATE_DONE) {
  307. spin_unlock_irqrestore(&task->task_state_lock, flags);
  308. SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
  309. task);
  310. return TASK_IS_DONE;
  311. }
  312. spin_unlock_irqrestore(&task->task_state_lock, flags);
  313. if (res == TMF_RESP_FUNC_COMPLETE) {
  314. SAS_DPRINTK("%s: task 0x%p is aborted\n",
  315. __func__, task);
  316. return TASK_IS_ABORTED;
  317. } else if (si->dft->lldd_query_task) {
  318. SAS_DPRINTK("%s: querying task 0x%p\n",
  319. __func__, task);
  320. res = si->dft->lldd_query_task(task);
  321. switch (res) {
  322. case TMF_RESP_FUNC_SUCC:
  323. SAS_DPRINTK("%s: task 0x%p at LU\n",
  324. __func__, task);
  325. return TASK_IS_AT_LU;
  326. case TMF_RESP_FUNC_COMPLETE:
  327. SAS_DPRINTK("%s: task 0x%p not at LU\n",
  328. __func__, task);
  329. return TASK_IS_NOT_AT_LU;
  330. case TMF_RESP_FUNC_FAILED:
  331. SAS_DPRINTK("%s: task 0x%p failed to abort\n",
  332. __func__, task);
  333. return TASK_ABORT_FAILED;
  334. }
  335. }
  336. }
  337. return res;
  338. }
  339. static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
  340. {
  341. int res = TMF_RESP_FUNC_FAILED;
  342. struct scsi_lun lun;
  343. struct sas_internal *i =
  344. to_sas_internal(dev->port->ha->core.shost->transportt);
  345. int_to_scsilun(cmd->device->lun, &lun);
  346. SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
  347. SAS_ADDR(dev->sas_addr),
  348. cmd->device->lun);
  349. if (i->dft->lldd_abort_task_set)
  350. res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
  351. if (res == TMF_RESP_FUNC_FAILED) {
  352. if (i->dft->lldd_clear_task_set)
  353. res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
  354. }
  355. if (res == TMF_RESP_FUNC_FAILED) {
  356. if (i->dft->lldd_lu_reset)
  357. res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
  358. }
  359. return res;
  360. }
  361. static int sas_recover_I_T(struct domain_device *dev)
  362. {
  363. int res = TMF_RESP_FUNC_FAILED;
  364. struct sas_internal *i =
  365. to_sas_internal(dev->port->ha->core.shost->transportt);
  366. SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
  367. SAS_ADDR(dev->sas_addr));
  368. if (i->dft->lldd_I_T_nexus_reset)
  369. res = i->dft->lldd_I_T_nexus_reset(dev);
  370. return res;
  371. }
  372. /* take a reference on the last known good phy for this device */
  373. struct sas_phy *sas_get_local_phy(struct domain_device *dev)
  374. {
  375. struct sas_ha_struct *ha = dev->port->ha;
  376. struct sas_phy *phy;
  377. unsigned long flags;
  378. /* a published domain device always has a valid phy, it may be
  379. * stale, but it is never NULL
  380. */
  381. BUG_ON(!dev->phy);
  382. spin_lock_irqsave(&ha->phy_port_lock, flags);
  383. phy = dev->phy;
  384. get_device(&phy->dev);
  385. spin_unlock_irqrestore(&ha->phy_port_lock, flags);
  386. return phy;
  387. }
  388. EXPORT_SYMBOL_GPL(sas_get_local_phy);
  389. /* Attempt to send a LUN reset message to a device */
  390. int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
  391. {
  392. struct domain_device *dev = cmd_to_domain_dev(cmd);
  393. struct sas_internal *i =
  394. to_sas_internal(dev->port->ha->core.shost->transportt);
  395. struct scsi_lun lun;
  396. int res;
  397. int_to_scsilun(cmd->device->lun, &lun);
  398. if (!i->dft->lldd_lu_reset)
  399. return FAILED;
  400. res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
  401. if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
  402. return SUCCESS;
  403. return FAILED;
  404. }
  405. /* Attempt to send a phy (bus) reset */
  406. int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
  407. {
  408. struct domain_device *dev = cmd_to_domain_dev(cmd);
  409. struct sas_phy *phy = sas_get_local_phy(dev);
  410. int res;
  411. res = sas_phy_reset(phy, 1);
  412. if (res)
  413. SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
  414. kobject_name(&phy->dev.kobj),
  415. res);
  416. sas_put_local_phy(phy);
  417. if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
  418. return SUCCESS;
  419. return FAILED;
  420. }
  421. /* Try to reset a device */
  422. static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
  423. {
  424. int res;
  425. struct Scsi_Host *shost = cmd->device->host;
  426. if (!shost->hostt->eh_device_reset_handler)
  427. goto try_bus_reset;
  428. res = shost->hostt->eh_device_reset_handler(cmd);
  429. if (res == SUCCESS)
  430. return res;
  431. try_bus_reset:
  432. if (shost->hostt->eh_bus_reset_handler)
  433. return shost->hostt->eh_bus_reset_handler(cmd);
  434. return FAILED;
  435. }
  436. static void sas_eh_handle_sas_errors(struct Scsi_Host *shost, struct list_head *work_q)
  437. {
  438. struct scsi_cmnd *cmd, *n;
  439. enum task_disposition res = TASK_IS_DONE;
  440. int tmf_resp, need_reset;
  441. struct sas_internal *i = to_sas_internal(shost->transportt);
  442. unsigned long flags;
  443. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  444. LIST_HEAD(done);
  445. /* clean out any commands that won the completion vs eh race */
  446. list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
  447. struct domain_device *dev = cmd_to_domain_dev(cmd);
  448. struct sas_task *task;
  449. spin_lock_irqsave(&dev->done_lock, flags);
  450. /* by this point the lldd has either observed
  451. * SAS_HA_FROZEN and is leaving the task alone, or has
  452. * won the race with eh and decided to complete it
  453. */
  454. task = TO_SAS_TASK(cmd);
  455. spin_unlock_irqrestore(&dev->done_lock, flags);
  456. if (!task)
  457. list_move_tail(&cmd->eh_entry, &done);
  458. }
  459. Again:
  460. list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
  461. struct sas_task *task = TO_SAS_TASK(cmd);
  462. list_del_init(&cmd->eh_entry);
  463. spin_lock_irqsave(&task->task_state_lock, flags);
  464. need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
  465. spin_unlock_irqrestore(&task->task_state_lock, flags);
  466. if (need_reset) {
  467. SAS_DPRINTK("%s: task 0x%p requests reset\n",
  468. __func__, task);
  469. goto reset;
  470. }
  471. SAS_DPRINTK("trying to find task 0x%p\n", task);
  472. res = sas_scsi_find_task(task);
  473. cmd->eh_eflags = 0;
  474. switch (res) {
  475. case TASK_IS_NOT_AT_HA:
  476. SAS_DPRINTK("%s: task 0x%p is not at ha: %s\n",
  477. __func__, task,
  478. cmd->retries ? "retry" : "aborted");
  479. if (cmd->retries)
  480. cmd->retries--;
  481. sas_eh_finish_cmd(cmd);
  482. continue;
  483. case TASK_IS_DONE:
  484. SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
  485. task);
  486. sas_eh_finish_cmd(cmd);
  487. continue;
  488. case TASK_IS_ABORTED:
  489. SAS_DPRINTK("%s: task 0x%p is aborted\n",
  490. __func__, task);
  491. sas_eh_finish_cmd(cmd);
  492. continue;
  493. case TASK_IS_AT_LU:
  494. SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
  495. reset:
  496. tmf_resp = sas_recover_lu(task->dev, cmd);
  497. if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
  498. SAS_DPRINTK("dev %016llx LU %x is "
  499. "recovered\n",
  500. SAS_ADDR(task->dev),
  501. cmd->device->lun);
  502. sas_eh_finish_cmd(cmd);
  503. sas_scsi_clear_queue_lu(work_q, cmd);
  504. goto Again;
  505. }
  506. /* fallthrough */
  507. case TASK_IS_NOT_AT_LU:
  508. case TASK_ABORT_FAILED:
  509. SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
  510. task);
  511. tmf_resp = sas_recover_I_T(task->dev);
  512. if (tmf_resp == TMF_RESP_FUNC_COMPLETE ||
  513. tmf_resp == -ENODEV) {
  514. struct domain_device *dev = task->dev;
  515. SAS_DPRINTK("I_T %016llx recovered\n",
  516. SAS_ADDR(task->dev->sas_addr));
  517. sas_eh_finish_cmd(cmd);
  518. sas_scsi_clear_queue_I_T(work_q, dev);
  519. goto Again;
  520. }
  521. /* Hammer time :-) */
  522. try_to_reset_cmd_device(cmd);
  523. if (i->dft->lldd_clear_nexus_port) {
  524. struct asd_sas_port *port = task->dev->port;
  525. SAS_DPRINTK("clearing nexus for port:%d\n",
  526. port->id);
  527. res = i->dft->lldd_clear_nexus_port(port);
  528. if (res == TMF_RESP_FUNC_COMPLETE) {
  529. SAS_DPRINTK("clear nexus port:%d "
  530. "succeeded\n", port->id);
  531. sas_eh_finish_cmd(cmd);
  532. sas_scsi_clear_queue_port(work_q,
  533. port);
  534. goto Again;
  535. }
  536. }
  537. if (i->dft->lldd_clear_nexus_ha) {
  538. SAS_DPRINTK("clear nexus ha\n");
  539. res = i->dft->lldd_clear_nexus_ha(ha);
  540. if (res == TMF_RESP_FUNC_COMPLETE) {
  541. SAS_DPRINTK("clear nexus ha "
  542. "succeeded\n");
  543. sas_eh_finish_cmd(cmd);
  544. goto clear_q;
  545. }
  546. }
  547. /* If we are here -- this means that no amount
  548. * of effort could recover from errors. Quite
  549. * possibly the HA just disappeared.
  550. */
  551. SAS_DPRINTK("error from device %llx, LUN %x "
  552. "couldn't be recovered in any way\n",
  553. SAS_ADDR(task->dev->sas_addr),
  554. cmd->device->lun);
  555. sas_eh_finish_cmd(cmd);
  556. goto clear_q;
  557. }
  558. }
  559. out:
  560. list_splice_tail(&done, work_q);
  561. list_splice_tail_init(&ha->eh_ata_q, work_q);
  562. return;
  563. clear_q:
  564. SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__);
  565. list_for_each_entry_safe(cmd, n, work_q, eh_entry)
  566. sas_eh_finish_cmd(cmd);
  567. goto out;
  568. }
  569. void sas_scsi_recover_host(struct Scsi_Host *shost)
  570. {
  571. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  572. unsigned long flags;
  573. LIST_HEAD(eh_work_q);
  574. spin_lock_irqsave(shost->host_lock, flags);
  575. list_splice_init(&shost->eh_cmd_q, &eh_work_q);
  576. shost->host_eh_scheduled = 0;
  577. spin_unlock_irqrestore(shost->host_lock, flags);
  578. SAS_DPRINTK("Enter %s busy: %d failed: %d\n",
  579. __func__, shost->host_busy, shost->host_failed);
  580. /*
  581. * Deal with commands that still have SAS tasks (i.e. they didn't
  582. * complete via the normal sas_task completion mechanism),
  583. * SAS_HA_FROZEN gives eh dominion over all sas_task completion.
  584. */
  585. set_bit(SAS_HA_FROZEN, &ha->state);
  586. sas_eh_handle_sas_errors(shost, &eh_work_q);
  587. clear_bit(SAS_HA_FROZEN, &ha->state);
  588. if (list_empty(&eh_work_q))
  589. goto out;
  590. /*
  591. * Now deal with SCSI commands that completed ok but have a an error
  592. * code (and hopefully sense data) attached. This is roughly what
  593. * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
  594. * command we see here has no sas_task and is thus unknown to the HA.
  595. */
  596. sas_ata_eh(shost, &eh_work_q, &ha->eh_done_q);
  597. if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
  598. scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
  599. out:
  600. if (ha->lldd_max_execute_num > 1)
  601. wake_up_process(ha->core.queue_thread);
  602. /* now link into libata eh --- if we have any ata devices */
  603. sas_ata_strategy_handler(shost);
  604. scsi_eh_flush_done_q(&ha->eh_done_q);
  605. SAS_DPRINTK("--- Exit %s: busy: %d failed: %d\n",
  606. __func__, shost->host_busy, shost->host_failed);
  607. }
  608. enum blk_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
  609. {
  610. scmd_printk(KERN_DEBUG, cmd, "command %p timed out\n", cmd);
  611. return BLK_EH_NOT_HANDLED;
  612. }
  613. int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
  614. {
  615. struct domain_device *dev = sdev_to_domain_dev(sdev);
  616. if (dev_is_sata(dev))
  617. return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
  618. return -EINVAL;
  619. }
  620. struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
  621. {
  622. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
  623. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  624. struct domain_device *found_dev = NULL;
  625. int i;
  626. unsigned long flags;
  627. spin_lock_irqsave(&ha->phy_port_lock, flags);
  628. for (i = 0; i < ha->num_phys; i++) {
  629. struct asd_sas_port *port = ha->sas_port[i];
  630. struct domain_device *dev;
  631. spin_lock(&port->dev_list_lock);
  632. list_for_each_entry(dev, &port->dev_list, dev_list_node) {
  633. if (rphy == dev->rphy) {
  634. found_dev = dev;
  635. spin_unlock(&port->dev_list_lock);
  636. goto found;
  637. }
  638. }
  639. spin_unlock(&port->dev_list_lock);
  640. }
  641. found:
  642. spin_unlock_irqrestore(&ha->phy_port_lock, flags);
  643. return found_dev;
  644. }
  645. int sas_target_alloc(struct scsi_target *starget)
  646. {
  647. struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
  648. struct domain_device *found_dev = sas_find_dev_by_rphy(rphy);
  649. if (!found_dev)
  650. return -ENODEV;
  651. kref_get(&found_dev->kref);
  652. starget->hostdata = found_dev;
  653. return 0;
  654. }
  655. #define SAS_DEF_QD 256
  656. int sas_slave_configure(struct scsi_device *scsi_dev)
  657. {
  658. struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
  659. struct sas_ha_struct *sas_ha;
  660. BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
  661. if (dev_is_sata(dev)) {
  662. ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
  663. return 0;
  664. }
  665. sas_ha = dev->port->ha;
  666. sas_read_port_mode_page(scsi_dev);
  667. if (scsi_dev->tagged_supported) {
  668. scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
  669. scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
  670. } else {
  671. SAS_DPRINTK("device %llx, LUN %x doesn't support "
  672. "TCQ\n", SAS_ADDR(dev->sas_addr),
  673. scsi_dev->lun);
  674. scsi_dev->tagged_supported = 0;
  675. scsi_set_tag_type(scsi_dev, 0);
  676. scsi_deactivate_tcq(scsi_dev, 1);
  677. }
  678. scsi_dev->allow_restart = 1;
  679. return 0;
  680. }
  681. int sas_change_queue_depth(struct scsi_device *sdev, int depth, int reason)
  682. {
  683. struct domain_device *dev = sdev_to_domain_dev(sdev);
  684. if (dev_is_sata(dev))
  685. return __ata_change_queue_depth(dev->sata_dev.ap, sdev, depth,
  686. reason);
  687. switch (reason) {
  688. case SCSI_QDEPTH_DEFAULT:
  689. case SCSI_QDEPTH_RAMP_UP:
  690. if (!sdev->tagged_supported)
  691. depth = 1;
  692. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  693. break;
  694. case SCSI_QDEPTH_QFULL:
  695. scsi_track_queue_full(sdev, depth);
  696. break;
  697. default:
  698. return -EOPNOTSUPP;
  699. }
  700. return depth;
  701. }
  702. int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
  703. {
  704. struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
  705. if (dev_is_sata(dev))
  706. return -EINVAL;
  707. if (!scsi_dev->tagged_supported)
  708. return 0;
  709. scsi_deactivate_tcq(scsi_dev, 1);
  710. scsi_set_tag_type(scsi_dev, qt);
  711. scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
  712. return qt;
  713. }
  714. int sas_bios_param(struct scsi_device *scsi_dev,
  715. struct block_device *bdev,
  716. sector_t capacity, int *hsc)
  717. {
  718. hsc[0] = 255;
  719. hsc[1] = 63;
  720. sector_div(capacity, 255*63);
  721. hsc[2] = capacity;
  722. return 0;
  723. }
  724. /* ---------- Task Collector Thread implementation ---------- */
  725. static void sas_queue(struct sas_ha_struct *sas_ha)
  726. {
  727. struct scsi_core *core = &sas_ha->core;
  728. unsigned long flags;
  729. LIST_HEAD(q);
  730. int can_queue;
  731. int res;
  732. struct sas_internal *i = to_sas_internal(core->shost->transportt);
  733. mutex_lock(&core->task_queue_flush);
  734. spin_lock_irqsave(&core->task_queue_lock, flags);
  735. while (!kthread_should_stop() &&
  736. !list_empty(&core->task_queue) &&
  737. !test_bit(SAS_HA_FROZEN, &sas_ha->state)) {
  738. can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
  739. if (can_queue >= 0) {
  740. can_queue = core->task_queue_size;
  741. list_splice_init(&core->task_queue, &q);
  742. } else {
  743. struct list_head *a, *n;
  744. can_queue = sas_ha->lldd_queue_size;
  745. list_for_each_safe(a, n, &core->task_queue) {
  746. list_move_tail(a, &q);
  747. if (--can_queue == 0)
  748. break;
  749. }
  750. can_queue = sas_ha->lldd_queue_size;
  751. }
  752. core->task_queue_size -= can_queue;
  753. spin_unlock_irqrestore(&core->task_queue_lock, flags);
  754. {
  755. struct sas_task *task = list_entry(q.next,
  756. struct sas_task,
  757. list);
  758. list_del_init(&q);
  759. res = i->dft->lldd_execute_task(task, can_queue,
  760. GFP_KERNEL);
  761. if (unlikely(res))
  762. __list_add(&q, task->list.prev, &task->list);
  763. }
  764. spin_lock_irqsave(&core->task_queue_lock, flags);
  765. if (res) {
  766. list_splice_init(&q, &core->task_queue); /*at head*/
  767. core->task_queue_size += can_queue;
  768. }
  769. }
  770. spin_unlock_irqrestore(&core->task_queue_lock, flags);
  771. mutex_unlock(&core->task_queue_flush);
  772. }
  773. /**
  774. * sas_queue_thread -- The Task Collector thread
  775. * @_sas_ha: pointer to struct sas_ha
  776. */
  777. static int sas_queue_thread(void *_sas_ha)
  778. {
  779. struct sas_ha_struct *sas_ha = _sas_ha;
  780. while (1) {
  781. set_current_state(TASK_INTERRUPTIBLE);
  782. schedule();
  783. sas_queue(sas_ha);
  784. if (kthread_should_stop())
  785. break;
  786. }
  787. return 0;
  788. }
  789. int sas_init_queue(struct sas_ha_struct *sas_ha)
  790. {
  791. struct scsi_core *core = &sas_ha->core;
  792. spin_lock_init(&core->task_queue_lock);
  793. mutex_init(&core->task_queue_flush);
  794. core->task_queue_size = 0;
  795. INIT_LIST_HEAD(&core->task_queue);
  796. core->queue_thread = kthread_run(sas_queue_thread, sas_ha,
  797. "sas_queue_%d", core->shost->host_no);
  798. if (IS_ERR(core->queue_thread))
  799. return PTR_ERR(core->queue_thread);
  800. return 0;
  801. }
  802. void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
  803. {
  804. unsigned long flags;
  805. struct scsi_core *core = &sas_ha->core;
  806. struct sas_task *task, *n;
  807. kthread_stop(core->queue_thread);
  808. if (!list_empty(&core->task_queue))
  809. SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
  810. SAS_ADDR(sas_ha->sas_addr));
  811. spin_lock_irqsave(&core->task_queue_lock, flags);
  812. list_for_each_entry_safe(task, n, &core->task_queue, list) {
  813. struct scsi_cmnd *cmd = task->uldd_task;
  814. list_del_init(&task->list);
  815. ASSIGN_SAS_TASK(cmd, NULL);
  816. sas_free_task(task);
  817. cmd->result = DID_ABORT << 16;
  818. cmd->scsi_done(cmd);
  819. }
  820. spin_unlock_irqrestore(&core->task_queue_lock, flags);
  821. }
  822. /*
  823. * Tell an upper layer that it needs to initiate an abort for a given task.
  824. * This should only ever be called by an LLDD.
  825. */
  826. void sas_task_abort(struct sas_task *task)
  827. {
  828. struct scsi_cmnd *sc = task->uldd_task;
  829. /* Escape for libsas internal commands */
  830. if (!sc) {
  831. if (!del_timer(&task->timer))
  832. return;
  833. task->timer.function(task->timer.data);
  834. return;
  835. }
  836. if (dev_is_sata(task->dev)) {
  837. sas_ata_task_abort(task);
  838. } else {
  839. struct request_queue *q = sc->device->request_queue;
  840. unsigned long flags;
  841. spin_lock_irqsave(q->queue_lock, flags);
  842. blk_abort_request(sc->request);
  843. spin_unlock_irqrestore(q->queue_lock, flags);
  844. scsi_schedule_eh(sc->device->host);
  845. }
  846. }
  847. void sas_target_destroy(struct scsi_target *starget)
  848. {
  849. struct domain_device *found_dev = starget->hostdata;
  850. if (!found_dev)
  851. return;
  852. starget->hostdata = NULL;
  853. sas_put_device(found_dev);
  854. }
  855. static void sas_parse_addr(u8 *sas_addr, const char *p)
  856. {
  857. int i;
  858. for (i = 0; i < SAS_ADDR_SIZE; i++) {
  859. u8 h, l;
  860. if (!*p)
  861. break;
  862. h = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
  863. p++;
  864. l = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
  865. p++;
  866. sas_addr[i] = (h<<4) | l;
  867. }
  868. }
  869. #define SAS_STRING_ADDR_SIZE 16
  870. int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
  871. {
  872. int res;
  873. const struct firmware *fw;
  874. res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
  875. if (res)
  876. return res;
  877. if (fw->size < SAS_STRING_ADDR_SIZE) {
  878. res = -ENODEV;
  879. goto out;
  880. }
  881. sas_parse_addr(addr, fw->data);
  882. out:
  883. release_firmware(fw);
  884. return res;
  885. }
  886. EXPORT_SYMBOL_GPL(sas_request_addr);
  887. EXPORT_SYMBOL_GPL(sas_queuecommand);
  888. EXPORT_SYMBOL_GPL(sas_target_alloc);
  889. EXPORT_SYMBOL_GPL(sas_slave_configure);
  890. EXPORT_SYMBOL_GPL(sas_change_queue_depth);
  891. EXPORT_SYMBOL_GPL(sas_change_queue_type);
  892. EXPORT_SYMBOL_GPL(sas_bios_param);
  893. EXPORT_SYMBOL_GPL(sas_task_abort);
  894. EXPORT_SYMBOL_GPL(sas_phy_reset);
  895. EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
  896. EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
  897. EXPORT_SYMBOL_GPL(sas_target_destroy);
  898. EXPORT_SYMBOL_GPL(sas_ioctl);