libata-bmdma.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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/config.h>
  35. #include <linux/kernel.h>
  36. #include <linux/pci.h>
  37. #include <linux/libata.h>
  38. #include "libata.h"
  39. /**
  40. * ata_tf_load_pio - send taskfile registers to host controller
  41. * @ap: Port to which output is sent
  42. * @tf: ATA taskfile register set
  43. *
  44. * Outputs ATA taskfile to standard ATA host controller.
  45. *
  46. * LOCKING:
  47. * Inherited from caller.
  48. */
  49. static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
  50. {
  51. struct ata_ioports *ioaddr = &ap->ioaddr;
  52. unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
  53. if (tf->ctl != ap->last_ctl) {
  54. outb(tf->ctl, ioaddr->ctl_addr);
  55. ap->last_ctl = tf->ctl;
  56. ata_wait_idle(ap);
  57. }
  58. if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
  59. outb(tf->hob_feature, ioaddr->feature_addr);
  60. outb(tf->hob_nsect, ioaddr->nsect_addr);
  61. outb(tf->hob_lbal, ioaddr->lbal_addr);
  62. outb(tf->hob_lbam, ioaddr->lbam_addr);
  63. outb(tf->hob_lbah, ioaddr->lbah_addr);
  64. VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
  65. tf->hob_feature,
  66. tf->hob_nsect,
  67. tf->hob_lbal,
  68. tf->hob_lbam,
  69. tf->hob_lbah);
  70. }
  71. if (is_addr) {
  72. outb(tf->feature, ioaddr->feature_addr);
  73. outb(tf->nsect, ioaddr->nsect_addr);
  74. outb(tf->lbal, ioaddr->lbal_addr);
  75. outb(tf->lbam, ioaddr->lbam_addr);
  76. outb(tf->lbah, ioaddr->lbah_addr);
  77. VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
  78. tf->feature,
  79. tf->nsect,
  80. tf->lbal,
  81. tf->lbam,
  82. tf->lbah);
  83. }
  84. if (tf->flags & ATA_TFLAG_DEVICE) {
  85. outb(tf->device, ioaddr->device_addr);
  86. VPRINTK("device 0x%X\n", tf->device);
  87. }
  88. ata_wait_idle(ap);
  89. }
  90. /**
  91. * ata_tf_load_mmio - 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 using MMIO.
  96. *
  97. * LOCKING:
  98. * Inherited from caller.
  99. */
  100. static void ata_tf_load_mmio(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. writeb(tf->ctl, (void __iomem *) ap->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. writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
  111. writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
  112. writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
  113. writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
  114. writeb(tf->hob_lbah, (void __iomem *) 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. writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
  124. writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
  125. writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
  126. writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
  127. writeb(tf->lbah, (void __iomem *) 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. writeb(tf->device, (void __iomem *) ioaddr->device_addr);
  137. VPRINTK("device 0x%X\n", tf->device);
  138. }
  139. ata_wait_idle(ap);
  140. }
  141. /**
  142. * ata_tf_load - send taskfile registers to host controller
  143. * @ap: Port to which output is sent
  144. * @tf: ATA taskfile register set
  145. *
  146. * Outputs ATA taskfile to standard ATA host controller using MMIO
  147. * or PIO as indicated by the ATA_FLAG_MMIO flag.
  148. * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
  149. * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
  150. * hob_lbal, hob_lbam, and hob_lbah.
  151. *
  152. * This function waits for idle (!BUSY and !DRQ) after writing
  153. * registers. If the control register has a new value, this
  154. * function also waits for idle after writing control and before
  155. * writing the remaining registers.
  156. *
  157. * May be used as the tf_load() entry in ata_port_operations.
  158. *
  159. * LOCKING:
  160. * Inherited from caller.
  161. */
  162. void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
  163. {
  164. if (ap->flags & ATA_FLAG_MMIO)
  165. ata_tf_load_mmio(ap, tf);
  166. else
  167. ata_tf_load_pio(ap, tf);
  168. }
  169. /**
  170. * ata_exec_command_pio - issue ATA command to host controller
  171. * @ap: port to which command is being issued
  172. * @tf: ATA taskfile register set
  173. *
  174. * Issues PIO write to ATA command register, with proper
  175. * synchronization with interrupt handler / other threads.
  176. *
  177. * LOCKING:
  178. * spin_lock_irqsave(host_set lock)
  179. */
  180. static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
  181. {
  182. DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
  183. outb(tf->command, ap->ioaddr.command_addr);
  184. ata_pause(ap);
  185. }
  186. /**
  187. * ata_exec_command_mmio - issue ATA command to host controller
  188. * @ap: port to which command is being issued
  189. * @tf: ATA taskfile register set
  190. *
  191. * Issues MMIO write to ATA command register, with proper
  192. * synchronization with interrupt handler / other threads.
  193. *
  194. * LOCKING:
  195. * spin_lock_irqsave(host_set lock)
  196. */
  197. static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
  198. {
  199. DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
  200. writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
  201. ata_pause(ap);
  202. }
  203. /**
  204. * ata_exec_command - issue ATA command to host controller
  205. * @ap: port to which command is being issued
  206. * @tf: ATA taskfile register set
  207. *
  208. * Issues PIO/MMIO write to ATA command register, with proper
  209. * synchronization with interrupt handler / other threads.
  210. *
  211. * LOCKING:
  212. * spin_lock_irqsave(host_set lock)
  213. */
  214. void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
  215. {
  216. if (ap->flags & ATA_FLAG_MMIO)
  217. ata_exec_command_mmio(ap, tf);
  218. else
  219. ata_exec_command_pio(ap, tf);
  220. }
  221. /**
  222. * ata_tf_read_pio - input device's ATA taskfile shadow registers
  223. * @ap: Port from which input is read
  224. * @tf: ATA taskfile register set for storing input
  225. *
  226. * Reads ATA taskfile registers for currently-selected device
  227. * into @tf.
  228. *
  229. * LOCKING:
  230. * Inherited from caller.
  231. */
  232. static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
  233. {
  234. struct ata_ioports *ioaddr = &ap->ioaddr;
  235. tf->command = ata_check_status(ap);
  236. tf->feature = inb(ioaddr->error_addr);
  237. tf->nsect = inb(ioaddr->nsect_addr);
  238. tf->lbal = inb(ioaddr->lbal_addr);
  239. tf->lbam = inb(ioaddr->lbam_addr);
  240. tf->lbah = inb(ioaddr->lbah_addr);
  241. tf->device = inb(ioaddr->device_addr);
  242. if (tf->flags & ATA_TFLAG_LBA48) {
  243. outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
  244. tf->hob_feature = inb(ioaddr->error_addr);
  245. tf->hob_nsect = inb(ioaddr->nsect_addr);
  246. tf->hob_lbal = inb(ioaddr->lbal_addr);
  247. tf->hob_lbam = inb(ioaddr->lbam_addr);
  248. tf->hob_lbah = inb(ioaddr->lbah_addr);
  249. }
  250. }
  251. /**
  252. * ata_tf_read_mmio - input device's ATA taskfile shadow registers
  253. * @ap: Port from which input is read
  254. * @tf: ATA taskfile register set for storing input
  255. *
  256. * Reads ATA taskfile registers for currently-selected device
  257. * into @tf via MMIO.
  258. *
  259. * LOCKING:
  260. * Inherited from caller.
  261. */
  262. static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
  263. {
  264. struct ata_ioports *ioaddr = &ap->ioaddr;
  265. tf->command = ata_check_status(ap);
  266. tf->feature = readb((void __iomem *)ioaddr->error_addr);
  267. tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
  268. tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
  269. tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
  270. tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
  271. tf->device = readb((void __iomem *)ioaddr->device_addr);
  272. if (tf->flags & ATA_TFLAG_LBA48) {
  273. writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
  274. tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
  275. tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
  276. tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
  277. tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
  278. tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
  279. }
  280. }
  281. /**
  282. * ata_tf_read - input device's ATA taskfile shadow registers
  283. * @ap: Port from which input is read
  284. * @tf: ATA taskfile register set for storing input
  285. *
  286. * Reads ATA taskfile registers for currently-selected device
  287. * into @tf.
  288. *
  289. * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
  290. * is set, also reads the hob registers.
  291. *
  292. * May be used as the tf_read() entry in ata_port_operations.
  293. *
  294. * LOCKING:
  295. * Inherited from caller.
  296. */
  297. void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  298. {
  299. if (ap->flags & ATA_FLAG_MMIO)
  300. ata_tf_read_mmio(ap, tf);
  301. else
  302. ata_tf_read_pio(ap, tf);
  303. }
  304. /**
  305. * ata_check_status_pio - Read device status reg & clear interrupt
  306. * @ap: port where the device is
  307. *
  308. * Reads ATA taskfile status register for currently-selected device
  309. * and return its value. This also clears pending interrupts
  310. * from this device
  311. *
  312. * LOCKING:
  313. * Inherited from caller.
  314. */
  315. static u8 ata_check_status_pio(struct ata_port *ap)
  316. {
  317. return inb(ap->ioaddr.status_addr);
  318. }
  319. /**
  320. * ata_check_status_mmio - Read device status reg & clear interrupt
  321. * @ap: port where the device is
  322. *
  323. * Reads ATA taskfile status register for currently-selected device
  324. * via MMIO and return its value. This also clears pending interrupts
  325. * from this device
  326. *
  327. * LOCKING:
  328. * Inherited from caller.
  329. */
  330. static u8 ata_check_status_mmio(struct ata_port *ap)
  331. {
  332. return readb((void __iomem *) ap->ioaddr.status_addr);
  333. }
  334. /**
  335. * ata_check_status - Read device status reg & clear interrupt
  336. * @ap: port where the device is
  337. *
  338. * Reads ATA taskfile status register for currently-selected device
  339. * and return its value. This also clears pending interrupts
  340. * from this device
  341. *
  342. * May be used as the check_status() entry in ata_port_operations.
  343. *
  344. * LOCKING:
  345. * Inherited from caller.
  346. */
  347. u8 ata_check_status(struct ata_port *ap)
  348. {
  349. if (ap->flags & ATA_FLAG_MMIO)
  350. return ata_check_status_mmio(ap);
  351. return ata_check_status_pio(ap);
  352. }
  353. /**
  354. * ata_altstatus - Read device alternate status reg
  355. * @ap: port where the device is
  356. *
  357. * Reads ATA taskfile alternate status register for
  358. * currently-selected device and return its value.
  359. *
  360. * Note: may NOT be used as the check_altstatus() entry in
  361. * ata_port_operations.
  362. *
  363. * LOCKING:
  364. * Inherited from caller.
  365. */
  366. u8 ata_altstatus(struct ata_port *ap)
  367. {
  368. if (ap->ops->check_altstatus)
  369. return ap->ops->check_altstatus(ap);
  370. if (ap->flags & ATA_FLAG_MMIO)
  371. return readb((void __iomem *)ap->ioaddr.altstatus_addr);
  372. return inb(ap->ioaddr.altstatus_addr);
  373. }
  374. #ifdef CONFIG_PCI
  375. static struct ata_probe_ent *
  376. ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
  377. {
  378. struct ata_probe_ent *probe_ent;
  379. probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
  380. if (!probe_ent) {
  381. printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
  382. kobject_name(&(dev->kobj)));
  383. return NULL;
  384. }
  385. INIT_LIST_HEAD(&probe_ent->node);
  386. probe_ent->dev = dev;
  387. probe_ent->sht = port->sht;
  388. probe_ent->host_flags = port->host_flags;
  389. probe_ent->pio_mask = port->pio_mask;
  390. probe_ent->mwdma_mask = port->mwdma_mask;
  391. probe_ent->udma_mask = port->udma_mask;
  392. probe_ent->port_ops = port->port_ops;
  393. return probe_ent;
  394. }
  395. /**
  396. * ata_pci_init_native_mode - Initialize native-mode driver
  397. * @pdev: pci device to be initialized
  398. * @port: array[2] of pointers to port info structures.
  399. * @ports: bitmap of ports present
  400. *
  401. * Utility function which allocates and initializes an
  402. * ata_probe_ent structure for a standard dual-port
  403. * PIO-based IDE controller. The returned ata_probe_ent
  404. * structure can be passed to ata_device_add(). The returned
  405. * ata_probe_ent structure should then be freed with kfree().
  406. *
  407. * The caller need only pass the address of the primary port, the
  408. * secondary will be deduced automatically. If the device has non
  409. * standard secondary port mappings this function can be called twice,
  410. * once for each interface.
  411. */
  412. struct ata_probe_ent *
  413. ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
  414. {
  415. struct ata_probe_ent *probe_ent =
  416. ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
  417. int p = 0;
  418. if (!probe_ent)
  419. return NULL;
  420. probe_ent->irq = pdev->irq;
  421. probe_ent->irq_flags = SA_SHIRQ;
  422. probe_ent->private_data = port[0]->private_data;
  423. if (ports & ATA_PORT_PRIMARY) {
  424. probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
  425. probe_ent->port[p].altstatus_addr =
  426. probe_ent->port[p].ctl_addr =
  427. pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
  428. probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
  429. ata_std_ports(&probe_ent->port[p]);
  430. p++;
  431. }
  432. if (ports & ATA_PORT_SECONDARY) {
  433. probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
  434. probe_ent->port[p].altstatus_addr =
  435. probe_ent->port[p].ctl_addr =
  436. pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
  437. probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
  438. ata_std_ports(&probe_ent->port[p]);
  439. p++;
  440. }
  441. probe_ent->n_ports = p;
  442. return probe_ent;
  443. }
  444. static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev,
  445. struct ata_port_info *port, int port_num)
  446. {
  447. struct ata_probe_ent *probe_ent;
  448. probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
  449. if (!probe_ent)
  450. return NULL;
  451. probe_ent->legacy_mode = 1;
  452. probe_ent->n_ports = 1;
  453. probe_ent->hard_port_no = port_num;
  454. probe_ent->private_data = port->private_data;
  455. switch(port_num)
  456. {
  457. case 0:
  458. probe_ent->irq = 14;
  459. probe_ent->port[0].cmd_addr = 0x1f0;
  460. probe_ent->port[0].altstatus_addr =
  461. probe_ent->port[0].ctl_addr = 0x3f6;
  462. break;
  463. case 1:
  464. probe_ent->irq = 15;
  465. probe_ent->port[0].cmd_addr = 0x170;
  466. probe_ent->port[0].altstatus_addr =
  467. probe_ent->port[0].ctl_addr = 0x376;
  468. break;
  469. }
  470. probe_ent->port[0].bmdma_addr =
  471. pci_resource_start(pdev, 4) + 8 * port_num;
  472. ata_std_ports(&probe_ent->port[0]);
  473. return probe_ent;
  474. }
  475. /**
  476. * ata_pci_init_one - Initialize/register PCI IDE host controller
  477. * @pdev: Controller to be initialized
  478. * @port_info: Information from low-level host driver
  479. * @n_ports: Number of ports attached to host controller
  480. *
  481. * This is a helper function which can be called from a driver's
  482. * xxx_init_one() probe function if the hardware uses traditional
  483. * IDE taskfile registers.
  484. *
  485. * This function calls pci_enable_device(), reserves its register
  486. * regions, sets the dma mask, enables bus master mode, and calls
  487. * ata_device_add()
  488. *
  489. * LOCKING:
  490. * Inherited from PCI layer (may sleep).
  491. *
  492. * RETURNS:
  493. * Zero on success, negative on errno-based value on error.
  494. */
  495. int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
  496. unsigned int n_ports)
  497. {
  498. struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
  499. struct ata_port_info *port[2];
  500. u8 tmp8, mask;
  501. unsigned int legacy_mode = 0;
  502. int disable_dev_on_err = 1;
  503. int rc;
  504. DPRINTK("ENTER\n");
  505. port[0] = port_info[0];
  506. if (n_ports > 1)
  507. port[1] = port_info[1];
  508. else
  509. port[1] = port[0];
  510. if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
  511. && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
  512. /* TODO: What if one channel is in native mode ... */
  513. pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
  514. mask = (1 << 2) | (1 << 0);
  515. if ((tmp8 & mask) != mask)
  516. legacy_mode = (1 << 3);
  517. }
  518. /* FIXME... */
  519. if ((!legacy_mode) && (n_ports > 2)) {
  520. printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
  521. n_ports = 2;
  522. /* For now */
  523. }
  524. /* FIXME: Really for ATA it isn't safe because the device may be
  525. multi-purpose and we want to leave it alone if it was already
  526. enabled. Secondly for shared use as Arjan says we want refcounting
  527. Checking dev->is_enabled is insufficient as this is not set at
  528. boot for the primary video which is BIOS enabled
  529. */
  530. rc = pci_enable_device(pdev);
  531. if (rc)
  532. return rc;
  533. rc = pci_request_regions(pdev, DRV_NAME);
  534. if (rc) {
  535. disable_dev_on_err = 0;
  536. goto err_out;
  537. }
  538. /* FIXME: Should use platform specific mappers for legacy port ranges */
  539. if (legacy_mode) {
  540. if (!request_region(0x1f0, 8, "libata")) {
  541. struct resource *conflict, res;
  542. res.start = 0x1f0;
  543. res.end = 0x1f0 + 8 - 1;
  544. conflict = ____request_resource(&ioport_resource, &res);
  545. if (!strcmp(conflict->name, "libata"))
  546. legacy_mode |= (1 << 0);
  547. else {
  548. disable_dev_on_err = 0;
  549. printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
  550. }
  551. } else
  552. legacy_mode |= (1 << 0);
  553. if (!request_region(0x170, 8, "libata")) {
  554. struct resource *conflict, res;
  555. res.start = 0x170;
  556. res.end = 0x170 + 8 - 1;
  557. conflict = ____request_resource(&ioport_resource, &res);
  558. if (!strcmp(conflict->name, "libata"))
  559. legacy_mode |= (1 << 1);
  560. else {
  561. disable_dev_on_err = 0;
  562. printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
  563. }
  564. } else
  565. legacy_mode |= (1 << 1);
  566. }
  567. /* we have legacy mode, but all ports are unavailable */
  568. if (legacy_mode == (1 << 3)) {
  569. rc = -EBUSY;
  570. goto err_out_regions;
  571. }
  572. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  573. if (rc)
  574. goto err_out_regions;
  575. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  576. if (rc)
  577. goto err_out_regions;
  578. if (legacy_mode) {
  579. if (legacy_mode & (1 << 0))
  580. probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
  581. if (legacy_mode & (1 << 1))
  582. probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
  583. } else {
  584. if (n_ports == 2)
  585. probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
  586. else
  587. probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
  588. }
  589. if (!probe_ent && !probe_ent2) {
  590. rc = -ENOMEM;
  591. goto err_out_regions;
  592. }
  593. pci_set_master(pdev);
  594. /* FIXME: check ata_device_add return */
  595. if (legacy_mode) {
  596. if (legacy_mode & (1 << 0))
  597. ata_device_add(probe_ent);
  598. if (legacy_mode & (1 << 1))
  599. ata_device_add(probe_ent2);
  600. } else
  601. ata_device_add(probe_ent);
  602. kfree(probe_ent);
  603. kfree(probe_ent2);
  604. return 0;
  605. err_out_regions:
  606. if (legacy_mode & (1 << 0))
  607. release_region(0x1f0, 8);
  608. if (legacy_mode & (1 << 1))
  609. release_region(0x170, 8);
  610. pci_release_regions(pdev);
  611. err_out:
  612. if (disable_dev_on_err)
  613. pci_disable_device(pdev);
  614. return rc;
  615. }
  616. #endif /* CONFIG_PCI */