sas_ata.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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 num = 0;
  142. unsigned int xfer = 0;
  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 (is_atapi_taskfile(&qc->tf)) {
  157. memcpy(task->ata_task.atapi_packet, qc->cdb, qc->dev->cdb_len);
  158. task->total_xfer_len = qc->nbytes + qc->pad_len;
  159. task->num_scatter = qc->pad_len ? qc->n_elem + 1 : qc->n_elem;
  160. } else {
  161. ata_for_each_sg(sg, qc) {
  162. num++;
  163. xfer += sg->length;
  164. }
  165. task->total_xfer_len = xfer;
  166. task->num_scatter = num;
  167. }
  168. task->data_dir = qc->dma_dir;
  169. task->scatter = qc->__sg;
  170. task->ata_task.retry_count = 1;
  171. task->task_state_flags = SAS_TASK_STATE_PENDING;
  172. qc->lldd_task = task;
  173. switch (qc->tf.protocol) {
  174. case ATA_PROT_NCQ:
  175. task->ata_task.use_ncq = 1;
  176. /* fall through */
  177. case ATA_PROT_ATAPI_DMA:
  178. case ATA_PROT_DMA:
  179. task->ata_task.dma_xfer = 1;
  180. break;
  181. }
  182. if (qc->scsicmd)
  183. ASSIGN_SAS_TASK(qc->scsicmd, task);
  184. if (sas_ha->lldd_max_execute_num < 2)
  185. res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
  186. else
  187. res = sas_queue_up(task);
  188. /* Examine */
  189. if (res) {
  190. SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
  191. if (qc->scsicmd)
  192. ASSIGN_SAS_TASK(qc->scsicmd, NULL);
  193. sas_free_task(task);
  194. return AC_ERR_SYSTEM;
  195. }
  196. return 0;
  197. }
  198. static u8 sas_ata_check_status(struct ata_port *ap)
  199. {
  200. struct domain_device *dev = ap->private_data;
  201. return dev->sata_dev.tf.command;
  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 = 0;
  209. if (i->dft->lldd_I_T_nexus_reset)
  210. res = i->dft->lldd_I_T_nexus_reset(dev);
  211. if (res)
  212. SAS_DPRINTK("%s: Unable to reset I T nexus?\n", __FUNCTION__);
  213. switch (dev->sata_dev.command_set) {
  214. case ATA_COMMAND_SET:
  215. SAS_DPRINTK("%s: Found ATA device.\n", __FUNCTION__);
  216. ap->device[0].class = ATA_DEV_ATA;
  217. break;
  218. case ATAPI_COMMAND_SET:
  219. SAS_DPRINTK("%s: Found ATAPI device.\n", __FUNCTION__);
  220. ap->device[0].class = ATA_DEV_ATAPI;
  221. break;
  222. default:
  223. SAS_DPRINTK("%s: Unknown SATA command set: %d.\n",
  224. __FUNCTION__,
  225. dev->sata_dev.command_set);
  226. ap->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 void sas_ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  256. {
  257. struct domain_device *dev = ap->private_data;
  258. memcpy(tf, &dev->sata_dev.tf, sizeof (*tf));
  259. }
  260. static int sas_ata_scr_write(struct ata_port *ap, unsigned int sc_reg_in,
  261. u32 val)
  262. {
  263. struct domain_device *dev = ap->private_data;
  264. SAS_DPRINTK("STUB %s\n", __FUNCTION__);
  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->sactive = val;
  277. break;
  278. default:
  279. return -EINVAL;
  280. }
  281. return 0;
  282. }
  283. static int sas_ata_scr_read(struct ata_port *ap, unsigned int sc_reg_in,
  284. u32 *val)
  285. {
  286. struct domain_device *dev = ap->private_data;
  287. SAS_DPRINTK("STUB %s\n", __FUNCTION__);
  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->sactive;
  300. return 0;
  301. default:
  302. return -EINVAL;
  303. }
  304. }
  305. static struct ata_port_operations sas_sata_ops = {
  306. .port_disable = ata_port_disable,
  307. .check_status = sas_ata_check_status,
  308. .check_altstatus = sas_ata_check_status,
  309. .dev_select = ata_noop_dev_select,
  310. .phy_reset = sas_ata_phy_reset,
  311. .post_internal_cmd = sas_ata_post_internal,
  312. .tf_read = sas_ata_tf_read,
  313. .qc_prep = ata_noop_qc_prep,
  314. .qc_issue = sas_ata_qc_issue,
  315. .port_start = ata_sas_port_start,
  316. .port_stop = ata_sas_port_stop,
  317. .scr_read = sas_ata_scr_read,
  318. .scr_write = sas_ata_scr_write
  319. };
  320. static struct ata_port_info sata_port_info = {
  321. .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY | ATA_FLAG_SATA_RESET |
  322. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA | ATA_FLAG_NCQ,
  323. .pio_mask = 0x1f, /* PIO0-4 */
  324. .mwdma_mask = 0x07, /* MWDMA0-2 */
  325. .udma_mask = ATA_UDMA6,
  326. .port_ops = &sas_sata_ops
  327. };
  328. int sas_ata_init_host_and_port(struct domain_device *found_dev,
  329. struct scsi_target *starget)
  330. {
  331. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  332. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  333. struct ata_port *ap;
  334. ata_host_init(&found_dev->sata_dev.ata_host,
  335. ha->dev,
  336. sata_port_info.flags,
  337. &sas_sata_ops);
  338. ap = ata_sas_port_alloc(&found_dev->sata_dev.ata_host,
  339. &sata_port_info,
  340. shost);
  341. if (!ap) {
  342. SAS_DPRINTK("ata_sas_port_alloc failed.\n");
  343. return -ENODEV;
  344. }
  345. ap->private_data = found_dev;
  346. ap->cbl = ATA_CBL_SATA;
  347. ap->scsi_host = shost;
  348. found_dev->sata_dev.ap = ap;
  349. return 0;
  350. }
  351. void sas_ata_task_abort(struct sas_task *task)
  352. {
  353. struct ata_queued_cmd *qc = task->uldd_task;
  354. struct completion *waiting;
  355. /* Bounce SCSI-initiated commands to the SCSI EH */
  356. if (qc->scsicmd) {
  357. scsi_req_abort_cmd(qc->scsicmd);
  358. scsi_schedule_eh(qc->scsicmd->device->host);
  359. return;
  360. }
  361. /* Internal command, fake a timeout and complete. */
  362. qc->flags &= ~ATA_QCFLAG_ACTIVE;
  363. qc->flags |= ATA_QCFLAG_FAILED;
  364. qc->err_mask |= AC_ERR_TIMEOUT;
  365. waiting = qc->private_data;
  366. complete(waiting);
  367. }
  368. static void sas_task_timedout(unsigned long _task)
  369. {
  370. struct sas_task *task = (void *) _task;
  371. unsigned long flags;
  372. spin_lock_irqsave(&task->task_state_lock, flags);
  373. if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
  374. task->task_state_flags |= SAS_TASK_STATE_ABORTED;
  375. spin_unlock_irqrestore(&task->task_state_lock, flags);
  376. complete(&task->completion);
  377. }
  378. static void sas_disc_task_done(struct sas_task *task)
  379. {
  380. if (!del_timer(&task->timer))
  381. return;
  382. complete(&task->completion);
  383. }
  384. #define SAS_DEV_TIMEOUT 10
  385. /**
  386. * sas_execute_task -- Basic task processing for discovery
  387. * @task: the task to be executed
  388. * @buffer: pointer to buffer to do I/O
  389. * @size: size of @buffer
  390. * @dma_dir: DMA direction. DMA_xxx
  391. */
  392. static int sas_execute_task(struct sas_task *task, void *buffer, int size,
  393. enum dma_data_direction dma_dir)
  394. {
  395. int res = 0;
  396. struct scatterlist *scatter = NULL;
  397. struct task_status_struct *ts = &task->task_status;
  398. int num_scatter = 0;
  399. int retries = 0;
  400. struct sas_internal *i =
  401. to_sas_internal(task->dev->port->ha->core.shost->transportt);
  402. if (dma_dir != DMA_NONE) {
  403. scatter = kzalloc(sizeof(*scatter), GFP_KERNEL);
  404. if (!scatter)
  405. goto out;
  406. sg_init_one(scatter, buffer, size);
  407. num_scatter = 1;
  408. }
  409. task->task_proto = task->dev->tproto;
  410. task->scatter = scatter;
  411. task->num_scatter = num_scatter;
  412. task->total_xfer_len = size;
  413. task->data_dir = dma_dir;
  414. task->task_done = sas_disc_task_done;
  415. if (dma_dir != DMA_NONE &&
  416. sas_protocol_ata(task->task_proto)) {
  417. task->num_scatter = dma_map_sg(task->dev->port->ha->dev,
  418. task->scatter,
  419. task->num_scatter,
  420. task->data_dir);
  421. }
  422. for (retries = 0; retries < 5; retries++) {
  423. task->task_state_flags = SAS_TASK_STATE_PENDING;
  424. init_completion(&task->completion);
  425. task->timer.data = (unsigned long) task;
  426. task->timer.function = sas_task_timedout;
  427. task->timer.expires = jiffies + SAS_DEV_TIMEOUT*HZ;
  428. add_timer(&task->timer);
  429. res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
  430. if (res) {
  431. del_timer(&task->timer);
  432. SAS_DPRINTK("executing SAS discovery task failed:%d\n",
  433. res);
  434. goto ex_err;
  435. }
  436. wait_for_completion(&task->completion);
  437. res = -ETASK;
  438. if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
  439. int res2;
  440. SAS_DPRINTK("task aborted, flags:0x%x\n",
  441. task->task_state_flags);
  442. res2 = i->dft->lldd_abort_task(task);
  443. SAS_DPRINTK("came back from abort task\n");
  444. if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
  445. if (res2 == TMF_RESP_FUNC_COMPLETE)
  446. continue; /* Retry the task */
  447. else
  448. goto ex_err;
  449. }
  450. }
  451. if (task->task_status.stat == SAM_BUSY ||
  452. task->task_status.stat == SAM_TASK_SET_FULL ||
  453. task->task_status.stat == SAS_QUEUE_FULL) {
  454. SAS_DPRINTK("task: q busy, sleeping...\n");
  455. schedule_timeout_interruptible(HZ);
  456. } else if (task->task_status.stat == SAM_CHECK_COND) {
  457. struct scsi_sense_hdr shdr;
  458. if (!scsi_normalize_sense(ts->buf, ts->buf_valid_size,
  459. &shdr)) {
  460. SAS_DPRINTK("couldn't normalize sense\n");
  461. continue;
  462. }
  463. if ((shdr.sense_key == 6 && shdr.asc == 0x29) ||
  464. (shdr.sense_key == 2 && shdr.asc == 4 &&
  465. shdr.ascq == 1)) {
  466. SAS_DPRINTK("device %016llx LUN: %016llx "
  467. "powering up or not ready yet, "
  468. "sleeping...\n",
  469. SAS_ADDR(task->dev->sas_addr),
  470. SAS_ADDR(task->ssp_task.LUN));
  471. schedule_timeout_interruptible(5*HZ);
  472. } else if (shdr.sense_key == 1) {
  473. res = 0;
  474. break;
  475. } else if (shdr.sense_key == 5) {
  476. break;
  477. } else {
  478. SAS_DPRINTK("dev %016llx LUN: %016llx "
  479. "sense key:0x%x ASC:0x%x ASCQ:0x%x"
  480. "\n",
  481. SAS_ADDR(task->dev->sas_addr),
  482. SAS_ADDR(task->ssp_task.LUN),
  483. shdr.sense_key,
  484. shdr.asc, shdr.ascq);
  485. }
  486. } else if (task->task_status.resp != SAS_TASK_COMPLETE ||
  487. task->task_status.stat != SAM_GOOD) {
  488. SAS_DPRINTK("task finished with resp:0x%x, "
  489. "stat:0x%x\n",
  490. task->task_status.resp,
  491. task->task_status.stat);
  492. goto ex_err;
  493. } else {
  494. res = 0;
  495. break;
  496. }
  497. }
  498. ex_err:
  499. if (dma_dir != DMA_NONE) {
  500. if (sas_protocol_ata(task->task_proto))
  501. dma_unmap_sg(task->dev->port->ha->dev,
  502. task->scatter, task->num_scatter,
  503. task->data_dir);
  504. kfree(scatter);
  505. }
  506. out:
  507. return res;
  508. }
  509. /* ---------- SATA ---------- */
  510. static void sas_get_ata_command_set(struct domain_device *dev)
  511. {
  512. struct dev_to_host_fis *fis =
  513. (struct dev_to_host_fis *) dev->frame_rcvd;
  514. if ((fis->sector_count == 1 && /* ATA */
  515. fis->lbal == 1 &&
  516. fis->lbam == 0 &&
  517. fis->lbah == 0 &&
  518. fis->device == 0)
  519. ||
  520. (fis->sector_count == 0 && /* CE-ATA (mATA) */
  521. fis->lbal == 0 &&
  522. fis->lbam == 0xCE &&
  523. fis->lbah == 0xAA &&
  524. (fis->device & ~0x10) == 0))
  525. dev->sata_dev.command_set = ATA_COMMAND_SET;
  526. else if ((fis->interrupt_reason == 1 && /* ATAPI */
  527. fis->lbal == 1 &&
  528. fis->byte_count_low == 0x14 &&
  529. fis->byte_count_high == 0xEB &&
  530. (fis->device & ~0x10) == 0))
  531. dev->sata_dev.command_set = ATAPI_COMMAND_SET;
  532. else if ((fis->sector_count == 1 && /* SEMB */
  533. fis->lbal == 1 &&
  534. fis->lbam == 0x3C &&
  535. fis->lbah == 0xC3 &&
  536. fis->device == 0)
  537. ||
  538. (fis->interrupt_reason == 1 && /* SATA PM */
  539. fis->lbal == 1 &&
  540. fis->byte_count_low == 0x69 &&
  541. fis->byte_count_high == 0x96 &&
  542. (fis->device & ~0x10) == 0))
  543. /* Treat it as a superset? */
  544. dev->sata_dev.command_set = ATAPI_COMMAND_SET;
  545. }
  546. /**
  547. * sas_issue_ata_cmd -- Basic SATA command processing for discovery
  548. * @dev: the device to send the command to
  549. * @command: the command register
  550. * @features: the features register
  551. * @buffer: pointer to buffer to do I/O
  552. * @size: size of @buffer
  553. * @dma_dir: DMA direction. DMA_xxx
  554. */
  555. static int sas_issue_ata_cmd(struct domain_device *dev, u8 command,
  556. u8 features, void *buffer, int size,
  557. enum dma_data_direction dma_dir)
  558. {
  559. int res = 0;
  560. struct sas_task *task;
  561. struct dev_to_host_fis *d2h_fis = (struct dev_to_host_fis *)
  562. &dev->frame_rcvd[0];
  563. res = -ENOMEM;
  564. task = sas_alloc_task(GFP_KERNEL);
  565. if (!task)
  566. goto out;
  567. task->dev = dev;
  568. task->ata_task.fis.fis_type = 0x27;
  569. task->ata_task.fis.command = command;
  570. task->ata_task.fis.features = features;
  571. task->ata_task.fis.device = d2h_fis->device;
  572. task->ata_task.retry_count = 1;
  573. res = sas_execute_task(task, buffer, size, dma_dir);
  574. sas_free_task(task);
  575. out:
  576. return res;
  577. }
  578. static void sas_sata_propagate_sas_addr(struct domain_device *dev)
  579. {
  580. unsigned long flags;
  581. struct asd_sas_port *port = dev->port;
  582. struct asd_sas_phy *phy;
  583. BUG_ON(dev->parent);
  584. memcpy(port->attached_sas_addr, dev->sas_addr, SAS_ADDR_SIZE);
  585. spin_lock_irqsave(&port->phy_list_lock, flags);
  586. list_for_each_entry(phy, &port->phy_list, port_phy_el)
  587. memcpy(phy->attached_sas_addr, dev->sas_addr, SAS_ADDR_SIZE);
  588. spin_unlock_irqrestore(&port->phy_list_lock, flags);
  589. }
  590. #define ATA_IDENTIFY_DEV 0xEC
  591. #define ATA_IDENTIFY_PACKET_DEV 0xA1
  592. #define ATA_SET_FEATURES 0xEF
  593. #define ATA_FEATURE_PUP_STBY_SPIN_UP 0x07
  594. /**
  595. * sas_discover_sata_dev -- discover a STP/SATA device (SATA_DEV)
  596. * @dev: STP/SATA device of interest (ATA/ATAPI)
  597. *
  598. * The LLDD has already been notified of this device, so that we can
  599. * send FISes to it. Here we try to get IDENTIFY DEVICE or IDENTIFY
  600. * PACKET DEVICE, if ATAPI device, so that the LLDD can fine-tune its
  601. * performance for this device.
  602. */
  603. static int sas_discover_sata_dev(struct domain_device *dev)
  604. {
  605. int res;
  606. __le16 *identify_x;
  607. u8 command;
  608. identify_x = kzalloc(512, GFP_KERNEL);
  609. if (!identify_x)
  610. return -ENOMEM;
  611. if (dev->sata_dev.command_set == ATA_COMMAND_SET) {
  612. dev->sata_dev.identify_device = identify_x;
  613. command = ATA_IDENTIFY_DEV;
  614. } else {
  615. dev->sata_dev.identify_packet_device = identify_x;
  616. command = ATA_IDENTIFY_PACKET_DEV;
  617. }
  618. res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
  619. DMA_FROM_DEVICE);
  620. if (res)
  621. goto out_err;
  622. /* lives on the media? */
  623. if (le16_to_cpu(identify_x[0]) & 4) {
  624. /* incomplete response */
  625. SAS_DPRINTK("sending SET FEATURE/PUP_STBY_SPIN_UP to "
  626. "dev %llx\n", SAS_ADDR(dev->sas_addr));
  627. if (!le16_to_cpu(identify_x[83] & (1<<6)))
  628. goto cont1;
  629. res = sas_issue_ata_cmd(dev, ATA_SET_FEATURES,
  630. ATA_FEATURE_PUP_STBY_SPIN_UP,
  631. NULL, 0, DMA_NONE);
  632. if (res)
  633. goto cont1;
  634. schedule_timeout_interruptible(5*HZ); /* More time? */
  635. res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
  636. DMA_FROM_DEVICE);
  637. if (res)
  638. goto out_err;
  639. }
  640. cont1:
  641. /* Get WWN */
  642. if (dev->port->oob_mode != SATA_OOB_MODE) {
  643. memcpy(dev->sas_addr, dev->sata_dev.rps_resp.rps.stp_sas_addr,
  644. SAS_ADDR_SIZE);
  645. } else if (dev->sata_dev.command_set == ATA_COMMAND_SET &&
  646. (le16_to_cpu(dev->sata_dev.identify_device[108]) & 0xF000)
  647. == 0x5000) {
  648. int i;
  649. for (i = 0; i < 4; i++) {
  650. dev->sas_addr[2*i] =
  651. (le16_to_cpu(dev->sata_dev.identify_device[108+i]) & 0xFF00) >> 8;
  652. dev->sas_addr[2*i+1] =
  653. le16_to_cpu(dev->sata_dev.identify_device[108+i]) & 0x00FF;
  654. }
  655. }
  656. sas_hash_addr(dev->hashed_sas_addr, dev->sas_addr);
  657. if (!dev->parent)
  658. sas_sata_propagate_sas_addr(dev);
  659. /* XXX Hint: register this SATA device with SATL.
  660. When this returns, dev->sata_dev->lu is alive and
  661. present.
  662. sas_satl_register_dev(dev);
  663. */
  664. sas_fill_in_rphy(dev, dev->rphy);
  665. return 0;
  666. out_err:
  667. dev->sata_dev.identify_packet_device = NULL;
  668. dev->sata_dev.identify_device = NULL;
  669. kfree(identify_x);
  670. return res;
  671. }
  672. static int sas_discover_sata_pm(struct domain_device *dev)
  673. {
  674. return -ENODEV;
  675. }
  676. /**
  677. * sas_discover_sata -- discover an STP/SATA domain device
  678. * @dev: pointer to struct domain_device of interest
  679. *
  680. * First we notify the LLDD of this device, so we can send frames to
  681. * it. Then depending on the type of device we call the appropriate
  682. * discover functions. Once device discover is done, we notify the
  683. * LLDD so that it can fine-tune its parameters for the device, by
  684. * removing it and then adding it. That is, the second time around,
  685. * the driver would have certain fields, that it is looking at, set.
  686. * Finally we initialize the kobj so that the device can be added to
  687. * the system at registration time. Devices directly attached to a HA
  688. * port, have no parents. All other devices do, and should have their
  689. * "parent" pointer set appropriately before calling this function.
  690. */
  691. int sas_discover_sata(struct domain_device *dev)
  692. {
  693. int res;
  694. sas_get_ata_command_set(dev);
  695. res = sas_notify_lldd_dev_found(dev);
  696. if (res)
  697. return res;
  698. switch (dev->dev_type) {
  699. case SATA_DEV:
  700. res = sas_discover_sata_dev(dev);
  701. break;
  702. case SATA_PM:
  703. res = sas_discover_sata_pm(dev);
  704. break;
  705. default:
  706. break;
  707. }
  708. sas_notify_lldd_dev_gone(dev);
  709. if (!res) {
  710. sas_notify_lldd_dev_found(dev);
  711. res = sas_rphy_add(dev->rphy);
  712. }
  713. return res;
  714. }