Line data Source code
1 : /* $OpenBSD: ciss.c,v 1.75 2016/08/14 04:08:03 dlg Exp $ */
2 :
3 : /*
4 : * Copyright (c) 2005,2006 Michael Shalayeff
5 : * All rights reserved.
6 : *
7 : * Permission to use, copy, modify, and distribute this software for any
8 : * purpose with or without fee is hereby granted, provided that the above
9 : * copyright notice and this permission notice appear in all copies.
10 : *
11 : * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 : * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 : * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 : * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 : * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
16 : * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 : * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 : */
19 :
20 : #include "bio.h"
21 :
22 : /* #define CISS_DEBUG */
23 :
24 : #include <sys/param.h>
25 : #include <sys/systm.h>
26 : #include <sys/buf.h>
27 : #include <sys/ioctl.h>
28 : #include <sys/device.h>
29 : #include <sys/kernel.h>
30 : #include <sys/malloc.h>
31 :
32 : #include <machine/bus.h>
33 :
34 : #include <scsi/scsi_all.h>
35 : #include <scsi/scsiconf.h>
36 :
37 : #include <dev/ic/cissreg.h>
38 : #include <dev/ic/cissvar.h>
39 :
40 : #if NBIO > 0
41 : #include <dev/biovar.h>
42 : #endif
43 : #include <sys/sensors.h>
44 :
45 : #ifdef CISS_DEBUG
46 : #define CISS_DPRINTF(m,a) if (ciss_debug & (m)) printf a
47 : #define CISS_D_CMD 0x0001
48 : #define CISS_D_INTR 0x0002
49 : #define CISS_D_MISC 0x0004
50 : #define CISS_D_DMA 0x0008
51 : #define CISS_D_IOCTL 0x0010
52 : #define CISS_D_ERR 0x0020
53 : int ciss_debug = 0
54 : /* | CISS_D_CMD */
55 : /* | CISS_D_INTR */
56 : /* | CISS_D_MISC */
57 : /* | CISS_D_DMA */
58 : /* | CISS_D_IOCTL */
59 : /* | CISS_D_ERR */
60 : ;
61 : #else
62 : #define CISS_DPRINTF(m,a) /* m, a */
63 : #endif
64 :
65 : struct cfdriver ciss_cd = {
66 : NULL, "ciss", DV_DULL
67 : };
68 :
69 : void ciss_scsi_cmd(struct scsi_xfer *xs);
70 : int ciss_scsi_ioctl(struct scsi_link *, u_long, caddr_t, int);
71 : void cissminphys(struct buf *bp, struct scsi_link *sl);
72 :
73 : struct scsi_adapter ciss_switch = {
74 : ciss_scsi_cmd, cissminphys, NULL, NULL, ciss_scsi_ioctl
75 : };
76 :
77 : #if NBIO > 0
78 : int ciss_ioctl(struct device *, u_long, caddr_t);
79 : #endif
80 : int ciss_sync(struct ciss_softc *sc);
81 : void ciss_heartbeat(void *v);
82 : #ifndef SMALL_KERNEL
83 : void ciss_sensors(void *);
84 : #endif
85 :
86 : void * ciss_get_ccb(void *);
87 : void ciss_put_ccb(void *, void *);
88 : int ciss_cmd(struct ciss_ccb *ccb, int flags, int wait);
89 : int ciss_done(struct ciss_ccb *ccb);
90 : int ciss_error(struct ciss_ccb *ccb);
91 :
92 : struct ciss_ld *ciss_pdscan(struct ciss_softc *sc, int ld);
93 : int ciss_inq(struct ciss_softc *sc, struct ciss_inquiry *inq);
94 : int ciss_ldmap(struct ciss_softc *sc);
95 : int ciss_ldid(struct ciss_softc *, int, struct ciss_ldid *);
96 : int ciss_ldstat(struct ciss_softc *, int, struct ciss_ldstat *);
97 : int ciss_pdid(struct ciss_softc *, u_int8_t, struct ciss_pdid *, int);
98 : int ciss_blink(struct ciss_softc *, int, int, int, struct ciss_blink *);
99 :
100 : void *
101 0 : ciss_get_ccb(void *xsc)
102 : {
103 0 : struct ciss_softc *sc = xsc;
104 : struct ciss_ccb *ccb;
105 :
106 0 : mtx_enter(&sc->sc_free_ccb_mtx);
107 0 : ccb = SLIST_FIRST(&sc->sc_free_ccb);
108 0 : if (ccb != NULL) {
109 0 : SLIST_REMOVE_HEAD(&sc->sc_free_ccb, ccb_link);
110 0 : ccb->ccb_state = CISS_CCB_READY;
111 0 : ccb->ccb_xs = NULL;
112 0 : }
113 0 : mtx_leave(&sc->sc_free_ccb_mtx);
114 :
115 0 : return (ccb);
116 : }
117 :
118 : void
119 0 : ciss_put_ccb(void *xsc, void *xccb)
120 : {
121 0 : struct ciss_softc *sc = xsc;
122 0 : struct ciss_ccb *ccb = xccb;
123 :
124 0 : ccb->ccb_state = CISS_CCB_FREE;
125 0 : ccb->ccb_xs = NULL;
126 0 : ccb->ccb_data = NULL;
127 :
128 0 : mtx_enter(&sc->sc_free_ccb_mtx);
129 0 : SLIST_INSERT_HEAD(&sc->sc_free_ccb, ccb, ccb_link);
130 0 : mtx_leave(&sc->sc_free_ccb_mtx);
131 0 : }
132 :
133 : int
134 0 : ciss_attach(struct ciss_softc *sc)
135 : {
136 0 : struct scsibus_attach_args saa;
137 : struct scsibus_softc *scsibus;
138 : struct ciss_ccb *ccb;
139 : struct ciss_cmd *cmd;
140 : struct ciss_inquiry *inq;
141 0 : bus_dma_segment_t seg[1];
142 0 : int error, i, total, rseg, maxfer;
143 : ciss_lock_t lock;
144 : paddr_t pa;
145 :
146 0 : bus_space_read_region_4(sc->iot, sc->cfg_ioh, sc->cfgoff,
147 : (u_int32_t *)&sc->cfg, sizeof(sc->cfg) / 4);
148 :
149 0 : if (sc->cfg.signature != CISS_SIGNATURE) {
150 0 : printf(": bad sign 0x%08x\n", sc->cfg.signature);
151 0 : return -1;
152 : }
153 :
154 0 : if (!(sc->cfg.methods & CISS_METH_SIMPL)) {
155 0 : printf(": not simple 0x%08x\n", sc->cfg.methods);
156 0 : return -1;
157 : }
158 :
159 0 : sc->cfg.rmethod = CISS_METH_SIMPL;
160 0 : sc->cfg.paddr_lim = 0; /* 32bit addrs */
161 0 : sc->cfg.int_delay = 0; /* disable coalescing */
162 0 : sc->cfg.int_count = 0;
163 0 : strlcpy(sc->cfg.hostname, "HUMPPA", sizeof(sc->cfg.hostname));
164 0 : sc->cfg.driverf |= CISS_DRV_PRF; /* enable prefetch */
165 0 : if (!sc->cfg.maxsg)
166 0 : sc->cfg.maxsg = MAXPHYS / PAGE_SIZE;
167 :
168 0 : bus_space_write_region_4(sc->iot, sc->cfg_ioh, sc->cfgoff,
169 : (u_int32_t *)&sc->cfg, sizeof(sc->cfg) / 4);
170 0 : bus_space_barrier(sc->iot, sc->cfg_ioh, sc->cfgoff, sizeof(sc->cfg),
171 : BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE);
172 :
173 0 : bus_space_write_4(sc->iot, sc->ioh, CISS_IDB, CISS_IDB_CFG);
174 0 : bus_space_barrier(sc->iot, sc->ioh, CISS_IDB, 4,
175 : BUS_SPACE_BARRIER_WRITE);
176 0 : for (i = 1000; i--; DELAY(1000)) {
177 : /* XXX maybe IDB is really 64bit? - hp dl380 needs this */
178 0 : (void)bus_space_read_4(sc->iot, sc->ioh, CISS_IDB + 4);
179 0 : if (!(bus_space_read_4(sc->iot, sc->ioh, CISS_IDB) & CISS_IDB_CFG))
180 : break;
181 0 : bus_space_barrier(sc->iot, sc->ioh, CISS_IDB, 4,
182 : BUS_SPACE_BARRIER_READ);
183 : }
184 :
185 0 : if (bus_space_read_4(sc->iot, sc->ioh, CISS_IDB) & CISS_IDB_CFG) {
186 0 : printf(": cannot set config\n");
187 0 : return -1;
188 : }
189 :
190 0 : bus_space_read_region_4(sc->iot, sc->cfg_ioh, sc->cfgoff,
191 : (u_int32_t *)&sc->cfg, sizeof(sc->cfg) / 4);
192 :
193 0 : if (!(sc->cfg.amethod & CISS_METH_SIMPL)) {
194 0 : printf(": cannot simplify 0x%08x\n", sc->cfg.amethod);
195 0 : return -1;
196 : }
197 :
198 : /* i'm ready for you and i hope you're ready for me */
199 0 : for (i = 30000; i--; DELAY(1000)) {
200 0 : if (bus_space_read_4(sc->iot, sc->cfg_ioh, sc->cfgoff +
201 0 : offsetof(struct ciss_config, amethod)) & CISS_METH_READY)
202 : break;
203 0 : bus_space_barrier(sc->iot, sc->cfg_ioh, sc->cfgoff +
204 : offsetof(struct ciss_config, amethod), 4,
205 : BUS_SPACE_BARRIER_READ);
206 : }
207 :
208 0 : if (!(bus_space_read_4(sc->iot, sc->cfg_ioh, sc->cfgoff +
209 0 : offsetof(struct ciss_config, amethod)) & CISS_METH_READY)) {
210 0 : printf(": she never came ready for me 0x%08x\n",
211 0 : sc->cfg.amethod);
212 0 : return -1;
213 : }
214 :
215 0 : sc->maxcmd = sc->cfg.maxcmd;
216 0 : sc->maxsg = sc->cfg.maxsg;
217 0 : if (sc->maxsg > MAXPHYS / PAGE_SIZE)
218 0 : sc->maxsg = MAXPHYS / PAGE_SIZE;
219 0 : i = sizeof(struct ciss_ccb) +
220 0 : sizeof(ccb->ccb_cmd.sgl[0]) * (sc->maxsg - 1);
221 0 : for (sc->ccblen = 0x10; sc->ccblen < i; sc->ccblen <<= 1);
222 :
223 0 : total = sc->ccblen * sc->maxcmd;
224 0 : if ((error = bus_dmamem_alloc(sc->dmat, total, PAGE_SIZE, 0,
225 : sc->cmdseg, 1, &rseg, BUS_DMA_NOWAIT | BUS_DMA_ZERO))) {
226 0 : printf(": cannot allocate CCBs (%d)\n", error);
227 0 : return -1;
228 : }
229 :
230 0 : if ((error = bus_dmamem_map(sc->dmat, sc->cmdseg, rseg, total,
231 : (caddr_t *)&sc->ccbs, BUS_DMA_NOWAIT))) {
232 0 : printf(": cannot map CCBs (%d)\n", error);
233 0 : return -1;
234 : }
235 :
236 0 : if ((error = bus_dmamap_create(sc->dmat, total, 1,
237 : total, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &sc->cmdmap))) {
238 0 : printf(": cannot create CCBs dmamap (%d)\n", error);
239 0 : bus_dmamem_free(sc->dmat, sc->cmdseg, 1);
240 0 : return -1;
241 : }
242 :
243 0 : if ((error = bus_dmamap_load(sc->dmat, sc->cmdmap, sc->ccbs, total,
244 : NULL, BUS_DMA_NOWAIT))) {
245 0 : printf(": cannot load CCBs dmamap (%d)\n", error);
246 0 : bus_dmamem_free(sc->dmat, sc->cmdseg, 1);
247 0 : bus_dmamap_destroy(sc->dmat, sc->cmdmap);
248 0 : return -1;
249 : }
250 :
251 0 : SLIST_INIT(&sc->sc_free_ccb);
252 0 : mtx_init(&sc->sc_free_ccb_mtx, IPL_BIO);
253 :
254 0 : maxfer = sc->maxsg * PAGE_SIZE;
255 0 : for (i = 0; total; i++, total -= sc->ccblen) {
256 0 : ccb = sc->ccbs + i * sc->ccblen;
257 0 : cmd = &ccb->ccb_cmd;
258 0 : pa = sc->cmdseg[0].ds_addr + i * sc->ccblen;
259 :
260 0 : ccb->ccb_sc = sc;
261 0 : ccb->ccb_cmdpa = pa + offsetof(struct ciss_ccb, ccb_cmd);
262 0 : ccb->ccb_state = CISS_CCB_FREE;
263 :
264 0 : cmd->id = htole32(i << 2);
265 0 : cmd->id_hi = htole32(0);
266 0 : cmd->sgin = sc->maxsg;
267 0 : cmd->sglen = htole16((u_int16_t)cmd->sgin);
268 0 : cmd->err_len = htole32(sizeof(ccb->ccb_err));
269 0 : pa += offsetof(struct ciss_ccb, ccb_err);
270 0 : cmd->err_pa = htole64((u_int64_t)pa);
271 :
272 0 : if ((error = bus_dmamap_create(sc->dmat, maxfer, sc->maxsg,
273 : maxfer, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
274 : &ccb->ccb_dmamap)))
275 : break;
276 :
277 0 : SLIST_INSERT_HEAD(&sc->sc_free_ccb, ccb, ccb_link);
278 : }
279 :
280 0 : scsi_iopool_init(&sc->sc_iopool, sc, ciss_get_ccb, ciss_put_ccb);
281 :
282 0 : if (i < sc->maxcmd) {
283 0 : printf(": cannot create ccb#%d dmamap (%d)\n", i, error);
284 0 : if (i == 0) {
285 : /* TODO leaking cmd's dmamaps and shitz */
286 0 : bus_dmamem_free(sc->dmat, sc->cmdseg, 1);
287 0 : bus_dmamap_destroy(sc->dmat, sc->cmdmap);
288 0 : return -1;
289 : }
290 : }
291 :
292 0 : if ((error = bus_dmamem_alloc(sc->dmat, PAGE_SIZE, PAGE_SIZE, 0,
293 : seg, 1, &rseg, BUS_DMA_NOWAIT | BUS_DMA_ZERO))) {
294 0 : printf(": cannot allocate scratch buffer (%d)\n", error);
295 0 : return -1;
296 : }
297 :
298 0 : if ((error = bus_dmamem_map(sc->dmat, seg, rseg, PAGE_SIZE,
299 : (caddr_t *)&sc->scratch, BUS_DMA_NOWAIT))) {
300 0 : printf(": cannot map scratch buffer (%d)\n", error);
301 0 : return -1;
302 : }
303 :
304 0 : lock = CISS_LOCK_SCRATCH(sc);
305 0 : inq = sc->scratch;
306 0 : if (ciss_inq(sc, inq)) {
307 0 : printf(": adapter inquiry failed\n");
308 0 : CISS_UNLOCK_SCRATCH(sc, lock);
309 0 : bus_dmamem_free(sc->dmat, sc->cmdseg, 1);
310 0 : bus_dmamap_destroy(sc->dmat, sc->cmdmap);
311 0 : return -1;
312 : }
313 :
314 0 : if (!(inq->flags & CISS_INQ_BIGMAP)) {
315 0 : printf(": big map is not supported, flags=%b\n",
316 : inq->flags, CISS_INQ_BITS);
317 0 : CISS_UNLOCK_SCRATCH(sc, lock);
318 0 : bus_dmamem_free(sc->dmat, sc->cmdseg, 1);
319 0 : bus_dmamap_destroy(sc->dmat, sc->cmdmap);
320 0 : return -1;
321 : }
322 :
323 0 : sc->maxunits = inq->numld;
324 0 : sc->nbus = inq->nscsi_bus;
325 0 : sc->ndrives = inq->buswidth? inq->buswidth : 256;
326 0 : printf(": %d LD%s, HW rev %d, FW %4.4s/%4.4s",
327 0 : inq->numld, inq->numld == 1? "" : "s",
328 0 : inq->hw_rev, inq->fw_running, inq->fw_stored);
329 0 : if (sc->cfg.methods & CISS_METH_FIFO64)
330 0 : printf(", 64bit fifo");
331 0 : else if (sc->cfg.methods & CISS_METH_FIFO64_RRO)
332 0 : printf(", 64bit fifo rro");
333 0 : printf("\n");
334 :
335 0 : CISS_UNLOCK_SCRATCH(sc, lock);
336 :
337 0 : timeout_set(&sc->sc_hb, ciss_heartbeat, sc);
338 0 : timeout_add_sec(&sc->sc_hb, 3);
339 :
340 : /* map LDs */
341 0 : if (ciss_ldmap(sc)) {
342 0 : printf("%s: adapter LD map failed\n", sc->sc_dev.dv_xname);
343 0 : bus_dmamem_free(sc->dmat, sc->cmdseg, 1);
344 0 : bus_dmamap_destroy(sc->dmat, sc->cmdmap);
345 0 : return -1;
346 : }
347 :
348 0 : if (!(sc->sc_lds = mallocarray(sc->maxunits, sizeof(*sc->sc_lds),
349 : M_DEVBUF, M_NOWAIT | M_ZERO))) {
350 0 : bus_dmamem_free(sc->dmat, sc->cmdseg, 1);
351 0 : bus_dmamap_destroy(sc->dmat, sc->cmdmap);
352 0 : return -1;
353 : }
354 :
355 0 : sc->sc_flush = CISS_FLUSH_ENABLE;
356 :
357 0 : sc->sc_link.adapter_softc = sc;
358 0 : sc->sc_link.openings = sc->maxcmd;
359 0 : sc->sc_link.adapter = &ciss_switch;
360 0 : sc->sc_link.luns = 1;
361 0 : sc->sc_link.adapter_target = sc->maxunits;
362 0 : sc->sc_link.adapter_buswidth = sc->maxunits;
363 0 : sc->sc_link.pool = &sc->sc_iopool;
364 0 : bzero(&saa, sizeof(saa));
365 0 : saa.saa_sc_link = &sc->sc_link;
366 0 : scsibus = (struct scsibus_softc *)config_found_sm(&sc->sc_dev,
367 : &saa, scsiprint, NULL);
368 :
369 : #if NBIO > 0
370 : /* XXX for now we can only deal w/ one volume. */
371 0 : if (!scsibus || sc->maxunits > 1)
372 0 : return 0;
373 :
374 : /* now map all the physdevs into their lds */
375 : /* XXX currently we assign all pf 'em into ld#0 */
376 0 : for (i = 0; i < sc->maxunits; i++)
377 0 : if (!(sc->sc_lds[i] = ciss_pdscan(sc, i)))
378 0 : return 0;
379 :
380 0 : if (bio_register(&sc->sc_dev, ciss_ioctl) != 0)
381 0 : printf("%s: controller registration failed",
382 0 : sc->sc_dev.dv_xname);
383 :
384 0 : sc->sc_flags |= CISS_BIO;
385 : #ifndef SMALL_KERNEL
386 0 : sc->sensors = mallocarray(sc->maxunits, sizeof(struct ksensor),
387 : M_DEVBUF, M_NOWAIT | M_ZERO);
388 0 : if (sc->sensors) {
389 : struct device *dev;
390 :
391 0 : strlcpy(sc->sensordev.xname, sc->sc_dev.dv_xname,
392 : sizeof(sc->sensordev.xname));
393 0 : for (i = 0; i < sc->maxunits; i++) {
394 0 : sc->sensors[i].type = SENSOR_DRIVE;
395 0 : sc->sensors[i].status = SENSOR_S_UNKNOWN;
396 0 : dev = scsi_get_link(scsibus, i, 0)->device_softc;
397 0 : strlcpy(sc->sensors[i].desc, dev->dv_xname,
398 : sizeof(sc->sensors[i].desc));
399 0 : strlcpy(sc->sc_lds[i]->xname, dev->dv_xname,
400 : sizeof(sc->sc_lds[i]->xname));
401 0 : sensor_attach(&sc->sensordev, &sc->sensors[i]);
402 : }
403 0 : if (sensor_task_register(sc, ciss_sensors, 10) == NULL)
404 0 : free(sc->sensors, M_DEVBUF,
405 0 : sc->maxunits * sizeof(struct ksensor));
406 : else
407 0 : sensordev_install(&sc->sensordev);
408 0 : }
409 : #endif /* SMALL_KERNEL */
410 : #endif /* BIO > 0 */
411 :
412 0 : return 0;
413 0 : }
414 :
415 : void
416 0 : ciss_shutdown(void *v)
417 : {
418 0 : struct ciss_softc *sc = v;
419 :
420 0 : sc->sc_flush = CISS_FLUSH_DISABLE;
421 0 : timeout_del(&sc->sc_hb);
422 0 : ciss_sync(sc);
423 0 : }
424 :
425 : void
426 0 : cissminphys(struct buf *bp, struct scsi_link *sl)
427 : {
428 : #if 0 /* TODO */
429 : #define CISS_MAXFER (PAGE_SIZE * (sc->maxsg + 1))
430 : if (bp->b_bcount > CISS_MAXFER)
431 : bp->b_bcount = CISS_MAXFER;
432 : #endif
433 0 : minphys(bp);
434 0 : }
435 :
436 : /*
437 : * submit a command and optionally wait for completition.
438 : * wait arg abuses SCSI_POLL|SCSI_NOSLEEP flags to request
439 : * to wait (SCSI_POLL) and to allow tsleep() (!SCSI_NOSLEEP)
440 : * instead of busy loop waiting
441 : */
442 : int
443 0 : ciss_cmd(struct ciss_ccb *ccb, int flags, int wait)
444 : {
445 0 : struct ciss_softc *sc = ccb->ccb_sc;
446 0 : struct ciss_cmd *cmd = &ccb->ccb_cmd;
447 : struct ciss_ccb *ccb1;
448 0 : bus_dmamap_t dmap = ccb->ccb_dmamap;
449 : u_int64_t addr;
450 : u_int32_t id;
451 : int i, tohz, error = 0;
452 :
453 0 : splassert(IPL_BIO);
454 :
455 0 : if (ccb->ccb_state != CISS_CCB_READY) {
456 0 : printf("%s: ccb %d not ready state=%b\n", sc->sc_dev.dv_xname,
457 0 : cmd->id, ccb->ccb_state, CISS_CCB_BITS);
458 0 : return (EINVAL);
459 : }
460 :
461 0 : if (ccb->ccb_data) {
462 : bus_dma_segment_t *sgd;
463 :
464 0 : if ((error = bus_dmamap_load(sc->dmat, dmap, ccb->ccb_data,
465 : ccb->ccb_len, NULL, flags))) {
466 0 : if (error == EFBIG)
467 0 : printf("more than %d dma segs\n", sc->maxsg);
468 : else
469 0 : printf("error %d loading dma map\n", error);
470 0 : if (ccb->ccb_xs) {
471 0 : ccb->ccb_xs->error = XS_DRIVER_STUFFUP;
472 0 : scsi_done(ccb->ccb_xs);
473 0 : ccb->ccb_xs = NULL;
474 0 : }
475 0 : return (error);
476 : }
477 0 : cmd->sgin = dmap->dm_nsegs;
478 :
479 0 : sgd = dmap->dm_segs;
480 : CISS_DPRINTF(CISS_D_DMA, ("data=%p/%u<0x%lx/%u",
481 : ccb->ccb_data, ccb->ccb_len, sgd->ds_addr, sgd->ds_len));
482 :
483 0 : for (i = 0; i < dmap->dm_nsegs; sgd++, i++) {
484 0 : cmd->sgl[i].addr_lo = htole32(sgd->ds_addr);
485 0 : cmd->sgl[i].addr_hi =
486 0 : htole32((u_int64_t)sgd->ds_addr >> 32);
487 0 : cmd->sgl[i].len = htole32(sgd->ds_len);
488 0 : cmd->sgl[i].flags = htole32(0);
489 : if (i)
490 : CISS_DPRINTF(CISS_D_DMA,
491 : (",0x%lx/%u", sgd->ds_addr, sgd->ds_len));
492 : }
493 :
494 : CISS_DPRINTF(CISS_D_DMA, ("> "));
495 :
496 0 : bus_dmamap_sync(sc->dmat, dmap, 0, dmap->dm_mapsize,
497 : BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
498 0 : } else
499 0 : cmd->sgin = 0;
500 0 : cmd->sglen = htole16((u_int16_t)cmd->sgin);
501 0 : bzero(&ccb->ccb_err, sizeof(ccb->ccb_err));
502 :
503 0 : bus_dmamap_sync(sc->dmat, sc->cmdmap, 0, sc->cmdmap->dm_mapsize,
504 : BUS_DMASYNC_PREWRITE);
505 :
506 0 : ccb->ccb_state = CISS_CCB_ONQ;
507 : CISS_DPRINTF(CISS_D_CMD, ("submit=0x%x ", cmd->id));
508 0 : if (sc->cfg.methods & (CISS_METH_FIFO64|CISS_METH_FIFO64_RRO)) {
509 : /*
510 : * Write the upper 32bits immediately before the lower
511 : * 32bits and set bit 63 to indicate 64bit FIFO mode.
512 : */
513 0 : addr = (u_int64_t)ccb->ccb_cmdpa;
514 0 : bus_space_write_4(sc->iot, sc->ioh, CISS_INQ64_HI,
515 : (addr >> 32) | 0x80000000);
516 0 : bus_space_write_4(sc->iot, sc->ioh, CISS_INQ64_LO,
517 : addr & 0x00000000ffffffffULL);
518 0 : } else
519 0 : bus_space_write_4(sc->iot, sc->ioh, CISS_INQ, ccb->ccb_cmdpa);
520 :
521 0 : if (wait & SCSI_POLL) {
522 0 : struct timeval tv;
523 : int etick;
524 : CISS_DPRINTF(CISS_D_CMD, ("waiting "));
525 :
526 0 : i = ccb->ccb_xs? ccb->ccb_xs->timeout : 60000;
527 0 : tv.tv_sec = i / 1000;
528 0 : tv.tv_usec = (i % 1000) * 1000;
529 0 : tohz = tvtohz(&tv);
530 0 : if (tohz == 0)
531 : tohz = 1;
532 0 : for (i *= 100, etick = tick + tohz; i--; ) {
533 0 : if (!(wait & SCSI_NOSLEEP)) {
534 0 : ccb->ccb_state = CISS_CCB_POLL;
535 : CISS_DPRINTF(CISS_D_CMD, ("tsleep(%d) ", tohz));
536 0 : if (tsleep(ccb, PRIBIO + 1, "ciss_cmd",
537 0 : tohz) == EWOULDBLOCK) {
538 : break;
539 : }
540 0 : if (ccb->ccb_state != CISS_CCB_ONQ) {
541 0 : tohz = etick - tick;
542 0 : if (tohz <= 0)
543 : break;
544 : CISS_DPRINTF(CISS_D_CMD, ("T"));
545 0 : continue;
546 : }
547 : ccb1 = ccb;
548 0 : } else {
549 0 : DELAY(10);
550 :
551 0 : if (!(bus_space_read_4(sc->iot, sc->ioh,
552 0 : CISS_ISR) & sc->iem)) {
553 : CISS_DPRINTF(CISS_D_CMD, ("N"));
554 0 : continue;
555 : }
556 :
557 0 : if (sc->cfg.methods & CISS_METH_FIFO64) {
558 0 : if (bus_space_read_4(sc->iot, sc->ioh,
559 0 : CISS_OUTQ64_HI) == 0xffffffff) {
560 : CISS_DPRINTF(CISS_D_CMD, ("Q"));
561 0 : continue;
562 : }
563 0 : id = bus_space_read_4(sc->iot, sc->ioh,
564 : CISS_OUTQ64_LO);
565 0 : } else if (sc->cfg.methods &
566 : CISS_METH_FIFO64_RRO) {
567 0 : id = bus_space_read_4(sc->iot, sc->ioh,
568 : CISS_OUTQ64_LO);
569 0 : if (id == 0xffffffff) {
570 : CISS_DPRINTF(CISS_D_CMD, ("Q"));
571 0 : continue;
572 : }
573 0 : (void)bus_space_read_4(sc->iot,
574 : sc->ioh, CISS_OUTQ64_HI);
575 0 : } else {
576 0 : id = bus_space_read_4(sc->iot, sc->ioh,
577 : CISS_OUTQ);
578 0 : if (id == 0xffffffff) {
579 : CISS_DPRINTF(CISS_D_CMD, ("Q"));
580 0 : continue;
581 : }
582 : }
583 :
584 : CISS_DPRINTF(CISS_D_CMD, ("got=0x%x ", id));
585 0 : ccb1 = sc->ccbs + (id >> 2) * sc->ccblen;
586 0 : ccb1->ccb_cmd.id = htole32(id);
587 0 : ccb1->ccb_cmd.id_hi = htole32(0);
588 : }
589 :
590 0 : error = ciss_done(ccb1);
591 0 : if (ccb1 == ccb)
592 0 : return (error);
593 : }
594 :
595 : /* if never got a chance to be done above... */
596 0 : ccb->ccb_err.cmd_stat = CISS_ERR_TMO;
597 0 : error = ciss_done(ccb);
598 :
599 : CISS_DPRINTF(CISS_D_CMD, ("done %d:%d",
600 : ccb->ccb_err.cmd_stat, ccb->ccb_err.scsi_stat));
601 0 : }
602 :
603 0 : return (error);
604 0 : }
605 :
606 : int
607 0 : ciss_done(struct ciss_ccb *ccb)
608 : {
609 0 : struct ciss_softc *sc = ccb->ccb_sc;
610 0 : struct scsi_xfer *xs = ccb->ccb_xs;
611 0 : struct ciss_cmd *cmd = &ccb->ccb_cmd;
612 : ciss_lock_t lock;
613 : int error = 0;
614 :
615 : CISS_DPRINTF(CISS_D_CMD, ("ciss_done(%p) ", ccb));
616 :
617 0 : if (ccb->ccb_state != CISS_CCB_ONQ) {
618 0 : printf("%s: unqueued ccb %p ready, state=%b\n",
619 0 : sc->sc_dev.dv_xname, ccb, ccb->ccb_state, CISS_CCB_BITS);
620 0 : return 1;
621 : }
622 0 : lock = CISS_LOCK(sc);
623 0 : ccb->ccb_state = CISS_CCB_READY;
624 :
625 0 : if (ccb->ccb_cmd.id & CISS_CMD_ERR)
626 0 : error = ciss_error(ccb);
627 :
628 0 : if (ccb->ccb_data) {
629 0 : bus_dmamap_sync(sc->dmat, ccb->ccb_dmamap, 0,
630 : ccb->ccb_dmamap->dm_mapsize, (cmd->flags & CISS_CDB_IN) ?
631 : BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
632 0 : bus_dmamap_unload(sc->dmat, ccb->ccb_dmamap);
633 0 : }
634 :
635 0 : if (xs) {
636 0 : xs->resid = 0;
637 0 : scsi_done(xs);
638 0 : }
639 :
640 0 : CISS_UNLOCK(sc, lock);
641 :
642 0 : return error;
643 0 : }
644 :
645 : int
646 0 : ciss_error(struct ciss_ccb *ccb)
647 : {
648 0 : struct ciss_softc *sc = ccb->ccb_sc;
649 0 : struct ciss_error *err = &ccb->ccb_err;
650 0 : struct scsi_xfer *xs = ccb->ccb_xs;
651 : int rv;
652 :
653 0 : switch ((rv = letoh16(err->cmd_stat))) {
654 : case CISS_ERR_OK:
655 : rv = 0;
656 0 : break;
657 :
658 : case CISS_ERR_INVCMD:
659 0 : printf("%s: invalid cmd 0x%x: 0x%x is not valid @ 0x%x[%d]\n",
660 0 : sc->sc_dev.dv_xname, ccb->ccb_cmd.id,
661 0 : err->err_info, err->err_type[3], err->err_type[2]);
662 0 : if (xs) {
663 0 : bzero(&xs->sense, sizeof(xs->sense));
664 0 : xs->sense.error_code = SSD_ERRCODE_VALID |
665 : SSD_ERRCODE_CURRENT;
666 0 : xs->sense.flags = SKEY_ILLEGAL_REQUEST;
667 0 : xs->sense.add_sense_code = 0x24; /* ill field */
668 0 : xs->error = XS_SENSE;
669 0 : }
670 : rv = EIO;
671 0 : break;
672 :
673 : case CISS_ERR_TMO:
674 0 : xs->error = XS_TIMEOUT;
675 : rv = ETIMEDOUT;
676 0 : break;
677 :
678 : default:
679 0 : if (xs) {
680 0 : switch (err->scsi_stat) {
681 : case SCSI_CHECK:
682 0 : xs->error = XS_SENSE;
683 0 : bcopy(&err->sense[0], &xs->sense,
684 : sizeof(xs->sense));
685 : rv = EIO;
686 0 : break;
687 :
688 : case SCSI_BUSY:
689 0 : xs->error = XS_BUSY;
690 : rv = EBUSY;
691 0 : break;
692 :
693 : default:
694 : CISS_DPRINTF(CISS_D_ERR, ("%s: "
695 : "cmd_stat %x scsi_stat 0x%x\n",
696 : sc->sc_dev.dv_xname, rv, err->scsi_stat));
697 0 : xs->error = XS_DRIVER_STUFFUP;
698 : rv = EIO;
699 0 : break;
700 : }
701 0 : xs->resid = letoh32(err->resid);
702 0 : } else
703 : rv = EIO;
704 : }
705 0 : ccb->ccb_cmd.id &= htole32(~3);
706 :
707 0 : return rv;
708 : }
709 :
710 : int
711 0 : ciss_inq(struct ciss_softc *sc, struct ciss_inquiry *inq)
712 : {
713 : struct ciss_ccb *ccb;
714 : struct ciss_cmd *cmd;
715 : int rv;
716 : int s;
717 :
718 0 : ccb = scsi_io_get(&sc->sc_iopool, SCSI_POLL|SCSI_NOSLEEP);
719 0 : if (ccb == NULL)
720 0 : return ENOMEM;
721 :
722 0 : ccb->ccb_len = sizeof(*inq);
723 0 : ccb->ccb_data = inq;
724 0 : cmd = &ccb->ccb_cmd;
725 0 : cmd->tgt = htole32(CISS_CMD_MODE_PERIPH);
726 0 : cmd->tgt2 = 0;
727 0 : cmd->cdblen = 10;
728 0 : cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_IN;
729 0 : cmd->tmo = htole16(0);
730 0 : bzero(&cmd->cdb[0], sizeof(cmd->cdb));
731 0 : cmd->cdb[0] = CISS_CMD_CTRL_GET;
732 0 : cmd->cdb[6] = CISS_CMS_CTRL_CTRL;
733 0 : cmd->cdb[7] = sizeof(*inq) >> 8; /* biiiig endian */
734 0 : cmd->cdb[8] = sizeof(*inq) & 0xff;
735 :
736 0 : s = splbio();
737 0 : rv = ciss_cmd(ccb, BUS_DMA_NOWAIT, SCSI_POLL|SCSI_NOSLEEP);
738 0 : splx(s);
739 :
740 0 : scsi_io_put(&sc->sc_iopool, ccb);
741 :
742 0 : return (rv);
743 0 : }
744 :
745 : int
746 0 : ciss_ldmap(struct ciss_softc *sc)
747 : {
748 : struct ciss_ccb *ccb;
749 : struct ciss_cmd *cmd;
750 : struct ciss_ldmap *lmap;
751 : ciss_lock_t lock;
752 : int total, rv;
753 :
754 0 : lock = CISS_LOCK_SCRATCH(sc);
755 0 : lmap = sc->scratch;
756 0 : lmap->size = htobe32(sc->maxunits * sizeof(lmap->map));
757 0 : total = sizeof(*lmap) + (sc->maxunits - 1) * sizeof(lmap->map);
758 :
759 0 : ccb = scsi_io_get(&sc->sc_iopool, SCSI_POLL|SCSI_NOSLEEP);
760 0 : if (ccb == NULL) {
761 0 : CISS_UNLOCK_SCRATCH(sc, lock);
762 0 : return ENOMEM;
763 : }
764 :
765 0 : ccb->ccb_len = total;
766 0 : ccb->ccb_data = lmap;
767 0 : cmd = &ccb->ccb_cmd;
768 0 : cmd->tgt = CISS_CMD_MODE_PERIPH;
769 0 : cmd->tgt2 = 0;
770 0 : cmd->cdblen = 12;
771 0 : cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_IN;
772 0 : cmd->tmo = htole16(30);
773 0 : bzero(&cmd->cdb[0], sizeof(cmd->cdb));
774 0 : cmd->cdb[0] = CISS_CMD_LDMAP;
775 0 : cmd->cdb[8] = total >> 8; /* biiiig endian */
776 0 : cmd->cdb[9] = total & 0xff;
777 :
778 0 : rv = ciss_cmd(ccb, BUS_DMA_NOWAIT, SCSI_POLL|SCSI_NOSLEEP);
779 0 : scsi_io_put(&sc->sc_iopool, ccb);
780 0 : CISS_UNLOCK_SCRATCH(sc, lock);
781 :
782 0 : if (rv)
783 0 : return rv;
784 :
785 : CISS_DPRINTF(CISS_D_MISC, ("lmap %x:%x\n",
786 : lmap->map[0].tgt, lmap->map[0].tgt2));
787 :
788 0 : return 0;
789 0 : }
790 :
791 : int
792 0 : ciss_sync(struct ciss_softc *sc)
793 : {
794 : struct ciss_ccb *ccb;
795 : struct ciss_cmd *cmd;
796 : struct ciss_flush *flush;
797 : ciss_lock_t lock;
798 : int rv;
799 :
800 0 : lock = CISS_LOCK_SCRATCH(sc);
801 0 : flush = sc->scratch;
802 0 : bzero(flush, sizeof(*flush));
803 0 : flush->flush = sc->sc_flush;
804 :
805 0 : ccb = scsi_io_get(&sc->sc_iopool, SCSI_POLL|SCSI_NOSLEEP);
806 0 : if (ccb == NULL) {
807 0 : CISS_UNLOCK_SCRATCH(sc, lock);
808 0 : return ENOMEM;
809 : }
810 :
811 0 : ccb->ccb_len = sizeof(*flush);
812 0 : ccb->ccb_data = flush;
813 0 : cmd = &ccb->ccb_cmd;
814 0 : cmd->tgt = CISS_CMD_MODE_PERIPH;
815 0 : cmd->tgt2 = 0;
816 0 : cmd->cdblen = 10;
817 0 : cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_OUT;
818 0 : cmd->tmo = htole16(0);
819 0 : bzero(&cmd->cdb[0], sizeof(cmd->cdb));
820 0 : cmd->cdb[0] = CISS_CMD_CTRL_SET;
821 0 : cmd->cdb[6] = CISS_CMS_CTRL_FLUSH;
822 0 : cmd->cdb[7] = sizeof(*flush) >> 8; /* biiiig endian */
823 0 : cmd->cdb[8] = sizeof(*flush) & 0xff;
824 :
825 0 : rv = ciss_cmd(ccb, BUS_DMA_NOWAIT, SCSI_POLL|SCSI_NOSLEEP);
826 0 : scsi_io_put(&sc->sc_iopool, ccb);
827 0 : CISS_UNLOCK_SCRATCH(sc, lock);
828 :
829 0 : return rv;
830 0 : }
831 :
832 : void
833 0 : ciss_scsi_cmd(struct scsi_xfer *xs)
834 : {
835 0 : struct scsi_link *link = xs->sc_link;
836 0 : u_int8_t target = link->target;
837 : struct ciss_ccb *ccb;
838 : struct ciss_cmd *cmd;
839 : ciss_lock_t lock;
840 :
841 : CISS_DPRINTF(CISS_D_CMD, ("ciss_scsi_cmd "));
842 :
843 0 : if (xs->cmdlen > CISS_MAX_CDB) {
844 : CISS_DPRINTF(CISS_D_CMD, ("CDB too big %p ", xs));
845 0 : bzero(&xs->sense, sizeof(xs->sense));
846 0 : xs->sense.error_code = SSD_ERRCODE_VALID | SSD_ERRCODE_CURRENT;
847 0 : xs->sense.flags = SKEY_ILLEGAL_REQUEST;
848 0 : xs->sense.add_sense_code = 0x20; /* illcmd, 0x24 illfield */
849 0 : xs->error = XS_SENSE;
850 0 : scsi_done(xs);
851 0 : return;
852 : }
853 :
854 0 : xs->error = XS_NOERROR;
855 :
856 : /* XXX emulate SYNCHRONIZE_CACHE ??? */
857 :
858 0 : ccb = xs->io;
859 :
860 0 : cmd = &ccb->ccb_cmd;
861 0 : ccb->ccb_len = xs->datalen;
862 0 : ccb->ccb_data = xs->data;
863 0 : ccb->ccb_xs = xs;
864 0 : cmd->tgt = CISS_CMD_MODE_LD | target;
865 0 : cmd->tgt2 = 0;
866 0 : cmd->cdblen = xs->cmdlen;
867 0 : cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL;
868 0 : if (xs->flags & SCSI_DATA_IN)
869 0 : cmd->flags |= CISS_CDB_IN;
870 0 : else if (xs->flags & SCSI_DATA_OUT)
871 0 : cmd->flags |= CISS_CDB_OUT;
872 0 : cmd->tmo = htole16(xs->timeout < 1000? 1 : xs->timeout / 1000);
873 0 : bzero(&cmd->cdb[0], sizeof(cmd->cdb));
874 0 : bcopy(xs->cmd, &cmd->cdb[0], CISS_MAX_CDB);
875 :
876 0 : lock = CISS_LOCK(sc);
877 0 : ciss_cmd(ccb, BUS_DMA_WAITOK, xs->flags & (SCSI_POLL|SCSI_NOSLEEP));
878 0 : CISS_UNLOCK(sc, lock);
879 0 : }
880 :
881 : int
882 0 : ciss_intr(void *v)
883 : {
884 0 : struct ciss_softc *sc = v;
885 : struct ciss_ccb *ccb;
886 : bus_size_t reg;
887 : u_int32_t id;
888 : int hit = 0;
889 :
890 : CISS_DPRINTF(CISS_D_INTR, ("intr "));
891 :
892 0 : if (!(bus_space_read_4(sc->iot, sc->ioh, CISS_ISR) & sc->iem))
893 0 : return 0;
894 :
895 0 : if (sc->cfg.methods & CISS_METH_FIFO64)
896 0 : reg = CISS_OUTQ64_HI;
897 0 : else if (sc->cfg.methods & CISS_METH_FIFO64_RRO)
898 0 : reg = CISS_OUTQ64_LO;
899 : else
900 : reg = CISS_OUTQ;
901 0 : while ((id = bus_space_read_4(sc->iot, sc->ioh, reg)) != 0xffffffff) {
902 0 : if (reg == CISS_OUTQ64_HI)
903 0 : id = bus_space_read_4(sc->iot, sc->ioh,
904 : CISS_OUTQ64_LO);
905 0 : else if (reg == CISS_OUTQ64_LO)
906 0 : (void)bus_space_read_4(sc->iot, sc->ioh,
907 : CISS_OUTQ64_HI);
908 0 : ccb = sc->ccbs + (id >> 2) * sc->ccblen;
909 0 : ccb->ccb_cmd.id = htole32(id);
910 0 : ccb->ccb_cmd.id_hi = htole32(0); /* ignore the upper 32bits */
911 0 : if (ccb->ccb_state == CISS_CCB_POLL) {
912 0 : ccb->ccb_state = CISS_CCB_ONQ;
913 0 : wakeup(ccb);
914 0 : } else
915 0 : ciss_done(ccb);
916 :
917 : hit = 1;
918 : }
919 : CISS_DPRINTF(CISS_D_INTR, ("exit "));
920 0 : return hit;
921 0 : }
922 :
923 : void
924 0 : ciss_heartbeat(void *v)
925 : {
926 0 : struct ciss_softc *sc = v;
927 : u_int32_t hb;
928 :
929 0 : hb = bus_space_read_4(sc->iot, sc->cfg_ioh,
930 : sc->cfgoff + offsetof(struct ciss_config, heartbeat));
931 0 : if (hb == sc->heartbeat) {
932 0 : sc->fibrillation++;
933 : CISS_DPRINTF(CISS_D_ERR, ("%s: fibrillation #%d (value=%d)\n",
934 : sc->sc_dev.dv_xname, sc->fibrillation, hb));
935 0 : if (sc->fibrillation >= 11) {
936 : /* No heartbeat for 33 seconds */
937 0 : panic("%s: dead", sc->sc_dev.dv_xname); /* XXX reset! */
938 : }
939 : } else {
940 0 : sc->heartbeat = hb;
941 0 : if (sc->fibrillation) {
942 : CISS_DPRINTF(CISS_D_ERR, ("%s: "
943 : "fibrillation ended (value=%d)\n",
944 : sc->sc_dev.dv_xname, hb));
945 : }
946 0 : sc->fibrillation = 0;
947 : }
948 :
949 0 : timeout_add_sec(&sc->sc_hb, 3);
950 0 : }
951 :
952 : int
953 0 : ciss_scsi_ioctl(struct scsi_link *link, u_long cmd, caddr_t addr, int flag)
954 : {
955 : #if NBIO > 0
956 0 : return ciss_ioctl(link->adapter_softc, cmd, addr);
957 : #else
958 : return ENOTTY;
959 : #endif
960 : }
961 :
962 : #if NBIO > 0
963 : const int ciss_level[] = { 0, 4, 1, 5, 51, 7 };
964 : const int ciss_stat[] = { BIOC_SVONLINE, BIOC_SVOFFLINE, BIOC_SVOFFLINE,
965 : BIOC_SVDEGRADED, BIOC_SVREBUILD, BIOC_SVREBUILD, BIOC_SVDEGRADED,
966 : BIOC_SVDEGRADED, BIOC_SVINVALID, BIOC_SVINVALID, BIOC_SVBUILDING,
967 : BIOC_SVOFFLINE, BIOC_SVBUILDING };
968 :
969 : int
970 0 : ciss_ioctl(struct device *dev, u_long cmd, caddr_t addr)
971 : {
972 0 : struct ciss_softc *sc = (struct ciss_softc *)dev;
973 : struct bioc_inq *bi;
974 : struct bioc_vol *bv;
975 : struct bioc_disk *bd;
976 : struct bioc_blink *bb;
977 : /* struct bioc_alarm *ba; */
978 : /* struct bioc_setstate *bss; */
979 : struct ciss_ldid *ldid;
980 : struct ciss_ldstat *ldstat;
981 : struct ciss_pdid *pdid;
982 : struct ciss_blink *blink;
983 : struct ciss_ld *ldp;
984 : ciss_lock_t lock;
985 : u_int8_t drv;
986 : int ld, pd, error = 0;
987 : u_int blks;
988 :
989 0 : if (!(sc->sc_flags & CISS_BIO))
990 0 : return ENOTTY;
991 :
992 0 : lock = CISS_LOCK(sc);
993 0 : switch (cmd) {
994 : case BIOCINQ:
995 0 : bi = (struct bioc_inq *)addr;
996 0 : strlcpy(bi->bi_dev, sc->sc_dev.dv_xname, sizeof(bi->bi_dev));
997 0 : bi->bi_novol = sc->maxunits;
998 0 : bi->bi_nodisk = sc->ndrives;
999 0 : break;
1000 :
1001 : case BIOCVOL:
1002 0 : bv = (struct bioc_vol *)addr;
1003 0 : if (bv->bv_volid > sc->maxunits) {
1004 : error = EINVAL;
1005 0 : break;
1006 : }
1007 0 : ldp = sc->sc_lds[bv->bv_volid];
1008 0 : if (!ldp) {
1009 : error = EINVAL;
1010 0 : break;
1011 : }
1012 0 : ldid = sc->scratch;
1013 0 : if ((error = ciss_ldid(sc, bv->bv_volid, ldid)))
1014 : break;
1015 : /* params 30:88:ff:00:00:00:00:00:00:00:00:00:00:00:20:00 */
1016 0 : bv->bv_status = BIOC_SVINVALID;
1017 0 : blks = (u_int)letoh16(ldid->nblocks[1]) << 16 |
1018 0 : letoh16(ldid->nblocks[0]);
1019 0 : bv->bv_size = blks * (uint64_t)letoh16(ldid->blksize);
1020 0 : bv->bv_level = ciss_level[ldid->type];
1021 0 : bv->bv_nodisk = ldp->ndrives;
1022 0 : strlcpy(bv->bv_dev, ldp->xname, sizeof(bv->bv_dev));
1023 0 : strlcpy(bv->bv_vendor, "CISS", sizeof(bv->bv_vendor));
1024 0 : ldstat = sc->scratch;
1025 0 : bzero(ldstat, sizeof(*ldstat));
1026 0 : if ((error = ciss_ldstat(sc, bv->bv_volid, ldstat)))
1027 : break;
1028 0 : bv->bv_percent = -1;
1029 0 : bv->bv_seconds = 0;
1030 0 : if (ldstat->stat < nitems(ciss_stat))
1031 0 : bv->bv_status = ciss_stat[ldstat->stat];
1032 0 : if (bv->bv_status == BIOC_SVREBUILD ||
1033 0 : bv->bv_status == BIOC_SVBUILDING)
1034 0 : bv->bv_percent = (blks -
1035 0 : (((u_int)ldstat->prog[3] << 24) |
1036 0 : ((u_int)ldstat->prog[2] << 16) |
1037 0 : ((u_int)ldstat->prog[1] << 8) |
1038 0 : (u_int)ldstat->prog[0])) * 100ULL / blks;
1039 : break;
1040 :
1041 : case BIOCDISK:
1042 0 : bd = (struct bioc_disk *)addr;
1043 0 : if (bd->bd_volid > sc->maxunits) {
1044 : error = EINVAL;
1045 0 : break;
1046 : }
1047 0 : ldp = sc->sc_lds[bd->bd_volid];
1048 0 : if (!ldp || (pd = bd->bd_diskid) > ldp->ndrives) {
1049 : error = EINVAL;
1050 0 : break;
1051 : }
1052 0 : ldstat = sc->scratch;
1053 0 : if ((error = ciss_ldstat(sc, bd->bd_volid, ldstat)))
1054 : break;
1055 0 : bd->bd_status = -1;
1056 0 : if (ldstat->stat == CISS_LD_REBLD &&
1057 0 : ldstat->bigrebuild == ldp->tgts[pd])
1058 0 : bd->bd_status = BIOC_SDREBUILD;
1059 0 : if (ciss_bitset(ldp->tgts[pd] & (~CISS_BIGBIT),
1060 : ldstat->bigfailed)) {
1061 0 : bd->bd_status = BIOC_SDFAILED;
1062 0 : bd->bd_size = 0;
1063 0 : bd->bd_channel = (ldp->tgts[pd] & (~CISS_BIGBIT)) /
1064 0 : sc->ndrives;
1065 0 : bd->bd_target = ldp->tgts[pd] % sc->ndrives;
1066 0 : bd->bd_lun = 0;
1067 0 : bd->bd_vendor[0] = '\0';
1068 0 : bd->bd_serial[0] = '\0';
1069 0 : bd->bd_procdev[0] = '\0';
1070 0 : } else {
1071 0 : pdid = sc->scratch;
1072 0 : if ((error = ciss_pdid(sc, ldp->tgts[pd], pdid,
1073 : SCSI_POLL)))
1074 : break;
1075 0 : if (bd->bd_status < 0) {
1076 0 : if (pdid->config & CISS_PD_SPARE)
1077 0 : bd->bd_status = BIOC_SDHOTSPARE;
1078 0 : else if (pdid->present & CISS_PD_PRESENT)
1079 0 : bd->bd_status = BIOC_SDONLINE;
1080 : else
1081 0 : bd->bd_status = BIOC_SDINVALID;
1082 : }
1083 0 : bd->bd_size = (u_int64_t)letoh32(pdid->nblocks) *
1084 0 : letoh16(pdid->blksz);
1085 0 : bd->bd_channel = pdid->bus;
1086 0 : bd->bd_target = pdid->target;
1087 0 : bd->bd_lun = 0;
1088 0 : strlcpy(bd->bd_vendor, pdid->model,
1089 : sizeof(bd->bd_vendor));
1090 0 : strlcpy(bd->bd_serial, pdid->serial,
1091 : sizeof(bd->bd_serial));
1092 0 : bd->bd_procdev[0] = '\0';
1093 : }
1094 : break;
1095 :
1096 : case BIOCBLINK:
1097 0 : bb = (struct bioc_blink *)addr;
1098 0 : blink = sc->scratch;
1099 : error = EINVAL;
1100 : /* XXX workaround completely dumb scsi addressing */
1101 0 : for (ld = 0; ld < sc->maxunits; ld++) {
1102 0 : ldp = sc->sc_lds[ld];
1103 0 : if (!ldp)
1104 : continue;
1105 0 : if (sc->ndrives == 256)
1106 0 : drv = bb->bb_target;
1107 : else
1108 0 : drv = CISS_BIGBIT +
1109 0 : bb->bb_channel * sc->ndrives +
1110 0 : bb->bb_target;
1111 0 : for (pd = 0; pd < ldp->ndrives; pd++)
1112 0 : if (ldp->tgts[pd] == drv)
1113 0 : error = ciss_blink(sc, ld, pd,
1114 0 : bb->bb_status, blink);
1115 : }
1116 : break;
1117 :
1118 : case BIOCALARM:
1119 : case BIOCSETSTATE:
1120 : default:
1121 : CISS_DPRINTF(CISS_D_IOCTL, ("%s: invalid ioctl\n",
1122 : sc->sc_dev.dv_xname));
1123 : error = ENOTTY;
1124 0 : }
1125 0 : CISS_UNLOCK(sc, lock);
1126 :
1127 0 : return error;
1128 0 : }
1129 :
1130 : #ifndef SMALL_KERNEL
1131 : void
1132 0 : ciss_sensors(void *v)
1133 : {
1134 0 : struct ciss_softc *sc = v;
1135 : struct ciss_ldstat *ldstat;
1136 : int i, error;
1137 :
1138 0 : for (i = 0; i < sc->maxunits; i++) {
1139 0 : ldstat = sc->scratch;
1140 0 : if ((error = ciss_ldstat(sc, i, ldstat))) {
1141 0 : sc->sensors[i].value = 0;
1142 0 : sc->sensors[i].status = SENSOR_S_UNKNOWN;
1143 0 : continue;
1144 : }
1145 :
1146 0 : switch (ldstat->stat) {
1147 : case CISS_LD_OK:
1148 0 : sc->sensors[i].value = SENSOR_DRIVE_ONLINE;
1149 0 : sc->sensors[i].status = SENSOR_S_OK;
1150 0 : break;
1151 :
1152 : case CISS_LD_DEGRAD:
1153 0 : sc->sensors[i].value = SENSOR_DRIVE_PFAIL;
1154 0 : sc->sensors[i].status = SENSOR_S_WARN;
1155 0 : break;
1156 :
1157 : case CISS_LD_EXPND:
1158 : case CISS_LD_QEXPND:
1159 : case CISS_LD_RBLDRD:
1160 : case CISS_LD_REBLD:
1161 0 : sc->sensors[i].value = SENSOR_DRIVE_REBUILD;
1162 0 : sc->sensors[i].status = SENSOR_S_WARN;
1163 0 : break;
1164 :
1165 : case CISS_LD_NORDY:
1166 : case CISS_LD_PDINV:
1167 : case CISS_LD_PDUNC:
1168 : case CISS_LD_FAILED:
1169 : case CISS_LD_UNCONF:
1170 0 : sc->sensors[i].value = SENSOR_DRIVE_FAIL;
1171 0 : sc->sensors[i].status = SENSOR_S_CRIT;
1172 0 : break;
1173 :
1174 : default:
1175 0 : sc->sensors[i].value = 0;
1176 0 : sc->sensors[i].status = SENSOR_S_UNKNOWN;
1177 0 : }
1178 : }
1179 0 : }
1180 : #endif /* SMALL_KERNEL */
1181 :
1182 : int
1183 0 : ciss_ldid(struct ciss_softc *sc, int target, struct ciss_ldid *id)
1184 : {
1185 : struct ciss_ccb *ccb;
1186 : struct ciss_cmd *cmd;
1187 : int rv;
1188 : int s;
1189 :
1190 0 : ccb = scsi_io_get(&sc->sc_iopool, SCSI_POLL);
1191 0 : if (ccb == NULL)
1192 0 : return ENOMEM;
1193 :
1194 0 : ccb->ccb_len = sizeof(*id);
1195 0 : ccb->ccb_data = id;
1196 0 : cmd = &ccb->ccb_cmd;
1197 0 : cmd->tgt = htole32(CISS_CMD_MODE_PERIPH);
1198 0 : cmd->tgt2 = 0;
1199 0 : cmd->cdblen = 10;
1200 0 : cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_IN;
1201 0 : cmd->tmo = htole16(0);
1202 0 : bzero(&cmd->cdb[0], sizeof(cmd->cdb));
1203 0 : cmd->cdb[0] = CISS_CMD_CTRL_GET;
1204 0 : cmd->cdb[5] = target;
1205 0 : cmd->cdb[6] = CISS_CMS_CTRL_LDIDEXT;
1206 0 : cmd->cdb[7] = sizeof(*id) >> 8; /* biiiig endian */
1207 0 : cmd->cdb[8] = sizeof(*id) & 0xff;
1208 :
1209 0 : s = splbio();
1210 0 : rv = ciss_cmd(ccb, BUS_DMA_NOWAIT, SCSI_POLL);
1211 0 : splx(s);
1212 :
1213 0 : scsi_io_put(&sc->sc_iopool, ccb);
1214 :
1215 0 : return (rv);
1216 0 : }
1217 :
1218 : int
1219 0 : ciss_ldstat(struct ciss_softc *sc, int target, struct ciss_ldstat *stat)
1220 : {
1221 : struct ciss_ccb *ccb;
1222 : struct ciss_cmd *cmd;
1223 : int rv;
1224 : int s;
1225 :
1226 0 : ccb = scsi_io_get(&sc->sc_iopool, SCSI_POLL);
1227 0 : if (ccb == NULL)
1228 0 : return ENOMEM;
1229 :
1230 0 : ccb->ccb_len = sizeof(*stat);
1231 0 : ccb->ccb_data = stat;
1232 0 : cmd = &ccb->ccb_cmd;
1233 0 : cmd->tgt = htole32(CISS_CMD_MODE_PERIPH);
1234 0 : cmd->tgt2 = 0;
1235 0 : cmd->cdblen = 10;
1236 0 : cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_IN;
1237 0 : cmd->tmo = htole16(0);
1238 0 : bzero(&cmd->cdb[0], sizeof(cmd->cdb));
1239 0 : cmd->cdb[0] = CISS_CMD_CTRL_GET;
1240 0 : cmd->cdb[5] = target;
1241 0 : cmd->cdb[6] = CISS_CMS_CTRL_LDSTAT;
1242 0 : cmd->cdb[7] = sizeof(*stat) >> 8; /* biiiig endian */
1243 0 : cmd->cdb[8] = sizeof(*stat) & 0xff;
1244 :
1245 0 : s = splbio();
1246 0 : rv = ciss_cmd(ccb, BUS_DMA_NOWAIT, SCSI_POLL);
1247 0 : splx(s);
1248 :
1249 0 : scsi_io_put(&sc->sc_iopool, ccb);
1250 :
1251 0 : return (rv);
1252 0 : }
1253 :
1254 : int
1255 0 : ciss_pdid(struct ciss_softc *sc, u_int8_t drv, struct ciss_pdid *id, int wait)
1256 : {
1257 : struct ciss_ccb *ccb;
1258 : struct ciss_cmd *cmd;
1259 : int rv;
1260 : int s;
1261 :
1262 0 : ccb = scsi_io_get(&sc->sc_iopool, SCSI_POLL);
1263 0 : if (ccb == NULL)
1264 0 : return ENOMEM;
1265 :
1266 0 : ccb->ccb_len = sizeof(*id);
1267 0 : ccb->ccb_data = id;
1268 0 : cmd = &ccb->ccb_cmd;
1269 0 : cmd->tgt = htole32(CISS_CMD_MODE_PERIPH);
1270 0 : cmd->tgt2 = 0;
1271 0 : cmd->cdblen = 10;
1272 0 : cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_IN;
1273 0 : cmd->tmo = htole16(0);
1274 0 : bzero(&cmd->cdb[0], sizeof(cmd->cdb));
1275 0 : cmd->cdb[0] = CISS_CMD_CTRL_GET;
1276 0 : cmd->cdb[2] = drv;
1277 0 : cmd->cdb[6] = CISS_CMS_CTRL_PDID;
1278 0 : cmd->cdb[7] = sizeof(*id) >> 8; /* biiiig endian */
1279 0 : cmd->cdb[8] = sizeof(*id) & 0xff;
1280 :
1281 0 : s = splbio();
1282 0 : rv = ciss_cmd(ccb, BUS_DMA_NOWAIT, wait);
1283 0 : splx(s);
1284 :
1285 0 : scsi_io_put(&sc->sc_iopool, ccb);
1286 :
1287 0 : return (rv);
1288 0 : }
1289 :
1290 :
1291 : struct ciss_ld *
1292 0 : ciss_pdscan(struct ciss_softc *sc, int ld)
1293 : {
1294 : struct ciss_pdid *pdid;
1295 : struct ciss_ld *ldp;
1296 0 : u_int8_t drv, buf[128];
1297 : int i, j, k = 0;
1298 :
1299 0 : pdid = sc->scratch;
1300 0 : if (sc->ndrives == 256) {
1301 0 : for (i = 0; i < CISS_BIGBIT; i++)
1302 0 : if (!ciss_pdid(sc, i, pdid, SCSI_NOSLEEP|SCSI_POLL) &&
1303 0 : (pdid->present & CISS_PD_PRESENT))
1304 0 : buf[k++] = i;
1305 : } else
1306 0 : for (i = 0; i < sc->nbus; i++)
1307 0 : for (j = 0; j < sc->ndrives; j++) {
1308 0 : drv = CISS_BIGBIT + i * sc->ndrives + j;
1309 0 : if (!ciss_pdid(sc, drv, pdid,
1310 : SCSI_NOSLEEP|SCSI_POLL))
1311 0 : buf[k++] = drv;
1312 : }
1313 :
1314 0 : if (!k)
1315 0 : return NULL;
1316 :
1317 0 : ldp = malloc(sizeof(*ldp) + (k-1), M_DEVBUF, M_NOWAIT);
1318 0 : if (!ldp)
1319 0 : return NULL;
1320 :
1321 0 : bzero(&ldp->bling, sizeof(ldp->bling));
1322 0 : ldp->ndrives = k;
1323 0 : bcopy(buf, ldp->tgts, k);
1324 0 : return ldp;
1325 0 : }
1326 :
1327 : int
1328 0 : ciss_blink(struct ciss_softc *sc, int ld, int pd, int stat,
1329 : struct ciss_blink *blink)
1330 : {
1331 : struct ciss_ccb *ccb;
1332 : struct ciss_cmd *cmd;
1333 : struct ciss_ld *ldp;
1334 : int rv;
1335 : int s;
1336 :
1337 0 : if (ld > sc->maxunits)
1338 0 : return EINVAL;
1339 :
1340 0 : ldp = sc->sc_lds[ld];
1341 0 : if (!ldp || pd > ldp->ndrives)
1342 0 : return EINVAL;
1343 :
1344 0 : ldp->bling.pdtab[ldp->tgts[pd]] = stat == BIOC_SBUNBLINK? 0 :
1345 : CISS_BLINK_ALL;
1346 0 : bcopy(&ldp->bling, blink, sizeof(*blink));
1347 :
1348 0 : ccb = scsi_io_get(&sc->sc_iopool, SCSI_POLL);
1349 0 : if (ccb == NULL)
1350 0 : return ENOMEM;
1351 :
1352 0 : ccb->ccb_len = sizeof(*blink);
1353 0 : ccb->ccb_data = blink;
1354 0 : cmd = &ccb->ccb_cmd;
1355 0 : cmd->tgt = htole32(CISS_CMD_MODE_PERIPH);
1356 0 : cmd->tgt2 = 0;
1357 0 : cmd->cdblen = 10;
1358 0 : cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_OUT;
1359 0 : cmd->tmo = htole16(0);
1360 0 : bzero(&cmd->cdb[0], sizeof(cmd->cdb));
1361 0 : cmd->cdb[0] = CISS_CMD_CTRL_SET;
1362 0 : cmd->cdb[6] = CISS_CMS_CTRL_PDBLINK;
1363 0 : cmd->cdb[7] = sizeof(*blink) >> 8; /* biiiig endian */
1364 0 : cmd->cdb[8] = sizeof(*blink) & 0xff;
1365 :
1366 0 : s = splbio();
1367 0 : rv = ciss_cmd(ccb, BUS_DMA_NOWAIT, SCSI_POLL);
1368 0 : splx(s);
1369 :
1370 0 : scsi_io_put(&sc->sc_iopool, ccb);
1371 :
1372 0 : return (rv);
1373 0 : }
1374 : #endif
|