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