PageRenderTime 182ms CodeModel.GetById 6ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/scsi/libsas/sas_ata.c

https://bitbucket.org/abioy/linux
C | 777 lines | 574 code | 98 blank | 105 comment | 127 complexity | 0c36aeacb328f5f433e128211ba73109 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
  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 request_queue *q = qc->scsicmd->device->request_queue;
  346. struct completion *waiting;
  347. unsigned long flags;
  348. /* Bounce SCSI-initiated commands to the SCSI EH */
  349. if (qc->scsicmd) {
  350. spin_lock_irqsave(q->queue_lock, flags);
  351. blk_abort_request(qc->scsicmd->request);
  352. spin_unlock_irqrestore(q->queue_lock, flags);
  353. scsi_schedule_eh(qc->scsicmd->device->host);
  354. return;
  355. }
  356. /* Internal command, fake a timeout and complete. */
  357. qc->flags &= ~ATA_QCFLAG_ACTIVE;
  358. qc->flags |= ATA_QCFLAG_FAILED;
  359. qc->err_mask |= AC_ERR_TIMEOUT;
  360. waiting = qc->private_data;
  361. complete(waiting);
  362. }
  363. static void sas_task_timedout(unsigned long _task)
  364. {
  365. struct sas_task *task = (void *) _task;
  366. unsigned long flags;
  367. spin_lock_irqsave(&task->task_state_lock, flags);
  368. if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
  369. task->task_state_flags |= SAS_TASK_STATE_ABORTED;
  370. spin_unlock_irqrestore(&task->task_state_lock, flags);
  371. complete(&task->completion);
  372. }
  373. static void sas_disc_task_done(struct sas_task *task)
  374. {
  375. if (!del_timer(&task->timer))
  376. return;
  377. complete(&task->completion);
  378. }
  379. #define SAS_DEV_TIMEOUT 10
  380. /**
  381. * sas_execute_task -- Basic task processing for discovery
  382. * @task: the task to be executed
  383. * @buffer: pointer to buffer to do I/O
  384. * @size: size of @buffer
  385. * @dma_dir: DMA direction. DMA_xxx
  386. */
  387. static int sas_execute_task(struct sas_task *task, void *buffer, int size,
  388. enum dma_data_direction dma_dir)
  389. {
  390. int res = 0;
  391. struct scatterlist *scatter = NULL;
  392. struct task_status_struct *ts = &task->task_status;
  393. int num_scatter = 0;
  394. int retries = 0;
  395. struct sas_internal *i =
  396. to_sas_internal(task->dev->port->ha->core.shost->transportt);
  397. if (dma_dir != DMA_NONE) {
  398. scatter = kzalloc(sizeof(*scatter), GFP_KERNEL);
  399. if (!scatter)
  400. goto out;
  401. sg_init_one(scatter, buffer, size);
  402. num_scatter = 1;
  403. }
  404. task->task_proto = task->dev->tproto;
  405. task->scatter = scatter;
  406. task->num_scatter = num_scatter;
  407. task->total_xfer_len = size;
  408. task->data_dir = dma_dir;
  409. task->task_done = sas_disc_task_done;
  410. if (dma_dir != DMA_NONE &&
  411. sas_protocol_ata(task->task_proto)) {
  412. task->num_scatter = dma_map_sg(task->dev->port->ha->dev,
  413. task->scatter,
  414. task->num_scatter,
  415. task->data_dir);
  416. }
  417. for (retries = 0; retries < 5; retries++) {
  418. task->task_state_flags = SAS_TASK_STATE_PENDING;
  419. init_completion(&task->completion);
  420. task->timer.data = (unsigned long) task;
  421. task->timer.function = sas_task_timedout;
  422. task->timer.expires = jiffies + SAS_DEV_TIMEOUT*HZ;
  423. add_timer(&task->timer);
  424. res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
  425. if (res) {
  426. del_timer(&task->timer);
  427. SAS_DPRINTK("executing SAS discovery task failed:%d\n",
  428. res);
  429. goto ex_err;
  430. }
  431. wait_for_completion(&task->completion);
  432. res = -ECOMM;
  433. if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
  434. int res2;
  435. SAS_DPRINTK("task aborted, flags:0x%x\n",
  436. task->task_state_flags);
  437. res2 = i->dft->lldd_abort_task(task);
  438. SAS_DPRINTK("came back from abort task\n");
  439. if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
  440. if (res2 == TMF_RESP_FUNC_COMPLETE)
  441. continue; /* Retry the task */
  442. else
  443. goto ex_err;
  444. }
  445. }
  446. if (task->task_status.stat == SAM_BUSY ||
  447. task->task_status.stat == SAM_TASK_SET_FULL ||
  448. task->task_status.stat == SAS_QUEUE_FULL) {
  449. SAS_DPRINTK("task: q busy, sleeping...\n");
  450. schedule_timeout_interruptible(HZ);
  451. } else if (task->task_status.stat == SAM_CHECK_COND) {
  452. struct scsi_sense_hdr shdr;
  453. if (!scsi_normalize_sense(ts->buf, ts->buf_valid_size,
  454. &shdr)) {
  455. SAS_DPRINTK("couldn't normalize sense\n");
  456. continue;
  457. }
  458. if ((shdr.sense_key == 6 && shdr.asc == 0x29) ||
  459. (shdr.sense_key == 2 && shdr.asc == 4 &&
  460. shdr.ascq == 1)) {
  461. SAS_DPRINTK("device %016llx LUN: %016llx "
  462. "powering up or not ready yet, "
  463. "sleeping...\n",
  464. SAS_ADDR(task->dev->sas_addr),
  465. SAS_ADDR(task->ssp_task.LUN));
  466. schedule_timeout_interruptible(5*HZ);
  467. } else if (shdr.sense_key == 1) {
  468. res = 0;
  469. break;
  470. } else if (shdr.sense_key == 5) {
  471. break;
  472. } else {
  473. SAS_DPRINTK("dev %016llx LUN: %016llx "
  474. "sense key:0x%x ASC:0x%x ASCQ:0x%x"
  475. "\n",
  476. SAS_ADDR(task->dev->sas_addr),
  477. SAS_ADDR(task->ssp_task.LUN),
  478. shdr.sense_key,
  479. shdr.asc, shdr.ascq);
  480. }
  481. } else if (task->task_status.resp != SAS_TASK_COMPLETE ||
  482. task->task_status.stat != SAM_GOOD) {
  483. SAS_DPRINTK("task finished with resp:0x%x, "
  484. "stat:0x%x\n",
  485. task->task_status.resp,
  486. task->task_status.stat);
  487. goto ex_err;
  488. } else {
  489. res = 0;
  490. break;
  491. }
  492. }
  493. ex_err:
  494. if (dma_dir != DMA_NONE) {
  495. if (sas_protocol_ata(task->task_proto))
  496. dma_unmap_sg(task->dev->port->ha->dev,
  497. task->scatter, task->num_scatter,
  498. task->data_dir);
  499. kfree(scatter);
  500. }
  501. out:
  502. return res;
  503. }
  504. /* ---------- SATA ---------- */
  505. static void sas_get_ata_command_set(struct domain_device *dev)
  506. {
  507. struct dev_to_host_fis *fis =
  508. (struct dev_to_host_fis *) dev->frame_rcvd;
  509. if ((fis->sector_count == 1 && /* ATA */
  510. fis->lbal == 1 &&
  511. fis->lbam == 0 &&
  512. fis->lbah == 0 &&
  513. fis->device == 0)
  514. ||
  515. (fis->sector_count == 0 && /* CE-ATA (mATA) */
  516. fis->lbal == 0 &&
  517. fis->lbam == 0xCE &&
  518. fis->lbah == 0xAA &&
  519. (fis->device & ~0x10) == 0))
  520. dev->sata_dev.command_set = ATA_COMMAND_SET;
  521. else if ((fis->interrupt_reason == 1 && /* ATAPI */
  522. fis->lbal == 1 &&
  523. fis->byte_count_low == 0x14 &&
  524. fis->byte_count_high == 0xEB &&
  525. (fis->device & ~0x10) == 0))
  526. dev->sata_dev.command_set = ATAPI_COMMAND_SET;
  527. else if ((fis->sector_count == 1 && /* SEMB */
  528. fis->lbal == 1 &&
  529. fis->lbam == 0x3C &&
  530. fis->lbah == 0xC3 &&
  531. fis->device == 0)
  532. ||
  533. (fis->interrupt_reason == 1 && /* SATA PM */
  534. fis->lbal == 1 &&
  535. fis->byte_count_low == 0x69 &&
  536. fis->byte_count_high == 0x96 &&
  537. (fis->device & ~0x10) == 0))
  538. /* Treat it as a superset? */
  539. dev->sata_dev.command_set = ATAPI_COMMAND_SET;
  540. }
  541. /**
  542. * sas_issue_ata_cmd -- Basic SATA command processing for discovery
  543. * @dev: the device to send the command to
  544. * @command: the command register
  545. * @features: the features register
  546. * @buffer: pointer to buffer to do I/O
  547. * @size: size of @buffer
  548. * @dma_dir: DMA direction. DMA_xxx
  549. */
  550. static int sas_issue_ata_cmd(struct domain_device *dev, u8 command,
  551. u8 features, void *buffer, int size,
  552. enum dma_data_direction dma_dir)
  553. {
  554. int res = 0;
  555. struct sas_task *task;
  556. struct dev_to_host_fis *d2h_fis = (struct dev_to_host_fis *)
  557. &dev->frame_rcvd[0];
  558. res = -ENOMEM;
  559. task = sas_alloc_task(GFP_KERNEL);
  560. if (!task)
  561. goto out;
  562. task->dev = dev;
  563. task->ata_task.fis.fis_type = 0x27;
  564. task->ata_task.fis.command = command;
  565. task->ata_task.fis.features = features;
  566. task->ata_task.fis.device = d2h_fis->device;
  567. task->ata_task.retry_count = 1;
  568. res = sas_execute_task(task, buffer, size, dma_dir);
  569. sas_free_task(task);
  570. out:
  571. return res;
  572. }
  573. #define ATA_IDENTIFY_DEV 0xEC
  574. #define ATA_IDENTIFY_PACKET_DEV 0xA1
  575. #define ATA_SET_FEATURES 0xEF
  576. #define ATA_FEATURE_PUP_STBY_SPIN_UP 0x07
  577. /**
  578. * sas_discover_sata_dev -- discover a STP/SATA device (SATA_DEV)
  579. * @dev: STP/SATA device of interest (ATA/ATAPI)
  580. *
  581. * The LLDD has already been notified of this device, so that we can
  582. * send FISes to it. Here we try to get IDENTIFY DEVICE or IDENTIFY
  583. * PACKET DEVICE, if ATAPI device, so that the LLDD can fine-tune its
  584. * performance for this device.
  585. */
  586. static int sas_discover_sata_dev(struct domain_device *dev)
  587. {
  588. int res;
  589. __le16 *identify_x;
  590. u8 command;
  591. identify_x = kzalloc(512, GFP_KERNEL);
  592. if (!identify_x)
  593. return -ENOMEM;
  594. if (dev->sata_dev.command_set == ATA_COMMAND_SET) {
  595. dev->sata_dev.identify_device = identify_x;
  596. command = ATA_IDENTIFY_DEV;
  597. } else {
  598. dev->sata_dev.identify_packet_device = identify_x;
  599. command = ATA_IDENTIFY_PACKET_DEV;
  600. }
  601. res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
  602. DMA_FROM_DEVICE);
  603. if (res)
  604. goto out_err;
  605. /* lives on the media? */
  606. if (le16_to_cpu(identify_x[0]) & 4) {
  607. /* incomplete response */
  608. SAS_DPRINTK("sending SET FEATURE/PUP_STBY_SPIN_UP to "
  609. "dev %llx\n", SAS_ADDR(dev->sas_addr));
  610. if (!(identify_x[83] & cpu_to_le16(1<<6)))
  611. goto cont1;
  612. res = sas_issue_ata_cmd(dev, ATA_SET_FEATURES,
  613. ATA_FEATURE_PUP_STBY_SPIN_UP,
  614. NULL, 0, DMA_NONE);
  615. if (res)
  616. goto cont1;
  617. schedule_timeout_interruptible(5*HZ); /* More time? */
  618. res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
  619. DMA_FROM_DEVICE);
  620. if (res)
  621. goto out_err;
  622. }
  623. cont1:
  624. /* XXX Hint: register this SATA device with SATL.
  625. When this returns, dev->sata_dev->lu is alive and
  626. present.
  627. sas_satl_register_dev(dev);
  628. */
  629. sas_fill_in_rphy(dev, dev->rphy);
  630. return 0;
  631. out_err:
  632. dev->sata_dev.identify_packet_device = NULL;
  633. dev->sata_dev.identify_device = NULL;
  634. kfree(identify_x);
  635. return res;
  636. }
  637. static int sas_discover_sata_pm(struct domain_device *dev)
  638. {
  639. return -ENODEV;
  640. }
  641. /**
  642. * sas_discover_sata -- discover an STP/SATA domain device
  643. * @dev: pointer to struct domain_device of interest
  644. *
  645. * First we notify the LLDD of this device, so we can send frames to
  646. * it. Then depending on the type of device we call the appropriate
  647. * discover functions. Once device discover is done, we notify the
  648. * LLDD so that it can fine-tune its parameters for the device, by
  649. * removing it and then adding it. That is, the second time around,
  650. * the driver would have certain fields, that it is looking at, set.
  651. * Finally we initialize the kobj so that the device can be added to
  652. * the system at registration time. Devices directly attached to a HA
  653. * port, have no parents. All other devices do, and should have their
  654. * "parent" pointer set appropriately before calling this function.
  655. */
  656. int sas_discover_sata(struct domain_device *dev)
  657. {
  658. int res;
  659. sas_get_ata_command_set(dev);
  660. res = sas_notify_lldd_dev_found(dev);
  661. if (res)
  662. return res;
  663. switch (dev->dev_type) {
  664. case SATA_DEV:
  665. res = sas_discover_sata_dev(dev);
  666. break;
  667. case SATA_PM:
  668. res = sas_discover_sata_pm(dev);
  669. break;
  670. default:
  671. break;
  672. }
  673. sas_notify_lldd_dev_gone(dev);
  674. if (!res) {
  675. sas_notify_lldd_dev_found(dev);
  676. res = sas_rphy_add(dev->rphy);
  677. }
  678. return res;
  679. }