2 * Copyright (c) 2001,2002 Søren Schmidt <sos@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
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 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * $FreeBSD: src/sys/dev/pst/pst-raid.c,v 1.2.2.1 2002/08/18 12:32:36 sos Exp $
29 * $DragonFly: src/sys/dev/raid/pst/pst-raid.c,v 1.11 2004/09/15 13:54:42 joerg Exp $
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
41 #include <sys/devicestat.h>
42 #include <sys/eventhandler.h>
43 #include <sys/malloc.h>
47 #include <machine/stdarg.h>
48 #include <machine/resource.h>
49 #include <machine/bus.h>
51 #include <bus/pci/pcivar.h>
52 #include <bus/pci/pcireg.h>
56 /* device structures */
57 static d_strategy_t pststrategy;
58 static struct cdevsw pst_cdevsw = {
66 /* close */ nullclose,
68 /* write */ physwrite,
72 /* strat */ pststrategy,
78 struct iop_softc *iop;
79 struct i2o_lct_entry *lct;
80 struct i2o_bsa_device *info;
84 struct buf_queue_head queue;
89 struct pst_softc *psc; /* pointer to softc */
90 u_int32_t mfa; /* frame addreess */
91 struct callout timeout; /* handle for untimeout */
92 struct buf *bp; /* associated bio ptr */
96 static int pst_probe(device_t);
97 static int pst_attach(device_t);
99 static int pst_shutdown(device_t);
101 static void pst_start(struct pst_softc *);
102 static void pst_done(struct iop_softc *, u_int32_t, struct i2o_single_reply *);
103 static int pst_rw(struct pst_request *);
104 static void pst_timeout(void *);
105 static void bpack(int8_t *, int8_t *, int);
108 static MALLOC_DEFINE(M_PSTRAID, "pst", "Promise SuperTrak RAID driver");
111 pst_add_raid(struct iop_softc *sc, struct i2o_lct_entry *lct)
113 struct pst_softc *psc;
114 device_t child = device_add_child(sc->dev, "pst", -1);
118 psc = malloc(sizeof(struct pst_softc), M_PSTRAID, M_INTWAIT | M_ZERO);
121 device_set_softc(child, psc);
122 return bus_generic_attach(sc->dev);
126 pst_probe(device_t dev)
128 device_set_desc(dev, "Promise SuperTrak RAID");
133 pst_attach(device_t dev)
135 struct pst_softc *psc = device_get_softc(dev);
136 struct i2o_get_param_reply *reply;
137 struct i2o_device_identity *ident;
138 int lun = device_get_unit(dev);
141 if (!(reply = iop_get_util_params(psc->iop, psc->lct->local_tid,
142 I2O_PARAMS_OPERATION_FIELD_GET,
143 I2O_BSA_DEVICE_INFO_GROUP_NO)))
146 psc->info = malloc(sizeof(struct i2o_bsa_device), M_PSTRAID, M_INTWAIT);
147 bcopy(reply->result, psc->info, sizeof(struct i2o_bsa_device));
148 contigfree(reply, PAGE_SIZE, M_PSTRAID);
150 if (!(reply = iop_get_util_params(psc->iop, psc->lct->local_tid,
151 I2O_PARAMS_OPERATION_FIELD_GET,
152 I2O_UTIL_DEVICE_IDENTITY_GROUP_NO)))
154 ident = (struct i2o_device_identity *)reply->result;
156 printf("pst: vendor=<%.16s> product=<%.16s>\n",
157 ident->vendor, ident->product);
158 printf("pst: description=<%.16s> revision=<%.8s>\n",
159 ident->description, ident->revision);
160 printf("pst: capacity=%lld blocksize=%d\n",
161 psc->info->capacity, psc->info->block_size);
163 bpack(ident->vendor, ident->vendor, 16);
164 bpack(ident->product, ident->product, 16);
165 sprintf(name, "%s %s", ident->vendor, ident->product);
166 contigfree(reply, PAGE_SIZE, M_PSTRAID);
168 bufq_init(&psc->queue);
170 psc->device = disk_create(lun, &psc->disk, 0, &pst_cdevsw);
171 psc->device->si_drv1 = psc;
172 psc->device->si_iosize_max = 64 * 1024; /*I2O_SGL_MAX_SEGS * PAGE_SIZE;*/
174 bzero(&psc->disk.d_label, sizeof(struct disklabel));
175 psc->disk.d_label.d_secsize = psc->info->block_size;
176 psc->disk.d_label.d_nsectors = 63;
177 psc->disk.d_label.d_ntracks = 255;
178 psc->disk.d_label.d_ncylinders =
179 (psc->info->capacity / psc->info->block_size) / (255 * 63);
180 psc->disk.d_label.d_secpercyl = 255 * 63;
181 psc->disk.d_label.d_secperunit =
182 psc->info->capacity / psc->info->block_size;
184 devstat_add_entry(&psc->stats, "pst", lun, psc->info->block_size,
185 DEVSTAT_NO_ORDERED_TAGS,
186 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_IDE,
187 DEVSTAT_PRIORITY_DISK);
189 printf("pst%d: %lluMB <%.40s> [%d/%d/%d] on %.16s\n", lun,
190 (unsigned long long)psc->disk.d_label.d_secperunit / (1024 * 2),
191 name, psc->disk.d_label.d_ncylinders, 255, 63,
192 device_get_nameunit(psc->iop->dev));
194 EVENTHANDLER_REGISTER(shutdown_post_sync, pst_shutdown,
195 dev, SHUTDOWN_PRI_DEFAULT);
202 pst_shutdown(device_t dev)
204 struct pst_softc *psc = device_get_softc(dev);
205 struct i2o_bsa_cache_flush_message *msg;
208 mfa = iop_get_mfa(psc->iop);
209 msg = (struct i2o_bsa_cache_flush_message *)(psc->iop->ibase + mfa);
210 bzero(msg, sizeof(struct i2o_bsa_cache_flush_message));
211 msg->version_offset = 0x01;
212 msg->message_flags = 0x0;
213 msg->message_size = sizeof(struct i2o_bsa_cache_flush_message) >> 2;
214 msg->target_address = psc->lct->local_tid;
215 msg->initiator_address = I2O_TID_HOST;
216 msg->function = I2O_BSA_CACHE_FLUSH;
217 msg->control_flags = 0x0; /* 0x80 = post progress reports */
218 if (iop_queue_wait_msg(psc->iop, mfa, (struct i2o_basic_message *)msg))
219 printf("pst: shutdown failed!\n");
225 pststrategy(struct buf *bp)
227 struct pst_softc *psc = bp->b_dev->si_drv1;
230 bufqdisksort(&psc->queue, bp);
236 pst_start(struct pst_softc *psc)
238 struct pst_request *request;
242 if (psc->outstanding < (I2O_IOP_OUTBOUND_FRAME_COUNT - 1) &&
243 (bp = bufq_first(&psc->queue))) {
244 if ((mfa = iop_get_mfa(psc->iop)) != 0xffffffff) {
245 request = malloc(sizeof(struct pst_request),
246 M_PSTRAID, M_INTWAIT | M_ZERO);
251 callout_init(&request->timeout);
253 callout_reset(&request->timeout, 10 * hz, pst_timeout, request);
254 bufq_remove(&psc->queue, bp);
255 devstat_start_transaction(&psc->stats);
256 if (pst_rw(request)) {
257 devstat_end_transaction_buf(&psc->stats, request->bp);
258 request->bp->b_error = EIO;
259 request->bp->b_flags |= B_ERROR;
260 biodone(request->bp);
261 iop_free_mfa(request->psc->iop, request->mfa);
263 callout_stop(&request->timeout);
264 free(request, M_PSTRAID);
271 pst_done(struct iop_softc *sc, u_int32_t mfa, struct i2o_single_reply *reply)
273 struct pst_request *request =
274 (struct pst_request *)reply->transaction_context;
275 struct pst_softc *psc = request->psc;
278 callout_stop(&request->timeout);
279 request->bp->b_resid = request->bp->b_bcount - reply->donecount;
280 devstat_end_transaction_buf(&psc->stats, request->bp);
282 request->bp->b_error = EIO;
283 request->bp->b_flags |= B_ERROR;
285 biodone(request->bp);
286 free(request, M_PSTRAID);
288 psc->iop->reg->oqueue = mfa;
295 pst_timeout(void *xrequest)
297 struct pst_request *request = xrequest;
300 printf("pst: timeout mfa=0x%08x cmd=%s\n",
301 request->mfa, request->bp->b_flags & B_READ ? "READ" : "WRITE");
302 iop_free_mfa(request->psc->iop, request->mfa);
303 if ((request->mfa = iop_get_mfa(request->psc->iop)) == 0xffffffff) {
304 printf("pst: timeout no mfa possible\n");
305 devstat_end_transaction_buf(&request->psc->stats, request->bp);
306 request->bp->b_error = EIO;
307 request->bp->b_flags |= B_ERROR;
308 biodone(request->bp);
309 request->psc->outstanding--;
314 callout_reset(&request->timeout, 10 * hz, pst_timeout, request);
315 if (pst_rw(request)) {
316 iop_free_mfa(request->psc->iop, request->mfa);
317 devstat_end_transaction_buf(&request->psc->stats, request->bp);
318 request->bp->b_error = EIO;
319 request->bp->b_flags |= B_ERROR;
320 biodone(request->bp);
321 request->psc->outstanding--;
327 pst_rw(struct pst_request *request)
329 struct i2o_bsa_rw_block_message *msg;
332 msg = (struct i2o_bsa_rw_block_message *)
333 (request->psc->iop->ibase + request->mfa);
334 bzero(msg, sizeof(struct i2o_bsa_rw_block_message));
335 msg->version_offset = 0x81;
336 msg->message_flags = 0x0;
337 msg->message_size = sizeof(struct i2o_bsa_rw_block_message) >> 2;
338 msg->target_address = request->psc->lct->local_tid;
339 msg->initiator_address = I2O_TID_HOST;
340 if (request->bp->b_flags & B_READ) {
341 msg->function = I2O_BSA_BLOCK_READ;
342 msg->control_flags = 0x0; /* 0x0c = read cache + readahead */
343 msg->fetch_ahead = 0x0; /* 8 Kb */
347 msg->function = I2O_BSA_BLOCK_WRITE;
348 msg->control_flags = 0x0; /* 0x10 = write behind cache */
349 msg->fetch_ahead = 0x0;
350 sgl_flag = I2O_SGL_DIR;
352 msg->initiator_context = (u_int32_t)pst_done;
353 msg->transaction_context = (u_int32_t)request;
354 msg->time_multiplier = 1;
355 msg->bytecount = request->bp->b_bcount;
356 msg->lba = ((u_int64_t)request->bp->b_pblkno) * (DEV_BSIZE * 1LL);
357 if (!iop_create_sgl((struct i2o_basic_message *)msg, request->bp->b_data,
358 request->bp->b_bcount, sgl_flag))
360 request->psc->iop->reg->iqueue = request->mfa;
365 bpack(int8_t *src, int8_t *dst, int len)
368 int8_t *ptr, *buf = dst;
370 for (i = j = blank = 0 ; i < len; i++) {
371 if (blank && src[i] == ' ') continue;
372 if (blank && src[i] != ' ') {
386 for (ptr = buf; ptr < buf+len; ++ptr)
389 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
393 static device_method_t pst_methods[] = {
394 DEVMETHOD(device_probe, pst_probe),
395 DEVMETHOD(device_attach, pst_attach),
399 static driver_t pst_driver = {
402 sizeof(struct pst_softc),
405 static devclass_t pst_devclass;
407 DRIVER_MODULE(pst, pstpci, pst_driver, pst_devclass, 0, 0);