1 /* $NetBSD: ofdisk.c,v 1.54 2020/01/26 21:43:52 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.54 2020/01/26 21:43:52 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 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") == 0 || strcmp(type, "scsi") == 0; 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 DIOCRMWEDGES: 371 case DIOCMWEDGES: 372 if (OFDISK_FLOPPY_P(of)) 373 return ENOTTY; 374 } 375 376 error = disk_ioctl(&of->sc_dk, dev, cmd, data, flag, l); 377 if (error != EPASSTHROUGH) 378 return error; 379 380 switch (cmd) { 381 case DIOCWDINFO: 382 case DIOCSDINFO: 383 #ifdef __HAVE_OLD_DISKLABEL 384 case ODIOCWDINFO: 385 case ODIOCSDINFO: 386 #endif 387 { 388 struct disklabel *lp; 389 390 #ifdef __HAVE_OLD_DISKLABEL 391 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) { 392 memset(&newlabel, 0, sizeof newlabel); 393 memcpy(&newlabel, data, sizeof (struct olddisklabel)); 394 lp = &newlabel; 395 } else 396 #endif 397 lp = (struct disklabel *)data; 398 399 if ((flag & FWRITE) == 0) 400 return EBADF; 401 402 mutex_enter(&of->sc_dk.dk_openlock); 403 404 error = setdisklabel(of->sc_dk.dk_label, 405 lp, /*of->sc_dk.dk_openmask */0, 406 of->sc_dk.dk_cpulabel); 407 if (error == 0 && (cmd == DIOCWDINFO 408 #ifdef __HAVE_OLD_DISKLABEL 409 || cmd == ODIOCWDINFO 410 #endif 411 )) 412 error = writedisklabel(MAKEDISKDEV(major(dev), 413 DISKUNIT(dev), RAW_PART), ofdisk_strategy, 414 of->sc_dk.dk_label, of->sc_dk.dk_cpulabel); 415 416 mutex_exit(&of->sc_dk.dk_openlock); 417 418 return error; 419 } 420 421 case DIOCGDEFLABEL: 422 ofdisk_getdefaultlabel(of, (struct disklabel *)data); 423 return 0; 424 #ifdef __HAVE_OLD_DISKLABEL 425 case ODIOCGDEFLABEL: 426 ofdisk_getdefaultlabel(of, &newlabel); 427 if (newlabel.d_npartitions > OLDMAXPARTITIONS) 428 return ENOTTY; 429 memcpy(data, &newlabel, sizeof (struct olddisklabel)); 430 return 0; 431 #endif 432 433 default: 434 return ENOTTY; 435 } 436 } 437 438 int 439 ofdisk_dump(dev_t dev, daddr_t blkno, void *va, size_t size) 440 { 441 return EINVAL; 442 } 443 444 int 445 ofdisk_size(dev_t dev) 446 { 447 struct ofdisk_softc *of; 448 struct disklabel *lp; 449 int size, part, omask; 450 451 of = device_lookup_private(&ofdisk_cd, DISKUNIT(dev)); 452 if (of == NULL) 453 return ENXIO; 454 455 part = DISKPART(dev); 456 omask = of->sc_dk.dk_openmask & (1 << part); 457 lp = of->sc_dk.dk_label; 458 459 if (omask == 0 && ofdisk_open(dev, 0, S_IFBLK, curlwp) != 0) 460 return -1; 461 462 if (lp->d_partitions[part].p_fstype != FS_SWAP) 463 size = -1; 464 else 465 size = lp->d_partitions[part].p_size * 466 (lp->d_secsize / DEV_BSIZE); 467 468 if (omask == 0 && ofdisk_close(dev, 0, S_IFBLK, curlwp) != 0) 469 return -1; 470 471 return size; 472 } 473 474 void 475 ofdisk_getdefaultlabel(struct ofdisk_softc *of, struct disklabel *lp) 476 { 477 478 memset(lp, 0, sizeof *lp); 479 480 /* 481 * XXX Firmware bug? Asking for block size gives a 482 * XXX ridiculous number! So we use what the boot program 483 * XXX uses. 484 */ 485 lp->d_secsize = DEV_BSIZE; 486 487 lp->d_secperunit = OF_call_method_1("#blocks", 488 of->sc_ihandle, 0); 489 if (lp->d_secperunit == (u_int32_t)-1) 490 lp->d_secperunit = 0x7fffffff; 491 492 lp->d_secpercyl = 1; 493 lp->d_nsectors = 1; 494 lp->d_ntracks = 1; 495 lp->d_ncylinders = lp->d_secperunit; 496 497 lp->d_partitions[RAW_PART].p_offset = 0; 498 lp->d_partitions[RAW_PART].p_size = lp->d_secperunit; 499 lp->d_npartitions = RAW_PART + 1; 500 501 lp->d_magic = DISKMAGIC; 502 lp->d_magic2 = DISKMAGIC; 503 lp->d_checksum = dkcksum(lp); 504 } 505 506 void 507 ofdisk_getdisklabel(dev_t dev) 508 { 509 int unit = DISKUNIT(dev); 510 struct ofdisk_softc *of = 511 device_lookup_private(&ofdisk_cd, unit); 512 struct disklabel *lp = of->sc_dk.dk_label; 513 const char *errmes; 514 int l; 515 516 ofdisk_getdefaultlabel(of, lp); 517 518 /* 519 * Don't read the disklabel on a floppy; simply 520 * assign all partitions the same size/offset as 521 * RAW_PART. (This is essentially what the ISA 522 * floppy driver does, but we don't deal with 523 * density stuff.) 524 */ 525 if (OFDISK_FLOPPY_P(of)) { 526 lp->d_npartitions = MAXPARTITIONS; 527 for (l = 0; l < lp->d_npartitions; l++) { 528 if (l == RAW_PART) 529 continue; 530 /* struct copy */ 531 lp->d_partitions[l] = 532 lp->d_partitions[RAW_PART]; 533 } 534 lp->d_checksum = dkcksum(lp); 535 } else { 536 errmes = readdisklabel(MAKEDISKDEV(major(dev), 537 unit, RAW_PART), ofdisk_strategy, lp, 538 of->sc_dk.dk_cpulabel); 539 if (errmes != NULL) 540 printf("%s: %s\n", device_xname(of->sc_dev), errmes); 541 } 542 } 543