This source file includes following definitions.
- aha1740_makecode
- aha1740_test_port
- aha1740_intr_handle
- aha1740_queuecommand
- internal_done
- aha1740_command
- aha1740_getconfig
- aha1740_detect
- aha1740_abort
- aha1740_reset
- aha1740_biosparam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #ifdef MODULE
21 #include <linux/module.h>
22 #endif
23
24 #include <linux/kernel.h>
25 #include <linux/head.h>
26 #include <linux/types.h>
27 #include <linux/string.h>
28 #include <linux/ioport.h>
29 #include <linux/proc_fs.h>
30 #include <linux/sched.h>
31 #include <asm/dma.h>
32
33 #include <asm/system.h>
34 #include <asm/io.h>
35 #include "../block/blk.h"
36 #include "scsi.h"
37 #include "hosts.h"
38 #include "sd.h"
39
40 #include "aha1740.h"
41
42
43
44
45
46 #ifdef DEBUG
47 #define DEB(x) x
48 #else
49 #define DEB(x)
50 #endif
51
52
53
54
55
56 static unsigned int slot, base;
57 static unsigned char irq_level;
58
59 static struct ecb ecb[AHA1740_ECBS];
60
61 static int aha1740_last_ecb_used = 0;
62
63 int aha1740_makecode(unchar *sense, unchar *status)
64 {
65 struct statusword
66 {
67 ushort don:1,
68 du:1,
69 :1, qf:1,
70 sc:1,
71 dor:1,
72 ch:1,
73 intr:1,
74 asa:1,
75 sns:1,
76 :1, ini:1,
77 me:1,
78 :1, eca:1,
79 :1;
80 } status_word;
81 int retval = DID_OK;
82
83 status_word = * (struct statusword *) status;
84 #ifdef DEBUG
85 printk("makecode from %x,%x,%x,%x %x,%x,%x,%x",status[0],status[1],status[2],status[3],
86 sense[0],sense[1],sense[2],sense[3]);
87 #endif
88 if (!status_word.don)
89 {
90 if ( (status[1]&0x18) || status_word.sc )
91 {
92
93 switch ( status[2] )
94 {
95 case 0x12:
96 if ( status_word.dor )
97 retval=DID_ERROR;
98
99 case 0x00:
100 break;
101 case 0x11:
102 case 0x21:
103 retval=DID_TIME_OUT;
104 break;
105 case 0x0a:
106 retval=DID_BAD_TARGET;
107 break;
108 case 0x04:
109 case 0x05:
110 retval=DID_ABORT;
111
112 break;
113 default:
114 retval=DID_ERROR;
115 }
116 }
117 else
118 {
119 if ( status_word.qf )
120 {
121 retval = DID_TIME_OUT;
122
123 printk("aha1740.c: WARNING: AHA1740 queue overflow!\n");
124 }
125 else if ( status[0]&0x60 )
126 {
127 retval = DID_ERROR;
128 }
129
130
131
132 }
133 }
134
135 return status[3] | retval << 16;
136 }
137
138 int aha1740_test_port(void)
139 {
140 char name[4],tmp;
141
142
143 name[0]= 'A' -1 + ((tmp = inb(HID0)) >> 2);
144 name[1]= 'A' -1 + ((tmp & 3) << 3);
145 name[1]+= ((tmp = inb(HID1)) >> 5)&0x7;
146 name[2]= 'A' -1 + (tmp & 0x1f);
147 name[3]=0;
148 tmp = inb(HID2);
149 if ( strcmp ( name, HID_MFG ) || inb(HID2) != HID_PRD )
150 return 0;
151
152
153
154
155
156 if ( inb(EBCNTRL) != EBCNTRL_VALUE )
157 {
158 printk("aha1740: Board detected, but EBCNTRL = %x, so disabled it.\n",
159 inb(EBCNTRL));
160 return 0;
161 }
162
163 if ( inb(PORTADR) & PORTADDR_ENH )
164 return 1;
165
166 printk("aha1740: Board detected, but not in enhanced mode, so disabled it.\n");
167 return 0;
168 }
169
170
171 void aha1740_intr_handle(int irq, struct pt_regs * regs)
172 {
173 void (*my_done)(Scsi_Cmnd *);
174 int errstatus, adapstat;
175 int number_serviced;
176 struct ecb *ecbptr;
177 Scsi_Cmnd *SCtmp;
178
179 number_serviced = 0;
180
181 while(inb(G2STAT) & G2STAT_INTPEND)
182 {
183 DEB(printk("aha1740_intr top of loop.\n"));
184 adapstat = inb(G2INTST);
185 ecbptr = (struct ecb *) bus_to_virt(inl(MBOXIN0));
186 outb(G2CNTRL_IRST,G2CNTRL);
187
188 switch ( adapstat & G2INTST_MASK )
189 {
190 case G2INTST_CCBRETRY:
191 case G2INTST_CCBERROR:
192 case G2INTST_CCBGOOD:
193 outb(G2CNTRL_HRDY,G2CNTRL);
194 if (!ecbptr)
195 {
196 printk("Aha1740 null ecbptr in interrupt (%x,%x,%x,%d)\n",
197 inb(G2STAT),adapstat,inb(G2INTST),number_serviced++);
198 continue;
199 }
200 SCtmp = ecbptr->SCpnt;
201 if (!SCtmp)
202 {
203 printk("Aha1740 null SCtmp in interrupt (%x,%x,%x,%d)\n",
204 inb(G2STAT),adapstat,inb(G2INTST),number_serviced++);
205 continue;
206 }
207 if (SCtmp->host_scribble)
208 scsi_free(SCtmp->host_scribble, 512);
209
210
211
212 if ( (adapstat & G2INTST_MASK) == G2INTST_CCBERROR )
213 {
214 memcpy(SCtmp->sense_buffer, ecbptr->sense,
215 sizeof(SCtmp->sense_buffer));
216 errstatus = aha1740_makecode(ecbptr->sense,ecbptr->status);
217 }
218 else
219 errstatus = 0;
220 DEB(if (errstatus) printk("aha1740_intr_handle: returning %6x\n", errstatus));
221 SCtmp->result = errstatus;
222 my_done = ecbptr->done;
223 memset(ecbptr,0,sizeof(struct ecb));
224 if ( my_done )
225 my_done(SCtmp);
226 break;
227 case G2INTST_HARDFAIL:
228 printk("aha1740 hardware failure!\n");
229 panic("aha1740.c");
230 case G2INTST_ASNEVENT:
231 printk("aha1740 asynchronous event: %02x %02x %02x %02x %02x\n",adapstat,
232 inb(MBOXIN0),inb(MBOXIN1),inb(MBOXIN2),inb(MBOXIN3));
233 outb(G2CNTRL_HRDY,G2CNTRL);
234 break;
235 case G2INTST_CMDGOOD:
236
237 break;
238 case G2INTST_CMDERROR:
239
240 break;
241 }
242 number_serviced++;
243 }
244 }
245
246 int aha1740_queuecommand(Scsi_Cmnd * SCpnt, void (*done)(Scsi_Cmnd *))
247 {
248 unchar direction;
249 unchar *cmd = (unchar *) SCpnt->cmnd;
250 unchar target = SCpnt->target;
251 unsigned long flags;
252 void *buff = SCpnt->request_buffer;
253 int bufflen = SCpnt->request_bufflen;
254 int ecbno;
255 DEB(int i);
256
257
258 if(*cmd == REQUEST_SENSE)
259 {
260 if (bufflen != sizeof(SCpnt->sense_buffer))
261 {
262 printk("Wrong buffer length supplied for request sense (%d)\n",bufflen);
263 }
264 SCpnt->result = 0;
265 done(SCpnt);
266 return 0;
267 }
268
269 #ifdef DEBUG
270 if (*cmd == READ_10 || *cmd == WRITE_10)
271 i = xscsi2int(cmd+2);
272 else if (*cmd == READ_6 || *cmd == WRITE_6)
273 i = scsi2int(cmd+2);
274 else
275 i = -1;
276 printk("aha1740_queuecommand: dev %d cmd %02x pos %d len %d ", target, *cmd, i, bufflen);
277 printk("scsi cmd:");
278 for (i = 0; i < SCpnt->cmd_len; i++) printk("%02x ", cmd[i]);
279 printk("\n");
280 #endif
281
282
283
284 save_flags(flags);
285 cli();
286 ecbno = aha1740_last_ecb_used + 1;
287 if (ecbno >= AHA1740_ECBS) ecbno = 0;
288
289 do{
290 if( ! ecb[ecbno].cmdw )
291 break;
292 ecbno++;
293 if (ecbno >= AHA1740_ECBS ) ecbno = 0;
294 } while (ecbno != aha1740_last_ecb_used);
295
296 if( ecb[ecbno].cmdw )
297 panic("Unable to find empty ecb for aha1740.\n");
298
299 ecb[ecbno].cmdw = AHA1740CMD_INIT;
300
301 aha1740_last_ecb_used = ecbno;
302 restore_flags(flags);
303
304 #ifdef DEBUG
305 printk("Sending command (%d %x)...",ecbno, done);
306 #endif
307
308 ecb[ecbno].cdblen = SCpnt->cmd_len;
309
310 direction = 0;
311 if (*cmd == READ_10 || *cmd == READ_6)
312 direction = 1;
313 else if (*cmd == WRITE_10 || *cmd == WRITE_6)
314 direction = 0;
315
316 memcpy(ecb[ecbno].cdb, cmd, ecb[ecbno].cdblen);
317
318 if (SCpnt->use_sg)
319 {
320 struct scatterlist * sgpnt;
321 struct aha1740_chain * cptr;
322 int i;
323 #ifdef DEBUG
324 unsigned char * ptr;
325 #endif
326 ecb[ecbno].sg = 1;
327 SCpnt->host_scribble = (unsigned char *) scsi_malloc(512);
328 sgpnt = (struct scatterlist *) SCpnt->request_buffer;
329 cptr = (struct aha1740_chain *) SCpnt->host_scribble;
330 if (cptr == NULL) panic("aha1740.c: unable to allocate DMA memory\n");
331 for(i=0; i<SCpnt->use_sg; i++)
332 {
333 cptr[i].dataptr = (long) sgpnt[i].address;
334 cptr[i].datalen = sgpnt[i].length;
335 }
336 ecb[ecbno].datalen = SCpnt->use_sg * sizeof(struct aha1740_chain);
337 ecb[ecbno].dataptr = (long) cptr;
338 #ifdef DEBUG
339 printk("cptr %x: ",cptr);
340 ptr = (unsigned char *) cptr;
341 for(i=0;i<24;i++) printk("%02x ", ptr[i]);
342 #endif
343 }
344 else
345 {
346 SCpnt->host_scribble = NULL;
347 ecb[ecbno].datalen = bufflen;
348 ecb[ecbno].dataptr = (long) buff;
349 }
350 ecb[ecbno].lun = SCpnt->lun;
351 ecb[ecbno].ses = 1;
352 ecb[ecbno].dir= direction;
353 ecb[ecbno].ars=1;
354 ecb[ecbno].senselen = 12;
355 ecb[ecbno].senseptr = (long) ecb[ecbno].sense;
356 ecb[ecbno].statusptr = (long) ecb[ecbno].status;
357 ecb[ecbno].done = done;
358 ecb[ecbno].SCpnt = SCpnt;
359 #ifdef DEBUG
360 {
361 int i;
362 printk("aha1740_command: sending.. ");
363 for (i = 0; i < sizeof(ecb[ecbno])-10; i++)
364 printk("%02x ", ((unchar *)&ecb[ecbno])[i]);
365 }
366 printk("\n");
367 #endif
368 if (done)
369 {
370
371
372
373
374
375
376
377
378
379 DEB(printk("aha1740[%d] critical section\n",ecbno));
380 save_flags(flags);
381 cli();
382 if ( ! (inb(G2STAT) & G2STAT_MBXOUT) )
383 {
384 printk("aha1740[%d]_mbxout wait!\n",ecbno);
385 cli();
386 }
387 mb();
388 while ( ! (inb(G2STAT) & G2STAT_MBXOUT) );
389 outl(virt_to_bus(ecb+ecbno), MBOXOUT0);
390 if ( inb(G2STAT) & G2STAT_BUSY )
391 {
392 printk("aha1740[%d]_attn wait!\n",ecbno);
393 cli();
394 }
395 while ( inb(G2STAT) & G2STAT_BUSY );
396 outb(ATTN_START | (target & 7), ATTN);
397 restore_flags(flags);
398 DEB(printk("aha1740[%d] request queued.\n",ecbno));
399 }
400 else
401 printk("aha1740_queuecommand: done can't be NULL\n");
402
403 return 0;
404 }
405
406 static volatile int internal_done_flag = 0;
407 static volatile int internal_done_errcode = 0;
408
409 static void internal_done(Scsi_Cmnd * SCpnt)
410 {
411 internal_done_errcode = SCpnt->result;
412 ++internal_done_flag;
413 }
414
415 int aha1740_command(Scsi_Cmnd * SCpnt)
416 {
417 aha1740_queuecommand(SCpnt, internal_done);
418
419 while (!internal_done_flag);
420 internal_done_flag = 0;
421 return internal_done_errcode;
422 }
423
424
425
426
427 void aha1740_getconfig(void)
428 {
429 static int intab[] = { 9,10,11,12,0,14,15,0 };
430
431 irq_level = intab [ inb(INTDEF)&0x7 ];
432 outb(inb(INTDEF) | 0x10, INTDEF);
433 }
434
435 int aha1740_detect(Scsi_Host_Template * tpnt)
436 {
437 memset(&ecb, 0, sizeof(struct ecb));
438 DEB(printk("aha1740_detect: \n"));
439
440 for ( slot=MINEISA; slot <= MAXEISA; slot++ )
441 {
442 base = SLOTBASE(slot);
443
444
445
446
447
448 if(check_region(base, 0x5c)) continue;
449 if ( aha1740_test_port()) break;
450 }
451 if ( slot > MAXEISA )
452 return 0;
453
454 aha1740_getconfig();
455
456 if ( (inb(G2STAT) & (G2STAT_MBXOUT | G2STAT_BUSY) ) != G2STAT_MBXOUT )
457 {
458 outb(G2CNTRL_HRST,G2CNTRL);
459 outb(0,G2CNTRL);
460 }
461
462 printk("Configuring Adaptec at IO:%x, IRQ %d\n",base,
463 irq_level);
464
465 DEB(printk("aha1740_detect: enable interrupt channel %d\n", irq_level));
466
467 if (request_irq(irq_level,aha1740_intr_handle, 0, "aha1740"))
468 {
469 printk("Unable to allocate IRQ for adaptec controller.\n");
470 return 0;
471 }
472 request_region(base, 0x5c,"aha1740");
473 return 1;
474 }
475
476
477
478
479
480
481
482
483
484 int aha1740_abort(Scsi_Cmnd * SCpnt)
485 {
486 DEB(printk("aha1740_abort called\n"));
487 return SCSI_ABORT_SNOOZE;
488 }
489
490
491
492
493
494 int aha1740_reset(Scsi_Cmnd * SCpnt)
495 {
496 DEB(printk("aha1740_reset called\n"));
497 return SCSI_RESET_PUNT;
498 }
499
500 int aha1740_biosparam(Disk * disk, int dev, int* ip)
501 {
502 int size = disk->capacity;
503 DEB(printk("aha1740_biosparam\n"));
504 ip[0] = 64;
505 ip[1] = 32;
506 ip[2] = size >> 11;
507
508 return 0;
509 }
510
511 #ifdef MODULE
512
513 Scsi_Host_Template driver_template = AHA1740;
514
515 #include "scsi_module.c"
516 #endif
517
518
519
520