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