libata-sff.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. /*
  2. * libata-sff.c - helper library for PCI IDE BMDMA
  3. *
  4. * Maintained by: Jeff Garzik <jgarzik@pobox.com>
  5. * Please ALWAYS copy linux-ide@vger.kernel.org
  6. * on emails.
  7. *
  8. * Copyright 2003-2006 Red Hat, Inc. All rights reserved.
  9. * Copyright 2003-2006 Jeff Garzik
  10. *
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; see the file COPYING. If not, write to
  24. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. *
  27. * libata documentation is available via 'make {ps|pdf}docs',
  28. * as Documentation/DocBook/libata.*
  29. *
  30. * Hardware documentation available from http://www.t13.org/ and
  31. * http://www.sata-io.org/
  32. *
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/pci.h>
  36. #include <linux/libata.h>
  37. #include "libata.h"
  38. /**
  39. * ata_irq_on - Enable interrupts on a port.
  40. * @ap: Port on which interrupts are enabled.
  41. *
  42. * Enable interrupts on a legacy IDE device using MMIO or PIO,
  43. * wait for idle, clear any pending interrupts.
  44. *
  45. * LOCKING:
  46. * Inherited from caller.
  47. */
  48. u8 ata_irq_on(struct ata_port *ap)
  49. {
  50. struct ata_ioports *ioaddr = &ap->ioaddr;
  51. u8 tmp;
  52. ap->ctl &= ~ATA_NIEN;
  53. ap->last_ctl = ap->ctl;
  54. if (ioaddr->ctl_addr)
  55. iowrite8(ap->ctl, ioaddr->ctl_addr);
  56. tmp = ata_wait_idle(ap);
  57. ap->ops->irq_clear(ap);
  58. return tmp;
  59. }
  60. /**
  61. * ata_tf_load - send taskfile registers to host controller
  62. * @ap: Port to which output is sent
  63. * @tf: ATA taskfile register set
  64. *
  65. * Outputs ATA taskfile to standard ATA host controller.
  66. *
  67. * LOCKING:
  68. * Inherited from caller.
  69. */
  70. void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
  71. {
  72. struct ata_ioports *ioaddr = &ap->ioaddr;
  73. unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
  74. if (tf->ctl != ap->last_ctl) {
  75. if (ioaddr->ctl_addr)
  76. iowrite8(tf->ctl, ioaddr->ctl_addr);
  77. ap->last_ctl = tf->ctl;
  78. ata_wait_idle(ap);
  79. }
  80. if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
  81. WARN_ON(!ioaddr->ctl_addr);
  82. iowrite8(tf->hob_feature, ioaddr->feature_addr);
  83. iowrite8(tf->hob_nsect, ioaddr->nsect_addr);
  84. iowrite8(tf->hob_lbal, ioaddr->lbal_addr);
  85. iowrite8(tf->hob_lbam, ioaddr->lbam_addr);
  86. iowrite8(tf->hob_lbah, ioaddr->lbah_addr);
  87. VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
  88. tf->hob_feature,
  89. tf->hob_nsect,
  90. tf->hob_lbal,
  91. tf->hob_lbam,
  92. tf->hob_lbah);
  93. }
  94. if (is_addr) {
  95. iowrite8(tf->feature, ioaddr->feature_addr);
  96. iowrite8(tf->nsect, ioaddr->nsect_addr);
  97. iowrite8(tf->lbal, ioaddr->lbal_addr);
  98. iowrite8(tf->lbam, ioaddr->lbam_addr);
  99. iowrite8(tf->lbah, ioaddr->lbah_addr);
  100. VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
  101. tf->feature,
  102. tf->nsect,
  103. tf->lbal,
  104. tf->lbam,
  105. tf->lbah);
  106. }
  107. if (tf->flags & ATA_TFLAG_DEVICE) {
  108. iowrite8(tf->device, ioaddr->device_addr);
  109. VPRINTK("device 0x%X\n", tf->device);
  110. }
  111. ata_wait_idle(ap);
  112. }
  113. /**
  114. * ata_exec_command - issue ATA command to host controller
  115. * @ap: port to which command is being issued
  116. * @tf: ATA taskfile register set
  117. *
  118. * Issues ATA command, with proper synchronization with interrupt
  119. * handler / other threads.
  120. *
  121. * LOCKING:
  122. * spin_lock_irqsave(host lock)
  123. */
  124. void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
  125. {
  126. DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command);
  127. iowrite8(tf->command, ap->ioaddr.command_addr);
  128. ata_pause(ap);
  129. }
  130. /**
  131. * ata_tf_read - input device's ATA taskfile shadow registers
  132. * @ap: Port from which input is read
  133. * @tf: ATA taskfile register set for storing input
  134. *
  135. * Reads ATA taskfile registers for currently-selected device
  136. * into @tf. Assumes the device has a fully SFF compliant task file
  137. * layout and behaviour. If you device does not (eg has a different
  138. * status method) then you will need to provide a replacement tf_read
  139. *
  140. * LOCKING:
  141. * Inherited from caller.
  142. */
  143. void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  144. {
  145. struct ata_ioports *ioaddr = &ap->ioaddr;
  146. tf->command = ata_check_status(ap);
  147. tf->feature = ioread8(ioaddr->error_addr);
  148. tf->nsect = ioread8(ioaddr->nsect_addr);
  149. tf->lbal = ioread8(ioaddr->lbal_addr);
  150. tf->lbam = ioread8(ioaddr->lbam_addr);
  151. tf->lbah = ioread8(ioaddr->lbah_addr);
  152. tf->device = ioread8(ioaddr->device_addr);
  153. if (tf->flags & ATA_TFLAG_LBA48) {
  154. if (likely(ioaddr->ctl_addr)) {
  155. iowrite8(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
  156. tf->hob_feature = ioread8(ioaddr->error_addr);
  157. tf->hob_nsect = ioread8(ioaddr->nsect_addr);
  158. tf->hob_lbal = ioread8(ioaddr->lbal_addr);
  159. tf->hob_lbam = ioread8(ioaddr->lbam_addr);
  160. tf->hob_lbah = ioread8(ioaddr->lbah_addr);
  161. iowrite8(tf->ctl, ioaddr->ctl_addr);
  162. ap->last_ctl = tf->ctl;
  163. } else
  164. WARN_ON(1);
  165. }
  166. }
  167. /**
  168. * ata_check_status - Read device status reg & clear interrupt
  169. * @ap: port where the device is
  170. *
  171. * Reads ATA taskfile status register for currently-selected device
  172. * and return its value. This also clears pending interrupts
  173. * from this device
  174. *
  175. * LOCKING:
  176. * Inherited from caller.
  177. */
  178. u8 ata_check_status(struct ata_port *ap)
  179. {
  180. return ioread8(ap->ioaddr.status_addr);
  181. }
  182. /**
  183. * ata_altstatus - Read device alternate status reg
  184. * @ap: port where the device is
  185. *
  186. * Reads ATA taskfile alternate status register for
  187. * currently-selected device and return its value.
  188. *
  189. * Note: may NOT be used as the check_altstatus() entry in
  190. * ata_port_operations.
  191. *
  192. * LOCKING:
  193. * Inherited from caller.
  194. */
  195. u8 ata_altstatus(struct ata_port *ap)
  196. {
  197. if (ap->ops->check_altstatus)
  198. return ap->ops->check_altstatus(ap);
  199. return ioread8(ap->ioaddr.altstatus_addr);
  200. }
  201. /**
  202. * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
  203. * @qc: Info associated with this ATA transaction.
  204. *
  205. * LOCKING:
  206. * spin_lock_irqsave(host lock)
  207. */
  208. void ata_bmdma_setup(struct ata_queued_cmd *qc)
  209. {
  210. struct ata_port *ap = qc->ap;
  211. unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
  212. u8 dmactl;
  213. /* load PRD table addr. */
  214. mb(); /* make sure PRD table writes are visible to controller */
  215. iowrite32(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
  216. /* specify data direction, triple-check start bit is clear */
  217. dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  218. dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
  219. if (!rw)
  220. dmactl |= ATA_DMA_WR;
  221. iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  222. /* issue r/w command */
  223. ap->ops->exec_command(ap, &qc->tf);
  224. }
  225. /**
  226. * ata_bmdma_start - Start a PCI IDE BMDMA transaction
  227. * @qc: Info associated with this ATA transaction.
  228. *
  229. * LOCKING:
  230. * spin_lock_irqsave(host lock)
  231. */
  232. void ata_bmdma_start(struct ata_queued_cmd *qc)
  233. {
  234. struct ata_port *ap = qc->ap;
  235. u8 dmactl;
  236. /* start host DMA transaction */
  237. dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  238. iowrite8(dmactl | ATA_DMA_START, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  239. /* Strictly, one may wish to issue an ioread8() here, to
  240. * flush the mmio write. However, control also passes
  241. * to the hardware at this point, and it will interrupt
  242. * us when we are to resume control. So, in effect,
  243. * we don't care when the mmio write flushes.
  244. * Further, a read of the DMA status register _immediately_
  245. * following the write may not be what certain flaky hardware
  246. * is expected, so I think it is best to not add a readb()
  247. * without first all the MMIO ATA cards/mobos.
  248. * Or maybe I'm just being paranoid.
  249. *
  250. * FIXME: The posting of this write means I/O starts are
  251. * unneccessarily delayed for MMIO
  252. */
  253. }
  254. /**
  255. * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
  256. * @ap: Port associated with this ATA transaction.
  257. *
  258. * Clear interrupt and error flags in DMA status register.
  259. *
  260. * May be used as the irq_clear() entry in ata_port_operations.
  261. *
  262. * LOCKING:
  263. * spin_lock_irqsave(host lock)
  264. */
  265. void ata_bmdma_irq_clear(struct ata_port *ap)
  266. {
  267. void __iomem *mmio = ap->ioaddr.bmdma_addr;
  268. if (!mmio)
  269. return;
  270. iowrite8(ioread8(mmio + ATA_DMA_STATUS), mmio + ATA_DMA_STATUS);
  271. }
  272. /**
  273. * ata_noop_irq_clear - Noop placeholder for irq_clear
  274. * @ap: Port associated with this ATA transaction.
  275. */
  276. void ata_noop_irq_clear(struct ata_port *ap)
  277. {
  278. }
  279. /**
  280. * ata_bmdma_status - Read PCI IDE BMDMA status
  281. * @ap: Port associated with this ATA transaction.
  282. *
  283. * Read and return BMDMA status register.
  284. *
  285. * May be used as the bmdma_status() entry in ata_port_operations.
  286. *
  287. * LOCKING:
  288. * spin_lock_irqsave(host lock)
  289. */
  290. u8 ata_bmdma_status(struct ata_port *ap)
  291. {
  292. return ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
  293. }
  294. /**
  295. * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
  296. * @qc: Command we are ending DMA for
  297. *
  298. * Clears the ATA_DMA_START flag in the dma control register
  299. *
  300. * May be used as the bmdma_stop() entry in ata_port_operations.
  301. *
  302. * LOCKING:
  303. * spin_lock_irqsave(host lock)
  304. */
  305. void ata_bmdma_stop(struct ata_queued_cmd *qc)
  306. {
  307. struct ata_port *ap = qc->ap;
  308. void __iomem *mmio = ap->ioaddr.bmdma_addr;
  309. /* clear start/stop bit */
  310. iowrite8(ioread8(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
  311. mmio + ATA_DMA_CMD);
  312. /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
  313. ata_altstatus(ap); /* dummy read */
  314. }
  315. /**
  316. * ata_bmdma_freeze - Freeze BMDMA controller port
  317. * @ap: port to freeze
  318. *
  319. * Freeze BMDMA controller port.
  320. *
  321. * LOCKING:
  322. * Inherited from caller.
  323. */
  324. void ata_bmdma_freeze(struct ata_port *ap)
  325. {
  326. struct ata_ioports *ioaddr = &ap->ioaddr;
  327. ap->ctl |= ATA_NIEN;
  328. ap->last_ctl = ap->ctl;
  329. if (ioaddr->ctl_addr)
  330. iowrite8(ap->ctl, ioaddr->ctl_addr);
  331. /* Under certain circumstances, some controllers raise IRQ on
  332. * ATA_NIEN manipulation. Also, many controllers fail to mask
  333. * previously pending IRQ on ATA_NIEN assertion. Clear it.
  334. */
  335. ata_chk_status(ap);
  336. ap->ops->irq_clear(ap);
  337. }
  338. /**
  339. * ata_bmdma_thaw - Thaw BMDMA controller port
  340. * @ap: port to thaw
  341. *
  342. * Thaw BMDMA controller port.
  343. *
  344. * LOCKING:
  345. * Inherited from caller.
  346. */
  347. void ata_bmdma_thaw(struct ata_port *ap)
  348. {
  349. /* clear & re-enable interrupts */
  350. ata_chk_status(ap);
  351. ap->ops->irq_clear(ap);
  352. ap->ops->irq_on(ap);
  353. }
  354. /**
  355. * ata_bmdma_drive_eh - Perform EH with given methods for BMDMA controller
  356. * @ap: port to handle error for
  357. * @prereset: prereset method (can be NULL)
  358. * @softreset: softreset method (can be NULL)
  359. * @hardreset: hardreset method (can be NULL)
  360. * @postreset: postreset method (can be NULL)
  361. *
  362. * Handle error for ATA BMDMA controller. It can handle both
  363. * PATA and SATA controllers. Many controllers should be able to
  364. * use this EH as-is or with some added handling before and
  365. * after.
  366. *
  367. * This function is intended to be used for constructing
  368. * ->error_handler callback by low level drivers.
  369. *
  370. * LOCKING:
  371. * Kernel thread context (may sleep)
  372. */
  373. void ata_bmdma_drive_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
  374. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  375. ata_postreset_fn_t postreset)
  376. {
  377. struct ata_queued_cmd *qc;
  378. unsigned long flags;
  379. int thaw = 0;
  380. qc = __ata_qc_from_tag(ap, ap->link.active_tag);
  381. if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
  382. qc = NULL;
  383. /* reset PIO HSM and stop DMA engine */
  384. spin_lock_irqsave(ap->lock, flags);
  385. ap->hsm_task_state = HSM_ST_IDLE;
  386. if (qc && (qc->tf.protocol == ATA_PROT_DMA ||
  387. qc->tf.protocol == ATAPI_PROT_DMA)) {
  388. u8 host_stat;
  389. host_stat = ap->ops->bmdma_status(ap);
  390. /* BMDMA controllers indicate host bus error by
  391. * setting DMA_ERR bit and timing out. As it wasn't
  392. * really a timeout event, adjust error mask and
  393. * cancel frozen state.
  394. */
  395. if (qc->err_mask == AC_ERR_TIMEOUT && (host_stat & ATA_DMA_ERR)) {
  396. qc->err_mask = AC_ERR_HOST_BUS;
  397. thaw = 1;
  398. }
  399. ap->ops->bmdma_stop(qc);
  400. }
  401. ata_altstatus(ap);
  402. ata_chk_status(ap);
  403. ap->ops->irq_clear(ap);
  404. spin_unlock_irqrestore(ap->lock, flags);
  405. if (thaw)
  406. ata_eh_thaw_port(ap);
  407. /* PIO and DMA engines have been stopped, perform recovery */
  408. ata_do_eh(ap, prereset, softreset, hardreset, postreset);
  409. }
  410. /**
  411. * ata_bmdma_error_handler - Stock error handler for BMDMA controller
  412. * @ap: port to handle error for
  413. *
  414. * Stock error handler for BMDMA controller.
  415. *
  416. * LOCKING:
  417. * Kernel thread context (may sleep)
  418. */
  419. void ata_bmdma_error_handler(struct ata_port *ap)
  420. {
  421. ata_reset_fn_t softreset = NULL, hardreset = NULL;
  422. if (ap->ioaddr.ctl_addr)
  423. softreset = ata_std_softreset;
  424. if (sata_scr_valid(&ap->link))
  425. hardreset = sata_std_hardreset;
  426. ata_bmdma_drive_eh(ap, ata_std_prereset, softreset, hardreset,
  427. ata_std_postreset);
  428. }
  429. /**
  430. * ata_bmdma_post_internal_cmd - Stock post_internal_cmd for
  431. * BMDMA controller
  432. * @qc: internal command to clean up
  433. *
  434. * LOCKING:
  435. * Kernel thread context (may sleep)
  436. */
  437. void ata_bmdma_post_internal_cmd(struct ata_queued_cmd *qc)
  438. {
  439. if (qc->ap->ioaddr.bmdma_addr)
  440. ata_bmdma_stop(qc);
  441. }
  442. /**
  443. * ata_sff_port_start - Set port up for dma.
  444. * @ap: Port to initialize
  445. *
  446. * Called just after data structures for each port are
  447. * initialized. Allocates space for PRD table if the device
  448. * is DMA capable SFF.
  449. *
  450. * May be used as the port_start() entry in ata_port_operations.
  451. *
  452. * LOCKING:
  453. * Inherited from caller.
  454. */
  455. int ata_sff_port_start(struct ata_port *ap)
  456. {
  457. if (ap->ioaddr.bmdma_addr)
  458. return ata_port_start(ap);
  459. return 0;
  460. }
  461. #ifdef CONFIG_PCI
  462. static int ata_resources_present(struct pci_dev *pdev, int port)
  463. {
  464. int i;
  465. /* Check the PCI resources for this channel are enabled */
  466. port = port * 2;
  467. for (i = 0; i < 2; i ++) {
  468. if (pci_resource_start(pdev, port + i) == 0 ||
  469. pci_resource_len(pdev, port + i) == 0)
  470. return 0;
  471. }
  472. return 1;
  473. }
  474. /**
  475. * ata_pci_init_bmdma - acquire PCI BMDMA resources and init ATA host
  476. * @host: target ATA host
  477. *
  478. * Acquire PCI BMDMA resources and initialize @host accordingly.
  479. *
  480. * LOCKING:
  481. * Inherited from calling layer (may sleep).
  482. *
  483. * RETURNS:
  484. * 0 on success, -errno otherwise.
  485. */
  486. int ata_pci_init_bmdma(struct ata_host *host)
  487. {
  488. struct device *gdev = host->dev;
  489. struct pci_dev *pdev = to_pci_dev(gdev);
  490. int i, rc;
  491. /* No BAR4 allocation: No DMA */
  492. if (pci_resource_start(pdev, 4) == 0)
  493. return 0;
  494. /* TODO: If we get no DMA mask we should fall back to PIO */
  495. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  496. if (rc)
  497. return rc;
  498. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  499. if (rc)
  500. return rc;
  501. /* request and iomap DMA region */
  502. rc = pcim_iomap_regions(pdev, 1 << 4, dev_driver_string(gdev));
  503. if (rc) {
  504. dev_printk(KERN_ERR, gdev, "failed to request/iomap BAR4\n");
  505. return -ENOMEM;
  506. }
  507. host->iomap = pcim_iomap_table(pdev);
  508. for (i = 0; i < 2; i++) {
  509. struct ata_port *ap = host->ports[i];
  510. void __iomem *bmdma = host->iomap[4] + 8 * i;
  511. if (ata_port_is_dummy(ap))
  512. continue;
  513. ap->ioaddr.bmdma_addr = bmdma;
  514. if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) &&
  515. (ioread8(bmdma + 2) & 0x80))
  516. host->flags |= ATA_HOST_SIMPLEX;
  517. ata_port_desc(ap, "bmdma 0x%llx",
  518. (unsigned long long)pci_resource_start(pdev, 4) + 8 * i);
  519. }
  520. return 0;
  521. }
  522. /**
  523. * ata_pci_init_sff_host - acquire native PCI ATA resources and init host
  524. * @host: target ATA host
  525. *
  526. * Acquire native PCI ATA resources for @host and initialize the
  527. * first two ports of @host accordingly. Ports marked dummy are
  528. * skipped and allocation failure makes the port dummy.
  529. *
  530. * Note that native PCI resources are valid even for legacy hosts
  531. * as we fix up pdev resources array early in boot, so this
  532. * function can be used for both native and legacy SFF hosts.
  533. *
  534. * LOCKING:
  535. * Inherited from calling layer (may sleep).
  536. *
  537. * RETURNS:
  538. * 0 if at least one port is initialized, -ENODEV if no port is
  539. * available.
  540. */
  541. int ata_pci_init_sff_host(struct ata_host *host)
  542. {
  543. struct device *gdev = host->dev;
  544. struct pci_dev *pdev = to_pci_dev(gdev);
  545. unsigned int mask = 0;
  546. int i, rc;
  547. /* request, iomap BARs and init port addresses accordingly */
  548. for (i = 0; i < 2; i++) {
  549. struct ata_port *ap = host->ports[i];
  550. int base = i * 2;
  551. void __iomem * const *iomap;
  552. if (ata_port_is_dummy(ap))
  553. continue;
  554. /* Discard disabled ports. Some controllers show
  555. * their unused channels this way. Disabled ports are
  556. * made dummy.
  557. */
  558. if (!ata_resources_present(pdev, i)) {
  559. ap->ops = &ata_dummy_port_ops;
  560. continue;
  561. }
  562. rc = pcim_iomap_regions(pdev, 0x3 << base,
  563. dev_driver_string(gdev));
  564. if (rc) {
  565. dev_printk(KERN_WARNING, gdev,
  566. "failed to request/iomap BARs for port %d "
  567. "(errno=%d)\n", i, rc);
  568. if (rc == -EBUSY)
  569. pcim_pin_device(pdev);
  570. ap->ops = &ata_dummy_port_ops;
  571. continue;
  572. }
  573. host->iomap = iomap = pcim_iomap_table(pdev);
  574. ap->ioaddr.cmd_addr = iomap[base];
  575. ap->ioaddr.altstatus_addr =
  576. ap->ioaddr.ctl_addr = (void __iomem *)
  577. ((unsigned long)iomap[base + 1] | ATA_PCI_CTL_OFS);
  578. ata_std_ports(&ap->ioaddr);
  579. ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
  580. (unsigned long long)pci_resource_start(pdev, base),
  581. (unsigned long long)pci_resource_start(pdev, base + 1));
  582. mask |= 1 << i;
  583. }
  584. if (!mask) {
  585. dev_printk(KERN_ERR, gdev, "no available native port\n");
  586. return -ENODEV;
  587. }
  588. return 0;
  589. }
  590. /**
  591. * ata_pci_prepare_sff_host - helper to prepare native PCI ATA host
  592. * @pdev: target PCI device
  593. * @ppi: array of port_info, must be enough for two ports
  594. * @r_host: out argument for the initialized ATA host
  595. *
  596. * Helper to allocate ATA host for @pdev, acquire all native PCI
  597. * resources and initialize it accordingly in one go.
  598. *
  599. * LOCKING:
  600. * Inherited from calling layer (may sleep).
  601. *
  602. * RETURNS:
  603. * 0 on success, -errno otherwise.
  604. */
  605. int ata_pci_prepare_sff_host(struct pci_dev *pdev,
  606. const struct ata_port_info * const * ppi,
  607. struct ata_host **r_host)
  608. {
  609. struct ata_host *host;
  610. int rc;
  611. if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL))
  612. return -ENOMEM;
  613. host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2);
  614. if (!host) {
  615. dev_printk(KERN_ERR, &pdev->dev,
  616. "failed to allocate ATA host\n");
  617. rc = -ENOMEM;
  618. goto err_out;
  619. }
  620. rc = ata_pci_init_sff_host(host);
  621. if (rc)
  622. goto err_out;
  623. /* init DMA related stuff */
  624. rc = ata_pci_init_bmdma(host);
  625. if (rc)
  626. goto err_bmdma;
  627. devres_remove_group(&pdev->dev, NULL);
  628. *r_host = host;
  629. return 0;
  630. err_bmdma:
  631. /* This is necessary because PCI and iomap resources are
  632. * merged and releasing the top group won't release the
  633. * acquired resources if some of those have been acquired
  634. * before entering this function.
  635. */
  636. pcim_iounmap_regions(pdev, 0xf);
  637. err_out:
  638. devres_release_group(&pdev->dev, NULL);
  639. return rc;
  640. }
  641. /**
  642. * ata_pci_activate_sff_host - start SFF host, request IRQ and register it
  643. * @host: target SFF ATA host
  644. * @irq_handler: irq_handler used when requesting IRQ(s)
  645. * @sht: scsi_host_template to use when registering the host
  646. *
  647. * This is the counterpart of ata_host_activate() for SFF ATA
  648. * hosts. This separate helper is necessary because SFF hosts
  649. * use two separate interrupts in legacy mode.
  650. *
  651. * LOCKING:
  652. * Inherited from calling layer (may sleep).
  653. *
  654. * RETURNS:
  655. * 0 on success, -errno otherwise.
  656. */
  657. int ata_pci_activate_sff_host(struct ata_host *host,
  658. irq_handler_t irq_handler,
  659. struct scsi_host_template *sht)
  660. {
  661. struct device *dev = host->dev;
  662. struct pci_dev *pdev = to_pci_dev(dev);
  663. const char *drv_name = dev_driver_string(host->dev);
  664. int legacy_mode = 0, rc;
  665. rc = ata_host_start(host);
  666. if (rc)
  667. return rc;
  668. if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
  669. u8 tmp8, mask;
  670. /* TODO: What if one channel is in native mode ... */
  671. pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
  672. mask = (1 << 2) | (1 << 0);
  673. if ((tmp8 & mask) != mask)
  674. legacy_mode = 1;
  675. #if defined(CONFIG_NO_ATA_LEGACY)
  676. /* Some platforms with PCI limits cannot address compat
  677. port space. In that case we punt if their firmware has
  678. left a device in compatibility mode */
  679. if (legacy_mode) {
  680. printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
  681. return -EOPNOTSUPP;
  682. }
  683. #endif
  684. }
  685. if (!devres_open_group(dev, NULL, GFP_KERNEL))
  686. return -ENOMEM;
  687. if (!legacy_mode && pdev->irq) {
  688. rc = devm_request_irq(dev, pdev->irq, irq_handler,
  689. IRQF_SHARED, drv_name, host);
  690. if (rc)
  691. goto out;
  692. ata_port_desc(host->ports[0], "irq %d", pdev->irq);
  693. ata_port_desc(host->ports[1], "irq %d", pdev->irq);
  694. } else if (legacy_mode) {
  695. if (!ata_port_is_dummy(host->ports[0])) {
  696. rc = devm_request_irq(dev, ATA_PRIMARY_IRQ(pdev),
  697. irq_handler, IRQF_SHARED,
  698. drv_name, host);
  699. if (rc)
  700. goto out;
  701. ata_port_desc(host->ports[0], "irq %d",
  702. ATA_PRIMARY_IRQ(pdev));
  703. }
  704. if (!ata_port_is_dummy(host->ports[1])) {
  705. rc = devm_request_irq(dev, ATA_SECONDARY_IRQ(pdev),
  706. irq_handler, IRQF_SHARED,
  707. drv_name, host);
  708. if (rc)
  709. goto out;
  710. ata_port_desc(host->ports[1], "irq %d",
  711. ATA_SECONDARY_IRQ(pdev));
  712. }
  713. }
  714. rc = ata_host_register(host, sht);
  715. out:
  716. if (rc == 0)
  717. devres_remove_group(dev, NULL);
  718. else
  719. devres_release_group(dev, NULL);
  720. return rc;
  721. }
  722. /**
  723. * ata_pci_init_one - Initialize/register PCI IDE host controller
  724. * @pdev: Controller to be initialized
  725. * @ppi: array of port_info, must be enough for two ports
  726. * @sht: scsi_host_template to use when registering the host
  727. *
  728. * This is a helper function which can be called from a driver's
  729. * xxx_init_one() probe function if the hardware uses traditional
  730. * IDE taskfile registers.
  731. *
  732. * This function calls pci_enable_device(), reserves its register
  733. * regions, sets the dma mask, enables bus master mode, and calls
  734. * ata_device_add()
  735. *
  736. * ASSUMPTION:
  737. * Nobody makes a single channel controller that appears solely as
  738. * the secondary legacy port on PCI.
  739. *
  740. * LOCKING:
  741. * Inherited from PCI layer (may sleep).
  742. *
  743. * RETURNS:
  744. * Zero on success, negative on errno-based value on error.
  745. */
  746. int ata_pci_init_one(struct pci_dev *pdev,
  747. const struct ata_port_info * const * ppi,
  748. struct scsi_host_template *sht)
  749. {
  750. struct device *dev = &pdev->dev;
  751. const struct ata_port_info *pi = NULL;
  752. struct ata_host *host = NULL;
  753. int i, rc;
  754. DPRINTK("ENTER\n");
  755. /* look up the first valid port_info */
  756. for (i = 0; i < 2 && ppi[i]; i++) {
  757. if (ppi[i]->port_ops != &ata_dummy_port_ops) {
  758. pi = ppi[i];
  759. break;
  760. }
  761. }
  762. if (!pi) {
  763. dev_printk(KERN_ERR, &pdev->dev,
  764. "no valid port_info specified\n");
  765. return -EINVAL;
  766. }
  767. if (!devres_open_group(dev, NULL, GFP_KERNEL))
  768. return -ENOMEM;
  769. rc = pcim_enable_device(pdev);
  770. if (rc)
  771. goto out;
  772. /* prepare and activate SFF host */
  773. rc = ata_pci_prepare_sff_host(pdev, ppi, &host);
  774. if (rc)
  775. goto out;
  776. pci_set_master(pdev);
  777. rc = ata_pci_activate_sff_host(host, ata_interrupt, sht);
  778. out:
  779. if (rc == 0)
  780. devres_remove_group(&pdev->dev, NULL);
  781. else
  782. devres_release_group(&pdev->dev, NULL);
  783. return rc;
  784. }
  785. /**
  786. * ata_pci_clear_simplex - attempt to kick device out of simplex
  787. * @pdev: PCI device
  788. *
  789. * Some PCI ATA devices report simplex mode but in fact can be told to
  790. * enter non simplex mode. This implements the necessary logic to
  791. * perform the task on such devices. Calling it on other devices will
  792. * have -undefined- behaviour.
  793. */
  794. int ata_pci_clear_simplex(struct pci_dev *pdev)
  795. {
  796. unsigned long bmdma = pci_resource_start(pdev, 4);
  797. u8 simplex;
  798. if (bmdma == 0)
  799. return -ENOENT;
  800. simplex = inb(bmdma + 0x02);
  801. outb(simplex & 0x60, bmdma + 0x02);
  802. simplex = inb(bmdma + 0x02);
  803. if (simplex & 0x80)
  804. return -EOPNOTSUPP;
  805. return 0;
  806. }
  807. unsigned long ata_pci_default_filter(struct ata_device *adev, unsigned long xfer_mask)
  808. {
  809. /* Filter out DMA modes if the device has been configured by
  810. the BIOS as PIO only */
  811. if (adev->link->ap->ioaddr.bmdma_addr == NULL)
  812. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  813. return xfer_mask;
  814. }
  815. #endif /* CONFIG_PCI */