commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.git] / drivers / scsi / mac53c94.c
1 /*
2  * SCSI low-level driver for the 53c94 SCSI bus adaptor found
3  * on Power Macintosh computers, controlling the external SCSI chain.
4  * We assume the 53c94 is connected to a DBDMA (descriptor-based DMA)
5  * controller.
6  *
7  * Paul Mackerras, August 1996.
8  * Copyright (C) 1996 Paul Mackerras.
9  */
10 #include <linux/kernel.h>
11 #include <linux/delay.h>
12 #include <linux/types.h>
13 #include <linux/string.h>
14 #include <linux/slab.h>
15 #include <linux/blkdev.h>
16 #include <linux/proc_fs.h>
17 #include <linux/stat.h>
18 #include <linux/spinlock.h>
19 #include <linux/interrupt.h>
20 #include <asm/dbdma.h>
21 #include <asm/io.h>
22 #include <asm/pgtable.h>
23 #include <asm/prom.h>
24 #include <asm/system.h>
25 #include <asm/pci-bridge.h>
26
27 #include "scsi.h"
28 #include "hosts.h"
29 #include "mac53c94.h"
30
31 enum fsc_phase {
32         idle,
33         selecting,
34         dataing,
35         completing,
36         busfreeing,
37 };
38
39 struct fsc_state {
40         volatile struct mac53c94_regs *regs;
41         int     intr;
42         volatile struct dbdma_regs *dma;
43         int     dmaintr;
44         int     clk_freq;
45         struct  Scsi_Host *host;
46         struct  fsc_state *next;
47         Scsi_Cmnd *request_q;
48         Scsi_Cmnd *request_qtail;
49         Scsi_Cmnd *current_req;         /* req we're currently working on */
50         enum fsc_phase phase;           /* what we're currently trying to do */
51         struct dbdma_cmd *dma_cmds;     /* space for dbdma commands, aligned */
52         void    *dma_cmd_space;
53         struct  pci_dev *pdev;
54         dma_addr_t dma_addr;
55 };
56
57 static struct fsc_state *all_53c94s;
58
59 static void mac53c94_init(struct fsc_state *);
60 static void mac53c94_start(struct fsc_state *);
61 static void mac53c94_interrupt(int, void *, struct pt_regs *);
62 static irqreturn_t do_mac53c94_interrupt(int, void *, struct pt_regs *);
63 static void cmd_done(struct fsc_state *, int result);
64 static void set_dma_cmds(struct fsc_state *, Scsi_Cmnd *);
65 static int data_goes_out(Scsi_Cmnd *);
66
67 int
68 mac53c94_detect(Scsi_Host_Template *tp)
69 {
70         struct device_node *node;
71         int nfscs;
72         struct fsc_state *state, **prev_statep;
73         struct Scsi_Host *host;
74         void *dma_cmd_space;
75         unsigned char *clkprop;
76         int proplen;
77         struct pci_dev *pdev;
78         u8 pbus, devfn;
79
80         nfscs = 0;
81         prev_statep = &all_53c94s;
82         for (node = find_devices("53c94"); node != 0; node = node->next) {
83                 if (node->n_addrs != 2 || node->n_intrs != 2) {
84                         printk(KERN_ERR "mac53c94: expected 2 addrs and intrs"
85                                " (got %d/%d) for node %s\n",
86                                node->n_addrs, node->n_intrs, node->full_name);
87                         continue;
88                 }
89
90                 pdev = NULL;
91                 if (node->parent != NULL
92                     && !pci_device_from_OF_node(node->parent, &pbus, &devfn))
93                         pdev = pci_find_slot(pbus, devfn);
94                 if (pdev == NULL) {
95                         printk(KERN_ERR "mac53c94: can't find PCI device "
96                                "for %s\n", node->full_name);
97                         continue;
98                 }
99
100                 host = scsi_register(tp, sizeof(struct fsc_state));
101                 if (host == NULL)
102                         break;
103                 host->unique_id = nfscs;
104
105                 state = (struct fsc_state *) host->hostdata;
106                 if (state == 0) {
107                         /* "can't happen" */
108                         printk(KERN_ERR "mac53c94: no state for %s?!\n",
109                                node->full_name);
110                         scsi_unregister(host);
111                         break;
112                 }
113                 state->host = host;
114                 state->pdev = pdev;
115
116                 state->regs = (volatile struct mac53c94_regs *)
117                         ioremap(node->addrs[0].address, 0x1000);
118                 state->intr = node->intrs[0].line;
119                 state->dma = (volatile struct dbdma_regs *)
120                         ioremap(node->addrs[1].address, 0x1000);
121                 state->dmaintr = node->intrs[1].line;
122                 if (state->regs == NULL || state->dma == NULL) {
123                         printk(KERN_ERR "mac53c94: ioremap failed for %s\n",
124                                node->full_name);
125                         if (state->dma != NULL)
126                                 iounmap(state->dma);
127                         if (state->regs != NULL)
128                                 iounmap(state->regs);
129                         scsi_unregister(host);
130                         break;
131                 }
132
133                 clkprop = get_property(node, "clock-frequency", &proplen);
134                 if (clkprop == NULL || proplen != sizeof(int)) {
135                         printk(KERN_ERR "%s: can't get clock frequency, "
136                                "assuming 25MHz\n", node->full_name);
137                         state->clk_freq = 25000000;
138                 } else
139                         state->clk_freq = *(int *)clkprop;
140
141                 /* Space for dma command list: +1 for stop command,
142                    +1 to allow for aligning. */
143                 dma_cmd_space = kmalloc((host->sg_tablesize + 2) *
144                                         sizeof(struct dbdma_cmd), GFP_KERNEL);
145                 if (dma_cmd_space == 0) {
146                         printk(KERN_ERR "mac53c94: couldn't allocate dma "
147                                "command space for %s\n", node->full_name);
148                         goto err_cleanup;
149                 }
150                 state->dma_cmds = (struct dbdma_cmd *)
151                         DBDMA_ALIGN(dma_cmd_space);
152                 memset(state->dma_cmds, 0, (host->sg_tablesize + 1)
153                        * sizeof(struct dbdma_cmd));
154                 state->dma_cmd_space = dma_cmd_space;
155
156                 *prev_statep = state;
157                 prev_statep = &state->next;
158
159                 if (request_irq(state->intr, do_mac53c94_interrupt, 0,
160                                 "53C94", state)) {
161                         printk(KERN_ERR "mac53C94: can't get irq %d for %s\n",
162                                state->intr, node->full_name);
163                 err_cleanup:
164                         iounmap(state->dma);
165                         iounmap(state->regs);
166                         scsi_unregister(host);
167                         break;
168                 }
169
170                 mac53c94_init(state);
171
172                 ++nfscs;
173         }
174         return nfscs;
175 }
176
177 int
178 mac53c94_release(struct Scsi_Host *host)
179 {
180         struct fsc_state *fp = (struct fsc_state *) host->hostdata;
181
182         if (fp == 0)
183                 return 0;
184         if (fp->regs)
185                 iounmap((void *) fp->regs);
186         if (fp->dma)
187                 iounmap((void *) fp->dma);
188         kfree(fp->dma_cmd_space);
189         free_irq(fp->intr, fp);
190         return 0;
191 }
192
193 int
194 mac53c94_queue(Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
195 {
196         struct fsc_state *state;
197
198 #if 0
199         if (data_goes_out(cmd)) {
200                 int i;
201                 printk(KERN_DEBUG "mac53c94_queue %p: command is", cmd);
202                 for (i = 0; i < cmd->cmd_len; ++i)
203                         printk(" %.2x", cmd->cmnd[i]);
204                 printk("\n" KERN_DEBUG "use_sg=%d request_bufflen=%d request_buffer=%p\n",
205                        cmd->use_sg, cmd->request_bufflen, cmd->request_buffer);
206         }
207 #endif
208
209         cmd->scsi_done = done;
210         cmd->host_scribble = NULL;
211
212         state = (struct fsc_state *) cmd->device->host->hostdata;
213
214         if (state->request_q == NULL)
215                 state->request_q = cmd;
216         else
217                 state->request_qtail->host_scribble = (void *) cmd;
218         state->request_qtail = cmd;
219
220         if (state->phase == idle)
221                 mac53c94_start(state);
222
223         return 0;
224 }
225
226 int
227 mac53c94_abort(Scsi_Cmnd *cmd)
228 {
229         return SCSI_ABORT_SNOOZE;
230 }
231
232 int
233 mac53c94_host_reset(Scsi_Cmnd *cmd)
234 {
235         struct fsc_state *state = (struct fsc_state *) cmd->device->host->hostdata;
236         volatile struct mac53c94_regs *regs = state->regs;
237         volatile struct dbdma_regs *dma = state->dma;
238
239         st_le32(&dma->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
240         regs->command = CMD_SCSI_RESET; /* assert RST */
241         eieio();
242         udelay(100);                    /* leave it on for a while (>= 25us) */
243         regs->command = CMD_RESET;
244         eieio();
245         udelay(20);
246         mac53c94_init(state);
247         regs->command = CMD_NOP;
248         eieio();
249         return SUCCESS;
250 }
251
252 static void
253 mac53c94_init(struct fsc_state *state)
254 {
255         volatile struct mac53c94_regs *regs = state->regs;
256         volatile struct dbdma_regs *dma = state->dma;
257         int x;
258
259         regs->config1 = state->host->this_id | CF1_PAR_ENABLE;
260         regs->sel_timeout = TIMO_VAL(250);      /* 250ms */
261         regs->clk_factor = CLKF_VAL(state->clk_freq);
262         regs->config2 = CF2_FEATURE_EN;
263         regs->config3 = 0;
264         regs->sync_period = 0;
265         regs->sync_offset = 0;
266         eieio();
267         x = regs->interrupt;
268         st_le32(&dma->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
269 }
270
271 /*
272  * Start the next command for a 53C94.
273  * Should be called with interrupts disabled.
274  */
275 static void
276 mac53c94_start(struct fsc_state *state)
277 {
278         Scsi_Cmnd *cmd;
279         volatile struct mac53c94_regs *regs = state->regs;
280         int i;
281
282         if (state->phase != idle || state->current_req != NULL)
283                 panic("inappropriate mac53c94_start (state=%p)", state);
284         if (state->request_q == NULL)
285                 return;
286         state->current_req = cmd = state->request_q;
287         state->request_q = (Scsi_Cmnd *) cmd->host_scribble;
288
289         /* Off we go */
290         regs->count_lo = 0;
291         regs->count_mid = 0;
292         regs->count_hi = 0;
293         eieio();
294         regs->command = CMD_NOP + CMD_DMA_MODE;
295         udelay(1);
296         eieio();
297         regs->command = CMD_FLUSH;
298         udelay(1);
299         eieio();
300         regs->dest_id = cmd->device->id;
301         regs->sync_period = 0;
302         regs->sync_offset = 0;
303         eieio();
304
305         /* load the command into the FIFO */
306         for (i = 0; i < cmd->cmd_len; ++i) {
307                 regs->fifo = cmd->cmnd[i];
308                 eieio();
309         }
310
311         /* do select without ATN XXX */
312         regs->command = CMD_SELECT;
313         state->phase = selecting;
314
315         if (cmd->use_sg > 0 || cmd->request_bufflen != 0)
316                 set_dma_cmds(state, cmd);
317 }
318
319 static irqreturn_t
320 do_mac53c94_interrupt(int irq, void *dev_id, struct pt_regs *ptregs)
321 {
322         unsigned long flags;
323         struct Scsi_Host *dev = ((struct fsc_state *) dev_id)->current_req->device->host;
324         
325         spin_lock_irqsave(dev->host_lock, flags);
326         mac53c94_interrupt(irq, dev_id, ptregs);
327         spin_unlock_irqrestore(dev->host_lock, flags);
328         return IRQ_HANDLED;
329 }
330
331 static void
332 mac53c94_interrupt(int irq, void *dev_id, struct pt_regs *ptregs)
333 {
334         struct fsc_state *state = (struct fsc_state *) dev_id;
335         volatile struct mac53c94_regs *regs = state->regs;
336         volatile struct dbdma_regs *dma = state->dma;
337         Scsi_Cmnd *cmd = state->current_req;
338         int nb, stat, seq, intr;
339         static int mac53c94_errors;
340         int dma_dir;
341
342         /*
343          * Apparently, reading the interrupt register unlatches
344          * the status and sequence step registers.
345          */
346         seq = regs->seqstep;
347         stat = regs->status;
348         intr = regs->interrupt;
349
350 #if 0
351         printk(KERN_DEBUG "mac53c94_intr, intr=%x stat=%x seq=%x phase=%d\n",
352                intr, stat, seq, state->phase);
353 #endif
354
355         if (intr & INTR_RESET) {
356                 /* SCSI bus was reset */
357                 printk(KERN_INFO "external SCSI bus reset detected\n");
358                 regs->command = CMD_NOP;
359                 st_le32(&dma->control, RUN << 16);      /* stop dma */
360                 cmd_done(state, DID_RESET << 16);
361                 return;
362         }
363         if (intr & INTR_ILL_CMD) {
364                 printk(KERN_ERR "53c94: invalid cmd, intr=%x stat=%x seq=%x phase=%d\n",
365                        intr, stat, seq, state->phase);
366                 cmd_done(state, DID_ERROR << 16);
367                 return;
368         }
369         if (stat & STAT_ERROR) {
370 #if 0
371                 /* XXX these seem to be harmless? */
372                 printk("53c94: bad error, intr=%x stat=%x seq=%x phase=%d\n",
373                        intr, stat, seq, state->phase);
374 #endif
375                 ++mac53c94_errors;
376                 regs->command = CMD_NOP + CMD_DMA_MODE;
377                 eieio();
378         }
379         if (cmd == 0) {
380                 printk(KERN_DEBUG "53c94: interrupt with no command active?\n");
381                 return;
382         }
383         if (stat & STAT_PARITY) {
384                 printk(KERN_ERR "mac53c94: parity error\n");
385                 cmd_done(state, DID_PARITY << 16);
386                 return;
387         }
388         switch (state->phase) {
389         case selecting:
390                 if (intr & INTR_DISCONNECT) {
391                         /* selection timed out */
392                         cmd_done(state, DID_BAD_TARGET << 16);
393                         return;
394                 }
395                 if (intr != INTR_BUS_SERV + INTR_DONE) {
396                         printk(KERN_DEBUG "got intr %x during selection\n", intr);
397                         cmd_done(state, DID_ERROR << 16);
398                         return;
399                 }
400                 if ((seq & SS_MASK) != SS_DONE) {
401                         printk(KERN_DEBUG "seq step %x after command\n", seq);
402                         cmd_done(state, DID_ERROR << 16);
403                         return;
404                 }
405                 regs->command = CMD_NOP;
406                 /* set DMA controller going if any data to transfer */
407                 if ((stat & (STAT_MSG|STAT_CD)) == 0
408                     && (cmd->use_sg > 0 || cmd->request_bufflen != 0)) {
409                         nb = cmd->SCp.this_residual;
410                         if (nb > 0xfff0)
411                                 nb = 0xfff0;
412                         cmd->SCp.this_residual -= nb;
413                         regs->count_lo = nb;
414                         regs->count_mid = nb >> 8;
415                         eieio();
416                         regs->command = CMD_DMA_MODE + CMD_NOP;
417                         eieio();
418                         st_le32(&dma->cmdptr, virt_to_phys(state->dma_cmds));
419                         st_le32(&dma->control, (RUN << 16) | RUN);
420                         eieio();
421                         regs->command = CMD_DMA_MODE + CMD_XFER_DATA;
422                         state->phase = dataing;
423                         break;
424                 } else if ((stat & STAT_PHASE) == STAT_CD + STAT_IO) {
425                         /* up to status phase already */
426                         regs->command = CMD_I_COMPLETE;
427                         state->phase = completing;
428                 } else {
429                         printk(KERN_DEBUG "in unexpected phase %x after cmd\n",
430                                stat & STAT_PHASE);
431                         cmd_done(state, DID_ERROR << 16);
432                         return;
433                 }
434                 break;
435
436         case dataing:
437                 if (intr != INTR_BUS_SERV) {
438                         printk(KERN_DEBUG "got intr %x before status\n", intr);
439                         cmd_done(state, DID_ERROR << 16);
440                         return;
441                 }
442                 if (cmd->SCp.this_residual != 0
443                     && (stat & (STAT_MSG|STAT_CD)) == 0) {
444                         /* Set up the count regs to transfer more */
445                         nb = cmd->SCp.this_residual;
446                         if (nb > 0xfff0)
447                                 nb = 0xfff0;
448                         cmd->SCp.this_residual -= nb;
449                         regs->count_lo = nb;
450                         regs->count_mid = nb >> 8;
451                         eieio();
452                         regs->command = CMD_DMA_MODE + CMD_NOP;
453                         eieio();
454                         regs->command = CMD_DMA_MODE + CMD_XFER_DATA;
455                         break;
456                 }
457                 if ((stat & STAT_PHASE) != STAT_CD + STAT_IO) {
458                         printk(KERN_DEBUG "intr %x before data xfer complete\n", intr);
459                 }
460                 out_le32(&dma->control, RUN << 16);     /* stop dma */
461                 dma_dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
462                 if (cmd->use_sg != 0) {
463                         pci_unmap_sg(state->pdev,
464                                 (struct scatterlist *)cmd->request_buffer,
465                                 cmd->use_sg, dma_dir);
466                 } else {
467                         pci_unmap_single(state->pdev, state->dma_addr,
468                                 cmd->request_bufflen, dma_dir);
469                 }
470                 /* should check dma status */
471                 regs->command = CMD_I_COMPLETE;
472                 state->phase = completing;
473                 break;
474         case completing:
475                 if (intr != INTR_DONE) {
476                         printk(KERN_DEBUG "got intr %x on completion\n", intr);
477                         cmd_done(state, DID_ERROR << 16);
478                         return;
479                 }
480                 cmd->SCp.Status = regs->fifo; eieio();
481                 cmd->SCp.Message = regs->fifo; eieio();
482                 cmd->result = 
483                 regs->command = CMD_ACCEPT_MSG;
484                 state->phase = busfreeing;
485                 break;
486         case busfreeing:
487                 if (intr != INTR_DISCONNECT) {
488                         printk(KERN_DEBUG "got intr %x when expected disconnect\n", intr);
489                 }
490                 cmd_done(state, (DID_OK << 16) + (cmd->SCp.Message << 8)
491                          + cmd->SCp.Status);
492                 break;
493         default:
494                 printk(KERN_DEBUG "don't know about phase %d\n", state->phase);
495         }
496 }
497
498 static void
499 cmd_done(struct fsc_state *state, int result)
500 {
501         Scsi_Cmnd *cmd;
502
503         cmd = state->current_req;
504         if (cmd != 0) {
505                 cmd->result = result;
506                 (*cmd->scsi_done)(cmd);
507                 state->current_req = NULL;
508         }
509         state->phase = idle;
510         mac53c94_start(state);
511 }
512
513 /*
514  * Set up DMA commands for transferring data.
515  */
516 static void
517 set_dma_cmds(struct fsc_state *state, Scsi_Cmnd *cmd)
518 {
519         int i, dma_cmd, total;
520         struct scatterlist *scl;
521         struct dbdma_cmd *dcmds;
522         dma_addr_t dma_addr;
523         u32 dma_len;
524         int dma_dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
525
526         dma_cmd = data_goes_out(cmd)? OUTPUT_MORE: INPUT_MORE;
527         dcmds = state->dma_cmds;
528         if (cmd->use_sg > 0) {
529                 int nseg;
530
531                 total = 0;
532                 scl = (struct scatterlist *) cmd->buffer;
533                 nseg = pci_map_sg(state->pdev, scl, cmd->use_sg, dma_dir);
534                 for (i = 0; i < nseg; ++i) {
535                         dma_addr = sg_dma_address(scl);
536                         dma_len = sg_dma_len(scl);
537                         if (dma_len > 0xffff)
538                                 panic("mac53c94: scatterlist element >= 64k");
539                         total += dma_len;
540                         st_le16(&dcmds->req_count, dma_len);
541                         st_le16(&dcmds->command, dma_cmd);
542                         st_le32(&dcmds->phy_addr, dma_addr);
543                         dcmds->xfer_status = 0;
544                         ++scl;
545                         ++dcmds;
546                 }
547         } else {
548                 total = cmd->request_bufflen;
549                 if (total > 0xffff)
550                         panic("mac53c94: transfer size >= 64k");
551                 dma_addr = pci_map_single(state->pdev, cmd->request_buffer,
552                                           total, dma_dir);
553                 state->dma_addr = dma_addr;
554                 st_le16(&dcmds->req_count, total);
555                 st_le32(&dcmds->phy_addr, dma_addr);
556                 dcmds->xfer_status = 0;
557                 ++dcmds;
558         }
559         dma_cmd += OUTPUT_LAST - OUTPUT_MORE;
560         st_le16(&dcmds[-1].command, dma_cmd);
561         st_le16(&dcmds->command, DBDMA_STOP);
562         cmd->SCp.this_residual = total;
563 }
564
565 /*
566  * Work out whether data will be going out from the host adaptor or into it.
567  */
568 static int
569 data_goes_out(Scsi_Cmnd *cmd)
570 {
571         switch (cmd->sc_data_direction) {
572         case SCSI_DATA_WRITE:
573                 return 1;
574         case SCSI_DATA_READ:
575                 return 0;
576         }
577
578         /* for SCSI_DATA_UNKNOWN or SCSI_DATA_NONE, fall back on the
579            old method for now... */
580         switch (cmd->cmnd[0]) {
581         case CHANGE_DEFINITION: 
582         case COMPARE:     
583         case COPY:
584         case COPY_VERIFY:           
585         case FORMAT_UNIT:        
586         case LOG_SELECT:
587         case MEDIUM_SCAN:         
588         case MODE_SELECT:
589         case MODE_SELECT_10:
590         case REASSIGN_BLOCKS: 
591         case RESERVE:
592         case SEARCH_EQUAL:        
593         case SEARCH_EQUAL_12: 
594         case SEARCH_HIGH:        
595         case SEARCH_HIGH_12:  
596         case SEARCH_LOW:
597         case SEARCH_LOW_12:
598         case SEND_DIAGNOSTIC: 
599         case SEND_VOLUME_TAG:        
600         case SET_WINDOW: 
601         case UPDATE_BLOCK:      
602         case WRITE_BUFFER:
603         case WRITE_6:   
604         case WRITE_10:  
605         case WRITE_12:    
606         case WRITE_LONG:        
607         case WRITE_LONG_2:      /* alternate code for WRITE_LONG */
608         case WRITE_SAME:        
609         case WRITE_VERIFY:
610         case WRITE_VERIFY_12:
611                 return 1;
612         default:
613                 return 0;
614         }
615 }
616
617 static Scsi_Host_Template driver_template = {
618         .proc_name      = "53c94",
619         .name           = "53C94",
620         .detect         = mac53c94_detect,
621         .release        = mac53c94_release,
622         .queuecommand   = mac53c94_queue,
623         .eh_abort_handler = mac53c94_abort,
624         .eh_host_reset_handler = mac53c94_host_reset,
625         .can_queue      = 1,
626         .this_id        = 7,
627         .sg_tablesize   = SG_ALL,
628         .cmd_per_lun    = 1,
629         .use_clustering = DISABLE_CLUSTERING,
630 };
631
632 #include "scsi_module.c"