sas_ata.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. /*
  2. * Support for SATA devices on Serial Attached SCSI (SAS) controllers
  3. *
  4. * Copyright (C) 2006 IBM Corporation
  5. *
  6. * Written by: Darrick J. Wong <djwong@us.ibm.com>, IBM Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. * USA
  22. */
  23. #include <linux/scatterlist.h>
  24. #include <linux/slab.h>
  25. #include <scsi/sas_ata.h>
  26. #include "sas_internal.h"
  27. #include <scsi/scsi_host.h>
  28. #include <scsi/scsi_device.h>
  29. #include <scsi/scsi_tcq.h>
  30. #include <scsi/scsi.h>
  31. #include <scsi/scsi_transport.h>
  32. #include <scsi/scsi_transport_sas.h>
  33. #include "../scsi_sas_internal.h"
  34. #include "../scsi_transport_api.h"
  35. #include <scsi/scsi_eh.h>
  36. static enum ata_completion_errors sas_to_ata_err(struct task_status_struct *ts)
  37. {
  38. /* Cheesy attempt to translate SAS errors into ATA. Hah! */
  39. /* transport error */
  40. if (ts->resp == SAS_TASK_UNDELIVERED)
  41. return AC_ERR_ATA_BUS;
  42. /* ts->resp == SAS_TASK_COMPLETE */
  43. /* task delivered, what happened afterwards? */
  44. switch (ts->stat) {
  45. case SAS_DEV_NO_RESPONSE:
  46. return AC_ERR_TIMEOUT;
  47. case SAS_INTERRUPTED:
  48. case SAS_PHY_DOWN:
  49. case SAS_NAK_R_ERR:
  50. return AC_ERR_ATA_BUS;
  51. case SAS_DATA_UNDERRUN:
  52. /*
  53. * Some programs that use the taskfile interface
  54. * (smartctl in particular) can cause underrun
  55. * problems. Ignore these errors, perhaps at our
  56. * peril.
  57. */
  58. return 0;
  59. case SAS_DATA_OVERRUN:
  60. case SAS_QUEUE_FULL:
  61. case SAS_DEVICE_UNKNOWN:
  62. case SAS_SG_ERR:
  63. return AC_ERR_INVALID;
  64. case SAS_OPEN_TO:
  65. case SAS_OPEN_REJECT:
  66. SAS_DPRINTK("%s: Saw error %d. What to do?\n",
  67. __func__, ts->stat);
  68. return AC_ERR_OTHER;
  69. case SAM_STAT_CHECK_CONDITION:
  70. case SAS_ABORTED_TASK:
  71. return AC_ERR_DEV;
  72. case SAS_PROTO_RESPONSE:
  73. /* This means the ending_fis has the error
  74. * value; return 0 here to collect it */
  75. return 0;
  76. default:
  77. return 0;
  78. }
  79. }
  80. static void sas_ata_task_done(struct sas_task *task)
  81. {
  82. struct ata_queued_cmd *qc = task->uldd_task;
  83. struct domain_device *dev;
  84. struct task_status_struct *stat = &task->task_status;
  85. struct ata_task_resp *resp = (struct ata_task_resp *)stat->buf;
  86. struct sas_ha_struct *sas_ha;
  87. enum ata_completion_errors ac;
  88. unsigned long flags;
  89. struct ata_link *link;
  90. if (!qc)
  91. goto qc_already_gone;
  92. dev = qc->ap->private_data;
  93. sas_ha = dev->port->ha;
  94. link = &dev->sata_dev.ap->link;
  95. spin_lock_irqsave(dev->sata_dev.ap->lock, flags);
  96. if (stat->stat == SAS_PROTO_RESPONSE || stat->stat == SAM_STAT_GOOD ||
  97. ((stat->stat == SAM_STAT_CHECK_CONDITION &&
  98. dev->sata_dev.command_set == ATAPI_COMMAND_SET))) {
  99. ata_tf_from_fis(resp->ending_fis, &dev->sata_dev.tf);
  100. if (!link->sactive) {
  101. qc->err_mask |= ac_err_mask(dev->sata_dev.tf.command);
  102. } else {
  103. link->eh_info.err_mask |= ac_err_mask(dev->sata_dev.tf.command);
  104. if (unlikely(link->eh_info.err_mask))
  105. qc->flags |= ATA_QCFLAG_FAILED;
  106. }
  107. } else {
  108. ac = sas_to_ata_err(stat);
  109. if (ac) {
  110. SAS_DPRINTK("%s: SAS error %x\n", __func__,
  111. stat->stat);
  112. /* We saw a SAS error. Send a vague error. */
  113. if (!link->sactive) {
  114. qc->err_mask = ac;
  115. } else {
  116. link->eh_info.err_mask |= AC_ERR_DEV;
  117. qc->flags |= ATA_QCFLAG_FAILED;
  118. }
  119. dev->sata_dev.tf.feature = 0x04; /* status err */
  120. dev->sata_dev.tf.command = ATA_ERR;
  121. }
  122. }
  123. qc->lldd_task = NULL;
  124. if (qc->scsicmd)
  125. ASSIGN_SAS_TASK(qc->scsicmd, NULL);
  126. ata_qc_complete(qc);
  127. spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
  128. /*
  129. * If the sas_task has an ata qc, a scsi_cmnd and the aborted
  130. * flag is set, then we must have come in via the libsas EH
  131. * functions. When we exit this function, we need to put the
  132. * scsi_cmnd on the list of finished errors. The ata_qc_complete
  133. * call cleans up the libata side of things but we're protected
  134. * from the scsi_cmnd going away because the scsi_cmnd is owned
  135. * by the EH, making libata's call to scsi_done a NOP.
  136. */
  137. spin_lock_irqsave(&task->task_state_lock, flags);
  138. if (qc->scsicmd && task->task_state_flags & SAS_TASK_STATE_ABORTED)
  139. scsi_eh_finish_cmd(qc->scsicmd, &sas_ha->eh_done_q);
  140. spin_unlock_irqrestore(&task->task_state_lock, flags);
  141. qc_already_gone:
  142. list_del_init(&task->list);
  143. sas_free_task(task);
  144. }
  145. static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
  146. {
  147. unsigned long flags;
  148. struct sas_task *task;
  149. struct scatterlist *sg;
  150. int ret = AC_ERR_SYSTEM;
  151. unsigned int si, xfer = 0;
  152. struct ata_port *ap = qc->ap;
  153. struct domain_device *dev = ap->private_data;
  154. struct sas_ha_struct *sas_ha = dev->port->ha;
  155. struct Scsi_Host *host = sas_ha->core.shost;
  156. struct sas_internal *i = to_sas_internal(host->transportt);
  157. /* TODO: audit callers to ensure they are ready for qc_issue to
  158. * unconditionally re-enable interrupts
  159. */
  160. local_irq_save(flags);
  161. spin_unlock(ap->lock);
  162. /* If the device fell off, no sense in issuing commands */
  163. if (dev->gone)
  164. goto out;
  165. task = sas_alloc_task(GFP_ATOMIC);
  166. if (!task)
  167. goto out;
  168. task->dev = dev;
  169. task->task_proto = SAS_PROTOCOL_STP;
  170. task->task_done = sas_ata_task_done;
  171. if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
  172. qc->tf.command == ATA_CMD_FPDMA_READ) {
  173. /* Need to zero out the tag libata assigned us */
  174. qc->tf.nsect = 0;
  175. }
  176. ata_tf_to_fis(&qc->tf, 1, 0, (u8*)&task->ata_task.fis);
  177. task->uldd_task = qc;
  178. if (ata_is_atapi(qc->tf.protocol)) {
  179. memcpy(task->ata_task.atapi_packet, qc->cdb, qc->dev->cdb_len);
  180. task->total_xfer_len = qc->nbytes;
  181. task->num_scatter = qc->n_elem;
  182. } else {
  183. for_each_sg(qc->sg, sg, qc->n_elem, si)
  184. xfer += sg->length;
  185. task->total_xfer_len = xfer;
  186. task->num_scatter = si;
  187. }
  188. task->data_dir = qc->dma_dir;
  189. task->scatter = qc->sg;
  190. task->ata_task.retry_count = 1;
  191. task->task_state_flags = SAS_TASK_STATE_PENDING;
  192. qc->lldd_task = task;
  193. switch (qc->tf.protocol) {
  194. case ATA_PROT_NCQ:
  195. task->ata_task.use_ncq = 1;
  196. /* fall through */
  197. case ATAPI_PROT_DMA:
  198. case ATA_PROT_DMA:
  199. task->ata_task.dma_xfer = 1;
  200. break;
  201. }
  202. if (qc->scsicmd)
  203. ASSIGN_SAS_TASK(qc->scsicmd, task);
  204. if (sas_ha->lldd_max_execute_num < 2)
  205. ret = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
  206. else
  207. ret = sas_queue_up(task);
  208. /* Examine */
  209. if (ret) {
  210. SAS_DPRINTK("lldd_execute_task returned: %d\n", ret);
  211. if (qc->scsicmd)
  212. ASSIGN_SAS_TASK(qc->scsicmd, NULL);
  213. sas_free_task(task);
  214. ret = AC_ERR_SYSTEM;
  215. }
  216. out:
  217. spin_lock(ap->lock);
  218. local_irq_restore(flags);
  219. return ret;
  220. }
  221. static bool sas_ata_qc_fill_rtf(struct ata_queued_cmd *qc)
  222. {
  223. struct domain_device *dev = qc->ap->private_data;
  224. memcpy(&qc->result_tf, &dev->sata_dev.tf, sizeof(qc->result_tf));
  225. return true;
  226. }
  227. static int sas_ata_hard_reset(struct ata_link *link, unsigned int *class,
  228. unsigned long deadline)
  229. {
  230. struct ata_port *ap = link->ap;
  231. struct domain_device *dev = ap->private_data;
  232. struct sas_internal *i =
  233. to_sas_internal(dev->port->ha->core.shost->transportt);
  234. int res = TMF_RESP_FUNC_FAILED;
  235. int ret = 0;
  236. if (i->dft->lldd_I_T_nexus_reset)
  237. res = i->dft->lldd_I_T_nexus_reset(dev);
  238. if (res != TMF_RESP_FUNC_COMPLETE) {
  239. SAS_DPRINTK("%s: Unable to reset I T nexus?\n", __func__);
  240. ret = -EAGAIN;
  241. }
  242. switch (dev->sata_dev.command_set) {
  243. case ATA_COMMAND_SET:
  244. SAS_DPRINTK("%s: Found ATA device.\n", __func__);
  245. *class = ATA_DEV_ATA;
  246. break;
  247. case ATAPI_COMMAND_SET:
  248. SAS_DPRINTK("%s: Found ATAPI device.\n", __func__);
  249. *class = ATA_DEV_ATAPI;
  250. break;
  251. default:
  252. SAS_DPRINTK("%s: Unknown SATA command set: %d.\n",
  253. __func__,
  254. dev->sata_dev.command_set);
  255. *class = ATA_DEV_UNKNOWN;
  256. break;
  257. }
  258. ap->cbl = ATA_CBL_SATA;
  259. return ret;
  260. }
  261. static int sas_ata_soft_reset(struct ata_link *link, unsigned int *class,
  262. unsigned long deadline)
  263. {
  264. struct ata_port *ap = link->ap;
  265. struct domain_device *dev = ap->private_data;
  266. struct sas_internal *i =
  267. to_sas_internal(dev->port->ha->core.shost->transportt);
  268. int res = TMF_RESP_FUNC_FAILED;
  269. int ret = 0;
  270. if (i->dft->lldd_ata_soft_reset)
  271. res = i->dft->lldd_ata_soft_reset(dev);
  272. if (res != TMF_RESP_FUNC_COMPLETE) {
  273. SAS_DPRINTK("%s: Unable to soft reset\n", __func__);
  274. ret = -EAGAIN;
  275. }
  276. switch (dev->sata_dev.command_set) {
  277. case ATA_COMMAND_SET:
  278. SAS_DPRINTK("%s: Found ATA device.\n", __func__);
  279. *class = ATA_DEV_ATA;
  280. break;
  281. case ATAPI_COMMAND_SET:
  282. SAS_DPRINTK("%s: Found ATAPI device.\n", __func__);
  283. *class = ATA_DEV_ATAPI;
  284. break;
  285. default:
  286. SAS_DPRINTK("%s: Unknown SATA command set: %d.\n",
  287. __func__, dev->sata_dev.command_set);
  288. *class = ATA_DEV_UNKNOWN;
  289. break;
  290. }
  291. ap->cbl = ATA_CBL_SATA;
  292. return ret;
  293. }
  294. static void sas_ata_post_internal(struct ata_queued_cmd *qc)
  295. {
  296. if (qc->flags & ATA_QCFLAG_FAILED)
  297. qc->err_mask |= AC_ERR_OTHER;
  298. if (qc->err_mask) {
  299. /*
  300. * Find the sas_task and kill it. By this point,
  301. * libata has decided to kill the qc, so we needn't
  302. * bother with sas_ata_task_done. But we still
  303. * ought to abort the task.
  304. */
  305. struct sas_task *task = qc->lldd_task;
  306. unsigned long flags;
  307. qc->lldd_task = NULL;
  308. if (task) {
  309. /* Should this be a AT(API) device reset? */
  310. spin_lock_irqsave(&task->task_state_lock, flags);
  311. task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
  312. spin_unlock_irqrestore(&task->task_state_lock, flags);
  313. task->uldd_task = NULL;
  314. __sas_task_abort(task);
  315. }
  316. }
  317. }
  318. static struct ata_port_operations sas_sata_ops = {
  319. .prereset = ata_std_prereset,
  320. .softreset = sas_ata_soft_reset,
  321. .hardreset = sas_ata_hard_reset,
  322. .postreset = ata_std_postreset,
  323. .error_handler = ata_std_error_handler,
  324. .post_internal_cmd = sas_ata_post_internal,
  325. .qc_defer = ata_std_qc_defer,
  326. .qc_prep = ata_noop_qc_prep,
  327. .qc_issue = sas_ata_qc_issue,
  328. .qc_fill_rtf = sas_ata_qc_fill_rtf,
  329. .port_start = ata_sas_port_start,
  330. .port_stop = ata_sas_port_stop,
  331. };
  332. static struct ata_port_info sata_port_info = {
  333. .flags = ATA_FLAG_SATA | ATA_FLAG_PIO_DMA | ATA_FLAG_NCQ,
  334. .pio_mask = ATA_PIO4,
  335. .mwdma_mask = ATA_MWDMA2,
  336. .udma_mask = ATA_UDMA6,
  337. .port_ops = &sas_sata_ops
  338. };
  339. int sas_ata_init_host_and_port(struct domain_device *found_dev,
  340. struct scsi_target *starget)
  341. {
  342. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  343. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  344. struct ata_port *ap;
  345. ata_host_init(&found_dev->sata_dev.ata_host,
  346. ha->dev,
  347. sata_port_info.flags,
  348. &sas_sata_ops);
  349. ap = ata_sas_port_alloc(&found_dev->sata_dev.ata_host,
  350. &sata_port_info,
  351. shost);
  352. if (!ap) {
  353. SAS_DPRINTK("ata_sas_port_alloc failed.\n");
  354. return -ENODEV;
  355. }
  356. ap->private_data = found_dev;
  357. ap->cbl = ATA_CBL_SATA;
  358. ap->scsi_host = shost;
  359. found_dev->sata_dev.ap = ap;
  360. return 0;
  361. }
  362. void sas_ata_task_abort(struct sas_task *task)
  363. {
  364. struct ata_queued_cmd *qc = task->uldd_task;
  365. struct completion *waiting;
  366. /* Bounce SCSI-initiated commands to the SCSI EH */
  367. if (qc->scsicmd) {
  368. struct request_queue *q = qc->scsicmd->device->request_queue;
  369. unsigned long flags;
  370. spin_lock_irqsave(q->queue_lock, flags);
  371. blk_abort_request(qc->scsicmd->request);
  372. spin_unlock_irqrestore(q->queue_lock, flags);
  373. scsi_schedule_eh(qc->scsicmd->device->host);
  374. return;
  375. }
  376. /* Internal command, fake a timeout and complete. */
  377. qc->flags &= ~ATA_QCFLAG_ACTIVE;
  378. qc->flags |= ATA_QCFLAG_FAILED;
  379. qc->err_mask |= AC_ERR_TIMEOUT;
  380. waiting = qc->private_data;
  381. complete(waiting);
  382. }
  383. static void sas_task_timedout(unsigned long _task)
  384. {
  385. struct sas_task *task = (void *) _task;
  386. unsigned long flags;
  387. spin_lock_irqsave(&task->task_state_lock, flags);
  388. if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
  389. task->task_state_flags |= SAS_TASK_STATE_ABORTED;
  390. spin_unlock_irqrestore(&task->task_state_lock, flags);
  391. complete(&task->completion);
  392. }
  393. static void sas_disc_task_done(struct sas_task *task)
  394. {
  395. if (!del_timer(&task->timer))
  396. return;
  397. complete(&task->completion);
  398. }
  399. #define SAS_DEV_TIMEOUT 10
  400. /**
  401. * sas_execute_task -- Basic task processing for discovery
  402. * @task: the task to be executed
  403. * @buffer: pointer to buffer to do I/O
  404. * @size: size of @buffer
  405. * @dma_dir: DMA direction. DMA_xxx
  406. */
  407. static int sas_execute_task(struct sas_task *task, void *buffer, int size,
  408. enum dma_data_direction dma_dir)
  409. {
  410. int res = 0;
  411. struct scatterlist *scatter = NULL;
  412. struct task_status_struct *ts = &task->task_status;
  413. int num_scatter = 0;
  414. int retries = 0;
  415. struct sas_internal *i =
  416. to_sas_internal(task->dev->port->ha->core.shost->transportt);
  417. if (dma_dir != DMA_NONE) {
  418. scatter = kzalloc(sizeof(*scatter), GFP_KERNEL);
  419. if (!scatter)
  420. goto out;
  421. sg_init_one(scatter, buffer, size);
  422. num_scatter = 1;
  423. }
  424. task->task_proto = task->dev->tproto;
  425. task->scatter = scatter;
  426. task->num_scatter = num_scatter;
  427. task->total_xfer_len = size;
  428. task->data_dir = dma_dir;
  429. task->task_done = sas_disc_task_done;
  430. if (dma_dir != DMA_NONE &&
  431. sas_protocol_ata(task->task_proto)) {
  432. task->num_scatter = dma_map_sg(task->dev->port->ha->dev,
  433. task->scatter,
  434. task->num_scatter,
  435. task->data_dir);
  436. }
  437. for (retries = 0; retries < 5; retries++) {
  438. task->task_state_flags = SAS_TASK_STATE_PENDING;
  439. init_completion(&task->completion);
  440. task->timer.data = (unsigned long) task;
  441. task->timer.function = sas_task_timedout;
  442. task->timer.expires = jiffies + SAS_DEV_TIMEOUT*HZ;
  443. add_timer(&task->timer);
  444. res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
  445. if (res) {
  446. del_timer(&task->timer);
  447. SAS_DPRINTK("executing SAS discovery task failed:%d\n",
  448. res);
  449. goto ex_err;
  450. }
  451. wait_for_completion(&task->completion);
  452. res = -ECOMM;
  453. if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
  454. int res2;
  455. SAS_DPRINTK("task aborted, flags:0x%x\n",
  456. task->task_state_flags);
  457. res2 = i->dft->lldd_abort_task(task);
  458. SAS_DPRINTK("came back from abort task\n");
  459. if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
  460. if (res2 == TMF_RESP_FUNC_COMPLETE)
  461. continue; /* Retry the task */
  462. else
  463. goto ex_err;
  464. }
  465. }
  466. if (task->task_status.stat == SAM_STAT_BUSY ||
  467. task->task_status.stat == SAM_STAT_TASK_SET_FULL ||
  468. task->task_status.stat == SAS_QUEUE_FULL) {
  469. SAS_DPRINTK("task: q busy, sleeping...\n");
  470. schedule_timeout_interruptible(HZ);
  471. } else if (task->task_status.stat == SAM_STAT_CHECK_CONDITION) {
  472. struct scsi_sense_hdr shdr;
  473. if (!scsi_normalize_sense(ts->buf, ts->buf_valid_size,
  474. &shdr)) {
  475. SAS_DPRINTK("couldn't normalize sense\n");
  476. continue;
  477. }
  478. if ((shdr.sense_key == 6 && shdr.asc == 0x29) ||
  479. (shdr.sense_key == 2 && shdr.asc == 4 &&
  480. shdr.ascq == 1)) {
  481. SAS_DPRINTK("device %016llx LUN: %016llx "
  482. "powering up or not ready yet, "
  483. "sleeping...\n",
  484. SAS_ADDR(task->dev->sas_addr),
  485. SAS_ADDR(task->ssp_task.LUN));
  486. schedule_timeout_interruptible(5*HZ);
  487. } else if (shdr.sense_key == 1) {
  488. res = 0;
  489. break;
  490. } else if (shdr.sense_key == 5) {
  491. break;
  492. } else {
  493. SAS_DPRINTK("dev %016llx LUN: %016llx "
  494. "sense key:0x%x ASC:0x%x ASCQ:0x%x"
  495. "\n",
  496. SAS_ADDR(task->dev->sas_addr),
  497. SAS_ADDR(task->ssp_task.LUN),
  498. shdr.sense_key,
  499. shdr.asc, shdr.ascq);
  500. }
  501. } else if (task->task_status.resp != SAS_TASK_COMPLETE ||
  502. task->task_status.stat != SAM_STAT_GOOD) {
  503. SAS_DPRINTK("task finished with resp:0x%x, "
  504. "stat:0x%x\n",
  505. task->task_status.resp,
  506. task->task_status.stat);
  507. goto ex_err;
  508. } else {
  509. res = 0;
  510. break;
  511. }
  512. }
  513. ex_err:
  514. if (dma_dir != DMA_NONE) {
  515. if (sas_protocol_ata(task->task_proto))
  516. dma_unmap_sg(task->dev->port->ha->dev,
  517. task->scatter, task->num_scatter,
  518. task->data_dir);
  519. kfree(scatter);
  520. }
  521. out:
  522. return res;
  523. }
  524. /* ---------- SATA ---------- */
  525. static void sas_get_ata_command_set(struct domain_device *dev)
  526. {
  527. struct dev_to_host_fis *fis =
  528. (struct dev_to_host_fis *) dev->frame_rcvd;
  529. if ((fis->sector_count == 1 && /* ATA */
  530. fis->lbal == 1 &&
  531. fis->lbam == 0 &&
  532. fis->lbah == 0 &&
  533. fis->device == 0)
  534. ||
  535. (fis->sector_count == 0 && /* CE-ATA (mATA) */
  536. fis->lbal == 0 &&
  537. fis->lbam == 0xCE &&
  538. fis->lbah == 0xAA &&
  539. (fis->device & ~0x10) == 0))
  540. dev->sata_dev.command_set = ATA_COMMAND_SET;
  541. else if ((fis->interrupt_reason == 1 && /* ATAPI */
  542. fis->lbal == 1 &&
  543. fis->byte_count_low == 0x14 &&
  544. fis->byte_count_high == 0xEB &&
  545. (fis->device & ~0x10) == 0))
  546. dev->sata_dev.command_set = ATAPI_COMMAND_SET;
  547. else if ((fis->sector_count == 1 && /* SEMB */
  548. fis->lbal == 1 &&
  549. fis->lbam == 0x3C &&
  550. fis->lbah == 0xC3 &&
  551. fis->device == 0)
  552. ||
  553. (fis->interrupt_reason == 1 && /* SATA PM */
  554. fis->lbal == 1 &&
  555. fis->byte_count_low == 0x69 &&
  556. fis->byte_count_high == 0x96 &&
  557. (fis->device & ~0x10) == 0))
  558. /* Treat it as a superset? */
  559. dev->sata_dev.command_set = ATAPI_COMMAND_SET;
  560. }
  561. /**
  562. * sas_issue_ata_cmd -- Basic SATA command processing for discovery
  563. * @dev: the device to send the command to
  564. * @command: the command register
  565. * @features: the features register
  566. * @buffer: pointer to buffer to do I/O
  567. * @size: size of @buffer
  568. * @dma_dir: DMA direction. DMA_xxx
  569. */
  570. static int sas_issue_ata_cmd(struct domain_device *dev, u8 command,
  571. u8 features, void *buffer, int size,
  572. enum dma_data_direction dma_dir)
  573. {
  574. int res = 0;
  575. struct sas_task *task;
  576. struct dev_to_host_fis *d2h_fis = (struct dev_to_host_fis *)
  577. &dev->frame_rcvd[0];
  578. res = -ENOMEM;
  579. task = sas_alloc_task(GFP_KERNEL);
  580. if (!task)
  581. goto out;
  582. task->dev = dev;
  583. task->ata_task.fis.fis_type = 0x27;
  584. task->ata_task.fis.command = command;
  585. task->ata_task.fis.features = features;
  586. task->ata_task.fis.device = d2h_fis->device;
  587. task->ata_task.retry_count = 1;
  588. res = sas_execute_task(task, buffer, size, dma_dir);
  589. sas_free_task(task);
  590. out:
  591. return res;
  592. }
  593. #define ATA_IDENTIFY_DEV 0xEC
  594. #define ATA_IDENTIFY_PACKET_DEV 0xA1
  595. #define ATA_SET_FEATURES 0xEF
  596. #define ATA_FEATURE_PUP_STBY_SPIN_UP 0x07
  597. /**
  598. * sas_discover_sata_dev -- discover a STP/SATA device (SATA_DEV)
  599. * @dev: STP/SATA device of interest (ATA/ATAPI)
  600. *
  601. * The LLDD has already been notified of this device, so that we can
  602. * send FISes to it. Here we try to get IDENTIFY DEVICE or IDENTIFY
  603. * PACKET DEVICE, if ATAPI device, so that the LLDD can fine-tune its
  604. * performance for this device.
  605. */
  606. static int sas_discover_sata_dev(struct domain_device *dev)
  607. {
  608. int res;
  609. __le16 *identify_x;
  610. u8 command;
  611. identify_x = kzalloc(512, GFP_KERNEL);
  612. if (!identify_x)
  613. return -ENOMEM;
  614. if (dev->sata_dev.command_set == ATA_COMMAND_SET) {
  615. dev->sata_dev.identify_device = identify_x;
  616. command = ATA_IDENTIFY_DEV;
  617. } else {
  618. dev->sata_dev.identify_packet_device = identify_x;
  619. command = ATA_IDENTIFY_PACKET_DEV;
  620. }
  621. res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
  622. DMA_FROM_DEVICE);
  623. if (res)
  624. goto out_err;
  625. /* lives on the media? */
  626. if (le16_to_cpu(identify_x[0]) & 4) {
  627. /* incomplete response */
  628. SAS_DPRINTK("sending SET FEATURE/PUP_STBY_SPIN_UP to "
  629. "dev %llx\n", SAS_ADDR(dev->sas_addr));
  630. if (!(identify_x[83] & cpu_to_le16(1<<6)))
  631. goto cont1;
  632. res = sas_issue_ata_cmd(dev, ATA_SET_FEATURES,
  633. ATA_FEATURE_PUP_STBY_SPIN_UP,
  634. NULL, 0, DMA_NONE);
  635. if (res)
  636. goto cont1;
  637. schedule_timeout_interruptible(5*HZ); /* More time? */
  638. res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
  639. DMA_FROM_DEVICE);
  640. if (res)
  641. goto out_err;
  642. }
  643. cont1:
  644. /* XXX Hint: register this SATA device with SATL.
  645. When this returns, dev->sata_dev->lu is alive and
  646. present.
  647. sas_satl_register_dev(dev);
  648. */
  649. sas_fill_in_rphy(dev, dev->rphy);
  650. return 0;
  651. out_err:
  652. dev->sata_dev.identify_packet_device = NULL;
  653. dev->sata_dev.identify_device = NULL;
  654. kfree(identify_x);
  655. return res;
  656. }
  657. static int sas_discover_sata_pm(struct domain_device *dev)
  658. {
  659. return -ENODEV;
  660. }
  661. /**
  662. * sas_discover_sata -- discover an STP/SATA domain device
  663. * @dev: pointer to struct domain_device of interest
  664. *
  665. * First we notify the LLDD of this device, so we can send frames to
  666. * it. Then depending on the type of device we call the appropriate
  667. * discover functions. Once device discover is done, we notify the
  668. * LLDD so that it can fine-tune its parameters for the device, by
  669. * removing it and then adding it. That is, the second time around,
  670. * the driver would have certain fields, that it is looking at, set.
  671. * Finally we initialize the kobj so that the device can be added to
  672. * the system at registration time. Devices directly attached to a HA
  673. * port, have no parents. All other devices do, and should have their
  674. * "parent" pointer set appropriately before calling this function.
  675. */
  676. int sas_discover_sata(struct domain_device *dev)
  677. {
  678. int res;
  679. sas_get_ata_command_set(dev);
  680. res = sas_notify_lldd_dev_found(dev);
  681. if (res)
  682. return res;
  683. switch (dev->dev_type) {
  684. case SATA_DEV:
  685. res = sas_discover_sata_dev(dev);
  686. break;
  687. case SATA_PM:
  688. res = sas_discover_sata_pm(dev);
  689. break;
  690. default:
  691. break;
  692. }
  693. sas_notify_lldd_dev_gone(dev);
  694. if (!res) {
  695. sas_notify_lldd_dev_found(dev);
  696. res = sas_rphy_add(dev->rphy);
  697. }
  698. return res;
  699. }
  700. void sas_ata_strategy_handler(struct Scsi_Host *shost)
  701. {
  702. struct scsi_device *sdev;
  703. shost_for_each_device(sdev, shost) {
  704. struct domain_device *ddev = sdev_to_domain_dev(sdev);
  705. struct ata_port *ap = ddev->sata_dev.ap;
  706. if (!dev_is_sata(ddev))
  707. continue;
  708. ata_port_printk(ap, KERN_DEBUG, "sas eh calling libata port error handler");
  709. ata_scsi_port_error_handler(shost, ap);
  710. }
  711. }
  712. int sas_ata_timed_out(struct scsi_cmnd *cmd, struct sas_task *task,
  713. enum blk_eh_timer_return *rtn)
  714. {
  715. struct domain_device *ddev = cmd_to_domain_dev(cmd);
  716. if (!dev_is_sata(ddev) || task)
  717. return 0;
  718. /* we're a sata device with no task, so this must be a libata
  719. * eh timeout. Ideally should hook into libata timeout
  720. * handling, but there's no point, it just wants to activate
  721. * the eh thread */
  722. *rtn = BLK_EH_NOT_HANDLED;
  723. return 1;
  724. }
  725. int sas_ata_eh(struct Scsi_Host *shost, struct list_head *work_q,
  726. struct list_head *done_q)
  727. {
  728. int rtn = 0;
  729. struct scsi_cmnd *cmd, *n;
  730. struct ata_port *ap;
  731. do {
  732. LIST_HEAD(sata_q);
  733. ap = NULL;
  734. list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
  735. struct domain_device *ddev = cmd_to_domain_dev(cmd);
  736. if (!dev_is_sata(ddev) || TO_SAS_TASK(cmd))
  737. continue;
  738. if (ap && ap != ddev->sata_dev.ap)
  739. continue;
  740. ap = ddev->sata_dev.ap;
  741. rtn = 1;
  742. list_move(&cmd->eh_entry, &sata_q);
  743. }
  744. if (!list_empty(&sata_q)) {
  745. ata_port_printk(ap, KERN_DEBUG, "sas eh calling libata cmd error handler\n");
  746. ata_scsi_cmd_error_handler(shost, ap, &sata_q);
  747. /*
  748. * ata's error handler may leave the cmd on the list
  749. * so make sure they don't remain on a stack list
  750. * about to go out of scope.
  751. *
  752. * This looks strange, since the commands are
  753. * now part of no list, but the next error
  754. * action will be ata_port_error_handler()
  755. * which takes no list and sweeps them up
  756. * anyway from the ata tag array.
  757. */
  758. while (!list_empty(&sata_q))
  759. list_del_init(sata_q.next);
  760. }
  761. } while (ap);
  762. return rtn;
  763. }