pst-raid.c revision 112977
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: head/sys/dev/pst/pst-raid.c 112977 2003-04-02 11:36:43Z sos $ 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 <pci/pcivar.h> 49#include <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 struct mtx mtx; 61 int outstanding; 62}; 63 64struct 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 */ 72static disk_strategy_t pststrategy; 73static int pst_probe(device_t); 74static int pst_attach(device_t); 75static int pst_shutdown(device_t); 76static void pst_start(struct pst_softc *); 77static void pst_done(struct iop_softc *, u_int32_t, struct i2o_single_reply *); 78static int pst_rw(struct pst_request *); 79static void pst_timeout(struct pst_request *); 80static void bpack(int8_t *, int8_t *, int); 81 82/* local vars */ 83static MALLOC_DEFINE(M_PSTRAID, "pst", "Promise SuperTrak RAID driver"); 84 85int 86pst_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 104static int 105pst_probe(device_t dev) 106{ 107 device_set_desc(dev, "Promise SuperTrak RAID"); 108 return 0; 109} 110 111static int 112pst_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 175static int 176pst_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 197static void 198pststrategy(struct bio *bp) 199{ 200 struct pst_softc *psc = bp->bio_disk->d_drv1; 201 202 mtx_lock(&psc->mtx); 203 bioq_disksort(&psc->queue, bp); 204 pst_start(psc); 205 mtx_unlock(&psc->mtx); 206} 207 208static void 209pst_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 bioq_remove(&psc->queue, bp); 219 if (!(request = malloc(sizeof(struct pst_request), 220 M_PSTRAID, M_NOWAIT | M_ZERO))) { 221 printf("pst: out of memory in start\n"); 222 biofinish(request->bp, NULL, ENOMEM); 223 iop_free_mfa(psc->iop, mfa); 224 return; 225 } 226 psc->outstanding++; 227 request->psc = psc; 228 request->mfa = mfa; 229 request->bp = bp; 230 if (dumping) 231 request->timeout_handle.callout = NULL; 232 else 233 request->timeout_handle = 234 timeout((timeout_t*)pst_timeout, request, 10 * hz); 235 if (pst_rw(request)) { 236 biofinish(request->bp, NULL, EIO); 237 iop_free_mfa(request->psc->iop, request->mfa); 238 psc->outstanding--; 239 free(request, M_PSTRAID); 240 } 241 } 242 } 243} 244 245static void 246pst_done(struct iop_softc *sc, u_int32_t mfa, struct i2o_single_reply *reply) 247{ 248 struct pst_request *request = 249 (struct pst_request *)reply->transaction_context; 250 struct pst_softc *psc = request->psc; 251 252 untimeout((timeout_t *)pst_timeout, request, request->timeout_handle); 253 request->bp->bio_resid = request->bp->bio_bcount - reply->donecount; 254 biofinish(request->bp, NULL, reply->status ? EIO : 0); 255 free(request, M_PSTRAID); 256 mtx_lock(&psc->mtx); 257 psc->iop->reg->oqueue = mfa; 258 psc->outstanding--; 259 pst_start(psc); 260 mtx_unlock(&psc->mtx); 261} 262 263static void 264pst_timeout(struct pst_request *request) 265{ 266 printf("pst: timeout mfa=0x%08x cmd=0x%02x\n", 267 request->mfa, request->bp->bio_cmd); 268 mtx_lock(&request->psc->mtx); 269 iop_free_mfa(request->psc->iop, request->mfa); 270 if ((request->mfa = iop_get_mfa(request->psc->iop)) == 0xffffffff) { 271 printf("pst: timeout no mfa possible\n"); 272 biofinish(request->bp, NULL, EIO); 273 request->psc->outstanding--; 274 mtx_unlock(&request->psc->mtx); 275 return; 276 } 277 if (dumping) 278 request->timeout_handle.callout = NULL; 279 else 280 request->timeout_handle = 281 timeout((timeout_t*)pst_timeout, request, 10 * hz); 282 if (pst_rw(request)) { 283 iop_free_mfa(request->psc->iop, request->mfa); 284 biofinish(request->bp, NULL, EIO); 285 request->psc->outstanding--; 286 } 287 mtx_unlock(&request->psc->mtx); 288} 289 290int 291pst_rw(struct pst_request *request) 292{ 293 struct i2o_bsa_rw_block_message *msg; 294 int sgl_flag; 295 296 msg = (struct i2o_bsa_rw_block_message *) 297 (request->psc->iop->ibase + request->mfa); 298 bzero(msg, sizeof(struct i2o_bsa_rw_block_message)); 299 msg->version_offset = 0x81; 300 msg->message_flags = 0x0; 301 msg->message_size = sizeof(struct i2o_bsa_rw_block_message) >> 2; 302 msg->target_address = request->psc->lct->local_tid; 303 msg->initiator_address = I2O_TID_HOST; 304 switch (request->bp->bio_cmd) { 305 case BIO_READ: 306 msg->function = I2O_BSA_BLOCK_READ; 307 msg->control_flags = 0x0; /* 0x0c = read cache + readahead */ 308 msg->fetch_ahead = 0x0; /* 8 Kb */ 309 sgl_flag = 0; 310 break; 311 case BIO_WRITE: 312 msg->function = I2O_BSA_BLOCK_WRITE; 313 msg->control_flags = 0x0; /* 0x10 = write behind cache */ 314 msg->fetch_ahead = 0x0; 315 sgl_flag = I2O_SGL_DIR; 316 break; 317 default: 318 printf("pst: unknown command type\n"); 319 return -1; 320 } 321 msg->initiator_context = (u_int32_t)pst_done; 322 msg->transaction_context = (u_int32_t)request; 323 msg->time_multiplier = 1; 324 msg->bytecount = request->bp->bio_bcount; 325 msg->lba = ((u_int64_t)request->bp->bio_pblkno) * (DEV_BSIZE * 1LL); 326 if (!iop_create_sgl((struct i2o_basic_message *)msg, request->bp->bio_data, 327 request->bp->bio_bcount, sgl_flag)) 328 return -1; 329 request->psc->iop->reg->iqueue = request->mfa; 330 return 0; 331} 332 333static void 334bpack(int8_t *src, int8_t *dst, int len) 335{ 336 int i, j, blank; 337 int8_t *ptr, *buf = dst; 338 339 for (i = j = blank = 0 ; i < len; i++) { 340 if (blank && src[i] == ' ') continue; 341 if (blank && src[i] != ' ') { 342 dst[j++] = src[i]; 343 blank = 0; 344 continue; 345 } 346 if (src[i] == ' ') { 347 blank = 1; 348 if (i == 0) 349 continue; 350 } 351 dst[j++] = src[i]; 352 } 353 if (j < len) 354 dst[j] = 0x00; 355 for (ptr = buf; ptr < buf+len; ++ptr) 356 if (!*ptr) 357 *ptr = ' '; 358 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr) 359 *ptr = 0; 360} 361 362static device_method_t pst_methods[] = { 363 DEVMETHOD(device_probe, pst_probe), 364 DEVMETHOD(device_attach, pst_attach), 365 { 0, 0 } 366}; 367 368static driver_t pst_driver = { 369 "pst", 370 pst_methods, 371 sizeof(struct pst_softc), 372}; 373 374static devclass_t pst_devclass; 375 376DRIVER_MODULE(pst, pstpci, pst_driver, pst_devclass, 0, 0); 377