xref: /openbsd/sys/scsi/mpath_emc.c (revision 1525749f)
1 /*	$OpenBSD: mpath_emc.c,v 1.25 2022/07/02 08:50:42 visa 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 /* EMC CLARiiON AX/CX 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 
32 #include <scsi/scsi_all.h>
33 #include <scsi/scsiconf.h>
34 #include <scsi/mpathvar.h>
35 
36 #define EMC_VPD_SP_INFO			0xc0
37 
38 struct emc_vpd_sp_info {
39 	struct scsi_vpd_hdr	hdr; /* EMC_VPD_SP_INFO */
40 
41 	u_int8_t		lun_state;
42 #define EMC_SP_INFO_LUN_STATE_UNBOUND	0x00
43 #define EMC_SP_INFO_LUN_STATE_BOUND	0x01
44 #define EMC_SP_INFO_LUN_STATE_OWNED	0x02
45 	u_int8_t		default_sp;
46 	u_int8_t		_reserved1[1];
47 	u_int8_t		port;
48 	u_int8_t		current_sp;
49 	u_int8_t		_reserved2[1];
50 	u_int8_t		unique_id[16];
51 	u_int8_t		_reserved3[1];
52 	u_int8_t		type;
53 	u_int8_t		failover_mode;
54 	u_int8_t		_reserved4[21];
55 	u_int8_t		serial[16];
56 } __packed;
57 
58 struct emc_softc {
59 	struct device		sc_dev;
60 	struct mpath_path	sc_path;
61 	struct scsi_xshandler   sc_xsh;
62 	struct emc_vpd_sp_info	*sc_pg;
63 };
64 #define DEVNAME(_s) ((_s)->sc_dev.dv_xname)
65 
66 int		emc_match(struct device *, void *, void *);
67 void		emc_attach(struct device *, struct device *, void *);
68 int		emc_detach(struct device *, int);
69 int		emc_activate(struct device *, int);
70 
71 const struct cfattach emc_ca = {
72 	sizeof(struct emc_softc),
73 	emc_match,
74 	emc_attach,
75 	emc_detach,
76 	emc_activate
77 };
78 
79 struct cfdriver emc_cd = {
80 	NULL,
81 	"emc",
82 	DV_DULL
83 };
84 
85 void		emc_mpath_start(struct scsi_xfer *);
86 int		emc_mpath_checksense(struct scsi_xfer *);
87 void		emc_mpath_status(struct scsi_link *);
88 
89 const struct mpath_ops emc_mpath_ops = {
90 	"emc",
91 	emc_mpath_checksense,
92 	emc_mpath_status
93 };
94 
95 struct emc_device {
96 	char *vendor;
97 	char *product;
98 };
99 
100 void		emc_status(struct scsi_xfer *);
101 void		emc_status_done(struct scsi_xfer *);
102 
103 int		emc_sp_info(struct emc_softc *, int *);
104 
105 struct emc_device emc_devices[] = {
106 /*	  " vendor "  "     device     " */
107 /*	  "01234567"  "0123456789012345" */
108 	{ "DGC     ", "RAID" },
109 	{ "DGC     ", "DISK" },
110 	{ "DGC     ", "VRAID" }
111 };
112 
113 int
emc_match(struct device * parent,void * match,void * aux)114 emc_match(struct device *parent, void *match, void *aux)
115 {
116 	struct scsi_attach_args *sa = aux;
117 	struct scsi_inquiry_data *inq = &sa->sa_sc_link->inqdata;
118 	struct emc_device *s;
119 	int i;
120 
121 	if (mpath_path_probe(sa->sa_sc_link) != 0)
122 		return (0);
123 
124 	for (i = 0; i < nitems(emc_devices); i++) {
125 		s = &emc_devices[i];
126 
127 		if (bcmp(s->vendor, inq->vendor, strlen(s->vendor)) == 0 &&
128 		    bcmp(s->product, inq->product, strlen(s->product)) == 0)
129 			return (8);
130 	}
131 
132 	return (0);
133 }
134 
135 void
emc_attach(struct device * parent,struct device * self,void * aux)136 emc_attach(struct device *parent, struct device *self, void *aux)
137 {
138 	struct emc_softc *sc = (struct emc_softc *)self;
139 	struct scsi_attach_args *sa = aux;
140 	struct scsi_link *link = sa->sa_sc_link;
141 	int sp;
142 
143 	printf("\n");
144 
145 	/* init link */
146 	link->device_softc = sc;
147 
148 	/* init path */
149 	scsi_xsh_set(&sc->sc_path.p_xsh, link, emc_mpath_start);
150 	sc->sc_path.p_link = link;
151 
152 	/* init status handler */
153 	scsi_xsh_set(&sc->sc_xsh, link, emc_status);
154 	sc->sc_pg = dma_alloc(sizeof(*sc->sc_pg), PR_WAITOK);
155 
156 	/* let's go */
157 
158 	if (emc_sp_info(sc, &sp)) {
159 		printf("%s: unable to get sp info\n", DEVNAME(sc));
160 		return;
161 	}
162 
163 	if (mpath_path_attach(&sc->sc_path, sp, &emc_mpath_ops) != 0)
164 		printf("%s: unable to attach path\n", DEVNAME(sc));
165 }
166 
167 int
emc_detach(struct device * self,int flags)168 emc_detach(struct device *self, int flags)
169 {
170 	struct emc_softc *sc = (struct emc_softc *)self;
171 
172 	dma_free(sc->sc_pg, sizeof(*sc->sc_pg));
173 
174 	return (0);
175 }
176 
177 int
emc_activate(struct device * self,int act)178 emc_activate(struct device *self, int act)
179 {
180 	struct emc_softc *sc = (struct emc_softc *)self;
181 
182 	switch (act) {
183 	case DVACT_DEACTIVATE:
184 		if (sc->sc_path.p_group != NULL)
185 			mpath_path_detach(&sc->sc_path);
186 		break;
187 	}
188 	return (0);
189 }
190 
191 void
emc_mpath_start(struct scsi_xfer * xs)192 emc_mpath_start(struct scsi_xfer *xs)
193 {
194 	struct emc_softc *sc = xs->sc_link->device_softc;
195 
196 	mpath_start(&sc->sc_path, xs);
197 }
198 
199 int
emc_mpath_checksense(struct scsi_xfer * xs)200 emc_mpath_checksense(struct scsi_xfer *xs)
201 {
202 	struct scsi_sense_data *sense = &xs->sense;
203 
204 	if ((sense->error_code & SSD_ERRCODE) == SSD_ERRCODE_CURRENT &&
205 	    (sense->flags & SSD_KEY) == SKEY_NOT_READY &&
206 	    ASC_ASCQ(sense) == 0x0403) {
207 		/* Logical Unit Not Ready, Manual Intervention Required */
208 		return (MPATH_SENSE_FAILOVER);
209 	}
210 
211 	return (MPATH_SENSE_DECLINED);
212 }
213 
214 void
emc_mpath_status(struct scsi_link * link)215 emc_mpath_status(struct scsi_link *link)
216 {
217 	struct emc_softc *sc = link->device_softc;
218 
219 	scsi_xsh_add(&sc->sc_xsh);
220 }
221 
222 void
emc_status(struct scsi_xfer * xs)223 emc_status(struct scsi_xfer *xs)
224 {
225 	struct scsi_link *link = xs->sc_link;
226 	struct emc_softc *sc = link->device_softc;
227 
228 	scsi_init_inquiry(xs, SI_EVPD, EMC_VPD_SP_INFO,
229 	    sc->sc_pg, sizeof(*sc->sc_pg));
230 
231 	xs->done = emc_status_done;
232 
233 	scsi_xs_exec(xs);
234 }
235 
236 void
emc_status_done(struct scsi_xfer * xs)237 emc_status_done(struct scsi_xfer *xs)
238 {
239 	struct scsi_link *link = xs->sc_link;
240 	struct emc_softc *sc = link->device_softc;
241 	struct emc_vpd_sp_info *pg = sc->sc_pg;
242 	int status = MPATH_S_UNKNOWN;
243 
244 	if (xs->error == XS_NOERROR) {
245 		status = (pg->lun_state == EMC_SP_INFO_LUN_STATE_OWNED) ?
246 		    MPATH_S_ACTIVE : MPATH_S_PASSIVE;
247 	}
248 
249 	scsi_xs_put(xs);
250 
251 	mpath_path_status(&sc->sc_path, status);
252 }
253 
254 int
emc_sp_info(struct emc_softc * sc,int * sp)255 emc_sp_info(struct emc_softc *sc, int *sp)
256 {
257 	struct emc_vpd_sp_info *pg = sc->sc_pg;
258 	int error;
259 
260 	error = scsi_inquire_vpd(sc->sc_path.p_link, pg, sizeof(*pg),
261 	    EMC_VPD_SP_INFO, scsi_autoconf);
262 	if (error != 0)
263 		return (error);
264 
265 	*sp = pg->current_sp;
266 
267 	printf("%s: SP-%c port %d\n", DEVNAME(sc), pg->current_sp + 'A',
268 	    pg->port);
269 
270 	return (0);
271 }
272