aic79xx_pci.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. /*
  2. * Product specific probe and attach routines for:
  3. * aic7901 and aic7902 SCSI controllers
  4. *
  5. * Copyright (c) 1994-2001 Justin T. Gibbs.
  6. * Copyright (c) 2000-2002 Adaptec Inc.
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions, and the following disclaimer,
  14. * without modification.
  15. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  16. * substantially similar to the "NO WARRANTY" disclaimer below
  17. * ("Disclaimer") and any redistribution must be conditioned upon
  18. * including a substantially similar Disclaimer requirement for further
  19. * binary redistribution.
  20. * 3. Neither the names of the above-listed copyright holders nor the names
  21. * of any contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * Alternatively, this software may be distributed under the terms of the
  25. * GNU General Public License ("GPL") version 2 as published by the Free
  26. * Software Foundation.
  27. *
  28. * NO WARRANTY
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  32. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  34. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  35. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  36. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  37. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  38. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  39. * POSSIBILITY OF SUCH DAMAGES.
  40. *
  41. * $Id: //depot/aic7xxx/aic7xxx/aic79xx_pci.c#77 $
  42. *
  43. * $FreeBSD$
  44. */
  45. #ifdef __linux__
  46. #include "aic79xx_osm.h"
  47. #include "aic79xx_inline.h"
  48. #else
  49. #include <dev/aic7xxx/aic79xx_osm.h>
  50. #include <dev/aic7xxx/aic79xx_inline.h>
  51. #endif
  52. #include "aic79xx_pci.h"
  53. static __inline uint64_t
  54. ahd_compose_id(u_int device, u_int vendor, u_int subdevice, u_int subvendor)
  55. {
  56. uint64_t id;
  57. id = subvendor
  58. | (subdevice << 16)
  59. | ((uint64_t)vendor << 32)
  60. | ((uint64_t)device << 48);
  61. return (id);
  62. }
  63. #define ID_AIC7902_PCI_REV_A4 0x3
  64. #define ID_AIC7902_PCI_REV_B0 0x10
  65. #define SUBID_HP 0x0E11
  66. #define DEVID_9005_HOSTRAID(id) ((id) & 0x80)
  67. #define DEVID_9005_TYPE(id) ((id) & 0xF)
  68. #define DEVID_9005_TYPE_HBA 0x0 /* Standard Card */
  69. #define DEVID_9005_TYPE_HBA_2EXT 0x1 /* 2 External Ports */
  70. #define DEVID_9005_TYPE_IROC 0x8 /* Raid(0,1,10) Card */
  71. #define DEVID_9005_TYPE_MB 0xF /* On Motherboard */
  72. #define DEVID_9005_MFUNC(id) ((id) & 0x10)
  73. #define DEVID_9005_PACKETIZED(id) ((id) & 0x8000)
  74. #define SUBID_9005_TYPE(id) ((id) & 0xF)
  75. #define SUBID_9005_TYPE_HBA 0x0 /* Standard Card */
  76. #define SUBID_9005_TYPE_MB 0xF /* On Motherboard */
  77. #define SUBID_9005_AUTOTERM(id) (((id) & 0x10) == 0)
  78. #define SUBID_9005_LEGACYCONN_FUNC(id) ((id) & 0x20)
  79. #define SUBID_9005_SEEPTYPE(id) ((id) & 0x0C0) >> 6)
  80. #define SUBID_9005_SEEPTYPE_NONE 0x0
  81. #define SUBID_9005_SEEPTYPE_4K 0x1
  82. static ahd_device_setup_t ahd_aic7901_setup;
  83. static ahd_device_setup_t ahd_aic7901A_setup;
  84. static ahd_device_setup_t ahd_aic7902_setup;
  85. static ahd_device_setup_t ahd_aic790X_setup;
  86. struct ahd_pci_identity ahd_pci_ident_table [] =
  87. {
  88. /* aic7901 based controllers */
  89. {
  90. ID_AHA_29320A,
  91. ID_ALL_MASK,
  92. "Adaptec 29320A Ultra320 SCSI adapter",
  93. ahd_aic7901_setup
  94. },
  95. {
  96. ID_AHA_29320ALP,
  97. ID_ALL_MASK,
  98. "Adaptec 29320ALP Ultra320 SCSI adapter",
  99. ahd_aic7901_setup
  100. },
  101. /* aic7902 based controllers */
  102. {
  103. ID_AHA_29320,
  104. ID_ALL_MASK,
  105. "Adaptec 29320 Ultra320 SCSI adapter",
  106. ahd_aic7902_setup
  107. },
  108. {
  109. ID_AHA_29320B,
  110. ID_ALL_MASK,
  111. "Adaptec 29320B Ultra320 SCSI adapter",
  112. ahd_aic7902_setup
  113. },
  114. {
  115. ID_AHA_29320LP,
  116. ID_ALL_MASK,
  117. "Adaptec 29320LP Ultra320 SCSI adapter",
  118. ahd_aic7901A_setup
  119. },
  120. {
  121. ID_AHA_39320,
  122. ID_ALL_MASK,
  123. "Adaptec 39320 Ultra320 SCSI adapter",
  124. ahd_aic7902_setup
  125. },
  126. {
  127. ID_AHA_39320_B,
  128. ID_ALL_MASK,
  129. "Adaptec 39320 Ultra320 SCSI adapter",
  130. ahd_aic7902_setup
  131. },
  132. {
  133. ID_AHA_39320A,
  134. ID_ALL_MASK,
  135. "Adaptec 39320A Ultra320 SCSI adapter",
  136. ahd_aic7902_setup
  137. },
  138. {
  139. ID_AHA_39320D,
  140. ID_ALL_MASK,
  141. "Adaptec 39320D Ultra320 SCSI adapter",
  142. ahd_aic7902_setup
  143. },
  144. {
  145. ID_AHA_39320D_HP,
  146. ID_ALL_MASK,
  147. "Adaptec (HP OEM) 39320D Ultra320 SCSI adapter",
  148. ahd_aic7902_setup
  149. },
  150. {
  151. ID_AHA_39320D_B,
  152. ID_ALL_MASK,
  153. "Adaptec 39320D Ultra320 SCSI adapter",
  154. ahd_aic7902_setup
  155. },
  156. {
  157. ID_AHA_39320D_B_HP,
  158. ID_ALL_MASK,
  159. "Adaptec (HP OEM) 39320D Ultra320 SCSI adapter",
  160. ahd_aic7902_setup
  161. },
  162. /* Generic chip probes for devices we don't know 'exactly' */
  163. {
  164. ID_AIC7901 & ID_9005_GENERIC_MASK,
  165. ID_9005_GENERIC_MASK,
  166. "Adaptec AIC7901 Ultra320 SCSI adapter",
  167. ahd_aic7901_setup
  168. },
  169. {
  170. ID_AIC7901A & ID_DEV_VENDOR_MASK,
  171. ID_DEV_VENDOR_MASK,
  172. "Adaptec AIC7901A Ultra320 SCSI adapter",
  173. ahd_aic7901A_setup
  174. },
  175. {
  176. ID_AIC7902 & ID_9005_GENERIC_MASK,
  177. ID_9005_GENERIC_MASK,
  178. "Adaptec AIC7902 Ultra320 SCSI adapter",
  179. ahd_aic7902_setup
  180. }
  181. };
  182. const u_int ahd_num_pci_devs = NUM_ELEMENTS(ahd_pci_ident_table);
  183. #define DEVCONFIG 0x40
  184. #define PCIXINITPAT 0x0000E000ul
  185. #define PCIXINIT_PCI33_66 0x0000E000ul
  186. #define PCIXINIT_PCIX50_66 0x0000C000ul
  187. #define PCIXINIT_PCIX66_100 0x0000A000ul
  188. #define PCIXINIT_PCIX100_133 0x00008000ul
  189. #define PCI_BUS_MODES_INDEX(devconfig) \
  190. (((devconfig) & PCIXINITPAT) >> 13)
  191. static const char *pci_bus_modes[] =
  192. {
  193. "PCI bus mode unknown",
  194. "PCI bus mode unknown",
  195. "PCI bus mode unknown",
  196. "PCI bus mode unknown",
  197. "PCI-X 101-133Mhz",
  198. "PCI-X 67-100Mhz",
  199. "PCI-X 50-66Mhz",
  200. "PCI 33 or 66Mhz"
  201. };
  202. #define TESTMODE 0x00000800ul
  203. #define IRDY_RST 0x00000200ul
  204. #define FRAME_RST 0x00000100ul
  205. #define PCI64BIT 0x00000080ul
  206. #define MRDCEN 0x00000040ul
  207. #define ENDIANSEL 0x00000020ul
  208. #define MIXQWENDIANEN 0x00000008ul
  209. #define DACEN 0x00000004ul
  210. #define STPWLEVEL 0x00000002ul
  211. #define QWENDIANSEL 0x00000001ul
  212. #define DEVCONFIG1 0x44
  213. #define PREQDIS 0x01
  214. #define CSIZE_LATTIME 0x0c
  215. #define CACHESIZE 0x000000fful
  216. #define LATTIME 0x0000ff00ul
  217. static int ahd_check_extport(struct ahd_softc *ahd);
  218. static void ahd_configure_termination(struct ahd_softc *ahd,
  219. u_int adapter_control);
  220. static void ahd_pci_split_intr(struct ahd_softc *ahd, u_int intstat);
  221. struct ahd_pci_identity *
  222. ahd_find_pci_device(ahd_dev_softc_t pci)
  223. {
  224. uint64_t full_id;
  225. uint16_t device;
  226. uint16_t vendor;
  227. uint16_t subdevice;
  228. uint16_t subvendor;
  229. struct ahd_pci_identity *entry;
  230. u_int i;
  231. vendor = ahd_pci_read_config(pci, PCIR_DEVVENDOR, /*bytes*/2);
  232. device = ahd_pci_read_config(pci, PCIR_DEVICE, /*bytes*/2);
  233. subvendor = ahd_pci_read_config(pci, PCIR_SUBVEND_0, /*bytes*/2);
  234. subdevice = ahd_pci_read_config(pci, PCIR_SUBDEV_0, /*bytes*/2);
  235. full_id = ahd_compose_id(device,
  236. vendor,
  237. subdevice,
  238. subvendor);
  239. /*
  240. * Controllers, mask out the IROC/HostRAID bit
  241. */
  242. full_id &= ID_ALL_IROC_MASK;
  243. for (i = 0; i < ahd_num_pci_devs; i++) {
  244. entry = &ahd_pci_ident_table[i];
  245. if (entry->full_id == (full_id & entry->id_mask)) {
  246. /* Honor exclusion entries. */
  247. if (entry->name == NULL)
  248. return (NULL);
  249. return (entry);
  250. }
  251. }
  252. return (NULL);
  253. }
  254. int
  255. ahd_pci_config(struct ahd_softc *ahd, struct ahd_pci_identity *entry)
  256. {
  257. struct scb_data *shared_scb_data;
  258. u_long l;
  259. u_int command;
  260. uint32_t devconfig;
  261. uint16_t subvendor;
  262. int error;
  263. shared_scb_data = NULL;
  264. ahd->description = entry->name;
  265. /*
  266. * Record if this is an HP board.
  267. */
  268. subvendor = ahd_pci_read_config(ahd->dev_softc,
  269. PCIR_SUBVEND_0, /*bytes*/2);
  270. if (subvendor == SUBID_HP)
  271. ahd->flags |= AHD_HP_BOARD;
  272. error = entry->setup(ahd);
  273. if (error != 0)
  274. return (error);
  275. devconfig = ahd_pci_read_config(ahd->dev_softc, DEVCONFIG, /*bytes*/4);
  276. if ((devconfig & PCIXINITPAT) == PCIXINIT_PCI33_66) {
  277. ahd->chip |= AHD_PCI;
  278. /* Disable PCIX workarounds when running in PCI mode. */
  279. ahd->bugs &= ~AHD_PCIX_BUG_MASK;
  280. } else {
  281. ahd->chip |= AHD_PCIX;
  282. }
  283. ahd->bus_description = pci_bus_modes[PCI_BUS_MODES_INDEX(devconfig)];
  284. ahd_power_state_change(ahd, AHD_POWER_STATE_D0);
  285. error = ahd_pci_map_registers(ahd);
  286. if (error != 0)
  287. return (error);
  288. /*
  289. * If we need to support high memory, enable dual
  290. * address cycles. This bit must be set to enable
  291. * high address bit generation even if we are on a
  292. * 64bit bus (PCI64BIT set in devconfig).
  293. */
  294. if ((ahd->flags & (AHD_39BIT_ADDRESSING|AHD_64BIT_ADDRESSING)) != 0) {
  295. uint32_t devconfig;
  296. if (bootverbose)
  297. printf("%s: Enabling 39Bit Addressing\n",
  298. ahd_name(ahd));
  299. devconfig = ahd_pci_read_config(ahd->dev_softc,
  300. DEVCONFIG, /*bytes*/4);
  301. devconfig |= DACEN;
  302. ahd_pci_write_config(ahd->dev_softc, DEVCONFIG,
  303. devconfig, /*bytes*/4);
  304. }
  305. /* Ensure busmastering is enabled */
  306. command = ahd_pci_read_config(ahd->dev_softc, PCIR_COMMAND, /*bytes*/2);
  307. command |= PCIM_CMD_BUSMASTEREN;
  308. ahd_pci_write_config(ahd->dev_softc, PCIR_COMMAND, command, /*bytes*/2);
  309. error = ahd_softc_init(ahd);
  310. if (error != 0)
  311. return (error);
  312. ahd->bus_intr = ahd_pci_intr;
  313. error = ahd_reset(ahd, /*reinit*/FALSE);
  314. if (error != 0)
  315. return (ENXIO);
  316. ahd->pci_cachesize =
  317. ahd_pci_read_config(ahd->dev_softc, CSIZE_LATTIME,
  318. /*bytes*/1) & CACHESIZE;
  319. ahd->pci_cachesize *= 4;
  320. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  321. /* See if we have a SEEPROM and perform auto-term */
  322. error = ahd_check_extport(ahd);
  323. if (error != 0)
  324. return (error);
  325. /* Core initialization */
  326. error = ahd_init(ahd);
  327. if (error != 0)
  328. return (error);
  329. /*
  330. * Allow interrupts now that we are completely setup.
  331. */
  332. error = ahd_pci_map_int(ahd);
  333. if (error != 0)
  334. return (error);
  335. ahd_list_lock(&l);
  336. /*
  337. * Link this softc in with all other ahd instances.
  338. */
  339. ahd_softc_insert(ahd);
  340. ahd_list_unlock(&l);
  341. return (0);
  342. }
  343. /*
  344. * Perform some simple tests that should catch situations where
  345. * our registers are invalidly mapped.
  346. */
  347. int
  348. ahd_pci_test_register_access(struct ahd_softc *ahd)
  349. {
  350. uint32_t cmd;
  351. u_int targpcistat;
  352. u_int pci_status1;
  353. int error;
  354. uint8_t hcntrl;
  355. error = EIO;
  356. /*
  357. * Enable PCI error interrupt status, but suppress NMIs
  358. * generated by SERR raised due to target aborts.
  359. */
  360. cmd = ahd_pci_read_config(ahd->dev_softc, PCIR_COMMAND, /*bytes*/2);
  361. ahd_pci_write_config(ahd->dev_softc, PCIR_COMMAND,
  362. cmd & ~PCIM_CMD_SERRESPEN, /*bytes*/2);
  363. /*
  364. * First a simple test to see if any
  365. * registers can be read. Reading
  366. * HCNTRL has no side effects and has
  367. * at least one bit that is guaranteed to
  368. * be zero so it is a good register to
  369. * use for this test.
  370. */
  371. hcntrl = ahd_inb(ahd, HCNTRL);
  372. if (hcntrl == 0xFF)
  373. goto fail;
  374. /*
  375. * Next create a situation where write combining
  376. * or read prefetching could be initiated by the
  377. * CPU or host bridge. Our device does not support
  378. * either, so look for data corruption and/or flaged
  379. * PCI errors. First pause without causing another
  380. * chip reset.
  381. */
  382. hcntrl &= ~CHIPRST;
  383. ahd_outb(ahd, HCNTRL, hcntrl|PAUSE);
  384. while (ahd_is_paused(ahd) == 0)
  385. ;
  386. /* Clear any PCI errors that occurred before our driver attached. */
  387. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  388. targpcistat = ahd_inb(ahd, TARGPCISTAT);
  389. ahd_outb(ahd, TARGPCISTAT, targpcistat);
  390. pci_status1 = ahd_pci_read_config(ahd->dev_softc,
  391. PCIR_STATUS + 1, /*bytes*/1);
  392. ahd_pci_write_config(ahd->dev_softc, PCIR_STATUS + 1,
  393. pci_status1, /*bytes*/1);
  394. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  395. ahd_outb(ahd, CLRINT, CLRPCIINT);
  396. ahd_outb(ahd, SEQCTL0, PERRORDIS);
  397. ahd_outl(ahd, SRAM_BASE, 0x5aa555aa);
  398. if (ahd_inl(ahd, SRAM_BASE) != 0x5aa555aa)
  399. goto fail;
  400. if ((ahd_inb(ahd, INTSTAT) & PCIINT) != 0) {
  401. u_int targpcistat;
  402. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  403. targpcistat = ahd_inb(ahd, TARGPCISTAT);
  404. if ((targpcistat & STA) != 0)
  405. goto fail;
  406. }
  407. error = 0;
  408. fail:
  409. if ((ahd_inb(ahd, INTSTAT) & PCIINT) != 0) {
  410. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  411. targpcistat = ahd_inb(ahd, TARGPCISTAT);
  412. /* Silently clear any latched errors. */
  413. ahd_outb(ahd, TARGPCISTAT, targpcistat);
  414. pci_status1 = ahd_pci_read_config(ahd->dev_softc,
  415. PCIR_STATUS + 1, /*bytes*/1);
  416. ahd_pci_write_config(ahd->dev_softc, PCIR_STATUS + 1,
  417. pci_status1, /*bytes*/1);
  418. ahd_outb(ahd, CLRINT, CLRPCIINT);
  419. }
  420. ahd_outb(ahd, SEQCTL0, PERRORDIS|FAILDIS);
  421. ahd_pci_write_config(ahd->dev_softc, PCIR_COMMAND, cmd, /*bytes*/2);
  422. return (error);
  423. }
  424. /*
  425. * Check the external port logic for a serial eeprom
  426. * and termination/cable detection contrls.
  427. */
  428. static int
  429. ahd_check_extport(struct ahd_softc *ahd)
  430. {
  431. struct vpd_config vpd;
  432. struct seeprom_config *sc;
  433. u_int adapter_control;
  434. int have_seeprom;
  435. int error;
  436. sc = ahd->seep_config;
  437. have_seeprom = ahd_acquire_seeprom(ahd);
  438. if (have_seeprom) {
  439. u_int start_addr;
  440. /*
  441. * Fetch VPD for this function and parse it.
  442. */
  443. if (bootverbose)
  444. printf("%s: Reading VPD from SEEPROM...",
  445. ahd_name(ahd));
  446. /* Address is always in units of 16bit words */
  447. start_addr = ((2 * sizeof(*sc))
  448. + (sizeof(vpd) * (ahd->channel - 'A'))) / 2;
  449. error = ahd_read_seeprom(ahd, (uint16_t *)&vpd,
  450. start_addr, sizeof(vpd)/2,
  451. /*bytestream*/TRUE);
  452. if (error == 0)
  453. error = ahd_parse_vpddata(ahd, &vpd);
  454. if (bootverbose)
  455. printf("%s: VPD parsing %s\n",
  456. ahd_name(ahd),
  457. error == 0 ? "successful" : "failed");
  458. if (bootverbose)
  459. printf("%s: Reading SEEPROM...", ahd_name(ahd));
  460. /* Address is always in units of 16bit words */
  461. start_addr = (sizeof(*sc) / 2) * (ahd->channel - 'A');
  462. error = ahd_read_seeprom(ahd, (uint16_t *)sc,
  463. start_addr, sizeof(*sc)/2,
  464. /*bytestream*/FALSE);
  465. if (error != 0) {
  466. printf("Unable to read SEEPROM\n");
  467. have_seeprom = 0;
  468. } else {
  469. have_seeprom = ahd_verify_cksum(sc);
  470. if (bootverbose) {
  471. if (have_seeprom == 0)
  472. printf ("checksum error\n");
  473. else
  474. printf ("done.\n");
  475. }
  476. }
  477. ahd_release_seeprom(ahd);
  478. }
  479. if (!have_seeprom) {
  480. u_int nvram_scb;
  481. /*
  482. * Pull scratch ram settings and treat them as
  483. * if they are the contents of an seeprom if
  484. * the 'ADPT', 'BIOS', or 'ASPI' signature is found
  485. * in SCB 0xFF. We manually compose the data as 16bit
  486. * values to avoid endian issues.
  487. */
  488. ahd_set_scbptr(ahd, 0xFF);
  489. nvram_scb = ahd_inb_scbram(ahd, SCB_BASE + NVRAM_SCB_OFFSET);
  490. if (nvram_scb != 0xFF
  491. && ((ahd_inb_scbram(ahd, SCB_BASE + 0) == 'A'
  492. && ahd_inb_scbram(ahd, SCB_BASE + 1) == 'D'
  493. && ahd_inb_scbram(ahd, SCB_BASE + 2) == 'P'
  494. && ahd_inb_scbram(ahd, SCB_BASE + 3) == 'T')
  495. || (ahd_inb_scbram(ahd, SCB_BASE + 0) == 'B'
  496. && ahd_inb_scbram(ahd, SCB_BASE + 1) == 'I'
  497. && ahd_inb_scbram(ahd, SCB_BASE + 2) == 'O'
  498. && ahd_inb_scbram(ahd, SCB_BASE + 3) == 'S')
  499. || (ahd_inb_scbram(ahd, SCB_BASE + 0) == 'A'
  500. && ahd_inb_scbram(ahd, SCB_BASE + 1) == 'S'
  501. && ahd_inb_scbram(ahd, SCB_BASE + 2) == 'P'
  502. && ahd_inb_scbram(ahd, SCB_BASE + 3) == 'I'))) {
  503. uint16_t *sc_data;
  504. int i;
  505. ahd_set_scbptr(ahd, nvram_scb);
  506. sc_data = (uint16_t *)sc;
  507. for (i = 0; i < 64; i += 2)
  508. *sc_data++ = ahd_inw_scbram(ahd, SCB_BASE+i);
  509. have_seeprom = ahd_verify_cksum(sc);
  510. if (have_seeprom)
  511. ahd->flags |= AHD_SCB_CONFIG_USED;
  512. }
  513. }
  514. #ifdef AHD_DEBUG
  515. if (have_seeprom != 0
  516. && (ahd_debug & AHD_DUMP_SEEPROM) != 0) {
  517. uint16_t *sc_data;
  518. int i;
  519. printf("%s: Seeprom Contents:", ahd_name(ahd));
  520. sc_data = (uint16_t *)sc;
  521. for (i = 0; i < (sizeof(*sc)); i += 2)
  522. printf("\n\t0x%.4x", sc_data[i]);
  523. printf("\n");
  524. }
  525. #endif
  526. if (!have_seeprom) {
  527. if (bootverbose)
  528. printf("%s: No SEEPROM available.\n", ahd_name(ahd));
  529. ahd->flags |= AHD_USEDEFAULTS;
  530. error = ahd_default_config(ahd);
  531. adapter_control = CFAUTOTERM|CFSEAUTOTERM;
  532. free(ahd->seep_config, M_DEVBUF);
  533. ahd->seep_config = NULL;
  534. } else {
  535. error = ahd_parse_cfgdata(ahd, sc);
  536. adapter_control = sc->adapter_control;
  537. }
  538. if (error != 0)
  539. return (error);
  540. ahd_configure_termination(ahd, adapter_control);
  541. return (0);
  542. }
  543. static void
  544. ahd_configure_termination(struct ahd_softc *ahd, u_int adapter_control)
  545. {
  546. int error;
  547. u_int sxfrctl1;
  548. uint8_t termctl;
  549. uint32_t devconfig;
  550. devconfig = ahd_pci_read_config(ahd->dev_softc, DEVCONFIG, /*bytes*/4);
  551. devconfig &= ~STPWLEVEL;
  552. if ((ahd->flags & AHD_STPWLEVEL_A) != 0)
  553. devconfig |= STPWLEVEL;
  554. if (bootverbose)
  555. printf("%s: STPWLEVEL is %s\n",
  556. ahd_name(ahd), (devconfig & STPWLEVEL) ? "on" : "off");
  557. ahd_pci_write_config(ahd->dev_softc, DEVCONFIG, devconfig, /*bytes*/4);
  558. /* Make sure current sensing is off. */
  559. if ((ahd->flags & AHD_CURRENT_SENSING) != 0) {
  560. (void)ahd_write_flexport(ahd, FLXADDR_ROMSTAT_CURSENSECTL, 0);
  561. }
  562. /*
  563. * Read to sense. Write to set.
  564. */
  565. error = ahd_read_flexport(ahd, FLXADDR_TERMCTL, &termctl);
  566. if ((adapter_control & CFAUTOTERM) == 0) {
  567. if (bootverbose)
  568. printf("%s: Manual Primary Termination\n",
  569. ahd_name(ahd));
  570. termctl &= ~(FLX_TERMCTL_ENPRILOW|FLX_TERMCTL_ENPRIHIGH);
  571. if ((adapter_control & CFSTERM) != 0)
  572. termctl |= FLX_TERMCTL_ENPRILOW;
  573. if ((adapter_control & CFWSTERM) != 0)
  574. termctl |= FLX_TERMCTL_ENPRIHIGH;
  575. } else if (error != 0) {
  576. printf("%s: Primary Auto-Term Sensing failed! "
  577. "Using Defaults.\n", ahd_name(ahd));
  578. termctl = FLX_TERMCTL_ENPRILOW|FLX_TERMCTL_ENPRIHIGH;
  579. }
  580. if ((adapter_control & CFSEAUTOTERM) == 0) {
  581. if (bootverbose)
  582. printf("%s: Manual Secondary Termination\n",
  583. ahd_name(ahd));
  584. termctl &= ~(FLX_TERMCTL_ENSECLOW|FLX_TERMCTL_ENSECHIGH);
  585. if ((adapter_control & CFSELOWTERM) != 0)
  586. termctl |= FLX_TERMCTL_ENSECLOW;
  587. if ((adapter_control & CFSEHIGHTERM) != 0)
  588. termctl |= FLX_TERMCTL_ENSECHIGH;
  589. } else if (error != 0) {
  590. printf("%s: Secondary Auto-Term Sensing failed! "
  591. "Using Defaults.\n", ahd_name(ahd));
  592. termctl |= FLX_TERMCTL_ENSECLOW|FLX_TERMCTL_ENSECHIGH;
  593. }
  594. /*
  595. * Now set the termination based on what we found.
  596. */
  597. sxfrctl1 = ahd_inb(ahd, SXFRCTL1) & ~STPWEN;
  598. if ((termctl & FLX_TERMCTL_ENPRILOW) != 0) {
  599. ahd->flags |= AHD_TERM_ENB_A;
  600. sxfrctl1 |= STPWEN;
  601. }
  602. /* Must set the latch once in order to be effective. */
  603. ahd_outb(ahd, SXFRCTL1, sxfrctl1|STPWEN);
  604. ahd_outb(ahd, SXFRCTL1, sxfrctl1);
  605. error = ahd_write_flexport(ahd, FLXADDR_TERMCTL, termctl);
  606. if (error != 0) {
  607. printf("%s: Unable to set termination settings!\n",
  608. ahd_name(ahd));
  609. } else if (bootverbose) {
  610. printf("%s: Primary High byte termination %sabled\n",
  611. ahd_name(ahd),
  612. (termctl & FLX_TERMCTL_ENPRIHIGH) ? "En" : "Dis");
  613. printf("%s: Primary Low byte termination %sabled\n",
  614. ahd_name(ahd),
  615. (termctl & FLX_TERMCTL_ENPRILOW) ? "En" : "Dis");
  616. printf("%s: Secondary High byte termination %sabled\n",
  617. ahd_name(ahd),
  618. (termctl & FLX_TERMCTL_ENSECHIGH) ? "En" : "Dis");
  619. printf("%s: Secondary Low byte termination %sabled\n",
  620. ahd_name(ahd),
  621. (termctl & FLX_TERMCTL_ENSECLOW) ? "En" : "Dis");
  622. }
  623. return;
  624. }
  625. #define DPE 0x80
  626. #define SSE 0x40
  627. #define RMA 0x20
  628. #define RTA 0x10
  629. #define STA 0x08
  630. #define DPR 0x01
  631. static const char *split_status_source[] =
  632. {
  633. "DFF0",
  634. "DFF1",
  635. "OVLY",
  636. "CMC",
  637. };
  638. static const char *pci_status_source[] =
  639. {
  640. "DFF0",
  641. "DFF1",
  642. "SG",
  643. "CMC",
  644. "OVLY",
  645. "NONE",
  646. "MSI",
  647. "TARG"
  648. };
  649. static const char *split_status_strings[] =
  650. {
  651. "%s: Received split response in %s.\n",
  652. "%s: Received split completion error message in %s\n",
  653. "%s: Receive overrun in %s\n",
  654. "%s: Count not complete in %s\n",
  655. "%s: Split completion data bucket in %s\n",
  656. "%s: Split completion address error in %s\n",
  657. "%s: Split completion byte count error in %s\n",
  658. "%s: Signaled Target-abort to early terminate a split in %s\n"
  659. };
  660. static const char *pci_status_strings[] =
  661. {
  662. "%s: Data Parity Error has been reported via PERR# in %s\n",
  663. "%s: Target initial wait state error in %s\n",
  664. "%s: Split completion read data parity error in %s\n",
  665. "%s: Split completion address attribute parity error in %s\n",
  666. "%s: Received a Target Abort in %s\n",
  667. "%s: Received a Master Abort in %s\n",
  668. "%s: Signal System Error Detected in %s\n",
  669. "%s: Address or Write Phase Parity Error Detected in %s.\n"
  670. };
  671. void
  672. ahd_pci_intr(struct ahd_softc *ahd)
  673. {
  674. uint8_t pci_status[8];
  675. ahd_mode_state saved_modes;
  676. u_int pci_status1;
  677. u_int intstat;
  678. u_int i;
  679. u_int reg;
  680. intstat = ahd_inb(ahd, INTSTAT);
  681. if ((intstat & SPLTINT) != 0)
  682. ahd_pci_split_intr(ahd, intstat);
  683. if ((intstat & PCIINT) == 0)
  684. return;
  685. printf("%s: PCI error Interrupt\n", ahd_name(ahd));
  686. saved_modes = ahd_save_modes(ahd);
  687. ahd_dump_card_state(ahd);
  688. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  689. for (i = 0, reg = DF0PCISTAT; i < 8; i++, reg++) {
  690. if (i == 5)
  691. continue;
  692. pci_status[i] = ahd_inb(ahd, reg);
  693. /* Clear latched errors. So our interrupt deasserts. */
  694. ahd_outb(ahd, reg, pci_status[i]);
  695. }
  696. for (i = 0; i < 8; i++) {
  697. u_int bit;
  698. if (i == 5)
  699. continue;
  700. for (bit = 0; bit < 8; bit++) {
  701. if ((pci_status[i] & (0x1 << bit)) != 0) {
  702. static const char *s;
  703. s = pci_status_strings[bit];
  704. if (i == 7/*TARG*/ && bit == 3)
  705. s = "%s: Signaled Target Abort\n";
  706. printf(s, ahd_name(ahd), pci_status_source[i]);
  707. }
  708. }
  709. }
  710. pci_status1 = ahd_pci_read_config(ahd->dev_softc,
  711. PCIR_STATUS + 1, /*bytes*/1);
  712. ahd_pci_write_config(ahd->dev_softc, PCIR_STATUS + 1,
  713. pci_status1, /*bytes*/1);
  714. ahd_restore_modes(ahd, saved_modes);
  715. ahd_outb(ahd, CLRINT, CLRPCIINT);
  716. ahd_unpause(ahd);
  717. }
  718. static void
  719. ahd_pci_split_intr(struct ahd_softc *ahd, u_int intstat)
  720. {
  721. uint8_t split_status[4];
  722. uint8_t split_status1[4];
  723. uint8_t sg_split_status[2];
  724. uint8_t sg_split_status1[2];
  725. ahd_mode_state saved_modes;
  726. u_int i;
  727. uint16_t pcix_status;
  728. /*
  729. * Check for splits in all modes. Modes 0 and 1
  730. * additionally have SG engine splits to look at.
  731. */
  732. pcix_status = ahd_pci_read_config(ahd->dev_softc, PCIXR_STATUS,
  733. /*bytes*/2);
  734. printf("%s: PCI Split Interrupt - PCI-X status = 0x%x\n",
  735. ahd_name(ahd), pcix_status);
  736. saved_modes = ahd_save_modes(ahd);
  737. for (i = 0; i < 4; i++) {
  738. ahd_set_modes(ahd, i, i);
  739. split_status[i] = ahd_inb(ahd, DCHSPLTSTAT0);
  740. split_status1[i] = ahd_inb(ahd, DCHSPLTSTAT1);
  741. /* Clear latched errors. So our interrupt deasserts. */
  742. ahd_outb(ahd, DCHSPLTSTAT0, split_status[i]);
  743. ahd_outb(ahd, DCHSPLTSTAT1, split_status1[i]);
  744. if (i > 1)
  745. continue;
  746. sg_split_status[i] = ahd_inb(ahd, SGSPLTSTAT0);
  747. sg_split_status1[i] = ahd_inb(ahd, SGSPLTSTAT1);
  748. /* Clear latched errors. So our interrupt deasserts. */
  749. ahd_outb(ahd, SGSPLTSTAT0, sg_split_status[i]);
  750. ahd_outb(ahd, SGSPLTSTAT1, sg_split_status1[i]);
  751. }
  752. for (i = 0; i < 4; i++) {
  753. u_int bit;
  754. for (bit = 0; bit < 8; bit++) {
  755. if ((split_status[i] & (0x1 << bit)) != 0) {
  756. static const char *s;
  757. s = split_status_strings[bit];
  758. printf(s, ahd_name(ahd),
  759. split_status_source[i]);
  760. }
  761. if (i > 1)
  762. continue;
  763. if ((sg_split_status[i] & (0x1 << bit)) != 0) {
  764. static const char *s;
  765. s = split_status_strings[bit];
  766. printf(s, ahd_name(ahd), "SG");
  767. }
  768. }
  769. }
  770. /*
  771. * Clear PCI-X status bits.
  772. */
  773. ahd_pci_write_config(ahd->dev_softc, PCIXR_STATUS,
  774. pcix_status, /*bytes*/2);
  775. ahd_outb(ahd, CLRINT, CLRSPLTINT);
  776. ahd_restore_modes(ahd, saved_modes);
  777. }
  778. static int
  779. ahd_aic7901_setup(struct ahd_softc *ahd)
  780. {
  781. ahd->chip = AHD_AIC7901;
  782. ahd->features = AHD_AIC7901_FE;
  783. return (ahd_aic790X_setup(ahd));
  784. }
  785. static int
  786. ahd_aic7901A_setup(struct ahd_softc *ahd)
  787. {
  788. ahd->chip = AHD_AIC7901A;
  789. ahd->features = AHD_AIC7901A_FE;
  790. return (ahd_aic790X_setup(ahd));
  791. }
  792. static int
  793. ahd_aic7902_setup(struct ahd_softc *ahd)
  794. {
  795. ahd->chip = AHD_AIC7902;
  796. ahd->features = AHD_AIC7902_FE;
  797. return (ahd_aic790X_setup(ahd));
  798. }
  799. static int
  800. ahd_aic790X_setup(struct ahd_softc *ahd)
  801. {
  802. ahd_dev_softc_t pci;
  803. u_int rev;
  804. pci = ahd->dev_softc;
  805. rev = ahd_pci_read_config(pci, PCIR_REVID, /*bytes*/1);
  806. if (rev < ID_AIC7902_PCI_REV_A4) {
  807. printf("%s: Unable to attach to unsupported chip revision %d\n",
  808. ahd_name(ahd), rev);
  809. ahd_pci_write_config(pci, PCIR_COMMAND, 0, /*bytes*/2);
  810. return (ENXIO);
  811. }
  812. ahd->channel = ahd_get_pci_function(pci) + 'A';
  813. if (rev < ID_AIC7902_PCI_REV_B0) {
  814. /*
  815. * Enable A series workarounds.
  816. */
  817. ahd->bugs |= AHD_SENT_SCB_UPDATE_BUG|AHD_ABORT_LQI_BUG
  818. | AHD_PKT_BITBUCKET_BUG|AHD_LONG_SETIMO_BUG
  819. | AHD_NLQICRC_DELAYED_BUG|AHD_SCSIRST_BUG
  820. | AHD_LQO_ATNO_BUG|AHD_AUTOFLUSH_BUG
  821. | AHD_CLRLQO_AUTOCLR_BUG|AHD_PCIX_MMAPIO_BUG
  822. | AHD_PCIX_CHIPRST_BUG|AHD_PCIX_SCBRAM_RD_BUG
  823. | AHD_PKTIZED_STATUS_BUG|AHD_PKT_LUN_BUG
  824. | AHD_MDFF_WSCBPTR_BUG|AHD_REG_SLOW_SETTLE_BUG
  825. | AHD_SET_MODE_BUG|AHD_BUSFREEREV_BUG
  826. | AHD_NONPACKFIFO_BUG|AHD_PACED_NEGTABLE_BUG
  827. | AHD_FAINT_LED_BUG;
  828. /*
  829. * IO Cell paramter setup.
  830. */
  831. AHD_SET_PRECOMP(ahd, AHD_PRECOMP_CUTBACK_29);
  832. if ((ahd->flags & AHD_HP_BOARD) == 0)
  833. AHD_SET_SLEWRATE(ahd, AHD_SLEWRATE_DEF_REVA);
  834. } else {
  835. u_int devconfig1;
  836. ahd->features |= AHD_RTI|AHD_NEW_IOCELL_OPTS
  837. | AHD_NEW_DFCNTRL_OPTS|AHD_FAST_CDB_DELIVERY;
  838. ahd->bugs |= AHD_LQOOVERRUN_BUG|AHD_EARLY_REQ_BUG;
  839. /*
  840. * Some issues have been resolved in the 7901B.
  841. */
  842. if ((ahd->features & AHD_MULTI_FUNC) != 0)
  843. ahd->bugs |= AHD_INTCOLLISION_BUG|AHD_ABORT_LQI_BUG;
  844. /*
  845. * IO Cell paramter setup.
  846. */
  847. AHD_SET_PRECOMP(ahd, AHD_PRECOMP_CUTBACK_29);
  848. AHD_SET_SLEWRATE(ahd, AHD_SLEWRATE_DEF_REVB);
  849. AHD_SET_AMPLITUDE(ahd, AHD_AMPLITUDE_DEF);
  850. /*
  851. * Set the PREQDIS bit for H2B which disables some workaround
  852. * that doesn't work on regular PCI busses.
  853. * XXX - Find out exactly what this does from the hardware
  854. * folks!
  855. */
  856. devconfig1 = ahd_pci_read_config(pci, DEVCONFIG1, /*bytes*/1);
  857. ahd_pci_write_config(pci, DEVCONFIG1,
  858. devconfig1|PREQDIS, /*bytes*/1);
  859. devconfig1 = ahd_pci_read_config(pci, DEVCONFIG1, /*bytes*/1);
  860. }
  861. return (0);
  862. }