summaryrefslogtreecommitdiff
path: root/usr/src/lib/storage/libg_fc/common/mpath.c
blob: f76fa80a85080734087fe2aae071974bd12ab793 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */


/*LINTLIBRARY*/

/*
 * I18N message number ranges
 *  This file: (not defined yet)
 *  Shared common messages: 1 - 1999
 */

/*
 *	This module is part of the Fibre Channel Interface library.
 */

/* #define		_POSIX_SOURCE 1 */


/*	Includes	*/
#include	<stdlib.h>
#include	<stdio.h>
#include	<sys/file.h>
#include	<sys/types.h>
#include	<sys/stat.h>
#include	<sys/mkdev.h>
#include	<sys/param.h>
#include	<fcntl.h>
#include	<unistd.h>
#include	<string.h>
#include	<sys/scsi/scsi.h>
#include	<dirent.h>		/* for DIR */
#include	<sys/vtoc.h>
#include	<nl_types.h>
#include	<strings.h>
#include	<sys/ddi.h>		/* for max */
#include	<fnmatch.h>
#include	<l_common.h>
#include	<stgcom.h>
#include	<l_error.h>
#include	<g_state.h>
#include	<sys/fibre-channel/ulp/fcp_util.h>
#include	<sys/fibre-channel/impl/fc_error.h>
#include	<sys/fibre-channel/impl/fcph.h>
#include	<sys/socalio.h>
#include	<libdevinfo.h>
#include	<libnvpair.h>
#include	<sys/scsi/adapters/scsi_vhci.h>
#include	<errno.h>

/* Some forward declarations of static functions */
static void g_free_pi_list(sv_path_info_t *, uint_t num_paths);
static int get_pathlist(char *, sv_iocdata_t *, int *);
static int stms_path_enable_disable(char *, char *, int);
static int stms_path_enable_disable_all(char *, int);

/*
 * To get lun number of a given device pathname using driver ioctl.
 * This interface is called directly by g_get_lun_number
 *
 * inputs;
 * outputs:
 * returns:
 *    0 - success
 *   !0 - failure
 */
int
g_get_lun_str(char *dev_path, char lunstr[], int path_num)
{
	char		*char_ptr, *charptr1;
	int		fd = 0;
	sv_iocdata_t	ioc;
	char		phci_path[MAXPATHLEN];
	char		client_path[MAXPATHLEN];
	char		paddr[MAXNAMELEN];
	uint_t		num_elem = 0, i;
	sv_path_info_t	*pi = NULL;
	int		retval = 0;
	uint_t		num_paths;

	if (strstr(dev_path, "/devices") == NULL) {
		return (-1);
	}

	num_paths = path_num + 1;
	(void) strcpy(client_path, dev_path + DEV_PREFIX_LEN-1);
	if ((char_ptr = strrchr(client_path, ':')) != NULL) {
		*char_ptr = '\0';
	}

	ioc.client	= client_path;
	ioc.phci	= phci_path;
	ioc.addr	= paddr;
	ioc.buf_elem	= 0;
	ioc.ret_buf	= NULL;
	ioc.ret_elem	= &num_elem;

	if ((fd = g_object_open(VHCI_NODE, O_RDWR)) < 0) {
		return (L_OPEN_PATH_FAIL);
	}

	/* Allocate memory for path info structs */
	pi = (sv_path_info_t *)calloc((size_t)num_paths,
		sizeof (sv_path_info_t));
	ioc.buf_elem = num_paths;
	ioc.ret_buf  = pi;

	/* Allocate memory for getting per path info properties */

	for (i = 0; i < num_paths; i++) {
		pi[i].ret_prop.buf_size = SV_PROP_MAX_BUF_SIZE;
		if (((pi[i].ret_prop.buf =
			malloc(SV_PROP_MAX_BUF_SIZE)) == NULL) ||
			((pi[i].ret_prop.ret_buf_size =
				malloc(sizeof (*pi[i].ret_prop.ret_buf_size)))
				    == NULL)) {
			/* Free memory for per path info properties */
			g_free_pi_list(pi, num_paths);
			(void) close(fd);
			return (-1);
		}
	}

	retval = ioctl(fd, SCSI_VHCI_GET_CLIENT_MULTIPATH_INFO, &ioc);
	if (retval != 0) {
		/* Free memory for per path info properties */
		g_free_pi_list(pi, num_paths);
		(void) close(fd);
		return (retval);
	}

	if (path_num < ioc.buf_elem) {
		charptr1 = strchr(pi[path_num].ret_addr, ',');
		retval = 0;
	} else {
		charptr1 = strchr(pi[0].ret_addr, ',');
		retval = -1;
	}

	if (charptr1 != NULL) {
		charptr1++;
		if (charptr1 != NULL) {
			(void) strcpy(lunstr, charptr1);
		}
	}

	/* Free memory for per path info properties */
	g_free_pi_list(pi, num_paths);
	(void) close(fd);
	return (retval);
}

