libata-sff.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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. /**
  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->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_check_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. }
  156. }
  157. /**
  158. * ata_check_status - Read device status reg & clear interrupt
  159. * @ap: port where the device is
  160. *
  161. * Reads ATA taskfile status register for currently-selected device
  162. * and return its value. This also clears pending interrupts
  163. * from this device
  164. *
  165. * LOCKING:
  166. * Inherited from caller.
  167. */
  168. u8 ata_check_status(struct ata_port *ap)
  169. {
  170. return ioread8(ap->ioaddr.status_addr);
  171. }
  172. /**
  173. * ata_altstatus - Read device alternate status reg
  174. * @ap: port where the device is
  175. *
  176. * Reads ATA taskfile alternate status register for
  177. * currently-selected device and return its value.
  178. *
  179. * Note: may NOT be used as the check_altstatus() entry in
  180. * ata_port_operations.
  181. *
  182. * LOCKING:
  183. * Inherited from caller.
  184. */
  185. u8 ata_altstatus(struct ata_port *ap)
  186. {
  187. if (ap->ops->check_altstatus)
  188. return ap->ops->check_altstatus(ap);
  189. return ioread8(ap->ioaddr.altstatus_addr);
  190. }
  191. /**
  192. * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
  193. * @qc: Info associated with this ATA transaction.
  194. *
  195. * LOCKING:
  196. * spin_lock_irqsave(host lock)
  197. */
  198. void ata_bmdma_setup(struct ata_queued_cmd *qc)
  199. {
  200. struct ata_port *ap = qc->ap;
  201. unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
  202. u8 dmactl;
  203. /* load PRD table addr. */
  204. mb(); /* make sure PRD table writes are visible to controller */
  205. iowrite32(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
  206. /* specify data direction, triple-check start bit is clear */
  207. dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  208. dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
  209. if (!rw)
  210. dmactl |= ATA_DMA_WR;
  211. iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  212. /* issue r/w command */
  213. ap->ops->exec_command(ap, &qc->tf);
  214. }
  215. /**
  216. * ata_bmdma_start - Start a PCI IDE BMDMA transaction
  217. * @qc: Info associated with this ATA transaction.
  218. *
  219. * LOCKING:
  220. * spin_lock_irqsave(host lock)
  221. */
  222. void ata_bmdma_start (struct ata_queued_cmd *qc)
  223. {
  224. struct ata_port *ap = qc->ap;
  225. u8 dmactl;
  226. /* start host DMA transaction */
  227. dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  228. iowrite8(dmactl | ATA_DMA_START, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  229. /* Strictly, one may wish to issue a readb() here, to
  230. * flush the mmio write. However, control also passes
  231. * to the hardware at this point, and it will interrupt
  232. * us when we are to resume control. So, in effect,
  233. * we don't care when the mmio write flushes.
  234. * Further, a read of the DMA status register _immediately_
  235. * following the write may not be what certain flaky hardware
  236. * is expected, so I think it is best to not add a readb()
  237. * without first all the MMIO ATA cards/mobos.
  238. * Or maybe I'm just being paranoid.
  239. */
  240. }
  241. /**
  242. * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
  243. * @ap: Port associated with this ATA transaction.
  244. *
  245. * Clear interrupt and error flags in DMA status register.
  246. *
  247. * May be used as the irq_clear() entry in ata_port_operations.
  248. *
  249. * LOCKING:
  250. * spin_lock_irqsave(host lock)
  251. */
  252. void ata_bmdma_irq_clear(struct ata_port *ap)
  253. {
  254. void __iomem *mmio = ap->ioaddr.bmdma_addr;
  255. if (!mmio)
  256. return;
  257. iowrite8(ioread8(mmio + ATA_DMA_STATUS), mmio + ATA_DMA_STATUS);
  258. }
  259. /**
  260. * ata_bmdma_status - Read PCI IDE BMDMA status
  261. * @ap: Port associated with this ATA transaction.
  262. *
  263. * Read and return BMDMA status register.
  264. *
  265. * May be used as the bmdma_status() entry in ata_port_operations.
  266. *
  267. * LOCKING:
  268. * spin_lock_irqsave(host lock)
  269. */
  270. u8 ata_bmdma_status(struct ata_port *ap)
  271. {
  272. return ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
  273. }
  274. /**
  275. * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
  276. * @qc: Command we are ending DMA for
  277. *
  278. * Clears the ATA_DMA_START flag in the dma control register
  279. *
  280. * May be used as the bmdma_stop() entry in ata_port_operations.
  281. *
  282. * LOCKING:
  283. * spin_lock_irqsave(host lock)
  284. */
  285. void ata_bmdma_stop(struct ata_queued_cmd *qc)
  286. {
  287. struct ata_port *ap = qc->ap;
  288. void __iomem *mmio = ap->ioaddr.bmdma_addr;
  289. /* clear start/stop bit */
  290. iowrite8(ioread8(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
  291. mmio + ATA_DMA_CMD);
  292. /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
  293. ata_altstatus(ap); /* dummy read */
  294. }
  295. /**
  296. * ata_bmdma_freeze - Freeze BMDMA controller port
  297. * @ap: port to freeze
  298. *
  299. * Freeze BMDMA controller port.
  300. *
  301. * LOCKING:
  302. * Inherited from caller.
  303. */
  304. void ata_bmdma_freeze(struct ata_port *ap)
  305. {
  306. struct ata_ioports *ioaddr = &ap->ioaddr;
  307. ap->ctl |= ATA_NIEN;
  308. ap->last_ctl = ap->ctl;
  309. iowrite8(ap->ctl, ioaddr->ctl_addr);
  310. /* Under certain circumstances, some controllers raise IRQ on
  311. * ATA_NIEN manipulation. Also, many controllers fail to mask
  312. * previously pending IRQ on ATA_NIEN assertion. Clear it.
  313. */
  314. ata_chk_status(ap);
  315. ap->ops->irq_clear(ap);
  316. }
  317. /**
  318. * ata_bmdma_thaw - Thaw BMDMA controller port
  319. * @ap: port to thaw
  320. *
  321. * Thaw BMDMA controller port.
  322. *
  323. * LOCKING:
  324. * Inherited from caller.
  325. */
  326. void ata_bmdma_thaw(struct ata_port *ap)
  327. {
  328. /* clear & re-enable interrupts */
  329. ata_chk_status(ap);
  330. ap->ops->irq_clear(ap);
  331. if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
  332. ata_irq_on(ap);
  333. }
  334. /**
  335. * ata_bmdma_drive_eh - Perform EH with given methods for BMDMA controller
  336. * @ap: port to handle error for
  337. * @prereset: prereset method (can be NULL)
  338. * @softreset: softreset method (can be NULL)
  339. * @hardreset: hardreset method (can be NULL)
  340. * @postreset: postreset method (can be NULL)
  341. *
  342. * Handle error for ATA BMDMA controller. It can handle both
  343. * PATA and SATA controllers. Many controllers should be able to
  344. * use this EH as-is or with some added handling before and
  345. * after.
  346. *
  347. * This function is intended to be used for constructing
  348. * ->error_handler callback by low level drivers.
  349. *
  350. * LOCKING:
  351. * Kernel thread context (may sleep)
  352. */
  353. void ata_bmdma_drive_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
  354. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  355. ata_postreset_fn_t postreset)
  356. {
  357. struct ata_queued_cmd *qc;
  358. unsigned long flags;
  359. int thaw = 0;
  360. qc = __ata_qc_from_tag(ap, ap->active_tag);
  361. if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
  362. qc = NULL;
  363. /* reset PIO HSM and stop DMA engine */
  364. spin_lock_irqsave(ap->lock, flags);
  365. ap->hsm_task_state = HSM_ST_IDLE;
  366. if (qc && (qc->tf.protocol == ATA_PROT_DMA ||
  367. qc->tf.protocol == ATA_PROT_ATAPI_DMA)) {
  368. u8 host_stat;
  369. host_stat = ap->ops->bmdma_status(ap);
  370. /* BMDMA controllers indicate host bus error by
  371. * setting DMA_ERR bit and timing out. As it wasn't
  372. * really a timeout event, adjust error mask and
  373. * cancel frozen state.
  374. */
  375. if (qc->err_mask == AC_ERR_TIMEOUT && (host_stat & ATA_DMA_ERR)) {
  376. qc->err_mask = AC_ERR_HOST_BUS;
  377. thaw = 1;
  378. }
  379. ap->ops->bmdma_stop(qc);
  380. }
  381. ata_altstatus(ap);
  382. ata_chk_status(ap);
  383. ap->ops->irq_clear(ap);
  384. spin_unlock_irqrestore(ap->lock, flags);
  385. if (thaw)
  386. ata_eh_thaw_port(ap);
  387. /* PIO and DMA engines have been stopped, perform recovery */
  388. ata_do_eh(ap, prereset, softreset, hardreset, postreset);
  389. }
  390. /**
  391. * ata_bmdma_error_handler - Stock error handler for BMDMA controller
  392. * @ap: port to handle error for
  393. *
  394. * Stock error handler for BMDMA controller.
  395. *
  396. * LOCKING:
  397. * Kernel thread context (may sleep)
  398. */
  399. void ata_bmdma_error_handler(struct ata_port *ap)
  400. {
  401. ata_reset_fn_t hardreset;
  402. hardreset = NULL;
  403. if (sata_scr_valid(ap))
  404. hardreset = sata_std_hardreset;
  405. ata_bmdma_drive_eh(ap, ata_std_prereset, ata_std_softreset, hardreset,
  406. ata_std_postreset);
  407. }
  408. /**
  409. * ata_bmdma_post_internal_cmd - Stock post_internal_cmd for
  410. * BMDMA controller
  411. * @qc: internal command to clean up
  412. *
  413. * LOCKING:
  414. * Kernel thread context (may sleep)
  415. */
  416. void ata_bmdma_post_internal_cmd(struct ata_queued_cmd *qc)
  417. {
  418. if (qc->ap->ioaddr.bmdma_addr)
  419. ata_bmdma_stop(qc);
  420. }
  421. #ifdef CONFIG_PCI
  422. static int ata_resources_present(struct pci_dev *pdev, int port)
  423. {
  424. int i;
  425. /* Check the PCI resources for this channel are enabled */
  426. port = port * 2;
  427. for (i = 0; i < 2; i ++) {
  428. if (pci_resource_start(pdev, port + i) == 0 ||
  429. pci_resource_len(pdev, port + i) == 0)
  430. return 0;
  431. }
  432. return 1;
  433. }
  434. /**
  435. * ata_pci_init_native_mode - Initialize native-mode driver
  436. * @pdev: pci device to be initialized
  437. * @port: array[2] of pointers to port info structures.
  438. * @ports: bitmap of ports present
  439. *
  440. * Utility function which allocates and initializes an
  441. * ata_probe_ent structure for a standard dual-port
  442. * PIO-based IDE controller. The returned ata_probe_ent
  443. * structure can be passed to ata_device_add(). The returned
  444. * ata_probe_ent structure should then be freed with kfree().
  445. *
  446. * The caller need only pass the address of the primary port, the
  447. * secondary will be deduced automatically. If the device has non
  448. * standard secondary port mappings this function can be called twice,
  449. * once for each interface.
  450. */
  451. struct ata_probe_ent *
  452. ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
  453. {
  454. struct ata_probe_ent *probe_ent;
  455. int i, p = 0;
  456. void __iomem * const *iomap;
  457. /* iomap BARs */
  458. for (i = 0; i < 4; i++) {
  459. if (pcim_iomap(pdev, i, 0) == NULL) {
  460. dev_printk(KERN_ERR, &pdev->dev,
  461. "failed to iomap PCI BAR %d\n", i);
  462. return NULL;
  463. }
  464. }
  465. pcim_iomap(pdev, 4, 0); /* may fail */
  466. iomap = pcim_iomap_table(pdev);
  467. /* alloc and init probe_ent */
  468. probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
  469. if (!probe_ent)
  470. return NULL;
  471. probe_ent->irq = pdev->irq;
  472. probe_ent->irq_flags = IRQF_SHARED;
  473. /* Discard disabled ports. Some controllers show their
  474. unused channels this way */
  475. if (ata_resources_present(pdev, 0) == 0)
  476. ports &= ~ATA_PORT_PRIMARY;
  477. if (ata_resources_present(pdev, 1) == 0)
  478. ports &= ~ATA_PORT_SECONDARY;
  479. if (ports & ATA_PORT_PRIMARY) {
  480. probe_ent->port[p].cmd_addr = iomap[0];
  481. probe_ent->port[p].altstatus_addr =
  482. probe_ent->port[p].ctl_addr = (void __iomem *)
  483. ((unsigned long)iomap[1] | ATA_PCI_CTL_OFS);
  484. if (iomap[4]) {
  485. if ((!(port[p]->flags & ATA_FLAG_IGN_SIMPLEX)) &&
  486. (ioread8(iomap[4] + 2) & 0x80))
  487. probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
  488. probe_ent->port[p].bmdma_addr = iomap[4];
  489. }
  490. ata_std_ports(&probe_ent->port[p]);
  491. p++;
  492. }
  493. if (ports & ATA_PORT_SECONDARY) {
  494. probe_ent->port[p].cmd_addr = iomap[2];
  495. probe_ent->port[p].altstatus_addr =
  496. probe_ent->port[p].ctl_addr = (void __iomem *)
  497. ((unsigned long)iomap[3] | ATA_PCI_CTL_OFS);
  498. if (iomap[4]) {
  499. if ((!(port[p]->flags & ATA_FLAG_IGN_SIMPLEX)) &&
  500. (ioread8(iomap[4] + 10) & 0x80))
  501. probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
  502. probe_ent->port[p].bmdma_addr = iomap[4] + 8;
  503. }
  504. ata_std_ports(&probe_ent->port[p]);
  505. probe_ent->pinfo2 = port[1];
  506. p++;
  507. }
  508. probe_ent->n_ports = p;
  509. return probe_ent;
  510. }
  511. static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev,
  512. struct ata_port_info **port, int port_mask)
  513. {
  514. struct ata_probe_ent *probe_ent;
  515. void __iomem *iomap[5] = { }, *bmdma;
  516. if (port_mask & ATA_PORT_PRIMARY) {
  517. iomap[0] = devm_ioport_map(&pdev->dev, ATA_PRIMARY_CMD, 8);
  518. iomap[1] = devm_ioport_map(&pdev->dev, ATA_PRIMARY_CTL, 1);
  519. if (!iomap[0] || !iomap[1])
  520. return NULL;
  521. }
  522. if (port_mask & ATA_PORT_SECONDARY) {
  523. iomap[2] = devm_ioport_map(&pdev->dev, ATA_SECONDARY_CMD, 8);
  524. iomap[3] = devm_ioport_map(&pdev->dev, ATA_SECONDARY_CTL, 1);
  525. if (!iomap[2] || !iomap[3])
  526. return NULL;
  527. }
  528. bmdma = pcim_iomap(pdev, 4, 16); /* may fail */
  529. /* alloc and init probe_ent */
  530. probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
  531. if (!probe_ent)
  532. return NULL;
  533. probe_ent->n_ports = 2;
  534. probe_ent->irq_flags = IRQF_SHARED;
  535. if (port_mask & ATA_PORT_PRIMARY) {
  536. probe_ent->irq = ATA_PRIMARY_IRQ(pdev);
  537. probe_ent->port[0].cmd_addr = iomap[0];
  538. probe_ent->port[0].altstatus_addr =
  539. probe_ent->port[0].ctl_addr = iomap[1];
  540. if (bmdma) {
  541. probe_ent->port[0].bmdma_addr = bmdma;
  542. if ((!(port[0]->flags & ATA_FLAG_IGN_SIMPLEX)) &&
  543. (ioread8(bmdma + 2) & 0x80))
  544. probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
  545. }
  546. ata_std_ports(&probe_ent->port[0]);
  547. } else
  548. probe_ent->dummy_port_mask |= ATA_PORT_PRIMARY;
  549. if (port_mask & ATA_PORT_SECONDARY) {
  550. if (probe_ent->irq)
  551. probe_ent->irq2 = ATA_SECONDARY_IRQ(pdev);
  552. else
  553. probe_ent->irq = ATA_SECONDARY_IRQ(pdev);
  554. probe_ent->port[1].cmd_addr = iomap[2];
  555. probe_ent->port[1].altstatus_addr =
  556. probe_ent->port[1].ctl_addr = iomap[3];
  557. if (bmdma) {
  558. probe_ent->port[1].bmdma_addr = bmdma + 8;
  559. if ((!(port[1]->flags & ATA_FLAG_IGN_SIMPLEX)) &&
  560. (ioread8(bmdma + 10) & 0x80))
  561. probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
  562. }
  563. ata_std_ports(&probe_ent->port[1]);
  564. /* FIXME: could be pointing to stack area; must copy */
  565. probe_ent->pinfo2 = port[1];
  566. } else
  567. probe_ent->dummy_port_mask |= ATA_PORT_SECONDARY;
  568. return probe_ent;
  569. }
  570. /**
  571. * ata_pci_init_one - Initialize/register PCI IDE host controller
  572. * @pdev: Controller to be initialized
  573. * @port_info: Information from low-level host driver
  574. * @n_ports: Number of ports attached to host controller
  575. *
  576. * This is a helper function which can be called from a driver's
  577. * xxx_init_one() probe function if the hardware uses traditional
  578. * IDE taskfile registers.
  579. *
  580. * This function calls pci_enable_device(), reserves its register
  581. * regions, sets the dma mask, enables bus master mode, and calls
  582. * ata_device_add()
  583. *
  584. * ASSUMPTION:
  585. * Nobody makes a single channel controller that appears solely as
  586. * the secondary legacy port on PCI.
  587. *
  588. * LOCKING:
  589. * Inherited from PCI layer (may sleep).
  590. *
  591. * RETURNS:
  592. * Zero on success, negative on errno-based value on error.
  593. */
  594. int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
  595. unsigned int n_ports)
  596. {
  597. struct device *dev = &pdev->dev;
  598. struct ata_probe_ent *probe_ent = NULL;
  599. struct ata_port_info *port[2];
  600. u8 mask;
  601. unsigned int legacy_mode = 0;
  602. int rc;
  603. DPRINTK("ENTER\n");
  604. if (!devres_open_group(dev, NULL, GFP_KERNEL))
  605. return -ENOMEM;
  606. BUG_ON(n_ports < 1 || n_ports > 2);
  607. port[0] = port_info[0];
  608. if (n_ports > 1)
  609. port[1] = port_info[1];
  610. else
  611. port[1] = port[0];
  612. /* FIXME: Really for ATA it isn't safe because the device may be
  613. multi-purpose and we want to leave it alone if it was already
  614. enabled. Secondly for shared use as Arjan says we want refcounting
  615. Checking dev->is_enabled is insufficient as this is not set at
  616. boot for the primary video which is BIOS enabled
  617. */
  618. rc = pcim_enable_device(pdev);
  619. if (rc)
  620. goto err_out;
  621. if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
  622. u8 tmp8;
  623. /* TODO: What if one channel is in native mode ... */
  624. pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
  625. mask = (1 << 2) | (1 << 0);
  626. if ((tmp8 & mask) != mask)
  627. legacy_mode = (1 << 3);
  628. #if defined(CONFIG_NO_ATA_LEGACY)
  629. /* Some platforms with PCI limits cannot address compat
  630. port space. In that case we punt if their firmware has
  631. left a device in compatibility mode */
  632. if (legacy_mode) {
  633. printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
  634. rc = -EOPNOTSUPP;
  635. goto err_out;
  636. }
  637. #endif
  638. }
  639. if (!legacy_mode) {
  640. rc = pci_request_regions(pdev, DRV_NAME);
  641. if (rc) {
  642. pcim_pin_device(pdev);
  643. goto err_out;
  644. }
  645. } else {
  646. /* Deal with combined mode hack. This side of the logic all
  647. goes away once the combined mode hack is killed in 2.6.21 */
  648. if (!devm_request_region(dev, ATA_PRIMARY_CMD, 8, "libata")) {
  649. struct resource *conflict, res;
  650. res.start = ATA_PRIMARY_CMD;
  651. res.end = ATA_PRIMARY_CMD + 8 - 1;
  652. conflict = ____request_resource(&ioport_resource, &res);
  653. while (conflict->child)
  654. conflict = ____request_resource(conflict, &res);
  655. if (!strcmp(conflict->name, "libata"))
  656. legacy_mode |= ATA_PORT_PRIMARY;
  657. else {
  658. pcim_pin_device(pdev);
  659. printk(KERN_WARNING "ata: 0x%0X IDE port busy\n" \
  660. "ata: conflict with %s\n",
  661. ATA_PRIMARY_CMD,
  662. conflict->name);
  663. }
  664. } else
  665. legacy_mode |= ATA_PORT_PRIMARY;
  666. if (!devm_request_region(dev, ATA_SECONDARY_CMD, 8, "libata")) {
  667. struct resource *conflict, res;
  668. res.start = ATA_SECONDARY_CMD;
  669. res.end = ATA_SECONDARY_CMD + 8 - 1;
  670. conflict = ____request_resource(&ioport_resource, &res);
  671. while (conflict->child)
  672. conflict = ____request_resource(conflict, &res);
  673. if (!strcmp(conflict->name, "libata"))
  674. legacy_mode |= ATA_PORT_SECONDARY;
  675. else {
  676. pcim_pin_device(pdev);
  677. printk(KERN_WARNING "ata: 0x%X IDE port busy\n" \
  678. "ata: conflict with %s\n",
  679. ATA_SECONDARY_CMD,
  680. conflict->name);
  681. }
  682. } else
  683. legacy_mode |= ATA_PORT_SECONDARY;
  684. if (legacy_mode & ATA_PORT_PRIMARY)
  685. pci_request_region(pdev, 1, DRV_NAME);
  686. if (legacy_mode & ATA_PORT_SECONDARY)
  687. pci_request_region(pdev, 3, DRV_NAME);
  688. /* If there is a DMA resource, allocate it */
  689. pci_request_region(pdev, 4, DRV_NAME);
  690. }
  691. /* we have legacy mode, but all ports are unavailable */
  692. if (legacy_mode == (1 << 3)) {
  693. rc = -EBUSY;
  694. goto err_out;
  695. }
  696. /* TODO: If we get no DMA mask we should fall back to PIO */
  697. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  698. if (rc)
  699. goto err_out;
  700. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  701. if (rc)
  702. goto err_out;
  703. if (legacy_mode) {
  704. probe_ent = ata_pci_init_legacy_port(pdev, port, legacy_mode);
  705. } else {
  706. if (n_ports == 2)
  707. probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
  708. else
  709. probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
  710. }
  711. if (!probe_ent) {
  712. rc = -ENOMEM;
  713. goto err_out;
  714. }
  715. pci_set_master(pdev);
  716. if (!ata_device_add(probe_ent)) {
  717. rc = -ENODEV;
  718. goto err_out;
  719. }
  720. devm_kfree(dev, probe_ent);
  721. devres_remove_group(dev, NULL);
  722. return 0;
  723. err_out:
  724. devres_release_group(dev, NULL);
  725. return rc;
  726. }
  727. /**
  728. * ata_pci_clear_simplex - attempt to kick device out of simplex
  729. * @pdev: PCI device
  730. *
  731. * Some PCI ATA devices report simplex mode but in fact can be told to
  732. * enter non simplex mode. This implements the neccessary logic to
  733. * perform the task on such devices. Calling it on other devices will
  734. * have -undefined- behaviour.
  735. */
  736. int ata_pci_clear_simplex(struct pci_dev *pdev)
  737. {
  738. unsigned long bmdma = pci_resource_start(pdev, 4);
  739. u8 simplex;
  740. if (bmdma == 0)
  741. return -ENOENT;
  742. simplex = inb(bmdma + 0x02);
  743. outb(simplex & 0x60, bmdma + 0x02);
  744. simplex = inb(bmdma + 0x02);
  745. if (simplex & 0x80)
  746. return -EOPNOTSUPP;
  747. return 0;
  748. }
  749. unsigned long ata_pci_default_filter(const struct ata_port *ap, struct ata_device *adev, unsigned long xfer_mask)
  750. {
  751. /* Filter out DMA modes if the device has been configured by
  752. the BIOS as PIO only */
  753. if (ap->ioaddr.bmdma_addr == 0)
  754. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  755. return xfer_mask;
  756. }
  757. #endif /* CONFIG_PCI */