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