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