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