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