sas_ata.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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 SAM_CHECK_COND:
  65. case SAS_OPEN_TO:
  66. case SAS_OPEN_REJECT:
  67. SAS_DPRINTK("%s: Saw error %d. What to do?\n",
  68. __func__, ts->stat);
  69. return AC_ERR_OTHER;
  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. if (!qc)
  90. goto qc_already_gone;
  91. dev = qc->ap->private_data;
  92. sas_ha = dev->port->ha;
  93. spin_lock_irqsave(dev->sata_dev.ap->lock, flags);
  94. if (stat->stat == SAS_PROTO_RESPONSE || stat->stat == SAM_GOOD) {
  95. ata_tf_from_fis(resp->ending_fis, &dev->sata_dev.tf);
  96. qc->err_mask |= ac_err_mask(dev->sata_dev.tf.command);
  97. dev->sata_dev.sstatus = resp->sstatus;
  98. dev->sata_dev.serror = resp->serror;
  99. dev->sata_dev.scontrol = resp->scontrol;
  100. } else if (stat->stat != SAM_STAT_GOOD) {
  101. ac = sas_to_ata_err(stat);
  102. if (ac) {
  103. SAS_DPRINTK("%s: SAS error %x\n", __func__,
  104. stat->stat);
  105. /* We saw a SAS error. Send a vague error. */
  106. qc->err_mask = ac;
  107. dev->sata_dev.tf.feature = 0x04; /* status err */
  108. dev->sata_dev.tf.command = ATA_ERR;
  109. }
  110. }
  111. qc->lldd_task = NULL;
  112. if (qc->scsicmd)
  113. ASSIGN_SAS_TASK(qc->scsicmd, NULL);
  114. ata_qc_complete(qc);
  115. spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
  116. /*
  117. * If the sas_task has an ata qc, a scsi_cmnd and the aborted
  118. * flag is set, then we must have come in via the libsas EH
  119. * functions. When we exit this function, we need to put the
  120. * scsi_cmnd on the list of finished errors. The ata_qc_complete
  121. * call cleans up the libata side of things but we're protected
  122. * from the scsi_cmnd going away because the scsi_cmnd is owned
  123. * by the EH, making libata's call to scsi_done a NOP.
  124. */
  125. spin_lock_irqsave(&task->task_state_lock, flags);
  126. if (qc->scsicmd && task->task_state_flags & SAS_TASK_STATE_ABORTED)
  127. scsi_eh_finish_cmd(qc->scsicmd, &sas_ha->eh_done_q);
  128. spin_unlock_irqrestore(&task->task_state_lock, flags);
  129. qc_already_gone:
  130. list_del_init(&task->list);
  131. sas_free_task(task);
  132. }
  133. static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
  134. {
  135. int res;
  136. struct sas_task *task;
  137. struct domain_device *dev = qc->ap->private_data;
  138. struct sas_ha_struct *sas_ha = dev->port->ha;
  139. struct Scsi_Host *host = sas_ha->core.shost;
  140. struct sas_internal *i = to_sas_internal(host->transportt);
  141. struct scatterlist *sg;
  142. unsigned int xfer = 0;
  143. unsigned int si;
  144. task = sas_alloc_task(GFP_ATOMIC);
  145. if (!task)
  146. return AC_ERR_SYSTEM;
  147. task->dev = dev;
  148. task->task_proto = SAS_PROTOCOL_STP;
  149. task->task_done = sas_ata_task_done;
  150. if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
  151. qc->tf.command == ATA_CMD_FPDMA_READ) {
  152. /* Need to zero out the tag libata assigned us */
  153. qc->tf.nsect = 0;
  154. }
  155. ata_tf_to_fis(&qc->tf, 1, 0, (u8*)&task->ata_task.fis);
  156. task->uldd_task = qc;
  157. if (ata_is_atapi(qc->tf.protocol)) {
  158. memcpy(task->ata_task.atapi_packet, qc->cdb, qc->dev->cdb_len);
  159. task->total_xfer_len = qc->nbytes;
  160. task->num_scatter = qc->n_elem;
  161. } else {
  162. for_each_sg(qc->sg, sg, qc->n_elem, si)
  163. xfer += sg->length;
  164. task->total_xfer_len = xfer;
  165. task->num_scatter = si;
  166. }
  167. task->data_dir = qc->dma_dir;
  168. task->scatter = qc->sg;
  169. task->ata_task.retry_count = 1;
  170. task->task_state_flags = SAS_TASK_STATE_PENDING;
  171. qc->lldd_task = task;
  172. switch (qc->tf.protocol) {
  173. case ATA_PROT_NCQ:
  174. task->ata_task.use_ncq = 1;
  175. /* fall through */
  176. case ATAPI_PROT_DMA:
  177. case ATA_PROT_DMA:
  178. task->ata_task.dma_xfer = 1;
  179. break;
  180. }
  181. if (qc->scsicmd)
  182. ASSIGN_SAS_TASK(qc->scsicmd, task);
  183. if (sas_ha->lldd_max_execute_num < 2)
  184. res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
  185. else
  186. res = sas_queue_up(task);
  187. /* Examine */
  188. if (res) {
  189. SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
  190. if (qc->scsicmd)
  191. ASSIGN_SAS_TASK(qc->scsicmd, NULL);
  192. sas_free_task(task);
  193. return AC_ERR_SYSTEM;
  194. }
  195. return 0;
  196. }
  197. static bool sas_ata_qc_fill_rtf(struct ata_queued_cmd *qc)
  198. {
  199. struct domain_device *dev = qc->ap->private_data;
  200. memcpy(&qc->result_tf, &dev->sata_dev.tf, sizeof(qc->result_tf));
  201. return true;
  202. }
  203. static void sas_ata_phy_reset(struct ata_port *ap)
  204. {
  205. struct domain_device *dev = ap->private_data;
  206. struct sas_internal *i =
  207. to_sas_internal(dev->port->ha->core.shost->transportt);
  208. int res = TMF_RESP_FUNC_FAILED;
  209. if (i->dft->lldd_I_T_nexus_reset)
  210. res = i->dft->lldd_I_T_nexus_reset(dev);
  211. if (res != TMF_RESP_FUNC_COMPLETE)
  212. SAS_DPRINTK("%s: Unable to reset I T nexus?\n", __func__);
  213. switch (dev->sata_dev.command_set) {
  214. case ATA_COMMAND_SET:
  215. SAS_DPRINTK("%s: Found ATA device.\n", __func__);
  216. ap->link.device[0].class = ATA_DEV_ATA;
  217. break;
  218. case ATAPI_COMMAND_SET:
  219. SAS_DPRINTK("%s: Found ATAPI device.\n", __func__);
  220. ap->link.device[0].class = ATA_DEV_ATAPI;
  221. break;
  222. default:
  223. SAS_DPRINTK("%s: Unknown SATA command set: %d.\n",
  224. __func__,
  225. dev->sata_dev.command_set);
  226. ap->link.device[0].class = ATA_DEV_UNKNOWN;
  227. break;
  228. }
  229. ap->cbl = ATA_CBL_SATA;
  230. }
  231. static void sas_ata_post_internal(struct ata_queued_cmd *qc)
  232. {
  233. if (qc->flags & ATA_QCFLAG_FAILED)
  234. qc->err_mask |= AC_ERR_OTHER;
  235. if (qc->err_mask) {
  236. /*
  237. * Find the sas_task and kill it. By this point,
  238. * libata has decided to kill the qc, so we needn't
  239. * bother with sas_ata_task_done. But we still
  240. * ought to abort the task.
  241. */
  242. struct sas_task *task = qc->lldd_task;
  243. unsigned long flags;
  244. qc->lldd_task = NULL;
  245. if (task) {
  246. /* Should this be a AT(API) device reset? */
  247. spin_lock_irqsave(&task->task_state_lock, flags);
  248. task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
  249. spin_unlock_irqrestore(&task->task_state_lock, flags);
  250. task->uldd_task = NULL;
  251. __sas_task_abort(task);
  252. }
  253. }
  254. }
  255. static int sas_ata_scr_write(struct ata_link *link, unsigned int sc_reg_in,
  256. u32 val)
  257. {
  258. struct domain_device *dev = link->ap->private_data;
  259. SAS_DPRINTK("STUB %s\n", __func__);
  260. switch (sc_reg_in) {
  261. case SCR_STATUS:
  262. dev->sata_dev.sstatus = val;
  263. break;
  264. case SCR_CONTROL:
  265. dev->sata_dev.scontrol = val;
  266. break;
  267. case SCR_ERROR:
  268. dev->sata_dev.serror = val;
  269. break;
  270. case SCR_ACTIVE:
  271. dev->sata_dev.ap->link.sactive = val;
  272. break;
  273. default:
  274. return -EINVAL;
  275. }
  276. return 0;
  277. }
  278. static int sas_ata_scr_read(struct ata_link *link, unsigned int sc_reg_in,
  279. u32 *val)
  280. {
  281. struct domain_device *dev = link->ap->private_data;
  282. SAS_DPRINTK("STUB %s\n", __func__);
  283. switch (sc_reg_in) {
  284. case SCR_STATUS:
  285. *val = dev->sata_dev.sstatus;
  286. return 0;
  287. case SCR_CONTROL:
  288. *val = dev->sata_dev.scontrol;
  289. return 0;
  290. case SCR_ERROR:
  291. *val = dev->sata_dev.serror;
  292. return 0;
  293. case SCR_ACTIVE:
  294. *val = dev->sata_dev.ap->link.sactive;
  295. return 0;
  296. default:
  297. return -EINVAL;
  298. }
  299. }
  300. static struct ata_port_operations sas_sata_ops = {
  301. .phy_reset = sas_ata_phy_reset,
  302. .post_internal_cmd = sas_ata_post_internal,
  303. .qc_prep = ata_noop_qc_prep,
  304. .qc_issue = sas_ata_qc_issue,
  305. .qc_fill_rtf = sas_ata_qc_fill_rtf,
  306. .port_start = ata_sas_port_start,
  307. .port_stop = ata_sas_port_stop,
  308. .scr_read = sas_ata_scr_read,
  309. .scr_write = sas_ata_scr_write
  310. };
  311. static struct ata_port_info sata_port_info = {
  312. .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY | ATA_FLAG_SATA_RESET |
  313. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA | ATA_FLAG_NCQ,
  314. .pio_mask = 0x1f, /* PIO0-4 */
  315. .mwdma_mask = 0x07, /* MWDMA0-2 */
  316. .udma_mask = ATA_UDMA6,
  317. .port_ops = &sas_sata_ops
  318. };
  319. int sas_ata_init_host_and_port(struct domain_device *found_dev,
  320. struct scsi_target *starget)
  321. {
  322. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  323. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  324. struct ata_port *ap;
  325. ata_host_init(&found_dev->sata_dev.ata_host,
  326. ha->dev,
  327. sata_port_info.flags,
  328. &sas_sata_ops);
  329. ap = ata_sas_port_alloc(&found_dev->sata_dev.ata_host,
  330. &sata_port_info,
  331. shost);
  332. if (!ap) {
  333. SAS_DPRINTK("ata_sas_port_alloc failed.\n");
  334. return -ENODEV;
  335. }
  336. ap->private_data = found_dev;
  337. ap->cbl = ATA_CBL_SATA;
  338. ap->scsi_host = shost;
  339. found_dev->sata_dev.ap = ap;
  340. return 0;
  341. }
  342. void sas_ata_task_abort(struct sas_task *task)
  343. {
  344. struct ata_queued_cmd *qc = task->uldd_task;
  345. struct completion *waiting;
  346. /* Bounce SCSI-initiated commands to the SCSI EH */
  347. if (qc->scsicmd) {
  348. blk_abort_request(qc->scsicmd->request);
  349. scsi_schedule_eh(qc->scsicmd->device->host);
  350. return;
  351. }
  352. /* Internal command, fake a timeout and complete. */
  353. qc->flags &= ~ATA_QCFLAG_ACTIVE;
  354. qc->flags |= ATA_QCFLAG_FAILED;
  355. qc->err_mask |= AC_ERR_TIMEOUT;
  356. waiting = qc->private_data;
  357. complete(waiting);
  358. }
  359. static void sas_task_timedout(unsigned long _task)
  360. {
  361. struct sas_task *task = (void *) _task;
  362. unsigned long flags;
  363. spin_lock_irqsave(&task->task_state_lock, flags);
  364. if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
  365. task->task_state_flags |= SAS_TASK_STATE_ABORTED;
  366. spin_unlock_irqrestore(&task->task_state_lock, flags);
  367. complete(&task->completion);
  368. }
  369. static void sas_disc_task_done(struct sas_task *task)
  370. {
  371. if (!del_timer(&task->timer))
  372. return;
  373. complete(&task->completion);
  374. }
  375. #define SAS_DEV_TIMEOUT 10
  376. /**
  377. * sas_execute_task -- Basic task processing for discovery
  378. * @task: the task to be executed
  379. * @buffer: pointer to buffer to do I/O
  380. * @size: size of @buffer
  381. * @dma_dir: DMA direction. DMA_xxx
  382. */
  383. static int sas_execute_task(struct sas_task *task, void *buffer, int size,
  384. enum dma_data_direction dma_dir)
  385. {
  386. int res = 0;
  387. struct scatterlist *scatter = NULL;
  388. struct task_status_struct *ts = &task->task_status;
  389. int num_scatter = 0;
  390. int retries = 0;
  391. struct sas_internal *i =
  392. to_sas_internal(task->dev->port->ha->core.shost->transportt);
  393. if (dma_dir != DMA_NONE) {
  394. scatter = kzalloc(sizeof(*scatter), GFP_KERNEL);
  395. if (!scatter)
  396. goto out;
  397. sg_init_one(scatter, buffer, size);
  398. num_scatter = 1;
  399. }
  400. task->task_proto = task->dev->tproto;
  401. task->scatter = scatter;
  402. task->num_scatter = num_scatter;
  403. task->total_xfer_len = size;
  404. task->data_dir = dma_dir;
  405. task->task_done = sas_disc_task_done;
  406. if (dma_dir != DMA_NONE &&
  407. sas_protocol_ata(task->task_proto)) {
  408. task->num_scatter = dma_map_sg(task->dev->port->ha->dev,
  409. task->scatter,
  410. task->num_scatter,
  411. task->data_dir);
  412. }
  413. for (retries = 0; retries < 5; retries++) {
  414. task->task_state_flags = SAS_TASK_STATE_PENDING;
  415. init_completion(&task->completion);
  416. task->timer.data = (unsigned long) task;
  417. task->timer.function = sas_task_timedout;
  418. task->timer.expires = jiffies + SAS_DEV_TIMEOUT*HZ;
  419. add_timer(&task->timer);
  420. res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
  421. if (res) {
  422. del_timer(&task->timer);
  423. SAS_DPRINTK("executing SAS discovery task failed:%d\n",
  424. res);
  425. goto ex_err;
  426. }
  427. wait_for_completion(&task->completion);
  428. res = -ECOMM;
  429. if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
  430. int res2;
  431. SAS_DPRINTK("task aborted, flags:0x%x\n",
  432. task->task_state_flags);
  433. res2 = i->dft->lldd_abort_task(task);
  434. SAS_DPRINTK("came back from abort task\n");
  435. if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
  436. if (res2 == TMF_RESP_FUNC_COMPLETE)
  437. continue; /* Retry the task */
  438. else
  439. goto ex_err;
  440. }
  441. }
  442. if (task->task_status.stat == SAM_BUSY ||
  443. task->task_status.stat == SAM_TASK_SET_FULL ||
  444. task->task_status.stat == SAS_QUEUE_FULL) {
  445. SAS_DPRINTK("task: q busy, sleeping...\n");
  446. schedule_timeout_interruptible(HZ);
  447. } else if (task->task_status.stat == SAM_CHECK_COND) {
  448. struct scsi_sense_hdr shdr;
  449. if (!scsi_normalize_sense(ts->buf, ts->buf_valid_size,
  450. &shdr)) {
  451. SAS_DPRINTK("couldn't normalize sense\n");
  452. continue;
  453. }
  454. if ((shdr.sense_key == 6 && shdr.asc == 0x29) ||
  455. (shdr.sense_key == 2 && shdr.asc == 4 &&
  456. shdr.ascq == 1)) {
  457. SAS_DPRINTK("device %016llx LUN: %016llx "
  458. "powering up or not ready yet, "
  459. "sleeping...\n",
  460. SAS_ADDR(task->dev->sas_addr),
  461. SAS_ADDR(task->ssp_task.LUN));
  462. schedule_timeout_interruptible(5*HZ);
  463. } else if (shdr.sense_key == 1) {
  464. res = 0;
  465. break;
  466. } else if (shdr.sense_key == 5) {
  467. break;
  468. } else {
  469. SAS_DPRINTK("dev %016llx LUN: %016llx "
  470. "sense key:0x%x ASC:0x%x ASCQ:0x%x"
  471. "\n",
  472. SAS_ADDR(task->dev->sas_addr),
  473. SAS_ADDR(task->ssp_task.LUN),
  474. shdr.sense_key,
  475. shdr.asc, shdr.ascq);
  476. }
  477. } else if (task->task_status.resp != SAS_TASK_COMPLETE ||
  478. task->task_status.stat != SAM_GOOD) {
  479. SAS_DPRINTK("task finished with resp:0x%x, "
  480. "stat:0x%x\n",
  481. task->task_status.resp,
  482. task->task_status.stat);
  483. goto ex_err;
  484. } else {
  485. res = 0;
  486. break;
  487. }
  488. }
  489. ex_err:
  490. if (dma_dir != DMA_NONE) {
  491. if (sas_protocol_ata(task->task_proto))
  492. dma_unmap_sg(task->dev->port->ha->dev,
  493. task->scatter, task->num_scatter,
  494. task->data_dir);
  495. kfree(scatter);
  496. }
  497. out:
  498. return res;
  499. }
  500. /* ---------- SATA ---------- */
  501. static void sas_get_ata_command_set(struct domain_device *dev)
  502. {
  503. struct dev_to_host_fis *fis =
  504. (struct dev_to_host_fis *) dev->frame_rcvd;
  505. if ((fis->sector_count == 1 && /* ATA */
  506. fis->lbal == 1 &&
  507. fis->lbam == 0 &&
  508. fis->lbah == 0 &&
  509. fis->device == 0)
  510. ||
  511. (fis->sector_count == 0 && /* CE-ATA (mATA) */
  512. fis->lbal == 0 &&
  513. fis->lbam == 0xCE &&
  514. fis->lbah == 0xAA &&
  515. (fis->device & ~0x10) == 0))
  516. dev->sata_dev.command_set = ATA_COMMAND_SET;
  517. else if ((fis->interrupt_reason == 1 && /* ATAPI */
  518. fis->lbal == 1 &&
  519. fis->byte_count_low == 0x14 &&
  520. fis->byte_count_high == 0xEB &&
  521. (fis->device & ~0x10) == 0))
  522. dev->sata_dev.command_set = ATAPI_COMMAND_SET;
  523. else if ((fis->sector_count == 1 && /* SEMB */
  524. fis->lbal == 1 &&
  525. fis->lbam == 0x3C &&
  526. fis->lbah == 0xC3 &&
  527. fis->device == 0)
  528. ||
  529. (fis->interrupt_reason == 1 && /* SATA PM */
  530. fis->lbal == 1 &&
  531. fis->byte_count_low == 0x69 &&
  532. fis->byte_count_high == 0x96 &&
  533. (fis->device & ~0x10) == 0))
  534. /* Treat it as a superset? */
  535. dev->sata_dev.command_set = ATAPI_COMMAND_SET;
  536. }
  537. /**
  538. * sas_issue_ata_cmd -- Basic SATA command processing for discovery
  539. * @dev: the device to send the command to
  540. * @command: the command register
  541. * @features: the features register
  542. * @buffer: pointer to buffer to do I/O
  543. * @size: size of @buffer
  544. * @dma_dir: DMA direction. DMA_xxx
  545. */
  546. static int sas_issue_ata_cmd(struct domain_device *dev, u8 command,
  547. u8 features, void *buffer, int size,
  548. enum dma_data_direction dma_dir)
  549. {
  550. int res = 0;
  551. struct sas_task *task;
  552. struct dev_to_host_fis *d2h_fis = (struct dev_to_host_fis *)
  553. &dev->frame_rcvd[0];
  554. res = -ENOMEM;
  555. task = sas_alloc_task(GFP_KERNEL);
  556. if (!task)
  557. goto out;
  558. task->dev = dev;
  559. task->ata_task.fis.fis_type = 0x27;
  560. task->ata_task.fis.command = command;
  561. task->ata_task.fis.features = features;
  562. task->ata_task.fis.device = d2h_fis->device;
  563. task->ata_task.retry_count = 1;
  564. res = sas_execute_task(task, buffer, size, dma_dir);
  565. sas_free_task(task);
  566. out:
  567. return res;
  568. }
  569. #define ATA_IDENTIFY_DEV 0xEC
  570. #define ATA_IDENTIFY_PACKET_DEV 0xA1
  571. #define ATA_SET_FEATURES 0xEF
  572. #define ATA_FEATURE_PUP_STBY_SPIN_UP 0x07
  573. /**
  574. * sas_discover_sata_dev -- discover a STP/SATA device (SATA_DEV)
  575. * @dev: STP/SATA device of interest (ATA/ATAPI)
  576. *
  577. * The LLDD has already been notified of this device, so that we can
  578. * send FISes to it. Here we try to get IDENTIFY DEVICE or IDENTIFY
  579. * PACKET DEVICE, if ATAPI device, so that the LLDD can fine-tune its
  580. * performance for this device.
  581. */
  582. static int sas_discover_sata_dev(struct domain_device *dev)
  583. {
  584. int res;
  585. __le16 *identify_x;
  586. u8 command;
  587. identify_x = kzalloc(512, GFP_KERNEL);
  588. if (!identify_x)
  589. return -ENOMEM;
  590. if (dev->sata_dev.command_set == ATA_COMMAND_SET) {
  591. dev->sata_dev.identify_device = identify_x;
  592. command = ATA_IDENTIFY_DEV;
  593. } else {
  594. dev->sata_dev.identify_packet_device = identify_x;
  595. command = ATA_IDENTIFY_PACKET_DEV;
  596. }
  597. res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
  598. DMA_FROM_DEVICE);
  599. if (res)
  600. goto out_err;
  601. /* lives on the media? */
  602. if (le16_to_cpu(identify_x[0]) & 4) {
  603. /* incomplete response */
  604. SAS_DPRINTK("sending SET FEATURE/PUP_STBY_SPIN_UP to "
  605. "dev %llx\n", SAS_ADDR(dev->sas_addr));
  606. if (!(identify_x[83] & cpu_to_le16(1<<6)))
  607. goto cont1;
  608. res = sas_issue_ata_cmd(dev, ATA_SET_FEATURES,
  609. ATA_FEATURE_PUP_STBY_SPIN_UP,
  610. NULL, 0, DMA_NONE);
  611. if (res)
  612. goto cont1;
  613. schedule_timeout_interruptible(5*HZ); /* More time? */
  614. res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
  615. DMA_FROM_DEVICE);
  616. if (res)
  617. goto out_err;
  618. }
  619. cont1:
  620. /* XXX Hint: register this SATA device with SATL.
  621. When this returns, dev->sata_dev->lu is alive and
  622. present.
  623. sas_satl_register_dev(dev);
  624. */
  625. sas_fill_in_rphy(dev, dev->rphy);
  626. return 0;
  627. out_err:
  628. dev->sata_dev.identify_packet_device = NULL;
  629. dev->sata_dev.identify_device = NULL;
  630. kfree(identify_x);
  631. return res;
  632. }
  633. static int sas_discover_sata_pm(struct domain_device *dev)
  634. {
  635. return -ENODEV;
  636. }
  637. /**
  638. * sas_discover_sata -- discover an STP/SATA domain device
  639. * @dev: pointer to struct domain_device of interest
  640. *
  641. * First we notify the LLDD of this device, so we can send frames to
  642. * it. Then depending on the type of device we call the appropriate
  643. * discover functions. Once device discover is done, we notify the
  644. * LLDD so that it can fine-tune its parameters for the device, by
  645. * removing it and then adding it. That is, the second time around,
  646. * the driver would have certain fields, that it is looking at, set.
  647. * Finally we initialize the kobj so that the device can be added to
  648. * the system at registration time. Devices directly attached to a HA
  649. * port, have no parents. All other devices do, and should have their
  650. * "parent" pointer set appropriately before calling this function.
  651. */
  652. int sas_discover_sata(struct domain_device *dev)
  653. {
  654. int res;
  655. sas_get_ata_command_set(dev);
  656. res = sas_notify_lldd_dev_found(dev);
  657. if (res)
  658. return res;
  659. switch (dev->dev_type) {
  660. case SATA_DEV:
  661. res = sas_discover_sata_dev(dev);
  662. break;
  663. case SATA_PM:
  664. res = sas_discover_sata_pm(dev);
  665. break;
  666. default:
  667. break;
  668. }
  669. sas_notify_lldd_dev_gone(dev);
  670. if (!res) {
  671. sas_notify_lldd_dev_found(dev);
  672. res = sas_rphy_add(dev->rphy);
  673. }
  674. return res;
  675. }