blob: 16709735b5466a522d4a9e8cb3f040d746c2de9d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/************************************************************************
2 * Linux driver for *
3 * ICP vortex GmbH: GDT ISA/EISA/PCI Disk Array Controllers *
4 * Intel Corporation: Storage RAID Controllers *
5 * *
6 * gdth.c *
Leubner, Achimcbd5f692006-06-09 11:34:29 -07007 * Copyright (C) 1995-06 ICP vortex GmbH, Achim Leubner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Copyright (C) 2002-04 Intel Corporation *
Leubner, Achimcbd5f692006-06-09 11:34:29 -07009 * Copyright (C) 2003-06 Adaptec Inc. *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * <achim_leubner@adaptec.com> *
11 * *
12 * Additions/Fixes: *
13 * Boji Tony Kannanthanam <boji.t.kannanthanam@intel.com> *
14 * Johannes Dinner <johannes_dinner@adaptec.com> *
15 * *
16 * This program is free software; you can redistribute it and/or modify *
17 * it under the terms of the GNU General Public License as published *
18 * by the Free Software Foundation; either version 2 of the License, *
19 * or (at your option) any later version. *
20 * *
21 * This program is distributed in the hope that it will be useful, *
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
24 * GNU General Public License for more details. *
25 * *
26 * You should have received a copy of the GNU General Public License *
27 * along with this kernel; if not, write to the Free Software *
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
29 * *
Jeff Garzik8d7a5da2007-10-02 22:54:28 +020030 * Linux kernel 2.6.x supported *
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * *
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 ************************************************************************/
33
34/* All GDT Disk Array Controllers are fully supported by this driver.
35 * This includes the PCI/EISA/ISA SCSI Disk Array Controllers and the
36 * PCI Fibre Channel Disk Array Controllers. See gdth.h for a complete
37 * list of all controller types.
38 *
39 * If you have one or more GDT3000/3020 EISA controllers with
40 * controller BIOS disabled, you have to set the IRQ values with the
41 * command line option "gdth=irq1,irq2,...", where the irq1,irq2,... are
42 * the IRQ values for the EISA controllers.
43 *
44 * After the optional list of IRQ values, other possible
45 * command line options are:
46 * disable:Y disable driver
47 * disable:N enable driver
48 * reserve_mode:0 reserve no drives for the raw service
49 * reserve_mode:1 reserve all not init., removable drives
50 * reserve_mode:2 reserve all not init. drives
51 * reserve_list:h,b,t,l,h,b,t,l,... reserve particular drive(s) with
52 * h- controller no., b- channel no.,
53 * t- target ID, l- LUN
54 * reverse_scan:Y reverse scan order for PCI controllers
55 * reverse_scan:N scan PCI controllers like BIOS
56 * max_ids:x x - target ID count per channel (1..MAXID)
57 * rescan:Y rescan all channels/IDs
58 * rescan:N use all devices found until now
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * hdr_channel:x x - number of virtual bus for host drives
60 * shared_access:Y disable driver reserve/release protocol to
61 * access a shared resource from several nodes,
Adrian Bunk575c9682006-01-15 02:00:17 +010062 * appropriate controller firmware required
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * shared_access:N enable driver reserve/release protocol
64 * probe_eisa_isa:Y scan for EISA/ISA controllers
65 * probe_eisa_isa:N do not scan for EISA/ISA controllers
66 * force_dma32:Y use only 32 bit DMA mode
67 * force_dma32:N use 64 bit DMA mode, if supported
68 *
69 * The default values are: "gdth=disable:N,reserve_mode:1,reverse_scan:N,
Christoph Hellwig52759e62007-10-02 22:59:53 +020070 * max_ids:127,rescan:N,hdr_channel:0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 * shared_access:Y,probe_eisa_isa:N,force_dma32:N".
72 * Here is another example: "gdth=reserve_list:0,1,2,0,0,1,3,0,rescan:Y".
73 *
74 * When loading the gdth driver as a module, the same options are available.
75 * You can set the IRQs with "IRQ=...". However, the syntax to specify the
76 * options changes slightly. You must replace all ',' between options
77 * with ' ' and all ':' with '=' and you must use
78 * '1' in place of 'Y' and '0' in place of 'N'.
79 *
80 * Default: "modprobe gdth disable=0 reserve_mode=1 reverse_scan=0
Christoph Hellwig52759e62007-10-02 22:59:53 +020081 * max_ids=127 rescan=0 hdr_channel=0 shared_access=0
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * probe_eisa_isa=0 force_dma32=0"
83 * The other example: "modprobe gdth reserve_list=0,1,2,0,0,1,3,0 rescan=1".
84 */
85
86/* The meaning of the Scsi_Pointer members in this driver is as follows:
87 * ptr: Chaining
Boaz Harroshd35055a2008-02-27 15:31:20 -080088 * this_residual: unused
89 * buffer: unused
Boaz Harrosh3892d882007-10-02 23:18:03 +020090 * dma_handle: unused
Boaz Harroshd35055a2008-02-27 15:31:20 -080091 * buffers_residual: unused
Boaz Harrosh3892d882007-10-02 23:18:03 +020092 * Status: unused
Boaz Harroshf842b642007-10-02 23:16:01 +020093 * Message: unused
94 * have_data_in: unused
95 * sent_command: unused
96 * phase: unused
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 */
98
99
100/* interrupt coalescing */
101/* #define INT_COAL */
102
103/* statistics */
104#define GDTH_STATISTICS
105
106#include <linux/module.h>
107
108#include <linux/version.h>
109#include <linux/kernel.h>
110#include <linux/types.h>
111#include <linux/pci.h>
112#include <linux/string.h>
113#include <linux/ctype.h>
114#include <linux/ioport.h>
115#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#include <linux/interrupt.h>
117#include <linux/in.h>
118#include <linux/proc_fs.h>
119#include <linux/time.h>
120#include <linux/timer.h>
Matthias Gehre910638a2006-03-28 01:56:48 -0800121#include <linux/dma-mapping.h>
Christoph Hellwig835cc242007-10-02 23:09:56 +0200122#include <linux/list.h>
Arnd Bergmannc45d15d2010-06-02 14:28:52 +0200123#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +0900124#include <linux/slab.h>
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#ifdef GDTH_RTC
127#include <linux/mc146818rtc.h>
128#endif
129#include <linux/reboot.h>
130
131#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#include <asm/io.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -0800133#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#include <linux/blkdev.h>
Boaz Harrosh3892d882007-10-02 23:18:03 +0200136#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138#include "scsi.h"
139#include <scsi/scsi_host.h>
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700140#include "gdth.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Arnd Bergmannc45d15d2010-06-02 14:28:52 +0200142static DEFINE_MUTEX(gdth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static void gdth_delay(int milliseconds);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500144static void gdth_eval_mapping(u32 size, u32 *cyls, int *heads, int *secs);
David Howells7d12e782006-10-05 14:55:46 +0100145static irqreturn_t gdth_interrupt(int irq, void *dev_id);
Jeff Garzik230e8862007-12-13 16:14:12 -0800146static irqreturn_t __gdth_interrupt(gdth_ha_str *ha,
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +0200147 int gdth_from_wait, int* pIndex);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500148static int gdth_sync_event(gdth_ha_str *ha, int service, u8 index,
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +0200149 struct scsi_cmnd *scp);
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200150static int gdth_async_event(gdth_ha_str *ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static void gdth_log_event(gdth_evt_data *dvr, char *buffer);
152
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +0200153static void gdth_putq(gdth_ha_str *ha, struct scsi_cmnd *scp, u8 priority);
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200154static void gdth_next(gdth_ha_str *ha);
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +0200155static int gdth_fill_raw_cmd(gdth_ha_str *ha, struct scsi_cmnd *scp, u8 b);
156static int gdth_special_cmd(gdth_ha_str *ha, struct scsi_cmnd *scp);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500157static gdth_evt_str *gdth_store_event(gdth_ha_str *ha, u16 source,
158 u16 idx, gdth_evt_data *evt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static int gdth_read_event(gdth_ha_str *ha, int handle, gdth_evt_str *estr);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500160static void gdth_readapp_event(gdth_ha_str *ha, u8 application,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 gdth_evt_str *estr);
162static void gdth_clear_events(void);
163
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +0200164static void gdth_copy_internal_data(gdth_ha_str *ha, struct scsi_cmnd *scp,
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500165 char *buffer, u16 count);
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +0200166static int gdth_internal_cache_cmd(gdth_ha_str *ha, struct scsi_cmnd *scp);
167static int gdth_fill_cache_cmd(gdth_ha_str *ha, struct scsi_cmnd *scp,
168 u16 hdrive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200170static void gdth_enable_int(gdth_ha_str *ha);
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200171static int gdth_test_busy(gdth_ha_str *ha);
172static int gdth_get_cmd_index(gdth_ha_str *ha);
173static void gdth_release_event(gdth_ha_str *ha);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500174static int gdth_wait(gdth_ha_str *ha, int index,u32 time);
175static int gdth_internal_cmd(gdth_ha_str *ha, u8 service, u16 opcode,
176 u32 p1, u64 p2,u64 p3);
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200177static int gdth_search_drives(gdth_ha_str *ha);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500178static int gdth_analyse_hdrive(gdth_ha_str *ha, u16 hdrive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200180static const char *gdth_ctr_name(gdth_ha_str *ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182static int gdth_open(struct inode *inode, struct file *filep);
183static int gdth_close(struct inode *inode, struct file *filep);
Arnd Bergmannf4927c42010-04-27 00:24:01 +0200184static long gdth_unlocked_ioctl(struct file *filep, unsigned int cmd,
185 unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200187static void gdth_flush(gdth_ha_str *ha);
Jeff Garzikf2812332010-11-16 02:10:29 -0500188static int gdth_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200189static int __gdth_queuecommand(gdth_ha_str *ha, struct scsi_cmnd *scp,
190 struct gdth_cmndinfo *cmndinfo);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700191static void gdth_scsi_done(struct scsi_cmnd *scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193#ifdef DEBUG_GDTH
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500194static u8 DebugState = DEBUG_GDTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196#ifdef __SERIAL__
197#define MAX_SERBUF 160
198static void ser_init(void);
199static void ser_puts(char *str);
200static void ser_putc(char c);
201static int ser_printk(const char *fmt, ...);
202static char strbuf[MAX_SERBUF+1];
203#ifdef __COM2__
204#define COM_BASE 0x2f8
205#else
206#define COM_BASE 0x3f8
207#endif
208static void ser_init()
209{
210 unsigned port=COM_BASE;
211
212 outb(0x80,port+3);
213 outb(0,port+1);
214 /* 19200 Baud, if 9600: outb(12,port) */
215 outb(6, port);
216 outb(3,port+3);
217 outb(0,port+1);
218 /*
219 ser_putc('I');
220 ser_putc(' ');
221 */
222}
223
224static void ser_puts(char *str)
225{
226 char *ptr;
227
228 ser_init();
229 for (ptr=str;*ptr;++ptr)
230 ser_putc(*ptr);
231}
232
233static void ser_putc(char c)
234{
235 unsigned port=COM_BASE;
236
237 while ((inb(port+5) & 0x20)==0);
238 outb(c,port);
239 if (c==0x0a)
240 {
241 while ((inb(port+5) & 0x20)==0);
242 outb(0x0d,port);
243 }
244}
245
246static int ser_printk(const char *fmt, ...)
247{
248 va_list args;
249 int i;
250
251 va_start(args,fmt);
252 i = vsprintf(strbuf,fmt,args);
253 ser_puts(strbuf);
254 va_end(args);
255 return i;
256}
257
258#define TRACE(a) {if (DebugState==1) {ser_printk a;}}
259#define TRACE2(a) {if (DebugState==1 || DebugState==2) {ser_printk a;}}
260#define TRACE3(a) {if (DebugState!=0) {ser_printk a;}}
261
262#else /* !__SERIAL__ */
263#define TRACE(a) {if (DebugState==1) {printk a;}}
264#define TRACE2(a) {if (DebugState==1 || DebugState==2) {printk a;}}
265#define TRACE3(a) {if (DebugState!=0) {printk a;}}
266#endif
267
268#else /* !DEBUG */
269#define TRACE(a)
270#define TRACE2(a)
271#define TRACE3(a)
272#endif
273
274#ifdef GDTH_STATISTICS
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500275static u32 max_rq=0, max_index=0, max_sg=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276#ifdef INT_COAL
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500277static u32 max_int_coal=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278#endif
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500279static u32 act_ints=0, act_ios=0, act_stats=0, act_rq=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280static struct timer_list gdth_timer;
281#endif
282
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500283#define PTR2USHORT(a) (u16)(unsigned long)(a)
Tobias Klauser6391a112006-06-08 22:23:48 -0700284#define GDTOFFSOF(a,b) (size_t)&(((a*)0)->b)
285#define INDEX_OK(i,t) ((i)<ARRAY_SIZE(t))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287#define BUS_L2P(a,b) ((b)>(a)->virt_bus ? (b-1):(b))
288
Christoph Hellwigaed91cb2007-10-02 22:48:16 +0200289#ifdef CONFIG_ISA
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500290static u8 gdth_drq_tab[4] = {5,6,7,7}; /* DRQ table */
Christoph Hellwigaed91cb2007-10-02 22:48:16 +0200291#endif
Dhaval Gianic8e91b02007-10-12 15:12:31 +0530292#if defined(CONFIG_EISA) || defined(CONFIG_ISA)
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500293static u8 gdth_irq_tab[6] = {0,10,11,12,14,0}; /* IRQ table */
Christoph Hellwig706a5d42007-10-02 22:49:35 +0200294#endif
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500295static u8 gdth_polling; /* polling if TRUE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296static int gdth_ctr_count = 0; /* controller count */
Boaz Harrosh884f7fb2007-10-02 23:11:24 +0200297static LIST_HEAD(gdth_instances); /* controller list */
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500298static u8 gdth_write_through = FALSE; /* write through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299static gdth_evt_str ebuffer[MAX_EVENTS]; /* event buffer */
300static int elastidx;
301static int eoldidx;
302static int major;
303
304#define DIN 1 /* IN data direction */
305#define DOU 2 /* OUT data direction */
306#define DNO DIN /* no data transfer */
307#define DUN DIN /* unknown data direction */
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500308static u8 gdth_direction_tab[0x100] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 DNO,DNO,DIN,DIN,DOU,DIN,DIN,DOU,DIN,DUN,DOU,DOU,DUN,DUN,DUN,DIN,
310 DNO,DIN,DIN,DOU,DIN,DOU,DNO,DNO,DOU,DNO,DIN,DNO,DIN,DOU,DNO,DUN,
311 DIN,DUN,DIN,DUN,DOU,DIN,DUN,DUN,DIN,DIN,DOU,DNO,DUN,DIN,DOU,DOU,
312 DOU,DOU,DOU,DNO,DIN,DNO,DNO,DIN,DOU,DOU,DOU,DOU,DIN,DOU,DIN,DOU,
313 DOU,DOU,DIN,DIN,DIN,DNO,DUN,DNO,DNO,DNO,DUN,DNO,DOU,DIN,DUN,DUN,
314 DUN,DUN,DUN,DUN,DUN,DOU,DUN,DUN,DUN,DUN,DIN,DUN,DUN,DUN,DUN,DUN,
315 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
316 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
317 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DIN,DUN,DOU,DUN,DUN,DUN,DUN,DUN,
318 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DIN,DUN,
319 DUN,DUN,DUN,DUN,DUN,DNO,DNO,DUN,DIN,DNO,DOU,DUN,DNO,DUN,DOU,DOU,
320 DOU,DOU,DOU,DNO,DUN,DIN,DOU,DIN,DIN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
321 DUN,DUN,DOU,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
322 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
323 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DOU,DUN,DUN,DUN,DUN,DUN,
324 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN
325};
326
327/* LILO and modprobe/insmod parameters */
328/* IRQ list for GDT3000/3020 EISA controllers */
329static int irq[MAXHA] __initdata =
330{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
331 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
332/* disable driver flag */
333static int disable __initdata = 0;
334/* reserve flag */
335static int reserve_mode = 1;
336/* reserve list */
337static int reserve_list[MAX_RES_ARGS] =
338{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
339 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
340 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
341/* scan order for PCI controllers */
342static int reverse_scan = 0;
343/* virtual channel for the host drives */
344static int hdr_channel = 0;
345/* max. IDs per channel */
346static int max_ids = MAXID;
347/* rescan all IDs */
348static int rescan = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349/* shared access */
350static int shared_access = 1;
351/* enable support for EISA and ISA controllers */
352static int probe_eisa_isa = 0;
353/* 64 bit DMA mode, support for drives > 2 TB, if force_dma32 = 0 */
354static int force_dma32 = 0;
355
356/* parameters for modprobe/insmod */
David Howells88f06b72017-04-04 16:54:27 +0100357module_param_hw_array(irq, int, irq, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358module_param(disable, int, 0);
359module_param(reserve_mode, int, 0);
360module_param_array(reserve_list, int, NULL, 0);
361module_param(reverse_scan, int, 0);
362module_param(hdr_channel, int, 0);
363module_param(max_ids, int, 0);
364module_param(rescan, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365module_param(shared_access, int, 0);
366module_param(probe_eisa_isa, int, 0);
367module_param(force_dma32, int, 0);
368MODULE_AUTHOR("Achim Leubner");
369MODULE_LICENSE("GPL");
370
371/* ioctl interface */
Arjan van de Ven00977a52007-02-12 00:55:34 -0800372static const struct file_operations gdth_fops = {
Arnd Bergmannf4927c42010-04-27 00:24:01 +0200373 .unlocked_ioctl = gdth_unlocked_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 .open = gdth_open,
375 .release = gdth_close,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200376 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377};
378
379#include "gdth_proc.h"
380#include "gdth_proc.c"
381
Boaz Harrosh884f7fb2007-10-02 23:11:24 +0200382static gdth_ha_str *gdth_find_ha(int hanum)
383{
384 gdth_ha_str *ha;
385
386 list_for_each_entry(ha, &gdth_instances, list)
387 if (hanum == ha->hanum)
388 return ha;
389
390 return NULL;
391}
392
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200393static struct gdth_cmndinfo *gdth_get_cmndinfo(gdth_ha_str *ha)
394{
395 struct gdth_cmndinfo *priv = NULL;
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500396 unsigned long flags;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200397 int i;
398
399 spin_lock_irqsave(&ha->smp_lock, flags);
400
401 for (i=0; i<GDTH_MAXCMDS; ++i) {
402 if (ha->cmndinfo[i].index == 0) {
403 priv = &ha->cmndinfo[i];
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200404 memset(priv, 0, sizeof(*priv));
Boaz Harroshee54cc62008-02-27 15:29:15 -0800405 priv->index = i+1;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200406 break;
407 }
408 }
409
410 spin_unlock_irqrestore(&ha->smp_lock, flags);
411
412 return priv;
413}
414
415static void gdth_put_cmndinfo(struct gdth_cmndinfo *priv)
416{
417 BUG_ON(!priv);
418 priv->index = 0;
419}
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421static void gdth_delay(int milliseconds)
422{
423 if (milliseconds == 0) {
424 udelay(1);
425 } else {
426 mdelay(milliseconds);
427 }
428}
429
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700430static void gdth_scsi_done(struct scsi_cmnd *scp)
431{
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200432 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
433 int internal_command = cmndinfo->internal_command;
434
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +0200435 TRACE2(("gdth_scsi_done()\n"));
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700436
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200437 gdth_put_cmndinfo(cmndinfo);
438 scp->host_scribble = NULL;
439
440 if (internal_command)
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +0200441 complete((struct completion *)scp->request);
442 else
443 scp->scsi_done(scp);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700444}
445
446int __gdth_execute(struct scsi_device *sdev, gdth_cmd_str *gdtcmd, char *cmnd,
447 int timeout, u32 *info)
448{
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200449 gdth_ha_str *ha = shost_priv(sdev->host);
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +0200450 struct scsi_cmnd *scp;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200451 struct gdth_cmndinfo cmndinfo;
Peter Zijlstra6e9a4732006-09-30 23:28:10 -0700452 DECLARE_COMPLETION_ONSTACK(wait);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700453 int rval;
454
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +0200455 scp = kzalloc(sizeof(*scp), GFP_KERNEL);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700456 if (!scp)
457 return -ENOMEM;
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +0200458
Sven Schnelle1b96f892008-03-10 22:50:04 +0100459 scp->sense_buffer = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
460 if (!scp->sense_buffer) {
461 kfree(scp);
462 return -ENOMEM;
463 }
464
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700465 scp->device = sdev;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200466 memset(&cmndinfo, 0, sizeof(cmndinfo));
467
Christoph Hellwigbeb40482006-06-10 18:01:03 +0200468 /* use request field to save the ptr. to completion struct. */
469 scp->request = (struct request *)&wait;
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700470 scp->cmd_len = 12;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300471 scp->cmnd = cmnd;
Boaz Harroshf842b642007-10-02 23:16:01 +0200472 cmndinfo.priority = IOCTL_PRI;
Boaz Harroshee54cc62008-02-27 15:29:15 -0800473 cmndinfo.internal_cmd_str = gdtcmd;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200474 cmndinfo.internal_command = 1;
475
476 TRACE(("__gdth_execute() cmd 0x%x\n", scp->cmnd[0]));
477 __gdth_queuecommand(ha, scp, &cmndinfo);
478
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700479 wait_for_completion(&wait);
480
Boaz Harroshf842b642007-10-02 23:16:01 +0200481 rval = cmndinfo.status;
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700482 if (info)
Boaz Harroshf842b642007-10-02 23:16:01 +0200483 *info = cmndinfo.info;
Sven Schnelle1b96f892008-03-10 22:50:04 +0100484 kfree(scp->sense_buffer);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700485 kfree(scp);
486 return rval;
487}
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700488
489int gdth_execute(struct Scsi_Host *shost, gdth_cmd_str *gdtcmd, char *cmnd,
490 int timeout, u32 *info)
491{
492 struct scsi_device *sdev = scsi_get_host_dev(shost);
493 int rval = __gdth_execute(sdev, gdtcmd, cmnd, timeout, info);
494
495 scsi_free_host_dev(sdev);
496 return rval;
497}
498
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500499static void gdth_eval_mapping(u32 size, u32 *cyls, int *heads, int *secs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 *cyls = size /HEADS/SECS;
502 if (*cyls <= MAXCYLS) {
503 *heads = HEADS;
504 *secs = SECS;
505 } else { /* too high for 64*32 */
506 *cyls = size /MEDHEADS/MEDSECS;
507 if (*cyls <= MAXCYLS) {
508 *heads = MEDHEADS;
509 *secs = MEDSECS;
510 } else { /* too high for 127*63 */
511 *cyls = size /BIGHEADS/BIGSECS;
512 *heads = BIGHEADS;
513 *secs = BIGSECS;
514 }
515 }
516}
517
518/* controller search and initialization functions */
Christoph Hellwig706a5d42007-10-02 22:49:35 +0200519#ifdef CONFIG_EISA
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500520static int __init gdth_search_eisa(u16 eisa_adr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500522 u32 id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 TRACE(("gdth_search_eisa() adr. %x\n",eisa_adr));
525 id = inl(eisa_adr+ID0REG);
526 if (id == GDT3A_ID || id == GDT3B_ID) { /* GDT3000A or GDT3000B */
527 if ((inb(eisa_adr+EISAREG) & 8) == 0)
528 return 0; /* not EISA configured */
529 return 1;
530 }
531 if (id == GDT3_ID) /* GDT3000 */
532 return 1;
533
534 return 0;
535}
Christoph Hellwig706a5d42007-10-02 22:49:35 +0200536#endif /* CONFIG_EISA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Christoph Hellwigaed91cb2007-10-02 22:48:16 +0200538#ifdef CONFIG_ISA
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500539static int __init gdth_search_isa(u32 bios_adr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 void __iomem *addr;
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500542 u32 id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 TRACE(("gdth_search_isa() bios adr. %x\n",bios_adr));
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500545 if ((addr = ioremap(bios_adr+BIOS_ID_OFFS, sizeof(u32))) != NULL) {
Jeff Garzika52667f2007-10-02 22:55:53 +0200546 id = readl(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 iounmap(addr);
548 if (id == GDT2_ID) /* GDT2000 */
549 return 1;
550 }
551 return 0;
552}
Christoph Hellwigaed91cb2007-10-02 22:48:16 +0200553#endif /* CONFIG_ISA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Christoph Hellwig8514ef22007-10-02 22:51:06 +0200555#ifdef CONFIG_PCI
Christoph Hellwig8514ef22007-10-02 22:51:06 +0200556
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500557static bool gdth_search_vortex(u16 device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Jeff Garzikcff26802008-02-15 22:20:09 -0500559 if (device <= PCI_DEVICE_ID_VORTEX_GDT6555)
560 return true;
561 if (device >= PCI_DEVICE_ID_VORTEX_GDT6x17RP &&
562 device <= PCI_DEVICE_ID_VORTEX_GDTMAXRP)
563 return true;
564 if (device == PCI_DEVICE_ID_VORTEX_GDTNEWRX ||
565 device == PCI_DEVICE_ID_VORTEX_GDTNEWRX2)
566 return true;
567 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
Jeff Garzikcff26802008-02-15 22:20:09 -0500570static int gdth_pci_probe_one(gdth_pci_str *pcistr, gdth_ha_str **ha_out);
571static int gdth_pci_init_one(struct pci_dev *pdev,
572 const struct pci_device_id *ent);
573static void gdth_pci_remove_one(struct pci_dev *pdev);
574static void gdth_remove_one(gdth_ha_str *ha);
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576/* Vortex only makes RAID controllers.
577 * We do not really want to specify all 550 ids here, so wildcard match.
578 */
Jeff Garzikcff26802008-02-15 22:20:09 -0500579static const struct pci_device_id gdthtable[] = {
580 { PCI_VDEVICE(VORTEX, PCI_ANY_ID) },
581 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_SRC) },
582 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_SRC_XSCALE) },
583 { } /* terminate list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584};
Jeff Garzikcff26802008-02-15 22:20:09 -0500585MODULE_DEVICE_TABLE(pci, gdthtable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Jeff Garzikcff26802008-02-15 22:20:09 -0500587static struct pci_driver gdth_pci_driver = {
588 .name = "gdth",
589 .id_table = gdthtable,
590 .probe = gdth_pci_init_one,
591 .remove = gdth_pci_remove_one,
592};
593
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800594static void gdth_pci_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Jeff Garzikcff26802008-02-15 22:20:09 -0500596 gdth_ha_str *ha = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Jeff Garzikcff26802008-02-15 22:20:09 -0500598 list_del(&ha->list);
599 gdth_remove_one(ha);
600
601 pci_disable_device(pdev);
602}
603
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800604static int gdth_pci_init_one(struct pci_dev *pdev,
605 const struct pci_device_id *ent)
Jeff Garzikcff26802008-02-15 22:20:09 -0500606{
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500607 u16 vendor = pdev->vendor;
608 u16 device = pdev->device;
609 unsigned long base0, base1, base2;
Jeff Garzikcff26802008-02-15 22:20:09 -0500610 int rc;
611 gdth_pci_str gdth_pcistr;
612 gdth_ha_str *ha = NULL;
613
614 TRACE(("gdth_search_dev() cnt %d vendor %x device %x\n",
615 gdth_ctr_count, vendor, device));
616
617 memset(&gdth_pcistr, 0, sizeof(gdth_pcistr));
618
619 if (vendor == PCI_VENDOR_ID_VORTEX && !gdth_search_vortex(device))
620 return -ENODEV;
621
622 rc = pci_enable_device(pdev);
623 if (rc)
624 return rc;
625
626 if (gdth_ctr_count >= MAXHA)
627 return -EBUSY;
Sergio Luis99109302008-02-12 20:48:03 -0300628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /* GDT PCI controller found, resources are already in pdev */
Jeff Garzikcff26802008-02-15 22:20:09 -0500630 gdth_pcistr.pdev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 base0 = pci_resource_flags(pdev, 0);
632 base1 = pci_resource_flags(pdev, 1);
633 base2 = pci_resource_flags(pdev, 2);
634 if (device <= PCI_DEVICE_ID_VORTEX_GDT6000B || /* GDT6000/B */
635 device >= PCI_DEVICE_ID_VORTEX_GDT6x17RP) { /* MPR */
636 if (!(base0 & IORESOURCE_MEM))
Jeff Garzikcff26802008-02-15 22:20:09 -0500637 return -ENODEV;
638 gdth_pcistr.dpmem = pci_resource_start(pdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 } else { /* GDT6110, GDT6120, .. */
640 if (!(base0 & IORESOURCE_MEM) ||
641 !(base2 & IORESOURCE_MEM) ||
642 !(base1 & IORESOURCE_IO))
Jeff Garzikcff26802008-02-15 22:20:09 -0500643 return -ENODEV;
644 gdth_pcistr.dpmem = pci_resource_start(pdev, 2);
645 gdth_pcistr.io = pci_resource_start(pdev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647 TRACE2(("Controller found at %d/%d, irq %d, dpmem 0x%lx\n",
Jeff Garzikcff26802008-02-15 22:20:09 -0500648 gdth_pcistr.pdev->bus->number,
649 PCI_SLOT(gdth_pcistr.pdev->devfn),
650 gdth_pcistr.irq,
651 gdth_pcistr.dpmem));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Jeff Garzikcff26802008-02-15 22:20:09 -0500653 rc = gdth_pci_probe_one(&gdth_pcistr, &ha);
654 if (rc)
655 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Jeff Garzikcff26802008-02-15 22:20:09 -0500657 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
Christoph Hellwig8514ef22007-10-02 22:51:06 +0200659#endif /* CONFIG_PCI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Christoph Hellwig706a5d42007-10-02 22:49:35 +0200661#ifdef CONFIG_EISA
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500662static int __init gdth_init_eisa(u16 eisa_adr,gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500664 u32 retries,id;
665 u8 prot_ver,eisacf,i,irq_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 TRACE(("gdth_init_eisa() adr. %x\n",eisa_adr));
668
669 /* disable board interrupts, deinitialize services */
670 outb(0xff,eisa_adr+EDOORREG);
671 outb(0x00,eisa_adr+EDENABREG);
672 outb(0x00,eisa_adr+EINTENABREG);
673
674 outb(0xff,eisa_adr+LDOORREG);
675 retries = INIT_RETRIES;
676 gdth_delay(20);
677 while (inb(eisa_adr+EDOORREG) != 0xff) {
678 if (--retries == 0) {
679 printk("GDT-EISA: Initialization error (DEINIT failed)\n");
680 return 0;
681 }
682 gdth_delay(1);
683 TRACE2(("wait for DEINIT: retries=%d\n",retries));
684 }
685 prot_ver = inb(eisa_adr+MAILBOXREG);
686 outb(0xff,eisa_adr+EDOORREG);
687 if (prot_ver != PROTOCOL_VERSION) {
688 printk("GDT-EISA: Illegal protocol version\n");
689 return 0;
690 }
691 ha->bmic = eisa_adr;
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500692 ha->brd_phys = (u32)eisa_adr >> 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 outl(0,eisa_adr+MAILBOXREG);
695 outl(0,eisa_adr+MAILBOXREG+4);
696 outl(0,eisa_adr+MAILBOXREG+8);
697 outl(0,eisa_adr+MAILBOXREG+12);
698
699 /* detect IRQ */
700 if ((id = inl(eisa_adr+ID0REG)) == GDT3_ID) {
701 ha->oem_id = OEM_ID_ICP;
702 ha->type = GDT_EISA;
703 ha->stype = id;
704 outl(1,eisa_adr+MAILBOXREG+8);
705 outb(0xfe,eisa_adr+LDOORREG);
706 retries = INIT_RETRIES;
707 gdth_delay(20);
708 while (inb(eisa_adr+EDOORREG) != 0xfe) {
709 if (--retries == 0) {
710 printk("GDT-EISA: Initialization error (get IRQ failed)\n");
711 return 0;
712 }
713 gdth_delay(1);
714 }
715 ha->irq = inb(eisa_adr+MAILBOXREG);
716 outb(0xff,eisa_adr+EDOORREG);
717 TRACE2(("GDT3000/3020: IRQ=%d\n",ha->irq));
718 /* check the result */
719 if (ha->irq == 0) {
720 TRACE2(("Unknown IRQ, use IRQ table from cmd line !\n"));
721 for (i = 0, irq_found = FALSE;
722 i < MAXHA && irq[i] != 0xff; ++i) {
723 if (irq[i]==10 || irq[i]==11 || irq[i]==12 || irq[i]==14) {
724 irq_found = TRUE;
725 break;
726 }
727 }
728 if (irq_found) {
729 ha->irq = irq[i];
730 irq[i] = 0;
731 printk("GDT-EISA: Can not detect controller IRQ,\n");
732 printk("Use IRQ setting from command line (IRQ = %d)\n",
733 ha->irq);
734 } else {
735 printk("GDT-EISA: Initialization error (unknown IRQ), Enable\n");
736 printk("the controller BIOS or use command line parameters\n");
737 return 0;
738 }
739 }
740 } else {
741 eisacf = inb(eisa_adr+EISAREG) & 7;
742 if (eisacf > 4) /* level triggered */
743 eisacf -= 4;
744 ha->irq = gdth_irq_tab[eisacf];
745 ha->oem_id = OEM_ID_ICP;
746 ha->type = GDT_EISA;
747 ha->stype = id;
748 }
749
750 ha->dma64_support = 0;
751 return 1;
752}
Christoph Hellwig706a5d42007-10-02 22:49:35 +0200753#endif /* CONFIG_EISA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Christoph Hellwigaed91cb2007-10-02 22:48:16 +0200755#ifdef CONFIG_ISA
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500756static int __init gdth_init_isa(u32 bios_adr,gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 register gdt2_dpram_str __iomem *dp2_ptr;
759 int i;
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500760 u8 irq_drq,prot_ver;
761 u32 retries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 TRACE(("gdth_init_isa() bios adr. %x\n",bios_adr));
764
765 ha->brd = ioremap(bios_adr, sizeof(gdt2_dpram_str));
766 if (ha->brd == NULL) {
767 printk("GDT-ISA: Initialization error (DPMEM remap error)\n");
768 return 0;
769 }
770 dp2_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +0200771 writeb(1, &dp2_ptr->io.memlock); /* switch off write protection */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 /* reset interface area */
773 memset_io(&dp2_ptr->u, 0, sizeof(dp2_ptr->u));
Jeff Garzika52667f2007-10-02 22:55:53 +0200774 if (readl(&dp2_ptr->u) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 printk("GDT-ISA: Initialization error (DPMEM write error)\n");
776 iounmap(ha->brd);
777 return 0;
778 }
779
780 /* disable board interrupts, read DRQ and IRQ */
Jeff Garzika52667f2007-10-02 22:55:53 +0200781 writeb(0xff, &dp2_ptr->io.irqdel);
782 writeb(0x00, &dp2_ptr->io.irqen);
783 writeb(0x00, &dp2_ptr->u.ic.S_Status);
784 writeb(0x00, &dp2_ptr->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Jeff Garzika52667f2007-10-02 22:55:53 +0200786 irq_drq = readb(&dp2_ptr->io.rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 for (i=0; i<3; ++i) {
788 if ((irq_drq & 1)==0)
789 break;
790 irq_drq >>= 1;
791 }
792 ha->drq = gdth_drq_tab[i];
793
Jeff Garzika52667f2007-10-02 22:55:53 +0200794 irq_drq = readb(&dp2_ptr->io.rq) >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 for (i=1; i<5; ++i) {
796 if ((irq_drq & 1)==0)
797 break;
798 irq_drq >>= 1;
799 }
800 ha->irq = gdth_irq_tab[i];
801
802 /* deinitialize services */
Jeff Garzika52667f2007-10-02 22:55:53 +0200803 writel(bios_adr, &dp2_ptr->u.ic.S_Info[0]);
804 writeb(0xff, &dp2_ptr->u.ic.S_Cmd_Indx);
805 writeb(0, &dp2_ptr->io.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 retries = INIT_RETRIES;
807 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +0200808 while (readb(&dp2_ptr->u.ic.S_Status) != 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (--retries == 0) {
810 printk("GDT-ISA: Initialization error (DEINIT failed)\n");
811 iounmap(ha->brd);
812 return 0;
813 }
814 gdth_delay(1);
815 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500816 prot_ver = (u8)readl(&dp2_ptr->u.ic.S_Info[0]);
Jeff Garzika52667f2007-10-02 22:55:53 +0200817 writeb(0, &dp2_ptr->u.ic.Status);
818 writeb(0xff, &dp2_ptr->io.irqdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (prot_ver != PROTOCOL_VERSION) {
820 printk("GDT-ISA: Illegal protocol version\n");
821 iounmap(ha->brd);
822 return 0;
823 }
824
825 ha->oem_id = OEM_ID_ICP;
826 ha->type = GDT_ISA;
827 ha->ic_all_size = sizeof(dp2_ptr->u);
828 ha->stype= GDT2_ID;
829 ha->brd_phys = bios_adr >> 4;
830
831 /* special request to controller BIOS */
Jeff Garzika52667f2007-10-02 22:55:53 +0200832 writel(0x00, &dp2_ptr->u.ic.S_Info[0]);
833 writel(0x00, &dp2_ptr->u.ic.S_Info[1]);
834 writel(0x01, &dp2_ptr->u.ic.S_Info[2]);
835 writel(0x00, &dp2_ptr->u.ic.S_Info[3]);
836 writeb(0xfe, &dp2_ptr->u.ic.S_Cmd_Indx);
837 writeb(0, &dp2_ptr->io.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 retries = INIT_RETRIES;
839 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +0200840 while (readb(&dp2_ptr->u.ic.S_Status) != 0xfe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (--retries == 0) {
842 printk("GDT-ISA: Initialization error\n");
843 iounmap(ha->brd);
844 return 0;
845 }
846 gdth_delay(1);
847 }
Jeff Garzika52667f2007-10-02 22:55:53 +0200848 writeb(0, &dp2_ptr->u.ic.Status);
849 writeb(0xff, &dp2_ptr->io.irqdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 ha->dma64_support = 0;
852 return 1;
853}
Christoph Hellwigaed91cb2007-10-02 22:48:16 +0200854#endif /* CONFIG_ISA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Christoph Hellwig8514ef22007-10-02 22:51:06 +0200856#ifdef CONFIG_PCI
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800857static int gdth_init_pci(struct pci_dev *pdev, gdth_pci_str *pcistr,
858 gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
860 register gdt6_dpram_str __iomem *dp6_ptr;
861 register gdt6c_dpram_str __iomem *dp6c_ptr;
862 register gdt6m_dpram_str __iomem *dp6m_ptr;
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500863 u32 retries;
864 u8 prot_ver;
865 u16 command;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 int i, found = FALSE;
867
868 TRACE(("gdth_init_pci()\n"));
869
Jeff Garzik4c9c8d72008-02-15 21:35:26 -0500870 if (pdev->vendor == PCI_VENDOR_ID_INTEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 ha->oem_id = OEM_ID_INTEL;
872 else
873 ha->oem_id = OEM_ID_ICP;
Jeff Garzik4c9c8d72008-02-15 21:35:26 -0500874 ha->brd_phys = (pdev->bus->number << 8) | (pdev->devfn & 0xf8);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500875 ha->stype = (u32)pdev->device;
Jeff Garzik4c9c8d72008-02-15 21:35:26 -0500876 ha->irq = pdev->irq;
877 ha->pdev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Jeff Garzik8e9a8a02007-07-17 05:25:17 -0400879 if (ha->pdev->device <= PCI_DEVICE_ID_VORTEX_GDT6000B) { /* GDT6000/B */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 TRACE2(("init_pci() dpmem %lx irq %d\n",pcistr->dpmem,ha->irq));
881 ha->brd = ioremap(pcistr->dpmem, sizeof(gdt6_dpram_str));
882 if (ha->brd == NULL) {
883 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
884 return 0;
885 }
886 /* check and reset interface area */
887 dp6_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +0200888 writel(DPMEM_MAGIC, &dp6_ptr->u);
889 if (readl(&dp6_ptr->u) != DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 printk("GDT-PCI: Cannot access DPMEM at 0x%lx (shadowed?)\n",
891 pcistr->dpmem);
892 found = FALSE;
893 for (i = 0xC8000; i < 0xE8000; i += 0x4000) {
894 iounmap(ha->brd);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500895 ha->brd = ioremap(i, sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (ha->brd == NULL) {
897 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
898 return 0;
899 }
Jeff Garzika52667f2007-10-02 22:55:53 +0200900 if (readw(ha->brd) != 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 TRACE2(("init_pci_old() address 0x%x busy\n", i));
902 continue;
903 }
904 iounmap(ha->brd);
Jeff Garzik4c9c8d72008-02-15 21:35:26 -0500905 pci_write_config_dword(pdev, PCI_BASE_ADDRESS_0, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 ha->brd = ioremap(i, sizeof(gdt6_dpram_str));
907 if (ha->brd == NULL) {
908 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
909 return 0;
910 }
911 dp6_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +0200912 writel(DPMEM_MAGIC, &dp6_ptr->u);
913 if (readl(&dp6_ptr->u) == DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 printk("GDT-PCI: Use free address at 0x%x\n", i);
915 found = TRUE;
916 break;
917 }
918 }
919 if (!found) {
920 printk("GDT-PCI: No free address found!\n");
921 iounmap(ha->brd);
922 return 0;
923 }
924 }
925 memset_io(&dp6_ptr->u, 0, sizeof(dp6_ptr->u));
Jeff Garzika52667f2007-10-02 22:55:53 +0200926 if (readl(&dp6_ptr->u) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 printk("GDT-PCI: Initialization error (DPMEM write error)\n");
928 iounmap(ha->brd);
929 return 0;
930 }
931
932 /* disable board interrupts, deinit services */
Jeff Garzika52667f2007-10-02 22:55:53 +0200933 writeb(0xff, &dp6_ptr->io.irqdel);
934 writeb(0x00, &dp6_ptr->io.irqen);
935 writeb(0x00, &dp6_ptr->u.ic.S_Status);
936 writeb(0x00, &dp6_ptr->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Jeff Garzika52667f2007-10-02 22:55:53 +0200938 writel(pcistr->dpmem, &dp6_ptr->u.ic.S_Info[0]);
939 writeb(0xff, &dp6_ptr->u.ic.S_Cmd_Indx);
940 writeb(0, &dp6_ptr->io.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 retries = INIT_RETRIES;
942 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +0200943 while (readb(&dp6_ptr->u.ic.S_Status) != 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 if (--retries == 0) {
945 printk("GDT-PCI: Initialization error (DEINIT failed)\n");
946 iounmap(ha->brd);
947 return 0;
948 }
949 gdth_delay(1);
950 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500951 prot_ver = (u8)readl(&dp6_ptr->u.ic.S_Info[0]);
Jeff Garzika52667f2007-10-02 22:55:53 +0200952 writeb(0, &dp6_ptr->u.ic.S_Status);
953 writeb(0xff, &dp6_ptr->io.irqdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (prot_ver != PROTOCOL_VERSION) {
955 printk("GDT-PCI: Illegal protocol version\n");
956 iounmap(ha->brd);
957 return 0;
958 }
959
960 ha->type = GDT_PCI;
961 ha->ic_all_size = sizeof(dp6_ptr->u);
962
963 /* special command to controller BIOS */
Jeff Garzika52667f2007-10-02 22:55:53 +0200964 writel(0x00, &dp6_ptr->u.ic.S_Info[0]);
965 writel(0x00, &dp6_ptr->u.ic.S_Info[1]);
966 writel(0x00, &dp6_ptr->u.ic.S_Info[2]);
967 writel(0x00, &dp6_ptr->u.ic.S_Info[3]);
968 writeb(0xfe, &dp6_ptr->u.ic.S_Cmd_Indx);
969 writeb(0, &dp6_ptr->io.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 retries = INIT_RETRIES;
971 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +0200972 while (readb(&dp6_ptr->u.ic.S_Status) != 0xfe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (--retries == 0) {
974 printk("GDT-PCI: Initialization error\n");
975 iounmap(ha->brd);
976 return 0;
977 }
978 gdth_delay(1);
979 }
Jeff Garzika52667f2007-10-02 22:55:53 +0200980 writeb(0, &dp6_ptr->u.ic.S_Status);
981 writeb(0xff, &dp6_ptr->io.irqdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 ha->dma64_support = 0;
984
Jeff Garzik8e9a8a02007-07-17 05:25:17 -0400985 } else if (ha->pdev->device <= PCI_DEVICE_ID_VORTEX_GDT6555) { /* GDT6110, ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 ha->plx = (gdt6c_plx_regs *)pcistr->io;
987 TRACE2(("init_pci_new() dpmem %lx irq %d\n",
988 pcistr->dpmem,ha->irq));
989 ha->brd = ioremap(pcistr->dpmem, sizeof(gdt6c_dpram_str));
990 if (ha->brd == NULL) {
991 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
992 iounmap(ha->brd);
993 return 0;
994 }
995 /* check and reset interface area */
996 dp6c_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +0200997 writel(DPMEM_MAGIC, &dp6c_ptr->u);
998 if (readl(&dp6c_ptr->u) != DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 printk("GDT-PCI: Cannot access DPMEM at 0x%lx (shadowed?)\n",
1000 pcistr->dpmem);
1001 found = FALSE;
1002 for (i = 0xC8000; i < 0xE8000; i += 0x4000) {
1003 iounmap(ha->brd);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001004 ha->brd = ioremap(i, sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (ha->brd == NULL) {
1006 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1007 return 0;
1008 }
Jeff Garzika52667f2007-10-02 22:55:53 +02001009 if (readw(ha->brd) != 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 TRACE2(("init_pci_plx() address 0x%x busy\n", i));
1011 continue;
1012 }
1013 iounmap(ha->brd);
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05001014 pci_write_config_dword(pdev, PCI_BASE_ADDRESS_2, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 ha->brd = ioremap(i, sizeof(gdt6c_dpram_str));
1016 if (ha->brd == NULL) {
1017 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1018 return 0;
1019 }
1020 dp6c_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001021 writel(DPMEM_MAGIC, &dp6c_ptr->u);
1022 if (readl(&dp6c_ptr->u) == DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 printk("GDT-PCI: Use free address at 0x%x\n", i);
1024 found = TRUE;
1025 break;
1026 }
1027 }
1028 if (!found) {
1029 printk("GDT-PCI: No free address found!\n");
1030 iounmap(ha->brd);
1031 return 0;
1032 }
1033 }
1034 memset_io(&dp6c_ptr->u, 0, sizeof(dp6c_ptr->u));
Jeff Garzika52667f2007-10-02 22:55:53 +02001035 if (readl(&dp6c_ptr->u) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 printk("GDT-PCI: Initialization error (DPMEM write error)\n");
1037 iounmap(ha->brd);
1038 return 0;
1039 }
1040
1041 /* disable board interrupts, deinit services */
1042 outb(0x00,PTR2USHORT(&ha->plx->control1));
1043 outb(0xff,PTR2USHORT(&ha->plx->edoor_reg));
1044
Jeff Garzika52667f2007-10-02 22:55:53 +02001045 writeb(0x00, &dp6c_ptr->u.ic.S_Status);
1046 writeb(0x00, &dp6c_ptr->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Jeff Garzika52667f2007-10-02 22:55:53 +02001048 writel(pcistr->dpmem, &dp6c_ptr->u.ic.S_Info[0]);
1049 writeb(0xff, &dp6c_ptr->u.ic.S_Cmd_Indx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 outb(1,PTR2USHORT(&ha->plx->ldoor_reg));
1052
1053 retries = INIT_RETRIES;
1054 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +02001055 while (readb(&dp6c_ptr->u.ic.S_Status) != 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (--retries == 0) {
1057 printk("GDT-PCI: Initialization error (DEINIT failed)\n");
1058 iounmap(ha->brd);
1059 return 0;
1060 }
1061 gdth_delay(1);
1062 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001063 prot_ver = (u8)readl(&dp6c_ptr->u.ic.S_Info[0]);
Jeff Garzika52667f2007-10-02 22:55:53 +02001064 writeb(0, &dp6c_ptr->u.ic.Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (prot_ver != PROTOCOL_VERSION) {
1066 printk("GDT-PCI: Illegal protocol version\n");
1067 iounmap(ha->brd);
1068 return 0;
1069 }
1070
1071 ha->type = GDT_PCINEW;
1072 ha->ic_all_size = sizeof(dp6c_ptr->u);
1073
1074 /* special command to controller BIOS */
Jeff Garzika52667f2007-10-02 22:55:53 +02001075 writel(0x00, &dp6c_ptr->u.ic.S_Info[0]);
1076 writel(0x00, &dp6c_ptr->u.ic.S_Info[1]);
1077 writel(0x00, &dp6c_ptr->u.ic.S_Info[2]);
1078 writel(0x00, &dp6c_ptr->u.ic.S_Info[3]);
1079 writeb(0xfe, &dp6c_ptr->u.ic.S_Cmd_Indx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 outb(1,PTR2USHORT(&ha->plx->ldoor_reg));
1082
1083 retries = INIT_RETRIES;
1084 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +02001085 while (readb(&dp6c_ptr->u.ic.S_Status) != 0xfe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (--retries == 0) {
1087 printk("GDT-PCI: Initialization error\n");
1088 iounmap(ha->brd);
1089 return 0;
1090 }
1091 gdth_delay(1);
1092 }
Jeff Garzika52667f2007-10-02 22:55:53 +02001093 writeb(0, &dp6c_ptr->u.ic.S_Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 ha->dma64_support = 0;
1096
1097 } else { /* MPR */
1098 TRACE2(("init_pci_mpr() dpmem %lx irq %d\n",pcistr->dpmem,ha->irq));
1099 ha->brd = ioremap(pcistr->dpmem, sizeof(gdt6m_dpram_str));
1100 if (ha->brd == NULL) {
1101 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1102 return 0;
1103 }
1104
1105 /* manipulate config. space to enable DPMEM, start RP controller */
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05001106 pci_read_config_word(pdev, PCI_COMMAND, &command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 command |= 6;
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05001108 pci_write_config_word(pdev, PCI_COMMAND, command);
Bjorn Helgaas8108de92012-11-06 15:04:44 -07001109 gdth_delay(1);
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 dp6m_ptr = ha->brd;
1112
1113 /* Ensure that it is safe to access the non HW portions of DPMEM.
1114 * Aditional check needed for Xscale based RAID controllers */
Jeff Garzika52667f2007-10-02 22:55:53 +02001115 while( ((int)readb(&dp6m_ptr->i960r.sema0_reg) ) & 3 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 gdth_delay(1);
1117
1118 /* check and reset interface area */
Jeff Garzika52667f2007-10-02 22:55:53 +02001119 writel(DPMEM_MAGIC, &dp6m_ptr->u);
1120 if (readl(&dp6m_ptr->u) != DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 printk("GDT-PCI: Cannot access DPMEM at 0x%lx (shadowed?)\n",
1122 pcistr->dpmem);
1123 found = FALSE;
1124 for (i = 0xC8000; i < 0xE8000; i += 0x4000) {
1125 iounmap(ha->brd);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001126 ha->brd = ioremap(i, sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (ha->brd == NULL) {
1128 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1129 return 0;
1130 }
Jeff Garzika52667f2007-10-02 22:55:53 +02001131 if (readw(ha->brd) != 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 TRACE2(("init_pci_mpr() address 0x%x busy\n", i));
1133 continue;
1134 }
1135 iounmap(ha->brd);
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05001136 pci_write_config_dword(pdev, PCI_BASE_ADDRESS_0, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 ha->brd = ioremap(i, sizeof(gdt6m_dpram_str));
1138 if (ha->brd == NULL) {
1139 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1140 return 0;
1141 }
1142 dp6m_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001143 writel(DPMEM_MAGIC, &dp6m_ptr->u);
1144 if (readl(&dp6m_ptr->u) == DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 printk("GDT-PCI: Use free address at 0x%x\n", i);
1146 found = TRUE;
1147 break;
1148 }
1149 }
1150 if (!found) {
1151 printk("GDT-PCI: No free address found!\n");
1152 iounmap(ha->brd);
1153 return 0;
1154 }
1155 }
1156 memset_io(&dp6m_ptr->u, 0, sizeof(dp6m_ptr->u));
1157
1158 /* disable board interrupts, deinit services */
Jeff Garzika52667f2007-10-02 22:55:53 +02001159 writeb(readb(&dp6m_ptr->i960r.edoor_en_reg) | 4,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 &dp6m_ptr->i960r.edoor_en_reg);
Jeff Garzika52667f2007-10-02 22:55:53 +02001161 writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
1162 writeb(0x00, &dp6m_ptr->u.ic.S_Status);
1163 writeb(0x00, &dp6m_ptr->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Jeff Garzika52667f2007-10-02 22:55:53 +02001165 writel(pcistr->dpmem, &dp6m_ptr->u.ic.S_Info[0]);
1166 writeb(0xff, &dp6m_ptr->u.ic.S_Cmd_Indx);
1167 writeb(1, &dp6m_ptr->i960r.ldoor_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 retries = INIT_RETRIES;
1169 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +02001170 while (readb(&dp6m_ptr->u.ic.S_Status) != 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if (--retries == 0) {
1172 printk("GDT-PCI: Initialization error (DEINIT failed)\n");
1173 iounmap(ha->brd);
1174 return 0;
1175 }
1176 gdth_delay(1);
1177 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001178 prot_ver = (u8)readl(&dp6m_ptr->u.ic.S_Info[0]);
Jeff Garzika52667f2007-10-02 22:55:53 +02001179 writeb(0, &dp6m_ptr->u.ic.S_Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (prot_ver != PROTOCOL_VERSION) {
1181 printk("GDT-PCI: Illegal protocol version\n");
1182 iounmap(ha->brd);
1183 return 0;
1184 }
1185
1186 ha->type = GDT_PCIMPR;
1187 ha->ic_all_size = sizeof(dp6m_ptr->u);
1188
1189 /* special command to controller BIOS */
Jeff Garzika52667f2007-10-02 22:55:53 +02001190 writel(0x00, &dp6m_ptr->u.ic.S_Info[0]);
1191 writel(0x00, &dp6m_ptr->u.ic.S_Info[1]);
1192 writel(0x00, &dp6m_ptr->u.ic.S_Info[2]);
1193 writel(0x00, &dp6m_ptr->u.ic.S_Info[3]);
1194 writeb(0xfe, &dp6m_ptr->u.ic.S_Cmd_Indx);
1195 writeb(1, &dp6m_ptr->i960r.ldoor_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 retries = INIT_RETRIES;
1197 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +02001198 while (readb(&dp6m_ptr->u.ic.S_Status) != 0xfe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if (--retries == 0) {
1200 printk("GDT-PCI: Initialization error\n");
1201 iounmap(ha->brd);
1202 return 0;
1203 }
1204 gdth_delay(1);
1205 }
Jeff Garzika52667f2007-10-02 22:55:53 +02001206 writeb(0, &dp6m_ptr->u.ic.S_Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 /* read FW version to detect 64-bit DMA support */
Jeff Garzika52667f2007-10-02 22:55:53 +02001209 writeb(0xfd, &dp6m_ptr->u.ic.S_Cmd_Indx);
1210 writeb(1, &dp6m_ptr->i960r.ldoor_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 retries = INIT_RETRIES;
1212 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +02001213 while (readb(&dp6m_ptr->u.ic.S_Status) != 0xfd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (--retries == 0) {
1215 printk("GDT-PCI: Initialization error (DEINIT failed)\n");
1216 iounmap(ha->brd);
1217 return 0;
1218 }
1219 gdth_delay(1);
1220 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001221 prot_ver = (u8)(readl(&dp6m_ptr->u.ic.S_Info[0]) >> 16);
Jeff Garzika52667f2007-10-02 22:55:53 +02001222 writeb(0, &dp6m_ptr->u.ic.S_Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (prot_ver < 0x2b) /* FW < x.43: no 64-bit DMA support */
1224 ha->dma64_support = 0;
1225 else
1226 ha->dma64_support = 1;
1227 }
1228
1229 return 1;
1230}
Christoph Hellwig8514ef22007-10-02 22:51:06 +02001231#endif /* CONFIG_PCI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233/* controller protocol functions */
1234
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08001235static void gdth_enable_int(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001237 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 gdt2_dpram_str __iomem *dp2_ptr;
1239 gdt6_dpram_str __iomem *dp6_ptr;
1240 gdt6m_dpram_str __iomem *dp6m_ptr;
1241
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001242 TRACE(("gdth_enable_int() hanum %d\n",ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 spin_lock_irqsave(&ha->smp_lock, flags);
1244
1245 if (ha->type == GDT_EISA) {
1246 outb(0xff, ha->bmic + EDOORREG);
1247 outb(0xff, ha->bmic + EDENABREG);
1248 outb(0x01, ha->bmic + EINTENABREG);
1249 } else if (ha->type == GDT_ISA) {
1250 dp2_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001251 writeb(1, &dp2_ptr->io.irqdel);
1252 writeb(0, &dp2_ptr->u.ic.Cmd_Index);
1253 writeb(1, &dp2_ptr->io.irqen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 } else if (ha->type == GDT_PCI) {
1255 dp6_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001256 writeb(1, &dp6_ptr->io.irqdel);
1257 writeb(0, &dp6_ptr->u.ic.Cmd_Index);
1258 writeb(1, &dp6_ptr->io.irqen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 } else if (ha->type == GDT_PCINEW) {
1260 outb(0xff, PTR2USHORT(&ha->plx->edoor_reg));
1261 outb(0x03, PTR2USHORT(&ha->plx->control1));
1262 } else if (ha->type == GDT_PCIMPR) {
1263 dp6m_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001264 writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
1265 writeb(readb(&dp6m_ptr->i960r.edoor_en_reg) & ~4,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 &dp6m_ptr->i960r.edoor_en_reg);
1267 }
1268 spin_unlock_irqrestore(&ha->smp_lock, flags);
1269}
1270
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001271/* return IStatus if interrupt was from this card else 0 */
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001272static u8 gdth_get_status(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001274 u8 IStatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Jeff Garzik230e8862007-12-13 16:14:12 -08001276 TRACE(("gdth_get_status() irq %d ctr_count %d\n", ha->irq, gdth_ctr_count));
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (ha->type == GDT_EISA)
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001279 IStatus = inb((u16)ha->bmic + EDOORREG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 else if (ha->type == GDT_ISA)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001281 IStatus =
Jeff Garzika52667f2007-10-02 22:55:53 +02001282 readb(&((gdt2_dpram_str __iomem *)ha->brd)->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 else if (ha->type == GDT_PCI)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001284 IStatus =
Jeff Garzika52667f2007-10-02 22:55:53 +02001285 readb(&((gdt6_dpram_str __iomem *)ha->brd)->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 else if (ha->type == GDT_PCINEW)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001287 IStatus = inb(PTR2USHORT(&ha->plx->edoor_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 else if (ha->type == GDT_PCIMPR)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001289 IStatus =
Jeff Garzika52667f2007-10-02 22:55:53 +02001290 readb(&((gdt6m_dpram_str __iomem *)ha->brd)->i960r.edoor_reg);
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001291
1292 return IStatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001294
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001295static int gdth_test_busy(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 register int gdtsema0 = 0;
1298
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001299 TRACE(("gdth_test_busy() hanum %d\n", ha->hanum));
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (ha->type == GDT_EISA)
1302 gdtsema0 = (int)inb(ha->bmic + SEMA0REG);
1303 else if (ha->type == GDT_ISA)
Jeff Garzika52667f2007-10-02 22:55:53 +02001304 gdtsema0 = (int)readb(&((gdt2_dpram_str __iomem *)ha->brd)->u.ic.Sema0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 else if (ha->type == GDT_PCI)
Jeff Garzika52667f2007-10-02 22:55:53 +02001306 gdtsema0 = (int)readb(&((gdt6_dpram_str __iomem *)ha->brd)->u.ic.Sema0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 else if (ha->type == GDT_PCINEW)
1308 gdtsema0 = (int)inb(PTR2USHORT(&ha->plx->sema0_reg));
1309 else if (ha->type == GDT_PCIMPR)
1310 gdtsema0 =
Jeff Garzika52667f2007-10-02 22:55:53 +02001311 (int)readb(&((gdt6m_dpram_str __iomem *)ha->brd)->i960r.sema0_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 return (gdtsema0 & 1);
1314}
1315
1316
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001317static int gdth_get_cmd_index(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 int i;
1320
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001321 TRACE(("gdth_get_cmd_index() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 for (i=0; i<GDTH_MAXCMDS; ++i) {
1324 if (ha->cmd_tab[i].cmnd == UNUSED_CMND) {
1325 ha->cmd_tab[i].cmnd = ha->pccb->RequestBuffer;
1326 ha->cmd_tab[i].service = ha->pccb->Service;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001327 ha->pccb->CommandIndex = (u32)i+2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return (i+2);
1329 }
1330 }
1331 return 0;
1332}
1333
1334
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001335static void gdth_set_sema0(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001337 TRACE(("gdth_set_sema0() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (ha->type == GDT_EISA) {
1340 outb(1, ha->bmic + SEMA0REG);
1341 } else if (ha->type == GDT_ISA) {
Jeff Garzika52667f2007-10-02 22:55:53 +02001342 writeb(1, &((gdt2_dpram_str __iomem *)ha->brd)->u.ic.Sema0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 } else if (ha->type == GDT_PCI) {
Jeff Garzika52667f2007-10-02 22:55:53 +02001344 writeb(1, &((gdt6_dpram_str __iomem *)ha->brd)->u.ic.Sema0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 } else if (ha->type == GDT_PCINEW) {
1346 outb(1, PTR2USHORT(&ha->plx->sema0_reg));
1347 } else if (ha->type == GDT_PCIMPR) {
Jeff Garzika52667f2007-10-02 22:55:53 +02001348 writeb(1, &((gdt6m_dpram_str __iomem *)ha->brd)->i960r.sema0_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350}
1351
1352
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001353static void gdth_copy_command(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 register gdth_cmd_str *cmd_ptr;
1356 register gdt6m_dpram_str __iomem *dp6m_ptr;
1357 register gdt6c_dpram_str __iomem *dp6c_ptr;
1358 gdt6_dpram_str __iomem *dp6_ptr;
1359 gdt2_dpram_str __iomem *dp2_ptr;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001360 u16 cp_count,dp_offset,cmd_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001362 TRACE(("gdth_copy_command() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 cp_count = ha->cmd_len;
1365 dp_offset= ha->cmd_offs_dpmem;
1366 cmd_no = ha->cmd_cnt;
1367 cmd_ptr = ha->pccb;
1368
1369 ++ha->cmd_cnt;
1370 if (ha->type == GDT_EISA)
1371 return; /* no DPMEM, no copy */
1372
1373 /* set cpcount dword aligned */
1374 if (cp_count & 3)
1375 cp_count += (4 - (cp_count & 3));
1376
1377 ha->cmd_offs_dpmem += cp_count;
1378
1379 /* set offset and service, copy command to DPMEM */
1380 if (ha->type == GDT_ISA) {
1381 dp2_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001382 writew(dp_offset + DPMEM_COMMAND_OFFSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 &dp2_ptr->u.ic.comm_queue[cmd_no].offset);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001384 writew((u16)cmd_ptr->Service,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 &dp2_ptr->u.ic.comm_queue[cmd_no].serv_id);
1386 memcpy_toio(&dp2_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
1387 } else if (ha->type == GDT_PCI) {
1388 dp6_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001389 writew(dp_offset + DPMEM_COMMAND_OFFSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 &dp6_ptr->u.ic.comm_queue[cmd_no].offset);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001391 writew((u16)cmd_ptr->Service,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 &dp6_ptr->u.ic.comm_queue[cmd_no].serv_id);
1393 memcpy_toio(&dp6_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
1394 } else if (ha->type == GDT_PCINEW) {
1395 dp6c_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001396 writew(dp_offset + DPMEM_COMMAND_OFFSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 &dp6c_ptr->u.ic.comm_queue[cmd_no].offset);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001398 writew((u16)cmd_ptr->Service,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 &dp6c_ptr->u.ic.comm_queue[cmd_no].serv_id);
1400 memcpy_toio(&dp6c_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
1401 } else if (ha->type == GDT_PCIMPR) {
1402 dp6m_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001403 writew(dp_offset + DPMEM_COMMAND_OFFSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 &dp6m_ptr->u.ic.comm_queue[cmd_no].offset);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001405 writew((u16)cmd_ptr->Service,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 &dp6m_ptr->u.ic.comm_queue[cmd_no].serv_id);
1407 memcpy_toio(&dp6m_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
1408 }
1409}
1410
1411
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001412static void gdth_release_event(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001414 TRACE(("gdth_release_event() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416#ifdef GDTH_STATISTICS
1417 {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001418 u32 i,j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 for (i=0,j=0; j<GDTH_MAXCMDS; ++j) {
1420 if (ha->cmd_tab[j].cmnd != UNUSED_CMND)
1421 ++i;
1422 }
1423 if (max_index < i) {
1424 max_index = i;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001425 TRACE3(("GDT: max_index = %d\n",(u16)i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
1427 }
1428#endif
1429
1430 if (ha->pccb->OpCode == GDT_INIT)
1431 ha->pccb->Service |= 0x80;
1432
1433 if (ha->type == GDT_EISA) {
1434 if (ha->pccb->OpCode == GDT_INIT) /* store DMA buffer */
1435 outl(ha->ccb_phys, ha->bmic + MAILBOXREG);
1436 outb(ha->pccb->Service, ha->bmic + LDOORREG);
1437 } else if (ha->type == GDT_ISA) {
Jeff Garzika52667f2007-10-02 22:55:53 +02001438 writeb(0, &((gdt2_dpram_str __iomem *)ha->brd)->io.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 } else if (ha->type == GDT_PCI) {
Jeff Garzika52667f2007-10-02 22:55:53 +02001440 writeb(0, &((gdt6_dpram_str __iomem *)ha->brd)->io.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 } else if (ha->type == GDT_PCINEW) {
1442 outb(1, PTR2USHORT(&ha->plx->ldoor_reg));
1443 } else if (ha->type == GDT_PCIMPR) {
Jeff Garzika52667f2007-10-02 22:55:53 +02001444 writeb(1, &((gdt6m_dpram_str __iomem *)ha->brd)->i960r.ldoor_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
1446}
1447
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001448static int gdth_wait(gdth_ha_str *ha, int index, u32 time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 int answer_found = FALSE;
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001451 int wait_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001453 TRACE(("gdth_wait() hanum %d index %d time %d\n", ha->hanum, index, time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 if (index == 0)
1456 return 1; /* no wait required */
1457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 do {
Jeff Garzik230e8862007-12-13 16:14:12 -08001459 __gdth_interrupt(ha, true, &wait_index);
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001460 if (wait_index == index) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 answer_found = TRUE;
1462 break;
1463 }
1464 gdth_delay(1);
1465 } while (--time);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001466
1467 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 gdth_delay(0);
1469
1470 return (answer_found);
1471}
1472
1473
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001474static int gdth_internal_cmd(gdth_ha_str *ha, u8 service, u16 opcode,
1475 u32 p1, u64 p2, u64 p3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 register gdth_cmd_str *cmd_ptr;
1478 int retries,index;
1479
1480 TRACE2(("gdth_internal_cmd() service %d opcode %d\n",service,opcode));
1481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 cmd_ptr = ha->pccb;
1483 memset((char*)cmd_ptr,0,sizeof(gdth_cmd_str));
1484
1485 /* make command */
1486 for (retries = INIT_RETRIES;;) {
1487 cmd_ptr->Service = service;
1488 cmd_ptr->RequestBuffer = INTERNAL_CMND;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001489 if (!(index=gdth_get_cmd_index(ha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 TRACE(("GDT: No free command index found\n"));
1491 return 0;
1492 }
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001493 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 cmd_ptr->OpCode = opcode;
1495 cmd_ptr->BoardNode = LOCALBOARD;
1496 if (service == CACHESERVICE) {
1497 if (opcode == GDT_IOCTL) {
1498 cmd_ptr->u.ioctl.subfunc = p1;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001499 cmd_ptr->u.ioctl.channel = (u32)p2;
1500 cmd_ptr->u.ioctl.param_size = (u16)p3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 cmd_ptr->u.ioctl.p_param = ha->scratch_phys;
1502 } else {
1503 if (ha->cache_feat & GDT_64BIT) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001504 cmd_ptr->u.cache64.DeviceNo = (u16)p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 cmd_ptr->u.cache64.BlockNo = p2;
1506 } else {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001507 cmd_ptr->u.cache.DeviceNo = (u16)p1;
1508 cmd_ptr->u.cache.BlockNo = (u32)p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 }
1510 }
1511 } else if (service == SCSIRAWSERVICE) {
1512 if (ha->raw_feat & GDT_64BIT) {
1513 cmd_ptr->u.raw64.direction = p1;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001514 cmd_ptr->u.raw64.bus = (u8)p2;
1515 cmd_ptr->u.raw64.target = (u8)p3;
1516 cmd_ptr->u.raw64.lun = (u8)(p3 >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 } else {
1518 cmd_ptr->u.raw.direction = p1;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001519 cmd_ptr->u.raw.bus = (u8)p2;
1520 cmd_ptr->u.raw.target = (u8)p3;
1521 cmd_ptr->u.raw.lun = (u8)(p3 >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
1523 } else if (service == SCREENSERVICE) {
1524 if (opcode == GDT_REALTIME) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001525 *(u32 *)&cmd_ptr->u.screen.su.data[0] = p1;
1526 *(u32 *)&cmd_ptr->u.screen.su.data[4] = (u32)p2;
1527 *(u32 *)&cmd_ptr->u.screen.su.data[8] = (u32)p3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
1529 }
1530 ha->cmd_len = sizeof(gdth_cmd_str);
1531 ha->cmd_offs_dpmem = 0;
1532 ha->cmd_cnt = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001533 gdth_copy_command(ha);
1534 gdth_release_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 gdth_delay(20);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001536 if (!gdth_wait(ha, index, INIT_TIMEOUT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 printk("GDT: Initialization error (timeout service %d)\n",service);
1538 return 0;
1539 }
1540 if (ha->status != S_BSY || --retries == 0)
1541 break;
1542 gdth_delay(1);
1543 }
1544
1545 return (ha->status != S_OK ? 0:1);
1546}
1547
1548
1549/* search for devices */
1550
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08001551static int gdth_search_drives(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001553 u16 cdev_cnt, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 int ok;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001555 u32 bus_no, drv_cnt, drv_no, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 gdth_getch_str *chn;
1557 gdth_drlist_str *drl;
1558 gdth_iochan_str *ioc;
1559 gdth_raw_iochan_str *iocr;
1560 gdth_arcdl_str *alst;
1561 gdth_alist_str *alst2;
1562 gdth_oem_str_ioctl *oemstr;
1563#ifdef INT_COAL
1564 gdth_perf_modes *pmod;
1565#endif
1566
1567#ifdef GDTH_RTC
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001568 u8 rtc[12];
1569 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570#endif
1571
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001572 TRACE(("gdth_search_drives() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 ok = 0;
1574
1575 /* initialize controller services, at first: screen service */
1576 ha->screen_feat = 0;
1577 if (!force_dma32) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001578 ok = gdth_internal_cmd(ha, SCREENSERVICE, GDT_X_INIT_SCR, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 if (ok)
1580 ha->screen_feat = GDT_64BIT;
1581 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001582 if (force_dma32 || (!ok && ha->status == (u16)S_NOFUNC))
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001583 ok = gdth_internal_cmd(ha, SCREENSERVICE, GDT_INIT, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 if (!ok) {
1585 printk("GDT-HA %d: Initialization error screen service (code %d)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001586 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 return 0;
1588 }
1589 TRACE2(("gdth_search_drives(): SCREENSERVICE initialized\n"));
1590
1591#ifdef GDTH_RTC
1592 /* read realtime clock info, send to controller */
1593 /* 1. wait for the falling edge of update flag */
1594 spin_lock_irqsave(&rtc_lock, flags);
1595 for (j = 0; j < 1000000; ++j)
1596 if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
1597 break;
1598 for (j = 0; j < 1000000; ++j)
1599 if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
1600 break;
1601 /* 2. read info */
1602 do {
1603 for (j = 0; j < 12; ++j)
1604 rtc[j] = CMOS_READ(j);
1605 } while (rtc[0] != CMOS_READ(0));
Robert P. J. Day7a960b72007-05-23 14:41:40 -07001606 spin_unlock_irqrestore(&rtc_lock, flags);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001607 TRACE2(("gdth_search_drives(): RTC: %x/%x/%x\n",*(u32 *)&rtc[0],
1608 *(u32 *)&rtc[4], *(u32 *)&rtc[8]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 /* 3. send to controller firmware */
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001610 gdth_internal_cmd(ha, SCREENSERVICE, GDT_REALTIME, *(u32 *)&rtc[0],
1611 *(u32 *)&rtc[4], *(u32 *)&rtc[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612#endif
1613
1614 /* unfreeze all IOs */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001615 gdth_internal_cmd(ha, CACHESERVICE, GDT_UNFREEZE_IO, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 /* initialize cache service */
1618 ha->cache_feat = 0;
1619 if (!force_dma32) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001620 ok = gdth_internal_cmd(ha, CACHESERVICE, GDT_X_INIT_HOST, LINUX_OS,
1621 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 if (ok)
1623 ha->cache_feat = GDT_64BIT;
1624 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001625 if (force_dma32 || (!ok && ha->status == (u16)S_NOFUNC))
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001626 ok = gdth_internal_cmd(ha, CACHESERVICE, GDT_INIT, LINUX_OS, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (!ok) {
1628 printk("GDT-HA %d: Initialization error cache service (code %d)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001629 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 return 0;
1631 }
1632 TRACE2(("gdth_search_drives(): CACHESERVICE initialized\n"));
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001633 cdev_cnt = (u16)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 ha->fw_vers = ha->service;
1635
1636#ifdef INT_COAL
1637 if (ha->type == GDT_PCIMPR) {
1638 /* set perf. modes */
1639 pmod = (gdth_perf_modes *)ha->pscratch;
1640 pmod->version = 1;
1641 pmod->st_mode = 1; /* enable one status buffer */
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001642 *((u64 *)&pmod->st_buff_addr1) = ha->coal_stat_phys;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 pmod->st_buff_indx1 = COALINDEX;
1644 pmod->st_buff_addr2 = 0;
1645 pmod->st_buff_u_addr2 = 0;
1646 pmod->st_buff_indx2 = 0;
1647 pmod->st_buff_size = sizeof(gdth_coal_status) * MAXOFFSETS;
1648 pmod->cmd_mode = 0; // disable all cmd buffers
1649 pmod->cmd_buff_addr1 = 0;
1650 pmod->cmd_buff_u_addr1 = 0;
1651 pmod->cmd_buff_indx1 = 0;
1652 pmod->cmd_buff_addr2 = 0;
1653 pmod->cmd_buff_u_addr2 = 0;
1654 pmod->cmd_buff_indx2 = 0;
1655 pmod->cmd_buff_size = 0;
1656 pmod->reserved1 = 0;
1657 pmod->reserved2 = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001658 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, SET_PERF_MODES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 INVALID_CHANNEL,sizeof(gdth_perf_modes))) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001660 printk("GDT-HA %d: Interrupt coalescing activated\n", ha->hanum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
1662 }
1663#endif
1664
1665 /* detect number of buses - try new IOCTL */
1666 iocr = (gdth_raw_iochan_str *)ha->pscratch;
1667 iocr->hdr.version = 0xffffffff;
1668 iocr->hdr.list_entries = MAXBUS;
1669 iocr->hdr.first_chan = 0;
1670 iocr->hdr.last_chan = MAXBUS-1;
1671 iocr->hdr.list_offset = GDTOFFSOF(gdth_raw_iochan_str, list[0]);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001672 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, IOCHAN_RAW_DESC,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 INVALID_CHANNEL,sizeof(gdth_raw_iochan_str))) {
1674 TRACE2(("IOCHAN_RAW_DESC supported!\n"));
1675 ha->bus_cnt = iocr->hdr.chan_count;
1676 for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
1677 if (iocr->list[bus_no].proc_id < MAXID)
1678 ha->bus_id[bus_no] = iocr->list[bus_no].proc_id;
1679 else
1680 ha->bus_id[bus_no] = 0xff;
1681 }
1682 } else {
1683 /* old method */
1684 chn = (gdth_getch_str *)ha->pscratch;
1685 for (bus_no = 0; bus_no < MAXBUS; ++bus_no) {
1686 chn->channel_no = bus_no;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001687 if (!gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 SCSI_CHAN_CNT | L_CTRL_PATTERN,
1689 IO_CHANNEL | INVALID_CHANNEL,
1690 sizeof(gdth_getch_str))) {
1691 if (bus_no == 0) {
1692 printk("GDT-HA %d: Error detecting channel count (0x%x)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001693 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 return 0;
1695 }
1696 break;
1697 }
1698 if (chn->siop_id < MAXID)
1699 ha->bus_id[bus_no] = chn->siop_id;
1700 else
1701 ha->bus_id[bus_no] = 0xff;
1702 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001703 ha->bus_cnt = (u8)bus_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 }
1705 TRACE2(("gdth_search_drives() %d channels\n",ha->bus_cnt));
1706
1707 /* read cache configuration */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001708 if (!gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, CACHE_INFO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 INVALID_CHANNEL,sizeof(gdth_cinfo_str))) {
1710 printk("GDT-HA %d: Initialization error cache service (code %d)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001711 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 return 0;
1713 }
1714 ha->cpar = ((gdth_cinfo_str *)ha->pscratch)->cpar;
1715 TRACE2(("gdth_search_drives() cinfo: vs %x sta %d str %d dw %d b %d\n",
1716 ha->cpar.version,ha->cpar.state,ha->cpar.strategy,
1717 ha->cpar.write_back,ha->cpar.block_size));
1718
1719 /* read board info and features */
1720 ha->more_proc = FALSE;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001721 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, BOARD_INFO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 INVALID_CHANNEL,sizeof(gdth_binfo_str))) {
1723 memcpy(&ha->binfo, (gdth_binfo_str *)ha->pscratch,
1724 sizeof(gdth_binfo_str));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001725 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, BOARD_FEATURES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 INVALID_CHANNEL,sizeof(gdth_bfeat_str))) {
1727 TRACE2(("BOARD_INFO/BOARD_FEATURES supported\n"));
1728 ha->bfeat = *(gdth_bfeat_str *)ha->pscratch;
1729 ha->more_proc = TRUE;
1730 }
1731 } else {
1732 TRACE2(("BOARD_INFO requires firmware >= 1.10/2.08\n"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001733 strcpy(ha->binfo.type_string, gdth_ctr_name(ha));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 }
1735 TRACE2(("Controller name: %s\n",ha->binfo.type_string));
1736
1737 /* read more informations */
1738 if (ha->more_proc) {
1739 /* physical drives, channel addresses */
1740 ioc = (gdth_iochan_str *)ha->pscratch;
1741 ioc->hdr.version = 0xffffffff;
1742 ioc->hdr.list_entries = MAXBUS;
1743 ioc->hdr.first_chan = 0;
1744 ioc->hdr.last_chan = MAXBUS-1;
1745 ioc->hdr.list_offset = GDTOFFSOF(gdth_iochan_str, list[0]);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001746 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, IOCHAN_DESC,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 INVALID_CHANNEL,sizeof(gdth_iochan_str))) {
1748 for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
1749 ha->raw[bus_no].address = ioc->list[bus_no].address;
1750 ha->raw[bus_no].local_no = ioc->list[bus_no].local_no;
1751 }
1752 } else {
1753 for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
1754 ha->raw[bus_no].address = IO_CHANNEL;
1755 ha->raw[bus_no].local_no = bus_no;
1756 }
1757 }
1758 for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
1759 chn = (gdth_getch_str *)ha->pscratch;
1760 chn->channel_no = ha->raw[bus_no].local_no;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001761 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 SCSI_CHAN_CNT | L_CTRL_PATTERN,
1763 ha->raw[bus_no].address | INVALID_CHANNEL,
1764 sizeof(gdth_getch_str))) {
1765 ha->raw[bus_no].pdev_cnt = chn->drive_cnt;
1766 TRACE2(("Channel %d: %d phys. drives\n",
1767 bus_no,chn->drive_cnt));
1768 }
1769 if (ha->raw[bus_no].pdev_cnt > 0) {
1770 drl = (gdth_drlist_str *)ha->pscratch;
1771 drl->sc_no = ha->raw[bus_no].local_no;
1772 drl->sc_cnt = ha->raw[bus_no].pdev_cnt;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001773 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 SCSI_DR_LIST | L_CTRL_PATTERN,
1775 ha->raw[bus_no].address | INVALID_CHANNEL,
1776 sizeof(gdth_drlist_str))) {
1777 for (j = 0; j < ha->raw[bus_no].pdev_cnt; ++j)
1778 ha->raw[bus_no].id_list[j] = drl->sc_list[j];
1779 } else {
1780 ha->raw[bus_no].pdev_cnt = 0;
1781 }
1782 }
1783 }
1784
1785 /* logical drives */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001786 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, CACHE_DRV_CNT,
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001787 INVALID_CHANNEL,sizeof(u32))) {
1788 drv_cnt = *(u32 *)ha->pscratch;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001789 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, CACHE_DRV_LIST,
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001790 INVALID_CHANNEL,drv_cnt * sizeof(u32))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 for (j = 0; j < drv_cnt; ++j) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001792 drv_no = ((u32 *)ha->pscratch)[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (drv_no < MAX_LDRIVES) {
1794 ha->hdr[drv_no].is_logdrv = TRUE;
1795 TRACE2(("Drive %d is log. drive\n",drv_no));
1796 }
1797 }
1798 }
1799 alst = (gdth_arcdl_str *)ha->pscratch;
1800 alst->entries_avail = MAX_LDRIVES;
1801 alst->first_entry = 0;
1802 alst->list_offset = GDTOFFSOF(gdth_arcdl_str, list[0]);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001803 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 ARRAY_DRV_LIST2 | LA_CTRL_PATTERN,
1805 INVALID_CHANNEL, sizeof(gdth_arcdl_str) +
1806 (alst->entries_avail-1) * sizeof(gdth_alist_str))) {
1807 for (j = 0; j < alst->entries_init; ++j) {
1808 ha->hdr[j].is_arraydrv = alst->list[j].is_arrayd;
1809 ha->hdr[j].is_master = alst->list[j].is_master;
1810 ha->hdr[j].is_parity = alst->list[j].is_parity;
1811 ha->hdr[j].is_hotfix = alst->list[j].is_hotfix;
1812 ha->hdr[j].master_no = alst->list[j].cd_handle;
1813 }
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001814 } else if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 ARRAY_DRV_LIST | LA_CTRL_PATTERN,
1816 0, 35 * sizeof(gdth_alist_str))) {
1817 for (j = 0; j < 35; ++j) {
1818 alst2 = &((gdth_alist_str *)ha->pscratch)[j];
1819 ha->hdr[j].is_arraydrv = alst2->is_arrayd;
1820 ha->hdr[j].is_master = alst2->is_master;
1821 ha->hdr[j].is_parity = alst2->is_parity;
1822 ha->hdr[j].is_hotfix = alst2->is_hotfix;
1823 ha->hdr[j].master_no = alst2->cd_handle;
1824 }
1825 }
1826 }
1827 }
1828
1829 /* initialize raw service */
1830 ha->raw_feat = 0;
1831 if (!force_dma32) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001832 ok = gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_X_INIT_RAW, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 if (ok)
1834 ha->raw_feat = GDT_64BIT;
1835 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001836 if (force_dma32 || (!ok && ha->status == (u16)S_NOFUNC))
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001837 ok = gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_INIT, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 if (!ok) {
1839 printk("GDT-HA %d: Initialization error raw service (code %d)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001840 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 return 0;
1842 }
1843 TRACE2(("gdth_search_drives(): RAWSERVICE initialized\n"));
1844
1845 /* set/get features raw service (scatter/gather) */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001846 if (gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_SET_FEAT, SCATTER_GATHER,
1847 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 TRACE2(("gdth_search_drives(): set features RAWSERVICE OK\n"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001849 if (gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_GET_FEAT, 0, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 TRACE2(("gdth_search_dr(): get feat RAWSERVICE %d\n",
1851 ha->info));
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001852 ha->raw_feat |= (u16)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 }
1854 }
1855
1856 /* set/get features cache service (equal to raw service) */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001857 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_SET_FEAT, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 SCATTER_GATHER,0)) {
1859 TRACE2(("gdth_search_drives(): set features CACHESERVICE OK\n"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001860 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_GET_FEAT, 0, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 TRACE2(("gdth_search_dr(): get feat CACHESERV. %d\n",
1862 ha->info));
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001863 ha->cache_feat |= (u16)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 }
1865 }
1866
1867 /* reserve drives for raw service */
1868 if (reserve_mode != 0) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001869 gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_RESERVE_ALL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 reserve_mode == 1 ? 1 : 3, 0, 0);
1871 TRACE2(("gdth_search_drives(): RESERVE_ALL code %d\n",
1872 ha->status));
1873 }
1874 for (i = 0; i < MAX_RES_ARGS; i += 4) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001875 if (reserve_list[i] == ha->hanum && reserve_list[i+1] < ha->bus_cnt &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 reserve_list[i+2] < ha->tid_cnt && reserve_list[i+3] < MAXLUN) {
1877 TRACE2(("gdth_search_drives(): reserve ha %d bus %d id %d lun %d\n",
1878 reserve_list[i], reserve_list[i+1],
1879 reserve_list[i+2], reserve_list[i+3]));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001880 if (!gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_RESERVE, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 reserve_list[i+1], reserve_list[i+2] |
1882 (reserve_list[i+3] << 8))) {
1883 printk("GDT-HA %d: Error raw service (RESERVE, code %d)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001884 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 }
1886 }
1887 }
1888
1889 /* Determine OEM string using IOCTL */
1890 oemstr = (gdth_oem_str_ioctl *)ha->pscratch;
1891 oemstr->params.ctl_version = 0x01;
1892 oemstr->params.buffer_size = sizeof(oemstr->text);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001893 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 CACHE_READ_OEM_STRING_RECORD,INVALID_CHANNEL,
1895 sizeof(gdth_oem_str_ioctl))) {
1896 TRACE2(("gdth_search_drives(): CACHE_READ_OEM_STRING_RECORD OK\n"));
1897 printk("GDT-HA %d: Vendor: %s Name: %s\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001898 ha->hanum, oemstr->text.oem_company_name, ha->binfo.type_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 /* Save the Host Drive inquiry data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 strlcpy(ha->oem_name,oemstr->text.scsi_host_drive_inquiry_vendor_id,
1901 sizeof(ha->oem_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 } else {
1903 /* Old method, based on PCI ID */
1904 TRACE2(("gdth_search_drives(): CACHE_READ_OEM_STRING_RECORD failed\n"));
1905 printk("GDT-HA %d: Name: %s\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001906 ha->hanum, ha->binfo.type_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 if (ha->oem_id == OEM_ID_INTEL)
1908 strlcpy(ha->oem_name,"Intel ", sizeof(ha->oem_name));
1909 else
1910 strlcpy(ha->oem_name,"ICP ", sizeof(ha->oem_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
1912
1913 /* scanning for host drives */
1914 for (i = 0; i < cdev_cnt; ++i)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001915 gdth_analyse_hdrive(ha, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 TRACE(("gdth_search_drives() OK\n"));
1918 return 1;
1919}
1920
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001921static int gdth_analyse_hdrive(gdth_ha_str *ha, u16 hdrive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001923 u32 drv_cyls;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 int drv_hds, drv_secs;
1925
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001926 TRACE(("gdth_analyse_hdrive() hanum %d drive %d\n", ha->hanum, hdrive));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 if (hdrive >= MAX_HDRIVES)
1928 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001930 if (!gdth_internal_cmd(ha, CACHESERVICE, GDT_INFO, hdrive, 0, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return 0;
1932 ha->hdr[hdrive].present = TRUE;
1933 ha->hdr[hdrive].size = ha->info;
1934
1935 /* evaluate mapping (sectors per head, heads per cylinder) */
1936 ha->hdr[hdrive].size &= ~SECS32;
1937 if (ha->info2 == 0) {
1938 gdth_eval_mapping(ha->hdr[hdrive].size,&drv_cyls,&drv_hds,&drv_secs);
1939 } else {
1940 drv_hds = ha->info2 & 0xff;
1941 drv_secs = (ha->info2 >> 8) & 0xff;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001942 drv_cyls = (u32)ha->hdr[hdrive].size / drv_hds / drv_secs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001944 ha->hdr[hdrive].heads = (u8)drv_hds;
1945 ha->hdr[hdrive].secs = (u8)drv_secs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 /* round size */
1947 ha->hdr[hdrive].size = drv_cyls * drv_hds * drv_secs;
1948
1949 if (ha->cache_feat & GDT_64BIT) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001950 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_X_INFO, hdrive, 0, 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 && ha->info2 != 0) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001952 ha->hdr[hdrive].size = ((u64)ha->info2 << 32) | ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
1954 }
1955 TRACE2(("gdth_search_dr() cdr. %d size %d hds %d scs %d\n",
1956 hdrive,ha->hdr[hdrive].size,drv_hds,drv_secs));
1957
1958 /* get informations about device */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001959 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_DEVTYPE, hdrive, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 TRACE2(("gdth_search_dr() cache drive %d devtype %d\n",
1961 hdrive,ha->info));
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001962 ha->hdr[hdrive].devtype = (u16)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 }
1964
1965 /* cluster info */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001966 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_CLUST_INFO, hdrive, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 TRACE2(("gdth_search_dr() cache drive %d cluster info %d\n",
1968 hdrive,ha->info));
1969 if (!shared_access)
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001970 ha->hdr[hdrive].cluster_type = (u8)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
1972
1973 /* R/W attributes */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001974 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_RW_ATTRIBS, hdrive, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 TRACE2(("gdth_search_dr() cache drive %d r/w attrib. %d\n",
1976 hdrive,ha->info));
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001977 ha->hdr[hdrive].rw_attribs = (u8)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 }
1979
1980 return 1;
1981}
1982
1983
1984/* command queueing/sending functions */
1985
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02001986static void gdth_putq(gdth_ha_str *ha, struct scsi_cmnd *scp, u8 priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02001988 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02001989 register struct scsi_cmnd *pscp;
1990 register struct scsi_cmnd *nscp;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001991 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993 TRACE(("gdth_putq() priority %d\n",priority));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 spin_lock_irqsave(&ha->smp_lock, flags);
1995
Jens Axboe242f9dc2008-09-14 05:55:09 -07001996 if (!cmndinfo->internal_command)
Boaz Harroshf842b642007-10-02 23:16:01 +02001997 cmndinfo->priority = priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
1999 if (ha->req_first==NULL) {
2000 ha->req_first = scp; /* queue was empty */
2001 scp->SCp.ptr = NULL;
2002 } else { /* queue not empty */
2003 pscp = ha->req_first;
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02002004 nscp = (struct scsi_cmnd *)pscp->SCp.ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 /* priority: 0-highest,..,0xff-lowest */
Boaz Harroshf842b642007-10-02 23:16:01 +02002006 while (nscp && gdth_cmnd_priv(nscp)->priority <= priority) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 pscp = nscp;
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02002008 nscp = (struct scsi_cmnd *)pscp->SCp.ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
2010 pscp->SCp.ptr = (char *)scp;
2011 scp->SCp.ptr = (char *)nscp;
2012 }
2013 spin_unlock_irqrestore(&ha->smp_lock, flags);
2014
2015#ifdef GDTH_STATISTICS
2016 flags = 0;
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02002017 for (nscp=ha->req_first; nscp; nscp=(struct scsi_cmnd*)nscp->SCp.ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 ++flags;
2019 if (max_rq < flags) {
2020 max_rq = flags;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002021 TRACE3(("GDT: max_rq = %d\n",(u16)max_rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
2023#endif
2024}
2025
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002026static void gdth_next(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02002028 register struct scsi_cmnd *pscp;
2029 register struct scsi_cmnd *nscp;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002030 u8 b, t, l, firsttime;
2031 u8 this_cmd, next_cmd;
2032 unsigned long flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 int cmd_index;
2034
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002035 TRACE(("gdth_next() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (!gdth_polling)
2037 spin_lock_irqsave(&ha->smp_lock, flags);
2038
2039 ha->cmd_cnt = ha->cmd_offs_dpmem = 0;
2040 this_cmd = firsttime = TRUE;
2041 next_cmd = gdth_polling ? FALSE:TRUE;
2042 cmd_index = 0;
2043
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02002044 for (nscp = pscp = ha->req_first; nscp; nscp = (struct scsi_cmnd *)nscp->SCp.ptr) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002045 struct gdth_cmndinfo *nscp_cmndinfo = gdth_cmnd_priv(nscp);
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02002046 if (nscp != pscp && nscp != (struct scsi_cmnd *)pscp->SCp.ptr)
2047 pscp = (struct scsi_cmnd *)pscp->SCp.ptr;
Boaz Harroshf842b642007-10-02 23:16:01 +02002048 if (!nscp_cmndinfo->internal_command) {
Christoph Hellwig52759e62007-10-02 22:59:53 +02002049 b = nscp->device->channel;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07002050 t = nscp->device->id;
2051 l = nscp->device->lun;
Boaz Harroshf842b642007-10-02 23:16:01 +02002052 if (nscp_cmndinfo->priority >= DEFAULT_PRI) {
Leubner, Achimcbd5f692006-06-09 11:34:29 -07002053 if ((b != ha->virt_bus && ha->raw[BUS_L2P(ha,b)].lock) ||
2054 (b == ha->virt_bus && t < MAX_HDRIVES && ha->hdr[t].lock))
2055 continue;
2056 }
2057 } else
2058 b = t = l = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060 if (firsttime) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002061 if (gdth_test_busy(ha)) { /* controller busy ? */
2062 TRACE(("gdth_next() controller %d busy !\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 if (!gdth_polling) {
2064 spin_unlock_irqrestore(&ha->smp_lock, flags);
2065 return;
2066 }
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002067 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 gdth_delay(1);
2069 }
2070 firsttime = FALSE;
2071 }
2072
Boaz Harroshf842b642007-10-02 23:16:01 +02002073 if (!nscp_cmndinfo->internal_command) {
2074 if (nscp_cmndinfo->phase == -1) {
2075 nscp_cmndinfo->phase = CACHESERVICE; /* default: cache svc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 if (nscp->cmnd[0] == TEST_UNIT_READY) {
2077 TRACE2(("TEST_UNIT_READY Bus %d Id %d LUN %d\n",
2078 b, t, l));
2079 /* TEST_UNIT_READY -> set scan mode */
2080 if ((ha->scan_mode & 0x0f) == 0) {
2081 if (b == 0 && t == 0 && l == 0) {
2082 ha->scan_mode |= 1;
2083 TRACE2(("Scan mode: 0x%x\n", ha->scan_mode));
2084 }
2085 } else if ((ha->scan_mode & 0x0f) == 1) {
2086 if (b == 0 && ((t == 0 && l == 1) ||
2087 (t == 1 && l == 0))) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002088 nscp_cmndinfo->OpCode = GDT_SCAN_START;
2089 nscp_cmndinfo->phase = ((ha->scan_mode & 0x10 ? 1:0) << 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 | SCSIRAWSERVICE;
2091 ha->scan_mode = 0x12;
2092 TRACE2(("Scan mode: 0x%x (SCAN_START)\n",
2093 ha->scan_mode));
2094 } else {
2095 ha->scan_mode &= 0x10;
2096 TRACE2(("Scan mode: 0x%x\n", ha->scan_mode));
2097 }
2098 } else if (ha->scan_mode == 0x12) {
2099 if (b == ha->bus_cnt && t == ha->tid_cnt-1) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002100 nscp_cmndinfo->phase = SCSIRAWSERVICE;
2101 nscp_cmndinfo->OpCode = GDT_SCAN_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 ha->scan_mode &= 0x10;
2103 TRACE2(("Scan mode: 0x%x (SCAN_END)\n",
2104 ha->scan_mode));
2105 }
2106 }
2107 }
2108 if (b == ha->virt_bus && nscp->cmnd[0] != INQUIRY &&
2109 nscp->cmnd[0] != READ_CAPACITY && nscp->cmnd[0] != MODE_SENSE &&
2110 (ha->hdr[t].cluster_type & CLUSTER_DRIVE)) {
2111 /* always GDT_CLUST_INFO! */
Boaz Harroshf842b642007-10-02 23:16:01 +02002112 nscp_cmndinfo->OpCode = GDT_CLUST_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
2114 }
2115 }
2116
Boaz Harroshf842b642007-10-02 23:16:01 +02002117 if (nscp_cmndinfo->OpCode != -1) {
2118 if ((nscp_cmndinfo->phase & 0xff) == CACHESERVICE) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002119 if (!(cmd_index=gdth_fill_cache_cmd(ha, nscp, t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 this_cmd = FALSE;
2121 next_cmd = FALSE;
Boaz Harroshf842b642007-10-02 23:16:01 +02002122 } else if ((nscp_cmndinfo->phase & 0xff) == SCSIRAWSERVICE) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002123 if (!(cmd_index=gdth_fill_raw_cmd(ha, nscp, BUS_L2P(ha, b))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 this_cmd = FALSE;
2125 next_cmd = FALSE;
2126 } else {
2127 memset((char*)nscp->sense_buffer,0,16);
2128 nscp->sense_buffer[0] = 0x70;
2129 nscp->sense_buffer[2] = NOT_READY;
2130 nscp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
Boaz Harroshf842b642007-10-02 23:16:01 +02002131 if (!nscp_cmndinfo->wait_for_completion)
2132 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002134 gdth_scsi_done(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 }
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02002136 } else if (gdth_cmnd_priv(nscp)->internal_command) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002137 if (!(cmd_index=gdth_special_cmd(ha, nscp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 this_cmd = FALSE;
2139 next_cmd = FALSE;
2140 } else if (b != ha->virt_bus) {
2141 if (ha->raw[BUS_L2P(ha,b)].io_cnt[t] >= GDTH_MAX_RAW ||
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002142 !(cmd_index=gdth_fill_raw_cmd(ha, nscp, BUS_L2P(ha, b))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 this_cmd = FALSE;
2144 else
2145 ha->raw[BUS_L2P(ha,b)].io_cnt[t]++;
2146 } else if (t >= MAX_HDRIVES || !ha->hdr[t].present || l != 0) {
2147 TRACE2(("Command 0x%x to bus %d id %d lun %d -> IGNORE\n",
2148 nscp->cmnd[0], b, t, l));
2149 nscp->result = DID_BAD_TARGET << 16;
Boaz Harroshf842b642007-10-02 23:16:01 +02002150 if (!nscp_cmndinfo->wait_for_completion)
2151 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002153 gdth_scsi_done(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 } else {
2155 switch (nscp->cmnd[0]) {
2156 case TEST_UNIT_READY:
2157 case INQUIRY:
2158 case REQUEST_SENSE:
2159 case READ_CAPACITY:
2160 case VERIFY:
2161 case START_STOP:
2162 case MODE_SENSE:
Hannes Reineckeeb846d92014-11-17 14:25:19 +01002163 case SERVICE_ACTION_IN_16:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 TRACE(("cache cmd %x/%x/%x/%x/%x/%x\n",nscp->cmnd[0],
2165 nscp->cmnd[1],nscp->cmnd[2],nscp->cmnd[3],
2166 nscp->cmnd[4],nscp->cmnd[5]));
2167 if (ha->hdr[t].media_changed && nscp->cmnd[0] != INQUIRY) {
2168 /* return UNIT_ATTENTION */
2169 TRACE2(("cmd 0x%x target %d: UNIT_ATTENTION\n",
2170 nscp->cmnd[0], t));
2171 ha->hdr[t].media_changed = FALSE;
2172 memset((char*)nscp->sense_buffer,0,16);
2173 nscp->sense_buffer[0] = 0x70;
2174 nscp->sense_buffer[2] = UNIT_ATTENTION;
2175 nscp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
Boaz Harroshf842b642007-10-02 23:16:01 +02002176 if (!nscp_cmndinfo->wait_for_completion)
2177 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002179 gdth_scsi_done(nscp);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002180 } else if (gdth_internal_cache_cmd(ha, nscp))
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002181 gdth_scsi_done(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 break;
2183
2184 case ALLOW_MEDIUM_REMOVAL:
2185 TRACE(("cache cmd %x/%x/%x/%x/%x/%x\n",nscp->cmnd[0],
2186 nscp->cmnd[1],nscp->cmnd[2],nscp->cmnd[3],
2187 nscp->cmnd[4],nscp->cmnd[5]));
2188 if ( (nscp->cmnd[4]&1) && !(ha->hdr[t].devtype&1) ) {
2189 TRACE(("Prevent r. nonremov. drive->do nothing\n"));
2190 nscp->result = DID_OK << 16;
2191 nscp->sense_buffer[0] = 0;
Boaz Harroshf842b642007-10-02 23:16:01 +02002192 if (!nscp_cmndinfo->wait_for_completion)
2193 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002195 gdth_scsi_done(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 } else {
2197 nscp->cmnd[3] = (ha->hdr[t].devtype&1) ? 1:0;
2198 TRACE(("Prevent/allow r. %d rem. drive %d\n",
2199 nscp->cmnd[4],nscp->cmnd[3]));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002200 if (!(cmd_index=gdth_fill_cache_cmd(ha, nscp, t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 this_cmd = FALSE;
2202 }
2203 break;
2204
2205 case RESERVE:
2206 case RELEASE:
2207 TRACE2(("cache cmd %s\n",nscp->cmnd[0] == RESERVE ?
2208 "RESERVE" : "RELEASE"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002209 if (!(cmd_index=gdth_fill_cache_cmd(ha, nscp, t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 this_cmd = FALSE;
2211 break;
2212
2213 case READ_6:
2214 case WRITE_6:
2215 case READ_10:
2216 case WRITE_10:
2217 case READ_16:
2218 case WRITE_16:
2219 if (ha->hdr[t].media_changed) {
2220 /* return UNIT_ATTENTION */
2221 TRACE2(("cmd 0x%x target %d: UNIT_ATTENTION\n",
2222 nscp->cmnd[0], t));
2223 ha->hdr[t].media_changed = FALSE;
2224 memset((char*)nscp->sense_buffer,0,16);
2225 nscp->sense_buffer[0] = 0x70;
2226 nscp->sense_buffer[2] = UNIT_ATTENTION;
2227 nscp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
Boaz Harroshf842b642007-10-02 23:16:01 +02002228 if (!nscp_cmndinfo->wait_for_completion)
2229 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002231 gdth_scsi_done(nscp);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002232 } else if (!(cmd_index=gdth_fill_cache_cmd(ha, nscp, t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 this_cmd = FALSE;
2234 break;
2235
2236 default:
2237 TRACE2(("cache cmd %x/%x/%x/%x/%x/%x unknown\n",nscp->cmnd[0],
2238 nscp->cmnd[1],nscp->cmnd[2],nscp->cmnd[3],
2239 nscp->cmnd[4],nscp->cmnd[5]));
2240 printk("GDT-HA %d: Unknown SCSI command 0x%x to cache service !\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002241 ha->hanum, nscp->cmnd[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 nscp->result = DID_ABORT << 16;
Boaz Harroshf842b642007-10-02 23:16:01 +02002243 if (!nscp_cmndinfo->wait_for_completion)
2244 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002246 gdth_scsi_done(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 break;
2248 }
2249 }
2250
2251 if (!this_cmd)
2252 break;
2253 if (nscp == ha->req_first)
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02002254 ha->req_first = pscp = (struct scsi_cmnd *)nscp->SCp.ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 else
2256 pscp->SCp.ptr = nscp->SCp.ptr;
2257 if (!next_cmd)
2258 break;
2259 }
2260
2261 if (ha->cmd_cnt > 0) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002262 gdth_release_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 }
2264
2265 if (!gdth_polling)
2266 spin_unlock_irqrestore(&ha->smp_lock, flags);
2267
2268 if (gdth_polling && ha->cmd_cnt > 0) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002269 if (!gdth_wait(ha, cmd_index, POLL_TIMEOUT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 printk("GDT-HA %d: Command %d timed out !\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002271 ha->hanum, cmd_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 }
2273}
Boaz Harrosh3892d882007-10-02 23:18:03 +02002274
2275/*
2276 * gdth_copy_internal_data() - copy to/from a buffer onto a scsi_cmnd's
2277 * buffers, kmap_atomic() as needed.
2278 */
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02002279static void gdth_copy_internal_data(gdth_ha_str *ha, struct scsi_cmnd *scp,
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002280 char *buffer, u16 count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002282 u16 cpcount,i, max_sg = scsi_sg_count(scp);
2283 u16 cpsum,cpnow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 struct scatterlist *sl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 char *address;
2286
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002287 cpcount = min_t(u16, count, scsi_bufflen(scp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Boaz Harrosh3892d882007-10-02 23:18:03 +02002289 if (cpcount) {
2290 cpsum=0;
2291 scsi_for_each_sg(scp, sl, max_sg, i) {
Leubner, Achimcbd5f692006-06-09 11:34:29 -07002292 unsigned long flags;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002293 cpnow = (u16)sl->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 TRACE(("copy_internal() now %d sum %d count %d %d\n",
Boaz Harroshd35055a2008-02-27 15:31:20 -08002295 cpnow, cpsum, cpcount, scsi_bufflen(scp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 if (cpsum+cpnow > cpcount)
2297 cpnow = cpcount - cpsum;
2298 cpsum += cpnow;
Jens Axboe45711f12007-10-22 21:19:53 +02002299 if (!sg_page(sl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 printk("GDT-HA %d: invalid sc/gt element in gdth_copy_internal_data()\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002301 ha->hanum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 return;
2303 }
Leubner, Achimcbd5f692006-06-09 11:34:29 -07002304 local_irq_save(flags);
Cong Wang77dfce02011-11-25 23:14:23 +08002305 address = kmap_atomic(sg_page(sl)) + sl->offset;
Boaz Harroshee54cc62008-02-27 15:29:15 -08002306 memcpy(address, buffer, cpnow);
Jens Axboe45711f12007-10-22 21:19:53 +02002307 flush_dcache_page(sg_page(sl));
Cong Wang77dfce02011-11-25 23:14:23 +08002308 kunmap_atomic(address);
Leubner, Achimcbd5f692006-06-09 11:34:29 -07002309 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 if (cpsum == cpcount)
2311 break;
2312 buffer += cpnow;
2313 }
Boaz Harrosh3892d882007-10-02 23:18:03 +02002314 } else if (count) {
2315 printk("GDT-HA %d: SCSI command with no buffers but data transfer expected!\n",
2316 ha->hanum);
2317 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 }
2319}
2320
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02002321static int gdth_internal_cache_cmd(gdth_ha_str *ha, struct scsi_cmnd *scp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002323 u8 t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 gdth_inq_data inq;
2325 gdth_rdcap_data rdc;
2326 gdth_sense_data sd;
2327 gdth_modep_data mpd;
Boaz Harroshf842b642007-10-02 23:16:01 +02002328 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 t = scp->device->id;
2331 TRACE(("gdth_internal_cache_cmd() cmd 0x%x hdrive %d\n",
2332 scp->cmnd[0],t));
2333
2334 scp->result = DID_OK << 16;
2335 scp->sense_buffer[0] = 0;
2336
2337 switch (scp->cmnd[0]) {
2338 case TEST_UNIT_READY:
2339 case VERIFY:
2340 case START_STOP:
2341 TRACE2(("Test/Verify/Start hdrive %d\n",t));
2342 break;
2343
2344 case INQUIRY:
2345 TRACE2(("Inquiry hdrive %d devtype %d\n",
2346 t,ha->hdr[t].devtype));
2347 inq.type_qual = (ha->hdr[t].devtype&4) ? TYPE_ROM:TYPE_DISK;
2348 /* you can here set all disks to removable, if you want to do
2349 a flush using the ALLOW_MEDIUM_REMOVAL command */
2350 inq.modif_rmb = 0x00;
2351 if ((ha->hdr[t].devtype & 1) ||
2352 (ha->hdr[t].cluster_type & CLUSTER_DRIVE))
2353 inq.modif_rmb = 0x80;
2354 inq.version = 2;
2355 inq.resp_aenc = 2;
2356 inq.add_length= 32;
2357 strcpy(inq.vendor,ha->oem_name);
Arnd Bergmann0606ffe2017-07-14 14:06:57 +02002358 snprintf(inq.product, sizeof(inq.product), "Host Drive #%02d",t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 strcpy(inq.revision," ");
Boaz Harroshee54cc62008-02-27 15:29:15 -08002360 gdth_copy_internal_data(ha, scp, (char*)&inq, sizeof(gdth_inq_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 break;
2362
2363 case REQUEST_SENSE:
2364 TRACE2(("Request sense hdrive %d\n",t));
2365 sd.errorcode = 0x70;
2366 sd.segno = 0x00;
2367 sd.key = NO_SENSE;
2368 sd.info = 0;
2369 sd.add_length= 0;
Boaz Harroshee54cc62008-02-27 15:29:15 -08002370 gdth_copy_internal_data(ha, scp, (char*)&sd, sizeof(gdth_sense_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 break;
2372
2373 case MODE_SENSE:
2374 TRACE2(("Mode sense hdrive %d\n",t));
2375 memset((char*)&mpd,0,sizeof(gdth_modep_data));
2376 mpd.hd.data_length = sizeof(gdth_modep_data);
2377 mpd.hd.dev_par = (ha->hdr[t].devtype&2) ? 0x80:0;
2378 mpd.hd.bd_length = sizeof(mpd.bd);
2379 mpd.bd.block_length[0] = (SECTOR_SIZE & 0x00ff0000) >> 16;
2380 mpd.bd.block_length[1] = (SECTOR_SIZE & 0x0000ff00) >> 8;
2381 mpd.bd.block_length[2] = (SECTOR_SIZE & 0x000000ff);
Boaz Harroshee54cc62008-02-27 15:29:15 -08002382 gdth_copy_internal_data(ha, scp, (char*)&mpd, sizeof(gdth_modep_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 break;
2384
2385 case READ_CAPACITY:
2386 TRACE2(("Read capacity hdrive %d\n",t));
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002387 if (ha->hdr[t].size > (u64)0xffffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 rdc.last_block_no = 0xffffffff;
2389 else
2390 rdc.last_block_no = cpu_to_be32(ha->hdr[t].size-1);
2391 rdc.block_length = cpu_to_be32(SECTOR_SIZE);
Boaz Harroshee54cc62008-02-27 15:29:15 -08002392 gdth_copy_internal_data(ha, scp, (char*)&rdc, sizeof(gdth_rdcap_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 break;
2394
Hannes Reineckeeb846d92014-11-17 14:25:19 +01002395 case SERVICE_ACTION_IN_16:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 if ((scp->cmnd[1] & 0x1f) == SAI_READ_CAPACITY_16 &&
2397 (ha->cache_feat & GDT_64BIT)) {
2398 gdth_rdcap16_data rdc16;
2399
2400 TRACE2(("Read capacity (16) hdrive %d\n",t));
2401 rdc16.last_block_no = cpu_to_be64(ha->hdr[t].size-1);
2402 rdc16.block_length = cpu_to_be32(SECTOR_SIZE);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002403 gdth_copy_internal_data(ha, scp, (char*)&rdc16,
Boaz Harroshee54cc62008-02-27 15:29:15 -08002404 sizeof(gdth_rdcap16_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 } else {
2406 scp->result = DID_ABORT << 16;
2407 }
2408 break;
2409
2410 default:
2411 TRACE2(("Internal cache cmd 0x%x unknown\n",scp->cmnd[0]));
2412 break;
2413 }
2414
Boaz Harroshf842b642007-10-02 23:16:01 +02002415 if (!cmndinfo->wait_for_completion)
2416 cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 else
2418 return 1;
2419
2420 return 0;
2421}
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002422
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02002423static int gdth_fill_cache_cmd(gdth_ha_str *ha, struct scsi_cmnd *scp,
2424 u16 hdrive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 register gdth_cmd_str *cmdp;
Boaz Harroshf842b642007-10-02 23:16:01 +02002427 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002428 u32 cnt, blockcnt;
2429 u64 no, blockno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 int i, cmd_index, read_write, sgcnt, mode64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 cmdp = ha->pccb;
2433 TRACE(("gdth_fill_cache_cmd() cmd 0x%x cmdsize %d hdrive %d\n",
2434 scp->cmnd[0],scp->cmd_len,hdrive));
2435
2436 if (ha->type==GDT_EISA && ha->cmd_cnt>0)
2437 return 0;
2438
2439 mode64 = (ha->cache_feat & GDT_64BIT) ? TRUE : FALSE;
2440 /* test for READ_16, WRITE_16 if !mode64 ? ---
2441 not required, should not occur due to error return on
2442 READ_CAPACITY_16 */
2443
2444 cmdp->Service = CACHESERVICE;
2445 cmdp->RequestBuffer = scp;
2446 /* search free command index */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002447 if (!(cmd_index=gdth_get_cmd_index(ha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 TRACE(("GDT: No free command index found\n"));
2449 return 0;
2450 }
2451 /* if it's the first command, set command semaphore */
2452 if (ha->cmd_cnt == 0)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002453 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
2455 /* fill command */
2456 read_write = 0;
Boaz Harroshf842b642007-10-02 23:16:01 +02002457 if (cmndinfo->OpCode != -1)
2458 cmdp->OpCode = cmndinfo->OpCode; /* special cache cmd. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 else if (scp->cmnd[0] == RESERVE)
2460 cmdp->OpCode = GDT_RESERVE_DRV;
2461 else if (scp->cmnd[0] == RELEASE)
2462 cmdp->OpCode = GDT_RELEASE_DRV;
2463 else if (scp->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
2464 if (scp->cmnd[4] & 1) /* prevent ? */
2465 cmdp->OpCode = GDT_MOUNT;
2466 else if (scp->cmnd[3] & 1) /* removable drive ? */
2467 cmdp->OpCode = GDT_UNMOUNT;
2468 else
2469 cmdp->OpCode = GDT_FLUSH;
2470 } else if (scp->cmnd[0] == WRITE_6 || scp->cmnd[0] == WRITE_10 ||
2471 scp->cmnd[0] == WRITE_12 || scp->cmnd[0] == WRITE_16
2472 ) {
2473 read_write = 1;
2474 if (gdth_write_through || ((ha->hdr[hdrive].rw_attribs & 1) &&
2475 (ha->cache_feat & GDT_WR_THROUGH)))
2476 cmdp->OpCode = GDT_WRITE_THR;
2477 else
2478 cmdp->OpCode = GDT_WRITE;
2479 } else {
2480 read_write = 2;
2481 cmdp->OpCode = GDT_READ;
2482 }
2483
2484 cmdp->BoardNode = LOCALBOARD;
2485 if (mode64) {
2486 cmdp->u.cache64.DeviceNo = hdrive;
2487 cmdp->u.cache64.BlockNo = 1;
2488 cmdp->u.cache64.sg_canz = 0;
2489 } else {
2490 cmdp->u.cache.DeviceNo = hdrive;
2491 cmdp->u.cache.BlockNo = 1;
2492 cmdp->u.cache.sg_canz = 0;
2493 }
2494
2495 if (read_write) {
2496 if (scp->cmd_len == 16) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002497 memcpy(&no, &scp->cmnd[2], sizeof(u64));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 blockno = be64_to_cpu(no);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002499 memcpy(&cnt, &scp->cmnd[10], sizeof(u32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 blockcnt = be32_to_cpu(cnt);
2501 } else if (scp->cmd_len == 10) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002502 memcpy(&no, &scp->cmnd[2], sizeof(u32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 blockno = be32_to_cpu(no);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002504 memcpy(&cnt, &scp->cmnd[7], sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 blockcnt = be16_to_cpu(cnt);
2506 } else {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002507 memcpy(&no, &scp->cmnd[0], sizeof(u32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 blockno = be32_to_cpu(no) & 0x001fffffUL;
2509 blockcnt= scp->cmnd[4]==0 ? 0x100 : scp->cmnd[4];
2510 }
2511 if (mode64) {
2512 cmdp->u.cache64.BlockNo = blockno;
2513 cmdp->u.cache64.BlockCnt = blockcnt;
2514 } else {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002515 cmdp->u.cache.BlockNo = (u32)blockno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 cmdp->u.cache.BlockCnt = blockcnt;
2517 }
2518
Boaz Harroshd35055a2008-02-27 15:31:20 -08002519 if (scsi_bufflen(scp)) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002520 cmndinfo->dma_dir = (read_write == 1 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
Boaz Harroshd35055a2008-02-27 15:31:20 -08002522 sgcnt = pci_map_sg(ha->pdev, scsi_sglist(scp), scsi_sg_count(scp),
Boaz Harrosh3892d882007-10-02 23:18:03 +02002523 cmndinfo->dma_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 if (mode64) {
Boaz Harrosh3892d882007-10-02 23:18:03 +02002525 struct scatterlist *sl;
2526
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002527 cmdp->u.cache64.DestAddr= (u64)-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 cmdp->u.cache64.sg_canz = sgcnt;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002529 scsi_for_each_sg(scp, sl, sgcnt, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 cmdp->u.cache64.sg_lst[i].sg_ptr = sg_dma_address(sl);
2531#ifdef GDTH_DMA_STATISTICS
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002532 if (cmdp->u.cache64.sg_lst[i].sg_ptr > (u64)0xffffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 ha->dma64_cnt++;
2534 else
2535 ha->dma32_cnt++;
2536#endif
2537 cmdp->u.cache64.sg_lst[i].sg_len = sg_dma_len(sl);
2538 }
2539 } else {
Boaz Harrosh3892d882007-10-02 23:18:03 +02002540 struct scatterlist *sl;
2541
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 cmdp->u.cache.DestAddr= 0xffffffff;
2543 cmdp->u.cache.sg_canz = sgcnt;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002544 scsi_for_each_sg(scp, sl, sgcnt, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 cmdp->u.cache.sg_lst[i].sg_ptr = sg_dma_address(sl);
2546#ifdef GDTH_DMA_STATISTICS
2547 ha->dma32_cnt++;
2548#endif
2549 cmdp->u.cache.sg_lst[i].sg_len = sg_dma_len(sl);
2550 }
2551 }
2552
2553#ifdef GDTH_STATISTICS
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002554 if (max_sg < (u32)sgcnt) {
2555 max_sg = (u32)sgcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 TRACE3(("GDT: max_sg = %d\n",max_sg));
2557 }
2558#endif
2559
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 }
2561 }
2562 /* evaluate command size, check space */
2563 if (mode64) {
2564 TRACE(("cache cmd: addr. %x sganz %x sgptr0 %x sglen0 %x\n",
2565 cmdp->u.cache64.DestAddr,cmdp->u.cache64.sg_canz,
2566 cmdp->u.cache64.sg_lst[0].sg_ptr,
2567 cmdp->u.cache64.sg_lst[0].sg_len));
2568 TRACE(("cache cmd: cmd %d blockno. %d, blockcnt %d\n",
2569 cmdp->OpCode,cmdp->u.cache64.BlockNo,cmdp->u.cache64.BlockCnt));
2570 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.cache64.sg_lst) +
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002571 (u16)cmdp->u.cache64.sg_canz * sizeof(gdth_sg64_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 } else {
2573 TRACE(("cache cmd: addr. %x sganz %x sgptr0 %x sglen0 %x\n",
2574 cmdp->u.cache.DestAddr,cmdp->u.cache.sg_canz,
2575 cmdp->u.cache.sg_lst[0].sg_ptr,
2576 cmdp->u.cache.sg_lst[0].sg_len));
2577 TRACE(("cache cmd: cmd %d blockno. %d, blockcnt %d\n",
2578 cmdp->OpCode,cmdp->u.cache.BlockNo,cmdp->u.cache.BlockCnt));
2579 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.cache.sg_lst) +
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002580 (u16)cmdp->u.cache.sg_canz * sizeof(gdth_sg_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 }
2582 if (ha->cmd_len & 3)
2583 ha->cmd_len += (4 - (ha->cmd_len & 3));
2584
2585 if (ha->cmd_cnt > 0) {
2586 if ((ha->cmd_offs_dpmem + ha->cmd_len + DPMEM_COMMAND_OFFSET) >
2587 ha->ic_all_size) {
2588 TRACE2(("gdth_fill_cache() DPMEM overflow\n"));
2589 ha->cmd_tab[cmd_index-2].cmnd = UNUSED_CMND;
2590 return 0;
2591 }
2592 }
2593
2594 /* copy command */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002595 gdth_copy_command(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 return cmd_index;
2597}
2598
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02002599static int gdth_fill_raw_cmd(gdth_ha_str *ha, struct scsi_cmnd *scp, u8 b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 register gdth_cmd_str *cmdp;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002602 u16 i;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002603 dma_addr_t sense_paddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 int cmd_index, sgcnt, mode64;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002605 u8 t,l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 struct page *page;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002607 unsigned long offset;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02002608 struct gdth_cmndinfo *cmndinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 t = scp->device->id;
2611 l = scp->device->lun;
2612 cmdp = ha->pccb;
2613 TRACE(("gdth_fill_raw_cmd() cmd 0x%x bus %d ID %d LUN %d\n",
2614 scp->cmnd[0],b,t,l));
2615
2616 if (ha->type==GDT_EISA && ha->cmd_cnt>0)
2617 return 0;
2618
2619 mode64 = (ha->raw_feat & GDT_64BIT) ? TRUE : FALSE;
2620
2621 cmdp->Service = SCSIRAWSERVICE;
2622 cmdp->RequestBuffer = scp;
2623 /* search free command index */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002624 if (!(cmd_index=gdth_get_cmd_index(ha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 TRACE(("GDT: No free command index found\n"));
2626 return 0;
2627 }
2628 /* if it's the first command, set command semaphore */
2629 if (ha->cmd_cnt == 0)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002630 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02002632 cmndinfo = gdth_cmnd_priv(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 /* fill command */
Boaz Harroshf842b642007-10-02 23:16:01 +02002634 if (cmndinfo->OpCode != -1) {
2635 cmdp->OpCode = cmndinfo->OpCode; /* special raw cmd. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 cmdp->BoardNode = LOCALBOARD;
2637 if (mode64) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002638 cmdp->u.raw64.direction = (cmndinfo->phase >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 TRACE2(("special raw cmd 0x%x param 0x%x\n",
2640 cmdp->OpCode, cmdp->u.raw64.direction));
2641 /* evaluate command size */
2642 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw64.sg_lst);
2643 } else {
Boaz Harroshf842b642007-10-02 23:16:01 +02002644 cmdp->u.raw.direction = (cmndinfo->phase >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 TRACE2(("special raw cmd 0x%x param 0x%x\n",
2646 cmdp->OpCode, cmdp->u.raw.direction));
2647 /* evaluate command size */
2648 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw.sg_lst);
2649 }
2650
2651 } else {
2652 page = virt_to_page(scp->sense_buffer);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002653 offset = (unsigned long)scp->sense_buffer & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 sense_paddr = pci_map_page(ha->pdev,page,offset,
2655 16,PCI_DMA_FROMDEVICE);
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02002656
2657 cmndinfo->sense_paddr = sense_paddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 cmdp->OpCode = GDT_WRITE; /* always */
2659 cmdp->BoardNode = LOCALBOARD;
2660 if (mode64) {
2661 cmdp->u.raw64.reserved = 0;
2662 cmdp->u.raw64.mdisc_time = 0;
2663 cmdp->u.raw64.mcon_time = 0;
2664 cmdp->u.raw64.clen = scp->cmd_len;
2665 cmdp->u.raw64.target = t;
2666 cmdp->u.raw64.lun = l;
2667 cmdp->u.raw64.bus = b;
2668 cmdp->u.raw64.priority = 0;
Boaz Harroshd35055a2008-02-27 15:31:20 -08002669 cmdp->u.raw64.sdlen = scsi_bufflen(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 cmdp->u.raw64.sense_len = 16;
2671 cmdp->u.raw64.sense_data = sense_paddr;
2672 cmdp->u.raw64.direction =
2673 gdth_direction_tab[scp->cmnd[0]]==DOU ? GDTH_DATA_OUT:GDTH_DATA_IN;
2674 memcpy(cmdp->u.raw64.cmd,scp->cmnd,16);
Joerg Dorchainbb9ba312007-03-06 02:46:54 -08002675 cmdp->u.raw64.sg_ranz = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 } else {
2677 cmdp->u.raw.reserved = 0;
2678 cmdp->u.raw.mdisc_time = 0;
2679 cmdp->u.raw.mcon_time = 0;
2680 cmdp->u.raw.clen = scp->cmd_len;
2681 cmdp->u.raw.target = t;
2682 cmdp->u.raw.lun = l;
2683 cmdp->u.raw.bus = b;
2684 cmdp->u.raw.priority = 0;
2685 cmdp->u.raw.link_p = 0;
Boaz Harroshd35055a2008-02-27 15:31:20 -08002686 cmdp->u.raw.sdlen = scsi_bufflen(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 cmdp->u.raw.sense_len = 16;
2688 cmdp->u.raw.sense_data = sense_paddr;
2689 cmdp->u.raw.direction =
2690 gdth_direction_tab[scp->cmnd[0]]==DOU ? GDTH_DATA_OUT:GDTH_DATA_IN;
2691 memcpy(cmdp->u.raw.cmd,scp->cmnd,12);
Joerg Dorchainbb9ba312007-03-06 02:46:54 -08002692 cmdp->u.raw.sg_ranz = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 }
2694
Boaz Harroshd35055a2008-02-27 15:31:20 -08002695 if (scsi_bufflen(scp)) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002696 cmndinfo->dma_dir = PCI_DMA_BIDIRECTIONAL;
Boaz Harroshd35055a2008-02-27 15:31:20 -08002697 sgcnt = pci_map_sg(ha->pdev, scsi_sglist(scp), scsi_sg_count(scp),
Boaz Harrosh3892d882007-10-02 23:18:03 +02002698 cmndinfo->dma_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 if (mode64) {
Boaz Harrosh3892d882007-10-02 23:18:03 +02002700 struct scatterlist *sl;
2701
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002702 cmdp->u.raw64.sdata = (u64)-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 cmdp->u.raw64.sg_ranz = sgcnt;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002704 scsi_for_each_sg(scp, sl, sgcnt, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 cmdp->u.raw64.sg_lst[i].sg_ptr = sg_dma_address(sl);
2706#ifdef GDTH_DMA_STATISTICS
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002707 if (cmdp->u.raw64.sg_lst[i].sg_ptr > (u64)0xffffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 ha->dma64_cnt++;
2709 else
2710 ha->dma32_cnt++;
2711#endif
2712 cmdp->u.raw64.sg_lst[i].sg_len = sg_dma_len(sl);
2713 }
2714 } else {
Boaz Harrosh3892d882007-10-02 23:18:03 +02002715 struct scatterlist *sl;
2716
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 cmdp->u.raw.sdata = 0xffffffff;
2718 cmdp->u.raw.sg_ranz = sgcnt;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002719 scsi_for_each_sg(scp, sl, sgcnt, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 cmdp->u.raw.sg_lst[i].sg_ptr = sg_dma_address(sl);
2721#ifdef GDTH_DMA_STATISTICS
2722 ha->dma32_cnt++;
2723#endif
2724 cmdp->u.raw.sg_lst[i].sg_len = sg_dma_len(sl);
2725 }
2726 }
2727
2728#ifdef GDTH_STATISTICS
2729 if (max_sg < sgcnt) {
2730 max_sg = sgcnt;
2731 TRACE3(("GDT: max_sg = %d\n",sgcnt));
2732 }
2733#endif
2734
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 }
2736 if (mode64) {
2737 TRACE(("raw cmd: addr. %x sganz %x sgptr0 %x sglen0 %x\n",
2738 cmdp->u.raw64.sdata,cmdp->u.raw64.sg_ranz,
2739 cmdp->u.raw64.sg_lst[0].sg_ptr,
2740 cmdp->u.raw64.sg_lst[0].sg_len));
2741 /* evaluate command size */
2742 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw64.sg_lst) +
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002743 (u16)cmdp->u.raw64.sg_ranz * sizeof(gdth_sg64_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 } else {
2745 TRACE(("raw cmd: addr. %x sganz %x sgptr0 %x sglen0 %x\n",
2746 cmdp->u.raw.sdata,cmdp->u.raw.sg_ranz,
2747 cmdp->u.raw.sg_lst[0].sg_ptr,
2748 cmdp->u.raw.sg_lst[0].sg_len));
2749 /* evaluate command size */
2750 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw.sg_lst) +
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002751 (u16)cmdp->u.raw.sg_ranz * sizeof(gdth_sg_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 }
2753 }
2754 /* check space */
2755 if (ha->cmd_len & 3)
2756 ha->cmd_len += (4 - (ha->cmd_len & 3));
2757
2758 if (ha->cmd_cnt > 0) {
2759 if ((ha->cmd_offs_dpmem + ha->cmd_len + DPMEM_COMMAND_OFFSET) >
2760 ha->ic_all_size) {
2761 TRACE2(("gdth_fill_raw() DPMEM overflow\n"));
2762 ha->cmd_tab[cmd_index-2].cmnd = UNUSED_CMND;
2763 return 0;
2764 }
2765 }
2766
2767 /* copy command */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002768 gdth_copy_command(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 return cmd_index;
2770}
2771
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02002772static int gdth_special_cmd(gdth_ha_str *ha, struct scsi_cmnd *scp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 register gdth_cmd_str *cmdp;
Boaz Harroshee54cc62008-02-27 15:29:15 -08002775 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 int cmd_index;
2777
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 cmdp= ha->pccb;
2779 TRACE2(("gdth_special_cmd(): "));
2780
2781 if (ha->type==GDT_EISA && ha->cmd_cnt>0)
2782 return 0;
2783
Boaz Harroshee54cc62008-02-27 15:29:15 -08002784 *cmdp = *cmndinfo->internal_cmd_str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 cmdp->RequestBuffer = scp;
2786
2787 /* search free command index */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002788 if (!(cmd_index=gdth_get_cmd_index(ha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 TRACE(("GDT: No free command index found\n"));
2790 return 0;
2791 }
2792
2793 /* if it's the first command, set command semaphore */
2794 if (ha->cmd_cnt == 0)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002795 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
2797 /* evaluate command size, check space */
2798 if (cmdp->OpCode == GDT_IOCTL) {
2799 TRACE2(("IOCTL\n"));
2800 ha->cmd_len =
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002801 GDTOFFSOF(gdth_cmd_str,u.ioctl.p_param) + sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 } else if (cmdp->Service == CACHESERVICE) {
2803 TRACE2(("cache command %d\n",cmdp->OpCode));
2804 if (ha->cache_feat & GDT_64BIT)
2805 ha->cmd_len =
2806 GDTOFFSOF(gdth_cmd_str,u.cache64.sg_lst) + sizeof(gdth_sg64_str);
2807 else
2808 ha->cmd_len =
2809 GDTOFFSOF(gdth_cmd_str,u.cache.sg_lst) + sizeof(gdth_sg_str);
2810 } else if (cmdp->Service == SCSIRAWSERVICE) {
2811 TRACE2(("raw command %d\n",cmdp->OpCode));
2812 if (ha->raw_feat & GDT_64BIT)
2813 ha->cmd_len =
2814 GDTOFFSOF(gdth_cmd_str,u.raw64.sg_lst) + sizeof(gdth_sg64_str);
2815 else
2816 ha->cmd_len =
2817 GDTOFFSOF(gdth_cmd_str,u.raw.sg_lst) + sizeof(gdth_sg_str);
2818 }
2819
2820 if (ha->cmd_len & 3)
2821 ha->cmd_len += (4 - (ha->cmd_len & 3));
2822
2823 if (ha->cmd_cnt > 0) {
2824 if ((ha->cmd_offs_dpmem + ha->cmd_len + DPMEM_COMMAND_OFFSET) >
2825 ha->ic_all_size) {
2826 TRACE2(("gdth_special_cmd() DPMEM overflow\n"));
2827 ha->cmd_tab[cmd_index-2].cmnd = UNUSED_CMND;
2828 return 0;
2829 }
2830 }
2831
2832 /* copy command */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002833 gdth_copy_command(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 return cmd_index;
2835}
2836
2837
2838/* Controller event handling functions */
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002839static gdth_evt_str *gdth_store_event(gdth_ha_str *ha, u16 source,
2840 u16 idx, gdth_evt_data *evt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
2842 gdth_evt_str *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
2844 /* no GDTH_LOCK_HA() ! */
2845 TRACE2(("gdth_store_event() source %d idx %d\n", source, idx));
2846 if (source == 0) /* no source -> no event */
2847 return NULL;
2848
2849 if (ebuffer[elastidx].event_source == source &&
2850 ebuffer[elastidx].event_idx == idx &&
2851 ((evt->size != 0 && ebuffer[elastidx].event_data.size != 0 &&
2852 !memcmp((char *)&ebuffer[elastidx].event_data.eu,
2853 (char *)&evt->eu, evt->size)) ||
2854 (evt->size == 0 && ebuffer[elastidx].event_data.size == 0 &&
2855 !strcmp((char *)&ebuffer[elastidx].event_data.event_string,
2856 (char *)&evt->event_string)))) {
2857 e = &ebuffer[elastidx];
Alison Schofield5a412c32016-02-17 21:29:34 -08002858 e->last_stamp = (u32)ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 ++e->same_count;
2860 } else {
2861 if (ebuffer[elastidx].event_source != 0) { /* entry not free ? */
2862 ++elastidx;
2863 if (elastidx == MAX_EVENTS)
2864 elastidx = 0;
2865 if (elastidx == eoldidx) { /* reached mark ? */
2866 ++eoldidx;
2867 if (eoldidx == MAX_EVENTS)
2868 eoldidx = 0;
2869 }
2870 }
2871 e = &ebuffer[elastidx];
2872 e->event_source = source;
2873 e->event_idx = idx;
Alison Schofield5a412c32016-02-17 21:29:34 -08002874 e->first_stamp = e->last_stamp = (u32)ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 e->same_count = 1;
2876 e->event_data = *evt;
2877 e->application = 0;
2878 }
2879 return e;
2880}
2881
2882static int gdth_read_event(gdth_ha_str *ha, int handle, gdth_evt_str *estr)
2883{
2884 gdth_evt_str *e;
2885 int eindex;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002886 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887
2888 TRACE2(("gdth_read_event() handle %d\n", handle));
2889 spin_lock_irqsave(&ha->smp_lock, flags);
2890 if (handle == -1)
2891 eindex = eoldidx;
2892 else
2893 eindex = handle;
2894 estr->event_source = 0;
2895
Dave Jones690e7442009-10-19 19:55:13 -04002896 if (eindex < 0 || eindex >= MAX_EVENTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 spin_unlock_irqrestore(&ha->smp_lock, flags);
2898 return eindex;
2899 }
2900 e = &ebuffer[eindex];
2901 if (e->event_source != 0) {
2902 if (eindex != elastidx) {
2903 if (++eindex == MAX_EVENTS)
2904 eindex = 0;
2905 } else {
2906 eindex = -1;
2907 }
2908 memcpy(estr, e, sizeof(gdth_evt_str));
2909 }
2910 spin_unlock_irqrestore(&ha->smp_lock, flags);
2911 return eindex;
2912}
2913
2914static void gdth_readapp_event(gdth_ha_str *ha,
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002915 u8 application, gdth_evt_str *estr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916{
2917 gdth_evt_str *e;
2918 int eindex;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002919 unsigned long flags;
2920 u8 found = FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
2922 TRACE2(("gdth_readapp_event() app. %d\n", application));
2923 spin_lock_irqsave(&ha->smp_lock, flags);
2924 eindex = eoldidx;
2925 for (;;) {
2926 e = &ebuffer[eindex];
2927 if (e->event_source == 0)
2928 break;
2929 if ((e->application & application) == 0) {
2930 e->application |= application;
2931 found = TRUE;
2932 break;
2933 }
2934 if (eindex == elastidx)
2935 break;
2936 if (++eindex == MAX_EVENTS)
2937 eindex = 0;
2938 }
2939 if (found)
2940 memcpy(estr, e, sizeof(gdth_evt_str));
2941 else
2942 estr->event_source = 0;
2943 spin_unlock_irqrestore(&ha->smp_lock, flags);
2944}
2945
2946static void gdth_clear_events(void)
2947{
2948 TRACE(("gdth_clear_events()"));
2949
2950 eoldidx = elastidx = 0;
2951 ebuffer[0].event_source = 0;
2952}
2953
2954
2955/* SCSI interface functions */
2956
Jeff Garzik230e8862007-12-13 16:14:12 -08002957static irqreturn_t __gdth_interrupt(gdth_ha_str *ha,
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002958 int gdth_from_wait, int* pIndex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 gdt6m_dpram_str __iomem *dp6m_ptr = NULL;
2961 gdt6_dpram_str __iomem *dp6_ptr;
2962 gdt2_dpram_str __iomem *dp2_ptr;
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02002963 struct scsi_cmnd *scp;
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002964 int rval, i;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002965 u8 IStatus;
2966 u16 Service;
2967 unsigned long flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968#ifdef INT_COAL
2969 int coalesced = FALSE;
2970 int next = FALSE;
2971 gdth_coal_status *pcs = NULL;
2972 int act_int_coal = 0;
2973#endif
2974
Jeff Garzik230e8862007-12-13 16:14:12 -08002975 TRACE(("gdth_interrupt() IRQ %d\n", ha->irq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
2977 /* if polling and not from gdth_wait() -> return */
2978 if (gdth_polling) {
2979 if (!gdth_from_wait) {
2980 return IRQ_HANDLED;
2981 }
2982 }
2983
2984 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002985 spin_lock_irqsave(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
2987 /* search controller */
Jeff Garzik230e8862007-12-13 16:14:12 -08002988 IStatus = gdth_get_status(ha);
2989 if (IStatus == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 /* spurious interrupt */
2991 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002992 spin_unlock_irqrestore(&ha->smp_lock, flags);
2993 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
2996#ifdef GDTH_STATISTICS
2997 ++act_ints;
2998#endif
2999
3000#ifdef INT_COAL
3001 /* See if the fw is returning coalesced status */
3002 if (IStatus == COALINDEX) {
3003 /* Coalesced status. Setup the initial status
3004 buffer pointer and flags */
3005 pcs = ha->coal_stat;
3006 coalesced = TRUE;
3007 next = TRUE;
3008 }
3009
3010 do {
3011 if (coalesced) {
3012 /* For coalesced requests all status
3013 information is found in the status buffer */
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003014 IStatus = (u8)(pcs->status & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 }
3016#endif
3017
3018 if (ha->type == GDT_EISA) {
3019 if (IStatus & 0x80) { /* error flag */
3020 IStatus &= ~0x80;
3021 ha->status = inw(ha->bmic + MAILBOXREG+8);
3022 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
3023 } else /* no error */
3024 ha->status = S_OK;
3025 ha->info = inl(ha->bmic + MAILBOXREG+12);
3026 ha->service = inw(ha->bmic + MAILBOXREG+10);
3027 ha->info2 = inl(ha->bmic + MAILBOXREG+4);
3028
3029 outb(0xff, ha->bmic + EDOORREG); /* acknowledge interrupt */
3030 outb(0x00, ha->bmic + SEMA1REG); /* reset status semaphore */
3031 } else if (ha->type == GDT_ISA) {
3032 dp2_ptr = ha->brd;
3033 if (IStatus & 0x80) { /* error flag */
3034 IStatus &= ~0x80;
Jeff Garzika52667f2007-10-02 22:55:53 +02003035 ha->status = readw(&dp2_ptr->u.ic.Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
3037 } else /* no error */
3038 ha->status = S_OK;
Jeff Garzika52667f2007-10-02 22:55:53 +02003039 ha->info = readl(&dp2_ptr->u.ic.Info[0]);
3040 ha->service = readw(&dp2_ptr->u.ic.Service);
3041 ha->info2 = readl(&dp2_ptr->u.ic.Info[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
Jeff Garzika52667f2007-10-02 22:55:53 +02003043 writeb(0xff, &dp2_ptr->io.irqdel); /* acknowledge interrupt */
3044 writeb(0, &dp2_ptr->u.ic.Cmd_Index);/* reset command index */
3045 writeb(0, &dp2_ptr->io.Sema1); /* reset status semaphore */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 } else if (ha->type == GDT_PCI) {
3047 dp6_ptr = ha->brd;
3048 if (IStatus & 0x80) { /* error flag */
3049 IStatus &= ~0x80;
Jeff Garzika52667f2007-10-02 22:55:53 +02003050 ha->status = readw(&dp6_ptr->u.ic.Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
3052 } else /* no error */
3053 ha->status = S_OK;
Jeff Garzika52667f2007-10-02 22:55:53 +02003054 ha->info = readl(&dp6_ptr->u.ic.Info[0]);
3055 ha->service = readw(&dp6_ptr->u.ic.Service);
3056 ha->info2 = readl(&dp6_ptr->u.ic.Info[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
Jeff Garzika52667f2007-10-02 22:55:53 +02003058 writeb(0xff, &dp6_ptr->io.irqdel); /* acknowledge interrupt */
3059 writeb(0, &dp6_ptr->u.ic.Cmd_Index);/* reset command index */
3060 writeb(0, &dp6_ptr->io.Sema1); /* reset status semaphore */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 } else if (ha->type == GDT_PCINEW) {
3062 if (IStatus & 0x80) { /* error flag */
3063 IStatus &= ~0x80;
3064 ha->status = inw(PTR2USHORT(&ha->plx->status));
3065 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
3066 } else
3067 ha->status = S_OK;
3068 ha->info = inl(PTR2USHORT(&ha->plx->info[0]));
3069 ha->service = inw(PTR2USHORT(&ha->plx->service));
3070 ha->info2 = inl(PTR2USHORT(&ha->plx->info[1]));
3071
3072 outb(0xff, PTR2USHORT(&ha->plx->edoor_reg));
3073 outb(0x00, PTR2USHORT(&ha->plx->sema1_reg));
3074 } else if (ha->type == GDT_PCIMPR) {
3075 dp6m_ptr = ha->brd;
3076 if (IStatus & 0x80) { /* error flag */
3077 IStatus &= ~0x80;
3078#ifdef INT_COAL
3079 if (coalesced)
Jean Delvare107e7162006-11-09 21:45:09 +01003080 ha->status = pcs->ext_status & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 else
3082#endif
Jeff Garzika52667f2007-10-02 22:55:53 +02003083 ha->status = readw(&dp6m_ptr->i960r.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
3085 } else /* no error */
3086 ha->status = S_OK;
3087#ifdef INT_COAL
3088 /* get information */
3089 if (coalesced) {
3090 ha->info = pcs->info0;
3091 ha->info2 = pcs->info1;
Jean Delvare107e7162006-11-09 21:45:09 +01003092 ha->service = (pcs->ext_status >> 16) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 } else
3094#endif
3095 {
Jeff Garzika52667f2007-10-02 22:55:53 +02003096 ha->info = readl(&dp6m_ptr->i960r.info[0]);
3097 ha->service = readw(&dp6m_ptr->i960r.service);
3098 ha->info2 = readl(&dp6m_ptr->i960r.info[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 }
3100 /* event string */
3101 if (IStatus == ASYNCINDEX) {
3102 if (ha->service != SCREENSERVICE &&
3103 (ha->fw_vers & 0xff) >= 0x1a) {
Jeff Garzika52667f2007-10-02 22:55:53 +02003104 ha->dvr.severity = readb
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 (&((gdt6m_dpram_str __iomem *)ha->brd)->i960r.severity);
3106 for (i = 0; i < 256; ++i) {
Jeff Garzika52667f2007-10-02 22:55:53 +02003107 ha->dvr.event_string[i] = readb
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 (&((gdt6m_dpram_str __iomem *)ha->brd)->i960r.evt_str[i]);
3109 if (ha->dvr.event_string[i] == 0)
3110 break;
3111 }
3112 }
3113 }
3114#ifdef INT_COAL
3115 /* Make sure that non coalesced interrupts get cleared
3116 before being handled by gdth_async_event/gdth_sync_event */
3117 if (!coalesced)
3118#endif
3119 {
Jeff Garzika52667f2007-10-02 22:55:53 +02003120 writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
3121 writeb(0, &dp6m_ptr->i960r.sema1_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 }
3123 } else {
3124 TRACE2(("gdth_interrupt() unknown controller type\n"));
3125 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003126 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 return IRQ_HANDLED;
3128 }
3129
3130 TRACE(("gdth_interrupt() index %d stat %d info %d\n",
3131 IStatus,ha->status,ha->info));
3132
3133 if (gdth_from_wait) {
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003134 *pIndex = (int)IStatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 }
3136
3137 if (IStatus == ASYNCINDEX) {
3138 TRACE2(("gdth_interrupt() async. event\n"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003139 gdth_async_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003141 spin_unlock_irqrestore(&ha->smp_lock, flags);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003142 gdth_next(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 return IRQ_HANDLED;
3144 }
3145
3146 if (IStatus == SPEZINDEX) {
3147 TRACE2(("Service unknown or not initialized !\n"));
3148 ha->dvr.size = sizeof(ha->dvr.eu.driver);
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003149 ha->dvr.eu.driver.ionode = ha->hanum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 gdth_store_event(ha, ES_DRIVER, 4, &ha->dvr);
3151 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003152 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 return IRQ_HANDLED;
3154 }
3155 scp = ha->cmd_tab[IStatus-2].cmnd;
3156 Service = ha->cmd_tab[IStatus-2].service;
3157 ha->cmd_tab[IStatus-2].cmnd = UNUSED_CMND;
3158 if (scp == UNUSED_CMND) {
3159 TRACE2(("gdth_interrupt() index to unused command (%d)\n",IStatus));
3160 ha->dvr.size = sizeof(ha->dvr.eu.driver);
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003161 ha->dvr.eu.driver.ionode = ha->hanum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 ha->dvr.eu.driver.index = IStatus;
3163 gdth_store_event(ha, ES_DRIVER, 1, &ha->dvr);
3164 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003165 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 return IRQ_HANDLED;
3167 }
3168 if (scp == INTERNAL_CMND) {
3169 TRACE(("gdth_interrupt() answer to internal command\n"));
3170 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003171 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 return IRQ_HANDLED;
3173 }
3174
3175 TRACE(("gdth_interrupt() sync. status\n"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003176 rval = gdth_sync_event(ha,Service,IStatus,scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003178 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 if (rval == 2) {
Boaz Harroshf842b642007-10-02 23:16:01 +02003180 gdth_putq(ha, scp, gdth_cmnd_priv(scp)->priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 } else if (rval == 1) {
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02003182 gdth_scsi_done(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 }
3184
3185#ifdef INT_COAL
3186 if (coalesced) {
3187 /* go to the next status in the status buffer */
3188 ++pcs;
3189#ifdef GDTH_STATISTICS
3190 ++act_int_coal;
3191 if (act_int_coal > max_int_coal) {
3192 max_int_coal = act_int_coal;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003193 printk("GDT: max_int_coal = %d\n",(u16)max_int_coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 }
3195#endif
3196 /* see if there is another status */
3197 if (pcs->status == 0)
3198 /* Stop the coalesce loop */
3199 next = FALSE;
3200 }
3201 } while (next);
3202
3203 /* coalescing only for new GDT_PCIMPR controllers available */
3204 if (ha->type == GDT_PCIMPR && coalesced) {
Jeff Garzika52667f2007-10-02 22:55:53 +02003205 writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
3206 writeb(0, &dp6m_ptr->i960r.sema1_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 }
3208#endif
3209
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003210 gdth_next(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 return IRQ_HANDLED;
3212}
3213
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003214static irqreturn_t gdth_interrupt(int irq, void *dev_id)
3215{
Jeff Garzik230e8862007-12-13 16:14:12 -08003216 gdth_ha_str *ha = dev_id;
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003217
Jeff Garzik230e8862007-12-13 16:14:12 -08003218 return __gdth_interrupt(ha, false, NULL);
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003219}
3220
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003221static int gdth_sync_event(gdth_ha_str *ha, int service, u8 index,
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02003222 struct scsi_cmnd *scp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 gdth_msg_str *msg;
3225 gdth_cmd_str *cmdp;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003226 u8 b, t;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02003227 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 cmdp = ha->pccb;
3230 TRACE(("gdth_sync_event() serv %d status %d\n",
3231 service,ha->status));
3232
3233 if (service == SCREENSERVICE) {
3234 msg = ha->pmsg;
3235 TRACE(("len: %d, answer: %d, ext: %d, alen: %d\n",
3236 msg->msg_len,msg->msg_answer,msg->msg_ext,msg->msg_alen));
3237 if (msg->msg_len > MSGLEN+1)
3238 msg->msg_len = MSGLEN+1;
3239 if (msg->msg_len)
3240 if (!(msg->msg_answer && msg->msg_ext)) {
3241 msg->msg_text[msg->msg_len] = '\0';
3242 printk("%s",msg->msg_text);
3243 }
3244
3245 if (msg->msg_ext && !msg->msg_answer) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003246 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 gdth_delay(0);
3248 cmdp->Service = SCREENSERVICE;
3249 cmdp->RequestBuffer = SCREEN_CMND;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003250 gdth_get_cmd_index(ha);
3251 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 cmdp->OpCode = GDT_READ;
3253 cmdp->BoardNode = LOCALBOARD;
3254 cmdp->u.screen.reserved = 0;
3255 cmdp->u.screen.su.msg.msg_handle= msg->msg_handle;
3256 cmdp->u.screen.su.msg.msg_addr = ha->msg_phys;
3257 ha->cmd_offs_dpmem = 0;
3258 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.screen.su.msg.msg_addr)
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003259 + sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 ha->cmd_cnt = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003261 gdth_copy_command(ha);
3262 gdth_release_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 return 0;
3264 }
3265
3266 if (msg->msg_answer && msg->msg_alen) {
3267 /* default answers (getchar() not possible) */
3268 if (msg->msg_alen == 1) {
3269 msg->msg_alen = 0;
3270 msg->msg_len = 1;
3271 msg->msg_text[0] = 0;
3272 } else {
3273 msg->msg_alen -= 2;
3274 msg->msg_len = 2;
3275 msg->msg_text[0] = 1;
3276 msg->msg_text[1] = 0;
3277 }
3278 msg->msg_ext = 0;
3279 msg->msg_answer = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003280 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 gdth_delay(0);
3282 cmdp->Service = SCREENSERVICE;
3283 cmdp->RequestBuffer = SCREEN_CMND;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003284 gdth_get_cmd_index(ha);
3285 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 cmdp->OpCode = GDT_WRITE;
3287 cmdp->BoardNode = LOCALBOARD;
3288 cmdp->u.screen.reserved = 0;
3289 cmdp->u.screen.su.msg.msg_handle= msg->msg_handle;
3290 cmdp->u.screen.su.msg.msg_addr = ha->msg_phys;
3291 ha->cmd_offs_dpmem = 0;
3292 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.screen.su.msg.msg_addr)
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003293 + sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 ha->cmd_cnt = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003295 gdth_copy_command(ha);
3296 gdth_release_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 return 0;
3298 }
3299 printk("\n");
3300
3301 } else {
Christoph Hellwig52759e62007-10-02 22:59:53 +02003302 b = scp->device->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 t = scp->device->id;
Boaz Harroshf842b642007-10-02 23:16:01 +02003304 if (cmndinfo->OpCode == -1 && b != ha->virt_bus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 ha->raw[BUS_L2P(ha,b)].io_cnt[t]--;
3306 }
3307 /* cache or raw service */
3308 if (ha->status == S_BSY) {
3309 TRACE2(("Controller busy -> retry !\n"));
Boaz Harroshf842b642007-10-02 23:16:01 +02003310 if (cmndinfo->OpCode == GDT_MOUNT)
3311 cmndinfo->OpCode = GDT_CLUST_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 /* retry */
3313 return 2;
3314 }
Boaz Harroshd35055a2008-02-27 15:31:20 -08003315 if (scsi_bufflen(scp))
3316 pci_unmap_sg(ha->pdev, scsi_sglist(scp), scsi_sg_count(scp),
Boaz Harrosh3892d882007-10-02 23:18:03 +02003317 cmndinfo->dma_dir);
3318
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02003319 if (cmndinfo->sense_paddr)
3320 pci_unmap_page(ha->pdev, cmndinfo->sense_paddr, 16,
3321 PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
3323 if (ha->status == S_OK) {
Boaz Harroshf842b642007-10-02 23:16:01 +02003324 cmndinfo->status = S_OK;
3325 cmndinfo->info = ha->info;
3326 if (cmndinfo->OpCode != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 TRACE2(("gdth_sync_event(): special cmd 0x%x OK\n",
Boaz Harroshf842b642007-10-02 23:16:01 +02003328 cmndinfo->OpCode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 /* special commands GDT_CLUST_INFO/GDT_MOUNT ? */
Boaz Harroshf842b642007-10-02 23:16:01 +02003330 if (cmndinfo->OpCode == GDT_CLUST_INFO) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003331 ha->hdr[t].cluster_type = (u8)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 if (!(ha->hdr[t].cluster_type &
3333 CLUSTER_MOUNTED)) {
3334 /* NOT MOUNTED -> MOUNT */
Boaz Harroshf842b642007-10-02 23:16:01 +02003335 cmndinfo->OpCode = GDT_MOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 if (ha->hdr[t].cluster_type &
3337 CLUSTER_RESERVED) {
3338 /* cluster drive RESERVED (on the other node) */
Boaz Harroshf842b642007-10-02 23:16:01 +02003339 cmndinfo->phase = -2; /* reservation conflict */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 }
3341 } else {
Boaz Harroshf842b642007-10-02 23:16:01 +02003342 cmndinfo->OpCode = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 }
3344 } else {
Boaz Harroshf842b642007-10-02 23:16:01 +02003345 if (cmndinfo->OpCode == GDT_MOUNT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 ha->hdr[t].cluster_type |= CLUSTER_MOUNTED;
3347 ha->hdr[t].media_changed = TRUE;
Boaz Harroshf842b642007-10-02 23:16:01 +02003348 } else if (cmndinfo->OpCode == GDT_UNMOUNT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 ha->hdr[t].cluster_type &= ~CLUSTER_MOUNTED;
3350 ha->hdr[t].media_changed = TRUE;
3351 }
Boaz Harroshf842b642007-10-02 23:16:01 +02003352 cmndinfo->OpCode = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 }
3354 /* retry */
Boaz Harroshf842b642007-10-02 23:16:01 +02003355 cmndinfo->priority = HIGH_PRI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 return 2;
3357 } else {
3358 /* RESERVE/RELEASE ? */
3359 if (scp->cmnd[0] == RESERVE) {
3360 ha->hdr[t].cluster_type |= CLUSTER_RESERVED;
3361 } else if (scp->cmnd[0] == RELEASE) {
3362 ha->hdr[t].cluster_type &= ~CLUSTER_RESERVED;
3363 }
3364 scp->result = DID_OK << 16;
3365 scp->sense_buffer[0] = 0;
3366 }
3367 } else {
Boaz Harroshf842b642007-10-02 23:16:01 +02003368 cmndinfo->status = ha->status;
3369 cmndinfo->info = ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370
Boaz Harroshf842b642007-10-02 23:16:01 +02003371 if (cmndinfo->OpCode != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 TRACE2(("gdth_sync_event(): special cmd 0x%x error 0x%x\n",
Boaz Harroshf842b642007-10-02 23:16:01 +02003373 cmndinfo->OpCode, ha->status));
3374 if (cmndinfo->OpCode == GDT_SCAN_START ||
3375 cmndinfo->OpCode == GDT_SCAN_END) {
3376 cmndinfo->OpCode = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 /* retry */
Boaz Harroshf842b642007-10-02 23:16:01 +02003378 cmndinfo->priority = HIGH_PRI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 return 2;
3380 }
3381 memset((char*)scp->sense_buffer,0,16);
3382 scp->sense_buffer[0] = 0x70;
3383 scp->sense_buffer[2] = NOT_READY;
3384 scp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
3385 } else if (service == CACHESERVICE) {
3386 if (ha->status == S_CACHE_UNKNOWN &&
3387 (ha->hdr[t].cluster_type &
3388 CLUSTER_RESERVE_STATE) == CLUSTER_RESERVE_STATE) {
3389 /* bus reset -> force GDT_CLUST_INFO */
3390 ha->hdr[t].cluster_type &= ~CLUSTER_RESERVED;
3391 }
3392 memset((char*)scp->sense_buffer,0,16);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003393 if (ha->status == (u16)S_CACHE_RESERV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 scp->result = (DID_OK << 16) | (RESERVATION_CONFLICT << 1);
3395 } else {
3396 scp->sense_buffer[0] = 0x70;
3397 scp->sense_buffer[2] = NOT_READY;
3398 scp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
3399 }
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02003400 if (!cmndinfo->internal_command) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 ha->dvr.size = sizeof(ha->dvr.eu.sync);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003402 ha->dvr.eu.sync.ionode = ha->hanum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 ha->dvr.eu.sync.service = service;
3404 ha->dvr.eu.sync.status = ha->status;
3405 ha->dvr.eu.sync.info = ha->info;
3406 ha->dvr.eu.sync.hostdrive = t;
3407 if (ha->status >= 0x8000)
3408 gdth_store_event(ha, ES_SYNC, 0, &ha->dvr);
3409 else
3410 gdth_store_event(ha, ES_SYNC, service, &ha->dvr);
3411 }
3412 } else {
3413 /* sense buffer filled from controller firmware (DMA) */
3414 if (ha->status != S_RAW_SCSI || ha->info >= 0x100) {
3415 scp->result = DID_BAD_TARGET << 16;
3416 } else {
3417 scp->result = (DID_OK << 16) | ha->info;
3418 }
3419 }
3420 }
Boaz Harroshf842b642007-10-02 23:16:01 +02003421 if (!cmndinfo->wait_for_completion)
3422 cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 else
3424 return 1;
3425 }
3426
3427 return 0;
3428}
3429
3430static char *async_cache_tab[] = {
3431/* 0*/ "\011\000\002\002\002\004\002\006\004"
3432 "GDT HA %u, service %u, async. status %u/%lu unknown",
3433/* 1*/ "\011\000\002\002\002\004\002\006\004"
3434 "GDT HA %u, service %u, async. status %u/%lu unknown",
3435/* 2*/ "\005\000\002\006\004"
3436 "GDT HA %u, Host Drive %lu not ready",
3437/* 3*/ "\005\000\002\006\004"
3438 "GDT HA %u, Host Drive %lu: REASSIGN not successful and/or data error on reassigned blocks. Drive may crash in the future and should be replaced",
3439/* 4*/ "\005\000\002\006\004"
3440 "GDT HA %u, mirror update on Host Drive %lu failed",
3441/* 5*/ "\005\000\002\006\004"
3442 "GDT HA %u, Mirror Drive %lu failed",
3443/* 6*/ "\005\000\002\006\004"
3444 "GDT HA %u, Mirror Drive %lu: REASSIGN not successful and/or data error on reassigned blocks. Drive may crash in the future and should be replaced",
3445/* 7*/ "\005\000\002\006\004"
3446 "GDT HA %u, Host Drive %lu write protected",
3447/* 8*/ "\005\000\002\006\004"
3448 "GDT HA %u, media changed in Host Drive %lu",
3449/* 9*/ "\005\000\002\006\004"
3450 "GDT HA %u, Host Drive %lu is offline",
3451/*10*/ "\005\000\002\006\004"
3452 "GDT HA %u, media change of Mirror Drive %lu",
3453/*11*/ "\005\000\002\006\004"
3454 "GDT HA %u, Mirror Drive %lu is write protected",
3455/*12*/ "\005\000\002\006\004"
3456 "GDT HA %u, general error on Host Drive %lu. Please check the devices of this drive!",
3457/*13*/ "\007\000\002\006\002\010\002"
3458 "GDT HA %u, Array Drive %u: Cache Drive %u failed",
3459/*14*/ "\005\000\002\006\002"
3460 "GDT HA %u, Array Drive %u: FAIL state entered",
3461/*15*/ "\005\000\002\006\002"
3462 "GDT HA %u, Array Drive %u: error",
3463/*16*/ "\007\000\002\006\002\010\002"
3464 "GDT HA %u, Array Drive %u: failed drive replaced by Cache Drive %u",
3465/*17*/ "\005\000\002\006\002"
3466 "GDT HA %u, Array Drive %u: parity build failed",
3467/*18*/ "\005\000\002\006\002"
3468 "GDT HA %u, Array Drive %u: drive rebuild failed",
3469/*19*/ "\005\000\002\010\002"
3470 "GDT HA %u, Test of Hot Fix %u failed",
3471/*20*/ "\005\000\002\006\002"
3472 "GDT HA %u, Array Drive %u: drive build finished successfully",
3473/*21*/ "\005\000\002\006\002"
3474 "GDT HA %u, Array Drive %u: drive rebuild finished successfully",
3475/*22*/ "\007\000\002\006\002\010\002"
3476 "GDT HA %u, Array Drive %u: Hot Fix %u activated",
3477/*23*/ "\005\000\002\006\002"
3478 "GDT HA %u, Host Drive %u: processing of i/o aborted due to serious drive error",
3479/*24*/ "\005\000\002\010\002"
3480 "GDT HA %u, mirror update on Cache Drive %u completed",
3481/*25*/ "\005\000\002\010\002"
3482 "GDT HA %u, mirror update on Cache Drive %lu failed",
3483/*26*/ "\005\000\002\006\002"
3484 "GDT HA %u, Array Drive %u: drive rebuild started",
3485/*27*/ "\005\000\002\012\001"
3486 "GDT HA %u, Fault bus %u: SHELF OK detected",
3487/*28*/ "\005\000\002\012\001"
3488 "GDT HA %u, Fault bus %u: SHELF not OK detected",
3489/*29*/ "\007\000\002\012\001\013\001"
3490 "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug started",
3491/*30*/ "\007\000\002\012\001\013\001"
3492 "GDT HA %u, Fault bus %u, ID %u: new disk detected",
3493/*31*/ "\007\000\002\012\001\013\001"
3494 "GDT HA %u, Fault bus %u, ID %u: old disk detected",
3495/*32*/ "\007\000\002\012\001\013\001"
3496 "GDT HA %u, Fault bus %u, ID %u: plugging an active disk is invalid",
3497/*33*/ "\007\000\002\012\001\013\001"
3498 "GDT HA %u, Fault bus %u, ID %u: invalid device detected",
3499/*34*/ "\011\000\002\012\001\013\001\006\004"
3500 "GDT HA %u, Fault bus %u, ID %u: insufficient disk capacity (%lu MB required)",
3501/*35*/ "\007\000\002\012\001\013\001"
3502 "GDT HA %u, Fault bus %u, ID %u: disk write protected",
3503/*36*/ "\007\000\002\012\001\013\001"
3504 "GDT HA %u, Fault bus %u, ID %u: disk not available",
3505/*37*/ "\007\000\002\012\001\006\004"
3506 "GDT HA %u, Fault bus %u: swap detected (%lu)",
3507/*38*/ "\007\000\002\012\001\013\001"
3508 "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug finished successfully",
3509/*39*/ "\007\000\002\012\001\013\001"
3510 "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug aborted due to user Hot Plug",
3511/*40*/ "\007\000\002\012\001\013\001"
3512 "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug aborted",
3513/*41*/ "\007\000\002\012\001\013\001"
3514 "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug for Hot Fix started",
3515/*42*/ "\005\000\002\006\002"
3516 "GDT HA %u, Array Drive %u: drive build started",
3517/*43*/ "\003\000\002"
3518 "GDT HA %u, DRAM parity error detected",
3519/*44*/ "\005\000\002\006\002"
3520 "GDT HA %u, Mirror Drive %u: update started",
3521/*45*/ "\007\000\002\006\002\010\002"
3522 "GDT HA %u, Mirror Drive %u: Hot Fix %u activated",
3523/*46*/ "\005\000\002\006\002"
3524 "GDT HA %u, Array Drive %u: no matching Pool Hot Fix Drive available",
3525/*47*/ "\005\000\002\006\002"
3526 "GDT HA %u, Array Drive %u: Pool Hot Fix Drive available",
3527/*48*/ "\005\000\002\006\002"
3528 "GDT HA %u, Mirror Drive %u: no matching Pool Hot Fix Drive available",
3529/*49*/ "\005\000\002\006\002"
3530 "GDT HA %u, Mirror Drive %u: Pool Hot Fix Drive available",
3531/*50*/ "\007\000\002\012\001\013\001"
3532 "GDT HA %u, SCSI bus %u, ID %u: IGNORE_WIDE_RESIDUE message received",
3533/*51*/ "\005\000\002\006\002"
3534 "GDT HA %u, Array Drive %u: expand started",
3535/*52*/ "\005\000\002\006\002"
3536 "GDT HA %u, Array Drive %u: expand finished successfully",
3537/*53*/ "\005\000\002\006\002"
3538 "GDT HA %u, Array Drive %u: expand failed",
3539/*54*/ "\003\000\002"
3540 "GDT HA %u, CPU temperature critical",
3541/*55*/ "\003\000\002"
3542 "GDT HA %u, CPU temperature OK",
3543/*56*/ "\005\000\002\006\004"
3544 "GDT HA %u, Host drive %lu created",
3545/*57*/ "\005\000\002\006\002"
3546 "GDT HA %u, Array Drive %u: expand restarted",
3547/*58*/ "\005\000\002\006\002"
3548 "GDT HA %u, Array Drive %u: expand stopped",
3549/*59*/ "\005\000\002\010\002"
3550 "GDT HA %u, Mirror Drive %u: drive build quited",
3551/*60*/ "\005\000\002\006\002"
3552 "GDT HA %u, Array Drive %u: parity build quited",
3553/*61*/ "\005\000\002\006\002"
3554 "GDT HA %u, Array Drive %u: drive rebuild quited",
3555/*62*/ "\005\000\002\006\002"
3556 "GDT HA %u, Array Drive %u: parity verify started",
3557/*63*/ "\005\000\002\006\002"
3558 "GDT HA %u, Array Drive %u: parity verify done",
3559/*64*/ "\005\000\002\006\002"
3560 "GDT HA %u, Array Drive %u: parity verify failed",
3561/*65*/ "\005\000\002\006\002"
3562 "GDT HA %u, Array Drive %u: parity error detected",
3563/*66*/ "\005\000\002\006\002"
3564 "GDT HA %u, Array Drive %u: parity verify quited",
3565/*67*/ "\005\000\002\006\002"
3566 "GDT HA %u, Host Drive %u reserved",
3567/*68*/ "\005\000\002\006\002"
3568 "GDT HA %u, Host Drive %u mounted and released",
3569/*69*/ "\005\000\002\006\002"
3570 "GDT HA %u, Host Drive %u released",
3571/*70*/ "\003\000\002"
3572 "GDT HA %u, DRAM error detected and corrected with ECC",
3573/*71*/ "\003\000\002"
3574 "GDT HA %u, Uncorrectable DRAM error detected with ECC",
3575/*72*/ "\011\000\002\012\001\013\001\014\001"
3576 "GDT HA %u, SCSI bus %u, ID %u, LUN %u: reassigning block",
3577/*73*/ "\005\000\002\006\002"
3578 "GDT HA %u, Host drive %u resetted locally",
3579/*74*/ "\005\000\002\006\002"
3580 "GDT HA %u, Host drive %u resetted remotely",
3581/*75*/ "\003\000\002"
3582 "GDT HA %u, async. status 75 unknown",
3583};
3584
3585
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003586static int gdth_async_event(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 gdth_cmd_str *cmdp;
3589 int cmd_index;
3590
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 cmdp= ha->pccb;
3592 TRACE2(("gdth_async_event() ha %d serv %d\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003593 ha->hanum, ha->service));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
3595 if (ha->service == SCREENSERVICE) {
3596 if (ha->status == MSG_REQUEST) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003597 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 gdth_delay(0);
3599 cmdp->Service = SCREENSERVICE;
3600 cmdp->RequestBuffer = SCREEN_CMND;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003601 cmd_index = gdth_get_cmd_index(ha);
3602 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 cmdp->OpCode = GDT_READ;
3604 cmdp->BoardNode = LOCALBOARD;
3605 cmdp->u.screen.reserved = 0;
3606 cmdp->u.screen.su.msg.msg_handle= MSG_INV_HANDLE;
3607 cmdp->u.screen.su.msg.msg_addr = ha->msg_phys;
3608 ha->cmd_offs_dpmem = 0;
3609 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.screen.su.msg.msg_addr)
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003610 + sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 ha->cmd_cnt = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003612 gdth_copy_command(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 if (ha->type == GDT_EISA)
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003614 printk("[EISA slot %d] ",(u16)ha->brd_phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 else if (ha->type == GDT_ISA)
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003616 printk("[DPMEM 0x%4X] ",(u16)ha->brd_phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 else
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003618 printk("[PCI %d/%d] ",(u16)(ha->brd_phys>>8),
3619 (u16)((ha->brd_phys>>3)&0x1f));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003620 gdth_release_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 }
3622
3623 } else {
3624 if (ha->type == GDT_PCIMPR &&
3625 (ha->fw_vers & 0xff) >= 0x1a) {
3626 ha->dvr.size = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003627 ha->dvr.eu.async.ionode = ha->hanum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 ha->dvr.eu.async.status = ha->status;
3629 /* severity and event_string already set! */
3630 } else {
3631 ha->dvr.size = sizeof(ha->dvr.eu.async);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003632 ha->dvr.eu.async.ionode = ha->hanum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 ha->dvr.eu.async.service = ha->service;
3634 ha->dvr.eu.async.status = ha->status;
3635 ha->dvr.eu.async.info = ha->info;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003636 *(u32 *)ha->dvr.eu.async.scsi_coord = ha->info2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 }
3638 gdth_store_event( ha, ES_ASYNC, ha->service, &ha->dvr );
3639 gdth_log_event( &ha->dvr, NULL );
3640
3641 /* new host drive from expand? */
3642 if (ha->service == CACHESERVICE && ha->status == 56) {
3643 TRACE2(("gdth_async_event(): new host drive %d created\n",
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003644 (u16)ha->info));
3645 /* gdth_analyse_hdrive(hanum, (u16)ha->info); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 }
3647 }
3648 return 1;
3649}
3650
3651static void gdth_log_event(gdth_evt_data *dvr, char *buffer)
3652{
3653 gdth_stackframe stack;
3654 char *f = NULL;
3655 int i,j;
3656
3657 TRACE2(("gdth_log_event()\n"));
3658 if (dvr->size == 0) {
3659 if (buffer == NULL) {
3660 printk("Adapter %d: %s\n",dvr->eu.async.ionode,dvr->event_string);
3661 } else {
3662 sprintf(buffer,"Adapter %d: %s\n",
3663 dvr->eu.async.ionode,dvr->event_string);
3664 }
3665 } else if (dvr->eu.async.service == CACHESERVICE &&
3666 INDEX_OK(dvr->eu.async.status, async_cache_tab)) {
3667 TRACE2(("GDT: Async. event cache service, event no.: %d\n",
3668 dvr->eu.async.status));
3669
3670 f = async_cache_tab[dvr->eu.async.status];
3671
3672 /* i: parameter to push, j: stack element to fill */
3673 for (j=0,i=1; i < f[0]; i+=2) {
3674 switch (f[i+1]) {
3675 case 4:
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003676 stack.b[j++] = *(u32*)&dvr->eu.stream[(int)f[i]];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 break;
3678 case 2:
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003679 stack.b[j++] = *(u16*)&dvr->eu.stream[(int)f[i]];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 break;
3681 case 1:
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003682 stack.b[j++] = *(u8*)&dvr->eu.stream[(int)f[i]];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 break;
3684 default:
3685 break;
3686 }
3687 }
3688
3689 if (buffer == NULL) {
3690 printk(&f[(int)f[0]],stack);
3691 printk("\n");
3692 } else {
3693 sprintf(buffer,&f[(int)f[0]],stack);
3694 }
3695
3696 } else {
3697 if (buffer == NULL) {
3698 printk("GDT HA %u, Unknown async. event service %d event no. %d\n",
3699 dvr->eu.async.ionode,dvr->eu.async.service,dvr->eu.async.status);
3700 } else {
3701 sprintf(buffer,"GDT HA %u, Unknown async. event service %d event no. %d",
3702 dvr->eu.async.ionode,dvr->eu.async.service,dvr->eu.async.status);
3703 }
3704 }
3705}
3706
3707#ifdef GDTH_STATISTICS
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003708static u8 gdth_timer_running;
James Bottomley2d6f0d02008-05-04 22:34:49 -05003709
Kees Cook79325892017-10-11 17:17:42 -07003710static void gdth_timeout(struct timer_list *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003712 u32 i;
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02003713 struct scsi_cmnd *nscp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 gdth_ha_str *ha;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003715 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
James Bottomley2d6f0d02008-05-04 22:34:49 -05003717 if(unlikely(list_empty(&gdth_instances))) {
3718 gdth_timer_running = 0;
3719 return;
3720 }
Boaz Harroshb31ddd32008-02-27 15:27:16 -08003721
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02003722 ha = list_first_entry(&gdth_instances, gdth_ha_str, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 spin_lock_irqsave(&ha->smp_lock, flags);
3724
3725 for (act_stats=0,i=0; i<GDTH_MAXCMDS; ++i)
3726 if (ha->cmd_tab[i].cmnd != UNUSED_CMND)
3727 ++act_stats;
3728
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02003729 for (act_rq=0,
3730 nscp=ha->req_first; nscp; nscp=(struct scsi_cmnd*)nscp->SCp.ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 ++act_rq;
3732
3733 TRACE2(("gdth_to(): ints %d, ios %d, act_stats %d, act_rq %d\n",
3734 act_ints, act_ios, act_stats, act_rq));
3735 act_ints = act_ios = 0;
3736
3737 gdth_timer.expires = jiffies + 30 * HZ;
3738 add_timer(&gdth_timer);
3739 spin_unlock_irqrestore(&ha->smp_lock, flags);
3740}
James Bottomley2d6f0d02008-05-04 22:34:49 -05003741
3742static void gdth_timer_init(void)
3743{
3744 if (gdth_timer_running)
3745 return;
3746 gdth_timer_running = 1;
3747 TRACE2(("gdth_detect(): Initializing timer !\n"));
3748 gdth_timer.expires = jiffies + HZ;
James Bottomley2d6f0d02008-05-04 22:34:49 -05003749 add_timer(&gdth_timer);
3750}
3751#else
3752static inline void gdth_timer_init(void)
3753{
3754}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755#endif
3756
8e879042005-04-17 15:28:39 -05003757static void __init internal_setup(char *str,int *ints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758{
3759 int i, argc;
3760 char *cur_str, *argv;
3761
3762 TRACE2(("internal_setup() str %s ints[0] %d\n",
3763 str ? str:"NULL", ints ? ints[0]:0));
3764
3765 /* read irq[] from ints[] */
3766 if (ints) {
3767 argc = ints[0];
3768 if (argc > 0) {
3769 if (argc > MAXHA)
3770 argc = MAXHA;
3771 for (i = 0; i < argc; ++i)
3772 irq[i] = ints[i+1];
3773 }
3774 }
3775
3776 /* analyse string */
3777 argv = str;
3778 while (argv && (cur_str = strchr(argv, ':'))) {
3779 int val = 0, c = *++cur_str;
3780
3781 if (c == 'n' || c == 'N')
3782 val = 0;
3783 else if (c == 'y' || c == 'Y')
3784 val = 1;
3785 else
3786 val = (int)simple_strtoul(cur_str, NULL, 0);
3787
3788 if (!strncmp(argv, "disable:", 8))
3789 disable = val;
3790 else if (!strncmp(argv, "reserve_mode:", 13))
3791 reserve_mode = val;
3792 else if (!strncmp(argv, "reverse_scan:", 13))
3793 reverse_scan = val;
3794 else if (!strncmp(argv, "hdr_channel:", 12))
3795 hdr_channel = val;
3796 else if (!strncmp(argv, "max_ids:", 8))
3797 max_ids = val;
3798 else if (!strncmp(argv, "rescan:", 7))
3799 rescan = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 else if (!strncmp(argv, "shared_access:", 14))
3801 shared_access = val;
3802 else if (!strncmp(argv, "probe_eisa_isa:", 15))
3803 probe_eisa_isa = val;
3804 else if (!strncmp(argv, "reserve_list:", 13)) {
3805 reserve_list[0] = val;
3806 for (i = 1; i < MAX_RES_ARGS; i++) {
3807 cur_str = strchr(cur_str, ',');
3808 if (!cur_str)
3809 break;
3810 if (!isdigit((int)*++cur_str)) {
3811 --cur_str;
3812 break;
3813 }
3814 reserve_list[i] =
3815 (int)simple_strtoul(cur_str, NULL, 0);
3816 }
3817 if (!cur_str)
3818 break;
3819 argv = ++cur_str;
3820 continue;
3821 }
3822
3823 if ((argv = strchr(argv, ',')))
3824 ++argv;
3825 }
3826}
3827
3828int __init option_setup(char *str)
3829{
3830 int ints[MAXHA];
3831 char *cur = str;
3832 int i = 1;
3833
3834 TRACE2(("option_setup() str %s\n", str ? str:"NULL"));
3835
Roel Kluin6ce00ca2010-03-11 14:09:35 -08003836 while (cur && isdigit(*cur) && i < MAXHA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 ints[i++] = simple_strtoul(cur, NULL, 0);
3838 if ((cur = strchr(cur, ',')) != NULL) cur++;
3839 }
3840
3841 ints[0] = i - 1;
3842 internal_setup(cur, ints);
3843 return 1;
3844}
3845
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003846static const char *gdth_ctr_name(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 TRACE2(("gdth_ctr_name()\n"));
3849
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 if (ha->type == GDT_EISA) {
3851 switch (ha->stype) {
3852 case GDT3_ID:
3853 return("GDT3000/3020");
3854 case GDT3A_ID:
3855 return("GDT3000A/3020A/3050A");
3856 case GDT3B_ID:
3857 return("GDT3000B/3010A");
3858 }
3859 } else if (ha->type == GDT_ISA) {
3860 return("GDT2000/2020");
3861 } else if (ha->type == GDT_PCI) {
Jeff Garzik8e9a8a02007-07-17 05:25:17 -04003862 switch (ha->pdev->device) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 case PCI_DEVICE_ID_VORTEX_GDT60x0:
3864 return("GDT6000/6020/6050");
3865 case PCI_DEVICE_ID_VORTEX_GDT6000B:
3866 return("GDT6000B/6010");
3867 }
3868 }
3869 /* new controllers (GDT_PCINEW, GDT_PCIMPR, ..) use board_info IOCTL! */
3870
3871 return("");
3872}
3873
8e879042005-04-17 15:28:39 -05003874static const char *gdth_info(struct Scsi_Host *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875{
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003876 gdth_ha_str *ha = shost_priv(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877
3878 TRACE2(("gdth_info()\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 return ((const char *)ha->binfo.type_string);
3880}
3881
Jens Axboe242f9dc2008-09-14 05:55:09 -07003882static enum blk_eh_timer_return gdth_timed_out(struct scsi_cmnd *scp)
3883{
3884 gdth_ha_str *ha = shost_priv(scp->device->host);
3885 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003886 u8 b, t;
3887 unsigned long flags;
Christoph Hellwig66005932018-05-29 15:52:29 +02003888 enum blk_eh_timer_return retval = BLK_EH_DONE;
Jens Axboe242f9dc2008-09-14 05:55:09 -07003889
3890 TRACE(("%s() cmd 0x%x\n", scp->cmnd[0], __func__));
3891 b = scp->device->channel;
3892 t = scp->device->id;
3893
3894 /*
3895 * We don't really honor the command timeout, but we try to
3896 * honor 6 times of the actual command timeout! So reset the
3897 * timer if this is less than 6th timeout on this command!
3898 */
3899 if (++cmndinfo->timeout_count < 6)
3900 retval = BLK_EH_RESET_TIMER;
3901
3902 /* Reset the timeout if it is locked IO */
3903 spin_lock_irqsave(&ha->smp_lock, flags);
3904 if ((b != ha->virt_bus && ha->raw[BUS_L2P(ha, b)].lock) ||
3905 (b == ha->virt_bus && t < MAX_HDRIVES && ha->hdr[t].lock)) {
3906 TRACE2(("%s(): locked IO, reset timeout\n", __func__));
3907 retval = BLK_EH_RESET_TIMER;
3908 }
3909 spin_unlock_irqrestore(&ha->smp_lock, flags);
3910
3911 return retval;
3912}
3913
3914
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02003915static int gdth_eh_bus_reset(struct scsi_cmnd *scp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916{
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003917 gdth_ha_str *ha = shost_priv(scp->device->host);
3918 int i;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003919 unsigned long flags;
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02003920 struct scsi_cmnd *cmnd;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003921 u8 b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922
3923 TRACE2(("gdth_eh_bus_reset()\n"));
3924
Christoph Hellwig52759e62007-10-02 22:59:53 +02003925 b = scp->device->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926
3927 /* clear command tab */
3928 spin_lock_irqsave(&ha->smp_lock, flags);
3929 for (i = 0; i < GDTH_MAXCMDS; ++i) {
3930 cmnd = ha->cmd_tab[i].cmnd;
3931 if (!SPECIAL_SCP(cmnd) && cmnd->device->channel == b)
3932 ha->cmd_tab[i].cmnd = UNUSED_CMND;
3933 }
3934 spin_unlock_irqrestore(&ha->smp_lock, flags);
3935
3936 if (b == ha->virt_bus) {
3937 /* host drives */
3938 for (i = 0; i < MAX_HDRIVES; ++i) {
3939 if (ha->hdr[i].present) {
3940 spin_lock_irqsave(&ha->smp_lock, flags);
3941 gdth_polling = TRUE;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003942 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 gdth_delay(0);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003944 if (gdth_internal_cmd(ha, CACHESERVICE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 GDT_CLUST_RESET, i, 0, 0))
3946 ha->hdr[i].cluster_type &= ~CLUSTER_RESERVED;
3947 gdth_polling = FALSE;
3948 spin_unlock_irqrestore(&ha->smp_lock, flags);
3949 }
3950 }
3951 } else {
3952 /* raw devices */
3953 spin_lock_irqsave(&ha->smp_lock, flags);
3954 for (i = 0; i < MAXID; ++i)
3955 ha->raw[BUS_L2P(ha,b)].io_cnt[i] = 0;
3956 gdth_polling = TRUE;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003957 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 gdth_delay(0);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003959 gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_RESET_BUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 BUS_L2P(ha,b), 0, 0);
3961 gdth_polling = FALSE;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07003962 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 }
3964 return SUCCESS;
3965}
3966
8e879042005-04-17 15:28:39 -05003967static int gdth_bios_param(struct scsi_device *sdev,struct block_device *bdev,sector_t cap,int *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003969 u8 b, t;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003970 gdth_ha_str *ha = shost_priv(sdev->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 struct scsi_device *sd;
3972 unsigned capacity;
3973
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 sd = sdev;
3975 capacity = cap;
Christoph Hellwig52759e62007-10-02 22:59:53 +02003976 b = sd->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 t = sd->id;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003978 TRACE2(("gdth_bios_param() ha %d bus %d target %d\n", ha->hanum, b, t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979
3980 if (b != ha->virt_bus || ha->hdr[t].heads == 0) {
3981 /* raw device or host drive without mapping information */
3982 TRACE2(("Evaluate mapping\n"));
3983 gdth_eval_mapping(capacity,&ip[2],&ip[0],&ip[1]);
3984 } else {
3985 ip[0] = ha->hdr[t].heads;
3986 ip[1] = ha->hdr[t].secs;
3987 ip[2] = capacity / ip[0] / ip[1];
3988 }
3989
3990 TRACE2(("gdth_bios_param(): %d heads, %d secs, %d cyls\n",
3991 ip[0],ip[1],ip[2]));
3992 return 0;
3993}
3994
3995
Jeff Garzikf2812332010-11-16 02:10:29 -05003996static int gdth_queuecommand_lck(struct scsi_cmnd *scp,
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02003997 void (*done)(struct scsi_cmnd *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998{
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003999 gdth_ha_str *ha = shost_priv(scp->device->host);
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02004000 struct gdth_cmndinfo *cmndinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001
4002 TRACE(("gdth_queuecommand() cmd 0x%x\n", scp->cmnd[0]));
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02004003
4004 cmndinfo = gdth_get_cmndinfo(ha);
4005 BUG_ON(!cmndinfo);
4006
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02004007 scp->scsi_done = done;
Jens Axboe242f9dc2008-09-14 05:55:09 -07004008 cmndinfo->timeout_count = 0;
Boaz Harroshf842b642007-10-02 23:16:01 +02004009 cmndinfo->priority = DEFAULT_PRI;
Boaz Harrosh3892d882007-10-02 23:18:03 +02004010
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02004011 return __gdth_queuecommand(ha, scp, cmndinfo);
4012}
4013
Jeff Garzikf2812332010-11-16 02:10:29 -05004014static DEF_SCSI_QCMD(gdth_queuecommand)
4015
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02004016static int __gdth_queuecommand(gdth_ha_str *ha, struct scsi_cmnd *scp,
4017 struct gdth_cmndinfo *cmndinfo)
4018{
4019 scp->host_scribble = (unsigned char *)cmndinfo;
Boaz Harroshf842b642007-10-02 23:16:01 +02004020 cmndinfo->wait_for_completion = 1;
4021 cmndinfo->phase = -1;
4022 cmndinfo->OpCode = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024#ifdef GDTH_STATISTICS
4025 ++act_ios;
4026#endif
4027
Boaz Harroshf842b642007-10-02 23:16:01 +02004028 gdth_putq(ha, scp, cmndinfo->priority);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004029 gdth_next(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 return 0;
4031}
4032
4033
4034static int gdth_open(struct inode *inode, struct file *filep)
4035{
4036 gdth_ha_str *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037
Arnd Bergmannc45d15d2010-06-02 14:28:52 +02004038 mutex_lock(&gdth_mutex);
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004039 list_for_each_entry(ha, &gdth_instances, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 if (!ha->sdev)
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004041 ha->sdev = scsi_get_host_dev(ha->shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 }
Arnd Bergmannc45d15d2010-06-02 14:28:52 +02004043 mutex_unlock(&gdth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044
4045 TRACE(("gdth_open()\n"));
4046 return 0;
4047}
4048
4049static int gdth_close(struct inode *inode, struct file *filep)
4050{
4051 TRACE(("gdth_close()\n"));
4052 return 0;
4053}
4054
4055static int ioc_event(void __user *arg)
4056{
4057 gdth_ioctl_event evt;
4058 gdth_ha_str *ha;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004059 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004061 if (copy_from_user(&evt, arg, sizeof(gdth_ioctl_event)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004063 ha = gdth_find_ha(evt.ionode);
4064 if (!ha)
4065 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066
4067 if (evt.erase == 0xff) {
4068 if (evt.event.event_source == ES_TEST)
4069 evt.event.event_data.size=sizeof(evt.event.event_data.eu.test);
4070 else if (evt.event.event_source == ES_DRIVER)
4071 evt.event.event_data.size=sizeof(evt.event.event_data.eu.driver);
4072 else if (evt.event.event_source == ES_SYNC)
4073 evt.event.event_data.size=sizeof(evt.event.event_data.eu.sync);
4074 else
4075 evt.event.event_data.size=sizeof(evt.event.event_data.eu.async);
4076 spin_lock_irqsave(&ha->smp_lock, flags);
4077 gdth_store_event(ha, evt.event.event_source, evt.event.event_idx,
4078 &evt.event.event_data);
4079 spin_unlock_irqrestore(&ha->smp_lock, flags);
4080 } else if (evt.erase == 0xfe) {
4081 gdth_clear_events();
4082 } else if (evt.erase == 0) {
4083 evt.handle = gdth_read_event(ha, evt.handle, &evt.event);
4084 } else {
4085 gdth_readapp_event(ha, evt.erase, &evt.event);
4086 }
4087 if (copy_to_user(arg, &evt, sizeof(gdth_ioctl_event)))
4088 return -EFAULT;
4089 return 0;
4090}
4091
4092static int ioc_lockdrv(void __user *arg)
4093{
4094 gdth_ioctl_lockdrv ldrv;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004095 u8 i, j;
4096 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 gdth_ha_str *ha;
4098
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004099 if (copy_from_user(&ldrv, arg, sizeof(gdth_ioctl_lockdrv)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004101 ha = gdth_find_ha(ldrv.ionode);
4102 if (!ha)
4103 return -EFAULT;
4104
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 for (i = 0; i < ldrv.drive_cnt && i < MAX_HDRIVES; ++i) {
4106 j = ldrv.drives[i];
4107 if (j >= MAX_HDRIVES || !ha->hdr[j].present)
4108 continue;
4109 if (ldrv.lock) {
4110 spin_lock_irqsave(&ha->smp_lock, flags);
4111 ha->hdr[j].lock = 1;
4112 spin_unlock_irqrestore(&ha->smp_lock, flags);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004113 gdth_wait_completion(ha, ha->bus_cnt, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 } else {
4115 spin_lock_irqsave(&ha->smp_lock, flags);
4116 ha->hdr[j].lock = 0;
4117 spin_unlock_irqrestore(&ha->smp_lock, flags);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004118 gdth_next(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 }
4120 }
4121 return 0;
4122}
4123
4124static int ioc_resetdrv(void __user *arg, char *cmnd)
4125{
4126 gdth_ioctl_reset res;
4127 gdth_cmd_str cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 gdth_ha_str *ha;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004129 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130
4131 if (copy_from_user(&res, arg, sizeof(gdth_ioctl_reset)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004132 res.number >= MAX_HDRIVES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004134 ha = gdth_find_ha(res.ionode);
4135 if (!ha)
4136 return -EFAULT;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004137
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 if (!ha->hdr[res.number].present)
4139 return 0;
4140 memset(&cmd, 0, sizeof(gdth_cmd_str));
4141 cmd.Service = CACHESERVICE;
4142 cmd.OpCode = GDT_CLUST_RESET;
4143 if (ha->cache_feat & GDT_64BIT)
4144 cmd.u.cache64.DeviceNo = res.number;
4145 else
4146 cmd.u.cache.DeviceNo = res.number;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004147
4148 rval = __gdth_execute(ha->sdev, &cmd, cmnd, 30, NULL);
4149 if (rval < 0)
4150 return rval;
4151 res.status = rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152
4153 if (copy_to_user(arg, &res, sizeof(gdth_ioctl_reset)))
4154 return -EFAULT;
4155 return 0;
4156}
4157
4158static int ioc_general(void __user *arg, char *cmnd)
4159{
4160 gdth_ioctl_general gen;
4161 char *buf = NULL;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004162 u64 paddr;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004163 gdth_ha_str *ha;
4164 int rval;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004165
4166 if (copy_from_user(&gen, arg, sizeof(gdth_ioctl_general)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004168 ha = gdth_find_ha(gen.ionode);
4169 if (!ha)
4170 return -EFAULT;
Dan Carpenterf63ae562010-10-08 09:03:07 +02004171
4172 if (gen.data_len > INT_MAX)
4173 return -EINVAL;
4174 if (gen.sense_len > INT_MAX)
4175 return -EINVAL;
4176 if (gen.data_len + gen.sense_len > INT_MAX)
4177 return -EINVAL;
4178
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 if (gen.data_len + gen.sense_len != 0) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004180 if (!(buf = gdth_ioctl_alloc(ha, gen.data_len + gen.sense_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 FALSE, &paddr)))
4182 return -EFAULT;
4183 if (copy_from_user(buf, arg + sizeof(gdth_ioctl_general),
4184 gen.data_len + gen.sense_len)) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004185 gdth_ioctl_free(ha, gen.data_len+gen.sense_len, buf, paddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 return -EFAULT;
4187 }
4188
4189 if (gen.command.OpCode == GDT_IOCTL) {
4190 gen.command.u.ioctl.p_param = paddr;
4191 } else if (gen.command.Service == CACHESERVICE) {
4192 if (ha->cache_feat & GDT_64BIT) {
4193 /* copy elements from 32-bit IOCTL structure */
4194 gen.command.u.cache64.BlockCnt = gen.command.u.cache.BlockCnt;
4195 gen.command.u.cache64.BlockNo = gen.command.u.cache.BlockNo;
4196 gen.command.u.cache64.DeviceNo = gen.command.u.cache.DeviceNo;
4197 /* addresses */
4198 if (ha->cache_feat & SCATTER_GATHER) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004199 gen.command.u.cache64.DestAddr = (u64)-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 gen.command.u.cache64.sg_canz = 1;
4201 gen.command.u.cache64.sg_lst[0].sg_ptr = paddr;
4202 gen.command.u.cache64.sg_lst[0].sg_len = gen.data_len;
4203 gen.command.u.cache64.sg_lst[1].sg_len = 0;
4204 } else {
4205 gen.command.u.cache64.DestAddr = paddr;
4206 gen.command.u.cache64.sg_canz = 0;
4207 }
4208 } else {
4209 if (ha->cache_feat & SCATTER_GATHER) {
4210 gen.command.u.cache.DestAddr = 0xffffffff;
4211 gen.command.u.cache.sg_canz = 1;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004212 gen.command.u.cache.sg_lst[0].sg_ptr = (u32)paddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 gen.command.u.cache.sg_lst[0].sg_len = gen.data_len;
4214 gen.command.u.cache.sg_lst[1].sg_len = 0;
4215 } else {
4216 gen.command.u.cache.DestAddr = paddr;
4217 gen.command.u.cache.sg_canz = 0;
4218 }
4219 }
4220 } else if (gen.command.Service == SCSIRAWSERVICE) {
4221 if (ha->raw_feat & GDT_64BIT) {
4222 /* copy elements from 32-bit IOCTL structure */
4223 char cmd[16];
4224 gen.command.u.raw64.sense_len = gen.command.u.raw.sense_len;
4225 gen.command.u.raw64.bus = gen.command.u.raw.bus;
4226 gen.command.u.raw64.lun = gen.command.u.raw.lun;
4227 gen.command.u.raw64.target = gen.command.u.raw.target;
4228 memcpy(cmd, gen.command.u.raw.cmd, 16);
4229 memcpy(gen.command.u.raw64.cmd, cmd, 16);
4230 gen.command.u.raw64.clen = gen.command.u.raw.clen;
4231 gen.command.u.raw64.sdlen = gen.command.u.raw.sdlen;
4232 gen.command.u.raw64.direction = gen.command.u.raw.direction;
4233 /* addresses */
4234 if (ha->raw_feat & SCATTER_GATHER) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004235 gen.command.u.raw64.sdata = (u64)-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 gen.command.u.raw64.sg_ranz = 1;
4237 gen.command.u.raw64.sg_lst[0].sg_ptr = paddr;
4238 gen.command.u.raw64.sg_lst[0].sg_len = gen.data_len;
4239 gen.command.u.raw64.sg_lst[1].sg_len = 0;
4240 } else {
4241 gen.command.u.raw64.sdata = paddr;
4242 gen.command.u.raw64.sg_ranz = 0;
4243 }
4244 gen.command.u.raw64.sense_data = paddr + gen.data_len;
4245 } else {
4246 if (ha->raw_feat & SCATTER_GATHER) {
4247 gen.command.u.raw.sdata = 0xffffffff;
4248 gen.command.u.raw.sg_ranz = 1;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004249 gen.command.u.raw.sg_lst[0].sg_ptr = (u32)paddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 gen.command.u.raw.sg_lst[0].sg_len = gen.data_len;
4251 gen.command.u.raw.sg_lst[1].sg_len = 0;
4252 } else {
4253 gen.command.u.raw.sdata = paddr;
4254 gen.command.u.raw.sg_ranz = 0;
4255 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004256 gen.command.u.raw.sense_data = (u32)paddr + gen.data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 }
4258 } else {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004259 gdth_ioctl_free(ha, gen.data_len+gen.sense_len, buf, paddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 return -EFAULT;
4261 }
4262 }
4263
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004264 rval = __gdth_execute(ha->sdev, &gen.command, cmnd, gen.timeout, &gen.info);
Julia Lawall5c100072010-12-29 15:57:54 +01004265 if (rval < 0) {
4266 gdth_ioctl_free(ha, gen.data_len+gen.sense_len, buf, paddr);
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004267 return rval;
Julia Lawall5c100072010-12-29 15:57:54 +01004268 }
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004269 gen.status = rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270
4271 if (copy_to_user(arg + sizeof(gdth_ioctl_general), buf,
4272 gen.data_len + gen.sense_len)) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004273 gdth_ioctl_free(ha, gen.data_len+gen.sense_len, buf, paddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 return -EFAULT;
4275 }
4276 if (copy_to_user(arg, &gen,
4277 sizeof(gdth_ioctl_general) - sizeof(gdth_cmd_str))) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004278 gdth_ioctl_free(ha, gen.data_len+gen.sense_len, buf, paddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 return -EFAULT;
4280 }
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004281 gdth_ioctl_free(ha, gen.data_len+gen.sense_len, buf, paddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 return 0;
4283}
4284
4285static int ioc_hdrlist(void __user *arg, char *cmnd)
4286{
4287 gdth_ioctl_rescan *rsc;
4288 gdth_cmd_str *cmd;
4289 gdth_ha_str *ha;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004290 u8 i;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004291 int rc = -ENOMEM;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004292 u32 cluster_type = 0;
4293
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 rsc = kmalloc(sizeof(*rsc), GFP_KERNEL);
4295 cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
4296 if (!rsc || !cmd)
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004297 goto free_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298
4299 if (copy_from_user(rsc, arg, sizeof(gdth_ioctl_rescan)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004300 (NULL == (ha = gdth_find_ha(rsc->ionode)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 rc = -EFAULT;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004302 goto free_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 memset(cmd, 0, sizeof(gdth_cmd_str));
4305
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 for (i = 0; i < MAX_HDRIVES; ++i) {
4307 if (!ha->hdr[i].present) {
4308 rsc->hdr_list[i].bus = 0xff;
4309 continue;
4310 }
4311 rsc->hdr_list[i].bus = ha->virt_bus;
4312 rsc->hdr_list[i].target = i;
4313 rsc->hdr_list[i].lun = 0;
4314 rsc->hdr_list[i].cluster_type = ha->hdr[i].cluster_type;
4315 if (ha->hdr[i].cluster_type & CLUSTER_DRIVE) {
4316 cmd->Service = CACHESERVICE;
4317 cmd->OpCode = GDT_CLUST_INFO;
4318 if (ha->cache_feat & GDT_64BIT)
4319 cmd->u.cache64.DeviceNo = i;
4320 else
4321 cmd->u.cache.DeviceNo = i;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004322 if (__gdth_execute(ha->sdev, cmd, cmnd, 30, &cluster_type) == S_OK)
4323 rsc->hdr_list[i].cluster_type = cluster_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 }
4325 }
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004326
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 if (copy_to_user(arg, rsc, sizeof(gdth_ioctl_rescan)))
4328 rc = -EFAULT;
4329 else
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004330 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331
4332free_fail:
4333 kfree(rsc);
4334 kfree(cmd);
4335 return rc;
4336}
4337
4338static int ioc_rescan(void __user *arg, char *cmnd)
4339{
4340 gdth_ioctl_rescan *rsc;
4341 gdth_cmd_str *cmd;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004342 u16 i, status, hdr_cnt;
4343 u32 info;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004344 int cyls, hds, secs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 int rc = -ENOMEM;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004346 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 gdth_ha_str *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348
4349 rsc = kmalloc(sizeof(*rsc), GFP_KERNEL);
4350 cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
4351 if (!cmd || !rsc)
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004352 goto free_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353
4354 if (copy_from_user(rsc, arg, sizeof(gdth_ioctl_rescan)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004355 (NULL == (ha = gdth_find_ha(rsc->ionode)))) {
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004356 rc = -EFAULT;
4357 goto free_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 memset(cmd, 0, sizeof(gdth_cmd_str));
4360
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 if (rsc->flag == 0) {
4362 /* old method: re-init. cache service */
4363 cmd->Service = CACHESERVICE;
4364 if (ha->cache_feat & GDT_64BIT) {
4365 cmd->OpCode = GDT_X_INIT_HOST;
4366 cmd->u.cache64.DeviceNo = LINUX_OS;
4367 } else {
4368 cmd->OpCode = GDT_INIT;
4369 cmd->u.cache.DeviceNo = LINUX_OS;
4370 }
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004371
4372 status = __gdth_execute(ha->sdev, cmd, cmnd, 30, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 i = 0;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004374 hdr_cnt = (status == S_OK ? (u16)info : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 } else {
4376 i = rsc->hdr_no;
4377 hdr_cnt = i + 1;
4378 }
4379
4380 for (; i < hdr_cnt && i < MAX_HDRIVES; ++i) {
4381 cmd->Service = CACHESERVICE;
4382 cmd->OpCode = GDT_INFO;
4383 if (ha->cache_feat & GDT_64BIT)
4384 cmd->u.cache64.DeviceNo = i;
4385 else
4386 cmd->u.cache.DeviceNo = i;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004387
4388 status = __gdth_execute(ha->sdev, cmd, cmnd, 30, &info);
4389
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 spin_lock_irqsave(&ha->smp_lock, flags);
4391 rsc->hdr_list[i].bus = ha->virt_bus;
4392 rsc->hdr_list[i].target = i;
4393 rsc->hdr_list[i].lun = 0;
4394 if (status != S_OK) {
4395 ha->hdr[i].present = FALSE;
4396 } else {
4397 ha->hdr[i].present = TRUE;
4398 ha->hdr[i].size = info;
4399 /* evaluate mapping */
4400 ha->hdr[i].size &= ~SECS32;
4401 gdth_eval_mapping(ha->hdr[i].size,&cyls,&hds,&secs);
4402 ha->hdr[i].heads = hds;
4403 ha->hdr[i].secs = secs;
4404 /* round size */
4405 ha->hdr[i].size = cyls * hds * secs;
4406 }
4407 spin_unlock_irqrestore(&ha->smp_lock, flags);
4408 if (status != S_OK)
4409 continue;
4410
4411 /* extended info, if GDT_64BIT, for drives > 2 TB */
4412 /* but we need ha->info2, not yet stored in scp->SCp */
4413
4414 /* devtype, cluster info, R/W attribs */
4415 cmd->Service = CACHESERVICE;
4416 cmd->OpCode = GDT_DEVTYPE;
4417 if (ha->cache_feat & GDT_64BIT)
4418 cmd->u.cache64.DeviceNo = i;
4419 else
4420 cmd->u.cache.DeviceNo = i;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004421
4422 status = __gdth_execute(ha->sdev, cmd, cmnd, 30, &info);
4423
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 spin_lock_irqsave(&ha->smp_lock, flags);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004425 ha->hdr[i].devtype = (status == S_OK ? (u16)info : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 spin_unlock_irqrestore(&ha->smp_lock, flags);
4427
4428 cmd->Service = CACHESERVICE;
4429 cmd->OpCode = GDT_CLUST_INFO;
4430 if (ha->cache_feat & GDT_64BIT)
4431 cmd->u.cache64.DeviceNo = i;
4432 else
4433 cmd->u.cache.DeviceNo = i;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004434
4435 status = __gdth_execute(ha->sdev, cmd, cmnd, 30, &info);
4436
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 spin_lock_irqsave(&ha->smp_lock, flags);
4438 ha->hdr[i].cluster_type =
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004439 ((status == S_OK && !shared_access) ? (u16)info : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 spin_unlock_irqrestore(&ha->smp_lock, flags);
4441 rsc->hdr_list[i].cluster_type = ha->hdr[i].cluster_type;
4442
4443 cmd->Service = CACHESERVICE;
4444 cmd->OpCode = GDT_RW_ATTRIBS;
4445 if (ha->cache_feat & GDT_64BIT)
4446 cmd->u.cache64.DeviceNo = i;
4447 else
4448 cmd->u.cache.DeviceNo = i;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004449
4450 status = __gdth_execute(ha->sdev, cmd, cmnd, 30, &info);
4451
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 spin_lock_irqsave(&ha->smp_lock, flags);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004453 ha->hdr[i].rw_attribs = (status == S_OK ? (u16)info : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 spin_unlock_irqrestore(&ha->smp_lock, flags);
4455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456
4457 if (copy_to_user(arg, rsc, sizeof(gdth_ioctl_rescan)))
4458 rc = -EFAULT;
4459 else
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004460 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461
4462free_fail:
4463 kfree(rsc);
4464 kfree(cmd);
4465 return rc;
4466}
4467
Arnd Bergmannf4927c42010-04-27 00:24:01 +02004468static int gdth_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469{
4470 gdth_ha_str *ha;
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02004471 struct scsi_cmnd *scp;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004472 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 char cmnd[MAX_COMMAND_SIZE];
4474 void __user *argp = (void __user *)arg;
4475
4476 memset(cmnd, 0xff, 12);
4477
4478 TRACE(("gdth_ioctl() cmd 0x%x\n", cmd));
4479
4480 switch (cmd) {
4481 case GDTIOCTL_CTRCNT:
4482 {
4483 int cnt = gdth_ctr_count;
4484 if (put_user(cnt, (int __user *)argp))
4485 return -EFAULT;
4486 break;
4487 }
4488
4489 case GDTIOCTL_DRVERS:
4490 {
4491 int ver = (GDTH_VERSION<<8) | GDTH_SUBVERSION;
4492 if (put_user(ver, (int __user *)argp))
4493 return -EFAULT;
4494 break;
4495 }
4496
4497 case GDTIOCTL_OSVERS:
4498 {
4499 gdth_ioctl_osvers osv;
4500
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004501 osv.version = (u8)(LINUX_VERSION_CODE >> 16);
4502 osv.subversion = (u8)(LINUX_VERSION_CODE >> 8);
4503 osv.revision = (u16)(LINUX_VERSION_CODE & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 if (copy_to_user(argp, &osv, sizeof(gdth_ioctl_osvers)))
4505 return -EFAULT;
4506 break;
4507 }
4508
4509 case GDTIOCTL_CTRTYPE:
4510 {
4511 gdth_ioctl_ctrtype ctrt;
4512
4513 if (copy_from_user(&ctrt, argp, sizeof(gdth_ioctl_ctrtype)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004514 (NULL == (ha = gdth_find_ha(ctrt.ionode))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004516
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 if (ha->type == GDT_ISA || ha->type == GDT_EISA) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004518 ctrt.type = (u8)((ha->stype>>20) - 0x10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 } else {
4520 if (ha->type != GDT_PCIMPR) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004521 ctrt.type = (u8)((ha->stype<<4) + 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 } else {
4523 ctrt.type =
4524 (ha->oem_id == OEM_ID_INTEL ? 0xfd : 0xfe);
4525 if (ha->stype >= 0x300)
Jeff Garzik8e9a8a02007-07-17 05:25:17 -04004526 ctrt.ext_type = 0x6000 | ha->pdev->subsystem_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 else
4528 ctrt.ext_type = 0x6000 | ha->stype;
4529 }
Jeff Garzik8e9a8a02007-07-17 05:25:17 -04004530 ctrt.device_id = ha->pdev->device;
4531 ctrt.sub_device_id = ha->pdev->subsystem_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 }
4533 ctrt.info = ha->brd_phys;
4534 ctrt.oem_id = ha->oem_id;
4535 if (copy_to_user(argp, &ctrt, sizeof(gdth_ioctl_ctrtype)))
4536 return -EFAULT;
4537 break;
4538 }
4539
4540 case GDTIOCTL_GENERAL:
4541 return ioc_general(argp, cmnd);
4542
4543 case GDTIOCTL_EVENT:
4544 return ioc_event(argp);
4545
4546 case GDTIOCTL_LOCKDRV:
4547 return ioc_lockdrv(argp);
4548
4549 case GDTIOCTL_LOCKCHN:
4550 {
4551 gdth_ioctl_lockchn lchn;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004552 u8 i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553
4554 if (copy_from_user(&lchn, argp, sizeof(gdth_ioctl_lockchn)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004555 (NULL == (ha = gdth_find_ha(lchn.ionode))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004557
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 i = lchn.channel;
4559 if (i < ha->bus_cnt) {
4560 if (lchn.lock) {
4561 spin_lock_irqsave(&ha->smp_lock, flags);
4562 ha->raw[i].lock = 1;
4563 spin_unlock_irqrestore(&ha->smp_lock, flags);
Jens Axboe242f9dc2008-09-14 05:55:09 -07004564 for (j = 0; j < ha->tid_cnt; ++j)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004565 gdth_wait_completion(ha, i, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 } else {
4567 spin_lock_irqsave(&ha->smp_lock, flags);
4568 ha->raw[i].lock = 0;
4569 spin_unlock_irqrestore(&ha->smp_lock, flags);
Jens Axboe242f9dc2008-09-14 05:55:09 -07004570 for (j = 0; j < ha->tid_cnt; ++j)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004571 gdth_next(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 }
4573 }
4574 break;
4575 }
4576
4577 case GDTIOCTL_RESCAN:
4578 return ioc_rescan(argp, cmnd);
4579
4580 case GDTIOCTL_HDRLIST:
4581 return ioc_hdrlist(argp, cmnd);
4582
4583 case GDTIOCTL_RESET_BUS:
4584 {
4585 gdth_ioctl_reset res;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004586 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587
4588 if (copy_from_user(&res, argp, sizeof(gdth_ioctl_reset)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004589 (NULL == (ha = gdth_find_ha(res.ionode))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02004592 scp = kzalloc(sizeof(*scp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 if (!scp)
4594 return -ENOMEM;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004595 scp->device = ha->sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 scp->cmd_len = 12;
Christoph Hellwig52759e62007-10-02 22:59:53 +02004597 scp->device->channel = res.number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 rval = gdth_eh_bus_reset(scp);
4599 res.status = (rval == SUCCESS ? S_OK : S_GENERR);
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004600 kfree(scp);
Jeff Garzik8d7a5da2007-10-02 22:54:28 +02004601
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 if (copy_to_user(argp, &res, sizeof(gdth_ioctl_reset)))
4603 return -EFAULT;
4604 break;
4605 }
4606
4607 case GDTIOCTL_RESET_DRV:
4608 return ioc_resetdrv(argp, cmnd);
4609
4610 default:
4611 break;
4612 }
4613 return 0;
4614}
4615
Arnd Bergmannf4927c42010-04-27 00:24:01 +02004616static long gdth_unlocked_ioctl(struct file *file, unsigned int cmd,
4617 unsigned long arg)
4618{
4619 int ret;
4620
Arnd Bergmannc45d15d2010-06-02 14:28:52 +02004621 mutex_lock(&gdth_mutex);
Arnd Bergmannf4927c42010-04-27 00:24:01 +02004622 ret = gdth_ioctl(file, cmd, arg);
Arnd Bergmannc45d15d2010-06-02 14:28:52 +02004623 mutex_unlock(&gdth_mutex);
Arnd Bergmannf4927c42010-04-27 00:24:01 +02004624
4625 return ret;
4626}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627
4628/* flush routine */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004629static void gdth_flush(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630{
4631 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 gdth_cmd_str gdtcmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 char cmnd[MAX_COMMAND_SIZE];
4634 memset(cmnd, 0xff, MAX_COMMAND_SIZE);
4635
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004636 TRACE2(("gdth_flush() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 for (i = 0; i < MAX_HDRIVES; ++i) {
4639 if (ha->hdr[i].present) {
4640 gdtcmd.BoardNode = LOCALBOARD;
4641 gdtcmd.Service = CACHESERVICE;
4642 gdtcmd.OpCode = GDT_FLUSH;
4643 if (ha->cache_feat & GDT_64BIT) {
4644 gdtcmd.u.cache64.DeviceNo = i;
4645 gdtcmd.u.cache64.BlockNo = 1;
4646 gdtcmd.u.cache64.sg_canz = 0;
4647 } else {
4648 gdtcmd.u.cache.DeviceNo = i;
4649 gdtcmd.u.cache.BlockNo = 1;
4650 gdtcmd.u.cache.sg_canz = 0;
4651 }
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004652 TRACE2(("gdth_flush(): flush ha %d drive %d\n", ha->hanum, i));
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004653
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004654 gdth_execute(ha->shost, &gdtcmd, cmnd, 30, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 }
4656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657}
4658
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004659/* configure lun */
4660static int gdth_slave_configure(struct scsi_device *sdev)
4661{
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004662 sdev->skip_ms_page_3f = 1;
4663 sdev->skip_ms_page_8 = 1;
4664 return 0;
4665}
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004666
Christoph Hellwig835cc242007-10-02 23:09:56 +02004667static struct scsi_host_template gdth_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 .name = "GDT SCSI Disk Array Controller",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 .info = gdth_info,
4670 .queuecommand = gdth_queuecommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 .eh_bus_reset_handler = gdth_eh_bus_reset,
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004672 .slave_configure = gdth_slave_configure,
Jeff Garzik69916b72007-10-02 22:57:58 +02004673 .bios_param = gdth_bios_param,
Al Viro3e0552e2013-03-31 00:52:08 -04004674 .show_info = gdth_show_info,
4675 .write_info = gdth_set_info,
Jens Axboe242f9dc2008-09-14 05:55:09 -07004676 .eh_timed_out = gdth_timed_out,
Jeff Garzik69916b72007-10-02 22:57:58 +02004677 .proc_name = "gdth",
4678 .can_queue = GDTH_MAXCMDS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 .this_id = -1,
4680 .sg_tablesize = GDTH_MAXSG,
4681 .cmd_per_lun = GDTH_MAXC_P_L,
4682 .unchecked_isa_dma = 1,
4683 .use_clustering = ENABLE_CLUSTERING,
Martin K. Petersen54b2b502013-10-23 06:25:40 -04004684 .no_write_same = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685};
4686
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004687#ifdef CONFIG_ISA
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004688static int __init gdth_isa_probe_one(u32 isa_bios)
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004689{
4690 struct Scsi_Host *shp;
4691 gdth_ha_str *ha;
4692 dma_addr_t scratch_dma_handle = 0;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004693 int error, i;
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004694
4695 if (!gdth_search_isa(isa_bios))
4696 return -ENXIO;
4697
Christoph Hellwig835cc242007-10-02 23:09:56 +02004698 shp = scsi_host_alloc(&gdth_template, sizeof(gdth_ha_str));
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004699 if (!shp)
4700 return -ENOMEM;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004701 ha = shost_priv(shp);
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004702
4703 error = -ENODEV;
4704 if (!gdth_init_isa(isa_bios,ha))
4705 goto out_host_put;
4706
4707 /* controller found and initialized */
4708 printk("Configuring GDT-ISA HA at BIOS 0x%05X IRQ %u DRQ %u\n",
4709 isa_bios, ha->irq, ha->drq);
4710
Michael Opdenacker4909cc22014-03-05 06:09:41 +01004711 error = request_irq(ha->irq, gdth_interrupt, 0, "gdth", ha);
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004712 if (error) {
4713 printk("GDT-ISA: Unable to allocate IRQ\n");
4714 goto out_host_put;
4715 }
4716
4717 error = request_dma(ha->drq, "gdth");
4718 if (error) {
4719 printk("GDT-ISA: Unable to allocate DMA channel\n");
4720 goto out_free_irq;
4721 }
4722
4723 set_dma_mode(ha->drq,DMA_MODE_CASCADE);
4724 enable_dma(ha->drq);
4725 shp->unchecked_isa_dma = 1;
4726 shp->irq = ha->irq;
4727 shp->dma_channel = ha->drq;
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004728
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004729 ha->hanum = gdth_ctr_count++;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004730 ha->shost = shp;
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004731
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004732 ha->pccb = &ha->cmdext;
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004733 ha->ccb_phys = 0L;
4734 ha->pdev = NULL;
4735
4736 error = -ENOMEM;
4737
4738 ha->pscratch = pci_alloc_consistent(ha->pdev, GDTH_SCRATCH,
4739 &scratch_dma_handle);
4740 if (!ha->pscratch)
4741 goto out_dec_counters;
4742 ha->scratch_phys = scratch_dma_handle;
4743
4744 ha->pmsg = pci_alloc_consistent(ha->pdev, sizeof(gdth_msg_str),
4745 &scratch_dma_handle);
4746 if (!ha->pmsg)
4747 goto out_free_pscratch;
4748 ha->msg_phys = scratch_dma_handle;
4749
4750#ifdef INT_COAL
4751 ha->coal_stat = pci_alloc_consistent(ha->pdev,
4752 sizeof(gdth_coal_status) * MAXOFFSETS,
4753 &scratch_dma_handle);
4754 if (!ha->coal_stat)
4755 goto out_free_pmsg;
4756 ha->coal_stat_phys = scratch_dma_handle;
4757#endif
4758
4759 ha->scratch_busy = FALSE;
4760 ha->req_first = NULL;
4761 ha->tid_cnt = MAX_HDRIVES;
4762 if (max_ids > 0 && max_ids < ha->tid_cnt)
4763 ha->tid_cnt = max_ids;
4764 for (i = 0; i < GDTH_MAXCMDS; ++i)
4765 ha->cmd_tab[i].cmnd = UNUSED_CMND;
4766 ha->scan_mode = rescan ? 0x10 : 0;
4767
4768 error = -ENODEV;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004769 if (!gdth_search_drives(ha)) {
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004770 printk("GDT-ISA: Error during device scan\n");
4771 goto out_free_coal_stat;
4772 }
4773
4774 if (hdr_channel < 0 || hdr_channel > ha->bus_cnt)
4775 hdr_channel = ha->bus_cnt;
4776 ha->virt_bus = hdr_channel;
4777
4778 if (ha->cache_feat & ha->raw_feat & ha->screen_feat & GDT_64BIT)
4779 shp->max_cmd_len = 16;
4780
4781 shp->max_id = ha->tid_cnt;
4782 shp->max_lun = MAXLUN;
Christoph Hellwig52759e62007-10-02 22:59:53 +02004783 shp->max_channel = ha->bus_cnt;
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004784
4785 spin_lock_init(&ha->smp_lock);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004786 gdth_enable_int(ha);
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004787
Christoph Hellwig835cc242007-10-02 23:09:56 +02004788 error = scsi_add_host(shp, NULL);
4789 if (error)
4790 goto out_free_coal_stat;
4791 list_add_tail(&ha->list, &gdth_instances);
James Bottomley2d6f0d02008-05-04 22:34:49 -05004792 gdth_timer_init();
Boaz Harrosh61c92812008-02-12 19:35:22 +02004793
4794 scsi_scan_host(shp);
4795
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004796 return 0;
4797
4798 out_free_coal_stat:
4799#ifdef INT_COAL
4800 pci_free_consistent(ha->pdev, sizeof(gdth_coal_status) * MAXOFFSETS,
4801 ha->coal_stat, ha->coal_stat_phys);
4802 out_free_pmsg:
4803#endif
4804 pci_free_consistent(ha->pdev, sizeof(gdth_msg_str),
4805 ha->pmsg, ha->msg_phys);
4806 out_free_pscratch:
4807 pci_free_consistent(ha->pdev, GDTH_SCRATCH,
4808 ha->pscratch, ha->scratch_phys);
4809 out_dec_counters:
4810 gdth_ctr_count--;
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004811 out_free_irq:
4812 free_irq(ha->irq, ha);
4813 out_host_put:
Christoph Hellwig835cc242007-10-02 23:09:56 +02004814 scsi_host_put(shp);
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004815 return error;
4816}
4817#endif /* CONFIG_ISA */
4818
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004819#ifdef CONFIG_EISA
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004820static int __init gdth_eisa_probe_one(u16 eisa_slot)
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004821{
4822 struct Scsi_Host *shp;
4823 gdth_ha_str *ha;
4824 dma_addr_t scratch_dma_handle = 0;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004825 int error, i;
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004826
4827 if (!gdth_search_eisa(eisa_slot))
4828 return -ENXIO;
4829
Christoph Hellwig835cc242007-10-02 23:09:56 +02004830 shp = scsi_host_alloc(&gdth_template, sizeof(gdth_ha_str));
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004831 if (!shp)
4832 return -ENOMEM;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004833 ha = shost_priv(shp);
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004834
4835 error = -ENODEV;
4836 if (!gdth_init_eisa(eisa_slot,ha))
4837 goto out_host_put;
4838
4839 /* controller found and initialized */
4840 printk("Configuring GDT-EISA HA at Slot %d IRQ %u\n",
4841 eisa_slot >> 12, ha->irq);
4842
Michael Opdenacker4909cc22014-03-05 06:09:41 +01004843 error = request_irq(ha->irq, gdth_interrupt, 0, "gdth", ha);
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004844 if (error) {
4845 printk("GDT-EISA: Unable to allocate IRQ\n");
4846 goto out_host_put;
4847 }
4848
4849 shp->unchecked_isa_dma = 0;
4850 shp->irq = ha->irq;
4851 shp->dma_channel = 0xff;
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004852
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004853 ha->hanum = gdth_ctr_count++;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004854 ha->shost = shp;
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004855
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004856 TRACE2(("EISA detect Bus 0: hanum %d\n", ha->hanum));
4857
4858 ha->pccb = &ha->cmdext;
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004859 ha->ccb_phys = 0L;
4860
4861 error = -ENOMEM;
4862
4863 ha->pdev = NULL;
4864 ha->pscratch = pci_alloc_consistent(ha->pdev, GDTH_SCRATCH,
4865 &scratch_dma_handle);
4866 if (!ha->pscratch)
4867 goto out_free_irq;
4868 ha->scratch_phys = scratch_dma_handle;
4869
4870 ha->pmsg = pci_alloc_consistent(ha->pdev, sizeof(gdth_msg_str),
4871 &scratch_dma_handle);
4872 if (!ha->pmsg)
4873 goto out_free_pscratch;
4874 ha->msg_phys = scratch_dma_handle;
4875
4876#ifdef INT_COAL
4877 ha->coal_stat = pci_alloc_consistent(ha->pdev,
4878 sizeof(gdth_coal_status) * MAXOFFSETS,
4879 &scratch_dma_handle);
4880 if (!ha->coal_stat)
4881 goto out_free_pmsg;
4882 ha->coal_stat_phys = scratch_dma_handle;
4883#endif
4884
4885 ha->ccb_phys = pci_map_single(ha->pdev,ha->pccb,
4886 sizeof(gdth_cmd_str), PCI_DMA_BIDIRECTIONAL);
4887 if (!ha->ccb_phys)
4888 goto out_free_coal_stat;
4889
4890 ha->scratch_busy = FALSE;
4891 ha->req_first = NULL;
4892 ha->tid_cnt = MAX_HDRIVES;
4893 if (max_ids > 0 && max_ids < ha->tid_cnt)
4894 ha->tid_cnt = max_ids;
4895 for (i = 0; i < GDTH_MAXCMDS; ++i)
4896 ha->cmd_tab[i].cmnd = UNUSED_CMND;
4897 ha->scan_mode = rescan ? 0x10 : 0;
4898
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004899 if (!gdth_search_drives(ha)) {
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004900 printk("GDT-EISA: Error during device scan\n");
4901 error = -ENODEV;
4902 goto out_free_ccb_phys;
4903 }
4904
4905 if (hdr_channel < 0 || hdr_channel > ha->bus_cnt)
4906 hdr_channel = ha->bus_cnt;
4907 ha->virt_bus = hdr_channel;
4908
4909 if (ha->cache_feat & ha->raw_feat & ha->screen_feat & GDT_64BIT)
4910 shp->max_cmd_len = 16;
4911
4912 shp->max_id = ha->tid_cnt;
4913 shp->max_lun = MAXLUN;
Christoph Hellwig52759e62007-10-02 22:59:53 +02004914 shp->max_channel = ha->bus_cnt;
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004915
4916 spin_lock_init(&ha->smp_lock);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004917 gdth_enable_int(ha);
Christoph Hellwig835cc242007-10-02 23:09:56 +02004918
4919 error = scsi_add_host(shp, NULL);
4920 if (error)
Roel Kluin2c076ee2010-08-10 18:01:10 -07004921 goto out_free_ccb_phys;
Christoph Hellwig835cc242007-10-02 23:09:56 +02004922 list_add_tail(&ha->list, &gdth_instances);
James Bottomley2d6f0d02008-05-04 22:34:49 -05004923 gdth_timer_init();
Boaz Harrosh61c92812008-02-12 19:35:22 +02004924
4925 scsi_scan_host(shp);
4926
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004927 return 0;
4928
4929 out_free_ccb_phys:
4930 pci_unmap_single(ha->pdev,ha->ccb_phys, sizeof(gdth_cmd_str),
4931 PCI_DMA_BIDIRECTIONAL);
4932 out_free_coal_stat:
4933#ifdef INT_COAL
4934 pci_free_consistent(ha->pdev, sizeof(gdth_coal_status) * MAXOFFSETS,
4935 ha->coal_stat, ha->coal_stat_phys);
4936 out_free_pmsg:
4937#endif
4938 pci_free_consistent(ha->pdev, sizeof(gdth_msg_str),
4939 ha->pmsg, ha->msg_phys);
4940 out_free_pscratch:
4941 pci_free_consistent(ha->pdev, GDTH_SCRATCH,
4942 ha->pscratch, ha->scratch_phys);
4943 out_free_irq:
4944 free_irq(ha->irq, ha);
4945 gdth_ctr_count--;
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004946 out_host_put:
Christoph Hellwig835cc242007-10-02 23:09:56 +02004947 scsi_host_put(shp);
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004948 return error;
4949}
4950#endif /* CONFIG_EISA */
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004951
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004952#ifdef CONFIG_PCI
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004953static int gdth_pci_probe_one(gdth_pci_str *pcistr, gdth_ha_str **ha_out)
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004954{
4955 struct Scsi_Host *shp;
4956 gdth_ha_str *ha;
4957 dma_addr_t scratch_dma_handle = 0;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004958 int error, i;
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05004959 struct pci_dev *pdev = pcistr->pdev;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004960
Jeff Garzikcff26802008-02-15 22:20:09 -05004961 *ha_out = NULL;
4962
Christoph Hellwig835cc242007-10-02 23:09:56 +02004963 shp = scsi_host_alloc(&gdth_template, sizeof(gdth_ha_str));
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004964 if (!shp)
4965 return -ENOMEM;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004966 ha = shost_priv(shp);
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004967
4968 error = -ENODEV;
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05004969 if (!gdth_init_pci(pdev, pcistr, ha))
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004970 goto out_host_put;
4971
4972 /* controller found and initialized */
4973 printk("Configuring GDT-PCI HA at %d/%d IRQ %u\n",
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05004974 pdev->bus->number,
4975 PCI_SLOT(pdev->devfn),
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004976 ha->irq);
4977
4978 error = request_irq(ha->irq, gdth_interrupt,
Michael Opdenacker4909cc22014-03-05 06:09:41 +01004979 IRQF_SHARED, "gdth", ha);
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004980 if (error) {
4981 printk("GDT-PCI: Unable to allocate IRQ\n");
4982 goto out_host_put;
4983 }
4984
4985 shp->unchecked_isa_dma = 0;
4986 shp->irq = ha->irq;
4987 shp->dma_channel = 0xff;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004988
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004989 ha->hanum = gdth_ctr_count++;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004990 ha->shost = shp;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004991
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004992 ha->pccb = &ha->cmdext;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004993 ha->ccb_phys = 0L;
4994
4995 error = -ENOMEM;
4996
4997 ha->pscratch = pci_alloc_consistent(ha->pdev, GDTH_SCRATCH,
4998 &scratch_dma_handle);
4999 if (!ha->pscratch)
5000 goto out_free_irq;
5001 ha->scratch_phys = scratch_dma_handle;
5002
5003 ha->pmsg = pci_alloc_consistent(ha->pdev, sizeof(gdth_msg_str),
5004 &scratch_dma_handle);
5005 if (!ha->pmsg)
5006 goto out_free_pscratch;
5007 ha->msg_phys = scratch_dma_handle;
5008
5009#ifdef INT_COAL
5010 ha->coal_stat = pci_alloc_consistent(ha->pdev,
5011 sizeof(gdth_coal_status) * MAXOFFSETS,
5012 &scratch_dma_handle);
5013 if (!ha->coal_stat)
5014 goto out_free_pmsg;
5015 ha->coal_stat_phys = scratch_dma_handle;
5016#endif
5017
5018 ha->scratch_busy = FALSE;
5019 ha->req_first = NULL;
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05005020 ha->tid_cnt = pdev->device >= 0x200 ? MAXID : MAX_HDRIVES;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005021 if (max_ids > 0 && max_ids < ha->tid_cnt)
5022 ha->tid_cnt = max_ids;
5023 for (i = 0; i < GDTH_MAXCMDS; ++i)
5024 ha->cmd_tab[i].cmnd = UNUSED_CMND;
5025 ha->scan_mode = rescan ? 0x10 : 0;
5026
5027 error = -ENODEV;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02005028 if (!gdth_search_drives(ha)) {
5029 printk("GDT-PCI %d: Error during device scan\n", ha->hanum);
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005030 goto out_free_coal_stat;
5031 }
5032
5033 if (hdr_channel < 0 || hdr_channel > ha->bus_cnt)
5034 hdr_channel = ha->bus_cnt;
5035 ha->virt_bus = hdr_channel;
5036
5037 /* 64-bit DMA only supported from FW >= x.43 */
5038 if (!(ha->cache_feat & ha->raw_feat & ha->screen_feat & GDT_64BIT) ||
5039 !ha->dma64_support) {
Yang Hongyang284901a2009-04-06 19:01:15 -07005040 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005041 printk(KERN_WARNING "GDT-PCI %d: "
Boaz Harrosh45f1a412007-10-02 23:05:53 +02005042 "Unable to set 32-bit DMA\n", ha->hanum);
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005043 goto out_free_coal_stat;
5044 }
5045 } else {
5046 shp->max_cmd_len = 16;
Yang Hongyang6a355282009-04-06 19:01:13 -07005047 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02005048 printk("GDT-PCI %d: 64-bit DMA enabled\n", ha->hanum);
Yang Hongyang284901a2009-04-06 19:01:15 -07005049 } else if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005050 printk(KERN_WARNING "GDT-PCI %d: "
Boaz Harrosh45f1a412007-10-02 23:05:53 +02005051 "Unable to set 64/32-bit DMA\n", ha->hanum);
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005052 goto out_free_coal_stat;
5053 }
5054 }
5055
5056 shp->max_id = ha->tid_cnt;
5057 shp->max_lun = MAXLUN;
Christoph Hellwig52759e62007-10-02 22:59:53 +02005058 shp->max_channel = ha->bus_cnt;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005059
5060 spin_lock_init(&ha->smp_lock);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02005061 gdth_enable_int(ha);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005062
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05005063 error = scsi_add_host(shp, &pdev->dev);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005064 if (error)
5065 goto out_free_coal_stat;
5066 list_add_tail(&ha->list, &gdth_instances);
Boaz Harrosh61c92812008-02-12 19:35:22 +02005067
Jeff Garzikcff26802008-02-15 22:20:09 -05005068 pci_set_drvdata(ha->pdev, ha);
James Bottomley2d6f0d02008-05-04 22:34:49 -05005069 gdth_timer_init();
Jeff Garzikcff26802008-02-15 22:20:09 -05005070
Boaz Harrosh61c92812008-02-12 19:35:22 +02005071 scsi_scan_host(shp);
5072
Jeff Garzikcff26802008-02-15 22:20:09 -05005073 *ha_out = ha;
5074
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005075 return 0;
5076
5077 out_free_coal_stat:
5078#ifdef INT_COAL
5079 pci_free_consistent(ha->pdev, sizeof(gdth_coal_status) * MAXOFFSETS,
5080 ha->coal_stat, ha->coal_stat_phys);
5081 out_free_pmsg:
5082#endif
5083 pci_free_consistent(ha->pdev, sizeof(gdth_msg_str),
5084 ha->pmsg, ha->msg_phys);
5085 out_free_pscratch:
5086 pci_free_consistent(ha->pdev, GDTH_SCRATCH,
5087 ha->pscratch, ha->scratch_phys);
5088 out_free_irq:
5089 free_irq(ha->irq, ha);
5090 gdth_ctr_count--;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005091 out_host_put:
Christoph Hellwig835cc242007-10-02 23:09:56 +02005092 scsi_host_put(shp);
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005093 return error;
5094}
5095#endif /* CONFIG_PCI */
5096
Christoph Hellwig835cc242007-10-02 23:09:56 +02005097static void gdth_remove_one(gdth_ha_str *ha)
5098{
5099 struct Scsi_Host *shp = ha->shost;
5100
5101 TRACE2(("gdth_remove_one()\n"));
5102
5103 scsi_remove_host(shp);
5104
Boaz Harroshb31ddd32008-02-27 15:27:16 -08005105 gdth_flush(ha);
5106
Christoph Hellwig835cc242007-10-02 23:09:56 +02005107 if (ha->sdev) {
5108 scsi_free_host_dev(ha->sdev);
5109 ha->sdev = NULL;
5110 }
5111
Christoph Hellwig835cc242007-10-02 23:09:56 +02005112 if (shp->irq)
5113 free_irq(shp->irq,ha);
5114
5115#ifdef CONFIG_ISA
5116 if (shp->dma_channel != 0xff)
5117 free_dma(shp->dma_channel);
5118#endif
5119#ifdef INT_COAL
5120 if (ha->coal_stat)
5121 pci_free_consistent(ha->pdev, sizeof(gdth_coal_status) *
5122 MAXOFFSETS, ha->coal_stat, ha->coal_stat_phys);
5123#endif
5124 if (ha->pscratch)
5125 pci_free_consistent(ha->pdev, GDTH_SCRATCH,
5126 ha->pscratch, ha->scratch_phys);
5127 if (ha->pmsg)
5128 pci_free_consistent(ha->pdev, sizeof(gdth_msg_str),
5129 ha->pmsg, ha->msg_phys);
5130 if (ha->ccb_phys)
5131 pci_unmap_single(ha->pdev,ha->ccb_phys,
5132 sizeof(gdth_cmd_str),PCI_DMA_BIDIRECTIONAL);
5133
5134 scsi_host_put(shp);
5135}
5136
Dave Jones1fe6dbf2010-01-04 10:19:34 -05005137static int gdth_halt(struct notifier_block *nb, unsigned long event, void *buf)
Boaz Harroshb31ddd32008-02-27 15:27:16 -08005138{
5139 gdth_ha_str *ha;
5140
5141 TRACE2(("gdth_halt() event %d\n", (int)event));
5142 if (event != SYS_RESTART && event != SYS_HALT && event != SYS_POWER_OFF)
5143 return NOTIFY_DONE;
5144
5145 list_for_each_entry(ha, &gdth_instances, list)
5146 gdth_flush(ha);
5147
5148 return NOTIFY_OK;
5149}
5150
5151static struct notifier_block gdth_notifier = {
5152 gdth_halt, NULL, 0
5153};
5154
Christoph Hellwig835cc242007-10-02 23:09:56 +02005155static int __init gdth_init(void)
5156{
5157 if (disable) {
5158 printk("GDT-HA: Controller driver disabled from"
5159 " command line !\n");
5160 return 0;
5161 }
5162
5163 printk("GDT-HA: Storage RAID Controller Driver. Version: %s\n",
5164 GDTH_VERSION_STR);
5165
5166 /* initializations */
5167 gdth_polling = TRUE;
5168 gdth_clear_events();
Kees Cook79325892017-10-11 17:17:42 -07005169 timer_setup(&gdth_timer, gdth_timeout, 0);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005170
5171 /* As default we do not probe for EISA or ISA controllers */
5172 if (probe_eisa_isa) {
5173 /* scanning for controllers, at first: ISA controller */
5174#ifdef CONFIG_ISA
Dave Jones1fe6dbf2010-01-04 10:19:34 -05005175 u32 isa_bios;
Christoph Hellwig835cc242007-10-02 23:09:56 +02005176 for (isa_bios = 0xc8000UL; isa_bios <= 0xd8000UL;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02005177 isa_bios += 0x8000UL)
Christoph Hellwig835cc242007-10-02 23:09:56 +02005178 gdth_isa_probe_one(isa_bios);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005179#endif
5180#ifdef CONFIG_EISA
5181 {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05005182 u16 eisa_slot;
Christoph Hellwig835cc242007-10-02 23:09:56 +02005183 for (eisa_slot = 0x1000; eisa_slot <= 0x8000;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02005184 eisa_slot += 0x1000)
Christoph Hellwig835cc242007-10-02 23:09:56 +02005185 gdth_eisa_probe_one(eisa_slot);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005186 }
5187#endif
5188 }
5189
5190#ifdef CONFIG_PCI
5191 /* scanning for PCI controllers */
James Bottomleya85591f2008-05-04 22:35:58 -05005192 if (pci_register_driver(&gdth_pci_driver)) {
5193 gdth_ha_str *ha;
5194
5195 list_for_each_entry(ha, &gdth_instances, list)
5196 gdth_remove_one(ha);
5197 return -ENODEV;
5198 }
Christoph Hellwig835cc242007-10-02 23:09:56 +02005199#endif /* CONFIG_PCI */
5200
5201 TRACE2(("gdth_detect() %d controller detected\n", gdth_ctr_count));
Linus Torvaldsc596cc42007-10-15 12:46:16 -07005202
Christoph Hellwig835cc242007-10-02 23:09:56 +02005203 major = register_chrdev(0,"gdth", &gdth_fops);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005204 register_reboot_notifier(&gdth_notifier);
5205 gdth_polling = FALSE;
5206 return 0;
5207}
5208
5209static void __exit gdth_exit(void)
5210{
5211 gdth_ha_str *ha;
5212
Boaz Harroshb31ddd32008-02-27 15:27:16 -08005213 unregister_chrdev(major, "gdth");
5214 unregister_reboot_notifier(&gdth_notifier);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005215
5216#ifdef GDTH_STATISTICS
Boaz Harroshb31ddd32008-02-27 15:27:16 -08005217 del_timer_sync(&gdth_timer);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005218#endif
Boaz Harroshb31ddd32008-02-27 15:27:16 -08005219
Jeff Garzikcff26802008-02-15 22:20:09 -05005220#ifdef CONFIG_PCI
James Bottomleya85591f2008-05-04 22:35:58 -05005221 pci_unregister_driver(&gdth_pci_driver);
Jeff Garzikcff26802008-02-15 22:20:09 -05005222#endif
5223
Boaz Harroshb31ddd32008-02-27 15:27:16 -08005224 list_for_each_entry(ha, &gdth_instances, list)
5225 gdth_remove_one(ha);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005226}
5227
5228module_init(gdth_init);
5229module_exit(gdth_exit);
5230
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231#ifndef MODULE
5232__setup("gdth=", option_setup);
5233#endif