blob: 878b9ccc3d6d51e46c69f7cda6158c7138371860 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/************************************************************************
2 * Linux driver for *
Christoph Hellwig31481452018-12-12 08:41:21 +01003 * ICP vortex GmbH: GDT PCI Disk Array Controllers *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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.
Christoph Hellwig31481452018-12-12 08:41:21 +010035 * This includes the PCI SCSI Disk Array Controllers and the
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * PCI Fibre Channel Disk Array Controllers. See gdth.h for a complete
37 * list of all controller types.
38 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * After the optional list of IRQ values, other possible
40 * command line options are:
41 * disable:Y disable driver
42 * disable:N enable driver
43 * reserve_mode:0 reserve no drives for the raw service
44 * reserve_mode:1 reserve all not init., removable drives
45 * reserve_mode:2 reserve all not init. drives
46 * reserve_list:h,b,t,l,h,b,t,l,... reserve particular drive(s) with
47 * h- controller no., b- channel no.,
48 * t- target ID, l- LUN
49 * reverse_scan:Y reverse scan order for PCI controllers
50 * reverse_scan:N scan PCI controllers like BIOS
51 * max_ids:x x - target ID count per channel (1..MAXID)
52 * rescan:Y rescan all channels/IDs
53 * rescan:N use all devices found until now
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * hdr_channel:x x - number of virtual bus for host drives
55 * shared_access:Y disable driver reserve/release protocol to
56 * access a shared resource from several nodes,
Adrian Bunk575c9682006-01-15 02:00:17 +010057 * appropriate controller firmware required
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 * shared_access:N enable driver reserve/release protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * force_dma32:Y use only 32 bit DMA mode
60 * force_dma32:N use 64 bit DMA mode, if supported
61 *
62 * The default values are: "gdth=disable:N,reserve_mode:1,reverse_scan:N,
Christoph Hellwig52759e62007-10-02 22:59:53 +020063 * max_ids:127,rescan:N,hdr_channel:0,
Christoph Hellwig31481452018-12-12 08:41:21 +010064 * shared_access:Y,force_dma32:N".
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 * Here is another example: "gdth=reserve_list:0,1,2,0,0,1,3,0,rescan:Y".
66 *
67 * When loading the gdth driver as a module, the same options are available.
68 * You can set the IRQs with "IRQ=...". However, the syntax to specify the
69 * options changes slightly. You must replace all ',' between options
70 * with ' ' and all ':' with '=' and you must use
71 * '1' in place of 'Y' and '0' in place of 'N'.
72 *
73 * Default: "modprobe gdth disable=0 reserve_mode=1 reverse_scan=0
Christoph Hellwig52759e62007-10-02 22:59:53 +020074 * max_ids=127 rescan=0 hdr_channel=0 shared_access=0
Christoph Hellwig31481452018-12-12 08:41:21 +010075 * force_dma32=0"
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 * The other example: "modprobe gdth reserve_list=0,1,2,0,0,1,3,0 rescan=1".
77 */
78
79/* The meaning of the Scsi_Pointer members in this driver is as follows:
80 * ptr: Chaining
Boaz Harroshd35055a2008-02-27 15:31:20 -080081 * this_residual: unused
82 * buffer: unused
Boaz Harrosh3892d882007-10-02 23:18:03 +020083 * dma_handle: unused
Boaz Harroshd35055a2008-02-27 15:31:20 -080084 * buffers_residual: unused
Boaz Harrosh3892d882007-10-02 23:18:03 +020085 * Status: unused
Boaz Harroshf842b642007-10-02 23:16:01 +020086 * Message: unused
87 * have_data_in: unused
88 * sent_command: unused
89 * phase: unused
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 */
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* statistics */
93#define GDTH_STATISTICS
94
95#include <linux/module.h>
96
97#include <linux/version.h>
98#include <linux/kernel.h>
99#include <linux/types.h>
100#include <linux/pci.h>
101#include <linux/string.h>
102#include <linux/ctype.h>
103#include <linux/ioport.h>
104#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#include <linux/interrupt.h>
106#include <linux/in.h>
107#include <linux/proc_fs.h>
108#include <linux/time.h>
109#include <linux/timer.h>
Matthias Gehre910638a2006-03-28 01:56:48 -0800110#include <linux/dma-mapping.h>
Christoph Hellwig835cc242007-10-02 23:09:56 +0200111#include <linux/list.h>
Arnd Bergmannc45d15d2010-06-02 14:28:52 +0200112#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +0900113#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#include <linux/reboot.h>
115
116#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#include <asm/io.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -0800118#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120#include <linux/blkdev.h>
Boaz Harrosh3892d882007-10-02 23:18:03 +0200121#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123#include "scsi.h"
124#include <scsi/scsi_host.h>
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700125#include "gdth.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Arnd Bergmannc45d15d2010-06-02 14:28:52 +0200127static DEFINE_MUTEX(gdth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128static void gdth_delay(int milliseconds);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500129static void gdth_eval_mapping(u32 size, u32 *cyls, int *heads, int *secs);
David Howells7d12e782006-10-05 14:55:46 +0100130static irqreturn_t gdth_interrupt(int irq, void *dev_id);
Jeff Garzik230e8862007-12-13 16:14:12 -0800131static irqreturn_t __gdth_interrupt(gdth_ha_str *ha,
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +0200132 int gdth_from_wait, int* pIndex);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500133static int gdth_sync_event(gdth_ha_str *ha, int service, u8 index,
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +0200134 struct scsi_cmnd *scp);
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200135static int gdth_async_event(gdth_ha_str *ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static void gdth_log_event(gdth_evt_data *dvr, char *buffer);
137
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +0200138static void gdth_putq(gdth_ha_str *ha, struct scsi_cmnd *scp, u8 priority);
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200139static void gdth_next(gdth_ha_str *ha);
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +0200140static int gdth_fill_raw_cmd(gdth_ha_str *ha, struct scsi_cmnd *scp, u8 b);
141static int gdth_special_cmd(gdth_ha_str *ha, struct scsi_cmnd *scp);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500142static gdth_evt_str *gdth_store_event(gdth_ha_str *ha, u16 source,
143 u16 idx, gdth_evt_data *evt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static int gdth_read_event(gdth_ha_str *ha, int handle, gdth_evt_str *estr);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500145static void gdth_readapp_event(gdth_ha_str *ha, u8 application,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 gdth_evt_str *estr);
147static void gdth_clear_events(void);
148
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +0200149static void gdth_copy_internal_data(gdth_ha_str *ha, struct scsi_cmnd *scp,
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500150 char *buffer, u16 count);
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +0200151static int gdth_internal_cache_cmd(gdth_ha_str *ha, struct scsi_cmnd *scp);
152static int gdth_fill_cache_cmd(gdth_ha_str *ha, struct scsi_cmnd *scp,
153 u16 hdrive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200155static void gdth_enable_int(gdth_ha_str *ha);
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200156static int gdth_test_busy(gdth_ha_str *ha);
157static int gdth_get_cmd_index(gdth_ha_str *ha);
158static void gdth_release_event(gdth_ha_str *ha);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500159static int gdth_wait(gdth_ha_str *ha, int index,u32 time);
160static int gdth_internal_cmd(gdth_ha_str *ha, u8 service, u16 opcode,
161 u32 p1, u64 p2,u64 p3);
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200162static int gdth_search_drives(gdth_ha_str *ha);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500163static int gdth_analyse_hdrive(gdth_ha_str *ha, u16 hdrive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200165static const char *gdth_ctr_name(gdth_ha_str *ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167static int gdth_open(struct inode *inode, struct file *filep);
168static int gdth_close(struct inode *inode, struct file *filep);
Arnd Bergmannf4927c42010-04-27 00:24:01 +0200169static long gdth_unlocked_ioctl(struct file *filep, unsigned int cmd,
170 unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200172static void gdth_flush(gdth_ha_str *ha);
Jeff Garzikf2812332010-11-16 02:10:29 -0500173static int gdth_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200174static int __gdth_queuecommand(gdth_ha_str *ha, struct scsi_cmnd *scp,
175 struct gdth_cmndinfo *cmndinfo);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700176static void gdth_scsi_done(struct scsi_cmnd *scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178#ifdef DEBUG_GDTH
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500179static u8 DebugState = DEBUG_GDTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180#define TRACE(a) {if (DebugState==1) {printk a;}}
181#define TRACE2(a) {if (DebugState==1 || DebugState==2) {printk a;}}
182#define TRACE3(a) {if (DebugState!=0) {printk a;}}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183#else /* !DEBUG */
184#define TRACE(a)
185#define TRACE2(a)
186#define TRACE3(a)
187#endif
188
189#ifdef GDTH_STATISTICS
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500190static u32 max_rq=0, max_index=0, max_sg=0;
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500191static u32 act_ints=0, act_ios=0, act_stats=0, act_rq=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192static struct timer_list gdth_timer;
193#endif
194
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500195#define PTR2USHORT(a) (u16)(unsigned long)(a)
Tobias Klauser6391a112006-06-08 22:23:48 -0700196#define GDTOFFSOF(a,b) (size_t)&(((a*)0)->b)
197#define INDEX_OK(i,t) ((i)<ARRAY_SIZE(t))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199#define BUS_L2P(a,b) ((b)>(a)->virt_bus ? (b-1):(b))
200
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500201static u8 gdth_polling; /* polling if TRUE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202static int gdth_ctr_count = 0; /* controller count */
Boaz Harrosh884f7fb2007-10-02 23:11:24 +0200203static LIST_HEAD(gdth_instances); /* controller list */
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500204static u8 gdth_write_through = FALSE; /* write through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205static gdth_evt_str ebuffer[MAX_EVENTS]; /* event buffer */
206static int elastidx;
207static int eoldidx;
208static int major;
209
210#define DIN 1 /* IN data direction */
211#define DOU 2 /* OUT data direction */
212#define DNO DIN /* no data transfer */
213#define DUN DIN /* unknown data direction */
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500214static u8 gdth_direction_tab[0x100] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 DNO,DNO,DIN,DIN,DOU,DIN,DIN,DOU,DIN,DUN,DOU,DOU,DUN,DUN,DUN,DIN,
216 DNO,DIN,DIN,DOU,DIN,DOU,DNO,DNO,DOU,DNO,DIN,DNO,DIN,DOU,DNO,DUN,
217 DIN,DUN,DIN,DUN,DOU,DIN,DUN,DUN,DIN,DIN,DOU,DNO,DUN,DIN,DOU,DOU,
218 DOU,DOU,DOU,DNO,DIN,DNO,DNO,DIN,DOU,DOU,DOU,DOU,DIN,DOU,DIN,DOU,
219 DOU,DOU,DIN,DIN,DIN,DNO,DUN,DNO,DNO,DNO,DUN,DNO,DOU,DIN,DUN,DUN,
220 DUN,DUN,DUN,DUN,DUN,DOU,DUN,DUN,DUN,DUN,DIN,DUN,DUN,DUN,DUN,DUN,
221 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
222 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
223 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DIN,DUN,DOU,DUN,DUN,DUN,DUN,DUN,
224 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DIN,DUN,
225 DUN,DUN,DUN,DUN,DUN,DNO,DNO,DUN,DIN,DNO,DOU,DUN,DNO,DUN,DOU,DOU,
226 DOU,DOU,DOU,DNO,DUN,DIN,DOU,DIN,DIN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
227 DUN,DUN,DOU,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
228 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
229 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DOU,DUN,DUN,DUN,DUN,DUN,
230 DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN
231};
232
233/* LILO and modprobe/insmod parameters */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234/* disable driver flag */
235static int disable __initdata = 0;
236/* reserve flag */
237static int reserve_mode = 1;
238/* reserve list */
239static int reserve_list[MAX_RES_ARGS] =
240{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
241 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
242 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
243/* scan order for PCI controllers */
244static int reverse_scan = 0;
245/* virtual channel for the host drives */
246static int hdr_channel = 0;
247/* max. IDs per channel */
248static int max_ids = MAXID;
249/* rescan all IDs */
250static int rescan = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/* shared access */
252static int shared_access = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253/* 64 bit DMA mode, support for drives > 2 TB, if force_dma32 = 0 */
254static int force_dma32 = 0;
255
256/* parameters for modprobe/insmod */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257module_param(disable, int, 0);
258module_param(reserve_mode, int, 0);
259module_param_array(reserve_list, int, NULL, 0);
260module_param(reverse_scan, int, 0);
261module_param(hdr_channel, int, 0);
262module_param(max_ids, int, 0);
263module_param(rescan, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264module_param(shared_access, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265module_param(force_dma32, int, 0);
266MODULE_AUTHOR("Achim Leubner");
267MODULE_LICENSE("GPL");
268
269/* ioctl interface */
Arjan van de Ven00977a52007-02-12 00:55:34 -0800270static const struct file_operations gdth_fops = {
Arnd Bergmannf4927c42010-04-27 00:24:01 +0200271 .unlocked_ioctl = gdth_unlocked_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 .open = gdth_open,
273 .release = gdth_close,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200274 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275};
276
277#include "gdth_proc.h"
278#include "gdth_proc.c"
279
Boaz Harrosh884f7fb2007-10-02 23:11:24 +0200280static gdth_ha_str *gdth_find_ha(int hanum)
281{
282 gdth_ha_str *ha;
283
284 list_for_each_entry(ha, &gdth_instances, list)
285 if (hanum == ha->hanum)
286 return ha;
287
288 return NULL;
289}
290
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200291static struct gdth_cmndinfo *gdth_get_cmndinfo(gdth_ha_str *ha)
292{
293 struct gdth_cmndinfo *priv = NULL;
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500294 unsigned long flags;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200295 int i;
296
297 spin_lock_irqsave(&ha->smp_lock, flags);
298
299 for (i=0; i<GDTH_MAXCMDS; ++i) {
300 if (ha->cmndinfo[i].index == 0) {
301 priv = &ha->cmndinfo[i];
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200302 memset(priv, 0, sizeof(*priv));
Boaz Harroshee54cc62008-02-27 15:29:15 -0800303 priv->index = i+1;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200304 break;
305 }
306 }
307
308 spin_unlock_irqrestore(&ha->smp_lock, flags);
309
310 return priv;
311}
312
313static void gdth_put_cmndinfo(struct gdth_cmndinfo *priv)
314{
315 BUG_ON(!priv);
316 priv->index = 0;
317}
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319static void gdth_delay(int milliseconds)
320{
321 if (milliseconds == 0) {
322 udelay(1);
323 } else {
324 mdelay(milliseconds);
325 }
326}
327
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700328static void gdth_scsi_done(struct scsi_cmnd *scp)
329{
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200330 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
331 int internal_command = cmndinfo->internal_command;
332
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +0200333 TRACE2(("gdth_scsi_done()\n"));
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700334
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200335 gdth_put_cmndinfo(cmndinfo);
336 scp->host_scribble = NULL;
337
338 if (internal_command)
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +0200339 complete((struct completion *)scp->request);
340 else
341 scp->scsi_done(scp);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700342}
343
344int __gdth_execute(struct scsi_device *sdev, gdth_cmd_str *gdtcmd, char *cmnd,
345 int timeout, u32 *info)
346{
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200347 gdth_ha_str *ha = shost_priv(sdev->host);
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +0200348 struct scsi_cmnd *scp;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200349 struct gdth_cmndinfo cmndinfo;
Peter Zijlstra6e9a4732006-09-30 23:28:10 -0700350 DECLARE_COMPLETION_ONSTACK(wait);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700351 int rval;
352
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +0200353 scp = kzalloc(sizeof(*scp), GFP_KERNEL);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700354 if (!scp)
355 return -ENOMEM;
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +0200356
Sven Schnelle1b96f892008-03-10 22:50:04 +0100357 scp->sense_buffer = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
358 if (!scp->sense_buffer) {
359 kfree(scp);
360 return -ENOMEM;
361 }
362
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700363 scp->device = sdev;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200364 memset(&cmndinfo, 0, sizeof(cmndinfo));
365
Christoph Hellwigbeb40482006-06-10 18:01:03 +0200366 /* use request field to save the ptr. to completion struct. */
367 scp->request = (struct request *)&wait;
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700368 scp->cmd_len = 12;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300369 scp->cmnd = cmnd;
Boaz Harroshf842b642007-10-02 23:16:01 +0200370 cmndinfo.priority = IOCTL_PRI;
Boaz Harroshee54cc62008-02-27 15:29:15 -0800371 cmndinfo.internal_cmd_str = gdtcmd;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +0200372 cmndinfo.internal_command = 1;
373
374 TRACE(("__gdth_execute() cmd 0x%x\n", scp->cmnd[0]));
375 __gdth_queuecommand(ha, scp, &cmndinfo);
376
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700377 wait_for_completion(&wait);
378
Boaz Harroshf842b642007-10-02 23:16:01 +0200379 rval = cmndinfo.status;
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700380 if (info)
Boaz Harroshf842b642007-10-02 23:16:01 +0200381 *info = cmndinfo.info;
Sven Schnelle1b96f892008-03-10 22:50:04 +0100382 kfree(scp->sense_buffer);
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700383 kfree(scp);
384 return rval;
385}
Leubner, Achimcbd5f692006-06-09 11:34:29 -0700386
387int gdth_execute(struct Scsi_Host *shost, gdth_cmd_str *gdtcmd, char *cmnd,
388 int timeout, u32 *info)
389{
390 struct scsi_device *sdev = scsi_get_host_dev(shost);
391 int rval = __gdth_execute(sdev, gdtcmd, cmnd, timeout, info);
392
393 scsi_free_host_dev(sdev);
394 return rval;
395}
396
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500397static void gdth_eval_mapping(u32 size, u32 *cyls, int *heads, int *secs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 *cyls = size /HEADS/SECS;
400 if (*cyls <= MAXCYLS) {
401 *heads = HEADS;
402 *secs = SECS;
403 } else { /* too high for 64*32 */
404 *cyls = size /MEDHEADS/MEDSECS;
405 if (*cyls <= MAXCYLS) {
406 *heads = MEDHEADS;
407 *secs = MEDSECS;
408 } else { /* too high for 127*63 */
409 *cyls = size /BIGHEADS/BIGSECS;
410 *heads = BIGHEADS;
411 *secs = BIGSECS;
412 }
413 }
414}
415
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500416static bool gdth_search_vortex(u16 device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Jeff Garzikcff26802008-02-15 22:20:09 -0500418 if (device <= PCI_DEVICE_ID_VORTEX_GDT6555)
419 return true;
420 if (device >= PCI_DEVICE_ID_VORTEX_GDT6x17RP &&
421 device <= PCI_DEVICE_ID_VORTEX_GDTMAXRP)
422 return true;
423 if (device == PCI_DEVICE_ID_VORTEX_GDTNEWRX ||
424 device == PCI_DEVICE_ID_VORTEX_GDTNEWRX2)
425 return true;
426 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
Jeff Garzikcff26802008-02-15 22:20:09 -0500429static int gdth_pci_probe_one(gdth_pci_str *pcistr, gdth_ha_str **ha_out);
430static int gdth_pci_init_one(struct pci_dev *pdev,
431 const struct pci_device_id *ent);
432static void gdth_pci_remove_one(struct pci_dev *pdev);
433static void gdth_remove_one(gdth_ha_str *ha);
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435/* Vortex only makes RAID controllers.
436 * We do not really want to specify all 550 ids here, so wildcard match.
437 */
Jeff Garzikcff26802008-02-15 22:20:09 -0500438static const struct pci_device_id gdthtable[] = {
439 { PCI_VDEVICE(VORTEX, PCI_ANY_ID) },
440 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_SRC) },
441 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_SRC_XSCALE) },
442 { } /* terminate list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443};
Jeff Garzikcff26802008-02-15 22:20:09 -0500444MODULE_DEVICE_TABLE(pci, gdthtable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Jeff Garzikcff26802008-02-15 22:20:09 -0500446static struct pci_driver gdth_pci_driver = {
447 .name = "gdth",
448 .id_table = gdthtable,
449 .probe = gdth_pci_init_one,
450 .remove = gdth_pci_remove_one,
451};
452
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800453static void gdth_pci_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Jeff Garzikcff26802008-02-15 22:20:09 -0500455 gdth_ha_str *ha = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Jeff Garzikcff26802008-02-15 22:20:09 -0500457 list_del(&ha->list);
458 gdth_remove_one(ha);
459
460 pci_disable_device(pdev);
461}
462
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800463static int gdth_pci_init_one(struct pci_dev *pdev,
464 const struct pci_device_id *ent)
Jeff Garzikcff26802008-02-15 22:20:09 -0500465{
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500466 u16 vendor = pdev->vendor;
467 u16 device = pdev->device;
468 unsigned long base0, base1, base2;
Jeff Garzikcff26802008-02-15 22:20:09 -0500469 int rc;
470 gdth_pci_str gdth_pcistr;
471 gdth_ha_str *ha = NULL;
472
473 TRACE(("gdth_search_dev() cnt %d vendor %x device %x\n",
474 gdth_ctr_count, vendor, device));
475
476 memset(&gdth_pcistr, 0, sizeof(gdth_pcistr));
477
478 if (vendor == PCI_VENDOR_ID_VORTEX && !gdth_search_vortex(device))
479 return -ENODEV;
480
481 rc = pci_enable_device(pdev);
482 if (rc)
483 return rc;
484
485 if (gdth_ctr_count >= MAXHA)
486 return -EBUSY;
Sergio Luis99109302008-02-12 20:48:03 -0300487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 /* GDT PCI controller found, resources are already in pdev */
Jeff Garzikcff26802008-02-15 22:20:09 -0500489 gdth_pcistr.pdev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 base0 = pci_resource_flags(pdev, 0);
491 base1 = pci_resource_flags(pdev, 1);
492 base2 = pci_resource_flags(pdev, 2);
493 if (device <= PCI_DEVICE_ID_VORTEX_GDT6000B || /* GDT6000/B */
494 device >= PCI_DEVICE_ID_VORTEX_GDT6x17RP) { /* MPR */
495 if (!(base0 & IORESOURCE_MEM))
Jeff Garzikcff26802008-02-15 22:20:09 -0500496 return -ENODEV;
497 gdth_pcistr.dpmem = pci_resource_start(pdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 } else { /* GDT6110, GDT6120, .. */
499 if (!(base0 & IORESOURCE_MEM) ||
500 !(base2 & IORESOURCE_MEM) ||
501 !(base1 & IORESOURCE_IO))
Jeff Garzikcff26802008-02-15 22:20:09 -0500502 return -ENODEV;
503 gdth_pcistr.dpmem = pci_resource_start(pdev, 2);
504 gdth_pcistr.io = pci_resource_start(pdev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506 TRACE2(("Controller found at %d/%d, irq %d, dpmem 0x%lx\n",
Jeff Garzikcff26802008-02-15 22:20:09 -0500507 gdth_pcistr.pdev->bus->number,
508 PCI_SLOT(gdth_pcistr.pdev->devfn),
509 gdth_pcistr.irq,
510 gdth_pcistr.dpmem));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Jeff Garzikcff26802008-02-15 22:20:09 -0500512 rc = gdth_pci_probe_one(&gdth_pcistr, &ha);
513 if (rc)
514 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Jeff Garzikcff26802008-02-15 22:20:09 -0500516 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800519static int gdth_init_pci(struct pci_dev *pdev, gdth_pci_str *pcistr,
520 gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 register gdt6_dpram_str __iomem *dp6_ptr;
523 register gdt6c_dpram_str __iomem *dp6c_ptr;
524 register gdt6m_dpram_str __iomem *dp6m_ptr;
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500525 u32 retries;
526 u8 prot_ver;
527 u16 command;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 int i, found = FALSE;
529
530 TRACE(("gdth_init_pci()\n"));
531
Jeff Garzik4c9c8d72008-02-15 21:35:26 -0500532 if (pdev->vendor == PCI_VENDOR_ID_INTEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 ha->oem_id = OEM_ID_INTEL;
534 else
535 ha->oem_id = OEM_ID_ICP;
Jeff Garzik4c9c8d72008-02-15 21:35:26 -0500536 ha->brd_phys = (pdev->bus->number << 8) | (pdev->devfn & 0xf8);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500537 ha->stype = (u32)pdev->device;
Jeff Garzik4c9c8d72008-02-15 21:35:26 -0500538 ha->irq = pdev->irq;
539 ha->pdev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Jeff Garzik8e9a8a02007-07-17 05:25:17 -0400541 if (ha->pdev->device <= PCI_DEVICE_ID_VORTEX_GDT6000B) { /* GDT6000/B */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 TRACE2(("init_pci() dpmem %lx irq %d\n",pcistr->dpmem,ha->irq));
543 ha->brd = ioremap(pcistr->dpmem, sizeof(gdt6_dpram_str));
544 if (ha->brd == NULL) {
545 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
546 return 0;
547 }
548 /* check and reset interface area */
549 dp6_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +0200550 writel(DPMEM_MAGIC, &dp6_ptr->u);
551 if (readl(&dp6_ptr->u) != DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 printk("GDT-PCI: Cannot access DPMEM at 0x%lx (shadowed?)\n",
553 pcistr->dpmem);
554 found = FALSE;
555 for (i = 0xC8000; i < 0xE8000; i += 0x4000) {
556 iounmap(ha->brd);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500557 ha->brd = ioremap(i, sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (ha->brd == NULL) {
559 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
560 return 0;
561 }
Jeff Garzika52667f2007-10-02 22:55:53 +0200562 if (readw(ha->brd) != 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 TRACE2(("init_pci_old() address 0x%x busy\n", i));
564 continue;
565 }
566 iounmap(ha->brd);
Jeff Garzik4c9c8d72008-02-15 21:35:26 -0500567 pci_write_config_dword(pdev, PCI_BASE_ADDRESS_0, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 ha->brd = ioremap(i, sizeof(gdt6_dpram_str));
569 if (ha->brd == NULL) {
570 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
571 return 0;
572 }
573 dp6_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +0200574 writel(DPMEM_MAGIC, &dp6_ptr->u);
575 if (readl(&dp6_ptr->u) == DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 printk("GDT-PCI: Use free address at 0x%x\n", i);
577 found = TRUE;
578 break;
579 }
580 }
581 if (!found) {
582 printk("GDT-PCI: No free address found!\n");
583 iounmap(ha->brd);
584 return 0;
585 }
586 }
587 memset_io(&dp6_ptr->u, 0, sizeof(dp6_ptr->u));
Jeff Garzika52667f2007-10-02 22:55:53 +0200588 if (readl(&dp6_ptr->u) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 printk("GDT-PCI: Initialization error (DPMEM write error)\n");
590 iounmap(ha->brd);
591 return 0;
592 }
593
594 /* disable board interrupts, deinit services */
Jeff Garzika52667f2007-10-02 22:55:53 +0200595 writeb(0xff, &dp6_ptr->io.irqdel);
596 writeb(0x00, &dp6_ptr->io.irqen);
597 writeb(0x00, &dp6_ptr->u.ic.S_Status);
598 writeb(0x00, &dp6_ptr->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Jeff Garzika52667f2007-10-02 22:55:53 +0200600 writel(pcistr->dpmem, &dp6_ptr->u.ic.S_Info[0]);
601 writeb(0xff, &dp6_ptr->u.ic.S_Cmd_Indx);
602 writeb(0, &dp6_ptr->io.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 retries = INIT_RETRIES;
604 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +0200605 while (readb(&dp6_ptr->u.ic.S_Status) != 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (--retries == 0) {
607 printk("GDT-PCI: Initialization error (DEINIT failed)\n");
608 iounmap(ha->brd);
609 return 0;
610 }
611 gdth_delay(1);
612 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500613 prot_ver = (u8)readl(&dp6_ptr->u.ic.S_Info[0]);
Jeff Garzika52667f2007-10-02 22:55:53 +0200614 writeb(0, &dp6_ptr->u.ic.S_Status);
615 writeb(0xff, &dp6_ptr->io.irqdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (prot_ver != PROTOCOL_VERSION) {
617 printk("GDT-PCI: Illegal protocol version\n");
618 iounmap(ha->brd);
619 return 0;
620 }
621
622 ha->type = GDT_PCI;
623 ha->ic_all_size = sizeof(dp6_ptr->u);
624
625 /* special command to controller BIOS */
Jeff Garzika52667f2007-10-02 22:55:53 +0200626 writel(0x00, &dp6_ptr->u.ic.S_Info[0]);
627 writel(0x00, &dp6_ptr->u.ic.S_Info[1]);
628 writel(0x00, &dp6_ptr->u.ic.S_Info[2]);
629 writel(0x00, &dp6_ptr->u.ic.S_Info[3]);
630 writeb(0xfe, &dp6_ptr->u.ic.S_Cmd_Indx);
631 writeb(0, &dp6_ptr->io.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 retries = INIT_RETRIES;
633 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +0200634 while (readb(&dp6_ptr->u.ic.S_Status) != 0xfe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (--retries == 0) {
636 printk("GDT-PCI: Initialization error\n");
637 iounmap(ha->brd);
638 return 0;
639 }
640 gdth_delay(1);
641 }
Jeff Garzika52667f2007-10-02 22:55:53 +0200642 writeb(0, &dp6_ptr->u.ic.S_Status);
643 writeb(0xff, &dp6_ptr->io.irqdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 ha->dma64_support = 0;
646
Jeff Garzik8e9a8a02007-07-17 05:25:17 -0400647 } else if (ha->pdev->device <= PCI_DEVICE_ID_VORTEX_GDT6555) { /* GDT6110, ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 ha->plx = (gdt6c_plx_regs *)pcistr->io;
649 TRACE2(("init_pci_new() dpmem %lx irq %d\n",
650 pcistr->dpmem,ha->irq));
651 ha->brd = ioremap(pcistr->dpmem, sizeof(gdt6c_dpram_str));
652 if (ha->brd == NULL) {
653 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
654 iounmap(ha->brd);
655 return 0;
656 }
657 /* check and reset interface area */
658 dp6c_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +0200659 writel(DPMEM_MAGIC, &dp6c_ptr->u);
660 if (readl(&dp6c_ptr->u) != DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 printk("GDT-PCI: Cannot access DPMEM at 0x%lx (shadowed?)\n",
662 pcistr->dpmem);
663 found = FALSE;
664 for (i = 0xC8000; i < 0xE8000; i += 0x4000) {
665 iounmap(ha->brd);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500666 ha->brd = ioremap(i, sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (ha->brd == NULL) {
668 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
669 return 0;
670 }
Jeff Garzika52667f2007-10-02 22:55:53 +0200671 if (readw(ha->brd) != 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 TRACE2(("init_pci_plx() address 0x%x busy\n", i));
673 continue;
674 }
675 iounmap(ha->brd);
Jeff Garzik4c9c8d72008-02-15 21:35:26 -0500676 pci_write_config_dword(pdev, PCI_BASE_ADDRESS_2, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 ha->brd = ioremap(i, sizeof(gdt6c_dpram_str));
678 if (ha->brd == NULL) {
679 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
680 return 0;
681 }
682 dp6c_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +0200683 writel(DPMEM_MAGIC, &dp6c_ptr->u);
684 if (readl(&dp6c_ptr->u) == DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 printk("GDT-PCI: Use free address at 0x%x\n", i);
686 found = TRUE;
687 break;
688 }
689 }
690 if (!found) {
691 printk("GDT-PCI: No free address found!\n");
692 iounmap(ha->brd);
693 return 0;
694 }
695 }
696 memset_io(&dp6c_ptr->u, 0, sizeof(dp6c_ptr->u));
Jeff Garzika52667f2007-10-02 22:55:53 +0200697 if (readl(&dp6c_ptr->u) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 printk("GDT-PCI: Initialization error (DPMEM write error)\n");
699 iounmap(ha->brd);
700 return 0;
701 }
702
703 /* disable board interrupts, deinit services */
704 outb(0x00,PTR2USHORT(&ha->plx->control1));
705 outb(0xff,PTR2USHORT(&ha->plx->edoor_reg));
706
Jeff Garzika52667f2007-10-02 22:55:53 +0200707 writeb(0x00, &dp6c_ptr->u.ic.S_Status);
708 writeb(0x00, &dp6c_ptr->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Jeff Garzika52667f2007-10-02 22:55:53 +0200710 writel(pcistr->dpmem, &dp6c_ptr->u.ic.S_Info[0]);
711 writeb(0xff, &dp6c_ptr->u.ic.S_Cmd_Indx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 outb(1,PTR2USHORT(&ha->plx->ldoor_reg));
714
715 retries = INIT_RETRIES;
716 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +0200717 while (readb(&dp6c_ptr->u.ic.S_Status) != 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (--retries == 0) {
719 printk("GDT-PCI: Initialization error (DEINIT failed)\n");
720 iounmap(ha->brd);
721 return 0;
722 }
723 gdth_delay(1);
724 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500725 prot_ver = (u8)readl(&dp6c_ptr->u.ic.S_Info[0]);
Jeff Garzika52667f2007-10-02 22:55:53 +0200726 writeb(0, &dp6c_ptr->u.ic.Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (prot_ver != PROTOCOL_VERSION) {
728 printk("GDT-PCI: Illegal protocol version\n");
729 iounmap(ha->brd);
730 return 0;
731 }
732
733 ha->type = GDT_PCINEW;
734 ha->ic_all_size = sizeof(dp6c_ptr->u);
735
736 /* special command to controller BIOS */
Jeff Garzika52667f2007-10-02 22:55:53 +0200737 writel(0x00, &dp6c_ptr->u.ic.S_Info[0]);
738 writel(0x00, &dp6c_ptr->u.ic.S_Info[1]);
739 writel(0x00, &dp6c_ptr->u.ic.S_Info[2]);
740 writel(0x00, &dp6c_ptr->u.ic.S_Info[3]);
741 writeb(0xfe, &dp6c_ptr->u.ic.S_Cmd_Indx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 outb(1,PTR2USHORT(&ha->plx->ldoor_reg));
744
745 retries = INIT_RETRIES;
746 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +0200747 while (readb(&dp6c_ptr->u.ic.S_Status) != 0xfe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (--retries == 0) {
749 printk("GDT-PCI: Initialization error\n");
750 iounmap(ha->brd);
751 return 0;
752 }
753 gdth_delay(1);
754 }
Jeff Garzika52667f2007-10-02 22:55:53 +0200755 writeb(0, &dp6c_ptr->u.ic.S_Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 ha->dma64_support = 0;
758
759 } else { /* MPR */
760 TRACE2(("init_pci_mpr() dpmem %lx irq %d\n",pcistr->dpmem,ha->irq));
761 ha->brd = ioremap(pcistr->dpmem, sizeof(gdt6m_dpram_str));
762 if (ha->brd == NULL) {
763 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
764 return 0;
765 }
766
767 /* manipulate config. space to enable DPMEM, start RP controller */
Jeff Garzik4c9c8d72008-02-15 21:35:26 -0500768 pci_read_config_word(pdev, PCI_COMMAND, &command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 command |= 6;
Jeff Garzik4c9c8d72008-02-15 21:35:26 -0500770 pci_write_config_word(pdev, PCI_COMMAND, command);
Bjorn Helgaas8108de92012-11-06 15:04:44 -0700771 gdth_delay(1);
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 dp6m_ptr = ha->brd;
774
775 /* Ensure that it is safe to access the non HW portions of DPMEM.
776 * Aditional check needed for Xscale based RAID controllers */
Jeff Garzika52667f2007-10-02 22:55:53 +0200777 while( ((int)readb(&dp6m_ptr->i960r.sema0_reg) ) & 3 )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 gdth_delay(1);
779
780 /* check and reset interface area */
Jeff Garzika52667f2007-10-02 22:55:53 +0200781 writel(DPMEM_MAGIC, &dp6m_ptr->u);
782 if (readl(&dp6m_ptr->u) != DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 printk("GDT-PCI: Cannot access DPMEM at 0x%lx (shadowed?)\n",
784 pcistr->dpmem);
785 found = FALSE;
786 for (i = 0xC8000; i < 0xE8000; i += 0x4000) {
787 iounmap(ha->brd);
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500788 ha->brd = ioremap(i, sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (ha->brd == NULL) {
790 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
791 return 0;
792 }
Jeff Garzika52667f2007-10-02 22:55:53 +0200793 if (readw(ha->brd) != 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 TRACE2(("init_pci_mpr() address 0x%x busy\n", i));
795 continue;
796 }
797 iounmap(ha->brd);
Jeff Garzik4c9c8d72008-02-15 21:35:26 -0500798 pci_write_config_dword(pdev, PCI_BASE_ADDRESS_0, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 ha->brd = ioremap(i, sizeof(gdt6m_dpram_str));
800 if (ha->brd == NULL) {
801 printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
802 return 0;
803 }
804 dp6m_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +0200805 writel(DPMEM_MAGIC, &dp6m_ptr->u);
806 if (readl(&dp6m_ptr->u) == DPMEM_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 printk("GDT-PCI: Use free address at 0x%x\n", i);
808 found = TRUE;
809 break;
810 }
811 }
812 if (!found) {
813 printk("GDT-PCI: No free address found!\n");
814 iounmap(ha->brd);
815 return 0;
816 }
817 }
818 memset_io(&dp6m_ptr->u, 0, sizeof(dp6m_ptr->u));
819
820 /* disable board interrupts, deinit services */
Jeff Garzika52667f2007-10-02 22:55:53 +0200821 writeb(readb(&dp6m_ptr->i960r.edoor_en_reg) | 4,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 &dp6m_ptr->i960r.edoor_en_reg);
Jeff Garzika52667f2007-10-02 22:55:53 +0200823 writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
824 writeb(0x00, &dp6m_ptr->u.ic.S_Status);
825 writeb(0x00, &dp6m_ptr->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Jeff Garzika52667f2007-10-02 22:55:53 +0200827 writel(pcistr->dpmem, &dp6m_ptr->u.ic.S_Info[0]);
828 writeb(0xff, &dp6m_ptr->u.ic.S_Cmd_Indx);
829 writeb(1, &dp6m_ptr->i960r.ldoor_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 retries = INIT_RETRIES;
831 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +0200832 while (readb(&dp6m_ptr->u.ic.S_Status) != 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (--retries == 0) {
834 printk("GDT-PCI: Initialization error (DEINIT failed)\n");
835 iounmap(ha->brd);
836 return 0;
837 }
838 gdth_delay(1);
839 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500840 prot_ver = (u8)readl(&dp6m_ptr->u.ic.S_Info[0]);
Jeff Garzika52667f2007-10-02 22:55:53 +0200841 writeb(0, &dp6m_ptr->u.ic.S_Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (prot_ver != PROTOCOL_VERSION) {
843 printk("GDT-PCI: Illegal protocol version\n");
844 iounmap(ha->brd);
845 return 0;
846 }
847
848 ha->type = GDT_PCIMPR;
849 ha->ic_all_size = sizeof(dp6m_ptr->u);
850
851 /* special command to controller BIOS */
Jeff Garzika52667f2007-10-02 22:55:53 +0200852 writel(0x00, &dp6m_ptr->u.ic.S_Info[0]);
853 writel(0x00, &dp6m_ptr->u.ic.S_Info[1]);
854 writel(0x00, &dp6m_ptr->u.ic.S_Info[2]);
855 writel(0x00, &dp6m_ptr->u.ic.S_Info[3]);
856 writeb(0xfe, &dp6m_ptr->u.ic.S_Cmd_Indx);
857 writeb(1, &dp6m_ptr->i960r.ldoor_reg);
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(&dp6m_ptr->u.ic.S_Status) != 0xfe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (--retries == 0) {
862 printk("GDT-PCI: Initialization error\n");
863 iounmap(ha->brd);
864 return 0;
865 }
866 gdth_delay(1);
867 }
Jeff Garzika52667f2007-10-02 22:55:53 +0200868 writeb(0, &dp6m_ptr->u.ic.S_Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 /* read FW version to detect 64-bit DMA support */
Jeff Garzika52667f2007-10-02 22:55:53 +0200871 writeb(0xfd, &dp6m_ptr->u.ic.S_Cmd_Indx);
872 writeb(1, &dp6m_ptr->i960r.ldoor_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 retries = INIT_RETRIES;
874 gdth_delay(20);
Jeff Garzika52667f2007-10-02 22:55:53 +0200875 while (readb(&dp6m_ptr->u.ic.S_Status) != 0xfd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (--retries == 0) {
877 printk("GDT-PCI: Initialization error (DEINIT failed)\n");
878 iounmap(ha->brd);
879 return 0;
880 }
881 gdth_delay(1);
882 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500883 prot_ver = (u8)(readl(&dp6m_ptr->u.ic.S_Info[0]) >> 16);
Jeff Garzika52667f2007-10-02 22:55:53 +0200884 writeb(0, &dp6m_ptr->u.ic.S_Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (prot_ver < 0x2b) /* FW < x.43: no 64-bit DMA support */
886 ha->dma64_support = 0;
887 else
888 ha->dma64_support = 1;
889 }
890
891 return 1;
892}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894/* controller protocol functions */
895
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800896static void gdth_enable_int(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500898 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 gdt6_dpram_str __iomem *dp6_ptr;
900 gdt6m_dpram_str __iomem *dp6m_ptr;
901
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200902 TRACE(("gdth_enable_int() hanum %d\n",ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 spin_lock_irqsave(&ha->smp_lock, flags);
904
Christoph Hellwig31481452018-12-12 08:41:21 +0100905 if (ha->type == GDT_PCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 dp6_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +0200907 writeb(1, &dp6_ptr->io.irqdel);
908 writeb(0, &dp6_ptr->u.ic.Cmd_Index);
909 writeb(1, &dp6_ptr->io.irqen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 } else if (ha->type == GDT_PCINEW) {
911 outb(0xff, PTR2USHORT(&ha->plx->edoor_reg));
912 outb(0x03, PTR2USHORT(&ha->plx->control1));
913 } else if (ha->type == GDT_PCIMPR) {
914 dp6m_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +0200915 writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
916 writeb(readb(&dp6m_ptr->i960r.edoor_en_reg) & ~4,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 &dp6m_ptr->i960r.edoor_en_reg);
918 }
919 spin_unlock_irqrestore(&ha->smp_lock, flags);
920}
921
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +0200922/* return IStatus if interrupt was from this card else 0 */
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500923static u8 gdth_get_status(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500925 u8 IStatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Jeff Garzik230e8862007-12-13 16:14:12 -0800927 TRACE(("gdth_get_status() irq %d ctr_count %d\n", ha->irq, gdth_ctr_count));
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +0200928
Christoph Hellwig31481452018-12-12 08:41:21 +0100929 if (ha->type == GDT_PCI)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +0200930 IStatus =
Jeff Garzika52667f2007-10-02 22:55:53 +0200931 readb(&((gdt6_dpram_str __iomem *)ha->brd)->u.ic.Cmd_Index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 else if (ha->type == GDT_PCINEW)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +0200933 IStatus = inb(PTR2USHORT(&ha->plx->edoor_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 else if (ha->type == GDT_PCIMPR)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +0200935 IStatus =
Jeff Garzika52667f2007-10-02 22:55:53 +0200936 readb(&((gdt6m_dpram_str __iomem *)ha->brd)->i960r.edoor_reg);
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +0200937
938 return IStatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +0200940
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200941static int gdth_test_busy(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 register int gdtsema0 = 0;
944
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200945 TRACE(("gdth_test_busy() hanum %d\n", ha->hanum));
946
Christoph Hellwig31481452018-12-12 08:41:21 +0100947 if (ha->type == GDT_PCI)
Jeff Garzika52667f2007-10-02 22:55:53 +0200948 gdtsema0 = (int)readb(&((gdt6_dpram_str __iomem *)ha->brd)->u.ic.Sema0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 else if (ha->type == GDT_PCINEW)
950 gdtsema0 = (int)inb(PTR2USHORT(&ha->plx->sema0_reg));
951 else if (ha->type == GDT_PCIMPR)
952 gdtsema0 =
Jeff Garzika52667f2007-10-02 22:55:53 +0200953 (int)readb(&((gdt6m_dpram_str __iomem *)ha->brd)->i960r.sema0_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 return (gdtsema0 & 1);
956}
957
958
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200959static int gdth_get_cmd_index(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 int i;
962
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200963 TRACE(("gdth_get_cmd_index() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 for (i=0; i<GDTH_MAXCMDS; ++i) {
966 if (ha->cmd_tab[i].cmnd == UNUSED_CMND) {
967 ha->cmd_tab[i].cmnd = ha->pccb->RequestBuffer;
968 ha->cmd_tab[i].service = ha->pccb->Service;
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500969 ha->pccb->CommandIndex = (u32)i+2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return (i+2);
971 }
972 }
973 return 0;
974}
975
976
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200977static void gdth_set_sema0(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200979 TRACE(("gdth_set_sema0() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Christoph Hellwig31481452018-12-12 08:41:21 +0100981 if (ha->type == GDT_PCI) {
Jeff Garzika52667f2007-10-02 22:55:53 +0200982 writeb(1, &((gdt6_dpram_str __iomem *)ha->brd)->u.ic.Sema0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 } else if (ha->type == GDT_PCINEW) {
984 outb(1, PTR2USHORT(&ha->plx->sema0_reg));
985 } else if (ha->type == GDT_PCIMPR) {
Jeff Garzika52667f2007-10-02 22:55:53 +0200986 writeb(1, &((gdt6m_dpram_str __iomem *)ha->brd)->i960r.sema0_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
988}
989
990
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200991static void gdth_copy_command(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 register gdth_cmd_str *cmd_ptr;
994 register gdt6m_dpram_str __iomem *dp6m_ptr;
995 register gdt6c_dpram_str __iomem *dp6c_ptr;
996 gdt6_dpram_str __iomem *dp6_ptr;
Dave Jones1fe6dbf2010-01-04 10:19:34 -0500997 u16 cp_count,dp_offset,cmd_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Boaz Harrosh45f1a412007-10-02 23:05:53 +0200999 TRACE(("gdth_copy_command() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 cp_count = ha->cmd_len;
1002 dp_offset= ha->cmd_offs_dpmem;
1003 cmd_no = ha->cmd_cnt;
1004 cmd_ptr = ha->pccb;
1005
1006 ++ha->cmd_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 /* set cpcount dword aligned */
1009 if (cp_count & 3)
1010 cp_count += (4 - (cp_count & 3));
1011
1012 ha->cmd_offs_dpmem += cp_count;
1013
1014 /* set offset and service, copy command to DPMEM */
Christoph Hellwig31481452018-12-12 08:41:21 +01001015 if (ha->type == GDT_PCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 dp6_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001017 writew(dp_offset + DPMEM_COMMAND_OFFSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 &dp6_ptr->u.ic.comm_queue[cmd_no].offset);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001019 writew((u16)cmd_ptr->Service,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 &dp6_ptr->u.ic.comm_queue[cmd_no].serv_id);
1021 memcpy_toio(&dp6_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
1022 } else if (ha->type == GDT_PCINEW) {
1023 dp6c_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001024 writew(dp_offset + DPMEM_COMMAND_OFFSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 &dp6c_ptr->u.ic.comm_queue[cmd_no].offset);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001026 writew((u16)cmd_ptr->Service,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 &dp6c_ptr->u.ic.comm_queue[cmd_no].serv_id);
1028 memcpy_toio(&dp6c_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
1029 } else if (ha->type == GDT_PCIMPR) {
1030 dp6m_ptr = ha->brd;
Jeff Garzika52667f2007-10-02 22:55:53 +02001031 writew(dp_offset + DPMEM_COMMAND_OFFSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 &dp6m_ptr->u.ic.comm_queue[cmd_no].offset);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001033 writew((u16)cmd_ptr->Service,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 &dp6m_ptr->u.ic.comm_queue[cmd_no].serv_id);
1035 memcpy_toio(&dp6m_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
1036 }
1037}
1038
1039
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001040static void gdth_release_event(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001042 TRACE(("gdth_release_event() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044#ifdef GDTH_STATISTICS
1045 {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001046 u32 i,j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 for (i=0,j=0; j<GDTH_MAXCMDS; ++j) {
1048 if (ha->cmd_tab[j].cmnd != UNUSED_CMND)
1049 ++i;
1050 }
1051 if (max_index < i) {
1052 max_index = i;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001053 TRACE3(("GDT: max_index = %d\n",(u16)i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
1055 }
1056#endif
1057
1058 if (ha->pccb->OpCode == GDT_INIT)
1059 ha->pccb->Service |= 0x80;
1060
Christoph Hellwig31481452018-12-12 08:41:21 +01001061 if (ha->type == GDT_PCI) {
Jeff Garzika52667f2007-10-02 22:55:53 +02001062 writeb(0, &((gdt6_dpram_str __iomem *)ha->brd)->io.event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 } else if (ha->type == GDT_PCINEW) {
1064 outb(1, PTR2USHORT(&ha->plx->ldoor_reg));
1065 } else if (ha->type == GDT_PCIMPR) {
Jeff Garzika52667f2007-10-02 22:55:53 +02001066 writeb(1, &((gdt6m_dpram_str __iomem *)ha->brd)->i960r.ldoor_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068}
1069
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001070static int gdth_wait(gdth_ha_str *ha, int index, u32 time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 int answer_found = FALSE;
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001073 int wait_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001075 TRACE(("gdth_wait() hanum %d index %d time %d\n", ha->hanum, index, time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (index == 0)
1078 return 1; /* no wait required */
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 do {
Jeff Garzik230e8862007-12-13 16:14:12 -08001081 __gdth_interrupt(ha, true, &wait_index);
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02001082 if (wait_index == index) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 answer_found = TRUE;
1084 break;
1085 }
1086 gdth_delay(1);
1087 } while (--time);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001088
1089 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 gdth_delay(0);
1091
1092 return (answer_found);
1093}
1094
1095
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001096static int gdth_internal_cmd(gdth_ha_str *ha, u8 service, u16 opcode,
1097 u32 p1, u64 p2, u64 p3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 register gdth_cmd_str *cmd_ptr;
1100 int retries,index;
1101
1102 TRACE2(("gdth_internal_cmd() service %d opcode %d\n",service,opcode));
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 cmd_ptr = ha->pccb;
1105 memset((char*)cmd_ptr,0,sizeof(gdth_cmd_str));
1106
1107 /* make command */
1108 for (retries = INIT_RETRIES;;) {
1109 cmd_ptr->Service = service;
1110 cmd_ptr->RequestBuffer = INTERNAL_CMND;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001111 if (!(index=gdth_get_cmd_index(ha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 TRACE(("GDT: No free command index found\n"));
1113 return 0;
1114 }
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001115 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 cmd_ptr->OpCode = opcode;
1117 cmd_ptr->BoardNode = LOCALBOARD;
1118 if (service == CACHESERVICE) {
1119 if (opcode == GDT_IOCTL) {
1120 cmd_ptr->u.ioctl.subfunc = p1;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001121 cmd_ptr->u.ioctl.channel = (u32)p2;
1122 cmd_ptr->u.ioctl.param_size = (u16)p3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 cmd_ptr->u.ioctl.p_param = ha->scratch_phys;
1124 } else {
1125 if (ha->cache_feat & GDT_64BIT) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001126 cmd_ptr->u.cache64.DeviceNo = (u16)p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 cmd_ptr->u.cache64.BlockNo = p2;
1128 } else {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001129 cmd_ptr->u.cache.DeviceNo = (u16)p1;
1130 cmd_ptr->u.cache.BlockNo = (u32)p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
1132 }
1133 } else if (service == SCSIRAWSERVICE) {
1134 if (ha->raw_feat & GDT_64BIT) {
1135 cmd_ptr->u.raw64.direction = p1;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001136 cmd_ptr->u.raw64.bus = (u8)p2;
1137 cmd_ptr->u.raw64.target = (u8)p3;
1138 cmd_ptr->u.raw64.lun = (u8)(p3 >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 } else {
1140 cmd_ptr->u.raw.direction = p1;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001141 cmd_ptr->u.raw.bus = (u8)p2;
1142 cmd_ptr->u.raw.target = (u8)p3;
1143 cmd_ptr->u.raw.lun = (u8)(p3 >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
1145 } else if (service == SCREENSERVICE) {
1146 if (opcode == GDT_REALTIME) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001147 *(u32 *)&cmd_ptr->u.screen.su.data[0] = p1;
1148 *(u32 *)&cmd_ptr->u.screen.su.data[4] = (u32)p2;
1149 *(u32 *)&cmd_ptr->u.screen.su.data[8] = (u32)p3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
1151 }
1152 ha->cmd_len = sizeof(gdth_cmd_str);
1153 ha->cmd_offs_dpmem = 0;
1154 ha->cmd_cnt = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001155 gdth_copy_command(ha);
1156 gdth_release_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 gdth_delay(20);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001158 if (!gdth_wait(ha, index, INIT_TIMEOUT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 printk("GDT: Initialization error (timeout service %d)\n",service);
1160 return 0;
1161 }
1162 if (ha->status != S_BSY || --retries == 0)
1163 break;
1164 gdth_delay(1);
1165 }
1166
1167 return (ha->status != S_OK ? 0:1);
1168}
1169
1170
1171/* search for devices */
1172
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08001173static int gdth_search_drives(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001175 u16 cdev_cnt, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 int ok;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001177 u32 bus_no, drv_cnt, drv_no, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 gdth_getch_str *chn;
1179 gdth_drlist_str *drl;
1180 gdth_iochan_str *ioc;
1181 gdth_raw_iochan_str *iocr;
1182 gdth_arcdl_str *alst;
1183 gdth_alist_str *alst2;
1184 gdth_oem_str_ioctl *oemstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001186 TRACE(("gdth_search_drives() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 ok = 0;
1188
1189 /* initialize controller services, at first: screen service */
1190 ha->screen_feat = 0;
1191 if (!force_dma32) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001192 ok = gdth_internal_cmd(ha, SCREENSERVICE, GDT_X_INIT_SCR, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 if (ok)
1194 ha->screen_feat = GDT_64BIT;
1195 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001196 if (force_dma32 || (!ok && ha->status == (u16)S_NOFUNC))
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001197 ok = gdth_internal_cmd(ha, SCREENSERVICE, GDT_INIT, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 if (!ok) {
1199 printk("GDT-HA %d: Initialization error screen service (code %d)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001200 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return 0;
1202 }
1203 TRACE2(("gdth_search_drives(): SCREENSERVICE initialized\n"));
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 /* unfreeze all IOs */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001206 gdth_internal_cmd(ha, CACHESERVICE, GDT_UNFREEZE_IO, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 /* initialize cache service */
1209 ha->cache_feat = 0;
1210 if (!force_dma32) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001211 ok = gdth_internal_cmd(ha, CACHESERVICE, GDT_X_INIT_HOST, LINUX_OS,
1212 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 if (ok)
1214 ha->cache_feat = GDT_64BIT;
1215 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001216 if (force_dma32 || (!ok && ha->status == (u16)S_NOFUNC))
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001217 ok = gdth_internal_cmd(ha, CACHESERVICE, GDT_INIT, LINUX_OS, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 if (!ok) {
1219 printk("GDT-HA %d: Initialization error cache service (code %d)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001220 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return 0;
1222 }
1223 TRACE2(("gdth_search_drives(): CACHESERVICE initialized\n"));
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001224 cdev_cnt = (u16)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 ha->fw_vers = ha->service;
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 /* detect number of buses - try new IOCTL */
1228 iocr = (gdth_raw_iochan_str *)ha->pscratch;
1229 iocr->hdr.version = 0xffffffff;
1230 iocr->hdr.list_entries = MAXBUS;
1231 iocr->hdr.first_chan = 0;
1232 iocr->hdr.last_chan = MAXBUS-1;
1233 iocr->hdr.list_offset = GDTOFFSOF(gdth_raw_iochan_str, list[0]);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001234 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, IOCHAN_RAW_DESC,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 INVALID_CHANNEL,sizeof(gdth_raw_iochan_str))) {
1236 TRACE2(("IOCHAN_RAW_DESC supported!\n"));
1237 ha->bus_cnt = iocr->hdr.chan_count;
1238 for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
1239 if (iocr->list[bus_no].proc_id < MAXID)
1240 ha->bus_id[bus_no] = iocr->list[bus_no].proc_id;
1241 else
1242 ha->bus_id[bus_no] = 0xff;
1243 }
1244 } else {
1245 /* old method */
1246 chn = (gdth_getch_str *)ha->pscratch;
1247 for (bus_no = 0; bus_no < MAXBUS; ++bus_no) {
1248 chn->channel_no = bus_no;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001249 if (!gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 SCSI_CHAN_CNT | L_CTRL_PATTERN,
1251 IO_CHANNEL | INVALID_CHANNEL,
1252 sizeof(gdth_getch_str))) {
1253 if (bus_no == 0) {
1254 printk("GDT-HA %d: Error detecting channel count (0x%x)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001255 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return 0;
1257 }
1258 break;
1259 }
1260 if (chn->siop_id < MAXID)
1261 ha->bus_id[bus_no] = chn->siop_id;
1262 else
1263 ha->bus_id[bus_no] = 0xff;
1264 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001265 ha->bus_cnt = (u8)bus_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
1267 TRACE2(("gdth_search_drives() %d channels\n",ha->bus_cnt));
1268
1269 /* read cache configuration */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001270 if (!gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, CACHE_INFO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 INVALID_CHANNEL,sizeof(gdth_cinfo_str))) {
1272 printk("GDT-HA %d: Initialization error cache service (code %d)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001273 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return 0;
1275 }
1276 ha->cpar = ((gdth_cinfo_str *)ha->pscratch)->cpar;
1277 TRACE2(("gdth_search_drives() cinfo: vs %x sta %d str %d dw %d b %d\n",
1278 ha->cpar.version,ha->cpar.state,ha->cpar.strategy,
1279 ha->cpar.write_back,ha->cpar.block_size));
1280
1281 /* read board info and features */
1282 ha->more_proc = FALSE;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001283 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, BOARD_INFO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 INVALID_CHANNEL,sizeof(gdth_binfo_str))) {
1285 memcpy(&ha->binfo, (gdth_binfo_str *)ha->pscratch,
1286 sizeof(gdth_binfo_str));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001287 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, BOARD_FEATURES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 INVALID_CHANNEL,sizeof(gdth_bfeat_str))) {
1289 TRACE2(("BOARD_INFO/BOARD_FEATURES supported\n"));
1290 ha->bfeat = *(gdth_bfeat_str *)ha->pscratch;
1291 ha->more_proc = TRUE;
1292 }
1293 } else {
1294 TRACE2(("BOARD_INFO requires firmware >= 1.10/2.08\n"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001295 strcpy(ha->binfo.type_string, gdth_ctr_name(ha));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
1297 TRACE2(("Controller name: %s\n",ha->binfo.type_string));
1298
1299 /* read more informations */
1300 if (ha->more_proc) {
1301 /* physical drives, channel addresses */
1302 ioc = (gdth_iochan_str *)ha->pscratch;
1303 ioc->hdr.version = 0xffffffff;
1304 ioc->hdr.list_entries = MAXBUS;
1305 ioc->hdr.first_chan = 0;
1306 ioc->hdr.last_chan = MAXBUS-1;
1307 ioc->hdr.list_offset = GDTOFFSOF(gdth_iochan_str, list[0]);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001308 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, IOCHAN_DESC,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 INVALID_CHANNEL,sizeof(gdth_iochan_str))) {
1310 for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
1311 ha->raw[bus_no].address = ioc->list[bus_no].address;
1312 ha->raw[bus_no].local_no = ioc->list[bus_no].local_no;
1313 }
1314 } else {
1315 for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
1316 ha->raw[bus_no].address = IO_CHANNEL;
1317 ha->raw[bus_no].local_no = bus_no;
1318 }
1319 }
1320 for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
1321 chn = (gdth_getch_str *)ha->pscratch;
1322 chn->channel_no = ha->raw[bus_no].local_no;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001323 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 SCSI_CHAN_CNT | L_CTRL_PATTERN,
1325 ha->raw[bus_no].address | INVALID_CHANNEL,
1326 sizeof(gdth_getch_str))) {
1327 ha->raw[bus_no].pdev_cnt = chn->drive_cnt;
1328 TRACE2(("Channel %d: %d phys. drives\n",
1329 bus_no,chn->drive_cnt));
1330 }
1331 if (ha->raw[bus_no].pdev_cnt > 0) {
1332 drl = (gdth_drlist_str *)ha->pscratch;
1333 drl->sc_no = ha->raw[bus_no].local_no;
1334 drl->sc_cnt = ha->raw[bus_no].pdev_cnt;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001335 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 SCSI_DR_LIST | L_CTRL_PATTERN,
1337 ha->raw[bus_no].address | INVALID_CHANNEL,
1338 sizeof(gdth_drlist_str))) {
1339 for (j = 0; j < ha->raw[bus_no].pdev_cnt; ++j)
1340 ha->raw[bus_no].id_list[j] = drl->sc_list[j];
1341 } else {
1342 ha->raw[bus_no].pdev_cnt = 0;
1343 }
1344 }
1345 }
1346
1347 /* logical drives */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001348 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, CACHE_DRV_CNT,
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001349 INVALID_CHANNEL,sizeof(u32))) {
1350 drv_cnt = *(u32 *)ha->pscratch;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001351 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL, CACHE_DRV_LIST,
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001352 INVALID_CHANNEL,drv_cnt * sizeof(u32))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 for (j = 0; j < drv_cnt; ++j) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001354 drv_no = ((u32 *)ha->pscratch)[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 if (drv_no < MAX_LDRIVES) {
1356 ha->hdr[drv_no].is_logdrv = TRUE;
1357 TRACE2(("Drive %d is log. drive\n",drv_no));
1358 }
1359 }
1360 }
1361 alst = (gdth_arcdl_str *)ha->pscratch;
1362 alst->entries_avail = MAX_LDRIVES;
1363 alst->first_entry = 0;
1364 alst->list_offset = GDTOFFSOF(gdth_arcdl_str, list[0]);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001365 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 ARRAY_DRV_LIST2 | LA_CTRL_PATTERN,
1367 INVALID_CHANNEL, sizeof(gdth_arcdl_str) +
1368 (alst->entries_avail-1) * sizeof(gdth_alist_str))) {
1369 for (j = 0; j < alst->entries_init; ++j) {
1370 ha->hdr[j].is_arraydrv = alst->list[j].is_arrayd;
1371 ha->hdr[j].is_master = alst->list[j].is_master;
1372 ha->hdr[j].is_parity = alst->list[j].is_parity;
1373 ha->hdr[j].is_hotfix = alst->list[j].is_hotfix;
1374 ha->hdr[j].master_no = alst->list[j].cd_handle;
1375 }
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001376 } else if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 ARRAY_DRV_LIST | LA_CTRL_PATTERN,
1378 0, 35 * sizeof(gdth_alist_str))) {
1379 for (j = 0; j < 35; ++j) {
1380 alst2 = &((gdth_alist_str *)ha->pscratch)[j];
1381 ha->hdr[j].is_arraydrv = alst2->is_arrayd;
1382 ha->hdr[j].is_master = alst2->is_master;
1383 ha->hdr[j].is_parity = alst2->is_parity;
1384 ha->hdr[j].is_hotfix = alst2->is_hotfix;
1385 ha->hdr[j].master_no = alst2->cd_handle;
1386 }
1387 }
1388 }
1389 }
1390
1391 /* initialize raw service */
1392 ha->raw_feat = 0;
1393 if (!force_dma32) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001394 ok = gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_X_INIT_RAW, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 if (ok)
1396 ha->raw_feat = GDT_64BIT;
1397 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001398 if (force_dma32 || (!ok && ha->status == (u16)S_NOFUNC))
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001399 ok = gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_INIT, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 if (!ok) {
1401 printk("GDT-HA %d: Initialization error raw service (code %d)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001402 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 return 0;
1404 }
1405 TRACE2(("gdth_search_drives(): RAWSERVICE initialized\n"));
1406
1407 /* set/get features raw service (scatter/gather) */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001408 if (gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_SET_FEAT, SCATTER_GATHER,
1409 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 TRACE2(("gdth_search_drives(): set features RAWSERVICE OK\n"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001411 if (gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_GET_FEAT, 0, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 TRACE2(("gdth_search_dr(): get feat RAWSERVICE %d\n",
1413 ha->info));
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001414 ha->raw_feat |= (u16)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
1416 }
1417
1418 /* set/get features cache service (equal to raw service) */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001419 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_SET_FEAT, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 SCATTER_GATHER,0)) {
1421 TRACE2(("gdth_search_drives(): set features CACHESERVICE OK\n"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001422 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_GET_FEAT, 0, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 TRACE2(("gdth_search_dr(): get feat CACHESERV. %d\n",
1424 ha->info));
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001425 ha->cache_feat |= (u16)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
1427 }
1428
1429 /* reserve drives for raw service */
1430 if (reserve_mode != 0) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001431 gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_RESERVE_ALL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 reserve_mode == 1 ? 1 : 3, 0, 0);
1433 TRACE2(("gdth_search_drives(): RESERVE_ALL code %d\n",
1434 ha->status));
1435 }
1436 for (i = 0; i < MAX_RES_ARGS; i += 4) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001437 if (reserve_list[i] == ha->hanum && reserve_list[i+1] < ha->bus_cnt &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 reserve_list[i+2] < ha->tid_cnt && reserve_list[i+3] < MAXLUN) {
1439 TRACE2(("gdth_search_drives(): reserve ha %d bus %d id %d lun %d\n",
1440 reserve_list[i], reserve_list[i+1],
1441 reserve_list[i+2], reserve_list[i+3]));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001442 if (!gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_RESERVE, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 reserve_list[i+1], reserve_list[i+2] |
1444 (reserve_list[i+3] << 8))) {
1445 printk("GDT-HA %d: Error raw service (RESERVE, code %d)\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001446 ha->hanum, ha->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 }
1448 }
1449 }
1450
1451 /* Determine OEM string using IOCTL */
1452 oemstr = (gdth_oem_str_ioctl *)ha->pscratch;
1453 oemstr->params.ctl_version = 0x01;
1454 oemstr->params.buffer_size = sizeof(oemstr->text);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001455 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_IOCTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 CACHE_READ_OEM_STRING_RECORD,INVALID_CHANNEL,
1457 sizeof(gdth_oem_str_ioctl))) {
1458 TRACE2(("gdth_search_drives(): CACHE_READ_OEM_STRING_RECORD OK\n"));
1459 printk("GDT-HA %d: Vendor: %s Name: %s\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001460 ha->hanum, oemstr->text.oem_company_name, ha->binfo.type_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 /* Save the Host Drive inquiry data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 strlcpy(ha->oem_name,oemstr->text.scsi_host_drive_inquiry_vendor_id,
1463 sizeof(ha->oem_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 } else {
1465 /* Old method, based on PCI ID */
1466 TRACE2(("gdth_search_drives(): CACHE_READ_OEM_STRING_RECORD failed\n"));
1467 printk("GDT-HA %d: Name: %s\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001468 ha->hanum, ha->binfo.type_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 if (ha->oem_id == OEM_ID_INTEL)
1470 strlcpy(ha->oem_name,"Intel ", sizeof(ha->oem_name));
1471 else
1472 strlcpy(ha->oem_name,"ICP ", sizeof(ha->oem_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 }
1474
1475 /* scanning for host drives */
1476 for (i = 0; i < cdev_cnt; ++i)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001477 gdth_analyse_hdrive(ha, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 TRACE(("gdth_search_drives() OK\n"));
1480 return 1;
1481}
1482
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001483static int gdth_analyse_hdrive(gdth_ha_str *ha, u16 hdrive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001485 u32 drv_cyls;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 int drv_hds, drv_secs;
1487
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001488 TRACE(("gdth_analyse_hdrive() hanum %d drive %d\n", ha->hanum, hdrive));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 if (hdrive >= MAX_HDRIVES)
1490 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001492 if (!gdth_internal_cmd(ha, CACHESERVICE, GDT_INFO, hdrive, 0, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return 0;
1494 ha->hdr[hdrive].present = TRUE;
1495 ha->hdr[hdrive].size = ha->info;
1496
1497 /* evaluate mapping (sectors per head, heads per cylinder) */
1498 ha->hdr[hdrive].size &= ~SECS32;
1499 if (ha->info2 == 0) {
1500 gdth_eval_mapping(ha->hdr[hdrive].size,&drv_cyls,&drv_hds,&drv_secs);
1501 } else {
1502 drv_hds = ha->info2 & 0xff;
1503 drv_secs = (ha->info2 >> 8) & 0xff;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001504 drv_cyls = (u32)ha->hdr[hdrive].size / drv_hds / drv_secs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001506 ha->hdr[hdrive].heads = (u8)drv_hds;
1507 ha->hdr[hdrive].secs = (u8)drv_secs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 /* round size */
1509 ha->hdr[hdrive].size = drv_cyls * drv_hds * drv_secs;
1510
1511 if (ha->cache_feat & GDT_64BIT) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001512 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_X_INFO, hdrive, 0, 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 && ha->info2 != 0) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001514 ha->hdr[hdrive].size = ((u64)ha->info2 << 32) | ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 }
1516 }
1517 TRACE2(("gdth_search_dr() cdr. %d size %d hds %d scs %d\n",
1518 hdrive,ha->hdr[hdrive].size,drv_hds,drv_secs));
1519
1520 /* get informations about device */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001521 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_DEVTYPE, hdrive, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 TRACE2(("gdth_search_dr() cache drive %d devtype %d\n",
1523 hdrive,ha->info));
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001524 ha->hdr[hdrive].devtype = (u16)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 }
1526
1527 /* cluster info */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001528 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_CLUST_INFO, hdrive, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 TRACE2(("gdth_search_dr() cache drive %d cluster info %d\n",
1530 hdrive,ha->info));
1531 if (!shared_access)
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001532 ha->hdr[hdrive].cluster_type = (u8)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
1534
1535 /* R/W attributes */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001536 if (gdth_internal_cmd(ha, CACHESERVICE, GDT_RW_ATTRIBS, hdrive, 0, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 TRACE2(("gdth_search_dr() cache drive %d r/w attrib. %d\n",
1538 hdrive,ha->info));
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001539 ha->hdr[hdrive].rw_attribs = (u8)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 }
1541
1542 return 1;
1543}
1544
1545
1546/* command queueing/sending functions */
1547
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02001548static void gdth_putq(gdth_ha_str *ha, struct scsi_cmnd *scp, u8 priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02001550 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02001551 register struct scsi_cmnd *pscp;
1552 register struct scsi_cmnd *nscp;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001553 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555 TRACE(("gdth_putq() priority %d\n",priority));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 spin_lock_irqsave(&ha->smp_lock, flags);
1557
Jens Axboe242f9dc2008-09-14 05:55:09 -07001558 if (!cmndinfo->internal_command)
Boaz Harroshf842b642007-10-02 23:16:01 +02001559 cmndinfo->priority = priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 if (ha->req_first==NULL) {
1562 ha->req_first = scp; /* queue was empty */
1563 scp->SCp.ptr = NULL;
1564 } else { /* queue not empty */
1565 pscp = ha->req_first;
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02001566 nscp = (struct scsi_cmnd *)pscp->SCp.ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 /* priority: 0-highest,..,0xff-lowest */
Boaz Harroshf842b642007-10-02 23:16:01 +02001568 while (nscp && gdth_cmnd_priv(nscp)->priority <= priority) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 pscp = nscp;
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02001570 nscp = (struct scsi_cmnd *)pscp->SCp.ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
1572 pscp->SCp.ptr = (char *)scp;
1573 scp->SCp.ptr = (char *)nscp;
1574 }
1575 spin_unlock_irqrestore(&ha->smp_lock, flags);
1576
1577#ifdef GDTH_STATISTICS
1578 flags = 0;
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02001579 for (nscp=ha->req_first; nscp; nscp=(struct scsi_cmnd*)nscp->SCp.ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 ++flags;
1581 if (max_rq < flags) {
1582 max_rq = flags;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001583 TRACE3(("GDT: max_rq = %d\n",(u16)max_rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
1585#endif
1586}
1587
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001588static void gdth_next(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02001590 register struct scsi_cmnd *pscp;
1591 register struct scsi_cmnd *nscp;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001592 u8 b, t, l, firsttime;
1593 u8 this_cmd, next_cmd;
1594 unsigned long flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 int cmd_index;
1596
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001597 TRACE(("gdth_next() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 if (!gdth_polling)
1599 spin_lock_irqsave(&ha->smp_lock, flags);
1600
1601 ha->cmd_cnt = ha->cmd_offs_dpmem = 0;
1602 this_cmd = firsttime = TRUE;
1603 next_cmd = gdth_polling ? FALSE:TRUE;
1604 cmd_index = 0;
1605
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02001606 for (nscp = pscp = ha->req_first; nscp; nscp = (struct scsi_cmnd *)nscp->SCp.ptr) {
Boaz Harroshf842b642007-10-02 23:16:01 +02001607 struct gdth_cmndinfo *nscp_cmndinfo = gdth_cmnd_priv(nscp);
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02001608 if (nscp != pscp && nscp != (struct scsi_cmnd *)pscp->SCp.ptr)
1609 pscp = (struct scsi_cmnd *)pscp->SCp.ptr;
Boaz Harroshf842b642007-10-02 23:16:01 +02001610 if (!nscp_cmndinfo->internal_command) {
Christoph Hellwig52759e62007-10-02 22:59:53 +02001611 b = nscp->device->channel;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07001612 t = nscp->device->id;
1613 l = nscp->device->lun;
Boaz Harroshf842b642007-10-02 23:16:01 +02001614 if (nscp_cmndinfo->priority >= DEFAULT_PRI) {
Leubner, Achimcbd5f692006-06-09 11:34:29 -07001615 if ((b != ha->virt_bus && ha->raw[BUS_L2P(ha,b)].lock) ||
1616 (b == ha->virt_bus && t < MAX_HDRIVES && ha->hdr[t].lock))
1617 continue;
1618 }
1619 } else
1620 b = t = l = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 if (firsttime) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001623 if (gdth_test_busy(ha)) { /* controller busy ? */
1624 TRACE(("gdth_next() controller %d busy !\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 if (!gdth_polling) {
1626 spin_unlock_irqrestore(&ha->smp_lock, flags);
1627 return;
1628 }
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001629 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 gdth_delay(1);
1631 }
1632 firsttime = FALSE;
1633 }
1634
Boaz Harroshf842b642007-10-02 23:16:01 +02001635 if (!nscp_cmndinfo->internal_command) {
1636 if (nscp_cmndinfo->phase == -1) {
1637 nscp_cmndinfo->phase = CACHESERVICE; /* default: cache svc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (nscp->cmnd[0] == TEST_UNIT_READY) {
1639 TRACE2(("TEST_UNIT_READY Bus %d Id %d LUN %d\n",
1640 b, t, l));
1641 /* TEST_UNIT_READY -> set scan mode */
1642 if ((ha->scan_mode & 0x0f) == 0) {
1643 if (b == 0 && t == 0 && l == 0) {
1644 ha->scan_mode |= 1;
1645 TRACE2(("Scan mode: 0x%x\n", ha->scan_mode));
1646 }
1647 } else if ((ha->scan_mode & 0x0f) == 1) {
1648 if (b == 0 && ((t == 0 && l == 1) ||
1649 (t == 1 && l == 0))) {
Boaz Harroshf842b642007-10-02 23:16:01 +02001650 nscp_cmndinfo->OpCode = GDT_SCAN_START;
1651 nscp_cmndinfo->phase = ((ha->scan_mode & 0x10 ? 1:0) << 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 | SCSIRAWSERVICE;
1653 ha->scan_mode = 0x12;
1654 TRACE2(("Scan mode: 0x%x (SCAN_START)\n",
1655 ha->scan_mode));
1656 } else {
1657 ha->scan_mode &= 0x10;
1658 TRACE2(("Scan mode: 0x%x\n", ha->scan_mode));
1659 }
1660 } else if (ha->scan_mode == 0x12) {
1661 if (b == ha->bus_cnt && t == ha->tid_cnt-1) {
Boaz Harroshf842b642007-10-02 23:16:01 +02001662 nscp_cmndinfo->phase = SCSIRAWSERVICE;
1663 nscp_cmndinfo->OpCode = GDT_SCAN_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 ha->scan_mode &= 0x10;
1665 TRACE2(("Scan mode: 0x%x (SCAN_END)\n",
1666 ha->scan_mode));
1667 }
1668 }
1669 }
1670 if (b == ha->virt_bus && nscp->cmnd[0] != INQUIRY &&
1671 nscp->cmnd[0] != READ_CAPACITY && nscp->cmnd[0] != MODE_SENSE &&
1672 (ha->hdr[t].cluster_type & CLUSTER_DRIVE)) {
1673 /* always GDT_CLUST_INFO! */
Boaz Harroshf842b642007-10-02 23:16:01 +02001674 nscp_cmndinfo->OpCode = GDT_CLUST_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
1676 }
1677 }
1678
Boaz Harroshf842b642007-10-02 23:16:01 +02001679 if (nscp_cmndinfo->OpCode != -1) {
1680 if ((nscp_cmndinfo->phase & 0xff) == CACHESERVICE) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001681 if (!(cmd_index=gdth_fill_cache_cmd(ha, nscp, t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 this_cmd = FALSE;
1683 next_cmd = FALSE;
Boaz Harroshf842b642007-10-02 23:16:01 +02001684 } else if ((nscp_cmndinfo->phase & 0xff) == SCSIRAWSERVICE) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001685 if (!(cmd_index=gdth_fill_raw_cmd(ha, nscp, BUS_L2P(ha, b))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 this_cmd = FALSE;
1687 next_cmd = FALSE;
1688 } else {
1689 memset((char*)nscp->sense_buffer,0,16);
1690 nscp->sense_buffer[0] = 0x70;
1691 nscp->sense_buffer[2] = NOT_READY;
1692 nscp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
Boaz Harroshf842b642007-10-02 23:16:01 +02001693 if (!nscp_cmndinfo->wait_for_completion)
1694 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02001696 gdth_scsi_done(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 }
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02001698 } else if (gdth_cmnd_priv(nscp)->internal_command) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001699 if (!(cmd_index=gdth_special_cmd(ha, nscp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 this_cmd = FALSE;
1701 next_cmd = FALSE;
1702 } else if (b != ha->virt_bus) {
1703 if (ha->raw[BUS_L2P(ha,b)].io_cnt[t] >= GDTH_MAX_RAW ||
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001704 !(cmd_index=gdth_fill_raw_cmd(ha, nscp, BUS_L2P(ha, b))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 this_cmd = FALSE;
1706 else
1707 ha->raw[BUS_L2P(ha,b)].io_cnt[t]++;
1708 } else if (t >= MAX_HDRIVES || !ha->hdr[t].present || l != 0) {
1709 TRACE2(("Command 0x%x to bus %d id %d lun %d -> IGNORE\n",
1710 nscp->cmnd[0], b, t, l));
1711 nscp->result = DID_BAD_TARGET << 16;
Boaz Harroshf842b642007-10-02 23:16:01 +02001712 if (!nscp_cmndinfo->wait_for_completion)
1713 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02001715 gdth_scsi_done(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 } else {
1717 switch (nscp->cmnd[0]) {
1718 case TEST_UNIT_READY:
1719 case INQUIRY:
1720 case REQUEST_SENSE:
1721 case READ_CAPACITY:
1722 case VERIFY:
1723 case START_STOP:
1724 case MODE_SENSE:
Hannes Reineckeeb846d92014-11-17 14:25:19 +01001725 case SERVICE_ACTION_IN_16:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 TRACE(("cache cmd %x/%x/%x/%x/%x/%x\n",nscp->cmnd[0],
1727 nscp->cmnd[1],nscp->cmnd[2],nscp->cmnd[3],
1728 nscp->cmnd[4],nscp->cmnd[5]));
1729 if (ha->hdr[t].media_changed && nscp->cmnd[0] != INQUIRY) {
1730 /* return UNIT_ATTENTION */
1731 TRACE2(("cmd 0x%x target %d: UNIT_ATTENTION\n",
1732 nscp->cmnd[0], t));
1733 ha->hdr[t].media_changed = FALSE;
1734 memset((char*)nscp->sense_buffer,0,16);
1735 nscp->sense_buffer[0] = 0x70;
1736 nscp->sense_buffer[2] = UNIT_ATTENTION;
1737 nscp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
Boaz Harroshf842b642007-10-02 23:16:01 +02001738 if (!nscp_cmndinfo->wait_for_completion)
1739 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02001741 gdth_scsi_done(nscp);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001742 } else if (gdth_internal_cache_cmd(ha, nscp))
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02001743 gdth_scsi_done(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 break;
1745
1746 case ALLOW_MEDIUM_REMOVAL:
1747 TRACE(("cache cmd %x/%x/%x/%x/%x/%x\n",nscp->cmnd[0],
1748 nscp->cmnd[1],nscp->cmnd[2],nscp->cmnd[3],
1749 nscp->cmnd[4],nscp->cmnd[5]));
1750 if ( (nscp->cmnd[4]&1) && !(ha->hdr[t].devtype&1) ) {
1751 TRACE(("Prevent r. nonremov. drive->do nothing\n"));
1752 nscp->result = DID_OK << 16;
1753 nscp->sense_buffer[0] = 0;
Boaz Harroshf842b642007-10-02 23:16:01 +02001754 if (!nscp_cmndinfo->wait_for_completion)
1755 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02001757 gdth_scsi_done(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 } else {
1759 nscp->cmnd[3] = (ha->hdr[t].devtype&1) ? 1:0;
1760 TRACE(("Prevent/allow r. %d rem. drive %d\n",
1761 nscp->cmnd[4],nscp->cmnd[3]));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001762 if (!(cmd_index=gdth_fill_cache_cmd(ha, nscp, t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 this_cmd = FALSE;
1764 }
1765 break;
1766
1767 case RESERVE:
1768 case RELEASE:
1769 TRACE2(("cache cmd %s\n",nscp->cmnd[0] == RESERVE ?
1770 "RESERVE" : "RELEASE"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001771 if (!(cmd_index=gdth_fill_cache_cmd(ha, nscp, t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 this_cmd = FALSE;
1773 break;
1774
1775 case READ_6:
1776 case WRITE_6:
1777 case READ_10:
1778 case WRITE_10:
1779 case READ_16:
1780 case WRITE_16:
1781 if (ha->hdr[t].media_changed) {
1782 /* return UNIT_ATTENTION */
1783 TRACE2(("cmd 0x%x target %d: UNIT_ATTENTION\n",
1784 nscp->cmnd[0], t));
1785 ha->hdr[t].media_changed = FALSE;
1786 memset((char*)nscp->sense_buffer,0,16);
1787 nscp->sense_buffer[0] = 0x70;
1788 nscp->sense_buffer[2] = UNIT_ATTENTION;
1789 nscp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
Boaz Harroshf842b642007-10-02 23:16:01 +02001790 if (!nscp_cmndinfo->wait_for_completion)
1791 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02001793 gdth_scsi_done(nscp);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001794 } else if (!(cmd_index=gdth_fill_cache_cmd(ha, nscp, t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 this_cmd = FALSE;
1796 break;
1797
1798 default:
1799 TRACE2(("cache cmd %x/%x/%x/%x/%x/%x unknown\n",nscp->cmnd[0],
1800 nscp->cmnd[1],nscp->cmnd[2],nscp->cmnd[3],
1801 nscp->cmnd[4],nscp->cmnd[5]));
1802 printk("GDT-HA %d: Unknown SCSI command 0x%x to cache service !\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001803 ha->hanum, nscp->cmnd[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 nscp->result = DID_ABORT << 16;
Boaz Harroshf842b642007-10-02 23:16:01 +02001805 if (!nscp_cmndinfo->wait_for_completion)
1806 nscp_cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 else
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02001808 gdth_scsi_done(nscp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 break;
1810 }
1811 }
1812
1813 if (!this_cmd)
1814 break;
1815 if (nscp == ha->req_first)
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02001816 ha->req_first = pscp = (struct scsi_cmnd *)nscp->SCp.ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 else
1818 pscp->SCp.ptr = nscp->SCp.ptr;
1819 if (!next_cmd)
1820 break;
1821 }
1822
1823 if (ha->cmd_cnt > 0) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001824 gdth_release_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 }
1826
1827 if (!gdth_polling)
1828 spin_unlock_irqrestore(&ha->smp_lock, flags);
1829
1830 if (gdth_polling && ha->cmd_cnt > 0) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001831 if (!gdth_wait(ha, cmd_index, POLL_TIMEOUT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 printk("GDT-HA %d: Command %d timed out !\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001833 ha->hanum, cmd_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 }
1835}
Boaz Harrosh3892d882007-10-02 23:18:03 +02001836
1837/*
1838 * gdth_copy_internal_data() - copy to/from a buffer onto a scsi_cmnd's
1839 * buffers, kmap_atomic() as needed.
1840 */
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02001841static void gdth_copy_internal_data(gdth_ha_str *ha, struct scsi_cmnd *scp,
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001842 char *buffer, u16 count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001844 u16 cpcount,i, max_sg = scsi_sg_count(scp);
1845 u16 cpsum,cpnow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 struct scatterlist *sl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 char *address;
1848
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001849 cpcount = min_t(u16, count, scsi_bufflen(scp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Boaz Harrosh3892d882007-10-02 23:18:03 +02001851 if (cpcount) {
1852 cpsum=0;
1853 scsi_for_each_sg(scp, sl, max_sg, i) {
Leubner, Achimcbd5f692006-06-09 11:34:29 -07001854 unsigned long flags;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001855 cpnow = (u16)sl->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 TRACE(("copy_internal() now %d sum %d count %d %d\n",
Boaz Harroshd35055a2008-02-27 15:31:20 -08001857 cpnow, cpsum, cpcount, scsi_bufflen(scp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 if (cpsum+cpnow > cpcount)
1859 cpnow = cpcount - cpsum;
1860 cpsum += cpnow;
Jens Axboe45711f12007-10-22 21:19:53 +02001861 if (!sg_page(sl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 printk("GDT-HA %d: invalid sc/gt element in gdth_copy_internal_data()\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001863 ha->hanum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 return;
1865 }
Leubner, Achimcbd5f692006-06-09 11:34:29 -07001866 local_irq_save(flags);
Cong Wang77dfce02011-11-25 23:14:23 +08001867 address = kmap_atomic(sg_page(sl)) + sl->offset;
Boaz Harroshee54cc62008-02-27 15:29:15 -08001868 memcpy(address, buffer, cpnow);
Jens Axboe45711f12007-10-22 21:19:53 +02001869 flush_dcache_page(sg_page(sl));
Cong Wang77dfce02011-11-25 23:14:23 +08001870 kunmap_atomic(address);
Leubner, Achimcbd5f692006-06-09 11:34:29 -07001871 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 if (cpsum == cpcount)
1873 break;
1874 buffer += cpnow;
1875 }
Boaz Harrosh3892d882007-10-02 23:18:03 +02001876 } else if (count) {
1877 printk("GDT-HA %d: SCSI command with no buffers but data transfer expected!\n",
1878 ha->hanum);
1879 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 }
1881}
1882
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02001883static int gdth_internal_cache_cmd(gdth_ha_str *ha, struct scsi_cmnd *scp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001885 u8 t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 gdth_inq_data inq;
1887 gdth_rdcap_data rdc;
1888 gdth_sense_data sd;
1889 gdth_modep_data mpd;
Boaz Harroshf842b642007-10-02 23:16:01 +02001890 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 t = scp->device->id;
1893 TRACE(("gdth_internal_cache_cmd() cmd 0x%x hdrive %d\n",
1894 scp->cmnd[0],t));
1895
1896 scp->result = DID_OK << 16;
1897 scp->sense_buffer[0] = 0;
1898
1899 switch (scp->cmnd[0]) {
1900 case TEST_UNIT_READY:
1901 case VERIFY:
1902 case START_STOP:
1903 TRACE2(("Test/Verify/Start hdrive %d\n",t));
1904 break;
1905
1906 case INQUIRY:
1907 TRACE2(("Inquiry hdrive %d devtype %d\n",
1908 t,ha->hdr[t].devtype));
1909 inq.type_qual = (ha->hdr[t].devtype&4) ? TYPE_ROM:TYPE_DISK;
1910 /* you can here set all disks to removable, if you want to do
1911 a flush using the ALLOW_MEDIUM_REMOVAL command */
1912 inq.modif_rmb = 0x00;
1913 if ((ha->hdr[t].devtype & 1) ||
1914 (ha->hdr[t].cluster_type & CLUSTER_DRIVE))
1915 inq.modif_rmb = 0x80;
1916 inq.version = 2;
1917 inq.resp_aenc = 2;
1918 inq.add_length= 32;
1919 strcpy(inq.vendor,ha->oem_name);
Arnd Bergmann0606ffe2017-07-14 14:06:57 +02001920 snprintf(inq.product, sizeof(inq.product), "Host Drive #%02d",t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 strcpy(inq.revision," ");
Boaz Harroshee54cc62008-02-27 15:29:15 -08001922 gdth_copy_internal_data(ha, scp, (char*)&inq, sizeof(gdth_inq_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 break;
1924
1925 case REQUEST_SENSE:
1926 TRACE2(("Request sense hdrive %d\n",t));
1927 sd.errorcode = 0x70;
1928 sd.segno = 0x00;
1929 sd.key = NO_SENSE;
1930 sd.info = 0;
1931 sd.add_length= 0;
Boaz Harroshee54cc62008-02-27 15:29:15 -08001932 gdth_copy_internal_data(ha, scp, (char*)&sd, sizeof(gdth_sense_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 break;
1934
1935 case MODE_SENSE:
1936 TRACE2(("Mode sense hdrive %d\n",t));
1937 memset((char*)&mpd,0,sizeof(gdth_modep_data));
1938 mpd.hd.data_length = sizeof(gdth_modep_data);
1939 mpd.hd.dev_par = (ha->hdr[t].devtype&2) ? 0x80:0;
1940 mpd.hd.bd_length = sizeof(mpd.bd);
1941 mpd.bd.block_length[0] = (SECTOR_SIZE & 0x00ff0000) >> 16;
1942 mpd.bd.block_length[1] = (SECTOR_SIZE & 0x0000ff00) >> 8;
1943 mpd.bd.block_length[2] = (SECTOR_SIZE & 0x000000ff);
Boaz Harroshee54cc62008-02-27 15:29:15 -08001944 gdth_copy_internal_data(ha, scp, (char*)&mpd, sizeof(gdth_modep_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 break;
1946
1947 case READ_CAPACITY:
1948 TRACE2(("Read capacity hdrive %d\n",t));
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001949 if (ha->hdr[t].size > (u64)0xffffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 rdc.last_block_no = 0xffffffff;
1951 else
1952 rdc.last_block_no = cpu_to_be32(ha->hdr[t].size-1);
1953 rdc.block_length = cpu_to_be32(SECTOR_SIZE);
Boaz Harroshee54cc62008-02-27 15:29:15 -08001954 gdth_copy_internal_data(ha, scp, (char*)&rdc, sizeof(gdth_rdcap_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 break;
1956
Hannes Reineckeeb846d92014-11-17 14:25:19 +01001957 case SERVICE_ACTION_IN_16:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 if ((scp->cmnd[1] & 0x1f) == SAI_READ_CAPACITY_16 &&
1959 (ha->cache_feat & GDT_64BIT)) {
1960 gdth_rdcap16_data rdc16;
1961
1962 TRACE2(("Read capacity (16) hdrive %d\n",t));
1963 rdc16.last_block_no = cpu_to_be64(ha->hdr[t].size-1);
1964 rdc16.block_length = cpu_to_be32(SECTOR_SIZE);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001965 gdth_copy_internal_data(ha, scp, (char*)&rdc16,
Boaz Harroshee54cc62008-02-27 15:29:15 -08001966 sizeof(gdth_rdcap16_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 } else {
1968 scp->result = DID_ABORT << 16;
1969 }
1970 break;
1971
1972 default:
1973 TRACE2(("Internal cache cmd 0x%x unknown\n",scp->cmnd[0]));
1974 break;
1975 }
1976
Boaz Harroshf842b642007-10-02 23:16:01 +02001977 if (!cmndinfo->wait_for_completion)
1978 cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 else
1980 return 1;
1981
1982 return 0;
1983}
Boaz Harrosh45f1a412007-10-02 23:05:53 +02001984
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02001985static int gdth_fill_cache_cmd(gdth_ha_str *ha, struct scsi_cmnd *scp,
1986 u16 hdrive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 register gdth_cmd_str *cmdp;
Boaz Harroshf842b642007-10-02 23:16:01 +02001989 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05001990 u32 cnt, blockcnt;
1991 u64 no, blockno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 int i, cmd_index, read_write, sgcnt, mode64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 cmdp = ha->pccb;
1995 TRACE(("gdth_fill_cache_cmd() cmd 0x%x cmdsize %d hdrive %d\n",
1996 scp->cmnd[0],scp->cmd_len,hdrive));
1997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 mode64 = (ha->cache_feat & GDT_64BIT) ? TRUE : FALSE;
1999 /* test for READ_16, WRITE_16 if !mode64 ? ---
2000 not required, should not occur due to error return on
2001 READ_CAPACITY_16 */
2002
2003 cmdp->Service = CACHESERVICE;
2004 cmdp->RequestBuffer = scp;
2005 /* search free command index */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002006 if (!(cmd_index=gdth_get_cmd_index(ha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 TRACE(("GDT: No free command index found\n"));
2008 return 0;
2009 }
2010 /* if it's the first command, set command semaphore */
2011 if (ha->cmd_cnt == 0)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002012 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
2014 /* fill command */
2015 read_write = 0;
Boaz Harroshf842b642007-10-02 23:16:01 +02002016 if (cmndinfo->OpCode != -1)
2017 cmdp->OpCode = cmndinfo->OpCode; /* special cache cmd. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 else if (scp->cmnd[0] == RESERVE)
2019 cmdp->OpCode = GDT_RESERVE_DRV;
2020 else if (scp->cmnd[0] == RELEASE)
2021 cmdp->OpCode = GDT_RELEASE_DRV;
2022 else if (scp->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
2023 if (scp->cmnd[4] & 1) /* prevent ? */
2024 cmdp->OpCode = GDT_MOUNT;
2025 else if (scp->cmnd[3] & 1) /* removable drive ? */
2026 cmdp->OpCode = GDT_UNMOUNT;
2027 else
2028 cmdp->OpCode = GDT_FLUSH;
2029 } else if (scp->cmnd[0] == WRITE_6 || scp->cmnd[0] == WRITE_10 ||
2030 scp->cmnd[0] == WRITE_12 || scp->cmnd[0] == WRITE_16
2031 ) {
2032 read_write = 1;
2033 if (gdth_write_through || ((ha->hdr[hdrive].rw_attribs & 1) &&
2034 (ha->cache_feat & GDT_WR_THROUGH)))
2035 cmdp->OpCode = GDT_WRITE_THR;
2036 else
2037 cmdp->OpCode = GDT_WRITE;
2038 } else {
2039 read_write = 2;
2040 cmdp->OpCode = GDT_READ;
2041 }
2042
2043 cmdp->BoardNode = LOCALBOARD;
2044 if (mode64) {
2045 cmdp->u.cache64.DeviceNo = hdrive;
2046 cmdp->u.cache64.BlockNo = 1;
2047 cmdp->u.cache64.sg_canz = 0;
2048 } else {
2049 cmdp->u.cache.DeviceNo = hdrive;
2050 cmdp->u.cache.BlockNo = 1;
2051 cmdp->u.cache.sg_canz = 0;
2052 }
2053
2054 if (read_write) {
2055 if (scp->cmd_len == 16) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002056 memcpy(&no, &scp->cmnd[2], sizeof(u64));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 blockno = be64_to_cpu(no);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002058 memcpy(&cnt, &scp->cmnd[10], sizeof(u32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 blockcnt = be32_to_cpu(cnt);
2060 } else if (scp->cmd_len == 10) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002061 memcpy(&no, &scp->cmnd[2], sizeof(u32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 blockno = be32_to_cpu(no);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002063 memcpy(&cnt, &scp->cmnd[7], sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 blockcnt = be16_to_cpu(cnt);
2065 } else {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002066 memcpy(&no, &scp->cmnd[0], sizeof(u32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 blockno = be32_to_cpu(no) & 0x001fffffUL;
2068 blockcnt= scp->cmnd[4]==0 ? 0x100 : scp->cmnd[4];
2069 }
2070 if (mode64) {
2071 cmdp->u.cache64.BlockNo = blockno;
2072 cmdp->u.cache64.BlockCnt = blockcnt;
2073 } else {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002074 cmdp->u.cache.BlockNo = (u32)blockno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 cmdp->u.cache.BlockCnt = blockcnt;
2076 }
2077
Boaz Harroshd35055a2008-02-27 15:31:20 -08002078 if (scsi_bufflen(scp)) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002079 cmndinfo->dma_dir = (read_write == 1 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
Boaz Harroshd35055a2008-02-27 15:31:20 -08002081 sgcnt = pci_map_sg(ha->pdev, scsi_sglist(scp), scsi_sg_count(scp),
Boaz Harrosh3892d882007-10-02 23:18:03 +02002082 cmndinfo->dma_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 if (mode64) {
Boaz Harrosh3892d882007-10-02 23:18:03 +02002084 struct scatterlist *sl;
2085
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002086 cmdp->u.cache64.DestAddr= (u64)-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 cmdp->u.cache64.sg_canz = sgcnt;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002088 scsi_for_each_sg(scp, sl, sgcnt, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 cmdp->u.cache64.sg_lst[i].sg_ptr = sg_dma_address(sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 cmdp->u.cache64.sg_lst[i].sg_len = sg_dma_len(sl);
2091 }
2092 } else {
Boaz Harrosh3892d882007-10-02 23:18:03 +02002093 struct scatterlist *sl;
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 cmdp->u.cache.DestAddr= 0xffffffff;
2096 cmdp->u.cache.sg_canz = sgcnt;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002097 scsi_for_each_sg(scp, sl, sgcnt, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 cmdp->u.cache.sg_lst[i].sg_ptr = sg_dma_address(sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 cmdp->u.cache.sg_lst[i].sg_len = sg_dma_len(sl);
2100 }
2101 }
2102
2103#ifdef GDTH_STATISTICS
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002104 if (max_sg < (u32)sgcnt) {
2105 max_sg = (u32)sgcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 TRACE3(("GDT: max_sg = %d\n",max_sg));
2107 }
2108#endif
2109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 }
2111 }
2112 /* evaluate command size, check space */
2113 if (mode64) {
2114 TRACE(("cache cmd: addr. %x sganz %x sgptr0 %x sglen0 %x\n",
2115 cmdp->u.cache64.DestAddr,cmdp->u.cache64.sg_canz,
2116 cmdp->u.cache64.sg_lst[0].sg_ptr,
2117 cmdp->u.cache64.sg_lst[0].sg_len));
2118 TRACE(("cache cmd: cmd %d blockno. %d, blockcnt %d\n",
2119 cmdp->OpCode,cmdp->u.cache64.BlockNo,cmdp->u.cache64.BlockCnt));
2120 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.cache64.sg_lst) +
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002121 (u16)cmdp->u.cache64.sg_canz * sizeof(gdth_sg64_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 } else {
2123 TRACE(("cache cmd: addr. %x sganz %x sgptr0 %x sglen0 %x\n",
2124 cmdp->u.cache.DestAddr,cmdp->u.cache.sg_canz,
2125 cmdp->u.cache.sg_lst[0].sg_ptr,
2126 cmdp->u.cache.sg_lst[0].sg_len));
2127 TRACE(("cache cmd: cmd %d blockno. %d, blockcnt %d\n",
2128 cmdp->OpCode,cmdp->u.cache.BlockNo,cmdp->u.cache.BlockCnt));
2129 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.cache.sg_lst) +
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002130 (u16)cmdp->u.cache.sg_canz * sizeof(gdth_sg_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 }
2132 if (ha->cmd_len & 3)
2133 ha->cmd_len += (4 - (ha->cmd_len & 3));
2134
2135 if (ha->cmd_cnt > 0) {
2136 if ((ha->cmd_offs_dpmem + ha->cmd_len + DPMEM_COMMAND_OFFSET) >
2137 ha->ic_all_size) {
2138 TRACE2(("gdth_fill_cache() DPMEM overflow\n"));
2139 ha->cmd_tab[cmd_index-2].cmnd = UNUSED_CMND;
2140 return 0;
2141 }
2142 }
2143
2144 /* copy command */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002145 gdth_copy_command(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 return cmd_index;
2147}
2148
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02002149static int gdth_fill_raw_cmd(gdth_ha_str *ha, struct scsi_cmnd *scp, u8 b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 register gdth_cmd_str *cmdp;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002152 u16 i;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002153 dma_addr_t sense_paddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 int cmd_index, sgcnt, mode64;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002155 u8 t,l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 struct page *page;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002157 unsigned long offset;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02002158 struct gdth_cmndinfo *cmndinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 t = scp->device->id;
2161 l = scp->device->lun;
2162 cmdp = ha->pccb;
2163 TRACE(("gdth_fill_raw_cmd() cmd 0x%x bus %d ID %d LUN %d\n",
2164 scp->cmnd[0],b,t,l));
2165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 mode64 = (ha->raw_feat & GDT_64BIT) ? TRUE : FALSE;
2167
2168 cmdp->Service = SCSIRAWSERVICE;
2169 cmdp->RequestBuffer = scp;
2170 /* search free command index */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002171 if (!(cmd_index=gdth_get_cmd_index(ha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 TRACE(("GDT: No free command index found\n"));
2173 return 0;
2174 }
2175 /* if it's the first command, set command semaphore */
2176 if (ha->cmd_cnt == 0)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002177 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02002179 cmndinfo = gdth_cmnd_priv(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 /* fill command */
Boaz Harroshf842b642007-10-02 23:16:01 +02002181 if (cmndinfo->OpCode != -1) {
2182 cmdp->OpCode = cmndinfo->OpCode; /* special raw cmd. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 cmdp->BoardNode = LOCALBOARD;
2184 if (mode64) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002185 cmdp->u.raw64.direction = (cmndinfo->phase >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 TRACE2(("special raw cmd 0x%x param 0x%x\n",
2187 cmdp->OpCode, cmdp->u.raw64.direction));
2188 /* evaluate command size */
2189 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw64.sg_lst);
2190 } else {
Boaz Harroshf842b642007-10-02 23:16:01 +02002191 cmdp->u.raw.direction = (cmndinfo->phase >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 TRACE2(("special raw cmd 0x%x param 0x%x\n",
2193 cmdp->OpCode, cmdp->u.raw.direction));
2194 /* evaluate command size */
2195 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw.sg_lst);
2196 }
2197
2198 } else {
2199 page = virt_to_page(scp->sense_buffer);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002200 offset = (unsigned long)scp->sense_buffer & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 sense_paddr = pci_map_page(ha->pdev,page,offset,
2202 16,PCI_DMA_FROMDEVICE);
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02002203
2204 cmndinfo->sense_paddr = sense_paddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 cmdp->OpCode = GDT_WRITE; /* always */
2206 cmdp->BoardNode = LOCALBOARD;
2207 if (mode64) {
2208 cmdp->u.raw64.reserved = 0;
2209 cmdp->u.raw64.mdisc_time = 0;
2210 cmdp->u.raw64.mcon_time = 0;
2211 cmdp->u.raw64.clen = scp->cmd_len;
2212 cmdp->u.raw64.target = t;
2213 cmdp->u.raw64.lun = l;
2214 cmdp->u.raw64.bus = b;
2215 cmdp->u.raw64.priority = 0;
Boaz Harroshd35055a2008-02-27 15:31:20 -08002216 cmdp->u.raw64.sdlen = scsi_bufflen(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 cmdp->u.raw64.sense_len = 16;
2218 cmdp->u.raw64.sense_data = sense_paddr;
2219 cmdp->u.raw64.direction =
2220 gdth_direction_tab[scp->cmnd[0]]==DOU ? GDTH_DATA_OUT:GDTH_DATA_IN;
2221 memcpy(cmdp->u.raw64.cmd,scp->cmnd,16);
Joerg Dorchainbb9ba312007-03-06 02:46:54 -08002222 cmdp->u.raw64.sg_ranz = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 } else {
2224 cmdp->u.raw.reserved = 0;
2225 cmdp->u.raw.mdisc_time = 0;
2226 cmdp->u.raw.mcon_time = 0;
2227 cmdp->u.raw.clen = scp->cmd_len;
2228 cmdp->u.raw.target = t;
2229 cmdp->u.raw.lun = l;
2230 cmdp->u.raw.bus = b;
2231 cmdp->u.raw.priority = 0;
2232 cmdp->u.raw.link_p = 0;
Boaz Harroshd35055a2008-02-27 15:31:20 -08002233 cmdp->u.raw.sdlen = scsi_bufflen(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 cmdp->u.raw.sense_len = 16;
2235 cmdp->u.raw.sense_data = sense_paddr;
2236 cmdp->u.raw.direction =
2237 gdth_direction_tab[scp->cmnd[0]]==DOU ? GDTH_DATA_OUT:GDTH_DATA_IN;
2238 memcpy(cmdp->u.raw.cmd,scp->cmnd,12);
Joerg Dorchainbb9ba312007-03-06 02:46:54 -08002239 cmdp->u.raw.sg_ranz = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 }
2241
Boaz Harroshd35055a2008-02-27 15:31:20 -08002242 if (scsi_bufflen(scp)) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002243 cmndinfo->dma_dir = PCI_DMA_BIDIRECTIONAL;
Boaz Harroshd35055a2008-02-27 15:31:20 -08002244 sgcnt = pci_map_sg(ha->pdev, scsi_sglist(scp), scsi_sg_count(scp),
Boaz Harrosh3892d882007-10-02 23:18:03 +02002245 cmndinfo->dma_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 if (mode64) {
Boaz Harrosh3892d882007-10-02 23:18:03 +02002247 struct scatterlist *sl;
2248
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002249 cmdp->u.raw64.sdata = (u64)-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 cmdp->u.raw64.sg_ranz = sgcnt;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002251 scsi_for_each_sg(scp, sl, sgcnt, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 cmdp->u.raw64.sg_lst[i].sg_ptr = sg_dma_address(sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 cmdp->u.raw64.sg_lst[i].sg_len = sg_dma_len(sl);
2254 }
2255 } else {
Boaz Harrosh3892d882007-10-02 23:18:03 +02002256 struct scatterlist *sl;
2257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 cmdp->u.raw.sdata = 0xffffffff;
2259 cmdp->u.raw.sg_ranz = sgcnt;
Boaz Harrosh3892d882007-10-02 23:18:03 +02002260 scsi_for_each_sg(scp, sl, sgcnt, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 cmdp->u.raw.sg_lst[i].sg_ptr = sg_dma_address(sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 cmdp->u.raw.sg_lst[i].sg_len = sg_dma_len(sl);
2263 }
2264 }
2265
2266#ifdef GDTH_STATISTICS
2267 if (max_sg < sgcnt) {
2268 max_sg = sgcnt;
2269 TRACE3(("GDT: max_sg = %d\n",sgcnt));
2270 }
2271#endif
2272
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
2274 if (mode64) {
2275 TRACE(("raw cmd: addr. %x sganz %x sgptr0 %x sglen0 %x\n",
2276 cmdp->u.raw64.sdata,cmdp->u.raw64.sg_ranz,
2277 cmdp->u.raw64.sg_lst[0].sg_ptr,
2278 cmdp->u.raw64.sg_lst[0].sg_len));
2279 /* evaluate command size */
2280 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw64.sg_lst) +
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002281 (u16)cmdp->u.raw64.sg_ranz * sizeof(gdth_sg64_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 } else {
2283 TRACE(("raw cmd: addr. %x sganz %x sgptr0 %x sglen0 %x\n",
2284 cmdp->u.raw.sdata,cmdp->u.raw.sg_ranz,
2285 cmdp->u.raw.sg_lst[0].sg_ptr,
2286 cmdp->u.raw.sg_lst[0].sg_len));
2287 /* evaluate command size */
2288 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw.sg_lst) +
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002289 (u16)cmdp->u.raw.sg_ranz * sizeof(gdth_sg_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 }
2291 }
2292 /* check space */
2293 if (ha->cmd_len & 3)
2294 ha->cmd_len += (4 - (ha->cmd_len & 3));
2295
2296 if (ha->cmd_cnt > 0) {
2297 if ((ha->cmd_offs_dpmem + ha->cmd_len + DPMEM_COMMAND_OFFSET) >
2298 ha->ic_all_size) {
2299 TRACE2(("gdth_fill_raw() DPMEM overflow\n"));
2300 ha->cmd_tab[cmd_index-2].cmnd = UNUSED_CMND;
2301 return 0;
2302 }
2303 }
2304
2305 /* copy command */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002306 gdth_copy_command(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 return cmd_index;
2308}
2309
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02002310static int gdth_special_cmd(gdth_ha_str *ha, struct scsi_cmnd *scp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 register gdth_cmd_str *cmdp;
Boaz Harroshee54cc62008-02-27 15:29:15 -08002313 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 int cmd_index;
2315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 cmdp= ha->pccb;
2317 TRACE2(("gdth_special_cmd(): "));
2318
Boaz Harroshee54cc62008-02-27 15:29:15 -08002319 *cmdp = *cmndinfo->internal_cmd_str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 cmdp->RequestBuffer = scp;
2321
2322 /* search free command index */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002323 if (!(cmd_index=gdth_get_cmd_index(ha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 TRACE(("GDT: No free command index found\n"));
2325 return 0;
2326 }
2327
2328 /* if it's the first command, set command semaphore */
2329 if (ha->cmd_cnt == 0)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002330 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
2332 /* evaluate command size, check space */
2333 if (cmdp->OpCode == GDT_IOCTL) {
2334 TRACE2(("IOCTL\n"));
2335 ha->cmd_len =
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002336 GDTOFFSOF(gdth_cmd_str,u.ioctl.p_param) + sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 } else if (cmdp->Service == CACHESERVICE) {
2338 TRACE2(("cache command %d\n",cmdp->OpCode));
2339 if (ha->cache_feat & GDT_64BIT)
2340 ha->cmd_len =
2341 GDTOFFSOF(gdth_cmd_str,u.cache64.sg_lst) + sizeof(gdth_sg64_str);
2342 else
2343 ha->cmd_len =
2344 GDTOFFSOF(gdth_cmd_str,u.cache.sg_lst) + sizeof(gdth_sg_str);
2345 } else if (cmdp->Service == SCSIRAWSERVICE) {
2346 TRACE2(("raw command %d\n",cmdp->OpCode));
2347 if (ha->raw_feat & GDT_64BIT)
2348 ha->cmd_len =
2349 GDTOFFSOF(gdth_cmd_str,u.raw64.sg_lst) + sizeof(gdth_sg64_str);
2350 else
2351 ha->cmd_len =
2352 GDTOFFSOF(gdth_cmd_str,u.raw.sg_lst) + sizeof(gdth_sg_str);
2353 }
2354
2355 if (ha->cmd_len & 3)
2356 ha->cmd_len += (4 - (ha->cmd_len & 3));
2357
2358 if (ha->cmd_cnt > 0) {
2359 if ((ha->cmd_offs_dpmem + ha->cmd_len + DPMEM_COMMAND_OFFSET) >
2360 ha->ic_all_size) {
2361 TRACE2(("gdth_special_cmd() DPMEM overflow\n"));
2362 ha->cmd_tab[cmd_index-2].cmnd = UNUSED_CMND;
2363 return 0;
2364 }
2365 }
2366
2367 /* copy command */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002368 gdth_copy_command(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 return cmd_index;
2370}
2371
2372
2373/* Controller event handling functions */
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002374static gdth_evt_str *gdth_store_event(gdth_ha_str *ha, u16 source,
2375 u16 idx, gdth_evt_data *evt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376{
2377 gdth_evt_str *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
2379 /* no GDTH_LOCK_HA() ! */
2380 TRACE2(("gdth_store_event() source %d idx %d\n", source, idx));
2381 if (source == 0) /* no source -> no event */
2382 return NULL;
2383
2384 if (ebuffer[elastidx].event_source == source &&
2385 ebuffer[elastidx].event_idx == idx &&
2386 ((evt->size != 0 && ebuffer[elastidx].event_data.size != 0 &&
2387 !memcmp((char *)&ebuffer[elastidx].event_data.eu,
2388 (char *)&evt->eu, evt->size)) ||
2389 (evt->size == 0 && ebuffer[elastidx].event_data.size == 0 &&
2390 !strcmp((char *)&ebuffer[elastidx].event_data.event_string,
2391 (char *)&evt->event_string)))) {
2392 e = &ebuffer[elastidx];
Alison Schofield5a412c32016-02-17 21:29:34 -08002393 e->last_stamp = (u32)ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 ++e->same_count;
2395 } else {
2396 if (ebuffer[elastidx].event_source != 0) { /* entry not free ? */
2397 ++elastidx;
2398 if (elastidx == MAX_EVENTS)
2399 elastidx = 0;
2400 if (elastidx == eoldidx) { /* reached mark ? */
2401 ++eoldidx;
2402 if (eoldidx == MAX_EVENTS)
2403 eoldidx = 0;
2404 }
2405 }
2406 e = &ebuffer[elastidx];
2407 e->event_source = source;
2408 e->event_idx = idx;
Alison Schofield5a412c32016-02-17 21:29:34 -08002409 e->first_stamp = e->last_stamp = (u32)ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 e->same_count = 1;
2411 e->event_data = *evt;
2412 e->application = 0;
2413 }
2414 return e;
2415}
2416
2417static int gdth_read_event(gdth_ha_str *ha, int handle, gdth_evt_str *estr)
2418{
2419 gdth_evt_str *e;
2420 int eindex;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002421 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
2423 TRACE2(("gdth_read_event() handle %d\n", handle));
2424 spin_lock_irqsave(&ha->smp_lock, flags);
2425 if (handle == -1)
2426 eindex = eoldidx;
2427 else
2428 eindex = handle;
2429 estr->event_source = 0;
2430
Dave Jones690e7442009-10-19 19:55:13 -04002431 if (eindex < 0 || eindex >= MAX_EVENTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 spin_unlock_irqrestore(&ha->smp_lock, flags);
2433 return eindex;
2434 }
2435 e = &ebuffer[eindex];
2436 if (e->event_source != 0) {
2437 if (eindex != elastidx) {
2438 if (++eindex == MAX_EVENTS)
2439 eindex = 0;
2440 } else {
2441 eindex = -1;
2442 }
2443 memcpy(estr, e, sizeof(gdth_evt_str));
2444 }
2445 spin_unlock_irqrestore(&ha->smp_lock, flags);
2446 return eindex;
2447}
2448
2449static void gdth_readapp_event(gdth_ha_str *ha,
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002450 u8 application, gdth_evt_str *estr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451{
2452 gdth_evt_str *e;
2453 int eindex;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002454 unsigned long flags;
2455 u8 found = FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
2457 TRACE2(("gdth_readapp_event() app. %d\n", application));
2458 spin_lock_irqsave(&ha->smp_lock, flags);
2459 eindex = eoldidx;
2460 for (;;) {
2461 e = &ebuffer[eindex];
2462 if (e->event_source == 0)
2463 break;
2464 if ((e->application & application) == 0) {
2465 e->application |= application;
2466 found = TRUE;
2467 break;
2468 }
2469 if (eindex == elastidx)
2470 break;
2471 if (++eindex == MAX_EVENTS)
2472 eindex = 0;
2473 }
2474 if (found)
2475 memcpy(estr, e, sizeof(gdth_evt_str));
2476 else
2477 estr->event_source = 0;
2478 spin_unlock_irqrestore(&ha->smp_lock, flags);
2479}
2480
2481static void gdth_clear_events(void)
2482{
2483 TRACE(("gdth_clear_events()"));
2484
2485 eoldidx = elastidx = 0;
2486 ebuffer[0].event_source = 0;
2487}
2488
2489
2490/* SCSI interface functions */
2491
Jeff Garzik230e8862007-12-13 16:14:12 -08002492static irqreturn_t __gdth_interrupt(gdth_ha_str *ha,
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002493 int gdth_from_wait, int* pIndex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 gdt6m_dpram_str __iomem *dp6m_ptr = NULL;
2496 gdt6_dpram_str __iomem *dp6_ptr;
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02002497 struct scsi_cmnd *scp;
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002498 int rval, i;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002499 u8 IStatus;
2500 u16 Service;
2501 unsigned long flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
Jeff Garzik230e8862007-12-13 16:14:12 -08002503 TRACE(("gdth_interrupt() IRQ %d\n", ha->irq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
2505 /* if polling and not from gdth_wait() -> return */
2506 if (gdth_polling) {
2507 if (!gdth_from_wait) {
2508 return IRQ_HANDLED;
2509 }
2510 }
2511
2512 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002513 spin_lock_irqsave(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
2515 /* search controller */
Jeff Garzik230e8862007-12-13 16:14:12 -08002516 IStatus = gdth_get_status(ha);
2517 if (IStatus == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 /* spurious interrupt */
2519 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002520 spin_unlock_irqrestore(&ha->smp_lock, flags);
2521 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
2524#ifdef GDTH_STATISTICS
2525 ++act_ints;
2526#endif
2527
Christoph Hellwig31481452018-12-12 08:41:21 +01002528 if (ha->type == GDT_PCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 dp6_ptr = ha->brd;
2530 if (IStatus & 0x80) { /* error flag */
2531 IStatus &= ~0x80;
Jeff Garzika52667f2007-10-02 22:55:53 +02002532 ha->status = readw(&dp6_ptr->u.ic.Status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
2534 } else /* no error */
2535 ha->status = S_OK;
Jeff Garzika52667f2007-10-02 22:55:53 +02002536 ha->info = readl(&dp6_ptr->u.ic.Info[0]);
2537 ha->service = readw(&dp6_ptr->u.ic.Service);
2538 ha->info2 = readl(&dp6_ptr->u.ic.Info[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
Jeff Garzika52667f2007-10-02 22:55:53 +02002540 writeb(0xff, &dp6_ptr->io.irqdel); /* acknowledge interrupt */
2541 writeb(0, &dp6_ptr->u.ic.Cmd_Index);/* reset command index */
2542 writeb(0, &dp6_ptr->io.Sema1); /* reset status semaphore */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 } else if (ha->type == GDT_PCINEW) {
2544 if (IStatus & 0x80) { /* error flag */
2545 IStatus &= ~0x80;
2546 ha->status = inw(PTR2USHORT(&ha->plx->status));
2547 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
2548 } else
2549 ha->status = S_OK;
2550 ha->info = inl(PTR2USHORT(&ha->plx->info[0]));
2551 ha->service = inw(PTR2USHORT(&ha->plx->service));
2552 ha->info2 = inl(PTR2USHORT(&ha->plx->info[1]));
2553
2554 outb(0xff, PTR2USHORT(&ha->plx->edoor_reg));
2555 outb(0x00, PTR2USHORT(&ha->plx->sema1_reg));
2556 } else if (ha->type == GDT_PCIMPR) {
2557 dp6m_ptr = ha->brd;
2558 if (IStatus & 0x80) { /* error flag */
2559 IStatus &= ~0x80;
Christoph Hellwigd8e1e6a2018-12-12 08:41:26 +01002560 ha->status = readw(&dp6m_ptr->i960r.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
2562 } else /* no error */
2563 ha->status = S_OK;
Christoph Hellwigd8e1e6a2018-12-12 08:41:26 +01002564
2565 ha->info = readl(&dp6m_ptr->i960r.info[0]);
2566 ha->service = readw(&dp6m_ptr->i960r.service);
2567 ha->info2 = readl(&dp6m_ptr->i960r.info[1]);
2568
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 /* event string */
2570 if (IStatus == ASYNCINDEX) {
2571 if (ha->service != SCREENSERVICE &&
2572 (ha->fw_vers & 0xff) >= 0x1a) {
Jeff Garzika52667f2007-10-02 22:55:53 +02002573 ha->dvr.severity = readb
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 (&((gdt6m_dpram_str __iomem *)ha->brd)->i960r.severity);
2575 for (i = 0; i < 256; ++i) {
Jeff Garzika52667f2007-10-02 22:55:53 +02002576 ha->dvr.event_string[i] = readb
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 (&((gdt6m_dpram_str __iomem *)ha->brd)->i960r.evt_str[i]);
2578 if (ha->dvr.event_string[i] == 0)
2579 break;
2580 }
2581 }
2582 }
Christoph Hellwigd8e1e6a2018-12-12 08:41:26 +01002583 writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
2584 writeb(0, &dp6m_ptr->i960r.sema1_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 } else {
2586 TRACE2(("gdth_interrupt() unknown controller type\n"));
2587 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002588 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 return IRQ_HANDLED;
2590 }
2591
2592 TRACE(("gdth_interrupt() index %d stat %d info %d\n",
2593 IStatus,ha->status,ha->info));
2594
2595 if (gdth_from_wait) {
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002596 *pIndex = (int)IStatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 }
2598
2599 if (IStatus == ASYNCINDEX) {
2600 TRACE2(("gdth_interrupt() async. event\n"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002601 gdth_async_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002603 spin_unlock_irqrestore(&ha->smp_lock, flags);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002604 gdth_next(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 return IRQ_HANDLED;
2606 }
2607
2608 if (IStatus == SPEZINDEX) {
2609 TRACE2(("Service unknown or not initialized !\n"));
2610 ha->dvr.size = sizeof(ha->dvr.eu.driver);
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002611 ha->dvr.eu.driver.ionode = ha->hanum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 gdth_store_event(ha, ES_DRIVER, 4, &ha->dvr);
2613 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002614 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 return IRQ_HANDLED;
2616 }
2617 scp = ha->cmd_tab[IStatus-2].cmnd;
2618 Service = ha->cmd_tab[IStatus-2].service;
2619 ha->cmd_tab[IStatus-2].cmnd = UNUSED_CMND;
2620 if (scp == UNUSED_CMND) {
2621 TRACE2(("gdth_interrupt() index to unused command (%d)\n",IStatus));
2622 ha->dvr.size = sizeof(ha->dvr.eu.driver);
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002623 ha->dvr.eu.driver.ionode = ha->hanum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 ha->dvr.eu.driver.index = IStatus;
2625 gdth_store_event(ha, ES_DRIVER, 1, &ha->dvr);
2626 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002627 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 return IRQ_HANDLED;
2629 }
2630 if (scp == INTERNAL_CMND) {
2631 TRACE(("gdth_interrupt() answer to internal command\n"));
2632 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002633 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 return IRQ_HANDLED;
2635 }
2636
2637 TRACE(("gdth_interrupt() sync. status\n"));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002638 rval = gdth_sync_event(ha,Service,IStatus,scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 if (!gdth_polling)
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002640 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 if (rval == 2) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002642 gdth_putq(ha, scp, gdth_cmnd_priv(scp)->priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 } else if (rval == 1) {
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02002644 gdth_scsi_done(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 }
2646
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002647 gdth_next(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 return IRQ_HANDLED;
2649}
2650
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002651static irqreturn_t gdth_interrupt(int irq, void *dev_id)
2652{
Jeff Garzik230e8862007-12-13 16:14:12 -08002653 gdth_ha_str *ha = dev_id;
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002654
Jeff Garzik230e8862007-12-13 16:14:12 -08002655 return __gdth_interrupt(ha, false, NULL);
Boaz Harrosh10e1b4b2007-10-02 23:08:10 +02002656}
2657
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002658static int gdth_sync_event(gdth_ha_str *ha, int service, u8 index,
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02002659 struct scsi_cmnd *scp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 gdth_msg_str *msg;
2662 gdth_cmd_str *cmdp;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002663 u8 b, t;
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02002664 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 cmdp = ha->pccb;
2667 TRACE(("gdth_sync_event() serv %d status %d\n",
2668 service,ha->status));
2669
2670 if (service == SCREENSERVICE) {
2671 msg = ha->pmsg;
2672 TRACE(("len: %d, answer: %d, ext: %d, alen: %d\n",
2673 msg->msg_len,msg->msg_answer,msg->msg_ext,msg->msg_alen));
2674 if (msg->msg_len > MSGLEN+1)
2675 msg->msg_len = MSGLEN+1;
2676 if (msg->msg_len)
2677 if (!(msg->msg_answer && msg->msg_ext)) {
2678 msg->msg_text[msg->msg_len] = '\0';
2679 printk("%s",msg->msg_text);
2680 }
2681
2682 if (msg->msg_ext && !msg->msg_answer) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002683 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 gdth_delay(0);
2685 cmdp->Service = SCREENSERVICE;
2686 cmdp->RequestBuffer = SCREEN_CMND;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002687 gdth_get_cmd_index(ha);
2688 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 cmdp->OpCode = GDT_READ;
2690 cmdp->BoardNode = LOCALBOARD;
2691 cmdp->u.screen.reserved = 0;
2692 cmdp->u.screen.su.msg.msg_handle= msg->msg_handle;
2693 cmdp->u.screen.su.msg.msg_addr = ha->msg_phys;
2694 ha->cmd_offs_dpmem = 0;
2695 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.screen.su.msg.msg_addr)
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002696 + sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 ha->cmd_cnt = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002698 gdth_copy_command(ha);
2699 gdth_release_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 return 0;
2701 }
2702
2703 if (msg->msg_answer && msg->msg_alen) {
2704 /* default answers (getchar() not possible) */
2705 if (msg->msg_alen == 1) {
2706 msg->msg_alen = 0;
2707 msg->msg_len = 1;
2708 msg->msg_text[0] = 0;
2709 } else {
2710 msg->msg_alen -= 2;
2711 msg->msg_len = 2;
2712 msg->msg_text[0] = 1;
2713 msg->msg_text[1] = 0;
2714 }
2715 msg->msg_ext = 0;
2716 msg->msg_answer = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002717 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 gdth_delay(0);
2719 cmdp->Service = SCREENSERVICE;
2720 cmdp->RequestBuffer = SCREEN_CMND;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002721 gdth_get_cmd_index(ha);
2722 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 cmdp->OpCode = GDT_WRITE;
2724 cmdp->BoardNode = LOCALBOARD;
2725 cmdp->u.screen.reserved = 0;
2726 cmdp->u.screen.su.msg.msg_handle= msg->msg_handle;
2727 cmdp->u.screen.su.msg.msg_addr = ha->msg_phys;
2728 ha->cmd_offs_dpmem = 0;
2729 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.screen.su.msg.msg_addr)
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002730 + sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 ha->cmd_cnt = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002732 gdth_copy_command(ha);
2733 gdth_release_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 return 0;
2735 }
2736 printk("\n");
2737
2738 } else {
Christoph Hellwig52759e62007-10-02 22:59:53 +02002739 b = scp->device->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 t = scp->device->id;
Boaz Harroshf842b642007-10-02 23:16:01 +02002741 if (cmndinfo->OpCode == -1 && b != ha->virt_bus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 ha->raw[BUS_L2P(ha,b)].io_cnt[t]--;
2743 }
2744 /* cache or raw service */
2745 if (ha->status == S_BSY) {
2746 TRACE2(("Controller busy -> retry !\n"));
Boaz Harroshf842b642007-10-02 23:16:01 +02002747 if (cmndinfo->OpCode == GDT_MOUNT)
2748 cmndinfo->OpCode = GDT_CLUST_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 /* retry */
2750 return 2;
2751 }
Boaz Harroshd35055a2008-02-27 15:31:20 -08002752 if (scsi_bufflen(scp))
2753 pci_unmap_sg(ha->pdev, scsi_sglist(scp), scsi_sg_count(scp),
Boaz Harrosh3892d882007-10-02 23:18:03 +02002754 cmndinfo->dma_dir);
2755
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02002756 if (cmndinfo->sense_paddr)
2757 pci_unmap_page(ha->pdev, cmndinfo->sense_paddr, 16,
2758 PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
2760 if (ha->status == S_OK) {
Boaz Harroshf842b642007-10-02 23:16:01 +02002761 cmndinfo->status = S_OK;
2762 cmndinfo->info = ha->info;
2763 if (cmndinfo->OpCode != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 TRACE2(("gdth_sync_event(): special cmd 0x%x OK\n",
Boaz Harroshf842b642007-10-02 23:16:01 +02002765 cmndinfo->OpCode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 /* special commands GDT_CLUST_INFO/GDT_MOUNT ? */
Boaz Harroshf842b642007-10-02 23:16:01 +02002767 if (cmndinfo->OpCode == GDT_CLUST_INFO) {
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002768 ha->hdr[t].cluster_type = (u8)ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 if (!(ha->hdr[t].cluster_type &
2770 CLUSTER_MOUNTED)) {
2771 /* NOT MOUNTED -> MOUNT */
Boaz Harroshf842b642007-10-02 23:16:01 +02002772 cmndinfo->OpCode = GDT_MOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 if (ha->hdr[t].cluster_type &
2774 CLUSTER_RESERVED) {
2775 /* cluster drive RESERVED (on the other node) */
Boaz Harroshf842b642007-10-02 23:16:01 +02002776 cmndinfo->phase = -2; /* reservation conflict */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 }
2778 } else {
Boaz Harroshf842b642007-10-02 23:16:01 +02002779 cmndinfo->OpCode = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 }
2781 } else {
Boaz Harroshf842b642007-10-02 23:16:01 +02002782 if (cmndinfo->OpCode == GDT_MOUNT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 ha->hdr[t].cluster_type |= CLUSTER_MOUNTED;
2784 ha->hdr[t].media_changed = TRUE;
Boaz Harroshf842b642007-10-02 23:16:01 +02002785 } else if (cmndinfo->OpCode == GDT_UNMOUNT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 ha->hdr[t].cluster_type &= ~CLUSTER_MOUNTED;
2787 ha->hdr[t].media_changed = TRUE;
2788 }
Boaz Harroshf842b642007-10-02 23:16:01 +02002789 cmndinfo->OpCode = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 }
2791 /* retry */
Boaz Harroshf842b642007-10-02 23:16:01 +02002792 cmndinfo->priority = HIGH_PRI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 return 2;
2794 } else {
2795 /* RESERVE/RELEASE ? */
2796 if (scp->cmnd[0] == RESERVE) {
2797 ha->hdr[t].cluster_type |= CLUSTER_RESERVED;
2798 } else if (scp->cmnd[0] == RELEASE) {
2799 ha->hdr[t].cluster_type &= ~CLUSTER_RESERVED;
2800 }
2801 scp->result = DID_OK << 16;
2802 scp->sense_buffer[0] = 0;
2803 }
2804 } else {
Boaz Harroshf842b642007-10-02 23:16:01 +02002805 cmndinfo->status = ha->status;
2806 cmndinfo->info = ha->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
Boaz Harroshf842b642007-10-02 23:16:01 +02002808 if (cmndinfo->OpCode != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 TRACE2(("gdth_sync_event(): special cmd 0x%x error 0x%x\n",
Boaz Harroshf842b642007-10-02 23:16:01 +02002810 cmndinfo->OpCode, ha->status));
2811 if (cmndinfo->OpCode == GDT_SCAN_START ||
2812 cmndinfo->OpCode == GDT_SCAN_END) {
2813 cmndinfo->OpCode = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 /* retry */
Boaz Harroshf842b642007-10-02 23:16:01 +02002815 cmndinfo->priority = HIGH_PRI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 return 2;
2817 }
2818 memset((char*)scp->sense_buffer,0,16);
2819 scp->sense_buffer[0] = 0x70;
2820 scp->sense_buffer[2] = NOT_READY;
2821 scp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
2822 } else if (service == CACHESERVICE) {
2823 if (ha->status == S_CACHE_UNKNOWN &&
2824 (ha->hdr[t].cluster_type &
2825 CLUSTER_RESERVE_STATE) == CLUSTER_RESERVE_STATE) {
2826 /* bus reset -> force GDT_CLUST_INFO */
2827 ha->hdr[t].cluster_type &= ~CLUSTER_RESERVED;
2828 }
2829 memset((char*)scp->sense_buffer,0,16);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05002830 if (ha->status == (u16)S_CACHE_RESERV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 scp->result = (DID_OK << 16) | (RESERVATION_CONFLICT << 1);
2832 } else {
2833 scp->sense_buffer[0] = 0x70;
2834 scp->sense_buffer[2] = NOT_READY;
2835 scp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
2836 }
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02002837 if (!cmndinfo->internal_command) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 ha->dvr.size = sizeof(ha->dvr.eu.sync);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02002839 ha->dvr.eu.sync.ionode = ha->hanum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 ha->dvr.eu.sync.service = service;
2841 ha->dvr.eu.sync.status = ha->status;
2842 ha->dvr.eu.sync.info = ha->info;
2843 ha->dvr.eu.sync.hostdrive = t;
2844 if (ha->status >= 0x8000)
2845 gdth_store_event(ha, ES_SYNC, 0, &ha->dvr);
2846 else
2847 gdth_store_event(ha, ES_SYNC, service, &ha->dvr);
2848 }
2849 } else {
2850 /* sense buffer filled from controller firmware (DMA) */
2851 if (ha->status != S_RAW_SCSI || ha->info >= 0x100) {
2852 scp->result = DID_BAD_TARGET << 16;
2853 } else {
2854 scp->result = (DID_OK << 16) | ha->info;
2855 }
2856 }
2857 }
Boaz Harroshf842b642007-10-02 23:16:01 +02002858 if (!cmndinfo->wait_for_completion)
2859 cmndinfo->wait_for_completion++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 else
2861 return 1;
2862 }
2863
2864 return 0;
2865}
2866
2867static char *async_cache_tab[] = {
2868/* 0*/ "\011\000\002\002\002\004\002\006\004"
2869 "GDT HA %u, service %u, async. status %u/%lu unknown",
2870/* 1*/ "\011\000\002\002\002\004\002\006\004"
2871 "GDT HA %u, service %u, async. status %u/%lu unknown",
2872/* 2*/ "\005\000\002\006\004"
2873 "GDT HA %u, Host Drive %lu not ready",
2874/* 3*/ "\005\000\002\006\004"
2875 "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",
2876/* 4*/ "\005\000\002\006\004"
2877 "GDT HA %u, mirror update on Host Drive %lu failed",
2878/* 5*/ "\005\000\002\006\004"
2879 "GDT HA %u, Mirror Drive %lu failed",
2880/* 6*/ "\005\000\002\006\004"
2881 "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",
2882/* 7*/ "\005\000\002\006\004"
2883 "GDT HA %u, Host Drive %lu write protected",
2884/* 8*/ "\005\000\002\006\004"
2885 "GDT HA %u, media changed in Host Drive %lu",
2886/* 9*/ "\005\000\002\006\004"
2887 "GDT HA %u, Host Drive %lu is offline",
2888/*10*/ "\005\000\002\006\004"
2889 "GDT HA %u, media change of Mirror Drive %lu",
2890/*11*/ "\005\000\002\006\004"
2891 "GDT HA %u, Mirror Drive %lu is write protected",
2892/*12*/ "\005\000\002\006\004"
2893 "GDT HA %u, general error on Host Drive %lu. Please check the devices of this drive!",
2894/*13*/ "\007\000\002\006\002\010\002"
2895 "GDT HA %u, Array Drive %u: Cache Drive %u failed",
2896/*14*/ "\005\000\002\006\002"
2897 "GDT HA %u, Array Drive %u: FAIL state entered",
2898/*15*/ "\005\000\002\006\002"
2899 "GDT HA %u, Array Drive %u: error",
2900/*16*/ "\007\000\002\006\002\010\002"
2901 "GDT HA %u, Array Drive %u: failed drive replaced by Cache Drive %u",
2902/*17*/ "\005\000\002\006\002"
2903 "GDT HA %u, Array Drive %u: parity build failed",
2904/*18*/ "\005\000\002\006\002"
2905 "GDT HA %u, Array Drive %u: drive rebuild failed",
2906/*19*/ "\005\000\002\010\002"
2907 "GDT HA %u, Test of Hot Fix %u failed",
2908/*20*/ "\005\000\002\006\002"
2909 "GDT HA %u, Array Drive %u: drive build finished successfully",
2910/*21*/ "\005\000\002\006\002"
2911 "GDT HA %u, Array Drive %u: drive rebuild finished successfully",
2912/*22*/ "\007\000\002\006\002\010\002"
2913 "GDT HA %u, Array Drive %u: Hot Fix %u activated",
2914/*23*/ "\005\000\002\006\002"
2915 "GDT HA %u, Host Drive %u: processing of i/o aborted due to serious drive error",
2916/*24*/ "\005\000\002\010\002"
2917 "GDT HA %u, mirror update on Cache Drive %u completed",
2918/*25*/ "\005\000\002\010\002"
2919 "GDT HA %u, mirror update on Cache Drive %lu failed",
2920/*26*/ "\005\000\002\006\002"
2921 "GDT HA %u, Array Drive %u: drive rebuild started",
2922/*27*/ "\005\000\002\012\001"
2923 "GDT HA %u, Fault bus %u: SHELF OK detected",
2924/*28*/ "\005\000\002\012\001"
2925 "GDT HA %u, Fault bus %u: SHELF not OK detected",
2926/*29*/ "\007\000\002\012\001\013\001"
2927 "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug started",
2928/*30*/ "\007\000\002\012\001\013\001"
2929 "GDT HA %u, Fault bus %u, ID %u: new disk detected",
2930/*31*/ "\007\000\002\012\001\013\001"
2931 "GDT HA %u, Fault bus %u, ID %u: old disk detected",
2932/*32*/ "\007\000\002\012\001\013\001"
2933 "GDT HA %u, Fault bus %u, ID %u: plugging an active disk is invalid",
2934/*33*/ "\007\000\002\012\001\013\001"
2935 "GDT HA %u, Fault bus %u, ID %u: invalid device detected",
2936/*34*/ "\011\000\002\012\001\013\001\006\004"
2937 "GDT HA %u, Fault bus %u, ID %u: insufficient disk capacity (%lu MB required)",
2938/*35*/ "\007\000\002\012\001\013\001"
2939 "GDT HA %u, Fault bus %u, ID %u: disk write protected",
2940/*36*/ "\007\000\002\012\001\013\001"
2941 "GDT HA %u, Fault bus %u, ID %u: disk not available",
2942/*37*/ "\007\000\002\012\001\006\004"
2943 "GDT HA %u, Fault bus %u: swap detected (%lu)",
2944/*38*/ "\007\000\002\012\001\013\001"
2945 "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug finished successfully",
2946/*39*/ "\007\000\002\012\001\013\001"
2947 "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug aborted due to user Hot Plug",
2948/*40*/ "\007\000\002\012\001\013\001"
2949 "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug aborted",
2950/*41*/ "\007\000\002\012\001\013\001"
2951 "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug for Hot Fix started",
2952/*42*/ "\005\000\002\006\002"
2953 "GDT HA %u, Array Drive %u: drive build started",
2954/*43*/ "\003\000\002"
2955 "GDT HA %u, DRAM parity error detected",
2956/*44*/ "\005\000\002\006\002"
2957 "GDT HA %u, Mirror Drive %u: update started",
2958/*45*/ "\007\000\002\006\002\010\002"
2959 "GDT HA %u, Mirror Drive %u: Hot Fix %u activated",
2960/*46*/ "\005\000\002\006\002"
2961 "GDT HA %u, Array Drive %u: no matching Pool Hot Fix Drive available",
2962/*47*/ "\005\000\002\006\002"
2963 "GDT HA %u, Array Drive %u: Pool Hot Fix Drive available",
2964/*48*/ "\005\000\002\006\002"
2965 "GDT HA %u, Mirror Drive %u: no matching Pool Hot Fix Drive available",
2966/*49*/ "\005\000\002\006\002"
2967 "GDT HA %u, Mirror Drive %u: Pool Hot Fix Drive available",
2968/*50*/ "\007\000\002\012\001\013\001"
2969 "GDT HA %u, SCSI bus %u, ID %u: IGNORE_WIDE_RESIDUE message received",
2970/*51*/ "\005\000\002\006\002"
2971 "GDT HA %u, Array Drive %u: expand started",
2972/*52*/ "\005\000\002\006\002"
2973 "GDT HA %u, Array Drive %u: expand finished successfully",
2974/*53*/ "\005\000\002\006\002"
2975 "GDT HA %u, Array Drive %u: expand failed",
2976/*54*/ "\003\000\002"
2977 "GDT HA %u, CPU temperature critical",
2978/*55*/ "\003\000\002"
2979 "GDT HA %u, CPU temperature OK",
2980/*56*/ "\005\000\002\006\004"
2981 "GDT HA %u, Host drive %lu created",
2982/*57*/ "\005\000\002\006\002"
2983 "GDT HA %u, Array Drive %u: expand restarted",
2984/*58*/ "\005\000\002\006\002"
2985 "GDT HA %u, Array Drive %u: expand stopped",
2986/*59*/ "\005\000\002\010\002"
2987 "GDT HA %u, Mirror Drive %u: drive build quited",
2988/*60*/ "\005\000\002\006\002"
2989 "GDT HA %u, Array Drive %u: parity build quited",
2990/*61*/ "\005\000\002\006\002"
2991 "GDT HA %u, Array Drive %u: drive rebuild quited",
2992/*62*/ "\005\000\002\006\002"
2993 "GDT HA %u, Array Drive %u: parity verify started",
2994/*63*/ "\005\000\002\006\002"
2995 "GDT HA %u, Array Drive %u: parity verify done",
2996/*64*/ "\005\000\002\006\002"
2997 "GDT HA %u, Array Drive %u: parity verify failed",
2998/*65*/ "\005\000\002\006\002"
2999 "GDT HA %u, Array Drive %u: parity error detected",
3000/*66*/ "\005\000\002\006\002"
3001 "GDT HA %u, Array Drive %u: parity verify quited",
3002/*67*/ "\005\000\002\006\002"
3003 "GDT HA %u, Host Drive %u reserved",
3004/*68*/ "\005\000\002\006\002"
3005 "GDT HA %u, Host Drive %u mounted and released",
3006/*69*/ "\005\000\002\006\002"
3007 "GDT HA %u, Host Drive %u released",
3008/*70*/ "\003\000\002"
3009 "GDT HA %u, DRAM error detected and corrected with ECC",
3010/*71*/ "\003\000\002"
3011 "GDT HA %u, Uncorrectable DRAM error detected with ECC",
3012/*72*/ "\011\000\002\012\001\013\001\014\001"
3013 "GDT HA %u, SCSI bus %u, ID %u, LUN %u: reassigning block",
3014/*73*/ "\005\000\002\006\002"
3015 "GDT HA %u, Host drive %u resetted locally",
3016/*74*/ "\005\000\002\006\002"
3017 "GDT HA %u, Host drive %u resetted remotely",
3018/*75*/ "\003\000\002"
3019 "GDT HA %u, async. status 75 unknown",
3020};
3021
3022
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003023static int gdth_async_event(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 gdth_cmd_str *cmdp;
3026 int cmd_index;
3027
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 cmdp= ha->pccb;
3029 TRACE2(("gdth_async_event() ha %d serv %d\n",
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003030 ha->hanum, ha->service));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
3032 if (ha->service == SCREENSERVICE) {
3033 if (ha->status == MSG_REQUEST) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003034 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 gdth_delay(0);
3036 cmdp->Service = SCREENSERVICE;
3037 cmdp->RequestBuffer = SCREEN_CMND;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003038 cmd_index = gdth_get_cmd_index(ha);
3039 gdth_set_sema0(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 cmdp->OpCode = GDT_READ;
3041 cmdp->BoardNode = LOCALBOARD;
3042 cmdp->u.screen.reserved = 0;
3043 cmdp->u.screen.su.msg.msg_handle= MSG_INV_HANDLE;
3044 cmdp->u.screen.su.msg.msg_addr = ha->msg_phys;
3045 ha->cmd_offs_dpmem = 0;
3046 ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.screen.su.msg.msg_addr)
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003047 + sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 ha->cmd_cnt = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003049 gdth_copy_command(ha);
Christoph Hellwig31481452018-12-12 08:41:21 +01003050 printk("[PCI %d/%d] ",(u16)(ha->brd_phys>>8),
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003051 (u16)((ha->brd_phys>>3)&0x1f));
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003052 gdth_release_event(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 }
3054
3055 } else {
3056 if (ha->type == GDT_PCIMPR &&
3057 (ha->fw_vers & 0xff) >= 0x1a) {
3058 ha->dvr.size = 0;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003059 ha->dvr.eu.async.ionode = ha->hanum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 ha->dvr.eu.async.status = ha->status;
3061 /* severity and event_string already set! */
3062 } else {
3063 ha->dvr.size = sizeof(ha->dvr.eu.async);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003064 ha->dvr.eu.async.ionode = ha->hanum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 ha->dvr.eu.async.service = ha->service;
3066 ha->dvr.eu.async.status = ha->status;
3067 ha->dvr.eu.async.info = ha->info;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003068 *(u32 *)ha->dvr.eu.async.scsi_coord = ha->info2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 }
3070 gdth_store_event( ha, ES_ASYNC, ha->service, &ha->dvr );
3071 gdth_log_event( &ha->dvr, NULL );
3072
3073 /* new host drive from expand? */
3074 if (ha->service == CACHESERVICE && ha->status == 56) {
3075 TRACE2(("gdth_async_event(): new host drive %d created\n",
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003076 (u16)ha->info));
3077 /* gdth_analyse_hdrive(hanum, (u16)ha->info); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 }
3079 }
3080 return 1;
3081}
3082
3083static void gdth_log_event(gdth_evt_data *dvr, char *buffer)
3084{
3085 gdth_stackframe stack;
3086 char *f = NULL;
3087 int i,j;
3088
3089 TRACE2(("gdth_log_event()\n"));
3090 if (dvr->size == 0) {
3091 if (buffer == NULL) {
3092 printk("Adapter %d: %s\n",dvr->eu.async.ionode,dvr->event_string);
3093 } else {
3094 sprintf(buffer,"Adapter %d: %s\n",
3095 dvr->eu.async.ionode,dvr->event_string);
3096 }
3097 } else if (dvr->eu.async.service == CACHESERVICE &&
3098 INDEX_OK(dvr->eu.async.status, async_cache_tab)) {
3099 TRACE2(("GDT: Async. event cache service, event no.: %d\n",
3100 dvr->eu.async.status));
3101
3102 f = async_cache_tab[dvr->eu.async.status];
3103
3104 /* i: parameter to push, j: stack element to fill */
3105 for (j=0,i=1; i < f[0]; i+=2) {
3106 switch (f[i+1]) {
3107 case 4:
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003108 stack.b[j++] = *(u32*)&dvr->eu.stream[(int)f[i]];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 break;
3110 case 2:
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003111 stack.b[j++] = *(u16*)&dvr->eu.stream[(int)f[i]];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 break;
3113 case 1:
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003114 stack.b[j++] = *(u8*)&dvr->eu.stream[(int)f[i]];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 break;
3116 default:
3117 break;
3118 }
3119 }
3120
3121 if (buffer == NULL) {
3122 printk(&f[(int)f[0]],stack);
3123 printk("\n");
3124 } else {
3125 sprintf(buffer,&f[(int)f[0]],stack);
3126 }
3127
3128 } else {
3129 if (buffer == NULL) {
3130 printk("GDT HA %u, Unknown async. event service %d event no. %d\n",
3131 dvr->eu.async.ionode,dvr->eu.async.service,dvr->eu.async.status);
3132 } else {
3133 sprintf(buffer,"GDT HA %u, Unknown async. event service %d event no. %d",
3134 dvr->eu.async.ionode,dvr->eu.async.service,dvr->eu.async.status);
3135 }
3136 }
3137}
3138
3139#ifdef GDTH_STATISTICS
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003140static u8 gdth_timer_running;
James Bottomley2d6f0d02008-05-04 22:34:49 -05003141
Kees Cook79325892017-10-11 17:17:42 -07003142static void gdth_timeout(struct timer_list *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003144 u32 i;
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02003145 struct scsi_cmnd *nscp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 gdth_ha_str *ha;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003147 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
James Bottomley2d6f0d02008-05-04 22:34:49 -05003149 if(unlikely(list_empty(&gdth_instances))) {
3150 gdth_timer_running = 0;
3151 return;
3152 }
Boaz Harroshb31ddd32008-02-27 15:27:16 -08003153
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02003154 ha = list_first_entry(&gdth_instances, gdth_ha_str, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 spin_lock_irqsave(&ha->smp_lock, flags);
3156
3157 for (act_stats=0,i=0; i<GDTH_MAXCMDS; ++i)
3158 if (ha->cmd_tab[i].cmnd != UNUSED_CMND)
3159 ++act_stats;
3160
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02003161 for (act_rq=0,
3162 nscp=ha->req_first; nscp; nscp=(struct scsi_cmnd*)nscp->SCp.ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 ++act_rq;
3164
3165 TRACE2(("gdth_to(): ints %d, ios %d, act_stats %d, act_rq %d\n",
3166 act_ints, act_ios, act_stats, act_rq));
3167 act_ints = act_ios = 0;
3168
3169 gdth_timer.expires = jiffies + 30 * HZ;
3170 add_timer(&gdth_timer);
3171 spin_unlock_irqrestore(&ha->smp_lock, flags);
3172}
James Bottomley2d6f0d02008-05-04 22:34:49 -05003173
3174static void gdth_timer_init(void)
3175{
3176 if (gdth_timer_running)
3177 return;
3178 gdth_timer_running = 1;
3179 TRACE2(("gdth_detect(): Initializing timer !\n"));
3180 gdth_timer.expires = jiffies + HZ;
James Bottomley2d6f0d02008-05-04 22:34:49 -05003181 add_timer(&gdth_timer);
3182}
3183#else
3184static inline void gdth_timer_init(void)
3185{
3186}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187#endif
3188
8e879042005-04-17 15:28:39 -05003189static void __init internal_setup(char *str,int *ints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
Christoph Hellwig31481452018-12-12 08:41:21 +01003191 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 char *cur_str, *argv;
3193
3194 TRACE2(("internal_setup() str %s ints[0] %d\n",
3195 str ? str:"NULL", ints ? ints[0]:0));
3196
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 /* analyse string */
3198 argv = str;
3199 while (argv && (cur_str = strchr(argv, ':'))) {
3200 int val = 0, c = *++cur_str;
3201
3202 if (c == 'n' || c == 'N')
3203 val = 0;
3204 else if (c == 'y' || c == 'Y')
3205 val = 1;
3206 else
3207 val = (int)simple_strtoul(cur_str, NULL, 0);
3208
3209 if (!strncmp(argv, "disable:", 8))
3210 disable = val;
3211 else if (!strncmp(argv, "reserve_mode:", 13))
3212 reserve_mode = val;
3213 else if (!strncmp(argv, "reverse_scan:", 13))
3214 reverse_scan = val;
3215 else if (!strncmp(argv, "hdr_channel:", 12))
3216 hdr_channel = val;
3217 else if (!strncmp(argv, "max_ids:", 8))
3218 max_ids = val;
3219 else if (!strncmp(argv, "rescan:", 7))
3220 rescan = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 else if (!strncmp(argv, "shared_access:", 14))
3222 shared_access = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 else if (!strncmp(argv, "reserve_list:", 13)) {
3224 reserve_list[0] = val;
3225 for (i = 1; i < MAX_RES_ARGS; i++) {
3226 cur_str = strchr(cur_str, ',');
3227 if (!cur_str)
3228 break;
3229 if (!isdigit((int)*++cur_str)) {
3230 --cur_str;
3231 break;
3232 }
3233 reserve_list[i] =
3234 (int)simple_strtoul(cur_str, NULL, 0);
3235 }
3236 if (!cur_str)
3237 break;
3238 argv = ++cur_str;
3239 continue;
3240 }
3241
3242 if ((argv = strchr(argv, ',')))
3243 ++argv;
3244 }
3245}
3246
3247int __init option_setup(char *str)
3248{
3249 int ints[MAXHA];
3250 char *cur = str;
3251 int i = 1;
3252
3253 TRACE2(("option_setup() str %s\n", str ? str:"NULL"));
3254
Roel Kluin6ce00ca2010-03-11 14:09:35 -08003255 while (cur && isdigit(*cur) && i < MAXHA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 ints[i++] = simple_strtoul(cur, NULL, 0);
3257 if ((cur = strchr(cur, ',')) != NULL) cur++;
3258 }
3259
3260 ints[0] = i - 1;
3261 internal_setup(cur, ints);
3262 return 1;
3263}
3264
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003265static const char *gdth_ctr_name(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 TRACE2(("gdth_ctr_name()\n"));
3268
Christoph Hellwig31481452018-12-12 08:41:21 +01003269 if (ha->type == GDT_PCI) {
Jeff Garzik8e9a8a02007-07-17 05:25:17 -04003270 switch (ha->pdev->device) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 case PCI_DEVICE_ID_VORTEX_GDT60x0:
3272 return("GDT6000/6020/6050");
3273 case PCI_DEVICE_ID_VORTEX_GDT6000B:
3274 return("GDT6000B/6010");
3275 }
3276 }
3277 /* new controllers (GDT_PCINEW, GDT_PCIMPR, ..) use board_info IOCTL! */
3278
3279 return("");
3280}
3281
8e879042005-04-17 15:28:39 -05003282static const char *gdth_info(struct Scsi_Host *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283{
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003284 gdth_ha_str *ha = shost_priv(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
3286 TRACE2(("gdth_info()\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 return ((const char *)ha->binfo.type_string);
3288}
3289
Jens Axboe242f9dc2008-09-14 05:55:09 -07003290static enum blk_eh_timer_return gdth_timed_out(struct scsi_cmnd *scp)
3291{
3292 gdth_ha_str *ha = shost_priv(scp->device->host);
3293 struct gdth_cmndinfo *cmndinfo = gdth_cmnd_priv(scp);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003294 u8 b, t;
3295 unsigned long flags;
Christoph Hellwig66005932018-05-29 15:52:29 +02003296 enum blk_eh_timer_return retval = BLK_EH_DONE;
Jens Axboe242f9dc2008-09-14 05:55:09 -07003297
3298 TRACE(("%s() cmd 0x%x\n", scp->cmnd[0], __func__));
3299 b = scp->device->channel;
3300 t = scp->device->id;
3301
3302 /*
3303 * We don't really honor the command timeout, but we try to
3304 * honor 6 times of the actual command timeout! So reset the
3305 * timer if this is less than 6th timeout on this command!
3306 */
3307 if (++cmndinfo->timeout_count < 6)
3308 retval = BLK_EH_RESET_TIMER;
3309
3310 /* Reset the timeout if it is locked IO */
3311 spin_lock_irqsave(&ha->smp_lock, flags);
3312 if ((b != ha->virt_bus && ha->raw[BUS_L2P(ha, b)].lock) ||
3313 (b == ha->virt_bus && t < MAX_HDRIVES && ha->hdr[t].lock)) {
3314 TRACE2(("%s(): locked IO, reset timeout\n", __func__));
3315 retval = BLK_EH_RESET_TIMER;
3316 }
3317 spin_unlock_irqrestore(&ha->smp_lock, flags);
3318
3319 return retval;
3320}
3321
3322
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02003323static int gdth_eh_bus_reset(struct scsi_cmnd *scp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324{
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003325 gdth_ha_str *ha = shost_priv(scp->device->host);
3326 int i;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003327 unsigned long flags;
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02003328 struct scsi_cmnd *cmnd;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003329 u8 b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330
3331 TRACE2(("gdth_eh_bus_reset()\n"));
3332
Christoph Hellwig52759e62007-10-02 22:59:53 +02003333 b = scp->device->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334
3335 /* clear command tab */
3336 spin_lock_irqsave(&ha->smp_lock, flags);
3337 for (i = 0; i < GDTH_MAXCMDS; ++i) {
3338 cmnd = ha->cmd_tab[i].cmnd;
3339 if (!SPECIAL_SCP(cmnd) && cmnd->device->channel == b)
3340 ha->cmd_tab[i].cmnd = UNUSED_CMND;
3341 }
3342 spin_unlock_irqrestore(&ha->smp_lock, flags);
3343
3344 if (b == ha->virt_bus) {
3345 /* host drives */
3346 for (i = 0; i < MAX_HDRIVES; ++i) {
3347 if (ha->hdr[i].present) {
3348 spin_lock_irqsave(&ha->smp_lock, flags);
3349 gdth_polling = TRUE;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003350 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 gdth_delay(0);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003352 if (gdth_internal_cmd(ha, CACHESERVICE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 GDT_CLUST_RESET, i, 0, 0))
3354 ha->hdr[i].cluster_type &= ~CLUSTER_RESERVED;
3355 gdth_polling = FALSE;
3356 spin_unlock_irqrestore(&ha->smp_lock, flags);
3357 }
3358 }
3359 } else {
3360 /* raw devices */
3361 spin_lock_irqsave(&ha->smp_lock, flags);
3362 for (i = 0; i < MAXID; ++i)
3363 ha->raw[BUS_L2P(ha,b)].io_cnt[i] = 0;
3364 gdth_polling = TRUE;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003365 while (gdth_test_busy(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 gdth_delay(0);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003367 gdth_internal_cmd(ha, SCSIRAWSERVICE, GDT_RESET_BUS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 BUS_L2P(ha,b), 0, 0);
3369 gdth_polling = FALSE;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07003370 spin_unlock_irqrestore(&ha->smp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 }
3372 return SUCCESS;
3373}
3374
8e879042005-04-17 15:28:39 -05003375static int gdth_bios_param(struct scsi_device *sdev,struct block_device *bdev,sector_t cap,int *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376{
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003377 u8 b, t;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003378 gdth_ha_str *ha = shost_priv(sdev->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 struct scsi_device *sd;
3380 unsigned capacity;
3381
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 sd = sdev;
3383 capacity = cap;
Christoph Hellwig52759e62007-10-02 22:59:53 +02003384 b = sd->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 t = sd->id;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003386 TRACE2(("gdth_bios_param() ha %d bus %d target %d\n", ha->hanum, b, t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387
3388 if (b != ha->virt_bus || ha->hdr[t].heads == 0) {
3389 /* raw device or host drive without mapping information */
3390 TRACE2(("Evaluate mapping\n"));
3391 gdth_eval_mapping(capacity,&ip[2],&ip[0],&ip[1]);
3392 } else {
3393 ip[0] = ha->hdr[t].heads;
3394 ip[1] = ha->hdr[t].secs;
3395 ip[2] = capacity / ip[0] / ip[1];
3396 }
3397
3398 TRACE2(("gdth_bios_param(): %d heads, %d secs, %d cyls\n",
3399 ip[0],ip[1],ip[2]));
3400 return 0;
3401}
3402
3403
Jeff Garzikf2812332010-11-16 02:10:29 -05003404static int gdth_queuecommand_lck(struct scsi_cmnd *scp,
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02003405 void (*done)(struct scsi_cmnd *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406{
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003407 gdth_ha_str *ha = shost_priv(scp->device->host);
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02003408 struct gdth_cmndinfo *cmndinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409
3410 TRACE(("gdth_queuecommand() cmd 0x%x\n", scp->cmnd[0]));
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02003411
3412 cmndinfo = gdth_get_cmndinfo(ha);
3413 BUG_ON(!cmndinfo);
3414
Matthew Wilcoxb8bff2a2007-10-02 22:40:22 +02003415 scp->scsi_done = done;
Jens Axboe242f9dc2008-09-14 05:55:09 -07003416 cmndinfo->timeout_count = 0;
Boaz Harroshf842b642007-10-02 23:16:01 +02003417 cmndinfo->priority = DEFAULT_PRI;
Boaz Harrosh3892d882007-10-02 23:18:03 +02003418
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02003419 return __gdth_queuecommand(ha, scp, cmndinfo);
3420}
3421
Jeff Garzikf2812332010-11-16 02:10:29 -05003422static DEF_SCSI_QCMD(gdth_queuecommand)
3423
Boaz Harrosh3058d5d2007-10-02 23:14:39 +02003424static int __gdth_queuecommand(gdth_ha_str *ha, struct scsi_cmnd *scp,
3425 struct gdth_cmndinfo *cmndinfo)
3426{
3427 scp->host_scribble = (unsigned char *)cmndinfo;
Boaz Harroshf842b642007-10-02 23:16:01 +02003428 cmndinfo->wait_for_completion = 1;
3429 cmndinfo->phase = -1;
3430 cmndinfo->OpCode = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432#ifdef GDTH_STATISTICS
3433 ++act_ios;
3434#endif
3435
Boaz Harroshf842b642007-10-02 23:16:01 +02003436 gdth_putq(ha, scp, cmndinfo->priority);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003437 gdth_next(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 return 0;
3439}
3440
3441
3442static int gdth_open(struct inode *inode, struct file *filep)
3443{
3444 gdth_ha_str *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
Arnd Bergmannc45d15d2010-06-02 14:28:52 +02003446 mutex_lock(&gdth_mutex);
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02003447 list_for_each_entry(ha, &gdth_instances, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 if (!ha->sdev)
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02003449 ha->sdev = scsi_get_host_dev(ha->shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 }
Arnd Bergmannc45d15d2010-06-02 14:28:52 +02003451 mutex_unlock(&gdth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
3453 TRACE(("gdth_open()\n"));
3454 return 0;
3455}
3456
3457static int gdth_close(struct inode *inode, struct file *filep)
3458{
3459 TRACE(("gdth_close()\n"));
3460 return 0;
3461}
3462
3463static int ioc_event(void __user *arg)
3464{
3465 gdth_ioctl_event evt;
3466 gdth_ha_str *ha;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003467 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02003469 if (copy_from_user(&evt, arg, sizeof(gdth_ioctl_event)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02003471 ha = gdth_find_ha(evt.ionode);
3472 if (!ha)
3473 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474
3475 if (evt.erase == 0xff) {
3476 if (evt.event.event_source == ES_TEST)
3477 evt.event.event_data.size=sizeof(evt.event.event_data.eu.test);
3478 else if (evt.event.event_source == ES_DRIVER)
3479 evt.event.event_data.size=sizeof(evt.event.event_data.eu.driver);
3480 else if (evt.event.event_source == ES_SYNC)
3481 evt.event.event_data.size=sizeof(evt.event.event_data.eu.sync);
3482 else
3483 evt.event.event_data.size=sizeof(evt.event.event_data.eu.async);
3484 spin_lock_irqsave(&ha->smp_lock, flags);
3485 gdth_store_event(ha, evt.event.event_source, evt.event.event_idx,
3486 &evt.event.event_data);
3487 spin_unlock_irqrestore(&ha->smp_lock, flags);
3488 } else if (evt.erase == 0xfe) {
3489 gdth_clear_events();
3490 } else if (evt.erase == 0) {
3491 evt.handle = gdth_read_event(ha, evt.handle, &evt.event);
3492 } else {
3493 gdth_readapp_event(ha, evt.erase, &evt.event);
3494 }
3495 if (copy_to_user(arg, &evt, sizeof(gdth_ioctl_event)))
3496 return -EFAULT;
3497 return 0;
3498}
3499
3500static int ioc_lockdrv(void __user *arg)
3501{
3502 gdth_ioctl_lockdrv ldrv;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003503 u8 i, j;
3504 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 gdth_ha_str *ha;
3506
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02003507 if (copy_from_user(&ldrv, arg, sizeof(gdth_ioctl_lockdrv)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02003509 ha = gdth_find_ha(ldrv.ionode);
3510 if (!ha)
3511 return -EFAULT;
3512
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 for (i = 0; i < ldrv.drive_cnt && i < MAX_HDRIVES; ++i) {
3514 j = ldrv.drives[i];
3515 if (j >= MAX_HDRIVES || !ha->hdr[j].present)
3516 continue;
3517 if (ldrv.lock) {
3518 spin_lock_irqsave(&ha->smp_lock, flags);
3519 ha->hdr[j].lock = 1;
3520 spin_unlock_irqrestore(&ha->smp_lock, flags);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003521 gdth_wait_completion(ha, ha->bus_cnt, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 } else {
3523 spin_lock_irqsave(&ha->smp_lock, flags);
3524 ha->hdr[j].lock = 0;
3525 spin_unlock_irqrestore(&ha->smp_lock, flags);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003526 gdth_next(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 }
3528 }
3529 return 0;
3530}
3531
3532static int ioc_resetdrv(void __user *arg, char *cmnd)
3533{
3534 gdth_ioctl_reset res;
3535 gdth_cmd_str cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 gdth_ha_str *ha;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07003537 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
3539 if (copy_from_user(&res, arg, sizeof(gdth_ioctl_reset)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02003540 res.number >= MAX_HDRIVES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02003542 ha = gdth_find_ha(res.ionode);
3543 if (!ha)
3544 return -EFAULT;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003545
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 if (!ha->hdr[res.number].present)
3547 return 0;
3548 memset(&cmd, 0, sizeof(gdth_cmd_str));
3549 cmd.Service = CACHESERVICE;
3550 cmd.OpCode = GDT_CLUST_RESET;
3551 if (ha->cache_feat & GDT_64BIT)
3552 cmd.u.cache64.DeviceNo = res.number;
3553 else
3554 cmd.u.cache.DeviceNo = res.number;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07003555
3556 rval = __gdth_execute(ha->sdev, &cmd, cmnd, 30, NULL);
3557 if (rval < 0)
3558 return rval;
3559 res.status = rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
3561 if (copy_to_user(arg, &res, sizeof(gdth_ioctl_reset)))
3562 return -EFAULT;
3563 return 0;
3564}
3565
Christoph Hellwig9f475eb2018-12-12 08:41:18 +01003566static void gdth_ioc_cacheservice(gdth_ha_str *ha, gdth_ioctl_general *gen,
3567 u64 paddr)
3568{
3569 if (ha->cache_feat & GDT_64BIT) {
3570 /* copy elements from 32-bit IOCTL structure */
3571 gen->command.u.cache64.BlockCnt = gen->command.u.cache.BlockCnt;
3572 gen->command.u.cache64.BlockNo = gen->command.u.cache.BlockNo;
3573 gen->command.u.cache64.DeviceNo = gen->command.u.cache.DeviceNo;
3574
3575 if (ha->cache_feat & SCATTER_GATHER) {
3576 gen->command.u.cache64.DestAddr = (u64)-1;
3577 gen->command.u.cache64.sg_canz = 1;
3578 gen->command.u.cache64.sg_lst[0].sg_ptr = paddr;
3579 gen->command.u.cache64.sg_lst[0].sg_len = gen->data_len;
3580 gen->command.u.cache64.sg_lst[1].sg_len = 0;
3581 } else {
3582 gen->command.u.cache64.DestAddr = paddr;
3583 gen->command.u.cache64.sg_canz = 0;
3584 }
3585 } else {
3586 if (ha->cache_feat & SCATTER_GATHER) {
3587 gen->command.u.cache.DestAddr = 0xffffffff;
3588 gen->command.u.cache.sg_canz = 1;
3589 gen->command.u.cache.sg_lst[0].sg_ptr = (u32)paddr;
3590 gen->command.u.cache.sg_lst[0].sg_len = gen->data_len;
3591 gen->command.u.cache.sg_lst[1].sg_len = 0;
3592 } else {
3593 gen->command.u.cache.DestAddr = paddr;
3594 gen->command.u.cache.sg_canz = 0;
3595 }
3596 }
3597}
3598
3599static void gdth_ioc_scsiraw(gdth_ha_str *ha, gdth_ioctl_general *gen,
3600 u64 paddr)
3601{
3602 if (ha->raw_feat & GDT_64BIT) {
3603 /* copy elements from 32-bit IOCTL structure */
3604 char cmd[16];
3605
3606 gen->command.u.raw64.sense_len = gen->command.u.raw.sense_len;
3607 gen->command.u.raw64.bus = gen->command.u.raw.bus;
3608 gen->command.u.raw64.lun = gen->command.u.raw.lun;
3609 gen->command.u.raw64.target = gen->command.u.raw.target;
3610 memcpy(cmd, gen->command.u.raw.cmd, 16);
3611 memcpy(gen->command.u.raw64.cmd, cmd, 16);
3612 gen->command.u.raw64.clen = gen->command.u.raw.clen;
3613 gen->command.u.raw64.sdlen = gen->command.u.raw.sdlen;
3614 gen->command.u.raw64.direction = gen->command.u.raw.direction;
3615
3616 /* addresses */
3617 if (ha->raw_feat & SCATTER_GATHER) {
3618 gen->command.u.raw64.sdata = (u64)-1;
3619 gen->command.u.raw64.sg_ranz = 1;
3620 gen->command.u.raw64.sg_lst[0].sg_ptr = paddr;
3621 gen->command.u.raw64.sg_lst[0].sg_len = gen->data_len;
3622 gen->command.u.raw64.sg_lst[1].sg_len = 0;
3623 } else {
3624 gen->command.u.raw64.sdata = paddr;
3625 gen->command.u.raw64.sg_ranz = 0;
3626 }
3627
3628 gen->command.u.raw64.sense_data = paddr + gen->data_len;
3629 } else {
3630 if (ha->raw_feat & SCATTER_GATHER) {
3631 gen->command.u.raw.sdata = 0xffffffff;
3632 gen->command.u.raw.sg_ranz = 1;
3633 gen->command.u.raw.sg_lst[0].sg_ptr = (u32)paddr;
3634 gen->command.u.raw.sg_lst[0].sg_len = gen->data_len;
3635 gen->command.u.raw.sg_lst[1].sg_len = 0;
3636 } else {
3637 gen->command.u.raw.sdata = paddr;
3638 gen->command.u.raw.sg_ranz = 0;
3639 }
3640
3641 gen->command.u.raw.sense_data = (u32)paddr + gen->data_len;
3642 }
3643}
3644
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645static int ioc_general(void __user *arg, char *cmnd)
3646{
Christoph Hellwig9f475eb2018-12-12 08:41:18 +01003647 gdth_ioctl_general gen;
3648 gdth_ha_str *ha;
3649 char *buf = NULL;
Christoph Hellwig463563f2018-12-12 08:41:20 +01003650 dma_addr_t paddr;
Christoph Hellwig9f475eb2018-12-12 08:41:18 +01003651 int rval;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02003652
Christoph Hellwig9f475eb2018-12-12 08:41:18 +01003653 if (copy_from_user(&gen, arg, sizeof(gdth_ioctl_general)))
3654 return -EFAULT;
3655 ha = gdth_find_ha(gen.ionode);
3656 if (!ha)
3657 return -EFAULT;
Dan Carpenterf63ae562010-10-08 09:03:07 +02003658
Christoph Hellwig9f475eb2018-12-12 08:41:18 +01003659 if (gen.data_len > INT_MAX)
3660 return -EINVAL;
3661 if (gen.sense_len > INT_MAX)
3662 return -EINVAL;
3663 if (gen.data_len + gen.sense_len > INT_MAX)
3664 return -EINVAL;
Dan Carpenterf63ae562010-10-08 09:03:07 +02003665
Christoph Hellwig9f475eb2018-12-12 08:41:18 +01003666 if (gen.data_len + gen.sense_len > 0) {
Christoph Hellwig463563f2018-12-12 08:41:20 +01003667 buf = pci_alloc_consistent(ha->pdev,
3668 gen.data_len + gen.sense_len, &paddr);
Christoph Hellwig9f475eb2018-12-12 08:41:18 +01003669 if (!buf)
3670 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
Christoph Hellwig9f475eb2018-12-12 08:41:18 +01003672 rval = -EFAULT;
3673 if (copy_from_user(buf, arg + sizeof(gdth_ioctl_general),
3674 gen.data_len + gen.sense_len))
3675 goto out_free_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676
Christoph Hellwig9f475eb2018-12-12 08:41:18 +01003677 if (gen.command.OpCode == GDT_IOCTL)
3678 gen.command.u.ioctl.p_param = paddr;
3679 else if (gen.command.Service == CACHESERVICE)
3680 gdth_ioc_cacheservice(ha, &gen, paddr);
3681 else if (gen.command.Service == SCSIRAWSERVICE)
3682 gdth_ioc_scsiraw(ha, &gen, paddr);
3683 else
3684 goto out_free_buf;
3685 }
3686
3687 rval = __gdth_execute(ha->sdev, &gen.command, cmnd, gen.timeout,
3688 &gen.info);
3689 if (rval < 0)
3690 goto out_free_buf;
3691 gen.status = rval;
3692
3693 rval = -EFAULT;
3694 if (copy_to_user(arg + sizeof(gdth_ioctl_general), buf,
3695 gen.data_len + gen.sense_len))
3696 goto out_free_buf;
3697 if (copy_to_user(arg, &gen,
3698 sizeof(gdth_ioctl_general) - sizeof(gdth_cmd_str)))
3699 goto out_free_buf;
3700
3701 rval = 0;
3702out_free_buf:
Christoph Hellwig463563f2018-12-12 08:41:20 +01003703 pci_free_consistent(ha->pdev, gen.data_len + gen.sense_len, buf, paddr);
Christoph Hellwig9f475eb2018-12-12 08:41:18 +01003704 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705}
3706
3707static int ioc_hdrlist(void __user *arg, char *cmnd)
3708{
3709 gdth_ioctl_rescan *rsc;
3710 gdth_cmd_str *cmd;
3711 gdth_ha_str *ha;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003712 u8 i;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003713 int rc = -ENOMEM;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07003714 u32 cluster_type = 0;
3715
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 rsc = kmalloc(sizeof(*rsc), GFP_KERNEL);
3717 cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
3718 if (!rsc || !cmd)
Leubner, Achimcbd5f692006-06-09 11:34:29 -07003719 goto free_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
3721 if (copy_from_user(rsc, arg, sizeof(gdth_ioctl_rescan)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02003722 (NULL == (ha = gdth_find_ha(rsc->ionode)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 rc = -EFAULT;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07003724 goto free_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 memset(cmd, 0, sizeof(gdth_cmd_str));
3727
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 for (i = 0; i < MAX_HDRIVES; ++i) {
3729 if (!ha->hdr[i].present) {
3730 rsc->hdr_list[i].bus = 0xff;
3731 continue;
3732 }
3733 rsc->hdr_list[i].bus = ha->virt_bus;
3734 rsc->hdr_list[i].target = i;
3735 rsc->hdr_list[i].lun = 0;
3736 rsc->hdr_list[i].cluster_type = ha->hdr[i].cluster_type;
3737 if (ha->hdr[i].cluster_type & CLUSTER_DRIVE) {
3738 cmd->Service = CACHESERVICE;
3739 cmd->OpCode = GDT_CLUST_INFO;
3740 if (ha->cache_feat & GDT_64BIT)
3741 cmd->u.cache64.DeviceNo = i;
3742 else
3743 cmd->u.cache.DeviceNo = i;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07003744 if (__gdth_execute(ha->sdev, cmd, cmnd, 30, &cluster_type) == S_OK)
3745 rsc->hdr_list[i].cluster_type = cluster_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 }
3747 }
Leubner, Achimcbd5f692006-06-09 11:34:29 -07003748
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 if (copy_to_user(arg, rsc, sizeof(gdth_ioctl_rescan)))
3750 rc = -EFAULT;
3751 else
Leubner, Achimcbd5f692006-06-09 11:34:29 -07003752 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753
3754free_fail:
3755 kfree(rsc);
3756 kfree(cmd);
3757 return rc;
3758}
3759
3760static int ioc_rescan(void __user *arg, char *cmnd)
3761{
3762 gdth_ioctl_rescan *rsc;
3763 gdth_cmd_str *cmd;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003764 u16 i, status, hdr_cnt;
3765 u32 info;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003766 int cyls, hds, secs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 int rc = -ENOMEM;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003768 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 gdth_ha_str *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770
3771 rsc = kmalloc(sizeof(*rsc), GFP_KERNEL);
3772 cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
3773 if (!cmd || !rsc)
Leubner, Achimcbd5f692006-06-09 11:34:29 -07003774 goto free_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775
3776 if (copy_from_user(rsc, arg, sizeof(gdth_ioctl_rescan)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02003777 (NULL == (ha = gdth_find_ha(rsc->ionode)))) {
Leubner, Achimcbd5f692006-06-09 11:34:29 -07003778 rc = -EFAULT;
3779 goto free_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 memset(cmd, 0, sizeof(gdth_cmd_str));
3782
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 if (rsc->flag == 0) {
3784 /* old method: re-init. cache service */
3785 cmd->Service = CACHESERVICE;
3786 if (ha->cache_feat & GDT_64BIT) {
3787 cmd->OpCode = GDT_X_INIT_HOST;
3788 cmd->u.cache64.DeviceNo = LINUX_OS;
3789 } else {
3790 cmd->OpCode = GDT_INIT;
3791 cmd->u.cache.DeviceNo = LINUX_OS;
3792 }
Leubner, Achimcbd5f692006-06-09 11:34:29 -07003793
3794 status = __gdth_execute(ha->sdev, cmd, cmnd, 30, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 i = 0;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003796 hdr_cnt = (status == S_OK ? (u16)info : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 } else {
3798 i = rsc->hdr_no;
3799 hdr_cnt = i + 1;
3800 }
3801
3802 for (; i < hdr_cnt && i < MAX_HDRIVES; ++i) {
3803 cmd->Service = CACHESERVICE;
3804 cmd->OpCode = GDT_INFO;
3805 if (ha->cache_feat & GDT_64BIT)
3806 cmd->u.cache64.DeviceNo = i;
3807 else
3808 cmd->u.cache.DeviceNo = i;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07003809
3810 status = __gdth_execute(ha->sdev, cmd, cmnd, 30, &info);
3811
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 spin_lock_irqsave(&ha->smp_lock, flags);
3813 rsc->hdr_list[i].bus = ha->virt_bus;
3814 rsc->hdr_list[i].target = i;
3815 rsc->hdr_list[i].lun = 0;
3816 if (status != S_OK) {
3817 ha->hdr[i].present = FALSE;
3818 } else {
3819 ha->hdr[i].present = TRUE;
3820 ha->hdr[i].size = info;
3821 /* evaluate mapping */
3822 ha->hdr[i].size &= ~SECS32;
3823 gdth_eval_mapping(ha->hdr[i].size,&cyls,&hds,&secs);
3824 ha->hdr[i].heads = hds;
3825 ha->hdr[i].secs = secs;
3826 /* round size */
3827 ha->hdr[i].size = cyls * hds * secs;
3828 }
3829 spin_unlock_irqrestore(&ha->smp_lock, flags);
3830 if (status != S_OK)
3831 continue;
3832
3833 /* extended info, if GDT_64BIT, for drives > 2 TB */
3834 /* but we need ha->info2, not yet stored in scp->SCp */
3835
3836 /* devtype, cluster info, R/W attribs */
3837 cmd->Service = CACHESERVICE;
3838 cmd->OpCode = GDT_DEVTYPE;
3839 if (ha->cache_feat & GDT_64BIT)
3840 cmd->u.cache64.DeviceNo = i;
3841 else
3842 cmd->u.cache.DeviceNo = i;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07003843
3844 status = __gdth_execute(ha->sdev, cmd, cmnd, 30, &info);
3845
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 spin_lock_irqsave(&ha->smp_lock, flags);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003847 ha->hdr[i].devtype = (status == S_OK ? (u16)info : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 spin_unlock_irqrestore(&ha->smp_lock, flags);
3849
3850 cmd->Service = CACHESERVICE;
3851 cmd->OpCode = GDT_CLUST_INFO;
3852 if (ha->cache_feat & GDT_64BIT)
3853 cmd->u.cache64.DeviceNo = i;
3854 else
3855 cmd->u.cache.DeviceNo = i;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07003856
3857 status = __gdth_execute(ha->sdev, cmd, cmnd, 30, &info);
3858
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 spin_lock_irqsave(&ha->smp_lock, flags);
3860 ha->hdr[i].cluster_type =
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003861 ((status == S_OK && !shared_access) ? (u16)info : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 spin_unlock_irqrestore(&ha->smp_lock, flags);
3863 rsc->hdr_list[i].cluster_type = ha->hdr[i].cluster_type;
3864
3865 cmd->Service = CACHESERVICE;
3866 cmd->OpCode = GDT_RW_ATTRIBS;
3867 if (ha->cache_feat & GDT_64BIT)
3868 cmd->u.cache64.DeviceNo = i;
3869 else
3870 cmd->u.cache.DeviceNo = i;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07003871
3872 status = __gdth_execute(ha->sdev, cmd, cmnd, 30, &info);
3873
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 spin_lock_irqsave(&ha->smp_lock, flags);
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003875 ha->hdr[i].rw_attribs = (status == S_OK ? (u16)info : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 spin_unlock_irqrestore(&ha->smp_lock, flags);
3877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878
3879 if (copy_to_user(arg, rsc, sizeof(gdth_ioctl_rescan)))
3880 rc = -EFAULT;
3881 else
Leubner, Achimcbd5f692006-06-09 11:34:29 -07003882 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883
3884free_fail:
3885 kfree(rsc);
3886 kfree(cmd);
3887 return rc;
3888}
3889
Arnd Bergmannf4927c42010-04-27 00:24:01 +02003890static int gdth_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891{
3892 gdth_ha_str *ha;
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +02003893 struct scsi_cmnd *scp;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003894 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 char cmnd[MAX_COMMAND_SIZE];
3896 void __user *argp = (void __user *)arg;
3897
3898 memset(cmnd, 0xff, 12);
3899
3900 TRACE(("gdth_ioctl() cmd 0x%x\n", cmd));
3901
3902 switch (cmd) {
3903 case GDTIOCTL_CTRCNT:
3904 {
3905 int cnt = gdth_ctr_count;
3906 if (put_user(cnt, (int __user *)argp))
3907 return -EFAULT;
3908 break;
3909 }
3910
3911 case GDTIOCTL_DRVERS:
3912 {
3913 int ver = (GDTH_VERSION<<8) | GDTH_SUBVERSION;
3914 if (put_user(ver, (int __user *)argp))
3915 return -EFAULT;
3916 break;
3917 }
3918
3919 case GDTIOCTL_OSVERS:
3920 {
3921 gdth_ioctl_osvers osv;
3922
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003923 osv.version = (u8)(LINUX_VERSION_CODE >> 16);
3924 osv.subversion = (u8)(LINUX_VERSION_CODE >> 8);
3925 osv.revision = (u16)(LINUX_VERSION_CODE & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 if (copy_to_user(argp, &osv, sizeof(gdth_ioctl_osvers)))
3927 return -EFAULT;
3928 break;
3929 }
3930
3931 case GDTIOCTL_CTRTYPE:
3932 {
3933 gdth_ioctl_ctrtype ctrt;
3934
3935 if (copy_from_user(&ctrt, argp, sizeof(gdth_ioctl_ctrtype)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02003936 (NULL == (ha = gdth_find_ha(ctrt.ionode))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02003938
Christoph Hellwig31481452018-12-12 08:41:21 +01003939 if (ha->type != GDT_PCIMPR) {
3940 ctrt.type = (u8)((ha->stype<<4) + 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 } else {
Christoph Hellwig31481452018-12-12 08:41:21 +01003942 ctrt.type = (ha->oem_id == OEM_ID_INTEL ? 0xfd : 0xfe);
3943 if (ha->stype >= 0x300)
3944 ctrt.ext_type = 0x6000 | ha->pdev->subsystem_device;
3945 else
3946 ctrt.ext_type = 0x6000 | ha->stype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 }
Christoph Hellwig31481452018-12-12 08:41:21 +01003948 ctrt.device_id = ha->pdev->device;
3949 ctrt.sub_device_id = ha->pdev->subsystem_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 ctrt.info = ha->brd_phys;
3951 ctrt.oem_id = ha->oem_id;
3952 if (copy_to_user(argp, &ctrt, sizeof(gdth_ioctl_ctrtype)))
3953 return -EFAULT;
3954 break;
3955 }
3956
3957 case GDTIOCTL_GENERAL:
3958 return ioc_general(argp, cmnd);
3959
3960 case GDTIOCTL_EVENT:
3961 return ioc_event(argp);
3962
3963 case GDTIOCTL_LOCKDRV:
3964 return ioc_lockdrv(argp);
3965
3966 case GDTIOCTL_LOCKCHN:
3967 {
3968 gdth_ioctl_lockchn lchn;
Dave Jones1fe6dbf2010-01-04 10:19:34 -05003969 u8 i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970
3971 if (copy_from_user(&lchn, argp, sizeof(gdth_ioctl_lockchn)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02003972 (NULL == (ha = gdth_find_ha(lchn.ionode))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 return -EFAULT;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02003974
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 i = lchn.channel;
3976 if (i < ha->bus_cnt) {
3977 if (lchn.lock) {
3978 spin_lock_irqsave(&ha->smp_lock, flags);
3979 ha->raw[i].lock = 1;
3980 spin_unlock_irqrestore(&ha->smp_lock, flags);
Jens Axboe242f9dc2008-09-14 05:55:09 -07003981 for (j = 0; j < ha->tid_cnt; ++j)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003982 gdth_wait_completion(ha, i, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 } else {
3984 spin_lock_irqsave(&ha->smp_lock, flags);
3985 ha->raw[i].lock = 0;
3986 spin_unlock_irqrestore(&ha->smp_lock, flags);
Jens Axboe242f9dc2008-09-14 05:55:09 -07003987 for (j = 0; j < ha->tid_cnt; ++j)
Boaz Harrosh45f1a412007-10-02 23:05:53 +02003988 gdth_next(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 }
3990 }
3991 break;
3992 }
3993
3994 case GDTIOCTL_RESCAN:
3995 return ioc_rescan(argp, cmnd);
3996
3997 case GDTIOCTL_HDRLIST:
3998 return ioc_hdrlist(argp, cmnd);
3999
4000 case GDTIOCTL_RESET_BUS:
4001 {
4002 gdth_ioctl_reset res;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004003 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004
4005 if (copy_from_user(&res, argp, sizeof(gdth_ioctl_reset)) ||
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004006 (NULL == (ha = gdth_find_ha(res.ionode))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02004009 scp = kzalloc(sizeof(*scp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 if (!scp)
4011 return -ENOMEM;
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004012 scp->device = ha->sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 scp->cmd_len = 12;
Christoph Hellwig52759e62007-10-02 22:59:53 +02004014 scp->device->channel = res.number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 rval = gdth_eh_bus_reset(scp);
4016 res.status = (rval == SUCCESS ? S_OK : S_GENERR);
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004017 kfree(scp);
Jeff Garzik8d7a5da2007-10-02 22:54:28 +02004018
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 if (copy_to_user(argp, &res, sizeof(gdth_ioctl_reset)))
4020 return -EFAULT;
4021 break;
4022 }
4023
4024 case GDTIOCTL_RESET_DRV:
4025 return ioc_resetdrv(argp, cmnd);
4026
4027 default:
4028 break;
4029 }
4030 return 0;
4031}
4032
Arnd Bergmannf4927c42010-04-27 00:24:01 +02004033static long gdth_unlocked_ioctl(struct file *file, unsigned int cmd,
4034 unsigned long arg)
4035{
4036 int ret;
4037
Arnd Bergmannc45d15d2010-06-02 14:28:52 +02004038 mutex_lock(&gdth_mutex);
Arnd Bergmannf4927c42010-04-27 00:24:01 +02004039 ret = gdth_ioctl(file, cmd, arg);
Arnd Bergmannc45d15d2010-06-02 14:28:52 +02004040 mutex_unlock(&gdth_mutex);
Arnd Bergmannf4927c42010-04-27 00:24:01 +02004041
4042 return ret;
4043}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044
4045/* flush routine */
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004046static void gdth_flush(gdth_ha_str *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047{
4048 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 gdth_cmd_str gdtcmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 char cmnd[MAX_COMMAND_SIZE];
4051 memset(cmnd, 0xff, MAX_COMMAND_SIZE);
4052
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004053 TRACE2(("gdth_flush() hanum %d\n", ha->hanum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 for (i = 0; i < MAX_HDRIVES; ++i) {
4056 if (ha->hdr[i].present) {
4057 gdtcmd.BoardNode = LOCALBOARD;
4058 gdtcmd.Service = CACHESERVICE;
4059 gdtcmd.OpCode = GDT_FLUSH;
4060 if (ha->cache_feat & GDT_64BIT) {
4061 gdtcmd.u.cache64.DeviceNo = i;
4062 gdtcmd.u.cache64.BlockNo = 1;
4063 gdtcmd.u.cache64.sg_canz = 0;
4064 } else {
4065 gdtcmd.u.cache.DeviceNo = i;
4066 gdtcmd.u.cache.BlockNo = 1;
4067 gdtcmd.u.cache.sg_canz = 0;
4068 }
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004069 TRACE2(("gdth_flush(): flush ha %d drive %d\n", ha->hanum, i));
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004070
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004071 gdth_execute(ha->shost, &gdtcmd, cmnd, 30, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 }
4073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074}
4075
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004076/* configure lun */
4077static int gdth_slave_configure(struct scsi_device *sdev)
4078{
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004079 sdev->skip_ms_page_3f = 1;
4080 sdev->skip_ms_page_8 = 1;
4081 return 0;
4082}
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004083
Christoph Hellwig835cc242007-10-02 23:09:56 +02004084static struct scsi_host_template gdth_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 .name = "GDT SCSI Disk Array Controller",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 .info = gdth_info,
4087 .queuecommand = gdth_queuecommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 .eh_bus_reset_handler = gdth_eh_bus_reset,
Leubner, Achimcbd5f692006-06-09 11:34:29 -07004089 .slave_configure = gdth_slave_configure,
Jeff Garzik69916b72007-10-02 22:57:58 +02004090 .bios_param = gdth_bios_param,
Al Viro3e0552e2013-03-31 00:52:08 -04004091 .show_info = gdth_show_info,
4092 .write_info = gdth_set_info,
Jens Axboe242f9dc2008-09-14 05:55:09 -07004093 .eh_timed_out = gdth_timed_out,
Jeff Garzik69916b72007-10-02 22:57:58 +02004094 .proc_name = "gdth",
4095 .can_queue = GDTH_MAXCMDS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 .this_id = -1,
4097 .sg_tablesize = GDTH_MAXSG,
4098 .cmd_per_lun = GDTH_MAXC_P_L,
4099 .unchecked_isa_dma = 1,
Martin K. Petersen54b2b502013-10-23 06:25:40 -04004100 .no_write_same = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101};
4102
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004103static int gdth_pci_probe_one(gdth_pci_str *pcistr, gdth_ha_str **ha_out)
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004104{
4105 struct Scsi_Host *shp;
4106 gdth_ha_str *ha;
4107 dma_addr_t scratch_dma_handle = 0;
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004108 int error, i;
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05004109 struct pci_dev *pdev = pcistr->pdev;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004110
Jeff Garzikcff26802008-02-15 22:20:09 -05004111 *ha_out = NULL;
4112
Christoph Hellwig835cc242007-10-02 23:09:56 +02004113 shp = scsi_host_alloc(&gdth_template, sizeof(gdth_ha_str));
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004114 if (!shp)
4115 return -ENOMEM;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004116 ha = shost_priv(shp);
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004117
4118 error = -ENODEV;
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05004119 if (!gdth_init_pci(pdev, pcistr, ha))
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004120 goto out_host_put;
4121
4122 /* controller found and initialized */
4123 printk("Configuring GDT-PCI HA at %d/%d IRQ %u\n",
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05004124 pdev->bus->number,
4125 PCI_SLOT(pdev->devfn),
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004126 ha->irq);
4127
4128 error = request_irq(ha->irq, gdth_interrupt,
Michael Opdenacker4909cc22014-03-05 06:09:41 +01004129 IRQF_SHARED, "gdth", ha);
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004130 if (error) {
4131 printk("GDT-PCI: Unable to allocate IRQ\n");
4132 goto out_host_put;
4133 }
4134
4135 shp->unchecked_isa_dma = 0;
4136 shp->irq = ha->irq;
4137 shp->dma_channel = 0xff;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004138
Boaz Harrosh884f7fb2007-10-02 23:11:24 +02004139 ha->hanum = gdth_ctr_count++;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004140 ha->shost = shp;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004141
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004142 ha->pccb = &ha->cmdext;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004143 ha->ccb_phys = 0L;
4144
4145 error = -ENOMEM;
4146
4147 ha->pscratch = pci_alloc_consistent(ha->pdev, GDTH_SCRATCH,
4148 &scratch_dma_handle);
4149 if (!ha->pscratch)
4150 goto out_free_irq;
4151 ha->scratch_phys = scratch_dma_handle;
4152
4153 ha->pmsg = pci_alloc_consistent(ha->pdev, sizeof(gdth_msg_str),
4154 &scratch_dma_handle);
4155 if (!ha->pmsg)
4156 goto out_free_pscratch;
4157 ha->msg_phys = scratch_dma_handle;
4158
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004159 ha->scratch_busy = FALSE;
4160 ha->req_first = NULL;
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05004161 ha->tid_cnt = pdev->device >= 0x200 ? MAXID : MAX_HDRIVES;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004162 if (max_ids > 0 && max_ids < ha->tid_cnt)
4163 ha->tid_cnt = max_ids;
4164 for (i = 0; i < GDTH_MAXCMDS; ++i)
4165 ha->cmd_tab[i].cmnd = UNUSED_CMND;
4166 ha->scan_mode = rescan ? 0x10 : 0;
4167
4168 error = -ENODEV;
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004169 if (!gdth_search_drives(ha)) {
4170 printk("GDT-PCI %d: Error during device scan\n", ha->hanum);
Christoph Hellwigd8e1e6a2018-12-12 08:41:26 +01004171 goto out_free_pmsg;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004172 }
4173
4174 if (hdr_channel < 0 || hdr_channel > ha->bus_cnt)
4175 hdr_channel = ha->bus_cnt;
4176 ha->virt_bus = hdr_channel;
4177
4178 /* 64-bit DMA only supported from FW >= x.43 */
4179 if (!(ha->cache_feat & ha->raw_feat & ha->screen_feat & GDT_64BIT) ||
4180 !ha->dma64_support) {
Yang Hongyang284901a2009-04-06 19:01:15 -07004181 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004182 printk(KERN_WARNING "GDT-PCI %d: "
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004183 "Unable to set 32-bit DMA\n", ha->hanum);
Christoph Hellwigd8e1e6a2018-12-12 08:41:26 +01004184 goto out_free_pmsg;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004185 }
4186 } else {
4187 shp->max_cmd_len = 16;
Yang Hongyang6a355282009-04-06 19:01:13 -07004188 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004189 printk("GDT-PCI %d: 64-bit DMA enabled\n", ha->hanum);
Yang Hongyang284901a2009-04-06 19:01:15 -07004190 } else if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004191 printk(KERN_WARNING "GDT-PCI %d: "
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004192 "Unable to set 64/32-bit DMA\n", ha->hanum);
Christoph Hellwigd8e1e6a2018-12-12 08:41:26 +01004193 goto out_free_pmsg;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004194 }
4195 }
4196
4197 shp->max_id = ha->tid_cnt;
4198 shp->max_lun = MAXLUN;
Christoph Hellwig52759e62007-10-02 22:59:53 +02004199 shp->max_channel = ha->bus_cnt;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004200
4201 spin_lock_init(&ha->smp_lock);
Boaz Harrosh45f1a412007-10-02 23:05:53 +02004202 gdth_enable_int(ha);
Christoph Hellwig835cc242007-10-02 23:09:56 +02004203
Jeff Garzik4c9c8d72008-02-15 21:35:26 -05004204 error = scsi_add_host(shp, &pdev->dev);
Christoph Hellwig835cc242007-10-02 23:09:56 +02004205 if (error)
Christoph Hellwigd8e1e6a2018-12-12 08:41:26 +01004206 goto out_free_pmsg;
Christoph Hellwig835cc242007-10-02 23:09:56 +02004207 list_add_tail(&ha->list, &gdth_instances);
Boaz Harrosh61c92812008-02-12 19:35:22 +02004208
Jeff Garzikcff26802008-02-15 22:20:09 -05004209 pci_set_drvdata(ha->pdev, ha);
James Bottomley2d6f0d02008-05-04 22:34:49 -05004210 gdth_timer_init();
Jeff Garzikcff26802008-02-15 22:20:09 -05004211
Boaz Harrosh61c92812008-02-12 19:35:22 +02004212 scsi_scan_host(shp);
4213
Jeff Garzikcff26802008-02-15 22:20:09 -05004214 *ha_out = ha;
4215
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004216 return 0;
4217
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004218 out_free_pmsg:
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004219 pci_free_consistent(ha->pdev, sizeof(gdth_msg_str),
4220 ha->pmsg, ha->msg_phys);
4221 out_free_pscratch:
4222 pci_free_consistent(ha->pdev, GDTH_SCRATCH,
4223 ha->pscratch, ha->scratch_phys);
4224 out_free_irq:
4225 free_irq(ha->irq, ha);
4226 gdth_ctr_count--;
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004227 out_host_put:
Christoph Hellwig835cc242007-10-02 23:09:56 +02004228 scsi_host_put(shp);
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004229 return error;
4230}
Christoph Hellwig8514ef22007-10-02 22:51:06 +02004231
Christoph Hellwig835cc242007-10-02 23:09:56 +02004232static void gdth_remove_one(gdth_ha_str *ha)
4233{
4234 struct Scsi_Host *shp = ha->shost;
4235
4236 TRACE2(("gdth_remove_one()\n"));
4237
4238 scsi_remove_host(shp);
4239
Boaz Harroshb31ddd32008-02-27 15:27:16 -08004240 gdth_flush(ha);
4241
Christoph Hellwig835cc242007-10-02 23:09:56 +02004242 if (ha->sdev) {
4243 scsi_free_host_dev(ha->sdev);
4244 ha->sdev = NULL;
4245 }
4246
Christoph Hellwig835cc242007-10-02 23:09:56 +02004247 if (shp->irq)
4248 free_irq(shp->irq,ha);
4249
Christoph Hellwig835cc242007-10-02 23:09:56 +02004250 if (ha->pscratch)
4251 pci_free_consistent(ha->pdev, GDTH_SCRATCH,
4252 ha->pscratch, ha->scratch_phys);
4253 if (ha->pmsg)
4254 pci_free_consistent(ha->pdev, sizeof(gdth_msg_str),
4255 ha->pmsg, ha->msg_phys);
4256 if (ha->ccb_phys)
4257 pci_unmap_single(ha->pdev,ha->ccb_phys,
4258 sizeof(gdth_cmd_str),PCI_DMA_BIDIRECTIONAL);
4259
4260 scsi_host_put(shp);
4261}
4262
Dave Jones1fe6dbf2010-01-04 10:19:34 -05004263static int gdth_halt(struct notifier_block *nb, unsigned long event, void *buf)
Boaz Harroshb31ddd32008-02-27 15:27:16 -08004264{
4265 gdth_ha_str *ha;
4266
4267 TRACE2(("gdth_halt() event %d\n", (int)event));
4268 if (event != SYS_RESTART && event != SYS_HALT && event != SYS_POWER_OFF)
4269 return NOTIFY_DONE;
4270
4271 list_for_each_entry(ha, &gdth_instances, list)
4272 gdth_flush(ha);
4273
4274 return NOTIFY_OK;
4275}
4276
4277static struct notifier_block gdth_notifier = {
4278 gdth_halt, NULL, 0
4279};
4280
Christoph Hellwig835cc242007-10-02 23:09:56 +02004281static int __init gdth_init(void)
4282{
4283 if (disable) {
4284 printk("GDT-HA: Controller driver disabled from"
4285 " command line !\n");
4286 return 0;
4287 }
4288
4289 printk("GDT-HA: Storage RAID Controller Driver. Version: %s\n",
4290 GDTH_VERSION_STR);
4291
4292 /* initializations */
4293 gdth_polling = TRUE;
4294 gdth_clear_events();
Kees Cook79325892017-10-11 17:17:42 -07004295 timer_setup(&gdth_timer, gdth_timeout, 0);
Christoph Hellwig835cc242007-10-02 23:09:56 +02004296
Christoph Hellwig835cc242007-10-02 23:09:56 +02004297 /* scanning for PCI controllers */
James Bottomleya85591f2008-05-04 22:35:58 -05004298 if (pci_register_driver(&gdth_pci_driver)) {
4299 gdth_ha_str *ha;
4300
4301 list_for_each_entry(ha, &gdth_instances, list)
4302 gdth_remove_one(ha);
4303 return -ENODEV;
4304 }
Christoph Hellwig835cc242007-10-02 23:09:56 +02004305
4306 TRACE2(("gdth_detect() %d controller detected\n", gdth_ctr_count));
Linus Torvaldsc596cc42007-10-15 12:46:16 -07004307
Christoph Hellwig835cc242007-10-02 23:09:56 +02004308 major = register_chrdev(0,"gdth", &gdth_fops);
Christoph Hellwig835cc242007-10-02 23:09:56 +02004309 register_reboot_notifier(&gdth_notifier);
4310 gdth_polling = FALSE;
4311 return 0;
4312}
4313
4314static void __exit gdth_exit(void)
4315{
4316 gdth_ha_str *ha;
4317
Boaz Harroshb31ddd32008-02-27 15:27:16 -08004318 unregister_chrdev(major, "gdth");
4319 unregister_reboot_notifier(&gdth_notifier);
Christoph Hellwig835cc242007-10-02 23:09:56 +02004320
4321#ifdef GDTH_STATISTICS
Boaz Harroshb31ddd32008-02-27 15:27:16 -08004322 del_timer_sync(&gdth_timer);
Christoph Hellwig835cc242007-10-02 23:09:56 +02004323#endif
Boaz Harroshb31ddd32008-02-27 15:27:16 -08004324
James Bottomleya85591f2008-05-04 22:35:58 -05004325 pci_unregister_driver(&gdth_pci_driver);
Jeff Garzikcff26802008-02-15 22:20:09 -05004326
Boaz Harroshb31ddd32008-02-27 15:27:16 -08004327 list_for_each_entry(ha, &gdth_instances, list)
4328 gdth_remove_one(ha);
Christoph Hellwig835cc242007-10-02 23:09:56 +02004329}
4330
4331module_init(gdth_init);
4332module_exit(gdth_exit);
4333
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334#ifndef MODULE
4335__setup("gdth=", option_setup);
4336#endif