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