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.15 2006/04/30 17:22:16 dillon 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>
45 #include <sys/thread2.h>
48 #include <machine/stdarg.h>
49 #include <machine/resource.h>
50 #include <machine/bus.h>
52 #include <bus/pci/pcivar.h>
53 #include <bus/pci/pcireg.h>
57 /* device structures */
58 static d_strategy_t pststrategy;
59 static struct cdevsw pst_cdevsw = {
67 /* close */ nullclose,
69 /* write */ physwrite,
73 /* strat */ pststrategy,
79 struct iop_softc *iop;
80 struct i2o_lct_entry *lct;
81 struct i2o_bsa_device *info;
85 struct bio_queue_head bio_queue;
90 struct pst_softc *psc; /* pointer to softc */
91 u_int32_t mfa; /* frame addreess */
92 struct callout timeout; /* handle for untimeout */
93 struct bio *bio; /* associated bio ptr */
97 static int pst_probe(device_t);
98 static int pst_attach(device_t);
100 static int pst_shutdown(device_t);
102 static void pst_start(struct pst_softc *);
103 static void pst_done(struct iop_softc *, u_int32_t, struct i2o_single_reply *);
104 static int pst_rw(struct pst_request *);
105 static void pst_timeout(void *);
106 static void bpack(int8_t *, int8_t *, int);
109 static MALLOC_DEFINE(M_PSTRAID, "pst", "Promise SuperTrak RAID driver");
112 pst_add_raid(struct iop_softc *sc, struct i2o_lct_entry *lct)
114 struct pst_softc *psc;
115 device_t child = device_add_child(sc->dev, "pst", -1);
119 psc = malloc(sizeof(struct pst_softc), M_PSTRAID, M_INTWAIT | M_ZERO);
122 device_set_softc(child, psc);
123 return bus_generic_attach(sc->dev);
127 pst_probe(device_t dev)
129 device_set_desc(dev, "Promise SuperTrak RAID");
134 pst_attach(device_t dev)
136 struct pst_softc *psc = device_get_softc(dev);
137 struct i2o_get_param_reply *reply;
138 struct i2o_device_identity *ident;
139 int lun = device_get_unit(dev);
142 if (!(reply = iop_get_util_params(psc->iop, psc->lct->local_tid,
143 I2O_PARAMS_OPERATION_FIELD_GET,
144 I2O_BSA_DEVICE_INFO_GROUP_NO)))
147 psc->info = malloc(sizeof(struct i2o_bsa_device), M_PSTRAID, M_INTWAIT);
148 bcopy(reply->result, psc->info, sizeof(struct i2o_bsa_device));
149 contigfree(reply, PAGE_SIZE, M_PSTRAID);
151 if (!(reply = iop_get_util_params(psc->iop, psc->lct->local_tid,
152 I2O_PARAMS_OPERATION_FIELD_GET,
153 I2O_UTIL_DEVICE_IDENTITY_GROUP_NO)))
155 ident = (struct i2o_device_identity *)reply->result;
157 printf("pst: vendor=<%.16s> product=<%.16s>\n",
158 ident->vendor, ident->product);
159 printf("pst: description=<%.16s> revision=<%.8s>\n",
160 ident->description, ident->revision);
161 printf("pst: capacity=%lld blocksize=%d\n",
162 psc->info->capacity, psc->info->block_size);
164 bpack(ident->vendor, ident->vendor, 16);
165 bpack(ident->product, ident->product, 16);
166 sprintf(name, "%s %s", ident->vendor, ident->product);
167 contigfree(reply, PAGE_SIZE, M_PSTRAID);
169 bioq_init(&psc->bio_queue);
171 psc->device = disk_create(lun, &psc->disk, 0, &pst_cdevsw);
172 psc->device->si_drv1 = psc;
173 psc->device->si_iosize_max = 64 * 1024; /*I2O_SGL_MAX_SEGS * PAGE_SIZE;*/
175 bzero(&psc->disk.d_label, sizeof(struct disklabel));
176 psc->disk.d_label.d_secsize = psc->info->block_size;
177 psc->disk.d_label.d_nsectors = 63;
178 psc->disk.d_label.d_ntracks = 255;
179 psc->disk.d_label.d_ncylinders =
180 (psc->info->capacity / psc->info->block_size) / (255 * 63);
181 psc->disk.d_label.d_secpercyl = 255 * 63;
182 psc->disk.d_label.d_secperunit =
183 psc->info->capacity / psc->info->block_size;
185 devstat_add_entry(&psc->stats, "pst", lun, psc->info->block_size,
186 DEVSTAT_NO_ORDERED_TAGS,
187 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_IDE,
188 DEVSTAT_PRIORITY_DISK);
190 printf("pst%d: %lluMB <%.40s> [%d/%d/%d] on %.16s\n", lun,
191 (unsigned long long)psc->disk.d_label.d_secperunit / (1024 * 2),
192 name, psc->disk.d_label.d_ncylinders, 255, 63,
193 device_get_nameunit(psc->iop->dev));
195 EVENTHANDLER_REGISTER(shutdown_post_sync, pst_shutdown,
196 dev, SHUTDOWN_PRI_DEFAULT);
203 pst_shutdown(device_t dev)
205 struct pst_softc *psc = device_get_softc(dev);
206 struct i2o_bsa_cache_flush_message *msg;
209 mfa = iop_get_mfa(psc->iop);
210 msg = (struct i2o_bsa_cache_flush_message *)(psc->iop->ibase + mfa);
211 bzero(msg, sizeof(struct i2o_bsa_cache_flush_message));
212 msg->version_offset = 0x01;
213 msg->message_flags = 0x0;
214 msg->message_size = sizeof(struct i2o_bsa_cache_flush_message) >> 2;
215 msg->target_address = psc->lct->local_tid;
216 msg->initiator_address = I2O_TID_HOST;
217 msg->function = I2O_BSA_CACHE_FLUSH;
218 msg->control_flags = 0x0; /* 0x80 = post progress reports */
219 if (iop_queue_wait_msg(psc->iop, mfa, (struct i2o_basic_message *)msg))
220 printf("pst: shutdown failed!\n");
226 pststrategy(dev_t dev, struct bio *bio)
228 struct pst_softc *psc = dev->si_drv1;
231 bioqdisksort(&psc->bio_queue, bio);
237 pst_start(struct pst_softc *psc)
239 struct pst_request *request;
244 if (psc->outstanding < (I2O_IOP_OUTBOUND_FRAME_COUNT - 1) &&
245 (bio = bioq_first(&psc->bio_queue))) {
246 if ((mfa = iop_get_mfa(psc->iop)) != 0xffffffff) {
247 request = malloc(sizeof(struct pst_request),
248 M_PSTRAID, M_INTWAIT | M_ZERO);
253 callout_init(&request->timeout);
255 callout_reset(&request->timeout, 10 * hz, pst_timeout, request);
256 bioq_remove(&psc->bio_queue, bio);
258 devstat_start_transaction(&psc->stats);
259 if (pst_rw(request)) {
260 devstat_end_transaction_buf(&psc->stats, bp);
262 bp->b_flags |= B_ERROR;
264 iop_free_mfa(request->psc->iop, request->mfa);
266 callout_stop(&request->timeout);
267 free(request, M_PSTRAID);
274 pst_done(struct iop_softc *sc, u_int32_t mfa, struct i2o_single_reply *reply)
276 struct pst_request *request =
277 (struct pst_request *)reply->transaction_context;
278 struct pst_softc *psc = request->psc;
279 struct buf *bp = request->bio->bio_buf;
281 callout_stop(&request->timeout);
282 bp->b_resid = bp->b_bcount - reply->donecount;
283 devstat_end_transaction_buf(&psc->stats, bp);
286 bp->b_flags |= B_ERROR;
288 biodone(request->bio);
289 free(request, M_PSTRAID);
291 psc->iop->reg->oqueue = mfa;
298 pst_timeout(void *xrequest)
300 struct pst_request *request = xrequest;
301 struct buf *bp = request->bio->bio_buf;
304 printf("pst: timeout mfa=0x%08x cmd=%s\n",
305 request->mfa, (bp->b_cmd == BUF_CMD_READ) ? "READ" : "WRITE");
306 iop_free_mfa(request->psc->iop, request->mfa);
307 if ((request->mfa = iop_get_mfa(request->psc->iop)) == 0xffffffff) {
308 printf("pst: timeout no mfa possible\n");
309 devstat_end_transaction_buf(&request->psc->stats, bp);
311 bp->b_flags |= B_ERROR;
312 biodone(request->bio);
313 request->psc->outstanding--;
318 callout_reset(&request->timeout, 10 * hz, pst_timeout, request);
319 if (pst_rw(request)) {
320 iop_free_mfa(request->psc->iop, request->mfa);
321 devstat_end_transaction_buf(&request->psc->stats, bp);
323 bp->b_flags |= B_ERROR;
324 biodone(request->bio);
325 request->psc->outstanding--;
331 pst_rw(struct pst_request *request)
333 struct i2o_bsa_rw_block_message *msg;
335 struct buf *bp = request->bio->bio_buf;
337 msg = (struct i2o_bsa_rw_block_message *)
338 (request->psc->iop->ibase + request->mfa);
339 bzero(msg, sizeof(struct i2o_bsa_rw_block_message));
340 msg->version_offset = 0x81;
341 msg->message_flags = 0x0;
342 msg->message_size = sizeof(struct i2o_bsa_rw_block_message) >> 2;
343 msg->target_address = request->psc->lct->local_tid;
344 msg->initiator_address = I2O_TID_HOST;
345 if (bp->b_cmd == BUF_CMD_READ) {
346 msg->function = I2O_BSA_BLOCK_READ;
347 msg->control_flags = 0x0; /* 0x0c = read cache + readahead */
348 msg->fetch_ahead = 0x0; /* 8 Kb */
352 msg->function = I2O_BSA_BLOCK_WRITE;
353 msg->control_flags = 0x0; /* 0x10 = write behind cache */
354 msg->fetch_ahead = 0x0;
355 sgl_flag = I2O_SGL_DIR;
357 msg->initiator_context = (u_int32_t)pst_done;
358 msg->transaction_context = (u_int32_t)request;
359 msg->time_multiplier = 1;
360 msg->bytecount = bp->b_bcount;
361 msg->lba = request->bio->bio_offset; /* 64 bits */
362 if (!iop_create_sgl((struct i2o_basic_message *)msg, bp->b_data,
363 bp->b_bcount, sgl_flag))
365 request->psc->iop->reg->iqueue = request->mfa;
370 bpack(int8_t *src, int8_t *dst, int len)
373 int8_t *ptr, *buf = dst;
375 for (i = j = blank = 0 ; i < len; i++) {
376 if (blank && src[i] == ' ') continue;
377 if (blank && src[i] != ' ') {
391 for (ptr = buf; ptr < buf+len; ++ptr)
394 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
398 static device_method_t pst_methods[] = {
399 DEVMETHOD(device_probe, pst_probe),
400 DEVMETHOD(device_attach, pst_attach),
404 static driver_t pst_driver = {
407 sizeof(struct pst_softc),
410 static devclass_t pst_devclass;
412 DRIVER_MODULE(pst, pstpci, pst_driver, pst_devclass, 0, 0);