blob: ba3c94c9c25f22cb00bbf2dd64db394e85c51944 [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>
Jonathan Corbet46787b42008-05-15 16:09:48 -0600123#include <linux/smp_lock.h>
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#ifdef GDTH_RTC
126#include <linux/mc146818rtc.h>
127#endif
128#include <linux/reboot.h>
129
130#include <asm/dma.h>
131#include <asm/system.h>
132#include <asm/io.h>
133#include <asm/uaccess.h>
134#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
142static void gdth_delay(int milliseconds);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500143static void gdth_eval_mapping(u32 size, u32 *cyls, int *heads, int *secs);
David Howells7d12e782006-10-05 14:55:46 +0100144static irqreturn_t gdth_interrupt(int irq, void *dev_id);
Jeff Garzik230e8862007-12-13 16:14:12 -0800145static irqreturn_t __gdth_interrupt(gdth_ha_str *ha,
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +0200146 int gdth_from_wait, int* pIndex);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500147static int gdth_sync_event(gdth_ha_str *ha, int service, u8 index,
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200148 Scsi_Cmnd *scp);
149static int gdth_async_event(gdth_ha_str *ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static void gdth_log_event(gdth_evt_data *dvr, char *buffer);
151
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500152static void gdth_putq(gdth_ha_str *ha, Scsi_Cmnd *scp, u8 priority);
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200153static void gdth_next(gdth_ha_str *ha);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500154static int gdth_fill_raw_cmd(gdth_ha_str *ha, Scsi_Cmnd *scp, u8 b);
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200155static int gdth_special_cmd(gdth_ha_str *ha, Scsi_Cmnd *scp);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500156static gdth_evt_str *gdth_store_event(gdth_ha_str *ha, u16 source,
157 u16 idx, gdth_evt_data *evt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static int gdth_read_event(gdth_ha_str *ha, int handle, gdth_evt_str *estr);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500159static void gdth_readapp_event(gdth_ha_str *ha, u8 application,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 gdth_evt_str *estr);
161static void gdth_clear_events(void);
162
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200163static void gdth_copy_internal_data(gdth_ha_str *ha, Scsi_Cmnd *scp,
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500164 char *buffer, u16 count);
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200165static int gdth_internal_cache_cmd(gdth_ha_str *ha, Scsi_Cmnd *scp);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500166static int gdth_fill_cache_cmd(gdth_ha_str *ha, Scsi_Cmnd *scp, u16 hdrive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200168static void gdth_enable_int(gdth_ha_str *ha);
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200169static int gdth_test_busy(gdth_ha_str *ha);
170static int gdth_get_cmd_index(gdth_ha_str *ha);
171static void gdth_release_event(gdth_ha_str *ha);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500172static int gdth_wait(gdth_ha_str *ha, int index,u32 time);
173static int gdth_internal_cmd(gdth_ha_str *ha, u8 service, u16 opcode,
174 u32 p1, u64 p2,u64 p3);
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200175static int gdth_search_drives(gdth_ha_str *ha);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500176static int gdth_analyse_hdrive(gdth_ha_str *ha, u16 hdrive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200178static const char *gdth_ctr_name(gdth_ha_str *ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180static int gdth_open(struct inode *inode, struct file *filep);
181static int gdth_close(struct inode *inode, struct file *filep);
182static int gdth_ioctl(struct inode *inode, struct file *filep,
183 unsigned int cmd, unsigned long arg);
184
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200185static void gdth_flush(gdth_ha_str *ha);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700186static int gdth_queuecommand(Scsi_Cmnd *scp,void (*done)(Scsi_Cmnd *));
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200187static int __gdth_queuecommand(gdth_ha_str *ha, struct scsi_cmnd *scp,
188 struct gdth_cmndinfo *cmndinfo);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700189static void gdth_scsi_done(struct scsi_cmnd *scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191#ifdef DEBUG_GDTH
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500192static u8 DebugState = DEBUG_GDTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194#ifdef __SERIAL__
195#define MAX_SERBUF 160
196static void ser_init(void);
197static void ser_puts(char *str);
198static void ser_putc(char c);
199static int ser_printk(const char *fmt, ...);
200static char strbuf[MAX_SERBUF+1];
201#ifdef __COM2__
202#define COM_BASE 0x2f8
203#else
204#define COM_BASE 0x3f8
205#endif
206static void ser_init()
207{
208 unsigned port=COM_BASE;
209
210 outb(0x80,port+3);
211 outb(0,port+1);
212 /* 19200 Baud, if 9600: outb(12,port) */
213 outb(6, port);
214 outb(3,port+3);
215 outb(0,port+1);
216 /*
217 ser_putc('I');
218 ser_putc(' ');
219 */
220}
221
222static void ser_puts(char *str)
223{
224 char *ptr;
225
226 ser_init();
227 for (ptr=str;*ptr;++ptr)
228 ser_putc(*ptr);
229}
230
231static void ser_putc(char c)
232{
233 unsigned port=COM_BASE;
234
235 while ((inb(port+5) & 0x20)==0);
236 outb(c,port);
237 if (c==0x0a)
238 {
239 while ((inb(port+5) & 0x20)==0);
240 outb(0x0d,port);
241 }
242}
243
244static int ser_printk(const char *fmt, ...)
245{
246 va_list args;
247 int i;
248
249 va_start(args,fmt);
250 i = vsprintf(strbuf,fmt,args);
251 ser_puts(strbuf);
252 va_end(args);
253 return i;
254}
255
256#define TRACE(a) {if (DebugState==1) {ser_printk a;}}
257#define TRACE2(a) {if (DebugState==1 || DebugState==2) {ser_printk a;}}
258#define TRACE3(a) {if (DebugState!=0) {ser_printk a;}}
259
260#else /* !__SERIAL__ */
261#define TRACE(a) {if (DebugState==1) {printk a;}}
262#define TRACE2(a) {if (DebugState==1 || DebugState==2) {printk a;}}
263#define TRACE3(a) {if (DebugState!=0) {printk a;}}
264#endif
265
266#else /* !DEBUG */
267#define TRACE(a)
268#define TRACE2(a)
269#define TRACE3(a)
270#endif
271
272#ifdef GDTH_STATISTICS
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500273static u32 max_rq=0, max_index=0, max_sg=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274#ifdef INT_COAL
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500275static u32 max_int_coal=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276#endif
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500277static u32 act_ints=0, act_ios=0, act_stats=0, act_rq=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278static struct timer_list gdth_timer;
279#endif
280
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500281#define PTR2USHORT(a) (u16)(unsigned long)(a)
Tobias Klauser6391a112006-06-08 22:23:48 -0700282#define GDTOFFSOF(a,b) (size_t)&(((a*)0)->b)
283#define INDEX_OK(i,t) ((i)<ARRAY_SIZE(t))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285#define BUS_L2P(a,b) ((b)>(a)->virt_bus ? (b-1):(b))
286
Christoph Hellwigaed91cb2007-10-02 22:48:16 +0200287#ifdef CONFIG_ISA
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500288static u8 gdth_drq_tab[4] = {5,6,7,7}; /* DRQ table */
Christoph Hellwigaed91cb2007-10-02 22:48:16 +0200289#endif
Dhaval Gianic8e91b02007-10-12 15:12:31 +0530290#if defined(CONFIG_EISA) || defined(CONFIG_ISA)
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500291static u8 gdth_irq_tab[6] = {0,10,11,12,14,0}; /* IRQ table */
Christoph Hellwig706a5d42007-10-02 22:49:35 +0200292#endif
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500293static u8 gdth_polling; /* polling if TRUE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294static int gdth_ctr_count = 0; /* controller count */
Boaz Harrosh884f7fb2007-10-02 23:11:24 +0200295static LIST_HEAD(gdth_instances); /* controller list */
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500296static u8 gdth_write_through = FALSE; /* write through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297static gdth_evt_str ebuffer[MAX_EVENTS]; /* event buffer */
298static int elastidx;
299static int eoldidx;
300static int major;
301
302#define DIN 1 /* IN data direction */
303#define DOU 2 /* OUT data direction */
304#define DNO DIN /* no data transfer */
305#define DUN DIN /* unknown data direction */
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500306static u8 gdth_direction_tab[0x100] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 DNO,DNO,DIN,DIN,DOU,DIN,DIN,DOU,DIN,DUN,DOU,DOU,DUN,DUN,DUN,DIN,
308 DNO,DIN,DIN,DOU,DIN,DOU,DNO,DNO,DOU,DNO,DIN,DNO,DIN,DOU,DNO,DUN,
309 DIN,DUN,DIN,DUN,DOU,DIN,DUN,DUN,DIN,DIN,DOU,DNO,DUN,DIN,DOU,DOU,
310 DOU,DOU,DOU,DNO,DIN,DNO,DNO,DIN,DOU,DOU,DOU,DOU,DIN,DOU,DIN,DOU,
311 DOU,DOU,DIN,DIN,DIN,DNO,DUN,DNO,DNO,DNO,DUN,DNO,DOU,DIN,DUN,DUN,
312 DUN,DUN,DUN,DUN,DUN,DOU,DUN,DUN,DUN,DUN,DIN,DUN,DUN,DUN,DUN,DUN,
313 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
314 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
315 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DIN,DUN,DOU,DUN,DUN,DUN,DUN,DUN,
316 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DIN,DUN,
317 DUN,DUN,DUN,DUN,DUN,DNO,DNO,DUN,DIN,DNO,DOU,DUN,DNO,DUN,DOU,DOU,
318 DOU,DOU,DOU,DNO,DUN,DIN,DOU,DIN,DIN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
319 DUN,DUN,DOU,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
320 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
321 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DOU,DUN,DUN,DUN,DUN,DUN,
322 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN
323};
324
325/* LILO and modprobe/insmod parameters */
326/* IRQ list for GDT3000/3020 EISA controllers */
327static int irq[MAXHA] __initdata =
328{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
329 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
330/* disable driver flag */
331static int disable __initdata = 0;
332/* reserve flag */
333static int reserve_mode = 1;
334/* reserve list */
335static int reserve_list[MAX_RES_ARGS] =
336{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
337 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
338 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
339/* scan order for PCI controllers */
340static int reverse_scan = 0;
341/* virtual channel for the host drives */
342static int hdr_channel = 0;
343/* max. IDs per channel */
344static int max_ids = MAXID;
345/* rescan all IDs */
346static int rescan = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347/* shared access */
348static int shared_access = 1;
349/* enable support for EISA and ISA controllers */
350static int probe_eisa_isa = 0;
351/* 64 bit DMA mode, support for drives > 2 TB, if force_dma32 = 0 */
352static int force_dma32 = 0;
353
354/* parameters for modprobe/insmod */
355module_param_array(irq, int, NULL, 0);
356module_param(disable, int, 0);
357module_param(reserve_mode, int, 0);
358module_param_array(reserve_list, int, NULL, 0);
359module_param(reverse_scan, int, 0);
360module_param(hdr_channel, int, 0);
361module_param(max_ids, int, 0);
362module_param(rescan, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363module_param(shared_access, int, 0);
364module_param(probe_eisa_isa, int, 0);
365module_param(force_dma32, int, 0);
366MODULE_AUTHOR("Achim Leubner");
367MODULE_LICENSE("GPL");
368
369/* ioctl interface */
Arjan van de Ven00977a52007-02-12 00:55:34 -0800370static const struct file_operations gdth_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 .ioctl = gdth_ioctl,
372 .open = gdth_open,
373 .release = gdth_close,
374};
375
376#include "gdth_proc.h"
377#include "gdth_proc.c"
378
Boaz Harrosh884f7fb2007-10-02 23:11:24 +0200379static gdth_ha_str *gdth_find_ha(int hanum)
380{
381 gdth_ha_str *ha;
382
383 list_for_each_entry(ha, &gdth_instances, list)
384 if (hanum == ha->hanum)
385 return ha;
386
387 return NULL;
388}
389
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200390static struct gdth_cmndinfo *gdth_get_cmndinfo(gdth_ha_str *ha)
391{
392 struct gdth_cmndinfo *priv = NULL;
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500393 unsigned long flags;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200394 int i;
395
396 spin_lock_irqsave(&ha->smp_lock, flags);
397
398 for (i=0; i<GDTH_MAXCMDS; ++i) {
399 if (ha->cmndinfo[i].index == 0) {
400 priv = &ha->cmndinfo[i];
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200401 memset(priv, 0, sizeof(*priv));
Boaz Harroshee54cc62008-02-27 15:29:15 -0800402 priv->index = i+1;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200403 break;
404 }
405 }
406
407 spin_unlock_irqrestore(&ha->smp_lock, flags);
408
409 return priv;
410}
411
412static void gdth_put_cmndinfo(struct gdth_cmndinfo *priv)
413{
414 BUG_ON(!priv);
415 priv->index = 0;
416}
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418static void gdth_delay(int milliseconds)
419{
420 if (milliseconds == 0) {
421 udelay(1);
422 } else {
423 mdelay(milliseconds);
424 }
425}
426
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700427static void gdth_scsi_done(struct scsi_cmnd *scp)
428{
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200429 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
430 int internal_command = cmndinfo->internal_command;
431
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +0200432 TRACE2(("gdth_scsi_done()\n"));
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700433
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200434 gdth_put_cmndinfo(cmndinfo);
435 scp->host_scribble = NULL;
436
437 if (internal_command)
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +0200438 complete((struct completion *)scp->request);
439 else
440 scp->scsi_done(scp);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700441}
442
443int __gdth_execute(struct scsi_device *sdev, gdth_cmd_str *gdtcmd, char *cmnd,
444 int timeout, u32 *info)
445{
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200446 gdth_ha_str *ha = shost_priv(sdev->host);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700447 Scsi_Cmnd *scp;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200448 struct gdth_cmndinfo cmndinfo;
Peter Zijlstra6e9a4732006-09-30 23:28:10 -0700449 DECLARE_COMPLETION_ONSTACK(wait);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700450 int rval;
451
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +0200452 scp = kzalloc(sizeof(*scp), GFP_KERNEL);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700453 if (!scp)
454 return -ENOMEM;
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +0200455
Sven Schnelle1b96f892008-03-10 22:50:04 +0100456 scp->sense_buffer = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
457 if (!scp->sense_buffer) {
458 kfree(scp);
459 return -ENOMEM;
460 }
461
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700462 scp->device = sdev;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200463 memset(&cmndinfo, 0, sizeof(cmndinfo));
464
Christoph Hellwigbeb40482006-06-10 18:01:03 +0200465 /* use request field to save the ptr. to completion struct. */
466 scp->request = (struct request *)&wait;
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700467 scp->cmd_len = 12;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300468 scp->cmnd = cmnd;
Boaz Harroshf842b642007-10-02 23:16:01 +0200469 cmndinfo.priority = IOCTL_PRI;
Boaz Harroshee54cc62008-02-27 15:29:15 -0800470 cmndinfo.internal_cmd_str = gdtcmd;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200471 cmndinfo.internal_command = 1;
472
473 TRACE(("__gdth_execute() cmd 0x%x\n", scp->cmnd[0]));
474 __gdth_queuecommand(ha, scp, &cmndinfo);
475
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700476 wait_for_completion(&wait);
477
Boaz Harroshf842b642007-10-02 23:16:01 +0200478 rval = cmndinfo.status;
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700479 if (info)
Boaz Harroshf842b642007-10-02 23:16:01 +0200480 *info = cmndinfo.info;
Sven Schnelle1b96f892008-03-10 22:50:04 +0100481 kfree(scp->sense_buffer);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700482 kfree(scp);
483 return rval;
484}
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700485
486int gdth_execute(struct Scsi_Host *shost, gdth_cmd_str *gdtcmd, char *cmnd,
487 int timeout, u32 *info)
488{
489 struct scsi_device *sdev = scsi_get_host_dev(shost);
490 int rval = __gdth_execute(sdev, gdtcmd, cmnd, timeout, info);
491
492 scsi_free_host_dev(sdev);
493 return rval;
494}
495
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500496static void gdth_eval_mapping(u32 size, u32 *cyls, int *heads, int *secs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 *cyls = size /HEADS/SECS;
499 if (*cyls <= MAXCYLS) {
500 *heads = HEADS;
501 *secs = SECS;
502 } else { /* too high for 64*32 */
503 *cyls = size /MEDHEADS/MEDSECS;
504 if (*cyls <= MAXCYLS) {
505 *heads = MEDHEADS;
506 *secs = MEDSECS;
507 } else { /* too high for 127*63 */
508 *cyls = size /BIGHEADS/BIGSECS;
509 *heads = BIGHEADS;
510 *secs = BIGSECS;
511 }
512 }
513}
514
515/* controller search and initialization functions */
Christoph Hellwig706a5d42007-10-02 22:49:35 +0200516#ifdef CONFIG_EISA
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500517static int __init gdth_search_eisa(u16 eisa_adr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500519 u32 id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 TRACE(("gdth_search_eisa() adr. %x\n",eisa_adr));
522 id = inl(eisa_adr+ID0REG);
523 if (id == GDT3A_ID || id == GDT3B_ID) { /* GDT3000A or GDT3000B */
524 if ((inb(eisa_adr+EISAREG) & 8) == 0)
525 return 0; /* not EISA configured */
526 return 1;
527 }
528 if (id == GDT3_ID) /* GDT3000 */
529 return 1;
530
531 return 0;
532}
Christoph Hellwig706a5d42007-10-02 22:49:35 +0200533#endif /* CONFIG_EISA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Christoph Hellwigaed91cb2007-10-02 22:48:16 +0200535#ifdef CONFIG_ISA
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500536static int __init gdth_search_isa(u32 bios_adr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 void __iomem *addr;
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500539 u32 id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 TRACE(("gdth_search_isa() bios adr. %x\n",bios_adr));
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500542 if ((addr = ioremap(bios_adr+BIOS_ID_OFFS, sizeof(u32))) != NULL) {
Jeff Garzika52667f2007-10-02 22:55:53 +0200543 id = readl(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 iounmap(addr);
545 if (id == GDT2_ID) /* GDT2000 */
546 return 1;
547 }
548 return 0;
549}
Christoph Hellwigaed91cb2007-10-02 22:48:16 +0200550#endif /* CONFIG_ISA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Christoph Hellwig8514ef22007-10-02 22:51:06 +0200552#ifdef CONFIG_PCI
Christoph Hellwig8514ef22007-10-02 22:51:06 +0200553
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500554static bool gdth_search_vortex(u16 device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Jeff Garzikcff26802008-02-15 22:20:09 -0500556 if (device <= PCI_DEVICE_ID_VORTEX_GDT6555)
557 return true;
558 if (device >= PCI_DEVICE_ID_VORTEX_GDT6x17RP &&
559 device <= PCI_DEVICE_ID_VORTEX_GDTMAXRP)
560 return true;
561 if (device == PCI_DEVICE_ID_VORTEX_GDTNEWRX ||
562 device == PCI_DEVICE_ID_VORTEX_GDTNEWRX2)
563 return true;
564 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Jeff Garzikcff26802008-02-15 22:20:09 -0500567static int gdth_pci_probe_one(gdth_pci_str *pcistr, gdth_ha_str **ha_out);
568static int gdth_pci_init_one(struct pci_dev *pdev,
569 const struct pci_device_id *ent);
570static void gdth_pci_remove_one(struct pci_dev *pdev);
571static void gdth_remove_one(gdth_ha_str *ha);
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573/* Vortex only makes RAID controllers.
574 * We do not really want to specify all 550 ids here, so wildcard match.
575 */
Jeff Garzikcff26802008-02-15 22:20:09 -0500576static const struct pci_device_id gdthtable[] = {
577 { PCI_VDEVICE(VORTEX, PCI_ANY_ID) },
578 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_SRC) },
579 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_SRC_XSCALE) },
580 { } /* terminate list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581};
Jeff Garzikcff26802008-02-15 22:20:09 -0500582MODULE_DEVICE_TABLE(pci, gdthtable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Jeff Garzikcff26802008-02-15 22:20:09 -0500584static struct pci_driver gdth_pci_driver = {
585 .name = "gdth",
586 .id_table = gdthtable,
587 .probe = gdth_pci_init_one,
588 .remove = gdth_pci_remove_one,
589};
590
Al Viroced71722008-11-22 17:35:14 +0000591static void __devexit gdth_pci_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Jeff Garzikcff26802008-02-15 22:20:09 -0500593 gdth_ha_str *ha = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Jeff Garzikcff26802008-02-15 22:20:09 -0500595 pci_set_drvdata(pdev, NULL);
596
597 list_del(&ha->list);
598 gdth_remove_one(ha);
599
600 pci_disable_device(pdev);
601}
602
Al Viroced71722008-11-22 17:35:14 +0000603static int __devinit gdth_pci_init_one(struct pci_dev *pdev,
Jeff Garzikcff26802008-02-15 22:20:09 -0500604 const struct pci_device_id *ent)
605{
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500606 u16 vendor = pdev->vendor;
607 u16 device = pdev->device;
608 unsigned long base0, base1, base2;
Jeff Garzikcff26802008-02-15 22:20:09 -0500609 int rc;
610 gdth_pci_str gdth_pcistr;
611 gdth_ha_str *ha = NULL;
612
613 TRACE(("gdth_search_dev() cnt %d vendor %x device %x\n",
614 gdth_ctr_count, vendor, device));
615
616 memset(&gdth_pcistr, 0, sizeof(gdth_pcistr));
617
618 if (vendor == PCI_VENDOR_ID_VORTEX && !gdth_search_vortex(device))
619 return -ENODEV;
620
621 rc = pci_enable_device(pdev);
622 if (rc)
623 return rc;
624
625 if (gdth_ctr_count >= MAXHA)
626 return -EBUSY;
Sergio Luis99109302008-02-12 20:48:03 -0300627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 /* GDT PCI controller found, resources are already in pdev */
Jeff Garzikcff26802008-02-15 22:20:09 -0500629 gdth_pcistr.pdev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 base0 = pci_resource_flags(pdev, 0);
631 base1 = pci_resource_flags(pdev, 1);
632 base2 = pci_resource_flags(pdev, 2);
633 if (device <= PCI_DEVICE_ID_VORTEX_GDT6000B || /* GDT6000/B */
634 device >= PCI_DEVICE_ID_VORTEX_GDT6x17RP) { /* MPR */
635 if (!(base0 & IORESOURCE_MEM))
Jeff Garzikcff26802008-02-15 22:20:09 -0500636 return -ENODEV;
637 gdth_pcistr.dpmem = pci_resource_start(pdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 } else { /* GDT6110, GDT6120, .. */
639 if (!(base0 & IORESOURCE_MEM) ||
640 !(base2 & IORESOURCE_MEM) ||
641 !(base1 & IORESOURCE_IO))
Jeff Garzikcff26802008-02-15 22:20:09 -0500642 return -ENODEV;
643 gdth_pcistr.dpmem = pci_resource_start(pdev, 2);
644 gdth_pcistr.io = pci_resource_start(pdev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646 TRACE2(("Controller found at %d/%d, irq %d, dpmem 0x%lx\n",
Jeff Garzikcff26802008-02-15 22:20:09 -0500647 gdth_pcistr.pdev->bus->number,
648 PCI_SLOT(gdth_pcistr.pdev->devfn),
649 gdth_pcistr.irq,
650 gdth_pcistr.dpmem));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Jeff Garzikcff26802008-02-15 22:20:09 -0500652 rc = gdth_pci_probe_one(&gdth_pcistr, &ha);
653 if (rc)
654 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Jeff Garzikcff26802008-02-15 22:20:09 -0500656 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
Christoph Hellwig8514ef22007-10-02 22:51:06 +0200658#endif /* CONFIG_PCI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Christoph Hellwig706a5d42007-10-02 22:49:35 +0200660#ifdef CONFIG_EISA
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500661static int __init gdth_init_eisa(u16 eisa_adr,gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500663 u32 retries,id;
664 u8 prot_ver,eisacf,i,irq_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 TRACE(("gdth_init_eisa() adr. %x\n",eisa_adr));
667
668 /* disable board interrupts, deinitialize services */
669 outb(0xff,eisa_adr+EDOORREG);
670 outb(0x00,eisa_adr+EDENABREG);
671 outb(0x00,eisa_adr+EINTENABREG);
672
673 outb(0xff,eisa_adr+LDOORREG);
674 retries = INIT_RETRIES;
675 gdth_delay(20);
676 while (inb(eisa_adr+EDOORREG) != 0xff) {
677 if (--retries == 0) {
678 printk("GDT-EISA: Initialization error (DEINIT failed)\n");
679 return 0;
680 }
681 gdth_delay(1);
682 TRACE2(("wait for DEINIT: retries=%d\n",retries));
683 }
684 prot_ver = inb(eisa_adr+MAILBOXREG);
685 outb(0xff,eisa_adr+EDOORREG);
686 if (prot_ver != PROTOCOL_VERSION) {
687 printk("GDT-EISA: Illegal protocol version\n");
688 return 0;
689 }
690 ha->bmic = eisa_adr;
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500691 ha->brd_phys = (u32)eisa_adr >> 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 outl(0,eisa_adr+MAILBOXREG);
694 outl(0,eisa_adr+MAILBOXREG+4);
695 outl(0,eisa_adr+MAILBOXREG+8);
696 outl(0,eisa_adr+MAILBOXREG+12);
697
698 /* detect IRQ */
699 if ((id = inl(eisa_adr+ID0REG)) == GDT3_ID) {
700 ha->oem_id = OEM_ID_ICP;
701 ha->type = GDT_EISA;
702 ha->stype = id;
703 outl(1,eisa_adr+MAILBOXREG+8);
704 outb(0xfe,eisa_adr+LDOORREG);
705 retries = INIT_RETRIES;
706 gdth_delay(20);
707 while (inb(eisa_adr+EDOORREG) != 0xfe) {
708 if (--retries == 0) {
709 printk("GDT-EISA: Initialization error (get IRQ failed)\n");
710 return 0;
711 }
712 gdth_delay(1);
713 }
714 ha->irq = inb(eisa_adr+MAILBOXREG);
715 outb(0xff,eisa_adr+EDOORREG);
716 TRACE2(("GDT3000/3020: IRQ=%d\n",ha->irq));
717 /* check the result */
718 if (ha->irq == 0) {
719 TRACE2(("Unknown IRQ, use IRQ table from cmd line !\n"));
720 for (i = 0, irq_found = FALSE;
721 i < MAXHA && irq[i] != 0xff; ++i) {
722 if (irq[i]==10 || irq[i]==11 || irq[i]==12 || irq[i]==14) {
723 irq_found = TRUE;
724 break;
725 }
726 }
727 if (irq_found) {
728 ha->irq = irq[i];
729 irq[i] = 0;
730 printk("GDT-EISA: Can not detect controller IRQ,\n");
731 printk("Use IRQ setting from command line (IRQ = %d)\n",
732 ha->irq);
733 } else {
734 printk("GDT-EISA: Initialization error (unknown IRQ), Enable\n");
735 printk("the controller BIOS or use command line parameters\n");
736 return 0;
737 }
738 }
739 } else {
740 eisacf = inb(eisa_adr+EISAREG) & 7;
741 if (eisacf > 4) /* level triggered */
742 eisacf -= 4;
743 ha->irq = gdth_irq_tab[eisacf];
744 ha->oem_id = OEM_ID_ICP;
745 ha->type = GDT_EISA;
746 ha->stype = id;
747 }
748
749 ha->dma64_support = 0;
750 return 1;
751}
Christoph Hellwig706a5d42007-10-02 22:49:35 +0200752#endif /* CONFIG_EISA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Christoph Hellwigaed91cb2007-10-02 22:48:16 +0200754#ifdef CONFIG_ISA
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500755static int __init gdth_init_isa(u32 bios_adr,gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 register gdt2_dpram_str __iomem *dp2_ptr;
758 int i;
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500759 u8 irq_drq,prot_ver;
760 u32 retries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 TRACE(("gdth_init_isa() bios adr. %x\n",bios_adr));
763
764 ha->brd = ioremap(bios_adr, sizeof(gdt2_dpram_str));
765 if (ha->brd == NULL) {
766 printk("GDT-ISA: Initialization error (DPMEM remap error)\n");
767 return 0;
768 }
769 dp2_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +0200770 writeb(1, &dp2_ptr->io.memlock); /* switch off write protection */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 /* reset interface area */
772 memset_io(&dp2_ptr->u, 0, sizeof(dp2_ptr->u));
Jeff Garzika52667f2007-10-02 22:55:53 +0200773 if (readl(&dp2_ptr->u) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 printk("GDT-ISA: Initialization error (DPMEM write error)\n");
775 iounmap(ha->brd);
776 return 0;
777 }
778
779 /* disable board interrupts, read DRQ and IRQ */
Jeff Garzika52667f2007-10-02 22:55:53 +0200780 writeb(0xff, &dp2_ptr->io.irqdel);
781 writeb(0x00, &dp2_ptr->io.irqen);
782 writeb(0x00, &dp2_ptr->u.ic.S_Status);
783 writeb(0x00, &dp2_ptr->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Jeff Garzika52667f2007-10-02 22:55:53 +0200785 irq_drq = readb(&dp2_ptr->io.rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 for (i=0; i<3; ++i) {
787 if ((irq_drq & 1)==0)
788 break;
789 irq_drq >>= 1;
790 }
791 ha->drq = gdth_drq_tab[i];
792
Jeff Garzika52667f2007-10-02 22:55:53 +0200793 irq_drq = readb(&dp2_ptr->io.rq) >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 for (i=1; i<5; ++i) {
795 if ((irq_drq & 1)==0)
796 break;
797 irq_drq >>= 1;
798 }
799 ha->irq = gdth_irq_tab[i];
800
801 /* deinitialize services */
Jeff Garzika52667f2007-10-02 22:55:53 +0200802 writel(bios_adr, &dp2_ptr->u.ic.S_Info[0]);
803 writeb(0xff, &dp2_ptr->u.ic.S_Cmd_Indx);
804 writeb(0, &dp2_ptr->io.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 retries = INIT_RETRIES;
806 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +0200807 while (readb(&dp2_ptr->u.ic.S_Status) != 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (--retries == 0) {
809 printk("GDT-ISA: Initialization error (DEINIT failed)\n");
810 iounmap(ha->brd);
811 return 0;
812 }
813 gdth_delay(1);
814 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500815 prot_ver = (u8)readl(&dp2_ptr->u.ic.S_Info[0]);
Jeff Garzika52667f2007-10-02 22:55:53 +0200816 writeb(0, &dp2_ptr->u.ic.Status);
817 writeb(0xff, &dp2_ptr->io.irqdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (prot_ver != PROTOCOL_VERSION) {
819 printk("GDT-ISA: Illegal protocol version\n");
820 iounmap(ha->brd);
821 return 0;
822 }
823
824 ha->oem_id = OEM_ID_ICP;
825 ha->type = GDT_ISA;
826 ha->ic_all_size = sizeof(dp2_ptr->u);
827 ha->stype= GDT2_ID;
828 ha->brd_phys = bios_adr >> 4;
829
830 /* special request to controller BIOS */
Jeff Garzika52667f2007-10-02 22:55:53 +0200831 writel(0x00, &dp2_ptr->u.ic.S_Info[0]);
832 writel(0x00, &dp2_ptr->u.ic.S_Info[1]);
833 writel(0x01, &dp2_ptr->u.ic.S_Info[2]);
834 writel(0x00, &dp2_ptr->u.ic.S_Info[3]);
835 writeb(0xfe, &dp2_ptr->u.ic.S_Cmd_Indx);
836 writeb(0, &dp2_ptr->io.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 retries = INIT_RETRIES;
838 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +0200839 while (readb(&dp2_ptr->u.ic.S_Status) != 0xfe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (--retries == 0) {
841 printk("GDT-ISA: Initialization error\n");
842 iounmap(ha->brd);
843 return 0;
844 }
845 gdth_delay(1);
846 }
Jeff Garzika52667f2007-10-02 22:55:53 +0200847 writeb(0, &dp2_ptr->u.ic.Status);
848 writeb(0xff, &dp2_ptr->io.irqdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 ha->dma64_support = 0;
851 return 1;
852}
Christoph Hellwigaed91cb2007-10-02 22:48:16 +0200853#endif /* CONFIG_ISA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Christoph Hellwig8514ef22007-10-02 22:51:06 +0200855#ifdef CONFIG_PCI
Al Viroced71722008-11-22 17:35:14 +0000856static int __devinit gdth_init_pci(struct pci_dev *pdev, gdth_pci_str *pcistr,
Jeff Garzikcff26802008-02-15 22:20:09 -0500857 gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
859 register gdt6_dpram_str __iomem *dp6_ptr;
860 register gdt6c_dpram_str __iomem *dp6c_ptr;
861 register gdt6m_dpram_str __iomem *dp6m_ptr;
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500862 u32 retries;
863 u8 prot_ver;
864 u16 command;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 int i, found = FALSE;
866
867 TRACE(("gdth_init_pci()\n"));
868
Jeff Garzik4c9c8d72008-02-15 21:35:26 -0500869 if (pdev->vendor == PCI_VENDOR_ID_INTEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 ha->oem_id = OEM_ID_INTEL;
871 else
872 ha->oem_id = OEM_ID_ICP;
Jeff Garzik4c9c8d72008-02-15 21:35:26 -0500873 ha->brd_phys = (pdev->bus->number << 8) | (pdev->devfn & 0xf8);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500874 ha->stype = (u32)pdev->device;
Jeff Garzik4c9c8d72008-02-15 21:35:26 -0500875 ha->irq = pdev->irq;
876 ha->pdev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Jeff Garzik8e9a8a02007-07-17 05:25:17 -0400878 if (ha->pdev->device <= PCI_DEVICE_ID_VORTEX_GDT6000B) { /* GDT6000/B */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 TRACE2(("init_pci() dpmem %lx irq %d\n",pcistr->dpmem,ha->irq));
880 ha->brd = ioremap(pcistr->dpmem, sizeof(gdt6_dpram_str));
881 if (ha->brd == NULL) {
882 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
883 return 0;
884 }
885 /* check and reset interface area */
886 dp6_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +0200887 writel(DPMEM_MAGIC, &dp6_ptr->u);
888 if (readl(&dp6_ptr->u) != DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 printk("GDT-PCI: Cannot access DPMEM at 0x%lx (shadowed?)\n",
890 pcistr->dpmem);
891 found = FALSE;
892 for (i = 0xC8000; i < 0xE8000; i += 0x4000) {
893 iounmap(ha->brd);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500894 ha->brd = ioremap(i, sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (ha->brd == NULL) {
896 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
897 return 0;
898 }
Jeff Garzika52667f2007-10-02 22:55:53 +0200899 if (readw(ha->brd) != 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 TRACE2(("init_pci_old() address 0x%x busy\n", i));
901 continue;
902 }
903 iounmap(ha->brd);
Jeff Garzik4c9c8d72008-02-15 21:35:26 -0500904 pci_write_config_dword(pdev, PCI_BASE_ADDRESS_0, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 ha->brd = ioremap(i, sizeof(gdt6_dpram_str));
906 if (ha->brd == NULL) {
907 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
908 return 0;
909 }
910 dp6_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +0200911 writel(DPMEM_MAGIC, &dp6_ptr->u);
912 if (readl(&dp6_ptr->u) == DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 printk("GDT-PCI: Use free address at 0x%x\n", i);
914 found = TRUE;
915 break;
916 }
917 }
918 if (!found) {
919 printk("GDT-PCI: No free address found!\n");
920 iounmap(ha->brd);
921 return 0;
922 }
923 }
924 memset_io(&dp6_ptr->u, 0, sizeof(dp6_ptr->u));
Jeff Garzika52667f2007-10-02 22:55:53 +0200925 if (readl(&dp6_ptr->u) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 printk("GDT-PCI: Initialization error (DPMEM write error)\n");
927 iounmap(ha->brd);
928 return 0;
929 }
930
931 /* disable board interrupts, deinit services */
Jeff Garzika52667f2007-10-02 22:55:53 +0200932 writeb(0xff, &dp6_ptr->io.irqdel);
933 writeb(0x00, &dp6_ptr->io.irqen);
934 writeb(0x00, &dp6_ptr->u.ic.S_Status);
935 writeb(0x00, &dp6_ptr->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Jeff Garzika52667f2007-10-02 22:55:53 +0200937 writel(pcistr->dpmem, &dp6_ptr->u.ic.S_Info[0]);
938 writeb(0xff, &dp6_ptr->u.ic.S_Cmd_Indx);
939 writeb(0, &dp6_ptr->io.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 retries = INIT_RETRIES;
941 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +0200942 while (readb(&dp6_ptr->u.ic.S_Status) != 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (--retries == 0) {
944 printk("GDT-PCI: Initialization error (DEINIT failed)\n");
945 iounmap(ha->brd);
946 return 0;
947 }
948 gdth_delay(1);
949 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500950 prot_ver = (u8)readl(&dp6_ptr->u.ic.S_Info[0]);
Jeff Garzika52667f2007-10-02 22:55:53 +0200951 writeb(0, &dp6_ptr->u.ic.S_Status);
952 writeb(0xff, &dp6_ptr->io.irqdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (prot_ver != PROTOCOL_VERSION) {
954 printk("GDT-PCI: Illegal protocol version\n");
955 iounmap(ha->brd);
956 return 0;
957 }
958
959 ha->type = GDT_PCI;
960 ha->ic_all_size = sizeof(dp6_ptr->u);
961
962 /* special command to controller BIOS */
Jeff Garzika52667f2007-10-02 22:55:53 +0200963 writel(0x00, &dp6_ptr->u.ic.S_Info[0]);
964 writel(0x00, &dp6_ptr->u.ic.S_Info[1]);
965 writel(0x00, &dp6_ptr->u.ic.S_Info[2]);
966 writel(0x00, &dp6_ptr->u.ic.S_Info[3]);
967 writeb(0xfe, &dp6_ptr->u.ic.S_Cmd_Indx);
968 writeb(0, &dp6_ptr->io.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 retries = INIT_RETRIES;
970 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +0200971 while (readb(&dp6_ptr->u.ic.S_Status) != 0xfe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (--retries == 0) {
973 printk("GDT-PCI: Initialization error\n");
974 iounmap(ha->brd);
975 return 0;
976 }
977 gdth_delay(1);
978 }
Jeff Garzika52667f2007-10-02 22:55:53 +0200979 writeb(0, &dp6_ptr->u.ic.S_Status);
980 writeb(0xff, &dp6_ptr->io.irqdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 ha->dma64_support = 0;
983
Jeff Garzik8e9a8a02007-07-17 05:25:17 -0400984 } else if (ha->pdev->device <= PCI_DEVICE_ID_VORTEX_GDT6555) { /* GDT6110, ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 ha->plx = (gdt6c_plx_regs *)pcistr->io;
986 TRACE2(("init_pci_new() dpmem %lx irq %d\n",
987 pcistr->dpmem,ha->irq));
988 ha->brd = ioremap(pcistr->dpmem, sizeof(gdt6c_dpram_str));
989 if (ha->brd == NULL) {
990 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
991 iounmap(ha->brd);
992 return 0;
993 }
994 /* check and reset interface area */
995 dp6c_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +0200996 writel(DPMEM_MAGIC, &dp6c_ptr->u);
997 if (readl(&dp6c_ptr->u) != DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 printk("GDT-PCI: Cannot access DPMEM at 0x%lx (shadowed?)\n",
999 pcistr->dpmem);
1000 found = FALSE;
1001 for (i = 0xC8000; i < 0xE8000; i += 0x4000) {
1002 iounmap(ha->brd);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001003 ha->brd = ioremap(i, sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (ha->brd == NULL) {
1005 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1006 return 0;
1007 }
Jeff Garzika52667f2007-10-02 22:55:53 +02001008 if (readw(ha->brd) != 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 TRACE2(("init_pci_plx() address 0x%x busy\n", i));
1010 continue;
1011 }
1012 iounmap(ha->brd);
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05001013 pci_write_config_dword(pdev, PCI_BASE_ADDRESS_2, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 ha->brd = ioremap(i, sizeof(gdt6c_dpram_str));
1015 if (ha->brd == NULL) {
1016 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1017 return 0;
1018 }
1019 dp6c_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001020 writel(DPMEM_MAGIC, &dp6c_ptr->u);
1021 if (readl(&dp6c_ptr->u) == DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 printk("GDT-PCI: Use free address at 0x%x\n", i);
1023 found = TRUE;
1024 break;
1025 }
1026 }
1027 if (!found) {
1028 printk("GDT-PCI: No free address found!\n");
1029 iounmap(ha->brd);
1030 return 0;
1031 }
1032 }
1033 memset_io(&dp6c_ptr->u, 0, sizeof(dp6c_ptr->u));
Jeff Garzika52667f2007-10-02 22:55:53 +02001034 if (readl(&dp6c_ptr->u) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 printk("GDT-PCI: Initialization error (DPMEM write error)\n");
1036 iounmap(ha->brd);
1037 return 0;
1038 }
1039
1040 /* disable board interrupts, deinit services */
1041 outb(0x00,PTR2USHORT(&ha->plx->control1));
1042 outb(0xff,PTR2USHORT(&ha->plx->edoor_reg));
1043
Jeff Garzika52667f2007-10-02 22:55:53 +02001044 writeb(0x00, &dp6c_ptr->u.ic.S_Status);
1045 writeb(0x00, &dp6c_ptr->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Jeff Garzika52667f2007-10-02 22:55:53 +02001047 writel(pcistr->dpmem, &dp6c_ptr->u.ic.S_Info[0]);
1048 writeb(0xff, &dp6c_ptr->u.ic.S_Cmd_Indx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 outb(1,PTR2USHORT(&ha->plx->ldoor_reg));
1051
1052 retries = INIT_RETRIES;
1053 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +02001054 while (readb(&dp6c_ptr->u.ic.S_Status) != 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (--retries == 0) {
1056 printk("GDT-PCI: Initialization error (DEINIT failed)\n");
1057 iounmap(ha->brd);
1058 return 0;
1059 }
1060 gdth_delay(1);
1061 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001062 prot_ver = (u8)readl(&dp6c_ptr->u.ic.S_Info[0]);
Jeff Garzika52667f2007-10-02 22:55:53 +02001063 writeb(0, &dp6c_ptr->u.ic.Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (prot_ver != PROTOCOL_VERSION) {
1065 printk("GDT-PCI: Illegal protocol version\n");
1066 iounmap(ha->brd);
1067 return 0;
1068 }
1069
1070 ha->type = GDT_PCINEW;
1071 ha->ic_all_size = sizeof(dp6c_ptr->u);
1072
1073 /* special command to controller BIOS */
Jeff Garzika52667f2007-10-02 22:55:53 +02001074 writel(0x00, &dp6c_ptr->u.ic.S_Info[0]);
1075 writel(0x00, &dp6c_ptr->u.ic.S_Info[1]);
1076 writel(0x00, &dp6c_ptr->u.ic.S_Info[2]);
1077 writel(0x00, &dp6c_ptr->u.ic.S_Info[3]);
1078 writeb(0xfe, &dp6c_ptr->u.ic.S_Cmd_Indx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 outb(1,PTR2USHORT(&ha->plx->ldoor_reg));
1081
1082 retries = INIT_RETRIES;
1083 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +02001084 while (readb(&dp6c_ptr->u.ic.S_Status) != 0xfe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (--retries == 0) {
1086 printk("GDT-PCI: Initialization error\n");
1087 iounmap(ha->brd);
1088 return 0;
1089 }
1090 gdth_delay(1);
1091 }
Jeff Garzika52667f2007-10-02 22:55:53 +02001092 writeb(0, &dp6c_ptr->u.ic.S_Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 ha->dma64_support = 0;
1095
1096 } else { /* MPR */
1097 TRACE2(("init_pci_mpr() dpmem %lx irq %d\n",pcistr->dpmem,ha->irq));
1098 ha->brd = ioremap(pcistr->dpmem, sizeof(gdt6m_dpram_str));
1099 if (ha->brd == NULL) {
1100 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1101 return 0;
1102 }
1103
1104 /* manipulate config. space to enable DPMEM, start RP controller */
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05001105 pci_read_config_word(pdev, PCI_COMMAND, &command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 command |= 6;
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05001107 pci_write_config_word(pdev, PCI_COMMAND, command);
1108 if (pci_resource_start(pdev, 8) == 1UL)
1109 pci_resource_start(pdev, 8) = 0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 i = 0xFEFF0001UL;
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05001111 pci_write_config_dword(pdev, PCI_ROM_ADDRESS, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 gdth_delay(1);
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05001113 pci_write_config_dword(pdev, PCI_ROM_ADDRESS,
1114 pci_resource_start(pdev, 8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 dp6m_ptr = ha->brd;
1117
1118 /* Ensure that it is safe to access the non HW portions of DPMEM.
1119 * Aditional check needed for Xscale based RAID controllers */
Jeff Garzika52667f2007-10-02 22:55:53 +02001120 while( ((int)readb(&dp6m_ptr->i960r.sema0_reg) ) & 3 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 gdth_delay(1);
1122
1123 /* check and reset interface area */
Jeff Garzika52667f2007-10-02 22:55:53 +02001124 writel(DPMEM_MAGIC, &dp6m_ptr->u);
1125 if (readl(&dp6m_ptr->u) != DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 printk("GDT-PCI: Cannot access DPMEM at 0x%lx (shadowed?)\n",
1127 pcistr->dpmem);
1128 found = FALSE;
1129 for (i = 0xC8000; i < 0xE8000; i += 0x4000) {
1130 iounmap(ha->brd);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001131 ha->brd = ioremap(i, sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 if (ha->brd == NULL) {
1133 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1134 return 0;
1135 }
Jeff Garzika52667f2007-10-02 22:55:53 +02001136 if (readw(ha->brd) != 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 TRACE2(("init_pci_mpr() address 0x%x busy\n", i));
1138 continue;
1139 }
1140 iounmap(ha->brd);
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05001141 pci_write_config_dword(pdev, PCI_BASE_ADDRESS_0, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 ha->brd = ioremap(i, sizeof(gdt6m_dpram_str));
1143 if (ha->brd == NULL) {
1144 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1145 return 0;
1146 }
1147 dp6m_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001148 writel(DPMEM_MAGIC, &dp6m_ptr->u);
1149 if (readl(&dp6m_ptr->u) == DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 printk("GDT-PCI: Use free address at 0x%x\n", i);
1151 found = TRUE;
1152 break;
1153 }
1154 }
1155 if (!found) {
1156 printk("GDT-PCI: No free address found!\n");
1157 iounmap(ha->brd);
1158 return 0;
1159 }
1160 }
1161 memset_io(&dp6m_ptr->u, 0, sizeof(dp6m_ptr->u));
1162
1163 /* disable board interrupts, deinit services */
Jeff Garzika52667f2007-10-02 22:55:53 +02001164 writeb(readb(&dp6m_ptr->i960r.edoor_en_reg) | 4,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 &dp6m_ptr->i960r.edoor_en_reg);
Jeff Garzika52667f2007-10-02 22:55:53 +02001166 writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
1167 writeb(0x00, &dp6m_ptr->u.ic.S_Status);
1168 writeb(0x00, &dp6m_ptr->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Jeff Garzika52667f2007-10-02 22:55:53 +02001170 writel(pcistr->dpmem, &dp6m_ptr->u.ic.S_Info[0]);
1171 writeb(0xff, &dp6m_ptr->u.ic.S_Cmd_Indx);
1172 writeb(1, &dp6m_ptr->i960r.ldoor_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 retries = INIT_RETRIES;
1174 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +02001175 while (readb(&dp6m_ptr->u.ic.S_Status) != 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (--retries == 0) {
1177 printk("GDT-PCI: Initialization error (DEINIT failed)\n");
1178 iounmap(ha->brd);
1179 return 0;
1180 }
1181 gdth_delay(1);
1182 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001183 prot_ver = (u8)readl(&dp6m_ptr->u.ic.S_Info[0]);
Jeff Garzika52667f2007-10-02 22:55:53 +02001184 writeb(0, &dp6m_ptr->u.ic.S_Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (prot_ver != PROTOCOL_VERSION) {
1186 printk("GDT-PCI: Illegal protocol version\n");
1187 iounmap(ha->brd);
1188 return 0;
1189 }
1190
1191 ha->type = GDT_PCIMPR;
1192 ha->ic_all_size = sizeof(dp6m_ptr->u);
1193
1194 /* special command to controller BIOS */
Jeff Garzika52667f2007-10-02 22:55:53 +02001195 writel(0x00, &dp6m_ptr->u.ic.S_Info[0]);
1196 writel(0x00, &dp6m_ptr->u.ic.S_Info[1]);
1197 writel(0x00, &dp6m_ptr->u.ic.S_Info[2]);
1198 writel(0x00, &dp6m_ptr->u.ic.S_Info[3]);
1199 writeb(0xfe, &dp6m_ptr->u.ic.S_Cmd_Indx);
1200 writeb(1, &dp6m_ptr->i960r.ldoor_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 retries = INIT_RETRIES;
1202 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +02001203 while (readb(&dp6m_ptr->u.ic.S_Status) != 0xfe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (--retries == 0) {
1205 printk("GDT-PCI: Initialization error\n");
1206 iounmap(ha->brd);
1207 return 0;
1208 }
1209 gdth_delay(1);
1210 }
Jeff Garzika52667f2007-10-02 22:55:53 +02001211 writeb(0, &dp6m_ptr->u.ic.S_Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 /* read FW version to detect 64-bit DMA support */
Jeff Garzika52667f2007-10-02 22:55:53 +02001214 writeb(0xfd, &dp6m_ptr->u.ic.S_Cmd_Indx);
1215 writeb(1, &dp6m_ptr->i960r.ldoor_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 retries = INIT_RETRIES;
1217 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +02001218 while (readb(&dp6m_ptr->u.ic.S_Status) != 0xfd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (--retries == 0) {
1220 printk("GDT-PCI: Initialization error (DEINIT failed)\n");
1221 iounmap(ha->brd);
1222 return 0;
1223 }
1224 gdth_delay(1);
1225 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001226 prot_ver = (u8)(readl(&dp6m_ptr->u.ic.S_Info[0]) >> 16);
Jeff Garzika52667f2007-10-02 22:55:53 +02001227 writeb(0, &dp6m_ptr->u.ic.S_Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (prot_ver < 0x2b) /* FW < x.43: no 64-bit DMA support */
1229 ha->dma64_support = 0;
1230 else
1231 ha->dma64_support = 1;
1232 }
1233
1234 return 1;
1235}
Christoph Hellwig8514ef22007-10-02 22:51:06 +02001236#endif /* CONFIG_PCI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238/* controller protocol functions */
1239
Al Viroced71722008-11-22 17:35:14 +00001240static void __devinit gdth_enable_int(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001242 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 gdt2_dpram_str __iomem *dp2_ptr;
1244 gdt6_dpram_str __iomem *dp6_ptr;
1245 gdt6m_dpram_str __iomem *dp6m_ptr;
1246
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001247 TRACE(("gdth_enable_int() hanum %d\n",ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 spin_lock_irqsave(&ha->smp_lock, flags);
1249
1250 if (ha->type == GDT_EISA) {
1251 outb(0xff, ha->bmic + EDOORREG);
1252 outb(0xff, ha->bmic + EDENABREG);
1253 outb(0x01, ha->bmic + EINTENABREG);
1254 } else if (ha->type == GDT_ISA) {
1255 dp2_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001256 writeb(1, &dp2_ptr->io.irqdel);
1257 writeb(0, &dp2_ptr->u.ic.Cmd_Index);
1258 writeb(1, &dp2_ptr->io.irqen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 } else if (ha->type == GDT_PCI) {
1260 dp6_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001261 writeb(1, &dp6_ptr->io.irqdel);
1262 writeb(0, &dp6_ptr->u.ic.Cmd_Index);
1263 writeb(1, &dp6_ptr->io.irqen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 } else if (ha->type == GDT_PCINEW) {
1265 outb(0xff, PTR2USHORT(&ha->plx->edoor_reg));
1266 outb(0x03, PTR2USHORT(&ha->plx->control1));
1267 } else if (ha->type == GDT_PCIMPR) {
1268 dp6m_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001269 writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
1270 writeb(readb(&dp6m_ptr->i960r.edoor_en_reg) & ~4,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 &dp6m_ptr->i960r.edoor_en_reg);
1272 }
1273 spin_unlock_irqrestore(&ha->smp_lock, flags);
1274}
1275
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001276/* return IStatus if interrupt was from this card else 0 */
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001277static u8 gdth_get_status(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001279 u8 IStatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Jeff Garzik230e8862007-12-13 16:14:12 -08001281 TRACE(("gdth_get_status() irq %d ctr_count %d\n", ha->irq, gdth_ctr_count));
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (ha->type == GDT_EISA)
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001284 IStatus = inb((u16)ha->bmic + EDOORREG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 else if (ha->type == GDT_ISA)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001286 IStatus =
Jeff Garzika52667f2007-10-02 22:55:53 +02001287 readb(&((gdt2_dpram_str __iomem *)ha->brd)->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 else if (ha->type == GDT_PCI)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001289 IStatus =
Jeff Garzika52667f2007-10-02 22:55:53 +02001290 readb(&((gdt6_dpram_str __iomem *)ha->brd)->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 else if (ha->type == GDT_PCINEW)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001292 IStatus = inb(PTR2USHORT(&ha->plx->edoor_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 else if (ha->type == GDT_PCIMPR)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001294 IStatus =
Jeff Garzika52667f2007-10-02 22:55:53 +02001295 readb(&((gdt6m_dpram_str __iomem *)ha->brd)->i960r.edoor_reg);
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001296
1297 return IStatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001299
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001300static int gdth_test_busy(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 register int gdtsema0 = 0;
1303
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001304 TRACE(("gdth_test_busy() hanum %d\n", ha->hanum));
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 if (ha->type == GDT_EISA)
1307 gdtsema0 = (int)inb(ha->bmic + SEMA0REG);
1308 else if (ha->type == GDT_ISA)
Jeff Garzika52667f2007-10-02 22:55:53 +02001309 gdtsema0 = (int)readb(&((gdt2_dpram_str __iomem *)ha->brd)->u.ic.Sema0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 else if (ha->type == GDT_PCI)
Jeff Garzika52667f2007-10-02 22:55:53 +02001311 gdtsema0 = (int)readb(&((gdt6_dpram_str __iomem *)ha->brd)->u.ic.Sema0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 else if (ha->type == GDT_PCINEW)
1313 gdtsema0 = (int)inb(PTR2USHORT(&ha->plx->sema0_reg));
1314 else if (ha->type == GDT_PCIMPR)
1315 gdtsema0 =
Jeff Garzika52667f2007-10-02 22:55:53 +02001316 (int)readb(&((gdt6m_dpram_str __iomem *)ha->brd)->i960r.sema0_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 return (gdtsema0 & 1);
1319}
1320
1321
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001322static int gdth_get_cmd_index(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 int i;
1325
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001326 TRACE(("gdth_get_cmd_index() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 for (i=0; i<GDTH_MAXCMDS; ++i) {
1329 if (ha->cmd_tab[i].cmnd == UNUSED_CMND) {
1330 ha->cmd_tab[i].cmnd = ha->pccb->RequestBuffer;
1331 ha->cmd_tab[i].service = ha->pccb->Service;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001332 ha->pccb->CommandIndex = (u32)i+2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 return (i+2);
1334 }
1335 }
1336 return 0;
1337}
1338
1339
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001340static void gdth_set_sema0(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001342 TRACE(("gdth_set_sema0() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (ha->type == GDT_EISA) {
1345 outb(1, ha->bmic + SEMA0REG);
1346 } else if (ha->type == GDT_ISA) {
Jeff Garzika52667f2007-10-02 22:55:53 +02001347 writeb(1, &((gdt2_dpram_str __iomem *)ha->brd)->u.ic.Sema0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 } else if (ha->type == GDT_PCI) {
Jeff Garzika52667f2007-10-02 22:55:53 +02001349 writeb(1, &((gdt6_dpram_str __iomem *)ha->brd)->u.ic.Sema0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 } else if (ha->type == GDT_PCINEW) {
1351 outb(1, PTR2USHORT(&ha->plx->sema0_reg));
1352 } else if (ha->type == GDT_PCIMPR) {
Jeff Garzika52667f2007-10-02 22:55:53 +02001353 writeb(1, &((gdt6m_dpram_str __iomem *)ha->brd)->i960r.sema0_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355}
1356
1357
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001358static void gdth_copy_command(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 register gdth_cmd_str *cmd_ptr;
1361 register gdt6m_dpram_str __iomem *dp6m_ptr;
1362 register gdt6c_dpram_str __iomem *dp6c_ptr;
1363 gdt6_dpram_str __iomem *dp6_ptr;
1364 gdt2_dpram_str __iomem *dp2_ptr;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001365 u16 cp_count,dp_offset,cmd_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001367 TRACE(("gdth_copy_command() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 cp_count = ha->cmd_len;
1370 dp_offset= ha->cmd_offs_dpmem;
1371 cmd_no = ha->cmd_cnt;
1372 cmd_ptr = ha->pccb;
1373
1374 ++ha->cmd_cnt;
1375 if (ha->type == GDT_EISA)
1376 return; /* no DPMEM, no copy */
1377
1378 /* set cpcount dword aligned */
1379 if (cp_count & 3)
1380 cp_count += (4 - (cp_count & 3));
1381
1382 ha->cmd_offs_dpmem += cp_count;
1383
1384 /* set offset and service, copy command to DPMEM */
1385 if (ha->type == GDT_ISA) {
1386 dp2_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001387 writew(dp_offset + DPMEM_COMMAND_OFFSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 &dp2_ptr->u.ic.comm_queue[cmd_no].offset);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001389 writew((u16)cmd_ptr->Service,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 &dp2_ptr->u.ic.comm_queue[cmd_no].serv_id);
1391 memcpy_toio(&dp2_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
1392 } else if (ha->type == GDT_PCI) {
1393 dp6_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001394 writew(dp_offset + DPMEM_COMMAND_OFFSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 &dp6_ptr->u.ic.comm_queue[cmd_no].offset);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001396 writew((u16)cmd_ptr->Service,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 &dp6_ptr->u.ic.comm_queue[cmd_no].serv_id);
1398 memcpy_toio(&dp6_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
1399 } else if (ha->type == GDT_PCINEW) {
1400 dp6c_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001401 writew(dp_offset + DPMEM_COMMAND_OFFSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 &dp6c_ptr->u.ic.comm_queue[cmd_no].offset);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001403 writew((u16)cmd_ptr->Service,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 &dp6c_ptr->u.ic.comm_queue[cmd_no].serv_id);
1405 memcpy_toio(&dp6c_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
1406 } else if (ha->type == GDT_PCIMPR) {
1407 dp6m_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001408 writew(dp_offset + DPMEM_COMMAND_OFFSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 &dp6m_ptr->u.ic.comm_queue[cmd_no].offset);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001410 writew((u16)cmd_ptr->Service,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 &dp6m_ptr->u.ic.comm_queue[cmd_no].serv_id);
1412 memcpy_toio(&dp6m_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
1413 }
1414}
1415
1416
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001417static void gdth_release_event(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001419 TRACE(("gdth_release_event() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421#ifdef GDTH_STATISTICS
1422 {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001423 u32 i,j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 for (i=0,j=0; j<GDTH_MAXCMDS; ++j) {
1425 if (ha->cmd_tab[j].cmnd != UNUSED_CMND)
1426 ++i;
1427 }
1428 if (max_index < i) {
1429 max_index = i;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001430 TRACE3(("GDT: max_index = %d\n",(u16)i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 }
1432 }
1433#endif
1434
1435 if (ha->pccb->OpCode == GDT_INIT)
1436 ha->pccb->Service |= 0x80;
1437
1438 if (ha->type == GDT_EISA) {
1439 if (ha->pccb->OpCode == GDT_INIT) /* store DMA buffer */
1440 outl(ha->ccb_phys, ha->bmic + MAILBOXREG);
1441 outb(ha->pccb->Service, ha->bmic + LDOORREG);
1442 } else if (ha->type == GDT_ISA) {
Jeff Garzika52667f2007-10-02 22:55:53 +02001443 writeb(0, &((gdt2_dpram_str __iomem *)ha->brd)->io.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 } else if (ha->type == GDT_PCI) {
Jeff Garzika52667f2007-10-02 22:55:53 +02001445 writeb(0, &((gdt6_dpram_str __iomem *)ha->brd)->io.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 } else if (ha->type == GDT_PCINEW) {
1447 outb(1, PTR2USHORT(&ha->plx->ldoor_reg));
1448 } else if (ha->type == GDT_PCIMPR) {
Jeff Garzika52667f2007-10-02 22:55:53 +02001449 writeb(1, &((gdt6m_dpram_str __iomem *)ha->brd)->i960r.ldoor_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
1451}
1452
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001453static int gdth_wait(gdth_ha_str *ha, int index, u32 time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 int answer_found = FALSE;
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001456 int wait_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001458 TRACE(("gdth_wait() hanum %d index %d time %d\n", ha->hanum, index, time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (index == 0)
1461 return 1; /* no wait required */
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 do {
Jeff Garzik230e8862007-12-13 16:14:12 -08001464 __gdth_interrupt(ha, true, &wait_index);
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001465 if (wait_index == index) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 answer_found = TRUE;
1467 break;
1468 }
1469 gdth_delay(1);
1470 } while (--time);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001471
1472 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 gdth_delay(0);
1474
1475 return (answer_found);
1476}
1477
1478
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001479static int gdth_internal_cmd(gdth_ha_str *ha, u8 service, u16 opcode,
1480 u32 p1, u64 p2, u64 p3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 register gdth_cmd_str *cmd_ptr;
1483 int retries,index;
1484
1485 TRACE2(("gdth_internal_cmd() service %d opcode %d\n",service,opcode));
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 cmd_ptr = ha->pccb;
1488 memset((char*)cmd_ptr,0,sizeof(gdth_cmd_str));
1489
1490 /* make command */
1491 for (retries = INIT_RETRIES;;) {
1492 cmd_ptr->Service = service;
1493 cmd_ptr->RequestBuffer = INTERNAL_CMND;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001494 if (!(index=gdth_get_cmd_index(ha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 TRACE(("GDT: No free command index found\n"));
1496 return 0;
1497 }
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001498 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 cmd_ptr->OpCode = opcode;
1500 cmd_ptr->BoardNode = LOCALBOARD;
1501 if (service == CACHESERVICE) {
1502 if (opcode == GDT_IOCTL) {
1503 cmd_ptr->u.ioctl.subfunc = p1;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001504 cmd_ptr->u.ioctl.channel = (u32)p2;
1505 cmd_ptr->u.ioctl.param_size = (u16)p3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 cmd_ptr->u.ioctl.p_param = ha->scratch_phys;
1507 } else {
1508 if (ha->cache_feat & GDT_64BIT) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001509 cmd_ptr->u.cache64.DeviceNo = (u16)p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 cmd_ptr->u.cache64.BlockNo = p2;
1511 } else {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001512 cmd_ptr->u.cache.DeviceNo = (u16)p1;
1513 cmd_ptr->u.cache.BlockNo = (u32)p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 }
1515 }
1516 } else if (service == SCSIRAWSERVICE) {
1517 if (ha->raw_feat & GDT_64BIT) {
1518 cmd_ptr->u.raw64.direction = p1;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001519 cmd_ptr->u.raw64.bus = (u8)p2;
1520 cmd_ptr->u.raw64.target = (u8)p3;
1521 cmd_ptr->u.raw64.lun = (u8)(p3 >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 } else {
1523 cmd_ptr->u.raw.direction = p1;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001524 cmd_ptr->u.raw.bus = (u8)p2;
1525 cmd_ptr->u.raw.target = (u8)p3;
1526 cmd_ptr->u.raw.lun = (u8)(p3 >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
1528 } else if (service == SCREENSERVICE) {
1529 if (opcode == GDT_REALTIME) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001530 *(u32 *)&cmd_ptr->u.screen.su.data[0] = p1;
1531 *(u32 *)&cmd_ptr->u.screen.su.data[4] = (u32)p2;
1532 *(u32 *)&cmd_ptr->u.screen.su.data[8] = (u32)p3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
1534 }
1535 ha->cmd_len = sizeof(gdth_cmd_str);
1536 ha->cmd_offs_dpmem = 0;
1537 ha->cmd_cnt = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001538 gdth_copy_command(ha);
1539 gdth_release_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 gdth_delay(20);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001541 if (!gdth_wait(ha, index, INIT_TIMEOUT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 printk("GDT: Initialization error (timeout service %d)\n",service);
1543 return 0;
1544 }
1545 if (ha->status != S_BSY || --retries == 0)
1546 break;
1547 gdth_delay(1);
1548 }
1549
1550 return (ha->status != S_OK ? 0:1);
1551}
1552
1553
1554/* search for devices */
1555
Al Viroced71722008-11-22 17:35:14 +00001556static int __devinit gdth_search_drives(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001558 u16 cdev_cnt, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 int ok;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001560 u32 bus_no, drv_cnt, drv_no, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 gdth_getch_str *chn;
1562 gdth_drlist_str *drl;
1563 gdth_iochan_str *ioc;
1564 gdth_raw_iochan_str *iocr;
1565 gdth_arcdl_str *alst;
1566 gdth_alist_str *alst2;
1567 gdth_oem_str_ioctl *oemstr;
1568#ifdef INT_COAL
1569 gdth_perf_modes *pmod;
1570#endif
1571
1572#ifdef GDTH_RTC
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001573 u8 rtc[12];
1574 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575#endif
1576
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001577 TRACE(("gdth_search_drives() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 ok = 0;
1579
1580 /* initialize controller services, at first: screen service */
1581 ha->screen_feat = 0;
1582 if (!force_dma32) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001583 ok = gdth_internal_cmd(ha, SCREENSERVICE, GDT_X_INIT_SCR, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 if (ok)
1585 ha->screen_feat = GDT_64BIT;
1586 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001587 if (force_dma32 || (!ok && ha->status == (u16)S_NOFUNC))
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001588 ok = gdth_internal_cmd(ha, SCREENSERVICE, GDT_INIT, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (!ok) {
1590 printk("GDT-HA %d: Initialization error screen service (code %d)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001591 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return 0;
1593 }
1594 TRACE2(("gdth_search_drives(): SCREENSERVICE initialized\n"));
1595
1596#ifdef GDTH_RTC
1597 /* read realtime clock info, send to controller */
1598 /* 1. wait for the falling edge of update flag */
1599 spin_lock_irqsave(&rtc_lock, flags);
1600 for (j = 0; j < 1000000; ++j)
1601 if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
1602 break;
1603 for (j = 0; j < 1000000; ++j)
1604 if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
1605 break;
1606 /* 2. read info */
1607 do {
1608 for (j = 0; j < 12; ++j)
1609 rtc[j] = CMOS_READ(j);
1610 } while (rtc[0] != CMOS_READ(0));
Robert P. J. Day7a960b72007-05-23 14:41:40 -07001611 spin_unlock_irqrestore(&rtc_lock, flags);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001612 TRACE2(("gdth_search_drives(): RTC: %x/%x/%x\n",*(u32 *)&rtc[0],
1613 *(u32 *)&rtc[4], *(u32 *)&rtc[8]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 /* 3. send to controller firmware */
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001615 gdth_internal_cmd(ha, SCREENSERVICE, GDT_REALTIME, *(u32 *)&rtc[0],
1616 *(u32 *)&rtc[4], *(u32 *)&rtc[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617#endif
1618
1619 /* unfreeze all IOs */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001620 gdth_internal_cmd(ha, CACHESERVICE, GDT_UNFREEZE_IO, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 /* initialize cache service */
1623 ha->cache_feat = 0;
1624 if (!force_dma32) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001625 ok = gdth_internal_cmd(ha, CACHESERVICE, GDT_X_INIT_HOST, LINUX_OS,
1626 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (ok)
1628 ha->cache_feat = GDT_64BIT;
1629 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001630 if (force_dma32 || (!ok && ha->status == (u16)S_NOFUNC))
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001631 ok = gdth_internal_cmd(ha, CACHESERVICE, GDT_INIT, LINUX_OS, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 if (!ok) {
1633 printk("GDT-HA %d: Initialization error cache service (code %d)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001634 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return 0;
1636 }
1637 TRACE2(("gdth_search_drives(): CACHESERVICE initialized\n"));
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001638 cdev_cnt = (u16)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 ha->fw_vers = ha->service;
1640
1641#ifdef INT_COAL
1642 if (ha->type == GDT_PCIMPR) {
1643 /* set perf. modes */
1644 pmod = (gdth_perf_modes *)ha->pscratch;
1645 pmod->version = 1;
1646 pmod->st_mode = 1; /* enable one status buffer */
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001647 *((u64 *)&pmod->st_buff_addr1) = ha->coal_stat_phys;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 pmod->st_buff_indx1 = COALINDEX;
1649 pmod->st_buff_addr2 = 0;
1650 pmod->st_buff_u_addr2 = 0;
1651 pmod->st_buff_indx2 = 0;
1652 pmod->st_buff_size = sizeof(gdth_coal_status) * MAXOFFSETS;
1653 pmod->cmd_mode = 0; // disable all cmd buffers
1654 pmod->cmd_buff_addr1 = 0;
1655 pmod->cmd_buff_u_addr1 = 0;
1656 pmod->cmd_buff_indx1 = 0;
1657 pmod->cmd_buff_addr2 = 0;
1658 pmod->cmd_buff_u_addr2 = 0;
1659 pmod->cmd_buff_indx2 = 0;
1660 pmod->cmd_buff_size = 0;
1661 pmod->reserved1 = 0;
1662 pmod->reserved2 = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001663 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, SET_PERF_MODES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 INVALID_CHANNEL,sizeof(gdth_perf_modes))) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001665 printk("GDT-HA %d: Interrupt coalescing activated\n", ha->hanum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
1667 }
1668#endif
1669
1670 /* detect number of buses - try new IOCTL */
1671 iocr = (gdth_raw_iochan_str *)ha->pscratch;
1672 iocr->hdr.version = 0xffffffff;
1673 iocr->hdr.list_entries = MAXBUS;
1674 iocr->hdr.first_chan = 0;
1675 iocr->hdr.last_chan = MAXBUS-1;
1676 iocr->hdr.list_offset = GDTOFFSOF(gdth_raw_iochan_str, list[0]);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001677 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, IOCHAN_RAW_DESC,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 INVALID_CHANNEL,sizeof(gdth_raw_iochan_str))) {
1679 TRACE2(("IOCHAN_RAW_DESC supported!\n"));
1680 ha->bus_cnt = iocr->hdr.chan_count;
1681 for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
1682 if (iocr->list[bus_no].proc_id < MAXID)
1683 ha->bus_id[bus_no] = iocr->list[bus_no].proc_id;
1684 else
1685 ha->bus_id[bus_no] = 0xff;
1686 }
1687 } else {
1688 /* old method */
1689 chn = (gdth_getch_str *)ha->pscratch;
1690 for (bus_no = 0; bus_no < MAXBUS; ++bus_no) {
1691 chn->channel_no = bus_no;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001692 if (!gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 SCSI_CHAN_CNT | L_CTRL_PATTERN,
1694 IO_CHANNEL | INVALID_CHANNEL,
1695 sizeof(gdth_getch_str))) {
1696 if (bus_no == 0) {
1697 printk("GDT-HA %d: Error detecting channel count (0x%x)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001698 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return 0;
1700 }
1701 break;
1702 }
1703 if (chn->siop_id < MAXID)
1704 ha->bus_id[bus_no] = chn->siop_id;
1705 else
1706 ha->bus_id[bus_no] = 0xff;
1707 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001708 ha->bus_cnt = (u8)bus_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710 TRACE2(("gdth_search_drives() %d channels\n",ha->bus_cnt));
1711
1712 /* read cache configuration */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001713 if (!gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, CACHE_INFO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 INVALID_CHANNEL,sizeof(gdth_cinfo_str))) {
1715 printk("GDT-HA %d: Initialization error cache service (code %d)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001716 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 return 0;
1718 }
1719 ha->cpar = ((gdth_cinfo_str *)ha->pscratch)->cpar;
1720 TRACE2(("gdth_search_drives() cinfo: vs %x sta %d str %d dw %d b %d\n",
1721 ha->cpar.version,ha->cpar.state,ha->cpar.strategy,
1722 ha->cpar.write_back,ha->cpar.block_size));
1723
1724 /* read board info and features */
1725 ha->more_proc = FALSE;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001726 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, BOARD_INFO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 INVALID_CHANNEL,sizeof(gdth_binfo_str))) {
1728 memcpy(&ha->binfo, (gdth_binfo_str *)ha->pscratch,
1729 sizeof(gdth_binfo_str));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001730 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, BOARD_FEATURES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 INVALID_CHANNEL,sizeof(gdth_bfeat_str))) {
1732 TRACE2(("BOARD_INFO/BOARD_FEATURES supported\n"));
1733 ha->bfeat = *(gdth_bfeat_str *)ha->pscratch;
1734 ha->more_proc = TRUE;
1735 }
1736 } else {
1737 TRACE2(("BOARD_INFO requires firmware >= 1.10/2.08\n"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001738 strcpy(ha->binfo.type_string, gdth_ctr_name(ha));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 }
1740 TRACE2(("Controller name: %s\n",ha->binfo.type_string));
1741
1742 /* read more informations */
1743 if (ha->more_proc) {
1744 /* physical drives, channel addresses */
1745 ioc = (gdth_iochan_str *)ha->pscratch;
1746 ioc->hdr.version = 0xffffffff;
1747 ioc->hdr.list_entries = MAXBUS;
1748 ioc->hdr.first_chan = 0;
1749 ioc->hdr.last_chan = MAXBUS-1;
1750 ioc->hdr.list_offset = GDTOFFSOF(gdth_iochan_str, list[0]);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001751 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, IOCHAN_DESC,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 INVALID_CHANNEL,sizeof(gdth_iochan_str))) {
1753 for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
1754 ha->raw[bus_no].address = ioc->list[bus_no].address;
1755 ha->raw[bus_no].local_no = ioc->list[bus_no].local_no;
1756 }
1757 } else {
1758 for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
1759 ha->raw[bus_no].address = IO_CHANNEL;
1760 ha->raw[bus_no].local_no = bus_no;
1761 }
1762 }
1763 for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
1764 chn = (gdth_getch_str *)ha->pscratch;
1765 chn->channel_no = ha->raw[bus_no].local_no;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001766 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 SCSI_CHAN_CNT | L_CTRL_PATTERN,
1768 ha->raw[bus_no].address | INVALID_CHANNEL,
1769 sizeof(gdth_getch_str))) {
1770 ha->raw[bus_no].pdev_cnt = chn->drive_cnt;
1771 TRACE2(("Channel %d: %d phys. drives\n",
1772 bus_no,chn->drive_cnt));
1773 }
1774 if (ha->raw[bus_no].pdev_cnt > 0) {
1775 drl = (gdth_drlist_str *)ha->pscratch;
1776 drl->sc_no = ha->raw[bus_no].local_no;
1777 drl->sc_cnt = ha->raw[bus_no].pdev_cnt;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001778 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 SCSI_DR_LIST | L_CTRL_PATTERN,
1780 ha->raw[bus_no].address | INVALID_CHANNEL,
1781 sizeof(gdth_drlist_str))) {
1782 for (j = 0; j < ha->raw[bus_no].pdev_cnt; ++j)
1783 ha->raw[bus_no].id_list[j] = drl->sc_list[j];
1784 } else {
1785 ha->raw[bus_no].pdev_cnt = 0;
1786 }
1787 }
1788 }
1789
1790 /* logical drives */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001791 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, CACHE_DRV_CNT,
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001792 INVALID_CHANNEL,sizeof(u32))) {
1793 drv_cnt = *(u32 *)ha->pscratch;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001794 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, CACHE_DRV_LIST,
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001795 INVALID_CHANNEL,drv_cnt * sizeof(u32))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 for (j = 0; j < drv_cnt; ++j) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001797 drv_no = ((u32 *)ha->pscratch)[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (drv_no < MAX_LDRIVES) {
1799 ha->hdr[drv_no].is_logdrv = TRUE;
1800 TRACE2(("Drive %d is log. drive\n",drv_no));
1801 }
1802 }
1803 }
1804 alst = (gdth_arcdl_str *)ha->pscratch;
1805 alst->entries_avail = MAX_LDRIVES;
1806 alst->first_entry = 0;
1807 alst->list_offset = GDTOFFSOF(gdth_arcdl_str, list[0]);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001808 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 ARRAY_DRV_LIST2 | LA_CTRL_PATTERN,
1810 INVALID_CHANNEL, sizeof(gdth_arcdl_str) +
1811 (alst->entries_avail-1) * sizeof(gdth_alist_str))) {
1812 for (j = 0; j < alst->entries_init; ++j) {
1813 ha->hdr[j].is_arraydrv = alst->list[j].is_arrayd;
1814 ha->hdr[j].is_master = alst->list[j].is_master;
1815 ha->hdr[j].is_parity = alst->list[j].is_parity;
1816 ha->hdr[j].is_hotfix = alst->list[j].is_hotfix;
1817 ha->hdr[j].master_no = alst->list[j].cd_handle;
1818 }
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001819 } else if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 ARRAY_DRV_LIST | LA_CTRL_PATTERN,
1821 0, 35 * sizeof(gdth_alist_str))) {
1822 for (j = 0; j < 35; ++j) {
1823 alst2 = &((gdth_alist_str *)ha->pscratch)[j];
1824 ha->hdr[j].is_arraydrv = alst2->is_arrayd;
1825 ha->hdr[j].is_master = alst2->is_master;
1826 ha->hdr[j].is_parity = alst2->is_parity;
1827 ha->hdr[j].is_hotfix = alst2->is_hotfix;
1828 ha->hdr[j].master_no = alst2->cd_handle;
1829 }
1830 }
1831 }
1832 }
1833
1834 /* initialize raw service */
1835 ha->raw_feat = 0;
1836 if (!force_dma32) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001837 ok = gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_X_INIT_RAW, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 if (ok)
1839 ha->raw_feat = GDT_64BIT;
1840 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001841 if (force_dma32 || (!ok && ha->status == (u16)S_NOFUNC))
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001842 ok = gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_INIT, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 if (!ok) {
1844 printk("GDT-HA %d: Initialization error raw service (code %d)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001845 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return 0;
1847 }
1848 TRACE2(("gdth_search_drives(): RAWSERVICE initialized\n"));
1849
1850 /* set/get features raw service (scatter/gather) */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001851 if (gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_SET_FEAT, SCATTER_GATHER,
1852 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 TRACE2(("gdth_search_drives(): set features RAWSERVICE OK\n"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001854 if (gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_GET_FEAT, 0, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 TRACE2(("gdth_search_dr(): get feat RAWSERVICE %d\n",
1856 ha->info));
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001857 ha->raw_feat |= (u16)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 }
1859 }
1860
1861 /* set/get features cache service (equal to raw service) */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001862 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_SET_FEAT, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 SCATTER_GATHER,0)) {
1864 TRACE2(("gdth_search_drives(): set features CACHESERVICE OK\n"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001865 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_GET_FEAT, 0, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 TRACE2(("gdth_search_dr(): get feat CACHESERV. %d\n",
1867 ha->info));
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001868 ha->cache_feat |= (u16)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 }
1870 }
1871
1872 /* reserve drives for raw service */
1873 if (reserve_mode != 0) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001874 gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_RESERVE_ALL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 reserve_mode == 1 ? 1 : 3, 0, 0);
1876 TRACE2(("gdth_search_drives(): RESERVE_ALL code %d\n",
1877 ha->status));
1878 }
1879 for (i = 0; i < MAX_RES_ARGS; i += 4) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001880 if (reserve_list[i] == ha->hanum && reserve_list[i+1] < ha->bus_cnt &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 reserve_list[i+2] < ha->tid_cnt && reserve_list[i+3] < MAXLUN) {
1882 TRACE2(("gdth_search_drives(): reserve ha %d bus %d id %d lun %d\n",
1883 reserve_list[i], reserve_list[i+1],
1884 reserve_list[i+2], reserve_list[i+3]));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001885 if (!gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_RESERVE, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 reserve_list[i+1], reserve_list[i+2] |
1887 (reserve_list[i+3] << 8))) {
1888 printk("GDT-HA %d: Error raw service (RESERVE, code %d)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001889 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 }
1891 }
1892 }
1893
1894 /* Determine OEM string using IOCTL */
1895 oemstr = (gdth_oem_str_ioctl *)ha->pscratch;
1896 oemstr->params.ctl_version = 0x01;
1897 oemstr->params.buffer_size = sizeof(oemstr->text);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001898 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 CACHE_READ_OEM_STRING_RECORD,INVALID_CHANNEL,
1900 sizeof(gdth_oem_str_ioctl))) {
1901 TRACE2(("gdth_search_drives(): CACHE_READ_OEM_STRING_RECORD OK\n"));
1902 printk("GDT-HA %d: Vendor: %s Name: %s\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001903 ha->hanum, oemstr->text.oem_company_name, ha->binfo.type_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 /* Save the Host Drive inquiry data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 strlcpy(ha->oem_name,oemstr->text.scsi_host_drive_inquiry_vendor_id,
1906 sizeof(ha->oem_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 } else {
1908 /* Old method, based on PCI ID */
1909 TRACE2(("gdth_search_drives(): CACHE_READ_OEM_STRING_RECORD failed\n"));
1910 printk("GDT-HA %d: Name: %s\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001911 ha->hanum, ha->binfo.type_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 if (ha->oem_id == OEM_ID_INTEL)
1913 strlcpy(ha->oem_name,"Intel ", sizeof(ha->oem_name));
1914 else
1915 strlcpy(ha->oem_name,"ICP ", sizeof(ha->oem_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 }
1917
1918 /* scanning for host drives */
1919 for (i = 0; i < cdev_cnt; ++i)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001920 gdth_analyse_hdrive(ha, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 TRACE(("gdth_search_drives() OK\n"));
1923 return 1;
1924}
1925
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001926static int gdth_analyse_hdrive(gdth_ha_str *ha, u16 hdrive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001928 u32 drv_cyls;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 int drv_hds, drv_secs;
1930
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001931 TRACE(("gdth_analyse_hdrive() hanum %d drive %d\n", ha->hanum, hdrive));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 if (hdrive >= MAX_HDRIVES)
1933 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001935 if (!gdth_internal_cmd(ha, CACHESERVICE, GDT_INFO, hdrive, 0, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return 0;
1937 ha->hdr[hdrive].present = TRUE;
1938 ha->hdr[hdrive].size = ha->info;
1939
1940 /* evaluate mapping (sectors per head, heads per cylinder) */
1941 ha->hdr[hdrive].size &= ~SECS32;
1942 if (ha->info2 == 0) {
1943 gdth_eval_mapping(ha->hdr[hdrive].size,&drv_cyls,&drv_hds,&drv_secs);
1944 } else {
1945 drv_hds = ha->info2 & 0xff;
1946 drv_secs = (ha->info2 >> 8) & 0xff;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001947 drv_cyls = (u32)ha->hdr[hdrive].size / drv_hds / drv_secs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001949 ha->hdr[hdrive].heads = (u8)drv_hds;
1950 ha->hdr[hdrive].secs = (u8)drv_secs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 /* round size */
1952 ha->hdr[hdrive].size = drv_cyls * drv_hds * drv_secs;
1953
1954 if (ha->cache_feat & GDT_64BIT) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001955 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_X_INFO, hdrive, 0, 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 && ha->info2 != 0) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001957 ha->hdr[hdrive].size = ((u64)ha->info2 << 32) | ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
1959 }
1960 TRACE2(("gdth_search_dr() cdr. %d size %d hds %d scs %d\n",
1961 hdrive,ha->hdr[hdrive].size,drv_hds,drv_secs));
1962
1963 /* get informations about device */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001964 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_DEVTYPE, hdrive, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 TRACE2(("gdth_search_dr() cache drive %d devtype %d\n",
1966 hdrive,ha->info));
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001967 ha->hdr[hdrive].devtype = (u16)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
1969
1970 /* cluster info */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001971 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_CLUST_INFO, hdrive, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 TRACE2(("gdth_search_dr() cache drive %d cluster info %d\n",
1973 hdrive,ha->info));
1974 if (!shared_access)
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001975 ha->hdr[hdrive].cluster_type = (u8)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 }
1977
1978 /* R/W attributes */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001979 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_RW_ATTRIBS, hdrive, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 TRACE2(("gdth_search_dr() cache drive %d r/w attrib. %d\n",
1981 hdrive,ha->info));
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001982 ha->hdr[hdrive].rw_attribs = (u8)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
1984
1985 return 1;
1986}
1987
1988
1989/* command queueing/sending functions */
1990
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001991static void gdth_putq(gdth_ha_str *ha, Scsi_Cmnd *scp, u8 priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02001993 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 register Scsi_Cmnd *pscp;
1995 register Scsi_Cmnd *nscp;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001996 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 TRACE(("gdth_putq() priority %d\n",priority));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 spin_lock_irqsave(&ha->smp_lock, flags);
2000
Jens Axboe242f9dc2008-09-14 05:55:09 -07002001 if (!cmndinfo->internal_command)
Boaz Harroshf842b642007-10-02 23:16:01 +02002002 cmndinfo->priority = priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004 if (ha->req_first==NULL) {
2005 ha->req_first = scp; /* queue was empty */
2006 scp->SCp.ptr = NULL;
2007 } else { /* queue not empty */
2008 pscp = ha->req_first;
2009 nscp = (Scsi_Cmnd *)pscp->SCp.ptr;
2010 /* priority: 0-highest,..,0xff-lowest */
Boaz Harroshf842b642007-10-02 23:16:01 +02002011 while (nscp && gdth_cmnd_priv(nscp)->priority <= priority) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 pscp = nscp;
2013 nscp = (Scsi_Cmnd *)pscp->SCp.ptr;
2014 }
2015 pscp->SCp.ptr = (char *)scp;
2016 scp->SCp.ptr = (char *)nscp;
2017 }
2018 spin_unlock_irqrestore(&ha->smp_lock, flags);
2019
2020#ifdef GDTH_STATISTICS
2021 flags = 0;
2022 for (nscp=ha->req_first; nscp; nscp=(Scsi_Cmnd*)nscp->SCp.ptr)
2023 ++flags;
2024 if (max_rq < flags) {
2025 max_rq = flags;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002026 TRACE3(("GDT: max_rq = %d\n",(u16)max_rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 }
2028#endif
2029}
2030
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002031static void gdth_next(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 register Scsi_Cmnd *pscp;
2034 register Scsi_Cmnd *nscp;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002035 u8 b, t, l, firsttime;
2036 u8 this_cmd, next_cmd;
2037 unsigned long flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 int cmd_index;
2039
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002040 TRACE(("gdth_next() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 if (!gdth_polling)
2042 spin_lock_irqsave(&ha->smp_lock, flags);
2043
2044 ha->cmd_cnt = ha->cmd_offs_dpmem = 0;
2045 this_cmd = firsttime = TRUE;
2046 next_cmd = gdth_polling ? FALSE:TRUE;
2047 cmd_index = 0;
2048
2049 for (nscp = pscp = ha->req_first; nscp; nscp = (Scsi_Cmnd *)nscp->SCp.ptr) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002050 struct gdth_cmndinfo *nscp_cmndinfo = gdth_cmnd_priv(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 if (nscp != pscp && nscp != (Scsi_Cmnd *)pscp->SCp.ptr)
2052 pscp = (Scsi_Cmnd *)pscp->SCp.ptr;
Boaz Harroshf842b642007-10-02 23:16:01 +02002053 if (!nscp_cmndinfo->internal_command) {
Christoph Hellwig52759e62007-10-02 22:59:53 +02002054 b = nscp->device->channel;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07002055 t = nscp->device->id;
2056 l = nscp->device->lun;
Boaz Harroshf842b642007-10-02 23:16:01 +02002057 if (nscp_cmndinfo->priority >= DEFAULT_PRI) {
Leubner, Achimcbd5f692006-06-09 11:34:29 -07002058 if ((b != ha->virt_bus && ha->raw[BUS_L2P(ha,b)].lock) ||
2059 (b == ha->virt_bus && t < MAX_HDRIVES && ha->hdr[t].lock))
2060 continue;
2061 }
2062 } else
2063 b = t = l = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 if (firsttime) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002066 if (gdth_test_busy(ha)) { /* controller busy ? */
2067 TRACE(("gdth_next() controller %d busy !\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 if (!gdth_polling) {
2069 spin_unlock_irqrestore(&ha->smp_lock, flags);
2070 return;
2071 }
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002072 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 gdth_delay(1);
2074 }
2075 firsttime = FALSE;
2076 }
2077
Boaz Harroshf842b642007-10-02 23:16:01 +02002078 if (!nscp_cmndinfo->internal_command) {
2079 if (nscp_cmndinfo->phase == -1) {
2080 nscp_cmndinfo->phase = CACHESERVICE; /* default: cache svc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 if (nscp->cmnd[0] == TEST_UNIT_READY) {
2082 TRACE2(("TEST_UNIT_READY Bus %d Id %d LUN %d\n",
2083 b, t, l));
2084 /* TEST_UNIT_READY -> set scan mode */
2085 if ((ha->scan_mode & 0x0f) == 0) {
2086 if (b == 0 && t == 0 && l == 0) {
2087 ha->scan_mode |= 1;
2088 TRACE2(("Scan mode: 0x%x\n", ha->scan_mode));
2089 }
2090 } else if ((ha->scan_mode & 0x0f) == 1) {
2091 if (b == 0 && ((t == 0 && l == 1) ||
2092 (t == 1 && l == 0))) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002093 nscp_cmndinfo->OpCode = GDT_SCAN_START;
2094 nscp_cmndinfo->phase = ((ha->scan_mode & 0x10 ? 1:0) << 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 | SCSIRAWSERVICE;
2096 ha->scan_mode = 0x12;
2097 TRACE2(("Scan mode: 0x%x (SCAN_START)\n",
2098 ha->scan_mode));
2099 } else {
2100 ha->scan_mode &= 0x10;
2101 TRACE2(("Scan mode: 0x%x\n", ha->scan_mode));
2102 }
2103 } else if (ha->scan_mode == 0x12) {
2104 if (b == ha->bus_cnt && t == ha->tid_cnt-1) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002105 nscp_cmndinfo->phase = SCSIRAWSERVICE;
2106 nscp_cmndinfo->OpCode = GDT_SCAN_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 ha->scan_mode &= 0x10;
2108 TRACE2(("Scan mode: 0x%x (SCAN_END)\n",
2109 ha->scan_mode));
2110 }
2111 }
2112 }
2113 if (b == ha->virt_bus && nscp->cmnd[0] != INQUIRY &&
2114 nscp->cmnd[0] != READ_CAPACITY && nscp->cmnd[0] != MODE_SENSE &&
2115 (ha->hdr[t].cluster_type & CLUSTER_DRIVE)) {
2116 /* always GDT_CLUST_INFO! */
Boaz Harroshf842b642007-10-02 23:16:01 +02002117 nscp_cmndinfo->OpCode = GDT_CLUST_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
2119 }
2120 }
2121
Boaz Harroshf842b642007-10-02 23:16:01 +02002122 if (nscp_cmndinfo->OpCode != -1) {
2123 if ((nscp_cmndinfo->phase & 0xff) == CACHESERVICE) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002124 if (!(cmd_index=gdth_fill_cache_cmd(ha, nscp, t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 this_cmd = FALSE;
2126 next_cmd = FALSE;
Boaz Harroshf842b642007-10-02 23:16:01 +02002127 } else if ((nscp_cmndinfo->phase & 0xff) == SCSIRAWSERVICE) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002128 if (!(cmd_index=gdth_fill_raw_cmd(ha, nscp, BUS_L2P(ha, b))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 this_cmd = FALSE;
2130 next_cmd = FALSE;
2131 } else {
2132 memset((char*)nscp->sense_buffer,0,16);
2133 nscp->sense_buffer[0] = 0x70;
2134 nscp->sense_buffer[2] = NOT_READY;
2135 nscp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
Boaz Harroshf842b642007-10-02 23:16:01 +02002136 if (!nscp_cmndinfo->wait_for_completion)
2137 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002139 gdth_scsi_done(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02002141 } else if (gdth_cmnd_priv(nscp)->internal_command) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002142 if (!(cmd_index=gdth_special_cmd(ha, nscp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 this_cmd = FALSE;
2144 next_cmd = FALSE;
2145 } else if (b != ha->virt_bus) {
2146 if (ha->raw[BUS_L2P(ha,b)].io_cnt[t] >= GDTH_MAX_RAW ||
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002147 !(cmd_index=gdth_fill_raw_cmd(ha, nscp, BUS_L2P(ha, b))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 this_cmd = FALSE;
2149 else
2150 ha->raw[BUS_L2P(ha,b)].io_cnt[t]++;
2151 } else if (t >= MAX_HDRIVES || !ha->hdr[t].present || l != 0) {
2152 TRACE2(("Command 0x%x to bus %d id %d lun %d -> IGNORE\n",
2153 nscp->cmnd[0], b, t, l));
2154 nscp->result = DID_BAD_TARGET << 16;
Boaz Harroshf842b642007-10-02 23:16:01 +02002155 if (!nscp_cmndinfo->wait_for_completion)
2156 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002158 gdth_scsi_done(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 } else {
2160 switch (nscp->cmnd[0]) {
2161 case TEST_UNIT_READY:
2162 case INQUIRY:
2163 case REQUEST_SENSE:
2164 case READ_CAPACITY:
2165 case VERIFY:
2166 case START_STOP:
2167 case MODE_SENSE:
2168 case SERVICE_ACTION_IN:
2169 TRACE(("cache cmd %x/%x/%x/%x/%x/%x\n",nscp->cmnd[0],
2170 nscp->cmnd[1],nscp->cmnd[2],nscp->cmnd[3],
2171 nscp->cmnd[4],nscp->cmnd[5]));
2172 if (ha->hdr[t].media_changed && nscp->cmnd[0] != INQUIRY) {
2173 /* return UNIT_ATTENTION */
2174 TRACE2(("cmd 0x%x target %d: UNIT_ATTENTION\n",
2175 nscp->cmnd[0], t));
2176 ha->hdr[t].media_changed = FALSE;
2177 memset((char*)nscp->sense_buffer,0,16);
2178 nscp->sense_buffer[0] = 0x70;
2179 nscp->sense_buffer[2] = UNIT_ATTENTION;
2180 nscp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
Boaz Harroshf842b642007-10-02 23:16:01 +02002181 if (!nscp_cmndinfo->wait_for_completion)
2182 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002184 gdth_scsi_done(nscp);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002185 } else if (gdth_internal_cache_cmd(ha, nscp))
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002186 gdth_scsi_done(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 break;
2188
2189 case ALLOW_MEDIUM_REMOVAL:
2190 TRACE(("cache cmd %x/%x/%x/%x/%x/%x\n",nscp->cmnd[0],
2191 nscp->cmnd[1],nscp->cmnd[2],nscp->cmnd[3],
2192 nscp->cmnd[4],nscp->cmnd[5]));
2193 if ( (nscp->cmnd[4]&1) && !(ha->hdr[t].devtype&1) ) {
2194 TRACE(("Prevent r. nonremov. drive->do nothing\n"));
2195 nscp->result = DID_OK << 16;
2196 nscp->sense_buffer[0] = 0;
Boaz Harroshf842b642007-10-02 23:16:01 +02002197 if (!nscp_cmndinfo->wait_for_completion)
2198 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002200 gdth_scsi_done(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 } else {
2202 nscp->cmnd[3] = (ha->hdr[t].devtype&1) ? 1:0;
2203 TRACE(("Prevent/allow r. %d rem. drive %d\n",
2204 nscp->cmnd[4],nscp->cmnd[3]));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002205 if (!(cmd_index=gdth_fill_cache_cmd(ha, nscp, t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 this_cmd = FALSE;
2207 }
2208 break;
2209
2210 case RESERVE:
2211 case RELEASE:
2212 TRACE2(("cache cmd %s\n",nscp->cmnd[0] == RESERVE ?
2213 "RESERVE" : "RELEASE"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002214 if (!(cmd_index=gdth_fill_cache_cmd(ha, nscp, t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 this_cmd = FALSE;
2216 break;
2217
2218 case READ_6:
2219 case WRITE_6:
2220 case READ_10:
2221 case WRITE_10:
2222 case READ_16:
2223 case WRITE_16:
2224 if (ha->hdr[t].media_changed) {
2225 /* return UNIT_ATTENTION */
2226 TRACE2(("cmd 0x%x target %d: UNIT_ATTENTION\n",
2227 nscp->cmnd[0], t));
2228 ha->hdr[t].media_changed = FALSE;
2229 memset((char*)nscp->sense_buffer,0,16);
2230 nscp->sense_buffer[0] = 0x70;
2231 nscp->sense_buffer[2] = UNIT_ATTENTION;
2232 nscp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
Boaz Harroshf842b642007-10-02 23:16:01 +02002233 if (!nscp_cmndinfo->wait_for_completion)
2234 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002236 gdth_scsi_done(nscp);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002237 } else if (!(cmd_index=gdth_fill_cache_cmd(ha, nscp, t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 this_cmd = FALSE;
2239 break;
2240
2241 default:
2242 TRACE2(("cache cmd %x/%x/%x/%x/%x/%x unknown\n",nscp->cmnd[0],
2243 nscp->cmnd[1],nscp->cmnd[2],nscp->cmnd[3],
2244 nscp->cmnd[4],nscp->cmnd[5]));
2245 printk("GDT-HA %d: Unknown SCSI command 0x%x to cache service !\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002246 ha->hanum, nscp->cmnd[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 nscp->result = DID_ABORT << 16;
Boaz Harroshf842b642007-10-02 23:16:01 +02002248 if (!nscp_cmndinfo->wait_for_completion)
2249 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002251 gdth_scsi_done(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 break;
2253 }
2254 }
2255
2256 if (!this_cmd)
2257 break;
2258 if (nscp == ha->req_first)
2259 ha->req_first = pscp = (Scsi_Cmnd *)nscp->SCp.ptr;
2260 else
2261 pscp->SCp.ptr = nscp->SCp.ptr;
2262 if (!next_cmd)
2263 break;
2264 }
2265
2266 if (ha->cmd_cnt > 0) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002267 gdth_release_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 }
2269
2270 if (!gdth_polling)
2271 spin_unlock_irqrestore(&ha->smp_lock, flags);
2272
2273 if (gdth_polling && ha->cmd_cnt > 0) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002274 if (!gdth_wait(ha, cmd_index, POLL_TIMEOUT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 printk("GDT-HA %d: Command %d timed out !\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002276 ha->hanum, cmd_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 }
2278}
Boaz Harrosh3892d882007-10-02 23:18:03 +02002279
2280/*
2281 * gdth_copy_internal_data() - copy to/from a buffer onto a scsi_cmnd's
2282 * buffers, kmap_atomic() as needed.
2283 */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002284static void gdth_copy_internal_data(gdth_ha_str *ha, Scsi_Cmnd *scp,
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002285 char *buffer, u16 count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002287 u16 cpcount,i, max_sg = scsi_sg_count(scp);
2288 u16 cpsum,cpnow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 struct scatterlist *sl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 char *address;
2291
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002292 cpcount = min_t(u16, count, scsi_bufflen(scp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Boaz Harrosh3892d882007-10-02 23:18:03 +02002294 if (cpcount) {
2295 cpsum=0;
2296 scsi_for_each_sg(scp, sl, max_sg, i) {
Leubner, Achimcbd5f692006-06-09 11:34:29 -07002297 unsigned long flags;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002298 cpnow = (u16)sl->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 TRACE(("copy_internal() now %d sum %d count %d %d\n",
Boaz Harroshd35055a2008-02-27 15:31:20 -08002300 cpnow, cpsum, cpcount, scsi_bufflen(scp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 if (cpsum+cpnow > cpcount)
2302 cpnow = cpcount - cpsum;
2303 cpsum += cpnow;
Jens Axboe45711f12007-10-22 21:19:53 +02002304 if (!sg_page(sl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 printk("GDT-HA %d: invalid sc/gt element in gdth_copy_internal_data()\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002306 ha->hanum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 return;
2308 }
Leubner, Achimcbd5f692006-06-09 11:34:29 -07002309 local_irq_save(flags);
Jens Axboe45711f12007-10-22 21:19:53 +02002310 address = kmap_atomic(sg_page(sl), KM_BIO_SRC_IRQ) + sl->offset;
Boaz Harroshee54cc62008-02-27 15:29:15 -08002311 memcpy(address, buffer, cpnow);
Jens Axboe45711f12007-10-22 21:19:53 +02002312 flush_dcache_page(sg_page(sl));
Leubner, Achimcbd5f692006-06-09 11:34:29 -07002313 kunmap_atomic(address, KM_BIO_SRC_IRQ);
Leubner, Achimcbd5f692006-06-09 11:34:29 -07002314 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 if (cpsum == cpcount)
2316 break;
2317 buffer += cpnow;
2318 }
Boaz Harrosh3892d882007-10-02 23:18:03 +02002319 } else if (count) {
2320 printk("GDT-HA %d: SCSI command with no buffers but data transfer expected!\n",
2321 ha->hanum);
2322 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 }
2324}
2325
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002326static int gdth_internal_cache_cmd(gdth_ha_str *ha, Scsi_Cmnd *scp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002328 u8 t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 gdth_inq_data inq;
2330 gdth_rdcap_data rdc;
2331 gdth_sense_data sd;
2332 gdth_modep_data mpd;
Boaz Harroshf842b642007-10-02 23:16:01 +02002333 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 t = scp->device->id;
2336 TRACE(("gdth_internal_cache_cmd() cmd 0x%x hdrive %d\n",
2337 scp->cmnd[0],t));
2338
2339 scp->result = DID_OK << 16;
2340 scp->sense_buffer[0] = 0;
2341
2342 switch (scp->cmnd[0]) {
2343 case TEST_UNIT_READY:
2344 case VERIFY:
2345 case START_STOP:
2346 TRACE2(("Test/Verify/Start hdrive %d\n",t));
2347 break;
2348
2349 case INQUIRY:
2350 TRACE2(("Inquiry hdrive %d devtype %d\n",
2351 t,ha->hdr[t].devtype));
2352 inq.type_qual = (ha->hdr[t].devtype&4) ? TYPE_ROM:TYPE_DISK;
2353 /* you can here set all disks to removable, if you want to do
2354 a flush using the ALLOW_MEDIUM_REMOVAL command */
2355 inq.modif_rmb = 0x00;
2356 if ((ha->hdr[t].devtype & 1) ||
2357 (ha->hdr[t].cluster_type & CLUSTER_DRIVE))
2358 inq.modif_rmb = 0x80;
2359 inq.version = 2;
2360 inq.resp_aenc = 2;
2361 inq.add_length= 32;
2362 strcpy(inq.vendor,ha->oem_name);
2363 sprintf(inq.product,"Host Drive #%02d",t);
2364 strcpy(inq.revision," ");
Boaz Harroshee54cc62008-02-27 15:29:15 -08002365 gdth_copy_internal_data(ha, scp, (char*)&inq, sizeof(gdth_inq_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 break;
2367
2368 case REQUEST_SENSE:
2369 TRACE2(("Request sense hdrive %d\n",t));
2370 sd.errorcode = 0x70;
2371 sd.segno = 0x00;
2372 sd.key = NO_SENSE;
2373 sd.info = 0;
2374 sd.add_length= 0;
Boaz Harroshee54cc62008-02-27 15:29:15 -08002375 gdth_copy_internal_data(ha, scp, (char*)&sd, sizeof(gdth_sense_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 break;
2377
2378 case MODE_SENSE:
2379 TRACE2(("Mode sense hdrive %d\n",t));
2380 memset((char*)&mpd,0,sizeof(gdth_modep_data));
2381 mpd.hd.data_length = sizeof(gdth_modep_data);
2382 mpd.hd.dev_par = (ha->hdr[t].devtype&2) ? 0x80:0;
2383 mpd.hd.bd_length = sizeof(mpd.bd);
2384 mpd.bd.block_length[0] = (SECTOR_SIZE & 0x00ff0000) >> 16;
2385 mpd.bd.block_length[1] = (SECTOR_SIZE & 0x0000ff00) >> 8;
2386 mpd.bd.block_length[2] = (SECTOR_SIZE & 0x000000ff);
Boaz Harroshee54cc62008-02-27 15:29:15 -08002387 gdth_copy_internal_data(ha, scp, (char*)&mpd, sizeof(gdth_modep_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 break;
2389
2390 case READ_CAPACITY:
2391 TRACE2(("Read capacity hdrive %d\n",t));
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002392 if (ha->hdr[t].size > (u64)0xffffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 rdc.last_block_no = 0xffffffff;
2394 else
2395 rdc.last_block_no = cpu_to_be32(ha->hdr[t].size-1);
2396 rdc.block_length = cpu_to_be32(SECTOR_SIZE);
Boaz Harroshee54cc62008-02-27 15:29:15 -08002397 gdth_copy_internal_data(ha, scp, (char*)&rdc, sizeof(gdth_rdcap_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 break;
2399
2400 case SERVICE_ACTION_IN:
2401 if ((scp->cmnd[1] & 0x1f) == SAI_READ_CAPACITY_16 &&
2402 (ha->cache_feat & GDT_64BIT)) {
2403 gdth_rdcap16_data rdc16;
2404
2405 TRACE2(("Read capacity (16) hdrive %d\n",t));
2406 rdc16.last_block_no = cpu_to_be64(ha->hdr[t].size-1);
2407 rdc16.block_length = cpu_to_be32(SECTOR_SIZE);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002408 gdth_copy_internal_data(ha, scp, (char*)&rdc16,
Boaz Harroshee54cc62008-02-27 15:29:15 -08002409 sizeof(gdth_rdcap16_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 } else {
2411 scp->result = DID_ABORT << 16;
2412 }
2413 break;
2414
2415 default:
2416 TRACE2(("Internal cache cmd 0x%x unknown\n",scp->cmnd[0]));
2417 break;
2418 }
2419
Boaz Harroshf842b642007-10-02 23:16:01 +02002420 if (!cmndinfo->wait_for_completion)
2421 cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 else
2423 return 1;
2424
2425 return 0;
2426}
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002427
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002428static int gdth_fill_cache_cmd(gdth_ha_str *ha, Scsi_Cmnd *scp, u16 hdrive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 register gdth_cmd_str *cmdp;
Boaz Harroshf842b642007-10-02 23:16:01 +02002431 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002432 u32 cnt, blockcnt;
2433 u64 no, blockno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 int i, cmd_index, read_write, sgcnt, mode64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 cmdp = ha->pccb;
2437 TRACE(("gdth_fill_cache_cmd() cmd 0x%x cmdsize %d hdrive %d\n",
2438 scp->cmnd[0],scp->cmd_len,hdrive));
2439
2440 if (ha->type==GDT_EISA && ha->cmd_cnt>0)
2441 return 0;
2442
2443 mode64 = (ha->cache_feat & GDT_64BIT) ? TRUE : FALSE;
2444 /* test for READ_16, WRITE_16 if !mode64 ? ---
2445 not required, should not occur due to error return on
2446 READ_CAPACITY_16 */
2447
2448 cmdp->Service = CACHESERVICE;
2449 cmdp->RequestBuffer = scp;
2450 /* search free command index */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002451 if (!(cmd_index=gdth_get_cmd_index(ha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 TRACE(("GDT: No free command index found\n"));
2453 return 0;
2454 }
2455 /* if it's the first command, set command semaphore */
2456 if (ha->cmd_cnt == 0)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002457 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
2459 /* fill command */
2460 read_write = 0;
Boaz Harroshf842b642007-10-02 23:16:01 +02002461 if (cmndinfo->OpCode != -1)
2462 cmdp->OpCode = cmndinfo->OpCode; /* special cache cmd. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 else if (scp->cmnd[0] == RESERVE)
2464 cmdp->OpCode = GDT_RESERVE_DRV;
2465 else if (scp->cmnd[0] == RELEASE)
2466 cmdp->OpCode = GDT_RELEASE_DRV;
2467 else if (scp->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
2468 if (scp->cmnd[4] & 1) /* prevent ? */
2469 cmdp->OpCode = GDT_MOUNT;
2470 else if (scp->cmnd[3] & 1) /* removable drive ? */
2471 cmdp->OpCode = GDT_UNMOUNT;
2472 else
2473 cmdp->OpCode = GDT_FLUSH;
2474 } else if (scp->cmnd[0] == WRITE_6 || scp->cmnd[0] == WRITE_10 ||
2475 scp->cmnd[0] == WRITE_12 || scp->cmnd[0] == WRITE_16
2476 ) {
2477 read_write = 1;
2478 if (gdth_write_through || ((ha->hdr[hdrive].rw_attribs & 1) &&
2479 (ha->cache_feat & GDT_WR_THROUGH)))
2480 cmdp->OpCode = GDT_WRITE_THR;
2481 else
2482 cmdp->OpCode = GDT_WRITE;
2483 } else {
2484 read_write = 2;
2485 cmdp->OpCode = GDT_READ;
2486 }
2487
2488 cmdp->BoardNode = LOCALBOARD;
2489 if (mode64) {
2490 cmdp->u.cache64.DeviceNo = hdrive;
2491 cmdp->u.cache64.BlockNo = 1;
2492 cmdp->u.cache64.sg_canz = 0;
2493 } else {
2494 cmdp->u.cache.DeviceNo = hdrive;
2495 cmdp->u.cache.BlockNo = 1;
2496 cmdp->u.cache.sg_canz = 0;
2497 }
2498
2499 if (read_write) {
2500 if (scp->cmd_len == 16) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002501 memcpy(&no, &scp->cmnd[2], sizeof(u64));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 blockno = be64_to_cpu(no);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002503 memcpy(&cnt, &scp->cmnd[10], sizeof(u32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 blockcnt = be32_to_cpu(cnt);
2505 } else if (scp->cmd_len == 10) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002506 memcpy(&no, &scp->cmnd[2], sizeof(u32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 blockno = be32_to_cpu(no);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002508 memcpy(&cnt, &scp->cmnd[7], sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 blockcnt = be16_to_cpu(cnt);
2510 } else {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002511 memcpy(&no, &scp->cmnd[0], sizeof(u32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 blockno = be32_to_cpu(no) & 0x001fffffUL;
2513 blockcnt= scp->cmnd[4]==0 ? 0x100 : scp->cmnd[4];
2514 }
2515 if (mode64) {
2516 cmdp->u.cache64.BlockNo = blockno;
2517 cmdp->u.cache64.BlockCnt = blockcnt;
2518 } else {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002519 cmdp->u.cache.BlockNo = (u32)blockno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 cmdp->u.cache.BlockCnt = blockcnt;
2521 }
2522
Boaz Harroshd35055a2008-02-27 15:31:20 -08002523 if (scsi_bufflen(scp)) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002524 cmndinfo->dma_dir = (read_write == 1 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
Boaz Harroshd35055a2008-02-27 15:31:20 -08002526 sgcnt = pci_map_sg(ha->pdev, scsi_sglist(scp), scsi_sg_count(scp),
Boaz Harrosh3892d882007-10-02 23:18:03 +02002527 cmndinfo->dma_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 if (mode64) {
Boaz Harrosh3892d882007-10-02 23:18:03 +02002529 struct scatterlist *sl;
2530
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002531 cmdp->u.cache64.DestAddr= (u64)-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 cmdp->u.cache64.sg_canz = sgcnt;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002533 scsi_for_each_sg(scp, sl, sgcnt, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 cmdp->u.cache64.sg_lst[i].sg_ptr = sg_dma_address(sl);
2535#ifdef GDTH_DMA_STATISTICS
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002536 if (cmdp->u.cache64.sg_lst[i].sg_ptr > (u64)0xffffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 ha->dma64_cnt++;
2538 else
2539 ha->dma32_cnt++;
2540#endif
2541 cmdp->u.cache64.sg_lst[i].sg_len = sg_dma_len(sl);
2542 }
2543 } else {
Boaz Harrosh3892d882007-10-02 23:18:03 +02002544 struct scatterlist *sl;
2545
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 cmdp->u.cache.DestAddr= 0xffffffff;
2547 cmdp->u.cache.sg_canz = sgcnt;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002548 scsi_for_each_sg(scp, sl, sgcnt, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 cmdp->u.cache.sg_lst[i].sg_ptr = sg_dma_address(sl);
2550#ifdef GDTH_DMA_STATISTICS
2551 ha->dma32_cnt++;
2552#endif
2553 cmdp->u.cache.sg_lst[i].sg_len = sg_dma_len(sl);
2554 }
2555 }
2556
2557#ifdef GDTH_STATISTICS
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002558 if (max_sg < (u32)sgcnt) {
2559 max_sg = (u32)sgcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 TRACE3(("GDT: max_sg = %d\n",max_sg));
2561 }
2562#endif
2563
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 }
2565 }
2566 /* evaluate command size, check space */
2567 if (mode64) {
2568 TRACE(("cache cmd: addr. %x sganz %x sgptr0 %x sglen0 %x\n",
2569 cmdp->u.cache64.DestAddr,cmdp->u.cache64.sg_canz,
2570 cmdp->u.cache64.sg_lst[0].sg_ptr,
2571 cmdp->u.cache64.sg_lst[0].sg_len));
2572 TRACE(("cache cmd: cmd %d blockno. %d, blockcnt %d\n",
2573 cmdp->OpCode,cmdp->u.cache64.BlockNo,cmdp->u.cache64.BlockCnt));
2574 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.cache64.sg_lst) +
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002575 (u16)cmdp->u.cache64.sg_canz * sizeof(gdth_sg64_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 } else {
2577 TRACE(("cache cmd: addr. %x sganz %x sgptr0 %x sglen0 %x\n",
2578 cmdp->u.cache.DestAddr,cmdp->u.cache.sg_canz,
2579 cmdp->u.cache.sg_lst[0].sg_ptr,
2580 cmdp->u.cache.sg_lst[0].sg_len));
2581 TRACE(("cache cmd: cmd %d blockno. %d, blockcnt %d\n",
2582 cmdp->OpCode,cmdp->u.cache.BlockNo,cmdp->u.cache.BlockCnt));
2583 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.cache.sg_lst) +
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002584 (u16)cmdp->u.cache.sg_canz * sizeof(gdth_sg_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 }
2586 if (ha->cmd_len & 3)
2587 ha->cmd_len += (4 - (ha->cmd_len & 3));
2588
2589 if (ha->cmd_cnt > 0) {
2590 if ((ha->cmd_offs_dpmem + ha->cmd_len + DPMEM_COMMAND_OFFSET) >
2591 ha->ic_all_size) {
2592 TRACE2(("gdth_fill_cache() DPMEM overflow\n"));
2593 ha->cmd_tab[cmd_index-2].cmnd = UNUSED_CMND;
2594 return 0;
2595 }
2596 }
2597
2598 /* copy command */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002599 gdth_copy_command(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 return cmd_index;
2601}
2602
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002603static int gdth_fill_raw_cmd(gdth_ha_str *ha, Scsi_Cmnd *scp, u8 b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 register gdth_cmd_str *cmdp;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002606 u16 i;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002607 dma_addr_t sense_paddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 int cmd_index, sgcnt, mode64;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002609 u8 t,l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 struct page *page;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002611 unsigned long offset;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02002612 struct gdth_cmndinfo *cmndinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 t = scp->device->id;
2615 l = scp->device->lun;
2616 cmdp = ha->pccb;
2617 TRACE(("gdth_fill_raw_cmd() cmd 0x%x bus %d ID %d LUN %d\n",
2618 scp->cmnd[0],b,t,l));
2619
2620 if (ha->type==GDT_EISA && ha->cmd_cnt>0)
2621 return 0;
2622
2623 mode64 = (ha->raw_feat & GDT_64BIT) ? TRUE : FALSE;
2624
2625 cmdp->Service = SCSIRAWSERVICE;
2626 cmdp->RequestBuffer = scp;
2627 /* search free command index */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002628 if (!(cmd_index=gdth_get_cmd_index(ha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 TRACE(("GDT: No free command index found\n"));
2630 return 0;
2631 }
2632 /* if it's the first command, set command semaphore */
2633 if (ha->cmd_cnt == 0)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002634 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02002636 cmndinfo = gdth_cmnd_priv(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 /* fill command */
Boaz Harroshf842b642007-10-02 23:16:01 +02002638 if (cmndinfo->OpCode != -1) {
2639 cmdp->OpCode = cmndinfo->OpCode; /* special raw cmd. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 cmdp->BoardNode = LOCALBOARD;
2641 if (mode64) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002642 cmdp->u.raw64.direction = (cmndinfo->phase >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 TRACE2(("special raw cmd 0x%x param 0x%x\n",
2644 cmdp->OpCode, cmdp->u.raw64.direction));
2645 /* evaluate command size */
2646 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw64.sg_lst);
2647 } else {
Boaz Harroshf842b642007-10-02 23:16:01 +02002648 cmdp->u.raw.direction = (cmndinfo->phase >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 TRACE2(("special raw cmd 0x%x param 0x%x\n",
2650 cmdp->OpCode, cmdp->u.raw.direction));
2651 /* evaluate command size */
2652 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw.sg_lst);
2653 }
2654
2655 } else {
2656 page = virt_to_page(scp->sense_buffer);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002657 offset = (unsigned long)scp->sense_buffer & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 sense_paddr = pci_map_page(ha->pdev,page,offset,
2659 16,PCI_DMA_FROMDEVICE);
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02002660
2661 cmndinfo->sense_paddr = sense_paddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 cmdp->OpCode = GDT_WRITE; /* always */
2663 cmdp->BoardNode = LOCALBOARD;
2664 if (mode64) {
2665 cmdp->u.raw64.reserved = 0;
2666 cmdp->u.raw64.mdisc_time = 0;
2667 cmdp->u.raw64.mcon_time = 0;
2668 cmdp->u.raw64.clen = scp->cmd_len;
2669 cmdp->u.raw64.target = t;
2670 cmdp->u.raw64.lun = l;
2671 cmdp->u.raw64.bus = b;
2672 cmdp->u.raw64.priority = 0;
Boaz Harroshd35055a2008-02-27 15:31:20 -08002673 cmdp->u.raw64.sdlen = scsi_bufflen(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 cmdp->u.raw64.sense_len = 16;
2675 cmdp->u.raw64.sense_data = sense_paddr;
2676 cmdp->u.raw64.direction =
2677 gdth_direction_tab[scp->cmnd[0]]==DOU ? GDTH_DATA_OUT:GDTH_DATA_IN;
2678 memcpy(cmdp->u.raw64.cmd,scp->cmnd,16);
Joerg Dorchainbb9ba312007-03-06 02:46:54 -08002679 cmdp->u.raw64.sg_ranz = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 } else {
2681 cmdp->u.raw.reserved = 0;
2682 cmdp->u.raw.mdisc_time = 0;
2683 cmdp->u.raw.mcon_time = 0;
2684 cmdp->u.raw.clen = scp->cmd_len;
2685 cmdp->u.raw.target = t;
2686 cmdp->u.raw.lun = l;
2687 cmdp->u.raw.bus = b;
2688 cmdp->u.raw.priority = 0;
2689 cmdp->u.raw.link_p = 0;
Boaz Harroshd35055a2008-02-27 15:31:20 -08002690 cmdp->u.raw.sdlen = scsi_bufflen(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 cmdp->u.raw.sense_len = 16;
2692 cmdp->u.raw.sense_data = sense_paddr;
2693 cmdp->u.raw.direction =
2694 gdth_direction_tab[scp->cmnd[0]]==DOU ? GDTH_DATA_OUT:GDTH_DATA_IN;
2695 memcpy(cmdp->u.raw.cmd,scp->cmnd,12);
Joerg Dorchainbb9ba312007-03-06 02:46:54 -08002696 cmdp->u.raw.sg_ranz = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 }
2698
Boaz Harroshd35055a2008-02-27 15:31:20 -08002699 if (scsi_bufflen(scp)) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002700 cmndinfo->dma_dir = PCI_DMA_BIDIRECTIONAL;
Boaz Harroshd35055a2008-02-27 15:31:20 -08002701 sgcnt = pci_map_sg(ha->pdev, scsi_sglist(scp), scsi_sg_count(scp),
Boaz Harrosh3892d882007-10-02 23:18:03 +02002702 cmndinfo->dma_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 if (mode64) {
Boaz Harrosh3892d882007-10-02 23:18:03 +02002704 struct scatterlist *sl;
2705
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002706 cmdp->u.raw64.sdata = (u64)-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 cmdp->u.raw64.sg_ranz = sgcnt;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002708 scsi_for_each_sg(scp, sl, sgcnt, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 cmdp->u.raw64.sg_lst[i].sg_ptr = sg_dma_address(sl);
2710#ifdef GDTH_DMA_STATISTICS
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002711 if (cmdp->u.raw64.sg_lst[i].sg_ptr > (u64)0xffffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 ha->dma64_cnt++;
2713 else
2714 ha->dma32_cnt++;
2715#endif
2716 cmdp->u.raw64.sg_lst[i].sg_len = sg_dma_len(sl);
2717 }
2718 } else {
Boaz Harrosh3892d882007-10-02 23:18:03 +02002719 struct scatterlist *sl;
2720
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 cmdp->u.raw.sdata = 0xffffffff;
2722 cmdp->u.raw.sg_ranz = sgcnt;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002723 scsi_for_each_sg(scp, sl, sgcnt, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 cmdp->u.raw.sg_lst[i].sg_ptr = sg_dma_address(sl);
2725#ifdef GDTH_DMA_STATISTICS
2726 ha->dma32_cnt++;
2727#endif
2728 cmdp->u.raw.sg_lst[i].sg_len = sg_dma_len(sl);
2729 }
2730 }
2731
2732#ifdef GDTH_STATISTICS
2733 if (max_sg < sgcnt) {
2734 max_sg = sgcnt;
2735 TRACE3(("GDT: max_sg = %d\n",sgcnt));
2736 }
2737#endif
2738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 }
2740 if (mode64) {
2741 TRACE(("raw cmd: addr. %x sganz %x sgptr0 %x sglen0 %x\n",
2742 cmdp->u.raw64.sdata,cmdp->u.raw64.sg_ranz,
2743 cmdp->u.raw64.sg_lst[0].sg_ptr,
2744 cmdp->u.raw64.sg_lst[0].sg_len));
2745 /* evaluate command size */
2746 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw64.sg_lst) +
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002747 (u16)cmdp->u.raw64.sg_ranz * sizeof(gdth_sg64_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 } else {
2749 TRACE(("raw cmd: addr. %x sganz %x sgptr0 %x sglen0 %x\n",
2750 cmdp->u.raw.sdata,cmdp->u.raw.sg_ranz,
2751 cmdp->u.raw.sg_lst[0].sg_ptr,
2752 cmdp->u.raw.sg_lst[0].sg_len));
2753 /* evaluate command size */
2754 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw.sg_lst) +
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002755 (u16)cmdp->u.raw.sg_ranz * sizeof(gdth_sg_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 }
2757 }
2758 /* check space */
2759 if (ha->cmd_len & 3)
2760 ha->cmd_len += (4 - (ha->cmd_len & 3));
2761
2762 if (ha->cmd_cnt > 0) {
2763 if ((ha->cmd_offs_dpmem + ha->cmd_len + DPMEM_COMMAND_OFFSET) >
2764 ha->ic_all_size) {
2765 TRACE2(("gdth_fill_raw() DPMEM overflow\n"));
2766 ha->cmd_tab[cmd_index-2].cmnd = UNUSED_CMND;
2767 return 0;
2768 }
2769 }
2770
2771 /* copy command */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002772 gdth_copy_command(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 return cmd_index;
2774}
2775
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002776static int gdth_special_cmd(gdth_ha_str *ha, Scsi_Cmnd *scp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 register gdth_cmd_str *cmdp;
Boaz Harroshee54cc62008-02-27 15:29:15 -08002779 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 int cmd_index;
2781
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 cmdp= ha->pccb;
2783 TRACE2(("gdth_special_cmd(): "));
2784
2785 if (ha->type==GDT_EISA && ha->cmd_cnt>0)
2786 return 0;
2787
Boaz Harroshee54cc62008-02-27 15:29:15 -08002788 *cmdp = *cmndinfo->internal_cmd_str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 cmdp->RequestBuffer = scp;
2790
2791 /* search free command index */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002792 if (!(cmd_index=gdth_get_cmd_index(ha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 TRACE(("GDT: No free command index found\n"));
2794 return 0;
2795 }
2796
2797 /* if it's the first command, set command semaphore */
2798 if (ha->cmd_cnt == 0)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002799 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
2801 /* evaluate command size, check space */
2802 if (cmdp->OpCode == GDT_IOCTL) {
2803 TRACE2(("IOCTL\n"));
2804 ha->cmd_len =
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002805 GDTOFFSOF(gdth_cmd_str,u.ioctl.p_param) + sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 } else if (cmdp->Service == CACHESERVICE) {
2807 TRACE2(("cache command %d\n",cmdp->OpCode));
2808 if (ha->cache_feat & GDT_64BIT)
2809 ha->cmd_len =
2810 GDTOFFSOF(gdth_cmd_str,u.cache64.sg_lst) + sizeof(gdth_sg64_str);
2811 else
2812 ha->cmd_len =
2813 GDTOFFSOF(gdth_cmd_str,u.cache.sg_lst) + sizeof(gdth_sg_str);
2814 } else if (cmdp->Service == SCSIRAWSERVICE) {
2815 TRACE2(("raw command %d\n",cmdp->OpCode));
2816 if (ha->raw_feat & GDT_64BIT)
2817 ha->cmd_len =
2818 GDTOFFSOF(gdth_cmd_str,u.raw64.sg_lst) + sizeof(gdth_sg64_str);
2819 else
2820 ha->cmd_len =
2821 GDTOFFSOF(gdth_cmd_str,u.raw.sg_lst) + sizeof(gdth_sg_str);
2822 }
2823
2824 if (ha->cmd_len & 3)
2825 ha->cmd_len += (4 - (ha->cmd_len & 3));
2826
2827 if (ha->cmd_cnt > 0) {
2828 if ((ha->cmd_offs_dpmem + ha->cmd_len + DPMEM_COMMAND_OFFSET) >
2829 ha->ic_all_size) {
2830 TRACE2(("gdth_special_cmd() DPMEM overflow\n"));
2831 ha->cmd_tab[cmd_index-2].cmnd = UNUSED_CMND;
2832 return 0;
2833 }
2834 }
2835
2836 /* copy command */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002837 gdth_copy_command(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 return cmd_index;
2839}
2840
2841
2842/* Controller event handling functions */
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002843static gdth_evt_str *gdth_store_event(gdth_ha_str *ha, u16 source,
2844 u16 idx, gdth_evt_data *evt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845{
2846 gdth_evt_str *e;
2847 struct timeval tv;
2848
2849 /* no GDTH_LOCK_HA() ! */
2850 TRACE2(("gdth_store_event() source %d idx %d\n", source, idx));
2851 if (source == 0) /* no source -> no event */
2852 return NULL;
2853
2854 if (ebuffer[elastidx].event_source == source &&
2855 ebuffer[elastidx].event_idx == idx &&
2856 ((evt->size != 0 && ebuffer[elastidx].event_data.size != 0 &&
2857 !memcmp((char *)&ebuffer[elastidx].event_data.eu,
2858 (char *)&evt->eu, evt->size)) ||
2859 (evt->size == 0 && ebuffer[elastidx].event_data.size == 0 &&
2860 !strcmp((char *)&ebuffer[elastidx].event_data.event_string,
2861 (char *)&evt->event_string)))) {
2862 e = &ebuffer[elastidx];
2863 do_gettimeofday(&tv);
2864 e->last_stamp = tv.tv_sec;
2865 ++e->same_count;
2866 } else {
2867 if (ebuffer[elastidx].event_source != 0) { /* entry not free ? */
2868 ++elastidx;
2869 if (elastidx == MAX_EVENTS)
2870 elastidx = 0;
2871 if (elastidx == eoldidx) { /* reached mark ? */
2872 ++eoldidx;
2873 if (eoldidx == MAX_EVENTS)
2874 eoldidx = 0;
2875 }
2876 }
2877 e = &ebuffer[elastidx];
2878 e->event_source = source;
2879 e->event_idx = idx;
2880 do_gettimeofday(&tv);
2881 e->first_stamp = e->last_stamp = tv.tv_sec;
2882 e->same_count = 1;
2883 e->event_data = *evt;
2884 e->application = 0;
2885 }
2886 return e;
2887}
2888
2889static int gdth_read_event(gdth_ha_str *ha, int handle, gdth_evt_str *estr)
2890{
2891 gdth_evt_str *e;
2892 int eindex;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002893 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
2895 TRACE2(("gdth_read_event() handle %d\n", handle));
2896 spin_lock_irqsave(&ha->smp_lock, flags);
2897 if (handle == -1)
2898 eindex = eoldidx;
2899 else
2900 eindex = handle;
2901 estr->event_source = 0;
2902
Dave Jones690e7442009-10-19 19:55:13 -04002903 if (eindex < 0 || eindex >= MAX_EVENTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 spin_unlock_irqrestore(&ha->smp_lock, flags);
2905 return eindex;
2906 }
2907 e = &ebuffer[eindex];
2908 if (e->event_source != 0) {
2909 if (eindex != elastidx) {
2910 if (++eindex == MAX_EVENTS)
2911 eindex = 0;
2912 } else {
2913 eindex = -1;
2914 }
2915 memcpy(estr, e, sizeof(gdth_evt_str));
2916 }
2917 spin_unlock_irqrestore(&ha->smp_lock, flags);
2918 return eindex;
2919}
2920
2921static void gdth_readapp_event(gdth_ha_str *ha,
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002922 u8 application, gdth_evt_str *estr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923{
2924 gdth_evt_str *e;
2925 int eindex;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002926 unsigned long flags;
2927 u8 found = FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
2929 TRACE2(("gdth_readapp_event() app. %d\n", application));
2930 spin_lock_irqsave(&ha->smp_lock, flags);
2931 eindex = eoldidx;
2932 for (;;) {
2933 e = &ebuffer[eindex];
2934 if (e->event_source == 0)
2935 break;
2936 if ((e->application & application) == 0) {
2937 e->application |= application;
2938 found = TRUE;
2939 break;
2940 }
2941 if (eindex == elastidx)
2942 break;
2943 if (++eindex == MAX_EVENTS)
2944 eindex = 0;
2945 }
2946 if (found)
2947 memcpy(estr, e, sizeof(gdth_evt_str));
2948 else
2949 estr->event_source = 0;
2950 spin_unlock_irqrestore(&ha->smp_lock, flags);
2951}
2952
2953static void gdth_clear_events(void)
2954{
2955 TRACE(("gdth_clear_events()"));
2956
2957 eoldidx = elastidx = 0;
2958 ebuffer[0].event_source = 0;
2959}
2960
2961
2962/* SCSI interface functions */
2963
Jeff Garzik230e8862007-12-13 16:14:12 -08002964static irqreturn_t __gdth_interrupt(gdth_ha_str *ha,
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002965 int gdth_from_wait, int* pIndex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 gdt6m_dpram_str __iomem *dp6m_ptr = NULL;
2968 gdt6_dpram_str __iomem *dp6_ptr;
2969 gdt2_dpram_str __iomem *dp2_ptr;
2970 Scsi_Cmnd *scp;
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002971 int rval, i;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002972 u8 IStatus;
2973 u16 Service;
2974 unsigned long flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975#ifdef INT_COAL
2976 int coalesced = FALSE;
2977 int next = FALSE;
2978 gdth_coal_status *pcs = NULL;
2979 int act_int_coal = 0;
2980#endif
2981
Jeff Garzik230e8862007-12-13 16:14:12 -08002982 TRACE(("gdth_interrupt() IRQ %d\n", ha->irq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
2984 /* if polling and not from gdth_wait() -> return */
2985 if (gdth_polling) {
2986 if (!gdth_from_wait) {
2987 return IRQ_HANDLED;
2988 }
2989 }
2990
2991 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002992 spin_lock_irqsave(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
2994 /* search controller */
Jeff Garzik230e8862007-12-13 16:14:12 -08002995 IStatus = gdth_get_status(ha);
2996 if (IStatus == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 /* spurious interrupt */
2998 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002999 spin_unlock_irqrestore(&ha->smp_lock, flags);
3000 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
3003#ifdef GDTH_STATISTICS
3004 ++act_ints;
3005#endif
3006
3007#ifdef INT_COAL
3008 /* See if the fw is returning coalesced status */
3009 if (IStatus == COALINDEX) {
3010 /* Coalesced status. Setup the initial status
3011 buffer pointer and flags */
3012 pcs = ha->coal_stat;
3013 coalesced = TRUE;
3014 next = TRUE;
3015 }
3016
3017 do {
3018 if (coalesced) {
3019 /* For coalesced requests all status
3020 information is found in the status buffer */
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003021 IStatus = (u8)(pcs->status & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 }
3023#endif
3024
3025 if (ha->type == GDT_EISA) {
3026 if (IStatus & 0x80) { /* error flag */
3027 IStatus &= ~0x80;
3028 ha->status = inw(ha->bmic + MAILBOXREG+8);
3029 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
3030 } else /* no error */
3031 ha->status = S_OK;
3032 ha->info = inl(ha->bmic + MAILBOXREG+12);
3033 ha->service = inw(ha->bmic + MAILBOXREG+10);
3034 ha->info2 = inl(ha->bmic + MAILBOXREG+4);
3035
3036 outb(0xff, ha->bmic + EDOORREG); /* acknowledge interrupt */
3037 outb(0x00, ha->bmic + SEMA1REG); /* reset status semaphore */
3038 } else if (ha->type == GDT_ISA) {
3039 dp2_ptr = ha->brd;
3040 if (IStatus & 0x80) { /* error flag */
3041 IStatus &= ~0x80;
Jeff Garzika52667f2007-10-02 22:55:53 +02003042 ha->status = readw(&dp2_ptr->u.ic.Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
3044 } else /* no error */
3045 ha->status = S_OK;
Jeff Garzika52667f2007-10-02 22:55:53 +02003046 ha->info = readl(&dp2_ptr->u.ic.Info[0]);
3047 ha->service = readw(&dp2_ptr->u.ic.Service);
3048 ha->info2 = readl(&dp2_ptr->u.ic.Info[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
Jeff Garzika52667f2007-10-02 22:55:53 +02003050 writeb(0xff, &dp2_ptr->io.irqdel); /* acknowledge interrupt */
3051 writeb(0, &dp2_ptr->u.ic.Cmd_Index);/* reset command index */
3052 writeb(0, &dp2_ptr->io.Sema1); /* reset status semaphore */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 } else if (ha->type == GDT_PCI) {
3054 dp6_ptr = ha->brd;
3055 if (IStatus & 0x80) { /* error flag */
3056 IStatus &= ~0x80;
Jeff Garzika52667f2007-10-02 22:55:53 +02003057 ha->status = readw(&dp6_ptr->u.ic.Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
3059 } else /* no error */
3060 ha->status = S_OK;
Jeff Garzika52667f2007-10-02 22:55:53 +02003061 ha->info = readl(&dp6_ptr->u.ic.Info[0]);
3062 ha->service = readw(&dp6_ptr->u.ic.Service);
3063 ha->info2 = readl(&dp6_ptr->u.ic.Info[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
Jeff Garzika52667f2007-10-02 22:55:53 +02003065 writeb(0xff, &dp6_ptr->io.irqdel); /* acknowledge interrupt */
3066 writeb(0, &dp6_ptr->u.ic.Cmd_Index);/* reset command index */
3067 writeb(0, &dp6_ptr->io.Sema1); /* reset status semaphore */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 } else if (ha->type == GDT_PCINEW) {
3069 if (IStatus & 0x80) { /* error flag */
3070 IStatus &= ~0x80;
3071 ha->status = inw(PTR2USHORT(&ha->plx->status));
3072 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
3073 } else
3074 ha->status = S_OK;
3075 ha->info = inl(PTR2USHORT(&ha->plx->info[0]));
3076 ha->service = inw(PTR2USHORT(&ha->plx->service));
3077 ha->info2 = inl(PTR2USHORT(&ha->plx->info[1]));
3078
3079 outb(0xff, PTR2USHORT(&ha->plx->edoor_reg));
3080 outb(0x00, PTR2USHORT(&ha->plx->sema1_reg));
3081 } else if (ha->type == GDT_PCIMPR) {
3082 dp6m_ptr = ha->brd;
3083 if (IStatus & 0x80) { /* error flag */
3084 IStatus &= ~0x80;
3085#ifdef INT_COAL
3086 if (coalesced)
Jean Delvare107e7162006-11-09 21:45:09 +01003087 ha->status = pcs->ext_status & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 else
3089#endif
Jeff Garzika52667f2007-10-02 22:55:53 +02003090 ha->status = readw(&dp6m_ptr->i960r.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
3092 } else /* no error */
3093 ha->status = S_OK;
3094#ifdef INT_COAL
3095 /* get information */
3096 if (coalesced) {
3097 ha->info = pcs->info0;
3098 ha->info2 = pcs->info1;
Jean Delvare107e7162006-11-09 21:45:09 +01003099 ha->service = (pcs->ext_status >> 16) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 } else
3101#endif
3102 {
Jeff Garzika52667f2007-10-02 22:55:53 +02003103 ha->info = readl(&dp6m_ptr->i960r.info[0]);
3104 ha->service = readw(&dp6m_ptr->i960r.service);
3105 ha->info2 = readl(&dp6m_ptr->i960r.info[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 }
3107 /* event string */
3108 if (IStatus == ASYNCINDEX) {
3109 if (ha->service != SCREENSERVICE &&
3110 (ha->fw_vers & 0xff) >= 0x1a) {
Jeff Garzika52667f2007-10-02 22:55:53 +02003111 ha->dvr.severity = readb
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 (&((gdt6m_dpram_str __iomem *)ha->brd)->i960r.severity);
3113 for (i = 0; i < 256; ++i) {
Jeff Garzika52667f2007-10-02 22:55:53 +02003114 ha->dvr.event_string[i] = readb
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 (&((gdt6m_dpram_str __iomem *)ha->brd)->i960r.evt_str[i]);
3116 if (ha->dvr.event_string[i] == 0)
3117 break;
3118 }
3119 }
3120 }
3121#ifdef INT_COAL
3122 /* Make sure that non coalesced interrupts get cleared
3123 before being handled by gdth_async_event/gdth_sync_event */
3124 if (!coalesced)
3125#endif
3126 {
Jeff Garzika52667f2007-10-02 22:55:53 +02003127 writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
3128 writeb(0, &dp6m_ptr->i960r.sema1_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 }
3130 } else {
3131 TRACE2(("gdth_interrupt() unknown controller type\n"));
3132 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003133 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 return IRQ_HANDLED;
3135 }
3136
3137 TRACE(("gdth_interrupt() index %d stat %d info %d\n",
3138 IStatus,ha->status,ha->info));
3139
3140 if (gdth_from_wait) {
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003141 *pIndex = (int)IStatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 }
3143
3144 if (IStatus == ASYNCINDEX) {
3145 TRACE2(("gdth_interrupt() async. event\n"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003146 gdth_async_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003148 spin_unlock_irqrestore(&ha->smp_lock, flags);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003149 gdth_next(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 return IRQ_HANDLED;
3151 }
3152
3153 if (IStatus == SPEZINDEX) {
3154 TRACE2(("Service unknown or not initialized !\n"));
3155 ha->dvr.size = sizeof(ha->dvr.eu.driver);
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003156 ha->dvr.eu.driver.ionode = ha->hanum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 gdth_store_event(ha, ES_DRIVER, 4, &ha->dvr);
3158 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003159 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 return IRQ_HANDLED;
3161 }
3162 scp = ha->cmd_tab[IStatus-2].cmnd;
3163 Service = ha->cmd_tab[IStatus-2].service;
3164 ha->cmd_tab[IStatus-2].cmnd = UNUSED_CMND;
3165 if (scp == UNUSED_CMND) {
3166 TRACE2(("gdth_interrupt() index to unused command (%d)\n",IStatus));
3167 ha->dvr.size = sizeof(ha->dvr.eu.driver);
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003168 ha->dvr.eu.driver.ionode = ha->hanum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 ha->dvr.eu.driver.index = IStatus;
3170 gdth_store_event(ha, ES_DRIVER, 1, &ha->dvr);
3171 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003172 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 return IRQ_HANDLED;
3174 }
3175 if (scp == INTERNAL_CMND) {
3176 TRACE(("gdth_interrupt() answer to internal command\n"));
3177 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 return IRQ_HANDLED;
3180 }
3181
3182 TRACE(("gdth_interrupt() sync. status\n"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003183 rval = gdth_sync_event(ha,Service,IStatus,scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003185 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 if (rval == 2) {
Boaz Harroshf842b642007-10-02 23:16:01 +02003187 gdth_putq(ha, scp, gdth_cmnd_priv(scp)->priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 } else if (rval == 1) {
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02003189 gdth_scsi_done(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 }
3191
3192#ifdef INT_COAL
3193 if (coalesced) {
3194 /* go to the next status in the status buffer */
3195 ++pcs;
3196#ifdef GDTH_STATISTICS
3197 ++act_int_coal;
3198 if (act_int_coal > max_int_coal) {
3199 max_int_coal = act_int_coal;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003200 printk("GDT: max_int_coal = %d\n",(u16)max_int_coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 }
3202#endif
3203 /* see if there is another status */
3204 if (pcs->status == 0)
3205 /* Stop the coalesce loop */
3206 next = FALSE;
3207 }
3208 } while (next);
3209
3210 /* coalescing only for new GDT_PCIMPR controllers available */
3211 if (ha->type == GDT_PCIMPR && coalesced) {
Jeff Garzika52667f2007-10-02 22:55:53 +02003212 writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
3213 writeb(0, &dp6m_ptr->i960r.sema1_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 }
3215#endif
3216
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003217 gdth_next(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 return IRQ_HANDLED;
3219}
3220
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003221static irqreturn_t gdth_interrupt(int irq, void *dev_id)
3222{
Jeff Garzik230e8862007-12-13 16:14:12 -08003223 gdth_ha_str *ha = dev_id;
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003224
Jeff Garzik230e8862007-12-13 16:14:12 -08003225 return __gdth_interrupt(ha, false, NULL);
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003226}
3227
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003228static int gdth_sync_event(gdth_ha_str *ha, int service, u8 index,
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003229 Scsi_Cmnd *scp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 gdth_msg_str *msg;
3232 gdth_cmd_str *cmdp;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003233 u8 b, t;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02003234 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 cmdp = ha->pccb;
3237 TRACE(("gdth_sync_event() serv %d status %d\n",
3238 service,ha->status));
3239
3240 if (service == SCREENSERVICE) {
3241 msg = ha->pmsg;
3242 TRACE(("len: %d, answer: %d, ext: %d, alen: %d\n",
3243 msg->msg_len,msg->msg_answer,msg->msg_ext,msg->msg_alen));
3244 if (msg->msg_len > MSGLEN+1)
3245 msg->msg_len = MSGLEN+1;
3246 if (msg->msg_len)
3247 if (!(msg->msg_answer && msg->msg_ext)) {
3248 msg->msg_text[msg->msg_len] = '\0';
3249 printk("%s",msg->msg_text);
3250 }
3251
3252 if (msg->msg_ext && !msg->msg_answer) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003253 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 gdth_delay(0);
3255 cmdp->Service = SCREENSERVICE;
3256 cmdp->RequestBuffer = SCREEN_CMND;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003257 gdth_get_cmd_index(ha);
3258 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 cmdp->OpCode = GDT_READ;
3260 cmdp->BoardNode = LOCALBOARD;
3261 cmdp->u.screen.reserved = 0;
3262 cmdp->u.screen.su.msg.msg_handle= msg->msg_handle;
3263 cmdp->u.screen.su.msg.msg_addr = ha->msg_phys;
3264 ha->cmd_offs_dpmem = 0;
3265 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.screen.su.msg.msg_addr)
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003266 + sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 ha->cmd_cnt = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003268 gdth_copy_command(ha);
3269 gdth_release_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 return 0;
3271 }
3272
3273 if (msg->msg_answer && msg->msg_alen) {
3274 /* default answers (getchar() not possible) */
3275 if (msg->msg_alen == 1) {
3276 msg->msg_alen = 0;
3277 msg->msg_len = 1;
3278 msg->msg_text[0] = 0;
3279 } else {
3280 msg->msg_alen -= 2;
3281 msg->msg_len = 2;
3282 msg->msg_text[0] = 1;
3283 msg->msg_text[1] = 0;
3284 }
3285 msg->msg_ext = 0;
3286 msg->msg_answer = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003287 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 gdth_delay(0);
3289 cmdp->Service = SCREENSERVICE;
3290 cmdp->RequestBuffer = SCREEN_CMND;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003291 gdth_get_cmd_index(ha);
3292 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 cmdp->OpCode = GDT_WRITE;
3294 cmdp->BoardNode = LOCALBOARD;
3295 cmdp->u.screen.reserved = 0;
3296 cmdp->u.screen.su.msg.msg_handle= msg->msg_handle;
3297 cmdp->u.screen.su.msg.msg_addr = ha->msg_phys;
3298 ha->cmd_offs_dpmem = 0;
3299 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.screen.su.msg.msg_addr)
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003300 + sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 ha->cmd_cnt = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003302 gdth_copy_command(ha);
3303 gdth_release_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 return 0;
3305 }
3306 printk("\n");
3307
3308 } else {
Christoph Hellwig52759e62007-10-02 22:59:53 +02003309 b = scp->device->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 t = scp->device->id;
Boaz Harroshf842b642007-10-02 23:16:01 +02003311 if (cmndinfo->OpCode == -1 && b != ha->virt_bus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 ha->raw[BUS_L2P(ha,b)].io_cnt[t]--;
3313 }
3314 /* cache or raw service */
3315 if (ha->status == S_BSY) {
3316 TRACE2(("Controller busy -> retry !\n"));
Boaz Harroshf842b642007-10-02 23:16:01 +02003317 if (cmndinfo->OpCode == GDT_MOUNT)
3318 cmndinfo->OpCode = GDT_CLUST_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 /* retry */
3320 return 2;
3321 }
Boaz Harroshd35055a2008-02-27 15:31:20 -08003322 if (scsi_bufflen(scp))
3323 pci_unmap_sg(ha->pdev, scsi_sglist(scp), scsi_sg_count(scp),
Boaz Harrosh3892d882007-10-02 23:18:03 +02003324 cmndinfo->dma_dir);
3325
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02003326 if (cmndinfo->sense_paddr)
3327 pci_unmap_page(ha->pdev, cmndinfo->sense_paddr, 16,
3328 PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329
3330 if (ha->status == S_OK) {
Boaz Harroshf842b642007-10-02 23:16:01 +02003331 cmndinfo->status = S_OK;
3332 cmndinfo->info = ha->info;
3333 if (cmndinfo->OpCode != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 TRACE2(("gdth_sync_event(): special cmd 0x%x OK\n",
Boaz Harroshf842b642007-10-02 23:16:01 +02003335 cmndinfo->OpCode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 /* special commands GDT_CLUST_INFO/GDT_MOUNT ? */
Boaz Harroshf842b642007-10-02 23:16:01 +02003337 if (cmndinfo->OpCode == GDT_CLUST_INFO) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003338 ha->hdr[t].cluster_type = (u8)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 if (!(ha->hdr[t].cluster_type &
3340 CLUSTER_MOUNTED)) {
3341 /* NOT MOUNTED -> MOUNT */
Boaz Harroshf842b642007-10-02 23:16:01 +02003342 cmndinfo->OpCode = GDT_MOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 if (ha->hdr[t].cluster_type &
3344 CLUSTER_RESERVED) {
3345 /* cluster drive RESERVED (on the other node) */
Boaz Harroshf842b642007-10-02 23:16:01 +02003346 cmndinfo->phase = -2; /* reservation conflict */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 }
3348 } else {
Boaz Harroshf842b642007-10-02 23:16:01 +02003349 cmndinfo->OpCode = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 }
3351 } else {
Boaz Harroshf842b642007-10-02 23:16:01 +02003352 if (cmndinfo->OpCode == GDT_MOUNT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 ha->hdr[t].cluster_type |= CLUSTER_MOUNTED;
3354 ha->hdr[t].media_changed = TRUE;
Boaz Harroshf842b642007-10-02 23:16:01 +02003355 } else if (cmndinfo->OpCode == GDT_UNMOUNT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 ha->hdr[t].cluster_type &= ~CLUSTER_MOUNTED;
3357 ha->hdr[t].media_changed = TRUE;
3358 }
Boaz Harroshf842b642007-10-02 23:16:01 +02003359 cmndinfo->OpCode = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 }
3361 /* retry */
Boaz Harroshf842b642007-10-02 23:16:01 +02003362 cmndinfo->priority = HIGH_PRI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 return 2;
3364 } else {
3365 /* RESERVE/RELEASE ? */
3366 if (scp->cmnd[0] == RESERVE) {
3367 ha->hdr[t].cluster_type |= CLUSTER_RESERVED;
3368 } else if (scp->cmnd[0] == RELEASE) {
3369 ha->hdr[t].cluster_type &= ~CLUSTER_RESERVED;
3370 }
3371 scp->result = DID_OK << 16;
3372 scp->sense_buffer[0] = 0;
3373 }
3374 } else {
Boaz Harroshf842b642007-10-02 23:16:01 +02003375 cmndinfo->status = ha->status;
3376 cmndinfo->info = ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377
Boaz Harroshf842b642007-10-02 23:16:01 +02003378 if (cmndinfo->OpCode != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 TRACE2(("gdth_sync_event(): special cmd 0x%x error 0x%x\n",
Boaz Harroshf842b642007-10-02 23:16:01 +02003380 cmndinfo->OpCode, ha->status));
3381 if (cmndinfo->OpCode == GDT_SCAN_START ||
3382 cmndinfo->OpCode == GDT_SCAN_END) {
3383 cmndinfo->OpCode = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 /* retry */
Boaz Harroshf842b642007-10-02 23:16:01 +02003385 cmndinfo->priority = HIGH_PRI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 return 2;
3387 }
3388 memset((char*)scp->sense_buffer,0,16);
3389 scp->sense_buffer[0] = 0x70;
3390 scp->sense_buffer[2] = NOT_READY;
3391 scp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
3392 } else if (service == CACHESERVICE) {
3393 if (ha->status == S_CACHE_UNKNOWN &&
3394 (ha->hdr[t].cluster_type &
3395 CLUSTER_RESERVE_STATE) == CLUSTER_RESERVE_STATE) {
3396 /* bus reset -> force GDT_CLUST_INFO */
3397 ha->hdr[t].cluster_type &= ~CLUSTER_RESERVED;
3398 }
3399 memset((char*)scp->sense_buffer,0,16);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003400 if (ha->status == (u16)S_CACHE_RESERV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 scp->result = (DID_OK << 16) | (RESERVATION_CONFLICT << 1);
3402 } else {
3403 scp->sense_buffer[0] = 0x70;
3404 scp->sense_buffer[2] = NOT_READY;
3405 scp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
3406 }
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02003407 if (!cmndinfo->internal_command) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 ha->dvr.size = sizeof(ha->dvr.eu.sync);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003409 ha->dvr.eu.sync.ionode = ha->hanum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 ha->dvr.eu.sync.service = service;
3411 ha->dvr.eu.sync.status = ha->status;
3412 ha->dvr.eu.sync.info = ha->info;
3413 ha->dvr.eu.sync.hostdrive = t;
3414 if (ha->status >= 0x8000)
3415 gdth_store_event(ha, ES_SYNC, 0, &ha->dvr);
3416 else
3417 gdth_store_event(ha, ES_SYNC, service, &ha->dvr);
3418 }
3419 } else {
3420 /* sense buffer filled from controller firmware (DMA) */
3421 if (ha->status != S_RAW_SCSI || ha->info >= 0x100) {
3422 scp->result = DID_BAD_TARGET << 16;
3423 } else {
3424 scp->result = (DID_OK << 16) | ha->info;
3425 }
3426 }
3427 }
Boaz Harroshf842b642007-10-02 23:16:01 +02003428 if (!cmndinfo->wait_for_completion)
3429 cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 else
3431 return 1;
3432 }
3433
3434 return 0;
3435}
3436
3437static char *async_cache_tab[] = {
3438/* 0*/ "\011\000\002\002\002\004\002\006\004"
3439 "GDT HA %u, service %u, async. status %u/%lu unknown",
3440/* 1*/ "\011\000\002\002\002\004\002\006\004"
3441 "GDT HA %u, service %u, async. status %u/%lu unknown",
3442/* 2*/ "\005\000\002\006\004"
3443 "GDT HA %u, Host Drive %lu not ready",
3444/* 3*/ "\005\000\002\006\004"
3445 "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",
3446/* 4*/ "\005\000\002\006\004"
3447 "GDT HA %u, mirror update on Host Drive %lu failed",
3448/* 5*/ "\005\000\002\006\004"
3449 "GDT HA %u, Mirror Drive %lu failed",
3450/* 6*/ "\005\000\002\006\004"
3451 "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",
3452/* 7*/ "\005\000\002\006\004"
3453 "GDT HA %u, Host Drive %lu write protected",
3454/* 8*/ "\005\000\002\006\004"
3455 "GDT HA %u, media changed in Host Drive %lu",
3456/* 9*/ "\005\000\002\006\004"
3457 "GDT HA %u, Host Drive %lu is offline",
3458/*10*/ "\005\000\002\006\004"
3459 "GDT HA %u, media change of Mirror Drive %lu",
3460/*11*/ "\005\000\002\006\004"
3461 "GDT HA %u, Mirror Drive %lu is write protected",
3462/*12*/ "\005\000\002\006\004"
3463 "GDT HA %u, general error on Host Drive %lu. Please check the devices of this drive!",
3464/*13*/ "\007\000\002\006\002\010\002"
3465 "GDT HA %u, Array Drive %u: Cache Drive %u failed",
3466/*14*/ "\005\000\002\006\002"
3467 "GDT HA %u, Array Drive %u: FAIL state entered",
3468/*15*/ "\005\000\002\006\002"
3469 "GDT HA %u, Array Drive %u: error",
3470/*16*/ "\007\000\002\006\002\010\002"
3471 "GDT HA %u, Array Drive %u: failed drive replaced by Cache Drive %u",
3472/*17*/ "\005\000\002\006\002"
3473 "GDT HA %u, Array Drive %u: parity build failed",
3474/*18*/ "\005\000\002\006\002"
3475 "GDT HA %u, Array Drive %u: drive rebuild failed",
3476/*19*/ "\005\000\002\010\002"
3477 "GDT HA %u, Test of Hot Fix %u failed",
3478/*20*/ "\005\000\002\006\002"
3479 "GDT HA %u, Array Drive %u: drive build finished successfully",
3480/*21*/ "\005\000\002\006\002"
3481 "GDT HA %u, Array Drive %u: drive rebuild finished successfully",
3482/*22*/ "\007\000\002\006\002\010\002"
3483 "GDT HA %u, Array Drive %u: Hot Fix %u activated",
3484/*23*/ "\005\000\002\006\002"
3485 "GDT HA %u, Host Drive %u: processing of i/o aborted due to serious drive error",
3486/*24*/ "\005\000\002\010\002"
3487 "GDT HA %u, mirror update on Cache Drive %u completed",
3488/*25*/ "\005\000\002\010\002"
3489 "GDT HA %u, mirror update on Cache Drive %lu failed",
3490/*26*/ "\005\000\002\006\002"
3491 "GDT HA %u, Array Drive %u: drive rebuild started",
3492/*27*/ "\005\000\002\012\001"
3493 "GDT HA %u, Fault bus %u: SHELF OK detected",
3494/*28*/ "\005\000\002\012\001"
3495 "GDT HA %u, Fault bus %u: SHELF not OK detected",
3496/*29*/ "\007\000\002\012\001\013\001"
3497 "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug started",
3498/*30*/ "\007\000\002\012\001\013\001"
3499 "GDT HA %u, Fault bus %u, ID %u: new disk detected",
3500/*31*/ "\007\000\002\012\001\013\001"
3501 "GDT HA %u, Fault bus %u, ID %u: old disk detected",
3502/*32*/ "\007\000\002\012\001\013\001"
3503 "GDT HA %u, Fault bus %u, ID %u: plugging an active disk is invalid",
3504/*33*/ "\007\000\002\012\001\013\001"
3505 "GDT HA %u, Fault bus %u, ID %u: invalid device detected",
3506/*34*/ "\011\000\002\012\001\013\001\006\004"
3507 "GDT HA %u, Fault bus %u, ID %u: insufficient disk capacity (%lu MB required)",
3508/*35*/ "\007\000\002\012\001\013\001"
3509 "GDT HA %u, Fault bus %u, ID %u: disk write protected",
3510/*36*/ "\007\000\002\012\001\013\001"
3511 "GDT HA %u, Fault bus %u, ID %u: disk not available",
3512/*37*/ "\007\000\002\012\001\006\004"
3513 "GDT HA %u, Fault bus %u: swap detected (%lu)",
3514/*38*/ "\007\000\002\012\001\013\001"
3515 "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug finished successfully",
3516/*39*/ "\007\000\002\012\001\013\001"
3517 "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug aborted due to user Hot Plug",
3518/*40*/ "\007\000\002\012\001\013\001"
3519 "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug aborted",
3520/*41*/ "\007\000\002\012\001\013\001"
3521 "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug for Hot Fix started",
3522/*42*/ "\005\000\002\006\002"
3523 "GDT HA %u, Array Drive %u: drive build started",
3524/*43*/ "\003\000\002"
3525 "GDT HA %u, DRAM parity error detected",
3526/*44*/ "\005\000\002\006\002"
3527 "GDT HA %u, Mirror Drive %u: update started",
3528/*45*/ "\007\000\002\006\002\010\002"
3529 "GDT HA %u, Mirror Drive %u: Hot Fix %u activated",
3530/*46*/ "\005\000\002\006\002"
3531 "GDT HA %u, Array Drive %u: no matching Pool Hot Fix Drive available",
3532/*47*/ "\005\000\002\006\002"
3533 "GDT HA %u, Array Drive %u: Pool Hot Fix Drive available",
3534/*48*/ "\005\000\002\006\002"
3535 "GDT HA %u, Mirror Drive %u: no matching Pool Hot Fix Drive available",
3536/*49*/ "\005\000\002\006\002"
3537 "GDT HA %u, Mirror Drive %u: Pool Hot Fix Drive available",
3538/*50*/ "\007\000\002\012\001\013\001"
3539 "GDT HA %u, SCSI bus %u, ID %u: IGNORE_WIDE_RESIDUE message received",
3540/*51*/ "\005\000\002\006\002"
3541 "GDT HA %u, Array Drive %u: expand started",
3542/*52*/ "\005\000\002\006\002"
3543 "GDT HA %u, Array Drive %u: expand finished successfully",
3544/*53*/ "\005\000\002\006\002"
3545 "GDT HA %u, Array Drive %u: expand failed",
3546/*54*/ "\003\000\002"
3547 "GDT HA %u, CPU temperature critical",
3548/*55*/ "\003\000\002"
3549 "GDT HA %u, CPU temperature OK",
3550/*56*/ "\005\000\002\006\004"
3551 "GDT HA %u, Host drive %lu created",
3552/*57*/ "\005\000\002\006\002"
3553 "GDT HA %u, Array Drive %u: expand restarted",
3554/*58*/ "\005\000\002\006\002"
3555 "GDT HA %u, Array Drive %u: expand stopped",
3556/*59*/ "\005\000\002\010\002"
3557 "GDT HA %u, Mirror Drive %u: drive build quited",
3558/*60*/ "\005\000\002\006\002"
3559 "GDT HA %u, Array Drive %u: parity build quited",
3560/*61*/ "\005\000\002\006\002"
3561 "GDT HA %u, Array Drive %u: drive rebuild quited",
3562/*62*/ "\005\000\002\006\002"
3563 "GDT HA %u, Array Drive %u: parity verify started",
3564/*63*/ "\005\000\002\006\002"
3565 "GDT HA %u, Array Drive %u: parity verify done",
3566/*64*/ "\005\000\002\006\002"
3567 "GDT HA %u, Array Drive %u: parity verify failed",
3568/*65*/ "\005\000\002\006\002"
3569 "GDT HA %u, Array Drive %u: parity error detected",
3570/*66*/ "\005\000\002\006\002"
3571 "GDT HA %u, Array Drive %u: parity verify quited",
3572/*67*/ "\005\000\002\006\002"
3573 "GDT HA %u, Host Drive %u reserved",
3574/*68*/ "\005\000\002\006\002"
3575 "GDT HA %u, Host Drive %u mounted and released",
3576/*69*/ "\005\000\002\006\002"
3577 "GDT HA %u, Host Drive %u released",
3578/*70*/ "\003\000\002"
3579 "GDT HA %u, DRAM error detected and corrected with ECC",
3580/*71*/ "\003\000\002"
3581 "GDT HA %u, Uncorrectable DRAM error detected with ECC",
3582/*72*/ "\011\000\002\012\001\013\001\014\001"
3583 "GDT HA %u, SCSI bus %u, ID %u, LUN %u: reassigning block",
3584/*73*/ "\005\000\002\006\002"
3585 "GDT HA %u, Host drive %u resetted locally",
3586/*74*/ "\005\000\002\006\002"
3587 "GDT HA %u, Host drive %u resetted remotely",
3588/*75*/ "\003\000\002"
3589 "GDT HA %u, async. status 75 unknown",
3590};
3591
3592
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003593static int gdth_async_event(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 gdth_cmd_str *cmdp;
3596 int cmd_index;
3597
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 cmdp= ha->pccb;
3599 TRACE2(("gdth_async_event() ha %d serv %d\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003600 ha->hanum, ha->service));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
3602 if (ha->service == SCREENSERVICE) {
3603 if (ha->status == MSG_REQUEST) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003604 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 gdth_delay(0);
3606 cmdp->Service = SCREENSERVICE;
3607 cmdp->RequestBuffer = SCREEN_CMND;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003608 cmd_index = gdth_get_cmd_index(ha);
3609 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 cmdp->OpCode = GDT_READ;
3611 cmdp->BoardNode = LOCALBOARD;
3612 cmdp->u.screen.reserved = 0;
3613 cmdp->u.screen.su.msg.msg_handle= MSG_INV_HANDLE;
3614 cmdp->u.screen.su.msg.msg_addr = ha->msg_phys;
3615 ha->cmd_offs_dpmem = 0;
3616 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.screen.su.msg.msg_addr)
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003617 + sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 ha->cmd_cnt = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003619 gdth_copy_command(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 if (ha->type == GDT_EISA)
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003621 printk("[EISA slot %d] ",(u16)ha->brd_phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 else if (ha->type == GDT_ISA)
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003623 printk("[DPMEM 0x%4X] ",(u16)ha->brd_phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 else
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003625 printk("[PCI %d/%d] ",(u16)(ha->brd_phys>>8),
3626 (u16)((ha->brd_phys>>3)&0x1f));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003627 gdth_release_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 }
3629
3630 } else {
3631 if (ha->type == GDT_PCIMPR &&
3632 (ha->fw_vers & 0xff) >= 0x1a) {
3633 ha->dvr.size = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003634 ha->dvr.eu.async.ionode = ha->hanum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 ha->dvr.eu.async.status = ha->status;
3636 /* severity and event_string already set! */
3637 } else {
3638 ha->dvr.size = sizeof(ha->dvr.eu.async);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003639 ha->dvr.eu.async.ionode = ha->hanum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 ha->dvr.eu.async.service = ha->service;
3641 ha->dvr.eu.async.status = ha->status;
3642 ha->dvr.eu.async.info = ha->info;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003643 *(u32 *)ha->dvr.eu.async.scsi_coord = ha->info2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 }
3645 gdth_store_event( ha, ES_ASYNC, ha->service, &ha->dvr );
3646 gdth_log_event( &ha->dvr, NULL );
3647
3648 /* new host drive from expand? */
3649 if (ha->service == CACHESERVICE && ha->status == 56) {
3650 TRACE2(("gdth_async_event(): new host drive %d created\n",
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003651 (u16)ha->info));
3652 /* gdth_analyse_hdrive(hanum, (u16)ha->info); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 }
3654 }
3655 return 1;
3656}
3657
3658static void gdth_log_event(gdth_evt_data *dvr, char *buffer)
3659{
3660 gdth_stackframe stack;
3661 char *f = NULL;
3662 int i,j;
3663
3664 TRACE2(("gdth_log_event()\n"));
3665 if (dvr->size == 0) {
3666 if (buffer == NULL) {
3667 printk("Adapter %d: %s\n",dvr->eu.async.ionode,dvr->event_string);
3668 } else {
3669 sprintf(buffer,"Adapter %d: %s\n",
3670 dvr->eu.async.ionode,dvr->event_string);
3671 }
3672 } else if (dvr->eu.async.service == CACHESERVICE &&
3673 INDEX_OK(dvr->eu.async.status, async_cache_tab)) {
3674 TRACE2(("GDT: Async. event cache service, event no.: %d\n",
3675 dvr->eu.async.status));
3676
3677 f = async_cache_tab[dvr->eu.async.status];
3678
3679 /* i: parameter to push, j: stack element to fill */
3680 for (j=0,i=1; i < f[0]; i+=2) {
3681 switch (f[i+1]) {
3682 case 4:
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003683 stack.b[j++] = *(u32*)&dvr->eu.stream[(int)f[i]];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 break;
3685 case 2:
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003686 stack.b[j++] = *(u16*)&dvr->eu.stream[(int)f[i]];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 break;
3688 case 1:
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003689 stack.b[j++] = *(u8*)&dvr->eu.stream[(int)f[i]];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 break;
3691 default:
3692 break;
3693 }
3694 }
3695
3696 if (buffer == NULL) {
3697 printk(&f[(int)f[0]],stack);
3698 printk("\n");
3699 } else {
3700 sprintf(buffer,&f[(int)f[0]],stack);
3701 }
3702
3703 } else {
3704 if (buffer == NULL) {
3705 printk("GDT HA %u, Unknown async. event service %d event no. %d\n",
3706 dvr->eu.async.ionode,dvr->eu.async.service,dvr->eu.async.status);
3707 } else {
3708 sprintf(buffer,"GDT HA %u, Unknown async. event service %d event no. %d",
3709 dvr->eu.async.ionode,dvr->eu.async.service,dvr->eu.async.status);
3710 }
3711 }
3712}
3713
3714#ifdef GDTH_STATISTICS
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003715static u8 gdth_timer_running;
James Bottomley2d6f0d02008-05-04 22:34:49 -05003716
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003717static void gdth_timeout(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003719 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 Scsi_Cmnd *nscp;
3721 gdth_ha_str *ha;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003722 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723
James Bottomley2d6f0d02008-05-04 22:34:49 -05003724 if(unlikely(list_empty(&gdth_instances))) {
3725 gdth_timer_running = 0;
3726 return;
3727 }
Boaz Harroshb31ddd32008-02-27 15:27:16 -08003728
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02003729 ha = list_first_entry(&gdth_instances, gdth_ha_str, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 spin_lock_irqsave(&ha->smp_lock, flags);
3731
3732 for (act_stats=0,i=0; i<GDTH_MAXCMDS; ++i)
3733 if (ha->cmd_tab[i].cmnd != UNUSED_CMND)
3734 ++act_stats;
3735
3736 for (act_rq=0,nscp=ha->req_first; nscp; nscp=(Scsi_Cmnd*)nscp->SCp.ptr)
3737 ++act_rq;
3738
3739 TRACE2(("gdth_to(): ints %d, ios %d, act_stats %d, act_rq %d\n",
3740 act_ints, act_ios, act_stats, act_rq));
3741 act_ints = act_ios = 0;
3742
3743 gdth_timer.expires = jiffies + 30 * HZ;
3744 add_timer(&gdth_timer);
3745 spin_unlock_irqrestore(&ha->smp_lock, flags);
3746}
James Bottomley2d6f0d02008-05-04 22:34:49 -05003747
3748static void gdth_timer_init(void)
3749{
3750 if (gdth_timer_running)
3751 return;
3752 gdth_timer_running = 1;
3753 TRACE2(("gdth_detect(): Initializing timer !\n"));
3754 gdth_timer.expires = jiffies + HZ;
3755 gdth_timer.data = 0L;
3756 gdth_timer.function = gdth_timeout;
3757 add_timer(&gdth_timer);
3758}
3759#else
3760static inline void gdth_timer_init(void)
3761{
3762}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763#endif
3764
8e879042005-04-17 15:28:39 -05003765static void __init internal_setup(char *str,int *ints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766{
3767 int i, argc;
3768 char *cur_str, *argv;
3769
3770 TRACE2(("internal_setup() str %s ints[0] %d\n",
3771 str ? str:"NULL", ints ? ints[0]:0));
3772
3773 /* read irq[] from ints[] */
3774 if (ints) {
3775 argc = ints[0];
3776 if (argc > 0) {
3777 if (argc > MAXHA)
3778 argc = MAXHA;
3779 for (i = 0; i < argc; ++i)
3780 irq[i] = ints[i+1];
3781 }
3782 }
3783
3784 /* analyse string */
3785 argv = str;
3786 while (argv && (cur_str = strchr(argv, ':'))) {
3787 int val = 0, c = *++cur_str;
3788
3789 if (c == 'n' || c == 'N')
3790 val = 0;
3791 else if (c == 'y' || c == 'Y')
3792 val = 1;
3793 else
3794 val = (int)simple_strtoul(cur_str, NULL, 0);
3795
3796 if (!strncmp(argv, "disable:", 8))
3797 disable = val;
3798 else if (!strncmp(argv, "reserve_mode:", 13))
3799 reserve_mode = val;
3800 else if (!strncmp(argv, "reverse_scan:", 13))
3801 reverse_scan = val;
3802 else if (!strncmp(argv, "hdr_channel:", 12))
3803 hdr_channel = val;
3804 else if (!strncmp(argv, "max_ids:", 8))
3805 max_ids = val;
3806 else if (!strncmp(argv, "rescan:", 7))
3807 rescan = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 else if (!strncmp(argv, "shared_access:", 14))
3809 shared_access = val;
3810 else if (!strncmp(argv, "probe_eisa_isa:", 15))
3811 probe_eisa_isa = val;
3812 else if (!strncmp(argv, "reserve_list:", 13)) {
3813 reserve_list[0] = val;
3814 for (i = 1; i < MAX_RES_ARGS; i++) {
3815 cur_str = strchr(cur_str, ',');
3816 if (!cur_str)
3817 break;
3818 if (!isdigit((int)*++cur_str)) {
3819 --cur_str;
3820 break;
3821 }
3822 reserve_list[i] =
3823 (int)simple_strtoul(cur_str, NULL, 0);
3824 }
3825 if (!cur_str)
3826 break;
3827 argv = ++cur_str;
3828 continue;
3829 }
3830
3831 if ((argv = strchr(argv, ',')))
3832 ++argv;
3833 }
3834}
3835
3836int __init option_setup(char *str)
3837{
3838 int ints[MAXHA];
3839 char *cur = str;
3840 int i = 1;
3841
3842 TRACE2(("option_setup() str %s\n", str ? str:"NULL"));
3843
3844 while (cur && isdigit(*cur) && i <= MAXHA) {
3845 ints[i++] = simple_strtoul(cur, NULL, 0);
3846 if ((cur = strchr(cur, ',')) != NULL) cur++;
3847 }
3848
3849 ints[0] = i - 1;
3850 internal_setup(cur, ints);
3851 return 1;
3852}
3853
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003854static const char *gdth_ctr_name(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 TRACE2(("gdth_ctr_name()\n"));
3857
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 if (ha->type == GDT_EISA) {
3859 switch (ha->stype) {
3860 case GDT3_ID:
3861 return("GDT3000/3020");
3862 case GDT3A_ID:
3863 return("GDT3000A/3020A/3050A");
3864 case GDT3B_ID:
3865 return("GDT3000B/3010A");
3866 }
3867 } else if (ha->type == GDT_ISA) {
3868 return("GDT2000/2020");
3869 } else if (ha->type == GDT_PCI) {
Jeff Garzik8e9a8a02007-07-17 05:25:17 -04003870 switch (ha->pdev->device) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 case PCI_DEVICE_ID_VORTEX_GDT60x0:
3872 return("GDT6000/6020/6050");
3873 case PCI_DEVICE_ID_VORTEX_GDT6000B:
3874 return("GDT6000B/6010");
3875 }
3876 }
3877 /* new controllers (GDT_PCINEW, GDT_PCIMPR, ..) use board_info IOCTL! */
3878
3879 return("");
3880}
3881
8e879042005-04-17 15:28:39 -05003882static const char *gdth_info(struct Scsi_Host *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883{
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003884 gdth_ha_str *ha = shost_priv(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885
3886 TRACE2(("gdth_info()\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 return ((const char *)ha->binfo.type_string);
3888}
3889
Jens Axboe242f9dc2008-09-14 05:55:09 -07003890static enum blk_eh_timer_return gdth_timed_out(struct scsi_cmnd *scp)
3891{
3892 gdth_ha_str *ha = shost_priv(scp->device->host);
3893 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003894 u8 b, t;
3895 unsigned long flags;
Jens Axboe242f9dc2008-09-14 05:55:09 -07003896 enum blk_eh_timer_return retval = BLK_EH_NOT_HANDLED;
3897
3898 TRACE(("%s() cmd 0x%x\n", scp->cmnd[0], __func__));
3899 b = scp->device->channel;
3900 t = scp->device->id;
3901
3902 /*
3903 * We don't really honor the command timeout, but we try to
3904 * honor 6 times of the actual command timeout! So reset the
3905 * timer if this is less than 6th timeout on this command!
3906 */
3907 if (++cmndinfo->timeout_count < 6)
3908 retval = BLK_EH_RESET_TIMER;
3909
3910 /* Reset the timeout if it is locked IO */
3911 spin_lock_irqsave(&ha->smp_lock, flags);
3912 if ((b != ha->virt_bus && ha->raw[BUS_L2P(ha, b)].lock) ||
3913 (b == ha->virt_bus && t < MAX_HDRIVES && ha->hdr[t].lock)) {
3914 TRACE2(("%s(): locked IO, reset timeout\n", __func__));
3915 retval = BLK_EH_RESET_TIMER;
3916 }
3917 spin_unlock_irqrestore(&ha->smp_lock, flags);
3918
3919 return retval;
3920}
3921
3922
8e879042005-04-17 15:28:39 -05003923static int gdth_eh_bus_reset(Scsi_Cmnd *scp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924{
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003925 gdth_ha_str *ha = shost_priv(scp->device->host);
3926 int i;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003927 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 Scsi_Cmnd *cmnd;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003929 u8 b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
3931 TRACE2(("gdth_eh_bus_reset()\n"));
3932
Christoph Hellwig52759e62007-10-02 22:59:53 +02003933 b = scp->device->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934
3935 /* clear command tab */
3936 spin_lock_irqsave(&ha->smp_lock, flags);
3937 for (i = 0; i < GDTH_MAXCMDS; ++i) {
3938 cmnd = ha->cmd_tab[i].cmnd;
3939 if (!SPECIAL_SCP(cmnd) && cmnd->device->channel == b)
3940 ha->cmd_tab[i].cmnd = UNUSED_CMND;
3941 }
3942 spin_unlock_irqrestore(&ha->smp_lock, flags);
3943
3944 if (b == ha->virt_bus) {
3945 /* host drives */
3946 for (i = 0; i < MAX_HDRIVES; ++i) {
3947 if (ha->hdr[i].present) {
3948 spin_lock_irqsave(&ha->smp_lock, flags);
3949 gdth_polling = TRUE;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003950 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 gdth_delay(0);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003952 if (gdth_internal_cmd(ha, CACHESERVICE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 GDT_CLUST_RESET, i, 0, 0))
3954 ha->hdr[i].cluster_type &= ~CLUSTER_RESERVED;
3955 gdth_polling = FALSE;
3956 spin_unlock_irqrestore(&ha->smp_lock, flags);
3957 }
3958 }
3959 } else {
3960 /* raw devices */
3961 spin_lock_irqsave(&ha->smp_lock, flags);
3962 for (i = 0; i < MAXID; ++i)
3963 ha->raw[BUS_L2P(ha,b)].io_cnt[i] = 0;
3964 gdth_polling = TRUE;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003965 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 gdth_delay(0);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003967 gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_RESET_BUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 BUS_L2P(ha,b), 0, 0);
3969 gdth_polling = FALSE;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07003970 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 }
3972 return SUCCESS;
3973}
3974
8e879042005-04-17 15:28:39 -05003975static int gdth_bios_param(struct scsi_device *sdev,struct block_device *bdev,sector_t cap,int *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003977 u8 b, t;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003978 gdth_ha_str *ha = shost_priv(sdev->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 struct scsi_device *sd;
3980 unsigned capacity;
3981
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 sd = sdev;
3983 capacity = cap;
Christoph Hellwig52759e62007-10-02 22:59:53 +02003984 b = sd->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 t = sd->id;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003986 TRACE2(("gdth_bios_param() ha %d bus %d target %d\n", ha->hanum, b, t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987
3988 if (b != ha->virt_bus || ha->hdr[t].heads == 0) {
3989 /* raw device or host drive without mapping information */
3990 TRACE2(("Evaluate mapping\n"));
3991 gdth_eval_mapping(capacity,&ip[2],&ip[0],&ip[1]);
3992 } else {
3993 ip[0] = ha->hdr[t].heads;
3994 ip[1] = ha->hdr[t].secs;
3995 ip[2] = capacity / ip[0] / ip[1];
3996 }
3997
3998 TRACE2(("gdth_bios_param(): %d heads, %d secs, %d cyls\n",
3999 ip[0],ip[1],ip[2]));
4000 return 0;
4001}
4002
4003
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02004004static int gdth_queuecommand(struct scsi_cmnd *scp,
4005 void (*done)(struct scsi_cmnd *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006{
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004007 gdth_ha_str *ha = shost_priv(scp->device->host);
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02004008 struct gdth_cmndinfo *cmndinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009
4010 TRACE(("gdth_queuecommand() cmd 0x%x\n", scp->cmnd[0]));
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02004011
4012 cmndinfo = gdth_get_cmndinfo(ha);
4013 BUG_ON(!cmndinfo);
4014
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02004015 scp->scsi_done = done;
Jens Axboe242f9dc2008-09-14 05:55:09 -07004016 cmndinfo->timeout_count = 0;
Boaz Harroshf842b642007-10-02 23:16:01 +02004017 cmndinfo->priority = DEFAULT_PRI;
Boaz Harrosh3892d882007-10-02 23:18:03 +02004018
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02004019 return __gdth_queuecommand(ha, scp, cmndinfo);
4020}
4021
4022static int __gdth_queuecommand(gdth_ha_str *ha, struct scsi_cmnd *scp,
4023 struct gdth_cmndinfo *cmndinfo)
4024{
4025 scp->host_scribble = (unsigned char *)cmndinfo;
Boaz Harroshf842b642007-10-02 23:16:01 +02004026 cmndinfo->wait_for_completion = 1;
4027 cmndinfo->phase = -1;
4028 cmndinfo->OpCode = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030#ifdef GDTH_STATISTICS
4031 ++act_ios;
4032#endif
4033
Boaz Harroshf842b642007-10-02 23:16:01 +02004034 gdth_putq(ha, scp, cmndinfo->priority);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004035 gdth_next(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 return 0;
4037}
4038
4039
4040static int gdth_open(struct inode *inode, struct file *filep)
4041{
4042 gdth_ha_str *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043
Jonathan Corbet46787b42008-05-15 16:09:48 -06004044 lock_kernel();
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004045 list_for_each_entry(ha, &gdth_instances, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 if (!ha->sdev)
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004047 ha->sdev = scsi_get_host_dev(ha->shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 }
Jonathan Corbet46787b42008-05-15 16:09:48 -06004049 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050
4051 TRACE(("gdth_open()\n"));
4052 return 0;
4053}
4054
4055static int gdth_close(struct inode *inode, struct file *filep)
4056{
4057 TRACE(("gdth_close()\n"));
4058 return 0;
4059}
4060
4061static int ioc_event(void __user *arg)
4062{
4063 gdth_ioctl_event evt;
4064 gdth_ha_str *ha;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004065 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004067 if (copy_from_user(&evt, arg, sizeof(gdth_ioctl_event)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004069 ha = gdth_find_ha(evt.ionode);
4070 if (!ha)
4071 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072
4073 if (evt.erase == 0xff) {
4074 if (evt.event.event_source == ES_TEST)
4075 evt.event.event_data.size=sizeof(evt.event.event_data.eu.test);
4076 else if (evt.event.event_source == ES_DRIVER)
4077 evt.event.event_data.size=sizeof(evt.event.event_data.eu.driver);
4078 else if (evt.event.event_source == ES_SYNC)
4079 evt.event.event_data.size=sizeof(evt.event.event_data.eu.sync);
4080 else
4081 evt.event.event_data.size=sizeof(evt.event.event_data.eu.async);
4082 spin_lock_irqsave(&ha->smp_lock, flags);
4083 gdth_store_event(ha, evt.event.event_source, evt.event.event_idx,
4084 &evt.event.event_data);
4085 spin_unlock_irqrestore(&ha->smp_lock, flags);
4086 } else if (evt.erase == 0xfe) {
4087 gdth_clear_events();
4088 } else if (evt.erase == 0) {
4089 evt.handle = gdth_read_event(ha, evt.handle, &evt.event);
4090 } else {
4091 gdth_readapp_event(ha, evt.erase, &evt.event);
4092 }
4093 if (copy_to_user(arg, &evt, sizeof(gdth_ioctl_event)))
4094 return -EFAULT;
4095 return 0;
4096}
4097
4098static int ioc_lockdrv(void __user *arg)
4099{
4100 gdth_ioctl_lockdrv ldrv;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004101 u8 i, j;
4102 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 gdth_ha_str *ha;
4104
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004105 if (copy_from_user(&ldrv, arg, sizeof(gdth_ioctl_lockdrv)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004107 ha = gdth_find_ha(ldrv.ionode);
4108 if (!ha)
4109 return -EFAULT;
4110
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 for (i = 0; i < ldrv.drive_cnt && i < MAX_HDRIVES; ++i) {
4112 j = ldrv.drives[i];
4113 if (j >= MAX_HDRIVES || !ha->hdr[j].present)
4114 continue;
4115 if (ldrv.lock) {
4116 spin_lock_irqsave(&ha->smp_lock, flags);
4117 ha->hdr[j].lock = 1;
4118 spin_unlock_irqrestore(&ha->smp_lock, flags);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004119 gdth_wait_completion(ha, ha->bus_cnt, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 } else {
4121 spin_lock_irqsave(&ha->smp_lock, flags);
4122 ha->hdr[j].lock = 0;
4123 spin_unlock_irqrestore(&ha->smp_lock, flags);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004124 gdth_next(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 }
4126 }
4127 return 0;
4128}
4129
4130static int ioc_resetdrv(void __user *arg, char *cmnd)
4131{
4132 gdth_ioctl_reset res;
4133 gdth_cmd_str cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 gdth_ha_str *ha;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004135 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136
4137 if (copy_from_user(&res, arg, sizeof(gdth_ioctl_reset)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004138 res.number >= MAX_HDRIVES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004140 ha = gdth_find_ha(res.ionode);
4141 if (!ha)
4142 return -EFAULT;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004143
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 if (!ha->hdr[res.number].present)
4145 return 0;
4146 memset(&cmd, 0, sizeof(gdth_cmd_str));
4147 cmd.Service = CACHESERVICE;
4148 cmd.OpCode = GDT_CLUST_RESET;
4149 if (ha->cache_feat & GDT_64BIT)
4150 cmd.u.cache64.DeviceNo = res.number;
4151 else
4152 cmd.u.cache.DeviceNo = res.number;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004153
4154 rval = __gdth_execute(ha->sdev, &cmd, cmnd, 30, NULL);
4155 if (rval < 0)
4156 return rval;
4157 res.status = rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158
4159 if (copy_to_user(arg, &res, sizeof(gdth_ioctl_reset)))
4160 return -EFAULT;
4161 return 0;
4162}
4163
4164static int ioc_general(void __user *arg, char *cmnd)
4165{
4166 gdth_ioctl_general gen;
4167 char *buf = NULL;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004168 u64 paddr;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004169 gdth_ha_str *ha;
4170 int rval;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004171
4172 if (copy_from_user(&gen, arg, sizeof(gdth_ioctl_general)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004174 ha = gdth_find_ha(gen.ionode);
4175 if (!ha)
4176 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 if (gen.data_len + gen.sense_len != 0) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004178 if (!(buf = gdth_ioctl_alloc(ha, gen.data_len + gen.sense_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 FALSE, &paddr)))
4180 return -EFAULT;
4181 if (copy_from_user(buf, arg + sizeof(gdth_ioctl_general),
4182 gen.data_len + gen.sense_len)) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004183 gdth_ioctl_free(ha, gen.data_len+gen.sense_len, buf, paddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 return -EFAULT;
4185 }
4186
4187 if (gen.command.OpCode == GDT_IOCTL) {
4188 gen.command.u.ioctl.p_param = paddr;
4189 } else if (gen.command.Service == CACHESERVICE) {
4190 if (ha->cache_feat & GDT_64BIT) {
4191 /* copy elements from 32-bit IOCTL structure */
4192 gen.command.u.cache64.BlockCnt = gen.command.u.cache.BlockCnt;
4193 gen.command.u.cache64.BlockNo = gen.command.u.cache.BlockNo;
4194 gen.command.u.cache64.DeviceNo = gen.command.u.cache.DeviceNo;
4195 /* addresses */
4196 if (ha->cache_feat & SCATTER_GATHER) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004197 gen.command.u.cache64.DestAddr = (u64)-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 gen.command.u.cache64.sg_canz = 1;
4199 gen.command.u.cache64.sg_lst[0].sg_ptr = paddr;
4200 gen.command.u.cache64.sg_lst[0].sg_len = gen.data_len;
4201 gen.command.u.cache64.sg_lst[1].sg_len = 0;
4202 } else {
4203 gen.command.u.cache64.DestAddr = paddr;
4204 gen.command.u.cache64.sg_canz = 0;
4205 }
4206 } else {
4207 if (ha->cache_feat & SCATTER_GATHER) {
4208 gen.command.u.cache.DestAddr = 0xffffffff;
4209 gen.command.u.cache.sg_canz = 1;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004210 gen.command.u.cache.sg_lst[0].sg_ptr = (u32)paddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 gen.command.u.cache.sg_lst[0].sg_len = gen.data_len;
4212 gen.command.u.cache.sg_lst[1].sg_len = 0;
4213 } else {
4214 gen.command.u.cache.DestAddr = paddr;
4215 gen.command.u.cache.sg_canz = 0;
4216 }
4217 }
4218 } else if (gen.command.Service == SCSIRAWSERVICE) {
4219 if (ha->raw_feat & GDT_64BIT) {
4220 /* copy elements from 32-bit IOCTL structure */
4221 char cmd[16];
4222 gen.command.u.raw64.sense_len = gen.command.u.raw.sense_len;
4223 gen.command.u.raw64.bus = gen.command.u.raw.bus;
4224 gen.command.u.raw64.lun = gen.command.u.raw.lun;
4225 gen.command.u.raw64.target = gen.command.u.raw.target;
4226 memcpy(cmd, gen.command.u.raw.cmd, 16);
4227 memcpy(gen.command.u.raw64.cmd, cmd, 16);
4228 gen.command.u.raw64.clen = gen.command.u.raw.clen;
4229 gen.command.u.raw64.sdlen = gen.command.u.raw.sdlen;
4230 gen.command.u.raw64.direction = gen.command.u.raw.direction;
4231 /* addresses */
4232 if (ha->raw_feat & SCATTER_GATHER) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004233 gen.command.u.raw64.sdata = (u64)-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 gen.command.u.raw64.sg_ranz = 1;
4235 gen.command.u.raw64.sg_lst[0].sg_ptr = paddr;
4236 gen.command.u.raw64.sg_lst[0].sg_len = gen.data_len;
4237 gen.command.u.raw64.sg_lst[1].sg_len = 0;
4238 } else {
4239 gen.command.u.raw64.sdata = paddr;
4240 gen.command.u.raw64.sg_ranz = 0;
4241 }
4242 gen.command.u.raw64.sense_data = paddr + gen.data_len;
4243 } else {
4244 if (ha->raw_feat & SCATTER_GATHER) {
4245 gen.command.u.raw.sdata = 0xffffffff;
4246 gen.command.u.raw.sg_ranz = 1;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004247 gen.command.u.raw.sg_lst[0].sg_ptr = (u32)paddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 gen.command.u.raw.sg_lst[0].sg_len = gen.data_len;
4249 gen.command.u.raw.sg_lst[1].sg_len = 0;
4250 } else {
4251 gen.command.u.raw.sdata = paddr;
4252 gen.command.u.raw.sg_ranz = 0;
4253 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004254 gen.command.u.raw.sense_data = (u32)paddr + gen.data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 }
4256 } else {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004257 gdth_ioctl_free(ha, gen.data_len+gen.sense_len, buf, paddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 return -EFAULT;
4259 }
4260 }
4261
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004262 rval = __gdth_execute(ha->sdev, &gen.command, cmnd, gen.timeout, &gen.info);
4263 if (rval < 0)
4264 return rval;
4265 gen.status = rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
4267 if (copy_to_user(arg + sizeof(gdth_ioctl_general), buf,
4268 gen.data_len + gen.sense_len)) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004269 gdth_ioctl_free(ha, gen.data_len+gen.sense_len, buf, paddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 return -EFAULT;
4271 }
4272 if (copy_to_user(arg, &gen,
4273 sizeof(gdth_ioctl_general) - sizeof(gdth_cmd_str))) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004274 gdth_ioctl_free(ha, gen.data_len+gen.sense_len, buf, paddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 return -EFAULT;
4276 }
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004277 gdth_ioctl_free(ha, gen.data_len+gen.sense_len, buf, paddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 return 0;
4279}
4280
4281static int ioc_hdrlist(void __user *arg, char *cmnd)
4282{
4283 gdth_ioctl_rescan *rsc;
4284 gdth_cmd_str *cmd;
4285 gdth_ha_str *ha;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004286 u8 i;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004287 int rc = -ENOMEM;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004288 u32 cluster_type = 0;
4289
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 rsc = kmalloc(sizeof(*rsc), GFP_KERNEL);
4291 cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
4292 if (!rsc || !cmd)
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004293 goto free_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294
4295 if (copy_from_user(rsc, arg, sizeof(gdth_ioctl_rescan)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004296 (NULL == (ha = gdth_find_ha(rsc->ionode)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 rc = -EFAULT;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004298 goto free_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 memset(cmd, 0, sizeof(gdth_cmd_str));
4301
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 for (i = 0; i < MAX_HDRIVES; ++i) {
4303 if (!ha->hdr[i].present) {
4304 rsc->hdr_list[i].bus = 0xff;
4305 continue;
4306 }
4307 rsc->hdr_list[i].bus = ha->virt_bus;
4308 rsc->hdr_list[i].target = i;
4309 rsc->hdr_list[i].lun = 0;
4310 rsc->hdr_list[i].cluster_type = ha->hdr[i].cluster_type;
4311 if (ha->hdr[i].cluster_type & CLUSTER_DRIVE) {
4312 cmd->Service = CACHESERVICE;
4313 cmd->OpCode = GDT_CLUST_INFO;
4314 if (ha->cache_feat & GDT_64BIT)
4315 cmd->u.cache64.DeviceNo = i;
4316 else
4317 cmd->u.cache.DeviceNo = i;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004318 if (__gdth_execute(ha->sdev, cmd, cmnd, 30, &cluster_type) == S_OK)
4319 rsc->hdr_list[i].cluster_type = cluster_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 }
4321 }
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004322
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 if (copy_to_user(arg, rsc, sizeof(gdth_ioctl_rescan)))
4324 rc = -EFAULT;
4325 else
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004326 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
4328free_fail:
4329 kfree(rsc);
4330 kfree(cmd);
4331 return rc;
4332}
4333
4334static int ioc_rescan(void __user *arg, char *cmnd)
4335{
4336 gdth_ioctl_rescan *rsc;
4337 gdth_cmd_str *cmd;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004338 u16 i, status, hdr_cnt;
4339 u32 info;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004340 int cyls, hds, secs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 int rc = -ENOMEM;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004342 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 gdth_ha_str *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344
4345 rsc = kmalloc(sizeof(*rsc), GFP_KERNEL);
4346 cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
4347 if (!cmd || !rsc)
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004348 goto free_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349
4350 if (copy_from_user(rsc, arg, sizeof(gdth_ioctl_rescan)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004351 (NULL == (ha = gdth_find_ha(rsc->ionode)))) {
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004352 rc = -EFAULT;
4353 goto free_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 memset(cmd, 0, sizeof(gdth_cmd_str));
4356
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 if (rsc->flag == 0) {
4358 /* old method: re-init. cache service */
4359 cmd->Service = CACHESERVICE;
4360 if (ha->cache_feat & GDT_64BIT) {
4361 cmd->OpCode = GDT_X_INIT_HOST;
4362 cmd->u.cache64.DeviceNo = LINUX_OS;
4363 } else {
4364 cmd->OpCode = GDT_INIT;
4365 cmd->u.cache.DeviceNo = LINUX_OS;
4366 }
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004367
4368 status = __gdth_execute(ha->sdev, cmd, cmnd, 30, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 i = 0;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004370 hdr_cnt = (status == S_OK ? (u16)info : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 } else {
4372 i = rsc->hdr_no;
4373 hdr_cnt = i + 1;
4374 }
4375
4376 for (; i < hdr_cnt && i < MAX_HDRIVES; ++i) {
4377 cmd->Service = CACHESERVICE;
4378 cmd->OpCode = GDT_INFO;
4379 if (ha->cache_feat & GDT_64BIT)
4380 cmd->u.cache64.DeviceNo = i;
4381 else
4382 cmd->u.cache.DeviceNo = i;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004383
4384 status = __gdth_execute(ha->sdev, cmd, cmnd, 30, &info);
4385
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 spin_lock_irqsave(&ha->smp_lock, flags);
4387 rsc->hdr_list[i].bus = ha->virt_bus;
4388 rsc->hdr_list[i].target = i;
4389 rsc->hdr_list[i].lun = 0;
4390 if (status != S_OK) {
4391 ha->hdr[i].present = FALSE;
4392 } else {
4393 ha->hdr[i].present = TRUE;
4394 ha->hdr[i].size = info;
4395 /* evaluate mapping */
4396 ha->hdr[i].size &= ~SECS32;
4397 gdth_eval_mapping(ha->hdr[i].size,&cyls,&hds,&secs);
4398 ha->hdr[i].heads = hds;
4399 ha->hdr[i].secs = secs;
4400 /* round size */
4401 ha->hdr[i].size = cyls * hds * secs;
4402 }
4403 spin_unlock_irqrestore(&ha->smp_lock, flags);
4404 if (status != S_OK)
4405 continue;
4406
4407 /* extended info, if GDT_64BIT, for drives > 2 TB */
4408 /* but we need ha->info2, not yet stored in scp->SCp */
4409
4410 /* devtype, cluster info, R/W attribs */
4411 cmd->Service = CACHESERVICE;
4412 cmd->OpCode = GDT_DEVTYPE;
4413 if (ha->cache_feat & GDT_64BIT)
4414 cmd->u.cache64.DeviceNo = i;
4415 else
4416 cmd->u.cache.DeviceNo = i;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004417
4418 status = __gdth_execute(ha->sdev, cmd, cmnd, 30, &info);
4419
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 spin_lock_irqsave(&ha->smp_lock, flags);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004421 ha->hdr[i].devtype = (status == S_OK ? (u16)info : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 spin_unlock_irqrestore(&ha->smp_lock, flags);
4423
4424 cmd->Service = CACHESERVICE;
4425 cmd->OpCode = GDT_CLUST_INFO;
4426 if (ha->cache_feat & GDT_64BIT)
4427 cmd->u.cache64.DeviceNo = i;
4428 else
4429 cmd->u.cache.DeviceNo = i;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004430
4431 status = __gdth_execute(ha->sdev, cmd, cmnd, 30, &info);
4432
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 spin_lock_irqsave(&ha->smp_lock, flags);
4434 ha->hdr[i].cluster_type =
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004435 ((status == S_OK && !shared_access) ? (u16)info : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 spin_unlock_irqrestore(&ha->smp_lock, flags);
4437 rsc->hdr_list[i].cluster_type = ha->hdr[i].cluster_type;
4438
4439 cmd->Service = CACHESERVICE;
4440 cmd->OpCode = GDT_RW_ATTRIBS;
4441 if (ha->cache_feat & GDT_64BIT)
4442 cmd->u.cache64.DeviceNo = i;
4443 else
4444 cmd->u.cache.DeviceNo = i;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004445
4446 status = __gdth_execute(ha->sdev, cmd, cmnd, 30, &info);
4447
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 spin_lock_irqsave(&ha->smp_lock, flags);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004449 ha->hdr[i].rw_attribs = (status == S_OK ? (u16)info : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 spin_unlock_irqrestore(&ha->smp_lock, flags);
4451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452
4453 if (copy_to_user(arg, rsc, sizeof(gdth_ioctl_rescan)))
4454 rc = -EFAULT;
4455 else
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004456 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
4458free_fail:
4459 kfree(rsc);
4460 kfree(cmd);
4461 return rc;
4462}
4463
4464static int gdth_ioctl(struct inode *inode, struct file *filep,
4465 unsigned int cmd, unsigned long arg)
4466{
4467 gdth_ha_str *ha;
4468 Scsi_Cmnd *scp;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004469 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 char cmnd[MAX_COMMAND_SIZE];
4471 void __user *argp = (void __user *)arg;
4472
4473 memset(cmnd, 0xff, 12);
4474
4475 TRACE(("gdth_ioctl() cmd 0x%x\n", cmd));
4476
4477 switch (cmd) {
4478 case GDTIOCTL_CTRCNT:
4479 {
4480 int cnt = gdth_ctr_count;
4481 if (put_user(cnt, (int __user *)argp))
4482 return -EFAULT;
4483 break;
4484 }
4485
4486 case GDTIOCTL_DRVERS:
4487 {
4488 int ver = (GDTH_VERSION<<8) | GDTH_SUBVERSION;
4489 if (put_user(ver, (int __user *)argp))
4490 return -EFAULT;
4491 break;
4492 }
4493
4494 case GDTIOCTL_OSVERS:
4495 {
4496 gdth_ioctl_osvers osv;
4497
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004498 osv.version = (u8)(LINUX_VERSION_CODE >> 16);
4499 osv.subversion = (u8)(LINUX_VERSION_CODE >> 8);
4500 osv.revision = (u16)(LINUX_VERSION_CODE & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 if (copy_to_user(argp, &osv, sizeof(gdth_ioctl_osvers)))
4502 return -EFAULT;
4503 break;
4504 }
4505
4506 case GDTIOCTL_CTRTYPE:
4507 {
4508 gdth_ioctl_ctrtype ctrt;
4509
4510 if (copy_from_user(&ctrt, argp, sizeof(gdth_ioctl_ctrtype)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004511 (NULL == (ha = gdth_find_ha(ctrt.ionode))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004513
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 if (ha->type == GDT_ISA || ha->type == GDT_EISA) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004515 ctrt.type = (u8)((ha->stype>>20) - 0x10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 } else {
4517 if (ha->type != GDT_PCIMPR) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004518 ctrt.type = (u8)((ha->stype<<4) + 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 } else {
4520 ctrt.type =
4521 (ha->oem_id == OEM_ID_INTEL ? 0xfd : 0xfe);
4522 if (ha->stype >= 0x300)
Jeff Garzik8e9a8a02007-07-17 05:25:17 -04004523 ctrt.ext_type = 0x6000 | ha->pdev->subsystem_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 else
4525 ctrt.ext_type = 0x6000 | ha->stype;
4526 }
Jeff Garzik8e9a8a02007-07-17 05:25:17 -04004527 ctrt.device_id = ha->pdev->device;
4528 ctrt.sub_device_id = ha->pdev->subsystem_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 }
4530 ctrt.info = ha->brd_phys;
4531 ctrt.oem_id = ha->oem_id;
4532 if (copy_to_user(argp, &ctrt, sizeof(gdth_ioctl_ctrtype)))
4533 return -EFAULT;
4534 break;
4535 }
4536
4537 case GDTIOCTL_GENERAL:
4538 return ioc_general(argp, cmnd);
4539
4540 case GDTIOCTL_EVENT:
4541 return ioc_event(argp);
4542
4543 case GDTIOCTL_LOCKDRV:
4544 return ioc_lockdrv(argp);
4545
4546 case GDTIOCTL_LOCKCHN:
4547 {
4548 gdth_ioctl_lockchn lchn;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004549 u8 i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550
4551 if (copy_from_user(&lchn, argp, sizeof(gdth_ioctl_lockchn)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004552 (NULL == (ha = gdth_find_ha(lchn.ionode))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004554
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 i = lchn.channel;
4556 if (i < ha->bus_cnt) {
4557 if (lchn.lock) {
4558 spin_lock_irqsave(&ha->smp_lock, flags);
4559 ha->raw[i].lock = 1;
4560 spin_unlock_irqrestore(&ha->smp_lock, flags);
Jens Axboe242f9dc2008-09-14 05:55:09 -07004561 for (j = 0; j < ha->tid_cnt; ++j)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004562 gdth_wait_completion(ha, i, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 } else {
4564 spin_lock_irqsave(&ha->smp_lock, flags);
4565 ha->raw[i].lock = 0;
4566 spin_unlock_irqrestore(&ha->smp_lock, flags);
Jens Axboe242f9dc2008-09-14 05:55:09 -07004567 for (j = 0; j < ha->tid_cnt; ++j)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004568 gdth_next(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 }
4570 }
4571 break;
4572 }
4573
4574 case GDTIOCTL_RESCAN:
4575 return ioc_rescan(argp, cmnd);
4576
4577 case GDTIOCTL_HDRLIST:
4578 return ioc_hdrlist(argp, cmnd);
4579
4580 case GDTIOCTL_RESET_BUS:
4581 {
4582 gdth_ioctl_reset res;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004583 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584
4585 if (copy_from_user(&res, argp, sizeof(gdth_ioctl_reset)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004586 (NULL == (ha = gdth_find_ha(res.ionode))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02004589 scp = kzalloc(sizeof(*scp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 if (!scp)
4591 return -ENOMEM;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004592 scp->device = ha->sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 scp->cmd_len = 12;
Christoph Hellwig52759e62007-10-02 22:59:53 +02004594 scp->device->channel = res.number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 rval = gdth_eh_bus_reset(scp);
4596 res.status = (rval == SUCCESS ? S_OK : S_GENERR);
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004597 kfree(scp);
Jeff Garzik8d7a5da2007-10-02 22:54:28 +02004598
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 if (copy_to_user(argp, &res, sizeof(gdth_ioctl_reset)))
4600 return -EFAULT;
4601 break;
4602 }
4603
4604 case GDTIOCTL_RESET_DRV:
4605 return ioc_resetdrv(argp, cmnd);
4606
4607 default:
4608 break;
4609 }
4610 return 0;
4611}
4612
4613
4614/* flush routine */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004615static void gdth_flush(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616{
4617 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 gdth_cmd_str gdtcmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 char cmnd[MAX_COMMAND_SIZE];
4620 memset(cmnd, 0xff, MAX_COMMAND_SIZE);
4621
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004622 TRACE2(("gdth_flush() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 for (i = 0; i < MAX_HDRIVES; ++i) {
4625 if (ha->hdr[i].present) {
4626 gdtcmd.BoardNode = LOCALBOARD;
4627 gdtcmd.Service = CACHESERVICE;
4628 gdtcmd.OpCode = GDT_FLUSH;
4629 if (ha->cache_feat & GDT_64BIT) {
4630 gdtcmd.u.cache64.DeviceNo = i;
4631 gdtcmd.u.cache64.BlockNo = 1;
4632 gdtcmd.u.cache64.sg_canz = 0;
4633 } else {
4634 gdtcmd.u.cache.DeviceNo = i;
4635 gdtcmd.u.cache.BlockNo = 1;
4636 gdtcmd.u.cache.sg_canz = 0;
4637 }
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004638 TRACE2(("gdth_flush(): flush ha %d drive %d\n", ha->hanum, i));
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004639
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004640 gdth_execute(ha->shost, &gdtcmd, cmnd, 30, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 }
4642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643}
4644
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004645/* configure lun */
4646static int gdth_slave_configure(struct scsi_device *sdev)
4647{
4648 scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
4649 sdev->skip_ms_page_3f = 1;
4650 sdev->skip_ms_page_8 = 1;
4651 return 0;
4652}
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004653
Christoph Hellwig835cc242007-10-02 23:09:56 +02004654static struct scsi_host_template gdth_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 .name = "GDT SCSI Disk Array Controller",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 .info = gdth_info,
4657 .queuecommand = gdth_queuecommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 .eh_bus_reset_handler = gdth_eh_bus_reset,
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004659 .slave_configure = gdth_slave_configure,
Jeff Garzik69916b72007-10-02 22:57:58 +02004660 .bios_param = gdth_bios_param,
4661 .proc_info = gdth_proc_info,
Jens Axboe242f9dc2008-09-14 05:55:09 -07004662 .eh_timed_out = gdth_timed_out,
Jeff Garzik69916b72007-10-02 22:57:58 +02004663 .proc_name = "gdth",
4664 .can_queue = GDTH_MAXCMDS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 .this_id = -1,
4666 .sg_tablesize = GDTH_MAXSG,
4667 .cmd_per_lun = GDTH_MAXC_P_L,
4668 .unchecked_isa_dma = 1,
4669 .use_clustering = ENABLE_CLUSTERING,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670};
4671
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004672#ifdef CONFIG_ISA
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004673static int __init gdth_isa_probe_one(u32 isa_bios)
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004674{
4675 struct Scsi_Host *shp;
4676 gdth_ha_str *ha;
4677 dma_addr_t scratch_dma_handle = 0;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004678 int error, i;
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004679
4680 if (!gdth_search_isa(isa_bios))
4681 return -ENXIO;
4682
Christoph Hellwig835cc242007-10-02 23:09:56 +02004683 shp = scsi_host_alloc(&gdth_template, sizeof(gdth_ha_str));
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004684 if (!shp)
4685 return -ENOMEM;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004686 ha = shost_priv(shp);
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004687
4688 error = -ENODEV;
4689 if (!gdth_init_isa(isa_bios,ha))
4690 goto out_host_put;
4691
4692 /* controller found and initialized */
4693 printk("Configuring GDT-ISA HA at BIOS 0x%05X IRQ %u DRQ %u\n",
4694 isa_bios, ha->irq, ha->drq);
4695
4696 error = request_irq(ha->irq, gdth_interrupt, IRQF_DISABLED, "gdth", ha);
4697 if (error) {
4698 printk("GDT-ISA: Unable to allocate IRQ\n");
4699 goto out_host_put;
4700 }
4701
4702 error = request_dma(ha->drq, "gdth");
4703 if (error) {
4704 printk("GDT-ISA: Unable to allocate DMA channel\n");
4705 goto out_free_irq;
4706 }
4707
4708 set_dma_mode(ha->drq,DMA_MODE_CASCADE);
4709 enable_dma(ha->drq);
4710 shp->unchecked_isa_dma = 1;
4711 shp->irq = ha->irq;
4712 shp->dma_channel = ha->drq;
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004713
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004714 ha->hanum = gdth_ctr_count++;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004715 ha->shost = shp;
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004716
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004717 ha->pccb = &ha->cmdext;
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004718 ha->ccb_phys = 0L;
4719 ha->pdev = NULL;
4720
4721 error = -ENOMEM;
4722
4723 ha->pscratch = pci_alloc_consistent(ha->pdev, GDTH_SCRATCH,
4724 &scratch_dma_handle);
4725 if (!ha->pscratch)
4726 goto out_dec_counters;
4727 ha->scratch_phys = scratch_dma_handle;
4728
4729 ha->pmsg = pci_alloc_consistent(ha->pdev, sizeof(gdth_msg_str),
4730 &scratch_dma_handle);
4731 if (!ha->pmsg)
4732 goto out_free_pscratch;
4733 ha->msg_phys = scratch_dma_handle;
4734
4735#ifdef INT_COAL
4736 ha->coal_stat = pci_alloc_consistent(ha->pdev,
4737 sizeof(gdth_coal_status) * MAXOFFSETS,
4738 &scratch_dma_handle);
4739 if (!ha->coal_stat)
4740 goto out_free_pmsg;
4741 ha->coal_stat_phys = scratch_dma_handle;
4742#endif
4743
4744 ha->scratch_busy = FALSE;
4745 ha->req_first = NULL;
4746 ha->tid_cnt = MAX_HDRIVES;
4747 if (max_ids > 0 && max_ids < ha->tid_cnt)
4748 ha->tid_cnt = max_ids;
4749 for (i = 0; i < GDTH_MAXCMDS; ++i)
4750 ha->cmd_tab[i].cmnd = UNUSED_CMND;
4751 ha->scan_mode = rescan ? 0x10 : 0;
4752
4753 error = -ENODEV;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004754 if (!gdth_search_drives(ha)) {
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004755 printk("GDT-ISA: Error during device scan\n");
4756 goto out_free_coal_stat;
4757 }
4758
4759 if (hdr_channel < 0 || hdr_channel > ha->bus_cnt)
4760 hdr_channel = ha->bus_cnt;
4761 ha->virt_bus = hdr_channel;
4762
4763 if (ha->cache_feat & ha->raw_feat & ha->screen_feat & GDT_64BIT)
4764 shp->max_cmd_len = 16;
4765
4766 shp->max_id = ha->tid_cnt;
4767 shp->max_lun = MAXLUN;
Christoph Hellwig52759e62007-10-02 22:59:53 +02004768 shp->max_channel = ha->bus_cnt;
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004769
4770 spin_lock_init(&ha->smp_lock);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004771 gdth_enable_int(ha);
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004772
Christoph Hellwig835cc242007-10-02 23:09:56 +02004773 error = scsi_add_host(shp, NULL);
4774 if (error)
4775 goto out_free_coal_stat;
4776 list_add_tail(&ha->list, &gdth_instances);
James Bottomley2d6f0d02008-05-04 22:34:49 -05004777 gdth_timer_init();
Boaz Harrosh61c92812008-02-12 19:35:22 +02004778
4779 scsi_scan_host(shp);
4780
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004781 return 0;
4782
4783 out_free_coal_stat:
4784#ifdef INT_COAL
4785 pci_free_consistent(ha->pdev, sizeof(gdth_coal_status) * MAXOFFSETS,
4786 ha->coal_stat, ha->coal_stat_phys);
4787 out_free_pmsg:
4788#endif
4789 pci_free_consistent(ha->pdev, sizeof(gdth_msg_str),
4790 ha->pmsg, ha->msg_phys);
4791 out_free_pscratch:
4792 pci_free_consistent(ha->pdev, GDTH_SCRATCH,
4793 ha->pscratch, ha->scratch_phys);
4794 out_dec_counters:
4795 gdth_ctr_count--;
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004796 out_free_irq:
4797 free_irq(ha->irq, ha);
4798 out_host_put:
Christoph Hellwig835cc242007-10-02 23:09:56 +02004799 scsi_host_put(shp);
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004800 return error;
4801}
4802#endif /* CONFIG_ISA */
4803
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004804#ifdef CONFIG_EISA
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004805static int __init gdth_eisa_probe_one(u16 eisa_slot)
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004806{
4807 struct Scsi_Host *shp;
4808 gdth_ha_str *ha;
4809 dma_addr_t scratch_dma_handle = 0;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004810 int error, i;
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004811
4812 if (!gdth_search_eisa(eisa_slot))
4813 return -ENXIO;
4814
Christoph Hellwig835cc242007-10-02 23:09:56 +02004815 shp = scsi_host_alloc(&gdth_template, sizeof(gdth_ha_str));
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004816 if (!shp)
4817 return -ENOMEM;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004818 ha = shost_priv(shp);
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004819
4820 error = -ENODEV;
4821 if (!gdth_init_eisa(eisa_slot,ha))
4822 goto out_host_put;
4823
4824 /* controller found and initialized */
4825 printk("Configuring GDT-EISA HA at Slot %d IRQ %u\n",
4826 eisa_slot >> 12, ha->irq);
4827
4828 error = request_irq(ha->irq, gdth_interrupt, IRQF_DISABLED, "gdth", ha);
4829 if (error) {
4830 printk("GDT-EISA: Unable to allocate IRQ\n");
4831 goto out_host_put;
4832 }
4833
4834 shp->unchecked_isa_dma = 0;
4835 shp->irq = ha->irq;
4836 shp->dma_channel = 0xff;
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004837
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004838 ha->hanum = gdth_ctr_count++;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004839 ha->shost = shp;
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004840
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004841 TRACE2(("EISA detect Bus 0: hanum %d\n", ha->hanum));
4842
4843 ha->pccb = &ha->cmdext;
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004844 ha->ccb_phys = 0L;
4845
4846 error = -ENOMEM;
4847
4848 ha->pdev = NULL;
4849 ha->pscratch = pci_alloc_consistent(ha->pdev, GDTH_SCRATCH,
4850 &scratch_dma_handle);
4851 if (!ha->pscratch)
4852 goto out_free_irq;
4853 ha->scratch_phys = scratch_dma_handle;
4854
4855 ha->pmsg = pci_alloc_consistent(ha->pdev, sizeof(gdth_msg_str),
4856 &scratch_dma_handle);
4857 if (!ha->pmsg)
4858 goto out_free_pscratch;
4859 ha->msg_phys = scratch_dma_handle;
4860
4861#ifdef INT_COAL
4862 ha->coal_stat = pci_alloc_consistent(ha->pdev,
4863 sizeof(gdth_coal_status) * MAXOFFSETS,
4864 &scratch_dma_handle);
4865 if (!ha->coal_stat)
4866 goto out_free_pmsg;
4867 ha->coal_stat_phys = scratch_dma_handle;
4868#endif
4869
4870 ha->ccb_phys = pci_map_single(ha->pdev,ha->pccb,
4871 sizeof(gdth_cmd_str), PCI_DMA_BIDIRECTIONAL);
4872 if (!ha->ccb_phys)
4873 goto out_free_coal_stat;
4874
4875 ha->scratch_busy = FALSE;
4876 ha->req_first = NULL;
4877 ha->tid_cnt = MAX_HDRIVES;
4878 if (max_ids > 0 && max_ids < ha->tid_cnt)
4879 ha->tid_cnt = max_ids;
4880 for (i = 0; i < GDTH_MAXCMDS; ++i)
4881 ha->cmd_tab[i].cmnd = UNUSED_CMND;
4882 ha->scan_mode = rescan ? 0x10 : 0;
4883
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004884 if (!gdth_search_drives(ha)) {
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004885 printk("GDT-EISA: Error during device scan\n");
4886 error = -ENODEV;
4887 goto out_free_ccb_phys;
4888 }
4889
4890 if (hdr_channel < 0 || hdr_channel > ha->bus_cnt)
4891 hdr_channel = ha->bus_cnt;
4892 ha->virt_bus = hdr_channel;
4893
4894 if (ha->cache_feat & ha->raw_feat & ha->screen_feat & GDT_64BIT)
4895 shp->max_cmd_len = 16;
4896
4897 shp->max_id = ha->tid_cnt;
4898 shp->max_lun = MAXLUN;
Christoph Hellwig52759e62007-10-02 22:59:53 +02004899 shp->max_channel = ha->bus_cnt;
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004900
4901 spin_lock_init(&ha->smp_lock);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004902 gdth_enable_int(ha);
Christoph Hellwig835cc242007-10-02 23:09:56 +02004903
4904 error = scsi_add_host(shp, NULL);
4905 if (error)
4906 goto out_free_coal_stat;
4907 list_add_tail(&ha->list, &gdth_instances);
James Bottomley2d6f0d02008-05-04 22:34:49 -05004908 gdth_timer_init();
Boaz Harrosh61c92812008-02-12 19:35:22 +02004909
4910 scsi_scan_host(shp);
4911
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004912 return 0;
4913
4914 out_free_ccb_phys:
4915 pci_unmap_single(ha->pdev,ha->ccb_phys, sizeof(gdth_cmd_str),
4916 PCI_DMA_BIDIRECTIONAL);
4917 out_free_coal_stat:
4918#ifdef INT_COAL
4919 pci_free_consistent(ha->pdev, sizeof(gdth_coal_status) * MAXOFFSETS,
4920 ha->coal_stat, ha->coal_stat_phys);
4921 out_free_pmsg:
4922#endif
4923 pci_free_consistent(ha->pdev, sizeof(gdth_msg_str),
4924 ha->pmsg, ha->msg_phys);
4925 out_free_pscratch:
4926 pci_free_consistent(ha->pdev, GDTH_SCRATCH,
4927 ha->pscratch, ha->scratch_phys);
4928 out_free_irq:
4929 free_irq(ha->irq, ha);
4930 gdth_ctr_count--;
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004931 out_host_put:
Christoph Hellwig835cc242007-10-02 23:09:56 +02004932 scsi_host_put(shp);
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004933 return error;
4934}
4935#endif /* CONFIG_EISA */
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004936
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004937#ifdef CONFIG_PCI
Al Viroced71722008-11-22 17:35:14 +00004938static int __devinit gdth_pci_probe_one(gdth_pci_str *pcistr,
Jeff Garzikcff26802008-02-15 22:20:09 -05004939 gdth_ha_str **ha_out)
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004940{
4941 struct Scsi_Host *shp;
4942 gdth_ha_str *ha;
4943 dma_addr_t scratch_dma_handle = 0;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004944 int error, i;
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05004945 struct pci_dev *pdev = pcistr->pdev;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004946
Jeff Garzikcff26802008-02-15 22:20:09 -05004947 *ha_out = NULL;
4948
Christoph Hellwig835cc242007-10-02 23:09:56 +02004949 shp = scsi_host_alloc(&gdth_template, sizeof(gdth_ha_str));
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004950 if (!shp)
4951 return -ENOMEM;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004952 ha = shost_priv(shp);
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004953
4954 error = -ENODEV;
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05004955 if (!gdth_init_pci(pdev, pcistr, ha))
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004956 goto out_host_put;
4957
4958 /* controller found and initialized */
4959 printk("Configuring GDT-PCI HA at %d/%d IRQ %u\n",
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05004960 pdev->bus->number,
4961 PCI_SLOT(pdev->devfn),
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004962 ha->irq);
4963
4964 error = request_irq(ha->irq, gdth_interrupt,
4965 IRQF_DISABLED|IRQF_SHARED, "gdth", ha);
4966 if (error) {
4967 printk("GDT-PCI: Unable to allocate IRQ\n");
4968 goto out_host_put;
4969 }
4970
4971 shp->unchecked_isa_dma = 0;
4972 shp->irq = ha->irq;
4973 shp->dma_channel = 0xff;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004974
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004975 ha->hanum = gdth_ctr_count++;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004976 ha->shost = shp;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004977
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004978 ha->pccb = &ha->cmdext;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004979 ha->ccb_phys = 0L;
4980
4981 error = -ENOMEM;
4982
4983 ha->pscratch = pci_alloc_consistent(ha->pdev, GDTH_SCRATCH,
4984 &scratch_dma_handle);
4985 if (!ha->pscratch)
4986 goto out_free_irq;
4987 ha->scratch_phys = scratch_dma_handle;
4988
4989 ha->pmsg = pci_alloc_consistent(ha->pdev, sizeof(gdth_msg_str),
4990 &scratch_dma_handle);
4991 if (!ha->pmsg)
4992 goto out_free_pscratch;
4993 ha->msg_phys = scratch_dma_handle;
4994
4995#ifdef INT_COAL
4996 ha->coal_stat = pci_alloc_consistent(ha->pdev,
4997 sizeof(gdth_coal_status) * MAXOFFSETS,
4998 &scratch_dma_handle);
4999 if (!ha->coal_stat)
5000 goto out_free_pmsg;
5001 ha->coal_stat_phys = scratch_dma_handle;
5002#endif
5003
5004 ha->scratch_busy = FALSE;
5005 ha->req_first = NULL;
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05005006 ha->tid_cnt = pdev->device >= 0x200 ? MAXID : MAX_HDRIVES;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005007 if (max_ids > 0 && max_ids < ha->tid_cnt)
5008 ha->tid_cnt = max_ids;
5009 for (i = 0; i < GDTH_MAXCMDS; ++i)
5010 ha->cmd_tab[i].cmnd = UNUSED_CMND;
5011 ha->scan_mode = rescan ? 0x10 : 0;
5012
5013 error = -ENODEV;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02005014 if (!gdth_search_drives(ha)) {
5015 printk("GDT-PCI %d: Error during device scan\n", ha->hanum);
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005016 goto out_free_coal_stat;
5017 }
5018
5019 if (hdr_channel < 0 || hdr_channel > ha->bus_cnt)
5020 hdr_channel = ha->bus_cnt;
5021 ha->virt_bus = hdr_channel;
5022
5023 /* 64-bit DMA only supported from FW >= x.43 */
5024 if (!(ha->cache_feat & ha->raw_feat & ha->screen_feat & GDT_64BIT) ||
5025 !ha->dma64_support) {
Yang Hongyang284901a2009-04-06 19:01:15 -07005026 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005027 printk(KERN_WARNING "GDT-PCI %d: "
Boaz Harrosh45f1a412007-10-02 23:05:53 +02005028 "Unable to set 32-bit DMA\n", ha->hanum);
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005029 goto out_free_coal_stat;
5030 }
5031 } else {
5032 shp->max_cmd_len = 16;
Yang Hongyang6a355282009-04-06 19:01:13 -07005033 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02005034 printk("GDT-PCI %d: 64-bit DMA enabled\n", ha->hanum);
Yang Hongyang284901a2009-04-06 19:01:15 -07005035 } else if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005036 printk(KERN_WARNING "GDT-PCI %d: "
Boaz Harrosh45f1a412007-10-02 23:05:53 +02005037 "Unable to set 64/32-bit DMA\n", ha->hanum);
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005038 goto out_free_coal_stat;
5039 }
5040 }
5041
5042 shp->max_id = ha->tid_cnt;
5043 shp->max_lun = MAXLUN;
Christoph Hellwig52759e62007-10-02 22:59:53 +02005044 shp->max_channel = ha->bus_cnt;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005045
5046 spin_lock_init(&ha->smp_lock);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02005047 gdth_enable_int(ha);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005048
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05005049 error = scsi_add_host(shp, &pdev->dev);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005050 if (error)
5051 goto out_free_coal_stat;
5052 list_add_tail(&ha->list, &gdth_instances);
Boaz Harrosh61c92812008-02-12 19:35:22 +02005053
Jeff Garzikcff26802008-02-15 22:20:09 -05005054 pci_set_drvdata(ha->pdev, ha);
James Bottomley2d6f0d02008-05-04 22:34:49 -05005055 gdth_timer_init();
Jeff Garzikcff26802008-02-15 22:20:09 -05005056
Boaz Harrosh61c92812008-02-12 19:35:22 +02005057 scsi_scan_host(shp);
5058
Jeff Garzikcff26802008-02-15 22:20:09 -05005059 *ha_out = ha;
5060
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005061 return 0;
5062
5063 out_free_coal_stat:
5064#ifdef INT_COAL
5065 pci_free_consistent(ha->pdev, sizeof(gdth_coal_status) * MAXOFFSETS,
5066 ha->coal_stat, ha->coal_stat_phys);
5067 out_free_pmsg:
5068#endif
5069 pci_free_consistent(ha->pdev, sizeof(gdth_msg_str),
5070 ha->pmsg, ha->msg_phys);
5071 out_free_pscratch:
5072 pci_free_consistent(ha->pdev, GDTH_SCRATCH,
5073 ha->pscratch, ha->scratch_phys);
5074 out_free_irq:
5075 free_irq(ha->irq, ha);
5076 gdth_ctr_count--;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005077 out_host_put:
Christoph Hellwig835cc242007-10-02 23:09:56 +02005078 scsi_host_put(shp);
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005079 return error;
5080}
5081#endif /* CONFIG_PCI */
5082
Christoph Hellwig835cc242007-10-02 23:09:56 +02005083static void gdth_remove_one(gdth_ha_str *ha)
5084{
5085 struct Scsi_Host *shp = ha->shost;
5086
5087 TRACE2(("gdth_remove_one()\n"));
5088
5089 scsi_remove_host(shp);
5090
Boaz Harroshb31ddd32008-02-27 15:27:16 -08005091 gdth_flush(ha);
5092
Christoph Hellwig835cc242007-10-02 23:09:56 +02005093 if (ha->sdev) {
5094 scsi_free_host_dev(ha->sdev);
5095 ha->sdev = NULL;
5096 }
5097
Christoph Hellwig835cc242007-10-02 23:09:56 +02005098 if (shp->irq)
5099 free_irq(shp->irq,ha);
5100
5101#ifdef CONFIG_ISA
5102 if (shp->dma_channel != 0xff)
5103 free_dma(shp->dma_channel);
5104#endif
5105#ifdef INT_COAL
5106 if (ha->coal_stat)
5107 pci_free_consistent(ha->pdev, sizeof(gdth_coal_status) *
5108 MAXOFFSETS, ha->coal_stat, ha->coal_stat_phys);
5109#endif
5110 if (ha->pscratch)
5111 pci_free_consistent(ha->pdev, GDTH_SCRATCH,
5112 ha->pscratch, ha->scratch_phys);
5113 if (ha->pmsg)
5114 pci_free_consistent(ha->pdev, sizeof(gdth_msg_str),
5115 ha->pmsg, ha->msg_phys);
5116 if (ha->ccb_phys)
5117 pci_unmap_single(ha->pdev,ha->ccb_phys,
5118 sizeof(gdth_cmd_str),PCI_DMA_BIDIRECTIONAL);
5119
5120 scsi_host_put(shp);
5121}
5122
Dave Jones1fe6dbf2010-01-04 10:19:34 -05005123static int gdth_halt(struct notifier_block *nb, unsigned long event, void *buf)
Boaz Harroshb31ddd32008-02-27 15:27:16 -08005124{
5125 gdth_ha_str *ha;
5126
5127 TRACE2(("gdth_halt() event %d\n", (int)event));
5128 if (event != SYS_RESTART && event != SYS_HALT && event != SYS_POWER_OFF)
5129 return NOTIFY_DONE;
5130
5131 list_for_each_entry(ha, &gdth_instances, list)
5132 gdth_flush(ha);
5133
5134 return NOTIFY_OK;
5135}
5136
5137static struct notifier_block gdth_notifier = {
5138 gdth_halt, NULL, 0
5139};
5140
Christoph Hellwig835cc242007-10-02 23:09:56 +02005141static int __init gdth_init(void)
5142{
5143 if (disable) {
5144 printk("GDT-HA: Controller driver disabled from"
5145 " command line !\n");
5146 return 0;
5147 }
5148
5149 printk("GDT-HA: Storage RAID Controller Driver. Version: %s\n",
5150 GDTH_VERSION_STR);
5151
5152 /* initializations */
5153 gdth_polling = TRUE;
5154 gdth_clear_events();
James Bottomley2d6f0d02008-05-04 22:34:49 -05005155 init_timer(&gdth_timer);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005156
5157 /* As default we do not probe for EISA or ISA controllers */
5158 if (probe_eisa_isa) {
5159 /* scanning for controllers, at first: ISA controller */
5160#ifdef CONFIG_ISA
Dave Jones1fe6dbf2010-01-04 10:19:34 -05005161 u32 isa_bios;
Christoph Hellwig835cc242007-10-02 23:09:56 +02005162 for (isa_bios = 0xc8000UL; isa_bios <= 0xd8000UL;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02005163 isa_bios += 0x8000UL)
Christoph Hellwig835cc242007-10-02 23:09:56 +02005164 gdth_isa_probe_one(isa_bios);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005165#endif
5166#ifdef CONFIG_EISA
5167 {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05005168 u16 eisa_slot;
Christoph Hellwig835cc242007-10-02 23:09:56 +02005169 for (eisa_slot = 0x1000; eisa_slot <= 0x8000;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02005170 eisa_slot += 0x1000)
Christoph Hellwig835cc242007-10-02 23:09:56 +02005171 gdth_eisa_probe_one(eisa_slot);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005172 }
5173#endif
5174 }
5175
5176#ifdef CONFIG_PCI
5177 /* scanning for PCI controllers */
James Bottomleya85591f2008-05-04 22:35:58 -05005178 if (pci_register_driver(&gdth_pci_driver)) {
5179 gdth_ha_str *ha;
5180
5181 list_for_each_entry(ha, &gdth_instances, list)
5182 gdth_remove_one(ha);
5183 return -ENODEV;
5184 }
Christoph Hellwig835cc242007-10-02 23:09:56 +02005185#endif /* CONFIG_PCI */
5186
5187 TRACE2(("gdth_detect() %d controller detected\n", gdth_ctr_count));
Linus Torvaldsc596cc42007-10-15 12:46:16 -07005188
Christoph Hellwig835cc242007-10-02 23:09:56 +02005189 major = register_chrdev(0,"gdth", &gdth_fops);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005190 register_reboot_notifier(&gdth_notifier);
5191 gdth_polling = FALSE;
5192 return 0;
5193}
5194
5195static void __exit gdth_exit(void)
5196{
5197 gdth_ha_str *ha;
5198
Boaz Harroshb31ddd32008-02-27 15:27:16 -08005199 unregister_chrdev(major, "gdth");
5200 unregister_reboot_notifier(&gdth_notifier);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005201
5202#ifdef GDTH_STATISTICS
Boaz Harroshb31ddd32008-02-27 15:27:16 -08005203 del_timer_sync(&gdth_timer);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005204#endif
Boaz Harroshb31ddd32008-02-27 15:27:16 -08005205
Jeff Garzikcff26802008-02-15 22:20:09 -05005206#ifdef CONFIG_PCI
James Bottomleya85591f2008-05-04 22:35:58 -05005207 pci_unregister_driver(&gdth_pci_driver);
Jeff Garzikcff26802008-02-15 22:20:09 -05005208#endif
5209
Boaz Harroshb31ddd32008-02-27 15:27:16 -08005210 list_for_each_entry(ha, &gdth_instances, list)
5211 gdth_remove_one(ha);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005212}
5213
5214module_init(gdth_init);
5215module_exit(gdth_exit);
5216
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217#ifndef MODULE
5218__setup("gdth=", option_setup);
5219#endif