patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / scsi / aic7xxx / aic7xxx_osm.h
1 /*
2  * Adaptec AIC7xxx device driver for Linux.
3  *
4  * Copyright (c) 1994 John Aycock
5  *   The University of Calgary Department of Computer Science.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; see the file COPYING.  If not, write to
19  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20  * 
21  * Copyright (c) 2000-2003 Adaptec Inc.
22  * All rights reserved.
23  *
24  * Redistribution and use in source and binary forms, with or without
25  * modification, are permitted provided that the following conditions
26  * are met:
27  * 1. Redistributions of source code must retain the above copyright
28  *    notice, this list of conditions, and the following disclaimer,
29  *    without modification.
30  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
31  *    substantially similar to the "NO WARRANTY" disclaimer below
32  *    ("Disclaimer") and any redistribution must be conditioned upon
33  *    including a substantially similar Disclaimer requirement for further
34  *    binary redistribution.
35  * 3. Neither the names of the above-listed copyright holders nor the names
36  *    of any contributors may be used to endorse or promote products derived
37  *    from this software without specific prior written permission.
38  *
39  * Alternatively, this software may be distributed under the terms of the
40  * GNU General Public License ("GPL") version 2 as published by the Free
41  * Software Foundation.
42  *
43  * NO WARRANTY
44  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
45  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
46  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
47  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
48  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
52  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
53  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
54  * POSSIBILITY OF SUCH DAMAGES.
55  *
56  * $Id: //depot/aic7xxx/linux/drivers/scsi/aic7xxx/aic7xxx_osm.h#151 $
57  *
58  */
59 #ifndef _AIC7XXX_LINUX_H_
60 #define _AIC7XXX_LINUX_H_
61
62 #include <linux/types.h>
63 #include <linux/blkdev.h>
64 #include <linux/delay.h>
65 #include <linux/ioport.h>
66 #include <linux/pci.h>
67 #include <linux/smp_lock.h>
68 #include <linux/version.h>
69 #include <linux/module.h>
70 #include <asm/byteorder.h>
71 #include <asm/io.h>
72
73 #ifndef KERNEL_VERSION
74 #define KERNEL_VERSION(x,y,z) (((x)<<16)+((y)<<8)+(z))
75 #endif
76
77 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
78 #include <linux/interrupt.h> /* For tasklet support. */
79 #include <linux/config.h>
80 #include <linux/slab.h>
81 #else
82 #include <linux/malloc.h>
83 #endif
84
85 /* Core SCSI definitions */
86 #define AIC_LIB_PREFIX ahc
87 #include "scsi.h"
88 #include "hosts.h"
89
90 /* Name space conflict with BSD queue macros */
91 #ifdef LIST_HEAD
92 #undef LIST_HEAD
93 #endif
94
95 #include "cam.h"
96 #include "queue.h"
97 #include "scsi_message.h"
98 #include "aiclib.h"
99
100 /*********************************** Debugging ********************************/
101 #ifdef CONFIG_AIC7XXX_DEBUG_ENABLE
102 #ifdef CONFIG_AIC7XXX_DEBUG_MASK
103 #define AHC_DEBUG 1
104 #define AHC_DEBUG_OPTS CONFIG_AIC7XXX_DEBUG_MASK
105 #else
106 /*
107  * Compile in debugging code, but do not enable any printfs.
108  */
109 #define AHC_DEBUG 1
110 #endif
111 /* No debugging code. */
112 #endif
113
114 /************************* Forward Declarations *******************************/
115 struct ahc_softc;
116 typedef struct pci_dev *ahc_dev_softc_t;
117 typedef Scsi_Cmnd      *ahc_io_ctx_t;
118
119 /******************************* Byte Order ***********************************/
120 #define ahc_htobe16(x)  cpu_to_be16(x)
121 #define ahc_htobe32(x)  cpu_to_be32(x)
122 #define ahc_htobe64(x)  cpu_to_be64(x)
123 #define ahc_htole16(x)  cpu_to_le16(x)
124 #define ahc_htole32(x)  cpu_to_le32(x)
125 #define ahc_htole64(x)  cpu_to_le64(x)
126
127 #define ahc_be16toh(x)  be16_to_cpu(x)
128 #define ahc_be32toh(x)  be32_to_cpu(x)
129 #define ahc_be64toh(x)  be64_to_cpu(x)
130 #define ahc_le16toh(x)  le16_to_cpu(x)
131 #define ahc_le32toh(x)  le32_to_cpu(x)
132 #define ahc_le64toh(x)  le64_to_cpu(x)
133
134 #ifndef LITTLE_ENDIAN
135 #define LITTLE_ENDIAN 1234
136 #endif
137
138 #ifndef BIG_ENDIAN
139 #define BIG_ENDIAN 4321
140 #endif
141
142 #ifndef BYTE_ORDER
143 #if defined(__BIG_ENDIAN)
144 #define BYTE_ORDER BIG_ENDIAN
145 #endif
146 #if defined(__LITTLE_ENDIAN)
147 #define BYTE_ORDER LITTLE_ENDIAN
148 #endif
149 #endif /* BYTE_ORDER */
150
151 /************************* Configuration Data *********************************/
152 extern u_int aic7xxx_no_probe;
153 extern u_int aic7xxx_allow_memio;
154 extern int aic7xxx_detect_complete;
155 extern Scsi_Host_Template aic7xxx_driver_template;
156
157 /***************************** Bus Space/DMA **********************************/
158
159 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,2,17)
160 typedef dma_addr_t bus_addr_t;
161 #else
162 typedef uint32_t bus_addr_t;
163 #endif
164 typedef uint32_t bus_size_t;
165
166 typedef enum {
167         BUS_SPACE_MEMIO,
168         BUS_SPACE_PIO
169 } bus_space_tag_t;
170
171 typedef union {
172         u_long            ioport;
173         volatile uint8_t *maddr;
174 } bus_space_handle_t;
175
176 typedef struct bus_dma_segment
177 {
178         bus_addr_t      ds_addr;
179         bus_size_t      ds_len;
180 } bus_dma_segment_t;
181
182 struct ahc_linux_dma_tag
183 {
184         bus_size_t      alignment;
185         bus_size_t      boundary;
186         bus_size_t      maxsize;
187 };
188 typedef struct ahc_linux_dma_tag* bus_dma_tag_t;
189
190 struct ahc_linux_dmamap
191 {
192         bus_addr_t      bus_addr;
193 };
194 typedef struct ahc_linux_dmamap* bus_dmamap_t;
195
196 typedef int bus_dma_filter_t(void*, bus_addr_t);
197 typedef void bus_dmamap_callback_t(void *, bus_dma_segment_t *, int, int);
198
199 #define BUS_DMA_WAITOK          0x0
200 #define BUS_DMA_NOWAIT          0x1
201 #define BUS_DMA_ALLOCNOW        0x2
202 #define BUS_DMA_LOAD_SEGS       0x4     /*
203                                          * Argument is an S/G list not
204                                          * a single buffer.
205                                          */
206
207 #define BUS_SPACE_MAXADDR       0xFFFFFFFF
208 #define BUS_SPACE_MAXADDR_32BIT 0xFFFFFFFF
209 #define BUS_SPACE_MAXSIZE_32BIT 0xFFFFFFFF
210
211 int     ahc_dma_tag_create(struct ahc_softc *, bus_dma_tag_t /*parent*/,
212                            bus_size_t /*alignment*/, bus_size_t /*boundary*/,
213                            bus_addr_t /*lowaddr*/, bus_addr_t /*highaddr*/,
214                            bus_dma_filter_t*/*filter*/, void */*filterarg*/,
215                            bus_size_t /*maxsize*/, int /*nsegments*/,
216                            bus_size_t /*maxsegsz*/, int /*flags*/,
217                            bus_dma_tag_t */*dma_tagp*/);
218
219 void    ahc_dma_tag_destroy(struct ahc_softc *, bus_dma_tag_t /*tag*/);
220
221 int     ahc_dmamem_alloc(struct ahc_softc *, bus_dma_tag_t /*dmat*/,
222                          void** /*vaddr*/, int /*flags*/,
223                          bus_dmamap_t* /*mapp*/);
224
225 void    ahc_dmamem_free(struct ahc_softc *, bus_dma_tag_t /*dmat*/,
226                         void* /*vaddr*/, bus_dmamap_t /*map*/);
227
228 void    ahc_dmamap_destroy(struct ahc_softc *, bus_dma_tag_t /*tag*/,
229                            bus_dmamap_t /*map*/);
230
231 int     ahc_dmamap_load(struct ahc_softc *ahc, bus_dma_tag_t /*dmat*/,
232                         bus_dmamap_t /*map*/, void * /*buf*/,
233                         bus_size_t /*buflen*/, bus_dmamap_callback_t *,
234                         void */*callback_arg*/, int /*flags*/);
235
236 int     ahc_dmamap_unload(struct ahc_softc *, bus_dma_tag_t, bus_dmamap_t);
237
238 /*
239  * Operations performed by ahc_dmamap_sync().
240  */
241 #define BUS_DMASYNC_PREREAD     0x01    /* pre-read synchronization */
242 #define BUS_DMASYNC_POSTREAD    0x02    /* post-read synchronization */
243 #define BUS_DMASYNC_PREWRITE    0x04    /* pre-write synchronization */
244 #define BUS_DMASYNC_POSTWRITE   0x08    /* post-write synchronization */
245
246 /*
247  * XXX
248  * ahc_dmamap_sync is only used on buffers allocated with
249  * the pci_alloc_consistent() API.  Although I'm not sure how
250  * this works on architectures with a write buffer, Linux does
251  * not have an API to sync "coherent" memory.  Perhaps we need
252  * to do an mb()?
253  */
254 #define ahc_dmamap_sync(ahc, dma_tag, dmamap, offset, len, op)
255
256 /************************** Timer DataStructures ******************************/
257 typedef struct timer_list ahc_timer_t;
258
259 /********************************** Includes **********************************/
260 #ifdef CONFIG_AIC7XXX_REG_PRETTY_PRINT
261 #define AIC_DEBUG_REGISTERS 1
262 #else
263 #define AIC_DEBUG_REGISTERS 0
264 #endif
265 #include "aic7xxx.h"
266
267 /***************************** Timer Facilities *******************************/
268 #define ahc_timer_init init_timer
269 #define ahc_timer_stop del_timer_sync
270 typedef void ahc_linux_callback_t (u_long);  
271 static __inline void ahc_timer_reset(ahc_timer_t *timer, int usec,
272                                      ahc_callback_t *func, void *arg);
273 static __inline void ahc_scb_timer_reset(struct scb *scb, u_int usec);
274
275 static __inline void
276 ahc_timer_reset(ahc_timer_t *timer, int usec, ahc_callback_t *func, void *arg)
277 {
278         struct ahc_softc *ahc;
279
280         ahc = (struct ahc_softc *)arg;
281         del_timer(timer);
282         timer->data = (u_long)arg;
283         timer->expires = jiffies + (usec * HZ)/1000000;
284         timer->function = (ahc_linux_callback_t*)func;
285         add_timer(timer);
286 }
287
288 static __inline void
289 ahc_scb_timer_reset(struct scb *scb, u_int usec)
290 {
291         mod_timer(&scb->io_ctx->eh_timeout, jiffies + (usec * HZ)/1000000);
292 }
293
294 /***************************** SMP support ************************************/
295 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,17)
296 #include <linux/spinlock.h>
297 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,93)
298 #include <linux/smp.h>
299 #endif
300
301 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0) || defined(SCSI_HAS_HOST_LOCK))
302 #define AHC_SCSI_HAS_HOST_LOCK 1
303 #else
304 #define AHC_SCSI_HAS_HOST_LOCK 0
305 #endif
306
307 #define AIC7XXX_DRIVER_VERSION "6.2.36"
308
309 /**************************** Front End Queues ********************************/
310 /*
311  * Data structure used to cast the Linux struct scsi_cmnd to something
312  * that allows us to use the queue macros.  The linux structure has
313  * plenty of space to hold the links fields as required by the queue
314  * macros, but the queue macors require them to have the correct type.
315  */
316 struct ahc_cmd_internal {
317         /* Area owned by the Linux scsi layer. */
318         uint8_t private[offsetof(struct scsi_cmnd, SCp.Status)];
319         union {
320                 STAILQ_ENTRY(ahc_cmd)   ste;
321                 LIST_ENTRY(ahc_cmd)     le;
322                 TAILQ_ENTRY(ahc_cmd)    tqe;
323         } links;
324         uint32_t                        end;
325 };
326
327 struct ahc_cmd {
328         union {
329                 struct ahc_cmd_internal icmd;
330                 struct scsi_cmnd        scsi_cmd;
331         } un;
332 };
333
334 #define acmd_icmd(cmd) ((cmd)->un.icmd)
335 #define acmd_scsi_cmd(cmd) ((cmd)->un.scsi_cmd)
336 #define acmd_links un.icmd.links
337
338 /*************************** Device Data Structures ***************************/
339 /*
340  * A per probed device structure used to deal with some error recovery
341  * scenarios that the Linux mid-layer code just doesn't know how to
342  * handle.  The structure allocated for a device only becomes persistent
343  * after a successfully completed inquiry command to the target when
344  * that inquiry data indicates a lun is present.
345  */
346 TAILQ_HEAD(ahc_busyq, ahc_cmd);
347 typedef enum {
348         AHC_DEV_UNCONFIGURED     = 0x01,
349         AHC_DEV_FREEZE_TIL_EMPTY = 0x02, /* Freeze queue until active == 0 */
350         AHC_DEV_TIMER_ACTIVE     = 0x04, /* Our timer is active */
351         AHC_DEV_ON_RUN_LIST      = 0x08, /* Queued to be run later */
352         AHC_DEV_Q_BASIC          = 0x10, /* Allow basic device queuing */
353         AHC_DEV_Q_TAGGED         = 0x20, /* Allow full SCSI2 command queueing */
354         AHC_DEV_PERIODIC_OTAG    = 0x40, /* Send OTAG to prevent starvation */
355         AHC_DEV_SLAVE_CONFIGURED = 0x80  /* slave_configure() has been called */
356 } ahc_linux_dev_flags;
357
358 struct ahc_linux_target;
359 struct ahc_linux_device {
360         TAILQ_ENTRY(ahc_linux_device) links;
361         struct          ahc_busyq busyq;
362
363         /*
364          * The number of transactions currently
365          * queued to the device.
366          */
367         int                     active;
368
369         /*
370          * The currently allowed number of 
371          * transactions that can be queued to
372          * the device.  Must be signed for
373          * conversion from tagged to untagged
374          * mode where the device may have more
375          * than one outstanding active transaction.
376          */
377         int                     openings;
378
379         /*
380          * A positive count indicates that this
381          * device's queue is halted.
382          */
383         u_int                   qfrozen;
384         
385         /*
386          * Cumulative command counter.
387          */
388         u_long                  commands_issued;
389
390         /*
391          * The number of tagged transactions when
392          * running at our current opening level
393          * that have been successfully received by
394          * this device since the last QUEUE FULL.
395          */
396         u_int                   tag_success_count;
397 #define AHC_TAG_SUCCESS_INTERVAL 50
398
399         ahc_linux_dev_flags     flags;
400
401         /*
402          * Per device timer.
403          */
404         struct timer_list       timer;
405
406         /*
407          * The high limit for the tags variable.
408          */
409         u_int                   maxtags;
410
411         /*
412          * The computed number of tags outstanding
413          * at the time of the last QUEUE FULL event.
414          */
415         u_int                   tags_on_last_queuefull;
416
417         /*
418          * How many times we have seen a queue full
419          * with the same number of tags.  This is used
420          * to stop our adaptive queue depth algorithm
421          * on devices with a fixed number of tags.
422          */
423         u_int                   last_queuefull_same_count;
424 #define AHC_LOCK_TAGS_COUNT 50
425
426         /*
427          * How many transactions have been queued
428          * without the device going idle.  We use
429          * this statistic to determine when to issue
430          * an ordered tag to prevent transaction
431          * starvation.  This statistic is only updated
432          * if the AHC_DEV_PERIODIC_OTAG flag is set
433          * on this device.
434          */
435         u_int                   commands_since_idle_or_otag;
436 #define AHC_OTAG_THRESH 500
437
438         int                     lun;
439         Scsi_Device            *scsi_device;
440         struct                  ahc_linux_target *target;
441 };
442
443 typedef enum {
444         AHC_DV_REQUIRED          = 0x01,
445         AHC_INQ_VALID            = 0x02,
446         AHC_BASIC_DV             = 0x04,
447         AHC_ENHANCED_DV          = 0x08
448 } ahc_linux_targ_flags;
449
450 /* DV States */
451 typedef enum {
452         AHC_DV_STATE_EXIT = 0,
453         AHC_DV_STATE_INQ_SHORT_ASYNC,
454         AHC_DV_STATE_INQ_ASYNC,
455         AHC_DV_STATE_INQ_ASYNC_VERIFY,
456         AHC_DV_STATE_TUR,
457         AHC_DV_STATE_REBD,
458         AHC_DV_STATE_INQ_VERIFY,
459         AHC_DV_STATE_WEB,
460         AHC_DV_STATE_REB,
461         AHC_DV_STATE_SU,
462         AHC_DV_STATE_BUSY
463 } ahc_dv_state;
464
465 struct ahc_linux_target {
466         struct ahc_linux_device  *devices[AHC_NUM_LUNS];
467         int                       channel;
468         int                       target;
469         int                       refcount;
470         struct ahc_transinfo      last_tinfo;
471         struct ahc_softc         *ahc;
472         ahc_linux_targ_flags      flags;
473         struct scsi_inquiry_data *inq_data;
474         /*
475          * The next "fallback" period to use for narrow/wide transfers.
476          */
477         uint8_t                   dv_next_narrow_period;
478         uint8_t                   dv_next_wide_period;
479         uint8_t                   dv_max_width;
480         uint8_t                   dv_max_ppr_options;
481         uint8_t                   dv_last_ppr_options;
482         u_int                     dv_echo_size;
483         ahc_dv_state              dv_state;
484         u_int                     dv_state_retry;
485         char                     *dv_buffer;
486         char                     *dv_buffer1;
487 };
488
489 /********************* Definitions Required by the Core ***********************/
490 /*
491  * Number of SG segments we require.  So long as the S/G segments for
492  * a particular transaction are allocated in a physically contiguous
493  * manner and are allocated below 4GB, the number of S/G segments is
494  * unrestricted.
495  */
496 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
497 /*
498  * We dynamically adjust the number of segments in pre-2.5 kernels to
499  * avoid fragmentation issues in the SCSI mid-layer's private memory
500  * allocator.  See aic7xxx_osm.c ahc_linux_size_nseg() for details.
501  */
502 extern u_int ahc_linux_nseg;
503 #define AHC_NSEG ahc_linux_nseg
504 #define AHC_LINUX_MIN_NSEG 64
505 #else
506 #define AHC_NSEG 128
507 #endif
508
509 /*
510  * Per-SCB OSM storage.
511  */
512 typedef enum {
513         AHC_UP_EH_SEMAPHORE = 0x1
514 } ahc_linux_scb_flags;
515
516 struct scb_platform_data {
517         struct ahc_linux_device *dev;
518         bus_addr_t               buf_busaddr;
519         uint32_t                 xfer_len;
520 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0)
521         uint32_t                 resid;         /* Transfer residual */
522 #endif
523         uint32_t                 sense_resid;   /* Auto-Sense residual */
524         ahc_linux_scb_flags      flags;
525 };
526
527 /*
528  * Define a structure used for each host adapter.  All members are
529  * aligned on a boundary >= the size of the member to honor the
530  * alignment restrictions of the various platforms supported by
531  * this driver.
532  */
533 typedef enum {
534         AHC_DV_WAIT_SIMQ_EMPTY   = 0x01,
535         AHC_DV_WAIT_SIMQ_RELEASE = 0x02,
536         AHC_DV_ACTIVE            = 0x04,
537         AHC_DV_SHUTDOWN          = 0x08,
538         AHC_RUN_CMPLT_Q_TIMER    = 0x10
539 } ahc_linux_softc_flags;
540
541 TAILQ_HEAD(ahc_completeq, ahc_cmd);
542
543 struct ahc_platform_data {
544         /*
545          * Fields accessed from interrupt context.
546          */
547         struct ahc_linux_target *targets[AHC_NUM_TARGETS]; 
548         TAILQ_HEAD(, ahc_linux_device) device_runq;
549         struct ahc_completeq     completeq;
550
551         spinlock_t               spin_lock;
552 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
553         struct tasklet_struct    runq_tasklet;
554 #endif
555         u_int                    qfrozen;
556         pid_t                    dv_pid;
557         struct timer_list        completeq_timer;
558         struct timer_list        reset_timer;
559         struct semaphore         eh_sem;
560         struct semaphore         dv_sem;
561         struct semaphore         dv_cmd_sem;    /* XXX This needs to be in
562                                                  * the target struct
563                                                  */
564         struct scsi_device      *dv_scsi_dev;
565         struct Scsi_Host        *host;          /* pointer to scsi host */
566 #define AHC_LINUX_NOIRQ ((uint32_t)~0)
567         uint32_t                 irq;           /* IRQ for this adapter */
568         uint32_t                 bios_address;
569         uint32_t                 mem_busaddr;   /* Mem Base Addr */
570         bus_addr_t               hw_dma_mask;
571         ahc_linux_softc_flags    flags;
572 };
573
574 /************************** OS Utility Wrappers *******************************/
575 #define printf printk
576 #define M_NOWAIT GFP_ATOMIC
577 #define M_WAITOK 0
578 #define malloc(size, type, flags) kmalloc(size, flags)
579 #define free(ptr, type) kfree(ptr)
580
581 static __inline void ahc_delay(long);
582 static __inline void
583 ahc_delay(long usec)
584 {
585         /*
586          * udelay on Linux can have problems for
587          * multi-millisecond waits.  Wait at most
588          * 1024us per call.
589          */
590         while (usec > 0) {
591                 udelay(usec % 1024);
592                 usec -= 1024;
593         }
594 }
595
596
597 /***************************** Low Level I/O **********************************/
598 static __inline uint8_t ahc_inb(struct ahc_softc * ahc, long port);
599 static __inline void ahc_outb(struct ahc_softc * ahc, long port, uint8_t val);
600 static __inline void ahc_outsb(struct ahc_softc * ahc, long port,
601                                uint8_t *, int count);
602 static __inline void ahc_insb(struct ahc_softc * ahc, long port,
603                                uint8_t *, int count);
604
605 static __inline uint8_t
606 ahc_inb(struct ahc_softc * ahc, long port)
607 {
608         uint8_t x;
609
610         if (ahc->tag == BUS_SPACE_MEMIO) {
611                 x = readb(ahc->bsh.maddr + port);
612         } else {
613                 x = inb(ahc->bsh.ioport + port);
614         }
615         mb();
616         return (x);
617 }
618
619 static __inline void
620 ahc_outb(struct ahc_softc * ahc, long port, uint8_t val)
621 {
622         if (ahc->tag == BUS_SPACE_MEMIO) {
623                 writeb(val, ahc->bsh.maddr + port);
624         } else {
625                 outb(val, ahc->bsh.ioport + port);
626         }
627         mb();
628 }
629
630 static __inline void
631 ahc_outsb(struct ahc_softc * ahc, long port, uint8_t *array, int count)
632 {
633         int i;
634
635         /*
636          * There is probably a more efficient way to do this on Linux
637          * but we don't use this for anything speed critical and this
638          * should work.
639          */
640         for (i = 0; i < count; i++)
641                 ahc_outb(ahc, port, *array++);
642 }
643
644 static __inline void
645 ahc_insb(struct ahc_softc * ahc, long port, uint8_t *array, int count)
646 {
647         int i;
648
649         /*
650          * There is probably a more efficient way to do this on Linux
651          * but we don't use this for anything speed critical and this
652          * should work.
653          */
654         for (i = 0; i < count; i++)
655                 *array++ = ahc_inb(ahc, port);
656 }
657
658 /**************************** Initialization **********************************/
659 int             ahc_linux_register_host(struct ahc_softc *,
660                                         Scsi_Host_Template *);
661
662 uint64_t        ahc_linux_get_memsize(void);
663
664 /*************************** Pretty Printing **********************************/
665 struct info_str {
666         char *buffer;
667         int length;
668         off_t offset;
669         int pos;
670 };
671
672 void    ahc_format_transinfo(struct info_str *info,
673                              struct ahc_transinfo *tinfo);
674
675 /******************************** Locking *************************************/
676 /* Lock protecting internal data structures */
677 static __inline void ahc_lockinit(struct ahc_softc *);
678 static __inline void ahc_lock(struct ahc_softc *, unsigned long *flags);
679 static __inline void ahc_unlock(struct ahc_softc *, unsigned long *flags);
680
681 /* Lock acquisition and release of the above lock in midlayer entry points. */
682 static __inline void ahc_midlayer_entrypoint_lock(struct ahc_softc *,
683                                                   unsigned long *flags);
684 static __inline void ahc_midlayer_entrypoint_unlock(struct ahc_softc *,
685                                                     unsigned long *flags);
686
687 /* Lock held during command compeletion to the upper layer */
688 static __inline void ahc_done_lockinit(struct ahc_softc *);
689 static __inline void ahc_done_lock(struct ahc_softc *, unsigned long *flags);
690 static __inline void ahc_done_unlock(struct ahc_softc *, unsigned long *flags);
691
692 /* Lock held during ahc_list manipulation and ahc softc frees */
693 extern spinlock_t ahc_list_spinlock;
694 static __inline void ahc_list_lockinit(void);
695 static __inline void ahc_list_lock(unsigned long *flags);
696 static __inline void ahc_list_unlock(unsigned long *flags);
697
698 static __inline void
699 ahc_lockinit(struct ahc_softc *ahc)
700 {
701         spin_lock_init(&ahc->platform_data->spin_lock);
702 }
703
704 static __inline void
705 ahc_lock(struct ahc_softc *ahc, unsigned long *flags)
706 {
707         spin_lock_irqsave(&ahc->platform_data->spin_lock, *flags);
708 }
709
710 static __inline void
711 ahc_unlock(struct ahc_softc *ahc, unsigned long *flags)
712 {
713         spin_unlock_irqrestore(&ahc->platform_data->spin_lock, *flags);
714 }
715
716 static __inline void
717 ahc_midlayer_entrypoint_lock(struct ahc_softc *ahc, unsigned long *flags)
718 {
719         /*
720          * In 2.5.X and some 2.4.X versions, the midlayer takes our
721          * lock just before calling us, so we avoid locking again.
722          * For other kernel versions, the io_request_lock is taken
723          * just before our entry point is called.  In this case, we
724          * trade the io_request_lock for our per-softc lock.
725          */
726 #if AHC_SCSI_HAS_HOST_LOCK == 0
727         spin_unlock(&io_request_lock);
728         spin_lock(&ahc->platform_data->spin_lock);
729 #endif
730 }
731
732 static __inline void
733 ahc_midlayer_entrypoint_unlock(struct ahc_softc *ahc, unsigned long *flags)
734 {
735 #if AHC_SCSI_HAS_HOST_LOCK == 0
736         spin_unlock(&ahc->platform_data->spin_lock);
737         spin_lock(&io_request_lock);
738 #endif
739 }
740
741 static __inline void
742 ahc_done_lockinit(struct ahc_softc *ahc)
743 {
744         /*
745          * In 2.5.X, our own lock is held during completions.
746          * In previous versions, the io_request_lock is used.
747          * In either case, we can't initialize this lock again.
748          */
749 }
750
751 static __inline void
752 ahc_done_lock(struct ahc_softc *ahc, unsigned long *flags)
753 {
754 #if AHC_SCSI_HAS_HOST_LOCK == 0
755         spin_lock_irqsave(&io_request_lock, *flags);
756 #endif
757 }
758
759 static __inline void
760 ahc_done_unlock(struct ahc_softc *ahc, unsigned long *flags)
761 {
762 #if AHC_SCSI_HAS_HOST_LOCK == 0
763         spin_unlock_irqrestore(&io_request_lock, *flags);
764 #endif
765 }
766
767 static __inline void
768 ahc_list_lockinit(void)
769 {
770         spin_lock_init(&ahc_list_spinlock);
771 }
772
773 static __inline void
774 ahc_list_lock(unsigned long *flags)
775 {
776         spin_lock_irqsave(&ahc_list_spinlock, *flags);
777 }
778
779 static __inline void
780 ahc_list_unlock(unsigned long *flags)
781 {
782         spin_unlock_irqrestore(&ahc_list_spinlock, *flags);
783 }
784
785 /******************************* PCI Definitions ******************************/
786 /*
787  * PCIM_xxx: mask to locate subfield in register
788  * PCIR_xxx: config register offset
789  * PCIC_xxx: device class
790  * PCIS_xxx: device subclass
791  * PCIP_xxx: device programming interface
792  * PCIV_xxx: PCI vendor ID (only required to fixup ancient devices)
793  * PCID_xxx: device ID
794  */
795 #define PCIR_DEVVENDOR          0x00
796 #define PCIR_VENDOR             0x00
797 #define PCIR_DEVICE             0x02
798 #define PCIR_COMMAND            0x04
799 #define PCIM_CMD_PORTEN         0x0001
800 #define PCIM_CMD_MEMEN          0x0002
801 #define PCIM_CMD_BUSMASTEREN    0x0004
802 #define PCIM_CMD_MWRICEN        0x0010
803 #define PCIM_CMD_PERRESPEN      0x0040
804 #define PCIM_CMD_SERRESPEN      0x0100
805 #define PCIR_STATUS             0x06
806 #define PCIR_REVID              0x08
807 #define PCIR_PROGIF             0x09
808 #define PCIR_SUBCLASS           0x0a
809 #define PCIR_CLASS              0x0b
810 #define PCIR_CACHELNSZ          0x0c
811 #define PCIR_LATTIMER           0x0d
812 #define PCIR_HEADERTYPE         0x0e
813 #define PCIM_MFDEV              0x80
814 #define PCIR_BIST               0x0f
815 #define PCIR_CAP_PTR            0x34
816
817 /* config registers for header type 0 devices */
818 #define PCIR_MAPS       0x10
819 #define PCIR_SUBVEND_0  0x2c
820 #define PCIR_SUBDEV_0   0x2e
821
822 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
823 extern struct pci_driver aic7xxx_pci_driver;
824 #endif
825
826 typedef enum
827 {
828         AHC_POWER_STATE_D0,
829         AHC_POWER_STATE_D1,
830         AHC_POWER_STATE_D2,
831         AHC_POWER_STATE_D3
832 } ahc_power_state;
833
834 /**************************** VL/EISA Routines ********************************/
835 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0) \
836   && (defined(__i386__) || defined(__alpha__)) \
837   && (!defined(CONFIG_EISA)))
838 #define CONFIG_EISA
839 #endif
840
841 #ifdef CONFIG_EISA
842 extern uint32_t aic7xxx_probe_eisa_vl;
843 int                      ahc_linux_eisa_init(void);
844 void                     ahc_linux_eisa_exit(void);
845 int                      aic7770_map_registers(struct ahc_softc *ahc,
846                                                u_int port);
847 int                      aic7770_map_int(struct ahc_softc *ahc, u_int irq);
848 #else
849 static inline int       ahc_linux_eisa_init(void) {
850         return -ENODEV;
851 }
852 static inline void      ahc_linux_eisa_exit(void) {
853 }
854 #endif
855
856 /******************************* PCI Routines *********************************/
857 #ifdef CONFIG_PCI
858 void                     ahc_power_state_change(struct ahc_softc *ahc,
859                                                 ahc_power_state new_state);
860 int                      ahc_linux_pci_init(void);
861 void                     ahc_linux_pci_exit(void);
862 int                      ahc_pci_map_registers(struct ahc_softc *ahc);
863 int                      ahc_pci_map_int(struct ahc_softc *ahc);
864
865 static __inline uint32_t ahc_pci_read_config(ahc_dev_softc_t pci,
866                                              int reg, int width);
867
868 static __inline uint32_t
869 ahc_pci_read_config(ahc_dev_softc_t pci, int reg, int width)
870 {
871         switch (width) {
872         case 1:
873         {
874                 uint8_t retval;
875
876                 pci_read_config_byte(pci, reg, &retval);
877                 return (retval);
878         }
879         case 2:
880         {
881                 uint16_t retval;
882                 pci_read_config_word(pci, reg, &retval);
883                 return (retval);
884         }
885         case 4:
886         {
887                 uint32_t retval;
888                 pci_read_config_dword(pci, reg, &retval);
889                 return (retval);
890         }
891         default:
892                 panic("ahc_pci_read_config: Read size too big");
893                 /* NOTREACHED */
894                 return (0);
895         }
896 }
897
898 static __inline void ahc_pci_write_config(ahc_dev_softc_t pci,
899                                           int reg, uint32_t value,
900                                           int width);
901
902 static __inline void
903 ahc_pci_write_config(ahc_dev_softc_t pci, int reg, uint32_t value, int width)
904 {
905         switch (width) {
906         case 1:
907                 pci_write_config_byte(pci, reg, value);
908                 break;
909         case 2:
910                 pci_write_config_word(pci, reg, value);
911                 break;
912         case 4:
913                 pci_write_config_dword(pci, reg, value);
914                 break;
915         default:
916                 panic("ahc_pci_write_config: Write size too big");
917                 /* NOTREACHED */
918         }
919 }
920
921 static __inline int ahc_get_pci_function(ahc_dev_softc_t);
922 static __inline int
923 ahc_get_pci_function(ahc_dev_softc_t pci)
924 {
925         return (PCI_FUNC(pci->devfn));
926 }
927
928 static __inline int ahc_get_pci_slot(ahc_dev_softc_t);
929 static __inline int
930 ahc_get_pci_slot(ahc_dev_softc_t pci)
931 {
932         return (PCI_SLOT(pci->devfn));
933 }
934
935 static __inline int ahc_get_pci_bus(ahc_dev_softc_t);
936 static __inline int
937 ahc_get_pci_bus(ahc_dev_softc_t pci)
938 {
939         return (pci->bus->number);
940 }
941 #else
942 static inline int ahc_linux_pci_init(void) {
943         return -ENODEV;
944 }
945 static inline void ahc_linux_pci_exit(void) {
946 }
947 #endif
948
949 static __inline void ahc_flush_device_writes(struct ahc_softc *);
950 static __inline void
951 ahc_flush_device_writes(struct ahc_softc *ahc)
952 {
953         /* XXX Is this sufficient for all architectures??? */
954         ahc_inb(ahc, INTSTAT);
955 }
956
957 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,3,0)
958 #define pci_map_sg(pdev, sg_list, nseg, direction) (nseg)
959 #define pci_unmap_sg(pdev, sg_list, nseg, direction)
960 #define sg_dma_address(sg) (VIRT_TO_BUS((sg)->address))
961 #define sg_dma_len(sg) ((sg)->length)
962 #define pci_map_single(pdev, buffer, bufflen, direction) \
963         (VIRT_TO_BUS(buffer))
964 #define pci_unmap_single(pdev, buffer, buflen, direction)
965 #endif
966
967 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,3)
968 #define ahc_pci_set_dma_mask pci_set_dma_mask
969 #else
970 /*
971  * Always "return" 0 for success.
972  */
973 #define ahc_pci_set_dma_mask(dev_softc, mask)                   \
974         (((dev_softc)->dma_mask = mask) && 0)
975 #endif
976 /**************************** Proc FS Support *********************************/
977 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
978 int     ahc_linux_proc_info(char *, char **, off_t, int, int, int);
979 #else
980 int     ahc_linux_proc_info(struct Scsi_Host *, char *, char **,
981                             off_t, int, int);
982 #endif
983
984 /*************************** Domain Validation ********************************/
985 #define AHC_DV_CMD(cmd) ((cmd)->scsi_done == ahc_linux_dv_complete)
986 #define AHC_DV_SIMQ_FROZEN(ahc)                                 \
987         ((((ahc)->platform_data->flags & AHC_DV_ACTIVE) != 0)   \
988          && (ahc)->platform_data->qfrozen == 1)
989
990 /*********************** Transaction Access Wrappers *************************/
991 static __inline void ahc_cmd_set_transaction_status(Scsi_Cmnd *, uint32_t);
992 static __inline void ahc_set_transaction_status(struct scb *, uint32_t);
993 static __inline void ahc_cmd_set_scsi_status(Scsi_Cmnd *, uint32_t);
994 static __inline void ahc_set_scsi_status(struct scb *, uint32_t);
995 static __inline uint32_t ahc_cmd_get_transaction_status(Scsi_Cmnd *cmd);
996 static __inline uint32_t ahc_get_transaction_status(struct scb *);
997 static __inline uint32_t ahc_cmd_get_scsi_status(Scsi_Cmnd *cmd);
998 static __inline uint32_t ahc_get_scsi_status(struct scb *);
999 static __inline void ahc_set_transaction_tag(struct scb *, int, u_int);
1000 static __inline u_long ahc_get_transfer_length(struct scb *);
1001 static __inline int ahc_get_transfer_dir(struct scb *);
1002 static __inline void ahc_set_residual(struct scb *, u_long);
1003 static __inline void ahc_set_sense_residual(struct scb *scb, u_long resid);
1004 static __inline u_long ahc_get_residual(struct scb *);
1005 static __inline u_long ahc_get_sense_residual(struct scb *);
1006 static __inline int ahc_perform_autosense(struct scb *);
1007 static __inline uint32_t ahc_get_sense_bufsize(struct ahc_softc *,
1008                                                struct scb *);
1009 static __inline void ahc_notify_xfer_settings_change(struct ahc_softc *,
1010                                                      struct ahc_devinfo *);
1011 static __inline void ahc_platform_scb_free(struct ahc_softc *ahc,
1012                                            struct scb *scb);
1013 static __inline void ahc_freeze_scb(struct scb *scb);
1014
1015 static __inline
1016 void ahc_cmd_set_transaction_status(Scsi_Cmnd *cmd, uint32_t status)
1017 {
1018         cmd->result &= ~(CAM_STATUS_MASK << 16);
1019         cmd->result |= status << 16;
1020 }
1021
1022 static __inline
1023 void ahc_set_transaction_status(struct scb *scb, uint32_t status)
1024 {
1025         ahc_cmd_set_transaction_status(scb->io_ctx,status);
1026 }
1027
1028 static __inline
1029 void ahc_cmd_set_scsi_status(Scsi_Cmnd *cmd, uint32_t status)
1030 {
1031         cmd->result &= ~0xFFFF;
1032         cmd->result |= status;
1033 }
1034
1035 static __inline
1036 void ahc_set_scsi_status(struct scb *scb, uint32_t status)
1037 {
1038         ahc_cmd_set_scsi_status(scb->io_ctx, status);
1039 }
1040
1041 static __inline
1042 uint32_t ahc_cmd_get_transaction_status(Scsi_Cmnd *cmd)
1043 {
1044         return ((cmd->result >> 16) & CAM_STATUS_MASK);
1045 }
1046
1047 static __inline
1048 uint32_t ahc_get_transaction_status(struct scb *scb)
1049 {
1050         return (ahc_cmd_get_transaction_status(scb->io_ctx));
1051 }
1052
1053 static __inline
1054 uint32_t ahc_cmd_get_scsi_status(Scsi_Cmnd *cmd)
1055 {
1056         return (cmd->result & 0xFFFF);
1057 }
1058
1059 static __inline
1060 uint32_t ahc_get_scsi_status(struct scb *scb)
1061 {
1062         return (ahc_cmd_get_scsi_status(scb->io_ctx));
1063 }
1064
1065 static __inline
1066 void ahc_set_transaction_tag(struct scb *scb, int enabled, u_int type)
1067 {
1068         /*
1069          * Nothing to do for linux as the incoming transaction
1070          * has no concept of tag/non tagged, etc.
1071          */
1072 }
1073
1074 static __inline
1075 u_long ahc_get_transfer_length(struct scb *scb)
1076 {
1077         return (scb->platform_data->xfer_len);
1078 }
1079
1080 static __inline
1081 int ahc_get_transfer_dir(struct scb *scb)
1082 {
1083 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,40)
1084         return (scb->io_ctx->sc_data_direction);
1085 #else
1086         if (scb->io_ctx->bufflen == 0)
1087                 return (CAM_DIR_NONE);
1088
1089         switch(scb->io_ctx->cmnd[0]) {
1090         case 0x08:  /* READ(6)  */
1091         case 0x28:  /* READ(10) */
1092         case 0xA8:  /* READ(12) */
1093                 return (CAM_DIR_IN);
1094         case 0x0A:  /* WRITE(6)  */
1095         case 0x2A:  /* WRITE(10) */
1096         case 0xAA:  /* WRITE(12) */
1097                 return (CAM_DIR_OUT);
1098         default:
1099                 return (CAM_DIR_NONE);
1100         }
1101 #endif
1102 }
1103
1104 static __inline
1105 void ahc_set_residual(struct scb *scb, u_long resid)
1106 {
1107 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
1108         scb->io_ctx->resid = resid;
1109 #else
1110         scb->platform_data->resid = resid;
1111 #endif
1112 }
1113
1114 static __inline
1115 void ahc_set_sense_residual(struct scb *scb, u_long resid)
1116 {
1117         scb->platform_data->sense_resid = resid;
1118 }
1119
1120 static __inline
1121 u_long ahc_get_residual(struct scb *scb)
1122 {
1123 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
1124         return (scb->io_ctx->resid);
1125 #else
1126         return (scb->platform_data->resid);
1127 #endif
1128 }
1129
1130 static __inline
1131 u_long ahc_get_sense_residual(struct scb *scb)
1132 {
1133         return (scb->platform_data->sense_resid);
1134 }
1135
1136 static __inline
1137 int ahc_perform_autosense(struct scb *scb)
1138 {
1139         /*
1140          * We always perform autosense in Linux.
1141          * On other platforms this is set on a
1142          * per-transaction basis.
1143          */
1144         return (1);
1145 }
1146
1147 static __inline uint32_t
1148 ahc_get_sense_bufsize(struct ahc_softc *ahc, struct scb *scb)
1149 {
1150         return (sizeof(struct scsi_sense_data));
1151 }
1152
1153 static __inline void
1154 ahc_notify_xfer_settings_change(struct ahc_softc *ahc,
1155                                 struct ahc_devinfo *devinfo)
1156 {
1157         /* Nothing to do here for linux */
1158 }
1159
1160 static __inline void
1161 ahc_platform_scb_free(struct ahc_softc *ahc, struct scb *scb)
1162 {
1163         ahc->flags &= ~AHC_RESOURCE_SHORTAGE;
1164 }
1165
1166 int     ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg);
1167 void    ahc_platform_free(struct ahc_softc *ahc);
1168 void    ahc_platform_freeze_devq(struct ahc_softc *ahc, struct scb *scb);
1169
1170 static __inline void
1171 ahc_freeze_scb(struct scb *scb)
1172 {
1173         if ((scb->io_ctx->result & (CAM_DEV_QFRZN << 16)) == 0) {
1174                 scb->io_ctx->result |= CAM_DEV_QFRZN << 16;
1175                 scb->platform_data->dev->qfrozen++;
1176         }
1177 }
1178
1179 void    ahc_platform_set_tags(struct ahc_softc *ahc,
1180                               struct ahc_devinfo *devinfo, ahc_queue_alg);
1181 int     ahc_platform_abort_scbs(struct ahc_softc *ahc, int target,
1182                                 char channel, int lun, u_int tag,
1183                                 role_t role, uint32_t status);
1184 irqreturn_t
1185         ahc_linux_isr(int irq, void *dev_id, struct pt_regs * regs);
1186 void    ahc_platform_flushwork(struct ahc_softc *ahc);
1187 int     ahc_softc_comp(struct ahc_softc *, struct ahc_softc *);
1188 void    ahc_done(struct ahc_softc*, struct scb*);
1189 void    ahc_send_async(struct ahc_softc *, char channel,
1190                        u_int target, u_int lun, ac_code, void *);
1191 void    ahc_print_path(struct ahc_softc *, struct scb *);
1192 void    ahc_platform_dump_card_state(struct ahc_softc *ahc);
1193
1194 #ifdef CONFIG_PCI
1195 #define AHC_PCI_CONFIG 1
1196 #else
1197 #define AHC_PCI_CONFIG 0
1198 #endif
1199 #define bootverbose aic7xxx_verbose
1200 extern u_int aic7xxx_verbose;
1201 #endif /* _AIC7XXX_LINUX_H_ */