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