1 /* 2 * FreeBSD platform specific driver option settings, data structures, 3 * function declarations and includes. 4 * 5 * Copyright (c) 1994, 1995, 1996, 1997, 1998, 1999, 2000 Justin T. Gibbs. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 16 * 17 * Alternatively, this software may be distributed under the terms of the 18 * GNU Public License ("GPL"). 19 * 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 30 * SUCH DAMAGE. 31 * 32 * $Id$ 33 * 34 * $FreeBSD$ 35 */ 36 37 #ifndef _AIC7XXX_FREEBSD_H_ 38 #define _AIC7XXX_FREEBSD_H_ 39 40 #include <opt_aic7xxx.h> /* for config options */ 41 #include <pci.h> /* for NPCI */ 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/bus.h> /* For device_t */ 46 #include <sys/kernel.h> 47 #include <sys/malloc.h> 48 #include <sys/queue.h> 49 50 #if NPCI > 0 51 #define AHC_SUPPORT_PCI 1 52 #ifdef AHC_ALLOW_MEMIO 53 #include <machine/bus_memio.h> 54 #endif 55 #endif 56 #include <machine/bus_pio.h> 57 #include <machine/bus.h> 58 #include <machine/clock.h> 59 #include <machine/resource.h> 60 61 #include <sys/rman.h> 62 63 #if NPCI > 0 64 #include <pci/pcireg.h> 65 #include <pci/pcivar.h> 66 #endif 67 68 #include <cam/cam.h> 69 #include <cam/cam_ccb.h> 70 #include <cam/cam_debug.h> 71 #include <cam/cam_sim.h> 72 #include <cam/cam_xpt_sim.h> 73 74 #include <cam/scsi/scsi_all.h> 75 #include <cam/scsi/scsi_message.h> 76 77 /****************************** Platform Macros *******************************/ 78 #define SIM_IS_SCSIBUS_B(ahc, sim) \ 79 ((sim) == ahc->platform_data->sim_b) 80 #define SIM_CHANNEL(ahc, sim) \ 81 (((sim) == ahc->platform_data->sim_b) ? 'B' : 'A') 82 #define SIM_SCSI_ID(ahc, sim) \ 83 (((sim) == ahc->platform_data->sim_b) ? ahc->our_id_b : ahc->our_id) 84 #define SIM_PATH(ahc, sim) \ 85 (((sim) == ahc->platform_data->sim_b) ? ahc->platform_data->path_b \ 86 : ahc->platform_data->path) 87 #define BUILD_SCSIID(ahc, sim, target_id, our_id) \ 88 ((((target_id) << TID_SHIFT) & TID) | (our_id) \ 89 | (SIM_IS_SCSIBUS_B(ahc, sim) ? TWIN_CHNLB : 0)) 90 91 #define SCB_GET_SIM(ahc, scb) \ 92 (SCB_GET_CHANNEL(ahc, scb) == 'A' ? (ahc)->platform_data->sim \ 93 : (ahc)->platform_data->sim_b) 94 95 /************************* Forward Declarations *******************************/ 96 typedef device_t ahc_dev_softc_t; 97 typedef union ccb *ahc_io_ctx_t; 98 99 /***************************** Bus Space/DMA **********************************/ 100 #define ahc_dma_tag_create(ahc, parent_tag, alignment, boundary, \ 101 lowaddr, highaddr, filter, filterarg, \ 102 maxsize, nsegments, maxsegsz, flags, \ 103 dma_tagp) \ 104 bus_dma_tag_create(parent_tag, alignment, boundary, \ 105 lowaddr, highaddr, filter, filterarg, \ 106 maxsize, nsegments, maxsegsz, flags, \ 107 dma_tagp) 108 109 #define ahc_dma_tag_destroy(ahc, tag) \ 110 bus_dma_tag_destroy(tag) 111 112 #define ahc_dmamem_alloc(ahc, dmat, vaddr, flags, mapp) \ 113 bus_dmamem_alloc(dmat, vaddr, flags, mapp) 114 115 #define ahc_dmamem_free(ahc, dmat, vaddr, map) \ 116 bus_dmamem_free(dmat, vaddr, map) 117 118 #define ahc_dmamap_create(ahc, tag, flags, mapp) \ 119 bus_dmamap_create(tag, flags, mapp) 120 121 #define ahc_dmamap_destroy(ahc, tag, map) \ 122 bus_dmamap_destroy(tag, map) 123 124 #define ahc_dmamap_load(ahc, dmat, map, addr, buflen, callback, \ 125 callback_arg, flags) \ 126 bus_dmamap_load(dmat, map, addr, buflen, callback, callback_arg, flags) 127 128 #define ahc_dmamap_unload(ahc, tag, map) \ 129 bus_dmamap_unload(tag, map) 130 131 #define ahc_dmamap_sync(ahc, dma_tag, dmamap, op) \ 132 bus_dmamap_sync(dma_tag_dmamap, op) 133 134 /************************ Tunable Driver Parameters **************************/ 135 /* 136 * The number of dma segments supported. The sequencer can handle any number 137 * of physically contiguous S/G entrys. To reduce the driver's memory 138 * consumption, we limit the number supported to be sufficient to handle 139 * the largest mapping supported by the kernel, MAXPHYS. Assuming the 140 * transfer is as fragmented as possible and unaligned, this turns out to 141 * be the number of paged sized transfers in MAXPHYS plus an extra element 142 * to handle any unaligned residual. The sequencer fetches SG elements 143 * in 128 byte chucks, so make the number per-transaction a nice multiple 144 * of 16 (8 byte S/G elements). 145 */ 146 /* XXX Worth the space??? */ 147 #define AHC_NSEG (roundup(btoc(MAXPHYS) + 1, 16)) 148 149 /* This driver supports target mode */ 150 #define AHC_TARGET_MODE 1 151 152 /************************** Softc/SCB Platform Data ***************************/ 153 struct ahc_platform_data { 154 /* 155 * Hooks into the XPT. 156 */ 157 struct cam_sim *sim; 158 struct cam_sim *sim_b; 159 struct cam_path *path; 160 struct cam_path *path_b; 161 162 int regs_res_type; 163 int regs_res_id; 164 int irq_res_type; 165 struct resource *regs; 166 struct resource *irq; 167 void *ih; 168 }; 169 170 struct scb_platform_data { 171 }; 172 173 /***************************** Core Includes **********************************/ 174 #include <dev/aic7xxx/aic7xxx.h> 175 176 /*************************** Device Access ************************************/ 177 #define ahc_inb(ahc, port) \ 178 bus_space_read_1((ahc)->tag, (ahc)->bsh, port) 179 180 #define ahc_outb(ahc, port, value) \ 181 bus_space_write_1((ahc)->tag, (ahc)->bsh, port, value) 182 183 #define ahc_outsb(ahc, port, valp, count) \ 184 bus_space_write_multi_1((ahc)->tag, (ahc)->bsh, port, valp, count) 185 186 #define ahc_insb(ahc, port, valp, count) \ 187 bus_space_read_multi_1((ahc)->tag, (ahc)->bsh, port, valp, count) 188 189 static __inline void ahc_flush_device_writes(struct ahc_softc *); 190 191 static __inline void 192 ahc_flush_device_writes(struct ahc_softc *ahc) 193 { 194 /* XXX Is this sufficient for all architectures??? */ 195 ahc_inb(ahc, INTSTAT); 196 } 197 198 /**************************** Locking Primitives ******************************/ 199 /* Lock protecting internal data structures */ 200 static __inline void ahc_lockinit(struct ahc_softc *); 201 static __inline void ahc_lock(struct ahc_softc *, unsigned long *flags); 202 static __inline void ahc_unlock(struct ahc_softc *, unsigned long *flags); 203 204 /* Lock held during command compeletion to the upper layer */ 205 static __inline void ahc_done_lockinit(struct ahc_softc *); 206 static __inline void ahc_done_lock(struct ahc_softc *, unsigned long *flags); 207 static __inline void ahc_done_unlock(struct ahc_softc *, unsigned long *flags); 208 209 static __inline void 210 ahc_lockinit(struct ahc_softc *ahc) 211 { 212 } 213 214 static __inline void 215 ahc_lock(struct ahc_softc *ahc, unsigned long *flags) 216 { 217 *flags = splcam(); 218 } 219 220 static __inline void 221 ahc_unlock(struct ahc_softc *ahc, unsigned long *flags) 222 { 223 splx(*flags); 224 } 225 226 /* Lock held during command compeletion to the upper layer */ 227 static __inline void 228 ahc_done_lockinit(struct ahc_softc *ahc) 229 { 230 } 231 232 static __inline void 233 ahc_done_lock(struct ahc_softc *ahc, unsigned long *flags) 234 { 235 } 236 237 static __inline void 238 ahc_done_unlock(struct ahc_softc *ahc, unsigned long *flags) 239 { 240 } 241 242 /****************************** OS Primitives *********************************/ 243 #define ahc_delay DELAY 244 245 /************************** Transaction Operations ****************************/ 246 static __inline void ahc_set_transaction_status(struct scb *, uint32_t); 247 static __inline void ahc_set_scsi_status(struct scb *, uint32_t); 248 static __inline uint32_t ahc_get_transaction_status(struct scb *); 249 static __inline uint32_t ahc_get_scsi_status(struct scb *); 250 static __inline void ahc_set_transaction_tag(struct scb *, int, u_int); 251 static __inline u_long ahc_get_transfer_length(struct scb *); 252 static __inline int ahc_get_transfer_dir(struct scb *); 253 static __inline void ahc_set_residual(struct scb *, u_long); 254 static __inline void ahc_set_sense_residual(struct scb *, u_long); 255 static __inline u_long ahc_get_residual(struct scb *); 256 static __inline int ahc_perform_autosense(struct scb *); 257 static __inline uint32_t ahc_get_sense_bufsize(struct ahc_softc*, struct scb*); 258 static __inline void ahc_freeze_ccb(union ccb *ccb); 259 static __inline void ahc_freeze_scb(struct scb *scb); 260 static __inline void ahc_platform_freeze_devq(struct ahc_softc *, struct scb *); 261 static __inline int ahc_platform_abort_scbs(struct ahc_softc *ahc, int target, 262 char channel, int lun, u_int tag, 263 role_t role, uint32_t status); 264 265 static __inline 266 void ahc_set_transaction_status(struct scb *scb, uint32_t status) 267 { 268 scb->io_ctx->ccb_h.status &= ~CAM_STATUS_MASK; 269 scb->io_ctx->ccb_h.status |= status; 270 } 271 272 static __inline 273 void ahc_set_scsi_status(struct scb *scb, uint32_t status) 274 { 275 scb->io_ctx->csio.scsi_status = status; 276 } 277 278 static __inline 279 uint32_t ahc_get_transaction_status(struct scb *scb) 280 { 281 return (scb->io_ctx->ccb_h.status & CAM_STATUS_MASK); 282 } 283 284 static __inline 285 uint32_t ahc_get_scsi_status(struct scb *scb) 286 { 287 return (scb->io_ctx->csio.scsi_status); 288 } 289 290 static __inline 291 void ahc_set_transaction_tag(struct scb *scb, int enabled, u_int type) 292 { 293 scb->io_ctx->csio.tag_action = type; 294 if (enabled) 295 scb->io_ctx->ccb_h.flags |= CAM_TAG_ACTION_VALID; 296 else 297 scb->io_ctx->ccb_h.flags &= ~CAM_TAG_ACTION_VALID; 298 } 299 300 static __inline 301 u_long ahc_get_transfer_length(struct scb *scb) 302 { 303 return (scb->io_ctx->csio.dxfer_len); 304 } 305 306 static __inline 307 int ahc_get_transfer_dir(struct scb *scb) 308 { 309 return (scb->io_ctx->ccb_h.flags & CAM_DIR_MASK); 310 } 311 312 static __inline 313 void ahc_set_residual(struct scb *scb, u_long resid) 314 { 315 scb->io_ctx->csio.resid = resid; 316 } 317 318 static __inline 319 void ahc_set_sense_residual(struct scb *scb, u_long resid) 320 { 321 scb->io_ctx->csio.sense_resid = resid; 322 } 323 324 static __inline 325 u_long ahc_get_residual(struct scb *scb) 326 { 327 return (scb->io_ctx->csio.resid); 328 } 329 330 static __inline 331 int ahc_perform_autosense(struct scb *scb) 332 { 333 return (!(scb->io_ctx->ccb_h.flags & CAM_DIS_AUTOSENSE)); 334 } 335 336 static __inline uint32_t 337 ahc_get_sense_bufsize(struct ahc_softc *ahc, struct scb *scb) 338 { 339 return (sizeof(struct scsi_sense_data)); 340 } 341 342 static __inline void 343 ahc_freeze_ccb(union ccb *ccb) 344 { 345 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) { 346 ccb->ccb_h.status |= CAM_DEV_QFRZN; 347 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1); 348 } 349 } 350 351 static __inline void 352 ahc_freeze_scb(struct scb *scb) 353 { 354 ahc_freeze_ccb(scb->io_ctx); 355 } 356 357 static __inline void 358 ahc_platform_freeze_devq(struct ahc_softc *ahc, struct scb *scb) 359 { 360 /* Nothing to do here for FreeBSD */ 361 } 362 363 static __inline int 364 ahc_platform_abort_scbs(struct ahc_softc *ahc, int target, 365 char channel, int lun, u_int tag, 366 role_t role, uint32_t status) 367 { 368 /* Nothing to do here for FreeBSD */ 369 return (0); 370 } 371 372 static __inline void 373 ahc_platform_scb_free(struct ahc_softc *ahc, struct scb *scb) 374 { 375 /* What do we do to generically handle driver resource shortages??? */ 376 if ((ahc->flags & AHC_RESOURCE_SHORTAGE) != 0 377 && scb->io_ctx != NULL 378 && (scb->io_ctx->ccb_h.status & CAM_RELEASE_SIMQ) == 0) { 379 scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ; 380 ahc->flags &= ~AHC_RESOURCE_SHORTAGE; 381 } 382 scb->io_ctx = NULL; 383 } 384 385 /********************************** PCI ***************************************/ 386 #ifdef AHC_SUPPORT_PCI 387 static __inline uint32_t ahc_pci_read_config(ahc_dev_softc_t pci, 388 int reg, int width); 389 static __inline void ahc_pci_write_config(ahc_dev_softc_t pci, 390 int reg, uint32_t value, 391 int width); 392 static __inline int ahc_get_pci_function(ahc_dev_softc_t); 393 static __inline int ahc_get_pci_slot(ahc_dev_softc_t); 394 static __inline int ahc_get_pci_bus(ahc_dev_softc_t); 395 396 int ahc_pci_map_registers(struct ahc_softc *ahc); 397 int ahc_pci_map_int(struct ahc_softc *ahc); 398 399 static __inline uint32_t 400 ahc_pci_read_config(ahc_dev_softc_t pci, int reg, int width) 401 { 402 return (pci_read_config(pci, reg, width)); 403 } 404 405 static __inline void 406 ahc_pci_write_config(ahc_dev_softc_t pci, int reg, uint32_t value, int width) 407 { 408 pci_write_config(pci, reg, value, width); 409 } 410 411 static __inline int 412 ahc_get_pci_function(ahc_dev_softc_t pci) 413 { 414 return (pci_get_function(pci)); 415 } 416 417 static __inline int 418 ahc_get_pci_slot(ahc_dev_softc_t pci) 419 { 420 return (pci_get_slot(pci)); 421 } 422 423 static __inline int 424 ahc_get_pci_bus(ahc_dev_softc_t pci) 425 { 426 return (pci_get_bus(pci)); 427 } 428 #endif 429 /******************************** VL/EISA *************************************/ 430 int aic7770_map_registers(struct ahc_softc *ahc); 431 int aic7770_map_int(struct ahc_softc *ahc); 432 433 /********************************* Debug **************************************/ 434 static __inline void ahc_print_path(struct ahc_softc *, struct scb *); 435 static __inline void ahc_platform_dump_card_state(struct ahc_softc *ahc); 436 437 static __inline void 438 ahc_print_path(struct ahc_softc *ahc, struct scb *scb) 439 { 440 xpt_print_path(scb->io_ctx->ccb_h.path); 441 } 442 443 static __inline void 444 ahc_platform_dump_card_state(struct ahc_softc *ahc) 445 { 446 /* Nothing to do here for FreeBSD */ 447 } 448 /**************************** Transfer Settings *******************************/ 449 void ahc_notify_xfer_settings_change(struct ahc_softc *, 450 struct ahc_devinfo *); 451 void ahc_platform_set_tags(struct ahc_softc *, struct ahc_devinfo *, 452 int /*enable*/); 453 454 /***************************** Initialization *********************************/ 455 int ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg); 456 void ahc_platform_free(struct ahc_softc *ahc); 457 int ahc_attach(struct ahc_softc *); 458 int ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc); 459 460 /************************ Misc Function Declarations **************************/ 461 timeout_t ahc_timeout; 462 void ahc_done(struct ahc_softc *ahc, struct scb *scb); 463 void ahc_send_async(struct ahc_softc *, char /*channel*/, 464 u_int /*target*/, u_int /*lun*/, ac_code); 465 #endif /* _AIC7XXX_FREEBSD_H_ */ 466