ciss.c revision 1.1.4.2 1 1.1.4.2 tron /* $NetBSD: ciss.c,v 1.1.4.2 2006/04/08 23:31:38 tron Exp $ */
2 1.1.4.2 tron /* $OpenBSD: ciss.c,v 1.13 2006/02/02 22:13:04 brad Exp $ */
3 1.1.4.2 tron
4 1.1.4.2 tron /*
5 1.1.4.2 tron * Copyright (c) 2005 Michael Shalayeff
6 1.1.4.2 tron * All rights reserved.
7 1.1.4.2 tron *
8 1.1.4.2 tron * Permission to use, copy, modify, and distribute this software for any
9 1.1.4.2 tron * purpose with or without fee is hereby granted, provided that the above
10 1.1.4.2 tron * copyright notice and this permission notice appear in all copies.
11 1.1.4.2 tron *
12 1.1.4.2 tron * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 1.1.4.2 tron * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 1.1.4.2 tron * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 1.1.4.2 tron * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 1.1.4.2 tron * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
17 1.1.4.2 tron * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
18 1.1.4.2 tron * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 1.1.4.2 tron */
20 1.1.4.2 tron
21 1.1.4.2 tron #include <sys/cdefs.h>
22 1.1.4.2 tron __KERNEL_RCSID(0, "$NetBSD: ciss.c,v 1.1.4.2 2006/04/08 23:31:38 tron Exp $");
23 1.1.4.2 tron
24 1.1.4.2 tron /* #define CISS_DEBUG */
25 1.1.4.2 tron
26 1.1.4.2 tron #include <sys/param.h>
27 1.1.4.2 tron #include <sys/systm.h>
28 1.1.4.2 tron #include <sys/buf.h>
29 1.1.4.2 tron #include <sys/ioctl.h>
30 1.1.4.2 tron #include <sys/device.h>
31 1.1.4.2 tron #include <sys/kernel.h>
32 1.1.4.2 tron #include <sys/malloc.h>
33 1.1.4.2 tron #include <sys/proc.h>
34 1.1.4.2 tron #include <sys/kthread.h>
35 1.1.4.2 tron
36 1.1.4.2 tron #include <uvm/uvm_extern.h>
37 1.1.4.2 tron
38 1.1.4.2 tron #include <machine/bus.h>
39 1.1.4.2 tron
40 1.1.4.2 tron #include <dev/scsipi/scsi_all.h>
41 1.1.4.2 tron #include <dev/scsipi/scsi_disk.h>
42 1.1.4.2 tron #include <dev/scsipi/scsiconf.h>
43 1.1.4.2 tron
44 1.1.4.2 tron #include <dev/ic/cissreg.h>
45 1.1.4.2 tron #include <dev/ic/cissvar.h>
46 1.1.4.2 tron
47 1.1.4.2 tron #ifdef CISS_DEBUG
48 1.1.4.2 tron #define CISS_DPRINTF(m,a) if (ciss_debug & (m)) printf a
49 1.1.4.2 tron #define CISS_D_CMD 0x0001
50 1.1.4.2 tron #define CISS_D_INTR 0x0002
51 1.1.4.2 tron #define CISS_D_MISC 0x0004
52 1.1.4.2 tron #define CISS_D_DMA 0x0008
53 1.1.4.2 tron #define CISS_D_IOCTL 0x0010
54 1.1.4.2 tron #define CISS_D_ERR 0x0020
55 1.1.4.2 tron int ciss_debug = 0
56 1.1.4.2 tron | CISS_D_CMD
57 1.1.4.2 tron | CISS_D_INTR
58 1.1.4.2 tron | CISS_D_MISC
59 1.1.4.2 tron | CISS_D_DMA
60 1.1.4.2 tron | CISS_D_IOCTL
61 1.1.4.2 tron | CISS_D_ERR
62 1.1.4.2 tron ;
63 1.1.4.2 tron #else
64 1.1.4.2 tron #define CISS_DPRINTF(m,a) /* m, a */
65 1.1.4.2 tron #endif
66 1.1.4.2 tron
67 1.1.4.2 tron static void ciss_scsi_cmd(struct scsipi_channel *chan,
68 1.1.4.2 tron scsipi_adapter_req_t req, void *arg);
69 1.1.4.2 tron static int ciss_scsi_ioctl(struct scsipi_channel *chan, u_long cmd,
70 1.1.4.2 tron caddr_t addr, int flag, struct proc *p);
71 1.1.4.2 tron static void cissminphys(struct buf *bp);
72 1.1.4.2 tron
73 1.1.4.2 tron #if 0
74 1.1.4.2 tron static void ciss_scsi_raw_cmd(struct scsipi_channel *chan,
75 1.1.4.2 tron scsipi_adapter_req_t req, void *arg);
76 1.1.4.2 tron #endif
77 1.1.4.2 tron
78 1.1.4.2 tron #if NBIO > 0
79 1.1.4.2 tron static int ciss_ioctl(struct device *, u_long, caddr_t);
80 1.1.4.2 tron #endif
81 1.1.4.2 tron static int ciss_sync(struct ciss_softc *sc);
82 1.1.4.2 tron static void ciss_heartbeat(void *v);
83 1.1.4.2 tron static void ciss_shutdown(void *v);
84 1.1.4.2 tron #if 0
85 1.1.4.2 tron static void ciss_kthread(void *v);
86 1.1.4.2 tron #endif
87 1.1.4.2 tron
88 1.1.4.2 tron static struct ciss_ccb *ciss_get_ccb(struct ciss_softc *sc);
89 1.1.4.2 tron static void ciss_put_ccb(struct ciss_ccb *ccb);
90 1.1.4.2 tron static int ciss_cmd(struct ciss_ccb *ccb, int flags, int wait);
91 1.1.4.2 tron static int ciss_done(struct ciss_ccb *ccb);
92 1.1.4.2 tron static int ciss_error(struct ciss_ccb *ccb);
93 1.1.4.2 tron static int ciss_inq(struct ciss_softc *sc, struct ciss_inquiry *inq);
94 1.1.4.2 tron static int ciss_ldmap(struct ciss_softc *sc);
95 1.1.4.2 tron
96 1.1.4.2 tron static struct ciss_ccb *
97 1.1.4.2 tron ciss_get_ccb(struct ciss_softc *sc)
98 1.1.4.2 tron {
99 1.1.4.2 tron struct ciss_ccb *ccb;
100 1.1.4.2 tron
101 1.1.4.2 tron if ((ccb = TAILQ_LAST(&sc->sc_free_ccb, ciss_queue_head))) {
102 1.1.4.2 tron TAILQ_REMOVE(&sc->sc_free_ccb, ccb, ccb_link);
103 1.1.4.2 tron ccb->ccb_state = CISS_CCB_READY;
104 1.1.4.2 tron }
105 1.1.4.2 tron return ccb;
106 1.1.4.2 tron }
107 1.1.4.2 tron
108 1.1.4.2 tron static void
109 1.1.4.2 tron ciss_put_ccb(struct ciss_ccb *ccb)
110 1.1.4.2 tron {
111 1.1.4.2 tron struct ciss_softc *sc = ccb->ccb_sc;
112 1.1.4.2 tron
113 1.1.4.2 tron ccb->ccb_state = CISS_CCB_FREE;
114 1.1.4.2 tron TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, ccb_link);
115 1.1.4.2 tron }
116 1.1.4.2 tron
117 1.1.4.2 tron int
118 1.1.4.2 tron ciss_attach(struct ciss_softc *sc)
119 1.1.4.2 tron {
120 1.1.4.2 tron struct ciss_ccb *ccb;
121 1.1.4.2 tron struct ciss_cmd *cmd;
122 1.1.4.2 tron struct ciss_inquiry *inq;
123 1.1.4.2 tron bus_dma_segment_t seg[1];
124 1.1.4.2 tron int error, i, total, rseg, maxfer;
125 1.1.4.2 tron ciss_lock_t lock;
126 1.1.4.2 tron paddr_t pa;
127 1.1.4.2 tron
128 1.1.4.2 tron bus_space_read_region_4(sc->sc_iot, sc->cfg_ioh, sc->cfgoff,
129 1.1.4.2 tron (u_int32_t *)&sc->cfg, sizeof(sc->cfg) / 4);
130 1.1.4.2 tron
131 1.1.4.2 tron if (sc->cfg.signature != CISS_SIGNATURE) {
132 1.1.4.2 tron printf(": bad sign 0x%08x\n", sc->cfg.signature);
133 1.1.4.2 tron return -1;
134 1.1.4.2 tron }
135 1.1.4.2 tron
136 1.1.4.2 tron if (!(sc->cfg.methods & CISS_METH_SIMPL)) {
137 1.1.4.2 tron printf(": not simple 0x%08x\n", sc->cfg.methods);
138 1.1.4.2 tron return -1;
139 1.1.4.2 tron }
140 1.1.4.2 tron
141 1.1.4.2 tron sc->cfg.rmethod = CISS_METH_SIMPL;
142 1.1.4.2 tron sc->cfg.paddr_lim = 0; /* 32bit addrs */
143 1.1.4.2 tron sc->cfg.int_delay = 0; /* disable coalescing */
144 1.1.4.2 tron sc->cfg.int_count = 0;
145 1.1.4.2 tron strlcpy(sc->cfg.hostname, "HUMPPA", sizeof(sc->cfg.hostname));
146 1.1.4.2 tron sc->cfg.driverf |= CISS_DRV_PRF; /* enable prefetch */
147 1.1.4.2 tron if (!sc->cfg.maxsg)
148 1.1.4.2 tron sc->cfg.maxsg = MAXPHYS / PAGE_SIZE + 1;
149 1.1.4.2 tron
150 1.1.4.2 tron bus_space_write_region_4(sc->sc_iot, sc->cfg_ioh, sc->cfgoff,
151 1.1.4.2 tron (u_int32_t *)&sc->cfg, sizeof(sc->cfg) / 4);
152 1.1.4.2 tron bus_space_barrier(sc->sc_iot, sc->cfg_ioh, sc->cfgoff, sizeof(sc->cfg),
153 1.1.4.2 tron BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE);
154 1.1.4.2 tron
155 1.1.4.2 tron bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_IDB, CISS_IDB_CFG);
156 1.1.4.2 tron bus_space_barrier(sc->sc_iot, sc->sc_ioh, CISS_IDB, 4,
157 1.1.4.2 tron BUS_SPACE_BARRIER_WRITE);
158 1.1.4.2 tron for (i = 1000; i--; DELAY(1000)) {
159 1.1.4.2 tron /* XXX maybe IDB is really 64bit? - hp dl380 needs this */
160 1.1.4.2 tron (void)bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IDB + 4);
161 1.1.4.2 tron if (!(bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IDB) & CISS_IDB_CFG))
162 1.1.4.2 tron break;
163 1.1.4.2 tron bus_space_barrier(sc->sc_iot, sc->sc_ioh, CISS_IDB, 4,
164 1.1.4.2 tron BUS_SPACE_BARRIER_READ);
165 1.1.4.2 tron }
166 1.1.4.2 tron
167 1.1.4.2 tron if (bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IDB) & CISS_IDB_CFG) {
168 1.1.4.2 tron printf(": cannot set config\n");
169 1.1.4.2 tron return -1;
170 1.1.4.2 tron }
171 1.1.4.2 tron
172 1.1.4.2 tron bus_space_read_region_4(sc->sc_iot, sc->cfg_ioh, sc->cfgoff,
173 1.1.4.2 tron (u_int32_t *)&sc->cfg, sizeof(sc->cfg) / 4);
174 1.1.4.2 tron
175 1.1.4.2 tron if (!(sc->cfg.amethod & CISS_METH_SIMPL)) {
176 1.1.4.2 tron printf(": cannot simplify 0x%08x\n", sc->cfg.amethod);
177 1.1.4.2 tron return -1;
178 1.1.4.2 tron }
179 1.1.4.2 tron
180 1.1.4.2 tron /* i'm ready for you and i hope you're ready for me */
181 1.1.4.2 tron for (i = 30000; i--; DELAY(1000)) {
182 1.1.4.2 tron if (bus_space_read_4(sc->sc_iot, sc->cfg_ioh, sc->cfgoff +
183 1.1.4.2 tron offsetof(struct ciss_config, amethod)) & CISS_METH_READY)
184 1.1.4.2 tron break;
185 1.1.4.2 tron bus_space_barrier(sc->sc_iot, sc->cfg_ioh, sc->cfgoff +
186 1.1.4.2 tron offsetof(struct ciss_config, amethod), 4,
187 1.1.4.2 tron BUS_SPACE_BARRIER_READ);
188 1.1.4.2 tron }
189 1.1.4.2 tron
190 1.1.4.2 tron if (!(bus_space_read_4(sc->sc_iot, sc->cfg_ioh, sc->cfgoff +
191 1.1.4.2 tron offsetof(struct ciss_config, amethod)) & CISS_METH_READY)) {
192 1.1.4.2 tron printf(": she never came ready for me 0x%08x\n",
193 1.1.4.2 tron sc->cfg.amethod);
194 1.1.4.2 tron return -1;
195 1.1.4.2 tron }
196 1.1.4.2 tron
197 1.1.4.2 tron sc->maxcmd = sc->cfg.maxcmd;
198 1.1.4.2 tron sc->maxsg = sc->cfg.maxsg;
199 1.1.4.2 tron if (sc->maxsg > MAXPHYS / PAGE_SIZE + 1)
200 1.1.4.2 tron sc->maxsg = MAXPHYS / PAGE_SIZE + 1;
201 1.1.4.2 tron i = sizeof(struct ciss_ccb) +
202 1.1.4.2 tron sizeof(ccb->ccb_cmd.sgl[0]) * (sc->maxsg - 1);
203 1.1.4.2 tron for (sc->ccblen = 0x10; sc->ccblen < i; sc->ccblen <<= 1);
204 1.1.4.2 tron
205 1.1.4.2 tron total = sc->ccblen * sc->maxcmd;
206 1.1.4.2 tron if ((error = bus_dmamem_alloc(sc->sc_dmat, total, PAGE_SIZE, 0,
207 1.1.4.2 tron sc->cmdseg, 1, &rseg, BUS_DMA_NOWAIT))) {
208 1.1.4.2 tron printf(": cannot allocate CCBs (%d)\n", error);
209 1.1.4.2 tron return -1;
210 1.1.4.2 tron }
211 1.1.4.2 tron
212 1.1.4.2 tron if ((error = bus_dmamem_map(sc->sc_dmat, sc->cmdseg, rseg, total,
213 1.1.4.2 tron (caddr_t *)&sc->ccbs, BUS_DMA_NOWAIT))) {
214 1.1.4.2 tron printf(": cannot map CCBs (%d)\n", error);
215 1.1.4.2 tron return -1;
216 1.1.4.2 tron }
217 1.1.4.2 tron bzero(sc->ccbs, total);
218 1.1.4.2 tron
219 1.1.4.2 tron if ((error = bus_dmamap_create(sc->sc_dmat, total, 1,
220 1.1.4.2 tron total, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &sc->cmdmap))) {
221 1.1.4.2 tron printf(": cannot create CCBs dmamap (%d)\n", error);
222 1.1.4.2 tron bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
223 1.1.4.2 tron return -1;
224 1.1.4.2 tron }
225 1.1.4.2 tron
226 1.1.4.2 tron if ((error = bus_dmamap_load(sc->sc_dmat, sc->cmdmap, sc->ccbs, total,
227 1.1.4.2 tron NULL, BUS_DMA_NOWAIT))) {
228 1.1.4.2 tron printf(": cannot load CCBs dmamap (%d)\n", error);
229 1.1.4.2 tron bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
230 1.1.4.2 tron bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
231 1.1.4.2 tron return -1;
232 1.1.4.2 tron }
233 1.1.4.2 tron
234 1.1.4.2 tron TAILQ_INIT(&sc->sc_ccbq);
235 1.1.4.2 tron TAILQ_INIT(&sc->sc_ccbdone);
236 1.1.4.2 tron TAILQ_INIT(&sc->sc_free_ccb);
237 1.1.4.2 tron
238 1.1.4.2 tron maxfer = sc->maxsg * PAGE_SIZE;
239 1.1.4.2 tron for (i = 0; total > 0 && i < sc->maxcmd; i++, total -= sc->ccblen) {
240 1.1.4.2 tron ccb = (struct ciss_ccb *) (sc->ccbs + i * sc->ccblen);
241 1.1.4.2 tron cmd = &ccb->ccb_cmd;
242 1.1.4.2 tron pa = sc->cmdseg[0].ds_addr + i * sc->ccblen;
243 1.1.4.2 tron
244 1.1.4.2 tron ccb->ccb_sc = sc;
245 1.1.4.2 tron ccb->ccb_cmdpa = pa + offsetof(struct ciss_ccb, ccb_cmd);
246 1.1.4.2 tron ccb->ccb_state = CISS_CCB_FREE;
247 1.1.4.2 tron
248 1.1.4.2 tron cmd->id = htole32(i << 2);
249 1.1.4.2 tron cmd->id_hi = htole32(0);
250 1.1.4.2 tron cmd->sgin = sc->maxsg;
251 1.1.4.2 tron cmd->sglen = htole16((u_int16_t)cmd->sgin);
252 1.1.4.2 tron cmd->err_len = htole32(sizeof(ccb->ccb_err));
253 1.1.4.2 tron pa += offsetof(struct ciss_ccb, ccb_err);
254 1.1.4.2 tron cmd->err_pa = htole64((u_int64_t)pa);
255 1.1.4.2 tron
256 1.1.4.2 tron if ((error = bus_dmamap_create(sc->sc_dmat, maxfer, sc->maxsg,
257 1.1.4.2 tron maxfer, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
258 1.1.4.2 tron &ccb->ccb_dmamap)))
259 1.1.4.2 tron break;
260 1.1.4.2 tron
261 1.1.4.2 tron TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, ccb_link);
262 1.1.4.2 tron }
263 1.1.4.2 tron
264 1.1.4.2 tron if (i < sc->maxcmd) {
265 1.1.4.2 tron printf(": cannot create ccb#%d dmamap (%d)\n", i, error);
266 1.1.4.2 tron if (i == 0) {
267 1.1.4.2 tron /* TODO leaking cmd's dmamaps and shitz */
268 1.1.4.2 tron bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
269 1.1.4.2 tron bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
270 1.1.4.2 tron return -1;
271 1.1.4.2 tron }
272 1.1.4.2 tron }
273 1.1.4.2 tron
274 1.1.4.2 tron if ((error = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE, 0,
275 1.1.4.2 tron seg, 1, &rseg, BUS_DMA_NOWAIT))) {
276 1.1.4.2 tron printf(": cannot allocate scratch buffer (%d)\n", error);
277 1.1.4.2 tron return -1;
278 1.1.4.2 tron }
279 1.1.4.2 tron
280 1.1.4.2 tron if ((error = bus_dmamem_map(sc->sc_dmat, seg, rseg, PAGE_SIZE,
281 1.1.4.2 tron (caddr_t *)&sc->scratch, BUS_DMA_NOWAIT))) {
282 1.1.4.2 tron printf(": cannot map scratch buffer (%d)\n", error);
283 1.1.4.2 tron return -1;
284 1.1.4.2 tron }
285 1.1.4.2 tron bzero(sc->scratch, PAGE_SIZE);
286 1.1.4.2 tron
287 1.1.4.2 tron lock = CISS_LOCK_SCRATCH(sc);
288 1.1.4.2 tron inq = sc->scratch;
289 1.1.4.2 tron if (ciss_inq(sc, inq)) {
290 1.1.4.2 tron printf(": adapter inquiry failed\n");
291 1.1.4.2 tron CISS_UNLOCK_SCRATCH(sc, lock);
292 1.1.4.2 tron bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
293 1.1.4.2 tron bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
294 1.1.4.2 tron return -1;
295 1.1.4.2 tron }
296 1.1.4.2 tron
297 1.1.4.2 tron if (!(inq->flags & CISS_INQ_BIGMAP)) {
298 1.1.4.2 tron printf(": big map is not supported, flags=0x%x\n",
299 1.1.4.2 tron inq->flags);
300 1.1.4.2 tron CISS_UNLOCK_SCRATCH(sc, lock);
301 1.1.4.2 tron bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
302 1.1.4.2 tron bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
303 1.1.4.2 tron return -1;
304 1.1.4.2 tron }
305 1.1.4.2 tron
306 1.1.4.2 tron sc->maxunits = inq->numld;
307 1.1.4.2 tron sc->nbus = inq->nscsi_bus;
308 1.1.4.2 tron sc->ndrives = inq->buswidth;
309 1.1.4.2 tron printf(": %d LD%s, HW rev %d, FW %4.4s/%4.4s\n",
310 1.1.4.2 tron inq->numld, inq->numld == 1? "" : "s",
311 1.1.4.2 tron inq->hw_rev, inq->fw_running, inq->fw_stored);
312 1.1.4.2 tron
313 1.1.4.2 tron CISS_UNLOCK_SCRATCH(sc, lock);
314 1.1.4.2 tron
315 1.1.4.2 tron callout_init(&sc->sc_hb);
316 1.1.4.2 tron callout_setfunc(&sc->sc_hb, ciss_heartbeat, sc);
317 1.1.4.2 tron callout_schedule(&sc->sc_hb, hz * 3);
318 1.1.4.2 tron
319 1.1.4.2 tron /* map LDs */
320 1.1.4.2 tron if (ciss_ldmap(sc)) {
321 1.1.4.2 tron printf("%s: adapter LD map failed\n", sc->sc_dev.dv_xname);
322 1.1.4.2 tron bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
323 1.1.4.2 tron bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
324 1.1.4.2 tron return -1;
325 1.1.4.2 tron }
326 1.1.4.2 tron
327 1.1.4.2 tron /* TODO scan all physdev */
328 1.1.4.2 tron /* TODO scan all logdev */
329 1.1.4.2 tron
330 1.1.4.2 tron sc->sc_flush = CISS_FLUSH_ENABLE;
331 1.1.4.2 tron if (!(sc->sc_sh = shutdownhook_establish(ciss_shutdown, sc))) {
332 1.1.4.2 tron printf(": unable to establish shutdown hook\n");
333 1.1.4.2 tron bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
334 1.1.4.2 tron bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
335 1.1.4.2 tron return -1;
336 1.1.4.2 tron }
337 1.1.4.2 tron
338 1.1.4.2 tron #if 0
339 1.1.4.2 tron if (kthread_create(ciss_kthread, sc, NULL, "%s", sc->sc_dev.dv_xname)) {
340 1.1.4.2 tron printf(": unable to create kernel thread\n");
341 1.1.4.2 tron shutdownhook_disestablish(sc->sc_sh);
342 1.1.4.2 tron bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
343 1.1.4.2 tron bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
344 1.1.4.2 tron return -1;
345 1.1.4.2 tron }
346 1.1.4.2 tron #endif
347 1.1.4.2 tron
348 1.1.4.2 tron sc->sc_channel.chan_adapter = &sc->sc_adapter;
349 1.1.4.2 tron sc->sc_channel.chan_bustype = &scsi_bustype;
350 1.1.4.2 tron sc->sc_channel.chan_channel = 0;
351 1.1.4.2 tron sc->sc_channel.chan_ntargets = sc->maxunits;
352 1.1.4.2 tron sc->sc_channel.chan_nluns = 8;
353 1.1.4.2 tron sc->sc_channel.chan_openings = sc->maxcmd / (sc->maxunits? sc->maxunits : 1);
354 1.1.4.2 tron sc->sc_channel.chan_flags = 0;
355 1.1.4.2 tron sc->sc_channel.chan_id = sc->maxunits;
356 1.1.4.2 tron
357 1.1.4.2 tron sc->sc_adapter.adapt_dev = (struct device *) sc;
358 1.1.4.2 tron sc->sc_adapter.adapt_openings = sc->maxcmd / (sc->maxunits? sc->maxunits : 1);
359 1.1.4.2 tron sc->sc_adapter.adapt_max_periph = sc->maxunits;
360 1.1.4.2 tron sc->sc_adapter.adapt_request = ciss_scsi_cmd;
361 1.1.4.2 tron sc->sc_adapter.adapt_minphys = cissminphys;
362 1.1.4.2 tron sc->sc_adapter.adapt_ioctl = ciss_scsi_ioctl;
363 1.1.4.2 tron sc->sc_adapter.adapt_nchannels = 1;
364 1.1.4.2 tron config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
365 1.1.4.2 tron
366 1.1.4.2 tron #if 0
367 1.1.4.2 tron sc->sc_link_raw.adapter_softc = sc;
368 1.1.4.2 tron sc->sc_link.openings = sc->maxcmd / (sc->maxunits? sc->maxunits : 1);
369 1.1.4.2 tron sc->sc_link_raw.adapter = &ciss_raw_switch;
370 1.1.4.2 tron sc->sc_link_raw.adapter_target = sc->ndrives;
371 1.1.4.2 tron sc->sc_link_raw.adapter_buswidth = sc->ndrives;
372 1.1.4.2 tron config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
373 1.1.4.2 tron #endif
374 1.1.4.2 tron
375 1.1.4.2 tron #if NBIO1 > 0
376 1.1.4.2 tron if (bio_register(&sc->sc_dev, ciss_ioctl) != 0)
377 1.1.4.2 tron printf("%s: controller registration failed",
378 1.1.4.2 tron sc->sc_dev.dv_xname);
379 1.1.4.2 tron #endif
380 1.1.4.2 tron
381 1.1.4.2 tron return 0;
382 1.1.4.2 tron }
383 1.1.4.2 tron
384 1.1.4.2 tron static void
385 1.1.4.2 tron ciss_shutdown(void *v)
386 1.1.4.2 tron {
387 1.1.4.2 tron struct ciss_softc *sc = v;
388 1.1.4.2 tron
389 1.1.4.2 tron sc->sc_flush = CISS_FLUSH_DISABLE;
390 1.1.4.2 tron /* timeout_del(&sc->sc_hb); */
391 1.1.4.2 tron ciss_sync(sc);
392 1.1.4.2 tron }
393 1.1.4.2 tron
394 1.1.4.2 tron static void
395 1.1.4.2 tron cissminphys(struct buf *bp)
396 1.1.4.2 tron {
397 1.1.4.2 tron #if 0 /* TOSO */
398 1.1.4.2 tron #define CISS_MAXFER (PAGE_SIZE * (sc->maxsg + 1))
399 1.1.4.2 tron if (bp->b_bcount > CISS_MAXFER)
400 1.1.4.2 tron bp->b_bcount = CISS_MAXFER;
401 1.1.4.2 tron #endif
402 1.1.4.2 tron minphys(bp);
403 1.1.4.2 tron }
404 1.1.4.2 tron
405 1.1.4.2 tron /*
406 1.1.4.2 tron * submit a command and optionally wait for completition.
407 1.1.4.2 tron * wait arg abuses XS_CTL_POLL|XS_CTL_NOSLEEP flags to request
408 1.1.4.2 tron * to wait (XS_CTL_POLL) and to allow tsleep() (!XS_CTL_NOSLEEP)
409 1.1.4.2 tron * instead of busy loop waiting
410 1.1.4.2 tron */
411 1.1.4.2 tron static int
412 1.1.4.2 tron ciss_cmd(struct ciss_ccb *ccb, int flags, int wait)
413 1.1.4.2 tron {
414 1.1.4.2 tron struct ciss_softc *sc = ccb->ccb_sc;
415 1.1.4.2 tron struct ciss_cmd *cmd = &ccb->ccb_cmd;
416 1.1.4.2 tron struct ciss_ccb *ccb1;
417 1.1.4.2 tron bus_dmamap_t dmap = ccb->ccb_dmamap;
418 1.1.4.2 tron u_int32_t id;
419 1.1.4.2 tron int i, tohz, error = 0;
420 1.1.4.2 tron
421 1.1.4.2 tron if (ccb->ccb_state != CISS_CCB_READY) {
422 1.1.4.2 tron printf("%s: ccb %d not ready state=0x%x\n", sc->sc_dev.dv_xname,
423 1.1.4.2 tron cmd->id, ccb->ccb_state);
424 1.1.4.2 tron return (EINVAL);
425 1.1.4.2 tron }
426 1.1.4.2 tron
427 1.1.4.2 tron if (ccb->ccb_data) {
428 1.1.4.2 tron bus_dma_segment_t *sgd;
429 1.1.4.2 tron
430 1.1.4.2 tron if ((error = bus_dmamap_load(sc->sc_dmat, dmap, ccb->ccb_data,
431 1.1.4.2 tron ccb->ccb_len, NULL, flags))) {
432 1.1.4.2 tron if (error == EFBIG)
433 1.1.4.2 tron printf("more than %d dma segs\n", sc->maxsg);
434 1.1.4.2 tron else
435 1.1.4.2 tron printf("error %d loading dma map\n", error);
436 1.1.4.2 tron ciss_put_ccb(ccb);
437 1.1.4.2 tron return (error);
438 1.1.4.2 tron }
439 1.1.4.2 tron cmd->sgin = dmap->dm_nsegs;
440 1.1.4.2 tron
441 1.1.4.2 tron sgd = dmap->dm_segs;
442 1.1.4.2 tron CISS_DPRINTF(CISS_D_DMA, ("data=%p/%u<0x%lx/%lu",
443 1.1.4.2 tron ccb->ccb_data, ccb->ccb_len, sgd->ds_addr, sgd->ds_len));
444 1.1.4.2 tron
445 1.1.4.2 tron for (i = 0; i < dmap->dm_nsegs; sgd++, i++) {
446 1.1.4.2 tron cmd->sgl[i].addr_lo = htole32(sgd->ds_addr);
447 1.1.4.2 tron cmd->sgl[i].addr_hi =
448 1.1.4.2 tron htole32((u_int64_t)sgd->ds_addr >> 32);
449 1.1.4.2 tron cmd->sgl[i].len = htole32(sgd->ds_len);
450 1.1.4.2 tron cmd->sgl[i].flags = htole32(0);
451 1.1.4.2 tron if (i)
452 1.1.4.2 tron CISS_DPRINTF(CISS_D_DMA,
453 1.1.4.2 tron (",0x%lx/%lu", sgd->ds_addr, sgd->ds_len));
454 1.1.4.2 tron }
455 1.1.4.2 tron
456 1.1.4.2 tron CISS_DPRINTF(CISS_D_DMA, ("> "));
457 1.1.4.2 tron
458 1.1.4.2 tron bus_dmamap_sync(sc->sc_dmat, dmap, 0, dmap->dm_mapsize,
459 1.1.4.2 tron BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
460 1.1.4.2 tron } else
461 1.1.4.2 tron cmd->sgin = 0;
462 1.1.4.2 tron cmd->sglen = htole16((u_int16_t)cmd->sgin);
463 1.1.4.2 tron bzero(&ccb->ccb_err, sizeof(ccb->ccb_err));
464 1.1.4.2 tron
465 1.1.4.2 tron bus_dmamap_sync(sc->sc_dmat, sc->cmdmap, 0, sc->cmdmap->dm_mapsize,
466 1.1.4.2 tron BUS_DMASYNC_PREWRITE);
467 1.1.4.2 tron
468 1.1.4.2 tron if ((wait & (XS_CTL_POLL|XS_CTL_NOSLEEP)) == (XS_CTL_POLL|XS_CTL_NOSLEEP))
469 1.1.4.2 tron bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_IMR,
470 1.1.4.2 tron bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IMR) | sc->iem);
471 1.1.4.2 tron
472 1.1.4.2 tron TAILQ_INSERT_TAIL(&sc->sc_ccbq, ccb, ccb_link);
473 1.1.4.2 tron ccb->ccb_state = CISS_CCB_ONQ;
474 1.1.4.2 tron CISS_DPRINTF(CISS_D_CMD, ("submit=0x%x ", cmd->id));
475 1.1.4.2 tron bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_INQ, ccb->ccb_cmdpa);
476 1.1.4.2 tron
477 1.1.4.2 tron if (wait & XS_CTL_POLL) {
478 1.1.4.2 tron int etick;
479 1.1.4.2 tron CISS_DPRINTF(CISS_D_CMD, ("waiting "));
480 1.1.4.2 tron
481 1.1.4.2 tron i = ccb->ccb_xs? ccb->ccb_xs->timeout : 60000;
482 1.1.4.2 tron tohz = (i / 1000) * hz + (i % 1000) * (hz / 1000);
483 1.1.4.2 tron if (tohz == 0)
484 1.1.4.2 tron tohz = 1;
485 1.1.4.2 tron for (i *= 100, etick = tick + tohz; i--; ) {
486 1.1.4.2 tron if (!(wait & XS_CTL_NOSLEEP)) {
487 1.1.4.2 tron ccb->ccb_state = CISS_CCB_POLL;
488 1.1.4.2 tron CISS_DPRINTF(CISS_D_CMD, ("tsleep(%d) ", tohz));
489 1.1.4.2 tron if (tsleep(ccb, PRIBIO + 1, "ciss_cmd",
490 1.1.4.2 tron tohz) == EWOULDBLOCK) {
491 1.1.4.2 tron break;
492 1.1.4.2 tron }
493 1.1.4.2 tron if (ccb->ccb_state != CISS_CCB_ONQ) {
494 1.1.4.2 tron tohz = etick - tick;
495 1.1.4.2 tron if (tohz <= 0)
496 1.1.4.2 tron break;
497 1.1.4.2 tron CISS_DPRINTF(CISS_D_CMD, ("T"));
498 1.1.4.2 tron continue;
499 1.1.4.2 tron }
500 1.1.4.2 tron ccb1 = ccb;
501 1.1.4.2 tron } else {
502 1.1.4.2 tron DELAY(10);
503 1.1.4.2 tron
504 1.1.4.2 tron if (!(bus_space_read_4(sc->sc_iot, sc->sc_ioh,
505 1.1.4.2 tron CISS_ISR) & sc->iem)) {
506 1.1.4.2 tron CISS_DPRINTF(CISS_D_CMD, ("N"));
507 1.1.4.2 tron continue;
508 1.1.4.2 tron }
509 1.1.4.2 tron
510 1.1.4.2 tron if ((id = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
511 1.1.4.2 tron CISS_OUTQ)) == 0xffffffff) {
512 1.1.4.2 tron CISS_DPRINTF(CISS_D_CMD, ("Q"));
513 1.1.4.2 tron continue;
514 1.1.4.2 tron }
515 1.1.4.2 tron
516 1.1.4.2 tron CISS_DPRINTF(CISS_D_CMD, ("got=0x%x ", id));
517 1.1.4.2 tron ccb1 = (struct ciss_ccb *)
518 1.1.4.2 tron (sc->ccbs + (id >> 2) * sc->ccblen);
519 1.1.4.2 tron ccb1->ccb_cmd.id = htole32(id);
520 1.1.4.2 tron }
521 1.1.4.2 tron
522 1.1.4.2 tron error = ciss_done(ccb1);
523 1.1.4.2 tron if (ccb1 == ccb)
524 1.1.4.2 tron break;
525 1.1.4.2 tron }
526 1.1.4.2 tron
527 1.1.4.2 tron /* if never got a chance to be done above... */
528 1.1.4.2 tron if (ccb->ccb_state != CISS_CCB_FREE) {
529 1.1.4.2 tron ccb->ccb_err.cmd_stat = CISS_ERR_TMO;
530 1.1.4.2 tron error = ciss_done(ccb);
531 1.1.4.2 tron }
532 1.1.4.2 tron
533 1.1.4.2 tron CISS_DPRINTF(CISS_D_CMD, ("done %d:%d",
534 1.1.4.2 tron ccb->ccb_err.cmd_stat, ccb->ccb_err.scsi_stat));
535 1.1.4.2 tron }
536 1.1.4.2 tron
537 1.1.4.2 tron if ((wait & (XS_CTL_POLL|XS_CTL_NOSLEEP)) == (XS_CTL_POLL|XS_CTL_NOSLEEP))
538 1.1.4.2 tron bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_IMR,
539 1.1.4.2 tron bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IMR) & ~sc->iem);
540 1.1.4.2 tron
541 1.1.4.2 tron return (error);
542 1.1.4.2 tron }
543 1.1.4.2 tron
544 1.1.4.2 tron static int
545 1.1.4.2 tron ciss_done(struct ciss_ccb *ccb)
546 1.1.4.2 tron {
547 1.1.4.2 tron struct ciss_softc *sc = ccb->ccb_sc;
548 1.1.4.2 tron struct scsipi_xfer *xs = ccb->ccb_xs;
549 1.1.4.2 tron ciss_lock_t lock;
550 1.1.4.2 tron int error = 0;
551 1.1.4.2 tron
552 1.1.4.2 tron CISS_DPRINTF(CISS_D_CMD, ("ciss_done(%p) ", ccb));
553 1.1.4.2 tron
554 1.1.4.2 tron if (ccb->ccb_state != CISS_CCB_ONQ) {
555 1.1.4.2 tron printf("%s: unqueued ccb %p ready, state=0x%x\n",
556 1.1.4.2 tron sc->sc_dev.dv_xname, ccb, ccb->ccb_state);
557 1.1.4.2 tron return 1;
558 1.1.4.2 tron }
559 1.1.4.2 tron
560 1.1.4.2 tron lock = CISS_LOCK(sc);
561 1.1.4.2 tron ccb->ccb_state = CISS_CCB_READY;
562 1.1.4.2 tron TAILQ_REMOVE(&sc->sc_ccbq, ccb, ccb_link);
563 1.1.4.2 tron
564 1.1.4.2 tron if (ccb->ccb_cmd.id & CISS_CMD_ERR)
565 1.1.4.2 tron error = ciss_error(ccb);
566 1.1.4.2 tron
567 1.1.4.2 tron if (ccb->ccb_data) {
568 1.1.4.2 tron bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap, 0,
569 1.1.4.2 tron ccb->ccb_dmamap->dm_mapsize,
570 1.1.4.2 tron (xs && xs->xs_control & XS_CTL_DATA_IN) ?
571 1.1.4.2 tron BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
572 1.1.4.2 tron bus_dmamap_unload(sc->sc_dmat, ccb->ccb_dmamap);
573 1.1.4.2 tron ccb->ccb_xs = NULL;
574 1.1.4.2 tron ccb->ccb_data = NULL;
575 1.1.4.2 tron }
576 1.1.4.2 tron
577 1.1.4.2 tron ciss_put_ccb(ccb);
578 1.1.4.2 tron
579 1.1.4.2 tron if (xs) {
580 1.1.4.2 tron xs->resid = 0;
581 1.1.4.2 tron xs->xs_status |= XS_STS_DONE;
582 1.1.4.2 tron CISS_DPRINTF(CISS_D_CMD, ("scsipi_done(%p) ", xs));
583 1.1.4.2 tron scsipi_done(xs);
584 1.1.4.2 tron }
585 1.1.4.2 tron CISS_UNLOCK(sc, lock);
586 1.1.4.2 tron
587 1.1.4.2 tron return error;
588 1.1.4.2 tron }
589 1.1.4.2 tron
590 1.1.4.2 tron static int
591 1.1.4.2 tron ciss_error(struct ciss_ccb *ccb)
592 1.1.4.2 tron {
593 1.1.4.2 tron struct ciss_softc *sc = ccb->ccb_sc;
594 1.1.4.2 tron struct ciss_error *err = &ccb->ccb_err;
595 1.1.4.2 tron struct scsipi_xfer *xs = ccb->ccb_xs;
596 1.1.4.2 tron int rv;
597 1.1.4.2 tron
598 1.1.4.2 tron switch ((rv = le16toh(err->cmd_stat))) {
599 1.1.4.2 tron case CISS_ERR_OK:
600 1.1.4.2 tron break;
601 1.1.4.2 tron
602 1.1.4.2 tron case CISS_ERR_INVCMD:
603 1.1.4.2 tron printf("%s: invalid cmd 0x%x: 0x%x is not valid @ 0x%x[%d]\n",
604 1.1.4.2 tron sc->sc_dev.dv_xname, ccb->ccb_cmd.id,
605 1.1.4.2 tron err->err_info, err->err_type[3], err->err_type[2]);
606 1.1.4.2 tron if (xs) {
607 1.1.4.2 tron bzero(&xs->sense, sizeof(xs->sense));
608 1.1.4.2 tron xs->sense.scsi_sense.response_code =
609 1.1.4.2 tron SSD_RCODE_CURRENT | SSD_RCODE_VALID;
610 1.1.4.2 tron xs->sense.scsi_sense.flags = SKEY_ILLEGAL_REQUEST;
611 1.1.4.2 tron xs->sense.scsi_sense.asc = 0x24; /* ill field */
612 1.1.4.2 tron xs->sense.scsi_sense.ascq = 0x0;
613 1.1.4.2 tron xs->error = XS_SENSE;
614 1.1.4.2 tron }
615 1.1.4.2 tron break;
616 1.1.4.2 tron
617 1.1.4.2 tron case CISS_ERR_TMO:
618 1.1.4.2 tron xs->error = XS_TIMEOUT;
619 1.1.4.2 tron break;
620 1.1.4.2 tron
621 1.1.4.2 tron case CISS_ERR_UNRUN:
622 1.1.4.2 tron /* Underrun */
623 1.1.4.2 tron xs->resid = le32toh(err->resid);
624 1.1.4.2 tron CISS_DPRINTF(CISS_D_CMD, (" underrun resid=0x%x ",
625 1.1.4.2 tron xs->resid));
626 1.1.4.2 tron break;
627 1.1.4.2 tron default:
628 1.1.4.2 tron if (xs) {
629 1.1.4.2 tron CISS_DPRINTF(CISS_D_CMD, ("scsi_stat=%x ", err->scsi_stat));
630 1.1.4.2 tron switch (err->scsi_stat) {
631 1.1.4.2 tron case SCSI_CHECK:
632 1.1.4.2 tron xs->error = XS_SENSE;
633 1.1.4.2 tron bcopy(&err->sense[0], &xs->sense,
634 1.1.4.2 tron sizeof(xs->sense));
635 1.1.4.2 tron CISS_DPRINTF(CISS_D_CMD, (" sense=%02x %02x %02x %02x ",
636 1.1.4.2 tron err->sense[0], err->sense[1], err->sense[2], err->sense[3]));
637 1.1.4.2 tron break;
638 1.1.4.2 tron
639 1.1.4.2 tron case XS_BUSY:
640 1.1.4.2 tron xs->error = XS_BUSY;
641 1.1.4.2 tron break;
642 1.1.4.2 tron
643 1.1.4.2 tron default:
644 1.1.4.2 tron CISS_DPRINTF(CISS_D_ERR, ("%s: "
645 1.1.4.2 tron "cmd_stat=%x scsi_stat=0x%x resid=0x%x\n",
646 1.1.4.2 tron sc->sc_dev.dv_xname, rv, err->scsi_stat,
647 1.1.4.2 tron le32toh(err->resid)));
648 1.1.4.2 tron printf("ciss driver stuffup in %s:%d: %s()\n",
649 1.1.4.2 tron __FILE__, __LINE__, __FUNCTION__);
650 1.1.4.2 tron xs->error = XS_DRIVER_STUFFUP;
651 1.1.4.2 tron break;
652 1.1.4.2 tron }
653 1.1.4.2 tron xs->resid = le32toh(err->resid);
654 1.1.4.2 tron }
655 1.1.4.2 tron }
656 1.1.4.2 tron ccb->ccb_cmd.id &= htole32(~3);
657 1.1.4.2 tron
658 1.1.4.2 tron return rv;
659 1.1.4.2 tron }
660 1.1.4.2 tron
661 1.1.4.2 tron static int
662 1.1.4.2 tron ciss_inq(struct ciss_softc *sc, struct ciss_inquiry *inq)
663 1.1.4.2 tron {
664 1.1.4.2 tron struct ciss_ccb *ccb;
665 1.1.4.2 tron struct ciss_cmd *cmd;
666 1.1.4.2 tron
667 1.1.4.2 tron ccb = ciss_get_ccb(sc);
668 1.1.4.2 tron ccb->ccb_len = sizeof(*inq);
669 1.1.4.2 tron ccb->ccb_data = inq;
670 1.1.4.2 tron cmd = &ccb->ccb_cmd;
671 1.1.4.2 tron cmd->tgt = htole32(CISS_CMD_MODE_PERIPH);
672 1.1.4.2 tron cmd->tgt2 = 0;
673 1.1.4.2 tron cmd->cdblen = 10;
674 1.1.4.2 tron cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_IN;
675 1.1.4.2 tron cmd->tmo = htole16(0);
676 1.1.4.2 tron bzero(&cmd->cdb[0], sizeof(cmd->cdb));
677 1.1.4.2 tron cmd->cdb[0] = CISS_CMD_CTRL_GET;
678 1.1.4.2 tron cmd->cdb[6] = CISS_CMS_CTRL_CTRL;
679 1.1.4.2 tron cmd->cdb[7] = sizeof(*inq) >> 8; /* biiiig endian */
680 1.1.4.2 tron cmd->cdb[8] = sizeof(*inq) & 0xff;
681 1.1.4.2 tron
682 1.1.4.2 tron return ciss_cmd(ccb, BUS_DMA_NOWAIT, XS_CTL_POLL|XS_CTL_NOSLEEP);
683 1.1.4.2 tron }
684 1.1.4.2 tron
685 1.1.4.2 tron static int
686 1.1.4.2 tron ciss_ldmap(struct ciss_softc *sc)
687 1.1.4.2 tron {
688 1.1.4.2 tron struct ciss_ccb *ccb;
689 1.1.4.2 tron struct ciss_cmd *cmd;
690 1.1.4.2 tron struct ciss_ldmap *lmap;
691 1.1.4.2 tron ciss_lock_t lock;
692 1.1.4.2 tron int total, rv;
693 1.1.4.2 tron
694 1.1.4.2 tron lock = CISS_LOCK_SCRATCH(sc);
695 1.1.4.2 tron lmap = sc->scratch;
696 1.1.4.2 tron lmap->size = htobe32(sc->maxunits * sizeof(lmap->map));
697 1.1.4.2 tron total = sizeof(*lmap) + (sc->maxunits - 1) * sizeof(lmap->map);
698 1.1.4.2 tron
699 1.1.4.2 tron ccb = ciss_get_ccb(sc);
700 1.1.4.2 tron ccb->ccb_len = total;
701 1.1.4.2 tron ccb->ccb_data = lmap;
702 1.1.4.2 tron cmd = &ccb->ccb_cmd;
703 1.1.4.2 tron cmd->tgt = CISS_CMD_MODE_PERIPH;
704 1.1.4.2 tron cmd->tgt2 = 0;
705 1.1.4.2 tron cmd->cdblen = 12;
706 1.1.4.2 tron cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_IN;
707 1.1.4.2 tron cmd->tmo = htole16(30);
708 1.1.4.2 tron bzero(&cmd->cdb[0], sizeof(cmd->cdb));
709 1.1.4.2 tron cmd->cdb[0] = CISS_CMD_LDMAP;
710 1.1.4.2 tron cmd->cdb[8] = total >> 8; /* biiiig endian */
711 1.1.4.2 tron cmd->cdb[9] = total & 0xff;
712 1.1.4.2 tron
713 1.1.4.2 tron rv = ciss_cmd(ccb, BUS_DMA_NOWAIT, XS_CTL_POLL|XS_CTL_NOSLEEP);
714 1.1.4.2 tron CISS_UNLOCK_SCRATCH(sc, lock);
715 1.1.4.2 tron
716 1.1.4.2 tron if (rv)
717 1.1.4.2 tron return rv;
718 1.1.4.2 tron
719 1.1.4.2 tron CISS_DPRINTF(CISS_D_MISC, ("lmap %x:%x\n",
720 1.1.4.2 tron lmap->map[0].tgt, lmap->map[0].tgt2));
721 1.1.4.2 tron
722 1.1.4.2 tron return 0;
723 1.1.4.2 tron }
724 1.1.4.2 tron
725 1.1.4.2 tron static int
726 1.1.4.2 tron ciss_sync(struct ciss_softc *sc)
727 1.1.4.2 tron {
728 1.1.4.2 tron struct ciss_ccb *ccb;
729 1.1.4.2 tron struct ciss_cmd *cmd;
730 1.1.4.2 tron struct ciss_flush *flush;
731 1.1.4.2 tron ciss_lock_t lock;
732 1.1.4.2 tron int rv;
733 1.1.4.2 tron
734 1.1.4.2 tron lock = CISS_LOCK_SCRATCH(sc);
735 1.1.4.2 tron flush = sc->scratch;
736 1.1.4.2 tron bzero(flush, sizeof(*flush));
737 1.1.4.2 tron flush->flush = sc->sc_flush;
738 1.1.4.2 tron
739 1.1.4.2 tron ccb = ciss_get_ccb(sc);
740 1.1.4.2 tron ccb->ccb_len = sizeof(*flush);
741 1.1.4.2 tron ccb->ccb_data = flush;
742 1.1.4.2 tron cmd = &ccb->ccb_cmd;
743 1.1.4.2 tron cmd->tgt = CISS_CMD_MODE_PERIPH;
744 1.1.4.2 tron cmd->tgt2 = 0;
745 1.1.4.2 tron cmd->cdblen = 10;
746 1.1.4.2 tron cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL | CISS_CDB_OUT;
747 1.1.4.2 tron cmd->tmo = 0;
748 1.1.4.2 tron bzero(&cmd->cdb[0], sizeof(cmd->cdb));
749 1.1.4.2 tron cmd->cdb[0] = CISS_CMD_CTRL_SET;
750 1.1.4.2 tron cmd->cdb[6] = CISS_CMS_CTRL_FLUSH;
751 1.1.4.2 tron cmd->cdb[7] = sizeof(*flush) >> 8; /* biiiig endian */
752 1.1.4.2 tron cmd->cdb[8] = sizeof(*flush) & 0xff;
753 1.1.4.2 tron
754 1.1.4.2 tron rv = ciss_cmd(ccb, BUS_DMA_NOWAIT, XS_CTL_POLL|XS_CTL_NOSLEEP);
755 1.1.4.2 tron CISS_UNLOCK_SCRATCH(sc, lock);
756 1.1.4.2 tron
757 1.1.4.2 tron return rv;
758 1.1.4.2 tron }
759 1.1.4.2 tron
760 1.1.4.2 tron #if 0
761 1.1.4.2 tron static void
762 1.1.4.2 tron ciss_scsi_raw_cmd(struct scsipi_channel *chan, scsipi_adapter_req_t req,
763 1.1.4.2 tron void *arg) /* TODO */
764 1.1.4.2 tron {
765 1.1.4.2 tron struct scsipi_xfer *xs = (struct scsipi_xfer *) arg;
766 1.1.4.2 tron struct ciss_rawsoftc *rsc =
767 1.1.4.2 tron (struct ciss_rawsoftc *) chan->chan_adapter->adapt_dev;
768 1.1.4.2 tron struct ciss_softc *sc = rsc->sc_softc;
769 1.1.4.2 tron struct ciss_ccb *ccb;
770 1.1.4.2 tron struct ciss_cmd *cmd;
771 1.1.4.2 tron ciss_lock_t lock;
772 1.1.4.2 tron int error;
773 1.1.4.2 tron
774 1.1.4.2 tron CISS_DPRINTF(CISS_D_CMD, ("ciss_scsi_raw_cmd "));
775 1.1.4.2 tron
776 1.1.4.2 tron switch (req)
777 1.1.4.2 tron {
778 1.1.4.2 tron case ADAPTER_REQ_RUN_XFER:
779 1.1.4.2 tron if (xs->cmdlen > CISS_MAX_CDB) {
780 1.1.4.2 tron CISS_DPRINTF(CISS_D_CMD, ("CDB too big %p ", xs));
781 1.1.4.2 tron bzero(&xs->sense, sizeof(xs->sense));
782 1.1.4.2 tron printf("ciss driver stuffup in %s:%d: %s()\n",
783 1.1.4.2 tron __FILE__, __LINE__, __FUNCTION__);
784 1.1.4.2 tron xs->error = XS_DRIVER_STUFFUP;
785 1.1.4.2 tron scsipi_done(xs);
786 1.1.4.2 tron break;
787 1.1.4.2 tron }
788 1.1.4.2 tron
789 1.1.4.2 tron lock = CISS_LOCK(sc);
790 1.1.4.2 tron error = 0;
791 1.1.4.2 tron xs->error = XS_NOERROR;
792 1.1.4.2 tron
793 1.1.4.2 tron /* TODO check this target has not yet employed w/ any volume */
794 1.1.4.2 tron
795 1.1.4.2 tron ccb = ciss_get_ccb(sc);
796 1.1.4.2 tron cmd = &ccb->ccb_cmd;
797 1.1.4.2 tron ccb->ccb_len = xs->datalen;
798 1.1.4.2 tron ccb->ccb_data = xs->data;
799 1.1.4.2 tron ccb->ccb_xs = xs;
800 1.1.4.2 tron
801 1.1.4.2 tron cmd->cdblen = xs->cmdlen;
802 1.1.4.2 tron cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL;
803 1.1.4.2 tron if (xs->xs_control & XS_CTL_DATA_IN)
804 1.1.4.2 tron cmd->flags |= CISS_CDB_IN;
805 1.1.4.2 tron else if (xs->xs_control & XS_CTL_DATA_OUT)
806 1.1.4.2 tron cmd->flags |= CISS_CDB_OUT;
807 1.1.4.2 tron cmd->tmo = xs->timeout < 1000? 1 : xs->timeout / 1000;
808 1.1.4.2 tron bzero(&cmd->cdb[0], sizeof(cmd->cdb));
809 1.1.4.2 tron bcopy(xs->cmd, &cmd->cdb[0], CISS_MAX_CDB);
810 1.1.4.2 tron
811 1.1.4.2 tron if (ciss_cmd(ccb, BUS_DMA_WAITOK,
812 1.1.4.2 tron xs->xs_control & (XS_CTL_POLL|XS_CTL_NOSLEEP))) {
813 1.1.4.2 tron printf("ciss driver stuffup in %s:%d: %s()\n",
814 1.1.4.2 tron __FILE__, __LINE__, __FUNCTION__);
815 1.1.4.2 tron xs->error = XS_DRIVER_STUFFUP;
816 1.1.4.2 tron scsipi_done(xs);
817 1.1.4.2 tron CISS_UNLOCK(sc, lock);
818 1.1.4.2 tron break;
819 1.1.4.2 tron }
820 1.1.4.2 tron
821 1.1.4.2 tron CISS_UNLOCK(sc, lock);
822 1.1.4.2 tron break;
823 1.1.4.2 tron
824 1.1.4.2 tron case ADAPTER_REQ_GROW_RESOURCES:
825 1.1.4.2 tron /*
826 1.1.4.2 tron * Not supported.
827 1.1.4.2 tron */
828 1.1.4.2 tron break;
829 1.1.4.2 tron
830 1.1.4.2 tron case ADAPTER_REQ_SET_XFER_MODE:
831 1.1.4.2 tron /*
832 1.1.4.2 tron * We can't change the transfer mode, but at least let
833 1.1.4.2 tron * scsipi know what the adapter has negociated.
834 1.1.4.2 tron */
835 1.1.4.2 tron /* Get xfer mode and return it */
836 1.1.4.2 tron break;
837 1.1.4.2 tron }
838 1.1.4.2 tron }
839 1.1.4.2 tron #endif
840 1.1.4.2 tron
841 1.1.4.2 tron static void
842 1.1.4.2 tron ciss_scsi_cmd(struct scsipi_channel *chan, scsipi_adapter_req_t req,
843 1.1.4.2 tron void *arg)
844 1.1.4.2 tron {
845 1.1.4.2 tron struct scsipi_xfer *xs = (struct scsipi_xfer *) arg;
846 1.1.4.2 tron struct ciss_softc *sc =
847 1.1.4.2 tron (struct ciss_softc *) chan->chan_adapter->adapt_dev;
848 1.1.4.2 tron u_int8_t target;
849 1.1.4.2 tron struct ciss_ccb *ccb;
850 1.1.4.2 tron struct ciss_cmd *cmd;
851 1.1.4.2 tron int error;
852 1.1.4.2 tron ciss_lock_t lock;
853 1.1.4.2 tron
854 1.1.4.2 tron CISS_DPRINTF(CISS_D_CMD, ("ciss_scsi_cmd "));
855 1.1.4.2 tron
856 1.1.4.2 tron switch (req)
857 1.1.4.2 tron {
858 1.1.4.2 tron case ADAPTER_REQ_RUN_XFER:
859 1.1.4.2 tron target = xs->xs_periph->periph_target;
860 1.1.4.2 tron CISS_DPRINTF(CISS_D_CMD, ("targ=%d ", target));
861 1.1.4.2 tron if (xs->cmdlen > CISS_MAX_CDB) {
862 1.1.4.2 tron CISS_DPRINTF(CISS_D_CMD, ("CDB too big %p ", xs));
863 1.1.4.2 tron bzero(&xs->sense, sizeof(xs->sense));
864 1.1.4.2 tron printf("ciss driver stuffup in %s:%d: %s()\n",
865 1.1.4.2 tron __FILE__, __LINE__, __FUNCTION__);
866 1.1.4.2 tron xs->error = XS_DRIVER_STUFFUP;
867 1.1.4.2 tron scsipi_done(xs);
868 1.1.4.2 tron break;
869 1.1.4.2 tron }
870 1.1.4.2 tron
871 1.1.4.2 tron lock = CISS_LOCK(sc);
872 1.1.4.2 tron error = 0;
873 1.1.4.2 tron xs->error = XS_NOERROR;
874 1.1.4.2 tron
875 1.1.4.2 tron /* XXX emulate SYNCHRONIZE_CACHE ??? */
876 1.1.4.2 tron
877 1.1.4.2 tron ccb = ciss_get_ccb(sc);
878 1.1.4.2 tron cmd = &ccb->ccb_cmd;
879 1.1.4.2 tron ccb->ccb_len = xs->datalen;
880 1.1.4.2 tron ccb->ccb_data = xs->data;
881 1.1.4.2 tron ccb->ccb_xs = xs;
882 1.1.4.2 tron cmd->tgt = CISS_CMD_MODE_LD | target;
883 1.1.4.2 tron cmd->tgt2 = 0;
884 1.1.4.2 tron cmd->cdblen = xs->cmdlen;
885 1.1.4.2 tron cmd->flags = CISS_CDB_CMD | CISS_CDB_SIMPL;
886 1.1.4.2 tron if (xs->xs_control & XS_CTL_DATA_IN)
887 1.1.4.2 tron cmd->flags |= CISS_CDB_IN;
888 1.1.4.2 tron else if (xs->xs_control & XS_CTL_DATA_OUT)
889 1.1.4.2 tron cmd->flags |= CISS_CDB_OUT;
890 1.1.4.2 tron cmd->tmo = xs->timeout < 1000? 1 : xs->timeout / 1000;
891 1.1.4.2 tron bzero(&cmd->cdb[0], sizeof(cmd->cdb));
892 1.1.4.2 tron bcopy(xs->cmd, &cmd->cdb[0], CISS_MAX_CDB);
893 1.1.4.2 tron CISS_DPRINTF(CISS_D_CMD, ("cmd=%02x %02x %02x %02x %02x %02x ",
894 1.1.4.2 tron cmd->cdb[0], cmd->cdb[1], cmd->cdb[2],
895 1.1.4.2 tron cmd->cdb[3], cmd->cdb[4], cmd->cdb[5]));
896 1.1.4.2 tron
897 1.1.4.2 tron if (ciss_cmd(ccb, BUS_DMA_WAITOK,
898 1.1.4.2 tron xs->xs_control & (XS_CTL_POLL|XS_CTL_NOSLEEP))) {
899 1.1.4.2 tron printf("ciss driver stuffup in %s:%d: %s()\n",
900 1.1.4.2 tron __FILE__, __LINE__, __FUNCTION__);
901 1.1.4.2 tron xs->error = XS_DRIVER_STUFFUP;
902 1.1.4.2 tron scsipi_done(xs);
903 1.1.4.2 tron CISS_UNLOCK(sc, lock);
904 1.1.4.2 tron return;
905 1.1.4.2 tron }
906 1.1.4.2 tron
907 1.1.4.2 tron CISS_UNLOCK(sc, lock);
908 1.1.4.2 tron break;
909 1.1.4.2 tron case ADAPTER_REQ_GROW_RESOURCES:
910 1.1.4.2 tron /*
911 1.1.4.2 tron * Not supported.
912 1.1.4.2 tron */
913 1.1.4.2 tron break;
914 1.1.4.2 tron case ADAPTER_REQ_SET_XFER_MODE:
915 1.1.4.2 tron /*
916 1.1.4.2 tron * We can't change the transfer mode, but at least let
917 1.1.4.2 tron * scsipi know what the adapter has negociated.
918 1.1.4.2 tron */
919 1.1.4.2 tron /* FIXME: get xfer mode and write it into arg */
920 1.1.4.2 tron break;
921 1.1.4.2 tron }
922 1.1.4.2 tron }
923 1.1.4.2 tron
924 1.1.4.2 tron int
925 1.1.4.2 tron ciss_intr(void *v)
926 1.1.4.2 tron {
927 1.1.4.2 tron struct ciss_softc *sc = v;
928 1.1.4.2 tron struct ciss_ccb *ccb;
929 1.1.4.2 tron ciss_lock_t lock;
930 1.1.4.2 tron u_int32_t id;
931 1.1.4.2 tron int hit = 0;
932 1.1.4.2 tron
933 1.1.4.2 tron CISS_DPRINTF(CISS_D_INTR, ("intr "));
934 1.1.4.2 tron
935 1.1.4.2 tron if (!(bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_ISR) & sc->iem))
936 1.1.4.2 tron return 0;
937 1.1.4.2 tron
938 1.1.4.2 tron lock = CISS_LOCK(sc);
939 1.1.4.2 tron while ((id = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_OUTQ)) !=
940 1.1.4.2 tron 0xffffffff) {
941 1.1.4.2 tron
942 1.1.4.2 tron ccb = (struct ciss_ccb *) (sc->ccbs + (id >> 2) * sc->ccblen);
943 1.1.4.2 tron ccb->ccb_cmd.id = htole32(id);
944 1.1.4.2 tron if (ccb->ccb_state == CISS_CCB_POLL) {
945 1.1.4.2 tron ccb->ccb_state = CISS_CCB_ONQ;
946 1.1.4.2 tron wakeup(ccb);
947 1.1.4.2 tron } else
948 1.1.4.2 tron ciss_done(ccb);
949 1.1.4.2 tron
950 1.1.4.2 tron hit = 1;
951 1.1.4.2 tron }
952 1.1.4.2 tron CISS_UNLOCK(sc, lock);
953 1.1.4.2 tron
954 1.1.4.2 tron CISS_DPRINTF(CISS_D_INTR, ("exit\n"));
955 1.1.4.2 tron return hit;
956 1.1.4.2 tron }
957 1.1.4.2 tron
958 1.1.4.2 tron static void
959 1.1.4.2 tron ciss_heartbeat(void *v)
960 1.1.4.2 tron {
961 1.1.4.2 tron struct ciss_softc *sc = v;
962 1.1.4.2 tron u_int32_t hb;
963 1.1.4.2 tron
964 1.1.4.2 tron hb = bus_space_read_4(sc->sc_iot, sc->cfg_ioh,
965 1.1.4.2 tron sc->cfgoff + offsetof(struct ciss_config, heartbeat));
966 1.1.4.2 tron if (hb == sc->heartbeat)
967 1.1.4.2 tron panic("ciss: dead"); /* XX reset! */
968 1.1.4.2 tron else
969 1.1.4.2 tron sc->heartbeat = hb;
970 1.1.4.2 tron
971 1.1.4.2 tron callout_schedule(&sc->sc_hb, hz * 3);
972 1.1.4.2 tron }
973 1.1.4.2 tron
974 1.1.4.2 tron #if 0
975 1.1.4.2 tron static void
976 1.1.4.2 tron ciss_kthread(void *v)
977 1.1.4.2 tron {
978 1.1.4.2 tron struct ciss_softc *sc = v;
979 1.1.4.2 tron ciss_lock_t lock;
980 1.1.4.2 tron
981 1.1.4.2 tron for (;;) {
982 1.1.4.2 tron tsleep(sc, PRIBIO, sc->sc_dev.dv_xname, 0);
983 1.1.4.2 tron
984 1.1.4.2 tron lock = CISS_LOCK(sc);
985 1.1.4.2 tron
986 1.1.4.2 tron
987 1.1.4.2 tron
988 1.1.4.2 tron CISS_UNLOCK(sc, lock);
989 1.1.4.2 tron }
990 1.1.4.2 tron }
991 1.1.4.2 tron #endif
992 1.1.4.2 tron
993 1.1.4.2 tron static int
994 1.1.4.2 tron ciss_scsi_ioctl(struct scsipi_channel *chan, u_long cmd,
995 1.1.4.2 tron caddr_t addr, int flag, struct proc *p)
996 1.1.4.2 tron {
997 1.1.4.2 tron #if NBIO > 0
998 1.1.4.2 tron return ciss_ioctl(chan->chan_adapter->adapt_dev, cmd, addr);
999 1.1.4.2 tron #else
1000 1.1.4.2 tron return ENOTTY;
1001 1.1.4.2 tron #endif
1002 1.1.4.2 tron }
1003 1.1.4.2 tron
1004 1.1.4.2 tron #if NBIO > 0
1005 1.1.4.2 tron static int
1006 1.1.4.2 tron ciss_ioctl(struct device *dev, u_long cmd, caddr_t addr) /* TODO */
1007 1.1.4.2 tron {
1008 1.1.4.2 tron /* struct ciss_softc *sc = (struct ciss_softc *)dev; */
1009 1.1.4.2 tron ciss_lock_t lock;
1010 1.1.4.2 tron int error;
1011 1.1.4.2 tron
1012 1.1.4.2 tron lock = CISS_LOCK(sc);
1013 1.1.4.2 tron switch (cmd) {
1014 1.1.4.2 tron case BIOCINQ:
1015 1.1.4.2 tron case BIOCVOL:
1016 1.1.4.2 tron case BIOCDISK:
1017 1.1.4.2 tron case BIOCALARM:
1018 1.1.4.2 tron case BIOCBLINK:
1019 1.1.4.2 tron case BIOCSETSTATE:
1020 1.1.4.2 tron default:
1021 1.1.4.2 tron CISS_DPRINTF(CISS_D_IOCTL, ("%s: invalid ioctl\n",
1022 1.1.4.2 tron sc->sc_dev.dv_xname));
1023 1.1.4.2 tron error = ENOTTY;
1024 1.1.4.2 tron }
1025 1.1.4.2 tron CISS_UNLOCK(sc, lock);
1026 1.1.4.2 tron
1027 1.1.4.2 tron return error;
1028 1.1.4.2 tron }
1029 1.1.4.2 tron #endif
1030