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