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 $
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
39 #include <sys/devicestat.h>
40 #include <sys/eventhandler.h>
41 #include <sys/malloc.h>
45 #include <sys/thread2.h>
50 #include <machine/stdarg.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 address */
82 struct callout timeout; /* callout handle */
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 struct disk_info info;
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 kprintf("pst: vendor=<%.16s> product=<%.16s>\n",
149 ident->vendor, ident->product);
150 kprintf("pst: description=<%.16s> revision=<%.8s>\n",
151 ident->description, ident->revision);
152 kprintf("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 ksprintf(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, &pst_ops);
163 psc->device->si_drv1 = psc;
164 psc->device->si_iosize_max = 64 * 1024; /*I2O_SGL_MAX_SEGS * PAGE_SIZE;*/
166 bzero(&info, sizeof(info));
167 info.d_media_blksize = psc->info->block_size; /* mandatory */
168 info.d_media_size = psc->info->capacity; /* (in bytes) */
170 info.d_secpertrack = 63; /* optional */
172 info.d_secpercyl = info.d_nheads * info.d_secpertrack;
174 (psc->info->capacity / psc->info->block_size) / info.d_secpercyl;
176 disk_setdiskinfo(&psc->disk, &info);
178 devstat_add_entry(&psc->stats, "pst", lun, psc->info->block_size,
179 DEVSTAT_NO_ORDERED_TAGS,
180 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_IDE,
181 DEVSTAT_PRIORITY_DISK);
183 kprintf("pst%d: %juMB <%.40s> [%d/%d/%d] on %.16s\n", lun,
184 (uintmax_t)(info.d_media_size / (1024 * 1024)), name,
185 info.d_ncylinders , info.d_nheads, info.d_secpertrack,
186 device_get_nameunit(psc->iop->dev));
188 EVENTHANDLER_REGISTER(shutdown_post_sync, pst_shutdown,
189 dev, SHUTDOWN_PRI_DRIVER);
196 pst_shutdown(device_t dev)
198 struct pst_softc *psc = device_get_softc(dev);
199 struct i2o_bsa_cache_flush_message *msg;
202 mfa = iop_get_mfa(psc->iop);
203 msg = (struct i2o_bsa_cache_flush_message *)(psc->iop->ibase + mfa);
204 bzero(msg, sizeof(struct i2o_bsa_cache_flush_message));
205 msg->version_offset = 0x01;
206 msg->message_flags = 0x0;
207 msg->message_size = sizeof(struct i2o_bsa_cache_flush_message) >> 2;
208 msg->target_address = psc->lct->local_tid;
209 msg->initiator_address = I2O_TID_HOST;
210 msg->function = I2O_BSA_CACHE_FLUSH;
211 msg->control_flags = 0x0; /* 0x80 = post progress reports */
212 if (iop_queue_wait_msg(psc->iop, mfa, (struct i2o_basic_message *)msg))
213 kprintf("pst: shutdown failed!\n");
219 pststrategy(struct dev_strategy_args *ap)
221 struct pst_softc *psc = ap->a_head.a_dev->si_drv1;
224 bioqdisksort(&psc->bio_queue, ap->a_bio);
231 pst_start(struct pst_softc *psc)
233 struct pst_request *request;
238 if (psc->outstanding < (I2O_IOP_OUTBOUND_FRAME_COUNT - 1) &&
239 (bio = bioq_first(&psc->bio_queue))) {
240 if ((mfa = iop_get_mfa(psc->iop)) != 0xffffffff) {
241 request = kmalloc(sizeof(struct pst_request),
242 M_PSTRAID, M_INTWAIT | M_ZERO);
247 callout_init(&request->timeout);
249 callout_reset(&request->timeout, 10 * hz, pst_timeout, request);
250 bioq_remove(&psc->bio_queue, bio);
252 devstat_start_transaction(&psc->stats);
253 if (pst_rw(request)) {
254 devstat_end_transaction_buf(&psc->stats, bp);
256 bp->b_flags |= B_ERROR;
258 iop_free_mfa(request->psc->iop, request->mfa);
260 callout_stop(&request->timeout);
261 kfree(request, M_PSTRAID);
268 pst_done(struct iop_softc *sc, u_int32_t mfa, struct i2o_single_reply *reply)
270 struct pst_request *request =
271 (struct pst_request *)reply->transaction_context;
272 struct pst_softc *psc = request->psc;
273 struct buf *bp = request->bio->bio_buf;
275 callout_stop(&request->timeout);
276 bp->b_resid = bp->b_bcount - reply->donecount;
277 devstat_end_transaction_buf(&psc->stats, bp);
280 bp->b_flags |= B_ERROR;
282 biodone(request->bio);
283 kfree(request, M_PSTRAID);
285 psc->iop->reg->oqueue = mfa;
292 pst_timeout(void *xrequest)
294 struct pst_request *request = xrequest;
295 struct buf *bp = request->bio->bio_buf;
298 kprintf("pst: timeout mfa=0x%08x cmd=%s\n",
299 request->mfa, (bp->b_cmd == BUF_CMD_READ) ? "READ" : "WRITE");
300 iop_free_mfa(request->psc->iop, request->mfa);
301 if ((request->mfa = iop_get_mfa(request->psc->iop)) == 0xffffffff) {
302 kprintf("pst: timeout no mfa possible\n");
303 devstat_end_transaction_buf(&request->psc->stats, bp);
305 bp->b_flags |= B_ERROR;
306 biodone(request->bio);
307 request->psc->outstanding--;
312 callout_reset(&request->timeout, 10 * hz, pst_timeout, request);
313 if (pst_rw(request)) {
314 iop_free_mfa(request->psc->iop, request->mfa);
315 devstat_end_transaction_buf(&request->psc->stats, bp);
317 bp->b_flags |= B_ERROR;
318 biodone(request->bio);
319 request->psc->outstanding--;
325 pst_rw(struct pst_request *request)
327 struct i2o_bsa_rw_block_message *msg;
329 struct buf *bp = request->bio->bio_buf;
331 msg = (struct i2o_bsa_rw_block_message *)
332 (request->psc->iop->ibase + request->mfa);
333 bzero(msg, sizeof(struct i2o_bsa_rw_block_message));
334 msg->version_offset = 0x81;
335 msg->message_flags = 0x0;
336 msg->message_size = sizeof(struct i2o_bsa_rw_block_message) >> 2;
337 msg->target_address = request->psc->lct->local_tid;
338 msg->initiator_address = I2O_TID_HOST;
339 if (bp->b_cmd == BUF_CMD_READ) {
340 msg->function = I2O_BSA_BLOCK_READ;
341 msg->control_flags = 0x0; /* 0x0c = read cache + readahead */
342 msg->fetch_ahead = 0x0; /* 8 Kb */
346 msg->function = I2O_BSA_BLOCK_WRITE;
347 msg->control_flags = 0x0; /* 0x10 = write behind cache */
348 msg->fetch_ahead = 0x0;
349 sgl_flag = I2O_SGL_DIR;
351 msg->initiator_context = (u_int32_t)pst_done;
352 msg->transaction_context = (u_int32_t)request;
353 msg->time_multiplier = 1;
354 msg->bytecount = bp->b_bcount;
355 msg->lba = request->bio->bio_offset; /* 64 bits */
356 if (!iop_create_sgl((struct i2o_basic_message *)msg, bp->b_data,
357 bp->b_bcount, sgl_flag))
359 request->psc->iop->reg->iqueue = request->mfa;
364 bpack(int8_t *src, int8_t *dst, int len)
367 int8_t *ptr, *buf = dst;
369 for (i = j = blank = 0 ; i < len; i++) {
370 if (blank && src[i] == ' ') continue;
371 if (blank && src[i] != ' ') {
385 for (ptr = buf; ptr < buf+len; ++ptr)
388 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
392 static device_method_t pst_methods[] = {
393 DEVMETHOD(device_probe, pst_probe),
394 DEVMETHOD(device_attach, pst_attach),
398 static driver_t pst_driver = {
401 sizeof(struct pst_softc),
404 static devclass_t pst_devclass;
406 DRIVER_MODULE(pst, pstpci, pst_driver, pst_devclass, NULL, NULL);