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