libata-sff.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  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_native_mode - Initialize native-mode driver
  466. * @pdev: pci device to be initialized
  467. * @port: array[2] of pointers to port info structures.
  468. * @ports: bitmap of ports present
  469. *
  470. * Utility function which allocates and initializes an
  471. * ata_probe_ent structure for a standard dual-port
  472. * PIO-based IDE controller. The returned ata_probe_ent
  473. * structure can be passed to ata_device_add(). The returned
  474. * ata_probe_ent structure should then be freed with kfree().
  475. *
  476. * The caller need only pass the address of the primary port, the
  477. * secondary will be deduced automatically. If the device has non
  478. * standard secondary port mappings this function can be called twice,
  479. * once for each interface.
  480. */
  481. struct ata_probe_ent *
  482. ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
  483. {
  484. struct ata_probe_ent *probe_ent;
  485. int i;
  486. void __iomem * const *iomap;
  487. /* Discard disabled ports. Some controllers show their unused
  488. * channels this way. Disabled ports will be made dummy.
  489. */
  490. if (ata_resources_present(pdev, 0) == 0)
  491. ports &= ~ATA_PORT_PRIMARY;
  492. if (ata_resources_present(pdev, 1) == 0)
  493. ports &= ~ATA_PORT_SECONDARY;
  494. if (!ports) {
  495. dev_printk(KERN_ERR, &pdev->dev, "no available port\n");
  496. return NULL;
  497. }
  498. /* iomap BARs */
  499. for (i = 0; i < 4; i++) {
  500. if (!(ports & (1 << (i / 2))))
  501. continue;
  502. if (pcim_iomap(pdev, i, 0) == NULL) {
  503. dev_printk(KERN_ERR, &pdev->dev,
  504. "failed to iomap PCI BAR %d\n", i);
  505. return NULL;
  506. }
  507. }
  508. pcim_iomap(pdev, 4, 0); /* may fail */
  509. iomap = pcim_iomap_table(pdev);
  510. /* alloc and init probe_ent */
  511. probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
  512. if (!probe_ent)
  513. return NULL;
  514. probe_ent->n_ports = 2;
  515. probe_ent->irq = pdev->irq;
  516. probe_ent->irq_flags = IRQF_SHARED;
  517. if (ports & ATA_PORT_PRIMARY) {
  518. probe_ent->port[0].cmd_addr = iomap[0];
  519. probe_ent->port[0].altstatus_addr =
  520. probe_ent->port[0].ctl_addr = (void __iomem *)
  521. ((unsigned long)iomap[1] | ATA_PCI_CTL_OFS);
  522. if (iomap[4]) {
  523. if ((!(port[0]->flags & ATA_FLAG_IGN_SIMPLEX)) &&
  524. (ioread8(iomap[4] + 2) & 0x80))
  525. probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
  526. probe_ent->port[0].bmdma_addr = iomap[4];
  527. }
  528. ata_std_ports(&probe_ent->port[0]);
  529. } else
  530. probe_ent->dummy_port_mask |= ATA_PORT_PRIMARY;
  531. if (ports & ATA_PORT_SECONDARY) {
  532. probe_ent->port[1].cmd_addr = iomap[2];
  533. probe_ent->port[1].altstatus_addr =
  534. probe_ent->port[1].ctl_addr = (void __iomem *)
  535. ((unsigned long)iomap[3] | ATA_PCI_CTL_OFS);
  536. if (iomap[4]) {
  537. if ((!(port[1]->flags & ATA_FLAG_IGN_SIMPLEX)) &&
  538. (ioread8(iomap[4] + 10) & 0x80))
  539. probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
  540. probe_ent->port[1].bmdma_addr = iomap[4] + 8;
  541. }
  542. ata_std_ports(&probe_ent->port[1]);
  543. probe_ent->pinfo2 = port[1];
  544. } else
  545. probe_ent->dummy_port_mask |= ATA_PORT_SECONDARY;
  546. return probe_ent;
  547. }
  548. /**
  549. * ata_pci_init_bmdma - acquire PCI BMDMA resources and init ATA host
  550. * @host: target ATA host
  551. *
  552. * Acquire PCI BMDMA resources and initialize @host accordingly.
  553. *
  554. * LOCKING:
  555. * Inherited from calling layer (may sleep).
  556. *
  557. * RETURNS:
  558. * 0 on success, -errno otherwise.
  559. */
  560. static int ata_pci_init_bmdma(struct ata_host *host)
  561. {
  562. struct device *gdev = host->dev;
  563. struct pci_dev *pdev = to_pci_dev(gdev);
  564. int i, rc;
  565. /* TODO: If we get no DMA mask we should fall back to PIO */
  566. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  567. if (rc)
  568. return rc;
  569. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  570. if (rc)
  571. return rc;
  572. /* request and iomap DMA region */
  573. rc = pcim_iomap_regions(pdev, 1 << 4, DRV_NAME);
  574. if (rc) {
  575. dev_printk(KERN_ERR, gdev, "failed to request/iomap BAR4\n");
  576. return -ENOMEM;
  577. }
  578. host->iomap = pcim_iomap_table(pdev);
  579. for (i = 0; i < 2; i++) {
  580. struct ata_port *ap = host->ports[i];
  581. void __iomem *bmdma = host->iomap[4] + 8 * i;
  582. if (ata_port_is_dummy(ap))
  583. continue;
  584. ap->ioaddr.bmdma_addr = bmdma;
  585. if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) &&
  586. (ioread8(bmdma + 2) & 0x80))
  587. host->flags |= ATA_HOST_SIMPLEX;
  588. }
  589. return 0;
  590. }
  591. /**
  592. * ata_pci_init_native_host - acquire native ATA resources and init host
  593. * @host: target ATA host
  594. * @port_mask: ports to consider
  595. *
  596. * Acquire native PCI ATA resources for @host and initialize
  597. * @host accordoingly.
  598. *
  599. * LOCKING:
  600. * Inherited from calling layer (may sleep).
  601. *
  602. * RETURNS:
  603. * 0 on success, -errno otherwise.
  604. */
  605. int ata_pci_init_native_host(struct ata_host *host, unsigned int port_mask)
  606. {
  607. struct device *gdev = host->dev;
  608. struct pci_dev *pdev = to_pci_dev(gdev);
  609. int i, rc;
  610. /* Discard disabled ports. Some controllers show their unused
  611. * channels this way. Disabled ports are made dummy.
  612. */
  613. for (i = 0; i < 2; i++) {
  614. if ((port_mask & (1 << i)) && !ata_resources_present(pdev, i)) {
  615. host->ports[i]->ops = &ata_dummy_port_ops;
  616. port_mask &= ~(1 << i);
  617. }
  618. }
  619. if (!port_mask) {
  620. dev_printk(KERN_ERR, gdev, "no available port\n");
  621. return -ENODEV;
  622. }
  623. /* request, iomap BARs and init port addresses accordingly */
  624. for (i = 0; i < 2; i++) {
  625. struct ata_port *ap = host->ports[i];
  626. int base = i * 2;
  627. void __iomem * const *iomap;
  628. if (!(port_mask & (1 << i)))
  629. continue;
  630. rc = pcim_iomap_regions(pdev, 0x3 << base, DRV_NAME);
  631. if (rc) {
  632. dev_printk(KERN_ERR, gdev, "failed to request/iomap "
  633. "BARs for port %d (errno=%d)\n", i, rc);
  634. if (rc == -EBUSY)
  635. pcim_pin_device(pdev);
  636. return rc;
  637. }
  638. host->iomap = iomap = pcim_iomap_table(pdev);
  639. ap->ioaddr.cmd_addr = iomap[base];
  640. ap->ioaddr.altstatus_addr =
  641. ap->ioaddr.ctl_addr = (void __iomem *)
  642. ((unsigned long)iomap[base + 1] | ATA_PCI_CTL_OFS);
  643. ata_std_ports(&ap->ioaddr);
  644. }
  645. return 0;
  646. }
  647. /**
  648. * ata_pci_prepare_native_host - helper to prepare native PCI ATA host
  649. * @pdev: target PCI device
  650. * @ppi: array of port_info
  651. * @n_ports: number of ports to allocate
  652. * @r_host: out argument for the initialized ATA host
  653. *
  654. * Helper to allocate ATA host for @pdev, acquire all native PCI
  655. * resources and initialize it accordingly in one go.
  656. *
  657. * LOCKING:
  658. * Inherited from calling layer (may sleep).
  659. *
  660. * RETURNS:
  661. * 0 on success, -errno otherwise.
  662. */
  663. int ata_pci_prepare_native_host(struct pci_dev *pdev,
  664. const struct ata_port_info * const * ppi,
  665. int n_ports, struct ata_host **r_host)
  666. {
  667. struct ata_host *host;
  668. unsigned int port_mask;
  669. int rc;
  670. if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL))
  671. return -ENOMEM;
  672. host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2);
  673. if (!host) {
  674. dev_printk(KERN_ERR, &pdev->dev,
  675. "failed to allocate ATA host\n");
  676. rc = -ENOMEM;
  677. goto err_out;
  678. }
  679. port_mask = ATA_PORT_PRIMARY;
  680. if (n_ports > 1)
  681. port_mask |= ATA_PORT_SECONDARY;
  682. rc = ata_pci_init_native_host(host, port_mask);
  683. if (rc)
  684. goto err_out;
  685. /* init DMA related stuff */
  686. rc = ata_pci_init_bmdma(host);
  687. if (rc)
  688. goto err_bmdma;
  689. devres_remove_group(&pdev->dev, NULL);
  690. *r_host = host;
  691. return 0;
  692. err_bmdma:
  693. /* This is necessary because PCI and iomap resources are
  694. * merged and releasing the top group won't release the
  695. * acquired resources if some of those have been acquired
  696. * before entering this function.
  697. */
  698. pcim_iounmap_regions(pdev, 0xf);
  699. err_out:
  700. devres_release_group(&pdev->dev, NULL);
  701. return rc;
  702. }
  703. struct ata_legacy_devres {
  704. unsigned int mask;
  705. unsigned long cmd_port[2];
  706. void __iomem * cmd_addr[2];
  707. void __iomem * ctl_addr[2];
  708. unsigned int irq[2];
  709. void * irq_dev_id[2];
  710. };
  711. static void ata_legacy_free_irqs(struct ata_legacy_devres *legacy_dr)
  712. {
  713. int i;
  714. for (i = 0; i < 2; i++) {
  715. if (!legacy_dr->irq[i])
  716. continue;
  717. free_irq(legacy_dr->irq[i], legacy_dr->irq_dev_id[i]);
  718. legacy_dr->irq[i] = 0;
  719. legacy_dr->irq_dev_id[i] = NULL;
  720. }
  721. }
  722. static void ata_legacy_release(struct device *gdev, void *res)
  723. {
  724. struct ata_legacy_devres *this = res;
  725. int i;
  726. ata_legacy_free_irqs(this);
  727. for (i = 0; i < 2; i++) {
  728. if (this->cmd_addr[i])
  729. ioport_unmap(this->cmd_addr[i]);
  730. if (this->ctl_addr[i])
  731. ioport_unmap(this->ctl_addr[i]);
  732. if (this->cmd_port[i])
  733. release_region(this->cmd_port[i], 8);
  734. }
  735. }
  736. static int ata_init_legacy_port(struct ata_port *ap,
  737. struct ata_legacy_devres *legacy_dr)
  738. {
  739. struct ata_host *host = ap->host;
  740. int port_no = ap->port_no;
  741. unsigned long cmd_port, ctl_port;
  742. if (port_no == 0) {
  743. cmd_port = ATA_PRIMARY_CMD;
  744. ctl_port = ATA_PRIMARY_CTL;
  745. } else {
  746. cmd_port = ATA_SECONDARY_CMD;
  747. ctl_port = ATA_SECONDARY_CTL;
  748. }
  749. /* request cmd_port */
  750. if (request_region(cmd_port, 8, "libata"))
  751. legacy_dr->cmd_port[port_no] = cmd_port;
  752. else {
  753. dev_printk(KERN_WARNING, host->dev,
  754. "0x%0lX IDE port busy\n", cmd_port);
  755. return -EBUSY;
  756. }
  757. /* iomap cmd and ctl ports */
  758. legacy_dr->cmd_addr[port_no] = ioport_map(cmd_port, 8);
  759. legacy_dr->ctl_addr[port_no] = ioport_map(ctl_port, 1);
  760. if (!legacy_dr->cmd_addr[port_no] || !legacy_dr->ctl_addr[port_no])
  761. return -ENOMEM;
  762. /* init IO addresses */
  763. ap->ioaddr.cmd_addr = legacy_dr->cmd_addr[port_no];
  764. ap->ioaddr.altstatus_addr = legacy_dr->ctl_addr[port_no];
  765. ap->ioaddr.ctl_addr = legacy_dr->ctl_addr[port_no];
  766. ata_std_ports(&ap->ioaddr);
  767. return 0;
  768. }
  769. /**
  770. * ata_init_legacy_host - acquire legacy ATA resources and init ATA host
  771. * @host: target ATA host
  772. * @legacy_mask: out parameter, mask indicating ports is in legacy mode
  773. * @was_busy: out parameter, indicates whether any port was busy
  774. *
  775. * Acquire legacy ATA resources for ports.
  776. *
  777. * LOCKING:
  778. * Inherited from calling layer (may sleep).
  779. *
  780. * RETURNS:
  781. * 0 on success, -errno otherwise.
  782. */
  783. static int ata_init_legacy_host(struct ata_host *host,
  784. unsigned int *legacy_mask, int *was_busy)
  785. {
  786. struct device *gdev = host->dev;
  787. struct ata_legacy_devres *legacy_dr;
  788. int i, rc;
  789. if (!devres_open_group(gdev, NULL, GFP_KERNEL))
  790. return -ENOMEM;
  791. rc = -ENOMEM;
  792. legacy_dr = devres_alloc(ata_legacy_release, sizeof(*legacy_dr),
  793. GFP_KERNEL);
  794. if (!legacy_dr)
  795. goto err_out;
  796. devres_add(gdev, legacy_dr);
  797. for (i = 0; i < 2; i++) {
  798. *legacy_mask &= ~(1 << i);
  799. rc = ata_init_legacy_port(host->ports[i], legacy_dr);
  800. if (rc == 0)
  801. legacy_dr->mask |= 1 << i;
  802. else if (rc == -EBUSY)
  803. (*was_busy)++;
  804. }
  805. if (!legacy_dr->mask)
  806. return -EBUSY;
  807. for (i = 0; i < 2; i++)
  808. if (!(legacy_dr->mask & (1 << i)))
  809. host->ports[i]->ops = &ata_dummy_port_ops;
  810. *legacy_mask |= legacy_dr->mask;
  811. devres_remove_group(gdev, NULL);
  812. return 0;
  813. err_out:
  814. devres_release_group(gdev, NULL);
  815. return rc;
  816. }
  817. /**
  818. * ata_request_legacy_irqs - request legacy ATA IRQs
  819. * @host: target ATA host
  820. * @handler: array of IRQ handlers
  821. * @irq_flags: array of IRQ flags
  822. * @dev_id: array of IRQ dev_ids
  823. *
  824. * Request legacy IRQs for non-dummy legacy ports in @host. All
  825. * IRQ parameters are passed as array to allow ports to have
  826. * separate IRQ handlers.
  827. *
  828. * LOCKING:
  829. * Inherited from calling layer (may sleep).
  830. *
  831. * RETURNS:
  832. * 0 on success, -errno otherwise.
  833. */
  834. static int ata_request_legacy_irqs(struct ata_host *host,
  835. irq_handler_t const *handler,
  836. const unsigned int *irq_flags,
  837. void * const *dev_id)
  838. {
  839. struct device *gdev = host->dev;
  840. struct ata_legacy_devres *legacy_dr;
  841. int i, rc;
  842. legacy_dr = devres_find(host->dev, ata_legacy_release, NULL, NULL);
  843. BUG_ON(!legacy_dr);
  844. for (i = 0; i < 2; i++) {
  845. unsigned int irq;
  846. /* FIXME: ATA_*_IRQ() should take generic device not pci_dev */
  847. if (i == 0)
  848. irq = ATA_PRIMARY_IRQ(to_pci_dev(gdev));
  849. else
  850. irq = ATA_SECONDARY_IRQ(to_pci_dev(gdev));
  851. if (!(legacy_dr->mask & (1 << i)))
  852. continue;
  853. if (!handler[i]) {
  854. dev_printk(KERN_ERR, gdev,
  855. "NULL handler specified for port %d\n", i);
  856. rc = -EINVAL;
  857. goto err_out;
  858. }
  859. rc = request_irq(irq, handler[i], irq_flags[i], DRV_NAME,
  860. dev_id[i]);
  861. if (rc) {
  862. dev_printk(KERN_ERR, gdev,
  863. "irq %u request failed (errno=%d)\n", irq, rc);
  864. goto err_out;
  865. }
  866. /* record irq allocation in legacy_dr */
  867. legacy_dr->irq[i] = irq;
  868. legacy_dr->irq_dev_id[i] = dev_id[i];
  869. /* only used to print info */
  870. if (i == 0)
  871. host->irq = irq;
  872. else
  873. host->irq2 = irq;
  874. }
  875. return 0;
  876. err_out:
  877. ata_legacy_free_irqs(legacy_dr);
  878. return rc;
  879. }
  880. /**
  881. * ata_pci_init_one - Initialize/register PCI IDE host controller
  882. * @pdev: Controller to be initialized
  883. * @port_info: Information from low-level host driver
  884. * @n_ports: Number of ports attached to host controller
  885. *
  886. * This is a helper function which can be called from a driver's
  887. * xxx_init_one() probe function if the hardware uses traditional
  888. * IDE taskfile registers.
  889. *
  890. * This function calls pci_enable_device(), reserves its register
  891. * regions, sets the dma mask, enables bus master mode, and calls
  892. * ata_device_add()
  893. *
  894. * ASSUMPTION:
  895. * Nobody makes a single channel controller that appears solely as
  896. * the secondary legacy port on PCI.
  897. *
  898. * LOCKING:
  899. * Inherited from PCI layer (may sleep).
  900. *
  901. * RETURNS:
  902. * Zero on success, negative on errno-based value on error.
  903. */
  904. int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
  905. unsigned int n_ports)
  906. {
  907. struct device *dev = &pdev->dev;
  908. struct ata_host *host = NULL;
  909. const struct ata_port_info *port[2];
  910. u8 mask;
  911. unsigned int legacy_mode = 0;
  912. int rc;
  913. DPRINTK("ENTER\n");
  914. if (!devres_open_group(dev, NULL, GFP_KERNEL))
  915. return -ENOMEM;
  916. BUG_ON(n_ports < 1 || n_ports > 2);
  917. port[0] = port_info[0];
  918. if (n_ports > 1)
  919. port[1] = port_info[1];
  920. else
  921. port[1] = port[0];
  922. /* FIXME: Really for ATA it isn't safe because the device may be
  923. multi-purpose and we want to leave it alone if it was already
  924. enabled. Secondly for shared use as Arjan says we want refcounting
  925. Checking dev->is_enabled is insufficient as this is not set at
  926. boot for the primary video which is BIOS enabled
  927. */
  928. rc = pcim_enable_device(pdev);
  929. if (rc)
  930. goto err_out;
  931. if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
  932. u8 tmp8;
  933. /* TODO: What if one channel is in native mode ... */
  934. pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
  935. mask = (1 << 2) | (1 << 0);
  936. if ((tmp8 & mask) != mask)
  937. legacy_mode = (1 << 3);
  938. #if defined(CONFIG_NO_ATA_LEGACY)
  939. /* Some platforms with PCI limits cannot address compat
  940. port space. In that case we punt if their firmware has
  941. left a device in compatibility mode */
  942. if (legacy_mode) {
  943. printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
  944. rc = -EOPNOTSUPP;
  945. goto err_out;
  946. }
  947. #endif
  948. }
  949. /* alloc and init host */
  950. host = ata_host_alloc_pinfo(dev, port, 2);
  951. if (!host) {
  952. dev_printk(KERN_ERR, &pdev->dev,
  953. "failed to allocate ATA host\n");
  954. rc = -ENOMEM;
  955. goto err_out;
  956. }
  957. if (!legacy_mode) {
  958. unsigned int port_mask;
  959. port_mask = ATA_PORT_PRIMARY;
  960. if (n_ports > 1)
  961. port_mask |= ATA_PORT_SECONDARY;
  962. rc = ata_pci_init_native_host(host, port_mask);
  963. if (rc)
  964. goto err_out;
  965. } else {
  966. int was_busy = 0;
  967. rc = ata_init_legacy_host(host, &legacy_mode, &was_busy);
  968. if (was_busy)
  969. pcim_pin_device(pdev);
  970. if (rc)
  971. goto err_out;
  972. /* request respective PCI regions, may fail */
  973. rc = pci_request_region(pdev, 1, DRV_NAME);
  974. rc = pci_request_region(pdev, 3, DRV_NAME);
  975. }
  976. /* init BMDMA, may fail */
  977. ata_pci_init_bmdma(host);
  978. pci_set_master(pdev);
  979. /* start host and request IRQ */
  980. rc = ata_host_start(host);
  981. if (rc)
  982. goto err_out;
  983. if (!legacy_mode)
  984. rc = devm_request_irq(dev, pdev->irq,
  985. port_info[0]->port_ops->irq_handler,
  986. IRQF_SHARED, DRV_NAME, host);
  987. else {
  988. irq_handler_t handler[2] = { host->ops->irq_handler,
  989. host->ops->irq_handler };
  990. unsigned int irq_flags[2] = { IRQF_SHARED, IRQF_SHARED };
  991. void *dev_id[2] = { host, host };
  992. rc = ata_request_legacy_irqs(host, handler, irq_flags, dev_id);
  993. }
  994. if (rc)
  995. goto err_out;
  996. /* register */
  997. rc = ata_host_register(host, port_info[0]->sht);
  998. if (rc)
  999. goto err_out;
  1000. devres_remove_group(dev, NULL);
  1001. return 0;
  1002. err_out:
  1003. devres_release_group(dev, NULL);
  1004. return rc;
  1005. }
  1006. /**
  1007. * ata_pci_clear_simplex - attempt to kick device out of simplex
  1008. * @pdev: PCI device
  1009. *
  1010. * Some PCI ATA devices report simplex mode but in fact can be told to
  1011. * enter non simplex mode. This implements the neccessary logic to
  1012. * perform the task on such devices. Calling it on other devices will
  1013. * have -undefined- behaviour.
  1014. */
  1015. int ata_pci_clear_simplex(struct pci_dev *pdev)
  1016. {
  1017. unsigned long bmdma = pci_resource_start(pdev, 4);
  1018. u8 simplex;
  1019. if (bmdma == 0)
  1020. return -ENOENT;
  1021. simplex = inb(bmdma + 0x02);
  1022. outb(simplex & 0x60, bmdma + 0x02);
  1023. simplex = inb(bmdma + 0x02);
  1024. if (simplex & 0x80)
  1025. return -EOPNOTSUPP;
  1026. return 0;
  1027. }
  1028. unsigned long ata_pci_default_filter(struct ata_device *adev, unsigned long xfer_mask)
  1029. {
  1030. /* Filter out DMA modes if the device has been configured by
  1031. the BIOS as PIO only */
  1032. if (adev->ap->ioaddr.bmdma_addr == 0)
  1033. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  1034. return xfer_mask;
  1035. }
  1036. #endif /* CONFIG_PCI */