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