pst-raid.c revision 125975
1217309Snwhitehorn/*-
2251843Sbapt * Copyright (c) 2001,2002,2003 S�ren Schmidt <sos@FreeBSD.org>
3217309Snwhitehorn * All rights reserved.
4217309Snwhitehorn *
5217309Snwhitehorn * Redistribution and use in source and binary forms, with or without
6217309Snwhitehorn * modification, are permitted provided that the following conditions
7217309Snwhitehorn * are met:
8217309Snwhitehorn * 1. Redistributions of source code must retain the above copyright
9217309Snwhitehorn *    notice, this list of conditions and the following disclaimer,
10217309Snwhitehorn *    without modification, immediately at the beginning of the file.
11217309Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright
12217309Snwhitehorn *    notice, this list of conditions and the following disclaimer in the
13217309Snwhitehorn *    documentation and/or other materials provided with the distribution.
14217309Snwhitehorn * 3. The name of the author may not be used to endorse or promote products
15217309Snwhitehorn *    derived from this software without specific prior written permission.
16217309Snwhitehorn *
17217309Snwhitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18217309Snwhitehorn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19217309Snwhitehorn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20217309Snwhitehorn * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21217309Snwhitehorn * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22217309Snwhitehorn * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23217309Snwhitehorn * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24217309Snwhitehorn * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25217309Snwhitehorn * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26217309Snwhitehorn * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27217309Snwhitehorn */
28217309Snwhitehorn
29217309Snwhitehorn#include <sys/cdefs.h>
30217309Snwhitehorn__FBSDID("$FreeBSD: head/sys/dev/pst/pst-raid.c 125975 2004-02-18 21:36:53Z phk $");
31217309Snwhitehorn
32217309Snwhitehorn#include <sys/param.h>
33217309Snwhitehorn#include <sys/systm.h>
34217309Snwhitehorn#include <sys/kernel.h>
35217309Snwhitehorn#include <sys/module.h>
36217309Snwhitehorn#include <sys/bus.h>
37217309Snwhitehorn#include <sys/bio.h>
38217309Snwhitehorn#include <sys/conf.h>
39217309Snwhitehorn#include <sys/eventhandler.h>
40217309Snwhitehorn#include <sys/malloc.h>
41217309Snwhitehorn#include <sys/lock.h>
42217309Snwhitehorn#include <sys/mutex.h>
43217309Snwhitehorn#include <vm/vm.h>
44217309Snwhitehorn#include <vm/pmap.h>
45217309Snwhitehorn#include <machine/stdarg.h>
46217309Snwhitehorn#include <machine/resource.h>
47217309Snwhitehorn#include <machine/bus.h>
48217309Snwhitehorn#include <sys/rman.h>
49217309Snwhitehorn#include <dev/pci/pcivar.h>
50217309Snwhitehorn#include <dev/pci/pcireg.h>
51217309Snwhitehorn#include <geom/geom_disk.h>
52217309Snwhitehorn
53217309Snwhitehorn#include "dev/pst/pst-iop.h"
54217309Snwhitehorn
55217309Snwhitehornstruct pst_softc {
56217309Snwhitehorn    struct iop_softc		*iop;
57217309Snwhitehorn    struct i2o_lct_entry	*lct;
58217309Snwhitehorn    struct i2o_bsa_device	*info;
59217309Snwhitehorn    struct disk			*disk;
60217309Snwhitehorn    struct bio_queue_head	queue;
61217309Snwhitehorn};
62217309Snwhitehorn
63217309Snwhitehornstruct pst_request {
64217309Snwhitehorn    struct pst_softc		*psc;		/* pointer to softc */
65217309Snwhitehorn    u_int32_t			mfa;		/* frame addreess */
66217309Snwhitehorn    struct callout_handle	timeout_handle; /* handle for untimeout */
67217309Snwhitehorn    struct bio			*bp;		/* associated bio ptr */
68217309Snwhitehorn};
69217309Snwhitehorn
70217309Snwhitehorn/* prototypes */
71217309Snwhitehornstatic disk_strategy_t pststrategy;
72217309Snwhitehornstatic int pst_probe(device_t);
73217309Snwhitehornstatic int pst_attach(device_t);
74217309Snwhitehornstatic int pst_shutdown(device_t);
75217309Snwhitehornstatic void pst_start(struct pst_softc *);
76217309Snwhitehornstatic void pst_done(struct iop_softc *, u_int32_t, struct i2o_single_reply *);
77217309Snwhitehornstatic int pst_rw(struct pst_request *);
78251843Sbaptstatic void pst_timeout(struct pst_request *);
79217309Snwhitehornstatic void bpack(int8_t *, int8_t *, int);
80217309Snwhitehorn
81217309Snwhitehorn/* local vars */
82217309Snwhitehornstatic MALLOC_DEFINE(M_PSTRAID, "pst", "Promise SuperTrak RAID driver");
83217309Snwhitehorn
84217309Snwhitehornint
85217309Snwhitehornpst_add_raid(struct iop_softc *sc, struct i2o_lct_entry *lct)
86217309Snwhitehorn{
87217309Snwhitehorn    struct pst_softc *psc;
88217309Snwhitehorn    device_t child = device_add_child(sc->dev, "pst", -1);
89217309Snwhitehorn
90217309Snwhitehorn    if (!child)
91217309Snwhitehorn	return ENOMEM;
92217309Snwhitehorn    if (!(psc = malloc(sizeof(struct pst_softc),
93217309Snwhitehorn		       M_PSTRAID, M_NOWAIT | M_ZERO))) {
94217309Snwhitehorn	device_delete_child(sc->dev, child);
95217309Snwhitehorn	return ENOMEM;
96217309Snwhitehorn    }
97217309Snwhitehorn    psc->iop = sc;
98217309Snwhitehorn    psc->lct = lct;
99217309Snwhitehorn    device_set_softc(child, psc);
100217309Snwhitehorn    return bus_generic_attach(sc->dev);
101217309Snwhitehorn}
102217309Snwhitehorn
103217309Snwhitehornstatic int
104217309Snwhitehornpst_probe(device_t dev)
105217309Snwhitehorn{
106217309Snwhitehorn    device_set_desc(dev, "Promise SuperTrak RAID");
107    return 0;
108}
109
110static int
111pst_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_PSTRAID);
127	return ENOMEM;
128    }
129    bcopy(reply->result, psc->info, sizeof(struct i2o_bsa_device));
130    contigfree(reply, PAGE_SIZE, M_PSTRAID);
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_PSTRAID);
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
176static int
177pst_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
198static void
199pststrategy(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
209static void
210pst_start(struct pst_softc *psc)
211{
212    struct pst_request *request;
213    struct bio *bp;
214    u_int32_t mfa;
215
216    if (psc->iop->outstanding < (I2O_IOP_OUTBOUND_FRAME_COUNT - 1) &&
217	(bp = bioq_first(&psc->queue))) {
218	if ((mfa = iop_get_mfa(psc->iop)) != 0xffffffff) {
219	    bioq_remove(&psc->queue, bp);
220	    if (!(request = malloc(sizeof(struct pst_request),
221				   M_PSTRAID, M_NOWAIT | M_ZERO))) {
222		printf("pst: out of memory in start\n");
223		biofinish(request->bp, NULL, ENOMEM);
224		iop_free_mfa(psc->iop, mfa);
225		return;
226	    }
227	    psc->iop->outstanding++;
228	    request->psc = psc;
229	    request->mfa = mfa;
230	    request->bp = bp;
231	    if (pst_rw(request)) {
232		biofinish(request->bp, NULL, EIO);
233		iop_free_mfa(request->psc->iop, request->mfa);
234		psc->iop->outstanding--;
235		free(request, M_PSTRAID);
236	    }
237	}
238    }
239}
240
241static void
242pst_done(struct iop_softc *sc, u_int32_t mfa, struct i2o_single_reply *reply)
243{
244    struct pst_request *request =
245	(struct pst_request *)reply->transaction_context;
246    struct pst_softc *psc = request->psc;
247
248    untimeout((timeout_t *)pst_timeout, request, request->timeout_handle);
249    request->bp->bio_resid = request->bp->bio_bcount - reply->donecount;
250    biofinish(request->bp, NULL, reply->status ? EIO : 0);
251    free(request, M_PSTRAID);
252    psc->iop->reg->oqueue = mfa;
253    psc->iop->outstanding--;
254    pst_start(psc);
255}
256
257int
258pst_rw(struct pst_request *request)
259{
260    struct i2o_bsa_rw_block_message *msg;
261    int sgl_flag;
262
263    msg = (struct i2o_bsa_rw_block_message *)
264	  (request->psc->iop->ibase + request->mfa);
265    bzero(msg, sizeof(struct i2o_bsa_rw_block_message));
266    msg->version_offset = 0x81;
267    msg->message_flags = 0x0;
268    msg->message_size = sizeof(struct i2o_bsa_rw_block_message) >> 2;
269    msg->target_address = request->psc->lct->local_tid;
270    msg->initiator_address = I2O_TID_HOST;
271    switch (request->bp->bio_cmd) {
272    case BIO_READ:
273	msg->function = I2O_BSA_BLOCK_READ;
274	msg->control_flags = 0x0; /* 0x0c = read cache + readahead */
275	msg->fetch_ahead = 0x0; /* 8 Kb */
276	sgl_flag = 0;
277	break;
278    case BIO_WRITE:
279	msg->function = I2O_BSA_BLOCK_WRITE;
280	msg->control_flags = 0x0; /* 0x10 = write behind cache */
281	msg->fetch_ahead = 0x0;
282	sgl_flag = I2O_SGL_DIR;
283	break;
284    default:
285	printf("pst: unknown command type 0x%02x\n", request->bp->bio_cmd);
286	return -1;
287    }
288    msg->initiator_context = (u_int32_t)pst_done;
289    msg->transaction_context = (u_int32_t)request;
290    msg->time_multiplier = 1;
291    msg->bytecount = request->bp->bio_bcount;
292    msg->lba = ((u_int64_t)request->bp->bio_pblkno) * (DEV_BSIZE * 1LL);
293
294    if (!iop_create_sgl((struct i2o_basic_message *)msg, request->bp->bio_data,
295			request->bp->bio_bcount, sgl_flag))
296	return -1;
297
298    request->psc->iop->reg->iqueue = request->mfa;
299
300    if (dumping)
301	request->timeout_handle.callout = NULL;
302    else
303	request->timeout_handle =
304	    timeout((timeout_t*)pst_timeout, request, 10 * hz);
305    return 0;
306}
307
308static void
309pst_timeout(struct pst_request *request)
310{
311    printf("pst: timeout mfa=0x%08x cmd=0x%02x\n",
312	   request->mfa, request->bp->bio_cmd);
313    mtx_lock(&request->psc->iop->mtx);
314    iop_free_mfa(request->psc->iop, request->mfa);
315    if ((request->mfa = iop_get_mfa(request->psc->iop)) == 0xffffffff) {
316	printf("pst: timeout no mfa possible\n");
317	biofinish(request->bp, NULL, EIO);
318	request->psc->iop->outstanding--;
319	mtx_unlock(&request->psc->iop->mtx);
320	return;
321    }
322    if (pst_rw(request)) {
323	iop_free_mfa(request->psc->iop, request->mfa);
324	biofinish(request->bp, NULL, EIO);
325	request->psc->iop->outstanding--;
326    }
327    mtx_unlock(&request->psc->iop->mtx);
328}
329
330static void
331bpack(int8_t *src, int8_t *dst, int len)
332{
333    int i, j, blank;
334    int8_t *ptr, *buf = dst;
335
336    for (i = j = blank = 0 ; i < len; i++) {
337	if (blank && src[i] == ' ')
338	    continue;
339	if (blank && src[i] != ' ') {
340	    dst[j++] = src[i];
341	    blank = 0;
342	    continue;
343	}
344	if (src[i] == ' ') {
345	    blank = 1;
346	    if (i == 0)
347		continue;
348	}
349	dst[j++] = src[i];
350    }
351    if (j < len)
352	dst[j] = 0x00;
353    for (ptr = buf; ptr < buf+len; ++ptr)
354	if (!*ptr)
355	    *ptr = ' ';
356    for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
357        *ptr = 0;
358}
359
360static device_method_t pst_methods[] = {
361    DEVMETHOD(device_probe,	pst_probe),
362    DEVMETHOD(device_attach,	pst_attach),
363    { 0, 0 }
364};
365
366static driver_t pst_driver = {
367    "pst",
368    pst_methods,
369    sizeof(struct pst_softc),
370};
371
372static devclass_t pst_devclass;
373
374DRIVER_MODULE(pst, pstpci, pst_driver, pst_devclass, 0, 0);
375