libata-sff.c 27 KB

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