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