PageRenderTime 61ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/usr/src/uts/sun4u/tazmo/io/envctrl.c

https://bitbucket.org/0xffea/illumos-dccp
C | 4101 lines | 2781 code | 595 blank | 725 comment | 655 complexity | 408a973f82f257af244f86a1b31441fd MD5 | raw file
Possible License(s): LGPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, MPL-2.0-no-copyleft-exception, AGPL-3.0, BSD-3-Clause, GPL-2.0, LGPL-2.1, LGPL-3.0, AGPL-1.0, GPL-3.0, 0BSD, BSD-2-Clause
  1. /*
  2. * CDDL HEADER START
  3. *
  4. * The contents of this file are subject to the terms of the
  5. * Common Development and Distribution License (the "License").
  6. * You may not use this file except in compliance with the License.
  7. *
  8. * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  9. * or http://www.opensolaris.org/os/licensing.
  10. * See the License for the specific language governing permissions
  11. * and limitations under the License.
  12. *
  13. * When distributing Covered Code, include this CDDL HEADER in each
  14. * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15. * If applicable, add the following below this CDDL HEADER, with the
  16. * fields enclosed by brackets "[]" replaced with your own identifying
  17. * information: Portions Copyright [yyyy] [name of copyright owner]
  18. *
  19. * CDDL HEADER END
  20. */
  21. /*
  22. * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
  23. * Use is subject to license terms.
  24. */
  25. /*
  26. * ENVCTRL_ Environment Monitoring driver for i2c
  27. *
  28. */
  29. #include <sys/param.h>
  30. #include <sys/types.h>
  31. #include <sys/signal.h>
  32. #include <sys/errno.h>
  33. #include <sys/file.h>
  34. #include <sys/termio.h>
  35. #include <sys/termios.h>
  36. #include <sys/cmn_err.h>
  37. #include <sys/stream.h>
  38. #include <sys/strsun.h>
  39. #include <sys/stropts.h>
  40. #include <sys/strtty.h>
  41. #include <sys/debug.h>
  42. #include <sys/eucioctl.h>
  43. #include <sys/cred.h>
  44. #include <sys/uio.h>
  45. #include <sys/stat.h>
  46. #include <sys/kmem.h>
  47. #include <sys/ddi.h>
  48. #include <sys/sunddi.h>
  49. #include <sys/obpdefs.h>
  50. #include <sys/conf.h> /* req. by dev_ops flags MTSAFE etc. */
  51. #include <sys/modctl.h> /* for modldrv */
  52. #include <sys/stat.h> /* ddi_create_minor_node S_IFCHR */
  53. #include <sys/open.h> /* for open params. */
  54. #include <sys/uio.h> /* for read/write */
  55. #include <sys/envctrl.h> /* Environment header */
  56. /* driver entry point fn definitions */
  57. static int envctrl_open(queue_t *, dev_t *, int, int, cred_t *);
  58. static int envctrl_close(queue_t *, int, cred_t *);
  59. static uint_t envctrl_bus_isr(caddr_t);
  60. static uint_t envctrl_dev_isr(caddr_t);
  61. /* configuration entry point fn definitions */
  62. static int envctrl_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
  63. static int envctrl_attach(dev_info_t *, ddi_attach_cmd_t);
  64. static int envctrl_detach(dev_info_t *, ddi_detach_cmd_t);
  65. /* Driver private routines */
  66. static void envctrl_init_bus(struct envctrlunit *);
  67. static int envctrl_xmit(struct envctrlunit *, caddr_t *, int);
  68. static void envctrl_recv(struct envctrlunit *, caddr_t *, int);
  69. static void envctrl_get_sys_temperatures(struct envctrlunit *, uint8_t *);
  70. static int envctrl_get_lm75_temp(struct envctrlunit *);
  71. static int envctrl_get_ps_temp(struct envctrlunit *, uint8_t);
  72. static int envctrl_get_cpu_temp(struct envctrlunit *, int);
  73. static void envctrl_fan_fail_service(struct envctrlunit *);
  74. static void envctrl_PS_intr_service(struct envctrlunit *, uint8_t);
  75. static void envctrl_ps_probe(struct envctrlunit *);
  76. static void envctrl_tempr_poll(void *);
  77. static void envctrl_pshotplug_poll(void *);
  78. static void envctrl_led_blink(void *);
  79. static void envctrl_reset_dflop(struct envctrlunit *);
  80. static void envctrl_enable_devintrs(struct envctrlunit *);
  81. static void envctrl_stop_clock(struct envctrlunit *);
  82. static void envctrl_reset_watchdog(struct envctrlunit *, uint8_t *);
  83. static void envctrl_abort_seq_handler(char *msg);
  84. static uint8_t envctrl_get_fpm_status(struct envctrlunit *);
  85. static void envctrl_set_fsp(struct envctrlunit *, uint8_t *);
  86. static int envctrl_set_dskled(struct envctrlunit *,
  87. struct envctrl_pcf8574_chip *);
  88. static int envctrl_get_dskled(struct envctrlunit *,
  89. struct envctrl_pcf8574_chip *);
  90. static void envctrl_probe_cpus(struct envctrlunit *);
  91. static int envctrl_match_cpu(dev_info_t *, void *);
  92. static int envctrl_isother_fault_led(struct envctrlunit *,
  93. uint8_t, uint8_t);
  94. /* Kstat routines */
  95. static void envctrl_add_kstats(struct envctrlunit *);
  96. static int envctrl_ps_kstat_update(kstat_t *, int);
  97. static int envctrl_fanstat_kstat_update(kstat_t *, int);
  98. static int envctrl_encl_kstat_update(kstat_t *, int);
  99. static void envctrl_init_fan_kstats(struct envctrlunit *);
  100. static void envctrl_init_encl_kstats(struct envctrlunit *);
  101. static void envctrl_add_encl_kstats(struct envctrlunit *, int, int,
  102. uint8_t);
  103. static void envctrl_mod_encl_kstats(struct envctrlunit *, int, int,
  104. uint8_t);
  105. /* Streams Routines */
  106. static int envctrl_wput(queue_t *, mblk_t *);
  107. /* External routines */
  108. extern void power_down(const char *);
  109. extern int prom_getprop();
  110. extern int prom_getproplen();
  111. extern void prom_printf(const char *fmt, ...);
  112. extern void (*abort_seq_handler)();
  113. static void *envctrlsoft_statep;
  114. /* Local Variables */
  115. /* Indicates whether or not the overtemp thread has been started */
  116. static int envctrl_debug_flags = 0;
  117. static int envctrl_afb_present = 0;
  118. static int envctrl_power_off_overide = 0;
  119. static int envctrl_max_retries = 100;
  120. static int envctrl_allow_detach = 0;
  121. static int envctrl_numcpus = 1;
  122. static int envctrl_p0_enclosure = 0; /* set to 1 if it is a P0 */
  123. static int envctrl_handler = 1; /* 1 is the default */
  124. static clock_t overtemp_timeout_hz;
  125. static clock_t blink_timeout_hz;
  126. static clock_t pshotplug_timeout_hz;
  127. static int controller_present[] = {-1, -1, -1};
  128. #ifdef MULTIFAN
  129. static int envctrl_fan_debug = 0;
  130. #endif
  131. static int eHc_debug = 0;
  132. static int power_supply_previous_state[] = {-1, -1, -1};
  133. extern void pci_thermal_rem_intr(dev_info_t *, uint_t);
  134. #define LOOP_TIMEOUT 25
  135. #define INIT_FAN_VAL 35
  136. #define DCMNERR if (eHc_debug & 0x1) cmn_err
  137. #define DCMN2ERR if (eHc_debug & 0x2) cmn_err
  138. #define MAX_FAN_FAIL_RETRY 3
  139. uint8_t backaddrs[] = {ENVCTRL_PCF8574_DEV0, ENVCTRL_PCF8574_DEV1,
  140. ENVCTRL_PCF8574_DEV2};
  141. struct module_info envctrlinfo = {
  142. /* id, name, min pkt siz, max pkt siz, hi water, low water */
  143. 42, "envctrl", 0, 2048, (1024 * 20), (1024 * 1)
  144. };
  145. static struct qinit envctrl_rinit = {
  146. putq, NULL, envctrl_open, envctrl_close, NULL, &envctrlinfo, NULL
  147. };
  148. static struct qinit envctrl_wint = {
  149. envctrl_wput, NULL, envctrl_open, envctrl_close,
  150. NULL, &envctrlinfo, NULL
  151. };
  152. struct streamtab envctrl_str_info = {
  153. &envctrl_rinit, &envctrl_wint, NULL, NULL
  154. };
  155. static struct cb_ops envctrl_cb_ops = {
  156. nodev, /* cb_open */
  157. nodev, /* cb_close */
  158. nodev, /* cb_strategy */
  159. nodev, /* cb_print */
  160. nodev, /* cb_dump */
  161. nodev, /* cb_read */
  162. nodev, /* cb_write */
  163. nodev, /* cb_ioctl */
  164. nodev, /* cb_devmap */
  165. nodev, /* cb_mmap */
  166. nodev, /* cb_segmap */
  167. nochpoll, /* cb_chpoll */
  168. ddi_prop_op, /* cb_prop_op */
  169. &envctrl_str_info, /* cb_stream */
  170. D_MP /* cb_flag */
  171. };
  172. /*
  173. * Declare ops vectors for auto configuration.
  174. */
  175. struct dev_ops envctrl_ops = {
  176. DEVO_REV, /* devo_rev */
  177. 0, /* devo_refcnt */
  178. envctrl_getinfo, /* devo_getinfo */
  179. nulldev, /* devo_identify */
  180. nulldev, /* devo_probe */
  181. envctrl_attach, /* devo_attach */
  182. envctrl_detach, /* devo_detach */
  183. nodev, /* devo_reset */
  184. &envctrl_cb_ops, /* devo_cb_ops */
  185. (struct bus_ops *)NULL, /* devo_bus_ops */
  186. nulldev, /* devo_power */
  187. ddi_quiesce_not_supported, /* devo_quiesce */
  188. };
  189. extern struct mod_ops mod_driverops;
  190. static struct modldrv envctrlmodldrv = {
  191. &mod_driverops, /* type of module - driver */
  192. "I2C ENVCTRL_driver",
  193. &envctrl_ops,
  194. };
  195. static struct modlinkage envctrlmodlinkage = {
  196. MODREV_1,
  197. &envctrlmodldrv,
  198. 0
  199. };
  200. /*
  201. * The following defines are for the i2c protocol routines.
  202. * This section of defines should be removed once the envctrl_targets.c
  203. * file is included.
  204. */
  205. #define EHC_SUCCESS 0
  206. #define EHC_FAILURE (-1)
  207. #define EHC_NO_SLAVE_ACK 3
  208. #define EHC_MAX_WAIT 7 /* decimal */
  209. #define EHC_S1_PIN 0x80
  210. #define EHC_S1_ES1 0x20
  211. #define EHC_S1_ES0 0x40
  212. #define EHC_S1_NBB 0x01
  213. #define EHC_S1_ACK 0x01
  214. #define EHC_S1_STA 0x04
  215. #define EHC_S1_STO 0x02
  216. #define EHC_S1_LRB 0x08
  217. #define EHC_S1_BER 0x10
  218. #define EHC_S1_LAB 0x02
  219. #define EHC_S0_OWN 0x55
  220. #define EHC_S0_CLK 0x1c
  221. #define EHC_BYTE_READ 0x01
  222. #define EHC_LONGEST_MSG 1000 /* decimal */
  223. /*
  224. * PCF8591 Chip Used for temperature sensors
  225. *
  226. * Addressing Register definition.
  227. * A0-A2 valid range is 0-7
  228. *
  229. * 7 6 5 4 3 2 1 0
  230. * ------------------------------------------------
  231. * | 1 | 0 | 0 | 1 | A2 | A1 | A0 | R/W |
  232. * ------------------------------------------------
  233. */
  234. #define EHC_PCF8591_MAX_DEVS 0x08
  235. #define EHC_DEV0 0x00
  236. #define EHC_DEV1 0x02
  237. #define EHC_DEV2 0x04
  238. #define EHC_DEV3 0x06
  239. #define EHC_DEV4 0x08
  240. #define EHC_DEV5 0x0A
  241. #define EHC_DEV6 0x0C
  242. #define EHC_DEV7 0x0E
  243. /*
  244. * CONTROL OF CHIP
  245. * PCF8591 Temp sensing control register definitions
  246. *
  247. * 7 6 5 4 3 2 1 0
  248. * ---------------------------------------------
  249. * | 0 | AOE | X | X | 0 | AIF | X | X |
  250. * ---------------------------------------------
  251. * AOE = Analog out enable.. not used on out implementation
  252. * 5 & 4 = Analog Input Programming.. see data sheet for bits..
  253. *
  254. * AIF = Auto increment flag
  255. * bits 1 & 0 are for the Chennel number.
  256. */
  257. #define EHC_PCF8591_ANALOG_OUTPUT_EN 0x40
  258. #define EHC_PCF8591_ANALOG_INPUT_EN 0x00
  259. #define EHC_PCF8591_READ_BIT 0x01
  260. #define EHC_PCF8591_AUTO_INCR 0x04
  261. #define EHC_PCF8591_OSCILATOR 0x40
  262. #define EHC_PCF8591_MAX_PORTS 0x04
  263. #define EHC_PCF8591_CH_0 0x00
  264. #define EHC_PCF8591_CH_1 0x01
  265. #define EHC_PCF8591_CH_2 0x02
  266. #define EHC_PCF8591_CH_3 0x03
  267. /*
  268. * PCF8574 Fan Fail, Power Supply Fail Detector
  269. * This device is driven by interrupts. Each time it interrupts
  270. * you must look at the CSR to see which ports caused the interrupt
  271. * they are indicated by a 1.
  272. *
  273. * Address map of this chip
  274. *
  275. * -------------------------------------------
  276. * | 0 | 1 | 1 | 1 | A2 | A1 | A0 | 0 |
  277. * -------------------------------------------
  278. *
  279. */
  280. #define EHC_PCF8574_PORT0 0x01
  281. #define EHC_PCF8574_PORT1 0x02
  282. #define EHC_PCF8574_PORT2 0x04
  283. #define EHC_PCF8574_PORT3 0x08
  284. #define EHC_PCF8574_PORT4 0x10
  285. #define EHC_PCF8574_PORT5 0x20
  286. #define EHC_PCF8574_PORT6 0x40
  287. #define EHC_PCF8574_PORT7 0x80
  288. /*
  289. * Defines for the PCF8583 Clock Calendar Chip.
  290. */
  291. #define EHC_PCF8583_READ_BIT 0x01
  292. #define ALARM_CTR_REG_MINS 0x03
  293. #define ALARM_REG_MINS 0x0B
  294. #define ALARM_TIMER_REG 0x0F
  295. struct eHc_pcd8584_regs {
  296. uint8_t s0; /* Own Address S0' */
  297. uint8_t s1; /* Control Status register */
  298. uint8_t clock_s2; /* Clock programming register */
  299. };
  300. struct eHc_envcunit {
  301. struct eHc_pcd8584_regs *bus_ctl_regs;
  302. ddi_acc_handle_t ctlr_handle;
  303. kmutex_t umutex;
  304. };
  305. /*
  306. * Prototypes for static routines
  307. */
  308. static int eHc_write_tda8444(struct eHc_envcunit *, int, int, int, uint8_t *,
  309. int);
  310. static int eHc_read_pcf8591(struct eHc_envcunit *, int, int, int, int, int,
  311. uint8_t *, int);
  312. static int eHc_read_pcf8574a(struct eHc_envcunit *, int, uint8_t *, int);
  313. static int eHc_write_pcf8574a(struct eHc_envcunit *, int, uint8_t *, int);
  314. static int eHc_read_pcf8574(struct eHc_envcunit *, int, uint8_t *, int);
  315. static int eHc_write_pcf8574(struct eHc_envcunit *, int, uint8_t *, int);
  316. static int eHc_read_lm75(struct eHc_envcunit *, int, uint8_t *, int);
  317. static int eHc_write_pcf8583(struct eHc_envcunit *, int, uint8_t *, int);
  318. static int eHc_start_pcf8584(struct eHc_envcunit *, uint8_t);
  319. static void eHc_stop_pcf8584(struct eHc_envcunit *);
  320. static int eHc_read_pcf8584(struct eHc_envcunit *, uint8_t *);
  321. static int eHc_write_pcf8584(struct eHc_envcunit *, uint8_t);
  322. static int eHc_after_read_pcf8584(struct eHc_envcunit *, uint8_t *);
  323. /*
  324. * End of i2c protocol definitions section
  325. */
  326. int
  327. _init(void)
  328. {
  329. int error;
  330. if ((error = mod_install(&envctrlmodlinkage)) == 0) {
  331. (void) ddi_soft_state_init(&envctrlsoft_statep,
  332. sizeof (struct envctrlunit), 1);
  333. }
  334. return (error);
  335. }
  336. int
  337. _fini(void)
  338. {
  339. int error;
  340. if ((error = mod_remove(&envctrlmodlinkage)) == 0)
  341. ddi_soft_state_fini(&envctrlsoft_statep);
  342. return (error);
  343. }
  344. int
  345. _info(struct modinfo *modinfop)
  346. {
  347. return (mod_info(&envctrlmodlinkage, modinfop));
  348. }
  349. static int
  350. envctrl_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
  351. {
  352. int instance;
  353. char name[16];
  354. uint8_t fspval;
  355. struct envctrlunit *unitp;
  356. struct ddi_device_acc_attr attr;
  357. int *reg_prop;
  358. uchar_t *creg_prop;
  359. uint_t len, tblsz;
  360. int i, cputemp, status;
  361. uint8_t buf[3];
  362. status = len = tblsz = 0;
  363. attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
  364. attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
  365. attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
  366. instance = ddi_get_instance(dip);
  367. switch (cmd) {
  368. case DDI_ATTACH:
  369. break;
  370. case DDI_RESUME:
  371. if (!(unitp = ddi_get_soft_state(envctrlsoft_statep, instance)))
  372. return (DDI_FAILURE);
  373. mutex_enter(&unitp->umutex);
  374. if (!unitp->suspended) {
  375. mutex_exit(&unitp->umutex);
  376. return (DDI_FAILURE);
  377. }
  378. unitp->suspended = 0;
  379. mutex_exit(&unitp->umutex);
  380. unitp->initting = B_TRUE;
  381. envctrl_init_bus(unitp);
  382. unitp->initting = B_FALSE;
  383. mutex_enter(&unitp->umutex);
  384. envctrl_ps_probe(unitp);
  385. envctrl_probe_cpus(unitp);
  386. mutex_exit(&unitp->umutex);
  387. return (DDI_SUCCESS);
  388. default:
  389. return (DDI_FAILURE);
  390. }
  391. /* Set up timer values */
  392. overtemp_timeout_hz = drv_usectohz(OVERTEMP_TIMEOUT_USEC);
  393. blink_timeout_hz = drv_usectohz(BLINK_TIMEOUT_USEC);
  394. pshotplug_timeout_hz = drv_usectohz(BLINK_TIMEOUT_USEC * 6);
  395. if (ddi_soft_state_zalloc(envctrlsoft_statep, instance) != 0) {
  396. cmn_err(CE_WARN, "envctrl failed to zalloc softstate\n");
  397. goto failed;
  398. }
  399. unitp = ddi_get_soft_state(envctrlsoft_statep, instance);
  400. if (ddi_regs_map_setup(dip, 0, (caddr_t *)&unitp->bus_ctl_regs, 0,
  401. sizeof (struct envctrl_pcd8584_regs), &attr,
  402. &unitp->ctlr_handle) != DDI_SUCCESS) {
  403. cmn_err(CE_WARN, "I2c failed to map in bus_control regs\n");
  404. return (DDI_FAILURE);
  405. }
  406. /*
  407. * If the PCI nexus has added a thermal interrupt, we first need
  408. * to remove that interrupt handler.
  409. *
  410. * WARNING: Removing another driver's interrupt handler is not
  411. * allowed. The pci_thermal_rem_intr() call below is needed to retain
  412. * the legacy behavior on Tazmo systems.
  413. */
  414. pci_thermal_rem_intr(dip, (uint_t)0);
  415. /* add interrupts */
  416. if (ddi_get_iblock_cookie(dip, 1,
  417. &unitp->ic_trap_cookie) != DDI_SUCCESS) {
  418. cmn_err(CE_WARN, "ddi_get_iblock_cookie FAILED \n");
  419. goto failed;
  420. }
  421. mutex_init(&unitp->umutex, NULL, MUTEX_DRIVER,
  422. (void *)unitp->ic_trap_cookie);
  423. if (ddi_add_intr(dip, 0, &unitp->ic_trap_cookie, NULL, envctrl_bus_isr,
  424. (caddr_t)unitp) != DDI_SUCCESS) {
  425. cmn_err(CE_WARN, "envctrl_attach failed to add hard intr %d\n",
  426. instance);
  427. goto remlock;
  428. }
  429. if (ddi_add_intr(dip, 1, &unitp->ic_trap_cookie, NULL, envctrl_dev_isr,
  430. (caddr_t)unitp) != DDI_SUCCESS) {
  431. cmn_err(CE_WARN, "envctrl_attach failed to add hard intr %d\n",
  432. instance);
  433. goto remhardintr;
  434. }
  435. (void) sprintf(name, "envctrl%d", instance);
  436. if (ddi_create_minor_node(dip, name, S_IFCHR, instance, DDI_PSEUDO,
  437. NULL) == DDI_FAILURE) {
  438. ddi_remove_minor_node(dip, NULL);
  439. goto remhardintr1;
  440. }
  441. mutex_enter(&unitp->umutex);
  442. switch (ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
  443. ENVCTRL_LED_BLINK, -1)) {
  444. case 1:
  445. unitp->activity_led_blink = B_TRUE;
  446. break;
  447. case 0:
  448. default:
  449. unitp->activity_led_blink = B_FALSE;
  450. break;
  451. }
  452. unitp->shutdown = B_FALSE;
  453. unitp->num_ps_present = unitp->num_encl_present = 0;
  454. unitp->num_fans_present = MIN_FAN_BANKS;
  455. unitp->num_fans_failed = ENVCTRL_CHAR_ZERO;
  456. unitp->AFB_present = B_TRUE;
  457. unitp->dip = dip;
  458. #ifdef DEBUG
  459. if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
  460. DDI_PROP_DONTPASS, ENVCTRL_PANEL_LEDS_PR,
  461. &reg_prop, &len) == DDI_PROP_SUCCESS)
  462. ddi_prop_free((void *)reg_prop);
  463. ASSERT(len != 0);
  464. len = 0;
  465. if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
  466. DDI_PROP_DONTPASS, ENVCTRL_PANEL_LEDS_STA,
  467. &reg_prop, &len) == DDI_PROP_SUCCESS)
  468. ddi_prop_free((void *)reg_prop);
  469. ASSERT(len != 0);
  470. len = 0;
  471. if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
  472. DDI_PROP_DONTPASS, ENVCTRL_DISK_LEDS_STA,
  473. &reg_prop, &len) == DDI_PROP_SUCCESS)
  474. ddi_prop_free((void *)reg_prop);
  475. ASSERT(len != 0);
  476. #endif /* DEBUG */
  477. /*
  478. * if we have prom fan tables, overide the static tables in
  479. * header file.
  480. */
  481. if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip,
  482. DDI_PROP_DONTPASS, "cpu-fan-speeds",
  483. &creg_prop, &len) == DDI_PROP_SUCCESS) {
  484. tblsz = (sizeof (acme_cpu_fanspd) / sizeof (short));
  485. if (len <= tblsz) {
  486. for (i = 0; i < len; i++) {
  487. acme_cpu_fanspd[i] = creg_prop[i];
  488. }
  489. }
  490. ddi_prop_free((void *)creg_prop);
  491. }
  492. len = 0;
  493. if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip,
  494. DDI_PROP_DONTPASS, "ps-fan-speeds",
  495. &creg_prop, &len) == DDI_PROP_SUCCESS) {
  496. tblsz = (sizeof (acme_ps_fanspd) / sizeof (short));
  497. if (len <= tblsz) {
  498. for (i = 0; i < len; i++) {
  499. acme_ps_fanspd[i] = creg_prop[i];
  500. }
  501. }
  502. ddi_prop_free((void *)creg_prop);
  503. }
  504. switch (ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
  505. "fan-override", -1)) {
  506. case 1:
  507. case 2:
  508. unitp->AFB_present = B_TRUE;
  509. break;
  510. case 0:
  511. default:
  512. unitp->AFB_present = B_FALSE;
  513. break;
  514. }
  515. /* For debug */
  516. if (envctrl_afb_present) {
  517. unitp->AFB_present = B_TRUE;
  518. }
  519. if (unitp->AFB_present == B_TRUE)
  520. unitp->num_fans_present++;
  521. /* initialize the envctrl bus controller */
  522. mutex_exit(&unitp->umutex);
  523. unitp->initting = B_TRUE;
  524. envctrl_init_bus(unitp);
  525. unitp->initting = B_FALSE;
  526. drv_usecwait(1000);
  527. mutex_enter(&unitp->umutex);
  528. /* Initialize the PCF8583 eggtimer registers */
  529. buf[0] = ALARM_CTR_REG_MINS;
  530. buf[1] = 0x0;
  531. status = eHc_write_pcf8583((struct eHc_envcunit *)unitp,
  532. PCF8583_BASE_ADDR | 0, buf, 2);
  533. if (status != DDI_SUCCESS)
  534. cmn_err(CE_WARN, "write to PCF8583 failed\n");
  535. buf[0] = ALARM_REG_MINS;
  536. buf[1] = 0x58;
  537. status = eHc_write_pcf8583((struct eHc_envcunit *)unitp,
  538. PCF8583_BASE_ADDR | 0, buf, 2);
  539. if (status != DDI_SUCCESS)
  540. cmn_err(CE_WARN, "write to PCF8583 failed\n");
  541. buf[0] = ALARM_TIMER_REG;
  542. buf[1] = 0x80;
  543. status = eHc_write_pcf8583((struct eHc_envcunit *)unitp,
  544. PCF8583_BASE_ADDR | 0, buf, 2);
  545. if (status != DDI_SUCCESS)
  546. cmn_err(CE_WARN, "write to PCF8583 failed\n");
  547. unitp->timeout_id = 0;
  548. unitp->blink_timeout_id = 0;
  549. if (envctrl_numcpus > 1) {
  550. unitp->num_cpus_present = envctrl_numcpus;
  551. }
  552. envctrl_probe_cpus(unitp);
  553. envctrl_ps_probe(unitp);
  554. /*
  555. * clear the fan failures, if any before we do
  556. * real work
  557. */
  558. unitp->initting = B_TRUE;
  559. envctrl_fan_fail_service(unitp);
  560. unitp->initting = B_FALSE;
  561. /*
  562. * we need to init the fan kstats before the tempr_poll
  563. */
  564. envctrl_add_kstats(unitp);
  565. envctrl_init_fan_kstats(unitp);
  566. envctrl_init_encl_kstats(unitp);
  567. if (unitp->activity_led_blink == B_TRUE) {
  568. unitp->present_led_state = B_FALSE;
  569. mutex_exit(&unitp->umutex);
  570. envctrl_led_blink((void *)unitp);
  571. mutex_enter(&unitp->umutex);
  572. } else {
  573. fspval = ENVCTRL_FSP_ACTIVE;
  574. envctrl_set_fsp(unitp, &fspval);
  575. }
  576. #ifndef TESTBED
  577. for (i = 0; i < ENVCTRL_MAX_CPUS; i++) {
  578. if (unitp->cpu_pr_location[i] == B_TRUE) {
  579. cputemp = envctrl_get_cpu_temp(unitp, i);
  580. envctrl_add_encl_kstats(unitp, ENVCTRL_ENCL_CPUTEMPR,
  581. i, cputemp);
  582. if (cputemp >= MAX_CPU_TEMP) {
  583. if (!(envctrl_power_off_overide)) {
  584. cmn_err(CE_WARN,
  585. "CPU %d OVERHEATING!!", i);
  586. unitp->shutdown = B_TRUE;
  587. } else {
  588. cmn_err(CE_WARN,
  589. "CPU %d OVERHEATING!!", i);
  590. }
  591. }
  592. }
  593. }
  594. #else
  595. cputemp = envctrl_get_cpu_temp(unitp, 0);
  596. envctrl_add_encl_kstats(unitp, ENVCTRL_ENCL_CPUTEMPR, INSTANCE_0,
  597. cputemp);
  598. #endif
  599. mutex_exit(&unitp->umutex);
  600. envctrl_tempr_poll((void *)unitp);
  601. /*
  602. * interpose envctrl's abort sequence handler
  603. */
  604. if (envctrl_handler) {
  605. abort_seq_handler = envctrl_abort_seq_handler;
  606. }
  607. ddi_report_dev(dip);
  608. return (DDI_SUCCESS);
  609. remhardintr1:
  610. ddi_remove_intr(dip, (uint_t)1, unitp->ic_trap_cookie);
  611. remhardintr:
  612. ddi_remove_intr(dip, (uint_t)0, unitp->ic_trap_cookie);
  613. remlock:
  614. mutex_destroy(&unitp->umutex);
  615. failed:
  616. if (unitp->ctlr_handle)
  617. ddi_regs_map_free(&unitp->ctlr_handle);
  618. cmn_err(CE_WARN, "envctrl_attach:failed.\n");
  619. return (DDI_FAILURE);
  620. }
  621. static int
  622. envctrl_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
  623. {
  624. int instance;
  625. struct envctrlunit *unitp;
  626. instance = ddi_get_instance(dip);
  627. unitp = ddi_get_soft_state(envctrlsoft_statep, instance);
  628. switch (cmd) {
  629. case DDI_DETACH:
  630. if (envctrl_allow_detach) {
  631. if (unitp->psksp != NULL) {
  632. kstat_delete(unitp->psksp);
  633. }
  634. if (unitp->fanksp != NULL) {
  635. kstat_delete(unitp->fanksp);
  636. }
  637. if (unitp->enclksp != NULL) {
  638. kstat_delete(unitp->enclksp);
  639. }
  640. if (unitp->timeout_id != 0) {
  641. (void) untimeout(unitp->timeout_id);
  642. unitp->timeout_id = 0;
  643. }
  644. if (unitp->blink_timeout_id != 0) {
  645. (void) untimeout(unitp->blink_timeout_id);
  646. unitp->blink_timeout_id = 0;
  647. }
  648. ddi_remove_minor_node(dip, NULL);
  649. ddi_remove_intr(dip, (uint_t)0, unitp->ic_trap_cookie);
  650. ddi_remove_intr(dip, (uint_t)1, unitp->ic_trap_cookie);
  651. ddi_regs_map_free(&unitp->ctlr_handle);
  652. mutex_destroy(&unitp->umutex);
  653. return (DDI_SUCCESS);
  654. } else {
  655. return (DDI_FAILURE);
  656. }
  657. case DDI_SUSPEND:
  658. if (!(unitp = ddi_get_soft_state(envctrlsoft_statep, instance)))
  659. return (DDI_FAILURE);
  660. mutex_enter(&unitp->umutex);
  661. if (unitp->suspended) {
  662. cmn_err(CE_WARN, "envctrl already suspended\n");
  663. mutex_exit(&unitp->umutex);
  664. return (DDI_FAILURE);
  665. }
  666. unitp->suspended = 1;
  667. mutex_exit(&unitp->umutex);
  668. return (DDI_SUCCESS);
  669. default:
  670. cmn_err(CE_WARN, "envctrl suspend general fault\n");
  671. return (DDI_FAILURE);
  672. }
  673. }
  674. /* ARGSUSED */
  675. int
  676. envctrl_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
  677. void **result)
  678. {
  679. dev_t dev = (dev_t)arg;
  680. struct envctrlunit *unitp;
  681. int ret;
  682. minor_t instance = getminor(dev);
  683. switch (infocmd) {
  684. case DDI_INFO_DEVT2DEVINFO:
  685. if ((unitp = (struct envctrlunit *)
  686. ddi_get_soft_state(envctrlsoft_statep,
  687. instance)) != NULL) {
  688. *result = unitp->dip;
  689. ret = DDI_SUCCESS;
  690. } else {
  691. *result = NULL;
  692. ret = DDI_FAILURE;
  693. }
  694. break;
  695. case DDI_INFO_DEVT2INSTANCE:
  696. *result = (void *)(uintptr_t)instance;
  697. ret = DDI_SUCCESS;
  698. break;
  699. default:
  700. ret = DDI_FAILURE;
  701. break;
  702. }
  703. return (ret);
  704. }
  705. /* ARGSUSED */
  706. static int
  707. envctrl_open(queue_t *q, dev_t *dev, int flag, int sflag, cred_t *credp)
  708. {
  709. struct envctrlunit *unitp;
  710. int status = 0;
  711. int instance;
  712. instance = getminor(*dev);
  713. if (instance < 0)
  714. return (ENXIO);
  715. unitp = (struct envctrlunit *)
  716. ddi_get_soft_state(envctrlsoft_statep, instance);
  717. if (unitp == NULL)
  718. return (ENXIO);
  719. mutex_enter(&unitp->umutex);
  720. if (flag & FWRITE) {
  721. if ((unitp->oflag & FWRITE)) {
  722. mutex_exit(&unitp->umutex);
  723. return (EBUSY);
  724. } else {
  725. unitp->oflag |= FWRITE;
  726. }
  727. }
  728. q->q_ptr = WR(q)->q_ptr = (caddr_t)unitp;
  729. /*
  730. * if device is open with O_NONBLOCK flag set, let read(2) return 0
  731. * if no data waiting to be read. Writes will block on flow control.
  732. */
  733. /* enable the stream */
  734. qprocson(q);
  735. unitp->readq = RD(q);
  736. unitp->writeq = WR(q);
  737. unitp->msg = (mblk_t *)NULL;
  738. mutex_exit(&unitp->umutex);
  739. return (status);
  740. }
  741. /* ARGSUSED */
  742. static int
  743. envctrl_close(queue_t *q, int flag, cred_t *cred_p)
  744. {
  745. struct envctrlunit *unitp;
  746. unitp = (struct envctrlunit *)q->q_ptr;
  747. mutex_enter(&unitp->umutex);
  748. unitp->oflag = B_FALSE;
  749. unitp->current_mode = ENVCTRL_NORMAL_MODE;
  750. /* disable the stream */
  751. q->q_ptr = WR(q)->q_ptr = NULL;
  752. qprocsoff(q);
  753. mutex_exit(&unitp->umutex);
  754. return (DDI_SUCCESS);
  755. }
  756. /*
  757. * standard put procedure for envctrl
  758. */
  759. static int
  760. envctrl_wput(queue_t *q, mblk_t *mp)
  761. {
  762. struct msgb *mp1;
  763. struct envctrlunit *unitp;
  764. struct iocblk *iocp;
  765. struct copyresp *csp;
  766. struct envctrl_tda8444t_chip *fanspeed;
  767. struct envctrl_pcf8574_chip *ledchip;
  768. struct envctrl_pcf8591_chip *temp, *a_fanspeed;
  769. struct copyreq *cqp;
  770. int cmd;
  771. unitp = (struct envctrlunit *)q->q_ptr;
  772. switch (DB_TYPE(mp)) {
  773. case M_DATA:
  774. while (mp) {
  775. DB_TYPE(mp) = M_DATA;
  776. mp1 = unlinkb(mp);
  777. mp->b_cont = NULL;
  778. if ((mp->b_wptr - mp->b_rptr) <= 0) {
  779. freemsg(mp);
  780. } else {
  781. (void) putq(q, mp);
  782. }
  783. mp = mp1;
  784. }
  785. break;
  786. case M_IOCTL:
  787. {
  788. iocp = (struct iocblk *)(void *)mp->b_rptr;
  789. cmd = iocp->ioc_cmd;
  790. switch (cmd) {
  791. case ENVCTRL_IOC_SETMODE:
  792. case ENVCTRL_IOC_GETMODE:
  793. if (iocp->ioc_count == TRANSPARENT) {
  794. mcopyin(mp, *(caddr_t *)mp->b_cont->b_rptr,
  795. sizeof (uchar_t), NULL);
  796. qreply(q, mp);
  797. } else {
  798. miocnak(q, mp, 0, EINVAL);
  799. }
  800. break;
  801. case ENVCTRL_IOC_RESETTMPR:
  802. /*
  803. * For diags, cancel the current temp poll
  804. * and reset it for a new one.
  805. */
  806. if (unitp->current_mode == ENVCTRL_DIAG_MODE) {
  807. if (unitp->timeout_id != 0) {
  808. (void) untimeout(unitp->timeout_id);
  809. unitp->timeout_id = 0;
  810. }
  811. envctrl_tempr_poll((void *)unitp);
  812. miocack(q, mp, 0, 0);
  813. } else {
  814. miocnak(q, mp, 0, EINVAL);
  815. }
  816. break;
  817. case ENVCTRL_IOC_GETTEMP:
  818. if (iocp->ioc_count == TRANSPARENT) {
  819. mcopyin(mp, *(caddr_t *)mp->b_cont->b_rptr,
  820. sizeof (struct envctrl_pcf8591_chip), NULL);
  821. qreply(q, mp);
  822. } else {
  823. miocnak(q, mp, 0, EINVAL);
  824. }
  825. break;
  826. case ENVCTRL_IOC_SETTEMP:
  827. if (unitp->current_mode == ENVCTRL_DIAG_MODE &&
  828. iocp->ioc_count == TRANSPARENT) {
  829. mcopyin(mp, *(caddr_t *)mp->b_cont->b_rptr,
  830. sizeof (uint8_t), NULL);
  831. qreply(q, mp);
  832. } else {
  833. miocnak(q, mp, 0, EINVAL);
  834. }
  835. break;
  836. case ENVCTRL_IOC_SETWDT:
  837. if (unitp->current_mode == ENVCTRL_DIAG_MODE &&
  838. iocp->ioc_count == TRANSPARENT) {
  839. mcopyin(mp, *(caddr_t *)mp->b_cont->b_rptr,
  840. sizeof (uint8_t), NULL);
  841. qreply(q, mp);
  842. } else {
  843. miocnak(q, mp, 0, EINVAL);
  844. }
  845. break;
  846. case ENVCTRL_IOC_SETFAN:
  847. /*
  848. * we must be in diag mode before we can
  849. * set any fan speeds.
  850. */
  851. if (unitp->current_mode == ENVCTRL_DIAG_MODE &&
  852. iocp->ioc_count == TRANSPARENT) {
  853. mcopyin(mp, *(caddr_t *)mp->b_cont->b_rptr,
  854. sizeof (struct envctrl_tda8444t_chip),
  855. NULL);
  856. qreply(q, mp);
  857. } else {
  858. miocnak(q, mp, 0, EINVAL);
  859. }
  860. break;
  861. case ENVCTRL_IOC_GETFAN:
  862. if (iocp->ioc_count == TRANSPARENT) {
  863. mcopyin(mp, *(caddr_t *)mp->b_cont->b_rptr,
  864. sizeof (struct envctrl_pcf8591_chip), NULL);
  865. qreply(q, mp);
  866. } else {
  867. miocnak(q, mp, 0, EINVAL);
  868. }
  869. break;
  870. case ENVCTRL_IOC_SETFSP:
  871. if (iocp->ioc_count == TRANSPARENT) {
  872. mcopyin(mp, *(caddr_t *)mp->b_cont->b_rptr,
  873. sizeof (uint8_t), NULL);
  874. qreply(q, mp);
  875. } else {
  876. miocnak(q, mp, 0, EINVAL);
  877. }
  878. break;
  879. case ENVCTRL_IOC_SETDSKLED:
  880. case ENVCTRL_IOC_GETDSKLED:
  881. if (iocp->ioc_count == TRANSPARENT) {
  882. mcopyin(mp, *(caddr_t *)mp->b_cont->b_rptr,
  883. sizeof (struct envctrl_pcf8574_chip), NULL);
  884. qreply(q, mp);
  885. } else {
  886. miocnak(q, mp, 0, EINVAL);
  887. }
  888. break;
  889. default:
  890. miocnak(q, mp, 0, EINVAL);
  891. break;
  892. }
  893. break;
  894. }
  895. case M_IOCDATA:
  896. {
  897. uint8_t *tempr, *wdval;
  898. long state;
  899. csp = (struct copyresp *)(void *)mp->b_rptr;
  900. /*
  901. * If copy request failed, quit now
  902. */
  903. if (csp->cp_rval != 0) {
  904. miocnak(q, mp, 0, EINVAL);
  905. return (0);
  906. }
  907. cqp = (struct copyreq *)(void *)mp->b_rptr;
  908. cmd = csp->cp_cmd;
  909. state = (long)cqp->cq_private;
  910. switch (cmd) {
  911. case ENVCTRL_IOC_SETFAN:
  912. fanspeed = (struct envctrl_tda8444t_chip *)
  913. (void *)mp->b_cont->b_rptr;
  914. mutex_enter(&unitp->umutex);
  915. if (envctrl_xmit(unitp, (caddr_t *)(void *)fanspeed,
  916. fanspeed->type) == DDI_FAILURE) {
  917. /*
  918. * Fix for a ADF bug
  919. * move mutex to after fan fail call
  920. * bugid 4016121
  921. */
  922. envctrl_fan_fail_service(unitp);
  923. mutex_exit(&unitp->umutex);
  924. miocnak(q, mp, 0, EINVAL);
  925. } else {
  926. mutex_exit(&unitp->umutex);
  927. miocack(q, mp, 0, 0);
  928. }
  929. break;
  930. case ENVCTRL_IOC_SETFSP:
  931. wdval = (uint8_t *)(void *)mp->b_cont->b_rptr;
  932. mutex_enter(&unitp->umutex);
  933. /*
  934. * If a user is in normal mode and they try
  935. * to set anything other than a disk fault or
  936. * a gen fault it is an invalid operation.
  937. * in diag mode we allow everything to be
  938. * twiddled.
  939. */
  940. if (unitp->current_mode == ENVCTRL_NORMAL_MODE) {
  941. if (*wdval & ~ENVCTRL_FSP_USRMASK) {
  942. mutex_exit(&unitp->umutex);
  943. miocnak(q, mp, 0, EINVAL);
  944. break;
  945. }
  946. }
  947. envctrl_set_fsp(unitp, wdval);
  948. mutex_exit(&unitp->umutex);
  949. miocack(q, mp, 0, 0);
  950. break;
  951. case ENVCTRL_IOC_SETDSKLED:
  952. ledchip = (struct envctrl_pcf8574_chip *)
  953. (void *)mp->b_cont->b_rptr;
  954. mutex_enter(&unitp->umutex);
  955. if (envctrl_set_dskled(unitp, ledchip)) {
  956. miocnak(q, mp, 0, EINVAL);
  957. } else {
  958. miocack(q, mp, 0, 0);
  959. }
  960. mutex_exit(&unitp->umutex);
  961. break;
  962. case ENVCTRL_IOC_GETDSKLED:
  963. if (state == -1) {
  964. miocack(q, mp, 0, 0);
  965. break;
  966. }
  967. ledchip = (struct envctrl_pcf8574_chip *)
  968. (void *)mp->b_cont->b_rptr;
  969. mutex_enter(&unitp->umutex);
  970. if (envctrl_get_dskled(unitp, ledchip)) {
  971. miocnak(q, mp, 0, EINVAL);
  972. } else {
  973. mcopyout(mp, (void *)-1,
  974. sizeof (struct envctrl_pcf8574_chip),
  975. csp->cp_private, NULL);
  976. qreply(q, mp);
  977. }
  978. mutex_exit(&unitp->umutex);
  979. break;
  980. case ENVCTRL_IOC_GETTEMP:
  981. /* Get the user buffer address */
  982. if (state == -1) {
  983. miocack(q, mp, 0, 0);
  984. break;
  985. }
  986. temp = (struct envctrl_pcf8591_chip *)
  987. (void *)mp->b_cont->b_rptr;
  988. mutex_enter(&unitp->umutex);
  989. envctrl_recv(unitp, (caddr_t *)(void *)temp, PCF8591);
  990. mutex_exit(&unitp->umutex);
  991. mcopyout(mp, (void *)-1,
  992. sizeof (struct envctrl_pcf8591_chip),
  993. csp->cp_private, NULL);
  994. qreply(q, mp);
  995. break;
  996. case ENVCTRL_IOC_GETFAN:
  997. /* Get the user buffer address */
  998. if (state == -1) {
  999. miocack(q, mp, 0, 0);
  1000. break;
  1001. }
  1002. a_fanspeed = (struct envctrl_pcf8591_chip *)
  1003. (void *)mp->b_cont->b_rptr;
  1004. mutex_enter(&unitp->umutex);
  1005. envctrl_recv(unitp, (caddr_t *)(void *)a_fanspeed,
  1006. PCF8591);
  1007. mutex_exit(&unitp->umutex);
  1008. mcopyout(mp, (void *)-1,
  1009. sizeof (struct envctrl_pcf8591_chip),
  1010. csp->cp_private, NULL);
  1011. qreply(q, mp);
  1012. break;
  1013. case ENVCTRL_IOC_SETTEMP:
  1014. tempr = (uint8_t *)(void *)mp->b_cont->b_rptr;
  1015. if (*tempr > MAX_DIAG_TEMPR) {
  1016. miocnak(q, mp, 0, EINVAL);
  1017. } else {
  1018. mutex_enter(&unitp->umutex);
  1019. envctrl_get_sys_temperatures(unitp, tempr);
  1020. mutex_exit(&unitp->umutex);
  1021. miocack(q, mp, 0, 0);
  1022. }
  1023. break;
  1024. case ENVCTRL_IOC_SETWDT:
  1025. /* reset watchdog timeout period */
  1026. wdval = (uint8_t *)(void *)mp->b_cont->b_rptr;
  1027. if (*wdval > MAX_CL_VAL) {
  1028. miocnak(q, mp, 0, EINVAL);
  1029. } else {
  1030. mutex_enter(&unitp->umutex);
  1031. envctrl_reset_watchdog(unitp, wdval);
  1032. mutex_exit(&unitp->umutex);
  1033. miocack(q, mp, 0, 0);
  1034. }
  1035. break;
  1036. case ENVCTRL_IOC_GETMODE:
  1037. /* Get the user buffer address */
  1038. if (state == -1) {
  1039. miocack(q, mp, 0, 0);
  1040. break;
  1041. }
  1042. tempr = (uchar_t *)(void *)mp->b_cont->b_rptr;
  1043. *tempr = unitp->current_mode;
  1044. mcopyout(mp, (void *)-1, sizeof (uchar_t),
  1045. csp->cp_private, NULL);
  1046. qreply(q, mp);
  1047. break;
  1048. case ENVCTRL_IOC_SETMODE:
  1049. /* Set mode */
  1050. wdval = (uint8_t *)(void *)mp->b_cont->b_rptr;
  1051. if (*wdval == ENVCTRL_DIAG_MODE || *wdval ==
  1052. ENVCTRL_NORMAL_MODE) {
  1053. mutex_enter(&unitp->umutex);
  1054. unitp->current_mode = *wdval;
  1055. if (unitp->timeout_id != 0 &&
  1056. *wdval == ENVCTRL_DIAG_MODE) {
  1057. (void) untimeout(unitp->timeout_id);
  1058. unitp->timeout_id =
  1059. (timeout(envctrl_tempr_poll,
  1060. (caddr_t)unitp,
  1061. overtemp_timeout_hz));
  1062. }
  1063. if (*wdval == ENVCTRL_NORMAL_MODE) {
  1064. envctrl_get_sys_temperatures(unitp,
  1065. (uint8_t *)NULL);
  1066. /*
  1067. * going to normal mode we
  1068. * need to go to diag mode
  1069. * just in case we have
  1070. * injected a fan fault. It
  1071. * may not be cleared and if
  1072. * we call fan_failsrvc it will
  1073. * power off the ystem if we are
  1074. * in NORMAL_MODE. Also we need
  1075. * to delay 1 bit of time here
  1076. * to allow the fans to rotate
  1077. * back up and clear the intr
  1078. * after we get the sys temps.
  1079. */
  1080. unitp->current_mode =
  1081. ENVCTRL_DIAG_MODE;
  1082. envctrl_fan_fail_service(unitp);
  1083. unitp->current_mode =
  1084. ENVCTRL_NORMAL_MODE;
  1085. }
  1086. mutex_exit(&unitp->umutex);
  1087. miocack(q, mp, 0, 0);
  1088. } else {
  1089. miocnak(q, mp, 0, EINVAL);
  1090. }
  1091. break;
  1092. default:
  1093. freemsg(mp);
  1094. break;
  1095. }
  1096. break;
  1097. }
  1098. case M_FLUSH:
  1099. if (*mp->b_rptr & FLUSHR) {
  1100. *mp->b_rptr &= ~FLUSHW;
  1101. qreply(q, mp);
  1102. } else {
  1103. freemsg(mp);
  1104. }
  1105. break;
  1106. default:
  1107. freemsg(mp);
  1108. break;
  1109. }
  1110. return (0);
  1111. }
  1112. uint_t
  1113. envctrl_bus_isr(caddr_t arg)
  1114. {
  1115. struct envctrlunit *unitp = (struct envctrlunit *)(void *)arg;
  1116. int ic = DDI_INTR_UNCLAIMED;
  1117. mutex_enter(&unitp->umutex);
  1118. /*
  1119. * NOT USED
  1120. */
  1121. mutex_exit(&unitp->umutex);
  1122. return (ic);
  1123. }
  1124. uint_t
  1125. envctrl_dev_isr(caddr_t arg)
  1126. {
  1127. struct envctrlunit *unitp = (struct envctrlunit *)(void *)arg;
  1128. uint8_t recv_data;
  1129. int ic;
  1130. int retrys = 0;
  1131. int status;
  1132. ic = DDI_INTR_UNCLAIMED;
  1133. mutex_enter(&unitp->umutex);
  1134. /*
  1135. * First check to see if it is an interrupt for us by
  1136. * looking at the "ganged" interrrupt and vector
  1137. * according to the major type
  1138. * 0x70 is the addr of the ganged interrupt controller.
  1139. * Address map for the port byte read is as follows
  1140. * MSB
  1141. * -------------------------
  1142. * | | | | | | | | |
  1143. * -------------------------
  1144. * P7 P6 P5 P4 P3 P2 P1 P0
  1145. * P0 = Power Supply 1 intr
  1146. * P1 = Power Supply 2 intr
  1147. * P2 = Power Supply 3 intr
  1148. * P3 = Dlfop enable for fan sped set
  1149. * P4 = ENVCTRL_ Fan Fail intr
  1150. * P5 = Front Panel Interrupt
  1151. * P6 = Power Fail Detect Low.
  1152. * P7 = Enable Interrupts to system
  1153. */
  1154. retry:
  1155. status = eHc_read_pcf8574a((struct eHc_envcunit *)unitp,
  1156. PCF8574A_BASE_ADDR | ENVCTRL_PCF8574_DEV0, &recv_data, 1);
  1157. /*
  1158. * This extra read is needed since the first read is discarded
  1159. * and the second read seems to return 0xFF.
  1160. */
  1161. if (recv_data == 0xFF) {
  1162. status = eHc_read_pcf8574a((struct eHc_envcunit *)unitp,
  1163. PCF8574A_BASE_ADDR | ENVCTRL_PCF8574_DEV0, &recv_data, 1);
  1164. }
  1165. if (envctrl_debug_flags)
  1166. cmn_err(CE_WARN, "envctrl_dev_isr: status= %d, data = %x\n",
  1167. status, recv_data);
  1168. /*
  1169. * if the i2c bus is hung it is imperative that this
  1170. * be cleared on an interrupt or else it will
  1171. * hang the system with continuous interrupts
  1172. */
  1173. if (status == DDI_FAILURE) {
  1174. drv_usecwait(1000);
  1175. if (retrys < envctrl_max_retries) {
  1176. retrys++;
  1177. goto retry;
  1178. } else {
  1179. if (envctrl_debug_flags)
  1180. cmn_err(CE_WARN,
  1181. "DEVISR FAILED received 0x%x\n", recv_data);
  1182. mutex_exit(&unitp->umutex);
  1183. envctrl_init_bus(unitp);
  1184. mutex_enter(&unitp->umutex);
  1185. envctrl_ps_probe(unitp);
  1186. mutex_exit(&unitp->umutex);
  1187. ic = DDI_INTR_CLAIMED;
  1188. return (ic);
  1189. }
  1190. }
  1191. /*
  1192. * Port 0 = PS1 interrupt
  1193. * Port 1 = PS2 Interrupt
  1194. * Port 2 = PS3 Interrupt
  1195. * Port 3 = SPARE
  1196. * Port 4 = Fan Fail Intr
  1197. * Port 5 = Front Panle Module intr
  1198. * Port 6 = Keyswitch Intr
  1199. * Port 7 = ESINTR ENABLE ???
  1200. */
  1201. if (!(recv_data & ENVCTRL_PCF8574_PORT0)) {
  1202. envctrl_PS_intr_service(unitp, PS1);
  1203. ic = DDI_INTR_CLAIMED;
  1204. }
  1205. if (!(recv_data & ENVCTRL_PCF8574_PORT1)) {
  1206. envctrl_PS_intr_service(unitp, PS2);
  1207. ic = DDI_INTR_CLAIMED;
  1208. }
  1209. if (!(recv_data & ENVCTRL_PCF8574_PORT2)) {
  1210. envctrl_PS_intr_service(unitp, PS3);
  1211. ic = DDI_INTR_CLAIMED;
  1212. }
  1213. if (!(recv_data & ENVCTRL_PCF8574_PORT3)) {
  1214. ic = DDI_INTR_CLAIMED;
  1215. }
  1216. if (!(recv_data & ENVCTRL_PCF8574_PORT4)) {
  1217. /*
  1218. * Check for a fan fail
  1219. * Single fan fail
  1220. * shutdown system
  1221. */
  1222. envctrl_fan_fail_service(unitp);
  1223. ic = DDI_INTR_CLAIMED;
  1224. }
  1225. if (!(recv_data & ENVCTRL_PCF8574_PORT5)) {
  1226. (void) envctrl_get_fpm_status(unitp);
  1227. ic = DDI_INTR_CLAIMED;
  1228. }
  1229. if (!(recv_data & ENVCTRL_PCF8574_PORT6)) {
  1230. ic = DDI_INTR_CLAIMED;
  1231. }
  1232. if (!(recv_data & ENVCTRL_PCF8574_PORT7)) {
  1233. ic = DDI_INTR_CLAIMED;
  1234. }
  1235. if ((recv_data == 0xFF)) {
  1236. ic = DDI_INTR_CLAIMED;
  1237. }
  1238. mutex_exit(&unitp->umutex);
  1239. return (ic);
  1240. }
  1241. static void
  1242. envctrl_init_bus(struct envctrlunit *unitp)
  1243. {
  1244. int i;
  1245. uint8_t noval = NULL;
  1246. struct envctrl_tda8444t_chip fan;
  1247. int fans[] = {ENVCTRL_CPU_FANS, ENVCTRL_PS_FANS, ENVCTRL_AFB_FANS};
  1248. mutex_enter(&unitp->umutex);
  1249. /* Sets the Mode to 808x type bus */
  1250. ddi_put8(unitp->ctlr_handle,
  1251. &unitp->bus_ctl_regs->s0, ENVCTRL_CHAR_ZERO);
  1252. /* SET UP SLAVE ADDR XXX Required..send 0x80 */
  1253. ddi_put8(unitp->ctlr_handle, &unitp->bus_ctl_regs->s1,
  1254. ENVCTRL_BUS_INIT0);
  1255. (void) ddi_put8(unitp->ctlr_handle, &unitp->bus_ctl_regs->s0,
  1256. ENVCTRL_BUS_INIT1);
  1257. /* Set the clock now */
  1258. ddi_put8(unitp->ctlr_handle,
  1259. &unitp->bus_ctl_regs->s1, ENVCTRL_BUS_CLOCK0);
  1260. /* S0 is now S2 necause of the previous write to S1 */
  1261. /* clock= 12MHz, SCL=90KHz */
  1262. ddi_put8(unitp->ctlr_handle,
  1263. &unitp->bus_ctl_regs->s0, ENVCTRL_BUS_CLOCK1);
  1264. /* Enable serial interface */
  1265. ddi_put8(unitp->ctlr_handle,
  1266. &unitp->bus_ctl_regs->s1, ENVCTRL_BUS_ESI);
  1267. envctrl_stop_clock(unitp);
  1268. /*
  1269. * This has been added here because the DAC is powered
  1270. * on at "0". When the reset_dflop routine is called
  1271. * this switched the fans from blast to DAC control.
  1272. * if the DAC is at "0", then the fans momentarily lose
  1273. * power until the temp polling and fan set routine is
  1274. * first called. If the fans lose power, then there is
  1275. * a fan fault generated and the system will power off.
  1276. * We only want to do this IF the bus is first being
  1277. * initted. This will cause errors in Sunvts if we reset
  1278. * the fan speed under normal operation. Sometimes we need
  1279. * to be able to induce fan faults. Init bus is a common
  1280. * routine to unwedge the i2c bus in some cases.
  1281. */
  1282. if (unitp->initting == B_TRUE) {
  1283. fan.chip_num = ENVCTRL_TDA8444T_DEV7;
  1284. fan.val = INIT_FAN_VAL;
  1285. for (i = 0; i < sizeof (fans)/sizeof (int); i++) {
  1286. fan.fan_num = fans[i];
  1287. if ((fans[i] == ENVCTRL_AFB_FANS) &&
  1288. (unitp->AFB_present == B_FALSE))
  1289. continue;
  1290. (void) envctrl_xmit(unitp, (caddr_t *)(void *)&fan,
  1291. TDA8444T);
  1292. }
  1293. }
  1294. envctrl_reset_dflop(unitp);
  1295. envctrl_enable_devintrs(unitp);
  1296. unitp->current_mode = ENVCTRL_NORMAL_MODE;
  1297. envctrl_reset_watchdog(unitp, &noval);
  1298. mutex_exit(&unitp->umutex);
  1299. }
  1300. static int
  1301. envctrl_xmit(struct envctrlunit *unitp, caddr_t *data, int chip_type)
  1302. {
  1303. struct envctrl_tda8444t_chip *fanspeed;
  1304. struct envctrl_pcf8574_chip *ioport;
  1305. uint8_t slave_addr;
  1306. uint8_t buf[2];
  1307. int retrys = 0;
  1308. int status;
  1309. ASSERT(MUTEX_HELD(&unitp->umutex));
  1310. switch (chip_type) {
  1311. case TDA8444T:
  1312. fanspeed = (struct envctrl_tda8444t_chip *)data;
  1313. if (fanspeed->chip_num > ENVCTRL_FAN_ADDR_MAX) {
  1314. return (DDI_FAILURE);
  1315. }
  1316. if (fanspeed->fan_num > ENVCTRL_PORT7) {
  1317. return (DDI_FAILURE);
  1318. }
  1319. if (fanspeed->val > MAX_FAN_VAL) {
  1320. return (DDI_FAILURE);
  1321. }
  1322. retry0:
  1323. slave_addr = (TDA8444T_BASE_ADDR | fanspeed->chip_num);
  1324. buf[0] = fanspeed->val;
  1325. status = eHc_write_tda8444((struct eHc_envcunit *)unitp,
  1326. TDA8444T_BASE_ADDR | fanspeed->chip_num, 0xF,
  1327. fanspeed->fan_num, buf, 1);
  1328. if (status != DDI_SUCCESS) {
  1329. drv_usecwait(1000);
  1330. if (retrys < envctrl_max_retries) {
  1331. retrys++;
  1332. goto retry0;
  1333. } else {
  1334. mutex_exit(&unitp->umutex);
  1335. envctrl_init_bus(unitp);
  1336. mutex_enter(&unitp->umutex);
  1337. if (envctrl_debug_flags)
  1338. cmn_err(CE_WARN,
  1339. "envctrl_xmit: Write to TDA8444 " \
  1340. "failed\n");
  1341. return (DDI_FAILURE);
  1342. }
  1343. }
  1344. /*
  1345. * Update the kstats.
  1346. */
  1347. switch (fanspeed->fan_num) {
  1348. case ENVCTRL_CPU_FANS:
  1349. unitp->fan_kstats[ENVCTRL_FAN_TYPE_CPU].fanspeed =
  1350. fanspeed->val;
  1351. break;
  1352. case ENVCTRL_PS_FANS:
  1353. unitp->fan_kstats[ENVCTRL_FAN_TYPE_PS].fanspeed =
  1354. fanspeed->val;
  1355. break;
  1356. case ENVCTRL_AFB_FANS:
  1357. unitp->fan_kstats[ENVCTRL_FAN_TYPE_AFB].fanspeed =
  1358. fanspeed->val;
  1359. break;
  1360. default:
  1361. break;
  1362. }
  1363. break;
  1364. case PCF8574:
  1365. ioport = (struct envctrl_pcf8574_chip *)data;
  1366. buf[0] = ioport->val;
  1367. if (ioport->chip_num > ENVCTRL_PCF8574_DEV7)
  1368. return (DDI_FAILURE);
  1369. retry:
  1370. if (ioport->type == PCF8574A) {
  1371. slave_addr = (PCF8574A_BASE_ADDR | ioport->chip_num);
  1372. status =
  1373. eHc_write_pcf8574a((struct eHc_envcunit *)unitp,
  1374. PCF8574A_BASE_ADDR | ioport->chip_num, buf, 1);
  1375. } else {
  1376. slave_addr = (PCF8574_BASE_ADDR | ioport->chip_num);
  1377. status = eHc_write_pcf8574((struct eHc_envcunit *)unitp,
  1378. PCF8574_BASE_ADDR | ioport->chip_num, buf, 1);
  1379. }
  1380. if (status != DDI_SUCCESS) {
  1381. drv_usecwait(1000);
  1382. if (retrys < envctrl_max_retries) {
  1383. retrys++;
  1384. goto retry;
  1385. } else {
  1386. mutex_exit(&unitp->umutex);
  1387. envctrl_init_bus(unitp);
  1388. mutex_enter(&unitp->umutex);
  1389. if (envctrl_debug_flags)
  1390. cmn_err(CE_WARN, "Write to PCF8574 " \
  1391. "failed, addr = %X\n", slave_addr);
  1392. if (envctrl_debug_flags)
  1393. cmn_err(CE_WARN, "envctrl_xmit: PCF8574\
  1394. dev = %d, port = %d\n",
  1395. ioport->chip_num, ioport->type);
  1396. return (DDI_FAILURE);
  1397. }
  1398. }
  1399. break;
  1400. default:
  1401. return (DDI_FAILURE);
  1402. }
  1403. return (DDI_SUCCESS);
  1404. }
  1405. static void
  1406. envctrl_recv(struct envctrlunit *unitp, caddr_t *data, int chip_type)
  1407. {
  1408. struct envctrl_pcf8591_chip *temp;
  1409. struct envctrl_pcf8574_chip *ioport;
  1410. uint8_t slave_addr, recv_data;
  1411. int retrys = 0;
  1412. int status;
  1413. uint8_t buf[1];
  1414. ASSERT(MUTEX_HELD(&unitp->umutex));
  1415. switch (chip_type) {
  1416. case PCF8591:
  1417. temp = (struct envctrl_pcf8591_chip *)data;
  1418. slave_addr = (PCF8591_BASE_ADDR | temp->chip_num);
  1419. retry:
  1420. status = eHc_read_pcf8591((struct eHc_envcunit *)unitp,
  1421. PCF8591_BASE_ADDR | temp->chip_num & 0xF,
  1422. temp->sensor_num, 0, 0, 1, &recv_data, 1);
  1423. /*
  1424. * another place to catch the i2c bus hang on an 8591 read
  1425. * In this instance we will just return the data that is read
  1426. * after the max_retry because this could be a valid value.
  1427. */
  1428. if (status != DDI_SUCCESS) {
  1429. drv_usecwait(1000);
  1430. if (retrys < envctrl_max_retries) {
  1431. retrys++;
  1432. goto retry;
  1433. } else {
  1434. mutex_exit(&unitp->umutex);
  1435. envctrl_init_bus(unitp);
  1436. mutex_enter(&unitp->umutex);
  1437. if (envctrl_debug_flags)
  1438. cmn_err(CE_WARN, "Read from PCF8591 " \
  1439. "failed, slave_addr = %x\n",
  1440. slave_addr);
  1441. }
  1442. }
  1443. temp->temp_val = recv_data;
  1444. break;
  1445. case TDA8444T:
  1446. printf("envctrl_recv: attempting to read TDA8444T\n");
  1447. return;
  1448. case PCF8574:
  1449. ioport = (struct envctrl_pcf8574_chip *)data;
  1450. retry1:
  1451. if (ioport->chip_num > ENVCTRL_PCF8574_DEV7)
  1452. cmn_err(CE_WARN, "envctrl: dev out of range 0x%x\n",
  1453. ioport->chip_num);
  1454. if (ioport->type == PCF8574A) {
  1455. slave_addr = (PCF8574_READ_BIT | PCF8574A_BASE_ADDR |
  1456. ioport->chip_num);
  1457. status = eHc_read_pcf8574a((struct eHc_envcunit *)unitp,
  1458. PCF8574A_BASE_ADDR | ioport->chip_num, buf, 1);
  1459. } else {
  1460. slave_addr = (PCF8574_READ_BIT | PCF8574_BASE_ADDR |
  1461. ioport->chip_num);
  1462. status = eHc_read_pcf8574((struct eHc_envcunit *)unitp,
  1463. PCF8574_BASE_ADDR | ioport->chip_num, buf, 1);
  1464. }
  1465. if (status != DDI_SUCCESS) {
  1466. drv_usecwait(1000);
  1467. if (retrys < envctrl_max_retries) {
  1468. retrys++;
  1469. goto retry1;
  1470. } else {
  1471. mutex_exit(&unitp->umutex);
  1472. envctrl_init_bus(unitp);
  1473. mutex_enter(&unitp->umutex);
  1474. if (envctrl_debug_flags)
  1475. cmn_err(CE_WARN, "Read from PCF8574 "\
  1476. "failed, addr = %X\n", slave_addr);
  1477. if (envctrl_debug_flags)
  1478. cmn_err(CE_WARN, "envctrl_recv: PCF8574\
  1479. dev = %d, port = %d\n",
  1480. ioport->chip_num, ioport->type);
  1481. }
  1482. }
  1483. ioport->val = buf[0];
  1484. break;
  1485. default:
  1486. break;
  1487. }
  1488. }
  1489. static int
  1490. envctrl_get_ps_temp(struct envctrlunit *unitp, uint8_t psaddr)
  1491. {
  1492. uint8_t tempr;
  1493. int i, retrys;
  1494. int status;
  1495. uint8_t buf[4];
  1496. ASSERT(MUTEX_HELD(&unitp->umutex));
  1497. tempr = 0;
  1498. retrys = 0;
  1499. retry:
  1500. status = eHc_read_pcf8591((struct eHc_envcunit *)unitp,
  1501. PCF8591_BASE_ADDR | psaddr & 0xF, 0, 1, 0, 1, buf, 4);
  1502. tempr = 0;
  1503. for (i = 0; i < PCF8591_MAX_PORTS; i++) {
  1504. /*
  1505. * The pcf8591 will return 0xff if no port
  1506. * is there.. this is bogus for setting temps.
  1507. * so just ignore it!
  1508. */
  1509. if (envctrl_debug_flags) {
  1510. cmn_err(CE_WARN, "PS addr 0x%x recvd 0x%x on port %d\n",
  1511. psaddr, buf[i], i);
  1512. }
  1513. if (buf[i] > tempr && buf[i] < MAX_PS_ADVAL) {
  1514. tempr = buf[i];
  1515. }
  1516. }
  1517. /*
  1518. * This routine is a safeguard to make sure that if the
  1519. * powersupply temps cannot be read that we do something
  1520. * to make sure that the system will notify the user and
  1521. * it will stay running with the fans at 100%. The calling
  1522. * routine should take care of that.
  1523. */
  1524. if (status != DDI_SUCCESS) {
  1525. drv_usecwait(1000);
  1526. if (retrys < envctrl_max_retries) {
  1527. retrys++;
  1528. goto retry;
  1529. } else {
  1530. mutex_exit(&unitp->umutex);
  1531. envctrl_init_bus(unitp);
  1532. mutex_enter(&unitp->umutex);
  1533. if (envctrl_debug_flags)
  1534. cmn_err(CE_WARN,
  1535. "Cannot read Power Supply Temps addr = %X",
  1536. psaddr);
  1537. return (PS_DEFAULT_VAL);
  1538. }
  1539. }
  1540. return (ps_temps[tempr]);
  1541. }
  1542. static int
  1543. envctrl_get_cpu_temp(struct envctrlunit *unitp, int cpunum)
  1544. {
  1545. uint8_t recv_data;
  1546. int retrys;
  1547. int status;
  1548. ASSERT(MUTEX_HELD(&unitp->umutex));
  1549. /*
  1550. * This routine takes in the number of the port that
  1551. * we want to read in the 8591. This should be the
  1552. * location of the COU thermistor for one of the 4
  1553. * cpu's. It will return the temperature in degrees C
  1554. * to the caller.
  1555. */
  1556. retrys = 0;
  1557. retry:
  1558. status = eHc_read_pcf8591((struct eHc_envcunit *)unitp,
  1559. PCF8591_BASE_ADDR | PCF8591_DEV7, cpunum, 0, 0, 0,
  1560. &recv_data, 1);
  1561. /*
  1562. * We need to take a sledge hammer to the bus if we get back
  1563. * value of the chip. This means that the i2c bus got wedged.
  1564. * On the 1.4 systems this happens sometimes while running
  1565. * sunvts. We will return the max cpu temp minus 10 to make
  1566. * the fans run at full speed so that we don;t cook the
  1567. * system.
  1568. * At this point this is a workaround for hardware glitch.
  1569. */
  1570. if (status == DDI_FAILURE) {
  1571. drv_usecwait(1000);
  1572. if (retrys < envctrl_max_retries) {
  1573. retrys++;
  1574. goto retry;
  1575. } else {
  1576. mutex_exit(&unitp->umutex);
  1577. envctrl_init_bus(unitp);
  1578. mutex_enter(&unitp->umutex);
  1579. if (envctrl_debug_flags)
  1580. cmn_err(CE_WARN, "envctrl CPU TEMP read " \
  1581. "failed\n");
  1582. /* we don't want to power off the system */
  1583. return (MAX_CPU_TEMP - 10);
  1584. }
  1585. }
  1586. return (cpu_temps[recv_data]);
  1587. }
  1588. static int
  1589. envctrl_get_lm75_temp(struct envctrlunit *unitp)
  1590. {
  1591. int k;
  1592. ushort_t lmval;
  1593. uint8_t tmp1;
  1594. uint8_t tmp2;
  1595. int status;
  1596. uint8_t buf[2];
  1597. ASSERT(MUTEX_HELD(&unitp->umutex));
  1598. status = eHc_read_lm75((struct eHc_envcunit *)unitp,
  1599. LM75_BASE_ADDR | LM75_CONFIG_ADDRA, buf, 2);
  1600. if (status != DDI_SUCCESS)
  1601. cmn_err(CE_WARN, "read of LM75 failed\n");
  1602. tmp1 = buf[0];
  1603. tmp2 = buf[1];
  1604. /*
  1605. * Store the forst 8 bits in the upper nibble of the
  1606. * short, then store the lower 8 bits in the lower nibble
  1607. * of the short, shift 7 to the right to get the 9 bit value
  1608. * that the lm75 is really sending.
  1609. */
  1610. lmval = tmp1 << 8;
  1611. lmval = (lmval | tmp2);
  1612. lmval = (lmval >> 7);
  1613. /*
  1614. * Check the 9th bit to see if it is a negative
  1615. * temperature. If so change into 2's compliment
  1616. * and divide by 2 since each value is equal to a
  1617. * half degree strp in degrees C
  1618. */
  1619. if (lmval & LM75_COMP_MASK) {
  1620. tmp1 = (lmval & LM75_COMP_MASK_UPPER);
  1621. tmp1 = -tmp1;
  1622. tmp1 = tmp1/2;
  1623. k = 0 - tmp1;
  1624. } else {
  1625. k = lmval /2;
  1626. }
  1627. return (k);
  1628. }
  1629. static void
  1630. envctrl_tempr_poll(void *arg)
  1631. {
  1632. int diag_flag = 0;
  1633. struct envctrlunit *unitp = (struct envctrlunit *)arg;
  1634. mutex_enter(&unitp->umutex);
  1635. if (unitp->shutdown == B_TRUE) {
  1636. (void) power_down("Fatal System Environmental Control Error");
  1637. }
  1638. /*
  1639. * if we are in diag mode and the temp poll thread goes off,
  1640. * this means that the system is too heavily loaded and the 60 second
  1641. * window to execute the test is failing. We will change the fanspeed
  1642. * but will not check for a fanfault. This will cause a system shutdown
  1643. * if the system has had a fanfault injected.
  1644. */
  1645. if (unitp->current_mode == ENVCTRL_DIAG_MODE) {
  1646. diag_flag++;
  1647. if (envctrl_debug_flags) {
  1648. cmn_err(CE_WARN,
  1649. "Tempr poll went off while in DIAG MODE");
  1650. }
  1651. }
  1652. unitp->current_mode = ENVCTRL_NORMAL_MODE;
  1653. envctrl_get_sys_temperatures(unitp, (uint8_t *)NULL);
  1654. if (diag_flag == 0) {
  1655. envctrl_fan_fail_service(unitp);
  1656. }
  1657. /* now have this thread sleep for a while */
  1658. unitp->timeout_id = (timeout(envctrl_tempr_poll,
  1659. (caddr_t)unitp, overtemp_timeout_hz));
  1660. mutex_exit(&unitp->umutex);
  1661. }
  1662. static void
  1663. envctrl_led_blink(void *arg)
  1664. {
  1665. struct envctrl_pcf8574_chip fspchip;
  1666. struct envctrlunit *unitp = (struct envctrlunit *)arg;
  1667. mutex_enter(&unitp->umutex);
  1668. fspchip.type = PCF8574A;
  1669. fspchip.chip_num = ENVCTRL_PCF8574_DEV6; /* 0x01 port 1 */
  1670. envctrl_recv(unitp, (caddr_t *)(void *)&fspchip, PCF8574);
  1671. if (unitp->present_led_state == B_TRUE) {
  1672. /*
  1673. * Now we need to "or" in fault bits of the FSP
  1674. * module for the mass storage fault led.
  1675. * and set it.
  1676. */
  1677. fspchip.val = (fspchip.val & ~(ENVCTRL_PCF8574_PORT4) |
  1678. 0xC0);
  1679. unitp->present_led_state = B_FALSE;
  1680. } else {
  1681. fspchip.val = (fspchip.val | ENVCTRL_PCF8574_PORT4 | 0xC0);
  1682. unitp->present_led_state = B_TRUE;
  1683. }
  1684. (void) envctrl_xmit(unitp, (caddr_t *)(void *)&fspchip, PCF8574);
  1685. /* now have this thread sleep for a while */
  1686. unitp->blink_timeout_id = (timeout(envctrl_led_blink,
  1687. (caddr_t)unitp, blink_timeout_hz));
  1688. mutex_exit(&unitp->umutex);
  1689. }
  1690. /* called with mutex held */
  1691. static void
  1692. envctrl_get_sys_temperatures(struct envctrlunit *unitp, uint8_t *diag_tempr)
  1693. {
  1694. int temperature, tmptemp, cputemp, hicputemp, ambtemp;
  1695. int i;
  1696. struct envctrl_tda8444t_chip fan;
  1697. uint8_t psaddr[] = {PSTEMP3, PSTEMP2, PSTEMP1, PSTEMP0};
  1698. uint8_t noval = NULL;
  1699. uint8_t fspval;
  1700. ASSERT(MUTEX_HELD(&unitp->umutex));
  1701. fan.fan_num = ENVCTRL_CPU_FANS;
  1702. fan.chip_num = ENVCTRL_TDA8444T_DEV7;
  1703. tmptemp = 0; /* Right init value ?? */
  1704. /*
  1705. * THis routine is caled once every minute
  1706. * we wil re-se the watchdog timer each time
  1707. * we poll the temps. The watchdog timer is
  1708. * set up for 3 minutes. Should the kernel thread
  1709. * wedge, for some reason the watchdog will go off
  1710. * and blast the fans.
  1711. */
  1712. if (unitp->current_mode == ENVCTRL_DIAG_MODE) {
  1713. unitp->current_mode = ENVCTRL_NORMAL_MODE;
  1714. envctrl_reset_watchdog(unitp, &noval);
  1715. unitp->current_mode = ENVCTRL_DIAG_MODE;
  1716. } else {
  1717. envctrl_reset_watchdog(unitp, &noval);
  1718. }
  1719. /*
  1720. * we need to reset the dflop to allow the fans to be
  1721. * set if the watchdog goes of and the kernel resumes
  1722. * resetting the dflop alos resets the device interrupts
  1723. * we need to reenable them also.
  1724. */
  1725. envctrl_reset_dflop(unitp);
  1726. envctrl_enable_devintrs(unitp);
  1727. /*
  1728. * If we are in diag mode we allow the system to be
  1729. * faked out as to what the temperature is
  1730. * to see if the fans speed up.
  1731. */
  1732. if (unitp->current_mode == ENVCTRL_DIAG_MODE && diag_tempr != NULL) {
  1733. if (unitp->timeout_id != 0) {
  1734. (void) untimeout(unitp->timeout_id);
  1735. }
  1736. ambtemp = *diag_tempr;
  1737. unitp->timeout_id = (timeout(envctrl_tempr_poll,
  1738. (caddr_t)unitp, overtemp_timeout_hz));
  1739. } else {
  1740. ambtemp = envctrl_get_lm75_temp(unitp);
  1741. /*
  1742. * Sometimes when we read the temp it comes back bogus
  1743. * to fix this we just need to reset the envctrl bus
  1744. */
  1745. if (ambtemp == -100) {
  1746. mutex_exit(&unitp->umutex);
  1747. envctrl_init_bus(unitp);
  1748. mutex_enter(&unitp->umutex);
  1749. ambtemp = envctrl_get_lm75_temp(unitp);
  1750. }
  1751. }
  1752. envctrl_mod_encl_kstats(unitp, ENVCTRL_ENCL_AMBTEMPR, INSTANCE_0,
  1753. ambtemp);
  1754. fspval = envctrl_get_fpm_status(unitp);
  1755. if (ambtemp > MAX_AMB_TEMP) {
  1756. fspval |= (ENVCTRL_FSP_TEMP_ERR | ENVCTRL_FSP_GEN_ERR);
  1757. if (!(envctrl_power_off_overide) &&
  1758. unitp->current_mode == ENVCTRL_NORMAL_MODE) {
  1759. unitp->shutdown = B_TRUE;
  1760. }
  1761. if (unitp->current_mode == ENVCTRL_NORMAL_MODE) {
  1762. cmn_err(CE_WARN,
  1763. "Ambient Temperature is %d C, shutdown now\n",
  1764. ambtemp);
  1765. }
  1766. } else {
  1767. if (envctrl_isother_fault_led(unitp, fspval,
  1768. ENVCTRL_FSP_TEMP_ERR)) {
  1769. fspval &= ~(ENVCTRL_FSP_TEMP_ERR);
  1770. } else {
  1771. fspval &= ~(ENVCTRL_FSP_TEMP_ERR | ENVCTRL_FSP_GEN_ERR);
  1772. }
  1773. }
  1774. envctrl_set_fsp(unitp, &fspval);
  1775. cputemp = hicputemp = 0;
  1776. #ifndef TESTBED
  1777. for (i = 0; i < ENVCTRL_MAX_CPUS; i++) {
  1778. if (unitp->cpu_pr_location[i] == B_TRUE) {
  1779. cputemp = envctrl_get_cpu_temp(unitp, i);
  1780. envctrl_mod_encl_kstats(unitp, ENVCTRL_ENCL_CPUTEMPR,
  1781. i, cputemp);
  1782. if (cputemp >= MAX_CPU_TEMP) {
  1783. if (!(envctrl_power_off_overide)) {
  1784. unitp->shutdown = B_TRUE;
  1785. }
  1786. cmn_err(CE_WARN,
  1787. "CPU %d OVERHEATING!!!", i);
  1788. }
  1789. if (cputemp > hicputemp) {
  1790. hicputemp = cputemp;
  1791. }
  1792. }
  1793. }
  1794. #else
  1795. cputemp = envctrl_get_cpu_temp(unitp, 0);
  1796. envctrl_mod_encl_kstats(unitp, ENVCTRL_ENCL_CPUTEMPR, 0, cputemp);
  1797. #endif
  1798. fspval = envctrl_get_fpm_status(unitp);
  1799. /*
  1800. * We first look at the ambient temp. If the system is at idle
  1801. * the cpu temps will be approx 20 degrees above ambient.
  1802. * If the cpu's rise above 20, then the CPU fans are set
  1803. * according to the cpu temp minus 20 degrees C.
  1804. */
  1805. if (unitp->current_mode == ENVCTRL_DIAG_MODE && diag_tempr != NULL) {
  1806. temperature = ambtemp;
  1807. } else {
  1808. temperature = hicputemp - CPU_AMB_RISE;
  1809. }
  1810. if (temperature < 0) {
  1811. fan.val = MAX_FAN_SPEED; /* blast it is out of range */
  1812. } else if (temperature > MAX_AMB_TEMP) {
  1813. fan.val = MAX_FAN_SPEED;
  1814. fspval |= (ENVCTRL_FSP_TEMP_ERR | ENVCTRL_FSP_GEN_ERR);
  1815. if (unitp->current_mode == ENVCTRL_NORMAL_MODE) {
  1816. cmn_err(CE_WARN,
  1817. "CPU Fans set to MAX. CPU Temp is %d C\n",
  1818. hicputemp);
  1819. }
  1820. } else if (ambtemp < MAX_AMB_TEMP) {
  1821. if (!envctrl_p0_enclosure) {
  1822. fan.val = acme_cpu_fanspd[temperature];
  1823. } else {
  1824. fan.val = fan_speed[temperature];
  1825. }
  1826. if (envctrl_isother_fault_led(unitp, fspval,
  1827. ENVCTRL_FSP_TEMP_ERR)) {
  1828. fspval &= ~(ENVCTRL_FSP_TEMP_ERR);
  1829. } else {
  1830. fspval &= ~(ENVCTRL_FSP_TEMP_ERR | ENVCTRL_FSP_GEN_ERR);
  1831. }
  1832. }
  1833. envctrl_set_fsp(unitp, &fspval);
  1834. /*
  1835. * Update temperature kstats. FSP kstats are updated in the
  1836. * set and get routine.
  1837. */
  1838. unitp->fan_kstats[ENVCTRL_FAN_TYPE_CPU].fanspeed = fan.val;
  1839. /* CPU FANS */
  1840. (void) envctrl_xmit(unitp, (caddr_t *)(void *)&fan, TDA8444T);
  1841. /* The afb Fan is always at max */
  1842. if (unitp->AFB_present == B_TRUE) {
  1843. fan.val = AFB_MAX;
  1844. /* AFB FANS */
  1845. unitp->fan_kstats[ENVCTRL_FAN_TYPE_AFB].fanspeed = fan.val;
  1846. fan.fan_num = ENVCTRL_AFB_FANS;
  1847. (void) envctrl_xmit(unitp, (caddr_t *)(void *)&fan, TDA8444T);
  1848. }
  1849. /*
  1850. * Now set the Powersupply fans
  1851. */
  1852. tmptemp = temperature = 0;
  1853. for (i = 0; i <= MAXPS; i++) {
  1854. if (unitp->ps_present[i]) {
  1855. tmptemp = envctrl_get_ps_temp(unitp, psaddr[i]);
  1856. unitp->ps_kstats[i].ps_tempr = tmptemp & 0xFFFF;
  1857. if (tmptemp > temperature) {
  1858. temperature = tmptemp;
  1859. }
  1860. if (temperature >= MAX_PS_TEMP) {
  1861. if (!(envctrl_power_off_overide)) {
  1862. unitp->shutdown = B_TRUE;
  1863. }
  1864. cmn_err(CE_WARN,
  1865. "Power Supply %d OVERHEATING!!!\
  1866. Temp is %d C", i, temperature);
  1867. }
  1868. }
  1869. }
  1870. fan.fan_num = ENVCTRL_PS_FANS;
  1871. if (temperature > PS_TEMP_WARN) {
  1872. fspval = envctrl_get_fpm_status(unitp);
  1873. fspval |= (ENVCTRL_FSP_TEMP_ERR | ENVCTRL_FSP_GEN_ERR);
  1874. envctrl_set_fsp(unitp, &fspval);
  1875. fan.val = MAX_FAN_SPEED;
  1876. cmn_err(CE_WARN, "A Power Supply is close to OVERHEATING!!!");
  1877. } else {
  1878. if (temperature - ambtemp > PS_AMB_RISE) {
  1879. ambtemp = temperature - PS_AMB_RISE;
  1880. }
  1881. if (!envctrl_p0_enclosure) {
  1882. fan.val = acme_ps_fanspd[ambtemp];
  1883. } else {
  1884. fan.val = ps_fans[ambtemp];
  1885. }
  1886. }
  1887. /*
  1888. * XXX add in error condition for ps overtemp
  1889. */
  1890. unitp->fan_kstats[ENVCTRL_FAN_TYPE_PS].fanspeed = fan.val;
  1891. (void) envctrl_xmit(unitp, (caddr_t *)(void *)&fan, TDA8444T);
  1892. }
  1893. /* called with mutex held */
  1894. static void
  1895. envctrl_fan_fail_service(struct envctrlunit *unitp)
  1896. {
  1897. uint8_t recv_data, fpmstat;
  1898. int fantype;
  1899. int psfanflt, cpufanflt, afbfanflt;
  1900. int retries = 0, max_retry_count;
  1901. int status;
  1902. psfanflt = cpufanflt = afbfanflt = 0;
  1903. /*
  1904. * The fan fail sensor is located at address 0x70
  1905. * on the envctrl bus.
  1906. */
  1907. ASSERT(MUTEX_HELD(&unitp->umutex));
  1908. retry:
  1909. status = eHc_read_pcf8574a((struct eHc_envcunit *)unitp,
  1910. PCF8574A_BASE_ADDR | ENVCTRL_PCF8574_DEV4, &recv_data, 1);
  1911. if (status != DDI_SUCCESS)
  1912. cmn_err(CE_WARN, "fan_fail_service: status = %d, data = %x\n",
  1913. status, recv_data);
  1914. /*
  1915. * If all fan ports are high (0xff) then we don't have any
  1916. * fan faults. Reset the kstats
  1917. */
  1918. if (recv_data == 0xff) {
  1919. unitp->fan_kstats[ENVCTRL_FAN_TYPE_PS].fans_ok = B_TRUE;
  1920. unitp->fan_kstats[ENVCTRL_FAN_TYPE_CPU].fans_ok = B_TRUE;
  1921. unitp->fan_kstats[ENVCTRL_FAN_TYPE_AFB].fans_ok = B_TRUE;
  1922. unitp->fan_kstats[ENVCTRL_FAN_TYPE_PS].fanflt_num = 0;
  1923. unitp->fan_kstats[ENVCTRL_FAN_TYPE_CPU].fanflt_num = 0;
  1924. unitp->fan_kstats[ENVCTRL_FAN_TYPE_AFB].fanflt_num = 0;
  1925. unitp->num_fans_failed = 0;
  1926. fpmstat = envctrl_get_fpm_status(unitp);
  1927. if (!(envctrl_isother_fault_led(unitp, fpmstat, 0))) {
  1928. fpmstat &= ~(ENVCTRL_FSP_GEN_ERR);
  1929. }
  1930. if (unitp->shutdown != B_TRUE) {
  1931. envctrl_set_fsp(unitp, &fpmstat);
  1932. }
  1933. return;
  1934. }
  1935. fantype = ENVCTRL_FAN_TYPE_PS;
  1936. if (!(recv_data & ENVCTRL_PCF8574_PORT0)) {
  1937. psfanflt = PS_FAN_3;
  1938. }
  1939. if (!(recv_data & ENVCTRL_PCF8574_PORT1)) {
  1940. psfanflt = PS_FAN_2;
  1941. }
  1942. if (!(recv_data & ENVCTRL_PCF8574_PORT2)) {
  1943. psfanflt = PS_FAN_1;
  1944. }
  1945. if (psfanflt != 0) {
  1946. unitp->fan_kstats[fantype].fans_ok = B_FALSE;
  1947. unitp->fan_kstats[fantype].fanflt_num = psfanflt - 1;
  1948. if (retries == MAX_FAN_FAIL_RETRY && status == DDI_SUCCESS &&
  1949. unitp->current_mode == ENVCTRL_NORMAL_MODE) {
  1950. cmn_err(CE_WARN, "PS Fan Number %d Failed",
  1951. psfanflt - 1);
  1952. }
  1953. } else {
  1954. unitp->fan_kstats[fantype].fans_ok = B_TRUE;
  1955. unitp->fan_kstats[fantype].fanflt_num = 0;
  1956. }
  1957. fantype = ENVCTRL_FAN_TYPE_CPU;
  1958. if (!(recv_data & ENVCTRL_PCF8574_PORT3)) {
  1959. cpufanflt = CPU_FAN_1;
  1960. }
  1961. if (!(recv_data & ENVCTRL_PCF8574_PORT4)) {
  1962. cpufanflt = CPU_FAN_2;
  1963. }
  1964. if (!(recv_data & ENVCTRL_PCF8574_PORT5)) {
  1965. cpufanflt = CPU_FAN_3;
  1966. }
  1967. if (cpufanflt != 0) {
  1968. unitp->fan_kstats[fantype].fans_ok = B_FALSE;
  1969. unitp->fan_kstats[fantype].fanflt_num = cpufanflt - 1;
  1970. if (retries == MAX_FAN_FAIL_RETRY && status == DDI_SUCCESS &&
  1971. unitp->current_mode == ENVCTRL_NORMAL_MODE) {
  1972. cmn_err(CE_WARN, "CPU Fan Number %d Failed",
  1973. cpufanflt - 1);
  1974. }
  1975. } else {
  1976. unitp->fan_kstats[fantype].fans_ok = B_TRUE;
  1977. unitp->fan_kstats[fantype].fanflt_num = 0;
  1978. }
  1979. if (!(recv_data & ENVCTRL_PCF8574_PORT6) &&
  1980. (unitp->AFB_present == B_TRUE)) {
  1981. /*
  1982. * If the afb is present and the afb fan fails,
  1983. * we need to power off or else it will melt!
  1984. * If it isn't present just log the error.
  1985. * We make the decision off of the afbfanflt
  1986. * flag later on in an if statement.
  1987. */
  1988. afbfanflt++;
  1989. unitp->fan_kstats[ENVCTRL_FAN_TYPE_AFB].fans_ok
  1990. = B_FALSE;
  1991. unitp->fan_kstats[ENVCTRL_FAN_TYPE_AFB].fanflt_num =
  1992. AFB_FAN_1;
  1993. if (unitp->current_mode == ENVCTRL_NORMAL_MODE) {
  1994. cmn_err(CE_WARN, "AFB Fan Failed");
  1995. }
  1996. }
  1997. /*
  1998. * If we have no Fan Faults Clear the LED's
  1999. * If we have fan faults set the Gen Fault LED.
  2000. */
  2001. if (psfanflt == 0 && cpufanflt == 0 && afbfanflt == 0 &&
  2002. unitp->num_fans_failed != 0) {
  2003. fpmstat = envctrl_get_fpm_status(unitp);
  2004. if (!(envctrl_isother_fault_led(unitp,
  2005. fpmstat, 0))) {
  2006. fpmstat &= ~(ENVCTRL_FSP_GEN_ERR);
  2007. }
  2008. envctrl_set_fsp(unitp, &fpmstat);
  2009. } else if (psfanflt != 0 || cpufanflt != 0 || afbfanflt != 0) {
  2010. fpmstat = envctrl_get_fpm_status(unitp);
  2011. fpmstat |= ENVCTRL_FSP_GEN_ERR;
  2012. envctrl_set_fsp(unitp, &fpmstat);
  2013. }
  2014. if (unitp->AFB_present == B_FALSE) {
  2015. afbfanflt = 0;
  2016. }
  2017. if ((cpufanflt > 0 || psfanflt > 0 || afbfanflt > 0 ||
  2018. (status != DDI_SUCCESS)) && !unitp->initting &&
  2019. unitp->current_mode == ENVCTRL_NORMAL_MODE) {
  2020. if (status != DDI_SUCCESS)
  2021. max_retry_count = envctrl_max_retries;
  2022. else
  2023. max_retry_count = MAX_FAN_FAIL_RETRY;
  2024. if (retries <= max_retry_count) {
  2025. retries++;
  2026. drv_usecwait(1000);
  2027. if (retries == max_retry_count) {
  2028. cmn_err(CE_WARN,
  2029. "Fan Fail is 0x%x, retries = %d\n",
  2030. recv_data, retries);
  2031. }
  2032. envctrl_get_sys_temperatures(unitp,
  2033. (uint8_t *)NULL);
  2034. goto retry;
  2035. }
  2036. if (!(envctrl_power_off_overide)) {
  2037. unitp->shutdown = B_TRUE;
  2038. }
  2039. cmn_err(CE_WARN, "Fan Failure(s), System Shutdown");
  2040. }
  2041. unitp->num_fans_failed = (psfanflt + cpufanflt + afbfanflt);
  2042. }
  2043. /*
  2044. * Check for power supply insertion and failure.
  2045. * This is a bit tricky, because a power supply insertion will
  2046. * trigger a load share interrupt as well as PS present in the
  2047. * new supply. if we detect an insertion clear
  2048. * interrupts, disable interrupts, wait for a couple of seconds
  2049. * come back and see if the PSOK bit is set, PS_PRESENT is set
  2050. * and the share fail interrupts are gone. If not this is a
  2051. * real load share fail event.
  2052. * Called with mutex held
  2053. */
  2054. static void
  2055. envctrl_PS_intr_service(struct envctrlunit *unitp, uint8_t psaddr)
  2056. {
  2057. uint8_t recv_data;
  2058. int status, retrys = 0;
  2059. ASSERT(MUTEX_HELD(&unitp->umutex));
  2060. if (unitp->current_mode == ENVCTRL_DIAG_MODE) {
  2061. return;
  2062. }
  2063. retry:
  2064. status = eHc_read_pcf8574a((struct eHc_envcunit *)unitp,
  2065. PCF8574A_BASE_ADDR | psaddr & 0xF, &recv_data, 1);
  2066. if (status != DDI_SUCCESS) {
  2067. drv_usecwait(1000);
  2068. if (retrys < envctrl_max_retries) {
  2069. retrys++;
  2070. goto retry;
  2071. } else {
  2072. mutex_exit(&unitp->umutex);
  2073. envctrl_init_bus(unitp);
  2074. mutex_enter(&unitp->umutex);
  2075. if (envctrl_debug_flags)
  2076. cmn_err(CE_WARN,
  2077. "PS_intr_service: Read from 8574A " \
  2078. "failed\n");
  2079. }
  2080. }
  2081. /*
  2082. * setup a timeout thread to poll the ps after a
  2083. * couple of seconds. This allows for the PS to settle
  2084. * and doesn't report false errors on a hotplug
  2085. */
  2086. unitp->pshotplug_id = (timeout(envctrl_pshotplug_poll,
  2087. (caddr_t)unitp, pshotplug_timeout_hz));
  2088. }
  2089. /* called with mutex held */
  2090. static void
  2091. envctrl_reset_dflop(struct envctrlunit *unitp)
  2092. {
  2093. struct envctrl_pcf8574_chip initval;
  2094. ASSERT(MUTEX_HELD(&unitp->umutex));
  2095. /*
  2096. * This initialization sequence allows a
  2097. * to change state to stop the fans from
  2098. * blastion upon poweron. If this isn't
  2099. * done the writes to the 8444 will not complete
  2100. * to the hardware because the dflop will
  2101. * be closed
  2102. */
  2103. initval.chip_num = ENVCTRL_PCF8574_DEV0; /* 0x01 port 1 */
  2104. initval.type = PCF8574A;
  2105. initval.val = ENVCTRL_DFLOP_INIT0;
  2106. (void) envctrl_xmit(unitp, (caddr_t *)(void *)&initval, PCF8574);
  2107. initval.val = ENVCTRL_DFLOP_INIT1;
  2108. (void) envctrl_xmit(unitp, (caddr_t *)(void *)&initval, PCF8574);
  2109. }
  2110. static void
  2111. envctrl_add_encl_kstats(struct envctrlunit *unitp, int type,
  2112. int instance, uint8_t val)
  2113. {
  2114. int i = 0;
  2115. boolean_t inserted = B_FALSE;
  2116. ASSERT(MUTEX_HELD(&unitp->umutex));
  2117. while (i < MAX_DEVS && inserted == B_FALSE) {
  2118. if (unitp->encl_kstats[i].instance == I2C_NODEV) {
  2119. unitp->encl_kstats[i].instance = instance;
  2120. unitp->encl_kstats[i].type = type;
  2121. unitp->encl_kstats[i].value = val;
  2122. inserted = B_TRUE;
  2123. }
  2124. i++;
  2125. }
  2126. unitp->num_encl_present++;
  2127. }
  2128. /* called with mutex held */
  2129. static void
  2130. envctrl_enable_devintrs(struct envctrlunit *unitp)
  2131. {
  2132. struct envctrl_pcf8574_chip initval;
  2133. ASSERT(MUTEX_HELD(&unitp->umutex));
  2134. /*
  2135. * This initialization sequence allows a
  2136. * to change state to stop the fans from
  2137. * blastion upon poweron. If this isn't
  2138. * done the writes to the 8444 will not complete
  2139. * to the hardware because the dflop will
  2140. * be closed
  2141. */
  2142. initval.chip_num = ENVCTRL_PCF8574_DEV0; /* 0x01 port 1 */
  2143. initval.type = PCF8574A;
  2144. initval.val = ENVCTRL_DEVINTR_INTI0;
  2145. (void) envctrl_xmit(unitp, (caddr_t *)(void *)&initval, PCF8574);
  2146. /*
  2147. * set lowerbits all high p0 = PS1, p1 = PS2
  2148. * p2 = PS3 p4 = envctrl intr_ctrl
  2149. */
  2150. initval.val = ENVCTRL_DEVINTR_INTI1;
  2151. (void) envctrl_xmit(unitp, (caddr_t *)(void *)&initval, PCF8574);
  2152. }
  2153. /* called with mutex held */
  2154. static void
  2155. envctrl_stop_clock(struct envctrlunit *unitp)
  2156. {
  2157. int status;
  2158. uint8_t buf[2];
  2159. /*
  2160. * This routine talks to the PCF8583 which
  2161. * is a clock calendar chip on the envctrl bus.
  2162. * We use this chip as a watchdog timer for the
  2163. * fan control. At reset this chip pulses the interrupt
  2164. * line every 1 second. We need to be able to shut
  2165. * this off.
  2166. */
  2167. ASSERT(MUTEX_HELD(&unitp->umutex));
  2168. buf[0] = CLOCK_CSR_REG;
  2169. buf[1] = CLOCK_DISABLE;
  2170. status = eHc_write_pcf8583((struct eHc_envcunit *)unitp,
  2171. PCF8583_BASE_ADDR | 0, buf, 2);
  2172. if (status != DDI_SUCCESS)
  2173. cmn_err(CE_WARN, "write to PCF8583 failed\n");
  2174. }
  2175. static void
  2176. envctrl_reset_watchdog(struct envctrlunit *unitp, uint8_t *wdval)
  2177. {
  2178. uint8_t w, r;
  2179. uint8_t res = 0;
  2180. int status;
  2181. uint8_t buf[3];
  2182. ASSERT(MUTEX_HELD(&unitp->umutex));
  2183. /* the clock MUST be stopped before we re-set it */
  2184. envctrl_stop_clock(unitp);
  2185. /*
  2186. * Reset the minutes counter to 0.
  2187. */
  2188. buf[0] = ALARM_CTR_REG_MINS;
  2189. buf[1] = 0x0;
  2190. status = eHc_write_pcf8583((struct eHc_envcunit *)unitp,
  2191. PCF8583_BASE_ADDR | 0, buf, 2);
  2192. if (status != DDI_SUCCESS)
  2193. cmn_err(CE_WARN, "write to PCF8583 failed\n");
  2194. /*
  2195. * set up the alarm timer for 3 minutes
  2196. * start by setting reg 8 ALARM_CTRL_REG
  2197. * If we are in diag mode, we set the timer in
  2198. * seconds. Valid values are 40-99. The timer
  2199. * counts up to 99. 40 would be 59 seconds
  2200. */
  2201. buf[0] = CLOCK_ALARM_REG_A;
  2202. if (unitp->current_mode == ENVCTRL_DIAG_MODE) {
  2203. if (unitp->timeout_id != 0) {
  2204. (void) untimeout(unitp->timeout_id);
  2205. unitp->timeout_id = 0;
  2206. unitp->timeout_id = (timeout(envctrl_tempr_poll,
  2207. (caddr_t)unitp, overtemp_timeout_hz));
  2208. }
  2209. buf[1] = CLOCK_ENABLE_TIMER_S;
  2210. } else {
  2211. buf[1] = CLOCK_ENABLE_TIMER;
  2212. }
  2213. /* STEP 10: End Transmission */
  2214. status = eHc_write_pcf8583((struct eHc_envcunit *)unitp,
  2215. PCF8583_BASE_ADDR | 0, buf, 2);
  2216. if (status != DDI_SUCCESS)
  2217. cmn_err(CE_WARN, "Reset envctrl watchdog failed\n");
  2218. /*
  2219. * Now set up the alarm timer register it
  2220. * counts from 0-99 with an intr triggered
  2221. * when it gets to overflow.. or 99. It will
  2222. * also count from a pre-set value which is
  2223. * where we are seting from. We want a 3 minute fail
  2224. * safe so our value is 99-3 or 96.
  2225. * we are programming register 7 in the 8583.
  2226. */
  2227. buf[0] = ALARM_CTRL_REG;
  2228. /*
  2229. * Allow the diagnostic to set the egg timer val.
  2230. * never allow it to be set greater than the default.
  2231. */
  2232. if (unitp->current_mode == ENVCTRL_DIAG_MODE) {
  2233. if (*wdval > MAX_CL_VAL) {
  2234. buf[1] = EGG_TIMER_VAL;
  2235. } else {
  2236. w = *wdval/10;
  2237. r = *wdval%10;
  2238. res = res | r;
  2239. res = (0x99 - (res | (w << 4)));
  2240. buf[1] = res;
  2241. }
  2242. } else {
  2243. buf[1] = EGG_TIMER_VAL;
  2244. }
  2245. status = eHc_write_pcf8583((struct eHc_envcunit *)unitp,
  2246. PCF8583_BASE_ADDR | 0, buf, 2);
  2247. if (status != DDI_SUCCESS)
  2248. cmn_err(CE_WARN, "Reset envctrl watchdog failed\n");
  2249. /*
  2250. * Now that we have set up.. it is time
  2251. * to re-start the clock in the CSR.
  2252. */
  2253. buf[0] = CLOCK_CSR_REG;
  2254. buf[1] = CLOCK_ENABLE;
  2255. status = eHc_write_pcf8583((struct eHc_envcunit *)unitp,
  2256. PCF8583_BASE_ADDR | 0, buf, 2);
  2257. if (status != DDI_SUCCESS)
  2258. cmn_err(CE_WARN, "Reset envctrl watchdog failed\n");
  2259. }
  2260. /* Called with unip mutex held */
  2261. static void
  2262. envctrl_ps_probe(struct envctrlunit *unitp)
  2263. {
  2264. uint8_t recv_data, fpmstat;
  2265. uint8_t psaddr[] = {PS1, PS2, PS3, PSTEMP0};
  2266. int i;
  2267. int ps_error = 0, retrys = 0;
  2268. int devaddr;
  2269. int status;
  2270. int twotimes = 0;
  2271. ASSERT(MUTEX_HELD(&unitp->umutex));
  2272. unitp->num_ps_present = 0;
  2273. for (i = 0; i <= MAXPS; i++) {
  2274. unitp->ps_present[i] = B_FALSE;
  2275. unitp->ps_kstats[i].ps_rating = 0;
  2276. unitp->ps_kstats[i].ps_tempr = 0;
  2277. switch (psaddr[i]) {
  2278. case PS1:
  2279. devaddr = ENVCTRL_PCF8574_DEV3;
  2280. break;
  2281. case PS2:
  2282. devaddr = ENVCTRL_PCF8574_DEV2;
  2283. break;
  2284. case PS3:
  2285. devaddr = ENVCTRL_PCF8574_DEV1;
  2286. break;
  2287. case PSTEMP0:
  2288. devaddr = 0;
  2289. break;
  2290. }
  2291. retrys = 0;
  2292. retry:
  2293. status = eHc_read_pcf8574a((struct eHc_envcunit *)unitp,
  2294. PCF8574A_BASE_ADDR | devaddr, &recv_data, 1);
  2295. if (status != DDI_SUCCESS) {
  2296. drv_usecwait(1000);
  2297. if (retrys < envctrl_max_retries) {
  2298. retrys++;
  2299. goto retry;
  2300. } else {
  2301. mutex_exit(&unitp->umutex);
  2302. envctrl_init_bus(unitp);
  2303. mutex_enter(&unitp->umutex);
  2304. /*
  2305. * If we just reset the bus we need to reread
  2306. * the status. If a second attempt still fails
  2307. * then report the read failure.
  2308. */
  2309. if (twotimes == 0) {
  2310. twotimes++;
  2311. retrys = 0;
  2312. goto retry;
  2313. } else {
  2314. cmn_err(CE_WARN,
  2315. "PS_probe: Read from 8574A failed\n");
  2316. }
  2317. }
  2318. }
  2319. /*
  2320. * Port 0 = PS Present
  2321. * Port 1 = PS Type
  2322. * Port 2 = PS Type
  2323. * Port 3 = PS TYpe
  2324. * Port 4 = DC Status
  2325. * Port 5 = Current Limit
  2326. * Port 6 = Current Share
  2327. * Port 7 = SPARE
  2328. */
  2329. /*
  2330. * Port 0 = PS Present
  2331. * Port is pulled LOW "0" to indicate
  2332. * present.
  2333. */
  2334. if (!(recv_data & ENVCTRL_PCF8574_PORT0)) {
  2335. unitp->ps_present[i] = B_TRUE;
  2336. /* update unit kstat array */
  2337. unitp->ps_kstats[i].instance = i;
  2338. unitp->ps_kstats[i].ps_tempr = ENVCTRL_INIT_TEMPR;
  2339. ++unitp->num_ps_present;
  2340. if (power_supply_previous_state[i] == 0) {
  2341. cmn_err(CE_NOTE,
  2342. "Power Supply %d inserted\n", i);
  2343. }
  2344. power_supply_previous_state[i] = 1;
  2345. if (!(recv_data & ENVCTRL_PCF8574_PORT1)) {
  2346. unitp->ps_kstats[i].ps_rating = ENVCTRL_PS_550;
  2347. }
  2348. if (!(recv_data & ENVCTRL_PCF8574_PORT2)) {
  2349. unitp->ps_kstats[i].ps_rating = ENVCTRL_PS_650;
  2350. }
  2351. if (!(recv_data & ENVCTRL_PCF8574_PORT3)) {
  2352. cmn_err(CE_WARN,
  2353. "Power Supply %d NOT okay\n", i);
  2354. unitp->ps_kstats[i].ps_ok = B_FALSE;
  2355. ps_error++;
  2356. } else {
  2357. unitp->ps_kstats[i].ps_ok = B_TRUE;
  2358. }
  2359. if (!(recv_data & ENVCTRL_PCF8574_PORT4)) {
  2360. cmn_err(CE_WARN,
  2361. "Power Supply %d Overloaded\n", i);
  2362. unitp->ps_kstats[i].limit_ok = B_FALSE;
  2363. ps_error++;
  2364. } else {
  2365. unitp->ps_kstats[i].limit_ok = B_TRUE;
  2366. }
  2367. if (!(recv_data & ENVCTRL_PCF8574_PORT5)) {
  2368. cmn_err(CE_WARN,
  2369. "Power Supply %d load share err\n", i);
  2370. unitp->ps_kstats[i].curr_share_ok = B_FALSE;
  2371. ps_error++;
  2372. } else {
  2373. unitp->ps_kstats[i].curr_share_ok = B_TRUE;
  2374. }
  2375. if (!(recv_data & ENVCTRL_PCF8574_PORT6)) {
  2376. cmn_err(CE_WARN,
  2377. "PS %d Shouln't interrupt\n", i);
  2378. ps_error++;
  2379. }
  2380. if (!(recv_data & ENVCTRL_PCF8574_PORT7)) {
  2381. cmn_err(CE_WARN,
  2382. "PS %d Shouln't interrupt\n", i);
  2383. ps_error++;
  2384. }
  2385. } else {
  2386. /* No power supply present */
  2387. if (power_supply_previous_state[i] == 1) {
  2388. cmn_err(CE_NOTE,
  2389. "Power Supply %d removed\n", i);
  2390. }
  2391. power_supply_previous_state[i] = 0;
  2392. }
  2393. }
  2394. fpmstat = envctrl_get_fpm_status(unitp);
  2395. if (ps_error) {
  2396. fpmstat |= (ENVCTRL_FSP_PS_ERR | ENVCTRL_FSP_GEN_ERR);
  2397. } else {
  2398. if (envctrl_isother_fault_led(unitp, fpmstat,
  2399. ENVCTRL_FSP_PS_ERR)) {
  2400. fpmstat &= ~(ENVCTRL_FSP_PS_ERR);
  2401. } else {
  2402. fpmstat &= ~(ENVCTRL_FSP_PS_ERR |
  2403. ENVCTRL_FSP_GEN_ERR);
  2404. }
  2405. }
  2406. envctrl_set_fsp(unitp, &fpmstat);
  2407. /*
  2408. * We need to reset all of the fans etc when a supply is
  2409. * interrupted and added, but we don't want to reset the
  2410. * fans if we are in DIAG mode. This will mess up SUNVTS.
  2411. */
  2412. if (unitp->current_mode == ENVCTRL_NORMAL_MODE) {
  2413. envctrl_get_sys_temperatures(unitp, (uint8_t *)NULL);
  2414. }
  2415. }
  2416. /*
  2417. * consider key switch position when handling an abort sequence
  2418. */
  2419. static void
  2420. envctrl_abort_seq_handler(char *msg)
  2421. {
  2422. struct envctrlunit *unitp;
  2423. int i;
  2424. uint8_t secure = 0;
  2425. /*
  2426. * Find the instance of the device available on this host.
  2427. * Note that there may be only one, but the instance may
  2428. * not be zero.
  2429. */
  2430. for (i = 0; i < MAX_DEVS; i++) {
  2431. if (unitp = (struct envctrlunit *)
  2432. ddi_get_soft_state(envctrlsoft_statep, i))
  2433. break;
  2434. }
  2435. ASSERT(unitp);
  2436. for (i = 0; i < MAX_DEVS; i++) {
  2437. if ((unitp->encl_kstats[i].type == ENVCTRL_ENCL_FSP) &&
  2438. (unitp->encl_kstats[i].instance != I2C_NODEV)) {
  2439. secure = unitp->encl_kstats[i].value;
  2440. break;
  2441. }
  2442. }
  2443. /*
  2444. * take the logical not because we are in hardware mode only
  2445. */
  2446. if ((secure & ENVCTRL_FSP_KEYMASK) == ENVCTRL_FSP_KEYLOCKED) {
  2447. cmn_err(CE_CONT,
  2448. "!envctrl: ignoring debug enter sequence\n");
  2449. } else {
  2450. if (envctrl_debug_flags) {
  2451. cmn_err(CE_CONT, "!envctrl: allowing debug enter\n");
  2452. }
  2453. debug_enter(msg);
  2454. }
  2455. }
  2456. /*
  2457. * get the front Panel module LED and keyswitch status.
  2458. * this part is addressed at 0x7C on the i2c bus.
  2459. * called with mutex held
  2460. */
  2461. static uint8_t
  2462. envctrl_get_fpm_status(struct envctrlunit *unitp)
  2463. {
  2464. uint8_t recv_data;
  2465. int status, retrys = 0;
  2466. ASSERT(MUTEX_HELD(&unitp->umutex));
  2467. retry:
  2468. status = eHc_read_pcf8574a((struct eHc_envcunit *)unitp,
  2469. PCF8574A_BASE_ADDR | ENVCTRL_PCF8574_DEV6, &recv_data, 1);
  2470. /*
  2471. * yet another place where a read can cause the
  2472. * the SDA line of the i2c bus to get stuck low.
  2473. * this funky sequence frees the SDA line.
  2474. */
  2475. if (status != DDI_SUCCESS) {
  2476. drv_usecwait(1000);
  2477. if (retrys < envctrl_max_retries) {
  2478. retrys++;
  2479. goto retry;
  2480. } else {
  2481. mutex_exit(&unitp->umutex);
  2482. envctrl_init_bus(unitp);
  2483. mutex_enter(&unitp->umutex);
  2484. if (envctrl_debug_flags)
  2485. cmn_err(CE_WARN, "Read from PCF8574 (FPM) "\
  2486. "failed\n");
  2487. }
  2488. }
  2489. recv_data = ~recv_data;
  2490. envctrl_mod_encl_kstats(unitp, ENVCTRL_ENCL_FSP,
  2491. INSTANCE_0, recv_data);
  2492. return (recv_data);
  2493. }
  2494. static void
  2495. envctrl_set_fsp(struct envctrlunit *unitp, uint8_t *val)
  2496. {
  2497. struct envctrl_pcf8574_chip chip;
  2498. ASSERT(MUTEX_HELD(&unitp->umutex));
  2499. chip.val = ENVCTRL_FSP_OFF; /* init all values to off */
  2500. chip.chip_num = ENVCTRL_PCF8574_DEV6; /* 0x01 port 1 */
  2501. chip.type = PCF8574A;
  2502. /*
  2503. * strip off bits that are R/O
  2504. */
  2505. chip.val = (~(ENVCTRL_FSP_KEYMASK | ENVCTRL_FSP_POMASK) & (*val));
  2506. chip.val = ~chip.val;
  2507. (void) envctrl_xmit(unitp, (caddr_t *)(void *)&chip, PCF8574);
  2508. }
  2509. static int
  2510. envctrl_get_dskled(struct envctrlunit *unitp, struct envctrl_pcf8574_chip *chip)
  2511. {
  2512. uint_t oldtype;
  2513. ASSERT(MUTEX_HELD(&unitp->umutex));
  2514. if (chip->chip_num > ENVCTRL_PCF8574_DEV2 ||
  2515. chip->type != ENVCTRL_ENCL_BACKPLANE4 &&
  2516. chip->type != ENVCTRL_ENCL_BACKPLANE8) {
  2517. return (DDI_FAILURE);
  2518. }
  2519. oldtype = chip->type;
  2520. chip->type = PCF8574;
  2521. envctrl_recv(unitp, (caddr_t *)(void *)chip, PCF8574);
  2522. chip->type = oldtype;
  2523. chip->val = ~chip->val;
  2524. return (DDI_SUCCESS);
  2525. }
  2526. static int
  2527. envctrl_set_dskled(struct envctrlunit *unitp, struct envctrl_pcf8574_chip *chip)
  2528. {
  2529. struct envctrl_pcf8574_chip fspchip;
  2530. struct envctrl_pcf8574_chip backchip;
  2531. int i, instance;
  2532. int diskfault = 0;
  2533. uint8_t controller_addr[] = {ENVCTRL_PCF8574_DEV0, ENVCTRL_PCF8574_DEV1,
  2534. ENVCTRL_PCF8574_DEV2};
  2535. /*
  2536. * We need to check the type of disk led being set. If it
  2537. * is a 4 slot backplane then the upper 4 bits (7, 6, 5, 4) are
  2538. * invalid.
  2539. */
  2540. ASSERT(MUTEX_HELD(&unitp->umutex));
  2541. if (chip->chip_num > ENVCTRL_PCF8574_DEV2 ||
  2542. chip->val > ENVCTRL_DISK8LED_ALLOFF ||
  2543. chip->val < ENVCTRL_CHAR_ZERO) {
  2544. return (DDI_FAILURE);
  2545. }
  2546. if (chip->type != ENVCTRL_ENCL_BACKPLANE4 &&
  2547. chip->type != ENVCTRL_ENCL_BACKPLANE8) {
  2548. return (DDI_FAILURE);
  2549. }
  2550. /*
  2551. * Check all of the other controllwes LED states to make sure
  2552. * that there are no disk faults. If so then if the user is
  2553. * clearing the disk faults on this contoller, turn off
  2554. * the mass storage fault led.
  2555. */
  2556. backchip.type = PCF8574;
  2557. for (i = 0; i <= MAX_TAZ_CONTROLLERS; i++) {
  2558. if (controller_present[i] == -1)
  2559. continue;
  2560. backchip.chip_num = controller_addr[i];
  2561. envctrl_recv(unitp, (caddr_t *)(void *)&backchip, PCF8574);
  2562. if (chip->chip_num == controller_addr[i]) {
  2563. if (chip->val != ENVCTRL_CHAR_ZERO)
  2564. diskfault++;
  2565. } else if ((~backchip.val & 0xFF) != ENVCTRL_CHAR_ZERO) {
  2566. diskfault++;
  2567. }
  2568. }
  2569. fspchip.type = PCF8574A;
  2570. fspchip.chip_num = ENVCTRL_PCF8574_DEV6; /* 0x01 port 1 */
  2571. envctrl_recv(unitp, (caddr_t *)(void *)&fspchip, PCF8574);
  2572. if (diskfault) {
  2573. if (!(envctrl_isother_fault_led(unitp, fspchip.val & 0xFF,
  2574. ENVCTRL_FSP_DISK_ERR))) {
  2575. fspchip.val &= ~(ENVCTRL_FSP_DISK_ERR);
  2576. } else {
  2577. fspchip.val &= ~(ENVCTRL_FSP_DISK_ERR |
  2578. ENVCTRL_FSP_GEN_ERR);
  2579. }
  2580. fspchip.val = (fspchip.val &
  2581. ~(ENVCTRL_FSP_DISK_ERR | ENVCTRL_FSP_GEN_ERR));
  2582. } else {
  2583. fspchip.val = (fspchip.val |
  2584. (ENVCTRL_FSP_DISK_ERR | ENVCTRL_FSP_GEN_ERR));
  2585. }
  2586. fspchip.type = PCF8574A;
  2587. fspchip.chip_num = ENVCTRL_PCF8574_DEV6; /* 0x01 port 1 */
  2588. (void) envctrl_xmit(unitp, (caddr_t *)(void *)&fspchip, PCF8574);
  2589. for (i = 0; i < (sizeof (backaddrs) / sizeof (uint8_t)); i++) {
  2590. if (chip->chip_num == backaddrs[i]) {
  2591. instance = i;
  2592. }
  2593. }
  2594. switch (chip->type) {
  2595. case ENVCTRL_ENCL_BACKPLANE4:
  2596. envctrl_mod_encl_kstats(unitp, ENVCTRL_ENCL_BACKPLANE4,
  2597. instance, chip->val);
  2598. break;
  2599. case ENVCTRL_ENCL_BACKPLANE8:
  2600. envctrl_mod_encl_kstats(unitp, ENVCTRL_ENCL_BACKPLANE8,
  2601. instance, chip->val);
  2602. break;
  2603. default:
  2604. break;
  2605. }
  2606. chip->type = PCF8574;
  2607. /*
  2608. * we take the ones compliment of the val passed in
  2609. * because the hardware thinks that a "low" or "0"
  2610. * is the way to indicate a fault. of course software
  2611. * knows that a 1 is a TRUE state or fault. ;-)
  2612. */
  2613. chip->val = ~(chip->val);
  2614. (void) envctrl_xmit(unitp, (caddr_t *)(void *)chip, PCF8574);
  2615. return (DDI_SUCCESS);
  2616. }
  2617. void
  2618. envctrl_add_kstats(struct envctrlunit *unitp)
  2619. {
  2620. ASSERT(MUTEX_HELD(&unitp->umutex));
  2621. if ((unitp->enclksp = kstat_create(ENVCTRL_MODULE_NAME, unitp->instance,
  2622. ENVCTRL_KSTAT_ENCL, "misc", KSTAT_TYPE_RAW,
  2623. sizeof (unitp->encl_kstats),
  2624. KSTAT_FLAG_PERSISTENT)) == NULL) {
  2625. cmn_err(CE_WARN, "envctrl%d: encl raw kstat_create failed",
  2626. unitp->instance);
  2627. return;
  2628. }
  2629. unitp->enclksp->ks_update = envctrl_encl_kstat_update;
  2630. unitp->enclksp->ks_private = (void *)unitp;
  2631. kstat_install(unitp->enclksp);
  2632. if ((unitp->fanksp = kstat_create(ENVCTRL_MODULE_NAME, unitp->instance,
  2633. ENVCTRL_KSTAT_FANSTAT, "misc", KSTAT_TYPE_RAW,
  2634. sizeof (unitp->fan_kstats),
  2635. KSTAT_FLAG_PERSISTENT)) == NULL) {
  2636. cmn_err(CE_WARN, "envctrl%d: fans kstat_create failed",
  2637. unitp->instance);
  2638. return;
  2639. }
  2640. unitp->fanksp->ks_update = envctrl_fanstat_kstat_update;
  2641. unitp->fanksp->ks_private = (void *)unitp;
  2642. kstat_install(unitp->fanksp);
  2643. if ((unitp->psksp = kstat_create(ENVCTRL_MODULE_NAME, unitp->instance,
  2644. ENVCTRL_KSTAT_PSNAME, "misc", KSTAT_TYPE_RAW,
  2645. sizeof (unitp->ps_kstats),
  2646. KSTAT_FLAG_PERSISTENT)) == NULL) {
  2647. cmn_err(CE_WARN, "envctrl%d: ps name kstat_create failed",
  2648. unitp->instance);
  2649. return;
  2650. }
  2651. unitp->psksp->ks_update = envctrl_ps_kstat_update;
  2652. unitp->psksp->ks_private = (void *)unitp;
  2653. kstat_install(unitp->psksp);
  2654. }
  2655. int
  2656. envctrl_ps_kstat_update(kstat_t *ksp, int rw)
  2657. {
  2658. struct envctrlunit *unitp;
  2659. char *kstatp;
  2660. unitp = (struct envctrlunit *)ksp->ks_private;
  2661. mutex_enter(&unitp->umutex);
  2662. ASSERT(MUTEX_HELD(&unitp->umutex));
  2663. kstatp = (char *)ksp->ks_data;
  2664. if (rw == KSTAT_WRITE) {
  2665. return (EACCES);
  2666. } else {
  2667. unitp->psksp->ks_ndata = unitp->num_ps_present;
  2668. bcopy(&unitp->ps_kstats, kstatp, sizeof (unitp->ps_kstats));
  2669. }
  2670. mutex_exit(&unitp->umutex);
  2671. return (DDI_SUCCESS);
  2672. }
  2673. int
  2674. envctrl_fanstat_kstat_update(kstat_t *ksp, int rw)
  2675. {
  2676. struct envctrlunit *unitp;
  2677. char *kstatp;
  2678. kstatp = (char *)ksp->ks_data;
  2679. unitp = (struct envctrlunit *)ksp->ks_private;
  2680. mutex_enter(&unitp->umutex);
  2681. ASSERT(MUTEX_HELD(&unitp->umutex));
  2682. if (rw == KSTAT_WRITE) {
  2683. return (EACCES);
  2684. } else {
  2685. unitp->fanksp->ks_ndata = unitp->num_fans_present;
  2686. bcopy(unitp->fan_kstats, kstatp, sizeof (unitp->fan_kstats));
  2687. }
  2688. mutex_exit(&unitp->umutex);
  2689. return (DDI_SUCCESS);
  2690. }
  2691. int
  2692. envctrl_encl_kstat_update(kstat_t *ksp, int rw)
  2693. {
  2694. struct envctrlunit *unitp;
  2695. char *kstatp;
  2696. kstatp = (char *)ksp->ks_data;
  2697. unitp = (struct envctrlunit *)ksp->ks_private;
  2698. mutex_enter(&unitp->umutex);
  2699. ASSERT(MUTEX_HELD(&unitp->umutex));
  2700. if (rw == KSTAT_WRITE) {
  2701. return (EACCES);
  2702. } else {
  2703. unitp->enclksp->ks_ndata = unitp->num_encl_present;
  2704. (void) envctrl_get_fpm_status(unitp);
  2705. /* XXX Need to ad disk updates too ??? */
  2706. bcopy(unitp->encl_kstats, kstatp, sizeof (unitp->encl_kstats));
  2707. }
  2708. mutex_exit(&unitp->umutex);
  2709. return (DDI_SUCCESS);
  2710. }
  2711. /*
  2712. * called with unitp lock held
  2713. * type, fanspeed and fanflt will be set by the service routines
  2714. */
  2715. static void
  2716. envctrl_init_fan_kstats(struct envctrlunit *unitp)
  2717. {
  2718. int i;
  2719. ASSERT(MUTEX_HELD(&unitp->umutex));
  2720. for (i = 0; i < unitp->num_fans_present; i++) {
  2721. unitp->fan_kstats[i].instance = 0;
  2722. unitp->fan_kstats[i].type = 0;
  2723. unitp->fan_kstats[i].fans_ok = B_TRUE;
  2724. unitp->fan_kstats[i].fanflt_num = B_FALSE;
  2725. unitp->fan_kstats[i].fanspeed = B_FALSE;
  2726. }
  2727. unitp->fan_kstats[ENVCTRL_FAN_TYPE_PS].type = ENVCTRL_FAN_TYPE_PS;
  2728. unitp->fan_kstats[ENVCTRL_FAN_TYPE_CPU].type = ENVCTRL_FAN_TYPE_CPU;
  2729. if (unitp->AFB_present == B_TRUE)
  2730. unitp->fan_kstats[ENVCTRL_FAN_TYPE_AFB].type =
  2731. ENVCTRL_FAN_TYPE_AFB;
  2732. }
  2733. static void
  2734. envctrl_init_encl_kstats(struct envctrlunit *unitp)
  2735. {
  2736. int i;
  2737. uint8_t val;
  2738. struct envctrl_pcf8574_chip chip;
  2739. int *reg_prop;
  2740. uint_t len = 0;
  2741. ASSERT(MUTEX_HELD(&unitp->umutex));
  2742. for (i = 0; i < MAX_DEVS; i++) {
  2743. unitp->encl_kstats[i].instance = I2C_NODEV;
  2744. }
  2745. /*
  2746. * add in kstats now
  2747. * We ALWAYS HAVE THE FOLLOWING
  2748. * 1. FSP
  2749. * 2. AMB TEMPR
  2750. * 3. (1) CPU TEMPR
  2751. * 4. (1) 4 slot disk backplane
  2752. * OPTIONAL
  2753. * 8 slot backplane
  2754. * more cpu's
  2755. */
  2756. chip.type = PCF8574A;
  2757. chip.chip_num = ENVCTRL_PCF8574_DEV6; /* 0x01 port 1 */
  2758. envctrl_recv(unitp, (caddr_t *)(void *)&chip, PCF8574);
  2759. envctrl_add_encl_kstats(unitp, ENVCTRL_ENCL_FSP, INSTANCE_0,
  2760. chip.val & 0xFF);
  2761. val = envctrl_get_lm75_temp(unitp) & 0xFF;
  2762. envctrl_add_encl_kstats(unitp, ENVCTRL_ENCL_AMBTEMPR, INSTANCE_0, val);
  2763. if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, unitp->dip,
  2764. DDI_PROP_DONTPASS, ENVCTRL_DISK_LEDS_PR,
  2765. &reg_prop, &len) != DDI_PROP_SUCCESS) {
  2766. cmn_err(CE_WARN, "prop lookup of %s failed\n",
  2767. ENVCTRL_DISK_LEDS_PR);
  2768. return;
  2769. }
  2770. ASSERT(len != 0);
  2771. chip.type = PCF8574;
  2772. for (i = 0; i < len; i++) {
  2773. chip.chip_num = backaddrs[i];
  2774. if (reg_prop[i] == ENVCTRL_4SLOT_BACKPLANE) {
  2775. envctrl_recv(unitp, (caddr_t *)(void *)&chip, PCF8574);
  2776. envctrl_add_encl_kstats(unitp, ENVCTRL_ENCL_BACKPLANE4,
  2777. i, ~chip.val);
  2778. controller_present[i] = 1;
  2779. }
  2780. if (reg_prop[i] == ENVCTRL_8SLOT_BACKPLANE) {
  2781. envctrl_recv(unitp, (caddr_t *)(void *)&chip, PCF8574);
  2782. envctrl_add_encl_kstats(unitp, ENVCTRL_ENCL_BACKPLANE8,
  2783. i, ~chip.val);
  2784. controller_present[i] = 1;
  2785. }
  2786. }
  2787. ddi_prop_free((void *)reg_prop);
  2788. }
  2789. static void
  2790. envctrl_mod_encl_kstats(struct envctrlunit *unitp, int type,
  2791. int instance, uint8_t val)
  2792. {
  2793. int i = 0;
  2794. boolean_t inserted = B_FALSE;
  2795. ASSERT(MUTEX_HELD(&unitp->umutex));
  2796. while (i < MAX_DEVS && inserted == B_FALSE) {
  2797. if (unitp->encl_kstats[i].instance == instance &&
  2798. unitp->encl_kstats[i].type == type) {
  2799. unitp->encl_kstats[i].value = val;
  2800. inserted = B_TRUE;
  2801. }
  2802. i++;
  2803. }
  2804. }
  2805. static void
  2806. envctrl_probe_cpus(struct envctrlunit *unitp)
  2807. {
  2808. int instance;
  2809. /*
  2810. * The cpu search is as follows:
  2811. * If there is only 1 CPU module it is named as
  2812. * SUNW,UltraSPARC. If this is a match we still don't
  2813. * know what slot the cpu module is in therefore
  2814. * we need to check the "upa-portid" property.
  2815. * If we have more than 1 cpu, then they are appended by
  2816. * instance numbers and slot locations. e.g.
  2817. * SUNW,UltraSPARC@1,0 (slot 1). it would have been
  2818. * nice to have the naming consistent for one CPU e.g.
  2819. * SUNW,UltraSPARC@0,0...sigh
  2820. */
  2821. for (instance = 0; instance < ENVCTRL_MAX_CPUS; instance++) {
  2822. unitp->cpu_pr_location[instance] = B_FALSE;
  2823. }
  2824. ddi_walk_devs(ddi_root_node(), envctrl_match_cpu, unitp);
  2825. }
  2826. static int
  2827. envctrl_match_cpu(dev_info_t *dip, void *arg)
  2828. {
  2829. int cpu_slot;
  2830. char name[32];
  2831. char name1[32];
  2832. struct envctrlunit *unitp = (struct envctrlunit *)arg;
  2833. (void) sprintf(name, "%s", ENVCTRL_TAZCPU_STRING);
  2834. (void) sprintf(name1, "%s", ENVCTRL_TAZBLKBRDCPU_STRING);
  2835. if ((strcmp(ddi_node_name(dip), name) == 0) ||
  2836. (strcmp(ddi_node_name(dip), name1) == 0)) {
  2837. if ((cpu_slot = (int)ddi_getprop(DDI_DEV_T_ANY, dip,
  2838. DDI_PROP_DONTPASS, "upa-portid", -1)) == -1) {
  2839. cmn_err(CE_WARN, "envctrl no cpu upa-portid");
  2840. } else {
  2841. unitp->cpu_pr_location[cpu_slot] = B_TRUE;
  2842. unitp->num_cpus_present++;
  2843. }
  2844. }
  2845. return (DDI_WALK_CONTINUE);
  2846. }
  2847. /*
  2848. * This routine returns TRUE if some other error condition
  2849. * has set the GEN_ERR FAULT LED. Tp further complicate this
  2850. * LED panel we have overloaded the GEN_ERR LED to indicate
  2851. * that a fan fault has occurred without having a fan fault
  2852. * LED as does all other error conditions. So we just take the
  2853. * software state and return true. The whole purpose of this functon
  2854. * is to tell us wehther or not we can shut off the GEN_FAULT LED.
  2855. * NOTE: this ledval is usually one of the following FSP vals
  2856. * EXCEPT in the case of the fan fail.. we pass in a "0".
  2857. */
  2858. static int
  2859. envctrl_isother_fault_led(struct envctrlunit *unitp, uint8_t fspval,
  2860. uint8_t thisled)
  2861. {
  2862. int status = B_FALSE;
  2863. if (fspval != 0) {
  2864. fspval = (fspval & ~(thisled));
  2865. }
  2866. if (unitp->num_fans_failed > 0 && thisled != 0) {
  2867. status = B_TRUE;
  2868. } else if (fspval & ENVCTRL_FSP_DISK_ERR) {
  2869. status = B_TRUE;
  2870. } else if (fspval & ENVCTRL_FSP_PS_ERR) {
  2871. status = B_TRUE;
  2872. } else if (fspval & ENVCTRL_FSP_TEMP_ERR) {
  2873. status = B_TRUE;
  2874. }
  2875. return (status);
  2876. }
  2877. static void
  2878. envctrl_pshotplug_poll(void *arg)
  2879. {
  2880. struct envctrlunit *unitp = (struct envctrlunit *)arg;
  2881. mutex_enter(&unitp->umutex);
  2882. envctrl_ps_probe(unitp);
  2883. mutex_exit(&unitp->umutex);
  2884. }
  2885. /*
  2886. * The following routines implement the i2c protocol.
  2887. * They should be removed once the envctrl_targets.c file is included.
  2888. */
  2889. /*
  2890. * put host interface into master mode
  2891. */
  2892. static int
  2893. eHc_start_pcf8584(struct eHc_envcunit *ehcp, uint8_t byteaddress)
  2894. {
  2895. uint8_t poll_status;
  2896. uint8_t discard;
  2897. int i;
  2898. /* wait if bus is busy */
  2899. i = 0;
  2900. do {
  2901. drv_usecwait(1000);
  2902. poll_status =
  2903. ddi_get8(ehcp->ctlr_handle, &ehcp->bus_ctl_regs->s1);
  2904. i++;
  2905. } while (((poll_status & EHC_S1_NBB) == 0) && i < EHC_MAX_WAIT);
  2906. if (i == EHC_MAX_WAIT) {
  2907. DCMNERR(CE_WARN, "eHc_start_pcf8584: I2C bus busy");
  2908. return (EHC_FAILURE);
  2909. }
  2910. if (poll_status & EHC_S1_BER) {
  2911. DCMN2ERR(CE_WARN, "eHc_start_pcf8584: I2C bus error");
  2912. return (EHC_FAILURE);
  2913. }
  2914. if (poll_status & EHC_S1_LAB) {
  2915. DCMN2ERR(CE_WARN, "eHc_start_pcf8584: Lost arbitration");
  2916. return (EHC_FAILURE);
  2917. }
  2918. /* load the slave address */
  2919. ddi_put8(ehcp->ctlr_handle, &ehcp->bus_ctl_regs->s0, byteaddress);
  2920. /* generate the "start condition" and clock out the slave address */
  2921. ddi_put8(ehcp->ctlr_handle, &ehcp->bus_ctl_regs->s1,
  2922. EHC_S1_PIN | EHC_S1_ES0 | EHC_S1_STA | EHC_S1_ACK);
  2923. /* wait for completion of transmission */
  2924. i = 0;
  2925. do {
  2926. drv_usecwait(1000);
  2927. poll_status =
  2928. ddi_get8(ehcp->ctlr_handle, &ehcp->bus_ctl_regs->s1);
  2929. i++;
  2930. } while ((poll_status & EHC_S1_PIN) && i < EHC_MAX_WAIT);
  2931. if (i == EHC_MAX_WAIT) {
  2932. DCMNERR(CE_WARN, "eHc_start_pcf8584: I2C bus busy");
  2933. return (EHC_FAILURE);
  2934. }
  2935. if (poll_status & EHC_S1_BER) {
  2936. DCMN2ERR(CE_WARN, "eHc_start_pcf8584: I2C bus error");
  2937. return (EHC_FAILURE);
  2938. }
  2939. if (poll_status & EHC_S1_LAB) {
  2940. DCMN2ERR(CE_WARN, "eHc_start_pcf8584: Lost arbitration");
  2941. return (EHC_FAILURE);
  2942. }
  2943. if (poll_status & EHC_S1_LRB) {
  2944. DCMNERR(CE_WARN, "eHc_start_pcf8584: No slave ACK");
  2945. return (EHC_NO_SLAVE_ACK);
  2946. }
  2947. /*
  2948. * If this is a read we are setting up for (as indicated by
  2949. * the least significant byte being set), read
  2950. * and discard the first byte off the bus - this
  2951. * is the slave address.
  2952. */
  2953. i = 0;
  2954. if (byteaddress & EHC_BYTE_READ) {
  2955. discard = ddi_get8(ehcp->ctlr_handle, &ehcp->bus_ctl_regs->s0);
  2956. #ifdef lint
  2957. discard = discard;
  2958. #endif
  2959. /* wait for completion of transmission */
  2960. do {
  2961. drv_usecwait(1000);
  2962. poll_status = ddi_get8(ehcp->ctlr_handle,
  2963. &ehcp->bus_ctl_regs->s1);
  2964. i++;
  2965. } while ((poll_status & EHC_S1_PIN) && i < EHC_MAX_WAIT);
  2966. if (i == EHC_MAX_WAIT) {
  2967. DCMNERR(CE_WARN, "eHc_start_pcf8584: I2C bus busy");
  2968. return (EHC_FAILURE);
  2969. }
  2970. if (poll_status & EHC_S1_BER) {
  2971. DCMN2ERR(CE_WARN,
  2972. "eHc_start_pcf8584: I2C bus error");
  2973. return (EHC_FAILURE);
  2974. }
  2975. if (poll_status & EHC_S1_LAB) {
  2976. DCMN2ERR(CE_WARN,
  2977. "eHc_start_pcf8584: Lost arbitration");
  2978. return (EHC_FAILURE);
  2979. }
  2980. }
  2981. return (EHC_SUCCESS);
  2982. }
  2983. /*
  2984. * put host interface into slave/receiver mode
  2985. */
  2986. static void
  2987. eHc_stop_pcf8584(struct eHc_envcunit *ehcp)
  2988. {
  2989. ddi_put8(ehcp->ctlr_handle, &ehcp->bus_ctl_regs->s1,
  2990. EHC_S1_PIN | EHC_S1_ES0 | EHC_S1_STO | EHC_S1_ACK);
  2991. }
  2992. static int
  2993. eHc_read_pcf8584(struct eHc_envcunit *ehcp, uint8_t *data)
  2994. {
  2995. uint8_t poll_status;
  2996. int i = 0;
  2997. /* Read the byte of interest */
  2998. *data = ddi_get8(ehcp->ctlr_handle, &ehcp->bus_ctl_regs->s0);
  2999. /* wait for completion of transmission */
  3000. do {
  3001. drv_usecwait(1000);
  3002. poll_status =
  3003. ddi_get8(ehcp->ctlr_handle, &ehcp->bus_ctl_regs->s1);
  3004. i++;
  3005. } while ((poll_status & EHC_S1_PIN) && i < EHC_MAX_WAIT);
  3006. if (i == EHC_MAX_WAIT) {
  3007. DCMNERR(CE_WARN, "eHc_read_pcf8584: I2C bus busy");
  3008. return (EHC_FAILURE);
  3009. }
  3010. if (poll_status & EHC_S1_BER) {
  3011. DCMN2ERR(CE_WARN, "eHc_read_pcf8584: I2C bus error");
  3012. return (EHC_FAILURE);
  3013. }
  3014. if (poll_status & EHC_S1_LAB) {
  3015. DCMN2ERR(CE_WARN, "eHc_read_pcf8584: Lost arbitration");
  3016. return (EHC_FAILURE);
  3017. }
  3018. return (EHC_SUCCESS);
  3019. }
  3020. /*
  3021. * host interface is in transmitter state, thus mode is master/transmitter
  3022. * NOTE to Bill: this check the LRB bit (only done in transmit mode).
  3023. */
  3024. static int
  3025. eHc_write_pcf8584(struct eHc_envcunit *ehcp, uint8_t data)
  3026. {
  3027. uint8_t poll_status;
  3028. int i = 0;
  3029. /* send the data, EHC_S1_PIN should go to "1" immediately */
  3030. ddi_put8(ehcp->ctlr_handle, &ehcp->bus_ctl_regs->s0, data);
  3031. /* wait for completion of transmission */
  3032. do {
  3033. drv_usecwait(1000);
  3034. poll_status =
  3035. ddi_get8(ehcp->ctlr_handle, &ehcp->bus_ctl_regs->s1);
  3036. i++;
  3037. } while ((poll_status & EHC_S1_PIN) && i < EHC_MAX_WAIT);
  3038. if (i == EHC_MAX_WAIT) {
  3039. DCMNERR(CE_WARN, "eHc_write_pcf8584: I2C bus busy");
  3040. return (EHC_FAILURE);
  3041. }
  3042. if (poll_status & EHC_S1_BER) {
  3043. DCMN2ERR(CE_WARN, "eHc_write_pcf8584: I2C bus error");
  3044. return (EHC_FAILURE);
  3045. }
  3046. if (poll_status & EHC_S1_LAB) {
  3047. DCMN2ERR(CE_WARN, "eHc_write_pcf8584: Lost arbitration");
  3048. return (EHC_FAILURE);
  3049. }
  3050. if (poll_status & EHC_S1_LRB) {
  3051. DCMNERR(CE_WARN, "eHc_write_pcf8584: No slave ACK");
  3052. return (EHC_NO_SLAVE_ACK);
  3053. }
  3054. return (EHC_SUCCESS);
  3055. }
  3056. static int
  3057. eHc_after_read_pcf8584(struct eHc_envcunit *ehcp, uint8_t *data)
  3058. {
  3059. uint8_t discard;
  3060. uint8_t poll_status;
  3061. int i = 0;
  3062. /* set ACK in register S1 to 0 */
  3063. ddi_put8(ehcp->ctlr_handle, &ehcp->bus_ctl_regs->s1, EHC_S1_ES0);
  3064. /*
  3065. * Read the "byte-before-the-last-byte" - sets PIN bit to '1'
  3066. */
  3067. *data = ddi_get8(ehcp->ctlr_handle, &ehcp->bus_ctl_regs->s0);
  3068. /* wait for completion of transmission */
  3069. do {
  3070. drv_usecwait(1000);
  3071. poll_status =
  3072. ddi_get8(ehcp->ctlr_handle, &ehcp->bus_ctl_regs->s1);
  3073. i++;
  3074. } while ((poll_status & EHC_S1_PIN) && i < EHC_MAX_WAIT);
  3075. if (i == EHC_MAX_WAIT) {
  3076. DCMNERR(CE_WARN, "eHc_after_read_pcf8584: I2C bus busy");
  3077. return (EHC_FAILURE);
  3078. }
  3079. if (poll_status & EHC_S1_BER) {
  3080. DCMN2ERR(CE_WARN,
  3081. "eHc_after_read_pcf8584: I2C bus error");
  3082. return (EHC_FAILURE);
  3083. }
  3084. if (poll_status & EHC_S1_LAB) {
  3085. DCMN2ERR(CE_WARN, "eHc_after_read_pcf8584: Lost arbitration");
  3086. return (EHC_FAILURE);
  3087. }
  3088. /*
  3089. * Generate the "stop" condition.
  3090. */
  3091. eHc_stop_pcf8584(ehcp);
  3092. /*
  3093. * Read the "last" byte.
  3094. */
  3095. discard = ddi_get8(ehcp->ctlr_handle, &ehcp->bus_ctl_regs->s0);
  3096. #ifdef lint
  3097. discard = discard;
  3098. #endif
  3099. return (EHC_SUCCESS);
  3100. }
  3101. /*
  3102. * Write to the TDA8444 chip.
  3103. * byteaddress = chip type base address | chip offset address.
  3104. */
  3105. static int
  3106. eHc_write_tda8444(struct eHc_envcunit *ehcp, int byteaddress, int instruction,
  3107. int subaddress, uint8_t *buf, int size)
  3108. {
  3109. uint8_t control;
  3110. int i, status;
  3111. ASSERT((byteaddress & 0x1) == 0);
  3112. ASSERT(subaddress < 8);
  3113. ASSERT(instruction == 0xf || instruction == 0x0);
  3114. ASSERT(MUTEX_HELD(&ehcp->umutex));
  3115. control = (instruction << 4) | subaddress;
  3116. if ((status = eHc_start_pcf8584(ehcp, byteaddress)) != EHC_SUCCESS) {
  3117. if (status == EHC_NO_SLAVE_ACK) {
  3118. /*
  3119. * Send the "stop" condition.
  3120. */
  3121. eHc_stop_pcf8584(ehcp);
  3122. }
  3123. return (EHC_FAILURE);
  3124. }
  3125. if ((status = eHc_write_pcf8584(ehcp, control)) != EHC_SUCCESS) {
  3126. if (status == EHC_NO_SLAVE_ACK) {
  3127. /*
  3128. * Send the "stop" condition.
  3129. */
  3130. eHc_stop_pcf8584(ehcp);
  3131. }
  3132. return (EHC_FAILURE);
  3133. }
  3134. for (i = 0; i < size; i++) {
  3135. if ((status = eHc_write_pcf8584(ehcp, (buf[i] & 0x3f))) !=
  3136. EHC_SUCCESS) {
  3137. if (status == EHC_NO_SLAVE_ACK)
  3138. eHc_stop_pcf8584(ehcp);
  3139. return (EHC_FAILURE);
  3140. }
  3141. }
  3142. eHc_stop_pcf8584(ehcp);
  3143. return (EHC_SUCCESS);
  3144. }
  3145. /*
  3146. * Read from PCF8574A chip.
  3147. * byteaddress = chip type base address | chip offset address.
  3148. */
  3149. static int
  3150. eHc_read_pcf8574a(struct eHc_envcunit *ehcp, int byteaddress, uint8_t *buf,
  3151. int size)
  3152. {
  3153. int i;
  3154. int status;
  3155. uint8_t discard;
  3156. ASSERT((byteaddress & 0x1) == 0);
  3157. ASSERT(MUTEX_HELD(&ehcp->umutex));
  3158. /*
  3159. * Put the bus into the start condition
  3160. */
  3161. if ((status = eHc_start_pcf8584(ehcp, EHC_BYTE_READ | byteaddress)) !=
  3162. EHC_SUCCESS) {
  3163. if (status == EHC_NO_SLAVE_ACK) {
  3164. /*
  3165. * Send the "stop" condition.
  3166. */
  3167. eHc_stop_pcf8584(ehcp);
  3168. /*
  3169. * Read the last byte - discard it.
  3170. */
  3171. discard = ddi_get8(ehcp->ctlr_handle,
  3172. &ehcp->bus_ctl_regs->s0);
  3173. #ifdef lint
  3174. discard = discard;
  3175. #endif
  3176. }
  3177. return (EHC_FAILURE);
  3178. }
  3179. for (i = 0; i < size - 1; i++) {
  3180. if ((status = eHc_read_pcf8584(ehcp, &buf[i])) != EHC_SUCCESS) {
  3181. return (EHC_FAILURE);
  3182. }
  3183. }
  3184. /*
  3185. * Handle the part of the bus protocol which comes
  3186. * after a read, including reading the last byte.
  3187. */
  3188. if (eHc_after_read_pcf8584(ehcp, &buf[i]) != EHC_SUCCESS) {
  3189. return (EHC_FAILURE);
  3190. }
  3191. return (EHC_SUCCESS);
  3192. }
  3193. /*
  3194. * Write to the PCF8574A chip.
  3195. * byteaddress = chip type base address | chip offset address.
  3196. */
  3197. static int
  3198. eHc_write_pcf8574a(struct eHc_envcunit *ehcp, int byteaddress, uint8_t *buf,
  3199. int size)
  3200. {
  3201. int i;
  3202. int status;
  3203. ASSERT((byteaddress & 0x1) == 0);
  3204. ASSERT(MUTEX_HELD(&ehcp->umutex));
  3205. /*
  3206. * Put the bus into the start condition (write)
  3207. */
  3208. if ((status = eHc_start_pcf8584(ehcp, byteaddress)) != EHC_SUCCESS) {
  3209. if (status == EHC_NO_SLAVE_ACK) {
  3210. /*
  3211. * Send the "stop" condition.
  3212. */
  3213. eHc_stop_pcf8584(ehcp);
  3214. }
  3215. return (EHC_FAILURE);
  3216. }
  3217. /*
  3218. * Send the data - poll as needed.
  3219. */
  3220. for (i = 0; i < size; i++) {
  3221. if ((status = eHc_write_pcf8584(ehcp, buf[i])) != EHC_SUCCESS) {
  3222. if (status == EHC_NO_SLAVE_ACK)
  3223. eHc_stop_pcf8584(ehcp);
  3224. return (EHC_FAILURE);
  3225. }
  3226. }
  3227. /*
  3228. * Transmission complete - generate stop condition and
  3229. * put device back into slave receiver mode.
  3230. */
  3231. eHc_stop_pcf8584(ehcp);
  3232. return (EHC_SUCCESS);
  3233. }
  3234. /*
  3235. * Read from the PCF8574 chip.
  3236. * byteaddress = chip type base address | chip offset address.
  3237. */
  3238. static int
  3239. eHc_read_pcf8574(struct eHc_envcunit *ehcp, int byteaddress, uint8_t *buf,
  3240. int size)
  3241. {
  3242. int i;
  3243. int status;
  3244. uint8_t discard;
  3245. ASSERT((byteaddress & 0x1) == 0);
  3246. ASSERT(MUTEX_HELD(&ehcp->umutex));
  3247. /*
  3248. * Put the bus into the start condition
  3249. */
  3250. if ((status = eHc_start_pcf8584(ehcp, EHC_BYTE_READ | byteaddress)) !=
  3251. EHC_SUCCESS) {
  3252. if (status == EHC_NO_SLAVE_ACK) {
  3253. /*
  3254. * Send the "stop" condition.
  3255. */
  3256. eHc_stop_pcf8584(ehcp);
  3257. /*
  3258. * Read the last byte - discard it.
  3259. */
  3260. discard = ddi_get8(ehcp->ctlr_handle,
  3261. &ehcp->bus_ctl_regs->s0);
  3262. #ifdef lint
  3263. discard = discard;
  3264. #endif
  3265. }
  3266. return (EHC_FAILURE);
  3267. }
  3268. for (i = 0; i < size - 1; i++) {
  3269. if ((status = eHc_read_pcf8584(ehcp, &buf[i])) != EHC_SUCCESS) {
  3270. return (EHC_FAILURE);
  3271. }
  3272. }
  3273. /*
  3274. * Handle the part of the bus protocol which comes
  3275. * after a read.
  3276. */
  3277. if (eHc_after_read_pcf8584(ehcp, &buf[i]) != EHC_SUCCESS) {
  3278. return (EHC_FAILURE);
  3279. }
  3280. return (EHC_SUCCESS);
  3281. }
  3282. /*
  3283. * Write to the PCF8574 chip.
  3284. * byteaddress = chip type base address | chip offset address.
  3285. */
  3286. static int
  3287. eHc_write_pcf8574(struct eHc_envcunit *ehcp, int byteaddress, uint8_t *buf,
  3288. int size)
  3289. {
  3290. int i;
  3291. int status;
  3292. ASSERT((byteaddress & 0x1) == 0);
  3293. ASSERT(MUTEX_HELD(&ehcp->umutex));
  3294. /*
  3295. * Put the bus into the start condition (write)
  3296. */
  3297. if ((status = eHc_start_pcf8584(ehcp, byteaddress)) != EHC_SUCCESS) {
  3298. if (status == EHC_NO_SLAVE_ACK) {
  3299. /*
  3300. * Send the "stop" condition.
  3301. */
  3302. eHc_stop_pcf8584(ehcp);
  3303. }
  3304. return (EHC_FAILURE);
  3305. }
  3306. /*
  3307. * Send the data - poll as needed.
  3308. */
  3309. for (i = 0; i < size; i++) {
  3310. if ((status = eHc_write_pcf8584(ehcp, buf[i])) != EHC_SUCCESS) {
  3311. if (status == EHC_NO_SLAVE_ACK)
  3312. eHc_stop_pcf8584(ehcp);
  3313. return (EHC_FAILURE);
  3314. }
  3315. }
  3316. /*
  3317. * Transmission complete - generate stop condition and
  3318. * put device back into slave receiver mode.
  3319. */
  3320. eHc_stop_pcf8584(ehcp);
  3321. return (EHC_SUCCESS);
  3322. }
  3323. /*
  3324. * Read from the LM75
  3325. * byteaddress = chip type base address | chip offset address.
  3326. */
  3327. static int
  3328. eHc_read_lm75(struct eHc_envcunit *ehcp, int byteaddress, uint8_t *buf,
  3329. int size)
  3330. {
  3331. int i;
  3332. int status;
  3333. uint8_t discard;
  3334. ASSERT((byteaddress & 0x1) == 0);
  3335. ASSERT(MUTEX_HELD(&ehcp->umutex));
  3336. /*
  3337. * Put the bus into the start condition
  3338. */
  3339. if ((status = eHc_start_pcf8584(ehcp, EHC_BYTE_READ | byteaddress)) !=
  3340. EHC_SUCCESS) {
  3341. if (status == EHC_NO_SLAVE_ACK) {
  3342. /*
  3343. * Send the stop condition.
  3344. */
  3345. eHc_stop_pcf8584(ehcp);
  3346. /*
  3347. * Read the last byte - discard it.
  3348. */
  3349. discard = ddi_get8(ehcp->ctlr_handle,
  3350. &ehcp->bus_ctl_regs->s0);
  3351. #ifdef lint
  3352. discard = discard;
  3353. #endif
  3354. }
  3355. return (EHC_FAILURE);
  3356. }
  3357. for (i = 0; i < size - 1; i++) {
  3358. if ((status = eHc_read_pcf8584(ehcp, &buf[i])) != EHC_SUCCESS) {
  3359. return (EHC_FAILURE);
  3360. }
  3361. }
  3362. /*
  3363. * Handle the part of the bus protocol which comes
  3364. * after a read.
  3365. */
  3366. if (eHc_after_read_pcf8584(ehcp, &buf[i]) != EHC_SUCCESS) {
  3367. return (EHC_FAILURE);
  3368. }
  3369. return (EHC_SUCCESS);
  3370. }
  3371. /*
  3372. * Write to the PCF8583 chip.
  3373. * byteaddress = chip type base address | chip offset address.
  3374. */
  3375. static int
  3376. eHc_write_pcf8583(struct eHc_envcunit *ehcp, int byteaddress, uint8_t *buf,
  3377. int size)
  3378. {
  3379. int i;
  3380. int status;
  3381. ASSERT((byteaddress & 0x1) == 0);
  3382. ASSERT(MUTEX_HELD(&ehcp->umutex));
  3383. if ((status = eHc_start_pcf8584(ehcp, byteaddress)) != EHC_SUCCESS) {
  3384. if (status == EHC_NO_SLAVE_ACK) {
  3385. /*
  3386. * Send the "stop" condition.
  3387. */
  3388. eHc_stop_pcf8584(ehcp);
  3389. }
  3390. return (EHC_FAILURE);
  3391. }
  3392. /*
  3393. * Send the data - poll as needed.
  3394. */
  3395. for (i = 0; i < size; i++) {
  3396. if ((status = eHc_write_pcf8584(ehcp, buf[i])) != EHC_SUCCESS) {
  3397. if (status == EHC_NO_SLAVE_ACK)
  3398. eHc_stop_pcf8584(ehcp);
  3399. return (EHC_FAILURE);
  3400. }
  3401. }
  3402. /*
  3403. * Transmission complete - generate stop condition and
  3404. * put device back into slave receiver mode.
  3405. */
  3406. eHc_stop_pcf8584(ehcp);
  3407. return (EHC_SUCCESS);
  3408. }
  3409. /*
  3410. * Read from the PCF8581 chip.
  3411. * byteaddress = chip type base address | chip offset address.
  3412. */
  3413. static int
  3414. eHc_read_pcf8591(struct eHc_envcunit *ehcp, int byteaddress, int channel,
  3415. int autoinc, int amode, int aenable, uint8_t *buf, int size)
  3416. {
  3417. int i;
  3418. int status;
  3419. uint8_t control;
  3420. uint8_t discard;
  3421. ASSERT((byteaddress & 0x1) == 0);
  3422. ASSERT(channel < 4);
  3423. ASSERT(amode < 4);
  3424. ASSERT(MUTEX_HELD(&ehcp->umutex));
  3425. /*
  3426. * Write the control word to the PCF8591.
  3427. * Follow the control word with a repeated START byte
  3428. * rather than a STOP so that reads can follow without giving
  3429. * up the bus.
  3430. */
  3431. control = ((aenable << 6) | (amode << 4) | (autoinc << 2) | channel);
  3432. if ((status = eHc_start_pcf8584(ehcp, byteaddress)) != EHC_SUCCESS) {
  3433. if (status == EHC_NO_SLAVE_ACK) {
  3434. eHc_stop_pcf8584(ehcp);
  3435. }
  3436. return (EHC_FAILURE);
  3437. }
  3438. if ((status = eHc_write_pcf8584(ehcp, control)) != EHC_SUCCESS) {
  3439. if (status == EHC_NO_SLAVE_ACK)
  3440. eHc_stop_pcf8584(ehcp);
  3441. return (EHC_FAILURE);
  3442. }
  3443. /*
  3444. * The following two operations, 0x45 to S1, and the byteaddress
  3445. * to S0, will result in a repeated START being sent out on the bus.
  3446. * Refer to Fig.8 of Philips Semiconductors PCF8584 product spec.
  3447. */
  3448. ddi_put8(ehcp->ctlr_handle, &ehcp->bus_ctl_regs->s1,
  3449. EHC_S1_ES0 | EHC_S1_STA | EHC_S1_ACK);
  3450. ddi_put8(ehcp->ctlr_handle, &ehcp->bus_ctl_regs->s0,
  3451. EHC_BYTE_READ | byteaddress);
  3452. i = 0;
  3453. do {
  3454. drv_usecwait(1000);
  3455. status =
  3456. ddi_get8(ehcp->ctlr_handle, &ehcp->bus_ctl_regs->s1);
  3457. i++;
  3458. } while ((status & EHC_S1_PIN) && i < EHC_MAX_WAIT);
  3459. if (i == EHC_MAX_WAIT) {
  3460. DCMNERR(CE_WARN, "eHc_read_pcf8591(): read of S1 failed");
  3461. return (EHC_FAILURE);
  3462. }
  3463. if (status & EHC_S1_LRB) {
  3464. DCMNERR(CE_WARN, "eHc_read_pcf8591(): No slave ACK");
  3465. /*
  3466. * Send the stop condition.
  3467. */
  3468. eHc_stop_pcf8584(ehcp);
  3469. /*
  3470. * Read the last byte - discard it.
  3471. */
  3472. discard = ddi_get8(ehcp->ctlr_handle, &ehcp->bus_ctl_regs->s0);
  3473. #ifdef lint
  3474. discard = discard;
  3475. #endif
  3476. return (EHC_FAILURE);
  3477. }
  3478. if (status & EHC_S1_BER) {
  3479. DCMN2ERR(CE_WARN, "eHc_read_pcf8591(): Bus error");
  3480. return (EHC_FAILURE);
  3481. }
  3482. if (status & EHC_S1_LAB) {
  3483. DCMN2ERR(CE_WARN, "eHc_read_pcf8591(): Lost Arbitration");
  3484. return (EHC_FAILURE);
  3485. }
  3486. /*
  3487. * Discard first read as per PCF8584 master receiver protocol.
  3488. * This is normally done in the eHc_start_pcf8584() routine.
  3489. */
  3490. if ((status = eHc_read_pcf8584(ehcp, &discard)) != EHC_SUCCESS) {
  3491. return (EHC_FAILURE);
  3492. }
  3493. /* Discard second read as per PCF8591 protocol */
  3494. if ((status = eHc_read_pcf8584(ehcp, &discard)) != EHC_SUCCESS) {
  3495. return (EHC_FAILURE);
  3496. }
  3497. for (i = 0; i < size - 1; i++) {
  3498. if ((status = eHc_read_pcf8584(ehcp, &buf[i])) != EHC_SUCCESS) {
  3499. return (EHC_FAILURE);
  3500. }
  3501. }
  3502. if (eHc_after_read_pcf8584(ehcp, &buf[i]) != EHC_SUCCESS) {
  3503. return (EHC_FAILURE);
  3504. }
  3505. return (EHC_SUCCESS);
  3506. }