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