xref: /freebsd/sys/dev/pst/pst-raid.c (revision fed1ca4b719c56c930f2259d80663cd34be812bb)
1 /*-
2  * Copyright (c) 2001,2002,2003 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 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
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/bio.h>
38 #include <sys/conf.h>
39 #include <sys/eventhandler.h>
40 #include <sys/malloc.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #include <vm/vm.h>
44 #include <vm/pmap.h>
45 #include <machine/stdarg.h>
46 #include <machine/resource.h>
47 #include <machine/bus.h>
48 #include <sys/rman.h>
49 #include <dev/pci/pcivar.h>
50 #include <dev/pci/pcireg.h>
51 #include <geom/geom_disk.h>
52 
53 #include "dev/pst/pst-iop.h"
54 
55 struct pst_softc {
56     struct iop_softc		*iop;
57     struct i2o_lct_entry	*lct;
58     struct i2o_bsa_device	*info;
59     struct disk			*disk;
60     struct bio_queue_head	queue;
61 };
62 
63 struct pst_request {
64     struct pst_softc		*psc;		/* pointer to softc */
65     u_int32_t			mfa;		/* frame addreess */
66     struct callout		timeout;	/* timeout timer */
67     struct bio			*bp;		/* associated bio ptr */
68 };
69 
70 /* prototypes */
71 static disk_strategy_t pststrategy;
72 static int pst_probe(device_t);
73 static int pst_attach(device_t);
74 static int pst_shutdown(device_t);
75 static void pst_start(struct pst_softc *);
76 static void pst_done(struct iop_softc *, u_int32_t, struct i2o_single_reply *);
77 static int pst_rw(struct pst_request *);
78 static void pst_timeout(void *);
79 static void bpack(int8_t *, int8_t *, int);
80 
81 /* local vars */
82 static MALLOC_DEFINE(M_PSTRAID, "pst", "Promise SuperTrak RAID driver");
83 
84 int
85 pst_add_raid(struct iop_softc *sc, struct i2o_lct_entry *lct)
86 {
87     struct pst_softc *psc;
88     device_t child = device_add_child(sc->dev, "pst", -1);
89 
90     if (!child)
91 	return ENOMEM;
92     if (!(psc = malloc(sizeof(struct pst_softc),
93 		       M_PSTRAID, M_NOWAIT | M_ZERO))) {
94 	device_delete_child(sc->dev, child);
95 	return ENOMEM;
96     }
97     psc->iop = sc;
98     psc->lct = lct;
99     device_set_softc(child, psc);
100     return device_probe_and_attach(child);
101 }
102 
103 static int
104 pst_probe(device_t dev)
105 {
106     device_set_desc(dev, "Promise SuperTrak RAID");
107     return 0;
108 }
109 
110 static int
111 pst_attach(device_t dev)
112 {
113     struct pst_softc *psc = device_get_softc(dev);
114     struct i2o_get_param_reply *reply;
115     struct i2o_device_identity *ident;
116     int lun = device_get_unit(dev);
117     int8_t name [32];
118 
119     if (!(reply = iop_get_util_params(psc->iop, psc->lct->local_tid,
120 				      I2O_PARAMS_OPERATION_FIELD_GET,
121 				      I2O_BSA_DEVICE_INFO_GROUP_NO)))
122 	return ENODEV;
123 
124     if (!(psc->info = (struct i2o_bsa_device *)
125 	    malloc(sizeof(struct i2o_bsa_device), M_PSTRAID, M_NOWAIT))) {
126 	contigfree(reply, PAGE_SIZE, M_PSTIOP);
127 	return ENOMEM;
128     }
129     bcopy(reply->result, psc->info, sizeof(struct i2o_bsa_device));
130     contigfree(reply, PAGE_SIZE, M_PSTIOP);
131 
132     if (!(reply = iop_get_util_params(psc->iop, psc->lct->local_tid,
133 				      I2O_PARAMS_OPERATION_FIELD_GET,
134 				      I2O_UTIL_DEVICE_IDENTITY_GROUP_NO)))
135 	return ENODEV;
136     ident = (struct i2o_device_identity *)reply->result;
137 #ifdef PSTDEBUG
138     printf("pst: vendor=<%.16s> product=<%.16s>\n",
139 	   ident->vendor, ident->product);
140     printf("pst: description=<%.16s> revision=<%.8s>\n",
141 	   ident->description, ident->revision);
142     printf("pst: capacity=%lld blocksize=%d\n",
143 	   psc->info->capacity, psc->info->block_size);
144 #endif
145     bpack(ident->vendor, ident->vendor, 16);
146     bpack(ident->product, ident->product, 16);
147     sprintf(name, "%s %s", ident->vendor, ident->product);
148     contigfree(reply, PAGE_SIZE, M_PSTIOP);
149 
150     bioq_init(&psc->queue);
151 
152     psc->disk = disk_alloc();
153     psc->disk->d_name = "pst";
154     psc->disk->d_strategy = pststrategy;
155     psc->disk->d_maxsize = 64 * 1024; /*I2O_SGL_MAX_SEGS * PAGE_SIZE;*/
156     psc->disk->d_drv1 = psc;
157     psc->disk->d_unit = lun;
158 
159     psc->disk->d_sectorsize = psc->info->block_size;
160     psc->disk->d_mediasize = psc->info->capacity;
161     psc->disk->d_fwsectors = 63;
162     psc->disk->d_fwheads = 255;
163 
164     disk_create(psc->disk, DISK_VERSION);
165 
166     printf("pst%d: %lluMB <%.40s> [%lld/%d/%d] on %.16s\n", lun,
167 	   (unsigned long long)psc->info->capacity / (1024 * 1024),
168 	   name, psc->info->capacity/(512*255*63), 255, 63,
169 	   device_get_nameunit(psc->iop->dev));
170 
171     EVENTHANDLER_REGISTER(shutdown_post_sync, pst_shutdown,
172 			  dev, SHUTDOWN_PRI_FIRST);
173     return 0;
174 }
175 
176 static int
177 pst_shutdown(device_t dev)
178 {
179     struct pst_softc *psc = device_get_softc(dev);
180     struct i2o_bsa_cache_flush_message *msg;
181     int mfa;
182 
183     mfa = iop_get_mfa(psc->iop);
184     msg = (struct i2o_bsa_cache_flush_message *)(psc->iop->ibase + mfa);
185     bzero(msg, sizeof(struct i2o_bsa_cache_flush_message));
186     msg->version_offset = 0x01;
187     msg->message_flags = 0x0;
188     msg->message_size = sizeof(struct i2o_bsa_cache_flush_message) >> 2;
189     msg->target_address = psc->lct->local_tid;
190     msg->initiator_address = I2O_TID_HOST;
191     msg->function = I2O_BSA_CACHE_FLUSH;
192     msg->control_flags = 0x0; /* 0x80 = post progress reports */
193     if (iop_queue_wait_msg(psc->iop, mfa, (struct i2o_basic_message *)msg))
194 	printf("pst: shutdown failed!\n");
195     return 0;
196 }
197 
198 static void
199 pststrategy(struct bio *bp)
200 {
201     struct pst_softc *psc = bp->bio_disk->d_drv1;
202 
203     mtx_lock(&psc->iop->mtx);
204     bioq_disksort(&psc->queue, bp);
205     pst_start(psc);
206     mtx_unlock(&psc->iop->mtx);
207 }
208 
209 static void
210 pst_start(struct pst_softc *psc)
211 {
212     struct pst_request *request;
213     struct bio *bp;
214     u_int32_t mfa;
215 
216     if (psc->iop->outstanding < (I2O_IOP_OUTBOUND_FRAME_COUNT - 1) &&
217 	(bp = bioq_first(&psc->queue))) {
218 	if ((mfa = iop_get_mfa(psc->iop)) != 0xffffffff) {
219 	    bioq_remove(&psc->queue, bp);
220 	    if (!(request = malloc(sizeof(struct pst_request),
221 				   M_PSTRAID, M_NOWAIT | M_ZERO))) {
222 		printf("pst: out of memory in start\n");
223 		biofinish(request->bp, NULL, ENOMEM);
224 		iop_free_mfa(psc->iop, mfa);
225 		return;
226 	    }
227 	    callout_init_mtx(&request->timeout, &psc->iop->mtx, 0);
228 	    psc->iop->outstanding++;
229 	    request->psc = psc;
230 	    request->mfa = mfa;
231 	    request->bp = bp;
232 	    if (pst_rw(request)) {
233 		biofinish(request->bp, NULL, EIO);
234 		iop_free_mfa(request->psc->iop, request->mfa);
235 		psc->iop->outstanding--;
236 		free(request, M_PSTRAID);
237 	    }
238 	}
239     }
240 }
241 
242 static void
243 pst_done(struct iop_softc *sc, u_int32_t mfa, struct i2o_single_reply *reply)
244 {
245     struct pst_request *request =
246 	(struct pst_request *)reply->transaction_context;
247     struct pst_softc *psc = request->psc;
248 
249     callout_stop(&request->timeout);
250     request->bp->bio_resid = request->bp->bio_bcount - reply->donecount;
251     biofinish(request->bp, NULL, reply->status ? EIO : 0);
252     free(request, M_PSTRAID);
253     psc->iop->reg->oqueue = mfa;
254     psc->iop->outstanding--;
255     pst_start(psc);
256 }
257 
258 int
259 pst_rw(struct pst_request *request)
260 {
261     struct i2o_bsa_rw_block_message *msg;
262     int sgl_flag;
263 
264     msg = (struct i2o_bsa_rw_block_message *)
265 	  (request->psc->iop->ibase + request->mfa);
266     bzero(msg, sizeof(struct i2o_bsa_rw_block_message));
267     msg->version_offset = 0x81;
268     msg->message_flags = 0x0;
269     msg->message_size = sizeof(struct i2o_bsa_rw_block_message) >> 2;
270     msg->target_address = request->psc->lct->local_tid;
271     msg->initiator_address = I2O_TID_HOST;
272     switch (request->bp->bio_cmd) {
273     case BIO_READ:
274 	msg->function = I2O_BSA_BLOCK_READ;
275 	msg->control_flags = 0x0; /* 0x0c = read cache + readahead */
276 	msg->fetch_ahead = 0x0; /* 8 Kb */
277 	sgl_flag = 0;
278 	break;
279     case BIO_WRITE:
280 	msg->function = I2O_BSA_BLOCK_WRITE;
281 	msg->control_flags = 0x0; /* 0x10 = write behind cache */
282 	msg->fetch_ahead = 0x0;
283 	sgl_flag = I2O_SGL_DIR;
284 	break;
285     default:
286 	printf("pst: unknown command type 0x%02x\n", request->bp->bio_cmd);
287 	return -1;
288     }
289     msg->initiator_context = (u_int32_t)pst_done;
290     msg->transaction_context = (u_int32_t)request;
291     msg->time_multiplier = 1;
292     msg->bytecount = request->bp->bio_bcount;
293     msg->lba = ((u_int64_t)request->bp->bio_pblkno) * (DEV_BSIZE * 1LL);
294 
295     if (!iop_create_sgl((struct i2o_basic_message *)msg, request->bp->bio_data,
296 			request->bp->bio_bcount, sgl_flag))
297 	return -1;
298 
299     request->psc->iop->reg->iqueue = request->mfa;
300 
301     if (!dumping)
302 	callout_reset(&request->timeout, 10 * hz, pst_timeout, request);
303     return 0;
304 }
305 
306 static void
307 pst_timeout(void *arg)
308 {
309     struct pst_request *request;
310 
311     request = arg;
312     printf("pst: timeout mfa=0x%08x cmd=0x%02x\n",
313 	   request->mfa, request->bp->bio_cmd);
314     mtx_assert(&request->psc->iop->mtx, MA_OWNED);
315     iop_free_mfa(request->psc->iop, request->mfa);
316     if ((request->mfa = iop_get_mfa(request->psc->iop)) == 0xffffffff) {
317 	printf("pst: timeout no mfa possible\n");
318 	biofinish(request->bp, NULL, EIO);
319 	request->psc->iop->outstanding--;
320 	return;
321     }
322     if (pst_rw(request)) {
323 	iop_free_mfa(request->psc->iop, request->mfa);
324 	biofinish(request->bp, NULL, EIO);
325 	request->psc->iop->outstanding--;
326     }
327 }
328 
329 static void
330 bpack(int8_t *src, int8_t *dst, int len)
331 {
332     int i, j, blank;
333     int8_t *ptr, *buf = dst;
334 
335     for (i = j = blank = 0 ; i < len; i++) {
336 	if (blank && src[i] == ' ')
337 	    continue;
338 	if (blank && src[i] != ' ') {
339 	    dst[j++] = src[i];
340 	    blank = 0;
341 	    continue;
342 	}
343 	if (src[i] == ' ') {
344 	    blank = 1;
345 	    if (i == 0)
346 		continue;
347 	}
348 	dst[j++] = src[i];
349     }
350     if (j < len)
351 	dst[j] = 0x00;
352     for (ptr = buf; ptr < buf+len; ++ptr)
353 	if (!*ptr)
354 	    *ptr = ' ';
355     for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
356         *ptr = 0;
357 }
358 
359 static device_method_t pst_methods[] = {
360     DEVMETHOD(device_probe,	pst_probe),
361     DEVMETHOD(device_attach,	pst_attach),
362     { 0, 0 }
363 };
364 
365 static driver_t pst_driver = {
366     "pst",
367     pst_methods,
368     sizeof(struct pst_softc),
369 };
370 
371 static devclass_t pst_devclass;
372 
373 DRIVER_MODULE(pst, pstpci, pst_driver, pst_devclass, 0, 0);
374