blob: 23d1a28b929dcbd9cf8345e3fc454b41c01c5737 [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 Harrosh3892d882007-10-02 23:18:03 +020088 * this_residual: gdth_bufflen
89 * buffer: gdth_sglist
90 * dma_handle: unused
91 * buffers_residual: gdth_sg_count
92 * 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>
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#ifdef GDTH_RTC
125#include <linux/mc146818rtc.h>
126#endif
127#include <linux/reboot.h>
128
129#include <asm/dma.h>
130#include <asm/system.h>
131#include <asm/io.h>
132#include <asm/uaccess.h>
133#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#include <linux/blkdev.h>
Boaz Harrosh3892d882007-10-02 23:18:03 +0200135#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137#include "scsi.h"
138#include <scsi/scsi_host.h>
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700139#include "gdth.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141static void gdth_delay(int milliseconds);
142static void gdth_eval_mapping(ulong32 size, ulong32 *cyls, int *heads, int *secs);
David Howells7d12e782006-10-05 14:55:46 +0100143static irqreturn_t gdth_interrupt(int irq, void *dev_id);
Jeff Garzik230e8862007-12-13 16:14:12 -0800144static irqreturn_t __gdth_interrupt(gdth_ha_str *ha,
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +0200145 int gdth_from_wait, int* pIndex);
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200146static int gdth_sync_event(gdth_ha_str *ha, int service, unchar index,
147 Scsi_Cmnd *scp);
148static int gdth_async_event(gdth_ha_str *ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static void gdth_log_event(gdth_evt_data *dvr, char *buffer);
150
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200151static void gdth_putq(gdth_ha_str *ha, Scsi_Cmnd *scp, unchar priority);
152static void gdth_next(gdth_ha_str *ha);
153static int gdth_fill_raw_cmd(gdth_ha_str *ha, Scsi_Cmnd *scp, unchar b);
154static int gdth_special_cmd(gdth_ha_str *ha, Scsi_Cmnd *scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static gdth_evt_str *gdth_store_event(gdth_ha_str *ha, ushort source,
156 ushort idx, gdth_evt_data *evt);
157static int gdth_read_event(gdth_ha_str *ha, int handle, gdth_evt_str *estr);
158static void gdth_readapp_event(gdth_ha_str *ha, unchar application,
159 gdth_evt_str *estr);
160static void gdth_clear_events(void);
161
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200162static void gdth_copy_internal_data(gdth_ha_str *ha, Scsi_Cmnd *scp,
Boaz Harrosh3892d882007-10-02 23:18:03 +0200163 char *buffer, ushort count, int to_buffer);
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200164static int gdth_internal_cache_cmd(gdth_ha_str *ha, Scsi_Cmnd *scp);
165static int gdth_fill_cache_cmd(gdth_ha_str *ha, Scsi_Cmnd *scp, ushort hdrive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200167static void gdth_enable_int(gdth_ha_str *ha);
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200168static int gdth_test_busy(gdth_ha_str *ha);
169static int gdth_get_cmd_index(gdth_ha_str *ha);
170static void gdth_release_event(gdth_ha_str *ha);
171static int gdth_wait(gdth_ha_str *ha, int index,ulong32 time);
172static int gdth_internal_cmd(gdth_ha_str *ha, unchar service, ushort opcode,
173 ulong32 p1, ulong64 p2,ulong64 p3);
174static int gdth_search_drives(gdth_ha_str *ha);
175static int gdth_analyse_hdrive(gdth_ha_str *ha, ushort hdrive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200177static const char *gdth_ctr_name(gdth_ha_str *ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179static int gdth_open(struct inode *inode, struct file *filep);
180static int gdth_close(struct inode *inode, struct file *filep);
181static int gdth_ioctl(struct inode *inode, struct file *filep,
182 unsigned int cmd, unsigned long arg);
183
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200184static void gdth_flush(gdth_ha_str *ha);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700185static int gdth_queuecommand(Scsi_Cmnd *scp,void (*done)(Scsi_Cmnd *));
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200186static int __gdth_queuecommand(gdth_ha_str *ha, struct scsi_cmnd *scp,
187 struct gdth_cmndinfo *cmndinfo);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700188static void gdth_scsi_done(struct scsi_cmnd *scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190#ifdef DEBUG_GDTH
191static unchar DebugState = DEBUG_GDTH;
192
193#ifdef __SERIAL__
194#define MAX_SERBUF 160
195static void ser_init(void);
196static void ser_puts(char *str);
197static void ser_putc(char c);
198static int ser_printk(const char *fmt, ...);
199static char strbuf[MAX_SERBUF+1];
200#ifdef __COM2__
201#define COM_BASE 0x2f8
202#else
203#define COM_BASE 0x3f8
204#endif
205static void ser_init()
206{
207 unsigned port=COM_BASE;
208
209 outb(0x80,port+3);
210 outb(0,port+1);
211 /* 19200 Baud, if 9600: outb(12,port) */
212 outb(6, port);
213 outb(3,port+3);
214 outb(0,port+1);
215 /*
216 ser_putc('I');
217 ser_putc(' ');
218 */
219}
220
221static void ser_puts(char *str)
222{
223 char *ptr;
224
225 ser_init();
226 for (ptr=str;*ptr;++ptr)
227 ser_putc(*ptr);
228}
229
230static void ser_putc(char c)
231{
232 unsigned port=COM_BASE;
233
234 while ((inb(port+5) & 0x20)==0);
235 outb(c,port);
236 if (c==0x0a)
237 {
238 while ((inb(port+5) & 0x20)==0);
239 outb(0x0d,port);
240 }
241}
242
243static int ser_printk(const char *fmt, ...)
244{
245 va_list args;
246 int i;
247
248 va_start(args,fmt);
249 i = vsprintf(strbuf,fmt,args);
250 ser_puts(strbuf);
251 va_end(args);
252 return i;
253}
254
255#define TRACE(a) {if (DebugState==1) {ser_printk a;}}
256#define TRACE2(a) {if (DebugState==1 || DebugState==2) {ser_printk a;}}
257#define TRACE3(a) {if (DebugState!=0) {ser_printk a;}}
258
259#else /* !__SERIAL__ */
260#define TRACE(a) {if (DebugState==1) {printk a;}}
261#define TRACE2(a) {if (DebugState==1 || DebugState==2) {printk a;}}
262#define TRACE3(a) {if (DebugState!=0) {printk a;}}
263#endif
264
265#else /* !DEBUG */
266#define TRACE(a)
267#define TRACE2(a)
268#define TRACE3(a)
269#endif
270
271#ifdef GDTH_STATISTICS
272static ulong32 max_rq=0, max_index=0, max_sg=0;
273#ifdef INT_COAL
274static ulong32 max_int_coal=0;
275#endif
276static ulong32 act_ints=0, act_ios=0, act_stats=0, act_rq=0;
277static struct timer_list gdth_timer;
278#endif
279
280#define PTR2USHORT(a) (ushort)(ulong)(a)
Tobias Klauser6391a112006-06-08 22:23:48 -0700281#define GDTOFFSOF(a,b) (size_t)&(((a*)0)->b)
282#define INDEX_OK(i,t) ((i)<ARRAY_SIZE(t))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284#define BUS_L2P(a,b) ((b)>(a)->virt_bus ? (b-1):(b))
285
Christoph Hellwigaed91cb2007-10-02 22:48:16 +0200286#ifdef CONFIG_ISA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287static unchar gdth_drq_tab[4] = {5,6,7,7}; /* DRQ table */
Christoph Hellwigaed91cb2007-10-02 22:48:16 +0200288#endif
Dhaval Gianic8e91b02007-10-12 15:12:31 +0530289#if defined(CONFIG_EISA) || defined(CONFIG_ISA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290static unchar gdth_irq_tab[6] = {0,10,11,12,14,0}; /* IRQ table */
Christoph Hellwig706a5d42007-10-02 22:49:35 +0200291#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292static unchar gdth_polling; /* polling if TRUE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293static int gdth_ctr_count = 0; /* controller count */
Boaz Harrosh884f7fb2007-10-02 23:11:24 +0200294static LIST_HEAD(gdth_instances); /* controller list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295static unchar gdth_write_through = FALSE; /* write through */
296static gdth_evt_str ebuffer[MAX_EVENTS]; /* event buffer */
297static int elastidx;
298static int eoldidx;
299static int major;
300
301#define DIN 1 /* IN data direction */
302#define DOU 2 /* OUT data direction */
303#define DNO DIN /* no data transfer */
304#define DUN DIN /* unknown data direction */
305static unchar gdth_direction_tab[0x100] = {
306 DNO,DNO,DIN,DIN,DOU,DIN,DIN,DOU,DIN,DUN,DOU,DOU,DUN,DUN,DUN,DIN,
307 DNO,DIN,DIN,DOU,DIN,DOU,DNO,DNO,DOU,DNO,DIN,DNO,DIN,DOU,DNO,DUN,
308 DIN,DUN,DIN,DUN,DOU,DIN,DUN,DUN,DIN,DIN,DOU,DNO,DUN,DIN,DOU,DOU,
309 DOU,DOU,DOU,DNO,DIN,DNO,DNO,DIN,DOU,DOU,DOU,DOU,DIN,DOU,DIN,DOU,
310 DOU,DOU,DIN,DIN,DIN,DNO,DUN,DNO,DNO,DNO,DUN,DNO,DOU,DIN,DUN,DUN,
311 DUN,DUN,DUN,DUN,DUN,DOU,DUN,DUN,DUN,DUN,DIN,DUN,DUN,DUN,DUN,DUN,
312 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
313 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
314 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DIN,DUN,DOU,DUN,DUN,DUN,DUN,DUN,
315 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DIN,DUN,
316 DUN,DUN,DUN,DUN,DUN,DNO,DNO,DUN,DIN,DNO,DOU,DUN,DNO,DUN,DOU,DOU,
317 DOU,DOU,DOU,DNO,DUN,DIN,DOU,DIN,DIN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
318 DUN,DUN,DOU,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
319 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
320 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DOU,DUN,DUN,DUN,DUN,DUN,
321 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN
322};
323
324/* LILO and modprobe/insmod parameters */
325/* IRQ list for GDT3000/3020 EISA controllers */
326static int irq[MAXHA] __initdata =
327{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
328 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
329/* disable driver flag */
330static int disable __initdata = 0;
331/* reserve flag */
332static int reserve_mode = 1;
333/* reserve list */
334static int reserve_list[MAX_RES_ARGS] =
335{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
336 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
337 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
338/* scan order for PCI controllers */
339static int reverse_scan = 0;
340/* virtual channel for the host drives */
341static int hdr_channel = 0;
342/* max. IDs per channel */
343static int max_ids = MAXID;
344/* rescan all IDs */
345static int rescan = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346/* shared access */
347static int shared_access = 1;
348/* enable support for EISA and ISA controllers */
349static int probe_eisa_isa = 0;
350/* 64 bit DMA mode, support for drives > 2 TB, if force_dma32 = 0 */
351static int force_dma32 = 0;
352
353/* parameters for modprobe/insmod */
354module_param_array(irq, int, NULL, 0);
355module_param(disable, int, 0);
356module_param(reserve_mode, int, 0);
357module_param_array(reserve_list, int, NULL, 0);
358module_param(reverse_scan, int, 0);
359module_param(hdr_channel, int, 0);
360module_param(max_ids, int, 0);
361module_param(rescan, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362module_param(shared_access, int, 0);
363module_param(probe_eisa_isa, int, 0);
364module_param(force_dma32, int, 0);
365MODULE_AUTHOR("Achim Leubner");
366MODULE_LICENSE("GPL");
367
368/* ioctl interface */
Arjan van de Ven00977a52007-02-12 00:55:34 -0800369static const struct file_operations gdth_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 .ioctl = gdth_ioctl,
371 .open = gdth_open,
372 .release = gdth_close,
373};
374
Boaz Harrosh3892d882007-10-02 23:18:03 +0200375/*
376 * gdth scsi_command access wrappers.
377 * below 6 functions are used throughout the driver to access scsi_command's
378 * io parameters. The reason we do not use the regular accessors from
379 * scsi_cmnd.h is because of gdth_execute(). Since it is unrecommended for
380 * llds to directly set scsi_cmnd's IO members. This driver will use SCp
381 * members for IO parameters, and will copy scsi_cmnd's members to Scp
382 * members in queuecommand. For internal commands through gdth_execute()
383 * SCp's members will be set directly.
384 */
385static inline unsigned gdth_bufflen(struct scsi_cmnd *cmd)
386{
387 return (unsigned)cmd->SCp.this_residual;
388}
389
390static inline void gdth_set_bufflen(struct scsi_cmnd *cmd, unsigned bufflen)
391{
392 cmd->SCp.this_residual = bufflen;
393}
394
395static inline unsigned gdth_sg_count(struct scsi_cmnd *cmd)
396{
397 return (unsigned)cmd->SCp.buffers_residual;
398}
399
400static inline void gdth_set_sg_count(struct scsi_cmnd *cmd, unsigned sg_count)
401{
402 cmd->SCp.buffers_residual = sg_count;
403}
404
405static inline struct scatterlist *gdth_sglist(struct scsi_cmnd *cmd)
406{
407 return cmd->SCp.buffer;
408}
409
410static inline void gdth_set_sglist(struct scsi_cmnd *cmd,
411 struct scatterlist *sglist)
412{
413 cmd->SCp.buffer = sglist;
414}
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416#include "gdth_proc.h"
417#include "gdth_proc.c"
418
Boaz Harrosh884f7fb2007-10-02 23:11:24 +0200419static gdth_ha_str *gdth_find_ha(int hanum)
420{
421 gdth_ha_str *ha;
422
423 list_for_each_entry(ha, &gdth_instances, list)
424 if (hanum == ha->hanum)
425 return ha;
426
427 return NULL;
428}
429
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200430static struct gdth_cmndinfo *gdth_get_cmndinfo(gdth_ha_str *ha)
431{
432 struct gdth_cmndinfo *priv = NULL;
433 ulong flags;
434 int i;
435
436 spin_lock_irqsave(&ha->smp_lock, flags);
437
438 for (i=0; i<GDTH_MAXCMDS; ++i) {
439 if (ha->cmndinfo[i].index == 0) {
440 priv = &ha->cmndinfo[i];
441 priv->index = i+1;
442 memset(priv, 0, sizeof(*priv));
443 break;
444 }
445 }
446
447 spin_unlock_irqrestore(&ha->smp_lock, flags);
448
449 return priv;
450}
451
452static void gdth_put_cmndinfo(struct gdth_cmndinfo *priv)
453{
454 BUG_ON(!priv);
455 priv->index = 0;
456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458static void gdth_delay(int milliseconds)
459{
460 if (milliseconds == 0) {
461 udelay(1);
462 } else {
463 mdelay(milliseconds);
464 }
465}
466
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700467static void gdth_scsi_done(struct scsi_cmnd *scp)
468{
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200469 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
470 int internal_command = cmndinfo->internal_command;
471
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +0200472 TRACE2(("gdth_scsi_done()\n"));
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700473
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200474 gdth_put_cmndinfo(cmndinfo);
475 scp->host_scribble = NULL;
476
477 if (internal_command)
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +0200478 complete((struct completion *)scp->request);
479 else
480 scp->scsi_done(scp);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700481}
482
483int __gdth_execute(struct scsi_device *sdev, gdth_cmd_str *gdtcmd, char *cmnd,
484 int timeout, u32 *info)
485{
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200486 gdth_ha_str *ha = shost_priv(sdev->host);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700487 Scsi_Cmnd *scp;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200488 struct gdth_cmndinfo cmndinfo;
Boaz Harrosh3892d882007-10-02 23:18:03 +0200489 struct scatterlist one_sg;
Peter Zijlstra6e9a4732006-09-30 23:28:10 -0700490 DECLARE_COMPLETION_ONSTACK(wait);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700491 int rval;
492
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +0200493 scp = kzalloc(sizeof(*scp), GFP_KERNEL);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700494 if (!scp)
495 return -ENOMEM;
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +0200496
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700497 scp->device = sdev;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200498 memset(&cmndinfo, 0, sizeof(cmndinfo));
499
Christoph Hellwigbeb40482006-06-10 18:01:03 +0200500 /* use request field to save the ptr. to completion struct. */
501 scp->request = (struct request *)&wait;
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700502 scp->timeout_per_command = timeout*HZ;
Boaz Harrosh3892d882007-10-02 23:18:03 +0200503 sg_init_one(&one_sg, gdtcmd, sizeof(*gdtcmd));
504 gdth_set_sglist(scp, &one_sg);
505 gdth_set_sg_count(scp, 1);
506 gdth_set_bufflen(scp, sizeof(*gdtcmd));
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700507 scp->cmd_len = 12;
508 memcpy(scp->cmnd, cmnd, 12);
Boaz Harroshf842b642007-10-02 23:16:01 +0200509 cmndinfo.priority = IOCTL_PRI;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200510 cmndinfo.internal_command = 1;
511
512 TRACE(("__gdth_execute() cmd 0x%x\n", scp->cmnd[0]));
513 __gdth_queuecommand(ha, scp, &cmndinfo);
514
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700515 wait_for_completion(&wait);
516
Boaz Harroshf842b642007-10-02 23:16:01 +0200517 rval = cmndinfo.status;
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700518 if (info)
Boaz Harroshf842b642007-10-02 23:16:01 +0200519 *info = cmndinfo.info;
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700520 kfree(scp);
521 return rval;
522}
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700523
524int gdth_execute(struct Scsi_Host *shost, gdth_cmd_str *gdtcmd, char *cmnd,
525 int timeout, u32 *info)
526{
527 struct scsi_device *sdev = scsi_get_host_dev(shost);
528 int rval = __gdth_execute(sdev, gdtcmd, cmnd, timeout, info);
529
530 scsi_free_host_dev(sdev);
531 return rval;
532}
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534static void gdth_eval_mapping(ulong32 size, ulong32 *cyls, int *heads, int *secs)
535{
536 *cyls = size /HEADS/SECS;
537 if (*cyls <= MAXCYLS) {
538 *heads = HEADS;
539 *secs = SECS;
540 } else { /* too high for 64*32 */
541 *cyls = size /MEDHEADS/MEDSECS;
542 if (*cyls <= MAXCYLS) {
543 *heads = MEDHEADS;
544 *secs = MEDSECS;
545 } else { /* too high for 127*63 */
546 *cyls = size /BIGHEADS/BIGSECS;
547 *heads = BIGHEADS;
548 *secs = BIGSECS;
549 }
550 }
551}
552
553/* controller search and initialization functions */
Christoph Hellwig706a5d42007-10-02 22:49:35 +0200554#ifdef CONFIG_EISA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555static int __init gdth_search_eisa(ushort eisa_adr)
556{
557 ulong32 id;
558
559 TRACE(("gdth_search_eisa() adr. %x\n",eisa_adr));
560 id = inl(eisa_adr+ID0REG);
561 if (id == GDT3A_ID || id == GDT3B_ID) { /* GDT3000A or GDT3000B */
562 if ((inb(eisa_adr+EISAREG) & 8) == 0)
563 return 0; /* not EISA configured */
564 return 1;
565 }
566 if (id == GDT3_ID) /* GDT3000 */
567 return 1;
568
569 return 0;
570}
Christoph Hellwig706a5d42007-10-02 22:49:35 +0200571#endif /* CONFIG_EISA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Christoph Hellwigaed91cb2007-10-02 22:48:16 +0200573#ifdef CONFIG_ISA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574static int __init gdth_search_isa(ulong32 bios_adr)
575{
576 void __iomem *addr;
577 ulong32 id;
578
579 TRACE(("gdth_search_isa() bios adr. %x\n",bios_adr));
580 if ((addr = ioremap(bios_adr+BIOS_ID_OFFS, sizeof(ulong32))) != NULL) {
Jeff Garzika52667f2007-10-02 22:55:53 +0200581 id = readl(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 iounmap(addr);
583 if (id == GDT2_ID) /* GDT2000 */
584 return 1;
585 }
586 return 0;
587}
Christoph Hellwigaed91cb2007-10-02 22:48:16 +0200588#endif /* CONFIG_ISA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Christoph Hellwig8514ef22007-10-02 22:51:06 +0200590#ifdef CONFIG_PCI
591static void gdth_search_dev(gdth_pci_str *pcistr, ushort *cnt,
592 ushort vendor, ushort dev);
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594static int __init gdth_search_pci(gdth_pci_str *pcistr)
595{
596 ushort device, cnt;
597
598 TRACE(("gdth_search_pci()\n"));
599
600 cnt = 0;
601 for (device = 0; device <= PCI_DEVICE_ID_VORTEX_GDT6555; ++device)
602 gdth_search_dev(pcistr, &cnt, PCI_VENDOR_ID_VORTEX, device);
603 for (device = PCI_DEVICE_ID_VORTEX_GDT6x17RP;
604 device <= PCI_DEVICE_ID_VORTEX_GDTMAXRP; ++device)
605 gdth_search_dev(pcistr, &cnt, PCI_VENDOR_ID_VORTEX, device);
606 gdth_search_dev(pcistr, &cnt, PCI_VENDOR_ID_VORTEX,
607 PCI_DEVICE_ID_VORTEX_GDTNEWRX);
608 gdth_search_dev(pcistr, &cnt, PCI_VENDOR_ID_VORTEX,
609 PCI_DEVICE_ID_VORTEX_GDTNEWRX2);
610 gdth_search_dev(pcistr, &cnt, PCI_VENDOR_ID_INTEL,
611 PCI_DEVICE_ID_INTEL_SRC);
612 gdth_search_dev(pcistr, &cnt, PCI_VENDOR_ID_INTEL,
613 PCI_DEVICE_ID_INTEL_SRC_XSCALE);
614 return cnt;
615}
616
617/* Vortex only makes RAID controllers.
618 * We do not really want to specify all 550 ids here, so wildcard match.
619 */
David Rientjes6c4b7e42007-05-23 14:41:52 -0700620static struct pci_device_id gdthtable[] __maybe_unused = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 {PCI_VENDOR_ID_VORTEX,PCI_ANY_ID,PCI_ANY_ID, PCI_ANY_ID},
622 {PCI_VENDOR_ID_INTEL,PCI_DEVICE_ID_INTEL_SRC,PCI_ANY_ID,PCI_ANY_ID},
623 {PCI_VENDOR_ID_INTEL,PCI_DEVICE_ID_INTEL_SRC_XSCALE,PCI_ANY_ID,PCI_ANY_ID},
624 {0}
625};
626MODULE_DEVICE_TABLE(pci,gdthtable);
627
628static void __init gdth_search_dev(gdth_pci_str *pcistr, ushort *cnt,
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700629 ushort vendor, ushort device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 ulong base0, base1, base2;
632 struct pci_dev *pdev;
633
634 TRACE(("gdth_search_dev() cnt %d vendor %x device %x\n",
635 *cnt, vendor, device));
636
637 pdev = NULL;
Sergio Luis99109302008-02-12 20:48:03 -0300638 while ((pdev = pci_get_device(vendor, device, pdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 != NULL) {
640 if (pci_enable_device(pdev))
641 continue;
Sergio Luis99109302008-02-12 20:48:03 -0300642 if (*cnt >= MAXHA) {
643 pci_dev_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return;
Sergio Luis99109302008-02-12 20:48:03 -0300645 }
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /* GDT PCI controller found, resources are already in pdev */
648 pcistr[*cnt].pdev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 pcistr[*cnt].irq = pdev->irq;
650 base0 = pci_resource_flags(pdev, 0);
651 base1 = pci_resource_flags(pdev, 1);
652 base2 = pci_resource_flags(pdev, 2);
653 if (device <= PCI_DEVICE_ID_VORTEX_GDT6000B || /* GDT6000/B */
654 device >= PCI_DEVICE_ID_VORTEX_GDT6x17RP) { /* MPR */
655 if (!(base0 & IORESOURCE_MEM))
656 continue;
657 pcistr[*cnt].dpmem = pci_resource_start(pdev, 0);
658 } else { /* GDT6110, GDT6120, .. */
659 if (!(base0 & IORESOURCE_MEM) ||
660 !(base2 & IORESOURCE_MEM) ||
661 !(base1 & IORESOURCE_IO))
662 continue;
663 pcistr[*cnt].dpmem = pci_resource_start(pdev, 2);
664 pcistr[*cnt].io_mm = pci_resource_start(pdev, 0);
665 pcistr[*cnt].io = pci_resource_start(pdev, 1);
666 }
667 TRACE2(("Controller found at %d/%d, irq %d, dpmem 0x%lx\n",
Jeff Garzik8e9a8a02007-07-17 05:25:17 -0400668 pcistr[*cnt].pdev->bus->number,
669 PCI_SLOT(pcistr[*cnt].pdev->devfn),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 pcistr[*cnt].irq, pcistr[*cnt].dpmem));
671 (*cnt)++;
672 }
673}
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675static void __init gdth_sort_pci(gdth_pci_str *pcistr, int cnt)
676{
677 gdth_pci_str temp;
678 int i, changed;
679
680 TRACE(("gdth_sort_pci() cnt %d\n",cnt));
681 if (cnt == 0)
682 return;
683
684 do {
685 changed = FALSE;
686 for (i = 0; i < cnt-1; ++i) {
687 if (!reverse_scan) {
Jeff Garzik8e9a8a02007-07-17 05:25:17 -0400688 if ((pcistr[i].pdev->bus->number > pcistr[i+1].pdev->bus->number) ||
689 (pcistr[i].pdev->bus->number == pcistr[i+1].pdev->bus->number &&
690 PCI_SLOT(pcistr[i].pdev->devfn) >
691 PCI_SLOT(pcistr[i+1].pdev->devfn))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 temp = pcistr[i];
693 pcistr[i] = pcistr[i+1];
694 pcistr[i+1] = temp;
695 changed = TRUE;
696 }
697 } else {
Jeff Garzik8e9a8a02007-07-17 05:25:17 -0400698 if ((pcistr[i].pdev->bus->number < pcistr[i+1].pdev->bus->number) ||
699 (pcistr[i].pdev->bus->number == pcistr[i+1].pdev->bus->number &&
700 PCI_SLOT(pcistr[i].pdev->devfn) <
701 PCI_SLOT(pcistr[i+1].pdev->devfn))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 temp = pcistr[i];
703 pcistr[i] = pcistr[i+1];
704 pcistr[i+1] = temp;
705 changed = TRUE;
706 }
707 }
708 }
709 } while (changed);
710}
Christoph Hellwig8514ef22007-10-02 22:51:06 +0200711#endif /* CONFIG_PCI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Christoph Hellwig706a5d42007-10-02 22:49:35 +0200713#ifdef CONFIG_EISA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714static int __init gdth_init_eisa(ushort eisa_adr,gdth_ha_str *ha)
715{
716 ulong32 retries,id;
717 unchar prot_ver,eisacf,i,irq_found;
718
719 TRACE(("gdth_init_eisa() adr. %x\n",eisa_adr));
720
721 /* disable board interrupts, deinitialize services */
722 outb(0xff,eisa_adr+EDOORREG);
723 outb(0x00,eisa_adr+EDENABREG);
724 outb(0x00,eisa_adr+EINTENABREG);
725
726 outb(0xff,eisa_adr+LDOORREG);
727 retries = INIT_RETRIES;
728 gdth_delay(20);
729 while (inb(eisa_adr+EDOORREG) != 0xff) {
730 if (--retries == 0) {
731 printk("GDT-EISA: Initialization error (DEINIT failed)\n");
732 return 0;
733 }
734 gdth_delay(1);
735 TRACE2(("wait for DEINIT: retries=%d\n",retries));
736 }
737 prot_ver = inb(eisa_adr+MAILBOXREG);
738 outb(0xff,eisa_adr+EDOORREG);
739 if (prot_ver != PROTOCOL_VERSION) {
740 printk("GDT-EISA: Illegal protocol version\n");
741 return 0;
742 }
743 ha->bmic = eisa_adr;
744 ha->brd_phys = (ulong32)eisa_adr >> 12;
745
746 outl(0,eisa_adr+MAILBOXREG);
747 outl(0,eisa_adr+MAILBOXREG+4);
748 outl(0,eisa_adr+MAILBOXREG+8);
749 outl(0,eisa_adr+MAILBOXREG+12);
750
751 /* detect IRQ */
752 if ((id = inl(eisa_adr+ID0REG)) == GDT3_ID) {
753 ha->oem_id = OEM_ID_ICP;
754 ha->type = GDT_EISA;
755 ha->stype = id;
756 outl(1,eisa_adr+MAILBOXREG+8);
757 outb(0xfe,eisa_adr+LDOORREG);
758 retries = INIT_RETRIES;
759 gdth_delay(20);
760 while (inb(eisa_adr+EDOORREG) != 0xfe) {
761 if (--retries == 0) {
762 printk("GDT-EISA: Initialization error (get IRQ failed)\n");
763 return 0;
764 }
765 gdth_delay(1);
766 }
767 ha->irq = inb(eisa_adr+MAILBOXREG);
768 outb(0xff,eisa_adr+EDOORREG);
769 TRACE2(("GDT3000/3020: IRQ=%d\n",ha->irq));
770 /* check the result */
771 if (ha->irq == 0) {
772 TRACE2(("Unknown IRQ, use IRQ table from cmd line !\n"));
773 for (i = 0, irq_found = FALSE;
774 i < MAXHA && irq[i] != 0xff; ++i) {
775 if (irq[i]==10 || irq[i]==11 || irq[i]==12 || irq[i]==14) {
776 irq_found = TRUE;
777 break;
778 }
779 }
780 if (irq_found) {
781 ha->irq = irq[i];
782 irq[i] = 0;
783 printk("GDT-EISA: Can not detect controller IRQ,\n");
784 printk("Use IRQ setting from command line (IRQ = %d)\n",
785 ha->irq);
786 } else {
787 printk("GDT-EISA: Initialization error (unknown IRQ), Enable\n");
788 printk("the controller BIOS or use command line parameters\n");
789 return 0;
790 }
791 }
792 } else {
793 eisacf = inb(eisa_adr+EISAREG) & 7;
794 if (eisacf > 4) /* level triggered */
795 eisacf -= 4;
796 ha->irq = gdth_irq_tab[eisacf];
797 ha->oem_id = OEM_ID_ICP;
798 ha->type = GDT_EISA;
799 ha->stype = id;
800 }
801
802 ha->dma64_support = 0;
803 return 1;
804}
Christoph Hellwig706a5d42007-10-02 22:49:35 +0200805#endif /* CONFIG_EISA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Christoph Hellwigaed91cb2007-10-02 22:48:16 +0200807#ifdef CONFIG_ISA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808static int __init gdth_init_isa(ulong32 bios_adr,gdth_ha_str *ha)
809{
810 register gdt2_dpram_str __iomem *dp2_ptr;
811 int i;
812 unchar irq_drq,prot_ver;
813 ulong32 retries;
814
815 TRACE(("gdth_init_isa() bios adr. %x\n",bios_adr));
816
817 ha->brd = ioremap(bios_adr, sizeof(gdt2_dpram_str));
818 if (ha->brd == NULL) {
819 printk("GDT-ISA: Initialization error (DPMEM remap error)\n");
820 return 0;
821 }
822 dp2_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +0200823 writeb(1, &dp2_ptr->io.memlock); /* switch off write protection */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 /* reset interface area */
825 memset_io(&dp2_ptr->u, 0, sizeof(dp2_ptr->u));
Jeff Garzika52667f2007-10-02 22:55:53 +0200826 if (readl(&dp2_ptr->u) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 printk("GDT-ISA: Initialization error (DPMEM write error)\n");
828 iounmap(ha->brd);
829 return 0;
830 }
831
832 /* disable board interrupts, read DRQ and IRQ */
Jeff Garzika52667f2007-10-02 22:55:53 +0200833 writeb(0xff, &dp2_ptr->io.irqdel);
834 writeb(0x00, &dp2_ptr->io.irqen);
835 writeb(0x00, &dp2_ptr->u.ic.S_Status);
836 writeb(0x00, &dp2_ptr->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Jeff Garzika52667f2007-10-02 22:55:53 +0200838 irq_drq = readb(&dp2_ptr->io.rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 for (i=0; i<3; ++i) {
840 if ((irq_drq & 1)==0)
841 break;
842 irq_drq >>= 1;
843 }
844 ha->drq = gdth_drq_tab[i];
845
Jeff Garzika52667f2007-10-02 22:55:53 +0200846 irq_drq = readb(&dp2_ptr->io.rq) >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 for (i=1; i<5; ++i) {
848 if ((irq_drq & 1)==0)
849 break;
850 irq_drq >>= 1;
851 }
852 ha->irq = gdth_irq_tab[i];
853
854 /* deinitialize services */
Jeff Garzika52667f2007-10-02 22:55:53 +0200855 writel(bios_adr, &dp2_ptr->u.ic.S_Info[0]);
856 writeb(0xff, &dp2_ptr->u.ic.S_Cmd_Indx);
857 writeb(0, &dp2_ptr->io.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 retries = INIT_RETRIES;
859 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +0200860 while (readb(&dp2_ptr->u.ic.S_Status) != 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (--retries == 0) {
862 printk("GDT-ISA: Initialization error (DEINIT failed)\n");
863 iounmap(ha->brd);
864 return 0;
865 }
866 gdth_delay(1);
867 }
Jeff Garzika52667f2007-10-02 22:55:53 +0200868 prot_ver = (unchar)readl(&dp2_ptr->u.ic.S_Info[0]);
869 writeb(0, &dp2_ptr->u.ic.Status);
870 writeb(0xff, &dp2_ptr->io.irqdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (prot_ver != PROTOCOL_VERSION) {
872 printk("GDT-ISA: Illegal protocol version\n");
873 iounmap(ha->brd);
874 return 0;
875 }
876
877 ha->oem_id = OEM_ID_ICP;
878 ha->type = GDT_ISA;
879 ha->ic_all_size = sizeof(dp2_ptr->u);
880 ha->stype= GDT2_ID;
881 ha->brd_phys = bios_adr >> 4;
882
883 /* special request to controller BIOS */
Jeff Garzika52667f2007-10-02 22:55:53 +0200884 writel(0x00, &dp2_ptr->u.ic.S_Info[0]);
885 writel(0x00, &dp2_ptr->u.ic.S_Info[1]);
886 writel(0x01, &dp2_ptr->u.ic.S_Info[2]);
887 writel(0x00, &dp2_ptr->u.ic.S_Info[3]);
888 writeb(0xfe, &dp2_ptr->u.ic.S_Cmd_Indx);
889 writeb(0, &dp2_ptr->io.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 retries = INIT_RETRIES;
891 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +0200892 while (readb(&dp2_ptr->u.ic.S_Status) != 0xfe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (--retries == 0) {
894 printk("GDT-ISA: Initialization error\n");
895 iounmap(ha->brd);
896 return 0;
897 }
898 gdth_delay(1);
899 }
Jeff Garzika52667f2007-10-02 22:55:53 +0200900 writeb(0, &dp2_ptr->u.ic.Status);
901 writeb(0xff, &dp2_ptr->io.irqdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 ha->dma64_support = 0;
904 return 1;
905}
Christoph Hellwigaed91cb2007-10-02 22:48:16 +0200906#endif /* CONFIG_ISA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Christoph Hellwig8514ef22007-10-02 22:51:06 +0200908#ifdef CONFIG_PCI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909static int __init gdth_init_pci(gdth_pci_str *pcistr,gdth_ha_str *ha)
910{
911 register gdt6_dpram_str __iomem *dp6_ptr;
912 register gdt6c_dpram_str __iomem *dp6c_ptr;
913 register gdt6m_dpram_str __iomem *dp6m_ptr;
914 ulong32 retries;
915 unchar prot_ver;
916 ushort command;
917 int i, found = FALSE;
918
919 TRACE(("gdth_init_pci()\n"));
920
Jeff Garzik8e9a8a02007-07-17 05:25:17 -0400921 if (pcistr->pdev->vendor == PCI_VENDOR_ID_INTEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 ha->oem_id = OEM_ID_INTEL;
923 else
924 ha->oem_id = OEM_ID_ICP;
Jeff Garzik8e9a8a02007-07-17 05:25:17 -0400925 ha->brd_phys = (pcistr->pdev->bus->number << 8) | (pcistr->pdev->devfn & 0xf8);
926 ha->stype = (ulong32)pcistr->pdev->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 ha->irq = pcistr->irq;
928 ha->pdev = pcistr->pdev;
929
Jeff Garzik8e9a8a02007-07-17 05:25:17 -0400930 if (ha->pdev->device <= PCI_DEVICE_ID_VORTEX_GDT6000B) { /* GDT6000/B */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 TRACE2(("init_pci() dpmem %lx irq %d\n",pcistr->dpmem,ha->irq));
932 ha->brd = ioremap(pcistr->dpmem, sizeof(gdt6_dpram_str));
933 if (ha->brd == NULL) {
934 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
935 return 0;
936 }
937 /* check and reset interface area */
938 dp6_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +0200939 writel(DPMEM_MAGIC, &dp6_ptr->u);
940 if (readl(&dp6_ptr->u) != DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 printk("GDT-PCI: Cannot access DPMEM at 0x%lx (shadowed?)\n",
942 pcistr->dpmem);
943 found = FALSE;
944 for (i = 0xC8000; i < 0xE8000; i += 0x4000) {
945 iounmap(ha->brd);
946 ha->brd = ioremap(i, sizeof(ushort));
947 if (ha->brd == NULL) {
948 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
949 return 0;
950 }
Jeff Garzika52667f2007-10-02 22:55:53 +0200951 if (readw(ha->brd) != 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 TRACE2(("init_pci_old() address 0x%x busy\n", i));
953 continue;
954 }
955 iounmap(ha->brd);
956 pci_write_config_dword(pcistr->pdev,
957 PCI_BASE_ADDRESS_0, i);
958 ha->brd = ioremap(i, sizeof(gdt6_dpram_str));
959 if (ha->brd == NULL) {
960 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
961 return 0;
962 }
963 dp6_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +0200964 writel(DPMEM_MAGIC, &dp6_ptr->u);
965 if (readl(&dp6_ptr->u) == DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 printk("GDT-PCI: Use free address at 0x%x\n", i);
967 found = TRUE;
968 break;
969 }
970 }
971 if (!found) {
972 printk("GDT-PCI: No free address found!\n");
973 iounmap(ha->brd);
974 return 0;
975 }
976 }
977 memset_io(&dp6_ptr->u, 0, sizeof(dp6_ptr->u));
Jeff Garzika52667f2007-10-02 22:55:53 +0200978 if (readl(&dp6_ptr->u) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 printk("GDT-PCI: Initialization error (DPMEM write error)\n");
980 iounmap(ha->brd);
981 return 0;
982 }
983
984 /* disable board interrupts, deinit services */
Jeff Garzika52667f2007-10-02 22:55:53 +0200985 writeb(0xff, &dp6_ptr->io.irqdel);
986 writeb(0x00, &dp6_ptr->io.irqen);
987 writeb(0x00, &dp6_ptr->u.ic.S_Status);
988 writeb(0x00, &dp6_ptr->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Jeff Garzika52667f2007-10-02 22:55:53 +0200990 writel(pcistr->dpmem, &dp6_ptr->u.ic.S_Info[0]);
991 writeb(0xff, &dp6_ptr->u.ic.S_Cmd_Indx);
992 writeb(0, &dp6_ptr->io.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 retries = INIT_RETRIES;
994 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +0200995 while (readb(&dp6_ptr->u.ic.S_Status) != 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (--retries == 0) {
997 printk("GDT-PCI: Initialization error (DEINIT failed)\n");
998 iounmap(ha->brd);
999 return 0;
1000 }
1001 gdth_delay(1);
1002 }
Jeff Garzika52667f2007-10-02 22:55:53 +02001003 prot_ver = (unchar)readl(&dp6_ptr->u.ic.S_Info[0]);
1004 writeb(0, &dp6_ptr->u.ic.S_Status);
1005 writeb(0xff, &dp6_ptr->io.irqdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (prot_ver != PROTOCOL_VERSION) {
1007 printk("GDT-PCI: Illegal protocol version\n");
1008 iounmap(ha->brd);
1009 return 0;
1010 }
1011
1012 ha->type = GDT_PCI;
1013 ha->ic_all_size = sizeof(dp6_ptr->u);
1014
1015 /* special command to controller BIOS */
Jeff Garzika52667f2007-10-02 22:55:53 +02001016 writel(0x00, &dp6_ptr->u.ic.S_Info[0]);
1017 writel(0x00, &dp6_ptr->u.ic.S_Info[1]);
1018 writel(0x00, &dp6_ptr->u.ic.S_Info[2]);
1019 writel(0x00, &dp6_ptr->u.ic.S_Info[3]);
1020 writeb(0xfe, &dp6_ptr->u.ic.S_Cmd_Indx);
1021 writeb(0, &dp6_ptr->io.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 retries = INIT_RETRIES;
1023 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +02001024 while (readb(&dp6_ptr->u.ic.S_Status) != 0xfe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (--retries == 0) {
1026 printk("GDT-PCI: Initialization error\n");
1027 iounmap(ha->brd);
1028 return 0;
1029 }
1030 gdth_delay(1);
1031 }
Jeff Garzika52667f2007-10-02 22:55:53 +02001032 writeb(0, &dp6_ptr->u.ic.S_Status);
1033 writeb(0xff, &dp6_ptr->io.irqdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 ha->dma64_support = 0;
1036
Jeff Garzik8e9a8a02007-07-17 05:25:17 -04001037 } else if (ha->pdev->device <= PCI_DEVICE_ID_VORTEX_GDT6555) { /* GDT6110, ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 ha->plx = (gdt6c_plx_regs *)pcistr->io;
1039 TRACE2(("init_pci_new() dpmem %lx irq %d\n",
1040 pcistr->dpmem,ha->irq));
1041 ha->brd = ioremap(pcistr->dpmem, sizeof(gdt6c_dpram_str));
1042 if (ha->brd == NULL) {
1043 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1044 iounmap(ha->brd);
1045 return 0;
1046 }
1047 /* check and reset interface area */
1048 dp6c_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001049 writel(DPMEM_MAGIC, &dp6c_ptr->u);
1050 if (readl(&dp6c_ptr->u) != DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 printk("GDT-PCI: Cannot access DPMEM at 0x%lx (shadowed?)\n",
1052 pcistr->dpmem);
1053 found = FALSE;
1054 for (i = 0xC8000; i < 0xE8000; i += 0x4000) {
1055 iounmap(ha->brd);
1056 ha->brd = ioremap(i, sizeof(ushort));
1057 if (ha->brd == NULL) {
1058 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1059 return 0;
1060 }
Jeff Garzika52667f2007-10-02 22:55:53 +02001061 if (readw(ha->brd) != 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 TRACE2(("init_pci_plx() address 0x%x busy\n", i));
1063 continue;
1064 }
1065 iounmap(ha->brd);
1066 pci_write_config_dword(pcistr->pdev,
1067 PCI_BASE_ADDRESS_2, i);
1068 ha->brd = ioremap(i, sizeof(gdt6c_dpram_str));
1069 if (ha->brd == NULL) {
1070 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1071 return 0;
1072 }
1073 dp6c_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001074 writel(DPMEM_MAGIC, &dp6c_ptr->u);
1075 if (readl(&dp6c_ptr->u) == DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 printk("GDT-PCI: Use free address at 0x%x\n", i);
1077 found = TRUE;
1078 break;
1079 }
1080 }
1081 if (!found) {
1082 printk("GDT-PCI: No free address found!\n");
1083 iounmap(ha->brd);
1084 return 0;
1085 }
1086 }
1087 memset_io(&dp6c_ptr->u, 0, sizeof(dp6c_ptr->u));
Jeff Garzika52667f2007-10-02 22:55:53 +02001088 if (readl(&dp6c_ptr->u) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 printk("GDT-PCI: Initialization error (DPMEM write error)\n");
1090 iounmap(ha->brd);
1091 return 0;
1092 }
1093
1094 /* disable board interrupts, deinit services */
1095 outb(0x00,PTR2USHORT(&ha->plx->control1));
1096 outb(0xff,PTR2USHORT(&ha->plx->edoor_reg));
1097
Jeff Garzika52667f2007-10-02 22:55:53 +02001098 writeb(0x00, &dp6c_ptr->u.ic.S_Status);
1099 writeb(0x00, &dp6c_ptr->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Jeff Garzika52667f2007-10-02 22:55:53 +02001101 writel(pcistr->dpmem, &dp6c_ptr->u.ic.S_Info[0]);
1102 writeb(0xff, &dp6c_ptr->u.ic.S_Cmd_Indx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 outb(1,PTR2USHORT(&ha->plx->ldoor_reg));
1105
1106 retries = INIT_RETRIES;
1107 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +02001108 while (readb(&dp6c_ptr->u.ic.S_Status) != 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (--retries == 0) {
1110 printk("GDT-PCI: Initialization error (DEINIT failed)\n");
1111 iounmap(ha->brd);
1112 return 0;
1113 }
1114 gdth_delay(1);
1115 }
Jeff Garzika52667f2007-10-02 22:55:53 +02001116 prot_ver = (unchar)readl(&dp6c_ptr->u.ic.S_Info[0]);
1117 writeb(0, &dp6c_ptr->u.ic.Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (prot_ver != PROTOCOL_VERSION) {
1119 printk("GDT-PCI: Illegal protocol version\n");
1120 iounmap(ha->brd);
1121 return 0;
1122 }
1123
1124 ha->type = GDT_PCINEW;
1125 ha->ic_all_size = sizeof(dp6c_ptr->u);
1126
1127 /* special command to controller BIOS */
Jeff Garzika52667f2007-10-02 22:55:53 +02001128 writel(0x00, &dp6c_ptr->u.ic.S_Info[0]);
1129 writel(0x00, &dp6c_ptr->u.ic.S_Info[1]);
1130 writel(0x00, &dp6c_ptr->u.ic.S_Info[2]);
1131 writel(0x00, &dp6c_ptr->u.ic.S_Info[3]);
1132 writeb(0xfe, &dp6c_ptr->u.ic.S_Cmd_Indx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 outb(1,PTR2USHORT(&ha->plx->ldoor_reg));
1135
1136 retries = INIT_RETRIES;
1137 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +02001138 while (readb(&dp6c_ptr->u.ic.S_Status) != 0xfe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (--retries == 0) {
1140 printk("GDT-PCI: Initialization error\n");
1141 iounmap(ha->brd);
1142 return 0;
1143 }
1144 gdth_delay(1);
1145 }
Jeff Garzika52667f2007-10-02 22:55:53 +02001146 writeb(0, &dp6c_ptr->u.ic.S_Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 ha->dma64_support = 0;
1149
1150 } else { /* MPR */
1151 TRACE2(("init_pci_mpr() dpmem %lx irq %d\n",pcistr->dpmem,ha->irq));
1152 ha->brd = ioremap(pcistr->dpmem, sizeof(gdt6m_dpram_str));
1153 if (ha->brd == NULL) {
1154 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1155 return 0;
1156 }
1157
1158 /* manipulate config. space to enable DPMEM, start RP controller */
1159 pci_read_config_word(pcistr->pdev, PCI_COMMAND, &command);
1160 command |= 6;
1161 pci_write_config_word(pcistr->pdev, PCI_COMMAND, command);
1162 if (pci_resource_start(pcistr->pdev, 8) == 1UL)
1163 pci_resource_start(pcistr->pdev, 8) = 0UL;
1164 i = 0xFEFF0001UL;
1165 pci_write_config_dword(pcistr->pdev, PCI_ROM_ADDRESS, i);
1166 gdth_delay(1);
1167 pci_write_config_dword(pcistr->pdev, PCI_ROM_ADDRESS,
1168 pci_resource_start(pcistr->pdev, 8));
1169
1170 dp6m_ptr = ha->brd;
1171
1172 /* Ensure that it is safe to access the non HW portions of DPMEM.
1173 * Aditional check needed for Xscale based RAID controllers */
Jeff Garzika52667f2007-10-02 22:55:53 +02001174 while( ((int)readb(&dp6m_ptr->i960r.sema0_reg) ) & 3 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 gdth_delay(1);
1176
1177 /* check and reset interface area */
Jeff Garzika52667f2007-10-02 22:55:53 +02001178 writel(DPMEM_MAGIC, &dp6m_ptr->u);
1179 if (readl(&dp6m_ptr->u) != DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 printk("GDT-PCI: Cannot access DPMEM at 0x%lx (shadowed?)\n",
1181 pcistr->dpmem);
1182 found = FALSE;
1183 for (i = 0xC8000; i < 0xE8000; i += 0x4000) {
1184 iounmap(ha->brd);
1185 ha->brd = ioremap(i, sizeof(ushort));
1186 if (ha->brd == NULL) {
1187 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1188 return 0;
1189 }
Jeff Garzika52667f2007-10-02 22:55:53 +02001190 if (readw(ha->brd) != 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 TRACE2(("init_pci_mpr() address 0x%x busy\n", i));
1192 continue;
1193 }
1194 iounmap(ha->brd);
1195 pci_write_config_dword(pcistr->pdev,
1196 PCI_BASE_ADDRESS_0, i);
1197 ha->brd = ioremap(i, sizeof(gdt6m_dpram_str));
1198 if (ha->brd == NULL) {
1199 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1200 return 0;
1201 }
1202 dp6m_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001203 writel(DPMEM_MAGIC, &dp6m_ptr->u);
1204 if (readl(&dp6m_ptr->u) == DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 printk("GDT-PCI: Use free address at 0x%x\n", i);
1206 found = TRUE;
1207 break;
1208 }
1209 }
1210 if (!found) {
1211 printk("GDT-PCI: No free address found!\n");
1212 iounmap(ha->brd);
1213 return 0;
1214 }
1215 }
1216 memset_io(&dp6m_ptr->u, 0, sizeof(dp6m_ptr->u));
1217
1218 /* disable board interrupts, deinit services */
Jeff Garzika52667f2007-10-02 22:55:53 +02001219 writeb(readb(&dp6m_ptr->i960r.edoor_en_reg) | 4,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 &dp6m_ptr->i960r.edoor_en_reg);
Jeff Garzika52667f2007-10-02 22:55:53 +02001221 writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
1222 writeb(0x00, &dp6m_ptr->u.ic.S_Status);
1223 writeb(0x00, &dp6m_ptr->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Jeff Garzika52667f2007-10-02 22:55:53 +02001225 writel(pcistr->dpmem, &dp6m_ptr->u.ic.S_Info[0]);
1226 writeb(0xff, &dp6m_ptr->u.ic.S_Cmd_Indx);
1227 writeb(1, &dp6m_ptr->i960r.ldoor_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 retries = INIT_RETRIES;
1229 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +02001230 while (readb(&dp6m_ptr->u.ic.S_Status) != 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (--retries == 0) {
1232 printk("GDT-PCI: Initialization error (DEINIT failed)\n");
1233 iounmap(ha->brd);
1234 return 0;
1235 }
1236 gdth_delay(1);
1237 }
Jeff Garzika52667f2007-10-02 22:55:53 +02001238 prot_ver = (unchar)readl(&dp6m_ptr->u.ic.S_Info[0]);
1239 writeb(0, &dp6m_ptr->u.ic.S_Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (prot_ver != PROTOCOL_VERSION) {
1241 printk("GDT-PCI: Illegal protocol version\n");
1242 iounmap(ha->brd);
1243 return 0;
1244 }
1245
1246 ha->type = GDT_PCIMPR;
1247 ha->ic_all_size = sizeof(dp6m_ptr->u);
1248
1249 /* special command to controller BIOS */
Jeff Garzika52667f2007-10-02 22:55:53 +02001250 writel(0x00, &dp6m_ptr->u.ic.S_Info[0]);
1251 writel(0x00, &dp6m_ptr->u.ic.S_Info[1]);
1252 writel(0x00, &dp6m_ptr->u.ic.S_Info[2]);
1253 writel(0x00, &dp6m_ptr->u.ic.S_Info[3]);
1254 writeb(0xfe, &dp6m_ptr->u.ic.S_Cmd_Indx);
1255 writeb(1, &dp6m_ptr->i960r.ldoor_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 retries = INIT_RETRIES;
1257 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +02001258 while (readb(&dp6m_ptr->u.ic.S_Status) != 0xfe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (--retries == 0) {
1260 printk("GDT-PCI: Initialization error\n");
1261 iounmap(ha->brd);
1262 return 0;
1263 }
1264 gdth_delay(1);
1265 }
Jeff Garzika52667f2007-10-02 22:55:53 +02001266 writeb(0, &dp6m_ptr->u.ic.S_Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 /* read FW version to detect 64-bit DMA support */
Jeff Garzika52667f2007-10-02 22:55:53 +02001269 writeb(0xfd, &dp6m_ptr->u.ic.S_Cmd_Indx);
1270 writeb(1, &dp6m_ptr->i960r.ldoor_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 retries = INIT_RETRIES;
1272 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +02001273 while (readb(&dp6m_ptr->u.ic.S_Status) != 0xfd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (--retries == 0) {
1275 printk("GDT-PCI: Initialization error (DEINIT failed)\n");
1276 iounmap(ha->brd);
1277 return 0;
1278 }
1279 gdth_delay(1);
1280 }
Jeff Garzika52667f2007-10-02 22:55:53 +02001281 prot_ver = (unchar)(readl(&dp6m_ptr->u.ic.S_Info[0]) >> 16);
1282 writeb(0, &dp6m_ptr->u.ic.S_Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (prot_ver < 0x2b) /* FW < x.43: no 64-bit DMA support */
1284 ha->dma64_support = 0;
1285 else
1286 ha->dma64_support = 1;
1287 }
1288
1289 return 1;
1290}
Christoph Hellwig8514ef22007-10-02 22:51:06 +02001291#endif /* CONFIG_PCI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293/* controller protocol functions */
1294
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001295static void __init gdth_enable_int(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 ulong flags;
1298 gdt2_dpram_str __iomem *dp2_ptr;
1299 gdt6_dpram_str __iomem *dp6_ptr;
1300 gdt6m_dpram_str __iomem *dp6m_ptr;
1301
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001302 TRACE(("gdth_enable_int() hanum %d\n",ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 spin_lock_irqsave(&ha->smp_lock, flags);
1304
1305 if (ha->type == GDT_EISA) {
1306 outb(0xff, ha->bmic + EDOORREG);
1307 outb(0xff, ha->bmic + EDENABREG);
1308 outb(0x01, ha->bmic + EINTENABREG);
1309 } else if (ha->type == GDT_ISA) {
1310 dp2_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001311 writeb(1, &dp2_ptr->io.irqdel);
1312 writeb(0, &dp2_ptr->u.ic.Cmd_Index);
1313 writeb(1, &dp2_ptr->io.irqen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 } else if (ha->type == GDT_PCI) {
1315 dp6_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001316 writeb(1, &dp6_ptr->io.irqdel);
1317 writeb(0, &dp6_ptr->u.ic.Cmd_Index);
1318 writeb(1, &dp6_ptr->io.irqen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 } else if (ha->type == GDT_PCINEW) {
1320 outb(0xff, PTR2USHORT(&ha->plx->edoor_reg));
1321 outb(0x03, PTR2USHORT(&ha->plx->control1));
1322 } else if (ha->type == GDT_PCIMPR) {
1323 dp6m_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001324 writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
1325 writeb(readb(&dp6m_ptr->i960r.edoor_en_reg) & ~4,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 &dp6m_ptr->i960r.edoor_en_reg);
1327 }
1328 spin_unlock_irqrestore(&ha->smp_lock, flags);
1329}
1330
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001331/* return IStatus if interrupt was from this card else 0 */
Jeff Garzik230e8862007-12-13 16:14:12 -08001332static unchar gdth_get_status(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001334 unchar IStatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Jeff Garzik230e8862007-12-13 16:14:12 -08001336 TRACE(("gdth_get_status() irq %d ctr_count %d\n", ha->irq, gdth_ctr_count));
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (ha->type == GDT_EISA)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001339 IStatus = inb((ushort)ha->bmic + EDOORREG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 else if (ha->type == GDT_ISA)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001341 IStatus =
Jeff Garzika52667f2007-10-02 22:55:53 +02001342 readb(&((gdt2_dpram_str __iomem *)ha->brd)->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 else if (ha->type == GDT_PCI)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001344 IStatus =
Jeff Garzika52667f2007-10-02 22:55:53 +02001345 readb(&((gdt6_dpram_str __iomem *)ha->brd)->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 else if (ha->type == GDT_PCINEW)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001347 IStatus = inb(PTR2USHORT(&ha->plx->edoor_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 else if (ha->type == GDT_PCIMPR)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001349 IStatus =
Jeff Garzika52667f2007-10-02 22:55:53 +02001350 readb(&((gdt6m_dpram_str __iomem *)ha->brd)->i960r.edoor_reg);
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001351
1352 return IStatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001354
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001355static int gdth_test_busy(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 register int gdtsema0 = 0;
1358
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001359 TRACE(("gdth_test_busy() hanum %d\n", ha->hanum));
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (ha->type == GDT_EISA)
1362 gdtsema0 = (int)inb(ha->bmic + SEMA0REG);
1363 else if (ha->type == GDT_ISA)
Jeff Garzika52667f2007-10-02 22:55:53 +02001364 gdtsema0 = (int)readb(&((gdt2_dpram_str __iomem *)ha->brd)->u.ic.Sema0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 else if (ha->type == GDT_PCI)
Jeff Garzika52667f2007-10-02 22:55:53 +02001366 gdtsema0 = (int)readb(&((gdt6_dpram_str __iomem *)ha->brd)->u.ic.Sema0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 else if (ha->type == GDT_PCINEW)
1368 gdtsema0 = (int)inb(PTR2USHORT(&ha->plx->sema0_reg));
1369 else if (ha->type == GDT_PCIMPR)
1370 gdtsema0 =
Jeff Garzika52667f2007-10-02 22:55:53 +02001371 (int)readb(&((gdt6m_dpram_str __iomem *)ha->brd)->i960r.sema0_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 return (gdtsema0 & 1);
1374}
1375
1376
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001377static int gdth_get_cmd_index(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 int i;
1380
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001381 TRACE(("gdth_get_cmd_index() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 for (i=0; i<GDTH_MAXCMDS; ++i) {
1384 if (ha->cmd_tab[i].cmnd == UNUSED_CMND) {
1385 ha->cmd_tab[i].cmnd = ha->pccb->RequestBuffer;
1386 ha->cmd_tab[i].service = ha->pccb->Service;
1387 ha->pccb->CommandIndex = (ulong32)i+2;
1388 return (i+2);
1389 }
1390 }
1391 return 0;
1392}
1393
1394
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001395static void gdth_set_sema0(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001397 TRACE(("gdth_set_sema0() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (ha->type == GDT_EISA) {
1400 outb(1, ha->bmic + SEMA0REG);
1401 } else if (ha->type == GDT_ISA) {
Jeff Garzika52667f2007-10-02 22:55:53 +02001402 writeb(1, &((gdt2_dpram_str __iomem *)ha->brd)->u.ic.Sema0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 } else if (ha->type == GDT_PCI) {
Jeff Garzika52667f2007-10-02 22:55:53 +02001404 writeb(1, &((gdt6_dpram_str __iomem *)ha->brd)->u.ic.Sema0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 } else if (ha->type == GDT_PCINEW) {
1406 outb(1, PTR2USHORT(&ha->plx->sema0_reg));
1407 } else if (ha->type == GDT_PCIMPR) {
Jeff Garzika52667f2007-10-02 22:55:53 +02001408 writeb(1, &((gdt6m_dpram_str __iomem *)ha->brd)->i960r.sema0_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
1410}
1411
1412
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001413static void gdth_copy_command(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 register gdth_cmd_str *cmd_ptr;
1416 register gdt6m_dpram_str __iomem *dp6m_ptr;
1417 register gdt6c_dpram_str __iomem *dp6c_ptr;
1418 gdt6_dpram_str __iomem *dp6_ptr;
1419 gdt2_dpram_str __iomem *dp2_ptr;
1420 ushort cp_count,dp_offset,cmd_no;
1421
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001422 TRACE(("gdth_copy_command() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 cp_count = ha->cmd_len;
1425 dp_offset= ha->cmd_offs_dpmem;
1426 cmd_no = ha->cmd_cnt;
1427 cmd_ptr = ha->pccb;
1428
1429 ++ha->cmd_cnt;
1430 if (ha->type == GDT_EISA)
1431 return; /* no DPMEM, no copy */
1432
1433 /* set cpcount dword aligned */
1434 if (cp_count & 3)
1435 cp_count += (4 - (cp_count & 3));
1436
1437 ha->cmd_offs_dpmem += cp_count;
1438
1439 /* set offset and service, copy command to DPMEM */
1440 if (ha->type == GDT_ISA) {
1441 dp2_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001442 writew(dp_offset + DPMEM_COMMAND_OFFSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 &dp2_ptr->u.ic.comm_queue[cmd_no].offset);
Jeff Garzika52667f2007-10-02 22:55:53 +02001444 writew((ushort)cmd_ptr->Service,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 &dp2_ptr->u.ic.comm_queue[cmd_no].serv_id);
1446 memcpy_toio(&dp2_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
1447 } else if (ha->type == GDT_PCI) {
1448 dp6_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001449 writew(dp_offset + DPMEM_COMMAND_OFFSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 &dp6_ptr->u.ic.comm_queue[cmd_no].offset);
Jeff Garzika52667f2007-10-02 22:55:53 +02001451 writew((ushort)cmd_ptr->Service,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 &dp6_ptr->u.ic.comm_queue[cmd_no].serv_id);
1453 memcpy_toio(&dp6_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
1454 } else if (ha->type == GDT_PCINEW) {
1455 dp6c_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001456 writew(dp_offset + DPMEM_COMMAND_OFFSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 &dp6c_ptr->u.ic.comm_queue[cmd_no].offset);
Jeff Garzika52667f2007-10-02 22:55:53 +02001458 writew((ushort)cmd_ptr->Service,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 &dp6c_ptr->u.ic.comm_queue[cmd_no].serv_id);
1460 memcpy_toio(&dp6c_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
1461 } else if (ha->type == GDT_PCIMPR) {
1462 dp6m_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001463 writew(dp_offset + DPMEM_COMMAND_OFFSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 &dp6m_ptr->u.ic.comm_queue[cmd_no].offset);
Jeff Garzika52667f2007-10-02 22:55:53 +02001465 writew((ushort)cmd_ptr->Service,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 &dp6m_ptr->u.ic.comm_queue[cmd_no].serv_id);
1467 memcpy_toio(&dp6m_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
1468 }
1469}
1470
1471
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001472static void gdth_release_event(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001474 TRACE(("gdth_release_event() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476#ifdef GDTH_STATISTICS
1477 {
1478 ulong32 i,j;
1479 for (i=0,j=0; j<GDTH_MAXCMDS; ++j) {
1480 if (ha->cmd_tab[j].cmnd != UNUSED_CMND)
1481 ++i;
1482 }
1483 if (max_index < i) {
1484 max_index = i;
1485 TRACE3(("GDT: max_index = %d\n",(ushort)i));
1486 }
1487 }
1488#endif
1489
1490 if (ha->pccb->OpCode == GDT_INIT)
1491 ha->pccb->Service |= 0x80;
1492
1493 if (ha->type == GDT_EISA) {
1494 if (ha->pccb->OpCode == GDT_INIT) /* store DMA buffer */
1495 outl(ha->ccb_phys, ha->bmic + MAILBOXREG);
1496 outb(ha->pccb->Service, ha->bmic + LDOORREG);
1497 } else if (ha->type == GDT_ISA) {
Jeff Garzika52667f2007-10-02 22:55:53 +02001498 writeb(0, &((gdt2_dpram_str __iomem *)ha->brd)->io.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 } else if (ha->type == GDT_PCI) {
Jeff Garzika52667f2007-10-02 22:55:53 +02001500 writeb(0, &((gdt6_dpram_str __iomem *)ha->brd)->io.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 } else if (ha->type == GDT_PCINEW) {
1502 outb(1, PTR2USHORT(&ha->plx->ldoor_reg));
1503 } else if (ha->type == GDT_PCIMPR) {
Jeff Garzika52667f2007-10-02 22:55:53 +02001504 writeb(1, &((gdt6m_dpram_str __iomem *)ha->brd)->i960r.ldoor_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
1506}
1507
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001508static int gdth_wait(gdth_ha_str *ha, int index, ulong32 time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 int answer_found = FALSE;
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001511 int wait_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001513 TRACE(("gdth_wait() hanum %d index %d time %d\n", ha->hanum, index, time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 if (index == 0)
1516 return 1; /* no wait required */
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 do {
Jeff Garzik230e8862007-12-13 16:14:12 -08001519 __gdth_interrupt(ha, true, &wait_index);
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001520 if (wait_index == index) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 answer_found = TRUE;
1522 break;
1523 }
1524 gdth_delay(1);
1525 } while (--time);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001526
1527 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 gdth_delay(0);
1529
1530 return (answer_found);
1531}
1532
1533
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001534static int gdth_internal_cmd(gdth_ha_str *ha, unchar service, ushort opcode,
1535 ulong32 p1, ulong64 p2, ulong64 p3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 register gdth_cmd_str *cmd_ptr;
1538 int retries,index;
1539
1540 TRACE2(("gdth_internal_cmd() service %d opcode %d\n",service,opcode));
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 cmd_ptr = ha->pccb;
1543 memset((char*)cmd_ptr,0,sizeof(gdth_cmd_str));
1544
1545 /* make command */
1546 for (retries = INIT_RETRIES;;) {
1547 cmd_ptr->Service = service;
1548 cmd_ptr->RequestBuffer = INTERNAL_CMND;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001549 if (!(index=gdth_get_cmd_index(ha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 TRACE(("GDT: No free command index found\n"));
1551 return 0;
1552 }
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001553 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 cmd_ptr->OpCode = opcode;
1555 cmd_ptr->BoardNode = LOCALBOARD;
1556 if (service == CACHESERVICE) {
1557 if (opcode == GDT_IOCTL) {
1558 cmd_ptr->u.ioctl.subfunc = p1;
1559 cmd_ptr->u.ioctl.channel = (ulong32)p2;
1560 cmd_ptr->u.ioctl.param_size = (ushort)p3;
1561 cmd_ptr->u.ioctl.p_param = ha->scratch_phys;
1562 } else {
1563 if (ha->cache_feat & GDT_64BIT) {
1564 cmd_ptr->u.cache64.DeviceNo = (ushort)p1;
1565 cmd_ptr->u.cache64.BlockNo = p2;
1566 } else {
1567 cmd_ptr->u.cache.DeviceNo = (ushort)p1;
1568 cmd_ptr->u.cache.BlockNo = (ulong32)p2;
1569 }
1570 }
1571 } else if (service == SCSIRAWSERVICE) {
1572 if (ha->raw_feat & GDT_64BIT) {
1573 cmd_ptr->u.raw64.direction = p1;
1574 cmd_ptr->u.raw64.bus = (unchar)p2;
1575 cmd_ptr->u.raw64.target = (unchar)p3;
1576 cmd_ptr->u.raw64.lun = (unchar)(p3 >> 8);
1577 } else {
1578 cmd_ptr->u.raw.direction = p1;
1579 cmd_ptr->u.raw.bus = (unchar)p2;
1580 cmd_ptr->u.raw.target = (unchar)p3;
1581 cmd_ptr->u.raw.lun = (unchar)(p3 >> 8);
1582 }
1583 } else if (service == SCREENSERVICE) {
1584 if (opcode == GDT_REALTIME) {
1585 *(ulong32 *)&cmd_ptr->u.screen.su.data[0] = p1;
1586 *(ulong32 *)&cmd_ptr->u.screen.su.data[4] = (ulong32)p2;
1587 *(ulong32 *)&cmd_ptr->u.screen.su.data[8] = (ulong32)p3;
1588 }
1589 }
1590 ha->cmd_len = sizeof(gdth_cmd_str);
1591 ha->cmd_offs_dpmem = 0;
1592 ha->cmd_cnt = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001593 gdth_copy_command(ha);
1594 gdth_release_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 gdth_delay(20);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001596 if (!gdth_wait(ha, index, INIT_TIMEOUT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 printk("GDT: Initialization error (timeout service %d)\n",service);
1598 return 0;
1599 }
1600 if (ha->status != S_BSY || --retries == 0)
1601 break;
1602 gdth_delay(1);
1603 }
1604
1605 return (ha->status != S_OK ? 0:1);
1606}
1607
1608
1609/* search for devices */
1610
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001611static int __init gdth_search_drives(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 ushort cdev_cnt, i;
1614 int ok;
1615 ulong32 bus_no, drv_cnt, drv_no, j;
1616 gdth_getch_str *chn;
1617 gdth_drlist_str *drl;
1618 gdth_iochan_str *ioc;
1619 gdth_raw_iochan_str *iocr;
1620 gdth_arcdl_str *alst;
1621 gdth_alist_str *alst2;
1622 gdth_oem_str_ioctl *oemstr;
1623#ifdef INT_COAL
1624 gdth_perf_modes *pmod;
1625#endif
1626
1627#ifdef GDTH_RTC
1628 unchar rtc[12];
1629 ulong flags;
1630#endif
1631
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001632 TRACE(("gdth_search_drives() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 ok = 0;
1634
1635 /* initialize controller services, at first: screen service */
1636 ha->screen_feat = 0;
1637 if (!force_dma32) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001638 ok = gdth_internal_cmd(ha, SCREENSERVICE, GDT_X_INIT_SCR, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if (ok)
1640 ha->screen_feat = GDT_64BIT;
1641 }
1642 if (force_dma32 || (!ok && ha->status == (ushort)S_NOFUNC))
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001643 ok = gdth_internal_cmd(ha, SCREENSERVICE, GDT_INIT, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 if (!ok) {
1645 printk("GDT-HA %d: Initialization error screen service (code %d)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001646 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 return 0;
1648 }
1649 TRACE2(("gdth_search_drives(): SCREENSERVICE initialized\n"));
1650
1651#ifdef GDTH_RTC
1652 /* read realtime clock info, send to controller */
1653 /* 1. wait for the falling edge of update flag */
1654 spin_lock_irqsave(&rtc_lock, flags);
1655 for (j = 0; j < 1000000; ++j)
1656 if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
1657 break;
1658 for (j = 0; j < 1000000; ++j)
1659 if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
1660 break;
1661 /* 2. read info */
1662 do {
1663 for (j = 0; j < 12; ++j)
1664 rtc[j] = CMOS_READ(j);
1665 } while (rtc[0] != CMOS_READ(0));
Robert P. J. Day7a960b72007-05-23 14:41:40 -07001666 spin_unlock_irqrestore(&rtc_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 TRACE2(("gdth_search_drives(): RTC: %x/%x/%x\n",*(ulong32 *)&rtc[0],
1668 *(ulong32 *)&rtc[4], *(ulong32 *)&rtc[8]));
1669 /* 3. send to controller firmware */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001670 gdth_internal_cmd(ha, SCREENSERVICE, GDT_REALTIME, *(ulong32 *)&rtc[0],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 *(ulong32 *)&rtc[4], *(ulong32 *)&rtc[8]);
1672#endif
1673
1674 /* unfreeze all IOs */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001675 gdth_internal_cmd(ha, CACHESERVICE, GDT_UNFREEZE_IO, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 /* initialize cache service */
1678 ha->cache_feat = 0;
1679 if (!force_dma32) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001680 ok = gdth_internal_cmd(ha, CACHESERVICE, GDT_X_INIT_HOST, LINUX_OS,
1681 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 if (ok)
1683 ha->cache_feat = GDT_64BIT;
1684 }
1685 if (force_dma32 || (!ok && ha->status == (ushort)S_NOFUNC))
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001686 ok = gdth_internal_cmd(ha, CACHESERVICE, GDT_INIT, LINUX_OS, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if (!ok) {
1688 printk("GDT-HA %d: Initialization error cache service (code %d)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001689 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return 0;
1691 }
1692 TRACE2(("gdth_search_drives(): CACHESERVICE initialized\n"));
1693 cdev_cnt = (ushort)ha->info;
1694 ha->fw_vers = ha->service;
1695
1696#ifdef INT_COAL
1697 if (ha->type == GDT_PCIMPR) {
1698 /* set perf. modes */
1699 pmod = (gdth_perf_modes *)ha->pscratch;
1700 pmod->version = 1;
1701 pmod->st_mode = 1; /* enable one status buffer */
1702 *((ulong64 *)&pmod->st_buff_addr1) = ha->coal_stat_phys;
1703 pmod->st_buff_indx1 = COALINDEX;
1704 pmod->st_buff_addr2 = 0;
1705 pmod->st_buff_u_addr2 = 0;
1706 pmod->st_buff_indx2 = 0;
1707 pmod->st_buff_size = sizeof(gdth_coal_status) * MAXOFFSETS;
1708 pmod->cmd_mode = 0; // disable all cmd buffers
1709 pmod->cmd_buff_addr1 = 0;
1710 pmod->cmd_buff_u_addr1 = 0;
1711 pmod->cmd_buff_indx1 = 0;
1712 pmod->cmd_buff_addr2 = 0;
1713 pmod->cmd_buff_u_addr2 = 0;
1714 pmod->cmd_buff_indx2 = 0;
1715 pmod->cmd_buff_size = 0;
1716 pmod->reserved1 = 0;
1717 pmod->reserved2 = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001718 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, SET_PERF_MODES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 INVALID_CHANNEL,sizeof(gdth_perf_modes))) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001720 printk("GDT-HA %d: Interrupt coalescing activated\n", ha->hanum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
1722 }
1723#endif
1724
1725 /* detect number of buses - try new IOCTL */
1726 iocr = (gdth_raw_iochan_str *)ha->pscratch;
1727 iocr->hdr.version = 0xffffffff;
1728 iocr->hdr.list_entries = MAXBUS;
1729 iocr->hdr.first_chan = 0;
1730 iocr->hdr.last_chan = MAXBUS-1;
1731 iocr->hdr.list_offset = GDTOFFSOF(gdth_raw_iochan_str, list[0]);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001732 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, IOCHAN_RAW_DESC,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 INVALID_CHANNEL,sizeof(gdth_raw_iochan_str))) {
1734 TRACE2(("IOCHAN_RAW_DESC supported!\n"));
1735 ha->bus_cnt = iocr->hdr.chan_count;
1736 for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
1737 if (iocr->list[bus_no].proc_id < MAXID)
1738 ha->bus_id[bus_no] = iocr->list[bus_no].proc_id;
1739 else
1740 ha->bus_id[bus_no] = 0xff;
1741 }
1742 } else {
1743 /* old method */
1744 chn = (gdth_getch_str *)ha->pscratch;
1745 for (bus_no = 0; bus_no < MAXBUS; ++bus_no) {
1746 chn->channel_no = bus_no;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001747 if (!gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 SCSI_CHAN_CNT | L_CTRL_PATTERN,
1749 IO_CHANNEL | INVALID_CHANNEL,
1750 sizeof(gdth_getch_str))) {
1751 if (bus_no == 0) {
1752 printk("GDT-HA %d: Error detecting channel count (0x%x)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001753 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 return 0;
1755 }
1756 break;
1757 }
1758 if (chn->siop_id < MAXID)
1759 ha->bus_id[bus_no] = chn->siop_id;
1760 else
1761 ha->bus_id[bus_no] = 0xff;
1762 }
1763 ha->bus_cnt = (unchar)bus_no;
1764 }
1765 TRACE2(("gdth_search_drives() %d channels\n",ha->bus_cnt));
1766
1767 /* read cache configuration */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001768 if (!gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, CACHE_INFO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 INVALID_CHANNEL,sizeof(gdth_cinfo_str))) {
1770 printk("GDT-HA %d: Initialization error cache service (code %d)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001771 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return 0;
1773 }
1774 ha->cpar = ((gdth_cinfo_str *)ha->pscratch)->cpar;
1775 TRACE2(("gdth_search_drives() cinfo: vs %x sta %d str %d dw %d b %d\n",
1776 ha->cpar.version,ha->cpar.state,ha->cpar.strategy,
1777 ha->cpar.write_back,ha->cpar.block_size));
1778
1779 /* read board info and features */
1780 ha->more_proc = FALSE;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001781 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, BOARD_INFO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 INVALID_CHANNEL,sizeof(gdth_binfo_str))) {
1783 memcpy(&ha->binfo, (gdth_binfo_str *)ha->pscratch,
1784 sizeof(gdth_binfo_str));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001785 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, BOARD_FEATURES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 INVALID_CHANNEL,sizeof(gdth_bfeat_str))) {
1787 TRACE2(("BOARD_INFO/BOARD_FEATURES supported\n"));
1788 ha->bfeat = *(gdth_bfeat_str *)ha->pscratch;
1789 ha->more_proc = TRUE;
1790 }
1791 } else {
1792 TRACE2(("BOARD_INFO requires firmware >= 1.10/2.08\n"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001793 strcpy(ha->binfo.type_string, gdth_ctr_name(ha));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 }
1795 TRACE2(("Controller name: %s\n",ha->binfo.type_string));
1796
1797 /* read more informations */
1798 if (ha->more_proc) {
1799 /* physical drives, channel addresses */
1800 ioc = (gdth_iochan_str *)ha->pscratch;
1801 ioc->hdr.version = 0xffffffff;
1802 ioc->hdr.list_entries = MAXBUS;
1803 ioc->hdr.first_chan = 0;
1804 ioc->hdr.last_chan = MAXBUS-1;
1805 ioc->hdr.list_offset = GDTOFFSOF(gdth_iochan_str, list[0]);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001806 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, IOCHAN_DESC,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 INVALID_CHANNEL,sizeof(gdth_iochan_str))) {
1808 for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
1809 ha->raw[bus_no].address = ioc->list[bus_no].address;
1810 ha->raw[bus_no].local_no = ioc->list[bus_no].local_no;
1811 }
1812 } else {
1813 for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
1814 ha->raw[bus_no].address = IO_CHANNEL;
1815 ha->raw[bus_no].local_no = bus_no;
1816 }
1817 }
1818 for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
1819 chn = (gdth_getch_str *)ha->pscratch;
1820 chn->channel_no = ha->raw[bus_no].local_no;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001821 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 SCSI_CHAN_CNT | L_CTRL_PATTERN,
1823 ha->raw[bus_no].address | INVALID_CHANNEL,
1824 sizeof(gdth_getch_str))) {
1825 ha->raw[bus_no].pdev_cnt = chn->drive_cnt;
1826 TRACE2(("Channel %d: %d phys. drives\n",
1827 bus_no,chn->drive_cnt));
1828 }
1829 if (ha->raw[bus_no].pdev_cnt > 0) {
1830 drl = (gdth_drlist_str *)ha->pscratch;
1831 drl->sc_no = ha->raw[bus_no].local_no;
1832 drl->sc_cnt = ha->raw[bus_no].pdev_cnt;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001833 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 SCSI_DR_LIST | L_CTRL_PATTERN,
1835 ha->raw[bus_no].address | INVALID_CHANNEL,
1836 sizeof(gdth_drlist_str))) {
1837 for (j = 0; j < ha->raw[bus_no].pdev_cnt; ++j)
1838 ha->raw[bus_no].id_list[j] = drl->sc_list[j];
1839 } else {
1840 ha->raw[bus_no].pdev_cnt = 0;
1841 }
1842 }
1843 }
1844
1845 /* logical drives */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001846 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, CACHE_DRV_CNT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 INVALID_CHANNEL,sizeof(ulong32))) {
1848 drv_cnt = *(ulong32 *)ha->pscratch;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001849 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, CACHE_DRV_LIST,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 INVALID_CHANNEL,drv_cnt * sizeof(ulong32))) {
1851 for (j = 0; j < drv_cnt; ++j) {
1852 drv_no = ((ulong32 *)ha->pscratch)[j];
1853 if (drv_no < MAX_LDRIVES) {
1854 ha->hdr[drv_no].is_logdrv = TRUE;
1855 TRACE2(("Drive %d is log. drive\n",drv_no));
1856 }
1857 }
1858 }
1859 alst = (gdth_arcdl_str *)ha->pscratch;
1860 alst->entries_avail = MAX_LDRIVES;
1861 alst->first_entry = 0;
1862 alst->list_offset = GDTOFFSOF(gdth_arcdl_str, list[0]);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001863 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 ARRAY_DRV_LIST2 | LA_CTRL_PATTERN,
1865 INVALID_CHANNEL, sizeof(gdth_arcdl_str) +
1866 (alst->entries_avail-1) * sizeof(gdth_alist_str))) {
1867 for (j = 0; j < alst->entries_init; ++j) {
1868 ha->hdr[j].is_arraydrv = alst->list[j].is_arrayd;
1869 ha->hdr[j].is_master = alst->list[j].is_master;
1870 ha->hdr[j].is_parity = alst->list[j].is_parity;
1871 ha->hdr[j].is_hotfix = alst->list[j].is_hotfix;
1872 ha->hdr[j].master_no = alst->list[j].cd_handle;
1873 }
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001874 } else if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 ARRAY_DRV_LIST | LA_CTRL_PATTERN,
1876 0, 35 * sizeof(gdth_alist_str))) {
1877 for (j = 0; j < 35; ++j) {
1878 alst2 = &((gdth_alist_str *)ha->pscratch)[j];
1879 ha->hdr[j].is_arraydrv = alst2->is_arrayd;
1880 ha->hdr[j].is_master = alst2->is_master;
1881 ha->hdr[j].is_parity = alst2->is_parity;
1882 ha->hdr[j].is_hotfix = alst2->is_hotfix;
1883 ha->hdr[j].master_no = alst2->cd_handle;
1884 }
1885 }
1886 }
1887 }
1888
1889 /* initialize raw service */
1890 ha->raw_feat = 0;
1891 if (!force_dma32) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001892 ok = gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_X_INIT_RAW, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (ok)
1894 ha->raw_feat = GDT_64BIT;
1895 }
1896 if (force_dma32 || (!ok && ha->status == (ushort)S_NOFUNC))
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001897 ok = gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_INIT, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 if (!ok) {
1899 printk("GDT-HA %d: Initialization error raw service (code %d)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001900 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return 0;
1902 }
1903 TRACE2(("gdth_search_drives(): RAWSERVICE initialized\n"));
1904
1905 /* set/get features raw service (scatter/gather) */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001906 if (gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_SET_FEAT, SCATTER_GATHER,
1907 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 TRACE2(("gdth_search_drives(): set features RAWSERVICE OK\n"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001909 if (gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_GET_FEAT, 0, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 TRACE2(("gdth_search_dr(): get feat RAWSERVICE %d\n",
1911 ha->info));
1912 ha->raw_feat |= (ushort)ha->info;
1913 }
1914 }
1915
1916 /* set/get features cache service (equal to raw service) */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001917 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_SET_FEAT, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 SCATTER_GATHER,0)) {
1919 TRACE2(("gdth_search_drives(): set features CACHESERVICE OK\n"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001920 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_GET_FEAT, 0, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 TRACE2(("gdth_search_dr(): get feat CACHESERV. %d\n",
1922 ha->info));
1923 ha->cache_feat |= (ushort)ha->info;
1924 }
1925 }
1926
1927 /* reserve drives for raw service */
1928 if (reserve_mode != 0) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001929 gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_RESERVE_ALL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 reserve_mode == 1 ? 1 : 3, 0, 0);
1931 TRACE2(("gdth_search_drives(): RESERVE_ALL code %d\n",
1932 ha->status));
1933 }
1934 for (i = 0; i < MAX_RES_ARGS; i += 4) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001935 if (reserve_list[i] == ha->hanum && reserve_list[i+1] < ha->bus_cnt &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 reserve_list[i+2] < ha->tid_cnt && reserve_list[i+3] < MAXLUN) {
1937 TRACE2(("gdth_search_drives(): reserve ha %d bus %d id %d lun %d\n",
1938 reserve_list[i], reserve_list[i+1],
1939 reserve_list[i+2], reserve_list[i+3]));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001940 if (!gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_RESERVE, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 reserve_list[i+1], reserve_list[i+2] |
1942 (reserve_list[i+3] << 8))) {
1943 printk("GDT-HA %d: Error raw service (RESERVE, code %d)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001944 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
1946 }
1947 }
1948
1949 /* Determine OEM string using IOCTL */
1950 oemstr = (gdth_oem_str_ioctl *)ha->pscratch;
1951 oemstr->params.ctl_version = 0x01;
1952 oemstr->params.buffer_size = sizeof(oemstr->text);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001953 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 CACHE_READ_OEM_STRING_RECORD,INVALID_CHANNEL,
1955 sizeof(gdth_oem_str_ioctl))) {
1956 TRACE2(("gdth_search_drives(): CACHE_READ_OEM_STRING_RECORD OK\n"));
1957 printk("GDT-HA %d: Vendor: %s Name: %s\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001958 ha->hanum, oemstr->text.oem_company_name, ha->binfo.type_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 /* Save the Host Drive inquiry data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 strlcpy(ha->oem_name,oemstr->text.scsi_host_drive_inquiry_vendor_id,
1961 sizeof(ha->oem_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 } else {
1963 /* Old method, based on PCI ID */
1964 TRACE2(("gdth_search_drives(): CACHE_READ_OEM_STRING_RECORD failed\n"));
1965 printk("GDT-HA %d: Name: %s\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001966 ha->hanum, ha->binfo.type_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if (ha->oem_id == OEM_ID_INTEL)
1968 strlcpy(ha->oem_name,"Intel ", sizeof(ha->oem_name));
1969 else
1970 strlcpy(ha->oem_name,"ICP ", sizeof(ha->oem_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
1972
1973 /* scanning for host drives */
1974 for (i = 0; i < cdev_cnt; ++i)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001975 gdth_analyse_hdrive(ha, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 TRACE(("gdth_search_drives() OK\n"));
1978 return 1;
1979}
1980
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001981static int gdth_analyse_hdrive(gdth_ha_str *ha, ushort hdrive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 ulong32 drv_cyls;
1984 int drv_hds, drv_secs;
1985
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001986 TRACE(("gdth_analyse_hdrive() hanum %d drive %d\n", ha->hanum, hdrive));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 if (hdrive >= MAX_HDRIVES)
1988 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001990 if (!gdth_internal_cmd(ha, CACHESERVICE, GDT_INFO, hdrive, 0, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 return 0;
1992 ha->hdr[hdrive].present = TRUE;
1993 ha->hdr[hdrive].size = ha->info;
1994
1995 /* evaluate mapping (sectors per head, heads per cylinder) */
1996 ha->hdr[hdrive].size &= ~SECS32;
1997 if (ha->info2 == 0) {
1998 gdth_eval_mapping(ha->hdr[hdrive].size,&drv_cyls,&drv_hds,&drv_secs);
1999 } else {
2000 drv_hds = ha->info2 & 0xff;
2001 drv_secs = (ha->info2 >> 8) & 0xff;
2002 drv_cyls = (ulong32)ha->hdr[hdrive].size / drv_hds / drv_secs;
2003 }
2004 ha->hdr[hdrive].heads = (unchar)drv_hds;
2005 ha->hdr[hdrive].secs = (unchar)drv_secs;
2006 /* round size */
2007 ha->hdr[hdrive].size = drv_cyls * drv_hds * drv_secs;
2008
2009 if (ha->cache_feat & GDT_64BIT) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002010 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_X_INFO, hdrive, 0, 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 && ha->info2 != 0) {
2012 ha->hdr[hdrive].size = ((ulong64)ha->info2 << 32) | ha->info;
2013 }
2014 }
2015 TRACE2(("gdth_search_dr() cdr. %d size %d hds %d scs %d\n",
2016 hdrive,ha->hdr[hdrive].size,drv_hds,drv_secs));
2017
2018 /* get informations about device */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002019 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_DEVTYPE, hdrive, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 TRACE2(("gdth_search_dr() cache drive %d devtype %d\n",
2021 hdrive,ha->info));
2022 ha->hdr[hdrive].devtype = (ushort)ha->info;
2023 }
2024
2025 /* cluster info */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002026 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_CLUST_INFO, hdrive, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 TRACE2(("gdth_search_dr() cache drive %d cluster info %d\n",
2028 hdrive,ha->info));
2029 if (!shared_access)
2030 ha->hdr[hdrive].cluster_type = (unchar)ha->info;
2031 }
2032
2033 /* R/W attributes */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002034 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_RW_ATTRIBS, hdrive, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 TRACE2(("gdth_search_dr() cache drive %d r/w attrib. %d\n",
2036 hdrive,ha->info));
2037 ha->hdr[hdrive].rw_attribs = (unchar)ha->info;
2038 }
2039
2040 return 1;
2041}
2042
2043
2044/* command queueing/sending functions */
2045
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002046static void gdth_putq(gdth_ha_str *ha, Scsi_Cmnd *scp, unchar priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02002048 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 register Scsi_Cmnd *pscp;
2050 register Scsi_Cmnd *nscp;
2051 ulong flags;
2052 unchar b, t;
2053
2054 TRACE(("gdth_putq() priority %d\n",priority));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 spin_lock_irqsave(&ha->smp_lock, flags);
2056
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02002057 if (!cmndinfo->internal_command) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002058 cmndinfo->priority = priority;
Christoph Hellwig52759e62007-10-02 22:59:53 +02002059 b = scp->device->channel;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07002060 t = scp->device->id;
2061 if (priority >= DEFAULT_PRI) {
2062 if ((b != ha->virt_bus && ha->raw[BUS_L2P(ha,b)].lock) ||
2063 (b==ha->virt_bus && t<MAX_HDRIVES && ha->hdr[t].lock)) {
2064 TRACE2(("gdth_putq(): locked IO ->update_timeout()\n"));
Boaz Harroshf842b642007-10-02 23:16:01 +02002065 cmndinfo->timeout = gdth_update_timeout(scp, 0);
Leubner, Achimcbd5f692006-06-09 11:34:29 -07002066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
2068 }
2069
2070 if (ha->req_first==NULL) {
2071 ha->req_first = scp; /* queue was empty */
2072 scp->SCp.ptr = NULL;
2073 } else { /* queue not empty */
2074 pscp = ha->req_first;
2075 nscp = (Scsi_Cmnd *)pscp->SCp.ptr;
2076 /* priority: 0-highest,..,0xff-lowest */
Boaz Harroshf842b642007-10-02 23:16:01 +02002077 while (nscp && gdth_cmnd_priv(nscp)->priority <= priority) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 pscp = nscp;
2079 nscp = (Scsi_Cmnd *)pscp->SCp.ptr;
2080 }
2081 pscp->SCp.ptr = (char *)scp;
2082 scp->SCp.ptr = (char *)nscp;
2083 }
2084 spin_unlock_irqrestore(&ha->smp_lock, flags);
2085
2086#ifdef GDTH_STATISTICS
2087 flags = 0;
2088 for (nscp=ha->req_first; nscp; nscp=(Scsi_Cmnd*)nscp->SCp.ptr)
2089 ++flags;
2090 if (max_rq < flags) {
2091 max_rq = flags;
2092 TRACE3(("GDT: max_rq = %d\n",(ushort)max_rq));
2093 }
2094#endif
2095}
2096
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002097static void gdth_next(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 register Scsi_Cmnd *pscp;
2100 register Scsi_Cmnd *nscp;
2101 unchar b, t, l, firsttime;
2102 unchar this_cmd, next_cmd;
2103 ulong flags = 0;
2104 int cmd_index;
2105
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002106 TRACE(("gdth_next() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 if (!gdth_polling)
2108 spin_lock_irqsave(&ha->smp_lock, flags);
2109
2110 ha->cmd_cnt = ha->cmd_offs_dpmem = 0;
2111 this_cmd = firsttime = TRUE;
2112 next_cmd = gdth_polling ? FALSE:TRUE;
2113 cmd_index = 0;
2114
2115 for (nscp = pscp = ha->req_first; nscp; nscp = (Scsi_Cmnd *)nscp->SCp.ptr) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002116 struct gdth_cmndinfo *nscp_cmndinfo = gdth_cmnd_priv(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 if (nscp != pscp && nscp != (Scsi_Cmnd *)pscp->SCp.ptr)
2118 pscp = (Scsi_Cmnd *)pscp->SCp.ptr;
Boaz Harroshf842b642007-10-02 23:16:01 +02002119 if (!nscp_cmndinfo->internal_command) {
Christoph Hellwig52759e62007-10-02 22:59:53 +02002120 b = nscp->device->channel;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07002121 t = nscp->device->id;
2122 l = nscp->device->lun;
Boaz Harroshf842b642007-10-02 23:16:01 +02002123 if (nscp_cmndinfo->priority >= DEFAULT_PRI) {
Leubner, Achimcbd5f692006-06-09 11:34:29 -07002124 if ((b != ha->virt_bus && ha->raw[BUS_L2P(ha,b)].lock) ||
2125 (b == ha->virt_bus && t < MAX_HDRIVES && ha->hdr[t].lock))
2126 continue;
2127 }
2128 } else
2129 b = t = l = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
2131 if (firsttime) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002132 if (gdth_test_busy(ha)) { /* controller busy ? */
2133 TRACE(("gdth_next() controller %d busy !\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 if (!gdth_polling) {
2135 spin_unlock_irqrestore(&ha->smp_lock, flags);
2136 return;
2137 }
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002138 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 gdth_delay(1);
2140 }
2141 firsttime = FALSE;
2142 }
2143
Boaz Harroshf842b642007-10-02 23:16:01 +02002144 if (!nscp_cmndinfo->internal_command) {
2145 if (nscp_cmndinfo->phase == -1) {
2146 nscp_cmndinfo->phase = CACHESERVICE; /* default: cache svc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 if (nscp->cmnd[0] == TEST_UNIT_READY) {
2148 TRACE2(("TEST_UNIT_READY Bus %d Id %d LUN %d\n",
2149 b, t, l));
2150 /* TEST_UNIT_READY -> set scan mode */
2151 if ((ha->scan_mode & 0x0f) == 0) {
2152 if (b == 0 && t == 0 && l == 0) {
2153 ha->scan_mode |= 1;
2154 TRACE2(("Scan mode: 0x%x\n", ha->scan_mode));
2155 }
2156 } else if ((ha->scan_mode & 0x0f) == 1) {
2157 if (b == 0 && ((t == 0 && l == 1) ||
2158 (t == 1 && l == 0))) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002159 nscp_cmndinfo->OpCode = GDT_SCAN_START;
2160 nscp_cmndinfo->phase = ((ha->scan_mode & 0x10 ? 1:0) << 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 | SCSIRAWSERVICE;
2162 ha->scan_mode = 0x12;
2163 TRACE2(("Scan mode: 0x%x (SCAN_START)\n",
2164 ha->scan_mode));
2165 } else {
2166 ha->scan_mode &= 0x10;
2167 TRACE2(("Scan mode: 0x%x\n", ha->scan_mode));
2168 }
2169 } else if (ha->scan_mode == 0x12) {
2170 if (b == ha->bus_cnt && t == ha->tid_cnt-1) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002171 nscp_cmndinfo->phase = SCSIRAWSERVICE;
2172 nscp_cmndinfo->OpCode = GDT_SCAN_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 ha->scan_mode &= 0x10;
2174 TRACE2(("Scan mode: 0x%x (SCAN_END)\n",
2175 ha->scan_mode));
2176 }
2177 }
2178 }
2179 if (b == ha->virt_bus && nscp->cmnd[0] != INQUIRY &&
2180 nscp->cmnd[0] != READ_CAPACITY && nscp->cmnd[0] != MODE_SENSE &&
2181 (ha->hdr[t].cluster_type & CLUSTER_DRIVE)) {
2182 /* always GDT_CLUST_INFO! */
Boaz Harroshf842b642007-10-02 23:16:01 +02002183 nscp_cmndinfo->OpCode = GDT_CLUST_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 }
2185 }
2186 }
2187
Boaz Harroshf842b642007-10-02 23:16:01 +02002188 if (nscp_cmndinfo->OpCode != -1) {
2189 if ((nscp_cmndinfo->phase & 0xff) == CACHESERVICE) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002190 if (!(cmd_index=gdth_fill_cache_cmd(ha, nscp, t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 this_cmd = FALSE;
2192 next_cmd = FALSE;
Boaz Harroshf842b642007-10-02 23:16:01 +02002193 } else if ((nscp_cmndinfo->phase & 0xff) == SCSIRAWSERVICE) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002194 if (!(cmd_index=gdth_fill_raw_cmd(ha, nscp, BUS_L2P(ha, b))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 this_cmd = FALSE;
2196 next_cmd = FALSE;
2197 } else {
2198 memset((char*)nscp->sense_buffer,0,16);
2199 nscp->sense_buffer[0] = 0x70;
2200 nscp->sense_buffer[2] = NOT_READY;
2201 nscp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
Boaz Harroshf842b642007-10-02 23:16:01 +02002202 if (!nscp_cmndinfo->wait_for_completion)
2203 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002205 gdth_scsi_done(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 }
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02002207 } else if (gdth_cmnd_priv(nscp)->internal_command) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002208 if (!(cmd_index=gdth_special_cmd(ha, nscp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 this_cmd = FALSE;
2210 next_cmd = FALSE;
2211 } else if (b != ha->virt_bus) {
2212 if (ha->raw[BUS_L2P(ha,b)].io_cnt[t] >= GDTH_MAX_RAW ||
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002213 !(cmd_index=gdth_fill_raw_cmd(ha, nscp, BUS_L2P(ha, b))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 this_cmd = FALSE;
2215 else
2216 ha->raw[BUS_L2P(ha,b)].io_cnt[t]++;
2217 } else if (t >= MAX_HDRIVES || !ha->hdr[t].present || l != 0) {
2218 TRACE2(("Command 0x%x to bus %d id %d lun %d -> IGNORE\n",
2219 nscp->cmnd[0], b, t, l));
2220 nscp->result = DID_BAD_TARGET << 16;
Boaz Harroshf842b642007-10-02 23:16:01 +02002221 if (!nscp_cmndinfo->wait_for_completion)
2222 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002224 gdth_scsi_done(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 } else {
2226 switch (nscp->cmnd[0]) {
2227 case TEST_UNIT_READY:
2228 case INQUIRY:
2229 case REQUEST_SENSE:
2230 case READ_CAPACITY:
2231 case VERIFY:
2232 case START_STOP:
2233 case MODE_SENSE:
2234 case SERVICE_ACTION_IN:
2235 TRACE(("cache cmd %x/%x/%x/%x/%x/%x\n",nscp->cmnd[0],
2236 nscp->cmnd[1],nscp->cmnd[2],nscp->cmnd[3],
2237 nscp->cmnd[4],nscp->cmnd[5]));
2238 if (ha->hdr[t].media_changed && nscp->cmnd[0] != INQUIRY) {
2239 /* return UNIT_ATTENTION */
2240 TRACE2(("cmd 0x%x target %d: UNIT_ATTENTION\n",
2241 nscp->cmnd[0], t));
2242 ha->hdr[t].media_changed = FALSE;
2243 memset((char*)nscp->sense_buffer,0,16);
2244 nscp->sense_buffer[0] = 0x70;
2245 nscp->sense_buffer[2] = UNIT_ATTENTION;
2246 nscp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
Boaz Harroshf842b642007-10-02 23:16:01 +02002247 if (!nscp_cmndinfo->wait_for_completion)
2248 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002250 gdth_scsi_done(nscp);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002251 } else if (gdth_internal_cache_cmd(ha, nscp))
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002252 gdth_scsi_done(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 break;
2254
2255 case ALLOW_MEDIUM_REMOVAL:
2256 TRACE(("cache cmd %x/%x/%x/%x/%x/%x\n",nscp->cmnd[0],
2257 nscp->cmnd[1],nscp->cmnd[2],nscp->cmnd[3],
2258 nscp->cmnd[4],nscp->cmnd[5]));
2259 if ( (nscp->cmnd[4]&1) && !(ha->hdr[t].devtype&1) ) {
2260 TRACE(("Prevent r. nonremov. drive->do nothing\n"));
2261 nscp->result = DID_OK << 16;
2262 nscp->sense_buffer[0] = 0;
Boaz Harroshf842b642007-10-02 23:16:01 +02002263 if (!nscp_cmndinfo->wait_for_completion)
2264 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002266 gdth_scsi_done(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 } else {
2268 nscp->cmnd[3] = (ha->hdr[t].devtype&1) ? 1:0;
2269 TRACE(("Prevent/allow r. %d rem. drive %d\n",
2270 nscp->cmnd[4],nscp->cmnd[3]));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002271 if (!(cmd_index=gdth_fill_cache_cmd(ha, nscp, t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 this_cmd = FALSE;
2273 }
2274 break;
2275
2276 case RESERVE:
2277 case RELEASE:
2278 TRACE2(("cache cmd %s\n",nscp->cmnd[0] == RESERVE ?
2279 "RESERVE" : "RELEASE"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002280 if (!(cmd_index=gdth_fill_cache_cmd(ha, nscp, t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 this_cmd = FALSE;
2282 break;
2283
2284 case READ_6:
2285 case WRITE_6:
2286 case READ_10:
2287 case WRITE_10:
2288 case READ_16:
2289 case WRITE_16:
2290 if (ha->hdr[t].media_changed) {
2291 /* return UNIT_ATTENTION */
2292 TRACE2(("cmd 0x%x target %d: UNIT_ATTENTION\n",
2293 nscp->cmnd[0], t));
2294 ha->hdr[t].media_changed = FALSE;
2295 memset((char*)nscp->sense_buffer,0,16);
2296 nscp->sense_buffer[0] = 0x70;
2297 nscp->sense_buffer[2] = UNIT_ATTENTION;
2298 nscp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
Boaz Harroshf842b642007-10-02 23:16:01 +02002299 if (!nscp_cmndinfo->wait_for_completion)
2300 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002302 gdth_scsi_done(nscp);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002303 } else if (!(cmd_index=gdth_fill_cache_cmd(ha, nscp, t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 this_cmd = FALSE;
2305 break;
2306
2307 default:
2308 TRACE2(("cache cmd %x/%x/%x/%x/%x/%x unknown\n",nscp->cmnd[0],
2309 nscp->cmnd[1],nscp->cmnd[2],nscp->cmnd[3],
2310 nscp->cmnd[4],nscp->cmnd[5]));
2311 printk("GDT-HA %d: Unknown SCSI command 0x%x to cache service !\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002312 ha->hanum, nscp->cmnd[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 nscp->result = DID_ABORT << 16;
Boaz Harroshf842b642007-10-02 23:16:01 +02002314 if (!nscp_cmndinfo->wait_for_completion)
2315 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002317 gdth_scsi_done(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 break;
2319 }
2320 }
2321
2322 if (!this_cmd)
2323 break;
2324 if (nscp == ha->req_first)
2325 ha->req_first = pscp = (Scsi_Cmnd *)nscp->SCp.ptr;
2326 else
2327 pscp->SCp.ptr = nscp->SCp.ptr;
2328 if (!next_cmd)
2329 break;
2330 }
2331
2332 if (ha->cmd_cnt > 0) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002333 gdth_release_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 }
2335
2336 if (!gdth_polling)
2337 spin_unlock_irqrestore(&ha->smp_lock, flags);
2338
2339 if (gdth_polling && ha->cmd_cnt > 0) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002340 if (!gdth_wait(ha, cmd_index, POLL_TIMEOUT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 printk("GDT-HA %d: Command %d timed out !\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002342 ha->hanum, cmd_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 }
2344}
Boaz Harrosh3892d882007-10-02 23:18:03 +02002345
2346/*
2347 * gdth_copy_internal_data() - copy to/from a buffer onto a scsi_cmnd's
2348 * buffers, kmap_atomic() as needed.
2349 */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002350static void gdth_copy_internal_data(gdth_ha_str *ha, Scsi_Cmnd *scp,
Boaz Harrosh3892d882007-10-02 23:18:03 +02002351 char *buffer, ushort count, int to_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352{
Boaz Harrosh3892d882007-10-02 23:18:03 +02002353 ushort cpcount,i, max_sg = gdth_sg_count(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 ushort cpsum,cpnow;
2355 struct scatterlist *sl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 char *address;
2357
Boaz Harrosh3892d882007-10-02 23:18:03 +02002358 cpcount = min_t(ushort, count, gdth_bufflen(scp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Boaz Harrosh3892d882007-10-02 23:18:03 +02002360 if (cpcount) {
2361 cpsum=0;
2362 scsi_for_each_sg(scp, sl, max_sg, i) {
Leubner, Achimcbd5f692006-06-09 11:34:29 -07002363 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 cpnow = (ushort)sl->length;
2365 TRACE(("copy_internal() now %d sum %d count %d %d\n",
Boaz Harrosh3892d882007-10-02 23:18:03 +02002366 cpnow, cpsum, cpcount, gdth_bufflen(scp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 if (cpsum+cpnow > cpcount)
2368 cpnow = cpcount - cpsum;
2369 cpsum += cpnow;
Jens Axboe45711f12007-10-22 21:19:53 +02002370 if (!sg_page(sl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 printk("GDT-HA %d: invalid sc/gt element in gdth_copy_internal_data()\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002372 ha->hanum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 return;
2374 }
Leubner, Achimcbd5f692006-06-09 11:34:29 -07002375 local_irq_save(flags);
Jens Axboe45711f12007-10-22 21:19:53 +02002376 address = kmap_atomic(sg_page(sl), KM_BIO_SRC_IRQ) + sl->offset;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002377 if (to_buffer)
2378 memcpy(buffer, address, cpnow);
2379 else
2380 memcpy(address, buffer, cpnow);
Jens Axboe45711f12007-10-22 21:19:53 +02002381 flush_dcache_page(sg_page(sl));
Leubner, Achimcbd5f692006-06-09 11:34:29 -07002382 kunmap_atomic(address, KM_BIO_SRC_IRQ);
Leubner, Achimcbd5f692006-06-09 11:34:29 -07002383 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 if (cpsum == cpcount)
2385 break;
2386 buffer += cpnow;
2387 }
Boaz Harrosh3892d882007-10-02 23:18:03 +02002388 } else if (count) {
2389 printk("GDT-HA %d: SCSI command with no buffers but data transfer expected!\n",
2390 ha->hanum);
2391 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
2393}
2394
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002395static int gdth_internal_cache_cmd(gdth_ha_str *ha, Scsi_Cmnd *scp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 unchar t;
2398 gdth_inq_data inq;
2399 gdth_rdcap_data rdc;
2400 gdth_sense_data sd;
2401 gdth_modep_data mpd;
Boaz Harroshf842b642007-10-02 23:16:01 +02002402 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 t = scp->device->id;
2405 TRACE(("gdth_internal_cache_cmd() cmd 0x%x hdrive %d\n",
2406 scp->cmnd[0],t));
2407
2408 scp->result = DID_OK << 16;
2409 scp->sense_buffer[0] = 0;
2410
2411 switch (scp->cmnd[0]) {
2412 case TEST_UNIT_READY:
2413 case VERIFY:
2414 case START_STOP:
2415 TRACE2(("Test/Verify/Start hdrive %d\n",t));
2416 break;
2417
2418 case INQUIRY:
2419 TRACE2(("Inquiry hdrive %d devtype %d\n",
2420 t,ha->hdr[t].devtype));
2421 inq.type_qual = (ha->hdr[t].devtype&4) ? TYPE_ROM:TYPE_DISK;
2422 /* you can here set all disks to removable, if you want to do
2423 a flush using the ALLOW_MEDIUM_REMOVAL command */
2424 inq.modif_rmb = 0x00;
2425 if ((ha->hdr[t].devtype & 1) ||
2426 (ha->hdr[t].cluster_type & CLUSTER_DRIVE))
2427 inq.modif_rmb = 0x80;
2428 inq.version = 2;
2429 inq.resp_aenc = 2;
2430 inq.add_length= 32;
2431 strcpy(inq.vendor,ha->oem_name);
2432 sprintf(inq.product,"Host Drive #%02d",t);
2433 strcpy(inq.revision," ");
Boaz Harrosh3892d882007-10-02 23:18:03 +02002434 gdth_copy_internal_data(ha, scp, (char*)&inq, sizeof(gdth_inq_data), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 break;
2436
2437 case REQUEST_SENSE:
2438 TRACE2(("Request sense hdrive %d\n",t));
2439 sd.errorcode = 0x70;
2440 sd.segno = 0x00;
2441 sd.key = NO_SENSE;
2442 sd.info = 0;
2443 sd.add_length= 0;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002444 gdth_copy_internal_data(ha, scp, (char*)&sd, sizeof(gdth_sense_data), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 break;
2446
2447 case MODE_SENSE:
2448 TRACE2(("Mode sense hdrive %d\n",t));
2449 memset((char*)&mpd,0,sizeof(gdth_modep_data));
2450 mpd.hd.data_length = sizeof(gdth_modep_data);
2451 mpd.hd.dev_par = (ha->hdr[t].devtype&2) ? 0x80:0;
2452 mpd.hd.bd_length = sizeof(mpd.bd);
2453 mpd.bd.block_length[0] = (SECTOR_SIZE & 0x00ff0000) >> 16;
2454 mpd.bd.block_length[1] = (SECTOR_SIZE & 0x0000ff00) >> 8;
2455 mpd.bd.block_length[2] = (SECTOR_SIZE & 0x000000ff);
Boaz Harrosh3892d882007-10-02 23:18:03 +02002456 gdth_copy_internal_data(ha, scp, (char*)&mpd, sizeof(gdth_modep_data), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 break;
2458
2459 case READ_CAPACITY:
2460 TRACE2(("Read capacity hdrive %d\n",t));
2461 if (ha->hdr[t].size > (ulong64)0xffffffff)
2462 rdc.last_block_no = 0xffffffff;
2463 else
2464 rdc.last_block_no = cpu_to_be32(ha->hdr[t].size-1);
2465 rdc.block_length = cpu_to_be32(SECTOR_SIZE);
Boaz Harrosh3892d882007-10-02 23:18:03 +02002466 gdth_copy_internal_data(ha, scp, (char*)&rdc, sizeof(gdth_rdcap_data), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 break;
2468
2469 case SERVICE_ACTION_IN:
2470 if ((scp->cmnd[1] & 0x1f) == SAI_READ_CAPACITY_16 &&
2471 (ha->cache_feat & GDT_64BIT)) {
2472 gdth_rdcap16_data rdc16;
2473
2474 TRACE2(("Read capacity (16) hdrive %d\n",t));
2475 rdc16.last_block_no = cpu_to_be64(ha->hdr[t].size-1);
2476 rdc16.block_length = cpu_to_be32(SECTOR_SIZE);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002477 gdth_copy_internal_data(ha, scp, (char*)&rdc16,
Boaz Harrosh3892d882007-10-02 23:18:03 +02002478 sizeof(gdth_rdcap16_data), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 } else {
2480 scp->result = DID_ABORT << 16;
2481 }
2482 break;
2483
2484 default:
2485 TRACE2(("Internal cache cmd 0x%x unknown\n",scp->cmnd[0]));
2486 break;
2487 }
2488
Boaz Harroshf842b642007-10-02 23:16:01 +02002489 if (!cmndinfo->wait_for_completion)
2490 cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 else
2492 return 1;
2493
2494 return 0;
2495}
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002496
2497static int gdth_fill_cache_cmd(gdth_ha_str *ha, Scsi_Cmnd *scp, ushort hdrive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 register gdth_cmd_str *cmdp;
Boaz Harroshf842b642007-10-02 23:16:01 +02002500 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 ulong32 cnt, blockcnt;
2502 ulong64 no, blockno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 int i, cmd_index, read_write, sgcnt, mode64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 cmdp = ha->pccb;
2506 TRACE(("gdth_fill_cache_cmd() cmd 0x%x cmdsize %d hdrive %d\n",
2507 scp->cmnd[0],scp->cmd_len,hdrive));
2508
2509 if (ha->type==GDT_EISA && ha->cmd_cnt>0)
2510 return 0;
2511
2512 mode64 = (ha->cache_feat & GDT_64BIT) ? TRUE : FALSE;
2513 /* test for READ_16, WRITE_16 if !mode64 ? ---
2514 not required, should not occur due to error return on
2515 READ_CAPACITY_16 */
2516
2517 cmdp->Service = CACHESERVICE;
2518 cmdp->RequestBuffer = scp;
2519 /* search free command index */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002520 if (!(cmd_index=gdth_get_cmd_index(ha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 TRACE(("GDT: No free command index found\n"));
2522 return 0;
2523 }
2524 /* if it's the first command, set command semaphore */
2525 if (ha->cmd_cnt == 0)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002526 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
2528 /* fill command */
2529 read_write = 0;
Boaz Harroshf842b642007-10-02 23:16:01 +02002530 if (cmndinfo->OpCode != -1)
2531 cmdp->OpCode = cmndinfo->OpCode; /* special cache cmd. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 else if (scp->cmnd[0] == RESERVE)
2533 cmdp->OpCode = GDT_RESERVE_DRV;
2534 else if (scp->cmnd[0] == RELEASE)
2535 cmdp->OpCode = GDT_RELEASE_DRV;
2536 else if (scp->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
2537 if (scp->cmnd[4] & 1) /* prevent ? */
2538 cmdp->OpCode = GDT_MOUNT;
2539 else if (scp->cmnd[3] & 1) /* removable drive ? */
2540 cmdp->OpCode = GDT_UNMOUNT;
2541 else
2542 cmdp->OpCode = GDT_FLUSH;
2543 } else if (scp->cmnd[0] == WRITE_6 || scp->cmnd[0] == WRITE_10 ||
2544 scp->cmnd[0] == WRITE_12 || scp->cmnd[0] == WRITE_16
2545 ) {
2546 read_write = 1;
2547 if (gdth_write_through || ((ha->hdr[hdrive].rw_attribs & 1) &&
2548 (ha->cache_feat & GDT_WR_THROUGH)))
2549 cmdp->OpCode = GDT_WRITE_THR;
2550 else
2551 cmdp->OpCode = GDT_WRITE;
2552 } else {
2553 read_write = 2;
2554 cmdp->OpCode = GDT_READ;
2555 }
2556
2557 cmdp->BoardNode = LOCALBOARD;
2558 if (mode64) {
2559 cmdp->u.cache64.DeviceNo = hdrive;
2560 cmdp->u.cache64.BlockNo = 1;
2561 cmdp->u.cache64.sg_canz = 0;
2562 } else {
2563 cmdp->u.cache.DeviceNo = hdrive;
2564 cmdp->u.cache.BlockNo = 1;
2565 cmdp->u.cache.sg_canz = 0;
2566 }
2567
2568 if (read_write) {
2569 if (scp->cmd_len == 16) {
2570 memcpy(&no, &scp->cmnd[2], sizeof(ulong64));
2571 blockno = be64_to_cpu(no);
2572 memcpy(&cnt, &scp->cmnd[10], sizeof(ulong32));
2573 blockcnt = be32_to_cpu(cnt);
2574 } else if (scp->cmd_len == 10) {
2575 memcpy(&no, &scp->cmnd[2], sizeof(ulong32));
2576 blockno = be32_to_cpu(no);
2577 memcpy(&cnt, &scp->cmnd[7], sizeof(ushort));
2578 blockcnt = be16_to_cpu(cnt);
2579 } else {
2580 memcpy(&no, &scp->cmnd[0], sizeof(ulong32));
2581 blockno = be32_to_cpu(no) & 0x001fffffUL;
2582 blockcnt= scp->cmnd[4]==0 ? 0x100 : scp->cmnd[4];
2583 }
2584 if (mode64) {
2585 cmdp->u.cache64.BlockNo = blockno;
2586 cmdp->u.cache64.BlockCnt = blockcnt;
2587 } else {
2588 cmdp->u.cache.BlockNo = (ulong32)blockno;
2589 cmdp->u.cache.BlockCnt = blockcnt;
2590 }
2591
Boaz Harrosh3892d882007-10-02 23:18:03 +02002592 if (gdth_bufflen(scp)) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002593 cmndinfo->dma_dir = (read_write == 1 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
Boaz Harrosh3892d882007-10-02 23:18:03 +02002595 sgcnt = pci_map_sg(ha->pdev, gdth_sglist(scp), gdth_sg_count(scp),
2596 cmndinfo->dma_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 if (mode64) {
Boaz Harrosh3892d882007-10-02 23:18:03 +02002598 struct scatterlist *sl;
2599
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 cmdp->u.cache64.DestAddr= (ulong64)-1;
2601 cmdp->u.cache64.sg_canz = sgcnt;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002602 scsi_for_each_sg(scp, sl, sgcnt, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 cmdp->u.cache64.sg_lst[i].sg_ptr = sg_dma_address(sl);
2604#ifdef GDTH_DMA_STATISTICS
2605 if (cmdp->u.cache64.sg_lst[i].sg_ptr > (ulong64)0xffffffff)
2606 ha->dma64_cnt++;
2607 else
2608 ha->dma32_cnt++;
2609#endif
2610 cmdp->u.cache64.sg_lst[i].sg_len = sg_dma_len(sl);
2611 }
2612 } else {
Boaz Harrosh3892d882007-10-02 23:18:03 +02002613 struct scatterlist *sl;
2614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 cmdp->u.cache.DestAddr= 0xffffffff;
2616 cmdp->u.cache.sg_canz = sgcnt;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002617 scsi_for_each_sg(scp, sl, sgcnt, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 cmdp->u.cache.sg_lst[i].sg_ptr = sg_dma_address(sl);
2619#ifdef GDTH_DMA_STATISTICS
2620 ha->dma32_cnt++;
2621#endif
2622 cmdp->u.cache.sg_lst[i].sg_len = sg_dma_len(sl);
2623 }
2624 }
2625
2626#ifdef GDTH_STATISTICS
2627 if (max_sg < (ulong32)sgcnt) {
2628 max_sg = (ulong32)sgcnt;
2629 TRACE3(("GDT: max_sg = %d\n",max_sg));
2630 }
2631#endif
2632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 }
2634 }
2635 /* evaluate command size, check space */
2636 if (mode64) {
2637 TRACE(("cache cmd: addr. %x sganz %x sgptr0 %x sglen0 %x\n",
2638 cmdp->u.cache64.DestAddr,cmdp->u.cache64.sg_canz,
2639 cmdp->u.cache64.sg_lst[0].sg_ptr,
2640 cmdp->u.cache64.sg_lst[0].sg_len));
2641 TRACE(("cache cmd: cmd %d blockno. %d, blockcnt %d\n",
2642 cmdp->OpCode,cmdp->u.cache64.BlockNo,cmdp->u.cache64.BlockCnt));
2643 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.cache64.sg_lst) +
2644 (ushort)cmdp->u.cache64.sg_canz * sizeof(gdth_sg64_str);
2645 } else {
2646 TRACE(("cache cmd: addr. %x sganz %x sgptr0 %x sglen0 %x\n",
2647 cmdp->u.cache.DestAddr,cmdp->u.cache.sg_canz,
2648 cmdp->u.cache.sg_lst[0].sg_ptr,
2649 cmdp->u.cache.sg_lst[0].sg_len));
2650 TRACE(("cache cmd: cmd %d blockno. %d, blockcnt %d\n",
2651 cmdp->OpCode,cmdp->u.cache.BlockNo,cmdp->u.cache.BlockCnt));
2652 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.cache.sg_lst) +
2653 (ushort)cmdp->u.cache.sg_canz * sizeof(gdth_sg_str);
2654 }
2655 if (ha->cmd_len & 3)
2656 ha->cmd_len += (4 - (ha->cmd_len & 3));
2657
2658 if (ha->cmd_cnt > 0) {
2659 if ((ha->cmd_offs_dpmem + ha->cmd_len + DPMEM_COMMAND_OFFSET) >
2660 ha->ic_all_size) {
2661 TRACE2(("gdth_fill_cache() DPMEM overflow\n"));
2662 ha->cmd_tab[cmd_index-2].cmnd = UNUSED_CMND;
2663 return 0;
2664 }
2665 }
2666
2667 /* copy command */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002668 gdth_copy_command(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 return cmd_index;
2670}
2671
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002672static int gdth_fill_raw_cmd(gdth_ha_str *ha, Scsi_Cmnd *scp, unchar b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 register gdth_cmd_str *cmdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 ushort i;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002676 dma_addr_t sense_paddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 int cmd_index, sgcnt, mode64;
2678 unchar t,l;
2679 struct page *page;
2680 ulong offset;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02002681 struct gdth_cmndinfo *cmndinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 t = scp->device->id;
2684 l = scp->device->lun;
2685 cmdp = ha->pccb;
2686 TRACE(("gdth_fill_raw_cmd() cmd 0x%x bus %d ID %d LUN %d\n",
2687 scp->cmnd[0],b,t,l));
2688
2689 if (ha->type==GDT_EISA && ha->cmd_cnt>0)
2690 return 0;
2691
2692 mode64 = (ha->raw_feat & GDT_64BIT) ? TRUE : FALSE;
2693
2694 cmdp->Service = SCSIRAWSERVICE;
2695 cmdp->RequestBuffer = scp;
2696 /* search free command index */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002697 if (!(cmd_index=gdth_get_cmd_index(ha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 TRACE(("GDT: No free command index found\n"));
2699 return 0;
2700 }
2701 /* if it's the first command, set command semaphore */
2702 if (ha->cmd_cnt == 0)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002703 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02002705 cmndinfo = gdth_cmnd_priv(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 /* fill command */
Boaz Harroshf842b642007-10-02 23:16:01 +02002707 if (cmndinfo->OpCode != -1) {
2708 cmdp->OpCode = cmndinfo->OpCode; /* special raw cmd. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 cmdp->BoardNode = LOCALBOARD;
2710 if (mode64) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002711 cmdp->u.raw64.direction = (cmndinfo->phase >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 TRACE2(("special raw cmd 0x%x param 0x%x\n",
2713 cmdp->OpCode, cmdp->u.raw64.direction));
2714 /* evaluate command size */
2715 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw64.sg_lst);
2716 } else {
Boaz Harroshf842b642007-10-02 23:16:01 +02002717 cmdp->u.raw.direction = (cmndinfo->phase >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 TRACE2(("special raw cmd 0x%x param 0x%x\n",
2719 cmdp->OpCode, cmdp->u.raw.direction));
2720 /* evaluate command size */
2721 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw.sg_lst);
2722 }
2723
2724 } else {
2725 page = virt_to_page(scp->sense_buffer);
2726 offset = (ulong)scp->sense_buffer & ~PAGE_MASK;
2727 sense_paddr = pci_map_page(ha->pdev,page,offset,
2728 16,PCI_DMA_FROMDEVICE);
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02002729
2730 cmndinfo->sense_paddr = sense_paddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 cmdp->OpCode = GDT_WRITE; /* always */
2732 cmdp->BoardNode = LOCALBOARD;
2733 if (mode64) {
2734 cmdp->u.raw64.reserved = 0;
2735 cmdp->u.raw64.mdisc_time = 0;
2736 cmdp->u.raw64.mcon_time = 0;
2737 cmdp->u.raw64.clen = scp->cmd_len;
2738 cmdp->u.raw64.target = t;
2739 cmdp->u.raw64.lun = l;
2740 cmdp->u.raw64.bus = b;
2741 cmdp->u.raw64.priority = 0;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002742 cmdp->u.raw64.sdlen = gdth_bufflen(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 cmdp->u.raw64.sense_len = 16;
2744 cmdp->u.raw64.sense_data = sense_paddr;
2745 cmdp->u.raw64.direction =
2746 gdth_direction_tab[scp->cmnd[0]]==DOU ? GDTH_DATA_OUT:GDTH_DATA_IN;
2747 memcpy(cmdp->u.raw64.cmd,scp->cmnd,16);
Joerg Dorchainbb9ba312007-03-06 02:46:54 -08002748 cmdp->u.raw64.sg_ranz = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 } else {
2750 cmdp->u.raw.reserved = 0;
2751 cmdp->u.raw.mdisc_time = 0;
2752 cmdp->u.raw.mcon_time = 0;
2753 cmdp->u.raw.clen = scp->cmd_len;
2754 cmdp->u.raw.target = t;
2755 cmdp->u.raw.lun = l;
2756 cmdp->u.raw.bus = b;
2757 cmdp->u.raw.priority = 0;
2758 cmdp->u.raw.link_p = 0;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002759 cmdp->u.raw.sdlen = gdth_bufflen(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 cmdp->u.raw.sense_len = 16;
2761 cmdp->u.raw.sense_data = sense_paddr;
2762 cmdp->u.raw.direction =
2763 gdth_direction_tab[scp->cmnd[0]]==DOU ? GDTH_DATA_OUT:GDTH_DATA_IN;
2764 memcpy(cmdp->u.raw.cmd,scp->cmnd,12);
Joerg Dorchainbb9ba312007-03-06 02:46:54 -08002765 cmdp->u.raw.sg_ranz = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 }
2767
Boaz Harrosh3892d882007-10-02 23:18:03 +02002768 if (gdth_bufflen(scp)) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002769 cmndinfo->dma_dir = PCI_DMA_BIDIRECTIONAL;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002770 sgcnt = pci_map_sg(ha->pdev, gdth_sglist(scp), gdth_sg_count(scp),
2771 cmndinfo->dma_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 if (mode64) {
Boaz Harrosh3892d882007-10-02 23:18:03 +02002773 struct scatterlist *sl;
2774
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 cmdp->u.raw64.sdata = (ulong64)-1;
2776 cmdp->u.raw64.sg_ranz = sgcnt;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002777 scsi_for_each_sg(scp, sl, sgcnt, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 cmdp->u.raw64.sg_lst[i].sg_ptr = sg_dma_address(sl);
2779#ifdef GDTH_DMA_STATISTICS
2780 if (cmdp->u.raw64.sg_lst[i].sg_ptr > (ulong64)0xffffffff)
2781 ha->dma64_cnt++;
2782 else
2783 ha->dma32_cnt++;
2784#endif
2785 cmdp->u.raw64.sg_lst[i].sg_len = sg_dma_len(sl);
2786 }
2787 } else {
Boaz Harrosh3892d882007-10-02 23:18:03 +02002788 struct scatterlist *sl;
2789
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 cmdp->u.raw.sdata = 0xffffffff;
2791 cmdp->u.raw.sg_ranz = sgcnt;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002792 scsi_for_each_sg(scp, sl, sgcnt, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 cmdp->u.raw.sg_lst[i].sg_ptr = sg_dma_address(sl);
2794#ifdef GDTH_DMA_STATISTICS
2795 ha->dma32_cnt++;
2796#endif
2797 cmdp->u.raw.sg_lst[i].sg_len = sg_dma_len(sl);
2798 }
2799 }
2800
2801#ifdef GDTH_STATISTICS
2802 if (max_sg < sgcnt) {
2803 max_sg = sgcnt;
2804 TRACE3(("GDT: max_sg = %d\n",sgcnt));
2805 }
2806#endif
2807
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 }
2809 if (mode64) {
2810 TRACE(("raw cmd: addr. %x sganz %x sgptr0 %x sglen0 %x\n",
2811 cmdp->u.raw64.sdata,cmdp->u.raw64.sg_ranz,
2812 cmdp->u.raw64.sg_lst[0].sg_ptr,
2813 cmdp->u.raw64.sg_lst[0].sg_len));
2814 /* evaluate command size */
2815 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw64.sg_lst) +
2816 (ushort)cmdp->u.raw64.sg_ranz * sizeof(gdth_sg64_str);
2817 } else {
2818 TRACE(("raw cmd: addr. %x sganz %x sgptr0 %x sglen0 %x\n",
2819 cmdp->u.raw.sdata,cmdp->u.raw.sg_ranz,
2820 cmdp->u.raw.sg_lst[0].sg_ptr,
2821 cmdp->u.raw.sg_lst[0].sg_len));
2822 /* evaluate command size */
2823 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw.sg_lst) +
2824 (ushort)cmdp->u.raw.sg_ranz * sizeof(gdth_sg_str);
2825 }
2826 }
2827 /* check space */
2828 if (ha->cmd_len & 3)
2829 ha->cmd_len += (4 - (ha->cmd_len & 3));
2830
2831 if (ha->cmd_cnt > 0) {
2832 if ((ha->cmd_offs_dpmem + ha->cmd_len + DPMEM_COMMAND_OFFSET) >
2833 ha->ic_all_size) {
2834 TRACE2(("gdth_fill_raw() DPMEM overflow\n"));
2835 ha->cmd_tab[cmd_index-2].cmnd = UNUSED_CMND;
2836 return 0;
2837 }
2838 }
2839
2840 /* copy command */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002841 gdth_copy_command(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 return cmd_index;
2843}
2844
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002845static int gdth_special_cmd(gdth_ha_str *ha, Scsi_Cmnd *scp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 register gdth_cmd_str *cmdp;
2848 int cmd_index;
2849
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 cmdp= ha->pccb;
2851 TRACE2(("gdth_special_cmd(): "));
2852
2853 if (ha->type==GDT_EISA && ha->cmd_cnt>0)
2854 return 0;
2855
Boaz Harrosh3892d882007-10-02 23:18:03 +02002856 gdth_copy_internal_data(ha, scp, (char *)cmdp, sizeof(gdth_cmd_str), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 cmdp->RequestBuffer = scp;
2858
2859 /* search free command index */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002860 if (!(cmd_index=gdth_get_cmd_index(ha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 TRACE(("GDT: No free command index found\n"));
2862 return 0;
2863 }
2864
2865 /* if it's the first command, set command semaphore */
2866 if (ha->cmd_cnt == 0)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002867 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
2869 /* evaluate command size, check space */
2870 if (cmdp->OpCode == GDT_IOCTL) {
2871 TRACE2(("IOCTL\n"));
2872 ha->cmd_len =
2873 GDTOFFSOF(gdth_cmd_str,u.ioctl.p_param) + sizeof(ulong64);
2874 } else if (cmdp->Service == CACHESERVICE) {
2875 TRACE2(("cache command %d\n",cmdp->OpCode));
2876 if (ha->cache_feat & GDT_64BIT)
2877 ha->cmd_len =
2878 GDTOFFSOF(gdth_cmd_str,u.cache64.sg_lst) + sizeof(gdth_sg64_str);
2879 else
2880 ha->cmd_len =
2881 GDTOFFSOF(gdth_cmd_str,u.cache.sg_lst) + sizeof(gdth_sg_str);
2882 } else if (cmdp->Service == SCSIRAWSERVICE) {
2883 TRACE2(("raw command %d\n",cmdp->OpCode));
2884 if (ha->raw_feat & GDT_64BIT)
2885 ha->cmd_len =
2886 GDTOFFSOF(gdth_cmd_str,u.raw64.sg_lst) + sizeof(gdth_sg64_str);
2887 else
2888 ha->cmd_len =
2889 GDTOFFSOF(gdth_cmd_str,u.raw.sg_lst) + sizeof(gdth_sg_str);
2890 }
2891
2892 if (ha->cmd_len & 3)
2893 ha->cmd_len += (4 - (ha->cmd_len & 3));
2894
2895 if (ha->cmd_cnt > 0) {
2896 if ((ha->cmd_offs_dpmem + ha->cmd_len + DPMEM_COMMAND_OFFSET) >
2897 ha->ic_all_size) {
2898 TRACE2(("gdth_special_cmd() DPMEM overflow\n"));
2899 ha->cmd_tab[cmd_index-2].cmnd = UNUSED_CMND;
2900 return 0;
2901 }
2902 }
2903
2904 /* copy command */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002905 gdth_copy_command(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 return cmd_index;
2907}
2908
2909
2910/* Controller event handling functions */
2911static gdth_evt_str *gdth_store_event(gdth_ha_str *ha, ushort source,
2912 ushort idx, gdth_evt_data *evt)
2913{
2914 gdth_evt_str *e;
2915 struct timeval tv;
2916
2917 /* no GDTH_LOCK_HA() ! */
2918 TRACE2(("gdth_store_event() source %d idx %d\n", source, idx));
2919 if (source == 0) /* no source -> no event */
2920 return NULL;
2921
2922 if (ebuffer[elastidx].event_source == source &&
2923 ebuffer[elastidx].event_idx == idx &&
2924 ((evt->size != 0 && ebuffer[elastidx].event_data.size != 0 &&
2925 !memcmp((char *)&ebuffer[elastidx].event_data.eu,
2926 (char *)&evt->eu, evt->size)) ||
2927 (evt->size == 0 && ebuffer[elastidx].event_data.size == 0 &&
2928 !strcmp((char *)&ebuffer[elastidx].event_data.event_string,
2929 (char *)&evt->event_string)))) {
2930 e = &ebuffer[elastidx];
2931 do_gettimeofday(&tv);
2932 e->last_stamp = tv.tv_sec;
2933 ++e->same_count;
2934 } else {
2935 if (ebuffer[elastidx].event_source != 0) { /* entry not free ? */
2936 ++elastidx;
2937 if (elastidx == MAX_EVENTS)
2938 elastidx = 0;
2939 if (elastidx == eoldidx) { /* reached mark ? */
2940 ++eoldidx;
2941 if (eoldidx == MAX_EVENTS)
2942 eoldidx = 0;
2943 }
2944 }
2945 e = &ebuffer[elastidx];
2946 e->event_source = source;
2947 e->event_idx = idx;
2948 do_gettimeofday(&tv);
2949 e->first_stamp = e->last_stamp = tv.tv_sec;
2950 e->same_count = 1;
2951 e->event_data = *evt;
2952 e->application = 0;
2953 }
2954 return e;
2955}
2956
2957static int gdth_read_event(gdth_ha_str *ha, int handle, gdth_evt_str *estr)
2958{
2959 gdth_evt_str *e;
2960 int eindex;
2961 ulong flags;
2962
2963 TRACE2(("gdth_read_event() handle %d\n", handle));
2964 spin_lock_irqsave(&ha->smp_lock, flags);
2965 if (handle == -1)
2966 eindex = eoldidx;
2967 else
2968 eindex = handle;
2969 estr->event_source = 0;
2970
2971 if (eindex >= MAX_EVENTS) {
2972 spin_unlock_irqrestore(&ha->smp_lock, flags);
2973 return eindex;
2974 }
2975 e = &ebuffer[eindex];
2976 if (e->event_source != 0) {
2977 if (eindex != elastidx) {
2978 if (++eindex == MAX_EVENTS)
2979 eindex = 0;
2980 } else {
2981 eindex = -1;
2982 }
2983 memcpy(estr, e, sizeof(gdth_evt_str));
2984 }
2985 spin_unlock_irqrestore(&ha->smp_lock, flags);
2986 return eindex;
2987}
2988
2989static void gdth_readapp_event(gdth_ha_str *ha,
2990 unchar application, gdth_evt_str *estr)
2991{
2992 gdth_evt_str *e;
2993 int eindex;
2994 ulong flags;
2995 unchar found = FALSE;
2996
2997 TRACE2(("gdth_readapp_event() app. %d\n", application));
2998 spin_lock_irqsave(&ha->smp_lock, flags);
2999 eindex = eoldidx;
3000 for (;;) {
3001 e = &ebuffer[eindex];
3002 if (e->event_source == 0)
3003 break;
3004 if ((e->application & application) == 0) {
3005 e->application |= application;
3006 found = TRUE;
3007 break;
3008 }
3009 if (eindex == elastidx)
3010 break;
3011 if (++eindex == MAX_EVENTS)
3012 eindex = 0;
3013 }
3014 if (found)
3015 memcpy(estr, e, sizeof(gdth_evt_str));
3016 else
3017 estr->event_source = 0;
3018 spin_unlock_irqrestore(&ha->smp_lock, flags);
3019}
3020
3021static void gdth_clear_events(void)
3022{
3023 TRACE(("gdth_clear_events()"));
3024
3025 eoldidx = elastidx = 0;
3026 ebuffer[0].event_source = 0;
3027}
3028
3029
3030/* SCSI interface functions */
3031
Jeff Garzik230e8862007-12-13 16:14:12 -08003032static irqreturn_t __gdth_interrupt(gdth_ha_str *ha,
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003033 int gdth_from_wait, int* pIndex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 gdt6m_dpram_str __iomem *dp6m_ptr = NULL;
3036 gdt6_dpram_str __iomem *dp6_ptr;
3037 gdt2_dpram_str __iomem *dp2_ptr;
3038 Scsi_Cmnd *scp;
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003039 int rval, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 unchar IStatus;
3041 ushort Service;
3042 ulong flags = 0;
3043#ifdef INT_COAL
3044 int coalesced = FALSE;
3045 int next = FALSE;
3046 gdth_coal_status *pcs = NULL;
3047 int act_int_coal = 0;
3048#endif
3049
Jeff Garzik230e8862007-12-13 16:14:12 -08003050 TRACE(("gdth_interrupt() IRQ %d\n", ha->irq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
3052 /* if polling and not from gdth_wait() -> return */
3053 if (gdth_polling) {
3054 if (!gdth_from_wait) {
3055 return IRQ_HANDLED;
3056 }
3057 }
3058
3059 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003060 spin_lock_irqsave(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
3062 /* search controller */
Jeff Garzik230e8862007-12-13 16:14:12 -08003063 IStatus = gdth_get_status(ha);
3064 if (IStatus == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 /* spurious interrupt */
3066 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003067 spin_unlock_irqrestore(&ha->smp_lock, flags);
3068 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
3071#ifdef GDTH_STATISTICS
3072 ++act_ints;
3073#endif
3074
3075#ifdef INT_COAL
3076 /* See if the fw is returning coalesced status */
3077 if (IStatus == COALINDEX) {
3078 /* Coalesced status. Setup the initial status
3079 buffer pointer and flags */
3080 pcs = ha->coal_stat;
3081 coalesced = TRUE;
3082 next = TRUE;
3083 }
3084
3085 do {
3086 if (coalesced) {
3087 /* For coalesced requests all status
3088 information is found in the status buffer */
3089 IStatus = (unchar)(pcs->status & 0xff);
3090 }
3091#endif
3092
3093 if (ha->type == GDT_EISA) {
3094 if (IStatus & 0x80) { /* error flag */
3095 IStatus &= ~0x80;
3096 ha->status = inw(ha->bmic + MAILBOXREG+8);
3097 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
3098 } else /* no error */
3099 ha->status = S_OK;
3100 ha->info = inl(ha->bmic + MAILBOXREG+12);
3101 ha->service = inw(ha->bmic + MAILBOXREG+10);
3102 ha->info2 = inl(ha->bmic + MAILBOXREG+4);
3103
3104 outb(0xff, ha->bmic + EDOORREG); /* acknowledge interrupt */
3105 outb(0x00, ha->bmic + SEMA1REG); /* reset status semaphore */
3106 } else if (ha->type == GDT_ISA) {
3107 dp2_ptr = ha->brd;
3108 if (IStatus & 0x80) { /* error flag */
3109 IStatus &= ~0x80;
Jeff Garzika52667f2007-10-02 22:55:53 +02003110 ha->status = readw(&dp2_ptr->u.ic.Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
3112 } else /* no error */
3113 ha->status = S_OK;
Jeff Garzika52667f2007-10-02 22:55:53 +02003114 ha->info = readl(&dp2_ptr->u.ic.Info[0]);
3115 ha->service = readw(&dp2_ptr->u.ic.Service);
3116 ha->info2 = readl(&dp2_ptr->u.ic.Info[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
Jeff Garzika52667f2007-10-02 22:55:53 +02003118 writeb(0xff, &dp2_ptr->io.irqdel); /* acknowledge interrupt */
3119 writeb(0, &dp2_ptr->u.ic.Cmd_Index);/* reset command index */
3120 writeb(0, &dp2_ptr->io.Sema1); /* reset status semaphore */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 } else if (ha->type == GDT_PCI) {
3122 dp6_ptr = ha->brd;
3123 if (IStatus & 0x80) { /* error flag */
3124 IStatus &= ~0x80;
Jeff Garzika52667f2007-10-02 22:55:53 +02003125 ha->status = readw(&dp6_ptr->u.ic.Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
3127 } else /* no error */
3128 ha->status = S_OK;
Jeff Garzika52667f2007-10-02 22:55:53 +02003129 ha->info = readl(&dp6_ptr->u.ic.Info[0]);
3130 ha->service = readw(&dp6_ptr->u.ic.Service);
3131 ha->info2 = readl(&dp6_ptr->u.ic.Info[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
Jeff Garzika52667f2007-10-02 22:55:53 +02003133 writeb(0xff, &dp6_ptr->io.irqdel); /* acknowledge interrupt */
3134 writeb(0, &dp6_ptr->u.ic.Cmd_Index);/* reset command index */
3135 writeb(0, &dp6_ptr->io.Sema1); /* reset status semaphore */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 } else if (ha->type == GDT_PCINEW) {
3137 if (IStatus & 0x80) { /* error flag */
3138 IStatus &= ~0x80;
3139 ha->status = inw(PTR2USHORT(&ha->plx->status));
3140 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
3141 } else
3142 ha->status = S_OK;
3143 ha->info = inl(PTR2USHORT(&ha->plx->info[0]));
3144 ha->service = inw(PTR2USHORT(&ha->plx->service));
3145 ha->info2 = inl(PTR2USHORT(&ha->plx->info[1]));
3146
3147 outb(0xff, PTR2USHORT(&ha->plx->edoor_reg));
3148 outb(0x00, PTR2USHORT(&ha->plx->sema1_reg));
3149 } else if (ha->type == GDT_PCIMPR) {
3150 dp6m_ptr = ha->brd;
3151 if (IStatus & 0x80) { /* error flag */
3152 IStatus &= ~0x80;
3153#ifdef INT_COAL
3154 if (coalesced)
Jean Delvare107e7162006-11-09 21:45:09 +01003155 ha->status = pcs->ext_status & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 else
3157#endif
Jeff Garzika52667f2007-10-02 22:55:53 +02003158 ha->status = readw(&dp6m_ptr->i960r.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
3160 } else /* no error */
3161 ha->status = S_OK;
3162#ifdef INT_COAL
3163 /* get information */
3164 if (coalesced) {
3165 ha->info = pcs->info0;
3166 ha->info2 = pcs->info1;
Jean Delvare107e7162006-11-09 21:45:09 +01003167 ha->service = (pcs->ext_status >> 16) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 } else
3169#endif
3170 {
Jeff Garzika52667f2007-10-02 22:55:53 +02003171 ha->info = readl(&dp6m_ptr->i960r.info[0]);
3172 ha->service = readw(&dp6m_ptr->i960r.service);
3173 ha->info2 = readl(&dp6m_ptr->i960r.info[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 }
3175 /* event string */
3176 if (IStatus == ASYNCINDEX) {
3177 if (ha->service != SCREENSERVICE &&
3178 (ha->fw_vers & 0xff) >= 0x1a) {
Jeff Garzika52667f2007-10-02 22:55:53 +02003179 ha->dvr.severity = readb
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 (&((gdt6m_dpram_str __iomem *)ha->brd)->i960r.severity);
3181 for (i = 0; i < 256; ++i) {
Jeff Garzika52667f2007-10-02 22:55:53 +02003182 ha->dvr.event_string[i] = readb
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 (&((gdt6m_dpram_str __iomem *)ha->brd)->i960r.evt_str[i]);
3184 if (ha->dvr.event_string[i] == 0)
3185 break;
3186 }
3187 }
3188 }
3189#ifdef INT_COAL
3190 /* Make sure that non coalesced interrupts get cleared
3191 before being handled by gdth_async_event/gdth_sync_event */
3192 if (!coalesced)
3193#endif
3194 {
Jeff Garzika52667f2007-10-02 22:55:53 +02003195 writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
3196 writeb(0, &dp6m_ptr->i960r.sema1_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 }
3198 } else {
3199 TRACE2(("gdth_interrupt() unknown controller type\n"));
3200 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003201 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 return IRQ_HANDLED;
3203 }
3204
3205 TRACE(("gdth_interrupt() index %d stat %d info %d\n",
3206 IStatus,ha->status,ha->info));
3207
3208 if (gdth_from_wait) {
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003209 *pIndex = (int)IStatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 }
3211
3212 if (IStatus == ASYNCINDEX) {
3213 TRACE2(("gdth_interrupt() async. event\n"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003214 gdth_async_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003216 spin_unlock_irqrestore(&ha->smp_lock, flags);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003217 gdth_next(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 return IRQ_HANDLED;
3219 }
3220
3221 if (IStatus == SPEZINDEX) {
3222 TRACE2(("Service unknown or not initialized !\n"));
3223 ha->dvr.size = sizeof(ha->dvr.eu.driver);
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003224 ha->dvr.eu.driver.ionode = ha->hanum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 gdth_store_event(ha, ES_DRIVER, 4, &ha->dvr);
3226 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003227 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 return IRQ_HANDLED;
3229 }
3230 scp = ha->cmd_tab[IStatus-2].cmnd;
3231 Service = ha->cmd_tab[IStatus-2].service;
3232 ha->cmd_tab[IStatus-2].cmnd = UNUSED_CMND;
3233 if (scp == UNUSED_CMND) {
3234 TRACE2(("gdth_interrupt() index to unused command (%d)\n",IStatus));
3235 ha->dvr.size = sizeof(ha->dvr.eu.driver);
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003236 ha->dvr.eu.driver.ionode = ha->hanum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 ha->dvr.eu.driver.index = IStatus;
3238 gdth_store_event(ha, ES_DRIVER, 1, &ha->dvr);
3239 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003240 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 return IRQ_HANDLED;
3242 }
3243 if (scp == INTERNAL_CMND) {
3244 TRACE(("gdth_interrupt() answer to internal command\n"));
3245 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003246 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 return IRQ_HANDLED;
3248 }
3249
3250 TRACE(("gdth_interrupt() sync. status\n"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003251 rval = gdth_sync_event(ha,Service,IStatus,scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003253 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 if (rval == 2) {
Boaz Harroshf842b642007-10-02 23:16:01 +02003255 gdth_putq(ha, scp, gdth_cmnd_priv(scp)->priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 } else if (rval == 1) {
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02003257 gdth_scsi_done(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 }
3259
3260#ifdef INT_COAL
3261 if (coalesced) {
3262 /* go to the next status in the status buffer */
3263 ++pcs;
3264#ifdef GDTH_STATISTICS
3265 ++act_int_coal;
3266 if (act_int_coal > max_int_coal) {
3267 max_int_coal = act_int_coal;
3268 printk("GDT: max_int_coal = %d\n",(ushort)max_int_coal);
3269 }
3270#endif
3271 /* see if there is another status */
3272 if (pcs->status == 0)
3273 /* Stop the coalesce loop */
3274 next = FALSE;
3275 }
3276 } while (next);
3277
3278 /* coalescing only for new GDT_PCIMPR controllers available */
3279 if (ha->type == GDT_PCIMPR && coalesced) {
Jeff Garzika52667f2007-10-02 22:55:53 +02003280 writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
3281 writeb(0, &dp6m_ptr->i960r.sema1_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 }
3283#endif
3284
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003285 gdth_next(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 return IRQ_HANDLED;
3287}
3288
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003289static irqreturn_t gdth_interrupt(int irq, void *dev_id)
3290{
Jeff Garzik230e8862007-12-13 16:14:12 -08003291 gdth_ha_str *ha = dev_id;
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003292
Jeff Garzik230e8862007-12-13 16:14:12 -08003293 return __gdth_interrupt(ha, false, NULL);
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02003294}
3295
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003296static int gdth_sync_event(gdth_ha_str *ha, int service, unchar index,
3297 Scsi_Cmnd *scp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 gdth_msg_str *msg;
3300 gdth_cmd_str *cmdp;
3301 unchar b, t;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02003302 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 cmdp = ha->pccb;
3305 TRACE(("gdth_sync_event() serv %d status %d\n",
3306 service,ha->status));
3307
3308 if (service == SCREENSERVICE) {
3309 msg = ha->pmsg;
3310 TRACE(("len: %d, answer: %d, ext: %d, alen: %d\n",
3311 msg->msg_len,msg->msg_answer,msg->msg_ext,msg->msg_alen));
3312 if (msg->msg_len > MSGLEN+1)
3313 msg->msg_len = MSGLEN+1;
3314 if (msg->msg_len)
3315 if (!(msg->msg_answer && msg->msg_ext)) {
3316 msg->msg_text[msg->msg_len] = '\0';
3317 printk("%s",msg->msg_text);
3318 }
3319
3320 if (msg->msg_ext && !msg->msg_answer) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003321 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 gdth_delay(0);
3323 cmdp->Service = SCREENSERVICE;
3324 cmdp->RequestBuffer = SCREEN_CMND;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003325 gdth_get_cmd_index(ha);
3326 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 cmdp->OpCode = GDT_READ;
3328 cmdp->BoardNode = LOCALBOARD;
3329 cmdp->u.screen.reserved = 0;
3330 cmdp->u.screen.su.msg.msg_handle= msg->msg_handle;
3331 cmdp->u.screen.su.msg.msg_addr = ha->msg_phys;
3332 ha->cmd_offs_dpmem = 0;
3333 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.screen.su.msg.msg_addr)
3334 + sizeof(ulong64);
3335 ha->cmd_cnt = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003336 gdth_copy_command(ha);
3337 gdth_release_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 return 0;
3339 }
3340
3341 if (msg->msg_answer && msg->msg_alen) {
3342 /* default answers (getchar() not possible) */
3343 if (msg->msg_alen == 1) {
3344 msg->msg_alen = 0;
3345 msg->msg_len = 1;
3346 msg->msg_text[0] = 0;
3347 } else {
3348 msg->msg_alen -= 2;
3349 msg->msg_len = 2;
3350 msg->msg_text[0] = 1;
3351 msg->msg_text[1] = 0;
3352 }
3353 msg->msg_ext = 0;
3354 msg->msg_answer = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003355 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 gdth_delay(0);
3357 cmdp->Service = SCREENSERVICE;
3358 cmdp->RequestBuffer = SCREEN_CMND;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003359 gdth_get_cmd_index(ha);
3360 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 cmdp->OpCode = GDT_WRITE;
3362 cmdp->BoardNode = LOCALBOARD;
3363 cmdp->u.screen.reserved = 0;
3364 cmdp->u.screen.su.msg.msg_handle= msg->msg_handle;
3365 cmdp->u.screen.su.msg.msg_addr = ha->msg_phys;
3366 ha->cmd_offs_dpmem = 0;
3367 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.screen.su.msg.msg_addr)
3368 + sizeof(ulong64);
3369 ha->cmd_cnt = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003370 gdth_copy_command(ha);
3371 gdth_release_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 return 0;
3373 }
3374 printk("\n");
3375
3376 } else {
Christoph Hellwig52759e62007-10-02 22:59:53 +02003377 b = scp->device->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 t = scp->device->id;
Boaz Harroshf842b642007-10-02 23:16:01 +02003379 if (cmndinfo->OpCode == -1 && b != ha->virt_bus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 ha->raw[BUS_L2P(ha,b)].io_cnt[t]--;
3381 }
3382 /* cache or raw service */
3383 if (ha->status == S_BSY) {
3384 TRACE2(("Controller busy -> retry !\n"));
Boaz Harroshf842b642007-10-02 23:16:01 +02003385 if (cmndinfo->OpCode == GDT_MOUNT)
3386 cmndinfo->OpCode = GDT_CLUST_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 /* retry */
3388 return 2;
3389 }
Boaz Harrosh3892d882007-10-02 23:18:03 +02003390 if (gdth_bufflen(scp))
3391 pci_unmap_sg(ha->pdev, gdth_sglist(scp), gdth_sg_count(scp),
3392 cmndinfo->dma_dir);
3393
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02003394 if (cmndinfo->sense_paddr)
3395 pci_unmap_page(ha->pdev, cmndinfo->sense_paddr, 16,
3396 PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
3398 if (ha->status == S_OK) {
Boaz Harroshf842b642007-10-02 23:16:01 +02003399 cmndinfo->status = S_OK;
3400 cmndinfo->info = ha->info;
3401 if (cmndinfo->OpCode != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 TRACE2(("gdth_sync_event(): special cmd 0x%x OK\n",
Boaz Harroshf842b642007-10-02 23:16:01 +02003403 cmndinfo->OpCode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 /* special commands GDT_CLUST_INFO/GDT_MOUNT ? */
Boaz Harroshf842b642007-10-02 23:16:01 +02003405 if (cmndinfo->OpCode == GDT_CLUST_INFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 ha->hdr[t].cluster_type = (unchar)ha->info;
3407 if (!(ha->hdr[t].cluster_type &
3408 CLUSTER_MOUNTED)) {
3409 /* NOT MOUNTED -> MOUNT */
Boaz Harroshf842b642007-10-02 23:16:01 +02003410 cmndinfo->OpCode = GDT_MOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 if (ha->hdr[t].cluster_type &
3412 CLUSTER_RESERVED) {
3413 /* cluster drive RESERVED (on the other node) */
Boaz Harroshf842b642007-10-02 23:16:01 +02003414 cmndinfo->phase = -2; /* reservation conflict */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 }
3416 } else {
Boaz Harroshf842b642007-10-02 23:16:01 +02003417 cmndinfo->OpCode = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 }
3419 } else {
Boaz Harroshf842b642007-10-02 23:16:01 +02003420 if (cmndinfo->OpCode == GDT_MOUNT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 ha->hdr[t].cluster_type |= CLUSTER_MOUNTED;
3422 ha->hdr[t].media_changed = TRUE;
Boaz Harroshf842b642007-10-02 23:16:01 +02003423 } else if (cmndinfo->OpCode == GDT_UNMOUNT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 ha->hdr[t].cluster_type &= ~CLUSTER_MOUNTED;
3425 ha->hdr[t].media_changed = TRUE;
3426 }
Boaz Harroshf842b642007-10-02 23:16:01 +02003427 cmndinfo->OpCode = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 }
3429 /* retry */
Boaz Harroshf842b642007-10-02 23:16:01 +02003430 cmndinfo->priority = HIGH_PRI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 return 2;
3432 } else {
3433 /* RESERVE/RELEASE ? */
3434 if (scp->cmnd[0] == RESERVE) {
3435 ha->hdr[t].cluster_type |= CLUSTER_RESERVED;
3436 } else if (scp->cmnd[0] == RELEASE) {
3437 ha->hdr[t].cluster_type &= ~CLUSTER_RESERVED;
3438 }
3439 scp->result = DID_OK << 16;
3440 scp->sense_buffer[0] = 0;
3441 }
3442 } else {
Boaz Harroshf842b642007-10-02 23:16:01 +02003443 cmndinfo->status = ha->status;
3444 cmndinfo->info = ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
Boaz Harroshf842b642007-10-02 23:16:01 +02003446 if (cmndinfo->OpCode != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 TRACE2(("gdth_sync_event(): special cmd 0x%x error 0x%x\n",
Boaz Harroshf842b642007-10-02 23:16:01 +02003448 cmndinfo->OpCode, ha->status));
3449 if (cmndinfo->OpCode == GDT_SCAN_START ||
3450 cmndinfo->OpCode == GDT_SCAN_END) {
3451 cmndinfo->OpCode = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 /* retry */
Boaz Harroshf842b642007-10-02 23:16:01 +02003453 cmndinfo->priority = HIGH_PRI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 return 2;
3455 }
3456 memset((char*)scp->sense_buffer,0,16);
3457 scp->sense_buffer[0] = 0x70;
3458 scp->sense_buffer[2] = NOT_READY;
3459 scp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
3460 } else if (service == CACHESERVICE) {
3461 if (ha->status == S_CACHE_UNKNOWN &&
3462 (ha->hdr[t].cluster_type &
3463 CLUSTER_RESERVE_STATE) == CLUSTER_RESERVE_STATE) {
3464 /* bus reset -> force GDT_CLUST_INFO */
3465 ha->hdr[t].cluster_type &= ~CLUSTER_RESERVED;
3466 }
3467 memset((char*)scp->sense_buffer,0,16);
3468 if (ha->status == (ushort)S_CACHE_RESERV) {
3469 scp->result = (DID_OK << 16) | (RESERVATION_CONFLICT << 1);
3470 } else {
3471 scp->sense_buffer[0] = 0x70;
3472 scp->sense_buffer[2] = NOT_READY;
3473 scp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
3474 }
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02003475 if (!cmndinfo->internal_command) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 ha->dvr.size = sizeof(ha->dvr.eu.sync);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003477 ha->dvr.eu.sync.ionode = ha->hanum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 ha->dvr.eu.sync.service = service;
3479 ha->dvr.eu.sync.status = ha->status;
3480 ha->dvr.eu.sync.info = ha->info;
3481 ha->dvr.eu.sync.hostdrive = t;
3482 if (ha->status >= 0x8000)
3483 gdth_store_event(ha, ES_SYNC, 0, &ha->dvr);
3484 else
3485 gdth_store_event(ha, ES_SYNC, service, &ha->dvr);
3486 }
3487 } else {
3488 /* sense buffer filled from controller firmware (DMA) */
3489 if (ha->status != S_RAW_SCSI || ha->info >= 0x100) {
3490 scp->result = DID_BAD_TARGET << 16;
3491 } else {
3492 scp->result = (DID_OK << 16) | ha->info;
3493 }
3494 }
3495 }
Boaz Harroshf842b642007-10-02 23:16:01 +02003496 if (!cmndinfo->wait_for_completion)
3497 cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 else
3499 return 1;
3500 }
3501
3502 return 0;
3503}
3504
3505static char *async_cache_tab[] = {
3506/* 0*/ "\011\000\002\002\002\004\002\006\004"
3507 "GDT HA %u, service %u, async. status %u/%lu unknown",
3508/* 1*/ "\011\000\002\002\002\004\002\006\004"
3509 "GDT HA %u, service %u, async. status %u/%lu unknown",
3510/* 2*/ "\005\000\002\006\004"
3511 "GDT HA %u, Host Drive %lu not ready",
3512/* 3*/ "\005\000\002\006\004"
3513 "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",
3514/* 4*/ "\005\000\002\006\004"
3515 "GDT HA %u, mirror update on Host Drive %lu failed",
3516/* 5*/ "\005\000\002\006\004"
3517 "GDT HA %u, Mirror Drive %lu failed",
3518/* 6*/ "\005\000\002\006\004"
3519 "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",
3520/* 7*/ "\005\000\002\006\004"
3521 "GDT HA %u, Host Drive %lu write protected",
3522/* 8*/ "\005\000\002\006\004"
3523 "GDT HA %u, media changed in Host Drive %lu",
3524/* 9*/ "\005\000\002\006\004"
3525 "GDT HA %u, Host Drive %lu is offline",
3526/*10*/ "\005\000\002\006\004"
3527 "GDT HA %u, media change of Mirror Drive %lu",
3528/*11*/ "\005\000\002\006\004"
3529 "GDT HA %u, Mirror Drive %lu is write protected",
3530/*12*/ "\005\000\002\006\004"
3531 "GDT HA %u, general error on Host Drive %lu. Please check the devices of this drive!",
3532/*13*/ "\007\000\002\006\002\010\002"
3533 "GDT HA %u, Array Drive %u: Cache Drive %u failed",
3534/*14*/ "\005\000\002\006\002"
3535 "GDT HA %u, Array Drive %u: FAIL state entered",
3536/*15*/ "\005\000\002\006\002"
3537 "GDT HA %u, Array Drive %u: error",
3538/*16*/ "\007\000\002\006\002\010\002"
3539 "GDT HA %u, Array Drive %u: failed drive replaced by Cache Drive %u",
3540/*17*/ "\005\000\002\006\002"
3541 "GDT HA %u, Array Drive %u: parity build failed",
3542/*18*/ "\005\000\002\006\002"
3543 "GDT HA %u, Array Drive %u: drive rebuild failed",
3544/*19*/ "\005\000\002\010\002"
3545 "GDT HA %u, Test of Hot Fix %u failed",
3546/*20*/ "\005\000\002\006\002"
3547 "GDT HA %u, Array Drive %u: drive build finished successfully",
3548/*21*/ "\005\000\002\006\002"
3549 "GDT HA %u, Array Drive %u: drive rebuild finished successfully",
3550/*22*/ "\007\000\002\006\002\010\002"
3551 "GDT HA %u, Array Drive %u: Hot Fix %u activated",
3552/*23*/ "\005\000\002\006\002"
3553 "GDT HA %u, Host Drive %u: processing of i/o aborted due to serious drive error",
3554/*24*/ "\005\000\002\010\002"
3555 "GDT HA %u, mirror update on Cache Drive %u completed",
3556/*25*/ "\005\000\002\010\002"
3557 "GDT HA %u, mirror update on Cache Drive %lu failed",
3558/*26*/ "\005\000\002\006\002"
3559 "GDT HA %u, Array Drive %u: drive rebuild started",
3560/*27*/ "\005\000\002\012\001"
3561 "GDT HA %u, Fault bus %u: SHELF OK detected",
3562/*28*/ "\005\000\002\012\001"
3563 "GDT HA %u, Fault bus %u: SHELF not OK detected",
3564/*29*/ "\007\000\002\012\001\013\001"
3565 "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug started",
3566/*30*/ "\007\000\002\012\001\013\001"
3567 "GDT HA %u, Fault bus %u, ID %u: new disk detected",
3568/*31*/ "\007\000\002\012\001\013\001"
3569 "GDT HA %u, Fault bus %u, ID %u: old disk detected",
3570/*32*/ "\007\000\002\012\001\013\001"
3571 "GDT HA %u, Fault bus %u, ID %u: plugging an active disk is invalid",
3572/*33*/ "\007\000\002\012\001\013\001"
3573 "GDT HA %u, Fault bus %u, ID %u: invalid device detected",
3574/*34*/ "\011\000\002\012\001\013\001\006\004"
3575 "GDT HA %u, Fault bus %u, ID %u: insufficient disk capacity (%lu MB required)",
3576/*35*/ "\007\000\002\012\001\013\001"
3577 "GDT HA %u, Fault bus %u, ID %u: disk write protected",
3578/*36*/ "\007\000\002\012\001\013\001"
3579 "GDT HA %u, Fault bus %u, ID %u: disk not available",
3580/*37*/ "\007\000\002\012\001\006\004"
3581 "GDT HA %u, Fault bus %u: swap detected (%lu)",
3582/*38*/ "\007\000\002\012\001\013\001"
3583 "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug finished successfully",
3584/*39*/ "\007\000\002\012\001\013\001"
3585 "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug aborted due to user Hot Plug",
3586/*40*/ "\007\000\002\012\001\013\001"
3587 "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug aborted",
3588/*41*/ "\007\000\002\012\001\013\001"
3589 "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug for Hot Fix started",
3590/*42*/ "\005\000\002\006\002"
3591 "GDT HA %u, Array Drive %u: drive build started",
3592/*43*/ "\003\000\002"
3593 "GDT HA %u, DRAM parity error detected",
3594/*44*/ "\005\000\002\006\002"
3595 "GDT HA %u, Mirror Drive %u: update started",
3596/*45*/ "\007\000\002\006\002\010\002"
3597 "GDT HA %u, Mirror Drive %u: Hot Fix %u activated",
3598/*46*/ "\005\000\002\006\002"
3599 "GDT HA %u, Array Drive %u: no matching Pool Hot Fix Drive available",
3600/*47*/ "\005\000\002\006\002"
3601 "GDT HA %u, Array Drive %u: Pool Hot Fix Drive available",
3602/*48*/ "\005\000\002\006\002"
3603 "GDT HA %u, Mirror Drive %u: no matching Pool Hot Fix Drive available",
3604/*49*/ "\005\000\002\006\002"
3605 "GDT HA %u, Mirror Drive %u: Pool Hot Fix Drive available",
3606/*50*/ "\007\000\002\012\001\013\001"
3607 "GDT HA %u, SCSI bus %u, ID %u: IGNORE_WIDE_RESIDUE message received",
3608/*51*/ "\005\000\002\006\002"
3609 "GDT HA %u, Array Drive %u: expand started",
3610/*52*/ "\005\000\002\006\002"
3611 "GDT HA %u, Array Drive %u: expand finished successfully",
3612/*53*/ "\005\000\002\006\002"
3613 "GDT HA %u, Array Drive %u: expand failed",
3614/*54*/ "\003\000\002"
3615 "GDT HA %u, CPU temperature critical",
3616/*55*/ "\003\000\002"
3617 "GDT HA %u, CPU temperature OK",
3618/*56*/ "\005\000\002\006\004"
3619 "GDT HA %u, Host drive %lu created",
3620/*57*/ "\005\000\002\006\002"
3621 "GDT HA %u, Array Drive %u: expand restarted",
3622/*58*/ "\005\000\002\006\002"
3623 "GDT HA %u, Array Drive %u: expand stopped",
3624/*59*/ "\005\000\002\010\002"
3625 "GDT HA %u, Mirror Drive %u: drive build quited",
3626/*60*/ "\005\000\002\006\002"
3627 "GDT HA %u, Array Drive %u: parity build quited",
3628/*61*/ "\005\000\002\006\002"
3629 "GDT HA %u, Array Drive %u: drive rebuild quited",
3630/*62*/ "\005\000\002\006\002"
3631 "GDT HA %u, Array Drive %u: parity verify started",
3632/*63*/ "\005\000\002\006\002"
3633 "GDT HA %u, Array Drive %u: parity verify done",
3634/*64*/ "\005\000\002\006\002"
3635 "GDT HA %u, Array Drive %u: parity verify failed",
3636/*65*/ "\005\000\002\006\002"
3637 "GDT HA %u, Array Drive %u: parity error detected",
3638/*66*/ "\005\000\002\006\002"
3639 "GDT HA %u, Array Drive %u: parity verify quited",
3640/*67*/ "\005\000\002\006\002"
3641 "GDT HA %u, Host Drive %u reserved",
3642/*68*/ "\005\000\002\006\002"
3643 "GDT HA %u, Host Drive %u mounted and released",
3644/*69*/ "\005\000\002\006\002"
3645 "GDT HA %u, Host Drive %u released",
3646/*70*/ "\003\000\002"
3647 "GDT HA %u, DRAM error detected and corrected with ECC",
3648/*71*/ "\003\000\002"
3649 "GDT HA %u, Uncorrectable DRAM error detected with ECC",
3650/*72*/ "\011\000\002\012\001\013\001\014\001"
3651 "GDT HA %u, SCSI bus %u, ID %u, LUN %u: reassigning block",
3652/*73*/ "\005\000\002\006\002"
3653 "GDT HA %u, Host drive %u resetted locally",
3654/*74*/ "\005\000\002\006\002"
3655 "GDT HA %u, Host drive %u resetted remotely",
3656/*75*/ "\003\000\002"
3657 "GDT HA %u, async. status 75 unknown",
3658};
3659
3660
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003661static int gdth_async_event(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 gdth_cmd_str *cmdp;
3664 int cmd_index;
3665
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 cmdp= ha->pccb;
3667 TRACE2(("gdth_async_event() ha %d serv %d\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003668 ha->hanum, ha->service));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669
3670 if (ha->service == SCREENSERVICE) {
3671 if (ha->status == MSG_REQUEST) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003672 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 gdth_delay(0);
3674 cmdp->Service = SCREENSERVICE;
3675 cmdp->RequestBuffer = SCREEN_CMND;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003676 cmd_index = gdth_get_cmd_index(ha);
3677 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 cmdp->OpCode = GDT_READ;
3679 cmdp->BoardNode = LOCALBOARD;
3680 cmdp->u.screen.reserved = 0;
3681 cmdp->u.screen.su.msg.msg_handle= MSG_INV_HANDLE;
3682 cmdp->u.screen.su.msg.msg_addr = ha->msg_phys;
3683 ha->cmd_offs_dpmem = 0;
3684 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.screen.su.msg.msg_addr)
3685 + sizeof(ulong64);
3686 ha->cmd_cnt = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003687 gdth_copy_command(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 if (ha->type == GDT_EISA)
3689 printk("[EISA slot %d] ",(ushort)ha->brd_phys);
3690 else if (ha->type == GDT_ISA)
3691 printk("[DPMEM 0x%4X] ",(ushort)ha->brd_phys);
3692 else
3693 printk("[PCI %d/%d] ",(ushort)(ha->brd_phys>>8),
3694 (ushort)((ha->brd_phys>>3)&0x1f));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003695 gdth_release_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 }
3697
3698 } else {
3699 if (ha->type == GDT_PCIMPR &&
3700 (ha->fw_vers & 0xff) >= 0x1a) {
3701 ha->dvr.size = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003702 ha->dvr.eu.async.ionode = ha->hanum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 ha->dvr.eu.async.status = ha->status;
3704 /* severity and event_string already set! */
3705 } else {
3706 ha->dvr.size = sizeof(ha->dvr.eu.async);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003707 ha->dvr.eu.async.ionode = ha->hanum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 ha->dvr.eu.async.service = ha->service;
3709 ha->dvr.eu.async.status = ha->status;
3710 ha->dvr.eu.async.info = ha->info;
3711 *(ulong32 *)ha->dvr.eu.async.scsi_coord = ha->info2;
3712 }
3713 gdth_store_event( ha, ES_ASYNC, ha->service, &ha->dvr );
3714 gdth_log_event( &ha->dvr, NULL );
3715
3716 /* new host drive from expand? */
3717 if (ha->service == CACHESERVICE && ha->status == 56) {
3718 TRACE2(("gdth_async_event(): new host drive %d created\n",
3719 (ushort)ha->info));
3720 /* gdth_analyse_hdrive(hanum, (ushort)ha->info); */
3721 }
3722 }
3723 return 1;
3724}
3725
3726static void gdth_log_event(gdth_evt_data *dvr, char *buffer)
3727{
3728 gdth_stackframe stack;
3729 char *f = NULL;
3730 int i,j;
3731
3732 TRACE2(("gdth_log_event()\n"));
3733 if (dvr->size == 0) {
3734 if (buffer == NULL) {
3735 printk("Adapter %d: %s\n",dvr->eu.async.ionode,dvr->event_string);
3736 } else {
3737 sprintf(buffer,"Adapter %d: %s\n",
3738 dvr->eu.async.ionode,dvr->event_string);
3739 }
3740 } else if (dvr->eu.async.service == CACHESERVICE &&
3741 INDEX_OK(dvr->eu.async.status, async_cache_tab)) {
3742 TRACE2(("GDT: Async. event cache service, event no.: %d\n",
3743 dvr->eu.async.status));
3744
3745 f = async_cache_tab[dvr->eu.async.status];
3746
3747 /* i: parameter to push, j: stack element to fill */
3748 for (j=0,i=1; i < f[0]; i+=2) {
3749 switch (f[i+1]) {
3750 case 4:
3751 stack.b[j++] = *(ulong32*)&dvr->eu.stream[(int)f[i]];
3752 break;
3753 case 2:
3754 stack.b[j++] = *(ushort*)&dvr->eu.stream[(int)f[i]];
3755 break;
3756 case 1:
3757 stack.b[j++] = *(unchar*)&dvr->eu.stream[(int)f[i]];
3758 break;
3759 default:
3760 break;
3761 }
3762 }
3763
3764 if (buffer == NULL) {
3765 printk(&f[(int)f[0]],stack);
3766 printk("\n");
3767 } else {
3768 sprintf(buffer,&f[(int)f[0]],stack);
3769 }
3770
3771 } else {
3772 if (buffer == NULL) {
3773 printk("GDT HA %u, Unknown async. event service %d event no. %d\n",
3774 dvr->eu.async.ionode,dvr->eu.async.service,dvr->eu.async.status);
3775 } else {
3776 sprintf(buffer,"GDT HA %u, Unknown async. event service %d event no. %d",
3777 dvr->eu.async.ionode,dvr->eu.async.service,dvr->eu.async.status);
3778 }
3779 }
3780}
3781
3782#ifdef GDTH_STATISTICS
8e879042005-04-17 15:28:39 -05003783static void gdth_timeout(ulong data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784{
3785 ulong32 i;
3786 Scsi_Cmnd *nscp;
3787 gdth_ha_str *ha;
3788 ulong flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
Boaz Harroshb31ddd32008-02-27 15:27:16 -08003790 BUG_ON(list_empty(&gdth_instances));
3791
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02003792 ha = list_first_entry(&gdth_instances, gdth_ha_str, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 spin_lock_irqsave(&ha->smp_lock, flags);
3794
3795 for (act_stats=0,i=0; i<GDTH_MAXCMDS; ++i)
3796 if (ha->cmd_tab[i].cmnd != UNUSED_CMND)
3797 ++act_stats;
3798
3799 for (act_rq=0,nscp=ha->req_first; nscp; nscp=(Scsi_Cmnd*)nscp->SCp.ptr)
3800 ++act_rq;
3801
3802 TRACE2(("gdth_to(): ints %d, ios %d, act_stats %d, act_rq %d\n",
3803 act_ints, act_ios, act_stats, act_rq));
3804 act_ints = act_ios = 0;
3805
3806 gdth_timer.expires = jiffies + 30 * HZ;
3807 add_timer(&gdth_timer);
3808 spin_unlock_irqrestore(&ha->smp_lock, flags);
3809}
3810#endif
3811
8e879042005-04-17 15:28:39 -05003812static void __init internal_setup(char *str,int *ints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813{
3814 int i, argc;
3815 char *cur_str, *argv;
3816
3817 TRACE2(("internal_setup() str %s ints[0] %d\n",
3818 str ? str:"NULL", ints ? ints[0]:0));
3819
3820 /* read irq[] from ints[] */
3821 if (ints) {
3822 argc = ints[0];
3823 if (argc > 0) {
3824 if (argc > MAXHA)
3825 argc = MAXHA;
3826 for (i = 0; i < argc; ++i)
3827 irq[i] = ints[i+1];
3828 }
3829 }
3830
3831 /* analyse string */
3832 argv = str;
3833 while (argv && (cur_str = strchr(argv, ':'))) {
3834 int val = 0, c = *++cur_str;
3835
3836 if (c == 'n' || c == 'N')
3837 val = 0;
3838 else if (c == 'y' || c == 'Y')
3839 val = 1;
3840 else
3841 val = (int)simple_strtoul(cur_str, NULL, 0);
3842
3843 if (!strncmp(argv, "disable:", 8))
3844 disable = val;
3845 else if (!strncmp(argv, "reserve_mode:", 13))
3846 reserve_mode = val;
3847 else if (!strncmp(argv, "reverse_scan:", 13))
3848 reverse_scan = val;
3849 else if (!strncmp(argv, "hdr_channel:", 12))
3850 hdr_channel = val;
3851 else if (!strncmp(argv, "max_ids:", 8))
3852 max_ids = val;
3853 else if (!strncmp(argv, "rescan:", 7))
3854 rescan = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 else if (!strncmp(argv, "shared_access:", 14))
3856 shared_access = val;
3857 else if (!strncmp(argv, "probe_eisa_isa:", 15))
3858 probe_eisa_isa = val;
3859 else if (!strncmp(argv, "reserve_list:", 13)) {
3860 reserve_list[0] = val;
3861 for (i = 1; i < MAX_RES_ARGS; i++) {
3862 cur_str = strchr(cur_str, ',');
3863 if (!cur_str)
3864 break;
3865 if (!isdigit((int)*++cur_str)) {
3866 --cur_str;
3867 break;
3868 }
3869 reserve_list[i] =
3870 (int)simple_strtoul(cur_str, NULL, 0);
3871 }
3872 if (!cur_str)
3873 break;
3874 argv = ++cur_str;
3875 continue;
3876 }
3877
3878 if ((argv = strchr(argv, ',')))
3879 ++argv;
3880 }
3881}
3882
3883int __init option_setup(char *str)
3884{
3885 int ints[MAXHA];
3886 char *cur = str;
3887 int i = 1;
3888
3889 TRACE2(("option_setup() str %s\n", str ? str:"NULL"));
3890
3891 while (cur && isdigit(*cur) && i <= MAXHA) {
3892 ints[i++] = simple_strtoul(cur, NULL, 0);
3893 if ((cur = strchr(cur, ',')) != NULL) cur++;
3894 }
3895
3896 ints[0] = i - 1;
3897 internal_setup(cur, ints);
3898 return 1;
3899}
3900
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003901static const char *gdth_ctr_name(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 TRACE2(("gdth_ctr_name()\n"));
3904
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 if (ha->type == GDT_EISA) {
3906 switch (ha->stype) {
3907 case GDT3_ID:
3908 return("GDT3000/3020");
3909 case GDT3A_ID:
3910 return("GDT3000A/3020A/3050A");
3911 case GDT3B_ID:
3912 return("GDT3000B/3010A");
3913 }
3914 } else if (ha->type == GDT_ISA) {
3915 return("GDT2000/2020");
3916 } else if (ha->type == GDT_PCI) {
Jeff Garzik8e9a8a02007-07-17 05:25:17 -04003917 switch (ha->pdev->device) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 case PCI_DEVICE_ID_VORTEX_GDT60x0:
3919 return("GDT6000/6020/6050");
3920 case PCI_DEVICE_ID_VORTEX_GDT6000B:
3921 return("GDT6000B/6010");
3922 }
3923 }
3924 /* new controllers (GDT_PCINEW, GDT_PCIMPR, ..) use board_info IOCTL! */
3925
3926 return("");
3927}
3928
8e879042005-04-17 15:28:39 -05003929static const char *gdth_info(struct Scsi_Host *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930{
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003931 gdth_ha_str *ha = shost_priv(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932
3933 TRACE2(("gdth_info()\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 return ((const char *)ha->binfo.type_string);
3935}
3936
8e879042005-04-17 15:28:39 -05003937static int gdth_eh_bus_reset(Scsi_Cmnd *scp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938{
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003939 gdth_ha_str *ha = shost_priv(scp->device->host);
3940 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 ulong flags;
3942 Scsi_Cmnd *cmnd;
3943 unchar b;
3944
3945 TRACE2(("gdth_eh_bus_reset()\n"));
3946
Christoph Hellwig52759e62007-10-02 22:59:53 +02003947 b = scp->device->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948
3949 /* clear command tab */
3950 spin_lock_irqsave(&ha->smp_lock, flags);
3951 for (i = 0; i < GDTH_MAXCMDS; ++i) {
3952 cmnd = ha->cmd_tab[i].cmnd;
3953 if (!SPECIAL_SCP(cmnd) && cmnd->device->channel == b)
3954 ha->cmd_tab[i].cmnd = UNUSED_CMND;
3955 }
3956 spin_unlock_irqrestore(&ha->smp_lock, flags);
3957
3958 if (b == ha->virt_bus) {
3959 /* host drives */
3960 for (i = 0; i < MAX_HDRIVES; ++i) {
3961 if (ha->hdr[i].present) {
3962 spin_lock_irqsave(&ha->smp_lock, flags);
3963 gdth_polling = TRUE;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003964 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 gdth_delay(0);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003966 if (gdth_internal_cmd(ha, CACHESERVICE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 GDT_CLUST_RESET, i, 0, 0))
3968 ha->hdr[i].cluster_type &= ~CLUSTER_RESERVED;
3969 gdth_polling = FALSE;
3970 spin_unlock_irqrestore(&ha->smp_lock, flags);
3971 }
3972 }
3973 } else {
3974 /* raw devices */
3975 spin_lock_irqsave(&ha->smp_lock, flags);
3976 for (i = 0; i < MAXID; ++i)
3977 ha->raw[BUS_L2P(ha,b)].io_cnt[i] = 0;
3978 gdth_polling = TRUE;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003979 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 gdth_delay(0);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003981 gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_RESET_BUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 BUS_L2P(ha,b), 0, 0);
3983 gdth_polling = FALSE;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07003984 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 }
3986 return SUCCESS;
3987}
3988
8e879042005-04-17 15:28:39 -05003989static int gdth_bios_param(struct scsi_device *sdev,struct block_device *bdev,sector_t cap,int *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990{
3991 unchar b, t;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003992 gdth_ha_str *ha = shost_priv(sdev->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 struct scsi_device *sd;
3994 unsigned capacity;
3995
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 sd = sdev;
3997 capacity = cap;
Christoph Hellwig52759e62007-10-02 22:59:53 +02003998 b = sd->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 t = sd->id;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004000 TRACE2(("gdth_bios_param() ha %d bus %d target %d\n", ha->hanum, b, t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001
4002 if (b != ha->virt_bus || ha->hdr[t].heads == 0) {
4003 /* raw device or host drive without mapping information */
4004 TRACE2(("Evaluate mapping\n"));
4005 gdth_eval_mapping(capacity,&ip[2],&ip[0],&ip[1]);
4006 } else {
4007 ip[0] = ha->hdr[t].heads;
4008 ip[1] = ha->hdr[t].secs;
4009 ip[2] = capacity / ip[0] / ip[1];
4010 }
4011
4012 TRACE2(("gdth_bios_param(): %d heads, %d secs, %d cyls\n",
4013 ip[0],ip[1],ip[2]));
4014 return 0;
4015}
4016
4017
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02004018static int gdth_queuecommand(struct scsi_cmnd *scp,
4019 void (*done)(struct scsi_cmnd *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020{
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004021 gdth_ha_str *ha = shost_priv(scp->device->host);
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02004022 struct gdth_cmndinfo *cmndinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
4024 TRACE(("gdth_queuecommand() cmd 0x%x\n", scp->cmnd[0]));
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02004025
4026 cmndinfo = gdth_get_cmndinfo(ha);
4027 BUG_ON(!cmndinfo);
4028
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02004029 scp->scsi_done = done;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02004030 gdth_update_timeout(scp, scp->timeout_per_command * 6);
Boaz Harroshf842b642007-10-02 23:16:01 +02004031 cmndinfo->priority = DEFAULT_PRI;
Boaz Harrosh3892d882007-10-02 23:18:03 +02004032
4033 gdth_set_bufflen(scp, scsi_bufflen(scp));
4034 gdth_set_sg_count(scp, scsi_sg_count(scp));
4035 gdth_set_sglist(scp, scsi_sglist(scp));
4036
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02004037 return __gdth_queuecommand(ha, scp, cmndinfo);
4038}
4039
4040static int __gdth_queuecommand(gdth_ha_str *ha, struct scsi_cmnd *scp,
4041 struct gdth_cmndinfo *cmndinfo)
4042{
4043 scp->host_scribble = (unsigned char *)cmndinfo;
Boaz Harroshf842b642007-10-02 23:16:01 +02004044 cmndinfo->wait_for_completion = 1;
4045 cmndinfo->phase = -1;
4046 cmndinfo->OpCode = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048#ifdef GDTH_STATISTICS
4049 ++act_ios;
4050#endif
4051
Boaz Harroshf842b642007-10-02 23:16:01 +02004052 gdth_putq(ha, scp, cmndinfo->priority);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004053 gdth_next(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 return 0;
4055}
4056
4057
4058static int gdth_open(struct inode *inode, struct file *filep)
4059{
4060 gdth_ha_str *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004062 list_for_each_entry(ha, &gdth_instances, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 if (!ha->sdev)
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004064 ha->sdev = scsi_get_host_dev(ha->shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 }
4066
4067 TRACE(("gdth_open()\n"));
4068 return 0;
4069}
4070
4071static int gdth_close(struct inode *inode, struct file *filep)
4072{
4073 TRACE(("gdth_close()\n"));
4074 return 0;
4075}
4076
4077static int ioc_event(void __user *arg)
4078{
4079 gdth_ioctl_event evt;
4080 gdth_ha_str *ha;
4081 ulong flags;
4082
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004083 if (copy_from_user(&evt, arg, sizeof(gdth_ioctl_event)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004085 ha = gdth_find_ha(evt.ionode);
4086 if (!ha)
4087 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088
4089 if (evt.erase == 0xff) {
4090 if (evt.event.event_source == ES_TEST)
4091 evt.event.event_data.size=sizeof(evt.event.event_data.eu.test);
4092 else if (evt.event.event_source == ES_DRIVER)
4093 evt.event.event_data.size=sizeof(evt.event.event_data.eu.driver);
4094 else if (evt.event.event_source == ES_SYNC)
4095 evt.event.event_data.size=sizeof(evt.event.event_data.eu.sync);
4096 else
4097 evt.event.event_data.size=sizeof(evt.event.event_data.eu.async);
4098 spin_lock_irqsave(&ha->smp_lock, flags);
4099 gdth_store_event(ha, evt.event.event_source, evt.event.event_idx,
4100 &evt.event.event_data);
4101 spin_unlock_irqrestore(&ha->smp_lock, flags);
4102 } else if (evt.erase == 0xfe) {
4103 gdth_clear_events();
4104 } else if (evt.erase == 0) {
4105 evt.handle = gdth_read_event(ha, evt.handle, &evt.event);
4106 } else {
4107 gdth_readapp_event(ha, evt.erase, &evt.event);
4108 }
4109 if (copy_to_user(arg, &evt, sizeof(gdth_ioctl_event)))
4110 return -EFAULT;
4111 return 0;
4112}
4113
4114static int ioc_lockdrv(void __user *arg)
4115{
4116 gdth_ioctl_lockdrv ldrv;
4117 unchar i, j;
4118 ulong flags;
4119 gdth_ha_str *ha;
4120
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004121 if (copy_from_user(&ldrv, arg, sizeof(gdth_ioctl_lockdrv)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004123 ha = gdth_find_ha(ldrv.ionode);
4124 if (!ha)
4125 return -EFAULT;
4126
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 for (i = 0; i < ldrv.drive_cnt && i < MAX_HDRIVES; ++i) {
4128 j = ldrv.drives[i];
4129 if (j >= MAX_HDRIVES || !ha->hdr[j].present)
4130 continue;
4131 if (ldrv.lock) {
4132 spin_lock_irqsave(&ha->smp_lock, flags);
4133 ha->hdr[j].lock = 1;
4134 spin_unlock_irqrestore(&ha->smp_lock, flags);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004135 gdth_wait_completion(ha, ha->bus_cnt, j);
4136 gdth_stop_timeout(ha, ha->bus_cnt, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 } else {
4138 spin_lock_irqsave(&ha->smp_lock, flags);
4139 ha->hdr[j].lock = 0;
4140 spin_unlock_irqrestore(&ha->smp_lock, flags);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004141 gdth_start_timeout(ha, ha->bus_cnt, j);
4142 gdth_next(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 }
4144 }
4145 return 0;
4146}
4147
4148static int ioc_resetdrv(void __user *arg, char *cmnd)
4149{
4150 gdth_ioctl_reset res;
4151 gdth_cmd_str cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 gdth_ha_str *ha;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004153 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154
4155 if (copy_from_user(&res, arg, sizeof(gdth_ioctl_reset)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004156 res.number >= MAX_HDRIVES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004158 ha = gdth_find_ha(res.ionode);
4159 if (!ha)
4160 return -EFAULT;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004161
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 if (!ha->hdr[res.number].present)
4163 return 0;
4164 memset(&cmd, 0, sizeof(gdth_cmd_str));
4165 cmd.Service = CACHESERVICE;
4166 cmd.OpCode = GDT_CLUST_RESET;
4167 if (ha->cache_feat & GDT_64BIT)
4168 cmd.u.cache64.DeviceNo = res.number;
4169 else
4170 cmd.u.cache.DeviceNo = res.number;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004171
4172 rval = __gdth_execute(ha->sdev, &cmd, cmnd, 30, NULL);
4173 if (rval < 0)
4174 return rval;
4175 res.status = rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176
4177 if (copy_to_user(arg, &res, sizeof(gdth_ioctl_reset)))
4178 return -EFAULT;
4179 return 0;
4180}
4181
4182static int ioc_general(void __user *arg, char *cmnd)
4183{
4184 gdth_ioctl_general gen;
4185 char *buf = NULL;
4186 ulong64 paddr;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004187 gdth_ha_str *ha;
4188 int rval;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004189
4190 if (copy_from_user(&gen, arg, sizeof(gdth_ioctl_general)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004192 ha = gdth_find_ha(gen.ionode);
4193 if (!ha)
4194 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 if (gen.data_len + gen.sense_len != 0) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004196 if (!(buf = gdth_ioctl_alloc(ha, gen.data_len + gen.sense_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 FALSE, &paddr)))
4198 return -EFAULT;
4199 if (copy_from_user(buf, arg + sizeof(gdth_ioctl_general),
4200 gen.data_len + gen.sense_len)) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004201 gdth_ioctl_free(ha, gen.data_len+gen.sense_len, buf, paddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 return -EFAULT;
4203 }
4204
4205 if (gen.command.OpCode == GDT_IOCTL) {
4206 gen.command.u.ioctl.p_param = paddr;
4207 } else if (gen.command.Service == CACHESERVICE) {
4208 if (ha->cache_feat & GDT_64BIT) {
4209 /* copy elements from 32-bit IOCTL structure */
4210 gen.command.u.cache64.BlockCnt = gen.command.u.cache.BlockCnt;
4211 gen.command.u.cache64.BlockNo = gen.command.u.cache.BlockNo;
4212 gen.command.u.cache64.DeviceNo = gen.command.u.cache.DeviceNo;
4213 /* addresses */
4214 if (ha->cache_feat & SCATTER_GATHER) {
4215 gen.command.u.cache64.DestAddr = (ulong64)-1;
4216 gen.command.u.cache64.sg_canz = 1;
4217 gen.command.u.cache64.sg_lst[0].sg_ptr = paddr;
4218 gen.command.u.cache64.sg_lst[0].sg_len = gen.data_len;
4219 gen.command.u.cache64.sg_lst[1].sg_len = 0;
4220 } else {
4221 gen.command.u.cache64.DestAddr = paddr;
4222 gen.command.u.cache64.sg_canz = 0;
4223 }
4224 } else {
4225 if (ha->cache_feat & SCATTER_GATHER) {
4226 gen.command.u.cache.DestAddr = 0xffffffff;
4227 gen.command.u.cache.sg_canz = 1;
4228 gen.command.u.cache.sg_lst[0].sg_ptr = (ulong32)paddr;
4229 gen.command.u.cache.sg_lst[0].sg_len = gen.data_len;
4230 gen.command.u.cache.sg_lst[1].sg_len = 0;
4231 } else {
4232 gen.command.u.cache.DestAddr = paddr;
4233 gen.command.u.cache.sg_canz = 0;
4234 }
4235 }
4236 } else if (gen.command.Service == SCSIRAWSERVICE) {
4237 if (ha->raw_feat & GDT_64BIT) {
4238 /* copy elements from 32-bit IOCTL structure */
4239 char cmd[16];
4240 gen.command.u.raw64.sense_len = gen.command.u.raw.sense_len;
4241 gen.command.u.raw64.bus = gen.command.u.raw.bus;
4242 gen.command.u.raw64.lun = gen.command.u.raw.lun;
4243 gen.command.u.raw64.target = gen.command.u.raw.target;
4244 memcpy(cmd, gen.command.u.raw.cmd, 16);
4245 memcpy(gen.command.u.raw64.cmd, cmd, 16);
4246 gen.command.u.raw64.clen = gen.command.u.raw.clen;
4247 gen.command.u.raw64.sdlen = gen.command.u.raw.sdlen;
4248 gen.command.u.raw64.direction = gen.command.u.raw.direction;
4249 /* addresses */
4250 if (ha->raw_feat & SCATTER_GATHER) {
4251 gen.command.u.raw64.sdata = (ulong64)-1;
4252 gen.command.u.raw64.sg_ranz = 1;
4253 gen.command.u.raw64.sg_lst[0].sg_ptr = paddr;
4254 gen.command.u.raw64.sg_lst[0].sg_len = gen.data_len;
4255 gen.command.u.raw64.sg_lst[1].sg_len = 0;
4256 } else {
4257 gen.command.u.raw64.sdata = paddr;
4258 gen.command.u.raw64.sg_ranz = 0;
4259 }
4260 gen.command.u.raw64.sense_data = paddr + gen.data_len;
4261 } else {
4262 if (ha->raw_feat & SCATTER_GATHER) {
4263 gen.command.u.raw.sdata = 0xffffffff;
4264 gen.command.u.raw.sg_ranz = 1;
4265 gen.command.u.raw.sg_lst[0].sg_ptr = (ulong32)paddr;
4266 gen.command.u.raw.sg_lst[0].sg_len = gen.data_len;
4267 gen.command.u.raw.sg_lst[1].sg_len = 0;
4268 } else {
4269 gen.command.u.raw.sdata = paddr;
4270 gen.command.u.raw.sg_ranz = 0;
4271 }
4272 gen.command.u.raw.sense_data = (ulong32)paddr + gen.data_len;
4273 }
4274 } else {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004275 gdth_ioctl_free(ha, gen.data_len+gen.sense_len, buf, paddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 return -EFAULT;
4277 }
4278 }
4279
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004280 rval = __gdth_execute(ha->sdev, &gen.command, cmnd, gen.timeout, &gen.info);
4281 if (rval < 0)
4282 return rval;
4283 gen.status = rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
4285 if (copy_to_user(arg + sizeof(gdth_ioctl_general), buf,
4286 gen.data_len + gen.sense_len)) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004287 gdth_ioctl_free(ha, gen.data_len+gen.sense_len, buf, paddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 return -EFAULT;
4289 }
4290 if (copy_to_user(arg, &gen,
4291 sizeof(gdth_ioctl_general) - sizeof(gdth_cmd_str))) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004292 gdth_ioctl_free(ha, gen.data_len+gen.sense_len, buf, paddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 return -EFAULT;
4294 }
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004295 gdth_ioctl_free(ha, gen.data_len+gen.sense_len, buf, paddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 return 0;
4297}
4298
4299static int ioc_hdrlist(void __user *arg, char *cmnd)
4300{
4301 gdth_ioctl_rescan *rsc;
4302 gdth_cmd_str *cmd;
4303 gdth_ha_str *ha;
4304 unchar i;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004305 int rc = -ENOMEM;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004306 u32 cluster_type = 0;
4307
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 rsc = kmalloc(sizeof(*rsc), GFP_KERNEL);
4309 cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
4310 if (!rsc || !cmd)
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004311 goto free_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312
4313 if (copy_from_user(rsc, arg, sizeof(gdth_ioctl_rescan)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004314 (NULL == (ha = gdth_find_ha(rsc->ionode)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 rc = -EFAULT;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004316 goto free_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 memset(cmd, 0, sizeof(gdth_cmd_str));
4319
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 for (i = 0; i < MAX_HDRIVES; ++i) {
4321 if (!ha->hdr[i].present) {
4322 rsc->hdr_list[i].bus = 0xff;
4323 continue;
4324 }
4325 rsc->hdr_list[i].bus = ha->virt_bus;
4326 rsc->hdr_list[i].target = i;
4327 rsc->hdr_list[i].lun = 0;
4328 rsc->hdr_list[i].cluster_type = ha->hdr[i].cluster_type;
4329 if (ha->hdr[i].cluster_type & CLUSTER_DRIVE) {
4330 cmd->Service = CACHESERVICE;
4331 cmd->OpCode = GDT_CLUST_INFO;
4332 if (ha->cache_feat & GDT_64BIT)
4333 cmd->u.cache64.DeviceNo = i;
4334 else
4335 cmd->u.cache.DeviceNo = i;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004336 if (__gdth_execute(ha->sdev, cmd, cmnd, 30, &cluster_type) == S_OK)
4337 rsc->hdr_list[i].cluster_type = cluster_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 }
4339 }
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004340
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 if (copy_to_user(arg, rsc, sizeof(gdth_ioctl_rescan)))
4342 rc = -EFAULT;
4343 else
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004344 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345
4346free_fail:
4347 kfree(rsc);
4348 kfree(cmd);
4349 return rc;
4350}
4351
4352static int ioc_rescan(void __user *arg, char *cmnd)
4353{
4354 gdth_ioctl_rescan *rsc;
4355 gdth_cmd_str *cmd;
4356 ushort i, status, hdr_cnt;
4357 ulong32 info;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004358 int cyls, hds, secs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 int rc = -ENOMEM;
4360 ulong flags;
4361 gdth_ha_str *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362
4363 rsc = kmalloc(sizeof(*rsc), GFP_KERNEL);
4364 cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
4365 if (!cmd || !rsc)
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004366 goto free_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367
4368 if (copy_from_user(rsc, arg, sizeof(gdth_ioctl_rescan)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004369 (NULL == (ha = gdth_find_ha(rsc->ionode)))) {
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004370 rc = -EFAULT;
4371 goto free_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 memset(cmd, 0, sizeof(gdth_cmd_str));
4374
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 if (rsc->flag == 0) {
4376 /* old method: re-init. cache service */
4377 cmd->Service = CACHESERVICE;
4378 if (ha->cache_feat & GDT_64BIT) {
4379 cmd->OpCode = GDT_X_INIT_HOST;
4380 cmd->u.cache64.DeviceNo = LINUX_OS;
4381 } else {
4382 cmd->OpCode = GDT_INIT;
4383 cmd->u.cache.DeviceNo = LINUX_OS;
4384 }
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004385
4386 status = __gdth_execute(ha->sdev, cmd, cmnd, 30, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 i = 0;
4388 hdr_cnt = (status == S_OK ? (ushort)info : 0);
4389 } else {
4390 i = rsc->hdr_no;
4391 hdr_cnt = i + 1;
4392 }
4393
4394 for (; i < hdr_cnt && i < MAX_HDRIVES; ++i) {
4395 cmd->Service = CACHESERVICE;
4396 cmd->OpCode = GDT_INFO;
4397 if (ha->cache_feat & GDT_64BIT)
4398 cmd->u.cache64.DeviceNo = i;
4399 else
4400 cmd->u.cache.DeviceNo = i;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004401
4402 status = __gdth_execute(ha->sdev, cmd, cmnd, 30, &info);
4403
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 spin_lock_irqsave(&ha->smp_lock, flags);
4405 rsc->hdr_list[i].bus = ha->virt_bus;
4406 rsc->hdr_list[i].target = i;
4407 rsc->hdr_list[i].lun = 0;
4408 if (status != S_OK) {
4409 ha->hdr[i].present = FALSE;
4410 } else {
4411 ha->hdr[i].present = TRUE;
4412 ha->hdr[i].size = info;
4413 /* evaluate mapping */
4414 ha->hdr[i].size &= ~SECS32;
4415 gdth_eval_mapping(ha->hdr[i].size,&cyls,&hds,&secs);
4416 ha->hdr[i].heads = hds;
4417 ha->hdr[i].secs = secs;
4418 /* round size */
4419 ha->hdr[i].size = cyls * hds * secs;
4420 }
4421 spin_unlock_irqrestore(&ha->smp_lock, flags);
4422 if (status != S_OK)
4423 continue;
4424
4425 /* extended info, if GDT_64BIT, for drives > 2 TB */
4426 /* but we need ha->info2, not yet stored in scp->SCp */
4427
4428 /* devtype, cluster info, R/W attribs */
4429 cmd->Service = CACHESERVICE;
4430 cmd->OpCode = GDT_DEVTYPE;
4431 if (ha->cache_feat & GDT_64BIT)
4432 cmd->u.cache64.DeviceNo = i;
4433 else
4434 cmd->u.cache.DeviceNo = i;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004435
4436 status = __gdth_execute(ha->sdev, cmd, cmnd, 30, &info);
4437
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 spin_lock_irqsave(&ha->smp_lock, flags);
4439 ha->hdr[i].devtype = (status == S_OK ? (ushort)info : 0);
4440 spin_unlock_irqrestore(&ha->smp_lock, flags);
4441
4442 cmd->Service = CACHESERVICE;
4443 cmd->OpCode = GDT_CLUST_INFO;
4444 if (ha->cache_feat & GDT_64BIT)
4445 cmd->u.cache64.DeviceNo = i;
4446 else
4447 cmd->u.cache.DeviceNo = i;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004448
4449 status = __gdth_execute(ha->sdev, cmd, cmnd, 30, &info);
4450
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 spin_lock_irqsave(&ha->smp_lock, flags);
4452 ha->hdr[i].cluster_type =
4453 ((status == S_OK && !shared_access) ? (ushort)info : 0);
4454 spin_unlock_irqrestore(&ha->smp_lock, flags);
4455 rsc->hdr_list[i].cluster_type = ha->hdr[i].cluster_type;
4456
4457 cmd->Service = CACHESERVICE;
4458 cmd->OpCode = GDT_RW_ATTRIBS;
4459 if (ha->cache_feat & GDT_64BIT)
4460 cmd->u.cache64.DeviceNo = i;
4461 else
4462 cmd->u.cache.DeviceNo = i;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004463
4464 status = __gdth_execute(ha->sdev, cmd, cmnd, 30, &info);
4465
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 spin_lock_irqsave(&ha->smp_lock, flags);
4467 ha->hdr[i].rw_attribs = (status == S_OK ? (ushort)info : 0);
4468 spin_unlock_irqrestore(&ha->smp_lock, flags);
4469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470
4471 if (copy_to_user(arg, rsc, sizeof(gdth_ioctl_rescan)))
4472 rc = -EFAULT;
4473 else
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004474 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
4476free_fail:
4477 kfree(rsc);
4478 kfree(cmd);
4479 return rc;
4480}
4481
4482static int gdth_ioctl(struct inode *inode, struct file *filep,
4483 unsigned int cmd, unsigned long arg)
4484{
4485 gdth_ha_str *ha;
4486 Scsi_Cmnd *scp;
4487 ulong flags;
4488 char cmnd[MAX_COMMAND_SIZE];
4489 void __user *argp = (void __user *)arg;
4490
4491 memset(cmnd, 0xff, 12);
4492
4493 TRACE(("gdth_ioctl() cmd 0x%x\n", cmd));
4494
4495 switch (cmd) {
4496 case GDTIOCTL_CTRCNT:
4497 {
4498 int cnt = gdth_ctr_count;
4499 if (put_user(cnt, (int __user *)argp))
4500 return -EFAULT;
4501 break;
4502 }
4503
4504 case GDTIOCTL_DRVERS:
4505 {
4506 int ver = (GDTH_VERSION<<8) | GDTH_SUBVERSION;
4507 if (put_user(ver, (int __user *)argp))
4508 return -EFAULT;
4509 break;
4510 }
4511
4512 case GDTIOCTL_OSVERS:
4513 {
4514 gdth_ioctl_osvers osv;
4515
4516 osv.version = (unchar)(LINUX_VERSION_CODE >> 16);
4517 osv.subversion = (unchar)(LINUX_VERSION_CODE >> 8);
4518 osv.revision = (ushort)(LINUX_VERSION_CODE & 0xff);
4519 if (copy_to_user(argp, &osv, sizeof(gdth_ioctl_osvers)))
4520 return -EFAULT;
4521 break;
4522 }
4523
4524 case GDTIOCTL_CTRTYPE:
4525 {
4526 gdth_ioctl_ctrtype ctrt;
4527
4528 if (copy_from_user(&ctrt, argp, sizeof(gdth_ioctl_ctrtype)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004529 (NULL == (ha = gdth_find_ha(ctrt.ionode))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004531
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 if (ha->type == GDT_ISA || ha->type == GDT_EISA) {
4533 ctrt.type = (unchar)((ha->stype>>20) - 0x10);
4534 } else {
4535 if (ha->type != GDT_PCIMPR) {
4536 ctrt.type = (unchar)((ha->stype<<4) + 6);
4537 } else {
4538 ctrt.type =
4539 (ha->oem_id == OEM_ID_INTEL ? 0xfd : 0xfe);
4540 if (ha->stype >= 0x300)
Jeff Garzik8e9a8a02007-07-17 05:25:17 -04004541 ctrt.ext_type = 0x6000 | ha->pdev->subsystem_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 else
4543 ctrt.ext_type = 0x6000 | ha->stype;
4544 }
Jeff Garzik8e9a8a02007-07-17 05:25:17 -04004545 ctrt.device_id = ha->pdev->device;
4546 ctrt.sub_device_id = ha->pdev->subsystem_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 }
4548 ctrt.info = ha->brd_phys;
4549 ctrt.oem_id = ha->oem_id;
4550 if (copy_to_user(argp, &ctrt, sizeof(gdth_ioctl_ctrtype)))
4551 return -EFAULT;
4552 break;
4553 }
4554
4555 case GDTIOCTL_GENERAL:
4556 return ioc_general(argp, cmnd);
4557
4558 case GDTIOCTL_EVENT:
4559 return ioc_event(argp);
4560
4561 case GDTIOCTL_LOCKDRV:
4562 return ioc_lockdrv(argp);
4563
4564 case GDTIOCTL_LOCKCHN:
4565 {
4566 gdth_ioctl_lockchn lchn;
4567 unchar i, j;
4568
4569 if (copy_from_user(&lchn, argp, sizeof(gdth_ioctl_lockchn)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004570 (NULL == (ha = gdth_find_ha(lchn.ionode))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004572
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 i = lchn.channel;
4574 if (i < ha->bus_cnt) {
4575 if (lchn.lock) {
4576 spin_lock_irqsave(&ha->smp_lock, flags);
4577 ha->raw[i].lock = 1;
4578 spin_unlock_irqrestore(&ha->smp_lock, flags);
4579 for (j = 0; j < ha->tid_cnt; ++j) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004580 gdth_wait_completion(ha, i, j);
4581 gdth_stop_timeout(ha, i, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 }
4583 } else {
4584 spin_lock_irqsave(&ha->smp_lock, flags);
4585 ha->raw[i].lock = 0;
4586 spin_unlock_irqrestore(&ha->smp_lock, flags);
4587 for (j = 0; j < ha->tid_cnt; ++j) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004588 gdth_start_timeout(ha, i, j);
4589 gdth_next(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 }
4591 }
4592 }
4593 break;
4594 }
4595
4596 case GDTIOCTL_RESCAN:
4597 return ioc_rescan(argp, cmnd);
4598
4599 case GDTIOCTL_HDRLIST:
4600 return ioc_hdrlist(argp, cmnd);
4601
4602 case GDTIOCTL_RESET_BUS:
4603 {
4604 gdth_ioctl_reset res;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004605 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606
4607 if (copy_from_user(&res, argp, sizeof(gdth_ioctl_reset)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004608 (NULL == (ha = gdth_find_ha(res.ionode))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02004611 scp = kzalloc(sizeof(*scp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 if (!scp)
4613 return -ENOMEM;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004614 scp->device = ha->sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 scp->cmd_len = 12;
Christoph Hellwig52759e62007-10-02 22:59:53 +02004616 scp->device->channel = res.number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 rval = gdth_eh_bus_reset(scp);
4618 res.status = (rval == SUCCESS ? S_OK : S_GENERR);
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004619 kfree(scp);
Jeff Garzik8d7a5da2007-10-02 22:54:28 +02004620
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 if (copy_to_user(argp, &res, sizeof(gdth_ioctl_reset)))
4622 return -EFAULT;
4623 break;
4624 }
4625
4626 case GDTIOCTL_RESET_DRV:
4627 return ioc_resetdrv(argp, cmnd);
4628
4629 default:
4630 break;
4631 }
4632 return 0;
4633}
4634
4635
4636/* flush routine */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004637static void gdth_flush(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638{
4639 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 gdth_cmd_str gdtcmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 char cmnd[MAX_COMMAND_SIZE];
4642 memset(cmnd, 0xff, MAX_COMMAND_SIZE);
4643
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004644 TRACE2(("gdth_flush() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 for (i = 0; i < MAX_HDRIVES; ++i) {
4647 if (ha->hdr[i].present) {
4648 gdtcmd.BoardNode = LOCALBOARD;
4649 gdtcmd.Service = CACHESERVICE;
4650 gdtcmd.OpCode = GDT_FLUSH;
4651 if (ha->cache_feat & GDT_64BIT) {
4652 gdtcmd.u.cache64.DeviceNo = i;
4653 gdtcmd.u.cache64.BlockNo = 1;
4654 gdtcmd.u.cache64.sg_canz = 0;
4655 } else {
4656 gdtcmd.u.cache.DeviceNo = i;
4657 gdtcmd.u.cache.BlockNo = 1;
4658 gdtcmd.u.cache.sg_canz = 0;
4659 }
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004660 TRACE2(("gdth_flush(): flush ha %d drive %d\n", ha->hanum, i));
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004661
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004662 gdth_execute(ha->shost, &gdtcmd, cmnd, 30, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 }
4664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665}
4666
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004667/* configure lun */
4668static int gdth_slave_configure(struct scsi_device *sdev)
4669{
4670 scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
4671 sdev->skip_ms_page_3f = 1;
4672 sdev->skip_ms_page_8 = 1;
4673 return 0;
4674}
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004675
Christoph Hellwig835cc242007-10-02 23:09:56 +02004676static struct scsi_host_template gdth_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 .name = "GDT SCSI Disk Array Controller",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 .info = gdth_info,
4679 .queuecommand = gdth_queuecommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 .eh_bus_reset_handler = gdth_eh_bus_reset,
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004681 .slave_configure = gdth_slave_configure,
Jeff Garzik69916b72007-10-02 22:57:58 +02004682 .bios_param = gdth_bios_param,
4683 .proc_info = gdth_proc_info,
4684 .proc_name = "gdth",
4685 .can_queue = GDTH_MAXCMDS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 .this_id = -1,
4687 .sg_tablesize = GDTH_MAXSG,
4688 .cmd_per_lun = GDTH_MAXC_P_L,
4689 .unchecked_isa_dma = 1,
4690 .use_clustering = ENABLE_CLUSTERING,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691};
4692
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004693#ifdef CONFIG_ISA
Adrian Bunk05dd7ed2007-10-18 12:51:05 +02004694static int __init gdth_isa_probe_one(ulong32 isa_bios)
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004695{
4696 struct Scsi_Host *shp;
4697 gdth_ha_str *ha;
4698 dma_addr_t scratch_dma_handle = 0;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004699 int error, i;
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004700
4701 if (!gdth_search_isa(isa_bios))
4702 return -ENXIO;
4703
Christoph Hellwig835cc242007-10-02 23:09:56 +02004704 shp = scsi_host_alloc(&gdth_template, sizeof(gdth_ha_str));
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004705 if (!shp)
4706 return -ENOMEM;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004707 ha = shost_priv(shp);
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004708
4709 error = -ENODEV;
4710 if (!gdth_init_isa(isa_bios,ha))
4711 goto out_host_put;
4712
4713 /* controller found and initialized */
4714 printk("Configuring GDT-ISA HA at BIOS 0x%05X IRQ %u DRQ %u\n",
4715 isa_bios, ha->irq, ha->drq);
4716
4717 error = request_irq(ha->irq, gdth_interrupt, IRQF_DISABLED, "gdth", ha);
4718 if (error) {
4719 printk("GDT-ISA: Unable to allocate IRQ\n");
4720 goto out_host_put;
4721 }
4722
4723 error = request_dma(ha->drq, "gdth");
4724 if (error) {
4725 printk("GDT-ISA: Unable to allocate DMA channel\n");
4726 goto out_free_irq;
4727 }
4728
4729 set_dma_mode(ha->drq,DMA_MODE_CASCADE);
4730 enable_dma(ha->drq);
4731 shp->unchecked_isa_dma = 1;
4732 shp->irq = ha->irq;
4733 shp->dma_channel = ha->drq;
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004734
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004735 ha->hanum = gdth_ctr_count++;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004736 ha->shost = shp;
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004737
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004738 ha->pccb = &ha->cmdext;
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004739 ha->ccb_phys = 0L;
4740 ha->pdev = NULL;
4741
4742 error = -ENOMEM;
4743
4744 ha->pscratch = pci_alloc_consistent(ha->pdev, GDTH_SCRATCH,
4745 &scratch_dma_handle);
4746 if (!ha->pscratch)
4747 goto out_dec_counters;
4748 ha->scratch_phys = scratch_dma_handle;
4749
4750 ha->pmsg = pci_alloc_consistent(ha->pdev, sizeof(gdth_msg_str),
4751 &scratch_dma_handle);
4752 if (!ha->pmsg)
4753 goto out_free_pscratch;
4754 ha->msg_phys = scratch_dma_handle;
4755
4756#ifdef INT_COAL
4757 ha->coal_stat = pci_alloc_consistent(ha->pdev,
4758 sizeof(gdth_coal_status) * MAXOFFSETS,
4759 &scratch_dma_handle);
4760 if (!ha->coal_stat)
4761 goto out_free_pmsg;
4762 ha->coal_stat_phys = scratch_dma_handle;
4763#endif
4764
4765 ha->scratch_busy = FALSE;
4766 ha->req_first = NULL;
4767 ha->tid_cnt = MAX_HDRIVES;
4768 if (max_ids > 0 && max_ids < ha->tid_cnt)
4769 ha->tid_cnt = max_ids;
4770 for (i = 0; i < GDTH_MAXCMDS; ++i)
4771 ha->cmd_tab[i].cmnd = UNUSED_CMND;
4772 ha->scan_mode = rescan ? 0x10 : 0;
4773
4774 error = -ENODEV;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004775 if (!gdth_search_drives(ha)) {
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004776 printk("GDT-ISA: Error during device scan\n");
4777 goto out_free_coal_stat;
4778 }
4779
4780 if (hdr_channel < 0 || hdr_channel > ha->bus_cnt)
4781 hdr_channel = ha->bus_cnt;
4782 ha->virt_bus = hdr_channel;
4783
4784 if (ha->cache_feat & ha->raw_feat & ha->screen_feat & GDT_64BIT)
4785 shp->max_cmd_len = 16;
4786
4787 shp->max_id = ha->tid_cnt;
4788 shp->max_lun = MAXLUN;
Christoph Hellwig52759e62007-10-02 22:59:53 +02004789 shp->max_channel = ha->bus_cnt;
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004790
4791 spin_lock_init(&ha->smp_lock);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004792 gdth_enable_int(ha);
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004793
Christoph Hellwig835cc242007-10-02 23:09:56 +02004794 error = scsi_add_host(shp, NULL);
4795 if (error)
4796 goto out_free_coal_stat;
4797 list_add_tail(&ha->list, &gdth_instances);
Boaz Harrosh61c92812008-02-12 19:35:22 +02004798
4799 scsi_scan_host(shp);
4800
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004801 return 0;
4802
4803 out_free_coal_stat:
4804#ifdef INT_COAL
4805 pci_free_consistent(ha->pdev, sizeof(gdth_coal_status) * MAXOFFSETS,
4806 ha->coal_stat, ha->coal_stat_phys);
4807 out_free_pmsg:
4808#endif
4809 pci_free_consistent(ha->pdev, sizeof(gdth_msg_str),
4810 ha->pmsg, ha->msg_phys);
4811 out_free_pscratch:
4812 pci_free_consistent(ha->pdev, GDTH_SCRATCH,
4813 ha->pscratch, ha->scratch_phys);
4814 out_dec_counters:
4815 gdth_ctr_count--;
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004816 out_free_irq:
4817 free_irq(ha->irq, ha);
4818 out_host_put:
Christoph Hellwig835cc242007-10-02 23:09:56 +02004819 scsi_host_put(shp);
Christoph Hellwigaed91cb2007-10-02 22:48:16 +02004820 return error;
4821}
4822#endif /* CONFIG_ISA */
4823
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004824#ifdef CONFIG_EISA
Adrian Bunk05dd7ed2007-10-18 12:51:05 +02004825static int __init gdth_eisa_probe_one(ushort eisa_slot)
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004826{
4827 struct Scsi_Host *shp;
4828 gdth_ha_str *ha;
4829 dma_addr_t scratch_dma_handle = 0;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004830 int error, i;
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004831
4832 if (!gdth_search_eisa(eisa_slot))
4833 return -ENXIO;
4834
Christoph Hellwig835cc242007-10-02 23:09:56 +02004835 shp = scsi_host_alloc(&gdth_template, sizeof(gdth_ha_str));
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004836 if (!shp)
4837 return -ENOMEM;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004838 ha = shost_priv(shp);
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004839
4840 error = -ENODEV;
4841 if (!gdth_init_eisa(eisa_slot,ha))
4842 goto out_host_put;
4843
4844 /* controller found and initialized */
4845 printk("Configuring GDT-EISA HA at Slot %d IRQ %u\n",
4846 eisa_slot >> 12, ha->irq);
4847
4848 error = request_irq(ha->irq, gdth_interrupt, IRQF_DISABLED, "gdth", ha);
4849 if (error) {
4850 printk("GDT-EISA: Unable to allocate IRQ\n");
4851 goto out_host_put;
4852 }
4853
4854 shp->unchecked_isa_dma = 0;
4855 shp->irq = ha->irq;
4856 shp->dma_channel = 0xff;
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004857
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004858 ha->hanum = gdth_ctr_count++;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004859 ha->shost = shp;
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004860
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004861 TRACE2(("EISA detect Bus 0: hanum %d\n", ha->hanum));
4862
4863 ha->pccb = &ha->cmdext;
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004864 ha->ccb_phys = 0L;
4865
4866 error = -ENOMEM;
4867
4868 ha->pdev = NULL;
4869 ha->pscratch = pci_alloc_consistent(ha->pdev, GDTH_SCRATCH,
4870 &scratch_dma_handle);
4871 if (!ha->pscratch)
4872 goto out_free_irq;
4873 ha->scratch_phys = scratch_dma_handle;
4874
4875 ha->pmsg = pci_alloc_consistent(ha->pdev, sizeof(gdth_msg_str),
4876 &scratch_dma_handle);
4877 if (!ha->pmsg)
4878 goto out_free_pscratch;
4879 ha->msg_phys = scratch_dma_handle;
4880
4881#ifdef INT_COAL
4882 ha->coal_stat = pci_alloc_consistent(ha->pdev,
4883 sizeof(gdth_coal_status) * MAXOFFSETS,
4884 &scratch_dma_handle);
4885 if (!ha->coal_stat)
4886 goto out_free_pmsg;
4887 ha->coal_stat_phys = scratch_dma_handle;
4888#endif
4889
4890 ha->ccb_phys = pci_map_single(ha->pdev,ha->pccb,
4891 sizeof(gdth_cmd_str), PCI_DMA_BIDIRECTIONAL);
4892 if (!ha->ccb_phys)
4893 goto out_free_coal_stat;
4894
4895 ha->scratch_busy = FALSE;
4896 ha->req_first = NULL;
4897 ha->tid_cnt = MAX_HDRIVES;
4898 if (max_ids > 0 && max_ids < ha->tid_cnt)
4899 ha->tid_cnt = max_ids;
4900 for (i = 0; i < GDTH_MAXCMDS; ++i)
4901 ha->cmd_tab[i].cmnd = UNUSED_CMND;
4902 ha->scan_mode = rescan ? 0x10 : 0;
4903
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004904 if (!gdth_search_drives(ha)) {
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004905 printk("GDT-EISA: Error during device scan\n");
4906 error = -ENODEV;
4907 goto out_free_ccb_phys;
4908 }
4909
4910 if (hdr_channel < 0 || hdr_channel > ha->bus_cnt)
4911 hdr_channel = ha->bus_cnt;
4912 ha->virt_bus = hdr_channel;
4913
4914 if (ha->cache_feat & ha->raw_feat & ha->screen_feat & GDT_64BIT)
4915 shp->max_cmd_len = 16;
4916
4917 shp->max_id = ha->tid_cnt;
4918 shp->max_lun = MAXLUN;
Christoph Hellwig52759e62007-10-02 22:59:53 +02004919 shp->max_channel = ha->bus_cnt;
Christoph Hellwig706a5d42007-10-02 22:49:35 +02004920
4921 spin_lock_init(&ha->smp_lock);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004922 gdth_enable_int(ha);
Christoph Hellwig835cc242007-10-02 23:09:56 +02004923
4924 error = scsi_add_host(shp, NULL);
4925 if (error)
4926 goto out_free_coal_stat;
4927 list_add_tail(&ha->list, &gdth_instances);
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
Adrian Bunk05dd7ed2007-10-18 12:51:05 +02004957static int __init gdth_pci_probe_one(gdth_pci_str *pcistr, int ctr)
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;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004963
Christoph Hellwig835cc242007-10-02 23:09:56 +02004964 shp = scsi_host_alloc(&gdth_template, sizeof(gdth_ha_str));
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004965 if (!shp)
4966 return -ENOMEM;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004967 ha = shost_priv(shp);
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004968
4969 error = -ENODEV;
4970 if (!gdth_init_pci(&pcistr[ctr],ha))
4971 goto out_host_put;
4972
4973 /* controller found and initialized */
4974 printk("Configuring GDT-PCI HA at %d/%d IRQ %u\n",
4975 pcistr[ctr].pdev->bus->number,
4976 PCI_SLOT(pcistr[ctr].pdev->devfn),
4977 ha->irq);
4978
4979 error = request_irq(ha->irq, gdth_interrupt,
4980 IRQF_DISABLED|IRQF_SHARED, "gdth", ha);
4981 if (error) {
4982 printk("GDT-PCI: Unable to allocate IRQ\n");
4983 goto out_host_put;
4984 }
4985
4986 shp->unchecked_isa_dma = 0;
4987 shp->irq = ha->irq;
4988 shp->dma_channel = 0xff;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004989
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004990 ha->hanum = gdth_ctr_count++;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004991 ha->shost = shp;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004992
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004993 ha->pccb = &ha->cmdext;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004994 ha->ccb_phys = 0L;
4995
4996 error = -ENOMEM;
4997
4998 ha->pscratch = pci_alloc_consistent(ha->pdev, GDTH_SCRATCH,
4999 &scratch_dma_handle);
5000 if (!ha->pscratch)
5001 goto out_free_irq;
5002 ha->scratch_phys = scratch_dma_handle;
5003
5004 ha->pmsg = pci_alloc_consistent(ha->pdev, sizeof(gdth_msg_str),
5005 &scratch_dma_handle);
5006 if (!ha->pmsg)
5007 goto out_free_pscratch;
5008 ha->msg_phys = scratch_dma_handle;
5009
5010#ifdef INT_COAL
5011 ha->coal_stat = pci_alloc_consistent(ha->pdev,
5012 sizeof(gdth_coal_status) * MAXOFFSETS,
5013 &scratch_dma_handle);
5014 if (!ha->coal_stat)
5015 goto out_free_pmsg;
5016 ha->coal_stat_phys = scratch_dma_handle;
5017#endif
5018
5019 ha->scratch_busy = FALSE;
5020 ha->req_first = NULL;
5021 ha->tid_cnt = pcistr[ctr].pdev->device >= 0x200 ? MAXID : MAX_HDRIVES;
5022 if (max_ids > 0 && max_ids < ha->tid_cnt)
5023 ha->tid_cnt = max_ids;
5024 for (i = 0; i < GDTH_MAXCMDS; ++i)
5025 ha->cmd_tab[i].cmnd = UNUSED_CMND;
5026 ha->scan_mode = rescan ? 0x10 : 0;
5027
5028 error = -ENODEV;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02005029 if (!gdth_search_drives(ha)) {
5030 printk("GDT-PCI %d: Error during device scan\n", ha->hanum);
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005031 goto out_free_coal_stat;
5032 }
5033
5034 if (hdr_channel < 0 || hdr_channel > ha->bus_cnt)
5035 hdr_channel = ha->bus_cnt;
5036 ha->virt_bus = hdr_channel;
5037
5038 /* 64-bit DMA only supported from FW >= x.43 */
5039 if (!(ha->cache_feat & ha->raw_feat & ha->screen_feat & GDT_64BIT) ||
5040 !ha->dma64_support) {
5041 if (pci_set_dma_mask(pcistr[ctr].pdev, DMA_32BIT_MASK)) {
5042 printk(KERN_WARNING "GDT-PCI %d: "
Boaz Harrosh45f1a412007-10-02 23:05:53 +02005043 "Unable to set 32-bit DMA\n", ha->hanum);
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005044 goto out_free_coal_stat;
5045 }
5046 } else {
5047 shp->max_cmd_len = 16;
5048 if (!pci_set_dma_mask(pcistr[ctr].pdev, DMA_64BIT_MASK)) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02005049 printk("GDT-PCI %d: 64-bit DMA enabled\n", ha->hanum);
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005050 } else if (pci_set_dma_mask(pcistr[ctr].pdev, DMA_32BIT_MASK)) {
5051 printk(KERN_WARNING "GDT-PCI %d: "
Boaz Harrosh45f1a412007-10-02 23:05:53 +02005052 "Unable to set 64/32-bit DMA\n", ha->hanum);
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005053 goto out_free_coal_stat;
5054 }
5055 }
5056
5057 shp->max_id = ha->tid_cnt;
5058 shp->max_lun = MAXLUN;
Christoph Hellwig52759e62007-10-02 22:59:53 +02005059 shp->max_channel = ha->bus_cnt;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005060
5061 spin_lock_init(&ha->smp_lock);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02005062 gdth_enable_int(ha);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005063
5064 error = scsi_add_host(shp, &pcistr[ctr].pdev->dev);
5065 if (error)
5066 goto out_free_coal_stat;
5067 list_add_tail(&ha->list, &gdth_instances);
Boaz Harrosh61c92812008-02-12 19:35:22 +02005068
5069 scsi_scan_host(shp);
5070
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005071 return 0;
5072
5073 out_free_coal_stat:
5074#ifdef INT_COAL
5075 pci_free_consistent(ha->pdev, sizeof(gdth_coal_status) * MAXOFFSETS,
5076 ha->coal_stat, ha->coal_stat_phys);
5077 out_free_pmsg:
5078#endif
5079 pci_free_consistent(ha->pdev, sizeof(gdth_msg_str),
5080 ha->pmsg, ha->msg_phys);
5081 out_free_pscratch:
5082 pci_free_consistent(ha->pdev, GDTH_SCRATCH,
5083 ha->pscratch, ha->scratch_phys);
5084 out_free_irq:
5085 free_irq(ha->irq, ha);
5086 gdth_ctr_count--;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005087 out_host_put:
Christoph Hellwig835cc242007-10-02 23:09:56 +02005088 scsi_host_put(shp);
Christoph Hellwig8514ef22007-10-02 22:51:06 +02005089 return error;
5090}
5091#endif /* CONFIG_PCI */
5092
Christoph Hellwig835cc242007-10-02 23:09:56 +02005093static void gdth_remove_one(gdth_ha_str *ha)
5094{
5095 struct Scsi_Host *shp = ha->shost;
5096
5097 TRACE2(("gdth_remove_one()\n"));
5098
5099 scsi_remove_host(shp);
5100
Boaz Harroshb31ddd32008-02-27 15:27:16 -08005101 gdth_flush(ha);
5102
Christoph Hellwig835cc242007-10-02 23:09:56 +02005103 if (ha->sdev) {
5104 scsi_free_host_dev(ha->sdev);
5105 ha->sdev = NULL;
5106 }
5107
Christoph Hellwig835cc242007-10-02 23:09:56 +02005108 if (shp->irq)
5109 free_irq(shp->irq,ha);
5110
5111#ifdef CONFIG_ISA
5112 if (shp->dma_channel != 0xff)
5113 free_dma(shp->dma_channel);
5114#endif
5115#ifdef INT_COAL
5116 if (ha->coal_stat)
5117 pci_free_consistent(ha->pdev, sizeof(gdth_coal_status) *
5118 MAXOFFSETS, ha->coal_stat, ha->coal_stat_phys);
5119#endif
5120 if (ha->pscratch)
5121 pci_free_consistent(ha->pdev, GDTH_SCRATCH,
5122 ha->pscratch, ha->scratch_phys);
5123 if (ha->pmsg)
5124 pci_free_consistent(ha->pdev, sizeof(gdth_msg_str),
5125 ha->pmsg, ha->msg_phys);
5126 if (ha->ccb_phys)
5127 pci_unmap_single(ha->pdev,ha->ccb_phys,
5128 sizeof(gdth_cmd_str),PCI_DMA_BIDIRECTIONAL);
5129
5130 scsi_host_put(shp);
5131}
5132
Boaz Harroshb31ddd32008-02-27 15:27:16 -08005133static int gdth_halt(struct notifier_block *nb, ulong event, void *buf)
5134{
5135 gdth_ha_str *ha;
5136
5137 TRACE2(("gdth_halt() event %d\n", (int)event));
5138 if (event != SYS_RESTART && event != SYS_HALT && event != SYS_POWER_OFF)
5139 return NOTIFY_DONE;
5140
5141 list_for_each_entry(ha, &gdth_instances, list)
5142 gdth_flush(ha);
5143
5144 return NOTIFY_OK;
5145}
5146
5147static struct notifier_block gdth_notifier = {
5148 gdth_halt, NULL, 0
5149};
5150
Christoph Hellwig835cc242007-10-02 23:09:56 +02005151static int __init gdth_init(void)
5152{
5153 if (disable) {
5154 printk("GDT-HA: Controller driver disabled from"
5155 " command line !\n");
5156 return 0;
5157 }
5158
5159 printk("GDT-HA: Storage RAID Controller Driver. Version: %s\n",
5160 GDTH_VERSION_STR);
5161
5162 /* initializations */
5163 gdth_polling = TRUE;
5164 gdth_clear_events();
5165
5166 /* As default we do not probe for EISA or ISA controllers */
5167 if (probe_eisa_isa) {
5168 /* scanning for controllers, at first: ISA controller */
5169#ifdef CONFIG_ISA
5170 ulong32 isa_bios;
5171 for (isa_bios = 0xc8000UL; isa_bios <= 0xd8000UL;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02005172 isa_bios += 0x8000UL)
Christoph Hellwig835cc242007-10-02 23:09:56 +02005173 gdth_isa_probe_one(isa_bios);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005174#endif
5175#ifdef CONFIG_EISA
5176 {
5177 ushort eisa_slot;
5178 for (eisa_slot = 0x1000; eisa_slot <= 0x8000;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02005179 eisa_slot += 0x1000)
Christoph Hellwig835cc242007-10-02 23:09:56 +02005180 gdth_eisa_probe_one(eisa_slot);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005181 }
5182#endif
5183 }
5184
5185#ifdef CONFIG_PCI
5186 /* scanning for PCI controllers */
5187 {
5188 gdth_pci_str pcistr[MAXHA];
5189 int cnt,ctr;
5190
5191 cnt = gdth_search_pci(pcistr);
5192 printk("GDT-HA: Found %d PCI Storage RAID Controllers\n", cnt);
5193 gdth_sort_pci(pcistr,cnt);
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02005194 for (ctr = 0; ctr < cnt; ++ctr)
Christoph Hellwig835cc242007-10-02 23:09:56 +02005195 gdth_pci_probe_one(pcistr, ctr);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005196 }
5197#endif /* CONFIG_PCI */
5198
5199 TRACE2(("gdth_detect() %d controller detected\n", gdth_ctr_count));
Linus Torvaldsc596cc42007-10-15 12:46:16 -07005200
5201 if (list_empty(&gdth_instances))
5202 return -ENODEV;
5203
Christoph Hellwig835cc242007-10-02 23:09:56 +02005204#ifdef GDTH_STATISTICS
5205 TRACE2(("gdth_detect(): Initializing timer !\n"));
5206 init_timer(&gdth_timer);
5207 gdth_timer.expires = jiffies + HZ;
5208 gdth_timer.data = 0L;
5209 gdth_timer.function = gdth_timeout;
5210 add_timer(&gdth_timer);
5211#endif
5212 major = register_chrdev(0,"gdth", &gdth_fops);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005213 register_reboot_notifier(&gdth_notifier);
5214 gdth_polling = FALSE;
5215 return 0;
5216}
5217
5218static void __exit gdth_exit(void)
5219{
5220 gdth_ha_str *ha;
5221
Boaz Harroshb31ddd32008-02-27 15:27:16 -08005222 unregister_chrdev(major, "gdth");
5223 unregister_reboot_notifier(&gdth_notifier);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005224
5225#ifdef GDTH_STATISTICS
Boaz Harroshb31ddd32008-02-27 15:27:16 -08005226 del_timer_sync(&gdth_timer);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005227#endif
Boaz Harroshb31ddd32008-02-27 15:27:16 -08005228
5229 list_for_each_entry(ha, &gdth_instances, list)
5230 gdth_remove_one(ha);
Christoph Hellwig835cc242007-10-02 23:09:56 +02005231}
5232
5233module_init(gdth_init);
5234module_exit(gdth_exit);
5235
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236#ifndef MODULE
5237__setup("gdth=", option_setup);
5238#endif