xref: /illumos-gate/usr/src/lib/libnvme/common/libnvme_vendor.c (revision da7fc762b82ced1a0ec19a51e04cdf823187ec77)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2024 Oxide Computer Company
14  */
15 
16 /*
17  * Implements logic to map a device to a vendor-specific entity.
18  */
19 
20 #include <libdevinfo.h>
21 #include <sys/sysmacros.h>
22 #include <string.h>
23 
24 #include "libnvme_impl.h"
25 
26 const nvme_vsd_t *const nvme_vsd_devices[] = {
27 	&wdc_sn840,
28 	&wdc_sn650,
29 	&wdc_sn655
30 };
31 
32 /*
33  * Our job is to attempt to map a given device to vendor specific information,
34  * if it exists. It may not.
35  */
36 void
37 nvme_vendor_map_ctrl(nvme_ctrl_t *ctrl)
38 {
39 	int *vid, *did;
40 
41 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, ctrl->nc_devi, "vendor-id",
42 	    &vid) != 1 || di_prop_lookup_ints(DDI_DEV_T_ANY, ctrl->nc_devi,
43 	    "device-id", &did) != 1) {
44 		return;
45 	}
46 
47 	for (size_t i = 0; i < ARRAY_SIZE(nvme_vsd_devices); i++) {
48 		if (nvme_vsd_devices[i]->nvd_vid == vid[0] &&
49 		    nvme_vsd_devices[i]->nvd_did == did[0]) {
50 			ctrl->nc_vsd = nvme_vsd_devices[i];
51 			return;
52 		}
53 	}
54 }
55 
56 bool
57 nvme_vendor_vuc_supported(nvme_ctrl_t *ctrl, const char *name)
58 {
59 	if (ctrl->nc_vsd != NULL) {
60 		for (size_t i = 0; i < ctrl->nc_vsd->nvd_nvuc; i++) {
61 			if (strcmp(name, ctrl->nc_vsd->nvd_vuc[i].nvd_short) ==
62 			    0) {
63 				return (true);
64 			}
65 		}
66 	}
67 
68 	return (nvme_ctrl_error(ctrl, NVME_ERR_VU_FUNC_UNSUP_BY_DEV, 0,
69 	    "device missing support for vendor unique command %s", name));
70 }
71