/*
 * To give the lun number of a given device pathname
 *
 * inputs: physical pathname beginning with /devices
 * outputs: none
 * returns: lun number (if available) or -1 (if not available or
 *          failure)
 */
int
g_get_lun_number(char *path_phys)
{
	char		path0[MAXPATHLEN], lunarr[MAXPATHLEN];
	char		*charptr1, *charptr2, *charptr3;
	int		lunval = 0;

	if ((strstr(path_phys, "/devices")) == NULL) {
		return (-1);
	}

	if (((charptr3 = strstr(path_phys, SLSH_DRV_NAME_SSD)) == NULL) &&
		((charptr3 = strstr(path_phys, SLSH_DRV_NAME_ST)) == NULL)) {
		return (-1);
	}

	(void) strcpy(path0, charptr3);

	if ((charptr2 = strrchr(path0, ':')) != NULL) {
		*charptr2 = '\0';
	}

	if ((charptr1 = strchr(path0, ',')) != NULL) {
		charptr1++;
		if (*charptr1 != '0') {
			(void) strcpy(lunarr, charptr1);
		} else {
			return (0);
		}
	} else if (strstr(path_phys, SCSI_VHCI) != NULL) {
		/* for the time being */
		if (g_get_lun_str(path_phys, lunarr, 0) != 0) {
			return (-1);
		}
	} else {
		return (-1);
	}

	lunval = (int)strtol(lunarr, NULL, 16);

	return (lunval);
}

/*
 * Input - Space for client_path, phci_path and paddr fields of ioc structure
 * need to be allocated by the caller of this routine.
 */
