xref: /dragonfly/sys/dev/raid/twe/twe_freebsd.c (revision b1e9d17a)
1 /*-
2  * Copyright (c) 2000 Michael Smith
3  * Copyright (c) 2003 Paul Saab
4  * Copyright (c) 2003 Vinod Kashyap
5  * Copyright (c) 2000 BSDi
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/sys/dev/twe/twe_freebsd.c,v 1.2.2.9 2004/06/11 18:57:31 vkashyap Exp $
30  * $DragonFly: src/sys/dev/raid/twe/twe_freebsd.c,v 1.15 2005/08/22 21:16:20 hmp Exp $
31  */
32 
33 /*
34  * FreeBSD-specific code.
35  */
36 
37 #include <dev/raid/twe/twe_compat.h>
38 #include <dev/raid/twe/twereg.h>
39 #include <dev/raid/twe/twe_tables.h>
40 #include <dev/raid/twe/tweio.h>
41 #include <dev/raid/twe/twevar.h>
42 
43 static devclass_t	twe_devclass;
44 
45 #ifdef TWE_DEBUG
46 static u_int32_t	twed_bio_in;
47 #define TWED_BIO_IN	twed_bio_in++
48 static u_int32_t	twed_bio_out;
49 #define TWED_BIO_OUT	twed_bio_out++
50 #else
51 #define TWED_BIO_IN
52 #define TWED_BIO_OUT
53 #endif
54 
55 /********************************************************************************
56  ********************************************************************************
57                                                          Control device interface
58  ********************************************************************************
59  ********************************************************************************/
60 
61 static	d_open_t		twe_open;
62 static	d_close_t		twe_close;
63 static	d_ioctl_t		twe_ioctl_wrapper;
64 
65 static struct cdevsw twe_cdevsw = {
66 	"twe", 	/* name */
67 	TWE_CDEV_MAJOR, /* major number */
68 	0,  	/* flags */
69 	NULL, 	/* device port */
70 	NULL, 	/* cloning */
71     twe_open,
72     twe_close,
73     noread,
74     nowrite,
75     twe_ioctl_wrapper,
76     nopoll,
77     nommap,
78     nostrategy,
79     nodump,
80     nopsize
81 };
82 
83 /********************************************************************************
84  * Accept an open operation on the control device.
85  */
86 static int
87 twe_open(dev_t dev, int flags, int fmt, d_thread_t *td)
88 {
89     int			unit = minor(dev);
90     struct twe_softc	*sc = devclass_get_softc(twe_devclass, unit);
91 
92     sc->twe_state |= TWE_STATE_OPEN;
93     return(0);
94 }
95 
96 /********************************************************************************
97  * Accept the last close on the control device.
98  */
99 static int
100 twe_close(dev_t dev, int flags, int fmt, d_thread_t *td)
101 {
102     int			unit = minor(dev);
103     struct twe_softc	*sc = devclass_get_softc(twe_devclass, unit);
104 
105     sc->twe_state &= ~TWE_STATE_OPEN;
106     return (0);
107 }
108 
109 /********************************************************************************
110  * Handle controller-specific control operations.
111  */
112 static int
113 twe_ioctl_wrapper(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, d_thread_t *td)
114 {
115     struct twe_softc		*sc = (struct twe_softc *)dev->si_drv1;
116 
117     return(twe_ioctl(sc, cmd, addr));
118 }
119 
120 /********************************************************************************
121  ********************************************************************************
122                                                              PCI device interface
123  ********************************************************************************
124  ********************************************************************************/
125 
126 static int	twe_probe(device_t dev);
127 static int	twe_attach(device_t dev);
128 static void	twe_free(struct twe_softc *sc);
129 static int	twe_detach(device_t dev);
130 static int	twe_shutdown(device_t dev);
131 static int	twe_suspend(device_t dev);
132 static int	twe_resume(device_t dev);
133 static void	twe_pci_intr(void *arg);
134 static void	twe_intrhook(void *arg);
135 static void	twe_free_request(struct twe_request *tr);
136 static void	twe_setup_data_dmamap(void *arg, bus_dma_segment_t *segs,
137 								  int nsegments, int error);
138 static void	twe_setup_request_dmamap(void *arg, bus_dma_segment_t *segs,
139 									 int nsegments, int error);
140 
141 static device_method_t twe_methods[] = {
142     /* Device interface */
143     DEVMETHOD(device_probe,	twe_probe),
144     DEVMETHOD(device_attach,	twe_attach),
145     DEVMETHOD(device_detach,	twe_detach),
146     DEVMETHOD(device_shutdown,	twe_shutdown),
147     DEVMETHOD(device_suspend,	twe_suspend),
148     DEVMETHOD(device_resume,	twe_resume),
149 
150     DEVMETHOD(bus_print_child,	bus_generic_print_child),
151     DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
152     { 0, 0 }
153 };
154 
155 static driver_t twe_pci_driver = {
156 	"twe",
157 	twe_methods,
158 	sizeof(struct twe_softc)
159 };
160 
161 #ifdef TWE_OVERRIDE
162 DRIVER_MODULE(Xtwe, pci, twe_pci_driver, twe_devclass, 0, 0);
163 #else
164 DRIVER_MODULE(twe, pci, twe_pci_driver, twe_devclass, 0, 0);
165 #endif
166 
167 /********************************************************************************
168  * Match a 3ware Escalade ATA RAID controller.
169  */
170 static int
171 twe_probe(device_t dev)
172 {
173 
174     debug_called(4);
175 
176     if ((pci_get_vendor(dev) == TWE_VENDOR_ID) &&
177 	((pci_get_device(dev) == TWE_DEVICE_ID) ||
178 	 (pci_get_device(dev) == TWE_DEVICE_ID_ASIC))) {
179 	device_set_desc(dev, TWE_DEVICE_NAME " driver ver. " TWE_DRIVER_VERSION_STRING);
180 #ifdef TWE_OVERRIDE
181 	return(0);
182 #else
183 	return(-10);
184 #endif
185     }
186     return(ENXIO);
187 }
188 
189 /********************************************************************************
190  * Allocate resources, initialise the controller.
191  */
192 static int
193 twe_attach(device_t dev)
194 {
195     struct twe_softc	*sc;
196     int			rid, error;
197     u_int32_t		command;
198 
199     debug_called(4);
200 
201     /*
202      * Initialise the softc structure.
203      */
204     sc = device_get_softc(dev);
205     sc->twe_dev = dev;
206 
207     sysctl_ctx_init(&sc->sysctl_ctx);
208     sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
209 	SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
210 	device_get_nameunit(dev), CTLFLAG_RD, 0, "");
211     if (sc->sysctl_tree == NULL) {
212 	twe_printf(sc, "cannot add sysctl tree node\n");
213 	return (ENXIO);
214     }
215     SYSCTL_ADD_STRING(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
216 	OID_AUTO, "driver_version", CTLFLAG_RD, TWE_DRIVER_VERSION_STRING, 0,
217 	"TWE driver version");
218 
219     /*
220      * Make sure we are going to be able to talk to this board.
221      */
222     command = pci_read_config(dev, PCIR_COMMAND, 2);
223     if ((command & PCIM_CMD_PORTEN) == 0) {
224 	twe_printf(sc, "register window not available\n");
225 	return(ENXIO);
226     }
227     /*
228      * Force the busmaster enable bit on, in case the BIOS forgot.
229      */
230     command |= PCIM_CMD_BUSMASTEREN;
231     pci_write_config(dev, PCIR_COMMAND, command, 2);
232 
233     /*
234      * Allocate the PCI register window.
235      */
236     rid = TWE_IO_CONFIG_REG;
237     if ((sc->twe_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE)) == NULL) {
238 	twe_printf(sc, "can't allocate register window\n");
239 	twe_free(sc);
240 	return(ENXIO);
241     }
242     sc->twe_btag = rman_get_bustag(sc->twe_io);
243     sc->twe_bhandle = rman_get_bushandle(sc->twe_io);
244 
245     /*
246      * Allocate the parent bus DMA tag appropriate for PCI.
247      */
248     if (bus_dma_tag_create(NULL, 				/* parent */
249 			   1, 0, 				/* alignment, boundary */
250 			   BUS_SPACE_MAXADDR_32BIT, 		/* lowaddr */
251 			   BUS_SPACE_MAXADDR, 			/* highaddr */
252 			   NULL, NULL, 				/* filter, filterarg */
253 			   MAXBSIZE, TWE_MAX_SGL_LENGTH,	/* maxsize, nsegments */
254 			   BUS_SPACE_MAXSIZE_32BIT,		/* maxsegsize */
255 			   BUS_DMA_ALLOCNOW,			/* flags */
256 			   &sc->twe_parent_dmat)) {
257 	twe_printf(sc, "can't allocate parent DMA tag\n");
258 	twe_free(sc);
259 	return(ENOMEM);
260     }
261 
262     /*
263      * Allocate and connect our interrupt.
264      */
265     rid = 0;
266     if ((sc->twe_irq = bus_alloc_resource(sc->twe_dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
267 	twe_printf(sc, "can't allocate interrupt\n");
268 	twe_free(sc);
269 	return(ENXIO);
270     }
271     if (bus_setup_intr(sc->twe_dev, sc->twe_irq, INTR_TYPE_BIO | INTR_ENTROPY,  twe_pci_intr, sc, &sc->twe_intr, NULL)) {
272 	twe_printf(sc, "can't set up interrupt\n");
273 	twe_free(sc);
274 	return(ENXIO);
275     }
276 
277     /*
278      * Create DMA tag for mapping objects into controller-addressable space.
279      */
280     if (bus_dma_tag_create(sc->twe_parent_dmat, 	/* parent */
281 			   1, 0, 			/* alignment, boundary */
282 			   BUS_SPACE_MAXADDR,		/* lowaddr */
283 			   BUS_SPACE_MAXADDR, 		/* highaddr */
284 			   NULL, NULL, 			/* filter, filterarg */
285 			   MAXBSIZE, TWE_MAX_SGL_LENGTH,/* maxsize, nsegments */
286 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
287 			   0,				/* flags */
288 			   &sc->twe_buffer_dmat)) {
289 	twe_printf(sc, "can't allocate data buffer DMA tag\n");
290 	twe_free(sc);
291 	return(ENOMEM);
292     }
293 
294     /*
295      * Initialise the controller and driver core.
296      */
297     if ((error = twe_setup(sc))) {
298 	twe_free(sc);
299 	return(error);
300     }
301 
302     /*
303      * Print some information about the controller and configuration.
304      */
305     twe_describe_controller(sc);
306 
307     /*
308      * Create the control device.
309      */
310 	cdevsw_add(&twe_cdevsw, -1, device_get_unit(sc->twe_dev));
311     sc->twe_dev_t = make_dev(&twe_cdevsw, device_get_unit(sc->twe_dev),
312 			UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR, "twe%d",
313 			device_get_unit(sc->twe_dev));
314     sc->twe_dev_t->si_drv1 = sc;
315     /*
316      * Schedule ourselves to bring the controller up once interrupts are available.
317      * This isn't strictly necessary, since we disable interrupts while probing the
318      * controller, but it is more in keeping with common practice for other disk
319      * devices.
320      */
321     sc->twe_ich.ich_func = twe_intrhook;
322     sc->twe_ich.ich_arg = sc;
323     if (config_intrhook_establish(&sc->twe_ich) != 0) {
324 	twe_printf(sc, "can't establish configuration hook\n");
325 	twe_free(sc);
326 	return(ENXIO);
327     }
328 
329     return(0);
330 }
331 
332 /********************************************************************************
333  * Free all of the resources associated with (sc).
334  *
335  * Should not be called if the controller is active.
336  */
337 static void
338 twe_free(struct twe_softc *sc)
339 {
340     struct twe_request	*tr;
341 
342     debug_called(4);
343 
344     /* throw away any command buffers */
345     while ((tr = twe_dequeue_free(sc)) != NULL)
346 	twe_free_request(tr);
347 
348     /* destroy the data-transfer DMA tag */
349     if (sc->twe_buffer_dmat)
350 	bus_dma_tag_destroy(sc->twe_buffer_dmat);
351 
352     /* disconnect the interrupt handler */
353     if (sc->twe_intr)
354 	bus_teardown_intr(sc->twe_dev, sc->twe_irq, sc->twe_intr);
355     if (sc->twe_irq != NULL)
356 	bus_release_resource(sc->twe_dev, SYS_RES_IRQ, 0, sc->twe_irq);
357 
358     /* destroy the parent DMA tag */
359     if (sc->twe_parent_dmat)
360 	bus_dma_tag_destroy(sc->twe_parent_dmat);
361 
362     /* release the register window mapping */
363     if (sc->twe_io != NULL)
364 	bus_release_resource(sc->twe_dev, SYS_RES_IOPORT, TWE_IO_CONFIG_REG, sc->twe_io);
365 
366 	cdevsw_remove(&twe_cdevsw, -1, device_get_unit(sc->twe_dev));
367     /* destroy control device */
368     if (sc->twe_dev_t != (dev_t)NULL)
369 	destroy_dev(sc->twe_dev_t);
370 
371     sysctl_ctx_free(&sc->sysctl_ctx);
372 }
373 
374 /********************************************************************************
375  * Disconnect from the controller completely, in preparation for unload.
376  */
377 static int
378 twe_detach(device_t dev)
379 {
380     struct twe_softc	*sc = device_get_softc(dev);
381     int			error;
382 
383     debug_called(4);
384 
385     error = EBUSY;
386     crit_enter();
387     if (sc->twe_state & TWE_STATE_OPEN)
388 	goto out;
389 
390     /*
391      * Shut the controller down.
392      */
393     if ((error = twe_shutdown(dev)))
394 	goto out;
395 
396     twe_free(sc);
397 
398     error = 0;
399  out:
400     crit_exit();
401     return(error);
402 }
403 
404 /********************************************************************************
405  * Bring the controller down to a dormant state and detach all child devices.
406  *
407  * Note that we can assume that the bioq on the controller is empty, as we won't
408  * allow shutdown if any device is open.
409  */
410 static int
411 twe_shutdown(device_t dev)
412 {
413     struct twe_softc	*sc = device_get_softc(dev);
414     int			i, error = 0;
415 
416     debug_called(4);
417 
418     crit_enter();
419 
420     /*
421      * Delete all our child devices.
422      */
423     for (i = 0; i < TWE_MAX_UNITS; i++) {
424       if (sc->twe_drive[i].td_disk != 0)
425 	if ((error = twe_detach_drive(sc, i)) != 0)
426 	    goto out;
427     }
428 
429     /*
430      * Bring the controller down.
431      */
432     twe_deinit(sc);
433 
434  out:
435     crit_exit();
436     return(error);
437 }
438 
439 /********************************************************************************
440  * Bring the controller to a quiescent state, ready for system suspend.
441  */
442 static int
443 twe_suspend(device_t dev)
444 {
445     struct twe_softc	*sc = device_get_softc(dev);
446 
447     debug_called(4);
448 
449     crit_enter();
450     sc->twe_state |= TWE_STATE_SUSPEND;
451 
452     twe_disable_interrupts(sc);
453     crit_exit();
454 
455     return(0);
456 }
457 
458 /********************************************************************************
459  * Bring the controller back to a state ready for operation.
460  */
461 static int
462 twe_resume(device_t dev)
463 {
464     struct twe_softc	*sc = device_get_softc(dev);
465 
466     debug_called(4);
467 
468     sc->twe_state &= ~TWE_STATE_SUSPEND;
469     twe_enable_interrupts(sc);
470 
471     return(0);
472 }
473 
474 /*******************************************************************************
475  * Take an interrupt, or be poked by other code to look for interrupt-worthy
476  * status.
477  */
478 static void
479 twe_pci_intr(void *arg)
480 {
481     twe_intr((struct twe_softc *)arg);
482 }
483 
484 /********************************************************************************
485  * Delayed-startup hook
486  */
487 static void
488 twe_intrhook(void *arg)
489 {
490     struct twe_softc		*sc = (struct twe_softc *)arg;
491 
492     /* pull ourselves off the intrhook chain */
493     config_intrhook_disestablish(&sc->twe_ich);
494 
495     /* call core startup routine */
496     twe_init(sc);
497 }
498 
499 /********************************************************************************
500  * Given a detected drive, attach it to the bio interface.
501  *
502  * This is called from twe_add_unit.
503  */
504 int
505 twe_attach_drive(struct twe_softc *sc, struct twe_drive *dr)
506 {
507     char	buf[80];
508     int		error = 0;
509 
510     dr->td_disk =  device_add_child(sc->twe_dev, NULL, -1);
511     if (dr->td_disk == NULL) {
512 	twe_printf(sc, "Cannot add unit\n");
513 	return (EIO);
514     }
515     device_set_ivars(dr->td_disk, dr);
516 
517     /*
518      * XXX It would make sense to test the online/initialising bits, but they seem to be
519      * always set...
520      */
521     sprintf(buf, "Unit %d, %s, %s",
522 	    dr->td_twe_unit,
523 	    twe_describe_code(twe_table_unittype, dr->td_type),
524 	    twe_describe_code(twe_table_unitstate, dr->td_state & TWE_PARAM_UNITSTATUS_MASK));
525     device_set_desc_copy(dr->td_disk, buf);
526 
527     if ((error = bus_generic_attach(sc->twe_dev)) != 0) {
528 	twe_printf(sc, "Cannot attach unit to controller. error = %d\n", error);
529 	error = EIO;
530     }
531     return (error);
532 }
533 
534 /********************************************************************************
535  * Detach the specified unit if it exsists
536  *
537  * This is called from twe_del_unit.
538  */
539 int
540 twe_detach_drive(struct twe_softc *sc, int unit)
541 {
542     int	error = 0;
543 
544     if ((error = device_delete_child(sc->twe_dev, sc->twe_drive[unit].td_disk))) {
545 	twe_printf(sc, "Cannot delete unit. error = %d\n", error);
546 	return (error);
547     }
548     bzero(&sc->twe_drive[unit], sizeof(sc->twe_drive[unit]));
549     return (error);
550 }
551 
552 /********************************************************************************
553  * Clear a PCI parity error.
554  */
555 void
556 twe_clear_pci_parity_error(struct twe_softc *sc)
557 {
558     TWE_CONTROL(sc, TWE_CONTROL_CLEAR_PARITY_ERROR);
559     pci_write_config(sc->twe_dev, PCIR_STATUS, TWE_PCI_CLEAR_PARITY_ERROR, 2);
560 }
561 
562 /********************************************************************************
563  * Clear a PCI abort.
564  */
565 void
566 twe_clear_pci_abort(struct twe_softc *sc)
567 {
568     TWE_CONTROL(sc, TWE_CONTROL_CLEAR_PCI_ABORT);
569     pci_write_config(sc->twe_dev, PCIR_STATUS, TWE_PCI_CLEAR_PCI_ABORT, 2);
570 }
571 
572 /********************************************************************************
573  ********************************************************************************
574                                                                       Disk device
575  ********************************************************************************
576  ********************************************************************************/
577 
578 /*
579  * Disk device bus interface
580  */
581 static int twed_probe(device_t dev);
582 static int twed_attach(device_t dev);
583 static int twed_detach(device_t dev);
584 
585 static device_method_t twed_methods[] = {
586     DEVMETHOD(device_probe,	twed_probe),
587     DEVMETHOD(device_attach,	twed_attach),
588     DEVMETHOD(device_detach,	twed_detach),
589     { 0, 0 }
590 };
591 
592 static driver_t twed_driver = {
593     "twed",
594     twed_methods,
595     sizeof(struct twed_softc)
596 };
597 
598 static devclass_t	twed_devclass;
599 #ifdef TWE_OVERRIDE
600 DRIVER_MODULE(Xtwed, Xtwe, twed_driver, twed_devclass, 0, 0);
601 #else
602 DRIVER_MODULE(twed, twe, twed_driver, twed_devclass, 0, 0);
603 #endif
604 
605 /*
606  * Disk device control interface.
607  */
608 static	d_open_t	twed_open;
609 static	d_close_t	twed_close;
610 static	d_strategy_t	twed_strategy;
611 static	d_dump_t	twed_dump;
612 
613 static struct cdevsw twed_cdevsw = {
614 	"twed",
615 	TWED_CDEV_MAJOR,
616 	D_DISK,
617 	/* port */ NULL,
618 	/* clone */ NULL,
619     twed_open,
620     twed_close,
621     physread,
622     physwrite,
623     noioctl,
624     nopoll,
625     nommap,
626     twed_strategy,
627     twed_dump,
628     nopsize
629 };
630 
631 #if 0
632 static struct cdevsw	tweddisk_cdevsw;
633 #endif
634 #ifdef FREEBSD_4
635 static int		disks_registered = 0;
636 #endif
637 
638 /********************************************************************************
639  * Handle open from generic layer.
640  *
641  * Note that this is typically only called by the diskslice code, and not
642  * for opens on subdevices (eg. slices, partitions).
643  */
644 static int
645 twed_open(dev_t dev, int flags, int fmt, d_thread_t *td)
646 {
647     struct twed_softc	*sc = (struct twed_softc *)dev->si_drv1;
648     struct disklabel	*label;
649 
650     debug_called(4);
651 
652     if (sc == NULL)
653 	return (ENXIO);
654 
655     /* check that the controller is up and running */
656     if (sc->twed_controller->twe_state & TWE_STATE_SHUTDOWN)
657 	return(ENXIO);
658 
659     /* build synthetic label */
660     label = &sc->twed_disk.d_label;
661     bzero(label, sizeof(*label));
662     label->d_type = DTYPE_ESDI;
663     label->d_secsize    = TWE_BLOCK_SIZE;
664     label->d_nsectors   = sc->twed_drive->td_sectors;
665     label->d_ntracks    = sc->twed_drive->td_heads;
666     label->d_ncylinders = sc->twed_drive->td_cylinders;
667     label->d_secpercyl  = sc->twed_drive->td_sectors * sc->twed_drive->td_heads;
668     label->d_secperunit = sc->twed_drive->td_size;
669 
670     sc->twed_flags |= TWED_OPEN;
671     return (0);
672 }
673 
674 /********************************************************************************
675  * Handle last close of the disk device.
676  */
677 static int
678 twed_close(dev_t dev, int flags, int fmt, d_thread_t *td)
679 {
680     struct twed_softc	*sc = (struct twed_softc *)dev->si_drv1;
681 
682     debug_called(4);
683 
684     if (sc == NULL)
685 	return (ENXIO);
686 
687     sc->twed_flags &= ~TWED_OPEN;
688     return (0);
689 }
690 
691 /********************************************************************************
692  * Handle an I/O request.
693  */
694 static void
695 twed_strategy(twe_bio *bp)
696 {
697     struct twed_softc	*sc = (struct twed_softc *)TWE_BIO_SOFTC(bp);
698 
699     debug_called(4);
700 
701     TWED_BIO_IN;
702 
703     /* bogus disk? */
704     if ((sc == NULL) || (!sc->twed_drive->td_disk)) {
705 	TWE_BIO_SET_ERROR(bp, EINVAL);
706 	printf("twe: bio for invalid disk!\n");
707 	TWE_BIO_DONE(bp);
708 	TWED_BIO_OUT;
709 	return;
710     }
711 
712     /* perform accounting */
713     TWE_BIO_STATS_START(bp);
714 
715     /* queue the bio on the controller */
716     twe_enqueue_bio(sc->twed_controller, bp);
717 
718     /* poke the controller to start I/O */
719     twe_startio(sc->twed_controller);
720     return;
721 }
722 
723 /********************************************************************************
724  * System crashdump support
725  */
726 static int
727 twed_dump(dev_t dev, u_int count, u_int blkno, u_int secsize)
728 {
729     struct twed_softc	*twed_sc = (struct twed_softc *)dev->si_drv1;
730     struct twe_softc	*twe_sc  = (struct twe_softc *)twed_sc->twed_controller;
731 #if 0
732     u_int		count, blkno, secsize;
733 #endif
734     vm_paddr_t		addr = 0;
735     long		blkcnt;
736     int			dumppages = MAXDUMPPGS;
737     int			error;
738     int			i;
739 
740 #if 0
741     if ((error = disk_dumpcheck(dev, &count, &blkno, &secsize)))
742         return(error);
743 #endif
744 
745     if (!twed_sc || !twe_sc)
746 	return(ENXIO);
747 
748     blkcnt = howmany(PAGE_SIZE, secsize);
749 
750     while (count > 0) {
751 	caddr_t va = NULL;
752 
753 	if ((count / blkcnt) < dumppages)
754 	    dumppages = count / blkcnt;
755 
756 	for (i = 0; i < dumppages; ++i) {
757 	    vm_paddr_t a = addr + (i * PAGE_SIZE);
758 	    if (is_physical_memory(a))
759 		va = pmap_kenter_temporary(trunc_page(a), i);
760 	    else
761 		va = pmap_kenter_temporary(trunc_page(0), i);
762 	}
763 
764 	if ((error = twe_dump_blocks(twe_sc, twed_sc->twed_drive->td_twe_unit, blkno, va,
765 				     (PAGE_SIZE * dumppages) / TWE_BLOCK_SIZE)) != 0)
766 	    return(error);
767 
768 
769 	if (dumpstatus(addr, (off_t)count * DEV_BSIZE) < 0)
770 	    return(EINTR);
771 
772 	blkno += blkcnt * dumppages;
773 	count -= blkcnt * dumppages;
774 	addr += PAGE_SIZE * dumppages;
775     }
776     return(0);
777 }
778 
779 /********************************************************************************
780  * Handle completion of an I/O request.
781  */
782 void
783 twed_intr(twe_bio *bp)
784 {
785     debug_called(4);
786 
787     /* if no error, transfer completed */
788     if (!TWE_BIO_HAS_ERROR(bp))
789 	TWE_BIO_RESID(bp) = 0;
790 
791     TWE_BIO_STATS_END(bp);
792     TWE_BIO_DONE(bp);
793     TWED_BIO_OUT;
794 }
795 
796 /********************************************************************************
797  * Default probe stub.
798  */
799 static int
800 twed_probe(device_t dev)
801 {
802     return (0);
803 }
804 
805 /********************************************************************************
806  * Attach a unit to the controller.
807  */
808 static int
809 twed_attach(device_t dev)
810 {
811     struct twed_softc	*sc;
812     device_t		parent;
813     dev_t		dsk;
814 
815     debug_called(4);
816 
817     /* initialise our softc */
818     sc = device_get_softc(dev);
819     parent = device_get_parent(dev);
820     sc->twed_controller = (struct twe_softc *)device_get_softc(parent);
821     sc->twed_drive = device_get_ivars(dev);
822     sc->twed_drive->td_sys_unit = device_get_unit(dev);
823     sc->twed_dev = dev;
824 
825     /* report the drive */
826     twed_printf(sc, "%uMB (%u sectors)\n",
827 		sc->twed_drive->td_size / ((1024 * 1024) / TWE_BLOCK_SIZE),
828 		sc->twed_drive->td_size);
829 
830     devstat_add_entry(&sc->twed_stats, "twed", sc->twed_drive->td_sys_unit,
831 			TWE_BLOCK_SIZE,
832 			DEVSTAT_NO_ORDERED_TAGS,
833 			DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER,
834 			DEVSTAT_PRIORITY_ARRAY);
835 
836     /* attach a generic disk device to ourselves */
837     dsk = disk_create(sc->twed_drive->td_sys_unit, &sc->twed_disk,
838 			0, &twed_cdevsw);
839     dsk->si_drv1 = sc;
840 /*    dsk->si_drv2 = sc->twed_drive;*/
841     sc->twed_dev_t = dsk;
842 #ifdef FREEBSD_4
843     disks_registered++;
844 #endif
845 
846     /* set the maximum I/O size to the theoretical maximum allowed by the S/G list size */
847     dsk->si_iosize_max = (TWE_MAX_SGL_LENGTH - 1) * PAGE_SIZE;
848 
849     return (0);
850 }
851 
852 /********************************************************************************
853  * Disconnect ourselves from the system.
854  */
855 static int
856 twed_detach(device_t dev)
857 {
858     struct twed_softc *sc = (struct twed_softc *)device_get_softc(dev);
859 
860     debug_called(4);
861 
862     if (sc->twed_flags & TWED_OPEN)
863 	return(EBUSY);
864 
865     devstat_remove_entry(&sc->twed_stats);
866     disk_destroy(&sc->twed_disk);
867 #ifdef FREEBSD_4
868 	printf("Disks registered: %d\n", disks_registered);
869 #if 0
870     if (--disks_registered == 0)
871 	cdevsw_remove(&tweddisk_cdevsw);
872 #endif
873 #endif
874 
875     return(0);
876 }
877 
878 /********************************************************************************
879  ********************************************************************************
880                                                                              Misc
881  ********************************************************************************
882  ********************************************************************************/
883 
884 MALLOC_DEFINE(TWE_MALLOC_CLASS, "twe commands", "twe commands");
885 /********************************************************************************
886  * Allocate a command buffer
887  */
888 struct twe_request *
889 twe_allocate_request(struct twe_softc *sc)
890 {
891     struct twe_request	*tr;
892 	int aligned_size;
893 
894     /*
895      * TWE requires requests to be 512-byte aligned.  Depend on malloc()
896      * guarenteeing alignment for power-of-2 requests.  Note that the old
897      * (FreeBSD-4.x) malloc code aligned all requests, but the new slab
898      * allocator only guarentees same-size alignment for power-of-2 requests.
899      */
900     aligned_size = (sizeof(struct twe_request) + TWE_ALIGNMASK) &
901            ~TWE_ALIGNMASK;
902     tr = malloc(aligned_size, TWE_MALLOC_CLASS, M_INTWAIT|M_ZERO);
903     tr->tr_sc = sc;
904     if (bus_dmamap_create(sc->twe_buffer_dmat, 0, &tr->tr_cmdmap)) {
905 	twe_free_request(tr);
906 	return(NULL);
907     }
908     bus_dmamap_load(sc->twe_buffer_dmat, tr->tr_cmdmap, &tr->tr_command,
909 	sizeof(tr->tr_command), twe_setup_request_dmamap, tr, 0);
910     if (bus_dmamap_create(sc->twe_buffer_dmat, 0, &tr->tr_dmamap)) {
911 	bus_dmamap_destroy(sc->twe_buffer_dmat, tr->tr_cmdmap);
912 	twe_free_request(tr);
913 	return(NULL);
914     }
915     return(tr);
916 }
917 
918 /********************************************************************************
919  * Permanently discard a command buffer.
920  */
921 static void
922 twe_free_request(struct twe_request *tr)
923 {
924     struct twe_softc	*sc = tr->tr_sc;
925 
926     debug_called(4);
927 
928     bus_dmamap_unload(sc->twe_buffer_dmat, tr->tr_cmdmap);
929     bus_dmamap_destroy(sc->twe_buffer_dmat, tr->tr_cmdmap);
930     bus_dmamap_destroy(sc->twe_buffer_dmat, tr->tr_dmamap);
931     free(tr, TWE_MALLOC_CLASS);
932 }
933 
934 /********************************************************************************
935  * Map/unmap (tr)'s command and data in the controller's addressable space.
936  *
937  * These routines ensure that the data which the controller is going to try to
938  * access is actually visible to the controller, in a machine-independant
939  * fashion.  Due to a hardware limitation, I/O buffers must be 512-byte aligned
940  * and we take care of that here as well.
941  */
942 static void
943 twe_fillin_sgl(TWE_SG_Entry *sgl, bus_dma_segment_t *segs, int nsegments, int max_sgl)
944 {
945     int i;
946 
947     for (i = 0; i < nsegments; i++) {
948 	sgl[i].address = segs[i].ds_addr;
949 	sgl[i].length = segs[i].ds_len;
950     }
951     for (; i < max_sgl; i++) {				/* XXX necessary? */
952 	sgl[i].address = 0;
953 	sgl[i].length = 0;
954     }
955 }
956 
957 static void
958 twe_setup_data_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
959 {
960     struct twe_request	*tr = (struct twe_request *)arg;
961     TWE_Command		*cmd = &tr->tr_command;
962 
963     debug_called(4);
964 
965     if (tr->tr_flags & TWE_CMD_MAPPED)
966 	panic("already mapped command");
967 
968     tr->tr_flags |= TWE_CMD_MAPPED;
969 
970     if (tr->tr_flags & TWE_CMD_IN_PROGRESS)
971 	tr->tr_sc->twe_state &= ~TWE_STATE_FRZN;
972     /* save base of first segment in command (applicable if there only one segment) */
973     tr->tr_dataphys = segs[0].ds_addr;
974 
975     /* correct command size for s/g list size */
976     tr->tr_command.generic.size += 2 * nsegments;
977 
978     /*
979      * Due to the fact that parameter and I/O commands have the scatter/gather list in
980      * different places, we need to determine which sort of command this actually is
981      * before we can populate it correctly.
982      */
983     switch(cmd->generic.opcode) {
984     case TWE_OP_GET_PARAM:
985     case TWE_OP_SET_PARAM:
986 	cmd->generic.sgl_offset = 2;
987 	twe_fillin_sgl(&cmd->param.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
988 	break;
989     case TWE_OP_READ:
990     case TWE_OP_WRITE:
991 	cmd->generic.sgl_offset = 3;
992 	twe_fillin_sgl(&cmd->io.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
993 	break;
994     case TWE_OP_ATA_PASSTHROUGH:
995 	cmd->generic.sgl_offset = 5;
996 	twe_fillin_sgl(&cmd->ata.sgl[0], segs, nsegments, TWE_MAX_ATA_SGL_LENGTH);
997 	break;
998     default:
999 	/*
1000 	 * Fall back to what the linux driver does.
1001 	 * Do this because the API may send an opcode
1002 	 * the driver knows nothing about and this will
1003 	 * at least stop PCIABRT's from hosing us.
1004 	 */
1005 	switch (cmd->generic.sgl_offset) {
1006 	case 2:
1007 	    twe_fillin_sgl(&cmd->param.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
1008 	    break;
1009 	case 3:
1010 	    twe_fillin_sgl(&cmd->io.sgl[0], segs, nsegments, TWE_MAX_SGL_LENGTH);
1011 	    break;
1012 	case 5:
1013 	    twe_fillin_sgl(&cmd->ata.sgl[0], segs, nsegments, TWE_MAX_ATA_SGL_LENGTH);
1014 	    break;
1015 	}
1016     }
1017     if (tr->tr_flags & TWE_CMD_DATAIN)
1018 	bus_dmamap_sync(tr->tr_sc->twe_buffer_dmat, tr->tr_dmamap, BUS_DMASYNC_PREREAD);
1019     if (tr->tr_flags & TWE_CMD_DATAOUT) {
1020 	/* if we're using an alignment buffer, and we're writing data, copy the real data out */
1021 	if (tr->tr_flags & TWE_CMD_ALIGNBUF)
1022 	    bcopy(tr->tr_realdata, tr->tr_data, tr->tr_length);
1023 	bus_dmamap_sync(tr->tr_sc->twe_buffer_dmat, tr->tr_dmamap, BUS_DMASYNC_PREWRITE);
1024     }
1025     if (twe_start(tr) == EBUSY) {
1026 	tr->tr_sc->twe_state |= TWE_STATE_CTLR_BUSY;
1027 	twe_requeue_ready(tr);
1028     }
1029 }
1030 
1031 static void
1032 twe_setup_request_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1033 {
1034     struct twe_request	*tr = (struct twe_request *)arg;
1035 
1036     debug_called(4);
1037 
1038     /* command can't cross a page boundary */
1039     tr->tr_cmdphys = segs[0].ds_addr;
1040 }
1041 
1042 int
1043 twe_map_request(struct twe_request *tr)
1044 {
1045     struct twe_softc	*sc = tr->tr_sc;
1046     int			error = 0;
1047 
1048     debug_called(4);
1049 
1050     if (sc->twe_state & (TWE_STATE_CTLR_BUSY | TWE_STATE_FRZN)) {
1051 	twe_requeue_ready(tr);
1052 	return (EBUSY);
1053     }
1054 
1055     /*
1056      * Map the command into bus space.
1057      */
1058     bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_cmdmap, BUS_DMASYNC_PREWRITE);
1059 
1060     /*
1061      * If the command involves data, map that too.
1062      */
1063     if ((tr->tr_data != NULL) && ((tr->tr_flags & TWE_CMD_MAPPED) == 0)) {
1064 
1065 	/*
1066 	 * Data must be 512-byte aligned; allocate a fixup buffer if it's not.
1067 	 */
1068 	if (((vm_offset_t)tr->tr_data % TWE_ALIGNMENT) != 0) {
1069 		int aligned_size;
1070 
1071 		aligned_size = (tr->tr_length + TWE_ALIGNMASK) & ~TWE_ALIGNMASK;
1072 	    tr->tr_realdata = tr->tr_data;	/* save pointer to 'real' data */
1073 	    tr->tr_flags |= TWE_CMD_ALIGNBUF;
1074 	    tr->tr_data = malloc(aligned_size, TWE_MALLOC_CLASS, M_INTWAIT);
1075 	    if (tr->tr_data == NULL) {
1076 		twe_printf(sc, "%s: malloc failed\n", __func__);
1077 		tr->tr_data = tr->tr_realdata; /* restore original data pointer */
1078 		return(ENOMEM);
1079 	    }
1080 	}
1081 
1082 	/*
1083 	 * Map the data buffer into bus space and build the s/g list.
1084 	 */
1085 	if ((error = bus_dmamap_load(sc->twe_buffer_dmat, tr->tr_dmamap, tr->tr_data,
1086 			tr->tr_length, twe_setup_data_dmamap, tr, BUS_DMA_NOWAIT)
1087 			== EINPROGRESS)) {
1088 	    tr->tr_flags |= TWE_CMD_IN_PROGRESS;
1089 	    sc->twe_state |= TWE_STATE_FRZN;
1090 	    error = 0;
1091 	}
1092     } else {
1093 	if ((error = twe_start(tr)) == EBUSY) {
1094 	    sc->twe_state |= TWE_STATE_CTLR_BUSY;
1095 	    twe_requeue_ready(tr);
1096 	}
1097     }
1098 
1099     return(error);
1100 }
1101 
1102 void
1103 twe_unmap_request(struct twe_request *tr)
1104 {
1105     struct twe_softc	*sc = tr->tr_sc;
1106     debug_called(4);
1107 
1108     /*
1109      * Unmap the command from bus space.
1110      */
1111     bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_cmdmap, BUS_DMASYNC_POSTWRITE);
1112 
1113     /*
1114      * If the command involved data, unmap that too.
1115      */
1116     if (tr->tr_data != NULL) {
1117 
1118 	if (tr->tr_flags & TWE_CMD_DATAIN) {
1119 	    bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap, BUS_DMASYNC_POSTREAD);
1120 	    /* if we're using an alignment buffer, and we're reading data, copy the real data in */
1121 	    if (tr->tr_flags & TWE_CMD_ALIGNBUF)
1122 		bcopy(tr->tr_data, tr->tr_realdata, tr->tr_length);
1123 	}
1124 	if (tr->tr_flags & TWE_CMD_DATAOUT)
1125 	    bus_dmamap_sync(sc->twe_buffer_dmat, tr->tr_dmamap, BUS_DMASYNC_POSTWRITE);
1126 
1127 	bus_dmamap_unload(sc->twe_buffer_dmat, tr->tr_dmamap);
1128     }
1129 
1130     /* free alignment buffer if it was used */
1131     if (tr->tr_flags & TWE_CMD_ALIGNBUF) {
1132 	free(tr->tr_data, TWE_MALLOC_CLASS);
1133 	tr->tr_data = tr->tr_realdata;		/* restore 'real' data pointer */
1134     }
1135 }
1136 
1137 #ifdef TWE_DEBUG
1138 void twe_report(void);
1139 /********************************************************************************
1140  * Print current controller status, call from DDB.
1141  */
1142 void
1143 twe_report(void)
1144 {
1145     struct twe_softc	*sc;
1146     int			i;
1147 
1148     crit_enter();
1149     for (i = 0; (sc = devclass_get_softc(twe_devclass, i)) != NULL; i++)
1150 	twe_print_controller(sc);
1151     printf("twed: total bio count in %u  out %u\n", twed_bio_in, twed_bio_out);
1152     crit_exit();
1153 }
1154 #endif
1155