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#20 $
35 * $FreeBSD: src/sys/dev/aic7xxx/aic79xx_osm.h,v 1.4.2.3 2003/06/10 03:26:07 gibbs Exp $
36 * $DragonFly: src/sys/dev/disk/aic7xxx/aic79xx_osm.h,v 1.8 2007/01/27 15:03:25 swildner Exp $
39 #ifndef _AIC79XX_FREEBSD_H_
40 #define _AIC79XX_FREEBSD_H_
42 #include <opt_aic79xx.h> /* for config options */
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/bus.h> /* For device_t */
47 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
48 #include <sys/endian.h>
50 #include <sys/eventhandler.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/queue.h>
54 #include <sys/thread2.h>
56 #define AHD_PCI_CONFIG 1
57 #include <machine/endian.h>
58 #include <machine/clock.h>
62 #include <bus/pci/pcireg.h>
63 #include <bus/pci/pcivar.h>
65 #include <bus/cam/cam.h>
66 #include <bus/cam/cam_ccb.h>
67 #include <bus/cam/cam_debug.h>
68 #include <bus/cam/cam_sim.h>
69 #include <bus/cam/cam_xpt_sim.h>
71 #include <bus/cam/scsi/scsi_all.h>
72 #include <bus/cam/scsi/scsi_message.h>
73 #include <bus/cam/scsi/scsi_iu.h>
75 #ifdef CAM_NEW_TRAN_CODE
76 #define AHD_NEW_TRAN_SETTINGS
77 #endif /* CAM_NEW_TRAN_CODE */
79 /****************************** Platform Macros *******************************/
80 #define SIM_IS_SCSIBUS_B(ahd, sim) \
82 #define SIM_CHANNEL(ahd, sim) \
84 #define SIM_SCSI_ID(ahd, sim) \
86 #define SIM_PATH(ahd, sim) \
87 (ahd->platform_data->path)
88 #define BUILD_SCSIID(ahd, sim, target_id, our_id) \
89 ((((target_id) << TID_SHIFT) & TID) | (our_id))
92 #define SCB_GET_SIM(ahd, scb) \
93 ((ahd)->platform_data->sim)
96 #define offsetof(type, member) ((size_t)(&((type *)0)->member))
98 /************************* Forward Declarations *******************************/
99 typedef device_t ahd_dev_softc_t;
100 typedef union ccb *ahd_io_ctx_t;
102 /***************************** Bus Space/DMA **********************************/
103 #define ahd_dma_tag_create(ahd, parent_tag, alignment, boundary, \
104 lowaddr, highaddr, filter, filterarg, \
105 maxsize, nsegments, maxsegsz, flags, \
107 bus_dma_tag_create(parent_tag, alignment, boundary, \
108 lowaddr, highaddr, filter, filterarg, \
109 maxsize, nsegments, maxsegsz, flags, \
112 #define ahd_dma_tag_destroy(ahd, tag) \
113 bus_dma_tag_destroy(tag)
115 #define ahd_dmamem_alloc(ahd, dmat, vaddr, flags, mapp) \
116 bus_dmamem_alloc(dmat, vaddr, flags, mapp)
118 #define ahd_dmamem_free(ahd, dmat, vaddr, map) \
119 bus_dmamem_free(dmat, vaddr, map)
121 #define ahd_dmamap_create(ahd, tag, flags, mapp) \
122 bus_dmamap_create(tag, flags, mapp)
124 #define ahd_dmamap_destroy(ahd, tag, map) \
125 bus_dmamap_destroy(tag, map)
127 #define ahd_dmamap_load(ahd, dmat, map, addr, buflen, callback, \
128 callback_arg, flags) \
129 bus_dmamap_load(dmat, map, addr, buflen, callback, callback_arg, flags)
131 #define ahd_dmamap_unload(ahd, tag, map) \
132 bus_dmamap_unload(tag, map)
134 /* XXX Need to update Bus DMA for partial map syncs */
135 #define ahd_dmamap_sync(ahd, dma_tag, dmamap, offset, len, op) \
136 bus_dmamap_sync(dma_tag, dmamap, op)
138 /************************ Tunable Driver Parameters **************************/
140 * The number of dma segments supported. The sequencer can handle any number
141 * of physically contiguous S/G entrys. To reduce the driver's memory
142 * consumption, we limit the number supported to be sufficient to handle
143 * the largest mapping supported by the kernel, MAXPHYS. Assuming the
144 * transfer is as fragmented as possible and unaligned, this turns out to
145 * be the number of paged sized transfers in MAXPHYS plus an extra element
146 * to handle any unaligned residual. The sequencer fetches SG elements
147 * in cacheline sized chucks, so make the number per-transaction an even
148 * multiple of 16 which should align us on even the largest of cacheline
151 #define AHD_NSEG (roundup(btoc(MAXPHYS) + 1, 16))
153 /* This driver supports target mode */
155 #define AHD_TARGET_MODE 1
158 /************************** Softc/SCB Platform Data ***************************/
159 struct ahd_platform_data {
161 * Hooks into the XPT.
164 struct cam_sim *sim_b;
165 struct cam_path *path;
166 struct cam_path *path_b;
168 int regs_res_type[2];
171 struct resource *regs[2];
172 struct resource *irq;
177 struct scb_platform_data {
180 /********************************* Byte Order *********************************/
181 #if defined(__FreeBSD___) && __FreeBSD_version >= 500000
182 #define ahd_htobe16(x) htobe16(x)
183 #define ahd_htobe32(x) htobe32(x)
184 #define ahd_htobe64(x) htobe64(x)
185 #define ahd_htole16(x) htole16(x)
186 #define ahd_htole32(x) htole32(x)
187 #define ahd_htole64(x) htole64(x)
189 #define ahd_be16toh(x) be16toh(x)
190 #define ahd_be32toh(x) be32toh(x)
191 #define ahd_be64toh(x) be64toh(x)
192 #define ahd_le16toh(x) le16toh(x)
193 #define ahd_le32toh(x) le32toh(x)
194 #define ahd_le64toh(x) le64toh(x)
196 #define ahd_htobe16(x) (x)
197 #define ahd_htobe32(x) (x)
198 #define ahd_htobe64(x) (x)
199 #define ahd_htole16(x) (x)
200 #define ahd_htole32(x) (x)
201 #define ahd_htole64(x) (x)
203 #define ahd_be16toh(x) (x)
204 #define ahd_be32toh(x) (x)
205 #define ahd_be64toh(x) (x)
206 #define ahd_le16toh(x) (x)
207 #define ahd_le32toh(x) (x)
208 #define ahd_le64toh(x) (x)
211 /************************** Timer DataStructures ******************************/
212 typedef struct callout ahd_timer_t;
214 /***************************** Core Includes **********************************/
215 #if AHD_REG_PRETTY_PRINT
216 #define AIC_DEBUG_REGISTERS 1
218 #define AIC_DEBUG_REGISTERS 0
222 /***************************** Timer Facilities *******************************/
223 timeout_t ahd_timeout;
224 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
225 #define ahd_timer_init(timer) callout_init(timer, /*mpsafe*/0)
227 #define ahd_timer_init callout_init
229 #define ahd_timer_stop callout_stop
232 ahd_timer_reset(ahd_timer_t *timer, u_int usec, ahd_callback_t *func, void *arg)
234 callout_reset(timer, (usec * hz)/1000000, func, arg);
238 ahd_scb_timer_reset(struct scb *scb, u_int usec)
240 callout_reset(&scb->io_ctx->ccb_h.timeout_ch, (usec * hz)/1000000,
244 /*************************** Device Access ************************************/
245 #define ahd_inb(ahd, port) \
246 bus_space_read_1((ahd)->tags[(port) >> 8], \
247 (ahd)->bshs[(port) >> 8], (port) & 0xFF)
249 #define ahd_outb(ahd, port, value) \
250 bus_space_write_1((ahd)->tags[(port) >> 8], \
251 (ahd)->bshs[(port) >> 8], (port) & 0xFF, value)
253 #define ahd_inw_atomic(ahd, port) \
254 ahd_le16toh(bus_space_read_2((ahd)->tags[(port) >> 8], \
255 (ahd)->bshs[(port) >> 8], (port) & 0xFF))
257 #define ahd_outw_atomic(ahd, port, value) \
258 bus_space_write_2((ahd)->tags[(port) >> 8], \
259 (ahd)->bshs[(port) >> 8], \
260 (port & 0xFF), ahd_htole16(value))
262 #define ahd_outsb(ahd, port, valp, count) \
263 bus_space_write_multi_1((ahd)->tags[(port) >> 8], \
264 (ahd)->bshs[(port) >> 8], \
265 (port & 0xFF), valp, count)
267 #define ahd_insb(ahd, port, valp, count) \
268 bus_space_read_multi_1((ahd)->tags[(port) >> 8], \
269 (ahd)->bshs[(port) >> 8], \
270 (port & 0xFF), valp, count)
272 static __inline void ahd_flush_device_writes(struct ahd_softc *);
275 ahd_flush_device_writes(struct ahd_softc *ahd)
277 /* XXX Is this sufficient for all architectures??? */
278 ahd_inb(ahd, INTSTAT);
281 /**************************** Locking Primitives ******************************/
282 /* Lock protecting internal data structures */
283 static __inline void ahd_lock(void);
284 static __inline void ahd_unlock(void);
298 /****************************** OS Primitives *********************************/
299 #define ahd_delay DELAY
301 /************************** Transaction Operations ****************************/
302 static __inline void ahd_set_transaction_status(struct scb *, uint32_t);
303 static __inline void ahd_set_scsi_status(struct scb *, uint32_t);
304 static __inline uint32_t ahd_get_transaction_status(struct scb *);
305 static __inline uint32_t ahd_get_scsi_status(struct scb *);
306 static __inline void ahd_set_transaction_tag(struct scb *, int, u_int);
307 static __inline u_long ahd_get_transfer_length(struct scb *);
308 static __inline int ahd_get_transfer_dir(struct scb *);
309 static __inline void ahd_set_residual(struct scb *, u_long);
310 static __inline void ahd_set_sense_residual(struct scb *, u_long);
311 static __inline u_long ahd_get_residual(struct scb *);
312 static __inline int ahd_perform_autosense(struct scb *);
313 static __inline uint32_t ahd_get_sense_bufsize(struct ahd_softc*, struct scb*);
314 static __inline void ahd_freeze_simq(struct ahd_softc *);
315 static __inline void ahd_release_simq(struct ahd_softc *);
316 static __inline void ahd_freeze_ccb(union ccb *ccb);
317 static __inline void ahd_freeze_scb(struct scb *scb);
318 static __inline void ahd_platform_freeze_devq(struct ahd_softc *, struct scb *);
319 static __inline int ahd_platform_abort_scbs(struct ahd_softc *ahd, int target,
320 char channel, int lun, u_int tag,
321 role_t role, uint32_t status);
324 void ahd_set_transaction_status(struct scb *scb, uint32_t status)
326 scb->io_ctx->ccb_h.status &= ~CAM_STATUS_MASK;
327 scb->io_ctx->ccb_h.status |= status;
331 void ahd_set_scsi_status(struct scb *scb, uint32_t status)
333 scb->io_ctx->csio.scsi_status = status;
337 uint32_t ahd_get_transaction_status(struct scb *scb)
339 return (scb->io_ctx->ccb_h.status & CAM_STATUS_MASK);
343 uint32_t ahd_get_scsi_status(struct scb *scb)
345 return (scb->io_ctx->csio.scsi_status);
349 void ahd_set_transaction_tag(struct scb *scb, int enabled, u_int type)
351 scb->io_ctx->csio.tag_action = type;
353 scb->io_ctx->ccb_h.flags |= CAM_TAG_ACTION_VALID;
355 scb->io_ctx->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
359 u_long ahd_get_transfer_length(struct scb *scb)
361 return (scb->io_ctx->csio.dxfer_len);
365 int ahd_get_transfer_dir(struct scb *scb)
367 return (scb->io_ctx->ccb_h.flags & CAM_DIR_MASK);
371 void ahd_set_residual(struct scb *scb, u_long resid)
373 scb->io_ctx->csio.resid = resid;
377 void ahd_set_sense_residual(struct scb *scb, u_long resid)
379 scb->io_ctx->csio.sense_resid = resid;
383 u_long ahd_get_residual(struct scb *scb)
385 return (scb->io_ctx->csio.resid);
389 int ahd_perform_autosense(struct scb *scb)
391 return (!(scb->io_ctx->ccb_h.flags & CAM_DIS_AUTOSENSE));
394 static __inline uint32_t
395 ahd_get_sense_bufsize(struct ahd_softc *ahd, struct scb *scb)
397 return (sizeof(struct scsi_sense_data));
401 ahd_freeze_simq(struct ahd_softc *ahd)
403 xpt_freeze_simq(ahd->platform_data->sim, /*count*/1);
407 ahd_release_simq(struct ahd_softc *ahd)
409 xpt_release_simq(ahd->platform_data->sim, /*run queue*/TRUE);
413 ahd_freeze_ccb(union ccb *ccb)
415 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
416 ccb->ccb_h.status |= CAM_DEV_QFRZN;
417 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
422 ahd_freeze_scb(struct scb *scb)
424 ahd_freeze_ccb(scb->io_ctx);
428 ahd_platform_freeze_devq(struct ahd_softc *ahd, struct scb *scb)
430 /* Nothing to do here for FreeBSD */
434 ahd_platform_abort_scbs(struct ahd_softc *ahd, int target,
435 char channel, int lun, u_int tag,
436 role_t role, uint32_t status)
438 /* Nothing to do here for FreeBSD */
443 ahd_platform_scb_free(struct ahd_softc *ahd, struct scb *scb)
445 /* What do we do to generically handle driver resource shortages??? */
446 if ((ahd->flags & AHD_RESOURCE_SHORTAGE) != 0
447 && scb->io_ctx != NULL
448 && (scb->io_ctx->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
449 scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
450 ahd->flags &= ~AHD_RESOURCE_SHORTAGE;
455 /********************************** PCI ***************************************/
456 #ifdef AHD_PCI_CONFIG
457 static __inline uint32_t ahd_pci_read_config(ahd_dev_softc_t pci,
459 static __inline void ahd_pci_write_config(ahd_dev_softc_t pci,
460 int reg, uint32_t value,
462 static __inline int ahd_get_pci_function(ahd_dev_softc_t);
463 static __inline int ahd_get_pci_slot(ahd_dev_softc_t);
464 static __inline int ahd_get_pci_bus(ahd_dev_softc_t);
466 int ahd_pci_map_registers(struct ahd_softc *ahd);
467 int ahd_pci_map_int(struct ahd_softc *ahd);
469 static __inline uint32_t
470 ahd_pci_read_config(ahd_dev_softc_t pci, int reg, int width)
472 return (pci_read_config(pci, reg, width));
476 ahd_pci_write_config(ahd_dev_softc_t pci, int reg, uint32_t value, int width)
478 pci_write_config(pci, reg, value, width);
482 ahd_get_pci_function(ahd_dev_softc_t pci)
484 return (pci_get_function(pci));
488 ahd_get_pci_slot(ahd_dev_softc_t pci)
490 return (pci_get_slot(pci));
494 ahd_get_pci_bus(ahd_dev_softc_t pci)
496 return (pci_get_bus(pci));
507 void ahd_power_state_change(struct ahd_softc *ahd,
508 ahd_power_state new_state);
510 /******************************** VL/EISA *************************************/
511 int aic7770_map_registers(struct ahd_softc *ahd);
512 int aic7770_map_int(struct ahd_softc *ahd, int irq);
514 /********************************* Debug **************************************/
515 static __inline void ahd_print_path(struct ahd_softc *, struct scb *);
516 static __inline void ahd_platform_dump_card_state(struct ahd_softc *ahd);
519 ahd_print_path(struct ahd_softc *ahd, struct scb *scb)
521 xpt_print_path(scb->io_ctx->ccb_h.path);
525 ahd_platform_dump_card_state(struct ahd_softc *ahd)
527 /* Nothing to do here for FreeBSD */
529 /**************************** Transfer Settings *******************************/
530 void ahd_notify_xfer_settings_change(struct ahd_softc *,
531 struct ahd_devinfo *);
532 void ahd_platform_set_tags(struct ahd_softc *, struct ahd_devinfo *,
535 /************************* Initialization/Teardown ****************************/
536 int ahd_platform_alloc(struct ahd_softc *ahd, void *platform_arg);
537 void ahd_platform_free(struct ahd_softc *ahd);
538 int ahd_map_int(struct ahd_softc *ahd);
539 int ahd_attach(struct ahd_softc *);
540 int ahd_softc_comp(struct ahd_softc *lahd, struct ahd_softc *rahd);
541 int ahd_detach(device_t);
542 #define ahd_platform_init(arg)
545 /****************************** Interrupts ************************************/
546 void ahd_platform_intr(void *);
547 static __inline void ahd_platform_flushwork(struct ahd_softc *ahd);
549 ahd_platform_flushwork(struct ahd_softc *ahd)
553 /************************ Misc Function Declarations **************************/
554 void ahd_done(struct ahd_softc *ahd, struct scb *scb);
555 void ahd_send_async(struct ahd_softc *, char /*channel*/,
556 u_int /*target*/, u_int /*lun*/, ac_code, void *arg);
557 #endif /* _AIC79XX_FREEBSD_H_ */