xref: /freebsd/sys/dev/aic7xxx/aic79xx_osm.h (revision 09e8dea79366f1e5b3a73e8a271b26e4b6bf2e6a)
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 #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 #define AHD_PCI_CONFIG 1
54 #include <machine/bus_memio.h>
55 #include <machine/bus_pio.h>
56 #include <machine/bus.h>
57 #include <machine/endian.h>
58 #include <machine/clock.h>
59 #include <machine/resource.h>
60 
61 #include <sys/rman.h>
62 
63 #if NPCI > 0
64 #include <pci/pcireg.h>
65 #include <pci/pcivar.h>
66 #endif
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 *******************************/
102 typedef device_t ahd_dev_softc_t;
103 typedef 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 ***************************/
162 struct 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 
180 struct scb_platform_data {
181 };
182 
183 /********************************* Byte Order *********************************/
184 /*
185  * XXX Waiting for FreeBSD byte swapping functions.
186  * For now assume host is Little Endian.
187  */
188 #define ahd_htobe16(x) x
189 #define ahd_htobe32(x) x
190 #define ahd_htobe64(x) x
191 #define ahd_htole16(x) x
192 #define ahd_htole32(x) x
193 #define ahd_htole64(x) x
194 
195 #define ahd_be16toh(x) x
196 #define ahd_be32toh(x) x
197 #define ahd_be64toh(x) x
198 #define ahd_le16toh(x) x
199 #define ahd_le32toh(x) x
200 #define ahd_le64toh(x) x
201 
202 /************************** Timer DataStructures ******************************/
203 typedef struct callout ahd_timer_t;
204 
205 /***************************** Core Includes **********************************/
206 #include <dev/aic7xxx/aic79xx.h>
207 
208 /***************************** Timer Facilities *******************************/
209 #if __FreeBSD_version >= 500000
210 #define ahd_timer_init(timer) callout_init(timer, /*mpsafe*/0)
211 #else
212 #define ahd_timer_init callout_init
213 #endif
214 #define ahd_timer_stop callout_stop
215 
216 static __inline void
217 ahd_timer_reset(ahd_timer_t *timer, int usec, ahd_callback_t *func, void *arg)
218 {
219 	callout_reset(timer, (usec * hz)/1000000, func, arg);
220 }
221 
222 /*************************** Device Access ************************************/
223 #define ahd_inb(ahd, port)					\
224 	bus_space_read_1((ahd)->tags[(port) >> 8],		\
225 			 (ahd)->bshs[(port) >> 8], (port) & 0xFF)
226 
227 #define ahd_outb(ahd, port, value)				\
228 	bus_space_write_1((ahd)->tags[(port) >> 8],		\
229 			  (ahd)->bshs[(port) >> 8], (port) & 0xFF, value)
230 
231 #define ahd_inw_atomic(ahd, port)				\
232 	ahd_le16toh(bus_space_read_2((ahd)->tags[(port) >> 8],	\
233 				     (ahd)->bshs[(port) >> 8], (port) & 0xFF))
234 
235 #define ahd_outw_atomic(ahd, port, value)			\
236 	bus_space_write_2((ahd)->tags[(port) >> 8],		\
237 			  (ahd)->bshs[(port) >> 8],		\
238 			  (port & 0xFF), ahd_htole16(value))
239 
240 #define ahd_outsb(ahd, port, valp, count)			\
241 	bus_space_write_multi_1((ahd)->tags[(port) >> 8],	\
242 				(ahd)->bshs[(port) >> 8],	\
243 				(port & 0xFF), valp, count)
244 
245 #define ahd_insb(ahd, port, valp, count)			\
246 	bus_space_read_multi_1((ahd)->tags[(port) >> 8],	\
247 			       (ahd)->bshs[(port) >> 8],	\
248 			       (port & 0xFF), valp, count)
249 
250 static __inline void ahd_flush_device_writes(struct ahd_softc *);
251 
252 static __inline void
253 ahd_flush_device_writes(struct ahd_softc *ahd)
254 {
255 	/* XXX Is this sufficient for all architectures??? */
256 	ahd_inb(ahd, INTSTAT);
257 }
258 
259 /**************************** Locking Primitives ******************************/
260 /* Lock protecting internal data structures */
261 static __inline void ahd_lockinit(struct ahd_softc *);
262 static __inline void ahd_lock(struct ahd_softc *, unsigned long *flags);
263 static __inline void ahd_unlock(struct ahd_softc *, unsigned long *flags);
264 
265 /* Lock held during command compeletion to the upper layer */
266 static __inline void ahd_done_lockinit(struct ahd_softc *);
267 static __inline void ahd_done_lock(struct ahd_softc *, unsigned long *flags);
268 static __inline void ahd_done_unlock(struct ahd_softc *, unsigned long *flags);
269 
270 /* Lock held during ahc_list manipulation and ahc softc frees */
271 static __inline void ahd_list_lockinit(void);
272 static __inline void ahd_list_lock(unsigned long *flags);
273 static __inline void ahd_list_unlock(unsigned long *flags);
274 
275 static __inline void
276 ahd_lockinit(struct ahd_softc *ahd)
277 {
278 }
279 
280 static __inline void
281 ahd_lock(struct ahd_softc *ahd, unsigned long *flags)
282 {
283 	*flags = splcam();
284 }
285 
286 static __inline void
287 ahd_unlock(struct ahd_softc *ahd, unsigned long *flags)
288 {
289 	splx(*flags);
290 }
291 
292 /* Lock held during command compeletion to the upper layer */
293 static __inline void
294 ahd_done_lockinit(struct ahd_softc *ahd)
295 {
296 }
297 
298 static __inline void
299 ahd_done_lock(struct ahd_softc *ahd, unsigned long *flags)
300 {
301 }
302 
303 static __inline void
304 ahd_done_unlock(struct ahd_softc *ahd, unsigned long *flags)
305 {
306 }
307 
308 /* Lock held during ahc_list manipulation and ahc softc frees */
309 static __inline void
310 ahd_list_lockinit()
311 {
312 }
313 
314 static __inline void
315 ahd_list_lock(unsigned long *flags)
316 {
317 }
318 
319 static __inline void
320 ahd_list_unlock(unsigned long *flags)
321 {
322 }
323 /****************************** OS Primitives *********************************/
324 #define ahd_delay DELAY
325 
326 /************************** Transaction Operations ****************************/
327 static __inline void ahd_set_transaction_status(struct scb *, uint32_t);
328 static __inline void ahd_set_scsi_status(struct scb *, uint32_t);
329 static __inline uint32_t ahd_get_transaction_status(struct scb *);
330 static __inline uint32_t ahd_get_scsi_status(struct scb *);
331 static __inline void ahd_set_transaction_tag(struct scb *, int, u_int);
332 static __inline u_long ahd_get_transfer_length(struct scb *);
333 static __inline int ahd_get_transfer_dir(struct scb *);
334 static __inline void ahd_set_residual(struct scb *, u_long);
335 static __inline void ahd_set_sense_residual(struct scb *, u_long);
336 static __inline u_long ahd_get_residual(struct scb *);
337 static __inline int ahd_perform_autosense(struct scb *);
338 static __inline uint32_t ahd_get_sense_bufsize(struct ahd_softc*, struct scb*);
339 static __inline void ahd_freeze_simq(struct ahd_softc *);
340 static __inline void ahd_release_simq(struct ahd_softc *);
341 static __inline void ahd_freeze_ccb(union ccb *ccb);
342 static __inline void ahd_freeze_scb(struct scb *scb);
343 static __inline void ahd_platform_freeze_devq(struct ahd_softc *, struct scb *);
344 static __inline int  ahd_platform_abort_scbs(struct ahd_softc *ahd, int target,
345 					     char channel, int lun, u_int tag,
346 					     role_t role, uint32_t status);
347 
348 static __inline
349 void ahd_set_transaction_status(struct scb *scb, uint32_t status)
350 {
351 	scb->io_ctx->ccb_h.status &= ~CAM_STATUS_MASK;
352 	scb->io_ctx->ccb_h.status |= status;
353 }
354 
355 static __inline
356 void ahd_set_scsi_status(struct scb *scb, uint32_t status)
357 {
358 	scb->io_ctx->csio.scsi_status = status;
359 }
360 
361 static __inline
362 uint32_t ahd_get_transaction_status(struct scb *scb)
363 {
364 	return (scb->io_ctx->ccb_h.status & CAM_STATUS_MASK);
365 }
366 
367 static __inline
368 uint32_t ahd_get_scsi_status(struct scb *scb)
369 {
370 	return (scb->io_ctx->csio.scsi_status);
371 }
372 
373 static __inline
374 void ahd_set_transaction_tag(struct scb *scb, int enabled, u_int type)
375 {
376 	scb->io_ctx->csio.tag_action = type;
377 	if (enabled)
378 		scb->io_ctx->ccb_h.flags |= CAM_TAG_ACTION_VALID;
379 	else
380 		scb->io_ctx->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
381 }
382 
383 static __inline
384 u_long ahd_get_transfer_length(struct scb *scb)
385 {
386 	return (scb->io_ctx->csio.dxfer_len);
387 }
388 
389 static __inline
390 int ahd_get_transfer_dir(struct scb *scb)
391 {
392 	return (scb->io_ctx->ccb_h.flags & CAM_DIR_MASK);
393 }
394 
395 static __inline
396 void ahd_set_residual(struct scb *scb, u_long resid)
397 {
398 	scb->io_ctx->csio.resid = resid;
399 }
400 
401 static __inline
402 void ahd_set_sense_residual(struct scb *scb, u_long resid)
403 {
404 	scb->io_ctx->csio.sense_resid = resid;
405 }
406 
407 static __inline
408 u_long ahd_get_residual(struct scb *scb)
409 {
410 	return (scb->io_ctx->csio.resid);
411 }
412 
413 static __inline
414 int ahd_perform_autosense(struct scb *scb)
415 {
416 	return (!(scb->io_ctx->ccb_h.flags & CAM_DIS_AUTOSENSE));
417 }
418 
419 static __inline uint32_t
420 ahd_get_sense_bufsize(struct ahd_softc *ahd, struct scb *scb)
421 {
422 	return (sizeof(struct scsi_sense_data));
423 }
424 
425 static __inline void
426 ahd_freeze_simq(struct ahd_softc *ahd)
427 {
428 	xpt_freeze_simq(ahd->platform_data->sim, /*count*/1);
429 }
430 
431 static __inline void
432 ahd_release_simq(struct ahd_softc *ahd)
433 {
434 	xpt_release_simq(ahd->platform_data->sim, /*run queue*/TRUE);
435 }
436 
437 static __inline void
438 ahd_freeze_ccb(union ccb *ccb)
439 {
440 	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
441 		ccb->ccb_h.status |= CAM_DEV_QFRZN;
442 		xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
443 	}
444 }
445 
446 static __inline void
447 ahd_freeze_scb(struct scb *scb)
448 {
449 	ahd_freeze_ccb(scb->io_ctx);
450 }
451 
452 static __inline void
453 ahd_platform_freeze_devq(struct ahd_softc *ahd, struct scb *scb)
454 {
455 	/* Nothing to do here for FreeBSD */
456 }
457 
458 static __inline int
459 ahd_platform_abort_scbs(struct ahd_softc *ahd, int target,
460 			char channel, int lun, u_int tag,
461 			role_t role, uint32_t status)
462 {
463 	/* Nothing to do here for FreeBSD */
464 	return (0);
465 }
466 
467 static __inline void
468 ahd_platform_scb_free(struct ahd_softc *ahd, struct scb *scb)
469 {
470 	/* What do we do to generically handle driver resource shortages??? */
471 	if ((ahd->flags & AHD_RESOURCE_SHORTAGE) != 0
472 	 && scb->io_ctx != NULL
473 	 && (scb->io_ctx->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
474 		scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
475 		ahd->flags &= ~AHD_RESOURCE_SHORTAGE;
476 	}
477 	scb->io_ctx = NULL;
478 }
479 
480 /********************************** PCI ***************************************/
481 #ifdef AHD_PCI_CONFIG
482 static __inline uint32_t ahd_pci_read_config(ahd_dev_softc_t pci,
483 					     int reg, int width);
484 static __inline void	 ahd_pci_write_config(ahd_dev_softc_t pci,
485 					      int reg, uint32_t value,
486 					      int width);
487 static __inline int	 ahd_get_pci_function(ahd_dev_softc_t);
488 static __inline int	 ahd_get_pci_slot(ahd_dev_softc_t);
489 static __inline int	 ahd_get_pci_bus(ahd_dev_softc_t);
490 
491 int			 ahd_pci_map_registers(struct ahd_softc *ahd);
492 int			 ahd_pci_map_int(struct ahd_softc *ahd);
493 
494 static __inline uint32_t
495 ahd_pci_read_config(ahd_dev_softc_t pci, int reg, int width)
496 {
497 	return (pci_read_config(pci, reg, width));
498 }
499 
500 static __inline void
501 ahd_pci_write_config(ahd_dev_softc_t pci, int reg, uint32_t value, int width)
502 {
503 	pci_write_config(pci, reg, value, width);
504 }
505 
506 static __inline int
507 ahd_get_pci_function(ahd_dev_softc_t pci)
508 {
509 	return (pci_get_function(pci));
510 }
511 
512 static __inline int
513 ahd_get_pci_slot(ahd_dev_softc_t pci)
514 {
515 	return (pci_get_slot(pci));
516 }
517 
518 static __inline int
519 ahd_get_pci_bus(ahd_dev_softc_t pci)
520 {
521 	return (pci_get_bus(pci));
522 }
523 
524 typedef enum
525 {
526 	AHD_POWER_STATE_D0,
527 	AHD_POWER_STATE_D1,
528 	AHD_POWER_STATE_D2,
529 	AHD_POWER_STATE_D3
530 } ahd_power_state;
531 
532 void ahd_power_state_change(struct ahd_softc *ahd,
533 			    ahd_power_state new_state);
534 #endif
535 /******************************** VL/EISA *************************************/
536 int aic7770_map_registers(struct ahd_softc *ahd);
537 int aic7770_map_int(struct ahd_softc *ahd, int irq);
538 
539 /********************************* Debug **************************************/
540 static __inline void	ahd_print_path(struct ahd_softc *, struct scb *);
541 static __inline void	ahd_platform_dump_card_state(struct ahd_softc *ahd);
542 
543 static __inline void
544 ahd_print_path(struct ahd_softc *ahd, struct scb *scb)
545 {
546 	xpt_print_path(scb->io_ctx->ccb_h.path);
547 }
548 
549 static __inline void
550 ahd_platform_dump_card_state(struct ahd_softc *ahd)
551 {
552 	/* Nothing to do here for FreeBSD */
553 }
554 /**************************** Transfer Settings *******************************/
555 void	  ahd_notify_xfer_settings_change(struct ahd_softc *,
556 					  struct ahd_devinfo *);
557 void	  ahd_platform_set_tags(struct ahd_softc *, struct ahd_devinfo *,
558 				int /*enable*/);
559 
560 /************************* Initialization/Teardown ****************************/
561 int	  ahd_platform_alloc(struct ahd_softc *ahd, void *platform_arg);
562 void	  ahd_platform_free(struct ahd_softc *ahd);
563 int	  ahd_map_int(struct ahd_softc *ahd);
564 int	  ahd_attach(struct ahd_softc *);
565 int	  ahd_softc_comp(struct ahd_softc *lahd, struct ahd_softc *rahd);
566 int	  ahd_detach(device_t);
567 
568 /****************************** Interrupts ************************************/
569 void			ahd_platform_intr(void *);
570 static __inline void	ahd_platform_flushwork(struct ahd_softc *ahd);
571 static __inline void
572 ahd_platform_flushwork(struct ahd_softc *ahd)
573 {
574 }
575 
576 /************************ Misc Function Declarations **************************/
577 timeout_t ahd_timeout;
578 void	  ahd_done(struct ahd_softc *ahd, struct scb *scb);
579 void	  ahd_send_async(struct ahd_softc *, char /*channel*/,
580 			 u_int /*target*/, u_int /*lun*/, ac_code, void *arg);
581 #endif  /* _AIC79XX_FREEBSD_H_ */
582