xref: /dflybsd-src/sys/dev/raid/ips/ips_disk.c (revision ad30b684748061ca0c68e4a5ca21b45c240c52c5)
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  * $FreeBSD: src/sys/dev/ips/ips_disk.c,v 1.4 2003/09/22 04:59:07 njl Exp $
28  * $DragonFly: src/sys/dev/raid/ips/ips_disk.c,v 1.12 2007/05/15 00:01:04 dillon Exp $
29  */
30 
31 #include <sys/devicestat.h>
32 #include <dev/raid/ips/ips.h>
33 #include <dev/raid/ips/ips_disk.h>
34 #include <sys/stat.h>
35 
36 #include <vm/vm.h>
37 #include <vm/pmap.h>
38 #include <machine/md_var.h>
39 
40 static int ipsd_probe(device_t dev);
41 static int ipsd_attach(device_t dev);
42 static int ipsd_detach(device_t dev);
43 
44 #if 0
45 static int ipsd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length);
46 static void ipsd_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs,
47 			     int error);
48 static void ipsd_dump_block_complete(ips_command_t *command);
49 #endif
50 
51 static d_open_t ipsd_open;
52 static d_close_t ipsd_close;
53 static d_strategy_t ipsd_strategy;
54 static d_dump_t ipsd_dump_helper;
55 
56 static struct dev_ops ipsd_ops = {
57 	{ "ipsd", IPSD_CDEV_MAJOR, D_DISK },
58 	.d_open	=	ipsd_open,
59 	.d_close =	ipsd_close,
60 	.d_strategy =	ipsd_strategy,
61 	.d_read	=	physread,
62 	.d_write =	physwrite,
63 	.d_dump	=	ipsd_dump_helper,
64 };
65 
66 static device_method_t ipsd_methods[] = {
67 	DEVMETHOD(device_probe,		ipsd_probe),
68 	DEVMETHOD(device_attach,	ipsd_attach),
69 	DEVMETHOD(device_detach,	ipsd_detach),
70 	{ 0, 0 }
71 };
72 
73 
74 static driver_t ipsd_driver = {
75 	"ipsd",
76 	ipsd_methods,
77 	sizeof(ipsdisk_softc_t)
78 };
79 
80 static devclass_t ipsd_devclass;
81 DRIVER_MODULE(ipsd, ips, ipsd_driver, ipsd_devclass, 0, 0);
82 
83 /*
84  * handle opening of disk device.  It must set up all information about
85  * the geometry and size of the disk
86  */
87 static int
88 ipsd_open(struct dev_open_args *ap)
89 {
90 	cdev_t dev = ap->a_head.a_dev;
91 	ipsdisk_softc_t *dsc = dev->si_drv1;
92 
93 	if (dsc == NULL)
94 		return (ENXIO);
95 	dsc->state |= IPS_DEV_OPEN;
96 	DEVICE_PRINTF(2, dsc->dev, "I'm open\n");
97 	return 0;
98 }
99 
100 static int
101 ipsd_close(struct dev_close_args *ap)
102 {
103 	cdev_t dev = ap->a_head.a_dev;
104 	ipsdisk_softc_t *dsc = dev->si_drv1;
105 
106 	dsc->state &= ~IPS_DEV_OPEN;
107 	DEVICE_PRINTF(2, dsc->dev, "I'm closed for the day\n");
108 	return 0;
109 }
110 
111 /* ipsd_finish is called to clean up and return a completed IO request */
112 void
113 ipsd_finish(struct bio *bio)
114 {
115 	struct buf *bp = bio->bio_buf;
116 	ipsdisk_softc_t *dsc;
117 
118 	dsc = bio->bio_driver_info;
119 	if (bp->b_flags & B_ERROR) {
120 		device_printf(dsc->dev, "iobuf error %d\n", bp->b_error);
121 	} else {
122 		bp->b_resid = 0;
123 	}
124 	devstat_end_transaction_buf(&dsc->stats, bp);
125 	biodone(bio);
126 	ips_start_io_request(dsc->sc);
127 }
128 
129 
130 static int
131 ipsd_strategy(struct dev_strategy_args *ap)
132 {
133 	cdev_t dev = ap->a_head.a_dev;
134 	struct bio *bio = ap->a_bio;
135 	ipsdisk_softc_t *dsc;
136 
137 	dsc = dev->si_drv1;
138 	DEVICE_PRINTF(8, dsc->dev, "in strategy\n");
139 	bio->bio_driver_info = dsc;
140 	devstat_start_transaction(&dsc->stats);
141 	lockmgr(&dsc->sc->queue_lock, LK_EXCLUSIVE|LK_RETRY);
142 	bioq_insert_tail(&dsc->sc->bio_queue, bio);
143 	ips_start_io_request(dsc->sc);
144 	lockmgr(&dsc->sc->queue_lock, LK_RELEASE);
145 	return(0);
146 }
147 
148 static int
149 ipsd_probe(device_t dev)
150 {
151 	DEVICE_PRINTF(2, dev, "in probe\n");
152 	device_set_desc(dev, "Logical Drive");
153 	return 0;
154 }
155 
156 static int
157 ipsd_attach(device_t dev)
158 {
159 	device_t adapter;
160 	ipsdisk_softc_t *dsc;
161 	struct disk_info info;
162 	u_int totalsectors;
163 	u_int nheads, nsectors;
164 
165 	DEVICE_PRINTF(2, dev, "in attach\n");
166 	dsc = (ipsdisk_softc_t *)device_get_softc(dev);
167 	bzero(dsc, sizeof(ipsdisk_softc_t));
168 	adapter = device_get_parent(dev);
169 	dsc->dev = dev;
170 	dsc->sc = device_get_softc(adapter);
171 	dsc->unit = device_get_unit(dev);
172 	dsc->disk_number = (uintptr_t) device_get_ivars(dev);
173 	totalsectors = dsc->sc->drives[dsc->disk_number].sector_count;
174 	if ((totalsectors > 0x400000) &&
175 	    ((dsc->sc->adapter_info.miscflags & 0x8) == 0)) {
176 		nheads = IPS_NORM_HEADS;
177 		nsectors = IPS_NORM_SECTORS;
178 	} else {
179 		nheads = IPS_COMP_HEADS;
180 		nsectors = IPS_COMP_SECTORS;
181 	}
182 	devstat_add_entry(&dsc->stats, "ipsd", dsc->unit, DEV_BSIZE,
183 			  DEVSTAT_NO_ORDERED_TAGS,
184 			  DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_SCSI,
185 			  DEVSTAT_PRIORITY_DISK);
186 	dsc->ipsd_dev_t = disk_create(dsc->unit, &dsc->ipsd_disk, &ipsd_ops);
187 	dsc->ipsd_dev_t->si_drv1 = dsc;
188 	dsc->ipsd_dev_t->si_iosize_max = IPS_MAX_IO_SIZE;
189 
190 	bzero(&info, sizeof(info));
191 	info.d_media_blksize	= IPS_BLKSIZE;		/* mandatory */
192 	info.d_media_blocks	= totalsectors;
193 
194 	info.d_type		= DTYPE_ESDI;		/* optional */
195 	info.d_nheads		= nheads;
196 	info.d_secpertrack	= nsectors;
197 	info.d_ncylinders	= totalsectors / nheads / nsectors;
198 	info.d_secpercyl	= nsectors / nheads;
199 
200 	disk_setdiskinfo(&dsc->ipsd_disk, &info);
201 
202 	device_printf(dev, "Logical Drive  (%dMB)\n",
203 	    dsc->sc->drives[dsc->disk_number].sector_count >> 11);
204 	return 0;
205 }
206 
207 static int
208 ipsd_detach(device_t dev)
209 {
210 	ipsdisk_softc_t *dsc;
211 
212 	DEVICE_PRINTF(2, dev, "in detach\n");
213 	dsc = (ipsdisk_softc_t *)device_get_softc(dev);
214 	if (dsc->state & IPS_DEV_OPEN)
215 		return (EBUSY);
216 	devstat_remove_entry(&dsc->stats);
217 	disk_destroy(&dsc->ipsd_disk);
218 	return 0;
219 }
220 
221 static int
222 ipsd_dump_helper(struct dev_dump_args *ap)
223 {
224 	kprintf("dump support for IPS not yet working, will not dump\n");
225 	return (ENODEV);
226 
227 #if 0
228 	long blkcnt;
229 	caddr_t va;
230 	vm_offset_t addr, a;
231 	int dumppages = MAXDUMPPGS;
232 	int i;
233 
234 	addr = 0;
235 	blkcnt = howmany(PAGE_SIZE, secsize);
236 	while (count > 0) {
237 		va = NULL;
238 		if (count / blkcnt < dumppages)
239 			dumppages = count / blkcnt;
240 		for (i = 0; i < dumppages; i++) {
241 			a = addr + (i * PAGE_SIZE);
242 			if (!is_physical_memory(a))
243 				a = 0;
244 			va = pmap_kenter_temporary(trunc_page(a), i);
245 		}
246 
247 		ipsd_dump(dev, va, 0, blkno, PAGE_SIZE * dumppages);
248 		if (dumpstatus(addr, (off_t)count * DEV_BSIZE) < 0)
249 			return (EINTR);
250 		blkno += blkcnt * dumppages;
251 		count -= blkcnt * dumppages;
252 		addr += PAGE_SIZE * dumppages;
253 	}
254 	return (0);
255 #endif
256 }
257 
258 #if 0
259 
260 static int
261 ipsd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset,
262           size_t length)
263 {
264 	cdev_t dev = arg;
265 	ips_softc_t *sc;
266 	ips_command_t *command;
267 	ips_io_cmd *command_struct;
268 	ipsdisk_softc_t *dsc;
269 	off_t off;
270 	uint8_t *va;
271 	int len;
272 	int error = 0;
273 
274 	dsc = dev->si_drv1;
275 	if (dsc == NULL)
276 		return (EINVAL);
277 	sc = dsc->sc;
278 
279 	if (ips_get_free_cmd(sc, &command, 0) != 0) {
280 		kprintf("ipsd: failed to get cmd for dump\n");
281 		return (ENOMEM);
282 	}
283 
284 	command->data_dmatag = sc->sg_dmatag;
285 	command->callback = ipsd_dump_block_complete;
286 
287 	command_struct = (ips_io_cmd *)command->command_buffer;
288 	command_struct->id = command->id;
289 	command_struct->drivenum = sc->drives[dsc->disk_number].drivenum;
290 
291 	off = offset;
292 	va = virtual;
293 
294 	while (length > 0) {
295 		len = length > IPS_MAX_IO_SIZE ? IPS_MAX_IO_SIZE : length;
296 		command_struct->lba = off / IPS_BLKSIZE;
297 		if (bus_dmamap_load(command->data_dmatag, command->data_dmamap,
298 		    va, len, ipsd_dump_map_sg, command, 0) != 0) {
299 			error = EIO;
300 			break;
301 		}
302 		if (COMMAND_ERROR(&command->status)) {
303 			error = EIO;
304 			break;
305 		}
306 
307 		length -= len;
308 		off += len;
309 		va += len;
310 	}
311 	ips_insert_free_cmd(command->sc, command);
312 	return(error);
313 }
314 
315 static void
316 ipsd_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
317 {
318 	ips_softc_t *sc;
319 	ips_command_t *command;
320 	ips_sg_element_t *sg_list;
321 	ips_io_cmd *command_struct;
322 	int i, length;
323 
324 	command = (ips_command_t *)arg;
325 	sc = command->sc;
326 	length = 0;
327 
328 	if (error) {
329 		kprintf("ipsd_dump_map_sg: error %d\n", error);
330 		command->status.value = IPS_ERROR_STATUS;
331 		return;
332 	}
333 
334 	command_struct = (ips_io_cmd *)command->command_buffer;
335 
336 	if (nsegs != 1) {
337 		command_struct->segnum = nsegs;
338 		sg_list = (ips_sg_element_t *)((uint8_t *)
339 		    command->command_buffer + IPS_COMMAND_LEN);
340 		for (i = 0; i < nsegs; i++) {
341 			sg_list[i].addr = segs[i].ds_addr;
342 			sg_list[i].len = segs[i].ds_len;
343 			length += segs[i].ds_len;
344 		}
345 		command_struct->buffaddr =
346 		    (uint32_t)command->command_phys_addr + IPS_COMMAND_LEN;
347 		command_struct->command = IPS_SG_WRITE_CMD;
348 	} else {
349 		command_struct->buffaddr = segs[0].ds_addr;
350 		length = segs[0].ds_len;
351 		command_struct->segnum = 0;
352 		command_struct->command = IPS_WRITE_CMD;
353 	}
354 
355 	length = (length + IPS_BLKSIZE - 1) / IPS_BLKSIZE;
356 	command_struct->length = length;
357 	bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
358 	    BUS_DMASYNC_PREWRITE);
359 	bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
360 	    BUS_DMASYNC_PREWRITE);
361 
362 	sc->ips_issue_cmd(command);
363 	sc->ips_poll_cmd(command);
364 	return;
365 }
366 
367 static void
368 ipsd_dump_block_complete(ips_command_t *command)
369 {
370 	if (COMMAND_ERROR(&command->status)) {
371 		kprintf("ipsd_dump completion error= 0x%x\n",
372 		       command->status.value);
373 	}
374 	bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
375 	    BUS_DMASYNC_POSTWRITE);
376 	bus_dmamap_unload(command->data_dmatag, command->data_dmamap);
377 }
378 
379 #endif
380