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