static int
get_pathlist(char *dev_path, sv_iocdata_t *ioc, int *num_paths_to_copy)
{
	char	*physical_path, *physical_path_s;
	int	retval;
	int	fd;
	int	initial_path_count;
	int	current_path_count;
	int	i;
	char	*delimiter;
	int	malloc_error = 0;
	int	prop_buf_size;
	int	pathlist_retry_count = 0;

	if (strncmp(dev_path, SCSI_VHCI, strlen(SCSI_VHCI)) != 0) {
		if ((physical_path = g_get_physical_name(dev_path)) == NULL) {
			return (L_INVALID_PATH);
		}
		if (strncmp(physical_path, SCSI_VHCI, strlen(SCSI_VHCI)) != 0) {
			free(physical_path);
			return (L_INVALID_PATH);
		}
	} else {
		if ((physical_path = calloc(1, MAXPATHLEN)) == NULL) {
			return (L_MALLOC_FAILED);
		}
		(void) strcpy(physical_path, dev_path);
	}
	physical_path_s = physical_path;

	/* move beyond "/devices" prefix */
	physical_path += DEV_PREFIX_LEN-1;
	/* remove  :c,raw suffix */
	delimiter = strrchr(physical_path, ':');
	/* if we didn't find the ':' fine, else truncate */
	if (delimiter != NULL) {
		*delimiter = '\0';
	}

	/*
	 * We'll call ioctl SCSI_VHCI_GET_CLIENT_MULTIPATH_INFO
	 * at least twice.  The first time will get the path count
	 * and the size of the ioctl propoerty buffer.  The second
	 * time will get the path_info for each path.
	 *
	 * It's possible that additional paths are added while this
	 * code is running.  If the path count increases between the
	 * 2 ioctl's above, then we'll retry (and assume all is well).
	 */
	(void) strcpy(ioc->client, physical_path);
	ioc->buf_elem = 1;
	ioc->ret_elem = (uint_t *)&(initial_path_count);
	ioc->ret_buf = NULL;

	/* free physical path */
	free(physical_path_s);

	/* 0 buf_size asks driver to return actual size needed */
	/* open the ioctl file descriptor */
	if ((fd = g_object_open(VHCI_NODE, O_RDWR)) < 0) {
		return (L_OPEN_PATH_FAIL);
	}

	retval = ioctl(fd, SCSI_VHCI_GET_CLIENT_MULTIPATH_INFO, ioc);
	if (retval != 0) {
		close(fd);
		return (L_SCSI_VHCI_ERROR);
	}
	prop_buf_size = SV_PROP_MAX_BUF_SIZE;


	while (pathlist_retry_count <= RETRY_PATHLIST) {
		ioc->buf_elem = initial_path_count;
		/* Make driver put actual # paths in variable */
		ioc->ret_elem = (uint_t *)&(current_path_count);

		/*
		 * Allocate space for array of path_info structures.
		 * Allocate enough space for # paths from get_pathcount
		 */
		ioc->ret_buf = (sv_path_info_t *)
				calloc(initial_path_count,
					sizeof (sv_path_info_t));
		if (ioc->ret_buf == NULL) {
			close(fd);
			return (L_MALLOC_FAILED);
		}

		/*
		 * Allocate space for path properties returned by driver
		 */
		malloc_error = 0;
		for (i = 0; i < initial_path_count; i++) {
			ioc->ret_buf[i].ret_prop.buf_size = prop_buf_size;
			if ((ioc->ret_buf[i].ret_prop.buf =
			    (caddr_t)malloc(prop_buf_size)) == NULL) {
				malloc_error = 1;
				break;
			}
			if ((ioc->ret_buf[i].ret_prop.ret_buf_size =
				(uint_t *)malloc(sizeof (uint_t))) == NULL) {
				malloc_error = 1;
				break;
			}
		}
		if (malloc_error == 1) {
			for (i = 0; i < initial_path_count; i++) {
				free(ioc->ret_buf[i].ret_prop.buf);
				free(ioc->ret_buf[i].ret_prop.ret_buf_size);
			}
			free(ioc->ret_buf);
			close(fd);
			return (L_MALLOC_FAILED);
		}

		retval = ioctl(fd, SCSI_VHCI_GET_CLIENT_MULTIPATH_INFO, ioc);
		if (retval != 0) {
			for (i = 0; i < initial_path_count; i++) {
				free(ioc->ret_buf[i].ret_prop.buf);
				free(ioc->ret_buf[i].ret_prop.ret_buf_size);
			}
			free(ioc->ret_buf);
			close(fd);
			return (L_SCSI_VHCI_ERROR);
		}
		if (initial_path_count < current_path_count) {
			/* then a new path was added */
			pathlist_retry_count++;
			initial_path_count = current_path_count;
		} else {
			break;
		}
	}
	/* we are done with ioctl's, lose the fd */
	close(fd);

	/*
	 * Compare the length num elements from the ioctl response
	 *   and the caller's request - use smaller value.
	 *
	 * pathlist_p->path_count now has count returned from ioctl.
	 * ioc.buf_elem has the value the caller provided.
	 */
	if (initial_path_count < current_path_count) {
		/* More paths exist than we allocated space for */
		*num_paths_to_copy = initial_path_count;
	} else {
		*num_paths_to_copy = current_path_count;
	}
return (0);
}

