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