1 /* $OpenBSD: mpath_hds.c,v 1.23 2020/06/30 18:43:37 krw Exp $ */ 2 3 /* 4 * Copyright (c) 2011 David Gwynne <dlg@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 /* Hitachi Modular Storage support for mpath(4) */ 20 21 #include <sys/param.h> 22 #include <sys/systm.h> 23 #include <sys/kernel.h> 24 #include <sys/malloc.h> 25 #include <sys/device.h> 26 #include <sys/conf.h> 27 #include <sys/queue.h> 28 #include <sys/rwlock.h> 29 #include <sys/pool.h> 30 #include <sys/ioctl.h> 31 #include <sys/poll.h> 32 #include <sys/selinfo.h> 33 34 #include <scsi/scsi_all.h> 35 #include <scsi/scsiconf.h> 36 #include <scsi/mpathvar.h> 37 38 #define HDS_INQ_LDEV_OFFSET 44 39 #define HDS_INQ_LDEV_LEN 4 40 #define HDS_INQ_CTRL_OFFSET 49 41 #define HDS_INQ_PORT_OFFSET 50 42 #define HDS_INQ_TYPE_OFFSET 128 43 #define HDS_INQ_TYPE 0x44463030 /* "DF00" */ 44 45 #define HDS_VPD 0xe0 46 47 struct hds_vpd { 48 struct scsi_vpd_hdr hdr; /* HDS_VPD */ 49 u_int8_t state; 50 #define HDS_VPD_VALID 0x80 51 #define HDS_VPD_PREFERRED 0x40 52 53 /* followed by lots of unknown stuff */ 54 }; 55 56 #define HDS_SYMMETRIC 0 57 #define HDS_ASYMMETRIC 1 58 59 struct hds_softc { 60 struct device sc_dev; 61 struct mpath_path sc_path; 62 struct scsi_xshandler sc_xsh; 63 struct hds_vpd *sc_vpd; 64 int sc_mode; 65 int sc_ctrl; 66 }; 67 #define DEVNAME(_s) ((_s)->sc_dev.dv_xname) 68 69 int hds_match(struct device *, void *, void *); 70 void hds_attach(struct device *, struct device *, void *); 71 int hds_detach(struct device *, int); 72 int hds_activate(struct device *, int); 73 74 struct cfattach hds_ca = { 75 sizeof(struct hds_softc), 76 hds_match, 77 hds_attach, 78 hds_detach, 79 hds_activate 80 }; 81 82 struct cfdriver hds_cd = { 83 NULL, 84 "hds", 85 DV_DULL 86 }; 87 88 void hds_mpath_start(struct scsi_xfer *); 89 int hds_mpath_checksense(struct scsi_xfer *); 90 void hds_mpath_status(struct scsi_link *); 91 92 const struct mpath_ops hds_mpath_ops = { 93 "hds", 94 hds_mpath_checksense, 95 hds_mpath_status 96 }; 97 98 struct hds_device { 99 char *vendor; 100 char *product; 101 }; 102 103 int hds_inquiry(struct scsi_link *, int *); 104 int hds_info(struct hds_softc *); 105 106 void hds_status(struct scsi_xfer *); 107 void hds_status_done(struct scsi_xfer *); 108 109 struct hds_device hds_devices[] = { 110 /* " vendor " " device " */ 111 /* "01234567" "0123456789012345" */ 112 { "HITACHI ", "DF600F " }, 113 { "HITACHI ", "DF600F-CM " } 114 }; 115 116 int 117 hds_match(struct device *parent, void *match, void *aux) 118 { 119 struct scsi_attach_args *sa = aux; 120 struct scsi_link *link = sa->sa_sc_link; 121 struct scsi_inquiry_data *inq = &link->inqdata; 122 struct hds_device *s; 123 int i, mode; 124 125 if (mpath_path_probe(sa->sa_sc_link) != 0) 126 return (0); 127 128 for (i = 0; i < nitems(hds_devices); i++) { 129 s = &hds_devices[i]; 130 131 if (bcmp(s->vendor, inq->vendor, strlen(s->vendor)) == 0 && 132 bcmp(s->product, inq->product, strlen(s->product)) == 0 && 133 hds_inquiry(link, &mode) == 0) 134 return (8); 135 } 136 137 return (0); 138 } 139 140 void 141 hds_attach(struct device *parent, struct device *self, void *aux) 142 { 143 struct hds_softc *sc = (struct hds_softc *)self; 144 struct scsi_attach_args *sa = aux; 145 struct scsi_link *link = sa->sa_sc_link; 146 147 printf("\n"); 148 149 /* init link */ 150 link->device_softc = sc; 151 152 /* init path */ 153 scsi_xsh_set(&sc->sc_path.p_xsh, link, hds_mpath_start); 154 sc->sc_path.p_link = link; 155 156 /* init status handler */ 157 scsi_xsh_set(&sc->sc_xsh, link, hds_status); 158 sc->sc_vpd = dma_alloc(sizeof(*sc->sc_vpd), PR_WAITOK); 159 160 if (hds_inquiry(link, &sc->sc_mode) != 0) { 161 printf("%s: unable to query controller mode\n", DEVNAME(sc)); 162 return; 163 } 164 165 if (hds_info(sc) != 0) { 166 printf("%s: unable to query path info\n", DEVNAME(sc)); 167 return; 168 } 169 170 if (mpath_path_attach(&sc->sc_path, 171 (sc->sc_mode == HDS_SYMMETRIC) ? 0 : sc->sc_ctrl, 172 &hds_mpath_ops) != 0) 173 printf("%s: unable to attach path\n", DEVNAME(sc)); 174 } 175 176 int 177 hds_detach(struct device *self, int flags) 178 { 179 struct hds_softc *sc = (struct hds_softc *)self; 180 181 dma_free(sc->sc_vpd, sizeof(*sc->sc_vpd)); 182 183 return (0); 184 } 185 186 int 187 hds_activate(struct device *self, int act) 188 { 189 struct hds_softc *sc = (struct hds_softc *)self; 190 191 switch (act) { 192 case DVACT_DEACTIVATE: 193 if (sc->sc_path.p_group != NULL) 194 mpath_path_detach(&sc->sc_path); 195 break; 196 } 197 return (0); 198 } 199 200 void 201 hds_mpath_start(struct scsi_xfer *xs) 202 { 203 struct hds_softc *sc = xs->sc_link->device_softc; 204 205 mpath_start(&sc->sc_path, xs); 206 } 207 208 int 209 hds_mpath_checksense(struct scsi_xfer *xs) 210 { 211 return (MPATH_SENSE_DECLINED); 212 } 213 214 void 215 hds_mpath_status(struct scsi_link *link) 216 { 217 struct hds_softc *sc = link->device_softc; 218 219 if (sc->sc_mode == HDS_SYMMETRIC) 220 mpath_path_status(&sc->sc_path, MPATH_S_ACTIVE); 221 else 222 scsi_xsh_add(&sc->sc_xsh); 223 } 224 225 void 226 hds_status(struct scsi_xfer *xs) 227 { 228 struct scsi_link *link = xs->sc_link; 229 struct hds_softc *sc = link->device_softc; 230 231 scsi_init_inquiry(xs, SI_EVPD, HDS_VPD, 232 sc->sc_vpd, sizeof(*sc->sc_vpd)); 233 234 xs->done = hds_status_done; 235 236 scsi_xs_exec(xs); 237 } 238 239 void 240 hds_status_done(struct scsi_xfer *xs) 241 { 242 struct scsi_link *link = xs->sc_link; 243 struct hds_softc *sc = link->device_softc; 244 struct hds_vpd *vpd = sc->sc_vpd; 245 int status = MPATH_S_UNKNOWN; 246 247 if (xs->error == XS_NOERROR && 248 _2btol(vpd->hdr.page_length) >= sizeof(vpd->state) && 249 ISSET(vpd->state, HDS_VPD_VALID)) { 250 status = ISSET(vpd->state, HDS_VPD_PREFERRED) ? 251 MPATH_S_ACTIVE : MPATH_S_PASSIVE; 252 } 253 254 scsi_xs_put(xs); 255 256 mpath_path_status(&sc->sc_path, status); 257 } 258 259 int 260 hds_inquiry(struct scsi_link *link, int *mode) 261 { 262 struct scsi_xfer *xs; 263 u_int8_t *buf; 264 size_t len = link->inqdata.additional_length + 5; 265 int error; 266 267 if (len < HDS_INQ_TYPE_OFFSET + sizeof(int)) 268 return (ENXIO); 269 270 xs = scsi_xs_get(link, scsi_autoconf); 271 if (xs == NULL) 272 return (ENOMEM); 273 274 buf = dma_alloc(len, PR_WAITOK); 275 scsi_init_inquiry(xs, 0, 0, buf, len); 276 error = scsi_xs_sync(xs); 277 scsi_xs_put(xs); 278 if (error) 279 goto done; 280 281 if (buf[128] == '\0') 282 *mode = HDS_ASYMMETRIC; 283 else if (_4btol(&buf[HDS_INQ_TYPE_OFFSET]) == HDS_INQ_TYPE) 284 *mode = HDS_SYMMETRIC; 285 else 286 error = ENXIO; 287 288 done: 289 dma_free(buf, len); 290 return (error); 291 } 292 293 int 294 hds_info(struct hds_softc *sc) 295 { 296 struct scsi_link *link = sc->sc_path.p_link; 297 struct scsi_xfer *xs; 298 u_int8_t *buf; 299 size_t len = link->inqdata.additional_length + 5; 300 char ldev[9], ctrl, port; 301 int error; 302 303 xs = scsi_xs_get(link, scsi_autoconf); 304 if (xs == NULL) 305 return (ENOMEM); 306 307 buf = dma_alloc(len, PR_WAITOK); 308 scsi_init_inquiry(xs, 0, 0, buf, len); 309 error = scsi_xs_sync(xs); 310 scsi_xs_put(xs); 311 if (error) 312 goto done; 313 314 bzero(ldev, sizeof(ldev)); 315 scsi_strvis(ldev, &buf[HDS_INQ_LDEV_OFFSET], HDS_INQ_LDEV_LEN); 316 ctrl = buf[HDS_INQ_CTRL_OFFSET]; 317 port = buf[HDS_INQ_PORT_OFFSET]; 318 319 if (ctrl >= '0' && ctrl <= '9') { 320 printf("%s: ldev %s, controller %c, port %c, %s\n", 321 DEVNAME(sc), ldev, ctrl, port, 322 sc->sc_mode == HDS_SYMMETRIC ? "symmetric" : "asymmetric"); 323 324 sc->sc_ctrl = ctrl; 325 } else 326 error = ENXIO; 327 328 done: 329 dma_free(buf, len); 330 return (error); 331 } 332