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