scsi: core: take the DMA max mapping size into account
[linux-2.6-microblaze.git] / drivers / scsi / mac_scsi.c
1 /*
2  * Generic Macintosh NCR5380 driver
3  *
4  * Copyright 1998, Michael Schmitz <mschmitz@lbl.gov>
5  *
6  * Copyright 2019 Finn Thain
7  *
8  * derived in part from:
9  */
10 /*
11  * Generic Generic NCR5380 driver
12  *
13  * Copyright 1995, Russell King
14  */
15
16 #include <linux/delay.h>
17 #include <linux/types.h>
18 #include <linux/module.h>
19 #include <linux/ioport.h>
20 #include <linux/init.h>
21 #include <linux/blkdev.h>
22 #include <linux/interrupt.h>
23 #include <linux/platform_device.h>
24
25 #include <asm/hwtest.h>
26 #include <asm/io.h>
27 #include <asm/macintosh.h>
28 #include <asm/macints.h>
29 #include <asm/setup.h>
30
31 #include <scsi/scsi_host.h>
32
33 /* Definitions for the core NCR5380 driver. */
34
35 #define NCR5380_implementation_fields   int pdma_residual
36
37 #define NCR5380_read(reg)           in_8(hostdata->io + ((reg) << 4))
38 #define NCR5380_write(reg, value)   out_8(hostdata->io + ((reg) << 4), value)
39
40 #define NCR5380_dma_xfer_len            macscsi_dma_xfer_len
41 #define NCR5380_dma_recv_setup          macscsi_pread
42 #define NCR5380_dma_send_setup          macscsi_pwrite
43 #define NCR5380_dma_residual            macscsi_dma_residual
44
45 #define NCR5380_intr                    macscsi_intr
46 #define NCR5380_queue_command           macscsi_queue_command
47 #define NCR5380_abort                   macscsi_abort
48 #define NCR5380_host_reset              macscsi_host_reset
49 #define NCR5380_info                    macscsi_info
50
51 #include "NCR5380.h"
52
53 static int setup_can_queue = -1;
54 module_param(setup_can_queue, int, 0);
55 static int setup_cmd_per_lun = -1;
56 module_param(setup_cmd_per_lun, int, 0);
57 static int setup_sg_tablesize = -1;
58 module_param(setup_sg_tablesize, int, 0);
59 static int setup_use_pdma = 512;
60 module_param(setup_use_pdma, int, 0);
61 static int setup_hostid = -1;
62 module_param(setup_hostid, int, 0);
63 static int setup_toshiba_delay = -1;
64 module_param(setup_toshiba_delay, int, 0);
65
66 #ifndef MODULE
67 static int __init mac_scsi_setup(char *str)
68 {
69         int ints[8];
70
71         (void)get_options(str, ARRAY_SIZE(ints), ints);
72
73         if (ints[0] < 1) {
74                 pr_err("Usage: mac5380=<can_queue>[,<cmd_per_lun>[,<sg_tablesize>[,<hostid>[,<use_tags>[,<use_pdma>[,<toshiba_delay>]]]]]]\n");
75                 return 0;
76         }
77         if (ints[0] >= 1)
78                 setup_can_queue = ints[1];
79         if (ints[0] >= 2)
80                 setup_cmd_per_lun = ints[2];
81         if (ints[0] >= 3)
82                 setup_sg_tablesize = ints[3];
83         if (ints[0] >= 4)
84                 setup_hostid = ints[4];
85         /* ints[5] (use_tagged_queuing) is ignored */
86         if (ints[0] >= 6)
87                 setup_use_pdma = ints[6];
88         if (ints[0] >= 7)
89                 setup_toshiba_delay = ints[7];
90         return 1;
91 }
92
93 __setup("mac5380=", mac_scsi_setup);
94 #endif /* !MODULE */
95
96 /*
97  * According to "Inside Macintosh: Devices", Mac OS requires disk drivers to
98  * specify the number of bytes between the delays expected from a SCSI target.
99  * This allows the operating system to "prevent bus errors when a target fails
100  * to deliver the next byte within the processor bus error timeout period."
101  * Linux SCSI drivers lack knowledge of the timing behaviour of SCSI targets
102  * so bus errors are unavoidable.
103  *
104  * If a MOVE.B instruction faults, we assume that zero bytes were transferred
105  * and simply retry. That assumption probably depends on target behaviour but
106  * seems to hold up okay. The NOP provides synchronization: without it the
107  * fault can sometimes occur after the program counter has moved past the
108  * offending instruction. Post-increment addressing can't be used.
109  */
110
111 #define MOVE_BYTE(operands) \
112         asm volatile ( \
113                 "1:     moveb " operands "     \n" \
114                 "11:    nop                    \n" \
115                 "       addq #1,%0             \n" \
116                 "       subq #1,%1             \n" \
117                 "40:                           \n" \
118                 "                              \n" \
119                 ".section .fixup,\"ax\"        \n" \
120                 ".even                         \n" \
121                 "90:    movel #1, %2           \n" \
122                 "       jra 40b                \n" \
123                 ".previous                     \n" \
124                 "                              \n" \
125                 ".section __ex_table,\"a\"     \n" \
126                 ".align  4                     \n" \
127                 ".long   1b,90b                \n" \
128                 ".long  11b,90b                \n" \
129                 ".previous                     \n" \
130                 : "+a" (addr), "+r" (n), "+r" (result) : "a" (io))
131
132 /*
133  * If a MOVE.W (or MOVE.L) instruction faults, it cannot be retried because
134  * the residual byte count would be uncertain. In that situation the MOVE_WORD
135  * macro clears n in the fixup section to abort the transfer.
136  */
137
138 #define MOVE_WORD(operands) \
139         asm volatile ( \
140                 "1:     movew " operands "     \n" \
141                 "11:    nop                    \n" \
142                 "       subq #2,%1             \n" \
143                 "40:                           \n" \
144                 "                              \n" \
145                 ".section .fixup,\"ax\"        \n" \
146                 ".even                         \n" \
147                 "90:    movel #0, %1           \n" \
148                 "       movel #2, %2           \n" \
149                 "       jra 40b                \n" \
150                 ".previous                     \n" \
151                 "                              \n" \
152                 ".section __ex_table,\"a\"     \n" \
153                 ".align  4                     \n" \
154                 ".long   1b,90b                \n" \
155                 ".long  11b,90b                \n" \
156                 ".previous                     \n" \
157                 : "+a" (addr), "+r" (n), "+r" (result) : "a" (io))
158
159 #define MOVE_16_WORDS(operands) \
160         asm volatile ( \
161                 "1:     movew " operands "     \n" \
162                 "2:     movew " operands "     \n" \
163                 "3:     movew " operands "     \n" \
164                 "4:     movew " operands "     \n" \
165                 "5:     movew " operands "     \n" \
166                 "6:     movew " operands "     \n" \
167                 "7:     movew " operands "     \n" \
168                 "8:     movew " operands "     \n" \
169                 "9:     movew " operands "     \n" \
170                 "10:    movew " operands "     \n" \
171                 "11:    movew " operands "     \n" \
172                 "12:    movew " operands "     \n" \
173                 "13:    movew " operands "     \n" \
174                 "14:    movew " operands "     \n" \
175                 "15:    movew " operands "     \n" \
176                 "16:    movew " operands "     \n" \
177                 "17:    nop                    \n" \
178                 "       subl  #32,%1           \n" \
179                 "40:                           \n" \
180                 "                              \n" \
181                 ".section .fixup,\"ax\"        \n" \
182                 ".even                         \n" \
183                 "90:    movel #0, %1           \n" \
184                 "       movel #2, %2           \n" \
185                 "       jra 40b                \n" \
186                 ".previous                     \n" \
187                 "                              \n" \
188                 ".section __ex_table,\"a\"     \n" \
189                 ".align  4                     \n" \
190                 ".long   1b,90b                \n" \
191                 ".long   2b,90b                \n" \
192                 ".long   3b,90b                \n" \
193                 ".long   4b,90b                \n" \
194                 ".long   5b,90b                \n" \
195                 ".long   6b,90b                \n" \
196                 ".long   7b,90b                \n" \
197                 ".long   8b,90b                \n" \
198                 ".long   9b,90b                \n" \
199                 ".long  10b,90b                \n" \
200                 ".long  11b,90b                \n" \
201                 ".long  12b,90b                \n" \
202                 ".long  13b,90b                \n" \
203                 ".long  14b,90b                \n" \
204                 ".long  15b,90b                \n" \
205                 ".long  16b,90b                \n" \
206                 ".long  17b,90b                \n" \
207                 ".previous                     \n" \
208                 : "+a" (addr), "+r" (n), "+r" (result) : "a" (io))
209
210 #define MAC_PDMA_DELAY          32
211
212 static inline int mac_pdma_recv(void __iomem *io, unsigned char *start, int n)
213 {
214         unsigned char *addr = start;
215         int result = 0;
216
217         if (n >= 1) {
218                 MOVE_BYTE("%3@,%0@");
219                 if (result)
220                         goto out;
221         }
222         if (n >= 1 && ((unsigned long)addr & 1)) {
223                 MOVE_BYTE("%3@,%0@");
224                 if (result)
225                         goto out;
226         }
227         while (n >= 32)
228                 MOVE_16_WORDS("%3@,%0@+");
229         while (n >= 2)
230                 MOVE_WORD("%3@,%0@+");
231         if (result)
232                 return start - addr; /* Negated to indicate uncertain length */
233         if (n == 1)
234                 MOVE_BYTE("%3@,%0@");
235 out:
236         return addr - start;
237 }
238
239 static inline int mac_pdma_send(unsigned char *start, void __iomem *io, int n)
240 {
241         unsigned char *addr = start;
242         int result = 0;
243
244         if (n >= 1) {
245                 MOVE_BYTE("%0@,%3@");
246                 if (result)
247                         goto out;
248         }
249         if (n >= 1 && ((unsigned long)addr & 1)) {
250                 MOVE_BYTE("%0@,%3@");
251                 if (result)
252                         goto out;
253         }
254         while (n >= 32)
255                 MOVE_16_WORDS("%0@+,%3@");
256         while (n >= 2)
257                 MOVE_WORD("%0@+,%3@");
258         if (result)
259                 return start - addr; /* Negated to indicate uncertain length */
260         if (n == 1)
261                 MOVE_BYTE("%0@,%3@");
262 out:
263         return addr - start;
264 }
265
266 /* The "SCSI DMA" chip on the IIfx implements this register. */
267 #define CTRL_REG                0x8
268 #define CTRL_INTERRUPTS_ENABLE  BIT(1)
269 #define CTRL_HANDSHAKE_MODE     BIT(3)
270
271 static inline void write_ctrl_reg(struct NCR5380_hostdata *hostdata, u32 value)
272 {
273         out_be32(hostdata->io + (CTRL_REG << 4), value);
274 }
275
276 static inline int macscsi_pread(struct NCR5380_hostdata *hostdata,
277                                 unsigned char *dst, int len)
278 {
279         u8 __iomem *s = hostdata->pdma_io + (INPUT_DATA_REG << 4);
280         unsigned char *d = dst;
281         int result = 0;
282
283         hostdata->pdma_residual = len;
284
285         while (!NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG,
286                                       BASR_DRQ | BASR_PHASE_MATCH,
287                                       BASR_DRQ | BASR_PHASE_MATCH, HZ / 64)) {
288                 int bytes;
289
290                 if (macintosh_config->ident == MAC_MODEL_IIFX)
291                         write_ctrl_reg(hostdata, CTRL_HANDSHAKE_MODE |
292                                                  CTRL_INTERRUPTS_ENABLE);
293
294                 bytes = mac_pdma_recv(s, d, min(hostdata->pdma_residual, 512));
295
296                 if (bytes > 0) {
297                         d += bytes;
298                         hostdata->pdma_residual -= bytes;
299                 }
300
301                 if (hostdata->pdma_residual == 0)
302                         goto out;
303
304                 if (NCR5380_poll_politely2(hostdata, STATUS_REG, SR_REQ, SR_REQ,
305                                            BUS_AND_STATUS_REG, BASR_ACK,
306                                            BASR_ACK, HZ / 64) < 0)
307                         scmd_printk(KERN_DEBUG, hostdata->connected,
308                                     "%s: !REQ and !ACK\n", __func__);
309                 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH))
310                         goto out;
311
312                 if (bytes == 0)
313                         udelay(MAC_PDMA_DELAY);
314
315                 if (bytes >= 0)
316                         continue;
317
318                 dsprintk(NDEBUG_PSEUDO_DMA, hostdata->host,
319                          "%s: bus error (%d/%d)\n", __func__, d - dst, len);
320                 NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host);
321                 result = -1;
322                 goto out;
323         }
324
325         scmd_printk(KERN_ERR, hostdata->connected,
326                     "%s: phase mismatch or !DRQ\n", __func__);
327         NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host);
328         result = -1;
329 out:
330         if (macintosh_config->ident == MAC_MODEL_IIFX)
331                 write_ctrl_reg(hostdata, CTRL_INTERRUPTS_ENABLE);
332         return result;
333 }
334
335 static inline int macscsi_pwrite(struct NCR5380_hostdata *hostdata,
336                                  unsigned char *src, int len)
337 {
338         unsigned char *s = src;
339         u8 __iomem *d = hostdata->pdma_io + (OUTPUT_DATA_REG << 4);
340         int result = 0;
341
342         hostdata->pdma_residual = len;
343
344         while (!NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG,
345                                       BASR_DRQ | BASR_PHASE_MATCH,
346                                       BASR_DRQ | BASR_PHASE_MATCH, HZ / 64)) {
347                 int bytes;
348
349                 if (macintosh_config->ident == MAC_MODEL_IIFX)
350                         write_ctrl_reg(hostdata, CTRL_HANDSHAKE_MODE |
351                                                  CTRL_INTERRUPTS_ENABLE);
352
353                 bytes = mac_pdma_send(s, d, min(hostdata->pdma_residual, 512));
354
355                 if (bytes > 0) {
356                         s += bytes;
357                         hostdata->pdma_residual -= bytes;
358                 }
359
360                 if (hostdata->pdma_residual == 0) {
361                         if (NCR5380_poll_politely(hostdata, TARGET_COMMAND_REG,
362                                                   TCR_LAST_BYTE_SENT,
363                                                   TCR_LAST_BYTE_SENT,
364                                                   HZ / 64) < 0) {
365                                 scmd_printk(KERN_ERR, hostdata->connected,
366                                             "%s: Last Byte Sent timeout\n", __func__);
367                                 result = -1;
368                         }
369                         goto out;
370                 }
371
372                 if (NCR5380_poll_politely2(hostdata, STATUS_REG, SR_REQ, SR_REQ,
373                                            BUS_AND_STATUS_REG, BASR_ACK,
374                                            BASR_ACK, HZ / 64) < 0)
375                         scmd_printk(KERN_DEBUG, hostdata->connected,
376                                     "%s: !REQ and !ACK\n", __func__);
377                 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH))
378                         goto out;
379
380                 if (bytes == 0)
381                         udelay(MAC_PDMA_DELAY);
382
383                 if (bytes >= 0)
384                         continue;
385
386                 dsprintk(NDEBUG_PSEUDO_DMA, hostdata->host,
387                          "%s: bus error (%d/%d)\n", __func__, s - src, len);
388                 NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host);
389                 result = -1;
390                 goto out;
391         }
392
393         scmd_printk(KERN_ERR, hostdata->connected,
394                     "%s: phase mismatch or !DRQ\n", __func__);
395         NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host);
396         result = -1;
397 out:
398         if (macintosh_config->ident == MAC_MODEL_IIFX)
399                 write_ctrl_reg(hostdata, CTRL_INTERRUPTS_ENABLE);
400         return result;
401 }
402
403 static int macscsi_dma_xfer_len(struct NCR5380_hostdata *hostdata,
404                                 struct scsi_cmnd *cmd)
405 {
406         if (hostdata->flags & FLAG_NO_PSEUDO_DMA ||
407             cmd->SCp.this_residual < setup_use_pdma)
408                 return 0;
409
410         return cmd->SCp.this_residual;
411 }
412
413 static int macscsi_dma_residual(struct NCR5380_hostdata *hostdata)
414 {
415         return hostdata->pdma_residual;
416 }
417
418 #include "NCR5380.c"
419
420 #define DRV_MODULE_NAME         "mac_scsi"
421 #define PFX                     DRV_MODULE_NAME ": "
422
423 static struct scsi_host_template mac_scsi_template = {
424         .module                 = THIS_MODULE,
425         .proc_name              = DRV_MODULE_NAME,
426         .name                   = "Macintosh NCR5380 SCSI",
427         .info                   = macscsi_info,
428         .queuecommand           = macscsi_queue_command,
429         .eh_abort_handler       = macscsi_abort,
430         .eh_host_reset_handler  = macscsi_host_reset,
431         .can_queue              = 16,
432         .this_id                = 7,
433         .sg_tablesize           = 1,
434         .cmd_per_lun            = 2,
435         .dma_boundary           = PAGE_SIZE - 1,
436         .cmd_size               = NCR5380_CMD_SIZE,
437         .max_sectors            = 128,
438 };
439
440 static int __init mac_scsi_probe(struct platform_device *pdev)
441 {
442         struct Scsi_Host *instance;
443         struct NCR5380_hostdata *hostdata;
444         int error;
445         int host_flags = 0;
446         struct resource *irq, *pio_mem, *pdma_mem = NULL;
447
448         pio_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
449         if (!pio_mem)
450                 return -ENODEV;
451
452         pdma_mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
453
454         irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
455
456         if (!hwreg_present((unsigned char *)pio_mem->start +
457                            (STATUS_REG << 4))) {
458                 pr_info(PFX "no device detected at %pap\n", &pio_mem->start);
459                 return -ENODEV;
460         }
461
462         if (setup_can_queue > 0)
463                 mac_scsi_template.can_queue = setup_can_queue;
464         if (setup_cmd_per_lun > 0)
465                 mac_scsi_template.cmd_per_lun = setup_cmd_per_lun;
466         if (setup_sg_tablesize >= 0)
467                 mac_scsi_template.sg_tablesize = setup_sg_tablesize;
468         if (setup_hostid >= 0)
469                 mac_scsi_template.this_id = setup_hostid & 7;
470
471         instance = scsi_host_alloc(&mac_scsi_template,
472                                    sizeof(struct NCR5380_hostdata));
473         if (!instance)
474                 return -ENOMEM;
475
476         if (irq)
477                 instance->irq = irq->start;
478         else
479                 instance->irq = NO_IRQ;
480
481         hostdata = shost_priv(instance);
482         hostdata->base = pio_mem->start;
483         hostdata->io = (u8 __iomem *)pio_mem->start;
484
485         if (pdma_mem && setup_use_pdma)
486                 hostdata->pdma_io = (u8 __iomem *)pdma_mem->start;
487         else
488                 host_flags |= FLAG_NO_PSEUDO_DMA;
489
490         host_flags |= setup_toshiba_delay > 0 ? FLAG_TOSHIBA_DELAY : 0;
491
492         error = NCR5380_init(instance, host_flags | FLAG_LATE_DMA_SETUP);
493         if (error)
494                 goto fail_init;
495
496         if (instance->irq != NO_IRQ) {
497                 error = request_irq(instance->irq, macscsi_intr, IRQF_SHARED,
498                                     "NCR5380", instance);
499                 if (error)
500                         goto fail_irq;
501         }
502
503         NCR5380_maybe_reset_bus(instance);
504
505         error = scsi_add_host(instance, NULL);
506         if (error)
507                 goto fail_host;
508
509         platform_set_drvdata(pdev, instance);
510
511         scsi_scan_host(instance);
512         return 0;
513
514 fail_host:
515         if (instance->irq != NO_IRQ)
516                 free_irq(instance->irq, instance);
517 fail_irq:
518         NCR5380_exit(instance);
519 fail_init:
520         scsi_host_put(instance);
521         return error;
522 }
523
524 static int __exit mac_scsi_remove(struct platform_device *pdev)
525 {
526         struct Scsi_Host *instance = platform_get_drvdata(pdev);
527
528         scsi_remove_host(instance);
529         if (instance->irq != NO_IRQ)
530                 free_irq(instance->irq, instance);
531         NCR5380_exit(instance);
532         scsi_host_put(instance);
533         return 0;
534 }
535
536 static struct platform_driver mac_scsi_driver = {
537         .remove = __exit_p(mac_scsi_remove),
538         .driver = {
539                 .name   = DRV_MODULE_NAME,
540         },
541 };
542
543 module_platform_driver_probe(mac_scsi_driver, mac_scsi_probe);
544
545 MODULE_ALIAS("platform:" DRV_MODULE_NAME);
546 MODULE_LICENSE("GPL");