1 /************************************************************
2  *                                                          *
3  *               Linux EATA SCSI PIO driver                 *
4  *                                                          *
5  *  based on the CAM document CAM/89-004 rev. 2.0c,         *
6  *  DPT's driver kit, some internal documents and source,   *
7  *  and several other Linux scsi drivers and kernel docs.   *
8  *                                                          *
9  *  The driver currently:                                   *
10  *      -supports all EATA-PIO boards                       *
11  *      -only supports DASD devices                         *
12  *                                                          *
13  *  (c)1993-96 Michael Neuffer, Alfred Arnold               *
14  *             neuffer@goofy.zdv.uni-mainz.de               *
15  *             a.arnold@kfa-juelich.de                      *
16  *                                                          *
17  *  This program is free software; you can redistribute it  *
18  *  and/or modify it under the terms of the GNU General     *
19  *  Public License as published by the Free Software        *
20  *  Foundation; either version 2 of the License, or         *
21  *  (at your option) any later version.                     *
22  *                                                          *
23  *  This program is distributed in the hope that it will be *
24  *  useful, but WITHOUT ANY WARRANTY; without even the      *
25  *  implied warranty of MERCHANTABILITY or FITNESS FOR A    *
26  *  PARTICULAR PURPOSE.  See the GNU General Public License *
27  *  for more details.                                       *
28  *                                                          *
29  *  You should have received a copy of the GNU General      *
30  *  Public License along with this kernel; if not, write to *
31  *  the Free Software Foundation, Inc., 675 Mass Ave,       *
32  *  Cambridge, MA 02139, USA.                               *
33  *                                                          *
34  ************************************************************
35  *  last change: 96/07/16                  OS: Linux 2.0.8  *
36  ************************************************************/
37 
38 /* Look in eata_pio.h for configuration information */
39 
40 #include <linux/module.h>
41 
42 #include <linux/kernel.h>
43 #include <linux/sched.h>
44 #include <linux/string.h>
45 #include <linux/ioport.h>
46 #include <linux/slab.h>
47 #include <linux/in.h>
48 #include <linux/pci.h>
49 #include <linux/proc_fs.h>
50 #include <asm/io.h>
51 #include "eata_pio.h"
52 #include "eata_dma_proc.h"
53 #include "scsi.h"
54 #include "sd.h"
55 
56 #include <linux/stat.h>
57 #include <linux/config.h>	/* for CONFIG_PCI */
58 #include <linux/blk.h>
59 #include <linux/spinlock.h>
60 
61 static uint ISAbases[MAXISA] =
62 {0x1F0, 0x170, 0x330, 0x230};
63 static uint ISAirqs[MAXISA] =
64 {14,12,15,11};
65 static unchar EISAbases[] =
66 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
67 static uint registered_HBAs = 0;
68 static struct Scsi_Host *last_HBA = NULL;
69 static struct Scsi_Host *first_HBA = NULL;
70 static unchar reg_IRQ[] =
71 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
72 static unchar reg_IRQL[] =
73 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
74 
75 static ulong int_counter = 0;
76 static ulong queue_counter = 0;
77 
78 #include "eata_pio_proc.c"
79 
80 #ifdef MODULE
eata_pio_release(struct Scsi_Host * sh)81 int eata_pio_release(struct Scsi_Host *sh)
82 {
83     if (sh->irq && reg_IRQ[sh->irq] == 1) free_irq(sh->irq, NULL);
84     else reg_IRQ[sh->irq]--;
85     if (SD(sh)->channel == 0) {
86 	if (sh->io_port && sh->n_io_port)
87 	    release_region(sh->io_port, sh->n_io_port);
88     }
89     return(TRUE);
90 }
91 #endif
92 
IncStat(Scsi_Pointer * SCp,uint Increment)93 void IncStat(Scsi_Pointer *SCp, uint Increment)
94 {
95     SCp->ptr+=Increment;
96     if ((SCp->this_residual-=Increment)==0)
97     {
98 	if ((--SCp->buffers_residual)==0) SCp->Status=FALSE;
99 	else
100 	{
101 	    SCp->buffer++;
102 	    SCp->ptr=SCp->buffer->address;
103 	    SCp->this_residual=SCp->buffer->length;
104 	}
105     }
106 }
107 
108 void eata_pio_int_handler(int irq, void *dev_id, struct pt_regs * regs);
109 
do_eata_pio_int_handler(int irq,void * dev_id,struct pt_regs * regs)110 void do_eata_pio_int_handler(int irq, void *dev_id, struct pt_regs * regs)
111 {
112     unsigned long flags;
113 
114     spin_lock_irqsave(&io_request_lock, flags);
115     eata_pio_int_handler(irq, dev_id, regs);
116     spin_unlock_irqrestore(&io_request_lock, flags);
117 }
118 
eata_pio_int_handler(int irq,void * dev_id,struct pt_regs * regs)119 void eata_pio_int_handler(int irq, void *dev_id, struct pt_regs * regs)
120 {
121     uint eata_stat = 0xfffff;
122     Scsi_Cmnd *cmd;
123     hostdata *hd;
124     struct eata_ccb *cp;
125     uint base;
126     ulong flags;
127     uint x,z;
128     struct Scsi_Host *sh;
129     ushort zwickel=0;
130     unchar stat,odd;
131 
132     save_flags(flags);
133     cli();
134 
135     for (x = 1, sh = first_HBA; x <= registered_HBAs; x++, sh = SD(sh)->prev) {
136 	if (sh->irq != irq)
137 	    continue;
138 	if (inb((uint)sh->base + HA_RSTATUS) & HA_SBUSY)
139 	    continue;
140 
141 	int_counter++;
142 
143 	hd=SD(sh);
144 
145 	cp = &hd->ccb[0];
146 	cmd = cp->cmd;
147 	base = (uint) cmd->host->base;
148 
149 	do
150 	{
151 	    stat=inb(base+HA_RSTATUS);
152 	    if (stat&HA_SDRQ) {
153 		if (cp->DataIn)
154 		{
155 		    z=256; odd=FALSE;
156 		    while ((cmd->SCp.Status)&&((z>0)||(odd)))
157 		    {
158 			if (odd)
159 			{
160 			    *(cmd->SCp.ptr)=zwickel>>8;
161 			    IncStat(&cmd->SCp,1);
162 			    odd=FALSE;
163 			}
164 			x=min_t(unsigned int,z,cmd->SCp.this_residual/2);
165 			insw(base+HA_RDATA,cmd->SCp.ptr,x);
166 			z-=x;
167 			IncStat(&cmd->SCp,2*x);
168 			if ((z>0)&&(cmd->SCp.this_residual==1))
169 			{
170 			    zwickel=inw(base+HA_RDATA);
171 			    *(cmd->SCp.ptr)=zwickel&0xff;
172 			    IncStat(&cmd->SCp,1); z--;
173 			    odd=TRUE;
174 			}
175 		    }
176 		    while (z>0) {
177 			zwickel=inw(base+HA_RDATA);
178 			z--;
179 		    }
180 		}
181 		else /* cp->DataOut */
182 		{
183 		    odd=FALSE; z=256;
184 		    while ((cmd->SCp.Status)&&((z>0)||(odd)))
185 		    {
186 			if (odd)
187 			{
188 			    zwickel+=*(cmd->SCp.ptr)<<8;
189 			    IncStat(&cmd->SCp,1);
190 			    outw(zwickel,base+HA_RDATA);
191 			    z--;
192 			    odd=FALSE;
193 			}
194 			x=min_t(unsigned int,z,cmd->SCp.this_residual/2);
195 			outsw(base+HA_RDATA,cmd->SCp.ptr,x);
196 			z-=x;
197 			IncStat(&cmd->SCp,2*x);
198 			if ((z>0)&&(cmd->SCp.this_residual==1))
199 			{
200 			    zwickel=*(cmd->SCp.ptr);
201 			    zwickel&=0xff;
202 			    IncStat(&cmd->SCp,1);
203 			    odd=TRUE;
204 			}
205 		    }
206 		    while (z>0||odd) {
207 			outw(zwickel,base+HA_RDATA);
208 			z--;
209 			odd=FALSE;
210 		    }
211 		}
212 	    }
213 	}
214 	while ((stat&HA_SDRQ)||((stat&HA_SMORE)&&hd->moresupport));
215 
216 	/* terminate handler if HBA goes busy again, i.e. transfers
217 	 * more data */
218 
219 	if (stat&HA_SBUSY) break;
220 
221 	/* OK, this is quite stupid, but I haven't found any correct
222 	 * way to get HBA&SCSI status so far */
223 
224 	if (!(inb(base+HA_RSTATUS)&HA_SERROR))
225 	{
226 	    cmd->result=(DID_OK<<16);
227 	    hd->devflags|=(1<<cp->cp_id);
228 	}
229 	else if (hd->devflags&1<<cp->cp_id)
230 	    cmd->result=(DID_OK<<16)+0x02;
231 	else cmd->result=(DID_NO_CONNECT<<16);
232 
233 	if (cp->status == LOCKED) {
234 	    cp->status = FREE;
235 	    eata_stat = inb(base + HA_RSTATUS);
236 	    printk(KERN_NOTICE "eata_pio: int_handler, freeing locked "
237                    "queueslot\n");
238 	    DBG(DBG_INTR&&DBG_DELAY,DELAY(1));
239 	    restore_flags(flags);
240 	    return;
241 	}
242 
243 #if DBG_INTR2
244 	if (stat != 0x50)
245 	    printk(KERN_DEBUG "stat: %#.2x, result: %#.8x\n", stat,
246                    cmd->result);
247 	DBG(DBG_INTR&&DBG_DELAY,DELAY(1));
248 #endif
249 
250 	cp->status = FREE;   /* now we can release the slot  */
251 
252 	restore_flags(flags);
253 	cmd->scsi_done(cmd);
254 	save_flags(flags);
255 	cli();
256     }
257     restore_flags(flags);
258 
259     return;
260 }
261 
eata_pio_send_command(uint base,unchar command)262 inline uint eata_pio_send_command(uint base, unchar command)
263 {
264     uint loop = HZ/2;
265 
266     while (inb(base + HA_RSTATUS) & HA_SBUSY)
267 	if (--loop == 0)
268 	    return(TRUE);
269 
270     /* Enable interrupts for HBA.  It is not the best way to do it at this
271      * place, but I hope that it doesn't interfere with the IDE driver
272      * initialization this way */
273 
274     outb(HA_CTRL_8HEADS,base+HA_CTRLREG);
275 
276     outb(command, base + HA_WCOMMAND);
277     return(FALSE);
278 }
279 
eata_pio_queue(Scsi_Cmnd * cmd,void (* done)(Scsi_Cmnd *))280 int eata_pio_queue(Scsi_Cmnd * cmd, void (*done) (Scsi_Cmnd *))
281 {
282     uint x, y;
283     long flags;
284     uint base;
285 
286     hostdata *hd;
287     struct Scsi_Host *sh;
288     struct eata_ccb *cp;
289 
290     save_flags(flags);
291     cli();
292 
293     queue_counter++;
294 
295     hd = HD(cmd);
296     sh = cmd->host;
297     base = (uint) sh->base;
298 
299     /* use only slot 0, as 2001 can handle only one cmd at a time */
300 
301     y = x = 0;
302 
303     if (hd->ccb[y].status!=FREE) {
304 
305 	DBG(DBG_QUEUE, printk(KERN_EMERG "can_queue %d, x %d, y %d\n",
306                               sh->can_queue,x,y));
307 #if DEBUG_EATA
308 	panic(KERN_EMERG "eata_pio: run out of queue slots cmdno:%ld "
309               "intrno: %ld\n", queue_counter, int_counter);
310 #else
311 	panic(KERN_EMERG "eata_pio: run out of queue slots....\n");
312 #endif
313     }
314 
315     cp = &hd->ccb[y];
316 
317     memset(cp, 0, sizeof(struct eata_ccb));
318     memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
319 
320     cp->status = USED;      /* claim free slot */
321 
322     DBG(DBG_QUEUE, printk(KERN_DEBUG "eata_pio_queue pid %ld, target: %x, lun:"
323                           " %x, y %d\n", cmd->pid, cmd->target, cmd->lun, y));
324     DBG(DBG_QUEUE && DBG_DELAY, DELAY(1));
325 
326     cmd->scsi_done = (void *)done;
327 
328     switch (cmd->cmnd[0]) {
329     case CHANGE_DEFINITION: case COMPARE:         case COPY:
330     case COPY_VERIFY:       case LOG_SELECT:      case MODE_SELECT:
331     case MODE_SELECT_10:    case SEND_DIAGNOSTIC: case WRITE_BUFFER:
332     case FORMAT_UNIT:       case REASSIGN_BLOCKS: case RESERVE:
333     case SEARCH_EQUAL:      case SEARCH_HIGH:     case SEARCH_LOW:
334     case WRITE_6:           case WRITE_10:        case WRITE_VERIFY:
335     case UPDATE_BLOCK:      case WRITE_LONG:      case WRITE_SAME:
336     case SEARCH_HIGH_12:    case SEARCH_EQUAL_12: case SEARCH_LOW_12:
337     case WRITE_12:          case WRITE_VERIFY_12: case SET_WINDOW:
338     case MEDIUM_SCAN:       case SEND_VOLUME_TAG:
339     case 0xea:      /* alternate number for WRITE LONG */
340 	cp->DataOut = TRUE; /* Output mode */
341 	break;
342     case TEST_UNIT_READY:
343     default:
344 	cp->DataIn = TRUE;  /* Input mode  */
345     }
346 
347     cp->Interpret = (cmd->target == hd->hostid);
348     cp->cp_datalen = htonl((ulong)cmd->request_bufflen);
349     cp->Auto_Req_Sen = FALSE;
350     cp->cp_reqDMA = htonl(0);
351     cp->reqlen = 0;
352 
353     cp->cp_id = cmd->target;
354     cp->cp_lun = cmd->lun;
355     cp->cp_dispri = FALSE;
356     cp->cp_identify = TRUE;
357     memcpy(cp->cp_cdb, cmd->cmnd, COMMAND_SIZE(*cmd->cmnd));
358 
359     cp->cp_statDMA = htonl(0);
360 
361     cp->cp_viraddr = cp;
362     cp->cmd = cmd;
363     cmd->host_scribble = (char *)&hd->ccb[y];
364 
365     if (cmd->use_sg == 0)
366     {
367 	cmd->SCp.buffers_residual=1;
368 	cmd->SCp.ptr = cmd->request_buffer;
369 	cmd->SCp.this_residual = cmd->request_bufflen;
370 	cmd->SCp.buffer = NULL;
371     } else {
372 	cmd->SCp.buffer = cmd->request_buffer;
373 	cmd->SCp.buffers_residual = cmd->use_sg;
374 	cmd->SCp.ptr = cmd->SCp.buffer->address;
375 	cmd->SCp.this_residual = cmd->SCp.buffer->length;
376     }
377     cmd->SCp.Status = (cmd->SCp.this_residual != 0);  /* TRUE as long as bytes
378                                                        * are to transfer */
379 
380     if (eata_pio_send_command(base, EATA_CMD_PIO_SEND_CP))
381     {
382 	cmd->result = DID_BUS_BUSY << 16;
383 	printk(KERN_NOTICE "eata_pio_queue target %d, pid %ld, HBA busy, "
384                "returning DID_BUS_BUSY, done.\n", cmd->target, cmd->pid);
385         done(cmd);
386         cp->status = FREE;
387         restore_flags(flags);
388 	return (0);
389     }
390     while (!(inb(base + HA_RSTATUS) & HA_SDRQ));
391     outsw(base + HA_RDATA, cp, hd->cplen);
392     outb(EATA_CMD_PIO_TRUNC, base + HA_WCOMMAND);
393     for (x = 0; x < hd->cppadlen; x++) outw(0, base + HA_RDATA);
394 
395     DBG(DBG_QUEUE,printk(KERN_DEBUG "Queued base %#.4lx pid: %ld target: %x "
396                          "lun: %x slot %d irq %d\n", (long)sh->base, cmd->pid,
397 			 cmd->target, cmd->lun, y, sh->irq));
398     DBG(DBG_QUEUE && DBG_DELAY, DELAY(1));
399 
400     restore_flags(flags);
401     return (0);
402 }
403 
eata_pio_abort(Scsi_Cmnd * cmd)404 int eata_pio_abort(Scsi_Cmnd * cmd)
405 {
406     ulong flags;
407     uint loop = HZ;
408 
409     save_flags(flags);
410     cli();
411 
412     DBG(DBG_ABNORM, printk(KERN_WARNING "eata_pio_abort called pid: %ld "
413                            "target: %x lun: %x reason %x\n", cmd->pid,
414                            cmd->target, cmd->lun, cmd->abort_reason));
415     DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
416 
417 
418     while (inb((uint)(cmd->host->base) + HA_RAUXSTAT) & HA_ABUSY)
419 	if (--loop == 0) {
420 	    printk(KERN_WARNING "eata_pio: abort, timeout error.\n");
421 	    restore_flags(flags);
422 	    DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
423 	    return (SCSI_ABORT_ERROR);
424 	}
425     if (CD(cmd)->status == FREE) {
426 	DBG(DBG_ABNORM, printk(KERN_WARNING "Returning: SCSI_ABORT_NOT_RUNNING\n"));
427 	restore_flags(flags);
428 	return (SCSI_ABORT_NOT_RUNNING);
429     }
430     if (CD(cmd)->status == USED) {
431 	DBG(DBG_ABNORM, printk(KERN_WARNING "Returning: SCSI_ABORT_BUSY\n"));
432 	restore_flags(flags);
433 	return (SCSI_ABORT_BUSY);  /* SNOOZE */
434     }
435     if (CD(cmd)->status == RESET) {
436 	restore_flags(flags);
437 	printk(KERN_WARNING "eata_pio: abort, command reset error.\n");
438 	DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
439 	return (SCSI_ABORT_ERROR);
440     }
441     if (CD(cmd)->status == LOCKED) {
442 	restore_flags(flags);
443 	DBG(DBG_ABNORM, printk(KERN_WARNING "eata_pio: abort, queue slot "
444                                "locked.\n"));
445 	DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
446 	return (SCSI_ABORT_NOT_RUNNING);
447     }
448     restore_flags(flags);
449     panic("eata_pio: abort: invalid slot status\n");
450 }
451 
eata_pio_reset(Scsi_Cmnd * cmd,unsigned int dummy)452 int eata_pio_reset(Scsi_Cmnd * cmd, unsigned int dummy)
453 {
454     uint x, time, limit = 0;
455     ulong flags;
456     unchar success = FALSE;
457     Scsi_Cmnd *sp;
458 
459     save_flags(flags);
460     cli();
461     DBG(DBG_ABNORM, printk(KERN_WARNING "eata_pio_reset called pid:%ld target:"
462                            " %x lun: %x reason %x\n", cmd->pid, cmd->target,
463                            cmd->lun, cmd->abort_reason));
464 
465     if (HD(cmd)->state == RESET) {
466 	printk(KERN_WARNING "eata_pio_reset: exit, already in reset.\n");
467 	restore_flags(flags);
468 	DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
469 	return (SCSI_RESET_ERROR);
470     }
471 
472     /* force all slots to be free */
473 
474     for (x = 0; x < cmd->host->can_queue; x++) {
475 
476 	if (HD(cmd)->ccb[x].status == FREE)
477 	    continue;
478 
479 	sp = HD(cmd)->ccb[x].cmd;
480 	HD(cmd)->ccb[x].status = RESET;
481 	printk(KERN_WARNING "eata_pio_reset: slot %d in reset, pid %ld.\n", x,
482                sp->pid);
483 	DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
484 
485 	if (sp == NULL)
486 	    panic("eata_pio_reset: slot %d, sp==NULL.\n", x);
487 	DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
488     }
489 
490     /* hard reset the HBA  */
491     outb(EATA_CMD_RESET, (uint) cmd->host->base+HA_WCOMMAND);
492 
493     DBG(DBG_ABNORM, printk(KERN_WARNING "eata_pio_reset: board reset done.\n"));
494     HD(cmd)->state = RESET;
495 
496     time = jiffies;
497     while (time_before(jiffies, time + 3 * HZ) && limit++ < 10000000);
498 
499     DBG(DBG_ABNORM, printk(KERN_WARNING "eata_pio_reset: interrupts disabled, "
500                            "loops %d.\n", limit));
501     DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
502 
503     for (x = 0; x < cmd->host->can_queue; x++) {
504 
505 	/* Skip slots already set free by interrupt */
506 	if (HD(cmd)->ccb[x].status != RESET)
507 	    continue;
508 
509 	sp = HD(cmd)->ccb[x].cmd;
510 	sp->result = DID_RESET << 16;
511 
512 	/* This mailbox is terminated */
513 	printk(KERN_WARNING "eata_pio_reset: reset ccb %d.\n",x);
514 	HD(cmd)->ccb[x].status = FREE;
515 
516 	restore_flags(flags);
517 	sp->scsi_done(sp);
518 	cli();
519     }
520 
521     HD(cmd)->state = FALSE;
522     restore_flags(flags);
523 
524     if (success) { /* hmmm... */
525 	DBG(DBG_ABNORM, printk(KERN_WARNING "eata_pio_reset: exit, success.\n"));
526 	DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
527 	return (SCSI_RESET_SUCCESS);
528     } else {
529 	DBG(DBG_ABNORM, printk(KERN_WARNING "eata_pio_reset: exit, wakeup.\n"));
530 	DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
531 	return (SCSI_RESET_PUNT);
532     }
533 }
534 
get_pio_board_data(ulong base,uint irq,uint id,ulong cplen,ushort cppadlen)535 char * get_pio_board_data(ulong base, uint irq, uint id, ulong cplen, ushort cppadlen)
536 {
537     struct eata_ccb cp;
538     static char buff[256];
539     int z;
540 
541     memset(&cp, 0, sizeof(struct eata_ccb));
542     memset(buff, 0, sizeof(buff));
543 
544     cp.DataIn = TRUE;
545     cp.Interpret = TRUE;   /* Interpret command */
546 
547     cp.cp_datalen = htonl(254);
548     cp.cp_dataDMA = htonl(0);
549 
550     cp.cp_id = id;
551     cp.cp_lun = 0;
552 
553     cp.cp_cdb[0] = INQUIRY;
554     cp.cp_cdb[1] = 0;
555     cp.cp_cdb[2] = 0;
556     cp.cp_cdb[3] = 0;
557     cp.cp_cdb[4] = 254;
558     cp.cp_cdb[5] = 0;
559 
560     if (eata_pio_send_command((uint) base, EATA_CMD_PIO_SEND_CP))
561         return (NULL);
562     while (!(inb(base + HA_RSTATUS) & HA_SDRQ));
563     outsw(base + HA_RDATA, &cp, cplen);
564     outb(EATA_CMD_PIO_TRUNC, base + HA_WCOMMAND);
565     for (z = 0; z < cppadlen; z++) outw(0, base + HA_RDATA);
566 
567     while (inb(base + HA_RSTATUS) & HA_SBUSY);
568     if (inb(base + HA_RSTATUS) & HA_SERROR)
569 	return (NULL);
570     else if (!(inb(base + HA_RSTATUS) & HA_SDRQ))
571 	return (NULL);
572     else
573     {
574 	insw(base+HA_RDATA, &buff, 127);
575 	while (inb(base + HA_RSTATUS)&HA_SDRQ) inw(base + HA_RDATA);
576 	return (buff);
577     }
578 }
579 
get_pio_conf_PIO(u32 base,struct get_conf * buf)580 int get_pio_conf_PIO(u32 base, struct get_conf *buf)
581 {
582     ulong loop = HZ/2;
583     int z;
584     ushort *p;
585 
586     if(check_region(base, 9))
587 	return (FALSE);
588 
589     memset(buf, 0, sizeof(struct get_conf));
590 
591     while (inb(base + HA_RSTATUS) & HA_SBUSY)
592 	if (--loop == 0)
593 	    return (FALSE);
594 
595     DBG(DBG_PIO && DBG_PROBE,
596 	printk(KERN_DEBUG "Issuing PIO READ CONFIG to HBA at %#x\n", base));
597     eata_pio_send_command(base, EATA_CMD_PIO_READ_CONFIG);
598 
599     loop = HZ/2;
600     for (p = (ushort *) buf;
601 	 (long)p <= ((long)buf + (sizeof(struct get_conf) / 2)); p++) {
602 	while (!(inb(base + HA_RSTATUS) & HA_SDRQ))
603 	    if (--loop == 0)
604 		return (FALSE);
605 
606 	loop = HZ/2;
607 	*p = inw(base + HA_RDATA);
608     }
609     if (!(inb(base + HA_RSTATUS) & HA_SERROR)) {            /* Error ? */
610 	if (htonl(EATA_SIGNATURE) == buf->signature) {
611 	    DBG(DBG_PIO&&DBG_PROBE, printk(KERN_NOTICE "EATA Controller found "
612                                            "at %#4x EATA Level: %x\n", base,
613 					   (uint) (buf->version)));
614 
615 	    while (inb(base + HA_RSTATUS) & HA_SDRQ)
616 		inw(base + HA_RDATA);
617 	    if(ALLOW_DMA_BOARDS == FALSE) {
618 		for (z = 0; z < MAXISA; z++)
619 		    if (base == ISAbases[z]) {
620 			buf->IRQ = ISAirqs[z];
621 			break;
622 		    }
623 	    }
624 	    return (TRUE);
625 	}
626     } else {
627 	DBG(DBG_PROBE, printk("eata_dma: get_conf_PIO, error during transfer "
628 			      "for HBA at %x\n", base));
629     }
630     return (FALSE);
631 }
632 
print_pio_config(struct get_conf * gc)633 void print_pio_config(struct get_conf *gc)
634 {
635     printk("Please check values: (read config data)\n");
636     printk("LEN: %d ver:%d OCS:%d TAR:%d TRNXFR:%d MORES:%d\n",
637 	   (uint) ntohl(gc->len), gc->version,
638 	   gc->OCS_enabled, gc->TAR_support, gc->TRNXFR, gc->MORE_support);
639     printk("HAAV:%d SCSIID0:%d ID1:%d ID2:%d QUEUE:%d SG:%d SEC:%d\n",
640 	   gc->HAA_valid, gc->scsi_id[3], gc->scsi_id[2],
641 	   gc->scsi_id[1], ntohs(gc->queuesiz), ntohs(gc->SGsiz), gc->SECOND);
642     printk("IRQ:%d IRQT:%d FORCADR:%d MCH:%d RIDQ:%d\n",
643 	   gc->IRQ, gc->IRQ_TR, gc->FORCADR,
644 	   gc->MAX_CHAN, gc->ID_qest);
645     DBG(DPT_DEBUG, DELAY(14));
646 }
647 
print_selftest(uint base)648 static uint print_selftest(uint base)
649 {
650     unchar buffer[512];
651 #ifdef VERBOSE_SETUP
652     int z;
653 #endif
654 
655     printk("eata_pio: executing controller self test & setup...\n");
656     while (inb(base + HA_RSTATUS) & HA_SBUSY);
657     outb(EATA_CMD_PIO_SETUPTEST, base + HA_WCOMMAND);
658     do {
659 	while (inb(base + HA_RSTATUS) & HA_SBUSY)
660 	    /* nothing */ ;
661 	if (inb(base + HA_RSTATUS) & HA_SDRQ)
662 	{
663 	    insw(base + HA_RDATA, &buffer, 256);
664 #ifdef VERBOSE_SETUP
665 	    /* no beeps please... */
666 	    for (z = 0; z < 511 && buffer[z]; z++)
667 		if (buffer[z] != 7) printk("%c", buffer[z]);
668 #endif
669 	}
670     } while (inb(base+HA_RSTATUS) & (HA_SBUSY|HA_SDRQ));
671 
672     return (!(inb(base+HA_RSTATUS) & HA_SERROR));
673 }
674 
register_pio_HBA(long base,struct get_conf * gc,Scsi_Host_Template * tpnt)675 int register_pio_HBA(long base, struct get_conf *gc, Scsi_Host_Template * tpnt)
676 {
677     ulong size = 0;
678     char *buff;
679     ulong cplen;
680     ushort cppadlen;
681     struct Scsi_Host *sh;
682     hostdata *hd;
683 
684     DBG(DBG_REGISTER, print_pio_config(gc));
685 
686     if (gc->DMA_support == TRUE) {
687 	printk("HBA at %#.4lx supports DMA. Please use EATA-DMA driver.\n",base);
688 	if(ALLOW_DMA_BOARDS == FALSE)
689 	    return (FALSE);
690     }
691 
692     if ((buff = get_pio_board_data((uint)base, gc->IRQ, gc->scsi_id[3],
693 			       cplen   =(htonl(gc->cplen   )+1)/2,
694 			       cppadlen=(htons(gc->cppadlen)+1)/2)) == NULL)
695     {
696 	printk("HBA at %#lx didn't react on INQUIRY. Sorry.\n", (ulong) base);
697 	return (FALSE);
698     }
699 
700     if (print_selftest(base) == FALSE && ALLOW_DMA_BOARDS == FALSE)
701     {
702 	printk("HBA at %#lx failed while performing self test & setup.\n",
703 	       (ulong) base);
704 	return (FALSE);
705     }
706 
707     if (!reg_IRQ[gc->IRQ]) {    /* Interrupt already registered ? */
708 	if (!request_irq(gc->IRQ, do_eata_pio_int_handler, SA_INTERRUPT,
709 			 "EATA-PIO", NULL)){
710 	    reg_IRQ[gc->IRQ]++;
711 	    if (!gc->IRQ_TR)
712 		reg_IRQL[gc->IRQ] = TRUE;   /* IRQ is edge triggered */
713 	} else {
714 	    printk("Couldn't allocate IRQ %d, Sorry.\n", gc->IRQ);
715 	    return (FALSE);
716 	}
717     } else {            /* More than one HBA on this IRQ */
718 	if (reg_IRQL[gc->IRQ] == TRUE) {
719 	    printk("Can't support more than one HBA on this IRQ,\n"
720 		   "  if the IRQ is edge triggered. Sorry.\n");
721 	    return (FALSE);
722 	} else
723 	    reg_IRQ[gc->IRQ]++;
724     }
725 
726     request_region(base, 8, "eata_pio");
727 
728     size = sizeof(hostdata) + (sizeof(struct eata_ccb) * ntohs(gc->queuesiz));
729 
730     sh = scsi_register(tpnt, size);
731     if(sh == NULL)
732     {
733     	release_region(base, 8);
734     	return FALSE;
735     }
736 
737     hd = SD(sh);
738 
739     memset(hd->ccb, 0, (sizeof(struct eata_ccb) * ntohs(gc->queuesiz)));
740     memset(hd->reads, 0, sizeof(ulong) * 26);
741 
742     strncpy(SD(sh)->vendor, &buff[8], 8);
743     SD(sh)->vendor[8] = 0;
744     strncpy(SD(sh)->name, &buff[16], 17);
745     SD(sh)->name[17] = 0;
746     SD(sh)->revision[0] = buff[32];
747     SD(sh)->revision[1] = buff[33];
748     SD(sh)->revision[2] = buff[34];
749     SD(sh)->revision[3] = '.';
750     SD(sh)->revision[4] = buff[35];
751     SD(sh)->revision[5] = 0;
752 
753     switch (ntohl(gc->len)) {
754     case 0x1c:
755 	SD(sh)->EATA_revision = 'a';
756 	break;
757     case 0x1e:
758 	SD(sh)->EATA_revision = 'b';
759 	break;
760     case 0x22:
761 	SD(sh)->EATA_revision = 'c';
762 	break;
763     case 0x24:
764 	SD(sh)->EATA_revision = 'z';
765     default:
766 	SD(sh)->EATA_revision = '?';
767     }
768 
769     if(ntohl(gc->len) >= 0x22) {
770 	if (gc->is_PCI == TRUE)
771 	    hd->bustype = IS_PCI;
772 	else if (gc->is_EISA == TRUE)
773 	    hd->bustype = IS_EISA;
774 	else
775 	    hd->bustype = IS_ISA;
776     } else {
777 	if (buff[21] == '4')
778 	    hd->bustype = IS_PCI;
779 	else if (buff[21] == '2')
780 	    hd->bustype = IS_EISA;
781 	else
782 	    hd->bustype = IS_ISA;
783     }
784 
785     SD(sh)->cplen=cplen;
786     SD(sh)->cppadlen=cppadlen;
787     SD(sh)->hostid=gc->scsi_id[3];
788     SD(sh)->devflags=1<<gc->scsi_id[3];
789     SD(sh)->moresupport=gc->MORE_support;
790     sh->unique_id = base;
791     sh->base = base;
792     sh->io_port = base;
793     sh->n_io_port = 8;
794     sh->irq = gc->IRQ;
795     sh->dma_channel = PIO;
796     sh->this_id = gc->scsi_id[3];
797     sh->can_queue = 1;
798     sh->cmd_per_lun = 1;
799     sh->sg_tablesize = SG_ALL;
800 
801     hd->channel = 0;
802 
803     sh->max_id = 8;
804     sh->max_lun = 8;
805 
806     if (gc->SECOND)
807 	hd->primary = FALSE;
808     else
809 	hd->primary = TRUE;
810 
811     sh->unchecked_isa_dma = FALSE; /* We can only do PIO */
812 
813     hd->next = NULL;    /* build a linked list of all HBAs */
814     hd->prev = last_HBA;
815     if(hd->prev != NULL)
816 	SD(hd->prev)->next = sh;
817     last_HBA = sh;
818     if (first_HBA == NULL)
819 	first_HBA = sh;
820     registered_HBAs++;
821     return (1);
822 }
823 
find_pio_ISA(struct get_conf * buf,Scsi_Host_Template * tpnt)824 void find_pio_ISA(struct get_conf *buf, Scsi_Host_Template * tpnt)
825 {
826     int i;
827 
828     for (i = 0; i < MAXISA; i++) {
829 	if (ISAbases[i]) {
830 	    if (get_pio_conf_PIO(ISAbases[i], buf) == TRUE){
831 		register_pio_HBA(ISAbases[i], buf, tpnt);
832 	    }
833 	    ISAbases[i] = 0;
834 	}
835     }
836     return;
837 }
838 
find_pio_EISA(struct get_conf * buf,Scsi_Host_Template * tpnt)839 void find_pio_EISA(struct get_conf *buf, Scsi_Host_Template * tpnt)
840 {
841     u32 base;
842     int i;
843 
844 #if CHECKPAL
845     u8 pal1, pal2, pal3;
846 #endif
847 
848     for (i = 0; i < MAXEISA; i++) {
849 	if (EISAbases[i] == TRUE) { /* Still a possibility ?          */
850 
851 	    base = 0x1c88 + (i * 0x1000);
852 #if CHECKPAL
853 	    pal1 = inb((u16)base - 8);
854 	    pal2 = inb((u16)base - 7);
855 	    pal3 = inb((u16)base - 6);
856 
857 	    if (((pal1 == 0x12) && (pal2 == 0x14)) ||
858 		((pal1 == 0x38) && (pal2 == 0xa3) && (pal3 == 0x82)) ||
859 		((pal1 == 0x06) && (pal2 == 0x94) && (pal3 == 0x24))) {
860 		DBG(DBG_PROBE, printk(KERN_NOTICE "EISA EATA id tags found: "
861                                       "%x %x %x \n",
862 				      (int)pal1, (int)pal2, (int)pal3));
863 #endif
864 		if (get_pio_conf_PIO(base, buf) == TRUE) {
865 		    DBG(DBG_PROBE && DBG_EISA, print_pio_config(buf));
866 		    if (buf->IRQ) {
867 			register_pio_HBA(base, buf, tpnt);
868 		    } else
869 			printk(KERN_NOTICE "eata_dma: No valid IRQ. HBA "
870                                "removed from list\n");
871 		}
872 		/* Nothing found here so we take it from the list */
873 		EISAbases[i] = 0;
874 #if CHECKPAL
875 	    }
876 #endif
877 	}
878     }
879     return;
880 }
881 
find_pio_PCI(struct get_conf * buf,Scsi_Host_Template * tpnt)882 void find_pio_PCI(struct get_conf *buf, Scsi_Host_Template * tpnt)
883 {
884 #ifndef CONFIG_PCI
885     printk("eata_dma: kernel PCI support not enabled. Skipping scan for PCI HBAs.\n");
886 #else
887     struct pci_dev *dev = NULL;
888     u32 base, x;
889 
890     while ((dev = pci_find_device(PCI_VENDOR_ID_DPT, PCI_DEVICE_ID_DPT, dev)) != NULL) {
891 	    DBG(DBG_PROBE && DBG_PCI,
892 		printk("eata_pio: find_PCI, HBA at %s\n", dev->name));
893 	    if (pci_enable_device(dev))
894 	    	continue;
895 	    pci_set_master(dev);
896 	    base = pci_resource_flags(dev, 0);
897 	    if (base & IORESOURCE_MEM) {
898 		printk("eata_pio: invalid base address of device %s\n", dev->name);
899 		continue;
900 	    }
901 	    base = pci_resource_start(dev, 0);
902             /* EISA tag there ? */
903 	    if ((inb(base) == 0x12) && (inb(base + 1) == 0x14))
904 		continue;   /* Jep, it's forced, so move on  */
905 	    base += 0x10;   /* Now, THIS is the real address */
906 	    if (base != 0x1f8) {
907 		/* We didn't find it in the primary search */
908 		if (get_pio_conf_PIO(base, buf) == TRUE) {
909 		    if (buf->FORCADR)   /* If the address is forced */
910 			continue;       /* we'll find it later      */
911 
912 		    /* OK. We made it till here, so we can go now
913 		     * and register it. We  only have to check and
914 		     * eventually remove it from the EISA and ISA list
915 		     */
916 
917 		    register_pio_HBA(base, buf, tpnt);
918 
919 		    if (base < 0x1000) {
920 			for (x = 0; x < MAXISA; ++x) {
921 			    if (ISAbases[x] == base) {
922 				ISAbases[x] = 0;
923 				break;
924 			    }
925 			}
926 		    } else if ((base & 0x0fff) == 0x0c88) {
927 			x = (base >> 12) & 0x0f;
928 			EISAbases[x] = 0;
929 		    }
930 		}
931 #if CHECK_BLINK
932 		else if (check_blink_state(base) == TRUE) {
933 		    printk("eata_pio: HBA is in BLINK state.\n"
934 			   "Consult your HBAs manual to correct this.\n");
935 		}
936 #endif
937 	    }
938 	}
939 #endif /* #ifndef CONFIG_PCI */
940 }
941 
942 
eata_pio_detect(Scsi_Host_Template * tpnt)943 int eata_pio_detect(Scsi_Host_Template * tpnt)
944 {
945     struct Scsi_Host *HBA_ptr;
946     struct get_conf gc;
947     int i;
948 
949     DBG((DBG_PROBE && DBG_DELAY) || DPT_DEBUG,
950 	printk("Using lots of delays to let you read the debugging output\n"));
951 
952     tpnt->proc_name = "eata_pio";
953 
954     find_pio_PCI(&gc, tpnt);
955 
956     find_pio_EISA(&gc, tpnt);
957 
958     find_pio_ISA(&gc, tpnt);
959 
960     for (i = 0; i <= MAXIRQ; i++)
961 	if (reg_IRQ[i])
962 	    request_irq(i, do_eata_pio_int_handler, SA_INTERRUPT, "EATA-PIO", NULL);
963 
964     HBA_ptr = first_HBA;
965 
966     if (registered_HBAs != 0) {
967 	printk("EATA (Extended Attachment) PIO driver version: %d.%d%s\n"
968 	       "(c) 1993-95 Michael Neuffer, neuffer@goofy.zdv.uni-mainz.de\n"
969 	       "            Alfred Arnold,   a.arnold@kfa-juelich.de\n"
970 	       "This release only supports DASD devices (harddisks)\n",
971 	       VER_MAJOR, VER_MINOR, VER_SUB);
972 
973 	printk("Registered HBAs:\n");
974 	printk("HBA no. Boardtype: Revis: EATA: Bus: BaseIO: IRQ: Ch: ID: Pr:"
975                " QS: SG: CPL:\n");
976 	for (i = 1; i <= registered_HBAs; i++) {
977 	    printk("scsi%-2d: %.10s v%s 2.0%c  %s %#.4x   %2d   %d   %d   %c"
978                    "  %2d  %2d  %2d\n",
979 		   HBA_ptr->host_no, SD(HBA_ptr)->name, SD(HBA_ptr)->revision,
980 		   SD(HBA_ptr)->EATA_revision, (SD(HBA_ptr)->bustype == 'P')?
981 		   "PCI ":(SD(HBA_ptr)->bustype == 'E')?"EISA":"ISA ",
982 		   (uint) HBA_ptr->base, HBA_ptr->irq, SD(HBA_ptr)->channel,
983                    HBA_ptr->this_id, (SD(HBA_ptr)->primary == TRUE)?'Y':'N',
984 		   HBA_ptr->can_queue, HBA_ptr->sg_tablesize,
985                    HBA_ptr->cmd_per_lun);
986 	    HBA_ptr = SD(HBA_ptr)->next;
987 	}
988     }
989     DBG(DPT_DEBUG,DELAY(12));
990 
991     return (registered_HBAs);
992 }
993 
994 /* Eventually this will go into an include file, but this will be later */
995 static Scsi_Host_Template driver_template = EATA_PIO;
996 
997 #include "scsi_module.c"
998 MODULE_LICENSE("GPL");
999 
1000 /*
1001  * Overrides for Emacs so that we almost follow Linus's tabbing style.
1002  * Emacs will notice this stuff at the end of the file and automatically
1003  * adjust the settings for this buffer only.  This must remain at the end
1004  * of the file.
1005  * ---------------------------------------------------------------------------
1006  * Local variables:
1007  * c-indent-level: 4
1008  * c-brace-imaginary-offset: 0
1009  * c-brace-offset: -4
1010  * c-argdecl-indent: 4
1011  * c-label-offset: -4
1012  * c-continued-statement-offset: 4
1013  * c-continued-brace-offset: 0
1014  * indent-tabs-mode: nil
1015  * tab-width: 8
1016  * End:
1017  */
1018