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 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/kernel.h> 35 #include <sys/module.h> 36 #include <sys/bus.h> 37 #include <sys/bio.h> 38 #include <sys/conf.h> 39 #include <sys/eventhandler.h> 40 #include <sys/malloc.h> 41 #include <sys/lock.h> 42 #include <sys/mutex.h> 43 #include <vm/vm.h> 44 #include <vm/pmap.h> 45 #include <machine/stdarg.h> 46 #include <machine/resource.h> 47 #include <machine/bus.h> 48 #include <sys/rman.h> 49 #include <dev/pci/pcivar.h> 50 #include <dev/pci/pcireg.h> 51 #include <geom/geom_disk.h> 52 53 #include "dev/pst/pst-iop.h" 54 55 struct pst_softc { 56 struct iop_softc *iop; 57 struct i2o_lct_entry *lct; 58 struct i2o_bsa_device *info; 59 struct disk disk; 60 struct bio_queue_head queue; 61 }; 62 63 struct pst_request { 64 struct pst_softc *psc; /* pointer to softc */ 65 u_int32_t mfa; /* frame addreess */ 66 struct callout_handle timeout_handle; /* handle for untimeout */ 67 struct bio *bp; /* associated bio ptr */ 68 }; 69 70 /* prototypes */ 71 static disk_strategy_t pststrategy; 72 static int pst_probe(device_t); 73 static int pst_attach(device_t); 74 static int pst_shutdown(device_t); 75 static void pst_start(struct pst_softc *); 76 static void pst_done(struct iop_softc *, u_int32_t, struct i2o_single_reply *); 77 static int pst_rw(struct pst_request *); 78 static void pst_timeout(struct pst_request *); 79 static void bpack(int8_t *, int8_t *, int); 80 81 /* local vars */ 82 static MALLOC_DEFINE(M_PSTRAID, "pst", "Promise SuperTrak RAID driver"); 83 84 int 85 pst_add_raid(struct iop_softc *sc, struct i2o_lct_entry *lct) 86 { 87 struct pst_softc *psc; 88 device_t child = device_add_child(sc->dev, "pst", -1); 89 90 if (!child) 91 return ENOMEM; 92 if (!(psc = malloc(sizeof(struct pst_softc), 93 M_PSTRAID, M_NOWAIT | M_ZERO))) { 94 device_delete_child(sc->dev, child); 95 return ENOMEM; 96 } 97 psc->iop = sc; 98 psc->lct = lct; 99 device_set_softc(child, psc); 100 return bus_generic_attach(sc->dev); 101 } 102 103 static int 104 pst_probe(device_t dev) 105 { 106 device_set_desc(dev, "Promise SuperTrak RAID"); 107 return 0; 108 } 109 110 static int 111 pst_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.d_name = "pst"; 153 psc->disk.d_strategy = pststrategy; 154 psc->disk.d_maxsize = 64 * 1024; /*I2O_SGL_MAX_SEGS * PAGE_SIZE;*/ 155 psc->disk.d_drv1 = psc; 156 disk_create(lun, &psc->disk, DISKFLAG_NOGIANT, NULL, NULL); 157 158 psc->disk.d_sectorsize = psc->info->block_size; 159 psc->disk.d_mediasize = psc->info->capacity; 160 psc->disk.d_fwsectors = 63; 161 psc->disk.d_fwheads = 255; 162 163 printf("pst%d: %lluMB <%.40s> [%lld/%d/%d] on %.16s\n", lun, 164 (unsigned long long)psc->info->capacity / (1024 * 1024), 165 name, psc->info->capacity/(512*255*63), 255, 63, 166 device_get_nameunit(psc->iop->dev)); 167 168 EVENTHANDLER_REGISTER(shutdown_post_sync, pst_shutdown, 169 dev, SHUTDOWN_PRI_FIRST); 170 return 0; 171 } 172 173 static int 174 pst_shutdown(device_t dev) 175 { 176 struct pst_softc *psc = device_get_softc(dev); 177 struct i2o_bsa_cache_flush_message *msg; 178 int mfa; 179 180 mfa = iop_get_mfa(psc->iop); 181 msg = (struct i2o_bsa_cache_flush_message *)(psc->iop->ibase + mfa); 182 bzero(msg, sizeof(struct i2o_bsa_cache_flush_message)); 183 msg->version_offset = 0x01; 184 msg->message_flags = 0x0; 185 msg->message_size = sizeof(struct i2o_bsa_cache_flush_message) >> 2; 186 msg->target_address = psc->lct->local_tid; 187 msg->initiator_address = I2O_TID_HOST; 188 msg->function = I2O_BSA_CACHE_FLUSH; 189 msg->control_flags = 0x0; /* 0x80 = post progress reports */ 190 if (iop_queue_wait_msg(psc->iop, mfa, (struct i2o_basic_message *)msg)) 191 printf("pst: shutdown failed!\n"); 192 return 0; 193 } 194 195 static void 196 pststrategy(struct bio *bp) 197 { 198 struct pst_softc *psc = bp->bio_disk->d_drv1; 199 200 mtx_lock(&psc->iop->mtx); 201 bioq_disksort(&psc->queue, bp); 202 pst_start(psc); 203 mtx_unlock(&psc->iop->mtx); 204 } 205 206 static void 207 pst_start(struct pst_softc *psc) 208 { 209 struct pst_request *request; 210 struct bio *bp; 211 u_int32_t mfa; 212 213 if (psc->iop->outstanding < (I2O_IOP_OUTBOUND_FRAME_COUNT - 1) && 214 (bp = bioq_first(&psc->queue))) { 215 if ((mfa = iop_get_mfa(psc->iop)) != 0xffffffff) { 216 bioq_remove(&psc->queue, bp); 217 if (!(request = malloc(sizeof(struct pst_request), 218 M_PSTRAID, M_NOWAIT | M_ZERO))) { 219 printf("pst: out of memory in start\n"); 220 biofinish(request->bp, NULL, ENOMEM); 221 iop_free_mfa(psc->iop, mfa); 222 return; 223 } 224 psc->iop->outstanding++; 225 request->psc = psc; 226 request->mfa = mfa; 227 request->bp = bp; 228 if (pst_rw(request)) { 229 biofinish(request->bp, NULL, EIO); 230 iop_free_mfa(request->psc->iop, request->mfa); 231 psc->iop->outstanding--; 232 free(request, M_PSTRAID); 233 } 234 } 235 } 236 } 237 238 static void 239 pst_done(struct iop_softc *sc, u_int32_t mfa, struct i2o_single_reply *reply) 240 { 241 struct pst_request *request = 242 (struct pst_request *)reply->transaction_context; 243 struct pst_softc *psc = request->psc; 244 245 untimeout((timeout_t *)pst_timeout, request, request->timeout_handle); 246 request->bp->bio_resid = request->bp->bio_bcount - reply->donecount; 247 biofinish(request->bp, NULL, reply->status ? EIO : 0); 248 free(request, M_PSTRAID); 249 psc->iop->reg->oqueue = mfa; 250 psc->iop->outstanding--; 251 pst_start(psc); 252 } 253 254 int 255 pst_rw(struct pst_request *request) 256 { 257 struct i2o_bsa_rw_block_message *msg; 258 int sgl_flag; 259 260 msg = (struct i2o_bsa_rw_block_message *) 261 (request->psc->iop->ibase + request->mfa); 262 bzero(msg, sizeof(struct i2o_bsa_rw_block_message)); 263 msg->version_offset = 0x81; 264 msg->message_flags = 0x0; 265 msg->message_size = sizeof(struct i2o_bsa_rw_block_message) >> 2; 266 msg->target_address = request->psc->lct->local_tid; 267 msg->initiator_address = I2O_TID_HOST; 268 switch (request->bp->bio_cmd) { 269 case BIO_READ: 270 msg->function = I2O_BSA_BLOCK_READ; 271 msg->control_flags = 0x0; /* 0x0c = read cache + readahead */ 272 msg->fetch_ahead = 0x0; /* 8 Kb */ 273 sgl_flag = 0; 274 break; 275 case BIO_WRITE: 276 msg->function = I2O_BSA_BLOCK_WRITE; 277 msg->control_flags = 0x0; /* 0x10 = write behind cache */ 278 msg->fetch_ahead = 0x0; 279 sgl_flag = I2O_SGL_DIR; 280 break; 281 default: 282 printf("pst: unknown command type 0x%02x\n", request->bp->bio_cmd); 283 return -1; 284 } 285 msg->initiator_context = (u_int32_t)pst_done; 286 msg->transaction_context = (u_int32_t)request; 287 msg->time_multiplier = 1; 288 msg->bytecount = request->bp->bio_bcount; 289 msg->lba = ((u_int64_t)request->bp->bio_pblkno) * (DEV_BSIZE * 1LL); 290 291 if (!iop_create_sgl((struct i2o_basic_message *)msg, request->bp->bio_data, 292 request->bp->bio_bcount, sgl_flag)) 293 return -1; 294 295 request->psc->iop->reg->iqueue = request->mfa; 296 297 if (dumping) 298 request->timeout_handle.callout = NULL; 299 else 300 request->timeout_handle = 301 timeout((timeout_t*)pst_timeout, request, 10 * hz); 302 return 0; 303 } 304 305 static void 306 pst_timeout(struct pst_request *request) 307 { 308 printf("pst: timeout mfa=0x%08x cmd=0x%02x\n", 309 request->mfa, request->bp->bio_cmd); 310 mtx_lock(&request->psc->iop->mtx); 311 iop_free_mfa(request->psc->iop, request->mfa); 312 if ((request->mfa = iop_get_mfa(request->psc->iop)) == 0xffffffff) { 313 printf("pst: timeout no mfa possible\n"); 314 biofinish(request->bp, NULL, EIO); 315 request->psc->iop->outstanding--; 316 mtx_unlock(&request->psc->iop->mtx); 317 return; 318 } 319 if (pst_rw(request)) { 320 iop_free_mfa(request->psc->iop, request->mfa); 321 biofinish(request->bp, NULL, EIO); 322 request->psc->iop->outstanding--; 323 } 324 mtx_unlock(&request->psc->iop->mtx); 325 } 326 327 static void 328 bpack(int8_t *src, int8_t *dst, int len) 329 { 330 int i, j, blank; 331 int8_t *ptr, *buf = dst; 332 333 for (i = j = blank = 0 ; i < len; i++) { 334 if (blank && src[i] == ' ') 335 continue; 336 if (blank && src[i] != ' ') { 337 dst[j++] = src[i]; 338 blank = 0; 339 continue; 340 } 341 if (src[i] == ' ') { 342 blank = 1; 343 if (i == 0) 344 continue; 345 } 346 dst[j++] = src[i]; 347 } 348 if (j < len) 349 dst[j] = 0x00; 350 for (ptr = buf; ptr < buf+len; ++ptr) 351 if (!*ptr) 352 *ptr = ' '; 353 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr) 354 *ptr = 0; 355 } 356 357 static device_method_t pst_methods[] = { 358 DEVMETHOD(device_probe, pst_probe), 359 DEVMETHOD(device_attach, pst_attach), 360 { 0, 0 } 361 }; 362 363 static driver_t pst_driver = { 364 "pst", 365 pst_methods, 366 sizeof(struct pst_softc), 367 }; 368 369 static devclass_t pst_devclass; 370 371 DRIVER_MODULE(pst, pstpci, pst_driver, pst_devclass, 0, 0); 372