2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2001,2002,2003 Søren Schmidt <sos@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
41 #include <sys/eventhandler.h>
42 #include <sys/malloc.h>
44 #include <sys/mutex.h>
47 #include <machine/stdarg.h>
48 #include <machine/resource.h>
49 #include <machine/bus.h>
51 #include <dev/pci/pcivar.h>
52 #include <dev/pci/pcireg.h>
53 #include <geom/geom_disk.h>
55 #include "dev/pst/pst-iop.h"
58 struct iop_softc *iop;
59 struct i2o_lct_entry *lct;
60 struct i2o_bsa_device *info;
62 struct bio_queue_head queue;
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 */
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);
84 static MALLOC_DEFINE(M_PSTRAID, "pst", "Promise SuperTrak RAID driver");
87 pst_add_raid(struct iop_softc *sc, struct i2o_lct_entry *lct)
89 struct pst_softc *psc;
90 device_t child = device_add_child(sc->dev, "pst", -1);
94 if (!(psc = malloc(sizeof(struct pst_softc),
95 M_PSTRAID, M_NOWAIT | M_ZERO))) {
96 device_delete_child(sc->dev, child);
101 device_set_softc(child, psc);
102 return device_probe_and_attach(child);
106 pst_probe(device_t dev)
108 device_set_desc(dev, "Promise SuperTrak RAID");
113 pst_attach(device_t dev)
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);
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)))
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);
131 bcopy(reply->result, psc->info, sizeof(struct i2o_bsa_device));
132 contigfree(reply, PAGE_SIZE, M_PSTIOP);
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)))
138 ident = (struct i2o_device_identity *)reply->result;
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);
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);
152 bioq_init(&psc->queue);
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;
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;
166 disk_create(psc->disk, DISK_VERSION);
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));
173 EVENTHANDLER_REGISTER(shutdown_post_sync, pst_shutdown,
174 dev, SHUTDOWN_PRI_FIRST);
179 pst_shutdown(device_t dev)
181 struct pst_softc *psc = device_get_softc(dev);
182 struct i2o_bsa_cache_flush_message *msg;
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");
201 pststrategy(struct bio *bp)
203 struct pst_softc *psc = bp->bio_disk->d_drv1;
205 mtx_lock(&psc->iop->mtx);
206 bioq_disksort(&psc->queue, bp);
208 mtx_unlock(&psc->iop->mtx);
212 pst_start(struct pst_softc *psc)
214 struct pst_request *request;
218 if (psc->iop->outstanding < (I2O_IOP_OUTBOUND_FRAME_COUNT - 1) &&
219 (bp = bioq_first(&psc->queue))) {
220 if ((mfa = iop_get_mfa(psc->iop)) != 0xffffffff) {
221 bioq_remove(&psc->queue, bp);
222 if (!(request = malloc(sizeof(struct pst_request),
223 M_PSTRAID, M_NOWAIT | M_ZERO))) {
224 printf("pst: out of memory in start\n");
225 biofinish(request->bp, NULL, ENOMEM);
226 iop_free_mfa(psc->iop, mfa);
229 callout_init_mtx(&request->timeout, &psc->iop->mtx, 0);
230 psc->iop->outstanding++;
234 if (pst_rw(request)) {
235 biofinish(request->bp, NULL, EIO);
236 iop_free_mfa(request->psc->iop, request->mfa);
237 psc->iop->outstanding--;
238 free(request, M_PSTRAID);
245 pst_done(struct iop_softc *sc, u_int32_t mfa, struct i2o_single_reply *reply)
247 struct pst_request *request =
248 (struct pst_request *)reply->transaction_context;
249 struct pst_softc *psc = request->psc;
251 callout_stop(&request->timeout);
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 psc->iop->reg->oqueue = mfa;
256 psc->iop->outstanding--;
261 pst_rw(struct pst_request *request)
263 struct i2o_bsa_rw_block_message *msg;
266 msg = (struct i2o_bsa_rw_block_message *)
267 (request->psc->iop->ibase + request->mfa);
268 bzero(msg, sizeof(struct i2o_bsa_rw_block_message));
269 msg->version_offset = 0x81;
270 msg->message_flags = 0x0;
271 msg->message_size = sizeof(struct i2o_bsa_rw_block_message) >> 2;
272 msg->target_address = request->psc->lct->local_tid;
273 msg->initiator_address = I2O_TID_HOST;
274 switch (request->bp->bio_cmd) {
276 msg->function = I2O_BSA_BLOCK_READ;
277 msg->control_flags = 0x0; /* 0x0c = read cache + readahead */
278 msg->fetch_ahead = 0x0; /* 8 Kb */
282 msg->function = I2O_BSA_BLOCK_WRITE;
283 msg->control_flags = 0x0; /* 0x10 = write behind cache */
284 msg->fetch_ahead = 0x0;
285 sgl_flag = I2O_SGL_DIR;
288 printf("pst: unknown command type 0x%02x\n", request->bp->bio_cmd);
291 msg->initiator_context = (u_int32_t)pst_done;
292 msg->transaction_context = (u_int32_t)request;
293 msg->time_multiplier = 1;
294 msg->bytecount = request->bp->bio_bcount;
295 msg->lba = ((u_int64_t)request->bp->bio_pblkno) * (DEV_BSIZE * 1LL);
297 if (!iop_create_sgl((struct i2o_basic_message *)msg, request->bp->bio_data,
298 request->bp->bio_bcount, sgl_flag))
301 request->psc->iop->reg->iqueue = request->mfa;
304 callout_reset(&request->timeout, 10 * hz, pst_timeout, request);
309 pst_timeout(void *arg)
311 struct pst_request *request;
314 printf("pst: timeout mfa=0x%08x cmd=0x%02x\n",
315 request->mfa, request->bp->bio_cmd);
316 mtx_assert(&request->psc->iop->mtx, MA_OWNED);
317 iop_free_mfa(request->psc->iop, request->mfa);
318 if ((request->mfa = iop_get_mfa(request->psc->iop)) == 0xffffffff) {
319 printf("pst: timeout no mfa possible\n");
320 biofinish(request->bp, NULL, EIO);
321 request->psc->iop->outstanding--;
324 if (pst_rw(request)) {
325 iop_free_mfa(request->psc->iop, request->mfa);
326 biofinish(request->bp, NULL, EIO);
327 request->psc->iop->outstanding--;
332 bpack(int8_t *src, int8_t *dst, int len)
335 int8_t *ptr, *buf = dst;
337 for (i = j = blank = 0 ; i < len; i++) {
338 if (blank && src[i] == ' ')
340 if (blank && src[i] != ' ') {
354 for (ptr = buf; ptr < buf+len; ++ptr)
357 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
361 static device_method_t pst_methods[] = {
362 DEVMETHOD(device_probe, pst_probe),
363 DEVMETHOD(device_attach, pst_attach),
367 static driver_t pst_driver = {
370 sizeof(struct pst_softc),
373 static devclass_t pst_devclass;
375 DRIVER_MODULE(pst, pstpci, pst_driver, pst_devclass, 0, 0);