xref: /freebsd/usr.sbin/mptutil/mpt_cam.c (revision 0957b409)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2008 Yahoo!, Inc.
5  * All rights reserved.
6  * Written by: John Baldwin <jhb@FreeBSD.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    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 __RCSID("$FreeBSD$");
35 
36 #include <sys/param.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 
44 #include <camlib.h>
45 #include <cam/scsi/scsi_message.h>
46 #include <cam/scsi/scsi_pass.h>
47 
48 #include "mptutil.h"
49 
50 static int xptfd;
51 
52 static int
53 xpt_open(void)
54 {
55 
56 	if (xptfd == 0)
57 		xptfd = open(XPT_DEVICE, O_RDWR);
58 	return (xptfd);
59 }
60 
61 /* Fetch the path id of bus 0 for the opened mpt controller. */
62 static int
63 fetch_path_id(path_id_t *path_id)
64 {
65 	struct bus_match_pattern *b;
66 	union ccb ccb;
67 	size_t bufsize;
68 	int error;
69 
70 	if (xpt_open() < 0)
71 		return (ENXIO);
72 
73 	/* First, find the path id of bus 0 for this mpt controller. */
74 	bzero(&ccb, sizeof(ccb));
75 
76 	ccb.ccb_h.func_code = XPT_DEV_MATCH;
77 
78 	bufsize = sizeof(struct dev_match_result) * 1;
79 	ccb.cdm.num_matches = 0;
80 	ccb.cdm.match_buf_len = bufsize;
81 	ccb.cdm.matches = calloc(1, bufsize);
82 
83 	bufsize = sizeof(struct dev_match_pattern) * 1;
84 	ccb.cdm.num_patterns = 1;
85 	ccb.cdm.pattern_buf_len = bufsize;
86 	ccb.cdm.patterns = calloc(1, bufsize);
87 
88 	/* Match mptX bus 0. */
89 	ccb.cdm.patterns[0].type = DEV_MATCH_BUS;
90 	b = &ccb.cdm.patterns[0].pattern.bus_pattern;
91 	snprintf(b->dev_name, sizeof(b->dev_name), "mpt");
92 	b->unit_number = mpt_unit;
93 	b->bus_id = 0;
94 	b->flags = BUS_MATCH_NAME | BUS_MATCH_UNIT | BUS_MATCH_BUS_ID;
95 
96 	if (ioctl(xptfd, CAMIOCOMMAND, &ccb) < 0) {
97 		error = errno;
98 		free(ccb.cdm.matches);
99 		free(ccb.cdm.patterns);
100 		return (error);
101 	}
102 	free(ccb.cdm.patterns);
103 
104 	if (((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) ||
105 	    (ccb.cdm.status != CAM_DEV_MATCH_LAST)) {
106 		warnx("fetch_path_id got CAM error %#x, CDM error %d\n",
107 		    ccb.ccb_h.status, ccb.cdm.status);
108 		free(ccb.cdm.matches);
109 		return (EIO);
110 	}
111 
112 	/* We should have exactly 1 match for the bus. */
113 	if (ccb.cdm.num_matches != 1 ||
114 	    ccb.cdm.matches[0].type != DEV_MATCH_BUS) {
115 		free(ccb.cdm.matches);
116 		return (ENOENT);
117 	}
118 	*path_id = ccb.cdm.matches[0].result.bus_result.path_id;
119 	free(ccb.cdm.matches);
120 	return (0);
121 }
122 
123 int
124 mpt_query_disk(U8 VolumeBus, U8 VolumeID, struct mpt_query_disk *qd)
125 {
126 	struct periph_match_pattern *p;
127 	struct periph_match_result *r;
128 	union ccb ccb;
129 	path_id_t path_id;
130 	size_t bufsize;
131 	int error;
132 
133 	/* mpt(4) only handles devices on bus 0. */
134 	if (VolumeBus != 0)
135 		return (ENXIO);
136 
137 	if (xpt_open() < 0)
138 		return (ENXIO);
139 
140 	/* Find the path ID of bus 0. */
141 	error = fetch_path_id(&path_id);
142 	if (error)
143 		return (error);
144 
145 	bzero(&ccb, sizeof(ccb));
146 
147 	ccb.ccb_h.func_code = XPT_DEV_MATCH;
148 	ccb.ccb_h.path_id = CAM_XPT_PATH_ID;
149 	ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
150 	ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
151 
152 	bufsize = sizeof(struct dev_match_result) * 5;
153 	ccb.cdm.num_matches = 0;
154 	ccb.cdm.match_buf_len = bufsize;
155 	ccb.cdm.matches = calloc(1, bufsize);
156 
157 	bufsize = sizeof(struct dev_match_pattern) * 1;
158 	ccb.cdm.num_patterns = 1;
159 	ccb.cdm.pattern_buf_len = bufsize;
160 	ccb.cdm.patterns = calloc(1, bufsize);
161 
162 	/* Look for a "da" device at the specified target and lun. */
163 	ccb.cdm.patterns[0].type = DEV_MATCH_PERIPH;
164 	p = &ccb.cdm.patterns[0].pattern.periph_pattern;
165 	p->path_id = path_id;
166 	snprintf(p->periph_name, sizeof(p->periph_name), "da");
167 	p->target_id = VolumeID;
168 	p->flags = PERIPH_MATCH_PATH | PERIPH_MATCH_NAME | PERIPH_MATCH_TARGET;
169 
170 	if (ioctl(xptfd, CAMIOCOMMAND, &ccb) < 0) {
171 		error = errno;
172 		free(ccb.cdm.matches);
173 		free(ccb.cdm.patterns);
174 		return (error);
175 	}
176 	free(ccb.cdm.patterns);
177 
178 	if (((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) ||
179 	    (ccb.cdm.status != CAM_DEV_MATCH_LAST)) {
180 		warnx("mpt_query_disk got CAM error %#x, CDM error %d\n",
181 		    ccb.ccb_h.status, ccb.cdm.status);
182 		free(ccb.cdm.matches);
183 		return (EIO);
184 	}
185 
186 	/*
187 	 * We should have exactly 1 match for the peripheral.
188 	 * However, if we don't get a match, don't print an error
189 	 * message and return ENOENT.
190 	 */
191 	if (ccb.cdm.num_matches == 0) {
192 		free(ccb.cdm.matches);
193 		return (ENOENT);
194 	}
195 	if (ccb.cdm.num_matches != 1) {
196 		warnx("mpt_query_disk got %d matches, expected 1",
197 		    ccb.cdm.num_matches);
198 		free(ccb.cdm.matches);
199 		return (EIO);
200 	}
201 	if (ccb.cdm.matches[0].type != DEV_MATCH_PERIPH) {
202 		warnx("mpt_query_disk got wrong CAM match");
203 		free(ccb.cdm.matches);
204 		return (EIO);
205 	}
206 
207 	/* Copy out the data. */
208 	r = &ccb.cdm.matches[1].result.periph_result;
209 	snprintf(qd->devname, sizeof(qd->devname), "%s%d", r->periph_name,
210 	    r->unit_number);
211 	free(ccb.cdm.matches);
212 
213 	return (0);
214 }
215 
216 static int
217 periph_is_volume(CONFIG_PAGE_IOC_2 *ioc2, struct periph_match_result *r)
218 {
219 	CONFIG_PAGE_IOC_2_RAID_VOL *vol;
220 	int i;
221 
222 	if (ioc2 == NULL)
223 		return (0);
224 	vol = ioc2->RaidVolume;
225 	for (i = 0; i < ioc2->NumActiveVolumes; vol++, i++) {
226 		if (vol->VolumeBus == 0 && vol->VolumeID == r->target_id)
227 			return (1);
228 	}
229 	return (0);
230 }
231 
232 /* Much borrowed from scsireadcapacity() in src/sbin/camcontrol/camcontrol.c. */
233 static int
234 fetch_scsi_capacity(struct cam_device *dev, struct mpt_standalone_disk *disk)
235 {
236 	struct scsi_read_capacity_data rcap;
237 	struct scsi_read_capacity_data_long rcaplong;
238 	union ccb *ccb;
239 	int error;
240 
241 	ccb = cam_getccb(dev);
242 	if (ccb == NULL)
243 		return (ENOMEM);
244 
245 	/* Zero the rest of the ccb. */
246 	CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
247 
248 	scsi_read_capacity(&ccb->csio, 1, NULL, MSG_SIMPLE_Q_TAG, &rcap,
249 	    SSD_FULL_SIZE, 5000);
250 
251 	/* Disable freezing the device queue */
252 	ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
253 
254 	if (cam_send_ccb(dev, ccb) < 0) {
255 		error = errno;
256 		cam_freeccb(ccb);
257 		return (error);
258 	}
259 
260 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
261 		cam_freeccb(ccb);
262 		return (EIO);
263 	}
264 
265 	/*
266 	 * A last block of 2^32-1 means that the true capacity is over 2TB,
267 	 * and we need to issue the long READ CAPACITY to get the real
268 	 * capacity.  Otherwise, we're all set.
269 	 */
270 	if (scsi_4btoul(rcap.addr) != 0xffffffff) {
271 		disk->maxlba = scsi_4btoul(rcap.addr);
272 		cam_freeccb(ccb);
273 		return (0);
274 	}
275 
276 	/* Zero the rest of the ccb. */
277 	CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
278 
279 	scsi_read_capacity_16(&ccb->csio, 1, NULL, MSG_SIMPLE_Q_TAG, 0, 0, 0,
280 	    (uint8_t *)&rcaplong, sizeof(rcaplong), SSD_FULL_SIZE, 5000);
281 
282 	/* Disable freezing the device queue */
283 	ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
284 
285 	if (cam_send_ccb(dev, ccb) < 0) {
286 		error = errno;
287 		cam_freeccb(ccb);
288 		return (error);
289 	}
290 
291 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
292 		cam_freeccb(ccb);
293 		return (EIO);
294 	}
295 	cam_freeccb(ccb);
296 
297 	disk->maxlba = scsi_8btou64(rcaplong.addr);
298 	return (0);
299 }
300 
301 /* Borrowed heavily from scsi_all.c:scsi_print_inquiry(). */
302 static void
303 format_scsi_inquiry(struct mpt_standalone_disk *disk,
304     struct scsi_inquiry_data *inq_data)
305 {
306 	char vendor[16], product[48], revision[16], rstr[12];
307 
308 	if (SID_QUAL_IS_VENDOR_UNIQUE(inq_data))
309 		return;
310 	if (SID_TYPE(inq_data) != T_DIRECT)
311 		return;
312 	if (SID_QUAL(inq_data) != SID_QUAL_LU_CONNECTED)
313 		return;
314 
315 	cam_strvis(vendor, inq_data->vendor, sizeof(inq_data->vendor),
316 	    sizeof(vendor));
317 	cam_strvis(product, inq_data->product, sizeof(inq_data->product),
318 	    sizeof(product));
319 	cam_strvis(revision, inq_data->revision, sizeof(inq_data->revision),
320 	    sizeof(revision));
321 
322 	/* Hack for SATA disks, no idea how to tell speed. */
323 	if (strcmp(vendor, "ATA") == 0) {
324 		snprintf(disk->inqstring, sizeof(disk->inqstring),
325 		    "<%s %s> SATA", product, revision);
326 		return;
327 	}
328 
329 	switch (SID_ANSI_REV(inq_data)) {
330 	case SCSI_REV_CCS:
331 		strcpy(rstr, "SCSI-CCS");
332 		break;
333 	case 5:
334 		strcpy(rstr, "SAS");
335 		break;
336 	default:
337 		snprintf(rstr, sizeof (rstr), "SCSI-%d",
338 		    SID_ANSI_REV(inq_data));
339 		break;
340 	}
341 	snprintf(disk->inqstring, sizeof(disk->inqstring), "<%s %s %s> %s",
342 	    vendor, product, revision, rstr);
343 }
344 
345 /* Much borrowed from scsiinquiry() in src/sbin/camcontrol/camcontrol.c. */
346 static int
347 fetch_scsi_inquiry(struct cam_device *dev, struct mpt_standalone_disk *disk)
348 {
349 	struct scsi_inquiry_data *inq_buf;
350 	union ccb *ccb;
351 	int error;
352 
353 	ccb = cam_getccb(dev);
354 	if (ccb == NULL)
355 		return (ENOMEM);
356 
357 	/* Zero the rest of the ccb. */
358 	CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
359 
360 	inq_buf = calloc(1, sizeof(*inq_buf));
361 	if (inq_buf == NULL) {
362 		cam_freeccb(ccb);
363 		return (ENOMEM);
364 	}
365 	scsi_inquiry(&ccb->csio, 1, NULL, MSG_SIMPLE_Q_TAG, (void *)inq_buf,
366 	    SHORT_INQUIRY_LENGTH, 0, 0, SSD_FULL_SIZE, 5000);
367 
368 	/* Disable freezing the device queue */
369 	ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
370 
371 	if (cam_send_ccb(dev, ccb) < 0) {
372 		error = errno;
373 		free(inq_buf);
374 		cam_freeccb(ccb);
375 		return (error);
376 	}
377 
378 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
379 		free(inq_buf);
380 		cam_freeccb(ccb);
381 		return (EIO);
382 	}
383 
384 	cam_freeccb(ccb);
385 	format_scsi_inquiry(disk, inq_buf);
386 	free(inq_buf);
387 	return (0);
388 }
389 
390 int
391 mpt_fetch_disks(int fd, int *ndisks, struct mpt_standalone_disk **disksp)
392 {
393 	CONFIG_PAGE_IOC_2 *ioc2;
394 	struct mpt_standalone_disk *disks;
395 	struct periph_match_pattern *p;
396 	struct periph_match_result *r;
397 	struct cam_device *dev;
398 	union ccb ccb;
399 	path_id_t path_id;
400 	size_t bufsize;
401 	int count, error;
402 	uint32_t i;
403 
404 	if (xpt_open() < 0)
405 		return (ENXIO);
406 
407 	error = fetch_path_id(&path_id);
408 	if (error)
409 		return (error);
410 
411 	for (count = 100;; count+= 100) {
412 		/* Try to fetch 'count' disks in one go. */
413 		bzero(&ccb, sizeof(ccb));
414 
415 		ccb.ccb_h.func_code = XPT_DEV_MATCH;
416 
417 		bufsize = sizeof(struct dev_match_result) * (count + 1);
418 		ccb.cdm.num_matches = 0;
419 		ccb.cdm.match_buf_len = bufsize;
420 		ccb.cdm.matches = calloc(1, bufsize);
421 
422 		bufsize = sizeof(struct dev_match_pattern) * 1;
423 		ccb.cdm.num_patterns = 1;
424 		ccb.cdm.pattern_buf_len = bufsize;
425 		ccb.cdm.patterns = calloc(1, bufsize);
426 
427 		/* Match any "da" peripherals. */
428 		ccb.cdm.patterns[0].type = DEV_MATCH_PERIPH;
429 		p = &ccb.cdm.patterns[0].pattern.periph_pattern;
430 		p->path_id = path_id;
431 		snprintf(p->periph_name, sizeof(p->periph_name), "da");
432 		p->flags = PERIPH_MATCH_PATH | PERIPH_MATCH_NAME;
433 
434 		if (ioctl(xptfd, CAMIOCOMMAND, &ccb) < 0) {
435 			error = errno;
436 			free(ccb.cdm.matches);
437 			free(ccb.cdm.patterns);
438 			return (error);
439 		}
440 		free(ccb.cdm.patterns);
441 
442 		/* Check for CCB errors. */
443 		if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
444 			free(ccb.cdm.matches);
445 			return (EIO);
446 		}
447 
448 		/* If we need a longer list, try again. */
449 		if (ccb.cdm.status == CAM_DEV_MATCH_MORE) {
450 			free(ccb.cdm.matches);
451 			continue;
452 		}
453 
454 		/* If we got an error, abort. */
455 		if (ccb.cdm.status != CAM_DEV_MATCH_LAST) {
456 			free(ccb.cdm.matches);
457 			return (EIO);
458 		}
459 		break;
460 	}
461 
462 	/* Shortcut if we don't have any "da" devices. */
463 	if (ccb.cdm.num_matches == 0) {
464 		free(ccb.cdm.matches);
465 		*ndisks = 0;
466 		*disksp = NULL;
467 		return (0);
468 	}
469 
470 	/* We should have N matches, 1 for each "da" device. */
471 	for (i = 0; i < ccb.cdm.num_matches; i++) {
472 		if (ccb.cdm.matches[i].type != DEV_MATCH_PERIPH) {
473 			warnx("mpt_fetch_disks got wrong CAM matches");
474 			free(ccb.cdm.matches);
475 			return (EIO);
476 		}
477 	}
478 
479 	/*
480 	 * Some of the "da" peripherals may be for RAID volumes, so
481 	 * fetch the IOC 2 page (list of RAID volumes) so we can
482 	 * exclude them from the list.
483 	 */
484 	ioc2 = mpt_read_ioc_page(fd, 2, NULL);
485 	if (ioc2 == NULL)
486 		return (errno);
487 	disks = calloc(ccb.cdm.num_matches, sizeof(*disks));
488 	count = 0;
489 	for (i = 0; i < ccb.cdm.num_matches; i++) {
490 		r = &ccb.cdm.matches[i].result.periph_result;
491 		if (periph_is_volume(ioc2, r))
492 			continue;
493 		disks[count].bus = 0;
494 		disks[count].target = r->target_id;
495 		snprintf(disks[count].devname, sizeof(disks[count].devname),
496 		    "%s%d", r->periph_name, r->unit_number);
497 
498 		dev = cam_open_device(disks[count].devname, O_RDWR);
499 		if (dev != NULL) {
500 			fetch_scsi_capacity(dev, &disks[count]);
501 			fetch_scsi_inquiry(dev, &disks[count]);
502 			cam_close_device(dev);
503 		}
504 		count++;
505 	}
506 	free(ccb.cdm.matches);
507 	free(ioc2);
508 
509 	*ndisks = count;
510 	*disksp = disks;
511 	return (0);
512 }
513 
514 /*
515  * Instruct the mpt(4) device to rescan its buses to find new devices
516  * such as disks whose RAID physdisk page was removed or volumes that
517  * were created.  If id is -1, the entire bus is rescanned.
518  * Otherwise, only devices at the specified ID are rescanned.  If bus
519  * is -1, then all buses are scanned instead of the specified bus.
520  * Note that currently, only bus 0 is supported.
521  */
522 int
523 mpt_rescan_bus(int bus, int id)
524 {
525 	union ccb ccb;
526 	path_id_t path_id;
527 	int error;
528 
529 	/* mpt(4) only handles devices on bus 0. */
530 	if (bus != -1 && bus != 0)
531 		return (EINVAL);
532 
533 	if (xpt_open() < 0)
534 		return (ENXIO);
535 
536 	error = fetch_path_id(&path_id);
537 	if (error)
538 		return (error);
539 
540 	/* Perform the actual rescan. */
541 	bzero(&ccb, sizeof(ccb));
542 	ccb.ccb_h.path_id = path_id;
543 	if (id == -1) {
544 		ccb.ccb_h.func_code = XPT_SCAN_BUS;
545 		ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
546 		ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
547 		ccb.ccb_h.timeout = 5000;
548 	} else {
549 		ccb.ccb_h.func_code = XPT_SCAN_LUN;
550 		ccb.ccb_h.target_id = id;
551 		ccb.ccb_h.target_lun = 0;
552 	}
553 	ccb.crcn.flags = CAM_FLAG_NONE;
554 
555 	/* Run this at a low priority. */
556 	ccb.ccb_h.pinfo.priority = 5;
557 
558 	if (ioctl(xptfd, CAMIOCOMMAND, &ccb) == -1)
559 		return (errno);
560 
561 	if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
562 		warnx("mpt_rescan_bus rescan got CAM error %#x\n",
563 		    ccb.ccb_h.status & CAM_STATUS_MASK);
564 		return (EIO);
565 	}
566 
567 	return (0);
568 }
569