1 /* $NecBSD: bsif.c,v 1.6 1997/10/31 17:43:40 honda Exp $ */
3 * Copyright (c) HONDA Naofumi, KATO Takenori, 1996. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer as
11 * the first lines of this file unmodified.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * $FreeBSD: src/sys/i386/isa/bs/bsif.c,v 1.10.2.1 2000/08/24 08:06:08 kato Exp $
30 * $DragonFly: src/sys/dev/disk/i386/bs/Attic/bsif.c,v 1.5 2004/02/13 01:04:14 joerg Exp $
34 /* WARNING: Any bug report must contain BS_REL_VERSION */
35 #define BS_REL_VERSION "NetBSD1.2/030" /* major jump */
39 #include <i386/Cbus/dev/bs/bsif.h>
40 #endif /* __NetBSD__ */
41 #if defined(__DragonFly__) || defined(__FreeBSD__)
46 #endif /* __FreeBSD__ */
48 #include <bus/cam/cam.h>
49 #include <bus/cam/cam_ccb.h>
50 #include <bus/cam/cam_sim.h>
51 #include <bus/cam/cam_xpt_sim.h>
52 #include <bus/cam/cam_debug.h>
54 #include <bus/cam/scsi/scsi_all.h>
55 #include <bus/cam/scsi/scsi_message.h>
57 /**************************************************
59 **************************************************/
61 static void bs_scsi_minphys (struct buf *);
63 struct cfdriver bs_cd = {
67 struct scsi_device bs_dev = {
68 NULL, /* Use default error handler */
69 NULL, /* have a queue, served by this */
70 NULL, /* have no async handler */
71 NULL, /* Use default 'done' routine */
74 struct scsi_adapter pc98texa55bs = {
80 #endif /* __NetBSD__ */
82 #if defined(__DragonFly__) || defined(__FreeBSD__)
83 static int bsprobe (struct isa_device *);
84 static void bs_poll(struct cam_sim *sim);
85 static int bsattach(struct isa_device *);
86 static ointhand2_t bsintr;
87 static int bs_dmarangecheck (caddr_t, unsigned);
89 struct isa_driver bsdriver = {
95 struct scsi_device bs_dev = {
96 NULL, /* Use default error handler */
97 NULL, /* have a queue, served by this */
98 NULL, /* have no async handler */
99 NULL, /* Use default 'done' routine */
105 bs_adapter_info(unit)
111 static struct scsi_adapter pc98texa55bs = {
120 static u_short pc98_irq_ball[16] = {
121 IRQ0, IRQ1, IRQ2, IRQ3, IRQ4, IRQ5, IRQ6, IRQ7,
122 IRQ8, IRQ9, IRQ10, IRQ11, IRQ12, IRQ13, IRQ14, IRQ15
125 static struct bs_softc *bscdata[NBS];
126 #endif /* __FreeBSD__ */
128 /*****************************************************************
129 * OS <=> BS INTERFACE
130 *****************************************************************/
131 #if defined(__DragonFly__) || defined(__FreeBSD__)
134 struct isa_device *dev;
136 struct bs_softc *bsc;
137 int unit = dev->id_unit;
142 printf("bs%d: unit number too high\n", unit);
146 * Allocate a storage for us
149 printf("bs%d: memory already allocated\n", unit);
152 if (!(bsc = malloc(sizeof(struct bs_softc), M_TEMP, M_NOWAIT))) {
153 printf("bs%d cannot malloc!\n", unit);
156 bzero(bsc, sizeof(struct bs_softc));
157 callout_handle_init(&bsc->timeout_ch);
161 bsc->sc_cfgflags = DVCFG_MINOR(dev->id_flags);
162 bsc->sc_hw = DVCFG_HW(&bshw_hwsel, DVCFG_MAJOR(dev->id_flags));
163 if (bsc->sc_hw == NULL)
166 if ((bsc->sc_hw->hw_flags & BSHW_SMFIFO) &&
167 (dev->id_maddr != (caddr_t)MADDRUNK))
168 bsc->sm_offset = (u_long) dev->id_maddr;
170 bsc->sm_offset = (u_long) 0;
172 snprintf(bsc->sc_dvname, sizeof(bsc->sc_dvname), "bs%d", unit);
174 if (dev->id_iobase == 0)
176 printf("%s: iobase not specified. Assume default port(0x%x)\n",
177 bsc->sc_dvname, BSHW_DEFAULT_PORT);
178 dev->id_iobase = BSHW_DEFAULT_PORT;
181 bsc->sc_iobase = dev->id_iobase;
184 if (bshw_board_probe(bsc, &drq, &irq))
187 dev->id_irq = pc98_irq_ball[irq];
188 dev->id_drq = (short)drq;
190 /* initialize host queue and target info */
191 bs_hostque_init(bsc);
192 for (i = 0; i < NTARGETS; i++)
193 if (i != bsc->sc_hostid)
194 bs_init_target_info(bsc, i);
196 /* initialize ccb queue */
197 bs_init_ccbque(BS_MAX_CCB);
199 /* scsi bus reset and restart */
200 bsc->sc_hstate = BSC_BOOTUP;
201 bsc->sc_retry = RETRIES;
202 bsc->sc_wc = delaycount * 250; /* about 1 sec */
209 #endif /* __FreeBSD__ */
219 printf("%s: scsibus ", name);
224 #if defined(__DragonFly__) || defined(__FreeBSD__)
226 bs_poll(struct cam_sim *sim)
228 bs_sequencer(cam_sim_softc(sim));
233 struct isa_device *dev;
235 int unit = dev->id_unit;
236 struct bs_softc *bsc = bscdata[unit];
237 struct cam_devq *devq;
239 dev->id_ointr = bsintr;
242 * CAM support HN2 MAX_START, MAX_TAGS xxxx
244 devq = cam_simq_alloc(256/*MAX_START*/);
248 bsc->sim = cam_sim_alloc(bs_scsi_cmd, bs_poll, "bs",
249 bsc, unit, 1, 32/*MAX_TAGS*/, devq);
250 if (bsc->sim == NULL) {
255 if (xpt_bus_register(bsc->sim, 0) != CAM_SUCCESS) {
256 free(bsc->sim, M_DEVBUF);
260 if (xpt_create_path(&bsc->path, /*periph*/NULL,
261 cam_sim_path(bsc->sim), CAM_TARGET_WILDCARD,
262 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
263 xpt_bus_deregister(cam_sim_path(bsc->sim));
264 cam_sim_free(bsc->sim, /*free_simq*/TRUE);
265 free(bsc->sim, M_DEVBUF);
268 bs_start_timeout(bsc);
271 #endif /* __FreeBSD__ */
279 return bs_sequencer((struct bs_softc *)arg);
281 #endif /* __NetBSD__ */
283 #if defined(__DragonFly__) || defined(__FreeBSD__)
288 (void)bs_sequencer(bscdata[unit]);
290 #endif /* __FreeBSD__ */
292 /*****************************************************************
293 * JULIAN SCSI <=> BS INTERFACE
294 *****************************************************************/
295 #if !defined(__DragonFly__) && !defined(__FreeBSD__)
301 if (bp->b_bcount > BSDMABUFSIZ)
302 bp->b_bcount = BSDMABUFSIZ;
308 bs_target_open(sc, cf)
309 struct scsi_link *sc;
312 u_int target = sc->target;
313 struct bs_softc *bsc = (struct bs_softc *) (sc->adapter_softc);
314 struct targ_info *ti = bsc->sc_ti[target];
317 if ((bsc->sc_openf & (1 << target)) == 0)
320 if ((flags = cf->cf_flags) == 0)
321 flags = BS_SCSI_DEFCFG;
323 bs_setup_ctrl(ti, (u_int)sc->quirks, flags);
327 /*****************************************************************
328 * BS MEMORY ALLOCATION INTERFACE
329 *****************************************************************/
333 struct targ_info *ti;
335 struct bs_softc *bsc = ti->ti_bsc;
336 caddr_t addr, physaddr;
337 bus_dma_segment_t seg;
344 * A) total memory >= 16M at boot: MAXBSIZE * 7 = 112k.
345 * B) others: 4K * 7 = 28 K.
347 if (get_sysinfo(SYSINFO_MEMLEVEL) == MEM_LEVEL1 && cold != 0)
351 ti->bounce_size = NBPG * pages;
354 error = bus_dmamem_alloc(bsc->sc_dmat, ti->bounce_size, NBPG, 0,
355 &seg, 1, &rseg, BUS_DMA_NOWAIT);
356 if (rseg == 1 && error == 0)
357 error = bus_dmamem_map(bsc->sc_dmat, &seg, rseg,
358 ti->bounce_size, &addr, BUS_DMA_NOWAIT);
359 if (rseg != 1 || error != 0)
361 ti->bounce_size = NBPG;
362 if ((addr = malloc(NBPG, M_DEVBUF, M_NOWAIT)) == NULL)
366 physaddr = (caddr_t) vtophys(addr);
367 if ((u_int) physaddr >= RAM_END)
369 /* XXX: mem from malloc only! */
370 free(addr, M_DEVBUF);
374 ti->bounce_addr = (u_int8_t *) addr;
375 ti->bounce_phys = (u_int8_t *) physaddr;
379 bs_printf(ti, "bs_alloc_buf", "no phys bounce buffer");
380 printf("WARNING: this target is dislocated\n");
382 #endif /* __NetBSD__ */
384 #if defined(__DragonFly__) || defined(__FreeBSD__)
385 static int bs_dmarangecheck(caddr_t va, unsigned length)
387 vm_offset_t phys, priorpage = 0, endva;
389 endva = (vm_offset_t)round_page((unsigned long)(va+length));
390 for (; va < (caddr_t)endva; va += PAGE_SIZE) {
391 phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
393 panic("bs_dmarangecheck: no physical page present");
397 if (priorpage + PAGE_SIZE != phys)
407 struct targ_info *ti;
409 caddr_t addr, physaddr;
411 #if BS_BOUNCE_SIZE != 0
412 ti->bounce_size = BS_BOUNCE_SIZE;
414 ti->bounce_size = BSHW_NBPG;
416 /* Try malloc() first. It works better if it works. */
417 addr = malloc(ti->bounce_size, M_DEVBUF, M_NOWAIT);
419 if (bs_dmarangecheck(addr, ti->bounce_size) == 0) {
420 physaddr = (caddr_t) vtophys(addr);
421 ti->bounce_addr = (u_int8_t *) addr;
422 ti->bounce_phys = (u_int8_t *) physaddr;
425 free(addr, M_DEVBUF);
427 addr = contigmalloc(ti->bounce_size, M_DEVBUF, M_NOWAIT,
428 0ul, RAM_END, 1ul, 0x10000ul);
432 physaddr = (caddr_t) vtophys(addr);
433 if ((u_int) physaddr >= RAM_END)
441 ti->bounce_addr = (u_int8_t *) addr;
442 ti->bounce_phys = (u_int8_t *) physaddr;
446 bs_printf(ti, "bs_alloc_buf", "no phys bounce buffer");
447 printf("WARNING: this target is dislocated\n");
449 #endif /* __FreeBSD__ */