2 * FreeBSD platform specific driver option settings, data structures,
3 * function declarations and includes.
5 * Copyright (c) 1994-2001 Justin T. Gibbs.
6 * Copyright (c) 2001-2002 Adaptec Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * Alternatively, this software may be distributed under the terms of the
19 * GNU Public License ("GPL").
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
25 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $Id: //depot/aic7xxx/freebsd/dev/aic7xxx/aic79xx_osm.h#14 $
38 #ifndef _AIC79XX_FREEBSD_H_
39 #define _AIC79XX_FREEBSD_H_
41 #include <opt_aic79xx.h> /* for config options */
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/bus.h> /* For device_t */
46 #if __FreeBSD_version >= 500000
47 #include <sys/endian.h>
49 #include <sys/eventhandler.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/queue.h>
54 #define AHD_PCI_CONFIG 1
55 #include <machine/bus_memio.h>
56 #include <machine/bus_pio.h>
57 #include <machine/bus.h>
58 #include <machine/endian.h>
59 #include <machine/clock.h>
60 #include <machine/resource.h>
64 #include <pci/pcireg.h>
65 #include <pci/pcivar.h>
68 #include <cam/cam_ccb.h>
69 #include <cam/cam_debug.h>
70 #include <cam/cam_sim.h>
71 #include <cam/cam_xpt_sim.h>
73 #include <cam/scsi/scsi_all.h>
74 #include <cam/scsi/scsi_message.h>
75 #include <cam/scsi/scsi_iu.h>
77 #ifdef CAM_NEW_TRAN_CODE
78 #define AHD_NEW_TRAN_SETTINGS
79 #endif /* CAM_NEW_TRAN_CODE */
81 /****************************** Platform Macros *******************************/
82 #define SIM_IS_SCSIBUS_B(ahd, sim) \
84 #define SIM_CHANNEL(ahd, sim) \
86 #define SIM_SCSI_ID(ahd, sim) \
88 #define SIM_PATH(ahd, sim) \
89 (ahd->platform_data->path)
90 #define BUILD_SCSIID(ahd, sim, target_id, our_id) \
91 ((((target_id) << TID_SHIFT) & TID) | (our_id))
94 #define SCB_GET_SIM(ahd, scb) \
95 ((ahd)->platform_data->sim)
98 #define offsetof(type, member) ((size_t)(&((type *)0)->member))
100 /************************* Forward Declarations *******************************/
101 typedef device_t ahd_dev_softc_t;
102 typedef union ccb *ahd_io_ctx_t;
104 /***************************** Bus Space/DMA **********************************/
105 #define ahd_dma_tag_create(ahd, parent_tag, alignment, boundary, \
106 lowaddr, highaddr, filter, filterarg, \
107 maxsize, nsegments, maxsegsz, flags, \
109 bus_dma_tag_create(parent_tag, alignment, boundary, \
110 lowaddr, highaddr, filter, filterarg, \
111 maxsize, nsegments, maxsegsz, flags, \
114 #define ahd_dma_tag_destroy(ahd, tag) \
115 bus_dma_tag_destroy(tag)
117 #define ahd_dmamem_alloc(ahd, dmat, vaddr, flags, mapp) \
118 bus_dmamem_alloc(dmat, vaddr, flags, mapp)
120 #define ahd_dmamem_free(ahd, dmat, vaddr, map) \
121 bus_dmamem_free(dmat, vaddr, map)
123 #define ahd_dmamap_create(ahd, tag, flags, mapp) \
124 bus_dmamap_create(tag, flags, mapp)
126 #define ahd_dmamap_destroy(ahd, tag, map) \
127 bus_dmamap_destroy(tag, map)
129 #define ahd_dmamap_load(ahd, dmat, map, addr, buflen, callback, \
130 callback_arg, flags) \
131 bus_dmamap_load(dmat, map, addr, buflen, callback, callback_arg, flags)
133 #define ahd_dmamap_unload(ahd, tag, map) \
134 bus_dmamap_unload(tag, map)
136 /* XXX Need to update Bus DMA for partial map syncs */
137 #define ahd_dmamap_sync(ahd, dma_tag, dmamap, offset, len, op) \
138 bus_dmamap_sync(dma_tag, dmamap, op)
140 /************************ Tunable Driver Parameters **************************/
142 * The number of dma segments supported. The sequencer can handle any number
143 * of physically contiguous S/G entrys. To reduce the driver's memory
144 * consumption, we limit the number supported to be sufficient to handle
145 * the largest mapping supported by the kernel, MAXPHYS. Assuming the
146 * transfer is as fragmented as possible and unaligned, this turns out to
147 * be the number of paged sized transfers in MAXPHYS plus an extra element
148 * to handle any unaligned residual. The sequencer fetches SG elements
149 * in cacheline sized chucks, so make the number per-transaction an even
150 * multiple of 16 which should align us on even the largest of cacheline
153 #define AHD_NSEG (roundup(btoc(MAXPHYS) + 1, 16))
155 /* This driver supports target mode */
157 #define AHD_TARGET_MODE 1
160 /************************** Softc/SCB Platform Data ***************************/
161 struct ahd_platform_data {
163 * Hooks into the XPT.
166 struct cam_sim *sim_b;
167 struct cam_path *path;
168 struct cam_path *path_b;
170 int regs_res_type[2];
173 struct resource *regs[2];
174 struct resource *irq;
179 struct scb_platform_data {
182 /********************************* Byte Order *********************************/
183 #if __FreeBSD_version >= 500000
184 #define ahd_htobe16(x) htobe16(x)
185 #define ahd_htobe32(x) htobe32(x)
186 #define ahd_htobe64(x) htobe64(x)
187 #define ahd_htole16(x) htole16(x)
188 #define ahd_htole32(x) htole32(x)
189 #define ahd_htole64(x) htole64(x)
191 #define ahd_be16toh(x) be16toh(x)
192 #define ahd_be32toh(x) be32toh(x)
193 #define ahd_be64toh(x) be64toh(x)
194 #define ahd_le16toh(x) le16toh(x)
195 #define ahd_le32toh(x) le32toh(x)
196 #define ahd_le64toh(x) le64toh(x)
198 #define ahd_htobe16(x) (x)
199 #define ahd_htobe32(x) (x)
200 #define ahd_htobe64(x) (x)
201 #define ahd_htole16(x) (x)
202 #define ahd_htole32(x) (x)
203 #define ahd_htole64(x) (x)
205 #define ahd_be16toh(x) (x)
206 #define ahd_be32toh(x) (x)
207 #define ahd_be64toh(x) (x)
208 #define ahd_le16toh(x) (x)
209 #define ahd_le32toh(x) (x)
210 #define ahd_le64toh(x) (x)
213 /************************** Timer DataStructures ******************************/
214 typedef struct callout ahd_timer_t;
216 /***************************** Core Includes **********************************/
217 #if AHD_REG_PRETTY_PRINT
218 #define AIC_DEBUG_REGISTERS 1
220 #define AIC_DEBUG_REGISTERS 0
222 #include <dev/aic7xxx/aic79xx.h>
224 /***************************** Timer Facilities *******************************/
225 #if __FreeBSD_version >= 500000
226 #define ahd_timer_init(timer) callout_init(timer, /*mpsafe*/0)
228 #define ahd_timer_init callout_init
230 #define ahd_timer_stop callout_stop
233 ahd_timer_reset(ahd_timer_t *timer, int usec, ahd_callback_t *func, void *arg)
235 callout_reset(timer, (usec * hz)/1000000, func, arg);
238 /*************************** Device Access ************************************/
239 #define ahd_inb(ahd, port) \
240 bus_space_read_1((ahd)->tags[(port) >> 8], \
241 (ahd)->bshs[(port) >> 8], (port) & 0xFF)
243 #define ahd_outb(ahd, port, value) \
244 bus_space_write_1((ahd)->tags[(port) >> 8], \
245 (ahd)->bshs[(port) >> 8], (port) & 0xFF, value)
247 #define ahd_inw_atomic(ahd, port) \
248 ahd_le16toh(bus_space_read_2((ahd)->tags[(port) >> 8], \
249 (ahd)->bshs[(port) >> 8], (port) & 0xFF))
251 #define ahd_outw_atomic(ahd, port, value) \
252 bus_space_write_2((ahd)->tags[(port) >> 8], \
253 (ahd)->bshs[(port) >> 8], \
254 (port & 0xFF), ahd_htole16(value))
256 #define ahd_outsb(ahd, port, valp, count) \
257 bus_space_write_multi_1((ahd)->tags[(port) >> 8], \
258 (ahd)->bshs[(port) >> 8], \
259 (port & 0xFF), valp, count)
261 #define ahd_insb(ahd, port, valp, count) \
262 bus_space_read_multi_1((ahd)->tags[(port) >> 8], \
263 (ahd)->bshs[(port) >> 8], \
264 (port & 0xFF), valp, count)
266 static __inline void ahd_flush_device_writes(struct ahd_softc *);
269 ahd_flush_device_writes(struct ahd_softc *ahd)
271 /* XXX Is this sufficient for all architectures??? */
272 ahd_inb(ahd, INTSTAT);
275 /**************************** Locking Primitives ******************************/
276 /* Lock protecting internal data structures */
277 static __inline void ahd_lockinit(struct ahd_softc *);
278 static __inline void ahd_lock(struct ahd_softc *, unsigned long *flags);
279 static __inline void ahd_unlock(struct ahd_softc *, unsigned long *flags);
281 /* Lock held during command compeletion to the upper layer */
282 static __inline void ahd_done_lockinit(struct ahd_softc *);
283 static __inline void ahd_done_lock(struct ahd_softc *, unsigned long *flags);
284 static __inline void ahd_done_unlock(struct ahd_softc *, unsigned long *flags);
286 /* Lock held during ahd_list manipulation and ahd softc frees */
287 static __inline void ahd_list_lockinit(void);
288 static __inline void ahd_list_lock(unsigned long *flags);
289 static __inline void ahd_list_unlock(unsigned long *flags);
292 ahd_lockinit(struct ahd_softc *ahd)
297 ahd_lock(struct ahd_softc *ahd, unsigned long *flags)
303 ahd_unlock(struct ahd_softc *ahd, unsigned long *flags)
308 /* Lock held during command compeletion to the upper layer */
310 ahd_done_lockinit(struct ahd_softc *ahd)
315 ahd_done_lock(struct ahd_softc *ahd, unsigned long *flags)
320 ahd_done_unlock(struct ahd_softc *ahd, unsigned long *flags)
324 /* Lock held during ahd_list manipulation and ahd softc frees */
331 ahd_list_lock(unsigned long *flags)
336 ahd_list_unlock(unsigned long *flags)
339 /****************************** OS Primitives *********************************/
340 #define ahd_delay DELAY
342 /************************** Transaction Operations ****************************/
343 static __inline void ahd_set_transaction_status(struct scb *, uint32_t);
344 static __inline void ahd_set_scsi_status(struct scb *, uint32_t);
345 static __inline uint32_t ahd_get_transaction_status(struct scb *);
346 static __inline uint32_t ahd_get_scsi_status(struct scb *);
347 static __inline void ahd_set_transaction_tag(struct scb *, int, u_int);
348 static __inline u_long ahd_get_transfer_length(struct scb *);
349 static __inline int ahd_get_transfer_dir(struct scb *);
350 static __inline void ahd_set_residual(struct scb *, u_long);
351 static __inline void ahd_set_sense_residual(struct scb *, u_long);
352 static __inline u_long ahd_get_residual(struct scb *);
353 static __inline int ahd_perform_autosense(struct scb *);
354 static __inline uint32_t ahd_get_sense_bufsize(struct ahd_softc*, struct scb*);
355 static __inline void ahd_freeze_simq(struct ahd_softc *);
356 static __inline void ahd_release_simq(struct ahd_softc *);
357 static __inline void ahd_freeze_ccb(union ccb *ccb);
358 static __inline void ahd_freeze_scb(struct scb *scb);
359 static __inline void ahd_platform_freeze_devq(struct ahd_softc *, struct scb *);
360 static __inline int ahd_platform_abort_scbs(struct ahd_softc *ahd, int target,
361 char channel, int lun, u_int tag,
362 role_t role, uint32_t status);
365 void ahd_set_transaction_status(struct scb *scb, uint32_t status)
367 scb->io_ctx->ccb_h.status &= ~CAM_STATUS_MASK;
368 scb->io_ctx->ccb_h.status |= status;
372 void ahd_set_scsi_status(struct scb *scb, uint32_t status)
374 scb->io_ctx->csio.scsi_status = status;
378 uint32_t ahd_get_transaction_status(struct scb *scb)
380 return (scb->io_ctx->ccb_h.status & CAM_STATUS_MASK);
384 uint32_t ahd_get_scsi_status(struct scb *scb)
386 return (scb->io_ctx->csio.scsi_status);
390 void ahd_set_transaction_tag(struct scb *scb, int enabled, u_int type)
392 scb->io_ctx->csio.tag_action = type;
394 scb->io_ctx->ccb_h.flags |= CAM_TAG_ACTION_VALID;
396 scb->io_ctx->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
400 u_long ahd_get_transfer_length(struct scb *scb)
402 return (scb->io_ctx->csio.dxfer_len);
406 int ahd_get_transfer_dir(struct scb *scb)
408 return (scb->io_ctx->ccb_h.flags & CAM_DIR_MASK);
412 void ahd_set_residual(struct scb *scb, u_long resid)
414 scb->io_ctx->csio.resid = resid;
418 void ahd_set_sense_residual(struct scb *scb, u_long resid)
420 scb->io_ctx->csio.sense_resid = resid;
424 u_long ahd_get_residual(struct scb *scb)
426 return (scb->io_ctx->csio.resid);
430 int ahd_perform_autosense(struct scb *scb)
432 return (!(scb->io_ctx->ccb_h.flags & CAM_DIS_AUTOSENSE));
435 static __inline uint32_t
436 ahd_get_sense_bufsize(struct ahd_softc *ahd, struct scb *scb)
438 return (sizeof(struct scsi_sense_data));
442 ahd_freeze_simq(struct ahd_softc *ahd)
444 xpt_freeze_simq(ahd->platform_data->sim, /*count*/1);
448 ahd_release_simq(struct ahd_softc *ahd)
450 xpt_release_simq(ahd->platform_data->sim, /*run queue*/TRUE);
454 ahd_freeze_ccb(union ccb *ccb)
456 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
457 ccb->ccb_h.status |= CAM_DEV_QFRZN;
458 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
463 ahd_freeze_scb(struct scb *scb)
465 ahd_freeze_ccb(scb->io_ctx);
469 ahd_platform_freeze_devq(struct ahd_softc *ahd, struct scb *scb)
471 /* Nothing to do here for FreeBSD */
475 ahd_platform_abort_scbs(struct ahd_softc *ahd, int target,
476 char channel, int lun, u_int tag,
477 role_t role, uint32_t status)
479 /* Nothing to do here for FreeBSD */
484 ahd_platform_scb_free(struct ahd_softc *ahd, struct scb *scb)
486 /* What do we do to generically handle driver resource shortages??? */
487 if ((ahd->flags & AHD_RESOURCE_SHORTAGE) != 0
488 && scb->io_ctx != NULL
489 && (scb->io_ctx->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
490 scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
491 ahd->flags &= ~AHD_RESOURCE_SHORTAGE;
496 /********************************** PCI ***************************************/
497 #ifdef AHD_PCI_CONFIG
498 static __inline uint32_t ahd_pci_read_config(ahd_dev_softc_t pci,
500 static __inline void ahd_pci_write_config(ahd_dev_softc_t pci,
501 int reg, uint32_t value,
503 static __inline int ahd_get_pci_function(ahd_dev_softc_t);
504 static __inline int ahd_get_pci_slot(ahd_dev_softc_t);
505 static __inline int ahd_get_pci_bus(ahd_dev_softc_t);
507 int ahd_pci_map_registers(struct ahd_softc *ahd);
508 int ahd_pci_map_int(struct ahd_softc *ahd);
510 static __inline uint32_t
511 ahd_pci_read_config(ahd_dev_softc_t pci, int reg, int width)
513 return (pci_read_config(pci, reg, width));
517 ahd_pci_write_config(ahd_dev_softc_t pci, int reg, uint32_t value, int width)
519 pci_write_config(pci, reg, value, width);
523 ahd_get_pci_function(ahd_dev_softc_t pci)
525 return (pci_get_function(pci));
529 ahd_get_pci_slot(ahd_dev_softc_t pci)
531 return (pci_get_slot(pci));
535 ahd_get_pci_bus(ahd_dev_softc_t pci)
537 return (pci_get_bus(pci));
548 void ahd_power_state_change(struct ahd_softc *ahd,
549 ahd_power_state new_state);
551 /******************************** VL/EISA *************************************/
552 int aic7770_map_registers(struct ahd_softc *ahd);
553 int aic7770_map_int(struct ahd_softc *ahd, int irq);
555 /********************************* Debug **************************************/
556 static __inline void ahd_print_path(struct ahd_softc *, struct scb *);
557 static __inline void ahd_platform_dump_card_state(struct ahd_softc *ahd);
560 ahd_print_path(struct ahd_softc *ahd, struct scb *scb)
562 xpt_print_path(scb->io_ctx->ccb_h.path);
566 ahd_platform_dump_card_state(struct ahd_softc *ahd)
568 /* Nothing to do here for FreeBSD */
570 /**************************** Transfer Settings *******************************/
571 void ahd_notify_xfer_settings_change(struct ahd_softc *,
572 struct ahd_devinfo *);
573 void ahd_platform_set_tags(struct ahd_softc *, struct ahd_devinfo *,
576 /************************* Initialization/Teardown ****************************/
577 int ahd_platform_alloc(struct ahd_softc *ahd, void *platform_arg);
578 void ahd_platform_free(struct ahd_softc *ahd);
579 int ahd_map_int(struct ahd_softc *ahd);
580 int ahd_attach(struct ahd_softc *);
581 int ahd_softc_comp(struct ahd_softc *lahd, struct ahd_softc *rahd);
582 int ahd_detach(device_t);
584 /****************************** Interrupts ************************************/
585 void ahd_platform_intr(void *);
586 static __inline void ahd_platform_flushwork(struct ahd_softc *ahd);
588 ahd_platform_flushwork(struct ahd_softc *ahd)
592 /************************ Misc Function Declarations **************************/
593 timeout_t ahd_timeout;
594 void ahd_done(struct ahd_softc *ahd, struct scb *scb);
595 void ahd_send_async(struct ahd_softc *, char /*channel*/,
596 u_int /*target*/, u_int /*lun*/, ac_code, void *arg);
597 #endif /* _AIC79XX_FREEBSD_H_ */