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