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