1 /******************************************************************************
2 ** High Performance device driver for the Symbios 53C896 controller.
4 ** Copyright (C) 1998-2001 Gerard Roudier <groudier@free.fr>
6 ** This driver also supports all the Symbios 53C8XX controller family,
7 ** except 53C810 revisions < 16, 53C825 revisions < 16 and all
8 ** revisions of 53C815 controllers.
10 ** This driver is based on the Linux port of the FreeBSD ncr driver.
12 ** Copyright (C) 1994 Wolfgang Stanglmeier
14 **-----------------------------------------------------------------------------
16 ** This program is free software; you can redistribute it and/or modify
17 ** it under the terms of the GNU General Public License as published by
18 ** the Free Software Foundation; either version 2 of the License, or
19 ** (at your option) any later version.
21 ** This program is distributed in the hope that it will be useful,
22 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ** GNU General Public License for more details.
26 ** You should have received a copy of the GNU General Public License
27 ** along with this program; if not, write to the Free Software
28 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 **-----------------------------------------------------------------------------
32 ** The Linux port of the FreeBSD ncr driver has been achieved in
35 ** Gerard Roudier <groudier@free.fr>
37 ** Being given that this driver originates from the FreeBSD version, and
38 ** in order to keep synergy on both, any suggested enhancements and corrections
39 ** received on Linux are automatically a potential candidate for the FreeBSD
42 ** The original driver has been written for 386bsd and FreeBSD by
43 ** Wolfgang Stanglmeier <wolf@cologne.de>
44 ** Stefan Esser <se@mi.Uni-Koeln.de>
46 **-----------------------------------------------------------------------------
48 ** Major contributions:
49 ** --------------------
51 ** NVRAM detection and reading.
52 ** Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
54 *******************************************************************************
57 /*==========================================================
61 **==========================================================
64 #define DEBUG_ALLOC (0x0001)
65 #define DEBUG_PHASE (0x0002)
66 #define DEBUG_QUEUE (0x0008)
67 #define DEBUG_RESULT (0x0010)
68 #define DEBUG_POINTER (0x0020)
69 #define DEBUG_SCRIPT (0x0040)
70 #define DEBUG_TINY (0x0080)
71 #define DEBUG_TIMING (0x0100)
72 #define DEBUG_NEGO (0x0200)
73 #define DEBUG_TAGS (0x0400)
74 #define DEBUG_SCATTER (0x0800)
75 #define DEBUG_IC (0x1000)
78 ** Enable/Disable debug messages.
79 ** Can be changed at runtime too.
82 #ifdef SCSI_NCR_DEBUG_INFO_SUPPORT
83 static int ncr_debug = SCSI_NCR_DEBUG_FLAGS;
84 #define DEBUG_FLAGS ncr_debug
86 #define DEBUG_FLAGS SCSI_NCR_DEBUG_FLAGS
89 static inline struct list_head *ncr_list_pop(struct list_head *head)
91 if (!list_empty(head)) {
92 struct list_head *elem = head->next;
105 /*==========================================================
107 ** Simple power of two buddy-like allocator.
109 ** This simple code is not intended to be fast, but to
110 ** provide power of 2 aligned memory allocations.
111 ** Since the SCRIPTS processor only supplies 8 bit
112 ** arithmetic, this allocator allows simple and fast
113 ** address calculations from the SCRIPTS code.
114 ** In addition, cache line alignment is guaranteed for
115 ** power of 2 cache line size.
116 ** Enhanced in linux-2.3.44 to provide a memory pool
117 ** per pcidev to support dynamic dma mapping. (I would
118 ** have preferred a real bus astraction, btw).
120 **==========================================================
123 #define MEMO_SHIFT 4 /* 16 bytes minimum memory chunk */
124 #if PAGE_SIZE >= 8192
125 #define MEMO_PAGE_ORDER 0 /* 1 PAGE maximum */
127 #define MEMO_PAGE_ORDER 1 /* 2 PAGES maximum */
129 #define MEMO_FREE_UNUSED /* Free unused pages immediately */
131 #define MEMO_GFP_FLAGS GFP_ATOMIC
132 #define MEMO_CLUSTER_SHIFT (PAGE_SHIFT+MEMO_PAGE_ORDER)
133 #define MEMO_CLUSTER_SIZE (1UL << MEMO_CLUSTER_SHIFT)
134 #define MEMO_CLUSTER_MASK (MEMO_CLUSTER_SIZE-1)
136 typedef u_long m_addr_t; /* Enough bits to bit-hack addresses */
137 typedef struct device *m_bush_t; /* Something that addresses DMAable */
139 typedef struct m_link { /* Link between free memory chunks */
143 typedef struct m_vtob { /* Virtual to Bus address translation */
148 #define VTOB_HASH_SHIFT 5
149 #define VTOB_HASH_SIZE (1UL << VTOB_HASH_SHIFT)
150 #define VTOB_HASH_MASK (VTOB_HASH_SIZE-1)
151 #define VTOB_HASH_CODE(m) \
152 ((((m_addr_t) (m)) >> MEMO_CLUSTER_SHIFT) & VTOB_HASH_MASK)
154 typedef struct m_pool { /* Memory pool of a given kind */
156 m_addr_t (*getp)(struct m_pool *);
157 void (*freep)(struct m_pool *, m_addr_t);
159 m_vtob_s *(vtob[VTOB_HASH_SIZE]);
161 struct m_link h[PAGE_SHIFT-MEMO_SHIFT+MEMO_PAGE_ORDER+1];
164 static void *___m_alloc(m_pool_s *mp, int size)
167 int s = (1 << MEMO_SHIFT);
172 if (size > (PAGE_SIZE << MEMO_PAGE_ORDER))
182 if (s == (PAGE_SIZE << MEMO_PAGE_ORDER)) {
183 h[j].next = (m_link_s *)mp->getp(mp);
185 h[j].next->next = NULL;
191 a = (m_addr_t) h[j].next;
193 h[j].next = h[j].next->next;
197 h[j].next = (m_link_s *) (a+s);
198 h[j].next->next = NULL;
202 printk("___m_alloc(%d) = %p\n", size, (void *) a);
207 static void ___m_free(m_pool_s *mp, void *ptr, int size)
210 int s = (1 << MEMO_SHIFT);
216 printk("___m_free(%p, %d)\n", ptr, size);
219 if (size > (PAGE_SIZE << MEMO_PAGE_ORDER))
230 #ifdef MEMO_FREE_UNUSED
231 if (s == (PAGE_SIZE << MEMO_PAGE_ORDER)) {
238 while (q->next && q->next != (m_link_s *) b) {
242 ((m_link_s *) a)->next = h[i].next;
243 h[i].next = (m_link_s *) a;
246 q->next = q->next->next;
253 static DEFINE_SPINLOCK(ncr53c8xx_lock);
255 static void *__m_calloc2(m_pool_s *mp, int size, char *name, int uflags)
259 p = ___m_alloc(mp, size);
261 if (DEBUG_FLAGS & DEBUG_ALLOC)
262 printk ("new %-10s[%4d] @%p.\n", name, size, p);
266 else if (uflags & MEMO_WARN)
267 printk (NAME53C8XX ": failed to allocate %s[%d]\n", name, size);
272 #define __m_calloc(mp, s, n) __m_calloc2(mp, s, n, MEMO_WARN)
274 static void __m_free(m_pool_s *mp, void *ptr, int size, char *name)
276 if (DEBUG_FLAGS & DEBUG_ALLOC)
277 printk ("freeing %-10s[%4d] @%p.\n", name, size, ptr);
279 ___m_free(mp, ptr, size);
284 * With pci bus iommu support, we use a default pool of unmapped memory
285 * for memory we donnot need to DMA from/to and one pool per pcidev for
286 * memory accessed by the PCI chip. `mp0' is the default not DMAable pool.
289 static m_addr_t ___mp0_getp(m_pool_s *mp)
291 m_addr_t m = __get_free_pages(MEMO_GFP_FLAGS, MEMO_PAGE_ORDER);
297 static void ___mp0_freep(m_pool_s *mp, m_addr_t m)
299 free_pages(m, MEMO_PAGE_ORDER);
303 static m_pool_s mp0 = {NULL, ___mp0_getp, ___mp0_freep};
310 * With pci bus iommu support, we maintain one pool per pcidev and a
311 * hashed reverse table for virtual to bus physical address translations.
313 static m_addr_t ___dma_getp(m_pool_s *mp)
318 vbp = __m_calloc(&mp0, sizeof(*vbp), "VTOB");
321 vp = (m_addr_t) dma_alloc_coherent(mp->bush,
322 PAGE_SIZE<<MEMO_PAGE_ORDER,
325 int hc = VTOB_HASH_CODE(vp);
328 vbp->next = mp->vtob[hc];
335 __m_free(&mp0, vbp, sizeof(*vbp), "VTOB");
339 static void ___dma_freep(m_pool_s *mp, m_addr_t m)
341 m_vtob_s **vbpp, *vbp;
342 int hc = VTOB_HASH_CODE(m);
344 vbpp = &mp->vtob[hc];
345 while (*vbpp && (*vbpp)->vaddr != m)
346 vbpp = &(*vbpp)->next;
349 *vbpp = (*vbpp)->next;
350 dma_free_coherent(mp->bush, PAGE_SIZE<<MEMO_PAGE_ORDER,
351 (void *)vbp->vaddr, (dma_addr_t)vbp->baddr);
352 __m_free(&mp0, vbp, sizeof(*vbp), "VTOB");
357 static inline m_pool_s *___get_dma_pool(m_bush_t bush)
360 for (mp = mp0.next; mp && mp->bush != bush; mp = mp->next);
364 static m_pool_s *___cre_dma_pool(m_bush_t bush)
367 mp = __m_calloc(&mp0, sizeof(*mp), "MPOOL");
369 memset(mp, 0, sizeof(*mp));
371 mp->getp = ___dma_getp;
372 mp->freep = ___dma_freep;
379 static void ___del_dma_pool(m_pool_s *p)
381 struct m_pool **pp = &mp0.next;
383 while (*pp && *pp != p)
387 __m_free(&mp0, p, sizeof(*p), "MPOOL");
391 static void *__m_calloc_dma(m_bush_t bush, int size, char *name)
397 spin_lock_irqsave(&ncr53c8xx_lock, flags);
398 mp = ___get_dma_pool(bush);
400 mp = ___cre_dma_pool(bush);
402 m = __m_calloc(mp, size, name);
405 spin_unlock_irqrestore(&ncr53c8xx_lock, flags);
410 static void __m_free_dma(m_bush_t bush, void *m, int size, char *name)
415 spin_lock_irqsave(&ncr53c8xx_lock, flags);
416 mp = ___get_dma_pool(bush);
418 __m_free(mp, m, size, name);
421 spin_unlock_irqrestore(&ncr53c8xx_lock, flags);
424 static m_addr_t __vtobus(m_bush_t bush, void *m)
428 int hc = VTOB_HASH_CODE(m);
430 m_addr_t a = ((m_addr_t) m) & ~MEMO_CLUSTER_MASK;
432 spin_lock_irqsave(&ncr53c8xx_lock, flags);
433 mp = ___get_dma_pool(bush);
436 while (vp && (m_addr_t) vp->vaddr != a)
439 spin_unlock_irqrestore(&ncr53c8xx_lock, flags);
440 return vp ? vp->baddr + (((m_addr_t) m) - a) : 0;
443 #define _m_calloc_dma(np, s, n) __m_calloc_dma(np->dev, s, n)
444 #define _m_free_dma(np, p, s, n) __m_free_dma(np->dev, p, s, n)
445 #define m_calloc_dma(s, n) _m_calloc_dma(np, s, n)
446 #define m_free_dma(p, s, n) _m_free_dma(np, p, s, n)
447 #define _vtobus(np, p) __vtobus(np->dev, p)
448 #define vtobus(p) _vtobus(np, p)
451 * Deal with DMA mapping/unmapping.
454 /* To keep track of the dma mapping (sg/single) that has been set */
455 #define __data_mapped SCp.phase
456 #define __data_mapping SCp.have_data_in
458 static void __unmap_scsi_data(struct device *dev, struct scsi_cmnd *cmd)
460 switch(cmd->__data_mapped) {
462 dma_unmap_sg(dev, cmd->buffer, cmd->use_sg,
463 cmd->sc_data_direction);
466 dma_unmap_single(dev, cmd->__data_mapping,
467 cmd->request_bufflen,
468 cmd->sc_data_direction);
471 cmd->__data_mapped = 0;
474 static u_long __map_scsi_single_data(struct device *dev, struct scsi_cmnd *cmd)
478 if (cmd->request_bufflen == 0)
481 mapping = dma_map_single(dev, cmd->request_buffer,
482 cmd->request_bufflen,
483 cmd->sc_data_direction);
484 cmd->__data_mapped = 1;
485 cmd->__data_mapping = mapping;
490 static int __map_scsi_sg_data(struct device *dev, struct scsi_cmnd *cmd)
494 if (cmd->use_sg == 0)
497 use_sg = dma_map_sg(dev, cmd->buffer, cmd->use_sg,
498 cmd->sc_data_direction);
499 cmd->__data_mapped = 2;
500 cmd->__data_mapping = use_sg;
505 #define unmap_scsi_data(np, cmd) __unmap_scsi_data(np->dev, cmd)
506 #define map_scsi_single_data(np, cmd) __map_scsi_single_data(np->dev, cmd)
507 #define map_scsi_sg_data(np, cmd) __map_scsi_sg_data(np->dev, cmd)
509 /*==========================================================
513 ** This structure is initialized from linux config
514 ** options. It can be overridden at boot-up by the boot
517 **==========================================================
519 static struct ncr_driver_setup
520 driver_setup = SCSI_NCR_DRIVER_SETUP;
522 #ifdef SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT
523 static struct ncr_driver_setup
524 driver_safe_setup __initdata = SCSI_NCR_DRIVER_SAFE_SETUP;
527 #define initverbose (driver_setup.verbose)
528 #define bootverbose (np->verbose)
531 /*===================================================================
533 ** Driver setup from the boot command line
535 **===================================================================
545 #define OPT_MASTER_PARITY 2
546 #define OPT_SCSI_PARITY 3
547 #define OPT_DISCONNECTION 4
548 #define OPT_SPECIAL_FEATURES 5
549 #define OPT_UNUSED_1 6
550 #define OPT_FORCE_SYNC_NEGO 7
551 #define OPT_REVERSE_PROBE 8
552 #define OPT_DEFAULT_SYNC 9
553 #define OPT_VERBOSE 10
555 #define OPT_BURST_MAX 12
556 #define OPT_LED_PIN 13
557 #define OPT_MAX_WIDE 14
558 #define OPT_SETTLE_DELAY 15
559 #define OPT_DIFF_SUPPORT 16
561 #define OPT_PCI_FIX_UP 18
562 #define OPT_BUS_CHECK 19
563 #define OPT_OPTIMIZE 20
564 #define OPT_RECOVERY 21
565 #define OPT_SAFE_SETUP 22
566 #define OPT_USE_NVRAM 23
567 #define OPT_EXCLUDE 24
568 #define OPT_HOST_ID 25
570 #ifdef SCSI_NCR_IARB_SUPPORT
574 static char setup_token[] __initdata =
588 #ifdef SCSI_NCR_IARB_SUPPORT
591 ; /* DONNOT REMOVE THIS ';' */
599 static int __init get_setup_token(char *p)
601 char *cur = setup_token;
605 while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
608 if (!strncmp(p, cur, pc - cur))
616 static int __init sym53c8xx__setup(char *str)
618 #ifdef SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT
624 while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
636 val = (int) simple_strtoul(pv, &pe, 0);
638 switch (get_setup_token(cur)) {
640 driver_setup.default_tags = val;
641 if (pe && *pe == '/') {
643 while (*pe && *pe != ARG_SEP &&
644 i < sizeof(driver_setup.tag_ctrl)-1) {
645 driver_setup.tag_ctrl[i++] = *pe++;
647 driver_setup.tag_ctrl[i] = '\0';
650 case OPT_MASTER_PARITY:
651 driver_setup.master_parity = val;
653 case OPT_SCSI_PARITY:
654 driver_setup.scsi_parity = val;
656 case OPT_DISCONNECTION:
657 driver_setup.disconnection = val;
659 case OPT_SPECIAL_FEATURES:
660 driver_setup.special_features = val;
662 case OPT_FORCE_SYNC_NEGO:
663 driver_setup.force_sync_nego = val;
665 case OPT_REVERSE_PROBE:
666 driver_setup.reverse_probe = val;
668 case OPT_DEFAULT_SYNC:
669 driver_setup.default_sync = val;
672 driver_setup.verbose = val;
675 driver_setup.debug = val;
678 driver_setup.burst_max = val;
681 driver_setup.led_pin = val;
684 driver_setup.max_wide = val? 1:0;
686 case OPT_SETTLE_DELAY:
687 driver_setup.settle_delay = val;
689 case OPT_DIFF_SUPPORT:
690 driver_setup.diff_support = val;
693 driver_setup.irqm = val;
696 driver_setup.pci_fix_up = val;
699 driver_setup.bus_check = val;
702 driver_setup.optimize = val;
705 driver_setup.recovery = val;
708 driver_setup.use_nvram = val;
711 memcpy(&driver_setup, &driver_safe_setup,
712 sizeof(driver_setup));
715 if (xi < SCSI_NCR_MAX_EXCLUDES)
716 driver_setup.excludes[xi++] = val;
719 driver_setup.host_id = val;
721 #ifdef SCSI_NCR_IARB_SUPPORT
723 driver_setup.iarb = val;
727 printk("sym53c8xx_setup: unexpected boot option '%.*s' ignored\n", (int)(pc-cur+1), cur);
731 if ((cur = strchr(cur, ARG_SEP)) != NULL)
734 #endif /* SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT */
738 /*===================================================================
740 ** Get device queue depth from boot command line.
742 **===================================================================
744 #define DEF_DEPTH (driver_setup.default_tags)
745 #define ALL_TARGETS -2
750 static int device_queue_depth(int unit, int target, int lun)
753 char *p = driver_setup.tag_ctrl;
759 while ((c = *p++) != 0) {
760 v = simple_strtoul(p, &ep, 0);
769 t = (target == v) ? v : NO_TARGET;
774 u = (lun == v) ? v : NO_LUN;
778 (t == ALL_TARGETS || t == target) &&
779 (u == ALL_LUNS || u == lun))