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.6 2005/06/03 16:57:13 eirikn 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/bus_memio.h>
58 #include <machine/bus_pio.h>
59 #include <machine/bus.h>
60 #include <machine/endian.h>
61 #include <machine/clock.h>
62 #include <machine/resource.h>
66 #include <bus/pci/pcireg.h>
67 #include <bus/pci/pcivar.h>
69 #include <bus/cam/cam.h>
70 #include <bus/cam/cam_ccb.h>
71 #include <bus/cam/cam_debug.h>
72 #include <bus/cam/cam_sim.h>
73 #include <bus/cam/cam_xpt_sim.h>
75 #include <bus/cam/scsi/scsi_all.h>
76 #include <bus/cam/scsi/scsi_message.h>
77 #include <bus/cam/scsi/scsi_iu.h>
79 #ifdef CAM_NEW_TRAN_CODE
80 #define AHD_NEW_TRAN_SETTINGS
81 #endif /* CAM_NEW_TRAN_CODE */
83 /****************************** Platform Macros *******************************/
84 #define SIM_IS_SCSIBUS_B(ahd, sim) \
86 #define SIM_CHANNEL(ahd, sim) \
88 #define SIM_SCSI_ID(ahd, sim) \
90 #define SIM_PATH(ahd, sim) \
91 (ahd->platform_data->path)
92 #define BUILD_SCSIID(ahd, sim, target_id, our_id) \
93 ((((target_id) << TID_SHIFT) & TID) | (our_id))
96 #define SCB_GET_SIM(ahd, scb) \
97 ((ahd)->platform_data->sim)
100 #define offsetof(type, member) ((size_t)(&((type *)0)->member))
102 /************************* Forward Declarations *******************************/
103 typedef device_t ahd_dev_softc_t;
104 typedef union ccb *ahd_io_ctx_t;
106 /***************************** Bus Space/DMA **********************************/
107 #define ahd_dma_tag_create(ahd, parent_tag, alignment, boundary, \
108 lowaddr, highaddr, filter, filterarg, \
109 maxsize, nsegments, maxsegsz, flags, \
111 bus_dma_tag_create(parent_tag, alignment, boundary, \
112 lowaddr, highaddr, filter, filterarg, \
113 maxsize, nsegments, maxsegsz, flags, \
116 #define ahd_dma_tag_destroy(ahd, tag) \
117 bus_dma_tag_destroy(tag)
119 #define ahd_dmamem_alloc(ahd, dmat, vaddr, flags, mapp) \
120 bus_dmamem_alloc(dmat, vaddr, flags, mapp)
122 #define ahd_dmamem_free(ahd, dmat, vaddr, map) \
123 bus_dmamem_free(dmat, vaddr, map)
125 #define ahd_dmamap_create(ahd, tag, flags, mapp) \
126 bus_dmamap_create(tag, flags, mapp)
128 #define ahd_dmamap_destroy(ahd, tag, map) \
129 bus_dmamap_destroy(tag, map)
131 #define ahd_dmamap_load(ahd, dmat, map, addr, buflen, callback, \
132 callback_arg, flags) \
133 bus_dmamap_load(dmat, map, addr, buflen, callback, callback_arg, flags)
135 #define ahd_dmamap_unload(ahd, tag, map) \
136 bus_dmamap_unload(tag, map)
138 /* XXX Need to update Bus DMA for partial map syncs */
139 #define ahd_dmamap_sync(ahd, dma_tag, dmamap, offset, len, op) \
140 bus_dmamap_sync(dma_tag, dmamap, op)
142 /************************ Tunable Driver Parameters **************************/
144 * The number of dma segments supported. The sequencer can handle any number
145 * of physically contiguous S/G entrys. To reduce the driver's memory
146 * consumption, we limit the number supported to be sufficient to handle
147 * the largest mapping supported by the kernel, MAXPHYS. Assuming the
148 * transfer is as fragmented as possible and unaligned, this turns out to
149 * be the number of paged sized transfers in MAXPHYS plus an extra element
150 * to handle any unaligned residual. The sequencer fetches SG elements
151 * in cacheline sized chucks, so make the number per-transaction an even
152 * multiple of 16 which should align us on even the largest of cacheline
155 #define AHD_NSEG (roundup(btoc(MAXPHYS) + 1, 16))
157 /* This driver supports target mode */
159 #define AHD_TARGET_MODE 1
162 /************************** Softc/SCB Platform Data ***************************/
163 struct ahd_platform_data {
165 * Hooks into the XPT.
168 struct cam_sim *sim_b;
169 struct cam_path *path;
170 struct cam_path *path_b;
172 int regs_res_type[2];
175 struct resource *regs[2];
176 struct resource *irq;
181 struct scb_platform_data {
184 /********************************* Byte Order *********************************/
185 #if defined(__FreeBSD___) && __FreeBSD_version >= 500000
186 #define ahd_htobe16(x) htobe16(x)
187 #define ahd_htobe32(x) htobe32(x)
188 #define ahd_htobe64(x) htobe64(x)
189 #define ahd_htole16(x) htole16(x)
190 #define ahd_htole32(x) htole32(x)
191 #define ahd_htole64(x) htole64(x)
193 #define ahd_be16toh(x) be16toh(x)
194 #define ahd_be32toh(x) be32toh(x)
195 #define ahd_be64toh(x) be64toh(x)
196 #define ahd_le16toh(x) le16toh(x)
197 #define ahd_le32toh(x) le32toh(x)
198 #define ahd_le64toh(x) le64toh(x)
200 #define ahd_htobe16(x) (x)
201 #define ahd_htobe32(x) (x)
202 #define ahd_htobe64(x) (x)
203 #define ahd_htole16(x) (x)
204 #define ahd_htole32(x) (x)
205 #define ahd_htole64(x) (x)
207 #define ahd_be16toh(x) (x)
208 #define ahd_be32toh(x) (x)
209 #define ahd_be64toh(x) (x)
210 #define ahd_le16toh(x) (x)
211 #define ahd_le32toh(x) (x)
212 #define ahd_le64toh(x) (x)
215 /************************** Timer DataStructures ******************************/
216 typedef struct callout ahd_timer_t;
218 /***************************** Core Includes **********************************/
219 #if AHD_REG_PRETTY_PRINT
220 #define AIC_DEBUG_REGISTERS 1
222 #define AIC_DEBUG_REGISTERS 0
226 /***************************** Timer Facilities *******************************/
227 timeout_t ahd_timeout;
228 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
229 #define ahd_timer_init(timer) callout_init(timer, /*mpsafe*/0)
231 #define ahd_timer_init callout_init
233 #define ahd_timer_stop callout_stop
236 ahd_timer_reset(ahd_timer_t *timer, u_int usec, ahd_callback_t *func, void *arg)
238 callout_reset(timer, (usec * hz)/1000000, func, arg);
242 ahd_scb_timer_reset(struct scb *scb, u_int usec)
244 callout_reset(&scb->io_ctx->ccb_h.timeout_ch, (usec * hz)/1000000,
248 /*************************** Device Access ************************************/
249 #define ahd_inb(ahd, port) \
250 bus_space_read_1((ahd)->tags[(port) >> 8], \
251 (ahd)->bshs[(port) >> 8], (port) & 0xFF)
253 #define ahd_outb(ahd, port, value) \
254 bus_space_write_1((ahd)->tags[(port) >> 8], \
255 (ahd)->bshs[(port) >> 8], (port) & 0xFF, value)
257 #define ahd_inw_atomic(ahd, port) \
258 ahd_le16toh(bus_space_read_2((ahd)->tags[(port) >> 8], \
259 (ahd)->bshs[(port) >> 8], (port) & 0xFF))
261 #define ahd_outw_atomic(ahd, port, value) \
262 bus_space_write_2((ahd)->tags[(port) >> 8], \
263 (ahd)->bshs[(port) >> 8], \
264 (port & 0xFF), ahd_htole16(value))
266 #define ahd_outsb(ahd, port, valp, count) \
267 bus_space_write_multi_1((ahd)->tags[(port) >> 8], \
268 (ahd)->bshs[(port) >> 8], \
269 (port & 0xFF), valp, count)
271 #define ahd_insb(ahd, port, valp, count) \
272 bus_space_read_multi_1((ahd)->tags[(port) >> 8], \
273 (ahd)->bshs[(port) >> 8], \
274 (port & 0xFF), valp, count)
276 static __inline void ahd_flush_device_writes(struct ahd_softc *);
279 ahd_flush_device_writes(struct ahd_softc *ahd)
281 /* XXX Is this sufficient for all architectures??? */
282 ahd_inb(ahd, INTSTAT);
285 /**************************** Locking Primitives ******************************/
286 /* Lock protecting internal data structures */
287 static __inline void ahd_lockinit(struct ahd_softc *);
288 static __inline void ahd_lock(struct ahd_softc *, unsigned long *flags);
289 static __inline void ahd_unlock(struct ahd_softc *, unsigned long *flags);
291 /* Lock held during command compeletion to the upper layer */
292 static __inline void ahd_done_lockinit(struct ahd_softc *);
293 static __inline void ahd_done_lock(struct ahd_softc *, unsigned long *flags);
294 static __inline void ahd_done_unlock(struct ahd_softc *, unsigned long *flags);
296 /* Lock held during ahd_list manipulation and ahd softc frees */
297 static __inline void ahd_list_lockinit(void);
298 static __inline void ahd_list_lock(unsigned long *flags);
299 static __inline void ahd_list_unlock(unsigned long *flags);
302 ahd_lockinit(struct ahd_softc *ahd)
307 ahd_lock(struct ahd_softc *ahd, unsigned long *flags __unused)
313 ahd_unlock(struct ahd_softc *ahd, unsigned long *flags __unused)
318 /* Lock held during command compeletion to the upper layer */
320 ahd_done_lockinit(struct ahd_softc *ahd)
325 ahd_done_lock(struct ahd_softc *ahd, unsigned long *flags)
330 ahd_done_unlock(struct ahd_softc *ahd, unsigned long *flags)
334 /* Lock held during ahd_list manipulation and ahd softc frees */
336 ahd_list_lockinit(void)
341 ahd_list_lock(unsigned long *flags)
346 ahd_list_unlock(unsigned long *flags)
349 /****************************** OS Primitives *********************************/
350 #define ahd_delay DELAY
352 /************************** Transaction Operations ****************************/
353 static __inline void ahd_set_transaction_status(struct scb *, uint32_t);
354 static __inline void ahd_set_scsi_status(struct scb *, uint32_t);
355 static __inline uint32_t ahd_get_transaction_status(struct scb *);
356 static __inline uint32_t ahd_get_scsi_status(struct scb *);
357 static __inline void ahd_set_transaction_tag(struct scb *, int, u_int);
358 static __inline u_long ahd_get_transfer_length(struct scb *);
359 static __inline int ahd_get_transfer_dir(struct scb *);
360 static __inline void ahd_set_residual(struct scb *, u_long);
361 static __inline void ahd_set_sense_residual(struct scb *, u_long);
362 static __inline u_long ahd_get_residual(struct scb *);
363 static __inline int ahd_perform_autosense(struct scb *);
364 static __inline uint32_t ahd_get_sense_bufsize(struct ahd_softc*, struct scb*);
365 static __inline void ahd_freeze_simq(struct ahd_softc *);
366 static __inline void ahd_release_simq(struct ahd_softc *);
367 static __inline void ahd_freeze_ccb(union ccb *ccb);
368 static __inline void ahd_freeze_scb(struct scb *scb);
369 static __inline void ahd_platform_freeze_devq(struct ahd_softc *, struct scb *);
370 static __inline int ahd_platform_abort_scbs(struct ahd_softc *ahd, int target,
371 char channel, int lun, u_int tag,
372 role_t role, uint32_t status);
375 void ahd_set_transaction_status(struct scb *scb, uint32_t status)
377 scb->io_ctx->ccb_h.status &= ~CAM_STATUS_MASK;
378 scb->io_ctx->ccb_h.status |= status;
382 void ahd_set_scsi_status(struct scb *scb, uint32_t status)
384 scb->io_ctx->csio.scsi_status = status;
388 uint32_t ahd_get_transaction_status(struct scb *scb)
390 return (scb->io_ctx->ccb_h.status & CAM_STATUS_MASK);
394 uint32_t ahd_get_scsi_status(struct scb *scb)
396 return (scb->io_ctx->csio.scsi_status);
400 void ahd_set_transaction_tag(struct scb *scb, int enabled, u_int type)
402 scb->io_ctx->csio.tag_action = type;
404 scb->io_ctx->ccb_h.flags |= CAM_TAG_ACTION_VALID;
406 scb->io_ctx->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
410 u_long ahd_get_transfer_length(struct scb *scb)
412 return (scb->io_ctx->csio.dxfer_len);
416 int ahd_get_transfer_dir(struct scb *scb)
418 return (scb->io_ctx->ccb_h.flags & CAM_DIR_MASK);
422 void ahd_set_residual(struct scb *scb, u_long resid)
424 scb->io_ctx->csio.resid = resid;
428 void ahd_set_sense_residual(struct scb *scb, u_long resid)
430 scb->io_ctx->csio.sense_resid = resid;
434 u_long ahd_get_residual(struct scb *scb)
436 return (scb->io_ctx->csio.resid);
440 int ahd_perform_autosense(struct scb *scb)
442 return (!(scb->io_ctx->ccb_h.flags & CAM_DIS_AUTOSENSE));
445 static __inline uint32_t
446 ahd_get_sense_bufsize(struct ahd_softc *ahd, struct scb *scb)
448 return (sizeof(struct scsi_sense_data));
452 ahd_freeze_simq(struct ahd_softc *ahd)
454 xpt_freeze_simq(ahd->platform_data->sim, /*count*/1);
458 ahd_release_simq(struct ahd_softc *ahd)
460 xpt_release_simq(ahd->platform_data->sim, /*run queue*/TRUE);
464 ahd_freeze_ccb(union ccb *ccb)
466 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
467 ccb->ccb_h.status |= CAM_DEV_QFRZN;
468 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
473 ahd_freeze_scb(struct scb *scb)
475 ahd_freeze_ccb(scb->io_ctx);
479 ahd_platform_freeze_devq(struct ahd_softc *ahd, struct scb *scb)
481 /* Nothing to do here for FreeBSD */
485 ahd_platform_abort_scbs(struct ahd_softc *ahd, int target,
486 char channel, int lun, u_int tag,
487 role_t role, uint32_t status)
489 /* Nothing to do here for FreeBSD */
494 ahd_platform_scb_free(struct ahd_softc *ahd, struct scb *scb)
496 /* What do we do to generically handle driver resource shortages??? */
497 if ((ahd->flags & AHD_RESOURCE_SHORTAGE) != 0
498 && scb->io_ctx != NULL
499 && (scb->io_ctx->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
500 scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
501 ahd->flags &= ~AHD_RESOURCE_SHORTAGE;
506 /********************************** PCI ***************************************/
507 #ifdef AHD_PCI_CONFIG
508 static __inline uint32_t ahd_pci_read_config(ahd_dev_softc_t pci,
510 static __inline void ahd_pci_write_config(ahd_dev_softc_t pci,
511 int reg, uint32_t value,
513 static __inline int ahd_get_pci_function(ahd_dev_softc_t);
514 static __inline int ahd_get_pci_slot(ahd_dev_softc_t);
515 static __inline int ahd_get_pci_bus(ahd_dev_softc_t);
517 int ahd_pci_map_registers(struct ahd_softc *ahd);
518 int ahd_pci_map_int(struct ahd_softc *ahd);
520 static __inline uint32_t
521 ahd_pci_read_config(ahd_dev_softc_t pci, int reg, int width)
523 return (pci_read_config(pci, reg, width));
527 ahd_pci_write_config(ahd_dev_softc_t pci, int reg, uint32_t value, int width)
529 pci_write_config(pci, reg, value, width);
533 ahd_get_pci_function(ahd_dev_softc_t pci)
535 return (pci_get_function(pci));
539 ahd_get_pci_slot(ahd_dev_softc_t pci)
541 return (pci_get_slot(pci));
545 ahd_get_pci_bus(ahd_dev_softc_t pci)
547 return (pci_get_bus(pci));
558 void ahd_power_state_change(struct ahd_softc *ahd,
559 ahd_power_state new_state);
561 /******************************** VL/EISA *************************************/
562 int aic7770_map_registers(struct ahd_softc *ahd);
563 int aic7770_map_int(struct ahd_softc *ahd, int irq);
565 /********************************* Debug **************************************/
566 static __inline void ahd_print_path(struct ahd_softc *, struct scb *);
567 static __inline void ahd_platform_dump_card_state(struct ahd_softc *ahd);
570 ahd_print_path(struct ahd_softc *ahd, struct scb *scb)
572 xpt_print_path(scb->io_ctx->ccb_h.path);
576 ahd_platform_dump_card_state(struct ahd_softc *ahd)
578 /* Nothing to do here for FreeBSD */
580 /**************************** Transfer Settings *******************************/
581 void ahd_notify_xfer_settings_change(struct ahd_softc *,
582 struct ahd_devinfo *);
583 void ahd_platform_set_tags(struct ahd_softc *, struct ahd_devinfo *,
586 /************************* Initialization/Teardown ****************************/
587 int ahd_platform_alloc(struct ahd_softc *ahd, void *platform_arg);
588 void ahd_platform_free(struct ahd_softc *ahd);
589 int ahd_map_int(struct ahd_softc *ahd);
590 int ahd_attach(struct ahd_softc *);
591 int ahd_softc_comp(struct ahd_softc *lahd, struct ahd_softc *rahd);
592 int ahd_detach(device_t);
593 #define ahd_platform_init(arg)
596 /****************************** Interrupts ************************************/
597 void ahd_platform_intr(void *);
598 static __inline void ahd_platform_flushwork(struct ahd_softc *ahd);
600 ahd_platform_flushwork(struct ahd_softc *ahd)
604 /************************ Misc Function Declarations **************************/
605 void ahd_done(struct ahd_softc *ahd, struct scb *scb);
606 void ahd_send_async(struct ahd_softc *, char /*channel*/,
607 u_int /*target*/, u_int /*lun*/, ac_code, void *arg);
608 #endif /* _AIC79XX_FREEBSD_H_ */