1 /* $NetBSD: mscp_tape.c,v 1.22 2004/10/31 12:52:55 he Exp $ */ 2 /* 3 * Copyright (c) 1996 Ludd, University of Lule}, Sweden. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed at Ludd, University of 17 * Lule}, Sweden and its contributors. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 34 /* 35 * MSCP tape device driver 36 */ 37 38 /* 39 * TODO 40 * Write status handling code. 41 */ 42 43 #include <sys/cdefs.h> 44 __KERNEL_RCSID(0, "$NetBSD: mscp_tape.c,v 1.22 2004/10/31 12:52:55 he Exp $"); 45 46 #include <sys/param.h> 47 #include <sys/device.h> 48 #include <sys/kernel.h> 49 #include <sys/buf.h> 50 #include <sys/bufq.h> 51 #include <sys/ioccom.h> 52 #include <sys/mtio.h> 53 #include <sys/fcntl.h> 54 #include <sys/malloc.h> 55 #include <sys/systm.h> 56 #include <sys/proc.h> 57 #include <sys/conf.h> 58 59 #include <machine/bus.h> 60 #include <machine/cpu.h> 61 62 #include <dev/mscp/mscp.h> 63 #include <dev/mscp/mscpreg.h> 64 #include <dev/mscp/mscpvar.h> 65 66 #include "locators.h" 67 68 /* 69 * Drive status, per drive 70 */ 71 struct mt_softc { 72 struct device mt_dev; /* Autoconf struct */ 73 int mt_state; /* open/closed state */ 74 int mt_hwunit; /* Hardware unit number */ 75 int mt_inuse; /* Locks the tape drive for others */ 76 int mt_waswrite; /* Last operation was a write op */ 77 int mt_serex; /* Got serious exception */ 78 int mt_ioctlerr; /* Error after last ioctl */ 79 }; 80 81 #define MT_OFFLINE 0 82 #define MT_ONLINE 1 83 84 int mtmatch __P((struct device *, struct cfdata *, void *)); 85 void mtattach __P((struct device *, struct device *, void *)); 86 void mtdgram __P((struct device *, struct mscp *, struct mscp_softc *)); 87 void mtiodone __P((struct device *, struct buf *)); 88 int mtonline __P((struct device *, struct mscp *)); 89 int mtgotstatus __P((struct device *, struct mscp *)); 90 int mtioerror __P((struct device *, struct mscp *, struct buf *)); 91 void mtfillin __P((struct buf *, struct mscp *)); 92 int mtcmd __P((struct mt_softc *, int, int, int)); 93 void mtcmddone __P((struct device *, struct mscp *)); 94 int mt_putonline __P((struct mt_softc *)); 95 96 struct mscp_device mt_device = { 97 mtdgram, 98 mtiodone, 99 mtonline, 100 mtgotstatus, 101 0, 102 mtioerror, 103 0, 104 mtfillin, 105 mtcmddone, 106 }; 107 108 /* This is not good, should allow more than 4 tapes/device type */ 109 #define mtunit(dev) (minor(dev) & T_UNIT) 110 #define mtnorewind(dev) (dev & T_NOREWIND) 111 #define mthdensity(dev) (dev & T_1600BPI) 112 113 CFATTACH_DECL(mt, sizeof(struct mt_softc), 114 mtmatch, mtattach, NULL, NULL); 115 116 extern struct cfdriver mt_cd; 117 118 dev_type_open(mtopen); 119 dev_type_close(mtclose); 120 dev_type_read(mtread); 121 dev_type_write(mtwrite); 122 dev_type_ioctl(mtioctl); 123 dev_type_strategy(mtstrategy); 124 dev_type_dump(mtdump); 125 126 const struct bdevsw mt_bdevsw = { 127 mtopen, mtclose, mtstrategy, mtioctl, mtdump, nosize, D_TAPE 128 }; 129 130 const struct cdevsw mt_cdevsw = { 131 mtopen, mtclose, mtread, mtwrite, mtioctl, 132 nostop, notty, nopoll, nommap, nokqfilter, D_TAPE 133 }; 134 135 /* 136 * More driver definitions, for generic MSCP code. 137 */ 138 139 int 140 mtmatch(parent, cf, aux) 141 struct device *parent; 142 struct cfdata *cf; 143 void *aux; 144 { 145 struct drive_attach_args *da = aux; 146 struct mscp *mp = da->da_mp; 147 148 if ((da->da_typ & MSCPBUS_TAPE) == 0) 149 return 0; 150 if (cf->cf_loc[MSCPBUSCF_DRIVE] != MSCPBUSCF_DRIVE_DEFAULT && 151 cf->cf_loc[MSCPBUSCF_DRIVE] != mp->mscp_unit) 152 return 0; 153 return 1; 154 } 155 156 /* 157 * The attach routine only checks and prints drive type. 158 */ 159 void 160 mtattach(parent, self, aux) 161 struct device *parent, *self; 162 void *aux; 163 { 164 struct mt_softc *mt = (void *)self; 165 struct drive_attach_args *da = aux; 166 struct mscp *mp = da->da_mp; 167 struct mscp_softc *mi = (void *)parent; 168 169 mt->mt_hwunit = mp->mscp_unit; 170 mi->mi_dp[mp->mscp_unit] = self; 171 172 disk_printtype(mp->mscp_unit, mp->mscp_guse.guse_mediaid); 173 } 174 175 /* 176 * (Try to) put the drive online. This is done the first time the 177 * drive is opened, or if it has fallen offline. 178 */ 179 int 180 mt_putonline(mt) 181 struct mt_softc *mt; 182 { 183 struct mscp *mp; 184 struct mscp_softc *mi = (struct mscp_softc *)mt->mt_dev.dv_parent; 185 volatile int i; 186 187 (volatile int)mt->mt_state = MT_OFFLINE; 188 mp = mscp_getcp(mi, MSCP_WAIT); 189 mp->mscp_opcode = M_OP_ONLINE; 190 mp->mscp_unit = mt->mt_hwunit; 191 mp->mscp_cmdref = (long)&mt->mt_state; 192 *mp->mscp_addr |= MSCP_OWN | MSCP_INT; 193 194 /* Poll away */ 195 i = bus_space_read_2(mi->mi_iot, mi->mi_iph, 0); 196 if (tsleep(&mt->mt_state, PRIBIO, "mtonline", 240 * hz)) 197 return MSCP_FAILED; 198 199 if ((volatile int)mt->mt_state != MT_ONLINE) 200 return MSCP_FAILED; 201 202 return MSCP_DONE; 203 } 204 /* 205 * Open a drive. 206 */ 207 /*ARGSUSED*/ 208 int 209 mtopen(dev, flag, fmt, p) 210 dev_t dev; 211 int flag, fmt; 212 struct proc *p; 213 { 214 struct mt_softc *mt; 215 int unit; 216 217 /* 218 * Make sure this is a reasonable open request. 219 */ 220 unit = mtunit(dev); 221 if (unit >= mt_cd.cd_ndevs) 222 return ENXIO; 223 mt = mt_cd.cd_devs[unit]; 224 if (mt == 0) 225 return ENXIO; 226 227 if (mt->mt_inuse) 228 return EBUSY; 229 mt->mt_inuse = 1; 230 231 if (mt_putonline(mt) == MSCP_FAILED) { 232 mt->mt_inuse = 0; 233 return EIO; 234 } 235 236 return 0; 237 } 238 239 /* ARGSUSED */ 240 int 241 mtclose(dev, flags, fmt, p) 242 dev_t dev; 243 int flags, fmt; 244 struct proc *p; 245 { 246 int unit = mtunit(dev); 247 struct mt_softc *mt = mt_cd.cd_devs[unit]; 248 249 /* 250 * If we just have finished a writing, write EOT marks. 251 */ 252 if ((flags & FWRITE) && mt->mt_waswrite) { 253 mtcmd(mt, MTWEOF, 0, 0); 254 mtcmd(mt, MTWEOF, 0, 0); 255 mtcmd(mt, MTBSR, 1, 0); 256 } 257 if (mtnorewind(dev) == 0) 258 mtcmd(mt, MTREW, 0, 1); 259 if (mt->mt_serex) 260 mtcmd(mt, -1, 0, 0); 261 262 mt->mt_inuse = 0; /* Release the tape */ 263 return 0; 264 } 265 266 void 267 mtstrategy(bp) 268 struct buf *bp; 269 { 270 int unit; 271 struct mt_softc *mt; 272 273 /* 274 * Make sure this is a reasonable drive to use. 275 */ 276 unit = mtunit(bp->b_dev); 277 if (unit > mt_cd.cd_ndevs || (mt = mt_cd.cd_devs[unit]) == NULL) { 278 bp->b_error = ENXIO; 279 goto bad; 280 } 281 282 mt->mt_waswrite = bp->b_flags & B_READ ? 0 : 1; 283 mscp_strategy(bp, mt->mt_dev.dv_parent); 284 return; 285 286 bad: 287 bp->b_flags |= B_ERROR; 288 biodone(bp); 289 } 290 291 int 292 mtread(dev, uio, flag) 293 dev_t dev; 294 struct uio *uio; 295 int flag; 296 { 297 298 return (physio(mtstrategy, NULL, dev, B_READ, minphys, uio)); 299 } 300 301 int 302 mtwrite(dev, uio, flag) 303 dev_t dev; 304 struct uio *uio; 305 int flag; 306 { 307 308 return (physio(mtstrategy, NULL, dev, B_WRITE, minphys, uio)); 309 } 310 311 void 312 mtiodone(usc, bp) 313 struct device *usc; 314 struct buf *bp; 315 { 316 317 biodone(bp); 318 } 319 320 /* 321 * Fill in drive addresses in a mscp packet waiting for transfer. 322 */ 323 void 324 mtfillin(bp, mp) 325 struct buf *bp; 326 struct mscp *mp; 327 { 328 int unit = mtunit(bp->b_dev); 329 struct mt_softc *mt = mt_cd.cd_devs[unit]; 330 331 mp->mscp_unit = mt->mt_hwunit; 332 if (mt->mt_serex == 2) { 333 mp->mscp_modifier = M_MD_CLSEX; 334 mt->mt_serex = 0; 335 } else 336 mp->mscp_modifier = 0; 337 338 mp->mscp_seq.seq_bytecount = bp->b_bcount; 339 } 340 341 /* 342 * Handle an error datagram. 343 */ 344 void 345 mtdgram(usc, mp, mi) 346 struct device *usc; 347 struct mscp *mp; 348 struct mscp_softc *mi; 349 { 350 if (mscp_decodeerror(usc == NULL?"unconf mt" : usc->dv_xname, mp, mi)) 351 return; 352 } 353 354 /* 355 * A drive came on line, make sure it really _is_ on line before 356 * trying to use it. 357 */ 358 int 359 mtonline(usc, mp) 360 struct device *usc; 361 struct mscp *mp; 362 { 363 struct mt_softc *mt = (void *)usc; 364 365 wakeup((caddr_t)&mt->mt_state); 366 if ((mp->mscp_status & M_ST_MASK) == M_ST_SUCCESS) 367 mt->mt_state = MT_ONLINE; 368 369 return (MSCP_DONE); 370 } 371 372 /* 373 * We got some (configured) unit's status. Return DONE. 374 */ 375 int 376 mtgotstatus(usc, mp) 377 struct device *usc; 378 struct mscp *mp; 379 { 380 return (MSCP_DONE); 381 } 382 383 static char *mt_ioerrs[] = { 384 "invalid command", /* 1 M_ST_INVALCMD */ 385 "command aborted", /* 2 M_ST_ABORTED */ 386 "unit offline", /* 3 M_ST_OFFLINE */ 387 "unknown", /* 4 M_ST_AVAILABLE */ 388 "unknown", /* 5 M_ST_MFMTERR */ 389 "unit write protected", /* 6 M_ST_WRPROT */ 390 "compare error", /* 7 M_ST_COMPERR */ 391 "data error", /* 8 M_ST_DATAERR */ 392 "host buffer access error", /* 9 M_ST_HOSTBUFERR */ 393 "controller error", /* 10 M_ST_CTLRERR */ 394 "drive error", /* 11 M_ST_DRIVEERR */ 395 "formatter error", /* 12 M_ST_FORMATTERR */ 396 "BOT encountered", /* 13 M_ST_BOT */ 397 "tape mark encountered",/* 14 M_ST_TAPEMARK */ 398 "unknown", /* 15 */ 399 "record data truncated",/* 16 M_ST_RDTRUNC */ 400 }; 401 402 /* 403 * An I/O error, may be because of a tapemark encountered. 404 * Check that before failing. 405 */ 406 /*ARGSUSED*/ 407 int 408 mtioerror(usc, mp, bp) 409 struct device *usc; 410 struct mscp *mp; 411 struct buf *bp; 412 { 413 struct mt_softc *mt = (void *)usc; 414 int st = mp->mscp_status & M_ST_MASK; 415 416 if (mp->mscp_flags & M_EF_SEREX) 417 mt->mt_serex = 1; 418 if (st == M_ST_TAPEMARK) 419 mt->mt_serex = 2; 420 else { 421 if (st && st < 17) 422 printf("%s: error %d (%s)\n", mt->mt_dev.dv_xname, st, 423 mt_ioerrs[st-1]); 424 else 425 printf("%s: error %d\n", mt->mt_dev.dv_xname, st); 426 bp->b_flags |= B_ERROR; 427 bp->b_error = EROFS; 428 } 429 430 return (MSCP_DONE); 431 } 432 433 /* 434 * I/O controls. 435 */ 436 int 437 mtioctl(dev, cmd, data, flag, p) 438 dev_t dev; 439 u_long cmd; 440 caddr_t data; 441 int flag; 442 struct proc *p; 443 { 444 int unit = mtunit(dev); 445 struct mt_softc *mt = mt_cd.cd_devs[unit]; 446 struct mtop *mtop; 447 struct mtget *mtget; 448 int error = 0, count; 449 450 count = mtop->mt_count; 451 452 switch (cmd) { 453 454 case MTIOCTOP: 455 mtop = (void *)data; 456 if (mtop->mt_op == MTWEOF) { 457 while (mtop->mt_count-- > 0) 458 if ((error = mtcmd(mt, mtop->mt_op, 0, 0))) 459 break; 460 } else 461 error = mtcmd(mt, mtop->mt_op, mtop->mt_count, 0); 462 463 case MTIOCGET: 464 mtget = (void *)data; 465 mtget->mt_type = MT_ISTMSCP; 466 /* XXX we need to fill in more fields here */ 467 break; 468 469 default: 470 error = ENXIO; 471 break; 472 } 473 return (error); 474 } 475 476 /* 477 * No crash dump support... 478 */ 479 int 480 mtdump(dev, blkno, va, size) 481 dev_t dev; 482 daddr_t blkno; 483 caddr_t va; 484 size_t size; 485 { 486 return -1; 487 } 488 489 /* 490 * Send a command to the tape drive. Wait until the command is 491 * finished before returning. 492 * This routine must only be called when there are no data transfer 493 * active on this device. Can we be sure of this? Or does the ctlr 494 * queue up all command packets and take them in sequential order? 495 * It sure would be nice if my manual stated this... /ragge 496 */ 497 int 498 mtcmd(mt, cmd, count, complete) 499 struct mt_softc *mt; 500 int cmd, count, complete; 501 { 502 struct mscp *mp; 503 struct mscp_softc *mi = (void *)mt->mt_dev.dv_parent; 504 volatile int i; 505 506 mp = mscp_getcp(mi, MSCP_WAIT); 507 508 mt->mt_ioctlerr = 0; 509 mp->mscp_unit = mt->mt_hwunit; 510 mp->mscp_cmdref = -1; 511 *mp->mscp_addr |= MSCP_OWN | MSCP_INT; 512 513 switch (cmd) { 514 case MTWEOF: 515 mp->mscp_opcode = M_OP_WRITM; 516 break; 517 518 case MTBSF: 519 mp->mscp_modifier = M_MD_REVERSE; 520 case MTFSF: 521 mp->mscp_opcode = M_OP_POS; 522 mp->mscp_seq.seq_buffer = count; 523 break; 524 525 case MTBSR: 526 mp->mscp_modifier = M_MD_REVERSE; 527 case MTFSR: 528 mp->mscp_opcode = M_OP_POS; 529 mp->mscp_modifier |= M_MD_OBJCOUNT; 530 mp->mscp_seq.seq_bytecount = count; 531 break; 532 533 case MTREW: 534 mp->mscp_opcode = M_OP_POS; 535 mp->mscp_modifier = M_MD_REWIND | M_MD_CLSEX; 536 if (complete) 537 mp->mscp_modifier |= M_MD_IMMEDIATE; 538 mt->mt_serex = 0; 539 break; 540 541 case MTOFFL: 542 mp->mscp_opcode = M_OP_AVAILABLE; 543 mp->mscp_modifier = M_MD_UNLOAD | M_MD_CLSEX; 544 mt->mt_serex = 0; 545 break; 546 547 case MTNOP: 548 mp->mscp_opcode = M_OP_GETUNITST; 549 break; 550 551 case -1: /* Clear serious exception only */ 552 mp->mscp_opcode = M_OP_POS; 553 mp->mscp_modifier = M_MD_CLSEX; 554 mt->mt_serex = 0; 555 break; 556 557 default: 558 printf("Bad ioctl %x\n", cmd); 559 mp->mscp_opcode = M_OP_POS; 560 break; 561 } 562 563 i = bus_space_read_2(mi->mi_iot, mi->mi_iph, 0); 564 tsleep(&mt->mt_inuse, PRIBIO, "mtioctl", 0); 565 return mt->mt_ioctlerr; 566 } 567 568 /* 569 * Called from bus routines whenever a non-data transfer is finished. 570 */ 571 void 572 mtcmddone(usc, mp) 573 struct device *usc; 574 struct mscp *mp; 575 { 576 struct mt_softc *mt = (void *)usc; 577 578 if (mp->mscp_status) { 579 mt->mt_ioctlerr = EIO; 580 printf("%s: bad status %x\n", mt->mt_dev.dv_xname, 581 mp->mscp_status); 582 } 583 wakeup(&mt->mt_inuse); 584 } 585