2 * Copyright (C) 1997 Wu Ching Chen
3 * 2.1.x update (C) 1998 Krzysztof G. Baranowski
4 * 2.5.x update (C) 2002 Red Hat <alan@redhat.com>
5 * 2.6.x update (C) 2004 Red Hat <alan@redhat.com>
7 * Marcelo Tosatti <marcelo@conectiva.com.br> : SMP fixes
9 * Wu Ching Chen : NULL pointer fixes 2000/06/02
11 * enable 32 bit fifo transfer
12 * support cdrom & remove device run ultra speed
13 * fix disconnect bug 2000/12/21
14 * support atp880 chip lvd u160 2001/05/15
15 * fix prd table bug 2001/09/12 (7.1)
17 * atp885 support add by ACARD Hao Ping Lian 2005/01/05
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
22 #include <linux/kernel.h>
23 #include <linux/types.h>
24 #include <linux/string.h>
25 #include <linux/ioport.h>
26 #include <linux/delay.h>
27 #include <linux/proc_fs.h>
28 #include <linux/spinlock.h>
29 #include <linux/pci.h>
30 #include <linux/blkdev.h>
31 #include <linux/dma-mapping.h>
32 #include <asm/system.h>
35 #include <scsi/scsi.h>
36 #include <scsi/scsi_cmnd.h>
37 #include <scsi/scsi_device.h>
38 #include <scsi/scsi_host.h>
42 static struct scsi_host_template atp870u_template;
43 static void send_s870(struct atp_unit *dev,unsigned char c);
44 static void is885(struct atp_unit *dev, unsigned int wkport,unsigned char c);
45 static void tscam_885(void);
47 static irqreturn_t atp870u_intr_handle(int irq, void *dev_id, struct pt_regs *regs)
50 unsigned short int tmpcip, id;
51 unsigned char i, j, c, target_id, lun,cmdp;
53 struct scsi_cmnd *workreq;
54 unsigned int workport, tmport, tmport1;
55 unsigned long adrcnt, k;
60 struct Scsi_Host *host = dev_id;
61 struct atp_unit *dev = (struct atp_unit *)&host->hostdata;
63 for (c = 0; c < 2; c++) {
64 tmport = dev->ioport[c] + 0x1f;
75 printk("atp870u_intr_handle enter\n");
78 cmdp = inb(dev->ioport[c] + 0x10);
79 workport = dev->ioport[c];
80 if (dev->working[c] != 0) {
81 if (dev->dev_id == ATP885_DEVID) {
82 tmport1 = workport + 0x16;
83 if ((inb(tmport1) & 0x80) == 0)
84 outb((inb(tmport1) | 0x80), tmport1);
86 tmpcip = dev->pciport[c];
87 if ((inb(tmpcip) & 0x08) != 0)
90 for (k=0; k < 1000; k++) {
91 if ((inb(tmpcip) & 0x08) == 0) {
94 if ((inb(tmpcip) & 0x01) == 0) {
100 tmpcip = dev->pciport[c];
106 if (dev->dev_id == ATP885_DEVID) {
113 target_id = inb(tmport);
117 * Remap wide devices onto id numbers
120 if ((target_id & 0x40) != 0) {
121 target_id = (target_id & 0x07) | 0x08;
126 if ((j & 0x40) != 0) {
127 if (dev->last_cmd[c] == 0xff) {
128 dev->last_cmd[c] = target_id;
130 dev->last_cmd[c] |= 0x40;
132 if (dev->dev_id == ATP885_DEVID)
133 dev->r1f[c][target_id] |= j;
135 printk("atp870u_intr_handle status = %x\n",i);
138 if ((dev->last_cmd[c] & 0xf0) != 0x40) {
139 dev->last_cmd[c] = 0xff;
141 if (dev->dev_id == ATP885_DEVID) {
144 ((unsigned char *) &adrcnt)[2] = inb(tmport++);
145 ((unsigned char *) &adrcnt)[1] = inb(tmport++);
146 ((unsigned char *) &adrcnt)[0] = inb(tmport);
147 if (dev->id[c][target_id].last_len != adrcnt)
149 k = dev->id[c][target_id].last_len;
151 dev->id[c][target_id].tran_len = k;
152 dev->id[c][target_id].last_len = adrcnt;
155 printk("tmport = %x dev->id[c][target_id].last_len = %d dev->id[c][target_id].tran_len = %d\n",tmport,dev->id[c][target_id].last_len,dev->id[c][target_id].tran_len);
162 if (dev->wide_id[c] != 0) {
163 tmport = workport + 0x1b;
165 while ((inb(tmport) & 0x01) != 0x01) {
170 * Issue more commands
172 spin_lock_irqsave(dev->host->host_lock, flags);
173 if (((dev->quhd[c] != dev->quend[c]) || (dev->last_cmd[c] != 0xff)) &&
174 (dev->in_snd[c] == 0)) {
176 printk("Call sent_s870\n");
180 spin_unlock_irqrestore(dev->host->host_lock, flags);
186 printk("Status 0x85 return\n");
192 dev->last_cmd[c] |= 0x40;
198 if ((dev->last_cmd[c] & 0xf0) != 0x40) {
199 dev->last_cmd[c] = 0xff;
203 ((unsigned char *) &adrcnt)[2] = inb(tmport++);
204 ((unsigned char *) &adrcnt)[1] = inb(tmport++);
205 ((unsigned char *) &adrcnt)[0] = inb(tmport);
206 k = dev->id[c][target_id].last_len;
208 dev->id[c][target_id].tran_len = k;
209 dev->id[c][target_id].last_len = adrcnt;
218 if (dev->dev_id == ATP885_DEVID) {
219 if ((i == 0x4c) || (i == 0x4d) || (i == 0x8c) || (i == 0x8d)) {
220 if ((i == 0x4c) || (i == 0x8c))
227 if ((i == 0x80) || (i == 0x8f)) {
229 printk(KERN_DEBUG "Device reselect\n");
233 if (cmdp == 0x44 || i==0x80) {
235 lun = inb(tmport) & 0x07;
237 if ((dev->last_cmd[c] & 0xf0) != 0x40) {
238 dev->last_cmd[c] = 0xff;
242 printk("cmdp = 0x41\n");
246 ((unsigned char *) &adrcnt)[2] = inb(tmport++);
247 ((unsigned char *) &adrcnt)[1] = inb(tmport++);
248 ((unsigned char *) &adrcnt)[0] = inb(tmport);
249 k = dev->id[c][target_id].last_len;
251 dev->id[c][target_id].tran_len = k;
252 dev->id[c][target_id].last_len = adrcnt;
259 printk("cmdp != 0x41\n");
262 dev->id[c][target_id].dirct = 0x00;
264 outb(0x00, tmport++);
265 outb(0x00, tmport++);
266 outb(0x00, tmport++);
273 if (dev->last_cmd[c] != 0xff) {
274 dev->last_cmd[c] |= 0x40;
276 if (dev->dev_id == ATP885_DEVID) {
277 j = inb(dev->baseport + 0x29) & 0xfe;
278 outb(j, dev->baseport + 0x29);
279 tmport = workport + 0x16;
281 tmport = workport + 0x10;
286 target_id = inb(tmport);
288 * Remap wide identifiers
290 if ((target_id & 0x10) != 0) {
291 target_id = (target_id & 0x07) | 0x08;
295 if (dev->dev_id == ATP885_DEVID) {
296 tmport = workport + 0x10;
299 workreq = dev->id[c][target_id].curr_req;
301 scmd_printk(KERN_DEBUG, workreq, "CDB");
302 for (l = 0; l < workreq->cmd_len; l++)
303 printk(KERN_DEBUG " %x",workreq->cmnd[l]);
307 tmport = workport + 0x0f;
310 outb(dev->id[c][target_id].devsp, tmport++);
311 adrcnt = dev->id[c][target_id].tran_len;
312 k = dev->id[c][target_id].last_len;
314 outb(((unsigned char *) &k)[2], tmport++);
315 outb(((unsigned char *) &k)[1], tmport++);
316 outb(((unsigned char *) &k)[0], tmport++);
318 printk("k %x, k[0] 0x%x k[1] 0x%x k[2] 0x%x\n", k, inb(tmport-1), inb(tmport-2), inb(tmport-3));
323 j = (j & 0x07) | 0x40;
326 j |= dev->id[c][target_id].dirct;
330 /* enable 32 bit fifo transfer */
331 if (dev->dev_id == ATP885_DEVID) {
332 tmpcip = dev->pciport[c] + 1;
333 i=inb(tmpcip) & 0xf3;
334 //j=workreq->cmnd[0];
335 if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
339 } else if ((dev->dev_id == ATP880_DEVID1) ||
340 (dev->dev_id == ATP880_DEVID2) ) {
341 tmport = workport - 0x05;
342 if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
343 outb((unsigned char) ((inb(tmport) & 0x3f) | 0xc0), tmport);
345 outb((unsigned char) (inb(tmport) & 0x3f), tmport);
348 tmport = workport + 0x3a;
349 if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
350 outb((unsigned char) ((inb(tmport) & 0xf3) | 0x08), tmport);
352 outb((unsigned char) (inb(tmport) & 0xf3), tmport);
355 tmport = workport + 0x1b;
358 id = id << target_id;
360 * Is this a wide device
362 if ((id & dev->wide_id[c]) != 0) {
366 while ((inb(tmport) & 0x01) != j) {
369 if (dev->id[c][target_id].last_len == 0) {
370 tmport = workport + 0x18;
374 printk("dev->id[c][target_id].last_len = 0\n");
379 printk("target_id = %d adrcnt = %d\n",target_id,adrcnt);
381 prd = dev->id[c][target_id].prd_pos;
382 while (adrcnt != 0) {
383 id = ((unsigned short int *)prd)[2];
390 ((unsigned short int *)prd)[2] = (unsigned short int)
392 ((unsigned long *)prd)[0] += adrcnt;
394 dev->id[c][target_id].prd_pos = prd;
397 dev->id[c][target_id].prdaddr += 0x08;
400 dev->id[c][target_id].prd_pos = prd;
404 tmpcip = dev->pciport[c] + 0x04;
405 outl(dev->id[c][target_id].prdaddr, tmpcip);
407 printk("dev->id[%d][%d].prdaddr 0x%8x\n", c, target_id, dev->id[c][target_id].prdaddr);
409 if (dev->dev_id == ATP885_DEVID) {
417 tmport = workport + 0x18;
419 * Check transfer direction
421 if (dev->id[c][target_id].dirct != 0) {
426 printk("status 0x80 return dirct != 0\n");
434 printk("status 0x80 return dirct = 0\n");
440 * Current scsi request on this target
443 workreq = dev->id[c][target_id].curr_req;
446 if ((dev->last_cmd[c] & 0xf0) != 0x40)
448 dev->last_cmd[c] = 0xff;
451 workreq->result = errstus;
455 if ((dev->last_cmd[c] & 0xf0) != 0x40) {
456 dev->last_cmd[c] = 0xff;
460 errstus = inb(tmport);
461 if (((dev->r1f[c][target_id] & 0x10) != 0)&&(dev->dev_id==ATP885_DEVID)) {
462 printk(KERN_WARNING "AEC67162 CRC ERROR !\n");
465 workreq->result = errstus;
467 if (dev->dev_id == ATP885_DEVID) {
468 j = inb(dev->baseport + 0x29) | 0x01;
469 outb(j, dev->baseport + 0x29);
472 * Complete the command
474 if (workreq->use_sg) {
475 pci_unmap_sg(dev->pdev,
476 (struct scatterlist *)workreq->request_buffer,
478 workreq->sc_data_direction);
479 } else if (workreq->request_bufflen &&
480 workreq->sc_data_direction != DMA_NONE) {
481 pci_unmap_single(dev->pdev,
482 workreq->SCp.dma_handle,
483 workreq->request_bufflen,
484 workreq->sc_data_direction);
486 spin_lock_irqsave(dev->host->host_lock, flags);
487 (*workreq->scsi_done) (workreq);
489 printk("workreq->scsi_done\n");
492 * Clear it off the queue
494 dev->id[c][target_id].curr_req = NULL;
496 spin_unlock_irqrestore(dev->host->host_lock, flags);
500 if (dev->wide_id[c] != 0) {
501 tmport = workport + 0x1b;
503 while ((inb(tmport) & 0x01) != 0x01) {
508 * If there is stuff to send and nothing going then send it
510 spin_lock_irqsave(dev->host->host_lock, flags);
511 if (((dev->last_cmd[c] != 0xff) || (dev->quhd[c] != dev->quend[c])) &&
512 (dev->in_snd[c] == 0)) {
514 printk("Call sent_s870(scsi_done)\n");
518 spin_unlock_irqrestore(dev->host->host_lock, flags);
522 if ((dev->last_cmd[c] & 0xf0) != 0x40) {
523 dev->last_cmd[c] = 0xff;
531 outl(dev->id[c][target_id].prdaddr, tmpcip);
536 tmport = workport + 0x10;
538 if (dev->dev_id == ATP885_DEVID) {
540 k = dev->id[c][target_id].last_len;
541 outb((unsigned char) (((unsigned char *) (&k))[2]), tmport++);
542 outb((unsigned char) (((unsigned char *) (&k))[1]), tmport++);
543 outb((unsigned char) (((unsigned char *) (&k))[0]), tmport);
544 dev->id[c][target_id].dirct = 0x00;
547 dev->id[c][target_id].dirct = 0x00;
557 outl(dev->id[c][target_id].prdaddr, tmpcip);
562 tmport = workport + 0x10;
564 if (dev->dev_id == ATP885_DEVID) {
566 k = dev->id[c][target_id].last_len;
567 outb((unsigned char) (((unsigned char *) (&k))[2]), tmport++);
568 outb((unsigned char) (((unsigned char *) (&k))[1]), tmport++);
569 outb((unsigned char) (((unsigned char *) (&k))[0]), tmport++);
573 outb((unsigned char) (inb(tmport) | 0x20), tmport);
574 dev->id[c][target_id].dirct = 0x20;
587 dev->id[c][target_id].dirct = 0x00;
589 outb(0x00, tmport++);
590 outb(0x00, tmport++);
591 outb(0x00, tmport++);
597 // tmport = workport + 0x17;
599 // dev->working[c] = 0;
606 printk("atp870u_intr_handle exit\n");
611 * atp870u_queuecommand - Queue SCSI command
612 * @req_p: request block
613 * @done: completion function
615 * Queue a command to the ATP queue. Called with the host lock held.
617 static int atp870u_queuecommand(struct scsi_cmnd * req_p,
618 void (*done) (struct scsi_cmnd *))
621 unsigned int tmport,m;
622 struct atp_unit *dev;
623 struct Scsi_Host *host;
625 c = scmd_channel(req_p);
626 req_p->sense_buffer[0]=0;
628 if (scmd_channel(req_p) > 1) {
629 req_p->result = 0x00040000;
632 printk("atp870u_queuecommand : req_p->device->channel > 1\n");
637 host = req_p->device->host;
638 dev = (struct atp_unit *)&host->hostdata;
643 m = m << scmd_id(req_p);
646 * Fake a timeout for missing targets
649 if ((m & dev->active_id[c]) == 0) {
650 req_p->result = 0x00040000;
656 req_p->scsi_done = done;
659 printk( "atp870u_queuecommand: done can't be NULL\n");
670 if (dev->quend[c] >= qcnt) {
677 if (dev->quhd[c] == dev->quend[c]) {
678 if (dev->quend[c] == 0) {
679 dev->quend[c] = qcnt;
682 printk("atp870u_queuecommand : dev->quhd[c] == dev->quend[c]\n");
685 req_p->result = 0x00020000;
689 dev->quereq[c][dev->quend[c]] = req_p;
690 tmport = dev->ioport[c] + 0x1c;
692 printk("dev->ioport[c] = %x inb(tmport) = %x dev->in_int[%d] = %d dev->in_snd[%d] = %d\n",dev->ioport[c],inb(tmport),c,dev->in_int[c],c,dev->in_snd[c]);
694 if ((inb(tmport) == 0) && (dev->in_int[c] == 0) && (dev->in_snd[c] == 0)) {
696 printk("Call sent_s870(atp870u_queuecommand)\n");
701 printk("atp870u_queuecommand : exit\n");
707 * send_s870 - send a command to the controller
710 * On entry there is work queued to be done. We move some of that work to the
713 * Caller holds the host lock.
715 static void send_s870(struct atp_unit *dev,unsigned char c)
718 struct scsi_cmnd *workreq;
720 unsigned char j, target_id;
722 unsigned short int tmpcip, w;
723 unsigned long l, bttl = 0;
724 unsigned int workport;
725 struct scatterlist *sgpnt;
726 unsigned long sg_count;
728 if (dev->in_snd[c] != 0) {
730 printk("cmnd in_snd\n");
735 printk("Sent_s870 enter\n");
738 if ((dev->last_cmd[c] != 0xff) && ((dev->last_cmd[c] & 0x40) != 0)) {
739 dev->last_cmd[c] &= 0x0f;
740 workreq = dev->id[c][dev->last_cmd[c]].curr_req;
741 if (workreq != NULL) { /* check NULL pointer */
744 dev->last_cmd[c] = 0xff;
745 if (dev->quhd[c] == dev->quend[c]) {
750 if ((dev->last_cmd[c] != 0xff) && (dev->working[c] != 0)) {
757 if (dev->quhd[c] >= qcnt) {
760 workreq = dev->quereq[c][dev->quhd[c]];
761 if (dev->id[c][scmd_id(workreq)].curr_req == 0) {
762 dev->id[c][scmd_id(workreq)].curr_req = workreq;
763 dev->last_cmd[c] = scmd_id(workreq);
771 workport = dev->ioport[c];
772 tmport = workport + 0x1f;
773 if ((inb(tmport) & 0xb0) != 0) {
776 tmport = workport + 0x1c;
777 if (inb(tmport) == 0) {
782 printk("Abort to Send\n");
784 dev->last_cmd[c] |= 0x40;
789 printk("OK to Send\n");
790 scmd_printk(KERN_DEBUG, workreq, "CDB");
791 for(i=0;i<workreq->cmd_len;i++) {
792 printk(" %x",workreq->cmnd[i]);
796 if (dev->dev_id == ATP885_DEVID) {
797 j = inb(dev->baseport + 0x29) & 0xfe;
798 outb(j, dev->baseport + 0x29);
799 dev->r1f[c][scmd_id(workreq)] = 0;
802 if (workreq->cmnd[0] == READ_CAPACITY) {
803 if (workreq->request_bufflen > 8) {
804 workreq->request_bufflen = 0x08;
807 if (workreq->cmnd[0] == 0x00) {
808 workreq->request_bufflen = 0;
811 tmport = workport + 0x1b;
813 target_id = scmd_id(workreq);
820 if ((w & dev->wide_id[c]) != 0) {
824 while ((inb(tmport) & 0x01) != j) {
827 printk("send_s870 while loop 1\n");
835 outb(workreq->cmd_len, tmport++);
836 outb(0x2c, tmport++);
837 if (dev->dev_id == ATP885_DEVID) {
838 outb(0x7f, tmport++);
840 outb(0xcf, tmport++);
842 for (i = 0; i < workreq->cmd_len; i++) {
843 outb(workreq->cmnd[i], tmport++);
845 tmport = workport + 0x0f;
846 outb(workreq->device->lun, tmport);
851 outb(dev->id[c][target_id].devsp, tmport++);
853 printk("dev->id[%d][%d].devsp = %2x\n",c,target_id,dev->id[c][target_id].devsp);
856 * Figure out the transfer size
858 if (workreq->use_sg) {
860 printk("Using SGL\n");
864 sgpnt = (struct scatterlist *) workreq->request_buffer;
865 sg_count = pci_map_sg(dev->pdev, sgpnt, workreq->use_sg,
866 workreq->sc_data_direction);
868 for (i = 0; i < workreq->use_sg; i++) {
869 if (sgpnt[i].length == 0 || workreq->use_sg > ATP870U_SCATTER) {
870 panic("Foooooooood fight!");
872 l += sgpnt[i].length;
875 printk( "send_s870: workreq->use_sg %d, sg_count %d l %8ld\n", workreq->use_sg, sg_count, l);
877 } else if(workreq->request_bufflen && workreq->sc_data_direction != PCI_DMA_NONE) {
879 printk("Not using SGL\n");
881 workreq->SCp.dma_handle = pci_map_single(dev->pdev, workreq->request_buffer,
882 workreq->request_bufflen,
883 workreq->sc_data_direction);
884 l = workreq->request_bufflen;
886 printk( "send_s870: workreq->use_sg %d, l %8ld\n", workreq->use_sg, l);
890 * Write transfer size
892 outb((unsigned char) (((unsigned char *) (&l))[2]), tmport++);
893 outb((unsigned char) (((unsigned char *) (&l))[1]), tmport++);
894 outb((unsigned char) (((unsigned char *) (&l))[0]), tmport++);
896 dev->id[c][j].last_len = l;
897 dev->id[c][j].tran_len = 0;
899 printk("dev->id[%2d][%2d].last_len = %d\n",c,j,dev->id[c][j].last_len);
904 if ((j & 0x08) != 0) {
905 j = (j & 0x07) | 0x40;
908 * Check transfer direction
910 if (workreq->sc_data_direction == DMA_TO_DEVICE) {
911 outb((unsigned char) (j | 0x20), tmport++);
915 outb((unsigned char) (inb(tmport) | 0x80), tmport);
917 tmport = workport + 0x1c;
918 dev->id[c][target_id].dirct = 0;
920 if (inb(tmport) == 0) {
921 tmport = workport + 0x18;
923 printk("change SCSI_CMD_REG 0x08\n");
927 dev->last_cmd[c] |= 0x40;
932 tmpcip = dev->pciport[c];
933 prd = dev->id[c][target_id].prd_table;
934 dev->id[c][target_id].prd_pos = prd;
937 * Now write the request list. Either as scatter/gather or as
941 if (workreq->use_sg) {
942 sgpnt = (struct scatterlist *) workreq->request_buffer;
944 for (j = 0; j < workreq->use_sg; j++) {
945 bttl = sg_dma_address(&sgpnt[j]);
946 l=sg_dma_len(&sgpnt[j]);
948 printk("1. bttl %x, l %x\n",bttl, l);
950 while (l > 0x10000) {
951 (((u16 *) (prd))[i + 3]) = 0x0000;
952 (((u16 *) (prd))[i + 2]) = 0x0000;
953 (((u32 *) (prd))[i >> 1]) = cpu_to_le32(bttl);
958 (((u32 *) (prd))[i >> 1]) = cpu_to_le32(bttl);
959 (((u16 *) (prd))[i + 2]) = cpu_to_le16(l);
960 (((u16 *) (prd))[i + 3]) = 0;
963 (((u16 *) (prd))[i - 1]) = cpu_to_le16(0x8000);
965 printk("prd %4x %4x %4x %4x\n",(((unsigned short int *)prd)[0]),(((unsigned short int *)prd)[1]),(((unsigned short int *)prd)[2]),(((unsigned short int *)prd)[3]));
966 printk("2. bttl %x, l %x\n",bttl, l);
970 * For a linear request write a chain of blocks
972 bttl = workreq->SCp.dma_handle;
973 l = workreq->request_bufflen;
976 printk("3. bttl %x, l %x\n",bttl, l);
978 while (l > 0x10000) {
979 (((u16 *) (prd))[i + 3]) = 0x0000;
980 (((u16 *) (prd))[i + 2]) = 0x0000;
981 (((u32 *) (prd))[i >> 1]) = cpu_to_le32(bttl);
986 (((u16 *) (prd))[i + 3]) = cpu_to_le16(0x8000);
987 (((u16 *) (prd))[i + 2]) = cpu_to_le16(l);
988 (((u32 *) (prd))[i >> 1]) = cpu_to_le32(bttl);
990 printk("prd %4x %4x %4x %4x\n",(((unsigned short int *)prd)[0]),(((unsigned short int *)prd)[1]),(((unsigned short int *)prd)[2]),(((unsigned short int *)prd)[3]));
991 printk("4. bttl %x, l %x\n",bttl, l);
997 printk("send_s870: prdaddr_2 0x%8x tmpcip %x target_id %d\n", dev->id[c][target_id].prdaddr,tmpcip,target_id);
999 dev->id[c][target_id].prdaddr = dev->id[c][target_id].prd_bus;
1000 outl(dev->id[c][target_id].prdaddr, tmpcip);
1001 tmpcip = tmpcip - 2;
1004 if (dev->dev_id == ATP885_DEVID) {
1006 j=inb(tmpcip) & 0xf3;
1007 if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) ||
1008 (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
1013 } else if ((dev->dev_id == ATP880_DEVID1) ||
1014 (dev->dev_id == ATP880_DEVID2)) {
1016 tmport = workport - 0x05;
1017 if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
1018 outb((unsigned char) ((inb(tmport) & 0x3f) | 0xc0), tmport);
1020 outb((unsigned char) (inb(tmport) & 0x3f), tmport);
1024 tmport = workport + 0x3a;
1025 if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
1026 outb((inb(tmport) & 0xf3) | 0x08, tmport);
1028 outb(inb(tmport) & 0xf3, tmport);
1031 tmport = workport + 0x1c;
1033 if(workreq->sc_data_direction == DMA_TO_DEVICE) {
1034 dev->id[c][target_id].dirct = 0x20;
1035 if (inb(tmport) == 0) {
1036 tmport = workport + 0x18;
1040 printk( "start DMA(to target)\n");
1043 dev->last_cmd[c] |= 0x40;
1048 if (inb(tmport) == 0) {
1049 tmport = workport + 0x18;
1053 printk( "start DMA(to host)\n");
1056 dev->last_cmd[c] |= 0x40;
1063 static unsigned char fun_scam(struct atp_unit *dev, unsigned short int *val)
1065 unsigned int tmport;
1066 unsigned short int i, k;
1069 tmport = dev->ioport[0] + 0x1c;
1072 for (i = 0; i < 10; i++) { /* stable >= bus settle delay(400 ns) */
1074 j = (unsigned char) (k >> 8);
1075 if ((k & 0x8000) != 0) { /* DB7 all release? */
1079 *val |= 0x4000; /* assert DB6 */
1081 *val &= 0xdfff; /* assert DB5 */
1084 for (i = 0; i < 10; i++) { /* stable >= bus settle delay(400 ns) */
1085 if ((inw(tmport) & 0x2000) != 0) { /* DB5 all release? */
1089 *val |= 0x8000; /* no DB4-0, assert DB7 */
1092 *val &= 0xbfff; /* release DB6 */
1095 for (i = 0; i < 10; i++) { /* stable >= bus settle delay(400 ns) */
1096 if ((inw(tmport) & 0x4000) != 0) { /* DB6 all release? */
1104 static void tscam(struct Scsi_Host *host)
1107 unsigned int tmport;
1108 unsigned char i, j, k;
1110 unsigned short int m, assignid_map, val;
1111 unsigned char mbuf[33], quintet[2];
1112 struct atp_unit *dev = (struct atp_unit *)&host->hostdata;
1113 static unsigned char g2q_tab[8] = {
1114 0x38, 0x31, 0x32, 0x2b, 0x34, 0x2d, 0x2e, 0x27
1117 /* I can't believe we need this before we've even done anything. Remove it
1118 * and see if anyone bitches.
1119 for (i = 0; i < 0x10; i++) {
1124 tmport = dev->ioport[0] + 1;
1125 outb(0x08, tmport++);
1127 tmport = dev->ioport[0] + 0x11;
1130 if ((dev->scam_on & 0x40) == 0) {
1134 m <<= dev->host_id[0];
1136 if (dev->chip_ver < 4) {
1141 tmport = dev->ioport[0] + 0x02;
1142 outb(0x02, tmport++); /* 2*2=4ms,3EH 2/32*3E=3.9ms */
1150 for (i = 0; i < j; i++) {
1153 if ((m & assignid_map) != 0) {
1156 tmport = dev->ioport[0] + 0x0f;
1163 k = (i & 0x07) | 0x40;
1168 tmport = dev->ioport[0] + 0x1b;
1169 if (dev->chip_ver == 4) {
1175 tmport = dev->ioport[0] + 0x18;
1179 while ((inb(tmport) & 0x80) == 0x00)
1184 if ((k == 0x85) || (k == 0x42)) {
1187 tmport = dev->ioport[0] + 0x10;
1194 tmport = dev->ioport[0] + 0x02;
1196 tmport = dev->ioport[0] + 0x1b;
1201 val = 0x0080; /* bsy */
1202 tmport = dev->ioport[0] + 0x1c;
1204 val |= 0x0040; /* sel */
1206 val |= 0x0004; /* msg */
1208 inb(0x80); /* 2 deskew delay(45ns*2=90ns) */
1209 val &= 0x007f; /* no bsy */
1212 val &= 0x00fb; /* after 1ms no msg */
1215 if ((inb(tmport) & 0x04) != 0) {
1220 for (n = 0; n < 0x30000; n++) {
1221 if ((inb(tmport) & 0x80) != 0) { /* bsy ? */
1227 for (n = 0; n < 0x30000; n++) {
1228 if ((inb(tmport) & 0x81) == 0x0081) {
1235 val |= 0x8003; /* io,cd,db7 */
1238 val &= 0x00bf; /* no sel */
1243 if ((inb(tmport) & 0x80) == 0x00) { /* bsy ? */
1246 tmport = dev->ioport[0] + 0x15;
1251 while ((inb(tmport) & 0x80) == 0)
1257 val &= 0x00ff; /* synchronization */
1259 fun_scam(dev, &val);
1261 val &= 0x00ff; /* isolation */
1263 fun_scam(dev, &val);
1268 if ((inw(tmport) & 0x2000) == 0) {
1272 val &= 0x00ff; /* get ID_STRING */
1274 k = fun_scam(dev, &val);
1275 if ((k & 0x03) == 0) {
1280 if ((k & 0x02) != 0) {
1291 TCM_5: /* isolation complete.. */
1293 printk(" \n%x %x %x %s\n ",assignid_map,mbuf[0],mbuf[1],&mbuf[2]); */
1296 if ((j & 0x20) != 0) { /* bit5=1:ID upto 7 */
1299 if ((j & 0x06) == 0) { /* IDvalid? */
1306 if ((m & assignid_map) == 0) {
1313 G2Q5: /* srch from max acceptable ID# */
1314 k = i; /* max acceptable ID# */
1318 if ((m & assignid_map) == 0) {
1325 G2Q_QUIN: /* k=binID#, */
1328 quintet[0] = 0x38; /* 1st dft ID<8 */
1330 quintet[0] = 0x31; /* 1st ID>=8 */
1333 quintet[1] = g2q_tab[k];
1335 val &= 0x00ff; /* AssignID 1stQuintet,AH=001xxxxx */
1336 m = quintet[0] << 8;
1338 fun_scam(dev, &val);
1339 val &= 0x00ff; /* AssignID 2ndQuintet,AH=001xxxxx */
1340 m = quintet[1] << 8;
1342 fun_scam(dev, &val);
1348 static void is870(struct atp_unit *dev, unsigned int wkport)
1350 unsigned int tmport;
1351 unsigned char i, j, k, rmb, n;
1352 unsigned short int m;
1353 static unsigned char mbuf[512];
1354 static unsigned char satn[9] = { 0, 0, 0, 0, 0, 0, 0, 6, 6 };
1355 static unsigned char inqd[9] = { 0x12, 0, 0, 0, 0x24, 0, 0, 0x24, 6 };
1356 static unsigned char synn[6] = { 0x80, 1, 3, 1, 0x19, 0x0e };
1357 static unsigned char synu[6] = { 0x80, 1, 3, 1, 0x0c, 0x0e };
1358 static unsigned char synw[6] = { 0x80, 1, 3, 1, 0x0c, 0x07 };
1359 static unsigned char wide[6] = { 0x80, 1, 2, 3, 1, 0 };
1361 tmport = wkport + 0x3a;
1362 outb((unsigned char) (inb(tmport) | 0x10), tmport);
1364 for (i = 0; i < 16; i++) {
1365 if ((dev->chip_ver != 4) && (i > 7)) {
1370 if ((m & dev->active_id[0]) != 0) {
1373 if (i == dev->host_id[0]) {
1374 printk(KERN_INFO " ID: %2d Host Adapter\n", dev->host_id[0]);
1377 tmport = wkport + 0x1b;
1378 if (dev->chip_ver == 4) {
1383 tmport = wkport + 1;
1384 outb(0x08, tmport++);
1385 outb(0x7f, tmport++);
1386 outb(satn[0], tmport++);
1387 outb(satn[1], tmport++);
1388 outb(satn[2], tmport++);
1389 outb(satn[3], tmport++);
1390 outb(satn[4], tmport++);
1391 outb(satn[5], tmport++);
1395 outb(dev->id[0][i].devsp, tmport++);
1397 outb(satn[6], tmport++);
1398 outb(satn[7], tmport++);
1400 if ((j & 0x08) != 0) {
1401 j = (j & 0x07) | 0x40;
1405 outb(satn[8], tmport);
1408 while ((inb(tmport) & 0x80) == 0x00)
1412 if (inb(tmport) != 0x11 && inb(tmport) != 0x8e)
1415 while (inb(tmport) != 0x8e)
1418 dev->active_id[0] |= m;
1420 tmport = wkport + 0x10;
1422 tmport = wkport + 0x04;
1426 tmport = wkport + 0x18;
1429 while ((inb(tmport) & 0x80) == 0x00)
1434 tmport = wkport + 0x10;
1439 tmport = wkport + 3;
1440 outb(inqd[0], tmport++);
1441 outb(inqd[1], tmport++);
1442 outb(inqd[2], tmport++);
1443 outb(inqd[3], tmport++);
1444 outb(inqd[4], tmport++);
1445 outb(inqd[5], tmport);
1449 outb(dev->id[0][i].devsp, tmport++);
1451 outb(inqd[6], tmport++);
1452 outb(inqd[7], tmport++);
1454 outb(inqd[8], tmport);
1457 while ((inb(tmport) & 0x80) == 0x00)
1461 if (inb(tmport) != 0x11 && inb(tmport) != 0x8e)
1464 while (inb(tmport) != 0x8e)
1467 tmport = wkport + 0x1b;
1468 if (dev->chip_ver == 4)
1471 tmport = wkport + 0x18;
1477 if ((k & 0x01) != 0) {
1479 mbuf[j++] = inb(tmport);
1483 if ((k & 0x80) == 0) {
1491 tmport = wkport + 0x10;
1501 while ((inb(tmport) & 0x80) == 0x00)
1505 if (inb(tmport) != 0x16) {
1510 printk(KERN_INFO " ID: %2d %s\n", i, &mbuf[8]);
1511 dev->id[0][i].devtype = mbuf[0];
1514 if (dev->chip_ver != 4) {
1517 if ((mbuf[7] & 0x60) == 0) {
1520 if ((dev->global_map[0] & 0x20) == 0) {
1523 tmport = wkport + 0x1b;
1525 tmport = wkport + 3;
1526 outb(satn[0], tmport++);
1527 outb(satn[1], tmport++);
1528 outb(satn[2], tmport++);
1529 outb(satn[3], tmport++);
1530 outb(satn[4], tmport++);
1531 outb(satn[5], tmport++);
1535 outb(dev->id[0][i].devsp, tmport++);
1537 outb(satn[6], tmport++);
1538 outb(satn[7], tmport++);
1540 outb(satn[8], tmport);
1543 while ((inb(tmport) & 0x80) == 0x00)
1547 if (inb(tmport) != 0x11 && inb(tmport) != 0x8e)
1550 while (inb(tmport) != 0x8e)
1555 tmport = wkport + 0x14;
1561 while ((inb(tmport) & 0x80) == 0) {
1562 if ((inb(tmport) & 0x01) != 0) {
1564 outb(wide[j++], tmport);
1570 while ((inb(tmport) & 0x80) == 0x00)
1573 j = inb(tmport) & 0x0f;
1585 tmport = wkport + 0x18;
1588 while ((inb(tmport) & 0x80) == 0) {
1589 if ((inb(tmport) & 0x01) != 0) {
1596 j = inb(tmport) & 0x0f;
1608 tmport = wkport + 0x14;
1616 if ((j & 0x01) != 0) {
1618 mbuf[k++] = inb(tmport);
1622 if ((j & 0x80) == 0x00) {
1626 j = inb(tmport) & 0x0f;
1638 tmport = wkport + 0x10;
1640 tmport = wkport + 0x14;
1646 while ((inb(tmport) & 0x80) == 0x00)
1657 if (mbuf[0] != 0x01) {
1660 if (mbuf[1] != 0x02) {
1663 if (mbuf[2] != 0x03) {
1666 if (mbuf[3] != 0x01) {
1671 dev->wide_id[0] |= m;
1673 if ((dev->id[0][i].devtype == 0x00) || (dev->id[0][i].devtype == 0x07) || ((dev->id[0][i].devtype == 0x05) && ((n & 0x10) != 0))) {
1678 tmport = wkport + 0x1b;
1680 if ((m & dev->wide_id[0]) != 0) {
1684 tmport = wkport + 3;
1685 outb(satn[0], tmport++);
1686 outb(satn[1], tmport++);
1687 outb(satn[2], tmport++);
1688 outb(satn[3], tmport++);
1689 outb(satn[4], tmport++);
1690 outb(satn[5], tmport++);
1694 outb(dev->id[0][i].devsp, tmport++);
1696 outb(satn[6], tmport++);
1697 outb(satn[7], tmport++);
1699 outb(satn[8], tmport);
1702 while ((inb(tmport) & 0x80) == 0x00)
1706 if (inb(tmport) != 0x11 && inb(tmport) != 0x8e)
1709 while (inb(tmport) != 0x8e)
1714 tmport = wkport + 0x14;
1720 while ((inb(tmport) & 0x80) == 0) {
1721 if ((inb(tmport) & 0x01) != 0) {
1723 if ((m & dev->wide_id[0]) != 0) {
1724 outb(synw[j++], tmport);
1726 if ((m & dev->ultra_map[0]) != 0) {
1727 outb(synu[j++], tmport);
1729 outb(synn[j++], tmport);
1737 while ((inb(tmport) & 0x80) == 0x00)
1740 j = inb(tmport) & 0x0f;
1752 tmport = wkport + 0x18;
1755 while ((inb(tmport) & 0x80) == 0x00) {
1756 if ((inb(tmport) & 0x01) != 0x00) {
1779 tmport = wkport + 0x14;
1787 if ((j & 0x01) != 0x00) {
1789 mbuf[k++] = inb(tmport);
1793 if ((j & 0x80) == 0x00) {
1798 while ((inb(tmport) & 0x80) == 0x00)
1817 tmport = wkport + 0x10;
1820 tmport = wkport + 0x14;
1826 while ((inb(tmport) & 0x80) == 0x00)
1834 if (mbuf[0] != 0x01) {
1837 if (mbuf[1] != 0x03) {
1840 if (mbuf[4] == 0x00) {
1843 if (mbuf[3] > 0x64) {
1846 if (mbuf[4] > 0x0c) {
1849 dev->id[0][i].devsp = mbuf[4];
1850 if ((mbuf[3] < 0x0d) && (rmb == 0)) {
1854 if (mbuf[3] < 0x1a) {
1858 if (mbuf[3] < 0x33) {
1862 if (mbuf[3] < 0x4c) {
1868 dev->id[0][i].devsp = (dev->id[0][i].devsp & 0x0f) | j;
1870 tmport = wkport + 0x3a;
1871 outb((unsigned char) (inb(tmport) & 0xef), tmport);
1874 static void is880(struct atp_unit *dev, unsigned int wkport)
1876 unsigned int tmport;
1877 unsigned char i, j, k, rmb, n, lvdmode;
1878 unsigned short int m;
1879 static unsigned char mbuf[512];
1880 static unsigned char satn[9] = { 0, 0, 0, 0, 0, 0, 0, 6, 6 };
1881 static unsigned char inqd[9] = { 0x12, 0, 0, 0, 0x24, 0, 0, 0x24, 6 };
1882 static unsigned char synn[6] = { 0x80, 1, 3, 1, 0x19, 0x0e };
1883 unsigned char synu[6] = { 0x80, 1, 3, 1, 0x0a, 0x0e };
1884 static unsigned char synw[6] = { 0x80, 1, 3, 1, 0x19, 0x0e };
1885 unsigned char synuw[6] = { 0x80, 1, 3, 1, 0x0a, 0x0e };
1886 static unsigned char wide[6] = { 0x80, 1, 2, 3, 1, 0 };
1887 static unsigned char u3[9] = { 0x80, 1, 6, 4, 0x09, 00, 0x0e, 0x01, 0x02 };
1889 lvdmode = inb(wkport + 0x3f) & 0x40;
1891 for (i = 0; i < 16; i++) {
1894 if ((m & dev->active_id[0]) != 0) {
1897 if (i == dev->host_id[0]) {
1898 printk(KERN_INFO " ID: %2d Host Adapter\n", dev->host_id[0]);
1901 tmport = wkport + 0x5b;
1903 tmport = wkport + 0x41;
1904 outb(0x08, tmport++);
1905 outb(0x7f, tmport++);
1906 outb(satn[0], tmport++);
1907 outb(satn[1], tmport++);
1908 outb(satn[2], tmport++);
1909 outb(satn[3], tmport++);
1910 outb(satn[4], tmport++);
1911 outb(satn[5], tmport++);
1915 outb(dev->id[0][i].devsp, tmport++);
1917 outb(satn[6], tmport++);
1918 outb(satn[7], tmport++);
1920 if ((j & 0x08) != 0) {
1921 j = (j & 0x07) | 0x40;
1925 outb(satn[8], tmport);
1928 while ((inb(tmport) & 0x80) == 0x00)
1932 if (inb(tmport) != 0x11 && inb(tmport) != 0x8e)
1935 while (inb(tmport) != 0x8e)
1938 dev->active_id[0] |= m;
1940 tmport = wkport + 0x50;
1942 tmport = wkport + 0x54;
1946 tmport = wkport + 0x58;
1950 while ((inb(tmport) & 0x80) == 0x00)
1956 tmport = wkport + 0x50;
1961 tmport = wkport + 0x43;
1962 outb(inqd[0], tmport++);
1963 outb(inqd[1], tmport++);
1964 outb(inqd[2], tmport++);
1965 outb(inqd[3], tmport++);
1966 outb(inqd[4], tmport++);
1967 outb(inqd[5], tmport);
1971 outb(dev->id[0][i].devsp, tmport++);
1973 outb(inqd[6], tmport++);
1974 outb(inqd[7], tmport++);
1976 outb(inqd[8], tmport);
1979 while ((inb(tmport) & 0x80) == 0x00)
1983 if (inb(tmport) != 0x11 && inb(tmport) != 0x8e)
1986 while (inb(tmport) != 0x8e)
1989 tmport = wkport + 0x5b;
1991 tmport = wkport + 0x58;
1997 if ((k & 0x01) != 0) {
1999 mbuf[j++] = inb(tmport);
2003 if ((k & 0x80) == 0) {
2011 tmport = wkport + 0x50;
2020 while ((inb(tmport) & 0x80) == 0x00)
2024 if (inb(tmport) != 0x16)
2029 printk(KERN_INFO " ID: %2d %s\n", i, &mbuf[8]);
2030 dev->id[0][i].devtype = mbuf[0];
2033 if ((mbuf[7] & 0x60) == 0) {
2036 if ((i < 8) && ((dev->global_map[0] & 0x20) == 0)) {
2042 if (dev->sp[0][i] != 0x04) // force u2
2047 tmport = wkport + 0x5b;
2049 tmport = wkport + 0x43;
2050 outb(satn[0], tmport++);
2051 outb(satn[1], tmport++);
2052 outb(satn[2], tmport++);
2053 outb(satn[3], tmport++);
2054 outb(satn[4], tmport++);
2055 outb(satn[5], tmport++);
2059 outb(dev->id[0][i].devsp, tmport++);
2061 outb(satn[6], tmport++);
2062 outb(satn[7], tmport++);
2064 outb(satn[8], tmport);
2067 while ((inb(tmport) & 0x80) == 0x00)
2072 if (inb(tmport) != 0x11 && inb(tmport) != 0x8e)
2075 while (inb(tmport) != 0x8e)
2080 tmport = wkport + 0x54;
2086 while ((inb(tmport) & 0x80) == 0) {
2087 if ((inb(tmport) & 0x01) != 0) {
2089 outb(u3[j++], tmport);
2095 while ((inb(tmport) & 0x80) == 0x00)
2098 j = inb(tmport) & 0x0f;
2110 tmport = wkport + 0x58;
2113 while ((inb(tmport) & 0x80) == 0) {
2114 if ((inb(tmport) & 0x01) != 0) {
2121 j = inb(tmport) & 0x0f;
2133 tmport = wkport + 0x54;
2141 if ((j & 0x01) != 0) {
2143 mbuf[k++] = inb(tmport);
2147 if ((j & 0x80) == 0x00) {
2151 j = inb(tmport) & 0x0f;
2163 tmport = wkport + 0x50;
2165 tmport = wkport + 0x54;
2171 while ((inb(tmport) & 0x80) == 0x00)
2182 if (mbuf[0] != 0x01) {
2185 if (mbuf[1] != 0x06) {
2188 if (mbuf[2] != 0x04) {
2191 if (mbuf[3] == 0x09) {
2194 dev->wide_id[0] |= m;
2195 dev->id[0][i].devsp = 0xce;
2199 tmport = wkport + 0x5b;
2201 tmport = wkport + 0x43;
2202 outb(satn[0], tmport++);
2203 outb(satn[1], tmport++);
2204 outb(satn[2], tmport++);
2205 outb(satn[3], tmport++);
2206 outb(satn[4], tmport++);
2207 outb(satn[5], tmport++);
2211 outb(dev->id[0][i].devsp, tmport++);
2213 outb(satn[6], tmport++);
2214 outb(satn[7], tmport++);
2216 outb(satn[8], tmport);
2219 while ((inb(tmport) & 0x80) == 0x00)
2223 if (inb(tmport) != 0x11 && inb(tmport) != 0x8e)
2226 while (inb(tmport) != 0x8e)
2231 tmport = wkport + 0x54;
2237 while ((inb(tmport) & 0x80) == 0) {
2238 if ((inb(tmport) & 0x01) != 0) {
2240 outb(wide[j++], tmport);
2245 while ((inb(tmport) & 0x80) == 0x00)
2248 j = inb(tmport) & 0x0f;
2260 tmport = wkport + 0x58;
2263 while ((inb(tmport) & 0x80) == 0) {
2264 if ((inb(tmport) & 0x01) != 0) {
2271 j = inb(tmport) & 0x0f;
2283 tmport = wkport + 0x54;
2291 if ((j & 0x01) != 0) {
2293 mbuf[k++] = inb(tmport);
2297 if ((j & 0x80) == 0x00) {
2301 j = inb(tmport) & 0x0f;
2313 tmport = wkport + 0x50;
2315 tmport = wkport + 0x54;
2321 while ((inb(tmport) & 0x80) == 0x00)
2332 if (mbuf[0] != 0x01) {
2335 if (mbuf[1] != 0x02) {
2338 if (mbuf[2] != 0x03) {
2341 if (mbuf[3] != 0x01) {
2346 dev->wide_id[0] |= m;
2348 if ((dev->id[0][i].devtype == 0x00) || (dev->id[0][i].devtype == 0x07) || ((dev->id[0][i].devtype == 0x05) && ((n & 0x10) != 0))) {
2351 if ((dev->async[0] & m) != 0) {
2357 if (dev->sp[0][i] == 0x02) {
2361 if (dev->sp[0][i] >= 0x03) {
2366 tmport = wkport + 0x5b;
2368 if ((m & dev->wide_id[0]) != 0) {
2372 tmport = wkport + 0x43;
2373 outb(satn[0], tmport++);
2374 outb(satn[1], tmport++);
2375 outb(satn[2], tmport++);
2376 outb(satn[3], tmport++);
2377 outb(satn[4], tmport++);
2378 outb(satn[5], tmport++);
2382 outb(dev->id[0][i].devsp, tmport++);
2384 outb(satn[6], tmport++);
2385 outb(satn[7], tmport++);
2387 outb(satn[8], tmport);
2390 while ((inb(tmport) & 0x80) == 0x00)
2394 if ((inb(tmport) != 0x11) && (inb(tmport) != 0x8e)) {
2397 while (inb(tmport) != 0x8e)
2402 tmport = wkport + 0x54;
2408 while ((inb(tmport) & 0x80) == 0) {
2409 if ((inb(tmport) & 0x01) != 0) {
2411 if ((m & dev->wide_id[0]) != 0) {
2412 if ((m & dev->ultra_map[0]) != 0) {
2413 outb(synuw[j++], tmport);
2415 outb(synw[j++], tmport);
2418 if ((m & dev->ultra_map[0]) != 0) {
2419 outb(synu[j++], tmport);
2421 outb(synn[j++], tmport);
2429 while ((inb(tmport) & 0x80) == 0x00)
2432 j = inb(tmport) & 0x0f;
2444 tmport = wkport + 0x58;
2447 while ((inb(tmport) & 0x80) == 0x00) {
2448 if ((inb(tmport) & 0x01) != 0x00) {
2471 tmport = wkport + 0x54;
2479 if ((j & 0x01) != 0x00) {
2481 mbuf[k++] = inb(tmport);
2485 if ((j & 0x80) == 0x00) {
2490 while ((inb(tmport) & 0x80) == 0x00)
2509 tmport = wkport + 0x50;
2512 tmport = wkport + 0x54;
2518 while ((inb(tmport) & 0x80) == 0x00)
2526 if (mbuf[0] != 0x01) {
2529 if (mbuf[1] != 0x03) {
2532 if (mbuf[4] == 0x00) {
2535 if (mbuf[3] > 0x64) {
2538 if (mbuf[4] > 0x0e) {
2541 dev->id[0][i].devsp = mbuf[4];
2542 if (mbuf[3] < 0x0c) {
2546 if ((mbuf[3] < 0x0d) && (rmb == 0)) {
2550 if (mbuf[3] < 0x1a) {
2554 if (mbuf[3] < 0x33) {
2558 if (mbuf[3] < 0x4c) {
2564 dev->id[0][i].devsp = (dev->id[0][i].devsp & 0x0f) | j;
2568 static void atp870u_free_tables(struct Scsi_Host *host)
2570 struct atp_unit *atp_dev = (struct atp_unit *)&host->hostdata;
2572 for (j=0; j < 2; j++) {
2573 for (k = 0; k < 16; k++) {
2574 if (!atp_dev->id[j][k].prd_table)
2576 pci_free_consistent(atp_dev->pdev, 1024, atp_dev->id[j][k].prd_table, atp_dev->id[j][k].prd_bus);
2577 atp_dev->id[j][k].prd_table = NULL;
2582 static int atp870u_init_tables(struct Scsi_Host *host)
2584 struct atp_unit *atp_dev = (struct atp_unit *)&host->hostdata;
2586 for(c=0;c < 2;c++) {
2588 atp_dev->id[c][k].prd_table = pci_alloc_consistent(atp_dev->pdev, 1024, &(atp_dev->id[c][k].prd_bus));
2589 if (!atp_dev->id[c][k].prd_table) {
2590 printk("atp870u_init_tables fail\n");
2591 atp870u_free_tables(host);
2594 atp_dev->id[c][k].prdaddr = atp_dev->id[c][k].prd_bus;
2595 atp_dev->id[c][k].devsp=0x20;
2596 atp_dev->id[c][k].devtype = 0x7f;
2597 atp_dev->id[c][k].curr_req = NULL;
2600 atp_dev->active_id[c] = 0;
2601 atp_dev->wide_id[c] = 0;
2602 atp_dev->host_id[c] = 0x07;
2603 atp_dev->quhd[c] = 0;
2604 atp_dev->quend[c] = 0;
2605 atp_dev->last_cmd[c] = 0xff;
2606 atp_dev->in_snd[c] = 0;
2607 atp_dev->in_int[c] = 0;
2609 for (k = 0; k < qcnt; k++) {
2610 atp_dev->quereq[c][k] = NULL;
2612 for (k = 0; k < 16; k++) {
2613 atp_dev->id[c][k].curr_req = NULL;
2614 atp_dev->sp[c][k] = 0x04;
2620 /* return non-zero on detection */
2621 static int atp870u_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2623 unsigned char k, m, c;
2624 unsigned long flags;
2625 unsigned int base_io, tmport, error,n;
2626 unsigned char host_id;
2627 struct Scsi_Host *shpnt = NULL;
2628 struct atp_unit *atpdev, *p;
2629 unsigned char setupdata[2][16];
2632 atpdev = kzalloc(sizeof(*atpdev), GFP_KERNEL);
2636 if (pci_enable_device(pdev))
2639 if (!pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
2640 printk(KERN_INFO "atp870u: use 32bit DMA mask.\n");
2642 printk(KERN_ERR "atp870u: DMA mask required but not available.\n");
2647 * It's probably easier to weed out some revisions like
2648 * this than via the PCI device table
2650 if (ent->device == PCI_DEVICE_ID_ARTOP_AEC7610) {
2651 error = pci_read_config_byte(pdev, PCI_CLASS_REVISION, &atpdev->chip_ver);
2652 if (atpdev->chip_ver < 2)
2656 switch (ent->device) {
2657 case PCI_DEVICE_ID_ARTOP_AEC7612UW:
2658 case PCI_DEVICE_ID_ARTOP_AEC7612SUW:
2662 atpdev->chip_ver = 0x04;
2666 base_io = pci_resource_start(pdev, 0);
2667 base_io &= 0xfffffff8;
2669 if ((ent->device == ATP880_DEVID1)||(ent->device == ATP880_DEVID2)) {
2670 error = pci_read_config_byte(pdev, PCI_CLASS_REVISION, &atpdev->chip_ver);
2671 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x80);//JCC082803
2673 host_id = inb(base_io + 0x39);
2676 printk(KERN_INFO " ACARD AEC-67160 PCI Ultra3 LVD Host Adapter: %d"
2677 " IO:%x, IRQ:%d.\n", count, base_io, pdev->irq);
2678 atpdev->ioport[0] = base_io + 0x40;
2679 atpdev->pciport[0] = base_io + 0x28;
2680 atpdev->dev_id = ent->device;
2681 atpdev->host_id[0] = host_id;
2683 tmport = base_io + 0x22;
2684 atpdev->scam_on = inb(tmport);
2686 atpdev->global_map[0] = inb(tmport);
2688 atpdev->ultra_map[0] = inw(tmport);
2696 outw(n, base_io + 0x34);
2698 if (inb(base_io + 0x30) == 0xff)
2701 atpdev->sp[0][m++] = inb(base_io + 0x30);
2702 atpdev->sp[0][m++] = inb(base_io + 0x31);
2703 atpdev->sp[0][m++] = inb(base_io + 0x32);
2704 atpdev->sp[0][m++] = inb(base_io + 0x33);
2705 outw(n, base_io + 0x34);
2707 atpdev->sp[0][m++] = inb(base_io + 0x30);
2708 atpdev->sp[0][m++] = inb(base_io + 0x31);
2709 atpdev->sp[0][m++] = inb(base_io + 0x32);
2710 atpdev->sp[0][m++] = inb(base_io + 0x33);
2711 outw(n, base_io + 0x34);
2713 atpdev->sp[0][m++] = inb(base_io + 0x30);
2714 atpdev->sp[0][m++] = inb(base_io + 0x31);
2715 atpdev->sp[0][m++] = inb(base_io + 0x32);
2716 atpdev->sp[0][m++] = inb(base_io + 0x33);
2717 outw(n, base_io + 0x34);
2719 atpdev->sp[0][m++] = inb(base_io + 0x30);
2720 atpdev->sp[0][m++] = inb(base_io + 0x31);
2721 atpdev->sp[0][m++] = inb(base_io + 0x32);
2722 atpdev->sp[0][m++] = inb(base_io + 0x33);
2726 outw(0, base_io + 0x34);
2727 atpdev->ultra_map[0] = 0;
2728 atpdev->async[0] = 0;
2729 for (k = 0; k < 16; k++) {
2732 if (atpdev->sp[0][k] > 1) {
2733 atpdev->ultra_map[0] |= n;
2735 if (atpdev->sp[0][k] == 0)
2736 atpdev->async[0] |= n;
2739 atpdev->async[0] = ~(atpdev->async[0]);
2740 outb(atpdev->global_map[0], base_io + 0x35);
2742 shpnt = scsi_host_alloc(&atp870u_template, sizeof(struct atp_unit));
2746 p = (struct atp_unit *)&shpnt->hostdata;
2748 atpdev->host = shpnt;
2749 atpdev->pdev = pdev;
2750 pci_set_drvdata(pdev, p);
2751 memcpy(p, atpdev, sizeof(*atpdev));
2752 if (atp870u_init_tables(shpnt) < 0) {
2753 printk(KERN_ERR "Unable to allocate tables for Acard controller\n");
2757 if (request_irq(pdev->irq, atp870u_intr_handle, IRQF_SHARED, "atp880i", shpnt)) {
2758 printk(KERN_ERR "Unable to allocate IRQ%d for Acard controller.\n", pdev->irq);
2762 spin_lock_irqsave(shpnt->host_lock, flags);
2763 tmport = base_io + 0x38;
2764 k = inb(tmport) & 0x80;
2771 tmport = base_io + 0x5b;
2775 tmport = base_io + 0x40;
2776 outb((host_id | 0x08), tmport);
2780 while ((inb(tmport) & 0x80) == 0)
2784 tmport = base_io + 0x41;
2787 tmport = base_io + 0x51;
2792 tmport = base_io + 0x38;
2795 shpnt->this_id = host_id;
2796 shpnt->unique_id = base_io;
2797 shpnt->io_port = base_io;
2798 shpnt->n_io_port = 0x60; /* Number of bytes of I/O space used */
2799 shpnt->irq = pdev->irq;
2800 } else if (ent->device == ATP885_DEVID) {
2801 printk(KERN_INFO " ACARD AEC-67162 PCI Ultra3 LVD Host Adapter: IO:%x, IRQ:%d.\n"
2802 , base_io, pdev->irq);
2804 atpdev->pdev = pdev;
2805 atpdev->dev_id = ent->device;
2806 atpdev->baseport = base_io;
2807 atpdev->ioport[0] = base_io + 0x80;
2808 atpdev->ioport[1] = base_io + 0xc0;
2809 atpdev->pciport[0] = base_io + 0x40;
2810 atpdev->pciport[1] = base_io + 0x50;
2812 shpnt = scsi_host_alloc(&atp870u_template, sizeof(struct atp_unit));
2816 p = (struct atp_unit *)&shpnt->hostdata;
2818 atpdev->host = shpnt;
2819 atpdev->pdev = pdev;
2820 pci_set_drvdata(pdev, p);
2821 memcpy(p, atpdev, sizeof(struct atp_unit));
2822 if (atp870u_init_tables(shpnt) < 0)
2826 printk("request_irq() shpnt %p hostdata %p\n", shpnt, p);
2828 if (request_irq(pdev->irq, atp870u_intr_handle, IRQF_SHARED, "atp870u", shpnt)) {
2829 printk(KERN_ERR "Unable to allocate IRQ for Acard controller.\n");
2833 spin_lock_irqsave(shpnt->host_lock, flags);
2835 c=inb(base_io + 0x29);
2836 outb((c | 0x04),base_io + 0x29);
2843 outw(n,base_io + 0x3c);
2844 if (inl(base_io + 0x38) == 0xffffffff) {
2847 for (m=0; m < 2; m++) {
2848 p->global_map[m]= 0;
2849 for (k=0; k < 4; k++) {
2850 outw(n++,base_io + 0x3c);
2851 ((unsigned long *)&setupdata[m][0])[k]=inl(base_io + 0x38);
2853 for (k=0; k < 4; k++) {
2854 outw(n++,base_io + 0x3c);
2855 ((unsigned long *)&p->sp[m][0])[k]=inl(base_io + 0x38);
2862 printk( "Flash Read OK\n");
2864 c=inb(base_io + 0x29);
2865 outb((c & 0xfb),base_io + 0x29);
2866 for (c=0;c < 2;c++) {
2869 for (k=0; k < 16; k++) {
2872 if (p->sp[c][k] > 1) {
2873 p->ultra_map[c] |= n;
2875 if (p->sp[c][k] == 0) {
2880 p->async[c] = ~(p->async[c]);
2882 if (p->global_map[c] == 0) {
2884 if ((k & 0x40) != 0)
2885 p->global_map[c] |= 0x20;
2887 p->global_map[c] |= k;
2888 if ((setupdata[c][2] & 0x04) != 0)
2889 p->global_map[c] |= 0x08;
2890 p->host_id[c] = setupdata[c][0] & 0x07;
2894 k = inb(base_io + 0x28) & 0x8f;
2896 outb(k, base_io + 0x28);
2897 outb(0x80, base_io + 0x41);
2898 outb(0x80, base_io + 0x51);
2900 outb(0, base_io + 0x41);
2901 outb(0, base_io + 0x51);
2903 inb(base_io + 0x9b);
2904 inb(base_io + 0x97);
2905 inb(base_io + 0xdb);
2906 inb(base_io + 0xd7);
2907 tmport = base_io + 0x80;
2910 k = (k & 0x07) | 0x40;
2917 while ((inb(tmport) & 0x80) == 0)
2922 tmport = base_io + 0x81;
2925 tmport = base_io + 0x91;
2928 tmport = base_io + 0xc0;
2931 k = (k & 0x07) | 0x40;
2938 while ((inb(tmport) & 0x80) == 0)
2943 tmport = base_io + 0xc1;
2946 tmport = base_io + 0xd1;
2950 printk(KERN_INFO " Scanning Channel A SCSI Device ...\n");
2951 is885(p, base_io + 0x80, 0);
2952 printk(KERN_INFO " Scanning Channel B SCSI Device ...\n");
2953 is885(p, base_io + 0xc0, 1);
2955 k = inb(base_io + 0x28) & 0xcf;
2957 outb(k, base_io + 0x28);
2958 k = inb(base_io + 0x1f) | 0x80;
2959 outb(k, base_io + 0x1f);
2960 k = inb(base_io + 0x29) | 0x01;
2961 outb(k, base_io + 0x29);
2963 //printk("atp885: atp_host[0] 0x%p\n", atp_host[0]);