1 /* $NetBSD: ofdisk.c,v 1.51 2015/04/26 15:15:20 mlelstv Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996 Wolfgang Solfrank. 5 * Copyright (C) 1995, 1996 TooLs GmbH. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 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. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by TooLs GmbH. 19 * 4. The name of TooLs GmbH may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: ofdisk.c,v 1.51 2015/04/26 15:15:20 mlelstv Exp $"); 36 37 #include <sys/param.h> 38 #include <sys/buf.h> 39 #include <sys/device.h> 40 #include <sys/conf.h> 41 #include <sys/disklabel.h> 42 #include <sys/disk.h> 43 #include <sys/fcntl.h> 44 #include <sys/ioctl.h> 45 #include <sys/stat.h> 46 #include <sys/systm.h> 47 #include <sys/proc.h> 48 49 #include <dev/ofw/openfirm.h> 50 51 struct ofdisk_softc { 52 device_t sc_dev; 53 int sc_phandle; 54 int sc_unit; 55 int sc_flags; 56 struct disk sc_dk; 57 int sc_ihandle; 58 u_long max_transfer; 59 }; 60 61 /* sc_flags */ 62 #define OFDF_ISFLOPPY 0x01 /* we are a floppy drive */ 63 64 #define OFDISK_FLOPPY_P(of) ((of)->sc_flags & OFDF_ISFLOPPY) 65 66 static int ofdisk_match (device_t, cfdata_t, void *); 67 static void ofdisk_attach (device_t, device_t, void *); 68 69 CFATTACH_DECL_NEW(ofdisk, sizeof(struct ofdisk_softc), 70 ofdisk_match, ofdisk_attach, NULL, NULL); 71 72 extern struct cfdriver ofdisk_cd; 73 74 dev_type_open(ofdisk_open); 75 dev_type_close(ofdisk_close); 76 dev_type_read(ofdisk_read); 77 dev_type_write(ofdisk_write); 78 dev_type_ioctl(ofdisk_ioctl); 79 dev_type_strategy(ofdisk_strategy); 80 dev_type_dump(ofdisk_dump); 81 dev_type_size(ofdisk_size); 82 83 const struct bdevsw ofdisk_bdevsw = { 84 .d_open = ofdisk_open, 85 .d_close = ofdisk_close, 86 .d_strategy = ofdisk_strategy, 87 .d_ioctl = ofdisk_ioctl, 88 .d_dump = ofdisk_dump, 89 .d_psize = ofdisk_size, 90 .d_discard = nodiscard, 91 .d_flag = D_DISK 92 }; 93 94 const struct cdevsw ofdisk_cdevsw = { 95 .d_open = ofdisk_open, 96 .d_close = ofdisk_close, 97 .d_read = ofdisk_read, 98 .d_write = ofdisk_write, 99 .d_ioctl = ofdisk_ioctl, 100 .d_stop = nostop, 101 .d_tty = notty, 102 .d_poll = nopoll, 103 .d_mmap = nommap, 104 .d_kqfilter = nokqfilter, 105 .d_discard = nodiscard, 106 .d_flag = D_DISK 107 }; 108 109 static void ofminphys(struct buf *); 110 111 struct dkdriver ofdisk_dkdriver = { 112 .d_strategy = ofdisk_strategy, 113 .d_minphys = ofminphys 114 }; 115 116 void ofdisk_getdefaultlabel (struct ofdisk_softc *, struct disklabel *); 117 void ofdisk_getdisklabel (dev_t); 118 119 static int 120 ofdisk_match(device_t parent, cfdata_t match, void *aux) 121 { 122 struct ofbus_attach_args *oba = aux; 123 char type[8]; 124 int l; 125 126 if (strcmp(oba->oba_busname, "ofw")) 127 return (0); 128 if ((l = OF_getprop(oba->oba_phandle, "device_type", type, 129 sizeof type - 1)) < 0) 130 return 0; 131 if (l >= sizeof type) 132 return 0; 133 type[l] = 0; 134 return !strcmp(type, "block"); 135 } 136 137 static void 138 ofdisk_attach(device_t parent, device_t self, void *aux) 139 { 140 struct ofdisk_softc *of = device_private(self); 141 struct ofbus_attach_args *oba = aux; 142 char child[64]; 143 int l; 144 145 of->sc_dev = self; 146 if ((l = OF_getprop(oba->oba_phandle, "name", child, 147 sizeof child - 1)) < 0) 148 panic("device without name?"); 149 if (l >= sizeof child) 150 l = sizeof child - 1; 151 child[l] = 0; 152 153 of->sc_flags = 0; 154 of->sc_phandle = oba->oba_phandle; 155 of->sc_unit = oba->oba_unit; 156 of->sc_ihandle = 0; 157 disk_init(&of->sc_dk, device_xname(of->sc_dev), &ofdisk_dkdriver); 158 disk_attach(&of->sc_dk); 159 printf("\n"); 160 161 if (strcmp(child, "floppy") == 0) 162 of->sc_flags |= OFDF_ISFLOPPY; 163 else { 164 /* Discover wedges on this disk. */ 165 dkwedge_discover(&of->sc_dk); 166 } 167 } 168 169 int 170 ofdisk_open(dev_t dev, int flags, int fmt, struct lwp *lwp) 171 { 172 struct ofdisk_softc *of; 173 char path[256]; 174 int error, l, part; 175 176 of = device_lookup_private(&ofdisk_cd, DISKUNIT(dev)); 177 if (of == NULL) 178 return ENXIO; 179 180 part = DISKPART(dev); 181 182 mutex_enter(&of->sc_dk.dk_openlock); 183 184 /* 185 * If there are wedges, and this is not RAW_PART, then we 186 * need to fail. 187 */ 188 if (of->sc_dk.dk_nwedges != 0 && part != RAW_PART) { 189 error = EBUSY; 190 goto bad1; 191 } 192 193 if (!of->sc_ihandle) { 194 if ((l = OF_package_to_path(of->sc_phandle, path, 195 sizeof path - 3)) < 0 || 196 l >= sizeof path - 3) { 197 error = ENXIO; 198 goto bad1; 199 } 200 path[l] = 0; 201 202 /* 203 * XXX This is for the benefit of SCSI/IDE disks that don't 204 * XXX have all their childs in the device tree. 205 * XXX YES, I DO THINK THIS IS A BUG IN OPENFIRMWARE!!! 206 * XXX And yes, this is a very gross hack! 207 * XXX See also ofscsi.c 208 */ 209 if (!strcmp(path + l - 4, "disk")) { 210 path[l++] = '@'; 211 path[l++] = '0' + of->sc_unit; 212 path[l] = 0; 213 } 214 215 strlcat(path, ":0", sizeof(path)); 216 217 if ((of->sc_ihandle = OF_open(path)) == -1) { 218 error = ENXIO; 219 goto bad1; 220 } 221 222 /* 223 * Try to get characteristics of the disk. 224 */ 225 of->max_transfer = OF_call_method_1("max-transfer", 226 of->sc_ihandle, 0); 227 if (of->max_transfer > MAXPHYS) 228 of->max_transfer = MAXPHYS; 229 230 ofdisk_getdisklabel(dev); 231 } 232 233 switch (fmt) { 234 case S_IFCHR: 235 of->sc_dk.dk_copenmask |= 1 << part; 236 break; 237 case S_IFBLK: 238 of->sc_dk.dk_bopenmask |= 1 << part; 239 break; 240 } 241 of->sc_dk.dk_openmask = 242 of->sc_dk.dk_copenmask | of->sc_dk.dk_bopenmask; 243 244 245 error = 0; 246 bad1: 247 mutex_exit(&of->sc_dk.dk_openlock); 248 return (error); 249 } 250 251 int 252 ofdisk_close(dev_t dev, int flags, int fmt, struct lwp *l) 253 { 254 struct ofdisk_softc *of = 255 device_lookup_private(&ofdisk_cd, DISKUNIT(dev)); 256 257 mutex_enter(&of->sc_dk.dk_openlock); 258 259 switch (fmt) { 260 case S_IFCHR: 261 of->sc_dk.dk_copenmask &= ~(1 << DISKPART(dev)); 262 break; 263 case S_IFBLK: 264 of->sc_dk.dk_bopenmask &= ~(1 << DISKPART(dev)); 265 break; 266 } 267 of->sc_dk.dk_openmask = of->sc_dk.dk_copenmask | of->sc_dk.dk_bopenmask; 268 269 #ifdef FIRMWORKSBUGS 270 /* 271 * This is a hack to get the firmware to flush its buffers. 272 */ 273 OF_seek(of->sc_ihandle, 0); 274 #endif 275 if (!of->sc_dk.dk_openmask) { 276 OF_close(of->sc_ihandle); 277 of->sc_ihandle = 0; 278 } 279 280 mutex_exit(&of->sc_dk.dk_openlock); 281 return 0; 282 } 283 284 void 285 ofdisk_strategy(struct buf *bp) 286 { 287 struct ofdisk_softc *of = 288 device_lookup_private(&ofdisk_cd, DISKUNIT(bp->b_dev)); 289 struct partition *p; 290 u_quad_t off; 291 int read; 292 int (*OF_io)(int, void *, int); 293 daddr_t blkno = bp->b_blkno; 294 295 bp->b_resid = 0; 296 if (bp->b_bcount == 0) 297 goto done; 298 299 OF_io = bp->b_flags & B_READ ? OF_read : 300 (int(*)(int, void*, int))OF_write; 301 302 if (DISKPART(bp->b_dev) != RAW_PART) { 303 if (bounds_check_with_label(&of->sc_dk, bp, 0) <= 0) { 304 bp->b_resid = bp->b_bcount; 305 goto done; 306 } 307 p = &of->sc_dk.dk_label->d_partitions[DISKPART(bp->b_dev)]; 308 blkno = bp->b_blkno + p->p_offset; 309 } 310 311 disk_busy(&of->sc_dk); 312 313 off = (u_quad_t)blkno * DEV_BSIZE; 314 read = -1; 315 do { 316 if (OF_seek(of->sc_ihandle, off) < 0) 317 break; 318 read = OF_io(of->sc_ihandle, bp->b_data, bp->b_bcount); 319 } while (read == -2); 320 321 if (read < 0) { 322 bp->b_error = EIO; 323 bp->b_resid = bp->b_bcount; 324 } else 325 bp->b_resid = bp->b_bcount - read; 326 327 disk_unbusy(&of->sc_dk, bp->b_bcount - bp->b_resid, 328 (bp->b_flags & B_READ)); 329 330 done: 331 biodone(bp); 332 } 333 334 static void 335 ofminphys(struct buf *bp) 336 { 337 struct ofdisk_softc *of = 338 device_lookup_private(&ofdisk_cd, DISKUNIT(bp->b_dev)); 339 340 if (bp->b_bcount > of->max_transfer) 341 bp->b_bcount = of->max_transfer; 342 } 343 344 int 345 ofdisk_read(dev_t dev, struct uio *uio, int flags) 346 { 347 return physio(ofdisk_strategy, NULL, dev, B_READ, ofminphys, uio); 348 } 349 350 int 351 ofdisk_write(dev_t dev, struct uio *uio, int flags) 352 { 353 return physio(ofdisk_strategy, NULL, dev, B_WRITE, ofminphys, uio); 354 } 355 356 int 357 ofdisk_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 358 { 359 struct ofdisk_softc *of = 360 device_lookup_private(&ofdisk_cd, DISKUNIT(dev)); 361 int error; 362 #ifdef __HAVE_OLD_DISKLABEL 363 struct disklabel newlabel; 364 #endif 365 /* XXX: Why not allow wedges on floppy? */ 366 switch (cmd) { 367 case DIOCDWEDGE: 368 case DIOCAWEDGE: 369 case DIOCLWEDGES: 370 case DIOCMWEDGES: 371 if (OFDISK_FLOPPY_P(of)) 372 return ENOTTY; 373 } 374 375 error = disk_ioctl(&of->sc_dk, dev, cmd, data, flag, l); 376 if (error != EPASSTHROUGH) 377 return error; 378 379 switch (cmd) { 380 case DIOCWDINFO: 381 case DIOCSDINFO: 382 #ifdef __HAVE_OLD_DISKLABEL 383 case ODIOCWDINFO: 384 case ODIOCSDINFO: 385 #endif 386 { 387 struct disklabel *lp; 388 389 #ifdef __HAVE_OLD_DISKLABEL 390 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) { 391 memset(&newlabel, 0, sizeof newlabel); 392 memcpy(&newlabel, data, sizeof (struct olddisklabel)); 393 lp = &newlabel; 394 } else 395 #endif 396 lp = (struct disklabel *)data; 397 398 if ((flag & FWRITE) == 0) 399 return EBADF; 400 401 mutex_enter(&of->sc_dk.dk_openlock); 402 403 error = setdisklabel(of->sc_dk.dk_label, 404 lp, /*of->sc_dk.dk_openmask */0, 405 of->sc_dk.dk_cpulabel); 406 if (error == 0 && cmd == DIOCWDINFO 407 #ifdef __HAVE_OLD_DISKLABEL 408 || xfer == ODIOCWDINFO 409 #endif 410 ) 411 error = writedisklabel(MAKEDISKDEV(major(dev), 412 DISKUNIT(dev), RAW_PART), ofdisk_strategy, 413 of->sc_dk.dk_label, of->sc_dk.dk_cpulabel); 414 415 mutex_exit(&of->sc_dk.dk_openlock); 416 417 return error; 418 } 419 420 case DIOCGDEFLABEL: 421 ofdisk_getdefaultlabel(of, (struct disklabel *)data); 422 return 0; 423 #ifdef __HAVE_OLD_DISKLABEL 424 case DIOCGDEFLABEL: 425 ofdisk_getdefaultlabel(of, &newlabel); 426 if (newlabel.d_npartitions > OLDMAXPARTITIONS) 427 return ENOTTY; 428 memcpy(data, &newlabel, sizeof (struct olddisklabel)); 429 return 0; 430 #endif 431 432 default: 433 return ENOTTY; 434 } 435 } 436 437 int 438 ofdisk_dump(dev_t dev, daddr_t blkno, void *va, size_t size) 439 { 440 return EINVAL; 441 } 442 443 int 444 ofdisk_size(dev_t dev) 445 { 446 struct ofdisk_softc *of; 447 struct disklabel *lp; 448 int size, part, omask; 449 450 of = device_lookup_private(&ofdisk_cd, DISKUNIT(dev)); 451 if (of == NULL) 452 return ENXIO; 453 454 part = DISKPART(dev); 455 omask = of->sc_dk.dk_openmask & (1 << part); 456 lp = of->sc_dk.dk_label; 457 458 if (omask == 0 && ofdisk_open(dev, 0, S_IFBLK, curlwp) != 0) 459 return -1; 460 461 if (lp->d_partitions[part].p_fstype != FS_SWAP) 462 size = -1; 463 else 464 size = lp->d_partitions[part].p_size * 465 (lp->d_secsize / DEV_BSIZE); 466 467 if (omask == 0 && ofdisk_close(dev, 0, S_IFBLK, curlwp) != 0) 468 return -1; 469 470 return size; 471 } 472 473 void 474 ofdisk_getdefaultlabel(struct ofdisk_softc *of, struct disklabel *lp) 475 { 476 477 memset(lp, 0, sizeof *lp); 478 479 /* 480 * XXX Firmware bug? Asking for block size gives a 481 * XXX ridiculous number! So we use what the boot program 482 * XXX uses. 483 */ 484 lp->d_secsize = DEV_BSIZE; 485 486 lp->d_secperunit = OF_call_method_1("#blocks", 487 of->sc_ihandle, 0); 488 if (lp->d_secperunit == (u_int32_t)-1) 489 lp->d_secperunit = 0x7fffffff; 490 491 lp->d_secpercyl = 1; 492 lp->d_nsectors = 1; 493 lp->d_ntracks = 1; 494 lp->d_ncylinders = lp->d_secperunit; 495 496 lp->d_partitions[RAW_PART].p_offset = 0; 497 lp->d_partitions[RAW_PART].p_size = lp->d_secperunit; 498 lp->d_npartitions = RAW_PART + 1; 499 500 lp->d_magic = DISKMAGIC; 501 lp->d_magic2 = DISKMAGIC; 502 lp->d_checksum = dkcksum(lp); 503 } 504 505 void 506 ofdisk_getdisklabel(dev_t dev) 507 { 508 int unit = DISKUNIT(dev); 509 struct ofdisk_softc *of = 510 device_lookup_private(&ofdisk_cd, unit); 511 struct disklabel *lp = of->sc_dk.dk_label; 512 const char *errmes; 513 int l; 514 515 ofdisk_getdefaultlabel(of, lp); 516 517 /* 518 * Don't read the disklabel on a floppy; simply 519 * assign all partitions the same size/offset as 520 * RAW_PART. (This is essentially what the ISA 521 * floppy driver does, but we don't deal with 522 * density stuff.) 523 */ 524 if (OFDISK_FLOPPY_P(of)) { 525 lp->d_npartitions = MAXPARTITIONS; 526 for (l = 0; l < lp->d_npartitions; l++) { 527 if (l == RAW_PART) 528 continue; 529 /* struct copy */ 530 lp->d_partitions[l] = 531 lp->d_partitions[RAW_PART]; 532 } 533 lp->d_checksum = dkcksum(lp); 534 } else { 535 errmes = readdisklabel(MAKEDISKDEV(major(dev), 536 unit, RAW_PART), ofdisk_strategy, lp, 537 of->sc_dk.dk_cpulabel); 538 if (errmes != NULL) 539 printf("%s: %s\n", device_xname(of->sc_dev), errmes); 540 } 541 } 542