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