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