libata-sff.c 23 KB

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