pst-raid.c revision 111472
1272343Sngie/*-
2272343Sngie * Copyright (c) 2001,2002 S�ren Schmidt <sos@FreeBSD.org>
3272343Sngie * All rights reserved.
4272343Sngie *
5272343Sngie * Redistribution and use in source and binary forms, with or without
6272343Sngie * modification, are permitted provided that the following conditions
7272343Sngie * are met:
8272343Sngie * 1. Redistributions of source code must retain the above copyright
9272343Sngie *    notice, this list of conditions and the following disclaimer,
10272343Sngie *    without modification, immediately at the beginning of the file.
11272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
12272343Sngie *    notice, this list of conditions and the following disclaimer in the
13272343Sngie *    documentation and/or other materials provided with the distribution.
14272343Sngie * 3. The name of the author may not be used to endorse or promote products
15272343Sngie *    derived from this software without specific prior written permission.
16272343Sngie *
17272343Sngie * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18272343Sngie * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19272343Sngie * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20272343Sngie * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21272343Sngie * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22272343Sngie * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23272343Sngie * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24272343Sngie * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25272343Sngie * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26272343Sngie * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27272343Sngie *
28272343Sngie * $FreeBSD: head/sys/dev/pst/pst-raid.c 111472 2003-02-25 08:02:57Z phk $
29272343Sngie */
30272343Sngie
31272343Sngie#include <sys/param.h>
32272343Sngie#include <sys/systm.h>
33272343Sngie#include <sys/kernel.h>
34272343Sngie#include <sys/module.h>
35272343Sngie#include <sys/bus.h>
36272343Sngie#include <sys/bio.h>
37272343Sngie#include <sys/conf.h>
38276478Sngie#include <sys/disk.h>
39272343Sngie#include <sys/devicestat.h>
40276478Sngie#include <sys/eventhandler.h>
41272343Sngie#include <sys/malloc.h>
42272343Sngie#include <sys/lock.h>
43272343Sngie#include <sys/mutex.h>
44272343Sngie#include <vm/vm.h>
45272343Sngie#include <vm/pmap.h>
46272343Sngie#include <machine/stdarg.h>
47272343Sngie#include <machine/resource.h>
48272343Sngie#include <machine/bus.h>
49272343Sngie#include <sys/rman.h>
50272343Sngie#include <pci/pcivar.h>
51272343Sngie#include <pci/pcireg.h>
52272343Sngie
53272343Sngie#include "dev/pst/pst-iop.h"
54272343Sngie
55272343Sngiestruct pst_softc {
56272343Sngie    struct iop_softc		*iop;
57272343Sngie    struct i2o_lct_entry	*lct;
58272343Sngie    struct i2o_bsa_device	*info;
59    struct devstat		stats;
60    struct disk			disk;
61    struct bio_queue_head	queue;
62    struct mtx			mtx;
63    int				outstanding;
64};
65
66struct pst_request {
67    struct pst_softc		*psc;		/* pointer to softc */
68    u_int32_t			mfa;		/* frame addreess */
69    struct callout_handle	timeout_handle; /* handle for untimeout */
70    struct bio			*bp;		/* associated bio ptr */
71};
72
73/* prototypes */
74static disk_strategy_t pststrategy;
75static int pst_probe(device_t);
76static int pst_attach(device_t);
77static int pst_shutdown(device_t);
78static void pst_start(struct pst_softc *);
79static void pst_done(struct iop_softc *, u_int32_t, struct i2o_single_reply *);
80static int pst_rw(struct pst_request *);
81static void pst_timeout(struct pst_request *);
82static void bpack(int8_t *, int8_t *, int);
83
84/* local vars */
85static MALLOC_DEFINE(M_PSTRAID, "pst", "Promise SuperTrak RAID driver");
86
87int
88pst_add_raid(struct iop_softc *sc, struct i2o_lct_entry *lct)
89{
90    struct pst_softc *psc;
91    device_t child = device_add_child(sc->dev, "pst", -1);
92
93    if (!child)
94	return ENOMEM;
95    if (!(psc = malloc(sizeof(struct pst_softc),
96		       M_PSTRAID, M_NOWAIT | M_ZERO))) {
97	device_delete_child(sc->dev, child);
98	return ENOMEM;
99    }
100    psc->iop = sc;
101    psc->lct = lct;
102    device_set_softc(child, psc);
103    return bus_generic_attach(sc->dev);
104}
105
106static int
107pst_probe(device_t dev)
108{
109    device_set_desc(dev, "Promise SuperTrak RAID");
110    return 0;
111}
112
113static int
114pst_attach(device_t dev)
115{
116    struct pst_softc *psc = device_get_softc(dev);
117    struct i2o_get_param_reply *reply;
118    struct i2o_device_identity *ident;
119    int lun = device_get_unit(dev);
120    int8_t name [32];
121
122    if (!(reply = iop_get_util_params(psc->iop, psc->lct->local_tid,
123				      I2O_PARAMS_OPERATION_FIELD_GET,
124				      I2O_BSA_DEVICE_INFO_GROUP_NO)))
125	return ENODEV;
126
127    if (!(psc->info = (struct i2o_bsa_device *)
128	    malloc(sizeof(struct i2o_bsa_device), M_PSTRAID, M_NOWAIT))) {
129	contigfree(reply, PAGE_SIZE, M_PSTRAID);
130	return ENOMEM;
131    }
132    bcopy(reply->result, psc->info, sizeof(struct i2o_bsa_device));
133    contigfree(reply, PAGE_SIZE, M_PSTRAID);
134
135    if (!(reply = iop_get_util_params(psc->iop, psc->lct->local_tid,
136				      I2O_PARAMS_OPERATION_FIELD_GET,
137				      I2O_UTIL_DEVICE_IDENTITY_GROUP_NO)))
138	return ENODEV;
139    ident = (struct i2o_device_identity *)reply->result;
140#ifdef PSTDEBUG
141    printf("pst: vendor=<%.16s> product=<%.16s>\n",
142	   ident->vendor, ident->product);
143    printf("pst: description=<%.16s> revision=<%.8s>\n",
144	   ident->description, ident->revision);
145    printf("pst: capacity=%lld blocksize=%d\n",
146	   psc->info->capacity, psc->info->block_size);
147#endif
148    bpack(ident->vendor, ident->vendor, 16);
149    bpack(ident->product, ident->product, 16);
150    sprintf(name, "%s %s", ident->vendor, ident->product);
151    contigfree(reply, PAGE_SIZE, M_PSTRAID);
152
153    bioq_init(&psc->queue);
154    mtx_init(&psc->mtx, "pst lock", MTX_DEF, 0);
155
156    psc->disk.d_name = "pst";
157    psc->disk.d_strategy = pststrategy;
158    psc->disk.d_maxsize = 64 * 1024; /*I2O_SGL_MAX_SEGS * PAGE_SIZE;*/
159    psc->disk.d_drv1 = psc;
160    disk_create(lun, &psc->disk, 0, NULL, NULL);
161
162    psc->disk.d_sectorsize = psc->info->block_size;
163    psc->disk.d_mediasize = psc->info->capacity;
164    psc->disk.d_fwsectors = 63;
165    psc->disk.d_fwheads = 255;
166
167    devstat_add_entry(&psc->stats, "pst", lun, psc->info->block_size,
168		      DEVSTAT_NO_ORDERED_TAGS,
169		      DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_IDE,
170		      DEVSTAT_PRIORITY_DISK);
171
172    printf("pst%d: %lluMB <%.40s> [%lld/%d/%d] on %.16s\n", lun,
173	   (unsigned long long)psc->info->capacity / (1024 * 1024),
174	   name, psc->info->capacity/(512*255*63), 255, 63,
175	   device_get_nameunit(psc->iop->dev));
176
177    EVENTHANDLER_REGISTER(shutdown_post_sync, pst_shutdown,
178			  dev, SHUTDOWN_PRI_FIRST);
179    return 0;
180}
181
182static int
183pst_shutdown(device_t dev)
184{
185    struct pst_softc *psc = device_get_softc(dev);
186    struct i2o_bsa_cache_flush_message *msg;
187    int mfa;
188
189    mfa = iop_get_mfa(psc->iop);
190    msg = (struct i2o_bsa_cache_flush_message *)(psc->iop->ibase + mfa);
191    bzero(msg, sizeof(struct i2o_bsa_cache_flush_message));
192    msg->version_offset = 0x01;
193    msg->message_flags = 0x0;
194    msg->message_size = sizeof(struct i2o_bsa_cache_flush_message) >> 2;
195    msg->target_address = psc->lct->local_tid;
196    msg->initiator_address = I2O_TID_HOST;
197    msg->function = I2O_BSA_CACHE_FLUSH;
198    msg->control_flags = 0x0; /* 0x80 = post progress reports */
199    if (iop_queue_wait_msg(psc->iop, mfa, (struct i2o_basic_message *)msg))
200	printf("pst: shutdown failed!\n");
201    return 0;
202}
203
204static void
205pststrategy(struct bio *bp)
206{
207    struct pst_softc *psc = bp->bio_disk->d_drv1;
208
209    mtx_lock(&psc->mtx);
210    bioqdisksort(&psc->queue, bp);
211    pst_start(psc);
212    mtx_unlock(&psc->mtx);
213}
214
215static void
216pst_start(struct pst_softc *psc)
217{
218    struct pst_request *request;
219    struct bio *bp;
220    u_int32_t mfa;
221
222    if (psc->outstanding < (I2O_IOP_OUTBOUND_FRAME_COUNT - 1) &&
223	(bp = bioq_first(&psc->queue))) {
224	if ((mfa = iop_get_mfa(psc->iop)) != 0xffffffff) {
225	    if (!(request = malloc(sizeof(struct pst_request),
226				   M_PSTRAID, M_NOWAIT | M_ZERO))) {
227		printf("pst: out of memory in start\n");
228		iop_free_mfa(psc->iop, mfa);
229		return;
230	    }
231	    psc->outstanding++;
232	    request->psc = psc;
233	    request->mfa = mfa;
234	    request->bp = bp;
235	    if (dumping)
236		request->timeout_handle.callout = NULL;
237	    else
238		request->timeout_handle =
239		    timeout((timeout_t*)pst_timeout, request, 10 * hz);
240	    bioq_remove(&psc->queue, bp);
241	    devstat_start_transaction(&psc->stats);
242	    if (pst_rw(request)) {
243		biofinish(request->bp, &psc->stats, EIO);
244		iop_free_mfa(request->psc->iop, request->mfa);
245		psc->outstanding--;
246		free(request, M_PSTRAID);
247	    }
248	}
249    }
250}
251
252static void
253pst_done(struct iop_softc *sc, u_int32_t mfa, struct i2o_single_reply *reply)
254{
255    struct pst_request *request =
256	(struct pst_request *)reply->transaction_context;
257    struct pst_softc *psc = request->psc;
258
259    untimeout((timeout_t *)pst_timeout, request, request->timeout_handle);
260    request->bp->bio_resid = request->bp->bio_bcount - reply->donecount;
261    biofinish(request->bp, &psc->stats, reply->status ? EIO : 0);
262    free(request, M_PSTRAID);
263    mtx_lock(&psc->mtx);
264    psc->iop->reg->oqueue = mfa;
265    psc->outstanding--;
266    pst_start(psc);
267    mtx_unlock(&psc->mtx);
268}
269
270static void
271pst_timeout(struct pst_request *request)
272{
273    printf("pst: timeout mfa=0x%08x cmd=0x%02x\n",
274	   request->mfa, request->bp->bio_cmd);
275    mtx_lock(&request->psc->mtx);
276    iop_free_mfa(request->psc->iop, request->mfa);
277    if ((request->mfa = iop_get_mfa(request->psc->iop)) == 0xffffffff) {
278	printf("pst: timeout no mfa possible\n");
279	biofinish(request->bp, &request->psc->stats, EIO);
280	request->psc->outstanding--;
281	mtx_unlock(&request->psc->mtx);
282	return;
283    }
284    if (dumping)
285	request->timeout_handle.callout = NULL;
286    else
287	request->timeout_handle =
288	    timeout((timeout_t*)pst_timeout, request, 10 * hz);
289    if (pst_rw(request)) {
290	iop_free_mfa(request->psc->iop, request->mfa);
291	biofinish(request->bp, &request->psc->stats, EIO);
292	request->psc->outstanding--;
293    }
294    mtx_unlock(&request->psc->mtx);
295}
296
297int
298pst_rw(struct pst_request *request)
299{
300    struct i2o_bsa_rw_block_message *msg;
301    int sgl_flag;
302
303    msg = (struct i2o_bsa_rw_block_message *)
304	  (request->psc->iop->ibase + request->mfa);
305    bzero(msg, sizeof(struct i2o_bsa_rw_block_message));
306    msg->version_offset = 0x81;
307    msg->message_flags = 0x0;
308    msg->message_size = sizeof(struct i2o_bsa_rw_block_message) >> 2;
309    msg->target_address = request->psc->lct->local_tid;
310    msg->initiator_address = I2O_TID_HOST;
311    switch (request->bp->bio_cmd) {
312    case BIO_READ:
313	msg->function = I2O_BSA_BLOCK_READ;
314	msg->control_flags = 0x0; /* 0x0c = read cache + readahead */
315	msg->fetch_ahead = 0x0; /* 8 Kb */
316	sgl_flag = 0;
317	break;
318    case BIO_WRITE:
319	msg->function = I2O_BSA_BLOCK_WRITE;
320	msg->control_flags = 0x0; /* 0x10 = write behind cache */
321	msg->fetch_ahead = 0x0;
322	sgl_flag = I2O_SGL_DIR;
323	break;
324    default:
325	printf("pst: unknown command type\n");
326	return -1;
327    }
328    msg->initiator_context = (u_int32_t)pst_done;
329    msg->transaction_context = (u_int32_t)request;
330    msg->time_multiplier = 1;
331    msg->bytecount = request->bp->bio_bcount;
332    msg->lba = ((u_int64_t)request->bp->bio_pblkno) * (DEV_BSIZE * 1LL);
333    if (!iop_create_sgl((struct i2o_basic_message *)msg, request->bp->bio_data,
334			request->bp->bio_bcount, sgl_flag))
335	return -1;
336    request->psc->iop->reg->iqueue = request->mfa;
337    return 0;
338}
339
340static void
341bpack(int8_t *src, int8_t *dst, int len)
342{
343    int i, j, blank;
344    int8_t *ptr, *buf = dst;
345
346    for (i = j = blank = 0 ; i < len; i++) {
347	if (blank && src[i] == ' ') continue;
348	if (blank && src[i] != ' ') {
349	    dst[j++] = src[i];
350	    blank = 0;
351	    continue;
352	}
353	if (src[i] == ' ') {
354	    blank = 1;
355	    if (i == 0)
356		continue;
357	}
358	dst[j++] = src[i];
359    }
360    if (j < len)
361	dst[j] = 0x00;
362    for (ptr = buf; ptr < buf+len; ++ptr)
363	if (!*ptr)
364	    *ptr = ' ';
365    for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
366        *ptr = 0;
367}
368
369static device_method_t pst_methods[] = {
370    DEVMETHOD(device_probe,	pst_probe),
371    DEVMETHOD(device_attach,	pst_attach),
372    { 0, 0 }
373};
374
375static driver_t pst_driver = {
376    "pst",
377    pst_methods,
378    sizeof(struct pst_softc),
379};
380
381static devclass_t pst_devclass;
382
383DRIVER_MODULE(pst, pstpci, pst_driver, pst_devclass, 0, 0);
384