xref: /freebsd/sys/dev/pst/pst-raid.c (revision 62cfcf62f627e5093fb37026a6d8c98e4d2ef04c)
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     int error;
218 
219     if (psc->iop->outstanding < (I2O_IOP_OUTBOUND_FRAME_COUNT - 1) &&
220 	(bp = bioq_first(&psc->queue))) {
221 	if ((mfa = iop_get_mfa(psc->iop)) != 0xffffffff) {
222 	    bioq_remove(&psc->queue, bp);
223 	    if (!(request = malloc(sizeof(struct pst_request),
224 				   M_PSTRAID, M_NOWAIT | M_ZERO))) {
225 		printf("pst: out of memory in start\n");
226 		biofinish(request->bp, NULL, ENOMEM);
227 		iop_free_mfa(psc->iop, mfa);
228 		return;
229 	    }
230 	    callout_init_mtx(&request->timeout, &psc->iop->mtx, 0);
231 	    psc->iop->outstanding++;
232 	    request->psc = psc;
233 	    request->mfa = mfa;
234 	    request->bp = bp;
235 	    if ((error = pst_rw(request)) != 0) {
236 		biofinish(request->bp, NULL, error);
237 		iop_free_mfa(request->psc->iop, request->mfa);
238 		psc->iop->outstanding--;
239 		free(request, M_PSTRAID);
240 	    }
241 	}
242     }
243 }
244 
245 static void
246 pst_done(struct iop_softc *sc, u_int32_t mfa, struct i2o_single_reply *reply)
247 {
248     struct pst_request *request =
249 	(struct pst_request *)reply->transaction_context;
250     struct pst_softc *psc = request->psc;
251 
252     callout_stop(&request->timeout);
253     request->bp->bio_resid = request->bp->bio_bcount - reply->donecount;
254     biofinish(request->bp, NULL, reply->status ? EIO : 0);
255     free(request, M_PSTRAID);
256     psc->iop->reg->oqueue = mfa;
257     psc->iop->outstanding--;
258     pst_start(psc);
259 }
260 
261 int
262 pst_rw(struct pst_request *request)
263 {
264     struct i2o_bsa_rw_block_message *msg;
265     int sgl_flag;
266 
267     msg = (struct i2o_bsa_rw_block_message *)
268 	  (request->psc->iop->ibase + request->mfa);
269     bzero(msg, sizeof(struct i2o_bsa_rw_block_message));
270     msg->version_offset = 0x81;
271     msg->message_flags = 0x0;
272     msg->message_size = sizeof(struct i2o_bsa_rw_block_message) >> 2;
273     msg->target_address = request->psc->lct->local_tid;
274     msg->initiator_address = I2O_TID_HOST;
275     switch (request->bp->bio_cmd) {
276     case BIO_READ:
277 	msg->function = I2O_BSA_BLOCK_READ;
278 	msg->control_flags = 0x0; /* 0x0c = read cache + readahead */
279 	msg->fetch_ahead = 0x0; /* 8 Kb */
280 	sgl_flag = 0;
281 	break;
282     case BIO_WRITE:
283 	msg->function = I2O_BSA_BLOCK_WRITE;
284 	msg->control_flags = 0x0; /* 0x10 = write behind cache */
285 	msg->fetch_ahead = 0x0;
286 	sgl_flag = I2O_SGL_DIR;
287 	break;
288     default:
289 	printf("pst: unknown command type 0x%02x\n", request->bp->bio_cmd);
290 	return EOPNOTSUPP;
291     }
292     msg->initiator_context = (u_int32_t)pst_done;
293     msg->transaction_context = (u_int32_t)request;
294     msg->time_multiplier = 1;
295     msg->bytecount = request->bp->bio_bcount;
296     msg->lba = ((u_int64_t)request->bp->bio_pblkno) * (DEV_BSIZE * 1LL);
297 
298     if (!iop_create_sgl((struct i2o_basic_message *)msg, request->bp->bio_data,
299 			request->bp->bio_bcount, sgl_flag))
300 	return EIO;
301 
302     request->psc->iop->reg->iqueue = request->mfa;
303 
304     if (!dumping)
305 	callout_reset(&request->timeout, 10 * hz, pst_timeout, request);
306     return 0;
307 }
308 
309 static void
310 pst_timeout(void *arg)
311 {
312     struct pst_request *request;
313     int error;
314 
315     request = arg;
316     printf("pst: timeout mfa=0x%08x cmd=0x%02x\n",
317 	   request->mfa, request->bp->bio_cmd);
318     mtx_assert(&request->psc->iop->mtx, MA_OWNED);
319     iop_free_mfa(request->psc->iop, request->mfa);
320     if ((request->mfa = iop_get_mfa(request->psc->iop)) == 0xffffffff) {
321 	printf("pst: timeout no mfa possible\n");
322 	biofinish(request->bp, NULL, EIO);
323 	request->psc->iop->outstanding--;
324 	return;
325     }
326     if ((error = pst_rw(request)) != 0) {
327 	iop_free_mfa(request->psc->iop, request->mfa);
328 	biofinish(request->bp, NULL, error);
329 	request->psc->iop->outstanding--;
330     }
331 }
332 
333 static void
334 bpack(int8_t *src, int8_t *dst, int len)
335 {
336     int i, j, blank;
337     int8_t *ptr, *buf = dst;
338 
339     for (i = j = blank = 0 ; i < len; i++) {
340 	if (blank && src[i] == ' ')
341 	    continue;
342 	if (blank && src[i] != ' ') {
343 	    dst[j++] = src[i];
344 	    blank = 0;
345 	    continue;
346 	}
347 	if (src[i] == ' ') {
348 	    blank = 1;
349 	    if (i == 0)
350 		continue;
351 	}
352 	dst[j++] = src[i];
353     }
354     if (j < len)
355 	dst[j] = 0x00;
356     for (ptr = buf; ptr < buf+len; ++ptr)
357 	if (!*ptr)
358 	    *ptr = ' ';
359     for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
360         *ptr = 0;
361 }
362 
363 static device_method_t pst_methods[] = {
364     DEVMETHOD(device_probe,	pst_probe),
365     DEVMETHOD(device_attach,	pst_attach),
366     { 0, 0 }
367 };
368 
369 static driver_t pst_driver = {
370     "pst",
371     pst_methods,
372     sizeof(struct pst_softc),
373 };
374 
375 static devclass_t pst_devclass;
376 
377 DRIVER_MODULE(pst, pstpci, pst_driver, pst_devclass, 0, 0);
378