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