]> err.no Git - linux-2.6/blob - drivers/scsi/mac53c94.c
[SCSI] Remove no-op implementations of SCSI EH hooks
[linux-2.6] / 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 #include <asm/macio.h>
27
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
32
33 #include "mac53c94.h"
34
35 enum fsc_phase {
36         idle,
37         selecting,
38         dataing,
39         completing,
40         busfreeing,
41 };
42
43 struct fsc_state {
44         struct  mac53c94_regs __iomem *regs;
45         int     intr;
46         struct  dbdma_regs __iomem *dma;
47         int     dmaintr;
48         int     clk_freq;
49         struct  Scsi_Host *host;
50         struct scsi_cmnd *request_q;
51         struct scsi_cmnd *request_qtail;
52         struct scsi_cmnd *current_req;          /* req we're currently working on */
53         enum fsc_phase phase;           /* what we're currently trying to do */
54         struct dbdma_cmd *dma_cmds;     /* space for dbdma commands, aligned */
55         void    *dma_cmd_space;
56         struct  pci_dev *pdev;
57         dma_addr_t dma_addr;
58         struct macio_dev *mdev;
59 };
60
61 static void mac53c94_init(struct fsc_state *);
62 static void mac53c94_start(struct fsc_state *);
63 static void mac53c94_interrupt(int, void *, struct pt_regs *);
64 static irqreturn_t do_mac53c94_interrupt(int, void *, struct pt_regs *);
65 static void cmd_done(struct fsc_state *, int result);
66 static void set_dma_cmds(struct fsc_state *, struct scsi_cmnd *);
67
68
69 static int mac53c94_queue(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
70 {
71         struct fsc_state *state;
72
73 #if 0
74         if (cmd->sc_data_direction == DMA_TO_DEVICE) {
75                 int i;
76                 printk(KERN_DEBUG "mac53c94_queue %p: command is", cmd);
77                 for (i = 0; i < cmd->cmd_len; ++i)
78                         printk(" %.2x", cmd->cmnd[i]);
79                 printk("\n" KERN_DEBUG "use_sg=%d request_bufflen=%d request_buffer=%p\n",
80                        cmd->use_sg, cmd->request_bufflen, cmd->request_buffer);
81         }
82 #endif
83
84         cmd->scsi_done = done;
85         cmd->host_scribble = NULL;
86
87         state = (struct fsc_state *) cmd->device->host->hostdata;
88
89         if (state->request_q == NULL)
90                 state->request_q = cmd;
91         else
92                 state->request_qtail->host_scribble = (void *) cmd;
93         state->request_qtail = cmd;
94
95         if (state->phase == idle)
96                 mac53c94_start(state);
97
98         return 0;
99 }
100
101 static int mac53c94_host_reset(struct scsi_cmnd *cmd)
102 {
103         struct fsc_state *state = (struct fsc_state *) cmd->device->host->hostdata;
104         struct mac53c94_regs __iomem *regs = state->regs;
105         struct dbdma_regs __iomem *dma = state->dma;
106
107         writel((RUN|PAUSE|FLUSH|WAKE) << 16, &dma->control);
108         writeb(CMD_SCSI_RESET, &regs->command); /* assert RST */
109         udelay(100);                    /* leave it on for a while (>= 25us) */
110         writeb(CMD_RESET, &regs->command);
111         udelay(20);
112         mac53c94_init(state);
113         writeb(CMD_NOP, &regs->command);
114         return SUCCESS;
115 }
116
117 static void mac53c94_init(struct fsc_state *state)
118 {
119         struct mac53c94_regs __iomem *regs = state->regs;
120         struct dbdma_regs __iomem *dma = state->dma;
121         int x;
122
123         writeb(state->host->this_id | CF1_PAR_ENABLE, &regs->config1);
124         writeb(TIMO_VAL(250), &regs->sel_timeout);      /* 250ms */
125         writeb(CLKF_VAL(state->clk_freq), &regs->clk_factor);
126         writeb(CF2_FEATURE_EN, &regs->config2);
127         writeb(0, &regs->config3);
128         writeb(0, &regs->sync_period);
129         writeb(0, &regs->sync_offset);
130         x = readb(&regs->interrupt);
131         writel((RUN|PAUSE|FLUSH|WAKE) << 16, &dma->control);
132 }
133
134 /*
135  * Start the next command for a 53C94.
136  * Should be called with interrupts disabled.
137  */
138 static void mac53c94_start(struct fsc_state *state)
139 {
140         struct scsi_cmnd *cmd;
141         struct mac53c94_regs __iomem *regs = state->regs;
142         int i;
143
144         if (state->phase != idle || state->current_req != NULL)
145                 panic("inappropriate mac53c94_start (state=%p)", state);
146         if (state->request_q == NULL)
147                 return;
148         state->current_req = cmd = state->request_q;
149         state->request_q = (struct scsi_cmnd *) cmd->host_scribble;
150
151         /* Off we go */
152         writeb(0, &regs->count_lo);
153         writeb(0, &regs->count_mid);
154         writeb(0, &regs->count_hi);
155         writeb(CMD_NOP + CMD_DMA_MODE, &regs->command);
156         udelay(1);
157         writeb(CMD_FLUSH, &regs->command);
158         udelay(1);
159         writeb(cmd->device->id, &regs->dest_id);
160         writeb(0, &regs->sync_period);
161         writeb(0, &regs->sync_offset);
162
163         /* load the command into the FIFO */
164         for (i = 0; i < cmd->cmd_len; ++i)
165                 writeb(cmd->cmnd[i], &regs->fifo);
166
167         /* do select without ATN XXX */
168         writeb(CMD_SELECT, &regs->command);
169         state->phase = selecting;
170
171         if (cmd->use_sg > 0 || cmd->request_bufflen != 0)
172                 set_dma_cmds(state, cmd);
173 }
174
175 static irqreturn_t do_mac53c94_interrupt(int irq, void *dev_id, struct pt_regs *ptregs)
176 {
177         unsigned long flags;
178         struct Scsi_Host *dev = ((struct fsc_state *) dev_id)->current_req->device->host;
179         
180         spin_lock_irqsave(dev->host_lock, flags);
181         mac53c94_interrupt(irq, dev_id, ptregs);
182         spin_unlock_irqrestore(dev->host_lock, flags);
183         return IRQ_HANDLED;
184 }
185
186 static void mac53c94_interrupt(int irq, void *dev_id, struct pt_regs *ptregs)
187 {
188         struct fsc_state *state = (struct fsc_state *) dev_id;
189         struct mac53c94_regs __iomem *regs = state->regs;
190         struct dbdma_regs __iomem *dma = state->dma;
191         struct scsi_cmnd *cmd = state->current_req;
192         int nb, stat, seq, intr;
193         static int mac53c94_errors;
194
195         /*
196          * Apparently, reading the interrupt register unlatches
197          * the status and sequence step registers.
198          */
199         seq = readb(&regs->seqstep);
200         stat = readb(&regs->status);
201         intr = readb(&regs->interrupt);
202
203 #if 0
204         printk(KERN_DEBUG "mac53c94_intr, intr=%x stat=%x seq=%x phase=%d\n",
205                intr, stat, seq, state->phase);
206 #endif
207
208         if (intr & INTR_RESET) {
209                 /* SCSI bus was reset */
210                 printk(KERN_INFO "external SCSI bus reset detected\n");
211                 writeb(CMD_NOP, &regs->command);
212                 writel(RUN << 16, &dma->control);       /* stop dma */
213                 cmd_done(state, DID_RESET << 16);
214                 return;
215         }
216         if (intr & INTR_ILL_CMD) {
217                 printk(KERN_ERR "53c94: invalid cmd, intr=%x stat=%x seq=%x phase=%d\n",
218                        intr, stat, seq, state->phase);
219                 cmd_done(state, DID_ERROR << 16);
220                 return;
221         }
222         if (stat & STAT_ERROR) {
223 #if 0
224                 /* XXX these seem to be harmless? */
225                 printk("53c94: bad error, intr=%x stat=%x seq=%x phase=%d\n",
226                        intr, stat, seq, state->phase);
227 #endif
228                 ++mac53c94_errors;
229                 writeb(CMD_NOP + CMD_DMA_MODE, &regs->command);
230         }
231         if (cmd == 0) {
232                 printk(KERN_DEBUG "53c94: interrupt with no command active?\n");
233                 return;
234         }
235         if (stat & STAT_PARITY) {
236                 printk(KERN_ERR "mac53c94: parity error\n");
237                 cmd_done(state, DID_PARITY << 16);
238                 return;
239         }
240         switch (state->phase) {
241         case selecting:
242                 if (intr & INTR_DISCONNECT) {
243                         /* selection timed out */
244                         cmd_done(state, DID_BAD_TARGET << 16);
245                         return;
246                 }
247                 if (intr != INTR_BUS_SERV + INTR_DONE) {
248                         printk(KERN_DEBUG "got intr %x during selection\n", intr);
249                         cmd_done(state, DID_ERROR << 16);
250                         return;
251                 }
252                 if ((seq & SS_MASK) != SS_DONE) {
253                         printk(KERN_DEBUG "seq step %x after command\n", seq);
254                         cmd_done(state, DID_ERROR << 16);
255                         return;
256                 }
257                 writeb(CMD_NOP, &regs->command);
258                 /* set DMA controller going if any data to transfer */
259                 if ((stat & (STAT_MSG|STAT_CD)) == 0
260                     && (cmd->use_sg > 0 || cmd->request_bufflen != 0)) {
261                         nb = cmd->SCp.this_residual;
262                         if (nb > 0xfff0)
263                                 nb = 0xfff0;
264                         cmd->SCp.this_residual -= nb;
265                         writeb(nb, &regs->count_lo);
266                         writeb(nb >> 8, &regs->count_mid);
267                         writeb(CMD_DMA_MODE + CMD_NOP, &regs->command);
268                         writel(virt_to_phys(state->dma_cmds), &dma->cmdptr);
269                         writel((RUN << 16) | RUN, &dma->control);
270                         writeb(CMD_DMA_MODE + CMD_XFER_DATA, &regs->command);
271                         state->phase = dataing;
272                         break;
273                 } else if ((stat & STAT_PHASE) == STAT_CD + STAT_IO) {
274                         /* up to status phase already */
275                         writeb(CMD_I_COMPLETE, &regs->command);
276                         state->phase = completing;
277                 } else {
278                         printk(KERN_DEBUG "in unexpected phase %x after cmd\n",
279                                stat & STAT_PHASE);
280                         cmd_done(state, DID_ERROR << 16);
281                         return;
282                 }
283                 break;
284
285         case dataing:
286                 if (intr != INTR_BUS_SERV) {
287                         printk(KERN_DEBUG "got intr %x before status\n", intr);
288                         cmd_done(state, DID_ERROR << 16);
289                         return;
290                 }
291                 if (cmd->SCp.this_residual != 0
292                     && (stat & (STAT_MSG|STAT_CD)) == 0) {
293                         /* Set up the count regs to transfer more */
294                         nb = cmd->SCp.this_residual;
295                         if (nb > 0xfff0)
296                                 nb = 0xfff0;
297                         cmd->SCp.this_residual -= nb;
298                         writeb(nb, &regs->count_lo);
299                         writeb(nb >> 8, &regs->count_mid);
300                         writeb(CMD_DMA_MODE + CMD_NOP, &regs->command);
301                         writeb(CMD_DMA_MODE + CMD_XFER_DATA, &regs->command);
302                         break;
303                 }
304                 if ((stat & STAT_PHASE) != STAT_CD + STAT_IO) {
305                         printk(KERN_DEBUG "intr %x before data xfer complete\n", intr);
306                 }
307                 writel(RUN << 16, &dma->control);       /* stop dma */
308                 if (cmd->use_sg != 0) {
309                         pci_unmap_sg(state->pdev,
310                                 (struct scatterlist *)cmd->request_buffer,
311                                 cmd->use_sg, cmd->sc_data_direction);
312                 } else {
313                         pci_unmap_single(state->pdev, state->dma_addr,
314                                 cmd->request_bufflen, cmd->sc_data_direction);
315                 }
316                 /* should check dma status */
317                 writeb(CMD_I_COMPLETE, &regs->command);
318                 state->phase = completing;
319                 break;
320         case completing:
321                 if (intr != INTR_DONE) {
322                         printk(KERN_DEBUG "got intr %x on completion\n", intr);
323                         cmd_done(state, DID_ERROR << 16);
324                         return;
325                 }
326                 cmd->SCp.Status = readb(&regs->fifo);
327                 cmd->SCp.Message = readb(&regs->fifo);
328                 cmd->result = CMD_ACCEPT_MSG;
329                 writeb(CMD_ACCEPT_MSG, &regs->command);
330                 state->phase = busfreeing;
331                 break;
332         case busfreeing:
333                 if (intr != INTR_DISCONNECT) {
334                         printk(KERN_DEBUG "got intr %x when expected disconnect\n", intr);
335                 }
336                 cmd_done(state, (DID_OK << 16) + (cmd->SCp.Message << 8)
337                          + cmd->SCp.Status);
338                 break;
339         default:
340                 printk(KERN_DEBUG "don't know about phase %d\n", state->phase);
341         }
342 }
343
344 static void cmd_done(struct fsc_state *state, int result)
345 {
346         struct scsi_cmnd *cmd;
347
348         cmd = state->current_req;
349         if (cmd != 0) {
350                 cmd->result = result;
351                 (*cmd->scsi_done)(cmd);
352                 state->current_req = NULL;
353         }
354         state->phase = idle;
355         mac53c94_start(state);
356 }
357
358 /*
359  * Set up DMA commands for transferring data.
360  */
361 static void set_dma_cmds(struct fsc_state *state, struct scsi_cmnd *cmd)
362 {
363         int i, dma_cmd, total;
364         struct scatterlist *scl;
365         struct dbdma_cmd *dcmds;
366         dma_addr_t dma_addr;
367         u32 dma_len;
368
369         dma_cmd = cmd->sc_data_direction == DMA_TO_DEVICE ?
370                         OUTPUT_MORE : INPUT_MORE;
371         dcmds = state->dma_cmds;
372         if (cmd->use_sg > 0) {
373                 int nseg;
374
375                 total = 0;
376                 scl = (struct scatterlist *) cmd->buffer;
377                 nseg = pci_map_sg(state->pdev, scl, cmd->use_sg,
378                                 cmd->sc_data_direction);
379                 for (i = 0; i < nseg; ++i) {
380                         dma_addr = sg_dma_address(scl);
381                         dma_len = sg_dma_len(scl);
382                         if (dma_len > 0xffff)
383                                 panic("mac53c94: scatterlist element >= 64k");
384                         total += dma_len;
385                         st_le16(&dcmds->req_count, dma_len);
386                         st_le16(&dcmds->command, dma_cmd);
387                         st_le32(&dcmds->phy_addr, dma_addr);
388                         dcmds->xfer_status = 0;
389                         ++scl;
390                         ++dcmds;
391                 }
392         } else {
393                 total = cmd->request_bufflen;
394                 if (total > 0xffff)
395                         panic("mac53c94: transfer size >= 64k");
396                 dma_addr = pci_map_single(state->pdev, cmd->request_buffer,
397                                           total, cmd->sc_data_direction);
398                 state->dma_addr = dma_addr;
399                 st_le16(&dcmds->req_count, total);
400                 st_le32(&dcmds->phy_addr, dma_addr);
401                 dcmds->xfer_status = 0;
402                 ++dcmds;
403         }
404         dma_cmd += OUTPUT_LAST - OUTPUT_MORE;
405         st_le16(&dcmds[-1].command, dma_cmd);
406         st_le16(&dcmds->command, DBDMA_STOP);
407         cmd->SCp.this_residual = total;
408 }
409
410 static struct scsi_host_template mac53c94_template = {
411         .proc_name      = "53c94",
412         .name           = "53C94",
413         .queuecommand   = mac53c94_queue,
414         .eh_host_reset_handler = mac53c94_host_reset,
415         .can_queue      = 1,
416         .this_id        = 7,
417         .sg_tablesize   = SG_ALL,
418         .cmd_per_lun    = 1,
419         .use_clustering = DISABLE_CLUSTERING,
420 };
421
422 static int mac53c94_probe(struct macio_dev *mdev, const struct of_match *match)
423 {
424         struct device_node *node = macio_get_of_node(mdev);
425         struct pci_dev *pdev = macio_get_pci_dev(mdev);
426         struct fsc_state *state;
427         struct Scsi_Host *host;
428         void *dma_cmd_space;
429         unsigned char *clkprop;
430         int proplen;
431
432         if (macio_resource_count(mdev) != 2 || macio_irq_count(mdev) != 2) {
433                 printk(KERN_ERR "mac53c94: expected 2 addrs and intrs (got %d/%d)\n",
434                        node->n_addrs, node->n_intrs);
435                 return -ENODEV;
436         }
437
438         if (macio_request_resources(mdev, "mac53c94") != 0) {
439                 printk(KERN_ERR "mac53c94: unable to request memory resources");
440                 return -EBUSY;
441         }
442
443         host = scsi_host_alloc(&mac53c94_template, sizeof(struct fsc_state));
444         if (host == NULL) {
445                 printk(KERN_ERR "mac53c94: couldn't register host");
446                 goto out_release;
447         }
448
449         state = (struct fsc_state *) host->hostdata;
450         macio_set_drvdata(mdev, state);
451         state->host = host;
452         state->pdev = pdev;
453         state->mdev = mdev;
454
455         state->regs = (struct mac53c94_regs __iomem *)
456                 ioremap(macio_resource_start(mdev, 0), 0x1000);
457         state->intr = macio_irq(mdev, 0);
458         state->dma = (struct dbdma_regs __iomem *)
459                 ioremap(macio_resource_start(mdev, 1), 0x1000);
460         state->dmaintr = macio_irq(mdev, 1);
461         if (state->regs == NULL || state->dma == NULL) {
462                 printk(KERN_ERR "mac53c94: ioremap failed for %s\n",
463                        node->full_name);
464                 goto out_free;
465         }
466
467         clkprop = get_property(node, "clock-frequency", &proplen);
468         if (clkprop == NULL || proplen != sizeof(int)) {
469                 printk(KERN_ERR "%s: can't get clock frequency, "
470                        "assuming 25MHz\n", node->full_name);
471                 state->clk_freq = 25000000;
472         } else
473                 state->clk_freq = *(int *)clkprop;
474
475         /* Space for dma command list: +1 for stop command,
476          * +1 to allow for aligning.
477          * XXX FIXME: Use DMA consistent routines
478          */
479         dma_cmd_space = kmalloc((host->sg_tablesize + 2) *
480                                 sizeof(struct dbdma_cmd), GFP_KERNEL);
481         if (dma_cmd_space == 0) {
482                 printk(KERN_ERR "mac53c94: couldn't allocate dma "
483                        "command space for %s\n", node->full_name);
484                 goto out_free;
485         }
486         state->dma_cmds = (struct dbdma_cmd *)DBDMA_ALIGN(dma_cmd_space);
487         memset(state->dma_cmds, 0, (host->sg_tablesize + 1)
488                * sizeof(struct dbdma_cmd));
489         state->dma_cmd_space = dma_cmd_space;
490
491         mac53c94_init(state);
492
493         if (request_irq(state->intr, do_mac53c94_interrupt, 0, "53C94", state)) {
494                 printk(KERN_ERR "mac53C94: can't get irq %d for %s\n",
495                        state->intr, node->full_name);
496                 goto out_free_dma;
497         }
498
499         /* XXX FIXME: handle failure */
500         scsi_add_host(host, &mdev->ofdev.dev);
501         scsi_scan_host(host);
502
503         return 0;
504
505  out_free_dma:
506         kfree(state->dma_cmd_space);
507  out_free:
508         if (state->dma != NULL)
509                 iounmap(state->dma);
510         if (state->regs != NULL)
511                 iounmap(state->regs);
512         scsi_host_put(host);
513  out_release:
514         macio_release_resources(mdev);
515
516         return  -ENODEV;
517 }
518
519 static int mac53c94_remove(struct macio_dev *mdev)
520 {
521         struct fsc_state *fp = (struct fsc_state *)macio_get_drvdata(mdev);
522         struct Scsi_Host *host = fp->host;
523
524         scsi_remove_host(host);
525
526         free_irq(fp->intr, fp);
527
528         if (fp->regs)
529                 iounmap((void *) fp->regs);
530         if (fp->dma)
531                 iounmap((void *) fp->dma);
532         kfree(fp->dma_cmd_space);
533
534         scsi_host_put(host);
535
536         macio_release_resources(mdev);
537
538         return 0;
539 }
540
541
542 static struct of_match mac53c94_match[] = 
543 {
544         {
545         .name           = "53c94",
546         .type           = OF_ANY_MATCH,
547         .compatible     = OF_ANY_MATCH
548         },
549         {},
550 };
551
552 static struct macio_driver mac53c94_driver = 
553 {
554         .name           = "mac53c94",
555         .match_table    = mac53c94_match,
556         .probe          = mac53c94_probe,
557         .remove         = mac53c94_remove,
558 };
559
560
561 static int __init init_mac53c94(void)
562 {
563         return macio_register_driver(&mac53c94_driver);
564 }
565
566 static void __exit exit_mac53c94(void)
567 {
568         return macio_unregister_driver(&mac53c94_driver);
569 }
570
571 module_init(init_mac53c94);
572 module_exit(exit_mac53c94);
573
574 MODULE_DESCRIPTION("PowerMac 53c94 SCSI driver");
575 MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
576 MODULE_LICENSE("GPL");