1 /* $OpenBSD: aic7xxx_openbsd.h,v 1.11 2004/01/17 14:40:55 krw Exp $ */
2 /* $NetBSD: aic7xxx_osm.h,v 1.7 2003/11/02 11:07:44 wiz Exp $ */
3
4 /*
5 * OpenBSD platform specific driver option settings, data structures,
6 * function declarations and includes.
7 *
8 * Copyright (c) 1994-2001 Justin T. Gibbs.
9 * Copyright (c) 2001-2002 Steve Murphree, Jr.
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. The name of the author(s) may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * Alternatively, this software may be distributed under the terms of the
22 * GNU Public License ("GPL").
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
28 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * //depot/aic7xxx/freebsd/dev/aic7xxx/aic7xxx_osm.h#14 $
37 *
38 * $FreeBSD: /repoman/r/ncvs/src/sys/dev/aic7xxx/aic7xxx_osm.h,v 1.20 2002/12/04 22:51:29 scottl Exp $
39 */
40 /*
41 * Ported from FreeBSD by Pascal Renauld, Network Storage Solutions, Inc. - April 2003
42 */
43
44 #ifndef _AIC7XXX_OPENBSD_H_
45 #define _AIC7XXX_OPENBSD_H_
46
47 #include "pci.h" /* for config options */
48
49 #include <sys/param.h>
50 #include <sys/kernel.h>
51 #include <sys/systm.h>
52 #include <sys/device.h>
53 #include <sys/malloc.h>
54 #include <sys/buf.h>
55 #include <sys/proc.h>
56 #include <sys/queue.h>
57
58 #include <dev/pci/pcireg.h>
59 #include <dev/pci/pcivar.h>
60
61 #include <machine/bus.h>
62 #include <machine/intr.h>
63
64 #include <scsi/scsi_all.h>
65 #include <scsi/scsi_message.h>
66 #include <scsi/scsi_debug.h>
67 #include <scsi/scsiconf.h>
68
69 #include <uvm/uvm_extern.h>
70
71 #ifdef CAM_NEW_TRAN_CODE
72 #define AHC_NEW_TRAN_SETTINGS
73 #endif /* CAM_NEW_TRAN_CODE */
74
75 #if NPCI > 0
76 #define AHC_PCI_CONFIG 1
77 #endif
78
79 #define AHC_DEBUG AHC_SHOW_SENSE | AHC_SHOW_MISC | AHC_SHOW_CMDS
80
81 #ifdef DEBUG
82 #define bootverbose 1
83 #else
84 #define bootverbose 0
85 #endif
86 /****************************** Platform Macros *******************************/
87
88 #define SCSI_IS_SCSIBUS_B(ahc, sc_link) \
89 ((sc_link)->scsibus == (ahc)->sc_channel_b.scsibus)
90 #define SCSI_SCSI_ID(ahc, sc_link) \
91 (SCSI_IS_SCSIBUS_B(ahc, sc_link) ? ahc->our_id_b : ahc->our_id)
92 #define SCSI_CHANNEL(ahc, sc_link) \
93 (SCSI_IS_SCSIBUS_B(ahc, sc_link) ? 'B' : 'A')
94 #define BUILD_SCSIID(ahc, sc_link, target_id, our_id) \
95 ((((target_id) << TID_SHIFT) & TID) | (our_id))
96
97 #ifndef offsetof
98 #define offsetof(type, member) ((size_t)(&((type *)0)->member))
99 #endif
100 /************************* Forward Declarations *******************************/
101 typedef struct pci_attach_args * ahc_dev_softc_t;
102
103 /***************************** Bus Space/DMA **********************************/
104
105 /* XXX Need to update Bus DMA for partial map syncs */
106 #define ahc_dmamap_sync(ahc, dma_tag, dmamap, offset, len, op) \
107 bus_dmamap_sync(dma_tag, dmamap, offset, len, op)
108
109 /************************ Tunable Driver Parameters **************************/
110 /*
111 * The number of DMA segments supported. The sequencer can handle any number
112 * of physically contiguous S/G entrys. To reduce the driver's memory
113 * consumption, we limit the number supported to be sufficient to handle
114 * the largest mapping supported by the kernel, MAXPHYS. Assuming the
115 * transfer is as fragmented as possible and unaligned, this turns out to
116 * be the number of paged sized transfers in MAXPHYS plus an extra element
117 * to handle any unaligned residual. The sequencer fetches SG elements
118 * in cacheline sized chucks, so make the number per-transaction an even
119 * multiple of 16 which should align us on even the largest of cacheline
120 * boundaries.
121 */
122 #define AHC_NSEG (roundup(btoc(MAXPHYS) + 1, 16))
123
124 /* This driver supports target mode */
125 //#define AHC_TARGET_MODE 1
126
127 #include <dev/ic/aic7xxxvar.h>
128
129 /************************** Softc/SCB Platform Data ***************************/
130 struct ahc_platform_data {
131 };
132
133 struct scb_platform_data {
134 };
135
136 /*
137 * Some ISA devices (e.g. on a VLB) can perform 32-bit DMA. This
138 * flag is passed to bus_dmamap_create() to indicate that fact.
139 */
140 #ifndef ISABUS_DMA_32BIT
141 #define ISABUS_DMA_32BIT BUS_DMA_BUS1
142 #endif
143
144 /********************************* Byte Order *********************************/
145 #define ahc_htobe16(x) htobe16(x)
146 #define ahc_htobe32(x) htobe32(x)
147 #define ahc_htobe64(x) htobe64(x)
148 #define ahc_htole16(x) htole16(x)
149 #define ahc_htole32(x) htole32(x)
150 #define ahc_htole64(x) htole64(x)
151
152 #define ahc_be16toh(x) betoh16(x)
153 #define ahc_be32toh(x) betoh32(x)
154 #define ahc_be64toh(x) betoh64(x)
155 #define ahc_le16toh(x) letoh16(x)
156 #define ahc_le32toh(x) letoh32(x)
157 #define ahc_le64toh(x) letoh64(x)
158
159 /************************** Timer DataStructures ******************************/
160 typedef struct timeout ahc_timer_t;
161
162 /***************************** Core Includes **********************************/
163 #if AHC_REG_PRETTY_PRINT
164 #define AIC_DEBUG_REGISTERS 1
165 #else
166 #define AIC_DEBUG_REGISTERS 0
167 #endif
168
169 /***************************** Timer Facilities *******************************/
170 void ahc_timeout(void*);
171
172 #define ahc_timer_init callout_init
173 #define ahc_timer_stop callout_stop
174
175 static __inline void
ahc_timer_reset(ahc_timer_t * timer,u_int usec,ahc_callback_t * func,void * arg)176 ahc_timer_reset(ahc_timer_t *timer, u_int usec, ahc_callback_t *func, void *arg)
177 {
178 callout_reset(timer, (usec * hz)/1000000, func, arg);
179 }
180
181 static __inline void
ahc_scb_timer_reset(struct scb * scb,u_int usec)182 ahc_scb_timer_reset(struct scb *scb, u_int usec)
183 {
184 if (!(scb->xs->xs_control & XS_CTL_POLL)) {
185 callout_reset(&scb->xs->xs_callout,
186 (usec * hz)/1000000, ahc_timeout, scb);
187 }
188 }
189
190 /*************************** Device Access ************************************/
191 #define ahc_inb(ahc, port) \
192 bus_space_read_1((ahc)->tag, (ahc)->bsh, port)
193
194 #define ahc_outb(ahc, port, value) \
195 bus_space_write_1((ahc)->tag, (ahc)->bsh, port, value)
196
197 #define ahc_outsb(ahc, port, valp, count) \
198 bus_space_write_multi_1((ahc)->tag, (ahc)->bsh, port, valp, count)
199
200 #define ahc_insb(ahc, port, valp, count) \
201 bus_space_read_multi_1((ahc)->tag, (ahc)->bsh, port, valp, count)
202
203 static __inline void ahc_flush_device_writes(struct ahc_softc *);
204
205 static __inline void
ahc_flush_device_writes(struct ahc_softc * ahc)206 ahc_flush_device_writes(struct ahc_softc *ahc)
207 {
208 /* XXX Is this sufficient for all architectures??? */
209 ahc_inb(ahc, INTSTAT);
210 }
211
212 /**************************** Locking Primitives ******************************/
213 /* Lock protecting internal data structures */
214 static __inline void ahc_lockinit(struct ahc_softc *);
215 static __inline void ahc_lock(struct ahc_softc *, int *);
216 static __inline void ahc_unlock(struct ahc_softc *, int *);
217
218 /* Lock held during command completion to the upper layer */
219 static __inline void ahc_done_lockinit(struct ahc_softc *);
220 static __inline void ahc_done_lock(struct ahc_softc *, int *);
221 static __inline void ahc_done_unlock(struct ahc_softc *, int *);
222
223 /* Lock held during ahc_list manipulation and ahc softc frees */
224 static __inline void ahc_list_lockinit(void);
225 static __inline void ahc_list_lock(int *);
226 static __inline void ahc_list_unlock(int *);
227
228 static __inline void
ahc_lockinit(struct ahc_softc * ahc)229 ahc_lockinit(struct ahc_softc *ahc)
230 {
231 }
232
233 static __inline void
ahc_lock(struct ahc_softc * ahc,int * flags)234 ahc_lock(struct ahc_softc *ahc, int *flags)
235 {
236 *flags = splbio();
237 }
238
239 static __inline void
ahc_unlock(struct ahc_softc * ahc,int * flags)240 ahc_unlock(struct ahc_softc *ahc, int *flags)
241 {
242 splx(*flags);
243 }
244
245 /* Lock held during command completion to the upper layer */
246 static __inline void
ahc_done_lockinit(struct ahc_softc * ahc)247 ahc_done_lockinit(struct ahc_softc *ahc)
248 {
249 }
250
251 static __inline void
ahc_done_lock(struct ahc_softc * ahc,int * flags)252 ahc_done_lock(struct ahc_softc *ahc, int *flags)
253 {
254 }
255
256 static __inline void
ahc_done_unlock(struct ahc_softc * ahc,int * flags)257 ahc_done_unlock(struct ahc_softc *ahc, int *flags)
258 {
259 }
260
261 /* Lock held during ahc_list manipulation and ahc softc frees */
262 static __inline void
ahc_list_lockinit()263 ahc_list_lockinit()
264 {
265 }
266
267 static __inline void
ahc_list_lock(int * flags)268 ahc_list_lock(int *flags)
269 {
270 }
271
272 static __inline void
ahc_list_unlock(int * flags)273 ahc_list_unlock(int *flags)
274 {
275 }
276 /****************************** OS Primitives *********************************/
277 #define ahc_delay DELAY
278
279 /************************** Transaction Operations ****************************/
280 static __inline void ahc_set_transaction_status(struct scb *, uint32_t);
281 static __inline void ahc_set_scsi_status(struct scb *, uint32_t);
282 static __inline uint32_t ahc_get_transaction_status(struct scb *);
283 static __inline uint32_t ahc_get_scsi_status(struct scb *);
284 static __inline void ahc_set_transaction_tag(struct scb *, int, u_int);
285 static __inline u_long ahc_get_transfer_length(struct scb *);
286 static __inline int ahc_get_transfer_dir(struct scb *);
287 static __inline void ahc_set_residual(struct scb *, u_long);
288 static __inline void ahc_set_sense_residual(struct scb *, u_long);
289 static __inline u_long ahc_get_residual(struct scb *);
290 static __inline int ahc_perform_autosense(struct scb *);
291 static __inline uint32_t ahc_get_sense_bufsize(struct ahc_softc *,
292 struct scb *);
293 static __inline void ahc_freeze_scb(struct scb *);
294 static __inline void ahc_platform_freeze_devq(struct ahc_softc *, struct scb *);
295 static __inline int ahc_platform_abort_scbs(struct ahc_softc *, int, char,
296 int, u_int, role_t, uint32_t);
297
298 static __inline
ahc_set_transaction_status(struct scb * scb,uint32_t status)299 void ahc_set_transaction_status(struct scb *scb, uint32_t status)
300 {
301 scb->xs->error = status;
302 }
303
304 static __inline
ahc_set_scsi_status(struct scb * scb,uint32_t status)305 void ahc_set_scsi_status(struct scb *scb, uint32_t status)
306 {
307 scb->xs->status = status;
308 }
309
310 static __inline
ahc_get_transaction_status(struct scb * scb)311 uint32_t ahc_get_transaction_status(struct scb *scb)
312 {
313 if (scb->xs->flags & ITSDONE)
314 return CAM_REQ_CMP;
315 else
316 return scb->xs->error;
317 }
318
319 static __inline
ahc_get_scsi_status(struct scb * scb)320 uint32_t ahc_get_scsi_status(struct scb *scb)
321 {
322 return (scb->xs->status);
323 }
324
325 static __inline
ahc_set_transaction_tag(struct scb * scb,int enabled,u_int type)326 void ahc_set_transaction_tag(struct scb *scb, int enabled, u_int type)
327 {
328 }
329
330 static __inline
ahc_get_transfer_length(struct scb * scb)331 u_long ahc_get_transfer_length(struct scb *scb)
332 {
333 return (scb->xs->datalen);
334 }
335
336 static __inline
ahc_get_transfer_dir(struct scb * scb)337 int ahc_get_transfer_dir(struct scb *scb)
338 {
339 return (scb->xs->flags & (SCSI_DATA_IN | SCSI_DATA_OUT));
340 }
341
342 static __inline
ahc_set_residual(struct scb * scb,u_long resid)343 void ahc_set_residual(struct scb *scb, u_long resid)
344 {
345 scb->xs->resid = resid;
346 }
347
348 static __inline
ahc_set_sense_residual(struct scb * scb,u_long resid)349 void ahc_set_sense_residual(struct scb *scb, u_long resid)
350 {
351 scb->xs->resid = resid;
352 }
353
354 static __inline
ahc_get_residual(struct scb * scb)355 u_long ahc_get_residual(struct scb *scb)
356 {
357 return (scb->xs->resid);
358 }
359
360 static __inline
ahc_perform_autosense(struct scb * scb)361 int ahc_perform_autosense(struct scb *scb)
362 {
363 /* Return true for OpenBSD */
364 return (1);
365 }
366
367 static __inline uint32_t
ahc_get_sense_bufsize(struct ahc_softc * ahc,struct scb * scb)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
ahc_freeze_scb(struct scb * scb)374 ahc_freeze_scb(struct scb *scb)
375 {
376 struct scsi_xfer *xs = scb->xs;
377 int target;
378
379 target = xs->sc_link->target;
380 if (!(scb->flags & SCB_FREEZE_QUEUE)) {
381 scb->flags |= SCB_FREEZE_QUEUE;
382 }
383 }
384
385 static __inline void
ahc_platform_freeze_devq(struct ahc_softc * ahc,struct scb * scb)386 ahc_platform_freeze_devq(struct ahc_softc *ahc, struct scb *scb)
387 {
388 }
389
390 static __inline int
ahc_platform_abort_scbs(struct ahc_softc * ahc,int target,char channel,int lun,u_int tag,role_t role,uint32_t status)391 ahc_platform_abort_scbs(struct ahc_softc *ahc, int target,
392 char channel, int lun, u_int tag,
393 role_t role, uint32_t status)
394 {
395 return (0);
396 }
397
398 static __inline void
ahc_platform_scb_free(struct ahc_softc * ahc,struct scb * scb)399 ahc_platform_scb_free(struct ahc_softc *ahc, struct scb *scb)
400 {
401 int s;
402
403 ahc_lock(ahc, &s);
404
405 if ((ahc->flags & AHC_RESOURCE_SHORTAGE) != 0 ||
406 (scb->flags & SCB_RECOVERY_SCB) != 0) {
407 ahc->flags &= ~AHC_RESOURCE_SHORTAGE;
408 }
409
410 timeout_del(&scb->xs->stimeout);
411
412 ahc_unlock(ahc, &s);
413 }
414
415 /********************************** PCI ***************************************/
416 #ifdef AHC_PCI_CONFIG
417 static __inline uint32_t ahc_pci_read_config(ahc_dev_softc_t, int, int);
418 static __inline void ahc_pci_write_config(ahc_dev_softc_t, int, uint32_t,
419 int);
420 static __inline int ahc_get_pci_function(ahc_dev_softc_t);
421 static __inline int ahc_get_pci_slot(ahc_dev_softc_t);
422 static __inline int ahc_get_pci_bus(ahc_dev_softc_t);
423
424 int ahc_pci_map_registers(struct ahc_softc *);
425 int ahc_pci_map_int(struct ahc_softc *);
426
427 static __inline uint32_t
ahc_pci_read_config(ahc_dev_softc_t pci,int reg,int width)428 ahc_pci_read_config(ahc_dev_softc_t pci, int reg, int width)
429 {
430 return (pci_conf_read(pci->pa_pc, pci->pa_tag, reg));
431 }
432
433 static __inline void
ahc_pci_write_config(ahc_dev_softc_t pci,int reg,uint32_t value,int width)434 ahc_pci_write_config(ahc_dev_softc_t pci, int reg, uint32_t value, int width)
435 {
436 pci_conf_write(pci->pa_pc, pci->pa_tag, reg, value);
437 }
438
439 static __inline int
ahc_get_pci_function(ahc_dev_softc_t pci)440 ahc_get_pci_function(ahc_dev_softc_t pci)
441 {
442 return (pci->pa_function);
443 }
444
445 static __inline int
ahc_get_pci_slot(ahc_dev_softc_t pci)446 ahc_get_pci_slot(ahc_dev_softc_t pci)
447 {
448 return (pci->pa_device);
449 }
450
451 static __inline int
ahc_get_pci_bus(ahc_dev_softc_t pci)452 ahc_get_pci_bus(ahc_dev_softc_t pci)
453 {
454 return (pci->pa_bus);
455 }
456
457 typedef enum
458 {
459 AHC_POWER_STATE_D0,
460 AHC_POWER_STATE_D1,
461 AHC_POWER_STATE_D2,
462 AHC_POWER_STATE_D3
463 } ahc_power_state;
464
465 void ahc_power_state_change(struct ahc_softc *, ahc_power_state);
466 #endif
467 /******************************** VL/EISA *************************************/
468 int aic7770_map_registers(struct ahc_softc *, u_int);
469 int aic7770_map_int(struct ahc_softc *, int);
470
471 /********************************* Debug **************************************/
472 static __inline void ahc_print_path(struct ahc_softc *, struct scb *);
473 static __inline void ahc_platform_dump_card_state(struct ahc_softc *);
474
475 static __inline void
ahc_print_path(struct ahc_softc * ahc,struct scb * scb)476 ahc_print_path(struct ahc_softc *ahc, struct scb *scb)
477 {
478 sc_print_addr(scb->xs->sc_link);
479 }
480
481 static __inline void
ahc_platform_dump_card_state(struct ahc_softc * ahc)482 ahc_platform_dump_card_state(struct ahc_softc *ahc)
483 {
484 /* Nothing to do here for OpenBSD */
485 printf("FEATURES = 0x%x, FLAGS = 0x%x, CHIP = 0x%x BUGS =0x%x\n",
486 ahc->features, ahc->flags, ahc->chip, ahc->bugs);
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 *, int);
492
493 /************************* Initialization/Teardown ****************************/
494 int ahc_platform_alloc(struct ahc_softc *, void *);
495 void ahc_platform_free(struct ahc_softc *);
496 int ahc_map_int(struct ahc_softc *);
497 int ahc_attach(struct ahc_softc *);
498 int ahc_softc_comp(struct ahc_softc *, struct ahc_softc *);
499 int ahc_detach(struct device *, int);
500
501 /****************************** Interrupts ************************************/
502 int ahc_platform_intr(void *);
503 static __inline void ahc_platform_flushwork(struct ahc_softc *);
504 static __inline void
ahc_platform_flushwork(struct ahc_softc * ahc)505 ahc_platform_flushwork(struct ahc_softc *ahc)
506 {
507 }
508
509 /************************ Misc Function Declarations **************************/
510 void ahc_done(struct ahc_softc *, struct scb *);
511 void ahc_send_async(struct ahc_softc *, char, u_int, u_int, ac_code,
512 void *);
513
514 #endif /* _AIC7XXX_OPENBSD_H_ */
515