1 /* $NetBSD: mscp_tape.c,v 1.39 2012/10/27 17:18:27 chs 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.39 2012/10/27 17:18:27 chs 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 <sys/bus.h> 60 #include <sys/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 device_t 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(device_t, cfdata_t, void *); 85 void mtattach(device_t, device_t, void *); 86 void mtdgram(device_t, struct mscp *, struct mscp_softc *); 87 void mtiodone(device_t, struct buf *); 88 int mtonline(device_t, struct mscp *); 89 int mtgotstatus(device_t, struct mscp *); 90 int mtioerror(device_t, struct mscp *, struct buf *); 91 void mtfillin(struct buf *, struct mscp *); 92 int mtcmd(struct mt_softc *, int, int, int); 93 void mtcmddone(device_t, struct mscp *); 94 int mt_putonline(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_NEW(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(device_t parent, cfdata_t cf, void *aux) 141 { 142 struct drive_attach_args *da = aux; 143 struct mscp *mp = da->da_mp; 144 145 if ((da->da_typ & MSCPBUS_TAPE) == 0) 146 return 0; 147 if (cf->cf_loc[MSCPBUSCF_DRIVE] != MSCPBUSCF_DRIVE_DEFAULT && 148 cf->cf_loc[MSCPBUSCF_DRIVE] != mp->mscp_unit) 149 return 0; 150 return 1; 151 } 152 153 /* 154 * The attach routine only checks and prints drive type. 155 */ 156 void 157 mtattach(device_t parent, device_t self, void *aux) 158 { 159 struct mt_softc *mt = device_private(self); 160 struct drive_attach_args *da = aux; 161 struct mscp *mp = da->da_mp; 162 struct mscp_softc *mi = device_private(parent); 163 164 mt->mt_dev = self; 165 mt->mt_hwunit = mp->mscp_unit; 166 mi->mi_dp[mp->mscp_unit] = self; 167 168 disk_printtype(mp->mscp_unit, mp->mscp_guse.guse_mediaid); 169 } 170 171 /* 172 * (Try to) put the drive online. This is done the first time the 173 * drive is opened, or if it has fallen offline. 174 */ 175 int 176 mt_putonline(struct mt_softc *mt) 177 { 178 struct mscp *mp; 179 struct mscp_softc *mi = 180 device_private(device_parent(mt->mt_dev)); 181 volatile int i; 182 183 ((volatile struct mt_softc *) mt)->mt_state = MT_OFFLINE; 184 mp = mscp_getcp(mi, MSCP_WAIT); 185 mp->mscp_opcode = M_OP_ONLINE; 186 mp->mscp_unit = mt->mt_hwunit; 187 mp->mscp_cmdref = (long)&mt->mt_state; 188 *mp->mscp_addr |= MSCP_OWN | MSCP_INT; 189 190 /* Poll away */ 191 i = bus_space_read_2(mi->mi_iot, mi->mi_iph, 0); 192 if (tsleep(&mt->mt_state, PRIBIO, "mtonline", 240 * hz)) 193 return MSCP_FAILED; 194 195 if ((volatile int)mt->mt_state != MT_ONLINE) 196 return MSCP_FAILED; 197 198 return MSCP_DONE; 199 } 200 /* 201 * Open a drive. 202 */ 203 /*ARGSUSED*/ 204 int 205 mtopen(dev_t dev, int flag, int fmt, struct lwp *l) 206 { 207 struct mt_softc *mt; 208 int unit; 209 210 /* 211 * Make sure this is a reasonable open request. 212 */ 213 unit = mtunit(dev); 214 mt = device_lookup_private(&mt_cd, unit); 215 if (!mt) 216 return ENXIO; 217 218 if (mt->mt_inuse) 219 return EBUSY; 220 mt->mt_inuse = 1; 221 222 if (mt_putonline(mt) == MSCP_FAILED) { 223 mt->mt_inuse = 0; 224 return EIO; 225 } 226 227 return 0; 228 } 229 230 /* ARGSUSED */ 231 int 232 mtclose(dev_t dev, int flags, int fmt, struct lwp *l) 233 { 234 int unit = mtunit(dev); 235 struct mt_softc *mt = device_lookup_private(&mt_cd, unit); 236 237 /* 238 * If we just have finished a writing, write EOT marks. 239 */ 240 if ((flags & FWRITE) && mt->mt_waswrite) { 241 mtcmd(mt, MTWEOF, 0, 0); 242 mtcmd(mt, MTWEOF, 0, 0); 243 mtcmd(mt, MTBSR, 1, 0); 244 } 245 if (mtnorewind(dev) == 0) 246 mtcmd(mt, MTREW, 0, 1); 247 if (mt->mt_serex) 248 mtcmd(mt, -1, 0, 0); 249 250 mt->mt_inuse = 0; /* Release the tape */ 251 return 0; 252 } 253 254 void 255 mtstrategy(struct buf *bp) 256 { 257 int unit; 258 struct mt_softc *mt; 259 260 /* 261 * Make sure this is a reasonable drive to use. 262 */ 263 unit = mtunit(bp->b_dev); 264 if ((mt = device_lookup_private(&mt_cd, unit)) == NULL) { 265 bp->b_error = ENXIO; 266 biodone(bp); 267 return; 268 } 269 270 mt->mt_waswrite = bp->b_flags & B_READ ? 0 : 1; 271 mscp_strategy(bp, device_parent(mt->mt_dev)); 272 return; 273 } 274 275 int 276 mtread(dev_t dev, struct uio *uio, int flag) 277 { 278 279 return (physio(mtstrategy, NULL, dev, B_READ, minphys, uio)); 280 } 281 282 int 283 mtwrite(dev_t dev, struct uio *uio, int flag) 284 { 285 286 return (physio(mtstrategy, NULL, dev, B_WRITE, minphys, uio)); 287 } 288 289 void 290 mtiodone(device_t usc, struct buf *bp) 291 { 292 293 biodone(bp); 294 } 295 296 /* 297 * Fill in drive addresses in a mscp packet waiting for transfer. 298 */ 299 void 300 mtfillin(struct buf *bp, struct mscp *mp) 301 { 302 int unit = mtunit(bp->b_dev); 303 struct mt_softc *mt = device_lookup_private(&mt_cd, unit); 304 305 mp->mscp_unit = mt->mt_hwunit; 306 if (mt->mt_serex == 2) { 307 mp->mscp_modifier = M_MD_CLSEX; 308 mt->mt_serex = 0; 309 } else 310 mp->mscp_modifier = 0; 311 312 mp->mscp_seq.seq_bytecount = bp->b_bcount; 313 } 314 315 /* 316 * Handle an error datagram. 317 */ 318 void 319 mtdgram(device_t usc, struct mscp *mp, struct mscp_softc *mi) 320 { 321 if (mscp_decodeerror(usc == NULL?"unconf mt" : device_xname(usc), mp, mi)) 322 return; 323 } 324 325 /* 326 * A drive came on line, make sure it really _is_ on line before 327 * trying to use it. 328 */ 329 int 330 mtonline(device_t usc, struct mscp *mp) 331 { 332 struct mt_softc *mt = (void *)usc; 333 334 wakeup((void *)&mt->mt_state); 335 if ((mp->mscp_status & M_ST_MASK) == M_ST_SUCCESS) 336 mt->mt_state = MT_ONLINE; 337 338 return (MSCP_DONE); 339 } 340 341 /* 342 * We got some (configured) unit's status. Return DONE. 343 */ 344 int 345 mtgotstatus(device_t usc, struct mscp *mp) 346 { 347 return (MSCP_DONE); 348 } 349 350 static const char *mt_ioerrs[] = { 351 "invalid command", /* 1 M_ST_INVALCMD */ 352 "command aborted", /* 2 M_ST_ABORTED */ 353 "unit offline", /* 3 M_ST_OFFLINE */ 354 "unknown", /* 4 M_ST_AVAILABLE */ 355 "unknown", /* 5 M_ST_MFMTERR */ 356 "unit write protected", /* 6 M_ST_WRPROT */ 357 "compare error", /* 7 M_ST_COMPERR */ 358 "data error", /* 8 M_ST_DATAERR */ 359 "host buffer access error", /* 9 M_ST_HOSTBUFERR */ 360 "controller error", /* 10 M_ST_CTLRERR */ 361 "drive error", /* 11 M_ST_DRIVEERR */ 362 "formatter error", /* 12 M_ST_FORMATTERR */ 363 "BOT encountered", /* 13 M_ST_BOT */ 364 "tape mark encountered",/* 14 M_ST_TAPEMARK */ 365 "unknown", /* 15 */ 366 "record data truncated",/* 16 M_ST_RDTRUNC */ 367 }; 368 369 /* 370 * An I/O error, may be because of a tapemark encountered. 371 * Check that before failing. 372 */ 373 /*ARGSUSED*/ 374 int 375 mtioerror(device_t usc, struct mscp *mp, struct buf *bp) 376 { 377 struct mt_softc *mt = (void *)usc; 378 int st = mp->mscp_status & M_ST_MASK; 379 380 if (mp->mscp_flags & M_EF_SEREX) 381 mt->mt_serex = 1; 382 if (st == M_ST_TAPEMARK) 383 mt->mt_serex = 2; 384 else { 385 if (st && st < 17) 386 printf("%s: error %d (%s)\n", device_xname(mt->mt_dev), st, 387 mt_ioerrs[st-1]); 388 else 389 printf("%s: error %d\n", device_xname(mt->mt_dev), st); 390 bp->b_error = EROFS; 391 } 392 393 return (MSCP_DONE); 394 } 395 396 /* 397 * I/O controls. 398 */ 399 int 400 mtioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 401 { 402 int unit = mtunit(dev); 403 struct mt_softc *mt = device_lookup_private(&mt_cd, unit); 404 struct mtop *mtop; 405 int error = 0; 406 407 switch (cmd) { 408 409 case MTIOCTOP: 410 mtop = (void *)data; 411 if (mtop->mt_op == MTWEOF) { 412 while (mtop->mt_count-- > 0) 413 if ((error = mtcmd(mt, mtop->mt_op, 0, 0))) 414 break; 415 } else 416 error = mtcmd(mt, mtop->mt_op, mtop->mt_count, 0); 417 418 case MTIOCGET: 419 ((struct mtget *)data)->mt_type = MT_ISTMSCP; 420 /* XXX we need to fill in more fields here */ 421 break; 422 423 default: 424 error = ENXIO; 425 break; 426 } 427 return (error); 428 } 429 430 /* 431 * No crash dump support... 432 */ 433 int 434 mtdump(dev_t dev, daddr_t blkno, void *va, size_t size) 435 { 436 return -1; 437 } 438 439 /* 440 * Send a command to the tape drive. Wait until the command is 441 * finished before returning. 442 * This routine must only be called when there are no data transfer 443 * active on this device. Can we be sure of this? Or does the ctlr 444 * queue up all command packets and take them in sequential order? 445 * It sure would be nice if my manual stated this... /ragge 446 */ 447 int 448 mtcmd(struct mt_softc *mt, int cmd, int count, int complete) 449 { 450 struct mscp *mp; 451 struct mscp_softc *mi = device_private(device_parent(mt->mt_dev)); 452 volatile int i; 453 454 mp = mscp_getcp(mi, MSCP_WAIT); 455 456 mt->mt_ioctlerr = 0; 457 mp->mscp_unit = mt->mt_hwunit; 458 mp->mscp_cmdref = -1; 459 *mp->mscp_addr |= MSCP_OWN | MSCP_INT; 460 461 switch (cmd) { 462 case MTWEOF: 463 mp->mscp_opcode = M_OP_WRITM; 464 break; 465 466 case MTBSF: 467 mp->mscp_modifier = M_MD_REVERSE; 468 case MTFSF: 469 mp->mscp_opcode = M_OP_POS; 470 mp->mscp_seq.seq_buffer = count; 471 break; 472 473 case MTBSR: 474 mp->mscp_modifier = M_MD_REVERSE; 475 case MTFSR: 476 mp->mscp_opcode = M_OP_POS; 477 mp->mscp_modifier |= M_MD_OBJCOUNT; 478 mp->mscp_seq.seq_bytecount = count; 479 break; 480 481 case MTREW: 482 mp->mscp_opcode = M_OP_POS; 483 mp->mscp_modifier = M_MD_REWIND | M_MD_CLSEX; 484 if (complete) 485 mp->mscp_modifier |= M_MD_IMMEDIATE; 486 mt->mt_serex = 0; 487 break; 488 489 case MTOFFL: 490 mp->mscp_opcode = M_OP_AVAILABLE; 491 mp->mscp_modifier = M_MD_UNLOAD | M_MD_CLSEX; 492 mt->mt_serex = 0; 493 break; 494 495 case MTNOP: 496 mp->mscp_opcode = M_OP_GETUNITST; 497 break; 498 499 case -1: /* Clear serious exception only */ 500 mp->mscp_opcode = M_OP_POS; 501 mp->mscp_modifier = M_MD_CLSEX; 502 mt->mt_serex = 0; 503 break; 504 505 default: 506 printf("Bad ioctl %x\n", cmd); 507 mp->mscp_opcode = M_OP_POS; 508 break; 509 } 510 511 i = bus_space_read_2(mi->mi_iot, mi->mi_iph, 0); 512 tsleep(&mt->mt_inuse, PRIBIO, "mtioctl", 0); 513 return mt->mt_ioctlerr; 514 } 515 516 /* 517 * Called from bus routines whenever a non-data transfer is finished. 518 */ 519 void 520 mtcmddone(device_t usc, struct mscp *mp) 521 { 522 struct mt_softc *mt = (void *)usc; 523 524 if (mp->mscp_status) { 525 mt->mt_ioctlerr = EIO; 526 printf("%s: bad status %x\n", device_xname(mt->mt_dev), 527 mp->mscp_status); 528 } 529 wakeup(&mt->mt_inuse); 530 } 531