ch.c revision 1.17
1/* $NetBSD: ch.c,v 1.17 1996/03/17 00:59:44 thorpej Exp $ */ 2 3/* 4 * Copyright (c) 1994 Charles Hannum. 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 by Charles Hannum. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32/* 33 * Originally written by grefen@????? 34 * Based on scsi drivers by Julian Elischer (julian@tfs.com) 35 */ 36 37#include <sys/types.h> 38#include <sys/param.h> 39#include <sys/systm.h> 40#include <sys/errno.h> 41#include <sys/ioctl.h> 42#include <sys/buf.h> 43#include <sys/proc.h> 44#include <sys/user.h> 45#include <sys/chio.h> 46#include <sys/device.h> 47 48#include <scsi/scsi_all.h> 49#include <scsi/scsi_changer.h> 50#include <scsi/scsiconf.h> 51#include <scsi/scsi_conf.h> 52 53#define CHRETRIES 2 54 55#define CHMODE(z) (minor(z) & 0x0f) 56#define CHUNIT(z) (minor(z) >> 4) 57 58struct ch_softc { 59 struct device sc_dev; 60 61 struct scsi_link *sc_link; /* all the inter level info */ 62 u_int16_t chmo; /* Offset of first CHM */ 63 u_int16_t chms; /* No. of CHM */ 64 u_int16_t slots; /* No. of Storage Elements */ 65 u_int16_t sloto; /* Offset of first SE */ 66 u_int16_t imexs; /* No. of Import/Export Slots */ 67 u_int16_t imexo; /* Offset of first IM/EX */ 68 u_int16_t drives; /* No. of CTS */ 69 u_int16_t driveo; /* Offset of first CTS */ 70 u_int16_t rot; /* CHM can rotate */ 71 u_long op_matrix; /* possible operations */ 72 u_int16_t lsterr; /* details of lasterror */ 73 u_char stor; /* posible Storage locations */ 74}; 75 76int chmatch __P((struct device *, void *, void *)); 77void chattach __P((struct device *, struct device *, void *)); 78int ch_getelem __P((struct ch_softc *, short *, int, int , char *, int)); 79int ch_move __P((struct ch_softc *, short *, int, int , int , int )); 80int ch_position __P((struct ch_softc *, short *, int, int , int )); 81int ch_mode_sense __P((struct ch_softc *, int)); 82 83struct cfattach ch_ca = { 84 sizeof(struct ch_softc), chmatch, chattach 85}; 86 87struct cfdriver ch_cd = { 88 NULL, "ch", DV_DULL 89}; 90 91/* 92 * This driver is so simple it uses all the default services 93 */ 94struct scsi_device ch_switch = { 95 NULL, 96 NULL, 97 NULL, 98 NULL, 99}; 100 101struct scsi_inquiry_pattern ch_patterns[] = { 102 {T_CHANGER, T_REMOV, 103 "", "", ""}, 104}; 105 106int 107chmatch(parent, match, aux) 108 struct device *parent; 109 void *match, *aux; 110{ 111 struct scsibus_attach_args *sa = aux; 112 int priority; 113 114 (void)scsi_inqmatch(sa->sa_inqbuf, 115 (caddr_t)ch_patterns, sizeof(ch_patterns)/sizeof(ch_patterns[0]), 116 sizeof(ch_patterns[0]), &priority); 117 return (priority); 118} 119 120/* 121 * The routine called by the low level scsi routine when it discovers 122 * a device suitable for this driver. 123 */ 124void 125chattach(parent, self, aux) 126 struct device *parent, *self; 127 void *aux; 128{ 129 struct ch_softc *ch = (void *)self; 130 struct scsibus_attach_args *sa = aux; 131 struct scsi_link *sc_link = sa->sa_sc_link; 132 133 SC_DEBUG(sc_link, SDEV_DB2, ("chattach: ")); 134 135 /* 136 * Store information needed to contact our base driver 137 */ 138 ch->sc_link = sc_link; 139 sc_link->device = &ch_switch; 140 sc_link->device_softc = ch; 141 sc_link->openings = 1; 142 143 /* 144 * Use the subdriver to request information regarding 145 * the drive. We cannot use interrupts yet, so the 146 * request must specify this. 147 */ 148 printf("\n"); 149 printf("%s: ", ch->sc_dev.dv_xname); 150 if (ch_mode_sense(ch, SCSI_AUTOCONF) != 0) 151 printf("offline\n"); 152 else 153 printf("%d slot(s), %d drive(s), %d arm(s), %d i/e-slot(s)\n", 154 ch->slots, ch->drives, ch->chms, ch->imexs); 155} 156 157/* 158 * open the device. 159 */ 160int 161chopen(dev, flags, mode, p) 162 dev_t dev; 163 int flags; 164 int mode; 165 struct proc *p; 166{ 167 int error = 0; 168 int unit; 169 struct ch_softc *ch; 170 struct scsi_link *sc_link; 171 172 unit = CHUNIT(dev); 173 if (unit >= ch_cd.cd_ndevs) 174 return ENXIO; 175 ch = ch_cd.cd_devs[unit]; 176 if (!ch) 177 return ENXIO; 178 179 sc_link = ch->sc_link; 180 181 SC_DEBUG(sc_link, SDEV_DB1, 182 ("chopen: dev=0x%x (unit %d (of %d))\n", dev, unit, ch_cd.cd_ndevs)); 183 184 /* 185 * Only allow one at a time 186 */ 187 if (sc_link->flags & SDEV_OPEN) { 188 printf("%s: already open\n", ch->sc_dev.dv_xname); 189 return EBUSY; 190 } 191 192 /* 193 * Catch any unit attention errors. 194 */ 195 error = scsi_test_unit_ready(sc_link, SCSI_IGNORE_MEDIA_CHANGE); 196 if (error) 197 goto bad; 198 199 sc_link->flags |= SDEV_OPEN; /* unit attn are now errors */ 200 201 /* 202 * Make sure data is loaded 203 */ 204 if ((error = ch_mode_sense(ch, 0)) != 0) { 205 printf("%s: offline\n", ch->sc_dev.dv_xname); 206 goto bad; 207 } 208 209 SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n")); 210 return 0; 211 212bad: 213 sc_link->flags &= ~SDEV_OPEN; 214 return error; 215} 216 217/* 218 * close the device.. only called if we are the LAST 219 * occurence of an open device 220 */ 221int 222chclose(dev, flags, mode, p) 223 dev_t dev; 224 int flags; 225 int mode; 226 struct proc *p; 227{ 228 struct ch_softc *ch = ch_cd.cd_devs[CHUNIT(dev)]; 229 230 SC_DEBUG(ch->sc_link, SDEV_DB1, ("closing\n")); 231 ch->sc_link->flags &= ~SDEV_OPEN; 232 233 return 0; 234} 235 236/* 237 * Perform special action on behalf of the user 238 * Knows about the internals of this device 239 */ 240int 241chioctl(dev, cmd, arg, mode, p) 242 dev_t dev; 243 u_long cmd; 244 caddr_t arg; 245 int mode; 246 struct proc *p; 247{ 248 struct ch_softc *ch = ch_cd.cd_devs[CHUNIT(dev)]; 249 struct scsi_link *sc_link = ch->sc_link; 250 int flags; 251 252 /* 253 * Find the device that the user is talking about 254 */ 255 flags = 0; /* give error messages, act on errors etc. */ 256 257 switch (cmd) { 258 case CHIOOP: { 259 struct chop *chop = (struct chop *) arg; 260 SC_DEBUG(sc_link, SDEV_DB2, ("[chtape_chop: %x]\n", 261 chop->ch_op)); 262 263 switch (chop->ch_op) { 264 case CHGETPARAM: 265 chop->u.getparam.chmo = ch->chmo; 266 chop->u.getparam.chms = ch->chms; 267 chop->u.getparam.sloto = ch->sloto; 268 chop->u.getparam.slots = ch->slots; 269 chop->u.getparam.imexo = ch->imexo; 270 chop->u.getparam.imexs = ch->imexs; 271 chop->u.getparam.driveo = ch->driveo; 272 chop->u.getparam.drives = ch->drives; 273 chop->u.getparam.rot = ch->rot; 274 chop->result = 0; 275 return 0; 276 break; 277 case CHPOSITION: 278 return ch_position(ch, &chop->result, 279 chop->u.position.chm, chop->u.position.to, flags); 280 case CHMOVE: 281 return ch_move(ch, &chop->result, chop->u.position.chm, 282 chop->u.move.from, chop->u.move.to, flags); 283 case CHGETELEM: 284 return ch_getelem(ch, &chop->result, 285 chop->u.get_elem_stat.type, 286 chop->u.get_elem_stat.from, 287 (char *) &chop->u.get_elem_stat.elem_data, flags); 288 default: 289 return EINVAL; 290 } 291 } 292 default: 293 return scsi_do_ioctl(sc_link, dev, cmd, arg, mode, p); 294 } 295#ifdef DIAGNOSTIC 296 panic("chioctl: impossible"); 297#endif 298} 299 300int 301ch_getelem(ch, stat, type, from, data, flags) 302 struct ch_softc *ch; 303 short *stat; 304 int type, from; 305 char *data; 306 int flags; 307{ 308 struct scsi_read_element_status scsi_cmd; 309 char elbuf[32]; 310 int error; 311 312 bzero(&scsi_cmd, sizeof(scsi_cmd)); 313 scsi_cmd.opcode = READ_ELEMENT_STATUS; 314 scsi_cmd.byte2 = type; 315 scsi_cmd.starting_element_addr[0] = (from >> 8) & 0xff; 316 scsi_cmd.starting_element_addr[1] = from & 0xff; 317 scsi_cmd.number_of_elements[1] = 1; 318 scsi_cmd.allocation_length[2] = 32; 319 320 error = scsi_scsi_cmd(ch->sc_link, (struct scsi_generic *) &scsi_cmd, 321 sizeof(scsi_cmd), (u_char *) elbuf, 32, CHRETRIES, 100000, NULL, 322 SCSI_DATA_IN | flags); 323 if (error) 324 *stat = ch->lsterr; 325 else 326 *stat = 0; 327 bcopy(elbuf + 16, data, 16); 328 return error; 329} 330 331int 332ch_move(ch, stat, chm, from, to, flags) 333 struct ch_softc *ch; 334 short *stat; 335 int chm, from, to, flags; 336{ 337 struct scsi_move_medium scsi_cmd; 338 int error; 339 340 bzero(&scsi_cmd, sizeof(scsi_cmd)); 341 scsi_cmd.opcode = MOVE_MEDIUM; 342 scsi_cmd.transport_element_address[0] = (chm >> 8) & 0xff; 343 scsi_cmd.transport_element_address[1] = chm & 0xff; 344 scsi_cmd.source_address[0] = (from >> 8) & 0xff; 345 scsi_cmd.source_address[1] = from & 0xff; 346 scsi_cmd.destination_address[0] = (to >> 8) & 0xff; 347 scsi_cmd.destination_address[1] = to & 0xff; 348 scsi_cmd.invert = (chm & CH_INVERT) ? 1 : 0; 349 error = scsi_scsi_cmd(ch->sc_link, (struct scsi_generic *) &scsi_cmd, 350 sizeof(scsi_cmd), NULL, 0, CHRETRIES, 100000, NULL, flags); 351 if (error) 352 *stat = ch->lsterr; 353 else 354 *stat = 0; 355 return error; 356} 357 358int 359ch_position(ch, stat, chm, to, flags) 360 struct ch_softc *ch; 361 short *stat; 362 int chm, to, flags; 363{ 364 struct scsi_position_to_element scsi_cmd; 365 int error; 366 367 bzero(&scsi_cmd, sizeof(scsi_cmd)); 368 scsi_cmd.opcode = POSITION_TO_ELEMENT; 369 scsi_cmd.transport_element_address[0] = (chm >> 8) & 0xff; 370 scsi_cmd.transport_element_address[1] = chm & 0xff; 371 scsi_cmd.source_address[0] = (to >> 8) & 0xff; 372 scsi_cmd.source_address[1] = to & 0xff; 373 scsi_cmd.invert = (chm & CH_INVERT) ? 1 : 0; 374 error = scsi_scsi_cmd(ch->sc_link, (struct scsi_generic *) &scsi_cmd, 375 sizeof(scsi_cmd), NULL, 0, CHRETRIES, 100000, NULL, flags); 376 if (error) 377 *stat = ch->lsterr; 378 else 379 *stat = 0; 380 return error; 381} 382 383/* 384 * Get the scsi driver to send a full inquiry to the 385 * device and use the results to fill out the global 386 * parameter structure. 387 */ 388int 389ch_mode_sense(ch, flags) 390 struct ch_softc *ch; 391 int flags; 392{ 393 struct scsi_mode_sense scsi_cmd; 394 u_char scsi_sense[128]; /* Can't use scsi_mode_sense_data because of 395 * missing block descriptor. 396 */ 397 u_char *b; 398 int i, l; 399 int error; 400 struct scsi_link *sc_link = ch->sc_link; 401 402 /* 403 * First check if we have it all loaded 404 */ 405 if (sc_link->flags & SDEV_MEDIA_LOADED) 406 return 0; 407 408 /* 409 * First do a mode sense 410 */ 411 bzero(&scsi_cmd, sizeof(scsi_cmd)); 412 scsi_cmd.opcode = MODE_SENSE; 413 scsi_cmd.byte2 = SMS_DBD; 414 scsi_cmd.page = 0x3f; /* All Pages */ 415 scsi_cmd.length = sizeof(scsi_sense); 416 417 /* 418 * Read in the pages 419 */ 420 error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd, 421 sizeof(scsi_cmd), (u_char *) &scsi_sense, 422 sizeof(scsi_sense), CHRETRIES, 5000, NULL, 423 flags | SCSI_DATA_IN); 424 if (error) { 425 printf("%s: could not mode sense\n", ch->sc_dev.dv_xname); 426 return error; 427 } 428 429 sc_link->flags |= SDEV_MEDIA_LOADED; 430 l = scsi_sense[0] - 3; 431 b = &scsi_sense[4]; 432 433 /* 434 * To avoid alignment problems 435 */ 436/* XXXX - FIX THIS FOR MSB */ 437#define p2copy(valp) (valp[1] | (valp[0]<<8)); valp+=2 438#define p4copy(valp) (valp[3] | (valp[2]<<8) | (valp[1]<<16) | (valp[0]<<24)); valp+=4 439#if 0 440 printf("\nmode_sense %d\n", l); 441 for (i = 0; i < l + 4; i++) 442 printf("%x%c", scsi_sense[i], i % 8 == 7 ? '\n' : ':'); 443 printf("\n"); 444#endif 445 for (i = 0; i < l;) { 446 u_char pc = (*b++) & 0x3f; 447 u_char pl = *b++; 448 u_char *bb = b; 449 switch (pc) { 450 case 0x1d: 451 ch->chmo = p2copy(bb); 452 ch->chms = p2copy(bb); 453 ch->sloto = p2copy(bb); 454 ch->slots = p2copy(bb); 455 ch->imexo = p2copy(bb); 456 ch->imexs = p2copy(bb); 457 ch->driveo = p2copy(bb); 458 ch->drives = p2copy(bb); 459 break; 460 case 0x1e: 461 ch->rot = *b & 0x1; 462 break; 463 case 0x1f: 464 ch->stor = *b & 0xf; 465 bb += 2; 466 ch->stor = p4copy(bb); 467 break; 468 default: 469 break; 470 } 471 b += pl; 472 i += pl + 2; 473 } 474 SC_DEBUG(sc_link, SDEV_DB2, 475 (" cht(%d-%d)slot(%d-%d)imex(%d-%d)cts(%d-%d) %s rotate\n", 476 ch->chmo, ch->chms, ch->sloto, ch->slots, ch->imexo, ch->imexs, 477 ch->driveo, ch->drives, ch->rot ? "can" : "can't")); 478 return 0; 479} 480