2ab7cb800fca31b09765cdb3eeb18b2b2858ef23
[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.25 2007/11/14 18:27:52 swildner 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 address */
83     struct callout              timeout;        /* callout handle */
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     struct disk_info info;
131     int lun = device_get_unit(dev);
132     int8_t name [32];
133
134     if (!(reply = iop_get_util_params(psc->iop, psc->lct->local_tid,
135                                       I2O_PARAMS_OPERATION_FIELD_GET,
136                                       I2O_BSA_DEVICE_INFO_GROUP_NO)))
137         return ENODEV;
138
139     psc->info = kmalloc(sizeof(struct i2o_bsa_device), M_PSTRAID, M_INTWAIT);
140     bcopy(reply->result, psc->info, sizeof(struct i2o_bsa_device));
141     contigfree(reply, PAGE_SIZE, M_PSTRAID);
142
143     if (!(reply = iop_get_util_params(psc->iop, psc->lct->local_tid,
144                                       I2O_PARAMS_OPERATION_FIELD_GET,
145                                       I2O_UTIL_DEVICE_IDENTITY_GROUP_NO)))
146         return ENODEV;
147     ident = (struct i2o_device_identity *)reply->result;
148 #ifdef PSTDEBUG    
149     kprintf("pst: vendor=<%.16s> product=<%.16s>\n",
150            ident->vendor, ident->product);
151     kprintf("pst: description=<%.16s> revision=<%.8s>\n",
152            ident->description, ident->revision);
153     kprintf("pst: capacity=%lld blocksize=%d\n",
154            psc->info->capacity, psc->info->block_size);
155 #endif
156     bpack(ident->vendor, ident->vendor, 16);
157     bpack(ident->product, ident->product, 16);
158     ksprintf(name, "%s %s", ident->vendor, ident->product);
159     contigfree(reply, PAGE_SIZE, M_PSTRAID);
160
161     bioq_init(&psc->bio_queue);
162
163     psc->device = disk_create(lun, &psc->disk, &pst_ops);
164     psc->device->si_drv1 = psc;
165     psc->device->si_iosize_max = 64 * 1024; /*I2O_SGL_MAX_SEGS * PAGE_SIZE;*/
166
167     bzero(&info, sizeof(info));
168     info.d_media_blksize = psc->info->block_size;       /* mandatory */
169     info.d_media_size = psc->info->capacity;            /* (in bytes) */
170
171     info.d_secpertrack = 63;                            /* optional */
172     info.d_nheads = 255;
173     info.d_secpercyl = info.d_nheads * info.d_secpertrack;
174     info.d_ncylinders =
175         (psc->info->capacity / psc->info->block_size) / info.d_secpercyl;
176
177     disk_setdiskinfo(&psc->disk, &info);
178
179     devstat_add_entry(&psc->stats, "pst", lun, psc->info->block_size,
180                       DEVSTAT_NO_ORDERED_TAGS,
181                       DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_IDE,
182                       DEVSTAT_PRIORITY_DISK);
183
184     kprintf("pst%d: %lluMB <%.40s> [%d/%d/%d] on %.16s\n", lun,
185            info.d_media_size / (1024 * 1024), name,
186            info.d_ncylinders , info.d_nheads, info.d_secpertrack,
187            device_get_nameunit(psc->iop->dev));
188 #if 0
189     EVENTHANDLER_REGISTER(shutdown_post_sync, pst_shutdown,
190                           dev, SHUTDOWN_PRI_DRIVER);
191 #endif
192     return 0;
193 }
194
195 #if 0
196 static int
197 pst_shutdown(device_t dev)
198 {
199     struct pst_softc *psc = device_get_softc(dev);
200     struct i2o_bsa_cache_flush_message *msg;
201     int mfa;
202
203     mfa = iop_get_mfa(psc->iop);
204     msg = (struct i2o_bsa_cache_flush_message *)(psc->iop->ibase + mfa);
205     bzero(msg, sizeof(struct i2o_bsa_cache_flush_message));
206     msg->version_offset = 0x01;
207     msg->message_flags = 0x0;
208     msg->message_size = sizeof(struct i2o_bsa_cache_flush_message) >> 2;
209     msg->target_address = psc->lct->local_tid;
210     msg->initiator_address = I2O_TID_HOST;
211     msg->function = I2O_BSA_CACHE_FLUSH;
212     msg->control_flags = 0x0; /* 0x80 = post progress reports */
213     if (iop_queue_wait_msg(psc->iop, mfa, (struct i2o_basic_message *)msg))
214         kprintf("pst: shutdown failed!\n");
215     return 0;
216 }
217 #endif
218
219 static int
220 pststrategy(struct dev_strategy_args *ap)
221 {
222     struct pst_softc *psc = ap->a_head.a_dev->si_drv1;
223
224     crit_enter();
225     bioqdisksort(&psc->bio_queue, ap->a_bio);
226     pst_start(psc);
227     crit_exit();
228     return(0);
229 }
230
231 static void
232 pst_start(struct pst_softc *psc)
233 {
234     struct pst_request *request;
235     struct buf *bp;
236     struct bio *bio;
237     u_int32_t mfa;
238
239     if (psc->outstanding < (I2O_IOP_OUTBOUND_FRAME_COUNT - 1) &&
240         (bio = bioq_first(&psc->bio_queue))) {
241         if ((mfa = iop_get_mfa(psc->iop)) != 0xffffffff) {
242             request = kmalloc(sizeof(struct pst_request),
243                                M_PSTRAID, M_INTWAIT | M_ZERO);
244             psc->outstanding++;
245             request->psc = psc;
246             request->mfa = mfa;
247             request->bio = bio;
248             callout_init(&request->timeout);
249             if (!dumping)
250                 callout_reset(&request->timeout, 10 * hz, pst_timeout, request);
251             bioq_remove(&psc->bio_queue, bio);
252             bp = bio->bio_buf;
253             devstat_start_transaction(&psc->stats);
254             if (pst_rw(request)) {
255                 devstat_end_transaction_buf(&psc->stats, bp);
256                 bp->b_error = EIO;
257                 bp->b_flags |= B_ERROR;
258                 biodone(bio);
259                 iop_free_mfa(request->psc->iop, request->mfa);
260                 psc->outstanding--;
261                 callout_stop(&request->timeout);
262                 kfree(request, M_PSTRAID);
263             }
264         }
265     }
266 }
267
268 static void
269 pst_done(struct iop_softc *sc, u_int32_t mfa, struct i2o_single_reply *reply)
270 {
271     struct pst_request *request =
272         (struct pst_request *)reply->transaction_context;
273     struct pst_softc *psc = request->psc;
274     struct buf *bp = request->bio->bio_buf;
275
276     callout_stop(&request->timeout);
277     bp->b_resid = bp->b_bcount - reply->donecount;
278     devstat_end_transaction_buf(&psc->stats, bp);
279     if (reply->status) {
280         bp->b_error = EIO;
281         bp->b_flags |= B_ERROR;
282     }
283     biodone(request->bio);
284     kfree(request, M_PSTRAID);
285     crit_enter();
286     psc->iop->reg->oqueue = mfa;
287     psc->outstanding--;
288     pst_start(psc);
289     crit_exit();
290 }
291
292 static void
293 pst_timeout(void *xrequest)
294 {
295     struct pst_request *request = xrequest;
296     struct buf *bp = request->bio->bio_buf;
297
298     crit_enter();
299     kprintf("pst: timeout mfa=0x%08x cmd=%s\n",
300            request->mfa, (bp->b_cmd == BUF_CMD_READ) ? "READ" : "WRITE");
301     iop_free_mfa(request->psc->iop, request->mfa);
302     if ((request->mfa = iop_get_mfa(request->psc->iop)) == 0xffffffff) {
303         kprintf("pst: timeout no mfa possible\n");
304         devstat_end_transaction_buf(&request->psc->stats, bp);
305         bp->b_error = EIO;
306         bp->b_flags |= B_ERROR;
307         biodone(request->bio);
308         request->psc->outstanding--;
309         crit_exit();
310         return;
311     }
312     if (!dumping)
313         callout_reset(&request->timeout, 10 * hz, pst_timeout, request);
314     if (pst_rw(request)) {
315         iop_free_mfa(request->psc->iop, request->mfa);
316         devstat_end_transaction_buf(&request->psc->stats, bp);
317         bp->b_error = EIO;
318         bp->b_flags |= B_ERROR;
319         biodone(request->bio);
320         request->psc->outstanding--;
321     }
322     crit_exit();
323 }
324
325 int
326 pst_rw(struct pst_request *request)
327 {
328     struct i2o_bsa_rw_block_message *msg;
329     int sgl_flag;
330     struct buf *bp = request->bio->bio_buf;
331
332     msg = (struct i2o_bsa_rw_block_message *)
333           (request->psc->iop->ibase + request->mfa);
334     bzero(msg, sizeof(struct i2o_bsa_rw_block_message));
335     msg->version_offset = 0x81;
336     msg->message_flags = 0x0;
337     msg->message_size = sizeof(struct i2o_bsa_rw_block_message) >> 2;
338     msg->target_address = request->psc->lct->local_tid;
339     msg->initiator_address = I2O_TID_HOST;
340     if (bp->b_cmd == BUF_CMD_READ) {
341         msg->function = I2O_BSA_BLOCK_READ;
342         msg->control_flags = 0x0; /* 0x0c = read cache + readahead */
343         msg->fetch_ahead = 0x0; /* 8 Kb */
344         sgl_flag = 0;
345     }
346     else {
347         msg->function = I2O_BSA_BLOCK_WRITE;
348         msg->control_flags = 0x0; /* 0x10 = write behind cache */
349         msg->fetch_ahead = 0x0;
350         sgl_flag = I2O_SGL_DIR;
351     }
352     msg->initiator_context = (u_int32_t)pst_done;
353     msg->transaction_context = (u_int32_t)request;
354     msg->time_multiplier = 1;
355     msg->bytecount = bp->b_bcount;
356     msg->lba = request->bio->bio_offset;        /* 64 bits */
357     if (!iop_create_sgl((struct i2o_basic_message *)msg, bp->b_data,
358                         bp->b_bcount, sgl_flag))
359         return -1;
360     request->psc->iop->reg->iqueue = request->mfa;
361     return 0;
362 }
363
364 static void
365 bpack(int8_t *src, int8_t *dst, int len)
366 {
367     int i, j, blank;
368     int8_t *ptr, *buf = dst;
369
370     for (i = j = blank = 0 ; i < len; i++) {
371         if (blank && src[i] == ' ') continue;
372         if (blank && src[i] != ' ') {
373             dst[j++] = src[i];
374             blank = 0;
375             continue;
376         }
377         if (src[i] == ' ') {
378             blank = 1;
379             if (i == 0)
380                 continue;
381         }
382         dst[j++] = src[i];
383     }
384     if (j < len) 
385         dst[j] = 0x00;
386     for (ptr = buf; ptr < buf+len; ++ptr)
387         if (!*ptr)
388             *ptr = ' ';
389     for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
390         *ptr = 0;
391 }
392
393 static device_method_t pst_methods[] = {
394     DEVMETHOD(device_probe,     pst_probe),
395     DEVMETHOD(device_attach,    pst_attach),
396     { 0, 0 }
397 };
398         
399 static driver_t pst_driver = {
400     "pst",
401     pst_methods,
402     sizeof(struct pst_softc),
403 };
404
405 static devclass_t pst_devclass;
406
407 DRIVER_MODULE(pst, pstpci, pst_driver, pst_devclass, 0, 0);