libata-sff.c 23 KB

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