Do a major clean-up of the BUSDMA architecture. A large number of
[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.20 2006/10/25 20:56:01 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 <sys/rman.h>
45 #include <sys/buf2.h>
46 #include <sys/thread2.h>
47
48 #include <vm/vm.h>
49 #include <vm/pmap.h>
50
51 #include <machine/stdarg.h>
52
53 #include <bus/pci/pcivar.h>
54 #include <bus/pci/pcireg.h>
55
56 #include "pst-iop.h"
57
58 /* device structures */ 
59 static d_strategy_t pststrategy;
60 static struct dev_ops pst_ops = {
61         { "pst", 168, D_DISK },
62         .d_open =       nullopen,
63         .d_close =      nullclose,
64         .d_read =       physread,
65         .d_write =      physwrite,
66         .d_strategy =   pststrategy,
67 };
68
69 struct pst_softc {
70     struct iop_softc            *iop;
71     struct i2o_lct_entry        *lct;
72     struct i2o_bsa_device       *info;
73     cdev_t                      device;
74     struct devstat              stats;
75     struct disk                 disk;
76     struct bio_queue_head       bio_queue;
77     int                         outstanding;
78 };
79
80 struct pst_request {
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 */
85 };
86
87 /* prototypes */
88 static int pst_probe(device_t);
89 static int pst_attach(device_t);
90 #if 0
91 static int pst_shutdown(device_t);
92 #endif
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);
98
99 /* local vars */
100 static MALLOC_DEFINE(M_PSTRAID, "pst", "Promise SuperTrak RAID driver");
101
102 int
103 pst_add_raid(struct iop_softc *sc, struct i2o_lct_entry *lct)
104 {
105     struct pst_softc *psc;
106     device_t child = device_add_child(sc->dev, "pst", -1);
107
108     if (!child)
109         return ENOMEM;
110     psc = kmalloc(sizeof(struct pst_softc), M_PSTRAID, M_INTWAIT | M_ZERO); 
111     psc->iop = sc;
112     psc->lct = lct;
113     device_set_softc(child, psc);
114     return bus_generic_attach(sc->dev);
115 }
116
117 static int
118 pst_probe(device_t dev)
119 {
120     device_set_desc(dev, "Promise SuperTrak RAID");
121     return 0;
122 }
123
124 static int
125 pst_attach(device_t dev)
126 {
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);
131     int8_t name [32];
132
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)))
136         return ENODEV;
137
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);
141
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)))
145         return ENODEV;
146     ident = (struct i2o_device_identity *)reply->result;
147 #ifdef PSTDEBUG    
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);
154 #endif
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);
159
160     bioq_init(&psc->bio_queue);
161
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;*/
165
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;
175
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);
180
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));
185 #if 0
186     EVENTHANDLER_REGISTER(shutdown_post_sync, pst_shutdown,
187                           dev, SHUTDOWN_PRI_DEFAULT);
188 #endif
189     return 0;
190 }
191
192 #if 0
193 static int
194 pst_shutdown(device_t dev)
195 {
196     struct pst_softc *psc = device_get_softc(dev);
197     struct i2o_bsa_cache_flush_message *msg;
198     int mfa;
199
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");
212     return 0;
213 }
214 #endif
215
216 static int
217 pststrategy(struct dev_strategy_args *ap)
218 {
219     struct pst_softc *psc = ap->a_head.a_dev->si_drv1;
220
221     crit_enter();
222     bioqdisksort(&psc->bio_queue, ap->a_bio);
223     pst_start(psc);
224     crit_exit();
225     return(0);
226 }
227
228 static void
229 pst_start(struct pst_softc *psc)
230 {
231     struct pst_request *request;
232     struct buf *bp;
233     struct bio *bio;
234     u_int32_t mfa;
235
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);
241             psc->outstanding++;
242             request->psc = psc;
243             request->mfa = mfa;
244             request->bio = bio;
245             callout_init(&request->timeout);
246             if (!dumping)
247                 callout_reset(&request->timeout, 10 * hz, pst_timeout, request);
248             bioq_remove(&psc->bio_queue, bio);
249             bp = bio->bio_buf;
250             devstat_start_transaction(&psc->stats);
251             if (pst_rw(request)) {
252                 devstat_end_transaction_buf(&psc->stats, bp);
253                 bp->b_error = EIO;
254                 bp->b_flags |= B_ERROR;
255                 biodone(bio);
256                 iop_free_mfa(request->psc->iop, request->mfa);
257                 psc->outstanding--;
258                 callout_stop(&request->timeout);
259                 kfree(request, M_PSTRAID);
260             }
261         }
262     }
263 }
264
265 static void
266 pst_done(struct iop_softc *sc, u_int32_t mfa, struct i2o_single_reply *reply)
267 {
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;
272
273     callout_stop(&request->timeout);
274     bp->b_resid = bp->b_bcount - reply->donecount;
275     devstat_end_transaction_buf(&psc->stats, bp);
276     if (reply->status) {
277         bp->b_error = EIO;
278         bp->b_flags |= B_ERROR;
279     }
280     biodone(request->bio);
281     kfree(request, M_PSTRAID);
282     crit_enter();
283     psc->iop->reg->oqueue = mfa;
284     psc->outstanding--;
285     pst_start(psc);
286     crit_exit();
287 }
288
289 static void
290 pst_timeout(void *xrequest)
291 {
292     struct pst_request *request = xrequest;
293     struct buf *bp = request->bio->bio_buf;
294
295     crit_enter();
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);
302         bp->b_error = EIO;
303         bp->b_flags |= B_ERROR;
304         biodone(request->bio);
305         request->psc->outstanding--;
306         crit_exit();
307         return;
308     }
309     if (!dumping)
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);
314         bp->b_error = EIO;
315         bp->b_flags |= B_ERROR;
316         biodone(request->bio);
317         request->psc->outstanding--;
318     }
319     crit_exit();
320 }
321
322 int
323 pst_rw(struct pst_request *request)
324 {
325     struct i2o_bsa_rw_block_message *msg;
326     int sgl_flag;
327     struct buf *bp = request->bio->bio_buf;
328
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 */
341         sgl_flag = 0;
342     }
343     else {
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;
348     }
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))
356         return -1;
357     request->psc->iop->reg->iqueue = request->mfa;
358     return 0;
359 }
360
361 static void
362 bpack(int8_t *src, int8_t *dst, int len)
363 {
364     int i, j, blank;
365     int8_t *ptr, *buf = dst;
366
367     for (i = j = blank = 0 ; i < len; i++) {
368         if (blank && src[i] == ' ') continue;
369         if (blank && src[i] != ' ') {
370             dst[j++] = src[i];
371             blank = 0;
372             continue;
373         }
374         if (src[i] == ' ') {
375             blank = 1;
376             if (i == 0)
377                 continue;
378         }
379         dst[j++] = src[i];
380     }
381     if (j < len) 
382         dst[j] = 0x00;
383     for (ptr = buf; ptr < buf+len; ++ptr)
384         if (!*ptr)
385             *ptr = ' ';
386     for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
387         *ptr = 0;
388 }
389
390 static device_method_t pst_methods[] = {
391     DEVMETHOD(device_probe,     pst_probe),
392     DEVMETHOD(device_attach,    pst_attach),
393     { 0, 0 }
394 };
395         
396 static driver_t pst_driver = {
397     "pst",
398     pst_methods,
399     sizeof(struct pst_softc),
400 };
401
402 static devclass_t pst_devclass;
403
404 DRIVER_MODULE(pst, pstpci, pst_driver, pst_devclass, 0, 0);