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#11 $
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 /*************************** Attachment Bookkeeping ***************************/
92 extern devclass_t ahc_devclass;
94 /****************************** Platform Macros *******************************/
95 #define SIM_IS_SCSIBUS_B(ahc, sim) \
96 ((sim) == ahc->platform_data->sim_b)
97 #define SIM_CHANNEL(ahc, sim) \
98 (((sim) == ahc->platform_data->sim_b) ? 'B' : 'A')
99 #define SIM_SCSI_ID(ahc, sim) \
100 (((sim) == ahc->platform_data->sim_b) ? ahc->our_id_b : ahc->our_id)
101 #define SIM_PATH(ahc, sim) \
102 (((sim) == ahc->platform_data->sim_b) ? ahc->platform_data->path_b \
103 : ahc->platform_data->path)
104 #define BUILD_SCSIID(ahc, sim, target_id, our_id) \
105 ((((target_id) << TID_SHIFT) & TID) | (our_id) \
106 | (SIM_IS_SCSIBUS_B(ahc, sim) ? TWIN_CHNLB : 0))
108 #define SCB_GET_SIM(ahc, scb) \
109 (SCB_GET_CHANNEL(ahc, scb) == 'A' ? (ahc)->platform_data->sim \
110 : (ahc)->platform_data->sim_b)
113 #define offsetof(type, member) ((size_t)(&((type *)0)->member))
115 /************************* Forward Declarations *******************************/
116 typedef device_t ahc_dev_softc_t;
117 typedef union ccb *ahc_io_ctx_t;
119 /***************************** Bus Space/DMA **********************************/
120 #define ahc_dma_tag_create(ahc, parent_tag, alignment, boundary, \
121 lowaddr, highaddr, filter, filterarg, \
122 maxsize, nsegments, maxsegsz, flags, \
124 bus_dma_tag_create(parent_tag, alignment, boundary, \
125 lowaddr, highaddr, filter, filterarg, \
126 maxsize, nsegments, maxsegsz, flags, \
129 #define ahc_dma_tag_destroy(ahc, tag) \
130 bus_dma_tag_destroy(tag)
132 #define ahc_dmamem_alloc(ahc, dmat, vaddr, flags, mapp) \
133 bus_dmamem_alloc(dmat, vaddr, flags, mapp)
135 #define ahc_dmamem_free(ahc, dmat, vaddr, map) \
136 bus_dmamem_free(dmat, vaddr, map)
138 #define ahc_dmamap_create(ahc, tag, flags, mapp) \
139 bus_dmamap_create(tag, flags, mapp)
141 #define ahc_dmamap_destroy(ahc, tag, map) \
142 bus_dmamap_destroy(tag, map)
144 #define ahc_dmamap_load(ahc, dmat, map, addr, buflen, callback, \
145 callback_arg, flags) \
146 bus_dmamap_load(dmat, map, addr, buflen, callback, callback_arg, flags)
148 #define ahc_dmamap_unload(ahc, tag, map) \
149 bus_dmamap_unload(tag, map)
151 /* XXX Need to update Bus DMA for partial map syncs */
152 #define ahc_dmamap_sync(ahc, dma_tag, dmamap, offset, len, op) \
153 bus_dmamap_sync(dma_tag, dmamap, op)
155 /************************ Tunable Driver Parameters **************************/
157 * The number of dma segments supported. The sequencer can handle any number
158 * of physically contiguous S/G entrys. To reduce the driver's memory
159 * consumption, we limit the number supported to be sufficient to handle
160 * the largest mapping supported by the kernel, MAXPHYS. Assuming the
161 * transfer is as fragmented as possible and unaligned, this turns out to
162 * be the number of paged sized transfers in MAXPHYS plus an extra element
163 * to handle any unaligned residual. The sequencer fetches SG elements
164 * in cacheline sized chucks, so make the number per-transaction an even
165 * multiple of 16 which should align us on even the largest of cacheline
168 #define AHC_NSEG (roundup(btoc(MAXPHYS) + 1, 16))
170 /* This driver supports target mode */
171 #define AHC_TARGET_MODE 1
173 /************************** Softc/SCB Platform Data ***************************/
174 struct ahc_platform_data {
176 * Hooks into the XPT.
179 struct cam_sim *sim_b;
180 struct cam_path *path;
181 struct cam_path *path_b;
186 struct resource *regs;
187 struct resource *irq;
192 struct scb_platform_data {
195 /********************************* Byte Order *********************************/
196 #if __FreeBSD_version >= 500000
197 #define ahc_htobe16(x) htobe16(x)
198 #define ahc_htobe32(x) htobe32(x)
199 #define ahc_htobe64(x) htobe64(x)
200 #define ahc_htole16(x) htole16(x)
201 #define ahc_htole32(x) htole32(x)
202 #define ahc_htole64(x) htole64(x)
204 #define ahc_be16toh(x) be16toh(x)
205 #define ahc_be32toh(x) be32toh(x)
206 #define ahc_be64toh(x) be64toh(x)
207 #define ahc_le16toh(x) le16toh(x)
208 #define ahc_le32toh(x) le32toh(x)
209 #define ahc_le64toh(x) le64toh(x)
211 #define ahc_htobe16(x) (x)
212 #define ahc_htobe32(x) (x)
213 #define ahc_htobe64(x) (x)
214 #define ahc_htole16(x) (x)
215 #define ahc_htole32(x) (x)
216 #define ahc_htole64(x) (x)
218 #define ahc_be16toh(x) (x)
219 #define ahc_be32toh(x) (x)
220 #define ahc_be64toh(x) (x)
221 #define ahc_le16toh(x) (x)
222 #define ahc_le32toh(x) (x)
223 #define ahc_le64toh(x) (x)
226 /***************************** Core Includes **********************************/
227 #if AHC_REG_PRETTY_PRINT
228 #define AIC_DEBUG_REGISTERS 1
230 #define AIC_DEBUG_REGISTERS 0
232 #include <dev/aic7xxx/aic7xxx.h>
234 /*************************** Device Access ************************************/
235 #define ahc_inb(ahc, port) \
236 bus_space_read_1((ahc)->tag, (ahc)->bsh, port)
238 #define ahc_outb(ahc, port, value) \
239 bus_space_write_1((ahc)->tag, (ahc)->bsh, port, value)
241 #define ahc_outsb(ahc, port, valp, count) \
242 bus_space_write_multi_1((ahc)->tag, (ahc)->bsh, port, valp, count)
244 #define ahc_insb(ahc, port, valp, count) \
245 bus_space_read_multi_1((ahc)->tag, (ahc)->bsh, port, valp, count)
247 static __inline void ahc_flush_device_writes(struct ahc_softc *);
250 ahc_flush_device_writes(struct ahc_softc *ahc)
252 /* XXX Is this sufficient for all architectures??? */
253 ahc_inb(ahc, INTSTAT);
256 /**************************** Locking Primitives ******************************/
257 /* Lock protecting internal data structures */
258 static __inline void ahc_lockinit(struct ahc_softc *);
259 static __inline void ahc_lock(struct ahc_softc *, unsigned long *flags);
260 static __inline void ahc_unlock(struct ahc_softc *, unsigned long *flags);
262 /* Lock held during command compeletion to the upper layer */
263 static __inline void ahc_done_lockinit(struct ahc_softc *);
264 static __inline void ahc_done_lock(struct ahc_softc *, unsigned long *flags);
265 static __inline void ahc_done_unlock(struct ahc_softc *, unsigned long *flags);
267 /* Lock held during ahc_list manipulation and ahc softc frees */
268 static __inline void ahc_list_lockinit(void);
269 static __inline void ahc_list_lock(unsigned long *flags);
270 static __inline void ahc_list_unlock(unsigned long *flags);
273 ahc_lockinit(struct ahc_softc *ahc)
278 ahc_lock(struct ahc_softc *ahc, unsigned long *flags)
284 ahc_unlock(struct ahc_softc *ahc, unsigned long *flags)
289 /* Lock held during command compeletion to the upper layer */
291 ahc_done_lockinit(struct ahc_softc *ahc)
296 ahc_done_lock(struct ahc_softc *ahc, unsigned long *flags)
301 ahc_done_unlock(struct ahc_softc *ahc, unsigned long *flags)
305 /* Lock held during ahc_list manipulation and ahc softc frees */
312 ahc_list_lock(unsigned long *flags)
317 ahc_list_unlock(unsigned long *flags)
320 /****************************** OS Primitives *********************************/
321 #define ahc_delay DELAY
323 /************************** Transaction Operations ****************************/
324 static __inline void ahc_set_transaction_status(struct scb *, uint32_t);
325 static __inline void ahc_set_scsi_status(struct scb *, uint32_t);
326 static __inline uint32_t ahc_get_transaction_status(struct scb *);
327 static __inline uint32_t ahc_get_scsi_status(struct scb *);
328 static __inline void ahc_set_transaction_tag(struct scb *, int, u_int);
329 static __inline u_long ahc_get_transfer_length(struct scb *);
330 static __inline int ahc_get_transfer_dir(struct scb *);
331 static __inline void ahc_set_residual(struct scb *, u_long);
332 static __inline void ahc_set_sense_residual(struct scb *, u_long);
333 static __inline u_long ahc_get_residual(struct scb *);
334 static __inline int ahc_perform_autosense(struct scb *);
335 static __inline uint32_t ahc_get_sense_bufsize(struct ahc_softc*, struct scb*);
336 static __inline void ahc_freeze_ccb(union ccb *ccb);
337 static __inline void ahc_freeze_scb(struct scb *scb);
338 static __inline void ahc_platform_freeze_devq(struct ahc_softc *, struct scb *);
339 static __inline int ahc_platform_abort_scbs(struct ahc_softc *ahc, int target,
340 char channel, int lun, u_int tag,
341 role_t role, uint32_t status);
344 void ahc_set_transaction_status(struct scb *scb, uint32_t status)
346 scb->io_ctx->ccb_h.status &= ~CAM_STATUS_MASK;
347 scb->io_ctx->ccb_h.status |= status;
351 void ahc_set_scsi_status(struct scb *scb, uint32_t status)
353 scb->io_ctx->csio.scsi_status = status;
357 uint32_t ahc_get_transaction_status(struct scb *scb)
359 return (scb->io_ctx->ccb_h.status & CAM_STATUS_MASK);
363 uint32_t ahc_get_scsi_status(struct scb *scb)
365 return (scb->io_ctx->csio.scsi_status);
369 void ahc_set_transaction_tag(struct scb *scb, int enabled, u_int type)
371 scb->io_ctx->csio.tag_action = type;
373 scb->io_ctx->ccb_h.flags |= CAM_TAG_ACTION_VALID;
375 scb->io_ctx->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
379 u_long ahc_get_transfer_length(struct scb *scb)
381 return (scb->io_ctx->csio.dxfer_len);
385 int ahc_get_transfer_dir(struct scb *scb)
387 return (scb->io_ctx->ccb_h.flags & CAM_DIR_MASK);
391 void ahc_set_residual(struct scb *scb, u_long resid)
393 scb->io_ctx->csio.resid = resid;
397 void ahc_set_sense_residual(struct scb *scb, u_long resid)
399 scb->io_ctx->csio.sense_resid = resid;
403 u_long ahc_get_residual(struct scb *scb)
405 return (scb->io_ctx->csio.resid);
409 int ahc_perform_autosense(struct scb *scb)
411 return (!(scb->io_ctx->ccb_h.flags & CAM_DIS_AUTOSENSE));
414 static __inline uint32_t
415 ahc_get_sense_bufsize(struct ahc_softc *ahc, struct scb *scb)
417 return (sizeof(struct scsi_sense_data));
421 ahc_freeze_ccb(union ccb *ccb)
423 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
424 ccb->ccb_h.status |= CAM_DEV_QFRZN;
425 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
430 ahc_freeze_scb(struct scb *scb)
432 ahc_freeze_ccb(scb->io_ctx);
436 ahc_platform_freeze_devq(struct ahc_softc *ahc, struct scb *scb)
438 /* Nothing to do here for FreeBSD */
442 ahc_platform_abort_scbs(struct ahc_softc *ahc, int target,
443 char channel, int lun, u_int tag,
444 role_t role, uint32_t status)
446 /* Nothing to do here for FreeBSD */
451 ahc_platform_scb_free(struct ahc_softc *ahc, struct scb *scb)
453 /* What do we do to generically handle driver resource shortages??? */
454 if ((ahc->flags & AHC_RESOURCE_SHORTAGE) != 0
455 && scb->io_ctx != NULL
456 && (scb->io_ctx->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
457 scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
458 ahc->flags &= ~AHC_RESOURCE_SHORTAGE;
463 /********************************** PCI ***************************************/
464 #ifdef AHC_PCI_CONFIG
465 static __inline uint32_t ahc_pci_read_config(ahc_dev_softc_t pci,
467 static __inline void ahc_pci_write_config(ahc_dev_softc_t pci,
468 int reg, uint32_t value,
470 static __inline int ahc_get_pci_function(ahc_dev_softc_t);
471 static __inline int ahc_get_pci_slot(ahc_dev_softc_t);
472 static __inline int ahc_get_pci_bus(ahc_dev_softc_t);
474 int ahc_pci_map_registers(struct ahc_softc *ahc);
475 int ahc_pci_map_int(struct ahc_softc *ahc);
477 static __inline uint32_t
478 ahc_pci_read_config(ahc_dev_softc_t pci, int reg, int width)
480 return (pci_read_config(pci, reg, width));
484 ahc_pci_write_config(ahc_dev_softc_t pci, int reg, uint32_t value, int width)
486 pci_write_config(pci, reg, value, width);
490 ahc_get_pci_function(ahc_dev_softc_t pci)
492 return (pci_get_function(pci));
496 ahc_get_pci_slot(ahc_dev_softc_t pci)
498 return (pci_get_slot(pci));
502 ahc_get_pci_bus(ahc_dev_softc_t pci)
504 return (pci_get_bus(pci));
515 void ahc_power_state_change(struct ahc_softc *ahc,
516 ahc_power_state new_state);
518 /******************************** VL/EISA *************************************/
519 int aic7770_map_registers(struct ahc_softc *ahc, u_int port);
520 int aic7770_map_int(struct ahc_softc *ahc, int irq);
522 /********************************* Debug **************************************/
523 static __inline void ahc_print_path(struct ahc_softc *, struct scb *);
524 static __inline void ahc_platform_dump_card_state(struct ahc_softc *ahc);
527 ahc_print_path(struct ahc_softc *ahc, struct scb *scb)
529 xpt_print_path(scb->io_ctx->ccb_h.path);
533 ahc_platform_dump_card_state(struct ahc_softc *ahc)
535 /* Nothing to do here for FreeBSD */
537 /**************************** Transfer Settings *******************************/
538 void ahc_notify_xfer_settings_change(struct ahc_softc *,
539 struct ahc_devinfo *);
540 void ahc_platform_set_tags(struct ahc_softc *, struct ahc_devinfo *,
543 /************************* Initialization/Teardown ****************************/
544 int ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg);
545 void ahc_platform_free(struct ahc_softc *ahc);
546 int ahc_map_int(struct ahc_softc *ahc);
547 int ahc_attach(struct ahc_softc *);
548 int ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc);
549 int ahc_detach(device_t);
551 /****************************** Interrupts ************************************/
552 void ahc_platform_intr(void *);
553 static __inline void ahc_platform_flushwork(struct ahc_softc *ahc);
555 ahc_platform_flushwork(struct ahc_softc *ahc)
559 /************************ Misc Function Declarations **************************/
560 timeout_t ahc_timeout;
561 void ahc_done(struct ahc_softc *ahc, struct scb *scb);
562 void ahc_send_async(struct ahc_softc *, char /*channel*/,
563 u_int /*target*/, u_int /*lun*/, ac_code, void *arg);
564 #endif /* _AIC7XXX_FREEBSD_H_ */