sas_ata.c 22 KB

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