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