blob: 6d55b4e7e7923121d58535ab1a7ff83c50298c8c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/************************************************************************
2 * Linux driver for *
3 * ICP vortex GmbH: GDT ISA/EISA/PCI Disk Array Controllers *
4 * Intel Corporation: Storage RAID Controllers *
5 * *
6 * gdth.c *
Leubner, Achimcbd5f692006-06-09 11:34:29 -07007 * Copyright (C) 1995-06 ICP vortex GmbH, Achim Leubner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Copyright (C) 2002-04 Intel Corporation *
Leubner, Achimcbd5f692006-06-09 11:34:29 -07009 * Copyright (C) 2003-06 Adaptec Inc. *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * <achim_leubner@adaptec.com> *
11 * *
12 * Additions/Fixes: *
13 * Boji Tony Kannanthanam <boji.t.kannanthanam@intel.com> *
14 * Johannes Dinner <johannes_dinner@adaptec.com> *
15 * *
16 * This program is free software; you can redistribute it and/or modify *
17 * it under the terms of the GNU General Public License as published *
18 * by the Free Software Foundation; either version 2 of the License, *
19 * or (at your option) any later version. *
20 * *
21 * This program is distributed in the hope that it will be useful, *
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
24 * GNU General Public License for more details. *
25 * *
26 * You should have received a copy of the GNU General Public License *
27 * along with this kernel; if not, write to the Free Software *
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
29 * *
Jeff Garzik8d7a5da2007-10-02 22:54:28 +020030 * Linux kernel 2.6.x supported *
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * *
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 ************************************************************************/
33
34/* All GDT Disk Array Controllers are fully supported by this driver.
35 * This includes the PCI/EISA/ISA SCSI Disk Array Controllers and the
36 * PCI Fibre Channel Disk Array Controllers. See gdth.h for a complete
37 * list of all controller types.
38 *
39 * If you have one or more GDT3000/3020 EISA controllers with
40 * controller BIOS disabled, you have to set the IRQ values with the
41 * command line option "gdth=irq1,irq2,...", where the irq1,irq2,... are
42 * the IRQ values for the EISA controllers.
43 *
44 * After the optional list of IRQ values, other possible
45 * command line options are:
46 * disable:Y disable driver
47 * disable:N enable driver
48 * reserve_mode:0 reserve no drives for the raw service
49 * reserve_mode:1 reserve all not init., removable drives
50 * reserve_mode:2 reserve all not init. drives
51 * reserve_list:h,b,t,l,h,b,t,l,... reserve particular drive(s) with
52 * h- controller no., b- channel no.,
53 * t- target ID, l- LUN
54 * reverse_scan:Y reverse scan order for PCI controllers
55 * reverse_scan:N scan PCI controllers like BIOS
56 * max_ids:x x - target ID count per channel (1..MAXID)
57 * rescan:Y rescan all channels/IDs
58 * rescan:N use all devices found until now
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * hdr_channel:x x - number of virtual bus for host drives
60 * shared_access:Y disable driver reserve/release protocol to
61 * access a shared resource from several nodes,
Adrian Bunk575c9682006-01-15 02:00:17 +010062 * appropriate controller firmware required
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * shared_access:N enable driver reserve/release protocol
64 * probe_eisa_isa:Y scan for EISA/ISA controllers
65 * probe_eisa_isa:N do not scan for EISA/ISA controllers
66 * force_dma32:Y use only 32 bit DMA mode
67 * force_dma32:N use 64 bit DMA mode, if supported
68 *
69 * The default values are: "gdth=disable:N,reserve_mode:1,reverse_scan:N,
Christoph Hellwig52759e62007-10-02 22:59:53 +020070 * max_ids:127,rescan:N,hdr_channel:0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 * shared_access:Y,probe_eisa_isa:N,force_dma32:N".
72 * Here is another example: "gdth=reserve_list:0,1,2,0,0,1,3,0,rescan:Y".
73 *
74 * When loading the gdth driver as a module, the same options are available.
75 * You can set the IRQs with "IRQ=...". However, the syntax to specify the
76 * options changes slightly. You must replace all ',' between options
77 * with ' ' and all ':' with '=' and you must use
78 * '1' in place of 'Y' and '0' in place of 'N'.
79 *
80 * Default: "modprobe gdth disable=0 reserve_mode=1 reverse_scan=0
Christoph Hellwig52759e62007-10-02 22:59:53 +020081 * max_ids=127 rescan=0 hdr_channel=0 shared_access=0
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * probe_eisa_isa=0 force_dma32=0"
83 * The other example: "modprobe gdth reserve_list=0,1,2,0,0,1,3,0 rescan=1".
84 */
85
86/* The meaning of the Scsi_Pointer members in this driver is as follows:
87 * ptr: Chaining
Boaz Harroshd35055a2008-02-27 15:31:20 -080088 * this_residual: unused
89 * buffer: unused
Boaz Harrosh3892d882007-10-02 23:18:03 +020090 * dma_handle: unused
Boaz Harroshd35055a2008-02-27 15:31:20 -080091 * buffers_residual: unused
Boaz Harrosh3892d882007-10-02 23:18:03 +020092 * Status: unused
Boaz Harroshf842b642007-10-02 23:16:01 +020093 * Message: unused
94 * have_data_in: unused
95 * sent_command: unused
96 * phase: unused
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 */
98
99
100/* interrupt coalescing */
101/* #define INT_COAL */
102
103/* statistics */
104#define GDTH_STATISTICS
105
106#include <linux/module.h>
107
108#include <linux/version.h>
109#include <linux/kernel.h>
110#include <linux/types.h>
111#include <linux/pci.h>
112#include <linux/string.h>
113#include <linux/ctype.h>
114#include <linux/ioport.h>
115#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#include <linux/interrupt.h>
117#include <linux/in.h>
118#include <linux/proc_fs.h>
119#include <linux/time.h>
120#include <linux/timer.h>
Matthias Gehre910638a2006-03-28 01:56:48 -0800121#include <linux/dma-mapping.h>
Christoph Hellwig835cc242007-10-02 23:09:56 +0200122#include <linux/list.h>
Arnd Bergmannc45d15d2010-06-02 14:28:52 +0200123#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +0900124#include <linux/slab.h>
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#ifdef GDTH_RTC
127#include <linux/mc146818rtc.h>
128#endif
129#include <linux/reboot.h>
130
131#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#include <asm/io.h>
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
Arnd Bergmannc45d15d2010-06-02 14:28:52 +0200142static DEFINE_MUTEX(gdth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static void gdth_delay(int milliseconds);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500144static void gdth_eval_mapping(u32 size, u32 *cyls, int *heads, int *secs);
David Howells7d12e782006-10-05 14:55:46 +0100145static irqreturn_t gdth_interrupt(int irq, void *dev_id);
Jeff Garzik230e8862007-12-13 16:14:12 -0800146static irqreturn_t __gdth_interrupt(gdth_ha_str *ha,
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +0200147 int gdth_from_wait, int* pIndex);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500148static int gdth_sync_event(gdth_ha_str *ha, int service, u8 index,
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200149 Scsi_Cmnd *scp);
150static int gdth_async_event(gdth_ha_str *ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static void gdth_log_event(gdth_evt_data *dvr, char *buffer);
152
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500153static void gdth_putq(gdth_ha_str *ha, Scsi_Cmnd *scp, u8 priority);
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200154static void gdth_next(gdth_ha_str *ha);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500155static int gdth_fill_raw_cmd(gdth_ha_str *ha, Scsi_Cmnd *scp, u8 b);
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200156static int gdth_special_cmd(gdth_ha_str *ha, Scsi_Cmnd *scp);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500157static gdth_evt_str *gdth_store_event(gdth_ha_str *ha, u16 source,
158 u16 idx, gdth_evt_data *evt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static int gdth_read_event(gdth_ha_str *ha, int handle, gdth_evt_str *estr);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500160static void gdth_readapp_event(gdth_ha_str *ha, u8 application,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 gdth_evt_str *estr);
162static void gdth_clear_events(void);
163
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200164static void gdth_copy_internal_data(gdth_ha_str *ha, Scsi_Cmnd *scp,
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500165 char *buffer, u16 count);
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200166static int gdth_internal_cache_cmd(gdth_ha_str *ha, Scsi_Cmnd *scp);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500167static int gdth_fill_cache_cmd(gdth_ha_str *ha, Scsi_Cmnd *scp, u16 hdrive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200169static void gdth_enable_int(gdth_ha_str *ha);
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200170static int gdth_test_busy(gdth_ha_str *ha);
171static int gdth_get_cmd_index(gdth_ha_str *ha);
172static void gdth_release_event(gdth_ha_str *ha);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500173static int gdth_wait(gdth_ha_str *ha, int index,u32 time);
174static int gdth_internal_cmd(gdth_ha_str *ha, u8 service, u16 opcode,
175 u32 p1, u64 p2,u64 p3);
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200176static int gdth_search_drives(gdth_ha_str *ha);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500177static int gdth_analyse_hdrive(gdth_ha_str *ha, u16 hdrive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200179static const char *gdth_ctr_name(gdth_ha_str *ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181static int gdth_open(struct inode *inode, struct file *filep);
182static int gdth_close(struct inode *inode, struct file *filep);
Arnd Bergmannf4927c42010-04-27 00:24:01 +0200183static long gdth_unlocked_ioctl(struct file *filep, unsigned int cmd,
184 unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200186static void gdth_flush(gdth_ha_str *ha);
Jeff Garzikf2812332010-11-16 02:10:29 -0500187static int gdth_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200188static int __gdth_queuecommand(gdth_ha_str *ha, struct scsi_cmnd *scp,
189 struct gdth_cmndinfo *cmndinfo);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700190static void gdth_scsi_done(struct scsi_cmnd *scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192#ifdef DEBUG_GDTH
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500193static u8 DebugState = DEBUG_GDTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195#ifdef __SERIAL__
196#define MAX_SERBUF 160
197static void ser_init(void);
198static void ser_puts(char *str);
199static void ser_putc(char c);
200static int ser_printk(const char *fmt, ...);
201static char strbuf[MAX_SERBUF+1];
202#ifdef __COM2__
203#define COM_BASE 0x2f8
204#else
205#define COM_BASE 0x3f8
206#endif
207static void ser_init()
208{
209 unsigned port=COM_BASE;
210
211 outb(0x80,port+3);
212 outb(0,port+1);
213 /* 19200 Baud, if 9600: outb(12,port) */
214 outb(6, port);
215 outb(3,port+3);
216 outb(0,port+1);
217 /*
218 ser_putc('I');
219 ser_putc(' ');
220 */
221}
222
223static void ser_puts(char *str)
224{
225 char *ptr;
226
227 ser_init();
228 for (ptr=str;*ptr;++ptr)
229 ser_putc(*ptr);
230}
231
232static void ser_putc(char c)
233{
234 unsigned port=COM_BASE;
235
236 while ((inb(port+5) & 0x20)==0);
237 outb(c,port);
238 if (c==0x0a)
239 {
240 while ((inb(port+5) & 0x20)==0);
241 outb(0x0d,port);
242 }
243}
244
245static int ser_printk(const char *fmt, ...)
246{
247 va_list args;
248 int i;
249
250 va_start(args,fmt);
251 i = vsprintf(strbuf,fmt,args);
252 ser_puts(strbuf);
253 va_end(args);
254 return i;
255}
256
257#define TRACE(a) {if (DebugState==1) {ser_printk a;}}
258#define TRACE2(a) {if (DebugState==1 || DebugState==2) {ser_printk a;}}
259#define TRACE3(a) {if (DebugState!=0) {ser_printk a;}}
260
261#else /* !__SERIAL__ */
262#define TRACE(a) {if (DebugState==1) {printk a;}}
263#define TRACE2(a) {if (DebugState==1 || DebugState==2) {printk a;}}
264#define TRACE3(a) {if (DebugState!=0) {printk a;}}
265#endif
266
267#else /* !DEBUG */
268#define TRACE(a)
269#define TRACE2(a)
270#define TRACE3(a)
271#endif
272
273#ifdef GDTH_STATISTICS
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500274static u32 max_rq=0, max_index=0, max_sg=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275#ifdef INT_COAL
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500276static u32 max_int_coal=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277#endif
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500278static u32 act_ints=0, act_ios=0, act_stats=0, act_rq=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279static struct timer_list gdth_timer;
280#endif
281
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500282#define PTR2USHORT(a) (u16)(unsigned long)(a)
Tobias Klauser6391a112006-06-08 22:23:48 -0700283#define GDTOFFSOF(a,b) (size_t)&(((a*)0)->b)
284#define INDEX_OK(i,t) ((i)<ARRAY_SIZE(t))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286#define BUS_L2P(a,b) ((b)>(a)->virt_bus ? (b-1):(b))
287
Christoph Hellwigaed91cb2007-10-02 22:48:16 +0200288#ifdef CONFIG_ISA
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500289static u8 gdth_drq_tab[4] = {5,6,7,7}; /* DRQ table */
Christoph Hellwigaed91cb2007-10-02 22:48:16 +0200290#endif
Dhaval Gianic8e91b02007-10-12 15:12:31 +0530291#if defined(CONFIG_EISA) || defined(CONFIG_ISA)
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500292static u8 gdth_irq_tab[6] = {0,10,11,12,14,0}; /* IRQ table */
Christoph Hellwig706a5d42007-10-02 22:49:35 +0200293#endif
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500294static u8 gdth_polling; /* polling if TRUE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295static int gdth_ctr_count = 0; /* controller count */
Boaz Harrosh884f7fb2007-10-02 23:11:24 +0200296static LIST_HEAD(gdth_instances); /* controller list */
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500297static u8 gdth_write_through = FALSE; /* write through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298static gdth_evt_str ebuffer[MAX_EVENTS]; /* event buffer */
299static int elastidx;
300static int eoldidx;
301static int major;
302
303#define DIN 1 /* IN data direction */
304#define DOU 2 /* OUT data direction */
305#define DNO DIN /* no data transfer */
306#define DUN DIN /* unknown data direction */
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500307static u8 gdth_direction_tab[0x100] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 DNO,DNO,DIN,DIN,DOU,DIN,DIN,DOU,DIN,DUN,DOU,DOU,DUN,DUN,DUN,DIN,
309 DNO,DIN,DIN,DOU,DIN,DOU,DNO,DNO,DOU,DNO,DIN,DNO,DIN,DOU,DNO,DUN,
310 DIN,DUN,DIN,DUN,DOU,DIN,DUN,DUN,DIN,DIN,DOU,DNO,DUN,DIN,DOU,DOU,
311 DOU,DOU,DOU,DNO,DIN,DNO,DNO,DIN,DOU,DOU,DOU,DOU,DIN,DOU,DIN,DOU,
312 DOU,DOU,DIN,DIN,DIN,DNO,DUN,DNO,DNO,DNO,DUN,DNO,DOU,DIN,DUN,DUN,
313 DUN,DUN,DUN,DUN,DUN,DOU,DUN,DUN,DUN,DUN,DIN,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,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
316 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DIN,DUN,DOU,DUN,DUN,DUN,DUN,DUN,
317 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DIN,DUN,
318 DUN,DUN,DUN,DUN,DUN,DNO,DNO,DUN,DIN,DNO,DOU,DUN,DNO,DUN,DOU,DOU,
319 DOU,DOU,DOU,DNO,DUN,DIN,DOU,DIN,DIN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
320 DUN,DUN,DOU,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
321 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
322 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DOU,DUN,DUN,DUN,DUN,DUN,
323 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN
324};
325
326/* LILO and modprobe/insmod parameters */
327/* IRQ list for GDT3000/3020 EISA controllers */
328static int irq[MAXHA] __initdata =
329{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
330 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
331/* disable driver flag */
332static int disable __initdata = 0;
333/* reserve flag */
334static int reserve_mode = 1;
335/* reserve list */
336static int reserve_list[MAX_RES_ARGS] =
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,0xff,0xff,
339 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
340/* scan order for PCI controllers */
341static int reverse_scan = 0;
342/* virtual channel for the host drives */
343static int hdr_channel = 0;
344/* max. IDs per channel */
345static int max_ids = MAXID;
346/* rescan all IDs */
347static int rescan = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348/* shared access */
349static int shared_access = 1;
350/* enable support for EISA and ISA controllers */
351static int probe_eisa_isa = 0;
352/* 64 bit DMA mode, support for drives > 2 TB, if force_dma32 = 0 */
353static int force_dma32 = 0;
354
355/* parameters for modprobe/insmod */
356module_param_array(irq, int, NULL, 0);
357module_param(disable, int, 0);
358module_param(reserve_mode, int, 0);
359module_param_array(reserve_list, int, NULL, 0);
360module_param(reverse_scan, int, 0);
361module_param(hdr_channel, int, 0);
362module_param(max_ids, int, 0);
363module_param(rescan, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364module_param(shared_access, int, 0);
365module_param(probe_eisa_isa, int, 0);
366module_param(force_dma32, int, 0);
367MODULE_AUTHOR("Achim Leubner");
368MODULE_LICENSE("GPL");
369
370/* ioctl interface */
Arjan van de Ven00977a52007-02-12 00:55:34 -0800371static const struct file_operations gdth_fops = {
Arnd Bergmannf4927c42010-04-27 00:24:01 +0200372 .unlocked_ioctl = gdth_unlocked_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 .open = gdth_open,
374 .release = gdth_close,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200375 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376};
377
378#include "gdth_proc.h"
379#include "gdth_proc.c"
380
Boaz Harrosh884f7fb2007-10-02 23:11:24 +0200381static gdth_ha_str *gdth_find_ha(int hanum)
382{
383 gdth_ha_str *ha;
384
385 list_for_each_entry(ha, &gdth_instances, list)
386 if (hanum == ha->hanum)
387 return ha;
388
389 return NULL;
390}
391
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200392static struct gdth_cmndinfo *gdth_get_cmndinfo(gdth_ha_str *ha)
393{
394 struct gdth_cmndinfo *priv = NULL;
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500395 unsigned long flags;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200396 int i;
397
398 spin_lock_irqsave(&ha->smp_lock, flags);
399
400 for (i=0; i<GDTH_MAXCMDS; ++i) {
401 if (ha->cmndinfo[i].index == 0) {
402 priv = &ha->cmndinfo[i];
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200403 memset(priv, 0, sizeof(*priv));
Boaz Harroshee54cc62008-02-27 15:29:15 -0800404 priv->index = i+1;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200405 break;
406 }
407 }
408
409 spin_unlock_irqrestore(&ha->smp_lock, flags);
410
411 return priv;
412}
413
414static void gdth_put_cmndinfo(struct gdth_cmndinfo *priv)
415{
416 BUG_ON(!priv);
417 priv->index = 0;
418}
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420static void gdth_delay(int milliseconds)
421{
422 if (milliseconds == 0) {
423 udelay(1);
424 } else {
425 mdelay(milliseconds);
426 }
427}
428
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700429static void gdth_scsi_done(struct scsi_cmnd *scp)
430{
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200431 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
432 int internal_command = cmndinfo->internal_command;
433
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +0200434 TRACE2(("gdth_scsi_done()\n"));
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700435
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200436 gdth_put_cmndinfo(cmndinfo);
437 scp->host_scribble = NULL;
438
439 if (internal_command)
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +0200440 complete((struct completion *)scp->request);
441 else
442 scp->scsi_done(scp);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700443}
444
445int __gdth_execute(struct scsi_device *sdev, gdth_cmd_str *gdtcmd, char *cmnd,
446 int timeout, u32 *info)
447{
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200448 gdth_ha_str *ha = shost_priv(sdev->host);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700449 Scsi_Cmnd *scp;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200450 struct gdth_cmndinfo cmndinfo;
Peter Zijlstra6e9a4732006-09-30 23:28:10 -0700451 DECLARE_COMPLETION_ONSTACK(wait);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700452 int rval;
453
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +0200454 scp = kzalloc(sizeof(*scp), GFP_KERNEL);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700455 if (!scp)
456 return -ENOMEM;
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +0200457
Sven Schnelle1b96f892008-03-10 22:50:04 +0100458 scp->sense_buffer = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
459 if (!scp->sense_buffer) {
460 kfree(scp);
461 return -ENOMEM;
462 }
463
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700464 scp->device = sdev;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200465 memset(&cmndinfo, 0, sizeof(cmndinfo));
466
Christoph Hellwigbeb40482006-06-10 18:01:03 +0200467 /* use request field to save the ptr. to completion struct. */
468 scp->request = (struct request *)&wait;
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700469 scp->cmd_len = 12;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300470 scp->cmnd = cmnd;
Boaz Harroshf842b642007-10-02 23:16:01 +0200471 cmndinfo.priority = IOCTL_PRI;
Boaz Harroshee54cc62008-02-27 15:29:15 -0800472 cmndinfo.internal_cmd_str = gdtcmd;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200473 cmndinfo.internal_command = 1;
474
475 TRACE(("__gdth_execute() cmd 0x%x\n", scp->cmnd[0]));
476 __gdth_queuecommand(ha, scp, &cmndinfo);
477
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700478 wait_for_completion(&wait);
479
Boaz Harroshf842b642007-10-02 23:16:01 +0200480 rval = cmndinfo.status;
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700481 if (info)
Boaz Harroshf842b642007-10-02 23:16:01 +0200482 *info = cmndinfo.info;
Sven Schnelle1b96f892008-03-10 22:50:04 +0100483 kfree(scp->sense_buffer);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700484 kfree(scp);
485 return rval;
486}
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700487
488int gdth_execute(struct Scsi_Host *shost, gdth_cmd_str *gdtcmd, char *cmnd,
489 int timeout, u32 *info)
490{
491 struct scsi_device *sdev = scsi_get_host_dev(shost);
492 int rval = __gdth_execute(sdev, gdtcmd, cmnd, timeout, info);
493
494 scsi_free_host_dev(sdev);
495 return rval;
496}
497
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500498static void gdth_eval_mapping(u32 size, u32 *cyls, int *heads, int *secs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 *cyls = size /HEADS/SECS;
501 if (*cyls <= MAXCYLS) {
502 *heads = HEADS;
503 *secs = SECS;
504 } else { /* too high for 64*32 */
505 *cyls = size /MEDHEADS/MEDSECS;
506 if (*cyls <= MAXCYLS) {
507 *heads = MEDHEADS;
508 *secs = MEDSECS;
509 } else { /* too high for 127*63 */
510 *cyls = size /BIGHEADS/BIGSECS;
511 *heads = BIGHEADS;
512 *secs = BIGSECS;
513 }
514 }
515}
516
517/* controller search and initialization functions */
Christoph Hellwig706a5d42007-10-02 22:49:35 +0200518#ifdef CONFIG_EISA
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500519static int __init gdth_search_eisa(u16 eisa_adr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500521 u32 id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 TRACE(("gdth_search_eisa() adr. %x\n",eisa_adr));
524 id = inl(eisa_adr+ID0REG);
525 if (id == GDT3A_ID || id == GDT3B_ID) { /* GDT3000A or GDT3000B */
526 if ((inb(eisa_adr+EISAREG) & 8) == 0)
527 return 0; /* not EISA configured */
528 return 1;
529 }
530 if (id == GDT3_ID) /* GDT3000 */
531 return 1;
532
533 return 0;
534}
Christoph Hellwig706a5d42007-10-02 22:49:35 +0200535#endif /* CONFIG_EISA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Christoph Hellwigaed91cb2007-10-02 22:48:16 +0200537#ifdef CONFIG_ISA
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500538static int __init gdth_search_isa(u32 bios_adr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 void __iomem *addr;
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500541 u32 id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 TRACE(("gdth_search_isa() bios adr. %x\n",bios_adr));
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500544 if ((addr = ioremap(bios_adr+BIOS_ID_OFFS, sizeof(u32))) != NULL) {
Jeff Garzika52667f2007-10-02 22:55:53 +0200545 id = readl(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 iounmap(addr);
547 if (id == GDT2_ID) /* GDT2000 */
548 return 1;
549 }
550 return 0;
551}
Christoph Hellwigaed91cb2007-10-02 22:48:16 +0200552#endif /* CONFIG_ISA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Christoph Hellwig8514ef22007-10-02 22:51:06 +0200554#ifdef CONFIG_PCI
Christoph Hellwig8514ef22007-10-02 22:51:06 +0200555
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500556static bool gdth_search_vortex(u16 device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Jeff Garzikcff26802008-02-15 22:20:09 -0500558 if (device <= PCI_DEVICE_ID_VORTEX_GDT6555)
559 return true;
560 if (device >= PCI_DEVICE_ID_VORTEX_GDT6x17RP &&
561 device <= PCI_DEVICE_ID_VORTEX_GDTMAXRP)
562 return true;
563 if (device == PCI_DEVICE_ID_VORTEX_GDTNEWRX ||
564 device == PCI_DEVICE_ID_VORTEX_GDTNEWRX2)
565 return true;
566 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
Jeff Garzikcff26802008-02-15 22:20:09 -0500569static int gdth_pci_probe_one(gdth_pci_str *pcistr, gdth_ha_str **ha_out);
570static int gdth_pci_init_one(struct pci_dev *pdev,
571 const struct pci_device_id *ent);
572static void gdth_pci_remove_one(struct pci_dev *pdev);
573static void gdth_remove_one(gdth_ha_str *ha);
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575/* Vortex only makes RAID controllers.
576 * We do not really want to specify all 550 ids here, so wildcard match.
577 */
Jeff Garzikcff26802008-02-15 22:20:09 -0500578static const struct pci_device_id gdthtable[] = {
579 { PCI_VDEVICE(VORTEX, PCI_ANY_ID) },
580 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_SRC) },
581 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_SRC_XSCALE) },
582 { } /* terminate list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583};
Jeff Garzikcff26802008-02-15 22:20:09 -0500584MODULE_DEVICE_TABLE(pci, gdthtable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Jeff Garzikcff26802008-02-15 22:20:09 -0500586static struct pci_driver gdth_pci_driver = {
587 .name = "gdth",
588 .id_table = gdthtable,
589 .probe = gdth_pci_init_one,
590 .remove = gdth_pci_remove_one,
591};
592
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800593static void gdth_pci_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Jeff Garzikcff26802008-02-15 22:20:09 -0500595 gdth_ha_str *ha = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Jeff Garzikcff26802008-02-15 22:20:09 -0500597 pci_set_drvdata(pdev, NULL);
598
599 list_del(&ha->list);
600 gdth_remove_one(ha);
601
602 pci_disable_device(pdev);
603}
604
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800605static int gdth_pci_init_one(struct pci_dev *pdev,
606 const struct pci_device_id *ent)
Jeff Garzikcff26802008-02-15 22:20:09 -0500607{
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500608 u16 vendor = pdev->vendor;
609 u16 device = pdev->device;
610 unsigned long base0, base1, base2;
Jeff Garzikcff26802008-02-15 22:20:09 -0500611 int rc;
612 gdth_pci_str gdth_pcistr;
613 gdth_ha_str *ha = NULL;
614
615 TRACE(("gdth_search_dev() cnt %d vendor %x device %x\n",
616 gdth_ctr_count, vendor, device));
617
618 memset(&gdth_pcistr, 0, sizeof(gdth_pcistr));
619
620 if (vendor == PCI_VENDOR_ID_VORTEX && !gdth_search_vortex(device))
621 return -ENODEV;
622
623 rc = pci_enable_device(pdev);
624 if (rc)
625 return rc;
626
627 if (gdth_ctr_count >= MAXHA)
628 return -EBUSY;
Sergio Luis99109302008-02-12 20:48:03 -0300629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /* GDT PCI controller found, resources are already in pdev */
Jeff Garzikcff26802008-02-15 22:20:09 -0500631 gdth_pcistr.pdev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 base0 = pci_resource_flags(pdev, 0);
633 base1 = pci_resource_flags(pdev, 1);
634 base2 = pci_resource_flags(pdev, 2);
635 if (device <= PCI_DEVICE_ID_VORTEX_GDT6000B || /* GDT6000/B */
636 device >= PCI_DEVICE_ID_VORTEX_GDT6x17RP) { /* MPR */
637 if (!(base0 & IORESOURCE_MEM))
Jeff Garzikcff26802008-02-15 22:20:09 -0500638 return -ENODEV;
639 gdth_pcistr.dpmem = pci_resource_start(pdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 } else { /* GDT6110, GDT6120, .. */
641 if (!(base0 & IORESOURCE_MEM) ||
642 !(base2 & IORESOURCE_MEM) ||
643 !(base1 & IORESOURCE_IO))
Jeff Garzikcff26802008-02-15 22:20:09 -0500644 return -ENODEV;
645 gdth_pcistr.dpmem = pci_resource_start(pdev, 2);
646 gdth_pcistr.io = pci_resource_start(pdev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648 TRACE2(("Controller found at %d/%d, irq %d, dpmem 0x%lx\n",
Jeff Garzikcff26802008-02-15 22:20:09 -0500649 gdth_pcistr.pdev->bus->number,
650 PCI_SLOT(gdth_pcistr.pdev->devfn),
651 gdth_pcistr.irq,
652 gdth_pcistr.dpmem));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Jeff Garzikcff26802008-02-15 22:20:09 -0500654 rc = gdth_pci_probe_one(&gdth_pcistr, &ha);
655 if (rc)
656 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Jeff Garzikcff26802008-02-15 22:20:09 -0500658 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
Christoph Hellwig8514ef22007-10-02 22:51:06 +0200660#endif /* CONFIG_PCI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Christoph Hellwig706a5d42007-10-02 22:49:35 +0200662#ifdef CONFIG_EISA
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500663static int __init gdth_init_eisa(u16 eisa_adr,gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500665 u32 retries,id;
666 u8 prot_ver,eisacf,i,irq_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 TRACE(("gdth_init_eisa() adr. %x\n",eisa_adr));
669
670 /* disable board interrupts, deinitialize services */
671 outb(0xff,eisa_adr+EDOORREG);
672 outb(0x00,eisa_adr+EDENABREG);
673 outb(0x00,eisa_adr+EINTENABREG);
674
675 outb(0xff,eisa_adr+LDOORREG);
676 retries = INIT_RETRIES;
677 gdth_delay(20);
678 while (inb(eisa_adr+EDOORREG) != 0xff) {
679 if (--retries == 0) {
680 printk("GDT-EISA: Initialization error (DEINIT failed)\n");
681 return 0;
682 }
683 gdth_delay(1);
684 TRACE2(("wait for DEINIT: retries=%d\n",retries));
685 }
686 prot_ver = inb(eisa_adr+MAILBOXREG);
687 outb(0xff,eisa_adr+EDOORREG);
688 if (prot_ver != PROTOCOL_VERSION) {
689 printk("GDT-EISA: Illegal protocol version\n");
690 return 0;
691 }
692 ha->bmic = eisa_adr;
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500693 ha->brd_phys = (u32)eisa_adr >> 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 outl(0,eisa_adr+MAILBOXREG);
696 outl(0,eisa_adr+MAILBOXREG+4);
697 outl(0,eisa_adr+MAILBOXREG+8);
698 outl(0,eisa_adr+MAILBOXREG+12);
699
700 /* detect IRQ */
701 if ((id = inl(eisa_adr+ID0REG)) == GDT3_ID) {
702 ha->oem_id = OEM_ID_ICP;
703 ha->type = GDT_EISA;
704 ha->stype = id;
705 outl(1,eisa_adr+MAILBOXREG+8);
706 outb(0xfe,eisa_adr+LDOORREG);
707 retries = INIT_RETRIES;
708 gdth_delay(20);
709 while (inb(eisa_adr+EDOORREG) != 0xfe) {
710 if (--retries == 0) {
711 printk("GDT-EISA: Initialization error (get IRQ failed)\n");
712 return 0;
713 }
714 gdth_delay(1);
715 }
716 ha->irq = inb(eisa_adr+MAILBOXREG);
717 outb(0xff,eisa_adr+EDOORREG);
718 TRACE2(("GDT3000/3020: IRQ=%d\n",ha->irq));
719 /* check the result */
720 if (ha->irq == 0) {
721 TRACE2(("Unknown IRQ, use IRQ table from cmd line !\n"));
722 for (i = 0, irq_found = FALSE;
723 i < MAXHA && irq[i] != 0xff; ++i) {
724 if (irq[i]==10 || irq[i]==11 || irq[i]==12 || irq[i]==14) {
725 irq_found = TRUE;
726 break;
727 }
728 }
729 if (irq_found) {
730 ha->irq = irq[i];
731 irq[i] = 0;
732 printk("GDT-EISA: Can not detect controller IRQ,\n");
733 printk("Use IRQ setting from command line (IRQ = %d)\n",
734 ha->irq);
735 } else {
736 printk("GDT-EISA: Initialization error (unknown IRQ), Enable\n");
737 printk("the controller BIOS or use command line parameters\n");
738 return 0;
739 }
740 }
741 } else {
742 eisacf = inb(eisa_adr+EISAREG) & 7;
743 if (eisacf > 4) /* level triggered */
744 eisacf -= 4;
745 ha->irq = gdth_irq_tab[eisacf];
746 ha->oem_id = OEM_ID_ICP;
747 ha->type = GDT_EISA;
748 ha->stype = id;
749 }
750
751 ha->dma64_support = 0;
752 return 1;
753}
Christoph Hellwig706a5d42007-10-02 22:49:35 +0200754#endif /* CONFIG_EISA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Christoph Hellwigaed91cb2007-10-02 22:48:16 +0200756#ifdef CONFIG_ISA
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500757static int __init gdth_init_isa(u32 bios_adr,gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 register gdt2_dpram_str __iomem *dp2_ptr;
760 int i;
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500761 u8 irq_drq,prot_ver;
762 u32 retries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 TRACE(("gdth_init_isa() bios adr. %x\n",bios_adr));
765
766 ha->brd = ioremap(bios_adr, sizeof(gdt2_dpram_str));
767 if (ha->brd == NULL) {
768 printk("GDT-ISA: Initialization error (DPMEM remap error)\n");
769 return 0;
770 }
771 dp2_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +0200772 writeb(1, &dp2_ptr->io.memlock); /* switch off write protection */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 /* reset interface area */
774 memset_io(&dp2_ptr->u, 0, sizeof(dp2_ptr->u));
Jeff Garzika52667f2007-10-02 22:55:53 +0200775 if (readl(&dp2_ptr->u) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 printk("GDT-ISA: Initialization error (DPMEM write error)\n");
777 iounmap(ha->brd);
778 return 0;
779 }
780
781 /* disable board interrupts, read DRQ and IRQ */
Jeff Garzika52667f2007-10-02 22:55:53 +0200782 writeb(0xff, &dp2_ptr->io.irqdel);
783 writeb(0x00, &dp2_ptr->io.irqen);
784 writeb(0x00, &dp2_ptr->u.ic.S_Status);
785 writeb(0x00, &dp2_ptr->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Jeff Garzika52667f2007-10-02 22:55:53 +0200787 irq_drq = readb(&dp2_ptr->io.rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 for (i=0; i<3; ++i) {
789 if ((irq_drq & 1)==0)
790 break;
791 irq_drq >>= 1;
792 }
793 ha->drq = gdth_drq_tab[i];
794
Jeff Garzika52667f2007-10-02 22:55:53 +0200795 irq_drq = readb(&dp2_ptr->io.rq) >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 for (i=1; i<5; ++i) {
797 if ((irq_drq & 1)==0)
798 break;
799 irq_drq >>= 1;
800 }
801 ha->irq = gdth_irq_tab[i];
802
803 /* deinitialize services */
Jeff Garzika52667f2007-10-02 22:55:53 +0200804 writel(bios_adr, &dp2_ptr->u.ic.S_Info[0]);
805 writeb(0xff, &dp2_ptr->u.ic.S_Cmd_Indx);
806 writeb(0, &dp2_ptr->io.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 retries = INIT_RETRIES;
808 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +0200809 while (readb(&dp2_ptr->u.ic.S_Status) != 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (--retries == 0) {
811 printk("GDT-ISA: Initialization error (DEINIT failed)\n");
812 iounmap(ha->brd);
813 return 0;
814 }
815 gdth_delay(1);
816 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500817 prot_ver = (u8)readl(&dp2_ptr->u.ic.S_Info[0]);
Jeff Garzika52667f2007-10-02 22:55:53 +0200818 writeb(0, &dp2_ptr->u.ic.Status);
819 writeb(0xff, &dp2_ptr->io.irqdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (prot_ver != PROTOCOL_VERSION) {
821 printk("GDT-ISA: Illegal protocol version\n");
822 iounmap(ha->brd);
823 return 0;
824 }
825
826 ha->oem_id = OEM_ID_ICP;
827 ha->type = GDT_ISA;
828 ha->ic_all_size = sizeof(dp2_ptr->u);
829 ha->stype= GDT2_ID;
830 ha->brd_phys = bios_adr >> 4;
831
832 /* special request to controller BIOS */
Jeff Garzika52667f2007-10-02 22:55:53 +0200833 writel(0x00, &dp2_ptr->u.ic.S_Info[0]);
834 writel(0x00, &dp2_ptr->u.ic.S_Info[1]);
835 writel(0x01, &dp2_ptr->u.ic.S_Info[2]);
836 writel(0x00, &dp2_ptr->u.ic.S_Info[3]);
837 writeb(0xfe, &dp2_ptr->u.ic.S_Cmd_Indx);
838 writeb(0, &dp2_ptr->io.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 retries = INIT_RETRIES;
840 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +0200841 while (readb(&dp2_ptr->u.ic.S_Status) != 0xfe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (--retries == 0) {
843 printk("GDT-ISA: Initialization error\n");
844 iounmap(ha->brd);
845 return 0;
846 }
847 gdth_delay(1);
848 }
Jeff Garzika52667f2007-10-02 22:55:53 +0200849 writeb(0, &dp2_ptr->u.ic.Status);
850 writeb(0xff, &dp2_ptr->io.irqdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 ha->dma64_support = 0;
853 return 1;
854}
Christoph Hellwigaed91cb2007-10-02 22:48:16 +0200855#endif /* CONFIG_ISA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Christoph Hellwig8514ef22007-10-02 22:51:06 +0200857#ifdef CONFIG_PCI
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800858static int gdth_init_pci(struct pci_dev *pdev, gdth_pci_str *pcistr,
859 gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
861 register gdt6_dpram_str __iomem *dp6_ptr;
862 register gdt6c_dpram_str __iomem *dp6c_ptr;
863 register gdt6m_dpram_str __iomem *dp6m_ptr;
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500864 u32 retries;
865 u8 prot_ver;
866 u16 command;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 int i, found = FALSE;
868
869 TRACE(("gdth_init_pci()\n"));
870
Jeff Garzik4c9c8d72008-02-15 21:35:26 -0500871 if (pdev->vendor == PCI_VENDOR_ID_INTEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 ha->oem_id = OEM_ID_INTEL;
873 else
874 ha->oem_id = OEM_ID_ICP;
Jeff Garzik4c9c8d72008-02-15 21:35:26 -0500875 ha->brd_phys = (pdev->bus->number << 8) | (pdev->devfn & 0xf8);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500876 ha->stype = (u32)pdev->device;
Jeff Garzik4c9c8d72008-02-15 21:35:26 -0500877 ha->irq = pdev->irq;
878 ha->pdev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Jeff Garzik8e9a8a02007-07-17 05:25:17 -0400880 if (ha->pdev->device <= PCI_DEVICE_ID_VORTEX_GDT6000B) { /* GDT6000/B */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 TRACE2(("init_pci() dpmem %lx irq %d\n",pcistr->dpmem,ha->irq));
882 ha->brd = ioremap(pcistr->dpmem, sizeof(gdt6_dpram_str));
883 if (ha->brd == NULL) {
884 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
885 return 0;
886 }
887 /* check and reset interface area */
888 dp6_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +0200889 writel(DPMEM_MAGIC, &dp6_ptr->u);
890 if (readl(&dp6_ptr->u) != DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 printk("GDT-PCI: Cannot access DPMEM at 0x%lx (shadowed?)\n",
892 pcistr->dpmem);
893 found = FALSE;
894 for (i = 0xC8000; i < 0xE8000; i += 0x4000) {
895 iounmap(ha->brd);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500896 ha->brd = ioremap(i, sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (ha->brd == NULL) {
898 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
899 return 0;
900 }
Jeff Garzika52667f2007-10-02 22:55:53 +0200901 if (readw(ha->brd) != 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 TRACE2(("init_pci_old() address 0x%x busy\n", i));
903 continue;
904 }
905 iounmap(ha->brd);
Jeff Garzik4c9c8d72008-02-15 21:35:26 -0500906 pci_write_config_dword(pdev, PCI_BASE_ADDRESS_0, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 ha->brd = ioremap(i, sizeof(gdt6_dpram_str));
908 if (ha->brd == NULL) {
909 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
910 return 0;
911 }
912 dp6_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +0200913 writel(DPMEM_MAGIC, &dp6_ptr->u);
914 if (readl(&dp6_ptr->u) == DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 printk("GDT-PCI: Use free address at 0x%x\n", i);
916 found = TRUE;
917 break;
918 }
919 }
920 if (!found) {
921 printk("GDT-PCI: No free address found!\n");
922 iounmap(ha->brd);
923 return 0;
924 }
925 }
926 memset_io(&dp6_ptr->u, 0, sizeof(dp6_ptr->u));
Jeff Garzika52667f2007-10-02 22:55:53 +0200927 if (readl(&dp6_ptr->u) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 printk("GDT-PCI: Initialization error (DPMEM write error)\n");
929 iounmap(ha->brd);
930 return 0;
931 }
932
933 /* disable board interrupts, deinit services */
Jeff Garzika52667f2007-10-02 22:55:53 +0200934 writeb(0xff, &dp6_ptr->io.irqdel);
935 writeb(0x00, &dp6_ptr->io.irqen);
936 writeb(0x00, &dp6_ptr->u.ic.S_Status);
937 writeb(0x00, &dp6_ptr->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Jeff Garzika52667f2007-10-02 22:55:53 +0200939 writel(pcistr->dpmem, &dp6_ptr->u.ic.S_Info[0]);
940 writeb(0xff, &dp6_ptr->u.ic.S_Cmd_Indx);
941 writeb(0, &dp6_ptr->io.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 retries = INIT_RETRIES;
943 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +0200944 while (readb(&dp6_ptr->u.ic.S_Status) != 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (--retries == 0) {
946 printk("GDT-PCI: Initialization error (DEINIT failed)\n");
947 iounmap(ha->brd);
948 return 0;
949 }
950 gdth_delay(1);
951 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500952 prot_ver = (u8)readl(&dp6_ptr->u.ic.S_Info[0]);
Jeff Garzika52667f2007-10-02 22:55:53 +0200953 writeb(0, &dp6_ptr->u.ic.S_Status);
954 writeb(0xff, &dp6_ptr->io.irqdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (prot_ver != PROTOCOL_VERSION) {
956 printk("GDT-PCI: Illegal protocol version\n");
957 iounmap(ha->brd);
958 return 0;
959 }
960
961 ha->type = GDT_PCI;
962 ha->ic_all_size = sizeof(dp6_ptr->u);
963
964 /* special command to controller BIOS */
Jeff Garzika52667f2007-10-02 22:55:53 +0200965 writel(0x00, &dp6_ptr->u.ic.S_Info[0]);
966 writel(0x00, &dp6_ptr->u.ic.S_Info[1]);
967 writel(0x00, &dp6_ptr->u.ic.S_Info[2]);
968 writel(0x00, &dp6_ptr->u.ic.S_Info[3]);
969 writeb(0xfe, &dp6_ptr->u.ic.S_Cmd_Indx);
970 writeb(0, &dp6_ptr->io.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 retries = INIT_RETRIES;
972 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +0200973 while (readb(&dp6_ptr->u.ic.S_Status) != 0xfe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (--retries == 0) {
975 printk("GDT-PCI: Initialization error\n");
976 iounmap(ha->brd);
977 return 0;
978 }
979 gdth_delay(1);
980 }
Jeff Garzika52667f2007-10-02 22:55:53 +0200981 writeb(0, &dp6_ptr->u.ic.S_Status);
982 writeb(0xff, &dp6_ptr->io.irqdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 ha->dma64_support = 0;
985
Jeff Garzik8e9a8a02007-07-17 05:25:17 -0400986 } else if (ha->pdev->device <= PCI_DEVICE_ID_VORTEX_GDT6555) { /* GDT6110, ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 ha->plx = (gdt6c_plx_regs *)pcistr->io;
988 TRACE2(("init_pci_new() dpmem %lx irq %d\n",
989 pcistr->dpmem,ha->irq));
990 ha->brd = ioremap(pcistr->dpmem, sizeof(gdt6c_dpram_str));
991 if (ha->brd == NULL) {
992 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
993 iounmap(ha->brd);
994 return 0;
995 }
996 /* check and reset interface area */
997 dp6c_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +0200998 writel(DPMEM_MAGIC, &dp6c_ptr->u);
999 if (readl(&dp6c_ptr->u) != DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 printk("GDT-PCI: Cannot access DPMEM at 0x%lx (shadowed?)\n",
1001 pcistr->dpmem);
1002 found = FALSE;
1003 for (i = 0xC8000; i < 0xE8000; i += 0x4000) {
1004 iounmap(ha->brd);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001005 ha->brd = ioremap(i, sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (ha->brd == NULL) {
1007 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1008 return 0;
1009 }
Jeff Garzika52667f2007-10-02 22:55:53 +02001010 if (readw(ha->brd) != 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 TRACE2(("init_pci_plx() address 0x%x busy\n", i));
1012 continue;
1013 }
1014 iounmap(ha->brd);
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05001015 pci_write_config_dword(pdev, PCI_BASE_ADDRESS_2, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 ha->brd = ioremap(i, sizeof(gdt6c_dpram_str));
1017 if (ha->brd == NULL) {
1018 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1019 return 0;
1020 }
1021 dp6c_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001022 writel(DPMEM_MAGIC, &dp6c_ptr->u);
1023 if (readl(&dp6c_ptr->u) == DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 printk("GDT-PCI: Use free address at 0x%x\n", i);
1025 found = TRUE;
1026 break;
1027 }
1028 }
1029 if (!found) {
1030 printk("GDT-PCI: No free address found!\n");
1031 iounmap(ha->brd);
1032 return 0;
1033 }
1034 }
1035 memset_io(&dp6c_ptr->u, 0, sizeof(dp6c_ptr->u));
Jeff Garzika52667f2007-10-02 22:55:53 +02001036 if (readl(&dp6c_ptr->u) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 printk("GDT-PCI: Initialization error (DPMEM write error)\n");
1038 iounmap(ha->brd);
1039 return 0;
1040 }
1041
1042 /* disable board interrupts, deinit services */
1043 outb(0x00,PTR2USHORT(&ha->plx->control1));
1044 outb(0xff,PTR2USHORT(&ha->plx->edoor_reg));
1045
Jeff Garzika52667f2007-10-02 22:55:53 +02001046 writeb(0x00, &dp6c_ptr->u.ic.S_Status);
1047 writeb(0x00, &dp6c_ptr->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Jeff Garzika52667f2007-10-02 22:55:53 +02001049 writel(pcistr->dpmem, &dp6c_ptr->u.ic.S_Info[0]);
1050 writeb(0xff, &dp6c_ptr->u.ic.S_Cmd_Indx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 outb(1,PTR2USHORT(&ha->plx->ldoor_reg));
1053
1054 retries = INIT_RETRIES;
1055 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +02001056 while (readb(&dp6c_ptr->u.ic.S_Status) != 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (--retries == 0) {
1058 printk("GDT-PCI: Initialization error (DEINIT failed)\n");
1059 iounmap(ha->brd);
1060 return 0;
1061 }
1062 gdth_delay(1);
1063 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001064 prot_ver = (u8)readl(&dp6c_ptr->u.ic.S_Info[0]);
Jeff Garzika52667f2007-10-02 22:55:53 +02001065 writeb(0, &dp6c_ptr->u.ic.Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (prot_ver != PROTOCOL_VERSION) {
1067 printk("GDT-PCI: Illegal protocol version\n");
1068 iounmap(ha->brd);
1069 return 0;
1070 }
1071
1072 ha->type = GDT_PCINEW;
1073 ha->ic_all_size = sizeof(dp6c_ptr->u);
1074
1075 /* special command to controller BIOS */
Jeff Garzika52667f2007-10-02 22:55:53 +02001076 writel(0x00, &dp6c_ptr->u.ic.S_Info[0]);
1077 writel(0x00, &dp6c_ptr->u.ic.S_Info[1]);
1078 writel(0x00, &dp6c_ptr->u.ic.S_Info[2]);
1079 writel(0x00, &dp6c_ptr->u.ic.S_Info[3]);
1080 writeb(0xfe, &dp6c_ptr->u.ic.S_Cmd_Indx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 outb(1,PTR2USHORT(&ha->plx->ldoor_reg));
1083
1084 retries = INIT_RETRIES;
1085 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +02001086 while (readb(&dp6c_ptr->u.ic.S_Status) != 0xfe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (--retries == 0) {
1088 printk("GDT-PCI: Initialization error\n");
1089 iounmap(ha->brd);
1090 return 0;
1091 }
1092 gdth_delay(1);
1093 }
Jeff Garzika52667f2007-10-02 22:55:53 +02001094 writeb(0, &dp6c_ptr->u.ic.S_Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 ha->dma64_support = 0;
1097
1098 } else { /* MPR */
1099 TRACE2(("init_pci_mpr() dpmem %lx irq %d\n",pcistr->dpmem,ha->irq));
1100 ha->brd = ioremap(pcistr->dpmem, sizeof(gdt6m_dpram_str));
1101 if (ha->brd == NULL) {
1102 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1103 return 0;
1104 }
1105
1106 /* manipulate config. space to enable DPMEM, start RP controller */
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05001107 pci_read_config_word(pdev, PCI_COMMAND, &command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 command |= 6;
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05001109 pci_write_config_word(pdev, PCI_COMMAND, command);
Bjorn Helgaas8108de92012-11-06 15:04:44 -07001110 gdth_delay(1);
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 dp6m_ptr = ha->brd;
1113
1114 /* Ensure that it is safe to access the non HW portions of DPMEM.
1115 * Aditional check needed for Xscale based RAID controllers */
Jeff Garzika52667f2007-10-02 22:55:53 +02001116 while( ((int)readb(&dp6m_ptr->i960r.sema0_reg) ) & 3 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 gdth_delay(1);
1118
1119 /* check and reset interface area */
Jeff Garzika52667f2007-10-02 22:55:53 +02001120 writel(DPMEM_MAGIC, &dp6m_ptr->u);
1121 if (readl(&dp6m_ptr->u) != DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 printk("GDT-PCI: Cannot access DPMEM at 0x%lx (shadowed?)\n",
1123 pcistr->dpmem);
1124 found = FALSE;
1125 for (i = 0xC8000; i < 0xE8000; i += 0x4000) {
1126 iounmap(ha->brd);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001127 ha->brd = ioremap(i, sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 if (ha->brd == NULL) {
1129 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1130 return 0;
1131 }
Jeff Garzika52667f2007-10-02 22:55:53 +02001132 if (readw(ha->brd) != 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 TRACE2(("init_pci_mpr() address 0x%x busy\n", i));
1134 continue;
1135 }
1136 iounmap(ha->brd);
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05001137 pci_write_config_dword(pdev, PCI_BASE_ADDRESS_0, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 ha->brd = ioremap(i, sizeof(gdt6m_dpram_str));
1139 if (ha->brd == NULL) {
1140 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1141 return 0;
1142 }
1143 dp6m_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001144 writel(DPMEM_MAGIC, &dp6m_ptr->u);
1145 if (readl(&dp6m_ptr->u) == DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 printk("GDT-PCI: Use free address at 0x%x\n", i);
1147 found = TRUE;
1148 break;
1149 }
1150 }
1151 if (!found) {
1152 printk("GDT-PCI: No free address found!\n");
1153 iounmap(ha->brd);
1154 return 0;
1155 }
1156 }
1157 memset_io(&dp6m_ptr->u, 0, sizeof(dp6m_ptr->u));
1158
1159 /* disable board interrupts, deinit services */
Jeff Garzika52667f2007-10-02 22:55:53 +02001160 writeb(readb(&dp6m_ptr->i960r.edoor_en_reg) | 4,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 &dp6m_ptr->i960r.edoor_en_reg);
Jeff Garzika52667f2007-10-02 22:55:53 +02001162 writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
1163 writeb(0x00, &dp6m_ptr->u.ic.S_Status);
1164 writeb(0x00, &dp6m_ptr->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Jeff Garzika52667f2007-10-02 22:55:53 +02001166 writel(pcistr->dpmem, &dp6m_ptr->u.ic.S_Info[0]);
1167 writeb(0xff, &dp6m_ptr->u.ic.S_Cmd_Indx);
1168 writeb(1, &dp6m_ptr->i960r.ldoor_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 retries = INIT_RETRIES;
1170 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +02001171 while (readb(&dp6m_ptr->u.ic.S_Status) != 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (--retries == 0) {
1173 printk("GDT-PCI: Initialization error (DEINIT failed)\n");
1174 iounmap(ha->brd);
1175 return 0;
1176 }
1177 gdth_delay(1);
1178 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001179 prot_ver = (u8)readl(&dp6m_ptr->u.ic.S_Info[0]);
Jeff Garzika52667f2007-10-02 22:55:53 +02001180 writeb(0, &dp6m_ptr->u.ic.S_Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (prot_ver != PROTOCOL_VERSION) {
1182 printk("GDT-PCI: Illegal protocol version\n");
1183 iounmap(ha->brd);
1184 return 0;
1185 }
1186
1187 ha->type = GDT_PCIMPR;
1188 ha->ic_all_size = sizeof(dp6m_ptr->u);
1189
1190 /* special command to controller BIOS */
Jeff Garzika52667f2007-10-02 22:55:53 +02001191 writel(0x00, &dp6m_ptr->u.ic.S_Info[0]);
1192 writel(0x00, &dp6m_ptr->u.ic.S_Info[1]);
1193 writel(0x00, &dp6m_ptr->u.ic.S_Info[2]);
1194 writel(0x00, &dp6m_ptr->u.ic.S_Info[3]);
1195 writeb(0xfe, &dp6m_ptr->u.ic.S_Cmd_Indx);
1196 writeb(1, &dp6m_ptr->i960r.ldoor_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 retries = INIT_RETRIES;
1198 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +02001199 while (readb(&dp6m_ptr->u.ic.S_Status) != 0xfe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (--retries == 0) {
1201 printk("GDT-PCI: Initialization error\n");
1202 iounmap(ha->brd);
1203 return 0;
1204 }
1205 gdth_delay(1);
1206 }
Jeff Garzika52667f2007-10-02 22:55:53 +02001207 writeb(0, &dp6m_ptr->u.ic.S_Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 /* read FW version to detect 64-bit DMA support */
Jeff Garzika52667f2007-10-02 22:55:53 +02001210 writeb(0xfd, &dp6m_ptr->u.ic.S_Cmd_Indx);
1211 writeb(1, &dp6m_ptr->i960r.ldoor_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 retries = INIT_RETRIES;
1213 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +02001214 while (readb(&dp6m_ptr->u.ic.S_Status) != 0xfd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (--retries == 0) {
1216 printk("GDT-PCI: Initialization error (DEINIT failed)\n");
1217 iounmap(ha->brd);
1218 return 0;
1219 }
1220 gdth_delay(1);
1221 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001222 prot_ver = (u8)(readl(&dp6m_ptr->u.ic.S_Info[0]) >> 16);
Jeff Garzika52667f2007-10-02 22:55:53 +02001223 writeb(0, &dp6m_ptr->u.ic.S_Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (prot_ver < 0x2b) /* FW < x.43: no 64-bit DMA support */
1225 ha->dma64_support = 0;
1226 else
1227 ha->dma64_support = 1;
1228 }
1229
1230 return 1;
1231}
Christoph Hellwig8514ef22007-10-02 22:51:06 +02001232#endif /* CONFIG_PCI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234/* controller protocol functions */
1235
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08001236static void gdth_enable_int(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001238 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 gdt2_dpram_str __iomem *dp2_ptr;
1240 gdt6_dpram_str __iomem *dp6_ptr;
1241 gdt6m_dpram_str __iomem *dp6m_ptr;
1242
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001243 TRACE(("gdth_enable_int() hanum %d\n",ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 spin_lock_irqsave(&ha->smp_lock, flags);
1245
1246 if (ha->type == GDT_EISA) {
1247 outb(0xff, ha->bmic + EDOORREG);
1248 outb(0xff, ha->bmic + EDENABREG);
1249 outb(0x01, ha->bmic + EINTENABREG);
1250 } else if (ha->type == GDT_ISA) {
1251 dp2_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001252 writeb(1, &dp2_ptr->io.irqdel);
1253 writeb(0, &dp2_ptr->u.ic.Cmd_Index);
1254 writeb(1, &dp2_ptr->io.irqen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 } else if (ha->type == GDT_PCI) {
1256 dp6_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001257 writeb(1, &dp6_ptr->io.irqdel);
1258 writeb(0, &dp6_ptr->u.ic.Cmd_Index);
1259 writeb(1, &dp6_ptr->io.irqen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 } else if (ha->type == GDT_PCINEW) {
1261 outb(0xff, PTR2USHORT(&ha->plx->edoor_reg));
1262 outb(0x03, PTR2USHORT(&ha->plx->control1));
1263 } else if (ha->type == GDT_PCIMPR) {
1264 dp6m_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001265 writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
1266 writeb(readb(&dp6m_ptr->i960r.edoor_en_reg) & ~4,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 &dp6m_ptr->i960r.edoor_en_reg);
1268 }
1269 spin_unlock_irqrestore(&ha->smp_lock, flags);
1270}
1271
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001272/* return IStatus if interrupt was from this card else 0 */
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001273static u8 gdth_get_status(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001275 u8 IStatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Jeff Garzik230e8862007-12-13 16:14:12 -08001277 TRACE(("gdth_get_status() irq %d ctr_count %d\n", ha->irq, gdth_ctr_count));
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 if (ha->type == GDT_EISA)
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001280 IStatus = inb((u16)ha->bmic + EDOORREG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 else if (ha->type == GDT_ISA)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001282 IStatus =
Jeff Garzika52667f2007-10-02 22:55:53 +02001283 readb(&((gdt2_dpram_str __iomem *)ha->brd)->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 else if (ha->type == GDT_PCI)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001285 IStatus =
Jeff Garzika52667f2007-10-02 22:55:53 +02001286 readb(&((gdt6_dpram_str __iomem *)ha->brd)->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 else if (ha->type == GDT_PCINEW)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001288 IStatus = inb(PTR2USHORT(&ha->plx->edoor_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 else if (ha->type == GDT_PCIMPR)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001290 IStatus =
Jeff Garzika52667f2007-10-02 22:55:53 +02001291 readb(&((gdt6m_dpram_str __iomem *)ha->brd)->i960r.edoor_reg);
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001292
1293 return IStatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001295
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001296static int gdth_test_busy(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 register int gdtsema0 = 0;
1299
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001300 TRACE(("gdth_test_busy() hanum %d\n", ha->hanum));
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (ha->type == GDT_EISA)
1303 gdtsema0 = (int)inb(ha->bmic + SEMA0REG);
1304 else if (ha->type == GDT_ISA)
Jeff Garzika52667f2007-10-02 22:55:53 +02001305 gdtsema0 = (int)readb(&((gdt2_dpram_str __iomem *)ha->brd)->u.ic.Sema0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 else if (ha->type == GDT_PCI)
Jeff Garzika52667f2007-10-02 22:55:53 +02001307 gdtsema0 = (int)readb(&((gdt6_dpram_str __iomem *)ha->brd)->u.ic.Sema0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 else if (ha->type == GDT_PCINEW)
1309 gdtsema0 = (int)inb(PTR2USHORT(&ha->plx->sema0_reg));
1310 else if (ha->type == GDT_PCIMPR)
1311 gdtsema0 =
Jeff Garzika52667f2007-10-02 22:55:53 +02001312 (int)readb(&((gdt6m_dpram_str __iomem *)ha->brd)->i960r.sema0_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 return (gdtsema0 & 1);
1315}
1316
1317
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001318static int gdth_get_cmd_index(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 int i;
1321
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001322 TRACE(("gdth_get_cmd_index() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 for (i=0; i<GDTH_MAXCMDS; ++i) {
1325 if (ha->cmd_tab[i].cmnd == UNUSED_CMND) {
1326 ha->cmd_tab[i].cmnd = ha->pccb->RequestBuffer;
1327 ha->cmd_tab[i].service = ha->pccb->Service;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001328 ha->pccb->CommandIndex = (u32)i+2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return (i+2);
1330 }
1331 }
1332 return 0;
1333}
1334
1335
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001336static void gdth_set_sema0(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001338 TRACE(("gdth_set_sema0() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (ha->type == GDT_EISA) {
1341 outb(1, ha->bmic + SEMA0REG);
1342 } else if (ha->type == GDT_ISA) {
Jeff Garzika52667f2007-10-02 22:55:53 +02001343 writeb(1, &((gdt2_dpram_str __iomem *)ha->brd)->u.ic.Sema0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 } else if (ha->type == GDT_PCI) {
Jeff Garzika52667f2007-10-02 22:55:53 +02001345 writeb(1, &((gdt6_dpram_str __iomem *)ha->brd)->u.ic.Sema0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 } else if (ha->type == GDT_PCINEW) {
1347 outb(1, PTR2USHORT(&ha->plx->sema0_reg));
1348 } else if (ha->type == GDT_PCIMPR) {
Jeff Garzika52667f2007-10-02 22:55:53 +02001349 writeb(1, &((gdt6m_dpram_str __iomem *)ha->brd)->i960r.sema0_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 }
1351}
1352
1353
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001354static void gdth_copy_command(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 register gdth_cmd_str *cmd_ptr;
1357 register gdt6m_dpram_str __iomem *dp6m_ptr;
1358 register gdt6c_dpram_str __iomem *dp6c_ptr;
1359 gdt6_dpram_str __iomem *dp6_ptr;
1360 gdt2_dpram_str __iomem *dp2_ptr;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001361 u16 cp_count,dp_offset,cmd_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001363 TRACE(("gdth_copy_command() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 cp_count = ha->cmd_len;
1366 dp_offset= ha->cmd_offs_dpmem;
1367 cmd_no = ha->cmd_cnt;
1368 cmd_ptr = ha->pccb;
1369
1370 ++ha->cmd_cnt;
1371 if (ha->type == GDT_EISA)
1372 return; /* no DPMEM, no copy */
1373
1374 /* set cpcount dword aligned */
1375 if (cp_count & 3)
1376 cp_count += (4 - (cp_count & 3));
1377
1378 ha->cmd_offs_dpmem += cp_count;
1379
1380 /* set offset and service, copy command to DPMEM */
1381 if (ha->type == GDT_ISA) {
1382 dp2_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001383 writew(dp_offset + DPMEM_COMMAND_OFFSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 &dp2_ptr->u.ic.comm_queue[cmd_no].offset);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001385 writew((u16)cmd_ptr->Service,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 &dp2_ptr->u.ic.comm_queue[cmd_no].serv_id);
1387 memcpy_toio(&dp2_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
1388 } else if (ha->type == GDT_PCI) {
1389 dp6_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001390 writew(dp_offset + DPMEM_COMMAND_OFFSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 &dp6_ptr->u.ic.comm_queue[cmd_no].offset);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001392 writew((u16)cmd_ptr->Service,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 &dp6_ptr->u.ic.comm_queue[cmd_no].serv_id);
1394 memcpy_toio(&dp6_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
1395 } else if (ha->type == GDT_PCINEW) {
1396 dp6c_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001397 writew(dp_offset + DPMEM_COMMAND_OFFSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 &dp6c_ptr->u.ic.comm_queue[cmd_no].offset);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001399 writew((u16)cmd_ptr->Service,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 &dp6c_ptr->u.ic.comm_queue[cmd_no].serv_id);
1401 memcpy_toio(&dp6c_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
1402 } else if (ha->type == GDT_PCIMPR) {
1403 dp6m_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001404 writew(dp_offset + DPMEM_COMMAND_OFFSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 &dp6m_ptr->u.ic.comm_queue[cmd_no].offset);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001406 writew((u16)cmd_ptr->Service,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 &dp6m_ptr->u.ic.comm_queue[cmd_no].serv_id);
1408 memcpy_toio(&dp6m_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
1409 }
1410}
1411
1412
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001413static void gdth_release_event(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001415 TRACE(("gdth_release_event() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417#ifdef GDTH_STATISTICS
1418 {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001419 u32 i,j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 for (i=0,j=0; j<GDTH_MAXCMDS; ++j) {
1421 if (ha->cmd_tab[j].cmnd != UNUSED_CMND)
1422 ++i;
1423 }
1424 if (max_index < i) {
1425 max_index = i;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001426 TRACE3(("GDT: max_index = %d\n",(u16)i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428 }
1429#endif
1430
1431 if (ha->pccb->OpCode == GDT_INIT)
1432 ha->pccb->Service |= 0x80;
1433
1434 if (ha->type == GDT_EISA) {
1435 if (ha->pccb->OpCode == GDT_INIT) /* store DMA buffer */
1436 outl(ha->ccb_phys, ha->bmic + MAILBOXREG);
1437 outb(ha->pccb->Service, ha->bmic + LDOORREG);
1438 } else if (ha->type == GDT_ISA) {
Jeff Garzika52667f2007-10-02 22:55:53 +02001439 writeb(0, &((gdt2_dpram_str __iomem *)ha->brd)->io.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 } else if (ha->type == GDT_PCI) {
Jeff Garzika52667f2007-10-02 22:55:53 +02001441 writeb(0, &((gdt6_dpram_str __iomem *)ha->brd)->io.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 } else if (ha->type == GDT_PCINEW) {
1443 outb(1, PTR2USHORT(&ha->plx->ldoor_reg));
1444 } else if (ha->type == GDT_PCIMPR) {
Jeff Garzika52667f2007-10-02 22:55:53 +02001445 writeb(1, &((gdt6m_dpram_str __iomem *)ha->brd)->i960r.ldoor_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
1447}
1448
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001449static int gdth_wait(gdth_ha_str *ha, int index, u32 time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 int answer_found = FALSE;
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001452 int wait_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001454 TRACE(("gdth_wait() hanum %d index %d time %d\n", ha->hanum, index, time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 if (index == 0)
1457 return 1; /* no wait required */
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 do {
Jeff Garzik230e8862007-12-13 16:14:12 -08001460 __gdth_interrupt(ha, true, &wait_index);
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001461 if (wait_index == index) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 answer_found = TRUE;
1463 break;
1464 }
1465 gdth_delay(1);
1466 } while (--time);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001467
1468 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 gdth_delay(0);
1470
1471 return (answer_found);
1472}
1473
1474
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001475static int gdth_internal_cmd(gdth_ha_str *ha, u8 service, u16 opcode,
1476 u32 p1, u64 p2, u64 p3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 register gdth_cmd_str *cmd_ptr;
1479 int retries,index;
1480
1481 TRACE2(("gdth_internal_cmd() service %d opcode %d\n",service,opcode));
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 cmd_ptr = ha->pccb;
1484 memset((char*)cmd_ptr,0,sizeof(gdth_cmd_str));
1485
1486 /* make command */
1487 for (retries = INIT_RETRIES;;) {
1488 cmd_ptr->Service = service;
1489 cmd_ptr->RequestBuffer = INTERNAL_CMND;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001490 if (!(index=gdth_get_cmd_index(ha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 TRACE(("GDT: No free command index found\n"));
1492 return 0;
1493 }
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001494 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 cmd_ptr->OpCode = opcode;
1496 cmd_ptr->BoardNode = LOCALBOARD;
1497 if (service == CACHESERVICE) {
1498 if (opcode == GDT_IOCTL) {
1499 cmd_ptr->u.ioctl.subfunc = p1;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001500 cmd_ptr->u.ioctl.channel = (u32)p2;
1501 cmd_ptr->u.ioctl.param_size = (u16)p3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 cmd_ptr->u.ioctl.p_param = ha->scratch_phys;
1503 } else {
1504 if (ha->cache_feat & GDT_64BIT) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001505 cmd_ptr->u.cache64.DeviceNo = (u16)p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 cmd_ptr->u.cache64.BlockNo = p2;
1507 } else {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001508 cmd_ptr->u.cache.DeviceNo = (u16)p1;
1509 cmd_ptr->u.cache.BlockNo = (u32)p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 }
1511 }
1512 } else if (service == SCSIRAWSERVICE) {
1513 if (ha->raw_feat & GDT_64BIT) {
1514 cmd_ptr->u.raw64.direction = p1;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001515 cmd_ptr->u.raw64.bus = (u8)p2;
1516 cmd_ptr->u.raw64.target = (u8)p3;
1517 cmd_ptr->u.raw64.lun = (u8)(p3 >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 } else {
1519 cmd_ptr->u.raw.direction = p1;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001520 cmd_ptr->u.raw.bus = (u8)p2;
1521 cmd_ptr->u.raw.target = (u8)p3;
1522 cmd_ptr->u.raw.lun = (u8)(p3 >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
1524 } else if (service == SCREENSERVICE) {
1525 if (opcode == GDT_REALTIME) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001526 *(u32 *)&cmd_ptr->u.screen.su.data[0] = p1;
1527 *(u32 *)&cmd_ptr->u.screen.su.data[4] = (u32)p2;
1528 *(u32 *)&cmd_ptr->u.screen.su.data[8] = (u32)p3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
1530 }
1531 ha->cmd_len = sizeof(gdth_cmd_str);
1532 ha->cmd_offs_dpmem = 0;
1533 ha->cmd_cnt = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001534 gdth_copy_command(ha);
1535 gdth_release_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 gdth_delay(20);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001537 if (!gdth_wait(ha, index, INIT_TIMEOUT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 printk("GDT: Initialization error (timeout service %d)\n",service);
1539 return 0;
1540 }
1541 if (ha->status != S_BSY || --retries == 0)
1542 break;
1543 gdth_delay(1);
1544 }
1545
1546 return (ha->status != S_OK ? 0:1);
1547}
1548
1549
1550/* search for devices */
1551
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08001552static int gdth_search_drives(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001554 u16 cdev_cnt, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 int ok;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001556 u32 bus_no, drv_cnt, drv_no, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 gdth_getch_str *chn;
1558 gdth_drlist_str *drl;
1559 gdth_iochan_str *ioc;
1560 gdth_raw_iochan_str *iocr;
1561 gdth_arcdl_str *alst;
1562 gdth_alist_str *alst2;
1563 gdth_oem_str_ioctl *oemstr;
1564#ifdef INT_COAL
1565 gdth_perf_modes *pmod;
1566#endif
1567
1568#ifdef GDTH_RTC
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001569 u8 rtc[12];
1570 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571#endif
1572
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001573 TRACE(("gdth_search_drives() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 ok = 0;
1575
1576 /* initialize controller services, at first: screen service */
1577 ha->screen_feat = 0;
1578 if (!force_dma32) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001579 ok = gdth_internal_cmd(ha, SCREENSERVICE, GDT_X_INIT_SCR, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 if (ok)
1581 ha->screen_feat = GDT_64BIT;
1582 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001583 if (force_dma32 || (!ok && ha->status == (u16)S_NOFUNC))
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001584 ok = gdth_internal_cmd(ha, SCREENSERVICE, GDT_INIT, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 if (!ok) {
1586 printk("GDT-HA %d: Initialization error screen service (code %d)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001587 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 return 0;
1589 }
1590 TRACE2(("gdth_search_drives(): SCREENSERVICE initialized\n"));
1591
1592#ifdef GDTH_RTC
1593 /* read realtime clock info, send to controller */
1594 /* 1. wait for the falling edge of update flag */
1595 spin_lock_irqsave(&rtc_lock, flags);
1596 for (j = 0; j < 1000000; ++j)
1597 if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
1598 break;
1599 for (j = 0; j < 1000000; ++j)
1600 if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
1601 break;
1602 /* 2. read info */
1603 do {
1604 for (j = 0; j < 12; ++j)
1605 rtc[j] = CMOS_READ(j);
1606 } while (rtc[0] != CMOS_READ(0));
Robert P. J. Day7a960b72007-05-23 14:41:40 -07001607 spin_unlock_irqrestore(&rtc_lock, flags);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001608 TRACE2(("gdth_search_drives(): RTC: %x/%x/%x\n",*(u32 *)&rtc[0],
1609 *(u32 *)&rtc[4], *(u32 *)&rtc[8]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 /* 3. send to controller firmware */
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001611 gdth_internal_cmd(ha, SCREENSERVICE, GDT_REALTIME, *(u32 *)&rtc[0],
1612 *(u32 *)&rtc[4], *(u32 *)&rtc[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613#endif
1614
1615 /* unfreeze all IOs */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001616 gdth_internal_cmd(ha, CACHESERVICE, GDT_UNFREEZE_IO, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618 /* initialize cache service */
1619 ha->cache_feat = 0;
1620 if (!force_dma32) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001621 ok = gdth_internal_cmd(ha, CACHESERVICE, GDT_X_INIT_HOST, LINUX_OS,
1622 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 if (ok)
1624 ha->cache_feat = GDT_64BIT;
1625 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001626 if (force_dma32 || (!ok && ha->status == (u16)S_NOFUNC))
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001627 ok = gdth_internal_cmd(ha, CACHESERVICE, GDT_INIT, LINUX_OS, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 if (!ok) {
1629 printk("GDT-HA %d: Initialization error cache service (code %d)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001630 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 return 0;
1632 }
1633 TRACE2(("gdth_search_drives(): CACHESERVICE initialized\n"));
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001634 cdev_cnt = (u16)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 ha->fw_vers = ha->service;
1636
1637#ifdef INT_COAL
1638 if (ha->type == GDT_PCIMPR) {
1639 /* set perf. modes */
1640 pmod = (gdth_perf_modes *)ha->pscratch;
1641 pmod->version = 1;
1642 pmod->st_mode = 1; /* enable one status buffer */
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001643 *((u64 *)&pmod->st_buff_addr1) = ha->coal_stat_phys;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 pmod->st_buff_indx1 = COALINDEX;
1645 pmod->st_buff_addr2 = 0;
1646 pmod->st_buff_u_addr2 = 0;
1647 pmod->st_buff_indx2 = 0;
1648 pmod->st_buff_size = sizeof(gdth_coal_status) * MAXOFFSETS;
1649 pmod->cmd_mode = 0; // disable all cmd buffers
1650 pmod->cmd_buff_addr1 = 0;
1651 pmod->cmd_buff_u_addr1 = 0;
1652 pmod->cmd_buff_indx1 = 0;
1653 pmod->cmd_buff_addr2 = 0;
1654 pmod->cmd_buff_u_addr2 = 0;
1655 pmod->cmd_buff_indx2 = 0;
1656 pmod->cmd_buff_size = 0;
1657 pmod->reserved1 = 0;
1658 pmod->reserved2 = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001659 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, SET_PERF_MODES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 INVALID_CHANNEL,sizeof(gdth_perf_modes))) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001661 printk("GDT-HA %d: Interrupt coalescing activated\n", ha->hanum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 }
1663 }
1664#endif
1665
1666 /* detect number of buses - try new IOCTL */
1667 iocr = (gdth_raw_iochan_str *)ha->pscratch;
1668 iocr->hdr.version = 0xffffffff;
1669 iocr->hdr.list_entries = MAXBUS;
1670 iocr->hdr.first_chan = 0;
1671 iocr->hdr.last_chan = MAXBUS-1;
1672 iocr->hdr.list_offset = GDTOFFSOF(gdth_raw_iochan_str, list[0]);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001673 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, IOCHAN_RAW_DESC,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 INVALID_CHANNEL,sizeof(gdth_raw_iochan_str))) {
1675 TRACE2(("IOCHAN_RAW_DESC supported!\n"));
1676 ha->bus_cnt = iocr->hdr.chan_count;
1677 for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
1678 if (iocr->list[bus_no].proc_id < MAXID)
1679 ha->bus_id[bus_no] = iocr->list[bus_no].proc_id;
1680 else
1681 ha->bus_id[bus_no] = 0xff;
1682 }
1683 } else {
1684 /* old method */
1685 chn = (gdth_getch_str *)ha->pscratch;
1686 for (bus_no = 0; bus_no < MAXBUS; ++bus_no) {
1687 chn->channel_no = bus_no;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001688 if (!gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 SCSI_CHAN_CNT | L_CTRL_PATTERN,
1690 IO_CHANNEL | INVALID_CHANNEL,
1691 sizeof(gdth_getch_str))) {
1692 if (bus_no == 0) {
1693 printk("GDT-HA %d: Error detecting channel count (0x%x)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001694 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 return 0;
1696 }
1697 break;
1698 }
1699 if (chn->siop_id < MAXID)
1700 ha->bus_id[bus_no] = chn->siop_id;
1701 else
1702 ha->bus_id[bus_no] = 0xff;
1703 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001704 ha->bus_cnt = (u8)bus_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
1706 TRACE2(("gdth_search_drives() %d channels\n",ha->bus_cnt));
1707
1708 /* read cache configuration */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001709 if (!gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, CACHE_INFO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 INVALID_CHANNEL,sizeof(gdth_cinfo_str))) {
1711 printk("GDT-HA %d: Initialization error cache service (code %d)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001712 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 return 0;
1714 }
1715 ha->cpar = ((gdth_cinfo_str *)ha->pscratch)->cpar;
1716 TRACE2(("gdth_search_drives() cinfo: vs %x sta %d str %d dw %d b %d\n",
1717 ha->cpar.version,ha->cpar.state,ha->cpar.strategy,
1718 ha->cpar.write_back,ha->cpar.block_size));
1719
1720 /* read board info and features */
1721 ha->more_proc = FALSE;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001722 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, BOARD_INFO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 INVALID_CHANNEL,sizeof(gdth_binfo_str))) {
1724 memcpy(&ha->binfo, (gdth_binfo_str *)ha->pscratch,
1725 sizeof(gdth_binfo_str));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001726 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, BOARD_FEATURES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 INVALID_CHANNEL,sizeof(gdth_bfeat_str))) {
1728 TRACE2(("BOARD_INFO/BOARD_FEATURES supported\n"));
1729 ha->bfeat = *(gdth_bfeat_str *)ha->pscratch;
1730 ha->more_proc = TRUE;
1731 }
1732 } else {
1733 TRACE2(("BOARD_INFO requires firmware >= 1.10/2.08\n"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001734 strcpy(ha->binfo.type_string, gdth_ctr_name(ha));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 }
1736 TRACE2(("Controller name: %s\n",ha->binfo.type_string));
1737
1738 /* read more informations */
1739 if (ha->more_proc) {
1740 /* physical drives, channel addresses */
1741 ioc = (gdth_iochan_str *)ha->pscratch;
1742 ioc->hdr.version = 0xffffffff;
1743 ioc->hdr.list_entries = MAXBUS;
1744 ioc->hdr.first_chan = 0;
1745 ioc->hdr.last_chan = MAXBUS-1;
1746 ioc->hdr.list_offset = GDTOFFSOF(gdth_iochan_str, list[0]);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001747 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, IOCHAN_DESC,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 INVALID_CHANNEL,sizeof(gdth_iochan_str))) {
1749 for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
1750 ha->raw[bus_no].address = ioc->list[bus_no].address;
1751 ha->raw[bus_no].local_no = ioc->list[bus_no].local_no;
1752 }
1753 } else {
1754 for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
1755 ha->raw[bus_no].address = IO_CHANNEL;
1756 ha->raw[bus_no].local_no = bus_no;
1757 }
1758 }
1759 for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
1760 chn = (gdth_getch_str *)ha->pscratch;
1761 chn->channel_no = ha->raw[bus_no].local_no;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001762 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 SCSI_CHAN_CNT | L_CTRL_PATTERN,
1764 ha->raw[bus_no].address | INVALID_CHANNEL,
1765 sizeof(gdth_getch_str))) {
1766 ha->raw[bus_no].pdev_cnt = chn->drive_cnt;
1767 TRACE2(("Channel %d: %d phys. drives\n",
1768 bus_no,chn->drive_cnt));
1769 }
1770 if (ha->raw[bus_no].pdev_cnt > 0) {
1771 drl = (gdth_drlist_str *)ha->pscratch;
1772 drl->sc_no = ha->raw[bus_no].local_no;
1773 drl->sc_cnt = ha->raw[bus_no].pdev_cnt;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001774 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 SCSI_DR_LIST | L_CTRL_PATTERN,
1776 ha->raw[bus_no].address | INVALID_CHANNEL,
1777 sizeof(gdth_drlist_str))) {
1778 for (j = 0; j < ha->raw[bus_no].pdev_cnt; ++j)
1779 ha->raw[bus_no].id_list[j] = drl->sc_list[j];
1780 } else {
1781 ha->raw[bus_no].pdev_cnt = 0;
1782 }
1783 }
1784 }
1785
1786 /* logical drives */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001787 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, CACHE_DRV_CNT,
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001788 INVALID_CHANNEL,sizeof(u32))) {
1789 drv_cnt = *(u32 *)ha->pscratch;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001790 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, CACHE_DRV_LIST,
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001791 INVALID_CHANNEL,drv_cnt * sizeof(u32))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 for (j = 0; j < drv_cnt; ++j) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001793 drv_no = ((u32 *)ha->pscratch)[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 if (drv_no < MAX_LDRIVES) {
1795 ha->hdr[drv_no].is_logdrv = TRUE;
1796 TRACE2(("Drive %d is log. drive\n",drv_no));
1797 }
1798 }
1799 }
1800 alst = (gdth_arcdl_str *)ha->pscratch;
1801 alst->entries_avail = MAX_LDRIVES;
1802 alst->first_entry = 0;
1803 alst->list_offset = GDTOFFSOF(gdth_arcdl_str, list[0]);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001804 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 ARRAY_DRV_LIST2 | LA_CTRL_PATTERN,
1806 INVALID_CHANNEL, sizeof(gdth_arcdl_str) +
1807 (alst->entries_avail-1) * sizeof(gdth_alist_str))) {
1808 for (j = 0; j < alst->entries_init; ++j) {
1809 ha->hdr[j].is_arraydrv = alst->list[j].is_arrayd;
1810 ha->hdr[j].is_master = alst->list[j].is_master;
1811 ha->hdr[j].is_parity = alst->list[j].is_parity;
1812 ha->hdr[j].is_hotfix = alst->list[j].is_hotfix;
1813 ha->hdr[j].master_no = alst->list[j].cd_handle;
1814 }
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001815 } else if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 ARRAY_DRV_LIST | LA_CTRL_PATTERN,
1817 0, 35 * sizeof(gdth_alist_str))) {
1818 for (j = 0; j < 35; ++j) {
1819 alst2 = &((gdth_alist_str *)ha->pscratch)[j];
1820 ha->hdr[j].is_arraydrv = alst2->is_arrayd;
1821 ha->hdr[j].is_master = alst2->is_master;
1822 ha->hdr[j].is_parity = alst2->is_parity;
1823 ha->hdr[j].is_hotfix = alst2->is_hotfix;
1824 ha->hdr[j].master_no = alst2->cd_handle;
1825 }
1826 }
1827 }
1828 }
1829
1830 /* initialize raw service */
1831 ha->raw_feat = 0;
1832 if (!force_dma32) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001833 ok = gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_X_INIT_RAW, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 if (ok)
1835 ha->raw_feat = GDT_64BIT;
1836 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001837 if (force_dma32 || (!ok && ha->status == (u16)S_NOFUNC))
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001838 ok = gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_INIT, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 if (!ok) {
1840 printk("GDT-HA %d: Initialization error raw service (code %d)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001841 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 return 0;
1843 }
1844 TRACE2(("gdth_search_drives(): RAWSERVICE initialized\n"));
1845
1846 /* set/get features raw service (scatter/gather) */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001847 if (gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_SET_FEAT, SCATTER_GATHER,
1848 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 TRACE2(("gdth_search_drives(): set features RAWSERVICE OK\n"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001850 if (gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_GET_FEAT, 0, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 TRACE2(("gdth_search_dr(): get feat RAWSERVICE %d\n",
1852 ha->info));
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001853 ha->raw_feat |= (u16)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 }
1855 }
1856
1857 /* set/get features cache service (equal to raw service) */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001858 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_SET_FEAT, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 SCATTER_GATHER,0)) {
1860 TRACE2(("gdth_search_drives(): set features CACHESERVICE OK\n"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001861 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_GET_FEAT, 0, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 TRACE2(("gdth_search_dr(): get feat CACHESERV. %d\n",
1863 ha->info));
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001864 ha->cache_feat |= (u16)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 }
1866 }
1867
1868 /* reserve drives for raw service */
1869 if (reserve_mode != 0) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001870 gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_RESERVE_ALL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 reserve_mode == 1 ? 1 : 3, 0, 0);
1872 TRACE2(("gdth_search_drives(): RESERVE_ALL code %d\n",
1873 ha->status));
1874 }
1875 for (i = 0; i < MAX_RES_ARGS; i += 4) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001876 if (reserve_list[i] == ha->hanum && reserve_list[i+1] < ha->bus_cnt &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 reserve_list[i+2] < ha->tid_cnt && reserve_list[i+3] < MAXLUN) {
1878 TRACE2(("gdth_search_drives(): reserve ha %d bus %d id %d lun %d\n",
1879 reserve_list[i], reserve_list[i+1],
1880 reserve_list[i+2], reserve_list[i+3]));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001881 if (!gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_RESERVE, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 reserve_list[i+1], reserve_list[i+2] |
1883 (reserve_list[i+3] << 8))) {
1884 printk("GDT-HA %d: Error raw service (RESERVE, code %d)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001885 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 }
1887 }
1888 }
1889
1890 /* Determine OEM string using IOCTL */
1891 oemstr = (gdth_oem_str_ioctl *)ha->pscratch;
1892 oemstr->params.ctl_version = 0x01;
1893 oemstr->params.buffer_size = sizeof(oemstr->text);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001894 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 CACHE_READ_OEM_STRING_RECORD,INVALID_CHANNEL,
1896 sizeof(gdth_oem_str_ioctl))) {
1897 TRACE2(("gdth_search_drives(): CACHE_READ_OEM_STRING_RECORD OK\n"));
1898 printk("GDT-HA %d: Vendor: %s Name: %s\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001899 ha->hanum, oemstr->text.oem_company_name, ha->binfo.type_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 /* Save the Host Drive inquiry data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 strlcpy(ha->oem_name,oemstr->text.scsi_host_drive_inquiry_vendor_id,
1902 sizeof(ha->oem_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 } else {
1904 /* Old method, based on PCI ID */
1905 TRACE2(("gdth_search_drives(): CACHE_READ_OEM_STRING_RECORD failed\n"));
1906 printk("GDT-HA %d: Name: %s\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001907 ha->hanum, ha->binfo.type_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 if (ha->oem_id == OEM_ID_INTEL)
1909 strlcpy(ha->oem_name,"Intel ", sizeof(ha->oem_name));
1910 else
1911 strlcpy(ha->oem_name,"ICP ", sizeof(ha->oem_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
1913
1914 /* scanning for host drives */
1915 for (i = 0; i < cdev_cnt; ++i)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001916 gdth_analyse_hdrive(ha, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 TRACE(("gdth_search_drives() OK\n"));
1919 return 1;
1920}
1921
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001922static int gdth_analyse_hdrive(gdth_ha_str *ha, u16 hdrive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001924 u32 drv_cyls;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 int drv_hds, drv_secs;
1926
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001927 TRACE(("gdth_analyse_hdrive() hanum %d drive %d\n", ha->hanum, hdrive));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 if (hdrive >= MAX_HDRIVES)
1929 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001931 if (!gdth_internal_cmd(ha, CACHESERVICE, GDT_INFO, hdrive, 0, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return 0;
1933 ha->hdr[hdrive].present = TRUE;
1934 ha->hdr[hdrive].size = ha->info;
1935
1936 /* evaluate mapping (sectors per head, heads per cylinder) */
1937 ha->hdr[hdrive].size &= ~SECS32;
1938 if (ha->info2 == 0) {
1939 gdth_eval_mapping(ha->hdr[hdrive].size,&drv_cyls,&drv_hds,&drv_secs);
1940 } else {
1941 drv_hds = ha->info2 & 0xff;
1942 drv_secs = (ha->info2 >> 8) & 0xff;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001943 drv_cyls = (u32)ha->hdr[hdrive].size / drv_hds / drv_secs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001945 ha->hdr[hdrive].heads = (u8)drv_hds;
1946 ha->hdr[hdrive].secs = (u8)drv_secs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 /* round size */
1948 ha->hdr[hdrive].size = drv_cyls * drv_hds * drv_secs;
1949
1950 if (ha->cache_feat & GDT_64BIT) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001951 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_X_INFO, hdrive, 0, 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 && ha->info2 != 0) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001953 ha->hdr[hdrive].size = ((u64)ha->info2 << 32) | ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
1955 }
1956 TRACE2(("gdth_search_dr() cdr. %d size %d hds %d scs %d\n",
1957 hdrive,ha->hdr[hdrive].size,drv_hds,drv_secs));
1958
1959 /* get informations about device */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001960 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_DEVTYPE, hdrive, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 TRACE2(("gdth_search_dr() cache drive %d devtype %d\n",
1962 hdrive,ha->info));
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001963 ha->hdr[hdrive].devtype = (u16)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
1965
1966 /* cluster info */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001967 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_CLUST_INFO, hdrive, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 TRACE2(("gdth_search_dr() cache drive %d cluster info %d\n",
1969 hdrive,ha->info));
1970 if (!shared_access)
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001971 ha->hdr[hdrive].cluster_type = (u8)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 }
1973
1974 /* R/W attributes */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001975 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_RW_ATTRIBS, hdrive, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 TRACE2(("gdth_search_dr() cache drive %d r/w attrib. %d\n",
1977 hdrive,ha->info));
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001978 ha->hdr[hdrive].rw_attribs = (u8)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 }
1980
1981 return 1;
1982}
1983
1984
1985/* command queueing/sending functions */
1986
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001987static void gdth_putq(gdth_ha_str *ha, Scsi_Cmnd *scp, u8 priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02001989 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 register Scsi_Cmnd *pscp;
1991 register Scsi_Cmnd *nscp;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001992 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
1994 TRACE(("gdth_putq() priority %d\n",priority));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 spin_lock_irqsave(&ha->smp_lock, flags);
1996
Jens Axboe242f9dc2008-09-14 05:55:09 -07001997 if (!cmndinfo->internal_command)
Boaz Harroshf842b642007-10-02 23:16:01 +02001998 cmndinfo->priority = priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000 if (ha->req_first==NULL) {
2001 ha->req_first = scp; /* queue was empty */
2002 scp->SCp.ptr = NULL;
2003 } else { /* queue not empty */
2004 pscp = ha->req_first;
2005 nscp = (Scsi_Cmnd *)pscp->SCp.ptr;
2006 /* priority: 0-highest,..,0xff-lowest */
Boaz Harroshf842b642007-10-02 23:16:01 +02002007 while (nscp && gdth_cmnd_priv(nscp)->priority <= priority) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 pscp = nscp;
2009 nscp = (Scsi_Cmnd *)pscp->SCp.ptr;
2010 }
2011 pscp->SCp.ptr = (char *)scp;
2012 scp->SCp.ptr = (char *)nscp;
2013 }
2014 spin_unlock_irqrestore(&ha->smp_lock, flags);
2015
2016#ifdef GDTH_STATISTICS
2017 flags = 0;
2018 for (nscp=ha->req_first; nscp; nscp=(Scsi_Cmnd*)nscp->SCp.ptr)
2019 ++flags;
2020 if (max_rq < flags) {
2021 max_rq = flags;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002022 TRACE3(("GDT: max_rq = %d\n",(u16)max_rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 }
2024#endif
2025}
2026
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002027static void gdth_next(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 register Scsi_Cmnd *pscp;
2030 register Scsi_Cmnd *nscp;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002031 u8 b, t, l, firsttime;
2032 u8 this_cmd, next_cmd;
2033 unsigned long flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 int cmd_index;
2035
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002036 TRACE(("gdth_next() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 if (!gdth_polling)
2038 spin_lock_irqsave(&ha->smp_lock, flags);
2039
2040 ha->cmd_cnt = ha->cmd_offs_dpmem = 0;
2041 this_cmd = firsttime = TRUE;
2042 next_cmd = gdth_polling ? FALSE:TRUE;
2043 cmd_index = 0;
2044
2045 for (nscp = pscp = ha->req_first; nscp; nscp = (Scsi_Cmnd *)nscp->SCp.ptr) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002046 struct gdth_cmndinfo *nscp_cmndinfo = gdth_cmnd_priv(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 if (nscp != pscp && nscp != (Scsi_Cmnd *)pscp->SCp.ptr)
2048 pscp = (Scsi_Cmnd *)pscp->SCp.ptr;
Boaz Harroshf842b642007-10-02 23:16:01 +02002049 if (!nscp_cmndinfo->internal_command) {
Christoph Hellwig52759e62007-10-02 22:59:53 +02002050 b = nscp->device->channel;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07002051 t = nscp->device->id;
2052 l = nscp->device->lun;
Boaz Harroshf842b642007-10-02 23:16:01 +02002053 if (nscp_cmndinfo->priority >= DEFAULT_PRI) {
Leubner, Achimcbd5f692006-06-09 11:34:29 -07002054 if ((b != ha->virt_bus && ha->raw[BUS_L2P(ha,b)].lock) ||
2055 (b == ha->virt_bus && t < MAX_HDRIVES && ha->hdr[t].lock))
2056 continue;
2057 }
2058 } else
2059 b = t = l = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 if (firsttime) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002062 if (gdth_test_busy(ha)) { /* controller busy ? */
2063 TRACE(("gdth_next() controller %d busy !\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 if (!gdth_polling) {
2065 spin_unlock_irqrestore(&ha->smp_lock, flags);
2066 return;
2067 }
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002068 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 gdth_delay(1);
2070 }
2071 firsttime = FALSE;
2072 }
2073
Boaz Harroshf842b642007-10-02 23:16:01 +02002074 if (!nscp_cmndinfo->internal_command) {
2075 if (nscp_cmndinfo->phase == -1) {
2076 nscp_cmndinfo->phase = CACHESERVICE; /* default: cache svc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 if (nscp->cmnd[0] == TEST_UNIT_READY) {
2078 TRACE2(("TEST_UNIT_READY Bus %d Id %d LUN %d\n",
2079 b, t, l));
2080 /* TEST_UNIT_READY -> set scan mode */
2081 if ((ha->scan_mode & 0x0f) == 0) {
2082 if (b == 0 && t == 0 && l == 0) {
2083 ha->scan_mode |= 1;
2084 TRACE2(("Scan mode: 0x%x\n", ha->scan_mode));
2085 }
2086 } else if ((ha->scan_mode & 0x0f) == 1) {
2087 if (b == 0 && ((t == 0 && l == 1) ||
2088 (t == 1 && l == 0))) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002089 nscp_cmndinfo->OpCode = GDT_SCAN_START;
2090 nscp_cmndinfo->phase = ((ha->scan_mode & 0x10 ? 1:0) << 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 | SCSIRAWSERVICE;
2092 ha->scan_mode = 0x12;
2093 TRACE2(("Scan mode: 0x%x (SCAN_START)\n",
2094 ha->scan_mode));
2095 } else {
2096 ha->scan_mode &= 0x10;
2097 TRACE2(("Scan mode: 0x%x\n", ha->scan_mode));
2098 }
2099 } else if (ha->scan_mode == 0x12) {
2100 if (b == ha->bus_cnt && t == ha->tid_cnt-1) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002101 nscp_cmndinfo->phase = SCSIRAWSERVICE;
2102 nscp_cmndinfo->OpCode = GDT_SCAN_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 ha->scan_mode &= 0x10;
2104 TRACE2(("Scan mode: 0x%x (SCAN_END)\n",
2105 ha->scan_mode));
2106 }
2107 }
2108 }
2109 if (b == ha->virt_bus && nscp->cmnd[0] != INQUIRY &&
2110 nscp->cmnd[0] != READ_CAPACITY && nscp->cmnd[0] != MODE_SENSE &&
2111 (ha->hdr[t].cluster_type & CLUSTER_DRIVE)) {
2112 /* always GDT_CLUST_INFO! */
Boaz Harroshf842b642007-10-02 23:16:01 +02002113 nscp_cmndinfo->OpCode = GDT_CLUST_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 }
2115 }
2116 }
2117
Boaz Harroshf842b642007-10-02 23:16:01 +02002118 if (nscp_cmndinfo->OpCode != -1) {
2119 if ((nscp_cmndinfo->phase & 0xff) == CACHESERVICE) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002120 if (!(cmd_index=gdth_fill_cache_cmd(ha, nscp, t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 this_cmd = FALSE;
2122 next_cmd = FALSE;
Boaz Harroshf842b642007-10-02 23:16:01 +02002123 } else if ((nscp_cmndinfo->phase & 0xff) == SCSIRAWSERVICE) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002124 if (!(cmd_index=gdth_fill_raw_cmd(ha, nscp, BUS_L2P(ha, b))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 this_cmd = FALSE;
2126 next_cmd = FALSE;
2127 } else {
2128 memset((char*)nscp->sense_buffer,0,16);
2129 nscp->sense_buffer[0] = 0x70;
2130 nscp->sense_buffer[2] = NOT_READY;
2131 nscp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
Boaz Harroshf842b642007-10-02 23:16:01 +02002132 if (!nscp_cmndinfo->wait_for_completion)
2133 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002135 gdth_scsi_done(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 }
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02002137 } else if (gdth_cmnd_priv(nscp)->internal_command) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002138 if (!(cmd_index=gdth_special_cmd(ha, nscp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 this_cmd = FALSE;
2140 next_cmd = FALSE;
2141 } else if (b != ha->virt_bus) {
2142 if (ha->raw[BUS_L2P(ha,b)].io_cnt[t] >= GDTH_MAX_RAW ||
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002143 !(cmd_index=gdth_fill_raw_cmd(ha, nscp, BUS_L2P(ha, b))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 this_cmd = FALSE;
2145 else
2146 ha->raw[BUS_L2P(ha,b)].io_cnt[t]++;
2147 } else if (t >= MAX_HDRIVES || !ha->hdr[t].present || l != 0) {
2148 TRACE2(("Command 0x%x to bus %d id %d lun %d -> IGNORE\n",
2149 nscp->cmnd[0], b, t, l));
2150 nscp->result = DID_BAD_TARGET << 16;
Boaz Harroshf842b642007-10-02 23:16:01 +02002151 if (!nscp_cmndinfo->wait_for_completion)
2152 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002154 gdth_scsi_done(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 } else {
2156 switch (nscp->cmnd[0]) {
2157 case TEST_UNIT_READY:
2158 case INQUIRY:
2159 case REQUEST_SENSE:
2160 case READ_CAPACITY:
2161 case VERIFY:
2162 case START_STOP:
2163 case MODE_SENSE:
2164 case SERVICE_ACTION_IN:
2165 TRACE(("cache cmd %x/%x/%x/%x/%x/%x\n",nscp->cmnd[0],
2166 nscp->cmnd[1],nscp->cmnd[2],nscp->cmnd[3],
2167 nscp->cmnd[4],nscp->cmnd[5]));
2168 if (ha->hdr[t].media_changed && nscp->cmnd[0] != INQUIRY) {
2169 /* return UNIT_ATTENTION */
2170 TRACE2(("cmd 0x%x target %d: UNIT_ATTENTION\n",
2171 nscp->cmnd[0], t));
2172 ha->hdr[t].media_changed = FALSE;
2173 memset((char*)nscp->sense_buffer,0,16);
2174 nscp->sense_buffer[0] = 0x70;
2175 nscp->sense_buffer[2] = UNIT_ATTENTION;
2176 nscp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
Boaz Harroshf842b642007-10-02 23:16:01 +02002177 if (!nscp_cmndinfo->wait_for_completion)
2178 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002180 gdth_scsi_done(nscp);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002181 } else if (gdth_internal_cache_cmd(ha, nscp))
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002182 gdth_scsi_done(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 break;
2184
2185 case ALLOW_MEDIUM_REMOVAL:
2186 TRACE(("cache cmd %x/%x/%x/%x/%x/%x\n",nscp->cmnd[0],
2187 nscp->cmnd[1],nscp->cmnd[2],nscp->cmnd[3],
2188 nscp->cmnd[4],nscp->cmnd[5]));
2189 if ( (nscp->cmnd[4]&1) && !(ha->hdr[t].devtype&1) ) {
2190 TRACE(("Prevent r. nonremov. drive->do nothing\n"));
2191 nscp->result = DID_OK << 16;
2192 nscp->sense_buffer[0] = 0;
Boaz Harroshf842b642007-10-02 23:16:01 +02002193 if (!nscp_cmndinfo->wait_for_completion)
2194 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002196 gdth_scsi_done(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 } else {
2198 nscp->cmnd[3] = (ha->hdr[t].devtype&1) ? 1:0;
2199 TRACE(("Prevent/allow r. %d rem. drive %d\n",
2200 nscp->cmnd[4],nscp->cmnd[3]));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002201 if (!(cmd_index=gdth_fill_cache_cmd(ha, nscp, t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 this_cmd = FALSE;
2203 }
2204 break;
2205
2206 case RESERVE:
2207 case RELEASE:
2208 TRACE2(("cache cmd %s\n",nscp->cmnd[0] == RESERVE ?
2209 "RESERVE" : "RELEASE"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002210 if (!(cmd_index=gdth_fill_cache_cmd(ha, nscp, t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 this_cmd = FALSE;
2212 break;
2213
2214 case READ_6:
2215 case WRITE_6:
2216 case READ_10:
2217 case WRITE_10:
2218 case READ_16:
2219 case WRITE_16:
2220 if (ha->hdr[t].media_changed) {
2221 /* return UNIT_ATTENTION */
2222 TRACE2(("cmd 0x%x target %d: UNIT_ATTENTION\n",
2223 nscp->cmnd[0], t));
2224 ha->hdr[t].media_changed = FALSE;
2225 memset((char*)nscp->sense_buffer,0,16);
2226 nscp->sense_buffer[0] = 0x70;
2227 nscp->sense_buffer[2] = UNIT_ATTENTION;
2228 nscp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
Boaz Harroshf842b642007-10-02 23:16:01 +02002229 if (!nscp_cmndinfo->wait_for_completion)
2230 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002232 gdth_scsi_done(nscp);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002233 } else if (!(cmd_index=gdth_fill_cache_cmd(ha, nscp, t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 this_cmd = FALSE;
2235 break;
2236
2237 default:
2238 TRACE2(("cache cmd %x/%x/%x/%x/%x/%x unknown\n",nscp->cmnd[0],
2239 nscp->cmnd[1],nscp->cmnd[2],nscp->cmnd[3],
2240 nscp->cmnd[4],nscp->cmnd[5]));
2241 printk("GDT-HA %d: Unknown SCSI command 0x%x to cache service !\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002242 ha->hanum, nscp->cmnd[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 nscp->result = DID_ABORT << 16;
Boaz Harroshf842b642007-10-02 23:16:01 +02002244 if (!nscp_cmndinfo->wait_for_completion)
2245 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002247 gdth_scsi_done(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 break;
2249 }
2250 }
2251
2252 if (!this_cmd)
2253 break;
2254 if (nscp == ha->req_first)
2255 ha->req_first = pscp = (Scsi_Cmnd *)nscp->SCp.ptr;
2256 else
2257 pscp->SCp.ptr = nscp->SCp.ptr;
2258 if (!next_cmd)
2259 break;
2260 }
2261
2262 if (ha->cmd_cnt > 0) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002263 gdth_release_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 }
2265
2266 if (!gdth_polling)
2267 spin_unlock_irqrestore(&ha->smp_lock, flags);
2268
2269 if (gdth_polling && ha->cmd_cnt > 0) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002270 if (!gdth_wait(ha, cmd_index, POLL_TIMEOUT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 printk("GDT-HA %d: Command %d timed out !\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002272 ha->hanum, cmd_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
2274}
Boaz Harrosh3892d882007-10-02 23:18:03 +02002275
2276/*
2277 * gdth_copy_internal_data() - copy to/from a buffer onto a scsi_cmnd's
2278 * buffers, kmap_atomic() as needed.
2279 */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002280static void gdth_copy_internal_data(gdth_ha_str *ha, Scsi_Cmnd *scp,
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002281 char *buffer, u16 count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002283 u16 cpcount,i, max_sg = scsi_sg_count(scp);
2284 u16 cpsum,cpnow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 struct scatterlist *sl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 char *address;
2287
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002288 cpcount = min_t(u16, count, scsi_bufflen(scp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Boaz Harrosh3892d882007-10-02 23:18:03 +02002290 if (cpcount) {
2291 cpsum=0;
2292 scsi_for_each_sg(scp, sl, max_sg, i) {
Leubner, Achimcbd5f692006-06-09 11:34:29 -07002293 unsigned long flags;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002294 cpnow = (u16)sl->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 TRACE(("copy_internal() now %d sum %d count %d %d\n",
Boaz Harroshd35055a2008-02-27 15:31:20 -08002296 cpnow, cpsum, cpcount, scsi_bufflen(scp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 if (cpsum+cpnow > cpcount)
2298 cpnow = cpcount - cpsum;
2299 cpsum += cpnow;
Jens Axboe45711f12007-10-22 21:19:53 +02002300 if (!sg_page(sl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 printk("GDT-HA %d: invalid sc/gt element in gdth_copy_internal_data()\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002302 ha->hanum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 return;
2304 }
Leubner, Achimcbd5f692006-06-09 11:34:29 -07002305 local_irq_save(flags);
Cong Wang77dfce02011-11-25 23:14:23 +08002306 address = kmap_atomic(sg_page(sl)) + sl->offset;
Boaz Harroshee54cc62008-02-27 15:29:15 -08002307 memcpy(address, buffer, cpnow);
Jens Axboe45711f12007-10-22 21:19:53 +02002308 flush_dcache_page(sg_page(sl));
Cong Wang77dfce02011-11-25 23:14:23 +08002309 kunmap_atomic(address);
Leubner, Achimcbd5f692006-06-09 11:34:29 -07002310 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 if (cpsum == cpcount)
2312 break;
2313 buffer += cpnow;
2314 }
Boaz Harrosh3892d882007-10-02 23:18:03 +02002315 } else if (count) {
2316 printk("GDT-HA %d: SCSI command with no buffers but data transfer expected!\n",
2317 ha->hanum);
2318 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 }
2320}
2321
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002322static int gdth_internal_cache_cmd(gdth_ha_str *ha, Scsi_Cmnd *scp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002324 u8 t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 gdth_inq_data inq;
2326 gdth_rdcap_data rdc;
2327 gdth_sense_data sd;
2328 gdth_modep_data mpd;
Boaz Harroshf842b642007-10-02 23:16:01 +02002329 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 t = scp->device->id;
2332 TRACE(("gdth_internal_cache_cmd() cmd 0x%x hdrive %d\n",
2333 scp->cmnd[0],t));
2334
2335 scp->result = DID_OK << 16;
2336 scp->sense_buffer[0] = 0;
2337
2338 switch (scp->cmnd[0]) {
2339 case TEST_UNIT_READY:
2340 case VERIFY:
2341 case START_STOP:
2342 TRACE2(("Test/Verify/Start hdrive %d\n",t));
2343 break;
2344
2345 case INQUIRY:
2346 TRACE2(("Inquiry hdrive %d devtype %d\n",
2347 t,ha->hdr[t].devtype));
2348 inq.type_qual = (ha->hdr[t].devtype&4) ? TYPE_ROM:TYPE_DISK;
2349 /* you can here set all disks to removable, if you want to do
2350 a flush using the ALLOW_MEDIUM_REMOVAL command */
2351 inq.modif_rmb = 0x00;
2352 if ((ha->hdr[t].devtype & 1) ||
2353 (ha->hdr[t].cluster_type & CLUSTER_DRIVE))
2354 inq.modif_rmb = 0x80;
2355 inq.version = 2;
2356 inq.resp_aenc = 2;
2357 inq.add_length= 32;
2358 strcpy(inq.vendor,ha->oem_name);
2359 sprintf(inq.product,"Host Drive #%02d",t);
2360 strcpy(inq.revision," ");
Boaz Harroshee54cc62008-02-27 15:29:15 -08002361 gdth_copy_internal_data(ha, scp, (char*)&inq, sizeof(gdth_inq_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 break;
2363
2364 case REQUEST_SENSE:
2365 TRACE2(("Request sense hdrive %d\n",t));
2366 sd.errorcode = 0x70;
2367 sd.segno = 0x00;
2368 sd.key = NO_SENSE;
2369 sd.info = 0;
2370 sd.add_length= 0;
Boaz Harroshee54cc62008-02-27 15:29:15 -08002371 gdth_copy_internal_data(ha, scp, (char*)&sd, sizeof(gdth_sense_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 break;
2373
2374 case MODE_SENSE:
2375 TRACE2(("Mode sense hdrive %d\n",t));
2376 memset((char*)&mpd,0,sizeof(gdth_modep_data));
2377 mpd.hd.data_length = sizeof(gdth_modep_data);
2378 mpd.hd.dev_par = (ha->hdr[t].devtype&2) ? 0x80:0;
2379 mpd.hd.bd_length = sizeof(mpd.bd);
2380 mpd.bd.block_length[0] = (SECTOR_SIZE & 0x00ff0000) >> 16;
2381 mpd.bd.block_length[1] = (SECTOR_SIZE & 0x0000ff00) >> 8;
2382 mpd.bd.block_length[2] = (SECTOR_SIZE & 0x000000ff);
Boaz Harroshee54cc62008-02-27 15:29:15 -08002383 gdth_copy_internal_data(ha, scp, (char*)&mpd, sizeof(gdth_modep_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 break;
2385
2386 case READ_CAPACITY:
2387 TRACE2(("Read capacity hdrive %d\n",t));
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002388 if (ha->hdr[t].size > (u64)0xffffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 rdc.last_block_no = 0xffffffff;
2390 else
2391 rdc.last_block_no = cpu_to_be32(ha->hdr[t].size-1);
2392 rdc.block_length = cpu_to_be32(SECTOR_SIZE);
Boaz Harroshee54cc62008-02-27 15:29:15 -08002393 gdth_copy_internal_data(ha, scp, (char*)&rdc, sizeof(gdth_rdcap_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 break;
2395
2396 case SERVICE_ACTION_IN:
2397 if ((scp->cmnd[1] & 0x1f) == SAI_READ_CAPACITY_16 &&
2398 (ha->cache_feat & GDT_64BIT)) {
2399 gdth_rdcap16_data rdc16;
2400
2401 TRACE2(("Read capacity (16) hdrive %d\n",t));
2402 rdc16.last_block_no = cpu_to_be64(ha->hdr[t].size-1);
2403 rdc16.block_length = cpu_to_be32(SECTOR_SIZE);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002404 gdth_copy_internal_data(ha, scp, (char*)&rdc16,
Boaz Harroshee54cc62008-02-27 15:29:15 -08002405 sizeof(gdth_rdcap16_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 } else {
2407 scp->result = DID_ABORT << 16;
2408 }
2409 break;
2410
2411 default:
2412 TRACE2(("Internal cache cmd 0x%x unknown\n",scp->cmnd[0]));
2413 break;
2414 }
2415
Boaz Harroshf842b642007-10-02 23:16:01 +02002416 if (!cmndinfo->wait_for_completion)
2417 cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 else
2419 return 1;
2420
2421 return 0;
2422}
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002423
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002424static int gdth_fill_cache_cmd(gdth_ha_str *ha, Scsi_Cmnd *scp, u16 hdrive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 register gdth_cmd_str *cmdp;
Boaz Harroshf842b642007-10-02 23:16:01 +02002427 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002428 u32 cnt, blockcnt;
2429 u64 no, blockno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 int i, cmd_index, read_write, sgcnt, mode64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 cmdp = ha->pccb;
2433 TRACE(("gdth_fill_cache_cmd() cmd 0x%x cmdsize %d hdrive %d\n",
2434 scp->cmnd[0],scp->cmd_len,hdrive));
2435
2436 if (ha->type==GDT_EISA && ha->cmd_cnt>0)
2437 return 0;
2438
2439 mode64 = (ha->cache_feat & GDT_64BIT) ? TRUE : FALSE;
2440 /* test for READ_16, WRITE_16 if !mode64 ? ---
2441 not required, should not occur due to error return on
2442 READ_CAPACITY_16 */
2443
2444 cmdp->Service = CACHESERVICE;
2445 cmdp->RequestBuffer = scp;
2446 /* search free command index */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002447 if (!(cmd_index=gdth_get_cmd_index(ha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 TRACE(("GDT: No free command index found\n"));
2449 return 0;
2450 }
2451 /* if it's the first command, set command semaphore */
2452 if (ha->cmd_cnt == 0)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002453 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
2455 /* fill command */
2456 read_write = 0;
Boaz Harroshf842b642007-10-02 23:16:01 +02002457 if (cmndinfo->OpCode != -1)
2458 cmdp->OpCode = cmndinfo->OpCode; /* special cache cmd. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 else if (scp->cmnd[0] == RESERVE)
2460 cmdp->OpCode = GDT_RESERVE_DRV;
2461 else if (scp->cmnd[0] == RELEASE)
2462 cmdp->OpCode = GDT_RELEASE_DRV;
2463 else if (scp->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
2464 if (scp->cmnd[4] & 1) /* prevent ? */
2465 cmdp->OpCode = GDT_MOUNT;
2466 else if (scp->cmnd[3] & 1) /* removable drive ? */
2467 cmdp->OpCode = GDT_UNMOUNT;
2468 else
2469 cmdp->OpCode = GDT_FLUSH;
2470 } else if (scp->cmnd[0] == WRITE_6 || scp->cmnd[0] == WRITE_10 ||
2471 scp->cmnd[0] == WRITE_12 || scp->cmnd[0] == WRITE_16
2472 ) {
2473 read_write = 1;
2474 if (gdth_write_through || ((ha->hdr[hdrive].rw_attribs & 1) &&
2475 (ha->cache_feat & GDT_WR_THROUGH)))
2476 cmdp->OpCode = GDT_WRITE_THR;
2477 else
2478 cmdp->OpCode = GDT_WRITE;
2479 } else {
2480 read_write = 2;
2481 cmdp->OpCode = GDT_READ;
2482 }
2483
2484 cmdp->BoardNode = LOCALBOARD;
2485 if (mode64) {
2486 cmdp->u.cache64.DeviceNo = hdrive;
2487 cmdp->u.cache64.BlockNo = 1;
2488 cmdp->u.cache64.sg_canz = 0;
2489 } else {
2490 cmdp->u.cache.DeviceNo = hdrive;
2491 cmdp->u.cache.BlockNo = 1;
2492 cmdp->u.cache.sg_canz = 0;
2493 }
2494
2495 if (read_write) {
2496 if (scp->cmd_len == 16) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002497 memcpy(&no, &scp->cmnd[2], sizeof(u64));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 blockno = be64_to_cpu(no);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002499 memcpy(&cnt, &scp->cmnd[10], sizeof(u32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 blockcnt = be32_to_cpu(cnt);
2501 } else if (scp->cmd_len == 10) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002502 memcpy(&no, &scp->cmnd[2], sizeof(u32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 blockno = be32_to_cpu(no);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002504 memcpy(&cnt, &scp->cmnd[7], sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 blockcnt = be16_to_cpu(cnt);
2506 } else {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002507 memcpy(&no, &scp->cmnd[0], sizeof(u32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 blockno = be32_to_cpu(no) & 0x001fffffUL;
2509 blockcnt= scp->cmnd[4]==0 ? 0x100 : scp->cmnd[4];
2510 }
2511 if (mode64) {
2512 cmdp->u.cache64.BlockNo = blockno;
2513 cmdp->u.cache64.BlockCnt = blockcnt;
2514 } else {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002515 cmdp->u.cache.BlockNo = (u32)blockno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 cmdp->u.cache.BlockCnt = blockcnt;
2517 }
2518
Boaz Harroshd35055a2008-02-27 15:31:20 -08002519 if (scsi_bufflen(scp)) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002520 cmndinfo->dma_dir = (read_write == 1 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
Boaz Harroshd35055a2008-02-27 15:31:20 -08002522 sgcnt = pci_map_sg(ha->pdev, scsi_sglist(scp), scsi_sg_count(scp),
Boaz Harrosh3892d882007-10-02 23:18:03 +02002523 cmndinfo->dma_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 if (mode64) {
Boaz Harrosh3892d882007-10-02 23:18:03 +02002525 struct scatterlist *sl;
2526
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002527 cmdp->u.cache64.DestAddr= (u64)-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 cmdp->u.cache64.sg_canz = sgcnt;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002529 scsi_for_each_sg(scp, sl, sgcnt, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 cmdp->u.cache64.sg_lst[i].sg_ptr = sg_dma_address(sl);
2531#ifdef GDTH_DMA_STATISTICS
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002532 if (cmdp->u.cache64.sg_lst[i].sg_ptr > (u64)0xffffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 ha->dma64_cnt++;
2534 else
2535 ha->dma32_cnt++;
2536#endif
2537 cmdp->u.cache64.sg_lst[i].sg_len = sg_dma_len(sl);
2538 }
2539 } else {
Boaz Harrosh3892d882007-10-02 23:18:03 +02002540 struct scatterlist *sl;
2541
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 cmdp->u.cache.DestAddr= 0xffffffff;
2543 cmdp->u.cache.sg_canz = sgcnt;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002544 scsi_for_each_sg(scp, sl, sgcnt, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 cmdp->u.cache.sg_lst[i].sg_ptr = sg_dma_address(sl);
2546#ifdef GDTH_DMA_STATISTICS
2547 ha->dma32_cnt++;
2548#endif
2549 cmdp->u.cache.sg_lst[i].sg_len = sg_dma_len(sl);
2550 }
2551 }
2552
2553#ifdef GDTH_STATISTICS
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002554 if (max_sg < (u32)sgcnt) {
2555 max_sg = (u32)sgcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 TRACE3(("GDT: max_sg = %d\n",max_sg));
2557 }
2558#endif
2559
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 }
2561 }
2562 /* evaluate command size, check space */
2563 if (mode64) {
2564 TRACE(("cache cmd: addr. %x sganz %x sgptr0 %x sglen0 %x\n",
2565 cmdp->u.cache64.DestAddr,cmdp->u.cache64.sg_canz,
2566 cmdp->u.cache64.sg_lst[0].sg_ptr,
2567 cmdp->u.cache64.sg_lst[0].sg_len));
2568 TRACE(("cache cmd: cmd %d blockno. %d, blockcnt %d\n",
2569 cmdp->OpCode,cmdp->u.cache64.BlockNo,cmdp->u.cache64.BlockCnt));
2570 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.cache64.sg_lst) +
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002571 (u16)cmdp->u.cache64.sg_canz * sizeof(gdth_sg64_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 } else {
2573 TRACE(("cache cmd: addr. %x sganz %x sgptr0 %x sglen0 %x\n",
2574 cmdp->u.cache.DestAddr,cmdp->u.cache.sg_canz,
2575 cmdp->u.cache.sg_lst[0].sg_ptr,
2576 cmdp->u.cache.sg_lst[0].sg_len));
2577 TRACE(("cache cmd: cmd %d blockno. %d, blockcnt %d\n",
2578 cmdp->OpCode,cmdp->u.cache.BlockNo,cmdp->u.cache.BlockCnt));
2579 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.cache.sg_lst) +
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002580 (u16)cmdp->u.cache.sg_canz * sizeof(gdth_sg_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 }
2582 if (ha->cmd_len & 3)
2583 ha->cmd_len += (4 - (ha->cmd_len & 3));
2584
2585 if (ha->cmd_cnt > 0) {
2586 if ((ha->cmd_offs_dpmem + ha->cmd_len + DPMEM_COMMAND_OFFSET) >
2587 ha->ic_all_size) {
2588 TRACE2(("gdth_fill_cache() DPMEM overflow\n"));
2589 ha->cmd_tab[cmd_index-2].cmnd = UNUSED_CMND;
2590 return 0;
2591 }
2592 }
2593
2594 /* copy command */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002595 gdth_copy_command(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 return cmd_index;
2597}
2598
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002599static int gdth_fill_raw_cmd(gdth_ha_str *ha, Scsi_Cmnd *scp, u8 b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 register gdth_cmd_str *cmdp;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002602 u16 i;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002603 dma_addr_t sense_paddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 int cmd_index, sgcnt, mode64;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002605 u8 t,l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 struct page *page;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002607 unsigned long offset;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02002608 struct gdth_cmndinfo *cmndinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 t = scp->device->id;
2611 l = scp->device->lun;
2612 cmdp = ha->pccb;
2613 TRACE(("gdth_fill_raw_cmd() cmd 0x%x bus %d ID %d LUN %d\n",
2614 scp->cmnd[0],b,t,l));
2615
2616 if (ha->type==GDT_EISA && ha->cmd_cnt>0)
2617 return 0;
2618
2619 mode64 = (ha->raw_feat & GDT_64BIT) ? TRUE : FALSE;
2620
2621 cmdp->Service = SCSIRAWSERVICE;
2622 cmdp->RequestBuffer = scp;
2623 /* search free command index */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002624 if (!(cmd_index=gdth_get_cmd_index(ha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 TRACE(("GDT: No free command index found\n"));
2626 return 0;
2627 }
2628 /* if it's the first command, set command semaphore */
2629 if (ha->cmd_cnt == 0)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002630 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02002632 cmndinfo = gdth_cmnd_priv(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 /* fill command */
Boaz Harroshf842b642007-10-02 23:16:01 +02002634 if (cmndinfo->OpCode != -1) {
2635 cmdp->OpCode = cmndinfo->OpCode; /* special raw cmd. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 cmdp->BoardNode = LOCALBOARD;
2637 if (mode64) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002638 cmdp->u.raw64.direction = (cmndinfo->phase >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 TRACE2(("special raw cmd 0x%x param 0x%x\n",
2640 cmdp->OpCode, cmdp->u.raw64.direction));
2641 /* evaluate command size */
2642 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw64.sg_lst);
2643 } else {
Boaz Harroshf842b642007-10-02 23:16:01 +02002644 cmdp->u.raw.direction = (cmndinfo->phase >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 TRACE2(("special raw cmd 0x%x param 0x%x\n",
2646 cmdp->OpCode, cmdp->u.raw.direction));
2647 /* evaluate command size */
2648 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw.sg_lst);
2649 }
2650
2651 } else {
2652 page = virt_to_page(scp->sense_buffer);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002653 offset = (unsigned long)scp->sense_buffer & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 sense_paddr = pci_map_page(ha->pdev,page,offset,
2655 16,PCI_DMA_FROMDEVICE);
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02002656
2657 cmndinfo->sense_paddr = sense_paddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 cmdp->OpCode = GDT_WRITE; /* always */
2659 cmdp->BoardNode = LOCALBOARD;
2660 if (mode64) {
2661 cmdp->u.raw64.reserved = 0;
2662 cmdp->u.raw64.mdisc_time = 0;
2663 cmdp->u.raw64.mcon_time = 0;
2664 cmdp->u.raw64.clen = scp->cmd_len;
2665 cmdp->u.raw64.target = t;
2666 cmdp->u.raw64.lun = l;
2667 cmdp->u.raw64.bus = b;
2668 cmdp->u.raw64.priority = 0;
Boaz Harroshd35055a2008-02-27 15:31:20 -08002669 cmdp->u.raw64.sdlen = scsi_bufflen(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 cmdp->u.raw64.sense_len = 16;
2671 cmdp->u.raw64.sense_data = sense_paddr;
2672 cmdp->u.raw64.direction =
2673 gdth_direction_tab[scp->cmnd[0]]==DOU ? GDTH_DATA_OUT:GDTH_DATA_IN;
2674 memcpy(cmdp->u.raw64.cmd,scp->cmnd,16);
Joerg Dorchainbb9ba312007-03-06 02:46:54 -08002675 cmdp->u.raw64.sg_ranz = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 } else {
2677 cmdp->u.raw.reserved = 0;
2678 cmdp->u.raw.mdisc_time = 0;
2679 cmdp->u.raw.mcon_time = 0;
2680 cmdp->u.raw.clen = scp->cmd_len;
2681 cmdp->u.raw.target = t;
2682 cmdp->u.raw.lun = l;
2683 cmdp->u.raw.bus = b;
2684 cmdp->u.raw.priority = 0;
2685 cmdp->u.raw.link_p = 0;
Boaz Harroshd35055a2008-02-27 15:31:20 -08002686 cmdp->u.raw.sdlen = scsi_bufflen(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 cmdp->u.raw.sense_len = 16;
2688 cmdp->u.raw.sense_data = sense_paddr;
2689 cmdp->u.raw.direction =
2690 gdth_direction_tab[scp->cmnd[0]]==DOU ? GDTH_DATA_OUT:GDTH_DATA_IN;
2691 memcpy(cmdp->u.raw.cmd,scp->cmnd,12);
Joerg Dorchainbb9ba312007-03-06 02:46:54 -08002692 cmdp->u.raw.sg_ranz = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 }
2694
Boaz Harroshd35055a2008-02-27 15:31:20 -08002695 if (scsi_bufflen(scp)) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002696 cmndinfo->dma_dir = PCI_DMA_BIDIRECTIONAL;
Boaz Harroshd35055a2008-02-27 15:31:20 -08002697 sgcnt = pci_map_sg(ha->pdev, scsi_sglist(scp), scsi_sg_count(scp),
Boaz Harrosh3892d882007-10-02 23:18:03 +02002698 cmndinfo->dma_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 if (mode64) {
Boaz Harrosh3892d882007-10-02 23:18:03 +02002700 struct scatterlist *sl;
2701
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002702 cmdp->u.raw64.sdata = (u64)-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 cmdp->u.raw64.sg_ranz = sgcnt;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002704 scsi_for_each_sg(scp, sl, sgcnt, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 cmdp->u.raw64.sg_lst[i].sg_ptr = sg_dma_address(sl);
2706#ifdef GDTH_DMA_STATISTICS
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002707 if (cmdp->u.raw64.sg_lst[i].sg_ptr > (u64)0xffffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 ha->dma64_cnt++;
2709 else
2710 ha->dma32_cnt++;
2711#endif
2712 cmdp->u.raw64.sg_lst[i].sg_len = sg_dma_len(sl);
2713 }
2714 } else {
Boaz Harrosh3892d882007-10-02 23:18:03 +02002715 struct scatterlist *sl;
2716
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 cmdp->u.raw.sdata = 0xffffffff;
2718 cmdp->u.raw.sg_ranz = sgcnt;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002719 scsi_for_each_sg(scp, sl, sgcnt, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 cmdp->u.raw.sg_lst[i].sg_ptr = sg_dma_address(sl);
2721#ifdef GDTH_DMA_STATISTICS
2722 ha->dma32_cnt++;
2723#endif
2724 cmdp->u.raw.sg_lst[i].sg_len = sg_dma_len(sl);
2725 }
2726 }
2727
2728#ifdef GDTH_STATISTICS
2729 if (max_sg < sgcnt) {
2730 max_sg = sgcnt;
2731 TRACE3(("GDT: max_sg = %d\n",sgcnt));
2732 }
2733#endif
2734
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 }
2736 if (mode64) {
2737 TRACE(("raw cmd: addr. %x sganz %x sgptr0 %x sglen0 %x\n",
2738 cmdp->u.raw64.sdata,cmdp->u.raw64.sg_ranz,
2739 cmdp->u.raw64.sg_lst[0].sg_ptr,
2740 cmdp->u.raw64.sg_lst[0].sg_len));
2741 /* evaluate command size */
2742 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw64.sg_lst) +
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002743 (u16)cmdp->u.raw64.sg_ranz * sizeof(gdth_sg64_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 } else {
2745 TRACE(("raw cmd: addr. %x sganz %x sgptr0 %x sglen0 %x\n",
2746 cmdp->u.raw.sdata,cmdp->u.raw.sg_ranz,
2747 cmdp->u.raw.sg_lst[0].sg_ptr,
2748 cmdp->u.raw.sg_lst[0].sg_len));
2749 /* evaluate command size */
2750 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw.sg_lst) +
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002751 (u16)cmdp->u.raw.sg_ranz * sizeof(gdth_sg_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 }
2753 }
2754 /* check space */
2755 if (ha->cmd_len & 3)
2756 ha->cmd_len += (4 - (ha->cmd_len & 3));
2757
2758 if (ha->cmd_cnt > 0) {
2759 if ((ha->cmd_offs_dpmem + ha->cmd_len + DPMEM_COMMAND_OFFSET) >
2760 ha->ic_all_size) {
2761 TRACE2(("gdth_fill_raw() DPMEM overflow\n"));
2762 ha->cmd_tab[cmd_index-2].cmnd = UNUSED_CMND;
2763 return 0;
2764 }
2765 }
2766
2767 /* copy command */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002768 gdth_copy_command(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 return cmd_index;
2770}
2771
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002772static int gdth_special_cmd(gdth_ha_str *ha, Scsi_Cmnd *scp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 register gdth_cmd_str *cmdp;
Boaz Harroshee54cc62008-02-27 15:29:15 -08002775 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 int cmd_index;
2777
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 cmdp= ha->pccb;
2779 TRACE2(("gdth_special_cmd(): "));
2780
2781 if (ha->type==GDT_EISA && ha->cmd_cnt>0)
2782 return 0;
2783
Boaz Harroshee54cc62008-02-27 15:29:15 -08002784 *cmdp = *cmndinfo->internal_cmd_str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 cmdp->RequestBuffer = scp;
2786
2787 /* search free command index */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002788 if (!(cmd_index=gdth_get_cmd_index(ha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 TRACE(("GDT: No free command index found\n"));
2790 return 0;
2791 }
2792
2793 /* if it's the first command, set command semaphore */
2794 if (ha->cmd_cnt == 0)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002795 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
2797 /* evaluate command size, check space */
2798 if (cmdp->OpCode == GDT_IOCTL) {
2799 TRACE2(("IOCTL\n"));
2800 ha->cmd_len =
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002801 GDTOFFSOF(gdth_cmd_str,u.ioctl.p_param) + sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 } else if (cmdp->Service == CACHESERVICE) {
2803 TRACE2(("cache command %d\n",cmdp->OpCode));
2804 if (ha->cache_feat & GDT_64BIT)
2805 ha->cmd_len =
2806 GDTOFFSOF(gdth_cmd_str,u.cache64.sg_lst) + sizeof(gdth_sg64_str);
2807 else
2808 ha->cmd_len =
2809 GDTOFFSOF(gdth_cmd_str,u.cache.sg_lst) + sizeof(gdth_sg_str);
2810 } else if (cmdp->Service == SCSIRAWSERVICE) {
2811 TRACE2(("raw command %d\n",cmdp->OpCode));
2812 if (ha->raw_feat & GDT_64BIT)
2813 ha->cmd_len =
2814 GDTOFFSOF(gdth_cmd_str,u.raw64.sg_lst) + sizeof(gdth_sg64_str);
2815 else
2816 ha->cmd_len =
2817 GDTOFFSOF(gdth_cmd_str,u.raw.sg_lst) + sizeof(gdth_sg_str);
2818 }
2819
2820 if (ha->cmd_len & 3)
2821 ha->cmd_len += (4 - (ha->cmd_len & 3));
2822
2823 if (ha->cmd_cnt > 0) {
2824 if ((ha->cmd_offs_dpmem + ha->cmd_len + DPMEM_COMMAND_OFFSET) >
2825 ha->ic_all_size) {
2826 TRACE2(("gdth_special_cmd() DPMEM overflow\n"));
2827 ha->cmd_tab[cmd_index-2].cmnd = UNUSED_CMND;
2828 return 0;
2829 }
2830 }
2831
2832 /* copy command */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002833 gdth_copy_command(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 return cmd_index;
2835}
2836
2837
2838/* Controller event handling functions */
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002839static gdth_evt_str *gdth_store_event(gdth_ha_str *ha, u16 source,
2840 u16 idx, gdth_evt_data *evt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
2842 gdth_evt_str *e;
2843 struct timeval tv;
2844
2845 /* no GDTH_LOCK_HA() ! */
2846 TRACE2(("gdth_store_event() source %d idx %d\n", source, idx));
2847 if (source == 0) /* no source -> no event */
2848 return NULL;
2849
2850 if (ebuffer[elastidx].event_source == source &&
2851 ebuffer[elastidx].event_idx == idx &&
2852 ((evt->size != 0 && ebuffer[elastidx].event_data.size != 0 &&
2853 !memcmp((char *)&ebuffer[elastidx].event_data.eu,
2854 (char *)&evt->eu, evt->size)) ||
2855 (evt->size == 0 && ebuffer[elastidx].event_data.size == 0 &&
2856 !strcmp((char *)&ebuffer[elastidx].event_data.event_string,
2857 (char *)&evt->event_string)))) {
2858 e = &ebuffer[elastidx];
2859 do_gettimeofday(&tv);
2860 e->last_stamp = tv.tv_sec;
2861 ++e->same_count;
2862 } else {
2863 if (ebuffer[elastidx].event_source != 0) { /* entry not free ? */
2864 ++elastidx;
2865 if (elastidx == MAX_EVENTS)
2866 elastidx = 0;
2867 if (elastidx == eoldidx) { /* reached mark ? */
2868 ++eoldidx;
2869 if (eoldidx == MAX_EVENTS)
2870 eoldidx = 0;
2871 }
2872 }
2873 e = &ebuffer[elastidx];
2874 e->event_source = source;
2875 e->event_idx = idx;
2876 do_gettimeofday(&tv);
2877 e->first_stamp = e->last_stamp = tv.tv_sec;
2878 e->same_count = 1;
2879 e->event_data = *evt;
2880 e->application = 0;
2881 }
2882 return e;
2883}
2884
2885static int gdth_read_event(gdth_ha_str *ha, int handle, gdth_evt_str *estr)
2886{
2887 gdth_evt_str *e;
2888 int eindex;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002889 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
2891 TRACE2(("gdth_read_event() handle %d\n", handle));
2892 spin_lock_irqsave(&ha->smp_lock, flags);
2893 if (handle == -1)
2894 eindex = eoldidx;
2895 else
2896 eindex = handle;
2897 estr->event_source = 0;
2898
Dave Jones690e7442009-10-19 19:55:13 -04002899 if (eindex < 0 || eindex >= MAX_EVENTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 spin_unlock_irqrestore(&ha->smp_lock, flags);
2901 return eindex;
2902 }
2903 e = &ebuffer[eindex];
2904 if (e->event_source != 0) {
2905 if (eindex != elastidx) {
2906 if (++eindex == MAX_EVENTS)
2907 eindex = 0;
2908 } else {
2909 eindex = -1;
2910 }
2911 memcpy(estr, e, sizeof(gdth_evt_str));
2912 }
2913 spin_unlock_irqrestore(&ha->smp_lock, flags);
2914 return eindex;
2915}
2916
2917static void gdth_readapp_event(gdth_ha_str *ha,
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002918 u8 application, gdth_evt_str *estr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919{
2920 gdth_evt_str *e;
2921 int eindex;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002922 unsigned long flags;
2923 u8 found = FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
2925 TRACE2(("gdth_readapp_event() app. %d\n", application));
2926 spin_lock_irqsave(&ha->smp_lock, flags);
2927 eindex = eoldidx;
2928 for (;;) {
2929 e = &ebuffer[eindex];
2930 if (e->event_source == 0)
2931 break;
2932 if ((e->application & application) == 0) {
2933 e->application |= application;
2934 found = TRUE;
2935 break;
2936 }
2937 if (eindex == elastidx)
2938 break;
2939 if (++eindex == MAX_EVENTS)
2940 eindex = 0;
2941 }
2942 if (found)
2943 memcpy(estr, e, sizeof(gdth_evt_str));
2944 else
2945 estr->event_source = 0;
2946 spin_unlock_irqrestore(&ha->smp_lock, flags);
2947}
2948
2949static void gdth_clear_events(void)
2950{
2951 TRACE(("gdth_clear_events()"));
2952
2953 eoldidx = elastidx = 0;
2954 ebuffer[0].event_source = 0;
2955}
2956
2957
2958/* SCSI interface functions */
2959
Jeff Garzik230e8862007-12-13 16:14:12 -08002960static irqreturn_t __gdth_interrupt(gdth_ha_str *ha,
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002961 int gdth_from_wait, int* pIndex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 gdt6m_dpram_str __iomem *dp6m_ptr = NULL;
2964 gdt6_dpram_str __iomem *dp6_ptr;
2965 gdt2_dpram_str __iomem *dp2_ptr;
2966 Scsi_Cmnd *scp;
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002967 int rval, i;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002968 u8 IStatus;
2969 u16 Service;
2970 unsigned long flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971#ifdef INT_COAL
2972 int coalesced = FALSE;
2973 int next = FALSE;
2974 gdth_coal_status *pcs = NULL;
2975 int act_int_coal = 0;
2976#endif
2977
Jeff Garzik230e8862007-12-13 16:14:12 -08002978 TRACE(("gdth_interrupt() IRQ %d\n", ha->irq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
2980 /* if polling and not from gdth_wait() -> return */
2981 if (gdth_polling) {
2982 if (!gdth_from_wait) {
2983 return IRQ_HANDLED;
2984 }
2985 }
2986
2987 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002988 spin_lock_irqsave(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
2990 /* search controller */
Jeff Garzik230e8862007-12-13 16:14:12 -08002991 IStatus = gdth_get_status(ha);
2992 if (IStatus == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 /* spurious interrupt */
2994 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002995 spin_unlock_irqrestore(&ha->smp_lock, flags);
2996 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
2999#ifdef GDTH_STATISTICS
3000 ++act_ints;
3001#endif
3002
3003#ifdef INT_COAL
3004 /* See if the fw is returning coalesced status */
3005 if (IStatus == COALINDEX) {
3006 /* Coalesced status. Setup the initial status
3007 buffer pointer and flags */
3008 pcs = ha->coal_stat;
3009 coalesced = TRUE;
3010 next = TRUE;
3011 }
3012
3013 do {
3014 if (coalesced) {
3015 /* For coalesced requests all status
3016 information is found in the status buffer */
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003017 IStatus = (u8)(pcs->status & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 }
3019#endif
3020
3021 if (ha->type == GDT_EISA) {
3022 if (IStatus & 0x80) { /* error flag */
3023 IStatus &= ~0x80;
3024 ha->status = inw(ha->bmic + MAILBOXREG+8);
3025 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
3026 } else /* no error */
3027 ha->status = S_OK;
3028 ha->info = inl(ha->bmic + MAILBOXREG+12);
3029 ha->service = inw(ha->bmic + MAILBOXREG+10);
3030 ha->info2 = inl(ha->bmic + MAILBOXREG+4);
3031
3032 outb(0xff, ha->bmic + EDOORREG); /* acknowledge interrupt */
3033 outb(0x00, ha->bmic + SEMA1REG); /* reset status semaphore */
3034 } else if (ha->type == GDT_ISA) {
3035 dp2_ptr = ha->brd;
3036 if (IStatus & 0x80) { /* error flag */
3037 IStatus &= ~0x80;
Jeff Garzika52667f2007-10-02 22:55:53 +02003038 ha->status = readw(&dp2_ptr->u.ic.Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
3040 } else /* no error */
3041 ha->status = S_OK;
Jeff Garzika52667f2007-10-02 22:55:53 +02003042 ha->info = readl(&dp2_ptr->u.ic.Info[0]);
3043 ha->service = readw(&dp2_ptr->u.ic.Service);
3044 ha->info2 = readl(&dp2_ptr->u.ic.Info[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
Jeff Garzika52667f2007-10-02 22:55:53 +02003046 writeb(0xff, &dp2_ptr->io.irqdel); /* acknowledge interrupt */
3047 writeb(0, &dp2_ptr->u.ic.Cmd_Index);/* reset command index */
3048 writeb(0, &dp2_ptr->io.Sema1); /* reset status semaphore */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 } else if (ha->type == GDT_PCI) {
3050 dp6_ptr = ha->brd;
3051 if (IStatus & 0x80) { /* error flag */
3052 IStatus &= ~0x80;
Jeff Garzika52667f2007-10-02 22:55:53 +02003053 ha->status = readw(&dp6_ptr->u.ic.Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
3055 } else /* no error */
3056 ha->status = S_OK;
Jeff Garzika52667f2007-10-02 22:55:53 +02003057 ha->info = readl(&dp6_ptr->u.ic.Info[0]);
3058 ha->service = readw(&dp6_ptr->u.ic.Service);
3059 ha->info2 = readl(&dp6_ptr->u.ic.Info[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
Jeff Garzika52667f2007-10-02 22:55:53 +02003061 writeb(0xff, &dp6_ptr->io.irqdel); /* acknowledge interrupt */
3062 writeb(0, &dp6_ptr->u.ic.Cmd_Index);/* reset command index */
3063 writeb(0, &dp6_ptr->io.Sema1); /* reset status semaphore */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 } else if (ha->type == GDT_PCINEW) {
3065 if (IStatus & 0x80) { /* error flag */
3066 IStatus &= ~0x80;
3067 ha->status = inw(PTR2USHORT(&ha->plx->status));
3068 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
3069 } else
3070 ha->status = S_OK;
3071 ha->info = inl(PTR2USHORT(&ha->plx->info[0]));
3072 ha->service = inw(PTR2USHORT(&ha->plx->service));
3073 ha->info2 = inl(PTR2USHORT(&ha->plx->info[1]));
3074
3075 outb(0xff, PTR2USHORT(&ha->plx->edoor_reg));
3076 outb(0x00, PTR2USHORT(&ha->plx->sema1_reg));
3077 } else if (ha->type == GDT_PCIMPR) {
3078 dp6m_ptr = ha->brd;
3079 if (IStatus & 0x80) { /* error flag */
3080 IStatus &= ~0x80;
3081#ifdef INT_COAL
3082 if (coalesced)
Jean Delvare107e7162006-11-09 21:45:09 +01003083 ha->status = pcs->ext_status & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 else
3085#endif
Jeff Garzika52667f2007-10-02 22:55:53 +02003086 ha->status = readw(&dp6m_ptr->i960r.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
3088 } else /* no error */
3089 ha->status = S_OK;
3090#ifdef INT_COAL
3091 /* get information */
3092 if (coalesced) {
3093 ha->info = pcs->info0;
3094 ha->info2 = pcs->info1;
Jean Delvare107e7162006-11-09 21:45:09 +01003095 ha->service = (pcs->ext_status >> 16) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 } else
3097#endif
3098 {
Jeff Garzika52667f2007-10-02 22:55:53 +02003099 ha->info = readl(&dp6m_ptr->i960r.info[0]);
3100 ha->service = readw(&dp6m_ptr->i960r.service);
3101 ha->info2 = readl(&dp6m_ptr->i960r.info[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 }
3103 /* event string */
3104 if (IStatus == ASYNCINDEX) {
3105 if (ha->service != SCREENSERVICE &&
3106 (ha->fw_vers & 0xff) >= 0x1a) {
Jeff Garzika52667f2007-10-02 22:55:53 +02003107 ha->dvr.severity = readb
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 (&((gdt6m_dpram_str __iomem *)ha->brd)->i960r.severity);
3109 for (i = 0; i < 256; ++i) {
Jeff Garzika52667f2007-10-02 22:55:53 +02003110 ha->dvr.event_string[i] = readb
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 (&((gdt6m_dpram_str __iomem *)ha->brd)->i960r.evt_str[i]);
3112 if (ha->dvr.event_string[i] == 0)
3113 break;
3114 }
3115 }
3116 }
3117#ifdef INT_COAL
3118 /* Make sure that non coalesced interrupts get cleared
3119 before being handled by gdth_async_event/gdth_sync_event */
3120 if (!coalesced)
3121#endif
3122 {
Jeff Garzika52667f2007-10-02 22:55:53 +02003123 writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
3124 writeb(0, &dp6m_ptr->i960r.sema1_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 }
3126 } else {
3127 TRACE2(("gdth_interrupt() unknown controller type\n"));
3128 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003129 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 return IRQ_HANDLED;
3131 }
3132
3133 TRACE(("gdth_interrupt() index %d stat %d info %d\n",
3134 IStatus,ha->status,ha->info));
3135
3136 if (gdth_from_wait) {
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003137 *pIndex = (int)IStatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 }
3139
3140 if (IStatus == ASYNCINDEX) {
3141 TRACE2(("gdth_interrupt() async. event\n"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003142 gdth_async_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003144 spin_unlock_irqrestore(&ha->smp_lock, flags);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003145 gdth_next(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 return IRQ_HANDLED;
3147 }
3148
3149 if (IStatus == SPEZINDEX) {
3150 TRACE2(("Service unknown or not initialized !\n"));
3151 ha->dvr.size = sizeof(ha->dvr.eu.driver);
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003152 ha->dvr.eu.driver.ionode = ha->hanum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 gdth_store_event(ha, ES_DRIVER, 4, &ha->dvr);
3154 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003155 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 return IRQ_HANDLED;
3157 }
3158 scp = ha->cmd_tab[IStatus-2].cmnd;
3159 Service = ha->cmd_tab[IStatus-2].service;
3160 ha->cmd_tab[IStatus-2].cmnd = UNUSED_CMND;
3161 if (scp == UNUSED_CMND) {
3162 TRACE2(("gdth_interrupt() index to unused command (%d)\n",IStatus));
3163 ha->dvr.size = sizeof(ha->dvr.eu.driver);
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003164 ha->dvr.eu.driver.ionode = ha->hanum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 ha->dvr.eu.driver.index = IStatus;
3166 gdth_store_event(ha, ES_DRIVER, 1, &ha->dvr);
3167 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003168 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 return IRQ_HANDLED;
3170 }
3171 if (scp == INTERNAL_CMND) {
3172 TRACE(("gdth_interrupt() answer to internal command\n"));
3173 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003174 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 return IRQ_HANDLED;
3176 }
3177
3178 TRACE(("gdth_interrupt() sync. status\n"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003179 rval = gdth_sync_event(ha,Service,IStatus,scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003181 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 if (rval == 2) {
Boaz Harroshf842b642007-10-02 23:16:01 +02003183 gdth_putq(ha, scp, gdth_cmnd_priv(scp)->priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 } else if (rval == 1) {
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02003185 gdth_scsi_done(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 }
3187
3188#ifdef INT_COAL
3189 if (coalesced) {
3190 /* go to the next status in the status buffer */
3191 ++pcs;
3192#ifdef GDTH_STATISTICS
3193 ++act_int_coal;
3194 if (act_int_coal > max_int_coal) {
3195 max_int_coal = act_int_coal;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003196 printk("GDT: max_int_coal = %d\n",(u16)max_int_coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 }
3198#endif
3199 /* see if there is another status */
3200 if (pcs->status == 0)
3201 /* Stop the coalesce loop */
3202 next = FALSE;
3203 }
3204 } while (next);
3205
3206 /* coalescing only for new GDT_PCIMPR controllers available */
3207 if (ha->type == GDT_PCIMPR && coalesced) {
Jeff Garzika52667f2007-10-02 22:55:53 +02003208 writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
3209 writeb(0, &dp6m_ptr->i960r.sema1_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 }
3211#endif
3212
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003213 gdth_next(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 return IRQ_HANDLED;
3215}
3216
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003217static irqreturn_t gdth_interrupt(int irq, void *dev_id)
3218{
Jeff Garzik230e8862007-12-13 16:14:12 -08003219 gdth_ha_str *ha = dev_id;
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003220
Jeff Garzik230e8862007-12-13 16:14:12 -08003221 return __gdth_interrupt(ha, false, NULL);
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003222}
3223
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003224static int gdth_sync_event(gdth_ha_str *ha, int service, u8 index,
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003225 Scsi_Cmnd *scp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 gdth_msg_str *msg;
3228 gdth_cmd_str *cmdp;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003229 u8 b, t;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02003230 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 cmdp = ha->pccb;
3233 TRACE(("gdth_sync_event() serv %d status %d\n",
3234 service,ha->status));
3235
3236 if (service == SCREENSERVICE) {
3237 msg = ha->pmsg;
3238 TRACE(("len: %d, answer: %d, ext: %d, alen: %d\n",
3239 msg->msg_len,msg->msg_answer,msg->msg_ext,msg->msg_alen));
3240 if (msg->msg_len > MSGLEN+1)
3241 msg->msg_len = MSGLEN+1;
3242 if (msg->msg_len)
3243 if (!(msg->msg_answer && msg->msg_ext)) {
3244 msg->msg_text[msg->msg_len] = '\0';
3245 printk("%s",msg->msg_text);
3246 }
3247
3248 if (msg->msg_ext && !msg->msg_answer) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003249 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 gdth_delay(0);
3251 cmdp->Service = SCREENSERVICE;
3252 cmdp->RequestBuffer = SCREEN_CMND;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003253 gdth_get_cmd_index(ha);
3254 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 cmdp->OpCode = GDT_READ;
3256 cmdp->BoardNode = LOCALBOARD;
3257 cmdp->u.screen.reserved = 0;
3258 cmdp->u.screen.su.msg.msg_handle= msg->msg_handle;
3259 cmdp->u.screen.su.msg.msg_addr = ha->msg_phys;
3260 ha->cmd_offs_dpmem = 0;
3261 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.screen.su.msg.msg_addr)
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003262 + sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 ha->cmd_cnt = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003264 gdth_copy_command(ha);
3265 gdth_release_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 return 0;
3267 }
3268
3269 if (msg->msg_answer && msg->msg_alen) {
3270 /* default answers (getchar() not possible) */
3271 if (msg->msg_alen == 1) {
3272 msg->msg_alen = 0;
3273 msg->msg_len = 1;
3274 msg->msg_text[0] = 0;
3275 } else {
3276 msg->msg_alen -= 2;
3277 msg->msg_len = 2;
3278 msg->msg_text[0] = 1;
3279 msg->msg_text[1] = 0;
3280 }
3281 msg->msg_ext = 0;
3282 msg->msg_answer = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003283 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 gdth_delay(0);
3285 cmdp->Service = SCREENSERVICE;
3286 cmdp->RequestBuffer = SCREEN_CMND;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003287 gdth_get_cmd_index(ha);
3288 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 cmdp->OpCode = GDT_WRITE;
3290 cmdp->BoardNode = LOCALBOARD;
3291 cmdp->u.screen.reserved = 0;
3292 cmdp->u.screen.su.msg.msg_handle= msg->msg_handle;
3293 cmdp->u.screen.su.msg.msg_addr = ha->msg_phys;
3294 ha->cmd_offs_dpmem = 0;
3295 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.screen.su.msg.msg_addr)
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003296 + sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 ha->cmd_cnt = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003298 gdth_copy_command(ha);
3299 gdth_release_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 return 0;
3301 }
3302 printk("\n");
3303
3304 } else {
Christoph Hellwig52759e62007-10-02 22:59:53 +02003305 b = scp->device->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 t = scp->device->id;
Boaz Harroshf842b642007-10-02 23:16:01 +02003307 if (cmndinfo->OpCode == -1 && b != ha->virt_bus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 ha->raw[BUS_L2P(ha,b)].io_cnt[t]--;
3309 }
3310 /* cache or raw service */
3311 if (ha->status == S_BSY) {
3312 TRACE2(("Controller busy -> retry !\n"));
Boaz Harroshf842b642007-10-02 23:16:01 +02003313 if (cmndinfo->OpCode == GDT_MOUNT)
3314 cmndinfo->OpCode = GDT_CLUST_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 /* retry */
3316 return 2;
3317 }
Boaz Harroshd35055a2008-02-27 15:31:20 -08003318 if (scsi_bufflen(scp))
3319 pci_unmap_sg(ha->pdev, scsi_sglist(scp), scsi_sg_count(scp),
Boaz Harrosh3892d882007-10-02 23:18:03 +02003320 cmndinfo->dma_dir);
3321
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02003322 if (cmndinfo->sense_paddr)
3323 pci_unmap_page(ha->pdev, cmndinfo->sense_paddr, 16,
3324 PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325
3326 if (ha->status == S_OK) {
Boaz Harroshf842b642007-10-02 23:16:01 +02003327 cmndinfo->status = S_OK;
3328 cmndinfo->info = ha->info;
3329 if (cmndinfo->OpCode != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 TRACE2(("gdth_sync_event(): special cmd 0x%x OK\n",
Boaz Harroshf842b642007-10-02 23:16:01 +02003331 cmndinfo->OpCode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 /* special commands GDT_CLUST_INFO/GDT_MOUNT ? */
Boaz Harroshf842b642007-10-02 23:16:01 +02003333 if (cmndinfo->OpCode == GDT_CLUST_INFO) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003334 ha->hdr[t].cluster_type = (u8)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 if (!(ha->hdr[t].cluster_type &
3336 CLUSTER_MOUNTED)) {
3337 /* NOT MOUNTED -> MOUNT */
Boaz Harroshf842b642007-10-02 23:16:01 +02003338 cmndinfo->OpCode = GDT_MOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 if (ha->hdr[t].cluster_type &
3340 CLUSTER_RESERVED) {
3341 /* cluster drive RESERVED (on the other node) */
Boaz Harroshf842b642007-10-02 23:16:01 +02003342 cmndinfo->phase = -2; /* reservation conflict */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 }
3344 } else {
Boaz Harroshf842b642007-10-02 23:16:01 +02003345 cmndinfo->OpCode = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 }
3347 } else {
Boaz Harroshf842b642007-10-02 23:16:01 +02003348 if (cmndinfo->OpCode == GDT_MOUNT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 ha->hdr[t].cluster_type |= CLUSTER_MOUNTED;
3350 ha->hdr[t].media_changed = TRUE;
Boaz Harroshf842b642007-10-02 23:16:01 +02003351 } else if (cmndinfo->OpCode == GDT_UNMOUNT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 ha->hdr[t].cluster_type &= ~CLUSTER_MOUNTED;
3353 ha->hdr[t].media_changed = TRUE;
3354 }
Boaz Harroshf842b642007-10-02 23:16:01 +02003355 cmndinfo->OpCode = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 }
3357 /* retry */
Boaz Harroshf842b642007-10-02 23:16:01 +02003358 cmndinfo->priority = HIGH_PRI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 return 2;
3360 } else {
3361 /* RESERVE/RELEASE ? */
3362 if (scp->cmnd[0] == RESERVE) {
3363 ha->hdr[t].cluster_type |= CLUSTER_RESERVED;
3364 } else if (scp->cmnd[0] == RELEASE) {
3365 ha->hdr[t].cluster_type &= ~CLUSTER_RESERVED;
3366 }
3367 scp->result = DID_OK << 16;
3368 scp->sense_buffer[0] = 0;
3369 }
3370 } else {
Boaz Harroshf842b642007-10-02 23:16:01 +02003371 cmndinfo->status = ha->status;
3372 cmndinfo->info = ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373
Boaz Harroshf842b642007-10-02 23:16:01 +02003374 if (cmndinfo->OpCode != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 TRACE2(("gdth_sync_event(): special cmd 0x%x error 0x%x\n",
Boaz Harroshf842b642007-10-02 23:16:01 +02003376 cmndinfo->OpCode, ha->status));
3377 if (cmndinfo->OpCode == GDT_SCAN_START ||
3378 cmndinfo->OpCode == GDT_SCAN_END) {
3379 cmndinfo->OpCode = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 /* retry */
Boaz Harroshf842b642007-10-02 23:16:01 +02003381 cmndinfo->priority = HIGH_PRI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 return 2;
3383 }
3384 memset((char*)scp->sense_buffer,0,16);
3385 scp->sense_buffer[0] = 0x70;
3386 scp->sense_buffer[2] = NOT_READY;
3387 scp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
3388 } else if (service == CACHESERVICE) {
3389 if (ha->status == S_CACHE_UNKNOWN &&
3390 (ha->hdr[t].cluster_type &
3391 CLUSTER_RESERVE_STATE) == CLUSTER_RESERVE_STATE) {
3392 /* bus reset -> force GDT_CLUST_INFO */
3393 ha->hdr[t].cluster_type &= ~CLUSTER_RESERVED;
3394 }
3395 memset((char*)scp->sense_buffer,0,16);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003396 if (ha->status == (u16)S_CACHE_RESERV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 scp->result = (DID_OK << 16) | (RESERVATION_CONFLICT << 1);
3398 } else {
3399 scp->sense_buffer[0] = 0x70;
3400 scp->sense_buffer[2] = NOT_READY;
3401 scp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
3402 }
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02003403 if (!cmndinfo->internal_command) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 ha->dvr.size = sizeof(ha->dvr.eu.sync);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003405 ha->dvr.eu.sync.ionode = ha->hanum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 ha->dvr.eu.sync.service = service;
3407 ha->dvr.eu.sync.status = ha->status;
3408 ha->dvr.eu.sync.info = ha->info;
3409 ha->dvr.eu.sync.hostdrive = t;
3410 if (ha->status >= 0x8000)
3411 gdth_store_event(ha, ES_SYNC, 0, &ha->dvr);
3412 else
3413 gdth_store_event(ha, ES_SYNC, service, &ha->dvr);
3414 }
3415 } else {
3416 /* sense buffer filled from controller firmware (DMA) */
3417 if (ha->status != S_RAW_SCSI || ha->info >= 0x100) {
3418 scp->result = DID_BAD_TARGET << 16;
3419 } else {
3420 scp->result = (DID_OK << 16) | ha->info;
3421 }
3422 }
3423 }
Boaz Harroshf842b642007-10-02 23:16:01 +02003424 if (!cmndinfo->wait_for_completion)
3425 cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 else
3427 return 1;
3428 }
3429
3430 return 0;
3431}
3432
3433static char *async_cache_tab[] = {
3434/* 0*/ "\011\000\002\002\002\004\002\006\004"
3435 "GDT HA %u, service %u, async. status %u/%lu unknown",
3436/* 1*/ "\011\000\002\002\002\004\002\006\004"
3437 "GDT HA %u, service %u, async. status %u/%lu unknown",
3438/* 2*/ "\005\000\002\006\004"
3439 "GDT HA %u, Host Drive %lu not ready",
3440/* 3*/ "\005\000\002\006\004"
3441 "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",
3442/* 4*/ "\005\000\002\006\004"
3443 "GDT HA %u, mirror update on Host Drive %lu failed",
3444/* 5*/ "\005\000\002\006\004"
3445 "GDT HA %u, Mirror Drive %lu failed",
3446/* 6*/ "\005\000\002\006\004"
3447 "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",
3448/* 7*/ "\005\000\002\006\004"
3449 "GDT HA %u, Host Drive %lu write protected",
3450/* 8*/ "\005\000\002\006\004"
3451 "GDT HA %u, media changed in Host Drive %lu",
3452/* 9*/ "\005\000\002\006\004"
3453 "GDT HA %u, Host Drive %lu is offline",
3454/*10*/ "\005\000\002\006\004"
3455 "GDT HA %u, media change of Mirror Drive %lu",
3456/*11*/ "\005\000\002\006\004"
3457 "GDT HA %u, Mirror Drive %lu is write protected",
3458/*12*/ "\005\000\002\006\004"
3459 "GDT HA %u, general error on Host Drive %lu. Please check the devices of this drive!",
3460/*13*/ "\007\000\002\006\002\010\002"
3461 "GDT HA %u, Array Drive %u: Cache Drive %u failed",
3462/*14*/ "\005\000\002\006\002"
3463 "GDT HA %u, Array Drive %u: FAIL state entered",
3464/*15*/ "\005\000\002\006\002"
3465 "GDT HA %u, Array Drive %u: error",
3466/*16*/ "\007\000\002\006\002\010\002"
3467 "GDT HA %u, Array Drive %u: failed drive replaced by Cache Drive %u",
3468/*17*/ "\005\000\002\006\002"
3469 "GDT HA %u, Array Drive %u: parity build failed",
3470/*18*/ "\005\000\002\006\002"
3471 "GDT HA %u, Array Drive %u: drive rebuild failed",
3472/*19*/ "\005\000\002\010\002"
3473 "GDT HA %u, Test of Hot Fix %u failed",
3474/*20*/ "\005\000\002\006\002"
3475 "GDT HA %u, Array Drive %u: drive build finished successfully",
3476/*21*/ "\005\000\002\006\002"
3477 "GDT HA %u, Array Drive %u: drive rebuild finished successfully",
3478/*22*/ "\007\000\002\006\002\010\002"
3479 "GDT HA %u, Array Drive %u: Hot Fix %u activated",
3480/*23*/ "\005\000\002\006\002"
3481 "GDT HA %u, Host Drive %u: processing of i/o aborted due to serious drive error",
3482/*24*/ "\005\000\002\010\002"
3483 "GDT HA %u, mirror update on Cache Drive %u completed",
3484/*25*/ "\005\000\002\010\002"
3485 "GDT HA %u, mirror update on Cache Drive %lu failed",
3486/*26*/ "\005\000\002\006\002"
3487 "GDT HA %u, Array Drive %u: drive rebuild started",
3488/*27*/ "\005\000\002\012\001"
3489 "GDT HA %u, Fault bus %u: SHELF OK detected",
3490/*28*/ "\005\000\002\012\001"
3491 "GDT HA %u, Fault bus %u: SHELF not OK detected",
3492/*29*/ "\007\000\002\012\001\013\001"
3493 "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug started",
3494/*30*/ "\007\000\002\012\001\013\001"
3495 "GDT HA %u, Fault bus %u, ID %u: new disk detected",
3496/*31*/ "\007\000\002\012\001\013\001"
3497 "GDT HA %u, Fault bus %u, ID %u: old disk detected",
3498/*32*/ "\007\000\002\012\001\013\001"
3499 "GDT HA %u, Fault bus %u, ID %u: plugging an active disk is invalid",
3500/*33*/ "\007\000\002\012\001\013\001"
3501 "GDT HA %u, Fault bus %u, ID %u: invalid device detected",
3502/*34*/ "\011\000\002\012\001\013\001\006\004"
3503 "GDT HA %u, Fault bus %u, ID %u: insufficient disk capacity (%lu MB required)",
3504/*35*/ "\007\000\002\012\001\013\001"
3505 "GDT HA %u, Fault bus %u, ID %u: disk write protected",
3506/*36*/ "\007\000\002\012\001\013\001"
3507 "GDT HA %u, Fault bus %u, ID %u: disk not available",
3508/*37*/ "\007\000\002\012\001\006\004"
3509 "GDT HA %u, Fault bus %u: swap detected (%lu)",
3510/*38*/ "\007\000\002\012\001\013\001"
3511 "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug finished successfully",
3512/*39*/ "\007\000\002\012\001\013\001"
3513 "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug aborted due to user Hot Plug",
3514/*40*/ "\007\000\002\012\001\013\001"
3515 "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug aborted",
3516/*41*/ "\007\000\002\012\001\013\001"
3517 "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug for Hot Fix started",
3518/*42*/ "\005\000\002\006\002"
3519 "GDT HA %u, Array Drive %u: drive build started",
3520/*43*/ "\003\000\002"
3521 "GDT HA %u, DRAM parity error detected",
3522/*44*/ "\005\000\002\006\002"
3523 "GDT HA %u, Mirror Drive %u: update started",
3524/*45*/ "\007\000\002\006\002\010\002"
3525 "GDT HA %u, Mirror Drive %u: Hot Fix %u activated",
3526/*46*/ "\005\000\002\006\002"
3527 "GDT HA %u, Array Drive %u: no matching Pool Hot Fix Drive available",
3528/*47*/ "\005\000\002\006\002"
3529 "GDT HA %u, Array Drive %u: Pool Hot Fix Drive available",
3530/*48*/ "\005\000\002\006\002"
3531 "GDT HA %u, Mirror Drive %u: no matching Pool Hot Fix Drive available",
3532/*49*/ "\005\000\002\006\002"
3533 "GDT HA %u, Mirror Drive %u: Pool Hot Fix Drive available",
3534/*50*/ "\007\000\002\012\001\013\001"
3535 "GDT HA %u, SCSI bus %u, ID %u: IGNORE_WIDE_RESIDUE message received",
3536/*51*/ "\005\000\002\006\002"
3537 "GDT HA %u, Array Drive %u: expand started",
3538/*52*/ "\005\000\002\006\002"
3539 "GDT HA %u, Array Drive %u: expand finished successfully",
3540/*53*/ "\005\000\002\006\002"
3541 "GDT HA %u, Array Drive %u: expand failed",
3542/*54*/ "\003\000\002"
3543 "GDT HA %u, CPU temperature critical",
3544/*55*/ "\003\000\002"
3545 "GDT HA %u, CPU temperature OK",
3546/*56*/ "\005\000\002\006\004"
3547 "GDT HA %u, Host drive %lu created",
3548/*57*/ "\005\000\002\006\002"
3549 "GDT HA %u, Array Drive %u: expand restarted",
3550/*58*/ "\005\000\002\006\002"
3551 "GDT HA %u, Array Drive %u: expand stopped",
3552/*59*/ "\005\000\002\010\002"
3553 "GDT HA %u, Mirror Drive %u: drive build quited",
3554/*60*/ "\005\000\002\006\002"
3555 "GDT HA %u, Array Drive %u: parity build quited",
3556/*61*/ "\005\000\002\006\002"
3557 "GDT HA %u, Array Drive %u: drive rebuild quited",
3558/*62*/ "\005\000\002\006\002"
3559 "GDT HA %u, Array Drive %u: parity verify started",
3560/*63*/ "\005\000\002\006\002"
3561 "GDT HA %u, Array Drive %u: parity verify done",
3562/*64*/ "\005\000\002\006\002"
3563 "GDT HA %u, Array Drive %u: parity verify failed",
3564/*65*/ "\005\000\002\006\002"
3565 "GDT HA %u, Array Drive %u: parity error detected",
3566/*66*/ "\005\000\002\006\002"
3567 "GDT HA %u, Array Drive %u: parity verify quited",
3568/*67*/ "\005\000\002\006\002"
3569 "GDT HA %u, Host Drive %u reserved",
3570/*68*/ "\005\000\002\006\002"
3571 "GDT HA %u, Host Drive %u mounted and released",
3572/*69*/ "\005\000\002\006\002"
3573 "GDT HA %u, Host Drive %u released",
3574/*70*/ "\003\000\002"
3575 "GDT HA %u, DRAM error detected and corrected with ECC",
3576/*71*/ "\003\000\002"
3577 "GDT HA %u, Uncorrectable DRAM error detected with ECC",
3578/*72*/ "\011\000\002\012\001\013\001\014\001"
3579 "GDT HA %u, SCSI bus %u, ID %u, LUN %u: reassigning block",
3580/*73*/ "\005\000\002\006\002"
3581 "GDT HA %u, Host drive %u resetted locally",
3582/*74*/ "\005\000\002\006\002"
3583 "GDT HA %u, Host drive %u resetted remotely",
3584/*75*/ "\003\000\002"
3585 "GDT HA %u, async. status 75 unknown",
3586};
3587
3588
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003589static int gdth_async_event(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 gdth_cmd_str *cmdp;
3592 int cmd_index;
3593
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 cmdp= ha->pccb;
3595 TRACE2(("gdth_async_event() ha %d serv %d\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003596 ha->hanum, ha->service));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
3598 if (ha->service == SCREENSERVICE) {
3599 if (ha->status == MSG_REQUEST) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003600 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 gdth_delay(0);
3602 cmdp->Service = SCREENSERVICE;
3603 cmdp->RequestBuffer = SCREEN_CMND;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003604 cmd_index = gdth_get_cmd_index(ha);
3605 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 cmdp->OpCode = GDT_READ;
3607 cmdp->BoardNode = LOCALBOARD;
3608 cmdp->u.screen.reserved = 0;
3609 cmdp->u.screen.su.msg.msg_handle= MSG_INV_HANDLE;
3610 cmdp->u.screen.su.msg.msg_addr = ha->msg_phys;
3611 ha->cmd_offs_dpmem = 0;
3612 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.screen.su.msg.msg_addr)
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003613 + sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 ha->cmd_cnt = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003615 gdth_copy_command(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 if (ha->type == GDT_EISA)
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003617 printk("[EISA slot %d] ",(u16)ha->brd_phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 else if (ha->type == GDT_ISA)
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003619 printk("[DPMEM 0x%4X] ",(u16)ha->brd_phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 else
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003621 printk("[PCI %d/%d] ",(u16)(ha->brd_phys>>8),
3622 (u16)((ha->brd_phys>>3)&0x1f));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003623 gdth_release_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 }
3625
3626 } else {
3627 if (ha->type == GDT_PCIMPR &&
3628 (ha->fw_vers & 0xff) >= 0x1a) {
3629 ha->dvr.size = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003630 ha->dvr.eu.async.ionode = ha->hanum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 ha->dvr.eu.async.status = ha->status;
3632 /* severity and event_string already set! */
3633 } else {
3634 ha->dvr.size = sizeof(ha->dvr.eu.async);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003635 ha->dvr.eu.async.ionode = ha->hanum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 ha->dvr.eu.async.service = ha->service;
3637 ha->dvr.eu.async.status = ha->status;
3638 ha->dvr.eu.async.info = ha->info;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003639 *(u32 *)ha->dvr.eu.async.scsi_coord = ha->info2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 }
3641 gdth_store_event( ha, ES_ASYNC, ha->service, &ha->dvr );
3642 gdth_log_event( &ha->dvr, NULL );
3643
3644 /* new host drive from expand? */
3645 if (ha->service == CACHESERVICE && ha->status == 56) {
3646 TRACE2(("gdth_async_event(): new host drive %d created\n",
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003647 (u16)ha->info));
3648 /* gdth_analyse_hdrive(hanum, (u16)ha->info); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 }
3650 }
3651 return 1;
3652}
3653
3654static void gdth_log_event(gdth_evt_data *dvr, char *buffer)
3655{
3656 gdth_stackframe stack;
3657 char *f = NULL;
3658 int i,j;
3659
3660 TRACE2(("gdth_log_event()\n"));
3661 if (dvr->size == 0) {
3662 if (buffer == NULL) {
3663 printk("Adapter %d: %s\n",dvr->eu.async.ionode,dvr->event_string);
3664 } else {
3665 sprintf(buffer,"Adapter %d: %s\n",
3666 dvr->eu.async.ionode,dvr->event_string);
3667 }
3668 } else if (dvr->eu.async.service == CACHESERVICE &&
3669 INDEX_OK(dvr->eu.async.status, async_cache_tab)) {
3670 TRACE2(("GDT: Async. event cache service, event no.: %d\n",
3671 dvr->eu.async.status));
3672
3673 f = async_cache_tab[dvr->eu.async.status];
3674
3675 /* i: parameter to push, j: stack element to fill */
3676 for (j=0,i=1; i < f[0]; i+=2) {
3677 switch (f[i+1]) {
3678 case 4:
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003679 stack.b[j++] = *(u32*)&dvr->eu.stream[(int)f[i]];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 break;
3681 case 2:
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003682 stack.b[j++] = *(u16*)&dvr->eu.stream[(int)f[i]];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 break;
3684 case 1:
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003685 stack.b[j++] = *(u8*)&dvr->eu.stream[(int)f[i]];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 break;
3687 default:
3688 break;
3689 }
3690 }
3691
3692 if (buffer == NULL) {
3693 printk(&f[(int)f[0]],stack);
3694 printk("\n");
3695 } else {
3696 sprintf(buffer,&f[(int)f[0]],stack);
3697 }
3698
3699 } else {
3700 if (buffer == NULL) {
3701 printk("GDT HA %u, Unknown async. event service %d event no. %d\n",
3702 dvr->eu.async.ionode,dvr->eu.async.service,dvr->eu.async.status);
3703 } else {
3704 sprintf(buffer,"GDT HA %u, Unknown async. event service %d event no. %d",
3705 dvr->eu.async.ionode,dvr->eu.async.service,dvr->eu.async.status);
3706 }
3707 }
3708}
3709
3710#ifdef GDTH_STATISTICS
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003711static u8 gdth_timer_running;
James Bottomley2d6f0d02008-05-04 22:34:49 -05003712
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003713static void gdth_timeout(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003715 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 Scsi_Cmnd *nscp;
3717 gdth_ha_str *ha;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003718 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719
James Bottomley2d6f0d02008-05-04 22:34:49 -05003720 if(unlikely(list_empty(&gdth_instances))) {
3721 gdth_timer_running = 0;
3722 return;
3723 }
Boaz Harroshb31ddd32008-02-27 15:27:16 -08003724
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02003725 ha = list_first_entry(&gdth_instances, gdth_ha_str, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 spin_lock_irqsave(&ha->smp_lock, flags);
3727
3728 for (act_stats=0,i=0; i<GDTH_MAXCMDS; ++i)
3729 if (ha->cmd_tab[i].cmnd != UNUSED_CMND)
3730 ++act_stats;
3731
3732 for (act_rq=0,nscp=ha->req_first; nscp; nscp=(Scsi_Cmnd*)nscp->SCp.ptr)
3733 ++act_rq;
3734
3735 TRACE2(("gdth_to(): ints %d, ios %d, act_stats %d, act_rq %d\n",
3736 act_ints, act_ios, act_stats, act_rq));
3737 act_ints = act_ios = 0;
3738
3739 gdth_timer.expires = jiffies + 30 * HZ;
3740 add_timer(&gdth_timer);
3741 spin_unlock_irqrestore(&ha->smp_lock, flags);
3742}
James Bottomley2d6f0d02008-05-04 22:34:49 -05003743
3744static void gdth_timer_init(void)
3745{
3746 if (gdth_timer_running)
3747 return;
3748 gdth_timer_running = 1;
3749 TRACE2(("gdth_detect(): Initializing timer !\n"));
3750 gdth_timer.expires = jiffies + HZ;
3751 gdth_timer.data = 0L;
3752 gdth_timer.function = gdth_timeout;
3753 add_timer(&gdth_timer);
3754}
3755#else
3756static inline void gdth_timer_init(void)
3757{
3758}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759#endif
3760
8e879042005-04-17 15:28:39 -05003761static void __init internal_setup(char *str,int *ints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762{
3763 int i, argc;
3764 char *cur_str, *argv;
3765
3766 TRACE2(("internal_setup() str %s ints[0] %d\n",
3767 str ? str:"NULL", ints ? ints[0]:0));
3768
3769 /* read irq[] from ints[] */
3770 if (ints) {
3771 argc = ints[0];
3772 if (argc > 0) {
3773 if (argc > MAXHA)
3774 argc = MAXHA;
3775 for (i = 0; i < argc; ++i)
3776 irq[i] = ints[i+1];
3777 }
3778 }
3779
3780 /* analyse string */
3781 argv = str;
3782 while (argv && (cur_str = strchr(argv, ':'))) {
3783 int val = 0, c = *++cur_str;
3784
3785 if (c == 'n' || c == 'N')
3786 val = 0;
3787 else if (c == 'y' || c == 'Y')
3788 val = 1;
3789 else
3790 val = (int)simple_strtoul(cur_str, NULL, 0);
3791
3792 if (!strncmp(argv, "disable:", 8))
3793 disable = val;
3794 else if (!strncmp(argv, "reserve_mode:", 13))
3795 reserve_mode = val;
3796 else if (!strncmp(argv, "reverse_scan:", 13))
3797 reverse_scan = val;
3798 else if (!strncmp(argv, "hdr_channel:", 12))
3799 hdr_channel = val;
3800 else if (!strncmp(argv, "max_ids:", 8))
3801 max_ids = val;
3802 else if (!strncmp(argv, "rescan:", 7))
3803 rescan = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 else if (!strncmp(argv, "shared_access:", 14))
3805 shared_access = val;
3806 else if (!strncmp(argv, "probe_eisa_isa:", 15))
3807 probe_eisa_isa = val;
3808 else if (!strncmp(argv, "reserve_list:", 13)) {
3809 reserve_list[0] = val;
3810 for (i = 1; i < MAX_RES_ARGS; i++) {
3811 cur_str = strchr(cur_str, ',');
3812 if (!cur_str)
3813 break;
3814 if (!isdigit((int)*++cur_str)) {
3815 --cur_str;
3816 break;
3817 }
3818 reserve_list[i] =
3819 (int)simple_strtoul(cur_str, NULL, 0);
3820 }
3821 if (!cur_str)
3822 break;
3823 argv = ++cur_str;
3824 continue;
3825 }
3826
3827 if ((argv = strchr(argv, ',')))
3828 ++argv;
3829 }
3830}
3831
3832int __init option_setup(char *str)
3833{
3834 int ints[MAXHA];
3835 char *cur = str;
3836 int i = 1;
3837
3838 TRACE2(("option_setup() str %s\n", str ? str:"NULL"));
3839
Roel Kluin6ce00ca2010-03-11 14:09:35 -08003840 while (cur && isdigit(*cur) && i < MAXHA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 ints[i++] = simple_strtoul(cur, NULL, 0);
3842 if ((cur = strchr(cur, ',')) != NULL) cur++;
3843 }
3844
3845 ints[0] = i - 1;
3846 internal_setup(cur, ints);
3847 return 1;
3848}
3849
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003850static const char *gdth_ctr_name(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 TRACE2(("gdth_ctr_name()\n"));
3853
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 if (ha->type == GDT_EISA) {
3855 switch (ha->stype) {
3856 case GDT3_ID:
3857 return("GDT3000/3020");
3858 case GDT3A_ID:
3859 return("GDT3000A/3020A/3050A");
3860 case GDT3B_ID:
3861 return("GDT3000B/3010A");
3862 }
3863 } else if (ha->type == GDT_ISA) {
3864 return("GDT2000/2020");
3865 } else if (ha->type == GDT_PCI) {
Jeff Garzik8e9a8a02007-07-17 05:25:17 -04003866 switch (ha->pdev->device) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 case PCI_DEVICE_ID_VORTEX_GDT60x0:
3868 return("GDT6000/6020/6050");
3869 case PCI_DEVICE_ID_VORTEX_GDT6000B:
3870 return("GDT6000B/6010");
3871 }
3872 }
3873 /* new controllers (GDT_PCINEW, GDT_PCIMPR, ..) use board_info IOCTL! */
3874
3875 return("");
3876}
3877
8e879042005-04-17 15:28:39 -05003878static const char *gdth_info(struct Scsi_Host *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879{
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003880 gdth_ha_str *ha = shost_priv(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
3882 TRACE2(("gdth_info()\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 return ((const char *)ha->binfo.type_string);
3884}
3885
Jens Axboe242f9dc2008-09-14 05:55:09 -07003886static enum blk_eh_timer_return gdth_timed_out(struct scsi_cmnd *scp)
3887{
3888 gdth_ha_str *ha = shost_priv(scp->device->host);
3889 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003890 u8 b, t;
3891 unsigned long flags;
Jens Axboe242f9dc2008-09-14 05:55:09 -07003892 enum blk_eh_timer_return retval = BLK_EH_NOT_HANDLED;
3893
3894 TRACE(("%s() cmd 0x%x\n", scp->cmnd[0], __func__));
3895 b = scp->device->channel;
3896 t = scp->device->id;
3897
3898 /*
3899 * We don't really honor the command timeout, but we try to
3900 * honor 6 times of the actual command timeout! So reset the
3901 * timer if this is less than 6th timeout on this command!
3902 */
3903 if (++cmndinfo->timeout_count < 6)
3904 retval = BLK_EH_RESET_TIMER;
3905
3906 /* Reset the timeout if it is locked IO */
3907 spin_lock_irqsave(&ha->smp_lock, flags);
3908 if ((b != ha->virt_bus && ha->raw[BUS_L2P(ha, b)].lock) ||
3909 (b == ha->virt_bus && t < MAX_HDRIVES && ha->hdr[t].lock)) {
3910 TRACE2(("%s(): locked IO, reset timeout\n", __func__));
3911 retval = BLK_EH_RESET_TIMER;
3912 }
3913 spin_unlock_irqrestore(&ha->smp_lock, flags);
3914
3915 return retval;
3916}
3917
3918
8e879042005-04-17 15:28:39 -05003919static int gdth_eh_bus_reset(Scsi_Cmnd *scp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920{
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003921 gdth_ha_str *ha = shost_priv(scp->device->host);
3922 int i;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003923 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 Scsi_Cmnd *cmnd;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003925 u8 b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926
3927 TRACE2(("gdth_eh_bus_reset()\n"));
3928
Christoph Hellwig52759e62007-10-02 22:59:53 +02003929 b = scp->device->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
3931 /* clear command tab */
3932 spin_lock_irqsave(&ha->smp_lock, flags);
3933 for (i = 0; i < GDTH_MAXCMDS; ++i) {
3934 cmnd = ha->cmd_tab[i].cmnd;
3935 if (!SPECIAL_SCP(cmnd) && cmnd->device->channel == b)
3936 ha->cmd_tab[i].cmnd = UNUSED_CMND;
3937 }
3938 spin_unlock_irqrestore(&ha->smp_lock, flags);
3939
3940 if (b == ha->virt_bus) {
3941 /* host drives */
3942 for (i = 0; i < MAX_HDRIVES; ++i) {
3943 if (ha->hdr[i].present) {
3944 spin_lock_irqsave(&ha->smp_lock, flags);
3945 gdth_polling = TRUE;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003946 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 gdth_delay(0);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003948 if (gdth_internal_cmd(ha, CACHESERVICE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 GDT_CLUST_RESET, i, 0, 0))
3950 ha->hdr[i].cluster_type &= ~CLUSTER_RESERVED;
3951 gdth_polling = FALSE;
3952 spin_unlock_irqrestore(&ha->smp_lock, flags);
3953 }
3954 }
3955 } else {
3956 /* raw devices */
3957 spin_lock_irqsave(&ha->smp_lock, flags);
3958 for (i = 0; i < MAXID; ++i)
3959 ha->raw[BUS_L2P(ha,b)].io_cnt[i] = 0;
3960 gdth_polling = TRUE;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003961 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 gdth_delay(0);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003963 gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_RESET_BUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 BUS_L2P(ha,b), 0, 0);
3965 gdth_polling = FALSE;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07003966 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 }
3968 return SUCCESS;
3969}
3970
8e879042005-04-17 15:28:39 -05003971static int gdth_bios_param(struct scsi_device *sdev,struct block_device *bdev,sector_t cap,int *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003973 u8 b, t;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003974 gdth_ha_str *ha = shost_priv(sdev->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 struct scsi_device *sd;
3976 unsigned capacity;
3977
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 sd = sdev;
3979 capacity = cap;
Christoph Hellwig52759e62007-10-02 22:59:53 +02003980 b = sd->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 t = sd->id;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003982 TRACE2(("gdth_bios_param() ha %d bus %d target %d\n", ha->hanum, b, t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983
3984 if (b != ha->virt_bus || ha->hdr[t].heads == 0) {
3985 /* raw device or host drive without mapping information */
3986 TRACE2(("Evaluate mapping\n"));
3987 gdth_eval_mapping(capacity,&ip[2],&ip[0],&ip[1]);
3988 } else {
3989 ip[0] = ha->hdr[t].heads;
3990 ip[1] = ha->hdr[t].secs;
3991 ip[2] = capacity / ip[0] / ip[1];
3992 }
3993
3994 TRACE2(("gdth_bios_param(): %d heads, %d secs, %d cyls\n",
3995 ip[0],ip[1],ip[2]));
3996 return 0;
3997}
3998
3999
Jeff Garzikf2812332010-11-16 02:10:29 -05004000static int gdth_queuecommand_lck(struct scsi_cmnd *scp,
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02004001 void (*done)(struct scsi_cmnd *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002{
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004003 gdth_ha_str *ha = shost_priv(scp->device->host);
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02004004 struct gdth_cmndinfo *cmndinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
4006 TRACE(("gdth_queuecommand() cmd 0x%x\n", scp->cmnd[0]));
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02004007
4008 cmndinfo = gdth_get_cmndinfo(ha);
4009 BUG_ON(!cmndinfo);
4010
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02004011 scp->scsi_done = done;
Jens Axboe242f9dc2008-09-14 05:55:09 -07004012 cmndinfo->timeout_count = 0;
Boaz Harroshf842b642007-10-02 23:16:01 +02004013 cmndinfo->priority = DEFAULT_PRI;
Boaz Harrosh3892d882007-10-02 23:18:03 +02004014
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02004015 return __gdth_queuecommand(ha, scp, cmndinfo);
4016}
4017
Jeff Garzikf2812332010-11-16 02:10:29 -05004018static DEF_SCSI_QCMD(gdth_queuecommand)
4019
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02004020static int __gdth_queuecommand(gdth_ha_str *ha, struct scsi_cmnd *scp,
4021 struct gdth_cmndinfo *cmndinfo)
4022{
4023 scp->host_scribble = (unsigned char *)cmndinfo;
Boaz Harroshf842b642007-10-02 23:16:01 +02004024 cmndinfo->wait_for_completion = 1;
4025 cmndinfo->phase = -1;
4026 cmndinfo->OpCode = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028#ifdef GDTH_STATISTICS
4029 ++act_ios;
4030#endif
4031
Boaz Harroshf842b642007-10-02 23:16:01 +02004032 gdth_putq(ha, scp, cmndinfo->priority);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004033 gdth_next(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 return 0;
4035}
4036
4037
4038static int gdth_open(struct inode *inode, struct file *filep)
4039{
4040 gdth_ha_str *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
Arnd Bergmannc45d15d2010-06-02 14:28:52 +02004042 mutex_lock(&gdth_mutex);
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004043 list_for_each_entry(ha, &gdth_instances, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 if (!ha->sdev)
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004045 ha->sdev = scsi_get_host_dev(ha->shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 }
Arnd Bergmannc45d15d2010-06-02 14:28:52 +02004047 mutex_unlock(&gdth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048
4049 TRACE(("gdth_open()\n"));
4050 return 0;
4051}
4052
4053static int gdth_close(struct inode *inode, struct file *filep)
4054{
4055 TRACE(("gdth_close()\n"));
4056 return 0;
4057}
4058
4059static int ioc_event(void __user *arg)
4060{
4061 gdth_ioctl_event evt;
4062 gdth_ha_str *ha;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004063 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004065 if (copy_from_user(&evt, arg, sizeof(gdth_ioctl_event)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004067 ha = gdth_find_ha(evt.ionode);
4068 if (!ha)
4069 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070
4071 if (evt.erase == 0xff) {
4072 if (evt.event.event_source == ES_TEST)
4073 evt.event.event_data.size=sizeof(evt.event.event_data.eu.test);
4074 else if (evt.event.event_source == ES_DRIVER)
4075 evt.event.event_data.size=sizeof(evt.event.event_data.eu.driver);
4076 else if (evt.event.event_source == ES_SYNC)
4077 evt.event.event_data.size=sizeof(evt.event.event_data.eu.sync);
4078 else
4079 evt.event.event_data.size=sizeof(evt.event.event_data.eu.async);
4080 spin_lock_irqsave(&ha->smp_lock, flags);
4081 gdth_store_event(ha, evt.event.event_source, evt.event.event_idx,
4082 &evt.event.event_data);
4083 spin_unlock_irqrestore(&ha->smp_lock, flags);
4084 } else if (evt.erase == 0xfe) {
4085 gdth_clear_events();
4086 } else if (evt.erase == 0) {
4087 evt.handle = gdth_read_event(ha, evt.handle, &evt.event);
4088 } else {
4089 gdth_readapp_event(ha, evt.erase, &evt.event);
4090 }
4091 if (copy_to_user(arg, &evt, sizeof(gdth_ioctl_event)))
4092 return -EFAULT;
4093 return 0;
4094}
4095
4096static int ioc_lockdrv(void __user *arg)
4097{
4098 gdth_ioctl_lockdrv ldrv;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004099 u8 i, j;
4100 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 gdth_ha_str *ha;
4102
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004103 if (copy_from_user(&ldrv, arg, sizeof(gdth_ioctl_lockdrv)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004105 ha = gdth_find_ha(ldrv.ionode);
4106 if (!ha)
4107 return -EFAULT;
4108
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 for (i = 0; i < ldrv.drive_cnt && i < MAX_HDRIVES; ++i) {
4110 j = ldrv.drives[i];
4111 if (j >= MAX_HDRIVES || !ha->hdr[j].present)
4112 continue;
4113 if (ldrv.lock) {
4114 spin_lock_irqsave(&ha->smp_lock, flags);
4115 ha->hdr[j].lock = 1;
4116 spin_unlock_irqrestore(&ha->smp_lock, flags);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004117 gdth_wait_completion(ha, ha->bus_cnt, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 } else {
4119 spin_lock_irqsave(&ha->smp_lock, flags);
4120 ha->hdr[j].lock = 0;
4121 spin_unlock_irqrestore(&ha->smp_lock, flags);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004122 gdth_next(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 }
4124 }
4125 return 0;
4126}
4127
4128static int ioc_resetdrv(void __user *arg, char *cmnd)
4129{
4130 gdth_ioctl_reset res;
4131 gdth_cmd_str cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 gdth_ha_str *ha;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004133 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134
4135 if (copy_from_user(&res, arg, sizeof(gdth_ioctl_reset)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004136 res.number >= MAX_HDRIVES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004138 ha = gdth_find_ha(res.ionode);
4139 if (!ha)
4140 return -EFAULT;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004141
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 if (!ha->hdr[res.number].present)
4143 return 0;
4144 memset(&cmd, 0, sizeof(gdth_cmd_str));
4145 cmd.Service = CACHESERVICE;
4146 cmd.OpCode = GDT_CLUST_RESET;
4147 if (ha->cache_feat & GDT_64BIT)
4148 cmd.u.cache64.DeviceNo = res.number;
4149 else
4150 cmd.u.cache.DeviceNo = res.number;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004151
4152 rval = __gdth_execute(ha->sdev, &cmd, cmnd, 30, NULL);
4153 if (rval < 0)
4154 return rval;
4155 res.status = rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156
4157 if (copy_to_user(arg, &res, sizeof(gdth_ioctl_reset)))
4158 return -EFAULT;
4159 return 0;
4160}
4161
4162static int ioc_general(void __user *arg, char *cmnd)
4163{
4164 gdth_ioctl_general gen;
4165 char *buf = NULL;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004166 u64 paddr;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004167 gdth_ha_str *ha;
4168 int rval;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004169
4170 if (copy_from_user(&gen, arg, sizeof(gdth_ioctl_general)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004172 ha = gdth_find_ha(gen.ionode);
4173 if (!ha)
4174 return -EFAULT;
Dan Carpenterf63ae562010-10-08 09:03:07 +02004175
4176 if (gen.data_len > INT_MAX)
4177 return -EINVAL;
4178 if (gen.sense_len > INT_MAX)
4179 return -EINVAL;
4180 if (gen.data_len + gen.sense_len > INT_MAX)
4181 return -EINVAL;
4182
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 if (gen.data_len + gen.sense_len != 0) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004184 if (!(buf = gdth_ioctl_alloc(ha, gen.data_len + gen.sense_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 FALSE, &paddr)))
4186 return -EFAULT;
4187 if (copy_from_user(buf, arg + sizeof(gdth_ioctl_general),
4188 gen.data_len + gen.sense_len)) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004189 gdth_ioctl_free(ha, gen.data_len+gen.sense_len, buf, paddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 return -EFAULT;
4191 }
4192
4193 if (gen.command.OpCode == GDT_IOCTL) {
4194 gen.command.u.ioctl.p_param = paddr;
4195 } else if (gen.command.Service == CACHESERVICE) {
4196 if (ha->cache_feat & GDT_64BIT) {
4197 /* copy elements from 32-bit IOCTL structure */
4198 gen.command.u.cache64.BlockCnt = gen.command.u.cache.BlockCnt;
4199 gen.command.u.cache64.BlockNo = gen.command.u.cache.BlockNo;
4200 gen.command.u.cache64.DeviceNo = gen.command.u.cache.DeviceNo;
4201 /* addresses */
4202 if (ha->cache_feat & SCATTER_GATHER) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004203 gen.command.u.cache64.DestAddr = (u64)-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 gen.command.u.cache64.sg_canz = 1;
4205 gen.command.u.cache64.sg_lst[0].sg_ptr = paddr;
4206 gen.command.u.cache64.sg_lst[0].sg_len = gen.data_len;
4207 gen.command.u.cache64.sg_lst[1].sg_len = 0;
4208 } else {
4209 gen.command.u.cache64.DestAddr = paddr;
4210 gen.command.u.cache64.sg_canz = 0;
4211 }
4212 } else {
4213 if (ha->cache_feat & SCATTER_GATHER) {
4214 gen.command.u.cache.DestAddr = 0xffffffff;
4215 gen.command.u.cache.sg_canz = 1;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004216 gen.command.u.cache.sg_lst[0].sg_ptr = (u32)paddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 gen.command.u.cache.sg_lst[0].sg_len = gen.data_len;
4218 gen.command.u.cache.sg_lst[1].sg_len = 0;
4219 } else {
4220 gen.command.u.cache.DestAddr = paddr;
4221 gen.command.u.cache.sg_canz = 0;
4222 }
4223 }
4224 } else if (gen.command.Service == SCSIRAWSERVICE) {
4225 if (ha->raw_feat & GDT_64BIT) {
4226 /* copy elements from 32-bit IOCTL structure */
4227 char cmd[16];
4228 gen.command.u.raw64.sense_len = gen.command.u.raw.sense_len;
4229 gen.command.u.raw64.bus = gen.command.u.raw.bus;
4230 gen.command.u.raw64.lun = gen.command.u.raw.lun;
4231 gen.command.u.raw64.target = gen.command.u.raw.target;
4232 memcpy(cmd, gen.command.u.raw.cmd, 16);
4233 memcpy(gen.command.u.raw64.cmd, cmd, 16);
4234 gen.command.u.raw64.clen = gen.command.u.raw.clen;
4235 gen.command.u.raw64.sdlen = gen.command.u.raw.sdlen;
4236 gen.command.u.raw64.direction = gen.command.u.raw.direction;
4237 /* addresses */
4238 if (ha->raw_feat & SCATTER_GATHER) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004239 gen.command.u.raw64.sdata = (u64)-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 gen.command.u.raw64.sg_ranz = 1;
4241 gen.command.u.raw64.sg_lst[0].sg_ptr = paddr;
4242 gen.command.u.raw64.sg_lst[0].sg_len = gen.data_len;
4243 gen.command.u.raw64.sg_lst[1].sg_len = 0;
4244 } else {
4245 gen.command.u.raw64.sdata = paddr;
4246 gen.command.u.raw64.sg_ranz = 0;
4247 }
4248 gen.command.u.raw64.sense_data = paddr + gen.data_len;
4249 } else {
4250 if (ha->raw_feat & SCATTER_GATHER) {
4251 gen.command.u.raw.sdata = 0xffffffff;
4252 gen.command.u.raw.sg_ranz = 1;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004253 gen.command.u.raw.sg_lst[0].sg_ptr = (u32)paddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 gen.command.u.raw.sg_lst[0].sg_len = gen.data_len;
4255 gen.command.u.raw.sg_lst[1].sg_len = 0;
4256 } else {
4257 gen.command.u.raw.sdata = paddr;
4258 gen.command.u.raw.sg_ranz = 0;
4259 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004260 gen.command.u.raw.sense_data = (u32)paddr + gen.data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 }
4262 } else {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004263 gdth_ioctl_free(ha, gen.data_len+gen.sense_len, buf, paddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 return -EFAULT;
4265 }
4266 }
4267
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004268 rval = __gdth_execute(ha->sdev, &gen.command, cmnd, gen.timeout, &gen.info);
Julia Lawall5c100072010-12-29 15:57:54 +01004269 if (rval < 0) {
4270 gdth_ioctl_free(ha, gen.data_len+gen.sense_len, buf, paddr);
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004271 return rval;
Julia Lawall5c100072010-12-29 15:57:54 +01004272 }
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004273 gen.status = rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
4275 if (copy_to_user(arg + sizeof(gdth_ioctl_general), buf,
4276 gen.data_len + gen.sense_len)) {
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 -EFAULT;
4279 }
4280 if (copy_to_user(arg, &gen,
4281 sizeof(gdth_ioctl_general) - sizeof(gdth_cmd_str))) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004282 gdth_ioctl_free(ha, gen.data_len+gen.sense_len, buf, paddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 return -EFAULT;
4284 }
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004285 gdth_ioctl_free(ha, gen.data_len+gen.sense_len, buf, paddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 return 0;
4287}
4288
4289static int ioc_hdrlist(void __user *arg, char *cmnd)
4290{
4291 gdth_ioctl_rescan *rsc;
4292 gdth_cmd_str *cmd;
4293 gdth_ha_str *ha;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004294 u8 i;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004295 int rc = -ENOMEM;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004296 u32 cluster_type = 0;
4297
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 rsc = kmalloc(sizeof(*rsc), GFP_KERNEL);
4299 cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
4300 if (!rsc || !cmd)
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004301 goto free_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302
4303 if (copy_from_user(rsc, arg, sizeof(gdth_ioctl_rescan)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004304 (NULL == (ha = gdth_find_ha(rsc->ionode)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 rc = -EFAULT;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004306 goto free_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 memset(cmd, 0, sizeof(gdth_cmd_str));
4309
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 for (i = 0; i < MAX_HDRIVES; ++i) {
4311 if (!ha->hdr[i].present) {
4312 rsc->hdr_list[i].bus = 0xff;
4313 continue;
4314 }
4315 rsc->hdr_list[i].bus = ha->virt_bus;
4316 rsc->hdr_list[i].target = i;
4317 rsc->hdr_list[i].lun = 0;
4318 rsc->hdr_list[i].cluster_type = ha->hdr[i].cluster_type;
4319 if (ha->hdr[i].cluster_type & CLUSTER_DRIVE) {
4320 cmd->Service = CACHESERVICE;
4321 cmd->OpCode = GDT_CLUST_INFO;
4322 if (ha->cache_feat & GDT_64BIT)
4323 cmd->u.cache64.DeviceNo = i;
4324 else
4325 cmd->u.cache.DeviceNo = i;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004326 if (__gdth_execute(ha->sdev, cmd, cmnd, 30, &cluster_type) == S_OK)
4327 rsc->hdr_list[i].cluster_type = cluster_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 }
4329 }
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004330
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 if (copy_to_user(arg, rsc, sizeof(gdth_ioctl_rescan)))
4332 rc = -EFAULT;
4333 else
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004334 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335
4336free_fail:
4337 kfree(rsc);
4338 kfree(cmd);
4339 return rc;
4340}
4341
4342static int ioc_rescan(void __user *arg, char *cmnd)
4343{
4344 gdth_ioctl_rescan *rsc;
4345 gdth_cmd_str *cmd;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004346 u16 i, status, hdr_cnt;
4347 u32 info;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004348 int cyls, hds, secs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 int rc = -ENOMEM;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004350 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 gdth_ha_str *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352
4353 rsc = kmalloc(sizeof(*rsc), GFP_KERNEL);
4354 cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
4355 if (!cmd || !rsc)
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004356 goto free_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
4358 if (copy_from_user(rsc, arg, sizeof(gdth_ioctl_rescan)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004359 (NULL == (ha = gdth_find_ha(rsc->ionode)))) {
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004360 rc = -EFAULT;
4361 goto free_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 memset(cmd, 0, sizeof(gdth_cmd_str));
4364
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 if (rsc->flag == 0) {
4366 /* old method: re-init. cache service */
4367 cmd->Service = CACHESERVICE;
4368 if (ha->cache_feat & GDT_64BIT) {
4369 cmd->OpCode = GDT_X_INIT_HOST;
4370 cmd->u.cache64.DeviceNo = LINUX_OS;
4371 } else {
4372 cmd->OpCode = GDT_INIT;
4373 cmd->u.cache.DeviceNo = LINUX_OS;
4374 }
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004375
4376 status = __gdth_execute(ha->sdev, cmd, cmnd, 30, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 i = 0;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004378 hdr_cnt = (status == S_OK ? (u16)info : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 } else {
4380 i = rsc->hdr_no;
4381 hdr_cnt = i + 1;
4382 }
4383
4384 for (; i < hdr_cnt && i < MAX_HDRIVES; ++i) {
4385 cmd->Service = CACHESERVICE;
4386 cmd->OpCode = GDT_INFO;
4387 if (ha->cache_feat & GDT_64BIT)
4388 cmd->u.cache64.DeviceNo = i;
4389 else
4390 cmd->u.cache.DeviceNo = i;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004391
4392 status = __gdth_execute(ha->sdev, cmd, cmnd, 30, &info);
4393
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 spin_lock_irqsave(&ha->smp_lock, flags);
4395 rsc->hdr_list[i].bus = ha->virt_bus;
4396 rsc->hdr_list[i].target = i;
4397 rsc->hdr_list[i].lun = 0;
4398 if (status != S_OK) {
4399 ha->hdr[i].present = FALSE;
4400 } else {
4401 ha->hdr[i].present = TRUE;
4402 ha->hdr[i].size = info;
4403 /* evaluate mapping */
4404 ha->hdr[i].size &= ~SECS32;
4405 gdth_eval_mapping(ha->hdr[i].size,&cyls,&hds,&secs);
4406 ha->hdr[i].heads = hds;
4407 ha->hdr[i].secs = secs;
4408 /* round size */
4409 ha->hdr[i].size = cyls * hds * secs;
4410 }
4411 spin_unlock_irqrestore(&ha->smp_lock, flags);
4412 if (status != S_OK)
4413 continue;
4414
4415 /* extended info, if GDT_64BIT, for drives > 2 TB */
4416 /* but we need ha->info2, not yet stored in scp->SCp */
4417
4418 /* devtype, cluster info, R/W attribs */
4419 cmd->Service = CACHESERVICE;
4420 cmd->OpCode = GDT_DEVTYPE;
4421 if (ha->cache_feat & GDT_64BIT)
4422 cmd->u.cache64.DeviceNo = i;
4423 else
4424 cmd->u.cache.DeviceNo = i;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004425
4426 status = __gdth_execute(ha->sdev, cmd, cmnd, 30, &info);
4427
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 spin_lock_irqsave(&ha->smp_lock, flags);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004429 ha->hdr[i].devtype = (status == S_OK ? (u16)info : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 spin_unlock_irqrestore(&ha->smp_lock, flags);
4431
4432 cmd->Service = CACHESERVICE;
4433 cmd->OpCode = GDT_CLUST_INFO;
4434 if (ha->cache_feat & GDT_64BIT)
4435 cmd->u.cache64.DeviceNo = i;
4436 else
4437 cmd->u.cache.DeviceNo = i;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004438
4439 status = __gdth_execute(ha->sdev, cmd, cmnd, 30, &info);
4440
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 spin_lock_irqsave(&ha->smp_lock, flags);
4442 ha->hdr[i].cluster_type =
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004443 ((status == S_OK && !shared_access) ? (u16)info : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 spin_unlock_irqrestore(&ha->smp_lock, flags);
4445 rsc->hdr_list[i].cluster_type = ha->hdr[i].cluster_type;
4446
4447 cmd->Service = CACHESERVICE;
4448 cmd->OpCode = GDT_RW_ATTRIBS;
4449 if (ha->cache_feat & GDT_64BIT)
4450 cmd->u.cache64.DeviceNo = i;
4451 else
4452 cmd->u.cache.DeviceNo = i;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004453
4454 status = __gdth_execute(ha->sdev, cmd, cmnd, 30, &info);
4455
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 spin_lock_irqsave(&ha->smp_lock, flags);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004457 ha->hdr[i].rw_attribs = (status == S_OK ? (u16)info : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 spin_unlock_irqrestore(&ha->smp_lock, flags);
4459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460
4461 if (copy_to_user(arg, rsc, sizeof(gdth_ioctl_rescan)))
4462 rc = -EFAULT;
4463 else
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004464 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465
4466free_fail:
4467 kfree(rsc);
4468 kfree(cmd);
4469 return rc;
4470}
4471
Arnd Bergmannf4927c42010-04-27 00:24:01 +02004472static int gdth_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473{
4474 gdth_ha_str *ha;
4475 Scsi_Cmnd *scp;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004476 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 char cmnd[MAX_COMMAND_SIZE];
4478 void __user *argp = (void __user *)arg;
4479
4480 memset(cmnd, 0xff, 12);
4481
4482 TRACE(("gdth_ioctl() cmd 0x%x\n", cmd));
4483
4484 switch (cmd) {
4485 case GDTIOCTL_CTRCNT:
4486 {
4487 int cnt = gdth_ctr_count;
4488 if (put_user(cnt, (int __user *)argp))
4489 return -EFAULT;
4490 break;
4491 }
4492
4493 case GDTIOCTL_DRVERS:
4494 {
4495 int ver = (GDTH_VERSION<<8) | GDTH_SUBVERSION;
4496 if (put_user(ver, (int __user *)argp))
4497 return -EFAULT;
4498 break;
4499 }
4500
4501 case GDTIOCTL_OSVERS:
4502 {
4503 gdth_ioctl_osvers osv;
4504
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004505 osv.version = (u8)(LINUX_VERSION_CODE >> 16);
4506 osv.subversion = (u8)(LINUX_VERSION_CODE >> 8);
4507 osv.revision = (u16)(LINUX_VERSION_CODE & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 if (copy_to_user(argp, &osv, sizeof(gdth_ioctl_osvers)))
4509 return -EFAULT;
4510 break;
4511 }
4512
4513 case GDTIOCTL_CTRTYPE:
4514 {
4515 gdth_ioctl_ctrtype ctrt;
4516
4517 if (copy_from_user(&ctrt, argp, sizeof(gdth_ioctl_ctrtype)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004518 (NULL == (ha = gdth_find_ha(ctrt.ionode))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004520
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 if (ha->type == GDT_ISA || ha->type == GDT_EISA) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004522 ctrt.type = (u8)((ha->stype>>20) - 0x10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 } else {
4524 if (ha->type != GDT_PCIMPR) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004525 ctrt.type = (u8)((ha->stype<<4) + 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 } else {
4527 ctrt.type =
4528 (ha->oem_id == OEM_ID_INTEL ? 0xfd : 0xfe);
4529 if (ha->stype >= 0x300)
Jeff Garzik8e9a8a02007-07-17 05:25:17 -04004530 ctrt.ext_type = 0x6000 | ha->pdev->subsystem_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 else
4532 ctrt.ext_type = 0x6000 | ha->stype;
4533 }
Jeff Garzik8e9a8a02007-07-17 05:25:17 -04004534 ctrt.device_id = ha->pdev->device;
4535 ctrt.sub_device_id = ha->pdev->subsystem_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 }
4537 ctrt.info = ha->brd_phys;
4538 ctrt.oem_id = ha->oem_id;
4539 if (copy_to_user(argp, &ctrt, sizeof(gdth_ioctl_ctrtype)))
4540 return -EFAULT;
4541 break;
4542 }
4543
4544 case GDTIOCTL_GENERAL:
4545 return ioc_general(argp, cmnd);
4546
4547 case GDTIOCTL_EVENT:
4548 return ioc_event(argp);
4549
4550 case GDTIOCTL_LOCKDRV:
4551 return ioc_lockdrv(argp);
4552
4553 case GDTIOCTL_LOCKCHN:
4554 {
4555 gdth_ioctl_lockchn lchn;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004556 u8 i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557
4558 if (copy_from_user(&lchn, argp, sizeof(gdth_ioctl_lockchn)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004559 (NULL == (ha = gdth_find_ha(lchn.ionode))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004561
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 i = lchn.channel;
4563 if (i < ha->bus_cnt) {
4564 if (lchn.lock) {
4565 spin_lock_irqsave(&ha->smp_lock, flags);
4566 ha->raw[i].lock = 1;
4567 spin_unlock_irqrestore(&ha->smp_lock, flags);
Jens Axboe242f9dc2008-09-14 05:55:09 -07004568 for (j = 0; j < ha->tid_cnt; ++j)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004569 gdth_wait_completion(ha, i, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 } else {
4571 spin_lock_irqsave(&ha->smp_lock, flags);
4572 ha->raw[i].lock = 0;
4573 spin_unlock_irqrestore(&ha->smp_lock, flags);
Jens Axboe242f9dc2008-09-14 05:55:09 -07004574 for (j = 0; j < ha->tid_cnt; ++j)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004575 gdth_next(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 }
4577 }
4578 break;
4579 }
4580
4581 case GDTIOCTL_RESCAN:
4582 return ioc_rescan(argp, cmnd);
4583
4584 case GDTIOCTL_HDRLIST:
4585 return ioc_hdrlist(argp, cmnd);
4586
4587 case GDTIOCTL_RESET_BUS:
4588 {
4589 gdth_ioctl_reset res;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004590 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591
4592 if (copy_from_user(&res, argp, sizeof(gdth_ioctl_reset)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004593 (NULL == (ha = gdth_find_ha(res.ionode))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02004596 scp = kzalloc(sizeof(*scp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 if (!scp)
4598 return -ENOMEM;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004599 scp->device = ha->sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 scp->cmd_len = 12;
Christoph Hellwig52759e62007-10-02 22:59:53 +02004601 scp->device->channel = res.number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 rval = gdth_eh_bus_reset(scp);
4603 res.status = (rval == SUCCESS ? S_OK : S_GENERR);
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004604 kfree(scp);
Jeff Garzik8d7a5da2007-10-02 22:54:28 +02004605
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 if (copy_to_user(argp, &res, sizeof(gdth_ioctl_reset)))
4607 return -EFAULT;
4608 break;
4609 }
4610
4611 case GDTIOCTL_RESET_DRV:
4612 return ioc_resetdrv(argp, cmnd);
4613
4614 default:
4615 break;
4616 }
4617 return 0;
4618}
4619
Arnd Bergmannf4927c42010-04-27 00:24:01 +02004620static long gdth_unlocked_ioctl(struct file *file, unsigned int cmd,
4621 unsigned long arg)
4622{
4623 int ret;
4624
Arnd Bergmannc45d15d2010-06-02 14:28:52 +02004625 mutex_lock(&gdth_mutex);
Arnd Bergmannf4927c42010-04-27 00:24:01 +02004626 ret = gdth_ioctl(file, cmd, arg);
Arnd Bergmannc45d15d2010-06-02 14:28:52 +02004627 mutex_unlock(&gdth_mutex);
Arnd Bergmannf4927c42010-04-27 00:24:01 +02004628
4629 return ret;
4630}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631
4632/* flush routine */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004633static void gdth_flush(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634{
4635 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 gdth_cmd_str gdtcmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 char cmnd[MAX_COMMAND_SIZE];
4638 memset(cmnd, 0xff, MAX_COMMAND_SIZE);
4639
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004640 TRACE2(("gdth_flush() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 for (i = 0; i < MAX_HDRIVES; ++i) {
4643 if (ha->hdr[i].present) {
4644 gdtcmd.BoardNode = LOCALBOARD;
4645 gdtcmd.Service = CACHESERVICE;
4646 gdtcmd.OpCode = GDT_FLUSH;
4647 if (ha->cache_feat & GDT_64BIT) {
4648 gdtcmd.u.cache64.DeviceNo = i;
4649 gdtcmd.u.cache64.BlockNo = 1;
4650 gdtcmd.u.cache64.sg_canz = 0;
4651 } else {
4652 gdtcmd.u.cache.DeviceNo = i;
4653 gdtcmd.u.cache.BlockNo = 1;
4654 gdtcmd.u.cache.sg_canz = 0;
4655 }
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004656 TRACE2(("gdth_flush(): flush ha %d drive %d\n", ha->hanum, i));
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004657
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004658 gdth_execute(ha->shost, &gdtcmd, cmnd, 30, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 }
4660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661}
4662
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004663/* configure lun */
4664static int gdth_slave_configure(struct scsi_device *sdev)
4665{
4666 scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
4667 sdev->skip_ms_page_3f = 1;
4668 sdev->skip_ms_page_8 = 1;
4669 return 0;
4670}
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004671
Christoph Hellwig835cc242007-10-02 23:09:56 +02004672static struct scsi_host_template gdth_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 .name = "GDT SCSI Disk Array Controller",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 .info = gdth_info,
4675 .queuecommand = gdth_queuecommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 .eh_bus_reset_handler = gdth_eh_bus_reset,
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004677 .slave_configure = gdth_slave_configure,
Jeff Garzik69916b72007-10-02 22:57:58 +02004678 .bios_param = gdth_bios_param,
Al Viro3e0552e2013-03-31 00:52:08 -04004679 .show_info = gdth_show_info,
4680 .write_info = gdth_set_info,
Jens Axboe242f9dc2008-09-14 05:55:09 -07004681 .eh_timed_out = gdth_timed_out,
Jeff Garzik69916b72007-10-02 22:57:58 +02004682 .proc_name = "gdth",
4683 .can_queue = GDTH_MAXCMDS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 .this_id = -1,
4685 .sg_tablesize = GDTH_MAXSG,
4686 .cmd_per_lun = GDTH_MAXC_P_L,
4687 .unchecked_isa_dma = 1,
4688 .use_clustering = ENABLE_CLUSTERING,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689};
4690
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004691#ifdef CONFIG_ISA
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004692static int __init gdth_isa_probe_one(u32 isa_bios)
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004693{
4694 struct Scsi_Host *shp;
4695 gdth_ha_str *ha;
4696 dma_addr_t scratch_dma_handle = 0;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004697 int error, i;
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004698
4699 if (!gdth_search_isa(isa_bios))
4700 return -ENXIO;
4701
Christoph Hellwig835cc242007-10-02 23:09:56 +02004702 shp = scsi_host_alloc(&gdth_template, sizeof(gdth_ha_str));
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004703 if (!shp)
4704 return -ENOMEM;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004705 ha = shost_priv(shp);
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004706
4707 error = -ENODEV;
4708 if (!gdth_init_isa(isa_bios,ha))
4709 goto out_host_put;
4710
4711 /* controller found and initialized */
4712 printk("Configuring GDT-ISA HA at BIOS 0x%05X IRQ %u DRQ %u\n",
4713 isa_bios, ha->irq, ha->drq);
4714
4715 error = request_irq(ha->irq, gdth_interrupt, IRQF_DISABLED, "gdth", ha);
4716 if (error) {
4717 printk("GDT-ISA: Unable to allocate IRQ\n");
4718 goto out_host_put;
4719 }
4720
4721 error = request_dma(ha->drq, "gdth");
4722 if (error) {
4723 printk("GDT-ISA: Unable to allocate DMA channel\n");
4724 goto out_free_irq;
4725 }
4726
4727 set_dma_mode(ha->drq,DMA_MODE_CASCADE);
4728 enable_dma(ha->drq);
4729 shp->unchecked_isa_dma = 1;
4730 shp->irq = ha->irq;
4731 shp->dma_channel = ha->drq;
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004732
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004733 ha->hanum = gdth_ctr_count++;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004734 ha->shost = shp;
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004735
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004736 ha->pccb = &ha->cmdext;
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004737 ha->ccb_phys = 0L;
4738 ha->pdev = NULL;
4739
4740 error = -ENOMEM;
4741
4742 ha->pscratch = pci_alloc_consistent(ha->pdev, GDTH_SCRATCH,
4743 &scratch_dma_handle);
4744 if (!ha->pscratch)
4745 goto out_dec_counters;
4746 ha->scratch_phys = scratch_dma_handle;
4747
4748 ha->pmsg = pci_alloc_consistent(ha->pdev, sizeof(gdth_msg_str),
4749 &scratch_dma_handle);
4750 if (!ha->pmsg)
4751 goto out_free_pscratch;
4752 ha->msg_phys = scratch_dma_handle;
4753
4754#ifdef INT_COAL
4755 ha->coal_stat = pci_alloc_consistent(ha->pdev,
4756 sizeof(gdth_coal_status) * MAXOFFSETS,
4757 &scratch_dma_handle);
4758 if (!ha->coal_stat)
4759 goto out_free_pmsg;
4760 ha->coal_stat_phys = scratch_dma_handle;
4761#endif
4762
4763 ha->scratch_busy = FALSE;
4764 ha->req_first = NULL;
4765 ha->tid_cnt = MAX_HDRIVES;
4766 if (max_ids > 0 && max_ids < ha->tid_cnt)
4767 ha->tid_cnt = max_ids;
4768 for (i = 0; i < GDTH_MAXCMDS; ++i)
4769 ha->cmd_tab[i].cmnd = UNUSED_CMND;
4770 ha->scan_mode = rescan ? 0x10 : 0;
4771
4772 error = -ENODEV;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004773 if (!gdth_search_drives(ha)) {
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004774 printk("GDT-ISA: Error during device scan\n");
4775 goto out_free_coal_stat;
4776 }
4777
4778 if (hdr_channel < 0 || hdr_channel > ha->bus_cnt)
4779 hdr_channel = ha->bus_cnt;
4780 ha->virt_bus = hdr_channel;
4781
4782 if (ha->cache_feat & ha->raw_feat & ha->screen_feat & GDT_64BIT)
4783 shp->max_cmd_len = 16;
4784
4785 shp->max_id = ha->tid_cnt;
4786 shp->max_lun = MAXLUN;
Christoph Hellwig52759e62007-10-02 22:59:53 +02004787 shp->max_channel = ha->bus_cnt;
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004788
4789 spin_lock_init(&ha->smp_lock);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004790 gdth_enable_int(ha);
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004791
Christoph Hellwig835cc242007-10-02 23:09:56 +02004792 error = scsi_add_host(shp, NULL);
4793 if (error)
4794 goto out_free_coal_stat;
4795 list_add_tail(&ha->list, &gdth_instances);
James Bottomley2d6f0d02008-05-04 22:34:49 -05004796 gdth_timer_init();
Boaz Harrosh61c92812008-02-12 19:35:22 +02004797
4798 scsi_scan_host(shp);
4799
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004800 return 0;
4801
4802 out_free_coal_stat:
4803#ifdef INT_COAL
4804 pci_free_consistent(ha->pdev, sizeof(gdth_coal_status) * MAXOFFSETS,
4805 ha->coal_stat, ha->coal_stat_phys);
4806 out_free_pmsg:
4807#endif
4808 pci_free_consistent(ha->pdev, sizeof(gdth_msg_str),
4809 ha->pmsg, ha->msg_phys);
4810 out_free_pscratch:
4811 pci_free_consistent(ha->pdev, GDTH_SCRATCH,
4812 ha->pscratch, ha->scratch_phys);
4813 out_dec_counters:
4814 gdth_ctr_count--;
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004815 out_free_irq:
4816 free_irq(ha->irq, ha);
4817 out_host_put:
Christoph Hellwig835cc242007-10-02 23:09:56 +02004818 scsi_host_put(shp);
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004819 return error;
4820}
4821#endif /* CONFIG_ISA */
4822
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004823#ifdef CONFIG_EISA
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004824static int __init gdth_eisa_probe_one(u16 eisa_slot)
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004825{
4826 struct Scsi_Host *shp;
4827 gdth_ha_str *ha;
4828 dma_addr_t scratch_dma_handle = 0;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004829 int error, i;
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004830
4831 if (!gdth_search_eisa(eisa_slot))
4832 return -ENXIO;
4833
Christoph Hellwig835cc242007-10-02 23:09:56 +02004834 shp = scsi_host_alloc(&gdth_template, sizeof(gdth_ha_str));
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004835 if (!shp)
4836 return -ENOMEM;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004837 ha = shost_priv(shp);
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004838
4839 error = -ENODEV;
4840 if (!gdth_init_eisa(eisa_slot,ha))
4841 goto out_host_put;
4842
4843 /* controller found and initialized */
4844 printk("Configuring GDT-EISA HA at Slot %d IRQ %u\n",
4845 eisa_slot >> 12, ha->irq);
4846
4847 error = request_irq(ha->irq, gdth_interrupt, IRQF_DISABLED, "gdth", ha);
4848 if (error) {
4849 printk("GDT-EISA: Unable to allocate IRQ\n");
4850 goto out_host_put;
4851 }
4852
4853 shp->unchecked_isa_dma = 0;
4854 shp->irq = ha->irq;
4855 shp->dma_channel = 0xff;
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004856
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004857 ha->hanum = gdth_ctr_count++;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004858 ha->shost = shp;
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004859
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004860 TRACE2(("EISA detect Bus 0: hanum %d\n", ha->hanum));
4861
4862 ha->pccb = &ha->cmdext;
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004863 ha->ccb_phys = 0L;
4864
4865 error = -ENOMEM;
4866
4867 ha->pdev = NULL;
4868 ha->pscratch = pci_alloc_consistent(ha->pdev, GDTH_SCRATCH,
4869 &scratch_dma_handle);
4870 if (!ha->pscratch)
4871 goto out_free_irq;
4872 ha->scratch_phys = scratch_dma_handle;
4873
4874 ha->pmsg = pci_alloc_consistent(ha->pdev, sizeof(gdth_msg_str),
4875 &scratch_dma_handle);
4876 if (!ha->pmsg)
4877 goto out_free_pscratch;
4878 ha->msg_phys = scratch_dma_handle;
4879
4880#ifdef INT_COAL
4881 ha->coal_stat = pci_alloc_consistent(ha->pdev,
4882 sizeof(gdth_coal_status) * MAXOFFSETS,
4883 &scratch_dma_handle);
4884 if (!ha->coal_stat)
4885 goto out_free_pmsg;
4886 ha->coal_stat_phys = scratch_dma_handle;
4887#endif
4888
4889 ha->ccb_phys = pci_map_single(ha->pdev,ha->pccb,
4890 sizeof(gdth_cmd_str), PCI_DMA_BIDIRECTIONAL);
4891 if (!ha->ccb_phys)
4892 goto out_free_coal_stat;
4893
4894 ha->scratch_busy = FALSE;
4895 ha->req_first = NULL;
4896 ha->tid_cnt = MAX_HDRIVES;
4897 if (max_ids > 0 && max_ids < ha->tid_cnt)
4898 ha->tid_cnt = max_ids;
4899 for (i = 0; i < GDTH_MAXCMDS; ++i)
4900 ha->cmd_tab[i].cmnd = UNUSED_CMND;
4901 ha->scan_mode = rescan ? 0x10 : 0;
4902
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004903 if (!gdth_search_drives(ha)) {
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004904 printk("GDT-EISA: Error during device scan\n");
4905 error = -ENODEV;
4906 goto out_free_ccb_phys;
4907 }
4908
4909 if (hdr_channel < 0 || hdr_channel > ha->bus_cnt)
4910 hdr_channel = ha->bus_cnt;
4911 ha->virt_bus = hdr_channel;
4912
4913 if (ha->cache_feat & ha->raw_feat & ha->screen_feat & GDT_64BIT)
4914 shp->max_cmd_len = 16;
4915
4916 shp->max_id = ha->tid_cnt;
4917 shp->max_lun = MAXLUN;
Christoph Hellwig52759e62007-10-02 22:59:53 +02004918 shp->max_channel = ha->bus_cnt;
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004919
4920 spin_lock_init(&ha->smp_lock);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004921 gdth_enable_int(ha);
Christoph Hellwig835cc242007-10-02 23:09:56 +02004922
4923 error = scsi_add_host(shp, NULL);
4924 if (error)
Roel Kluin2c076ee2010-08-10 18:01:10 -07004925 goto out_free_ccb_phys;
Christoph Hellwig835cc242007-10-02 23:09:56 +02004926 list_add_tail(&ha->list, &gdth_instances);
James Bottomley2d6f0d02008-05-04 22:34:49 -05004927 gdth_timer_init();
Boaz Harrosh61c92812008-02-12 19:35:22 +02004928
4929 scsi_scan_host(shp);
4930
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004931 return 0;
4932
4933 out_free_ccb_phys:
4934 pci_unmap_single(ha->pdev,ha->ccb_phys, sizeof(gdth_cmd_str),
4935 PCI_DMA_BIDIRECTIONAL);
4936 out_free_coal_stat:
4937#ifdef INT_COAL
4938 pci_free_consistent(ha->pdev, sizeof(gdth_coal_status) * MAXOFFSETS,
4939 ha->coal_stat, ha->coal_stat_phys);
4940 out_free_pmsg:
4941#endif
4942 pci_free_consistent(ha->pdev, sizeof(gdth_msg_str),
4943 ha->pmsg, ha->msg_phys);
4944 out_free_pscratch:
4945 pci_free_consistent(ha->pdev, GDTH_SCRATCH,
4946 ha->pscratch, ha->scratch_phys);
4947 out_free_irq:
4948 free_irq(ha->irq, ha);
4949 gdth_ctr_count--;
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004950 out_host_put:
Christoph Hellwig835cc242007-10-02 23:09:56 +02004951 scsi_host_put(shp);
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004952 return error;
4953}
4954#endif /* CONFIG_EISA */
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004955
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004956#ifdef CONFIG_PCI
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004957static int gdth_pci_probe_one(gdth_pci_str *pcistr, gdth_ha_str **ha_out)
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004958{
4959 struct Scsi_Host *shp;
4960 gdth_ha_str *ha;
4961 dma_addr_t scratch_dma_handle = 0;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004962 int error, i;
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05004963 struct pci_dev *pdev = pcistr->pdev;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004964
Jeff Garzikcff26802008-02-15 22:20:09 -05004965 *ha_out = NULL;
4966
Christoph Hellwig835cc242007-10-02 23:09:56 +02004967 shp = scsi_host_alloc(&gdth_template, sizeof(gdth_ha_str));
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004968 if (!shp)
4969 return -ENOMEM;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004970 ha = shost_priv(shp);
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004971
4972 error = -ENODEV;
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05004973 if (!gdth_init_pci(pdev, pcistr, ha))
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004974 goto out_host_put;
4975
4976 /* controller found and initialized */
4977 printk("Configuring GDT-PCI HA at %d/%d IRQ %u\n",
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05004978 pdev->bus->number,
4979 PCI_SLOT(pdev->devfn),
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004980 ha->irq);
4981
4982 error = request_irq(ha->irq, gdth_interrupt,
4983 IRQF_DISABLED|IRQF_SHARED, "gdth", ha);
4984 if (error) {
4985 printk("GDT-PCI: Unable to allocate IRQ\n");
4986 goto out_host_put;
4987 }
4988
4989 shp->unchecked_isa_dma = 0;
4990 shp->irq = ha->irq;
4991 shp->dma_channel = 0xff;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004992
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004993 ha->hanum = gdth_ctr_count++;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004994 ha->shost = shp;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004995
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004996 ha->pccb = &ha->cmdext;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004997 ha->ccb_phys = 0L;
4998
4999 error = -ENOMEM;
5000
5001 ha->pscratch = pci_alloc_consistent(ha->pdev, GDTH_SCRATCH,
5002 &scratch_dma_handle);
5003 if (!ha->pscratch)
5004 goto out_free_irq;
5005 ha->scratch_phys = scratch_dma_handle;
5006
5007 ha->pmsg = pci_alloc_consistent(ha->pdev, sizeof(gdth_msg_str),
5008 &scratch_dma_handle);
5009 if (!ha->pmsg)
5010 goto out_free_pscratch;
5011 ha->msg_phys = scratch_dma_handle;
5012
5013#ifdef INT_COAL
5014 ha->coal_stat = pci_alloc_consistent(ha->pdev,
5015 sizeof(gdth_coal_status) * MAXOFFSETS,
5016 &scratch_dma_handle);
5017 if (!ha->coal_stat)
5018 goto out_free_pmsg;
5019 ha->coal_stat_phys = scratch_dma_handle;
5020#endif
5021
5022 ha->scratch_busy = FALSE;
5023 ha->req_first = NULL;
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05005024 ha->tid_cnt = pdev->device >= 0x200 ? MAXID : MAX_HDRIVES;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005025 if (max_ids > 0 && max_ids < ha->tid_cnt)
5026 ha->tid_cnt = max_ids;
5027 for (i = 0; i < GDTH_MAXCMDS; ++i)
5028 ha->cmd_tab[i].cmnd = UNUSED_CMND;
5029 ha->scan_mode = rescan ? 0x10 : 0;
5030
5031 error = -ENODEV;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02005032 if (!gdth_search_drives(ha)) {
5033 printk("GDT-PCI %d: Error during device scan\n", ha->hanum);
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005034 goto out_free_coal_stat;
5035 }
5036
5037 if (hdr_channel < 0 || hdr_channel > ha->bus_cnt)
5038 hdr_channel = ha->bus_cnt;
5039 ha->virt_bus = hdr_channel;
5040
5041 /* 64-bit DMA only supported from FW >= x.43 */
5042 if (!(ha->cache_feat & ha->raw_feat & ha->screen_feat & GDT_64BIT) ||
5043 !ha->dma64_support) {
Yang Hongyang284901a2009-04-06 19:01:15 -07005044 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005045 printk(KERN_WARNING "GDT-PCI %d: "
Boaz Harrosh45f1a412007-10-02 23:05:53 +02005046 "Unable to set 32-bit DMA\n", ha->hanum);
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005047 goto out_free_coal_stat;
5048 }
5049 } else {
5050 shp->max_cmd_len = 16;
Yang Hongyang6a355282009-04-06 19:01:13 -07005051 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02005052 printk("GDT-PCI %d: 64-bit DMA enabled\n", ha->hanum);
Yang Hongyang284901a2009-04-06 19:01:15 -07005053 } else if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005054 printk(KERN_WARNING "GDT-PCI %d: "
Boaz Harrosh45f1a412007-10-02 23:05:53 +02005055 "Unable to set 64/32-bit DMA\n", ha->hanum);
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005056 goto out_free_coal_stat;
5057 }
5058 }
5059
5060 shp->max_id = ha->tid_cnt;
5061 shp->max_lun = MAXLUN;
Christoph Hellwig52759e62007-10-02 22:59:53 +02005062 shp->max_channel = ha->bus_cnt;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005063
5064 spin_lock_init(&ha->smp_lock);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02005065 gdth_enable_int(ha);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005066
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05005067 error = scsi_add_host(shp, &pdev->dev);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005068 if (error)
5069 goto out_free_coal_stat;
5070 list_add_tail(&ha->list, &gdth_instances);
Boaz Harrosh61c92812008-02-12 19:35:22 +02005071
Jeff Garzikcff26802008-02-15 22:20:09 -05005072 pci_set_drvdata(ha->pdev, ha);
James Bottomley2d6f0d02008-05-04 22:34:49 -05005073 gdth_timer_init();
Jeff Garzikcff26802008-02-15 22:20:09 -05005074
Boaz Harrosh61c92812008-02-12 19:35:22 +02005075 scsi_scan_host(shp);
5076
Jeff Garzikcff26802008-02-15 22:20:09 -05005077 *ha_out = ha;
5078
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005079 return 0;
5080
5081 out_free_coal_stat:
5082#ifdef INT_COAL
5083 pci_free_consistent(ha->pdev, sizeof(gdth_coal_status) * MAXOFFSETS,
5084 ha->coal_stat, ha->coal_stat_phys);
5085 out_free_pmsg:
5086#endif
5087 pci_free_consistent(ha->pdev, sizeof(gdth_msg_str),
5088 ha->pmsg, ha->msg_phys);
5089 out_free_pscratch:
5090 pci_free_consistent(ha->pdev, GDTH_SCRATCH,
5091 ha->pscratch, ha->scratch_phys);
5092 out_free_irq:
5093 free_irq(ha->irq, ha);
5094 gdth_ctr_count--;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005095 out_host_put:
Christoph Hellwig835cc242007-10-02 23:09:56 +02005096 scsi_host_put(shp);
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005097 return error;
5098}
5099#endif /* CONFIG_PCI */
5100
Christoph Hellwig835cc242007-10-02 23:09:56 +02005101static void gdth_remove_one(gdth_ha_str *ha)
5102{
5103 struct Scsi_Host *shp = ha->shost;
5104
5105 TRACE2(("gdth_remove_one()\n"));
5106
5107 scsi_remove_host(shp);
5108
Boaz Harroshb31ddd32008-02-27 15:27:16 -08005109 gdth_flush(ha);
5110
Christoph Hellwig835cc242007-10-02 23:09:56 +02005111 if (ha->sdev) {
5112 scsi_free_host_dev(ha->sdev);
5113 ha->sdev = NULL;
5114 }
5115
Christoph Hellwig835cc242007-10-02 23:09:56 +02005116 if (shp->irq)
5117 free_irq(shp->irq,ha);
5118
5119#ifdef CONFIG_ISA
5120 if (shp->dma_channel != 0xff)
5121 free_dma(shp->dma_channel);
5122#endif
5123#ifdef INT_COAL
5124 if (ha->coal_stat)
5125 pci_free_consistent(ha->pdev, sizeof(gdth_coal_status) *
5126 MAXOFFSETS, ha->coal_stat, ha->coal_stat_phys);
5127#endif
5128 if (ha->pscratch)
5129 pci_free_consistent(ha->pdev, GDTH_SCRATCH,
5130 ha->pscratch, ha->scratch_phys);
5131 if (ha->pmsg)
5132 pci_free_consistent(ha->pdev, sizeof(gdth_msg_str),
5133 ha->pmsg, ha->msg_phys);
5134 if (ha->ccb_phys)
5135 pci_unmap_single(ha->pdev,ha->ccb_phys,
5136 sizeof(gdth_cmd_str),PCI_DMA_BIDIRECTIONAL);
5137
5138 scsi_host_put(shp);
5139}
5140
Dave Jones1fe6dbf2010-01-04 10:19:34 -05005141static int gdth_halt(struct notifier_block *nb, unsigned long event, void *buf)
Boaz Harroshb31ddd32008-02-27 15:27:16 -08005142{
5143 gdth_ha_str *ha;
5144
5145 TRACE2(("gdth_halt() event %d\n", (int)event));
5146 if (event != SYS_RESTART && event != SYS_HALT && event != SYS_POWER_OFF)
5147 return NOTIFY_DONE;
5148
5149 list_for_each_entry(ha, &gdth_instances, list)
5150 gdth_flush(ha);
5151
5152 return NOTIFY_OK;
5153}
5154
5155static struct notifier_block gdth_notifier = {
5156 gdth_halt, NULL, 0
5157};
5158
Christoph Hellwig835cc242007-10-02 23:09:56 +02005159static int __init gdth_init(void)
5160{
5161 if (disable) {
5162 printk("GDT-HA: Controller driver disabled from"
5163 " command line !\n");
5164 return 0;
5165 }
5166
5167 printk("GDT-HA: Storage RAID Controller Driver. Version: %s\n",
5168 GDTH_VERSION_STR);
5169
5170 /* initializations */
5171 gdth_polling = TRUE;
5172 gdth_clear_events();
James Bottomley2d6f0d02008-05-04 22:34:49 -05005173 init_timer(&gdth_timer);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005174
5175 /* As default we do not probe for EISA or ISA controllers */
5176 if (probe_eisa_isa) {
5177 /* scanning for controllers, at first: ISA controller */
5178#ifdef CONFIG_ISA
Dave Jones1fe6dbf2010-01-04 10:19:34 -05005179 u32 isa_bios;
Christoph Hellwig835cc242007-10-02 23:09:56 +02005180 for (isa_bios = 0xc8000UL; isa_bios <= 0xd8000UL;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02005181 isa_bios += 0x8000UL)
Christoph Hellwig835cc242007-10-02 23:09:56 +02005182 gdth_isa_probe_one(isa_bios);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005183#endif
5184#ifdef CONFIG_EISA
5185 {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05005186 u16 eisa_slot;
Christoph Hellwig835cc242007-10-02 23:09:56 +02005187 for (eisa_slot = 0x1000; eisa_slot <= 0x8000;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02005188 eisa_slot += 0x1000)
Christoph Hellwig835cc242007-10-02 23:09:56 +02005189 gdth_eisa_probe_one(eisa_slot);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005190 }
5191#endif
5192 }
5193
5194#ifdef CONFIG_PCI
5195 /* scanning for PCI controllers */
James Bottomleya85591f2008-05-04 22:35:58 -05005196 if (pci_register_driver(&gdth_pci_driver)) {
5197 gdth_ha_str *ha;
5198
5199 list_for_each_entry(ha, &gdth_instances, list)
5200 gdth_remove_one(ha);
5201 return -ENODEV;
5202 }
Christoph Hellwig835cc242007-10-02 23:09:56 +02005203#endif /* CONFIG_PCI */
5204
5205 TRACE2(("gdth_detect() %d controller detected\n", gdth_ctr_count));
Linus Torvaldsc596cc42007-10-15 12:46:16 -07005206
Christoph Hellwig835cc242007-10-02 23:09:56 +02005207 major = register_chrdev(0,"gdth", &gdth_fops);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005208 register_reboot_notifier(&gdth_notifier);
5209 gdth_polling = FALSE;
5210 return 0;
5211}
5212
5213static void __exit gdth_exit(void)
5214{
5215 gdth_ha_str *ha;
5216
Boaz Harroshb31ddd32008-02-27 15:27:16 -08005217 unregister_chrdev(major, "gdth");
5218 unregister_reboot_notifier(&gdth_notifier);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005219
5220#ifdef GDTH_STATISTICS
Boaz Harroshb31ddd32008-02-27 15:27:16 -08005221 del_timer_sync(&gdth_timer);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005222#endif
Boaz Harroshb31ddd32008-02-27 15:27:16 -08005223
Jeff Garzikcff26802008-02-15 22:20:09 -05005224#ifdef CONFIG_PCI
James Bottomleya85591f2008-05-04 22:35:58 -05005225 pci_unregister_driver(&gdth_pci_driver);
Jeff Garzikcff26802008-02-15 22:20:09 -05005226#endif
5227
Boaz Harroshb31ddd32008-02-27 15:27:16 -08005228 list_for_each_entry(ha, &gdth_instances, list)
5229 gdth_remove_one(ha);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005230}
5231
5232module_init(gdth_init);
5233module_exit(gdth_exit);
5234
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235#ifndef MODULE
5236__setup("gdth=", option_setup);
5237#endif