sas_ata.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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 <linux/async.h>
  26. #include <linux/export.h>
  27. #include <scsi/sas_ata.h>
  28. #include "sas_internal.h"
  29. #include <scsi/scsi_host.h>
  30. #include <scsi/scsi_device.h>
  31. #include <scsi/scsi_tcq.h>
  32. #include <scsi/scsi.h>
  33. #include <scsi/scsi_transport.h>
  34. #include <scsi/scsi_transport_sas.h>
  35. #include "../scsi_sas_internal.h"
  36. #include "../scsi_transport_api.h"
  37. #include <scsi/scsi_eh.h>
  38. static enum ata_completion_errors sas_to_ata_err(struct task_status_struct *ts)
  39. {
  40. /* Cheesy attempt to translate SAS errors into ATA. Hah! */
  41. /* transport error */
  42. if (ts->resp == SAS_TASK_UNDELIVERED)
  43. return AC_ERR_ATA_BUS;
  44. /* ts->resp == SAS_TASK_COMPLETE */
  45. /* task delivered, what happened afterwards? */
  46. switch (ts->stat) {
  47. case SAS_DEV_NO_RESPONSE:
  48. return AC_ERR_TIMEOUT;
  49. case SAS_INTERRUPTED:
  50. case SAS_PHY_DOWN:
  51. case SAS_NAK_R_ERR:
  52. return AC_ERR_ATA_BUS;
  53. case SAS_DATA_UNDERRUN:
  54. /*
  55. * Some programs that use the taskfile interface
  56. * (smartctl in particular) can cause underrun
  57. * problems. Ignore these errors, perhaps at our
  58. * peril.
  59. */
  60. return 0;
  61. case SAS_DATA_OVERRUN:
  62. case SAS_QUEUE_FULL:
  63. case SAS_DEVICE_UNKNOWN:
  64. case SAS_SG_ERR:
  65. return AC_ERR_INVALID;
  66. case SAS_OPEN_TO:
  67. case SAS_OPEN_REJECT:
  68. SAS_DPRINTK("%s: Saw error %d. What to do?\n",
  69. __func__, ts->stat);
  70. return AC_ERR_OTHER;
  71. case SAM_STAT_CHECK_CONDITION:
  72. case SAS_ABORTED_TASK:
  73. return AC_ERR_DEV;
  74. case SAS_PROTO_RESPONSE:
  75. /* This means the ending_fis has the error
  76. * value; return 0 here to collect it */
  77. return 0;
  78. default:
  79. return 0;
  80. }
  81. }
  82. static void sas_ata_task_done(struct sas_task *task)
  83. {
  84. struct ata_queued_cmd *qc = task->uldd_task;
  85. struct domain_device *dev = task->dev;
  86. struct task_status_struct *stat = &task->task_status;
  87. struct ata_task_resp *resp = (struct ata_task_resp *)stat->buf;
  88. struct sas_ha_struct *sas_ha = dev->port->ha;
  89. enum ata_completion_errors ac;
  90. unsigned long flags;
  91. struct ata_link *link;
  92. struct ata_port *ap;
  93. spin_lock_irqsave(&dev->done_lock, flags);
  94. if (test_bit(SAS_HA_FROZEN, &sas_ha->state))
  95. task = NULL;
  96. else if (qc && qc->scsicmd)
  97. ASSIGN_SAS_TASK(qc->scsicmd, NULL);
  98. spin_unlock_irqrestore(&dev->done_lock, flags);
  99. /* check if libsas-eh got to the task before us */
  100. if (unlikely(!task))
  101. return;
  102. if (!qc)
  103. goto qc_already_gone;
  104. ap = qc->ap;
  105. link = &ap->link;
  106. spin_lock_irqsave(ap->lock, flags);
  107. /* check if we lost the race with libata/sas_ata_post_internal() */
  108. if (unlikely(ap->pflags & ATA_PFLAG_FROZEN)) {
  109. spin_unlock_irqrestore(ap->lock, flags);
  110. if (qc->scsicmd)
  111. goto qc_already_gone;
  112. else {
  113. /* if eh is not involved and the port is frozen then the
  114. * ata internal abort process has taken responsibility
  115. * for this sas_task
  116. */
  117. return;
  118. }
  119. }
  120. if (stat->stat == SAS_PROTO_RESPONSE || stat->stat == SAM_STAT_GOOD ||
  121. ((stat->stat == SAM_STAT_CHECK_CONDITION &&
  122. dev->sata_dev.command_set == ATAPI_COMMAND_SET))) {
  123. ata_tf_from_fis(resp->ending_fis, &dev->sata_dev.tf);
  124. if (!link->sactive) {
  125. qc->err_mask |= ac_err_mask(dev->sata_dev.tf.command);
  126. } else {
  127. link->eh_info.err_mask |= ac_err_mask(dev->sata_dev.tf.command);
  128. if (unlikely(link->eh_info.err_mask))
  129. qc->flags |= ATA_QCFLAG_FAILED;
  130. }
  131. } else {
  132. ac = sas_to_ata_err(stat);
  133. if (ac) {
  134. SAS_DPRINTK("%s: SAS error %x\n", __func__,
  135. stat->stat);
  136. /* We saw a SAS error. Send a vague error. */
  137. if (!link->sactive) {
  138. qc->err_mask = ac;
  139. } else {
  140. link->eh_info.err_mask |= AC_ERR_DEV;
  141. qc->flags |= ATA_QCFLAG_FAILED;
  142. }
  143. dev->sata_dev.tf.feature = 0x04; /* status err */
  144. dev->sata_dev.tf.command = ATA_ERR;
  145. }
  146. }
  147. qc->lldd_task = NULL;
  148. ata_qc_complete(qc);
  149. spin_unlock_irqrestore(ap->lock, flags);
  150. qc_already_gone:
  151. list_del_init(&task->list);
  152. sas_free_task(task);
  153. }
  154. static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
  155. {
  156. unsigned long flags;
  157. struct sas_task *task;
  158. struct scatterlist *sg;
  159. int ret = AC_ERR_SYSTEM;
  160. unsigned int si, xfer = 0;
  161. struct ata_port *ap = qc->ap;
  162. struct domain_device *dev = ap->private_data;
  163. struct sas_ha_struct *sas_ha = dev->port->ha;
  164. struct Scsi_Host *host = sas_ha->core.shost;
  165. struct sas_internal *i = to_sas_internal(host->transportt);
  166. /* TODO: audit callers to ensure they are ready for qc_issue to
  167. * unconditionally re-enable interrupts
  168. */
  169. local_irq_save(flags);
  170. spin_unlock(ap->lock);
  171. /* If the device fell off, no sense in issuing commands */
  172. if (test_bit(SAS_DEV_GONE, &dev->state))
  173. goto out;
  174. task = sas_alloc_task(GFP_ATOMIC);
  175. if (!task)
  176. goto out;
  177. task->dev = dev;
  178. task->task_proto = SAS_PROTOCOL_STP;
  179. task->task_done = sas_ata_task_done;
  180. if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
  181. qc->tf.command == ATA_CMD_FPDMA_READ) {
  182. /* Need to zero out the tag libata assigned us */
  183. qc->tf.nsect = 0;
  184. }
  185. ata_tf_to_fis(&qc->tf, 1, 0, (u8*)&task->ata_task.fis);
  186. task->uldd_task = qc;
  187. if (ata_is_atapi(qc->tf.protocol)) {
  188. memcpy(task->ata_task.atapi_packet, qc->cdb, qc->dev->cdb_len);
  189. task->total_xfer_len = qc->nbytes;
  190. task->num_scatter = qc->n_elem;
  191. } else {
  192. for_each_sg(qc->sg, sg, qc->n_elem, si)
  193. xfer += sg->length;
  194. task->total_xfer_len = xfer;
  195. task->num_scatter = si;
  196. }
  197. task->data_dir = qc->dma_dir;
  198. task->scatter = qc->sg;
  199. task->ata_task.retry_count = 1;
  200. task->task_state_flags = SAS_TASK_STATE_PENDING;
  201. qc->lldd_task = task;
  202. switch (qc->tf.protocol) {
  203. case ATA_PROT_NCQ:
  204. task->ata_task.use_ncq = 1;
  205. /* fall through */
  206. case ATAPI_PROT_DMA:
  207. case ATA_PROT_DMA:
  208. task->ata_task.dma_xfer = 1;
  209. break;
  210. }
  211. if (qc->scsicmd)
  212. ASSIGN_SAS_TASK(qc->scsicmd, task);
  213. if (sas_ha->lldd_max_execute_num < 2)
  214. ret = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
  215. else
  216. ret = sas_queue_up(task);
  217. /* Examine */
  218. if (ret) {
  219. SAS_DPRINTK("lldd_execute_task returned: %d\n", ret);
  220. if (qc->scsicmd)
  221. ASSIGN_SAS_TASK(qc->scsicmd, NULL);
  222. sas_free_task(task);
  223. ret = AC_ERR_SYSTEM;
  224. }
  225. out:
  226. spin_lock(ap->lock);
  227. local_irq_restore(flags);
  228. return ret;
  229. }
  230. static bool sas_ata_qc_fill_rtf(struct ata_queued_cmd *qc)
  231. {
  232. struct domain_device *dev = qc->ap->private_data;
  233. memcpy(&qc->result_tf, &dev->sata_dev.tf, sizeof(qc->result_tf));
  234. return true;
  235. }
  236. static struct sas_internal *dev_to_sas_internal(struct domain_device *dev)
  237. {
  238. return to_sas_internal(dev->port->ha->core.shost->transportt);
  239. }
  240. static void sas_get_ata_command_set(struct domain_device *dev);
  241. int sas_get_ata_info(struct domain_device *dev, struct ex_phy *phy)
  242. {
  243. if (phy->attached_tproto & SAS_PROTOCOL_STP)
  244. dev->tproto = phy->attached_tproto;
  245. if (phy->attached_sata_dev)
  246. dev->tproto |= SATA_DEV;
  247. if (phy->attached_dev_type == SATA_PENDING)
  248. dev->dev_type = SATA_PENDING;
  249. else {
  250. int res;
  251. dev->dev_type = SATA_DEV;
  252. res = sas_get_report_phy_sata(dev->parent, phy->phy_id,
  253. &dev->sata_dev.rps_resp);
  254. if (res) {
  255. SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
  256. "0x%x\n", SAS_ADDR(dev->parent->sas_addr),
  257. phy->phy_id, res);
  258. return res;
  259. }
  260. memcpy(dev->frame_rcvd, &dev->sata_dev.rps_resp.rps.fis,
  261. sizeof(struct dev_to_host_fis));
  262. /* TODO switch to ata_dev_classify() */
  263. sas_get_ata_command_set(dev);
  264. }
  265. return 0;
  266. }
  267. static int sas_ata_clear_pending(struct domain_device *dev, struct ex_phy *phy)
  268. {
  269. int res;
  270. /* we weren't pending, so successfully end the reset sequence now */
  271. if (dev->dev_type != SATA_PENDING)
  272. return 1;
  273. /* hmmm, if this succeeds do we need to repost the domain_device to the
  274. * lldd so it can pick up new parameters?
  275. */
  276. res = sas_get_ata_info(dev, phy);
  277. if (res)
  278. return 0; /* retry */
  279. else
  280. return 1;
  281. }
  282. static int smp_ata_check_ready(struct ata_link *link)
  283. {
  284. int res;
  285. struct ata_port *ap = link->ap;
  286. struct domain_device *dev = ap->private_data;
  287. struct domain_device *ex_dev = dev->parent;
  288. struct sas_phy *phy = sas_get_local_phy(dev);
  289. struct ex_phy *ex_phy = &ex_dev->ex_dev.ex_phy[phy->number];
  290. res = sas_ex_phy_discover(ex_dev, phy->number);
  291. sas_put_local_phy(phy);
  292. /* break the wait early if the expander is unreachable,
  293. * otherwise keep polling
  294. */
  295. if (res == -ECOMM)
  296. return res;
  297. if (res != SMP_RESP_FUNC_ACC)
  298. return 0;
  299. switch (ex_phy->attached_dev_type) {
  300. case SATA_PENDING:
  301. return 0;
  302. case SAS_END_DEV:
  303. if (ex_phy->attached_sata_dev)
  304. return sas_ata_clear_pending(dev, ex_phy);
  305. default:
  306. return -ENODEV;
  307. }
  308. }
  309. static int local_ata_check_ready(struct ata_link *link)
  310. {
  311. struct ata_port *ap = link->ap;
  312. struct domain_device *dev = ap->private_data;
  313. struct sas_internal *i = dev_to_sas_internal(dev);
  314. if (i->dft->lldd_ata_check_ready)
  315. return i->dft->lldd_ata_check_ready(dev);
  316. else {
  317. /* lldd's that don't implement 'ready' checking get the
  318. * old default behavior of not coordinating reset
  319. * recovery with libata
  320. */
  321. return 1;
  322. }
  323. }
  324. static int sas_ata_printk(const char *level, const struct domain_device *ddev,
  325. const char *fmt, ...)
  326. {
  327. struct ata_port *ap = ddev->sata_dev.ap;
  328. struct device *dev = &ddev->rphy->dev;
  329. struct va_format vaf;
  330. va_list args;
  331. int r;
  332. va_start(args, fmt);
  333. vaf.fmt = fmt;
  334. vaf.va = &args;
  335. r = printk("%ssas: ata%u: %s: %pV",
  336. level, ap->print_id, dev_name(dev), &vaf);
  337. va_end(args);
  338. return r;
  339. }
  340. static int sas_ata_hard_reset(struct ata_link *link, unsigned int *class,
  341. unsigned long deadline)
  342. {
  343. int ret = 0, res;
  344. struct sas_phy *phy;
  345. struct ata_port *ap = link->ap;
  346. int (*check_ready)(struct ata_link *link);
  347. struct domain_device *dev = ap->private_data;
  348. struct sas_internal *i = dev_to_sas_internal(dev);
  349. if (test_bit(SAS_DEV_GONE, &dev->state))
  350. return -ENODEV;
  351. res = i->dft->lldd_I_T_nexus_reset(dev);
  352. if (res != TMF_RESP_FUNC_COMPLETE)
  353. sas_ata_printk(KERN_DEBUG, dev, "Unable to reset ata device?\n");
  354. phy = sas_get_local_phy(dev);
  355. if (scsi_is_sas_phy_local(phy))
  356. check_ready = local_ata_check_ready;
  357. else
  358. check_ready = smp_ata_check_ready;
  359. sas_put_local_phy(phy);
  360. ret = ata_wait_after_reset(link, deadline, check_ready);
  361. if (ret && ret != -EAGAIN)
  362. sas_ata_printk(KERN_ERR, dev, "reset failed (errno=%d)\n", ret);
  363. /* XXX: if the class changes during the reset the upper layer
  364. * should be informed, if the device has gone away we assume
  365. * libsas will eventually delete it
  366. */
  367. switch (dev->sata_dev.command_set) {
  368. case ATA_COMMAND_SET:
  369. *class = ATA_DEV_ATA;
  370. break;
  371. case ATAPI_COMMAND_SET:
  372. *class = ATA_DEV_ATAPI;
  373. break;
  374. }
  375. ap->cbl = ATA_CBL_SATA;
  376. return ret;
  377. }
  378. static int sas_ata_soft_reset(struct ata_link *link, unsigned int *class,
  379. unsigned long deadline)
  380. {
  381. struct ata_port *ap = link->ap;
  382. struct domain_device *dev = ap->private_data;
  383. struct sas_internal *i = dev_to_sas_internal(dev);
  384. int res = TMF_RESP_FUNC_FAILED;
  385. int ret = 0;
  386. if (i->dft->lldd_ata_soft_reset)
  387. res = i->dft->lldd_ata_soft_reset(dev);
  388. if (res != TMF_RESP_FUNC_COMPLETE) {
  389. SAS_DPRINTK("%s: Unable to soft reset\n", __func__);
  390. ret = -EAGAIN;
  391. }
  392. switch (dev->sata_dev.command_set) {
  393. case ATA_COMMAND_SET:
  394. SAS_DPRINTK("%s: Found ATA device.\n", __func__);
  395. *class = ATA_DEV_ATA;
  396. break;
  397. case ATAPI_COMMAND_SET:
  398. SAS_DPRINTK("%s: Found ATAPI device.\n", __func__);
  399. *class = ATA_DEV_ATAPI;
  400. break;
  401. default:
  402. SAS_DPRINTK("%s: Unknown SATA command set: %d.\n",
  403. __func__, dev->sata_dev.command_set);
  404. *class = ATA_DEV_UNKNOWN;
  405. break;
  406. }
  407. ap->cbl = ATA_CBL_SATA;
  408. return ret;
  409. }
  410. /*
  411. * notify the lldd to forget the sas_task for this internal ata command
  412. * that bypasses scsi-eh
  413. */
  414. static void sas_ata_internal_abort(struct sas_task *task)
  415. {
  416. struct sas_internal *si = dev_to_sas_internal(task->dev);
  417. unsigned long flags;
  418. int res;
  419. spin_lock_irqsave(&task->task_state_lock, flags);
  420. if (task->task_state_flags & SAS_TASK_STATE_ABORTED ||
  421. task->task_state_flags & SAS_TASK_STATE_DONE) {
  422. spin_unlock_irqrestore(&task->task_state_lock, flags);
  423. SAS_DPRINTK("%s: Task %p already finished.\n", __func__,
  424. task);
  425. goto out;
  426. }
  427. task->task_state_flags |= SAS_TASK_STATE_ABORTED;
  428. spin_unlock_irqrestore(&task->task_state_lock, flags);
  429. res = si->dft->lldd_abort_task(task);
  430. spin_lock_irqsave(&task->task_state_lock, flags);
  431. if (task->task_state_flags & SAS_TASK_STATE_DONE ||
  432. res == TMF_RESP_FUNC_COMPLETE) {
  433. spin_unlock_irqrestore(&task->task_state_lock, flags);
  434. goto out;
  435. }
  436. /* XXX we are not prepared to deal with ->lldd_abort_task()
  437. * failures. TODO: lldds need to unconditionally forget about
  438. * aborted ata tasks, otherwise we (likely) leak the sas task
  439. * here
  440. */
  441. SAS_DPRINTK("%s: Task %p leaked.\n", __func__, task);
  442. if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
  443. task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
  444. spin_unlock_irqrestore(&task->task_state_lock, flags);
  445. return;
  446. out:
  447. list_del_init(&task->list);
  448. sas_free_task(task);
  449. }
  450. static void sas_ata_post_internal(struct ata_queued_cmd *qc)
  451. {
  452. if (qc->flags & ATA_QCFLAG_FAILED)
  453. qc->err_mask |= AC_ERR_OTHER;
  454. if (qc->err_mask) {
  455. /*
  456. * Find the sas_task and kill it. By this point, libata
  457. * has decided to kill the qc and has frozen the port.
  458. * In this state sas_ata_task_done() will no longer free
  459. * the sas_task, so we need to notify the lldd (via
  460. * ->lldd_abort_task) that the task is dead and free it
  461. * ourselves.
  462. */
  463. struct sas_task *task = qc->lldd_task;
  464. qc->lldd_task = NULL;
  465. if (!task)
  466. return;
  467. task->uldd_task = NULL;
  468. sas_ata_internal_abort(task);
  469. }
  470. }
  471. static void sas_ata_set_dmamode(struct ata_port *ap, struct ata_device *ata_dev)
  472. {
  473. struct domain_device *dev = ap->private_data;
  474. struct sas_internal *i = dev_to_sas_internal(dev);
  475. if (i->dft->lldd_ata_set_dmamode)
  476. i->dft->lldd_ata_set_dmamode(dev);
  477. }
  478. static struct ata_port_operations sas_sata_ops = {
  479. .prereset = ata_std_prereset,
  480. .softreset = sas_ata_soft_reset,
  481. .hardreset = sas_ata_hard_reset,
  482. .postreset = ata_std_postreset,
  483. .error_handler = ata_std_error_handler,
  484. .post_internal_cmd = sas_ata_post_internal,
  485. .qc_defer = ata_std_qc_defer,
  486. .qc_prep = ata_noop_qc_prep,
  487. .qc_issue = sas_ata_qc_issue,
  488. .qc_fill_rtf = sas_ata_qc_fill_rtf,
  489. .port_start = ata_sas_port_start,
  490. .port_stop = ata_sas_port_stop,
  491. .set_dmamode = sas_ata_set_dmamode,
  492. };
  493. static struct ata_port_info sata_port_info = {
  494. .flags = ATA_FLAG_SATA | ATA_FLAG_PIO_DMA | ATA_FLAG_NCQ,
  495. .pio_mask = ATA_PIO4,
  496. .mwdma_mask = ATA_MWDMA2,
  497. .udma_mask = ATA_UDMA6,
  498. .port_ops = &sas_sata_ops
  499. };
  500. int sas_ata_init_host_and_port(struct domain_device *found_dev)
  501. {
  502. struct sas_ha_struct *ha = found_dev->port->ha;
  503. struct Scsi_Host *shost = ha->core.shost;
  504. struct ata_port *ap;
  505. ata_host_init(&found_dev->sata_dev.ata_host,
  506. ha->dev,
  507. sata_port_info.flags,
  508. &sas_sata_ops);
  509. ap = ata_sas_port_alloc(&found_dev->sata_dev.ata_host,
  510. &sata_port_info,
  511. shost);
  512. if (!ap) {
  513. SAS_DPRINTK("ata_sas_port_alloc failed.\n");
  514. return -ENODEV;
  515. }
  516. ap->private_data = found_dev;
  517. ap->cbl = ATA_CBL_SATA;
  518. ap->scsi_host = shost;
  519. /* publish initialized ata port */
  520. smp_wmb();
  521. found_dev->sata_dev.ap = ap;
  522. return 0;
  523. }
  524. void sas_ata_task_abort(struct sas_task *task)
  525. {
  526. struct ata_queued_cmd *qc = task->uldd_task;
  527. struct completion *waiting;
  528. /* Bounce SCSI-initiated commands to the SCSI EH */
  529. if (qc->scsicmd) {
  530. struct request_queue *q = qc->scsicmd->device->request_queue;
  531. unsigned long flags;
  532. spin_lock_irqsave(q->queue_lock, flags);
  533. blk_abort_request(qc->scsicmd->request);
  534. spin_unlock_irqrestore(q->queue_lock, flags);
  535. scsi_schedule_eh(qc->scsicmd->device->host);
  536. return;
  537. }
  538. /* Internal command, fake a timeout and complete. */
  539. qc->flags &= ~ATA_QCFLAG_ACTIVE;
  540. qc->flags |= ATA_QCFLAG_FAILED;
  541. qc->err_mask |= AC_ERR_TIMEOUT;
  542. waiting = qc->private_data;
  543. complete(waiting);
  544. }
  545. static void sas_get_ata_command_set(struct domain_device *dev)
  546. {
  547. struct dev_to_host_fis *fis =
  548. (struct dev_to_host_fis *) dev->frame_rcvd;
  549. if (dev->dev_type == SATA_PENDING)
  550. return;
  551. if ((fis->sector_count == 1 && /* ATA */
  552. fis->lbal == 1 &&
  553. fis->lbam == 0 &&
  554. fis->lbah == 0 &&
  555. fis->device == 0)
  556. ||
  557. (fis->sector_count == 0 && /* CE-ATA (mATA) */
  558. fis->lbal == 0 &&
  559. fis->lbam == 0xCE &&
  560. fis->lbah == 0xAA &&
  561. (fis->device & ~0x10) == 0))
  562. dev->sata_dev.command_set = ATA_COMMAND_SET;
  563. else if ((fis->interrupt_reason == 1 && /* ATAPI */
  564. fis->lbal == 1 &&
  565. fis->byte_count_low == 0x14 &&
  566. fis->byte_count_high == 0xEB &&
  567. (fis->device & ~0x10) == 0))
  568. dev->sata_dev.command_set = ATAPI_COMMAND_SET;
  569. else if ((fis->sector_count == 1 && /* SEMB */
  570. fis->lbal == 1 &&
  571. fis->lbam == 0x3C &&
  572. fis->lbah == 0xC3 &&
  573. fis->device == 0)
  574. ||
  575. (fis->interrupt_reason == 1 && /* SATA PM */
  576. fis->lbal == 1 &&
  577. fis->byte_count_low == 0x69 &&
  578. fis->byte_count_high == 0x96 &&
  579. (fis->device & ~0x10) == 0))
  580. /* Treat it as a superset? */
  581. dev->sata_dev.command_set = ATAPI_COMMAND_SET;
  582. }
  583. void sas_probe_sata(struct asd_sas_port *port)
  584. {
  585. struct domain_device *dev, *n;
  586. int err;
  587. mutex_lock(&port->ha->disco_mutex);
  588. list_for_each_entry_safe(dev, n, &port->disco_list, disco_list_node) {
  589. if (!dev_is_sata(dev))
  590. continue;
  591. err = sas_ata_init_host_and_port(dev);
  592. if (err)
  593. sas_fail_probe(dev, __func__, err);
  594. else
  595. ata_sas_async_port_init(dev->sata_dev.ap);
  596. }
  597. mutex_unlock(&port->ha->disco_mutex);
  598. list_for_each_entry_safe(dev, n, &port->disco_list, disco_list_node) {
  599. if (!dev_is_sata(dev))
  600. continue;
  601. sas_ata_wait_eh(dev);
  602. /* if libata could not bring the link up, don't surface
  603. * the device
  604. */
  605. if (ata_dev_disabled(sas_to_ata_dev(dev)))
  606. sas_fail_probe(dev, __func__, -ENODEV);
  607. }
  608. }
  609. /**
  610. * sas_discover_sata -- discover an STP/SATA domain device
  611. * @dev: pointer to struct domain_device of interest
  612. *
  613. * Devices directly attached to a HA port, have no parents. All other
  614. * devices do, and should have their "parent" pointer set appropriately
  615. * before calling this function.
  616. */
  617. int sas_discover_sata(struct domain_device *dev)
  618. {
  619. int res;
  620. if (dev->dev_type == SATA_PM)
  621. return -ENODEV;
  622. sas_get_ata_command_set(dev);
  623. sas_fill_in_rphy(dev, dev->rphy);
  624. res = sas_notify_lldd_dev_found(dev);
  625. if (res)
  626. return res;
  627. sas_discover_event(dev->port, DISCE_PROBE);
  628. return 0;
  629. }
  630. static void async_sas_ata_eh(void *data, async_cookie_t cookie)
  631. {
  632. struct domain_device *dev = data;
  633. struct ata_port *ap = dev->sata_dev.ap;
  634. struct sas_ha_struct *ha = dev->port->ha;
  635. /* hold a reference over eh since we may be racing with final
  636. * remove once all commands are completed
  637. */
  638. kref_get(&dev->kref);
  639. sas_ata_printk(KERN_DEBUG, dev, "dev error handler\n");
  640. ata_scsi_port_error_handler(ha->core.shost, ap);
  641. sas_put_device(dev);
  642. }
  643. static bool sas_ata_dev_eh_valid(struct domain_device *dev)
  644. {
  645. struct ata_port *ap;
  646. if (!dev_is_sata(dev))
  647. return false;
  648. ap = dev->sata_dev.ap;
  649. /* consume fully initialized ata ports */
  650. smp_rmb();
  651. return !!ap;
  652. }
  653. void sas_ata_strategy_handler(struct Scsi_Host *shost)
  654. {
  655. struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
  656. LIST_HEAD(async);
  657. int i;
  658. /* it's ok to defer revalidation events during ata eh, these
  659. * disks are in one of three states:
  660. * 1/ present for initial domain discovery, and these
  661. * resets will cause bcn flutters
  662. * 2/ hot removed, we'll discover that after eh fails
  663. * 3/ hot added after initial discovery, lost the race, and need
  664. * to catch the next train.
  665. */
  666. sas_disable_revalidation(sas_ha);
  667. spin_lock_irq(&sas_ha->phy_port_lock);
  668. for (i = 0; i < sas_ha->num_phys; i++) {
  669. struct asd_sas_port *port = sas_ha->sas_port[i];
  670. struct domain_device *dev;
  671. spin_lock(&port->dev_list_lock);
  672. list_for_each_entry(dev, &port->dev_list, dev_list_node) {
  673. if (!sas_ata_dev_eh_valid(dev))
  674. continue;
  675. async_schedule_domain(async_sas_ata_eh, dev, &async);
  676. }
  677. spin_unlock(&port->dev_list_lock);
  678. }
  679. spin_unlock_irq(&sas_ha->phy_port_lock);
  680. async_synchronize_full_domain(&async);
  681. sas_enable_revalidation(sas_ha);
  682. }
  683. void sas_ata_eh(struct Scsi_Host *shost, struct list_head *work_q,
  684. struct list_head *done_q)
  685. {
  686. struct scsi_cmnd *cmd, *n;
  687. struct domain_device *eh_dev;
  688. do {
  689. LIST_HEAD(sata_q);
  690. eh_dev = NULL;
  691. list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
  692. struct domain_device *ddev = cmd_to_domain_dev(cmd);
  693. if (!dev_is_sata(ddev) || TO_SAS_TASK(cmd))
  694. continue;
  695. if (eh_dev && eh_dev != ddev)
  696. continue;
  697. eh_dev = ddev;
  698. list_move(&cmd->eh_entry, &sata_q);
  699. }
  700. if (!list_empty(&sata_q)) {
  701. struct ata_port *ap = eh_dev->sata_dev.ap;
  702. sas_ata_printk(KERN_DEBUG, eh_dev, "cmd error handler\n");
  703. ata_scsi_cmd_error_handler(shost, ap, &sata_q);
  704. /*
  705. * ata's error handler may leave the cmd on the list
  706. * so make sure they don't remain on a stack list
  707. * about to go out of scope.
  708. *
  709. * This looks strange, since the commands are
  710. * now part of no list, but the next error
  711. * action will be ata_port_error_handler()
  712. * which takes no list and sweeps them up
  713. * anyway from the ata tag array.
  714. */
  715. while (!list_empty(&sata_q))
  716. list_del_init(sata_q.next);
  717. }
  718. } while (eh_dev);
  719. }
  720. void sas_ata_schedule_reset(struct domain_device *dev)
  721. {
  722. struct ata_eh_info *ehi;
  723. struct ata_port *ap;
  724. unsigned long flags;
  725. if (!dev_is_sata(dev))
  726. return;
  727. ap = dev->sata_dev.ap;
  728. ehi = &ap->link.eh_info;
  729. spin_lock_irqsave(ap->lock, flags);
  730. ehi->err_mask |= AC_ERR_TIMEOUT;
  731. ehi->action |= ATA_EH_RESET;
  732. ata_port_schedule_eh(ap);
  733. spin_unlock_irqrestore(ap->lock, flags);
  734. }
  735. EXPORT_SYMBOL_GPL(sas_ata_schedule_reset);
  736. void sas_ata_wait_eh(struct domain_device *dev)
  737. {
  738. struct ata_port *ap;
  739. if (!dev_is_sata(dev))
  740. return;
  741. ap = dev->sata_dev.ap;
  742. ata_port_wait_eh(ap);
  743. }