Convert files to UTF-8
[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  */
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/bus.h>
36 #include <sys/buf.h>
37 #include <sys/conf.h>
38 #include <sys/disk.h>
39 #include <sys/devicestat.h>
40 #include <sys/eventhandler.h>
41 #include <sys/malloc.h>
42 #include <sys/lock.h>
43 #include <sys/rman.h>
44 #include <sys/buf2.h>
45 #include <sys/thread2.h>
46
47 #include <vm/vm.h>
48 #include <vm/pmap.h>
49
50 #include <machine/stdarg.h>
51
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 dev_ops pst_ops = {
60         { "pst", 0, D_DISK },
61         .d_open =       nullopen,
62         .d_close =      nullclose,
63         .d_read =       physread,
64         .d_write =      physwrite,
65         .d_strategy =   pststrategy,
66 };
67
68 struct pst_softc {
69     struct iop_softc            *iop;
70     struct i2o_lct_entry        *lct;
71     struct i2o_bsa_device       *info;
72     cdev_t                      device;
73     struct devstat              stats;
74     struct disk                 disk;
75     struct bio_queue_head       bio_queue;
76     int                         outstanding;
77 };
78
79 struct pst_request {
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 */
84 };
85
86 /* prototypes */
87 static int pst_probe(device_t);
88 static int pst_attach(device_t);
89 #if 0
90 static int pst_shutdown(device_t);
91 #endif
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);
97
98 /* local vars */
99 static MALLOC_DEFINE(M_PSTRAID, "pst", "Promise SuperTrak RAID driver");
100
101 int
102 pst_add_raid(struct iop_softc *sc, struct i2o_lct_entry *lct)
103 {
104     struct pst_softc *psc;
105     device_t child = device_add_child(sc->dev, "pst", -1);
106
107     if (!child)
108         return ENOMEM;
109     psc = kmalloc(sizeof(struct pst_softc), M_PSTRAID, M_INTWAIT | M_ZERO); 
110     psc->iop = sc;
111     psc->lct = lct;
112     device_set_softc(child, psc);
113     return bus_generic_attach(sc->dev);
114 }
115
116 static int
117 pst_probe(device_t dev)
118 {
119     device_set_desc(dev, "Promise SuperTrak RAID");
120     return 0;
121 }
122
123 static int
124 pst_attach(device_t dev)
125 {
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);
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     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);
154 #endif
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);
159
160     bioq_init(&psc->bio_queue);
161
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;*/
165
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) */
169
170     info.d_secpertrack = 63;                            /* optional */
171     info.d_nheads = 255;
172     info.d_secpercyl = info.d_nheads * info.d_secpertrack;
173     info.d_ncylinders =
174         (psc->info->capacity / psc->info->block_size) / info.d_secpercyl;
175
176     disk_setdiskinfo(&psc->disk, &info);
177
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);
182
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));
187 #if 0
188     EVENTHANDLER_REGISTER(shutdown_post_sync, pst_shutdown,
189                           dev, SHUTDOWN_PRI_DRIVER);
190 #endif
191     return 0;
192 }
193
194 #if 0
195 static int
196 pst_shutdown(device_t dev)
197 {
198     struct pst_softc *psc = device_get_softc(dev);
199     struct i2o_bsa_cache_flush_message *msg;
200     int mfa;
201
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");
214     return 0;
215 }
216 #endif
217
218 static int
219 pststrategy(struct dev_strategy_args *ap)
220 {
221     struct pst_softc *psc = ap->a_head.a_dev->si_drv1;
222
223     crit_enter();
224     bioqdisksort(&psc->bio_queue, ap->a_bio);
225     pst_start(psc);
226     crit_exit();
227     return(0);
228 }
229
230 static void
231 pst_start(struct pst_softc *psc)
232 {
233     struct pst_request *request;
234     struct buf *bp;
235     struct bio *bio;
236     u_int32_t mfa;
237
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);
243             psc->outstanding++;
244             request->psc = psc;
245             request->mfa = mfa;
246             request->bio = bio;
247             callout_init(&request->timeout);
248             if (!dumping)
249                 callout_reset(&request->timeout, 10 * hz, pst_timeout, request);
250             bioq_remove(&psc->bio_queue, bio);
251             bp = bio->bio_buf;
252             devstat_start_transaction(&psc->stats);
253             if (pst_rw(request)) {
254                 devstat_end_transaction_buf(&psc->stats, bp);
255                 bp->b_error = EIO;
256                 bp->b_flags |= B_ERROR;
257                 biodone(bio);
258                 iop_free_mfa(request->psc->iop, request->mfa);
259                 psc->outstanding--;
260                 callout_stop(&request->timeout);
261                 kfree(request, M_PSTRAID);
262             }
263         }
264     }
265 }
266
267 static void
268 pst_done(struct iop_softc *sc, u_int32_t mfa, struct i2o_single_reply *reply)
269 {
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;
274
275     callout_stop(&request->timeout);
276     bp->b_resid = bp->b_bcount - reply->donecount;
277     devstat_end_transaction_buf(&psc->stats, bp);
278     if (reply->status) {
279         bp->b_error = EIO;
280         bp->b_flags |= B_ERROR;
281     }
282     biodone(request->bio);
283     kfree(request, M_PSTRAID);
284     crit_enter();
285     psc->iop->reg->oqueue = mfa;
286     psc->outstanding--;
287     pst_start(psc);
288     crit_exit();
289 }
290
291 static void
292 pst_timeout(void *xrequest)
293 {
294     struct pst_request *request = xrequest;
295     struct buf *bp = request->bio->bio_buf;
296
297     crit_enter();
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);
304         bp->b_error = EIO;
305         bp->b_flags |= B_ERROR;
306         biodone(request->bio);
307         request->psc->outstanding--;
308         crit_exit();
309         return;
310     }
311     if (!dumping)
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);
316         bp->b_error = EIO;
317         bp->b_flags |= B_ERROR;
318         biodone(request->bio);
319         request->psc->outstanding--;
320     }
321     crit_exit();
322 }
323
324 int
325 pst_rw(struct pst_request *request)
326 {
327     struct i2o_bsa_rw_block_message *msg;
328     int sgl_flag;
329     struct buf *bp = request->bio->bio_buf;
330
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 */
343         sgl_flag = 0;
344     }
345     else {
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;
350     }
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))
358         return -1;
359     request->psc->iop->reg->iqueue = request->mfa;
360     return 0;
361 }
362
363 static void
364 bpack(int8_t *src, int8_t *dst, int len)
365 {
366     int i, j, blank;
367     int8_t *ptr, *buf = dst;
368
369     for (i = j = blank = 0 ; i < len; i++) {
370         if (blank && src[i] == ' ') continue;
371         if (blank && src[i] != ' ') {
372             dst[j++] = src[i];
373             blank = 0;
374             continue;
375         }
376         if (src[i] == ' ') {
377             blank = 1;
378             if (i == 0)
379                 continue;
380         }
381         dst[j++] = src[i];
382     }
383     if (j < len) 
384         dst[j] = 0x00;
385     for (ptr = buf; ptr < buf+len; ++ptr)
386         if (!*ptr)
387             *ptr = ' ';
388     for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
389         *ptr = 0;
390 }
391
392 static device_method_t pst_methods[] = {
393     DEVMETHOD(device_probe,     pst_probe),
394     DEVMETHOD(device_attach,    pst_attach),
395     DEVMETHOD_END
396 };
397         
398 static driver_t pst_driver = {
399     "pst",
400     pst_methods,
401     sizeof(struct pst_softc),
402 };
403
404 static devclass_t pst_devclass;
405
406 DRIVER_MODULE(pst, pstpci, pst_driver, pst_devclass, NULL, NULL);