/*
 * To obtain pathlist of a given target device
 *
 * inputs:
 *	dev_path client device path
 *	example: /devices/scsi_vhci/ssd@g280000602200416d6257333030303261:c,raw
 * outputs:
 *	pathlist_p pathlist structure containing pathinfo node data
 * returns:
 *   0 - success
 *  !0 - failure
 */
int
g_get_pathlist(char *dev_path, struct mp_pathlist *pathlist_p)
{

	sv_iocdata_t	ioc;
	int	retval, caller_ret = 0;
	int	num_paths_to_copy;
	int	i;
	int	prop_buf_size;
	char	*path_class_val = NULL;
	char	*temp_addr;
	char	phci_path[MAXPATHLEN];
	char	client_path[MAXPATHLEN];
	char	paddr[MAXNAMELEN];


	ioc.client = client_path;
	ioc.phci = phci_path;
	ioc.addr = paddr;

	if ((caller_ret = get_pathlist(dev_path, &ioc, &num_paths_to_copy))
		!= 0) {
		return (caller_ret);
	}

	pathlist_p->path_count = num_paths_to_copy;
	pathlist_p->path_info = calloc(num_paths_to_copy,
					sizeof (mp_pathinfo_t));

	prop_buf_size = SV_PROP_MAX_BUF_SIZE;

	if (pathlist_p->path_info == NULL) {
		caller_ret = L_MALLOC_FAILED;
		/* force the loop to not run so we free buffers and exit */
		num_paths_to_copy = 0;
	}

	/* get ioctl reponse fields and copy them to caller's buffer */
	for (i = 0; i < num_paths_to_copy; i++) {
		nvlist_t *nvl;

		pathlist_p->path_info[i].path_state =
			ioc.ret_buf[i].ret_state;
		(void) strncpy(pathlist_p->path_info[i].path_hba, DEV_PREFIX,
			DEV_PREFIX_LEN - 1);
		(void) strcat(pathlist_p->path_info[i].path_hba,
			ioc.ret_buf[i].device.ret_phci);
		(void) strcpy(pathlist_p->path_info[i].path_dev,
			ioc.client);

		/*
		 * Check for leading 'w'. The mpxio framework was
		 * incorrectly implemented to skip 'w' in mdi_pi_get_addr().
		 * Since the leading 'w' is fibre-channel specific, we
		 * do it here to remove fibre-channel specific behavior
		 * from the mpxio framework.
		 */
		temp_addr = ioc.ret_buf[i].ret_addr;
		if (*temp_addr == 'w') {
			temp_addr++;
		}
		(void) strcpy(pathlist_p->path_info[i].path_addr, temp_addr);

		/* use nvlist_ calls to extract properties from retbuf */
		retval = nvlist_unpack(ioc.ret_buf[i].ret_prop.buf,
					prop_buf_size, &nvl, 0);
		if (retval != 0) { /* ??? same retcode */
			(void) strcpy(pathlist_p->path_info[i].path_class,
				"UNKNOWN PROB");
		} else {
			retval = nvlist_lookup_string(nvl, "path-class",
				&path_class_val);
			if (retval != 0) {
			(void) strcpy(pathlist_p->path_info[i].path_class,
				"UNKNOWN");
			} else {
				(void) strcpy(pathlist_p->path_info[i].
					path_class,
					path_class_val);
			}
			nvlist_free(nvl);
		}
	}

	/* free everything we alloced */
	for (i = 0; i < ioc.buf_elem; i++) {
		free(ioc.ret_buf[i].ret_prop.buf);
		free(ioc.ret_buf[i].ret_prop.ret_buf_size);
	}
	free(ioc.ret_buf);
return (caller_ret);
}

