xref: /freebsd/sys/dev/ips/ips_disk.c (revision 2eea70515c189e5c7c5457efd3e875ed395d736b)
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 int ipsd_dump(void *arg, void *virtual, vm_offset_t physical,
40 		     off_t offset, size_t length);
41 static void ipsd_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs,
42 			     int error);
43 static void ipsd_dump_block_complete(ips_command_t *command);
44 
45 static disk_open_t ipsd_open;
46 static disk_close_t ipsd_close;
47 static disk_strategy_t ipsd_strategy;
48 
49 static device_method_t ipsd_methods[] = {
50 	DEVMETHOD(device_probe,		ipsd_probe),
51 	DEVMETHOD(device_attach,	ipsd_attach),
52 	DEVMETHOD(device_detach,	ipsd_detach),
53 	{ 0, 0 }
54 };
55 
56 static driver_t ipsd_driver = {
57 	"ipsd",
58 	ipsd_methods,
59 	sizeof(ipsdisk_softc_t)
60 };
61 
62 static devclass_t ipsd_devclass;
63 DRIVER_MODULE(ipsd, ips, ipsd_driver, ipsd_devclass, 0, 0);
64 
65 /* handle opening of disk device.  It must set up all
66    information about the geometry and size of the disk */
67 static int ipsd_open(struct disk *dp)
68 {
69 	ipsdisk_softc_t *dsc = dp->d_drv1;
70 
71 	dsc->state |= IPS_DEV_OPEN;
72 	DEVICE_PRINTF(2, dsc->dev, "I'm open\n");
73        	return 0;
74 }
75 
76 static int ipsd_close(struct disk *dp)
77 {
78 	ipsdisk_softc_t *dsc = dp->d_drv1;
79 	dsc->state &= ~IPS_DEV_OPEN;
80 	DEVICE_PRINTF(2, dsc->dev, "I'm closed for the day\n");
81         return 0;
82 }
83 
84 /* ipsd_finish is called to clean up and return a completed IO request */
85 void ipsd_finish(struct bio *iobuf)
86 {
87 	ipsdisk_softc_t *dsc;
88 	dsc = iobuf->bio_disk->d_drv1;
89 
90 	if (iobuf->bio_flags & BIO_ERROR) {
91 		ipsdisk_softc_t *dsc;
92 		dsc = iobuf->bio_disk->d_drv1;
93 		device_printf(dsc->dev, "iobuf error %d\n", iobuf->bio_error);
94 	} else
95 		iobuf->bio_resid = 0;
96 
97 	biodone(iobuf);
98 	ips_start_io_request(dsc->sc);
99 }
100 
101 
102 static void ipsd_strategy(struct bio *iobuf)
103 {
104 	ipsdisk_softc_t *dsc;
105 
106 	dsc = iobuf->bio_disk->d_drv1;
107 	DEVICE_PRINTF(8,dsc->dev,"in strategy\n");
108 	iobuf->bio_driver1 = (void *)(uintptr_t)dsc->sc->drives[dsc->disk_number].drivenum;
109 	mtx_lock(&dsc->sc->queue_mtx);
110 	bioq_insert_tail(&dsc->sc->queue, iobuf);
111 	ips_start_io_request(dsc->sc);
112 	mtx_unlock(&dsc->sc->queue_mtx);
113 }
114 
115 static int ipsd_probe(device_t dev)
116 {
117 	DEVICE_PRINTF(2,dev, "in probe\n");
118 	device_set_desc(dev, "Logical Drive");
119 	return 0;
120 }
121 
122 static int ipsd_attach(device_t dev)
123 {
124 	device_t adapter;
125 	ipsdisk_softc_t *dsc;
126 	u_int totalsectors;
127 
128 	DEVICE_PRINTF(2,dev, "in attach\n");
129 
130 	dsc = (ipsdisk_softc_t *)device_get_softc(dev);
131 	bzero(dsc, sizeof(ipsdisk_softc_t));
132 	adapter = device_get_parent(dev);
133 	dsc->dev = dev;
134 	dsc->sc = device_get_softc(adapter);
135 	dsc->unit = device_get_unit(dev);
136 	dsc->disk_number = (uintptr_t) device_get_ivars(dev);
137 	dsc->ipsd_disk = disk_alloc();
138 	dsc->ipsd_disk->d_drv1 = dsc;
139 	dsc->ipsd_disk->d_name = "ipsd";
140 	dsc->ipsd_disk->d_maxsize = IPS_MAX_IO_SIZE;
141 	dsc->ipsd_disk->d_open = ipsd_open;
142 	dsc->ipsd_disk->d_close = ipsd_close;
143 	dsc->ipsd_disk->d_strategy = ipsd_strategy;
144 	dsc->ipsd_disk->d_dump = ipsd_dump;
145 
146 	totalsectors = dsc->sc->drives[dsc->disk_number].sector_count;
147    	if ((totalsectors > 0x400000) &&
148        			((dsc->sc->adapter_info.miscflags & 0x8) == 0)) {
149       		dsc->ipsd_disk->d_fwheads = IPS_NORM_HEADS;
150       		dsc->ipsd_disk->d_fwsectors = IPS_NORM_SECTORS;
151    	} else {
152       		dsc->ipsd_disk->d_fwheads = IPS_COMP_HEADS;
153       		dsc->ipsd_disk->d_fwsectors = IPS_COMP_SECTORS;
154    	}
155 	dsc->ipsd_disk->d_sectorsize = IPS_BLKSIZE;
156 	dsc->ipsd_disk->d_mediasize = (off_t)totalsectors * IPS_BLKSIZE;
157 	dsc->ipsd_disk->d_unit = dsc->unit;
158 	dsc->ipsd_disk->d_flags = 0;
159 	disk_create(dsc->ipsd_disk, DISK_VERSION);
160 
161 	device_printf(dev, "Logical Drive  (%dMB)\n",
162 		      dsc->sc->drives[dsc->disk_number].sector_count >> 11);
163 	return 0;
164 }
165 
166 static int ipsd_detach(device_t dev)
167 {
168 	ipsdisk_softc_t *dsc;
169 
170 	DEVICE_PRINTF(2, dev,"in detach\n");
171 	dsc = (ipsdisk_softc_t *)device_get_softc(dev);
172 	if(dsc->state & IPS_DEV_OPEN)
173 		return (EBUSY);
174 	disk_destroy(dsc->ipsd_disk);
175 	return 0;
176 }
177 
178 static int
179 ipsd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset,
180 	  size_t length)
181 {
182 	ipsdisk_softc_t *dsc;
183 	ips_softc_t *sc;
184 	ips_command_t *command;
185 	ips_io_cmd *command_struct;
186 	struct disk *dp;
187 	void *va;
188 	off_t off;
189 	size_t len;
190 	int error = 0;
191 
192 	dp = arg;
193 	dsc = dp->d_drv1;
194 	sc = dsc->sc;
195 
196 	if (dsc == NULL)
197 		return (EINVAL);
198 
199 	if (ips_get_free_cmd(sc, &command, 0) != 0) {
200 		printf("ipsd: failed to get cmd for dump\n");
201 		return (ENOMEM);
202 	}
203 
204 	command->data_dmatag = sc->sg_dmatag;
205 	command->callback = ipsd_dump_block_complete;
206 
207 	command_struct = (ips_io_cmd *)command->command_buffer;
208 	command_struct->id = command->id;
209 	command_struct->drivenum= sc->drives[dsc->disk_number].drivenum;
210 
211 	off = offset;
212 	va = virtual;
213 
214 	while (length > 0) {
215 		len =
216 		    (length > IPS_MAX_IO_SIZE) ? IPS_MAX_IO_SIZE : length;
217 
218 		command_struct->lba = off / IPS_BLKSIZE;
219 
220 		if (bus_dmamap_load(command->data_dmatag, command->data_dmamap,
221 		    va, len, ipsd_dump_map_sg, command, BUS_DMA_NOWAIT) != 0) {
222 			error = EIO;
223 			break;
224 		}
225 		if (COMMAND_ERROR(command)) {
226 			error = EIO;
227 			break;
228 		}
229 
230 		length -= len;
231 		off += len;
232 		va = (uint8_t *)va + len;
233 	}
234 
235 	ips_insert_free_cmd(command->sc, command);
236 	return (error);
237 }
238 
239 static void
240 ipsd_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
241 {
242 	ips_softc_t *sc;
243 	ips_command_t *command;
244 	ips_sg_element_t *sg_list;
245 	ips_io_cmd *command_struct;
246 	int i, length;
247 
248 	command = (ips_command_t *)arg;
249 	sc = command->sc;
250 	length = 0;
251 
252 	if (error) {
253 		printf("ipsd_dump_map_sg: error %d\n", error);
254 		ips_set_error(command, error);
255 		return;
256 	}
257 
258 	command_struct = (ips_io_cmd *)command->command_buffer;
259 
260 	if (nsegs != 1) {
261 		command_struct->segnum = nsegs;
262 		sg_list = (ips_sg_element_t *)((uint8_t *)
263 		    command->command_buffer + IPS_COMMAND_LEN);
264 		for (i = 0; i < nsegs; i++) {
265 			sg_list[i].addr = segs[i].ds_addr;
266 			sg_list[i].len = segs[i].ds_len;
267 			length += segs[i].ds_len;
268 		}
269 		command_struct->buffaddr =
270 		    (uint32_t)command->command_phys_addr + IPS_COMMAND_LEN;
271 		command_struct->command = IPS_SG_WRITE_CMD;
272 	} else {
273 		command_struct->buffaddr = segs[0].ds_addr;
274 		length = segs[0].ds_len;
275 		command_struct->segnum = 0;
276 		command_struct->command = IPS_WRITE_CMD;
277 	}
278 
279 	length = (length + IPS_BLKSIZE - 1) / IPS_BLKSIZE;
280 	command_struct->length = length;
281 	bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
282 	    BUS_DMASYNC_PREWRITE);
283 	bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
284 	    BUS_DMASYNC_PREWRITE);
285 
286 	sc->ips_issue_cmd(command);
287 	sc->ips_poll_cmd(command);
288 	return;
289 }
290 
291 static void
292 ipsd_dump_block_complete(ips_command_t *command)
293 {
294 
295 	if (COMMAND_ERROR(command))
296 		printf("ipsd_dump completion error= 0x%x\n",
297 		    command->status.value);
298 
299 	bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
300 	    BUS_DMASYNC_POSTWRITE);
301 	bus_dmamap_unload(command->data_dmatag, command->data_dmamap);
302 }
303