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.2 2003/06/17 04:28:29 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 <machine/stdarg.h>
47 #include <machine/resource.h>
48 #include <machine/bus.h>
50 #include <pci/pcivar.h>
51 #include <pci/pcireg.h>
53 #include "dev/pst/pst-iop.h"
55 /* device structures */
56 static d_strategy_t pststrategy;
57 static struct cdevsw pst_cdevsw = {
59 /* close */ nullclose,
61 /* write */ physwrite,
65 /* strat */ pststrategy,
72 static struct cdevsw pstdisk_cdevsw;
75 struct iop_softc *iop;
76 struct i2o_lct_entry *lct;
77 struct i2o_bsa_device *info;
81 struct buf_queue_head queue;
86 struct pst_softc *psc; /* pointer to softc */
87 u_int32_t mfa; /* frame addreess */
88 struct callout_handle timeout_handle; /* handle for untimeout */
89 struct buf *bp; /* associated bio ptr */
93 static int pst_probe(device_t);
94 static int pst_attach(device_t);
95 static int pst_shutdown(device_t);
96 static void pst_start(struct pst_softc *);
97 static void pst_done(struct iop_softc *, u_int32_t, struct i2o_single_reply *);
98 static int pst_rw(struct pst_request *);
99 static void pst_timeout(struct pst_request *);
100 static void bpack(int8_t *, int8_t *, int);
103 static MALLOC_DEFINE(M_PSTRAID, "pst", "Promise SuperTrak RAID driver");
106 pst_add_raid(struct iop_softc *sc, struct i2o_lct_entry *lct)
108 struct pst_softc *psc;
109 device_t child = device_add_child(sc->dev, "pst", -1);
113 psc = malloc(sizeof(struct pst_softc), M_PSTRAID, M_NOWAIT | M_ZERO);
116 device_set_softc(child, psc);
117 return bus_generic_attach(sc->dev);
121 pst_probe(device_t dev)
123 device_set_desc(dev, "Promise SuperTrak RAID");
128 pst_attach(device_t dev)
130 struct pst_softc *psc = device_get_softc(dev);
131 struct i2o_get_param_reply *reply;
132 struct i2o_device_identity *ident;
133 int lun = device_get_unit(dev);
136 if (!(reply = iop_get_util_params(psc->iop, psc->lct->local_tid,
137 I2O_PARAMS_OPERATION_FIELD_GET,
138 I2O_BSA_DEVICE_INFO_GROUP_NO)))
141 if (!(psc->info = (struct i2o_bsa_device *)
142 malloc(sizeof(struct i2o_bsa_device), M_PSTRAID, M_NOWAIT))) {
143 contigfree(reply, PAGE_SIZE, M_PSTRAID);
146 bcopy(reply->result, psc->info, sizeof(struct i2o_bsa_device));
147 contigfree(reply, PAGE_SIZE, M_PSTRAID);
149 if (!(reply = iop_get_util_params(psc->iop, psc->lct->local_tid,
150 I2O_PARAMS_OPERATION_FIELD_GET,
151 I2O_UTIL_DEVICE_IDENTITY_GROUP_NO)))
153 ident = (struct i2o_device_identity *)reply->result;
155 printf("pst: vendor=<%.16s> product=<%.16s>\n",
156 ident->vendor, ident->product);
157 printf("pst: description=<%.16s> revision=<%.8s>\n",
158 ident->description, ident->revision);
159 printf("pst: capacity=%lld blocksize=%d\n",
160 psc->info->capacity, psc->info->block_size);
162 bpack(ident->vendor, ident->vendor, 16);
163 bpack(ident->product, ident->product, 16);
164 sprintf(name, "%s %s", ident->vendor, ident->product);
165 contigfree(reply, PAGE_SIZE, M_PSTRAID);
167 bufq_init(&psc->queue);
169 psc->device = disk_create(lun, &psc->disk, 0, &pst_cdevsw, &pstdisk_cdevsw);
170 psc->device->si_drv1 = psc;
171 psc->device->si_iosize_max = 64 * 1024; /*I2O_SGL_MAX_SEGS * PAGE_SIZE;*/
173 bzero(&psc->disk.d_label, sizeof(struct disklabel));
174 psc->disk.d_label.d_secsize = psc->info->block_size;
175 psc->disk.d_label.d_nsectors = 63;
176 psc->disk.d_label.d_ntracks = 255;
177 psc->disk.d_label.d_ncylinders =
178 (psc->info->capacity / psc->info->block_size) / (255 * 63);
179 psc->disk.d_label.d_secpercyl = 255 * 63;
180 psc->disk.d_label.d_secperunit =
181 psc->info->capacity / psc->info->block_size;
183 devstat_add_entry(&psc->stats, "pst", lun, psc->info->block_size,
184 DEVSTAT_NO_ORDERED_TAGS,
185 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_IDE,
186 DEVSTAT_PRIORITY_DISK);
188 printf("pst%d: %lluMB <%.40s> [%d/%d/%d] on %.16s\n", lun,
189 (unsigned long long)psc->disk.d_label.d_secperunit / (1024 * 2),
190 name, psc->disk.d_label.d_ncylinders, 255, 63,
191 device_get_nameunit(psc->iop->dev));
193 EVENTHANDLER_REGISTER(shutdown_post_sync, pst_shutdown,
194 dev, SHUTDOWN_PRI_DEFAULT);
200 pst_shutdown(device_t dev)
202 struct pst_softc *psc = device_get_softc(dev);
203 struct i2o_bsa_cache_flush_message *msg;
206 mfa = iop_get_mfa(psc->iop);
207 msg = (struct i2o_bsa_cache_flush_message *)(psc->iop->ibase + mfa);
208 bzero(msg, sizeof(struct i2o_bsa_cache_flush_message));
209 msg->version_offset = 0x01;
210 msg->message_flags = 0x0;
211 msg->message_size = sizeof(struct i2o_bsa_cache_flush_message) >> 2;
212 msg->target_address = psc->lct->local_tid;
213 msg->initiator_address = I2O_TID_HOST;
214 msg->function = I2O_BSA_CACHE_FLUSH;
215 msg->control_flags = 0x0; /* 0x80 = post progress reports */
216 if (iop_queue_wait_msg(psc->iop, mfa, (struct i2o_basic_message *)msg))
217 printf("pst: shutdown failed!\n");
222 pststrategy(struct buf *bp)
224 struct pst_softc *psc = bp->b_dev->si_drv1;
227 bufqdisksort(&psc->queue, bp);
233 pst_start(struct pst_softc *psc)
235 struct pst_request *request;
239 if (psc->outstanding < (I2O_IOP_OUTBOUND_FRAME_COUNT - 1) &&
240 (bp = bufq_first(&psc->queue))) {
241 if ((mfa = iop_get_mfa(psc->iop)) != 0xffffffff) {
242 if (!(request = malloc(sizeof(struct pst_request),
243 M_PSTRAID, M_NOWAIT | M_ZERO))) {
244 printf("pst: out of memory in start\n");
245 iop_free_mfa(psc->iop, mfa);
253 request->timeout_handle.callout = NULL;
255 request->timeout_handle =
256 timeout((timeout_t*)pst_timeout, request, 10 * hz);
257 bufq_remove(&psc->queue, bp);
258 devstat_start_transaction(&psc->stats);
259 if (pst_rw(request)) {
260 devstat_end_transaction_buf(&psc->stats, request->bp);
261 request->bp->b_error = EIO;
262 request->bp->b_flags |= B_ERROR;
263 biodone(request->bp);
264 iop_free_mfa(request->psc->iop, request->mfa);
266 free(request, M_PSTRAID);
273 pst_done(struct iop_softc *sc, u_int32_t mfa, struct i2o_single_reply *reply)
275 struct pst_request *request =
276 (struct pst_request *)reply->transaction_context;
277 struct pst_softc *psc = request->psc;
280 untimeout((timeout_t *)pst_timeout, request, request->timeout_handle);
281 request->bp->b_resid = request->bp->b_bcount - reply->donecount;
282 devstat_end_transaction_buf(&psc->stats, request->bp);
284 request->bp->b_error = EIO;
285 request->bp->b_flags |= B_ERROR;
287 biodone(request->bp);
288 free(request, M_PSTRAID);
290 psc->iop->reg->oqueue = mfa;
297 pst_timeout(struct pst_request *request)
301 printf("pst: timeout mfa=0x%08x cmd=%s\n",
302 request->mfa, request->bp->b_flags & B_READ ? "READ" : "WRITE");
303 iop_free_mfa(request->psc->iop, request->mfa);
304 if ((request->mfa = iop_get_mfa(request->psc->iop)) == 0xffffffff) {
305 printf("pst: timeout no mfa possible\n");
306 devstat_end_transaction_buf(&request->psc->stats, request->bp);
307 request->bp->b_error = EIO;
308 request->bp->b_flags |= B_ERROR;
309 biodone(request->bp);
310 request->psc->outstanding--;
315 request->timeout_handle.callout = NULL;
317 request->timeout_handle =
318 timeout((timeout_t*)pst_timeout, request, 10 * hz);
319 if (pst_rw(request)) {
320 iop_free_mfa(request->psc->iop, request->mfa);
321 devstat_end_transaction_buf(&request->psc->stats, request->bp);
322 request->bp->b_error = EIO;
323 request->bp->b_flags |= B_ERROR;
324 biodone(request->bp);
325 request->psc->outstanding--;
331 pst_rw(struct pst_request *request)
333 struct i2o_bsa_rw_block_message *msg;
336 msg = (struct i2o_bsa_rw_block_message *)
337 (request->psc->iop->ibase + request->mfa);
338 bzero(msg, sizeof(struct i2o_bsa_rw_block_message));
339 msg->version_offset = 0x81;
340 msg->message_flags = 0x0;
341 msg->message_size = sizeof(struct i2o_bsa_rw_block_message) >> 2;
342 msg->target_address = request->psc->lct->local_tid;
343 msg->initiator_address = I2O_TID_HOST;
344 if (request->bp->b_flags & B_READ) {
345 msg->function = I2O_BSA_BLOCK_READ;
346 msg->control_flags = 0x0; /* 0x0c = read cache + readahead */
347 msg->fetch_ahead = 0x0; /* 8 Kb */
351 msg->function = I2O_BSA_BLOCK_WRITE;
352 msg->control_flags = 0x0; /* 0x10 = write behind cache */
353 msg->fetch_ahead = 0x0;
354 sgl_flag = I2O_SGL_DIR;
356 msg->initiator_context = (u_int32_t)pst_done;
357 msg->transaction_context = (u_int32_t)request;
358 msg->time_multiplier = 1;
359 msg->bytecount = request->bp->b_bcount;
360 msg->lba = ((u_int64_t)request->bp->b_pblkno) * (DEV_BSIZE * 1LL);
361 if (!iop_create_sgl((struct i2o_basic_message *)msg, request->bp->b_data,
362 request->bp->b_bcount, sgl_flag))
364 request->psc->iop->reg->iqueue = request->mfa;
369 bpack(int8_t *src, int8_t *dst, int len)
372 int8_t *ptr, *buf = dst;
374 for (i = j = blank = 0 ; i < len; i++) {
375 if (blank && src[i] == ' ') continue;
376 if (blank && src[i] != ' ') {
390 for (ptr = buf; ptr < buf+len; ++ptr)
393 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
397 static device_method_t pst_methods[] = {
398 DEVMETHOD(device_probe, pst_probe),
399 DEVMETHOD(device_attach, pst_attach),
403 static driver_t pst_driver = {
406 sizeof(struct pst_softc),
409 static devclass_t pst_devclass;
411 DRIVER_MODULE(pst, pstpci, pst_driver, pst_devclass, 0, 0);