1 /*+M************************************************************************* 2 * Adaptec AIC7xxx device driver for Linux. 3 * 4 * Copyright (c) 1994 John Aycock 5 * The University of Calgary Department of Computer Science. 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2, or (at your option) 10 * any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; see the file COPYING. If not, write to 19 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 20 * 21 * Sources include the Adaptec 1740 driver (aha1740.c), the Ultrastor 24F 22 * driver (ultrastor.c), various Linux kernel source, the Adaptec EISA 23 * config file (!adp7771.cfg), the Adaptec AHA-2740A Series User's Guide, 24 * the Linux Kernel Hacker's Guide, Writing a SCSI Device Driver for Linux, 25 * the Adaptec 1542 driver (aha1542.c), the Adaptec EISA overlay file 26 * (adp7770.ovl), the Adaptec AHA-2740 Series Technical Reference Manual, 27 * the Adaptec AIC-7770 Data Book, the ANSI SCSI specification, the 28 * ANSI SCSI-2 specification (draft 10c), ... 29 * 30 * ---------------------------------------------------------------- 31 * Modified to include support for wide and twin bus adapters, 32 * DMAing of SCBs, tagged queueing, IRQ sharing, bug fixes, 33 * and other rework of the code. 34 * 35 * Parts of this driver are based on the FreeBSD driver by Justin 36 * T. Gibbs. 37 * 38 * A Boot time option was also added for not resetting the scsi bus. 39 * 40 * Form: aic7xxx=extended,no_reset 41 * 42 * -- Daniel M. Eischen, deischen@iworks.InterWorks.org, 04/03/95 43 * 44 * $Id: aic7xxx.c,v 3.0 1996/04/16 09:11:53 deang Exp $ 45 *-M*************************************************************************/ 46
47 #ifdefMODULE 48 #include <linux/module.h>
49 #endif 50
51 #include <stdarg.h>
52 #include <asm/io.h>
53 #include <linux/string.h>
54 #include <linux/errno.h>
55 #include <linux/kernel.h>
56 #include <linux/ioport.h>
57 #include <linux/bios32.h>
58 #include <linux/delay.h>
59 #include <linux/sched.h>
60 #include <linux/pci.h>
61 #include <linux/proc_fs.h>
62 #include <linux/blk.h>
63 #include "sd.h"
64 #include "scsi.h"
65 #include "hosts.h"
66 #include "aic7xxx.h"
67 #include "aic7xxx_reg.h"
68 #include <linux/stat.h>
69
70 #include <linux/config.h> /* for CONFIG_PCI */ 71
72 structproc_dir_entryproc_scsi_aic7xxx = { 73 PROC_SCSI_AIC7XXX, 7, "aic7xxx",
74 S_IFDIR | S_IRUGO | S_IXUGO, 2
75 };
76
77 #defineAIC7XXX_C_VERSION "$Revision: 3.0 $"
78
79 #defineNUMBER(arr) (sizeof(arr) / sizeof(arr[0]))
80 #defineMIN(a,b) ((a < b) ? a : b)
81 #defineALL_TARGETS -1
82 #ifndefTRUE 83 # defineTRUE 1
84 #endif 85 #ifndefFALSE 86 # defineFALSE 0
87 #endif 88
89 /* 90 * Defines for PCI bus support, testing twin bus support, DMAing of 91 * SCBs, tagged queueing, commands (SCBs) per lun, and SCSI bus reset 92 * delay time. 93 * 94 * o PCI bus support - this has been implemented and working since 95 * the December 1, 1994 release of this driver. If you don't have 96 * a PCI bus, then you can configure your kernel without PCI 97 * support because all PCI dependent code is bracketed with 98 * "#ifdef CONFIG_PCI ... #endif CONFIG_PCI". 99 * 100 * o Twin bus support - this has been tested and does work. 101 * 102 * o DMAing of SCBs - thanks to Kai Makisara, this now works. 103 * This define is now taken out and DMAing of SCBs is always 104 * performed (8/12/95 - DE). 105 * 106 * o Tagged queueing - this driver is capable of tagged queueing 107 * but I am unsure as to how well the higher level driver implements 108 * tagged queueing. Therefore, the maximum commands per lun is 109 * set to 2. If you want to implement tagged queueing, ensure 110 * this define is not commented out. 111 * 112 * o Sharing IRQs - allowed for sharing of IRQs. This will allow 113 * for multiple aic7xxx host adapters sharing the same IRQ, but 114 * not for sharing IRQs with other devices. The higher level 115 * PCI code and interrupt handling needs to be modified to 116 * support this. 117 * 118 * o Commands per lun - If tagged queueing is enabled, then you 119 * may want to try increasing AIC7XXX_CMDS_PER_LUN to more 120 * than 2. By default, we limit the SCBs per lun to 2 with 121 * or without tagged queueing enabled. If tagged queueing is 122 * disabled, the sequencer will keep the 2nd SCB in the input 123 * queue until the first one completes - so it is OK to to have 124 * more than 1 SCB queued. If tagged queueing is enabled, then 125 * the sequencer will attempt to send the 2nd SCB to the device 126 * while the first SCB is executing and the device is disconnected. 127 * For adapters limited to 4 SCBs, you may want to actually 128 * decrease the commands per lun to 1, if you often have more 129 * than 2 devices active at the same time. This will allocate 130 * 1 SCB for each device and ensure that there will always be 131 * a free SCB for up to 4 devices active at the same time. 132 * 133 * o 3985 support - The 3985 adapter is much like the 3940, but 134 * has three 7870 controllers as opposed to two for the 3940. 135 * It will get probed and recognized as three different adapters, 136 * but all three controllers share the same bank of 255 SCBs 137 * instead of each controller having their own bank (like the 138 * controllers on the 3940). For this reason, it is important 139 * that all devices be resident on just one channel of the 3985. 140 * In the near future, we'll modify the driver to reserve 1/3 141 * of the SCBs for each controller. 142 * 143 * Daniel M. Eischen, deischen@iworks.InterWorks.org, 01/11/96 144 */ 145
146 /* Uncomment this for testing twin bus support. */ 147 #defineAIC7XXX_TWIN_SUPPORT 148
149 /* Uncomment this for tagged queueing. */ 150 /* #define AIC7XXX_TAGGED_QUEUEING */ 151
152 /* Uncomment this for allowing sharing of IRQs. */ 153 #defineAIC7XXX_SHARE_IRQS 154
155 /* 156 * You can try raising me if tagged queueing is enabled, or lowering 157 * me if you only have 4 SCBs. 158 */ 159 #defineAIC7XXX_CMDS_PER_LUN 2
160
161 /* Set this to the delay in seconds after SCSI bus reset. */ 162 #defineAIC7XXX_RESET_DELAY 15
163
164 /* 165 * Uncomment the following define for collection of SCSI transfer statistics 166 * for the /proc filesystem. 167 * 168 * NOTE: This does affect performance since it has to maintain statistics. 169 */ 170 /* #define AIC7XXX_PROC_STATS */ 171
172 /* 173 * For debugging the abort/reset code. 174 */ 175 /* #define AIC7XXX_DEBUG_ABORT */ 176
177 /* 178 * For general debug messages 179 */ 180 #defineAIC7XXX_DEBUG 181
182 /* 183 * Controller type and options 184 */ 185 typedefenum{ 186 AIC_NONE,
187 AIC_7770, /* EISA aic7770 on motherboard */ 188 AIC_7771, /* EISA aic7771 on 274x */ 189 AIC_284x, /* VLB aic7770 on 284x, BIOS disabled */ 190 AIC_7850, /* PCI aic7850 */ 191 AIC_7855, /* PCI aic7855 */ 192 AIC_7870, /* PCI aic7870 on motherboard */ 193 AIC_7871, /* PCI aic7871 on 294x */ 194 AIC_7872, /* PCI aic7872 on 3940 */ 195 AIC_7873, /* PCI aic7873 on 3985 */ 196 AIC_7874, /* PCI aic7874 on 294x Differential */ 197 AIC_7880, /* PCI aic7880 on motherboard */ 198 AIC_7881, /* PCI aic7881 on 294x Ultra */ 199 AIC_7882, /* PCI aic7882 on 3940 Ultra */ 200 AIC_7883, /* PCI aic7883 on 3985 Ultra */ 201 AIC_7884/* PCI aic7884 on 294x Ultra Differential */ 202 }aha_type;
203
204 typedefenum{ 205 AIC_777x, /* AIC-7770 based */ 206 AIC_785x, /* AIC-7850 based */ 207 AIC_787x, /* AIC-7870 based */ 208 AIC_788x/* AIC-7880 based */ 209 }aha_chip_type;
210
211 typedefenum{ 212 AIC_SINGLE, /* Single Channel */ 213 AIC_TWIN, /* Twin Channel */ 214 AIC_WIDE/* Wide Channel */ 215 }aha_bus_type;
216
217 typedefenum{ 218 AIC_UNKNOWN,
219 AIC_ENABLED,
220 AIC_DISABLED 221 }aha_status_type;
222
223 typedefenum{ 224 LIST_HEAD,
225 LIST_SECOND
226 } insert_type;
227
228 typedefenum{ 229 ABORT_RESET_INACTIVE,
230 ABORT_RESET_PENDING,
231 ABORT_RESET_SUCCESS 232 }aha_abort_reset_type;
233
234 /* 235 * Define an array of board names that can be indexed by aha_type. 236 * Don't forget to change this when changing the types! 237 */ 238 staticconstchar * board_names[] = { 239 "<AIC-7xxx Unknown>", /* AIC_NONE */ 240 "AIC-7770", /* AIC_7770 */ 241 "AHA-2740", /* AIC_7771 */ 242 "AHA-2840", /* AIC_284x */ 243 "AIC-7850", /* AIC_7850 */ 244 "AIC-7855", /* AIC_7855 */ 245 "AIC-7870", /* AIC_7870 */ 246 "AHA-2940", /* AIC_7871 */ 247 "AHA-3940", /* AIC_7872 */ 248 "AHA-3985", /* AIC_7873 */ 249 "AHA-2940 Differential", /* AIC_7874 */ 250 "AIC-7880 Ultra", /* AIC_7880 */ 251 "AHA-2940 Ultra", /* AIC_7881 */ 252 "AHA-3940 Ultra", /* AIC_7882 */ 253 "AHA-3985 Ultra", /* AIC_7883 */ 254 "AHA-2940 Ultra Differential" /* AIC_7884 */ 255 };
256
257 /* 258 * There should be a specific return value for this in scsi.h, but 259 * it seems that most drivers ignore it. 260 */ 261 #define DID_UNDERFLOW DID_ERROR 262
263 /* 264 * What we want to do is have the higher level scsi driver requeue 265 * the command to us. There is no specific driver status for this 266 * condition, but the higher level scsi driver will requeue the 267 * command on a DID_BUS_BUSY error. 268 */ 269 #defineDID_RETRY_COMMANDDID_BUS_BUSY 270
271 /* 272 * EISA/VL-bus stuff 273 */ 274 #defineMINSLOT 1
275 #defineMAXSLOT 15
276 #defineSLOTBASE(x) ((x) << 12)
277 #defineMAXIRQ 15
278
279 /* 280 * Standard EISA Host ID regs (Offset from slot base) 281 */ 282 #defineHID0 0x80 /* 0,1: msb of ID2, 2-7: ID1 */ 283 #defineHID1 0x81 /* 0-4: ID3, 5-7: LSB ID2 */ 284 #defineHID2 0x82 /* product */ 285 #define HID3 0x83 /* firmware revision */ 286
287 /* 288 * AIC-7770 I/O range to reserve for a card 289 */ 290 #defineMINREG 0xC00
291 #defineMAXREG 0xCBF
292
293 #defineINTDEF 0x5C /* Interrupt Definition Register */ 294
295 /* 296 * Some defines for the HCNTRL register. 297 */ 298 #defineREQ_PAUSEIRQMS | INTEN | PAUSE 299 #define UNPAUSE_274X IRQMS | INTEN 300 #defineUNPAUSE_284XINTEN 301 #defineUNPAUSE_294XIRQMS | INTEN 302
303 /* 304 * AIC-78X0 PCI registers 305 */ 306 #defineCLASS_PROGIF_REVID 0x08
307 #defineDEVREVID 0x000000FFul
308 #define PROGINFC 0x0000FF00ul
309 #define SUBCLASS 0x00FF0000ul
310 #define BASECLASS 0xFF000000ul
311
312 #defineCSIZE_LATTIME 0x0C
313 #defineCACHESIZE 0x0000003Ful /* only 5 bits */ 314 #defineLATTIME 0x0000FF00ul
315
316 #defineDEVCONFIG 0x40
317 #define MPORTMODE 0x00000400ul /* aic7870 only */ 318 #defineRAMPSM 0x00000200ul /* aic7870 only */ 319 #define VOLSENSE 0x00000100ul
320 #defineSCBRAMSEL 0x00000080ul
321 #define MRDCEN 0x00000040ul
322 #define EXTSCBTIME 0x00000020ul /* aic7870 only */ 323 #define EXTSCBPEN 0x00000010ul /* aic7870 only */ 324 #define BERREN 0x00000008ul
325 #define DACEN 0x00000004ul
326 #define STPWLEVEL 0x00000002ul
327 #define DIFACTNEGEN 0x00000001ul /* aic7870 only */ 328
329 /* 330 * 331 * Define the format of the SEEPROM registers (16 bits). 332 * 333 */ 334 structseeprom_config{ 335
336 /* 337 * SCSI ID Configuration Flags 338 */ 339 #defineCFXFER 0x0007 /* synchronous transfer rate */ 340 #defineCFSYNCH 0x0008 /* enable synchronous transfer */ 341 #defineCFDISC 0x0010 /* enable disconnection */ 342 #defineCFWIDEB 0x0020 /* wide bus device (wide card) */ 343 /* UNUSED 0x00C0 */ 344 #define CFSTART 0x0100 /* send start unit SCSI command */ 345 #define CFINCBIOS 0x0200 /* include in BIOS scan */ 346 #define CFRNFOUND 0x0400 /* report even if not found */ 347 /* UNUSED 0xF800 */ 348 unsignedshortdevice_flags[16]; /* words 0-15 */ 349
350 /* 351 * BIOS Control Bits 352 */ 353 #define CFSUPREM 0x0001 /* support all removable drives */ 354 #define CFSUPREMB 0x0002 /* support removable drives for boot only */ 355 #defineCFBIOSEN 0x0004 /* BIOS enabled */ 356 /* UNUSED 0x0008 */ 357 #define CFSM2DRV 0x0010 /* support more than two drives */ 358 #defineCF284XEXTEND 0x0020 /* extended translation (284x cards) */ 359 /* UNUSED 0x0040 */ 360 #defineCFEXTEND 0x0080 /* extended translation enabled */ 361 /* UNUSED 0xFF00 */ 362 unsignedshortbios_control; /* word 16 */ 363
364 /* 365 * Host Adapter Control Bits 366 */ 367 /* UNUSED 0x0001 */ 368 #defineCFULTRAEN 0x0002 /* Ultra SCSI speed enable (Ultra cards) */ 369 #define CF284XSELTO 0x0003 /* Selection timeout (284x cards) */ 370 #define CF284XFIFO 0x000C /* FIFO Threshold (284x cards) */ 371 #defineCFSTERM 0x0004 /* SCSI low byte termination (non-wide cards) */ 372 #defineCFWSTERM 0x0008 /* SCSI high byte termination (wide card) */ 373 #defineCFSPARITY 0x0010 /* SCSI parity */ 374 #defineCF284XSTERM 0x0020 /* SCSI low byte termination (284x cards) */ 375 #define CFRESETB 0x0040 /* reset SCSI bus at IC initialization */ 376 /* UNUSED 0xFF80 */ 377 unsignedshortadapter_control; /* word 17 */ 378
379 /* 380 * Bus Release, Host Adapter ID 381 */ 382 #defineCFSCSIID 0x000F /* host adapter SCSI ID */ 383 /* UNUSED 0x00F0 */ 384 #defineCFBRTIME 0xFF00 /* bus release time */ 385 unsignedshortbrtime_id; /* word 18 */ 386
387 /* 388 * Maximum targets 389 */ 390 #define CFMAXTARG 0x00FF /* maximum targets */ 391 /* UNUSED 0xFF00 */ 392 unsignedshortmax_targets; /* word 19 */ 393
394 unsignedshort res_1[11]; /* words 20-30 */ 395 unsignedshortchecksum; /* word 31 */ 396 };
397
398 /* 399 * Pause the sequencer and wait for it to actually stop - this 400 * is important since the sequencer can disable pausing for critical 401 * sections. 402 */ 403 #definePAUSE_SEQUENCER(p) \
404 outb(p->pause, HCNTRL + p->base); \
405 while ((inb(HCNTRL + p->base) & PAUSE) == 0) \
406 ; \
407
408 /* 409 * Unpause the sequencer. Unremarkable, yet done often enough to 410 * warrant an easy way to do it. 411 */ 412 #defineUNPAUSE_SEQUENCER(p) \
413 outb(p->unpause, HCNTRL + p->base)
414
415 /* 416 * Restart the sequencer program from address zero 417 */ 418 #defineRESTART_SEQUENCER(p) \
419 do{ \
420 outb(SEQRESET | FASTMODE, SEQCTL + p->base); \
421 }while (inb(SEQADDR0 + p->base) != 0 && \
422 inb(SEQADDR1 + p->base) != 0); \
423 UNPAUSE_SEQUENCER(p);
424
425 /* 426 * If an error occurs during a data transfer phase, run the command 427 * to completion - it's easier that way - making a note of the error 428 * condition in this location. This then will modify a DID_OK status 429 * into an appropriate error for the higher-level SCSI code. 430 */ 431 #defineaic7xxx_error(cmd) ((cmd)->SCp.Status)
432
433 /* 434 * Keep track of the targets returned status. 435 */ 436 #defineaic7xxx_status(cmd) ((cmd)->SCp.sent_command)
437
438 /* 439 * The position of the SCSI commands scb within the scb array. 440 */ 441 #defineaic7xxx_position(cmd) ((cmd)->SCp.have_data_in)
442
443 /* 444 * Since the sequencer code DMAs the scatter-gather structures 445 * directly from memory, we use this macro to assert that the 446 * kernel structure hasn't changed. 447 */ 448 #defineSG_STRUCT_CHECK(sg) \
449 ((char *) &(sg).address - (char *) &(sg) != 0 || \
450 (char *) &(sg).length - (char *) &(sg) != 8 || \
451 sizeof((sg).address) != 4 || \
452 sizeof((sg).length) != 4 || \
453 sizeof(sg) != 12)
454
455 /* 456 * "Static" structures. Note that these are NOT initialized 457 * to zero inside the kernel - we have to initialize them all 458 * explicitly. 459 * 460 * We support multiple adapter cards per interrupt, but keep a 461 * linked list of Scsi_Host structures for each IRQ. On an interrupt, 462 * use the IRQ as an index into aic7xxx_boards[] to locate the card 463 * information. 464 */ 465 staticstructScsi_Host *aic7xxx_boards[MAXIRQ + 1];
466
467 /* 468 * When we detect and register the card, it is possible to 469 * have the card raise a spurious interrupt. Because we need 470 * to support multiple cards, we cannot tell which card caused 471 * the spurious interrupt. And, we might not even have added 472 * the card info to the linked list at the time the spurious 473 * interrupt gets raised. This variable is suppose to keep track 474 * of when we are registering a card and how many spurious 475 * interrupts we have encountered. 476 * 477 * 0 - do not allow spurious interrupts. 478 * 1 - allow 1 spurious interrupt 479 * 2 - have 1 spurious interrupt, do not allow any more. 480 * 481 * I've made it an integer instead of a boolean in case we 482 * want to allow more than one spurious interrupt for debugging 483 * purposes. Otherwise, it could just go from true to false to 484 * true (or something like that). 485 * 486 * When the driver detects the cards, we'll set the count to 1 487 * for each card detection and registration. After the registration 488 * of a card completes, we'll set the count back to 0. So far, it 489 * seems to be enough to allow a spurious interrupt only during 490 * card registration; if a spurious interrupt is going to occur, 491 * this is where it happens. 492 * 493 * We should be able to find a way to avoid getting the spurious 494 * interrupt. But until we do, we have to keep this ugly code. 495 */ 496 staticintaic7xxx_spurious_count;
497
498 /* 499 * The driver keeps up to four scb structures per card in memory. Only the 500 * first 25 bytes of the structure are valid for the hardware, the rest used 501 * for driver level bookkeeping. 502 */ 503
504 structaic7xxx_scb{ 505 /* ------------ Begin hardware supported fields ---------------- */ 506 /* 0*/unsignedcharcontrol;
507 /* 1*/unsignedchartarget_channel_lun; /* 4/1/3 bits */ 508 /* 2*/unsignedchartarget_status;
509 /* 3*/unsignedcharSG_segment_count;
510 /* 4*/unsignedcharSG_list_pointer[4] __attribute__ ((packed));
511 /* 8*/unsignedcharresidual_SG_segment_count;
512 /* 9*/unsignedcharresidual_data_count[3];
513 /*12*/unsignedchardata_pointer[4] __attribute__ ((packed));
514 /*16*/unsignedlongdata_count;
515 /*20*/unsignedcharSCSI_cmd_pointer[4] __attribute__ ((packed));
516 /*24*/unsignedcharSCSI_cmd_length;
517 #defineSCB_PIO_TRANSFER_SIZE 25 /* 518 * amount we need to upload/download 519 * via rep in/outsb to perform 520 * a request sense. The second 521 * RESERVED byte is initialized to 522 * 0 in getscb(). 523 */ 524 /*25*/u_charnext_waiting; /* Used to thread SCBs awaiting selection. */ 525 /*-----------------end of hardware supported fields----------------*/ 526 structaic7xxx_scb *next; /* next ptr when in free list */ 527 Scsi_Cmnd *cmd; /* Scsi_Cmnd for this scb */ 528 #defineSCB_FREE 0x00
529 #defineSCB_ACTIVE 0x01
530 #defineSCB_ABORTED 0x02
531 #defineSCB_DEVICE_RESET 0x04
532 #define SCB_IMMED 0x08
533 #define SCB_SENSE 0x10
534 #defineSCB_QUEUED_FOR_DONE 0x40
535 intstate; /* current state of scb */ 536 unsignedintposition; /* Position in scb array */ 537 structscatterlistsg;
538 structscatterlistsense_sg;
539 unsignedcharsense_cmd[6]; /* Allocate 6 characters for sense command */ 540 };
541
542 staticstruct{ 543 unsignedcharerrno;
544 constchar *errmesg;
545 }hard_error[] = { 546 {ILLHADDR, "Illegal Host Access" },
547 {ILLSADDR, "Illegal Sequencer Address referenced" },
548 {ILLOPCODE, "Illegal Opcode in sequencer program" },
549 {PARERR, "Sequencer Ram Parity Error" } 550 };
551
552 staticunsignedchar 553 generic_sense[] = {REQUEST_SENSE, 0, 0, 0, 255, 0 };
554
555 /* 556 * The maximum number of SCBs we could have for ANY type 557 * of card. DON'T FORGET TO CHANGE THE SCB MASK IN THE 558 * SEQUENCER CODE IF THIS IS MODIFIED! 559 */ 560 #defineAIC7XXX_MAXSCB 255
561
562 /* 563 * Define a structure used for each host adapter, only one per IRQ. 564 */ 565 structaic7xxx_host{ 566 structScsi_Host *host; /* pointer to scsi host */ 567 intbase; /* card base address */ 568 intmaxscb; /* hardware SCBs */ 569 intnumscb; /* current number of scbs */ 570 intextended; /* extended xlate? */ 571 aha_typetype; /* card type */ 572 aha_chip_typechip_type; /* chip base type */ 573 intultra_enabled; /* Ultra SCSI speed enabled */ 574 intchan_num; /* for 3940/3985, channel number */ 575 aha_bus_typebus_type; /* normal/twin/wide bus */ 576 unsignedchara_scanned; /* 0 not scanned, 1 scanned */ 577 unsignedcharb_scanned; /* 0 not scanned, 1 scanned */ 578 unsignedintisr_count; /* Interrupt count */ 579 volatileunsignedcharunpause; /* unpause value for HCNTRL */ 580 volatileunsignedcharpause; /* pause value for HCNTRL */ 581 volatileunsignedshortneedsdtr_copy; /* default config */ 582 volatileunsignedshortneedsdtr;
583 volatileunsignedshortsdtr_pending;
584 volatileunsignedshortneedwdtr_copy; /* default config */ 585 volatileunsignedshortneedwdtr;
586 volatileunsignedshortwdtr_pending;
587 volatileunsignedshortdiscenable; /* Targets allowed to disconnect */ 588 structseeprom_configseeprom;
589 inthave_seeprom;
590 unsignedcharqcntmask;
591 structScsi_Host *next; /* allow for multiple IRQs */ 592 structaic7xxx_scbscb_array[AIC7XXX_MAXSCB]; /* active commands */ 593 structaic7xxx_scb *free_scb; /* list of free SCBs */ 594 structaic7xxx_scb *aborted_scb; /* list of aborted SCBs */ 595 #ifdefAIC7XXX_PROC_STATS 596 /* 597 * Statistics Kept: 598 * 599 * Total Xfers (count for each command that has a data xfer), 600 * broken down further by reads && writes. 601 * 602 * Binned sizes, writes && reads: 603 * < 512, 512, 1-2K, 2-4K, 4-8K, 8-16K, 16-32K, 32-64K, 64K-128K, > 128K 604 * 605 * Total amounts read/written above 512 bytes (amts under ignored) 606 */ 607 structaic7xxx_xferstats{ 608 longxfers; /* total xfer count */ 609 longw_total; /* total writes */ 610 longw_total512; /* 512 byte blocks written */ 611 longw_bins[10]; /* binned write */ 612 longr_total; /* total reads */ 613 longr_total512; /* 512 byte blocks read */ 614 longr_bins[10]; /* binned reads */ 615 }stats[2][16][8]; /* channel, target, lun */ 616 #endif/* AIC7XXX_PROC_STATS */ 617 };
618
619 structaic7xxx_host_config{ 620 intirq; /* IRQ number */ 621 intbase; /* I/O base */ 622 intmaxscb; /* hardware SCBs */ 623 intunpause; /* unpause value for HCNTRL */ 624 intpause; /* pause value for HCNTRL */ 625 intscsi_id; /* host SCSI ID */ 626 intscsi_id_b; /* host SCSI ID B channel for twin cards */ 627 intextended; /* extended xlate? */ 628 intultra_enabled; /* Ultra SCSI speed enabled */ 629 intchan_num; /* for 3940/3985, channel number */ 630 intuse_defaults; /* Use default wide/sync settings. */ 631 unsignedcharbusrtime; /* bus release time */ 632 unsignedcharbus_speed; /* bus speed */ 633 unsignedcharqcntmask;
634 aha_typetype; /* card type */ 635 aha_chip_typechip_type; /* chip base type */ 636 aha_bus_typebus_type; /* normal/twin/wide bus */ 637 aha_status_typebios; /* BIOS is enabled/disabled */ 638 aha_status_typeparity; /* bus parity enabled/disabled */ 639 aha_status_typelow_term; /* bus termination low byte */ 640 aha_status_typehigh_term; /* bus termination high byte (wide cards only) */ 641 };
642
643 /* 644 * Valid SCSIRATE values. (p. 3-17) 645 * Provides a mapping of transfer periods in ns to the proper value to 646 * stick in the scsiscfr reg to use that transfer rate. 647 */ 648 staticstruct{ 649 shortperiod;
650 /* Rates in Ultra mode have bit 8 of sxfr set */ 651 #defineULTRA_SXFR 0x100
652 shortrate;
653 constchar *english;
654 }aic7xxx_syncrates[] = { 655 { 50, 0x100, "20.0" },
656 { 62, 0x110, "16.0" },
657 { 75, 0x120, "13.4" },
658 { 100, 0x140, "10.0" },
659 { 100, 0x000, "10.0" },
660 { 125, 0x010, "8.0" },
661 { 150, 0x020, "6.67" },
662 { 175, 0x030, "5.7" },
663 { 200, 0x040, "5.0" },
664 { 225, 0x050, "4.4" },
665 { 250, 0x060, "4.0" },
666 { 275, 0x070, "3.6" } 667 };
668
669 staticintnum_aic7xxx_syncrates =
670 sizeof(aic7xxx_syncrates) / sizeof(aic7xxx_syncrates[0]);
671
672 #ifdefCONFIG_PCI 673 staticintnumber_of_39xxs = 0;
674 #endifCONFIG_PCI 675
676 #ifdefAIC7XXX_DEBUG 677 staticvoid 678 debug(constchar *fmt, ...)
/* */ 679 { 680 va_listap;
681 charbuf[256];
682
683 va_start(ap, fmt);
684 vsprintf(buf, fmt, ap);
685 printk(buf);
686 va_end(ap);
687 } 688
689 staticvoid 690 debug_config(structaic7xxx_host_config *p)
/* */ 691 { 692 intscsi_conf;
693 unsignedcharbrelease;
694 unsignedchardfthresh;
695
696 staticintDFT[] = { 0, 50, 75, 100 };
697 staticintSST[] = { 256, 128, 64, 32 };
698 staticconstchar *BUSW[] = { "", "-TWIN", "-WIDE" };
699
700 scsi_conf = inb(SCSICONF + p->base);
701
702 /* 703 * Scale the Data FIFO Threshold and the Bus Release Time; they are 704 * stored in formats compatible for writing to sequencer registers. 705 */ 706 dfthresh = p->bus_speed >> 6;
707
708 if (p->chip_type == AIC_777x)
709 { 710 brelease = p->busrtime >> 2;
711 } 712 else 713 { 714 brelease = p->busrtime;
715 } 716 if (brelease == 0)
717 { 718 brelease = 2;
719 } 720
721 switch (p->type)
722 { 723 caseAIC_7770:
724 caseAIC_7771:
725 printk("%s%s AT EISA SLOT %d:\n", board_names[p->type], BUSW[p->bus_type],
726 p->base >> 12);
727 break;
728
729 caseAIC_284x:
730 printk("%s%s AT VLB SLOT %d:\n", board_names[p->type], BUSW[p->bus_type],
731 p->base >> 12);
732 break;
733
734 caseAIC_7850:
735 caseAIC_7855:
736 caseAIC_7870:
737 caseAIC_7871:
738 caseAIC_7872:
739 caseAIC_7873:
740 caseAIC_7874:
741 caseAIC_7880:
742 caseAIC_7881:
743 caseAIC_7882:
744 caseAIC_7883:
745 caseAIC_7884:
746 printk("%s%s (PCI-bus):\n", board_names[p->type], BUSW[p->bus_type]);
747 break;
748
749 default:
750 panic("aic7xxx: (debug_config) internal error.\n");
751 } 752
753 printk(" irq %d\n"
754 " bus release time %d bclks\n"
755 " data fifo threshold %d%%\n",
756 p->irq,
757 brelease,
758 DFT[dfthresh]);
759
760 printk(" SCSI CHANNEL A:\n"
761 " scsi id %d\n"
762 " scsi selection timeout %d ms\n"
763 " scsi bus reset at power-on %sabled\n",
764 scsi_conf & 0x07,
765 SST[(scsi_conf >> 3) & 0x03],
766 (scsi_conf & 0x40) ? "en" : "dis");
767
768 if ((p->chip_type == AIC_777x) && (p->parity == AIC_UNKNOWN))
769 { 770 /* 771 * Set the parity for 7770 based cards. 772 */ 773 p->parity = (scsi_conf & 0x20) ? AIC_ENABLED : AIC_DISABLED;
774 } 775 if (p->parity != AIC_UNKNOWN)
776 { 777 printk(" scsi bus parity %sabled\n",
778 (p->parity == AIC_ENABLED) ? "en" : "dis");
779 } 780
781 if ((p->type == AIC_7770) || (p->type == AIC_7771))
782 { 783 p->low_term = (scsi_conf & 0x80) ? AIC_ENABLED : AIC_DISABLED;
784 } 785 if (p->low_term != AIC_UNKNOWN)
786 { 787 printk(" scsi bus termination (low byte) %sabled\n",
788 (p->low_term == AIC_ENABLED) ? "en" : "dis");
789 } 790 if ((p->bus_type == AIC_WIDE) && (p->high_term != AIC_UNKNOWN))
791 { 792 printk(" scsi bus termination (high byte) %sabled\n",
793 (p->high_term == AIC_ENABLED) ? "en" : "dis");
794 } 795 } 796
797 #if 0
798 staticvoid 799 debug_scb(structaic7xxx_scb *scb)
/* */ 800 { 801 printk("control 0x%x, tcl 0x%x, sg_count %d, sg_ptr 0x%x, cmdp 0x%x, cmdlen %d\n",
802 scb->control, scb->target_channel_lun, scb->SG_segment_count,
803 (scb->SG_list_pointer[3] << 24) | (scb->SG_list_pointer[2] << 16) |
804 (scb->SG_list_pointer[1] << 8) | scb->SG_list_pointer[0],
805 (scb->SCSI_cmd_pointer[3] << 24) | (scb->SCSI_cmd_pointer[2] << 16) |
806 (scb->SCSI_cmd_pointer[1] << 8) | scb->SCSI_cmd_pointer[0],
807 scb->SCSI_cmd_length);
808 printk("reserved 0x%x, target status 0x%x, resid SG count %d, resid data count %d\n",
809 (scb->RESERVED[1] << 8) | scb->RESERVED[0], scb->target_status,
810 scb->residual_SG_segment_count, scb->residual_data_count);
811 printk("data ptr 0x%x, data count %d, next waiting %d\n",
812 (scb->data_pointer[3] << 24) | (scb->data_pointer[2] << 16) |
813 (scb->data_pointer[1] << 8) | scb->data_pointer[0],
814 scb->data_count, scb->next_waiting);
815 printk("next ptr 0x%lx, Scsi Cmnd 0x%lx, state 0x%x, position %d\n",
816 (unsignedlong) scb->next, (unsignedlong) scb->cmd, scb->state,
817 scb->position);
818 } 819 #endif 820
821 #else 822 # definedebug(fmt, args...)
823 # definedebug_config(x)
824 # definedebug_scb(x)
825 #endifAIC7XXX_DEBUG 826
827 /* 828 * XXX - these options apply unilaterally to _all_ 274x/284x/294x 829 * cards in the system. This should be fixed, but then, 830 * does anyone really have more than one in a machine? 831 */ 832 staticunsignedintaic7xxx_extended = 0; /* extended translation on? */ 833 staticunsignedintaic7xxx_no_reset = 0; /* no resetting of SCSI bus */ 834
835 /*+F************************************************************************* 836 * Function: 837 * aic7xxx_setup 838 * 839 * Description: 840 * Handle Linux boot parameters. This routine allows for assigning a value 841 * to a parameter with a ':' between the parameter and the value. 842 * ie. aic7xxx=unpause:0x0A,extended 843 *-F*************************************************************************/ 844 void 845 aic7xxx_setup(char *s, int *dummy)
/* */ 846 { 847 inti, n;
848 char *p;
849
850 staticstruct{ 851 constchar *name;
852 unsignedint *flag;
853 }options[] = { 854 { "extended", &aic7xxx_extended},
855 { "no_reset", &aic7xxx_no_reset},
856 {NULL, NULL} 857 };
858
859 for (p = strtok(s, ","); p; p = strtok(NULL, ","))
860 { 861 for (i = 0; options[i].name; i++)
862 { 863 n = strlen(options[i].name);
864 if (!strncmp(options[i].name, p, n))
865 { 866 if (p[n] == ':')
867 { 868 *(options[i].flag) = simple_strtoul(p + n + 1, NULL, 0);
869 } 870 else 871 { 872 *(options[i].flag) = !0;
873 } 874 } 875 } 876 } 877 } 878
879 /*+F************************************************************************* 880 * Function: 881 * aic7xxx_loadseq 882 * 883 * Description: 884 * Load the sequencer code into the controller memory. 885 *-F*************************************************************************/ 886 staticvoid 887 aic7xxx_loadseq(intbase)
/* */ 888 { 889 staticunsignedcharseqprog[] = { 890 /* 891 * Each sequencer instruction is 29 bits 892 * long (fill in the excess with zeroes) 893 * and has to be loaded from least -> most 894 * significant byte, so this table has the 895 * byte ordering reversed. 896 */ 897 # include "aic7xxx_seq.h"
898 };
899
900 /* 901 * When the AIC-7770 is paused (as on chip reset), the 902 * sequencer address can be altered and a sequencer 903 * program can be loaded by writing it, byte by byte, to 904 * the sequencer RAM port - the Adaptec documentation 905 * recommends using REP OUTSB to do this, hence the inline 906 * assembly. Since the address autoincrements as we load 907 * the program, reset it back to zero afterward. Disable 908 * sequencer RAM parity error detection while loading, and 909 * make sure the LOADRAM bit is enabled for loading. 910 */ 911 outb(PERRORDIS | SEQRESET | LOADRAM, SEQCTL + base);
912
913 outsb(SEQRAM + base, seqprog, sizeof(seqprog));
914
915 /* 916 * WARNING! This is a magic sequence! After extensive 917 * experimentation, it seems that you MUST turn off the 918 * LOADRAM bit before you play with SEQADDR again, else 919 * you will end up with parity errors being flagged on 920 * your sequencer program. (You would also think that 921 * turning off LOADRAM and setting SEQRESET to reset the 922 * address to zero would work, but you need to do it twice 923 * for it to take effect on the address. Timing problem?) 924 */ 925 do{ 926 /* 927 * Actually, reset it until 928 * the address shows up as 929 * zero just to be safe.. 930 */ 931 outb(SEQRESET | FASTMODE, SEQCTL + base);
932 }while ((inb(SEQADDR0 + base) != 0) && (inb(SEQADDR1 + base) != 0));
933 } 934
935 /*+F************************************************************************* 936 * Function: 937 * aic7xxx_delay 938 * 939 * Description: 940 * Delay for specified amount of time. 941 *-F*************************************************************************/ 942 staticvoid 943 aic7xxx_delay(intseconds)
/* */ 944 { 945 unsignedlongi;
946
947 i = jiffies + (seconds * HZ); /* compute time to stop */ 948
949 while (jiffies < i)
950 { 951 ; /* Do nothing! */ 952 } 953 } 954
955 /*+F************************************************************************* 956 * Function: 957 * rcs_version 958 * 959 * Description: 960 * Return a string containing just the RCS version number from either 961 * an Id or Revision RCS clause. 962 *-F*************************************************************************/ 963 constchar *
964 rcs_version(constchar *version_info)
/* */ 965 { 966 staticcharbuf[10];
967 char *bp, *ep;
968
969 bp = NULL;
970 strcpy(buf, "????");
971 if (!strncmp(version_info, "$Id: ", 5))
972 { 973 if ((bp = strchr(version_info, ' ')) != NULL)
974 { 975 bp++;
976 if ((bp = strchr(bp, ' ')) != NULL)
977 { 978 bp++;
979 } 980 } 981 } 982 else 983 { 984 if (!strncmp(version_info, "$Revision: ", 11))
985 { 986 if ((bp = strchr(version_info, ' ')) != NULL)
987 { 988 bp++;
989 } 990 } 991 } 992
993 if (bp != NULL)
994 { 995 if ((ep = strchr(bp, ' ')) != NULL)
996 { 997 registerintlen = ep - bp;
998
999 strncpy(buf, bp, len);
1000 buf[len] = '\0';
1001 }1002 }1003
1004 returnbuf;
1005 }1006
1007 /*+F*************************************************************************1008 * Function:1009 * aic7xxx_info1010 *1011 * Description:1012 * Return a string describing the driver.1013 *-F*************************************************************************/1014 constchar *
1015 aic7xxx_info(structScsi_Host *notused)
/* */1016 {1017 staticcharbuffer[128];
1018
1019 strcpy(buffer, "Adaptec AHA274x/284x/294x (EISA/VLB/PCI-Fast SCSI) ");
1020 strcat(buffer, rcs_version(AIC7XXX_C_VERSION));
1021 strcat(buffer, "/");
1022 strcat(buffer, rcs_version(AIC7XXX_H_VERSION));
1023 strcat(buffer, "/");
1024 strcat(buffer, rcs_version(AIC7XXX_SEQ_VER));
1025
1026 returnbuffer;
1027 }1028
1029 /*+F*************************************************************************1030 * Function:1031 * aic7xxx_length1032 *1033 * Description:1034 * How much data should be transferred for this SCSI command? Stop1035 * at segment sg_last if it's a scatter-gather command so we can1036 * compute underflow easily.1037 *-F*************************************************************************/1038 staticunsigned1039 aic7xxx_length(Scsi_Cmnd *cmd, intsg_last)
/* */1040 {1041 inti, segments;
1042 unsignedlength;
1043 structscatterlist *sg;
1044
1045 segments = cmd->use_sg - sg_last;
1046 sg = (structscatterlist *) cmd->buffer;
1047
1048 if (cmd->use_sg)
1049 {1050 for (i = length = 0; (i < cmd->use_sg) && (i < segments); i++)
1051 {1052 length += sg[i].length;
1053 }1054 }1055 else1056 {1057 length = cmd->request_bufflen;
1058 }1059
1060 return (length);
1061 }1062
1063 /*+F*************************************************************************1064 * Function:1065 * aic7xxx_scsirate1066 *1067 * Description:1068 * Look up the valid period to SCSIRATE conversion in our table1069 *-F*************************************************************************/1070 staticvoid1071 aic7xxx_scsirate(structaic7xxx_host *p, unsignedchar *scsirate,
/* */1072 shortperiod, unsignedcharoffset,
1073 inttarget, charchannel)
1074 {1075 inti;
1076
1077 for (i = 0; i < num_aic7xxx_syncrates; i++)
1078 {1079 if ((aic7xxx_syncrates[i].period - period) >= 0)
1080 {1081 /*1082 * Watch out for Ultra speeds when ultra is not enabled and1083 * vice-versa.1084 */1085 if (p->ultra_enabled)
1086 {1087 if (!(aic7xxx_syncrates[i].rate & ULTRA_SXFR))
1088 {1089 printk ("aic7xxx: Target %d, channel %c, requests %sMHz transfers, "
1090 "but adapter in Ultra mode can only sync at 7.2MHz or "
1091 "above.\n", target, channel, aic7xxx_syncrates[i].english);
1092 break; /* Use asynchronous transfers. */1093 }1094 }1095 else1096 {1097 /*1098 * Check for an Ultra device trying to negotiate an Ultra rate1099 * on an adapter with Ultra mode disabled.1100 */1101 if (aic7xxx_syncrates[i].rate & ULTRA_SXFR)
1102 {1103 /*1104 * This should only happen if the driver is the first to negotiate1105 * and chooses a high rate. We'll just move down the table until1106 * we hit a non Ultra speed.1107 */1108 continue;
1109 }1110 }1111 *scsirate = (aic7xxx_syncrates[i].rate) | (offset & 0x0F);
1112 printk("aic7xxx: Target %d, channel %c, now synchronous at %sMHz, "
1113 "offset(0x%x).\n",
1114 target, channel, aic7xxx_syncrates[i].english, offset);
1115 return;
1116 }1117 }1118
1119 /*1120 * Default to asynchronous transfer1121 */1122 *scsirate = 0;
1123 printk("aic7xxx: Target %d, channel %c, using asynchronous transfers.\n",
1124 target, channel);
1125 }1126
1127 /*+F*************************************************************************1128 * Function:1129 * aic7xxx_putscb1130 *1131 * Description:1132 * Transfer a SCB to the controller.1133 *-F*************************************************************************/1134 staticinlinevoid1135 aic7xxx_putscb(structaic7xxx_host *p, structaic7xxx_scb *scb)
/* */1136 {1137 unsignedcharcurscb;
1138 intbase = p->base;
1139
1140 curscb = inb(SCBPTR + base);
1141 outb(scb->position, SCBPTR + base);
1142 outb(SCBAUTO, SCBCNT + base);
1143
1144 /*1145 * By turning on the SCB auto increment, any reference1146 * to the SCB I/O space postincrements the SCB address1147 * we're looking at. So turn this on and dump the relevant1148 * portion of the SCB to the card.1149 *1150 * We can do 16bit transfers on all but 284x.1151 */1152 if (p->type == AIC_284x)
1153 {1154 outsb(SCBARRAY + base, scb, SCB_PIO_TRANSFER_SIZE);
1155 }1156 else1157 {1158 outsl(SCBARRAY + base, scb, (SCB_PIO_TRANSFER_SIZE + 3) / 4);
1159 }1160
1161 outb(0, SCBCNT + base);
1162 outb(curscb, SCBPTR + base);
1163 }1164
1165 /*+F*************************************************************************1166 * Function:1167 * aic7xxx_getscb1168 *1169 * Description:1170 * Get a SCB from the controller.1171 *-F*************************************************************************/1172 staticinlinevoid1173 aic7xxx_getscb(structaic7xxx_host *p, structaic7xxx_scb *scb)
/* */1174 {1175 intbase = p->base;
1176
1177 /*1178 * This is almost identical to aic7xxx_putscb().1179 */1180 outb(SCBAUTO, SCBCNT + base);
1181 insb(SCBARRAY + base, scb, SCB_PIO_TRANSFER_SIZE);
1182 outb(0, SCBCNT + base);
1183 }1184
1185 /*+F*************************************************************************1186 * Function:1187 * aic7xxx_match_scb1188 *1189 * Description:1190 * Checks to see if an scb matches the target/channel as specified.1191 * If target is ALL_TARGETS (-1), then we're looking for any device1192 * on the specified channel; this happens when a channel is going1193 * to be reset and all devices on that channel must be aborted.1194 *-F*************************************************************************/1195 staticint1196 aic7xxx_match_scb(structaic7xxx_scb *scb, inttarget, charchannel)
/* */1197 {1198 inttarg = (scb->target_channel_lun >> 4) & 0x0F;
1199 charchan = (scb->target_channel_lun & SELBUSB) ? 'B' : 'A';
1200
1201 #ifdefAIC7XXX_DEBUG_ABORT1202 printk ("aic7xxx: (match_scb) comparing target/channel %d/%c to scb %d/%c\n",
1203 target, channel, targ, chan);
1204 #endif1205 if (target == ALL_TARGETS)
1206 {1207 return (chan == channel);
1208 }1209 else1210 {1211 return ((chan == channel) && (targ == target));
1212 }1213 }1214
1215 /*+F*************************************************************************1216 * Function:1217 * aic7xxx_busy_target1218 *1219 * Description:1220 * Set the specified target active.1221 *-F*************************************************************************/1222 staticvoid1223 aic7xxx_busy_target(unsignedchartarget, charchannel, intbase)
/* */1224 {1225 unsignedcharactive;
1226 unsignedlongactive_port = ACTIVE_A + base;
1227
1228 if ((target > 0x07) || (channel == 'B'))
1229 {1230 /*1231 * targets on the Second channel or above id 7 store info in byte two1232 * of ACTIVE1233 */1234 active_port++;
1235 }1236 active = inb(active_port);
1237 active |= (0x01 << (target & 0x07));
1238 outb(active, active_port);
1239 }1240
1241 /*+F*************************************************************************1242 * Function:1243 * aic7xxx_unbusy_target1244 *1245 * Description:1246 * Set the specified target inactive.1247 *-F*************************************************************************/1248 staticvoid1249 aic7xxx_unbusy_target(unsignedchartarget, charchannel, intbase)
/* */1250 {1251 unsignedcharactive;
1252 unsignedlongactive_port = ACTIVE_A + base;
1253
1254 #ifdef 0
1255 printk ("aic7xxx: (unbusy_target) target/channel %d/%c\n",
1256 target, channel);
1257 #endif1258 if ((target > 0x07) || (channel == 'B'))
1259 {1260 /*1261 * targets on the Second channel or above id 7 store info in byte two1262 * of ACTIVE1263 */1264 active_port++;
1265 }1266 active = inb(active_port);
1267 active &= ~(0x01 << (target & 0x07));
1268 outb(active, active_port);
1269 }1270
1271 /*+F*************************************************************************1272 * Function:1273 * aic7xxx_done1274 *1275 * Description:1276 * Calls the higher level scsi done function and frees the scb.1277 *-F*************************************************************************/1278 staticvoid1279 aic7xxx_done(structaic7xxx_host *p, structaic7xxx_scb *scb)
/* */1280 {1281 longflags;
1282 Scsi_Cmnd *cmd = scb->cmd;
1283
1284 #ifdef 0
1285 printk ("aic7xxx: (done) target/channel %d/%d\n",
1286 cmd->target, cmd->channel);
1287 #endif1288 /*1289 * This is a critical section, since we don't want the1290 * queue routine mucking with the host data.1291 */1292 save_flags(flags);
1293 cli();
1294
1295 /*1296 * Process the command after marking the scb as free1297 * and adding it to the free list.1298 */1299 scb->state = SCB_FREE;
1300 scb->next = p->free_scb;
1301 p->free_scb = scb;
1302 scb->cmd = NULL;
1303
1304 restore_flags(flags);
1305 cmd->scsi_done(cmd);
1306 }1307
1308 /*+F*************************************************************************1309 * Function:1310 * aic7xxx_done_aborted_scbs1311 *1312 * Description:1313 * Calls the scsi_done() for the Scsi_Cmnd of each scb in the1314 * aborted list, and adds each scb to the free list.1315 *-F*************************************************************************/1316 staticvoid1317 aic7xxx_done_aborted_scbs (structaic7xxx_host *p)
/* */1318 {1319 Scsi_Cmnd *cmd;
1320 structaic7xxx_scb *scb;
1321 inti;
1322
1323 #ifdefAIC7XXX_DEBUG_ABORT1324 intscb_aborted = 0;
1325 printk("aic7xxx: (done_aborted_scbs) calling scsi_done() for aborted scbs\n");
1326 #endif1327
1328 for (i = 0; i < p->numscb; i++)
1329 {1330 scb = &(p->scb_array[i]);
1331 if (scb->state & SCB_QUEUED_FOR_DONE)
1332 {1333 #ifdefAIC7XXX_DEBUG_ABORT1334 if (scb_aborted == 0)
1335 {1336 printk("aic7xxx: (done_aborted_scbs) Aborting scb %d", scb->position);
1337 scb_aborted++;
1338 }1339 else1340 {1341 printk(", %d", scb->position);
1342 }1343 #endif1344 /*1345 * Process the command after marking the scb as free1346 * and adding it to the free list.1347 */1348 scb->state = SCB_FREE;
1349 scb->next = p->free_scb;
1350 p->free_scb = scb;
1351 cmd = scb->cmd;
1352 scb->cmd = NULL;
1353 cmd->scsi_done(cmd); /* call the done function */1354 }1355 }1356 #ifdefAIC7XXX_DEBUG_ABORT1357 if (scb_aborted != 0)
1358 printk("\n");
1359 #endif1360 }1361
1362 /*+F*************************************************************************1363 * Function:1364 * aic7xxx_add_waiting_scb1365 *1366 * Description:1367 * Add this SCB to the head of the "waiting for selection" list.1368 *-F*************************************************************************/1369 staticvoid1370 aic7xxx_add_waiting_scb(u_longbase,
/* */1371 structaic7xxx_scb *scb)
1372 {1373 unsignedcharnext;
1374 unsignedcharcurscb;
1375
1376 curscb = inb(SCBPTR + base);
1377 next = inb(WAITING_SCBH + base);
1378
1379 outb(scb->position, SCBPTR + base);
1380 outb(next, SCB_NEXT_WAITING + base);
1381 outb(scb->position, WAITING_SCBH + base);
1382
1383 outb(curscb, SCBPTR + base);
1384 }1385
1386 /*+F*************************************************************************1387 * Function:1388 * aic7xxx_abort_waiting_scb1389 *1390 * Description:1391 * Manipulate the waiting for selection list and return the1392 * scb that follows the one that we remove.1393 *-F*************************************************************************/1394 staticunsignedchar1395 aic7xxx_abort_waiting_scb(structaic7xxx_host *p, structaic7xxx_scb *scb,
/* */1396 unsignedcharprev, unsignedchartimedout_scb)
1397 {1398 unsignedcharcurscb, next;
1399 inttarget = (scb->target_channel_lun >> 4) & 0x0F;
1400 charchannel = (scb->target_channel_lun & SELBUSB) ? 'B' : 'A';
1401 intbase = p->base;
1402
1403 /*1404 * Select the SCB we want to abort and pull the next pointer out of it.1405 */1406 curscb = inb(SCBPTR + base);
1407 outb(scb->position, SCBPTR + base);
1408 next = inb(SCB_NEXT_WAITING + base);
1409
1410 /*1411 * Clear the necessary fields1412 */1413 outb(0, SCB_CONTROL + base);
1414 outb(SCB_LIST_NULL, SCB_NEXT_WAITING + base);
1415 aic7xxx_unbusy_target(target, channel, base);
1416
1417 /*1418 * Update the waiting list1419 */1420 if (prev == SCB_LIST_NULL)
1421 {1422 /*1423 * First in the list1424 */1425 outb(next, WAITING_SCBH + base);
1426 }1427 else1428 {1429 /*1430 * Select the scb that pointed to us and update its next pointer.1431 */1432 outb(prev, SCBPTR + base);
1433 outb(next, SCB_NEXT_WAITING + base);
1434 }1435 /*1436 * Point us back at the original scb position and inform the SCSI1437 * system that the command has been aborted.1438 */1439 outb(curscb, SCBPTR + base);
1440 scb->state |= SCB_ABORTED | SCB_QUEUED_FOR_DONE;
1441 scb->cmd->result = (DID_RESET << 16);
1442
1443 #ifdefAIC7XXX_DEBUG_ABORT1444 printk ("aic7xxx: (abort_waiting_scb) target/channel %d/%c, prev %d, "
1445 "to_scb %d, next %d\n", target, channel, prev, timedout_scb, next);
1446 #endif1447 return (next);
1448 }1449
1450 /*+F*************************************************************************1451 * Function:1452 * aic7xxx_reset_device1453 *1454 * Description:1455 * The device at the given target/channel has been reset. Abort1456 * all active and queued scbs for that target/channel.1457 *-F*************************************************************************/1458 staticint1459 aic7xxx_reset_device(structaic7xxx_host *p, inttarget, charchannel,
/* */1460 unsignedchartimedout_scb)
1461 {1462 intbase = p->base;
1463 structaic7xxx_scb *scb;
1464 unsignedcharactive_scb;
1465 inti = 0;
1466 intfound = 0;
1467
1468 /*1469 * Restore this when we're done1470 */1471 active_scb = inb(SCBPTR + base);
1472
1473 #ifdefAIC7XXX_DEBUG_ABORT1474 printk ("aic7xxx: (reset_device) target/channel %d/%c, to_scb %d, "
1475 "active_scb %d\n", target, channel, timedout_scb, active_scb);
1476 #endif1477 /*1478 * Search the QINFIFO.1479 */1480 {1481 intsaved_queue[AIC7XXX_MAXSCB];
1482 intqueued = inb(QINCNT + base);
1483
1484 #ifdefAIC7XXX_DEBUG_ABORT1485 if (queued)
1486 printk ("aic7xxx: (reset_device) found %d SCBs in queue\n", queued);
1487 #endif1488 for (i = 0; i < (queued - found); i++)
1489 {1490 saved_queue[i] = inb(QINFIFO + base);
1491 scb = &(p->scb_array[saved_queue[i]]);
1492 if (aic7xxx_match_scb(scb, target, channel))
1493 {1494 /*1495 * We found an scb that needs to be aborted.1496 */1497 #ifdefAIC7XXX_DEBUG_ABORT1498 printk ("aic7xxx: (reset_device) aborting SCB %d\n", saved_queue[i]);
1499 #endif1500 scb->state |= SCB_ABORTED | SCB_QUEUED_FOR_DONE;
1501 scb->cmd->result = (DID_RESET << 16);
1502 outb(scb->position, SCBPTR + base);
1503 outb(0, SCBARRAY + base);
1504 i--;
1505 found++;
1506 }1507 }1508 /*1509 * Now put the saved scbs back.1510 */1511 for (queued = 0; queued < i; queued++)
1512 {1513 outb(saved_queue[queued], QINFIFO + base);
1514 }1515 }1516
1517 /*1518 * Search waiting for selection list.1519 */1520 {1521 unsignedcharnext, prev;
1522
1523 next = inb(WAITING_SCBH + base); /* Start at head of list. */1524 prev = SCB_LIST_NULL;
1525
1526 #ifdefAIC7XXX_DEBUG_ABORT1527 printk ("aic7xxx: (reset_device) Searching waiting SCBs, head %d\n", next);
1528 #endif1529 while (next != SCB_LIST_NULL)
1530 {1531 scb = &(p->scb_array[next]);
1532 /*1533 * Select the SCB.1534 */1535 if (aic7xxx_match_scb(scb, target, channel))
1536 {1537 next = aic7xxx_abort_waiting_scb(p, scb, prev, timedout_scb);
1538 found++;
1539 }1540 else1541 {1542 outb(scb->position, SCBPTR + base);
1543 prev = next;
1544 next = inb(SCB_NEXT_WAITING + base);
1545 }1546 }1547 }1548
1549 /*1550 * Go through the entire SCB array now and look for commands for1551 * for this target that are active. These are other (most likely1552 * tagged) commands that were disconnected when the reset occurred.1553 */1554 for (i = 0; i < p->numscb; i++)
1555 {1556 scb = &(p->scb_array[i]);
1557 if ((scb->state & SCB_ACTIVE) && aic7xxx_match_scb(scb, target, channel))
1558 {1559 /*1560 * Ensure the target is "free"1561 */1562 #ifdefAIC7XXX_DEBUG_ABORT1563 printk ("aic7xxx: (reset_device) freeing disconnected SCB %d\n", i);
1564 #endif1565 aic7xxx_unbusy_target(target, channel, base);
1566 outb(scb->position, SCBPTR + base);
1567 outb(0, SCBARRAY + base);
1568 scb->state |= SCB_ABORTED | SCB_QUEUED_FOR_DONE;
1569 scb->cmd->result = (DID_RESET << 16);
1570 found++;
1571 }1572 }1573
1574 outb(active_scb, SCBPTR + base);
1575 return (found);
1576 }1577
1578 /*+F*************************************************************************1579 * Function:1580 * aic7xxx_reset_current_bus1581 *1582 * Description:1583 * Reset the current SCSI bus.1584 *-F*************************************************************************/1585 staticvoid1586 aic7xxx_reset_current_bus(intbase)
/* */1587 {1588 #ifdefAIC7XXX_DEBUG_ABORT1589 printk ("aic7xxx: (reset_current_bus)\n");
1590 #endif1591 outb(SCSIRSTO, SCSISEQ + base);
1592 udelay(1000);
1593 outb(0, SCSISEQ + base);
1594 }1595
1596 /*+F*************************************************************************1597 * Function:1598 * aic7xxx_reset_channel1599 *1600 * Description:1601 * Reset the channel.1602 *-F*************************************************************************/1603 staticint1604 aic7xxx_reset_channel(structaic7xxx_host *p, charchannel,
/* */1605 unsignedchartimedout_scb, intinitiate_reset)
1606 {1607 intbase = p->base;
1608 unsignedcharsblkctl;
1609 charcur_channel;
1610 unsignedlongoffset, offset_max;
1611 intfound;
1612
1613 #ifdefAIC7XXX_DEBUG_ABORT1614 printk ("aic7xxx: (reset_channel) channel %c, to_scb %d\n",
1615 channel, timedout_scb);
1616 #endif1617 /*1618 * Clean up all the state information for the1619 * pending transactions on this bus.1620 */1621 found = aic7xxx_reset_device(p, ALL_TARGETS, channel, timedout_scb);
1622
1623 if (channel == 'B')
1624 {1625 p->needsdtr |= (p->needsdtr_copy & 0xFF00);
1626 p->sdtr_pending &= 0x00FF;
1627 outb(0, ACTIVE_B + base);
1628 offset = TARG_SCRATCH + base + 8;
1629 offset_max = TARG_SCRATCH + base + 16;
1630 }1631 else1632 {1633 if (p->bus_type == AIC_WIDE)
1634 {1635 p->needsdtr = p->needsdtr_copy;
1636 p->needwdtr = p->needwdtr_copy;
1637 p->sdtr_pending = 0x0;
1638 p->wdtr_pending = 0x0;
1639 outb(0, ACTIVE_A + base);
1640 outb(0, ACTIVE_B + base);
1641 offset = TARG_SCRATCH + base;
1642 offset_max = TARG_SCRATCH + base + 16;
1643 }1644 else1645 {1646 p->needsdtr |= (p->needsdtr_copy & 0x00FF);
1647 p->sdtr_pending &= 0xFF00;
1648 outb(0, ACTIVE_A + base);
1649 offset = TARG_SCRATCH + base;
1650 offset_max = TARG_SCRATCH + base + 8;
1651 }1652 }1653 while (offset < offset_max)
1654 {1655 /*1656 * Revert to async/narrow transfers1657 * until we renegotiate.1658 */1659 u_chartarg_scratch;
1660 targ_scratch = inb(offset);
1661 targ_scratch &= SXFR;
1662 outb(targ_scratch, offset);
1663 offset++;
1664 }1665
1666 /*1667 * Reset the bus and unpause/restart the controller1668 */1669
1670 /*1671 * Case 1: Command for another bus is active1672 */1673 sblkctl = inb(SBLKCTL + base);
1674 cur_channel = (sblkctl & SELBUSB) ? 'B' : 'A';
1675 if (cur_channel != channel)
1676 {1677 #ifdefAIC7XXX_DEBUG_ABORT1678 printk ("aic7xxx: (reset_channel) Stealthily resetting channel %c\n",
1679 channel);
1680 #endif1681 /*1682 * Stealthily reset the other bus without upsetting the current bus1683 */1684 outb(sblkctl ^ SELBUSB, SBLKCTL + base);
1685 if (initiate_reset)
1686 {1687 aic7xxx_reset_current_bus(base);
1688 }1689 /* Ensure we don't get a RSTI interrupt from this. */1690 outb(CLRSCSIRSTI | CLRSELTIMEO, CLRSINT1 + base);
1691 outb(CLRSCSIINT, CLRINT + base);
1692 outb(sblkctl, SBLKCTL + base);
1693
1694 UNPAUSE_SEQUENCER(p);
1695 }1696 /*1697 * Case 2: A command from this bus is active or we're idle1698 */1699 else1700 {1701 #ifdefAIC7XXX_DEBUG_ABORT1702 printk ("aic7xxx: (reset_channel) Resetting current channel %c\n",
1703 channel);
1704 #endif1705 if (initiate_reset)
1706 {1707 aic7xxx_reset_current_bus(base);
1708 }1709 /* Ensure we don't get a RSTI interrupt from this. */1710 outb(CLRSCSIRSTI | CLRSELTIMEO, CLRSINT1 + base);
1711 outb(CLRSCSIINT, CLRINT + base);
1712
1713 RESTART_SEQUENCER(p);
1714 #ifdefAIC7XXX_DEBUG_ABORT1715 printk ("aic7xxx: (reset_channel) Channel reset, sequencer restarted\n");
1716 #endif1717 }1718
1719 /*1720 * Set the time of last reset.1721 */1722 p->host->last_reset = jiffies;
1723
1724 /*1725 * Now loop through all the SCBs that have been marked for abortion,1726 * and call the scsi_done routines.1727 */1728 aic7xxx_done_aborted_scbs(p);
1729 returnfound;
1730 }1731
1732 /*+F*************************************************************************1733 * Function:1734 * aic7xxx_isr1735 *1736 * Description:1737 * SCSI controller interrupt handler.1738 *1739 * NOTE: Since we declared this using SA_INTERRUPT, interrupts should1740 * be disabled all through this function unless we say otherwise.1741 *-F*************************************************************************/1742 staticvoid1743 aic7xxx_isr(intirq, void *dev_id, structpt_regs * regs)
/* */1744 {1745 intbase, intstat;
1746 structaic7xxx_host *p;
1747 structaic7xxx_scb *scb;
1748 unsignedcharha_flags;
1749 shorttransfer;
1750 unsignedcharscsi_id, bus_width;
1751 unsignedcharoffset, rate, scratch, scratch_offset;
1752 unsignedcharmax_offset, rej_byte;
1753 unsignedshorttarget_mask;
1754 charchannel;
1755 void *addr;
1756 intactual;
1757 intscb_index;
1758 Scsi_Cmnd *cmd;
1759
1760 p = (structaic7xxx_host *) aic7xxx_boards[irq]->hostdata;
1761
1762 /*1763 * Search for the host with a pending interrupt. If we can't find1764 * one, then we've encountered a spurious interrupt.1765 */1766 while ((p != NULL) && !(inb(INTSTAT + p->base) & INT_PEND))
1767 {1768 if (p->next == NULL)
1769 {1770 p = NULL;
1771 }1772 else1773 {1774 p = (structaic7xxx_host *) p->next->hostdata;
1775 }1776 }1777
1778 if (p == NULL)
1779 {1780 if (aic7xxx_spurious_count == 1)
1781 {1782 aic7xxx_spurious_count = 2;
1783 printk("aic7xxx: (aic7xxx_isr) Encountered spurious interrupt.\n");
1784 return;
1785 }1786 else1787 {1788 /*1789 * The best we can do is to set p back to head of list and process1790 * the erroneous interrupt - most likely a BRKADRINT.1791 */1792 p = (structaic7xxx_host *) aic7xxx_boards[irq]->hostdata;
1793 }1794 }1795
1796 /*1797 * Keep track of interrupts for /proc/scsi1798 */1799 p->isr_count++;
1800
1801 if (!p->a_scanned && (p->isr_count == 1))
1802 {1803 /*1804 * We must only have one card at this IRQ and it must have been1805 * added to the board data before the spurious interrupt occurred.1806 * It is sufficient that we check isr_count and not the spurious1807 * interrupt count.1808 */1809 printk("aic7xxx: (aic7xxx_isr) Encountered spurious interrupt.\n");
1810 return;
1811 }1812
1813 base = p->base;
1814 /*1815 * Handle all the interrupt sources - especially for SCSI1816 * interrupts, we won't get a second chance at them.1817 */1818 intstat = inb(INTSTAT + base);
1819
1820 if (intstat & BRKADRINT)
1821 {1822 inti;
1823 unsignedcharerrno = inb(ERROR + base);
1824
1825 printk("aic7xxx: (aic7xxx_isr) BRKADRINT error(0x%x):\n", errno);
1826 for (i = 0; i < NUMBER(hard_error); i++)
1827 {1828 if (errno & hard_error[i].errno)
1829 {1830 printk(" %s\n", hard_error[i].errmesg);
1831 }1832 }1833 panic("aic7xxx: (aic7xxx_isr) BRKADRINT, error(0x%x) seqaddr(0x%x).\n",
1834 inb(ERROR + base), (inb(SEQADDR1 + base) << 8) | inb(SEQADDR0 + base));
1835 }1836
1837 if (intstat & SEQINT)
1838 {1839 /*1840 * Although the sequencer is paused immediately on1841 * a SEQINT, an interrupt for a SCSIINT condition will1842 * unpaused the sequencer before this point.1843 */1844 PAUSE_SEQUENCER(p);
1845
1846 scsi_id = (inb(SCSIID + base) >> 4) & 0x0F;
1847 scratch_offset = scsi_id;
1848 channel = 'A';
1849 if (inb(SBLKCTL + base) & SELBUSB)
1850 {1851 channel = 'B';
1852 scratch_offset += 8;
1853 }1854 target_mask = (0x01 << scratch_offset);
1855
1856 switch (intstat & SEQINT_MASK)
1857 {1858 caseBAD_PHASE:
1859 panic("aic7xxx: (aic7xxx_isr) Unknown scsi bus phase.\n");
1860 break;
1861
1862 caseSEND_REJECT:
1863 rej_byte = inb(REJBYTE + base);
1864 if ((rej_byte & 0xF0) == 0x20)
1865 {1866 scb_index = inb(SCBPTR + base);
1867 scb = &(p->scb_array[scb_index]);
1868 printk("aic7xxx: Warning - Tagged message received without identify."
1869 "Disabling tagged commands for target %d channel %c.\n",
1870 scsi_id, channel);
1871 scb->cmd->device->tagged_supported = 0;
1872 scb->cmd->device->tagged_queue = 0;
1873 }1874 else1875 {1876 debug("aic7xxx: Warning - Rejecting unknown message (0x%x) received "
1877 "from target %d channel %c.\n", rej_byte, scsi_id, channel);
1878 }1879 break;
1880
1881 caseNO_IDENT:
1882 panic("aic7xxx: Target %d, channel %c, did not send an IDENTIFY "
1883 "message. SAVED_TCL(0x%x).\n",
1884 scsi_id, channel, inb(SAVED_TCL + base));
1885 break;
1886
1887 caseNO_MATCH:
1888 printk("aic7xxx: No active SCB for reconnecting target %d, "
1889 "channel %c - Issuing ABORT. SAVED_TCL(0x%x).\n",
1890 scsi_id, channel, inb(SAVED_TCL + base));
1891 aic7xxx_unbusy_target(scsi_id, channel, base);
1892 outb(0, SCBARRAY + base);
1893 outb(CLRSELTIMEO, CLRSINT1 + base);
1894 RESTART_SEQUENCER(p);
1895 break;
1896
1897 caseSDTR_MSG:
1898 /*1899 * Help the sequencer to translate the negotiated1900 * transfer rate. Transfer is 1/4 the period1901 * in ns as is returned by the sync negotiation1902 * message. So, we must multiply by four.1903 */1904 transfer = (inb(ARG_1 + base) << 2);
1905 offset = inb(ACCUM + base);
1906 scratch = inb(TARG_SCRATCH + base + scratch_offset);
1907 /*1908 * The maximum offset for a wide device is 0x08; for a1909 * 8-bit bus device the maximum offset is 0x0F.1910 */1911 if (scratch & WIDEXFER)
1912 {1913 max_offset = 0x08;
1914 }1915 else1916 {1917 max_offset = 0x0F;
1918 }1919 aic7xxx_scsirate(p, &rate, transfer, MIN(offset, max_offset),
1920 scsi_id, channel);
1921 /*1922 * Preserve the wide transfer flag.1923 */1924 scratch = rate | (scratch & WIDEXFER);
1925 outb(scratch, TARG_SCRATCH + base + scratch_offset);
1926 outb(scratch, SCSIRATE + base);
1927 if ((scratch & 0x0F) == 0)
1928 {/*1929 * The requested rate was so low that asynchronous transfers1930 * are faster (not to mention the controller won't support1931 * them), so we issue a reject to ensure we go to asynchronous1932 * transfers.1933 */1934 outb(SEND_REJ, RETURN_1 + base);
1935 }1936 else1937 {1938 /*1939 * See if we initiated Sync Negotiation1940 */1941 if (p->sdtr_pending & target_mask)
1942 {1943 /*1944 * Don't send an SDTR back to the target.1945 */1946 outb(0, RETURN_1 + base);
1947 }1948 else1949 {1950 /*1951 * Send our own SDTR in reply.1952 */1953 printk("aic7xxx: Sending SDTR!!\n");
1954 outb(SEND_SDTR, RETURN_1 + base);
1955 }1956 }1957 /*1958 * Clear the flags.1959 */1960 p->needsdtr &= ~target_mask;
1961 p->sdtr_pending &= ~target_mask;
1962 break;
1963
1964 caseWDTR_MSG:
1965 {1966 bus_width = inb(ARG_1 + base);
1967 printk("aic7xxx: Received MSG_WDTR, Target %d, channel %c "
1968 "needwdtr(0x%x).\n", scsi_id, channel, p->needwdtr);
1969 scratch = inb(TARG_SCRATCH + base + scratch_offset);
1970
1971 if (p->wdtr_pending & target_mask)
1972 {1973 /*1974 * Don't send an WDTR back to the target, since we asked first.1975 */1976 outb(0, RETURN_1 + base);
1977 switch (bus_width)
1978 {1979 caseBUS_8_BIT:
1980 scratch &= 0x7F;
1981 break;
1982
1983 caseBUS_16_BIT:
1984 printk("aic7xxx: Target %d, channel %c, using 16 bit "
1985 "transfers.\n", scsi_id, channel);
1986 scratch |= 0x80;
1987 break;
1988
1989 caseBUS_32_BIT:
1990 outb(SEND_REJ, RETURN_1 + base);
1991 printk("aic7xxx: Target %d, channel %c, requesting 32 bit "
1992 "transfers, rejecting...\n", scsi_id, channel);
1993 break;
1994 }1995 }1996 else1997 {1998 /*1999 * Send our own WDTR in reply.2000 */2001 printk("aic7xxx: Will send WDTR!!\n");
2002 switch (bus_width)
2003 {2004 caseBUS_8_BIT:
2005 scratch &= 0x7F;
2006 break;
2007
2008 caseBUS_32_BIT:
2009 /*2010 * Negotiate 16 bits.2011 */2012 bus_width = BUS_16_BIT;
2013 /* Yes, we mean to fall thru here. */2014
2015 caseBUS_16_BIT:
2016 printk("aic7xxx: Target %d, channel %c, using 16 bit "
2017 "transfers.\n", scsi_id, channel);
2018 scratch |= 0x80;
2019 break;
2020 }2021 outb(bus_width | SEND_WDTR, RETURN_1 + base);
2022 }2023 p->needwdtr &= ~target_mask;
2024 p->wdtr_pending &= ~target_mask;
2025 outb(scratch, TARG_SCRATCH + base + scratch_offset);
2026 outb(scratch, SCSIRATE + base);
2027 break;
2028 }2029
2030 caseREJECT_MSG:
2031 {2032 /*2033 * What we care about here is if we had an2034 * outstanding SDTR or WDTR message for this2035 * target. If we did, this is a signal that2036 * the target is refusing negotiation.2037 */2038
2039 scratch = inb(TARG_SCRATCH + base + scratch_offset);
2040
2041 if (p->wdtr_pending & target_mask)
2042 {2043 /*2044 * note 8bit xfers and clear flag2045 */2046 scratch &= 0x7F;
2047 p->needwdtr &= ~target_mask;
2048 p->wdtr_pending &= ~target_mask;
2049 printk("aic7xxx: Target %d, channel %c, refusing WIDE negotiation. "
2050 "Using 8 bit transfers.\n", scsi_id, channel);
2051 }2052 else2053 {2054 if (p->sdtr_pending & target_mask)
2055 {2056 /*2057 * note asynch xfers and clear flag2058 */2059 scratch &= 0xF0;
2060 p->needsdtr &= ~target_mask;
2061 p->sdtr_pending &= ~target_mask;
2062 printk("aic7xxx: Target %d, channel %c, refusing synchronous "
2063 "negotiation. Using asynchronous transfers.\n",
2064 scsi_id, channel);
2065 }2066 /*2067 * Otherwise, we ignore it.2068 */2069 }2070 outb(scratch, TARG_SCRATCH + base + scratch_offset);
2071 outb(scratch, SCSIRATE + base);
2072 break;
2073 }2074
2075 caseBAD_STATUS:
2076 scb_index = inb(SCBPTR + base);
2077 scb = &(p->scb_array[scb_index]);
2078 outb(0, RETURN_1 + base); /* CHECK_CONDITION may change this */2079 if (!(scb->state & SCB_ACTIVE) || (scb->cmd == NULL))
2080 {2081 printk("aic7xxx: Referenced SCB not valid during SEQINT(0x%x) "
2082 "scb(%d) state(0x%x) cmd(0x%x).\n",
2083 intstat, scb_index, scb->state, (unsignedint) scb->cmd);
2084 }2085 else2086 {2087 cmd = scb->cmd;
2088 aic7xxx_getscb(p, scb);
2089 aic7xxx_status(cmd) = scb->target_status;
2090
2091 cmd->result |= scb->target_status;
2092
2093 switch (status_byte(scb->target_status))
2094 {2095 caseGOOD:
2096 printk("aic7xxx: Interrupted for status of GOOD???\n");
2097 break;
2098
2099 caseCHECK_CONDITION:
2100 if ((aic7xxx_error(cmd) == 0) && !(cmd->flags & WAS_SENSE))
2101 {2102 unsignedchartcl;
2103 unsignedcharcontrol;
2104 void *req_buf;
2105
2106 tcl = scb->target_channel_lun;
2107
2108 /*2109 * Send a sense command to the requesting target.2110 */2111 cmd->flags |= WAS_SENSE;
2112 memcpy((void *) scb->sense_cmd, (void *) generic_sense,
2113 sizeof(generic_sense));
2114
2115 scb->sense_cmd[1] = (cmd->lun << 5);
2116 scb->sense_cmd[4] = sizeof(cmd->sense_buffer);
2117
2118 scb->sense_sg.address = (char *) &cmd->sense_buffer;
2119 scb->sense_sg.length = sizeof(cmd->sense_buffer);
2120 req_buf = &scb->sense_sg;
2121 cmd->cmd_len = COMMAND_SIZE(cmd->cmnd[0]);
2122 control = scb->control;
2123
2124 memset(scb, 0, SCB_PIO_TRANSFER_SIZE);
2125 scb->control = control & DISCENB;
2126 scb->target_channel_lun = tcl;
2127 addr = scb->sense_cmd;
2128 scb->SCSI_cmd_length = COMMAND_SIZE(scb->sense_cmd[0]);
2129 memcpy(scb->SCSI_cmd_pointer, &addr,
2130 sizeof(scb->SCSI_cmd_pointer));
2131 scb->SG_segment_count = 1;
2132 memcpy(scb->SG_list_pointer, &req_buf,
2133 sizeof(scb->SG_list_pointer));
2134 scb->data_count = scb->sense_sg.length;
2135 memcpy(scb->data_pointer, &(scb->sense_sg.address), 4);
2136
2137 aic7xxx_putscb(p, scb);
2138 outb(SCB_LIST_NULL, SCB_NEXT_WAITING + base);
2139 /*2140 * Ensure that the target is "BUSY" so we don't get overlapping2141 * commands if we happen to be doing tagged I/O.2142 */2143 aic7xxx_busy_target(scsi_id, channel, base);
2144
2145 aic7xxx_add_waiting_scb(base, scb);
2146 outb(SEND_SENSE, RETURN_1 + base);
2147 }/* first time sense, no errors */2148 else2149 {2150 cmd->flags &= ~ASKED_FOR_SENSE;
2151 if (aic7xxx_error(cmd) == 0)
2152 {2153 aic7xxx_error(cmd) = DID_RETRY_COMMAND;
2154 }2155 }2156 break;
2157
2158 caseBUSY:
2159 printk("aic7xxx: Target busy.\n");
2160 if (!aic7xxx_error(cmd))
2161 {2162 aic7xxx_error(cmd) = DID_BUS_BUSY;
2163 }2164 break;
2165
2166 caseQUEUE_FULL:
2167 printk("aic7xxx: Queue full.\n");
2168 if (!aic7xxx_error(cmd))
2169 {2170 aic7xxx_error(cmd) = DID_RETRY_COMMAND;
2171 }2172 break;
2173
2174 default:
2175 printk("aic7xxx: Unexpected target status(0x%x).\n",
2176 scb->target_status);
2177 if (!aic7xxx_error(cmd))
2178 {2179 aic7xxx_error(cmd) = DID_RETRY_COMMAND;
2180 }2181 break;
2182 }/* end switch */2183 }/* end else of */2184 break;
2185
2186 caseRESIDUAL:
2187 scb_index = inb(SCBPTR + base);
2188 scb = &(p->scb_array[scb_index]);
2189 if (!(scb->state & SCB_ACTIVE) || (scb->cmd == NULL))
2190 {2191 printk("aic7xxx: Referenced SCB not valid during SEQINT(0x%x) "
2192 "scb(%d) state(0x%x) cmd(0x%x).\n",
2193 intstat, scb_index, scb->state, (unsignedint) scb->cmd);
2194 }2195 else2196 {2197 cmd = scb->cmd;
2198 /*2199 * Don't destroy valid residual information with2200 * residual coming from a check sense operation.2201 */2202 if (!(cmd->flags & WAS_SENSE))
2203 {2204 /*2205 * We had an underflow. At this time, there's only2206 * one other driver that bothers to check for this,2207 * and cmd->underflow seems to be set rather half-2208 * heartedly in the higher-level SCSI code.2209 */2210 actual = aic7xxx_length(cmd, scb->residual_SG_segment_count);
2211
2212 actual -= (inb(SCB_RESID_DCNT2 + base) << 16) |
2213 (inb(SCB_RESID_DCNT1 + base) << 8) |
2214 inb(SCB_RESID_DCNT0 + base);
2215
2216 if (actual < cmd->underflow)
2217 {2218 printk("aic7xxx: Target %d underflow - "
2219 "Wanted (at least) (%u) got(%u) count(%d).\n",
2220 cmd->target, cmd->underflow, actual,
2221 inb(SCB_RESID_SGCNT + base));
2222 aic7xxx_error(cmd) = DID_RETRY_COMMAND;
2223 aic7xxx_status(cmd) = scb->target_status;
2224 }2225 }2226 }2227 break;
2228
2229 caseABORT_TAG:
2230 scb_index = inb(SCBPTR + base);
2231 scb = &(p->scb_array[scb_index]);
2232 if (!(scb->state & SCB_ACTIVE) || (scb->cmd == NULL))
2233 {2234 printk("aic7xxx: Referenced SCB not valid during SEQINT(0x%x) "
2235 "scb(%d) state(0x%x) cmd(0x%x)\n",
2236 intstat, scb_index, scb->state, (unsignedint) scb->cmd);
2237 }2238 else2239 {2240 cmd = scb->cmd;
2241 /*2242 * We didn't receive a valid tag back from the target2243 * on a reconnect.2244 */2245 printk("aic7xxx: Invalid tag received on target %d, channel %c, "
2246 "lun %d - Sending ABORT_TAG.\n",
2247 scsi_id, channel, cmd->lun & 0x07);
2248
2249 cmd->result = (DID_RETRY_COMMAND << 16);
2250 aic7xxx_done(p, scb);
2251 }2252 break;
2253
2254 caseAWAITING_MSG:
2255 scb_index = inb(SCBPTR + base);
2256 scb = &(p->scb_array[scb_index]);
2257 if (!(scb->state & SCB_ACTIVE) || (scb->cmd == NULL))
2258 {2259 printk("aic7xxx: Referenced SCB not valid during SEQINT(0x%x) "
2260 "scb(%d) state(0x%x) cmd(0x%x).\n",
2261 intstat, scb_index, scb->state, (unsignedint) scb->cmd);
2262 }2263 else2264 {2265 /*2266 * This SCB had a zero length command, informing the sequencer2267 * that we wanted to send a special message to this target.2268 * We only do this for BUS_DEVICE_RESET messages currently.2269 */2270 if (scb->state & SCB_DEVICE_RESET)
2271 {2272 #ifdefAIC7XXX_DEBUG_ABORT2273 printk ("aic7xxx: (isr) sending bus device reset to target %d\n",
2274 scsi_id);
2275 #endif2276 outb(MSG_BUS_DEVICE_RESET, MSG0 + base);
2277 outb(1, MSG_LEN + base);
2278 }2279 else2280 {2281 panic("aic7xxx: AWAITING_SCB for an SCB that does "
2282 "not have a waiting message.\n");
2283 }2284 }2285 break;
2286
2287 caseIMMEDDONE:
2288 scb_index = inb(SCBPTR + base);
2289 scb = &(p->scb_array[scb_index]);
2290 #ifdefAIC7XXX_DEBUG_ABORT2291 printk ("aic7xxx: (isr) received IMMEDDONE for target %d, scb %d, state %d\n",
2292 scsi_id, scb_index, scb->state);
2293 #endif2294 if (scb->state & SCB_DEVICE_RESET)
2295 {2296 intfound;
2297
2298 /*2299 * Go back to async/narrow transfers and renegotiate.2300 */2301 aic7xxx_unbusy_target(scsi_id, channel, base);
2302 p->needsdtr |= (p->needsdtr_copy & target_mask);
2303 p->needwdtr |= (p->needwdtr_copy & target_mask);
2304 p->sdtr_pending &= ~target_mask;
2305 p->wdtr_pending &= ~target_mask;
2306 scratch = inb(TARG_SCRATCH + base + scratch_offset);
2307 scratch &= SXFR;
2308 outb(scratch, TARG_SCRATCH + base + scratch_offset);
2309 found = aic7xxx_reset_device(p, (int) scsi_id, channel, SCB_LIST_NULL);
2310 aic7xxx_done_aborted_scbs (p);
2311 }2312 else2313 {2314 panic("aic7xxx: Immediate complete for unknown operation.\n");
2315 }2316 break;
2317
2318 #if AIC7XXX_NOT_YET
2319 /* XXX Fill these in later */2320 case MESG_BUFFER_BUSY:
2321 break;
2322 caseMSGIN_PHASEMIS:
2323 break;
2324 #endif2325
2326 default: /* unknown */2327 debug("aic7xxx: SEQINT, INTSTAT(0x%x) SCSISIGI(0x%x).\n",
2328 intstat, inb(SCSISIGI + base));
2329 break;
2330 }2331
2332 /*2333 * Clear the sequencer interrupt and unpause the sequencer.2334 */2335 outb(CLRSEQINT, CLRINT + base);
2336 UNPAUSE_SEQUENCER(p);
2337 }2338
2339 if (intstat & SCSIINT)
2340 {2341 intstatus = inb(SSTAT1 + base);
2342 scsi_id = (inb(SCSIID + base) >> 4) & 0x0F;
2343 channel = 'A';
2344 if (inb(SBLKCTL + base) & SELBUSB)
2345 {2346 channel = 'B';
2347 }2348
2349 scb_index = inb(SCBPTR + base);
2350 scb = &(p->scb_array[scb_index]);
2351 if (!(scb->state & SCB_ACTIVE) || (scb->cmd == NULL))
2352 {2353 printk("aic7xxx: No command for SCB (SCSIINT).\n");
2354 /*2355 * Turn off the interrupt and set status to zero, so that it2356 * falls through the rest of the SCSIINT code.2357 */2358 outb(status, CLRSINT1 + base);
2359 UNPAUSE_SEQUENCER(p);
2360 outb(CLRSCSIINT, CLRINT + base);
2361 scb = NULL;
2362 }2363 elseif (status & SCSIRSTI)
2364 {2365 PAUSE_SEQUENCER(p);
2366 printk ("aic7xxx: Someone reset channel %c (SCSIINT).\n", channel);
2367 /*2368 * Go through and abort all commands for the channel, but do not2369 * reset the channel again.2370 */2371 aic7xxx_reset_channel (p, channel, SCB_LIST_NULL, FALSE);
2372 }2373 elseif (status & SCSIPERR)
2374 {2375 char *phase;
2376 unsignedcharmesg_out = MSG_NOP;
2377 unsignedcharlastphase = inb(LASTPHASE + base);
2378
2379 cmd = scb->cmd;
2380 switch (lastphase)
2381 {2382 caseP_DATAOUT:
2383 phase = "Data-Out";
2384 break;
2385 caseP_DATAIN:
2386 phase = "Data-In";
2387 mesg_out = MSG_INITIATOR_DET_ERROR;
2388 break;
2389 caseP_COMMAND:
2390 phase = "Command";
2391 break;
2392 caseP_MESGOUT:
2393 phase = "Message-Out";
2394 break;
2395 caseP_STATUS:
2396 phase = "Status";
2397 mesg_out = MSG_INITIATOR_DET_ERROR;
2398 break;
2399 caseP_MESGIN:
2400 phase = "Message-In";
2401 mesg_out = MSG_MSG_PARITY_ERROR;
2402 break;
2403 default:
2404 phase = "unknown";
2405 break;
2406 }2407
2408 /*2409 * A parity error has occurred during a data2410 * transfer phase. Flag it and continue.2411 */2412 printk("aic7xxx: Parity error during phase %s on target %d, "
2413 "channel %d, lun %d.\n", phase,
2414 cmd->target, cmd->channel & 0x01, cmd->lun & 0x07);
2415
2416 /*2417 * We've set the hardware to assert ATN if we get a parity2418 * error on "in" phases, so all we need to do is stuff the2419 * message buffer with the appropriate message. In phases2420 * have set mesg_out to something other than MSG_NOP.2421 */2422 if (mesg_out != MSG_NOP)
2423 {2424 outb(mesg_out, MSG0 + base);
2425 outb(1, MSG_LEN + base);
2426 cmd->result = DID_PARITY << 16;
2427 }2428 else2429 {2430 /*2431 * Should we allow the target to make this decision for us?2432 */2433 cmd->result = DID_RETRY_COMMAND << 16;
2434 }2435 aic7xxx_done(p, scb);
2436 }2437 elseif (status & SELTO)
2438 {2439 unsignedcharwaiting;
2440
2441 cmd = scb->cmd;
2442
2443 /*2444 * Hardware selection timer has expired. Turn2445 * off SCSI selection sequence.2446 */2447 outb(ENRSELI, SCSISEQ + base);
2448 cmd->result = (DID_TIME_OUT << 16);
2449 /*2450 * Clear an pending messages for the timed out2451 * target and mark the target as free.2452 */2453 ha_flags = inb(FLAGS + base);
2454 outb(0, MSG_LEN + base);
2455 aic7xxx_unbusy_target(scsi_id, channel, base);
2456
2457 outb(0, SCBARRAY + base);
2458
2459 /*2460 * Shut off the offending interrupt sources, reset2461 * the sequencer address to zero and unpause it,2462 * then call the high-level SCSI completion routine.2463 *2464 * WARNING! This is a magic sequence! After many2465 * hours of guesswork, turning off the SCSI interrupts2466 * in CLRSINT? does NOT clear the SCSIINT bit in2467 * INTSTAT. By writing to the (undocumented, unused2468 * according to the AIC-7770 manual) third bit of2469 * CLRINT, you can clear INTSTAT. But, if you do it2470 * while the sequencer is paused, you get a BRKADRINT2471 * with an Illegal Host Address status, so the2472 * sequencer has to be restarted first.2473 */2474 outb(CLRSELTIMEO, CLRSINT1 + base);
2475
2476 outb(CLRSCSIINT, CLRINT + base);
2477
2478 /*2479 * Shift the waiting for selection queue forward2480 */2481 waiting = inb(WAITING_SCBH + base);
2482 outb(waiting, SCBPTR + base);
2483 waiting = inb(SCB_NEXT_WAITING + base);
2484 outb(waiting, WAITING_SCBH + base);
2485
2486 RESTART_SEQUENCER(p);
2487 aic7xxx_done(p, scb);
2488 #if 0
2489 printk("aic7xxx: SELTO SCB(%d) state(0x%x) cmd(0x%x).\n",
2490 scb->position, scb->state, (unsignedint) scb->cmd);
2491 #endif2492 }2493 elseif (!(status & BUSFREE))
2494 {2495 /*2496 * We don't know what's going on. Turn off the2497 * interrupt source and try to continue.2498 */2499 printk("aic7xxx: SSTAT1(0x%x).\n", status);
2500 outb(status, CLRSINT1 + base);
2501 UNPAUSE_SEQUENCER(p);
2502 outb(CLRSCSIINT, CLRINT + base);
2503 }2504 }2505
2506 if (intstat & CMDCMPLT)
2507 {2508 intcomplete;
2509
2510 /*2511 * The sequencer will continue running when it2512 * issues this interrupt. There may be >1 commands2513 * finished, so loop until we've processed them all.2514 */2515 do{2516 complete = inb(QOUTFIFO + base);
2517
2518 scb = &(p->scb_array[complete]);
2519 if (!(scb->state & SCB_ACTIVE) || (scb->cmd == NULL))
2520 {2521 printk("aic7xxx: Warning - No command for SCB %d (CMDCMPLT).\n"
2522 " QOUTCNT(%d) QINCNT(%d) SCB state(0x%x) cmd(0x%x) "
2523 "pos(%d).\n",
2524 complete, inb(QOUTCNT + base), inb(QINCNT + base),
2525 scb->state, (unsignedint) scb->cmd, scb->position);
2526 outb(CLRCMDINT, CLRINT + base);
2527 continue;
2528 }2529 cmd = scb->cmd;
2530 cmd->result |= (aic7xxx_error(cmd) << 16);
2531 if ((cmd->flags & WAS_SENSE) && !(cmd->flags & ASKED_FOR_SENSE))
2532 {2533 /*2534 * Got sense information.2535 */2536 cmd->flags &= ASKED_FOR_SENSE;
2537 }2538 #if 0
2539 printk("aic7xxx: (complete) State(%d) cmd(0x%x) free(0x%x).\n",
2540 scb->state, (unsignedint) scb->cmd, (unsignedint) p->free_scb);
2541 #endif2542
2543 /*2544 * Clear interrupt status before checking2545 * the output queue again. This eliminates2546 * a race condition whereby a command could2547 * complete between the queue poll and the2548 * interrupt clearing, so notification of the2549 * command being complete never made it back2550 * up to the kernel.2551 */2552 outb(CLRCMDINT, CLRINT + base);
2553 aic7xxx_done(p, scb);
2554 #if 0
2555 if (scb != &p->scb_array[scb->position])
2556 {2557 printk("aic7xxx: (complete) Address mismatch, pos(%d).\n", scb->position);
2558 }2559 printk("aic7xxx: (complete) State(%d) cmd(0x%x) free(0x%x).\n",
2560 scb->state, (unsignedint) scb->cmd, (unsignedint) p->free_scb);
2561 #endif2562
2563 #ifdefAIC7XXX_PROC_STATS2564 /*2565 * XXX: we should actually know how much actually transferred2566 * XXX: for each command, but apparently that's too difficult.2567 */2568 actual = aic7xxx_length(cmd, 0);
2569 if (!(cmd->flags & WAS_SENSE) && (actual > 0))
2570 {2571 structaic7xxx_xferstats *sp;
2572 long *ptr;
2573 intx;
2574
2575 sp = &p->stats[cmd->channel & 0x01][cmd->target & 0x0F][cmd->lun & 0x07];
2576 sp->xfers++;
2577
2578 if (cmd->request.cmd == WRITE)
2579 {2580 sp->w_total++;
2581 sp->w_total512 += (actual >> 9);
2582 ptr = sp->w_bins;
2583 }2584 else2585 {2586 sp->r_total++;
2587 sp->r_total512 += (actual >> 9);
2588 ptr = sp->r_bins;
2589 }2590 for (x = 9; x <= 17; x++)
2591 {2592 if (actual < (1 << x))
2593 {2594 ptr[x - 9]++;
2595 break;
2596 }2597 }2598 if (x > 17)
2599 {2600 ptr[x - 9]++;
2601 }2602 }2603 #endif/* AIC7XXX_PROC_STATS */2604
2605 }while (inb(QOUTCNT + base) & p->qcntmask);
2606 }2607 }2608
2609 /*+F*************************************************************************2610 * Function:2611 * aic7xxx_probe2612 *2613 * Description:2614 * Probing for EISA boards: it looks like the first two bytes2615 * are a manufacturer code - three characters, five bits each:2616 *2617 * BYTE 0 BYTE 1 BYTE 2 BYTE 32618 * ?1111122 22233333 PPPPPPPP RRRRRRRR2619 *2620 * The characters are baselined off ASCII '@', so add that value2621 * to each to get the real ASCII code for it. The next two bytes2622 * appear to be a product and revision number, probably vendor-2623 * specific. This is what is being searched for at each port,2624 * and what should probably correspond to the ID= field in the2625 * ECU's .cfg file for the card - if your card is not detected,2626 * make sure your signature is listed in the array.2627 *2628 * The fourth byte's lowest bit seems to be an enabled/disabled2629 * flag (rest of the bits are reserved?).2630 *-F*************************************************************************/2631 staticaha_type2632 aic7xxx_probe(intslot, intbase, aha_status_type *bios)
/* */2633 {2634 inti;
2635 unsignedcharbuf[4];
2636
2637 staticstruct{2638 intn;
2639 unsignedcharsignature[sizeof(buf)];
2640 aha_typetype;
2641 intbios_disabled;
2642 }AIC7xxx[] = {2643 { 4, { 0x04, 0x90, 0x77, 0x71 }, AIC_7771, FALSE}, /* host adapter 274x */2644 { 4, { 0x04, 0x90, 0x77, 0x70 }, AIC_7770, FALSE}, /* motherboard 7770 */2645 { 4, { 0x04, 0x90, 0x77, 0x56 }, AIC_284x, FALSE}, /* 284x BIOS enabled */2646 { 4, { 0x04, 0x90, 0x77, 0x57 }, AIC_284x, TRUE}/* 284x BIOS disabled */2647 };
2648
2649 /*2650 * The VL-bus cards need to be primed by2651 * writing before a signature check.2652 */2653 for (i = 0; i < sizeof(buf); i++)
2654 {2655 outb(0x80 + i, base);
2656 buf[i] = inb(base + i);
2657 }2658
2659 for (i = 0; i < NUMBER(AIC7xxx); i++)
2660 {2661 /*2662 * Signature match on enabled card?2663 */2664 if (!memcmp(buf, AIC7xxx[i].signature, AIC7xxx[i].n))
2665 {2666 if (inb(base + 4) & 1)
2667 {2668 if (AIC7xxx[i].bios_disabled)
2669 {2670 *bios = AIC_DISABLED;
2671 }2672 else2673 {2674 *bios = AIC_ENABLED;
2675 }2676 return (AIC7xxx[i].type);
2677 }2678
2679 printk("aic7xxx: Disabled at slot %d, ignored.\n", slot);
2680 }2681 }2682
2683 return (AIC_NONE);
2684 }2685
2686 /*+F*************************************************************************2687 * Function:2688 * read_2840_seeprom2689 *2690 * Description:2691 * Reads the 2840 serial EEPROM and returns 1 if successful and 0 if2692 * not successful.2693 *2694 * See read_seeprom (for the 2940) for the instruction set of the 93C462695 * chip.2696 *2697 * The 2840 interface to the 93C46 serial EEPROM is through the2698 * STATUS_2840 and SEECTL_2840 registers. The CS_2840, CK_2840, and2699 * DO_2840 bits of the SEECTL_2840 register are connected to the chip2700 * select, clock, and data out lines respectively of the serial EEPROM.2701 * The DI_2840 bit of the STATUS_2840 is connected to the data in line2702 * of the serial EEPROM. The EEPROM_TF bit of STATUS_2840 register is2703 * useful in that it gives us an 800 nsec timer. After a read from the2704 * SEECTL_2840 register the timing flag is cleared and goes high 800 nsec2705 * later.2706 *2707 *-F*************************************************************************/2708 staticint2709 read_2840_seeprom(intbase, structseeprom_config *sc)
/* */2710 {2711 inti = 0, k = 0;
2712 unsignedchartemp;
2713 unsignedshortchecksum = 0;
2714 unsignedshort *seeprom = (unsignedshort *) sc;
2715 structseeprom_cmd{2716 unsignedcharlen;
2717 unsignedcharbits[3];
2718 };
2719 structseeprom_cmdseeprom_read = {3, {1, 1, 0}};
2720
2721 #defineCLOCK_PULSE(p) \
2722 while ((inb(STATUS_2840 + base) & EEPROM_TF) == 0) \
2723 { \
2724 ; /* Do nothing */ \
2725 } \
2726 (void) inb(SEECTL_2840 + base);
2727
2728 /*2729 * Read the first 32 registers of the seeprom. For the 2840,2730 * the 93C46 SEEPROM is a 1024-bit device with 64 16-bit registers2731 * but only the first 32 are used by Adaptec BIOS. The loop2732 * will range from 0 to 31.2733 */2734 for (k = 0; k < (sizeof(*sc) / 2); k++)
2735 {2736 /*2737 * Send chip select for one clock cycle.2738 */2739 outb(CK_2840 | CS_2840, SEECTL_2840 + base);
2740 CLOCK_PULSE(base);
2741
2742 /*2743 * Now we're ready to send the read command followed by the2744 * address of the 16-bit register we want to read.2745 */2746 for (i = 0; i < seeprom_read.len; i++)
2747 {2748 temp = CS_2840 | seeprom_read.bits[i];
2749 outb(temp, SEECTL_2840 + base);
2750 CLOCK_PULSE(base);
2751 temp = temp ^ CK_2840;
2752 outb(temp, SEECTL_2840 + base);
2753 CLOCK_PULSE(base);
2754 }2755 /*2756 * Send the 6 bit address (MSB first, LSB last).2757 */2758 for (i = 5; i >= 0; i--)
2759 {2760 temp = k;
2761 temp = (temp >> i) & 1; /* Mask out all but lower bit. */2762 temp = CS_2840 | temp;
2763 outb(temp, SEECTL_2840 + base);
2764 CLOCK_PULSE(base);
2765 temp = temp ^ CK_2840;
2766 outb(temp, SEECTL_2840 + base);
2767 CLOCK_PULSE(base);
2768 }2769
2770 /*2771 * Now read the 16 bit register. An initial 0 precedes the2772 * register contents which begins with bit 15 (MSB) and ends2773 * with bit 0 (LSB). The initial 0 will be shifted off the2774 * top of our word as we let the loop run from 0 to 16.2775 */2776 for (i = 0; i <= 16; i++)
2777 {2778 temp = CS_2840;
2779 outb(temp, SEECTL_2840 + base);
2780 CLOCK_PULSE(base);
2781 temp = temp ^ CK_2840;
2782 seeprom[k] = (seeprom[k] << 1) | (inb(STATUS_2840 + base) & DI_2840);
2783 outb(temp, SEECTL_2840 + base);
2784 CLOCK_PULSE(base);
2785 }2786 /*2787 * The serial EEPROM has a checksum in the last word. Keep a2788 * running checksum for all words read except for the last2789 * word. We'll verify the checksum after all words have been2790 * read.2791 */2792 if (k < (sizeof(*sc) / 2) - 1)
2793 {2794 checksum = checksum + seeprom[k];
2795 }2796
2797 /*2798 * Reset the chip select for the next command cycle.2799 */2800 outb(0, SEECTL_2840 + base);
2801 CLOCK_PULSE(base);
2802 outb(CK_2840, SEECTL_2840 + base);
2803 CLOCK_PULSE(base);
2804 outb(0, SEECTL_2840 + base);
2805 CLOCK_PULSE(base);
2806 }2807
2808 #if 0
2809 printk("Computed checksum 0x%x, checksum read 0x%x\n", checksum, sc->checksum);
2810 printk("Serial EEPROM:");
2811 for (k = 0; k < (sizeof(*sc) / 2); k++)
2812 {2813 if (((k % 8) == 0) && (k != 0))
2814 {2815 printk("\n ");
2816 }2817 printk(" 0x%x", seeprom[k]);
2818 }2819 printk("\n");
2820 #endif2821
2822 if (checksum != sc->checksum)
2823 {2824 printk("aic7xxx: SEEPROM checksum error, ignoring SEEPROM settings.\n");
2825 return (0);
2826 }2827
2828 return (1);
2829 #undefCLOCK_PULSE2830 }2831
2832 /*+F*************************************************************************2833 * Function:2834 * read_seeprom2835 *2836 * Description:2837 * Reads the serial EEPROM and returns 1 if successful and 0 if2838 * not successful.2839 *2840 * The instruction set of the 93C46 chip is as follows:2841 *2842 * Start OP2843 * Function Bit Code Address Data Description2844 * -------------------------------------------------------------------2845 * READ 1 10 A5 - A0 Reads data stored in memory,2846 * starting at specified address2847 * EWEN 1 00 11XXXX Write enable must precede2848 * all programming modes2849 * ERASE 1 11 A5 - A0 Erase register A5A4A3A2A1A02850 * WRITE 1 01 A5 - A0 D15 - D0 Writes register2851 * ERAL 1 00 10XXXX Erase all registers2852 * WRAL 1 00 01XXXX D15 - D0 Writes to all registers2853 * EWDS 1 00 00XXXX Disables all programming2854 * instructions2855 * *Note: A value of X for address is a don't care condition.2856 *2857 * The 93C46 has a four wire interface: clock, chip select, data in, and2858 * data out. In order to perform one of the above functions, you need2859 * to enable the chip select for a clock period (typically a minimum of2860 * 1 usec, with the clock high and low a minimum of 750 and 250 nsec2861 * respectively. While the chip select remains high, you can clock in2862 * the instructions (above) starting with the start bit, followed by the2863 * OP code, Address, and Data (if needed). For the READ instruction, the2864 * requested 16-bit register contents is read from the data out line but2865 * is preceded by an initial zero (leading 0, followed by 16-bits, MSB2866 * first). The clock cycling from low to high initiates the next data2867 * bit to be sent from the chip.2868 *2869 * The 7870 interface to the 93C46 serial EEPROM is through the SEECTL2870 * register. After successful arbitration for the memory port, the2871 * SEECS bit of the SEECTL register is connected to the chip select.2872 * The SEECK, SEEDO, and SEEDI are connected to the clock, data out,2873 * and data in lines respectively. The SEERDY bit of SEECTL is useful2874 * in that it gives us an 800 nsec timer. After a write to the SEECTL2875 * register, the SEERDY goes high 800 nsec later. The one exception2876 * to this is when we first request access to the memory port. The2877 * SEERDY goes high to signify that access has been granted and, for2878 * this case, has no implied timing.2879 *2880 *-F*************************************************************************/2881 staticint2882 read_seeprom(intbase, intoffset, structseeprom_config *sc)
/* */2883 {2884 inti = 0, k;
2885 unsignedlongtimeout;
2886 unsignedchartemp;
2887 unsignedshortchecksum = 0;
2888 unsignedshort *seeprom = (unsignedshort *) sc;
2889 structseeprom_cmd{2890 unsignedcharlen;
2891 unsignedcharbits[3];
2892 };
2893 structseeprom_cmdseeprom_read = {3, {1, 1, 0}};
2894
2895 #defineCLOCK_PULSE(p) \
2896 while ((inb(SEECTL + base) & SEERDY) == 0) \
2897 { \
2898 ; /* Do nothing */ \
2899 }2900
2901 /*2902 * Request access of the memory port. When access is2903 * granted, SEERDY will go high. We use a 1 second2904 * timeout which should be near 1 second more than2905 * is needed. Reason: after the 7870 chip reset, there2906 * should be no contention.2907 */2908 outb(SEEMS, SEECTL + base);
2909 timeout = jiffies + 100; /* 1 second timeout */2910 while ((jiffies < timeout) && ((inb(SEECTL + base) & SEERDY) == 0))
2911 {2912 ; /* Do nothing! Wait for access to be granted. */2913 }2914 if ((inb(SEECTL + base) & SEERDY) == 0)
2915 {2916 outb(0, SEECTL + base);
2917 return (0);
2918 }2919
2920 /*2921 * Read the first 32 registers of the seeprom. For the 7870,2922 * the 93C46 SEEPROM is a 1024-bit device with 64 16-bit registers2923 * but only the first 32 are used by Adaptec BIOS. The loop2924 * will range from 0 to 31.2925 */2926 for (k = 0; k < (sizeof(*sc) / 2); k++)
2927 {2928 /*2929 * Send chip select for one clock cycle.2930 */2931 outb(SEEMS | SEECK | SEECS, SEECTL + base);
2932 CLOCK_PULSE(base);
2933
2934 /*2935 * Now we're ready to send the read command followed by the2936 * address of the 16-bit register we want to read.2937 */2938 for (i = 0; i < seeprom_read.len; i++)
2939 {2940 temp = SEEMS | SEECS | (seeprom_read.bits[i] << 1);
2941 outb(temp, SEECTL + base);
2942 CLOCK_PULSE(base);
2943 temp = temp ^ SEECK;
2944 outb(temp, SEECTL + base);
2945 CLOCK_PULSE(base);
2946 }2947 /*2948 * Send the 6 bit address (MSB first, LSB last).2949 */2950 for (i = 5; i >= 0; i--)
2951 {2952 temp = k + offset;
2953 temp = (temp >> i) & 1; /* Mask out all but lower bit. */2954 temp = SEEMS | SEECS | (temp << 1);
2955 outb(temp, SEECTL + base);
2956 CLOCK_PULSE(base);
2957 temp = temp ^ SEECK;
2958 outb(temp, SEECTL + base);
2959 CLOCK_PULSE(base);
2960 }2961
2962 /*2963 * Now read the 16 bit register. An initial 0 precedes the2964 * register contents which begins with bit 15 (MSB) and ends2965 * with bit 0 (LSB). The initial 0 will be shifted off the2966 * top of our word as we let the loop run from 0 to 16.2967 */2968 for (i = 0; i <= 16; i++)
2969 {2970 temp = SEEMS | SEECS;
2971 outb(temp, SEECTL + base);
2972 CLOCK_PULSE(base);
2973 temp = temp ^ SEECK;
2974 seeprom[k] = (seeprom[k] << 1) | (inb(SEECTL + base) & SEEDI);
2975 outb(temp, SEECTL + base);
2976 CLOCK_PULSE(base);
2977 }2978
2979 /*2980 * The serial EEPROM has a checksum in the last word. Keep a2981 * running checksum for all words read except for the last2982 * word. We'll verify the checksum after all words have been2983 * read.2984 */2985 if (k < (sizeof(*sc) / 2) - 1)
2986 {2987 checksum = checksum + seeprom[k];
2988 }2989
2990 /*2991 * Reset the chip select for the next command cycle.2992 */2993 outb(SEEMS, SEECTL + base);
2994 CLOCK_PULSE(base);
2995 outb(SEEMS | SEECK, SEECTL + base);
2996 CLOCK_PULSE(base);
2997 outb(SEEMS, SEECTL + base);
2998 CLOCK_PULSE(base);
2999 }3000
3001 /*3002 * Release access to the memory port and the serial EEPROM.3003 */3004 outb(0, SEECTL + base);
3005
3006 #if 0
3007 printk("Computed checksum 0x%x, checksum read 0x%x\n", checksum, sc->checksum);
3008 printk("Serial EEPROM:");
3009 for (k = 0; k < (sizeof(*sc) / 2); k++)
3010 {3011 if (((k % 8) == 0) && (k != 0))
3012 {3013 printk("\n ");
3014 }3015 printk(" 0x%x", seeprom[k]);
3016 }3017 printk("\n");
3018 #endif3019
3020 if (checksum != sc->checksum)
3021 {3022 return (0);
3023 }3024
3025 return (1);
3026 #undefCLOCK_PULSE3027 }3028
3029 /*+F*************************************************************************3030 * Function:3031 * detect_maxscb3032 *3033 * Description:3034 * Detects the maximum number of SCBs for the controller and returns3035 * the count and a mask in config (config->maxscbs, config->qcntmask).3036 *-F*************************************************************************/3037 staticvoid3038 detect_maxscb(structaic7xxx_host_config *config)
/* */3039 {3040 unsignedcharsblkctl_reg;
3041 intbase, i;
3042
3043 base = config->base;
3044 switch (config->type)
3045 {3046 caseAIC_7770:
3047 caseAIC_7771:
3048 caseAIC_284x:
3049 /*3050 * Check for Rev C or E boards. Rev E boards can supposedly have3051 * more than 4 SCBs, while the Rev C boards are limited to 4 SCBs.3052 * Until we know how to access more than 4 SCBs for the Rev E chips,3053 * we limit them, along with the Rev C chips, to 4 SCBs.3054 *3055 * The Rev E boards have a read/write autoflush bit in the3056 * SBLKCTL register, while in the Rev C boards it is read only.3057 */3058 sblkctl_reg = inb(SBLKCTL + base) ^ AUTOFLUSHDIS;
3059 outb(sblkctl_reg, SBLKCTL + base);
3060 if (inb(SBLKCTL + base) == sblkctl_reg)
3061 {3062 /*3063 * We detected a Rev E board.3064 */3065 printk("aic7xxx: %s Rev E and subsequent.\n",
3066 board_names[config->type]);
3067 outb(sblkctl_reg ^ AUTOFLUSHDIS, SBLKCTL + base);
3068 }3069 else3070 {3071 printk("aic7xxx: %s Rev C and previous.\n", board_names[config->type]);
3072 }3073 break;
3074
3075 default:
3076 break;
3077 }3078
3079 /*3080 * Walk the SCBs to determine how many there are.3081 */3082 i = 1;
3083 outb (0, SCBPTR + base);
3084 outb (0, SCBARRAY + base);
3085
3086 while (i < AIC7XXX_MAXSCB)
3087 {3088 outb(i, SCBPTR + base);
3089 outb(i, SCBARRAY + base);
3090 if (inb(SCBARRAY + base) != i)
3091 break;
3092 outb(0, SCBPTR + base);
3093 if (inb(SCBARRAY + base) != 0)
3094 break;
3095
3096 outb(i, SCBPTR + base); /* Clear the control byte. */3097 outb(0, SCBARRAY + base);
3098
3099 config->qcntmask |= i; /* Update the count mask. */3100 i++;
3101 }3102 outb(i, SCBPTR + base); /* Ensure we clear the control bytes. */3103 outb(0, SCBARRAY + base);
3104 outb(0, SCBPTR + base);
3105 outb(0, SCBARRAY + base);
3106
3107 config->maxscb = i;
3108 config->qcntmask |= i;
3109
3110 printk("aic7xxx: Using %d SCB's after checking for SCB memory.\n",
3111 config->maxscb);
3112
3113 }3114
3115 /*+F*************************************************************************3116 * Function:3117 * aic7xxx_register3118 *3119 * Description:3120 * Register a Adaptec aic7xxx chip SCSI controller with the kernel.3121 *-F*************************************************************************/3122 staticint3123 aic7xxx_register(Scsi_Host_Template *template,
/* */3124 structaic7xxx_host_config *config)
3125 {3126 inti;
3127 unsignedcharsblkctl;
3128 intmax_targets;
3129 intfound = 1, base;
3130 unsignedchartarget_settings;
3131 unsignedcharscsi_conf, host_conf;
3132 inthave_seeprom = FALSE;
3133 structScsi_Host *host;
3134 structaic7xxx_host *p;
3135 structseeprom_configsc;
3136
3137 base = config->base;
3138
3139 /*3140 * Lock out other contenders for our i/o space.3141 */3142 request_region(MINREG + base, MAXREG - MINREG, "aic7xxx");
3143
3144 switch (config->type)
3145 {3146 caseAIC_7770:
3147 caseAIC_7771:
3148 /*3149 * For some 274x boards, we must clear the CHIPRST bit3150 * and pause the sequencer. For some reason, this makes3151 * the driver work. For 284x boards, we give it a3152 * CHIPRST just like the 294x boards.3153 *3154 * Use the BIOS settings to determine the interrupt3155 * trigger type (level or edge) and use this value3156 * for pausing and unpausing the sequencer.3157 */3158 config->unpause = (inb(HCNTRL + base) & IRQMS) | INTEN;
3159 config->pause = config->unpause | PAUSE;
3160 config->extended = aic7xxx_extended;
3161
3162 outb(config->pause | CHIPRST, HCNTRL + base);
3163 aic7xxx_delay(1);
3164 if (inb(HCNTRL + base) & CHIPRST)
3165 {3166 printk("aic7xxx: Chip reset not cleared; clearing manually.\n");
3167 }3168 outb(config->pause, HCNTRL + base);
3169
3170 /*3171 * Just to be on the safe side with the 274x, we will re-read the irq3172 * since there was some issue about resetting the board.3173 */3174 config->irq = inb(INTDEF + base) & 0x0F;
3175 if ((config->type == AIC_7771) &&
3176 (inb(HA_274_BIOSCTRL + base) & BIOSMODE) == BIOSDISABLED)
3177 {3178 config->bios = AIC_DISABLED;
3179 config->use_defaults = TRUE;
3180 }3181 else3182 {3183 host_conf = inb(HOSTCONF + base);
3184 config->bus_speed = host_conf & DFTHRSH;
3185 config->busrtime = (host_conf << 2) & BOFF;
3186 }3187
3188 /*3189 * Setup the FIFO threshold and the bus off time3190 */3191 outb(config->bus_speed & DFTHRSH, BUSSPD + base);
3192 outb(config->busrtime, BUSTIME + base);
3193
3194 /*3195 * A reminder until this can be detected automatically.3196 */3197 printk("aic7xxx: Extended translation %sabled.\n",
3198 config->extended ? "en" : "dis");
3199 break;
3200
3201 caseAIC_284x:
3202 outb(CHIPRST, HCNTRL + base);
3203 config->unpause = UNPAUSE_284X;
3204 config->pause = REQ_PAUSE; /* DWG would like to be like the rest */3205 aic7xxx_delay(1);
3206 outb(config->pause, HCNTRL + base);
3207
3208 config->parity = AIC_ENABLED;
3209 config->extended = aic7xxx_extended;
3210 config->irq = inb(INTDEF + base) & 0x0F;
3211 host_conf = inb(HOSTCONF + base);
3212
3213 printk("aic7xxx: Reading SEEPROM...");
3214 have_seeprom = read_2840_seeprom(base, &sc);
3215 if (!have_seeprom)
3216 {3217 printk("aic7xxx: Unable to read SEEPROM.\n");
3218 }3219 else3220 {3221 printk("done.\n");
3222 config->extended = ((sc.bios_control & CF284XEXTEND) >> 5);
3223 if (!(sc.bios_control & CFBIOSEN))
3224 {3225 /*3226 * The BIOS is disabled; the values left over in scratch3227 * RAM are still valid. Do not use defaults as in the3228 * AIC-7770 case.3229 */3230 config->bios = AIC_DISABLED;
3231 }3232 else3233 {3234 config->parity = (sc.adapter_control & CFSPARITY) ?
3235 AIC_ENABLED : AIC_DISABLED;
3236 config->low_term = (sc.adapter_control & CF284XSTERM) ?
3237 AIC_ENABLED : AIC_DISABLED;
3238 /*3239 * XXX - Adaptec *does* make 284x wide controllers, but the3240 * documents do not say where the high byte termination3241 * enable bit is located.3242 */3243 }3244 }3245
3246 host_conf = inb(HOSTCONF + base);
3247 config->bus_speed = host_conf & DFTHRSH;
3248 config->busrtime = (host_conf << 2) & BOFF;
3249
3250 /*3251 * Setup the FIFO threshold and the bus off time3252 */3253 outb(config->bus_speed & DFTHRSH, BUSSPD + base);
3254 outb(config->busrtime, BUSTIME + base);
3255
3256 printk("aic7xxx: Extended translation %sabled.\n",
3257 config->extended ? "en" : "dis");
3258 break;
3259
3260 caseAIC_7850:
3261 caseAIC_7855:
3262 caseAIC_7870:
3263 caseAIC_7871:
3264 caseAIC_7872:
3265 caseAIC_7873:
3266 caseAIC_7874:
3267 caseAIC_7880:
3268 caseAIC_7881:
3269 caseAIC_7882:
3270 caseAIC_7883:
3271 caseAIC_7884:
3272 outb(CHIPRST, HCNTRL + base);
3273 config->unpause = UNPAUSE_294X;
3274 config->pause = config->unpause | PAUSE;
3275 aic7xxx_delay(1);
3276 outb(config->pause, HCNTRL + base);
3277
3278 config->extended = aic7xxx_extended;
3279 config->scsi_id = 7;
3280 config->parity = AIC_ENABLED;
3281
3282 printk("aic7xxx: Reading SEEPROM...");
3283 have_seeprom = read_seeprom(base, config->chan_num * (sizeof(sc) / 2), &sc);
3284 if (!have_seeprom)
3285 {3286 printk("\naic7xxx: Unable to read SEEPROM; "
3287 "using leftover BIOS values.\n");
3288 }3289 else3290 {3291 printk("done.\n");
3292 if (!(sc.bios_control & CFBIOSEN))
3293 {3294 /*3295 * The BIOS is disabled; the values left over in scratch3296 * RAM are still valid. Do not use defaults as in the3297 * AIC-7770 case.3298 */3299 config->bios = AIC_DISABLED;
3300 }3301 else3302 {3303 config->extended = ((sc.bios_control & CFEXTEND) >> 7);
3304 config->scsi_id = (sc.brtime_id & CFSCSIID);
3305 config->parity = (sc.adapter_control & CFSPARITY) ?
3306 AIC_ENABLED : AIC_DISABLED;
3307 config->low_term = (sc.adapter_control & CFSTERM) ?
3308 AIC_ENABLED : AIC_DISABLED;
3309 config->high_term = (sc.adapter_control & CFWSTERM) ?
3310 AIC_ENABLED : AIC_DISABLED;
3311 config->busrtime = ((sc.brtime_id & CFBRTIME) >> 8);
3312 if (((config->type == AIC_7880) || (config->type == AIC_7882) ||
3313 (config->type == AIC_7883) || (config->type == AIC_7884)) &&
3314 (sc.adapter_control & CFULTRAEN))
3315 {3316 printk ("aic7xxx: Enabling support for Ultra SCSI speed.\n");
3317 config->ultra_enabled = TRUE;
3318 }3319 }3320 }3321
3322 /*3323 * XXX - force data fifo threshold to 100%. Why does this3324 * need to be done?3325 *3326 * We don't know where this is set in the SEEPROM or by the BIOS,3327 * so we default it to 100%.3328 */3329 config->bus_speed = DFTHRSH_100;
3330 scsi_conf = config->scsi_id | DFTHRSH_100;
3331 #if 0
3332 if (config->parity == AIC_ENABLED)
3333 {3334 scsi_conf |= ENSPCHK;
3335 }3336 #endif3337 outb(scsi_conf, SCSICONF + base);
3338 outb(DFTHRSH_100, DSPCISTATUS + base);
3339
3340 /*3341 * In case we are a wide card...3342 */3343 /*3344 * Try the following:3345 *3346 * 1) outb(config->scsi_id, SCSICONF + base + 1);3347 * 2) outb(scsiconf, SCSICONF + base + 1);3348 *3349 */3350 outb(config->scsi_id, SCSICONF + base + 1);
3351
3352 printk("aic7xxx: Extended translation %sabled.\n",
3353 config->extended ? "en" : "dis");
3354 break;
3355
3356 default:
3357 panic("aic7xxx: (aic7xxx_register) Internal error.\n");
3358 }3359
3360 detect_maxscb(config);
3361
3362 if (config->chip_type == AIC_777x)
3363 {3364 if (config->pause & IRQMS)
3365 {3366 printk("aic7xxx: Using level sensitive interrupts.\n");
3367 }3368 else3369 {3370 printk("aic7xxx: Using edge triggered interrupts.\n");
3371 }3372 }3373
3374 /*3375 * Read the bus type from the SBLKCTL register. Set the FLAGS3376 * register in the sequencer for twin and wide bus cards.3377 */3378 sblkctl = inb(SBLKCTL + base);
3379 switch (sblkctl & SELBUS_MASK)
3380 {3381 caseSELNARROW: /* narrow/normal bus */3382 config->scsi_id = inb(SCSICONF + base) & 0x07;
3383 config->bus_type = AIC_SINGLE;
3384 outb(SINGLE_BUS, FLAGS + base);
3385 break;
3386
3387 caseSELWIDE: /* Wide bus */3388 config->scsi_id = inb(SCSICONF + base + 1) & 0x0F;
3389 config->bus_type = AIC_WIDE;
3390 printk("aic7xxx: Enabling wide channel of %s-Wide.\n",
3391 board_names[config->type]);
3392 outb(WIDE_BUS, FLAGS + base);
3393 break;
3394
3395 caseSELBUSB: /* Twin bus */3396 config->scsi_id = inb(SCSICONF + base) & 0x07;
3397 #ifdefAIC7XXX_TWIN_SUPPORT3398 config->scsi_id_b = inb(SCSICONF + base + 1) & 0x07;
3399 config->bus_type = AIC_TWIN;
3400 printk("aic7xxx: Enabled channel B of %s-Twin.\n",
3401 board_names[config->type]);
3402 outb(TWIN_BUS, FLAGS + base);
3403 #else3404 config->bus_type = AIC_SINGLE;
3405 printk("aic7xxx: Channel B of %s-Twin will be ignored.\n",
3406 board_names[config->type]);
3407 outb(0, FLAGS + base);
3408 #endif3409 break;
3410
3411 default:
3412 printk("aic7xxx: Unsupported type 0x%x, please "
3413 "mail deang@teleport.com\n", inb(SBLKCTL + base));
3414 outb(0, FLAGS + base);
3415 return (0);
3416 }3417
3418 /*3419 * For the 294x cards, clearing DIAGLEDEN and DIAGLEDON, will3420 * take the card out of diagnostic mode and make the host adapter3421 * LED follow bus activity (will not always be on).3422 */3423 outb(sblkctl & ~(DIAGLEDEN | DIAGLEDON), SBLKCTL + base);
3424
3425 /*3426 * The IRQ level in i/o port 4 maps directly onto the real3427 * IRQ number. If it's ok, register it with the kernel.3428 *3429 * NB. the Adaptec documentation says the IRQ number is only3430 * in the lower four bits; the ECU information shows the3431 * high bit being used as well. Which is correct?3432 *3433 * The PCI cards get their interrupt from PCI BIOS.3434 */3435 if ((config->chip_type == AIC_777x) && ((config->irq < 9) || (config->irq > 15)))
3436 {3437 printk("aic7xxx: Host adapter uses unsupported IRQ level, ignoring.\n");
3438 return (0);
3439 }3440
3441 /*3442 * Check the IRQ to see if it is shared by another aic7xxx3443 * controller. If it is and sharing of IRQs is not defined,3444 * then return 0 hosts found. If sharing of IRQs is allowed3445 * or the IRQ is not shared by another host adapter, then3446 * proceed.3447 */3448 #ifndefAIC7XXX_SHARE_IRQS3449 if (aic7xxx_boards[config->irq] != NULL)
3450 {3451 printk("aic7xxx: Sharing of IRQ's is not configured.\n");
3452 return (0);
3453 }3454 #endif3455
3456 /*3457 * Print out debugging information before re-enabling3458 * the card - a lot of registers on it can't be read3459 * when the sequencer is active.3460 */3461 debug_config(config);
3462
3463 /*3464 * Before registry, make sure that the offsets of the3465 * struct scatterlist are what the sequencer will expect,3466 * otherwise disable scatter-gather altogether until someone3467 * can fix it. This is important since the sequencer will3468 * DMA elements of the SG array in while executing commands.3469 */3470 if (template->sg_tablesize != SG_NONE)
3471 {3472 structscatterlistsg;
3473
3474 if (SG_STRUCT_CHECK(sg))
3475 {3476 printk("aic7xxx: Warning - Kernel scatter-gather structures changed, "
3477 "disabling it.\n");
3478 template->sg_tablesize = SG_NONE;
3479 }3480 }3481
3482 /*3483 * Register each "host" and fill in the returned Scsi_Host3484 * structure as best we can. Some of the parameters aren't3485 * really relevant for bus types beyond ISA, and none of the3486 * high-level SCSI code looks at it anyway. Why are the fields3487 * there? Also save the pointer so that we can find the3488 * information when an IRQ is triggered.3489 */3490 host = scsi_register(template, sizeof(structaic7xxx_host));
3491 host->can_queue = config->maxscb;
3492 host->cmd_per_lun = AIC7XXX_CMDS_PER_LUN;
3493 host->this_id = config->scsi_id;
3494 host->irq = config->irq;
3495 if (config->bus_type == AIC_WIDE)
3496 {3497 host->max_id = 16;
3498 }3499 if (config->bus_type == AIC_TWIN)
3500 {3501 host->max_channel = 1;
3502 }3503
3504 p = (structaic7xxx_host *) host->hostdata;
3505
3506 p->host = host;
3507 p->isr_count = 0;
3508 p->a_scanned = FALSE;
3509 p->b_scanned = FALSE;
3510 p->base = base;
3511 p->maxscb = config->maxscb;
3512 p->qcntmask = config->qcntmask;
3513 p->numscb = 0;
3514 p->extended = config->extended;
3515 p->type = config->type;
3516 p->chip_type = config->chip_type;
3517 p->ultra_enabled = config->ultra_enabled;
3518 p->chan_num = config->chan_num;
3519 p->bus_type = config->bus_type;
3520 p->have_seeprom = have_seeprom;
3521 p->seeprom = sc;
3522 p->free_scb = NULL;
3523 p->aborted_scb = NULL;
3524 p->next = NULL;
3525
3526 p->unpause = config->unpause;
3527 p->pause = config->pause;
3528
3529 if (aic7xxx_boards[config->irq] == NULL)
3530 {3531 /*3532 * Warning! This must be done before requesting the irq. It is3533 * possible for some boards to raise an interrupt as soon as3534 * they are enabled. So when we request the irq from the Linux3535 * kernel, an interrupt is triggered immediately. Therefore, we3536 * must ensure the board data is correctly set before the request.3537 */3538 aic7xxx_boards[config->irq] = host;
3539
3540 /*3541 * Register IRQ with the kernel.3542 */3543 if (request_irq(config->irq, aic7xxx_isr, SA_INTERRUPT, "aic7xxx", NULL))
3544 {3545 printk("aic7xxx: Couldn't register IRQ %d, ignoring.\n", config->irq);
3546 aic7xxx_boards[config->irq] = NULL;
3547 return (0);
3548 }3549 }3550 else3551 {3552 /*3553 * We have found a host adapter sharing an IRQ of a previously3554 * registered host adapter. Add this host adapter's Scsi_Host3555 * to the beginning of the linked list of hosts at the same IRQ.3556 */3557 p->next = aic7xxx_boards[config->irq];
3558 aic7xxx_boards[config->irq] = host;
3559 }3560
3561 /*3562 * Load the sequencer program, then re-enable the board -3563 * resetting the AIC-7770 disables it, leaving the lights3564 * on with nobody home. On the PCI bus you *may* be home,3565 * but then your mailing address is dynamically assigned3566 * so no one can find you anyway :-)3567 */3568 printk("aic7xxx: Downloading sequencer code...");
3569 aic7xxx_loadseq(base);
3570
3571 /*3572 * Set Fast Mode and Enable the board3573 */3574 outb(FASTMODE, SEQCTL + base);
3575
3576 if (p->chip_type == AIC_777x)
3577 {3578 outb(ENABLE, BCTL + base);
3579 }3580
3581 printk("done.\n");
3582
3583 /*3584 * Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1, for both channels3585 */3586 if (p->bus_type == AIC_TWIN)
3587 {3588 /*3589 * Select Channel B.3590 */3591 outb((sblkctl & ~SELBUS_MASK) | SELBUSB, SBLKCTL + base);
3592
3593 outb(config->scsi_id_b, SCSIID + base);
3594 scsi_conf = inb(SCSICONF + base + 1) & (ENSPCHK | STIMESEL);
3595 outb(scsi_conf | ENSTIMER | ACTNEGEN | STPWEN, SXFRCTL1 + base);
3596 outb(ENSELTIMO, SIMODE1 + base);
3597 if (p->ultra_enabled)
3598 {3599 outb(DFON | SPIOEN | ULTRAEN, SXFRCTL0 + base);
3600 }3601 else3602 {3603 outb(DFON | SPIOEN, SXFRCTL0 + base);
3604 }3605
3606 /*3607 * Select Channel A3608 */3609 outb((sblkctl & ~SELBUS_MASK) | SELNARROW, SBLKCTL + base);
3610 }3611 outb(config->scsi_id, SCSIID + base);
3612 scsi_conf = inb(SCSICONF + base) & (ENSPCHK | STIMESEL);
3613 outb(scsi_conf | ENSTIMER | ACTNEGEN | STPWEN, SXFRCTL1 + base);
3614 outb(ENSELTIMO, SIMODE1 + base);
3615 if (p->ultra_enabled)
3616 {3617 outb(DFON | SPIOEN | ULTRAEN, SXFRCTL0 + base);
3618 }3619 else3620 {3621 outb(DFON | SPIOEN, SXFRCTL0 + base);
3622 }3623
3624 /*3625 * Look at the information that board initialization or the board3626 * BIOS has left us. In the lower four bits of each target's3627 * scratch space any value other than 0 indicates that we should3628 * initiate synchronous transfers. If it's zero, the user or the3629 * BIOS has decided to disable synchronous negotiation to that3630 * target so we don't activate the needsdtr flag.3631 */3632 p->needsdtr_copy = 0x0;
3633 p->sdtr_pending = 0x0;
3634 p->needwdtr_copy = 0x0;
3635 p->wdtr_pending = 0x0;
3636 if (p->bus_type == AIC_SINGLE)
3637 {3638 max_targets = 8;
3639 }3640 else3641 {3642 max_targets = 16;
3643 }3644
3645 /*3646 * Grab the disconnection disable table and invert it for our needs3647 */3648 if (have_seeprom)
3649 {3650 p->discenable = 0x0;
3651 }3652 else3653 {3654 if (config->bios == AIC_DISABLED)
3655 {3656 printk("aic7xxx : Host adapter BIOS disabled. Using default SCSI "
3657 "device parameters.\n");
3658 p->discenable = 0xFFFF;
3659 }3660 else3661 {3662 p->discenable = ~((inb(DISC_DSB + base + 1) << 8) |
3663 inb(DISC_DSB + base));
3664 }3665 }3666
3667 for (i = 0; i < max_targets; i++)
3668 {3669 if (have_seeprom)
3670 {3671 target_settings = ((sc.device_flags[i] & CFXFER) << 4);
3672 if (sc.device_flags[i] & CFSYNCH)
3673 {3674 p->needsdtr_copy |= (0x01 << i);
3675 }3676 if (sc.device_flags[i] & CFWIDEB)
3677 {3678 p->needwdtr_copy |= (0x01 << i);
3679 }3680 if (sc.device_flags[i] & CFDISC)
3681 {3682 p->discenable |= (0x01 << i);
3683 }3684 }3685 else3686 {3687 if (config->use_defaults)
3688 {3689 target_settings = 0; /* 10 MHz */3690 p->needsdtr_copy |= (0x01 << i);
3691 p->needwdtr_copy |= (0x01 << i);
3692 }3693 else3694 {3695 target_settings = inb(TARG_SCRATCH + base + i);
3696 if (target_settings & 0x0F)
3697 {3698 p->needsdtr_copy |= (0x01 << i);
3699 /*3700 * Default to asynchronous transfers (0 offset)3701 */3702 target_settings &= 0xF0;
3703 }3704 if (target_settings & 0x80)
3705 {3706 p->needwdtr_copy |= (0x01 << i);
3707 target_settings &= 0x7F;
3708 }3709 }3710 }3711 outb(target_settings, (TARG_SCRATCH + base + i));
3712 }3713
3714 /*3715 * If we are not wide, forget WDTR. This makes the driver3716 * work on some cards that don't leave these fields cleared3717 * when BIOS is not installed.3718 */3719 if (p->bus_type != AIC_WIDE)
3720 {3721 p->needwdtr_copy = 0;
3722 }3723 p->needsdtr = p->needsdtr_copy;
3724 p->needwdtr = p->needwdtr_copy;
3725 #if 0
3726 printk("NeedSdtr = 0x%x, 0x%x\n", p->needsdtr_copy, p->needsdtr);
3727 printk("NeedWdtr = 0x%x, 0x%x\n", p->needwdtr_copy, p->needwdtr);
3728 #endif3729
3730 /*3731 * For reconnecting targets, the sequencer code needs to3732 * know how many SCBs it has to search through.3733 */3734 outb(config->maxscb, SCBCOUNT + base);
3735
3736 /*3737 * 2s compliment of SCBCOUNT3738 */3739 i = p->maxscb;
3740 outb(-i & 0xff, COMP_SCBCOUNT + base);
3741
3742 /*3743 * Set the QCNT (queue count) mask to deal with broken aic7850s that3744 * sporadically get garbage in the upper bits of their QCNT registers.3745 */3746 outb(config->qcntmask, QCNTMASK + base);
3747
3748 /*3749 * Clear the active flags - no targets are busy.3750 */3751 outb(0, ACTIVE_A + base);
3752 outb(0, ACTIVE_B + base);
3753
3754 /*3755 * We don't have any waiting selections3756 */3757 outb(SCB_LIST_NULL, WAITING_SCBH + base);
3758
3759 /*3760 * Message out buffer starts empty3761 */3762 outb(0, MSG_LEN + base);
3763
3764 /*3765 * Reset the SCSI bus. Is this necessary?3766 * There may be problems for a warm boot without resetting3767 * the SCSI bus. Either BIOS settings in scratch RAM3768 * will not get reinitialized, or devices may stay at3769 * previous negotiated settings (SDTR and WDTR) while3770 * the driver will think that no negotiations have been3771 * performed.3772 *3773 * Some devices need a long time to "settle" after a SCSI3774 * bus reset.3775 */3776
3777 if (!aic7xxx_no_reset)
3778 {3779 printk("aic7xxx: Resetting the SCSI bus...");
3780 if (p->bus_type == AIC_TWIN)
3781 {3782 /*3783 * Select Channel B.3784 */3785 outb((sblkctl & ~SELBUS_MASK) | SELBUSB, SBLKCTL + base);
3786
3787 outb(SCSIRSTO, SCSISEQ + base);
3788 udelay(1000);
3789 outb(0, SCSISEQ + base);
3790
3791 /* Ensure we don't get a RSTI interrupt from this. */3792 outb(CLRSCSIRSTI, CLRSINT1 + base);
3793 outb(CLRSCSIINT, CLRINT + base);
3794
3795 /*3796 * Select Channel A.3797 */3798 outb((sblkctl & ~SELBUS_MASK) | SELNARROW, SBLKCTL + base);
3799 }3800
3801 outb(SCSIRSTO, SCSISEQ + base);
3802 udelay(1000);
3803 outb(0, SCSISEQ + base);
3804
3805 /* Ensure we don't get a RSTI interrupt from this. */3806 outb(CLRSCSIRSTI, CLRSINT1 + base);
3807 outb(CLRSCSIINT, CLRINT + base);
3808
3809 aic7xxx_delay(AIC7XXX_RESET_DELAY);
3810
3811 printk("done.\n");
3812 }3813
3814 /*3815 * Unpause the sequencer before returning and enable3816 * interrupts - we shouldn't get any until the first3817 * command is sent to us by the high-level SCSI code.3818 */3819 UNPAUSE_SEQUENCER(p);
3820 return (found);
3821 }3822
3823 /*+F*************************************************************************3824 * Function:3825 * aic7xxx_detect3826 *3827 * Description:3828 * Try to detect and register an Adaptec 7770 or 7870 SCSI controller.3829 *-F*************************************************************************/3830 int3831 aic7xxx_detect(Scsi_Host_Template *template)
/* */3832 {3833 intfound = 0, slot, base;
3834 unsignedcharirq = 0;
3835 inti;
3836 structaic7xxx_host_configconfig;
3837
3838 template->proc_dir = &proc_scsi_aic7xxx;
3839 config.chan_num = 0;
3840
3841 /*3842 * Since we may allow sharing of IRQs, it is imperative3843 * that we "null-out" the aic7xxx_boards array. It is3844 * not guaranteed to be initialized to 0 (NULL). We use3845 * a NULL entry to indicate that no prior hosts have3846 * been found/registered for that IRQ.3847 */3848 for (i = 0; i <= MAXIRQ; i++)
3849 {3850 aic7xxx_boards[i] = NULL;
3851 }3852
3853 /*3854 * Initialize the spurious count to 0.3855 */3856 aic7xxx_spurious_count = 0;
3857
3858 /*3859 * EISA/VL-bus card signature probe.3860 */3861 for (slot = MINSLOT; slot <= MAXSLOT; slot++)
3862 {3863 base = SLOTBASE(slot) + MINREG;
3864
3865 if (check_region(MINREG + base, MAXREG - MINREG))
3866 {3867 /*3868 * Some other driver has staked a3869 * claim to this i/o region already.3870 */3871 continue;
3872 }3873
3874 config.type = aic7xxx_probe(slot, HID0 + base, &(config.bios));
3875 if (config.type != AIC_NONE)
3876 {3877 /*3878 * We found a card, allow 1 spurious interrupt.3879 */3880 aic7xxx_spurious_count = 1;
3881
3882 /*3883 * We "find" a AIC-7770 if we locate the card3884 * signature and we can set it up and register3885 * it with the kernel without incident.3886 */3887 config.chip_type = AIC_777x;
3888 config.base = base;
3889 config.irq = irq;
3890 config.parity = AIC_ENABLED;
3891 config.low_term = AIC_UNKNOWN;
3892 config.high_term = AIC_UNKNOWN;
3893 config.ultra_enabled = FALSE;
3894 config.extended = aic7xxx_extended;
3895 config.bus_speed = DFTHRSH_100;
3896 config.busrtime = BOFF_60BCLKS;
3897 found += aic7xxx_register(template, &config);
3898
3899 /*3900 * Disallow spurious interrupts.3901 */3902 aic7xxx_spurious_count = 0;
3903 }3904 }3905
3906 #ifdefCONFIG_PCI3907 /*3908 * PCI-bus probe.3909 */3910 if (pcibios_present())
3911 {3912 struct3913 {3914 unsignedshortvendor_id;
3915 unsignedshortdevice_id;
3916 aha_typecard_type;
3917 aha_chip_typechip_type;
3918 }constaic7xxx_pci_devices[] = {3919 {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7850, AIC_7850, AIC_785x},
3920 {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7855, AIC_7855, AIC_785x},
3921 {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7870, AIC_7870, AIC_787x},
3922 {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7871, AIC_7871, AIC_787x},
3923 {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7872, AIC_7872, AIC_787x},
3924 {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7873, AIC_7873, AIC_787x},
3925 {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7874, AIC_7874, AIC_787x},
3926 {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7880, AIC_7880, AIC_788x},
3927 {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7881, AIC_7881, AIC_788x},
3928 {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7882, AIC_7882, AIC_788x},
3929 {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7883, AIC_7883, AIC_788x},
3930 {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7884, AIC_7884, AIC_788x}3931 };
3932
3933 interror;
3934 intdone = 0;
3935 unsignedintio_port;
3936 unsignedshortindex = 0;
3937 unsignedcharpci_bus, pci_device_fn;
3938 unsignedintcsize_lattime;
3939 unsignedintclass_revid;
3940 unsignedintdevconfig;
3941 charrev_id[] = {'B', 'C', 'D'};
3942
3943 for (i = 0; i < NUMBER(aic7xxx_pci_devices); i++)
3944 {3945 done = FALSE;
3946 while (!done)
3947 {3948 if (pcibios_find_device(aic7xxx_pci_devices[i].vendor_id,
3949 aic7xxx_pci_devices[i].device_id,
3950 index, &pci_bus, &pci_device_fn))
3951 {3952 done = TRUE;
3953 }3954 else/* Found an Adaptec PCI device. */3955 {3956 config.type = aic7xxx_pci_devices[i].card_type;
3957 config.chip_type = aic7xxx_pci_devices[i].chip_type;
3958 config.chan_num = 0;
3959 config.bios = AIC_ENABLED; /* Assume bios is enabled. */3960 config.use_defaults = FALSE;
3961 config.busrtime = 40;
3962 switch (config.type)
3963 {3964 caseAIC_7850:
3965 caseAIC_7855:
3966 config.bios = AIC_DISABLED;
3967 config.use_defaults = TRUE;
3968 config.bus_speed = DFTHRSH_100;
3969 break;
3970
3971 caseAIC_7872: /* 3940 */3972 caseAIC_7882: /* 3940-Ultra */3973 config.chan_num = number_of_39xxs & 0x1; /* Has 2 controllers */3974 number_of_39xxs++;
3975 if (number_of_39xxs == 2)
3976 {3977 number_of_39xxs = 0; /* To be consistent with 3985. */3978 }3979 break;
3980
3981 caseAIC_7873: /* 3985 */3982 caseAIC_7883: /* 3985-Ultra */3983 config.chan_num = number_of_39xxs & 0x3; /* Has 3 controllers */3984 number_of_39xxs++;
3985 if (number_of_39xxs == 3)
3986 {3987 number_of_39xxs = 0;
3988 }3989 break;
3990
3991 default:
3992 break;
3993 }3994
3995 /*3996 * Read sundry information from PCI BIOS.3997 */3998 error = pcibios_read_config_dword(pci_bus, pci_device_fn,
3999 PCI_BASE_ADDRESS_0, &io_port);
4000 error += pcibios_read_config_byte(pci_bus, pci_device_fn,
4001 PCI_INTERRUPT_LINE, &irq);
4002
4003 /*4004 * Ensure that we are using good values for the PCI burst size4005 * and latency timer.4006 */4007 error += pcibios_read_config_dword(pci_bus, pci_device_fn,
4008 CSIZE_LATTIME, &csize_lattime);
4009 if ((csize_lattime & CACHESIZE) == 0)
4010 {4011 /*4012 * Default to 8DWDs - what's the PCI define for this?4013 */4014 csize_lattime |= 8;
4015 }4016 if((csize_lattime & LATTIME) == 0)
4017 {4018 /*4019 * Default to 64 PCLKS (is this a good value?)4020 * This may also be available in the SEEPROM??4021 */4022 csize_lattime |= (64 << 8);
4023 }4024 pcibios_write_config_dword(pci_bus, pci_device_fn,
4025 CSIZE_LATTIME, csize_lattime);
4026 printk("aic7xxx: BurstLen = %d DWDs, Latency Timer = %d PCLKS\n",
4027 (int) (csize_lattime & CACHESIZE),
4028 (csize_lattime >> 8) & 0x000000ff);
4029
4030 error += pcibios_read_config_dword(pci_bus, pci_device_fn,
4031 CLASS_PROGIF_REVID, &class_revid);
4032 if ((class_revid & DEVREVID) < 3)
4033 {4034 printk("aic7xxx: %s Rev %c.\n", board_names[config.type],
4035 rev_id[class_revid & DEVREVID]);
4036 }4037
4038 error += pcibios_read_config_dword(pci_bus, pci_device_fn,
4039 DEVCONFIG, &devconfig);
4040 if (error)
4041 {4042 panic("aic7xxx: (aic7xxx_detect) Error %d reading PCI registers.\n",
4043 error);
4044 }4045
4046 printk("aic7xxx: devconfig = 0x%x.\n", devconfig);
4047
4048 /*4049 * The first bit of PCI_BASE_ADDRESS_0 is always set, so4050 * we mask it off.4051 */4052 base = io_port & 0xfffffffe;
4053
4054 /*4055 * I don't think we need to bother with allowing4056 * spurious interrupts for the 787x/785x, but what4057 * the hey.4058 */4059 aic7xxx_spurious_count = 1;
4060
4061 config.base = base;
4062 config.irq = irq;
4063 config.parity = AIC_ENABLED;
4064 config.low_term = AIC_UNKNOWN;
4065 config.high_term = AIC_UNKNOWN;
4066 config.extended = aic7xxx_extended;
4067 config.ultra_enabled = FALSE;
4068 if (devconfig & RAMPSM)
4069 {4070 /*4071 * External SRAM present. The probe will walk the SCBs to see4072 * how much SRAM we have and set the number of SCBs accordingly.4073 * We have to turn off SCBRAMSEL to access the external SCB4074 * SRAM.4075 *4076 * It seems that early versions of the aic7870 didn't use these4077 * bits, hence the hack for the 3940 above. I would guess that4078 * recent 3940s using later aic7870 or aic7880 chips do actually4079 * set RAMPSM.4080 *4081 * The documentation isn't clear, but it sounds like the value4082 * written to devconfig must not have RAMPSM set. The second4083 * sixteen bits of the register are R/O anyway, so it shouldn't4084 * affect RAMPSM either way.4085 */4086 printk ("aic7xxx: External RAM detected. Enabling RAM access.\n");
4087 devconfig &= ~(RAMPSM | SCBRAMSEL);
4088 pcibios_write_config_dword(pci_bus, pci_device_fn,
4089 DEVCONFIG, devconfig);
4090 }4091 found += aic7xxx_register(template, &config);
4092
4093 /*4094 * Disable spurious interrupts.4095 */4096 aic7xxx_spurious_count = 0;
4097
4098 index++;
4099 }/* Found an Adaptec PCI device. */4100 }4101 }4102 }4103 #endifCONFIG_PCI4104
4105 template->name = aic7xxx_info(NULL);
4106 return (found);
4107 }4108
4109
4110 /*+F*************************************************************************4111 * Function:4112 * aic7xxx_buildscb4113 *4114 * Description:4115 * Build a SCB.4116 *-F*************************************************************************/4117 staticvoid4118 aic7xxx_buildscb(structaic7xxx_host *p,
/* */4119 Scsi_Cmnd *cmd,
4120 structaic7xxx_scb *scb)
4121 {4122 void *addr;
4123 unsignedshortmask;
4124 structscatterlist *sg;
4125
4126 /*4127 * Setup the control byte if we need negotiation and have not4128 * already requested it.4129 */4130 #ifdefAIC7XXX_TAGGED_QUEUEING4131 if (cmd->device->tagged_supported)
4132 {4133 if (cmd->device->tagged_queue == 0)
4134 {4135 printk("aic7xxx: Enabling tagged queuing for target %d, "
4136 "channel %d.\n", cmd->target, cmd->channel);
4137 cmd->device->tagged_queue = 1;
4138 cmd->device->current_tag = 1; /* enable tagging */4139 }4140 cmd->tag = cmd->device->current_tag;
4141 cmd->device->current_tag++;
4142 scb->control |= TAG_ENB;
4143 }4144 #endif4145 mask = (0x01 << (cmd->target | (cmd->channel << 3)));
4146 if (p->discenable & mask)
4147 {4148 scb->control |= DISCENB;
4149 }4150 if ((p->needwdtr & mask) && !(p->wdtr_pending & mask))
4151 {4152 p->wdtr_pending |= mask;
4153 scb->control |= NEEDWDTR;
4154 #if 0
4155 printk("aic7xxx: Sending WDTR request to target %d.\n", cmd->target);
4156 #endif4157 }4158 else4159 {4160 if ((p->needsdtr & mask) && !(p->sdtr_pending & mask))
4161 {4162 p->sdtr_pending |= mask;
4163 scb->control |= NEEDSDTR;
4164 #if 0
4165 printk("aic7xxx: Sending SDTR request to target %d.\n", cmd->target);
4166 #endif4167 }4168 }4169
4170 #if 0
4171 printk("aic7xxx: (build_scb) Target %d, cmd(0x%x) size(%u) wdtr(0x%x) "
4172 "mask(0x%x).\n",
4173 cmd->target, cmd->cmnd[0], cmd->cmd_len, p->needwdtr, mask);
4174 #endif4175 scb->target_channel_lun = ((cmd->target << 4) & 0xF0) |
4176 ((cmd->channel & 0x01) << 3) | (cmd->lun & 0x07);
4177
4178 /*4179 * The interpretation of request_buffer and request_bufflen4180 * changes depending on whether or not use_sg is zero; a4181 * non-zero use_sg indicates the number of elements in the4182 * scatter-gather array.4183 */4184
4185 /*4186 * XXX - this relies on the host data being stored in a4187 * little-endian format.4188 */4189 addr = cmd->cmnd;
4190 scb->SCSI_cmd_length = cmd->cmd_len;
4191 memcpy(scb->SCSI_cmd_pointer, &addr, sizeof(scb->SCSI_cmd_pointer));
4192
4193 if (cmd->use_sg)
4194 {4195 scb->SG_segment_count = cmd->use_sg;
4196 memcpy(scb->SG_list_pointer, &cmd->request_buffer,
4197 sizeof(scb->SG_list_pointer));
4198 memcpy(&sg, &cmd->request_buffer, sizeof(sg));
4199 memcpy(scb->data_pointer, &(sg[0].address), sizeof(scb->data_pointer));
4200 scb->data_count = sg[0].length;
4201 #if 0
4202 debug("aic7xxx: (build_scb) SG segs(%d), length(%u), sg[0].length(%d).\n",
4203 cmd->use_sg, aic7xxx_length(cmd, 0), scb->data_count);
4204 #endif4205 }4206 else4207 {4208 #if 0
4209 debug("aic7xxx: (build_scb) Creating scatterlist, addr(0x%lx) length(%d).\n",
4210 (unsignedlong) cmd->request_buffer, cmd->request_bufflen);
4211 #endif4212 if (cmd->request_bufflen == 0)
4213 {4214 /*4215 * In case the higher level SCSI code ever tries to send a zero4216 * length command, ensure the SCB indicates no data. The driver4217 * will interpret a zero length command as a Bus Device Reset.4218 */4219 scb->SG_segment_count = 0;
4220 memset(scb->SG_list_pointer, 0, sizeof(scb->SG_list_pointer));
4221 memset(scb->data_pointer, 0, sizeof(scb->data_pointer));
4222 scb->data_count = 0;
4223 }4224 else4225 {4226 scb->SG_segment_count = 1;
4227 scb->sg.address = (char *) cmd->request_buffer;
4228 scb->sg.length = cmd->request_bufflen;
4229 addr = &scb->sg;
4230 memcpy(scb->SG_list_pointer, &addr, sizeof(scb->SG_list_pointer));
4231 scb->data_count = scb->sg.length;
4232 memcpy(scb->data_pointer, &cmd->request_buffer, sizeof(scb->data_pointer));
4233 }4234 }4235 }4236
4237 /*+F*************************************************************************4238 * Function:4239 * aic7xxx_queue4240 *4241 * Description:4242 * Queue a SCB to the controller.4243 *-F*************************************************************************/4244 int4245 aic7xxx_queue(Scsi_Cmnd *cmd, void (*fn)(Scsi_Cmnd *))
/* */4246 {4247 longflags;
4248 structaic7xxx_host *p;
4249 structaic7xxx_scb *scb;
4250
4251 p = (structaic7xxx_host *) cmd->host->hostdata;
4252
4253 /*4254 * Check to see if channel was scanned.4255 */4256 if (!p->a_scanned && (cmd->channel == 0))
4257 {4258 printk("aic7xxx: Scanning channel A for devices.\n");
4259 p->a_scanned = TRUE;
4260 }4261 else4262 {4263 if (!p->b_scanned && (cmd->channel == 1))
4264 {4265 printk("aic7xxx: Scanning channel B for devices.\n");
4266 p->b_scanned = TRUE;
4267 }4268 }4269
4270 #if 0
4271 debug("aic7xxx: (queue) cmd(0x%x) size(%u), target %d, channel %d, lun %d.\n",
4272 cmd->cmnd[0], cmd->cmd_len, cmd->target, cmd->channel,
4273 cmd->lun & 0x07);
4274 #endif4275
4276 /*4277 * This is a critical section, since we don't want the4278 * interrupt routine mucking with the host data or the4279 * card. Since the kernel documentation is vague on4280 * whether or not we are in a cli/sti pair already, save4281 * the flags to be on the safe side.4282 */4283 save_flags(flags);
4284 cli();
4285
4286 /*4287 * Find a free slot in the SCB array to load this command4288 * into. Since can_queue is set to the maximum number of4289 * SCBs for the card, we should always find one.4290 *4291 * First try to find an scb in the free list. If there are4292 * none in the free list, then check the current number of4293 * of scbs and take an unused one from the scb array.4294 */4295 scb = p->free_scb;
4296 if (scb != NULL)
4297 {/* found one in the free list */4298 p->free_scb = scb->next; /* remove and update head of list */4299 /*4300 * Warning! For some unknown reason, the scb at the head4301 * of the free list is not the same address that it should4302 * be. That's why we set the scb pointer taken by the4303 * position in the array. The scb at the head of the list4304 * should match this address, but it doesn't.4305 */4306 scb = &(p->scb_array[scb->position]);
4307 scb->control = 0;
4308 scb->state = SCB_ACTIVE;
4309 }4310 else4311 {4312 if (p->numscb >= p->maxscb)
4313 {4314 panic("aic7xxx: (aic7xxx_queue) Couldn't find a free SCB.\n");
4315 }4316 else4317 {4318 /*4319 * Initialize the scb within the scb array. The4320 * position within the array is the position on4321 * the board that it will be loaded.4322 */4323 scb = &(p->scb_array[p->numscb]);
4324 memset(scb, 0, sizeof(*scb));
4325
4326 scb->position = p->numscb;
4327 p->numscb++;
4328 scb->state = SCB_ACTIVE;
4329 }4330 }4331
4332 scb->cmd = cmd;
4333 aic7xxx_position(cmd) = scb->position;
4334 #if 0
4335 debug_scb(scb);
4336 #endif;
4337
4338 /*4339 * Construct the SCB beforehand, so the sequencer is4340 * paused a minimal amount of time.4341 */4342 aic7xxx_buildscb(p, cmd, scb);
4343
4344 #if 0
4345 if (scb != &p->scb_array[scb->position])
4346 {4347 printk("aic7xxx: (queue) Address of SCB by position does not match SCB "
4348 "address.\n");
4349 }4350 printk("aic7xxx: (queue) SCB pos(%d) cmdptr(0x%x) state(%d) freescb(0x%x)\n",
4351 scb->position, (unsignedint) scb->cmd,
4352 scb->state, (unsignedint) p->free_scb);
4353 #endif4354
4355 /*4356 * Make sure the Scsi_Cmnd pointer is saved, the struct it points to4357 * is set up properly, and the parity error flag is reset, then send4358 * the SCB to the sequencer and watch the fun begin.4359 */4360 cmd->scsi_done = fn;
4361 aic7xxx_error(cmd) = DID_OK;
4362 aic7xxx_status(cmd) = 0;
4363 cmd->result = 0;
4364 memset(&cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
4365
4366 /*4367 * Pause the sequencer so we can play with its registers -4368 * wait for it to acknowledge the pause.4369 *4370 * XXX - should the interrupts be left on while doing this?4371 */4372 PAUSE_SEQUENCER(p);
4373
4374 /*4375 * Save the SCB pointer and put our own pointer in - this4376 * selects one of the four banks of SCB registers. Load4377 * the SCB, then write its pointer into the queue in FIFO4378 * and restore the saved SCB pointer.4379 */4380 aic7xxx_putscb(p, scb);
4381 outb(scb->position, QINFIFO + p->base);
4382
4383 UNPAUSE_SEQUENCER(p);
4384 #if 0
4385 printk("aic7xxx: (queue) After - cmd(0x%lx) scb->cmd(0x%lx) pos(%d).\n",
4386 (long) cmd, (long) scb->cmd, scb->position);
4387 #endif;
4388 restore_flags(flags);
4389 return (0);
4390 }4391
4392 /*+F*************************************************************************4393 * Function:4394 * aic7xxx_abort_reset4395 *4396 * Description:4397 * Abort or reset the current SCSI command(s). If the scb has not4398 * previously been aborted, then we attempt to send a BUS_DEVICE_RESET4399 * message to the target. If the scb has previously been unsuccessfully4400 * aborted, then we will reset the channel and have all devices renegotiate.4401 * Returns an enumerated type that indicates the status of the operation.4402 *-F*************************************************************************/4403 staticaha_abort_reset_type4404 aic7xxx_abort_reset(Scsi_Cmnd *cmd, unsignedcharerrcode)
/* */4405 {4406 structaic7xxx_scb *scb;
4407 structaic7xxx_host *p;
4408 longflags;
4409 unsignedcharbus_state;
4410 aha_abort_reset_typescb_status = ABORT_RESET_SUCCESS;
4411 intbase, found;
4412 charchannel;
4413
4414 p = (structaic7xxx_host *) cmd->host->hostdata;
4415 scb = &(p->scb_array[aic7xxx_position(cmd)]);
4416 base = p->base;
4417 channel = scb->target_channel_lun & SELBUSB ? 'B': 'A';
4418
4419 save_flags(flags);
4420 cli();
4421
4422 if (scb->state & SCB_ACTIVE)
4423 {4424 /*4425 * Ensure that the card doesn't do anything4426 * behind our back.4427 */4428 PAUSE_SEQUENCER(p);
4429
4430 printk ("aic7xxx: (abort_reset) scb state 0x%x, ", scb->state);
4431 bus_state = inb(LASTPHASE + p->base);
4432
4433 switch (bus_state)
4434 {4435 caseP_DATAOUT:
4436 printk ("Data-Out phase, ");
4437 break;
4438 caseP_DATAIN:
4439 printk ("Data-In phase, ");
4440 break;
4441 caseP_COMMAND:
4442 printk ("Command phase, ");
4443 break;
4444 caseP_MESGOUT:
4445 printk ("Message-Out phase, ");
4446 break;
4447 caseP_STATUS:
4448 printk ("Status phase, ");
4449 break;
4450 caseP_MESGIN:
4451 printk ("Message-In phase, ");
4452 break;
4453 default:
4454 printk ("while idle, LASTPHASE = 0x%x, ", bus_state);
4455 /*4456 * We're not in a valid phase, so assume we're idle.4457 */4458 bus_state = 0;
4459 break;
4460 }4461 printk ("SCSISIGI = 0x%x\n", inb (p->base + SCSISIGI));
4462
4463 /*4464 * First, determine if we want to do a bus reset or simply a bus device4465 * reset. If this is the first time that a transaction has timed out,4466 * just schedule a bus device reset. Otherwise, we reset the bus and4467 * abort all pending I/Os on that bus.4468 */4469 if (scb->state & SCB_ABORTED)
4470 {4471 /*4472 * Been down this road before. Do a full bus reset.4473 */4474 found = aic7xxx_reset_channel(p, channel, scb->position, TRUE);
4475 }4476 else4477 {4478 unsignedcharactive_scb, control;
4479 structaic7xxx_scb *active_scbp;
4480
4481 /*4482 * Send a Bus Device Reset Message:4483 * The target we select to send the message to may be entirely4484 * different than the target pointed to by the scb that timed4485 * out. If the command is in the QINFIFO or the waiting for4486 * selection list, it's not tying up the bus and isn't responsible4487 * for the delay so we pick off the active command which should4488 * be the SCB selected by SCBPTR. If it's disconnected or active,4489 * we device reset the target scbp points to. Although it may4490 * be that this target is not responsible for the delay, it may4491 * also be that we're timing out on a command that just takes4492 * too much time, so we try the bus device reset there first.4493 */4494 active_scb = inb(SCBPTR + base);
4495 active_scbp = &(p->scb_array[active_scb]);
4496 control = inb(SCB_CONTROL + base);
4497
4498 /*4499 * Test to see if scbp is disconnected4500 */4501 outb(scb->position, SCBPTR + base);
4502 if (inb(SCB_CONTROL + base) & DISCONNECTED)
4503 {4504 #ifdefAIC7XXX_DEBUG_ABORT4505 printk ("aic7xxx: (abort_scb) scb %d is disconnected; "
4506 "bus device reset message queued.\n", scb->position);
4507 #endif4508 scb->state |= (SCB_DEVICE_RESET | SCB_ABORTED);
4509 scb->SG_segment_count = 0;
4510 memset(scb->SG_list_pointer, 0, sizeof(scb->SG_list_pointer));
4511 memset(scb->data_pointer, 0, sizeof(scb->data_pointer));
4512 scb->data_count = 0;
4513 aic7xxx_putscb(p, scb);
4514 aic7xxx_add_waiting_scb(base, scb);
4515 aic7xxx_error(scb->cmd) = errcode;
4516 scb_status = ABORT_RESET_PENDING;
4517 outb(active_scb, SCBPTR + base);
4518 UNPAUSE_SEQUENCER(p);
4519 }4520 else4521 {4522 /*4523 * Is the active SCB really active?4524 */4525 if ((active_scbp->state & SCB_ACTIVE) && bus_state)
4526 {4527 /*4528 * Load the message buffer and assert attention.4529 */4530 active_scbp->state |= (SCB_DEVICE_RESET | SCB_ABORTED);
4531 outb(1, MSG_LEN + base);
4532 outb(MSG_BUS_DEVICE_RESET, MSG0 + base);
4533 outb(bus_state | ATNO, SCSISIGO + base);
4534 #ifdefAIC7XXX_DEBUG_ABORT4535 printk ("aic7xxx: (abort_scb) asserted ATN - "
4536 "bus device reset in message buffer.\n");
4537 #endif4538 if (active_scbp != scb)
4539 {4540 /*4541 * XXX - We would like to increment the timeout on scb, but4542 * access to that routine is denied because it is hidden4543 * in scsi.c. If we were able to do this, it would give4544 * scb a new lease on life.4545 */4546 ;
4547 }4548 aic7xxx_error(scb->cmd) = errcode;
4549 scb_status = ABORT_RESET_PENDING;
4550 /*4551 * Restore the active SCB and unpause the sequencer.4552 */4553 outb(active_scb, SCBPTR + base);
4554
4555 UNPAUSE_SEQUENCER(p);
4556 }4557 else4558 {4559 #ifdefAIC7XXX_DEBUG_ABORT4560 printk ("aic7xxx: (abort_scb) no active command.\n");
4561 #endif4562 /*4563 * No active command to single out, so reset4564 * the bus for the timed out target.4565 */4566 aic7xxx_reset_channel(p, channel, scb->position, TRUE);
4567 }4568 }4569 }4570 }4571 else4572 {4573 /*4574 * The scb is not active and must have completed after the timeout4575 * check in scsi.c and before we check the scb state above. For4576 * this case we return SCSI_ABORT_NOT_RUNNING (if abort was called)4577 * or SCSI_RESET_SUCCESS (if reset was called).4578 */4579 #ifdefAIC7XXX_DEBUG_ABORT4580 printk ("aic7xxx: (abort_reset) called with no active scb, errcode 0x%x\n",
4581 errcode);
4582 #endif4583 scb_status = ABORT_RESET_INACTIVE;
4584 /*4585 * According to the comments in scsi.h and Michael Neuffer, if we do not4586 * have an active command for abort or reset, we should not call the4587 * command done function. Unfortunately, this hangs the system for me4588 * unless we *do* call the done function.4589 *4590 * XXX - Revisit this sometime!4591 */4592 cmd->result = errcode << 16;
4593 cmd->scsi_done(cmd);
4594 }4595
4596 restore_flags(flags);
4597 return (scb_status);
4598 }4599
4600
4601 /*+F*************************************************************************4602 * Function:4603 * aic7xxx_abort4604 *4605 * Description:4606 * Abort the current SCSI command(s).4607 *-F*************************************************************************/4608 int4609 aic7xxx_abort(Scsi_Cmnd *cmd)
/* */4610 {4611 #ifdefAIC7XXX_DEBUG_ABORT4612 printk ("aic7xxx: (abort) target/channel %d/%d\n", cmd->target, cmd->channel);
4613 #endif4614
4615 switch (aic7xxx_abort_reset(cmd, DID_ABORT))
4616 {4617 caseABORT_RESET_INACTIVE:
4618 return (SCSI_ABORT_NOT_RUNNING);
4619 break;
4620 caseABORT_RESET_PENDING:
4621 return (SCSI_ABORT_PENDING);
4622 break;
4623 caseABORT_RESET_SUCCESS:
4624 default:
4625 return (SCSI_ABORT_SUCCESS);
4626 break;
4627 }4628 }4629
4630 /*+F*************************************************************************4631 * Function:4632 * aic7xxx_reset4633 *4634 * Description:4635 * Resetting the bus always succeeds - is has to, otherwise the4636 * kernel will panic! Try a surgical technique - sending a BUS4637 * DEVICE RESET message - on the offending target before pulling4638 * the SCSI bus reset line.4639 *-F*************************************************************************/4640 int4641 aic7xxx_reset(Scsi_Cmnd *cmd)
/* */4642 {4643 #ifdefAIC7XXX_DEBUG_ABORT4644 printk ("aic7xxx: (reset) target/channel %d/%d\n", cmd->target, cmd->channel);
4645 #endif4646
4647 switch (aic7xxx_abort_reset(cmd, DID_RESET))
4648 {4649 caseABORT_RESET_PENDING:
4650 return (SCSI_RESET_PENDING);
4651 break;
4652 caseABORT_RESET_SUCCESS:
4653 return (SCSI_RESET_BUS_RESET | SCSI_RESET_SUCCESS);
4654 break;
4655 caseABORT_RESET_INACTIVE:
4656 default:
4657 return (SCSI_RESET_SUCCESS);
4658 break;
4659 }4660 }4661
4662 /*+F*************************************************************************4663 * Function:4664 * aic7xxx_biosparam4665 *4666 * Description:4667 * Return the disk geometry for the given SCSI device.4668 *-F*************************************************************************/4669 int4670 aic7xxx_biosparam(Disk *disk, kdev_tdev, intgeom[])
/* */4671 {4672 intheads, sectors, cylinders;
4673 structaic7xxx_host *p;
4674
4675 p = (structaic7xxx_host *) disk->device->host->hostdata;
4676
4677 /*4678 * XXX - if I could portably find the card's configuration4679 * information, then this could be autodetected instead4680 * of left to a boot-time switch.4681 */4682 heads = 64;
4683 sectors = 32;
4684 cylinders = disk->capacity / (heads * sectors);
4685
4686 if (p->extended && cylinders > 1024)
4687 {4688 heads = 255;
4689 sectors = 63;
4690 cylinders = disk->capacity / (255 * 63);
4691 }4692
4693 geom[0] = heads;
4694 geom[1] = sectors;
4695 geom[2] = cylinders;
4696
4697 return (0);
4698 }4699
4700 #include "aic7xxx_proc.c"
4701
4702 #ifdefMODULE4703 /* Eventually this will go into an include file, but this will be later */4704 Scsi_Host_Templatedriver_template = AIC7XXX;
4705
4706 #include "scsi_module.c"
4707 #endif4708
4709 /*4710 * Overrides for Emacs so that we almost follow Linus's tabbing style.4711 * Emacs will notice this stuff at the end of the file and automatically4712 * adjust the settings for this buffer only. This must remain at the end4713 * of the file.4714 * ---------------------------------------------------------------------------4715 * Local variables:4716 * c-indent-level: 24717 * c-brace-imaginary-offset: 04718 * c-brace-offset: -24719 * c-argdecl-indent: 24720 * c-label-offset: -24721 * c-continued-statement-offset: 24722 * c-continued-brace-offset: 04723 * indent-tabs-mode: nil4724 * tab-width: 84725 * End:4726 */4727