/*
 * To get the number of paths to a given device pathname using
 * driver ioctl.
 *
 * inputs:
 *   dev path you would like to recieve mp count on
 * outputs:
 * returns:
 *   0  - success
 *   -1 - bad device path
 *   -2 - open failure
 *   -3 - ioctl failure
 */
int
g_get_pathcount(char *dev_path)
{
	char		*char_ptr;
	int		fd = -1;
	sv_iocdata_t	ioc;
	char		phci_path[MAXPATHLEN];
	char		client_path[MAXPATHLEN];
	char		paddr[MAXNAMELEN];
	uint_t		num_elem = 0;
	int		retval = 0;
	char		*physical_path;

	/* translate device path to physical path */
	physical_path = g_get_physical_name(dev_path);
	/* ensure physical path is not NULL, or strcpy will core */
	if (physical_path == NULL) {
		return (-1);
	}
	/* copy physical path without /devices/ prefix */
	(void) strcpy(client_path, physical_path + DEV_PREFIX_LEN-1);
	free(physical_path);

	if ((char_ptr = strrchr(client_path, ':')) != NULL) {
		*char_ptr = '\0';
	}

	/* prepare sv_iocdata_t structure */
	ioc.client	= client_path;
	ioc.phci	= phci_path;
	ioc.addr	= paddr;
	ioc.buf_elem	= 0;
	ioc.ret_buf	= NULL;
	ioc.ret_elem	= &num_elem;

	strcpy(ioc.phci, client_path);

	/* Get file descr. for "/devices/scsi_vhci:devctl" */
	if ((fd = g_object_open(VHCI_NODE, O_RDWR)) < 0) {
		return (-2);
	}

	/* Issue open to device to get multipath_info (ie. count) */
	retval = ioctl(fd, SCSI_VHCI_GET_CLIENT_MULTIPATH_INFO, &ioc);
	close(fd);

	/* Check icotl status */
	if (retval == 0) {
		/* success */
		return (*ioc.ret_elem);
	} else {
		/* failure */
		return (-3);
	}

}


/*
 * Call driver to effect failover for a given pathclass
 *
 * inputs:
 * outputs:
 * returns:
 *   0  - success
 *   !0 - failure
 */
int
g_failover(char *dev_path, char *path_class)
{
int		fd = 0, ret = 0;
char		client_path[MAXPATHLEN];
char		class[MAXNAMELEN];
sv_switch_to_cntlr_iocdata_t	iocsc;

char		*char_ptr_start, *char_ptr_end;


	if (strstr(dev_path, SCSI_VHCI) == NULL) {
		return (L_INVALID_PATH);
	}

	char_ptr_start = dev_path + strlen("/devices");
	if ((char_ptr_end = strrchr(char_ptr_start, ':')) != NULL) {
		*char_ptr_end = '\0';
	}

	if ((fd = g_object_open(VHCI_NODE, O_RDWR)) < 0) {
		return (L_OPEN_PATH_FAIL);
	}

	iocsc.client = client_path;
	iocsc.class = class;

	strcpy(iocsc.client, char_ptr_start);
	strcpy(iocsc.class, path_class);

	if (ioctl(fd, SCSI_VHCI_SWITCH_TO_CNTLR, &iocsc) != 0) {
		switch (errno) {
			case EALREADY:
				ret = L_SCSI_VHCI_ALREADY_ACTIVE;
				break;
			case ENXIO:
				ret = L_INVALID_PATH;
				break;
			case EIO:
				ret = L_SCSI_VHCI_NO_STANDBY;
				break;
			case ENOTSUP:
				ret = L_SCSI_VHCI_FAILOVER_NOTSUP;
				break;
			case EBUSY:
				ret = L_SCSI_VHCI_FAILOVER_BUSY;
				break;
			case EFAULT:
			default:
				ret = L_SCSI_VHCI_ERROR;
		}
	}

	close(fd);
	return (ret);
}

