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