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