2 * FreeBSD platform specific driver option settings, data structures,
3 * function declarations and includes.
5 * Copyright (c) 1994-2001 Justin T. Gibbs.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * Alternatively, this software may be distributed under the terms of the
18 * GNU Public License ("GPL").
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $Id: //depot/aic7xxx/freebsd/dev/aic7xxx/aic7xxx_osm.h#10 $
37 #ifndef _AIC7XXX_FREEBSD_H_
38 #define _AIC7XXX_FREEBSD_H_
40 #include <opt_aic7xxx.h> /* for config options */
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/bus.h> /* For device_t */
45 #if __FreeBSD_version >= 500000
46 #include <sys/endian.h>
48 #include <sys/eventhandler.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/queue.h>
53 #if __FreeBSD_version < 500000
60 #define AHC_PCI_CONFIG 1
61 #ifdef AHC_ALLOW_MEMIO
62 #include <machine/bus_memio.h>
65 #include <machine/bus_pio.h>
66 #include <machine/bus.h>
67 #include <machine/endian.h>
68 #include <machine/clock.h>
69 #include <machine/resource.h>
74 #include <pci/pcireg.h>
75 #include <pci/pcivar.h>
79 #include <cam/cam_ccb.h>
80 #include <cam/cam_debug.h>
81 #include <cam/cam_sim.h>
82 #include <cam/cam_xpt_sim.h>
84 #include <cam/scsi/scsi_all.h>
85 #include <cam/scsi/scsi_message.h>
87 #ifdef CAM_NEW_TRAN_CODE
88 #define AHC_NEW_TRAN_SETTINGS
89 #endif /* CAM_NEW_TRAN_CODE */
91 /****************************** Platform Macros *******************************/
92 #define SIM_IS_SCSIBUS_B(ahc, sim) \
93 ((sim) == ahc->platform_data->sim_b)
94 #define SIM_CHANNEL(ahc, sim) \
95 (((sim) == ahc->platform_data->sim_b) ? 'B' : 'A')
96 #define SIM_SCSI_ID(ahc, sim) \
97 (((sim) == ahc->platform_data->sim_b) ? ahc->our_id_b : ahc->our_id)
98 #define SIM_PATH(ahc, sim) \
99 (((sim) == ahc->platform_data->sim_b) ? ahc->platform_data->path_b \
100 : ahc->platform_data->path)
101 #define BUILD_SCSIID(ahc, sim, target_id, our_id) \
102 ((((target_id) << TID_SHIFT) & TID) | (our_id) \
103 | (SIM_IS_SCSIBUS_B(ahc, sim) ? TWIN_CHNLB : 0))
105 #define SCB_GET_SIM(ahc, scb) \
106 (SCB_GET_CHANNEL(ahc, scb) == 'A' ? (ahc)->platform_data->sim \
107 : (ahc)->platform_data->sim_b)
110 #define offsetof(type, member) ((size_t)(&((type *)0)->member))
112 /************************* Forward Declarations *******************************/
113 typedef device_t ahc_dev_softc_t;
114 typedef union ccb *ahc_io_ctx_t;
116 /***************************** Bus Space/DMA **********************************/
117 #define ahc_dma_tag_create(ahc, parent_tag, alignment, boundary, \
118 lowaddr, highaddr, filter, filterarg, \
119 maxsize, nsegments, maxsegsz, flags, \
121 bus_dma_tag_create(parent_tag, alignment, boundary, \
122 lowaddr, highaddr, filter, filterarg, \
123 maxsize, nsegments, maxsegsz, flags, \
126 #define ahc_dma_tag_destroy(ahc, tag) \
127 bus_dma_tag_destroy(tag)
129 #define ahc_dmamem_alloc(ahc, dmat, vaddr, flags, mapp) \
130 bus_dmamem_alloc(dmat, vaddr, flags, mapp)
132 #define ahc_dmamem_free(ahc, dmat, vaddr, map) \
133 bus_dmamem_free(dmat, vaddr, map)
135 #define ahc_dmamap_create(ahc, tag, flags, mapp) \
136 bus_dmamap_create(tag, flags, mapp)
138 #define ahc_dmamap_destroy(ahc, tag, map) \
139 bus_dmamap_destroy(tag, map)
141 #define ahc_dmamap_load(ahc, dmat, map, addr, buflen, callback, \
142 callback_arg, flags) \
143 bus_dmamap_load(dmat, map, addr, buflen, callback, callback_arg, flags)
145 #define ahc_dmamap_unload(ahc, tag, map) \
146 bus_dmamap_unload(tag, map)
148 /* XXX Need to update Bus DMA for partial map syncs */
149 #define ahc_dmamap_sync(ahc, dma_tag, dmamap, offset, len, op) \
150 bus_dmamap_sync(dma_tag, dmamap, op)
152 /************************ Tunable Driver Parameters **************************/
154 * The number of dma segments supported. The sequencer can handle any number
155 * of physically contiguous S/G entrys. To reduce the driver's memory
156 * consumption, we limit the number supported to be sufficient to handle
157 * the largest mapping supported by the kernel, MAXPHYS. Assuming the
158 * transfer is as fragmented as possible and unaligned, this turns out to
159 * be the number of paged sized transfers in MAXPHYS plus an extra element
160 * to handle any unaligned residual. The sequencer fetches SG elements
161 * in cacheline sized chucks, so make the number per-transaction an even
162 * multiple of 16 which should align us on even the largest of cacheline
165 #define AHC_NSEG (roundup(btoc(MAXPHYS) + 1, 16))
167 /* This driver supports target mode */
168 #define AHC_TARGET_MODE 1
170 /************************** Softc/SCB Platform Data ***************************/
171 struct ahc_platform_data {
173 * Hooks into the XPT.
176 struct cam_sim *sim_b;
177 struct cam_path *path;
178 struct cam_path *path_b;
183 struct resource *regs;
184 struct resource *irq;
189 struct scb_platform_data {
192 /********************************* Byte Order *********************************/
193 #if __FreeBSD_version >= 500000
194 #define ahc_htobe16(x) htobe16(x)
195 #define ahc_htobe32(x) htobe32(x)
196 #define ahc_htobe64(x) htobe64(x)
197 #define ahc_htole16(x) htole16(x)
198 #define ahc_htole32(x) htole32(x)
199 #define ahc_htole64(x) htole64(x)
201 #define ahc_be16toh(x) be16toh(x)
202 #define ahc_be32toh(x) be32toh(x)
203 #define ahc_be64toh(x) be64toh(x)
204 #define ahc_le16toh(x) le16toh(x)
205 #define ahc_le32toh(x) le32toh(x)
206 #define ahc_le64toh(x) le64toh(x)
208 #define ahc_htobe16(x) (x)
209 #define ahc_htobe32(x) (x)
210 #define ahc_htobe64(x) (x)
211 #define ahc_htole16(x) (x)
212 #define ahc_htole32(x) (x)
213 #define ahc_htole64(x) (x)
215 #define ahc_be16toh(x) (x)
216 #define ahc_be32toh(x) (x)
217 #define ahc_be64toh(x) (x)
218 #define ahc_le16toh(x) (x)
219 #define ahc_le32toh(x) (x)
220 #define ahc_le64toh(x) (x)
223 /***************************** Core Includes **********************************/
224 #if AHC_REG_PRETTY_PRINT
225 #define AIC_DEBUG_REGISTERS 1
227 #define AIC_DEBUG_REGISTERS 0
229 #include <dev/aic7xxx/aic7xxx.h>
231 /*************************** Device Access ************************************/
232 #define ahc_inb(ahc, port) \
233 bus_space_read_1((ahc)->tag, (ahc)->bsh, port)
235 #define ahc_outb(ahc, port, value) \
236 bus_space_write_1((ahc)->tag, (ahc)->bsh, port, value)
238 #define ahc_outsb(ahc, port, valp, count) \
239 bus_space_write_multi_1((ahc)->tag, (ahc)->bsh, port, valp, count)
241 #define ahc_insb(ahc, port, valp, count) \
242 bus_space_read_multi_1((ahc)->tag, (ahc)->bsh, port, valp, count)
244 static __inline void ahc_flush_device_writes(struct ahc_softc *);
247 ahc_flush_device_writes(struct ahc_softc *ahc)
249 /* XXX Is this sufficient for all architectures??? */
250 ahc_inb(ahc, INTSTAT);
253 /**************************** Locking Primitives ******************************/
254 /* Lock protecting internal data structures */
255 static __inline void ahc_lockinit(struct ahc_softc *);
256 static __inline void ahc_lock(struct ahc_softc *, unsigned long *flags);
257 static __inline void ahc_unlock(struct ahc_softc *, unsigned long *flags);
259 /* Lock held during command compeletion to the upper layer */
260 static __inline void ahc_done_lockinit(struct ahc_softc *);
261 static __inline void ahc_done_lock(struct ahc_softc *, unsigned long *flags);
262 static __inline void ahc_done_unlock(struct ahc_softc *, unsigned long *flags);
264 /* Lock held during ahc_list manipulation and ahc softc frees */
265 static __inline void ahc_list_lockinit(void);
266 static __inline void ahc_list_lock(unsigned long *flags);
267 static __inline void ahc_list_unlock(unsigned long *flags);
270 ahc_lockinit(struct ahc_softc *ahc)
275 ahc_lock(struct ahc_softc *ahc, unsigned long *flags)
281 ahc_unlock(struct ahc_softc *ahc, unsigned long *flags)
286 /* Lock held during command compeletion to the upper layer */
288 ahc_done_lockinit(struct ahc_softc *ahc)
293 ahc_done_lock(struct ahc_softc *ahc, unsigned long *flags)
298 ahc_done_unlock(struct ahc_softc *ahc, unsigned long *flags)
302 /* Lock held during ahc_list manipulation and ahc softc frees */
309 ahc_list_lock(unsigned long *flags)
314 ahc_list_unlock(unsigned long *flags)
317 /****************************** OS Primitives *********************************/
318 #define ahc_delay DELAY
320 /************************** Transaction Operations ****************************/
321 static __inline void ahc_set_transaction_status(struct scb *, uint32_t);
322 static __inline void ahc_set_scsi_status(struct scb *, uint32_t);
323 static __inline uint32_t ahc_get_transaction_status(struct scb *);
324 static __inline uint32_t ahc_get_scsi_status(struct scb *);
325 static __inline void ahc_set_transaction_tag(struct scb *, int, u_int);
326 static __inline u_long ahc_get_transfer_length(struct scb *);
327 static __inline int ahc_get_transfer_dir(struct scb *);
328 static __inline void ahc_set_residual(struct scb *, u_long);
329 static __inline void ahc_set_sense_residual(struct scb *, u_long);
330 static __inline u_long ahc_get_residual(struct scb *);
331 static __inline int ahc_perform_autosense(struct scb *);
332 static __inline uint32_t ahc_get_sense_bufsize(struct ahc_softc*, struct scb*);
333 static __inline void ahc_freeze_ccb(union ccb *ccb);
334 static __inline void ahc_freeze_scb(struct scb *scb);
335 static __inline void ahc_platform_freeze_devq(struct ahc_softc *, struct scb *);
336 static __inline int ahc_platform_abort_scbs(struct ahc_softc *ahc, int target,
337 char channel, int lun, u_int tag,
338 role_t role, uint32_t status);
341 void ahc_set_transaction_status(struct scb *scb, uint32_t status)
343 scb->io_ctx->ccb_h.status &= ~CAM_STATUS_MASK;
344 scb->io_ctx->ccb_h.status |= status;
348 void ahc_set_scsi_status(struct scb *scb, uint32_t status)
350 scb->io_ctx->csio.scsi_status = status;
354 uint32_t ahc_get_transaction_status(struct scb *scb)
356 return (scb->io_ctx->ccb_h.status & CAM_STATUS_MASK);
360 uint32_t ahc_get_scsi_status(struct scb *scb)
362 return (scb->io_ctx->csio.scsi_status);
366 void ahc_set_transaction_tag(struct scb *scb, int enabled, u_int type)
368 scb->io_ctx->csio.tag_action = type;
370 scb->io_ctx->ccb_h.flags |= CAM_TAG_ACTION_VALID;
372 scb->io_ctx->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
376 u_long ahc_get_transfer_length(struct scb *scb)
378 return (scb->io_ctx->csio.dxfer_len);
382 int ahc_get_transfer_dir(struct scb *scb)
384 return (scb->io_ctx->ccb_h.flags & CAM_DIR_MASK);
388 void ahc_set_residual(struct scb *scb, u_long resid)
390 scb->io_ctx->csio.resid = resid;
394 void ahc_set_sense_residual(struct scb *scb, u_long resid)
396 scb->io_ctx->csio.sense_resid = resid;
400 u_long ahc_get_residual(struct scb *scb)
402 return (scb->io_ctx->csio.resid);
406 int ahc_perform_autosense(struct scb *scb)
408 return (!(scb->io_ctx->ccb_h.flags & CAM_DIS_AUTOSENSE));
411 static __inline uint32_t
412 ahc_get_sense_bufsize(struct ahc_softc *ahc, struct scb *scb)
414 return (sizeof(struct scsi_sense_data));
418 ahc_freeze_ccb(union ccb *ccb)
420 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
421 ccb->ccb_h.status |= CAM_DEV_QFRZN;
422 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
427 ahc_freeze_scb(struct scb *scb)
429 ahc_freeze_ccb(scb->io_ctx);
433 ahc_platform_freeze_devq(struct ahc_softc *ahc, struct scb *scb)
435 /* Nothing to do here for FreeBSD */
439 ahc_platform_abort_scbs(struct ahc_softc *ahc, int target,
440 char channel, int lun, u_int tag,
441 role_t role, uint32_t status)
443 /* Nothing to do here for FreeBSD */
448 ahc_platform_scb_free(struct ahc_softc *ahc, struct scb *scb)
450 /* What do we do to generically handle driver resource shortages??? */
451 if ((ahc->flags & AHC_RESOURCE_SHORTAGE) != 0
452 && scb->io_ctx != NULL
453 && (scb->io_ctx->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
454 scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
455 ahc->flags &= ~AHC_RESOURCE_SHORTAGE;
460 /********************************** PCI ***************************************/
461 #ifdef AHC_PCI_CONFIG
462 static __inline uint32_t ahc_pci_read_config(ahc_dev_softc_t pci,
464 static __inline void ahc_pci_write_config(ahc_dev_softc_t pci,
465 int reg, uint32_t value,
467 static __inline int ahc_get_pci_function(ahc_dev_softc_t);
468 static __inline int ahc_get_pci_slot(ahc_dev_softc_t);
469 static __inline int ahc_get_pci_bus(ahc_dev_softc_t);
471 int ahc_pci_map_registers(struct ahc_softc *ahc);
472 int ahc_pci_map_int(struct ahc_softc *ahc);
474 static __inline uint32_t
475 ahc_pci_read_config(ahc_dev_softc_t pci, int reg, int width)
477 return (pci_read_config(pci, reg, width));
481 ahc_pci_write_config(ahc_dev_softc_t pci, int reg, uint32_t value, int width)
483 pci_write_config(pci, reg, value, width);
487 ahc_get_pci_function(ahc_dev_softc_t pci)
489 return (pci_get_function(pci));
493 ahc_get_pci_slot(ahc_dev_softc_t pci)
495 return (pci_get_slot(pci));
499 ahc_get_pci_bus(ahc_dev_softc_t pci)
501 return (pci_get_bus(pci));
512 void ahc_power_state_change(struct ahc_softc *ahc,
513 ahc_power_state new_state);
515 /******************************** VL/EISA *************************************/
516 int aic7770_map_registers(struct ahc_softc *ahc, u_int port);
517 int aic7770_map_int(struct ahc_softc *ahc, int irq);
519 /********************************* Debug **************************************/
520 static __inline void ahc_print_path(struct ahc_softc *, struct scb *);
521 static __inline void ahc_platform_dump_card_state(struct ahc_softc *ahc);
524 ahc_print_path(struct ahc_softc *ahc, struct scb *scb)
526 xpt_print_path(scb->io_ctx->ccb_h.path);
530 ahc_platform_dump_card_state(struct ahc_softc *ahc)
532 /* Nothing to do here for FreeBSD */
534 /**************************** Transfer Settings *******************************/
535 void ahc_notify_xfer_settings_change(struct ahc_softc *,
536 struct ahc_devinfo *);
537 void ahc_platform_set_tags(struct ahc_softc *, struct ahc_devinfo *,
540 /************************* Initialization/Teardown ****************************/
541 int ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg);
542 void ahc_platform_free(struct ahc_softc *ahc);
543 int ahc_map_int(struct ahc_softc *ahc);
544 int ahc_attach(struct ahc_softc *);
545 int ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc);
546 int ahc_detach(device_t);
548 /****************************** Interrupts ************************************/
549 void ahc_platform_intr(void *);
550 static __inline void ahc_platform_flushwork(struct ahc_softc *ahc);
552 ahc_platform_flushwork(struct ahc_softc *ahc)
556 /************************ Misc Function Declarations **************************/
557 timeout_t ahc_timeout;
558 void ahc_done(struct ahc_softc *ahc, struct scb *scb);
559 void ahc_send_async(struct ahc_softc *, char /*channel*/,
560 u_int /*target*/, u_int /*lun*/, ac_code, void *arg);
561 #endif /* _AIC7XXX_FREEBSD_H_ */