static void
g_free_pi_list(sv_path_info_t *pi, uint_t num_paths)
{
sv_path_info_t *pi_h = pi;
int i = 0;

	while (i++ < num_paths && pi != NULL) {
		free(pi->ret_prop.buf);
		free(pi->ret_prop.ret_buf_size);
		pi++;
	}
	free(pi_h);
}


/*
 * Name: stms_path_enable_disable
 *
 * inputs:
 *
 * client_path	client device path
 *	example: /devices/scsi_vhci/ssd@g280000602200416d6257333030303261:c,raw
 *
 * phci		Controller device path
 *	example: /devices/pci@4,4000/SUNW,qlc@4/fp@0,0
 *
 * request should be set to one of the following:
 *	SCSI_VHCI_PATH_DISABLE
 *	SCSI_VHCI_PATH_ENABLE
 *
 * returns:
 *	0 for success
 *	non-zero otherwise
 */
static int
stms_path_enable_disable(char *client_path, char *phci, int request)
{
	char *ioc_phci;
	char *char_ptr_end;
	char *client_physical_path, *client_path_ptr;
	int fd;
	sv_iocdata_t	ioc;

	if (!client_path || !phci) {
		return (EINVAL);
	}

	if ((fd = g_object_open(VHCI_NODE, O_RDWR)) < 0) {
		return (L_OPEN_PATH_FAIL);
	}

	/*
	 * translate device path to physical path
	 * Save off the ptr for use by free
	 */
	client_path_ptr = client_physical_path =
		g_get_physical_name(client_path);

	/* ensure physical path is not NULL, or strcpy will core */
	if (client_physical_path == NULL) {
		return (EINVAL);
	}

	/*
	 * Must be a scsi_vhci path
	 */
	if (strstr(client_physical_path, SCSI_VHCI) == NULL) {
		free(client_path_ptr);
		return (L_INVALID_PATH);
	}

	/* physical path without /devices/ prefix */
	client_physical_path += DEV_PREFIX_LEN - 1;

	if ((char_ptr_end = strrchr(client_physical_path, ':')) != NULL) {
		*char_ptr_end = '\0';
	}

	/*
	 * If there is a '/devices', strip it, if not
	 * assume it is complete and correct
	 */
	if (strncmp(phci, DEV_PREFIX, DEV_PREFIX_LEN) == 0) {
		ioc_phci = phci + DEV_PREFIX_LEN - 1;
	} else {
		ioc_phci = phci;
	}

	memset(&ioc, 0, sizeof (ioc));

	ioc.client = client_physical_path;
	ioc.phci = ioc_phci;

	/*
	 * Issue requested operation
	 */
	if (ioctl(fd, request, &ioc) != 0) {
		free(client_path_ptr);
		return (errno);
	}
	free(client_path_ptr);
	return (0);
}

int
g_stms_path_disable(char *client_path, char *phci)
{
	return (stms_path_enable_disable(client_path, phci,
		SCSI_VHCI_PATH_DISABLE));
}

int
g_stms_path_enable(char *client_path, char *phci)
{
	return (stms_path_enable_disable(client_path, phci,
		SCSI_VHCI_PATH_ENABLE));
}

/*
 * Name: stms_path_enable_disable_all
 *
 * inputs:
 *
 * phci		Controller device path
 *	example: /devices/pci@4,4000/SUNW,qlc@4/fp@0,0
 *
 * request should be set to one of the following:
 *	SCSI_VHCI_PATH_DISABLE
 *	SCSI_VHCI_PATH_ENABLE
 *
 * returns:
 *	0 for success
 *	non-zero otherwise
 */

