Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / dev / raid / pst / pst-raid.c
1 /*-
2  * Copyright (c) 2001,2002 Søren Schmidt <sos@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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.
27  *
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 $
30  */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/buf.h>
38 #include <sys/conf.h>
39 #include <sys/disk.h>
40 #include <sys/devicestat.h>
41 #include <sys/eventhandler.h>
42 #include <sys/malloc.h>
43 #include <sys/lock.h>
44 #include <vm/vm.h>
45 #include <vm/pmap.h>
46 #include <machine/stdarg.h>
47 #include <machine/resource.h>
48 #include <machine/bus.h>
49 #include <sys/rman.h>
50 #include <pci/pcivar.h>
51 #include <pci/pcireg.h>
52
53 #include "dev/pst/pst-iop.h"
54
55 /* device structures */ 
56 static d_strategy_t pststrategy;
57 static struct cdevsw pst_cdevsw = {
58     /* open */  nullopen,
59     /* close */ nullclose,
60     /* read */  physread,
61     /* write */ physwrite,
62     /* ioctl */ noioctl,
63     /* poll */  nopoll,
64     /* mmap */  nommap,
65     /* strat */ pststrategy,
66     /* name */  "pst",
67     /* maj */   168,
68     /* dump */  nodump,
69     /* psize */ nopsize,
70     /* flags */ D_DISK,
71 };
72 static struct cdevsw pstdisk_cdevsw;
73
74 struct pst_softc {
75     struct iop_softc            *iop;
76     struct i2o_lct_entry        *lct;
77     struct i2o_bsa_device       *info;
78     dev_t                       device;
79     struct devstat              stats;
80     struct disk                 disk;
81     struct buf_queue_head       queue;
82     int                         outstanding;
83 };
84
85 struct pst_request {
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 */
90 };
91
92 /* prototypes */
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);
101
102 /* local vars */
103 static MALLOC_DEFINE(M_PSTRAID, "pst", "Promise SuperTrak RAID driver");
104
105 int
106 pst_add_raid(struct iop_softc *sc, struct i2o_lct_entry *lct)
107 {
108     struct pst_softc *psc;
109     device_t child = device_add_child(sc->dev, "pst", -1);
110
111     if (!child)
112         return ENOMEM;
113     psc = malloc(sizeof(struct pst_softc), M_PSTRAID, M_NOWAIT | M_ZERO); 
114     psc->iop = sc;
115     psc->lct = lct;
116     device_set_softc(child, psc);
117     return bus_generic_attach(sc->dev);
118 }
119
120 static int
121 pst_probe(device_t dev)
122 {
123     device_set_desc(dev, "Promise SuperTrak RAID");
124     return 0;
125 }
126
127 static int
128 pst_attach(device_t dev)
129 {
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);
134     int8_t name [32];
135
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)))
139         return ENODEV;
140
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);
144         return ENOMEM;
145     }
146     bcopy(reply->result, psc->info, sizeof(struct i2o_bsa_device));
147     contigfree(reply, PAGE_SIZE, M_PSTRAID);
148
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)))
152         return ENODEV;
153     ident = (struct i2o_device_identity *)reply->result;
154 #ifdef PSTDEBUG    
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);
161 #endif
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);
166
167     bufq_init(&psc->queue);
168
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;*/
172
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;
182
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);
187
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));
192 #if 0
193     EVENTHANDLER_REGISTER(shutdown_post_sync, pst_shutdown,
194                           dev, SHUTDOWN_PRI_DEFAULT);
195 #endif
196     return 0;
197 }
198
199 static int
200 pst_shutdown(device_t dev)
201 {
202     struct pst_softc *psc = device_get_softc(dev);
203     struct i2o_bsa_cache_flush_message *msg;
204     int mfa;
205
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");
218     return 0;
219 }
220
221 static void
222 pststrategy(struct buf *bp)
223 {
224     struct pst_softc *psc = bp->b_dev->si_drv1;
225     int s = splbio();
226
227     bufqdisksort(&psc->queue, bp);
228     pst_start(psc);
229     splx(s);
230 }
231
232 static void
233 pst_start(struct pst_softc *psc)
234 {
235     struct pst_request *request;
236     struct buf *bp;
237     u_int32_t mfa;
238
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);
246                 return;
247             }
248             psc->outstanding++;
249             request->psc = psc;
250             request->mfa = mfa;
251             request->bp = bp;
252             if (dumping)
253                 request->timeout_handle.callout = NULL;
254             else
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);
265                 psc->outstanding--;
266                 free(request, M_PSTRAID);
267             }
268         }
269     }
270 }
271
272 static void
273 pst_done(struct iop_softc *sc, u_int32_t mfa, struct i2o_single_reply *reply)
274 {
275     struct pst_request *request =
276         (struct pst_request *)reply->transaction_context;
277     struct pst_softc *psc = request->psc;
278     int s;
279
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);
283     if (reply->status) {
284         request->bp->b_error = EIO;
285         request->bp->b_flags |= B_ERROR;
286     }
287     biodone(request->bp);
288     free(request, M_PSTRAID);
289     s = splbio();
290     psc->iop->reg->oqueue = mfa;
291     psc->outstanding--;
292     pst_start(psc);
293     splx(s);
294 }
295
296 static void
297 pst_timeout(struct pst_request *request)
298 {
299     int s = splbio();
300
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--;
311         splx(s);
312         return;
313     }
314     if (dumping)
315         request->timeout_handle.callout = NULL;
316     else
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--;
326     }
327     splx(s);
328 }
329
330 int
331 pst_rw(struct pst_request *request)
332 {
333     struct i2o_bsa_rw_block_message *msg;
334     int sgl_flag;
335
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 */
348         sgl_flag = 0;
349     }
350     else {
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;
355     }
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))
363         return -1;
364     request->psc->iop->reg->iqueue = request->mfa;
365     return 0;
366 }
367
368 static void
369 bpack(int8_t *src, int8_t *dst, int len)
370 {
371     int i, j, blank;
372     int8_t *ptr, *buf = dst;
373
374     for (i = j = blank = 0 ; i < len; i++) {
375         if (blank && src[i] == ' ') continue;
376         if (blank && src[i] != ' ') {
377             dst[j++] = src[i];
378             blank = 0;
379             continue;
380         }
381         if (src[i] == ' ') {
382             blank = 1;
383             if (i == 0)
384                 continue;
385         }
386         dst[j++] = src[i];
387     }
388     if (j < len) 
389         dst[j] = 0x00;
390     for (ptr = buf; ptr < buf+len; ++ptr)
391         if (!*ptr)
392             *ptr = ' ';
393     for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
394         *ptr = 0;
395 }
396
397 static device_method_t pst_methods[] = {
398     DEVMETHOD(device_probe,     pst_probe),
399     DEVMETHOD(device_attach,    pst_attach),
400     { 0, 0 }
401 };
402         
403 static driver_t pst_driver = {
404     "pst",
405     pst_methods,
406     sizeof(struct pst_softc),
407 };
408
409 static devclass_t pst_devclass;
410
411 DRIVER_MODULE(pst, pstpci, pst_driver, pst_devclass, 0, 0);