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
5*691b55abSvikram * Common Development and Distribution License (the "License").
6*691b55abSvikram * 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 */
217c478bd9Sstevel@tonic-gate /*
22*691b55abSvikram * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <fcntl.h>
297c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate #include "libdiskmgt.h"
357c478bd9Sstevel@tonic-gate #include "disks_private.h"
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate static int add_path_state(descriptor_t *dp, nvlist_t *attrs);
387c478bd9Sstevel@tonic-gate static int add_wwn(descriptor_t *dp, nvlist_t *attrs);
397c478bd9Sstevel@tonic-gate static descriptor_t **get_assoc_drives(descriptor_t *desc, int *errp);
407c478bd9Sstevel@tonic-gate static descriptor_t **get_assoc_controllers(descriptor_t *desc, int *errp);
417c478bd9Sstevel@tonic-gate static char *path_state_name(di_path_state_t st);
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate descriptor_t **
path_get_assoc_descriptors(descriptor_t * desc,dm_desc_type_t type,int * errp)447c478bd9Sstevel@tonic-gate path_get_assoc_descriptors(descriptor_t *desc, dm_desc_type_t type, int *errp)
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate switch (type) {
477c478bd9Sstevel@tonic-gate case DM_DRIVE:
487c478bd9Sstevel@tonic-gate return (get_assoc_drives(desc, errp));
497c478bd9Sstevel@tonic-gate case DM_CONTROLLER:
507c478bd9Sstevel@tonic-gate return (get_assoc_controllers(desc, errp));
517c478bd9Sstevel@tonic-gate }
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate *errp = EINVAL;
547c478bd9Sstevel@tonic-gate return (NULL);
557c478bd9Sstevel@tonic-gate }
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate nvlist_t *
path_get_attributes(descriptor_t * dp,int * errp)587c478bd9Sstevel@tonic-gate path_get_attributes(descriptor_t *dp, int *errp)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate path_t *pp;
617c478bd9Sstevel@tonic-gate nvlist_t *attrs = NULL;
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate pp = dp->p.path;
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate if (nvlist_alloc(&attrs, NVATTRS, 0) != 0) {
667c478bd9Sstevel@tonic-gate *errp = ENOMEM;
677c478bd9Sstevel@tonic-gate return (NULL);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate if (nvlist_add_string(attrs, DM_CTYPE, pp->ctype) != 0) {
717c478bd9Sstevel@tonic-gate nvlist_free(attrs);
727c478bd9Sstevel@tonic-gate *errp = ENOMEM;
737c478bd9Sstevel@tonic-gate return (NULL);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate * We add the path state and wwn attributes only for descriptors that
787c478bd9Sstevel@tonic-gate * we got via their association to a specific drive (since these
797c478bd9Sstevel@tonic-gate * attributes are drive specific).
807c478bd9Sstevel@tonic-gate */
817c478bd9Sstevel@tonic-gate if (dp->name != NULL) {
827c478bd9Sstevel@tonic-gate if (add_path_state(dp, attrs) != 0) {
837c478bd9Sstevel@tonic-gate nvlist_free(attrs);
847c478bd9Sstevel@tonic-gate *errp = ENOMEM;
857c478bd9Sstevel@tonic-gate return (NULL);
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate if (add_wwn(dp, attrs) != 0) {
887c478bd9Sstevel@tonic-gate nvlist_free(attrs);
897c478bd9Sstevel@tonic-gate *errp = ENOMEM;
907c478bd9Sstevel@tonic-gate return (NULL);
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate *errp = 0;
957c478bd9Sstevel@tonic-gate return (attrs);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate descriptor_t *
path_get_descriptor_by_name(char * name,int * errp)997c478bd9Sstevel@tonic-gate path_get_descriptor_by_name(char *name, int *errp)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate descriptor_t **paths;
1027c478bd9Sstevel@tonic-gate int i;
1037c478bd9Sstevel@tonic-gate descriptor_t *path = NULL;
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate paths = cache_get_descriptors(DM_PATH, errp);
1067c478bd9Sstevel@tonic-gate if (*errp != 0) {
1077c478bd9Sstevel@tonic-gate return (NULL);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate for (i = 0; paths[i]; i++) {
1117c478bd9Sstevel@tonic-gate if (libdiskmgt_str_eq(name, paths[i]->p.path->name)) {
1127c478bd9Sstevel@tonic-gate path = paths[i];
1137c478bd9Sstevel@tonic-gate } else {
1147c478bd9Sstevel@tonic-gate /* clean up the unused descriptors */
1157c478bd9Sstevel@tonic-gate cache_free_descriptor(paths[i]);
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate free(paths);
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate if (path == NULL) {
1217c478bd9Sstevel@tonic-gate *errp = ENODEV;
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate
124*691b55abSvikram return (path);
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate /* ARGSUSED */
1287c478bd9Sstevel@tonic-gate descriptor_t **
path_get_descriptors(int filter[],int * errp)1297c478bd9Sstevel@tonic-gate path_get_descriptors(int filter[], int *errp)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate return (cache_get_descriptors(DM_PATH, errp));
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate char *
path_get_name(descriptor_t * desc)1357c478bd9Sstevel@tonic-gate path_get_name(descriptor_t *desc)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate return (desc->p.path->name);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate /* ARGSUSED */
1417c478bd9Sstevel@tonic-gate nvlist_t *
path_get_stats(descriptor_t * dp,int stat_type,int * errp)1427c478bd9Sstevel@tonic-gate path_get_stats(descriptor_t *dp, int stat_type, int *errp)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate /* There are no stat types defined for paths */
1457c478bd9Sstevel@tonic-gate *errp = EINVAL;
1467c478bd9Sstevel@tonic-gate return (NULL);
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate int
path_make_descriptors()1507c478bd9Sstevel@tonic-gate path_make_descriptors()
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate int error;
1537c478bd9Sstevel@tonic-gate controller_t *cp;
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate cp = cache_get_controllerlist();
1567c478bd9Sstevel@tonic-gate while (cp != NULL) {
1577c478bd9Sstevel@tonic-gate if (cp->paths != NULL) {
1587c478bd9Sstevel@tonic-gate int i;
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate for (i = 0; cp->paths[i]; i++) {
1617c478bd9Sstevel@tonic-gate cache_load_desc(DM_PATH, cp->paths[i], NULL, NULL, &error);
1627c478bd9Sstevel@tonic-gate if (error != 0) {
1637c478bd9Sstevel@tonic-gate return (error);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate cp = cp->next;
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate return (0);
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate /*
1747c478bd9Sstevel@tonic-gate * This is called when we have a name in the descriptor name field. That
1757c478bd9Sstevel@tonic-gate * only will be the case when the descriptor was created by getting the
1767c478bd9Sstevel@tonic-gate * association from a drive to the path. Since we filled in the name with
1777c478bd9Sstevel@tonic-gate * the drive device id in that case, we can use the device id to look up the
1787c478bd9Sstevel@tonic-gate * drive-path state.
1797c478bd9Sstevel@tonic-gate */
1807c478bd9Sstevel@tonic-gate static int
add_path_state(descriptor_t * dp,nvlist_t * attrs)1817c478bd9Sstevel@tonic-gate add_path_state(descriptor_t *dp, nvlist_t *attrs)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate ddi_devid_t devid;
1847c478bd9Sstevel@tonic-gate path_t *pp;
1857c478bd9Sstevel@tonic-gate int i;
1867c478bd9Sstevel@tonic-gate int status = 0;
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate if (devid_str_decode(dp->name, &devid, NULL) != 0) {
1897c478bd9Sstevel@tonic-gate return (0);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate /* find the index of the correct drive assoc. */
1937c478bd9Sstevel@tonic-gate pp = dp->p.path;
1947c478bd9Sstevel@tonic-gate for (i = 0; pp->disks[i] && pp->states[i] != -1; i++) {
1957c478bd9Sstevel@tonic-gate if (pp->disks[i]->devid != NULL &&
1967c478bd9Sstevel@tonic-gate devid_compare(pp->disks[i]->devid, devid) == 0) {
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate /* add the corresponding state */
1997c478bd9Sstevel@tonic-gate if (nvlist_add_string(attrs, DM_PATH_STATE,
2007c478bd9Sstevel@tonic-gate path_state_name(pp->states[i])) != 0) {
2017c478bd9Sstevel@tonic-gate status = ENOMEM;
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate break;
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate devid_free(devid);
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gate return (status);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate /*
2127c478bd9Sstevel@tonic-gate * This is called when we have a name in the descriptor name field. That
2137c478bd9Sstevel@tonic-gate * only will be the case when the descriptor was created by getting the
2147c478bd9Sstevel@tonic-gate * association from a drive to the path. Since we filled in the name with
2157c478bd9Sstevel@tonic-gate * the drive device id in that case, we can use the device id to look up the
2167c478bd9Sstevel@tonic-gate * drive wwn.
2177c478bd9Sstevel@tonic-gate */
2187c478bd9Sstevel@tonic-gate static int
add_wwn(descriptor_t * dp,nvlist_t * attrs)2197c478bd9Sstevel@tonic-gate add_wwn(descriptor_t *dp, nvlist_t *attrs)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate ddi_devid_t devid;
2227c478bd9Sstevel@tonic-gate path_t *pp;
2237c478bd9Sstevel@tonic-gate int i;
2247c478bd9Sstevel@tonic-gate int status = 0;
2257c478bd9Sstevel@tonic-gate
2267c478bd9Sstevel@tonic-gate if (devid_str_decode(dp->name, &devid, NULL) != 0) {
2277c478bd9Sstevel@tonic-gate return (0);
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate /* find the index of the correct drive assoc. */
2317c478bd9Sstevel@tonic-gate pp = dp->p.path;
2327c478bd9Sstevel@tonic-gate for (i = 0; pp->disks[i] && pp->states[i] != -1; i++) {
2337c478bd9Sstevel@tonic-gate if (pp->disks[i]->devid != NULL &&
2347c478bd9Sstevel@tonic-gate devid_compare(pp->disks[i]->devid, devid) == 0) {
2357c478bd9Sstevel@tonic-gate
2367c478bd9Sstevel@tonic-gate /* add the corresponding state */
2377c478bd9Sstevel@tonic-gate if (nvlist_add_string(attrs, DM_WWN, pp->wwns[i]) != 0) {
2387c478bd9Sstevel@tonic-gate status = ENOMEM;
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate break;
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate devid_free(devid);
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate return (status);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate static descriptor_t **
get_assoc_controllers(descriptor_t * desc,int * errp)2497c478bd9Sstevel@tonic-gate get_assoc_controllers(descriptor_t *desc, int *errp)
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate path_t *pp;
2527c478bd9Sstevel@tonic-gate descriptor_t **controllers;
2537c478bd9Sstevel@tonic-gate int i;
2547c478bd9Sstevel@tonic-gate
2557c478bd9Sstevel@tonic-gate pp = desc->p.path;
2567c478bd9Sstevel@tonic-gate
2577c478bd9Sstevel@tonic-gate /* a path can have just one controller */
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gate controllers = (descriptor_t **)calloc(2, sizeof (descriptor_t *));
2607c478bd9Sstevel@tonic-gate if (controllers == NULL) {
2617c478bd9Sstevel@tonic-gate *errp = ENOMEM;
2627c478bd9Sstevel@tonic-gate return (NULL);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate
2657c478bd9Sstevel@tonic-gate i = 0;
2667c478bd9Sstevel@tonic-gate if (pp->controller != NULL) {
2677c478bd9Sstevel@tonic-gate controllers[i++] = cache_get_desc(DM_CONTROLLER,
2687c478bd9Sstevel@tonic-gate pp->controller, NULL, NULL, errp);
2697c478bd9Sstevel@tonic-gate if (*errp != 0) {
2707c478bd9Sstevel@tonic-gate cache_free_descriptors(controllers);
2717c478bd9Sstevel@tonic-gate return (NULL);
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate
2757c478bd9Sstevel@tonic-gate controllers[i] = NULL;
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate *errp = 0;
2787c478bd9Sstevel@tonic-gate return (controllers);
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate static descriptor_t **
get_assoc_drives(descriptor_t * desc,int * errp)2827c478bd9Sstevel@tonic-gate get_assoc_drives(descriptor_t *desc, int *errp)
2837c478bd9Sstevel@tonic-gate {
2847c478bd9Sstevel@tonic-gate path_t *pp;
2857c478bd9Sstevel@tonic-gate descriptor_t **drives;
2867c478bd9Sstevel@tonic-gate int cnt;
2877c478bd9Sstevel@tonic-gate int i;
2887c478bd9Sstevel@tonic-gate
2897c478bd9Sstevel@tonic-gate pp = desc->p.path;
2907c478bd9Sstevel@tonic-gate
2917c478bd9Sstevel@tonic-gate /* Count how many we have. */
2927c478bd9Sstevel@tonic-gate for (cnt = 0; pp->disks[cnt]; cnt++);
2937c478bd9Sstevel@tonic-gate
2947c478bd9Sstevel@tonic-gate /* make the snapshot */
2957c478bd9Sstevel@tonic-gate drives = (descriptor_t **)calloc(cnt + 1, sizeof (descriptor_t *));
2967c478bd9Sstevel@tonic-gate if (drives == NULL) {
2977c478bd9Sstevel@tonic-gate *errp = ENOMEM;
2987c478bd9Sstevel@tonic-gate return (NULL);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate
3017c478bd9Sstevel@tonic-gate for (i = 0; pp->disks[i]; i++) {
3027c478bd9Sstevel@tonic-gate drives[i] = cache_get_desc(DM_DRIVE, pp->disks[i], NULL, NULL,
3037c478bd9Sstevel@tonic-gate errp);
3047c478bd9Sstevel@tonic-gate if (*errp != 0) {
3057c478bd9Sstevel@tonic-gate cache_free_descriptors(drives);
3067c478bd9Sstevel@tonic-gate return (NULL);
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate drives[i] = NULL;
3107c478bd9Sstevel@tonic-gate
3117c478bd9Sstevel@tonic-gate *errp = 0;
3127c478bd9Sstevel@tonic-gate return (drives);
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate
3157c478bd9Sstevel@tonic-gate static char *
path_state_name(di_path_state_t st)3167c478bd9Sstevel@tonic-gate path_state_name(di_path_state_t st)
3177c478bd9Sstevel@tonic-gate {
3187c478bd9Sstevel@tonic-gate switch (st) {
3197c478bd9Sstevel@tonic-gate case DI_PATH_STATE_ONLINE:
3207c478bd9Sstevel@tonic-gate return ("online");
3217c478bd9Sstevel@tonic-gate case DI_PATH_STATE_STANDBY:
3227c478bd9Sstevel@tonic-gate return ("standby");
3237c478bd9Sstevel@tonic-gate case DI_PATH_STATE_OFFLINE:
3247c478bd9Sstevel@tonic-gate return ("offline");
3257c478bd9Sstevel@tonic-gate case DI_PATH_STATE_FAULT:
3267c478bd9Sstevel@tonic-gate return ("faulted");
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate return ("unknown");
3297c478bd9Sstevel@tonic-gate }
330