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