1edc6143bSDavid Rhodus /*-
2edc6143bSDavid Rhodus * Written by: David Jeffery
3edc6143bSDavid Rhodus * Copyright (c) 2002 Adaptec Inc.
4edc6143bSDavid Rhodus * All rights reserved.
5edc6143bSDavid Rhodus *
6edc6143bSDavid Rhodus * Redistribution and use in source and binary forms, with or without
7edc6143bSDavid Rhodus * modification, are permitted provided that the following conditions
8edc6143bSDavid Rhodus * are met:
9edc6143bSDavid Rhodus * 1. Redistributions of source code must retain the above copyright
10edc6143bSDavid Rhodus * notice, this list of conditions and the following disclaimer.
11edc6143bSDavid Rhodus * 2. Redistributions in binary form must reproduce the above copyright
12edc6143bSDavid Rhodus * notice, this list of conditions and the following disclaimer in the
13edc6143bSDavid Rhodus * documentation and/or other materials provided with the distribution.
14edc6143bSDavid Rhodus *
15edc6143bSDavid Rhodus * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16edc6143bSDavid Rhodus * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17edc6143bSDavid Rhodus * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18edc6143bSDavid Rhodus * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19edc6143bSDavid Rhodus * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20edc6143bSDavid Rhodus * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21edc6143bSDavid Rhodus * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22edc6143bSDavid Rhodus * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23edc6143bSDavid Rhodus * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24edc6143bSDavid Rhodus * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25edc6143bSDavid Rhodus * SUCH DAMAGE.
26edc6143bSDavid Rhodus *
27ad57418eSJoerg Sonnenberger * $FreeBSD: src/sys/dev/ips/ips_disk.c,v 1.4 2003/09/22 04:59:07 njl Exp $
28edc6143bSDavid Rhodus */
29edc6143bSDavid Rhodus
30287d1e34SYONETANI Tomokazu #include <sys/devicestat.h>
31edc6143bSDavid Rhodus #include <dev/raid/ips/ips.h>
32edc6143bSDavid Rhodus #include <dev/raid/ips/ips_disk.h>
33edc6143bSDavid Rhodus #include <sys/stat.h>
34ba0cc1abSMatthew Dillon #include <sys/dtype.h>
35edc6143bSDavid Rhodus
364751a114SMatthew Dillon #include <vm/vm.h>
374751a114SMatthew Dillon #include <vm/pmap.h>
384751a114SMatthew Dillon #include <machine/md_var.h>
394751a114SMatthew Dillon
40edc6143bSDavid Rhodus static int ipsd_probe(device_t dev);
41edc6143bSDavid Rhodus static int ipsd_attach(device_t dev);
42edc6143bSDavid Rhodus static int ipsd_detach(device_t dev);
43edc6143bSDavid Rhodus
444751a114SMatthew Dillon static void ipsd_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs,
454751a114SMatthew Dillon int error);
464751a114SMatthew Dillon static void ipsd_dump_block_complete(ips_command_t *command);
474751a114SMatthew Dillon
48fef8985eSMatthew Dillon static d_open_t ipsd_open;
49fef8985eSMatthew Dillon static d_close_t ipsd_close;
50fef8985eSMatthew Dillon static d_strategy_t ipsd_strategy;
5129ede8d5SYONETANI Tomokazu static d_dump_t ipsd_dump;
52edc6143bSDavid Rhodus
53fef8985eSMatthew Dillon static struct dev_ops ipsd_ops = {
5488abd8b5SSascha Wildner { "ipsd", 0, D_DISK },
55fef8985eSMatthew Dillon .d_open = ipsd_open,
56fef8985eSMatthew Dillon .d_close = ipsd_close,
57fef8985eSMatthew Dillon .d_strategy = ipsd_strategy,
58fef8985eSMatthew Dillon .d_read = physread,
59fef8985eSMatthew Dillon .d_write = physwrite,
6029ede8d5SYONETANI Tomokazu .d_dump = ipsd_dump,
61edc6143bSDavid Rhodus };
62edc6143bSDavid Rhodus
63edc6143bSDavid Rhodus static device_method_t ipsd_methods[] = {
64edc6143bSDavid Rhodus DEVMETHOD(device_probe, ipsd_probe),
65edc6143bSDavid Rhodus DEVMETHOD(device_attach, ipsd_attach),
66edc6143bSDavid Rhodus DEVMETHOD(device_detach, ipsd_detach),
67*d3c9c58eSSascha Wildner DEVMETHOD_END
68edc6143bSDavid Rhodus };
69edc6143bSDavid Rhodus
70edc6143bSDavid Rhodus
71edc6143bSDavid Rhodus static driver_t ipsd_driver = {
72edc6143bSDavid Rhodus "ipsd",
73edc6143bSDavid Rhodus ipsd_methods,
74edc6143bSDavid Rhodus sizeof(ipsdisk_softc_t)
75edc6143bSDavid Rhodus };
76edc6143bSDavid Rhodus
77edc6143bSDavid Rhodus static devclass_t ipsd_devclass;
78aa2b9d05SSascha Wildner DRIVER_MODULE(ipsd, ips, ipsd_driver, ipsd_devclass, NULL, NULL);
79edc6143bSDavid Rhodus
80edc6143bSDavid Rhodus /*
81edc6143bSDavid Rhodus * handle opening of disk device. It must set up all information about
82edc6143bSDavid Rhodus * the geometry and size of the disk
83edc6143bSDavid Rhodus */
84edc6143bSDavid Rhodus static int
ipsd_open(struct dev_open_args * ap)85fef8985eSMatthew Dillon ipsd_open(struct dev_open_args *ap)
86edc6143bSDavid Rhodus {
87b13267a5SMatthew Dillon cdev_t dev = ap->a_head.a_dev;
88edc6143bSDavid Rhodus ipsdisk_softc_t *dsc = dev->si_drv1;
89edc6143bSDavid Rhodus
90edc6143bSDavid Rhodus if (dsc == NULL)
91edc6143bSDavid Rhodus return (ENXIO);
92edc6143bSDavid Rhodus dsc->state |= IPS_DEV_OPEN;
93edc6143bSDavid Rhodus DEVICE_PRINTF(2, dsc->dev, "I'm open\n");
94edc6143bSDavid Rhodus return 0;
95edc6143bSDavid Rhodus }
96edc6143bSDavid Rhodus
97edc6143bSDavid Rhodus static int
ipsd_close(struct dev_close_args * ap)98fef8985eSMatthew Dillon ipsd_close(struct dev_close_args *ap)
99edc6143bSDavid Rhodus {
100b13267a5SMatthew Dillon cdev_t dev = ap->a_head.a_dev;
101edc6143bSDavid Rhodus ipsdisk_softc_t *dsc = dev->si_drv1;
102edc6143bSDavid Rhodus
103edc6143bSDavid Rhodus dsc->state &= ~IPS_DEV_OPEN;
104edc6143bSDavid Rhodus DEVICE_PRINTF(2, dsc->dev, "I'm closed for the day\n");
105edc6143bSDavid Rhodus return 0;
106edc6143bSDavid Rhodus }
107edc6143bSDavid Rhodus
108edc6143bSDavid Rhodus /* ipsd_finish is called to clean up and return a completed IO request */
109edc6143bSDavid Rhodus void
ipsd_finish(struct bio * bio)11081b5c339SMatthew Dillon ipsd_finish(struct bio *bio)
111edc6143bSDavid Rhodus {
11281b5c339SMatthew Dillon struct buf *bp = bio->bio_buf;
113edc6143bSDavid Rhodus ipsdisk_softc_t *dsc;
114287d1e34SYONETANI Tomokazu
11581b5c339SMatthew Dillon dsc = bio->bio_driver_info;
11681b5c339SMatthew Dillon if (bp->b_flags & B_ERROR) {
11781b5c339SMatthew Dillon device_printf(dsc->dev, "iobuf error %d\n", bp->b_error);
11881b5c339SMatthew Dillon } else {
11981b5c339SMatthew Dillon bp->b_resid = 0;
12081b5c339SMatthew Dillon }
12181b5c339SMatthew Dillon devstat_end_transaction_buf(&dsc->stats, bp);
12281b5c339SMatthew Dillon biodone(bio);
1234751a114SMatthew Dillon ips_start_io_request(dsc->sc);
124edc6143bSDavid Rhodus }
125edc6143bSDavid Rhodus
126edc6143bSDavid Rhodus
127fef8985eSMatthew Dillon static int
ipsd_strategy(struct dev_strategy_args * ap)128fef8985eSMatthew Dillon ipsd_strategy(struct dev_strategy_args *ap)
129edc6143bSDavid Rhodus {
130b13267a5SMatthew Dillon cdev_t dev = ap->a_head.a_dev;
131fef8985eSMatthew Dillon struct bio *bio = ap->a_bio;
132edc6143bSDavid Rhodus ipsdisk_softc_t *dsc;
133edc6143bSDavid Rhodus
13481b5c339SMatthew Dillon dsc = dev->si_drv1;
135edc6143bSDavid Rhodus DEVICE_PRINTF(8, dsc->dev, "in strategy\n");
13681b5c339SMatthew Dillon bio->bio_driver_info = dsc;
137287d1e34SYONETANI Tomokazu devstat_start_transaction(&dsc->stats);
1387cd8d145SMatthew Dillon lockmgr(&dsc->sc->queue_lock, LK_EXCLUSIVE|LK_RETRY);
139ec1b64f8SMatthew Dillon bioqdisksort(&dsc->sc->bio_queue, bio);
1404751a114SMatthew Dillon ips_start_io_request(dsc->sc);
1417cd8d145SMatthew Dillon lockmgr(&dsc->sc->queue_lock, LK_RELEASE);
142fef8985eSMatthew Dillon return(0);
143edc6143bSDavid Rhodus }
144edc6143bSDavid Rhodus
145edc6143bSDavid Rhodus static int
ipsd_probe(device_t dev)146edc6143bSDavid Rhodus ipsd_probe(device_t dev)
147edc6143bSDavid Rhodus {
148edc6143bSDavid Rhodus DEVICE_PRINTF(2, dev, "in probe\n");
149edc6143bSDavid Rhodus device_set_desc(dev, "Logical Drive");
150edc6143bSDavid Rhodus return 0;
151edc6143bSDavid Rhodus }
152edc6143bSDavid Rhodus
153edc6143bSDavid Rhodus static int
ipsd_attach(device_t dev)154edc6143bSDavid Rhodus ipsd_attach(device_t dev)
155edc6143bSDavid Rhodus {
156edc6143bSDavid Rhodus device_t adapter;
157edc6143bSDavid Rhodus ipsdisk_softc_t *dsc;
158a688b15cSMatthew Dillon struct disk_info info;
159edc6143bSDavid Rhodus u_int totalsectors;
160edc6143bSDavid Rhodus u_int nheads, nsectors;
161edc6143bSDavid Rhodus
162edc6143bSDavid Rhodus DEVICE_PRINTF(2, dev, "in attach\n");
163edc6143bSDavid Rhodus dsc = (ipsdisk_softc_t *)device_get_softc(dev);
164edc6143bSDavid Rhodus bzero(dsc, sizeof(ipsdisk_softc_t));
165edc6143bSDavid Rhodus adapter = device_get_parent(dev);
166edc6143bSDavid Rhodus dsc->dev = dev;
167edc6143bSDavid Rhodus dsc->sc = device_get_softc(adapter);
168edc6143bSDavid Rhodus dsc->unit = device_get_unit(dev);
169edc6143bSDavid Rhodus dsc->disk_number = (uintptr_t) device_get_ivars(dev);
170edc6143bSDavid Rhodus totalsectors = dsc->sc->drives[dsc->disk_number].sector_count;
171edc6143bSDavid Rhodus if ((totalsectors > 0x400000) &&
172edc6143bSDavid Rhodus ((dsc->sc->adapter_info.miscflags & 0x8) == 0)) {
173edc6143bSDavid Rhodus nheads = IPS_NORM_HEADS;
174edc6143bSDavid Rhodus nsectors = IPS_NORM_SECTORS;
175edc6143bSDavid Rhodus } else {
176edc6143bSDavid Rhodus nheads = IPS_COMP_HEADS;
177edc6143bSDavid Rhodus nsectors = IPS_COMP_SECTORS;
178edc6143bSDavid Rhodus }
179287d1e34SYONETANI Tomokazu devstat_add_entry(&dsc->stats, "ipsd", dsc->unit, DEV_BSIZE,
180287d1e34SYONETANI Tomokazu DEVSTAT_NO_ORDERED_TAGS,
181287d1e34SYONETANI Tomokazu DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_SCSI,
182287d1e34SYONETANI Tomokazu DEVSTAT_PRIORITY_DISK);
183a688b15cSMatthew Dillon dsc->ipsd_dev_t = disk_create(dsc->unit, &dsc->ipsd_disk, &ipsd_ops);
184edc6143bSDavid Rhodus dsc->ipsd_dev_t->si_drv1 = dsc;
185edc6143bSDavid Rhodus dsc->ipsd_dev_t->si_iosize_max = IPS_MAX_IO_SIZE;
186a688b15cSMatthew Dillon
187a688b15cSMatthew Dillon bzero(&info, sizeof(info));
188a688b15cSMatthew Dillon info.d_media_blksize = IPS_BLKSIZE; /* mandatory */
189a688b15cSMatthew Dillon info.d_media_blocks = totalsectors;
190a688b15cSMatthew Dillon
191a688b15cSMatthew Dillon info.d_type = DTYPE_ESDI; /* optional */
192a688b15cSMatthew Dillon info.d_nheads = nheads;
193a688b15cSMatthew Dillon info.d_secpertrack = nsectors;
194a688b15cSMatthew Dillon info.d_ncylinders = totalsectors / nheads / nsectors;
195a688b15cSMatthew Dillon info.d_secpercyl = nsectors / nheads;
196a688b15cSMatthew Dillon
197a688b15cSMatthew Dillon disk_setdiskinfo(&dsc->ipsd_disk, &info);
198a688b15cSMatthew Dillon
199edc6143bSDavid Rhodus device_printf(dev, "Logical Drive (%dMB)\n",
200edc6143bSDavid Rhodus dsc->sc->drives[dsc->disk_number].sector_count >> 11);
201edc6143bSDavid Rhodus return 0;
202edc6143bSDavid Rhodus }
203edc6143bSDavid Rhodus
204edc6143bSDavid Rhodus static int
ipsd_detach(device_t dev)205edc6143bSDavid Rhodus ipsd_detach(device_t dev)
206edc6143bSDavid Rhodus {
207edc6143bSDavid Rhodus ipsdisk_softc_t *dsc;
208edc6143bSDavid Rhodus
209edc6143bSDavid Rhodus DEVICE_PRINTF(2, dev, "in detach\n");
210edc6143bSDavid Rhodus dsc = (ipsdisk_softc_t *)device_get_softc(dev);
211edc6143bSDavid Rhodus if (dsc->state & IPS_DEV_OPEN)
212edc6143bSDavid Rhodus return (EBUSY);
213287d1e34SYONETANI Tomokazu devstat_remove_entry(&dsc->stats);
214edc6143bSDavid Rhodus disk_destroy(&dsc->ipsd_disk);
215edc6143bSDavid Rhodus return 0;
216edc6143bSDavid Rhodus }
217edc6143bSDavid Rhodus
2184751a114SMatthew Dillon
2194751a114SMatthew Dillon static int
ipsd_dump(struct dev_dump_args * ap)22029ede8d5SYONETANI Tomokazu ipsd_dump(struct dev_dump_args *ap)
2214751a114SMatthew Dillon {
22229ede8d5SYONETANI Tomokazu cdev_t dev = ap->a_head.a_dev;
2234751a114SMatthew Dillon ips_softc_t *sc;
2244751a114SMatthew Dillon ips_command_t *command;
2254751a114SMatthew Dillon ips_io_cmd *command_struct;
2264751a114SMatthew Dillon ipsdisk_softc_t *dsc;
2274751a114SMatthew Dillon off_t off;
2284751a114SMatthew Dillon uint8_t *va;
22929ede8d5SYONETANI Tomokazu size_t len;
2304751a114SMatthew Dillon int error = 0;
2314751a114SMatthew Dillon
2324751a114SMatthew Dillon dsc = dev->si_drv1;
2334751a114SMatthew Dillon if (dsc == NULL)
2344751a114SMatthew Dillon return (EINVAL);
2354751a114SMatthew Dillon sc = dsc->sc;
2364751a114SMatthew Dillon
2374751a114SMatthew Dillon if (ips_get_free_cmd(sc, &command, 0) != 0) {
238e3869ec7SSascha Wildner kprintf("ipsd: failed to get cmd for dump\n");
2394751a114SMatthew Dillon return (ENOMEM);
2404751a114SMatthew Dillon }
2414751a114SMatthew Dillon
2424751a114SMatthew Dillon command->data_dmatag = sc->sg_dmatag;
2434751a114SMatthew Dillon command->callback = ipsd_dump_block_complete;
2444751a114SMatthew Dillon
2454751a114SMatthew Dillon command_struct = (ips_io_cmd *)command->command_buffer;
2464751a114SMatthew Dillon command_struct->id = command->id;
2474751a114SMatthew Dillon command_struct->drivenum = sc->drives[dsc->disk_number].drivenum;
2484751a114SMatthew Dillon
24929ede8d5SYONETANI Tomokazu off = ap->a_offset;
25029ede8d5SYONETANI Tomokazu va = ap->a_virtual;
2514751a114SMatthew Dillon
25229ede8d5SYONETANI Tomokazu size_t length = ap->a_length;
2534751a114SMatthew Dillon while (length > 0) {
2544751a114SMatthew Dillon len = length > IPS_MAX_IO_SIZE ? IPS_MAX_IO_SIZE : length;
2554751a114SMatthew Dillon command_struct->lba = off / IPS_BLKSIZE;
2564751a114SMatthew Dillon if (bus_dmamap_load(command->data_dmatag, command->data_dmamap,
2574751a114SMatthew Dillon va, len, ipsd_dump_map_sg, command, 0) != 0) {
2584751a114SMatthew Dillon error = EIO;
2594751a114SMatthew Dillon break;
2604751a114SMatthew Dillon }
2614751a114SMatthew Dillon if (COMMAND_ERROR(&command->status)) {
2624751a114SMatthew Dillon error = EIO;
2634751a114SMatthew Dillon break;
2644751a114SMatthew Dillon }
2654751a114SMatthew Dillon
2664751a114SMatthew Dillon length -= len;
2674751a114SMatthew Dillon off += len;
2684751a114SMatthew Dillon va += len;
2694751a114SMatthew Dillon }
2704751a114SMatthew Dillon ips_insert_free_cmd(command->sc, command);
2714751a114SMatthew Dillon return(error);
2724751a114SMatthew Dillon }
2734751a114SMatthew Dillon
2744751a114SMatthew Dillon static void
ipsd_dump_map_sg(void * arg,bus_dma_segment_t * segs,int nsegs,int error)2754751a114SMatthew Dillon ipsd_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
2764751a114SMatthew Dillon {
2774751a114SMatthew Dillon ips_softc_t *sc;
2784751a114SMatthew Dillon ips_command_t *command;
2794751a114SMatthew Dillon ips_sg_element_t *sg_list;
2804751a114SMatthew Dillon ips_io_cmd *command_struct;
2814751a114SMatthew Dillon int i, length;
2824751a114SMatthew Dillon
2834751a114SMatthew Dillon command = (ips_command_t *)arg;
2844751a114SMatthew Dillon sc = command->sc;
2854751a114SMatthew Dillon length = 0;
2864751a114SMatthew Dillon
2874751a114SMatthew Dillon if (error) {
288e3869ec7SSascha Wildner kprintf("ipsd_dump_map_sg: error %d\n", error);
2894751a114SMatthew Dillon command->status.value = IPS_ERROR_STATUS;
2904751a114SMatthew Dillon return;
2914751a114SMatthew Dillon }
2924751a114SMatthew Dillon
2934751a114SMatthew Dillon command_struct = (ips_io_cmd *)command->command_buffer;
2944751a114SMatthew Dillon
2954751a114SMatthew Dillon if (nsegs != 1) {
2964751a114SMatthew Dillon command_struct->segnum = nsegs;
2974751a114SMatthew Dillon sg_list = (ips_sg_element_t *)((uint8_t *)
2984751a114SMatthew Dillon command->command_buffer + IPS_COMMAND_LEN);
2994751a114SMatthew Dillon for (i = 0; i < nsegs; i++) {
3004751a114SMatthew Dillon sg_list[i].addr = segs[i].ds_addr;
3014751a114SMatthew Dillon sg_list[i].len = segs[i].ds_len;
3024751a114SMatthew Dillon length += segs[i].ds_len;
3034751a114SMatthew Dillon }
3044751a114SMatthew Dillon command_struct->buffaddr =
3054751a114SMatthew Dillon (uint32_t)command->command_phys_addr + IPS_COMMAND_LEN;
3064751a114SMatthew Dillon command_struct->command = IPS_SG_WRITE_CMD;
3074751a114SMatthew Dillon } else {
3084751a114SMatthew Dillon command_struct->buffaddr = segs[0].ds_addr;
3094751a114SMatthew Dillon length = segs[0].ds_len;
3104751a114SMatthew Dillon command_struct->segnum = 0;
3114751a114SMatthew Dillon command_struct->command = IPS_WRITE_CMD;
3124751a114SMatthew Dillon }
3134751a114SMatthew Dillon
3144751a114SMatthew Dillon length = (length + IPS_BLKSIZE - 1) / IPS_BLKSIZE;
3154751a114SMatthew Dillon command_struct->length = length;
3164751a114SMatthew Dillon bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
3174751a114SMatthew Dillon BUS_DMASYNC_PREWRITE);
3184751a114SMatthew Dillon bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
3194751a114SMatthew Dillon BUS_DMASYNC_PREWRITE);
3204751a114SMatthew Dillon
3214751a114SMatthew Dillon sc->ips_issue_cmd(command);
3224751a114SMatthew Dillon sc->ips_poll_cmd(command);
3234751a114SMatthew Dillon return;
3244751a114SMatthew Dillon }
3254751a114SMatthew Dillon
3264751a114SMatthew Dillon static void
ipsd_dump_block_complete(ips_command_t * command)3274751a114SMatthew Dillon ipsd_dump_block_complete(ips_command_t *command)
3284751a114SMatthew Dillon {
3294751a114SMatthew Dillon if (COMMAND_ERROR(&command->status)) {
330e3869ec7SSascha Wildner kprintf("ipsd_dump completion error= 0x%x\n",
3314751a114SMatthew Dillon command->status.value);
3324751a114SMatthew Dillon }
3334751a114SMatthew Dillon bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
3344751a114SMatthew Dillon BUS_DMASYNC_POSTWRITE);
3354751a114SMatthew Dillon bus_dmamap_unload(command->data_dmatag, command->data_dmamap);
3364751a114SMatthew Dillon }
337