xref: /freebsd/sys/dev/ips/ips_disk.c (revision c98323078dede7579020518ec84cdcb478e5c142)
1 /*-
2  * Written by: David Jeffery
3  * Copyright (c) 2002 Adaptec Inc.
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 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <dev/ips/ips.h>
32 #include <dev/ips/ips_disk.h>
33 #include <sys/stat.h>
34 
35 static int ipsd_probe(device_t dev);
36 static int ipsd_attach(device_t dev);
37 static int ipsd_detach(device_t dev);
38 
39 static disk_open_t ipsd_open;
40 static disk_close_t ipsd_close;
41 static disk_strategy_t ipsd_strategy;
42 
43 static device_method_t ipsd_methods[] = {
44 	DEVMETHOD(device_probe,		ipsd_probe),
45 	DEVMETHOD(device_attach,	ipsd_attach),
46 	DEVMETHOD(device_detach,	ipsd_detach),
47 	{ 0, 0 }
48 };
49 
50 
51 static driver_t ipsd_driver = {
52 	"ipsd",
53 	ipsd_methods,
54 	sizeof(ipsdisk_softc_t)
55 };
56 
57 static devclass_t ipsd_devclass;
58 DRIVER_MODULE(ipsd, ips, ipsd_driver, ipsd_devclass, 0, 0);
59 
60 /* handle opening of disk device.  It must set up all
61    information about the geometry and size of the disk */
62 static int ipsd_open(struct disk *dp)
63 {
64 	ipsdisk_softc_t *dsc = dp->d_drv1;
65 
66 	dsc->state |= IPS_DEV_OPEN;
67 	DEVICE_PRINTF(2, dsc->dev, "I'm open\n");
68        	return 0;
69 }
70 
71 static int ipsd_close(struct disk *dp)
72 {
73 	ipsdisk_softc_t *dsc = dp->d_drv1;
74 	dsc->state &= ~IPS_DEV_OPEN;
75 	DEVICE_PRINTF(2, dsc->dev, "I'm closed for the day\n");
76         return 0;
77 }
78 
79 /* ipsd_finish is called to clean up and return a completed IO request */
80 void ipsd_finish(struct bio *iobuf)
81 {
82 	ipsdisk_softc_t *dsc;
83 	dsc = iobuf->bio_disk->d_drv1;
84 
85 	if (iobuf->bio_flags & BIO_ERROR) {
86 		ipsdisk_softc_t *dsc;
87 		dsc = iobuf->bio_disk->d_drv1;
88 		device_printf(dsc->dev, "iobuf error %d\n", iobuf->bio_error);
89 	} else
90 		iobuf->bio_resid = 0;
91 
92 	biodone(iobuf);
93 	ips_start_io_request(dsc->sc);
94 }
95 
96 
97 static void ipsd_strategy(struct bio *iobuf)
98 {
99 	ipsdisk_softc_t *dsc;
100 
101 	dsc = iobuf->bio_disk->d_drv1;
102 	DEVICE_PRINTF(8,dsc->dev,"in strategy\n");
103 	iobuf->bio_driver1 = (void *)(uintptr_t)dsc->sc->drives[dsc->disk_number].drivenum;
104 	mtx_lock(&dsc->sc->queue_mtx);
105 	bioq_disksort(&dsc->sc->queue, iobuf);
106 	mtx_unlock(&dsc->sc->queue_mtx);
107 	ips_start_io_request(dsc->sc);
108 }
109 
110 static int ipsd_probe(device_t dev)
111 {
112 	DEVICE_PRINTF(2,dev, "in probe\n");
113 	device_set_desc(dev, "Logical Drive");
114 	return 0;
115 }
116 
117 static int ipsd_attach(device_t dev)
118 {
119 	device_t adapter;
120 	ipsdisk_softc_t *dsc;
121 	u_int totalsectors;
122 
123 	DEVICE_PRINTF(2,dev, "in attach\n");
124 
125 	dsc = (ipsdisk_softc_t *)device_get_softc(dev);
126 	bzero(dsc, sizeof(ipsdisk_softc_t));
127 	adapter = device_get_parent(dev);
128 	dsc->dev = dev;
129 	dsc->sc = device_get_softc(adapter);
130 	dsc->unit = device_get_unit(dev);
131 	dsc->disk_number = (uintptr_t) device_get_ivars(dev);
132 	dsc->ipsd_disk = disk_alloc();
133 	dsc->ipsd_disk->d_drv1 = dsc;
134 	dsc->ipsd_disk->d_name = "ipsd";
135 	dsc->ipsd_disk->d_maxsize = IPS_MAX_IO_SIZE;
136 	dsc->ipsd_disk->d_open = ipsd_open;
137 	dsc->ipsd_disk->d_close = ipsd_close;
138 	dsc->ipsd_disk->d_strategy = ipsd_strategy;
139 
140 	totalsectors = dsc->sc->drives[dsc->disk_number].sector_count;
141    	if ((totalsectors > 0x400000) &&
142        			((dsc->sc->adapter_info.miscflags & 0x8) == 0)) {
143       		dsc->ipsd_disk->d_fwheads = IPS_NORM_HEADS;
144       		dsc->ipsd_disk->d_fwsectors = IPS_NORM_SECTORS;
145    	} else {
146       		dsc->ipsd_disk->d_fwheads = IPS_COMP_HEADS;
147       		dsc->ipsd_disk->d_fwsectors = IPS_COMP_SECTORS;
148    	}
149 	dsc->ipsd_disk->d_sectorsize = IPS_BLKSIZE;
150 	dsc->ipsd_disk->d_mediasize = (off_t)totalsectors * IPS_BLKSIZE;
151 	dsc->ipsd_disk->d_unit = dsc->unit;
152 	dsc->ipsd_disk->d_flags = DISKFLAG_NEEDSGIANT;
153 	disk_create(dsc->ipsd_disk, DISK_VERSION);
154 
155 	device_printf(dev, "Logical Drive  (%dMB)\n",
156 		      dsc->sc->drives[dsc->disk_number].sector_count >> 11);
157 	return 0;
158 }
159 
160 static int ipsd_detach(device_t dev)
161 {
162 	ipsdisk_softc_t *dsc;
163 
164 	DEVICE_PRINTF(2, dev,"in detach\n");
165 	dsc = (ipsdisk_softc_t *)device_get_softc(dev);
166 	if(dsc->state & IPS_DEV_OPEN)
167 		return (EBUSY);
168 	disk_destroy(dsc->ipsd_disk);
169 	return 0;
170 }
171