1 /* 2 * Device driver optimized for the Symbios/LSI 53C896/53C895A/53C1010 3 * PCI-SCSI controllers. 4 * 5 * Copyright (C) 1999-2001 Gerard Roudier <groudier@free.fr> 6 * 7 * This driver also supports the following Symbios/LSI PCI-SCSI chips: 8 * 53C810A, 53C825A, 53C860, 53C875, 53C876, 53C885, 53C895, 9 * 53C810, 53C815, 53C825 and the 53C1510D is 53C8XX mode. 10 * 11 * 12 * This driver for FreeBSD-CAM is derived from the Linux sym53c8xx driver. 13 * Copyright (C) 1998-1999 Gerard Roudier 14 * 15 * The sym53c8xx driver is derived from the ncr53c8xx driver that had been 16 * a port of the FreeBSD ncr driver to Linux-1.2.13. 17 * 18 * The original ncr driver has been written for 386bsd and FreeBSD by 19 * Wolfgang Stanglmeier <wolf@cologne.de> 20 * Stefan Esser <se@mi.Uni-Koeln.de> 21 * Copyright (C) 1994 Wolfgang Stanglmeier 22 * 23 * The initialisation code, and part of the code that addresses 24 * FreeBSD-CAM services is based on the aic7xxx driver for FreeBSD-CAM 25 * written by Justin T. Gibbs. 26 * 27 * Other major contributions: 28 * 29 * NVRAM detection and reading. 30 * Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk> 31 * 32 *----------------------------------------------------------------------------- 33 * 34 * Redistribution and use in source and binary forms, with or without 35 * modification, are permitted provided that the following conditions 36 * are met: 37 * 1. Redistributions of source code must retain the above copyright 38 * notice, this list of conditions and the following disclaimer. 39 * 2. Redistributions in binary form must reproduce the above copyright 40 * notice, this list of conditions and the following disclaimer in the 41 * documentation and/or other materials provided with the distribution. 42 * 3. The name of the author may not be used to endorse or promote products 43 * derived from this software without specific prior written permission. 44 * 45 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 48 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 49 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 55 * SUCH DAMAGE. 56 */ 57 58 /* $FreeBSD$ */ 59 60 #define SYM_DRIVER_NAME "sym-1.6.5-20000902" 61 62 /* #define SYM_DEBUG_GENERIC_SUPPORT */ 63 /* #define CAM_NEW_TRAN_CODE */ 64 65 #include <sys/param.h> 66 67 /* 68 * Only use the BUS stuff for PCI under FreeBSD 4 and later versions. 69 * Note that the old BUS stuff also works for FreeBSD 4 and spares 70 * about 1 KB for the driver object file. 71 */ 72 #if __FreeBSD_version >= 400000 73 #define FreeBSD_Bus_Dma_Abstraction 74 #define FreeBSD_Bus_Io_Abstraction 75 #define FreeBSD_Bus_Space_Abstraction 76 #endif 77 78 /* 79 * Driver configuration options. 80 */ 81 #include "opt_sym.h" 82 #include <dev/sym/sym_conf.h> 83 84 #ifndef FreeBSD_Bus_Io_Abstraction 85 #include "ncr.h" /* To know if the ncr has been configured */ 86 #endif 87 88 #include <sys/systm.h> 89 #include <sys/malloc.h> 90 #include <sys/endian.h> 91 #include <sys/kernel.h> 92 #include <sys/lock.h> 93 #include <sys/mutex.h> 94 #ifdef FreeBSD_Bus_Io_Abstraction 95 #include <sys/module.h> 96 #include <sys/bus.h> 97 #endif 98 99 #include <sys/proc.h> 100 101 #include <pci/pcireg.h> 102 #include <pci/pcivar.h> 103 104 #ifdef FreeBSD_Bus_Space_Abstraction 105 #include <machine/bus_memio.h> 106 /* 107 * Only include bus_pio if needed. 108 * This avoids bus space primitives to be uselessly bloated 109 * by out-of-age PIO operations. 110 */ 111 #ifdef SYM_CONF_IOMAPPED 112 #include <machine/bus_pio.h> 113 #endif 114 #endif 115 #include <machine/bus.h> 116 117 #ifdef FreeBSD_Bus_Io_Abstraction 118 #include <machine/resource.h> 119 #include <sys/rman.h> 120 #endif 121 122 #include <cam/cam.h> 123 #include <cam/cam_ccb.h> 124 #include <cam/cam_sim.h> 125 #include <cam/cam_xpt_sim.h> 126 #include <cam/cam_debug.h> 127 128 #include <cam/scsi/scsi_all.h> 129 #include <cam/scsi/scsi_message.h> 130 131 #include <vm/vm.h> 132 #include <vm/vm_param.h> 133 #include <vm/pmap.h> 134 135 /* Short and quite clear integer types */ 136 typedef int8_t s8; 137 typedef int16_t s16; 138 typedef int32_t s32; 139 typedef u_int8_t u8; 140 typedef u_int16_t u16; 141 typedef u_int32_t u32; 142 143 /* 144 * From 'cam.error_recovery_diffs.20010313.context' patch. 145 */ 146 #ifdef CAM_NEW_TRAN_CODE 147 #define FreeBSD_New_Tran_Settings 148 #endif /* CAM_NEW_TRAN_CODE */ 149 150 /* 151 * Driver definitions. 152 */ 153 #include <dev/sym/sym_defs.h> 154 #include <dev/sym/sym_fw.h> 155 156 /* 157 * IA32 architecture does not reorder STORES and prevents 158 * LOADS from passing STORES. It is called `program order' 159 * by Intel and allows device drivers to deal with memory 160 * ordering by only ensuring that the code is not reordered 161 * by the compiler when ordering is required. 162 * Other architectures implement a weaker ordering that 163 * requires memory barriers (and also IO barriers when they 164 * make sense) to be used. 165 */ 166 167 #if defined __i386__ || defined __amd64__ 168 #define MEMORY_BARRIER() do { ; } while(0) 169 #elif defined __alpha__ 170 #define MEMORY_BARRIER() alpha_mb() 171 #elif defined __powerpc__ 172 #define MEMORY_BARRIER() __asm__ volatile("eieio; sync" : : : "memory") 173 #elif defined __ia64__ 174 #define MEMORY_BARRIER() __asm__ volatile("mf.a; mf" : : : "memory") 175 #elif defined __sparc64__ 176 #define MEMORY_BARRIER() __asm__ volatile("membar #Sync" : : : "memory") 177 #else 178 #error "Not supported platform" 179 #endif 180 181 /* 182 * Portable but silly implemented byte order primitives. 183 * We define the primitives we need, since FreeBSD doesn't 184 * seem to have them yet. 185 */ 186 #if BYTE_ORDER == BIG_ENDIAN 187 188 #define __revb16(x) ( (((u16)(x) & (u16)0x00ffU) << 8) | \ 189 (((u16)(x) & (u16)0xff00U) >> 8) ) 190 #define __revb32(x) ( (((u32)(x) & 0x000000ffU) << 24) | \ 191 (((u32)(x) & 0x0000ff00U) << 8) | \ 192 (((u32)(x) & 0x00ff0000U) >> 8) | \ 193 (((u32)(x) & 0xff000000U) >> 24) ) 194 195 #define __htole16(v) __revb16(v) 196 #define __htole32(v) __revb32(v) 197 #define __le16toh(v) __htole16(v) 198 #define __le32toh(v) __htole32(v) 199 200 static __inline u16 _htole16(u16 v) { return __htole16(v); } 201 static __inline u32 _htole32(u32 v) { return __htole32(v); } 202 #define _le16toh _htole16 203 #define _le32toh _htole32 204 205 #else /* LITTLE ENDIAN */ 206 207 #define __htole16(v) (v) 208 #define __htole32(v) (v) 209 #define __le16toh(v) (v) 210 #define __le32toh(v) (v) 211 212 #define _htole16(v) (v) 213 #define _htole32(v) (v) 214 #define _le16toh(v) (v) 215 #define _le32toh(v) (v) 216 217 #endif /* BYTE_ORDER */ 218 219 /* 220 * A la VMS/CAM-3 queue management. 221 */ 222 223 typedef struct sym_quehead { 224 struct sym_quehead *flink; /* Forward pointer */ 225 struct sym_quehead *blink; /* Backward pointer */ 226 } SYM_QUEHEAD; 227 228 #define sym_que_init(ptr) do { \ 229 (ptr)->flink = (ptr); (ptr)->blink = (ptr); \ 230 } while (0) 231 232 static __inline struct sym_quehead *sym_que_first(struct sym_quehead *head) 233 { 234 return (head->flink == head) ? 0 : head->flink; 235 } 236 237 static __inline struct sym_quehead *sym_que_last(struct sym_quehead *head) 238 { 239 return (head->blink == head) ? 0 : head->blink; 240 } 241 242 static __inline void __sym_que_add(struct sym_quehead * new, 243 struct sym_quehead * blink, 244 struct sym_quehead * flink) 245 { 246 flink->blink = new; 247 new->flink = flink; 248 new->blink = blink; 249 blink->flink = new; 250 } 251 252 static __inline void __sym_que_del(struct sym_quehead * blink, 253 struct sym_quehead * flink) 254 { 255 flink->blink = blink; 256 blink->flink = flink; 257 } 258 259 static __inline int sym_que_empty(struct sym_quehead *head) 260 { 261 return head->flink == head; 262 } 263 264 static __inline void sym_que_splice(struct sym_quehead *list, 265 struct sym_quehead *head) 266 { 267 struct sym_quehead *first = list->flink; 268 269 if (first != list) { 270 struct sym_quehead *last = list->blink; 271 struct sym_quehead *at = head->flink; 272 273 first->blink = head; 274 head->flink = first; 275 276 last->flink = at; 277 at->blink = last; 278 } 279 } 280 281 #define sym_que_entry(ptr, type, member) \ 282 ((type *)((char *)(ptr)-(unsigned int)(&((type *)0)->member))) 283 284 285 #define sym_insque(new, pos) __sym_que_add(new, pos, (pos)->flink) 286 287 #define sym_remque(el) __sym_que_del((el)->blink, (el)->flink) 288 289 #define sym_insque_head(new, head) __sym_que_add(new, head, (head)->flink) 290 291 static __inline struct sym_quehead *sym_remque_head(struct sym_quehead *head) 292 { 293 struct sym_quehead *elem = head->flink; 294 295 if (elem != head) 296 __sym_que_del(head, elem->flink); 297 else 298 elem = 0; 299 return elem; 300 } 301 302 #define sym_insque_tail(new, head) __sym_que_add(new, (head)->blink, head) 303 304 static __inline struct sym_quehead *sym_remque_tail(struct sym_quehead *head) 305 { 306 struct sym_quehead *elem = head->blink; 307 308 if (elem != head) 309 __sym_que_del(elem->blink, head); 310 else 311 elem = 0; 312 return elem; 313 } 314 315 /* 316 * This one may be useful. 317 */ 318 #define FOR_EACH_QUEUED_ELEMENT(head, qp) \ 319 for (qp = (head)->flink; qp != (head); qp = qp->flink) 320 /* 321 * FreeBSD does not offer our kind of queue in the CAM CCB. 322 * So, we have to cast. 323 */ 324 #define sym_qptr(p) ((struct sym_quehead *) (p)) 325 326 /* 327 * Simple bitmap operations. 328 */ 329 #define sym_set_bit(p, n) (((u32 *)(p))[(n)>>5] |= (1<<((n)&0x1f))) 330 #define sym_clr_bit(p, n) (((u32 *)(p))[(n)>>5] &= ~(1<<((n)&0x1f))) 331 #define sym_is_bit(p, n) (((u32 *)(p))[(n)>>5] & (1<<((n)&0x1f))) 332 333 /* 334 * Number of tasks per device we want to handle. 335 */ 336 #if SYM_CONF_MAX_TAG_ORDER > 8 337 #error "more than 256 tags per logical unit not allowed." 338 #endif 339 #define SYM_CONF_MAX_TASK (1<<SYM_CONF_MAX_TAG_ORDER) 340 341 /* 342 * Donnot use more tasks that we can handle. 343 */ 344 #ifndef SYM_CONF_MAX_TAG 345 #define SYM_CONF_MAX_TAG SYM_CONF_MAX_TASK 346 #endif 347 #if SYM_CONF_MAX_TAG > SYM_CONF_MAX_TASK 348 #undef SYM_CONF_MAX_TAG 349 #define SYM_CONF_MAX_TAG SYM_CONF_MAX_TASK 350 #endif 351 352 /* 353 * This one means 'NO TAG for this job' 354 */ 355 #define NO_TAG (256) 356 357 /* 358 * Number of SCSI targets. 359 */ 360 #if SYM_CONF_MAX_TARGET > 16 361 #error "more than 16 targets not allowed." 362 #endif 363 364 /* 365 * Number of logical units per target. 366 */ 367 #if SYM_CONF_MAX_LUN > 64 368 #error "more than 64 logical units per target not allowed." 369 #endif 370 371 /* 372 * Asynchronous pre-scaler (ns). Shall be 40 for 373 * the SCSI timings to be compliant. 374 */ 375 #define SYM_CONF_MIN_ASYNC (40) 376 377 /* 378 * Number of entries in the START and DONE queues. 379 * 380 * We limit to 1 PAGE in order to succeed allocation of 381 * these queues. Each entry is 8 bytes long (2 DWORDS). 382 */ 383 #ifdef SYM_CONF_MAX_START 384 #define SYM_CONF_MAX_QUEUE (SYM_CONF_MAX_START+2) 385 #else 386 #define SYM_CONF_MAX_QUEUE (7*SYM_CONF_MAX_TASK+2) 387 #define SYM_CONF_MAX_START (SYM_CONF_MAX_QUEUE-2) 388 #endif 389 390 #if SYM_CONF_MAX_QUEUE > PAGE_SIZE/8 391 #undef SYM_CONF_MAX_QUEUE 392 #define SYM_CONF_MAX_QUEUE PAGE_SIZE/8 393 #undef SYM_CONF_MAX_START 394 #define SYM_CONF_MAX_START (SYM_CONF_MAX_QUEUE-2) 395 #endif 396 397 /* 398 * For this one, we want a short name :-) 399 */ 400 #define MAX_QUEUE SYM_CONF_MAX_QUEUE 401 402 /* 403 * Active debugging tags and verbosity. 404 */ 405 #define DEBUG_ALLOC (0x0001) 406 #define DEBUG_PHASE (0x0002) 407 #define DEBUG_POLL (0x0004) 408 #define DEBUG_QUEUE (0x0008) 409 #define DEBUG_RESULT (0x0010) 410 #define DEBUG_SCATTER (0x0020) 411 #define DEBUG_SCRIPT (0x0040) 412 #define DEBUG_TINY (0x0080) 413 #define DEBUG_TIMING (0x0100) 414 #define DEBUG_NEGO (0x0200) 415 #define DEBUG_TAGS (0x0400) 416 #define DEBUG_POINTER (0x0800) 417 418 #if 0 419 static int sym_debug = 0; 420 #define DEBUG_FLAGS sym_debug 421 #else 422 /* #define DEBUG_FLAGS (0x0631) */ 423 #define DEBUG_FLAGS (0x0000) 424 425 #endif 426 #define sym_verbose (np->verbose) 427 428 /* 429 * Insert a delay in micro-seconds and milli-seconds. 430 */ 431 static void UDELAY(int us) { DELAY(us); } 432 static void MDELAY(int ms) { while (ms--) UDELAY(1000); } 433 434 /* 435 * Simple power of two buddy-like allocator. 436 * 437 * This simple code is not intended to be fast, but to 438 * provide power of 2 aligned memory allocations. 439 * Since the SCRIPTS processor only supplies 8 bit arithmetic, 440 * this allocator allows simple and fast address calculations 441 * from the SCRIPTS code. In addition, cache line alignment 442 * is guaranteed for power of 2 cache line size. 443 * 444 * This allocator has been developped for the Linux sym53c8xx 445 * driver, since this O/S does not provide naturally aligned 446 * allocations. 447 * It has the advantage of allowing the driver to use private 448 * pages of memory that will be useful if we ever need to deal 449 * with IO MMUs for PCI. 450 */ 451 452 #define MEMO_SHIFT 4 /* 16 bytes minimum memory chunk */ 453 #define MEMO_PAGE_ORDER 0 /* 1 PAGE maximum */ 454 #if 0 455 #define MEMO_FREE_UNUSED /* Free unused pages immediately */ 456 #endif 457 #define MEMO_WARN 1 458 #define MEMO_CLUSTER_SHIFT (PAGE_SHIFT+MEMO_PAGE_ORDER) 459 #define MEMO_CLUSTER_SIZE (1UL << MEMO_CLUSTER_SHIFT) 460 #define MEMO_CLUSTER_MASK (MEMO_CLUSTER_SIZE-1) 461 462 #define get_pages() malloc(MEMO_CLUSTER_SIZE, M_DEVBUF, M_NOWAIT) 463 #define free_pages(p) free((p), M_DEVBUF) 464 465 typedef u_long m_addr_t; /* Enough bits to bit-hack addresses */ 466 467 typedef struct m_link { /* Link between free memory chunks */ 468 struct m_link *next; 469 } m_link_s; 470 471 #ifdef FreeBSD_Bus_Dma_Abstraction 472 typedef struct m_vtob { /* Virtual to Bus address translation */ 473 struct m_vtob *next; 474 bus_dmamap_t dmamap; /* Map for this chunk */ 475 m_addr_t vaddr; /* Virtual address */ 476 m_addr_t baddr; /* Bus physical address */ 477 } m_vtob_s; 478 /* Hash this stuff a bit to speed up translations */ 479 #define VTOB_HASH_SHIFT 5 480 #define VTOB_HASH_SIZE (1UL << VTOB_HASH_SHIFT) 481 #define VTOB_HASH_MASK (VTOB_HASH_SIZE-1) 482 #define VTOB_HASH_CODE(m) \ 483 ((((m_addr_t) (m)) >> MEMO_CLUSTER_SHIFT) & VTOB_HASH_MASK) 484 #endif 485 486 typedef struct m_pool { /* Memory pool of a given kind */ 487 #ifdef FreeBSD_Bus_Dma_Abstraction 488 bus_dma_tag_t dev_dmat; /* Identifies the pool */ 489 bus_dma_tag_t dmat; /* Tag for our fixed allocations */ 490 m_addr_t (*getp)(struct m_pool *); 491 #ifdef MEMO_FREE_UNUSED 492 void (*freep)(struct m_pool *, m_addr_t); 493 #endif 494 #define M_GETP() mp->getp(mp) 495 #define M_FREEP(p) mp->freep(mp, p) 496 int nump; 497 m_vtob_s *(vtob[VTOB_HASH_SIZE]); 498 struct m_pool *next; 499 #else 500 #define M_GETP() get_pages() 501 #define M_FREEP(p) free_pages(p) 502 #endif /* FreeBSD_Bus_Dma_Abstraction */ 503 struct m_link h[MEMO_CLUSTER_SHIFT - MEMO_SHIFT + 1]; 504 } m_pool_s; 505 506 static void *___sym_malloc(m_pool_s *mp, int size) 507 { 508 int i = 0; 509 int s = (1 << MEMO_SHIFT); 510 int j; 511 m_addr_t a; 512 m_link_s *h = mp->h; 513 514 if (size > MEMO_CLUSTER_SIZE) 515 return 0; 516 517 while (size > s) { 518 s <<= 1; 519 ++i; 520 } 521 522 j = i; 523 while (!h[j].next) { 524 if (s == MEMO_CLUSTER_SIZE) { 525 h[j].next = (m_link_s *) M_GETP(); 526 if (h[j].next) 527 h[j].next->next = 0; 528 break; 529 } 530 ++j; 531 s <<= 1; 532 } 533 a = (m_addr_t) h[j].next; 534 if (a) { 535 h[j].next = h[j].next->next; 536 while (j > i) { 537 j -= 1; 538 s >>= 1; 539 h[j].next = (m_link_s *) (a+s); 540 h[j].next->next = 0; 541 } 542 } 543 #ifdef DEBUG 544 printf("___sym_malloc(%d) = %p\n", size, (void *) a); 545 #endif 546 return (void *) a; 547 } 548 549 static void ___sym_mfree(m_pool_s *mp, void *ptr, int size) 550 { 551 int i = 0; 552 int s = (1 << MEMO_SHIFT); 553 m_link_s *q; 554 m_addr_t a, b; 555 m_link_s *h = mp->h; 556 557 #ifdef DEBUG 558 printf("___sym_mfree(%p, %d)\n", ptr, size); 559 #endif 560 561 if (size > MEMO_CLUSTER_SIZE) 562 return; 563 564 while (size > s) { 565 s <<= 1; 566 ++i; 567 } 568 569 a = (m_addr_t) ptr; 570 571 while (1) { 572 #ifdef MEMO_FREE_UNUSED 573 if (s == MEMO_CLUSTER_SIZE) { 574 M_FREEP(a); 575 break; 576 } 577 #endif 578 b = a ^ s; 579 q = &h[i]; 580 while (q->next && q->next != (m_link_s *) b) { 581 q = q->next; 582 } 583 if (!q->next) { 584 ((m_link_s *) a)->next = h[i].next; 585 h[i].next = (m_link_s *) a; 586 break; 587 } 588 q->next = q->next->next; 589 a = a & b; 590 s <<= 1; 591 ++i; 592 } 593 } 594 595 static void *__sym_calloc2(m_pool_s *mp, int size, char *name, int uflags) 596 { 597 void *p; 598 599 p = ___sym_malloc(mp, size); 600 601 if (DEBUG_FLAGS & DEBUG_ALLOC) 602 printf ("new %-10s[%4d] @%p.\n", name, size, p); 603 604 if (p) 605 bzero(p, size); 606 else if (uflags & MEMO_WARN) 607 printf ("__sym_calloc2: failed to allocate %s[%d]\n", name, size); 608 609 return p; 610 } 611 612 #define __sym_calloc(mp, s, n) __sym_calloc2(mp, s, n, MEMO_WARN) 613 614 static void __sym_mfree(m_pool_s *mp, void *ptr, int size, char *name) 615 { 616 if (DEBUG_FLAGS & DEBUG_ALLOC) 617 printf ("freeing %-10s[%4d] @%p.\n", name, size, ptr); 618 619 ___sym_mfree(mp, ptr, size); 620 621 } 622 623 /* 624 * Default memory pool we donnot need to involve in DMA. 625 */ 626 #ifndef FreeBSD_Bus_Dma_Abstraction 627 /* 628 * Without the `bus dma abstraction', all the memory is assumed 629 * DMAable and a single pool is all what we need. 630 */ 631 static m_pool_s mp0; 632 633 #else 634 /* 635 * With the `bus dma abstraction', we use a separate pool for 636 * memory we donnot need to involve in DMA. 637 */ 638 static m_addr_t ___mp0_getp(m_pool_s *mp) 639 { 640 m_addr_t m = (m_addr_t) get_pages(); 641 if (m) 642 ++mp->nump; 643 return m; 644 } 645 646 #ifdef MEMO_FREE_UNUSED 647 static void ___mp0_freep(m_pool_s *mp, m_addr_t m) 648 { 649 free_pages(m); 650 --mp->nump; 651 } 652 #endif 653 654 #ifdef MEMO_FREE_UNUSED 655 static m_pool_s mp0 = {0, 0, ___mp0_getp, ___mp0_freep}; 656 #else 657 static m_pool_s mp0 = {0, 0, ___mp0_getp}; 658 #endif 659 660 #endif /* FreeBSD_Bus_Dma_Abstraction */ 661 662 /* 663 * Actual memory allocation routine for non-DMAed memory. 664 */ 665 static void *sym_calloc(int size, char *name) 666 { 667 void *m; 668 /* Lock */ 669 m = __sym_calloc(&mp0, size, name); 670 /* Unlock */ 671 return m; 672 } 673 674 /* 675 * Actual memory allocation routine for non-DMAed memory. 676 */ 677 static void sym_mfree(void *ptr, int size, char *name) 678 { 679 /* Lock */ 680 __sym_mfree(&mp0, ptr, size, name); 681 /* Unlock */ 682 } 683 684 /* 685 * DMAable pools. 686 */ 687 #ifndef FreeBSD_Bus_Dma_Abstraction 688 /* 689 * Without `bus dma abstraction', all the memory is DMAable, and 690 * only a single pool is needed (vtophys() is our friend). 691 */ 692 #define __sym_calloc_dma(b, s, n) sym_calloc(s, n) 693 #define __sym_mfree_dma(b, p, s, n) sym_mfree(p, s, n) 694 #ifdef __alpha__ 695 #define __vtobus(b, p) alpha_XXX_dmamap((vm_offset_t)(p)) 696 #else /*__i386__, __sparc64__*/ 697 #define __vtobus(b, p) vtophys(p) 698 #endif 699 700 #else 701 /* 702 * With `bus dma abstraction', we use a separate pool per parent 703 * BUS handle. A reverse table (hashed) is maintained for virtual 704 * to BUS address translation. 705 */ 706 static void getbaddrcb(void *arg, bus_dma_segment_t *segs, int nseg, int error) 707 { 708 bus_addr_t *baddr; 709 baddr = (bus_addr_t *)arg; 710 *baddr = segs->ds_addr; 711 } 712 713 static m_addr_t ___dma_getp(m_pool_s *mp) 714 { 715 m_vtob_s *vbp; 716 void *vaddr = 0; 717 bus_addr_t baddr = 0; 718 719 vbp = __sym_calloc(&mp0, sizeof(*vbp), "VTOB"); 720 if (!vbp) 721 goto out_err; 722 723 if (bus_dmamem_alloc(mp->dmat, &vaddr, 724 BUS_DMA_NOWAIT, &vbp->dmamap)) 725 goto out_err; 726 bus_dmamap_load(mp->dmat, vbp->dmamap, vaddr, 727 MEMO_CLUSTER_SIZE, getbaddrcb, &baddr, 0); 728 if (baddr) { 729 int hc = VTOB_HASH_CODE(vaddr); 730 vbp->vaddr = (m_addr_t) vaddr; 731 vbp->baddr = (m_addr_t) baddr; 732 vbp->next = mp->vtob[hc]; 733 mp->vtob[hc] = vbp; 734 ++mp->nump; 735 return (m_addr_t) vaddr; 736 } 737 out_err: 738 if (baddr) 739 bus_dmamap_unload(mp->dmat, vbp->dmamap); 740 if (vaddr) 741 bus_dmamem_free(mp->dmat, vaddr, vbp->dmamap); 742 if (vbp->dmamap) 743 bus_dmamap_destroy(mp->dmat, vbp->dmamap); 744 if (vbp) 745 __sym_mfree(&mp0, vbp, sizeof(*vbp), "VTOB"); 746 return 0; 747 } 748 749 #ifdef MEMO_FREE_UNUSED 750 static void ___dma_freep(m_pool_s *mp, m_addr_t m) 751 { 752 m_vtob_s **vbpp, *vbp; 753 int hc = VTOB_HASH_CODE(m); 754 755 vbpp = &mp->vtob[hc]; 756 while (*vbpp && (*vbpp)->vaddr != m) 757 vbpp = &(*vbpp)->next; 758 if (*vbpp) { 759 vbp = *vbpp; 760 *vbpp = (*vbpp)->next; 761 bus_dmamap_unload(mp->dmat, vbp->dmamap); 762 bus_dmamem_free(mp->dmat, (void *) vbp->vaddr, vbp->dmamap); 763 bus_dmamap_destroy(mp->dmat, vbp->dmamap); 764 __sym_mfree(&mp0, vbp, sizeof(*vbp), "VTOB"); 765 --mp->nump; 766 } 767 } 768 #endif 769 770 static __inline m_pool_s *___get_dma_pool(bus_dma_tag_t dev_dmat) 771 { 772 m_pool_s *mp; 773 for (mp = mp0.next; mp && mp->dev_dmat != dev_dmat; mp = mp->next); 774 return mp; 775 } 776 777 static m_pool_s *___cre_dma_pool(bus_dma_tag_t dev_dmat) 778 { 779 m_pool_s *mp = 0; 780 781 mp = __sym_calloc(&mp0, sizeof(*mp), "MPOOL"); 782 if (mp) { 783 mp->dev_dmat = dev_dmat; 784 if (!bus_dma_tag_create(dev_dmat, 1, MEMO_CLUSTER_SIZE, 785 BUS_SPACE_MAXADDR_32BIT, 786 BUS_SPACE_MAXADDR_32BIT, 787 NULL, NULL, MEMO_CLUSTER_SIZE, 1, 788 MEMO_CLUSTER_SIZE, 0, 789 busdma_lock_mutex, &Giant, &mp->dmat)) { 790 mp->getp = ___dma_getp; 791 #ifdef MEMO_FREE_UNUSED 792 mp->freep = ___dma_freep; 793 #endif 794 mp->next = mp0.next; 795 mp0.next = mp; 796 return mp; 797 } 798 } 799 if (mp) 800 __sym_mfree(&mp0, mp, sizeof(*mp), "MPOOL"); 801 return 0; 802 } 803 804 #ifdef MEMO_FREE_UNUSED 805 static void ___del_dma_pool(m_pool_s *p) 806 { 807 struct m_pool **pp = &mp0.next; 808 809 while (*pp && *pp != p) 810 pp = &(*pp)->next; 811 if (*pp) { 812 *pp = (*pp)->next; 813 bus_dma_tag_destroy(p->dmat); 814 __sym_mfree(&mp0, p, sizeof(*p), "MPOOL"); 815 } 816 } 817 #endif 818 819 static void *__sym_calloc_dma(bus_dma_tag_t dev_dmat, int size, char *name) 820 { 821 struct m_pool *mp; 822 void *m = 0; 823 824 /* Lock */ 825 mp = ___get_dma_pool(dev_dmat); 826 if (!mp) 827 mp = ___cre_dma_pool(dev_dmat); 828 if (mp) 829 m = __sym_calloc(mp, size, name); 830 #ifdef MEMO_FREE_UNUSED 831 if (mp && !mp->nump) 832 ___del_dma_pool(mp); 833 #endif 834 /* Unlock */ 835 836 return m; 837 } 838 839 static void 840 __sym_mfree_dma(bus_dma_tag_t dev_dmat, void *m, int size, char *name) 841 { 842 struct m_pool *mp; 843 844 /* Lock */ 845 mp = ___get_dma_pool(dev_dmat); 846 if (mp) 847 __sym_mfree(mp, m, size, name); 848 #ifdef MEMO_FREE_UNUSED 849 if (mp && !mp->nump) 850 ___del_dma_pool(mp); 851 #endif 852 /* Unlock */ 853 } 854 855 static m_addr_t __vtobus(bus_dma_tag_t dev_dmat, void *m) 856 { 857 m_pool_s *mp; 858 int hc = VTOB_HASH_CODE(m); 859 m_vtob_s *vp = 0; 860 m_addr_t a = ((m_addr_t) m) & ~MEMO_CLUSTER_MASK; 861 862 /* Lock */ 863 mp = ___get_dma_pool(dev_dmat); 864 if (mp) { 865 vp = mp->vtob[hc]; 866 while (vp && (m_addr_t) vp->vaddr != a) 867 vp = vp->next; 868 } 869 /* Unlock */ 870 if (!vp) 871 panic("sym: VTOBUS FAILED!\n"); 872 return vp ? vp->baddr + (((m_addr_t) m) - a) : 0; 873 } 874 875 #endif /* FreeBSD_Bus_Dma_Abstraction */ 876 877 /* 878 * Verbs for DMAable memory handling. 879 * The _uvptv_ macro avoids a nasty warning about pointer to volatile 880 * being discarded. 881 */ 882 #define _uvptv_(p) ((void *)((vm_offset_t)(p))) 883 #define _sym_calloc_dma(np, s, n) __sym_calloc_dma(np->bus_dmat, s, n) 884 #define _sym_mfree_dma(np, p, s, n) \ 885 __sym_mfree_dma(np->bus_dmat, _uvptv_(p), s, n) 886 #define sym_calloc_dma(s, n) _sym_calloc_dma(np, s, n) 887 #define sym_mfree_dma(p, s, n) _sym_mfree_dma(np, p, s, n) 888 #define _vtobus(np, p) __vtobus(np->bus_dmat, _uvptv_(p)) 889 #define vtobus(p) _vtobus(np, p) 890 891 892 /* 893 * Print a buffer in hexadecimal format. 894 */ 895 static void sym_printb_hex (u_char *p, int n) 896 { 897 while (n-- > 0) 898 printf (" %x", *p++); 899 } 900 901 /* 902 * Same with a label at beginning and .\n at end. 903 */ 904 static void sym_printl_hex (char *label, u_char *p, int n) 905 { 906 printf ("%s", label); 907 sym_printb_hex (p, n); 908 printf (".\n"); 909 } 910 911 /* 912 * Return a string for SCSI BUS mode. 913 */ 914 static char *sym_scsi_bus_mode(int mode) 915 { 916 switch(mode) { 917 case SMODE_HVD: return "HVD"; 918 case SMODE_SE: return "SE"; 919 case SMODE_LVD: return "LVD"; 920 } 921 return "??"; 922 } 923 924 /* 925 * Some poor and bogus sync table that refers to Tekram NVRAM layout. 926 */ 927 #ifdef SYM_CONF_NVRAM_SUPPORT 928 static u_char Tekram_sync[16] = 929 {25,31,37,43, 50,62,75,125, 12,15,18,21, 6,7,9,10}; 930 #endif 931 932 /* 933 * Union of supported NVRAM formats. 934 */ 935 struct sym_nvram { 936 int type; 937 #define SYM_SYMBIOS_NVRAM (1) 938 #define SYM_TEKRAM_NVRAM (2) 939 #ifdef SYM_CONF_NVRAM_SUPPORT 940 union { 941 Symbios_nvram Symbios; 942 Tekram_nvram Tekram; 943 } data; 944 #endif 945 }; 946 947 /* 948 * This one is hopefully useless, but actually useful. :-) 949 */ 950 #ifndef assert 951 #define assert(expression) { \ 952 if (!(expression)) { \ 953 (void)panic( \ 954 "assertion \"%s\" failed: file \"%s\", line %d\n", \ 955 #expression, \ 956 __FILE__, __LINE__); \ 957 } \ 958 } 959 #endif 960 961 /* 962 * Some provision for a possible big endian mode supported by 963 * Symbios chips (never seen, by the way). 964 * For now, this stuff does not deserve any comments. :) 965 */ 966 967 #define sym_offb(o) (o) 968 #define sym_offw(o) (o) 969 970 /* 971 * Some provision for support for BIG ENDIAN CPU. 972 * Btw, FreeBSD does not seem to be ready yet for big endian. 973 */ 974 975 #if BYTE_ORDER == BIG_ENDIAN 976 #define cpu_to_scr(dw) _htole32(dw) 977 #define scr_to_cpu(dw) _le32toh(dw) 978 #else 979 #define cpu_to_scr(dw) (dw) 980 #define scr_to_cpu(dw) (dw) 981 #endif 982 983 /* 984 * Access to the chip IO registers and on-chip RAM. 985 * We use the `bus space' interface under FreeBSD-4 and 986 * later kernel versions. 987 */ 988 989 #ifdef FreeBSD_Bus_Space_Abstraction 990 991 #if defined(SYM_CONF_IOMAPPED) 992 993 #define INB_OFF(o) bus_space_read_1(np->io_tag, np->io_bsh, o) 994 #define INW_OFF(o) bus_space_read_2(np->io_tag, np->io_bsh, o) 995 #define INL_OFF(o) bus_space_read_4(np->io_tag, np->io_bsh, o) 996 997 #define OUTB_OFF(o, v) bus_space_write_1(np->io_tag, np->io_bsh, o, (v)) 998 #define OUTW_OFF(o, v) bus_space_write_2(np->io_tag, np->io_bsh, o, (v)) 999 #define OUTL_OFF(o, v) bus_space_write_4(np->io_tag, np->io_bsh, o, (v)) 1000 1001 #else /* Memory mapped IO */ 1002 1003 #define INB_OFF(o) bus_space_read_1(np->mmio_tag, np->mmio_bsh, o) 1004 #define INW_OFF(o) bus_space_read_2(np->mmio_tag, np->mmio_bsh, o) 1005 #define INL_OFF(o) bus_space_read_4(np->mmio_tag, np->mmio_bsh, o) 1006 1007 #define OUTB_OFF(o, v) bus_space_write_1(np->mmio_tag, np->mmio_bsh, o, (v)) 1008 #define OUTW_OFF(o, v) bus_space_write_2(np->mmio_tag, np->mmio_bsh, o, (v)) 1009 #define OUTL_OFF(o, v) bus_space_write_4(np->mmio_tag, np->mmio_bsh, o, (v)) 1010 1011 #endif /* SYM_CONF_IOMAPPED */ 1012 1013 #define OUTRAM_OFF(o, a, l) \ 1014 bus_space_write_region_1(np->ram_tag, np->ram_bsh, o, (a), (l)) 1015 1016 #else /* not defined FreeBSD_Bus_Space_Abstraction */ 1017 1018 #if BYTE_ORDER == BIG_ENDIAN 1019 #error "BIG ENDIAN support requires bus space kernel interface" 1020 #endif 1021 1022 /* 1023 * Access to the chip IO registers and on-chip RAM. 1024 * We use legacy MMIO and IO interface for FreeBSD 3.X versions. 1025 */ 1026 1027 /* 1028 * Define some understable verbs for IO and MMIO. 1029 */ 1030 #define io_read8(p) scr_to_cpu(inb((p))) 1031 #define io_read16(p) scr_to_cpu(inw((p))) 1032 #define io_read32(p) scr_to_cpu(inl((p))) 1033 #define io_write8(p, v) outb((p), cpu_to_scr(v)) 1034 #define io_write16(p, v) outw((p), cpu_to_scr(v)) 1035 #define io_write32(p, v) outl((p), cpu_to_scr(v)) 1036 1037 #ifdef __alpha__ 1038 1039 #define mmio_read8(a) readb(a) 1040 #define mmio_read16(a) readw(a) 1041 #define mmio_read32(a) readl(a) 1042 #define mmio_write8(a, b) writeb(a, b) 1043 #define mmio_write16(a, b) writew(a, b) 1044 #define mmio_write32(a, b) writel(a, b) 1045 #define memcpy_to_pci(d, s, n) memcpy_toio((u32)(d), (void *)(s), (n)) 1046 1047 #else /*__i386__, __sparc64__*/ 1048 1049 #define mmio_read8(a) scr_to_cpu((*(volatile unsigned char *) (a))) 1050 #define mmio_read16(a) scr_to_cpu((*(volatile unsigned short *) (a))) 1051 #define mmio_read32(a) scr_to_cpu((*(volatile unsigned int *) (a))) 1052 #define mmio_write8(a, b) (*(volatile unsigned char *) (a)) = cpu_to_scr(b) 1053 #define mmio_write16(a, b) (*(volatile unsigned short *) (a)) = cpu_to_scr(b) 1054 #define mmio_write32(a, b) (*(volatile unsigned int *) (a)) = cpu_to_scr(b) 1055 #define memcpy_to_pci(d, s, n) bcopy((s), (void *)(d), (n)) 1056 1057 #endif 1058 1059 /* 1060 * Normal IO 1061 */ 1062 #if defined(SYM_CONF_IOMAPPED) 1063 1064 #define INB_OFF(o) io_read8(np->io_port + sym_offb(o)) 1065 #define OUTB_OFF(o, v) io_write8(np->io_port + sym_offb(o), (v)) 1066 1067 #define INW_OFF(o) io_read16(np->io_port + sym_offw(o)) 1068 #define OUTW_OFF(o, v) io_write16(np->io_port + sym_offw(o), (v)) 1069 1070 #define INL_OFF(o) io_read32(np->io_port + (o)) 1071 #define OUTL_OFF(o, v) io_write32(np->io_port + (o), (v)) 1072 1073 #else /* Memory mapped IO */ 1074 1075 #define INB_OFF(o) mmio_read8(np->mmio_va + sym_offb(o)) 1076 #define OUTB_OFF(o, v) mmio_write8(np->mmio_va + sym_offb(o), (v)) 1077 1078 #define INW_OFF(o) mmio_read16(np->mmio_va + sym_offw(o)) 1079 #define OUTW_OFF(o, v) mmio_write16(np->mmio_va + sym_offw(o), (v)) 1080 1081 #define INL_OFF(o) mmio_read32(np->mmio_va + (o)) 1082 #define OUTL_OFF(o, v) mmio_write32(np->mmio_va + (o), (v)) 1083 1084 #endif 1085 1086 #define OUTRAM_OFF(o, a, l) memcpy_to_pci(np->ram_va + (o), (a), (l)) 1087 1088 #endif /* FreeBSD_Bus_Space_Abstraction */ 1089 1090 /* 1091 * Common definitions for both bus space and legacy IO methods. 1092 */ 1093 #define INB(r) INB_OFF(offsetof(struct sym_reg,r)) 1094 #define INW(r) INW_OFF(offsetof(struct sym_reg,r)) 1095 #define INL(r) INL_OFF(offsetof(struct sym_reg,r)) 1096 1097 #define OUTB(r, v) OUTB_OFF(offsetof(struct sym_reg,r), (v)) 1098 #define OUTW(r, v) OUTW_OFF(offsetof(struct sym_reg,r), (v)) 1099 #define OUTL(r, v) OUTL_OFF(offsetof(struct sym_reg,r), (v)) 1100 1101 #define OUTONB(r, m) OUTB(r, INB(r) | (m)) 1102 #define OUTOFFB(r, m) OUTB(r, INB(r) & ~(m)) 1103 #define OUTONW(r, m) OUTW(r, INW(r) | (m)) 1104 #define OUTOFFW(r, m) OUTW(r, INW(r) & ~(m)) 1105 #define OUTONL(r, m) OUTL(r, INL(r) | (m)) 1106 #define OUTOFFL(r, m) OUTL(r, INL(r) & ~(m)) 1107 1108 /* 1109 * We normally want the chip to have a consistent view 1110 * of driver internal data structures when we restart it. 1111 * Thus these macros. 1112 */ 1113 #define OUTL_DSP(v) \ 1114 do { \ 1115 MEMORY_BARRIER(); \ 1116 OUTL (nc_dsp, (v)); \ 1117 } while (0) 1118 1119 #define OUTONB_STD() \ 1120 do { \ 1121 MEMORY_BARRIER(); \ 1122 OUTONB (nc_dcntl, (STD|NOCOM)); \ 1123 } while (0) 1124 1125 /* 1126 * Command control block states. 1127 */ 1128 #define HS_IDLE (0) 1129 #define HS_BUSY (1) 1130 #define HS_NEGOTIATE (2) /* sync/wide data transfer*/ 1131 #define HS_DISCONNECT (3) /* Disconnected by target */ 1132 #define HS_WAIT (4) /* waiting for resource */ 1133 1134 #define HS_DONEMASK (0x80) 1135 #define HS_COMPLETE (4|HS_DONEMASK) 1136 #define HS_SEL_TIMEOUT (5|HS_DONEMASK) /* Selection timeout */ 1137 #define HS_UNEXPECTED (6|HS_DONEMASK) /* Unexpected disconnect */ 1138 #define HS_COMP_ERR (7|HS_DONEMASK) /* Completed with error */ 1139 1140 /* 1141 * Software Interrupt Codes 1142 */ 1143 #define SIR_BAD_SCSI_STATUS (1) 1144 #define SIR_SEL_ATN_NO_MSG_OUT (2) 1145 #define SIR_MSG_RECEIVED (3) 1146 #define SIR_MSG_WEIRD (4) 1147 #define SIR_NEGO_FAILED (5) 1148 #define SIR_NEGO_PROTO (6) 1149 #define SIR_SCRIPT_STOPPED (7) 1150 #define SIR_REJECT_TO_SEND (8) 1151 #define SIR_SWIDE_OVERRUN (9) 1152 #define SIR_SODL_UNDERRUN (10) 1153 #define SIR_RESEL_NO_MSG_IN (11) 1154 #define SIR_RESEL_NO_IDENTIFY (12) 1155 #define SIR_RESEL_BAD_LUN (13) 1156 #define SIR_TARGET_SELECTED (14) 1157 #define SIR_RESEL_BAD_I_T_L (15) 1158 #define SIR_RESEL_BAD_I_T_L_Q (16) 1159 #define SIR_ABORT_SENT (17) 1160 #define SIR_RESEL_ABORTED (18) 1161 #define SIR_MSG_OUT_DONE (19) 1162 #define SIR_COMPLETE_ERROR (20) 1163 #define SIR_DATA_OVERRUN (21) 1164 #define SIR_BAD_PHASE (22) 1165 #define SIR_MAX (22) 1166 1167 /* 1168 * Extended error bit codes. 1169 * xerr_status field of struct sym_ccb. 1170 */ 1171 #define XE_EXTRA_DATA (1) /* unexpected data phase */ 1172 #define XE_BAD_PHASE (1<<1) /* illegal phase (4/5) */ 1173 #define XE_PARITY_ERR (1<<2) /* unrecovered SCSI parity error */ 1174 #define XE_SODL_UNRUN (1<<3) /* ODD transfer in DATA OUT phase */ 1175 #define XE_SWIDE_OVRUN (1<<4) /* ODD transfer in DATA IN phase */ 1176 1177 /* 1178 * Negotiation status. 1179 * nego_status field of struct sym_ccb. 1180 */ 1181 #define NS_SYNC (1) 1182 #define NS_WIDE (2) 1183 #define NS_PPR (3) 1184 1185 /* 1186 * A CCB hashed table is used to retrieve CCB address 1187 * from DSA value. 1188 */ 1189 #define CCB_HASH_SHIFT 8 1190 #define CCB_HASH_SIZE (1UL << CCB_HASH_SHIFT) 1191 #define CCB_HASH_MASK (CCB_HASH_SIZE-1) 1192 #define CCB_HASH_CODE(dsa) (((dsa) >> 9) & CCB_HASH_MASK) 1193 1194 /* 1195 * Device flags. 1196 */ 1197 #define SYM_DISC_ENABLED (1) 1198 #define SYM_TAGS_ENABLED (1<<1) 1199 #define SYM_SCAN_BOOT_DISABLED (1<<2) 1200 #define SYM_SCAN_LUNS_DISABLED (1<<3) 1201 1202 /* 1203 * Host adapter miscellaneous flags. 1204 */ 1205 #define SYM_AVOID_BUS_RESET (1) 1206 #define SYM_SCAN_TARGETS_HILO (1<<1) 1207 1208 /* 1209 * Device quirks. 1210 * Some devices, for example the CHEETAH 2 LVD, disconnects without 1211 * saving the DATA POINTER then reselects and terminates the IO. 1212 * On reselection, the automatic RESTORE DATA POINTER makes the 1213 * CURRENT DATA POINTER not point at the end of the IO. 1214 * This behaviour just breaks our calculation of the residual. 1215 * For now, we just force an AUTO SAVE on disconnection and will 1216 * fix that in a further driver version. 1217 */ 1218 #define SYM_QUIRK_AUTOSAVE 1 1219 1220 /* 1221 * Misc. 1222 */ 1223 #define SYM_SNOOP_TIMEOUT (10000000) 1224 #define SYM_PCI_IO PCIR_MAPS 1225 #define SYM_PCI_MMIO (PCIR_MAPS + 4) 1226 #define SYM_PCI_RAM (PCIR_MAPS + 8) 1227 #define SYM_PCI_RAM64 (PCIR_MAPS + 12) 1228 1229 /* 1230 * Back-pointer from the CAM CCB to our data structures. 1231 */ 1232 #define sym_hcb_ptr spriv_ptr0 1233 /* #define sym_ccb_ptr spriv_ptr1 */ 1234 1235 /* 1236 * We mostly have to deal with pointers. 1237 * Thus these typedef's. 1238 */ 1239 typedef struct sym_tcb *tcb_p; 1240 typedef struct sym_lcb *lcb_p; 1241 typedef struct sym_ccb *ccb_p; 1242 typedef struct sym_hcb *hcb_p; 1243 1244 /* 1245 * Gather negotiable parameters value 1246 */ 1247 struct sym_trans { 1248 #ifdef FreeBSD_New_Tran_Settings 1249 u8 scsi_version; 1250 u8 spi_version; 1251 #endif 1252 u8 period; 1253 u8 offset; 1254 u8 width; 1255 u8 options; /* PPR options */ 1256 }; 1257 1258 struct sym_tinfo { 1259 struct sym_trans current; 1260 struct sym_trans goal; 1261 struct sym_trans user; 1262 }; 1263 1264 #define BUS_8_BIT MSG_EXT_WDTR_BUS_8_BIT 1265 #define BUS_16_BIT MSG_EXT_WDTR_BUS_16_BIT 1266 1267 /* 1268 * Global TCB HEADER. 1269 * 1270 * Due to lack of indirect addressing on earlier NCR chips, 1271 * this substructure is copied from the TCB to a global 1272 * address after selection. 1273 * For SYMBIOS chips that support LOAD/STORE this copy is 1274 * not needed and thus not performed. 1275 */ 1276 struct sym_tcbh { 1277 /* 1278 * Scripts bus addresses of LUN table accessed from scripts. 1279 * LUN #0 is a special case, since multi-lun devices are rare, 1280 * and we we want to speed-up the general case and not waste 1281 * resources. 1282 */ 1283 u32 luntbl_sa; /* bus address of this table */ 1284 u32 lun0_sa; /* bus address of LCB #0 */ 1285 /* 1286 * Actual SYNC/WIDE IO registers value for this target. 1287 * 'sval', 'wval' and 'uval' are read from SCRIPTS and 1288 * so have alignment constraints. 1289 */ 1290 /*0*/ u_char uval; /* -> SCNTL4 register */ 1291 /*1*/ u_char sval; /* -> SXFER io register */ 1292 /*2*/ u_char filler1; 1293 /*3*/ u_char wval; /* -> SCNTL3 io register */ 1294 }; 1295 1296 /* 1297 * Target Control Block 1298 */ 1299 struct sym_tcb { 1300 /* 1301 * TCB header. 1302 * Assumed at offset 0. 1303 */ 1304 /*0*/ struct sym_tcbh head; 1305 1306 /* 1307 * LUN table used by the SCRIPTS processor. 1308 * An array of bus addresses is used on reselection. 1309 */ 1310 u32 *luntbl; /* LCBs bus address table */ 1311 1312 /* 1313 * LUN table used by the C code. 1314 */ 1315 lcb_p lun0p; /* LCB of LUN #0 (usual case) */ 1316 #if SYM_CONF_MAX_LUN > 1 1317 lcb_p *lunmp; /* Other LCBs [1..MAX_LUN] */ 1318 #endif 1319 1320 /* 1321 * Bitmap that tells about LUNs that succeeded at least 1322 * 1 IO and therefore assumed to be a real device. 1323 * Avoid useless allocation of the LCB structure. 1324 */ 1325 u32 lun_map[(SYM_CONF_MAX_LUN+31)/32]; 1326 1327 /* 1328 * Bitmap that tells about LUNs that haven't yet an LCB 1329 * allocated (not discovered or LCB allocation failed). 1330 */ 1331 u32 busy0_map[(SYM_CONF_MAX_LUN+31)/32]; 1332 1333 /* 1334 * Transfer capabilities (SIP) 1335 */ 1336 struct sym_tinfo tinfo; 1337 1338 /* 1339 * Keep track of the CCB used for the negotiation in order 1340 * to ensure that only 1 negotiation is queued at a time. 1341 */ 1342 ccb_p nego_cp; /* CCB used for the nego */ 1343 1344 /* 1345 * Set when we want to reset the device. 1346 */ 1347 u_char to_reset; 1348 1349 /* 1350 * Other user settable limits and options. 1351 * These limits are read from the NVRAM if present. 1352 */ 1353 u_char usrflags; 1354 u_short usrtags; 1355 }; 1356 1357 /* 1358 * Global LCB HEADER. 1359 * 1360 * Due to lack of indirect addressing on earlier NCR chips, 1361 * this substructure is copied from the LCB to a global 1362 * address after selection. 1363 * For SYMBIOS chips that support LOAD/STORE this copy is 1364 * not needed and thus not performed. 1365 */ 1366 struct sym_lcbh { 1367 /* 1368 * SCRIPTS address jumped by SCRIPTS on reselection. 1369 * For not probed logical units, this address points to 1370 * SCRIPTS that deal with bad LU handling (must be at 1371 * offset zero of the LCB for that reason). 1372 */ 1373 /*0*/ u32 resel_sa; 1374 1375 /* 1376 * Task (bus address of a CCB) read from SCRIPTS that points 1377 * to the unique ITL nexus allowed to be disconnected. 1378 */ 1379 u32 itl_task_sa; 1380 1381 /* 1382 * Task table bus address (read from SCRIPTS). 1383 */ 1384 u32 itlq_tbl_sa; 1385 }; 1386 1387 /* 1388 * Logical Unit Control Block 1389 */ 1390 struct sym_lcb { 1391 /* 1392 * TCB header. 1393 * Assumed at offset 0. 1394 */ 1395 /*0*/ struct sym_lcbh head; 1396 1397 /* 1398 * Task table read from SCRIPTS that contains pointers to 1399 * ITLQ nexuses. The bus address read from SCRIPTS is 1400 * inside the header. 1401 */ 1402 u32 *itlq_tbl; /* Kernel virtual address */ 1403 1404 /* 1405 * Busy CCBs management. 1406 */ 1407 u_short busy_itlq; /* Number of busy tagged CCBs */ 1408 u_short busy_itl; /* Number of busy untagged CCBs */ 1409 1410 /* 1411 * Circular tag allocation buffer. 1412 */ 1413 u_short ia_tag; /* Tag allocation index */ 1414 u_short if_tag; /* Tag release index */ 1415 u_char *cb_tags; /* Circular tags buffer */ 1416 1417 /* 1418 * Set when we want to clear all tasks. 1419 */ 1420 u_char to_clear; 1421 1422 /* 1423 * Capabilities. 1424 */ 1425 u_char user_flags; 1426 u_char current_flags; 1427 }; 1428 1429 /* 1430 * Action from SCRIPTS on a task. 1431 * Is part of the CCB, but is also used separately to plug 1432 * error handling action to perform from SCRIPTS. 1433 */ 1434 struct sym_actscr { 1435 u32 start; /* Jumped by SCRIPTS after selection */ 1436 u32 restart; /* Jumped by SCRIPTS on relection */ 1437 }; 1438 1439 /* 1440 * Phase mismatch context. 1441 * 1442 * It is part of the CCB and is used as parameters for the 1443 * DATA pointer. We need two contexts to handle correctly the 1444 * SAVED DATA POINTER. 1445 */ 1446 struct sym_pmc { 1447 struct sym_tblmove sg; /* Updated interrupted SG block */ 1448 u32 ret; /* SCRIPT return address */ 1449 }; 1450 1451 /* 1452 * LUN control block lookup. 1453 * We use a direct pointer for LUN #0, and a table of 1454 * pointers which is only allocated for devices that support 1455 * LUN(s) > 0. 1456 */ 1457 #if SYM_CONF_MAX_LUN <= 1 1458 #define sym_lp(np, tp, lun) (!lun) ? (tp)->lun0p : 0 1459 #else 1460 #define sym_lp(np, tp, lun) \ 1461 (!lun) ? (tp)->lun0p : (tp)->lunmp ? (tp)->lunmp[(lun)] : 0 1462 #endif 1463 1464 /* 1465 * Status are used by the host and the script processor. 1466 * 1467 * The last four bytes (status[4]) are copied to the 1468 * scratchb register (declared as scr0..scr3) just after the 1469 * select/reselect, and copied back just after disconnecting. 1470 * Inside the script the XX_REG are used. 1471 */ 1472 1473 /* 1474 * Last four bytes (script) 1475 */ 1476 #define QU_REG scr0 1477 #define HS_REG scr1 1478 #define HS_PRT nc_scr1 1479 #define SS_REG scr2 1480 #define SS_PRT nc_scr2 1481 #define HF_REG scr3 1482 #define HF_PRT nc_scr3 1483 1484 /* 1485 * Last four bytes (host) 1486 */ 1487 #define actualquirks phys.head.status[0] 1488 #define host_status phys.head.status[1] 1489 #define ssss_status phys.head.status[2] 1490 #define host_flags phys.head.status[3] 1491 1492 /* 1493 * Host flags 1494 */ 1495 #define HF_IN_PM0 1u 1496 #define HF_IN_PM1 (1u<<1) 1497 #define HF_ACT_PM (1u<<2) 1498 #define HF_DP_SAVED (1u<<3) 1499 #define HF_SENSE (1u<<4) 1500 #define HF_EXT_ERR (1u<<5) 1501 #define HF_DATA_IN (1u<<6) 1502 #ifdef SYM_CONF_IARB_SUPPORT 1503 #define HF_HINT_IARB (1u<<7) 1504 #endif 1505 1506 /* 1507 * Global CCB HEADER. 1508 * 1509 * Due to lack of indirect addressing on earlier NCR chips, 1510 * this substructure is copied from the ccb to a global 1511 * address after selection (or reselection) and copied back 1512 * before disconnect. 1513 * For SYMBIOS chips that support LOAD/STORE this copy is 1514 * not needed and thus not performed. 1515 */ 1516 1517 struct sym_ccbh { 1518 /* 1519 * Start and restart SCRIPTS addresses (must be at 0). 1520 */ 1521 /*0*/ struct sym_actscr go; 1522 1523 /* 1524 * SCRIPTS jump address that deal with data pointers. 1525 * 'savep' points to the position in the script responsible 1526 * for the actual transfer of data. 1527 * It's written on reception of a SAVE_DATA_POINTER message. 1528 */ 1529 u32 savep; /* Jump address to saved data pointer */ 1530 u32 lastp; /* SCRIPTS address at end of data */ 1531 u32 goalp; /* Not accessed for now from SCRIPTS */ 1532 1533 /* 1534 * Status fields. 1535 */ 1536 u8 status[4]; 1537 }; 1538 1539 /* 1540 * Data Structure Block 1541 * 1542 * During execution of a ccb by the script processor, the 1543 * DSA (data structure address) register points to this 1544 * substructure of the ccb. 1545 */ 1546 struct sym_dsb { 1547 /* 1548 * CCB header. 1549 * Also assumed at offset 0 of the sym_ccb structure. 1550 */ 1551 /*0*/ struct sym_ccbh head; 1552 1553 /* 1554 * Phase mismatch contexts. 1555 * We need two to handle correctly the SAVED DATA POINTER. 1556 * MUST BOTH BE AT OFFSET < 256, due to using 8 bit arithmetic 1557 * for address calculation from SCRIPTS. 1558 */ 1559 struct sym_pmc pm0; 1560 struct sym_pmc pm1; 1561 1562 /* 1563 * Table data for Script 1564 */ 1565 struct sym_tblsel select; 1566 struct sym_tblmove smsg; 1567 struct sym_tblmove smsg_ext; 1568 struct sym_tblmove cmd; 1569 struct sym_tblmove sense; 1570 struct sym_tblmove wresid; 1571 struct sym_tblmove data [SYM_CONF_MAX_SG]; 1572 }; 1573 1574 /* 1575 * Our Command Control Block 1576 */ 1577 struct sym_ccb { 1578 /* 1579 * This is the data structure which is pointed by the DSA 1580 * register when it is executed by the script processor. 1581 * It must be the first entry. 1582 */ 1583 struct sym_dsb phys; 1584 1585 /* 1586 * Pointer to CAM ccb and related stuff. 1587 */ 1588 union ccb *cam_ccb; /* CAM scsiio ccb */ 1589 u8 cdb_buf[16]; /* Copy of CDB */ 1590 u8 *sns_bbuf; /* Bounce buffer for sense data */ 1591 #define SYM_SNS_BBUF_LEN sizeof(struct scsi_sense_data) 1592 int data_len; /* Total data length */ 1593 int segments; /* Number of SG segments */ 1594 1595 /* 1596 * Miscellaneous status'. 1597 */ 1598 u_char nego_status; /* Negotiation status */ 1599 u_char xerr_status; /* Extended error flags */ 1600 u32 extra_bytes; /* Extraneous bytes transferred */ 1601 1602 /* 1603 * Message areas. 1604 * We prepare a message to be sent after selection. 1605 * We may use a second one if the command is rescheduled 1606 * due to CHECK_CONDITION or COMMAND TERMINATED. 1607 * Contents are IDENTIFY and SIMPLE_TAG. 1608 * While negotiating sync or wide transfer, 1609 * a SDTR or WDTR message is appended. 1610 */ 1611 u_char scsi_smsg [12]; 1612 u_char scsi_smsg2[12]; 1613 1614 /* 1615 * Auto request sense related fields. 1616 */ 1617 u_char sensecmd[6]; /* Request Sense command */ 1618 u_char sv_scsi_status; /* Saved SCSI status */ 1619 u_char sv_xerr_status; /* Saved extended status */ 1620 int sv_resid; /* Saved residual */ 1621 1622 /* 1623 * Map for the DMA of user data. 1624 */ 1625 #ifdef FreeBSD_Bus_Dma_Abstraction 1626 void *arg; /* Argument for some callback */ 1627 bus_dmamap_t dmamap; /* DMA map for user data */ 1628 u_char dmamapped; 1629 #define SYM_DMA_NONE 0 1630 #define SYM_DMA_READ 1 1631 #define SYM_DMA_WRITE 2 1632 #endif 1633 /* 1634 * Other fields. 1635 */ 1636 u32 ccb_ba; /* BUS address of this CCB */ 1637 u_short tag; /* Tag for this transfer */ 1638 /* NO_TAG means no tag */ 1639 u_char target; 1640 u_char lun; 1641 ccb_p link_ccbh; /* Host adapter CCB hash chain */ 1642 SYM_QUEHEAD 1643 link_ccbq; /* Link to free/busy CCB queue */ 1644 u32 startp; /* Initial data pointer */ 1645 int ext_sg; /* Extreme data pointer, used */ 1646 int ext_ofs; /* to calculate the residual. */ 1647 u_char to_abort; /* Want this IO to be aborted */ 1648 }; 1649 1650 #define CCB_BA(cp,lbl) (cp->ccb_ba + offsetof(struct sym_ccb, lbl)) 1651 1652 /* 1653 * Host Control Block 1654 */ 1655 struct sym_hcb { 1656 /* 1657 * Global headers. 1658 * Due to poorness of addressing capabilities, earlier 1659 * chips (810, 815, 825) copy part of the data structures 1660 * (CCB, TCB and LCB) in fixed areas. 1661 */ 1662 #ifdef SYM_CONF_GENERIC_SUPPORT 1663 struct sym_ccbh ccb_head; 1664 struct sym_tcbh tcb_head; 1665 struct sym_lcbh lcb_head; 1666 #endif 1667 /* 1668 * Idle task and invalid task actions and 1669 * their bus addresses. 1670 */ 1671 struct sym_actscr idletask, notask, bad_itl, bad_itlq; 1672 vm_offset_t idletask_ba, notask_ba, bad_itl_ba, bad_itlq_ba; 1673 1674 /* 1675 * Dummy lun table to protect us against target 1676 * returning bad lun number on reselection. 1677 */ 1678 u32 *badluntbl; /* Table physical address */ 1679 u32 badlun_sa; /* SCRIPT handler BUS address */ 1680 1681 /* 1682 * Bus address of this host control block. 1683 */ 1684 u32 hcb_ba; 1685 1686 /* 1687 * Bit 32-63 of the on-chip RAM bus address in LE format. 1688 * The START_RAM64 script loads the MMRS and MMWS from this 1689 * field. 1690 */ 1691 u32 scr_ram_seg; 1692 1693 /* 1694 * Chip and controller indentification. 1695 */ 1696 #ifdef FreeBSD_Bus_Io_Abstraction 1697 device_t device; 1698 #else 1699 pcici_t pci_tag; 1700 #endif 1701 int unit; 1702 char inst_name[8]; 1703 1704 /* 1705 * Initial value of some IO register bits. 1706 * These values are assumed to have been set by BIOS, and may 1707 * be used to probe adapter implementation differences. 1708 */ 1709 u_char sv_scntl0, sv_scntl3, sv_dmode, sv_dcntl, sv_ctest3, sv_ctest4, 1710 sv_ctest5, sv_gpcntl, sv_stest2, sv_stest4, sv_scntl4, 1711 sv_stest1; 1712 1713 /* 1714 * Actual initial value of IO register bits used by the 1715 * driver. They are loaded at initialisation according to 1716 * features that are to be enabled/disabled. 1717 */ 1718 u_char rv_scntl0, rv_scntl3, rv_dmode, rv_dcntl, rv_ctest3, rv_ctest4, 1719 rv_ctest5, rv_stest2, rv_ccntl0, rv_ccntl1, rv_scntl4; 1720 1721 /* 1722 * Target data. 1723 */ 1724 struct sym_tcb target[SYM_CONF_MAX_TARGET]; 1725 1726 /* 1727 * Target control block bus address array used by the SCRIPT 1728 * on reselection. 1729 */ 1730 u32 *targtbl; 1731 u32 targtbl_ba; 1732 1733 /* 1734 * CAM SIM information for this instance. 1735 */ 1736 struct cam_sim *sim; 1737 struct cam_path *path; 1738 1739 /* 1740 * Allocated hardware resources. 1741 */ 1742 #ifdef FreeBSD_Bus_Io_Abstraction 1743 struct resource *irq_res; 1744 struct resource *io_res; 1745 struct resource *mmio_res; 1746 struct resource *ram_res; 1747 int ram_id; 1748 void *intr; 1749 #endif 1750 1751 /* 1752 * Bus stuff. 1753 * 1754 * My understanding of PCI is that all agents must share the 1755 * same addressing range and model. 1756 * But some hardware architecture guys provide complex and 1757 * brain-deaded stuff that makes shit. 1758 * This driver only support PCI compliant implementations and 1759 * deals with part of the BUS stuff complexity only to fit O/S 1760 * requirements. 1761 */ 1762 #ifdef FreeBSD_Bus_Io_Abstraction 1763 bus_space_handle_t io_bsh; 1764 bus_space_tag_t io_tag; 1765 bus_space_handle_t mmio_bsh; 1766 bus_space_tag_t mmio_tag; 1767 bus_space_handle_t ram_bsh; 1768 bus_space_tag_t ram_tag; 1769 #endif 1770 1771 /* 1772 * DMA stuff. 1773 */ 1774 #ifdef FreeBSD_Bus_Dma_Abstraction 1775 bus_dma_tag_t bus_dmat; /* DMA tag from parent BUS */ 1776 bus_dma_tag_t data_dmat; /* DMA tag for user data */ 1777 #endif 1778 /* 1779 * Virtual and physical bus addresses of the chip. 1780 */ 1781 vm_offset_t mmio_va; /* MMIO kernel virtual address */ 1782 vm_offset_t mmio_pa; /* MMIO CPU physical address */ 1783 vm_offset_t mmio_ba; /* MMIO BUS address */ 1784 int mmio_ws; /* MMIO Window size */ 1785 1786 vm_offset_t ram_va; /* RAM kernel virtual address */ 1787 vm_offset_t ram_pa; /* RAM CPU physical address */ 1788 vm_offset_t ram_ba; /* RAM BUS address */ 1789 int ram_ws; /* RAM window size */ 1790 u32 io_port; /* IO port address */ 1791 1792 /* 1793 * SCRIPTS virtual and physical bus addresses. 1794 * 'script' is loaded in the on-chip RAM if present. 1795 * 'scripth' stays in main memory for all chips except the 1796 * 53C895A, 53C896 and 53C1010 that provide 8K on-chip RAM. 1797 */ 1798 u_char *scripta0; /* Copies of script and scripth */ 1799 u_char *scriptb0; /* Copies of script and scripth */ 1800 vm_offset_t scripta_ba; /* Actual script and scripth */ 1801 vm_offset_t scriptb_ba; /* bus addresses. */ 1802 vm_offset_t scriptb0_ba; 1803 u_short scripta_sz; /* Actual size of script A */ 1804 u_short scriptb_sz; /* Actual size of script B */ 1805 1806 /* 1807 * Bus addresses, setup and patch methods for 1808 * the selected firmware. 1809 */ 1810 struct sym_fwa_ba fwa_bas; /* Useful SCRIPTA bus addresses */ 1811 struct sym_fwb_ba fwb_bas; /* Useful SCRIPTB bus addresses */ 1812 void (*fw_setup)(hcb_p np, struct sym_fw *fw); 1813 void (*fw_patch)(hcb_p np); 1814 char *fw_name; 1815 1816 /* 1817 * General controller parameters and configuration. 1818 */ 1819 u_short device_id; /* PCI device id */ 1820 u_char revision_id; /* PCI device revision id */ 1821 u_int features; /* Chip features map */ 1822 u_char myaddr; /* SCSI id of the adapter */ 1823 u_char maxburst; /* log base 2 of dwords burst */ 1824 u_char maxwide; /* Maximum transfer width */ 1825 u_char minsync; /* Min sync period factor (ST) */ 1826 u_char maxsync; /* Max sync period factor (ST) */ 1827 u_char maxoffs; /* Max scsi offset (ST) */ 1828 u_char minsync_dt; /* Min sync period factor (DT) */ 1829 u_char maxsync_dt; /* Max sync period factor (DT) */ 1830 u_char maxoffs_dt; /* Max scsi offset (DT) */ 1831 u_char multiplier; /* Clock multiplier (1,2,4) */ 1832 u_char clock_divn; /* Number of clock divisors */ 1833 u32 clock_khz; /* SCSI clock frequency in KHz */ 1834 u32 pciclk_khz; /* Estimated PCI clock in KHz */ 1835 /* 1836 * Start queue management. 1837 * It is filled up by the host processor and accessed by the 1838 * SCRIPTS processor in order to start SCSI commands. 1839 */ 1840 volatile /* Prevent code optimizations */ 1841 u32 *squeue; /* Start queue virtual address */ 1842 u32 squeue_ba; /* Start queue BUS address */ 1843 u_short squeueput; /* Next free slot of the queue */ 1844 u_short actccbs; /* Number of allocated CCBs */ 1845 1846 /* 1847 * Command completion queue. 1848 * It is the same size as the start queue to avoid overflow. 1849 */ 1850 u_short dqueueget; /* Next position to scan */ 1851 volatile /* Prevent code optimizations */ 1852 u32 *dqueue; /* Completion (done) queue */ 1853 u32 dqueue_ba; /* Done queue BUS address */ 1854 1855 /* 1856 * Miscellaneous buffers accessed by the scripts-processor. 1857 * They shall be DWORD aligned, because they may be read or 1858 * written with a script command. 1859 */ 1860 u_char msgout[8]; /* Buffer for MESSAGE OUT */ 1861 u_char msgin [8]; /* Buffer for MESSAGE IN */ 1862 u32 lastmsg; /* Last SCSI message sent */ 1863 u_char scratch; /* Scratch for SCSI receive */ 1864 1865 /* 1866 * Miscellaneous configuration and status parameters. 1867 */ 1868 u_char usrflags; /* Miscellaneous user flags */ 1869 u_char scsi_mode; /* Current SCSI BUS mode */ 1870 u_char verbose; /* Verbosity for this controller*/ 1871 u32 cache; /* Used for cache test at init. */ 1872 1873 /* 1874 * CCB lists and queue. 1875 */ 1876 ccb_p ccbh[CCB_HASH_SIZE]; /* CCB hashed by DSA value */ 1877 SYM_QUEHEAD free_ccbq; /* Queue of available CCBs */ 1878 SYM_QUEHEAD busy_ccbq; /* Queue of busy CCBs */ 1879 1880 /* 1881 * During error handling and/or recovery, 1882 * active CCBs that are to be completed with 1883 * error or requeued are moved from the busy_ccbq 1884 * to the comp_ccbq prior to completion. 1885 */ 1886 SYM_QUEHEAD comp_ccbq; 1887 1888 /* 1889 * CAM CCB pending queue. 1890 */ 1891 SYM_QUEHEAD cam_ccbq; 1892 1893 /* 1894 * IMMEDIATE ARBITRATION (IARB) control. 1895 * 1896 * We keep track in 'last_cp' of the last CCB that has been 1897 * queued to the SCRIPTS processor and clear 'last_cp' when 1898 * this CCB completes. If last_cp is not zero at the moment 1899 * we queue a new CCB, we set a flag in 'last_cp' that is 1900 * used by the SCRIPTS as a hint for setting IARB. 1901 * We donnot set more than 'iarb_max' consecutive hints for 1902 * IARB in order to leave devices a chance to reselect. 1903 * By the way, any non zero value of 'iarb_max' is unfair. :) 1904 */ 1905 #ifdef SYM_CONF_IARB_SUPPORT 1906 u_short iarb_max; /* Max. # consecutive IARB hints*/ 1907 u_short iarb_count; /* Actual # of these hints */ 1908 ccb_p last_cp; 1909 #endif 1910 1911 /* 1912 * Command abort handling. 1913 * We need to synchronize tightly with the SCRIPTS 1914 * processor in order to handle things correctly. 1915 */ 1916 u_char abrt_msg[4]; /* Message to send buffer */ 1917 struct sym_tblmove abrt_tbl; /* Table for the MOV of it */ 1918 struct sym_tblsel abrt_sel; /* Sync params for selection */ 1919 u_char istat_sem; /* Tells the chip to stop (SEM) */ 1920 }; 1921 1922 #define HCB_BA(np, lbl) (np->hcb_ba + offsetof(struct sym_hcb, lbl)) 1923 1924 /* 1925 * Return the name of the controller. 1926 */ 1927 static __inline char *sym_name(hcb_p np) 1928 { 1929 return np->inst_name; 1930 } 1931 1932 /*--------------------------------------------------------------------------*/ 1933 /*------------------------------ FIRMWARES ---------------------------------*/ 1934 /*--------------------------------------------------------------------------*/ 1935 1936 /* 1937 * This stuff will be moved to a separate source file when 1938 * the driver will be broken into several source modules. 1939 */ 1940 1941 /* 1942 * Macros used for all firmwares. 1943 */ 1944 #define SYM_GEN_A(s, label) ((short) offsetof(s, label)), 1945 #define SYM_GEN_B(s, label) ((short) offsetof(s, label)), 1946 #define PADDR_A(label) SYM_GEN_PADDR_A(struct SYM_FWA_SCR, label) 1947 #define PADDR_B(label) SYM_GEN_PADDR_B(struct SYM_FWB_SCR, label) 1948 1949 1950 #ifdef SYM_CONF_GENERIC_SUPPORT 1951 /* 1952 * Allocate firmware #1 script area. 1953 */ 1954 #define SYM_FWA_SCR sym_fw1a_scr 1955 #define SYM_FWB_SCR sym_fw1b_scr 1956 #include <dev/sym/sym_fw1.h> 1957 struct sym_fwa_ofs sym_fw1a_ofs = { 1958 SYM_GEN_FW_A(struct SYM_FWA_SCR) 1959 }; 1960 struct sym_fwb_ofs sym_fw1b_ofs = { 1961 SYM_GEN_FW_B(struct SYM_FWB_SCR) 1962 }; 1963 #undef SYM_FWA_SCR 1964 #undef SYM_FWB_SCR 1965 #endif /* SYM_CONF_GENERIC_SUPPORT */ 1966 1967 /* 1968 * Allocate firmware #2 script area. 1969 */ 1970 #define SYM_FWA_SCR sym_fw2a_scr 1971 #define SYM_FWB_SCR sym_fw2b_scr 1972 #include <dev/sym/sym_fw2.h> 1973 struct sym_fwa_ofs sym_fw2a_ofs = { 1974 SYM_GEN_FW_A(struct SYM_FWA_SCR) 1975 }; 1976 struct sym_fwb_ofs sym_fw2b_ofs = { 1977 SYM_GEN_FW_B(struct SYM_FWB_SCR) 1978 SYM_GEN_B(struct SYM_FWB_SCR, start64) 1979 SYM_GEN_B(struct SYM_FWB_SCR, pm_handle) 1980 }; 1981 #undef SYM_FWA_SCR 1982 #undef SYM_FWB_SCR 1983 1984 #undef SYM_GEN_A 1985 #undef SYM_GEN_B 1986 #undef PADDR_A 1987 #undef PADDR_B 1988 1989 #ifdef SYM_CONF_GENERIC_SUPPORT 1990 /* 1991 * Patch routine for firmware #1. 1992 */ 1993 static void 1994 sym_fw1_patch(hcb_p np) 1995 { 1996 struct sym_fw1a_scr *scripta0; 1997 struct sym_fw1b_scr *scriptb0; 1998 1999 scripta0 = (struct sym_fw1a_scr *) np->scripta0; 2000 scriptb0 = (struct sym_fw1b_scr *) np->scriptb0; 2001 2002 /* 2003 * Remove LED support if not needed. 2004 */ 2005 if (!(np->features & FE_LED0)) { 2006 scripta0->idle[0] = cpu_to_scr(SCR_NO_OP); 2007 scripta0->reselected[0] = cpu_to_scr(SCR_NO_OP); 2008 scripta0->start[0] = cpu_to_scr(SCR_NO_OP); 2009 } 2010 2011 #ifdef SYM_CONF_IARB_SUPPORT 2012 /* 2013 * If user does not want to use IMMEDIATE ARBITRATION 2014 * when we are reselected while attempting to arbitrate, 2015 * patch the SCRIPTS accordingly with a SCRIPT NO_OP. 2016 */ 2017 if (!SYM_CONF_SET_IARB_ON_ARB_LOST) 2018 scripta0->ungetjob[0] = cpu_to_scr(SCR_NO_OP); 2019 #endif 2020 /* 2021 * Patch some data in SCRIPTS. 2022 * - start and done queue initial bus address. 2023 * - target bus address table bus address. 2024 */ 2025 scriptb0->startpos[0] = cpu_to_scr(np->squeue_ba); 2026 scriptb0->done_pos[0] = cpu_to_scr(np->dqueue_ba); 2027 scriptb0->targtbl[0] = cpu_to_scr(np->targtbl_ba); 2028 } 2029 #endif /* SYM_CONF_GENERIC_SUPPORT */ 2030 2031 /* 2032 * Patch routine for firmware #2. 2033 */ 2034 static void 2035 sym_fw2_patch(hcb_p np) 2036 { 2037 struct sym_fw2a_scr *scripta0; 2038 struct sym_fw2b_scr *scriptb0; 2039 2040 scripta0 = (struct sym_fw2a_scr *) np->scripta0; 2041 scriptb0 = (struct sym_fw2b_scr *) np->scriptb0; 2042 2043 /* 2044 * Remove LED support if not needed. 2045 */ 2046 if (!(np->features & FE_LED0)) { 2047 scripta0->idle[0] = cpu_to_scr(SCR_NO_OP); 2048 scripta0->reselected[0] = cpu_to_scr(SCR_NO_OP); 2049 scripta0->start[0] = cpu_to_scr(SCR_NO_OP); 2050 } 2051 2052 #ifdef SYM_CONF_IARB_SUPPORT 2053 /* 2054 * If user does not want to use IMMEDIATE ARBITRATION 2055 * when we are reselected while attempting to arbitrate, 2056 * patch the SCRIPTS accordingly with a SCRIPT NO_OP. 2057 */ 2058 if (!SYM_CONF_SET_IARB_ON_ARB_LOST) 2059 scripta0->ungetjob[0] = cpu_to_scr(SCR_NO_OP); 2060 #endif 2061 /* 2062 * Patch some variable in SCRIPTS. 2063 * - start and done queue initial bus address. 2064 * - target bus address table bus address. 2065 */ 2066 scriptb0->startpos[0] = cpu_to_scr(np->squeue_ba); 2067 scriptb0->done_pos[0] = cpu_to_scr(np->dqueue_ba); 2068 scriptb0->targtbl[0] = cpu_to_scr(np->targtbl_ba); 2069 2070 /* 2071 * Remove the load of SCNTL4 on reselection if not a C10. 2072 */ 2073 if (!(np->features & FE_C10)) { 2074 scripta0->resel_scntl4[0] = cpu_to_scr(SCR_NO_OP); 2075 scripta0->resel_scntl4[1] = cpu_to_scr(0); 2076 } 2077 2078 /* 2079 * Remove a couple of work-arounds specific to C1010 if 2080 * they are not desirable. See `sym_fw2.h' for more details. 2081 */ 2082 if (!(np->device_id == PCI_ID_LSI53C1010_2 && 2083 np->revision_id < 0x1 && 2084 np->pciclk_khz < 60000)) { 2085 scripta0->datao_phase[0] = cpu_to_scr(SCR_NO_OP); 2086 scripta0->datao_phase[1] = cpu_to_scr(0); 2087 } 2088 if (!(np->device_id == PCI_ID_LSI53C1010 && 2089 /* np->revision_id < 0xff */ 1)) { 2090 scripta0->sel_done[0] = cpu_to_scr(SCR_NO_OP); 2091 scripta0->sel_done[1] = cpu_to_scr(0); 2092 } 2093 2094 /* 2095 * Patch some other variables in SCRIPTS. 2096 * These ones are loaded by the SCRIPTS processor. 2097 */ 2098 scriptb0->pm0_data_addr[0] = 2099 cpu_to_scr(np->scripta_ba + 2100 offsetof(struct sym_fw2a_scr, pm0_data)); 2101 scriptb0->pm1_data_addr[0] = 2102 cpu_to_scr(np->scripta_ba + 2103 offsetof(struct sym_fw2a_scr, pm1_data)); 2104 } 2105 2106 /* 2107 * Fill the data area in scripts. 2108 * To be done for all firmwares. 2109 */ 2110 static void 2111 sym_fw_fill_data (u32 *in, u32 *out) 2112 { 2113 int i; 2114 2115 for (i = 0; i < SYM_CONF_MAX_SG; i++) { 2116 *in++ = SCR_CHMOV_TBL ^ SCR_DATA_IN; 2117 *in++ = offsetof (struct sym_dsb, data[i]); 2118 *out++ = SCR_CHMOV_TBL ^ SCR_DATA_OUT; 2119 *out++ = offsetof (struct sym_dsb, data[i]); 2120 } 2121 } 2122 2123 /* 2124 * Setup useful script bus addresses. 2125 * To be done for all firmwares. 2126 */ 2127 static void 2128 sym_fw_setup_bus_addresses(hcb_p np, struct sym_fw *fw) 2129 { 2130 u32 *pa; 2131 u_short *po; 2132 int i; 2133 2134 /* 2135 * Build the bus address table for script A 2136 * from the script A offset table. 2137 */ 2138 po = (u_short *) fw->a_ofs; 2139 pa = (u32 *) &np->fwa_bas; 2140 for (i = 0 ; i < sizeof(np->fwa_bas)/sizeof(u32) ; i++) 2141 pa[i] = np->scripta_ba + po[i]; 2142 2143 /* 2144 * Same for script B. 2145 */ 2146 po = (u_short *) fw->b_ofs; 2147 pa = (u32 *) &np->fwb_bas; 2148 for (i = 0 ; i < sizeof(np->fwb_bas)/sizeof(u32) ; i++) 2149 pa[i] = np->scriptb_ba + po[i]; 2150 } 2151 2152 #ifdef SYM_CONF_GENERIC_SUPPORT 2153 /* 2154 * Setup routine for firmware #1. 2155 */ 2156 static void 2157 sym_fw1_setup(hcb_p np, struct sym_fw *fw) 2158 { 2159 struct sym_fw1a_scr *scripta0; 2160 struct sym_fw1b_scr *scriptb0; 2161 2162 scripta0 = (struct sym_fw1a_scr *) np->scripta0; 2163 scriptb0 = (struct sym_fw1b_scr *) np->scriptb0; 2164 2165 /* 2166 * Fill variable parts in scripts. 2167 */ 2168 sym_fw_fill_data(scripta0->data_in, scripta0->data_out); 2169 2170 /* 2171 * Setup bus addresses used from the C code.. 2172 */ 2173 sym_fw_setup_bus_addresses(np, fw); 2174 } 2175 #endif /* SYM_CONF_GENERIC_SUPPORT */ 2176 2177 /* 2178 * Setup routine for firmware #2. 2179 */ 2180 static void 2181 sym_fw2_setup(hcb_p np, struct sym_fw *fw) 2182 { 2183 struct sym_fw2a_scr *scripta0; 2184 struct sym_fw2b_scr *scriptb0; 2185 2186 scripta0 = (struct sym_fw2a_scr *) np->scripta0; 2187 scriptb0 = (struct sym_fw2b_scr *) np->scriptb0; 2188 2189 /* 2190 * Fill variable parts in scripts. 2191 */ 2192 sym_fw_fill_data(scripta0->data_in, scripta0->data_out); 2193 2194 /* 2195 * Setup bus addresses used from the C code.. 2196 */ 2197 sym_fw_setup_bus_addresses(np, fw); 2198 } 2199 2200 /* 2201 * Allocate firmware descriptors. 2202 */ 2203 #ifdef SYM_CONF_GENERIC_SUPPORT 2204 static struct sym_fw sym_fw1 = SYM_FW_ENTRY(sym_fw1, "NCR-generic"); 2205 #endif /* SYM_CONF_GENERIC_SUPPORT */ 2206 static struct sym_fw sym_fw2 = SYM_FW_ENTRY(sym_fw2, "LOAD/STORE-based"); 2207 2208 /* 2209 * Find the most appropriate firmware for a chip. 2210 */ 2211 static struct sym_fw * 2212 sym_find_firmware(struct sym_pci_chip *chip) 2213 { 2214 if (chip->features & FE_LDSTR) 2215 return &sym_fw2; 2216 #ifdef SYM_CONF_GENERIC_SUPPORT 2217 else if (!(chip->features & (FE_PFEN|FE_NOPM|FE_DAC))) 2218 return &sym_fw1; 2219 #endif 2220 else 2221 return 0; 2222 } 2223 2224 /* 2225 * Bind a script to physical addresses. 2226 */ 2227 static void sym_fw_bind_script (hcb_p np, u32 *start, int len) 2228 { 2229 u32 opcode, new, old, tmp1, tmp2; 2230 u32 *end, *cur; 2231 int relocs; 2232 2233 cur = start; 2234 end = start + len/4; 2235 2236 while (cur < end) { 2237 2238 opcode = *cur; 2239 2240 /* 2241 * If we forget to change the length 2242 * in scripts, a field will be 2243 * padded with 0. This is an illegal 2244 * command. 2245 */ 2246 if (opcode == 0) { 2247 printf ("%s: ERROR0 IN SCRIPT at %d.\n", 2248 sym_name(np), (int) (cur-start)); 2249 MDELAY (10000); 2250 ++cur; 2251 continue; 2252 }; 2253 2254 /* 2255 * We use the bogus value 0xf00ff00f ;-) 2256 * to reserve data area in SCRIPTS. 2257 */ 2258 if (opcode == SCR_DATA_ZERO) { 2259 *cur++ = 0; 2260 continue; 2261 } 2262 2263 if (DEBUG_FLAGS & DEBUG_SCRIPT) 2264 printf ("%d: <%x>\n", (int) (cur-start), 2265 (unsigned)opcode); 2266 2267 /* 2268 * We don't have to decode ALL commands 2269 */ 2270 switch (opcode >> 28) { 2271 case 0xf: 2272 /* 2273 * LOAD / STORE DSA relative, don't relocate. 2274 */ 2275 relocs = 0; 2276 break; 2277 case 0xe: 2278 /* 2279 * LOAD / STORE absolute. 2280 */ 2281 relocs = 1; 2282 break; 2283 case 0xc: 2284 /* 2285 * COPY has TWO arguments. 2286 */ 2287 relocs = 2; 2288 tmp1 = cur[1]; 2289 tmp2 = cur[2]; 2290 if ((tmp1 ^ tmp2) & 3) { 2291 printf ("%s: ERROR1 IN SCRIPT at %d.\n", 2292 sym_name(np), (int) (cur-start)); 2293 MDELAY (10000); 2294 } 2295 /* 2296 * If PREFETCH feature not enabled, remove 2297 * the NO FLUSH bit if present. 2298 */ 2299 if ((opcode & SCR_NO_FLUSH) && 2300 !(np->features & FE_PFEN)) { 2301 opcode = (opcode & ~SCR_NO_FLUSH); 2302 } 2303 break; 2304 case 0x0: 2305 /* 2306 * MOVE/CHMOV (absolute address) 2307 */ 2308 if (!(np->features & FE_WIDE)) 2309 opcode = (opcode | OPC_MOVE); 2310 relocs = 1; 2311 break; 2312 case 0x1: 2313 /* 2314 * MOVE/CHMOV (table indirect) 2315 */ 2316 if (!(np->features & FE_WIDE)) 2317 opcode = (opcode | OPC_MOVE); 2318 relocs = 0; 2319 break; 2320 case 0x8: 2321 /* 2322 * JUMP / CALL 2323 * dont't relocate if relative :-) 2324 */ 2325 if (opcode & 0x00800000) 2326 relocs = 0; 2327 else if ((opcode & 0xf8400000) == 0x80400000)/*JUMP64*/ 2328 relocs = 2; 2329 else 2330 relocs = 1; 2331 break; 2332 case 0x4: 2333 case 0x5: 2334 case 0x6: 2335 case 0x7: 2336 relocs = 1; 2337 break; 2338 default: 2339 relocs = 0; 2340 break; 2341 }; 2342 2343 /* 2344 * Scriptify:) the opcode. 2345 */ 2346 *cur++ = cpu_to_scr(opcode); 2347 2348 /* 2349 * If no relocation, assume 1 argument 2350 * and just scriptize:) it. 2351 */ 2352 if (!relocs) { 2353 *cur = cpu_to_scr(*cur); 2354 ++cur; 2355 continue; 2356 } 2357 2358 /* 2359 * Otherwise performs all needed relocations. 2360 */ 2361 while (relocs--) { 2362 old = *cur; 2363 2364 switch (old & RELOC_MASK) { 2365 case RELOC_REGISTER: 2366 new = (old & ~RELOC_MASK) + np->mmio_ba; 2367 break; 2368 case RELOC_LABEL_A: 2369 new = (old & ~RELOC_MASK) + np->scripta_ba; 2370 break; 2371 case RELOC_LABEL_B: 2372 new = (old & ~RELOC_MASK) + np->scriptb_ba; 2373 break; 2374 case RELOC_SOFTC: 2375 new = (old & ~RELOC_MASK) + np->hcb_ba; 2376 break; 2377 case 0: 2378 /* 2379 * Don't relocate a 0 address. 2380 * They are mostly used for patched or 2381 * script self-modified areas. 2382 */ 2383 if (old == 0) { 2384 new = old; 2385 break; 2386 } 2387 /* fall through */ 2388 default: 2389 new = 0; 2390 panic("sym_fw_bind_script: " 2391 "weird relocation %x\n", old); 2392 break; 2393 } 2394 2395 *cur++ = cpu_to_scr(new); 2396 } 2397 }; 2398 } 2399 2400 /*--------------------------------------------------------------------------*/ 2401 /*--------------------------- END OF FIRMARES -----------------------------*/ 2402 /*--------------------------------------------------------------------------*/ 2403 2404 /* 2405 * Function prototypes. 2406 */ 2407 static void sym_save_initial_setting (hcb_p np); 2408 static int sym_prepare_setting (hcb_p np, struct sym_nvram *nvram); 2409 static int sym_prepare_nego (hcb_p np, ccb_p cp, int nego, u_char *msgptr); 2410 static void sym_put_start_queue (hcb_p np, ccb_p cp); 2411 static void sym_chip_reset (hcb_p np); 2412 static void sym_soft_reset (hcb_p np); 2413 static void sym_start_reset (hcb_p np); 2414 static int sym_reset_scsi_bus (hcb_p np, int enab_int); 2415 static int sym_wakeup_done (hcb_p np); 2416 static void sym_flush_busy_queue (hcb_p np, int cam_status); 2417 static void sym_flush_comp_queue (hcb_p np, int cam_status); 2418 static void sym_init (hcb_p np, int reason); 2419 static int sym_getsync(hcb_p np, u_char dt, u_char sfac, u_char *divp, 2420 u_char *fakp); 2421 static void sym_setsync (hcb_p np, ccb_p cp, u_char ofs, u_char per, 2422 u_char div, u_char fak); 2423 static void sym_setwide (hcb_p np, ccb_p cp, u_char wide); 2424 static void sym_setpprot(hcb_p np, ccb_p cp, u_char dt, u_char ofs, 2425 u_char per, u_char wide, u_char div, u_char fak); 2426 static void sym_settrans(hcb_p np, ccb_p cp, u_char dt, u_char ofs, 2427 u_char per, u_char wide, u_char div, u_char fak); 2428 static void sym_log_hard_error (hcb_p np, u_short sist, u_char dstat); 2429 static void sym_intr (void *arg); 2430 static void sym_poll (struct cam_sim *sim); 2431 static void sym_recover_scsi_int (hcb_p np, u_char hsts); 2432 static void sym_int_sto (hcb_p np); 2433 static void sym_int_udc (hcb_p np); 2434 static void sym_int_sbmc (hcb_p np); 2435 static void sym_int_par (hcb_p np, u_short sist); 2436 static void sym_int_ma (hcb_p np); 2437 static int sym_dequeue_from_squeue(hcb_p np, int i, int target, int lun, 2438 int task); 2439 static void sym_sir_bad_scsi_status (hcb_p np, int num, ccb_p cp); 2440 static int sym_clear_tasks (hcb_p np, int status, int targ, int lun, int task); 2441 static void sym_sir_task_recovery (hcb_p np, int num); 2442 static int sym_evaluate_dp (hcb_p np, ccb_p cp, u32 scr, int *ofs); 2443 static void sym_modify_dp (hcb_p np, tcb_p tp, ccb_p cp, int ofs); 2444 static int sym_compute_residual (hcb_p np, ccb_p cp); 2445 static int sym_show_msg (u_char * msg); 2446 static void sym_print_msg (ccb_p cp, char *label, u_char *msg); 2447 static void sym_sync_nego (hcb_p np, tcb_p tp, ccb_p cp); 2448 static void sym_ppr_nego (hcb_p np, tcb_p tp, ccb_p cp); 2449 static void sym_wide_nego (hcb_p np, tcb_p tp, ccb_p cp); 2450 static void sym_nego_default (hcb_p np, tcb_p tp, ccb_p cp); 2451 static void sym_nego_rejected (hcb_p np, tcb_p tp, ccb_p cp); 2452 static void sym_int_sir (hcb_p np); 2453 static void sym_free_ccb (hcb_p np, ccb_p cp); 2454 static ccb_p sym_get_ccb (hcb_p np, u_char tn, u_char ln, u_char tag_order); 2455 static ccb_p sym_alloc_ccb (hcb_p np); 2456 static ccb_p sym_ccb_from_dsa (hcb_p np, u32 dsa); 2457 static lcb_p sym_alloc_lcb (hcb_p np, u_char tn, u_char ln); 2458 static void sym_alloc_lcb_tags (hcb_p np, u_char tn, u_char ln); 2459 static int sym_snooptest (hcb_p np); 2460 static void sym_selectclock(hcb_p np, u_char scntl3); 2461 static void sym_getclock (hcb_p np, int mult); 2462 static int sym_getpciclock (hcb_p np); 2463 static void sym_complete_ok (hcb_p np, ccb_p cp); 2464 static void sym_complete_error (hcb_p np, ccb_p cp); 2465 static void sym_timeout (void *arg); 2466 static int sym_abort_scsiio (hcb_p np, union ccb *ccb, int timed_out); 2467 static void sym_reset_dev (hcb_p np, union ccb *ccb); 2468 static void sym_action (struct cam_sim *sim, union ccb *ccb); 2469 static void sym_action1 (struct cam_sim *sim, union ccb *ccb); 2470 static int sym_setup_cdb (hcb_p np, struct ccb_scsiio *csio, ccb_p cp); 2471 static void sym_setup_data_and_start (hcb_p np, struct ccb_scsiio *csio, 2472 ccb_p cp); 2473 #ifdef FreeBSD_Bus_Dma_Abstraction 2474 static int sym_fast_scatter_sg_physical(hcb_p np, ccb_p cp, 2475 bus_dma_segment_t *psegs, int nsegs); 2476 #else 2477 static int sym_scatter_virtual (hcb_p np, ccb_p cp, vm_offset_t vaddr, 2478 vm_size_t len); 2479 static int sym_scatter_sg_virtual (hcb_p np, ccb_p cp, 2480 bus_dma_segment_t *psegs, int nsegs); 2481 static int sym_scatter_physical (hcb_p np, ccb_p cp, vm_offset_t paddr, 2482 vm_size_t len); 2483 #endif 2484 static int sym_scatter_sg_physical (hcb_p np, ccb_p cp, 2485 bus_dma_segment_t *psegs, int nsegs); 2486 static void sym_action2 (struct cam_sim *sim, union ccb *ccb); 2487 static void sym_update_trans (hcb_p np, tcb_p tp, struct sym_trans *tip, 2488 struct ccb_trans_settings *cts); 2489 static void sym_update_dflags(hcb_p np, u_char *flags, 2490 struct ccb_trans_settings *cts); 2491 2492 #ifdef FreeBSD_Bus_Io_Abstraction 2493 static struct sym_pci_chip *sym_find_pci_chip (device_t dev); 2494 static int sym_pci_probe (device_t dev); 2495 static int sym_pci_attach (device_t dev); 2496 #else 2497 static struct sym_pci_chip *sym_find_pci_chip (pcici_t tag); 2498 static const char *sym_pci_probe (pcici_t tag, pcidi_t type); 2499 static void sym_pci_attach (pcici_t tag, int unit); 2500 static int sym_pci_attach2 (pcici_t tag, int unit); 2501 #endif 2502 2503 static void sym_pci_free (hcb_p np); 2504 static int sym_cam_attach (hcb_p np); 2505 static void sym_cam_free (hcb_p np); 2506 2507 static void sym_nvram_setup_host (hcb_p np, struct sym_nvram *nvram); 2508 static void sym_nvram_setup_target (hcb_p np, int targ, struct sym_nvram *nvp); 2509 static int sym_read_nvram (hcb_p np, struct sym_nvram *nvp); 2510 2511 /* 2512 * Print something which allows to retrieve the controler type, 2513 * unit, target, lun concerned by a kernel message. 2514 */ 2515 static void PRINT_TARGET (hcb_p np, int target) 2516 { 2517 printf ("%s:%d:", sym_name(np), target); 2518 } 2519 2520 static void PRINT_LUN(hcb_p np, int target, int lun) 2521 { 2522 printf ("%s:%d:%d:", sym_name(np), target, lun); 2523 } 2524 2525 static void PRINT_ADDR (ccb_p cp) 2526 { 2527 if (cp && cp->cam_ccb) 2528 xpt_print_path(cp->cam_ccb->ccb_h.path); 2529 } 2530 2531 /* 2532 * Take into account this ccb in the freeze count. 2533 */ 2534 static void sym_freeze_cam_ccb(union ccb *ccb) 2535 { 2536 if (!(ccb->ccb_h.flags & CAM_DEV_QFRZDIS)) { 2537 if (!(ccb->ccb_h.status & CAM_DEV_QFRZN)) { 2538 ccb->ccb_h.status |= CAM_DEV_QFRZN; 2539 xpt_freeze_devq(ccb->ccb_h.path, 1); 2540 } 2541 } 2542 } 2543 2544 /* 2545 * Set the status field of a CAM CCB. 2546 */ 2547 static __inline void sym_set_cam_status(union ccb *ccb, cam_status status) 2548 { 2549 ccb->ccb_h.status &= ~CAM_STATUS_MASK; 2550 ccb->ccb_h.status |= status; 2551 } 2552 2553 /* 2554 * Get the status field of a CAM CCB. 2555 */ 2556 static __inline int sym_get_cam_status(union ccb *ccb) 2557 { 2558 return ccb->ccb_h.status & CAM_STATUS_MASK; 2559 } 2560 2561 /* 2562 * Enqueue a CAM CCB. 2563 */ 2564 static void sym_enqueue_cam_ccb(hcb_p np, union ccb *ccb) 2565 { 2566 assert(!(ccb->ccb_h.status & CAM_SIM_QUEUED)); 2567 ccb->ccb_h.status = CAM_REQ_INPROG; 2568 2569 ccb->ccb_h.timeout_ch = timeout(sym_timeout, (caddr_t) ccb, 2570 ccb->ccb_h.timeout*hz/1000); 2571 ccb->ccb_h.status |= CAM_SIM_QUEUED; 2572 ccb->ccb_h.sym_hcb_ptr = np; 2573 2574 sym_insque_tail(sym_qptr(&ccb->ccb_h.sim_links), &np->cam_ccbq); 2575 } 2576 2577 /* 2578 * Complete a pending CAM CCB. 2579 */ 2580 static void sym_xpt_done(hcb_p np, union ccb *ccb) 2581 { 2582 if (ccb->ccb_h.status & CAM_SIM_QUEUED) { 2583 untimeout(sym_timeout, (caddr_t) ccb, ccb->ccb_h.timeout_ch); 2584 sym_remque(sym_qptr(&ccb->ccb_h.sim_links)); 2585 ccb->ccb_h.status &= ~CAM_SIM_QUEUED; 2586 ccb->ccb_h.sym_hcb_ptr = 0; 2587 } 2588 if (ccb->ccb_h.flags & CAM_DEV_QFREEZE) 2589 sym_freeze_cam_ccb(ccb); 2590 xpt_done(ccb); 2591 } 2592 2593 static void sym_xpt_done2(hcb_p np, union ccb *ccb, int cam_status) 2594 { 2595 sym_set_cam_status(ccb, cam_status); 2596 sym_xpt_done(np, ccb); 2597 } 2598 2599 /* 2600 * SYMBIOS chip clock divisor table. 2601 * 2602 * Divisors are multiplied by 10,000,000 in order to make 2603 * calculations more simple. 2604 */ 2605 #define _5M 5000000 2606 static u32 div_10M[] = {2*_5M, 3*_5M, 4*_5M, 6*_5M, 8*_5M, 12*_5M, 16*_5M}; 2607 2608 /* 2609 * SYMBIOS chips allow burst lengths of 2, 4, 8, 16, 32, 64, 2610 * 128 transfers. All chips support at least 16 transfers 2611 * bursts. The 825A, 875 and 895 chips support bursts of up 2612 * to 128 transfers and the 895A and 896 support bursts of up 2613 * to 64 transfers. All other chips support up to 16 2614 * transfers bursts. 2615 * 2616 * For PCI 32 bit data transfers each transfer is a DWORD. 2617 * It is a QUADWORD (8 bytes) for PCI 64 bit data transfers. 2618 * 2619 * We use log base 2 (burst length) as internal code, with 2620 * value 0 meaning "burst disabled". 2621 */ 2622 2623 /* 2624 * Burst length from burst code. 2625 */ 2626 #define burst_length(bc) (!(bc))? 0 : 1 << (bc) 2627 2628 /* 2629 * Burst code from io register bits. 2630 */ 2631 #define burst_code(dmode, ctest4, ctest5) \ 2632 (ctest4) & 0x80? 0 : (((dmode) & 0xc0) >> 6) + ((ctest5) & 0x04) + 1 2633 2634 /* 2635 * Set initial io register bits from burst code. 2636 */ 2637 static __inline void sym_init_burst(hcb_p np, u_char bc) 2638 { 2639 np->rv_ctest4 &= ~0x80; 2640 np->rv_dmode &= ~(0x3 << 6); 2641 np->rv_ctest5 &= ~0x4; 2642 2643 if (!bc) { 2644 np->rv_ctest4 |= 0x80; 2645 } 2646 else { 2647 --bc; 2648 np->rv_dmode |= ((bc & 0x3) << 6); 2649 np->rv_ctest5 |= (bc & 0x4); 2650 } 2651 } 2652 2653 2654 /* 2655 * Print out the list of targets that have some flag disabled by user. 2656 */ 2657 static void sym_print_targets_flag(hcb_p np, int mask, char *msg) 2658 { 2659 int cnt; 2660 int i; 2661 2662 for (cnt = 0, i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) { 2663 if (i == np->myaddr) 2664 continue; 2665 if (np->target[i].usrflags & mask) { 2666 if (!cnt++) 2667 printf("%s: %s disabled for targets", 2668 sym_name(np), msg); 2669 printf(" %d", i); 2670 } 2671 } 2672 if (cnt) 2673 printf(".\n"); 2674 } 2675 2676 /* 2677 * Save initial settings of some IO registers. 2678 * Assumed to have been set by BIOS. 2679 * We cannot reset the chip prior to reading the 2680 * IO registers, since informations will be lost. 2681 * Since the SCRIPTS processor may be running, this 2682 * is not safe on paper, but it seems to work quite 2683 * well. :) 2684 */ 2685 static void sym_save_initial_setting (hcb_p np) 2686 { 2687 np->sv_scntl0 = INB(nc_scntl0) & 0x0a; 2688 np->sv_scntl3 = INB(nc_scntl3) & 0x07; 2689 np->sv_dmode = INB(nc_dmode) & 0xce; 2690 np->sv_dcntl = INB(nc_dcntl) & 0xa8; 2691 np->sv_ctest3 = INB(nc_ctest3) & 0x01; 2692 np->sv_ctest4 = INB(nc_ctest4) & 0x80; 2693 np->sv_gpcntl = INB(nc_gpcntl); 2694 np->sv_stest1 = INB(nc_stest1); 2695 np->sv_stest2 = INB(nc_stest2) & 0x20; 2696 np->sv_stest4 = INB(nc_stest4); 2697 if (np->features & FE_C10) { /* Always large DMA fifo + ultra3 */ 2698 np->sv_scntl4 = INB(nc_scntl4); 2699 np->sv_ctest5 = INB(nc_ctest5) & 0x04; 2700 } 2701 else 2702 np->sv_ctest5 = INB(nc_ctest5) & 0x24; 2703 } 2704 2705 /* 2706 * Prepare io register values used by sym_init() according 2707 * to selected and supported features. 2708 */ 2709 static int sym_prepare_setting(hcb_p np, struct sym_nvram *nvram) 2710 { 2711 u_char burst_max; 2712 u32 period; 2713 int i; 2714 2715 /* 2716 * Wide ? 2717 */ 2718 np->maxwide = (np->features & FE_WIDE)? 1 : 0; 2719 2720 /* 2721 * Get the frequency of the chip's clock. 2722 */ 2723 if (np->features & FE_QUAD) 2724 np->multiplier = 4; 2725 else if (np->features & FE_DBLR) 2726 np->multiplier = 2; 2727 else 2728 np->multiplier = 1; 2729 2730 np->clock_khz = (np->features & FE_CLK80)? 80000 : 40000; 2731 np->clock_khz *= np->multiplier; 2732 2733 if (np->clock_khz != 40000) 2734 sym_getclock(np, np->multiplier); 2735 2736 /* 2737 * Divisor to be used for async (timer pre-scaler). 2738 */ 2739 i = np->clock_divn - 1; 2740 while (--i >= 0) { 2741 if (10ul * SYM_CONF_MIN_ASYNC * np->clock_khz > div_10M[i]) { 2742 ++i; 2743 break; 2744 } 2745 } 2746 np->rv_scntl3 = i+1; 2747 2748 /* 2749 * The C1010 uses hardwired divisors for async. 2750 * So, we just throw away, the async. divisor.:-) 2751 */ 2752 if (np->features & FE_C10) 2753 np->rv_scntl3 = 0; 2754 2755 /* 2756 * Minimum synchronous period factor supported by the chip. 2757 * Btw, 'period' is in tenths of nanoseconds. 2758 */ 2759 period = (4 * div_10M[0] + np->clock_khz - 1) / np->clock_khz; 2760 if (period <= 250) np->minsync = 10; 2761 else if (period <= 303) np->minsync = 11; 2762 else if (period <= 500) np->minsync = 12; 2763 else np->minsync = (period + 40 - 1) / 40; 2764 2765 /* 2766 * Check against chip SCSI standard support (SCSI-2,ULTRA,ULTRA2). 2767 */ 2768 if (np->minsync < 25 && 2769 !(np->features & (FE_ULTRA|FE_ULTRA2|FE_ULTRA3))) 2770 np->minsync = 25; 2771 else if (np->minsync < 12 && 2772 !(np->features & (FE_ULTRA2|FE_ULTRA3))) 2773 np->minsync = 12; 2774 2775 /* 2776 * Maximum synchronous period factor supported by the chip. 2777 */ 2778 period = (11 * div_10M[np->clock_divn - 1]) / (4 * np->clock_khz); 2779 np->maxsync = period > 2540 ? 254 : period / 10; 2780 2781 /* 2782 * If chip is a C1010, guess the sync limits in DT mode. 2783 */ 2784 if ((np->features & (FE_C10|FE_ULTRA3)) == (FE_C10|FE_ULTRA3)) { 2785 if (np->clock_khz == 160000) { 2786 np->minsync_dt = 9; 2787 np->maxsync_dt = 50; 2788 np->maxoffs_dt = 62; 2789 } 2790 } 2791 2792 /* 2793 * 64 bit addressing (895A/896/1010) ? 2794 */ 2795 if (np->features & FE_DAC) 2796 #if BITS_PER_LONG > 32 2797 np->rv_ccntl1 |= (XTIMOD | EXTIBMV); 2798 #else 2799 np->rv_ccntl1 |= (DDAC); 2800 #endif 2801 2802 /* 2803 * Phase mismatch handled by SCRIPTS (895A/896/1010) ? 2804 */ 2805 if (np->features & FE_NOPM) 2806 np->rv_ccntl0 |= (ENPMJ); 2807 2808 /* 2809 * C1010 Errata. 2810 * In dual channel mode, contention occurs if internal cycles 2811 * are used. Disable internal cycles. 2812 */ 2813 if (np->device_id == PCI_ID_LSI53C1010 && 2814 np->revision_id < 0x2) 2815 np->rv_ccntl0 |= DILS; 2816 2817 /* 2818 * Select burst length (dwords) 2819 */ 2820 burst_max = SYM_SETUP_BURST_ORDER; 2821 if (burst_max == 255) 2822 burst_max = burst_code(np->sv_dmode, np->sv_ctest4, 2823 np->sv_ctest5); 2824 if (burst_max > 7) 2825 burst_max = 7; 2826 if (burst_max > np->maxburst) 2827 burst_max = np->maxburst; 2828 2829 /* 2830 * DEL 352 - 53C810 Rev x11 - Part Number 609-0392140 - ITEM 2. 2831 * This chip and the 860 Rev 1 may wrongly use PCI cache line 2832 * based transactions on LOAD/STORE instructions. So we have 2833 * to prevent these chips from using such PCI transactions in 2834 * this driver. The generic ncr driver that does not use 2835 * LOAD/STORE instructions does not need this work-around. 2836 */ 2837 if ((np->device_id == PCI_ID_SYM53C810 && 2838 np->revision_id >= 0x10 && np->revision_id <= 0x11) || 2839 (np->device_id == PCI_ID_SYM53C860 && 2840 np->revision_id <= 0x1)) 2841 np->features &= ~(FE_WRIE|FE_ERL|FE_ERMP); 2842 2843 /* 2844 * Select all supported special features. 2845 * If we are using on-board RAM for scripts, prefetch (PFEN) 2846 * does not help, but burst op fetch (BOF) does. 2847 * Disabling PFEN makes sure BOF will be used. 2848 */ 2849 if (np->features & FE_ERL) 2850 np->rv_dmode |= ERL; /* Enable Read Line */ 2851 if (np->features & FE_BOF) 2852 np->rv_dmode |= BOF; /* Burst Opcode Fetch */ 2853 if (np->features & FE_ERMP) 2854 np->rv_dmode |= ERMP; /* Enable Read Multiple */ 2855 #if 1 2856 if ((np->features & FE_PFEN) && !np->ram_ba) 2857 #else 2858 if (np->features & FE_PFEN) 2859 #endif 2860 np->rv_dcntl |= PFEN; /* Prefetch Enable */ 2861 if (np->features & FE_CLSE) 2862 np->rv_dcntl |= CLSE; /* Cache Line Size Enable */ 2863 if (np->features & FE_WRIE) 2864 np->rv_ctest3 |= WRIE; /* Write and Invalidate */ 2865 if (np->features & FE_DFS) 2866 np->rv_ctest5 |= DFS; /* Dma Fifo Size */ 2867 2868 /* 2869 * Select some other 2870 */ 2871 if (SYM_SETUP_PCI_PARITY) 2872 np->rv_ctest4 |= MPEE; /* Master parity checking */ 2873 if (SYM_SETUP_SCSI_PARITY) 2874 np->rv_scntl0 |= 0x0a; /* full arb., ena parity, par->ATN */ 2875 2876 /* 2877 * Get parity checking, host ID and verbose mode from NVRAM 2878 */ 2879 np->myaddr = 255; 2880 sym_nvram_setup_host (np, nvram); 2881 2882 /* 2883 * Get SCSI addr of host adapter (set by bios?). 2884 */ 2885 if (np->myaddr == 255) { 2886 np->myaddr = INB(nc_scid) & 0x07; 2887 if (!np->myaddr) 2888 np->myaddr = SYM_SETUP_HOST_ID; 2889 } 2890 2891 /* 2892 * Prepare initial io register bits for burst length 2893 */ 2894 sym_init_burst(np, burst_max); 2895 2896 /* 2897 * Set SCSI BUS mode. 2898 * - LVD capable chips (895/895A/896/1010) report the 2899 * current BUS mode through the STEST4 IO register. 2900 * - For previous generation chips (825/825A/875), 2901 * user has to tell us how to check against HVD, 2902 * since a 100% safe algorithm is not possible. 2903 */ 2904 np->scsi_mode = SMODE_SE; 2905 if (np->features & (FE_ULTRA2|FE_ULTRA3)) 2906 np->scsi_mode = (np->sv_stest4 & SMODE); 2907 else if (np->features & FE_DIFF) { 2908 if (SYM_SETUP_SCSI_DIFF == 1) { 2909 if (np->sv_scntl3) { 2910 if (np->sv_stest2 & 0x20) 2911 np->scsi_mode = SMODE_HVD; 2912 } 2913 else if (nvram->type == SYM_SYMBIOS_NVRAM) { 2914 if (!(INB(nc_gpreg) & 0x08)) 2915 np->scsi_mode = SMODE_HVD; 2916 } 2917 } 2918 else if (SYM_SETUP_SCSI_DIFF == 2) 2919 np->scsi_mode = SMODE_HVD; 2920 } 2921 if (np->scsi_mode == SMODE_HVD) 2922 np->rv_stest2 |= 0x20; 2923 2924 /* 2925 * Set LED support from SCRIPTS. 2926 * Ignore this feature for boards known to use a 2927 * specific GPIO wiring and for the 895A, 896 2928 * and 1010 that drive the LED directly. 2929 */ 2930 if ((SYM_SETUP_SCSI_LED || 2931 (nvram->type == SYM_SYMBIOS_NVRAM || 2932 (nvram->type == SYM_TEKRAM_NVRAM && 2933 np->device_id == PCI_ID_SYM53C895))) && 2934 !(np->features & FE_LEDC) && !(np->sv_gpcntl & 0x01)) 2935 np->features |= FE_LED0; 2936 2937 /* 2938 * Set irq mode. 2939 */ 2940 switch(SYM_SETUP_IRQ_MODE & 3) { 2941 case 2: 2942 np->rv_dcntl |= IRQM; 2943 break; 2944 case 1: 2945 np->rv_dcntl |= (np->sv_dcntl & IRQM); 2946 break; 2947 default: 2948 break; 2949 } 2950 2951 /* 2952 * Configure targets according to driver setup. 2953 * If NVRAM present get targets setup from NVRAM. 2954 */ 2955 for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) { 2956 tcb_p tp = &np->target[i]; 2957 2958 #ifdef FreeBSD_New_Tran_Settings 2959 tp->tinfo.user.scsi_version = tp->tinfo.current.scsi_version= 2; 2960 tp->tinfo.user.spi_version = tp->tinfo.current.spi_version = 2; 2961 #endif 2962 tp->tinfo.user.period = np->minsync; 2963 tp->tinfo.user.offset = np->maxoffs; 2964 tp->tinfo.user.width = np->maxwide ? BUS_16_BIT : BUS_8_BIT; 2965 tp->usrflags |= (SYM_DISC_ENABLED | SYM_TAGS_ENABLED); 2966 tp->usrtags = SYM_SETUP_MAX_TAG; 2967 2968 sym_nvram_setup_target (np, i, nvram); 2969 2970 /* 2971 * For now, guess PPR/DT support from the period 2972 * and BUS width. 2973 */ 2974 if (np->features & FE_ULTRA3) { 2975 if (tp->tinfo.user.period <= 9 && 2976 tp->tinfo.user.width == BUS_16_BIT) { 2977 tp->tinfo.user.options |= PPR_OPT_DT; 2978 tp->tinfo.user.offset = np->maxoffs_dt; 2979 #ifdef FreeBSD_New_Tran_Settings 2980 tp->tinfo.user.spi_version = 3; 2981 #endif 2982 } 2983 } 2984 2985 if (!tp->usrtags) 2986 tp->usrflags &= ~SYM_TAGS_ENABLED; 2987 } 2988 2989 /* 2990 * Let user know about the settings. 2991 */ 2992 i = nvram->type; 2993 printf("%s: %s NVRAM, ID %d, Fast-%d, %s, %s\n", sym_name(np), 2994 i == SYM_SYMBIOS_NVRAM ? "Symbios" : 2995 (i == SYM_TEKRAM_NVRAM ? "Tekram" : "No"), 2996 np->myaddr, 2997 (np->features & FE_ULTRA3) ? 80 : 2998 (np->features & FE_ULTRA2) ? 40 : 2999 (np->features & FE_ULTRA) ? 20 : 10, 3000 sym_scsi_bus_mode(np->scsi_mode), 3001 (np->rv_scntl0 & 0xa) ? "parity checking" : "NO parity"); 3002 /* 3003 * Tell him more on demand. 3004 */ 3005 if (sym_verbose) { 3006 printf("%s: %s IRQ line driver%s\n", 3007 sym_name(np), 3008 np->rv_dcntl & IRQM ? "totem pole" : "open drain", 3009 np->ram_ba ? ", using on-chip SRAM" : ""); 3010 printf("%s: using %s firmware.\n", sym_name(np), np->fw_name); 3011 if (np->features & FE_NOPM) 3012 printf("%s: handling phase mismatch from SCRIPTS.\n", 3013 sym_name(np)); 3014 } 3015 /* 3016 * And still more. 3017 */ 3018 if (sym_verbose > 1) { 3019 printf ("%s: initial SCNTL3/DMODE/DCNTL/CTEST3/4/5 = " 3020 "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n", 3021 sym_name(np), np->sv_scntl3, np->sv_dmode, np->sv_dcntl, 3022 np->sv_ctest3, np->sv_ctest4, np->sv_ctest5); 3023 3024 printf ("%s: final SCNTL3/DMODE/DCNTL/CTEST3/4/5 = " 3025 "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n", 3026 sym_name(np), np->rv_scntl3, np->rv_dmode, np->rv_dcntl, 3027 np->rv_ctest3, np->rv_ctest4, np->rv_ctest5); 3028 } 3029 /* 3030 * Let user be aware of targets that have some disable flags set. 3031 */ 3032 sym_print_targets_flag(np, SYM_SCAN_BOOT_DISABLED, "SCAN AT BOOT"); 3033 if (sym_verbose) 3034 sym_print_targets_flag(np, SYM_SCAN_LUNS_DISABLED, 3035 "SCAN FOR LUNS"); 3036 3037 return 0; 3038 } 3039 3040 /* 3041 * Prepare the next negotiation message if needed. 3042 * 3043 * Fill in the part of message buffer that contains the 3044 * negotiation and the nego_status field of the CCB. 3045 * Returns the size of the message in bytes. 3046 */ 3047 3048 static int sym_prepare_nego(hcb_p np, ccb_p cp, int nego, u_char *msgptr) 3049 { 3050 tcb_p tp = &np->target[cp->target]; 3051 int msglen = 0; 3052 3053 /* 3054 * Early C1010 chips need a work-around for DT 3055 * data transfer to work. 3056 */ 3057 if (!(np->features & FE_U3EN)) 3058 tp->tinfo.goal.options = 0; 3059 /* 3060 * negotiate using PPR ? 3061 */ 3062 if (tp->tinfo.goal.options & PPR_OPT_MASK) 3063 nego = NS_PPR; 3064 /* 3065 * negotiate wide transfers ? 3066 */ 3067 else if (tp->tinfo.current.width != tp->tinfo.goal.width) 3068 nego = NS_WIDE; 3069 /* 3070 * negotiate synchronous transfers? 3071 */ 3072 else if (tp->tinfo.current.period != tp->tinfo.goal.period || 3073 tp->tinfo.current.offset != tp->tinfo.goal.offset) 3074 nego = NS_SYNC; 3075 3076 switch (nego) { 3077 case NS_SYNC: 3078 msgptr[msglen++] = M_EXTENDED; 3079 msgptr[msglen++] = 3; 3080 msgptr[msglen++] = M_X_SYNC_REQ; 3081 msgptr[msglen++] = tp->tinfo.goal.period; 3082 msgptr[msglen++] = tp->tinfo.goal.offset; 3083 break; 3084 case NS_WIDE: 3085 msgptr[msglen++] = M_EXTENDED; 3086 msgptr[msglen++] = 2; 3087 msgptr[msglen++] = M_X_WIDE_REQ; 3088 msgptr[msglen++] = tp->tinfo.goal.width; 3089 break; 3090 case NS_PPR: 3091 msgptr[msglen++] = M_EXTENDED; 3092 msgptr[msglen++] = 6; 3093 msgptr[msglen++] = M_X_PPR_REQ; 3094 msgptr[msglen++] = tp->tinfo.goal.period; 3095 msgptr[msglen++] = 0; 3096 msgptr[msglen++] = tp->tinfo.goal.offset; 3097 msgptr[msglen++] = tp->tinfo.goal.width; 3098 msgptr[msglen++] = tp->tinfo.goal.options & PPR_OPT_DT; 3099 break; 3100 }; 3101 3102 cp->nego_status = nego; 3103 3104 if (nego) { 3105 tp->nego_cp = cp; /* Keep track a nego will be performed */ 3106 if (DEBUG_FLAGS & DEBUG_NEGO) { 3107 sym_print_msg(cp, nego == NS_SYNC ? "sync msgout" : 3108 nego == NS_WIDE ? "wide msgout" : 3109 "ppr msgout", msgptr); 3110 }; 3111 }; 3112 3113 return msglen; 3114 } 3115 3116 /* 3117 * Insert a job into the start queue. 3118 */ 3119 static void sym_put_start_queue(hcb_p np, ccb_p cp) 3120 { 3121 u_short qidx; 3122 3123 #ifdef SYM_CONF_IARB_SUPPORT 3124 /* 3125 * If the previously queued CCB is not yet done, 3126 * set the IARB hint. The SCRIPTS will go with IARB 3127 * for this job when starting the previous one. 3128 * We leave devices a chance to win arbitration by 3129 * not using more than 'iarb_max' consecutive 3130 * immediate arbitrations. 3131 */ 3132 if (np->last_cp && np->iarb_count < np->iarb_max) { 3133 np->last_cp->host_flags |= HF_HINT_IARB; 3134 ++np->iarb_count; 3135 } 3136 else 3137 np->iarb_count = 0; 3138 np->last_cp = cp; 3139 #endif 3140 3141 /* 3142 * Insert first the idle task and then our job. 3143 * The MB should ensure proper ordering. 3144 */ 3145 qidx = np->squeueput + 2; 3146 if (qidx >= MAX_QUEUE*2) qidx = 0; 3147 3148 np->squeue [qidx] = cpu_to_scr(np->idletask_ba); 3149 MEMORY_BARRIER(); 3150 np->squeue [np->squeueput] = cpu_to_scr(cp->ccb_ba); 3151 3152 np->squeueput = qidx; 3153 3154 if (DEBUG_FLAGS & DEBUG_QUEUE) 3155 printf ("%s: queuepos=%d.\n", sym_name (np), np->squeueput); 3156 3157 /* 3158 * Script processor may be waiting for reselect. 3159 * Wake it up. 3160 */ 3161 MEMORY_BARRIER(); 3162 OUTB (nc_istat, SIGP|np->istat_sem); 3163 } 3164 3165 3166 /* 3167 * Soft reset the chip. 3168 * 3169 * Raising SRST when the chip is running may cause 3170 * problems on dual function chips (see below). 3171 * On the other hand, LVD devices need some delay 3172 * to settle and report actual BUS mode in STEST4. 3173 */ 3174 static void sym_chip_reset (hcb_p np) 3175 { 3176 OUTB (nc_istat, SRST); 3177 UDELAY (10); 3178 OUTB (nc_istat, 0); 3179 UDELAY(2000); /* For BUS MODE to settle */ 3180 } 3181 3182 /* 3183 * Soft reset the chip. 3184 * 3185 * Some 896 and 876 chip revisions may hang-up if we set 3186 * the SRST (soft reset) bit at the wrong time when SCRIPTS 3187 * are running. 3188 * So, we need to abort the current operation prior to 3189 * soft resetting the chip. 3190 */ 3191 static void sym_soft_reset (hcb_p np) 3192 { 3193 u_char istat; 3194 int i; 3195 3196 OUTB (nc_istat, CABRT); 3197 for (i = 1000000 ; i ; --i) { 3198 istat = INB (nc_istat); 3199 if (istat & SIP) { 3200 INW (nc_sist); 3201 continue; 3202 } 3203 if (istat & DIP) { 3204 OUTB (nc_istat, 0); 3205 INB (nc_dstat); 3206 break; 3207 } 3208 } 3209 if (!i) 3210 printf("%s: unable to abort current chip operation.\n", 3211 sym_name(np)); 3212 sym_chip_reset (np); 3213 } 3214 3215 /* 3216 * Start reset process. 3217 * 3218 * The interrupt handler will reinitialize the chip. 3219 */ 3220 static void sym_start_reset(hcb_p np) 3221 { 3222 (void) sym_reset_scsi_bus(np, 1); 3223 } 3224 3225 static int sym_reset_scsi_bus(hcb_p np, int enab_int) 3226 { 3227 u32 term; 3228 int retv = 0; 3229 3230 sym_soft_reset(np); /* Soft reset the chip */ 3231 if (enab_int) 3232 OUTW (nc_sien, RST); 3233 /* 3234 * Enable Tolerant, reset IRQD if present and 3235 * properly set IRQ mode, prior to resetting the bus. 3236 */ 3237 OUTB (nc_stest3, TE); 3238 OUTB (nc_dcntl, (np->rv_dcntl & IRQM)); 3239 OUTB (nc_scntl1, CRST); 3240 UDELAY (200); 3241 3242 if (!SYM_SETUP_SCSI_BUS_CHECK) 3243 goto out; 3244 /* 3245 * Check for no terminators or SCSI bus shorts to ground. 3246 * Read SCSI data bus, data parity bits and control signals. 3247 * We are expecting RESET to be TRUE and other signals to be 3248 * FALSE. 3249 */ 3250 term = INB(nc_sstat0); 3251 term = ((term & 2) << 7) + ((term & 1) << 17); /* rst sdp0 */ 3252 term |= ((INB(nc_sstat2) & 0x01) << 26) | /* sdp1 */ 3253 ((INW(nc_sbdl) & 0xff) << 9) | /* d7-0 */ 3254 ((INW(nc_sbdl) & 0xff00) << 10) | /* d15-8 */ 3255 INB(nc_sbcl); /* req ack bsy sel atn msg cd io */ 3256 3257 if (!(np->features & FE_WIDE)) 3258 term &= 0x3ffff; 3259 3260 if (term != (2<<7)) { 3261 printf("%s: suspicious SCSI data while resetting the BUS.\n", 3262 sym_name(np)); 3263 printf("%s: %sdp0,d7-0,rst,req,ack,bsy,sel,atn,msg,c/d,i/o = " 3264 "0x%lx, expecting 0x%lx\n", 3265 sym_name(np), 3266 (np->features & FE_WIDE) ? "dp1,d15-8," : "", 3267 (u_long)term, (u_long)(2<<7)); 3268 if (SYM_SETUP_SCSI_BUS_CHECK == 1) 3269 retv = 1; 3270 } 3271 out: 3272 OUTB (nc_scntl1, 0); 3273 /* MDELAY(100); */ 3274 return retv; 3275 } 3276 3277 /* 3278 * The chip may have completed jobs. Look at the DONE QUEUE. 3279 * 3280 * On architectures that may reorder LOAD/STORE operations, 3281 * a memory barrier may be needed after the reading of the 3282 * so-called `flag' and prior to dealing with the data. 3283 */ 3284 static int sym_wakeup_done (hcb_p np) 3285 { 3286 ccb_p cp; 3287 int i, n; 3288 u32 dsa; 3289 3290 n = 0; 3291 i = np->dqueueget; 3292 while (1) { 3293 dsa = scr_to_cpu(np->dqueue[i]); 3294 if (!dsa) 3295 break; 3296 np->dqueue[i] = 0; 3297 if ((i = i+2) >= MAX_QUEUE*2) 3298 i = 0; 3299 3300 cp = sym_ccb_from_dsa(np, dsa); 3301 if (cp) { 3302 MEMORY_BARRIER(); 3303 sym_complete_ok (np, cp); 3304 ++n; 3305 } 3306 else 3307 printf ("%s: bad DSA (%x) in done queue.\n", 3308 sym_name(np), (u_int) dsa); 3309 } 3310 np->dqueueget = i; 3311 3312 return n; 3313 } 3314 3315 /* 3316 * Complete all active CCBs with error. 3317 * Used on CHIP/SCSI RESET. 3318 */ 3319 static void sym_flush_busy_queue (hcb_p np, int cam_status) 3320 { 3321 /* 3322 * Move all active CCBs to the COMP queue 3323 * and flush this queue. 3324 */ 3325 sym_que_splice(&np->busy_ccbq, &np->comp_ccbq); 3326 sym_que_init(&np->busy_ccbq); 3327 sym_flush_comp_queue(np, cam_status); 3328 } 3329 3330 /* 3331 * Start chip. 3332 * 3333 * 'reason' means: 3334 * 0: initialisation. 3335 * 1: SCSI BUS RESET delivered or received. 3336 * 2: SCSI BUS MODE changed. 3337 */ 3338 static void sym_init (hcb_p np, int reason) 3339 { 3340 int i; 3341 u32 phys; 3342 3343 /* 3344 * Reset chip if asked, otherwise just clear fifos. 3345 */ 3346 if (reason == 1) 3347 sym_soft_reset(np); 3348 else { 3349 OUTB (nc_stest3, TE|CSF); 3350 OUTONB (nc_ctest3, CLF); 3351 } 3352 3353 /* 3354 * Clear Start Queue 3355 */ 3356 phys = np->squeue_ba; 3357 for (i = 0; i < MAX_QUEUE*2; i += 2) { 3358 np->squeue[i] = cpu_to_scr(np->idletask_ba); 3359 np->squeue[i+1] = cpu_to_scr(phys + (i+2)*4); 3360 } 3361 np->squeue[MAX_QUEUE*2-1] = cpu_to_scr(phys); 3362 3363 /* 3364 * Start at first entry. 3365 */ 3366 np->squeueput = 0; 3367 3368 /* 3369 * Clear Done Queue 3370 */ 3371 phys = np->dqueue_ba; 3372 for (i = 0; i < MAX_QUEUE*2; i += 2) { 3373 np->dqueue[i] = 0; 3374 np->dqueue[i+1] = cpu_to_scr(phys + (i+2)*4); 3375 } 3376 np->dqueue[MAX_QUEUE*2-1] = cpu_to_scr(phys); 3377 3378 /* 3379 * Start at first entry. 3380 */ 3381 np->dqueueget = 0; 3382 3383 /* 3384 * Install patches in scripts. 3385 * This also let point to first position the start 3386 * and done queue pointers used from SCRIPTS. 3387 */ 3388 np->fw_patch(np); 3389 3390 /* 3391 * Wakeup all pending jobs. 3392 */ 3393 sym_flush_busy_queue(np, CAM_SCSI_BUS_RESET); 3394 3395 /* 3396 * Init chip. 3397 */ 3398 OUTB (nc_istat, 0x00 ); /* Remove Reset, abort */ 3399 UDELAY (2000); /* The 895 needs time for the bus mode to settle */ 3400 3401 OUTB (nc_scntl0, np->rv_scntl0 | 0xc0); 3402 /* full arb., ena parity, par->ATN */ 3403 OUTB (nc_scntl1, 0x00); /* odd parity, and remove CRST!! */ 3404 3405 sym_selectclock(np, np->rv_scntl3); /* Select SCSI clock */ 3406 3407 OUTB (nc_scid , RRE|np->myaddr); /* Adapter SCSI address */ 3408 OUTW (nc_respid, 1ul<<np->myaddr); /* Id to respond to */ 3409 OUTB (nc_istat , SIGP ); /* Signal Process */ 3410 OUTB (nc_dmode , np->rv_dmode); /* Burst length, dma mode */ 3411 OUTB (nc_ctest5, np->rv_ctest5); /* Large fifo + large burst */ 3412 3413 OUTB (nc_dcntl , NOCOM|np->rv_dcntl); /* Protect SFBR */ 3414 OUTB (nc_ctest3, np->rv_ctest3); /* Write and invalidate */ 3415 OUTB (nc_ctest4, np->rv_ctest4); /* Master parity checking */ 3416 3417 /* Extended Sreq/Sack filtering not supported on the C10 */ 3418 if (np->features & FE_C10) 3419 OUTB (nc_stest2, np->rv_stest2); 3420 else 3421 OUTB (nc_stest2, EXT|np->rv_stest2); 3422 3423 OUTB (nc_stest3, TE); /* TolerANT enable */ 3424 OUTB (nc_stime0, 0x0c); /* HTH disabled STO 0.25 sec */ 3425 3426 /* 3427 * For now, disable AIP generation on C1010-66. 3428 */ 3429 if (np->device_id == PCI_ID_LSI53C1010_2) 3430 OUTB (nc_aipcntl1, DISAIP); 3431 3432 /* 3433 * C10101 Errata. 3434 * Errant SGE's when in narrow. Write bits 4 & 5 of 3435 * STEST1 register to disable SGE. We probably should do 3436 * that from SCRIPTS for each selection/reselection, but 3437 * I just don't want. :) 3438 */ 3439 if (np->device_id == PCI_ID_LSI53C1010 && 3440 /* np->revision_id < 0xff */ 1) 3441 OUTB (nc_stest1, INB(nc_stest1) | 0x30); 3442 3443 /* 3444 * DEL 441 - 53C876 Rev 5 - Part Number 609-0392787/2788 - ITEM 2. 3445 * Disable overlapped arbitration for some dual function devices, 3446 * regardless revision id (kind of post-chip-design feature. ;-)) 3447 */ 3448 if (np->device_id == PCI_ID_SYM53C875) 3449 OUTB (nc_ctest0, (1<<5)); 3450 else if (np->device_id == PCI_ID_SYM53C896) 3451 np->rv_ccntl0 |= DPR; 3452 3453 /* 3454 * Write CCNTL0/CCNTL1 for chips capable of 64 bit addressing 3455 * and/or hardware phase mismatch, since only such chips 3456 * seem to support those IO registers. 3457 */ 3458 if (np->features & (FE_DAC|FE_NOPM)) { 3459 OUTB (nc_ccntl0, np->rv_ccntl0); 3460 OUTB (nc_ccntl1, np->rv_ccntl1); 3461 } 3462 3463 /* 3464 * If phase mismatch handled by scripts (895A/896/1010), 3465 * set PM jump addresses. 3466 */ 3467 if (np->features & FE_NOPM) { 3468 OUTL (nc_pmjad1, SCRIPTB_BA (np, pm_handle)); 3469 OUTL (nc_pmjad2, SCRIPTB_BA (np, pm_handle)); 3470 } 3471 3472 /* 3473 * Enable GPIO0 pin for writing if LED support from SCRIPTS. 3474 * Also set GPIO5 and clear GPIO6 if hardware LED control. 3475 */ 3476 if (np->features & FE_LED0) 3477 OUTB(nc_gpcntl, INB(nc_gpcntl) & ~0x01); 3478 else if (np->features & FE_LEDC) 3479 OUTB(nc_gpcntl, (INB(nc_gpcntl) & ~0x41) | 0x20); 3480 3481 /* 3482 * enable ints 3483 */ 3484 OUTW (nc_sien , STO|HTH|MA|SGE|UDC|RST|PAR); 3485 OUTB (nc_dien , MDPE|BF|SSI|SIR|IID); 3486 3487 /* 3488 * For 895/6 enable SBMC interrupt and save current SCSI bus mode. 3489 * Try to eat the spurious SBMC interrupt that may occur when 3490 * we reset the chip but not the SCSI BUS (at initialization). 3491 */ 3492 if (np->features & (FE_ULTRA2|FE_ULTRA3)) { 3493 OUTONW (nc_sien, SBMC); 3494 if (reason == 0) { 3495 MDELAY(100); 3496 INW (nc_sist); 3497 } 3498 np->scsi_mode = INB (nc_stest4) & SMODE; 3499 } 3500 3501 /* 3502 * Fill in target structure. 3503 * Reinitialize usrsync. 3504 * Reinitialize usrwide. 3505 * Prepare sync negotiation according to actual SCSI bus mode. 3506 */ 3507 for (i=0;i<SYM_CONF_MAX_TARGET;i++) { 3508 tcb_p tp = &np->target[i]; 3509 3510 tp->to_reset = 0; 3511 tp->head.sval = 0; 3512 tp->head.wval = np->rv_scntl3; 3513 tp->head.uval = 0; 3514 3515 tp->tinfo.current.period = 0; 3516 tp->tinfo.current.offset = 0; 3517 tp->tinfo.current.width = BUS_8_BIT; 3518 tp->tinfo.current.options = 0; 3519 } 3520 3521 /* 3522 * Download SCSI SCRIPTS to on-chip RAM if present, 3523 * and start script processor. 3524 */ 3525 if (np->ram_ba) { 3526 if (sym_verbose > 1) 3527 printf ("%s: Downloading SCSI SCRIPTS.\n", 3528 sym_name(np)); 3529 if (np->ram_ws == 8192) { 3530 OUTRAM_OFF(4096, np->scriptb0, np->scriptb_sz); 3531 OUTL (nc_mmws, np->scr_ram_seg); 3532 OUTL (nc_mmrs, np->scr_ram_seg); 3533 OUTL (nc_sfs, np->scr_ram_seg); 3534 phys = SCRIPTB_BA (np, start64); 3535 } 3536 else 3537 phys = SCRIPTA_BA (np, init); 3538 OUTRAM_OFF(0, np->scripta0, np->scripta_sz); 3539 } 3540 else 3541 phys = SCRIPTA_BA (np, init); 3542 3543 np->istat_sem = 0; 3544 3545 OUTL (nc_dsa, np->hcb_ba); 3546 OUTL_DSP (phys); 3547 3548 /* 3549 * Notify the XPT about the RESET condition. 3550 */ 3551 if (reason != 0) 3552 xpt_async(AC_BUS_RESET, np->path, NULL); 3553 } 3554 3555 /* 3556 * Get clock factor and sync divisor for a given 3557 * synchronous factor period. 3558 */ 3559 static int 3560 sym_getsync(hcb_p np, u_char dt, u_char sfac, u_char *divp, u_char *fakp) 3561 { 3562 u32 clk = np->clock_khz; /* SCSI clock frequency in kHz */ 3563 int div = np->clock_divn; /* Number of divisors supported */ 3564 u32 fak; /* Sync factor in sxfer */ 3565 u32 per; /* Period in tenths of ns */ 3566 u32 kpc; /* (per * clk) */ 3567 int ret; 3568 3569 /* 3570 * Compute the synchronous period in tenths of nano-seconds 3571 */ 3572 if (dt && sfac <= 9) per = 125; 3573 else if (sfac <= 10) per = 250; 3574 else if (sfac == 11) per = 303; 3575 else if (sfac == 12) per = 500; 3576 else per = 40 * sfac; 3577 ret = per; 3578 3579 kpc = per * clk; 3580 if (dt) 3581 kpc <<= 1; 3582 3583 /* 3584 * For earliest C10 revision 0, we cannot use extra 3585 * clocks for the setting of the SCSI clocking. 3586 * Note that this limits the lowest sync data transfer 3587 * to 5 Mega-transfers per second and may result in 3588 * using higher clock divisors. 3589 */ 3590 #if 1 3591 if ((np->features & (FE_C10|FE_U3EN)) == FE_C10) { 3592 /* 3593 * Look for the lowest clock divisor that allows an 3594 * output speed not faster than the period. 3595 */ 3596 while (div > 0) { 3597 --div; 3598 if (kpc > (div_10M[div] << 2)) { 3599 ++div; 3600 break; 3601 } 3602 } 3603 fak = 0; /* No extra clocks */ 3604 if (div == np->clock_divn) { /* Are we too fast ? */ 3605 ret = -1; 3606 } 3607 *divp = div; 3608 *fakp = fak; 3609 return ret; 3610 } 3611 #endif 3612 3613 /* 3614 * Look for the greatest clock divisor that allows an 3615 * input speed faster than the period. 3616 */ 3617 while (div-- > 0) 3618 if (kpc >= (div_10M[div] << 2)) break; 3619 3620 /* 3621 * Calculate the lowest clock factor that allows an output 3622 * speed not faster than the period, and the max output speed. 3623 * If fak >= 1 we will set both XCLKH_ST and XCLKH_DT. 3624 * If fak >= 2 we will also set XCLKS_ST and XCLKS_DT. 3625 */ 3626 if (dt) { 3627 fak = (kpc - 1) / (div_10M[div] << 1) + 1 - 2; 3628 /* ret = ((2+fak)*div_10M[div])/np->clock_khz; */ 3629 } 3630 else { 3631 fak = (kpc - 1) / div_10M[div] + 1 - 4; 3632 /* ret = ((4+fak)*div_10M[div])/np->clock_khz; */ 3633 } 3634 3635 /* 3636 * Check against our hardware limits, or bugs :). 3637 */ 3638 if (fak < 0) {fak = 0; ret = -1;} 3639 if (fak > 2) {fak = 2; ret = -1;} 3640 3641 /* 3642 * Compute and return sync parameters. 3643 */ 3644 *divp = div; 3645 *fakp = fak; 3646 3647 return ret; 3648 } 3649 3650 /* 3651 * Tell the SCSI layer about the new transfer parameters. 3652 */ 3653 static void 3654 sym_xpt_async_transfer_neg(hcb_p np, int target, u_int spi_valid) 3655 { 3656 struct ccb_trans_settings cts; 3657 struct cam_path *path; 3658 int sts; 3659 tcb_p tp = &np->target[target]; 3660 3661 sts = xpt_create_path(&path, NULL, cam_sim_path(np->sim), target, 3662 CAM_LUN_WILDCARD); 3663 if (sts != CAM_REQ_CMP) 3664 return; 3665 3666 bzero(&cts, sizeof(cts)); 3667 3668 #ifdef FreeBSD_New_Tran_Settings 3669 #define cts__scsi (cts.proto_specific.scsi) 3670 #define cts__spi (cts.xport_specific.spi) 3671 3672 cts.type = CTS_TYPE_CURRENT_SETTINGS; 3673 cts.protocol = PROTO_SCSI; 3674 cts.transport = XPORT_SPI; 3675 cts.protocol_version = tp->tinfo.current.scsi_version; 3676 cts.transport_version = tp->tinfo.current.spi_version; 3677 3678 cts__spi.valid = spi_valid; 3679 if (spi_valid & CTS_SPI_VALID_SYNC_RATE) 3680 cts__spi.sync_period = tp->tinfo.current.period; 3681 if (spi_valid & CTS_SPI_VALID_SYNC_OFFSET) 3682 cts__spi.sync_offset = tp->tinfo.current.offset; 3683 if (spi_valid & CTS_SPI_VALID_BUS_WIDTH) 3684 cts__spi.bus_width = tp->tinfo.current.width; 3685 if (spi_valid & CTS_SPI_VALID_PPR_OPTIONS) 3686 cts__spi.ppr_options = tp->tinfo.current.options; 3687 #undef cts__spi 3688 #undef cts__scsi 3689 #else 3690 cts.valid = spi_valid; 3691 if (spi_valid & CCB_TRANS_SYNC_RATE_VALID) 3692 cts.sync_period = tp->tinfo.current.period; 3693 if (spi_valid & CCB_TRANS_SYNC_OFFSET_VALID) 3694 cts.sync_offset = tp->tinfo.current.offset; 3695 if (spi_valid & CCB_TRANS_BUS_WIDTH_VALID) 3696 cts.bus_width = tp->tinfo.current.width; 3697 #endif 3698 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1); 3699 xpt_async(AC_TRANSFER_NEG, path, &cts); 3700 xpt_free_path(path); 3701 } 3702 3703 #ifdef FreeBSD_New_Tran_Settings 3704 #define SYM_SPI_VALID_WDTR \ 3705 CTS_SPI_VALID_BUS_WIDTH | \ 3706 CTS_SPI_VALID_SYNC_RATE | \ 3707 CTS_SPI_VALID_SYNC_OFFSET 3708 #define SYM_SPI_VALID_SDTR \ 3709 CTS_SPI_VALID_SYNC_RATE | \ 3710 CTS_SPI_VALID_SYNC_OFFSET 3711 #define SYM_SPI_VALID_PPR \ 3712 CTS_SPI_VALID_PPR_OPTIONS | \ 3713 CTS_SPI_VALID_BUS_WIDTH | \ 3714 CTS_SPI_VALID_SYNC_RATE | \ 3715 CTS_SPI_VALID_SYNC_OFFSET 3716 #else 3717 #define SYM_SPI_VALID_WDTR \ 3718 CCB_TRANS_BUS_WIDTH_VALID | \ 3719 CCB_TRANS_SYNC_RATE_VALID | \ 3720 CCB_TRANS_SYNC_OFFSET_VALID 3721 #define SYM_SPI_VALID_SDTR \ 3722 CCB_TRANS_SYNC_RATE_VALID | \ 3723 CCB_TRANS_SYNC_OFFSET_VALID 3724 #define SYM_SPI_VALID_PPR \ 3725 CCB_TRANS_BUS_WIDTH_VALID | \ 3726 CCB_TRANS_SYNC_RATE_VALID | \ 3727 CCB_TRANS_SYNC_OFFSET_VALID 3728 #endif 3729 3730 /* 3731 * We received a WDTR. 3732 * Let everything be aware of the changes. 3733 */ 3734 static void sym_setwide(hcb_p np, ccb_p cp, u_char wide) 3735 { 3736 tcb_p tp = &np->target[cp->target]; 3737 3738 sym_settrans(np, cp, 0, 0, 0, wide, 0, 0); 3739 3740 /* 3741 * Tell the SCSI layer about the new transfer parameters. 3742 */ 3743 tp->tinfo.goal.width = tp->tinfo.current.width = wide; 3744 tp->tinfo.current.offset = 0; 3745 tp->tinfo.current.period = 0; 3746 tp->tinfo.current.options = 0; 3747 3748 sym_xpt_async_transfer_neg(np, cp->target, SYM_SPI_VALID_WDTR); 3749 } 3750 3751 /* 3752 * We received a SDTR. 3753 * Let everything be aware of the changes. 3754 */ 3755 static void 3756 sym_setsync(hcb_p np, ccb_p cp, u_char ofs, u_char per, u_char div, u_char fak) 3757 { 3758 tcb_p tp = &np->target[cp->target]; 3759 u_char wide = (cp->phys.select.sel_scntl3 & EWS) ? 1 : 0; 3760 3761 sym_settrans(np, cp, 0, ofs, per, wide, div, fak); 3762 3763 /* 3764 * Tell the SCSI layer about the new transfer parameters. 3765 */ 3766 tp->tinfo.goal.period = tp->tinfo.current.period = per; 3767 tp->tinfo.goal.offset = tp->tinfo.current.offset = ofs; 3768 tp->tinfo.goal.options = tp->tinfo.current.options = 0; 3769 3770 sym_xpt_async_transfer_neg(np, cp->target, SYM_SPI_VALID_SDTR); 3771 } 3772 3773 /* 3774 * We received a PPR. 3775 * Let everything be aware of the changes. 3776 */ 3777 static void sym_setpprot(hcb_p np, ccb_p cp, u_char dt, u_char ofs, 3778 u_char per, u_char wide, u_char div, u_char fak) 3779 { 3780 tcb_p tp = &np->target[cp->target]; 3781 3782 sym_settrans(np, cp, dt, ofs, per, wide, div, fak); 3783 3784 /* 3785 * Tell the SCSI layer about the new transfer parameters. 3786 */ 3787 tp->tinfo.goal.width = tp->tinfo.current.width = wide; 3788 tp->tinfo.goal.period = tp->tinfo.current.period = per; 3789 tp->tinfo.goal.offset = tp->tinfo.current.offset = ofs; 3790 tp->tinfo.goal.options = tp->tinfo.current.options = dt; 3791 3792 sym_xpt_async_transfer_neg(np, cp->target, SYM_SPI_VALID_PPR); 3793 } 3794 3795 /* 3796 * Switch trans mode for current job and it's target. 3797 */ 3798 static void sym_settrans(hcb_p np, ccb_p cp, u_char dt, u_char ofs, 3799 u_char per, u_char wide, u_char div, u_char fak) 3800 { 3801 SYM_QUEHEAD *qp; 3802 union ccb *ccb; 3803 tcb_p tp; 3804 u_char target = INB (nc_sdid) & 0x0f; 3805 u_char sval, wval, uval; 3806 3807 assert (cp); 3808 if (!cp) return; 3809 ccb = cp->cam_ccb; 3810 assert (ccb); 3811 if (!ccb) return; 3812 assert (target == (cp->target & 0xf)); 3813 tp = &np->target[target]; 3814 3815 sval = tp->head.sval; 3816 wval = tp->head.wval; 3817 uval = tp->head.uval; 3818 3819 #if 0 3820 printf("XXXX sval=%x wval=%x uval=%x (%x)\n", 3821 sval, wval, uval, np->rv_scntl3); 3822 #endif 3823 /* 3824 * Set the offset. 3825 */ 3826 if (!(np->features & FE_C10)) 3827 sval = (sval & ~0x1f) | ofs; 3828 else 3829 sval = (sval & ~0x3f) | ofs; 3830 3831 /* 3832 * Set the sync divisor and extra clock factor. 3833 */ 3834 if (ofs != 0) { 3835 wval = (wval & ~0x70) | ((div+1) << 4); 3836 if (!(np->features & FE_C10)) 3837 sval = (sval & ~0xe0) | (fak << 5); 3838 else { 3839 uval = uval & ~(XCLKH_ST|XCLKH_DT|XCLKS_ST|XCLKS_DT); 3840 if (fak >= 1) uval |= (XCLKH_ST|XCLKH_DT); 3841 if (fak >= 2) uval |= (XCLKS_ST|XCLKS_DT); 3842 } 3843 } 3844 3845 /* 3846 * Set the bus width. 3847 */ 3848 wval = wval & ~EWS; 3849 if (wide != 0) 3850 wval |= EWS; 3851 3852 /* 3853 * Set misc. ultra enable bits. 3854 */ 3855 if (np->features & FE_C10) { 3856 uval = uval & ~(U3EN|AIPCKEN); 3857 if (dt) { 3858 assert(np->features & FE_U3EN); 3859 uval |= U3EN; 3860 } 3861 } 3862 else { 3863 wval = wval & ~ULTRA; 3864 if (per <= 12) wval |= ULTRA; 3865 } 3866 3867 /* 3868 * Stop there if sync parameters are unchanged. 3869 */ 3870 if (tp->head.sval == sval && 3871 tp->head.wval == wval && 3872 tp->head.uval == uval) 3873 return; 3874 tp->head.sval = sval; 3875 tp->head.wval = wval; 3876 tp->head.uval = uval; 3877 3878 /* 3879 * Disable extended Sreq/Sack filtering if per < 50. 3880 * Not supported on the C1010. 3881 */ 3882 if (per < 50 && !(np->features & FE_C10)) 3883 OUTOFFB (nc_stest2, EXT); 3884 3885 /* 3886 * set actual value and sync_status 3887 */ 3888 OUTB (nc_sxfer, tp->head.sval); 3889 OUTB (nc_scntl3, tp->head.wval); 3890 3891 if (np->features & FE_C10) { 3892 OUTB (nc_scntl4, tp->head.uval); 3893 } 3894 3895 /* 3896 * patch ALL busy ccbs of this target. 3897 */ 3898 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) { 3899 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq); 3900 if (cp->target != target) 3901 continue; 3902 cp->phys.select.sel_scntl3 = tp->head.wval; 3903 cp->phys.select.sel_sxfer = tp->head.sval; 3904 if (np->features & FE_C10) { 3905 cp->phys.select.sel_scntl4 = tp->head.uval; 3906 } 3907 } 3908 } 3909 3910 /* 3911 * log message for real hard errors 3912 * 3913 * sym0 targ 0?: ERROR (ds:si) (so-si-sd) (sxfer/scntl3) @ name (dsp:dbc). 3914 * reg: r0 r1 r2 r3 r4 r5 r6 ..... rf. 3915 * 3916 * exception register: 3917 * ds: dstat 3918 * si: sist 3919 * 3920 * SCSI bus lines: 3921 * so: control lines as driven by chip. 3922 * si: control lines as seen by chip. 3923 * sd: scsi data lines as seen by chip. 3924 * 3925 * wide/fastmode: 3926 * sxfer: (see the manual) 3927 * scntl3: (see the manual) 3928 * 3929 * current script command: 3930 * dsp: script address (relative to start of script). 3931 * dbc: first word of script command. 3932 * 3933 * First 24 register of the chip: 3934 * r0..rf 3935 */ 3936 static void sym_log_hard_error(hcb_p np, u_short sist, u_char dstat) 3937 { 3938 u32 dsp; 3939 int script_ofs; 3940 int script_size; 3941 char *script_name; 3942 u_char *script_base; 3943 int i; 3944 3945 dsp = INL (nc_dsp); 3946 3947 if (dsp > np->scripta_ba && 3948 dsp <= np->scripta_ba + np->scripta_sz) { 3949 script_ofs = dsp - np->scripta_ba; 3950 script_size = np->scripta_sz; 3951 script_base = (u_char *) np->scripta0; 3952 script_name = "scripta"; 3953 } 3954 else if (np->scriptb_ba < dsp && 3955 dsp <= np->scriptb_ba + np->scriptb_sz) { 3956 script_ofs = dsp - np->scriptb_ba; 3957 script_size = np->scriptb_sz; 3958 script_base = (u_char *) np->scriptb0; 3959 script_name = "scriptb"; 3960 } else { 3961 script_ofs = dsp; 3962 script_size = 0; 3963 script_base = 0; 3964 script_name = "mem"; 3965 } 3966 3967 printf ("%s:%d: ERROR (%x:%x) (%x-%x-%x) (%x/%x) @ (%s %x:%08x).\n", 3968 sym_name (np), (unsigned)INB (nc_sdid)&0x0f, dstat, sist, 3969 (unsigned)INB (nc_socl), (unsigned)INB (nc_sbcl), 3970 (unsigned)INB (nc_sbdl), (unsigned)INB (nc_sxfer), 3971 (unsigned)INB (nc_scntl3), script_name, script_ofs, 3972 (unsigned)INL (nc_dbc)); 3973 3974 if (((script_ofs & 3) == 0) && 3975 (unsigned)script_ofs < script_size) { 3976 printf ("%s: script cmd = %08x\n", sym_name(np), 3977 scr_to_cpu((int) *(u32 *)(script_base + script_ofs))); 3978 } 3979 3980 printf ("%s: regdump:", sym_name(np)); 3981 for (i=0; i<24;i++) 3982 printf (" %02x", (unsigned)INB_OFF(i)); 3983 printf (".\n"); 3984 3985 /* 3986 * PCI BUS error, read the PCI ststus register. 3987 */ 3988 if (dstat & (MDPE|BF)) { 3989 u_short pci_sts; 3990 #ifdef FreeBSD_Bus_Io_Abstraction 3991 pci_sts = pci_read_config(np->device, PCIR_STATUS, 2); 3992 #else 3993 pci_sts = pci_cfgread(np->pci_tag, PCIR_STATUS, 2); 3994 #endif 3995 if (pci_sts & 0xf900) { 3996 #ifdef FreeBSD_Bus_Io_Abstraction 3997 pci_write_config(np->device, PCIR_STATUS, pci_sts, 2); 3998 #else 3999 pci_cfgwrite(np->pci_tag, PCIR_STATUS, pci_sts, 2); 4000 #endif 4001 printf("%s: PCI STATUS = 0x%04x\n", 4002 sym_name(np), pci_sts & 0xf900); 4003 } 4004 } 4005 } 4006 4007 /* 4008 * chip interrupt handler 4009 * 4010 * In normal situations, interrupt conditions occur one at 4011 * a time. But when something bad happens on the SCSI BUS, 4012 * the chip may raise several interrupt flags before 4013 * stopping and interrupting the CPU. The additionnal 4014 * interrupt flags are stacked in some extra registers 4015 * after the SIP and/or DIP flag has been raised in the 4016 * ISTAT. After the CPU has read the interrupt condition 4017 * flag from SIST or DSTAT, the chip unstacks the other 4018 * interrupt flags and sets the corresponding bits in 4019 * SIST or DSTAT. Since the chip starts stacking once the 4020 * SIP or DIP flag is set, there is a small window of time 4021 * where the stacking does not occur. 4022 * 4023 * Typically, multiple interrupt conditions may happen in 4024 * the following situations: 4025 * 4026 * - SCSI parity error + Phase mismatch (PAR|MA) 4027 * When a parity error is detected in input phase 4028 * and the device switches to msg-in phase inside a 4029 * block MOV. 4030 * - SCSI parity error + Unexpected disconnect (PAR|UDC) 4031 * When a stupid device does not want to handle the 4032 * recovery of an SCSI parity error. 4033 * - Some combinations of STO, PAR, UDC, ... 4034 * When using non compliant SCSI stuff, when user is 4035 * doing non compliant hot tampering on the BUS, when 4036 * something really bad happens to a device, etc ... 4037 * 4038 * The heuristic suggested by SYMBIOS to handle 4039 * multiple interrupts is to try unstacking all 4040 * interrupts conditions and to handle them on some 4041 * priority based on error severity. 4042 * This will work when the unstacking has been 4043 * successful, but we cannot be 100 % sure of that, 4044 * since the CPU may have been faster to unstack than 4045 * the chip is able to stack. Hmmm ... But it seems that 4046 * such a situation is very unlikely to happen. 4047 * 4048 * If this happen, for example STO caught by the CPU 4049 * then UDC happenning before the CPU have restarted 4050 * the SCRIPTS, the driver may wrongly complete the 4051 * same command on UDC, since the SCRIPTS didn't restart 4052 * and the DSA still points to the same command. 4053 * We avoid this situation by setting the DSA to an 4054 * invalid value when the CCB is completed and before 4055 * restarting the SCRIPTS. 4056 * 4057 * Another issue is that we need some section of our 4058 * recovery procedures to be somehow uninterruptible but 4059 * the SCRIPTS processor does not provides such a 4060 * feature. For this reason, we handle recovery preferently 4061 * from the C code and check against some SCRIPTS critical 4062 * sections from the C code. 4063 * 4064 * Hopefully, the interrupt handling of the driver is now 4065 * able to resist to weird BUS error conditions, but donnot 4066 * ask me for any guarantee that it will never fail. :-) 4067 * Use at your own decision and risk. 4068 */ 4069 4070 static void sym_intr1 (hcb_p np) 4071 { 4072 u_char istat, istatc; 4073 u_char dstat; 4074 u_short sist; 4075 4076 /* 4077 * interrupt on the fly ? 4078 * 4079 * A `dummy read' is needed to ensure that the 4080 * clear of the INTF flag reaches the device 4081 * before the scanning of the DONE queue. 4082 */ 4083 istat = INB (nc_istat); 4084 if (istat & INTF) { 4085 OUTB (nc_istat, (istat & SIGP) | INTF | np->istat_sem); 4086 istat = INB (nc_istat); /* DUMMY READ */ 4087 if (DEBUG_FLAGS & DEBUG_TINY) printf ("F "); 4088 (void)sym_wakeup_done (np); 4089 }; 4090 4091 if (!(istat & (SIP|DIP))) 4092 return; 4093 4094 #if 0 /* We should never get this one */ 4095 if (istat & CABRT) 4096 OUTB (nc_istat, CABRT); 4097 #endif 4098 4099 /* 4100 * PAR and MA interrupts may occur at the same time, 4101 * and we need to know of both in order to handle 4102 * this situation properly. We try to unstack SCSI 4103 * interrupts for that reason. BTW, I dislike a LOT 4104 * such a loop inside the interrupt routine. 4105 * Even if DMA interrupt stacking is very unlikely to 4106 * happen, we also try unstacking these ones, since 4107 * this has no performance impact. 4108 */ 4109 sist = 0; 4110 dstat = 0; 4111 istatc = istat; 4112 do { 4113 if (istatc & SIP) 4114 sist |= INW (nc_sist); 4115 if (istatc & DIP) 4116 dstat |= INB (nc_dstat); 4117 istatc = INB (nc_istat); 4118 istat |= istatc; 4119 } while (istatc & (SIP|DIP)); 4120 4121 if (DEBUG_FLAGS & DEBUG_TINY) 4122 printf ("<%d|%x:%x|%x:%x>", 4123 (int)INB(nc_scr0), 4124 dstat,sist, 4125 (unsigned)INL(nc_dsp), 4126 (unsigned)INL(nc_dbc)); 4127 /* 4128 * On paper, a memory barrier may be needed here. 4129 * And since we are paranoid ... :) 4130 */ 4131 MEMORY_BARRIER(); 4132 4133 /* 4134 * First, interrupts we want to service cleanly. 4135 * 4136 * Phase mismatch (MA) is the most frequent interrupt 4137 * for chip earlier than the 896 and so we have to service 4138 * it as quickly as possible. 4139 * A SCSI parity error (PAR) may be combined with a phase 4140 * mismatch condition (MA). 4141 * Programmed interrupts (SIR) are used to call the C code 4142 * from SCRIPTS. 4143 * The single step interrupt (SSI) is not used in this 4144 * driver. 4145 */ 4146 if (!(sist & (STO|GEN|HTH|SGE|UDC|SBMC|RST)) && 4147 !(dstat & (MDPE|BF|ABRT|IID))) { 4148 if (sist & PAR) sym_int_par (np, sist); 4149 else if (sist & MA) sym_int_ma (np); 4150 else if (dstat & SIR) sym_int_sir (np); 4151 else if (dstat & SSI) OUTONB_STD (); 4152 else goto unknown_int; 4153 return; 4154 }; 4155 4156 /* 4157 * Now, interrupts that donnot happen in normal 4158 * situations and that we may need to recover from. 4159 * 4160 * On SCSI RESET (RST), we reset everything. 4161 * On SCSI BUS MODE CHANGE (SBMC), we complete all 4162 * active CCBs with RESET status, prepare all devices 4163 * for negotiating again and restart the SCRIPTS. 4164 * On STO and UDC, we complete the CCB with the corres- 4165 * ponding status and restart the SCRIPTS. 4166 */ 4167 if (sist & RST) { 4168 xpt_print_path(np->path); 4169 printf("SCSI BUS reset detected.\n"); 4170 sym_init (np, 1); 4171 return; 4172 }; 4173 4174 OUTB (nc_ctest3, np->rv_ctest3 | CLF); /* clear dma fifo */ 4175 OUTB (nc_stest3, TE|CSF); /* clear scsi fifo */ 4176 4177 if (!(sist & (GEN|HTH|SGE)) && 4178 !(dstat & (MDPE|BF|ABRT|IID))) { 4179 if (sist & SBMC) sym_int_sbmc (np); 4180 else if (sist & STO) sym_int_sto (np); 4181 else if (sist & UDC) sym_int_udc (np); 4182 else goto unknown_int; 4183 return; 4184 }; 4185 4186 /* 4187 * Now, interrupts we are not able to recover cleanly. 4188 * 4189 * Log message for hard errors. 4190 * Reset everything. 4191 */ 4192 4193 sym_log_hard_error(np, sist, dstat); 4194 4195 if ((sist & (GEN|HTH|SGE)) || 4196 (dstat & (MDPE|BF|ABRT|IID))) { 4197 sym_start_reset(np); 4198 return; 4199 }; 4200 4201 unknown_int: 4202 /* 4203 * We just miss the cause of the interrupt. :( 4204 * Print a message. The timeout will do the real work. 4205 */ 4206 printf( "%s: unknown interrupt(s) ignored, " 4207 "ISTAT=0x%x DSTAT=0x%x SIST=0x%x\n", 4208 sym_name(np), istat, dstat, sist); 4209 } 4210 4211 static void sym_intr(void *arg) 4212 { 4213 if (DEBUG_FLAGS & DEBUG_TINY) printf ("["); 4214 sym_intr1((hcb_p) arg); 4215 if (DEBUG_FLAGS & DEBUG_TINY) printf ("]"); 4216 return; 4217 } 4218 4219 static void sym_poll(struct cam_sim *sim) 4220 { 4221 int s = splcam(); 4222 sym_intr(cam_sim_softc(sim)); 4223 splx(s); 4224 } 4225 4226 4227 /* 4228 * generic recovery from scsi interrupt 4229 * 4230 * The doc says that when the chip gets an SCSI interrupt, 4231 * it tries to stop in an orderly fashion, by completing 4232 * an instruction fetch that had started or by flushing 4233 * the DMA fifo for a write to memory that was executing. 4234 * Such a fashion is not enough to know if the instruction 4235 * that was just before the current DSP value has been 4236 * executed or not. 4237 * 4238 * There are some small SCRIPTS sections that deal with 4239 * the start queue and the done queue that may break any 4240 * assomption from the C code if we are interrupted 4241 * inside, so we reset if this happens. Btw, since these 4242 * SCRIPTS sections are executed while the SCRIPTS hasn't 4243 * started SCSI operations, it is very unlikely to happen. 4244 * 4245 * All the driver data structures are supposed to be 4246 * allocated from the same 4 GB memory window, so there 4247 * is a 1 to 1 relationship between DSA and driver data 4248 * structures. Since we are careful :) to invalidate the 4249 * DSA when we complete a command or when the SCRIPTS 4250 * pushes a DSA into a queue, we can trust it when it 4251 * points to a CCB. 4252 */ 4253 static void sym_recover_scsi_int (hcb_p np, u_char hsts) 4254 { 4255 u32 dsp = INL (nc_dsp); 4256 u32 dsa = INL (nc_dsa); 4257 ccb_p cp = sym_ccb_from_dsa(np, dsa); 4258 4259 /* 4260 * If we haven't been interrupted inside the SCRIPTS 4261 * critical pathes, we can safely restart the SCRIPTS 4262 * and trust the DSA value if it matches a CCB. 4263 */ 4264 if ((!(dsp > SCRIPTA_BA (np, getjob_begin) && 4265 dsp < SCRIPTA_BA (np, getjob_end) + 1)) && 4266 (!(dsp > SCRIPTA_BA (np, ungetjob) && 4267 dsp < SCRIPTA_BA (np, reselect) + 1)) && 4268 (!(dsp > SCRIPTB_BA (np, sel_for_abort) && 4269 dsp < SCRIPTB_BA (np, sel_for_abort_1) + 1)) && 4270 (!(dsp > SCRIPTA_BA (np, done) && 4271 dsp < SCRIPTA_BA (np, done_end) + 1))) { 4272 OUTB (nc_ctest3, np->rv_ctest3 | CLF); /* clear dma fifo */ 4273 OUTB (nc_stest3, TE|CSF); /* clear scsi fifo */ 4274 /* 4275 * If we have a CCB, let the SCRIPTS call us back for 4276 * the handling of the error with SCRATCHA filled with 4277 * STARTPOS. This way, we will be able to freeze the 4278 * device queue and requeue awaiting IOs. 4279 */ 4280 if (cp) { 4281 cp->host_status = hsts; 4282 OUTL_DSP (SCRIPTA_BA (np, complete_error)); 4283 } 4284 /* 4285 * Otherwise just restart the SCRIPTS. 4286 */ 4287 else { 4288 OUTL (nc_dsa, 0xffffff); 4289 OUTL_DSP (SCRIPTA_BA (np, start)); 4290 } 4291 } 4292 else 4293 goto reset_all; 4294 4295 return; 4296 4297 reset_all: 4298 sym_start_reset(np); 4299 } 4300 4301 /* 4302 * chip exception handler for selection timeout 4303 */ 4304 static void sym_int_sto (hcb_p np) 4305 { 4306 u32 dsp = INL (nc_dsp); 4307 4308 if (DEBUG_FLAGS & DEBUG_TINY) printf ("T"); 4309 4310 if (dsp == SCRIPTA_BA (np, wf_sel_done) + 8) 4311 sym_recover_scsi_int(np, HS_SEL_TIMEOUT); 4312 else 4313 sym_start_reset(np); 4314 } 4315 4316 /* 4317 * chip exception handler for unexpected disconnect 4318 */ 4319 static void sym_int_udc (hcb_p np) 4320 { 4321 printf ("%s: unexpected disconnect\n", sym_name(np)); 4322 sym_recover_scsi_int(np, HS_UNEXPECTED); 4323 } 4324 4325 /* 4326 * chip exception handler for SCSI bus mode change 4327 * 4328 * spi2-r12 11.2.3 says a transceiver mode change must 4329 * generate a reset event and a device that detects a reset 4330 * event shall initiate a hard reset. It says also that a 4331 * device that detects a mode change shall set data transfer 4332 * mode to eight bit asynchronous, etc... 4333 * So, just reinitializing all except chip should be enough. 4334 */ 4335 static void sym_int_sbmc (hcb_p np) 4336 { 4337 u_char scsi_mode = INB (nc_stest4) & SMODE; 4338 4339 /* 4340 * Notify user. 4341 */ 4342 xpt_print_path(np->path); 4343 printf("SCSI BUS mode change from %s to %s.\n", 4344 sym_scsi_bus_mode(np->scsi_mode), sym_scsi_bus_mode(scsi_mode)); 4345 4346 /* 4347 * Should suspend command processing for a few seconds and 4348 * reinitialize all except the chip. 4349 */ 4350 sym_init (np, 2); 4351 } 4352 4353 /* 4354 * chip exception handler for SCSI parity error. 4355 * 4356 * When the chip detects a SCSI parity error and is 4357 * currently executing a (CH)MOV instruction, it does 4358 * not interrupt immediately, but tries to finish the 4359 * transfer of the current scatter entry before 4360 * interrupting. The following situations may occur: 4361 * 4362 * - The complete scatter entry has been transferred 4363 * without the device having changed phase. 4364 * The chip will then interrupt with the DSP pointing 4365 * to the instruction that follows the MOV. 4366 * 4367 * - A phase mismatch occurs before the MOV finished 4368 * and phase errors are to be handled by the C code. 4369 * The chip will then interrupt with both PAR and MA 4370 * conditions set. 4371 * 4372 * - A phase mismatch occurs before the MOV finished and 4373 * phase errors are to be handled by SCRIPTS. 4374 * The chip will load the DSP with the phase mismatch 4375 * JUMP address and interrupt the host processor. 4376 */ 4377 static void sym_int_par (hcb_p np, u_short sist) 4378 { 4379 u_char hsts = INB (HS_PRT); 4380 u32 dsp = INL (nc_dsp); 4381 u32 dbc = INL (nc_dbc); 4382 u32 dsa = INL (nc_dsa); 4383 u_char sbcl = INB (nc_sbcl); 4384 u_char cmd = dbc >> 24; 4385 int phase = cmd & 7; 4386 ccb_p cp = sym_ccb_from_dsa(np, dsa); 4387 4388 printf("%s: SCSI parity error detected: SCR1=%d DBC=%x SBCL=%x\n", 4389 sym_name(np), hsts, dbc, sbcl); 4390 4391 /* 4392 * Check that the chip is connected to the SCSI BUS. 4393 */ 4394 if (!(INB (nc_scntl1) & ISCON)) { 4395 sym_recover_scsi_int(np, HS_UNEXPECTED); 4396 return; 4397 } 4398 4399 /* 4400 * If the nexus is not clearly identified, reset the bus. 4401 * We will try to do better later. 4402 */ 4403 if (!cp) 4404 goto reset_all; 4405 4406 /* 4407 * Check instruction was a MOV, direction was INPUT and 4408 * ATN is asserted. 4409 */ 4410 if ((cmd & 0xc0) || !(phase & 1) || !(sbcl & 0x8)) 4411 goto reset_all; 4412 4413 /* 4414 * Keep track of the parity error. 4415 */ 4416 OUTONB (HF_PRT, HF_EXT_ERR); 4417 cp->xerr_status |= XE_PARITY_ERR; 4418 4419 /* 4420 * Prepare the message to send to the device. 4421 */ 4422 np->msgout[0] = (phase == 7) ? M_PARITY : M_ID_ERROR; 4423 4424 /* 4425 * If the old phase was DATA IN phase, we have to deal with 4426 * the 3 situations described above. 4427 * For other input phases (MSG IN and STATUS), the device 4428 * must resend the whole thing that failed parity checking 4429 * or signal error. So, jumping to dispatcher should be OK. 4430 */ 4431 if (phase == 1 || phase == 5) { 4432 /* Phase mismatch handled by SCRIPTS */ 4433 if (dsp == SCRIPTB_BA (np, pm_handle)) 4434 OUTL_DSP (dsp); 4435 /* Phase mismatch handled by the C code */ 4436 else if (sist & MA) 4437 sym_int_ma (np); 4438 /* No phase mismatch occurred */ 4439 else { 4440 OUTL (nc_temp, dsp); 4441 OUTL_DSP (SCRIPTA_BA (np, dispatch)); 4442 } 4443 } 4444 else 4445 OUTL_DSP (SCRIPTA_BA (np, clrack)); 4446 return; 4447 4448 reset_all: 4449 sym_start_reset(np); 4450 return; 4451 } 4452 4453 /* 4454 * chip exception handler for phase errors. 4455 * 4456 * We have to construct a new transfer descriptor, 4457 * to transfer the rest of the current block. 4458 */ 4459 static void sym_int_ma (hcb_p np) 4460 { 4461 u32 dbc; 4462 u32 rest; 4463 u32 dsp; 4464 u32 dsa; 4465 u32 nxtdsp; 4466 u32 *vdsp; 4467 u32 oadr, olen; 4468 u32 *tblp; 4469 u32 newcmd; 4470 u_int delta; 4471 u_char cmd; 4472 u_char hflags, hflags0; 4473 struct sym_pmc *pm; 4474 ccb_p cp; 4475 4476 dsp = INL (nc_dsp); 4477 dbc = INL (nc_dbc); 4478 dsa = INL (nc_dsa); 4479 4480 cmd = dbc >> 24; 4481 rest = dbc & 0xffffff; 4482 delta = 0; 4483 4484 /* 4485 * locate matching cp if any. 4486 */ 4487 cp = sym_ccb_from_dsa(np, dsa); 4488 4489 /* 4490 * Donnot take into account dma fifo and various buffers in 4491 * INPUT phase since the chip flushes everything before 4492 * raising the MA interrupt for interrupted INPUT phases. 4493 * For DATA IN phase, we will check for the SWIDE later. 4494 */ 4495 if ((cmd & 7) != 1 && (cmd & 7) != 5) { 4496 u_char ss0, ss2; 4497 4498 if (np->features & FE_DFBC) 4499 delta = INW (nc_dfbc); 4500 else { 4501 u32 dfifo; 4502 4503 /* 4504 * Read DFIFO, CTEST[4-6] using 1 PCI bus ownership. 4505 */ 4506 dfifo = INL(nc_dfifo); 4507 4508 /* 4509 * Calculate remaining bytes in DMA fifo. 4510 * (CTEST5 = dfifo >> 16) 4511 */ 4512 if (dfifo & (DFS << 16)) 4513 delta = ((((dfifo >> 8) & 0x300) | 4514 (dfifo & 0xff)) - rest) & 0x3ff; 4515 else 4516 delta = ((dfifo & 0xff) - rest) & 0x7f; 4517 } 4518 4519 /* 4520 * The data in the dma fifo has not been transfered to 4521 * the target -> add the amount to the rest 4522 * and clear the data. 4523 * Check the sstat2 register in case of wide transfer. 4524 */ 4525 rest += delta; 4526 ss0 = INB (nc_sstat0); 4527 if (ss0 & OLF) rest++; 4528 if (!(np->features & FE_C10)) 4529 if (ss0 & ORF) rest++; 4530 if (cp && (cp->phys.select.sel_scntl3 & EWS)) { 4531 ss2 = INB (nc_sstat2); 4532 if (ss2 & OLF1) rest++; 4533 if (!(np->features & FE_C10)) 4534 if (ss2 & ORF1) rest++; 4535 }; 4536 4537 /* 4538 * Clear fifos. 4539 */ 4540 OUTB (nc_ctest3, np->rv_ctest3 | CLF); /* dma fifo */ 4541 OUTB (nc_stest3, TE|CSF); /* scsi fifo */ 4542 } 4543 4544 /* 4545 * log the information 4546 */ 4547 if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE)) 4548 printf ("P%x%x RL=%d D=%d ", cmd&7, INB(nc_sbcl)&7, 4549 (unsigned) rest, (unsigned) delta); 4550 4551 /* 4552 * try to find the interrupted script command, 4553 * and the address at which to continue. 4554 */ 4555 vdsp = 0; 4556 nxtdsp = 0; 4557 if (dsp > np->scripta_ba && 4558 dsp <= np->scripta_ba + np->scripta_sz) { 4559 vdsp = (u32 *)((char*)np->scripta0 + (dsp-np->scripta_ba-8)); 4560 nxtdsp = dsp; 4561 } 4562 else if (dsp > np->scriptb_ba && 4563 dsp <= np->scriptb_ba + np->scriptb_sz) { 4564 vdsp = (u32 *)((char*)np->scriptb0 + (dsp-np->scriptb_ba-8)); 4565 nxtdsp = dsp; 4566 } 4567 4568 /* 4569 * log the information 4570 */ 4571 if (DEBUG_FLAGS & DEBUG_PHASE) { 4572 printf ("\nCP=%p DSP=%x NXT=%x VDSP=%p CMD=%x ", 4573 cp, (unsigned)dsp, (unsigned)nxtdsp, vdsp, cmd); 4574 }; 4575 4576 if (!vdsp) { 4577 printf ("%s: interrupted SCRIPT address not found.\n", 4578 sym_name (np)); 4579 goto reset_all; 4580 } 4581 4582 if (!cp) { 4583 printf ("%s: SCSI phase error fixup: CCB already dequeued.\n", 4584 sym_name (np)); 4585 goto reset_all; 4586 } 4587 4588 /* 4589 * get old startaddress and old length. 4590 */ 4591 oadr = scr_to_cpu(vdsp[1]); 4592 4593 if (cmd & 0x10) { /* Table indirect */ 4594 tblp = (u32 *) ((char*) &cp->phys + oadr); 4595 olen = scr_to_cpu(tblp[0]); 4596 oadr = scr_to_cpu(tblp[1]); 4597 } else { 4598 tblp = (u32 *) 0; 4599 olen = scr_to_cpu(vdsp[0]) & 0xffffff; 4600 }; 4601 4602 if (DEBUG_FLAGS & DEBUG_PHASE) { 4603 printf ("OCMD=%x\nTBLP=%p OLEN=%x OADR=%x\n", 4604 (unsigned) (scr_to_cpu(vdsp[0]) >> 24), 4605 tblp, 4606 (unsigned) olen, 4607 (unsigned) oadr); 4608 }; 4609 4610 /* 4611 * check cmd against assumed interrupted script command. 4612 * If dt data phase, the MOVE instruction hasn't bit 4 of 4613 * the phase. 4614 */ 4615 if (((cmd & 2) ? cmd : (cmd & ~4)) != (scr_to_cpu(vdsp[0]) >> 24)) { 4616 PRINT_ADDR(cp); 4617 printf ("internal error: cmd=%02x != %02x=(vdsp[0] >> 24)\n", 4618 (unsigned)cmd, (unsigned)scr_to_cpu(vdsp[0]) >> 24); 4619 4620 goto reset_all; 4621 }; 4622 4623 /* 4624 * if old phase not dataphase, leave here. 4625 */ 4626 if (cmd & 2) { 4627 PRINT_ADDR(cp); 4628 printf ("phase change %x-%x %d@%08x resid=%d.\n", 4629 cmd&7, INB(nc_sbcl)&7, (unsigned)olen, 4630 (unsigned)oadr, (unsigned)rest); 4631 goto unexpected_phase; 4632 }; 4633 4634 /* 4635 * Choose the correct PM save area. 4636 * 4637 * Look at the PM_SAVE SCRIPT if you want to understand 4638 * this stuff. The equivalent code is implemented in 4639 * SCRIPTS for the 895A, 896 and 1010 that are able to 4640 * handle PM from the SCRIPTS processor. 4641 */ 4642 hflags0 = INB (HF_PRT); 4643 hflags = hflags0; 4644 4645 if (hflags & (HF_IN_PM0 | HF_IN_PM1 | HF_DP_SAVED)) { 4646 if (hflags & HF_IN_PM0) 4647 nxtdsp = scr_to_cpu(cp->phys.pm0.ret); 4648 else if (hflags & HF_IN_PM1) 4649 nxtdsp = scr_to_cpu(cp->phys.pm1.ret); 4650 4651 if (hflags & HF_DP_SAVED) 4652 hflags ^= HF_ACT_PM; 4653 } 4654 4655 if (!(hflags & HF_ACT_PM)) { 4656 pm = &cp->phys.pm0; 4657 newcmd = SCRIPTA_BA (np, pm0_data); 4658 } 4659 else { 4660 pm = &cp->phys.pm1; 4661 newcmd = SCRIPTA_BA (np, pm1_data); 4662 } 4663 4664 hflags &= ~(HF_IN_PM0 | HF_IN_PM1 | HF_DP_SAVED); 4665 if (hflags != hflags0) 4666 OUTB (HF_PRT, hflags); 4667 4668 /* 4669 * fillin the phase mismatch context 4670 */ 4671 pm->sg.addr = cpu_to_scr(oadr + olen - rest); 4672 pm->sg.size = cpu_to_scr(rest); 4673 pm->ret = cpu_to_scr(nxtdsp); 4674 4675 /* 4676 * If we have a SWIDE, 4677 * - prepare the address to write the SWIDE from SCRIPTS, 4678 * - compute the SCRIPTS address to restart from, 4679 * - move current data pointer context by one byte. 4680 */ 4681 nxtdsp = SCRIPTA_BA (np, dispatch); 4682 if ((cmd & 7) == 1 && cp && (cp->phys.select.sel_scntl3 & EWS) && 4683 (INB (nc_scntl2) & WSR)) { 4684 u32 tmp; 4685 4686 /* 4687 * Set up the table indirect for the MOVE 4688 * of the residual byte and adjust the data 4689 * pointer context. 4690 */ 4691 tmp = scr_to_cpu(pm->sg.addr); 4692 cp->phys.wresid.addr = cpu_to_scr(tmp); 4693 pm->sg.addr = cpu_to_scr(tmp + 1); 4694 tmp = scr_to_cpu(pm->sg.size); 4695 cp->phys.wresid.size = cpu_to_scr((tmp&0xff000000) | 1); 4696 pm->sg.size = cpu_to_scr(tmp - 1); 4697 4698 /* 4699 * If only the residual byte is to be moved, 4700 * no PM context is needed. 4701 */ 4702 if ((tmp&0xffffff) == 1) 4703 newcmd = pm->ret; 4704 4705 /* 4706 * Prepare the address of SCRIPTS that will 4707 * move the residual byte to memory. 4708 */ 4709 nxtdsp = SCRIPTB_BA (np, wsr_ma_helper); 4710 } 4711 4712 if (DEBUG_FLAGS & DEBUG_PHASE) { 4713 PRINT_ADDR(cp); 4714 printf ("PM %x %x %x / %x %x %x.\n", 4715 hflags0, hflags, newcmd, 4716 (unsigned)scr_to_cpu(pm->sg.addr), 4717 (unsigned)scr_to_cpu(pm->sg.size), 4718 (unsigned)scr_to_cpu(pm->ret)); 4719 } 4720 4721 /* 4722 * Restart the SCRIPTS processor. 4723 */ 4724 OUTL (nc_temp, newcmd); 4725 OUTL_DSP (nxtdsp); 4726 return; 4727 4728 /* 4729 * Unexpected phase changes that occurs when the current phase 4730 * is not a DATA IN or DATA OUT phase are due to error conditions. 4731 * Such event may only happen when the SCRIPTS is using a 4732 * multibyte SCSI MOVE. 4733 * 4734 * Phase change Some possible cause 4735 * 4736 * COMMAND --> MSG IN SCSI parity error detected by target. 4737 * COMMAND --> STATUS Bad command or refused by target. 4738 * MSG OUT --> MSG IN Message rejected by target. 4739 * MSG OUT --> COMMAND Bogus target that discards extended 4740 * negotiation messages. 4741 * 4742 * The code below does not care of the new phase and so 4743 * trusts the target. Why to annoy it ? 4744 * If the interrupted phase is COMMAND phase, we restart at 4745 * dispatcher. 4746 * If a target does not get all the messages after selection, 4747 * the code assumes blindly that the target discards extended 4748 * messages and clears the negotiation status. 4749 * If the target does not want all our response to negotiation, 4750 * we force a SIR_NEGO_PROTO interrupt (it is a hack that avoids 4751 * bloat for such a should_not_happen situation). 4752 * In all other situation, we reset the BUS. 4753 * Are these assumptions reasonnable ? (Wait and see ...) 4754 */ 4755 unexpected_phase: 4756 dsp -= 8; 4757 nxtdsp = 0; 4758 4759 switch (cmd & 7) { 4760 case 2: /* COMMAND phase */ 4761 nxtdsp = SCRIPTA_BA (np, dispatch); 4762 break; 4763 #if 0 4764 case 3: /* STATUS phase */ 4765 nxtdsp = SCRIPTA_BA (np, dispatch); 4766 break; 4767 #endif 4768 case 6: /* MSG OUT phase */ 4769 /* 4770 * If the device may want to use untagged when we want 4771 * tagged, we prepare an IDENTIFY without disc. granted, 4772 * since we will not be able to handle reselect. 4773 * Otherwise, we just don't care. 4774 */ 4775 if (dsp == SCRIPTA_BA (np, send_ident)) { 4776 if (cp->tag != NO_TAG && olen - rest <= 3) { 4777 cp->host_status = HS_BUSY; 4778 np->msgout[0] = M_IDENTIFY | cp->lun; 4779 nxtdsp = SCRIPTB_BA (np, ident_break_atn); 4780 } 4781 else 4782 nxtdsp = SCRIPTB_BA (np, ident_break); 4783 } 4784 else if (dsp == SCRIPTB_BA (np, send_wdtr) || 4785 dsp == SCRIPTB_BA (np, send_sdtr) || 4786 dsp == SCRIPTB_BA (np, send_ppr)) { 4787 nxtdsp = SCRIPTB_BA (np, nego_bad_phase); 4788 } 4789 break; 4790 #if 0 4791 case 7: /* MSG IN phase */ 4792 nxtdsp = SCRIPTA_BA (np, clrack); 4793 break; 4794 #endif 4795 } 4796 4797 if (nxtdsp) { 4798 OUTL_DSP (nxtdsp); 4799 return; 4800 } 4801 4802 reset_all: 4803 sym_start_reset(np); 4804 } 4805 4806 /* 4807 * Dequeue from the START queue all CCBs that match 4808 * a given target/lun/task condition (-1 means all), 4809 * and move them from the BUSY queue to the COMP queue 4810 * with CAM_REQUEUE_REQ status condition. 4811 * This function is used during error handling/recovery. 4812 * It is called with SCRIPTS not running. 4813 */ 4814 static int 4815 sym_dequeue_from_squeue(hcb_p np, int i, int target, int lun, int task) 4816 { 4817 int j; 4818 ccb_p cp; 4819 4820 /* 4821 * Make sure the starting index is within range. 4822 */ 4823 assert((i >= 0) && (i < 2*MAX_QUEUE)); 4824 4825 /* 4826 * Walk until end of START queue and dequeue every job 4827 * that matches the target/lun/task condition. 4828 */ 4829 j = i; 4830 while (i != np->squeueput) { 4831 cp = sym_ccb_from_dsa(np, scr_to_cpu(np->squeue[i])); 4832 assert(cp); 4833 #ifdef SYM_CONF_IARB_SUPPORT 4834 /* Forget hints for IARB, they may be no longer relevant */ 4835 cp->host_flags &= ~HF_HINT_IARB; 4836 #endif 4837 if ((target == -1 || cp->target == target) && 4838 (lun == -1 || cp->lun == lun) && 4839 (task == -1 || cp->tag == task)) { 4840 sym_set_cam_status(cp->cam_ccb, CAM_REQUEUE_REQ); 4841 sym_remque(&cp->link_ccbq); 4842 sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq); 4843 } 4844 else { 4845 if (i != j) 4846 np->squeue[j] = np->squeue[i]; 4847 if ((j += 2) >= MAX_QUEUE*2) j = 0; 4848 } 4849 if ((i += 2) >= MAX_QUEUE*2) i = 0; 4850 } 4851 if (i != j) /* Copy back the idle task if needed */ 4852 np->squeue[j] = np->squeue[i]; 4853 np->squeueput = j; /* Update our current start queue pointer */ 4854 4855 return (i - j) / 2; 4856 } 4857 4858 /* 4859 * Complete all CCBs queued to the COMP queue. 4860 * 4861 * These CCBs are assumed: 4862 * - Not to be referenced either by devices or 4863 * SCRIPTS-related queues and datas. 4864 * - To have to be completed with an error condition 4865 * or requeued. 4866 * 4867 * The device queue freeze count is incremented 4868 * for each CCB that does not prevent this. 4869 * This function is called when all CCBs involved 4870 * in error handling/recovery have been reaped. 4871 */ 4872 static void 4873 sym_flush_comp_queue(hcb_p np, int cam_status) 4874 { 4875 SYM_QUEHEAD *qp; 4876 ccb_p cp; 4877 4878 while ((qp = sym_remque_head(&np->comp_ccbq)) != 0) { 4879 union ccb *ccb; 4880 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq); 4881 sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq); 4882 /* Leave quiet CCBs waiting for resources */ 4883 if (cp->host_status == HS_WAIT) 4884 continue; 4885 ccb = cp->cam_ccb; 4886 if (cam_status) 4887 sym_set_cam_status(ccb, cam_status); 4888 sym_free_ccb(np, cp); 4889 sym_freeze_cam_ccb(ccb); 4890 sym_xpt_done(np, ccb); 4891 } 4892 } 4893 4894 /* 4895 * chip handler for bad SCSI status condition 4896 * 4897 * In case of bad SCSI status, we unqueue all the tasks 4898 * currently queued to the controller but not yet started 4899 * and then restart the SCRIPTS processor immediately. 4900 * 4901 * QUEUE FULL and BUSY conditions are handled the same way. 4902 * Basically all the not yet started tasks are requeued in 4903 * device queue and the queue is frozen until a completion. 4904 * 4905 * For CHECK CONDITION and COMMAND TERMINATED status, we use 4906 * the CCB of the failed command to prepare a REQUEST SENSE 4907 * SCSI command and queue it to the controller queue. 4908 * 4909 * SCRATCHA is assumed to have been loaded with STARTPOS 4910 * before the SCRIPTS called the C code. 4911 */ 4912 static void sym_sir_bad_scsi_status(hcb_p np, int num, ccb_p cp) 4913 { 4914 tcb_p tp = &np->target[cp->target]; 4915 u32 startp; 4916 u_char s_status = cp->ssss_status; 4917 u_char h_flags = cp->host_flags; 4918 int msglen; 4919 int nego; 4920 int i; 4921 4922 /* 4923 * Compute the index of the next job to start from SCRIPTS. 4924 */ 4925 i = (INL (nc_scratcha) - np->squeue_ba) / 4; 4926 4927 /* 4928 * The last CCB queued used for IARB hint may be 4929 * no longer relevant. Forget it. 4930 */ 4931 #ifdef SYM_CONF_IARB_SUPPORT 4932 if (np->last_cp) 4933 np->last_cp = 0; 4934 #endif 4935 4936 /* 4937 * Now deal with the SCSI status. 4938 */ 4939 switch(s_status) { 4940 case S_BUSY: 4941 case S_QUEUE_FULL: 4942 if (sym_verbose >= 2) { 4943 PRINT_ADDR(cp); 4944 printf (s_status == S_BUSY ? "BUSY" : "QUEUE FULL\n"); 4945 } 4946 default: /* S_INT, S_INT_COND_MET, S_CONFLICT */ 4947 sym_complete_error (np, cp); 4948 break; 4949 case S_TERMINATED: 4950 case S_CHECK_COND: 4951 /* 4952 * If we get an SCSI error when requesting sense, give up. 4953 */ 4954 if (h_flags & HF_SENSE) { 4955 sym_complete_error (np, cp); 4956 break; 4957 } 4958 4959 /* 4960 * Dequeue all queued CCBs for that device not yet started, 4961 * and restart the SCRIPTS processor immediately. 4962 */ 4963 (void) sym_dequeue_from_squeue(np, i, cp->target, cp->lun, -1); 4964 OUTL_DSP (SCRIPTA_BA (np, start)); 4965 4966 /* 4967 * Save some info of the actual IO. 4968 * Compute the data residual. 4969 */ 4970 cp->sv_scsi_status = cp->ssss_status; 4971 cp->sv_xerr_status = cp->xerr_status; 4972 cp->sv_resid = sym_compute_residual(np, cp); 4973 4974 /* 4975 * Prepare all needed data structures for 4976 * requesting sense data. 4977 */ 4978 4979 /* 4980 * identify message 4981 */ 4982 cp->scsi_smsg2[0] = M_IDENTIFY | cp->lun; 4983 msglen = 1; 4984 4985 /* 4986 * If we are currently using anything different from 4987 * async. 8 bit data transfers with that target, 4988 * start a negotiation, since the device may want 4989 * to report us a UNIT ATTENTION condition due to 4990 * a cause we currently ignore, and we donnot want 4991 * to be stuck with WIDE and/or SYNC data transfer. 4992 * 4993 * cp->nego_status is filled by sym_prepare_nego(). 4994 */ 4995 cp->nego_status = 0; 4996 nego = 0; 4997 if (tp->tinfo.current.options & PPR_OPT_MASK) 4998 nego = NS_PPR; 4999 else if (tp->tinfo.current.width != BUS_8_BIT) 5000 nego = NS_WIDE; 5001 else if (tp->tinfo.current.offset != 0) 5002 nego = NS_SYNC; 5003 if (nego) 5004 msglen += 5005 sym_prepare_nego (np,cp, nego, &cp->scsi_smsg2[msglen]); 5006 /* 5007 * Message table indirect structure. 5008 */ 5009 cp->phys.smsg.addr = cpu_to_scr(CCB_BA (cp, scsi_smsg2)); 5010 cp->phys.smsg.size = cpu_to_scr(msglen); 5011 5012 /* 5013 * sense command 5014 */ 5015 cp->phys.cmd.addr = cpu_to_scr(CCB_BA (cp, sensecmd)); 5016 cp->phys.cmd.size = cpu_to_scr(6); 5017 5018 /* 5019 * patch requested size into sense command 5020 */ 5021 cp->sensecmd[0] = 0x03; 5022 cp->sensecmd[1] = cp->lun << 5; 5023 #ifdef FreeBSD_New_Tran_Settings 5024 if (tp->tinfo.current.scsi_version > 2 || cp->lun > 7) 5025 cp->sensecmd[1] = 0; 5026 #endif 5027 cp->sensecmd[4] = SYM_SNS_BBUF_LEN; 5028 cp->data_len = SYM_SNS_BBUF_LEN; 5029 5030 /* 5031 * sense data 5032 */ 5033 bzero(cp->sns_bbuf, SYM_SNS_BBUF_LEN); 5034 cp->phys.sense.addr = cpu_to_scr(vtobus(cp->sns_bbuf)); 5035 cp->phys.sense.size = cpu_to_scr(SYM_SNS_BBUF_LEN); 5036 5037 /* 5038 * requeue the command. 5039 */ 5040 startp = SCRIPTB_BA (np, sdata_in); 5041 5042 cp->phys.head.savep = cpu_to_scr(startp); 5043 cp->phys.head.goalp = cpu_to_scr(startp + 16); 5044 cp->phys.head.lastp = cpu_to_scr(startp); 5045 cp->startp = cpu_to_scr(startp); 5046 5047 cp->actualquirks = SYM_QUIRK_AUTOSAVE; 5048 cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY; 5049 cp->ssss_status = S_ILLEGAL; 5050 cp->host_flags = (HF_SENSE|HF_DATA_IN); 5051 cp->xerr_status = 0; 5052 cp->extra_bytes = 0; 5053 5054 cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA (np, select)); 5055 5056 /* 5057 * Requeue the command. 5058 */ 5059 sym_put_start_queue(np, cp); 5060 5061 /* 5062 * Give back to upper layer everything we have dequeued. 5063 */ 5064 sym_flush_comp_queue(np, 0); 5065 break; 5066 } 5067 } 5068 5069 /* 5070 * After a device has accepted some management message 5071 * as BUS DEVICE RESET, ABORT TASK, etc ..., or when 5072 * a device signals a UNIT ATTENTION condition, some 5073 * tasks are thrown away by the device. We are required 5074 * to reflect that on our tasks list since the device 5075 * will never complete these tasks. 5076 * 5077 * This function move from the BUSY queue to the COMP 5078 * queue all disconnected CCBs for a given target that 5079 * match the following criteria: 5080 * - lun=-1 means any logical UNIT otherwise a given one. 5081 * - task=-1 means any task, otherwise a given one. 5082 */ 5083 static int 5084 sym_clear_tasks(hcb_p np, int cam_status, int target, int lun, int task) 5085 { 5086 SYM_QUEHEAD qtmp, *qp; 5087 int i = 0; 5088 ccb_p cp; 5089 5090 /* 5091 * Move the entire BUSY queue to our temporary queue. 5092 */ 5093 sym_que_init(&qtmp); 5094 sym_que_splice(&np->busy_ccbq, &qtmp); 5095 sym_que_init(&np->busy_ccbq); 5096 5097 /* 5098 * Put all CCBs that matches our criteria into 5099 * the COMP queue and put back other ones into 5100 * the BUSY queue. 5101 */ 5102 while ((qp = sym_remque_head(&qtmp)) != 0) { 5103 union ccb *ccb; 5104 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq); 5105 ccb = cp->cam_ccb; 5106 if (cp->host_status != HS_DISCONNECT || 5107 cp->target != target || 5108 (lun != -1 && cp->lun != lun) || 5109 (task != -1 && 5110 (cp->tag != NO_TAG && cp->scsi_smsg[2] != task))) { 5111 sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq); 5112 continue; 5113 } 5114 sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq); 5115 5116 /* Preserve the software timeout condition */ 5117 if (sym_get_cam_status(ccb) != CAM_CMD_TIMEOUT) 5118 sym_set_cam_status(ccb, cam_status); 5119 ++i; 5120 #if 0 5121 printf("XXXX TASK @%p CLEARED\n", cp); 5122 #endif 5123 } 5124 return i; 5125 } 5126 5127 /* 5128 * chip handler for TASKS recovery 5129 * 5130 * We cannot safely abort a command, while the SCRIPTS 5131 * processor is running, since we just would be in race 5132 * with it. 5133 * 5134 * As long as we have tasks to abort, we keep the SEM 5135 * bit set in the ISTAT. When this bit is set, the 5136 * SCRIPTS processor interrupts (SIR_SCRIPT_STOPPED) 5137 * each time it enters the scheduler. 5138 * 5139 * If we have to reset a target, clear tasks of a unit, 5140 * or to perform the abort of a disconnected job, we 5141 * restart the SCRIPTS for selecting the target. Once 5142 * selected, the SCRIPTS interrupts (SIR_TARGET_SELECTED). 5143 * If it loses arbitration, the SCRIPTS will interrupt again 5144 * the next time it will enter its scheduler, and so on ... 5145 * 5146 * On SIR_TARGET_SELECTED, we scan for the more 5147 * appropriate thing to do: 5148 * 5149 * - If nothing, we just sent a M_ABORT message to the 5150 * target to get rid of the useless SCSI bus ownership. 5151 * According to the specs, no tasks shall be affected. 5152 * - If the target is to be reset, we send it a M_RESET 5153 * message. 5154 * - If a logical UNIT is to be cleared , we send the 5155 * IDENTIFY(lun) + M_ABORT. 5156 * - If an untagged task is to be aborted, we send the 5157 * IDENTIFY(lun) + M_ABORT. 5158 * - If a tagged task is to be aborted, we send the 5159 * IDENTIFY(lun) + task attributes + M_ABORT_TAG. 5160 * 5161 * Once our 'kiss of death' :) message has been accepted 5162 * by the target, the SCRIPTS interrupts again 5163 * (SIR_ABORT_SENT). On this interrupt, we complete 5164 * all the CCBs that should have been aborted by the 5165 * target according to our message. 5166 */ 5167 static void sym_sir_task_recovery(hcb_p np, int num) 5168 { 5169 SYM_QUEHEAD *qp; 5170 ccb_p cp; 5171 tcb_p tp; 5172 int target=-1, lun=-1, task; 5173 int i, k; 5174 5175 switch(num) { 5176 /* 5177 * The SCRIPTS processor stopped before starting 5178 * the next command in order to allow us to perform 5179 * some task recovery. 5180 */ 5181 case SIR_SCRIPT_STOPPED: 5182 /* 5183 * Do we have any target to reset or unit to clear ? 5184 */ 5185 for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) { 5186 tp = &np->target[i]; 5187 if (tp->to_reset || 5188 (tp->lun0p && tp->lun0p->to_clear)) { 5189 target = i; 5190 break; 5191 } 5192 if (!tp->lunmp) 5193 continue; 5194 for (k = 1 ; k < SYM_CONF_MAX_LUN ; k++) { 5195 if (tp->lunmp[k] && tp->lunmp[k]->to_clear) { 5196 target = i; 5197 break; 5198 } 5199 } 5200 if (target != -1) 5201 break; 5202 } 5203 5204 /* 5205 * If not, walk the busy queue for any 5206 * disconnected CCB to be aborted. 5207 */ 5208 if (target == -1) { 5209 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) { 5210 cp = sym_que_entry(qp,struct sym_ccb,link_ccbq); 5211 if (cp->host_status != HS_DISCONNECT) 5212 continue; 5213 if (cp->to_abort) { 5214 target = cp->target; 5215 break; 5216 } 5217 } 5218 } 5219 5220 /* 5221 * If some target is to be selected, 5222 * prepare and start the selection. 5223 */ 5224 if (target != -1) { 5225 tp = &np->target[target]; 5226 np->abrt_sel.sel_id = target; 5227 np->abrt_sel.sel_scntl3 = tp->head.wval; 5228 np->abrt_sel.sel_sxfer = tp->head.sval; 5229 OUTL(nc_dsa, np->hcb_ba); 5230 OUTL_DSP (SCRIPTB_BA (np, sel_for_abort)); 5231 return; 5232 } 5233 5234 /* 5235 * Now look for a CCB to abort that haven't started yet. 5236 * Btw, the SCRIPTS processor is still stopped, so 5237 * we are not in race. 5238 */ 5239 i = 0; 5240 cp = 0; 5241 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) { 5242 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq); 5243 if (cp->host_status != HS_BUSY && 5244 cp->host_status != HS_NEGOTIATE) 5245 continue; 5246 if (!cp->to_abort) 5247 continue; 5248 #ifdef SYM_CONF_IARB_SUPPORT 5249 /* 5250 * If we are using IMMEDIATE ARBITRATION, we donnot 5251 * want to cancel the last queued CCB, since the 5252 * SCRIPTS may have anticipated the selection. 5253 */ 5254 if (cp == np->last_cp) { 5255 cp->to_abort = 0; 5256 continue; 5257 } 5258 #endif 5259 i = 1; /* Means we have found some */ 5260 break; 5261 } 5262 if (!i) { 5263 /* 5264 * We are done, so we donnot need 5265 * to synchronize with the SCRIPTS anylonger. 5266 * Remove the SEM flag from the ISTAT. 5267 */ 5268 np->istat_sem = 0; 5269 OUTB (nc_istat, SIGP); 5270 break; 5271 } 5272 /* 5273 * Compute index of next position in the start 5274 * queue the SCRIPTS intends to start and dequeue 5275 * all CCBs for that device that haven't been started. 5276 */ 5277 i = (INL (nc_scratcha) - np->squeue_ba) / 4; 5278 i = sym_dequeue_from_squeue(np, i, cp->target, cp->lun, -1); 5279 5280 /* 5281 * Make sure at least our IO to abort has been dequeued. 5282 */ 5283 assert(i && sym_get_cam_status(cp->cam_ccb) == CAM_REQUEUE_REQ); 5284 5285 /* 5286 * Keep track in cam status of the reason of the abort. 5287 */ 5288 if (cp->to_abort == 2) 5289 sym_set_cam_status(cp->cam_ccb, CAM_CMD_TIMEOUT); 5290 else 5291 sym_set_cam_status(cp->cam_ccb, CAM_REQ_ABORTED); 5292 5293 /* 5294 * Complete with error everything that we have dequeued. 5295 */ 5296 sym_flush_comp_queue(np, 0); 5297 break; 5298 /* 5299 * The SCRIPTS processor has selected a target 5300 * we may have some manual recovery to perform for. 5301 */ 5302 case SIR_TARGET_SELECTED: 5303 target = (INB (nc_sdid) & 0xf); 5304 tp = &np->target[target]; 5305 5306 np->abrt_tbl.addr = cpu_to_scr(vtobus(np->abrt_msg)); 5307 5308 /* 5309 * If the target is to be reset, prepare a 5310 * M_RESET message and clear the to_reset flag 5311 * since we donnot expect this operation to fail. 5312 */ 5313 if (tp->to_reset) { 5314 np->abrt_msg[0] = M_RESET; 5315 np->abrt_tbl.size = 1; 5316 tp->to_reset = 0; 5317 break; 5318 } 5319 5320 /* 5321 * Otherwise, look for some logical unit to be cleared. 5322 */ 5323 if (tp->lun0p && tp->lun0p->to_clear) 5324 lun = 0; 5325 else if (tp->lunmp) { 5326 for (k = 1 ; k < SYM_CONF_MAX_LUN ; k++) { 5327 if (tp->lunmp[k] && tp->lunmp[k]->to_clear) { 5328 lun = k; 5329 break; 5330 } 5331 } 5332 } 5333 5334 /* 5335 * If a logical unit is to be cleared, prepare 5336 * an IDENTIFY(lun) + ABORT MESSAGE. 5337 */ 5338 if (lun != -1) { 5339 lcb_p lp = sym_lp(np, tp, lun); 5340 lp->to_clear = 0; /* We donnot expect to fail here */ 5341 np->abrt_msg[0] = M_IDENTIFY | lun; 5342 np->abrt_msg[1] = M_ABORT; 5343 np->abrt_tbl.size = 2; 5344 break; 5345 } 5346 5347 /* 5348 * Otherwise, look for some disconnected job to 5349 * abort for this target. 5350 */ 5351 i = 0; 5352 cp = 0; 5353 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) { 5354 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq); 5355 if (cp->host_status != HS_DISCONNECT) 5356 continue; 5357 if (cp->target != target) 5358 continue; 5359 if (!cp->to_abort) 5360 continue; 5361 i = 1; /* Means we have some */ 5362 break; 5363 } 5364 5365 /* 5366 * If we have none, probably since the device has 5367 * completed the command before we won abitration, 5368 * send a M_ABORT message without IDENTIFY. 5369 * According to the specs, the device must just 5370 * disconnect the BUS and not abort any task. 5371 */ 5372 if (!i) { 5373 np->abrt_msg[0] = M_ABORT; 5374 np->abrt_tbl.size = 1; 5375 break; 5376 } 5377 5378 /* 5379 * We have some task to abort. 5380 * Set the IDENTIFY(lun) 5381 */ 5382 np->abrt_msg[0] = M_IDENTIFY | cp->lun; 5383 5384 /* 5385 * If we want to abort an untagged command, we 5386 * will send an IDENTIFY + M_ABORT. 5387 * Otherwise (tagged command), we will send 5388 * an IDENTIFY + task attributes + ABORT TAG. 5389 */ 5390 if (cp->tag == NO_TAG) { 5391 np->abrt_msg[1] = M_ABORT; 5392 np->abrt_tbl.size = 2; 5393 } 5394 else { 5395 np->abrt_msg[1] = cp->scsi_smsg[1]; 5396 np->abrt_msg[2] = cp->scsi_smsg[2]; 5397 np->abrt_msg[3] = M_ABORT_TAG; 5398 np->abrt_tbl.size = 4; 5399 } 5400 /* 5401 * Keep track of software timeout condition, since the 5402 * peripheral driver may not count retries on abort 5403 * conditions not due to timeout. 5404 */ 5405 if (cp->to_abort == 2) 5406 sym_set_cam_status(cp->cam_ccb, CAM_CMD_TIMEOUT); 5407 cp->to_abort = 0; /* We donnot expect to fail here */ 5408 break; 5409 5410 /* 5411 * The target has accepted our message and switched 5412 * to BUS FREE phase as we expected. 5413 */ 5414 case SIR_ABORT_SENT: 5415 target = (INB (nc_sdid) & 0xf); 5416 tp = &np->target[target]; 5417 5418 /* 5419 ** If we didn't abort anything, leave here. 5420 */ 5421 if (np->abrt_msg[0] == M_ABORT) 5422 break; 5423 5424 /* 5425 * If we sent a M_RESET, then a hardware reset has 5426 * been performed by the target. 5427 * - Reset everything to async 8 bit 5428 * - Tell ourself to negotiate next time :-) 5429 * - Prepare to clear all disconnected CCBs for 5430 * this target from our task list (lun=task=-1) 5431 */ 5432 lun = -1; 5433 task = -1; 5434 if (np->abrt_msg[0] == M_RESET) { 5435 tp->head.sval = 0; 5436 tp->head.wval = np->rv_scntl3; 5437 tp->head.uval = 0; 5438 tp->tinfo.current.period = 0; 5439 tp->tinfo.current.offset = 0; 5440 tp->tinfo.current.width = BUS_8_BIT; 5441 tp->tinfo.current.options = 0; 5442 } 5443 5444 /* 5445 * Otherwise, check for the LUN and TASK(s) 5446 * concerned by the cancelation. 5447 * If it is not ABORT_TAG then it is CLEAR_QUEUE 5448 * or an ABORT message :-) 5449 */ 5450 else { 5451 lun = np->abrt_msg[0] & 0x3f; 5452 if (np->abrt_msg[1] == M_ABORT_TAG) 5453 task = np->abrt_msg[2]; 5454 } 5455 5456 /* 5457 * Complete all the CCBs the device should have 5458 * aborted due to our 'kiss of death' message. 5459 */ 5460 i = (INL (nc_scratcha) - np->squeue_ba) / 4; 5461 (void) sym_dequeue_from_squeue(np, i, target, lun, -1); 5462 (void) sym_clear_tasks(np, CAM_REQ_ABORTED, target, lun, task); 5463 sym_flush_comp_queue(np, 0); 5464 5465 /* 5466 * If we sent a BDR, make uper layer aware of that. 5467 */ 5468 if (np->abrt_msg[0] == M_RESET) 5469 xpt_async(AC_SENT_BDR, np->path, NULL); 5470 break; 5471 } 5472 5473 /* 5474 * Print to the log the message we intend to send. 5475 */ 5476 if (num == SIR_TARGET_SELECTED) { 5477 PRINT_TARGET(np, target); 5478 sym_printl_hex("control msgout:", np->abrt_msg, 5479 np->abrt_tbl.size); 5480 np->abrt_tbl.size = cpu_to_scr(np->abrt_tbl.size); 5481 } 5482 5483 /* 5484 * Let the SCRIPTS processor continue. 5485 */ 5486 OUTONB_STD (); 5487 } 5488 5489 /* 5490 * Gerard's alchemy:) that deals with with the data 5491 * pointer for both MDP and the residual calculation. 5492 * 5493 * I didn't want to bloat the code by more than 200 5494 * lignes for the handling of both MDP and the residual. 5495 * This has been achieved by using a data pointer 5496 * representation consisting in an index in the data 5497 * array (dp_sg) and a negative offset (dp_ofs) that 5498 * have the following meaning: 5499 * 5500 * - dp_sg = SYM_CONF_MAX_SG 5501 * we are at the end of the data script. 5502 * - dp_sg < SYM_CONF_MAX_SG 5503 * dp_sg points to the next entry of the scatter array 5504 * we want to transfer. 5505 * - dp_ofs < 0 5506 * dp_ofs represents the residual of bytes of the 5507 * previous entry scatter entry we will send first. 5508 * - dp_ofs = 0 5509 * no residual to send first. 5510 * 5511 * The function sym_evaluate_dp() accepts an arbitray 5512 * offset (basically from the MDP message) and returns 5513 * the corresponding values of dp_sg and dp_ofs. 5514 */ 5515 5516 static int sym_evaluate_dp(hcb_p np, ccb_p cp, u32 scr, int *ofs) 5517 { 5518 u32 dp_scr; 5519 int dp_ofs, dp_sg, dp_sgmin; 5520 int tmp; 5521 struct sym_pmc *pm; 5522 5523 /* 5524 * Compute the resulted data pointer in term of a script 5525 * address within some DATA script and a signed byte offset. 5526 */ 5527 dp_scr = scr; 5528 dp_ofs = *ofs; 5529 if (dp_scr == SCRIPTA_BA (np, pm0_data)) 5530 pm = &cp->phys.pm0; 5531 else if (dp_scr == SCRIPTA_BA (np, pm1_data)) 5532 pm = &cp->phys.pm1; 5533 else 5534 pm = 0; 5535 5536 if (pm) { 5537 dp_scr = scr_to_cpu(pm->ret); 5538 dp_ofs -= scr_to_cpu(pm->sg.size); 5539 } 5540 5541 /* 5542 * If we are auto-sensing, then we are done. 5543 */ 5544 if (cp->host_flags & HF_SENSE) { 5545 *ofs = dp_ofs; 5546 return 0; 5547 } 5548 5549 /* 5550 * Deduce the index of the sg entry. 5551 * Keep track of the index of the first valid entry. 5552 * If result is dp_sg = SYM_CONF_MAX_SG, then we are at the 5553 * end of the data. 5554 */ 5555 tmp = scr_to_cpu(cp->phys.head.goalp); 5556 dp_sg = SYM_CONF_MAX_SG; 5557 if (dp_scr != tmp) 5558 dp_sg -= (tmp - 8 - (int)dp_scr) / (2*4); 5559 dp_sgmin = SYM_CONF_MAX_SG - cp->segments; 5560 5561 /* 5562 * Move to the sg entry the data pointer belongs to. 5563 * 5564 * If we are inside the data area, we expect result to be: 5565 * 5566 * Either, 5567 * dp_ofs = 0 and dp_sg is the index of the sg entry 5568 * the data pointer belongs to (or the end of the data) 5569 * Or, 5570 * dp_ofs < 0 and dp_sg is the index of the sg entry 5571 * the data pointer belongs to + 1. 5572 */ 5573 if (dp_ofs < 0) { 5574 int n; 5575 while (dp_sg > dp_sgmin) { 5576 --dp_sg; 5577 tmp = scr_to_cpu(cp->phys.data[dp_sg].size); 5578 n = dp_ofs + (tmp & 0xffffff); 5579 if (n > 0) { 5580 ++dp_sg; 5581 break; 5582 } 5583 dp_ofs = n; 5584 } 5585 } 5586 else if (dp_ofs > 0) { 5587 while (dp_sg < SYM_CONF_MAX_SG) { 5588 tmp = scr_to_cpu(cp->phys.data[dp_sg].size); 5589 dp_ofs -= (tmp & 0xffffff); 5590 ++dp_sg; 5591 if (dp_ofs <= 0) 5592 break; 5593 } 5594 } 5595 5596 /* 5597 * Make sure the data pointer is inside the data area. 5598 * If not, return some error. 5599 */ 5600 if (dp_sg < dp_sgmin || (dp_sg == dp_sgmin && dp_ofs < 0)) 5601 goto out_err; 5602 else if (dp_sg > SYM_CONF_MAX_SG || 5603 (dp_sg == SYM_CONF_MAX_SG && dp_ofs > 0)) 5604 goto out_err; 5605 5606 /* 5607 * Save the extreme pointer if needed. 5608 */ 5609 if (dp_sg > cp->ext_sg || 5610 (dp_sg == cp->ext_sg && dp_ofs > cp->ext_ofs)) { 5611 cp->ext_sg = dp_sg; 5612 cp->ext_ofs = dp_ofs; 5613 } 5614 5615 /* 5616 * Return data. 5617 */ 5618 *ofs = dp_ofs; 5619 return dp_sg; 5620 5621 out_err: 5622 return -1; 5623 } 5624 5625 /* 5626 * chip handler for MODIFY DATA POINTER MESSAGE 5627 * 5628 * We also call this function on IGNORE WIDE RESIDUE 5629 * messages that do not match a SWIDE full condition. 5630 * Btw, we assume in that situation that such a message 5631 * is equivalent to a MODIFY DATA POINTER (offset=-1). 5632 */ 5633 5634 static void sym_modify_dp(hcb_p np, tcb_p tp, ccb_p cp, int ofs) 5635 { 5636 int dp_ofs = ofs; 5637 u32 dp_scr = INL (nc_temp); 5638 u32 dp_ret; 5639 u32 tmp; 5640 u_char hflags; 5641 int dp_sg; 5642 struct sym_pmc *pm; 5643 5644 /* 5645 * Not supported for auto-sense. 5646 */ 5647 if (cp->host_flags & HF_SENSE) 5648 goto out_reject; 5649 5650 /* 5651 * Apply our alchemy:) (see comments in sym_evaluate_dp()), 5652 * to the resulted data pointer. 5653 */ 5654 dp_sg = sym_evaluate_dp(np, cp, dp_scr, &dp_ofs); 5655 if (dp_sg < 0) 5656 goto out_reject; 5657 5658 /* 5659 * And our alchemy:) allows to easily calculate the data 5660 * script address we want to return for the next data phase. 5661 */ 5662 dp_ret = cpu_to_scr(cp->phys.head.goalp); 5663 dp_ret = dp_ret - 8 - (SYM_CONF_MAX_SG - dp_sg) * (2*4); 5664 5665 /* 5666 * If offset / scatter entry is zero we donnot need 5667 * a context for the new current data pointer. 5668 */ 5669 if (dp_ofs == 0) { 5670 dp_scr = dp_ret; 5671 goto out_ok; 5672 } 5673 5674 /* 5675 * Get a context for the new current data pointer. 5676 */ 5677 hflags = INB (HF_PRT); 5678 5679 if (hflags & HF_DP_SAVED) 5680 hflags ^= HF_ACT_PM; 5681 5682 if (!(hflags & HF_ACT_PM)) { 5683 pm = &cp->phys.pm0; 5684 dp_scr = SCRIPTA_BA (np, pm0_data); 5685 } 5686 else { 5687 pm = &cp->phys.pm1; 5688 dp_scr = SCRIPTA_BA (np, pm1_data); 5689 } 5690 5691 hflags &= ~(HF_DP_SAVED); 5692 5693 OUTB (HF_PRT, hflags); 5694 5695 /* 5696 * Set up the new current data pointer. 5697 * ofs < 0 there, and for the next data phase, we 5698 * want to transfer part of the data of the sg entry 5699 * corresponding to index dp_sg-1 prior to returning 5700 * to the main data script. 5701 */ 5702 pm->ret = cpu_to_scr(dp_ret); 5703 tmp = scr_to_cpu(cp->phys.data[dp_sg-1].addr); 5704 tmp += scr_to_cpu(cp->phys.data[dp_sg-1].size) + dp_ofs; 5705 pm->sg.addr = cpu_to_scr(tmp); 5706 pm->sg.size = cpu_to_scr(-dp_ofs); 5707 5708 out_ok: 5709 OUTL (nc_temp, dp_scr); 5710 OUTL_DSP (SCRIPTA_BA (np, clrack)); 5711 return; 5712 5713 out_reject: 5714 OUTL_DSP (SCRIPTB_BA (np, msg_bad)); 5715 } 5716 5717 5718 /* 5719 * chip calculation of the data residual. 5720 * 5721 * As I used to say, the requirement of data residual 5722 * in SCSI is broken, useless and cannot be achieved 5723 * without huge complexity. 5724 * But most OSes and even the official CAM require it. 5725 * When stupidity happens to be so widely spread inside 5726 * a community, it gets hard to convince. 5727 * 5728 * Anyway, I don't care, since I am not going to use 5729 * any software that considers this data residual as 5730 * a relevant information. :) 5731 */ 5732 5733 static int sym_compute_residual(hcb_p np, ccb_p cp) 5734 { 5735 int dp_sg, dp_sgmin, resid = 0; 5736 int dp_ofs = 0; 5737 5738 /* 5739 * Check for some data lost or just thrown away. 5740 * We are not required to be quite accurate in this 5741 * situation. Btw, if we are odd for output and the 5742 * device claims some more data, it may well happen 5743 * than our residual be zero. :-) 5744 */ 5745 if (cp->xerr_status & (XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN)) { 5746 if (cp->xerr_status & XE_EXTRA_DATA) 5747 resid -= cp->extra_bytes; 5748 if (cp->xerr_status & XE_SODL_UNRUN) 5749 ++resid; 5750 if (cp->xerr_status & XE_SWIDE_OVRUN) 5751 --resid; 5752 } 5753 5754 /* 5755 * If all data has been transferred, 5756 * there is no residual. 5757 */ 5758 if (cp->phys.head.lastp == cp->phys.head.goalp) 5759 return resid; 5760 5761 /* 5762 * If no data transfer occurs, or if the data 5763 * pointer is weird, return full residual. 5764 */ 5765 if (cp->startp == cp->phys.head.lastp || 5766 sym_evaluate_dp(np, cp, scr_to_cpu(cp->phys.head.lastp), 5767 &dp_ofs) < 0) { 5768 return cp->data_len; 5769 } 5770 5771 /* 5772 * If we were auto-sensing, then we are done. 5773 */ 5774 if (cp->host_flags & HF_SENSE) { 5775 return -dp_ofs; 5776 } 5777 5778 /* 5779 * We are now full comfortable in the computation 5780 * of the data residual (2's complement). 5781 */ 5782 dp_sgmin = SYM_CONF_MAX_SG - cp->segments; 5783 resid = -cp->ext_ofs; 5784 for (dp_sg = cp->ext_sg; dp_sg < SYM_CONF_MAX_SG; ++dp_sg) { 5785 u_int tmp = scr_to_cpu(cp->phys.data[dp_sg].size); 5786 resid += (tmp & 0xffffff); 5787 } 5788 5789 /* 5790 * Hopefully, the result is not too wrong. 5791 */ 5792 return resid; 5793 } 5794 5795 /* 5796 * Print out the content of a SCSI message. 5797 */ 5798 5799 static int sym_show_msg (u_char * msg) 5800 { 5801 u_char i; 5802 printf ("%x",*msg); 5803 if (*msg==M_EXTENDED) { 5804 for (i=1;i<8;i++) { 5805 if (i-1>msg[1]) break; 5806 printf ("-%x",msg[i]); 5807 }; 5808 return (i+1); 5809 } else if ((*msg & 0xf0) == 0x20) { 5810 printf ("-%x",msg[1]); 5811 return (2); 5812 }; 5813 return (1); 5814 } 5815 5816 static void sym_print_msg (ccb_p cp, char *label, u_char *msg) 5817 { 5818 PRINT_ADDR(cp); 5819 if (label) 5820 printf ("%s: ", label); 5821 5822 (void) sym_show_msg (msg); 5823 printf (".\n"); 5824 } 5825 5826 /* 5827 * Negotiation for WIDE and SYNCHRONOUS DATA TRANSFER. 5828 * 5829 * When we try to negotiate, we append the negotiation message 5830 * to the identify and (maybe) simple tag message. 5831 * The host status field is set to HS_NEGOTIATE to mark this 5832 * situation. 5833 * 5834 * If the target doesn't answer this message immediately 5835 * (as required by the standard), the SIR_NEGO_FAILED interrupt 5836 * will be raised eventually. 5837 * The handler removes the HS_NEGOTIATE status, and sets the 5838 * negotiated value to the default (async / nowide). 5839 * 5840 * If we receive a matching answer immediately, we check it 5841 * for validity, and set the values. 5842 * 5843 * If we receive a Reject message immediately, we assume the 5844 * negotiation has failed, and fall back to standard values. 5845 * 5846 * If we receive a negotiation message while not in HS_NEGOTIATE 5847 * state, it's a target initiated negotiation. We prepare a 5848 * (hopefully) valid answer, set our parameters, and send back 5849 * this answer to the target. 5850 * 5851 * If the target doesn't fetch the answer (no message out phase), 5852 * we assume the negotiation has failed, and fall back to default 5853 * settings (SIR_NEGO_PROTO interrupt). 5854 * 5855 * When we set the values, we adjust them in all ccbs belonging 5856 * to this target, in the controller's register, and in the "phys" 5857 * field of the controller's struct sym_hcb. 5858 */ 5859 5860 /* 5861 * chip handler for SYNCHRONOUS DATA TRANSFER REQUEST (SDTR) message. 5862 */ 5863 static void sym_sync_nego(hcb_p np, tcb_p tp, ccb_p cp) 5864 { 5865 u_char chg, ofs, per, fak, div; 5866 int req = 1; 5867 5868 /* 5869 * Synchronous request message received. 5870 */ 5871 if (DEBUG_FLAGS & DEBUG_NEGO) { 5872 sym_print_msg(cp, "sync msgin", np->msgin); 5873 }; 5874 5875 /* 5876 * request or answer ? 5877 */ 5878 if (INB (HS_PRT) == HS_NEGOTIATE) { 5879 OUTB (HS_PRT, HS_BUSY); 5880 if (cp->nego_status && cp->nego_status != NS_SYNC) 5881 goto reject_it; 5882 req = 0; 5883 } 5884 5885 /* 5886 * get requested values. 5887 */ 5888 chg = 0; 5889 per = np->msgin[3]; 5890 ofs = np->msgin[4]; 5891 5892 /* 5893 * check values against our limits. 5894 */ 5895 if (ofs) { 5896 if (ofs > np->maxoffs) 5897 {chg = 1; ofs = np->maxoffs;} 5898 if (req) { 5899 if (ofs > tp->tinfo.user.offset) 5900 {chg = 1; ofs = tp->tinfo.user.offset;} 5901 } 5902 } 5903 5904 if (ofs) { 5905 if (per < np->minsync) 5906 {chg = 1; per = np->minsync;} 5907 if (req) { 5908 if (per < tp->tinfo.user.period) 5909 {chg = 1; per = tp->tinfo.user.period;} 5910 } 5911 } 5912 5913 div = fak = 0; 5914 if (ofs && sym_getsync(np, 0, per, &div, &fak) < 0) 5915 goto reject_it; 5916 5917 if (DEBUG_FLAGS & DEBUG_NEGO) { 5918 PRINT_ADDR(cp); 5919 printf ("sdtr: ofs=%d per=%d div=%d fak=%d chg=%d.\n", 5920 ofs, per, div, fak, chg); 5921 } 5922 5923 /* 5924 * This was an answer message 5925 */ 5926 if (req == 0) { 5927 if (chg) /* Answer wasn't acceptable. */ 5928 goto reject_it; 5929 sym_setsync (np, cp, ofs, per, div, fak); 5930 OUTL_DSP (SCRIPTA_BA (np, clrack)); 5931 return; 5932 } 5933 5934 /* 5935 * It was a request. Set value and 5936 * prepare an answer message 5937 */ 5938 sym_setsync (np, cp, ofs, per, div, fak); 5939 5940 np->msgout[0] = M_EXTENDED; 5941 np->msgout[1] = 3; 5942 np->msgout[2] = M_X_SYNC_REQ; 5943 np->msgout[3] = per; 5944 np->msgout[4] = ofs; 5945 5946 cp->nego_status = NS_SYNC; 5947 5948 if (DEBUG_FLAGS & DEBUG_NEGO) { 5949 sym_print_msg(cp, "sync msgout", np->msgout); 5950 } 5951 5952 np->msgin [0] = M_NOOP; 5953 5954 OUTL_DSP (SCRIPTB_BA (np, sdtr_resp)); 5955 return; 5956 reject_it: 5957 sym_setsync (np, cp, 0, 0, 0, 0); 5958 OUTL_DSP (SCRIPTB_BA (np, msg_bad)); 5959 } 5960 5961 /* 5962 * chip handler for PARALLEL PROTOCOL REQUEST (PPR) message. 5963 */ 5964 static void sym_ppr_nego(hcb_p np, tcb_p tp, ccb_p cp) 5965 { 5966 u_char chg, ofs, per, fak, dt, div, wide; 5967 int req = 1; 5968 5969 /* 5970 * Synchronous request message received. 5971 */ 5972 if (DEBUG_FLAGS & DEBUG_NEGO) { 5973 sym_print_msg(cp, "ppr msgin", np->msgin); 5974 }; 5975 5976 /* 5977 * get requested values. 5978 */ 5979 chg = 0; 5980 per = np->msgin[3]; 5981 ofs = np->msgin[5]; 5982 wide = np->msgin[6]; 5983 dt = np->msgin[7] & PPR_OPT_DT; 5984 5985 /* 5986 * request or answer ? 5987 */ 5988 if (INB (HS_PRT) == HS_NEGOTIATE) { 5989 OUTB (HS_PRT, HS_BUSY); 5990 if (cp->nego_status && cp->nego_status != NS_PPR) 5991 goto reject_it; 5992 req = 0; 5993 } 5994 5995 /* 5996 * check values against our limits. 5997 */ 5998 if (wide > np->maxwide) 5999 {chg = 1; wide = np->maxwide;} 6000 if (!wide || !(np->features & FE_ULTRA3)) 6001 dt &= ~PPR_OPT_DT; 6002 if (req) { 6003 if (wide > tp->tinfo.user.width) 6004 {chg = 1; wide = tp->tinfo.user.width;} 6005 } 6006 6007 if (!(np->features & FE_U3EN)) /* Broken U3EN bit not supported */ 6008 dt &= ~PPR_OPT_DT; 6009 6010 if (dt != (np->msgin[7] & PPR_OPT_MASK)) chg = 1; 6011 6012 if (ofs) { 6013 if (dt) { 6014 if (ofs > np->maxoffs_dt) 6015 {chg = 1; ofs = np->maxoffs_dt;} 6016 } 6017 else if (ofs > np->maxoffs) 6018 {chg = 1; ofs = np->maxoffs;} 6019 if (req) { 6020 if (ofs > tp->tinfo.user.offset) 6021 {chg = 1; ofs = tp->tinfo.user.offset;} 6022 } 6023 } 6024 6025 if (ofs) { 6026 if (dt) { 6027 if (per < np->minsync_dt) 6028 {chg = 1; per = np->minsync_dt;} 6029 } 6030 else if (per < np->minsync) 6031 {chg = 1; per = np->minsync;} 6032 if (req) { 6033 if (per < tp->tinfo.user.period) 6034 {chg = 1; per = tp->tinfo.user.period;} 6035 } 6036 } 6037 6038 div = fak = 0; 6039 if (ofs && sym_getsync(np, dt, per, &div, &fak) < 0) 6040 goto reject_it; 6041 6042 if (DEBUG_FLAGS & DEBUG_NEGO) { 6043 PRINT_ADDR(cp); 6044 printf ("ppr: " 6045 "dt=%x ofs=%d per=%d wide=%d div=%d fak=%d chg=%d.\n", 6046 dt, ofs, per, wide, div, fak, chg); 6047 } 6048 6049 /* 6050 * It was an answer. 6051 */ 6052 if (req == 0) { 6053 if (chg) /* Answer wasn't acceptable */ 6054 goto reject_it; 6055 sym_setpprot (np, cp, dt, ofs, per, wide, div, fak); 6056 OUTL_DSP (SCRIPTA_BA (np, clrack)); 6057 return; 6058 } 6059 6060 /* 6061 * It was a request. Set value and 6062 * prepare an answer message 6063 */ 6064 sym_setpprot (np, cp, dt, ofs, per, wide, div, fak); 6065 6066 np->msgout[0] = M_EXTENDED; 6067 np->msgout[1] = 6; 6068 np->msgout[2] = M_X_PPR_REQ; 6069 np->msgout[3] = per; 6070 np->msgout[4] = 0; 6071 np->msgout[5] = ofs; 6072 np->msgout[6] = wide; 6073 np->msgout[7] = dt; 6074 6075 cp->nego_status = NS_PPR; 6076 6077 if (DEBUG_FLAGS & DEBUG_NEGO) { 6078 sym_print_msg(cp, "ppr msgout", np->msgout); 6079 } 6080 6081 np->msgin [0] = M_NOOP; 6082 6083 OUTL_DSP (SCRIPTB_BA (np, ppr_resp)); 6084 return; 6085 reject_it: 6086 sym_setpprot (np, cp, 0, 0, 0, 0, 0, 0); 6087 OUTL_DSP (SCRIPTB_BA (np, msg_bad)); 6088 /* 6089 * If it was a device response that should result in 6090 * ST, we may want to try a legacy negotiation later. 6091 */ 6092 if (!req && !dt) { 6093 tp->tinfo.goal.options = 0; 6094 tp->tinfo.goal.width = wide; 6095 tp->tinfo.goal.period = per; 6096 tp->tinfo.goal.offset = ofs; 6097 } 6098 return; 6099 } 6100 6101 /* 6102 * chip handler for WIDE DATA TRANSFER REQUEST (WDTR) message. 6103 */ 6104 static void sym_wide_nego(hcb_p np, tcb_p tp, ccb_p cp) 6105 { 6106 u_char chg, wide; 6107 int req = 1; 6108 6109 /* 6110 * Wide request message received. 6111 */ 6112 if (DEBUG_FLAGS & DEBUG_NEGO) { 6113 sym_print_msg(cp, "wide msgin", np->msgin); 6114 }; 6115 6116 /* 6117 * Is it a request from the device? 6118 */ 6119 if (INB (HS_PRT) == HS_NEGOTIATE) { 6120 OUTB (HS_PRT, HS_BUSY); 6121 if (cp->nego_status && cp->nego_status != NS_WIDE) 6122 goto reject_it; 6123 req = 0; 6124 } 6125 6126 /* 6127 * get requested values. 6128 */ 6129 chg = 0; 6130 wide = np->msgin[3]; 6131 6132 /* 6133 * check values against driver limits. 6134 */ 6135 if (wide > np->maxwide) 6136 {chg = 1; wide = np->maxwide;} 6137 if (req) { 6138 if (wide > tp->tinfo.user.width) 6139 {chg = 1; wide = tp->tinfo.user.width;} 6140 } 6141 6142 if (DEBUG_FLAGS & DEBUG_NEGO) { 6143 PRINT_ADDR(cp); 6144 printf ("wdtr: wide=%d chg=%d.\n", wide, chg); 6145 } 6146 6147 /* 6148 * This was an answer message 6149 */ 6150 if (req == 0) { 6151 if (chg) /* Answer wasn't acceptable. */ 6152 goto reject_it; 6153 sym_setwide (np, cp, wide); 6154 6155 /* 6156 * Negotiate for SYNC immediately after WIDE response. 6157 * This allows to negotiate for both WIDE and SYNC on 6158 * a single SCSI command (Suggested by Justin Gibbs). 6159 */ 6160 if (tp->tinfo.goal.offset) { 6161 np->msgout[0] = M_EXTENDED; 6162 np->msgout[1] = 3; 6163 np->msgout[2] = M_X_SYNC_REQ; 6164 np->msgout[3] = tp->tinfo.goal.period; 6165 np->msgout[4] = tp->tinfo.goal.offset; 6166 6167 if (DEBUG_FLAGS & DEBUG_NEGO) { 6168 sym_print_msg(cp, "sync msgout", np->msgout); 6169 } 6170 6171 cp->nego_status = NS_SYNC; 6172 OUTB (HS_PRT, HS_NEGOTIATE); 6173 OUTL_DSP (SCRIPTB_BA (np, sdtr_resp)); 6174 return; 6175 } 6176 6177 OUTL_DSP (SCRIPTA_BA (np, clrack)); 6178 return; 6179 }; 6180 6181 /* 6182 * It was a request, set value and 6183 * prepare an answer message 6184 */ 6185 sym_setwide (np, cp, wide); 6186 6187 np->msgout[0] = M_EXTENDED; 6188 np->msgout[1] = 2; 6189 np->msgout[2] = M_X_WIDE_REQ; 6190 np->msgout[3] = wide; 6191 6192 np->msgin [0] = M_NOOP; 6193 6194 cp->nego_status = NS_WIDE; 6195 6196 if (DEBUG_FLAGS & DEBUG_NEGO) { 6197 sym_print_msg(cp, "wide msgout", np->msgout); 6198 } 6199 6200 OUTL_DSP (SCRIPTB_BA (np, wdtr_resp)); 6201 return; 6202 reject_it: 6203 OUTL_DSP (SCRIPTB_BA (np, msg_bad)); 6204 } 6205 6206 /* 6207 * Reset SYNC or WIDE to default settings. 6208 * 6209 * Called when a negotiation does not succeed either 6210 * on rejection or on protocol error. 6211 * 6212 * If it was a PPR that made problems, we may want to 6213 * try a legacy negotiation later. 6214 */ 6215 static void sym_nego_default(hcb_p np, tcb_p tp, ccb_p cp) 6216 { 6217 /* 6218 * any error in negotiation: 6219 * fall back to default mode. 6220 */ 6221 switch (cp->nego_status) { 6222 case NS_PPR: 6223 #if 0 6224 sym_setpprot (np, cp, 0, 0, 0, 0, 0, 0); 6225 #else 6226 tp->tinfo.goal.options = 0; 6227 if (tp->tinfo.goal.period < np->minsync) 6228 tp->tinfo.goal.period = np->minsync; 6229 if (tp->tinfo.goal.offset > np->maxoffs) 6230 tp->tinfo.goal.offset = np->maxoffs; 6231 #endif 6232 break; 6233 case NS_SYNC: 6234 sym_setsync (np, cp, 0, 0, 0, 0); 6235 break; 6236 case NS_WIDE: 6237 sym_setwide (np, cp, 0); 6238 break; 6239 }; 6240 np->msgin [0] = M_NOOP; 6241 np->msgout[0] = M_NOOP; 6242 cp->nego_status = 0; 6243 } 6244 6245 /* 6246 * chip handler for MESSAGE REJECT received in response to 6247 * a WIDE or SYNCHRONOUS negotiation. 6248 */ 6249 static void sym_nego_rejected(hcb_p np, tcb_p tp, ccb_p cp) 6250 { 6251 sym_nego_default(np, tp, cp); 6252 OUTB (HS_PRT, HS_BUSY); 6253 } 6254 6255 /* 6256 * chip exception handler for programmed interrupts. 6257 */ 6258 static void sym_int_sir (hcb_p np) 6259 { 6260 u_char num = INB (nc_dsps); 6261 u32 dsa = INL (nc_dsa); 6262 ccb_p cp = sym_ccb_from_dsa(np, dsa); 6263 u_char target = INB (nc_sdid) & 0x0f; 6264 tcb_p tp = &np->target[target]; 6265 int tmp; 6266 6267 if (DEBUG_FLAGS & DEBUG_TINY) printf ("I#%d", num); 6268 6269 switch (num) { 6270 /* 6271 * Command has been completed with error condition 6272 * or has been auto-sensed. 6273 */ 6274 case SIR_COMPLETE_ERROR: 6275 sym_complete_error(np, cp); 6276 return; 6277 /* 6278 * The C code is currently trying to recover from something. 6279 * Typically, user want to abort some command. 6280 */ 6281 case SIR_SCRIPT_STOPPED: 6282 case SIR_TARGET_SELECTED: 6283 case SIR_ABORT_SENT: 6284 sym_sir_task_recovery(np, num); 6285 return; 6286 /* 6287 * The device didn't go to MSG OUT phase after having 6288 * been selected with ATN. We donnot want to handle 6289 * that. 6290 */ 6291 case SIR_SEL_ATN_NO_MSG_OUT: 6292 printf ("%s:%d: No MSG OUT phase after selection with ATN.\n", 6293 sym_name (np), target); 6294 goto out_stuck; 6295 /* 6296 * The device didn't switch to MSG IN phase after 6297 * having reseleted the initiator. 6298 */ 6299 case SIR_RESEL_NO_MSG_IN: 6300 printf ("%s:%d: No MSG IN phase after reselection.\n", 6301 sym_name (np), target); 6302 goto out_stuck; 6303 /* 6304 * After reselection, the device sent a message that wasn't 6305 * an IDENTIFY. 6306 */ 6307 case SIR_RESEL_NO_IDENTIFY: 6308 printf ("%s:%d: No IDENTIFY after reselection.\n", 6309 sym_name (np), target); 6310 goto out_stuck; 6311 /* 6312 * The device reselected a LUN we donnot know about. 6313 */ 6314 case SIR_RESEL_BAD_LUN: 6315 np->msgout[0] = M_RESET; 6316 goto out; 6317 /* 6318 * The device reselected for an untagged nexus and we 6319 * haven't any. 6320 */ 6321 case SIR_RESEL_BAD_I_T_L: 6322 np->msgout[0] = M_ABORT; 6323 goto out; 6324 /* 6325 * The device reselected for a tagged nexus that we donnot 6326 * have. 6327 */ 6328 case SIR_RESEL_BAD_I_T_L_Q: 6329 np->msgout[0] = M_ABORT_TAG; 6330 goto out; 6331 /* 6332 * The SCRIPTS let us know that the device has grabbed 6333 * our message and will abort the job. 6334 */ 6335 case SIR_RESEL_ABORTED: 6336 np->lastmsg = np->msgout[0]; 6337 np->msgout[0] = M_NOOP; 6338 printf ("%s:%d: message %x sent on bad reselection.\n", 6339 sym_name (np), target, np->lastmsg); 6340 goto out; 6341 /* 6342 * The SCRIPTS let us know that a message has been 6343 * successfully sent to the device. 6344 */ 6345 case SIR_MSG_OUT_DONE: 6346 np->lastmsg = np->msgout[0]; 6347 np->msgout[0] = M_NOOP; 6348 /* Should we really care of that */ 6349 if (np->lastmsg == M_PARITY || np->lastmsg == M_ID_ERROR) { 6350 if (cp) { 6351 cp->xerr_status &= ~XE_PARITY_ERR; 6352 if (!cp->xerr_status) 6353 OUTOFFB (HF_PRT, HF_EXT_ERR); 6354 } 6355 } 6356 goto out; 6357 /* 6358 * The device didn't send a GOOD SCSI status. 6359 * We may have some work to do prior to allow 6360 * the SCRIPTS processor to continue. 6361 */ 6362 case SIR_BAD_SCSI_STATUS: 6363 if (!cp) 6364 goto out; 6365 sym_sir_bad_scsi_status(np, num, cp); 6366 return; 6367 /* 6368 * We are asked by the SCRIPTS to prepare a 6369 * REJECT message. 6370 */ 6371 case SIR_REJECT_TO_SEND: 6372 sym_print_msg(cp, "M_REJECT to send for ", np->msgin); 6373 np->msgout[0] = M_REJECT; 6374 goto out; 6375 /* 6376 * We have been ODD at the end of a DATA IN 6377 * transfer and the device didn't send a 6378 * IGNORE WIDE RESIDUE message. 6379 * It is a data overrun condition. 6380 */ 6381 case SIR_SWIDE_OVERRUN: 6382 if (cp) { 6383 OUTONB (HF_PRT, HF_EXT_ERR); 6384 cp->xerr_status |= XE_SWIDE_OVRUN; 6385 } 6386 goto out; 6387 /* 6388 * We have been ODD at the end of a DATA OUT 6389 * transfer. 6390 * It is a data underrun condition. 6391 */ 6392 case SIR_SODL_UNDERRUN: 6393 if (cp) { 6394 OUTONB (HF_PRT, HF_EXT_ERR); 6395 cp->xerr_status |= XE_SODL_UNRUN; 6396 } 6397 goto out; 6398 /* 6399 * The device wants us to tranfer more data than 6400 * expected or in the wrong direction. 6401 * The number of extra bytes is in scratcha. 6402 * It is a data overrun condition. 6403 */ 6404 case SIR_DATA_OVERRUN: 6405 if (cp) { 6406 OUTONB (HF_PRT, HF_EXT_ERR); 6407 cp->xerr_status |= XE_EXTRA_DATA; 6408 cp->extra_bytes += INL (nc_scratcha); 6409 } 6410 goto out; 6411 /* 6412 * The device switched to an illegal phase (4/5). 6413 */ 6414 case SIR_BAD_PHASE: 6415 if (cp) { 6416 OUTONB (HF_PRT, HF_EXT_ERR); 6417 cp->xerr_status |= XE_BAD_PHASE; 6418 } 6419 goto out; 6420 /* 6421 * We received a message. 6422 */ 6423 case SIR_MSG_RECEIVED: 6424 if (!cp) 6425 goto out_stuck; 6426 switch (np->msgin [0]) { 6427 /* 6428 * We received an extended message. 6429 * We handle MODIFY DATA POINTER, SDTR, WDTR 6430 * and reject all other extended messages. 6431 */ 6432 case M_EXTENDED: 6433 switch (np->msgin [2]) { 6434 case M_X_MODIFY_DP: 6435 if (DEBUG_FLAGS & DEBUG_POINTER) 6436 sym_print_msg(cp,"modify DP",np->msgin); 6437 tmp = (np->msgin[3]<<24) + (np->msgin[4]<<16) + 6438 (np->msgin[5]<<8) + (np->msgin[6]); 6439 sym_modify_dp(np, tp, cp, tmp); 6440 return; 6441 case M_X_SYNC_REQ: 6442 sym_sync_nego(np, tp, cp); 6443 return; 6444 case M_X_PPR_REQ: 6445 sym_ppr_nego(np, tp, cp); 6446 return; 6447 case M_X_WIDE_REQ: 6448 sym_wide_nego(np, tp, cp); 6449 return; 6450 default: 6451 goto out_reject; 6452 } 6453 break; 6454 /* 6455 * We received a 1/2 byte message not handled from SCRIPTS. 6456 * We are only expecting MESSAGE REJECT and IGNORE WIDE 6457 * RESIDUE messages that haven't been anticipated by 6458 * SCRIPTS on SWIDE full condition. Unanticipated IGNORE 6459 * WIDE RESIDUE messages are aliased as MODIFY DP (-1). 6460 */ 6461 case M_IGN_RESIDUE: 6462 if (DEBUG_FLAGS & DEBUG_POINTER) 6463 sym_print_msg(cp,"ign wide residue", np->msgin); 6464 sym_modify_dp(np, tp, cp, -1); 6465 return; 6466 case M_REJECT: 6467 if (INB (HS_PRT) == HS_NEGOTIATE) 6468 sym_nego_rejected(np, tp, cp); 6469 else { 6470 PRINT_ADDR(cp); 6471 printf ("M_REJECT received (%x:%x).\n", 6472 scr_to_cpu(np->lastmsg), np->msgout[0]); 6473 } 6474 goto out_clrack; 6475 break; 6476 default: 6477 goto out_reject; 6478 } 6479 break; 6480 /* 6481 * We received an unknown message. 6482 * Ignore all MSG IN phases and reject it. 6483 */ 6484 case SIR_MSG_WEIRD: 6485 sym_print_msg(cp, "WEIRD message received", np->msgin); 6486 OUTL_DSP (SCRIPTB_BA (np, msg_weird)); 6487 return; 6488 /* 6489 * Negotiation failed. 6490 * Target does not send us the reply. 6491 * Remove the HS_NEGOTIATE status. 6492 */ 6493 case SIR_NEGO_FAILED: 6494 OUTB (HS_PRT, HS_BUSY); 6495 /* 6496 * Negotiation failed. 6497 * Target does not want answer message. 6498 */ 6499 case SIR_NEGO_PROTO: 6500 sym_nego_default(np, tp, cp); 6501 goto out; 6502 }; 6503 6504 out: 6505 OUTONB_STD (); 6506 return; 6507 out_reject: 6508 OUTL_DSP (SCRIPTB_BA (np, msg_bad)); 6509 return; 6510 out_clrack: 6511 OUTL_DSP (SCRIPTA_BA (np, clrack)); 6512 return; 6513 out_stuck: 6514 return; 6515 } 6516 6517 /* 6518 * Acquire a control block 6519 */ 6520 static ccb_p sym_get_ccb (hcb_p np, u_char tn, u_char ln, u_char tag_order) 6521 { 6522 tcb_p tp = &np->target[tn]; 6523 lcb_p lp = sym_lp(np, tp, ln); 6524 u_short tag = NO_TAG; 6525 SYM_QUEHEAD *qp; 6526 ccb_p cp = (ccb_p) 0; 6527 6528 /* 6529 * Look for a free CCB 6530 */ 6531 if (sym_que_empty(&np->free_ccbq)) 6532 (void) sym_alloc_ccb(np); 6533 qp = sym_remque_head(&np->free_ccbq); 6534 if (!qp) 6535 goto out; 6536 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq); 6537 6538 /* 6539 * If the LCB is not yet available and the LUN 6540 * has been probed ok, try to allocate the LCB. 6541 */ 6542 if (!lp && sym_is_bit(tp->lun_map, ln)) { 6543 lp = sym_alloc_lcb(np, tn, ln); 6544 if (!lp) 6545 goto out_free; 6546 } 6547 6548 /* 6549 * If the LCB is not available here, then the 6550 * logical unit is not yet discovered. For those 6551 * ones only accept 1 SCSI IO per logical unit, 6552 * since we cannot allow disconnections. 6553 */ 6554 if (!lp) { 6555 if (!sym_is_bit(tp->busy0_map, ln)) 6556 sym_set_bit(tp->busy0_map, ln); 6557 else 6558 goto out_free; 6559 } else { 6560 /* 6561 * If we have been asked for a tagged command. 6562 */ 6563 if (tag_order) { 6564 /* 6565 * Debugging purpose. 6566 */ 6567 assert(lp->busy_itl == 0); 6568 /* 6569 * Allocate resources for tags if not yet. 6570 */ 6571 if (!lp->cb_tags) { 6572 sym_alloc_lcb_tags(np, tn, ln); 6573 if (!lp->cb_tags) 6574 goto out_free; 6575 } 6576 /* 6577 * Get a tag for this SCSI IO and set up 6578 * the CCB bus address for reselection, 6579 * and count it for this LUN. 6580 * Toggle reselect path to tagged. 6581 */ 6582 if (lp->busy_itlq < SYM_CONF_MAX_TASK) { 6583 tag = lp->cb_tags[lp->ia_tag]; 6584 if (++lp->ia_tag == SYM_CONF_MAX_TASK) 6585 lp->ia_tag = 0; 6586 lp->itlq_tbl[tag] = cpu_to_scr(cp->ccb_ba); 6587 ++lp->busy_itlq; 6588 lp->head.resel_sa = 6589 cpu_to_scr(SCRIPTA_BA (np, resel_tag)); 6590 } 6591 else 6592 goto out_free; 6593 } 6594 /* 6595 * This command will not be tagged. 6596 * If we already have either a tagged or untagged 6597 * one, refuse to overlap this untagged one. 6598 */ 6599 else { 6600 /* 6601 * Debugging purpose. 6602 */ 6603 assert(lp->busy_itl == 0 && lp->busy_itlq == 0); 6604 /* 6605 * Count this nexus for this LUN. 6606 * Set up the CCB bus address for reselection. 6607 * Toggle reselect path to untagged. 6608 */ 6609 if (++lp->busy_itl == 1) { 6610 lp->head.itl_task_sa = cpu_to_scr(cp->ccb_ba); 6611 lp->head.resel_sa = 6612 cpu_to_scr(SCRIPTA_BA (np, resel_no_tag)); 6613 } 6614 else 6615 goto out_free; 6616 } 6617 } 6618 /* 6619 * Put the CCB into the busy queue. 6620 */ 6621 sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq); 6622 6623 /* 6624 * Remember all informations needed to free this CCB. 6625 */ 6626 cp->to_abort = 0; 6627 cp->tag = tag; 6628 cp->target = tn; 6629 cp->lun = ln; 6630 6631 if (DEBUG_FLAGS & DEBUG_TAGS) { 6632 PRINT_LUN(np, tn, ln); 6633 printf ("ccb @%p using tag %d.\n", cp, tag); 6634 } 6635 6636 out: 6637 return cp; 6638 out_free: 6639 sym_insque_head(&cp->link_ccbq, &np->free_ccbq); 6640 return (ccb_p) 0; 6641 } 6642 6643 /* 6644 * Release one control block 6645 */ 6646 static void sym_free_ccb (hcb_p np, ccb_p cp) 6647 { 6648 tcb_p tp = &np->target[cp->target]; 6649 lcb_p lp = sym_lp(np, tp, cp->lun); 6650 6651 if (DEBUG_FLAGS & DEBUG_TAGS) { 6652 PRINT_LUN(np, cp->target, cp->lun); 6653 printf ("ccb @%p freeing tag %d.\n", cp, cp->tag); 6654 } 6655 6656 /* 6657 * If LCB available, 6658 */ 6659 if (lp) { 6660 /* 6661 * If tagged, release the tag, set the relect path 6662 */ 6663 if (cp->tag != NO_TAG) { 6664 /* 6665 * Free the tag value. 6666 */ 6667 lp->cb_tags[lp->if_tag] = cp->tag; 6668 if (++lp->if_tag == SYM_CONF_MAX_TASK) 6669 lp->if_tag = 0; 6670 /* 6671 * Make the reselect path invalid, 6672 * and uncount this CCB. 6673 */ 6674 lp->itlq_tbl[cp->tag] = cpu_to_scr(np->bad_itlq_ba); 6675 --lp->busy_itlq; 6676 } else { /* Untagged */ 6677 /* 6678 * Make the reselect path invalid, 6679 * and uncount this CCB. 6680 */ 6681 lp->head.itl_task_sa = cpu_to_scr(np->bad_itl_ba); 6682 --lp->busy_itl; 6683 } 6684 /* 6685 * If no JOB active, make the LUN reselect path invalid. 6686 */ 6687 if (lp->busy_itlq == 0 && lp->busy_itl == 0) 6688 lp->head.resel_sa = 6689 cpu_to_scr(SCRIPTB_BA (np, resel_bad_lun)); 6690 } 6691 /* 6692 * Otherwise, we only accept 1 IO per LUN. 6693 * Clear the bit that keeps track of this IO. 6694 */ 6695 else 6696 sym_clr_bit(tp->busy0_map, cp->lun); 6697 6698 /* 6699 * We donnot queue more than 1 ccb per target 6700 * with negotiation at any time. If this ccb was 6701 * used for negotiation, clear this info in the tcb. 6702 */ 6703 if (cp == tp->nego_cp) 6704 tp->nego_cp = 0; 6705 6706 #ifdef SYM_CONF_IARB_SUPPORT 6707 /* 6708 * If we just complete the last queued CCB, 6709 * clear this info that is no longer relevant. 6710 */ 6711 if (cp == np->last_cp) 6712 np->last_cp = 0; 6713 #endif 6714 6715 #ifdef FreeBSD_Bus_Dma_Abstraction 6716 /* 6717 * Unmap user data from DMA map if needed. 6718 */ 6719 if (cp->dmamapped) { 6720 bus_dmamap_unload(np->data_dmat, cp->dmamap); 6721 cp->dmamapped = 0; 6722 } 6723 #endif 6724 6725 /* 6726 * Make this CCB available. 6727 */ 6728 cp->cam_ccb = 0; 6729 cp->host_status = HS_IDLE; 6730 sym_remque(&cp->link_ccbq); 6731 sym_insque_head(&cp->link_ccbq, &np->free_ccbq); 6732 } 6733 6734 /* 6735 * Allocate a CCB from memory and initialize its fixed part. 6736 */ 6737 static ccb_p sym_alloc_ccb(hcb_p np) 6738 { 6739 ccb_p cp = 0; 6740 int hcode; 6741 6742 /* 6743 * Prevent from allocating more CCBs than we can 6744 * queue to the controller. 6745 */ 6746 if (np->actccbs >= SYM_CONF_MAX_START) 6747 return 0; 6748 6749 /* 6750 * Allocate memory for this CCB. 6751 */ 6752 cp = sym_calloc_dma(sizeof(struct sym_ccb), "CCB"); 6753 if (!cp) 6754 goto out_free; 6755 6756 /* 6757 * Allocate a bounce buffer for sense data. 6758 */ 6759 cp->sns_bbuf = sym_calloc_dma(SYM_SNS_BBUF_LEN, "SNS_BBUF"); 6760 if (!cp->sns_bbuf) 6761 goto out_free; 6762 6763 /* 6764 * Allocate a map for the DMA of user data. 6765 */ 6766 #ifdef FreeBSD_Bus_Dma_Abstraction 6767 if (bus_dmamap_create(np->data_dmat, 0, &cp->dmamap)) 6768 goto out_free; 6769 #endif 6770 /* 6771 * Count it. 6772 */ 6773 np->actccbs++; 6774 6775 /* 6776 * Compute the bus address of this ccb. 6777 */ 6778 cp->ccb_ba = vtobus(cp); 6779 6780 /* 6781 * Insert this ccb into the hashed list. 6782 */ 6783 hcode = CCB_HASH_CODE(cp->ccb_ba); 6784 cp->link_ccbh = np->ccbh[hcode]; 6785 np->ccbh[hcode] = cp; 6786 6787 /* 6788 * Initialyze the start and restart actions. 6789 */ 6790 cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA (np, idle)); 6791 cp->phys.head.go.restart = cpu_to_scr(SCRIPTB_BA (np, bad_i_t_l)); 6792 6793 /* 6794 * Initilialyze some other fields. 6795 */ 6796 cp->phys.smsg_ext.addr = cpu_to_scr(HCB_BA(np, msgin[2])); 6797 6798 /* 6799 * Chain into free ccb queue. 6800 */ 6801 sym_insque_head(&cp->link_ccbq, &np->free_ccbq); 6802 6803 return cp; 6804 out_free: 6805 if (cp) { 6806 if (cp->sns_bbuf) 6807 sym_mfree_dma(cp->sns_bbuf,SYM_SNS_BBUF_LEN,"SNS_BBUF"); 6808 sym_mfree_dma(cp, sizeof(*cp), "CCB"); 6809 } 6810 return 0; 6811 } 6812 6813 /* 6814 * Look up a CCB from a DSA value. 6815 */ 6816 static ccb_p sym_ccb_from_dsa(hcb_p np, u32 dsa) 6817 { 6818 int hcode; 6819 ccb_p cp; 6820 6821 hcode = CCB_HASH_CODE(dsa); 6822 cp = np->ccbh[hcode]; 6823 while (cp) { 6824 if (cp->ccb_ba == dsa) 6825 break; 6826 cp = cp->link_ccbh; 6827 } 6828 6829 return cp; 6830 } 6831 6832 /* 6833 * Target control block initialisation. 6834 * Nothing important to do at the moment. 6835 */ 6836 static void sym_init_tcb (hcb_p np, u_char tn) 6837 { 6838 /* 6839 * Check some alignments required by the chip. 6840 */ 6841 assert (((offsetof(struct sym_reg, nc_sxfer) ^ 6842 offsetof(struct sym_tcb, head.sval)) &3) == 0); 6843 assert (((offsetof(struct sym_reg, nc_scntl3) ^ 6844 offsetof(struct sym_tcb, head.wval)) &3) == 0); 6845 } 6846 6847 /* 6848 * Lun control block allocation and initialization. 6849 */ 6850 static lcb_p sym_alloc_lcb (hcb_p np, u_char tn, u_char ln) 6851 { 6852 tcb_p tp = &np->target[tn]; 6853 lcb_p lp = sym_lp(np, tp, ln); 6854 6855 /* 6856 * Already done, just return. 6857 */ 6858 if (lp) 6859 return lp; 6860 /* 6861 * Check against some race. 6862 */ 6863 assert(!sym_is_bit(tp->busy0_map, ln)); 6864 6865 /* 6866 * Initialize the target control block if not yet. 6867 */ 6868 sym_init_tcb (np, tn); 6869 6870 /* 6871 * Allocate the LCB bus address array. 6872 * Compute the bus address of this table. 6873 */ 6874 if (ln && !tp->luntbl) { 6875 int i; 6876 6877 tp->luntbl = sym_calloc_dma(256, "LUNTBL"); 6878 if (!tp->luntbl) 6879 goto fail; 6880 for (i = 0 ; i < 64 ; i++) 6881 tp->luntbl[i] = cpu_to_scr(vtobus(&np->badlun_sa)); 6882 tp->head.luntbl_sa = cpu_to_scr(vtobus(tp->luntbl)); 6883 } 6884 6885 /* 6886 * Allocate the table of pointers for LUN(s) > 0, if needed. 6887 */ 6888 if (ln && !tp->lunmp) { 6889 tp->lunmp = sym_calloc(SYM_CONF_MAX_LUN * sizeof(lcb_p), 6890 "LUNMP"); 6891 if (!tp->lunmp) 6892 goto fail; 6893 } 6894 6895 /* 6896 * Allocate the lcb. 6897 * Make it available to the chip. 6898 */ 6899 lp = sym_calloc_dma(sizeof(struct sym_lcb), "LCB"); 6900 if (!lp) 6901 goto fail; 6902 if (ln) { 6903 tp->lunmp[ln] = lp; 6904 tp->luntbl[ln] = cpu_to_scr(vtobus(lp)); 6905 } 6906 else { 6907 tp->lun0p = lp; 6908 tp->head.lun0_sa = cpu_to_scr(vtobus(lp)); 6909 } 6910 6911 /* 6912 * Let the itl task point to error handling. 6913 */ 6914 lp->head.itl_task_sa = cpu_to_scr(np->bad_itl_ba); 6915 6916 /* 6917 * Set the reselect pattern to our default. :) 6918 */ 6919 lp->head.resel_sa = cpu_to_scr(SCRIPTB_BA (np, resel_bad_lun)); 6920 6921 /* 6922 * Set user capabilities. 6923 */ 6924 lp->user_flags = tp->usrflags & (SYM_DISC_ENABLED | SYM_TAGS_ENABLED); 6925 6926 fail: 6927 return lp; 6928 } 6929 6930 /* 6931 * Allocate LCB resources for tagged command queuing. 6932 */ 6933 static void sym_alloc_lcb_tags (hcb_p np, u_char tn, u_char ln) 6934 { 6935 tcb_p tp = &np->target[tn]; 6936 lcb_p lp = sym_lp(np, tp, ln); 6937 int i; 6938 6939 /* 6940 * If LCB not available, try to allocate it. 6941 */ 6942 if (!lp && !(lp = sym_alloc_lcb(np, tn, ln))) 6943 goto fail; 6944 6945 /* 6946 * Allocate the task table and and the tag allocation 6947 * circular buffer. We want both or none. 6948 */ 6949 lp->itlq_tbl = sym_calloc_dma(SYM_CONF_MAX_TASK*4, "ITLQ_TBL"); 6950 if (!lp->itlq_tbl) 6951 goto fail; 6952 lp->cb_tags = sym_calloc(SYM_CONF_MAX_TASK, "CB_TAGS"); 6953 if (!lp->cb_tags) { 6954 sym_mfree_dma(lp->itlq_tbl, SYM_CONF_MAX_TASK*4, "ITLQ_TBL"); 6955 lp->itlq_tbl = 0; 6956 goto fail; 6957 } 6958 6959 /* 6960 * Initialize the task table with invalid entries. 6961 */ 6962 for (i = 0 ; i < SYM_CONF_MAX_TASK ; i++) 6963 lp->itlq_tbl[i] = cpu_to_scr(np->notask_ba); 6964 6965 /* 6966 * Fill up the tag buffer with tag numbers. 6967 */ 6968 for (i = 0 ; i < SYM_CONF_MAX_TASK ; i++) 6969 lp->cb_tags[i] = i; 6970 6971 /* 6972 * Make the task table available to SCRIPTS, 6973 * And accept tagged commands now. 6974 */ 6975 lp->head.itlq_tbl_sa = cpu_to_scr(vtobus(lp->itlq_tbl)); 6976 6977 return; 6978 fail: 6979 return; 6980 } 6981 6982 /* 6983 * Test the pci bus snoop logic :-( 6984 * 6985 * Has to be called with interrupts disabled. 6986 */ 6987 #ifndef SYM_CONF_IOMAPPED 6988 static int sym_regtest (hcb_p np) 6989 { 6990 register volatile u32 data; 6991 /* 6992 * chip registers may NOT be cached. 6993 * write 0xffffffff to a read only register area, 6994 * and try to read it back. 6995 */ 6996 data = 0xffffffff; 6997 OUTL_OFF(offsetof(struct sym_reg, nc_dstat), data); 6998 data = INL_OFF(offsetof(struct sym_reg, nc_dstat)); 6999 #if 1 7000 if (data == 0xffffffff) { 7001 #else 7002 if ((data & 0xe2f0fffd) != 0x02000080) { 7003 #endif 7004 printf ("CACHE TEST FAILED: reg dstat-sstat2 readback %x.\n", 7005 (unsigned) data); 7006 return (0x10); 7007 }; 7008 return (0); 7009 } 7010 #endif 7011 7012 static int sym_snooptest (hcb_p np) 7013 { 7014 u32 sym_rd, sym_wr, sym_bk, host_rd, host_wr, pc, dstat; 7015 int i, err=0; 7016 #ifndef SYM_CONF_IOMAPPED 7017 err |= sym_regtest (np); 7018 if (err) return (err); 7019 #endif 7020 restart_test: 7021 /* 7022 * Enable Master Parity Checking as we intend 7023 * to enable it for normal operations. 7024 */ 7025 OUTB (nc_ctest4, (np->rv_ctest4 & MPEE)); 7026 /* 7027 * init 7028 */ 7029 pc = SCRIPTB0_BA (np, snooptest); 7030 host_wr = 1; 7031 sym_wr = 2; 7032 /* 7033 * Set memory and register. 7034 */ 7035 np->cache = cpu_to_scr(host_wr); 7036 OUTL (nc_temp, sym_wr); 7037 /* 7038 * Start script (exchange values) 7039 */ 7040 OUTL (nc_dsa, np->hcb_ba); 7041 OUTL_DSP (pc); 7042 /* 7043 * Wait 'til done (with timeout) 7044 */ 7045 for (i=0; i<SYM_SNOOP_TIMEOUT; i++) 7046 if (INB(nc_istat) & (INTF|SIP|DIP)) 7047 break; 7048 if (i>=SYM_SNOOP_TIMEOUT) { 7049 printf ("CACHE TEST FAILED: timeout.\n"); 7050 return (0x20); 7051 }; 7052 /* 7053 * Check for fatal DMA errors. 7054 */ 7055 dstat = INB (nc_dstat); 7056 #if 1 /* Band aiding for broken hardwares that fail PCI parity */ 7057 if ((dstat & MDPE) && (np->rv_ctest4 & MPEE)) { 7058 printf ("%s: PCI DATA PARITY ERROR DETECTED - " 7059 "DISABLING MASTER DATA PARITY CHECKING.\n", 7060 sym_name(np)); 7061 np->rv_ctest4 &= ~MPEE; 7062 goto restart_test; 7063 } 7064 #endif 7065 if (dstat & (MDPE|BF|IID)) { 7066 printf ("CACHE TEST FAILED: DMA error (dstat=0x%02x).", dstat); 7067 return (0x80); 7068 } 7069 /* 7070 * Save termination position. 7071 */ 7072 pc = INL (nc_dsp); 7073 /* 7074 * Read memory and register. 7075 */ 7076 host_rd = scr_to_cpu(np->cache); 7077 sym_rd = INL (nc_scratcha); 7078 sym_bk = INL (nc_temp); 7079 7080 /* 7081 * Check termination position. 7082 */ 7083 if (pc != SCRIPTB0_BA (np, snoopend)+8) { 7084 printf ("CACHE TEST FAILED: script execution failed.\n"); 7085 printf ("start=%08lx, pc=%08lx, end=%08lx\n", 7086 (u_long) SCRIPTB0_BA (np, snooptest), (u_long) pc, 7087 (u_long) SCRIPTB0_BA (np, snoopend) +8); 7088 return (0x40); 7089 }; 7090 /* 7091 * Show results. 7092 */ 7093 if (host_wr != sym_rd) { 7094 printf ("CACHE TEST FAILED: host wrote %d, chip read %d.\n", 7095 (int) host_wr, (int) sym_rd); 7096 err |= 1; 7097 }; 7098 if (host_rd != sym_wr) { 7099 printf ("CACHE TEST FAILED: chip wrote %d, host read %d.\n", 7100 (int) sym_wr, (int) host_rd); 7101 err |= 2; 7102 }; 7103 if (sym_bk != sym_wr) { 7104 printf ("CACHE TEST FAILED: chip wrote %d, read back %d.\n", 7105 (int) sym_wr, (int) sym_bk); 7106 err |= 4; 7107 }; 7108 7109 return (err); 7110 } 7111 7112 /* 7113 * Determine the chip's clock frequency. 7114 * 7115 * This is essential for the negotiation of the synchronous 7116 * transfer rate. 7117 * 7118 * Note: we have to return the correct value. 7119 * THERE IS NO SAFE DEFAULT VALUE. 7120 * 7121 * Most NCR/SYMBIOS boards are delivered with a 40 Mhz clock. 7122 * 53C860 and 53C875 rev. 1 support fast20 transfers but 7123 * do not have a clock doubler and so are provided with a 7124 * 80 MHz clock. All other fast20 boards incorporate a doubler 7125 * and so should be delivered with a 40 MHz clock. 7126 * The recent fast40 chips (895/896/895A/1010) use a 40 Mhz base 7127 * clock and provide a clock quadrupler (160 Mhz). 7128 */ 7129 7130 /* 7131 * Select SCSI clock frequency 7132 */ 7133 static void sym_selectclock(hcb_p np, u_char scntl3) 7134 { 7135 /* 7136 * If multiplier not present or not selected, leave here. 7137 */ 7138 if (np->multiplier <= 1) { 7139 OUTB(nc_scntl3, scntl3); 7140 return; 7141 } 7142 7143 if (sym_verbose >= 2) 7144 printf ("%s: enabling clock multiplier\n", sym_name(np)); 7145 7146 OUTB(nc_stest1, DBLEN); /* Enable clock multiplier */ 7147 /* 7148 * Wait for the LCKFRQ bit to be set if supported by the chip. 7149 * Otherwise wait 20 micro-seconds. 7150 */ 7151 if (np->features & FE_LCKFRQ) { 7152 int i = 20; 7153 while (!(INB(nc_stest4) & LCKFRQ) && --i > 0) 7154 UDELAY (20); 7155 if (!i) 7156 printf("%s: the chip cannot lock the frequency\n", 7157 sym_name(np)); 7158 } else 7159 UDELAY (20); 7160 OUTB(nc_stest3, HSC); /* Halt the scsi clock */ 7161 OUTB(nc_scntl3, scntl3); 7162 OUTB(nc_stest1, (DBLEN|DBLSEL));/* Select clock multiplier */ 7163 OUTB(nc_stest3, 0x00); /* Restart scsi clock */ 7164 } 7165 7166 /* 7167 * calculate SCSI clock frequency (in KHz) 7168 */ 7169 static unsigned getfreq (hcb_p np, int gen) 7170 { 7171 unsigned int ms = 0; 7172 unsigned int f; 7173 7174 /* 7175 * Measure GEN timer delay in order 7176 * to calculate SCSI clock frequency 7177 * 7178 * This code will never execute too 7179 * many loop iterations (if DELAY is 7180 * reasonably correct). It could get 7181 * too low a delay (too high a freq.) 7182 * if the CPU is slow executing the 7183 * loop for some reason (an NMI, for 7184 * example). For this reason we will 7185 * if multiple measurements are to be 7186 * performed trust the higher delay 7187 * (lower frequency returned). 7188 */ 7189 OUTW (nc_sien , 0); /* mask all scsi interrupts */ 7190 (void) INW (nc_sist); /* clear pending scsi interrupt */ 7191 OUTB (nc_dien , 0); /* mask all dma interrupts */ 7192 (void) INW (nc_sist); /* another one, just to be sure :) */ 7193 OUTB (nc_scntl3, 4); /* set pre-scaler to divide by 3 */ 7194 OUTB (nc_stime1, 0); /* disable general purpose timer */ 7195 OUTB (nc_stime1, gen); /* set to nominal delay of 1<<gen * 125us */ 7196 while (!(INW(nc_sist) & GEN) && ms++ < 100000) 7197 UDELAY (1000); /* count ms */ 7198 OUTB (nc_stime1, 0); /* disable general purpose timer */ 7199 /* 7200 * set prescaler to divide by whatever 0 means 7201 * 0 ought to choose divide by 2, but appears 7202 * to set divide by 3.5 mode in my 53c810 ... 7203 */ 7204 OUTB (nc_scntl3, 0); 7205 7206 /* 7207 * adjust for prescaler, and convert into KHz 7208 */ 7209 f = ms ? ((1 << gen) * 4340) / ms : 0; 7210 7211 if (sym_verbose >= 2) 7212 printf ("%s: Delay (GEN=%d): %u msec, %u KHz\n", 7213 sym_name(np), gen, ms, f); 7214 7215 return f; 7216 } 7217 7218 static unsigned sym_getfreq (hcb_p np) 7219 { 7220 u_int f1, f2; 7221 int gen = 11; 7222 7223 (void) getfreq (np, gen); /* throw away first result */ 7224 f1 = getfreq (np, gen); 7225 f2 = getfreq (np, gen); 7226 if (f1 > f2) f1 = f2; /* trust lower result */ 7227 return f1; 7228 } 7229 7230 /* 7231 * Get/probe chip SCSI clock frequency 7232 */ 7233 static void sym_getclock (hcb_p np, int mult) 7234 { 7235 unsigned char scntl3 = np->sv_scntl3; 7236 unsigned char stest1 = np->sv_stest1; 7237 unsigned f1; 7238 7239 /* 7240 * For the C10 core, assume 40 MHz. 7241 */ 7242 if (np->features & FE_C10) { 7243 np->multiplier = mult; 7244 np->clock_khz = 40000 * mult; 7245 return; 7246 } 7247 7248 np->multiplier = 1; 7249 f1 = 40000; 7250 /* 7251 * True with 875/895/896/895A with clock multiplier selected 7252 */ 7253 if (mult > 1 && (stest1 & (DBLEN+DBLSEL)) == DBLEN+DBLSEL) { 7254 if (sym_verbose >= 2) 7255 printf ("%s: clock multiplier found\n", sym_name(np)); 7256 np->multiplier = mult; 7257 } 7258 7259 /* 7260 * If multiplier not found or scntl3 not 7,5,3, 7261 * reset chip and get frequency from general purpose timer. 7262 * Otherwise trust scntl3 BIOS setting. 7263 */ 7264 if (np->multiplier != mult || (scntl3 & 7) < 3 || !(scntl3 & 1)) { 7265 OUTB (nc_stest1, 0); /* make sure doubler is OFF */ 7266 f1 = sym_getfreq (np); 7267 7268 if (sym_verbose) 7269 printf ("%s: chip clock is %uKHz\n", sym_name(np), f1); 7270 7271 if (f1 < 45000) f1 = 40000; 7272 else if (f1 < 55000) f1 = 50000; 7273 else f1 = 80000; 7274 7275 if (f1 < 80000 && mult > 1) { 7276 if (sym_verbose >= 2) 7277 printf ("%s: clock multiplier assumed\n", 7278 sym_name(np)); 7279 np->multiplier = mult; 7280 } 7281 } else { 7282 if ((scntl3 & 7) == 3) f1 = 40000; 7283 else if ((scntl3 & 7) == 5) f1 = 80000; 7284 else f1 = 160000; 7285 7286 f1 /= np->multiplier; 7287 } 7288 7289 /* 7290 * Compute controller synchronous parameters. 7291 */ 7292 f1 *= np->multiplier; 7293 np->clock_khz = f1; 7294 } 7295 7296 /* 7297 * Get/probe PCI clock frequency 7298 */ 7299 static int sym_getpciclock (hcb_p np) 7300 { 7301 int f = 0; 7302 7303 /* 7304 * For the C1010-33, this doesn't work. 7305 * For the C1010-66, this will be tested when I'll have 7306 * such a beast to play with. 7307 */ 7308 if (!(np->features & FE_C10)) { 7309 OUTB (nc_stest1, SCLK); /* Use the PCI clock as SCSI clock */ 7310 f = (int) sym_getfreq (np); 7311 OUTB (nc_stest1, 0); 7312 } 7313 np->pciclk_khz = f; 7314 7315 return f; 7316 } 7317 7318 /*============= DRIVER ACTION/COMPLETION ====================*/ 7319 7320 /* 7321 * Print something that tells about extended errors. 7322 */ 7323 static void sym_print_xerr(ccb_p cp, int x_status) 7324 { 7325 if (x_status & XE_PARITY_ERR) { 7326 PRINT_ADDR(cp); 7327 printf ("unrecovered SCSI parity error.\n"); 7328 } 7329 if (x_status & XE_EXTRA_DATA) { 7330 PRINT_ADDR(cp); 7331 printf ("extraneous data discarded.\n"); 7332 } 7333 if (x_status & XE_BAD_PHASE) { 7334 PRINT_ADDR(cp); 7335 printf ("illegal scsi phase (4/5).\n"); 7336 } 7337 if (x_status & XE_SODL_UNRUN) { 7338 PRINT_ADDR(cp); 7339 printf ("ODD transfer in DATA OUT phase.\n"); 7340 } 7341 if (x_status & XE_SWIDE_OVRUN) { 7342 PRINT_ADDR(cp); 7343 printf ("ODD transfer in DATA IN phase.\n"); 7344 } 7345 } 7346 7347 /* 7348 * Choose the more appropriate CAM status if 7349 * the IO encountered an extended error. 7350 */ 7351 static int sym_xerr_cam_status(int cam_status, int x_status) 7352 { 7353 if (x_status) { 7354 if (x_status & XE_PARITY_ERR) 7355 cam_status = CAM_UNCOR_PARITY; 7356 else if (x_status &(XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN)) 7357 cam_status = CAM_DATA_RUN_ERR; 7358 else if (x_status & XE_BAD_PHASE) 7359 cam_status = CAM_REQ_CMP_ERR; 7360 else 7361 cam_status = CAM_REQ_CMP_ERR; 7362 } 7363 return cam_status; 7364 } 7365 7366 /* 7367 * Complete execution of a SCSI command with extented 7368 * error, SCSI status error, or having been auto-sensed. 7369 * 7370 * The SCRIPTS processor is not running there, so we 7371 * can safely access IO registers and remove JOBs from 7372 * the START queue. 7373 * SCRATCHA is assumed to have been loaded with STARTPOS 7374 * before the SCRIPTS called the C code. 7375 */ 7376 static void sym_complete_error (hcb_p np, ccb_p cp) 7377 { 7378 struct ccb_scsiio *csio; 7379 u_int cam_status; 7380 int i; 7381 7382 /* 7383 * Paranoid check. :) 7384 */ 7385 if (!cp || !cp->cam_ccb) 7386 return; 7387 7388 if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_RESULT)) { 7389 printf ("CCB=%lx STAT=%x/%x/%x DEV=%d/%d\n", (unsigned long)cp, 7390 cp->host_status, cp->ssss_status, cp->host_flags, 7391 cp->target, cp->lun); 7392 MDELAY(100); 7393 } 7394 7395 /* 7396 * Get CAM command pointer. 7397 */ 7398 csio = &cp->cam_ccb->csio; 7399 7400 /* 7401 * Check for extended errors. 7402 */ 7403 if (cp->xerr_status) { 7404 if (sym_verbose) 7405 sym_print_xerr(cp, cp->xerr_status); 7406 if (cp->host_status == HS_COMPLETE) 7407 cp->host_status = HS_COMP_ERR; 7408 } 7409 7410 /* 7411 * Calculate the residual. 7412 */ 7413 csio->sense_resid = 0; 7414 csio->resid = sym_compute_residual(np, cp); 7415 7416 if (!SYM_CONF_RESIDUAL_SUPPORT) {/* If user does not want residuals */ 7417 csio->resid = 0; /* throw them away. :) */ 7418 cp->sv_resid = 0; 7419 } 7420 7421 if (cp->host_flags & HF_SENSE) { /* Auto sense */ 7422 csio->scsi_status = cp->sv_scsi_status; /* Restore status */ 7423 csio->sense_resid = csio->resid; /* Swap residuals */ 7424 csio->resid = cp->sv_resid; 7425 cp->sv_resid = 0; 7426 if (sym_verbose && cp->sv_xerr_status) 7427 sym_print_xerr(cp, cp->sv_xerr_status); 7428 if (cp->host_status == HS_COMPLETE && 7429 cp->ssss_status == S_GOOD && 7430 cp->xerr_status == 0) { 7431 cam_status = sym_xerr_cam_status(CAM_SCSI_STATUS_ERROR, 7432 cp->sv_xerr_status); 7433 cam_status |= CAM_AUTOSNS_VALID; 7434 /* 7435 * Bounce back the sense data to user and 7436 * fix the residual. 7437 */ 7438 bzero(&csio->sense_data, csio->sense_len); 7439 bcopy(cp->sns_bbuf, &csio->sense_data, 7440 MIN(csio->sense_len, SYM_SNS_BBUF_LEN)); 7441 csio->sense_resid += csio->sense_len; 7442 csio->sense_resid -= SYM_SNS_BBUF_LEN; 7443 #if 0 7444 /* 7445 * If the device reports a UNIT ATTENTION condition 7446 * due to a RESET condition, we should consider all 7447 * disconnect CCBs for this unit as aborted. 7448 */ 7449 if (1) { 7450 u_char *p; 7451 p = (u_char *) csio->sense_data; 7452 if (p[0]==0x70 && p[2]==0x6 && p[12]==0x29) 7453 sym_clear_tasks(np, CAM_REQ_ABORTED, 7454 cp->target,cp->lun, -1); 7455 } 7456 #endif 7457 } 7458 else 7459 cam_status = CAM_AUTOSENSE_FAIL; 7460 } 7461 else if (cp->host_status == HS_COMPLETE) { /* Bad SCSI status */ 7462 csio->scsi_status = cp->ssss_status; 7463 cam_status = CAM_SCSI_STATUS_ERROR; 7464 } 7465 else if (cp->host_status == HS_SEL_TIMEOUT) /* Selection timeout */ 7466 cam_status = CAM_SEL_TIMEOUT; 7467 else if (cp->host_status == HS_UNEXPECTED) /* Unexpected BUS FREE*/ 7468 cam_status = CAM_UNEXP_BUSFREE; 7469 else { /* Extended error */ 7470 if (sym_verbose) { 7471 PRINT_ADDR(cp); 7472 printf ("COMMAND FAILED (%x %x %x).\n", 7473 cp->host_status, cp->ssss_status, 7474 cp->xerr_status); 7475 } 7476 csio->scsi_status = cp->ssss_status; 7477 /* 7478 * Set the most appropriate value for CAM status. 7479 */ 7480 cam_status = sym_xerr_cam_status(CAM_REQ_CMP_ERR, 7481 cp->xerr_status); 7482 } 7483 7484 /* 7485 * Dequeue all queued CCBs for that device 7486 * not yet started by SCRIPTS. 7487 */ 7488 i = (INL (nc_scratcha) - np->squeue_ba) / 4; 7489 (void) sym_dequeue_from_squeue(np, i, cp->target, cp->lun, -1); 7490 7491 /* 7492 * Restart the SCRIPTS processor. 7493 */ 7494 OUTL_DSP (SCRIPTA_BA (np, start)); 7495 7496 #ifdef FreeBSD_Bus_Dma_Abstraction 7497 /* 7498 * Synchronize DMA map if needed. 7499 */ 7500 if (cp->dmamapped) { 7501 bus_dmamap_sync(np->data_dmat, cp->dmamap, 7502 (cp->dmamapped == SYM_DMA_READ ? 7503 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE)); 7504 } 7505 #endif 7506 /* 7507 * Add this one to the COMP queue. 7508 * Complete all those commands with either error 7509 * or requeue condition. 7510 */ 7511 sym_set_cam_status((union ccb *) csio, cam_status); 7512 sym_remque(&cp->link_ccbq); 7513 sym_insque_head(&cp->link_ccbq, &np->comp_ccbq); 7514 sym_flush_comp_queue(np, 0); 7515 } 7516 7517 /* 7518 * Complete execution of a successful SCSI command. 7519 * 7520 * Only successful commands go to the DONE queue, 7521 * since we need to have the SCRIPTS processor 7522 * stopped on any error condition. 7523 * The SCRIPTS processor is running while we are 7524 * completing successful commands. 7525 */ 7526 static void sym_complete_ok (hcb_p np, ccb_p cp) 7527 { 7528 struct ccb_scsiio *csio; 7529 tcb_p tp; 7530 lcb_p lp; 7531 7532 /* 7533 * Paranoid check. :) 7534 */ 7535 if (!cp || !cp->cam_ccb) 7536 return; 7537 assert (cp->host_status == HS_COMPLETE); 7538 7539 /* 7540 * Get command, target and lun pointers. 7541 */ 7542 csio = &cp->cam_ccb->csio; 7543 tp = &np->target[cp->target]; 7544 lp = sym_lp(np, tp, cp->lun); 7545 7546 /* 7547 * Assume device discovered on first success. 7548 */ 7549 if (!lp) 7550 sym_set_bit(tp->lun_map, cp->lun); 7551 7552 /* 7553 * If all data have been transferred, given than no 7554 * extended error did occur, there is no residual. 7555 */ 7556 csio->resid = 0; 7557 if (cp->phys.head.lastp != cp->phys.head.goalp) 7558 csio->resid = sym_compute_residual(np, cp); 7559 7560 /* 7561 * Wrong transfer residuals may be worse than just always 7562 * returning zero. User can disable this feature from 7563 * sym_conf.h. Residual support is enabled by default. 7564 */ 7565 if (!SYM_CONF_RESIDUAL_SUPPORT) 7566 csio->resid = 0; 7567 7568 #ifdef FreeBSD_Bus_Dma_Abstraction 7569 /* 7570 * Synchronize DMA map if needed. 7571 */ 7572 if (cp->dmamapped) { 7573 bus_dmamap_sync(np->data_dmat, cp->dmamap, 7574 (cp->dmamapped == SYM_DMA_READ ? 7575 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE)); 7576 } 7577 #endif 7578 /* 7579 * Set status and complete the command. 7580 */ 7581 csio->scsi_status = cp->ssss_status; 7582 sym_set_cam_status((union ccb *) csio, CAM_REQ_CMP); 7583 sym_free_ccb (np, cp); 7584 sym_xpt_done(np, (union ccb *) csio); 7585 } 7586 7587 /* 7588 * Our timeout handler. 7589 */ 7590 static void sym_timeout1(void *arg) 7591 { 7592 union ccb *ccb = (union ccb *) arg; 7593 hcb_p np = ccb->ccb_h.sym_hcb_ptr; 7594 7595 /* 7596 * Check that the CAM CCB is still queued. 7597 */ 7598 if (!np) 7599 return; 7600 7601 switch(ccb->ccb_h.func_code) { 7602 case XPT_SCSI_IO: 7603 (void) sym_abort_scsiio(np, ccb, 1); 7604 break; 7605 default: 7606 break; 7607 } 7608 } 7609 7610 static void sym_timeout(void *arg) 7611 { 7612 int s = splcam(); 7613 sym_timeout1(arg); 7614 splx(s); 7615 } 7616 7617 /* 7618 * Abort an SCSI IO. 7619 */ 7620 static int sym_abort_scsiio(hcb_p np, union ccb *ccb, int timed_out) 7621 { 7622 ccb_p cp; 7623 SYM_QUEHEAD *qp; 7624 7625 /* 7626 * Look up our CCB control block. 7627 */ 7628 cp = 0; 7629 FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) { 7630 ccb_p cp2 = sym_que_entry(qp, struct sym_ccb, link_ccbq); 7631 if (cp2->cam_ccb == ccb) { 7632 cp = cp2; 7633 break; 7634 } 7635 } 7636 if (!cp || cp->host_status == HS_WAIT) 7637 return -1; 7638 7639 /* 7640 * If a previous abort didn't succeed in time, 7641 * perform a BUS reset. 7642 */ 7643 if (cp->to_abort) { 7644 sym_reset_scsi_bus(np, 1); 7645 return 0; 7646 } 7647 7648 /* 7649 * Mark the CCB for abort and allow time for. 7650 */ 7651 cp->to_abort = timed_out ? 2 : 1; 7652 ccb->ccb_h.timeout_ch = timeout(sym_timeout, (caddr_t) ccb, 10*hz); 7653 7654 /* 7655 * Tell the SCRIPTS processor to stop and synchronize with us. 7656 */ 7657 np->istat_sem = SEM; 7658 OUTB (nc_istat, SIGP|SEM); 7659 return 0; 7660 } 7661 7662 /* 7663 * Reset a SCSI device (all LUNs of a target). 7664 */ 7665 static void sym_reset_dev(hcb_p np, union ccb *ccb) 7666 { 7667 tcb_p tp; 7668 struct ccb_hdr *ccb_h = &ccb->ccb_h; 7669 7670 if (ccb_h->target_id == np->myaddr || 7671 ccb_h->target_id >= SYM_CONF_MAX_TARGET || 7672 ccb_h->target_lun >= SYM_CONF_MAX_LUN) { 7673 sym_xpt_done2(np, ccb, CAM_DEV_NOT_THERE); 7674 return; 7675 } 7676 7677 tp = &np->target[ccb_h->target_id]; 7678 7679 tp->to_reset = 1; 7680 sym_xpt_done2(np, ccb, CAM_REQ_CMP); 7681 7682 np->istat_sem = SEM; 7683 OUTB (nc_istat, SIGP|SEM); 7684 return; 7685 } 7686 7687 /* 7688 * SIM action entry point. 7689 */ 7690 static void sym_action(struct cam_sim *sim, union ccb *ccb) 7691 { 7692 int s = splcam(); 7693 sym_action1(sim, ccb); 7694 splx(s); 7695 } 7696 7697 static void sym_action1(struct cam_sim *sim, union ccb *ccb) 7698 { 7699 hcb_p np; 7700 tcb_p tp; 7701 lcb_p lp; 7702 ccb_p cp; 7703 int tmp; 7704 u_char idmsg, *msgptr; 7705 u_int msglen; 7706 struct ccb_scsiio *csio; 7707 struct ccb_hdr *ccb_h; 7708 7709 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("sym_action\n")); 7710 7711 /* 7712 * Retrieve our controller data structure. 7713 */ 7714 np = (hcb_p) cam_sim_softc(sim); 7715 7716 /* 7717 * The common case is SCSI IO. 7718 * We deal with other ones elsewhere. 7719 */ 7720 if (ccb->ccb_h.func_code != XPT_SCSI_IO) { 7721 sym_action2(sim, ccb); 7722 return; 7723 } 7724 csio = &ccb->csio; 7725 ccb_h = &csio->ccb_h; 7726 7727 /* 7728 * Work around races. 7729 */ 7730 if ((ccb_h->status & CAM_STATUS_MASK) != CAM_REQ_INPROG) { 7731 xpt_done(ccb); 7732 return; 7733 } 7734 7735 /* 7736 * Minimal checkings, so that we will not 7737 * go outside our tables. 7738 */ 7739 if (ccb_h->target_id == np->myaddr || 7740 ccb_h->target_id >= SYM_CONF_MAX_TARGET || 7741 ccb_h->target_lun >= SYM_CONF_MAX_LUN) { 7742 sym_xpt_done2(np, ccb, CAM_DEV_NOT_THERE); 7743 return; 7744 } 7745 7746 /* 7747 * Retreive the target and lun descriptors. 7748 */ 7749 tp = &np->target[ccb_h->target_id]; 7750 lp = sym_lp(np, tp, ccb_h->target_lun); 7751 7752 /* 7753 * Complete the 1st INQUIRY command with error 7754 * condition if the device is flagged NOSCAN 7755 * at BOOT in the NVRAM. This may speed up 7756 * the boot and maintain coherency with BIOS 7757 * device numbering. Clearing the flag allows 7758 * user to rescan skipped devices later. 7759 * We also return error for devices not flagged 7760 * for SCAN LUNS in the NVRAM since some mono-lun 7761 * devices behave badly when asked for some non 7762 * zero LUN. Btw, this is an absolute hack.:-) 7763 */ 7764 if (!(ccb_h->flags & CAM_CDB_PHYS) && 7765 (0x12 == ((ccb_h->flags & CAM_CDB_POINTER) ? 7766 csio->cdb_io.cdb_ptr[0] : csio->cdb_io.cdb_bytes[0]))) { 7767 if ((tp->usrflags & SYM_SCAN_BOOT_DISABLED) || 7768 ((tp->usrflags & SYM_SCAN_LUNS_DISABLED) && 7769 ccb_h->target_lun != 0)) { 7770 tp->usrflags &= ~SYM_SCAN_BOOT_DISABLED; 7771 sym_xpt_done2(np, ccb, CAM_DEV_NOT_THERE); 7772 return; 7773 } 7774 } 7775 7776 /* 7777 * Get a control block for this IO. 7778 */ 7779 tmp = ((ccb_h->flags & CAM_TAG_ACTION_VALID) != 0); 7780 cp = sym_get_ccb(np, ccb_h->target_id, ccb_h->target_lun, tmp); 7781 if (!cp) { 7782 sym_xpt_done2(np, ccb, CAM_RESRC_UNAVAIL); 7783 return; 7784 } 7785 7786 /* 7787 * Keep track of the IO in our CCB. 7788 */ 7789 cp->cam_ccb = ccb; 7790 7791 /* 7792 * Build the IDENTIFY message. 7793 */ 7794 idmsg = M_IDENTIFY | cp->lun; 7795 if (cp->tag != NO_TAG || (lp && (lp->current_flags & SYM_DISC_ENABLED))) 7796 idmsg |= 0x40; 7797 7798 msgptr = cp->scsi_smsg; 7799 msglen = 0; 7800 msgptr[msglen++] = idmsg; 7801 7802 /* 7803 * Build the tag message if present. 7804 */ 7805 if (cp->tag != NO_TAG) { 7806 u_char order = csio->tag_action; 7807 7808 switch(order) { 7809 case M_ORDERED_TAG: 7810 break; 7811 case M_HEAD_TAG: 7812 break; 7813 default: 7814 order = M_SIMPLE_TAG; 7815 } 7816 msgptr[msglen++] = order; 7817 7818 /* 7819 * For less than 128 tags, actual tags are numbered 7820 * 1,3,5,..2*MAXTAGS+1,since we may have to deal 7821 * with devices that have problems with #TAG 0 or too 7822 * great #TAG numbers. For more tags (up to 256), 7823 * we use directly our tag number. 7824 */ 7825 #if SYM_CONF_MAX_TASK > (512/4) 7826 msgptr[msglen++] = cp->tag; 7827 #else 7828 msgptr[msglen++] = (cp->tag << 1) + 1; 7829 #endif 7830 } 7831 7832 /* 7833 * Build a negotiation message if needed. 7834 * (nego_status is filled by sym_prepare_nego()) 7835 */ 7836 cp->nego_status = 0; 7837 if (tp->tinfo.current.width != tp->tinfo.goal.width || 7838 tp->tinfo.current.period != tp->tinfo.goal.period || 7839 tp->tinfo.current.offset != tp->tinfo.goal.offset || 7840 tp->tinfo.current.options != tp->tinfo.goal.options) { 7841 if (!tp->nego_cp && lp) 7842 msglen += sym_prepare_nego(np, cp, 0, msgptr + msglen); 7843 } 7844 7845 /* 7846 * Fill in our ccb 7847 */ 7848 7849 /* 7850 * Startqueue 7851 */ 7852 cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA (np, select)); 7853 cp->phys.head.go.restart = cpu_to_scr(SCRIPTA_BA (np, resel_dsa)); 7854 7855 /* 7856 * select 7857 */ 7858 cp->phys.select.sel_id = cp->target; 7859 cp->phys.select.sel_scntl3 = tp->head.wval; 7860 cp->phys.select.sel_sxfer = tp->head.sval; 7861 cp->phys.select.sel_scntl4 = tp->head.uval; 7862 7863 /* 7864 * message 7865 */ 7866 cp->phys.smsg.addr = cpu_to_scr(CCB_BA (cp, scsi_smsg)); 7867 cp->phys.smsg.size = cpu_to_scr(msglen); 7868 7869 /* 7870 * command 7871 */ 7872 if (sym_setup_cdb(np, csio, cp) < 0) { 7873 sym_free_ccb(np, cp); 7874 sym_xpt_done(np, ccb); 7875 return; 7876 } 7877 7878 /* 7879 * status 7880 */ 7881 #if 0 /* Provision */ 7882 cp->actualquirks = tp->quirks; 7883 #endif 7884 cp->actualquirks = SYM_QUIRK_AUTOSAVE; 7885 cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY; 7886 cp->ssss_status = S_ILLEGAL; 7887 cp->xerr_status = 0; 7888 cp->host_flags = 0; 7889 cp->extra_bytes = 0; 7890 7891 /* 7892 * extreme data pointer. 7893 * shall be positive, so -1 is lower than lowest.:) 7894 */ 7895 cp->ext_sg = -1; 7896 cp->ext_ofs = 0; 7897 7898 /* 7899 * Build the data descriptor block 7900 * and start the IO. 7901 */ 7902 sym_setup_data_and_start(np, csio, cp); 7903 } 7904 7905 /* 7906 * Setup buffers and pointers that address the CDB. 7907 * I bet, physical CDBs will never be used on the planet, 7908 * since they can be bounced without significant overhead. 7909 */ 7910 static int sym_setup_cdb(hcb_p np, struct ccb_scsiio *csio, ccb_p cp) 7911 { 7912 struct ccb_hdr *ccb_h; 7913 u32 cmd_ba; 7914 int cmd_len; 7915 7916 ccb_h = &csio->ccb_h; 7917 7918 /* 7919 * CDB is 16 bytes max. 7920 */ 7921 if (csio->cdb_len > sizeof(cp->cdb_buf)) { 7922 sym_set_cam_status(cp->cam_ccb, CAM_REQ_INVALID); 7923 return -1; 7924 } 7925 cmd_len = csio->cdb_len; 7926 7927 if (ccb_h->flags & CAM_CDB_POINTER) { 7928 /* CDB is a pointer */ 7929 if (!(ccb_h->flags & CAM_CDB_PHYS)) { 7930 /* CDB pointer is virtual */ 7931 bcopy(csio->cdb_io.cdb_ptr, cp->cdb_buf, cmd_len); 7932 cmd_ba = CCB_BA (cp, cdb_buf[0]); 7933 } else { 7934 /* CDB pointer is physical */ 7935 #if 0 7936 cmd_ba = ((u32)csio->cdb_io.cdb_ptr) & 0xffffffff; 7937 #else 7938 sym_set_cam_status(cp->cam_ccb, CAM_REQ_INVALID); 7939 return -1; 7940 #endif 7941 } 7942 } else { 7943 /* CDB is in the CAM ccb (buffer) */ 7944 bcopy(csio->cdb_io.cdb_bytes, cp->cdb_buf, cmd_len); 7945 cmd_ba = CCB_BA (cp, cdb_buf[0]); 7946 } 7947 7948 cp->phys.cmd.addr = cpu_to_scr(cmd_ba); 7949 cp->phys.cmd.size = cpu_to_scr(cmd_len); 7950 7951 return 0; 7952 } 7953 7954 /* 7955 * Set up data pointers used by SCRIPTS. 7956 */ 7957 static void __inline 7958 sym_setup_data_pointers(hcb_p np, ccb_p cp, int dir) 7959 { 7960 u32 lastp, goalp; 7961 7962 /* 7963 * No segments means no data. 7964 */ 7965 if (!cp->segments) 7966 dir = CAM_DIR_NONE; 7967 7968 /* 7969 * Set the data pointer. 7970 */ 7971 switch(dir) { 7972 case CAM_DIR_OUT: 7973 goalp = SCRIPTA_BA (np, data_out2) + 8; 7974 lastp = goalp - 8 - (cp->segments * (2*4)); 7975 break; 7976 case CAM_DIR_IN: 7977 cp->host_flags |= HF_DATA_IN; 7978 goalp = SCRIPTA_BA (np, data_in2) + 8; 7979 lastp = goalp - 8 - (cp->segments * (2*4)); 7980 break; 7981 case CAM_DIR_NONE: 7982 default: 7983 lastp = goalp = SCRIPTB_BA (np, no_data); 7984 break; 7985 } 7986 7987 cp->phys.head.lastp = cpu_to_scr(lastp); 7988 cp->phys.head.goalp = cpu_to_scr(goalp); 7989 cp->phys.head.savep = cpu_to_scr(lastp); 7990 cp->startp = cp->phys.head.savep; 7991 } 7992 7993 7994 #ifdef FreeBSD_Bus_Dma_Abstraction 7995 /* 7996 * Call back routine for the DMA map service. 7997 * If bounce buffers are used (why ?), we may sleep and then 7998 * be called there in another context. 7999 */ 8000 static void 8001 sym_execute_ccb(void *arg, bus_dma_segment_t *psegs, int nsegs, int error) 8002 { 8003 ccb_p cp; 8004 hcb_p np; 8005 union ccb *ccb; 8006 int s; 8007 8008 s = splcam(); 8009 8010 cp = (ccb_p) arg; 8011 ccb = cp->cam_ccb; 8012 np = (hcb_p) cp->arg; 8013 8014 /* 8015 * Deal with weird races. 8016 */ 8017 if (sym_get_cam_status(ccb) != CAM_REQ_INPROG) 8018 goto out_abort; 8019 8020 /* 8021 * Deal with weird errors. 8022 */ 8023 if (error) { 8024 cp->dmamapped = 0; 8025 sym_set_cam_status(cp->cam_ccb, CAM_REQ_ABORTED); 8026 goto out_abort; 8027 } 8028 8029 /* 8030 * Build the data descriptor for the chip. 8031 */ 8032 if (nsegs) { 8033 int retv; 8034 /* 896 rev 1 requires to be careful about boundaries */ 8035 if (np->device_id == PCI_ID_SYM53C896 && np->revision_id <= 1) 8036 retv = sym_scatter_sg_physical(np, cp, psegs, nsegs); 8037 else 8038 retv = sym_fast_scatter_sg_physical(np,cp, psegs,nsegs); 8039 if (retv < 0) { 8040 sym_set_cam_status(cp->cam_ccb, CAM_REQ_TOO_BIG); 8041 goto out_abort; 8042 } 8043 } 8044 8045 /* 8046 * Synchronize the DMA map only if we have 8047 * actually mapped the data. 8048 */ 8049 if (cp->dmamapped) { 8050 bus_dmamap_sync(np->data_dmat, cp->dmamap, 8051 (cp->dmamapped == SYM_DMA_READ ? 8052 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE)); 8053 } 8054 8055 /* 8056 * Set host status to busy state. 8057 * May have been set back to HS_WAIT to avoid a race. 8058 */ 8059 cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY; 8060 8061 /* 8062 * Set data pointers. 8063 */ 8064 sym_setup_data_pointers(np, cp, (ccb->ccb_h.flags & CAM_DIR_MASK)); 8065 8066 /* 8067 * Enqueue this IO in our pending queue. 8068 */ 8069 sym_enqueue_cam_ccb(np, ccb); 8070 8071 /* 8072 * When `#ifed 1', the code below makes the driver 8073 * panic on the first attempt to write to a SCSI device. 8074 * It is the first test we want to do after a driver 8075 * change that does not seem obviously safe. :) 8076 */ 8077 #if 0 8078 switch (cp->cdb_buf[0]) { 8079 case 0x0A: case 0x2A: case 0xAA: 8080 panic("XXXXXXXXXXXXX WRITE NOT YET ALLOWED XXXXXXXXXXXXXX\n"); 8081 MDELAY(10000); 8082 break; 8083 default: 8084 break; 8085 } 8086 #endif 8087 /* 8088 * Activate this job. 8089 */ 8090 sym_put_start_queue(np, cp); 8091 out: 8092 splx(s); 8093 return; 8094 out_abort: 8095 sym_free_ccb(np, cp); 8096 sym_xpt_done(np, ccb); 8097 goto out; 8098 } 8099 8100 /* 8101 * How complex it gets to deal with the data in CAM. 8102 * The Bus Dma stuff makes things still more complex. 8103 */ 8104 static void 8105 sym_setup_data_and_start(hcb_p np, struct ccb_scsiio *csio, ccb_p cp) 8106 { 8107 struct ccb_hdr *ccb_h; 8108 int dir, retv; 8109 8110 ccb_h = &csio->ccb_h; 8111 8112 /* 8113 * Now deal with the data. 8114 */ 8115 cp->data_len = csio->dxfer_len; 8116 cp->arg = np; 8117 8118 /* 8119 * No direction means no data. 8120 */ 8121 dir = (ccb_h->flags & CAM_DIR_MASK); 8122 if (dir == CAM_DIR_NONE) { 8123 sym_execute_ccb(cp, NULL, 0, 0); 8124 return; 8125 } 8126 8127 if (!(ccb_h->flags & CAM_SCATTER_VALID)) { 8128 /* Single buffer */ 8129 if (!(ccb_h->flags & CAM_DATA_PHYS)) { 8130 /* Buffer is virtual */ 8131 int s; 8132 8133 cp->dmamapped = (dir == CAM_DIR_IN) ? 8134 SYM_DMA_READ : SYM_DMA_WRITE; 8135 s = splsoftvm(); 8136 retv = bus_dmamap_load(np->data_dmat, cp->dmamap, 8137 csio->data_ptr, csio->dxfer_len, 8138 sym_execute_ccb, cp, 0); 8139 if (retv == EINPROGRESS) { 8140 cp->host_status = HS_WAIT; 8141 xpt_freeze_simq(np->sim, 1); 8142 csio->ccb_h.status |= CAM_RELEASE_SIMQ; 8143 } 8144 splx(s); 8145 } else { 8146 /* Buffer is physical */ 8147 struct bus_dma_segment seg; 8148 8149 seg.ds_addr = (bus_addr_t) csio->data_ptr; 8150 sym_execute_ccb(cp, &seg, 1, 0); 8151 } 8152 } else { 8153 /* Scatter/gather list */ 8154 struct bus_dma_segment *segs; 8155 8156 if ((ccb_h->flags & CAM_SG_LIST_PHYS) != 0) { 8157 /* The SG list pointer is physical */ 8158 sym_set_cam_status(cp->cam_ccb, CAM_REQ_INVALID); 8159 goto out_abort; 8160 } 8161 8162 if (!(ccb_h->flags & CAM_DATA_PHYS)) { 8163 /* SG buffer pointers are virtual */ 8164 sym_set_cam_status(cp->cam_ccb, CAM_REQ_INVALID); 8165 goto out_abort; 8166 } 8167 8168 /* SG buffer pointers are physical */ 8169 segs = (struct bus_dma_segment *)csio->data_ptr; 8170 sym_execute_ccb(cp, segs, csio->sglist_cnt, 0); 8171 } 8172 return; 8173 out_abort: 8174 sym_free_ccb(np, cp); 8175 sym_xpt_done(np, (union ccb *) csio); 8176 } 8177 8178 /* 8179 * Move the scatter list to our data block. 8180 */ 8181 static int 8182 sym_fast_scatter_sg_physical(hcb_p np, ccb_p cp, 8183 bus_dma_segment_t *psegs, int nsegs) 8184 { 8185 struct sym_tblmove *data; 8186 bus_dma_segment_t *psegs2; 8187 8188 if (nsegs > SYM_CONF_MAX_SG) 8189 return -1; 8190 8191 data = &cp->phys.data[SYM_CONF_MAX_SG-1]; 8192 psegs2 = &psegs[nsegs-1]; 8193 cp->segments = nsegs; 8194 8195 while (1) { 8196 data->addr = cpu_to_scr(psegs2->ds_addr); 8197 data->size = cpu_to_scr(psegs2->ds_len); 8198 if (DEBUG_FLAGS & DEBUG_SCATTER) { 8199 printf ("%s scatter: paddr=%lx len=%ld\n", 8200 sym_name(np), (long) psegs2->ds_addr, 8201 (long) psegs2->ds_len); 8202 } 8203 if (psegs2 != psegs) { 8204 --data; 8205 --psegs2; 8206 continue; 8207 } 8208 break; 8209 } 8210 return 0; 8211 } 8212 8213 #else /* FreeBSD_Bus_Dma_Abstraction */ 8214 8215 /* 8216 * How complex it gets to deal with the data in CAM. 8217 * Variant without the Bus Dma Abstraction option. 8218 */ 8219 static void 8220 sym_setup_data_and_start(hcb_p np, struct ccb_scsiio *csio, ccb_p cp) 8221 { 8222 struct ccb_hdr *ccb_h; 8223 int dir, retv; 8224 8225 ccb_h = &csio->ccb_h; 8226 8227 /* 8228 * Now deal with the data. 8229 */ 8230 cp->data_len = 0; 8231 cp->segments = 0; 8232 8233 /* 8234 * No direction means no data. 8235 */ 8236 dir = (ccb_h->flags & CAM_DIR_MASK); 8237 if (dir == CAM_DIR_NONE) 8238 goto end_scatter; 8239 8240 if (!(ccb_h->flags & CAM_SCATTER_VALID)) { 8241 /* Single buffer */ 8242 if (!(ccb_h->flags & CAM_DATA_PHYS)) { 8243 /* Buffer is virtual */ 8244 retv = sym_scatter_virtual(np, cp, 8245 (vm_offset_t) csio->data_ptr, 8246 (vm_size_t) csio->dxfer_len); 8247 } else { 8248 /* Buffer is physical */ 8249 retv = sym_scatter_physical(np, cp, 8250 (vm_offset_t) csio->data_ptr, 8251 (vm_size_t) csio->dxfer_len); 8252 } 8253 } else { 8254 /* Scatter/gather list */ 8255 int nsegs; 8256 struct bus_dma_segment *segs; 8257 segs = (struct bus_dma_segment *)csio->data_ptr; 8258 nsegs = csio->sglist_cnt; 8259 8260 if ((ccb_h->flags & CAM_SG_LIST_PHYS) != 0) { 8261 /* The SG list pointer is physical */ 8262 sym_set_cam_status(cp->cam_ccb, CAM_REQ_INVALID); 8263 goto out_abort; 8264 } 8265 if (!(ccb_h->flags & CAM_DATA_PHYS)) { 8266 /* SG buffer pointers are virtual */ 8267 retv = sym_scatter_sg_virtual(np, cp, segs, nsegs); 8268 } else { 8269 /* SG buffer pointers are physical */ 8270 retv = sym_scatter_sg_physical(np, cp, segs, nsegs); 8271 } 8272 } 8273 if (retv < 0) { 8274 sym_set_cam_status(cp->cam_ccb, CAM_REQ_TOO_BIG); 8275 goto out_abort; 8276 } 8277 8278 end_scatter: 8279 /* 8280 * Set data pointers. 8281 */ 8282 sym_setup_data_pointers(np, cp, dir); 8283 8284 /* 8285 * Enqueue this IO in our pending queue. 8286 */ 8287 sym_enqueue_cam_ccb(np, (union ccb *) csio); 8288 8289 /* 8290 * Activate this job. 8291 */ 8292 sym_put_start_queue(np, cp); 8293 8294 /* 8295 * Command is successfully queued. 8296 */ 8297 return; 8298 out_abort: 8299 sym_free_ccb(np, cp); 8300 sym_xpt_done(np, (union ccb *) csio); 8301 } 8302 8303 /* 8304 * Scatter a virtual buffer into bus addressable chunks. 8305 */ 8306 static int 8307 sym_scatter_virtual(hcb_p np, ccb_p cp, vm_offset_t vaddr, vm_size_t len) 8308 { 8309 u_long pe, pn; 8310 u_long n, k; 8311 int s; 8312 8313 cp->data_len += len; 8314 8315 pe = vaddr + len; 8316 n = len; 8317 s = SYM_CONF_MAX_SG - 1 - cp->segments; 8318 8319 while (n && s >= 0) { 8320 pn = (pe - 1) & ~PAGE_MASK; 8321 k = pe - pn; 8322 if (k > n) { 8323 k = n; 8324 pn = pe - n; 8325 } 8326 if (DEBUG_FLAGS & DEBUG_SCATTER) { 8327 printf ("%s scatter: va=%lx pa=%lx siz=%ld\n", 8328 sym_name(np), pn, (u_long) vtobus(pn), k); 8329 } 8330 cp->phys.data[s].addr = cpu_to_scr(vtobus(pn)); 8331 cp->phys.data[s].size = cpu_to_scr(k); 8332 pe = pn; 8333 n -= k; 8334 --s; 8335 } 8336 cp->segments = SYM_CONF_MAX_SG - 1 - s; 8337 8338 return n ? -1 : 0; 8339 } 8340 8341 /* 8342 * Scatter a SG list with virtual addresses into bus addressable chunks. 8343 */ 8344 static int 8345 sym_scatter_sg_virtual(hcb_p np, ccb_p cp, bus_dma_segment_t *psegs, int nsegs) 8346 { 8347 int i, retv = 0; 8348 8349 for (i = nsegs - 1 ; i >= 0 ; --i) { 8350 retv = sym_scatter_virtual(np, cp, 8351 psegs[i].ds_addr, psegs[i].ds_len); 8352 if (retv < 0) 8353 break; 8354 } 8355 return retv; 8356 } 8357 8358 /* 8359 * Scatter a physical buffer into bus addressable chunks. 8360 */ 8361 static int 8362 sym_scatter_physical(hcb_p np, ccb_p cp, vm_offset_t paddr, vm_size_t len) 8363 { 8364 struct bus_dma_segment seg; 8365 8366 seg.ds_addr = paddr; 8367 seg.ds_len = len; 8368 return sym_scatter_sg_physical(np, cp, &seg, 1); 8369 } 8370 8371 #endif /* FreeBSD_Bus_Dma_Abstraction */ 8372 8373 /* 8374 * Scatter a SG list with physical addresses into bus addressable chunks. 8375 * We need to ensure 16MB boundaries not to be crossed during DMA of 8376 * each segment, due to some chips being flawed. 8377 */ 8378 #define BOUND_MASK ((1UL<<24)-1) 8379 static int 8380 sym_scatter_sg_physical(hcb_p np, ccb_p cp, bus_dma_segment_t *psegs, int nsegs) 8381 { 8382 u_long ps, pe, pn; 8383 u_long k; 8384 int s, t; 8385 8386 #ifndef FreeBSD_Bus_Dma_Abstraction 8387 s = SYM_CONF_MAX_SG - 1 - cp->segments; 8388 #else 8389 s = SYM_CONF_MAX_SG - 1; 8390 #endif 8391 t = nsegs - 1; 8392 ps = psegs[t].ds_addr; 8393 pe = ps + psegs[t].ds_len; 8394 8395 while (s >= 0) { 8396 pn = (pe - 1) & ~BOUND_MASK; 8397 if (pn <= ps) 8398 pn = ps; 8399 k = pe - pn; 8400 if (DEBUG_FLAGS & DEBUG_SCATTER) { 8401 printf ("%s scatter: paddr=%lx len=%ld\n", 8402 sym_name(np), pn, k); 8403 } 8404 cp->phys.data[s].addr = cpu_to_scr(pn); 8405 cp->phys.data[s].size = cpu_to_scr(k); 8406 #ifndef FreeBSD_Bus_Dma_Abstraction 8407 cp->data_len += k; 8408 #endif 8409 --s; 8410 if (pn == ps) { 8411 if (--t < 0) 8412 break; 8413 ps = psegs[t].ds_addr; 8414 pe = ps + psegs[t].ds_len; 8415 } 8416 else 8417 pe = pn; 8418 } 8419 8420 cp->segments = SYM_CONF_MAX_SG - 1 - s; 8421 8422 return t >= 0 ? -1 : 0; 8423 } 8424 #undef BOUND_MASK 8425 8426 /* 8427 * SIM action for non performance critical stuff. 8428 */ 8429 static void sym_action2(struct cam_sim *sim, union ccb *ccb) 8430 { 8431 hcb_p np; 8432 tcb_p tp; 8433 lcb_p lp; 8434 struct ccb_hdr *ccb_h; 8435 8436 /* 8437 * Retrieve our controller data structure. 8438 */ 8439 np = (hcb_p) cam_sim_softc(sim); 8440 8441 ccb_h = &ccb->ccb_h; 8442 8443 switch (ccb_h->func_code) { 8444 case XPT_SET_TRAN_SETTINGS: 8445 { 8446 struct ccb_trans_settings *cts; 8447 8448 cts = &ccb->cts; 8449 tp = &np->target[ccb_h->target_id]; 8450 8451 /* 8452 * Update SPI transport settings in TARGET control block. 8453 * Update SCSI device settings in LUN control block. 8454 */ 8455 lp = sym_lp(np, tp, ccb_h->target_lun); 8456 #ifdef FreeBSD_New_Tran_Settings 8457 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) { 8458 #else 8459 if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0) { 8460 #endif 8461 sym_update_trans(np, tp, &tp->tinfo.goal, cts); 8462 if (lp) 8463 sym_update_dflags(np, &lp->current_flags, cts); 8464 } 8465 #ifdef FreeBSD_New_Tran_Settings 8466 if (cts->type == CTS_TYPE_USER_SETTINGS) { 8467 #else 8468 if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) { 8469 #endif 8470 sym_update_trans(np, tp, &tp->tinfo.user, cts); 8471 if (lp) 8472 sym_update_dflags(np, &lp->user_flags, cts); 8473 } 8474 8475 sym_xpt_done2(np, ccb, CAM_REQ_CMP); 8476 break; 8477 } 8478 case XPT_GET_TRAN_SETTINGS: 8479 { 8480 struct ccb_trans_settings *cts; 8481 struct sym_trans *tip; 8482 u_char dflags; 8483 8484 cts = &ccb->cts; 8485 tp = &np->target[ccb_h->target_id]; 8486 lp = sym_lp(np, tp, ccb_h->target_lun); 8487 8488 #ifdef FreeBSD_New_Tran_Settings 8489 #define cts__scsi (&cts->proto_specific.scsi) 8490 #define cts__spi (&cts->xport_specific.spi) 8491 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) { 8492 tip = &tp->tinfo.current; 8493 dflags = lp ? lp->current_flags : 0; 8494 } 8495 else { 8496 tip = &tp->tinfo.user; 8497 dflags = lp ? lp->user_flags : tp->usrflags; 8498 } 8499 8500 cts->protocol = PROTO_SCSI; 8501 cts->transport = XPORT_SPI; 8502 cts->protocol_version = tip->scsi_version; 8503 cts->transport_version = tip->spi_version; 8504 8505 cts__spi->sync_period = tip->period; 8506 cts__spi->sync_offset = tip->offset; 8507 cts__spi->bus_width = tip->width; 8508 cts__spi->ppr_options = tip->options; 8509 8510 cts__spi->valid = CTS_SPI_VALID_SYNC_RATE 8511 | CTS_SPI_VALID_SYNC_OFFSET 8512 | CTS_SPI_VALID_BUS_WIDTH 8513 | CTS_SPI_VALID_PPR_OPTIONS; 8514 8515 cts__spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB; 8516 if (dflags & SYM_DISC_ENABLED) 8517 cts__spi->flags |= CTS_SPI_FLAGS_DISC_ENB; 8518 cts__spi->valid |= CTS_SPI_VALID_DISC; 8519 8520 cts__scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB; 8521 if (dflags & SYM_TAGS_ENABLED) 8522 cts__scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB; 8523 cts__scsi->valid |= CTS_SCSI_VALID_TQ; 8524 #undef cts__spi 8525 #undef cts__scsi 8526 #else 8527 if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0) { 8528 tip = &tp->tinfo.current; 8529 dflags = lp ? lp->current_flags : 0; 8530 } 8531 else { 8532 tip = &tp->tinfo.user; 8533 dflags = lp ? lp->user_flags : tp->usrflags; 8534 } 8535 8536 cts->sync_period = tip->period; 8537 cts->sync_offset = tip->offset; 8538 cts->bus_width = tip->width; 8539 8540 cts->valid = CCB_TRANS_SYNC_RATE_VALID 8541 | CCB_TRANS_SYNC_OFFSET_VALID 8542 | CCB_TRANS_BUS_WIDTH_VALID; 8543 8544 cts->flags &= ~(CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB); 8545 8546 if (dflags & SYM_DISC_ENABLED) 8547 cts->flags |= CCB_TRANS_DISC_ENB; 8548 8549 if (dflags & SYM_TAGS_ENABLED) 8550 cts->flags |= CCB_TRANS_TAG_ENB; 8551 8552 cts->valid |= CCB_TRANS_DISC_VALID; 8553 cts->valid |= CCB_TRANS_TQ_VALID; 8554 #endif 8555 sym_xpt_done2(np, ccb, CAM_REQ_CMP); 8556 break; 8557 } 8558 case XPT_CALC_GEOMETRY: 8559 { 8560 cam_calc_geometry(&ccb->ccg, /*extended*/1); 8561 sym_xpt_done2(np, ccb, CAM_REQ_CMP); 8562 break; 8563 } 8564 case XPT_PATH_INQ: 8565 { 8566 struct ccb_pathinq *cpi = &ccb->cpi; 8567 cpi->version_num = 1; 8568 cpi->hba_inquiry = PI_MDP_ABLE|PI_SDTR_ABLE|PI_TAG_ABLE; 8569 if ((np->features & FE_WIDE) != 0) 8570 cpi->hba_inquiry |= PI_WIDE_16; 8571 cpi->target_sprt = 0; 8572 cpi->hba_misc = 0; 8573 if (np->usrflags & SYM_SCAN_TARGETS_HILO) 8574 cpi->hba_misc |= PIM_SCANHILO; 8575 if (np->usrflags & SYM_AVOID_BUS_RESET) 8576 cpi->hba_misc |= PIM_NOBUSRESET; 8577 cpi->hba_eng_cnt = 0; 8578 cpi->max_target = (np->features & FE_WIDE) ? 15 : 7; 8579 /* Semantic problem:)LUN number max = max number of LUNs - 1 */ 8580 cpi->max_lun = SYM_CONF_MAX_LUN-1; 8581 if (SYM_SETUP_MAX_LUN < SYM_CONF_MAX_LUN) 8582 cpi->max_lun = SYM_SETUP_MAX_LUN-1; 8583 cpi->bus_id = cam_sim_bus(sim); 8584 cpi->initiator_id = np->myaddr; 8585 cpi->base_transfer_speed = 3300; 8586 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 8587 strncpy(cpi->hba_vid, "Symbios", HBA_IDLEN); 8588 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 8589 cpi->unit_number = cam_sim_unit(sim); 8590 8591 #ifdef FreeBSD_New_Tran_Settings 8592 cpi->protocol = PROTO_SCSI; 8593 cpi->protocol_version = SCSI_REV_2; 8594 cpi->transport = XPORT_SPI; 8595 cpi->transport_version = 2; 8596 cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_ST; 8597 if (np->features & FE_ULTRA3) { 8598 cpi->transport_version = 3; 8599 cpi->xport_specific.spi.ppr_options = 8600 SID_SPI_CLOCK_DT_ST; 8601 } 8602 #endif 8603 sym_xpt_done2(np, ccb, CAM_REQ_CMP); 8604 break; 8605 } 8606 case XPT_ABORT: 8607 { 8608 union ccb *abort_ccb = ccb->cab.abort_ccb; 8609 switch(abort_ccb->ccb_h.func_code) { 8610 case XPT_SCSI_IO: 8611 if (sym_abort_scsiio(np, abort_ccb, 0) == 0) { 8612 sym_xpt_done2(np, ccb, CAM_REQ_CMP); 8613 break; 8614 } 8615 default: 8616 sym_xpt_done2(np, ccb, CAM_UA_ABORT); 8617 break; 8618 } 8619 break; 8620 } 8621 case XPT_RESET_DEV: 8622 { 8623 sym_reset_dev(np, ccb); 8624 break; 8625 } 8626 case XPT_RESET_BUS: 8627 { 8628 sym_reset_scsi_bus(np, 0); 8629 if (sym_verbose) { 8630 xpt_print_path(np->path); 8631 printf("SCSI BUS reset delivered.\n"); 8632 } 8633 sym_init (np, 1); 8634 sym_xpt_done2(np, ccb, CAM_REQ_CMP); 8635 break; 8636 } 8637 case XPT_ACCEPT_TARGET_IO: 8638 case XPT_CONT_TARGET_IO: 8639 case XPT_EN_LUN: 8640 case XPT_NOTIFY_ACK: 8641 case XPT_IMMED_NOTIFY: 8642 case XPT_TERM_IO: 8643 default: 8644 sym_xpt_done2(np, ccb, CAM_REQ_INVALID); 8645 break; 8646 } 8647 } 8648 8649 /* 8650 * Asynchronous notification handler. 8651 */ 8652 static void 8653 sym_async(void *cb_arg, u32 code, struct cam_path *path, void *arg) 8654 { 8655 hcb_p np; 8656 struct cam_sim *sim; 8657 u_int tn; 8658 tcb_p tp; 8659 int s; 8660 8661 s = splcam(); 8662 8663 sim = (struct cam_sim *) cb_arg; 8664 np = (hcb_p) cam_sim_softc(sim); 8665 8666 switch (code) { 8667 case AC_LOST_DEVICE: 8668 tn = xpt_path_target_id(path); 8669 if (tn >= SYM_CONF_MAX_TARGET) 8670 break; 8671 8672 tp = &np->target[tn]; 8673 8674 tp->to_reset = 0; 8675 tp->head.sval = 0; 8676 tp->head.wval = np->rv_scntl3; 8677 tp->head.uval = 0; 8678 8679 tp->tinfo.current.period = tp->tinfo.goal.period = 0; 8680 tp->tinfo.current.offset = tp->tinfo.goal.offset = 0; 8681 tp->tinfo.current.width = tp->tinfo.goal.width = BUS_8_BIT; 8682 tp->tinfo.current.options = tp->tinfo.goal.options = 0; 8683 8684 break; 8685 default: 8686 break; 8687 } 8688 8689 splx(s); 8690 } 8691 8692 /* 8693 * Update transfer settings of a target. 8694 */ 8695 static void sym_update_trans(hcb_p np, tcb_p tp, struct sym_trans *tip, 8696 struct ccb_trans_settings *cts) 8697 { 8698 /* 8699 * Update the infos. 8700 */ 8701 #ifdef FreeBSD_New_Tran_Settings 8702 #define cts__spi (&cts->xport_specific.spi) 8703 if ((cts__spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) 8704 tip->width = cts__spi->bus_width; 8705 if ((cts__spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0) 8706 tip->offset = cts__spi->sync_offset; 8707 if ((cts__spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0) 8708 tip->period = cts__spi->sync_period; 8709 if ((cts__spi->valid & CTS_SPI_VALID_PPR_OPTIONS) != 0) 8710 tip->options = (cts__spi->ppr_options & PPR_OPT_DT); 8711 if (cts->protocol_version != PROTO_VERSION_UNSPECIFIED && 8712 cts->protocol_version != PROTO_VERSION_UNKNOWN) 8713 tip->scsi_version = cts->protocol_version; 8714 if (cts->transport_version != XPORT_VERSION_UNSPECIFIED && 8715 cts->transport_version != XPORT_VERSION_UNKNOWN) 8716 tip->spi_version = cts->transport_version; 8717 #undef cts__spi 8718 #else 8719 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) != 0) 8720 tip->width = cts->bus_width; 8721 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0) 8722 tip->offset = cts->sync_offset; 8723 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) != 0) 8724 tip->period = cts->sync_period; 8725 #endif 8726 /* 8727 * Scale against driver configuration limits. 8728 */ 8729 if (tip->width > SYM_SETUP_MAX_WIDE) tip->width = SYM_SETUP_MAX_WIDE; 8730 if (tip->offset > SYM_SETUP_MAX_OFFS) tip->offset = SYM_SETUP_MAX_OFFS; 8731 if (tip->period < SYM_SETUP_MIN_SYNC) tip->period = SYM_SETUP_MIN_SYNC; 8732 8733 /* 8734 * Scale against actual controller BUS width. 8735 */ 8736 if (tip->width > np->maxwide) 8737 tip->width = np->maxwide; 8738 8739 #ifdef FreeBSD_New_Tran_Settings 8740 /* 8741 * Only accept DT if controller supports and SYNC/WIDE asked. 8742 */ 8743 if (!((np->features & (FE_C10|FE_ULTRA3)) == (FE_C10|FE_ULTRA3)) || 8744 !(tip->width == BUS_16_BIT && tip->offset)) { 8745 tip->options &= ~PPR_OPT_DT; 8746 } 8747 #else 8748 /* 8749 * For now, only assume DT if period <= 9, BUS 16 and offset != 0. 8750 */ 8751 tip->options = 0; 8752 if ((np->features & (FE_C10|FE_ULTRA3)) == (FE_C10|FE_ULTRA3) && 8753 tip->period <= 9 && tip->width == BUS_16_BIT && tip->offset) { 8754 tip->options |= PPR_OPT_DT; 8755 } 8756 #endif 8757 8758 /* 8759 * Scale period factor and offset against controller limits. 8760 */ 8761 if (tip->options & PPR_OPT_DT) { 8762 if (tip->period < np->minsync_dt) 8763 tip->period = np->minsync_dt; 8764 if (tip->period > np->maxsync_dt) 8765 tip->period = np->maxsync_dt; 8766 if (tip->offset > np->maxoffs_dt) 8767 tip->offset = np->maxoffs_dt; 8768 } 8769 else { 8770 if (tip->period < np->minsync) 8771 tip->period = np->minsync; 8772 if (tip->period > np->maxsync) 8773 tip->period = np->maxsync; 8774 if (tip->offset > np->maxoffs) 8775 tip->offset = np->maxoffs; 8776 } 8777 } 8778 8779 /* 8780 * Update flags for a device (logical unit). 8781 */ 8782 static void 8783 sym_update_dflags(hcb_p np, u_char *flags, struct ccb_trans_settings *cts) 8784 { 8785 #ifdef FreeBSD_New_Tran_Settings 8786 #define cts__scsi (&cts->proto_specific.scsi) 8787 #define cts__spi (&cts->xport_specific.spi) 8788 if ((cts__spi->valid & CTS_SPI_VALID_DISC) != 0) { 8789 if ((cts__spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0) 8790 *flags |= SYM_DISC_ENABLED; 8791 else 8792 *flags &= ~SYM_DISC_ENABLED; 8793 } 8794 8795 if ((cts__scsi->valid & CTS_SCSI_VALID_TQ) != 0) { 8796 if ((cts__scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) 8797 *flags |= SYM_TAGS_ENABLED; 8798 else 8799 *flags &= ~SYM_TAGS_ENABLED; 8800 } 8801 #undef cts__spi 8802 #undef cts__scsi 8803 #else 8804 if ((cts->valid & CCB_TRANS_DISC_VALID) != 0) { 8805 if ((cts->flags & CCB_TRANS_DISC_ENB) != 0) 8806 *flags |= SYM_DISC_ENABLED; 8807 else 8808 *flags &= ~SYM_DISC_ENABLED; 8809 } 8810 8811 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) { 8812 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) 8813 *flags |= SYM_TAGS_ENABLED; 8814 else 8815 *flags &= ~SYM_TAGS_ENABLED; 8816 } 8817 #endif 8818 } 8819 8820 8821 /*============= DRIVER INITIALISATION ==================*/ 8822 8823 #ifdef FreeBSD_Bus_Io_Abstraction 8824 8825 static device_method_t sym_pci_methods[] = { 8826 DEVMETHOD(device_probe, sym_pci_probe), 8827 DEVMETHOD(device_attach, sym_pci_attach), 8828 { 0, 0 } 8829 }; 8830 8831 static driver_t sym_pci_driver = { 8832 "sym", 8833 sym_pci_methods, 8834 sizeof(struct sym_hcb) 8835 }; 8836 8837 static devclass_t sym_devclass; 8838 8839 DRIVER_MODULE(sym, pci, sym_pci_driver, sym_devclass, 0, 0); 8840 8841 #else /* Pre-FreeBSD_Bus_Io_Abstraction */ 8842 8843 static u_long sym_unit; 8844 8845 static struct pci_device sym_pci_driver = { 8846 "sym", 8847 sym_pci_probe, 8848 sym_pci_attach, 8849 &sym_unit, 8850 NULL 8851 }; 8852 8853 #if __FreeBSD_version >= 400000 8854 COMPAT_PCI_DRIVER (sym, sym_pci_driver); 8855 #else 8856 DATA_SET (pcidevice_set, sym_pci_driver); 8857 #endif 8858 8859 #endif /* FreeBSD_Bus_Io_Abstraction */ 8860 8861 static struct sym_pci_chip sym_pci_dev_table[] = { 8862 {PCI_ID_SYM53C810, 0x0f, "810", 4, 8, 4, 64, 8863 FE_ERL} 8864 , 8865 #ifdef SYM_DEBUG_GENERIC_SUPPORT 8866 {PCI_ID_SYM53C810, 0xff, "810a", 4, 8, 4, 1, 8867 FE_BOF} 8868 , 8869 #else 8870 {PCI_ID_SYM53C810, 0xff, "810a", 4, 8, 4, 1, 8871 FE_CACHE_SET|FE_LDSTR|FE_PFEN|FE_BOF} 8872 , 8873 #endif 8874 {PCI_ID_SYM53C815, 0xff, "815", 4, 8, 4, 64, 8875 FE_BOF|FE_ERL} 8876 , 8877 {PCI_ID_SYM53C825, 0x0f, "825", 6, 8, 4, 64, 8878 FE_WIDE|FE_BOF|FE_ERL|FE_DIFF} 8879 , 8880 {PCI_ID_SYM53C825, 0xff, "825a", 6, 8, 4, 2, 8881 FE_WIDE|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM|FE_DIFF} 8882 , 8883 {PCI_ID_SYM53C860, 0xff, "860", 4, 8, 5, 1, 8884 FE_ULTRA|FE_CLK80|FE_CACHE_SET|FE_BOF|FE_LDSTR|FE_PFEN} 8885 , 8886 {PCI_ID_SYM53C875, 0x01, "875", 6, 16, 5, 2, 8887 FE_WIDE|FE_ULTRA|FE_CLK80|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN| 8888 FE_RAM|FE_DIFF} 8889 , 8890 {PCI_ID_SYM53C875, 0xff, "875", 6, 16, 5, 2, 8891 FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN| 8892 FE_RAM|FE_DIFF} 8893 , 8894 {PCI_ID_SYM53C875_2, 0xff, "875", 6, 16, 5, 2, 8895 FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN| 8896 FE_RAM|FE_DIFF} 8897 , 8898 {PCI_ID_SYM53C885, 0xff, "885", 6, 16, 5, 2, 8899 FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN| 8900 FE_RAM|FE_DIFF} 8901 , 8902 #ifdef SYM_DEBUG_GENERIC_SUPPORT 8903 {PCI_ID_SYM53C895, 0xff, "895", 6, 31, 7, 2, 8904 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS| 8905 FE_RAM|FE_LCKFRQ} 8906 , 8907 #else 8908 {PCI_ID_SYM53C895, 0xff, "895", 6, 31, 7, 2, 8909 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN| 8910 FE_RAM|FE_LCKFRQ} 8911 , 8912 #endif 8913 {PCI_ID_SYM53C896, 0xff, "896", 6, 31, 7, 4, 8914 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN| 8915 FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ} 8916 , 8917 {PCI_ID_SYM53C895A, 0xff, "895a", 6, 31, 7, 4, 8918 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN| 8919 FE_RAM|FE_RAM8K|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ} 8920 , 8921 {PCI_ID_LSI53C1010, 0x00, "1010-33", 6, 31, 7, 8, 8922 FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN| 8923 FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_CRC| 8924 FE_C10} 8925 , 8926 {PCI_ID_LSI53C1010, 0xff, "1010-33", 6, 31, 7, 8, 8927 FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN| 8928 FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_CRC| 8929 FE_C10|FE_U3EN} 8930 , 8931 {PCI_ID_LSI53C1010_2, 0xff, "1010-66", 6, 31, 7, 8, 8932 FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN| 8933 FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_66MHZ|FE_CRC| 8934 FE_C10|FE_U3EN} 8935 , 8936 {PCI_ID_LSI53C1510D, 0xff, "1510d", 6, 31, 7, 4, 8937 FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN| 8938 FE_RAM|FE_IO256|FE_LEDC} 8939 }; 8940 8941 #define sym_pci_num_devs \ 8942 (sizeof(sym_pci_dev_table) / sizeof(sym_pci_dev_table[0])) 8943 8944 /* 8945 * Look up the chip table. 8946 * 8947 * Return a pointer to the chip entry if found, 8948 * zero otherwise. 8949 */ 8950 static struct sym_pci_chip * 8951 #ifdef FreeBSD_Bus_Io_Abstraction 8952 sym_find_pci_chip(device_t dev) 8953 #else 8954 sym_find_pci_chip(pcici_t pci_tag) 8955 #endif 8956 { 8957 struct sym_pci_chip *chip; 8958 int i; 8959 u_short device_id; 8960 u_char revision; 8961 8962 #ifdef FreeBSD_Bus_Io_Abstraction 8963 if (pci_get_vendor(dev) != PCI_VENDOR_NCR) 8964 return 0; 8965 8966 device_id = pci_get_device(dev); 8967 revision = pci_get_revid(dev); 8968 #else 8969 if (pci_cfgread(pci_tag, PCIR_VENDOR, 2) != PCI_VENDOR_NCR) 8970 return 0; 8971 8972 device_id = pci_cfgread(pci_tag, PCIR_DEVICE, 2); 8973 revision = pci_cfgread(pci_tag, PCIR_REVID, 1); 8974 #endif 8975 8976 for (i = 0; i < sym_pci_num_devs; i++) { 8977 chip = &sym_pci_dev_table[i]; 8978 if (device_id != chip->device_id) 8979 continue; 8980 if (revision > chip->revision_id) 8981 continue; 8982 return chip; 8983 } 8984 8985 return 0; 8986 } 8987 8988 /* 8989 * Tell upper layer if the chip is supported. 8990 */ 8991 #ifdef FreeBSD_Bus_Io_Abstraction 8992 static int 8993 sym_pci_probe(device_t dev) 8994 { 8995 struct sym_pci_chip *chip; 8996 8997 chip = sym_find_pci_chip(dev); 8998 if (chip && sym_find_firmware(chip)) { 8999 device_set_desc(dev, chip->name); 9000 return (chip->lp_probe_bit & SYM_SETUP_LP_PROBE_MAP)? -2000 : 0; 9001 } 9002 return ENXIO; 9003 } 9004 #else /* Pre-FreeBSD_Bus_Io_Abstraction */ 9005 static const char * 9006 sym_pci_probe(pcici_t pci_tag, pcidi_t type) 9007 { 9008 struct sym_pci_chip *chip; 9009 9010 chip = sym_find_pci_chip(pci_tag); 9011 if (chip && sym_find_firmware(chip)) { 9012 #if NNCR > 0 9013 /* Only claim chips we are allowed to take precedence over the ncr */ 9014 if (!(chip->lp_probe_bit & SYM_SETUP_LP_PROBE_MAP)) 9015 #else 9016 if (1) 9017 #endif 9018 return chip->name; 9019 } 9020 return 0; 9021 } 9022 #endif 9023 9024 /* 9025 * Attach a sym53c8xx device. 9026 */ 9027 #ifdef FreeBSD_Bus_Io_Abstraction 9028 static int 9029 sym_pci_attach(device_t dev) 9030 #else 9031 static void 9032 sym_pci_attach(pcici_t pci_tag, int unit) 9033 { 9034 int err = sym_pci_attach2(pci_tag, unit); 9035 if (err) 9036 printf("sym: failed to attach unit %d - err=%d.\n", unit, err); 9037 } 9038 static int 9039 sym_pci_attach2(pcici_t pci_tag, int unit) 9040 #endif 9041 { 9042 struct sym_pci_chip *chip; 9043 u_short command; 9044 u_char cachelnsz; 9045 struct sym_hcb *np = 0; 9046 struct sym_nvram nvram; 9047 struct sym_fw *fw = 0; 9048 int i; 9049 #ifdef FreeBSD_Bus_Dma_Abstraction 9050 bus_dma_tag_t bus_dmat; 9051 9052 /* 9053 * I expected to be told about a parent 9054 * DMA tag, but didn't find any. 9055 */ 9056 bus_dmat = NULL; 9057 #endif 9058 9059 /* 9060 * Only probed devices should be attached. 9061 * We just enjoy being paranoid. :) 9062 */ 9063 #ifdef FreeBSD_Bus_Io_Abstraction 9064 chip = sym_find_pci_chip(dev); 9065 #else 9066 chip = sym_find_pci_chip(pci_tag); 9067 #endif 9068 if (chip == NULL || (fw = sym_find_firmware(chip)) == NULL) 9069 return (ENXIO); 9070 9071 /* 9072 * Allocate immediately the host control block, 9073 * since we are only expecting to succeed. :) 9074 * We keep track in the HCB of all the resources that 9075 * are to be released on error. 9076 */ 9077 #ifdef FreeBSD_Bus_Dma_Abstraction 9078 np = __sym_calloc_dma(bus_dmat, sizeof(*np), "HCB"); 9079 if (np) 9080 np->bus_dmat = bus_dmat; 9081 else 9082 goto attach_failed; 9083 #else 9084 np = sym_calloc_dma(sizeof(*np), "HCB"); 9085 if (!np) 9086 goto attach_failed; 9087 #endif 9088 9089 /* 9090 * Copy some useful infos to the HCB. 9091 */ 9092 np->hcb_ba = vtobus(np); 9093 np->verbose = bootverbose; 9094 #ifdef FreeBSD_Bus_Io_Abstraction 9095 np->device = dev; 9096 np->unit = device_get_unit(dev); 9097 np->device_id = pci_get_device(dev); 9098 np->revision_id = pci_get_revid(dev); 9099 #else 9100 np->pci_tag = pci_tag; 9101 np->unit = unit; 9102 np->device_id = pci_cfgread(pci_tag, PCIR_DEVICE, 2); 9103 np->revision_id = pci_cfgread(pci_tag, PCIR_REVID, 1); 9104 #endif 9105 np->features = chip->features; 9106 np->clock_divn = chip->nr_divisor; 9107 np->maxoffs = chip->offset_max; 9108 np->maxburst = chip->burst_max; 9109 np->scripta_sz = fw->a_size; 9110 np->scriptb_sz = fw->b_size; 9111 np->fw_setup = fw->setup; 9112 np->fw_patch = fw->patch; 9113 np->fw_name = fw->name; 9114 9115 /* 9116 * Edit its name. 9117 */ 9118 snprintf(np->inst_name, sizeof(np->inst_name), "sym%d", np->unit); 9119 9120 /* 9121 * Initialyze the CCB free and busy queues. 9122 */ 9123 sym_que_init(&np->free_ccbq); 9124 sym_que_init(&np->busy_ccbq); 9125 sym_que_init(&np->comp_ccbq); 9126 sym_que_init(&np->cam_ccbq); 9127 9128 /* 9129 * Allocate a tag for the DMA of user data. 9130 */ 9131 #ifdef FreeBSD_Bus_Dma_Abstraction 9132 if (bus_dma_tag_create(np->bus_dmat, 1, (1<<24), 9133 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 9134 NULL, NULL, 9135 BUS_SPACE_MAXSIZE, SYM_CONF_MAX_SG, 9136 (1<<24), 0, busdma_lock_mutex, &Giant, 9137 &np->data_dmat)) { 9138 device_printf(dev, "failed to create DMA tag.\n"); 9139 goto attach_failed; 9140 } 9141 #endif 9142 /* 9143 * Read and apply some fix-ups to the PCI COMMAND 9144 * register. We want the chip to be enabled for: 9145 * - BUS mastering 9146 * - PCI parity checking (reporting would also be fine) 9147 * - Write And Invalidate. 9148 */ 9149 #ifdef FreeBSD_Bus_Io_Abstraction 9150 command = pci_read_config(dev, PCIR_COMMAND, 2); 9151 #else 9152 command = pci_cfgread(pci_tag, PCIR_COMMAND, 2); 9153 #endif 9154 command |= PCIM_CMD_BUSMASTEREN; 9155 command |= PCIM_CMD_PERRESPEN; 9156 command |= /* PCIM_CMD_MWIEN */ 0x0010; 9157 #ifdef FreeBSD_Bus_Io_Abstraction 9158 pci_write_config(dev, PCIR_COMMAND, command, 2); 9159 #else 9160 pci_cfgwrite(pci_tag, PCIR_COMMAND, command, 2); 9161 #endif 9162 9163 /* 9164 * Let the device know about the cache line size, 9165 * if it doesn't yet. 9166 */ 9167 #ifdef FreeBSD_Bus_Io_Abstraction 9168 cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1); 9169 #else 9170 cachelnsz = pci_cfgread(pci_tag, PCIR_CACHELNSZ, 1); 9171 #endif 9172 if (!cachelnsz) { 9173 cachelnsz = 8; 9174 #ifdef FreeBSD_Bus_Io_Abstraction 9175 pci_write_config(dev, PCIR_CACHELNSZ, cachelnsz, 1); 9176 #else 9177 pci_cfgwrite(pci_tag, PCIR_CACHELNSZ, cachelnsz, 1); 9178 #endif 9179 } 9180 9181 /* 9182 * Alloc/get/map/retrieve everything that deals with MMIO. 9183 */ 9184 #ifdef FreeBSD_Bus_Io_Abstraction 9185 if ((command & PCIM_CMD_MEMEN) != 0) { 9186 int regs_id = SYM_PCI_MMIO; 9187 np->mmio_res = bus_alloc_resource(dev, SYS_RES_MEMORY, ®s_id, 9188 0, ~0, 1, RF_ACTIVE); 9189 } 9190 if (!np->mmio_res) { 9191 device_printf(dev, "failed to allocate MMIO resources\n"); 9192 goto attach_failed; 9193 } 9194 np->mmio_bsh = rman_get_bushandle(np->mmio_res); 9195 np->mmio_tag = rman_get_bustag(np->mmio_res); 9196 np->mmio_pa = rman_get_start(np->mmio_res); 9197 np->mmio_va = (vm_offset_t) rman_get_virtual(np->mmio_res); 9198 np->mmio_ba = np->mmio_pa; 9199 #else 9200 if ((command & PCIM_CMD_MEMEN) != 0) { 9201 vm_offset_t vaddr, paddr; 9202 if (!pci_map_mem(pci_tag, SYM_PCI_MMIO, &vaddr, &paddr)) { 9203 printf("%s: failed to map MMIO window\n", sym_name(np)); 9204 goto attach_failed; 9205 } 9206 np->mmio_va = vaddr; 9207 np->mmio_pa = paddr; 9208 np->mmio_ba = paddr; 9209 } 9210 #endif 9211 9212 /* 9213 * Allocate the IRQ. 9214 */ 9215 #ifdef FreeBSD_Bus_Io_Abstraction 9216 i = 0; 9217 np->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &i, 9218 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE); 9219 if (!np->irq_res) { 9220 device_printf(dev, "failed to allocate IRQ resource\n"); 9221 goto attach_failed; 9222 } 9223 #endif 9224 9225 #ifdef SYM_CONF_IOMAPPED 9226 /* 9227 * User want us to use normal IO with PCI. 9228 * Alloc/get/map/retrieve everything that deals with IO. 9229 */ 9230 #ifdef FreeBSD_Bus_Io_Abstraction 9231 if ((command & PCI_COMMAND_IO_ENABLE) != 0) { 9232 int regs_id = SYM_PCI_IO; 9233 np->io_res = bus_alloc_resource(dev, SYS_RES_IOPORT, ®s_id, 9234 0, ~0, 1, RF_ACTIVE); 9235 } 9236 if (!np->io_res) { 9237 device_printf(dev, "failed to allocate IO resources\n"); 9238 goto attach_failed; 9239 } 9240 np->io_bsh = rman_get_bushandle(np->io_res); 9241 np->io_tag = rman_get_bustag(np->io_res); 9242 np->io_port = rman_get_start(np->io_res); 9243 #else 9244 if ((command & PCI_COMMAND_IO_ENABLE) != 0) { 9245 pci_port_t io_port; 9246 if (!pci_map_port (pci_tag, SYM_PCI_IO, &io_port)) { 9247 printf("%s: failed to map IO window\n", sym_name(np)); 9248 goto attach_failed; 9249 } 9250 np->io_port = io_port; 9251 } 9252 #endif 9253 9254 #endif /* SYM_CONF_IOMAPPED */ 9255 9256 /* 9257 * If the chip has RAM. 9258 * Alloc/get/map/retrieve the corresponding resources. 9259 */ 9260 if ((np->features & (FE_RAM|FE_RAM8K)) && 9261 (command & PCIM_CMD_MEMEN) != 0) { 9262 #ifdef FreeBSD_Bus_Io_Abstraction 9263 int regs_id = SYM_PCI_RAM; 9264 if (np->features & FE_64BIT) 9265 regs_id = SYM_PCI_RAM64; 9266 np->ram_res = bus_alloc_resource(dev, SYS_RES_MEMORY, ®s_id, 9267 0, ~0, 1, RF_ACTIVE); 9268 if (!np->ram_res) { 9269 device_printf(dev,"failed to allocate RAM resources\n"); 9270 goto attach_failed; 9271 } 9272 np->ram_id = regs_id; 9273 np->ram_bsh = rman_get_bushandle(np->ram_res); 9274 np->ram_tag = rman_get_bustag(np->ram_res); 9275 np->ram_pa = rman_get_start(np->ram_res); 9276 np->ram_va = (vm_offset_t) rman_get_virtual(np->ram_res); 9277 np->ram_ba = np->ram_pa; 9278 #else 9279 vm_offset_t vaddr, paddr; 9280 int regs_id = SYM_PCI_RAM; 9281 if (np->features & FE_64BIT) 9282 regs_id = SYM_PCI_RAM64; 9283 if (!pci_map_mem(pci_tag, regs_id, &vaddr, &paddr)) { 9284 printf("%s: failed to map RAM window\n", sym_name(np)); 9285 goto attach_failed; 9286 } 9287 np->ram_va = vaddr; 9288 np->ram_pa = paddr; 9289 np->ram_ba = paddr; 9290 #endif 9291 } 9292 9293 /* 9294 * Save setting of some IO registers, so we will 9295 * be able to probe specific implementations. 9296 */ 9297 sym_save_initial_setting (np); 9298 9299 /* 9300 * Reset the chip now, since it has been reported 9301 * that SCSI clock calibration may not work properly 9302 * if the chip is currently active. 9303 */ 9304 sym_chip_reset (np); 9305 9306 /* 9307 * Try to read the user set-up. 9308 */ 9309 (void) sym_read_nvram(np, &nvram); 9310 9311 /* 9312 * Prepare controller and devices settings, according 9313 * to chip features, user set-up and driver set-up. 9314 */ 9315 (void) sym_prepare_setting(np, &nvram); 9316 9317 /* 9318 * Check the PCI clock frequency. 9319 * Must be performed after prepare_setting since it destroys 9320 * STEST1 that is used to probe for the clock doubler. 9321 */ 9322 i = sym_getpciclock(np); 9323 if (i > 37000) 9324 #ifdef FreeBSD_Bus_Io_Abstraction 9325 device_printf(dev, "PCI BUS clock seems too high: %u KHz.\n",i); 9326 #else 9327 printf("%s: PCI BUS clock seems too high: %u KHz.\n", 9328 sym_name(np), i); 9329 #endif 9330 9331 /* 9332 * Allocate the start queue. 9333 */ 9334 np->squeue = (u32 *) sym_calloc_dma(sizeof(u32)*(MAX_QUEUE*2),"SQUEUE"); 9335 if (!np->squeue) 9336 goto attach_failed; 9337 np->squeue_ba = vtobus(np->squeue); 9338 9339 /* 9340 * Allocate the done queue. 9341 */ 9342 np->dqueue = (u32 *) sym_calloc_dma(sizeof(u32)*(MAX_QUEUE*2),"DQUEUE"); 9343 if (!np->dqueue) 9344 goto attach_failed; 9345 np->dqueue_ba = vtobus(np->dqueue); 9346 9347 /* 9348 * Allocate the target bus address array. 9349 */ 9350 np->targtbl = (u32 *) sym_calloc_dma(256, "TARGTBL"); 9351 if (!np->targtbl) 9352 goto attach_failed; 9353 np->targtbl_ba = vtobus(np->targtbl); 9354 9355 /* 9356 * Allocate SCRIPTS areas. 9357 */ 9358 np->scripta0 = sym_calloc_dma(np->scripta_sz, "SCRIPTA0"); 9359 np->scriptb0 = sym_calloc_dma(np->scriptb_sz, "SCRIPTB0"); 9360 if (!np->scripta0 || !np->scriptb0) 9361 goto attach_failed; 9362 9363 /* 9364 * Allocate some CCB. We need at least ONE. 9365 */ 9366 if (!sym_alloc_ccb(np)) 9367 goto attach_failed; 9368 9369 /* 9370 * Calculate BUS addresses where we are going 9371 * to load the SCRIPTS. 9372 */ 9373 np->scripta_ba = vtobus(np->scripta0); 9374 np->scriptb_ba = vtobus(np->scriptb0); 9375 np->scriptb0_ba = np->scriptb_ba; 9376 9377 if (np->ram_ba) { 9378 np->scripta_ba = np->ram_ba; 9379 if (np->features & FE_RAM8K) { 9380 np->ram_ws = 8192; 9381 np->scriptb_ba = np->scripta_ba + 4096; 9382 #if BITS_PER_LONG > 32 9383 np->scr_ram_seg = cpu_to_scr(np->scripta_ba >> 32); 9384 #endif 9385 } 9386 else 9387 np->ram_ws = 4096; 9388 } 9389 9390 /* 9391 * Copy scripts to controller instance. 9392 */ 9393 bcopy(fw->a_base, np->scripta0, np->scripta_sz); 9394 bcopy(fw->b_base, np->scriptb0, np->scriptb_sz); 9395 9396 /* 9397 * Setup variable parts in scripts and compute 9398 * scripts bus addresses used from the C code. 9399 */ 9400 np->fw_setup(np, fw); 9401 9402 /* 9403 * Bind SCRIPTS with physical addresses usable by the 9404 * SCRIPTS processor (as seen from the BUS = BUS addresses). 9405 */ 9406 sym_fw_bind_script(np, (u32 *) np->scripta0, np->scripta_sz); 9407 sym_fw_bind_script(np, (u32 *) np->scriptb0, np->scriptb_sz); 9408 9409 #ifdef SYM_CONF_IARB_SUPPORT 9410 /* 9411 * If user wants IARB to be set when we win arbitration 9412 * and have other jobs, compute the max number of consecutive 9413 * settings of IARB hints before we leave devices a chance to 9414 * arbitrate for reselection. 9415 */ 9416 #ifdef SYM_SETUP_IARB_MAX 9417 np->iarb_max = SYM_SETUP_IARB_MAX; 9418 #else 9419 np->iarb_max = 4; 9420 #endif 9421 #endif 9422 9423 /* 9424 * Prepare the idle and invalid task actions. 9425 */ 9426 np->idletask.start = cpu_to_scr(SCRIPTA_BA (np, idle)); 9427 np->idletask.restart = cpu_to_scr(SCRIPTB_BA (np, bad_i_t_l)); 9428 np->idletask_ba = vtobus(&np->idletask); 9429 9430 np->notask.start = cpu_to_scr(SCRIPTA_BA (np, idle)); 9431 np->notask.restart = cpu_to_scr(SCRIPTB_BA (np, bad_i_t_l)); 9432 np->notask_ba = vtobus(&np->notask); 9433 9434 np->bad_itl.start = cpu_to_scr(SCRIPTA_BA (np, idle)); 9435 np->bad_itl.restart = cpu_to_scr(SCRIPTB_BA (np, bad_i_t_l)); 9436 np->bad_itl_ba = vtobus(&np->bad_itl); 9437 9438 np->bad_itlq.start = cpu_to_scr(SCRIPTA_BA (np, idle)); 9439 np->bad_itlq.restart = cpu_to_scr(SCRIPTB_BA (np,bad_i_t_l_q)); 9440 np->bad_itlq_ba = vtobus(&np->bad_itlq); 9441 9442 /* 9443 * Allocate and prepare the lun JUMP table that is used 9444 * for a target prior the probing of devices (bad lun table). 9445 * A private table will be allocated for the target on the 9446 * first INQUIRY response received. 9447 */ 9448 np->badluntbl = sym_calloc_dma(256, "BADLUNTBL"); 9449 if (!np->badluntbl) 9450 goto attach_failed; 9451 9452 np->badlun_sa = cpu_to_scr(SCRIPTB_BA (np, resel_bad_lun)); 9453 for (i = 0 ; i < 64 ; i++) /* 64 luns/target, no less */ 9454 np->badluntbl[i] = cpu_to_scr(vtobus(&np->badlun_sa)); 9455 9456 /* 9457 * Prepare the bus address array that contains the bus 9458 * address of each target control block. 9459 * For now, assume all logical units are wrong. :) 9460 */ 9461 for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) { 9462 np->targtbl[i] = cpu_to_scr(vtobus(&np->target[i])); 9463 np->target[i].head.luntbl_sa = 9464 cpu_to_scr(vtobus(np->badluntbl)); 9465 np->target[i].head.lun0_sa = 9466 cpu_to_scr(vtobus(&np->badlun_sa)); 9467 } 9468 9469 /* 9470 * Now check the cache handling of the pci chipset. 9471 */ 9472 if (sym_snooptest (np)) { 9473 #ifdef FreeBSD_Bus_Io_Abstraction 9474 device_printf(dev, "CACHE INCORRECTLY CONFIGURED.\n"); 9475 #else 9476 printf("%s: CACHE INCORRECTLY CONFIGURED.\n", sym_name(np)); 9477 #endif 9478 goto attach_failed; 9479 }; 9480 9481 /* 9482 * Now deal with CAM. 9483 * Hopefully, we will succeed with that one.:) 9484 */ 9485 if (!sym_cam_attach(np)) 9486 goto attach_failed; 9487 9488 /* 9489 * Sigh! we are done. 9490 */ 9491 return 0; 9492 9493 /* 9494 * We have failed. 9495 * We will try to free all the resources we have 9496 * allocated, but if we are a boot device, this 9497 * will not help that much.;) 9498 */ 9499 attach_failed: 9500 if (np) 9501 sym_pci_free(np); 9502 return ENXIO; 9503 } 9504 9505 /* 9506 * Free everything that have been allocated for this device. 9507 */ 9508 static void sym_pci_free(hcb_p np) 9509 { 9510 SYM_QUEHEAD *qp; 9511 ccb_p cp; 9512 tcb_p tp; 9513 lcb_p lp; 9514 int target, lun; 9515 int s; 9516 9517 /* 9518 * First free CAM resources. 9519 */ 9520 s = splcam(); 9521 sym_cam_free(np); 9522 splx(s); 9523 9524 /* 9525 * Now every should be quiet for us to 9526 * free other resources. 9527 */ 9528 #ifdef FreeBSD_Bus_Io_Abstraction 9529 if (np->ram_res) 9530 bus_release_resource(np->device, SYS_RES_MEMORY, 9531 np->ram_id, np->ram_res); 9532 if (np->mmio_res) 9533 bus_release_resource(np->device, SYS_RES_MEMORY, 9534 SYM_PCI_MMIO, np->mmio_res); 9535 if (np->io_res) 9536 bus_release_resource(np->device, SYS_RES_IOPORT, 9537 SYM_PCI_IO, np->io_res); 9538 if (np->irq_res) 9539 bus_release_resource(np->device, SYS_RES_IRQ, 9540 0, np->irq_res); 9541 #else 9542 /* 9543 * YEAH!!! 9544 * It seems there is no means to free MMIO resources. 9545 */ 9546 #endif 9547 9548 if (np->scriptb0) 9549 sym_mfree_dma(np->scriptb0, np->scriptb_sz, "SCRIPTB0"); 9550 if (np->scripta0) 9551 sym_mfree_dma(np->scripta0, np->scripta_sz, "SCRIPTA0"); 9552 if (np->squeue) 9553 sym_mfree_dma(np->squeue, sizeof(u32)*(MAX_QUEUE*2), "SQUEUE"); 9554 if (np->dqueue) 9555 sym_mfree_dma(np->dqueue, sizeof(u32)*(MAX_QUEUE*2), "DQUEUE"); 9556 9557 while ((qp = sym_remque_head(&np->free_ccbq)) != 0) { 9558 cp = sym_que_entry(qp, struct sym_ccb, link_ccbq); 9559 #ifdef FreeBSD_Bus_Dma_Abstraction 9560 bus_dmamap_destroy(np->data_dmat, cp->dmamap); 9561 #endif 9562 sym_mfree_dma(cp->sns_bbuf, SYM_SNS_BBUF_LEN, "SNS_BBUF"); 9563 sym_mfree_dma(cp, sizeof(*cp), "CCB"); 9564 } 9565 9566 if (np->badluntbl) 9567 sym_mfree_dma(np->badluntbl, 256,"BADLUNTBL"); 9568 9569 for (target = 0; target < SYM_CONF_MAX_TARGET ; target++) { 9570 tp = &np->target[target]; 9571 for (lun = 0 ; lun < SYM_CONF_MAX_LUN ; lun++) { 9572 lp = sym_lp(np, tp, lun); 9573 if (!lp) 9574 continue; 9575 if (lp->itlq_tbl) 9576 sym_mfree_dma(lp->itlq_tbl, SYM_CONF_MAX_TASK*4, 9577 "ITLQ_TBL"); 9578 if (lp->cb_tags) 9579 sym_mfree(lp->cb_tags, SYM_CONF_MAX_TASK, 9580 "CB_TAGS"); 9581 sym_mfree_dma(lp, sizeof(*lp), "LCB"); 9582 } 9583 #if SYM_CONF_MAX_LUN > 1 9584 if (tp->lunmp) 9585 sym_mfree(tp->lunmp, SYM_CONF_MAX_LUN*sizeof(lcb_p), 9586 "LUNMP"); 9587 #endif 9588 } 9589 if (np->targtbl) 9590 sym_mfree_dma(np->targtbl, 256, "TARGTBL"); 9591 #ifdef FreeBSD_Bus_Dma_Abstraction 9592 if (np->data_dmat) 9593 bus_dma_tag_destroy(np->data_dmat); 9594 #endif 9595 sym_mfree_dma(np, sizeof(*np), "HCB"); 9596 } 9597 9598 /* 9599 * Allocate CAM resources and register a bus to CAM. 9600 */ 9601 static int sym_cam_attach(hcb_p np) 9602 { 9603 struct cam_devq *devq = 0; 9604 struct cam_sim *sim = 0; 9605 struct cam_path *path = 0; 9606 struct ccb_setasync csa; 9607 int err, s; 9608 9609 s = splcam(); 9610 9611 /* 9612 * Establish our interrupt handler. 9613 */ 9614 #ifdef FreeBSD_Bus_Io_Abstraction 9615 err = bus_setup_intr(np->device, np->irq_res, 9616 INTR_TYPE_CAM | INTR_ENTROPY, sym_intr, np, 9617 &np->intr); 9618 if (err) { 9619 device_printf(np->device, "bus_setup_intr() failed: %d\n", 9620 err); 9621 goto fail; 9622 } 9623 #else 9624 err = 0; 9625 if (!pci_map_int (np->pci_tag, sym_intr, np, &cam_imask)) { 9626 printf("%s: failed to map interrupt\n", sym_name(np)); 9627 goto fail; 9628 } 9629 #endif 9630 9631 /* 9632 * Create the device queue for our sym SIM. 9633 */ 9634 devq = cam_simq_alloc(SYM_CONF_MAX_START); 9635 if (!devq) 9636 goto fail; 9637 9638 /* 9639 * Construct our SIM entry. 9640 */ 9641 sim = cam_sim_alloc(sym_action, sym_poll, "sym", np, np->unit, 9642 1, SYM_SETUP_MAX_TAG, devq); 9643 if (!sim) 9644 goto fail; 9645 devq = 0; 9646 9647 if (xpt_bus_register(sim, 0) != CAM_SUCCESS) 9648 goto fail; 9649 np->sim = sim; 9650 sim = 0; 9651 9652 if (xpt_create_path(&path, 0, 9653 cam_sim_path(np->sim), CAM_TARGET_WILDCARD, 9654 CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 9655 goto fail; 9656 } 9657 np->path = path; 9658 9659 /* 9660 * Hmmm... This should be useful, but I donnot want to 9661 * know about. 9662 */ 9663 #if __FreeBSD_version < 400000 9664 #ifdef __alpha__ 9665 #ifdef FreeBSD_Bus_Io_Abstraction 9666 alpha_register_pci_scsi(pci_get_bus(np->device), 9667 pci_get_slot(np->device), np->sim); 9668 #else 9669 alpha_register_pci_scsi(pci_tag->bus, pci_tag->slot, np->sim); 9670 #endif 9671 #endif 9672 #endif 9673 9674 /* 9675 * Establish our async notification handler. 9676 */ 9677 xpt_setup_ccb(&csa.ccb_h, np->path, 5); 9678 csa.ccb_h.func_code = XPT_SASYNC_CB; 9679 csa.event_enable = AC_LOST_DEVICE; 9680 csa.callback = sym_async; 9681 csa.callback_arg = np->sim; 9682 xpt_action((union ccb *)&csa); 9683 9684 /* 9685 * Start the chip now, without resetting the BUS, since 9686 * it seems that this must stay under control of CAM. 9687 * With LVD/SE capable chips and BUS in SE mode, we may 9688 * get a spurious SMBC interrupt. 9689 */ 9690 sym_init (np, 0); 9691 9692 splx(s); 9693 return 1; 9694 fail: 9695 if (sim) 9696 cam_sim_free(sim, FALSE); 9697 if (devq) 9698 cam_simq_free(devq); 9699 9700 sym_cam_free(np); 9701 9702 splx(s); 9703 return 0; 9704 } 9705 9706 /* 9707 * Free everything that deals with CAM. 9708 */ 9709 static void sym_cam_free(hcb_p np) 9710 { 9711 #ifdef FreeBSD_Bus_Io_Abstraction 9712 if (np->intr) 9713 bus_teardown_intr(np->device, np->irq_res, np->intr); 9714 #else 9715 /* pci_unmap_int(np->pci_tag); */ /* Does nothing */ 9716 #endif 9717 9718 if (np->sim) { 9719 xpt_bus_deregister(cam_sim_path(np->sim)); 9720 cam_sim_free(np->sim, /*free_devq*/ TRUE); 9721 } 9722 if (np->path) 9723 xpt_free_path(np->path); 9724 } 9725 9726 /*============ OPTIONNAL NVRAM SUPPORT =================*/ 9727 9728 /* 9729 * Get host setup from NVRAM. 9730 */ 9731 static void sym_nvram_setup_host (hcb_p np, struct sym_nvram *nvram) 9732 { 9733 #ifdef SYM_CONF_NVRAM_SUPPORT 9734 /* 9735 * Get parity checking, host ID, verbose mode 9736 * and miscellaneous host flags from NVRAM. 9737 */ 9738 switch(nvram->type) { 9739 case SYM_SYMBIOS_NVRAM: 9740 if (!(nvram->data.Symbios.flags & SYMBIOS_PARITY_ENABLE)) 9741 np->rv_scntl0 &= ~0x0a; 9742 np->myaddr = nvram->data.Symbios.host_id & 0x0f; 9743 if (nvram->data.Symbios.flags & SYMBIOS_VERBOSE_MSGS) 9744 np->verbose += 1; 9745 if (nvram->data.Symbios.flags1 & SYMBIOS_SCAN_HI_LO) 9746 np->usrflags |= SYM_SCAN_TARGETS_HILO; 9747 if (nvram->data.Symbios.flags2 & SYMBIOS_AVOID_BUS_RESET) 9748 np->usrflags |= SYM_AVOID_BUS_RESET; 9749 break; 9750 case SYM_TEKRAM_NVRAM: 9751 np->myaddr = nvram->data.Tekram.host_id & 0x0f; 9752 break; 9753 default: 9754 break; 9755 } 9756 #endif 9757 } 9758 9759 /* 9760 * Get target setup from NVRAM. 9761 */ 9762 #ifdef SYM_CONF_NVRAM_SUPPORT 9763 static void sym_Symbios_setup_target(hcb_p np,int target, Symbios_nvram *nvram); 9764 static void sym_Tekram_setup_target(hcb_p np,int target, Tekram_nvram *nvram); 9765 #endif 9766 9767 static void 9768 sym_nvram_setup_target (hcb_p np, int target, struct sym_nvram *nvp) 9769 { 9770 #ifdef SYM_CONF_NVRAM_SUPPORT 9771 switch(nvp->type) { 9772 case SYM_SYMBIOS_NVRAM: 9773 sym_Symbios_setup_target (np, target, &nvp->data.Symbios); 9774 break; 9775 case SYM_TEKRAM_NVRAM: 9776 sym_Tekram_setup_target (np, target, &nvp->data.Tekram); 9777 break; 9778 default: 9779 break; 9780 } 9781 #endif 9782 } 9783 9784 #ifdef SYM_CONF_NVRAM_SUPPORT 9785 /* 9786 * Get target set-up from Symbios format NVRAM. 9787 */ 9788 static void 9789 sym_Symbios_setup_target(hcb_p np, int target, Symbios_nvram *nvram) 9790 { 9791 tcb_p tp = &np->target[target]; 9792 Symbios_target *tn = &nvram->target[target]; 9793 9794 tp->tinfo.user.period = tn->sync_period ? (tn->sync_period + 3) / 4 : 0; 9795 tp->tinfo.user.width = tn->bus_width == 0x10 ? BUS_16_BIT : BUS_8_BIT; 9796 tp->usrtags = 9797 (tn->flags & SYMBIOS_QUEUE_TAGS_ENABLED)? SYM_SETUP_MAX_TAG : 0; 9798 9799 if (!(tn->flags & SYMBIOS_DISCONNECT_ENABLE)) 9800 tp->usrflags &= ~SYM_DISC_ENABLED; 9801 if (!(tn->flags & SYMBIOS_SCAN_AT_BOOT_TIME)) 9802 tp->usrflags |= SYM_SCAN_BOOT_DISABLED; 9803 if (!(tn->flags & SYMBIOS_SCAN_LUNS)) 9804 tp->usrflags |= SYM_SCAN_LUNS_DISABLED; 9805 } 9806 9807 /* 9808 * Get target set-up from Tekram format NVRAM. 9809 */ 9810 static void 9811 sym_Tekram_setup_target(hcb_p np, int target, Tekram_nvram *nvram) 9812 { 9813 tcb_p tp = &np->target[target]; 9814 struct Tekram_target *tn = &nvram->target[target]; 9815 int i; 9816 9817 if (tn->flags & TEKRAM_SYNC_NEGO) { 9818 i = tn->sync_index & 0xf; 9819 tp->tinfo.user.period = Tekram_sync[i]; 9820 } 9821 9822 tp->tinfo.user.width = 9823 (tn->flags & TEKRAM_WIDE_NEGO) ? BUS_16_BIT : BUS_8_BIT; 9824 9825 if (tn->flags & TEKRAM_TAGGED_COMMANDS) { 9826 tp->usrtags = 2 << nvram->max_tags_index; 9827 } 9828 9829 if (tn->flags & TEKRAM_DISCONNECT_ENABLE) 9830 tp->usrflags |= SYM_DISC_ENABLED; 9831 9832 /* If any device does not support parity, we will not use this option */ 9833 if (!(tn->flags & TEKRAM_PARITY_CHECK)) 9834 np->rv_scntl0 &= ~0x0a; /* SCSI parity checking disabled */ 9835 } 9836 9837 #ifdef SYM_CONF_DEBUG_NVRAM 9838 /* 9839 * Dump Symbios format NVRAM for debugging purpose. 9840 */ 9841 static void sym_display_Symbios_nvram(hcb_p np, Symbios_nvram *nvram) 9842 { 9843 int i; 9844 9845 /* display Symbios nvram host data */ 9846 printf("%s: HOST ID=%d%s%s%s%s%s%s\n", 9847 sym_name(np), nvram->host_id & 0x0f, 9848 (nvram->flags & SYMBIOS_SCAM_ENABLE) ? " SCAM" :"", 9849 (nvram->flags & SYMBIOS_PARITY_ENABLE) ? " PARITY" :"", 9850 (nvram->flags & SYMBIOS_VERBOSE_MSGS) ? " VERBOSE" :"", 9851 (nvram->flags & SYMBIOS_CHS_MAPPING) ? " CHS_ALT" :"", 9852 (nvram->flags2 & SYMBIOS_AVOID_BUS_RESET)?" NO_RESET" :"", 9853 (nvram->flags1 & SYMBIOS_SCAN_HI_LO) ? " HI_LO" :""); 9854 9855 /* display Symbios nvram drive data */ 9856 for (i = 0 ; i < 15 ; i++) { 9857 struct Symbios_target *tn = &nvram->target[i]; 9858 printf("%s-%d:%s%s%s%s WIDTH=%d SYNC=%d TMO=%d\n", 9859 sym_name(np), i, 9860 (tn->flags & SYMBIOS_DISCONNECT_ENABLE) ? " DISC" : "", 9861 (tn->flags & SYMBIOS_SCAN_AT_BOOT_TIME) ? " SCAN_BOOT" : "", 9862 (tn->flags & SYMBIOS_SCAN_LUNS) ? " SCAN_LUNS" : "", 9863 (tn->flags & SYMBIOS_QUEUE_TAGS_ENABLED)? " TCQ" : "", 9864 tn->bus_width, 9865 tn->sync_period / 4, 9866 tn->timeout); 9867 } 9868 } 9869 9870 /* 9871 * Dump TEKRAM format NVRAM for debugging purpose. 9872 */ 9873 static u_char Tekram_boot_delay[7] = {3, 5, 10, 20, 30, 60, 120}; 9874 static void sym_display_Tekram_nvram(hcb_p np, Tekram_nvram *nvram) 9875 { 9876 int i, tags, boot_delay; 9877 char *rem; 9878 9879 /* display Tekram nvram host data */ 9880 tags = 2 << nvram->max_tags_index; 9881 boot_delay = 0; 9882 if (nvram->boot_delay_index < 6) 9883 boot_delay = Tekram_boot_delay[nvram->boot_delay_index]; 9884 switch((nvram->flags & TEKRAM_REMOVABLE_FLAGS) >> 6) { 9885 default: 9886 case 0: rem = ""; break; 9887 case 1: rem = " REMOVABLE=boot device"; break; 9888 case 2: rem = " REMOVABLE=all"; break; 9889 } 9890 9891 printf("%s: HOST ID=%d%s%s%s%s%s%s%s%s%s BOOT DELAY=%d tags=%d\n", 9892 sym_name(np), nvram->host_id & 0x0f, 9893 (nvram->flags1 & SYMBIOS_SCAM_ENABLE) ? " SCAM" :"", 9894 (nvram->flags & TEKRAM_MORE_THAN_2_DRIVES) ? " >2DRIVES" :"", 9895 (nvram->flags & TEKRAM_DRIVES_SUP_1GB) ? " >1GB" :"", 9896 (nvram->flags & TEKRAM_RESET_ON_POWER_ON) ? " RESET" :"", 9897 (nvram->flags & TEKRAM_ACTIVE_NEGATION) ? " ACT_NEG" :"", 9898 (nvram->flags & TEKRAM_IMMEDIATE_SEEK) ? " IMM_SEEK" :"", 9899 (nvram->flags & TEKRAM_SCAN_LUNS) ? " SCAN_LUNS" :"", 9900 (nvram->flags1 & TEKRAM_F2_F6_ENABLED) ? " F2_F6" :"", 9901 rem, boot_delay, tags); 9902 9903 /* display Tekram nvram drive data */ 9904 for (i = 0; i <= 15; i++) { 9905 int sync, j; 9906 struct Tekram_target *tn = &nvram->target[i]; 9907 j = tn->sync_index & 0xf; 9908 sync = Tekram_sync[j]; 9909 printf("%s-%d:%s%s%s%s%s%s PERIOD=%d\n", 9910 sym_name(np), i, 9911 (tn->flags & TEKRAM_PARITY_CHECK) ? " PARITY" : "", 9912 (tn->flags & TEKRAM_SYNC_NEGO) ? " SYNC" : "", 9913 (tn->flags & TEKRAM_DISCONNECT_ENABLE) ? " DISC" : "", 9914 (tn->flags & TEKRAM_START_CMD) ? " START" : "", 9915 (tn->flags & TEKRAM_TAGGED_COMMANDS) ? " TCQ" : "", 9916 (tn->flags & TEKRAM_WIDE_NEGO) ? " WIDE" : "", 9917 sync); 9918 } 9919 } 9920 #endif /* SYM_CONF_DEBUG_NVRAM */ 9921 #endif /* SYM_CONF_NVRAM_SUPPORT */ 9922 9923 9924 /* 9925 * Try reading Symbios or Tekram NVRAM 9926 */ 9927 #ifdef SYM_CONF_NVRAM_SUPPORT 9928 static int sym_read_Symbios_nvram (hcb_p np, Symbios_nvram *nvram); 9929 static int sym_read_Tekram_nvram (hcb_p np, Tekram_nvram *nvram); 9930 #endif 9931 9932 static int sym_read_nvram(hcb_p np, struct sym_nvram *nvp) 9933 { 9934 #ifdef SYM_CONF_NVRAM_SUPPORT 9935 /* 9936 * Try to read SYMBIOS nvram. 9937 * Try to read TEKRAM nvram if Symbios nvram not found. 9938 */ 9939 if (SYM_SETUP_SYMBIOS_NVRAM && 9940 !sym_read_Symbios_nvram (np, &nvp->data.Symbios)) { 9941 nvp->type = SYM_SYMBIOS_NVRAM; 9942 #ifdef SYM_CONF_DEBUG_NVRAM 9943 sym_display_Symbios_nvram(np, &nvp->data.Symbios); 9944 #endif 9945 } 9946 else if (SYM_SETUP_TEKRAM_NVRAM && 9947 !sym_read_Tekram_nvram (np, &nvp->data.Tekram)) { 9948 nvp->type = SYM_TEKRAM_NVRAM; 9949 #ifdef SYM_CONF_DEBUG_NVRAM 9950 sym_display_Tekram_nvram(np, &nvp->data.Tekram); 9951 #endif 9952 } 9953 else 9954 nvp->type = 0; 9955 #else 9956 nvp->type = 0; 9957 #endif 9958 return nvp->type; 9959 } 9960 9961 9962 #ifdef SYM_CONF_NVRAM_SUPPORT 9963 /* 9964 * 24C16 EEPROM reading. 9965 * 9966 * GPOI0 - data in/data out 9967 * GPIO1 - clock 9968 * Symbios NVRAM wiring now also used by Tekram. 9969 */ 9970 9971 #define SET_BIT 0 9972 #define CLR_BIT 1 9973 #define SET_CLK 2 9974 #define CLR_CLK 3 9975 9976 /* 9977 * Set/clear data/clock bit in GPIO0 9978 */ 9979 static void S24C16_set_bit(hcb_p np, u_char write_bit, u_char *gpreg, 9980 int bit_mode) 9981 { 9982 UDELAY (5); 9983 switch (bit_mode){ 9984 case SET_BIT: 9985 *gpreg |= write_bit; 9986 break; 9987 case CLR_BIT: 9988 *gpreg &= 0xfe; 9989 break; 9990 case SET_CLK: 9991 *gpreg |= 0x02; 9992 break; 9993 case CLR_CLK: 9994 *gpreg &= 0xfd; 9995 break; 9996 9997 } 9998 OUTB (nc_gpreg, *gpreg); 9999 UDELAY (5); 10000 } 10001 10002 /* 10003 * Send START condition to NVRAM to wake it up. 10004 */ 10005 static void S24C16_start(hcb_p np, u_char *gpreg) 10006 { 10007 S24C16_set_bit(np, 1, gpreg, SET_BIT); 10008 S24C16_set_bit(np, 0, gpreg, SET_CLK); 10009 S24C16_set_bit(np, 0, gpreg, CLR_BIT); 10010 S24C16_set_bit(np, 0, gpreg, CLR_CLK); 10011 } 10012 10013 /* 10014 * Send STOP condition to NVRAM - puts NVRAM to sleep... ZZzzzz!! 10015 */ 10016 static void S24C16_stop(hcb_p np, u_char *gpreg) 10017 { 10018 S24C16_set_bit(np, 0, gpreg, SET_CLK); 10019 S24C16_set_bit(np, 1, gpreg, SET_BIT); 10020 } 10021 10022 /* 10023 * Read or write a bit to the NVRAM, 10024 * read if GPIO0 input else write if GPIO0 output 10025 */ 10026 static void S24C16_do_bit(hcb_p np, u_char *read_bit, u_char write_bit, 10027 u_char *gpreg) 10028 { 10029 S24C16_set_bit(np, write_bit, gpreg, SET_BIT); 10030 S24C16_set_bit(np, 0, gpreg, SET_CLK); 10031 if (read_bit) 10032 *read_bit = INB (nc_gpreg); 10033 S24C16_set_bit(np, 0, gpreg, CLR_CLK); 10034 S24C16_set_bit(np, 0, gpreg, CLR_BIT); 10035 } 10036 10037 /* 10038 * Output an ACK to the NVRAM after reading, 10039 * change GPIO0 to output and when done back to an input 10040 */ 10041 static void S24C16_write_ack(hcb_p np, u_char write_bit, u_char *gpreg, 10042 u_char *gpcntl) 10043 { 10044 OUTB (nc_gpcntl, *gpcntl & 0xfe); 10045 S24C16_do_bit(np, 0, write_bit, gpreg); 10046 OUTB (nc_gpcntl, *gpcntl); 10047 } 10048 10049 /* 10050 * Input an ACK from NVRAM after writing, 10051 * change GPIO0 to input and when done back to an output 10052 */ 10053 static void S24C16_read_ack(hcb_p np, u_char *read_bit, u_char *gpreg, 10054 u_char *gpcntl) 10055 { 10056 OUTB (nc_gpcntl, *gpcntl | 0x01); 10057 S24C16_do_bit(np, read_bit, 1, gpreg); 10058 OUTB (nc_gpcntl, *gpcntl); 10059 } 10060 10061 /* 10062 * WRITE a byte to the NVRAM and then get an ACK to see it was accepted OK, 10063 * GPIO0 must already be set as an output 10064 */ 10065 static void S24C16_write_byte(hcb_p np, u_char *ack_data, u_char write_data, 10066 u_char *gpreg, u_char *gpcntl) 10067 { 10068 int x; 10069 10070 for (x = 0; x < 8; x++) 10071 S24C16_do_bit(np, 0, (write_data >> (7 - x)) & 0x01, gpreg); 10072 10073 S24C16_read_ack(np, ack_data, gpreg, gpcntl); 10074 } 10075 10076 /* 10077 * READ a byte from the NVRAM and then send an ACK to say we have got it, 10078 * GPIO0 must already be set as an input 10079 */ 10080 static void S24C16_read_byte(hcb_p np, u_char *read_data, u_char ack_data, 10081 u_char *gpreg, u_char *gpcntl) 10082 { 10083 int x; 10084 u_char read_bit; 10085 10086 *read_data = 0; 10087 for (x = 0; x < 8; x++) { 10088 S24C16_do_bit(np, &read_bit, 1, gpreg); 10089 *read_data |= ((read_bit & 0x01) << (7 - x)); 10090 } 10091 10092 S24C16_write_ack(np, ack_data, gpreg, gpcntl); 10093 } 10094 10095 /* 10096 * Read 'len' bytes starting at 'offset'. 10097 */ 10098 static int sym_read_S24C16_nvram (hcb_p np, int offset, u_char *data, int len) 10099 { 10100 u_char gpcntl, gpreg; 10101 u_char old_gpcntl, old_gpreg; 10102 u_char ack_data; 10103 int retv = 1; 10104 int x; 10105 10106 /* save current state of GPCNTL and GPREG */ 10107 old_gpreg = INB (nc_gpreg); 10108 old_gpcntl = INB (nc_gpcntl); 10109 gpcntl = old_gpcntl & 0x1c; 10110 10111 /* set up GPREG & GPCNTL to set GPIO0 and GPIO1 in to known state */ 10112 OUTB (nc_gpreg, old_gpreg); 10113 OUTB (nc_gpcntl, gpcntl); 10114 10115 /* this is to set NVRAM into a known state with GPIO0/1 both low */ 10116 gpreg = old_gpreg; 10117 S24C16_set_bit(np, 0, &gpreg, CLR_CLK); 10118 S24C16_set_bit(np, 0, &gpreg, CLR_BIT); 10119 10120 /* now set NVRAM inactive with GPIO0/1 both high */ 10121 S24C16_stop(np, &gpreg); 10122 10123 /* activate NVRAM */ 10124 S24C16_start(np, &gpreg); 10125 10126 /* write device code and random address MSB */ 10127 S24C16_write_byte(np, &ack_data, 10128 0xa0 | ((offset >> 7) & 0x0e), &gpreg, &gpcntl); 10129 if (ack_data & 0x01) 10130 goto out; 10131 10132 /* write random address LSB */ 10133 S24C16_write_byte(np, &ack_data, 10134 offset & 0xff, &gpreg, &gpcntl); 10135 if (ack_data & 0x01) 10136 goto out; 10137 10138 /* regenerate START state to set up for reading */ 10139 S24C16_start(np, &gpreg); 10140 10141 /* rewrite device code and address MSB with read bit set (lsb = 0x01) */ 10142 S24C16_write_byte(np, &ack_data, 10143 0xa1 | ((offset >> 7) & 0x0e), &gpreg, &gpcntl); 10144 if (ack_data & 0x01) 10145 goto out; 10146 10147 /* now set up GPIO0 for inputting data */ 10148 gpcntl |= 0x01; 10149 OUTB (nc_gpcntl, gpcntl); 10150 10151 /* input all requested data - only part of total NVRAM */ 10152 for (x = 0; x < len; x++) 10153 S24C16_read_byte(np, &data[x], (x == (len-1)), &gpreg, &gpcntl); 10154 10155 /* finally put NVRAM back in inactive mode */ 10156 gpcntl &= 0xfe; 10157 OUTB (nc_gpcntl, gpcntl); 10158 S24C16_stop(np, &gpreg); 10159 retv = 0; 10160 out: 10161 /* return GPIO0/1 to original states after having accessed NVRAM */ 10162 OUTB (nc_gpcntl, old_gpcntl); 10163 OUTB (nc_gpreg, old_gpreg); 10164 10165 return retv; 10166 } 10167 10168 #undef SET_BIT /* 0 */ 10169 #undef CLR_BIT /* 1 */ 10170 #undef SET_CLK /* 2 */ 10171 #undef CLR_CLK /* 3 */ 10172 10173 /* 10174 * Try reading Symbios NVRAM. 10175 * Return 0 if OK. 10176 */ 10177 static int sym_read_Symbios_nvram (hcb_p np, Symbios_nvram *nvram) 10178 { 10179 static u_char Symbios_trailer[6] = {0xfe, 0xfe, 0, 0, 0, 0}; 10180 u_char *data = (u_char *) nvram; 10181 int len = sizeof(*nvram); 10182 u_short csum; 10183 int x; 10184 10185 /* probe the 24c16 and read the SYMBIOS 24c16 area */ 10186 if (sym_read_S24C16_nvram (np, SYMBIOS_NVRAM_ADDRESS, data, len)) 10187 return 1; 10188 10189 /* check valid NVRAM signature, verify byte count and checksum */ 10190 if (nvram->type != 0 || 10191 bcmp(nvram->trailer, Symbios_trailer, 6) || 10192 nvram->byte_count != len - 12) 10193 return 1; 10194 10195 /* verify checksum */ 10196 for (x = 6, csum = 0; x < len - 6; x++) 10197 csum += data[x]; 10198 if (csum != nvram->checksum) 10199 return 1; 10200 10201 return 0; 10202 } 10203 10204 /* 10205 * 93C46 EEPROM reading. 10206 * 10207 * GPOI0 - data in 10208 * GPIO1 - data out 10209 * GPIO2 - clock 10210 * GPIO4 - chip select 10211 * 10212 * Used by Tekram. 10213 */ 10214 10215 /* 10216 * Pulse clock bit in GPIO0 10217 */ 10218 static void T93C46_Clk(hcb_p np, u_char *gpreg) 10219 { 10220 OUTB (nc_gpreg, *gpreg | 0x04); 10221 UDELAY (2); 10222 OUTB (nc_gpreg, *gpreg); 10223 } 10224 10225 /* 10226 * Read bit from NVRAM 10227 */ 10228 static void T93C46_Read_Bit(hcb_p np, u_char *read_bit, u_char *gpreg) 10229 { 10230 UDELAY (2); 10231 T93C46_Clk(np, gpreg); 10232 *read_bit = INB (nc_gpreg); 10233 } 10234 10235 /* 10236 * Write bit to GPIO0 10237 */ 10238 static void T93C46_Write_Bit(hcb_p np, u_char write_bit, u_char *gpreg) 10239 { 10240 if (write_bit & 0x01) 10241 *gpreg |= 0x02; 10242 else 10243 *gpreg &= 0xfd; 10244 10245 *gpreg |= 0x10; 10246 10247 OUTB (nc_gpreg, *gpreg); 10248 UDELAY (2); 10249 10250 T93C46_Clk(np, gpreg); 10251 } 10252 10253 /* 10254 * Send STOP condition to NVRAM - puts NVRAM to sleep... ZZZzzz!! 10255 */ 10256 static void T93C46_Stop(hcb_p np, u_char *gpreg) 10257 { 10258 *gpreg &= 0xef; 10259 OUTB (nc_gpreg, *gpreg); 10260 UDELAY (2); 10261 10262 T93C46_Clk(np, gpreg); 10263 } 10264 10265 /* 10266 * Send read command and address to NVRAM 10267 */ 10268 static void T93C46_Send_Command(hcb_p np, u_short write_data, 10269 u_char *read_bit, u_char *gpreg) 10270 { 10271 int x; 10272 10273 /* send 9 bits, start bit (1), command (2), address (6) */ 10274 for (x = 0; x < 9; x++) 10275 T93C46_Write_Bit(np, (u_char) (write_data >> (8 - x)), gpreg); 10276 10277 *read_bit = INB (nc_gpreg); 10278 } 10279 10280 /* 10281 * READ 2 bytes from the NVRAM 10282 */ 10283 static void T93C46_Read_Word(hcb_p np, u_short *nvram_data, u_char *gpreg) 10284 { 10285 int x; 10286 u_char read_bit; 10287 10288 *nvram_data = 0; 10289 for (x = 0; x < 16; x++) { 10290 T93C46_Read_Bit(np, &read_bit, gpreg); 10291 10292 if (read_bit & 0x01) 10293 *nvram_data |= (0x01 << (15 - x)); 10294 else 10295 *nvram_data &= ~(0x01 << (15 - x)); 10296 } 10297 } 10298 10299 /* 10300 * Read Tekram NvRAM data. 10301 */ 10302 static int T93C46_Read_Data(hcb_p np, u_short *data,int len,u_char *gpreg) 10303 { 10304 u_char read_bit; 10305 int x; 10306 10307 for (x = 0; x < len; x++) { 10308 10309 /* output read command and address */ 10310 T93C46_Send_Command(np, 0x180 | x, &read_bit, gpreg); 10311 if (read_bit & 0x01) 10312 return 1; /* Bad */ 10313 T93C46_Read_Word(np, &data[x], gpreg); 10314 T93C46_Stop(np, gpreg); 10315 } 10316 10317 return 0; 10318 } 10319 10320 /* 10321 * Try reading 93C46 Tekram NVRAM. 10322 */ 10323 static int sym_read_T93C46_nvram (hcb_p np, Tekram_nvram *nvram) 10324 { 10325 u_char gpcntl, gpreg; 10326 u_char old_gpcntl, old_gpreg; 10327 int retv = 1; 10328 10329 /* save current state of GPCNTL and GPREG */ 10330 old_gpreg = INB (nc_gpreg); 10331 old_gpcntl = INB (nc_gpcntl); 10332 10333 /* set up GPREG & GPCNTL to set GPIO0/1/2/4 in to known state, 0 in, 10334 1/2/4 out */ 10335 gpreg = old_gpreg & 0xe9; 10336 OUTB (nc_gpreg, gpreg); 10337 gpcntl = (old_gpcntl & 0xe9) | 0x09; 10338 OUTB (nc_gpcntl, gpcntl); 10339 10340 /* input all of NVRAM, 64 words */ 10341 retv = T93C46_Read_Data(np, (u_short *) nvram, 10342 sizeof(*nvram) / sizeof(short), &gpreg); 10343 10344 /* return GPIO0/1/2/4 to original states after having accessed NVRAM */ 10345 OUTB (nc_gpcntl, old_gpcntl); 10346 OUTB (nc_gpreg, old_gpreg); 10347 10348 return retv; 10349 } 10350 10351 /* 10352 * Try reading Tekram NVRAM. 10353 * Return 0 if OK. 10354 */ 10355 static int sym_read_Tekram_nvram (hcb_p np, Tekram_nvram *nvram) 10356 { 10357 u_char *data = (u_char *) nvram; 10358 int len = sizeof(*nvram); 10359 u_short csum; 10360 int x; 10361 10362 switch (np->device_id) { 10363 case PCI_ID_SYM53C885: 10364 case PCI_ID_SYM53C895: 10365 case PCI_ID_SYM53C896: 10366 x = sym_read_S24C16_nvram(np, TEKRAM_24C16_NVRAM_ADDRESS, 10367 data, len); 10368 break; 10369 case PCI_ID_SYM53C875: 10370 x = sym_read_S24C16_nvram(np, TEKRAM_24C16_NVRAM_ADDRESS, 10371 data, len); 10372 if (!x) 10373 break; 10374 default: 10375 x = sym_read_T93C46_nvram(np, nvram); 10376 break; 10377 } 10378 if (x) 10379 return 1; 10380 10381 /* verify checksum */ 10382 for (x = 0, csum = 0; x < len - 1; x += 2) 10383 csum += data[x] + (data[x+1] << 8); 10384 if (csum != 0x1234) 10385 return 1; 10386 10387 return 0; 10388 } 10389 10390 #endif /* SYM_CONF_NVRAM_SUPPORT */ 10391