xref: /freebsd/sys/dev/pst/pst-raid.c (revision 734e82fe33aa764367791a7d603b383996c6b40b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #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     int error;
216 
217     if (psc->iop->outstanding < (I2O_IOP_OUTBOUND_FRAME_COUNT - 1) &&
218 	(bp = bioq_first(&psc->queue))) {
219 	if ((mfa = iop_get_mfa(psc->iop)) != 0xffffffff) {
220 	    bioq_remove(&psc->queue, bp);
221 	    if (!(request = malloc(sizeof(struct pst_request),
222 				   M_PSTRAID, M_NOWAIT | M_ZERO))) {
223 		printf("pst: out of memory in start\n");
224 		biofinish(request->bp, NULL, ENOMEM);
225 		iop_free_mfa(psc->iop, mfa);
226 		return;
227 	    }
228 	    callout_init_mtx(&request->timeout, &psc->iop->mtx, 0);
229 	    psc->iop->outstanding++;
230 	    request->psc = psc;
231 	    request->mfa = mfa;
232 	    request->bp = bp;
233 	    if ((error = pst_rw(request)) != 0) {
234 		biofinish(request->bp, NULL, error);
235 		iop_free_mfa(request->psc->iop, request->mfa);
236 		psc->iop->outstanding--;
237 		free(request, M_PSTRAID);
238 	    }
239 	}
240     }
241 }
242 
243 static void
244 pst_done(struct iop_softc *sc, u_int32_t mfa, struct i2o_single_reply *reply)
245 {
246     struct pst_request *request =
247 	(struct pst_request *)reply->transaction_context;
248     struct pst_softc *psc = request->psc;
249 
250     callout_stop(&request->timeout);
251     request->bp->bio_resid = request->bp->bio_bcount - reply->donecount;
252     biofinish(request->bp, NULL, reply->status ? EIO : 0);
253     free(request, M_PSTRAID);
254     psc->iop->reg->oqueue = mfa;
255     psc->iop->outstanding--;
256     pst_start(psc);
257 }
258 
259 int
260 pst_rw(struct pst_request *request)
261 {
262     struct i2o_bsa_rw_block_message *msg;
263     int sgl_flag;
264 
265     msg = (struct i2o_bsa_rw_block_message *)
266 	  (request->psc->iop->ibase + request->mfa);
267     bzero(msg, sizeof(struct i2o_bsa_rw_block_message));
268     msg->version_offset = 0x81;
269     msg->message_flags = 0x0;
270     msg->message_size = sizeof(struct i2o_bsa_rw_block_message) >> 2;
271     msg->target_address = request->psc->lct->local_tid;
272     msg->initiator_address = I2O_TID_HOST;
273     switch (request->bp->bio_cmd) {
274     case BIO_READ:
275 	msg->function = I2O_BSA_BLOCK_READ;
276 	msg->control_flags = 0x0; /* 0x0c = read cache + readahead */
277 	msg->fetch_ahead = 0x0; /* 8 Kb */
278 	sgl_flag = 0;
279 	break;
280     case BIO_WRITE:
281 	msg->function = I2O_BSA_BLOCK_WRITE;
282 	msg->control_flags = 0x0; /* 0x10 = write behind cache */
283 	msg->fetch_ahead = 0x0;
284 	sgl_flag = I2O_SGL_DIR;
285 	break;
286     default:
287 	printf("pst: unknown command type 0x%02x\n", request->bp->bio_cmd);
288 	return EOPNOTSUPP;
289     }
290     msg->initiator_context = (u_int32_t)pst_done;
291     msg->transaction_context = (u_int32_t)request;
292     msg->time_multiplier = 1;
293     msg->bytecount = request->bp->bio_bcount;
294     msg->lba = ((u_int64_t)request->bp->bio_pblkno) * (DEV_BSIZE * 1LL);
295 
296     if (!iop_create_sgl((struct i2o_basic_message *)msg, request->bp->bio_data,
297 			request->bp->bio_bcount, sgl_flag))
298 	return EIO;
299 
300     request->psc->iop->reg->iqueue = request->mfa;
301 
302     if (!dumping)
303 	callout_reset(&request->timeout, 10 * hz, pst_timeout, request);
304     return 0;
305 }
306 
307 static void
308 pst_timeout(void *arg)
309 {
310     struct pst_request *request;
311     int error;
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 ((error = pst_rw(request)) != 0) {
325 	iop_free_mfa(request->psc->iop, request->mfa);
326 	biofinish(request->bp, NULL, error);
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 DRIVER_MODULE(pst, pstpci, pst_driver, 0, 0);
374