sas_ata.c 21 KB

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