Home | History | Annotate | Line # | Download | only in ic
aic79xx_osm.h revision 1.14
      1 /*	$NetBSD: aic79xx_osm.h,v 1.14 2007/10/19 11:59:46 ad Exp $	*/
      2 
      3 /*
      4  * NetBSD platform specific driver option settings, data structures,
      5  * function declarations and includes.
      6  *
      7  * Copyright (c) 1994-2001 Justin T. Gibbs.
      8  * Copyright (c) 2001-2002 Adaptec Inc.
      9  * All rights reserved.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions, and the following disclaimer,
     16  *    without modification.
     17  * 2. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * Alternatively, this software may be distributed under the terms of the
     21  * GNU Public License ("GPL").
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
     27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  * $NetBSD: aic79xx_osm.h,v 1.14 2007/10/19 11:59:46 ad Exp $
     36  *
     37  * //depot/aic7xxx/freebsd/dev/aic7xxx/aic79xx_osm.h#19 $$NetBSD: aic79xx_osm.h,v 1.14 2007/10/19 11:59:46 ad Exp $
     38  *
     39  * $FreeBSD: src/sys/dev/aic7xxx/aic79xx_osm.h,v 1.9 2003/05/26 21:43:29 gibbs Exp $
     40  */
     41 /*
     42  * Ported from FreeBSD by Pascal Renauld, Network Storage Solutions, Inc.
     43  * - April 2003
     44  */
     45 
     46 #ifndef _AIC79XX_NETBSD_H_
     47 #define _AIC79XX_NETBSD_H_
     48 
     49 #include "opt_ahd.h"	/* for config options */
     50 
     51 #include <sys/param.h>
     52 #include <sys/kernel.h>
     53 #include <sys/systm.h>
     54 #include <sys/device.h>
     55 #include <sys/malloc.h>
     56 #include <sys/buf.h>
     57 #include <sys/proc.h>
     58 #include <sys/scsiio.h>
     59 #include <sys/reboot.h>
     60 #include <sys/kthread.h>
     61 
     62 #include <dev/pci/pcireg.h>
     63 #include <dev/pci/pcivar.h>
     64 
     65 #include <sys/bus.h>
     66 #include <sys/intr.h>
     67 
     68 #include <dev/scsipi/scsi_all.h>
     69 #include <dev/scsipi/scsipi_all.h>
     70 #include <dev/scsipi/scsi_message.h>
     71 #include <dev/scsipi/scsipi_debug.h>
     72 #include <dev/scsipi/scsiconf.h>
     73 #include <dev/scsipi/scsi_iu.h>
     74 
     75 #include <uvm/uvm_extern.h>
     76 
     77 
     78 /****************************** Platform Macros *******************************/
     79 #define	SIM_IS_SCSIBUS_B(ahd, sim)	\
     80 	(0)
     81 #define	SIM_CHANNEL(ahd, sim)	\
     82 	('A')
     83 #define	SIM_SCSI_ID(ahd, sim)	\
     84 	(ahd->our_id)
     85 #define	SIM_PATH(ahd, sim)	\
     86 	(ahd->platform_data->path)
     87 #define BUILD_SCSIID(ahd, sim, target_id, our_id) \
     88         ((((target_id) << TID_SHIFT) & TID) | (our_id))
     89 
     90 
     91 #define SCB_GET_SIM(ahd, scb) \
     92 	((ahd)->platform_data->sim)
     93 
     94 #ifndef offsetof
     95 #define offsetof(type, member)  ((size_t)(&((type *)0)->member))
     96 #endif
     97 
     98 /************************* Forward Declarations *******************************/
     99 typedef pcireg_t ahd_dev_softc_t;
    100 /***************************** Bus Space/DMA **********************************/
    101 #define ahd_dma_tag_create(ahd, parent_tag, alignment, boundary,	\
    102 			   lowaddr, highaddr, filter, filterarg,	\
    103 			   maxsize, nsegments, maxsegsz, flags,		\
    104 			   dma_tagp)					\
    105 	bus_dma_tag_create(parent_tag, alignment, boundary,		\
    106 			   lowaddr, highaddr, filter, filterarg,	\
    107 			   maxsize, nsegments, maxsegsz, flags,		\
    108 			   dma_tagp)
    109 
    110 #define ahd_dma_tag_destroy(ahd, tag)					\
    111 	bus_dma_tag_destroy(tag)
    112 
    113 #define ahd_dmamem_alloc(ahd, dmat, vaddr, flags, mapp)			\
    114 	bus_dmamem_alloc(dmat, vaddr, flags, mapp)
    115 
    116 #define ahd_dmamem_free(ahd, dmat, vaddr, map)				\
    117 	bus_dmamem_free(dmat, vaddr, map)
    118 
    119 #define ahd_dmamap_create(ahd, tag, flags, mapp)			\
    120 	bus_dmamap_create(tag, flags, mapp)
    121 
    122 #define ahd_dmamap_destroy(ahd, tag, map)				\
    123 	bus_dmamap_destroy(tag, map)
    124 
    125 #define ahd_dmamap_load(ahd, dmat, map, addr, buflen, callback,		\
    126 			callback_arg, flags)				\
    127 	bus_dmamap_load(dmat, map, addr, buflen, callback, callback_arg, flags)
    128 
    129 #define ahd_dmamap_unload(ahd, tag, map)				\
    130 	bus_dmamap_unload(tag, map)
    131 
    132 /* XXX Need to update Bus DMA for partial map syncs */
    133 #define ahd_dmamap_sync(ahd, dma_tag, dmamap, offset, len, op)		\
    134 	bus_dmamap_sync(dma_tag, dmamap, offset, len, op)
    135 
    136 /************************ Tunable Driver Parameters  **************************/
    137 /*
    138  * The number of DMA segments supported.  The sequencer can handle any number
    139  * of physically contiguous S/G entrys.  To reduce the driver's memory
    140  * consumption, we limit the number supported to be sufficient to handle
    141  * the largest mapping supported by the kernel, MAXPHYS.  Assuming the
    142  * transfer is as fragmented as possible and unaligned, this turns out to
    143  * be the number of paged sized transfers in MAXPHYS plus an extra element
    144  * to handle any unaligned residual.  The sequencer fetches SG elements
    145  * in cacheline sized chucks, so make the number per-transaction an even
    146  * multiple of 16 which should align us on even the largest of cacheline
    147  * boundaries.
    148  */
    149 #define AHD_NSEG (roundup(btoc(MAXPHYS) + 1, 16))
    150 
    151 /* This driver supports target mode */
    152 #if NOT_YET
    153 #define AHD_TARGET_MODE 1
    154 #endif
    155 
    156 /************************** Softc/SCB Platform Data ***************************/
    157 struct ahd_platform_data {
    158 };
    159 
    160 struct scb_platform_data {
    161 };
    162 
    163 /********************************* Byte Order *********************************/
    164 #define ahd_htobe16(x) htobe16(x)
    165 #define ahd_htobe32(x) htobe32(x)
    166 #define ahd_htobe64(x) htobe64(x)
    167 #define ahd_htole16(x) htole16(x)
    168 #define ahd_htole32(x) htole32(x)
    169 #define ahd_htole64(x) htole64(x)
    170 
    171 #define ahd_be16toh(x) be16toh(x)
    172 #define ahd_be32toh(x) be32toh(x)
    173 #define ahd_be64toh(x) be64toh(x)
    174 #define ahd_le16toh(x) le16toh(x)
    175 #define ahd_le32toh(x) le32toh(x)
    176 #define ahd_le64toh(x) le64toh(x)
    177 
    178 /************************** Timer DataStructures ******************************/
    179 typedef struct callout ahd_timer_t;
    180 
    181 /***************************** Core Includes **********************************/
    182 #if AHD_REG_PRETTY_PRINT
    183 #define AIC_DEBUG_REGISTERS 1
    184 #else
    185 #define AIC_DEBUG_REGISTERS 0
    186 #endif
    187 #include <dev/ic/aic79xxvar.h>
    188 
    189 /***************************** Timer Facilities *******************************/
    190 void ahd_timeout(void*);
    191 #define ahd_timer_init(timer) callout_init(timer, 0)
    192 #define ahd_timer_stop callout_stop
    193 
    194 static __inline void
    195 ahd_timer_reset(ahd_timer_t *timer, u_int usec, ahd_callback_t *func, void *arg)
    196 {
    197 	callout_reset(timer, (usec * hz)/1000000, func, arg);
    198 }
    199 
    200 static __inline void
    201 ahd_scb_timer_reset(struct scb *scb, u_int usec)
    202 {
    203 	if (!(scb->xs->xs_control & XS_CTL_POLL)) {
    204 		callout_reset(&scb->xs->xs_callout,
    205 			      (usec * hz)/1000000, ahd_timeout, scb);
    206 	}
    207 }
    208 
    209 /*************************** Device Access ************************************/
    210 #define ahd_inb(ahd, port)					\
    211 	bus_space_read_1((ahd)->tags[(port) >> 8],		\
    212 			 (ahd)->bshs[(port) >> 8], (port) & 0xFF)
    213 
    214 #define ahd_outb(ahd, port, value)				\
    215 	bus_space_write_1((ahd)->tags[(port) >> 8],		\
    216 			  (ahd)->bshs[(port) >> 8], (port) & 0xFF, value)
    217 
    218 #define ahd_inw_atomic(ahd, port)				\
    219 	ahd_le16toh(bus_space_read_2((ahd)->tags[(port) >> 8],	\
    220 				     (ahd)->bshs[(port) >> 8], (port) & 0xFF))
    221 
    222 #define ahd_outw_atomic(ahd, port, value)			\
    223 	bus_space_write_2((ahd)->tags[(port) >> 8],		\
    224 			  (ahd)->bshs[(port) >> 8],		\
    225 			  (port & 0xFF), ahd_htole16(value))
    226 
    227 #define ahd_outsb(ahd, port, valp, count)			\
    228 	bus_space_write_multi_1((ahd)->tags[(port) >> 8],	\
    229 				(ahd)->bshs[(port) >> 8],	\
    230 				(port & 0xFF), valp, count)
    231 
    232 #define ahd_insb(ahd, port, valp, count)			\
    233 	bus_space_read_multi_1((ahd)->tags[(port) >> 8],	\
    234 			       (ahd)->bshs[(port) >> 8],	\
    235 			       (port & 0xFF), valp, count)
    236 
    237 static __inline void ahd_flush_device_writes(struct ahd_softc *);
    238 
    239 static __inline void
    240 ahd_flush_device_writes(struct ahd_softc *ahd)
    241 {
    242 	/* XXX Is this sufficient for all architectures??? */
    243 	ahd_inb(ahd, INTSTAT);
    244 }
    245 
    246 /**************************** Locking Primitives ******************************/
    247 /* Lock protecting internal data structures */
    248 static __inline void ahd_lockinit(struct ahd_softc *);
    249 static __inline void ahd_lock(struct ahd_softc *, int *);
    250 static __inline void ahd_unlock(struct ahd_softc *, int *);
    251 
    252 /* Lock held during command completion to the upper layer */
    253 static __inline void ahd_done_lockinit(struct ahd_softc *);
    254 static __inline void ahd_done_lock(struct ahd_softc *, unsigned long *);
    255 static __inline void ahd_done_unlock(struct ahd_softc *, unsigned long *);
    256 
    257 /* Lock held during ahd_list manipulation and ahd softc frees */
    258 static __inline void ahd_list_lockinit(void);
    259 static __inline void ahd_list_lock(unsigned long *);
    260 static __inline void ahd_list_unlock(unsigned long *);
    261 
    262 static __inline void
    263 ahd_lockinit(struct ahd_softc *ahd)
    264 {
    265 }
    266 
    267 static __inline void
    268 ahd_lock(struct ahd_softc *ahd, int *flags)
    269 {
    270 	*flags = splbio();
    271 }
    272 
    273 static __inline void
    274 ahd_unlock(struct ahd_softc *ahd, int *flags)
    275 {
    276 	splx(*flags);
    277 }
    278 
    279 /* Lock held during command completion to the upper layer */
    280 static __inline void
    281 ahd_done_lockinit(struct ahd_softc *ahd)
    282 {
    283 }
    284 
    285 static __inline void
    286 ahd_done_lock(struct ahd_softc *ahd, unsigned long *flags)
    287 {
    288 }
    289 
    290 static __inline void
    291 ahd_done_unlock(struct ahd_softc *ahd, unsigned long *flags)
    292 {
    293 }
    294 
    295 /* Lock held during ahd_list manipulation and ahd softc frees */
    296 static __inline void
    297 ahd_list_lockinit(void)
    298 {
    299 }
    300 
    301 static __inline void
    302 ahd_list_lock(unsigned long *flags)
    303 {
    304 }
    305 
    306 static __inline void
    307 ahd_list_unlock(unsigned long *flags)
    308 {
    309 }
    310 /****************************** OS Primitives *********************************/
    311 #define ahd_delay DELAY
    312 
    313 /************************** Transaction Operations ****************************/
    314 static __inline void ahd_set_transaction_status(struct scb *, uint32_t);
    315 static __inline void ahd_set_scsi_status(struct scb *, uint32_t);
    316 static __inline void ahd_set_xfer_status(struct scb *, uint32_t);
    317 static __inline uint32_t ahd_get_transaction_status(struct scb *);
    318 static __inline uint32_t ahd_get_scsi_status(struct scb *);
    319 static __inline void ahd_set_transaction_tag(struct scb *, int, u_int);
    320 static __inline u_long ahd_get_transfer_length(struct scb *);
    321 static __inline int ahd_get_transfer_dir(struct scb *);
    322 static __inline void ahd_set_residual(struct scb *, u_long);
    323 static __inline void ahd_set_sense_residual(struct scb *, u_long);
    324 static __inline u_long ahd_get_residual(struct scb *);
    325 static __inline int ahd_perform_autosense(struct scb *);
    326 static __inline uint32_t ahd_get_sense_bufsize(struct ahd_softc*, struct scb*);
    327 static __inline void ahd_freeze_simq(struct ahd_softc *);
    328 static __inline void ahd_release_simq(struct ahd_softc *);
    329 static __inline void ahd_freeze_scb(struct scb *);
    330 static __inline void ahd_platform_freeze_devq(struct ahd_softc *, struct scb *);
    331 static __inline int  ahd_platform_abort_scbs(struct ahd_softc *, int,
    332     char, int, u_int, role_t, uint32_t);
    333 
    334 static __inline
    335 void ahd_set_transaction_status(struct scb *scb, uint32_t status)
    336 {
    337 	scb->xs->error = status;
    338 }
    339 
    340 static __inline
    341 void ahd_set_scsi_status(struct scb *scb, uint32_t status)
    342 {
    343 	scb->xs->xs_status = status;
    344 }
    345 
    346 static __inline
    347 void ahd_set_xfer_status(struct scb *scb, uint32_t status)
    348 {
    349 	scb->xs->status = status;
    350 }
    351 
    352 static __inline
    353 uint32_t ahd_get_transaction_status(struct scb *scb)
    354 {
    355 	return (scb->xs->error);
    356 }
    357 
    358 static __inline
    359 uint32_t ahd_get_scsi_status(struct scb *scb)
    360 {
    361 	return (scb->xs->xs_status);
    362 }
    363 
    364 static __inline
    365 void ahd_set_transaction_tag(struct scb *scb, int enabled, u_int type)
    366 {
    367 	scb->xs->xs_tag_type = type;
    368 }
    369 
    370 static __inline
    371 u_long ahd_get_transfer_length(struct scb *scb)
    372 {
    373 	return (scb->xs->datalen);
    374 }
    375 
    376 static __inline
    377 int ahd_get_transfer_dir(struct scb *scb)
    378 {
    379 	return (scb->xs->xs_control & (XS_CTL_DATA_IN|XS_CTL_DATA_OUT));
    380 }
    381 
    382 static __inline
    383 void ahd_set_residual(struct scb *scb, u_long resid)
    384 {
    385 	scb->xs->resid = resid;
    386 }
    387 
    388 
    389 static __inline
    390 void ahd_set_sense_residual(struct scb *scb, u_long resid)
    391 {
    392   //scb->xs->sense.scsi_sense.extra_len = resid; /* ??? */
    393 }
    394 
    395 
    396 static __inline
    397 u_long ahd_get_residual(struct scb *scb)
    398 {
    399 	return (scb->xs->resid);
    400 }
    401 
    402 static __inline
    403 int ahd_perform_autosense(struct scb *scb)
    404 {
    405 	return (!(scb->flags & SCB_SENSE));
    406 }
    407 
    408 static __inline uint32_t
    409 ahd_get_sense_bufsize(struct ahd_softc *ahd, struct scb *scb)
    410 {
    411 	return (sizeof(struct scsi_sense_data));
    412 }
    413 
    414 static __inline void
    415 ahd_freeze_simq(struct ahd_softc *ahd)
    416 {
    417 	/* do nothing for now */
    418 }
    419 
    420 static __inline void
    421 ahd_release_simq(struct ahd_softc *ahd)
    422 {
    423 	/* do nothing for now */
    424 }
    425 
    426 static __inline void
    427 ahd_freeze_scb(struct scb *scb)
    428 {
    429   	struct scsipi_xfer *xs = scb->xs;
    430 
    431 	if (!(scb->flags & SCB_FREEZE_QUEUE)) {
    432 	 	scsipi_periph_freeze(xs->xs_periph, 1);
    433 		scb->flags |= SCB_FREEZE_QUEUE;
    434 	}
    435 }
    436 
    437 static __inline void
    438 ahd_platform_freeze_devq(struct ahd_softc *ahd, struct scb *scb)
    439 {
    440 	/* Nothing to do here for NetBSD */
    441 }
    442 
    443 static __inline int
    444 ahd_platform_abort_scbs(struct ahd_softc *ahd, int target,
    445     char channel, int lun, u_int tag,
    446     role_t role, uint32_t status)
    447 {
    448 	/* Nothing to do here for NetBSD */
    449 	return (0);
    450 }
    451 
    452 static __inline void
    453 ahd_platform_scb_free(struct ahd_softc *ahd, struct scb *scb)
    454 {
    455 #ifdef __FreeBSD__
    456 	/* What do we do to generically handle driver resource shortages??? */
    457 	if ((ahd->flags & AHD_RESOURCE_SHORTAGE) != 0
    458 	 && scb->io_ctx != NULL
    459 	 && (scb->io_ctx->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
    460 		scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
    461 		ahd->flags &= ~AHD_RESOURCE_SHORTAGE;
    462 	}
    463 	scb->io_ctx = NULL;
    464 #endif /* end of modification */
    465 }
    466 
    467 /********************************** PCI ***************************************/
    468 #if AHD_PCI_CONFIG > 0
    469 static __inline uint32_t ahd_pci_read_config(ahd_dev_softc_t, int, int);
    470 static __inline void	 ahd_pci_write_config(ahd_dev_softc_t, int,
    471     uint32_t, int);
    472 static __inline int	 ahd_get_pci_function(ahd_dev_softc_t);
    473 static __inline int	 ahd_get_pci_slot(ahd_dev_softc_t);
    474 static __inline int	 ahd_get_pci_bus(ahd_dev_softc_t);
    475 
    476 int			 ahd_pci_map_registers(struct ahd_softc *);
    477 int			 ahd_pci_map_int(struct ahd_softc *);
    478 
    479 static __inline uint32_t
    480 ahd_pci_read_config(ahd_dev_softc_t pci, int reg, int width)
    481 {
    482 	return (pci_read_config(pci, reg, width));
    483 }
    484 
    485 static __inline void
    486 ahd_pci_write_config(ahd_dev_softc_t pci, int reg, uint32_t value, int width)
    487 {
    488 	pci_write_config(pci, reg, value, width);
    489 }
    490 
    491 static __inline int
    492 ahd_get_pci_function(ahd_dev_softc_t pci)
    493 {
    494 	return (pci_get_function(pci));
    495 }
    496 
    497 static __inline int
    498 ahd_get_pci_slot(ahd_dev_softc_t pci)
    499 {
    500 	return (pci_get_slot(pci));
    501 }
    502 
    503 
    504 static __inline int
    505 ahd_get_pci_bus(ahd_dev_softc_t pci)
    506 {
    507 	return (pci_get_bus(pci));
    508 }
    509 #endif
    510 
    511 typedef enum
    512 {
    513 	AHD_POWER_STATE_D0,
    514 	AHD_POWER_STATE_D1,
    515 	AHD_POWER_STATE_D2,
    516 	AHD_POWER_STATE_D3
    517 } ahd_power_state;
    518 
    519 void ahd_power_state_change(struct ahd_softc *, ahd_power_state);
    520 
    521 /******************************** VL/EISA *************************************/
    522 int aic7770_map_registers(struct ahd_softc *);
    523 int aic7770_map_int(struct ahd_softc *, int);
    524 
    525 /********************************* Debug **************************************/
    526 static __inline void	ahd_print_path(struct ahd_softc *, struct scb *);
    527 static __inline void	ahd_platform_dump_card_state(struct ahd_softc *);
    528 static __inline void
    529 ahd_print_path(struct ahd_softc *ahd, struct scb *scb)
    530 {
    531 	printf("%s:", ahd->sc_dev.dv_xname);
    532 }
    533 
    534 static __inline void
    535 ahd_platform_dump_card_state(struct ahd_softc *ahd)
    536 {
    537 	/* Nothing to do here for NetBSD */
    538 }
    539 /**************************** Transfer Settings *******************************/
    540 void	  ahd_notify_xfer_settings_change(struct ahd_softc *,
    541 					  struct ahd_devinfo *);
    542 void	  ahd_platform_set_tags(struct ahd_softc *, struct ahd_devinfo *,
    543 				ahd_queue_alg);
    544 
    545 /************************* Initialization/Teardown ****************************/
    546 int	  ahd_platform_alloc(struct ahd_softc *, void *);
    547 void	  ahd_platform_free(struct ahd_softc *);
    548 int	  ahd_map_int(struct ahd_softc *);
    549 int	  ahd_attach(struct ahd_softc *);
    550 int	  ahd_softc_comp(struct ahd_softc *, struct ahd_softc *);
    551 int 	  ahd_detach(struct device *, int);
    552 #define	ahd_platform_init(arg)
    553 
    554 
    555 /****************************** Interrupts ************************************/
    556 void			ahd_platform_intr(void *);
    557 static __inline void	ahd_platform_flushwork(struct ahd_softc *);
    558 static __inline void
    559 ahd_platform_flushwork(struct ahd_softc *ahd)
    560 {
    561 	/* Nothing to do here for NetBSD */
    562 }
    563 
    564 /************************ Misc Function Declarations **************************/
    565 void	ahd_done(struct ahd_softc *, struct scb *);
    566 void	ahd_send_async(struct ahd_softc *, char , u_int, u_int, u_int, void *);
    567 
    568 #endif  /* _AIC79XX_NETBSD_H_ */
    569