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.17 2006/09/05 00:55:42 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 dev_ops pst_ops = {
60 { "pst", 168, D_DISK },
65 .d_strategy = pststrategy,
69 struct iop_softc *iop;
70 struct i2o_lct_entry *lct;
71 struct i2o_bsa_device *info;
75 struct bio_queue_head bio_queue;
80 struct pst_softc *psc; /* pointer to softc */
81 u_int32_t mfa; /* frame addreess */
82 struct callout timeout; /* handle for untimeout */
83 struct bio *bio; /* associated bio ptr */
87 static int pst_probe(device_t);
88 static int pst_attach(device_t);
90 static int pst_shutdown(device_t);
92 static void pst_start(struct pst_softc *);
93 static void pst_done(struct iop_softc *, u_int32_t, struct i2o_single_reply *);
94 static int pst_rw(struct pst_request *);
95 static void pst_timeout(void *);
96 static void bpack(int8_t *, int8_t *, int);
99 static MALLOC_DEFINE(M_PSTRAID, "pst", "Promise SuperTrak RAID driver");
102 pst_add_raid(struct iop_softc *sc, struct i2o_lct_entry *lct)
104 struct pst_softc *psc;
105 device_t child = device_add_child(sc->dev, "pst", -1);
109 psc = kmalloc(sizeof(struct pst_softc), M_PSTRAID, M_INTWAIT | M_ZERO);
112 device_set_softc(child, psc);
113 return bus_generic_attach(sc->dev);
117 pst_probe(device_t dev)
119 device_set_desc(dev, "Promise SuperTrak RAID");
124 pst_attach(device_t dev)
126 struct pst_softc *psc = device_get_softc(dev);
127 struct i2o_get_param_reply *reply;
128 struct i2o_device_identity *ident;
129 int lun = device_get_unit(dev);
132 if (!(reply = iop_get_util_params(psc->iop, psc->lct->local_tid,
133 I2O_PARAMS_OPERATION_FIELD_GET,
134 I2O_BSA_DEVICE_INFO_GROUP_NO)))
137 psc->info = kmalloc(sizeof(struct i2o_bsa_device), M_PSTRAID, M_INTWAIT);
138 bcopy(reply->result, psc->info, sizeof(struct i2o_bsa_device));
139 contigfree(reply, PAGE_SIZE, M_PSTRAID);
141 if (!(reply = iop_get_util_params(psc->iop, psc->lct->local_tid,
142 I2O_PARAMS_OPERATION_FIELD_GET,
143 I2O_UTIL_DEVICE_IDENTITY_GROUP_NO)))
145 ident = (struct i2o_device_identity *)reply->result;
147 printf("pst: vendor=<%.16s> product=<%.16s>\n",
148 ident->vendor, ident->product);
149 printf("pst: description=<%.16s> revision=<%.8s>\n",
150 ident->description, ident->revision);
151 printf("pst: capacity=%lld blocksize=%d\n",
152 psc->info->capacity, psc->info->block_size);
154 bpack(ident->vendor, ident->vendor, 16);
155 bpack(ident->product, ident->product, 16);
156 sprintf(name, "%s %s", ident->vendor, ident->product);
157 contigfree(reply, PAGE_SIZE, M_PSTRAID);
159 bioq_init(&psc->bio_queue);
161 psc->device = disk_create(lun, &psc->disk, 0, &pst_ops);
162 psc->device->si_drv1 = psc;
163 psc->device->si_iosize_max = 64 * 1024; /*I2O_SGL_MAX_SEGS * PAGE_SIZE;*/
165 bzero(&psc->disk.d_label, sizeof(struct disklabel));
166 psc->disk.d_label.d_secsize = psc->info->block_size;
167 psc->disk.d_label.d_nsectors = 63;
168 psc->disk.d_label.d_ntracks = 255;
169 psc->disk.d_label.d_ncylinders =
170 (psc->info->capacity / psc->info->block_size) / (255 * 63);
171 psc->disk.d_label.d_secpercyl = 255 * 63;
172 psc->disk.d_label.d_secperunit =
173 psc->info->capacity / psc->info->block_size;
175 devstat_add_entry(&psc->stats, "pst", lun, psc->info->block_size,
176 DEVSTAT_NO_ORDERED_TAGS,
177 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_IDE,
178 DEVSTAT_PRIORITY_DISK);
180 printf("pst%d: %lluMB <%.40s> [%d/%d/%d] on %.16s\n", lun,
181 (unsigned long long)psc->disk.d_label.d_secperunit / (1024 * 2),
182 name, psc->disk.d_label.d_ncylinders, 255, 63,
183 device_get_nameunit(psc->iop->dev));
185 EVENTHANDLER_REGISTER(shutdown_post_sync, pst_shutdown,
186 dev, SHUTDOWN_PRI_DEFAULT);
193 pst_shutdown(device_t dev)
195 struct pst_softc *psc = device_get_softc(dev);
196 struct i2o_bsa_cache_flush_message *msg;
199 mfa = iop_get_mfa(psc->iop);
200 msg = (struct i2o_bsa_cache_flush_message *)(psc->iop->ibase + mfa);
201 bzero(msg, sizeof(struct i2o_bsa_cache_flush_message));
202 msg->version_offset = 0x01;
203 msg->message_flags = 0x0;
204 msg->message_size = sizeof(struct i2o_bsa_cache_flush_message) >> 2;
205 msg->target_address = psc->lct->local_tid;
206 msg->initiator_address = I2O_TID_HOST;
207 msg->function = I2O_BSA_CACHE_FLUSH;
208 msg->control_flags = 0x0; /* 0x80 = post progress reports */
209 if (iop_queue_wait_msg(psc->iop, mfa, (struct i2o_basic_message *)msg))
210 printf("pst: shutdown failed!\n");
216 pststrategy(struct dev_strategy_args *ap)
218 struct pst_softc *psc = ap->a_head.a_dev->si_drv1;
221 bioqdisksort(&psc->bio_queue, ap->a_bio);
228 pst_start(struct pst_softc *psc)
230 struct pst_request *request;
235 if (psc->outstanding < (I2O_IOP_OUTBOUND_FRAME_COUNT - 1) &&
236 (bio = bioq_first(&psc->bio_queue))) {
237 if ((mfa = iop_get_mfa(psc->iop)) != 0xffffffff) {
238 request = malloc(sizeof(struct pst_request),
239 M_PSTRAID, M_INTWAIT | M_ZERO);
244 callout_init(&request->timeout);
246 callout_reset(&request->timeout, 10 * hz, pst_timeout, request);
247 bioq_remove(&psc->bio_queue, bio);
249 devstat_start_transaction(&psc->stats);
250 if (pst_rw(request)) {
251 devstat_end_transaction_buf(&psc->stats, bp);
253 bp->b_flags |= B_ERROR;
255 iop_free_mfa(request->psc->iop, request->mfa);
257 callout_stop(&request->timeout);
258 kfree(request, M_PSTRAID);
265 pst_done(struct iop_softc *sc, u_int32_t mfa, struct i2o_single_reply *reply)
267 struct pst_request *request =
268 (struct pst_request *)reply->transaction_context;
269 struct pst_softc *psc = request->psc;
270 struct buf *bp = request->bio->bio_buf;
272 callout_stop(&request->timeout);
273 bp->b_resid = bp->b_bcount - reply->donecount;
274 devstat_end_transaction_buf(&psc->stats, bp);
277 bp->b_flags |= B_ERROR;
279 biodone(request->bio);
280 kfree(request, M_PSTRAID);
282 psc->iop->reg->oqueue = mfa;
289 pst_timeout(void *xrequest)
291 struct pst_request *request = xrequest;
292 struct buf *bp = request->bio->bio_buf;
295 printf("pst: timeout mfa=0x%08x cmd=%s\n",
296 request->mfa, (bp->b_cmd == BUF_CMD_READ) ? "READ" : "WRITE");
297 iop_free_mfa(request->psc->iop, request->mfa);
298 if ((request->mfa = iop_get_mfa(request->psc->iop)) == 0xffffffff) {
299 printf("pst: timeout no mfa possible\n");
300 devstat_end_transaction_buf(&request->psc->stats, bp);
302 bp->b_flags |= B_ERROR;
303 biodone(request->bio);
304 request->psc->outstanding--;
309 callout_reset(&request->timeout, 10 * hz, pst_timeout, request);
310 if (pst_rw(request)) {
311 iop_free_mfa(request->psc->iop, request->mfa);
312 devstat_end_transaction_buf(&request->psc->stats, bp);
314 bp->b_flags |= B_ERROR;
315 biodone(request->bio);
316 request->psc->outstanding--;
322 pst_rw(struct pst_request *request)
324 struct i2o_bsa_rw_block_message *msg;
326 struct buf *bp = request->bio->bio_buf;
328 msg = (struct i2o_bsa_rw_block_message *)
329 (request->psc->iop->ibase + request->mfa);
330 bzero(msg, sizeof(struct i2o_bsa_rw_block_message));
331 msg->version_offset = 0x81;
332 msg->message_flags = 0x0;
333 msg->message_size = sizeof(struct i2o_bsa_rw_block_message) >> 2;
334 msg->target_address = request->psc->lct->local_tid;
335 msg->initiator_address = I2O_TID_HOST;
336 if (bp->b_cmd == BUF_CMD_READ) {
337 msg->function = I2O_BSA_BLOCK_READ;
338 msg->control_flags = 0x0; /* 0x0c = read cache + readahead */
339 msg->fetch_ahead = 0x0; /* 8 Kb */
343 msg->function = I2O_BSA_BLOCK_WRITE;
344 msg->control_flags = 0x0; /* 0x10 = write behind cache */
345 msg->fetch_ahead = 0x0;
346 sgl_flag = I2O_SGL_DIR;
348 msg->initiator_context = (u_int32_t)pst_done;
349 msg->transaction_context = (u_int32_t)request;
350 msg->time_multiplier = 1;
351 msg->bytecount = bp->b_bcount;
352 msg->lba = request->bio->bio_offset; /* 64 bits */
353 if (!iop_create_sgl((struct i2o_basic_message *)msg, bp->b_data,
354 bp->b_bcount, sgl_flag))
356 request->psc->iop->reg->iqueue = request->mfa;
361 bpack(int8_t *src, int8_t *dst, int len)
364 int8_t *ptr, *buf = dst;
366 for (i = j = blank = 0 ; i < len; i++) {
367 if (blank && src[i] == ' ') continue;
368 if (blank && src[i] != ' ') {
382 for (ptr = buf; ptr < buf+len; ++ptr)
385 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
389 static device_method_t pst_methods[] = {
390 DEVMETHOD(device_probe, pst_probe),
391 DEVMETHOD(device_attach, pst_attach),
395 static driver_t pst_driver = {
398 sizeof(struct pst_softc),
401 static devclass_t pst_devclass;
403 DRIVER_MODULE(pst, pstpci, pst_driver, pst_devclass, 0, 0);