aic7xxx_osm.h revision 1.1 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: aic7xxx_osm.h,v 1.1 2003/04/19 19:33:30 fvdl Exp $
33 *
34 * //depot/aic7xxx/freebsd/dev/aic7xxx/aic7xxx_osm.h#14 $
35 *
36 * $FreeBSD: /repoman/r/ncvs/src/sys/dev/aic7xxx/aic7xxx_osm.h,v 1.20 2002/12/04 22:51:29 scottl Exp $
37 */
38 /*
39 * Ported from FreeBSD by Pascal Renauld, Network Storage Solutions, Inc. - April 2003
40 */
41
42 #ifndef _AIC7XXX_NETBSD_H_
43 #define _AIC7XXX_NETBSD_H_
44
45 #include "opt_ahc.h" /* for config options */
46
47 #include <sys/param.h>
48 #include <sys/kernel.h>
49 #include <sys/systm.h>
50 #include <sys/device.h>
51 #include <sys/malloc.h>
52 #include <sys/buf.h>
53 #include <sys/proc.h>
54 #include <sys/scsiio.h>
55 #include <sys/reboot.h>
56 #include <sys/kthread.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 <dev/scsipi/scsi_all.h>
65 #include <dev/scsipi/scsipi_all.h>
66 #include <dev/scsipi/scsi_message.h>
67 #include <dev/scsipi/scsipi_debug.h>
68 #include <dev/scsipi/scsiconf.h>
69 #include <dev/scsipi/scsi_iu.h>
70
71 #include <uvm/uvm_extern.h>
72
73 #ifdef CAM_NEW_TRAN_CODE
74 #define AHC_NEW_TRAN_SETTINGS
75 #endif /* CAM_NEW_TRAN_CODE */
76
77
78 /****************************** Platform Macros *******************************/
79 #define SIM_IS_SCSIBUS_B(ahc, sim) \
80 ((sim) == ahc->platform_data->sim_b)
81 #define SIM_CHANNEL(ahc, sim) \
82 (((sim) == ahc->platform_data->sim_b) ? 'B' : 'A')
83 #define SIM_SCSI_ID(ahc, sim) \
84 (((sim) == ahc->platform_data->sim_b) ? ahc->our_id_b : ahc->our_id)
85 #define SIM_PATH(ahc, sim) \
86 (((sim) == ahc->platform_data->sim_b) ? ahc->platform_data->path_b \
87 : ahc->platform_data->path)
88 #define BUILD_SCSIID(ahc, sim, target_id, our_id) \
89 ((((target_id) << TID_SHIFT) & TID) | (our_id))
90
91 #define SCB_GET_SIM(ahc, scb) \
92 (SCB_GET_CHANNEL(ahc, scb) == 'A' ? (ahc)->platform_data->sim \
93 : (ahc)->platform_data->sim_b)
94
95 #ifndef offsetof
96 #define offsetof(type, member) ((size_t)(&((type *)0)->member))
97 #endif
98 /************************* Forward Declarations *******************************/
99 typedef pcireg_t ahc_dev_softc_t;
100
101 /***************************** Bus Space/DMA **********************************/
102 #define ahc_dma_tag_create(ahc, parent_tag, alignment, boundary, \
103 lowaddr, highaddr, filter, filterarg, \
104 maxsize, nsegments, maxsegsz, flags, \
105 dma_tagp) \
106 bus_dma_tag_create(parent_tag, alignment, boundary, \
107 lowaddr, highaddr, filter, filterarg, \
108 maxsize, nsegments, maxsegsz, flags, \
109 dma_tagp)
110
111 #define ahc_dma_tag_destroy(ahc, tag) \
112 bus_dma_tag_destroy(tag)
113
114 #define ahc_dmamem_alloc(ahc, dmat, vaddr, flags, mapp) \
115 bus_dmamem_alloc(dmat, vaddr, flags, mapp)
116
117 #define ahc_dmamem_free(ahc, dmat, vaddr, map) \
118 bus_dmamem_free(dmat, vaddr, map)
119
120 #define ahc_dmamap_create(ahc, tag, flags, mapp) \
121 bus_dmamap_create(tag, flags, mapp)
122
123 #define ahc_dmamap_destroy(ahc, tag, map) \
124 bus_dmamap_destroy(tag, map)
125
126 #define ahc_dmamap_load(ahc, dmat, map, addr, buflen, callback, \
127 callback_arg, flags) \
128 bus_dmamap_load(dmat, map, addr, buflen, callback, callback_arg, flags)
129
130 #define ahc_dmamap_unload(ahc, tag, map) \
131 bus_dmamap_unload(tag, map)
132
133 /* XXX Need to update Bus DMA for partial map syncs */
134 #define ahc_dmamap_sync(ahc, dma_tag, dmamap, offset, len, op) \
135 bus_dmamap_sync(dma_tag, dmamap, offset, len, op)
136
137 /************************ Tunable Driver Parameters **************************/
138 /*
139 * The number of dma segments supported. The sequencer can handle any number
140 * of physically contiguous S/G entrys. To reduce the driver's memory
141 * consumption, we limit the number supported to be sufficient to handle
142 * the largest mapping supported by the kernel, MAXPHYS. Assuming the
143 * transfer is as fragmented as possible and unaligned, this turns out to
144 * be the number of paged sized transfers in MAXPHYS plus an extra element
145 * to handle any unaligned residual. The sequencer fetches SG elements
146 * in cacheline sized chucks, so make the number per-transaction an even
147 * multiple of 16 which should align us on even the largest of cacheline
148 * boundaries.
149 */
150 #define AHC_NSEG (roundup(btoc(MAXPHYS) + 1, 16))
151
152 /* This driver supports target mode */
153 //#define AHC_TARGET_MODE 1
154
155 /************************** Softc/SCB Platform Data ***************************/
156 struct ahc_platform_data {
157 /*
158 * Hooks into the XPT.
159 */
160 #if 0
161 struct cam_sim *sim;
162 struct cam_sim *sim_b;
163 struct cam_path *path;
164 struct cam_path *path_b;
165
166 int regs_res_type;
167 int regs_res_id;
168 int irq_res_type;
169 struct resource *regs;
170 struct resource *irq;
171 void *ih;
172 eventhandler_tag eh;
173 #endif
174 };
175
176 struct scb_platform_data {
177 };
178
179 /********************************* Byte Order *********************************/
180 #if __FreeBSD_version >= 500000
181 #define ahc_htobe16(x) htobe16(x)
182 #define ahc_htobe32(x) htobe32(x)
183 #define ahc_htobe64(x) htobe64(x)
184 #define ahc_htole16(x) htole16(x)
185 #define ahc_htole32(x) htole32(x)
186 #define ahc_htole64(x) htole64(x)
187
188 #define ahc_be16toh(x) be16toh(x)
189 #define ahc_be32toh(x) be32toh(x)
190 #define ahc_be64toh(x) be64toh(x)
191 #define ahc_le16toh(x) le16toh(x)
192 #define ahc_le32toh(x) le32toh(x)
193 #define ahc_le64toh(x) le64toh(x)
194 #else
195 #define ahc_htobe16(x) (x)
196 #define ahc_htobe32(x) (x)
197 #define ahc_htobe64(x) (x)
198 #define ahc_htole16(x) (x)
199 #define ahc_htole32(x) (x)
200 #define ahc_htole64(x) (x)
201
202 #define ahc_be16toh(x) (x)
203 #define ahc_be32toh(x) (x)
204 #define ahc_be64toh(x) (x)
205 #define ahc_le16toh(x) (x)
206 #define ahc_le32toh(x) (x)
207 #define ahc_le64toh(x) (x)
208 #endif
209
210 /************************** Timer DataStructures ******************************/
211 typedef struct callout ahc_timer_t;
212
213 /***************************** Core Includes **********************************/
214 #if AHC_REG_PRETTY_PRINT
215 #define AIC_DEBUG_REGISTERS 1
216 #else
217 #define AIC_DEBUG_REGISTERS 0
218 #endif
219
220 #include <dev/ic/aic7xxxvar.h>
221
222 /***************************** Timer Facilities *******************************/
223 void ahc_timeout(void*);
224
225 #if __FreeBSD_version >= 500000
226 #define ahc_timer_init(timer) callout_init(timer, /*mpsafe*/0)
227 #else
228 #define ahc_timer_init callout_init
229 #endif
230 #define ahc_timer_stop callout_stop
231
232 static __inline void
233 ahc_timer_reset(ahc_timer_t *timer, u_int usec, ahc_callback_t *func, void *arg)
234 {
235 callout_reset(timer, (usec * hz)/1000000, func, arg);
236 }
237
238 static __inline void
239 ahc_scb_timer_reset(struct scb *scb, u_int usec)
240 {
241 if (!(scb->xs->xs_control & XS_CTL_POLL)) {
242 callout_reset(&scb->xs->xs_callout,
243 (usec * hz)/1000000, ahc_timeout, scb);
244 }
245 }
246
247 /*************************** Device Access ************************************/
248 #define ahc_inb(ahc, port) \
249 bus_space_read_1((ahc)->tag, (ahc)->bsh, port)
250
251 #define ahc_outb(ahc, port, value) \
252 bus_space_write_1((ahc)->tag, (ahc)->bsh, port, value)
253
254 #define ahc_outsb(ahc, port, valp, count) \
255 bus_space_write_multi_1((ahc)->tag, (ahc)->bsh, port, valp, count)
256
257 #define ahc_insb(ahc, port, valp, count) \
258 bus_space_read_multi_1((ahc)->tag, (ahc)->bsh, port, valp, count)
259
260 static __inline void ahc_flush_device_writes(struct ahc_softc *);
261
262 static __inline void
263 ahc_flush_device_writes(struct ahc_softc *ahc)
264 {
265 /* XXX Is this sufficient for all architectures??? */
266 ahc_inb(ahc, INTSTAT);
267 }
268
269 /**************************** Locking Primitives ******************************/
270 /* Lock protecting internal data structures */
271 static __inline void ahc_lockinit(struct ahc_softc *);
272 static __inline void ahc_lock(struct ahc_softc *, unsigned long *flags);
273 static __inline void ahc_unlock(struct ahc_softc *, unsigned long *flags);
274
275 /* Lock held during command compeletion to the upper layer */
276 static __inline void ahc_done_lockinit(struct ahc_softc *);
277 static __inline void ahc_done_lock(struct ahc_softc *, unsigned long *flags);
278 static __inline void ahc_done_unlock(struct ahc_softc *, unsigned long *flags);
279
280 /* Lock held during ahc_list manipulation and ahc softc frees */
281 static __inline void ahc_list_lockinit(void);
282 static __inline void ahc_list_lock(unsigned long *flags);
283 static __inline void ahc_list_unlock(unsigned long *flags);
284
285 static __inline void
286 ahc_lockinit(struct ahc_softc *ahc)
287 {
288 }
289
290 static __inline void
291 ahc_lock(struct ahc_softc *ahc, unsigned long *flags)
292 {
293 *flags = splbio();
294 }
295
296 static __inline void
297 ahc_unlock(struct ahc_softc *ahc, unsigned long *flags)
298 {
299 splx(*flags);
300 }
301
302 /* Lock held during command compeletion to the upper layer */
303 static __inline void
304 ahc_done_lockinit(struct ahc_softc *ahc)
305 {
306 }
307
308 static __inline void
309 ahc_done_lock(struct ahc_softc *ahc, unsigned long *flags)
310 {
311 }
312
313 static __inline void
314 ahc_done_unlock(struct ahc_softc *ahc, unsigned long *flags)
315 {
316 }
317
318 /* Lock held during ahc_list manipulation and ahc softc frees */
319 static __inline void
320 ahc_list_lockinit()
321 {
322 }
323
324 static __inline void
325 ahc_list_lock(unsigned long *flags)
326 {
327 }
328
329 static __inline void
330 ahc_list_unlock(unsigned long *flags)
331 {
332 }
333 /****************************** OS Primitives *********************************/
334 #define ahc_delay DELAY
335
336 /************************** Transaction Operations ****************************/
337 static __inline void ahc_set_transaction_status(struct scb *, uint32_t);
338 static __inline void ahc_set_scsi_status(struct scb *, uint32_t);
339 static __inline uint32_t ahc_get_transaction_status(struct scb *);
340 static __inline uint32_t ahc_get_scsi_status(struct scb *);
341 static __inline void ahc_set_transaction_tag(struct scb *, int, u_int);
342 static __inline u_long ahc_get_transfer_length(struct scb *);
343 static __inline int ahc_get_transfer_dir(struct scb *);
344 static __inline void ahc_set_residual(struct scb *, u_long);
345 static __inline void ahc_set_sense_residual(struct scb *, u_long);
346 static __inline u_long ahc_get_residual(struct scb *);
347 static __inline int ahc_perform_autosense(struct scb *);
348 static __inline uint32_t ahc_get_sense_bufsize(struct ahc_softc*, struct scb*);
349 static __inline void ahc_freeze_scb(struct scb *scb);
350 static __inline void ahc_platform_freeze_devq(struct ahc_softc *, struct scb *);
351 static __inline int ahc_platform_abort_scbs(struct ahc_softc *ahc, int target,
352 char channel, int lun, u_int tag,
353 role_t role, uint32_t status);
354
355 static __inline
356 void ahc_set_transaction_status(struct scb *scb, uint32_t status)
357 {
358 scb->xs->error = status;
359 }
360
361 static __inline
362 void ahc_set_scsi_status(struct scb *scb, uint32_t status)
363 {
364 scb->xs->xs_status = status;
365 }
366
367 static __inline
368 uint32_t ahc_get_transaction_status(struct scb *scb)
369 {
370 return (scb->xs->error);
371 }
372
373 static __inline
374 uint32_t ahc_get_scsi_status(struct scb *scb)
375 {
376 return (scb->xs->xs_status);
377 }
378
379 static __inline
380 void ahc_set_transaction_tag(struct scb *scb, int enabled, u_int type)
381 {
382 scb->xs->xs_tag_type = type;
383 }
384
385 static __inline
386 u_long ahc_get_transfer_length(struct scb *scb)
387 {
388 return (scb->xs->datalen);
389 }
390
391 static __inline
392 int ahc_get_transfer_dir(struct scb *scb)
393 {
394 return (scb->xs->xs_control & (XS_CTL_DATA_IN|XS_CTL_DATA_OUT));
395 }
396
397 static __inline
398 void ahc_set_residual(struct scb *scb, u_long resid)
399 {
400 scb->xs->resid = resid;
401 }
402
403 static __inline
404 void ahc_set_sense_residual(struct scb *scb, u_long resid)
405 {
406 //scb->io_ctx->csio.sense_resid = resid;
407 }
408
409 static __inline
410 u_long ahc_get_residual(struct scb *scb)
411 {
412 return (scb->xs->resid);
413 }
414
415 static __inline
416 int ahc_perform_autosense(struct scb *scb)
417 {
418 return (!(scb->flags & SCB_SENSE));
419 }
420
421 static __inline uint32_t
422 ahc_get_sense_bufsize(struct ahc_softc *ahc, struct scb *scb)
423 {
424 return (sizeof(struct scsipi_sense_data));
425 }
426
427 static __inline void
428 ahc_freeze_scb(struct scb *scb)
429 {
430 struct scsipi_xfer *xs = scb->xs;
431
432 if (!(scb->flags & SCB_FREEZE_QUEUE)) {
433 scsipi_periph_freeze(xs->xs_periph, 1);
434 scb->flags |= SCB_FREEZE_QUEUE;
435 }
436 }
437
438 static __inline void
439 ahc_platform_freeze_devq(struct ahc_softc *ahc, struct scb *scb)
440 {
441 /* Nothing to do here for FreeBSD */
442 }
443
444 static __inline int
445 ahc_platform_abort_scbs(struct ahc_softc *ahc, int target,
446 char channel, int lun, u_int tag,
447 role_t role, uint32_t status)
448 {
449 /* Nothing to do here for FreeBSD */
450 return (0);
451 }
452
453 static __inline void
454 ahc_platform_scb_free(struct ahc_softc *ahc, struct scb *scb)
455 {
456 #ifdef _FreeBSD_
457 /* What do we do to generically handle driver resource shortages??? */
458 if ((ahc->flags & AHC_RESOURCE_SHORTAGE) != 0
459 && scb->io_ctx != NULL
460 && (scb->io_ctx->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
461 scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
462 ahc->flags &= ~AHC_RESOURCE_SHORTAGE;
463 }
464 scb->io_ctx = NULL;
465 #endif
466 }
467
468 /********************************** PCI ***************************************/
469 #ifdef AHC_PCI_CONFIG
470 static __inline uint32_t ahc_pci_read_config(ahc_dev_softc_t pci,
471 int reg, int width);
472 static __inline void ahc_pci_write_config(ahc_dev_softc_t pci,
473 int reg, uint32_t value,
474 int width);
475 static __inline int ahc_get_pci_function(ahc_dev_softc_t);
476 static __inline int ahc_get_pci_slot(ahc_dev_softc_t);
477 static __inline int ahc_get_pci_bus(ahc_dev_softc_t);
478
479 int ahc_pci_map_registers(struct ahc_softc *ahc);
480 int ahc_pci_map_int(struct ahc_softc *ahc);
481
482 static __inline uint32_t
483 ahc_pci_read_config(ahc_dev_softc_t pci, int reg, int width)
484 {
485 return (pci_read_config(pci, reg, width));
486 }
487
488 static __inline void
489 ahc_pci_write_config(ahc_dev_softc_t pci, int reg, uint32_t value, int width)
490 {
491 pci_write_config(pci, reg, value, width);
492 }
493
494 static __inline int
495 ahc_get_pci_function(ahc_dev_softc_t pci)
496 {
497 return (pci_get_function(pci));
498 }
499
500 static __inline int
501 ahc_get_pci_slot(ahc_dev_softc_t pci)
502 {
503 return (pci_get_slot(pci));
504 }
505
506 static __inline int
507 ahc_get_pci_bus(ahc_dev_softc_t pci)
508 {
509 return (pci_get_bus(pci));
510 }
511
512 typedef enum
513 {
514 AHC_POWER_STATE_D0,
515 AHC_POWER_STATE_D1,
516 AHC_POWER_STATE_D2,
517 AHC_POWER_STATE_D3
518 } ahc_power_state;
519
520 void ahc_power_state_change(struct ahc_softc *ahc,
521 ahc_power_state new_state);
522 #endif
523 /******************************** VL/EISA *************************************/
524 int aic7770_map_registers(struct ahc_softc *ahc, u_int port);
525 int aic7770_map_int(struct ahc_softc *ahc, int irq);
526
527 /********************************* Debug **************************************/
528 static __inline void ahc_print_path(struct ahc_softc *, struct scb *);
529 static __inline void ahc_platform_dump_card_state(struct ahc_softc *ahc);
530
531 static __inline void
532 ahc_print_path(struct ahc_softc *ahc, struct scb *scb)
533 {
534 printf("%s:", ahc->sc_dev.dv_xname);
535 }
536
537 static __inline void
538 ahc_platform_dump_card_state(struct ahc_softc *ahc)
539 {
540 /* Nothing to do here for FreeBSD */
541 }
542 /**************************** Transfer Settings *******************************/
543 void ahc_notify_xfer_settings_change(struct ahc_softc *,
544 struct ahc_devinfo *);
545 void ahc_platform_set_tags(struct ahc_softc *, struct ahc_devinfo *,
546 int /*enable*/);
547
548 /************************* Initialization/Teardown ****************************/
549 int ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg);
550 void ahc_platform_free(struct ahc_softc *ahc);
551 int ahc_map_int(struct ahc_softc *ahc);
552 int ahc_attach(struct ahc_softc *);
553 int ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc);
554 int ahc_detach(struct device *device, int flags);
555
556 /****************************** Interrupts ************************************/
557 void ahc_platform_intr(void *);
558 static __inline void ahc_platform_flushwork(struct ahc_softc *ahc);
559 static __inline void
560 ahc_platform_flushwork(struct ahc_softc *ahc)
561 {
562 }
563
564 /************************ Misc Function Declarations **************************/
565 void ahc_done(struct ahc_softc *ahc, struct scb *scb);
566 void ahc_send_async(struct ahc_softc *, char /*channel*/,
567 u_int /*target*/, u_int /*lun*/, u_int /*ac_code*/, void *arg);
568 #endif /* _AIC7XXX_NETBSD_H_ */
569