static int
stms_path_enable_disable_all(char *phci, int request)
{
	int fd;
	char *ioc_phci;
	sv_iocdata_t ioc;

	if (!phci) {
		return (EINVAL);
	}

	if ((fd = g_object_open(VHCI_NODE, O_RDWR)) < 0) {
		return (L_OPEN_PATH_FAIL);
	}

	memset(&ioc, 0, sizeof (ioc));

	/*
	 * If there is a '/devices', strip it, if not
	 * assume it is complete and correct
	 */
	if (strncmp(phci, DEV_PREFIX, DEV_PREFIX_LEN) == 0) {
		ioc_phci = phci + DEV_PREFIX_LEN - 1;
	} else {
		ioc_phci = phci;
	}

	ioc.client = "/scsi_vhci";
	ioc.phci = ioc_phci;

	/*
	 * Issue requested operation
	 */
	if (ioctl(fd, request, &ioc) != 0) {
		return (errno);
	}
	return (0);
}

int
g_stms_path_disable_all(char *phci)
{
	/*
	 * issue disable on all clients for a phci
	 */
	return (stms_path_enable_disable_all(phci, SCSI_VHCI_PATH_DISABLE));
}

int
g_stms_path_enable_all(char *phci)
{
	/*
	 * issue enable on all clients for a phci
	 */
	return (stms_path_enable_disable_all(phci, SCSI_VHCI_PATH_ENABLE));
}

/*
 * Name: stms_get_path_state
 *
 * inputs:
 *
 * client_path	client device path
 *	example: /devices/scsi_vhci/ssd@g280000602200416d6257333030303261:c,raw
 *
 * phci		Controller device path
 *	example: /devices/pci@4,4000/SUNW,qlc@4/fp@0,0
 *
 * outputs:
 * state set to one of enum mdi_pathinfo_state_t in sunmdi.h
 *	MDI_PATHINFO_STATE_*
 *
 * ext_state set to one or more of the bits defined in mdi_impldefs.h
 *	MDI_PATHINFO_STATE_*
 *
 *
 * returns:
 *	0 for success
 *	non-zero otherwise
 */
int
g_stms_get_path_state(char *client_path, char *phci, int *state, int *ext_state)
{
	sv_iocdata_t ioc;
	int num_paths;
	char *ioc_phci;
	int i;
	int found = 0;
	int err;
	char	phci_path[MAXPATHLEN];
	char	cpath[MAXPATHLEN];
	char	paddr[MAXNAMELEN];


	if (!client_path || !phci) {
		return (EINVAL);
	}

	ioc.client = cpath;
	ioc.phci = phci_path;
	ioc.addr = paddr;

	/*
	 * Get all the paths for this client
	 */
	if ((err = get_pathlist(client_path, &ioc, &num_paths))
		!= 0) {
		return (err);
	}

	/*
	 * If there is a '/devices', strip it, if not
	 * assume it is complete and correct
	 */
	if (strncmp(phci, DEV_PREFIX, DEV_PREFIX_LEN) == 0) {
		ioc_phci = phci + DEV_PREFIX_LEN - 1;
	} else {
		ioc_phci = phci;
	}

	/*
	 * get ioctl response states
	 * for the requested client and phci
	 * and copy them to caller's buffers
	 */
	for (i = 0; i < num_paths; i++) {
		if (strncmp(ioc_phci, ioc.ret_buf[i].device.ret_phci,
			strlen(ioc_phci)) == 0) {
			found++;
			*state = ioc.ret_buf[i].ret_state;
			*ext_state = ioc.ret_buf[i].ret_ext_state;
			break;
		}
	}

	/* free everything we alloced */
	for (i = 0; i < ioc.buf_elem; i++) {
		free(ioc.ret_buf[i].ret_prop.buf);
		free(ioc.ret_buf[i].ret_prop.ret_buf_size);
	}
	free(ioc.ret_buf);

	if (found) {
		return (0);
	} else {
		/* Requested path not found */
		return (ENXIO);
	}
}