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