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