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