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