sas_ata.c 22 KB

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