1a3114836SGerry Liu /*
2a3114836SGerry Liu * CDDL HEADER START
3a3114836SGerry Liu *
4a3114836SGerry Liu * The contents of this file are subject to the terms of the
5a3114836SGerry Liu * Common Development and Distribution License (the "License").
6a3114836SGerry Liu * You may not use this file except in compliance with the License.
7a3114836SGerry Liu *
8a3114836SGerry Liu * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9a3114836SGerry Liu * or http://www.opensolaris.org/os/licensing.
10a3114836SGerry Liu * See the License for the specific language governing permissions
11a3114836SGerry Liu * and limitations under the License.
12a3114836SGerry Liu *
13a3114836SGerry Liu * When distributing Covered Code, include this CDDL HEADER in each
14a3114836SGerry Liu * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15a3114836SGerry Liu * If applicable, add the following below this CDDL HEADER, with the
16a3114836SGerry Liu * fields enclosed by brackets "[]" replaced with your own identifying
17a3114836SGerry Liu * information: Portions Copyright [yyyy] [name of copyright owner]
18a3114836SGerry Liu *
19a3114836SGerry Liu * CDDL HEADER END
20a3114836SGerry Liu */
21a3114836SGerry Liu
22a3114836SGerry Liu /*
23a3114836SGerry Liu * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24a3114836SGerry Liu * Use is subject to license terms.
25a3114836SGerry Liu */
26a3114836SGerry Liu /*
27a3114836SGerry Liu * Copyright (c) 2010, Intel Corporation.
28a3114836SGerry Liu * All rights reserved.
29a3114836SGerry Liu */
30a3114836SGerry Liu
31a3114836SGerry Liu /*
32a3114836SGerry Liu * PIM-DR layer of DR driver. Provides interface between user
33a3114836SGerry Liu * level applications and the PSM-DR layer.
34a3114836SGerry Liu */
35a3114836SGerry Liu
36a3114836SGerry Liu #include <sys/note.h>
37a3114836SGerry Liu #include <sys/debug.h>
38a3114836SGerry Liu #include <sys/types.h>
39a3114836SGerry Liu #include <sys/errno.h>
40a3114836SGerry Liu #include <sys/cred.h>
41a3114836SGerry Liu #include <sys/dditypes.h>
42a3114836SGerry Liu #include <sys/devops.h>
43a3114836SGerry Liu #include <sys/modctl.h>
44a3114836SGerry Liu #include <sys/poll.h>
45a3114836SGerry Liu #include <sys/conf.h>
46a3114836SGerry Liu #include <sys/ddi.h>
47a3114836SGerry Liu #include <sys/sunddi.h>
48a3114836SGerry Liu #include <sys/sunndi.h>
49a3114836SGerry Liu #include <sys/stat.h>
50a3114836SGerry Liu #include <sys/kmem.h>
51a3114836SGerry Liu #include <sys/processor.h>
52a3114836SGerry Liu #include <sys/cpuvar.h>
53a3114836SGerry Liu #include <sys/mem_config.h>
54a3114836SGerry Liu
55a3114836SGerry Liu #include <sys/autoconf.h>
56a3114836SGerry Liu #include <sys/cmn_err.h>
57a3114836SGerry Liu
58a3114836SGerry Liu #include <sys/ddi_impldefs.h>
59a3114836SGerry Liu #include <sys/promif.h>
60a3114836SGerry Liu #include <sys/machsystm.h>
61a3114836SGerry Liu
62a3114836SGerry Liu #include <sys/dr.h>
63a3114836SGerry Liu #include <sys/drmach.h>
64a3114836SGerry Liu #include <sys/dr_util.h>
65a3114836SGerry Liu
66a3114836SGerry Liu extern int nulldev();
67a3114836SGerry Liu extern int nodev();
68a3114836SGerry Liu extern struct memlist *phys_install;
69a3114836SGerry Liu
70a3114836SGerry Liu #ifdef DEBUG
71a3114836SGerry Liu uint_t dr_debug = 0; /* dr.h for bit values */
72a3114836SGerry Liu #endif /* DEBUG */
73a3114836SGerry Liu
74*8682d1efSRichard Lowe static int dr_dev_type_to_nt(char *);
75*8682d1efSRichard Lowe
76a3114836SGerry Liu /*
77a3114836SGerry Liu * NOTE: state_str, nt_str and SBD_CMD_STR are only used in a debug
78a3114836SGerry Liu * kernel. They are, however, referenced during both debug and non-debug
79a3114836SGerry Liu * compiles.
80a3114836SGerry Liu */
81a3114836SGerry Liu
82a3114836SGerry Liu static char *state_str[] = {
83a3114836SGerry Liu "EMPTY", "OCCUPIED", "CONNECTED", "UNCONFIGURED",
84a3114836SGerry Liu "PARTIAL", "CONFIGURED", "RELEASE", "UNREFERENCED",
85a3114836SGerry Liu "FATAL"
86a3114836SGerry Liu };
87a3114836SGerry Liu
88a3114836SGerry Liu #define SBD_CMD_STR(c) \
89a3114836SGerry Liu (((c) == SBD_CMD_ASSIGN) ? "ASSIGN" : \
90a3114836SGerry Liu ((c) == SBD_CMD_UNASSIGN) ? "UNASSIGN" : \
91a3114836SGerry Liu ((c) == SBD_CMD_POWERON) ? "POWERON" : \
92a3114836SGerry Liu ((c) == SBD_CMD_POWEROFF) ? "POWEROFF" : \
93a3114836SGerry Liu ((c) == SBD_CMD_TEST) ? "TEST" : \
94a3114836SGerry Liu ((c) == SBD_CMD_CONNECT) ? "CONNECT" : \
95a3114836SGerry Liu ((c) == SBD_CMD_DISCONNECT) ? "DISCONNECT" : \
96a3114836SGerry Liu ((c) == SBD_CMD_CONFIGURE) ? "CONFIGURE" : \
97a3114836SGerry Liu ((c) == SBD_CMD_UNCONFIGURE) ? "UNCONFIGURE" : \
98a3114836SGerry Liu ((c) == SBD_CMD_GETNCM) ? "GETNCM" : \
99a3114836SGerry Liu ((c) == SBD_CMD_PASSTHRU) ? "PASSTHRU" : \
100a3114836SGerry Liu ((c) == SBD_CMD_STATUS) ? "STATUS" : "unknown")
101a3114836SGerry Liu
102a3114836SGerry Liu #define DR_GET_BOARD_DEVUNIT(sb, ut, un) (&((sb)->b_dev[DEVSET_NIX(ut)][un]))
103a3114836SGerry Liu
104a3114836SGerry Liu #define DR_MAKE_MINOR(i, b) (((i) << 16) | (b))
105a3114836SGerry Liu #define DR_MINOR2INST(m) (((m) >> 16) & 0xffff)
106a3114836SGerry Liu #define DR_MINOR2BNUM(m) ((m) & 0xffff)
107a3114836SGerry Liu
108a3114836SGerry Liu /* for the DR*INTERNAL_ERROR macros. see sys/dr.h. */
109a3114836SGerry Liu static char *dr_ie_fmt = "dr.c %d";
110a3114836SGerry Liu
111a3114836SGerry Liu /* struct for drmach device name to sbd_comp_type_t mapping */
112a3114836SGerry Liu typedef struct {
113a3114836SGerry Liu char *s_devtype;
114a3114836SGerry Liu sbd_comp_type_t s_nodetype;
115a3114836SGerry Liu } dr_devname_t;
116a3114836SGerry Liu
117a3114836SGerry Liu /* struct to map starfire device attributes - name:sbd_comp_type_t */
118a3114836SGerry Liu static dr_devname_t dr_devattr[] = {
119a3114836SGerry Liu { DRMACH_DEVTYPE_MEM, SBD_COMP_MEM },
120a3114836SGerry Liu { DRMACH_DEVTYPE_CPU, SBD_COMP_CPU },
121a3114836SGerry Liu { DRMACH_DEVTYPE_PCI, SBD_COMP_IO },
122a3114836SGerry Liu #if defined(DRMACH_DEVTYPE_SBUS)
123a3114836SGerry Liu { DRMACH_DEVTYPE_SBUS, SBD_COMP_IO },
124a3114836SGerry Liu #endif
125a3114836SGerry Liu #if defined(DRMACH_DEVTYPE_WCI)
126a3114836SGerry Liu { DRMACH_DEVTYPE_WCI, SBD_COMP_IO },
127a3114836SGerry Liu #endif
128a3114836SGerry Liu /* last s_devtype must be NULL, s_nodetype must be SBD_COMP_UNKNOWN */
129a3114836SGerry Liu { NULL, SBD_COMP_UNKNOWN }
130a3114836SGerry Liu };
131a3114836SGerry Liu
132a3114836SGerry Liu /*
133a3114836SGerry Liu * Per instance soft-state structure.
134a3114836SGerry Liu */
135a3114836SGerry Liu typedef struct dr_softstate {
136a3114836SGerry Liu dev_info_t *dip;
137a3114836SGerry Liu dr_board_t *boards;
138a3114836SGerry Liu kmutex_t i_lock;
139a3114836SGerry Liu int dr_initialized;
140a3114836SGerry Liu } dr_softstate_t;
141a3114836SGerry Liu
142a3114836SGerry Liu /*
143a3114836SGerry Liu * dr Global data elements
144a3114836SGerry Liu */
145a3114836SGerry Liu struct dr_global {
146a3114836SGerry Liu dr_softstate_t *softsp; /* pointer to initialize soft state */
147a3114836SGerry Liu kmutex_t lock;
148a3114836SGerry Liu } dr_g;
149a3114836SGerry Liu
150a3114836SGerry Liu dr_unsafe_devs_t dr_unsafe_devs;
151a3114836SGerry Liu
152a3114836SGerry Liu /*
153a3114836SGerry Liu * Table of known passthru commands.
154a3114836SGerry Liu */
155a3114836SGerry Liu struct {
156a3114836SGerry Liu char *pt_name;
157a3114836SGerry Liu int (*pt_func)(dr_handle_t *);
158a3114836SGerry Liu } pt_arr[] = {
159a3114836SGerry Liu "quiesce", dr_pt_test_suspend,
160a3114836SGerry Liu };
161a3114836SGerry Liu
162a3114836SGerry Liu int dr_modunload_okay = 0; /* set to non-zero to allow unload */
163a3114836SGerry Liu
164a3114836SGerry Liu /*
165a3114836SGerry Liu * State transition table. States valid transitions for "board" state.
166a3114836SGerry Liu * Recall that non-zero return value terminates operation, however
167a3114836SGerry Liu * the herrno value is what really indicates an error , if any.
168a3114836SGerry Liu */
169a3114836SGerry Liu static int
_cmd2index(int c)170a3114836SGerry Liu _cmd2index(int c)
171a3114836SGerry Liu {
172a3114836SGerry Liu /*
173a3114836SGerry Liu * Translate DR CMD to index into dr_state_transition.
174a3114836SGerry Liu */
175a3114836SGerry Liu switch (c) {
176a3114836SGerry Liu case SBD_CMD_CONNECT: return (0);
177a3114836SGerry Liu case SBD_CMD_DISCONNECT: return (1);
178a3114836SGerry Liu case SBD_CMD_CONFIGURE: return (2);
179a3114836SGerry Liu case SBD_CMD_UNCONFIGURE: return (3);
180a3114836SGerry Liu case SBD_CMD_ASSIGN: return (4);
181a3114836SGerry Liu case SBD_CMD_UNASSIGN: return (5);
182a3114836SGerry Liu case SBD_CMD_POWERON: return (6);
183a3114836SGerry Liu case SBD_CMD_POWEROFF: return (7);
184a3114836SGerry Liu case SBD_CMD_TEST: return (8);
185a3114836SGerry Liu default: return (-1);
186a3114836SGerry Liu }
187a3114836SGerry Liu }
188a3114836SGerry Liu
189a3114836SGerry Liu #define CMD2INDEX(c) _cmd2index(c)
190a3114836SGerry Liu
191a3114836SGerry Liu static struct dr_state_trans {
192a3114836SGerry Liu int x_cmd;
193a3114836SGerry Liu struct {
194a3114836SGerry Liu int x_rv; /* return value of pre_op */
195a3114836SGerry Liu int x_err; /* error, if any */
196a3114836SGerry Liu } x_op[DR_STATE_MAX];
197a3114836SGerry Liu } dr_state_transition[] = {
198a3114836SGerry Liu { SBD_CMD_CONNECT,
199a3114836SGerry Liu {
200a3114836SGerry Liu { 0, 0 }, /* empty */
201a3114836SGerry Liu { 0, 0 }, /* occupied */
202a3114836SGerry Liu { -1, ESBD_STATE }, /* connected */
203a3114836SGerry Liu { -1, ESBD_STATE }, /* unconfigured */
204a3114836SGerry Liu { -1, ESBD_STATE }, /* partial */
205a3114836SGerry Liu { -1, ESBD_STATE }, /* configured */
206a3114836SGerry Liu { -1, ESBD_STATE }, /* release */
207a3114836SGerry Liu { -1, ESBD_STATE }, /* unreferenced */
208a3114836SGerry Liu { -1, ESBD_FATAL_STATE }, /* fatal */
209a3114836SGerry Liu }
210a3114836SGerry Liu },
211a3114836SGerry Liu { SBD_CMD_DISCONNECT,
212a3114836SGerry Liu {
213a3114836SGerry Liu { -1, ESBD_STATE }, /* empty */
214a3114836SGerry Liu { 0, 0 }, /* occupied */
215a3114836SGerry Liu { 0, 0 }, /* connected */
216a3114836SGerry Liu { 0, 0 }, /* unconfigured */
217a3114836SGerry Liu { -1, ESBD_STATE }, /* partial */
218a3114836SGerry Liu { -1, ESBD_STATE }, /* configured */
219a3114836SGerry Liu { -1, ESBD_STATE }, /* release */
220a3114836SGerry Liu { -1, ESBD_STATE }, /* unreferenced */
221a3114836SGerry Liu { -1, ESBD_FATAL_STATE }, /* fatal */
222a3114836SGerry Liu }
223a3114836SGerry Liu },
224a3114836SGerry Liu { SBD_CMD_CONFIGURE,
225a3114836SGerry Liu {
226a3114836SGerry Liu { -1, ESBD_STATE }, /* empty */
227a3114836SGerry Liu { -1, ESBD_STATE }, /* occupied */
228a3114836SGerry Liu { 0, 0 }, /* connected */
229a3114836SGerry Liu { 0, 0 }, /* unconfigured */
230a3114836SGerry Liu { 0, 0 }, /* partial */
231a3114836SGerry Liu { 0, 0 }, /* configured */
232a3114836SGerry Liu { -1, ESBD_STATE }, /* release */
233a3114836SGerry Liu { -1, ESBD_STATE }, /* unreferenced */
234a3114836SGerry Liu { -1, ESBD_FATAL_STATE }, /* fatal */
235a3114836SGerry Liu }
236a3114836SGerry Liu },
237a3114836SGerry Liu { SBD_CMD_UNCONFIGURE,
238a3114836SGerry Liu {
239a3114836SGerry Liu { -1, ESBD_STATE }, /* empty */
240a3114836SGerry Liu { -1, ESBD_STATE }, /* occupied */
241a3114836SGerry Liu { -1, ESBD_STATE }, /* connected */
242a3114836SGerry Liu { -1, ESBD_STATE }, /* unconfigured */
243a3114836SGerry Liu { 0, 0 }, /* partial */
244a3114836SGerry Liu { 0, 0 }, /* configured */
245a3114836SGerry Liu { 0, 0 }, /* release */
246a3114836SGerry Liu { 0, 0 }, /* unreferenced */
247a3114836SGerry Liu { -1, ESBD_FATAL_STATE }, /* fatal */
248a3114836SGerry Liu }
249a3114836SGerry Liu },
250a3114836SGerry Liu { SBD_CMD_ASSIGN,
251a3114836SGerry Liu {
252a3114836SGerry Liu { 0, 0 }, /* empty */
253a3114836SGerry Liu { 0, 0 }, /* occupied */
254a3114836SGerry Liu { -1, ESBD_STATE }, /* connected */
255a3114836SGerry Liu { -1, ESBD_STATE }, /* unconfigured */
256a3114836SGerry Liu { -1, ESBD_STATE }, /* partial */
257a3114836SGerry Liu { -1, ESBD_STATE }, /* configured */
258a3114836SGerry Liu { -1, ESBD_STATE }, /* release */
259a3114836SGerry Liu { -1, ESBD_STATE }, /* unreferenced */
260a3114836SGerry Liu { -1, ESBD_FATAL_STATE }, /* fatal */
261a3114836SGerry Liu }
262a3114836SGerry Liu },
263a3114836SGerry Liu { SBD_CMD_UNASSIGN,
264a3114836SGerry Liu {
265a3114836SGerry Liu { 0, 0 }, /* empty */
266a3114836SGerry Liu { 0, 0 }, /* occupied */
267a3114836SGerry Liu { -1, ESBD_STATE }, /* connected */
268a3114836SGerry Liu { -1, ESBD_STATE }, /* unconfigured */
269a3114836SGerry Liu { -1, ESBD_STATE }, /* partial */
270a3114836SGerry Liu { -1, ESBD_STATE }, /* configured */
271a3114836SGerry Liu { -1, ESBD_STATE }, /* release */
272a3114836SGerry Liu { -1, ESBD_STATE }, /* unreferenced */
273a3114836SGerry Liu { -1, ESBD_FATAL_STATE }, /* fatal */
274a3114836SGerry Liu }
275a3114836SGerry Liu },
276a3114836SGerry Liu { SBD_CMD_POWERON,
277a3114836SGerry Liu {
278a3114836SGerry Liu { 0, 0 }, /* empty */
279a3114836SGerry Liu { 0, 0 }, /* occupied */
280a3114836SGerry Liu { -1, ESBD_STATE }, /* connected */
281a3114836SGerry Liu { -1, ESBD_STATE }, /* unconfigured */
282a3114836SGerry Liu { -1, ESBD_STATE }, /* partial */
283a3114836SGerry Liu { -1, ESBD_STATE }, /* configured */
284a3114836SGerry Liu { -1, ESBD_STATE }, /* release */
285a3114836SGerry Liu { -1, ESBD_STATE }, /* unreferenced */
286a3114836SGerry Liu { -1, ESBD_FATAL_STATE }, /* fatal */
287a3114836SGerry Liu }
288a3114836SGerry Liu },
289a3114836SGerry Liu { SBD_CMD_POWEROFF,
290a3114836SGerry Liu {
291a3114836SGerry Liu { 0, 0 }, /* empty */
292a3114836SGerry Liu { 0, 0 }, /* occupied */
293a3114836SGerry Liu { -1, ESBD_STATE }, /* connected */
294a3114836SGerry Liu { -1, ESBD_STATE }, /* unconfigured */
295a3114836SGerry Liu { -1, ESBD_STATE }, /* partial */
296a3114836SGerry Liu { -1, ESBD_STATE }, /* configured */
297a3114836SGerry Liu { -1, ESBD_STATE }, /* release */
298a3114836SGerry Liu { -1, ESBD_STATE }, /* unreferenced */
299a3114836SGerry Liu { -1, ESBD_FATAL_STATE }, /* fatal */
300a3114836SGerry Liu }
301a3114836SGerry Liu },
302a3114836SGerry Liu { SBD_CMD_TEST,
303a3114836SGerry Liu {
304a3114836SGerry Liu { 0, 0 }, /* empty */
305a3114836SGerry Liu { 0, 0 }, /* occupied */
306a3114836SGerry Liu { -1, ESBD_STATE }, /* connected */
307a3114836SGerry Liu { -1, ESBD_STATE }, /* unconfigured */
308a3114836SGerry Liu { -1, ESBD_STATE }, /* partial */
309a3114836SGerry Liu { -1, ESBD_STATE }, /* configured */
310a3114836SGerry Liu { -1, ESBD_STATE }, /* release */
311a3114836SGerry Liu { -1, ESBD_STATE }, /* unreferenced */
312a3114836SGerry Liu { -1, ESBD_FATAL_STATE }, /* fatal */
313a3114836SGerry Liu }
314a3114836SGerry Liu },
315a3114836SGerry Liu };
316a3114836SGerry Liu
317a3114836SGerry Liu /*
318a3114836SGerry Liu * Global R/W lock to synchronize access across
319a3114836SGerry Liu * multiple boards. Users wanting multi-board access
320a3114836SGerry Liu * must grab WRITE lock, others must grab READ lock.
321a3114836SGerry Liu */
322a3114836SGerry Liu krwlock_t dr_grwlock;
323a3114836SGerry Liu
324a3114836SGerry Liu /*
325a3114836SGerry Liu * Head of the boardlist used as a reference point for
326a3114836SGerry Liu * locating board structs.
327a3114836SGerry Liu * TODO: eliminate dr_boardlist
328a3114836SGerry Liu */
329a3114836SGerry Liu dr_board_t *dr_boardlist;
330a3114836SGerry Liu
331a3114836SGerry Liu /*
332a3114836SGerry Liu * DR support functions.
333a3114836SGerry Liu */
334a3114836SGerry Liu static dr_devset_t dr_dev2devset(sbd_comp_id_t *cid);
335a3114836SGerry Liu static int dr_check_transition(dr_board_t *bp,
336a3114836SGerry Liu dr_devset_t *devsetp,
337a3114836SGerry Liu struct dr_state_trans *transp,
338a3114836SGerry Liu int cmd);
339a3114836SGerry Liu static int dr_check_unit_attached(dr_common_unit_t *dp);
340a3114836SGerry Liu static sbd_error_t *dr_init_devlists(dr_board_t *bp);
341a3114836SGerry Liu static void dr_board_discovery(dr_board_t *bp);
342a3114836SGerry Liu static int dr_board_init(dr_board_t *bp, dev_info_t *dip, int bd);
343a3114836SGerry Liu static void dr_board_destroy(dr_board_t *bp);
344a3114836SGerry Liu static void dr_board_transition(dr_board_t *bp, dr_state_t st);
345a3114836SGerry Liu
346a3114836SGerry Liu /*
347a3114836SGerry Liu * DR driver (DDI) entry points.
348a3114836SGerry Liu */
349a3114836SGerry Liu static int dr_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd,
350a3114836SGerry Liu void *arg, void **result);
351a3114836SGerry Liu static int dr_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
352a3114836SGerry Liu static int dr_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
353a3114836SGerry Liu static int dr_probe(dev_info_t *dip);
354a3114836SGerry Liu static int dr_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
355a3114836SGerry Liu cred_t *cred_p, int *rval_p);
356a3114836SGerry Liu static int dr_close(dev_t dev, int flag, int otyp, cred_t *cred_p);
357a3114836SGerry Liu static int dr_open(dev_t *dev, int flag, int otyp, cred_t *cred_p);
358a3114836SGerry Liu
359a3114836SGerry Liu /*
360a3114836SGerry Liu * DR command processing operations.
361a3114836SGerry Liu */
362a3114836SGerry Liu static int dr_copyin_iocmd(dr_handle_t *hp);
363a3114836SGerry Liu static int dr_copyout_iocmd(dr_handle_t *hp);
364a3114836SGerry Liu static int dr_copyout_errs(dr_handle_t *hp);
365a3114836SGerry Liu static int dr_pre_op(dr_handle_t *hp);
366a3114836SGerry Liu static int dr_post_op(dr_handle_t *hp, int rv);
367a3114836SGerry Liu static int dr_exec_op(dr_handle_t *hp);
368a3114836SGerry Liu static void dr_assign_board(dr_handle_t *hp);
369a3114836SGerry Liu static void dr_unassign_board(dr_handle_t *hp);
370a3114836SGerry Liu static void dr_connect(dr_handle_t *hp);
371a3114836SGerry Liu static int dr_disconnect(dr_handle_t *hp);
372a3114836SGerry Liu static void dr_dev_configure(dr_handle_t *hp);
373a3114836SGerry Liu static void dr_dev_release(dr_handle_t *hp);
374a3114836SGerry Liu static int dr_dev_unconfigure(dr_handle_t *hp);
375a3114836SGerry Liu static void dr_dev_cancel(dr_handle_t *hp);
376a3114836SGerry Liu static int dr_dev_status(dr_handle_t *hp);
377a3114836SGerry Liu static int dr_get_ncm(dr_handle_t *hp);
378a3114836SGerry Liu static int dr_pt_ioctl(dr_handle_t *hp);
379a3114836SGerry Liu static void dr_poweron_board(dr_handle_t *hp);
380a3114836SGerry Liu static void dr_poweroff_board(dr_handle_t *hp);
381a3114836SGerry Liu static void dr_test_board(dr_handle_t *hp);
382a3114836SGerry Liu
383a3114836SGerry Liu /*
384a3114836SGerry Liu * Autoconfiguration data structures
385a3114836SGerry Liu */
386a3114836SGerry Liu struct cb_ops dr_cb_ops = {
387a3114836SGerry Liu dr_open, /* open */
388a3114836SGerry Liu dr_close, /* close */
389a3114836SGerry Liu nodev, /* strategy */
390a3114836SGerry Liu nodev, /* print */
391a3114836SGerry Liu nodev, /* dump */
392a3114836SGerry Liu nodev, /* read */
393a3114836SGerry Liu nodev, /* write */
394a3114836SGerry Liu dr_ioctl, /* ioctl */
395a3114836SGerry Liu nodev, /* devmap */
396a3114836SGerry Liu nodev, /* mmap */
397a3114836SGerry Liu nodev, /* segmap */
398a3114836SGerry Liu nochpoll, /* chpoll */
399a3114836SGerry Liu ddi_prop_op, /* cb_prop_op */
400a3114836SGerry Liu NULL, /* struct streamtab */
401a3114836SGerry Liu D_NEW | D_MP | D_MTSAFE, /* compatibility flags */
402a3114836SGerry Liu CB_REV, /* Rev */
403a3114836SGerry Liu nodev, /* cb_aread */
404a3114836SGerry Liu nodev /* cb_awrite */
405a3114836SGerry Liu };
406a3114836SGerry Liu
407a3114836SGerry Liu struct dev_ops dr_dev_ops = {
408a3114836SGerry Liu DEVO_REV, /* build version */
409a3114836SGerry Liu 0, /* dev ref count */
410a3114836SGerry Liu dr_getinfo, /* getinfo */
411a3114836SGerry Liu nulldev, /* identify */
412a3114836SGerry Liu dr_probe, /* probe */
413a3114836SGerry Liu dr_attach, /* attach */
414a3114836SGerry Liu dr_detach, /* detach */
415a3114836SGerry Liu nodev, /* reset */
416a3114836SGerry Liu &dr_cb_ops, /* cb_ops */
417a3114836SGerry Liu (struct bus_ops *)NULL, /* bus ops */
418a3114836SGerry Liu NULL, /* power */
419a3114836SGerry Liu ddi_quiesce_not_needed, /* quiesce */
420a3114836SGerry Liu };
421a3114836SGerry Liu
422a3114836SGerry Liu extern struct mod_ops mod_driverops;
423a3114836SGerry Liu
424a3114836SGerry Liu static struct modldrv modldrv = {
425a3114836SGerry Liu &mod_driverops,
426a3114836SGerry Liu "Dynamic Reconfiguration",
427a3114836SGerry Liu &dr_dev_ops
428a3114836SGerry Liu };
429a3114836SGerry Liu
430a3114836SGerry Liu static struct modlinkage modlinkage = {
431a3114836SGerry Liu MODREV_1,
432a3114836SGerry Liu (void *)&modldrv,
433a3114836SGerry Liu NULL
434a3114836SGerry Liu };
435a3114836SGerry Liu
436a3114836SGerry Liu /*
437a3114836SGerry Liu * Driver entry points.
438a3114836SGerry Liu */
439a3114836SGerry Liu int
_init(void)440a3114836SGerry Liu _init(void)
441a3114836SGerry Liu {
442a3114836SGerry Liu int err;
443a3114836SGerry Liu
444a3114836SGerry Liu /*
445a3114836SGerry Liu * If you need to support multiple nodes (instances), then
446a3114836SGerry Liu * whatever the maximum number of supported nodes is would
447a3114836SGerry Liu * need to passed as the third parameter to ddi_soft_state_init().
448a3114836SGerry Liu * Alternative would be to dynamically fini and re-init the
449a3114836SGerry Liu * soft state structure each time a node is attached.
450a3114836SGerry Liu */
451a3114836SGerry Liu err = ddi_soft_state_init((void **)&dr_g.softsp,
452a3114836SGerry Liu sizeof (dr_softstate_t), 1);
453a3114836SGerry Liu if (err)
454a3114836SGerry Liu return (err);
455a3114836SGerry Liu
456a3114836SGerry Liu mutex_init(&dr_g.lock, NULL, MUTEX_DRIVER, NULL);
457a3114836SGerry Liu rw_init(&dr_grwlock, NULL, RW_DEFAULT, NULL);
458a3114836SGerry Liu
459a3114836SGerry Liu return (mod_install(&modlinkage));
460a3114836SGerry Liu }
461a3114836SGerry Liu
462a3114836SGerry Liu int
_fini(void)463a3114836SGerry Liu _fini(void)
464a3114836SGerry Liu {
465a3114836SGerry Liu int err;
466a3114836SGerry Liu
467a3114836SGerry Liu if ((err = mod_remove(&modlinkage)) != 0)
468a3114836SGerry Liu return (err);
469a3114836SGerry Liu
470a3114836SGerry Liu mutex_destroy(&dr_g.lock);
471a3114836SGerry Liu rw_destroy(&dr_grwlock);
472a3114836SGerry Liu
473a3114836SGerry Liu ddi_soft_state_fini((void **)&dr_g.softsp);
474a3114836SGerry Liu
475a3114836SGerry Liu return (0);
476a3114836SGerry Liu }
477a3114836SGerry Liu
478a3114836SGerry Liu int
_info(struct modinfo * modinfop)479a3114836SGerry Liu _info(struct modinfo *modinfop)
480a3114836SGerry Liu {
481a3114836SGerry Liu return (mod_info(&modlinkage, modinfop));
482a3114836SGerry Liu }
483a3114836SGerry Liu
484a3114836SGerry Liu /*ARGSUSED1*/
485a3114836SGerry Liu static int
dr_open(dev_t * dev,int flag,int otyp,cred_t * cred_p)486a3114836SGerry Liu dr_open(dev_t *dev, int flag, int otyp, cred_t *cred_p)
487a3114836SGerry Liu {
488a3114836SGerry Liu int instance;
489a3114836SGerry Liu dr_softstate_t *softsp;
490a3114836SGerry Liu dr_board_t *bp;
491a3114836SGerry Liu
492a3114836SGerry Liu /*
493a3114836SGerry Liu * Don't open unless we've attached.
494a3114836SGerry Liu */
495a3114836SGerry Liu instance = DR_MINOR2INST(getminor(*dev));
496a3114836SGerry Liu softsp = ddi_get_soft_state(dr_g.softsp, instance);
497a3114836SGerry Liu if (softsp == NULL)
498a3114836SGerry Liu return (ENXIO);
499a3114836SGerry Liu
500a3114836SGerry Liu mutex_enter(&softsp->i_lock);
501a3114836SGerry Liu if (!softsp->dr_initialized) {
502a3114836SGerry Liu int bd;
503a3114836SGerry Liu int rv = 0;
504a3114836SGerry Liu
505a3114836SGerry Liu bp = softsp->boards;
506a3114836SGerry Liu
507a3114836SGerry Liu /* initialize each array element */
508a3114836SGerry Liu for (bd = 0; bd < MAX_BOARDS; bd++, bp++) {
509a3114836SGerry Liu rv = dr_board_init(bp, softsp->dip, bd);
510a3114836SGerry Liu if (rv)
511a3114836SGerry Liu break;
512a3114836SGerry Liu }
513a3114836SGerry Liu
514a3114836SGerry Liu if (rv == 0) {
515a3114836SGerry Liu softsp->dr_initialized = 1;
516a3114836SGerry Liu } else {
517a3114836SGerry Liu /* destroy elements initialized thus far */
518a3114836SGerry Liu while (--bp >= softsp->boards)
519a3114836SGerry Liu dr_board_destroy(bp);
520a3114836SGerry Liu
521a3114836SGerry Liu /* TODO: should this be another errno val ? */
522a3114836SGerry Liu mutex_exit(&softsp->i_lock);
523a3114836SGerry Liu return (ENXIO);
524a3114836SGerry Liu }
525a3114836SGerry Liu }
526a3114836SGerry Liu mutex_exit(&softsp->i_lock);
527a3114836SGerry Liu
528a3114836SGerry Liu bp = &softsp->boards[DR_MINOR2BNUM(getminor(*dev))];
529a3114836SGerry Liu
530a3114836SGerry Liu /*
531a3114836SGerry Liu * prevent opening of a dyn-ap for a board
532a3114836SGerry Liu * that does not exist
533a3114836SGerry Liu */
534a3114836SGerry Liu if (!bp->b_assigned) {
535a3114836SGerry Liu if (drmach_board_lookup(bp->b_num, &bp->b_id) != 0)
536a3114836SGerry Liu return (ENODEV);
537a3114836SGerry Liu }
538a3114836SGerry Liu
539a3114836SGerry Liu return (0);
540a3114836SGerry Liu }
541a3114836SGerry Liu
542a3114836SGerry Liu /*ARGSUSED*/
543a3114836SGerry Liu static int
dr_close(dev_t dev,int flag,int otyp,cred_t * cred_p)544a3114836SGerry Liu dr_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
545a3114836SGerry Liu {
546a3114836SGerry Liu return (0);
547a3114836SGerry Liu }
548a3114836SGerry Liu
549a3114836SGerry Liu /*
550a3114836SGerry Liu * Enable/disable DR features.
551a3114836SGerry Liu */
552a3114836SGerry Liu int dr_enable = 1;
553a3114836SGerry Liu
554a3114836SGerry Liu /*ARGSUSED3*/
555a3114836SGerry Liu static int
dr_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * cred_p,int * rval_p)556a3114836SGerry Liu dr_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
557a3114836SGerry Liu cred_t *cred_p, int *rval_p)
558a3114836SGerry Liu {
559a3114836SGerry Liu int rv = 0;
560a3114836SGerry Liu int instance;
561a3114836SGerry Liu int bd;
562a3114836SGerry Liu dr_handle_t *hp;
563a3114836SGerry Liu dr_softstate_t *softsp;
564a3114836SGerry Liu static fn_t f = "dr_ioctl";
565a3114836SGerry Liu
566a3114836SGerry Liu PR_ALL("%s...\n", f);
567a3114836SGerry Liu
568a3114836SGerry Liu instance = DR_MINOR2INST(getminor(dev));
569a3114836SGerry Liu softsp = ddi_get_soft_state(dr_g.softsp, instance);
570a3114836SGerry Liu if (softsp == NULL) {
571a3114836SGerry Liu cmn_err(CE_WARN, "dr%d: module not yet attached", instance);
572a3114836SGerry Liu return (ENXIO);
573a3114836SGerry Liu }
574a3114836SGerry Liu
575a3114836SGerry Liu if (!dr_enable) {
576a3114836SGerry Liu switch (cmd) {
577a3114836SGerry Liu case SBD_CMD_STATUS:
578a3114836SGerry Liu case SBD_CMD_GETNCM:
579a3114836SGerry Liu case SBD_CMD_PASSTHRU:
580a3114836SGerry Liu break;
581a3114836SGerry Liu default:
582a3114836SGerry Liu return (ENOTSUP);
583a3114836SGerry Liu }
584a3114836SGerry Liu }
585a3114836SGerry Liu
586a3114836SGerry Liu bd = DR_MINOR2BNUM(getminor(dev));
587a3114836SGerry Liu if (bd >= MAX_BOARDS)
588a3114836SGerry Liu return (ENXIO);
589a3114836SGerry Liu
590a3114836SGerry Liu /* get and initialize storage for new handle */
591a3114836SGerry Liu hp = GETSTRUCT(dr_handle_t, 1);
592a3114836SGerry Liu hp->h_bd = &softsp->boards[bd];
593a3114836SGerry Liu hp->h_err = NULL;
594a3114836SGerry Liu hp->h_dev = getminor(dev);
595a3114836SGerry Liu hp->h_cmd = cmd;
596a3114836SGerry Liu hp->h_mode = mode;
597a3114836SGerry Liu hp->h_iap = (sbd_ioctl_arg_t *)arg;
598a3114836SGerry Liu
599a3114836SGerry Liu /* copy sbd command into handle */
600a3114836SGerry Liu rv = dr_copyin_iocmd(hp);
601a3114836SGerry Liu if (rv) {
602a3114836SGerry Liu FREESTRUCT(hp, dr_handle_t, 1);
603a3114836SGerry Liu return (EINVAL);
604a3114836SGerry Liu }
605a3114836SGerry Liu
606a3114836SGerry Liu /* translate canonical name to component type */
607a3114836SGerry Liu if (hp->h_sbdcmd.cmd_cm.c_id.c_name[0] != '\0') {
608a3114836SGerry Liu hp->h_sbdcmd.cmd_cm.c_id.c_type =
609a3114836SGerry Liu dr_dev_type_to_nt(hp->h_sbdcmd.cmd_cm.c_id.c_name);
610a3114836SGerry Liu
611a3114836SGerry Liu PR_ALL("%s: c_name = %s, c_type = %d\n",
612a3114836SGerry Liu f,
613a3114836SGerry Liu hp->h_sbdcmd.cmd_cm.c_id.c_name,
614a3114836SGerry Liu hp->h_sbdcmd.cmd_cm.c_id.c_type);
615a3114836SGerry Liu } else {
616a3114836SGerry Liu /*EMPTY*/
617a3114836SGerry Liu PR_ALL("%s: c_name is NULL\n", f);
618a3114836SGerry Liu }
619a3114836SGerry Liu
620a3114836SGerry Liu /* determine scope of operation */
621a3114836SGerry Liu hp->h_devset = dr_dev2devset(&hp->h_sbdcmd.cmd_cm.c_id);
622a3114836SGerry Liu
623a3114836SGerry Liu switch (hp->h_cmd) {
624a3114836SGerry Liu case SBD_CMD_STATUS:
625a3114836SGerry Liu case SBD_CMD_GETNCM:
626a3114836SGerry Liu /* no locks needed for these commands */
627a3114836SGerry Liu break;
628a3114836SGerry Liu
629a3114836SGerry Liu default:
630a3114836SGerry Liu rw_enter(&dr_grwlock, RW_WRITER);
631a3114836SGerry Liu mutex_enter(&hp->h_bd->b_lock);
632a3114836SGerry Liu
633a3114836SGerry Liu /*
634a3114836SGerry Liu * If we're dealing with memory at all, then we have
635a3114836SGerry Liu * to keep the "exclusive" global lock held. This is
636a3114836SGerry Liu * necessary since we will probably need to look at
637a3114836SGerry Liu * multiple board structs. Otherwise, we only have
638a3114836SGerry Liu * to deal with the board in question and so can drop
639a3114836SGerry Liu * the global lock to "shared".
640a3114836SGerry Liu */
641a3114836SGerry Liu rv = DEVSET_IN_SET(hp->h_devset, SBD_COMP_MEM, DEVSET_ANYUNIT);
642a3114836SGerry Liu if (rv == 0)
643a3114836SGerry Liu rw_downgrade(&dr_grwlock);
644a3114836SGerry Liu break;
645a3114836SGerry Liu }
646a3114836SGerry Liu rv = 0;
647a3114836SGerry Liu
648a3114836SGerry Liu if (rv == 0)
649a3114836SGerry Liu rv = dr_pre_op(hp);
650a3114836SGerry Liu if (rv == 0) {
651a3114836SGerry Liu rv = dr_exec_op(hp);
652a3114836SGerry Liu rv = dr_post_op(hp, rv);
653a3114836SGerry Liu }
654a3114836SGerry Liu
655a3114836SGerry Liu if (rv == -1)
656a3114836SGerry Liu rv = EIO;
657a3114836SGerry Liu
658a3114836SGerry Liu if (hp->h_err != NULL)
659a3114836SGerry Liu if (!(rv = dr_copyout_errs(hp)))
660a3114836SGerry Liu rv = EIO;
661a3114836SGerry Liu
662a3114836SGerry Liu /* undo locking, if any, done before dr_pre_op */
663a3114836SGerry Liu switch (hp->h_cmd) {
664a3114836SGerry Liu case SBD_CMD_STATUS:
665a3114836SGerry Liu case SBD_CMD_GETNCM:
666a3114836SGerry Liu break;
667a3114836SGerry Liu
668a3114836SGerry Liu case SBD_CMD_ASSIGN:
669a3114836SGerry Liu case SBD_CMD_UNASSIGN:
670a3114836SGerry Liu case SBD_CMD_POWERON:
671a3114836SGerry Liu case SBD_CMD_POWEROFF:
672a3114836SGerry Liu case SBD_CMD_CONNECT:
673a3114836SGerry Liu case SBD_CMD_CONFIGURE:
674a3114836SGerry Liu case SBD_CMD_UNCONFIGURE:
675a3114836SGerry Liu case SBD_CMD_DISCONNECT:
676a3114836SGerry Liu /* Board changed state. Log a sysevent. */
677a3114836SGerry Liu if (rv == 0)
678a3114836SGerry Liu (void) drmach_log_sysevent(hp->h_bd->b_num, "",
679a3114836SGerry Liu SE_SLEEP, 0);
680a3114836SGerry Liu /* Fall through */
681a3114836SGerry Liu
682a3114836SGerry Liu default:
683a3114836SGerry Liu mutex_exit(&hp->h_bd->b_lock);
684a3114836SGerry Liu rw_exit(&dr_grwlock);
685a3114836SGerry Liu }
686a3114836SGerry Liu
687a3114836SGerry Liu if (hp->h_opts.size != 0)
688a3114836SGerry Liu FREESTRUCT(hp->h_opts.copts, char, hp->h_opts.size);
689a3114836SGerry Liu
690a3114836SGerry Liu FREESTRUCT(hp, dr_handle_t, 1);
691a3114836SGerry Liu
692a3114836SGerry Liu return (rv);
693a3114836SGerry Liu }
694a3114836SGerry Liu
695a3114836SGerry Liu /*ARGSUSED*/
696a3114836SGerry Liu static int
dr_probe(dev_info_t * dip)697a3114836SGerry Liu dr_probe(dev_info_t *dip)
698a3114836SGerry Liu {
699a3114836SGerry Liu return (DDI_PROBE_SUCCESS);
700a3114836SGerry Liu }
701a3114836SGerry Liu
702a3114836SGerry Liu static int
dr_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)703a3114836SGerry Liu dr_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
704a3114836SGerry Liu {
705a3114836SGerry Liu int rv, rv2;
706a3114836SGerry Liu int bd;
707a3114836SGerry Liu int instance;
708a3114836SGerry Liu sbd_error_t *err;
709a3114836SGerry Liu dr_softstate_t *softsp;
710a3114836SGerry Liu
711a3114836SGerry Liu instance = ddi_get_instance(dip);
712a3114836SGerry Liu
713a3114836SGerry Liu switch (cmd) {
714a3114836SGerry Liu case DDI_ATTACH:
715a3114836SGerry Liu rw_enter(&dr_grwlock, RW_WRITER);
716a3114836SGerry Liu
717a3114836SGerry Liu rv = ddi_soft_state_zalloc(dr_g.softsp, instance);
718a3114836SGerry Liu if (rv != DDI_SUCCESS) {
719a3114836SGerry Liu cmn_err(CE_WARN, "dr%d: failed to alloc soft-state",
720a3114836SGerry Liu instance);
721a3114836SGerry Liu return (DDI_FAILURE);
722a3114836SGerry Liu }
723a3114836SGerry Liu
724a3114836SGerry Liu /* initialize softstate structure */
725a3114836SGerry Liu softsp = ddi_get_soft_state(dr_g.softsp, instance);
726a3114836SGerry Liu softsp->dip = dip;
727a3114836SGerry Liu
728a3114836SGerry Liu mutex_init(&softsp->i_lock, NULL, MUTEX_DRIVER, NULL);
729a3114836SGerry Liu
730a3114836SGerry Liu /* allocate board array (aka boardlist) */
731a3114836SGerry Liu softsp->boards = GETSTRUCT(dr_board_t, MAX_BOARDS);
732a3114836SGerry Liu
733a3114836SGerry Liu /* TODO: eliminate dr_boardlist */
734a3114836SGerry Liu dr_boardlist = softsp->boards;
735a3114836SGerry Liu
736a3114836SGerry Liu /* initialize each array element */
737a3114836SGerry Liu rv = DDI_SUCCESS;
738a3114836SGerry Liu for (bd = 0; bd < MAX_BOARDS; bd++) {
739a3114836SGerry Liu dr_board_t *bp = &softsp->boards[bd];
740a3114836SGerry Liu char *p, *name;
741a3114836SGerry Liu int l, minor_num;
742a3114836SGerry Liu
743a3114836SGerry Liu /*
744a3114836SGerry Liu * initialized board attachment point path
745a3114836SGerry Liu * (relative to pseudo) in a form immediately
746a3114836SGerry Liu * reusable as an cfgadm command argument.
747a3114836SGerry Liu * TODO: clean this up
748a3114836SGerry Liu */
749a3114836SGerry Liu p = bp->b_path;
750a3114836SGerry Liu l = sizeof (bp->b_path);
751a3114836SGerry Liu (void) snprintf(p, l, "dr@%d:", instance);
752a3114836SGerry Liu while (*p != '\0') {
753a3114836SGerry Liu l--;
754a3114836SGerry Liu p++;
755a3114836SGerry Liu }
756a3114836SGerry Liu
757a3114836SGerry Liu name = p;
758a3114836SGerry Liu err = drmach_board_name(bd, p, l);
759a3114836SGerry Liu if (err) {
760a3114836SGerry Liu sbd_err_clear(&err);
761a3114836SGerry Liu rv = DDI_FAILURE;
762a3114836SGerry Liu break;
763a3114836SGerry Liu }
764a3114836SGerry Liu
765a3114836SGerry Liu minor_num = DR_MAKE_MINOR(instance, bd);
766a3114836SGerry Liu rv = ddi_create_minor_node(dip, name, S_IFCHR,
767a3114836SGerry Liu minor_num, DDI_NT_SBD_ATTACHMENT_POINT, NULL);
768a3114836SGerry Liu if (rv != DDI_SUCCESS)
769a3114836SGerry Liu rv = DDI_FAILURE;
770a3114836SGerry Liu }
771a3114836SGerry Liu
772a3114836SGerry Liu if (rv == DDI_SUCCESS) {
773a3114836SGerry Liu /*
774a3114836SGerry Liu * Announce the node's presence.
775a3114836SGerry Liu */
776a3114836SGerry Liu ddi_report_dev(dip);
777a3114836SGerry Liu } else {
778a3114836SGerry Liu ddi_remove_minor_node(dip, NULL);
779a3114836SGerry Liu }
780a3114836SGerry Liu /*
781a3114836SGerry Liu * Init registered unsafe devs.
782a3114836SGerry Liu */
783a3114836SGerry Liu dr_unsafe_devs.devnames = NULL;
784a3114836SGerry Liu rv2 = ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip,
785a3114836SGerry Liu DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
786a3114836SGerry Liu "unsupported-io-drivers", &dr_unsafe_devs.devnames,
787a3114836SGerry Liu &dr_unsafe_devs.ndevs);
788a3114836SGerry Liu
789a3114836SGerry Liu if (rv2 != DDI_PROP_SUCCESS)
790a3114836SGerry Liu dr_unsafe_devs.ndevs = 0;
791a3114836SGerry Liu
792a3114836SGerry Liu rw_exit(&dr_grwlock);
793a3114836SGerry Liu return (rv);
794a3114836SGerry Liu
795a3114836SGerry Liu default:
796a3114836SGerry Liu return (DDI_FAILURE);
797a3114836SGerry Liu }
798a3114836SGerry Liu
799a3114836SGerry Liu /*NOTREACHED*/
800a3114836SGerry Liu }
801a3114836SGerry Liu
802a3114836SGerry Liu static int
dr_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)803a3114836SGerry Liu dr_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
804a3114836SGerry Liu {
805a3114836SGerry Liu int instance;
806a3114836SGerry Liu dr_softstate_t *softsp;
807a3114836SGerry Liu
808a3114836SGerry Liu switch (cmd) {
809a3114836SGerry Liu case DDI_DETACH:
810a3114836SGerry Liu if (!dr_modunload_okay)
811a3114836SGerry Liu return (DDI_FAILURE);
812a3114836SGerry Liu
813a3114836SGerry Liu rw_enter(&dr_grwlock, RW_WRITER);
814a3114836SGerry Liu
815a3114836SGerry Liu instance = ddi_get_instance(dip);
816a3114836SGerry Liu softsp = ddi_get_soft_state(dr_g.softsp, instance);
817a3114836SGerry Liu
818a3114836SGerry Liu /* TODO: eliminate dr_boardlist */
819a3114836SGerry Liu ASSERT(softsp->boards == dr_boardlist);
820a3114836SGerry Liu
821a3114836SGerry Liu /* remove all minor nodes */
822a3114836SGerry Liu ddi_remove_minor_node(dip, NULL);
823a3114836SGerry Liu
824a3114836SGerry Liu if (softsp->dr_initialized) {
825a3114836SGerry Liu int bd;
826a3114836SGerry Liu
827a3114836SGerry Liu for (bd = 0; bd < MAX_BOARDS; bd++)
828a3114836SGerry Liu dr_board_destroy(&softsp->boards[bd]);
829a3114836SGerry Liu }
830a3114836SGerry Liu
831a3114836SGerry Liu FREESTRUCT(softsp->boards, dr_board_t, MAX_BOARDS);
832a3114836SGerry Liu mutex_destroy(&softsp->i_lock);
833a3114836SGerry Liu ddi_soft_state_free(dr_g.softsp, instance);
834a3114836SGerry Liu
835a3114836SGerry Liu rw_exit(&dr_grwlock);
836a3114836SGerry Liu return (DDI_SUCCESS);
837a3114836SGerry Liu
838a3114836SGerry Liu default:
839a3114836SGerry Liu return (DDI_FAILURE);
840a3114836SGerry Liu }
841a3114836SGerry Liu /*NOTREACHED*/
842a3114836SGerry Liu }
843a3114836SGerry Liu
844a3114836SGerry Liu static int
dr_getinfo(dev_info_t * dip,ddi_info_cmd_t cmd,void * arg,void ** result)845a3114836SGerry Liu dr_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
846a3114836SGerry Liu {
847a3114836SGerry Liu _NOTE(ARGUNUSED(dip))
848a3114836SGerry Liu
849a3114836SGerry Liu dev_t dev = (dev_t)arg;
850a3114836SGerry Liu int instance, error;
851a3114836SGerry Liu dr_softstate_t *softsp;
852a3114836SGerry Liu
853a3114836SGerry Liu *result = NULL;
854a3114836SGerry Liu error = DDI_SUCCESS;
855a3114836SGerry Liu instance = DR_MINOR2INST(getminor(dev));
856a3114836SGerry Liu
857a3114836SGerry Liu switch (cmd) {
858a3114836SGerry Liu case DDI_INFO_DEVT2DEVINFO:
859a3114836SGerry Liu softsp = ddi_get_soft_state(dr_g.softsp, instance);
860a3114836SGerry Liu if (softsp == NULL)
861a3114836SGerry Liu return (DDI_FAILURE);
862a3114836SGerry Liu *result = (void *)softsp->dip;
863a3114836SGerry Liu break;
864a3114836SGerry Liu
865a3114836SGerry Liu case DDI_INFO_DEVT2INSTANCE:
866a3114836SGerry Liu *result = (void *)(uintptr_t)instance;
867a3114836SGerry Liu break;
868a3114836SGerry Liu
869a3114836SGerry Liu default:
870a3114836SGerry Liu error = DDI_FAILURE;
871a3114836SGerry Liu break;
872a3114836SGerry Liu }
873a3114836SGerry Liu
874a3114836SGerry Liu return (error);
875a3114836SGerry Liu }
876a3114836SGerry Liu
877a3114836SGerry Liu /*
878a3114836SGerry Liu * DR operations.
879a3114836SGerry Liu */
880a3114836SGerry Liu
881a3114836SGerry Liu static int
dr_copyin_iocmd(dr_handle_t * hp)882a3114836SGerry Liu dr_copyin_iocmd(dr_handle_t *hp)
883a3114836SGerry Liu {
884a3114836SGerry Liu static fn_t f = "dr_copyin_iocmd";
885a3114836SGerry Liu sbd_cmd_t *scp = &hp->h_sbdcmd;
886a3114836SGerry Liu
887a3114836SGerry Liu if (hp->h_iap == NULL)
888a3114836SGerry Liu return (EINVAL);
889a3114836SGerry Liu
890a3114836SGerry Liu bzero((caddr_t)scp, sizeof (sbd_cmd_t));
891a3114836SGerry Liu
892a3114836SGerry Liu #ifdef _MULTI_DATAMODEL
893a3114836SGerry Liu if (ddi_model_convert_from(hp->h_mode & FMODELS) == DDI_MODEL_ILP32) {
894a3114836SGerry Liu sbd_cmd32_t scmd32;
895a3114836SGerry Liu
896a3114836SGerry Liu bzero((caddr_t)&scmd32, sizeof (sbd_cmd32_t));
897a3114836SGerry Liu
898a3114836SGerry Liu if (ddi_copyin((void *)hp->h_iap, (void *)&scmd32,
899a3114836SGerry Liu sizeof (sbd_cmd32_t), hp->h_mode)) {
900a3114836SGerry Liu cmn_err(CE_WARN,
901a3114836SGerry Liu "%s: (32bit) failed to copyin "
902a3114836SGerry Liu "sbdcmd-struct", f);
903a3114836SGerry Liu return (EFAULT);
904a3114836SGerry Liu }
905a3114836SGerry Liu scp->cmd_cm.c_id.c_type = scmd32.cmd_cm.c_id.c_type;
906a3114836SGerry Liu scp->cmd_cm.c_id.c_unit = scmd32.cmd_cm.c_id.c_unit;
907a3114836SGerry Liu bcopy(&scmd32.cmd_cm.c_id.c_name[0],
908a3114836SGerry Liu &scp->cmd_cm.c_id.c_name[0], OBP_MAXPROPNAME);
909a3114836SGerry Liu scp->cmd_cm.c_flags = scmd32.cmd_cm.c_flags;
910a3114836SGerry Liu scp->cmd_cm.c_len = scmd32.cmd_cm.c_len;
911a3114836SGerry Liu scp->cmd_cm.c_opts = (caddr_t)(uintptr_t)scmd32.cmd_cm.c_opts;
912a3114836SGerry Liu
913a3114836SGerry Liu switch (hp->h_cmd) {
914a3114836SGerry Liu case SBD_CMD_STATUS:
915a3114836SGerry Liu scp->cmd_stat.s_nbytes = scmd32.cmd_stat.s_nbytes;
916a3114836SGerry Liu scp->cmd_stat.s_statp =
917a3114836SGerry Liu (caddr_t)(uintptr_t)scmd32.cmd_stat.s_statp;
918a3114836SGerry Liu break;
919a3114836SGerry Liu default:
920a3114836SGerry Liu break;
921a3114836SGerry Liu
922a3114836SGerry Liu }
923a3114836SGerry Liu } else
924a3114836SGerry Liu #endif /* _MULTI_DATAMODEL */
925a3114836SGerry Liu if (ddi_copyin((void *)hp->h_iap, (void *)scp,
926a3114836SGerry Liu sizeof (sbd_cmd_t), hp->h_mode) != 0) {
927a3114836SGerry Liu cmn_err(CE_WARN,
928a3114836SGerry Liu "%s: failed to copyin sbdcmd-struct", f);
929a3114836SGerry Liu return (EFAULT);
930a3114836SGerry Liu }
931a3114836SGerry Liu
932a3114836SGerry Liu if ((hp->h_opts.size = scp->cmd_cm.c_len) != 0) {
933a3114836SGerry Liu hp->h_opts.copts = GETSTRUCT(char, scp->cmd_cm.c_len + 1);
934a3114836SGerry Liu ++hp->h_opts.size;
935a3114836SGerry Liu if (ddi_copyin((void *)scp->cmd_cm.c_opts,
936a3114836SGerry Liu (void *)hp->h_opts.copts,
937a3114836SGerry Liu scp->cmd_cm.c_len, hp->h_mode) != 0) {
938a3114836SGerry Liu cmn_err(CE_WARN, "%s: failed to copyin options", f);
939a3114836SGerry Liu return (EFAULT);
940a3114836SGerry Liu }
941a3114836SGerry Liu }
942a3114836SGerry Liu
943a3114836SGerry Liu return (0);
944a3114836SGerry Liu }
945a3114836SGerry Liu
946a3114836SGerry Liu static int
dr_copyout_iocmd(dr_handle_t * hp)947a3114836SGerry Liu dr_copyout_iocmd(dr_handle_t *hp)
948a3114836SGerry Liu {
949a3114836SGerry Liu static fn_t f = "dr_copyout_iocmd";
950a3114836SGerry Liu sbd_cmd_t *scp = &hp->h_sbdcmd;
951a3114836SGerry Liu
952a3114836SGerry Liu if (hp->h_iap == NULL)
953a3114836SGerry Liu return (EINVAL);
954a3114836SGerry Liu
955a3114836SGerry Liu #ifdef _MULTI_DATAMODEL
956a3114836SGerry Liu if (ddi_model_convert_from(hp->h_mode & FMODELS) == DDI_MODEL_ILP32) {
957a3114836SGerry Liu sbd_cmd32_t scmd32;
958a3114836SGerry Liu
959a3114836SGerry Liu scmd32.cmd_cm.c_id.c_type = scp->cmd_cm.c_id.c_type;
960a3114836SGerry Liu scmd32.cmd_cm.c_id.c_unit = scp->cmd_cm.c_id.c_unit;
961a3114836SGerry Liu bcopy(&scp->cmd_cm.c_id.c_name[0],
962a3114836SGerry Liu &scmd32.cmd_cm.c_id.c_name[0], OBP_MAXPROPNAME);
963a3114836SGerry Liu
964a3114836SGerry Liu scmd32.cmd_cm.c_flags = scp->cmd_cm.c_flags;
965a3114836SGerry Liu scmd32.cmd_cm.c_len = scp->cmd_cm.c_len;
966a3114836SGerry Liu scmd32.cmd_cm.c_opts = (caddr32_t)(uintptr_t)scp->cmd_cm.c_opts;
967a3114836SGerry Liu
968a3114836SGerry Liu switch (hp->h_cmd) {
969a3114836SGerry Liu case SBD_CMD_GETNCM:
970a3114836SGerry Liu scmd32.cmd_getncm.g_ncm = scp->cmd_getncm.g_ncm;
971a3114836SGerry Liu break;
972a3114836SGerry Liu default:
973a3114836SGerry Liu break;
974a3114836SGerry Liu }
975a3114836SGerry Liu
976a3114836SGerry Liu if (ddi_copyout((void *)&scmd32, (void *)hp->h_iap,
977a3114836SGerry Liu sizeof (sbd_cmd32_t), hp->h_mode)) {
978a3114836SGerry Liu cmn_err(CE_WARN,
979a3114836SGerry Liu "%s: (32bit) failed to copyout "
980a3114836SGerry Liu "sbdcmd-struct", f);
981a3114836SGerry Liu return (EFAULT);
982a3114836SGerry Liu }
983a3114836SGerry Liu } else
984a3114836SGerry Liu #endif /* _MULTI_DATAMODEL */
985a3114836SGerry Liu if (ddi_copyout((void *)scp, (void *)hp->h_iap,
986a3114836SGerry Liu sizeof (sbd_cmd_t), hp->h_mode) != 0) {
987a3114836SGerry Liu cmn_err(CE_WARN,
988a3114836SGerry Liu "%s: failed to copyout sbdcmd-struct", f);
989a3114836SGerry Liu return (EFAULT);
990a3114836SGerry Liu }
991a3114836SGerry Liu
992a3114836SGerry Liu return (0);
993a3114836SGerry Liu }
994a3114836SGerry Liu
995a3114836SGerry Liu static int
dr_copyout_errs(dr_handle_t * hp)996a3114836SGerry Liu dr_copyout_errs(dr_handle_t *hp)
997a3114836SGerry Liu {
998a3114836SGerry Liu static fn_t f = "dr_copyout_errs";
999a3114836SGerry Liu
1000a3114836SGerry Liu if (hp->h_err == NULL)
1001a3114836SGerry Liu return (0);
1002a3114836SGerry Liu
1003a3114836SGerry Liu if (hp->h_err->e_code) {
1004a3114836SGerry Liu PR_ALL("%s: error %d %s",
1005a3114836SGerry Liu f, hp->h_err->e_code, hp->h_err->e_rsc);
1006a3114836SGerry Liu }
1007a3114836SGerry Liu
1008a3114836SGerry Liu #ifdef _MULTI_DATAMODEL
1009a3114836SGerry Liu if (ddi_model_convert_from(hp->h_mode & FMODELS) == DDI_MODEL_ILP32) {
1010a3114836SGerry Liu sbd_error32_t *serr32p;
1011a3114836SGerry Liu
1012a3114836SGerry Liu serr32p = GETSTRUCT(sbd_error32_t, 1);
1013a3114836SGerry Liu
1014a3114836SGerry Liu serr32p->e_code = hp->h_err->e_code;
1015a3114836SGerry Liu bcopy(&hp->h_err->e_rsc[0], &serr32p->e_rsc[0],
1016a3114836SGerry Liu MAXPATHLEN);
1017a3114836SGerry Liu if (ddi_copyout((void *)serr32p,
1018a3114836SGerry Liu (void *)&((sbd_ioctl_arg32_t *)hp->h_iap)->i_err,
1019a3114836SGerry Liu sizeof (sbd_error32_t), hp->h_mode)) {
1020a3114836SGerry Liu cmn_err(CE_WARN,
1021a3114836SGerry Liu "%s: (32bit) failed to copyout", f);
1022a3114836SGerry Liu return (EFAULT);
1023a3114836SGerry Liu }
1024a3114836SGerry Liu FREESTRUCT(serr32p, sbd_error32_t, 1);
1025a3114836SGerry Liu } else
1026a3114836SGerry Liu #endif /* _MULTI_DATAMODEL */
1027a3114836SGerry Liu if (ddi_copyout((void *)hp->h_err,
1028a3114836SGerry Liu (void *)&hp->h_iap->i_err,
1029a3114836SGerry Liu sizeof (sbd_error_t), hp->h_mode)) {
1030a3114836SGerry Liu cmn_err(CE_WARN,
1031a3114836SGerry Liu "%s: failed to copyout", f);
1032a3114836SGerry Liu return (EFAULT);
1033a3114836SGerry Liu }
1034a3114836SGerry Liu
1035a3114836SGerry Liu sbd_err_clear(&hp->h_err);
1036a3114836SGerry Liu
1037a3114836SGerry Liu return (0);
1038a3114836SGerry Liu
1039a3114836SGerry Liu }
1040a3114836SGerry Liu
1041a3114836SGerry Liu /*
1042a3114836SGerry Liu * pre-op entry point must sbd_err_set_c(), if needed.
1043a3114836SGerry Liu * Return value of non-zero indicates failure.
1044a3114836SGerry Liu */
1045a3114836SGerry Liu static int
dr_pre_op(dr_handle_t * hp)1046a3114836SGerry Liu dr_pre_op(dr_handle_t *hp)
1047a3114836SGerry Liu {
1048a3114836SGerry Liu int rv = 0, t;
1049a3114836SGerry Liu int cmd, serr = 0;
1050a3114836SGerry Liu dr_devset_t devset;
1051a3114836SGerry Liu dr_board_t *bp = hp->h_bd;
1052a3114836SGerry Liu dr_handle_t *shp = hp;
1053a3114836SGerry Liu static fn_t f = "dr_pre_op";
1054a3114836SGerry Liu
1055a3114836SGerry Liu cmd = hp->h_cmd;
1056a3114836SGerry Liu devset = shp->h_devset;
1057a3114836SGerry Liu
1058a3114836SGerry Liu PR_ALL("%s (cmd = %s)...\n", f, SBD_CMD_STR(cmd));
1059a3114836SGerry Liu
1060a3114836SGerry Liu devset = DEVSET_AND(devset, DR_DEVS_PRESENT(bp));
1061a3114836SGerry Liu hp->h_err = drmach_pre_op(cmd, bp->b_id, &hp->h_opts, &devset);
1062a3114836SGerry Liu if (hp->h_err != NULL) {
1063a3114836SGerry Liu PR_ALL("drmach_pre_op failed for cmd %s(%d)\n",
1064a3114836SGerry Liu SBD_CMD_STR(cmd), cmd);
1065a3114836SGerry Liu return (-1);
1066a3114836SGerry Liu }
1067a3114836SGerry Liu
1068a3114836SGerry Liu /*
1069a3114836SGerry Liu * Check for valid state transitions.
1070a3114836SGerry Liu */
1071a3114836SGerry Liu if ((t = CMD2INDEX(cmd)) != -1) {
1072a3114836SGerry Liu struct dr_state_trans *transp;
1073a3114836SGerry Liu int state_err;
1074a3114836SGerry Liu
1075a3114836SGerry Liu transp = &dr_state_transition[t];
1076a3114836SGerry Liu ASSERT(transp->x_cmd == cmd);
1077a3114836SGerry Liu
1078a3114836SGerry Liu state_err = dr_check_transition(bp, &devset, transp, cmd);
1079a3114836SGerry Liu
1080a3114836SGerry Liu if (state_err < 0) {
1081a3114836SGerry Liu /*
1082a3114836SGerry Liu * Invalidate device.
1083a3114836SGerry Liu */
1084a3114836SGerry Liu dr_op_err(CE_IGNORE, hp, ESBD_INVAL, NULL);
1085a3114836SGerry Liu serr = -1;
1086a3114836SGerry Liu PR_ALL("%s: invalid devset (0x%x)\n",
1087a3114836SGerry Liu f, (uint_t)devset);
1088a3114836SGerry Liu } else if (state_err != 0) {
1089a3114836SGerry Liu /*
1090a3114836SGerry Liu * State transition is not a valid one.
1091a3114836SGerry Liu */
1092a3114836SGerry Liu dr_op_err(CE_IGNORE, hp,
1093a3114836SGerry Liu transp->x_op[state_err].x_err, NULL);
1094a3114836SGerry Liu
1095a3114836SGerry Liu serr = transp->x_op[state_err].x_rv;
1096a3114836SGerry Liu
1097a3114836SGerry Liu PR_ALL("%s: invalid state %s(%d) for cmd %s(%d)\n",
1098a3114836SGerry Liu f, state_str[state_err], state_err,
1099a3114836SGerry Liu SBD_CMD_STR(cmd), cmd);
1100a3114836SGerry Liu } else {
1101a3114836SGerry Liu shp->h_devset = devset;
1102a3114836SGerry Liu }
1103a3114836SGerry Liu }
1104a3114836SGerry Liu
1105a3114836SGerry Liu if (serr) {
1106a3114836SGerry Liu rv = -1;
1107a3114836SGerry Liu }
1108a3114836SGerry Liu
1109a3114836SGerry Liu return (rv);
1110a3114836SGerry Liu }
1111a3114836SGerry Liu
1112a3114836SGerry Liu static int
dr_post_op(dr_handle_t * hp,int rv)1113a3114836SGerry Liu dr_post_op(dr_handle_t *hp, int rv)
1114a3114836SGerry Liu {
1115a3114836SGerry Liu int cmd;
1116a3114836SGerry Liu sbd_error_t *err;
1117a3114836SGerry Liu dr_board_t *bp = hp->h_bd;
1118a3114836SGerry Liu static fn_t f = "dr_post_op";
1119a3114836SGerry Liu
1120a3114836SGerry Liu cmd = hp->h_cmd;
1121a3114836SGerry Liu
1122a3114836SGerry Liu PR_ALL("%s (cmd = %s)...\n", f, SBD_CMD_STR(cmd));
1123a3114836SGerry Liu
1124a3114836SGerry Liu err = drmach_post_op(cmd, bp->b_id, &hp->h_opts, rv);
1125a3114836SGerry Liu if (err != NULL) {
1126a3114836SGerry Liu PR_ALL("drmach_post_op failed for cmd %s(%d)\n",
1127a3114836SGerry Liu SBD_CMD_STR(cmd), cmd);
1128a3114836SGerry Liu if (rv == 0) {
1129a3114836SGerry Liu ASSERT(hp->h_err == NULL);
1130a3114836SGerry Liu hp->h_err = err;
1131a3114836SGerry Liu rv = -1;
1132a3114836SGerry Liu } else if (hp->h_err == NULL) {
1133a3114836SGerry Liu hp->h_err = err;
1134a3114836SGerry Liu } else {
1135a3114836SGerry Liu sbd_err_clear(&err);
1136a3114836SGerry Liu }
1137a3114836SGerry Liu }
1138a3114836SGerry Liu
1139a3114836SGerry Liu return (rv);
1140a3114836SGerry Liu }
1141a3114836SGerry Liu
1142a3114836SGerry Liu static int
dr_exec_op(dr_handle_t * hp)1143a3114836SGerry Liu dr_exec_op(dr_handle_t *hp)
1144a3114836SGerry Liu {
1145a3114836SGerry Liu int rv = 0;
1146a3114836SGerry Liu static fn_t f = "dr_exec_op";
1147a3114836SGerry Liu
1148a3114836SGerry Liu /* errors should have been caught by now */
1149a3114836SGerry Liu ASSERT(hp->h_err == NULL);
1150a3114836SGerry Liu
1151a3114836SGerry Liu switch (hp->h_cmd) {
1152a3114836SGerry Liu case SBD_CMD_ASSIGN:
1153a3114836SGerry Liu dr_assign_board(hp);
1154a3114836SGerry Liu break;
1155a3114836SGerry Liu
1156a3114836SGerry Liu case SBD_CMD_UNASSIGN:
1157a3114836SGerry Liu dr_unassign_board(hp);
1158a3114836SGerry Liu break;
1159a3114836SGerry Liu
1160a3114836SGerry Liu case SBD_CMD_POWEROFF:
1161a3114836SGerry Liu dr_poweroff_board(hp);
1162a3114836SGerry Liu break;
1163a3114836SGerry Liu
1164a3114836SGerry Liu case SBD_CMD_POWERON:
1165a3114836SGerry Liu dr_poweron_board(hp);
1166a3114836SGerry Liu break;
1167a3114836SGerry Liu
1168a3114836SGerry Liu case SBD_CMD_TEST:
1169a3114836SGerry Liu dr_test_board(hp);
1170a3114836SGerry Liu break;
1171a3114836SGerry Liu
1172a3114836SGerry Liu case SBD_CMD_CONNECT:
1173a3114836SGerry Liu dr_connect(hp);
1174a3114836SGerry Liu break;
1175a3114836SGerry Liu
1176a3114836SGerry Liu case SBD_CMD_CONFIGURE:
1177a3114836SGerry Liu dr_dev_configure(hp);
1178a3114836SGerry Liu break;
1179a3114836SGerry Liu
1180a3114836SGerry Liu case SBD_CMD_UNCONFIGURE:
1181a3114836SGerry Liu dr_dev_release(hp);
1182a3114836SGerry Liu if (hp->h_err == NULL)
1183a3114836SGerry Liu rv = dr_dev_unconfigure(hp);
1184a3114836SGerry Liu else
1185a3114836SGerry Liu dr_dev_cancel(hp);
1186a3114836SGerry Liu break;
1187a3114836SGerry Liu
1188a3114836SGerry Liu case SBD_CMD_DISCONNECT:
1189a3114836SGerry Liu rv = dr_disconnect(hp);
1190a3114836SGerry Liu break;
1191a3114836SGerry Liu
1192a3114836SGerry Liu case SBD_CMD_STATUS:
1193a3114836SGerry Liu rv = dr_dev_status(hp);
1194a3114836SGerry Liu break;
1195a3114836SGerry Liu
1196a3114836SGerry Liu case SBD_CMD_GETNCM:
1197a3114836SGerry Liu hp->h_sbdcmd.cmd_getncm.g_ncm = dr_get_ncm(hp);
1198a3114836SGerry Liu rv = dr_copyout_iocmd(hp);
1199a3114836SGerry Liu break;
1200a3114836SGerry Liu
1201a3114836SGerry Liu case SBD_CMD_PASSTHRU:
1202a3114836SGerry Liu rv = dr_pt_ioctl(hp);
1203a3114836SGerry Liu break;
1204a3114836SGerry Liu
1205a3114836SGerry Liu default:
1206a3114836SGerry Liu cmn_err(CE_WARN,
1207a3114836SGerry Liu "%s: unknown command (%d)",
1208a3114836SGerry Liu f, hp->h_cmd);
1209a3114836SGerry Liu break;
1210a3114836SGerry Liu }
1211a3114836SGerry Liu
1212a3114836SGerry Liu if (hp->h_err != NULL) {
1213a3114836SGerry Liu rv = -1;
1214a3114836SGerry Liu }
1215a3114836SGerry Liu
1216a3114836SGerry Liu return (rv);
1217a3114836SGerry Liu }
1218a3114836SGerry Liu
1219a3114836SGerry Liu static void
dr_assign_board(dr_handle_t * hp)1220a3114836SGerry Liu dr_assign_board(dr_handle_t *hp)
1221a3114836SGerry Liu {
1222a3114836SGerry Liu dr_board_t *bp = hp->h_bd;
1223a3114836SGerry Liu
1224a3114836SGerry Liu hp->h_err = drmach_board_assign(bp->b_num, &bp->b_id);
1225a3114836SGerry Liu if (hp->h_err == NULL) {
1226a3114836SGerry Liu bp->b_assigned = 1;
1227a3114836SGerry Liu }
1228a3114836SGerry Liu }
1229a3114836SGerry Liu
1230a3114836SGerry Liu static void
dr_unassign_board(dr_handle_t * hp)1231a3114836SGerry Liu dr_unassign_board(dr_handle_t *hp)
1232a3114836SGerry Liu {
1233a3114836SGerry Liu dr_board_t *bp = hp->h_bd;
1234a3114836SGerry Liu
1235a3114836SGerry Liu /*
1236a3114836SGerry Liu * Block out status during unassign.
1237a3114836SGerry Liu * Not doing cv_wait_sig here as starfire SSP software
1238a3114836SGerry Liu * ignores unassign failure and removes board from
1239a3114836SGerry Liu * domain mask causing system panic.
1240a3114836SGerry Liu * TODO: Change cv_wait to cv_wait_sig when SSP software
1241a3114836SGerry Liu * handles unassign failure.
1242a3114836SGerry Liu */
1243a3114836SGerry Liu dr_lock_status(bp);
1244a3114836SGerry Liu
1245a3114836SGerry Liu hp->h_err = drmach_board_unassign(bp->b_id);
1246a3114836SGerry Liu if (hp->h_err == NULL) {
1247a3114836SGerry Liu /*
1248a3114836SGerry Liu * clear drmachid_t handle; not valid after board unassign
1249a3114836SGerry Liu */
1250a3114836SGerry Liu bp->b_id = 0;
1251a3114836SGerry Liu bp->b_assigned = 0;
1252a3114836SGerry Liu }
1253a3114836SGerry Liu
1254a3114836SGerry Liu dr_unlock_status(bp);
1255a3114836SGerry Liu }
1256a3114836SGerry Liu
1257a3114836SGerry Liu static void
dr_poweron_board(dr_handle_t * hp)1258a3114836SGerry Liu dr_poweron_board(dr_handle_t *hp)
1259a3114836SGerry Liu {
1260a3114836SGerry Liu dr_board_t *bp = hp->h_bd;
1261a3114836SGerry Liu
1262a3114836SGerry Liu hp->h_err = drmach_board_poweron(bp->b_id);
1263a3114836SGerry Liu }
1264a3114836SGerry Liu
1265a3114836SGerry Liu static void
dr_poweroff_board(dr_handle_t * hp)1266a3114836SGerry Liu dr_poweroff_board(dr_handle_t *hp)
1267a3114836SGerry Liu {
1268a3114836SGerry Liu dr_board_t *bp = hp->h_bd;
1269a3114836SGerry Liu
1270a3114836SGerry Liu hp->h_err = drmach_board_poweroff(bp->b_id);
1271a3114836SGerry Liu }
1272a3114836SGerry Liu
1273a3114836SGerry Liu static void
dr_test_board(dr_handle_t * hp)1274a3114836SGerry Liu dr_test_board(dr_handle_t *hp)
1275a3114836SGerry Liu {
1276a3114836SGerry Liu dr_board_t *bp = hp->h_bd;
1277a3114836SGerry Liu hp->h_err = drmach_board_test(bp->b_id, &hp->h_opts,
1278a3114836SGerry Liu dr_cmd_flags(hp) & SBD_FLAG_FORCE);
1279a3114836SGerry Liu }
1280a3114836SGerry Liu
1281a3114836SGerry Liu /*
1282a3114836SGerry Liu * Create and populate the component nodes for a board. Assumes that the
1283a3114836SGerry Liu * devlists for the board have been initialized.
1284a3114836SGerry Liu */
1285a3114836SGerry Liu static void
dr_make_comp_nodes(dr_board_t * bp)1286a3114836SGerry Liu dr_make_comp_nodes(dr_board_t *bp)
1287a3114836SGerry Liu {
1288a3114836SGerry Liu int i;
1289a3114836SGerry Liu
1290a3114836SGerry Liu /*
1291a3114836SGerry Liu * Make nodes for the individual components on the board.
1292a3114836SGerry Liu * First we need to initialize memory unit data structures of board
1293a3114836SGerry Liu * structure.
1294a3114836SGerry Liu */
1295a3114836SGerry Liu for (i = 0; i < MAX_MEM_UNITS_PER_BOARD; i++) {
1296a3114836SGerry Liu dr_mem_unit_t *mp;
1297a3114836SGerry Liu
1298a3114836SGerry Liu mp = dr_get_mem_unit(bp, i);
1299a3114836SGerry Liu dr_init_mem_unit(mp);
1300a3114836SGerry Liu }
1301a3114836SGerry Liu
1302a3114836SGerry Liu /*
1303a3114836SGerry Liu * Initialize cpu unit data structures.
1304a3114836SGerry Liu */
1305a3114836SGerry Liu for (i = 0; i < MAX_CPU_UNITS_PER_BOARD; i++) {
1306a3114836SGerry Liu dr_cpu_unit_t *cp;
1307a3114836SGerry Liu
1308a3114836SGerry Liu cp = dr_get_cpu_unit(bp, i);
1309a3114836SGerry Liu dr_init_cpu_unit(cp);
1310a3114836SGerry Liu }
1311a3114836SGerry Liu
1312a3114836SGerry Liu /*
1313a3114836SGerry Liu * Initialize io unit data structures.
1314a3114836SGerry Liu */
1315a3114836SGerry Liu for (i = 0; i < MAX_IO_UNITS_PER_BOARD; i++) {
1316a3114836SGerry Liu dr_io_unit_t *ip;
1317a3114836SGerry Liu
1318a3114836SGerry Liu ip = dr_get_io_unit(bp, i);
1319a3114836SGerry Liu dr_init_io_unit(ip);
1320a3114836SGerry Liu }
1321a3114836SGerry Liu
1322a3114836SGerry Liu dr_board_transition(bp, DR_STATE_CONNECTED);
1323a3114836SGerry Liu
1324a3114836SGerry Liu bp->b_rstate = SBD_STAT_CONNECTED;
1325a3114836SGerry Liu bp->b_ostate = SBD_STAT_UNCONFIGURED;
1326a3114836SGerry Liu bp->b_cond = SBD_COND_OK;
1327a3114836SGerry Liu (void) drv_getparm(TIME, (void *)&bp->b_time);
1328a3114836SGerry Liu
1329a3114836SGerry Liu }
1330a3114836SGerry Liu
1331a3114836SGerry Liu /*
1332a3114836SGerry Liu * Only do work if called to operate on an entire board
1333a3114836SGerry Liu * which doesn't already have components present.
1334a3114836SGerry Liu */
1335a3114836SGerry Liu static void
dr_connect(dr_handle_t * hp)1336a3114836SGerry Liu dr_connect(dr_handle_t *hp)
1337a3114836SGerry Liu {
1338a3114836SGerry Liu dr_board_t *bp = hp->h_bd;
1339a3114836SGerry Liu static fn_t f = "dr_connect";
1340a3114836SGerry Liu
1341a3114836SGerry Liu PR_ALL("%s...\n", f);
1342a3114836SGerry Liu
1343a3114836SGerry Liu if (DR_DEVS_PRESENT(bp)) {
1344a3114836SGerry Liu /*
1345a3114836SGerry Liu * Board already has devices present.
1346a3114836SGerry Liu */
1347a3114836SGerry Liu PR_ALL("%s: devices already present (" DEVSET_FMT_STR ")\n",
1348a3114836SGerry Liu f, DEVSET_FMT_ARG(DR_DEVS_PRESENT(bp)));
1349a3114836SGerry Liu return;
1350a3114836SGerry Liu }
1351a3114836SGerry Liu
1352a3114836SGerry Liu hp->h_err = drmach_board_connect(bp->b_id, &hp->h_opts);
1353a3114836SGerry Liu if (hp->h_err)
1354a3114836SGerry Liu return;
1355a3114836SGerry Liu
1356a3114836SGerry Liu hp->h_err = dr_init_devlists(bp);
1357a3114836SGerry Liu if (hp->h_err)
1358a3114836SGerry Liu return;
1359a3114836SGerry Liu else if (bp->b_ndev == 0) {
1360a3114836SGerry Liu dr_op_err(CE_WARN, hp, ESBD_EMPTY_BD, bp->b_path);
1361a3114836SGerry Liu return;
1362a3114836SGerry Liu } else {
1363a3114836SGerry Liu dr_make_comp_nodes(bp);
1364a3114836SGerry Liu return;
1365a3114836SGerry Liu }
1366a3114836SGerry Liu /*NOTREACHED*/
1367a3114836SGerry Liu }
1368a3114836SGerry Liu
1369a3114836SGerry Liu static int
dr_disconnect(dr_handle_t * hp)1370a3114836SGerry Liu dr_disconnect(dr_handle_t *hp)
1371a3114836SGerry Liu {
1372a3114836SGerry Liu int i;
1373a3114836SGerry Liu dr_devset_t devset;
1374a3114836SGerry Liu dr_board_t *bp = hp->h_bd;
1375a3114836SGerry Liu static fn_t f = "dr_disconnect";
1376a3114836SGerry Liu
1377a3114836SGerry Liu PR_ALL("%s...\n", f);
1378a3114836SGerry Liu
1379a3114836SGerry Liu /*
1380a3114836SGerry Liu * Only devices which are present, but
1381a3114836SGerry Liu * unattached can be disconnected.
1382a3114836SGerry Liu */
1383a3114836SGerry Liu devset = hp->h_devset & DR_DEVS_PRESENT(bp) &
1384a3114836SGerry Liu DR_DEVS_UNATTACHED(bp);
1385a3114836SGerry Liu
1386a3114836SGerry Liu if ((devset == 0) && DR_DEVS_PRESENT(bp)) {
1387a3114836SGerry Liu dr_op_err(CE_IGNORE, hp, ESBD_EMPTY_BD, bp->b_path);
1388a3114836SGerry Liu return (0);
1389a3114836SGerry Liu }
1390a3114836SGerry Liu
1391a3114836SGerry Liu /*
1392a3114836SGerry Liu * Block out status during disconnect.
1393a3114836SGerry Liu */
1394a3114836SGerry Liu mutex_enter(&bp->b_slock);
1395a3114836SGerry Liu while (bp->b_sflags & DR_BSLOCK) {
1396a3114836SGerry Liu if (cv_wait_sig(&bp->b_scv, &bp->b_slock) == 0) {
1397a3114836SGerry Liu mutex_exit(&bp->b_slock);
1398a3114836SGerry Liu return (EINTR);
1399a3114836SGerry Liu }
1400a3114836SGerry Liu }
1401a3114836SGerry Liu bp->b_sflags |= DR_BSLOCK;
1402a3114836SGerry Liu mutex_exit(&bp->b_slock);
1403a3114836SGerry Liu
1404a3114836SGerry Liu hp->h_err = drmach_board_disconnect(bp->b_id, &hp->h_opts);
1405a3114836SGerry Liu if (hp->h_err && hp->h_err->e_code == EX86_WALK_DEPENDENCY) {
1406a3114836SGerry Liu /*
1407a3114836SGerry Liu * Other boards have dependency on this board. No device nodes
1408a3114836SGerry Liu * have been destroyed so keep current board status.
1409a3114836SGerry Liu */
1410a3114836SGerry Liu goto disconnect_done;
1411a3114836SGerry Liu }
1412a3114836SGerry Liu
1413a3114836SGerry Liu DR_DEVS_DISCONNECT(bp, devset);
1414a3114836SGerry Liu
1415a3114836SGerry Liu ASSERT((DR_DEVS_ATTACHED(bp) & devset) == 0);
1416a3114836SGerry Liu
1417a3114836SGerry Liu /*
1418a3114836SGerry Liu * Update per-device state transitions.
1419a3114836SGerry Liu */
1420a3114836SGerry Liu for (i = 0; i < MAX_CPU_UNITS_PER_BOARD; i++) {
1421a3114836SGerry Liu dr_cpu_unit_t *cp;
1422a3114836SGerry Liu
1423a3114836SGerry Liu if (!DEVSET_IN_SET(devset, SBD_COMP_CPU, i))
1424a3114836SGerry Liu continue;
1425a3114836SGerry Liu
1426a3114836SGerry Liu cp = dr_get_cpu_unit(bp, i);
1427a3114836SGerry Liu if (dr_disconnect_cpu(cp) == 0)
1428a3114836SGerry Liu dr_device_transition(&cp->sbc_cm, DR_STATE_EMPTY);
1429a3114836SGerry Liu else if (cp->sbc_cm.sbdev_error != NULL)
1430a3114836SGerry Liu DRERR_SET_C(&hp->h_err, &cp->sbc_cm.sbdev_error);
1431a3114836SGerry Liu
1432a3114836SGerry Liu ASSERT(cp->sbc_cm.sbdev_error == NULL);
1433a3114836SGerry Liu }
1434a3114836SGerry Liu
1435a3114836SGerry Liu for (i = 0; i < MAX_MEM_UNITS_PER_BOARD; i++) {
1436a3114836SGerry Liu dr_mem_unit_t *mp;
1437a3114836SGerry Liu
1438a3114836SGerry Liu if (!DEVSET_IN_SET(devset, SBD_COMP_MEM, i))
1439a3114836SGerry Liu continue;
1440a3114836SGerry Liu
1441a3114836SGerry Liu mp = dr_get_mem_unit(bp, i);
1442a3114836SGerry Liu if (dr_disconnect_mem(mp) == 0)
1443a3114836SGerry Liu dr_device_transition(&mp->sbm_cm, DR_STATE_EMPTY);
1444a3114836SGerry Liu else if (mp->sbm_cm.sbdev_error != NULL)
1445a3114836SGerry Liu DRERR_SET_C(&hp->h_err, &mp->sbm_cm.sbdev_error);
1446a3114836SGerry Liu
1447a3114836SGerry Liu ASSERT(mp->sbm_cm.sbdev_error == NULL);
1448a3114836SGerry Liu }
1449a3114836SGerry Liu
1450a3114836SGerry Liu for (i = 0; i < MAX_IO_UNITS_PER_BOARD; i++) {
1451a3114836SGerry Liu dr_io_unit_t *ip;
1452a3114836SGerry Liu
1453a3114836SGerry Liu if (!DEVSET_IN_SET(devset, SBD_COMP_IO, i))
1454a3114836SGerry Liu continue;
1455a3114836SGerry Liu
1456a3114836SGerry Liu ip = dr_get_io_unit(bp, i);
1457a3114836SGerry Liu if (dr_disconnect_io(ip) == 0)
1458a3114836SGerry Liu dr_device_transition(&ip->sbi_cm, DR_STATE_EMPTY);
1459a3114836SGerry Liu else if (ip->sbi_cm.sbdev_error != NULL)
1460a3114836SGerry Liu DRERR_SET_C(&hp->h_err, &ip->sbi_cm.sbdev_error);
1461a3114836SGerry Liu
1462a3114836SGerry Liu ASSERT(ip->sbi_cm.sbdev_error == NULL);
1463a3114836SGerry Liu }
1464a3114836SGerry Liu
1465a3114836SGerry Liu if (hp->h_err) {
1466a3114836SGerry Liu /*
1467a3114836SGerry Liu * For certain errors, drmach_board_disconnect will mark
1468a3114836SGerry Liu * the board as unusable; in these cases the devtree must
1469a3114836SGerry Liu * be purged so that status calls will succeed.
1470a3114836SGerry Liu * XXX
1471a3114836SGerry Liu * This implementation checks for discrete error codes -
1472a3114836SGerry Liu * someday, the i/f to drmach_board_disconnect should be
1473a3114836SGerry Liu * changed to avoid the e_code testing.
1474a3114836SGerry Liu */
1475a3114836SGerry Liu if (hp->h_err->e_code == EX86_DEPROBE) {
1476a3114836SGerry Liu bp->b_ostate = SBD_STAT_UNCONFIGURED;
1477a3114836SGerry Liu bp->b_busy = 0;
1478a3114836SGerry Liu (void) drv_getparm(TIME, (void *)&bp->b_time);
1479a3114836SGerry Liu
1480a3114836SGerry Liu if (drmach_board_deprobe(bp->b_id))
1481a3114836SGerry Liu goto disconnect_done;
1482a3114836SGerry Liu else
1483a3114836SGerry Liu bp->b_ndev = 0;
1484a3114836SGerry Liu }
1485a3114836SGerry Liu }
1486a3114836SGerry Liu
1487a3114836SGerry Liu /*
1488a3114836SGerry Liu * Once all the components on a board have been disconnect
1489a3114836SGerry Liu * the board's state can transition to disconnected and
1490a3114836SGerry Liu * we can allow the deprobe to take place.
1491a3114836SGerry Liu */
1492a3114836SGerry Liu if (hp->h_err == NULL && DR_DEVS_PRESENT(bp) == 0) {
1493a3114836SGerry Liu dr_board_transition(bp, DR_STATE_OCCUPIED);
1494a3114836SGerry Liu bp->b_rstate = SBD_STAT_DISCONNECTED;
1495a3114836SGerry Liu bp->b_ostate = SBD_STAT_UNCONFIGURED;
1496a3114836SGerry Liu bp->b_busy = 0;
1497a3114836SGerry Liu (void) drv_getparm(TIME, (void *)&bp->b_time);
1498a3114836SGerry Liu
1499a3114836SGerry Liu hp->h_err = drmach_board_deprobe(bp->b_id);
1500a3114836SGerry Liu
1501a3114836SGerry Liu if (hp->h_err == NULL) {
1502a3114836SGerry Liu bp->b_ndev = 0;
1503a3114836SGerry Liu dr_board_transition(bp, DR_STATE_EMPTY);
1504a3114836SGerry Liu bp->b_rstate = SBD_STAT_EMPTY;
1505a3114836SGerry Liu (void) drv_getparm(TIME, (void *)&bp->b_time);
1506a3114836SGerry Liu }
1507a3114836SGerry Liu }
1508a3114836SGerry Liu
1509a3114836SGerry Liu disconnect_done:
1510a3114836SGerry Liu dr_unlock_status(bp);
1511a3114836SGerry Liu
1512a3114836SGerry Liu return (0);
1513a3114836SGerry Liu }
1514a3114836SGerry Liu
1515a3114836SGerry Liu /*
1516a3114836SGerry Liu * Check if a particular device is a valid target of the current
1517a3114836SGerry Liu * operation. Return 1 if it is a valid target, and 0 otherwise.
1518a3114836SGerry Liu */
1519a3114836SGerry Liu static int
dr_dev_is_target(dr_dev_unit_t * dp,int present_only,uint_t uset)1520a3114836SGerry Liu dr_dev_is_target(dr_dev_unit_t *dp, int present_only, uint_t uset)
1521a3114836SGerry Liu {
1522a3114836SGerry Liu dr_common_unit_t *cp;
1523a3114836SGerry Liu int is_present;
1524a3114836SGerry Liu int is_attached;
1525a3114836SGerry Liu
1526a3114836SGerry Liu cp = &dp->du_common;
1527a3114836SGerry Liu
1528a3114836SGerry Liu /* check if the user requested this device */
1529a3114836SGerry Liu if ((uset & (1 << cp->sbdev_unum)) == 0) {
1530a3114836SGerry Liu return (0);
1531a3114836SGerry Liu }
1532a3114836SGerry Liu
1533a3114836SGerry Liu is_present = DR_DEV_IS_PRESENT(cp) ? 1 : 0;
1534a3114836SGerry Liu is_attached = DR_DEV_IS_ATTACHED(cp) ? 1 : 0;
1535a3114836SGerry Liu
1536a3114836SGerry Liu /*
1537a3114836SGerry Liu * If the present_only flag is set, a valid target
1538a3114836SGerry Liu * must be present but not attached. Otherwise, it
1539a3114836SGerry Liu * must be both present and attached.
1540a3114836SGerry Liu */
1541a3114836SGerry Liu if (is_present && (present_only ^ is_attached)) {
1542a3114836SGerry Liu /* sanity check */
1543a3114836SGerry Liu ASSERT(cp->sbdev_id != (drmachid_t)0);
1544a3114836SGerry Liu
1545a3114836SGerry Liu return (1);
1546a3114836SGerry Liu }
1547a3114836SGerry Liu
1548a3114836SGerry Liu return (0);
1549a3114836SGerry Liu }
1550a3114836SGerry Liu
1551a3114836SGerry Liu static void
dr_dev_make_list(dr_handle_t * hp,sbd_comp_type_t type,int present_only,dr_common_unit_t *** devlist,int * devnum)1552a3114836SGerry Liu dr_dev_make_list(dr_handle_t *hp, sbd_comp_type_t type, int present_only,
1553a3114836SGerry Liu dr_common_unit_t ***devlist, int *devnum)
1554a3114836SGerry Liu {
1555a3114836SGerry Liu dr_board_t *bp = hp->h_bd;
1556a3114836SGerry Liu int unum;
1557a3114836SGerry Liu int nunits;
1558a3114836SGerry Liu uint_t uset;
1559a3114836SGerry Liu int len;
1560a3114836SGerry Liu dr_common_unit_t **list, **wp;
1561a3114836SGerry Liu
1562a3114836SGerry Liu switch (type) {
1563a3114836SGerry Liu case SBD_COMP_CPU:
1564a3114836SGerry Liu nunits = MAX_CPU_UNITS_PER_BOARD;
1565a3114836SGerry Liu break;
1566a3114836SGerry Liu case SBD_COMP_MEM:
1567a3114836SGerry Liu nunits = MAX_MEM_UNITS_PER_BOARD;
1568a3114836SGerry Liu break;
1569a3114836SGerry Liu case SBD_COMP_IO:
1570a3114836SGerry Liu nunits = MAX_IO_UNITS_PER_BOARD;
1571a3114836SGerry Liu break;
1572a3114836SGerry Liu default:
1573a3114836SGerry Liu /* catch this in debug kernels */
1574a3114836SGerry Liu ASSERT(0);
1575a3114836SGerry Liu break;
1576a3114836SGerry Liu }
1577a3114836SGerry Liu
1578a3114836SGerry Liu /* allocate list storage. */
1579a3114836SGerry Liu len = sizeof (dr_common_unit_t *) * (nunits + 1);
1580a3114836SGerry Liu list = kmem_zalloc(len, KM_SLEEP);
1581a3114836SGerry Liu
1582a3114836SGerry Liu /* record length of storage in first element */
1583a3114836SGerry Liu *list++ = (dr_common_unit_t *)(uintptr_t)len;
1584a3114836SGerry Liu
1585a3114836SGerry Liu /* get bit array signifying which units are to be involved */
1586a3114836SGerry Liu uset = DEVSET_GET_UNITSET(hp->h_devset, type);
1587a3114836SGerry Liu
1588a3114836SGerry Liu /*
1589a3114836SGerry Liu * Adjust the loop count for CPU devices since all cores
1590a3114836SGerry Liu * in a CMP will be examined in a single iteration.
1591a3114836SGerry Liu */
1592a3114836SGerry Liu if (type == SBD_COMP_CPU) {
1593a3114836SGerry Liu nunits = MAX_CMP_UNITS_PER_BOARD;
1594a3114836SGerry Liu }
1595a3114836SGerry Liu
1596a3114836SGerry Liu /* populate list */
1597a3114836SGerry Liu for (wp = list, unum = 0; unum < nunits; unum++) {
1598a3114836SGerry Liu dr_dev_unit_t *dp;
1599a3114836SGerry Liu int core;
1600a3114836SGerry Liu int cunum;
1601a3114836SGerry Liu
1602a3114836SGerry Liu dp = DR_GET_BOARD_DEVUNIT(bp, type, unum);
1603a3114836SGerry Liu if (dr_dev_is_target(dp, present_only, uset)) {
1604a3114836SGerry Liu *wp++ = &dp->du_common;
1605a3114836SGerry Liu }
1606a3114836SGerry Liu
1607a3114836SGerry Liu /* further processing is only required for CPUs */
1608a3114836SGerry Liu if (type != SBD_COMP_CPU) {
1609a3114836SGerry Liu continue;
1610a3114836SGerry Liu }
1611a3114836SGerry Liu
1612a3114836SGerry Liu /*
1613a3114836SGerry Liu * Add any additional cores from the current CPU
1614a3114836SGerry Liu * device. This is to ensure that all the cores
1615a3114836SGerry Liu * are grouped together in the device list, and
1616a3114836SGerry Liu * consequently sequenced together during the actual
1617a3114836SGerry Liu * operation.
1618a3114836SGerry Liu */
1619a3114836SGerry Liu for (core = 1; core < MAX_CORES_PER_CMP; core++) {
1620a3114836SGerry Liu cunum = DR_CMP_CORE_UNUM(unum, core);
1621a3114836SGerry Liu dp = DR_GET_BOARD_DEVUNIT(bp, type, cunum);
1622a3114836SGerry Liu
1623a3114836SGerry Liu if (dr_dev_is_target(dp, present_only, uset)) {
1624a3114836SGerry Liu *wp++ = &dp->du_common;
1625a3114836SGerry Liu }
1626a3114836SGerry Liu }
1627a3114836SGerry Liu }
1628a3114836SGerry Liu
1629a3114836SGerry Liu /* calculate number of units in list, return result and list pointer */
1630a3114836SGerry Liu *devnum = wp - list;
1631a3114836SGerry Liu *devlist = list;
1632a3114836SGerry Liu }
1633a3114836SGerry Liu
1634a3114836SGerry Liu static void
dr_dev_clean_up(dr_handle_t * hp,dr_common_unit_t ** list,int devnum)1635a3114836SGerry Liu dr_dev_clean_up(dr_handle_t *hp, dr_common_unit_t **list, int devnum)
1636a3114836SGerry Liu {
1637a3114836SGerry Liu int len;
1638a3114836SGerry Liu int n = 0;
1639a3114836SGerry Liu dr_common_unit_t *cp, **rp = list;
1640a3114836SGerry Liu
1641a3114836SGerry Liu /*
1642a3114836SGerry Liu * move first encountered unit error to handle if handle
1643a3114836SGerry Liu * does not yet have a recorded error.
1644a3114836SGerry Liu */
1645a3114836SGerry Liu if (hp->h_err == NULL) {
1646a3114836SGerry Liu while (n++ < devnum) {
1647a3114836SGerry Liu cp = *rp++;
1648a3114836SGerry Liu if (cp->sbdev_error != NULL) {
1649a3114836SGerry Liu hp->h_err = cp->sbdev_error;
1650a3114836SGerry Liu cp->sbdev_error = NULL;
1651a3114836SGerry Liu break;
1652a3114836SGerry Liu }
1653a3114836SGerry Liu }
1654a3114836SGerry Liu }
1655a3114836SGerry Liu
1656a3114836SGerry Liu /* free remaining unit errors */
1657a3114836SGerry Liu while (n++ < devnum) {
1658a3114836SGerry Liu cp = *rp++;
1659a3114836SGerry Liu if (cp->sbdev_error != NULL) {
1660a3114836SGerry Liu sbd_err_clear(&cp->sbdev_error);
1661a3114836SGerry Liu cp->sbdev_error = NULL;
1662a3114836SGerry Liu }
1663a3114836SGerry Liu }
1664a3114836SGerry Liu
1665a3114836SGerry Liu /* free list */
1666a3114836SGerry Liu list -= 1;
1667a3114836SGerry Liu len = (int)(uintptr_t)list[0];
1668a3114836SGerry Liu kmem_free(list, len);
1669a3114836SGerry Liu }
1670a3114836SGerry Liu
1671a3114836SGerry Liu static int
dr_dev_walk(dr_handle_t * hp,sbd_comp_type_t type,int present_only,int (* pre_op)(dr_handle_t *,dr_common_unit_t **,int),void (* op)(dr_handle_t *,dr_common_unit_t *),int (* post_op)(dr_handle_t *,dr_common_unit_t **,int),void (* board_op)(dr_handle_t *,dr_common_unit_t **,int))1672a3114836SGerry Liu dr_dev_walk(dr_handle_t *hp, sbd_comp_type_t type, int present_only,
1673a3114836SGerry Liu int (*pre_op)(dr_handle_t *, dr_common_unit_t **, int),
1674a3114836SGerry Liu void (*op)(dr_handle_t *, dr_common_unit_t *),
1675a3114836SGerry Liu int (*post_op)(dr_handle_t *, dr_common_unit_t **, int),
1676a3114836SGerry Liu void (*board_op)(dr_handle_t *, dr_common_unit_t **, int))
1677a3114836SGerry Liu {
1678a3114836SGerry Liu int devnum, rv;
1679a3114836SGerry Liu dr_common_unit_t **devlist;
1680a3114836SGerry Liu
1681a3114836SGerry Liu dr_dev_make_list(hp, type, present_only, &devlist, &devnum);
1682a3114836SGerry Liu
1683a3114836SGerry Liu rv = 0;
1684a3114836SGerry Liu if (devnum > 0) {
1685a3114836SGerry Liu rv = (*pre_op)(hp, devlist, devnum);
1686a3114836SGerry Liu if (rv == 0) {
1687a3114836SGerry Liu int n;
1688a3114836SGerry Liu
1689a3114836SGerry Liu for (n = 0; n < devnum; n++)
1690a3114836SGerry Liu (*op)(hp, devlist[n]);
1691a3114836SGerry Liu
1692a3114836SGerry Liu rv = (*post_op)(hp, devlist, devnum);
1693a3114836SGerry Liu
1694a3114836SGerry Liu (*board_op)(hp, devlist, devnum);
1695a3114836SGerry Liu }
1696a3114836SGerry Liu }
1697a3114836SGerry Liu
1698a3114836SGerry Liu dr_dev_clean_up(hp, devlist, devnum);
1699a3114836SGerry Liu return (rv);
1700a3114836SGerry Liu }
1701a3114836SGerry Liu
1702a3114836SGerry Liu /*ARGSUSED*/
1703a3114836SGerry Liu static int
dr_dev_noop(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)1704a3114836SGerry Liu dr_dev_noop(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum)
1705a3114836SGerry Liu {
1706a3114836SGerry Liu return (0);
1707a3114836SGerry Liu }
1708a3114836SGerry Liu
1709a3114836SGerry Liu static void
dr_attach_update_state(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)1710a3114836SGerry Liu dr_attach_update_state(dr_handle_t *hp,
1711a3114836SGerry Liu dr_common_unit_t **devlist, int devnum)
1712a3114836SGerry Liu {
1713a3114836SGerry Liu dr_board_t *bp = hp->h_bd;
1714a3114836SGerry Liu int i;
1715a3114836SGerry Liu dr_devset_t devs_unattached, devs_present;
1716a3114836SGerry Liu static fn_t f = "dr_attach_update_state";
1717a3114836SGerry Liu
1718a3114836SGerry Liu for (i = 0; i < devnum; i++) {
1719a3114836SGerry Liu dr_common_unit_t *cp = devlist[i];
1720a3114836SGerry Liu
1721a3114836SGerry Liu if (dr_check_unit_attached(cp) == -1) {
1722a3114836SGerry Liu PR_ALL("%s: ERROR %s not attached\n",
1723a3114836SGerry Liu f, cp->sbdev_path);
1724a3114836SGerry Liu continue;
1725a3114836SGerry Liu }
1726a3114836SGerry Liu
1727a3114836SGerry Liu DR_DEV_SET_ATTACHED(cp);
1728a3114836SGerry Liu
1729a3114836SGerry Liu dr_device_transition(cp, DR_STATE_CONFIGURED);
1730a3114836SGerry Liu cp->sbdev_cond = SBD_COND_OK;
1731a3114836SGerry Liu }
1732a3114836SGerry Liu
1733a3114836SGerry Liu devs_present = DR_DEVS_PRESENT(bp);
1734a3114836SGerry Liu devs_unattached = DR_DEVS_UNATTACHED(bp);
1735a3114836SGerry Liu
1736a3114836SGerry Liu switch (bp->b_state) {
1737a3114836SGerry Liu case DR_STATE_CONNECTED:
1738a3114836SGerry Liu case DR_STATE_UNCONFIGURED:
1739a3114836SGerry Liu ASSERT(devs_present);
1740a3114836SGerry Liu
1741a3114836SGerry Liu if (devs_unattached == 0) {
1742a3114836SGerry Liu /*
1743a3114836SGerry Liu * All devices finally attached.
1744a3114836SGerry Liu */
1745a3114836SGerry Liu dr_board_transition(bp, DR_STATE_CONFIGURED);
1746a3114836SGerry Liu hp->h_bd->b_ostate = SBD_STAT_CONFIGURED;
1747a3114836SGerry Liu hp->h_bd->b_rstate = SBD_STAT_CONNECTED;
1748a3114836SGerry Liu hp->h_bd->b_cond = SBD_COND_OK;
1749a3114836SGerry Liu hp->h_bd->b_busy = 0;
1750a3114836SGerry Liu (void) drv_getparm(TIME, (void *)&hp->h_bd->b_time);
1751a3114836SGerry Liu } else if (devs_present != devs_unattached) {
1752a3114836SGerry Liu /*
1753a3114836SGerry Liu * Only some devices are fully attached.
1754a3114836SGerry Liu */
1755a3114836SGerry Liu dr_board_transition(bp, DR_STATE_PARTIAL);
1756a3114836SGerry Liu hp->h_bd->b_rstate = SBD_STAT_CONNECTED;
1757a3114836SGerry Liu hp->h_bd->b_ostate = SBD_STAT_CONFIGURED;
1758a3114836SGerry Liu (void) drv_getparm(TIME, (void *)&hp->h_bd->b_time);
1759a3114836SGerry Liu }
1760a3114836SGerry Liu break;
1761a3114836SGerry Liu
1762a3114836SGerry Liu case DR_STATE_PARTIAL:
1763a3114836SGerry Liu ASSERT(devs_present);
1764a3114836SGerry Liu /*
1765a3114836SGerry Liu * All devices finally attached.
1766a3114836SGerry Liu */
1767a3114836SGerry Liu if (devs_unattached == 0) {
1768a3114836SGerry Liu dr_board_transition(bp, DR_STATE_CONFIGURED);
1769a3114836SGerry Liu hp->h_bd->b_rstate = SBD_STAT_CONNECTED;
1770a3114836SGerry Liu hp->h_bd->b_ostate = SBD_STAT_CONFIGURED;
1771a3114836SGerry Liu hp->h_bd->b_cond = SBD_COND_OK;
1772a3114836SGerry Liu hp->h_bd->b_busy = 0;
1773a3114836SGerry Liu (void) drv_getparm(TIME, (void *)&hp->h_bd->b_time);
1774a3114836SGerry Liu }
1775a3114836SGerry Liu break;
1776a3114836SGerry Liu
1777a3114836SGerry Liu default:
1778a3114836SGerry Liu break;
1779a3114836SGerry Liu }
1780a3114836SGerry Liu }
1781a3114836SGerry Liu
1782a3114836SGerry Liu static void
dr_dev_configure(dr_handle_t * hp)1783a3114836SGerry Liu dr_dev_configure(dr_handle_t *hp)
1784a3114836SGerry Liu {
1785a3114836SGerry Liu int rv;
1786a3114836SGerry Liu
1787a3114836SGerry Liu rv = dr_dev_walk(hp, SBD_COMP_CPU, 1,
1788a3114836SGerry Liu dr_pre_attach_cpu,
1789a3114836SGerry Liu dr_attach_cpu,
1790a3114836SGerry Liu dr_post_attach_cpu,
1791a3114836SGerry Liu dr_attach_update_state);
1792a3114836SGerry Liu
1793a3114836SGerry Liu if (rv >= 0) {
1794a3114836SGerry Liu rv = dr_dev_walk(hp, SBD_COMP_MEM, 1,
1795a3114836SGerry Liu dr_pre_attach_mem,
1796a3114836SGerry Liu dr_attach_mem,
1797a3114836SGerry Liu dr_post_attach_mem,
1798a3114836SGerry Liu dr_attach_update_state);
1799a3114836SGerry Liu }
1800a3114836SGerry Liu
1801a3114836SGerry Liu if (rv >= 0) {
1802a3114836SGerry Liu (void) dr_dev_walk(hp, SBD_COMP_IO, 1,
1803a3114836SGerry Liu dr_pre_attach_io,
1804a3114836SGerry Liu dr_attach_io,
1805a3114836SGerry Liu dr_post_attach_io,
1806a3114836SGerry Liu dr_attach_update_state);
1807a3114836SGerry Liu }
1808a3114836SGerry Liu }
1809a3114836SGerry Liu
1810a3114836SGerry Liu static void
dr_release_update_state(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)1811a3114836SGerry Liu dr_release_update_state(dr_handle_t *hp,
1812a3114836SGerry Liu dr_common_unit_t **devlist, int devnum)
1813a3114836SGerry Liu {
1814a3114836SGerry Liu _NOTE(ARGUNUSED(devlist))
1815a3114836SGerry Liu _NOTE(ARGUNUSED(devnum))
1816a3114836SGerry Liu
1817a3114836SGerry Liu dr_board_t *bp = hp->h_bd;
1818a3114836SGerry Liu
1819a3114836SGerry Liu /*
1820a3114836SGerry Liu * If the entire board was released and all components
1821a3114836SGerry Liu * unreferenced then transfer it to the UNREFERENCED state.
1822a3114836SGerry Liu */
1823a3114836SGerry Liu if ((bp->b_state != DR_STATE_RELEASE) &&
1824a3114836SGerry Liu (DR_DEVS_RELEASED(bp) == DR_DEVS_ATTACHED(bp))) {
1825a3114836SGerry Liu dr_board_transition(bp, DR_STATE_RELEASE);
1826a3114836SGerry Liu hp->h_bd->b_busy = 1;
1827a3114836SGerry Liu }
1828a3114836SGerry Liu }
1829a3114836SGerry Liu
1830a3114836SGerry Liu /* called by dr_release_done [below] and dr_release_mem_done [dr_mem.c] */
1831a3114836SGerry Liu int
dr_release_dev_done(dr_common_unit_t * cp)1832a3114836SGerry Liu dr_release_dev_done(dr_common_unit_t *cp)
1833a3114836SGerry Liu {
1834a3114836SGerry Liu if (cp->sbdev_state == DR_STATE_RELEASE) {
1835a3114836SGerry Liu ASSERT(DR_DEV_IS_RELEASED(cp));
1836a3114836SGerry Liu
1837a3114836SGerry Liu DR_DEV_SET_UNREFERENCED(cp);
1838a3114836SGerry Liu
1839a3114836SGerry Liu dr_device_transition(cp, DR_STATE_UNREFERENCED);
1840a3114836SGerry Liu
1841a3114836SGerry Liu return (0);
1842a3114836SGerry Liu } else {
1843a3114836SGerry Liu return (-1);
1844a3114836SGerry Liu }
1845a3114836SGerry Liu }
1846a3114836SGerry Liu
1847a3114836SGerry Liu static void
dr_release_done(dr_handle_t * hp,dr_common_unit_t * cp)1848a3114836SGerry Liu dr_release_done(dr_handle_t *hp, dr_common_unit_t *cp)
1849a3114836SGerry Liu {
1850a3114836SGerry Liu _NOTE(ARGUNUSED(hp))
1851a3114836SGerry Liu
1852a3114836SGerry Liu dr_board_t *bp;
1853a3114836SGerry Liu static fn_t f = "dr_release_done";
1854a3114836SGerry Liu
1855a3114836SGerry Liu PR_ALL("%s...\n", f);
1856a3114836SGerry Liu
1857a3114836SGerry Liu /* get board pointer & sanity check */
1858a3114836SGerry Liu bp = cp->sbdev_bp;
1859a3114836SGerry Liu ASSERT(bp == hp->h_bd);
1860a3114836SGerry Liu
1861a3114836SGerry Liu /*
1862a3114836SGerry Liu * Transfer the device which just completed its release
1863a3114836SGerry Liu * to the UNREFERENCED state.
1864a3114836SGerry Liu */
1865a3114836SGerry Liu switch (cp->sbdev_type) {
1866a3114836SGerry Liu case SBD_COMP_MEM:
1867a3114836SGerry Liu dr_release_mem_done(cp);
1868a3114836SGerry Liu break;
1869a3114836SGerry Liu
1870a3114836SGerry Liu default:
1871a3114836SGerry Liu DR_DEV_SET_RELEASED(cp);
1872a3114836SGerry Liu
1873a3114836SGerry Liu dr_device_transition(cp, DR_STATE_RELEASE);
1874a3114836SGerry Liu
1875a3114836SGerry Liu (void) dr_release_dev_done(cp);
1876a3114836SGerry Liu break;
1877a3114836SGerry Liu }
1878a3114836SGerry Liu
1879a3114836SGerry Liu /*
1880a3114836SGerry Liu * If we're not already in the RELEASE state for this
1881a3114836SGerry Liu * board and we now have released all that were previously
1882a3114836SGerry Liu * attached, then transfer the board to the RELEASE state.
1883a3114836SGerry Liu */
1884a3114836SGerry Liu if ((bp->b_state == DR_STATE_RELEASE) &&
1885a3114836SGerry Liu (DR_DEVS_RELEASED(bp) == DR_DEVS_UNREFERENCED(bp))) {
1886a3114836SGerry Liu dr_board_transition(bp, DR_STATE_UNREFERENCED);
1887a3114836SGerry Liu bp->b_busy = 1;
1888a3114836SGerry Liu (void) drv_getparm(TIME, (void *)&bp->b_time);
1889a3114836SGerry Liu }
1890a3114836SGerry Liu }
1891a3114836SGerry Liu
1892a3114836SGerry Liu static void
dr_dev_release_mem(dr_handle_t * hp,dr_common_unit_t * dv)1893a3114836SGerry Liu dr_dev_release_mem(dr_handle_t *hp, dr_common_unit_t *dv)
1894a3114836SGerry Liu {
1895a3114836SGerry Liu dr_release_mem(dv);
1896a3114836SGerry Liu dr_release_done(hp, dv);
1897a3114836SGerry Liu }
1898a3114836SGerry Liu
1899a3114836SGerry Liu static void
dr_dev_release(dr_handle_t * hp)1900a3114836SGerry Liu dr_dev_release(dr_handle_t *hp)
1901a3114836SGerry Liu {
1902a3114836SGerry Liu int rv;
1903a3114836SGerry Liu
1904a3114836SGerry Liu hp->h_bd->b_busy = 1;
1905a3114836SGerry Liu
1906a3114836SGerry Liu rv = dr_dev_walk(hp, SBD_COMP_CPU, 0,
1907a3114836SGerry Liu dr_pre_release_cpu,
1908a3114836SGerry Liu dr_release_done,
1909a3114836SGerry Liu dr_dev_noop,
1910a3114836SGerry Liu dr_release_update_state);
1911a3114836SGerry Liu
1912a3114836SGerry Liu if (rv >= 0) {
1913a3114836SGerry Liu rv = dr_dev_walk(hp, SBD_COMP_MEM, 0,
1914a3114836SGerry Liu dr_pre_release_mem,
1915a3114836SGerry Liu dr_dev_release_mem,
1916a3114836SGerry Liu dr_dev_noop,
1917a3114836SGerry Liu dr_release_update_state);
1918a3114836SGerry Liu }
1919a3114836SGerry Liu
1920a3114836SGerry Liu if (rv >= 0) {
1921a3114836SGerry Liu rv = dr_dev_walk(hp, SBD_COMP_IO, 0,
1922a3114836SGerry Liu dr_pre_release_io,
1923a3114836SGerry Liu dr_release_done,
1924a3114836SGerry Liu dr_dev_noop,
1925a3114836SGerry Liu dr_release_update_state);
1926a3114836SGerry Liu
1927a3114836SGerry Liu }
1928a3114836SGerry Liu
1929a3114836SGerry Liu if (rv < 0)
1930a3114836SGerry Liu hp->h_bd->b_busy = 0;
1931a3114836SGerry Liu /* else, b_busy will be cleared in dr_detach_update_state() */
1932a3114836SGerry Liu }
1933a3114836SGerry Liu
1934a3114836SGerry Liu static void
dr_detach_update_state(dr_handle_t * hp,dr_common_unit_t ** devlist,int devnum)1935a3114836SGerry Liu dr_detach_update_state(dr_handle_t *hp,
1936a3114836SGerry Liu dr_common_unit_t **devlist, int devnum)
1937a3114836SGerry Liu {
1938a3114836SGerry Liu dr_board_t *bp = hp->h_bd;
1939a3114836SGerry Liu int i;
1940a3114836SGerry Liu dr_state_t bstate;
1941a3114836SGerry Liu static fn_t f = "dr_detach_update_state";
1942a3114836SGerry Liu
1943a3114836SGerry Liu for (i = 0; i < devnum; i++) {
1944a3114836SGerry Liu dr_common_unit_t *cp = devlist[i];
1945a3114836SGerry Liu
1946a3114836SGerry Liu if (dr_check_unit_attached(cp) >= 0) {
1947a3114836SGerry Liu /*
1948a3114836SGerry Liu * Device is still attached probably due
1949a3114836SGerry Liu * to an error. Need to keep track of it.
1950a3114836SGerry Liu */
1951a3114836SGerry Liu PR_ALL("%s: ERROR %s not detached\n",
1952a3114836SGerry Liu f, cp->sbdev_path);
1953a3114836SGerry Liu
1954a3114836SGerry Liu continue;
1955a3114836SGerry Liu }
1956a3114836SGerry Liu
1957a3114836SGerry Liu DR_DEV_CLR_ATTACHED(cp);
1958a3114836SGerry Liu DR_DEV_CLR_RELEASED(cp);
1959a3114836SGerry Liu DR_DEV_CLR_UNREFERENCED(cp);
1960a3114836SGerry Liu dr_device_transition(cp, DR_STATE_UNCONFIGURED);
1961a3114836SGerry Liu }
1962a3114836SGerry Liu
1963a3114836SGerry Liu bstate = bp->b_state;
1964a3114836SGerry Liu if (bstate != DR_STATE_UNCONFIGURED) {
1965a3114836SGerry Liu if (DR_DEVS_PRESENT(bp) == DR_DEVS_UNATTACHED(bp)) {
1966a3114836SGerry Liu /*
1967a3114836SGerry Liu * All devices are finally detached.
1968a3114836SGerry Liu */
1969a3114836SGerry Liu dr_board_transition(bp, DR_STATE_UNCONFIGURED);
1970a3114836SGerry Liu hp->h_bd->b_ostate = SBD_STAT_UNCONFIGURED;
1971a3114836SGerry Liu (void) drv_getparm(TIME, (void *)&hp->h_bd->b_time);
1972a3114836SGerry Liu } else if ((bp->b_state != DR_STATE_PARTIAL) &&
1973a3114836SGerry Liu (DR_DEVS_ATTACHED(bp) !=
1974a3114836SGerry Liu DR_DEVS_PRESENT(bp))) {
1975a3114836SGerry Liu /*
1976a3114836SGerry Liu * Some devices remain attached.
1977a3114836SGerry Liu */
1978a3114836SGerry Liu dr_board_transition(bp, DR_STATE_PARTIAL);
1979a3114836SGerry Liu (void) drv_getparm(TIME, (void *)&hp->h_bd->b_time);
1980a3114836SGerry Liu }
1981a3114836SGerry Liu
1982a3114836SGerry Liu if ((hp->h_devset & DR_DEVS_UNATTACHED(bp)) == hp->h_devset)
1983a3114836SGerry Liu hp->h_bd->b_busy = 0;
1984a3114836SGerry Liu }
1985a3114836SGerry Liu }
1986a3114836SGerry Liu
1987a3114836SGerry Liu static int
dr_dev_unconfigure(dr_handle_t * hp)1988a3114836SGerry Liu dr_dev_unconfigure(dr_handle_t *hp)
1989a3114836SGerry Liu {
1990a3114836SGerry Liu dr_board_t *bp = hp->h_bd;
1991a3114836SGerry Liu
1992a3114836SGerry Liu /*
1993a3114836SGerry Liu * Block out status during IO unconfig.
1994a3114836SGerry Liu */
1995a3114836SGerry Liu mutex_enter(&bp->b_slock);
1996a3114836SGerry Liu while (bp->b_sflags & DR_BSLOCK) {
1997a3114836SGerry Liu if (cv_wait_sig(&bp->b_scv, &bp->b_slock) == 0) {
1998a3114836SGerry Liu mutex_exit(&bp->b_slock);
1999a3114836SGerry Liu return (EINTR);
2000a3114836SGerry Liu }
2001a3114836SGerry Liu }
2002a3114836SGerry Liu bp->b_sflags |= DR_BSLOCK;
2003a3114836SGerry Liu mutex_exit(&bp->b_slock);
2004a3114836SGerry Liu
2005a3114836SGerry Liu (void) dr_dev_walk(hp, SBD_COMP_IO, 0,
2006a3114836SGerry Liu dr_pre_detach_io,
2007a3114836SGerry Liu dr_detach_io,
2008a3114836SGerry Liu dr_post_detach_io,
2009a3114836SGerry Liu dr_detach_update_state);
2010a3114836SGerry Liu
2011a3114836SGerry Liu dr_unlock_status(bp);
2012a3114836SGerry Liu
2013a3114836SGerry Liu (void) dr_dev_walk(hp, SBD_COMP_CPU, 0,
2014a3114836SGerry Liu dr_pre_detach_cpu,
2015a3114836SGerry Liu dr_detach_cpu,
2016a3114836SGerry Liu dr_post_detach_cpu,
2017a3114836SGerry Liu dr_detach_update_state);
2018a3114836SGerry Liu
2019a3114836SGerry Liu (void) dr_dev_walk(hp, SBD_COMP_MEM, 0,
2020a3114836SGerry Liu dr_pre_detach_mem,
2021a3114836SGerry Liu dr_detach_mem,
2022a3114836SGerry Liu dr_post_detach_mem,
2023a3114836SGerry Liu dr_detach_update_state);
2024a3114836SGerry Liu
2025a3114836SGerry Liu return (0);
2026a3114836SGerry Liu }
2027a3114836SGerry Liu
2028a3114836SGerry Liu static void
dr_dev_cancel(dr_handle_t * hp)2029a3114836SGerry Liu dr_dev_cancel(dr_handle_t *hp)
2030a3114836SGerry Liu {
2031a3114836SGerry Liu int i;
2032a3114836SGerry Liu dr_devset_t devset;
2033a3114836SGerry Liu dr_board_t *bp = hp->h_bd;
2034a3114836SGerry Liu static fn_t f = "dr_dev_cancel";
2035a3114836SGerry Liu
2036a3114836SGerry Liu PR_ALL("%s...\n", f);
2037a3114836SGerry Liu
2038a3114836SGerry Liu /*
2039a3114836SGerry Liu * Only devices which have been "released" are
2040a3114836SGerry Liu * subject to cancellation.
2041a3114836SGerry Liu */
2042a3114836SGerry Liu devset = hp->h_devset & DR_DEVS_RELEASED(bp);
2043a3114836SGerry Liu
2044a3114836SGerry Liu /*
2045a3114836SGerry Liu * Nothing to do for CPUs or IO other than change back
2046a3114836SGerry Liu * their state.
2047a3114836SGerry Liu */
2048a3114836SGerry Liu for (i = 0; i < MAX_CPU_UNITS_PER_BOARD; i++) {
2049a3114836SGerry Liu dr_cpu_unit_t *cp;
2050a3114836SGerry Liu dr_state_t nstate;
2051a3114836SGerry Liu
2052a3114836SGerry Liu if (!DEVSET_IN_SET(devset, SBD_COMP_CPU, i))
2053a3114836SGerry Liu continue;
2054a3114836SGerry Liu
2055a3114836SGerry Liu cp = dr_get_cpu_unit(bp, i);
2056a3114836SGerry Liu if (dr_cancel_cpu(cp) == 0)
2057a3114836SGerry Liu nstate = DR_STATE_CONFIGURED;
2058a3114836SGerry Liu else
2059a3114836SGerry Liu nstate = DR_STATE_FATAL;
2060a3114836SGerry Liu
2061a3114836SGerry Liu dr_device_transition(&cp->sbc_cm, nstate);
2062a3114836SGerry Liu }
2063a3114836SGerry Liu
2064a3114836SGerry Liu for (i = 0; i < MAX_IO_UNITS_PER_BOARD; i++) {
2065a3114836SGerry Liu dr_io_unit_t *ip;
2066a3114836SGerry Liu
2067a3114836SGerry Liu if (!DEVSET_IN_SET(devset, SBD_COMP_IO, i))
2068a3114836SGerry Liu continue;
2069a3114836SGerry Liu ip = dr_get_io_unit(bp, i);
2070a3114836SGerry Liu dr_device_transition(&ip->sbi_cm, DR_STATE_CONFIGURED);
2071a3114836SGerry Liu }
2072a3114836SGerry Liu for (i = 0; i < MAX_MEM_UNITS_PER_BOARD; i++) {
2073a3114836SGerry Liu dr_mem_unit_t *mp;
2074a3114836SGerry Liu dr_state_t nstate;
2075a3114836SGerry Liu
2076a3114836SGerry Liu if (!DEVSET_IN_SET(devset, SBD_COMP_MEM, i))
2077a3114836SGerry Liu continue;
2078a3114836SGerry Liu
2079a3114836SGerry Liu mp = dr_get_mem_unit(bp, i);
2080a3114836SGerry Liu if (dr_cancel_mem(mp) == 0)
2081a3114836SGerry Liu nstate = DR_STATE_CONFIGURED;
2082a3114836SGerry Liu else
2083a3114836SGerry Liu nstate = DR_STATE_FATAL;
2084a3114836SGerry Liu
2085a3114836SGerry Liu dr_device_transition(&mp->sbm_cm, nstate);
2086a3114836SGerry Liu }
2087a3114836SGerry Liu
2088a3114836SGerry Liu PR_ALL("%s: unreleasing devset (0x%x)\n", f, (uint_t)devset);
2089a3114836SGerry Liu
2090a3114836SGerry Liu DR_DEVS_CANCEL(bp, devset);
2091a3114836SGerry Liu
2092a3114836SGerry Liu if (DR_DEVS_RELEASED(bp) == 0) {
2093a3114836SGerry Liu dr_state_t new_state;
2094a3114836SGerry Liu /*
2095a3114836SGerry Liu * If the board no longer has any released devices
2096a3114836SGerry Liu * than transfer it back to the CONFIG/PARTIAL state.
2097a3114836SGerry Liu */
2098a3114836SGerry Liu if (DR_DEVS_ATTACHED(bp) == DR_DEVS_PRESENT(bp))
2099a3114836SGerry Liu new_state = DR_STATE_CONFIGURED;
2100a3114836SGerry Liu else
2101a3114836SGerry Liu new_state = DR_STATE_PARTIAL;
2102a3114836SGerry Liu if (bp->b_state != new_state) {
2103a3114836SGerry Liu dr_board_transition(bp, new_state);
2104a3114836SGerry Liu }
2105a3114836SGerry Liu hp->h_bd->b_ostate = SBD_STAT_CONFIGURED;
2106a3114836SGerry Liu hp->h_bd->b_busy = 0;
2107a3114836SGerry Liu (void) drv_getparm(TIME, (void *)&hp->h_bd->b_time);
2108a3114836SGerry Liu }
2109a3114836SGerry Liu }
2110a3114836SGerry Liu
2111a3114836SGerry Liu static int
dr_dev_status(dr_handle_t * hp)2112a3114836SGerry Liu dr_dev_status(dr_handle_t *hp)
2113a3114836SGerry Liu {
2114a3114836SGerry Liu int nstat, mode, ncm, sz, pbsz, pnstat;
2115a3114836SGerry Liu dr_handle_t *shp;
2116a3114836SGerry Liu dr_devset_t devset = 0;
2117a3114836SGerry Liu sbd_stat_t *dstatp = NULL;
2118a3114836SGerry Liu sbd_dev_stat_t *devstatp;
2119a3114836SGerry Liu dr_board_t *bp;
2120a3114836SGerry Liu drmach_status_t pstat;
2121a3114836SGerry Liu int rv = 0;
2122a3114836SGerry Liu
2123a3114836SGerry Liu #ifdef _MULTI_DATAMODEL
2124a3114836SGerry Liu int sz32 = 0;
2125a3114836SGerry Liu #endif /* _MULTI_DATAMODEL */
2126a3114836SGerry Liu
2127a3114836SGerry Liu static fn_t f = "dr_dev_status";
2128a3114836SGerry Liu
2129a3114836SGerry Liu PR_ALL("%s...\n", f);
2130a3114836SGerry Liu
2131a3114836SGerry Liu mode = hp->h_mode;
2132a3114836SGerry Liu shp = hp;
2133a3114836SGerry Liu devset = shp->h_devset;
2134a3114836SGerry Liu bp = hp->h_bd;
2135a3114836SGerry Liu
2136a3114836SGerry Liu /*
2137a3114836SGerry Liu * Block out disconnect, unassign, IO unconfigure and
2138a3114836SGerry Liu * devinfo branch creation during status.
2139a3114836SGerry Liu */
2140a3114836SGerry Liu mutex_enter(&bp->b_slock);
2141a3114836SGerry Liu while (bp->b_sflags & DR_BSLOCK) {
2142a3114836SGerry Liu if (cv_wait_sig(&bp->b_scv, &bp->b_slock) == 0) {
2143a3114836SGerry Liu mutex_exit(&bp->b_slock);
2144a3114836SGerry Liu return (EINTR);
2145a3114836SGerry Liu }
2146a3114836SGerry Liu }
2147a3114836SGerry Liu bp->b_sflags |= DR_BSLOCK;
2148a3114836SGerry Liu mutex_exit(&bp->b_slock);
2149a3114836SGerry Liu
2150a3114836SGerry Liu ncm = 1;
2151a3114836SGerry Liu if (hp->h_sbdcmd.cmd_cm.c_id.c_type == SBD_COMP_NONE) {
2152a3114836SGerry Liu if (dr_cmd_flags(hp) & SBD_FLAG_ALLCMP) {
2153a3114836SGerry Liu /*
2154a3114836SGerry Liu * Calculate the maximum number of components possible
2155a3114836SGerry Liu * for a board. This number will be used to size the
2156a3114836SGerry Liu * status scratch buffer used by board and component
2157a3114836SGerry Liu * status functions.
2158a3114836SGerry Liu * This buffer may differ in size from what is provided
2159a3114836SGerry Liu * by the plugin, since the known component set on the
2160a3114836SGerry Liu * board may change between the plugin's GETNCM call, and
2161a3114836SGerry Liu * the status call. Sizing will be adjusted to the plugin's
2162a3114836SGerry Liu * receptacle buffer at copyout time.
2163a3114836SGerry Liu */
2164a3114836SGerry Liu ncm = MAX_CPU_UNITS_PER_BOARD +
2165a3114836SGerry Liu MAX_MEM_UNITS_PER_BOARD +
2166a3114836SGerry Liu MAX_IO_UNITS_PER_BOARD;
2167a3114836SGerry Liu
2168a3114836SGerry Liu } else {
2169a3114836SGerry Liu /*
2170a3114836SGerry Liu * In the case of c_type == SBD_COMP_NONE, and
2171a3114836SGerry Liu * SBD_FLAG_ALLCMP not specified, only the board
2172a3114836SGerry Liu * info is to be returned, no components.
2173a3114836SGerry Liu */
2174a3114836SGerry Liu ncm = 0;
2175a3114836SGerry Liu devset = 0;
2176a3114836SGerry Liu }
2177a3114836SGerry Liu }
2178a3114836SGerry Liu
2179a3114836SGerry Liu sz = sizeof (sbd_stat_t);
2180a3114836SGerry Liu if (ncm > 1)
2181a3114836SGerry Liu sz += sizeof (sbd_dev_stat_t) * (ncm - 1);
2182a3114836SGerry Liu
2183a3114836SGerry Liu
2184a3114836SGerry Liu pbsz = (int)hp->h_sbdcmd.cmd_stat.s_nbytes;
2185a3114836SGerry Liu pnstat = (pbsz - sizeof (sbd_stat_t)) / sizeof (sbd_dev_stat_t);
2186a3114836SGerry Liu
2187a3114836SGerry Liu /*
2188a3114836SGerry Liu * s_nbytes describes the size of the preallocated user
2189a3114836SGerry Liu * buffer into which the application is execting to
2190a3114836SGerry Liu * receive the sbd_stat_t and sbd_dev_stat_t structures.
2191a3114836SGerry Liu */
2192a3114836SGerry Liu
2193a3114836SGerry Liu #ifdef _MULTI_DATAMODEL
2194a3114836SGerry Liu
2195a3114836SGerry Liu /*
2196a3114836SGerry Liu * More buffer space is required for the 64bit to 32bit
2197a3114836SGerry Liu * conversion of data structures.
2198a3114836SGerry Liu */
2199a3114836SGerry Liu if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
2200a3114836SGerry Liu sz32 = sizeof (sbd_stat32_t);
2201a3114836SGerry Liu if (ncm > 1)
2202a3114836SGerry Liu sz32 += sizeof (sbd_dev_stat32_t) * (ncm - 1);
2203a3114836SGerry Liu pnstat = (pbsz - sizeof (sbd_stat32_t))/
2204a3114836SGerry Liu sizeof (sbd_dev_stat32_t);
2205a3114836SGerry Liu }
2206a3114836SGerry Liu
2207a3114836SGerry Liu sz += sz32;
2208a3114836SGerry Liu #endif
2209a3114836SGerry Liu /*
2210a3114836SGerry Liu * Since one sbd_dev_stat_t is included in the sbd_stat_t,
2211a3114836SGerry Liu * increment the plugin's nstat count.
2212a3114836SGerry Liu */
2213a3114836SGerry Liu ++pnstat;
2214a3114836SGerry Liu
2215a3114836SGerry Liu if (bp->b_id == 0) {
2216a3114836SGerry Liu bzero(&pstat, sizeof (pstat));
2217a3114836SGerry Liu } else {
2218a3114836SGerry Liu sbd_error_t *err;
2219a3114836SGerry Liu
2220a3114836SGerry Liu err = drmach_status(bp->b_id, &pstat);
2221a3114836SGerry Liu if (err) {
2222a3114836SGerry Liu DRERR_SET_C(&hp->h_err, &err);
2223a3114836SGerry Liu rv = EIO;
2224a3114836SGerry Liu goto status_done;
2225a3114836SGerry Liu }
2226a3114836SGerry Liu }
2227a3114836SGerry Liu
2228a3114836SGerry Liu dstatp = (sbd_stat_t *)(void *)GETSTRUCT(char, sz);
2229a3114836SGerry Liu
2230a3114836SGerry Liu devstatp = &dstatp->s_stat[0];
2231a3114836SGerry Liu
2232a3114836SGerry Liu dstatp->s_board = bp->b_num;
2233a3114836SGerry Liu
2234a3114836SGerry Liu /*
2235a3114836SGerry Liu * Detect transitions between empty and disconnected.
2236a3114836SGerry Liu */
2237a3114836SGerry Liu if (!pstat.empty && (bp->b_rstate == SBD_STAT_EMPTY))
2238a3114836SGerry Liu bp->b_rstate = SBD_STAT_DISCONNECTED;
2239a3114836SGerry Liu else if (pstat.empty && (bp->b_rstate == SBD_STAT_DISCONNECTED))
2240a3114836SGerry Liu bp->b_rstate = SBD_STAT_EMPTY;
2241a3114836SGerry Liu
2242a3114836SGerry Liu dstatp->s_rstate = bp->b_rstate;
2243a3114836SGerry Liu dstatp->s_ostate = bp->b_ostate;
2244a3114836SGerry Liu dstatp->s_cond = bp->b_cond = pstat.cond;
2245a3114836SGerry Liu dstatp->s_busy = bp->b_busy | pstat.busy;
2246a3114836SGerry Liu dstatp->s_time = bp->b_time;
2247a3114836SGerry Liu dstatp->s_power = pstat.powered;
2248a3114836SGerry Liu dstatp->s_assigned = bp->b_assigned = pstat.assigned;
2249a3114836SGerry Liu dstatp->s_nstat = nstat = 0;
2250a3114836SGerry Liu bcopy(&pstat.type[0], &dstatp->s_type[0], SBD_TYPE_LEN);
2251a3114836SGerry Liu bcopy(&pstat.info[0], &dstatp->s_info[0], SBD_MAX_INFO);
2252a3114836SGerry Liu
2253a3114836SGerry Liu devset &= DR_DEVS_PRESENT(bp);
2254a3114836SGerry Liu if (devset == 0) {
2255a3114836SGerry Liu /*
2256a3114836SGerry Liu * No device chosen.
2257a3114836SGerry Liu */
2258a3114836SGerry Liu PR_ALL("%s: no device present\n", f);
2259a3114836SGerry Liu }
2260a3114836SGerry Liu
2261a3114836SGerry Liu if (DEVSET_IN_SET(devset, SBD_COMP_CPU, DEVSET_ANYUNIT))
2262a3114836SGerry Liu if ((nstat = dr_cpu_status(hp, devset, devstatp)) > 0) {
2263a3114836SGerry Liu dstatp->s_nstat += nstat;
2264a3114836SGerry Liu devstatp += nstat;
2265a3114836SGerry Liu }
2266a3114836SGerry Liu
2267a3114836SGerry Liu if (DEVSET_IN_SET(devset, SBD_COMP_MEM, DEVSET_ANYUNIT))
2268a3114836SGerry Liu if ((nstat = dr_mem_status(hp, devset, devstatp)) > 0) {
2269a3114836SGerry Liu dstatp->s_nstat += nstat;
2270a3114836SGerry Liu devstatp += nstat;
2271a3114836SGerry Liu }
2272a3114836SGerry Liu
2273a3114836SGerry Liu if (DEVSET_IN_SET(devset, SBD_COMP_IO, DEVSET_ANYUNIT))
2274a3114836SGerry Liu if ((nstat = dr_io_status(hp, devset, devstatp)) > 0) {
2275a3114836SGerry Liu dstatp->s_nstat += nstat;
2276a3114836SGerry Liu devstatp += nstat;
2277a3114836SGerry Liu }
2278a3114836SGerry Liu
2279a3114836SGerry Liu /*
2280a3114836SGerry Liu * Due to a possible change in number of components between
2281a3114836SGerry Liu * the time of plugin's GETNCM call and now, there may be
2282a3114836SGerry Liu * more or less components than the plugin's buffer can
2283a3114836SGerry Liu * hold. Adjust s_nstat accordingly.
2284a3114836SGerry Liu */
2285a3114836SGerry Liu
2286a3114836SGerry Liu dstatp->s_nstat = dstatp->s_nstat > pnstat ? pnstat : dstatp->s_nstat;
2287a3114836SGerry Liu
2288a3114836SGerry Liu #ifdef _MULTI_DATAMODEL
2289a3114836SGerry Liu if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
2290a3114836SGerry Liu int i, j;
2291a3114836SGerry Liu sbd_stat32_t *dstat32p;
2292a3114836SGerry Liu
2293a3114836SGerry Liu dstat32p = (sbd_stat32_t *)devstatp;
2294a3114836SGerry Liu
2295a3114836SGerry Liu /* Alignment Paranoia */
2296a3114836SGerry Liu if ((ulong_t)dstat32p & 0x1) {
2297a3114836SGerry Liu PR_ALL("%s: alignment: sz=0x%lx dstat32p=0x%p\n",
2298a3114836SGerry Liu f, sizeof (sbd_stat32_t), (void *)dstat32p);
2299a3114836SGerry Liu DR_OP_INTERNAL_ERROR(hp);
2300a3114836SGerry Liu rv = EINVAL;
2301a3114836SGerry Liu goto status_done;
2302a3114836SGerry Liu }
2303a3114836SGerry Liu
2304a3114836SGerry Liu /* paranoia: detect buffer overrun */
2305a3114836SGerry Liu if ((caddr_t)&dstat32p->s_stat[dstatp->s_nstat] >
2306a3114836SGerry Liu ((caddr_t)dstatp) + sz) {
2307a3114836SGerry Liu DR_OP_INTERNAL_ERROR(hp);
2308a3114836SGerry Liu rv = EINVAL;
2309a3114836SGerry Liu goto status_done;
2310a3114836SGerry Liu }
2311a3114836SGerry Liu
2312a3114836SGerry Liu /* copy sbd_stat_t structure members */
2313a3114836SGerry Liu #define _SBD_STAT(t, m) dstat32p->m = (t)dstatp->m
2314a3114836SGerry Liu _SBD_STAT(int32_t, s_board);
2315a3114836SGerry Liu _SBD_STAT(int32_t, s_rstate);
2316a3114836SGerry Liu _SBD_STAT(int32_t, s_ostate);
2317a3114836SGerry Liu _SBD_STAT(int32_t, s_cond);
2318a3114836SGerry Liu _SBD_STAT(int32_t, s_busy);
2319a3114836SGerry Liu _SBD_STAT(time32_t, s_time);
2320a3114836SGerry Liu _SBD_STAT(uint32_t, s_power);
2321a3114836SGerry Liu _SBD_STAT(uint32_t, s_assigned);
2322a3114836SGerry Liu _SBD_STAT(int32_t, s_nstat);
2323a3114836SGerry Liu bcopy(&dstatp->s_type[0], &dstat32p->s_type[0],
2324a3114836SGerry Liu SBD_TYPE_LEN);
2325a3114836SGerry Liu bcopy(&dstatp->s_info[0], &dstat32p->s_info[0],
2326a3114836SGerry Liu SBD_MAX_INFO);
2327a3114836SGerry Liu #undef _SBD_STAT
2328a3114836SGerry Liu
2329a3114836SGerry Liu for (i = 0; i < dstatp->s_nstat; i++) {
2330a3114836SGerry Liu sbd_dev_stat_t *dsp = &dstatp->s_stat[i];
2331a3114836SGerry Liu sbd_dev_stat32_t *ds32p = &dstat32p->s_stat[i];
2332a3114836SGerry Liu #define _SBD_DEV_STAT(t, m) ds32p->m = (t)dsp->m
2333a3114836SGerry Liu
2334a3114836SGerry Liu /* copy sbd_cm_stat_t structure members */
2335a3114836SGerry Liu _SBD_DEV_STAT(int32_t, ds_type);
2336a3114836SGerry Liu _SBD_DEV_STAT(int32_t, ds_unit);
2337a3114836SGerry Liu _SBD_DEV_STAT(int32_t, ds_ostate);
2338a3114836SGerry Liu _SBD_DEV_STAT(int32_t, ds_cond);
2339a3114836SGerry Liu _SBD_DEV_STAT(int32_t, ds_busy);
2340a3114836SGerry Liu _SBD_DEV_STAT(int32_t, ds_suspend);
2341a3114836SGerry Liu _SBD_DEV_STAT(time32_t, ds_time);
2342a3114836SGerry Liu bcopy(&dsp->ds_name[0], &ds32p->ds_name[0],
2343a3114836SGerry Liu OBP_MAXPROPNAME);
2344a3114836SGerry Liu
2345a3114836SGerry Liu switch (dsp->ds_type) {
2346a3114836SGerry Liu case SBD_COMP_CPU:
2347a3114836SGerry Liu /* copy sbd_cpu_stat_t structure members */
2348a3114836SGerry Liu _SBD_DEV_STAT(int32_t, d_cpu.cs_isbootproc);
2349a3114836SGerry Liu _SBD_DEV_STAT(int32_t, d_cpu.cs_cpuid);
2350a3114836SGerry Liu _SBD_DEV_STAT(int32_t, d_cpu.cs_speed);
2351a3114836SGerry Liu _SBD_DEV_STAT(int32_t, d_cpu.cs_ecache);
2352a3114836SGerry Liu break;
2353a3114836SGerry Liu
2354a3114836SGerry Liu case SBD_COMP_MEM:
2355a3114836SGerry Liu /* copy sbd_mem_stat_t structure members */
2356a3114836SGerry Liu _SBD_DEV_STAT(int32_t, d_mem.ms_interleave);
2357a3114836SGerry Liu _SBD_DEV_STAT(uint32_t, d_mem.ms_basepfn);
2358a3114836SGerry Liu _SBD_DEV_STAT(uint32_t, d_mem.ms_totpages);
2359a3114836SGerry Liu _SBD_DEV_STAT(uint32_t, d_mem.ms_detpages);
2360a3114836SGerry Liu _SBD_DEV_STAT(int32_t, d_mem.ms_pageslost);
2361a3114836SGerry Liu _SBD_DEV_STAT(uint32_t, d_mem.ms_managed_pages);
2362a3114836SGerry Liu _SBD_DEV_STAT(uint32_t, d_mem.ms_noreloc_pages);
2363a3114836SGerry Liu _SBD_DEV_STAT(uint32_t, d_mem.ms_noreloc_first);
2364a3114836SGerry Liu _SBD_DEV_STAT(uint32_t, d_mem.ms_noreloc_last);
2365a3114836SGerry Liu _SBD_DEV_STAT(int32_t, d_mem.ms_cage_enabled);
2366a3114836SGerry Liu _SBD_DEV_STAT(int32_t, d_mem.ms_peer_is_target);
2367a3114836SGerry Liu bcopy(&dsp->d_mem.ms_peer_ap_id[0],
2368a3114836SGerry Liu &ds32p->d_mem.ms_peer_ap_id[0],
2369a3114836SGerry Liu sizeof (ds32p->d_mem.ms_peer_ap_id));
2370a3114836SGerry Liu break;
2371a3114836SGerry Liu
2372a3114836SGerry Liu case SBD_COMP_IO:
2373a3114836SGerry Liu /* copy sbd_io_stat_t structure members */
2374a3114836SGerry Liu _SBD_DEV_STAT(int32_t, d_io.is_referenced);
2375a3114836SGerry Liu _SBD_DEV_STAT(int32_t, d_io.is_unsafe_count);
2376a3114836SGerry Liu
2377a3114836SGerry Liu for (j = 0; j < SBD_MAX_UNSAFE; j++)
2378a3114836SGerry Liu _SBD_DEV_STAT(int32_t,
2379a3114836SGerry Liu d_io.is_unsafe_list[j]);
2380a3114836SGerry Liu
2381a3114836SGerry Liu bcopy(&dsp->d_io.is_pathname[0],
2382a3114836SGerry Liu &ds32p->d_io.is_pathname[0], MAXPATHLEN);
2383a3114836SGerry Liu break;
2384a3114836SGerry Liu
2385a3114836SGerry Liu case SBD_COMP_CMP:
2386a3114836SGerry Liu /* copy sbd_cmp_stat_t structure members */
2387a3114836SGerry Liu bcopy(&dsp->d_cmp.ps_cpuid[0],
2388a3114836SGerry Liu &ds32p->d_cmp.ps_cpuid[0],
2389a3114836SGerry Liu sizeof (ds32p->d_cmp.ps_cpuid));
2390a3114836SGerry Liu _SBD_DEV_STAT(int32_t, d_cmp.ps_ncores);
2391a3114836SGerry Liu _SBD_DEV_STAT(int32_t, d_cmp.ps_speed);
2392a3114836SGerry Liu _SBD_DEV_STAT(int32_t, d_cmp.ps_ecache);
2393a3114836SGerry Liu break;
2394a3114836SGerry Liu
2395a3114836SGerry Liu default:
2396a3114836SGerry Liu cmn_err(CE_WARN, "%s: unknown dev type (%d)",
2397a3114836SGerry Liu f, (int)dsp->ds_type);
2398a3114836SGerry Liu rv = EFAULT;
2399a3114836SGerry Liu goto status_done;
2400a3114836SGerry Liu }
2401a3114836SGerry Liu #undef _SBD_DEV_STAT
2402a3114836SGerry Liu }
2403a3114836SGerry Liu
2404a3114836SGerry Liu
2405a3114836SGerry Liu if (ddi_copyout((void *)dstat32p,
2406a3114836SGerry Liu hp->h_sbdcmd.cmd_stat.s_statp, pbsz, mode) != 0) {
2407a3114836SGerry Liu cmn_err(CE_WARN,
2408a3114836SGerry Liu "%s: failed to copyout status "
2409a3114836SGerry Liu "for board %d", f, bp->b_num);
2410a3114836SGerry Liu rv = EFAULT;
2411a3114836SGerry Liu goto status_done;
2412a3114836SGerry Liu }
2413a3114836SGerry Liu } else
2414a3114836SGerry Liu #endif /* _MULTI_DATAMODEL */
2415a3114836SGerry Liu
2416a3114836SGerry Liu if (ddi_copyout((void *)dstatp, hp->h_sbdcmd.cmd_stat.s_statp,
2417a3114836SGerry Liu pbsz, mode) != 0) {
2418a3114836SGerry Liu cmn_err(CE_WARN,
2419a3114836SGerry Liu "%s: failed to copyout status for board %d",
2420a3114836SGerry Liu f, bp->b_num);
2421a3114836SGerry Liu rv = EFAULT;
2422a3114836SGerry Liu goto status_done;
2423a3114836SGerry Liu }
2424a3114836SGerry Liu
2425a3114836SGerry Liu status_done:
2426a3114836SGerry Liu if (dstatp != NULL)
2427a3114836SGerry Liu FREESTRUCT(dstatp, char, sz);
2428a3114836SGerry Liu
2429a3114836SGerry Liu dr_unlock_status(bp);
2430a3114836SGerry Liu
2431a3114836SGerry Liu return (rv);
2432a3114836SGerry Liu }
2433a3114836SGerry Liu
2434a3114836SGerry Liu static int
dr_get_ncm(dr_handle_t * hp)2435a3114836SGerry Liu dr_get_ncm(dr_handle_t *hp)
2436a3114836SGerry Liu {
2437a3114836SGerry Liu int i;
2438a3114836SGerry Liu int ncm = 0;
2439a3114836SGerry Liu dr_devset_t devset;
2440a3114836SGerry Liu
2441a3114836SGerry Liu devset = DR_DEVS_PRESENT(hp->h_bd);
2442a3114836SGerry Liu if (hp->h_sbdcmd.cmd_cm.c_id.c_type != SBD_COMP_NONE)
2443a3114836SGerry Liu devset &= DEVSET(hp->h_sbdcmd.cmd_cm.c_id.c_type,
2444a3114836SGerry Liu DEVSET_ANYUNIT);
2445a3114836SGerry Liu
2446a3114836SGerry Liu /*
2447a3114836SGerry Liu * Handle CPUs first to deal with possible CMP
2448a3114836SGerry Liu * devices. If the CPU is a CMP, we need to only
2449a3114836SGerry Liu * increment ncm once even if there are multiple
2450a3114836SGerry Liu * cores for that CMP present in the devset.
2451a3114836SGerry Liu */
2452a3114836SGerry Liu for (i = 0; i < MAX_CMP_UNITS_PER_BOARD; i++) {
2453a3114836SGerry Liu if (devset & DEVSET(SBD_COMP_CMP, i)) {
2454a3114836SGerry Liu ncm++;
2455a3114836SGerry Liu }
2456a3114836SGerry Liu }
2457a3114836SGerry Liu
2458a3114836SGerry Liu /* eliminate the CPU information from the devset */
2459a3114836SGerry Liu devset &= ~(DEVSET(SBD_COMP_CMP, DEVSET_ANYUNIT));
2460a3114836SGerry Liu
2461a3114836SGerry Liu for (i = 0; i < (sizeof (dr_devset_t) * 8); i++) {
2462a3114836SGerry Liu ncm += devset & 0x1;
2463a3114836SGerry Liu devset >>= 1;
2464a3114836SGerry Liu }
2465a3114836SGerry Liu
2466a3114836SGerry Liu return (ncm);
2467a3114836SGerry Liu }
2468a3114836SGerry Liu
2469a3114836SGerry Liu /* used by dr_mem.c */
2470a3114836SGerry Liu /* TODO: eliminate dr_boardlist */
2471a3114836SGerry Liu dr_board_t *
dr_lookup_board(int board_num)2472a3114836SGerry Liu dr_lookup_board(int board_num)
2473a3114836SGerry Liu {
2474a3114836SGerry Liu dr_board_t *bp;
2475a3114836SGerry Liu
2476a3114836SGerry Liu ASSERT(board_num >= 0 && board_num < MAX_BOARDS);
2477a3114836SGerry Liu
2478a3114836SGerry Liu bp = &dr_boardlist[board_num];
2479a3114836SGerry Liu ASSERT(bp->b_num == board_num);
2480a3114836SGerry Liu
2481a3114836SGerry Liu return (bp);
2482a3114836SGerry Liu }
2483a3114836SGerry Liu
2484a3114836SGerry Liu static dr_dev_unit_t *
dr_get_dev_unit(dr_board_t * bp,sbd_comp_type_t nt,int unit_num)2485a3114836SGerry Liu dr_get_dev_unit(dr_board_t *bp, sbd_comp_type_t nt, int unit_num)
2486a3114836SGerry Liu {
2487a3114836SGerry Liu dr_dev_unit_t *dp;
2488a3114836SGerry Liu
2489a3114836SGerry Liu dp = DR_GET_BOARD_DEVUNIT(bp, nt, unit_num);
2490a3114836SGerry Liu ASSERT(dp->du_common.sbdev_bp == bp);
2491a3114836SGerry Liu ASSERT(dp->du_common.sbdev_unum == unit_num);
2492a3114836SGerry Liu ASSERT(dp->du_common.sbdev_type == nt);
2493a3114836SGerry Liu
2494a3114836SGerry Liu return (dp);
2495a3114836SGerry Liu }
2496a3114836SGerry Liu
2497a3114836SGerry Liu dr_cpu_unit_t *
dr_get_cpu_unit(dr_board_t * bp,int unit_num)2498a3114836SGerry Liu dr_get_cpu_unit(dr_board_t *bp, int unit_num)
2499a3114836SGerry Liu {
2500a3114836SGerry Liu dr_dev_unit_t *dp;
2501a3114836SGerry Liu
2502a3114836SGerry Liu ASSERT(unit_num >= 0 && unit_num < MAX_CPU_UNITS_PER_BOARD);
2503a3114836SGerry Liu
2504a3114836SGerry Liu dp = dr_get_dev_unit(bp, SBD_COMP_CPU, unit_num);
2505a3114836SGerry Liu return (&dp->du_cpu);
2506a3114836SGerry Liu }
2507a3114836SGerry Liu
2508a3114836SGerry Liu dr_mem_unit_t *
dr_get_mem_unit(dr_board_t * bp,int unit_num)2509a3114836SGerry Liu dr_get_mem_unit(dr_board_t *bp, int unit_num)
2510a3114836SGerry Liu {
2511a3114836SGerry Liu dr_dev_unit_t *dp;
2512a3114836SGerry Liu
2513a3114836SGerry Liu ASSERT(unit_num >= 0 && unit_num < MAX_MEM_UNITS_PER_BOARD);
2514a3114836SGerry Liu
2515a3114836SGerry Liu dp = dr_get_dev_unit(bp, SBD_COMP_MEM, unit_num);
2516a3114836SGerry Liu return (&dp->du_mem);
2517a3114836SGerry Liu }
2518a3114836SGerry Liu
2519a3114836SGerry Liu dr_io_unit_t *
dr_get_io_unit(dr_board_t * bp,int unit_num)2520a3114836SGerry Liu dr_get_io_unit(dr_board_t *bp, int unit_num)
2521a3114836SGerry Liu {
2522a3114836SGerry Liu dr_dev_unit_t *dp;
2523a3114836SGerry Liu
2524a3114836SGerry Liu ASSERT(unit_num >= 0 && unit_num < MAX_IO_UNITS_PER_BOARD);
2525a3114836SGerry Liu
2526a3114836SGerry Liu dp = dr_get_dev_unit(bp, SBD_COMP_IO, unit_num);
2527a3114836SGerry Liu return (&dp->du_io);
2528a3114836SGerry Liu }
2529a3114836SGerry Liu
2530a3114836SGerry Liu dr_common_unit_t *
dr_get_common_unit(dr_board_t * bp,sbd_comp_type_t nt,int unum)2531a3114836SGerry Liu dr_get_common_unit(dr_board_t *bp, sbd_comp_type_t nt, int unum)
2532a3114836SGerry Liu {
2533a3114836SGerry Liu dr_dev_unit_t *dp;
2534a3114836SGerry Liu
2535a3114836SGerry Liu dp = dr_get_dev_unit(bp, nt, unum);
2536a3114836SGerry Liu return (&dp->du_common);
2537a3114836SGerry Liu }
2538a3114836SGerry Liu
2539a3114836SGerry Liu static dr_devset_t
dr_dev2devset(sbd_comp_id_t * cid)2540a3114836SGerry Liu dr_dev2devset(sbd_comp_id_t *cid)
2541a3114836SGerry Liu {
2542a3114836SGerry Liu static fn_t f = "dr_dev2devset";
2543a3114836SGerry Liu
2544a3114836SGerry Liu dr_devset_t devset;
2545a3114836SGerry Liu int unit = cid->c_unit;
2546a3114836SGerry Liu
2547a3114836SGerry Liu switch (cid->c_type) {
2548a3114836SGerry Liu case SBD_COMP_NONE:
2549a3114836SGerry Liu devset = DEVSET(SBD_COMP_CPU, DEVSET_ANYUNIT);
2550a3114836SGerry Liu devset |= DEVSET(SBD_COMP_MEM, DEVSET_ANYUNIT);
2551a3114836SGerry Liu devset |= DEVSET(SBD_COMP_IO, DEVSET_ANYUNIT);
2552a3114836SGerry Liu PR_ALL("%s: COMP_NONE devset = " DEVSET_FMT_STR "\n",
2553a3114836SGerry Liu f, DEVSET_FMT_ARG(devset));
2554a3114836SGerry Liu break;
2555a3114836SGerry Liu
2556a3114836SGerry Liu case SBD_COMP_CPU:
2557a3114836SGerry Liu if ((unit > MAX_CPU_UNITS_PER_BOARD) || (unit < 0)) {
2558a3114836SGerry Liu cmn_err(CE_WARN,
2559a3114836SGerry Liu "%s: invalid cpu unit# = %d",
2560a3114836SGerry Liu f, unit);
2561a3114836SGerry Liu devset = 0;
2562a3114836SGerry Liu } else {
2563a3114836SGerry Liu /*
2564a3114836SGerry Liu * Generate a devset that includes all the
2565a3114836SGerry Liu * cores of a CMP device. If this is not a
2566a3114836SGerry Liu * CMP, the extra cores will be eliminated
2567a3114836SGerry Liu * later since they are not present. This is
2568a3114836SGerry Liu * also true for CMP devices that do not have
2569a3114836SGerry Liu * all cores active.
2570a3114836SGerry Liu */
2571a3114836SGerry Liu devset = DEVSET(SBD_COMP_CMP, unit);
2572a3114836SGerry Liu }
2573a3114836SGerry Liu
2574a3114836SGerry Liu PR_ALL("%s: CPU devset = " DEVSET_FMT_STR "\n",
2575a3114836SGerry Liu f, DEVSET_FMT_ARG(devset));
2576a3114836SGerry Liu break;
2577a3114836SGerry Liu
2578a3114836SGerry Liu case SBD_COMP_MEM:
2579a3114836SGerry Liu if (unit == SBD_NULL_UNIT) {
2580a3114836SGerry Liu unit = 0;
2581a3114836SGerry Liu cid->c_unit = 0;
2582a3114836SGerry Liu }
2583a3114836SGerry Liu
2584a3114836SGerry Liu if ((unit > MAX_MEM_UNITS_PER_BOARD) || (unit < 0)) {
2585a3114836SGerry Liu cmn_err(CE_WARN,
2586a3114836SGerry Liu "%s: invalid mem unit# = %d",
2587a3114836SGerry Liu f, unit);
2588a3114836SGerry Liu devset = 0;
2589a3114836SGerry Liu } else
2590a3114836SGerry Liu devset = DEVSET(cid->c_type, unit);
2591a3114836SGerry Liu
2592a3114836SGerry Liu PR_ALL("%s: MEM devset = " DEVSET_FMT_STR "\n",
2593a3114836SGerry Liu f, DEVSET_FMT_ARG(devset));
2594a3114836SGerry Liu break;
2595a3114836SGerry Liu
2596a3114836SGerry Liu case SBD_COMP_IO:
2597a3114836SGerry Liu if ((unit > MAX_IO_UNITS_PER_BOARD) || (unit < 0)) {
2598a3114836SGerry Liu cmn_err(CE_WARN,
2599a3114836SGerry Liu "%s: invalid io unit# = %d",
2600a3114836SGerry Liu f, unit);
2601a3114836SGerry Liu devset = 0;
2602a3114836SGerry Liu } else
2603a3114836SGerry Liu devset = DEVSET(cid->c_type, unit);
2604a3114836SGerry Liu
2605a3114836SGerry Liu PR_ALL("%s: IO devset = " DEVSET_FMT_STR "\n",
2606a3114836SGerry Liu f, DEVSET_FMT_ARG(devset));
2607a3114836SGerry Liu break;
2608a3114836SGerry Liu
2609a3114836SGerry Liu default:
2610a3114836SGerry Liu case SBD_COMP_UNKNOWN:
2611a3114836SGerry Liu devset = 0;
2612a3114836SGerry Liu break;
2613a3114836SGerry Liu }
2614a3114836SGerry Liu
2615a3114836SGerry Liu return (devset);
2616a3114836SGerry Liu }
2617a3114836SGerry Liu
2618a3114836SGerry Liu /*
2619a3114836SGerry Liu * Converts a dynamic attachment point name to a SBD_COMP_* type.
2620a3114836SGerry Liu * Returns SDB_COMP_UNKNOWN if name is not recognized.
2621a3114836SGerry Liu */
2622a3114836SGerry Liu static int
dr_dev_type_to_nt(char * type)2623a3114836SGerry Liu dr_dev_type_to_nt(char *type)
2624a3114836SGerry Liu {
2625a3114836SGerry Liu int i;
2626a3114836SGerry Liu
2627a3114836SGerry Liu for (i = 0; dr_devattr[i].s_nodetype != SBD_COMP_UNKNOWN; i++)
2628a3114836SGerry Liu if (strcmp(dr_devattr[i].s_devtype, type) == 0)
2629a3114836SGerry Liu break;
2630a3114836SGerry Liu
2631a3114836SGerry Liu return (dr_devattr[i].s_nodetype);
2632a3114836SGerry Liu }
2633a3114836SGerry Liu
2634a3114836SGerry Liu /*
2635a3114836SGerry Liu * Converts a SBD_COMP_* type to a dynamic attachment point name.
2636a3114836SGerry Liu * Return NULL if SBD_COMP_ type is not recognized.
2637a3114836SGerry Liu */
2638a3114836SGerry Liu char *
dr_nt_to_dev_type(int nt)2639a3114836SGerry Liu dr_nt_to_dev_type(int nt)
2640a3114836SGerry Liu {
2641a3114836SGerry Liu int i;
2642a3114836SGerry Liu
2643a3114836SGerry Liu for (i = 0; dr_devattr[i].s_nodetype != SBD_COMP_UNKNOWN; i++)
2644a3114836SGerry Liu if (dr_devattr[i].s_nodetype == nt)
2645a3114836SGerry Liu break;
2646a3114836SGerry Liu
2647a3114836SGerry Liu return (dr_devattr[i].s_devtype);
2648a3114836SGerry Liu }
2649a3114836SGerry Liu
2650a3114836SGerry Liu /*
2651a3114836SGerry Liu * State transition policy is that if there is some component for which
2652a3114836SGerry Liu * the state transition is valid, then let it through. The exception is
2653a3114836SGerry Liu * SBD_CMD_DISCONNECT. On disconnect, the state transition must be valid
2654a3114836SGerry Liu * for ALL components.
2655a3114836SGerry Liu * Returns the state that is in error, if any.
2656a3114836SGerry Liu */
2657a3114836SGerry Liu static int
dr_check_transition(dr_board_t * bp,dr_devset_t * devsetp,struct dr_state_trans * transp,int cmd)2658a3114836SGerry Liu dr_check_transition(dr_board_t *bp, dr_devset_t *devsetp,
2659a3114836SGerry Liu struct dr_state_trans *transp, int cmd)
2660a3114836SGerry Liu {
2661a3114836SGerry Liu int s, ut;
2662a3114836SGerry Liu int state_err = 0;
2663a3114836SGerry Liu dr_devset_t devset;
2664a3114836SGerry Liu dr_common_unit_t *cp;
2665a3114836SGerry Liu static fn_t f = "dr_check_transition";
2666a3114836SGerry Liu
2667a3114836SGerry Liu devset = *devsetp;
2668a3114836SGerry Liu
2669a3114836SGerry Liu if (DEVSET_IN_SET(devset, SBD_COMP_CPU, DEVSET_ANYUNIT)) {
2670a3114836SGerry Liu for (ut = 0; ut < MAX_CPU_UNITS_PER_BOARD; ut++) {
2671a3114836SGerry Liu if (DEVSET_IN_SET(devset, SBD_COMP_CPU, ut) == 0)
2672a3114836SGerry Liu continue;
2673a3114836SGerry Liu
2674a3114836SGerry Liu cp = dr_get_common_unit(bp, SBD_COMP_CPU, ut);
2675a3114836SGerry Liu s = (int)cp->sbdev_state;
2676a3114836SGerry Liu if (!DR_DEV_IS_PRESENT(cp)) {
2677a3114836SGerry Liu DEVSET_DEL(devset, SBD_COMP_CPU, ut);
2678a3114836SGerry Liu } else {
2679a3114836SGerry Liu if (transp->x_op[s].x_rv) {
2680a3114836SGerry Liu if (!state_err)
2681a3114836SGerry Liu state_err = s;
2682a3114836SGerry Liu DEVSET_DEL(devset, SBD_COMP_CPU, ut);
2683a3114836SGerry Liu }
2684a3114836SGerry Liu }
2685a3114836SGerry Liu }
2686a3114836SGerry Liu }
2687a3114836SGerry Liu if (DEVSET_IN_SET(devset, SBD_COMP_MEM, DEVSET_ANYUNIT)) {
2688a3114836SGerry Liu for (ut = 0; ut < MAX_MEM_UNITS_PER_BOARD; ut++) {
2689a3114836SGerry Liu if (DEVSET_IN_SET(devset, SBD_COMP_MEM, ut) == 0)
2690a3114836SGerry Liu continue;
2691a3114836SGerry Liu
2692a3114836SGerry Liu cp = dr_get_common_unit(bp, SBD_COMP_MEM, ut);
2693a3114836SGerry Liu s = (int)cp->sbdev_state;
2694a3114836SGerry Liu if (!DR_DEV_IS_PRESENT(cp)) {
2695a3114836SGerry Liu DEVSET_DEL(devset, SBD_COMP_MEM, ut);
2696a3114836SGerry Liu } else {
2697a3114836SGerry Liu if (transp->x_op[s].x_rv) {
2698a3114836SGerry Liu if (!state_err)
2699a3114836SGerry Liu state_err = s;
2700a3114836SGerry Liu DEVSET_DEL(devset, SBD_COMP_MEM, ut);
2701a3114836SGerry Liu }
2702a3114836SGerry Liu }
2703a3114836SGerry Liu }
2704a3114836SGerry Liu }
2705a3114836SGerry Liu if (DEVSET_IN_SET(devset, SBD_COMP_IO, DEVSET_ANYUNIT)) {
2706a3114836SGerry Liu for (ut = 0; ut < MAX_IO_UNITS_PER_BOARD; ut++) {
2707a3114836SGerry Liu if (DEVSET_IN_SET(devset, SBD_COMP_IO, ut) == 0)
2708a3114836SGerry Liu continue;
2709a3114836SGerry Liu
2710a3114836SGerry Liu cp = dr_get_common_unit(bp, SBD_COMP_IO, ut);
2711a3114836SGerry Liu s = (int)cp->sbdev_state;
2712a3114836SGerry Liu if (!DR_DEV_IS_PRESENT(cp)) {
2713a3114836SGerry Liu DEVSET_DEL(devset, SBD_COMP_IO, ut);
2714a3114836SGerry Liu } else {
2715a3114836SGerry Liu if (transp->x_op[s].x_rv) {
2716a3114836SGerry Liu if (!state_err)
2717a3114836SGerry Liu state_err = s;
2718a3114836SGerry Liu DEVSET_DEL(devset, SBD_COMP_IO, ut);
2719a3114836SGerry Liu }
2720a3114836SGerry Liu }
2721a3114836SGerry Liu }
2722a3114836SGerry Liu }
2723a3114836SGerry Liu
2724a3114836SGerry Liu PR_ALL("%s: requested devset = 0x%x, final devset = 0x%x\n",
2725a3114836SGerry Liu f, (uint_t)*devsetp, (uint_t)devset);
2726a3114836SGerry Liu
2727a3114836SGerry Liu *devsetp = devset;
2728a3114836SGerry Liu /*
2729a3114836SGerry Liu * If there are some remaining components for which
2730a3114836SGerry Liu * this state transition is valid, then allow them
2731a3114836SGerry Liu * through, otherwise if none are left then return
2732a3114836SGerry Liu * the state error. The exception is SBD_CMD_DISCONNECT.
2733a3114836SGerry Liu * On disconnect, the state transition must be valid for ALL
2734a3114836SGerry Liu * components.
2735a3114836SGerry Liu */
2736a3114836SGerry Liu if (cmd == SBD_CMD_DISCONNECT)
2737a3114836SGerry Liu return (state_err);
2738a3114836SGerry Liu return (devset ? 0 : state_err);
2739a3114836SGerry Liu }
2740a3114836SGerry Liu
2741a3114836SGerry Liu void
dr_device_transition(dr_common_unit_t * cp,dr_state_t st)2742a3114836SGerry Liu dr_device_transition(dr_common_unit_t *cp, dr_state_t st)
2743a3114836SGerry Liu {
2744a3114836SGerry Liu PR_STATE("%s STATE %s(%d) -> %s(%d)\n",
2745a3114836SGerry Liu cp->sbdev_path,
2746a3114836SGerry Liu state_str[cp->sbdev_state], cp->sbdev_state,
2747a3114836SGerry Liu state_str[st], st);
2748a3114836SGerry Liu
2749a3114836SGerry Liu cp->sbdev_state = st;
2750a3114836SGerry Liu if (st == DR_STATE_CONFIGURED) {
2751a3114836SGerry Liu cp->sbdev_ostate = SBD_STAT_CONFIGURED;
2752a3114836SGerry Liu if (cp->sbdev_bp->b_ostate != SBD_STAT_CONFIGURED) {
2753a3114836SGerry Liu cp->sbdev_bp->b_ostate = SBD_STAT_CONFIGURED;
2754a3114836SGerry Liu (void) drv_getparm(TIME,
2755a3114836SGerry Liu (void *) &cp->sbdev_bp->b_time);
2756a3114836SGerry Liu }
2757a3114836SGerry Liu } else
2758a3114836SGerry Liu cp->sbdev_ostate = SBD_STAT_UNCONFIGURED;
2759a3114836SGerry Liu
2760a3114836SGerry Liu (void) drv_getparm(TIME, (void *) &cp->sbdev_time);
2761a3114836SGerry Liu }
2762a3114836SGerry Liu
2763a3114836SGerry Liu static void
dr_board_transition(dr_board_t * bp,dr_state_t st)2764a3114836SGerry Liu dr_board_transition(dr_board_t *bp, dr_state_t st)
2765a3114836SGerry Liu {
2766a3114836SGerry Liu PR_STATE("BOARD %d STATE: %s(%d) -> %s(%d)\n",
2767a3114836SGerry Liu bp->b_num,
2768a3114836SGerry Liu state_str[bp->b_state], bp->b_state,
2769a3114836SGerry Liu state_str[st], st);
2770a3114836SGerry Liu
2771a3114836SGerry Liu bp->b_state = st;
2772a3114836SGerry Liu }
2773a3114836SGerry Liu
2774a3114836SGerry Liu void
dr_op_err(int ce,dr_handle_t * hp,int code,char * fmt,...)2775a3114836SGerry Liu dr_op_err(int ce, dr_handle_t *hp, int code, char *fmt, ...)
2776a3114836SGerry Liu {
2777a3114836SGerry Liu sbd_error_t *err;
2778a3114836SGerry Liu va_list args;
2779a3114836SGerry Liu
2780a3114836SGerry Liu va_start(args, fmt);
2781a3114836SGerry Liu err = drerr_new_v(code, fmt, args);
2782a3114836SGerry Liu va_end(args);
2783a3114836SGerry Liu
2784a3114836SGerry Liu if (ce != CE_IGNORE)
2785a3114836SGerry Liu sbd_err_log(err, ce);
2786a3114836SGerry Liu
2787a3114836SGerry Liu DRERR_SET_C(&hp->h_err, &err);
2788a3114836SGerry Liu }
2789a3114836SGerry Liu
2790a3114836SGerry Liu void
dr_dev_err(int ce,dr_common_unit_t * cp,int code)2791a3114836SGerry Liu dr_dev_err(int ce, dr_common_unit_t *cp, int code)
2792a3114836SGerry Liu {
2793a3114836SGerry Liu sbd_error_t *err;
2794a3114836SGerry Liu
2795a3114836SGerry Liu err = drerr_new(0, code, cp->sbdev_path, NULL);
2796a3114836SGerry Liu
2797a3114836SGerry Liu if (ce != CE_IGNORE)
2798a3114836SGerry Liu sbd_err_log(err, ce);
2799a3114836SGerry Liu
2800a3114836SGerry Liu DRERR_SET_C(&cp->sbdev_error, &err);
2801a3114836SGerry Liu }
2802a3114836SGerry Liu
2803a3114836SGerry Liu /*
2804a3114836SGerry Liu * A callback routine. Called from the drmach layer as a result of
2805a3114836SGerry Liu * call to drmach_board_find_devices from dr_init_devlists.
2806a3114836SGerry Liu */
2807a3114836SGerry Liu static sbd_error_t *
dr_dev_found(void * data,const char * name,int unum,drmachid_t id)2808a3114836SGerry Liu dr_dev_found(void *data, const char *name, int unum, drmachid_t id)
2809a3114836SGerry Liu {
2810a3114836SGerry Liu dr_board_t *bp = data;
2811a3114836SGerry Liu dr_dev_unit_t *dp;
2812a3114836SGerry Liu int nt;
2813a3114836SGerry Liu static fn_t f = "dr_dev_found";
2814a3114836SGerry Liu
2815a3114836SGerry Liu PR_ALL("%s (board = %d, name = %s, unum = %d, id = %p)...\n",
2816a3114836SGerry Liu f, bp->b_num, name, unum, id);
2817a3114836SGerry Liu
2818a3114836SGerry Liu nt = dr_dev_type_to_nt((char *)name);
2819a3114836SGerry Liu if (nt == SBD_COMP_UNKNOWN) {
2820a3114836SGerry Liu /*
2821a3114836SGerry Liu * this should not happen. When it does, it indicates
2822a3114836SGerry Liu * a missmatch in devices supported by the drmach layer
2823a3114836SGerry Liu * vs devices supported by this layer.
2824a3114836SGerry Liu */
2825a3114836SGerry Liu return (DR_INTERNAL_ERROR());
2826a3114836SGerry Liu }
2827a3114836SGerry Liu
2828a3114836SGerry Liu dp = DR_GET_BOARD_DEVUNIT(bp, nt, unum);
2829a3114836SGerry Liu
2830a3114836SGerry Liu /* sanity check */
2831a3114836SGerry Liu ASSERT(dp->du_common.sbdev_bp == bp);
2832a3114836SGerry Liu ASSERT(dp->du_common.sbdev_unum == unum);
2833a3114836SGerry Liu ASSERT(dp->du_common.sbdev_type == nt);
2834a3114836SGerry Liu
2835a3114836SGerry Liu /* render dynamic attachment point path of this unit */
2836a3114836SGerry Liu (void) snprintf(dp->du_common.sbdev_path,
2837a3114836SGerry Liu sizeof (dp->du_common.sbdev_path), "%s::%s%d",
2838a3114836SGerry Liu bp->b_path, name, DR_UNUM2SBD_UNUM(unum, nt));
2839a3114836SGerry Liu
2840a3114836SGerry Liu dp->du_common.sbdev_id = id;
2841a3114836SGerry Liu DR_DEV_SET_PRESENT(&dp->du_common);
2842a3114836SGerry Liu
2843a3114836SGerry Liu bp->b_ndev++;
2844a3114836SGerry Liu
2845a3114836SGerry Liu return (NULL);
2846a3114836SGerry Liu }
2847a3114836SGerry Liu
2848a3114836SGerry Liu static sbd_error_t *
dr_init_devlists(dr_board_t * bp)2849a3114836SGerry Liu dr_init_devlists(dr_board_t *bp)
2850a3114836SGerry Liu {
2851a3114836SGerry Liu int i;
2852a3114836SGerry Liu sbd_error_t *err;
2853a3114836SGerry Liu dr_dev_unit_t *dp;
2854a3114836SGerry Liu static fn_t f = "dr_init_devlists";
2855a3114836SGerry Liu
2856a3114836SGerry Liu PR_ALL("%s (%s)...\n", f, bp->b_path);
2857a3114836SGerry Liu
2858a3114836SGerry Liu /* sanity check */
2859a3114836SGerry Liu ASSERT(bp->b_ndev == 0);
2860a3114836SGerry Liu
2861a3114836SGerry Liu DR_DEVS_DISCONNECT(bp, (uint_t)-1);
2862a3114836SGerry Liu
2863a3114836SGerry Liu /*
2864a3114836SGerry Liu * This routine builds the board's devlist and initializes
2865a3114836SGerry Liu * the common portion of the unit data structures.
2866a3114836SGerry Liu * Note: because the common portion is considered
2867a3114836SGerry Liu * uninitialized, the dr_get_*_unit() routines can not
2868a3114836SGerry Liu * be used.
2869a3114836SGerry Liu */
2870a3114836SGerry Liu
2871a3114836SGerry Liu /*
2872a3114836SGerry Liu * Clear out old entries, if any.
2873a3114836SGerry Liu */
2874a3114836SGerry Liu for (i = 0; i < MAX_CPU_UNITS_PER_BOARD; i++) {
2875a3114836SGerry Liu dp = DR_GET_BOARD_DEVUNIT(bp, SBD_COMP_CPU, i);
2876a3114836SGerry Liu
2877a3114836SGerry Liu bzero(dp, sizeof (*dp));
2878a3114836SGerry Liu dp->du_common.sbdev_bp = bp;
2879a3114836SGerry Liu dp->du_common.sbdev_unum = i;
2880a3114836SGerry Liu dp->du_common.sbdev_type = SBD_COMP_CPU;
2881a3114836SGerry Liu }
2882a3114836SGerry Liu
2883a3114836SGerry Liu for (i = 0; i < MAX_MEM_UNITS_PER_BOARD; i++) {
2884a3114836SGerry Liu dp = DR_GET_BOARD_DEVUNIT(bp, SBD_COMP_MEM, i);
2885a3114836SGerry Liu
2886a3114836SGerry Liu bzero(dp, sizeof (*dp));
2887a3114836SGerry Liu dp->du_common.sbdev_bp = bp;
2888a3114836SGerry Liu dp->du_common.sbdev_unum = i;
2889a3114836SGerry Liu dp->du_common.sbdev_type = SBD_COMP_MEM;
2890a3114836SGerry Liu }
2891a3114836SGerry Liu
2892a3114836SGerry Liu for (i = 0; i < MAX_IO_UNITS_PER_BOARD; i++) {
2893a3114836SGerry Liu dp = DR_GET_BOARD_DEVUNIT(bp, SBD_COMP_IO, i);
2894a3114836SGerry Liu
2895a3114836SGerry Liu bzero(dp, sizeof (*dp));
2896a3114836SGerry Liu dp->du_common.sbdev_bp = bp;
2897a3114836SGerry Liu dp->du_common.sbdev_unum = i;
2898a3114836SGerry Liu dp->du_common.sbdev_type = SBD_COMP_IO;
2899a3114836SGerry Liu }
2900a3114836SGerry Liu
2901a3114836SGerry Liu err = NULL;
2902a3114836SGerry Liu if (bp->b_id) {
2903a3114836SGerry Liu /* find devices on this board */
2904a3114836SGerry Liu err = drmach_board_find_devices(
2905a3114836SGerry Liu bp->b_id, bp, dr_dev_found);
2906a3114836SGerry Liu }
2907a3114836SGerry Liu
2908a3114836SGerry Liu return (err);
2909a3114836SGerry Liu }
2910a3114836SGerry Liu
2911a3114836SGerry Liu /*
2912a3114836SGerry Liu * Return the unit number of the respective drmachid if
2913a3114836SGerry Liu * it's found to be attached.
2914a3114836SGerry Liu */
2915a3114836SGerry Liu static int
dr_check_unit_attached(dr_common_unit_t * cp)2916a3114836SGerry Liu dr_check_unit_attached(dr_common_unit_t *cp)
2917a3114836SGerry Liu {
2918a3114836SGerry Liu int rv = 0;
2919a3114836SGerry Liu processorid_t cpuid;
2920a3114836SGerry Liu uint64_t basepa, endpa;
2921a3114836SGerry Liu struct memlist *ml;
2922a3114836SGerry Liu extern struct memlist *phys_install;
2923a3114836SGerry Liu sbd_error_t *err;
2924a3114836SGerry Liu int yes;
2925a3114836SGerry Liu static fn_t f = "dr_check_unit_attached";
2926a3114836SGerry Liu
2927a3114836SGerry Liu switch (cp->sbdev_type) {
2928a3114836SGerry Liu case SBD_COMP_CPU:
2929a3114836SGerry Liu err = drmach_cpu_get_id(cp->sbdev_id, &cpuid);
2930a3114836SGerry Liu if (err) {
2931a3114836SGerry Liu DRERR_SET_C(&cp->sbdev_error, &err);
2932a3114836SGerry Liu rv = -1;
2933a3114836SGerry Liu break;
2934a3114836SGerry Liu }
2935a3114836SGerry Liu mutex_enter(&cpu_lock);
2936a3114836SGerry Liu if (cpu_get(cpuid) == NULL)
2937a3114836SGerry Liu rv = -1;
2938a3114836SGerry Liu mutex_exit(&cpu_lock);
2939a3114836SGerry Liu break;
2940a3114836SGerry Liu
2941a3114836SGerry Liu case SBD_COMP_MEM:
2942a3114836SGerry Liu err = drmach_mem_get_slice_info(cp->sbdev_id,
2943a3114836SGerry Liu &basepa, &endpa, NULL);
2944a3114836SGerry Liu if (err) {
2945a3114836SGerry Liu DRERR_SET_C(&cp->sbdev_error, &err);
2946a3114836SGerry Liu rv = -1;
2947a3114836SGerry Liu break;
2948a3114836SGerry Liu }
2949a3114836SGerry Liu
2950a3114836SGerry Liu /*
2951a3114836SGerry Liu * Check if base address is in phys_install.
2952a3114836SGerry Liu */
2953a3114836SGerry Liu memlist_read_lock();
2954a3114836SGerry Liu for (ml = phys_install; ml; ml = ml->ml_next)
2955a3114836SGerry Liu if ((endpa <= ml->ml_address) ||
2956a3114836SGerry Liu (basepa >= (ml->ml_address + ml->ml_size)))
2957a3114836SGerry Liu continue;
2958a3114836SGerry Liu else
2959a3114836SGerry Liu break;
2960a3114836SGerry Liu memlist_read_unlock();
2961a3114836SGerry Liu if (ml == NULL)
2962a3114836SGerry Liu rv = -1;
2963a3114836SGerry Liu break;
2964a3114836SGerry Liu
2965a3114836SGerry Liu case SBD_COMP_IO:
2966a3114836SGerry Liu err = drmach_io_is_attached(cp->sbdev_id, &yes);
2967a3114836SGerry Liu if (err) {
2968a3114836SGerry Liu DRERR_SET_C(&cp->sbdev_error, &err);
2969a3114836SGerry Liu rv = -1;
2970a3114836SGerry Liu break;
2971a3114836SGerry Liu } else if (!yes)
2972a3114836SGerry Liu rv = -1;
2973a3114836SGerry Liu break;
2974a3114836SGerry Liu
2975a3114836SGerry Liu default:
2976a3114836SGerry Liu PR_ALL("%s: unexpected nodetype(%d) for id 0x%p\n",
2977a3114836SGerry Liu f, cp->sbdev_type, cp->sbdev_id);
2978a3114836SGerry Liu rv = -1;
2979a3114836SGerry Liu break;
2980a3114836SGerry Liu }
2981a3114836SGerry Liu
2982a3114836SGerry Liu return (rv);
2983a3114836SGerry Liu }
2984a3114836SGerry Liu
2985a3114836SGerry Liu /*
2986a3114836SGerry Liu * See if drmach recognizes the passthru command. DRMACH expects the
2987a3114836SGerry Liu * id to identify the thing to which the command is being applied. Using
2988a3114836SGerry Liu * nonsense SBD terms, that information has been perversely encoded in the
2989a3114836SGerry Liu * c_id member of the sbd_cmd_t structure. This logic reads those tea
2990a3114836SGerry Liu * leaves, finds the associated drmach id, then calls drmach to process
2991a3114836SGerry Liu * the passthru command.
2992a3114836SGerry Liu */
2993a3114836SGerry Liu static int
dr_pt_try_drmach(dr_handle_t * hp)2994a3114836SGerry Liu dr_pt_try_drmach(dr_handle_t *hp)
2995a3114836SGerry Liu {
2996a3114836SGerry Liu dr_board_t *bp = hp->h_bd;
2997a3114836SGerry Liu sbd_comp_id_t *comp_id = &hp->h_sbdcmd.cmd_cm.c_id;
2998a3114836SGerry Liu drmachid_t id;
2999a3114836SGerry Liu
3000a3114836SGerry Liu if (comp_id->c_type == SBD_COMP_NONE) {
3001a3114836SGerry Liu id = bp->b_id;
3002a3114836SGerry Liu } else {
3003a3114836SGerry Liu sbd_comp_type_t nt;
3004a3114836SGerry Liu
3005a3114836SGerry Liu nt = dr_dev_type_to_nt(comp_id->c_name);
3006a3114836SGerry Liu if (nt == SBD_COMP_UNKNOWN) {
3007a3114836SGerry Liu dr_op_err(CE_IGNORE, hp, ESBD_INVAL, comp_id->c_name);
3008a3114836SGerry Liu id = 0;
3009a3114836SGerry Liu } else {
3010a3114836SGerry Liu /* pt command applied to dynamic attachment point */
3011a3114836SGerry Liu dr_common_unit_t *cp;
3012a3114836SGerry Liu cp = dr_get_common_unit(bp, nt, comp_id->c_unit);
3013a3114836SGerry Liu id = cp->sbdev_id;
3014a3114836SGerry Liu }
3015a3114836SGerry Liu }
3016a3114836SGerry Liu
3017a3114836SGerry Liu if (hp->h_err == NULL)
3018a3114836SGerry Liu hp->h_err = drmach_passthru(id, &hp->h_opts);
3019a3114836SGerry Liu
3020a3114836SGerry Liu return (hp->h_err == NULL ? 0 : -1);
3021a3114836SGerry Liu }
3022a3114836SGerry Liu
3023a3114836SGerry Liu static int
dr_pt_ioctl(dr_handle_t * hp)3024a3114836SGerry Liu dr_pt_ioctl(dr_handle_t *hp)
3025a3114836SGerry Liu {
3026a3114836SGerry Liu int cmd, rv, len;
3027a3114836SGerry Liu int32_t sz;
3028a3114836SGerry Liu int found;
3029a3114836SGerry Liu char *copts;
3030a3114836SGerry Liu static fn_t f = "dr_pt_ioctl";
3031a3114836SGerry Liu
3032a3114836SGerry Liu PR_ALL("%s...\n", f);
3033a3114836SGerry Liu
3034a3114836SGerry Liu sz = hp->h_opts.size;
3035a3114836SGerry Liu copts = hp->h_opts.copts;
3036a3114836SGerry Liu
3037a3114836SGerry Liu if (sz == 0 || copts == (char *)NULL) {
3038a3114836SGerry Liu cmn_err(CE_WARN, "%s: invalid passthru args", f);
3039a3114836SGerry Liu return (EINVAL);
3040a3114836SGerry Liu }
3041a3114836SGerry Liu
3042a3114836SGerry Liu found = 0;
3043a3114836SGerry Liu for (cmd = 0; cmd < (sizeof (pt_arr) / sizeof (pt_arr[0])); cmd++) {
3044a3114836SGerry Liu len = strlen(pt_arr[cmd].pt_name);
3045a3114836SGerry Liu found = (strncmp(pt_arr[cmd].pt_name, copts, len) == 0);
3046a3114836SGerry Liu if (found)
3047a3114836SGerry Liu break;
3048a3114836SGerry Liu }
3049a3114836SGerry Liu
3050a3114836SGerry Liu if (found)
3051a3114836SGerry Liu rv = (*pt_arr[cmd].pt_func)(hp);
3052a3114836SGerry Liu else
3053a3114836SGerry Liu rv = dr_pt_try_drmach(hp);
3054a3114836SGerry Liu
3055a3114836SGerry Liu return (rv);
3056a3114836SGerry Liu }
3057a3114836SGerry Liu
3058a3114836SGerry Liu /*
3059a3114836SGerry Liu * Called at driver load time to determine the state and condition
3060a3114836SGerry Liu * of an existing board in the system.
3061a3114836SGerry Liu */
3062a3114836SGerry Liu static void
dr_board_discovery(dr_board_t * bp)3063a3114836SGerry Liu dr_board_discovery(dr_board_t *bp)
3064a3114836SGerry Liu {
3065a3114836SGerry Liu int i;
3066a3114836SGerry Liu dr_devset_t devs_lost, devs_attached = 0;
3067a3114836SGerry Liu dr_cpu_unit_t *cp;
3068a3114836SGerry Liu dr_mem_unit_t *mp;
3069a3114836SGerry Liu dr_io_unit_t *ip;
3070a3114836SGerry Liu static fn_t f = "dr_board_discovery";
3071a3114836SGerry Liu
3072a3114836SGerry Liu if (DR_DEVS_PRESENT(bp) == 0) {
3073a3114836SGerry Liu PR_ALL("%s: board %d has no devices present\n",
3074a3114836SGerry Liu f, bp->b_num);
3075a3114836SGerry Liu return;
3076a3114836SGerry Liu }
3077a3114836SGerry Liu
3078a3114836SGerry Liu /*
3079a3114836SGerry Liu * Check for existence of cpus.
3080a3114836SGerry Liu */
3081a3114836SGerry Liu for (i = 0; i < MAX_CPU_UNITS_PER_BOARD; i++) {
3082a3114836SGerry Liu cp = dr_get_cpu_unit(bp, i);
3083a3114836SGerry Liu
3084a3114836SGerry Liu if (!DR_DEV_IS_PRESENT(&cp->sbc_cm))
3085a3114836SGerry Liu continue;
3086a3114836SGerry Liu
3087a3114836SGerry Liu if (dr_check_unit_attached(&cp->sbc_cm) >= 0) {
3088a3114836SGerry Liu DR_DEV_SET_ATTACHED(&cp->sbc_cm);
3089a3114836SGerry Liu DEVSET_ADD(devs_attached, SBD_COMP_CPU, i);
3090a3114836SGerry Liu PR_ALL("%s: board %d, cpu-unit %d - attached\n",
3091a3114836SGerry Liu f, bp->b_num, i);
3092a3114836SGerry Liu }
3093a3114836SGerry Liu dr_init_cpu_unit(cp);
3094a3114836SGerry Liu }
3095a3114836SGerry Liu
3096a3114836SGerry Liu /*
3097a3114836SGerry Liu * Check for existence of memory.
3098a3114836SGerry Liu */
3099a3114836SGerry Liu for (i = 0; i < MAX_MEM_UNITS_PER_BOARD; i++) {
3100a3114836SGerry Liu mp = dr_get_mem_unit(bp, i);
3101a3114836SGerry Liu
3102a3114836SGerry Liu if (!DR_DEV_IS_PRESENT(&mp->sbm_cm))
3103a3114836SGerry Liu continue;
3104a3114836SGerry Liu
3105a3114836SGerry Liu if (dr_check_unit_attached(&mp->sbm_cm) >= 0) {
3106a3114836SGerry Liu DR_DEV_SET_ATTACHED(&mp->sbm_cm);
3107a3114836SGerry Liu DEVSET_ADD(devs_attached, SBD_COMP_MEM, i);
3108a3114836SGerry Liu PR_ALL("%s: board %d, mem-unit %d - attached\n",
3109a3114836SGerry Liu f, bp->b_num, i);
3110a3114836SGerry Liu }
3111a3114836SGerry Liu dr_init_mem_unit(mp);
3112a3114836SGerry Liu }
3113a3114836SGerry Liu
3114a3114836SGerry Liu /*
3115a3114836SGerry Liu * Check for i/o state.
3116a3114836SGerry Liu */
3117a3114836SGerry Liu for (i = 0; i < MAX_IO_UNITS_PER_BOARD; i++) {
3118a3114836SGerry Liu ip = dr_get_io_unit(bp, i);
3119a3114836SGerry Liu
3120a3114836SGerry Liu if (!DR_DEV_IS_PRESENT(&ip->sbi_cm))
3121a3114836SGerry Liu continue;
3122a3114836SGerry Liu
3123a3114836SGerry Liu if (dr_check_unit_attached(&ip->sbi_cm) >= 0) {
3124a3114836SGerry Liu /*
3125a3114836SGerry Liu * Found it!
3126a3114836SGerry Liu */
3127a3114836SGerry Liu DR_DEV_SET_ATTACHED(&ip->sbi_cm);
3128a3114836SGerry Liu DEVSET_ADD(devs_attached, SBD_COMP_IO, i);
3129a3114836SGerry Liu PR_ALL("%s: board %d, io-unit %d - attached\n",
3130a3114836SGerry Liu f, bp->b_num, i);
3131a3114836SGerry Liu }
3132a3114836SGerry Liu dr_init_io_unit(ip);
3133a3114836SGerry Liu }
3134a3114836SGerry Liu
3135a3114836SGerry Liu DR_DEVS_CONFIGURE(bp, devs_attached);
3136a3114836SGerry Liu if (devs_attached && ((devs_lost = DR_DEVS_UNATTACHED(bp)) != 0)) {
3137a3114836SGerry Liu int ut;
3138a3114836SGerry Liu
3139a3114836SGerry Liu /*
3140a3114836SGerry Liu * It is not legal on board discovery to have a
3141a3114836SGerry Liu * board that is only partially attached. A board
3142a3114836SGerry Liu * is either all attached or all connected. If a
3143a3114836SGerry Liu * board has at least one attached device, then
3144a3114836SGerry Liu * the the remaining devices, if any, must have
3145a3114836SGerry Liu * been lost or disconnected. These devices can
3146a3114836SGerry Liu * only be recovered by a full attach from scratch.
3147a3114836SGerry Liu * Note that devices previously in the unreferenced
3148a3114836SGerry Liu * state are subsequently lost until the next full
3149a3114836SGerry Liu * attach. This is necessary since the driver unload
3150a3114836SGerry Liu * that must have occurred would have wiped out the
3151a3114836SGerry Liu * information necessary to re-configure the device
3152a3114836SGerry Liu * back online, e.g. memlist.
3153a3114836SGerry Liu */
3154a3114836SGerry Liu PR_ALL("%s: some devices LOST (" DEVSET_FMT_STR ")...\n",
3155a3114836SGerry Liu f, DEVSET_FMT_ARG(devs_lost));
3156a3114836SGerry Liu
3157a3114836SGerry Liu for (ut = 0; ut < MAX_CPU_UNITS_PER_BOARD; ut++) {
3158a3114836SGerry Liu if (!DEVSET_IN_SET(devs_lost, SBD_COMP_CPU, ut))
3159a3114836SGerry Liu continue;
3160a3114836SGerry Liu
3161a3114836SGerry Liu cp = dr_get_cpu_unit(bp, ut);
3162a3114836SGerry Liu dr_device_transition(&cp->sbc_cm, DR_STATE_EMPTY);
3163a3114836SGerry Liu }
3164a3114836SGerry Liu
3165a3114836SGerry Liu for (ut = 0; ut < MAX_MEM_UNITS_PER_BOARD; ut++) {
3166a3114836SGerry Liu if (!DEVSET_IN_SET(devs_lost, SBD_COMP_MEM, ut))
3167a3114836SGerry Liu continue;
3168a3114836SGerry Liu
3169a3114836SGerry Liu mp = dr_get_mem_unit(bp, ut);
3170a3114836SGerry Liu dr_device_transition(&mp->sbm_cm, DR_STATE_EMPTY);
3171a3114836SGerry Liu }
3172a3114836SGerry Liu
3173a3114836SGerry Liu for (ut = 0; ut < MAX_IO_UNITS_PER_BOARD; ut++) {
3174a3114836SGerry Liu if (!DEVSET_IN_SET(devs_lost, SBD_COMP_IO, ut))
3175a3114836SGerry Liu continue;
3176a3114836SGerry Liu
3177a3114836SGerry Liu ip = dr_get_io_unit(bp, ut);
3178a3114836SGerry Liu dr_device_transition(&ip->sbi_cm, DR_STATE_EMPTY);
3179a3114836SGerry Liu }
3180a3114836SGerry Liu
3181a3114836SGerry Liu DR_DEVS_DISCONNECT(bp, devs_lost);
3182a3114836SGerry Liu }
3183a3114836SGerry Liu }
3184a3114836SGerry Liu
3185a3114836SGerry Liu static int
dr_board_init(dr_board_t * bp,dev_info_t * dip,int bd)3186a3114836SGerry Liu dr_board_init(dr_board_t *bp, dev_info_t *dip, int bd)
3187a3114836SGerry Liu {
3188a3114836SGerry Liu sbd_error_t *err;
3189a3114836SGerry Liu
3190a3114836SGerry Liu mutex_init(&bp->b_lock, NULL, MUTEX_DRIVER, NULL);
3191a3114836SGerry Liu mutex_init(&bp->b_slock, NULL, MUTEX_DRIVER, NULL);
3192a3114836SGerry Liu cv_init(&bp->b_scv, NULL, CV_DRIVER, NULL);
3193a3114836SGerry Liu bp->b_rstate = SBD_STAT_EMPTY;
3194a3114836SGerry Liu bp->b_ostate = SBD_STAT_UNCONFIGURED;
3195a3114836SGerry Liu bp->b_cond = SBD_COND_UNKNOWN;
3196a3114836SGerry Liu (void) drv_getparm(TIME, (void *)&bp->b_time);
3197a3114836SGerry Liu
3198a3114836SGerry Liu (void) drmach_board_lookup(bd, &bp->b_id);
3199a3114836SGerry Liu bp->b_num = bd;
3200a3114836SGerry Liu bp->b_dip = dip;
3201a3114836SGerry Liu
3202a3114836SGerry Liu bp->b_dev[DEVSET_NIX(SBD_COMP_CPU)] = GETSTRUCT(dr_dev_unit_t,
3203a3114836SGerry Liu MAX_CPU_UNITS_PER_BOARD);
3204a3114836SGerry Liu
3205a3114836SGerry Liu bp->b_dev[DEVSET_NIX(SBD_COMP_MEM)] = GETSTRUCT(dr_dev_unit_t,
3206a3114836SGerry Liu MAX_MEM_UNITS_PER_BOARD);
3207a3114836SGerry Liu
3208a3114836SGerry Liu bp->b_dev[DEVSET_NIX(SBD_COMP_IO)] = GETSTRUCT(dr_dev_unit_t,
3209a3114836SGerry Liu MAX_IO_UNITS_PER_BOARD);
3210a3114836SGerry Liu
3211a3114836SGerry Liu /*
3212a3114836SGerry Liu * Initialize the devlists
3213a3114836SGerry Liu */
3214a3114836SGerry Liu err = dr_init_devlists(bp);
3215a3114836SGerry Liu if (err) {
3216a3114836SGerry Liu sbd_err_clear(&err);
3217a3114836SGerry Liu dr_board_destroy(bp);
3218a3114836SGerry Liu return (-1);
3219a3114836SGerry Liu } else if (bp->b_ndev == 0) {
3220a3114836SGerry Liu dr_board_transition(bp, DR_STATE_EMPTY);
3221a3114836SGerry Liu } else {
3222a3114836SGerry Liu /*
3223a3114836SGerry Liu * Couldn't have made it down here without
3224a3114836SGerry Liu * having found at least one device.
3225a3114836SGerry Liu */
3226a3114836SGerry Liu ASSERT(DR_DEVS_PRESENT(bp) != 0);
3227a3114836SGerry Liu /*
3228a3114836SGerry Liu * Check the state of any possible devices on the
3229a3114836SGerry Liu * board.
3230a3114836SGerry Liu */
3231a3114836SGerry Liu dr_board_discovery(bp);
3232a3114836SGerry Liu
3233a3114836SGerry Liu bp->b_assigned = 1;
3234a3114836SGerry Liu
3235a3114836SGerry Liu if (DR_DEVS_UNATTACHED(bp) == 0) {
3236a3114836SGerry Liu /*
3237a3114836SGerry Liu * The board has no unattached devices, therefore
3238a3114836SGerry Liu * by reason of insanity it must be configured!
3239a3114836SGerry Liu */
3240a3114836SGerry Liu dr_board_transition(bp, DR_STATE_CONFIGURED);
3241a3114836SGerry Liu bp->b_ostate = SBD_STAT_CONFIGURED;
3242a3114836SGerry Liu bp->b_rstate = SBD_STAT_CONNECTED;
3243a3114836SGerry Liu bp->b_cond = SBD_COND_OK;
3244a3114836SGerry Liu (void) drv_getparm(TIME, (void *)&bp->b_time);
3245a3114836SGerry Liu } else if (DR_DEVS_ATTACHED(bp)) {
3246a3114836SGerry Liu dr_board_transition(bp, DR_STATE_PARTIAL);
3247a3114836SGerry Liu bp->b_ostate = SBD_STAT_CONFIGURED;
3248a3114836SGerry Liu bp->b_rstate = SBD_STAT_CONNECTED;
3249a3114836SGerry Liu bp->b_cond = SBD_COND_OK;
3250a3114836SGerry Liu (void) drv_getparm(TIME, (void *)&bp->b_time);
3251a3114836SGerry Liu } else {
3252a3114836SGerry Liu dr_board_transition(bp, DR_STATE_CONNECTED);
3253a3114836SGerry Liu bp->b_rstate = SBD_STAT_CONNECTED;
3254a3114836SGerry Liu (void) drv_getparm(TIME, (void *)&bp->b_time);
3255a3114836SGerry Liu }
3256a3114836SGerry Liu }
3257a3114836SGerry Liu
3258a3114836SGerry Liu return (0);
3259a3114836SGerry Liu }
3260a3114836SGerry Liu
3261a3114836SGerry Liu static void
dr_board_destroy(dr_board_t * bp)3262a3114836SGerry Liu dr_board_destroy(dr_board_t *bp)
3263a3114836SGerry Liu {
3264a3114836SGerry Liu PR_ALL("dr_board_destroy: num %d, path %s\n",
3265a3114836SGerry Liu bp->b_num, bp->b_path);
3266a3114836SGerry Liu
3267a3114836SGerry Liu dr_board_transition(bp, DR_STATE_EMPTY);
3268a3114836SGerry Liu bp->b_rstate = SBD_STAT_EMPTY;
3269a3114836SGerry Liu (void) drv_getparm(TIME, (void *)&bp->b_time);
3270a3114836SGerry Liu
3271a3114836SGerry Liu /*
3272a3114836SGerry Liu * Free up MEM unit structs.
3273a3114836SGerry Liu */
3274a3114836SGerry Liu FREESTRUCT(bp->b_dev[DEVSET_NIX(SBD_COMP_MEM)],
3275a3114836SGerry Liu dr_dev_unit_t, MAX_MEM_UNITS_PER_BOARD);
3276a3114836SGerry Liu bp->b_dev[DEVSET_NIX(SBD_COMP_MEM)] = NULL;
3277a3114836SGerry Liu /*
3278a3114836SGerry Liu * Free up CPU unit structs.
3279a3114836SGerry Liu */
3280a3114836SGerry Liu FREESTRUCT(bp->b_dev[DEVSET_NIX(SBD_COMP_CPU)],
3281a3114836SGerry Liu dr_dev_unit_t, MAX_CPU_UNITS_PER_BOARD);
3282a3114836SGerry Liu bp->b_dev[DEVSET_NIX(SBD_COMP_CPU)] = NULL;
3283a3114836SGerry Liu /*
3284a3114836SGerry Liu * Free up IO unit structs.
3285a3114836SGerry Liu */
3286a3114836SGerry Liu FREESTRUCT(bp->b_dev[DEVSET_NIX(SBD_COMP_IO)],
3287a3114836SGerry Liu dr_dev_unit_t, MAX_IO_UNITS_PER_BOARD);
3288a3114836SGerry Liu bp->b_dev[DEVSET_NIX(SBD_COMP_IO)] = NULL;
3289a3114836SGerry Liu
3290a3114836SGerry Liu mutex_destroy(&bp->b_lock);
3291a3114836SGerry Liu mutex_destroy(&bp->b_slock);
3292a3114836SGerry Liu cv_destroy(&bp->b_scv);
3293a3114836SGerry Liu
3294a3114836SGerry Liu /*
3295a3114836SGerry Liu * Reset the board structure to its initial state, otherwise it will
3296a3114836SGerry Liu * cause trouble on the next call to dr_board_init() for the same board.
3297a3114836SGerry Liu * dr_board_init() may be called multiple times for the same board
3298a3114836SGerry Liu * if DR driver fails to initialize some boards.
3299a3114836SGerry Liu */
3300a3114836SGerry Liu bzero(bp, sizeof (*bp));
3301a3114836SGerry Liu }
3302a3114836SGerry Liu
3303a3114836SGerry Liu void
dr_lock_status(dr_board_t * bp)3304a3114836SGerry Liu dr_lock_status(dr_board_t *bp)
3305a3114836SGerry Liu {
3306a3114836SGerry Liu mutex_enter(&bp->b_slock);
3307a3114836SGerry Liu while (bp->b_sflags & DR_BSLOCK)
3308a3114836SGerry Liu cv_wait(&bp->b_scv, &bp->b_slock);
3309a3114836SGerry Liu bp->b_sflags |= DR_BSLOCK;
3310a3114836SGerry Liu mutex_exit(&bp->b_slock);
3311a3114836SGerry Liu }
3312a3114836SGerry Liu
3313a3114836SGerry Liu void
dr_unlock_status(dr_board_t * bp)3314a3114836SGerry Liu dr_unlock_status(dr_board_t *bp)
3315a3114836SGerry Liu {
3316a3114836SGerry Liu mutex_enter(&bp->b_slock);
3317a3114836SGerry Liu bp->b_sflags &= ~DR_BSLOCK;
3318a3114836SGerry Liu cv_signal(&bp->b_scv);
3319a3114836SGerry Liu mutex_exit(&bp->b_slock);
3320a3114836SGerry Liu }
3321a3114836SGerry Liu
3322a3114836SGerry Liu /*
3323a3114836SGerry Liu * Extract flags passed via ioctl.
3324a3114836SGerry Liu */
3325a3114836SGerry Liu int
dr_cmd_flags(dr_handle_t * hp)3326a3114836SGerry Liu dr_cmd_flags(dr_handle_t *hp)
3327a3114836SGerry Liu {
3328a3114836SGerry Liu return (hp->h_sbdcmd.cmd_cm.c_flags);
3329a3114836SGerry Liu }
3330