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