libata-sff.c 27 KB

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