17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
54c06356bSdh * Common Development and Distribution License (the "License").
64c06356bSdh * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*540b2cc7SHans Rosenfeld * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
234c06356bSdh * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #include <devfsadm.h>
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <strings.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <limits.h>
327c478bd9Sstevel@tonic-gate #include <ctype.h>
334c06356bSdh #include <sys/int_fmtio.h>
344c06356bSdh #include <sys/scsi/scsi_address.h>
354c06356bSdh #include <sys/libdevid.h>
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate #define SGEN_LINK_RE "^scsi/.+/c[0-9]+t[0-9A-F]+d[0-9]+$"
387c478bd9Sstevel@tonic-gate #define SGEN_DIR "scsi"
397c478bd9Sstevel@tonic-gate #define SGEN_CLASS "generic-scsi"
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate static int sgen_callback(di_minor_t minor, di_node_t node);
427c478bd9Sstevel@tonic-gate static char *find_ctrlr(di_node_t node, di_minor_t minor);
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate static devfsadm_create_t sgen_create_cbt[] = {
467c478bd9Sstevel@tonic-gate { SGEN_CLASS, "ddi_generic:scsi", NULL,
477c478bd9Sstevel@tonic-gate TYPE_EXACT | CREATE_DEFER, ILEVEL_0, sgen_callback
487c478bd9Sstevel@tonic-gate }
497c478bd9Sstevel@tonic-gate };
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate DEVFSADM_CREATE_INIT_V0(sgen_create_cbt);
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate * HOT auto cleanup of sgen links not desired.
557c478bd9Sstevel@tonic-gate */
567c478bd9Sstevel@tonic-gate static devfsadm_remove_t sgen_remove_cbt[] = {
577c478bd9Sstevel@tonic-gate { SGEN_CLASS, SGEN_LINK_RE, RM_POST,
587c478bd9Sstevel@tonic-gate ILEVEL_0, devfsadm_rm_all
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate };
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate DEVFSADM_REMOVE_INIT_V0(sgen_remove_cbt);
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate static int
sgen_callback(di_minor_t minor,di_node_t node)657c478bd9Sstevel@tonic-gate sgen_callback(di_minor_t minor, di_node_t node)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate char *baddr, *cnum, *tstr;
687c478bd9Sstevel@tonic-gate char lpath[PATH_MAX], buf[PATH_MAX];
694c06356bSdh uchar_t *wwnstr;
704c06356bSdh char *tgt_port;
714c06356bSdh
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate if ((cnum = find_ctrlr(node, minor)) == NULL)
747c478bd9Sstevel@tonic-gate goto done;
757c478bd9Sstevel@tonic-gate
764c06356bSdh /*
774c06356bSdh * SCSAv3 attached devices.
784c06356bSdh */
797c478bd9Sstevel@tonic-gate if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
804c06356bSdh SCSI_ADDR_PROP_TARGET_PORT, &tgt_port) > 0) {
814c06356bSdh uint64_t wwn;
824c06356bSdh scsi_lun64_t sl;
834c06356bSdh scsi_lun_t lun;
844c06356bSdh int64_t lun64;
854c06356bSdh int64_t *lun64p;
864c06356bSdh int *intp;
874c06356bSdh uchar_t addr_method;
884c06356bSdh
894c06356bSdh /* Get lun property */
904c06356bSdh if ((di_prop_lookup_int64(DDI_DEV_T_ANY, node,
914c06356bSdh SCSI_ADDR_PROP_LUN64, &lun64p) > 0) &&
924c06356bSdh (*lun64p != SCSI_LUN64_ILLEGAL)) {
934c06356bSdh lun64 = *lun64p;
944c06356bSdh } else if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
954c06356bSdh SCSI_ADDR_PROP_LUN, &intp) > 0) {
964c06356bSdh lun64 = (uint64_t)*intp;
974c06356bSdh }
984c06356bSdh
994c06356bSdh lun = scsi_lun64_to_lun(lun64);
1004c06356bSdh
1014c06356bSdh addr_method = (lun.sl_lun1_msb & SCSI_LUN_AM_MASK);
1024c06356bSdh
1034c06356bSdh (void) scsi_wwnstr_to_wwn(tgt_port, &wwn);
1044c06356bSdh if ((addr_method == SCSI_LUN_AM_PDEV) &&
1054c06356bSdh (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
1064c06356bSdh (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
1074c06356bSdh (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
1084c06356bSdh (void) snprintf(lpath, PATH_MAX,
1094c06356bSdh "%s/%s/c%st%"PRIX64"d%"PRId64, SGEN_DIR,
1104c06356bSdh di_minor_name(minor), cnum, wwn, lun64);
1114c06356bSdh } else if ((addr_method == SCSI_LUN_AM_FLAT) &&
1124c06356bSdh (lun.sl_lun2_msb == 0) && (lun.sl_lun2_lsb == 0) &&
1134c06356bSdh (lun.sl_lun3_msb == 0) && (lun.sl_lun3_lsb == 0) &&
1144c06356bSdh (lun.sl_lun4_msb == 0) && (lun.sl_lun4_lsb == 0)) {
1154c06356bSdh sl = (lun.sl_lun1_msb << 8) | lun.sl_lun1_lsb;
1164c06356bSdh (void) snprintf(lpath, PATH_MAX,
1174c06356bSdh "%s/%s/c%st%"PRIX64"d%"PRIX16, SGEN_DIR,
1184c06356bSdh di_minor_name(minor), cnum, wwn, sl);
1194c06356bSdh } else {
1204c06356bSdh (void) snprintf(lpath, PATH_MAX,
1214c06356bSdh "%s/%s/c%st%"PRIX64"d%"PRIX64, SGEN_DIR,
1224c06356bSdh di_minor_name(minor), cnum, wwn, lun64);
1234c06356bSdh }
1244c06356bSdh } else if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
1254c06356bSdh "client-guid", (char **)&wwnstr) > 0) {
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate * MPXIO-enabled devices; lun is always 0.
1287c478bd9Sstevel@tonic-gate */
1294c06356bSdh if (strlcpy((char *)buf, (char *)wwnstr, sizeof (buf)) >=
1307c478bd9Sstevel@tonic-gate sizeof (buf))
1317c478bd9Sstevel@tonic-gate goto done;
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate for (tstr = buf; *tstr != '\0'; tstr++) {
1347c478bd9Sstevel@tonic-gate *tstr = toupper(*tstr);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate if (snprintf(lpath, sizeof (lpath), "%s/%s/c%st%sd0", SGEN_DIR,
1377c478bd9Sstevel@tonic-gate di_minor_name(minor), cnum, buf) >= sizeof (lpath))
1387c478bd9Sstevel@tonic-gate goto done;
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate } else if (di_prop_lookup_bytes(DDI_DEV_T_ANY, node,
1414c06356bSdh "port-wwn", &wwnstr) == 8) {
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate * "normal" fibre channel devices
1447c478bd9Sstevel@tonic-gate */
1457c478bd9Sstevel@tonic-gate int lun, *lunp, count;
1467c478bd9Sstevel@tonic-gate if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "lun", &lunp) > 0)
1477c478bd9Sstevel@tonic-gate lun = *lunp;
1487c478bd9Sstevel@tonic-gate else
1497c478bd9Sstevel@tonic-gate lun = 0;
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate for (count = 0, tstr = buf; count < 8; count++, tstr += 2)
1524c06356bSdh (void) sprintf(tstr, "%02X", wwnstr[count]);
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate *tstr = '\0';
1557c478bd9Sstevel@tonic-gate if (snprintf(lpath, sizeof (lpath), "%s/%s/c%st%sd%d", SGEN_DIR,
1567c478bd9Sstevel@tonic-gate di_minor_name(minor), cnum, buf, lun) >= sizeof (lpath))
1577c478bd9Sstevel@tonic-gate goto done;
1587c478bd9Sstevel@tonic-gate } else {
1597c478bd9Sstevel@tonic-gate /*
1607c478bd9Sstevel@tonic-gate * Parallel SCSI devices
1617c478bd9Sstevel@tonic-gate */
1627c478bd9Sstevel@tonic-gate uint_t targ, lun;
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate if ((baddr = di_bus_addr(node)) == NULL)
1657c478bd9Sstevel@tonic-gate goto done;
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate if (sscanf(baddr, "%X,%X", &targ, &lun) != 2)
1687c478bd9Sstevel@tonic-gate goto done;
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate if (snprintf(lpath, sizeof (lpath), "%s/%s/c%st%dd%d", SGEN_DIR,
1717c478bd9Sstevel@tonic-gate di_minor_name(minor), cnum, targ, lun) >= sizeof (lpath))
1727c478bd9Sstevel@tonic-gate goto done;
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate (void) devfsadm_mklink(lpath, node, minor, 0);
1767c478bd9Sstevel@tonic-gate done:
1777c478bd9Sstevel@tonic-gate free(cnum);
1787c478bd9Sstevel@tonic-gate return (DEVFSADM_CONTINUE);
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate /* index of enumeration rule applicable to this module */
1827c478bd9Sstevel@tonic-gate #define RULE_INDEX 2
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate static char *
find_ctrlr(di_node_t node,di_minor_t minor)1857c478bd9Sstevel@tonic-gate find_ctrlr(di_node_t node, di_minor_t minor)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate char path[PATH_MAX + 1];
1887c478bd9Sstevel@tonic-gate char *devfspath;
1897c478bd9Sstevel@tonic-gate char *buf, *mn;
190*540b2cc7SHans Rosenfeld boolean_t is_vhci;
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate devfsadm_enumerate_t rules[3] = {
1937c478bd9Sstevel@tonic-gate {"^r?dsk$/^c([0-9]+)", 1, MATCH_PARENT},
1947c478bd9Sstevel@tonic-gate {"^cfg$/^c([0-9]+)$", 1, MATCH_ADDR},
1957c478bd9Sstevel@tonic-gate {"^scsi$/^.+$/^c([0-9]+)", 1, MATCH_PARENT}
1967c478bd9Sstevel@tonic-gate };
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate mn = di_minor_name(minor);
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate if ((devfspath = di_devfs_path(node)) == NULL) {
2017c478bd9Sstevel@tonic-gate return (NULL);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate (void) strcpy(path, devfspath);
2047c478bd9Sstevel@tonic-gate (void) strcat(path, ":");
2057c478bd9Sstevel@tonic-gate (void) strcat(path, mn);
2067c478bd9Sstevel@tonic-gate di_devfs_path_free(devfspath);
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate * Use controller (parent) component of device path
2107c478bd9Sstevel@tonic-gate */
211*540b2cc7SHans Rosenfeld is_vhci = (strncmp(path, "/scsi_vhci/", 11) == 0);
2127c478bd9Sstevel@tonic-gate
213*540b2cc7SHans Rosenfeld if (ctrl_enumerate_int(path, RULE_INDEX, &buf, rules, 3, 1, is_vhci) ==
214*540b2cc7SHans Rosenfeld DEVFSADM_MULTIPLE) {
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate * We failed because there are multiple logical controller
2177c478bd9Sstevel@tonic-gate * numbers for a single physical controller. If we use node
2187c478bd9Sstevel@tonic-gate * name also for DEVICE paths in the match it should fix this
2197c478bd9Sstevel@tonic-gate * and only find one logical controller. (See 4045879).
2207c478bd9Sstevel@tonic-gate * NOTE: Rules for controllers are not changed, as there is
2217c478bd9Sstevel@tonic-gate * no unique controller number for them in this case.
2227c478bd9Sstevel@tonic-gate *
2237c478bd9Sstevel@tonic-gate * MATCH_UNCACHED flag is private to the "disks" and "sgen"
2247c478bd9Sstevel@tonic-gate * modules. NOT to be used by other modules.
2257c478bd9Sstevel@tonic-gate */
2267c478bd9Sstevel@tonic-gate rules[0].flags = MATCH_NODE | MATCH_UNCACHED; /* disks */
2277c478bd9Sstevel@tonic-gate rules[2].flags = MATCH_NODE | MATCH_UNCACHED; /* generic scsi */
228*540b2cc7SHans Rosenfeld if (ctrl_enumerate_int(path, RULE_INDEX, &buf, rules, 3, 0,
229*540b2cc7SHans Rosenfeld is_vhci)) {
2307c478bd9Sstevel@tonic-gate return (NULL);
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate return (buf);
2357c478bd9Sstevel@tonic-gate }
236