sas_ata.c 20 KB

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