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.20 2006/10/25 20:56:01 dillon Exp $
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
40 #include <sys/devicestat.h>
41 #include <sys/eventhandler.h>
42 #include <sys/malloc.h>
46 #include <sys/thread2.h>
51 #include <machine/stdarg.h>
53 #include <bus/pci/pcivar.h>
54 #include <bus/pci/pcireg.h>
58 /* device structures */
59 static d_strategy_t pststrategy;
60 static struct dev_ops pst_ops = {
61 { "pst", 168, D_DISK },
66 .d_strategy = pststrategy,
70 struct iop_softc *iop;
71 struct i2o_lct_entry *lct;
72 struct i2o_bsa_device *info;
76 struct bio_queue_head bio_queue;
81 struct pst_softc *psc; /* pointer to softc */
82 u_int32_t mfa; /* frame addreess */
83 struct callout timeout; /* handle for untimeout */
84 struct bio *bio; /* associated bio ptr */
88 static int pst_probe(device_t);
89 static int pst_attach(device_t);
91 static int pst_shutdown(device_t);
93 static void pst_start(struct pst_softc *);
94 static void pst_done(struct iop_softc *, u_int32_t, struct i2o_single_reply *);
95 static int pst_rw(struct pst_request *);
96 static void pst_timeout(void *);
97 static void bpack(int8_t *, int8_t *, int);
100 static MALLOC_DEFINE(M_PSTRAID, "pst", "Promise SuperTrak RAID driver");
103 pst_add_raid(struct iop_softc *sc, struct i2o_lct_entry *lct)
105 struct pst_softc *psc;
106 device_t child = device_add_child(sc->dev, "pst", -1);
110 psc = kmalloc(sizeof(struct pst_softc), M_PSTRAID, M_INTWAIT | M_ZERO);
113 device_set_softc(child, psc);
114 return bus_generic_attach(sc->dev);
118 pst_probe(device_t dev)
120 device_set_desc(dev, "Promise SuperTrak RAID");
125 pst_attach(device_t dev)
127 struct pst_softc *psc = device_get_softc(dev);
128 struct i2o_get_param_reply *reply;
129 struct i2o_device_identity *ident;
130 int lun = device_get_unit(dev);
133 if (!(reply = iop_get_util_params(psc->iop, psc->lct->local_tid,
134 I2O_PARAMS_OPERATION_FIELD_GET,
135 I2O_BSA_DEVICE_INFO_GROUP_NO)))
138 psc->info = kmalloc(sizeof(struct i2o_bsa_device), M_PSTRAID, M_INTWAIT);
139 bcopy(reply->result, psc->info, sizeof(struct i2o_bsa_device));
140 contigfree(reply, PAGE_SIZE, M_PSTRAID);
142 if (!(reply = iop_get_util_params(psc->iop, psc->lct->local_tid,
143 I2O_PARAMS_OPERATION_FIELD_GET,
144 I2O_UTIL_DEVICE_IDENTITY_GROUP_NO)))
146 ident = (struct i2o_device_identity *)reply->result;
148 printf("pst: vendor=<%.16s> product=<%.16s>\n",
149 ident->vendor, ident->product);
150 printf("pst: description=<%.16s> revision=<%.8s>\n",
151 ident->description, ident->revision);
152 printf("pst: capacity=%lld blocksize=%d\n",
153 psc->info->capacity, psc->info->block_size);
155 bpack(ident->vendor, ident->vendor, 16);
156 bpack(ident->product, ident->product, 16);
157 sprintf(name, "%s %s", ident->vendor, ident->product);
158 contigfree(reply, PAGE_SIZE, M_PSTRAID);
160 bioq_init(&psc->bio_queue);
162 psc->device = disk_create(lun, &psc->disk, 0, &pst_ops);
163 psc->device->si_drv1 = psc;
164 psc->device->si_iosize_max = 64 * 1024; /*I2O_SGL_MAX_SEGS * PAGE_SIZE;*/
166 bzero(&psc->disk.d_label, sizeof(struct disklabel));
167 psc->disk.d_label.d_secsize = psc->info->block_size;
168 psc->disk.d_label.d_nsectors = 63;
169 psc->disk.d_label.d_ntracks = 255;
170 psc->disk.d_label.d_ncylinders =
171 (psc->info->capacity / psc->info->block_size) / (255 * 63);
172 psc->disk.d_label.d_secpercyl = 255 * 63;
173 psc->disk.d_label.d_secperunit =
174 psc->info->capacity / psc->info->block_size;
176 devstat_add_entry(&psc->stats, "pst", lun, psc->info->block_size,
177 DEVSTAT_NO_ORDERED_TAGS,
178 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_IDE,
179 DEVSTAT_PRIORITY_DISK);
181 printf("pst%d: %lluMB <%.40s> [%d/%d/%d] on %.16s\n", lun,
182 (unsigned long long)psc->disk.d_label.d_secperunit / (1024 * 2),
183 name, psc->disk.d_label.d_ncylinders, 255, 63,
184 device_get_nameunit(psc->iop->dev));
186 EVENTHANDLER_REGISTER(shutdown_post_sync, pst_shutdown,
187 dev, SHUTDOWN_PRI_DEFAULT);
194 pst_shutdown(device_t dev)
196 struct pst_softc *psc = device_get_softc(dev);
197 struct i2o_bsa_cache_flush_message *msg;
200 mfa = iop_get_mfa(psc->iop);
201 msg = (struct i2o_bsa_cache_flush_message *)(psc->iop->ibase + mfa);
202 bzero(msg, sizeof(struct i2o_bsa_cache_flush_message));
203 msg->version_offset = 0x01;
204 msg->message_flags = 0x0;
205 msg->message_size = sizeof(struct i2o_bsa_cache_flush_message) >> 2;
206 msg->target_address = psc->lct->local_tid;
207 msg->initiator_address = I2O_TID_HOST;
208 msg->function = I2O_BSA_CACHE_FLUSH;
209 msg->control_flags = 0x0; /* 0x80 = post progress reports */
210 if (iop_queue_wait_msg(psc->iop, mfa, (struct i2o_basic_message *)msg))
211 printf("pst: shutdown failed!\n");
217 pststrategy(struct dev_strategy_args *ap)
219 struct pst_softc *psc = ap->a_head.a_dev->si_drv1;
222 bioqdisksort(&psc->bio_queue, ap->a_bio);
229 pst_start(struct pst_softc *psc)
231 struct pst_request *request;
236 if (psc->outstanding < (I2O_IOP_OUTBOUND_FRAME_COUNT - 1) &&
237 (bio = bioq_first(&psc->bio_queue))) {
238 if ((mfa = iop_get_mfa(psc->iop)) != 0xffffffff) {
239 request = kmalloc(sizeof(struct pst_request),
240 M_PSTRAID, M_INTWAIT | M_ZERO);
245 callout_init(&request->timeout);
247 callout_reset(&request->timeout, 10 * hz, pst_timeout, request);
248 bioq_remove(&psc->bio_queue, bio);
250 devstat_start_transaction(&psc->stats);
251 if (pst_rw(request)) {
252 devstat_end_transaction_buf(&psc->stats, bp);
254 bp->b_flags |= B_ERROR;
256 iop_free_mfa(request->psc->iop, request->mfa);
258 callout_stop(&request->timeout);
259 kfree(request, M_PSTRAID);
266 pst_done(struct iop_softc *sc, u_int32_t mfa, struct i2o_single_reply *reply)
268 struct pst_request *request =
269 (struct pst_request *)reply->transaction_context;
270 struct pst_softc *psc = request->psc;
271 struct buf *bp = request->bio->bio_buf;
273 callout_stop(&request->timeout);
274 bp->b_resid = bp->b_bcount - reply->donecount;
275 devstat_end_transaction_buf(&psc->stats, bp);
278 bp->b_flags |= B_ERROR;
280 biodone(request->bio);
281 kfree(request, M_PSTRAID);
283 psc->iop->reg->oqueue = mfa;
290 pst_timeout(void *xrequest)
292 struct pst_request *request = xrequest;
293 struct buf *bp = request->bio->bio_buf;
296 printf("pst: timeout mfa=0x%08x cmd=%s\n",
297 request->mfa, (bp->b_cmd == BUF_CMD_READ) ? "READ" : "WRITE");
298 iop_free_mfa(request->psc->iop, request->mfa);
299 if ((request->mfa = iop_get_mfa(request->psc->iop)) == 0xffffffff) {
300 printf("pst: timeout no mfa possible\n");
301 devstat_end_transaction_buf(&request->psc->stats, bp);
303 bp->b_flags |= B_ERROR;
304 biodone(request->bio);
305 request->psc->outstanding--;
310 callout_reset(&request->timeout, 10 * hz, pst_timeout, request);
311 if (pst_rw(request)) {
312 iop_free_mfa(request->psc->iop, request->mfa);
313 devstat_end_transaction_buf(&request->psc->stats, bp);
315 bp->b_flags |= B_ERROR;
316 biodone(request->bio);
317 request->psc->outstanding--;
323 pst_rw(struct pst_request *request)
325 struct i2o_bsa_rw_block_message *msg;
327 struct buf *bp = request->bio->bio_buf;
329 msg = (struct i2o_bsa_rw_block_message *)
330 (request->psc->iop->ibase + request->mfa);
331 bzero(msg, sizeof(struct i2o_bsa_rw_block_message));
332 msg->version_offset = 0x81;
333 msg->message_flags = 0x0;
334 msg->message_size = sizeof(struct i2o_bsa_rw_block_message) >> 2;
335 msg->target_address = request->psc->lct->local_tid;
336 msg->initiator_address = I2O_TID_HOST;
337 if (bp->b_cmd == BUF_CMD_READ) {
338 msg->function = I2O_BSA_BLOCK_READ;
339 msg->control_flags = 0x0; /* 0x0c = read cache + readahead */
340 msg->fetch_ahead = 0x0; /* 8 Kb */
344 msg->function = I2O_BSA_BLOCK_WRITE;
345 msg->control_flags = 0x0; /* 0x10 = write behind cache */
346 msg->fetch_ahead = 0x0;
347 sgl_flag = I2O_SGL_DIR;
349 msg->initiator_context = (u_int32_t)pst_done;
350 msg->transaction_context = (u_int32_t)request;
351 msg->time_multiplier = 1;
352 msg->bytecount = bp->b_bcount;
353 msg->lba = request->bio->bio_offset; /* 64 bits */
354 if (!iop_create_sgl((struct i2o_basic_message *)msg, bp->b_data,
355 bp->b_bcount, sgl_flag))
357 request->psc->iop->reg->iqueue = request->mfa;
362 bpack(int8_t *src, int8_t *dst, int len)
365 int8_t *ptr, *buf = dst;
367 for (i = j = blank = 0 ; i < len; i++) {
368 if (blank && src[i] == ' ') continue;
369 if (blank && src[i] != ' ') {
383 for (ptr = buf; ptr < buf+len; ++ptr)
386 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
390 static device_method_t pst_methods[] = {
391 DEVMETHOD(device_probe, pst_probe),
392 DEVMETHOD(device_attach, pst_attach),
396 static driver_t pst_driver = {
399 sizeof(struct pst_softc),
402 static devclass_t pst_devclass;
404 DRIVER_MODULE(pst, pstpci, pst_driver, pst_devclass, 0, 0);