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