xref: /freebsd/sys/dev/ips/ips_disk.c (revision 2f513db7)
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 
113 	if ((iobuf->bio_cmd != BIO_READ) &&
114 	    (iobuf->bio_cmd != BIO_WRITE)) {
115 		biofinish(iobuf, NULL, EOPNOTSUPP);
116 		return;
117 	}
118 
119 	mtx_lock(&dsc->sc->queue_mtx);
120 	bioq_insert_tail(&dsc->sc->queue, iobuf);
121 	ips_start_io_request(dsc->sc);
122 	mtx_unlock(&dsc->sc->queue_mtx);
123 }
124 
125 static int ipsd_probe(device_t dev)
126 {
127 	DEVICE_PRINTF(2,dev, "in probe\n");
128 	device_set_desc(dev, "Logical Drive");
129 	return 0;
130 }
131 
132 static int ipsd_attach(device_t dev)
133 {
134 	device_t adapter;
135 	ipsdisk_softc_t *dsc;
136 	u_int totalsectors;
137 
138 	DEVICE_PRINTF(2,dev, "in attach\n");
139 
140 	dsc = (ipsdisk_softc_t *)device_get_softc(dev);
141 	bzero(dsc, sizeof(ipsdisk_softc_t));
142 	adapter = device_get_parent(dev);
143 	dsc->dev = dev;
144 	dsc->sc = device_get_softc(adapter);
145 	dsc->unit = device_get_unit(dev);
146 	dsc->disk_number = (uintptr_t) device_get_ivars(dev);
147 	dsc->ipsd_disk = disk_alloc();
148 	dsc->ipsd_disk->d_drv1 = dsc;
149 	dsc->ipsd_disk->d_name = "ipsd";
150 	dsc->ipsd_disk->d_maxsize = IPS_MAX_IO_SIZE;
151 	dsc->ipsd_disk->d_open = ipsd_open;
152 	dsc->ipsd_disk->d_close = ipsd_close;
153 	dsc->ipsd_disk->d_strategy = ipsd_strategy;
154 	dsc->ipsd_disk->d_dump = ipsd_dump;
155 
156 	totalsectors = dsc->sc->drives[dsc->disk_number].sector_count;
157    	if ((totalsectors > 0x400000) &&
158        			((dsc->sc->adapter_info.miscflags & 0x8) == 0)) {
159       		dsc->ipsd_disk->d_fwheads = IPS_NORM_HEADS;
160       		dsc->ipsd_disk->d_fwsectors = IPS_NORM_SECTORS;
161    	} else {
162       		dsc->ipsd_disk->d_fwheads = IPS_COMP_HEADS;
163       		dsc->ipsd_disk->d_fwsectors = IPS_COMP_SECTORS;
164    	}
165 	dsc->ipsd_disk->d_sectorsize = IPS_BLKSIZE;
166 	dsc->ipsd_disk->d_mediasize = (off_t)totalsectors * IPS_BLKSIZE;
167 	dsc->ipsd_disk->d_unit = dsc->unit;
168 	dsc->ipsd_disk->d_flags = 0;
169 	disk_create(dsc->ipsd_disk, DISK_VERSION);
170 
171 	device_printf(dev, "Logical Drive  (%dMB)\n",
172 		      dsc->sc->drives[dsc->disk_number].sector_count >> 11);
173 	return 0;
174 }
175 
176 static int ipsd_detach(device_t dev)
177 {
178 	ipsdisk_softc_t *dsc;
179 
180 	DEVICE_PRINTF(2, dev,"in detach\n");
181 	dsc = (ipsdisk_softc_t *)device_get_softc(dev);
182 	if(dsc->state & IPS_DEV_OPEN)
183 		return (EBUSY);
184 	disk_destroy(dsc->ipsd_disk);
185 	return 0;
186 }
187 
188 static int
189 ipsd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset,
190 	  size_t length)
191 {
192 	ipsdisk_softc_t *dsc;
193 	ips_softc_t *sc;
194 	ips_command_t *command;
195 	ips_io_cmd *command_struct;
196 	struct disk *dp;
197 	void *va;
198 	off_t off;
199 	size_t len;
200 	int error = 0;
201 
202 	dp = arg;
203 	dsc = dp->d_drv1;
204 
205 	if (dsc == NULL)
206 		return (EINVAL);
207 	sc = dsc->sc;
208 
209 	if (ips_get_free_cmd(sc, &command, 0) != 0) {
210 		printf("ipsd: failed to get cmd for dump\n");
211 		return (ENOMEM);
212 	}
213 
214 	command->data_dmatag = sc->sg_dmatag;
215 	command->callback = ipsd_dump_block_complete;
216 
217 	command_struct = (ips_io_cmd *)command->command_buffer;
218 	command_struct->id = command->id;
219 	command_struct->drivenum= sc->drives[dsc->disk_number].drivenum;
220 
221 	off = offset;
222 	va = virtual;
223 
224 	while (length > 0) {
225 		len =
226 		    (length > IPS_MAX_IO_SIZE) ? IPS_MAX_IO_SIZE : length;
227 
228 		command_struct->lba = off / IPS_BLKSIZE;
229 
230 		if (bus_dmamap_load(command->data_dmatag, command->data_dmamap,
231 		    va, len, ipsd_dump_map_sg, command, BUS_DMA_NOWAIT) != 0) {
232 			error = EIO;
233 			break;
234 		}
235 		if (COMMAND_ERROR(command)) {
236 			error = EIO;
237 			break;
238 		}
239 
240 		length -= len;
241 		off += len;
242 		va = (uint8_t *)va + len;
243 	}
244 
245 	ips_insert_free_cmd(command->sc, command);
246 	return (error);
247 }
248 
249 static void
250 ipsd_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
251 {
252 	ips_softc_t *sc;
253 	ips_command_t *command;
254 	ips_sg_element_t *sg_list;
255 	ips_io_cmd *command_struct;
256 	int i, length;
257 
258 	command = (ips_command_t *)arg;
259 	sc = command->sc;
260 	length = 0;
261 
262 	if (error) {
263 		printf("ipsd_dump_map_sg: error %d\n", error);
264 		ips_set_error(command, error);
265 		return;
266 	}
267 
268 	command_struct = (ips_io_cmd *)command->command_buffer;
269 
270 	if (nsegs != 1) {
271 		command_struct->segnum = nsegs;
272 		sg_list = (ips_sg_element_t *)((uint8_t *)
273 		    command->command_buffer + IPS_COMMAND_LEN);
274 		for (i = 0; i < nsegs; i++) {
275 			sg_list[i].addr = segs[i].ds_addr;
276 			sg_list[i].len = segs[i].ds_len;
277 			length += segs[i].ds_len;
278 		}
279 		command_struct->buffaddr =
280 		    (uint32_t)command->command_phys_addr + IPS_COMMAND_LEN;
281 		command_struct->command = IPS_SG_WRITE_CMD;
282 	} else {
283 		command_struct->buffaddr = segs[0].ds_addr;
284 		length = segs[0].ds_len;
285 		command_struct->segnum = 0;
286 		command_struct->command = IPS_WRITE_CMD;
287 	}
288 
289 	length = (length + IPS_BLKSIZE - 1) / IPS_BLKSIZE;
290 	command_struct->length = length;
291 	bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
292 	    BUS_DMASYNC_PREWRITE);
293 	bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
294 	    BUS_DMASYNC_PREWRITE);
295 
296 	sc->ips_issue_cmd(command);
297 	sc->ips_poll_cmd(command);
298 	return;
299 }
300 
301 static void
302 ipsd_dump_block_complete(ips_command_t *command)
303 {
304 
305 	if (COMMAND_ERROR(command))
306 		printf("ipsd_dump completion error= 0x%x\n",
307 		    command->status.value);
308 
309 	bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
310 	    BUS_DMASYNC_POSTWRITE);
311 	bus_dmamap_unload(command->data_dmatag, command->data_dmamap);
312 }
313