17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
509fe1b16Sdnielsen * Common Development and Distribution License (the "License").
609fe1b16Sdnielsen * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
2109fe1b16Sdnielsen
227c478bd9Sstevel@tonic-gate /*
23*4c06356bSdh * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #include "cfga_scsi.h"
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate struct larg {
307c478bd9Sstevel@tonic-gate int ndevs;
317c478bd9Sstevel@tonic-gate int nelem;
327c478bd9Sstevel@tonic-gate char *dev;
337c478bd9Sstevel@tonic-gate char **dev_list;
347c478bd9Sstevel@tonic-gate };
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate #define ETC_VFSTAB "/etc/vfstab"
377c478bd9Sstevel@tonic-gate #define SCFGA_LOCK "/var/run/cfgadm_scsi"
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate /* Function prototypes */
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate static scfga_ret_t quiesce_confirm(apid_t *apidp,
427c478bd9Sstevel@tonic-gate msgid_t cmd_msg, prompt_t *pt, int *okp, int *quiesce, int *l_errnop);
437c478bd9Sstevel@tonic-gate static scfga_ret_t dev_hotplug(apid_t *apidp,
447c478bd9Sstevel@tonic-gate prompt_t *pt, cfga_flags_t flags, int quiesce, char **errstring);
457c478bd9Sstevel@tonic-gate static int disconnect(struct cfga_confirm *confp);
467c478bd9Sstevel@tonic-gate static int critical_ctrlr(const char *hba_phys);
477c478bd9Sstevel@tonic-gate static cfga_stat_t bus_devctl_to_recep_state(uint_t bus_dc_state);
487c478bd9Sstevel@tonic-gate static int get_hba_children(char *bus_path, char *dev_excl, char ***dev_list);
497c478bd9Sstevel@tonic-gate static char *get_node_path(char *minor_path);
507c478bd9Sstevel@tonic-gate static void free_dev_list_elements(char **dev_list);
517c478bd9Sstevel@tonic-gate static void free_dev_list(char **dev_list);
527c478bd9Sstevel@tonic-gate static int alloc_dev_list(struct larg *largp);
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate * Single thread all implicit quiesce operations
567c478bd9Sstevel@tonic-gate */
577c478bd9Sstevel@tonic-gate static mutex_t quiesce_mutex = DEFAULTMUTEX;
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate /*ARGSUSED*/
607c478bd9Sstevel@tonic-gate scfga_ret_t
bus_change_state(cfga_cmd_t state_change_cmd,apid_t * apidp,struct cfga_confirm * confp,cfga_flags_t flags,char ** errstring)617c478bd9Sstevel@tonic-gate bus_change_state(
627c478bd9Sstevel@tonic-gate cfga_cmd_t state_change_cmd,
637c478bd9Sstevel@tonic-gate apid_t *apidp,
647c478bd9Sstevel@tonic-gate struct cfga_confirm *confp,
657c478bd9Sstevel@tonic-gate cfga_flags_t flags,
667c478bd9Sstevel@tonic-gate char **errstring)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate int l_errno = 0, force;
697c478bd9Sstevel@tonic-gate uint_t state = 0;
707c478bd9Sstevel@tonic-gate cfga_stat_t bus_state;
717c478bd9Sstevel@tonic-gate scfga_cmd_t cmd;
727c478bd9Sstevel@tonic-gate msgid_t errid;
737c478bd9Sstevel@tonic-gate cfga_stat_t prereq;
747c478bd9Sstevel@tonic-gate scfga_ret_t ret;
757c478bd9Sstevel@tonic-gate char **dev_list = NULL;
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate assert(apidp->path != NULL);
787c478bd9Sstevel@tonic-gate assert(apidp->hba_phys != NULL);
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate * No dynamic components allowed
827c478bd9Sstevel@tonic-gate */
837c478bd9Sstevel@tonic-gate if (apidp->dyncomp != NULL) {
847c478bd9Sstevel@tonic-gate cfga_err(errstring, 0, ERR_NOT_BUSAPID, 0);
857c478bd9Sstevel@tonic-gate return (SCFGA_ERR);
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate /* Get bus state */
897c478bd9Sstevel@tonic-gate if (devctl_cmd(apidp->path, SCFGA_BUS_GETSTATE, &state,
907c478bd9Sstevel@tonic-gate &l_errno) != SCFGA_OK) {
917c478bd9Sstevel@tonic-gate cfga_err(errstring, l_errno, ERR_BUS_GETSTATE, 0);
927c478bd9Sstevel@tonic-gate return (SCFGA_ERR);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate bus_state = bus_devctl_to_recep_state(state);
967c478bd9Sstevel@tonic-gate force = ((flags & CFGA_FLAG_FORCE) == CFGA_FLAG_FORCE) ? 1 : 0;
977c478bd9Sstevel@tonic-gate assert(confp->confirm != NULL);
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate switch (state_change_cmd) {
1007c478bd9Sstevel@tonic-gate case CFGA_CMD_DISCONNECT: /* quiesce bus */
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate * If force flag not specified, check if controller is
1037c478bd9Sstevel@tonic-gate * critical.
1047c478bd9Sstevel@tonic-gate */
1057c478bd9Sstevel@tonic-gate if (!force) {
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate * This check is not foolproof, get user confirmation
1087c478bd9Sstevel@tonic-gate * if test passes.
1097c478bd9Sstevel@tonic-gate */
1107c478bd9Sstevel@tonic-gate if (critical_ctrlr(apidp->path)) {
1117c478bd9Sstevel@tonic-gate cfga_err(errstring, 0, ERR_CTRLR_CRIT, 0);
1127c478bd9Sstevel@tonic-gate ret = SCFGA_ERR;
1137c478bd9Sstevel@tonic-gate break;
1147c478bd9Sstevel@tonic-gate } else if (!disconnect(confp)) {
1157c478bd9Sstevel@tonic-gate ret = SCFGA_NACK;
1167c478bd9Sstevel@tonic-gate break;
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate cmd = SCFGA_BUS_QUIESCE;
1217c478bd9Sstevel@tonic-gate errid = ERR_BUS_QUIESCE;
1227c478bd9Sstevel@tonic-gate prereq = CFGA_STAT_CONNECTED;
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate goto common;
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate case CFGA_CMD_CONNECT: /* unquiesce bus */
1277c478bd9Sstevel@tonic-gate cmd = SCFGA_BUS_UNQUIESCE;
1287c478bd9Sstevel@tonic-gate errid = ERR_BUS_UNQUIESCE;
1297c478bd9Sstevel@tonic-gate prereq = CFGA_STAT_DISCONNECTED;
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate goto common;
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate case CFGA_CMD_CONFIGURE:
1347c478bd9Sstevel@tonic-gate cmd = SCFGA_BUS_CONFIGURE;
1357c478bd9Sstevel@tonic-gate errid = ERR_BUS_CONFIGURE;
1367c478bd9Sstevel@tonic-gate prereq = CFGA_STAT_CONNECTED;
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate goto common;
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate case CFGA_CMD_UNCONFIGURE:
1417c478bd9Sstevel@tonic-gate cmd = SCFGA_BUS_UNCONFIGURE;
1427c478bd9Sstevel@tonic-gate errid = ERR_BUS_UNCONFIGURE;
1437c478bd9Sstevel@tonic-gate prereq = CFGA_STAT_CONNECTED;
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate /* FALLTHROUGH */
1467c478bd9Sstevel@tonic-gate common:
1477c478bd9Sstevel@tonic-gate if (bus_state != prereq) {
1487c478bd9Sstevel@tonic-gate cfga_err(errstring, 0,
1497c478bd9Sstevel@tonic-gate (prereq == CFGA_STAT_CONNECTED)
1507c478bd9Sstevel@tonic-gate ? ERR_BUS_NOTCONNECTED
1517c478bd9Sstevel@tonic-gate : ERR_BUS_CONNECTED, 0);
1527c478bd9Sstevel@tonic-gate ret = SCFGA_ERR;
1537c478bd9Sstevel@tonic-gate break;
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate /*
1577c478bd9Sstevel@tonic-gate * When quiescing or unconfiguring a bus, first suspend or
1587c478bd9Sstevel@tonic-gate * offline it through RCM.
1597c478bd9Sstevel@tonic-gate * For unquiescing, we simple build the dev_list for
1607c478bd9Sstevel@tonic-gate * resume notification.
1617c478bd9Sstevel@tonic-gate */
1627c478bd9Sstevel@tonic-gate if (((apidp->flags & FLAG_DISABLE_RCM) == 0) &&
1637c478bd9Sstevel@tonic-gate ((cmd == SCFGA_BUS_QUIESCE) ||
1647c478bd9Sstevel@tonic-gate (cmd == SCFGA_BUS_UNQUIESCE) ||
1657c478bd9Sstevel@tonic-gate (cmd == SCFGA_BUS_UNCONFIGURE))) {
1667c478bd9Sstevel@tonic-gate ret = get_hba_children(apidp->path, NULL, &dev_list);
1677c478bd9Sstevel@tonic-gate if (ret != SCFGA_OK) {
1687c478bd9Sstevel@tonic-gate break;
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate if (cmd == SCFGA_BUS_QUIESCE) {
1717c478bd9Sstevel@tonic-gate if ((ret = scsi_rcm_suspend(dev_list,
1727c478bd9Sstevel@tonic-gate errstring, flags, 1)) != SCFGA_OK) {
1737c478bd9Sstevel@tonic-gate break;
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate } else if (cmd == SCFGA_BUS_UNCONFIGURE) {
1767c478bd9Sstevel@tonic-gate if ((ret = scsi_rcm_offline(dev_list,
1777c478bd9Sstevel@tonic-gate errstring, flags)) != SCFGA_OK) {
1787c478bd9Sstevel@tonic-gate break;
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate ret = devctl_cmd(apidp->path, cmd, NULL, &l_errno);
1847c478bd9Sstevel@tonic-gate if (ret != SCFGA_OK) {
1857c478bd9Sstevel@tonic-gate /*
1867c478bd9Sstevel@tonic-gate * EIO when child devices are busy may confuse user.
1877c478bd9Sstevel@tonic-gate * So explain it.
1887c478bd9Sstevel@tonic-gate */
1897c478bd9Sstevel@tonic-gate if (cmd == SCFGA_BUS_UNCONFIGURE && l_errno == EIO)
1907c478bd9Sstevel@tonic-gate errid = ERR_MAYBE_BUSY;
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate cfga_err(errstring, l_errno, errid, 0);
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate /*
1957c478bd9Sstevel@tonic-gate * If the bus was suspended in RCM, then cancel the RCM
1967c478bd9Sstevel@tonic-gate * operation. Discard RCM failures here because the
1977c478bd9Sstevel@tonic-gate * devctl's failure is what is most relevant.
1987c478bd9Sstevel@tonic-gate */
1997c478bd9Sstevel@tonic-gate if ((apidp->flags & FLAG_DISABLE_RCM) == 0) {
2007c478bd9Sstevel@tonic-gate if (cmd == SCFGA_BUS_QUIESCE)
2017c478bd9Sstevel@tonic-gate (void) scsi_rcm_resume(dev_list,
2027c478bd9Sstevel@tonic-gate errstring,
2037c478bd9Sstevel@tonic-gate (flags & (~CFGA_FLAG_FORCE)), 1);
2047c478bd9Sstevel@tonic-gate else if (cmd == SCFGA_BUS_UNCONFIGURE) {
2057c478bd9Sstevel@tonic-gate (void) devctl_cmd(apidp->path,
2067c478bd9Sstevel@tonic-gate SCFGA_BUS_CONFIGURE, NULL,
2077c478bd9Sstevel@tonic-gate &l_errno);
2087c478bd9Sstevel@tonic-gate (void) scsi_rcm_online(dev_list,
2097c478bd9Sstevel@tonic-gate errstring,
2107c478bd9Sstevel@tonic-gate (flags & (~CFGA_FLAG_FORCE)));
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate
2147c478bd9Sstevel@tonic-gate break;
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate
2177c478bd9Sstevel@tonic-gate /*
2187c478bd9Sstevel@tonic-gate * When unquiescing or configuring a bus, resume or online it
2197c478bd9Sstevel@tonic-gate * in RCM when the devctl command is complete.
2207c478bd9Sstevel@tonic-gate * When unconfiguring a bus, notify removal of devices.
2217c478bd9Sstevel@tonic-gate */
2227c478bd9Sstevel@tonic-gate if ((apidp->flags & FLAG_DISABLE_RCM) == 0) {
2237c478bd9Sstevel@tonic-gate if (cmd == SCFGA_BUS_UNQUIESCE) {
2247c478bd9Sstevel@tonic-gate ret = scsi_rcm_resume(dev_list, errstring,
2257c478bd9Sstevel@tonic-gate (flags & (~CFGA_FLAG_FORCE)), 1);
2267c478bd9Sstevel@tonic-gate } else if (cmd == SCFGA_BUS_UNCONFIGURE) {
2277c478bd9Sstevel@tonic-gate ret = scsi_rcm_remove(dev_list, errstring,
2287c478bd9Sstevel@tonic-gate (flags & (~CFGA_FLAG_FORCE)));
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate break;
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate case CFGA_CMD_LOAD:
2347c478bd9Sstevel@tonic-gate case CFGA_CMD_UNLOAD:
2357c478bd9Sstevel@tonic-gate ret = SCFGA_OPNOTSUPP;
2367c478bd9Sstevel@tonic-gate break;
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate default:
2397c478bd9Sstevel@tonic-gate cfga_err(errstring, 0, ERR_CMD_INVAL, 0);
2407c478bd9Sstevel@tonic-gate ret = SCFGA_ERR;
2417c478bd9Sstevel@tonic-gate break;
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate free_dev_list(dev_list);
2457c478bd9Sstevel@tonic-gate return (ret);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate scfga_ret_t
dev_change_state(cfga_cmd_t state_change_cmd,apid_t * apidp,cfga_flags_t flags,char ** errstring)2497c478bd9Sstevel@tonic-gate dev_change_state(
2507c478bd9Sstevel@tonic-gate cfga_cmd_t state_change_cmd,
2517c478bd9Sstevel@tonic-gate apid_t *apidp,
2527c478bd9Sstevel@tonic-gate cfga_flags_t flags,
2537c478bd9Sstevel@tonic-gate char **errstring)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate uint_t state = 0;
2567c478bd9Sstevel@tonic-gate int l_errno = 0;
2577c478bd9Sstevel@tonic-gate cfga_stat_t bus_state;
2587c478bd9Sstevel@tonic-gate scfga_cmd_t cmd;
2597c478bd9Sstevel@tonic-gate msgid_t errid;
2607c478bd9Sstevel@tonic-gate scfga_ret_t ret;
2617c478bd9Sstevel@tonic-gate char *dev_list[2] = {NULL};
2627c478bd9Sstevel@tonic-gate
2637c478bd9Sstevel@tonic-gate assert(apidp->path != NULL);
2647c478bd9Sstevel@tonic-gate assert(apidp->hba_phys != NULL);
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate /*
2677c478bd9Sstevel@tonic-gate * For a device, dynamic component must be present
2687c478bd9Sstevel@tonic-gate */
2697c478bd9Sstevel@tonic-gate if (apidp->dyncomp == NULL) {
2707c478bd9Sstevel@tonic-gate cfga_err(errstring, 0, ERR_APID_INVAL, 0);
2717c478bd9Sstevel@tonic-gate return (SCFGA_ERR);
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate /* Get bus state */
2757c478bd9Sstevel@tonic-gate if (devctl_cmd(apidp->hba_phys, SCFGA_BUS_GETSTATE, &state,
2767c478bd9Sstevel@tonic-gate &l_errno) != SCFGA_OK) {
2777c478bd9Sstevel@tonic-gate cfga_err(errstring, l_errno, ERR_BUS_GETSTATE, 0);
2787c478bd9Sstevel@tonic-gate return (SCFGA_ERR);
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate bus_state = bus_devctl_to_recep_state(state);
2827c478bd9Sstevel@tonic-gate
2837c478bd9Sstevel@tonic-gate switch (state_change_cmd) {
2847c478bd9Sstevel@tonic-gate case CFGA_CMD_CONFIGURE: /* online device */
2857c478bd9Sstevel@tonic-gate cmd = SCFGA_DEV_CONFIGURE;
2867c478bd9Sstevel@tonic-gate errid = ERR_DEV_CONFIGURE;
2877c478bd9Sstevel@tonic-gate goto common;
2887c478bd9Sstevel@tonic-gate
2897c478bd9Sstevel@tonic-gate case CFGA_CMD_UNCONFIGURE: /* offline device */
2907c478bd9Sstevel@tonic-gate cmd = SCFGA_DEV_UNCONFIGURE;
2917c478bd9Sstevel@tonic-gate errid = ERR_DEV_UNCONFIGURE;
2927c478bd9Sstevel@tonic-gate /* FALLTHROUGH */
2937c478bd9Sstevel@tonic-gate common:
2947c478bd9Sstevel@tonic-gate if (bus_state != CFGA_STAT_CONNECTED) {
2957c478bd9Sstevel@tonic-gate cfga_err(errstring, 0, ERR_BUS_NOTCONNECTED, 0);
2967c478bd9Sstevel@tonic-gate ret = SCFGA_ERR;
2977c478bd9Sstevel@tonic-gate break;
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate
300*4c06356bSdh if (apidp->dyntype == PATH_APID) {
301*4c06356bSdh /* call a scsi_vhci ioctl to do online/offline path. */
302*4c06356bSdh ret = path_apid_state_change(apidp, cmd,
303*4c06356bSdh flags, errstring, &l_errno, errid);
304*4c06356bSdh } else {
305*4c06356bSdh /*
306*4c06356bSdh * When unconfiguring a device, first offline it
307*4c06356bSdh * through RCM.
308*4c06356bSdh */
309*4c06356bSdh if ((apidp->flags & FLAG_DISABLE_RCM) == 0) {
310*4c06356bSdh if (cmd == SCFGA_DEV_UNCONFIGURE) {
311*4c06356bSdh dev_list[0] =
312*4c06356bSdh get_node_path(apidp->path);
313*4c06356bSdh if (dev_list[0] == NULL) {
314*4c06356bSdh ret = SCFGA_ERR;
315*4c06356bSdh break;
316*4c06356bSdh }
317*4c06356bSdh if ((ret = scsi_rcm_offline(dev_list,
318*4c06356bSdh errstring, flags)) != SCFGA_OK) {
319*4c06356bSdh break;
320*4c06356bSdh }
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate
324*4c06356bSdh ret = devctl_cmd(apidp->path, cmd, NULL, &l_errno);
325*4c06356bSdh if (ret != SCFGA_OK) {
326*4c06356bSdh cfga_err(errstring, l_errno, errid, 0);
3277c478bd9Sstevel@tonic-gate
3287c478bd9Sstevel@tonic-gate /*
3297c478bd9Sstevel@tonic-gate * If an unconfigure fails, cancel the RCM offline.
3307c478bd9Sstevel@tonic-gate * Discard any RCM failures so that the devctl
3317c478bd9Sstevel@tonic-gate * failure will still be reported.
3327c478bd9Sstevel@tonic-gate */
333*4c06356bSdh if ((apidp->flags & FLAG_DISABLE_RCM) == 0) {
334*4c06356bSdh if (cmd == SCFGA_DEV_UNCONFIGURE)
335*4c06356bSdh (void) scsi_rcm_online(dev_list,
336*4c06356bSdh errstring, flags);
337*4c06356bSdh }
338*4c06356bSdh break;
3397c478bd9Sstevel@tonic-gate }
340*4c06356bSdh if ((apidp->flags & FLAG_DISABLE_RCM) == 0) {
3417c478bd9Sstevel@tonic-gate /*
3427c478bd9Sstevel@tonic-gate * Unconfigure succeeded, call the RCM notify_remove.
3437c478bd9Sstevel@tonic-gate */
344*4c06356bSdh if (cmd == SCFGA_DEV_UNCONFIGURE)
345*4c06356bSdh (void) scsi_rcm_remove(dev_list,
3467c478bd9Sstevel@tonic-gate errstring, flags);
347*4c06356bSdh }
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate break;
3507c478bd9Sstevel@tonic-gate
3517c478bd9Sstevel@tonic-gate /*
3527c478bd9Sstevel@tonic-gate * Cannot disconnect/connect individual devices without affecting
3537c478bd9Sstevel@tonic-gate * other devices on the bus. So we don't support these ops.
3547c478bd9Sstevel@tonic-gate */
3557c478bd9Sstevel@tonic-gate case CFGA_CMD_DISCONNECT:
3567c478bd9Sstevel@tonic-gate case CFGA_CMD_CONNECT:
3577c478bd9Sstevel@tonic-gate cfga_err(errstring, 0, ERR_NOT_DEVOP, 0);
3587c478bd9Sstevel@tonic-gate ret = SCFGA_ERR;
3597c478bd9Sstevel@tonic-gate break;
3607c478bd9Sstevel@tonic-gate case CFGA_CMD_LOAD:
3617c478bd9Sstevel@tonic-gate case CFGA_CMD_UNLOAD:
3627c478bd9Sstevel@tonic-gate ret = SCFGA_OPNOTSUPP;
3637c478bd9Sstevel@tonic-gate break;
3647c478bd9Sstevel@tonic-gate default:
3657c478bd9Sstevel@tonic-gate cfga_err(errstring, 0, ERR_CMD_INVAL, 0);
3667c478bd9Sstevel@tonic-gate ret = SCFGA_ERR;
3677c478bd9Sstevel@tonic-gate break;
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate
3707c478bd9Sstevel@tonic-gate free_dev_list_elements(dev_list);
3717c478bd9Sstevel@tonic-gate return (ret);
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate
3747c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3757c478bd9Sstevel@tonic-gate scfga_ret_t
dev_remove(const char * func,scfga_cmd_t cmd,apid_t * apidp,prompt_t * prp,cfga_flags_t flags,char ** errstring)3767c478bd9Sstevel@tonic-gate dev_remove(
37709fe1b16Sdnielsen const char *func,
3787c478bd9Sstevel@tonic-gate scfga_cmd_t cmd,
3797c478bd9Sstevel@tonic-gate apid_t *apidp,
3807c478bd9Sstevel@tonic-gate prompt_t *prp,
3817c478bd9Sstevel@tonic-gate cfga_flags_t flags,
3827c478bd9Sstevel@tonic-gate char **errstring)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate int proceed, l_errno = 0;
3857c478bd9Sstevel@tonic-gate scfga_ret_t ret;
3867c478bd9Sstevel@tonic-gate int do_quiesce;
3877c478bd9Sstevel@tonic-gate char *dev_list[2] = {NULL};
3887c478bd9Sstevel@tonic-gate
3897c478bd9Sstevel@tonic-gate assert(apidp->hba_phys != NULL);
3907c478bd9Sstevel@tonic-gate assert(apidp->path != NULL);
3917c478bd9Sstevel@tonic-gate
3927c478bd9Sstevel@tonic-gate /* device operation only */
3937c478bd9Sstevel@tonic-gate if (apidp->dyncomp == NULL) {
3947c478bd9Sstevel@tonic-gate cfga_err(errstring, 0, ERR_NOT_BUSOP, 0);
3957c478bd9Sstevel@tonic-gate return (SCFGA_ERR);
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate
3987c478bd9Sstevel@tonic-gate proceed = 1;
3997c478bd9Sstevel@tonic-gate ret = quiesce_confirm(apidp, MSG_RMDEV, prp, &proceed, &do_quiesce,
4007c478bd9Sstevel@tonic-gate &l_errno);
4017c478bd9Sstevel@tonic-gate if (ret != SCFGA_OK) {
4027c478bd9Sstevel@tonic-gate cfga_err(errstring, l_errno, ERR_DEV_REMOVE, 0);
4037c478bd9Sstevel@tonic-gate return (ret);
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate
4067c478bd9Sstevel@tonic-gate if (!proceed) {
4077c478bd9Sstevel@tonic-gate return (SCFGA_NACK);
4087c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate
4107c478bd9Sstevel@tonic-gate /*
4117c478bd9Sstevel@tonic-gate * Offline the device in RCM
4127c478bd9Sstevel@tonic-gate */
4137c478bd9Sstevel@tonic-gate if ((apidp->flags & FLAG_DISABLE_RCM) == 0) {
4147c478bd9Sstevel@tonic-gate dev_list[0] = get_node_path(apidp->path);
4157c478bd9Sstevel@tonic-gate if (dev_list[0] == NULL)
4167c478bd9Sstevel@tonic-gate return (SCFGA_ERR);
4177c478bd9Sstevel@tonic-gate if ((ret = scsi_rcm_offline(dev_list, errstring, flags))
4187c478bd9Sstevel@tonic-gate != SCFGA_OK) {
4197c478bd9Sstevel@tonic-gate free_dev_list_elements(dev_list);
4207c478bd9Sstevel@tonic-gate return (ret);
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate
4247c478bd9Sstevel@tonic-gate /*
4257c478bd9Sstevel@tonic-gate * Offline the device
4267c478bd9Sstevel@tonic-gate */
4277c478bd9Sstevel@tonic-gate ret = devctl_cmd(apidp->path, SCFGA_DEV_UNCONFIGURE, NULL, &l_errno);
4287c478bd9Sstevel@tonic-gate if (ret != SCFGA_OK) {
4297c478bd9Sstevel@tonic-gate
4307c478bd9Sstevel@tonic-gate cfga_err(errstring, l_errno, ERR_DEV_REMOVE, 0);
4317c478bd9Sstevel@tonic-gate
4327c478bd9Sstevel@tonic-gate /*
4337c478bd9Sstevel@tonic-gate * Cancel the RCM offline. Discard the RCM failures so that
4347c478bd9Sstevel@tonic-gate * the above devctl failure is still reported.
4357c478bd9Sstevel@tonic-gate */
4367c478bd9Sstevel@tonic-gate if ((apidp->flags & FLAG_DISABLE_RCM) == 0)
4377c478bd9Sstevel@tonic-gate (void) scsi_rcm_online(dev_list, errstring, flags);
4387c478bd9Sstevel@tonic-gate free_dev_list_elements(dev_list);
4397c478bd9Sstevel@tonic-gate return (ret);
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate
4427c478bd9Sstevel@tonic-gate /* Do the physical removal */
4437c478bd9Sstevel@tonic-gate ret = dev_hotplug(apidp, prp, flags, do_quiesce, errstring);
4447c478bd9Sstevel@tonic-gate
4457c478bd9Sstevel@tonic-gate if (ret == SCFGA_OK) {
4467c478bd9Sstevel@tonic-gate /*
4477c478bd9Sstevel@tonic-gate * Complete the remove.
4487c478bd9Sstevel@tonic-gate * Since the device is already offlined, remove shouldn't
4497c478bd9Sstevel@tonic-gate * fail. Even if remove fails, there is no side effect.
4507c478bd9Sstevel@tonic-gate */
4517c478bd9Sstevel@tonic-gate (void) devctl_cmd(apidp->path, SCFGA_DEV_REMOVE,
4527c478bd9Sstevel@tonic-gate NULL, &l_errno);
4537c478bd9Sstevel@tonic-gate if ((apidp->flags & FLAG_DISABLE_RCM) == 0)
4547c478bd9Sstevel@tonic-gate ret = scsi_rcm_remove(dev_list, errstring, flags);
4557c478bd9Sstevel@tonic-gate } else {
4567c478bd9Sstevel@tonic-gate /*
4577c478bd9Sstevel@tonic-gate * Reconfigure the device and restore the device's RCM state.
4587c478bd9Sstevel@tonic-gate * If reconfigure succeeds, restore the state to online.
4597c478bd9Sstevel@tonic-gate * If reconfigure fails (e.g. a typo from user), we treat
4607c478bd9Sstevel@tonic-gate * the device as removed.
4617c478bd9Sstevel@tonic-gate */
4627c478bd9Sstevel@tonic-gate if (devctl_cmd(apidp->path, SCFGA_DEV_CONFIGURE, NULL, &l_errno)
4637c478bd9Sstevel@tonic-gate == SCFGA_OK) {
4647c478bd9Sstevel@tonic-gate if ((apidp->flags & FLAG_DISABLE_RCM) == 0)
4657c478bd9Sstevel@tonic-gate (void) scsi_rcm_online(dev_list, errstring,
4667c478bd9Sstevel@tonic-gate flags);
4677c478bd9Sstevel@tonic-gate } else {
4687c478bd9Sstevel@tonic-gate char *cp = strrchr(apidp->path, ':');
4697c478bd9Sstevel@tonic-gate if (cp)
4707c478bd9Sstevel@tonic-gate *cp = '\0';
4717c478bd9Sstevel@tonic-gate cfga_err(errstring, l_errno, ERR_DEV_RECONFIGURE,
4727c478bd9Sstevel@tonic-gate apidp->path, 0);
4737c478bd9Sstevel@tonic-gate if (cp)
4747c478bd9Sstevel@tonic-gate *cp = ':';
4757c478bd9Sstevel@tonic-gate if ((apidp->flags & FLAG_DISABLE_RCM) == 0)
4767c478bd9Sstevel@tonic-gate (void) scsi_rcm_remove(dev_list, errstring,
4777c478bd9Sstevel@tonic-gate flags);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate
4817c478bd9Sstevel@tonic-gate free_dev_list_elements(dev_list);
4827c478bd9Sstevel@tonic-gate return (ret);
4837c478bd9Sstevel@tonic-gate }
4847c478bd9Sstevel@tonic-gate
4857c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4867c478bd9Sstevel@tonic-gate scfga_ret_t
dev_insert(const char * func,scfga_cmd_t cmd,apid_t * apidp,prompt_t * prp,cfga_flags_t flags,char ** errstring)4877c478bd9Sstevel@tonic-gate dev_insert(
48809fe1b16Sdnielsen const char *func,
4897c478bd9Sstevel@tonic-gate scfga_cmd_t cmd,
4907c478bd9Sstevel@tonic-gate apid_t *apidp,
4917c478bd9Sstevel@tonic-gate prompt_t *prp,
4927c478bd9Sstevel@tonic-gate cfga_flags_t flags,
4937c478bd9Sstevel@tonic-gate char **errstring)
4947c478bd9Sstevel@tonic-gate {
4957c478bd9Sstevel@tonic-gate int proceed, l_errno = 0;
4967c478bd9Sstevel@tonic-gate scfga_ret_t ret;
4977c478bd9Sstevel@tonic-gate int do_quiesce;
4987c478bd9Sstevel@tonic-gate
4997c478bd9Sstevel@tonic-gate assert(apidp->hba_phys != NULL);
5007c478bd9Sstevel@tonic-gate assert(apidp->path != NULL);
5017c478bd9Sstevel@tonic-gate
5027c478bd9Sstevel@tonic-gate /* Currently, insert operation only allowed for bus */
5037c478bd9Sstevel@tonic-gate if (apidp->dyncomp != NULL) {
5047c478bd9Sstevel@tonic-gate cfga_err(errstring, 0, ERR_NOT_DEVOP, 0);
5057c478bd9Sstevel@tonic-gate return (SCFGA_ERR);
5067c478bd9Sstevel@tonic-gate }
5077c478bd9Sstevel@tonic-gate
5087c478bd9Sstevel@tonic-gate proceed = 1;
5097c478bd9Sstevel@tonic-gate ret = quiesce_confirm(apidp, MSG_INSDEV, prp, &proceed, &do_quiesce,
5107c478bd9Sstevel@tonic-gate &l_errno);
5117c478bd9Sstevel@tonic-gate if (ret != SCFGA_OK) {
5127c478bd9Sstevel@tonic-gate cfga_err(errstring, l_errno, ERR_DEV_INSERT, 0);
5137c478bd9Sstevel@tonic-gate return (ret);
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate
5167c478bd9Sstevel@tonic-gate if (!proceed) {
5177c478bd9Sstevel@tonic-gate return (SCFGA_NACK);
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate
5207c478bd9Sstevel@tonic-gate /* Do the physical addition */
5217c478bd9Sstevel@tonic-gate ret = dev_hotplug(apidp, prp, flags, do_quiesce, errstring);
5227c478bd9Sstevel@tonic-gate if (ret != SCFGA_OK) {
5237c478bd9Sstevel@tonic-gate return (ret);
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate
5267c478bd9Sstevel@tonic-gate /*
5277c478bd9Sstevel@tonic-gate * Configure bus to online new device(s).
5287c478bd9Sstevel@tonic-gate * Previously offlined devices will not be onlined.
5297c478bd9Sstevel@tonic-gate */
5307c478bd9Sstevel@tonic-gate ret = devctl_cmd(apidp->hba_phys, SCFGA_BUS_CONFIGURE, NULL, &l_errno);
5317c478bd9Sstevel@tonic-gate if (ret != SCFGA_OK) {
5327c478bd9Sstevel@tonic-gate cfga_err(errstring, l_errno, ERR_DEV_INSERT, 0);
5337c478bd9Sstevel@tonic-gate return (SCFGA_ERR);
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate
5367c478bd9Sstevel@tonic-gate return (SCFGA_OK);
5377c478bd9Sstevel@tonic-gate }
5387c478bd9Sstevel@tonic-gate
5397c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5407c478bd9Sstevel@tonic-gate scfga_ret_t
dev_replace(const char * func,scfga_cmd_t cmd,apid_t * apidp,prompt_t * prp,cfga_flags_t flags,char ** errstring)5417c478bd9Sstevel@tonic-gate dev_replace(
54209fe1b16Sdnielsen const char *func,
5437c478bd9Sstevel@tonic-gate scfga_cmd_t cmd,
5447c478bd9Sstevel@tonic-gate apid_t *apidp,
5457c478bd9Sstevel@tonic-gate prompt_t *prp,
5467c478bd9Sstevel@tonic-gate cfga_flags_t flags,
5477c478bd9Sstevel@tonic-gate char **errstring)
5487c478bd9Sstevel@tonic-gate {
5497c478bd9Sstevel@tonic-gate int proceed, l_errno = 0;
5507c478bd9Sstevel@tonic-gate scfga_ret_t ret, ret2;
5517c478bd9Sstevel@tonic-gate int do_quiesce;
5527c478bd9Sstevel@tonic-gate char *dev_list[2] = {NULL};
5537c478bd9Sstevel@tonic-gate
5547c478bd9Sstevel@tonic-gate assert(apidp->hba_phys != NULL);
5557c478bd9Sstevel@tonic-gate assert(apidp->path != NULL);
5567c478bd9Sstevel@tonic-gate
5577c478bd9Sstevel@tonic-gate /* device operation only */
5587c478bd9Sstevel@tonic-gate if (apidp->dyncomp == NULL) {
5597c478bd9Sstevel@tonic-gate cfga_err(errstring, 0, ERR_NOT_BUSOP, 0);
5607c478bd9Sstevel@tonic-gate return (SCFGA_ERR);
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate
5637c478bd9Sstevel@tonic-gate proceed = 1;
5647c478bd9Sstevel@tonic-gate ret = quiesce_confirm(apidp, MSG_REPLDEV, prp, &proceed, &do_quiesce,
5657c478bd9Sstevel@tonic-gate &l_errno);
5667c478bd9Sstevel@tonic-gate if (ret != SCFGA_OK) {
5677c478bd9Sstevel@tonic-gate cfga_err(errstring, l_errno, ERR_DEV_REPLACE, 0);
5687c478bd9Sstevel@tonic-gate return (ret);
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate
5717c478bd9Sstevel@tonic-gate if (!proceed) {
5727c478bd9Sstevel@tonic-gate return (SCFGA_NACK);
5737c478bd9Sstevel@tonic-gate }
5747c478bd9Sstevel@tonic-gate
5757c478bd9Sstevel@tonic-gate /* Offline the device in RCM */
5767c478bd9Sstevel@tonic-gate if ((apidp->flags & FLAG_DISABLE_RCM) == 0) {
5777c478bd9Sstevel@tonic-gate dev_list[0] = get_node_path(apidp->path);
5787c478bd9Sstevel@tonic-gate if (dev_list[0] == NULL)
5797c478bd9Sstevel@tonic-gate return (SCFGA_ERR);
5807c478bd9Sstevel@tonic-gate if ((ret = scsi_rcm_offline(dev_list, errstring, flags))
5817c478bd9Sstevel@tonic-gate != SCFGA_OK) {
5827c478bd9Sstevel@tonic-gate free_dev_list_elements(dev_list);
5837c478bd9Sstevel@tonic-gate return (ret);
5847c478bd9Sstevel@tonic-gate }
5857c478bd9Sstevel@tonic-gate }
5867c478bd9Sstevel@tonic-gate
5877c478bd9Sstevel@tonic-gate ret = devctl_cmd(apidp->path, SCFGA_DEV_REMOVE, NULL, &l_errno);
5887c478bd9Sstevel@tonic-gate if (ret != SCFGA_OK) {
5897c478bd9Sstevel@tonic-gate
5907c478bd9Sstevel@tonic-gate /*
5917c478bd9Sstevel@tonic-gate * Cancel the RCM offline. Discard any RCM failures so that
5927c478bd9Sstevel@tonic-gate * the devctl failure can still be reported.
5937c478bd9Sstevel@tonic-gate */
5947c478bd9Sstevel@tonic-gate if ((apidp->flags & FLAG_DISABLE_RCM) == 0)
5957c478bd9Sstevel@tonic-gate (void) scsi_rcm_online(dev_list, errstring, flags);
5967c478bd9Sstevel@tonic-gate
5977c478bd9Sstevel@tonic-gate cfga_err(errstring, l_errno, ERR_DEV_REPLACE, 0);
5987c478bd9Sstevel@tonic-gate free_dev_list_elements(dev_list);
5997c478bd9Sstevel@tonic-gate return (ret);
6007c478bd9Sstevel@tonic-gate }
6017c478bd9Sstevel@tonic-gate
6027c478bd9Sstevel@tonic-gate /* do the physical replace */
6037c478bd9Sstevel@tonic-gate ret = dev_hotplug(apidp, prp, flags, do_quiesce, errstring);
6047c478bd9Sstevel@tonic-gate
6057c478bd9Sstevel@tonic-gate /* Online the replacement, or restore state on error */
6067c478bd9Sstevel@tonic-gate ret2 = devctl_cmd(apidp->path, SCFGA_DEV_CONFIGURE, NULL, &l_errno);
6077c478bd9Sstevel@tonic-gate
6087c478bd9Sstevel@tonic-gate if (ret2 != SCFGA_OK) {
6097c478bd9Sstevel@tonic-gate cfga_err(errstring, l_errno, ERR_DEV_REPLACE, 0);
6107c478bd9Sstevel@tonic-gate }
6117c478bd9Sstevel@tonic-gate
6127c478bd9Sstevel@tonic-gate /*
6137c478bd9Sstevel@tonic-gate * Remove the replaced device in RCM, or online the device in RCM
6147c478bd9Sstevel@tonic-gate * to recover.
6157c478bd9Sstevel@tonic-gate */
6167c478bd9Sstevel@tonic-gate if ((apidp->flags & FLAG_DISABLE_RCM) == 0) {
6177c478bd9Sstevel@tonic-gate if (ret == SCFGA_OK)
6187c478bd9Sstevel@tonic-gate ret = scsi_rcm_remove(dev_list, errstring, flags);
6197c478bd9Sstevel@tonic-gate else if (ret2 == SCFGA_OK)
6207c478bd9Sstevel@tonic-gate ret2 = scsi_rcm_online(dev_list, errstring, flags);
6217c478bd9Sstevel@tonic-gate }
6227c478bd9Sstevel@tonic-gate free_dev_list_elements(dev_list);
6237c478bd9Sstevel@tonic-gate
6247c478bd9Sstevel@tonic-gate return (ret == SCFGA_OK ? ret2 : ret);
6257c478bd9Sstevel@tonic-gate }
6267c478bd9Sstevel@tonic-gate
62709fe1b16Sdnielsen #pragma weak plat_dev_led
62809fe1b16Sdnielsen /*ARGSUSED*/
62909fe1b16Sdnielsen scfga_ret_t
dev_led(const char * func,scfga_cmd_t cmd,apid_t * apidp,prompt_t * prp,cfga_flags_t flags,char ** errstring)63009fe1b16Sdnielsen dev_led(
63109fe1b16Sdnielsen const char *func,
63209fe1b16Sdnielsen scfga_cmd_t cmd,
63309fe1b16Sdnielsen apid_t *apidp,
63409fe1b16Sdnielsen prompt_t *prp,
63509fe1b16Sdnielsen cfga_flags_t flags,
63609fe1b16Sdnielsen char **errstring)
63709fe1b16Sdnielsen {
63809fe1b16Sdnielsen
63909fe1b16Sdnielsen /*
64009fe1b16Sdnielsen * The implementation of the led command is platform-specific, so
64109fe1b16Sdnielsen * the default behavior is to say that the functionality is not
64209fe1b16Sdnielsen * available for this device.
64309fe1b16Sdnielsen */
64409fe1b16Sdnielsen if (plat_dev_led) {
64509fe1b16Sdnielsen return (plat_dev_led(func, cmd, apidp, prp, flags, errstring));
64609fe1b16Sdnielsen }
64709fe1b16Sdnielsen cfga_err(errstring, 0, ERR_UNAVAILABLE, 0);
64809fe1b16Sdnielsen return (SCFGA_ERR);
64909fe1b16Sdnielsen }
65009fe1b16Sdnielsen
6517c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6527c478bd9Sstevel@tonic-gate scfga_ret_t
reset_common(const char * func,scfga_cmd_t cmd,apid_t * apidp,prompt_t * prp,cfga_flags_t flags,char ** errstring)6537c478bd9Sstevel@tonic-gate reset_common(
65409fe1b16Sdnielsen const char *func,
6557c478bd9Sstevel@tonic-gate scfga_cmd_t cmd,
6567c478bd9Sstevel@tonic-gate apid_t *apidp,
6577c478bd9Sstevel@tonic-gate prompt_t *prp,
6587c478bd9Sstevel@tonic-gate cfga_flags_t flags,
6597c478bd9Sstevel@tonic-gate char **errstring)
6607c478bd9Sstevel@tonic-gate {
6617c478bd9Sstevel@tonic-gate int l_errno = 0;
6627c478bd9Sstevel@tonic-gate scfga_ret_t ret;
6637c478bd9Sstevel@tonic-gate
6647c478bd9Sstevel@tonic-gate
6657c478bd9Sstevel@tonic-gate assert(apidp->path != NULL);
6667c478bd9Sstevel@tonic-gate assert(apidp->hba_phys != NULL);
6677c478bd9Sstevel@tonic-gate
6687c478bd9Sstevel@tonic-gate switch (cmd) {
6697c478bd9Sstevel@tonic-gate case SCFGA_RESET_DEV:
6707c478bd9Sstevel@tonic-gate if (apidp->dyncomp == NULL) {
6717c478bd9Sstevel@tonic-gate cfga_err(errstring, 0, ERR_NOT_BUSOP, 0);
6727c478bd9Sstevel@tonic-gate return (SCFGA_ERR);
6737c478bd9Sstevel@tonic-gate }
6747c478bd9Sstevel@tonic-gate break;
6757c478bd9Sstevel@tonic-gate
6767c478bd9Sstevel@tonic-gate case SCFGA_RESET_BUS:
6777c478bd9Sstevel@tonic-gate case SCFGA_RESET_ALL:
6787c478bd9Sstevel@tonic-gate if (apidp->dyncomp != NULL) {
6797c478bd9Sstevel@tonic-gate cfga_err(errstring, 0, ERR_NOT_DEVOP, 0);
6807c478bd9Sstevel@tonic-gate return (SCFGA_ERR);
6817c478bd9Sstevel@tonic-gate }
6827c478bd9Sstevel@tonic-gate break;
6837c478bd9Sstevel@tonic-gate default:
6847c478bd9Sstevel@tonic-gate cfga_err(errstring, 0, ERR_CMD_INVAL, 0);
6857c478bd9Sstevel@tonic-gate return (SCFGA_ERR);
6867c478bd9Sstevel@tonic-gate }
6877c478bd9Sstevel@tonic-gate
6887c478bd9Sstevel@tonic-gate ret = devctl_cmd(apidp->path, cmd, NULL, &l_errno);
6897c478bd9Sstevel@tonic-gate if (ret != SCFGA_OK) {
6907c478bd9Sstevel@tonic-gate cfga_err(errstring, l_errno, ERR_RESET, 0);
6917c478bd9Sstevel@tonic-gate }
6927c478bd9Sstevel@tonic-gate
6937c478bd9Sstevel@tonic-gate return (ret);
6947c478bd9Sstevel@tonic-gate }
6957c478bd9Sstevel@tonic-gate
6967c478bd9Sstevel@tonic-gate static int
disconnect(struct cfga_confirm * confp)6977c478bd9Sstevel@tonic-gate disconnect(struct cfga_confirm *confp)
6987c478bd9Sstevel@tonic-gate {
6997c478bd9Sstevel@tonic-gate int ans, append_newline;
7007c478bd9Sstevel@tonic-gate char *cq;
7017c478bd9Sstevel@tonic-gate
7027c478bd9Sstevel@tonic-gate append_newline = 0;
7037c478bd9Sstevel@tonic-gate cq = cfga_str(append_newline, WARN_DISCONNECT, 0);
7047c478bd9Sstevel@tonic-gate
7057c478bd9Sstevel@tonic-gate ans = confp->confirm(confp->appdata_ptr, cq);
7067c478bd9Sstevel@tonic-gate
7077c478bd9Sstevel@tonic-gate S_FREE(cq);
7087c478bd9Sstevel@tonic-gate
7097c478bd9Sstevel@tonic-gate return (ans == 1);
7107c478bd9Sstevel@tonic-gate }
7117c478bd9Sstevel@tonic-gate
7127c478bd9Sstevel@tonic-gate /*
7137c478bd9Sstevel@tonic-gate * Check for "scsi-no-quiesce" property
7147c478bd9Sstevel@tonic-gate * Return code: -1 error, 0 quiesce not required, 1 quiesce required
7157c478bd9Sstevel@tonic-gate */
7167c478bd9Sstevel@tonic-gate static int
quiesce_required(apid_t * apidp,int * l_errnop)7177c478bd9Sstevel@tonic-gate quiesce_required(apid_t *apidp, int *l_errnop)
7187c478bd9Sstevel@tonic-gate {
7197c478bd9Sstevel@tonic-gate di_node_t bus_node, dev_node;
7207c478bd9Sstevel@tonic-gate char *bus_path, *bus_end;
7217c478bd9Sstevel@tonic-gate char *dev_path, *dev_end;
7227c478bd9Sstevel@tonic-gate int *propval;
7237c478bd9Sstevel@tonic-gate
7247c478bd9Sstevel@tonic-gate /* take libdevinfo snapshot of subtree at hba */
7257c478bd9Sstevel@tonic-gate bus_path = apidp->hba_phys + strlen(DEVICES_DIR);
7267c478bd9Sstevel@tonic-gate bus_end = strrchr(bus_path, ':');
7277c478bd9Sstevel@tonic-gate if (bus_end)
7287c478bd9Sstevel@tonic-gate *bus_end = '\0';
7297c478bd9Sstevel@tonic-gate
7307c478bd9Sstevel@tonic-gate bus_node = di_init(bus_path, DINFOSUBTREE|DINFOPROP);
7317c478bd9Sstevel@tonic-gate if (bus_end)
7327c478bd9Sstevel@tonic-gate *bus_end = ':';
7337c478bd9Sstevel@tonic-gate if (bus_node == DI_NODE_NIL) {
7347c478bd9Sstevel@tonic-gate *l_errnop = errno;
7357c478bd9Sstevel@tonic-gate return (-1); /* error */
7367c478bd9Sstevel@tonic-gate }
7377c478bd9Sstevel@tonic-gate
7387c478bd9Sstevel@tonic-gate /* check bus node for property */
7397c478bd9Sstevel@tonic-gate if (di_prop_lookup_ints(DDI_DEV_T_ANY, bus_node, SCSI_NO_QUIESCE,
7407c478bd9Sstevel@tonic-gate &propval) == 1) {
7417c478bd9Sstevel@tonic-gate di_fini(bus_node);
7427c478bd9Sstevel@tonic-gate return (0); /* quiesce not required */
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate
7457c478bd9Sstevel@tonic-gate /* if this ap is HBA, return with quiesce required */
7467c478bd9Sstevel@tonic-gate if (apidp->dyncomp == NULL) {
7477c478bd9Sstevel@tonic-gate di_fini(bus_node);
7487c478bd9Sstevel@tonic-gate return (1);
7497c478bd9Sstevel@tonic-gate }
7507c478bd9Sstevel@tonic-gate
7517c478bd9Sstevel@tonic-gate /* check device node for property */
7527c478bd9Sstevel@tonic-gate dev_path = apidp->path + strlen(DEVICES_DIR);
7537c478bd9Sstevel@tonic-gate dev_end = strrchr(dev_path, ':');
7547c478bd9Sstevel@tonic-gate if (dev_end)
7557c478bd9Sstevel@tonic-gate *dev_end = '\0';
7567c478bd9Sstevel@tonic-gate
7577c478bd9Sstevel@tonic-gate dev_node = di_child_node(bus_node);
7587c478bd9Sstevel@tonic-gate while (dev_node != DI_NODE_NIL) {
7597c478bd9Sstevel@tonic-gate char *child_path;
7607c478bd9Sstevel@tonic-gate child_path = di_devfs_path(dev_node);
7617c478bd9Sstevel@tonic-gate if (strcmp(child_path, dev_path) == 0) {
7627c478bd9Sstevel@tonic-gate di_devfs_path_free(child_path);
7637c478bd9Sstevel@tonic-gate break;
7647c478bd9Sstevel@tonic-gate }
7657c478bd9Sstevel@tonic-gate di_devfs_path_free(child_path);
7667c478bd9Sstevel@tonic-gate dev_node = di_sibling_node(dev_node);
7677c478bd9Sstevel@tonic-gate }
7687c478bd9Sstevel@tonic-gate
7697c478bd9Sstevel@tonic-gate if (dev_end)
7707c478bd9Sstevel@tonic-gate *dev_end = ':';
7717c478bd9Sstevel@tonic-gate if (dev_node == DI_NODE_NIL) {
7727c478bd9Sstevel@tonic-gate di_fini(bus_node);
7737c478bd9Sstevel@tonic-gate return (1); /* dev not found (insert case) */
7747c478bd9Sstevel@tonic-gate }
7757c478bd9Sstevel@tonic-gate
7767c478bd9Sstevel@tonic-gate /* check child node for property */
7777c478bd9Sstevel@tonic-gate if (di_prop_lookup_ints(DDI_DEV_T_ANY, dev_node, "scsi-no-quiesce",
7787c478bd9Sstevel@tonic-gate &propval) == 1) {
7797c478bd9Sstevel@tonic-gate di_fini(bus_node);
7807c478bd9Sstevel@tonic-gate return (0); /* quiesce not required */
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate return (1); /* quiesce required */
7837c478bd9Sstevel@tonic-gate }
7847c478bd9Sstevel@tonic-gate
7857c478bd9Sstevel@tonic-gate static scfga_ret_t
quiesce_confirm(apid_t * apidp,msgid_t cmd_msg,prompt_t * prp,int * okp,int * quiesce,int * l_errnop)7867c478bd9Sstevel@tonic-gate quiesce_confirm(
7877c478bd9Sstevel@tonic-gate apid_t *apidp,
7887c478bd9Sstevel@tonic-gate msgid_t cmd_msg,
7897c478bd9Sstevel@tonic-gate prompt_t *prp,
7907c478bd9Sstevel@tonic-gate int *okp,
7917c478bd9Sstevel@tonic-gate int *quiesce,
7927c478bd9Sstevel@tonic-gate int *l_errnop)
7937c478bd9Sstevel@tonic-gate {
7947c478bd9Sstevel@tonic-gate char *buf = NULL, *hbap = NULL, *cq1 = NULL, *cq2 = NULL;
7957c478bd9Sstevel@tonic-gate char *cp;
7967c478bd9Sstevel@tonic-gate size_t len = 0;
7977c478bd9Sstevel@tonic-gate int i = 0, append_newline;
7987c478bd9Sstevel@tonic-gate scfga_ret_t ret;
7997c478bd9Sstevel@tonic-gate
8007c478bd9Sstevel@tonic-gate assert(apidp->path != NULL);
8017c478bd9Sstevel@tonic-gate assert(apidp->hba_phys != NULL);
8027c478bd9Sstevel@tonic-gate
8037c478bd9Sstevel@tonic-gate *quiesce = quiesce_required(apidp, l_errnop);
8047c478bd9Sstevel@tonic-gate if (*quiesce == -1)
8057c478bd9Sstevel@tonic-gate return (SCFGA_ERR);
8067c478bd9Sstevel@tonic-gate else if (*quiesce == 0)
8077c478bd9Sstevel@tonic-gate return (SCFGA_OK);
8087c478bd9Sstevel@tonic-gate
8097c478bd9Sstevel@tonic-gate /*
8107c478bd9Sstevel@tonic-gate * Try to create HBA logical ap_id.
8117c478bd9Sstevel@tonic-gate * If that fails use physical path
8127c478bd9Sstevel@tonic-gate */
8137c478bd9Sstevel@tonic-gate ret = make_hba_logid(apidp->hba_phys, &hbap, &i);
8147c478bd9Sstevel@tonic-gate if (ret != SCFGA_OK) {
8157c478bd9Sstevel@tonic-gate if ((hbap = get_node_path(apidp->hba_phys)) == NULL) {
8167c478bd9Sstevel@tonic-gate *l_errnop = errno;
8177c478bd9Sstevel@tonic-gate return (SCFGA_LIB_ERR);
8187c478bd9Sstevel@tonic-gate }
8197c478bd9Sstevel@tonic-gate }
8207c478bd9Sstevel@tonic-gate
8217c478bd9Sstevel@tonic-gate assert(hbap != NULL);
8227c478bd9Sstevel@tonic-gate
8237c478bd9Sstevel@tonic-gate append_newline = 0;
8247c478bd9Sstevel@tonic-gate cq1 = cfga_str(append_newline, CONF_QUIESCE_1, hbap, 0);
8257c478bd9Sstevel@tonic-gate cq2 = cfga_str(append_newline, CONF_QUIESCE_2, 0);
8267c478bd9Sstevel@tonic-gate len = strlen(cq1) + strlen(cq2) + 1; /* Includes term. NULL */
8277c478bd9Sstevel@tonic-gate
8287c478bd9Sstevel@tonic-gate if ((buf = calloc(1, len)) == NULL) {
8297c478bd9Sstevel@tonic-gate *l_errnop = errno;
8307c478bd9Sstevel@tonic-gate ret = SCFGA_LIB_ERR;
8317c478bd9Sstevel@tonic-gate S_FREE(cq1);
8327c478bd9Sstevel@tonic-gate S_FREE(cq2);
8337c478bd9Sstevel@tonic-gate goto out;
8347c478bd9Sstevel@tonic-gate }
8357c478bd9Sstevel@tonic-gate (void) strcpy(buf, cq1);
8367c478bd9Sstevel@tonic-gate (void) strcat(buf, cq2);
8377c478bd9Sstevel@tonic-gate
8387c478bd9Sstevel@tonic-gate S_FREE(cq1);
8397c478bd9Sstevel@tonic-gate S_FREE(cq2);
8407c478bd9Sstevel@tonic-gate
8417c478bd9Sstevel@tonic-gate
8427c478bd9Sstevel@tonic-gate /* Remove minor name (if any) from phys path */
8437c478bd9Sstevel@tonic-gate if ((cp = strrchr(apidp->path, ':')) != NULL) {
8447c478bd9Sstevel@tonic-gate *cp = '\0';
8457c478bd9Sstevel@tonic-gate }
8467c478bd9Sstevel@tonic-gate
8477c478bd9Sstevel@tonic-gate /* describe operation being attempted */
8487c478bd9Sstevel@tonic-gate cfga_msg(prp->msgp, cmd_msg, apidp->path, 0);
8497c478bd9Sstevel@tonic-gate
8507c478bd9Sstevel@tonic-gate /* Restore minor name */
8517c478bd9Sstevel@tonic-gate if (cp != NULL) {
8527c478bd9Sstevel@tonic-gate *cp = ':';
8537c478bd9Sstevel@tonic-gate }
8547c478bd9Sstevel@tonic-gate
8557c478bd9Sstevel@tonic-gate /* request permission to quiesce */
8567c478bd9Sstevel@tonic-gate assert(prp->confp != NULL && prp->confp->confirm != NULL);
8577c478bd9Sstevel@tonic-gate *okp = prp->confp->confirm(prp->confp->appdata_ptr, buf);
8587c478bd9Sstevel@tonic-gate
8597c478bd9Sstevel@tonic-gate ret = SCFGA_OK;
8607c478bd9Sstevel@tonic-gate /*FALLTHRU*/
8617c478bd9Sstevel@tonic-gate out:
8627c478bd9Sstevel@tonic-gate S_FREE(buf);
8637c478bd9Sstevel@tonic-gate S_FREE(hbap);
8647c478bd9Sstevel@tonic-gate return (ret);
8657c478bd9Sstevel@tonic-gate }
8667c478bd9Sstevel@tonic-gate
8677c478bd9Sstevel@tonic-gate static scfga_ret_t
suspend_in_rcm(apid_t * apidp,char *** suspend_list_ptr,char ** errstring,cfga_flags_t flags)8687c478bd9Sstevel@tonic-gate suspend_in_rcm(
8697c478bd9Sstevel@tonic-gate apid_t *apidp,
8707c478bd9Sstevel@tonic-gate char ***suspend_list_ptr,
8717c478bd9Sstevel@tonic-gate char **errstring,
8727c478bd9Sstevel@tonic-gate cfga_flags_t flags)
8737c478bd9Sstevel@tonic-gate {
8747c478bd9Sstevel@tonic-gate scfga_ret_t ret;
8757c478bd9Sstevel@tonic-gate char *bus_path = NULL;
8767c478bd9Sstevel@tonic-gate char *dev_path = NULL;
8777c478bd9Sstevel@tonic-gate char **suspend_list = NULL;
8787c478bd9Sstevel@tonic-gate
8797c478bd9Sstevel@tonic-gate *suspend_list_ptr = NULL;
8807c478bd9Sstevel@tonic-gate
8817c478bd9Sstevel@tonic-gate /* Suspend the bus through RCM */
8827c478bd9Sstevel@tonic-gate if (apidp->flags & FLAG_DISABLE_RCM)
8837c478bd9Sstevel@tonic-gate return (SCFGA_OK);
8847c478bd9Sstevel@tonic-gate
8857c478bd9Sstevel@tonic-gate /* The bus_path is the HBA path without its minor */
8867c478bd9Sstevel@tonic-gate if ((bus_path = get_node_path(apidp->hba_phys)) == NULL)
8877c478bd9Sstevel@tonic-gate return (SCFGA_ERR);
8887c478bd9Sstevel@tonic-gate
8897c478bd9Sstevel@tonic-gate /*
8907c478bd9Sstevel@tonic-gate * The dev_path is already initialized to NULL. If the AP Id
8917c478bd9Sstevel@tonic-gate * path differs from the HBA path, then the dev_path should
8927c478bd9Sstevel@tonic-gate * instead be set to the AP Id path without its minor.
8937c478bd9Sstevel@tonic-gate */
8947c478bd9Sstevel@tonic-gate if (strcmp(apidp->hba_phys, apidp->path) != 0) {
8957c478bd9Sstevel@tonic-gate if ((dev_path = get_node_path(apidp->path)) == NULL) {
8967c478bd9Sstevel@tonic-gate ret = SCFGA_ERR;
8977c478bd9Sstevel@tonic-gate goto out;
8987c478bd9Sstevel@tonic-gate }
8997c478bd9Sstevel@tonic-gate }
9007c478bd9Sstevel@tonic-gate
9017c478bd9Sstevel@tonic-gate if ((ret = get_hba_children(bus_path, dev_path, &suspend_list))
9027c478bd9Sstevel@tonic-gate != SCFGA_OK) {
9037c478bd9Sstevel@tonic-gate free_dev_list(suspend_list);
9047c478bd9Sstevel@tonic-gate goto out;
9057c478bd9Sstevel@tonic-gate }
9067c478bd9Sstevel@tonic-gate
9077c478bd9Sstevel@tonic-gate if (scsi_rcm_suspend(suspend_list, errstring, flags, 0) != SCFGA_OK) {
9087c478bd9Sstevel@tonic-gate ret = SCFGA_ERR;
9097c478bd9Sstevel@tonic-gate free_dev_list(suspend_list);
9107c478bd9Sstevel@tonic-gate } else {
9117c478bd9Sstevel@tonic-gate ret = SCFGA_OK;
9127c478bd9Sstevel@tonic-gate *suspend_list_ptr = suspend_list;
9137c478bd9Sstevel@tonic-gate }
9147c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/
9157c478bd9Sstevel@tonic-gate out:
9167c478bd9Sstevel@tonic-gate S_FREE(bus_path);
9177c478bd9Sstevel@tonic-gate S_FREE(dev_path);
9187c478bd9Sstevel@tonic-gate return (ret);
9197c478bd9Sstevel@tonic-gate }
9207c478bd9Sstevel@tonic-gate
9217c478bd9Sstevel@tonic-gate /*
9227c478bd9Sstevel@tonic-gate * Resume the bus through RCM if it successfully
9237c478bd9Sstevel@tonic-gate * unquiesced.
9247c478bd9Sstevel@tonic-gate */
9257c478bd9Sstevel@tonic-gate static void
resume_in_rcm(apid_t * apidp,char ** suspend_list,char ** errstring,cfga_flags_t flags)9267c478bd9Sstevel@tonic-gate resume_in_rcm(
9277c478bd9Sstevel@tonic-gate apid_t *apidp,
9287c478bd9Sstevel@tonic-gate char **suspend_list,
9297c478bd9Sstevel@tonic-gate char **errstring,
9307c478bd9Sstevel@tonic-gate cfga_flags_t flags)
9317c478bd9Sstevel@tonic-gate {
9327c478bd9Sstevel@tonic-gate if (apidp->flags & FLAG_DISABLE_RCM)
9337c478bd9Sstevel@tonic-gate return;
9347c478bd9Sstevel@tonic-gate
9357c478bd9Sstevel@tonic-gate (void) scsi_rcm_resume(suspend_list, errstring, flags, 0);
9367c478bd9Sstevel@tonic-gate
9377c478bd9Sstevel@tonic-gate free_dev_list(suspend_list);
9387c478bd9Sstevel@tonic-gate }
9397c478bd9Sstevel@tonic-gate
9407c478bd9Sstevel@tonic-gate static scfga_ret_t
wait_for_hotplug(prompt_t * pt,int msg)9417c478bd9Sstevel@tonic-gate wait_for_hotplug(prompt_t *pt, int msg)
9427c478bd9Sstevel@tonic-gate {
9437c478bd9Sstevel@tonic-gate char *cu = NULL;
9447c478bd9Sstevel@tonic-gate int append_newline = 0;
9457c478bd9Sstevel@tonic-gate scfga_ret_t ret;
9467c478bd9Sstevel@tonic-gate
9477c478bd9Sstevel@tonic-gate cu = cfga_str(append_newline, msg, 0);
9487c478bd9Sstevel@tonic-gate if (pt->confp->confirm(pt->confp->appdata_ptr, cu) != 1) {
9497c478bd9Sstevel@tonic-gate ret = SCFGA_NACK;
9507c478bd9Sstevel@tonic-gate } else {
9517c478bd9Sstevel@tonic-gate ret = SCFGA_OK;
9527c478bd9Sstevel@tonic-gate }
9537c478bd9Sstevel@tonic-gate S_FREE(cu);
9547c478bd9Sstevel@tonic-gate return (ret);
9557c478bd9Sstevel@tonic-gate }
9567c478bd9Sstevel@tonic-gate
9577c478bd9Sstevel@tonic-gate static scfga_ret_t
bus_quiesce(apid_t * apidp,prompt_t * pt,char ** errstring,cfga_flags_t flags)9587c478bd9Sstevel@tonic-gate bus_quiesce(apid_t *apidp, prompt_t *pt, char **errstring, cfga_flags_t flags)
9597c478bd9Sstevel@tonic-gate {
9607c478bd9Sstevel@tonic-gate int l_errno;
9617c478bd9Sstevel@tonic-gate scfga_ret_t ret;
9627c478bd9Sstevel@tonic-gate scfga_ret_t hpret;
9637c478bd9Sstevel@tonic-gate char **suspend_list = NULL;
9647c478bd9Sstevel@tonic-gate
9657c478bd9Sstevel@tonic-gate ret = suspend_in_rcm(apidp, &suspend_list, errstring, flags);
9667c478bd9Sstevel@tonic-gate if (ret != SCFGA_OK) {
9677c478bd9Sstevel@tonic-gate return (ret);
9687c478bd9Sstevel@tonic-gate }
9697c478bd9Sstevel@tonic-gate
9707c478bd9Sstevel@tonic-gate /*
9717c478bd9Sstevel@tonic-gate * If the quiesce fails, then cancel the RCM suspend.
9727c478bd9Sstevel@tonic-gate * Discard any RCM failures so that the devctl failure
9737c478bd9Sstevel@tonic-gate * can still be reported.
9747c478bd9Sstevel@tonic-gate */
9757c478bd9Sstevel@tonic-gate l_errno = 0;
9767c478bd9Sstevel@tonic-gate ret = devctl_cmd(apidp->hba_phys, SCFGA_BUS_QUIESCE, NULL, &l_errno);
9777c478bd9Sstevel@tonic-gate if (ret != SCFGA_OK) {
9787c478bd9Sstevel@tonic-gate resume_in_rcm(apidp, suspend_list, errstring, flags);
9797c478bd9Sstevel@tonic-gate cfga_err(errstring, l_errno, ERR_BUS_QUIESCE, 0);
9807c478bd9Sstevel@tonic-gate return (ret);
9817c478bd9Sstevel@tonic-gate }
9827c478bd9Sstevel@tonic-gate
9837c478bd9Sstevel@tonic-gate /*
9847c478bd9Sstevel@tonic-gate * Prompt user to proceed with physical hotplug
9857c478bd9Sstevel@tonic-gate * and wait until they are done.
9867c478bd9Sstevel@tonic-gate */
9877c478bd9Sstevel@tonic-gate hpret = wait_for_hotplug(pt, CONF_UNQUIESCE);
9887c478bd9Sstevel@tonic-gate
9897c478bd9Sstevel@tonic-gate /*
9907c478bd9Sstevel@tonic-gate * The unquiesce may fail with EALREADY (which is ok)
9917c478bd9Sstevel@tonic-gate * or some other error (which is not ok).
9927c478bd9Sstevel@tonic-gate */
9937c478bd9Sstevel@tonic-gate l_errno = 0;
9947c478bd9Sstevel@tonic-gate ret = devctl_cmd(apidp->hba_phys, SCFGA_BUS_UNQUIESCE, NULL, &l_errno);
9957c478bd9Sstevel@tonic-gate if (ret != SCFGA_OK && l_errno != EALREADY) {
9967c478bd9Sstevel@tonic-gate free_dev_list(suspend_list);
9977c478bd9Sstevel@tonic-gate cfga_err(errstring, l_errno, ERR_BUS_UNQUIESCE, 0);
9987c478bd9Sstevel@tonic-gate return (SCFGA_ERR);
9997c478bd9Sstevel@tonic-gate }
10007c478bd9Sstevel@tonic-gate
10017c478bd9Sstevel@tonic-gate resume_in_rcm(apidp, suspend_list, errstring, flags);
10027c478bd9Sstevel@tonic-gate
10037c478bd9Sstevel@tonic-gate return (hpret);
10047c478bd9Sstevel@tonic-gate }
10057c478bd9Sstevel@tonic-gate
10067c478bd9Sstevel@tonic-gate #define MAX_LOCK_TRIES 20
10077c478bd9Sstevel@tonic-gate #define MAX_UNLINK_TRIES 60
10087c478bd9Sstevel@tonic-gate #define s_getpid (int)getpid /* else lint causes problems */
10097c478bd9Sstevel@tonic-gate
10107c478bd9Sstevel@tonic-gate static void
s_unlink(char * file)10117c478bd9Sstevel@tonic-gate s_unlink(char *file)
10127c478bd9Sstevel@tonic-gate {
10137c478bd9Sstevel@tonic-gate int count = 0;
10147c478bd9Sstevel@tonic-gate
10157c478bd9Sstevel@tonic-gate retry:
10167c478bd9Sstevel@tonic-gate if (unlink(file) == -1) {
10177c478bd9Sstevel@tonic-gate if (errno != EINTR && errno != EAGAIN) {
10187c478bd9Sstevel@tonic-gate CFGA_TRACE1((stdout, "s_unlink[%d]: unlink failed: "
10197c478bd9Sstevel@tonic-gate "%s: %s\n", s_getpid(), file, strerror(errno)));
10207c478bd9Sstevel@tonic-gate return;
10217c478bd9Sstevel@tonic-gate }
10227c478bd9Sstevel@tonic-gate
10237c478bd9Sstevel@tonic-gate if (++count < MAX_UNLINK_TRIES) {
10247c478bd9Sstevel@tonic-gate (void) sleep(1);
10257c478bd9Sstevel@tonic-gate goto retry;
10267c478bd9Sstevel@tonic-gate }
10277c478bd9Sstevel@tonic-gate CFGA_TRACE1((stdout, "s_unlink[%d]: retry limit: %s\n",
10287c478bd9Sstevel@tonic-gate s_getpid(), file));
10297c478bd9Sstevel@tonic-gate } else {
10307c478bd9Sstevel@tonic-gate CFGA_TRACE3((stdout, "s_unlink[%d]: unlinked: %s\n",
10317c478bd9Sstevel@tonic-gate s_getpid(), file));
10327c478bd9Sstevel@tonic-gate }
10337c478bd9Sstevel@tonic-gate }
10347c478bd9Sstevel@tonic-gate
10357c478bd9Sstevel@tonic-gate static scfga_ret_t
create_lock(int * fdp,struct cfga_msg * msgp,char ** errstring)10367c478bd9Sstevel@tonic-gate create_lock(int *fdp, struct cfga_msg *msgp, char **errstring)
10377c478bd9Sstevel@tonic-gate {
10387c478bd9Sstevel@tonic-gate FILE *fp;
10397c478bd9Sstevel@tonic-gate int count;
10407c478bd9Sstevel@tonic-gate struct extmnttab ent;
10417c478bd9Sstevel@tonic-gate int mnted;
10427c478bd9Sstevel@tonic-gate
10437c478bd9Sstevel@tonic-gate
10447c478bd9Sstevel@tonic-gate *fdp = -1;
10457c478bd9Sstevel@tonic-gate
10467c478bd9Sstevel@tonic-gate /*
10477c478bd9Sstevel@tonic-gate * Check that /var/run is mounted. In the unlikely event
10487c478bd9Sstevel@tonic-gate * that the lock file is left behind, we want it
10497c478bd9Sstevel@tonic-gate * cleared on the next reboot.
10507c478bd9Sstevel@tonic-gate */
10517c478bd9Sstevel@tonic-gate errno = 0;
10527c478bd9Sstevel@tonic-gate if ((fp = fopen(MNTTAB, "r")) == NULL) {
10537c478bd9Sstevel@tonic-gate cfga_err(errstring, errno, ERRARG_OPEN, MNTTAB, 0);
10547c478bd9Sstevel@tonic-gate return (SCFGA_LIB_ERR);
10557c478bd9Sstevel@tonic-gate }
10567c478bd9Sstevel@tonic-gate
10577c478bd9Sstevel@tonic-gate resetmnttab(fp);
10587c478bd9Sstevel@tonic-gate
10597c478bd9Sstevel@tonic-gate mnted = 0;
10607c478bd9Sstevel@tonic-gate while (getextmntent(fp, &ent, sizeof (ent)) == 0) {
10617c478bd9Sstevel@tonic-gate if (strcmp(ent.mnt_mountp, "/var/run") == 0) {
10627c478bd9Sstevel@tonic-gate mnted = 1;
10637c478bd9Sstevel@tonic-gate break;
10647c478bd9Sstevel@tonic-gate }
10657c478bd9Sstevel@tonic-gate }
10667c478bd9Sstevel@tonic-gate
10677c478bd9Sstevel@tonic-gate (void) fclose(fp);
10687c478bd9Sstevel@tonic-gate
10697c478bd9Sstevel@tonic-gate if (!mnted) {
10707c478bd9Sstevel@tonic-gate cfga_err(errstring, 0, ERR_VAR_RUN, 0);
10717c478bd9Sstevel@tonic-gate return (SCFGA_LIB_ERR);
10727c478bd9Sstevel@tonic-gate }
10737c478bd9Sstevel@tonic-gate
10747c478bd9Sstevel@tonic-gate /*
10757c478bd9Sstevel@tonic-gate * Wait for a short period of time if we cannot O_EXCL create
10767c478bd9Sstevel@tonic-gate * lock file. If some other cfgadm process is finishing up, we
10777c478bd9Sstevel@tonic-gate * can get in. If the wait required is long however, just
10787c478bd9Sstevel@tonic-gate * return SYSTEM_BUSY to the user - a hotplug operation is
10797c478bd9Sstevel@tonic-gate * probably in progress.
10807c478bd9Sstevel@tonic-gate */
10817c478bd9Sstevel@tonic-gate count = 0;
10827c478bd9Sstevel@tonic-gate retry:
10837c478bd9Sstevel@tonic-gate *fdp = open(SCFGA_LOCK, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
10847c478bd9Sstevel@tonic-gate if (*fdp == -1 && errno == EEXIST) {
10857c478bd9Sstevel@tonic-gate if (++count < MAX_LOCK_TRIES) {
10867c478bd9Sstevel@tonic-gate if (count == 1)
10877c478bd9Sstevel@tonic-gate cfga_msg(msgp, MSG_WAIT_LOCK, 0);
10887c478bd9Sstevel@tonic-gate (void) sleep(1);
10897c478bd9Sstevel@tonic-gate goto retry;
10907c478bd9Sstevel@tonic-gate }
10917c478bd9Sstevel@tonic-gate }
10927c478bd9Sstevel@tonic-gate
10937c478bd9Sstevel@tonic-gate if (*fdp == -1 && errno == EEXIST) {
10947c478bd9Sstevel@tonic-gate cfga_err(errstring, 0, ERRARG_QUIESCE_LOCK, SCFGA_LOCK, 0);
10957c478bd9Sstevel@tonic-gate return (SCFGA_SYSTEM_BUSY);
10967c478bd9Sstevel@tonic-gate } else if (*fdp == -1) {
10977c478bd9Sstevel@tonic-gate cfga_err(errstring, errno, ERRARG_QUIESCE_LOCK, SCFGA_LOCK, 0);
10987c478bd9Sstevel@tonic-gate return (SCFGA_LIB_ERR);
10997c478bd9Sstevel@tonic-gate }
11007c478bd9Sstevel@tonic-gate
11017c478bd9Sstevel@tonic-gate CFGA_TRACE3((stdout, "create_lock[%d]: created lockfile: %s\n",
11027c478bd9Sstevel@tonic-gate s_getpid(), SCFGA_LOCK));
11037c478bd9Sstevel@tonic-gate
11047c478bd9Sstevel@tonic-gate return (SCFGA_OK);
11057c478bd9Sstevel@tonic-gate }
11067c478bd9Sstevel@tonic-gate
11077c478bd9Sstevel@tonic-gate static scfga_ret_t
syslock(int fd,char ** errstring)11087c478bd9Sstevel@tonic-gate syslock(int fd, char **errstring)
11097c478bd9Sstevel@tonic-gate {
11107c478bd9Sstevel@tonic-gate struct flock lock;
11117c478bd9Sstevel@tonic-gate int count;
11127c478bd9Sstevel@tonic-gate int rval;
11137c478bd9Sstevel@tonic-gate
11147c478bd9Sstevel@tonic-gate assert(fd != -1);
11157c478bd9Sstevel@tonic-gate
11167c478bd9Sstevel@tonic-gate CFGA_TRACE3((stdout, "syslock[%d]: trying lock: %s\n",
11177c478bd9Sstevel@tonic-gate s_getpid(), SCFGA_LOCK));
11187c478bd9Sstevel@tonic-gate
11197c478bd9Sstevel@tonic-gate lock.l_type = F_WRLCK;
11207c478bd9Sstevel@tonic-gate lock.l_whence = SEEK_SET;
11217c478bd9Sstevel@tonic-gate lock.l_start = 0;
11227c478bd9Sstevel@tonic-gate lock.l_len = 0;
11237c478bd9Sstevel@tonic-gate
11247c478bd9Sstevel@tonic-gate count = 0;
11257c478bd9Sstevel@tonic-gate while ((rval = fcntl(fd, F_SETLKW, &lock)) == -1 && errno == EINTR) {
11267c478bd9Sstevel@tonic-gate if (++count >= MAX_LOCK_TRIES) {
11277c478bd9Sstevel@tonic-gate CFGA_TRACE1((stdout, "syslock[%d]: retry limit: %s\n",
11287c478bd9Sstevel@tonic-gate s_getpid(), SCFGA_LOCK));
11297c478bd9Sstevel@tonic-gate goto badlock;
11307c478bd9Sstevel@tonic-gate }
11317c478bd9Sstevel@tonic-gate (void) sleep(1);
11327c478bd9Sstevel@tonic-gate }
11337c478bd9Sstevel@tonic-gate
11347c478bd9Sstevel@tonic-gate if (rval != -1) {
11357c478bd9Sstevel@tonic-gate CFGA_TRACE3((stdout, "syslock[%d]: locked file: %s\n",
11367c478bd9Sstevel@tonic-gate s_getpid(), SCFGA_LOCK));
11377c478bd9Sstevel@tonic-gate return (SCFGA_OK);
11387c478bd9Sstevel@tonic-gate }
11397c478bd9Sstevel@tonic-gate
11407c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/
11417c478bd9Sstevel@tonic-gate badlock:
11427c478bd9Sstevel@tonic-gate cfga_err(errstring, errno, ERRARG_LOCK, SCFGA_LOCK, 0);
11437c478bd9Sstevel@tonic-gate /* trace message to display pid */
11447c478bd9Sstevel@tonic-gate CFGA_TRACE1((stdout, "syslock[%d]: cannot lock %s\n",
11457c478bd9Sstevel@tonic-gate s_getpid(), SCFGA_LOCK));
11467c478bd9Sstevel@tonic-gate return (SCFGA_LIB_ERR);
11477c478bd9Sstevel@tonic-gate }
11487c478bd9Sstevel@tonic-gate
11497c478bd9Sstevel@tonic-gate static void
wait_for_child(pid_t cpid)11507c478bd9Sstevel@tonic-gate wait_for_child(pid_t cpid)
11517c478bd9Sstevel@tonic-gate {
11527c478bd9Sstevel@tonic-gate int status;
11537c478bd9Sstevel@tonic-gate pid_t rval;
11547c478bd9Sstevel@tonic-gate
11557c478bd9Sstevel@tonic-gate CFGA_TRACE2((stdout, "wait_for_child[%d]: child[%d]\n",
11567c478bd9Sstevel@tonic-gate s_getpid(), (int)cpid));
11577c478bd9Sstevel@tonic-gate
11587c478bd9Sstevel@tonic-gate for (;;) {
11597c478bd9Sstevel@tonic-gate while ((rval = waitpid(cpid, &status, 0)) != cpid) {
11607c478bd9Sstevel@tonic-gate if (errno == ECHILD) {
11617c478bd9Sstevel@tonic-gate CFGA_TRACE1((stdout, "waitpid[%d]: child[%d] "
11627c478bd9Sstevel@tonic-gate "doesn't exist\n", s_getpid(), (int)cpid));
11637c478bd9Sstevel@tonic-gate return;
11647c478bd9Sstevel@tonic-gate }
11657c478bd9Sstevel@tonic-gate
11667c478bd9Sstevel@tonic-gate CFGA_TRACE3((stdout, "waitpid[%d]: returned: %d"
11677c478bd9Sstevel@tonic-gate ": errno: %s\n", s_getpid(), (int)rval,
11687c478bd9Sstevel@tonic-gate strerror(errno)));
11697c478bd9Sstevel@tonic-gate }
11707c478bd9Sstevel@tonic-gate
11717c478bd9Sstevel@tonic-gate if (WIFEXITED(status)) {
11727c478bd9Sstevel@tonic-gate CFGA_TRACE2((stdout, "waitpid[%d]: child[%d]: "
11737c478bd9Sstevel@tonic-gate "normal exit\n", s_getpid(), (int)cpid));
11747c478bd9Sstevel@tonic-gate return;
11757c478bd9Sstevel@tonic-gate }
11767c478bd9Sstevel@tonic-gate
11777c478bd9Sstevel@tonic-gate if (WIFSIGNALED(status)) {
11787c478bd9Sstevel@tonic-gate CFGA_TRACE2((stdout, "waitpid[%d]: child[%d]: "
11797c478bd9Sstevel@tonic-gate "signal exit\n", s_getpid(), (int)cpid));
11807c478bd9Sstevel@tonic-gate return;
11817c478bd9Sstevel@tonic-gate }
11827c478bd9Sstevel@tonic-gate
11837c478bd9Sstevel@tonic-gate /*
11847c478bd9Sstevel@tonic-gate * The child has not terminated. We received status
11857c478bd9Sstevel@tonic-gate * because the child was either stopped or continued.
11867c478bd9Sstevel@tonic-gate * Wait for child termination by calling waitpid() again.
11877c478bd9Sstevel@tonic-gate */
11887c478bd9Sstevel@tonic-gate }
11897c478bd9Sstevel@tonic-gate }
11907c478bd9Sstevel@tonic-gate
11917c478bd9Sstevel@tonic-gate static void
wait_and_cleanup(int fd,apid_t * apidp)11927c478bd9Sstevel@tonic-gate wait_and_cleanup(int fd, apid_t *apidp)
11937c478bd9Sstevel@tonic-gate {
11947c478bd9Sstevel@tonic-gate int l_errno;
11957c478bd9Sstevel@tonic-gate scfga_ret_t ret;
11967c478bd9Sstevel@tonic-gate
11977c478bd9Sstevel@tonic-gate /* This is the child */
11987c478bd9Sstevel@tonic-gate CFGA_TRACE2((stdout, "child[%d]: Entering wait_cleanup\n", s_getpid()));
11997c478bd9Sstevel@tonic-gate
12007c478bd9Sstevel@tonic-gate if (syslock(fd, NULL) != SCFGA_OK) {
12017c478bd9Sstevel@tonic-gate CFGA_TRACE1((stdout, "child[%d]: lock failure "
12027c478bd9Sstevel@tonic-gate " - _exit(1)\n", s_getpid()));
12037c478bd9Sstevel@tonic-gate /*
12047c478bd9Sstevel@tonic-gate * As a last resort, unlink the lock file. This is relatively
12057c478bd9Sstevel@tonic-gate * safe as the child doesn't unquiesce the bus in this case.
12067c478bd9Sstevel@tonic-gate */
12077c478bd9Sstevel@tonic-gate s_unlink(SCFGA_LOCK);
12087c478bd9Sstevel@tonic-gate _exit(1);
12097c478bd9Sstevel@tonic-gate }
12107c478bd9Sstevel@tonic-gate
12117c478bd9Sstevel@tonic-gate l_errno = 0;
12127c478bd9Sstevel@tonic-gate ret = devctl_cmd(apidp->hba_phys, SCFGA_BUS_UNQUIESCE, NULL, &l_errno);
12137c478bd9Sstevel@tonic-gate if (ret != SCFGA_OK) {
12147c478bd9Sstevel@tonic-gate if (l_errno == EALREADY)
12157c478bd9Sstevel@tonic-gate CFGA_TRACE3((stdout, "child[%d]: bus already "
12167c478bd9Sstevel@tonic-gate "unquiesced: %s\n", s_getpid(), apidp->hba_phys));
12177c478bd9Sstevel@tonic-gate else
12187c478bd9Sstevel@tonic-gate CFGA_TRACE1((stdout, "child[%d]: unquiesce failed: "
12197c478bd9Sstevel@tonic-gate "%s\n", s_getpid(), strerror(l_errno)));
12207c478bd9Sstevel@tonic-gate } else {
12217c478bd9Sstevel@tonic-gate CFGA_TRACE1((stdout, "child[%d]: unquiesced bus: %s\n",
12227c478bd9Sstevel@tonic-gate s_getpid(), apidp->hba_phys));
12237c478bd9Sstevel@tonic-gate }
12247c478bd9Sstevel@tonic-gate
12257c478bd9Sstevel@tonic-gate s_unlink(SCFGA_LOCK);
12267c478bd9Sstevel@tonic-gate
12277c478bd9Sstevel@tonic-gate CFGA_TRACE2((stdout, "child[%d]: _exit(0)\n", s_getpid()));
12287c478bd9Sstevel@tonic-gate
12297c478bd9Sstevel@tonic-gate _exit(0);
12307c478bd9Sstevel@tonic-gate }
12317c478bd9Sstevel@tonic-gate
12327c478bd9Sstevel@tonic-gate static void
sigblk(sigset_t * osp)12337c478bd9Sstevel@tonic-gate sigblk(sigset_t *osp)
12347c478bd9Sstevel@tonic-gate {
12357c478bd9Sstevel@tonic-gate sigset_t set;
12367c478bd9Sstevel@tonic-gate
12377c478bd9Sstevel@tonic-gate (void) sigemptyset(&set);
12387c478bd9Sstevel@tonic-gate (void) sigemptyset(osp);
12397c478bd9Sstevel@tonic-gate (void) sigaddset(&set, SIGHUP);
12407c478bd9Sstevel@tonic-gate (void) sigaddset(&set, SIGINT);
12417c478bd9Sstevel@tonic-gate (void) sigaddset(&set, SIGQUIT);
12427c478bd9Sstevel@tonic-gate (void) sigaddset(&set, SIGTERM);
12437c478bd9Sstevel@tonic-gate (void) sigaddset(&set, SIGUSR1);
12447c478bd9Sstevel@tonic-gate (void) sigaddset(&set, SIGUSR2);
12457c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &set, osp);
12467c478bd9Sstevel@tonic-gate }
12477c478bd9Sstevel@tonic-gate
12487c478bd9Sstevel@tonic-gate static void
sigunblk(sigset_t * osp)12497c478bd9Sstevel@tonic-gate sigunblk(sigset_t *osp)
12507c478bd9Sstevel@tonic-gate {
12517c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, osp, NULL);
12527c478bd9Sstevel@tonic-gate }
12537c478bd9Sstevel@tonic-gate
12547c478bd9Sstevel@tonic-gate /*
12557c478bd9Sstevel@tonic-gate * Here is the algorithm used to ensure that a SCSI bus is not
12567c478bd9Sstevel@tonic-gate * left in the quiesced state:
12577c478bd9Sstevel@tonic-gate *
12587c478bd9Sstevel@tonic-gate * lock quiesce mutex // single threads this code
12597c478bd9Sstevel@tonic-gate * open(O_CREAT|O_EXCL) lock file // only 1 process at a time
12607c478bd9Sstevel@tonic-gate * exclusive record lock on lock file
12617c478bd9Sstevel@tonic-gate * fork1()
12627c478bd9Sstevel@tonic-gate * quiesce bus
12637c478bd9Sstevel@tonic-gate * do the physical hotplug operation
12647c478bd9Sstevel@tonic-gate * unquiesce bus
12657c478bd9Sstevel@tonic-gate * unlock record lock
12667c478bd9Sstevel@tonic-gate * -> *child*
12677c478bd9Sstevel@tonic-gate * -> wait for record lock
12687c478bd9Sstevel@tonic-gate * -> unconditionally unquiesce bus
12697c478bd9Sstevel@tonic-gate * -> unlink lock file
12707c478bd9Sstevel@tonic-gate * -> exit
12717c478bd9Sstevel@tonic-gate * wait for child to exit
12727c478bd9Sstevel@tonic-gate * unlock quiesce mutex
12737c478bd9Sstevel@tonic-gate *
12747c478bd9Sstevel@tonic-gate * NOTE1: since record locks are per-process and a close() can
12757c478bd9Sstevel@tonic-gate * release a lock, to keep things MT-safe we need a quiesce mutex.
12767c478bd9Sstevel@tonic-gate *
12777c478bd9Sstevel@tonic-gate * NOTE2: To ensure that the child does not unquiesce a bus quiesced
12787c478bd9Sstevel@tonic-gate * by an unrelated cfgadm_scsi operation, exactly 1 process in the
12797c478bd9Sstevel@tonic-gate * system can be doing an implicit quiesce operation The exclusive
12807c478bd9Sstevel@tonic-gate * creation of the lock file guarantees this.
12817c478bd9Sstevel@tonic-gate *
12827c478bd9Sstevel@tonic-gate * NOTE3: This works even if the parent process dumps core and/or is
12837c478bd9Sstevel@tonic-gate * abnormally terminated. If the parent dies before the child is
12847c478bd9Sstevel@tonic-gate * forked, the bus is not quiesced. If the parent dies after the
12857c478bd9Sstevel@tonic-gate * bus is quiesced, the child process will ensure that the bus is
12867c478bd9Sstevel@tonic-gate * unquiesced.
12877c478bd9Sstevel@tonic-gate */
12887c478bd9Sstevel@tonic-gate static scfga_ret_t
dev_hotplug(apid_t * apidp,prompt_t * pt,cfga_flags_t flags,int do_quiesce,char ** errstring)12897c478bd9Sstevel@tonic-gate dev_hotplug(
12907c478bd9Sstevel@tonic-gate apid_t *apidp,
12917c478bd9Sstevel@tonic-gate prompt_t *pt,
12927c478bd9Sstevel@tonic-gate cfga_flags_t flags,
12937c478bd9Sstevel@tonic-gate int do_quiesce,
12947c478bd9Sstevel@tonic-gate char **errstring)
12957c478bd9Sstevel@tonic-gate {
12967c478bd9Sstevel@tonic-gate scfga_ret_t ret;
12977c478bd9Sstevel@tonic-gate pid_t cpid;
12987c478bd9Sstevel@tonic-gate int fd;
12997c478bd9Sstevel@tonic-gate sigset_t oset;
13007c478bd9Sstevel@tonic-gate
13017c478bd9Sstevel@tonic-gate assert(apidp->hba_phys != NULL);
13027c478bd9Sstevel@tonic-gate assert(apidp->path != NULL);
13037c478bd9Sstevel@tonic-gate
13047c478bd9Sstevel@tonic-gate /* If no quiesce required, prompt the user to do the operation */
13057c478bd9Sstevel@tonic-gate if (!do_quiesce)
13067c478bd9Sstevel@tonic-gate return (wait_for_hotplug(pt, CONF_NO_QUIESCE));
13077c478bd9Sstevel@tonic-gate
13087c478bd9Sstevel@tonic-gate (void) mutex_lock(&quiesce_mutex);
13097c478bd9Sstevel@tonic-gate
13107c478bd9Sstevel@tonic-gate ret = create_lock(&fd, pt->msgp, errstring);
13117c478bd9Sstevel@tonic-gate if (ret != SCFGA_OK) {
13127c478bd9Sstevel@tonic-gate (void) mutex_unlock(&quiesce_mutex);
13137c478bd9Sstevel@tonic-gate return (ret);
13147c478bd9Sstevel@tonic-gate }
13157c478bd9Sstevel@tonic-gate
13167c478bd9Sstevel@tonic-gate ret = syslock(fd, errstring);
13177c478bd9Sstevel@tonic-gate if (ret != SCFGA_OK) {
13187c478bd9Sstevel@tonic-gate goto bad;
13197c478bd9Sstevel@tonic-gate }
13207c478bd9Sstevel@tonic-gate
13217c478bd9Sstevel@tonic-gate /*
13227c478bd9Sstevel@tonic-gate * block signals in the child. Parent may
13237c478bd9Sstevel@tonic-gate * exit, causing signal to be sent to child.
13247c478bd9Sstevel@tonic-gate */
13257c478bd9Sstevel@tonic-gate sigblk(&oset);
13267c478bd9Sstevel@tonic-gate
13277c478bd9Sstevel@tonic-gate switch (cpid = fork1()) {
13287c478bd9Sstevel@tonic-gate case 0:
13297c478bd9Sstevel@tonic-gate /* child */
13307c478bd9Sstevel@tonic-gate wait_and_cleanup(fd, apidp);
13317c478bd9Sstevel@tonic-gate _exit(0); /* paranoia */
13327c478bd9Sstevel@tonic-gate /*NOTREACHED*/
13337c478bd9Sstevel@tonic-gate case -1:
13347c478bd9Sstevel@tonic-gate cfga_err(errstring, errno, ERR_FORK, 0);
13357c478bd9Sstevel@tonic-gate sigunblk(&oset);
13367c478bd9Sstevel@tonic-gate ret = SCFGA_LIB_ERR;
13377c478bd9Sstevel@tonic-gate goto bad;
13387c478bd9Sstevel@tonic-gate default:
13397c478bd9Sstevel@tonic-gate /* parent */
13407c478bd9Sstevel@tonic-gate break;
13417c478bd9Sstevel@tonic-gate }
13427c478bd9Sstevel@tonic-gate
13437c478bd9Sstevel@tonic-gate sigunblk(&oset);
13447c478bd9Sstevel@tonic-gate
13457c478bd9Sstevel@tonic-gate /* We have forked successfully - this is the parent */
13467c478bd9Sstevel@tonic-gate ret = bus_quiesce(apidp, pt, errstring, flags);
13477c478bd9Sstevel@tonic-gate
13487c478bd9Sstevel@tonic-gate (void) close(fd); /* also unlocks */
13497c478bd9Sstevel@tonic-gate
13507c478bd9Sstevel@tonic-gate wait_for_child(cpid);
13517c478bd9Sstevel@tonic-gate
13527c478bd9Sstevel@tonic-gate (void) mutex_unlock(&quiesce_mutex);
13537c478bd9Sstevel@tonic-gate
13547c478bd9Sstevel@tonic-gate return (ret);
13557c478bd9Sstevel@tonic-gate bad:
13567c478bd9Sstevel@tonic-gate (void) close(fd);
13577c478bd9Sstevel@tonic-gate s_unlink(SCFGA_LOCK);
13587c478bd9Sstevel@tonic-gate (void) mutex_unlock(&quiesce_mutex);
13597c478bd9Sstevel@tonic-gate return (ret);
13607c478bd9Sstevel@tonic-gate }
13617c478bd9Sstevel@tonic-gate
13627c478bd9Sstevel@tonic-gate /*
13637c478bd9Sstevel@tonic-gate * Checks if HBA controls a critical file-system (/, /usr or swap)
13647c478bd9Sstevel@tonic-gate * This routine reads /etc/vfstab and is NOT foolproof.
13657c478bd9Sstevel@tonic-gate * If an error occurs, assumes that controller is NOT critical.
13667c478bd9Sstevel@tonic-gate */
13677c478bd9Sstevel@tonic-gate static int
critical_ctrlr(const char * hba_phys)13687c478bd9Sstevel@tonic-gate critical_ctrlr(const char *hba_phys)
13697c478bd9Sstevel@tonic-gate {
13707c478bd9Sstevel@tonic-gate FILE *fp;
13717c478bd9Sstevel@tonic-gate struct vfstab vfst;
13727c478bd9Sstevel@tonic-gate int vfsret = 1, rv = -1;
13737c478bd9Sstevel@tonic-gate char *bufp;
13747c478bd9Sstevel@tonic-gate const size_t buflen = PATH_MAX;
13757c478bd9Sstevel@tonic-gate char mount[MAXPATHLEN], fstype[MAXPATHLEN], spec[MAXPATHLEN];
13767c478bd9Sstevel@tonic-gate
13777c478bd9Sstevel@tonic-gate
13787c478bd9Sstevel@tonic-gate if ((bufp = calloc(1, buflen)) == NULL) {
13797c478bd9Sstevel@tonic-gate return (0);
13807c478bd9Sstevel@tonic-gate }
13817c478bd9Sstevel@tonic-gate
13827c478bd9Sstevel@tonic-gate fp = NULL;
13837c478bd9Sstevel@tonic-gate if ((fp = fopen(ETC_VFSTAB, "r")) == NULL) {
13847c478bd9Sstevel@tonic-gate rv = 0;
13857c478bd9Sstevel@tonic-gate goto out;
13867c478bd9Sstevel@tonic-gate }
13877c478bd9Sstevel@tonic-gate
13887c478bd9Sstevel@tonic-gate while ((vfsret = getvfsent(fp, &vfst)) == 0) {
13897c478bd9Sstevel@tonic-gate
13907c478bd9Sstevel@tonic-gate (void) strcpy(mount, S_STR(vfst.vfs_mountp));
13917c478bd9Sstevel@tonic-gate (void) strcpy(fstype, S_STR(vfst.vfs_fstype));
13927c478bd9Sstevel@tonic-gate (void) strcpy(spec, S_STR(vfst.vfs_special));
13937c478bd9Sstevel@tonic-gate
13947c478bd9Sstevel@tonic-gate /* Ignore non-critical entries */
13957c478bd9Sstevel@tonic-gate if (strcmp(mount, "/") && strcmp(mount, "/usr") &&
13967c478bd9Sstevel@tonic-gate strcmp(fstype, "swap")) {
13977c478bd9Sstevel@tonic-gate continue;
13987c478bd9Sstevel@tonic-gate }
13997c478bd9Sstevel@tonic-gate
14007c478bd9Sstevel@tonic-gate /* get physical path */
14017c478bd9Sstevel@tonic-gate if (realpath(spec, bufp) == NULL) {
14027c478bd9Sstevel@tonic-gate continue;
14037c478bd9Sstevel@tonic-gate }
14047c478bd9Sstevel@tonic-gate
14057c478bd9Sstevel@tonic-gate /* Check if critical partition is on the HBA */
14067c478bd9Sstevel@tonic-gate if (!(rv = hba_dev_cmp(hba_phys, bufp))) {
14077c478bd9Sstevel@tonic-gate break;
14087c478bd9Sstevel@tonic-gate }
14097c478bd9Sstevel@tonic-gate }
14107c478bd9Sstevel@tonic-gate
14117c478bd9Sstevel@tonic-gate rv = !vfsret;
14127c478bd9Sstevel@tonic-gate
14137c478bd9Sstevel@tonic-gate /*FALLTHRU*/
14147c478bd9Sstevel@tonic-gate out:
14157c478bd9Sstevel@tonic-gate S_FREE(bufp);
141609fe1b16Sdnielsen if (fp != NULL) {
141709fe1b16Sdnielsen (void) fclose(fp);
141809fe1b16Sdnielsen }
14197c478bd9Sstevel@tonic-gate return (rv);
14207c478bd9Sstevel@tonic-gate }
14217c478bd9Sstevel@tonic-gate
14227c478bd9Sstevel@tonic-gate /*
14237c478bd9Sstevel@tonic-gate * Convert bus state to receptacle state
14247c478bd9Sstevel@tonic-gate */
14257c478bd9Sstevel@tonic-gate static cfga_stat_t
bus_devctl_to_recep_state(uint_t bus_dc_state)14267c478bd9Sstevel@tonic-gate bus_devctl_to_recep_state(uint_t bus_dc_state)
14277c478bd9Sstevel@tonic-gate {
14287c478bd9Sstevel@tonic-gate cfga_stat_t rs;
14297c478bd9Sstevel@tonic-gate
14307c478bd9Sstevel@tonic-gate switch (bus_dc_state) {
14317c478bd9Sstevel@tonic-gate case BUS_ACTIVE:
14327c478bd9Sstevel@tonic-gate rs = CFGA_STAT_CONNECTED;
14337c478bd9Sstevel@tonic-gate break;
14347c478bd9Sstevel@tonic-gate case BUS_QUIESCED:
14357c478bd9Sstevel@tonic-gate case BUS_SHUTDOWN:
14367c478bd9Sstevel@tonic-gate rs = CFGA_STAT_DISCONNECTED;
14377c478bd9Sstevel@tonic-gate break;
14387c478bd9Sstevel@tonic-gate default:
14397c478bd9Sstevel@tonic-gate rs = CFGA_STAT_NONE;
14407c478bd9Sstevel@tonic-gate break;
14417c478bd9Sstevel@tonic-gate }
14427c478bd9Sstevel@tonic-gate
14437c478bd9Sstevel@tonic-gate return (rs);
14447c478bd9Sstevel@tonic-gate }
14457c478bd9Sstevel@tonic-gate
14467c478bd9Sstevel@tonic-gate static int
add_dev(di_node_t node,void * arg)14477c478bd9Sstevel@tonic-gate add_dev(di_node_t node, void *arg)
14487c478bd9Sstevel@tonic-gate {
14497c478bd9Sstevel@tonic-gate int ndevs, len;
14507c478bd9Sstevel@tonic-gate char *path, *p;
14517c478bd9Sstevel@tonic-gate struct larg *largp = (struct larg *)arg;
14527c478bd9Sstevel@tonic-gate
14537c478bd9Sstevel@tonic-gate /* ignore hba itself and all detached nodes */
14547c478bd9Sstevel@tonic-gate if (di_parent_node(node) == DI_NODE_NIL ||
14557c478bd9Sstevel@tonic-gate di_node_state(node) < DS_ATTACHED)
14567c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE);
14577c478bd9Sstevel@tonic-gate
14587c478bd9Sstevel@tonic-gate if ((path = di_devfs_path(node)) == NULL) {
14597c478bd9Sstevel@tonic-gate largp->ndevs = -1;
14607c478bd9Sstevel@tonic-gate return (DI_WALK_TERMINATE);
14617c478bd9Sstevel@tonic-gate }
14627c478bd9Sstevel@tonic-gate
14637c478bd9Sstevel@tonic-gate /* sizeof (DEVICES_DIR) includes the null terminator */
14647c478bd9Sstevel@tonic-gate len = strlen(path) + sizeof (DEVICES_DIR);
14657c478bd9Sstevel@tonic-gate if ((p = malloc(len)) == NULL) {
14667c478bd9Sstevel@tonic-gate di_devfs_path_free(path);
14677c478bd9Sstevel@tonic-gate largp->ndevs = -1;
14687c478bd9Sstevel@tonic-gate return (DI_WALK_TERMINATE);
14697c478bd9Sstevel@tonic-gate }
14707c478bd9Sstevel@tonic-gate (void) snprintf(p, len, "%s%s", DEVICES_DIR, path);
14717c478bd9Sstevel@tonic-gate di_devfs_path_free(path);
14727c478bd9Sstevel@tonic-gate
14737c478bd9Sstevel@tonic-gate /* ignore device to be excluded */
14747c478bd9Sstevel@tonic-gate if (largp->dev && strcmp(largp->dev, p) == 0) {
14757c478bd9Sstevel@tonic-gate free(p);
14767c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE);
14777c478bd9Sstevel@tonic-gate }
14787c478bd9Sstevel@tonic-gate
14797c478bd9Sstevel@tonic-gate /* grow dev_list to allow room for one more device */
14807c478bd9Sstevel@tonic-gate if (alloc_dev_list(largp) != 0) {
14817c478bd9Sstevel@tonic-gate free(p);
14827c478bd9Sstevel@tonic-gate return (DI_WALK_TERMINATE);
14837c478bd9Sstevel@tonic-gate }
14847c478bd9Sstevel@tonic-gate ndevs = largp->ndevs;
14857c478bd9Sstevel@tonic-gate largp->ndevs++;
14867c478bd9Sstevel@tonic-gate largp->dev_list[ndevs] = p;
14877c478bd9Sstevel@tonic-gate largp->dev_list[ndevs + 1] = NULL;
14887c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE);
14897c478bd9Sstevel@tonic-gate }
14907c478bd9Sstevel@tonic-gate
14917c478bd9Sstevel@tonic-gate /*
14927c478bd9Sstevel@tonic-gate * Get list of children excluding dev_excl (if not null).
14937c478bd9Sstevel@tonic-gate */
14947c478bd9Sstevel@tonic-gate static int
get_hba_children(char * bus_path,char * dev_excl,char *** dev_listp)14957c478bd9Sstevel@tonic-gate get_hba_children(char *bus_path, char *dev_excl, char ***dev_listp)
14967c478bd9Sstevel@tonic-gate {
14977c478bd9Sstevel@tonic-gate int err, ret;
14987c478bd9Sstevel@tonic-gate walkarg_t u;
14997c478bd9Sstevel@tonic-gate struct larg larg;
15007c478bd9Sstevel@tonic-gate
15017c478bd9Sstevel@tonic-gate *dev_listp = NULL;
15027c478bd9Sstevel@tonic-gate
15037c478bd9Sstevel@tonic-gate u.node_args.flags = DI_WALK_CLDFIRST;
15047c478bd9Sstevel@tonic-gate u.node_args.fcn = add_dev;
15057c478bd9Sstevel@tonic-gate
15067c478bd9Sstevel@tonic-gate larg.ndevs = 0;
15077c478bd9Sstevel@tonic-gate larg.nelem = 0;
15087c478bd9Sstevel@tonic-gate larg.dev = dev_excl;
15097c478bd9Sstevel@tonic-gate larg.dev_list = NULL;
15107c478bd9Sstevel@tonic-gate
15117c478bd9Sstevel@tonic-gate ret = walk_tree(bus_path, &larg, DINFOSUBTREE, &u, SCFGA_WALK_NODE,
15127c478bd9Sstevel@tonic-gate &err);
15137c478bd9Sstevel@tonic-gate if (larg.ndevs == -1) {
15147c478bd9Sstevel@tonic-gate free_dev_list(larg.dev_list);
15157c478bd9Sstevel@tonic-gate return (SCFGA_ERR);
15167c478bd9Sstevel@tonic-gate }
15177c478bd9Sstevel@tonic-gate *dev_listp = larg.dev_list;
15187c478bd9Sstevel@tonic-gate return (ret);
15197c478bd9Sstevel@tonic-gate }
15207c478bd9Sstevel@tonic-gate
15217c478bd9Sstevel@tonic-gate static char *
get_node_path(char * minor_path)15227c478bd9Sstevel@tonic-gate get_node_path(char *minor_path)
15237c478bd9Sstevel@tonic-gate {
15247c478bd9Sstevel@tonic-gate char *path, *cp;
15257c478bd9Sstevel@tonic-gate
15267c478bd9Sstevel@tonic-gate if ((path = strdup(minor_path)) == NULL)
15277c478bd9Sstevel@tonic-gate return (NULL);
15287c478bd9Sstevel@tonic-gate if ((cp = strrchr(path, ':')) != NULL)
15297c478bd9Sstevel@tonic-gate *cp = '\0';
15307c478bd9Sstevel@tonic-gate return (path);
15317c478bd9Sstevel@tonic-gate }
15327c478bd9Sstevel@tonic-gate
15337c478bd9Sstevel@tonic-gate /*
15347c478bd9Sstevel@tonic-gate * Ensure largp->dev_list has room for one more device.
15357c478bd9Sstevel@tonic-gate * Returns 0 on success, -1 on failure.
15367c478bd9Sstevel@tonic-gate */
15377c478bd9Sstevel@tonic-gate static int
alloc_dev_list(struct larg * largp)15387c478bd9Sstevel@tonic-gate alloc_dev_list(struct larg *largp)
15397c478bd9Sstevel@tonic-gate {
15407c478bd9Sstevel@tonic-gate int nelem;
15417c478bd9Sstevel@tonic-gate char **p;
15427c478bd9Sstevel@tonic-gate
15437c478bd9Sstevel@tonic-gate if (largp->nelem > largp->ndevs + 2) /* +1 for NULL termination */
15447c478bd9Sstevel@tonic-gate return (0);
15457c478bd9Sstevel@tonic-gate
15467c478bd9Sstevel@tonic-gate nelem = largp->nelem + 16;
15477c478bd9Sstevel@tonic-gate p = realloc(largp->dev_list, nelem * sizeof (char *));
15487c478bd9Sstevel@tonic-gate if (p == NULL)
15497c478bd9Sstevel@tonic-gate return (-1);
15507c478bd9Sstevel@tonic-gate
15517c478bd9Sstevel@tonic-gate largp->dev_list = p;
15527c478bd9Sstevel@tonic-gate largp->nelem = nelem;
15537c478bd9Sstevel@tonic-gate return (0);
15547c478bd9Sstevel@tonic-gate }
15557c478bd9Sstevel@tonic-gate
15567c478bd9Sstevel@tonic-gate static void
free_dev_list_elements(char ** dev_list)15577c478bd9Sstevel@tonic-gate free_dev_list_elements(char **dev_list)
15587c478bd9Sstevel@tonic-gate {
15597c478bd9Sstevel@tonic-gate while (*dev_list) {
15607c478bd9Sstevel@tonic-gate free(*dev_list);
15617c478bd9Sstevel@tonic-gate dev_list++;
15627c478bd9Sstevel@tonic-gate }
15637c478bd9Sstevel@tonic-gate }
15647c478bd9Sstevel@tonic-gate
15657c478bd9Sstevel@tonic-gate static void
free_dev_list(char ** dev_list)15667c478bd9Sstevel@tonic-gate free_dev_list(char **dev_list)
15677c478bd9Sstevel@tonic-gate {
15687c478bd9Sstevel@tonic-gate if (dev_list == NULL)
15697c478bd9Sstevel@tonic-gate return;
15707c478bd9Sstevel@tonic-gate
15717c478bd9Sstevel@tonic-gate free_dev_list_elements(dev_list);
15727c478bd9Sstevel@tonic-gate free(dev_list);
15737c478bd9Sstevel@tonic-gate }
1574