1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2001,2002,2003 Søren Schmidt <sos@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 18 * 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. 29 */ 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/module.h> 34 #include <sys/bio.h> 35 #include <sys/bus.h> 36 #include <sys/conf.h> 37 #include <sys/eventhandler.h> 38 #include <sys/kernel.h> 39 #include <sys/lock.h> 40 #include <sys/malloc.h> 41 #include <sys/mutex.h> 42 #include <sys/rman.h> 43 44 #include <vm/vm.h> 45 #include <vm/pmap.h> 46 47 #include <machine/stdarg.h> 48 #include <machine/resource.h> 49 #include <machine/bus.h> 50 51 #include <dev/pci/pcivar.h> 52 #include <dev/pci/pcireg.h> 53 #include "dev/pst/pst-iop.h" 54 55 #include <geom/geom_disk.h> 56 57 struct pst_softc { 58 struct iop_softc *iop; 59 struct i2o_lct_entry *lct; 60 struct i2o_bsa_device *info; 61 struct disk *disk; 62 struct bio_queue_head queue; 63 }; 64 65 struct pst_request { 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 */ 70 }; 71 72 /* prototypes */ 73 static disk_strategy_t pststrategy; 74 static int pst_probe(device_t); 75 static int pst_attach(device_t); 76 static void pst_shutdown_post_sync(device_t, int); 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); 82 83 /* local vars */ 84 static MALLOC_DEFINE(M_PSTRAID, "pst", "Promise SuperTrak RAID driver"); 85 86 int 87 pst_add_raid(struct iop_softc *sc, struct i2o_lct_entry *lct) 88 { 89 struct pst_softc *psc; 90 device_t child = device_add_child(sc->dev, "pst", -1); 91 92 if (!child) 93 return ENOMEM; 94 if (!(psc = malloc(sizeof(struct pst_softc), 95 M_PSTRAID, M_NOWAIT | M_ZERO))) { 96 device_delete_child(sc->dev, child); 97 return ENOMEM; 98 } 99 psc->iop = sc; 100 psc->lct = lct; 101 device_set_softc(child, psc); 102 return device_probe_and_attach(child); 103 } 104 105 static int 106 pst_probe(device_t dev) 107 { 108 device_set_desc(dev, "Promise SuperTrak RAID"); 109 return 0; 110 } 111 112 static int 113 pst_attach(device_t dev) 114 { 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); 119 int8_t name [32]; 120 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))) 124 return ENODEV; 125 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); 129 return ENOMEM; 130 } 131 bcopy(reply->result, psc->info, sizeof(struct i2o_bsa_device)); 132 contigfree(reply, PAGE_SIZE, M_PSTIOP); 133 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))) 137 return ENODEV; 138 ident = (struct i2o_device_identity *)reply->result; 139 #ifdef PSTDEBUG 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); 146 #endif 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); 151 152 bioq_init(&psc->queue); 153 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; 160 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; 165 166 disk_create(psc->disk, DISK_VERSION); 167 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)); 172 173 EVENTHANDLER_REGISTER(shutdown_post_sync, pst_shutdown_post_sync, 174 dev, SHUTDOWN_PRI_FIRST); 175 return 0; 176 } 177 178 static void 179 pst_shutdown_post_sync(device_t dev, int howto __unused) 180 { 181 struct pst_softc *psc = device_get_softc(dev); 182 struct i2o_bsa_cache_flush_message *msg; 183 int mfa; 184 185 if (SCHEDULER_STOPPED()) { 186 /* Request polled shutdown. */ 187 psc->iop->reg->oqueue_intr_mask = 0xffffffff; 188 } 189 190 mfa = iop_get_mfa(psc->iop); 191 msg = (struct i2o_bsa_cache_flush_message *)(psc->iop->ibase + mfa); 192 bzero(msg, sizeof(struct i2o_bsa_cache_flush_message)); 193 msg->version_offset = 0x01; 194 msg->message_flags = 0x0; 195 msg->message_size = sizeof(struct i2o_bsa_cache_flush_message) >> 2; 196 msg->target_address = psc->lct->local_tid; 197 msg->initiator_address = I2O_TID_HOST; 198 msg->function = I2O_BSA_CACHE_FLUSH; 199 msg->control_flags = 0x0; /* 0x80 = post progress reports */ 200 if (iop_queue_wait_msg(psc->iop, mfa, (struct i2o_basic_message *)msg)) 201 printf("pst: shutdown failed!\n"); 202 } 203 204 static void 205 pststrategy(struct bio *bp) 206 { 207 struct pst_softc *psc = bp->bio_disk->d_drv1; 208 209 mtx_lock(&psc->iop->mtx); 210 bioq_disksort(&psc->queue, bp); 211 pst_start(psc); 212 mtx_unlock(&psc->iop->mtx); 213 } 214 215 static void 216 pst_start(struct pst_softc *psc) 217 { 218 struct pst_request *request; 219 struct bio *bp; 220 u_int32_t mfa; 221 int error; 222 223 if (psc->iop->outstanding < (I2O_IOP_OUTBOUND_FRAME_COUNT - 1) && 224 (bp = bioq_first(&psc->queue))) { 225 if ((mfa = iop_get_mfa(psc->iop)) != 0xffffffff) { 226 bioq_remove(&psc->queue, bp); 227 if (!(request = malloc(sizeof(struct pst_request), 228 M_PSTRAID, M_NOWAIT | M_ZERO))) { 229 printf("pst: out of memory in start\n"); 230 biofinish(request->bp, NULL, ENOMEM); 231 iop_free_mfa(psc->iop, mfa); 232 return; 233 } 234 callout_init_mtx(&request->timeout, &psc->iop->mtx, 0); 235 psc->iop->outstanding++; 236 request->psc = psc; 237 request->mfa = mfa; 238 request->bp = bp; 239 if ((error = pst_rw(request)) != 0) { 240 biofinish(request->bp, NULL, error); 241 iop_free_mfa(request->psc->iop, request->mfa); 242 psc->iop->outstanding--; 243 free(request, M_PSTRAID); 244 } 245 } 246 } 247 } 248 249 static void 250 pst_done(struct iop_softc *sc, u_int32_t mfa, struct i2o_single_reply *reply) 251 { 252 struct pst_request *request = 253 (struct pst_request *)reply->transaction_context; 254 struct pst_softc *psc = request->psc; 255 256 callout_stop(&request->timeout); 257 request->bp->bio_resid = request->bp->bio_bcount - reply->donecount; 258 biofinish(request->bp, NULL, reply->status ? EIO : 0); 259 free(request, M_PSTRAID); 260 psc->iop->reg->oqueue = mfa; 261 psc->iop->outstanding--; 262 pst_start(psc); 263 } 264 265 int 266 pst_rw(struct pst_request *request) 267 { 268 struct i2o_bsa_rw_block_message *msg; 269 int sgl_flag; 270 271 msg = (struct i2o_bsa_rw_block_message *) 272 (request->psc->iop->ibase + request->mfa); 273 bzero(msg, sizeof(struct i2o_bsa_rw_block_message)); 274 msg->version_offset = 0x81; 275 msg->message_flags = 0x0; 276 msg->message_size = sizeof(struct i2o_bsa_rw_block_message) >> 2; 277 msg->target_address = request->psc->lct->local_tid; 278 msg->initiator_address = I2O_TID_HOST; 279 switch (request->bp->bio_cmd) { 280 case BIO_READ: 281 msg->function = I2O_BSA_BLOCK_READ; 282 msg->control_flags = 0x0; /* 0x0c = read cache + readahead */ 283 msg->fetch_ahead = 0x0; /* 8 Kb */ 284 sgl_flag = 0; 285 break; 286 case BIO_WRITE: 287 msg->function = I2O_BSA_BLOCK_WRITE; 288 msg->control_flags = 0x0; /* 0x10 = write behind cache */ 289 msg->fetch_ahead = 0x0; 290 sgl_flag = I2O_SGL_DIR; 291 break; 292 default: 293 printf("pst: unknown command type 0x%02x\n", request->bp->bio_cmd); 294 return EOPNOTSUPP; 295 } 296 msg->initiator_context = (u_int32_t)pst_done; 297 msg->transaction_context = (u_int32_t)request; 298 msg->time_multiplier = 1; 299 msg->bytecount = request->bp->bio_bcount; 300 msg->lba = ((u_int64_t)request->bp->bio_pblkno) * (DEV_BSIZE * 1LL); 301 302 if (!iop_create_sgl((struct i2o_basic_message *)msg, request->bp->bio_data, 303 request->bp->bio_bcount, sgl_flag)) 304 return EIO; 305 306 request->psc->iop->reg->iqueue = request->mfa; 307 308 if (!dumping) 309 callout_reset(&request->timeout, 10 * hz, pst_timeout, request); 310 return 0; 311 } 312 313 static void 314 pst_timeout(void *arg) 315 { 316 struct pst_request *request; 317 int error; 318 319 request = arg; 320 printf("pst: timeout mfa=0x%08x cmd=0x%02x\n", 321 request->mfa, request->bp->bio_cmd); 322 mtx_assert(&request->psc->iop->mtx, MA_OWNED); 323 iop_free_mfa(request->psc->iop, request->mfa); 324 if ((request->mfa = iop_get_mfa(request->psc->iop)) == 0xffffffff) { 325 printf("pst: timeout no mfa possible\n"); 326 biofinish(request->bp, NULL, EIO); 327 request->psc->iop->outstanding--; 328 return; 329 } 330 if ((error = pst_rw(request)) != 0) { 331 iop_free_mfa(request->psc->iop, request->mfa); 332 biofinish(request->bp, NULL, error); 333 request->psc->iop->outstanding--; 334 } 335 } 336 337 static void 338 bpack(int8_t *src, int8_t *dst, int len) 339 { 340 int i, j, blank; 341 int8_t *ptr, *buf = dst; 342 343 for (i = j = blank = 0 ; i < len; i++) { 344 if (blank && src[i] == ' ') 345 continue; 346 if (blank && src[i] != ' ') { 347 dst[j++] = src[i]; 348 blank = 0; 349 continue; 350 } 351 if (src[i] == ' ') { 352 blank = 1; 353 if (i == 0) 354 continue; 355 } 356 dst[j++] = src[i]; 357 } 358 if (j < len) 359 dst[j] = 0x00; 360 for (ptr = buf; ptr < buf+len; ++ptr) 361 if (!*ptr) 362 *ptr = ' '; 363 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr) 364 *ptr = 0; 365 } 366 367 static device_method_t pst_methods[] = { 368 DEVMETHOD(device_probe, pst_probe), 369 DEVMETHOD(device_attach, pst_attach), 370 { 0, 0 } 371 }; 372 373 static driver_t pst_driver = { 374 "pst", 375 pst_methods, 376 sizeof(struct pst_softc), 377 }; 378 379 DRIVER_MODULE(pst, pstpci, pst_driver, 0, 0); 380