1 /* $NetBSD: ofdisk.c,v 1.33 2006/03/29 07:10:25 thorpej 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.33 2006/03/29 07:10:25 thorpej 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 #include <sys/conf.h> 49 50 #include <dev/ofw/openfirm.h> 51 52 struct ofdisk_softc { 53 struct device sc_dev; 54 int sc_phandle; 55 int sc_unit; 56 int sc_flags; 57 struct disk sc_dk; 58 int sc_ihandle; 59 u_long max_transfer; 60 }; 61 62 /* sc_flags */ 63 #define OFDF_ISFLOPPY 0x01 /* we are a floppy drive */ 64 65 #define OFDISK_FLOPPY_P(of) ((of)->sc_flags & OFDF_ISFLOPPY) 66 67 static int ofdisk_match (struct device *, struct cfdata *, void *); 68 static void ofdisk_attach (struct device *, struct device *, void *); 69 70 CFATTACH_DECL(ofdisk, sizeof(struct ofdisk_softc), 71 ofdisk_match, ofdisk_attach, NULL, NULL); 72 73 extern struct cfdriver ofdisk_cd; 74 75 dev_type_open(ofdisk_open); 76 dev_type_close(ofdisk_close); 77 dev_type_read(ofdisk_read); 78 dev_type_write(ofdisk_write); 79 dev_type_ioctl(ofdisk_ioctl); 80 dev_type_strategy(ofdisk_strategy); 81 dev_type_dump(ofdisk_dump); 82 dev_type_size(ofdisk_size); 83 84 const struct bdevsw ofdisk_bdevsw = { 85 ofdisk_open, ofdisk_close, ofdisk_strategy, ofdisk_ioctl, 86 ofdisk_dump, ofdisk_size, D_DISK 87 }; 88 89 const struct cdevsw ofdisk_cdevsw = { 90 ofdisk_open, ofdisk_close, ofdisk_read, ofdisk_write, ofdisk_ioctl, 91 nostop, notty, nopoll, nommap, nokqfilter, D_DISK 92 }; 93 94 static void ofminphys(struct buf *); 95 96 struct dkdriver ofdisk_dkdriver = { ofdisk_strategy, ofminphys }; 97 98 void ofdisk_getdefaultlabel (struct ofdisk_softc *, struct disklabel *); 99 void ofdisk_getdisklabel (dev_t); 100 101 static int 102 ofdisk_match(struct device *parent, struct cfdata *match, void *aux) 103 { 104 struct ofbus_attach_args *oba = aux; 105 char type[8]; 106 int l; 107 108 if (strcmp(oba->oba_busname, "ofw")) 109 return (0); 110 if ((l = OF_getprop(oba->oba_phandle, "device_type", type, 111 sizeof type - 1)) < 0) 112 return 0; 113 if (l >= sizeof type) 114 return 0; 115 type[l] = 0; 116 return !strcmp(type, "block"); 117 } 118 119 static void 120 ofdisk_attach(struct device *parent, struct device *self, void *aux) 121 { 122 struct ofdisk_softc *of = device_private(self); 123 struct ofbus_attach_args *oba = aux; 124 char child[64]; 125 int l; 126 127 if ((l = OF_getprop(oba->oba_phandle, "name", child, 128 sizeof child - 1)) < 0) 129 panic("device without name?"); 130 if (l >= sizeof child) 131 l = sizeof child - 1; 132 child[l] = 0; 133 134 of->sc_flags = 0; 135 of->sc_phandle = oba->oba_phandle; 136 of->sc_unit = oba->oba_unit; 137 of->sc_ihandle = 0; 138 of->sc_dk.dk_driver = &ofdisk_dkdriver; 139 of->sc_dk.dk_name = of->sc_dev.dv_xname; 140 disk_attach(&of->sc_dk); 141 printf("\n"); 142 143 if (strcmp(child, "floppy") == 0) 144 of->sc_flags |= OFDF_ISFLOPPY; 145 else { 146 /* Discover wedges on this disk. */ 147 dkwedge_discover(&of->sc_dk); 148 } 149 } 150 151 int 152 ofdisk_open(dev_t dev, int flags, int fmt, struct lwp *lwp) 153 { 154 int unit = DISKUNIT(dev); 155 struct ofdisk_softc *of; 156 char path[256]; 157 int error, l, part; 158 159 if (unit >= ofdisk_cd.cd_ndevs) 160 return ENXIO; 161 if (!(of = ofdisk_cd.cd_devs[unit])) 162 return ENXIO; 163 164 part = DISKPART(dev); 165 166 if ((error = lockmgr(&of->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL)) != 0) 167 return (error); 168 169 /* 170 * If there are wedges, and this is not RAW_PART, then we 171 * need to fail. 172 */ 173 if (of->sc_dk.dk_nwedges != 0 && part != RAW_PART) { 174 error = EBUSY; 175 goto bad1; 176 } 177 178 if (!of->sc_ihandle) { 179 if ((l = OF_package_to_path(of->sc_phandle, path, 180 sizeof path - 3)) < 0 || 181 l >= sizeof path - 3) { 182 error = ENXIO; 183 goto bad1; 184 } 185 path[l] = 0; 186 187 /* 188 * XXX This is for the benefit of SCSI/IDE disks that don't 189 * XXX have all their childs in the device tree. 190 * XXX YES, I DO THINK THIS IS A BUG IN OPENFIRMWARE!!! 191 * XXX And yes, this is a very gross hack! 192 * XXX See also ofscsi.c 193 */ 194 if (!strcmp(path + l - 4, "disk")) { 195 path[l++] = '@'; 196 path[l++] = '0' + of->sc_unit; 197 path[l] = 0; 198 } 199 200 strlcat(path, ":0", sizeof(path)); 201 202 if ((of->sc_ihandle = OF_open(path)) == -1) { 203 error = ENXIO; 204 goto bad1; 205 } 206 207 /* 208 * Try to get characteristics of the disk. 209 */ 210 of->max_transfer = OF_call_method_1("max-transfer", 211 of->sc_ihandle, 0); 212 if (of->max_transfer > MAXPHYS) 213 of->max_transfer = MAXPHYS; 214 215 ofdisk_getdisklabel(dev); 216 } 217 218 switch (fmt) { 219 case S_IFCHR: 220 of->sc_dk.dk_copenmask |= 1 << part; 221 break; 222 case S_IFBLK: 223 of->sc_dk.dk_bopenmask |= 1 << part; 224 break; 225 } 226 of->sc_dk.dk_openmask = 227 of->sc_dk.dk_copenmask | of->sc_dk.dk_bopenmask; 228 229 (void) lockmgr(&of->sc_dk.dk_openlock, LK_RELEASE, NULL); 230 return 0; 231 232 bad1: 233 (void) lockmgr(&of->sc_dk.dk_openlock, LK_RELEASE, NULL); 234 return (error); 235 } 236 237 int 238 ofdisk_close(dev_t dev, int flags, int fmt, struct lwp *l) 239 { 240 struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(dev)]; 241 int error; 242 243 if ((error = lockmgr(&of->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL)) != 0) 244 return (error); 245 246 switch (fmt) { 247 case S_IFCHR: 248 of->sc_dk.dk_copenmask &= ~(1 << DISKPART(dev)); 249 break; 250 case S_IFBLK: 251 of->sc_dk.dk_bopenmask &= ~(1 << DISKPART(dev)); 252 break; 253 } 254 of->sc_dk.dk_openmask = of->sc_dk.dk_copenmask | of->sc_dk.dk_bopenmask; 255 256 #ifdef FIRMWORKSBUGS 257 /* 258 * This is a hack to get the firmware to flush its buffers. 259 */ 260 OF_seek(of->sc_ihandle, 0); 261 #endif 262 if (!of->sc_dk.dk_openmask) { 263 OF_close(of->sc_ihandle); 264 of->sc_ihandle = 0; 265 } 266 267 (void) lockmgr(&of->sc_dk.dk_openlock, LK_RELEASE, NULL); 268 return 0; 269 } 270 271 void 272 ofdisk_strategy(struct buf *bp) 273 { 274 struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(bp->b_dev)]; 275 struct partition *p; 276 u_quad_t off; 277 int read; 278 int (*OF_io)(int, void *, int); 279 daddr_t blkno = bp->b_blkno; 280 281 bp->b_resid = 0; 282 if (bp->b_bcount == 0) 283 goto done; 284 285 OF_io = bp->b_flags & B_READ ? OF_read : 286 (int(*)(int, void*, int))OF_write; 287 288 if (DISKPART(bp->b_dev) != RAW_PART) { 289 if (bounds_check_with_label(&of->sc_dk, bp, 0) <= 0) { 290 bp->b_resid = bp->b_bcount; 291 goto done; 292 } 293 p = &of->sc_dk.dk_label->d_partitions[DISKPART(bp->b_dev)]; 294 blkno = bp->b_blkno + p->p_offset; 295 } 296 297 disk_busy(&of->sc_dk); 298 299 off = (u_quad_t)blkno * DEV_BSIZE; 300 read = -1; 301 do { 302 if (OF_seek(of->sc_ihandle, off) < 0) 303 break; 304 read = OF_io(of->sc_ihandle, bp->b_data, bp->b_bcount); 305 } while (read == -2); 306 307 if (read < 0) { 308 bp->b_error = EIO; 309 bp->b_flags |= B_ERROR; 310 bp->b_resid = bp->b_bcount; 311 } else 312 bp->b_resid = bp->b_bcount - read; 313 314 disk_unbusy(&of->sc_dk, bp->b_bcount - bp->b_resid, 315 (bp->b_flags & B_READ)); 316 317 done: 318 biodone(bp); 319 } 320 321 static void 322 ofminphys(struct buf *bp) 323 { 324 struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(bp->b_dev)]; 325 326 if (bp->b_bcount > of->max_transfer) 327 bp->b_bcount = of->max_transfer; 328 } 329 330 int 331 ofdisk_read(dev_t dev, struct uio *uio, int flags) 332 { 333 return physio(ofdisk_strategy, NULL, dev, B_READ, ofminphys, uio); 334 } 335 336 int 337 ofdisk_write(dev_t dev, struct uio *uio, int flags) 338 { 339 return physio(ofdisk_strategy, NULL, dev, B_WRITE, ofminphys, uio); 340 } 341 342 int 343 ofdisk_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l) 344 { 345 struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(dev)]; 346 int error; 347 #ifdef __HAVE_OLD_DISKLABEL 348 struct disklabel newlabel; 349 #endif 350 351 switch (cmd) { 352 case DIOCGDINFO: 353 *(struct disklabel *)data = *of->sc_dk.dk_label; 354 return 0; 355 #ifdef __HAVE_OLD_DISKLABEL 356 case ODIOCGDINFO: 357 newlabel = *of->sc_dk.dk_label; 358 if (newlabel.d_npartitions > OLDMAXPARTITIONS) 359 return ENOTTY; 360 memcpy(data, &newlabel, sizeof (struct olddisklabel)); 361 return 0; 362 #endif 363 364 case DIOCGPART: 365 ((struct partinfo *)data)->disklab = of->sc_dk.dk_label; 366 ((struct partinfo *)data)->part = 367 &of->sc_dk.dk_label->d_partitions[DISKPART(dev)]; 368 return 0; 369 370 case DIOCWDINFO: 371 case DIOCSDINFO: 372 #ifdef __HAVE_OLD_DISKLABEL 373 case ODIOCWDINFO: 374 case ODIOCSDINFO: 375 #endif 376 { 377 struct disklabel *lp; 378 379 #ifdef __HAVE_OLD_DISKLABEL 380 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) { 381 memset(&newlabel, 0, sizeof newlabel); 382 memcpy(&newlabel, data, sizeof (struct olddisklabel)); 383 lp = &newlabel; 384 } else 385 #endif 386 lp = (struct disklabel *)data; 387 388 if ((flag & FWRITE) == 0) 389 return EBADF; 390 391 if ((error = lockmgr(&of->sc_dk.dk_openlock, LK_EXCLUSIVE, 392 NULL)) != 0) 393 return (error); 394 395 error = setdisklabel(of->sc_dk.dk_label, 396 lp, /*of->sc_dk.dk_openmask */0, 397 of->sc_dk.dk_cpulabel); 398 if (error == 0 && cmd == DIOCWDINFO 399 #ifdef __HAVE_OLD_DISKLABEL 400 || xfer == ODIOCWDINFO 401 #endif 402 ) 403 error = writedisklabel(MAKEDISKDEV(major(dev), 404 DISKUNIT(dev), RAW_PART), ofdisk_strategy, 405 of->sc_dk.dk_label, of->sc_dk.dk_cpulabel); 406 407 (void) lockmgr(&of->sc_dk.dk_openlock, LK_RELEASE, NULL); 408 409 return error; 410 } 411 412 case DIOCGDEFLABEL: 413 ofdisk_getdefaultlabel(of, (struct disklabel *)data); 414 return 0; 415 #ifdef __HAVE_OLD_DISKLABEL 416 case DIOCGDEFLABEL: 417 ofdisk_getdefaultlabel(of, &newlabel); 418 if (newlabel.d_npartitions > OLDMAXPARTITIONS) 419 return ENOTTY; 420 memcpy(data, &newlabel, sizeof (struct olddisklabel)); 421 return 0; 422 #endif 423 424 case DIOCAWEDGE: 425 { 426 struct dkwedge_info *dkw = (void *) data; 427 428 if (OFDISK_FLOPPY_P(of)) 429 return (ENOTTY); 430 431 if ((flag & FWRITE) == 0) 432 return (EBADF); 433 434 /* If the ioctl happens here, the parent is us. */ 435 strcpy(dkw->dkw_parent, of->sc_dev.dv_xname); 436 return (dkwedge_add(dkw)); 437 } 438 439 case DIOCDWEDGE: 440 { 441 struct dkwedge_info *dkw = (void *) data; 442 443 if (OFDISK_FLOPPY_P(of)) 444 return (ENOTTY); 445 446 if ((flag & FWRITE) == 0) 447 return (EBADF); 448 449 /* If the ioctl happens here, the parent is us. */ 450 strcpy(dkw->dkw_parent, of->sc_dev.dv_xname); 451 return (dkwedge_del(dkw)); 452 } 453 454 case DIOCLWEDGES: 455 { 456 struct dkwedge_list *dkwl = (void *) data; 457 458 if (OFDISK_FLOPPY_P(of)) 459 return (ENOTTY); 460 461 return (dkwedge_list(&of->sc_dk, dkwl, l)); 462 } 463 464 default: 465 return ENOTTY; 466 } 467 } 468 469 int 470 ofdisk_dump(dev_t dev, daddr_t blkno, caddr_t va, size_t size) 471 { 472 return EINVAL; 473 } 474 475 int 476 ofdisk_size(dev_t dev) 477 { 478 struct ofdisk_softc *of; 479 struct disklabel *lp; 480 int size, part, omask, unit; 481 482 unit = DISKUNIT(dev); 483 if (unit >= ofdisk_cd.cd_ndevs || 484 (of = ofdisk_cd.cd_devs[unit]) == NULL) 485 return -1; 486 487 part = DISKPART(dev); 488 omask = of->sc_dk.dk_openmask & (1 << part); 489 lp = of->sc_dk.dk_label; 490 491 if (omask == 0 && ofdisk_open(dev, 0, S_IFBLK, curlwp) != 0) 492 return -1; 493 494 if (lp->d_partitions[part].p_fstype != FS_SWAP) 495 size = -1; 496 else 497 size = lp->d_partitions[part].p_size * 498 (lp->d_secsize / DEV_BSIZE); 499 500 if (omask == 0 && ofdisk_close(dev, 0, S_IFBLK, curlwp) != 0) 501 return -1; 502 503 return size; 504 } 505 506 void 507 ofdisk_getdefaultlabel(struct ofdisk_softc *of, struct disklabel *lp) 508 { 509 510 memset(lp, 0, sizeof *lp); 511 512 /* 513 * XXX Firmware bug? Asking for block size gives a 514 * XXX ridiculous number! So we use what the boot program 515 * XXX uses. 516 */ 517 lp->d_secsize = DEV_BSIZE; 518 519 lp->d_secperunit = OF_call_method_1("#blocks", 520 of->sc_ihandle, 0); 521 if (lp->d_secperunit == (u_int32_t)-1) 522 lp->d_secperunit = 0x7fffffff; 523 524 lp->d_secpercyl = 1; 525 lp->d_nsectors = 1; 526 lp->d_ntracks = 1; 527 lp->d_ncylinders = lp->d_secperunit; 528 529 lp->d_partitions[RAW_PART].p_offset = 0; 530 lp->d_partitions[RAW_PART].p_size = lp->d_secperunit; 531 lp->d_npartitions = RAW_PART + 1; 532 533 lp->d_magic = DISKMAGIC; 534 lp->d_magic2 = DISKMAGIC; 535 lp->d_checksum = dkcksum(lp); 536 } 537 538 void 539 ofdisk_getdisklabel(dev) 540 dev_t dev; 541 { 542 int unit = DISKUNIT(dev); 543 struct ofdisk_softc *of = ofdisk_cd.cd_devs[unit]; 544 struct disklabel *lp = of->sc_dk.dk_label; 545 const char *errmes; 546 int l; 547 548 ofdisk_getdefaultlabel(of, lp); 549 550 /* 551 * Don't read the disklabel on a floppy; simply 552 * assign all partitions the same size/offset as 553 * RAW_PART. (This is essentially what the ISA 554 * floppy driver does, but we don't deal with 555 * density stuff.) 556 */ 557 if (OFDISK_FLOPPY_P(of)) { 558 lp->d_npartitions = MAXPARTITIONS; 559 for (l = 0; l < lp->d_npartitions; l++) { 560 if (l == RAW_PART) 561 continue; 562 /* struct copy */ 563 lp->d_partitions[l] = 564 lp->d_partitions[RAW_PART]; 565 } 566 lp->d_checksum = dkcksum(lp); 567 } else { 568 errmes = readdisklabel(MAKEDISKDEV(major(dev), 569 unit, RAW_PART), ofdisk_strategy, lp, 570 of->sc_dk.dk_cpulabel); 571 if (errmes != NULL) 572 printf("%s: %s\n", of->sc_dev.dv_xname, errmes); 573 } 574 } 575