arcmsr.c revision 1.8.4.2 1 1.8.4.2 ad /* $NetBSD: arcmsr.c,v 1.8.4.2 2007/12/08 17:57:26 ad Exp $ */
2 1.8.4.2 ad /* $OpenBSD: arc.c,v 1.68 2007/10/27 03:28:27 dlg Exp $ */
3 1.8.4.2 ad
4 1.8.4.2 ad /*
5 1.8.4.2 ad * Copyright (c) 2006 David Gwynne <dlg (at) openbsd.org>
6 1.8.4.2 ad *
7 1.8.4.2 ad * Permission to use, copy, modify, and distribute this software for any
8 1.8.4.2 ad * purpose with or without fee is hereby granted, provided that the above
9 1.8.4.2 ad * copyright notice and this permission notice appear in all copies.
10 1.8.4.2 ad *
11 1.8.4.2 ad * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.8.4.2 ad * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.8.4.2 ad * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.8.4.2 ad * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.8.4.2 ad * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.8.4.2 ad * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.8.4.2 ad * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.8.4.2 ad */
19 1.8.4.2 ad
20 1.8.4.2 ad #include "bio.h"
21 1.8.4.2 ad
22 1.8.4.2 ad #include <sys/cdefs.h>
23 1.8.4.2 ad __KERNEL_RCSID(0, "$NetBSD: arcmsr.c,v 1.8.4.2 2007/12/08 17:57:26 ad Exp $");
24 1.8.4.2 ad
25 1.8.4.2 ad #include <sys/param.h>
26 1.8.4.2 ad #include <sys/buf.h>
27 1.8.4.2 ad #include <sys/kernel.h>
28 1.8.4.2 ad #include <sys/malloc.h>
29 1.8.4.2 ad #include <sys/device.h>
30 1.8.4.2 ad #include <sys/kmem.h>
31 1.8.4.2 ad #include <sys/kthread.h>
32 1.8.4.2 ad #include <sys/mutex.h>
33 1.8.4.2 ad #include <sys/condvar.h>
34 1.8.4.2 ad #include <sys/rwlock.h>
35 1.8.4.2 ad
36 1.8.4.2 ad #if NBIO > 0
37 1.8.4.2 ad #include <sys/ioctl.h>
38 1.8.4.2 ad #include <dev/biovar.h>
39 1.8.4.2 ad #endif
40 1.8.4.2 ad
41 1.8.4.2 ad #include <dev/pci/pcireg.h>
42 1.8.4.2 ad #include <dev/pci/pcivar.h>
43 1.8.4.2 ad #include <dev/pci/pcidevs.h>
44 1.8.4.2 ad
45 1.8.4.2 ad #include <dev/scsipi/scsipi_all.h>
46 1.8.4.2 ad #include <dev/scsipi/scsi_all.h>
47 1.8.4.2 ad #include <dev/scsipi/scsiconf.h>
48 1.8.4.2 ad
49 1.8.4.2 ad #include <dev/sysmon/sysmonvar.h>
50 1.8.4.2 ad
51 1.8.4.2 ad #include <sys/bus.h>
52 1.8.4.2 ad
53 1.8.4.2 ad #include <uvm/uvm_extern.h> /* for PAGE_SIZE */
54 1.8.4.2 ad
55 1.8.4.2 ad #include <dev/pci/arcmsrvar.h>
56 1.8.4.2 ad
57 1.8.4.2 ad /* #define ARC_DEBUG */
58 1.8.4.2 ad #ifdef ARC_DEBUG
59 1.8.4.2 ad #define ARC_D_INIT (1<<0)
60 1.8.4.2 ad #define ARC_D_RW (1<<1)
61 1.8.4.2 ad #define ARC_D_DB (1<<2)
62 1.8.4.2 ad
63 1.8.4.2 ad int arcdebug = 0;
64 1.8.4.2 ad
65 1.8.4.2 ad #define DPRINTF(p...) do { if (arcdebug) printf(p); } while (0)
66 1.8.4.2 ad #define DNPRINTF(n, p...) do { if ((n) & arcdebug) printf(p); } while (0)
67 1.8.4.2 ad
68 1.8.4.2 ad #else
69 1.8.4.2 ad #define DPRINTF(p...) /* p */
70 1.8.4.2 ad #define DNPRINTF(n, p...) /* n, p */
71 1.8.4.2 ad #endif
72 1.8.4.2 ad
73 1.8.4.2 ad /*
74 1.8.4.2 ad * the fw header must always equal this.
75 1.8.4.2 ad */
76 1.8.4.2 ad static struct arc_fw_hdr arc_fw_hdr = { 0x5e, 0x01, 0x61 };
77 1.8.4.2 ad
78 1.8.4.2 ad /*
79 1.8.4.2 ad * autoconf(9) glue.
80 1.8.4.2 ad */
81 1.8.4.2 ad static int arc_match(device_t, struct cfdata *, void *);
82 1.8.4.2 ad static void arc_attach(device_t, device_t, void *);
83 1.8.4.2 ad static int arc_detach(device_t, int);
84 1.8.4.2 ad static void arc_shutdown(void *);
85 1.8.4.2 ad static int arc_intr(void *);
86 1.8.4.2 ad static void arc_minphys(struct buf *);
87 1.8.4.2 ad
88 1.8.4.2 ad CFATTACH_DECL(arcmsr, sizeof(struct arc_softc),
89 1.8.4.2 ad arc_match, arc_attach, arc_detach, NULL);
90 1.8.4.2 ad
91 1.8.4.2 ad /*
92 1.8.4.2 ad * bio(4) and sysmon_envsys(9) glue.
93 1.8.4.2 ad */
94 1.8.4.2 ad #if NBIO > 0
95 1.8.4.2 ad static int arc_bioctl(struct device *, u_long, void *);
96 1.8.4.2 ad static int arc_bio_inq(struct arc_softc *, struct bioc_inq *);
97 1.8.4.2 ad static int arc_bio_vol(struct arc_softc *, struct bioc_vol *);
98 1.8.4.2 ad static int arc_bio_disk(struct arc_softc *, struct bioc_disk *);
99 1.8.4.2 ad static int arc_bio_alarm(struct arc_softc *, struct bioc_alarm *);
100 1.8.4.2 ad static int arc_bio_alarm_state(struct arc_softc *, struct bioc_alarm *);
101 1.8.4.2 ad static int arc_bio_getvol(struct arc_softc *, int,
102 1.8.4.2 ad struct arc_fw_volinfo *);
103 1.8.4.2 ad static void arc_create_sensors(void *);
104 1.8.4.2 ad static void arc_refresh_sensors(struct sysmon_envsys *, envsys_data_t *);
105 1.8.4.2 ad #endif
106 1.8.4.2 ad
107 1.8.4.2 ad static int
108 1.8.4.2 ad arc_match(device_t parent, struct cfdata *match, void *aux)
109 1.8.4.2 ad {
110 1.8.4.2 ad struct pci_attach_args *pa = aux;
111 1.8.4.2 ad
112 1.8.4.2 ad if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ARECA) {
113 1.8.4.2 ad switch (PCI_PRODUCT(pa->pa_id)) {
114 1.8.4.2 ad case PCI_PRODUCT_ARECA_ARC1110:
115 1.8.4.2 ad case PCI_PRODUCT_ARECA_ARC1120:
116 1.8.4.2 ad case PCI_PRODUCT_ARECA_ARC1130:
117 1.8.4.2 ad case PCI_PRODUCT_ARECA_ARC1160:
118 1.8.4.2 ad case PCI_PRODUCT_ARECA_ARC1170:
119 1.8.4.2 ad case PCI_PRODUCT_ARECA_ARC1200:
120 1.8.4.2 ad case PCI_PRODUCT_ARECA_ARC1202:
121 1.8.4.2 ad case PCI_PRODUCT_ARECA_ARC1210:
122 1.8.4.2 ad case PCI_PRODUCT_ARECA_ARC1220:
123 1.8.4.2 ad case PCI_PRODUCT_ARECA_ARC1230:
124 1.8.4.2 ad case PCI_PRODUCT_ARECA_ARC1260:
125 1.8.4.2 ad case PCI_PRODUCT_ARECA_ARC1270:
126 1.8.4.2 ad case PCI_PRODUCT_ARECA_ARC1280:
127 1.8.4.2 ad case PCI_PRODUCT_ARECA_ARC1380:
128 1.8.4.2 ad case PCI_PRODUCT_ARECA_ARC1381:
129 1.8.4.2 ad case PCI_PRODUCT_ARECA_ARC1680:
130 1.8.4.2 ad case PCI_PRODUCT_ARECA_ARC1681:
131 1.8.4.2 ad return 1;
132 1.8.4.2 ad default:
133 1.8.4.2 ad break;
134 1.8.4.2 ad }
135 1.8.4.2 ad }
136 1.8.4.2 ad
137 1.8.4.2 ad return 0;
138 1.8.4.2 ad }
139 1.8.4.2 ad
140 1.8.4.2 ad static void
141 1.8.4.2 ad arc_attach(device_t parent, device_t self, void *aux)
142 1.8.4.2 ad {
143 1.8.4.2 ad struct arc_softc *sc = device_private(self);
144 1.8.4.2 ad struct pci_attach_args *pa = aux;
145 1.8.4.2 ad struct scsipi_adapter *adapt = &sc->sc_adapter;
146 1.8.4.2 ad struct scsipi_channel *chan = &sc->sc_chan;
147 1.8.4.2 ad
148 1.8.4.2 ad sc->sc_talking = 0;
149 1.8.4.2 ad rw_init(&sc->sc_rwlock);
150 1.8.4.2 ad mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_BIO);
151 1.8.4.2 ad cv_init(&sc->sc_condvar, "arcdb");
152 1.8.4.2 ad
153 1.8.4.2 ad if (arc_map_pci_resources(sc, pa) != 0) {
154 1.8.4.2 ad /* error message printed by arc_map_pci_resources */
155 1.8.4.2 ad return;
156 1.8.4.2 ad }
157 1.8.4.2 ad
158 1.8.4.2 ad if (arc_query_firmware(sc) != 0) {
159 1.8.4.2 ad /* error message printed by arc_query_firmware */
160 1.8.4.2 ad goto unmap_pci;
161 1.8.4.2 ad }
162 1.8.4.2 ad
163 1.8.4.2 ad if (arc_alloc_ccbs(sc) != 0) {
164 1.8.4.2 ad /* error message printed by arc_alloc_ccbs */
165 1.8.4.2 ad goto unmap_pci;
166 1.8.4.2 ad }
167 1.8.4.2 ad
168 1.8.4.2 ad sc->sc_shutdownhook = shutdownhook_establish(arc_shutdown, sc);
169 1.8.4.2 ad if (sc->sc_shutdownhook == NULL)
170 1.8.4.2 ad panic("unable to establish arc powerhook");
171 1.8.4.2 ad
172 1.8.4.2 ad memset(adapt, 0, sizeof(*adapt));
173 1.8.4.2 ad adapt->adapt_dev = self;
174 1.8.4.2 ad adapt->adapt_nchannels = 1;
175 1.8.4.2 ad adapt->adapt_openings = sc->sc_req_count / ARC_MAX_TARGET;
176 1.8.4.2 ad adapt->adapt_max_periph = adapt->adapt_openings;
177 1.8.4.2 ad adapt->adapt_minphys = arc_minphys;
178 1.8.4.2 ad adapt->adapt_request = arc_scsi_cmd;
179 1.8.4.2 ad
180 1.8.4.2 ad memset(chan, 0, sizeof(*chan));
181 1.8.4.2 ad chan->chan_adapter = adapt;
182 1.8.4.2 ad chan->chan_bustype = &scsi_bustype;
183 1.8.4.2 ad chan->chan_nluns = ARC_MAX_LUN;
184 1.8.4.2 ad chan->chan_ntargets = ARC_MAX_TARGET;
185 1.8.4.2 ad chan->chan_id = ARC_MAX_TARGET;
186 1.8.4.2 ad chan->chan_channel = 0;
187 1.8.4.2 ad chan->chan_flags = SCSIPI_CHAN_NOSETTLE;
188 1.8.4.2 ad
189 1.8.4.2 ad (void)config_found(self, &sc->sc_chan, scsiprint);
190 1.8.4.2 ad
191 1.8.4.2 ad /* enable interrupts */
192 1.8.4.2 ad arc_write(sc, ARC_REG_INTRMASK,
193 1.8.4.2 ad ~(ARC_REG_INTRMASK_POSTQUEUE|ARC_REG_INTRSTAT_DOORBELL));
194 1.8.4.2 ad
195 1.8.4.2 ad #if NBIO > 0
196 1.8.4.2 ad /*
197 1.8.4.2 ad * Register the driver to bio(4) and setup the sensors.
198 1.8.4.2 ad */
199 1.8.4.2 ad if (bio_register(self, arc_bioctl) != 0)
200 1.8.4.2 ad panic("%s: bioctl registration failed\n", device_xname(self));
201 1.8.4.2 ad
202 1.8.4.2 ad /*
203 1.8.4.2 ad * you need to talk to the firmware to get volume info. our firmware
204 1.8.4.2 ad * interface relies on being able to sleep, so we need to use a thread
205 1.8.4.2 ad * to do the work.
206 1.8.4.2 ad */
207 1.8.4.2 ad if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
208 1.8.4.2 ad arc_create_sensors, sc, &sc->sc_lwp, "arcmsr_sensors") != 0)
209 1.8.4.2 ad panic("%s: unable to create a kernel thread for sensors\n",
210 1.8.4.2 ad device_xname(self));
211 1.8.4.2 ad #endif
212 1.8.4.2 ad
213 1.8.4.2 ad return;
214 1.8.4.2 ad
215 1.8.4.2 ad unmap_pci:
216 1.8.4.2 ad arc_unmap_pci_resources(sc);
217 1.8.4.2 ad }
218 1.8.4.2 ad
219 1.8.4.2 ad static int
220 1.8.4.2 ad arc_detach(device_t self, int flags)
221 1.8.4.2 ad {
222 1.8.4.2 ad struct arc_softc *sc = device_private(self);
223 1.8.4.2 ad
224 1.8.4.2 ad shutdownhook_disestablish(sc->sc_shutdownhook);
225 1.8.4.2 ad
226 1.8.4.2 ad if (arc_msg0(sc, ARC_REG_INB_MSG0_STOP_BGRB) != 0)
227 1.8.4.2 ad aprint_error("%s: timeout waiting to stop bg rebuild\n",
228 1.8.4.2 ad device_xname(&sc->sc_dev));
229 1.8.4.2 ad
230 1.8.4.2 ad if (arc_msg0(sc, ARC_REG_INB_MSG0_FLUSH_CACHE) != 0)
231 1.8.4.2 ad aprint_error("%s: timeout waiting to flush cache\n",
232 1.8.4.2 ad device_xname(&sc->sc_dev));
233 1.8.4.2 ad
234 1.8.4.2 ad return 0;
235 1.8.4.2 ad }
236 1.8.4.2 ad
237 1.8.4.2 ad static void
238 1.8.4.2 ad arc_shutdown(void *xsc)
239 1.8.4.2 ad {
240 1.8.4.2 ad struct arc_softc *sc = xsc;
241 1.8.4.2 ad
242 1.8.4.2 ad if (arc_msg0(sc, ARC_REG_INB_MSG0_STOP_BGRB) != 0)
243 1.8.4.2 ad aprint_error("%s: timeout waiting to stop bg rebuild\n",
244 1.8.4.2 ad device_xname(&sc->sc_dev));
245 1.8.4.2 ad
246 1.8.4.2 ad if (arc_msg0(sc, ARC_REG_INB_MSG0_FLUSH_CACHE) != 0)
247 1.8.4.2 ad aprint_error("%s: timeout waiting to flush cache\n",
248 1.8.4.2 ad device_xname(&sc->sc_dev));
249 1.8.4.2 ad }
250 1.8.4.2 ad
251 1.8.4.2 ad static void
252 1.8.4.2 ad arc_minphys(struct buf *bp)
253 1.8.4.2 ad {
254 1.8.4.2 ad if (bp->b_bcount > MAXPHYS)
255 1.8.4.2 ad bp->b_bcount = MAXPHYS;
256 1.8.4.2 ad minphys(bp);
257 1.8.4.2 ad }
258 1.8.4.2 ad
259 1.8.4.2 ad static int
260 1.8.4.2 ad arc_intr(void *arg)
261 1.8.4.2 ad {
262 1.8.4.2 ad struct arc_softc *sc = arg;
263 1.8.4.2 ad struct arc_ccb *ccb = NULL;
264 1.8.4.2 ad char *kva = ARC_DMA_KVA(sc->sc_requests);
265 1.8.4.2 ad struct arc_io_cmd *cmd;
266 1.8.4.2 ad uint32_t reg, intrstat;
267 1.8.4.2 ad
268 1.8.4.2 ad mutex_spin_enter(&sc->sc_mutex);
269 1.8.4.2 ad intrstat = arc_read(sc, ARC_REG_INTRSTAT);
270 1.8.4.2 ad if (intrstat == 0x0) {
271 1.8.4.2 ad mutex_spin_exit(&sc->sc_mutex);
272 1.8.4.2 ad return 0;
273 1.8.4.2 ad }
274 1.8.4.2 ad
275 1.8.4.2 ad intrstat &= ARC_REG_INTRSTAT_POSTQUEUE | ARC_REG_INTRSTAT_DOORBELL;
276 1.8.4.2 ad arc_write(sc, ARC_REG_INTRSTAT, intrstat);
277 1.8.4.2 ad
278 1.8.4.2 ad if (intrstat & ARC_REG_INTRSTAT_DOORBELL) {
279 1.8.4.2 ad if (sc->sc_talking) {
280 1.8.4.2 ad /* if an ioctl is talking, wake it up */
281 1.8.4.2 ad arc_write(sc, ARC_REG_INTRMASK,
282 1.8.4.2 ad ~ARC_REG_INTRMASK_POSTQUEUE);
283 1.8.4.2 ad cv_broadcast(&sc->sc_condvar);
284 1.8.4.2 ad } else {
285 1.8.4.2 ad /* otherwise drop it */
286 1.8.4.2 ad reg = arc_read(sc, ARC_REG_OUTB_DOORBELL);
287 1.8.4.2 ad arc_write(sc, ARC_REG_OUTB_DOORBELL, reg);
288 1.8.4.2 ad if (reg & ARC_REG_OUTB_DOORBELL_WRITE_OK)
289 1.8.4.2 ad arc_write(sc, ARC_REG_INB_DOORBELL,
290 1.8.4.2 ad ARC_REG_INB_DOORBELL_READ_OK);
291 1.8.4.2 ad }
292 1.8.4.2 ad }
293 1.8.4.2 ad mutex_spin_exit(&sc->sc_mutex);
294 1.8.4.2 ad
295 1.8.4.2 ad while ((reg = arc_pop(sc)) != 0xffffffff) {
296 1.8.4.2 ad cmd = (struct arc_io_cmd *)(kva +
297 1.8.4.2 ad ((reg << ARC_REG_REPLY_QUEUE_ADDR_SHIFT) -
298 1.8.4.2 ad (uint32_t)ARC_DMA_DVA(sc->sc_requests)));
299 1.8.4.2 ad ccb = &sc->sc_ccbs[htole32(cmd->cmd.context)];
300 1.8.4.2 ad
301 1.8.4.2 ad bus_dmamap_sync(sc->sc_dmat, ARC_DMA_MAP(sc->sc_requests),
302 1.8.4.2 ad ccb->ccb_offset, ARC_MAX_IOCMDLEN,
303 1.8.4.2 ad BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
304 1.8.4.2 ad
305 1.8.4.2 ad arc_scsi_cmd_done(sc, ccb, reg);
306 1.8.4.2 ad }
307 1.8.4.2 ad
308 1.8.4.2 ad return 1;
309 1.8.4.2 ad }
310 1.8.4.2 ad
311 1.8.4.2 ad void
312 1.8.4.2 ad arc_scsi_cmd(struct scsipi_channel *chan, scsipi_adapter_req_t req, void *arg)
313 1.8.4.2 ad {
314 1.8.4.2 ad struct scsipi_periph *periph;
315 1.8.4.2 ad struct scsipi_xfer *xs;
316 1.8.4.2 ad struct scsipi_adapter *adapt = chan->chan_adapter;
317 1.8.4.2 ad struct arc_softc *sc = device_private(adapt->adapt_dev);
318 1.8.4.2 ad struct arc_ccb *ccb;
319 1.8.4.2 ad struct arc_msg_scsicmd *cmd;
320 1.8.4.2 ad uint32_t reg;
321 1.8.4.2 ad uint8_t target;
322 1.8.4.2 ad
323 1.8.4.2 ad switch (req) {
324 1.8.4.2 ad case ADAPTER_REQ_GROW_RESOURCES:
325 1.8.4.2 ad /* Not supported. */
326 1.8.4.2 ad return;
327 1.8.4.2 ad case ADAPTER_REQ_SET_XFER_MODE:
328 1.8.4.2 ad /* Not supported. */
329 1.8.4.2 ad return;
330 1.8.4.2 ad case ADAPTER_REQ_RUN_XFER:
331 1.8.4.2 ad break;
332 1.8.4.2 ad }
333 1.8.4.2 ad
334 1.8.4.2 ad mutex_spin_enter(&sc->sc_mutex);
335 1.8.4.2 ad
336 1.8.4.2 ad xs = arg;
337 1.8.4.2 ad periph = xs->xs_periph;
338 1.8.4.2 ad target = periph->periph_target;
339 1.8.4.2 ad
340 1.8.4.2 ad if (xs->cmdlen > ARC_MSG_CDBLEN) {
341 1.8.4.2 ad memset(&xs->sense, 0, sizeof(xs->sense));
342 1.8.4.2 ad xs->sense.scsi_sense.response_code = SSD_RCODE_VALID | 0x70;
343 1.8.4.2 ad xs->sense.scsi_sense.flags = SKEY_ILLEGAL_REQUEST;
344 1.8.4.2 ad xs->sense.scsi_sense.asc = 0x20;
345 1.8.4.2 ad xs->error = XS_SENSE;
346 1.8.4.2 ad xs->status = SCSI_CHECK;
347 1.8.4.2 ad mutex_spin_exit(&sc->sc_mutex);
348 1.8.4.2 ad scsipi_done(xs);
349 1.8.4.2 ad return;
350 1.8.4.2 ad }
351 1.8.4.2 ad
352 1.8.4.2 ad ccb = arc_get_ccb(sc);
353 1.8.4.2 ad if (ccb == NULL) {
354 1.8.4.2 ad xs->error = XS_RESOURCE_SHORTAGE;
355 1.8.4.2 ad mutex_spin_exit(&sc->sc_mutex);
356 1.8.4.2 ad scsipi_done(xs);
357 1.8.4.2 ad return;
358 1.8.4.2 ad }
359 1.8.4.2 ad
360 1.8.4.2 ad ccb->ccb_xs = xs;
361 1.8.4.2 ad
362 1.8.4.2 ad if (arc_load_xs(ccb) != 0) {
363 1.8.4.2 ad xs->error = XS_DRIVER_STUFFUP;
364 1.8.4.2 ad arc_put_ccb(sc, ccb);
365 1.8.4.2 ad mutex_spin_exit(&sc->sc_mutex);
366 1.8.4.2 ad scsipi_done(xs);
367 1.8.4.2 ad return;
368 1.8.4.2 ad }
369 1.8.4.2 ad
370 1.8.4.2 ad cmd = &ccb->ccb_cmd->cmd;
371 1.8.4.2 ad reg = ccb->ccb_cmd_post;
372 1.8.4.2 ad
373 1.8.4.2 ad /* bus is always 0 */
374 1.8.4.2 ad cmd->target = target;
375 1.8.4.2 ad cmd->lun = periph->periph_lun;
376 1.8.4.2 ad cmd->function = 1; /* XXX magic number */
377 1.8.4.2 ad
378 1.8.4.2 ad cmd->cdb_len = xs->cmdlen;
379 1.8.4.2 ad cmd->sgl_len = ccb->ccb_dmamap->dm_nsegs;
380 1.8.4.2 ad if (xs->xs_control & XS_CTL_DATA_OUT)
381 1.8.4.2 ad cmd->flags = ARC_MSG_SCSICMD_FLAG_WRITE;
382 1.8.4.2 ad if (ccb->ccb_dmamap->dm_nsegs > ARC_SGL_256LEN) {
383 1.8.4.2 ad cmd->flags |= ARC_MSG_SCSICMD_FLAG_SGL_BSIZE_512;
384 1.8.4.2 ad reg |= ARC_REG_POST_QUEUE_BIGFRAME;
385 1.8.4.2 ad }
386 1.8.4.2 ad
387 1.8.4.2 ad cmd->context = htole32(ccb->ccb_id);
388 1.8.4.2 ad cmd->data_len = htole32(xs->datalen);
389 1.8.4.2 ad
390 1.8.4.2 ad memcpy(cmd->cdb, xs->cmd, xs->cmdlen);
391 1.8.4.2 ad
392 1.8.4.2 ad /* we've built the command, let's put it on the hw */
393 1.8.4.2 ad bus_dmamap_sync(sc->sc_dmat, ARC_DMA_MAP(sc->sc_requests),
394 1.8.4.2 ad ccb->ccb_offset, ARC_MAX_IOCMDLEN,
395 1.8.4.2 ad BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
396 1.8.4.2 ad
397 1.8.4.2 ad arc_push(sc, reg);
398 1.8.4.2 ad if (xs->xs_control & XS_CTL_POLL) {
399 1.8.4.2 ad if (arc_complete(sc, ccb, xs->timeout) != 0) {
400 1.8.4.2 ad xs->error = XS_DRIVER_STUFFUP;
401 1.8.4.2 ad mutex_spin_exit(&sc->sc_mutex);
402 1.8.4.2 ad scsipi_done(xs);
403 1.8.4.2 ad return;
404 1.8.4.2 ad }
405 1.8.4.2 ad }
406 1.8.4.2 ad
407 1.8.4.2 ad mutex_spin_exit(&sc->sc_mutex);
408 1.8.4.2 ad }
409 1.8.4.2 ad
410 1.8.4.2 ad int
411 1.8.4.2 ad arc_load_xs(struct arc_ccb *ccb)
412 1.8.4.2 ad {
413 1.8.4.2 ad struct arc_softc *sc = ccb->ccb_sc;
414 1.8.4.2 ad struct scsipi_xfer *xs = ccb->ccb_xs;
415 1.8.4.2 ad bus_dmamap_t dmap = ccb->ccb_dmamap;
416 1.8.4.2 ad struct arc_sge *sgl = ccb->ccb_cmd->sgl, *sge;
417 1.8.4.2 ad uint64_t addr;
418 1.8.4.2 ad int i, error;
419 1.8.4.2 ad
420 1.8.4.2 ad if (xs->datalen == 0)
421 1.8.4.2 ad return 0;
422 1.8.4.2 ad
423 1.8.4.2 ad error = bus_dmamap_load(sc->sc_dmat, dmap,
424 1.8.4.2 ad xs->data, xs->datalen, NULL,
425 1.8.4.2 ad (xs->xs_control & XS_CTL_NOSLEEP) ?
426 1.8.4.2 ad BUS_DMA_NOWAIT : BUS_DMA_WAITOK);
427 1.8.4.2 ad if (error != 0) {
428 1.8.4.2 ad aprint_error("%s: error %d loading dmamap\n",
429 1.8.4.2 ad device_xname(&sc->sc_dev), error);
430 1.8.4.2 ad return 1;
431 1.8.4.2 ad }
432 1.8.4.2 ad
433 1.8.4.2 ad for (i = 0; i < dmap->dm_nsegs; i++) {
434 1.8.4.2 ad sge = &sgl[i];
435 1.8.4.2 ad
436 1.8.4.2 ad sge->sg_hdr = htole32(ARC_SGE_64BIT | dmap->dm_segs[i].ds_len);
437 1.8.4.2 ad addr = dmap->dm_segs[i].ds_addr;
438 1.8.4.2 ad sge->sg_hi_addr = htole32((uint32_t)(addr >> 32));
439 1.8.4.2 ad sge->sg_lo_addr = htole32((uint32_t)addr);
440 1.8.4.2 ad }
441 1.8.4.2 ad
442 1.8.4.2 ad bus_dmamap_sync(sc->sc_dmat, dmap, 0, dmap->dm_mapsize,
443 1.8.4.2 ad (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
444 1.8.4.2 ad BUS_DMASYNC_PREWRITE);
445 1.8.4.2 ad
446 1.8.4.2 ad return 0;
447 1.8.4.2 ad }
448 1.8.4.2 ad
449 1.8.4.2 ad void
450 1.8.4.2 ad arc_scsi_cmd_done(struct arc_softc *sc, struct arc_ccb *ccb, uint32_t reg)
451 1.8.4.2 ad {
452 1.8.4.2 ad struct scsipi_xfer *xs = ccb->ccb_xs;
453 1.8.4.2 ad struct arc_msg_scsicmd *cmd;
454 1.8.4.2 ad
455 1.8.4.2 ad if (xs->datalen != 0) {
456 1.8.4.2 ad bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap, 0,
457 1.8.4.2 ad ccb->ccb_dmamap->dm_mapsize,
458 1.8.4.2 ad (xs->xs_control & XS_CTL_DATA_IN) ?
459 1.8.4.2 ad BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
460 1.8.4.2 ad bus_dmamap_unload(sc->sc_dmat, ccb->ccb_dmamap);
461 1.8.4.2 ad }
462 1.8.4.2 ad
463 1.8.4.2 ad /* timeout_del */
464 1.8.4.2 ad xs->status |= XS_STS_DONE;
465 1.8.4.2 ad
466 1.8.4.2 ad if (reg & ARC_REG_REPLY_QUEUE_ERR) {
467 1.8.4.2 ad cmd = &ccb->ccb_cmd->cmd;
468 1.8.4.2 ad
469 1.8.4.2 ad switch (cmd->status) {
470 1.8.4.2 ad case ARC_MSG_STATUS_SELTIMEOUT:
471 1.8.4.2 ad case ARC_MSG_STATUS_ABORTED:
472 1.8.4.2 ad case ARC_MSG_STATUS_INIT_FAIL:
473 1.8.4.2 ad xs->status = SCSI_OK;
474 1.8.4.2 ad xs->error = XS_SELTIMEOUT;
475 1.8.4.2 ad break;
476 1.8.4.2 ad
477 1.8.4.2 ad case SCSI_CHECK:
478 1.8.4.2 ad memset(&xs->sense, 0, sizeof(xs->sense));
479 1.8.4.2 ad memcpy(&xs->sense, cmd->sense_data,
480 1.8.4.2 ad min(ARC_MSG_SENSELEN, sizeof(xs->sense)));
481 1.8.4.2 ad xs->sense.scsi_sense.response_code =
482 1.8.4.2 ad SSD_RCODE_VALID | 0x70;
483 1.8.4.2 ad xs->status = SCSI_CHECK;
484 1.8.4.2 ad xs->error = XS_SENSE;
485 1.8.4.2 ad xs->resid = 0;
486 1.8.4.2 ad break;
487 1.8.4.2 ad
488 1.8.4.2 ad default:
489 1.8.4.2 ad /* unknown device status */
490 1.8.4.2 ad xs->error = XS_BUSY; /* try again later? */
491 1.8.4.2 ad xs->status = SCSI_BUSY;
492 1.8.4.2 ad break;
493 1.8.4.2 ad }
494 1.8.4.2 ad } else {
495 1.8.4.2 ad xs->status = SCSI_OK;
496 1.8.4.2 ad xs->error = XS_NOERROR;
497 1.8.4.2 ad xs->resid = 0;
498 1.8.4.2 ad }
499 1.8.4.2 ad
500 1.8.4.2 ad arc_put_ccb(sc, ccb);
501 1.8.4.2 ad scsipi_done(xs);
502 1.8.4.2 ad }
503 1.8.4.2 ad
504 1.8.4.2 ad int
505 1.8.4.2 ad arc_complete(struct arc_softc *sc, struct arc_ccb *nccb, int timeout)
506 1.8.4.2 ad {
507 1.8.4.2 ad struct arc_ccb *ccb = NULL;
508 1.8.4.2 ad char *kva = ARC_DMA_KVA(sc->sc_requests);
509 1.8.4.2 ad struct arc_io_cmd *cmd;
510 1.8.4.2 ad uint32_t reg;
511 1.8.4.2 ad
512 1.8.4.2 ad do {
513 1.8.4.2 ad reg = arc_pop(sc);
514 1.8.4.2 ad if (reg == 0xffffffff) {
515 1.8.4.2 ad if (timeout-- == 0)
516 1.8.4.2 ad return 1;
517 1.8.4.2 ad
518 1.8.4.2 ad delay(1000);
519 1.8.4.2 ad continue;
520 1.8.4.2 ad }
521 1.8.4.2 ad
522 1.8.4.2 ad cmd = (struct arc_io_cmd *)(kva +
523 1.8.4.2 ad ((reg << ARC_REG_REPLY_QUEUE_ADDR_SHIFT) -
524 1.8.4.2 ad ARC_DMA_DVA(sc->sc_requests)));
525 1.8.4.2 ad ccb = &sc->sc_ccbs[htole32(cmd->cmd.context)];
526 1.8.4.2 ad
527 1.8.4.2 ad bus_dmamap_sync(sc->sc_dmat, ARC_DMA_MAP(sc->sc_requests),
528 1.8.4.2 ad ccb->ccb_offset, ARC_MAX_IOCMDLEN,
529 1.8.4.2 ad BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
530 1.8.4.2 ad
531 1.8.4.2 ad arc_scsi_cmd_done(sc, ccb, reg);
532 1.8.4.2 ad } while (nccb != ccb);
533 1.8.4.2 ad
534 1.8.4.2 ad return 0;
535 1.8.4.2 ad }
536 1.8.4.2 ad
537 1.8.4.2 ad int
538 1.8.4.2 ad arc_map_pci_resources(struct arc_softc *sc, struct pci_attach_args *pa)
539 1.8.4.2 ad {
540 1.8.4.2 ad pcireg_t memtype;
541 1.8.4.2 ad pci_intr_handle_t ih;
542 1.8.4.2 ad
543 1.8.4.2 ad sc->sc_pc = pa->pa_pc;
544 1.8.4.2 ad sc->sc_tag = pa->pa_tag;
545 1.8.4.2 ad sc->sc_dmat = pa->pa_dmat;
546 1.8.4.2 ad
547 1.8.4.2 ad memtype = pci_mapreg_type(sc->sc_pc, sc->sc_tag, ARC_PCI_BAR);
548 1.8.4.2 ad if (pci_mapreg_map(pa, ARC_PCI_BAR, memtype, 0, &sc->sc_iot,
549 1.8.4.2 ad &sc->sc_ioh, NULL, &sc->sc_ios) != 0) {
550 1.8.4.2 ad aprint_error(": unable to map system interface register\n");
551 1.8.4.2 ad return 1;
552 1.8.4.2 ad }
553 1.8.4.2 ad
554 1.8.4.2 ad if (pci_intr_map(pa, &ih) != 0) {
555 1.8.4.2 ad aprint_error(": unable to map interrupt\n");
556 1.8.4.2 ad goto unmap;
557 1.8.4.2 ad }
558 1.8.4.2 ad
559 1.8.4.2 ad sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO,
560 1.8.4.2 ad arc_intr, sc);
561 1.8.4.2 ad if (sc->sc_ih == NULL) {
562 1.8.4.2 ad aprint_error(": unable to map interrupt [2]\n");
563 1.8.4.2 ad goto unmap;
564 1.8.4.2 ad }
565 1.8.4.2 ad aprint_normal(": interrupting at %s\n",
566 1.8.4.2 ad pci_intr_string(pa->pa_pc, ih));
567 1.8.4.2 ad
568 1.8.4.2 ad return 0;
569 1.8.4.2 ad
570 1.8.4.2 ad unmap:
571 1.8.4.2 ad bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
572 1.8.4.2 ad sc->sc_ios = 0;
573 1.8.4.2 ad return 1;
574 1.8.4.2 ad }
575 1.8.4.2 ad
576 1.8.4.2 ad void
577 1.8.4.2 ad arc_unmap_pci_resources(struct arc_softc *sc)
578 1.8.4.2 ad {
579 1.8.4.2 ad pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
580 1.8.4.2 ad bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
581 1.8.4.2 ad sc->sc_ios = 0;
582 1.8.4.2 ad }
583 1.8.4.2 ad
584 1.8.4.2 ad int
585 1.8.4.2 ad arc_query_firmware(struct arc_softc *sc)
586 1.8.4.2 ad {
587 1.8.4.2 ad struct arc_msg_firmware_info fwinfo;
588 1.8.4.2 ad char string[81]; /* sizeof(vendor)*2+1 */
589 1.8.4.2 ad
590 1.8.4.2 ad if (arc_wait_eq(sc, ARC_REG_OUTB_ADDR1, ARC_REG_OUTB_ADDR1_FIRMWARE_OK,
591 1.8.4.2 ad ARC_REG_OUTB_ADDR1_FIRMWARE_OK) != 0) {
592 1.8.4.2 ad aprint_debug("%s: timeout waiting for firmware ok\n",
593 1.8.4.2 ad device_xname(&sc->sc_dev));
594 1.8.4.2 ad return 1;
595 1.8.4.2 ad }
596 1.8.4.2 ad
597 1.8.4.2 ad if (arc_msg0(sc, ARC_REG_INB_MSG0_GET_CONFIG) != 0) {
598 1.8.4.2 ad aprint_debug("%s: timeout waiting for get config\n",
599 1.8.4.2 ad device_xname(&sc->sc_dev));
600 1.8.4.2 ad return 1;
601 1.8.4.2 ad }
602 1.8.4.2 ad
603 1.8.4.2 ad if (arc_msg0(sc, ARC_REG_INB_MSG0_START_BGRB) != 0) {
604 1.8.4.2 ad aprint_debug("%s: timeout waiting to start bg rebuild\n",
605 1.8.4.2 ad device_xname(&sc->sc_dev));
606 1.8.4.2 ad return 1;
607 1.8.4.2 ad }
608 1.8.4.2 ad
609 1.8.4.2 ad arc_read_region(sc, ARC_REG_MSGBUF, &fwinfo, sizeof(fwinfo));
610 1.8.4.2 ad
611 1.8.4.2 ad DNPRINTF(ARC_D_INIT, "%s: signature: 0x%08x\n",
612 1.8.4.2 ad device_xname(&sc->sc_dev), htole32(fwinfo.signature));
613 1.8.4.2 ad
614 1.8.4.2 ad if (htole32(fwinfo.signature) != ARC_FWINFO_SIGNATURE_GET_CONFIG) {
615 1.8.4.2 ad aprint_error("%s: invalid firmware info from iop\n",
616 1.8.4.2 ad device_xname(&sc->sc_dev));
617 1.8.4.2 ad return 1;
618 1.8.4.2 ad }
619 1.8.4.2 ad
620 1.8.4.2 ad DNPRINTF(ARC_D_INIT, "%s: request_len: %d\n",
621 1.8.4.2 ad device_xname(&sc->sc_dev),
622 1.8.4.2 ad htole32(fwinfo.request_len));
623 1.8.4.2 ad DNPRINTF(ARC_D_INIT, "%s: queue_len: %d\n",
624 1.8.4.2 ad device_xname(&sc->sc_dev),
625 1.8.4.2 ad htole32(fwinfo.queue_len));
626 1.8.4.2 ad DNPRINTF(ARC_D_INIT, "%s: sdram_size: %d\n",
627 1.8.4.2 ad device_xname(&sc->sc_dev),
628 1.8.4.2 ad htole32(fwinfo.sdram_size));
629 1.8.4.2 ad DNPRINTF(ARC_D_INIT, "%s: sata_ports: %d\n",
630 1.8.4.2 ad device_xname(&sc->sc_dev),
631 1.8.4.2 ad htole32(fwinfo.sata_ports));
632 1.8.4.2 ad
633 1.8.4.2 ad scsipi_strvis(string, 81, fwinfo.vendor, sizeof(fwinfo.vendor));
634 1.8.4.2 ad DNPRINTF(ARC_D_INIT, "%s: vendor: \"%s\"\n",
635 1.8.4.2 ad device_xname(&sc->sc_dev), string);
636 1.8.4.2 ad
637 1.8.4.2 ad scsipi_strvis(string, 17, fwinfo.model, sizeof(fwinfo.model));
638 1.8.4.2 ad
639 1.8.4.2 ad aprint_normal("%s: Areca %s Host Adapter RAID controller\n",
640 1.8.4.2 ad device_xname(&sc->sc_dev), string);
641 1.8.4.2 ad
642 1.8.4.2 ad scsipi_strvis(string, 33, fwinfo.fw_version, sizeof(fwinfo.fw_version));
643 1.8.4.2 ad DNPRINTF(ARC_D_INIT, "%s: version: \"%s\"\n",
644 1.8.4.2 ad device_xname(&sc->sc_dev), string);
645 1.8.4.2 ad
646 1.8.4.2 ad if (htole32(fwinfo.request_len) != ARC_MAX_IOCMDLEN) {
647 1.8.4.2 ad aprint_error("%s: unexpected request frame size (%d != %d)\n",
648 1.8.4.2 ad device_xname(&sc->sc_dev),
649 1.8.4.2 ad htole32(fwinfo.request_len), ARC_MAX_IOCMDLEN);
650 1.8.4.2 ad return 1;
651 1.8.4.2 ad }
652 1.8.4.2 ad
653 1.8.4.2 ad sc->sc_req_count = htole32(fwinfo.queue_len);
654 1.8.4.2 ad
655 1.8.4.2 ad aprint_normal("%s: %d ports, %dMB SDRAM, firmware <%s>\n",
656 1.8.4.2 ad device_xname(&sc->sc_dev), htole32(fwinfo.sata_ports),
657 1.8.4.2 ad htole32(fwinfo.sdram_size), string);
658 1.8.4.2 ad
659 1.8.4.2 ad return 0;
660 1.8.4.2 ad }
661 1.8.4.2 ad
662 1.8.4.2 ad #if NBIO > 0
663 1.8.4.2 ad static int
664 1.8.4.2 ad arc_bioctl(struct device *self, u_long cmd, void *addr)
665 1.8.4.2 ad {
666 1.8.4.2 ad struct arc_softc *sc = device_private(self);
667 1.8.4.2 ad int error = 0;
668 1.8.4.2 ad
669 1.8.4.2 ad switch (cmd) {
670 1.8.4.2 ad case BIOCINQ:
671 1.8.4.2 ad error = arc_bio_inq(sc, (struct bioc_inq *)addr);
672 1.8.4.2 ad break;
673 1.8.4.2 ad
674 1.8.4.2 ad case BIOCVOL:
675 1.8.4.2 ad error = arc_bio_vol(sc, (struct bioc_vol *)addr);
676 1.8.4.2 ad break;
677 1.8.4.2 ad
678 1.8.4.2 ad case BIOCDISK:
679 1.8.4.2 ad error = arc_bio_disk(sc, (struct bioc_disk *)addr);
680 1.8.4.2 ad break;
681 1.8.4.2 ad
682 1.8.4.2 ad case BIOCALARM:
683 1.8.4.2 ad error = arc_bio_alarm(sc, (struct bioc_alarm *)addr);
684 1.8.4.2 ad break;
685 1.8.4.2 ad
686 1.8.4.2 ad default:
687 1.8.4.2 ad error = ENOTTY;
688 1.8.4.2 ad break;
689 1.8.4.2 ad }
690 1.8.4.2 ad
691 1.8.4.2 ad return error;
692 1.8.4.2 ad }
693 1.8.4.2 ad
694 1.8.4.2 ad static int
695 1.8.4.2 ad arc_bio_alarm(struct arc_softc *sc, struct bioc_alarm *ba)
696 1.8.4.2 ad {
697 1.8.4.2 ad uint8_t request[2], reply[1];
698 1.8.4.2 ad size_t len;
699 1.8.4.2 ad int error = 0;
700 1.8.4.2 ad
701 1.8.4.2 ad switch (ba->ba_opcode) {
702 1.8.4.2 ad case BIOC_SAENABLE:
703 1.8.4.2 ad case BIOC_SADISABLE:
704 1.8.4.2 ad request[0] = ARC_FW_SET_ALARM;
705 1.8.4.2 ad request[1] = (ba->ba_opcode == BIOC_SAENABLE) ?
706 1.8.4.2 ad ARC_FW_SET_ALARM_ENABLE : ARC_FW_SET_ALARM_DISABLE;
707 1.8.4.2 ad len = sizeof(request);
708 1.8.4.2 ad
709 1.8.4.2 ad break;
710 1.8.4.2 ad
711 1.8.4.2 ad case BIOC_SASILENCE:
712 1.8.4.2 ad request[0] = ARC_FW_MUTE_ALARM;
713 1.8.4.2 ad len = 1;
714 1.8.4.2 ad
715 1.8.4.2 ad break;
716 1.8.4.2 ad
717 1.8.4.2 ad case BIOC_GASTATUS:
718 1.8.4.2 ad /* system info is too big/ugly to deal with here */
719 1.8.4.2 ad return arc_bio_alarm_state(sc, ba);
720 1.8.4.2 ad
721 1.8.4.2 ad default:
722 1.8.4.2 ad return EOPNOTSUPP;
723 1.8.4.2 ad }
724 1.8.4.2 ad
725 1.8.4.2 ad error = arc_msgbuf(sc, request, len, reply, sizeof(reply));
726 1.8.4.2 ad if (error != 0)
727 1.8.4.2 ad return error;
728 1.8.4.2 ad
729 1.8.4.2 ad switch (reply[0]) {
730 1.8.4.2 ad case ARC_FW_CMD_OK:
731 1.8.4.2 ad return 0;
732 1.8.4.2 ad case ARC_FW_CMD_PASS_REQD:
733 1.8.4.2 ad return EPERM;
734 1.8.4.2 ad default:
735 1.8.4.2 ad return EIO;
736 1.8.4.2 ad }
737 1.8.4.2 ad }
738 1.8.4.2 ad
739 1.8.4.2 ad static int
740 1.8.4.2 ad arc_bio_alarm_state(struct arc_softc *sc, struct bioc_alarm *ba)
741 1.8.4.2 ad {
742 1.8.4.2 ad uint8_t request = ARC_FW_SYSINFO;
743 1.8.4.2 ad struct arc_fw_sysinfo *sysinfo;
744 1.8.4.2 ad int error = 0;
745 1.8.4.2 ad
746 1.8.4.2 ad sysinfo = kmem_zalloc(sizeof(struct arc_fw_sysinfo), KM_SLEEP);
747 1.8.4.2 ad
748 1.8.4.2 ad request = ARC_FW_SYSINFO;
749 1.8.4.2 ad error = arc_msgbuf(sc, &request, sizeof(request),
750 1.8.4.2 ad sysinfo, sizeof(struct arc_fw_sysinfo));
751 1.8.4.2 ad
752 1.8.4.2 ad if (error != 0)
753 1.8.4.2 ad goto out;
754 1.8.4.2 ad
755 1.8.4.2 ad ba->ba_status = sysinfo->alarm;
756 1.8.4.2 ad
757 1.8.4.2 ad out:
758 1.8.4.2 ad kmem_free(sysinfo, sizeof(*sysinfo));
759 1.8.4.2 ad return error;
760 1.8.4.2 ad }
761 1.8.4.2 ad
762 1.8.4.2 ad
763 1.8.4.2 ad static int
764 1.8.4.2 ad arc_bio_inq(struct arc_softc *sc, struct bioc_inq *bi)
765 1.8.4.2 ad {
766 1.8.4.2 ad uint8_t request[2];
767 1.8.4.2 ad struct arc_fw_sysinfo *sysinfo;
768 1.8.4.2 ad struct arc_fw_volinfo *volinfo;
769 1.8.4.2 ad int maxvols, nvols = 0, i;
770 1.8.4.2 ad int error = 0;
771 1.8.4.2 ad
772 1.8.4.2 ad sysinfo = kmem_zalloc(sizeof(struct arc_fw_sysinfo), KM_SLEEP);
773 1.8.4.2 ad volinfo = kmem_zalloc(sizeof(struct arc_fw_volinfo), KM_SLEEP);
774 1.8.4.2 ad
775 1.8.4.2 ad request[0] = ARC_FW_SYSINFO;
776 1.8.4.2 ad error = arc_msgbuf(sc, request, 1, sysinfo,
777 1.8.4.2 ad sizeof(struct arc_fw_sysinfo));
778 1.8.4.2 ad if (error != 0)
779 1.8.4.2 ad goto out;
780 1.8.4.2 ad
781 1.8.4.2 ad maxvols = sysinfo->max_volume_set;
782 1.8.4.2 ad
783 1.8.4.2 ad request[0] = ARC_FW_VOLINFO;
784 1.8.4.2 ad for (i = 0; i < maxvols; i++) {
785 1.8.4.2 ad request[1] = i;
786 1.8.4.2 ad error = arc_msgbuf(sc, request, sizeof(request), volinfo,
787 1.8.4.2 ad sizeof(struct arc_fw_volinfo));
788 1.8.4.2 ad if (error != 0)
789 1.8.4.2 ad goto out;
790 1.8.4.2 ad
791 1.8.4.2 ad /*
792 1.8.4.2 ad * I can't find an easy way to see if the volume exists or not
793 1.8.4.2 ad * except to say that if it has no capacity then it isn't there.
794 1.8.4.2 ad * Ignore passthru volumes, bioc_vol doesn't understand them.
795 1.8.4.2 ad */
796 1.8.4.2 ad if ((volinfo->capacity != 0 || volinfo->capacity2 != 0) &&
797 1.8.4.2 ad volinfo->raid_level != ARC_FW_VOL_RAIDLEVEL_PASSTHRU)
798 1.8.4.2 ad nvols++;
799 1.8.4.2 ad }
800 1.8.4.2 ad
801 1.8.4.2 ad strlcpy(bi->bi_dev, device_xname(&sc->sc_dev), sizeof(bi->bi_dev));
802 1.8.4.2 ad bi->bi_novol = nvols;
803 1.8.4.2 ad out:
804 1.8.4.2 ad kmem_free(volinfo, sizeof(*volinfo));
805 1.8.4.2 ad kmem_free(sysinfo, sizeof(*sysinfo));
806 1.8.4.2 ad return error;
807 1.8.4.2 ad }
808 1.8.4.2 ad
809 1.8.4.2 ad static int
810 1.8.4.2 ad arc_bio_getvol(struct arc_softc *sc, int vol, struct arc_fw_volinfo *volinfo)
811 1.8.4.2 ad {
812 1.8.4.2 ad uint8_t request[2];
813 1.8.4.2 ad struct arc_fw_sysinfo *sysinfo;
814 1.8.4.2 ad int error = 0;
815 1.8.4.2 ad int maxvols, nvols = 0, i;
816 1.8.4.2 ad
817 1.8.4.2 ad sysinfo = kmem_zalloc(sizeof(struct arc_fw_sysinfo), KM_SLEEP);
818 1.8.4.2 ad
819 1.8.4.2 ad request[0] = ARC_FW_SYSINFO;
820 1.8.4.2 ad
821 1.8.4.2 ad error = arc_msgbuf(sc, request, 1, sysinfo,
822 1.8.4.2 ad sizeof(struct arc_fw_sysinfo));
823 1.8.4.2 ad if (error != 0)
824 1.8.4.2 ad goto out;
825 1.8.4.2 ad
826 1.8.4.2 ad maxvols = sysinfo->max_volume_set;
827 1.8.4.2 ad
828 1.8.4.2 ad request[0] = ARC_FW_VOLINFO;
829 1.8.4.2 ad for (i = 0; i < maxvols; i++) {
830 1.8.4.2 ad request[1] = i;
831 1.8.4.2 ad error = arc_msgbuf(sc, request, sizeof(request), volinfo,
832 1.8.4.2 ad sizeof(struct arc_fw_volinfo));
833 1.8.4.2 ad if (error != 0)
834 1.8.4.2 ad goto out;
835 1.8.4.2 ad
836 1.8.4.2 ad if ((volinfo->capacity == 0 && volinfo->capacity2 == 0) ||
837 1.8.4.2 ad volinfo->raid_level == ARC_FW_VOL_RAIDLEVEL_PASSTHRU)
838 1.8.4.2 ad continue;
839 1.8.4.2 ad
840 1.8.4.2 ad if (nvols == vol)
841 1.8.4.2 ad break;
842 1.8.4.2 ad
843 1.8.4.2 ad nvols++;
844 1.8.4.2 ad }
845 1.8.4.2 ad
846 1.8.4.2 ad if (nvols != vol ||
847 1.8.4.2 ad (volinfo->capacity == 0 && volinfo->capacity2 == 0) ||
848 1.8.4.2 ad volinfo->raid_level == ARC_FW_VOL_RAIDLEVEL_PASSTHRU) {
849 1.8.4.2 ad error = ENODEV;
850 1.8.4.2 ad goto out;
851 1.8.4.2 ad }
852 1.8.4.2 ad
853 1.8.4.2 ad out:
854 1.8.4.2 ad kmem_free(sysinfo, sizeof(*sysinfo));
855 1.8.4.2 ad return error;
856 1.8.4.2 ad }
857 1.8.4.2 ad
858 1.8.4.2 ad static int
859 1.8.4.2 ad arc_bio_vol(struct arc_softc *sc, struct bioc_vol *bv)
860 1.8.4.2 ad {
861 1.8.4.2 ad struct arc_fw_volinfo *volinfo;
862 1.8.4.2 ad uint64_t blocks;
863 1.8.4.2 ad uint32_t status;
864 1.8.4.2 ad int error = 0;
865 1.8.4.2 ad
866 1.8.4.2 ad volinfo = kmem_zalloc(sizeof(struct arc_fw_volinfo), KM_SLEEP);
867 1.8.4.2 ad
868 1.8.4.2 ad error = arc_bio_getvol(sc, bv->bv_volid, volinfo);
869 1.8.4.2 ad if (error != 0)
870 1.8.4.2 ad goto out;
871 1.8.4.2 ad
872 1.8.4.2 ad bv->bv_percent = -1;
873 1.8.4.2 ad bv->bv_seconds = 0;
874 1.8.4.2 ad
875 1.8.4.2 ad status = htole32(volinfo->volume_status);
876 1.8.4.2 ad if (status == 0x0) {
877 1.8.4.2 ad if (htole32(volinfo->fail_mask) == 0x0)
878 1.8.4.2 ad bv->bv_status = BIOC_SVONLINE;
879 1.8.4.2 ad else
880 1.8.4.2 ad bv->bv_status = BIOC_SVDEGRADED;
881 1.8.4.2 ad } else if (status & ARC_FW_VOL_STATUS_NEED_REGEN) {
882 1.8.4.2 ad bv->bv_status = BIOC_SVDEGRADED;
883 1.8.4.2 ad } else if (status & ARC_FW_VOL_STATUS_FAILED) {
884 1.8.4.2 ad bv->bv_status = BIOC_SVOFFLINE;
885 1.8.4.2 ad } else if (status & ARC_FW_VOL_STATUS_INITTING) {
886 1.8.4.2 ad bv->bv_status = BIOC_SVBUILDING;
887 1.8.4.2 ad bv->bv_percent = htole32(volinfo->progress) / 10;
888 1.8.4.2 ad } else if (status & ARC_FW_VOL_STATUS_REBUILDING) {
889 1.8.4.2 ad bv->bv_status = BIOC_SVREBUILD;
890 1.8.4.2 ad bv->bv_percent = htole32(volinfo->progress) / 10;
891 1.8.4.2 ad } else if (status & ARC_FW_VOL_STATUS_MIGRATING) {
892 1.8.4.2 ad bv->bv_status = BIOC_SVMIGRATING;
893 1.8.4.2 ad bv->bv_percent = htole32(volinfo->progress) / 10;
894 1.8.4.2 ad }
895 1.8.4.2 ad
896 1.8.4.2 ad blocks = (uint64_t)htole32(volinfo->capacity2) << 32;
897 1.8.4.2 ad blocks += (uint64_t)htole32(volinfo->capacity);
898 1.8.4.2 ad bv->bv_size = blocks * ARC_BLOCKSIZE; /* XXX */
899 1.8.4.2 ad
900 1.8.4.2 ad switch (volinfo->raid_level) {
901 1.8.4.2 ad case ARC_FW_VOL_RAIDLEVEL_0:
902 1.8.4.2 ad bv->bv_level = 0;
903 1.8.4.2 ad break;
904 1.8.4.2 ad case ARC_FW_VOL_RAIDLEVEL_1:
905 1.8.4.2 ad bv->bv_level = 1;
906 1.8.4.2 ad break;
907 1.8.4.2 ad case ARC_FW_VOL_RAIDLEVEL_3:
908 1.8.4.2 ad bv->bv_level = 3;
909 1.8.4.2 ad break;
910 1.8.4.2 ad case ARC_FW_VOL_RAIDLEVEL_5:
911 1.8.4.2 ad bv->bv_level = 5;
912 1.8.4.2 ad break;
913 1.8.4.2 ad case ARC_FW_VOL_RAIDLEVEL_6:
914 1.8.4.2 ad bv->bv_level = 6;
915 1.8.4.2 ad break;
916 1.8.4.2 ad case ARC_FW_VOL_RAIDLEVEL_PASSTHRU:
917 1.8.4.2 ad default:
918 1.8.4.2 ad bv->bv_level = -1;
919 1.8.4.2 ad break;
920 1.8.4.2 ad }
921 1.8.4.2 ad
922 1.8.4.2 ad bv->bv_nodisk = volinfo->member_disks;
923 1.8.4.2 ad strlcpy(bv->bv_dev, volinfo->set_name, sizeof(bv->bv_dev));
924 1.8.4.2 ad
925 1.8.4.2 ad out:
926 1.8.4.2 ad kmem_free(volinfo, sizeof(*volinfo));
927 1.8.4.2 ad return error;
928 1.8.4.2 ad }
929 1.8.4.2 ad
930 1.8.4.2 ad static int
931 1.8.4.2 ad arc_bio_disk(struct arc_softc *sc, struct bioc_disk *bd)
932 1.8.4.2 ad {
933 1.8.4.2 ad uint8_t request[2];
934 1.8.4.2 ad struct arc_fw_volinfo *volinfo;
935 1.8.4.2 ad struct arc_fw_raidinfo *raidinfo;
936 1.8.4.2 ad struct arc_fw_diskinfo *diskinfo;
937 1.8.4.2 ad int error = 0;
938 1.8.4.2 ad uint64_t blocks;
939 1.8.4.2 ad char model[81];
940 1.8.4.2 ad char serial[41];
941 1.8.4.2 ad char rev[17];
942 1.8.4.2 ad
943 1.8.4.2 ad volinfo = kmem_zalloc(sizeof(struct arc_fw_volinfo), KM_SLEEP);
944 1.8.4.2 ad raidinfo = kmem_zalloc(sizeof(struct arc_fw_raidinfo), KM_SLEEP);
945 1.8.4.2 ad diskinfo = kmem_zalloc(sizeof(struct arc_fw_diskinfo), KM_SLEEP);
946 1.8.4.2 ad
947 1.8.4.2 ad error = arc_bio_getvol(sc, bd->bd_volid, volinfo);
948 1.8.4.2 ad if (error != 0)
949 1.8.4.2 ad goto out;
950 1.8.4.2 ad
951 1.8.4.2 ad request[0] = ARC_FW_RAIDINFO;
952 1.8.4.2 ad request[1] = volinfo->raid_set_number;
953 1.8.4.2 ad
954 1.8.4.2 ad error = arc_msgbuf(sc, request, sizeof(request), raidinfo,
955 1.8.4.2 ad sizeof(struct arc_fw_raidinfo));
956 1.8.4.2 ad if (error != 0)
957 1.8.4.2 ad goto out;
958 1.8.4.2 ad
959 1.8.4.2 ad if (bd->bd_diskid > raidinfo->member_devices) {
960 1.8.4.2 ad error = ENODEV;
961 1.8.4.2 ad goto out;
962 1.8.4.2 ad }
963 1.8.4.2 ad
964 1.8.4.2 ad if (raidinfo->device_array[bd->bd_diskid] == 0xff) {
965 1.8.4.2 ad /*
966 1.8.4.2 ad * the disk doesn't exist anymore. bio is too dumb to be
967 1.8.4.2 ad * able to display that, so put it on another bus
968 1.8.4.2 ad */
969 1.8.4.2 ad bd->bd_channel = 1;
970 1.8.4.2 ad bd->bd_target = 0;
971 1.8.4.2 ad bd->bd_lun = 0;
972 1.8.4.2 ad bd->bd_status = BIOC_SDOFFLINE;
973 1.8.4.2 ad strlcpy(bd->bd_vendor, "disk missing", sizeof(bd->bd_vendor));
974 1.8.4.2 ad goto out;
975 1.8.4.2 ad }
976 1.8.4.2 ad
977 1.8.4.2 ad request[0] = ARC_FW_DISKINFO;
978 1.8.4.2 ad request[1] = raidinfo->device_array[bd->bd_diskid];
979 1.8.4.2 ad error = arc_msgbuf(sc, request, sizeof(request), diskinfo,
980 1.8.4.2 ad sizeof(struct arc_fw_diskinfo));
981 1.8.4.2 ad if (error != 0)
982 1.8.4.2 ad goto out;
983 1.8.4.2 ad
984 1.8.4.2 ad #if 0
985 1.8.4.2 ad bd->bd_channel = diskinfo->scsi_attr.channel;
986 1.8.4.2 ad bd->bd_target = diskinfo->scsi_attr.target;
987 1.8.4.2 ad bd->bd_lun = diskinfo->scsi_attr.lun;
988 1.8.4.2 ad #endif
989 1.8.4.2 ad /*
990 1.8.4.2 ad * the firwmare doesnt seem to fill scsi_attr in, so fake it with
991 1.8.4.2 ad * the diskid.
992 1.8.4.2 ad */
993 1.8.4.2 ad bd->bd_channel = 0;
994 1.8.4.2 ad bd->bd_target = raidinfo->device_array[bd->bd_diskid];
995 1.8.4.2 ad bd->bd_lun = 0;
996 1.8.4.2 ad
997 1.8.4.2 ad bd->bd_status = BIOC_SDONLINE;
998 1.8.4.2 ad blocks = (uint64_t)htole32(diskinfo->capacity2) << 32;
999 1.8.4.2 ad blocks += (uint64_t)htole32(diskinfo->capacity);
1000 1.8.4.2 ad bd->bd_size = blocks * ARC_BLOCKSIZE; /* XXX */
1001 1.8.4.2 ad
1002 1.8.4.2 ad scsipi_strvis(model, 81, diskinfo->model, sizeof(diskinfo->model));
1003 1.8.4.2 ad scsipi_strvis(serial, 41, diskinfo->serial, sizeof(diskinfo->serial));
1004 1.8.4.2 ad scsipi_strvis(rev, 17, diskinfo->firmware_rev,
1005 1.8.4.2 ad sizeof(diskinfo->firmware_rev));
1006 1.8.4.2 ad
1007 1.8.4.2 ad snprintf(bd->bd_vendor, sizeof(bd->bd_vendor), "%s %s", model, rev);
1008 1.8.4.2 ad strlcpy(bd->bd_serial, serial, sizeof(bd->bd_serial));
1009 1.8.4.2 ad
1010 1.8.4.2 ad out:
1011 1.8.4.2 ad kmem_free(diskinfo, sizeof(*diskinfo));
1012 1.8.4.2 ad kmem_free(raidinfo, sizeof(*raidinfo));
1013 1.8.4.2 ad kmem_free(volinfo, sizeof(*volinfo));
1014 1.8.4.2 ad return error;
1015 1.8.4.2 ad }
1016 1.8.4.2 ad #endif /* NBIO > 0 */
1017 1.8.4.2 ad
1018 1.8.4.2 ad uint8_t
1019 1.8.4.2 ad arc_msg_cksum(void *cmd, uint16_t len)
1020 1.8.4.2 ad {
1021 1.8.4.2 ad uint8_t *buf = cmd;
1022 1.8.4.2 ad uint8_t cksum;
1023 1.8.4.2 ad int i;
1024 1.8.4.2 ad
1025 1.8.4.2 ad cksum = (uint8_t)(len >> 8) + (uint8_t)len;
1026 1.8.4.2 ad for (i = 0; i < len; i++)
1027 1.8.4.2 ad cksum += buf[i];
1028 1.8.4.2 ad
1029 1.8.4.2 ad return cksum;
1030 1.8.4.2 ad }
1031 1.8.4.2 ad
1032 1.8.4.2 ad
1033 1.8.4.2 ad int
1034 1.8.4.2 ad arc_msgbuf(struct arc_softc *sc, void *wptr, size_t wbuflen, void *rptr,
1035 1.8.4.2 ad size_t rbuflen)
1036 1.8.4.2 ad {
1037 1.8.4.2 ad uint8_t rwbuf[ARC_REG_IOC_RWBUF_MAXLEN];
1038 1.8.4.2 ad uint8_t *wbuf, *rbuf;
1039 1.8.4.2 ad int wlen, wdone = 0, rlen, rdone = 0;
1040 1.8.4.2 ad struct arc_fw_bufhdr *bufhdr;
1041 1.8.4.2 ad uint32_t reg, rwlen;
1042 1.8.4.2 ad int error = 0;
1043 1.8.4.2 ad #ifdef ARC_DEBUG
1044 1.8.4.2 ad int i;
1045 1.8.4.2 ad #endif
1046 1.8.4.2 ad
1047 1.8.4.2 ad wbuf = rbuf = NULL;
1048 1.8.4.2 ad
1049 1.8.4.2 ad DNPRINTF(ARC_D_DB, "%s: arc_msgbuf wbuflen: %d rbuflen: %d\n",
1050 1.8.4.2 ad device_xname(&sc->sc_dev), wbuflen, rbuflen);
1051 1.8.4.2 ad
1052 1.8.4.2 ad wlen = sizeof(struct arc_fw_bufhdr) + wbuflen + 1; /* 1 for cksum */
1053 1.8.4.2 ad wbuf = kmem_alloc(wlen, KM_SLEEP);
1054 1.8.4.2 ad
1055 1.8.4.2 ad rlen = sizeof(struct arc_fw_bufhdr) + rbuflen + 1; /* 1 for cksum */
1056 1.8.4.2 ad rbuf = kmem_alloc(rlen, KM_SLEEP);
1057 1.8.4.2 ad
1058 1.8.4.2 ad DNPRINTF(ARC_D_DB, "%s: arc_msgbuf wlen: %d rlen: %d\n",
1059 1.8.4.2 ad device_xname(&sc->sc_dev), wlen, rlen);
1060 1.8.4.2 ad
1061 1.8.4.2 ad bufhdr = (struct arc_fw_bufhdr *)wbuf;
1062 1.8.4.2 ad bufhdr->hdr = arc_fw_hdr;
1063 1.8.4.2 ad bufhdr->len = htole16(wbuflen);
1064 1.8.4.2 ad memcpy(wbuf + sizeof(struct arc_fw_bufhdr), wptr, wbuflen);
1065 1.8.4.2 ad wbuf[wlen - 1] = arc_msg_cksum(wptr, wbuflen);
1066 1.8.4.2 ad
1067 1.8.4.2 ad arc_lock(sc);
1068 1.8.4.2 ad if (arc_read(sc, ARC_REG_OUTB_DOORBELL) != 0) {
1069 1.8.4.2 ad error = EBUSY;
1070 1.8.4.2 ad goto out;
1071 1.8.4.2 ad }
1072 1.8.4.2 ad
1073 1.8.4.2 ad reg = ARC_REG_OUTB_DOORBELL_READ_OK;
1074 1.8.4.2 ad
1075 1.8.4.2 ad do {
1076 1.8.4.2 ad if ((reg & ARC_REG_OUTB_DOORBELL_READ_OK) && wdone < wlen) {
1077 1.8.4.2 ad memset(rwbuf, 0, sizeof(rwbuf));
1078 1.8.4.2 ad rwlen = (wlen - wdone) % sizeof(rwbuf);
1079 1.8.4.2 ad memcpy(rwbuf, &wbuf[wdone], rwlen);
1080 1.8.4.2 ad
1081 1.8.4.2 ad #ifdef ARC_DEBUG
1082 1.8.4.2 ad if (arcdebug & ARC_D_DB) {
1083 1.8.4.2 ad printf("%s: write %d:",
1084 1.8.4.2 ad device_xname(&sc->sc_dev), rwlen);
1085 1.8.4.2 ad for (i = 0; i < rwlen; i++)
1086 1.8.4.2 ad printf(" 0x%02x", rwbuf[i]);
1087 1.8.4.2 ad printf("\n");
1088 1.8.4.2 ad }
1089 1.8.4.2 ad #endif
1090 1.8.4.2 ad
1091 1.8.4.2 ad /* copy the chunk to the hw */
1092 1.8.4.2 ad arc_write(sc, ARC_REG_IOC_WBUF_LEN, rwlen);
1093 1.8.4.2 ad arc_write_region(sc, ARC_REG_IOC_WBUF, rwbuf,
1094 1.8.4.2 ad sizeof(rwbuf));
1095 1.8.4.2 ad
1096 1.8.4.2 ad /* say we have a buffer for the hw */
1097 1.8.4.2 ad arc_write(sc, ARC_REG_INB_DOORBELL,
1098 1.8.4.2 ad ARC_REG_INB_DOORBELL_WRITE_OK);
1099 1.8.4.2 ad
1100 1.8.4.2 ad wdone += rwlen;
1101 1.8.4.2 ad }
1102 1.8.4.2 ad
1103 1.8.4.2 ad while ((reg = arc_read(sc, ARC_REG_OUTB_DOORBELL)) == 0)
1104 1.8.4.2 ad arc_wait(sc);
1105 1.8.4.2 ad arc_write(sc, ARC_REG_OUTB_DOORBELL, reg);
1106 1.8.4.2 ad
1107 1.8.4.2 ad DNPRINTF(ARC_D_DB, "%s: reg: 0x%08x\n",
1108 1.8.4.2 ad device_xname(&sc->sc_dev), reg);
1109 1.8.4.2 ad
1110 1.8.4.2 ad if ((reg & ARC_REG_OUTB_DOORBELL_WRITE_OK) && rdone < rlen) {
1111 1.8.4.2 ad rwlen = arc_read(sc, ARC_REG_IOC_RBUF_LEN);
1112 1.8.4.2 ad if (rwlen > sizeof(rwbuf)) {
1113 1.8.4.2 ad DNPRINTF(ARC_D_DB, "%s: rwlen too big\n",
1114 1.8.4.2 ad device_xname(&sc->sc_dev));
1115 1.8.4.2 ad error = EIO;
1116 1.8.4.2 ad goto out;
1117 1.8.4.2 ad }
1118 1.8.4.2 ad
1119 1.8.4.2 ad arc_read_region(sc, ARC_REG_IOC_RBUF, rwbuf,
1120 1.8.4.2 ad sizeof(rwbuf));
1121 1.8.4.2 ad
1122 1.8.4.2 ad arc_write(sc, ARC_REG_INB_DOORBELL,
1123 1.8.4.2 ad ARC_REG_INB_DOORBELL_READ_OK);
1124 1.8.4.2 ad
1125 1.8.4.2 ad #ifdef ARC_DEBUG
1126 1.8.4.2 ad printf("%s: len: %d+%d=%d/%d\n",
1127 1.8.4.2 ad device_xname(&sc->sc_dev),
1128 1.8.4.2 ad rwlen, rdone, rwlen + rdone, rlen);
1129 1.8.4.2 ad if (arcdebug & ARC_D_DB) {
1130 1.8.4.2 ad printf("%s: read:",
1131 1.8.4.2 ad device_xname(&sc->sc_dev));
1132 1.8.4.2 ad for (i = 0; i < rwlen; i++)
1133 1.8.4.2 ad printf(" 0x%02x", rwbuf[i]);
1134 1.8.4.2 ad printf("\n");
1135 1.8.4.2 ad }
1136 1.8.4.2 ad #endif
1137 1.8.4.2 ad
1138 1.8.4.2 ad if ((rdone + rwlen) > rlen) {
1139 1.8.4.2 ad DNPRINTF(ARC_D_DB, "%s: rwbuf too big\n",
1140 1.8.4.2 ad device_xname(&sc->sc_dev));
1141 1.8.4.2 ad error = EIO;
1142 1.8.4.2 ad goto out;
1143 1.8.4.2 ad }
1144 1.8.4.2 ad
1145 1.8.4.2 ad memcpy(&rbuf[rdone], rwbuf, rwlen);
1146 1.8.4.2 ad rdone += rwlen;
1147 1.8.4.2 ad }
1148 1.8.4.2 ad } while (rdone != rlen);
1149 1.8.4.2 ad
1150 1.8.4.2 ad bufhdr = (struct arc_fw_bufhdr *)rbuf;
1151 1.8.4.2 ad if (memcmp(&bufhdr->hdr, &arc_fw_hdr, sizeof(bufhdr->hdr)) != 0 ||
1152 1.8.4.2 ad bufhdr->len != htole16(rbuflen)) {
1153 1.8.4.2 ad DNPRINTF(ARC_D_DB, "%s: rbuf hdr is wrong\n",
1154 1.8.4.2 ad device_xname(&sc->sc_dev));
1155 1.8.4.2 ad error = EIO;
1156 1.8.4.2 ad goto out;
1157 1.8.4.2 ad }
1158 1.8.4.2 ad
1159 1.8.4.2 ad memcpy(rptr, rbuf + sizeof(struct arc_fw_bufhdr), rbuflen);
1160 1.8.4.2 ad
1161 1.8.4.2 ad if (rbuf[rlen - 1] != arc_msg_cksum(rptr, rbuflen)) {
1162 1.8.4.2 ad DNPRINTF(ARC_D_DB, "%s: invalid cksum\n",
1163 1.8.4.2 ad device_xname(&sc->sc_dev));
1164 1.8.4.2 ad error = EIO;
1165 1.8.4.2 ad goto out;
1166 1.8.4.2 ad }
1167 1.8.4.2 ad
1168 1.8.4.2 ad out:
1169 1.8.4.2 ad arc_unlock(sc);
1170 1.8.4.2 ad kmem_free(wbuf, wlen);
1171 1.8.4.2 ad kmem_free(rbuf, rlen);
1172 1.8.4.2 ad
1173 1.8.4.2 ad return error;
1174 1.8.4.2 ad }
1175 1.8.4.2 ad
1176 1.8.4.2 ad void
1177 1.8.4.2 ad arc_lock(struct arc_softc *sc)
1178 1.8.4.2 ad {
1179 1.8.4.2 ad rw_enter(&sc->sc_rwlock, RW_WRITER);
1180 1.8.4.2 ad mutex_spin_enter(&sc->sc_mutex);
1181 1.8.4.2 ad arc_write(sc, ARC_REG_INTRMASK, ~ARC_REG_INTRMASK_POSTQUEUE);
1182 1.8.4.2 ad sc->sc_talking = 1;
1183 1.8.4.2 ad }
1184 1.8.4.2 ad
1185 1.8.4.2 ad void
1186 1.8.4.2 ad arc_unlock(struct arc_softc *sc)
1187 1.8.4.2 ad {
1188 1.8.4.2 ad KASSERT(mutex_owned(&sc->sc_mutex));
1189 1.8.4.2 ad
1190 1.8.4.2 ad sc->sc_talking = 0;
1191 1.8.4.2 ad arc_write(sc, ARC_REG_INTRMASK,
1192 1.8.4.2 ad ~(ARC_REG_INTRMASK_POSTQUEUE|ARC_REG_INTRMASK_DOORBELL));
1193 1.8.4.2 ad mutex_spin_exit(&sc->sc_mutex);
1194 1.8.4.2 ad rw_exit(&sc->sc_rwlock);
1195 1.8.4.2 ad }
1196 1.8.4.2 ad
1197 1.8.4.2 ad void
1198 1.8.4.2 ad arc_wait(struct arc_softc *sc)
1199 1.8.4.2 ad {
1200 1.8.4.2 ad KASSERT(mutex_owned(&sc->sc_mutex));
1201 1.8.4.2 ad
1202 1.8.4.2 ad arc_write(sc, ARC_REG_INTRMASK,
1203 1.8.4.2 ad ~(ARC_REG_INTRMASK_POSTQUEUE|ARC_REG_INTRMASK_DOORBELL));
1204 1.8.4.2 ad if (cv_timedwait_sig(&sc->sc_condvar, &sc->sc_mutex, hz) ==
1205 1.8.4.2 ad EWOULDBLOCK)
1206 1.8.4.2 ad arc_write(sc, ARC_REG_INTRMASK, ~ARC_REG_INTRMASK_POSTQUEUE);
1207 1.8.4.2 ad }
1208 1.8.4.2 ad
1209 1.8.4.2 ad #if NBIO > 0
1210 1.8.4.2 ad static void
1211 1.8.4.2 ad arc_create_sensors(void *arg)
1212 1.8.4.2 ad {
1213 1.8.4.2 ad struct arc_softc *sc = arg;
1214 1.8.4.2 ad struct bioc_inq bi;
1215 1.8.4.2 ad struct bioc_vol bv;
1216 1.8.4.2 ad int i;
1217 1.8.4.2 ad size_t slen;
1218 1.8.4.2 ad
1219 1.8.4.2 ad memset(&bi, 0, sizeof(bi));
1220 1.8.4.2 ad if (arc_bio_inq(sc, &bi) != 0) {
1221 1.8.4.2 ad aprint_error("%s: unable to query firmware for sensor info\n",
1222 1.8.4.2 ad device_xname(&sc->sc_dev));
1223 1.8.4.2 ad kthread_exit(0);
1224 1.8.4.2 ad }
1225 1.8.4.2 ad
1226 1.8.4.2 ad sc->sc_nsensors = bi.bi_novol;
1227 1.8.4.2 ad /*
1228 1.8.4.2 ad * There's no point to continue if there are no drives connected...
1229 1.8.4.2 ad */
1230 1.8.4.2 ad if (!sc->sc_nsensors)
1231 1.8.4.2 ad kthread_exit(0);
1232 1.8.4.2 ad
1233 1.8.4.2 ad sc->sc_sme = sysmon_envsys_create();
1234 1.8.4.2 ad slen = sizeof(envsys_data_t) * sc->sc_nsensors;
1235 1.8.4.2 ad sc->sc_sensors = kmem_zalloc(slen, KM_SLEEP);
1236 1.8.4.2 ad
1237 1.8.4.2 ad for (i = 0; i < sc->sc_nsensors; i++) {
1238 1.8.4.2 ad memset(&bv, 0, sizeof(bv));
1239 1.8.4.2 ad bv.bv_volid = i;
1240 1.8.4.2 ad if (arc_bio_vol(sc, &bv) != 0)
1241 1.8.4.2 ad goto bad;
1242 1.8.4.2 ad
1243 1.8.4.2 ad sc->sc_sensors[i].units = ENVSYS_DRIVE;
1244 1.8.4.2 ad sc->sc_sensors[i].monitor = true;
1245 1.8.4.2 ad sc->sc_sensors[i].flags = ENVSYS_FMONSTCHANGED;
1246 1.8.4.2 ad strlcpy(sc->sc_sensors[i].desc, bv.bv_dev,
1247 1.8.4.2 ad sizeof(sc->sc_sensors[i].desc));
1248 1.8.4.2 ad if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensors[i]))
1249 1.8.4.2 ad goto bad;
1250 1.8.4.2 ad }
1251 1.8.4.2 ad
1252 1.8.4.2 ad sc->sc_sme->sme_name = device_xname(&sc->sc_dev);
1253 1.8.4.2 ad sc->sc_sme->sme_cookie = sc;
1254 1.8.4.2 ad sc->sc_sme->sme_refresh = arc_refresh_sensors;
1255 1.8.4.2 ad if (sysmon_envsys_register(sc->sc_sme)) {
1256 1.8.4.2 ad aprint_debug("%s: unable to register with sysmon\n",
1257 1.8.4.2 ad device_xname(&sc->sc_dev));
1258 1.8.4.2 ad goto bad;
1259 1.8.4.2 ad }
1260 1.8.4.2 ad kthread_exit(0);
1261 1.8.4.2 ad
1262 1.8.4.2 ad bad:
1263 1.8.4.2 ad kmem_free(sc->sc_sensors, slen);
1264 1.8.4.2 ad sysmon_envsys_destroy(sc->sc_sme);
1265 1.8.4.2 ad kthread_exit(0);
1266 1.8.4.2 ad }
1267 1.8.4.2 ad
1268 1.8.4.2 ad static void
1269 1.8.4.2 ad arc_refresh_sensors(struct sysmon_envsys *sme, envsys_data_t *edata)
1270 1.8.4.2 ad {
1271 1.8.4.2 ad struct arc_softc *sc = sme->sme_cookie;
1272 1.8.4.2 ad struct bioc_vol bv;
1273 1.8.4.2 ad
1274 1.8.4.2 ad memset(&bv, 0, sizeof(bv));
1275 1.8.4.2 ad bv.bv_volid = edata->sensor;
1276 1.8.4.2 ad
1277 1.8.4.2 ad if (arc_bio_vol(sc, &bv)) {
1278 1.8.4.2 ad edata->value_cur = ENVSYS_DRIVE_EMPTY;
1279 1.8.4.2 ad edata->state = ENVSYS_SINVALID;
1280 1.8.4.2 ad return;
1281 1.8.4.2 ad }
1282 1.8.4.2 ad
1283 1.8.4.2 ad switch (bv.bv_status) {
1284 1.8.4.2 ad case BIOC_SVOFFLINE:
1285 1.8.4.2 ad edata->value_cur = ENVSYS_DRIVE_FAIL;
1286 1.8.4.2 ad edata->state = ENVSYS_SCRITICAL;
1287 1.8.4.2 ad break;
1288 1.8.4.2 ad case BIOC_SVDEGRADED:
1289 1.8.4.2 ad edata->value_cur = ENVSYS_DRIVE_PFAIL;
1290 1.8.4.2 ad edata->state = ENVSYS_SCRITICAL;
1291 1.8.4.2 ad break;
1292 1.8.4.2 ad case BIOC_SVBUILDING:
1293 1.8.4.2 ad edata->value_cur = ENVSYS_DRIVE_REBUILD;
1294 1.8.4.2 ad edata->state = ENVSYS_SVALID;
1295 1.8.4.2 ad break;
1296 1.8.4.2 ad case BIOC_SVMIGRATING:
1297 1.8.4.2 ad edata->value_cur = ENVSYS_DRIVE_MIGRATING;
1298 1.8.4.2 ad edata->state = ENVSYS_SVALID;
1299 1.8.4.2 ad break;
1300 1.8.4.2 ad case BIOC_SVSCRUB:
1301 1.8.4.2 ad case BIOC_SVONLINE:
1302 1.8.4.2 ad edata->value_cur = ENVSYS_DRIVE_ONLINE;
1303 1.8.4.2 ad edata->state = ENVSYS_SVALID;
1304 1.8.4.2 ad break;
1305 1.8.4.2 ad case BIOC_SVINVALID:
1306 1.8.4.2 ad /* FALLTRHOUGH */
1307 1.8.4.2 ad default:
1308 1.8.4.2 ad edata->value_cur = ENVSYS_DRIVE_EMPTY; /* unknown state */
1309 1.8.4.2 ad edata->state = ENVSYS_SINVALID;
1310 1.8.4.2 ad break;
1311 1.8.4.2 ad }
1312 1.8.4.2 ad }
1313 1.8.4.2 ad #endif /* NBIO > 0 */
1314 1.8.4.2 ad
1315 1.8.4.2 ad uint32_t
1316 1.8.4.2 ad arc_read(struct arc_softc *sc, bus_size_t r)
1317 1.8.4.2 ad {
1318 1.8.4.2 ad uint32_t v;
1319 1.8.4.2 ad
1320 1.8.4.2 ad bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
1321 1.8.4.2 ad BUS_SPACE_BARRIER_READ);
1322 1.8.4.2 ad v = bus_space_read_4(sc->sc_iot, sc->sc_ioh, r);
1323 1.8.4.2 ad
1324 1.8.4.2 ad DNPRINTF(ARC_D_RW, "%s: arc_read 0x%lx 0x%08x\n",
1325 1.8.4.2 ad device_xname(&sc->sc_dev), r, v);
1326 1.8.4.2 ad
1327 1.8.4.2 ad return v;
1328 1.8.4.2 ad }
1329 1.8.4.2 ad
1330 1.8.4.2 ad void
1331 1.8.4.2 ad arc_read_region(struct arc_softc *sc, bus_size_t r, void *buf, size_t len)
1332 1.8.4.2 ad {
1333 1.8.4.2 ad bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, len,
1334 1.8.4.2 ad BUS_SPACE_BARRIER_READ);
1335 1.8.4.2 ad bus_space_read_region_4(sc->sc_iot, sc->sc_ioh, r,
1336 1.8.4.2 ad (uint32_t *)buf, len >> 2);
1337 1.8.4.2 ad }
1338 1.8.4.2 ad
1339 1.8.4.2 ad void
1340 1.8.4.2 ad arc_write(struct arc_softc *sc, bus_size_t r, uint32_t v)
1341 1.8.4.2 ad {
1342 1.8.4.2 ad DNPRINTF(ARC_D_RW, "%s: arc_write 0x%lx 0x%08x\n",
1343 1.8.4.2 ad device_xname(&sc->sc_dev), r, v);
1344 1.8.4.2 ad
1345 1.8.4.2 ad bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v);
1346 1.8.4.2 ad bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
1347 1.8.4.2 ad BUS_SPACE_BARRIER_WRITE);
1348 1.8.4.2 ad }
1349 1.8.4.2 ad
1350 1.8.4.2 ad void
1351 1.8.4.2 ad arc_write_region(struct arc_softc *sc, bus_size_t r, void *buf, size_t len)
1352 1.8.4.2 ad {
1353 1.8.4.2 ad bus_space_write_region_4(sc->sc_iot, sc->sc_ioh, r,
1354 1.8.4.2 ad (const uint32_t *)buf, len >> 2);
1355 1.8.4.2 ad bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, len,
1356 1.8.4.2 ad BUS_SPACE_BARRIER_WRITE);
1357 1.8.4.2 ad }
1358 1.8.4.2 ad
1359 1.8.4.2 ad int
1360 1.8.4.2 ad arc_wait_eq(struct arc_softc *sc, bus_size_t r, uint32_t mask,
1361 1.8.4.2 ad uint32_t target)
1362 1.8.4.2 ad {
1363 1.8.4.2 ad int i;
1364 1.8.4.2 ad
1365 1.8.4.2 ad DNPRINTF(ARC_D_RW, "%s: arc_wait_eq 0x%lx 0x%08x 0x%08x\n",
1366 1.8.4.2 ad device_xname(&sc->sc_dev), r, mask, target);
1367 1.8.4.2 ad
1368 1.8.4.2 ad for (i = 0; i < 10000; i++) {
1369 1.8.4.2 ad if ((arc_read(sc, r) & mask) == target)
1370 1.8.4.2 ad return 0;
1371 1.8.4.2 ad delay(1000);
1372 1.8.4.2 ad }
1373 1.8.4.2 ad
1374 1.8.4.2 ad return 1;
1375 1.8.4.2 ad }
1376 1.8.4.2 ad
1377 1.8.4.2 ad int
1378 1.8.4.2 ad arc_wait_ne(struct arc_softc *sc, bus_size_t r, uint32_t mask,
1379 1.8.4.2 ad uint32_t target)
1380 1.8.4.2 ad {
1381 1.8.4.2 ad int i;
1382 1.8.4.2 ad
1383 1.8.4.2 ad DNPRINTF(ARC_D_RW, "%s: arc_wait_ne 0x%lx 0x%08x 0x%08x\n",
1384 1.8.4.2 ad device_xname(&sc->sc_dev), r, mask, target);
1385 1.8.4.2 ad
1386 1.8.4.2 ad for (i = 0; i < 10000; i++) {
1387 1.8.4.2 ad if ((arc_read(sc, r) & mask) != target)
1388 1.8.4.2 ad return 0;
1389 1.8.4.2 ad delay(1000);
1390 1.8.4.2 ad }
1391 1.8.4.2 ad
1392 1.8.4.2 ad return 1;
1393 1.8.4.2 ad }
1394 1.8.4.2 ad
1395 1.8.4.2 ad int
1396 1.8.4.2 ad arc_msg0(struct arc_softc *sc, uint32_t m)
1397 1.8.4.2 ad {
1398 1.8.4.2 ad /* post message */
1399 1.8.4.2 ad arc_write(sc, ARC_REG_INB_MSG0, m);
1400 1.8.4.2 ad /* wait for the fw to do it */
1401 1.8.4.2 ad if (arc_wait_eq(sc, ARC_REG_INTRSTAT, ARC_REG_INTRSTAT_MSG0,
1402 1.8.4.2 ad ARC_REG_INTRSTAT_MSG0) != 0)
1403 1.8.4.2 ad return 1;
1404 1.8.4.2 ad
1405 1.8.4.2 ad /* ack it */
1406 1.8.4.2 ad arc_write(sc, ARC_REG_INTRSTAT, ARC_REG_INTRSTAT_MSG0);
1407 1.8.4.2 ad
1408 1.8.4.2 ad return 0;
1409 1.8.4.2 ad }
1410 1.8.4.2 ad
1411 1.8.4.2 ad struct arc_dmamem *
1412 1.8.4.2 ad arc_dmamem_alloc(struct arc_softc *sc, size_t size)
1413 1.8.4.2 ad {
1414 1.8.4.2 ad struct arc_dmamem *adm;
1415 1.8.4.2 ad int nsegs;
1416 1.8.4.2 ad
1417 1.8.4.2 ad adm = kmem_zalloc(sizeof(*adm), KM_NOSLEEP);
1418 1.8.4.2 ad if (adm == NULL)
1419 1.8.4.2 ad return NULL;
1420 1.8.4.2 ad
1421 1.8.4.2 ad adm->adm_size = size;
1422 1.8.4.2 ad
1423 1.8.4.2 ad if (bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
1424 1.8.4.2 ad BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, &adm->adm_map) != 0)
1425 1.8.4.2 ad goto admfree;
1426 1.8.4.2 ad
1427 1.8.4.2 ad if (bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &adm->adm_seg,
1428 1.8.4.2 ad 1, &nsegs, BUS_DMA_NOWAIT) != 0)
1429 1.8.4.2 ad goto destroy;
1430 1.8.4.2 ad
1431 1.8.4.2 ad if (bus_dmamem_map(sc->sc_dmat, &adm->adm_seg, nsegs, size,
1432 1.8.4.2 ad &adm->adm_kva, BUS_DMA_NOWAIT|BUS_DMA_COHERENT) != 0)
1433 1.8.4.2 ad goto free;
1434 1.8.4.2 ad
1435 1.8.4.2 ad if (bus_dmamap_load(sc->sc_dmat, adm->adm_map, adm->adm_kva, size,
1436 1.8.4.2 ad NULL, BUS_DMA_NOWAIT) != 0)
1437 1.8.4.2 ad goto unmap;
1438 1.8.4.2 ad
1439 1.8.4.2 ad memset(adm->adm_kva, 0, size);
1440 1.8.4.2 ad
1441 1.8.4.2 ad return adm;
1442 1.8.4.2 ad
1443 1.8.4.2 ad unmap:
1444 1.8.4.2 ad bus_dmamem_unmap(sc->sc_dmat, adm->adm_kva, size);
1445 1.8.4.2 ad free:
1446 1.8.4.2 ad bus_dmamem_free(sc->sc_dmat, &adm->adm_seg, 1);
1447 1.8.4.2 ad destroy:
1448 1.8.4.2 ad bus_dmamap_destroy(sc->sc_dmat, adm->adm_map);
1449 1.8.4.2 ad admfree:
1450 1.8.4.2 ad kmem_free(adm, sizeof(*adm));
1451 1.8.4.2 ad
1452 1.8.4.2 ad return NULL;
1453 1.8.4.2 ad }
1454 1.8.4.2 ad
1455 1.8.4.2 ad void
1456 1.8.4.2 ad arc_dmamem_free(struct arc_softc *sc, struct arc_dmamem *adm)
1457 1.8.4.2 ad {
1458 1.8.4.2 ad bus_dmamap_unload(sc->sc_dmat, adm->adm_map);
1459 1.8.4.2 ad bus_dmamem_unmap(sc->sc_dmat, adm->adm_kva, adm->adm_size);
1460 1.8.4.2 ad bus_dmamem_free(sc->sc_dmat, &adm->adm_seg, 1);
1461 1.8.4.2 ad bus_dmamap_destroy(sc->sc_dmat, adm->adm_map);
1462 1.8.4.2 ad kmem_free(adm, sizeof(*adm));
1463 1.8.4.2 ad }
1464 1.8.4.2 ad
1465 1.8.4.2 ad int
1466 1.8.4.2 ad arc_alloc_ccbs(struct arc_softc *sc)
1467 1.8.4.2 ad {
1468 1.8.4.2 ad struct arc_ccb *ccb;
1469 1.8.4.2 ad uint8_t *cmd;
1470 1.8.4.2 ad int i;
1471 1.8.4.2 ad size_t ccbslen;
1472 1.8.4.2 ad
1473 1.8.4.2 ad TAILQ_INIT(&sc->sc_ccb_free);
1474 1.8.4.2 ad
1475 1.8.4.2 ad ccbslen = sizeof(struct arc_ccb) * sc->sc_req_count;
1476 1.8.4.2 ad sc->sc_ccbs = kmem_zalloc(ccbslen, KM_SLEEP);
1477 1.8.4.2 ad
1478 1.8.4.2 ad sc->sc_requests = arc_dmamem_alloc(sc,
1479 1.8.4.2 ad ARC_MAX_IOCMDLEN * sc->sc_req_count);
1480 1.8.4.2 ad if (sc->sc_requests == NULL) {
1481 1.8.4.2 ad aprint_error("%s: unable to allocate ccb dmamem\n",
1482 1.8.4.2 ad device_xname(&sc->sc_dev));
1483 1.8.4.2 ad goto free_ccbs;
1484 1.8.4.2 ad }
1485 1.8.4.2 ad cmd = ARC_DMA_KVA(sc->sc_requests);
1486 1.8.4.2 ad
1487 1.8.4.2 ad for (i = 0; i < sc->sc_req_count; i++) {
1488 1.8.4.2 ad ccb = &sc->sc_ccbs[i];
1489 1.8.4.2 ad
1490 1.8.4.2 ad if (bus_dmamap_create(sc->sc_dmat, MAXPHYS, ARC_SGL_MAXLEN,
1491 1.8.4.2 ad MAXPHYS, 0, 0, &ccb->ccb_dmamap) != 0) {
1492 1.8.4.2 ad aprint_error("%s: unable to create dmamap for ccb %d\n",
1493 1.8.4.2 ad device_xname(&sc->sc_dev), i);
1494 1.8.4.2 ad goto free_maps;
1495 1.8.4.2 ad }
1496 1.8.4.2 ad
1497 1.8.4.2 ad ccb->ccb_sc = sc;
1498 1.8.4.2 ad ccb->ccb_id = i;
1499 1.8.4.2 ad ccb->ccb_offset = ARC_MAX_IOCMDLEN * i;
1500 1.8.4.2 ad
1501 1.8.4.2 ad ccb->ccb_cmd = (struct arc_io_cmd *)&cmd[ccb->ccb_offset];
1502 1.8.4.2 ad ccb->ccb_cmd_post = (ARC_DMA_DVA(sc->sc_requests) +
1503 1.8.4.2 ad ccb->ccb_offset) >> ARC_REG_POST_QUEUE_ADDR_SHIFT;
1504 1.8.4.2 ad
1505 1.8.4.2 ad arc_put_ccb(sc, ccb);
1506 1.8.4.2 ad }
1507 1.8.4.2 ad
1508 1.8.4.2 ad return 0;
1509 1.8.4.2 ad
1510 1.8.4.2 ad free_maps:
1511 1.8.4.2 ad while ((ccb = arc_get_ccb(sc)) != NULL)
1512 1.8.4.2 ad bus_dmamap_destroy(sc->sc_dmat, ccb->ccb_dmamap);
1513 1.8.4.2 ad arc_dmamem_free(sc, sc->sc_requests);
1514 1.8.4.2 ad
1515 1.8.4.2 ad free_ccbs:
1516 1.8.4.2 ad kmem_free(sc->sc_ccbs, ccbslen);
1517 1.8.4.2 ad
1518 1.8.4.2 ad return 1;
1519 1.8.4.2 ad }
1520 1.8.4.2 ad
1521 1.8.4.2 ad struct arc_ccb *
1522 1.8.4.2 ad arc_get_ccb(struct arc_softc *sc)
1523 1.8.4.2 ad {
1524 1.8.4.2 ad struct arc_ccb *ccb;
1525 1.8.4.2 ad
1526 1.8.4.2 ad ccb = TAILQ_FIRST(&sc->sc_ccb_free);
1527 1.8.4.2 ad if (ccb != NULL)
1528 1.8.4.2 ad TAILQ_REMOVE(&sc->sc_ccb_free, ccb, ccb_link);
1529 1.8.4.2 ad
1530 1.8.4.2 ad return ccb;
1531 1.8.4.2 ad }
1532 1.8.4.2 ad
1533 1.8.4.2 ad void
1534 1.8.4.2 ad arc_put_ccb(struct arc_softc *sc, struct arc_ccb *ccb)
1535 1.8.4.2 ad {
1536 1.8.4.2 ad ccb->ccb_xs = NULL;
1537 1.8.4.2 ad memset(ccb->ccb_cmd, 0, ARC_MAX_IOCMDLEN);
1538 1.8.4.2 ad TAILQ_INSERT_TAIL(&sc->sc_ccb_free, ccb, ccb_link);
1539 1.8.4.2 ad }
1540