sas_ata.c 21 KB

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