aic7xxx_osm_pci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * Linux driver attachment glue for PCI based controllers.
  3. *
  4. * Copyright (c) 2000-2001 Adaptec Inc.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions, and the following disclaimer,
  12. * without modification.
  13. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  14. * substantially similar to the "NO WARRANTY" disclaimer below
  15. * ("Disclaimer") and any redistribution must be conditioned upon
  16. * including a substantially similar Disclaimer requirement for further
  17. * binary redistribution.
  18. * 3. Neither the names of the above-listed copyright holders nor the names
  19. * of any contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * Alternatively, this software may be distributed under the terms of the
  23. * GNU General Public License ("GPL") version 2 as published by the Free
  24. * Software Foundation.
  25. *
  26. * NO WARRANTY
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  30. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  33. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  35. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  36. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGES.
  38. *
  39. * $Id: //depot/aic7xxx/linux/drivers/scsi/aic7xxx/aic7xxx_osm_pci.c#47 $
  40. */
  41. #include "aic7xxx_osm.h"
  42. #include "aic7xxx_pci.h"
  43. /* Define the macro locally since it's different for different class of chips.
  44. */
  45. #define ID(x) ID_C(x, PCI_CLASS_STORAGE_SCSI)
  46. static const struct pci_device_id ahc_linux_pci_id_table[] = {
  47. /* aic7850 based controllers */
  48. ID(ID_AHA_2902_04_10_15_20C_30C),
  49. /* aic7860 based controllers */
  50. ID(ID_AHA_2930CU),
  51. ID(ID_AHA_1480A & ID_DEV_VENDOR_MASK),
  52. ID(ID_AHA_2940AU_0 & ID_DEV_VENDOR_MASK),
  53. ID(ID_AHA_2940AU_CN & ID_DEV_VENDOR_MASK),
  54. ID(ID_AHA_2930C_VAR & ID_DEV_VENDOR_MASK),
  55. /* aic7870 based controllers */
  56. ID(ID_AHA_2940),
  57. ID(ID_AHA_3940),
  58. ID(ID_AHA_398X),
  59. ID(ID_AHA_2944),
  60. ID(ID_AHA_3944),
  61. ID(ID_AHA_4944),
  62. /* aic7880 based controllers */
  63. ID(ID_AHA_2940U & ID_DEV_VENDOR_MASK),
  64. ID(ID_AHA_3940U & ID_DEV_VENDOR_MASK),
  65. ID(ID_AHA_2944U & ID_DEV_VENDOR_MASK),
  66. ID(ID_AHA_3944U & ID_DEV_VENDOR_MASK),
  67. ID(ID_AHA_398XU & ID_DEV_VENDOR_MASK),
  68. ID(ID_AHA_4944U & ID_DEV_VENDOR_MASK),
  69. ID(ID_AHA_2930U & ID_DEV_VENDOR_MASK),
  70. ID(ID_AHA_2940U_PRO & ID_DEV_VENDOR_MASK),
  71. ID(ID_AHA_2940U_CN & ID_DEV_VENDOR_MASK),
  72. /* aic7890 based controllers */
  73. ID(ID_AHA_2930U2),
  74. ID(ID_AHA_2940U2B),
  75. ID(ID_AHA_2940U2_OEM),
  76. ID(ID_AHA_2940U2),
  77. ID(ID_AHA_2950U2B),
  78. ID16(ID_AIC7890_ARO & ID_AIC7895_ARO_MASK),
  79. ID(ID_AAA_131U2),
  80. /* aic7890 based controllers */
  81. ID(ID_AHA_29160),
  82. ID(ID_AHA_29160_CPQ),
  83. ID(ID_AHA_29160N),
  84. ID(ID_AHA_29160C),
  85. ID(ID_AHA_29160B),
  86. ID(ID_AHA_19160B),
  87. ID(ID_AIC7892_ARO),
  88. /* aic7892 based controllers */
  89. ID(ID_AHA_2940U_DUAL),
  90. ID(ID_AHA_3940AU),
  91. ID(ID_AHA_3944AU),
  92. ID(ID_AIC7895_ARO),
  93. ID(ID_AHA_3950U2B_0),
  94. ID(ID_AHA_3950U2B_1),
  95. ID(ID_AHA_3950U2D_0),
  96. ID(ID_AHA_3950U2D_1),
  97. ID(ID_AIC7896_ARO),
  98. /* aic7899 based controllers */
  99. ID(ID_AHA_3960D),
  100. ID(ID_AHA_3960D_CPQ),
  101. ID(ID_AIC7899_ARO),
  102. /* Generic chip probes for devices we don't know exactly. */
  103. ID(ID_AIC7850 & ID_DEV_VENDOR_MASK),
  104. ID(ID_AIC7855 & ID_DEV_VENDOR_MASK),
  105. ID(ID_AIC7859 & ID_DEV_VENDOR_MASK),
  106. ID(ID_AIC7860 & ID_DEV_VENDOR_MASK),
  107. ID(ID_AIC7870 & ID_DEV_VENDOR_MASK),
  108. ID(ID_AIC7880 & ID_DEV_VENDOR_MASK),
  109. ID16(ID_AIC7890 & ID_9005_GENERIC_MASK),
  110. ID16(ID_AIC7892 & ID_9005_GENERIC_MASK),
  111. ID(ID_AIC7895 & ID_DEV_VENDOR_MASK),
  112. ID16(ID_AIC7896 & ID_9005_GENERIC_MASK),
  113. ID16(ID_AIC7899 & ID_9005_GENERIC_MASK),
  114. ID(ID_AIC7810 & ID_DEV_VENDOR_MASK),
  115. ID(ID_AIC7815 & ID_DEV_VENDOR_MASK),
  116. { 0 }
  117. };
  118. MODULE_DEVICE_TABLE(pci, ahc_linux_pci_id_table);
  119. #ifdef CONFIG_PM
  120. static int
  121. ahc_linux_pci_dev_suspend(struct pci_dev *pdev, pm_message_t mesg)
  122. {
  123. struct ahc_softc *ahc = pci_get_drvdata(pdev);
  124. int rc;
  125. if ((rc = ahc_suspend(ahc)))
  126. return rc;
  127. pci_save_state(pdev);
  128. pci_disable_device(pdev);
  129. if (mesg.event & PM_EVENT_SLEEP)
  130. pci_set_power_state(pdev, PCI_D3hot);
  131. return rc;
  132. }
  133. static int
  134. ahc_linux_pci_dev_resume(struct pci_dev *pdev)
  135. {
  136. struct ahc_softc *ahc = pci_get_drvdata(pdev);
  137. int rc;
  138. pci_set_power_state(pdev, PCI_D0);
  139. pci_restore_state(pdev);
  140. if ((rc = pci_enable_device(pdev))) {
  141. dev_printk(KERN_ERR, &pdev->dev,
  142. "failed to enable device after resume (%d)\n", rc);
  143. return rc;
  144. }
  145. pci_set_master(pdev);
  146. ahc_pci_resume(ahc);
  147. return (ahc_resume(ahc));
  148. }
  149. #endif
  150. static void
  151. ahc_linux_pci_dev_remove(struct pci_dev *pdev)
  152. {
  153. struct ahc_softc *ahc = pci_get_drvdata(pdev);
  154. u_long s;
  155. if (ahc->platform_data && ahc->platform_data->host)
  156. scsi_remove_host(ahc->platform_data->host);
  157. ahc_lock(ahc, &s);
  158. ahc_intr_enable(ahc, FALSE);
  159. ahc_unlock(ahc, &s);
  160. ahc_free(ahc);
  161. }
  162. static void
  163. ahc_linux_pci_inherit_flags(struct ahc_softc *ahc)
  164. {
  165. struct pci_dev *pdev = ahc->dev_softc, *master_pdev;
  166. unsigned int master_devfn = PCI_DEVFN(PCI_SLOT(pdev->devfn), 0);
  167. master_pdev = pci_get_slot(pdev->bus, master_devfn);
  168. if (master_pdev) {
  169. struct ahc_softc *master = pci_get_drvdata(master_pdev);
  170. if (master) {
  171. ahc->flags &= ~AHC_BIOS_ENABLED;
  172. ahc->flags |= master->flags & AHC_BIOS_ENABLED;
  173. ahc->flags &= ~AHC_PRIMARY_CHANNEL;
  174. ahc->flags |= master->flags & AHC_PRIMARY_CHANNEL;
  175. } else
  176. printk(KERN_ERR "aic7xxx: no multichannel peer found!\n");
  177. pci_dev_put(master_pdev);
  178. }
  179. }
  180. static int
  181. ahc_linux_pci_dev_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  182. {
  183. char buf[80];
  184. const uint64_t mask_39bit = 0x7FFFFFFFFFULL;
  185. struct ahc_softc *ahc;
  186. ahc_dev_softc_t pci;
  187. const struct ahc_pci_identity *entry;
  188. char *name;
  189. int error;
  190. struct device *dev = &pdev->dev;
  191. pci = pdev;
  192. entry = ahc_find_pci_device(pci);
  193. if (entry == NULL)
  194. return (-ENODEV);
  195. /*
  196. * Allocate a softc for this card and
  197. * set it up for attachment by our
  198. * common detect routine.
  199. */
  200. sprintf(buf, "ahc_pci:%d:%d:%d",
  201. ahc_get_pci_bus(pci),
  202. ahc_get_pci_slot(pci),
  203. ahc_get_pci_function(pci));
  204. name = kmalloc(strlen(buf) + 1, GFP_ATOMIC);
  205. if (name == NULL)
  206. return (-ENOMEM);
  207. strcpy(name, buf);
  208. ahc = ahc_alloc(NULL, name);
  209. if (ahc == NULL)
  210. return (-ENOMEM);
  211. if (pci_enable_device(pdev)) {
  212. ahc_free(ahc);
  213. return (-ENODEV);
  214. }
  215. pci_set_master(pdev);
  216. if (sizeof(dma_addr_t) > 4
  217. && ahc->features & AHC_LARGE_SCBS
  218. && dma_set_mask(dev, mask_39bit) == 0
  219. && dma_get_required_mask(dev) > DMA_BIT_MASK(32)) {
  220. ahc->flags |= AHC_39BIT_ADDRESSING;
  221. } else {
  222. if (dma_set_mask(dev, DMA_BIT_MASK(32))) {
  223. ahc_free(ahc);
  224. printk(KERN_WARNING "aic7xxx: No suitable DMA available.\n");
  225. return (-ENODEV);
  226. }
  227. }
  228. ahc->dev_softc = pci;
  229. error = ahc_pci_config(ahc, entry);
  230. if (error != 0) {
  231. ahc_free(ahc);
  232. return (-error);
  233. }
  234. /*
  235. * Second Function PCI devices need to inherit some
  236. * settings from function 0.
  237. */
  238. if ((ahc->features & AHC_MULTI_FUNC) && PCI_FUNC(pdev->devfn) != 0)
  239. ahc_linux_pci_inherit_flags(ahc);
  240. pci_set_drvdata(pdev, ahc);
  241. ahc_linux_register_host(ahc, &aic7xxx_driver_template);
  242. return (0);
  243. }
  244. /******************************* PCI Routines *********************************/
  245. uint32_t
  246. ahc_pci_read_config(ahc_dev_softc_t pci, int reg, int width)
  247. {
  248. switch (width) {
  249. case 1:
  250. {
  251. uint8_t retval;
  252. pci_read_config_byte(pci, reg, &retval);
  253. return (retval);
  254. }
  255. case 2:
  256. {
  257. uint16_t retval;
  258. pci_read_config_word(pci, reg, &retval);
  259. return (retval);
  260. }
  261. case 4:
  262. {
  263. uint32_t retval;
  264. pci_read_config_dword(pci, reg, &retval);
  265. return (retval);
  266. }
  267. default:
  268. panic("ahc_pci_read_config: Read size too big");
  269. /* NOTREACHED */
  270. return (0);
  271. }
  272. }
  273. void
  274. ahc_pci_write_config(ahc_dev_softc_t pci, int reg, uint32_t value, int width)
  275. {
  276. switch (width) {
  277. case 1:
  278. pci_write_config_byte(pci, reg, value);
  279. break;
  280. case 2:
  281. pci_write_config_word(pci, reg, value);
  282. break;
  283. case 4:
  284. pci_write_config_dword(pci, reg, value);
  285. break;
  286. default:
  287. panic("ahc_pci_write_config: Write size too big");
  288. /* NOTREACHED */
  289. }
  290. }
  291. static struct pci_driver aic7xxx_pci_driver = {
  292. .name = "aic7xxx",
  293. .probe = ahc_linux_pci_dev_probe,
  294. #ifdef CONFIG_PM
  295. .suspend = ahc_linux_pci_dev_suspend,
  296. .resume = ahc_linux_pci_dev_resume,
  297. #endif
  298. .remove = ahc_linux_pci_dev_remove,
  299. .id_table = ahc_linux_pci_id_table
  300. };
  301. int
  302. ahc_linux_pci_init(void)
  303. {
  304. return pci_register_driver(&aic7xxx_pci_driver);
  305. }
  306. void
  307. ahc_linux_pci_exit(void)
  308. {
  309. pci_unregister_driver(&aic7xxx_pci_driver);
  310. }
  311. static int
  312. ahc_linux_pci_reserve_io_region(struct ahc_softc *ahc, resource_size_t *base)
  313. {
  314. if (aic7xxx_allow_memio == 0)
  315. return (ENOMEM);
  316. *base = pci_resource_start(ahc->dev_softc, 0);
  317. if (*base == 0)
  318. return (ENOMEM);
  319. if (!request_region(*base, 256, "aic7xxx"))
  320. return (ENOMEM);
  321. return (0);
  322. }
  323. static int
  324. ahc_linux_pci_reserve_mem_region(struct ahc_softc *ahc,
  325. resource_size_t *bus_addr,
  326. uint8_t __iomem **maddr)
  327. {
  328. resource_size_t start;
  329. int error;
  330. error = 0;
  331. start = pci_resource_start(ahc->dev_softc, 1);
  332. if (start != 0) {
  333. *bus_addr = start;
  334. if (!request_mem_region(start, 0x1000, "aic7xxx"))
  335. error = ENOMEM;
  336. if (error == 0) {
  337. *maddr = ioremap_nocache(start, 256);
  338. if (*maddr == NULL) {
  339. error = ENOMEM;
  340. release_mem_region(start, 0x1000);
  341. }
  342. }
  343. } else
  344. error = ENOMEM;
  345. return (error);
  346. }
  347. int
  348. ahc_pci_map_registers(struct ahc_softc *ahc)
  349. {
  350. uint32_t command;
  351. resource_size_t base;
  352. uint8_t __iomem *maddr;
  353. int error;
  354. /*
  355. * If its allowed, we prefer memory mapped access.
  356. */
  357. command = ahc_pci_read_config(ahc->dev_softc, PCIR_COMMAND, 4);
  358. command &= ~(PCIM_CMD_PORTEN|PCIM_CMD_MEMEN);
  359. base = 0;
  360. maddr = NULL;
  361. error = ahc_linux_pci_reserve_mem_region(ahc, &base, &maddr);
  362. if (error == 0) {
  363. ahc->platform_data->mem_busaddr = base;
  364. ahc->tag = BUS_SPACE_MEMIO;
  365. ahc->bsh.maddr = maddr;
  366. ahc_pci_write_config(ahc->dev_softc, PCIR_COMMAND,
  367. command | PCIM_CMD_MEMEN, 4);
  368. /*
  369. * Do a quick test to see if memory mapped
  370. * I/O is functioning correctly.
  371. */
  372. if (ahc_pci_test_register_access(ahc) != 0) {
  373. printk("aic7xxx: PCI Device %d:%d:%d "
  374. "failed memory mapped test. Using PIO.\n",
  375. ahc_get_pci_bus(ahc->dev_softc),
  376. ahc_get_pci_slot(ahc->dev_softc),
  377. ahc_get_pci_function(ahc->dev_softc));
  378. iounmap(maddr);
  379. release_mem_region(ahc->platform_data->mem_busaddr,
  380. 0x1000);
  381. ahc->bsh.maddr = NULL;
  382. maddr = NULL;
  383. } else
  384. command |= PCIM_CMD_MEMEN;
  385. } else {
  386. printk("aic7xxx: PCI%d:%d:%d MEM region 0x%llx "
  387. "unavailable. Cannot memory map device.\n",
  388. ahc_get_pci_bus(ahc->dev_softc),
  389. ahc_get_pci_slot(ahc->dev_softc),
  390. ahc_get_pci_function(ahc->dev_softc),
  391. (unsigned long long)base);
  392. }
  393. /*
  394. * We always prefer memory mapped access.
  395. */
  396. if (maddr == NULL) {
  397. error = ahc_linux_pci_reserve_io_region(ahc, &base);
  398. if (error == 0) {
  399. ahc->tag = BUS_SPACE_PIO;
  400. ahc->bsh.ioport = (u_long)base;
  401. command |= PCIM_CMD_PORTEN;
  402. } else {
  403. printk("aic7xxx: PCI%d:%d:%d IO region 0x%llx[0..255] "
  404. "unavailable. Cannot map device.\n",
  405. ahc_get_pci_bus(ahc->dev_softc),
  406. ahc_get_pci_slot(ahc->dev_softc),
  407. ahc_get_pci_function(ahc->dev_softc),
  408. (unsigned long long)base);
  409. }
  410. }
  411. ahc_pci_write_config(ahc->dev_softc, PCIR_COMMAND, command, 4);
  412. return (error);
  413. }
  414. int
  415. ahc_pci_map_int(struct ahc_softc *ahc)
  416. {
  417. int error;
  418. error = request_irq(ahc->dev_softc->irq, ahc_linux_isr,
  419. IRQF_SHARED, "aic7xxx", ahc);
  420. if (error == 0)
  421. ahc->platform_data->irq = ahc->dev_softc->irq;
  422. return (-error);
  423. }