libata-sff.c 22 KB

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