libata-sff.c 28 KB

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