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