1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1997-2007 Kenneth D. Merry
5 * Copyright (c) 2012 The FreeBSD Foundation
6 * All rights reserved.
7 *
8 * Portions of this software were developed by Edward Tomasz Napierala
9 * under sponsorship from the FreeBSD Foundation.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 *    derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD: stable/11/usr.bin/iscsictl/periphs.c 330449 2018-03-05 07:26:05Z eadler $");
37
38#include <sys/ioctl.h>
39#include <sys/stdint.h>
40#include <sys/types.h>
41#include <sys/endian.h>
42#include <sys/sbuf.h>
43
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include <unistd.h>
48#include <inttypes.h>
49#include <limits.h>
50#include <fcntl.h>
51
52#include <cam/cam.h>
53#include <cam/cam_debug.h>
54#include <cam/cam_ccb.h>
55#include <cam/scsi/scsi_all.h>
56#include <cam/scsi/scsi_da.h>
57#include <cam/scsi/scsi_pass.h>
58#include <cam/scsi/scsi_message.h>
59#include <cam/scsi/smp_all.h>
60#include <cam/ata/ata_all.h>
61#include <camlib.h>
62#include <libxo/xo.h>
63
64#include "iscsictl.h"
65
66void
67print_periphs(int session_id)
68{
69	union ccb ccb;
70	int bufsize, fd;
71	unsigned int i;
72	int have_path_id, skip_bus, skip_device;
73	path_id_t path_id;
74
75	if ((fd = open(XPT_DEVICE, O_RDWR)) == -1) {
76		xo_warn("couldn't open %s", XPT_DEVICE);
77		return;
78	}
79
80	/*
81	 * First, iterate over the whole list to find the bus.
82	 */
83
84	bzero(&ccb, sizeof(union ccb));
85
86	ccb.ccb_h.path_id = CAM_XPT_PATH_ID;
87	ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
88	ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
89
90	ccb.ccb_h.func_code = XPT_DEV_MATCH;
91	bufsize = sizeof(struct dev_match_result) * 100;
92	ccb.cdm.match_buf_len = bufsize;
93	ccb.cdm.matches = (struct dev_match_result *)malloc(bufsize);
94	if (ccb.cdm.matches == NULL) {
95		xo_warnx("can't malloc memory for matches");
96		close(fd);
97		return;
98	}
99	ccb.cdm.num_matches = 0;
100
101	/*
102	 * We fetch all nodes, since we display most of them in the default
103	 * case, and all in the verbose case.
104	 */
105	ccb.cdm.num_patterns = 0;
106	ccb.cdm.pattern_buf_len = 0;
107
108	path_id = -1; /* Make GCC happy. */
109	have_path_id = 0;
110	skip_bus = 1;
111	skip_device = 1;
112
113	xo_open_container("devices");
114	xo_open_list("lun");
115	/*
116	 * We do the ioctl multiple times if necessary, in case there are
117	 * more than 100 nodes in the EDT.
118	 */
119	do {
120		if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
121			xo_warn("error sending CAMIOCOMMAND ioctl");
122			break;
123		}
124
125		if ((ccb.ccb_h.status != CAM_REQ_CMP)
126		 || ((ccb.cdm.status != CAM_DEV_MATCH_LAST)
127		    && (ccb.cdm.status != CAM_DEV_MATCH_MORE))) {
128			xo_warnx("got CAM error %#x, CDM error %d\n",
129			      ccb.ccb_h.status, ccb.cdm.status);
130			break;
131		}
132
133		for (i = 0; i < ccb.cdm.num_matches; i++) {
134			switch (ccb.cdm.matches[i].type) {
135			case DEV_MATCH_BUS: {
136				struct bus_match_result *bus_result;
137
138				bus_result = &ccb.cdm.matches[i].result.bus_result;
139
140				skip_bus = 1;
141
142				if (strcmp(bus_result->dev_name, "iscsi") != 0) {
143					//printf("not iscsi\n");
144					continue;
145				}
146
147				if ((int)bus_result->unit_number != session_id) {
148					//printf("wrong unit, %d != %d\n", bus_result->unit_number, session_id);
149					continue;
150				}
151				skip_bus = 0;
152			}
153			case DEV_MATCH_DEVICE: {
154				skip_device = 1;
155
156				if (skip_bus != 0)
157					continue;
158
159				skip_device = 0;
160				break;
161			}
162			case DEV_MATCH_PERIPH: {
163				struct periph_match_result *periph_result;
164
165				periph_result =
166				      &ccb.cdm.matches[i].result.periph_result;
167
168				if (skip_device != 0)
169					continue;
170
171				if (strcmp(periph_result->periph_name, "pass") == 0)
172					continue;
173
174				xo_open_instance("lun");
175				xo_emit("{e:lun/%d}", periph_result->target_lun);
176				xo_emit("{Vq:device/%s%d} ",
177				    periph_result->periph_name,
178				    periph_result->unit_number);
179				xo_close_instance("lun");
180
181				if (have_path_id == 0) {
182					path_id = periph_result->path_id;
183					have_path_id = 1;
184				}
185
186				break;
187			}
188			default:
189				fprintf(stdout, "unknown match type\n");
190				break;
191			}
192		}
193
194	} while ((ccb.ccb_h.status == CAM_REQ_CMP)
195		&& (ccb.cdm.status == CAM_DEV_MATCH_MORE));
196	xo_close_list("lun");
197
198	xo_emit("{e:scbus/%d}{e:bus/%d}{e:target/%d}",
199		have_path_id ? (int)path_id : -1, 0, have_path_id ? 0 : -1);
200	xo_close_container("devices");
201
202	close(fd);
203}
204
205