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