scsi: ibmvfc: Fix erroneous use of rtas_busy_delay with hcall return code
[linux-2.6-block.git] / drivers / scsi / a3000.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2#include <linux/types.h>
3#include <linux/mm.h>
1da177e4
LT
4#include <linux/ioport.h>
5#include <linux/init.h>
c2a24a4c 6#include <linux/slab.h>
1da177e4
LT
7#include <linux/spinlock.h>
8#include <linux/interrupt.h>
c2a24a4c 9#include <linux/platform_device.h>
e214806d 10#include <linux/dma-mapping.h>
acf3368f 11#include <linux/module.h>
1da177e4 12
1da177e4 13#include <asm/page.h>
1da177e4
LT
14#include <asm/amigaints.h>
15#include <asm/amigahw.h>
1da177e4 16
53555fb7
BVA
17#include <scsi/scsi.h>
18#include <scsi/scsi_cmnd.h>
19#include <scsi/scsi_device.h>
20#include <scsi/scsi_eh.h>
21#include <scsi/scsi_tcq.h>
1da177e4
LT
22#include "wd33c93.h"
23#include "a3000.h"
24
9387edbe 25
2b21d5e4
GU
26struct a3000_hostdata {
27 struct WD33C93_hostdata wh;
28 struct a3000_scsiregs *regs;
e214806d 29 struct device *dev;
2b21d5e4
GU
30};
31
e214806d
MS
32#define DMA_DIR(d) ((d == DATA_OUT_DIR) ? DMA_TO_DEVICE : DMA_FROM_DEVICE)
33
a8169e60 34static irqreturn_t a3000_intr(int irq, void *data)
1da177e4 35{
a8169e60 36 struct Scsi_Host *instance = data;
2b21d5e4
GU
37 struct a3000_hostdata *hdata = shost_priv(instance);
38 unsigned int status = hdata->regs->ISTR;
1da177e4 39 unsigned long flags;
1da177e4
LT
40
41 if (!(status & ISTR_INT_P))
42 return IRQ_NONE;
21351013 43 if (status & ISTR_INTS) {
a8169e60
GU
44 spin_lock_irqsave(instance->host_lock, flags);
45 wd33c93_intr(instance);
46 spin_unlock_irqrestore(instance->host_lock, flags);
1da177e4
LT
47 return IRQ_HANDLED;
48 }
a2cc701b 49 pr_warn("Non-serviced A3000 SCSI-interrupt? ISTR = %02x\n", status);
1da177e4
LT
50 return IRQ_NONE;
51}
52
65396410 53static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
1da177e4 54{
dbb2da55 55 struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(cmd);
e214806d 56 unsigned long len = scsi_pointer->this_residual;
a8169e60 57 struct Scsi_Host *instance = cmd->device->host;
2b21d5e4
GU
58 struct a3000_hostdata *hdata = shost_priv(instance);
59 struct WD33C93_hostdata *wh = &hdata->wh;
60 struct a3000_scsiregs *regs = hdata->regs;
21351013 61 unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
e214806d
MS
62 dma_addr_t addr;
63
64 addr = dma_map_single(hdata->dev, scsi_pointer->ptr,
65 len, DMA_DIR(dir_in));
66 if (dma_mapping_error(hdata->dev, addr)) {
67 dev_warn(hdata->dev, "cannot map SCSI data block %p\n",
68 scsi_pointer->ptr);
69 return 1;
70 }
71 scsi_pointer->dma_handle = addr;
21351013
GU
72
73 /*
74 * if the physical address has the wrong alignment, or if
75 * physical address is bad, or if it is a write and at the
76 * end of a physical memory chunk, then allocate a bounce
77 * buffer
e214806d
MS
78 * MSch 20220629 - only wrong alignment tested - bounce
79 * buffer returned by kmalloc is guaranteed to be aligned
21351013
GU
80 */
81 if (addr & A3000_XFER_MASK) {
e214806d
MS
82 WARN_ONCE(1, "Invalid alignment for DMA!");
83 /* drop useless mapping */
84 dma_unmap_single(hdata->dev, scsi_pointer->dma_handle,
85 scsi_pointer->this_residual,
86 DMA_DIR(dir_in));
87
dbb2da55 88 wh->dma_bounce_len = (scsi_pointer->this_residual + 511) & ~0x1ff;
2b21d5e4
GU
89 wh->dma_bounce_buffer = kmalloc(wh->dma_bounce_len,
90 GFP_KERNEL);
21351013
GU
91
92 /* can't allocate memory; use PIO */
2b21d5e4
GU
93 if (!wh->dma_bounce_buffer) {
94 wh->dma_bounce_len = 0;
e214806d 95 scsi_pointer->dma_handle = (dma_addr_t) NULL;
21351013
GU
96 return 1;
97 }
98
99 if (!dir_in) {
100 /* copy to bounce buffer for a write */
dbb2da55
BVA
101 memcpy(wh->dma_bounce_buffer, scsi_pointer->ptr,
102 scsi_pointer->this_residual);
21351013
GU
103 }
104
e214806d
MS
105 addr = dma_map_single(hdata->dev, scsi_pointer->ptr,
106 len, DMA_DIR(dir_in));
107 if (dma_mapping_error(hdata->dev, addr)) {
108 dev_warn(hdata->dev,
109 "cannot map SCSI data block %p\n",
110 scsi_pointer->ptr);
111 return 1;
112 }
113 scsi_pointer->dma_handle = addr;
1da177e4
LT
114 }
115
21351013
GU
116 /* setup dma direction */
117 if (!dir_in)
118 cntr |= CNTR_DDIR;
1da177e4 119
21351013 120 /* remember direction */
2b21d5e4 121 wh->dma_dir = dir_in;
1da177e4 122
d753722e 123 regs->CNTR = cntr;
1da177e4 124
21351013 125 /* setup DMA *physical* address */
d753722e 126 regs->ACR = addr;
1da177e4 127
e214806d 128 /* no more cache flush here - dma_map_single() takes care */
1da177e4 129
21351013
GU
130 /* start DMA */
131 mb(); /* make sure setup is completed */
d753722e 132 regs->ST_DMA = 1;
21351013 133 mb(); /* make sure DMA has started before next IO */
1da177e4 134
21351013
GU
135 /* return success */
136 return 0;
1da177e4
LT
137}
138
65396410
HK
139static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
140 int status)
1da177e4 141{
dbb2da55 142 struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(SCpnt);
2b21d5e4
GU
143 struct a3000_hostdata *hdata = shost_priv(instance);
144 struct WD33C93_hostdata *wh = &hdata->wh;
145 struct a3000_scsiregs *regs = hdata->regs;
afdbbc16 146
21351013
GU
147 /* disable SCSI interrupts */
148 unsigned short cntr = CNTR_PDMD;
149
2b21d5e4 150 if (!wh->dma_dir)
21351013
GU
151 cntr |= CNTR_DDIR;
152
d753722e 153 regs->CNTR = cntr;
21351013
GU
154 mb(); /* make sure CNTR is updated before next IO */
155
156 /* flush if we were reading */
2b21d5e4 157 if (wh->dma_dir) {
d753722e 158 regs->FLUSH = 1;
21351013 159 mb(); /* don't allow prefetch */
d753722e 160 while (!(regs->ISTR & ISTR_FE_FLG))
21351013
GU
161 barrier();
162 mb(); /* no IO until FLUSH is done */
163 }
164
165 /* clear a possible interrupt */
166 /* I think that this CINT is only necessary if you are
167 * using the terminal count features. HM 7 Mar 1994
168 */
d753722e 169 regs->CINT = 1;
21351013
GU
170
171 /* stop DMA */
d753722e 172 regs->SP_DMA = 1;
21351013
GU
173 mb(); /* make sure DMA is stopped before next IO */
174
175 /* restore the CONTROL bits (minus the direction flag) */
d753722e 176 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
21351013
GU
177 mb(); /* make sure CNTR is updated before next IO */
178
e214806d
MS
179 dma_unmap_single(hdata->dev, scsi_pointer->dma_handle,
180 scsi_pointer->this_residual,
181 DMA_DIR(wh->dma_dir));
182
21351013 183 /* copy from a bounce buffer, if necessary */
2b21d5e4 184 if (status && wh->dma_bounce_buffer) {
21351013 185 if (SCpnt) {
2b21d5e4 186 if (wh->dma_dir && SCpnt)
dbb2da55
BVA
187 memcpy(scsi_pointer->ptr, wh->dma_bounce_buffer,
188 scsi_pointer->this_residual);
2b21d5e4
GU
189 kfree(wh->dma_bounce_buffer);
190 wh->dma_bounce_buffer = NULL;
191 wh->dma_bounce_len = 0;
21351013 192 } else {
2b21d5e4
GU
193 kfree(wh->dma_bounce_buffer);
194 wh->dma_bounce_buffer = NULL;
195 wh->dma_bounce_len = 0;
21351013 196 }
1da177e4 197 }
1da177e4
LT
198}
199
88530b3e 200static const struct scsi_host_template amiga_a3000_scsi_template = {
c2a24a4c
GU
201 .module = THIS_MODULE,
202 .name = "Amiga 3000 built-in SCSI",
408bb25b
AV
203 .show_info = wd33c93_show_info,
204 .write_info = wd33c93_write_info,
c2a24a4c
GU
205 .proc_name = "A3000",
206 .queuecommand = wd33c93_queuecommand,
207 .eh_abort_handler = wd33c93_abort,
c2a24a4c
GU
208 .eh_host_reset_handler = wd33c93_host_reset,
209 .can_queue = CAN_QUEUE,
210 .this_id = 7,
211 .sg_tablesize = SG_ALL,
212 .cmd_per_lun = CMD_PER_LUN,
dbb2da55 213 .cmd_size = sizeof(struct scsi_pointer),
c2a24a4c
GU
214};
215
216static int __init amiga_a3000_scsi_probe(struct platform_device *pdev)
1da177e4 217{
c2a24a4c 218 struct resource *res;
a8169e60 219 struct Scsi_Host *instance;
c2a24a4c 220 int error;
c57c1cab 221 struct a3000_scsiregs *regs;
c2a24a4c 222 wd33c93_regs wdregs;
2b21d5e4 223 struct a3000_hostdata *hdata;
21351013 224
e214806d
MS
225 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32))) {
226 dev_warn(&pdev->dev, "cannot use 32 bit DMA\n");
227 return -ENODEV;
228 }
229
c2a24a4c
GU
230 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
231 if (!res)
232 return -ENODEV;
21351013 233
c2a24a4c
GU
234 if (!request_mem_region(res->start, resource_size(res), "wd33c93"))
235 return -EBUSY;
21351013 236
c2a24a4c 237 instance = scsi_host_alloc(&amiga_a3000_scsi_template,
2b21d5e4 238 sizeof(struct a3000_hostdata));
c2a24a4c
GU
239 if (!instance) {
240 error = -ENOMEM;
241 goto fail_alloc;
242 }
21351013 243
a8169e60 244 instance->irq = IRQ_AMIGA_PORTS;
c2a24a4c 245
6112ea08 246 regs = ZTWO_VADDR(res->start);
d753722e 247 regs->DAWR = DAWR_A3000;
c2a24a4c 248
d753722e
GU
249 wdregs.SASR = &regs->SASR;
250 wdregs.SCMD = &regs->SCMD;
c2a24a4c 251
a8169e60 252 hdata = shost_priv(instance);
e214806d 253 hdata->dev = &pdev->dev;
2b21d5e4
GU
254 hdata->wh.no_sync = 0xff;
255 hdata->wh.fast = 0;
256 hdata->wh.dma_mode = CTRL_DMA;
257 hdata->regs = regs;
c2a24a4c 258
a8169e60 259 wd33c93_init(instance, wdregs, dma_setup, dma_stop, WD33C93_FS_12_15);
c2a24a4c
GU
260 error = request_irq(IRQ_AMIGA_PORTS, a3000_intr, IRQF_SHARED,
261 "A3000 SCSI", instance);
262 if (error)
21351013 263 goto fail_irq;
c2a24a4c 264
d753722e 265 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
21351013 266
c2a24a4c
GU
267 error = scsi_add_host(instance, NULL);
268 if (error)
269 goto fail_host;
1da177e4 270
c2a24a4c
GU
271 platform_set_drvdata(pdev, instance);
272
273 scsi_scan_host(instance);
21351013 274 return 0;
c2a24a4c
GU
275
276fail_host:
277 free_irq(IRQ_AMIGA_PORTS, instance);
278fail_irq:
279 scsi_host_put(instance);
280fail_alloc:
281 release_mem_region(res->start, resource_size(res));
282 return error;
1da177e4
LT
283}
284
c2a24a4c 285static int __exit amiga_a3000_scsi_remove(struct platform_device *pdev)
1da177e4 286{
c2a24a4c 287 struct Scsi_Host *instance = platform_get_drvdata(pdev);
2b21d5e4 288 struct a3000_hostdata *hdata = shost_priv(instance);
c2a24a4c 289 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
68b3aa7c 290
2b21d5e4 291 hdata->regs->CNTR = 0;
c2a24a4c
GU
292 scsi_remove_host(instance);
293 free_irq(IRQ_AMIGA_PORTS, instance);
294 scsi_host_put(instance);
295 release_mem_region(res->start, resource_size(res));
296 return 0;
1da177e4
LT
297}
298
c2a24a4c
GU
299static struct platform_driver amiga_a3000_scsi_driver = {
300 .remove = __exit_p(amiga_a3000_scsi_remove),
301 .driver = {
302 .name = "amiga-a3000-scsi",
c2a24a4c 303 },
1da177e4
LT
304};
305
a915b84a 306module_platform_driver_probe(amiga_a3000_scsi_driver, amiga_a3000_scsi_probe);
1da177e4 307
c2a24a4c 308MODULE_DESCRIPTION("Amiga 3000 built-in SCSI");
1da177e4 309MODULE_LICENSE("GPL");
c2a24a4c 310MODULE_ALIAS("platform:amiga-a3000-scsi");