xref: /freebsd/sys/dev/pst/pst-raid.c (revision 0957b409a90fd597c1e9124cbaf3edd2b488f4ac)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2001,2002,2003 Søren Schmidt <sos@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer,
12  *    without modification, immediately at the beginning of the file.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/bus.h>
39 #include <sys/bio.h>
40 #include <sys/conf.h>
41 #include <sys/eventhandler.h>
42 #include <sys/malloc.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.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 <dev/pci/pcivar.h>
52 #include <dev/pci/pcireg.h>
53 #include <geom/geom_disk.h>
54 
55 #include "dev/pst/pst-iop.h"
56 
57 struct pst_softc {
58     struct iop_softc		*iop;
59     struct i2o_lct_entry	*lct;
60     struct i2o_bsa_device	*info;
61     struct disk			*disk;
62     struct bio_queue_head	queue;
63 };
64 
65 struct pst_request {
66     struct pst_softc		*psc;		/* pointer to softc */
67     u_int32_t			mfa;		/* frame addreess */
68     struct callout		timeout;	/* timeout timer */
69     struct bio			*bp;		/* associated bio ptr */
70 };
71 
72 /* prototypes */
73 static disk_strategy_t pststrategy;
74 static int pst_probe(device_t);
75 static int pst_attach(device_t);
76 static int pst_shutdown(device_t);
77 static void pst_start(struct pst_softc *);
78 static void pst_done(struct iop_softc *, u_int32_t, struct i2o_single_reply *);
79 static int pst_rw(struct pst_request *);
80 static void pst_timeout(void *);
81 static void bpack(int8_t *, int8_t *, int);
82 
83 /* local vars */
84 static MALLOC_DEFINE(M_PSTRAID, "pst", "Promise SuperTrak RAID driver");
85 
86 int
87 pst_add_raid(struct iop_softc *sc, struct i2o_lct_entry *lct)
88 {
89     struct pst_softc *psc;
90     device_t child = device_add_child(sc->dev, "pst", -1);
91 
92     if (!child)
93 	return ENOMEM;
94     if (!(psc = malloc(sizeof(struct pst_softc),
95 		       M_PSTRAID, M_NOWAIT | M_ZERO))) {
96 	device_delete_child(sc->dev, child);
97 	return ENOMEM;
98     }
99     psc->iop = sc;
100     psc->lct = lct;
101     device_set_softc(child, psc);
102     return device_probe_and_attach(child);
103 }
104 
105 static int
106 pst_probe(device_t dev)
107 {
108     device_set_desc(dev, "Promise SuperTrak RAID");
109     return 0;
110 }
111 
112 static int
113 pst_attach(device_t dev)
114 {
115     struct pst_softc *psc = device_get_softc(dev);
116     struct i2o_get_param_reply *reply;
117     struct i2o_device_identity *ident;
118     int lun = device_get_unit(dev);
119     int8_t name [32];
120 
121     if (!(reply = iop_get_util_params(psc->iop, psc->lct->local_tid,
122 				      I2O_PARAMS_OPERATION_FIELD_GET,
123 				      I2O_BSA_DEVICE_INFO_GROUP_NO)))
124 	return ENODEV;
125 
126     if (!(psc->info = (struct i2o_bsa_device *)
127 	    malloc(sizeof(struct i2o_bsa_device), M_PSTRAID, M_NOWAIT))) {
128 	contigfree(reply, PAGE_SIZE, M_PSTIOP);
129 	return ENOMEM;
130     }
131     bcopy(reply->result, psc->info, sizeof(struct i2o_bsa_device));
132     contigfree(reply, PAGE_SIZE, M_PSTIOP);
133 
134     if (!(reply = iop_get_util_params(psc->iop, psc->lct->local_tid,
135 				      I2O_PARAMS_OPERATION_FIELD_GET,
136 				      I2O_UTIL_DEVICE_IDENTITY_GROUP_NO)))
137 	return ENODEV;
138     ident = (struct i2o_device_identity *)reply->result;
139 #ifdef PSTDEBUG
140     printf("pst: vendor=<%.16s> product=<%.16s>\n",
141 	   ident->vendor, ident->product);
142     printf("pst: description=<%.16s> revision=<%.8s>\n",
143 	   ident->description, ident->revision);
144     printf("pst: capacity=%lld blocksize=%d\n",
145 	   psc->info->capacity, psc->info->block_size);
146 #endif
147     bpack(ident->vendor, ident->vendor, 16);
148     bpack(ident->product, ident->product, 16);
149     sprintf(name, "%s %s", ident->vendor, ident->product);
150     contigfree(reply, PAGE_SIZE, M_PSTIOP);
151 
152     bioq_init(&psc->queue);
153 
154     psc->disk = disk_alloc();
155     psc->disk->d_name = "pst";
156     psc->disk->d_strategy = pststrategy;
157     psc->disk->d_maxsize = 64 * 1024; /*I2O_SGL_MAX_SEGS * PAGE_SIZE;*/
158     psc->disk->d_drv1 = psc;
159     psc->disk->d_unit = lun;
160 
161     psc->disk->d_sectorsize = psc->info->block_size;
162     psc->disk->d_mediasize = psc->info->capacity;
163     psc->disk->d_fwsectors = 63;
164     psc->disk->d_fwheads = 255;
165 
166     disk_create(psc->disk, DISK_VERSION);
167 
168     printf("pst%d: %lluMB <%.40s> [%lld/%d/%d] on %.16s\n", lun,
169 	   (unsigned long long)psc->info->capacity / (1024 * 1024),
170 	   name, psc->info->capacity/(512*255*63), 255, 63,
171 	   device_get_nameunit(psc->iop->dev));
172 
173     EVENTHANDLER_REGISTER(shutdown_post_sync, pst_shutdown,
174 			  dev, SHUTDOWN_PRI_FIRST);
175     return 0;
176 }
177 
178 static int
179 pst_shutdown(device_t dev)
180 {
181     struct pst_softc *psc = device_get_softc(dev);
182     struct i2o_bsa_cache_flush_message *msg;
183     int mfa;
184 
185     mfa = iop_get_mfa(psc->iop);
186     msg = (struct i2o_bsa_cache_flush_message *)(psc->iop->ibase + mfa);
187     bzero(msg, sizeof(struct i2o_bsa_cache_flush_message));
188     msg->version_offset = 0x01;
189     msg->message_flags = 0x0;
190     msg->message_size = sizeof(struct i2o_bsa_cache_flush_message) >> 2;
191     msg->target_address = psc->lct->local_tid;
192     msg->initiator_address = I2O_TID_HOST;
193     msg->function = I2O_BSA_CACHE_FLUSH;
194     msg->control_flags = 0x0; /* 0x80 = post progress reports */
195     if (iop_queue_wait_msg(psc->iop, mfa, (struct i2o_basic_message *)msg))
196 	printf("pst: shutdown failed!\n");
197     return 0;
198 }
199 
200 static void
201 pststrategy(struct bio *bp)
202 {
203     struct pst_softc *psc = bp->bio_disk->d_drv1;
204 
205     mtx_lock(&psc->iop->mtx);
206     bioq_disksort(&psc->queue, bp);
207     pst_start(psc);
208     mtx_unlock(&psc->iop->mtx);
209 }
210 
211 static void
212 pst_start(struct pst_softc *psc)
213 {
214     struct pst_request *request;
215     struct bio *bp;
216     u_int32_t mfa;
217 
218     if (psc->iop->outstanding < (I2O_IOP_OUTBOUND_FRAME_COUNT - 1) &&
219 	(bp = bioq_first(&psc->queue))) {
220 	if ((mfa = iop_get_mfa(psc->iop)) != 0xffffffff) {
221 	    bioq_remove(&psc->queue, bp);
222 	    if (!(request = malloc(sizeof(struct pst_request),
223 				   M_PSTRAID, M_NOWAIT | M_ZERO))) {
224 		printf("pst: out of memory in start\n");
225 		biofinish(request->bp, NULL, ENOMEM);
226 		iop_free_mfa(psc->iop, mfa);
227 		return;
228 	    }
229 	    callout_init_mtx(&request->timeout, &psc->iop->mtx, 0);
230 	    psc->iop->outstanding++;
231 	    request->psc = psc;
232 	    request->mfa = mfa;
233 	    request->bp = bp;
234 	    if (pst_rw(request)) {
235 		biofinish(request->bp, NULL, EIO);
236 		iop_free_mfa(request->psc->iop, request->mfa);
237 		psc->iop->outstanding--;
238 		free(request, M_PSTRAID);
239 	    }
240 	}
241     }
242 }
243 
244 static void
245 pst_done(struct iop_softc *sc, u_int32_t mfa, struct i2o_single_reply *reply)
246 {
247     struct pst_request *request =
248 	(struct pst_request *)reply->transaction_context;
249     struct pst_softc *psc = request->psc;
250 
251     callout_stop(&request->timeout);
252     request->bp->bio_resid = request->bp->bio_bcount - reply->donecount;
253     biofinish(request->bp, NULL, reply->status ? EIO : 0);
254     free(request, M_PSTRAID);
255     psc->iop->reg->oqueue = mfa;
256     psc->iop->outstanding--;
257     pst_start(psc);
258 }
259 
260 int
261 pst_rw(struct pst_request *request)
262 {
263     struct i2o_bsa_rw_block_message *msg;
264     int sgl_flag;
265 
266     msg = (struct i2o_bsa_rw_block_message *)
267 	  (request->psc->iop->ibase + request->mfa);
268     bzero(msg, sizeof(struct i2o_bsa_rw_block_message));
269     msg->version_offset = 0x81;
270     msg->message_flags = 0x0;
271     msg->message_size = sizeof(struct i2o_bsa_rw_block_message) >> 2;
272     msg->target_address = request->psc->lct->local_tid;
273     msg->initiator_address = I2O_TID_HOST;
274     switch (request->bp->bio_cmd) {
275     case BIO_READ:
276 	msg->function = I2O_BSA_BLOCK_READ;
277 	msg->control_flags = 0x0; /* 0x0c = read cache + readahead */
278 	msg->fetch_ahead = 0x0; /* 8 Kb */
279 	sgl_flag = 0;
280 	break;
281     case BIO_WRITE:
282 	msg->function = I2O_BSA_BLOCK_WRITE;
283 	msg->control_flags = 0x0; /* 0x10 = write behind cache */
284 	msg->fetch_ahead = 0x0;
285 	sgl_flag = I2O_SGL_DIR;
286 	break;
287     default:
288 	printf("pst: unknown command type 0x%02x\n", request->bp->bio_cmd);
289 	return -1;
290     }
291     msg->initiator_context = (u_int32_t)pst_done;
292     msg->transaction_context = (u_int32_t)request;
293     msg->time_multiplier = 1;
294     msg->bytecount = request->bp->bio_bcount;
295     msg->lba = ((u_int64_t)request->bp->bio_pblkno) * (DEV_BSIZE * 1LL);
296 
297     if (!iop_create_sgl((struct i2o_basic_message *)msg, request->bp->bio_data,
298 			request->bp->bio_bcount, sgl_flag))
299 	return -1;
300 
301     request->psc->iop->reg->iqueue = request->mfa;
302 
303     if (!dumping)
304 	callout_reset(&request->timeout, 10 * hz, pst_timeout, request);
305     return 0;
306 }
307 
308 static void
309 pst_timeout(void *arg)
310 {
311     struct pst_request *request;
312 
313     request = arg;
314     printf("pst: timeout mfa=0x%08x cmd=0x%02x\n",
315 	   request->mfa, request->bp->bio_cmd);
316     mtx_assert(&request->psc->iop->mtx, MA_OWNED);
317     iop_free_mfa(request->psc->iop, request->mfa);
318     if ((request->mfa = iop_get_mfa(request->psc->iop)) == 0xffffffff) {
319 	printf("pst: timeout no mfa possible\n");
320 	biofinish(request->bp, NULL, EIO);
321 	request->psc->iop->outstanding--;
322 	return;
323     }
324     if (pst_rw(request)) {
325 	iop_free_mfa(request->psc->iop, request->mfa);
326 	biofinish(request->bp, NULL, EIO);
327 	request->psc->iop->outstanding--;
328     }
329 }
330 
331 static void
332 bpack(int8_t *src, int8_t *dst, int len)
333 {
334     int i, j, blank;
335     int8_t *ptr, *buf = dst;
336 
337     for (i = j = blank = 0 ; i < len; i++) {
338 	if (blank && src[i] == ' ')
339 	    continue;
340 	if (blank && src[i] != ' ') {
341 	    dst[j++] = src[i];
342 	    blank = 0;
343 	    continue;
344 	}
345 	if (src[i] == ' ') {
346 	    blank = 1;
347 	    if (i == 0)
348 		continue;
349 	}
350 	dst[j++] = src[i];
351     }
352     if (j < len)
353 	dst[j] = 0x00;
354     for (ptr = buf; ptr < buf+len; ++ptr)
355 	if (!*ptr)
356 	    *ptr = ' ';
357     for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
358         *ptr = 0;
359 }
360 
361 static device_method_t pst_methods[] = {
362     DEVMETHOD(device_probe,	pst_probe),
363     DEVMETHOD(device_attach,	pst_attach),
364     { 0, 0 }
365 };
366 
367 static driver_t pst_driver = {
368     "pst",
369     pst_methods,
370     sizeof(struct pst_softc),
371 };
372 
373 static devclass_t pst_devclass;
374 
375 DRIVER_MODULE(pst, pstpci, pst_driver, pst_devclass, 0, 0);
376