libata-sff.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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->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_native_mode - Initialize native-mode driver
  466. * @pdev: pci device to be initialized
  467. * @port: array[2] of pointers to port info structures.
  468. * @ports: bitmap of ports present
  469. *
  470. * Utility function which allocates and initializes an
  471. * ata_probe_ent structure for a standard dual-port
  472. * PIO-based IDE controller. The returned ata_probe_ent
  473. * structure can be passed to ata_device_add(). The returned
  474. * ata_probe_ent structure should then be freed with kfree().
  475. *
  476. * The caller need only pass the address of the primary port, the
  477. * secondary will be deduced automatically. If the device has non
  478. * standard secondary port mappings this function can be called twice,
  479. * once for each interface.
  480. */
  481. struct ata_probe_ent *
  482. ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
  483. {
  484. struct ata_probe_ent *probe_ent;
  485. int i, p = 0;
  486. void __iomem * const *iomap;
  487. /* iomap BARs */
  488. for (i = 0; i < 4; i++) {
  489. if (pcim_iomap(pdev, i, 0) == NULL) {
  490. dev_printk(KERN_ERR, &pdev->dev,
  491. "failed to iomap PCI BAR %d\n", i);
  492. return NULL;
  493. }
  494. }
  495. pcim_iomap(pdev, 4, 0); /* may fail */
  496. iomap = pcim_iomap_table(pdev);
  497. /* alloc and init probe_ent */
  498. probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
  499. if (!probe_ent)
  500. return NULL;
  501. probe_ent->irq = pdev->irq;
  502. probe_ent->irq_flags = IRQF_SHARED;
  503. /* Discard disabled ports. Some controllers show their
  504. unused channels this way */
  505. if (ata_resources_present(pdev, 0) == 0)
  506. ports &= ~ATA_PORT_PRIMARY;
  507. if (ata_resources_present(pdev, 1) == 0)
  508. ports &= ~ATA_PORT_SECONDARY;
  509. if (ports & ATA_PORT_PRIMARY) {
  510. probe_ent->port[p].cmd_addr = iomap[0];
  511. probe_ent->port[p].altstatus_addr =
  512. probe_ent->port[p].ctl_addr = (void __iomem *)
  513. ((unsigned long)iomap[1] | ATA_PCI_CTL_OFS);
  514. if (iomap[4]) {
  515. if ((!(port[p]->flags & ATA_FLAG_IGN_SIMPLEX)) &&
  516. (ioread8(iomap[4] + 2) & 0x80))
  517. probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
  518. probe_ent->port[p].bmdma_addr = iomap[4];
  519. }
  520. ata_std_ports(&probe_ent->port[p]);
  521. p++;
  522. }
  523. if (ports & ATA_PORT_SECONDARY) {
  524. probe_ent->port[p].cmd_addr = iomap[2];
  525. probe_ent->port[p].altstatus_addr =
  526. probe_ent->port[p].ctl_addr = (void __iomem *)
  527. ((unsigned long)iomap[3] | ATA_PCI_CTL_OFS);
  528. if (iomap[4]) {
  529. if ((!(port[p]->flags & ATA_FLAG_IGN_SIMPLEX)) &&
  530. (ioread8(iomap[4] + 10) & 0x80))
  531. probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
  532. probe_ent->port[p].bmdma_addr = iomap[4] + 8;
  533. }
  534. ata_std_ports(&probe_ent->port[p]);
  535. probe_ent->pinfo2 = port[1];
  536. p++;
  537. }
  538. probe_ent->n_ports = p;
  539. return probe_ent;
  540. }
  541. static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev,
  542. struct ata_port_info **port, int port_mask)
  543. {
  544. struct ata_probe_ent *probe_ent;
  545. void __iomem *iomap[5] = { }, *bmdma;
  546. if (port_mask & ATA_PORT_PRIMARY) {
  547. iomap[0] = devm_ioport_map(&pdev->dev, ATA_PRIMARY_CMD, 8);
  548. iomap[1] = devm_ioport_map(&pdev->dev, ATA_PRIMARY_CTL, 1);
  549. if (!iomap[0] || !iomap[1])
  550. return NULL;
  551. }
  552. if (port_mask & ATA_PORT_SECONDARY) {
  553. iomap[2] = devm_ioport_map(&pdev->dev, ATA_SECONDARY_CMD, 8);
  554. iomap[3] = devm_ioport_map(&pdev->dev, ATA_SECONDARY_CTL, 1);
  555. if (!iomap[2] || !iomap[3])
  556. return NULL;
  557. }
  558. bmdma = pcim_iomap(pdev, 4, 16); /* may fail */
  559. /* alloc and init probe_ent */
  560. probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
  561. if (!probe_ent)
  562. return NULL;
  563. probe_ent->n_ports = 2;
  564. probe_ent->irq_flags = IRQF_SHARED;
  565. if (port_mask & ATA_PORT_PRIMARY) {
  566. probe_ent->irq = ATA_PRIMARY_IRQ(pdev);
  567. probe_ent->port[0].cmd_addr = iomap[0];
  568. probe_ent->port[0].altstatus_addr =
  569. probe_ent->port[0].ctl_addr = iomap[1];
  570. if (bmdma) {
  571. probe_ent->port[0].bmdma_addr = bmdma;
  572. if ((!(port[0]->flags & ATA_FLAG_IGN_SIMPLEX)) &&
  573. (ioread8(bmdma + 2) & 0x80))
  574. probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
  575. }
  576. ata_std_ports(&probe_ent->port[0]);
  577. } else
  578. probe_ent->dummy_port_mask |= ATA_PORT_PRIMARY;
  579. if (port_mask & ATA_PORT_SECONDARY) {
  580. if (probe_ent->irq)
  581. probe_ent->irq2 = ATA_SECONDARY_IRQ(pdev);
  582. else
  583. probe_ent->irq = ATA_SECONDARY_IRQ(pdev);
  584. probe_ent->port[1].cmd_addr = iomap[2];
  585. probe_ent->port[1].altstatus_addr =
  586. probe_ent->port[1].ctl_addr = iomap[3];
  587. if (bmdma) {
  588. probe_ent->port[1].bmdma_addr = bmdma + 8;
  589. if ((!(port[1]->flags & ATA_FLAG_IGN_SIMPLEX)) &&
  590. (ioread8(bmdma + 10) & 0x80))
  591. probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
  592. }
  593. ata_std_ports(&probe_ent->port[1]);
  594. /* FIXME: could be pointing to stack area; must copy */
  595. probe_ent->pinfo2 = port[1];
  596. } else
  597. probe_ent->dummy_port_mask |= ATA_PORT_SECONDARY;
  598. return probe_ent;
  599. }
  600. /**
  601. * ata_pci_init_one - Initialize/register PCI IDE host controller
  602. * @pdev: Controller to be initialized
  603. * @port_info: Information from low-level host driver
  604. * @n_ports: Number of ports attached to host controller
  605. *
  606. * This is a helper function which can be called from a driver's
  607. * xxx_init_one() probe function if the hardware uses traditional
  608. * IDE taskfile registers.
  609. *
  610. * This function calls pci_enable_device(), reserves its register
  611. * regions, sets the dma mask, enables bus master mode, and calls
  612. * ata_device_add()
  613. *
  614. * ASSUMPTION:
  615. * Nobody makes a single channel controller that appears solely as
  616. * the secondary legacy port on PCI.
  617. *
  618. * LOCKING:
  619. * Inherited from PCI layer (may sleep).
  620. *
  621. * RETURNS:
  622. * Zero on success, negative on errno-based value on error.
  623. */
  624. int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
  625. unsigned int n_ports)
  626. {
  627. struct device *dev = &pdev->dev;
  628. struct ata_probe_ent *probe_ent = NULL;
  629. struct ata_port_info *port[2];
  630. u8 mask;
  631. unsigned int legacy_mode = 0;
  632. int rc;
  633. DPRINTK("ENTER\n");
  634. if (!devres_open_group(dev, NULL, GFP_KERNEL))
  635. return -ENOMEM;
  636. BUG_ON(n_ports < 1 || n_ports > 2);
  637. port[0] = port_info[0];
  638. if (n_ports > 1)
  639. port[1] = port_info[1];
  640. else
  641. port[1] = port[0];
  642. /* FIXME: Really for ATA it isn't safe because the device may be
  643. multi-purpose and we want to leave it alone if it was already
  644. enabled. Secondly for shared use as Arjan says we want refcounting
  645. Checking dev->is_enabled is insufficient as this is not set at
  646. boot for the primary video which is BIOS enabled
  647. */
  648. rc = pcim_enable_device(pdev);
  649. if (rc)
  650. goto err_out;
  651. if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
  652. u8 tmp8;
  653. /* TODO: What if one channel is in native mode ... */
  654. pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
  655. mask = (1 << 2) | (1 << 0);
  656. if ((tmp8 & mask) != mask)
  657. legacy_mode = (1 << 3);
  658. #if defined(CONFIG_NO_ATA_LEGACY)
  659. /* Some platforms with PCI limits cannot address compat
  660. port space. In that case we punt if their firmware has
  661. left a device in compatibility mode */
  662. if (legacy_mode) {
  663. printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
  664. rc = -EOPNOTSUPP;
  665. goto err_out;
  666. }
  667. #endif
  668. }
  669. if (!legacy_mode) {
  670. rc = pci_request_regions(pdev, DRV_NAME);
  671. if (rc) {
  672. pcim_pin_device(pdev);
  673. goto err_out;
  674. }
  675. } else {
  676. /* Deal with combined mode hack. This side of the logic all
  677. goes away once the combined mode hack is killed in 2.6.21 */
  678. if (!devm_request_region(dev, ATA_PRIMARY_CMD, 8, "libata")) {
  679. struct resource *conflict, res;
  680. res.start = ATA_PRIMARY_CMD;
  681. res.end = ATA_PRIMARY_CMD + 8 - 1;
  682. conflict = ____request_resource(&ioport_resource, &res);
  683. while (conflict->child)
  684. conflict = ____request_resource(conflict, &res);
  685. if (!strcmp(conflict->name, "libata"))
  686. legacy_mode |= ATA_PORT_PRIMARY;
  687. else {
  688. pcim_pin_device(pdev);
  689. printk(KERN_WARNING "ata: 0x%0X IDE port busy\n" \
  690. "ata: conflict with %s\n",
  691. ATA_PRIMARY_CMD,
  692. conflict->name);
  693. }
  694. } else
  695. legacy_mode |= ATA_PORT_PRIMARY;
  696. if (!devm_request_region(dev, ATA_SECONDARY_CMD, 8, "libata")) {
  697. struct resource *conflict, res;
  698. res.start = ATA_SECONDARY_CMD;
  699. res.end = ATA_SECONDARY_CMD + 8 - 1;
  700. conflict = ____request_resource(&ioport_resource, &res);
  701. while (conflict->child)
  702. conflict = ____request_resource(conflict, &res);
  703. if (!strcmp(conflict->name, "libata"))
  704. legacy_mode |= ATA_PORT_SECONDARY;
  705. else {
  706. pcim_pin_device(pdev);
  707. printk(KERN_WARNING "ata: 0x%X IDE port busy\n" \
  708. "ata: conflict with %s\n",
  709. ATA_SECONDARY_CMD,
  710. conflict->name);
  711. }
  712. } else
  713. legacy_mode |= ATA_PORT_SECONDARY;
  714. if (legacy_mode & ATA_PORT_PRIMARY)
  715. pci_request_region(pdev, 1, DRV_NAME);
  716. if (legacy_mode & ATA_PORT_SECONDARY)
  717. pci_request_region(pdev, 3, DRV_NAME);
  718. /* If there is a DMA resource, allocate it */
  719. pci_request_region(pdev, 4, DRV_NAME);
  720. }
  721. /* we have legacy mode, but all ports are unavailable */
  722. if (legacy_mode == (1 << 3)) {
  723. rc = -EBUSY;
  724. goto err_out;
  725. }
  726. /* TODO: If we get no DMA mask we should fall back to PIO */
  727. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  728. if (rc)
  729. goto err_out;
  730. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  731. if (rc)
  732. goto err_out;
  733. if (legacy_mode) {
  734. probe_ent = ata_pci_init_legacy_port(pdev, port, legacy_mode);
  735. } else {
  736. if (n_ports == 2)
  737. probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
  738. else
  739. probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
  740. }
  741. if (!probe_ent) {
  742. rc = -ENOMEM;
  743. goto err_out;
  744. }
  745. pci_set_master(pdev);
  746. if (!ata_device_add(probe_ent)) {
  747. rc = -ENODEV;
  748. goto err_out;
  749. }
  750. devm_kfree(dev, probe_ent);
  751. devres_remove_group(dev, NULL);
  752. return 0;
  753. err_out:
  754. devres_release_group(dev, NULL);
  755. return rc;
  756. }
  757. /**
  758. * ata_pci_clear_simplex - attempt to kick device out of simplex
  759. * @pdev: PCI device
  760. *
  761. * Some PCI ATA devices report simplex mode but in fact can be told to
  762. * enter non simplex mode. This implements the neccessary logic to
  763. * perform the task on such devices. Calling it on other devices will
  764. * have -undefined- behaviour.
  765. */
  766. int ata_pci_clear_simplex(struct pci_dev *pdev)
  767. {
  768. unsigned long bmdma = pci_resource_start(pdev, 4);
  769. u8 simplex;
  770. if (bmdma == 0)
  771. return -ENOENT;
  772. simplex = inb(bmdma + 0x02);
  773. outb(simplex & 0x60, bmdma + 0x02);
  774. simplex = inb(bmdma + 0x02);
  775. if (simplex & 0x80)
  776. return -EOPNOTSUPP;
  777. return 0;
  778. }
  779. unsigned long ata_pci_default_filter(const struct ata_port *ap, struct ata_device *adev, unsigned long xfer_mask)
  780. {
  781. /* Filter out DMA modes if the device has been configured by
  782. the BIOS as PIO only */
  783. if (ap->ioaddr.bmdma_addr == 0)
  784. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  785. return xfer_mask;
  786. }
  787. #endif /* CONFIG_PCI */