Major BUF/BIO work commit. Make I/O BIO-centric and specify the disk or
[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.14 2006/03/24 18:35:32 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/buf2.h>
39 #include <sys/conf.h>
40 #include <sys/disk.h>
41 #include <sys/devicestat.h>
42 #include <sys/eventhandler.h>
43 #include <sys/malloc.h>
44 #include <sys/lock.h>
45 #include <sys/thread2.h>
46 #include <vm/vm.h>
47 #include <vm/pmap.h>
48 #include <machine/stdarg.h>
49 #include <machine/resource.h>
50 #include <machine/bus.h>
51 #include <sys/rman.h>
52 #include <bus/pci/pcivar.h>
53 #include <bus/pci/pcireg.h>
54
55 #include "pst-iop.h"
56
57 /* device structures */ 
58 static d_strategy_t pststrategy;
59 static struct cdevsw pst_cdevsw = {
60     /* name */  "pst",
61     /* maj */   168,
62     /* flags */ D_DISK,
63     /* port */  NULL,
64     /* clone */ NULL,
65
66     /* open */  nullopen,
67     /* close */ nullclose,
68     /* read */  physread,
69     /* write */ physwrite,
70     /* ioctl */ noioctl,
71     /* poll */  nopoll,
72     /* mmap */  nommap,
73     /* strat */ pststrategy,
74     /* dump */  nodump,
75     /* psize */ nopsize
76 };
77
78 struct pst_softc {
79     struct iop_softc            *iop;
80     struct i2o_lct_entry        *lct;
81     struct i2o_bsa_device       *info;
82     dev_t                       device;
83     struct devstat              stats;
84     struct disk                 disk;
85     struct bio_queue_head       bio_queue;
86     int                         outstanding;
87 };
88
89 struct pst_request {
90     struct pst_softc            *psc;           /* pointer to softc */
91     u_int32_t                   mfa;            /* frame addreess */
92     struct callout              timeout;        /* handle for untimeout */
93     struct bio                  *bio;           /* associated bio ptr */
94 };
95
96 /* prototypes */
97 static int pst_probe(device_t);
98 static int pst_attach(device_t);
99 #if 0
100 static int pst_shutdown(device_t);
101 #endif
102 static void pst_start(struct pst_softc *);
103 static void pst_done(struct iop_softc *, u_int32_t, struct i2o_single_reply *);
104 static int pst_rw(struct pst_request *);
105 static void pst_timeout(void *);
106 static void bpack(int8_t *, int8_t *, int);
107
108 /* local vars */
109 static MALLOC_DEFINE(M_PSTRAID, "pst", "Promise SuperTrak RAID driver");
110
111 int
112 pst_add_raid(struct iop_softc *sc, struct i2o_lct_entry *lct)
113 {
114     struct pst_softc *psc;
115     device_t child = device_add_child(sc->dev, "pst", -1);
116
117     if (!child)
118         return ENOMEM;
119     psc = malloc(sizeof(struct pst_softc), M_PSTRAID, M_INTWAIT | M_ZERO); 
120     psc->iop = sc;
121     psc->lct = lct;
122     device_set_softc(child, psc);
123     return bus_generic_attach(sc->dev);
124 }
125
126 static int
127 pst_probe(device_t dev)
128 {
129     device_set_desc(dev, "Promise SuperTrak RAID");
130     return 0;
131 }
132
133 static int
134 pst_attach(device_t dev)
135 {
136     struct pst_softc *psc = device_get_softc(dev);
137     struct i2o_get_param_reply *reply;
138     struct i2o_device_identity *ident;
139     int lun = device_get_unit(dev);
140     int8_t name [32];
141
142     if (!(reply = iop_get_util_params(psc->iop, psc->lct->local_tid,
143                                       I2O_PARAMS_OPERATION_FIELD_GET,
144                                       I2O_BSA_DEVICE_INFO_GROUP_NO)))
145         return ENODEV;
146
147     psc->info = malloc(sizeof(struct i2o_bsa_device), M_PSTRAID, M_INTWAIT);
148     bcopy(reply->result, psc->info, sizeof(struct i2o_bsa_device));
149     contigfree(reply, PAGE_SIZE, M_PSTRAID);
150
151     if (!(reply = iop_get_util_params(psc->iop, psc->lct->local_tid,
152                                       I2O_PARAMS_OPERATION_FIELD_GET,
153                                       I2O_UTIL_DEVICE_IDENTITY_GROUP_NO)))
154         return ENODEV;
155     ident = (struct i2o_device_identity *)reply->result;
156 #ifdef PSTDEBUG    
157     printf("pst: vendor=<%.16s> product=<%.16s>\n",
158            ident->vendor, ident->product);
159     printf("pst: description=<%.16s> revision=<%.8s>\n",
160            ident->description, ident->revision);
161     printf("pst: capacity=%lld blocksize=%d\n",
162            psc->info->capacity, psc->info->block_size);
163 #endif
164     bpack(ident->vendor, ident->vendor, 16);
165     bpack(ident->product, ident->product, 16);
166     sprintf(name, "%s %s", ident->vendor, ident->product);
167     contigfree(reply, PAGE_SIZE, M_PSTRAID);
168
169     bioq_init(&psc->bio_queue);
170
171     psc->device = disk_create(lun, &psc->disk, 0, &pst_cdevsw);
172     psc->device->si_drv1 = psc;
173     psc->device->si_iosize_max = 64 * 1024; /*I2O_SGL_MAX_SEGS * PAGE_SIZE;*/
174
175     bzero(&psc->disk.d_label, sizeof(struct disklabel));
176     psc->disk.d_label.d_secsize = psc->info->block_size;
177     psc->disk.d_label.d_nsectors = 63;
178     psc->disk.d_label.d_ntracks = 255;
179     psc->disk.d_label.d_ncylinders =
180         (psc->info->capacity / psc->info->block_size) / (255 * 63);
181     psc->disk.d_label.d_secpercyl = 255 * 63;
182     psc->disk.d_label.d_secperunit =
183         psc->info->capacity / psc->info->block_size;
184
185     devstat_add_entry(&psc->stats, "pst", lun, psc->info->block_size,
186                       DEVSTAT_NO_ORDERED_TAGS,
187                       DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_IDE,
188                       DEVSTAT_PRIORITY_DISK);
189
190     printf("pst%d: %lluMB <%.40s> [%d/%d/%d] on %.16s\n", lun,
191            (unsigned long long)psc->disk.d_label.d_secperunit / (1024 * 2),
192            name, psc->disk.d_label.d_ncylinders, 255, 63,
193            device_get_nameunit(psc->iop->dev));
194 #if 0
195     EVENTHANDLER_REGISTER(shutdown_post_sync, pst_shutdown,
196                           dev, SHUTDOWN_PRI_DEFAULT);
197 #endif
198     return 0;
199 }
200
201 #if 0
202 static int
203 pst_shutdown(device_t dev)
204 {
205     struct pst_softc *psc = device_get_softc(dev);
206     struct i2o_bsa_cache_flush_message *msg;
207     int mfa;
208
209     mfa = iop_get_mfa(psc->iop);
210     msg = (struct i2o_bsa_cache_flush_message *)(psc->iop->ibase + mfa);
211     bzero(msg, sizeof(struct i2o_bsa_cache_flush_message));
212     msg->version_offset = 0x01;
213     msg->message_flags = 0x0;
214     msg->message_size = sizeof(struct i2o_bsa_cache_flush_message) >> 2;
215     msg->target_address = psc->lct->local_tid;
216     msg->initiator_address = I2O_TID_HOST;
217     msg->function = I2O_BSA_CACHE_FLUSH;
218     msg->control_flags = 0x0; /* 0x80 = post progress reports */
219     if (iop_queue_wait_msg(psc->iop, mfa, (struct i2o_basic_message *)msg))
220         printf("pst: shutdown failed!\n");
221     return 0;
222 }
223 #endif
224
225 static void
226 pststrategy(dev_t dev, struct bio *bio)
227 {
228     struct pst_softc *psc = dev->si_drv1;
229
230     crit_enter();
231     bioqdisksort(&psc->bio_queue, bio);
232     pst_start(psc);
233     crit_exit();
234 }
235
236 static void
237 pst_start(struct pst_softc *psc)
238 {
239     struct pst_request *request;
240     struct buf *bp;
241     struct bio *bio;
242     u_int32_t mfa;
243
244     if (psc->outstanding < (I2O_IOP_OUTBOUND_FRAME_COUNT - 1) &&
245         (bio = bioq_first(&psc->bio_queue))) {
246         if ((mfa = iop_get_mfa(psc->iop)) != 0xffffffff) {
247             request = malloc(sizeof(struct pst_request),
248                                M_PSTRAID, M_INTWAIT | M_ZERO);
249             psc->outstanding++;
250             request->psc = psc;
251             request->mfa = mfa;
252             request->bio = bio;
253             callout_init(&request->timeout);
254             if (!dumping)
255                 callout_reset(&request->timeout, 10 * hz, pst_timeout, request);
256             bioq_remove(&psc->bio_queue, bio);
257             bp = bio->bio_buf;
258             devstat_start_transaction(&psc->stats);
259             if (pst_rw(request)) {
260                 devstat_end_transaction_buf(&psc->stats, bp);
261                 bp->b_error = EIO;
262                 bp->b_flags |= B_ERROR;
263                 biodone(bio);
264                 iop_free_mfa(request->psc->iop, request->mfa);
265                 psc->outstanding--;
266                 callout_stop(&request->timeout);
267                 free(request, M_PSTRAID);
268             }
269         }
270     }
271 }
272
273 static void
274 pst_done(struct iop_softc *sc, u_int32_t mfa, struct i2o_single_reply *reply)
275 {
276     struct pst_request *request =
277         (struct pst_request *)reply->transaction_context;
278     struct pst_softc *psc = request->psc;
279     struct buf *bp = request->bio->bio_buf;
280
281     callout_stop(&request->timeout);
282     bp->b_resid = bp->b_bcount - reply->donecount;
283     devstat_end_transaction_buf(&psc->stats, bp);
284     if (reply->status) {
285         bp->b_error = EIO;
286         bp->b_flags |= B_ERROR;
287     }
288     biodone(request->bio);
289     free(request, M_PSTRAID);
290     crit_enter();
291     psc->iop->reg->oqueue = mfa;
292     psc->outstanding--;
293     pst_start(psc);
294     crit_exit();
295 }
296
297 static void
298 pst_timeout(void *xrequest)
299 {
300     struct pst_request *request = xrequest;
301     struct buf *bp = request->bio->bio_buf;
302
303     crit_enter();
304     printf("pst: timeout mfa=0x%08x cmd=%s\n",
305            request->mfa, bp->b_flags & B_READ ? "READ" : "WRITE");
306     iop_free_mfa(request->psc->iop, request->mfa);
307     if ((request->mfa = iop_get_mfa(request->psc->iop)) == 0xffffffff) {
308         printf("pst: timeout no mfa possible\n");
309         devstat_end_transaction_buf(&request->psc->stats, bp);
310         bp->b_error = EIO;
311         bp->b_flags |= B_ERROR;
312         biodone(request->bio);
313         request->psc->outstanding--;
314         crit_exit();
315         return;
316     }
317     if (!dumping)
318         callout_reset(&request->timeout, 10 * hz, pst_timeout, request);
319     if (pst_rw(request)) {
320         iop_free_mfa(request->psc->iop, request->mfa);
321         devstat_end_transaction_buf(&request->psc->stats, bp);
322         bp->b_error = EIO;
323         bp->b_flags |= B_ERROR;
324         biodone(request->bio);
325         request->psc->outstanding--;
326     }
327     crit_exit();
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     struct buf *bp = request->bio->bio_buf;
336
337     msg = (struct i2o_bsa_rw_block_message *)
338           (request->psc->iop->ibase + request->mfa);
339     bzero(msg, sizeof(struct i2o_bsa_rw_block_message));
340     msg->version_offset = 0x81;
341     msg->message_flags = 0x0;
342     msg->message_size = sizeof(struct i2o_bsa_rw_block_message) >> 2;
343     msg->target_address = request->psc->lct->local_tid;
344     msg->initiator_address = I2O_TID_HOST;
345     if (bp->b_flags & B_READ) {
346         msg->function = I2O_BSA_BLOCK_READ;
347         msg->control_flags = 0x0; /* 0x0c = read cache + readahead */
348         msg->fetch_ahead = 0x0; /* 8 Kb */
349         sgl_flag = 0;
350     }
351     else {
352         msg->function = I2O_BSA_BLOCK_WRITE;
353         msg->control_flags = 0x0; /* 0x10 = write behind cache */
354         msg->fetch_ahead = 0x0;
355         sgl_flag = I2O_SGL_DIR;
356     }
357     msg->initiator_context = (u_int32_t)pst_done;
358     msg->transaction_context = (u_int32_t)request;
359     msg->time_multiplier = 1;
360     msg->bytecount = bp->b_bcount;
361     msg->lba = request->bio->bio_offset;        /* 64 bits */
362     if (!iop_create_sgl((struct i2o_basic_message *)msg, bp->b_data,
363                         bp->b_bcount, sgl_flag))
364         return -1;
365     request->psc->iop->reg->iqueue = request->mfa;
366     return 0;
367 }
368
369 static void
370 bpack(int8_t *src, int8_t *dst, int len)
371 {
372     int i, j, blank;
373     int8_t *ptr, *buf = dst;
374
375     for (i = j = blank = 0 ; i < len; i++) {
376         if (blank && src[i] == ' ') continue;
377         if (blank && src[i] != ' ') {
378             dst[j++] = src[i];
379             blank = 0;
380             continue;
381         }
382         if (src[i] == ' ') {
383             blank = 1;
384             if (i == 0)
385                 continue;
386         }
387         dst[j++] = src[i];
388     }
389     if (j < len) 
390         dst[j] = 0x00;
391     for (ptr = buf; ptr < buf+len; ++ptr)
392         if (!*ptr)
393             *ptr = ' ';
394     for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
395         *ptr = 0;
396 }
397
398 static device_method_t pst_methods[] = {
399     DEVMETHOD(device_probe,     pst_probe),
400     DEVMETHOD(device_attach,    pst_attach),
401     { 0, 0 }
402 };
403         
404 static driver_t pst_driver = {
405     "pst",
406     pst_methods,
407     sizeof(struct pst_softc),
408 };
409
410 static devclass_t pst_devclass;
411
412 DRIVER_MODULE(pst, pstpci, pst_driver, pst_devclass, 0, 0);