aic79xx_osm.h revision 119277
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 * Copyright (c) 2001-2002 Adaptec Inc.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions, and the following disclaimer,
14 *    without modification.
15 * 2. The name of the author may not be used to endorse or promote products
16 *    derived from this software without specific prior written permission.
17 *
18 * Alternatively, this software may be distributed under the terms of the
19 * GNU Public License ("GPL").
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
25 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * $Id: aic79xx_osm.h,v 1.11 2003/07/01 15:51:52 scottl Exp $
34 *
35 * $FreeBSD: head/sys/dev/aic7xxx/aic79xx_osm.h 119277 2003-08-22 05:54:52Z imp $
36 */
37
38#ifndef _AIC79XX_FREEBSD_H_
39#define _AIC79XX_FREEBSD_H_
40
41#include <opt_aic79xx.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 __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
54#define AHD_PCI_CONFIG 1
55#include <machine/bus_memio.h>
56#include <machine/bus_pio.h>
57#include <machine/bus.h>
58#include <machine/endian.h>
59#include <machine/clock.h>
60#include <machine/resource.h>
61
62#include <sys/rman.h>
63
64#if __FreeBSD_version >= 500000
65#include <dev/pci/pcireg.h>
66#include <dev/pci/pcivar.h>
67#else
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#include <cam/scsi/scsi_iu.h>
81
82#ifdef CAM_NEW_TRAN_CODE
83#define AHD_NEW_TRAN_SETTINGS
84#endif /* CAM_NEW_TRAN_CODE */
85
86/****************************** Platform Macros *******************************/
87#define	SIM_IS_SCSIBUS_B(ahd, sim)	\
88	(0)
89#define	SIM_CHANNEL(ahd, sim)	\
90	('A')
91#define	SIM_SCSI_ID(ahd, sim)	\
92	(ahd->our_id)
93#define	SIM_PATH(ahd, sim)	\
94	(ahd->platform_data->path)
95#define BUILD_SCSIID(ahd, sim, target_id, our_id) \
96        ((((target_id) << TID_SHIFT) & TID) | (our_id))
97
98
99#define SCB_GET_SIM(ahd, scb) \
100	((ahd)->platform_data->sim)
101
102#ifndef offsetof
103#define offsetof(type, member)  ((size_t)(&((type *)0)->member))
104#endif
105/************************* Forward Declarations *******************************/
106typedef device_t ahd_dev_softc_t;
107typedef union ccb *ahd_io_ctx_t;
108
109/***************************** Bus Space/DMA **********************************/
110#define ahd_dma_tag_create(ahd, parent_tag, alignment, boundary,	\
111			   lowaddr, highaddr, filter, filterarg,	\
112			   maxsize, nsegments, maxsegsz, flags,		\
113			   dma_tagp)					\
114	bus_dma_tag_create(parent_tag, alignment, boundary,		\
115			   lowaddr, highaddr, filter, filterarg,	\
116			   maxsize, nsegments, maxsegsz, flags,		\
117			   busdma_lock_mutex, &Giant, dma_tagp)
118
119#define ahd_dma_tag_destroy(ahd, tag)					\
120	bus_dma_tag_destroy(tag)
121
122#define ahd_dmamem_alloc(ahd, dmat, vaddr, flags, mapp)			\
123	bus_dmamem_alloc(dmat, vaddr, flags, mapp)
124
125#define ahd_dmamem_free(ahd, dmat, vaddr, map)				\
126	bus_dmamem_free(dmat, vaddr, map)
127
128#define ahd_dmamap_create(ahd, tag, flags, mapp)			\
129	bus_dmamap_create(tag, flags, mapp)
130
131#define ahd_dmamap_destroy(ahd, tag, map)				\
132	bus_dmamap_destroy(tag, map)
133
134#define ahd_dmamap_load(ahd, dmat, map, addr, buflen, callback,		\
135			callback_arg, flags)				\
136	bus_dmamap_load(dmat, map, addr, buflen, callback, callback_arg, flags)
137
138#define ahd_dmamap_unload(ahd, tag, map)				\
139	bus_dmamap_unload(tag, map)
140
141/* XXX Need to update Bus DMA for partial map syncs */
142#define ahd_dmamap_sync(ahd, dma_tag, dmamap, offset, len, op)		\
143	bus_dmamap_sync(dma_tag, dmamap, op)
144
145/************************ Tunable Driver Parameters  **************************/
146/*
147 * The number of dma segments supported.  The sequencer can handle any number
148 * of physically contiguous S/G entrys.  To reduce the driver's memory
149 * consumption, we limit the number supported to be sufficient to handle
150 * the largest mapping supported by the kernel, MAXPHYS.  Assuming the
151 * transfer is as fragmented as possible and unaligned, this turns out to
152 * be the number of paged sized transfers in MAXPHYS plus an extra element
153 * to handle any unaligned residual.  The sequencer fetches SG elements
154 * in cacheline sized chucks, so make the number per-transaction an even
155 * multiple of 16 which should align us on even the largest of cacheline
156 * boundaries.
157 */
158#define AHD_NSEG (roundup(btoc(MAXPHYS) + 1, 16))
159
160/* This driver supports target mode */
161#if NOT_YET
162#define AHD_TARGET_MODE 1
163#endif
164
165/************************** Softc/SCB Platform Data ***************************/
166struct ahd_platform_data {
167	/*
168	 * Hooks into the XPT.
169	 */
170	struct	cam_sim		*sim;
171	struct	cam_sim		*sim_b;
172	struct	cam_path	*path;
173	struct	cam_path	*path_b;
174
175	int			 regs_res_type[2];
176	int			 regs_res_id[2];
177	int			 irq_res_type;
178	struct resource		*regs[2];
179	struct resource		*irq;
180	void			*ih;
181	eventhandler_tag	 eh;
182};
183
184struct scb_platform_data {
185};
186
187/********************************* Byte Order *********************************/
188#if __FreeBSD_version >= 500000
189#define ahd_htobe16(x) htobe16(x)
190#define ahd_htobe32(x) htobe32(x)
191#define ahd_htobe64(x) htobe64(x)
192#define ahd_htole16(x) htole16(x)
193#define ahd_htole32(x) htole32(x)
194#define ahd_htole64(x) htole64(x)
195
196#define ahd_be16toh(x) be16toh(x)
197#define ahd_be32toh(x) be32toh(x)
198#define ahd_be64toh(x) be64toh(x)
199#define ahd_le16toh(x) le16toh(x)
200#define ahd_le32toh(x) le32toh(x)
201#define ahd_le64toh(x) le64toh(x)
202#else
203#define ahd_htobe16(x) (x)
204#define ahd_htobe32(x) (x)
205#define ahd_htobe64(x) (x)
206#define ahd_htole16(x) (x)
207#define ahd_htole32(x) (x)
208#define ahd_htole64(x) (x)
209
210#define ahd_be16toh(x) (x)
211#define ahd_be32toh(x) (x)
212#define ahd_be64toh(x) (x)
213#define ahd_le16toh(x) (x)
214#define ahd_le32toh(x) (x)
215#define ahd_le64toh(x) (x)
216#endif
217
218/************************** Timer DataStructures ******************************/
219typedef struct callout ahd_timer_t;
220
221/***************************** Core Includes **********************************/
222#if AHD_REG_PRETTY_PRINT
223#define AIC_DEBUG_REGISTERS 1
224#else
225#define AIC_DEBUG_REGISTERS 0
226#endif
227#include <dev/aic7xxx/aic79xx.h>
228
229/***************************** Timer Facilities *******************************/
230timeout_t ahd_timeout;
231#if __FreeBSD_version >= 500000
232#define ahd_timer_init(timer) callout_init(timer, /*mpsafe*/0)
233#else
234#define ahd_timer_init callout_init
235#endif
236#define ahd_timer_stop callout_stop
237
238static __inline void
239ahd_timer_reset(ahd_timer_t *timer, u_int usec, ahd_callback_t *func, void *arg)
240{
241	callout_reset(timer, (usec * hz)/1000000, func, arg);
242}
243
244static __inline void
245ahd_scb_timer_reset(struct scb *scb, u_int usec)
246{
247	untimeout(ahd_timeout, (caddr_t)scb, scb->io_ctx->ccb_h.timeout_ch);
248	scb->io_ctx->ccb_h.timeout_ch =
249	    timeout(ahd_timeout, scb, (usec * hz)/1000000);
250}
251
252/*************************** Device Access ************************************/
253#define ahd_inb(ahd, port)					\
254	bus_space_read_1((ahd)->tags[(port) >> 8],		\
255			 (ahd)->bshs[(port) >> 8], (port) & 0xFF)
256
257#define ahd_outb(ahd, port, value)				\
258	bus_space_write_1((ahd)->tags[(port) >> 8],		\
259			  (ahd)->bshs[(port) >> 8], (port) & 0xFF, value)
260
261#define ahd_inw_atomic(ahd, port)				\
262	ahd_le16toh(bus_space_read_2((ahd)->tags[(port) >> 8],	\
263				     (ahd)->bshs[(port) >> 8], (port) & 0xFF))
264
265#define ahd_outw_atomic(ahd, port, value)			\
266	bus_space_write_2((ahd)->tags[(port) >> 8],		\
267			  (ahd)->bshs[(port) >> 8],		\
268			  (port & 0xFF), ahd_htole16(value))
269
270#define ahd_outsb(ahd, port, valp, count)			\
271	bus_space_write_multi_1((ahd)->tags[(port) >> 8],	\
272				(ahd)->bshs[(port) >> 8],	\
273				(port & 0xFF), valp, count)
274
275#define ahd_insb(ahd, port, valp, count)			\
276	bus_space_read_multi_1((ahd)->tags[(port) >> 8],	\
277			       (ahd)->bshs[(port) >> 8],	\
278			       (port & 0xFF), valp, count)
279
280static __inline void ahd_flush_device_writes(struct ahd_softc *);
281
282static __inline void
283ahd_flush_device_writes(struct ahd_softc *ahd)
284{
285	/* XXX Is this sufficient for all architectures??? */
286	ahd_inb(ahd, INTSTAT);
287}
288
289/**************************** Locking Primitives ******************************/
290/* Lock protecting internal data structures */
291static __inline void ahd_lockinit(struct ahd_softc *);
292static __inline void ahd_lock(struct ahd_softc *, unsigned long *flags);
293static __inline void ahd_unlock(struct ahd_softc *, unsigned long *flags);
294
295/* Lock held during command compeletion to the upper layer */
296static __inline void ahd_done_lockinit(struct ahd_softc *);
297static __inline void ahd_done_lock(struct ahd_softc *, unsigned long *flags);
298static __inline void ahd_done_unlock(struct ahd_softc *, unsigned long *flags);
299
300/* Lock held during ahd_list manipulation and ahd softc frees */
301static __inline void ahd_list_lockinit(void);
302static __inline void ahd_list_lock(unsigned long *flags);
303static __inline void ahd_list_unlock(unsigned long *flags);
304
305static __inline void
306ahd_lockinit(struct ahd_softc *ahd)
307{
308}
309
310static __inline void
311ahd_lock(struct ahd_softc *ahd, unsigned long *flags)
312{
313	*flags = splcam();
314}
315
316static __inline void
317ahd_unlock(struct ahd_softc *ahd, unsigned long *flags)
318{
319	splx(*flags);
320}
321
322/* Lock held during command compeletion to the upper layer */
323static __inline void
324ahd_done_lockinit(struct ahd_softc *ahd)
325{
326}
327
328static __inline void
329ahd_done_lock(struct ahd_softc *ahd, unsigned long *flags)
330{
331}
332
333static __inline void
334ahd_done_unlock(struct ahd_softc *ahd, unsigned long *flags)
335{
336}
337
338/* Lock held during ahd_list manipulation and ahd softc frees */
339static __inline void
340ahd_list_lockinit(void)
341{
342}
343
344static __inline void
345ahd_list_lock(unsigned long *flags)
346{
347}
348
349static __inline void
350ahd_list_unlock(unsigned long *flags)
351{
352}
353/****************************** OS Primitives *********************************/
354#define ahd_delay DELAY
355
356/************************** Transaction Operations ****************************/
357static __inline void ahd_set_transaction_status(struct scb *, uint32_t);
358static __inline void ahd_set_scsi_status(struct scb *, uint32_t);
359static __inline uint32_t ahd_get_transaction_status(struct scb *);
360static __inline uint32_t ahd_get_scsi_status(struct scb *);
361static __inline void ahd_set_transaction_tag(struct scb *, int, u_int);
362static __inline u_long ahd_get_transfer_length(struct scb *);
363static __inline int ahd_get_transfer_dir(struct scb *);
364static __inline void ahd_set_residual(struct scb *, u_long);
365static __inline void ahd_set_sense_residual(struct scb *, u_long);
366static __inline u_long ahd_get_residual(struct scb *);
367static __inline int ahd_perform_autosense(struct scb *);
368static __inline uint32_t ahd_get_sense_bufsize(struct ahd_softc*, struct scb*);
369static __inline void ahd_freeze_simq(struct ahd_softc *);
370static __inline void ahd_release_simq(struct ahd_softc *);
371static __inline void ahd_freeze_ccb(union ccb *ccb);
372static __inline void ahd_freeze_scb(struct scb *scb);
373static __inline void ahd_platform_freeze_devq(struct ahd_softc *, struct scb *);
374static __inline int  ahd_platform_abort_scbs(struct ahd_softc *ahd, int target,
375					     char channel, int lun, u_int tag,
376					     role_t role, uint32_t status);
377
378static __inline
379void ahd_set_transaction_status(struct scb *scb, uint32_t status)
380{
381	scb->io_ctx->ccb_h.status &= ~CAM_STATUS_MASK;
382	scb->io_ctx->ccb_h.status |= status;
383}
384
385static __inline
386void ahd_set_scsi_status(struct scb *scb, uint32_t status)
387{
388	scb->io_ctx->csio.scsi_status = status;
389}
390
391static __inline
392uint32_t ahd_get_transaction_status(struct scb *scb)
393{
394	return (scb->io_ctx->ccb_h.status & CAM_STATUS_MASK);
395}
396
397static __inline
398uint32_t ahd_get_scsi_status(struct scb *scb)
399{
400	return (scb->io_ctx->csio.scsi_status);
401}
402
403static __inline
404void ahd_set_transaction_tag(struct scb *scb, int enabled, u_int type)
405{
406	scb->io_ctx->csio.tag_action = type;
407	if (enabled)
408		scb->io_ctx->ccb_h.flags |= CAM_TAG_ACTION_VALID;
409	else
410		scb->io_ctx->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
411}
412
413static __inline
414u_long ahd_get_transfer_length(struct scb *scb)
415{
416	return (scb->io_ctx->csio.dxfer_len);
417}
418
419static __inline
420int ahd_get_transfer_dir(struct scb *scb)
421{
422	return (scb->io_ctx->ccb_h.flags & CAM_DIR_MASK);
423}
424
425static __inline
426void ahd_set_residual(struct scb *scb, u_long resid)
427{
428	scb->io_ctx->csio.resid = resid;
429}
430
431static __inline
432void ahd_set_sense_residual(struct scb *scb, u_long resid)
433{
434	scb->io_ctx->csio.sense_resid = resid;
435}
436
437static __inline
438u_long ahd_get_residual(struct scb *scb)
439{
440	return (scb->io_ctx->csio.resid);
441}
442
443static __inline
444int ahd_perform_autosense(struct scb *scb)
445{
446	return (!(scb->io_ctx->ccb_h.flags & CAM_DIS_AUTOSENSE));
447}
448
449static __inline uint32_t
450ahd_get_sense_bufsize(struct ahd_softc *ahd, struct scb *scb)
451{
452	return (sizeof(struct scsi_sense_data));
453}
454
455static __inline void
456ahd_freeze_simq(struct ahd_softc *ahd)
457{
458	xpt_freeze_simq(ahd->platform_data->sim, /*count*/1);
459}
460
461static __inline void
462ahd_release_simq(struct ahd_softc *ahd)
463{
464	xpt_release_simq(ahd->platform_data->sim, /*run queue*/TRUE);
465}
466
467static __inline void
468ahd_freeze_ccb(union ccb *ccb)
469{
470	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
471		ccb->ccb_h.status |= CAM_DEV_QFRZN;
472		xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
473	}
474}
475
476static __inline void
477ahd_freeze_scb(struct scb *scb)
478{
479	ahd_freeze_ccb(scb->io_ctx);
480}
481
482static __inline void
483ahd_platform_freeze_devq(struct ahd_softc *ahd, struct scb *scb)
484{
485	/* Nothing to do here for FreeBSD */
486}
487
488static __inline int
489ahd_platform_abort_scbs(struct ahd_softc *ahd, int target,
490			char channel, int lun, u_int tag,
491			role_t role, uint32_t status)
492{
493	/* Nothing to do here for FreeBSD */
494	return (0);
495}
496
497static __inline void
498ahd_platform_scb_free(struct ahd_softc *ahd, struct scb *scb)
499{
500	/* What do we do to generically handle driver resource shortages??? */
501	if ((ahd->flags & AHD_RESOURCE_SHORTAGE) != 0
502	 && scb->io_ctx != NULL
503	 && (scb->io_ctx->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
504		scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
505		ahd->flags &= ~AHD_RESOURCE_SHORTAGE;
506	}
507	scb->io_ctx = NULL;
508}
509
510/********************************** PCI ***************************************/
511#ifdef AHD_PCI_CONFIG
512static __inline uint32_t ahd_pci_read_config(ahd_dev_softc_t pci,
513					     int reg, int width);
514static __inline void	 ahd_pci_write_config(ahd_dev_softc_t pci,
515					      int reg, uint32_t value,
516					      int width);
517static __inline int	 ahd_get_pci_function(ahd_dev_softc_t);
518static __inline int	 ahd_get_pci_slot(ahd_dev_softc_t);
519static __inline int	 ahd_get_pci_bus(ahd_dev_softc_t);
520
521int			 ahd_pci_map_registers(struct ahd_softc *ahd);
522int			 ahd_pci_map_int(struct ahd_softc *ahd);
523
524static __inline uint32_t
525ahd_pci_read_config(ahd_dev_softc_t pci, int reg, int width)
526{
527	return (pci_read_config(pci, reg, width));
528}
529
530static __inline void
531ahd_pci_write_config(ahd_dev_softc_t pci, int reg, uint32_t value, int width)
532{
533	pci_write_config(pci, reg, value, width);
534}
535
536static __inline int
537ahd_get_pci_function(ahd_dev_softc_t pci)
538{
539	return (pci_get_function(pci));
540}
541
542static __inline int
543ahd_get_pci_slot(ahd_dev_softc_t pci)
544{
545	return (pci_get_slot(pci));
546}
547
548static __inline int
549ahd_get_pci_bus(ahd_dev_softc_t pci)
550{
551	return (pci_get_bus(pci));
552}
553
554typedef enum
555{
556	AHD_POWER_STATE_D0,
557	AHD_POWER_STATE_D1,
558	AHD_POWER_STATE_D2,
559	AHD_POWER_STATE_D3
560} ahd_power_state;
561
562void ahd_power_state_change(struct ahd_softc *ahd,
563			    ahd_power_state new_state);
564#endif
565/******************************** VL/EISA *************************************/
566int aic7770_map_registers(struct ahd_softc *ahd);
567int aic7770_map_int(struct ahd_softc *ahd, int irq);
568
569/********************************* Debug **************************************/
570static __inline void	ahd_print_path(struct ahd_softc *, struct scb *);
571static __inline void	ahd_platform_dump_card_state(struct ahd_softc *ahd);
572
573static __inline void
574ahd_print_path(struct ahd_softc *ahd, struct scb *scb)
575{
576	xpt_print_path(scb->io_ctx->ccb_h.path);
577}
578
579static __inline void
580ahd_platform_dump_card_state(struct ahd_softc *ahd)
581{
582	/* Nothing to do here for FreeBSD */
583}
584/**************************** Transfer Settings *******************************/
585void	  ahd_notify_xfer_settings_change(struct ahd_softc *,
586					  struct ahd_devinfo *);
587void	  ahd_platform_set_tags(struct ahd_softc *, struct ahd_devinfo *,
588				int /*enable*/);
589
590/************************* Initialization/Teardown ****************************/
591int	  ahd_platform_alloc(struct ahd_softc *ahd, void *platform_arg);
592void	  ahd_platform_free(struct ahd_softc *ahd);
593int	  ahd_map_int(struct ahd_softc *ahd);
594int	  ahd_attach(struct ahd_softc *);
595int	  ahd_softc_comp(struct ahd_softc *lahd, struct ahd_softc *rahd);
596int	  ahd_detach(device_t);
597#define	ahd_platform_init(arg)
598
599
600/****************************** Interrupts ************************************/
601void			ahd_platform_intr(void *);
602static __inline void	ahd_platform_flushwork(struct ahd_softc *ahd);
603static __inline void
604ahd_platform_flushwork(struct ahd_softc *ahd)
605{
606}
607
608/************************ Misc Function Declarations **************************/
609void	  ahd_done(struct ahd_softc *ahd, struct scb *scb);
610void	  ahd_send_async(struct ahd_softc *, char /*channel*/,
611			 u_int /*target*/, u_int /*lun*/, ac_code, void *arg);
612#endif  /* _AIC79XX_FREEBSD_H_ */
613