Blame view

drivers/scsi/3w-9xxx.c 75.1 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
  /*
     3w-9xxx.c -- 3ware 9000 Storage Controller device driver for Linux.
2c9bce5b4   Adam Radford   scsi: Update 3war...
3
4
     Written By: Adam Radford <aradford@gmail.com>
     Modifications By: Tom Couch
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
5

7a252fe7b   Adam Radford   [SCSI] 3w-9xxx: a...
6
     Copyright (C) 2004-2009 Applied Micro Circuits Corporation.
4deedd845   Adam Radford   [SCSI] 3w-xxxx, 3...
7
     Copyright (C) 2010 LSI Corporation.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; version 2 of the License.
  
     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.
  
     NO WARRANTY
     THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
     CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
     LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
     MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
     solely responsible for determining the appropriateness of using and
     distributing the Program and assumes all risks associated with its
     exercise of rights under this Agreement, including but not limited to
     the risks and costs of program errors, damage to or loss of data,
     programs or equipment, and unavailability or interruption of operations.
  
     DISCLAIMER OF LIABILITY
     NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
     TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
     USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
     HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  
     You should have received a copy of the GNU General Public License
     along with this program; if not, write to the Free Software
     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  
     Bugs/Comments/Suggestions should be mailed to:
2c9bce5b4   Adam Radford   scsi: Update 3war...
43
     aradford@gmail.com
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  
     Note: This version of the driver does not contain a bundled firmware
           image.
  
     History
     -------
     2.26.02.000 - Driver cleanup for kernel submission.
     2.26.02.001 - Replace schedule_timeout() calls with msleep().
     2.26.02.002 - Add support for PAE mode.
                   Add lun support.
                   Fix twa_remove() to free irq handler/unregister_chrdev()
                   before shutting down card.
                   Change to new 'change_queue_depth' api.
                   Fix 'handled=1' ISR usage, remove bogus IRQ check.
                   Remove un-needed eh_abort handler.
                   Add support for embedded firmware error strings.
d327d0823   Adam Radford   [SCSI] 3ware 9000...
60
     2.26.02.003 - Correctly handle single sgl's with use_sg=1.
49bfd8db4   Adam Radford   [SCSI] 3ware 9000...
61
     2.26.02.004 - Add support for 9550SX controllers.
62288f105   Adam Radford   [SCSI] 3ware 9000...
62
     2.26.02.005 - Fix use_sg == 0 mapping on systems with 4GB or higher.
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
63
64
     2.26.02.006 - Fix 9550SX pchip reset timeout.
                   Add big endian support.
1e08dcb39   Adam Radford   [SCSI] 3ware 9000...
65
     2.26.02.007 - Disable local interrupts during kmap/unmap_atomic().
4039c30ef   Adam Radford   [SCSI] 3ware 9000...
66
67
68
     2.26.02.008 - Free irq handler in __twa_shutdown().
                   Serialize reset code.
                   Add support for 9650SE controllers.
0e78d158b   Adam Radford   [SCSI] 3w-9xxx: a...
69
70
     2.26.02.009 - Fix dma mask setting to fallback to 32-bit if 64-bit fails.
     2.26.02.010 - Add support for 9690SA controllers.
3dabec717   Adam Radford   [SCSI] 3w-9xxx: a...
71
72
73
74
     2.26.02.011 - Increase max AENs drained to 256.
                   Add MSI support and "use_msi" module parameter.
                   Fix bug in twa_get_param() on 4GB+.
                   Use pci_resource_len() for ioremap().
7a252fe7b   Adam Radford   [SCSI] 3w-9xxx: a...
75
     2.26.02.012 - Add power management support.
53ca35359   Adam Radford   [SCSI] 3w-9xxx fi...
76
     2.26.02.013 - Fix bug in twa_load_sgl().
4deedd845   Adam Radford   [SCSI] 3w-xxxx, 3...
77
     2.26.02.014 - Force 60 second timeout default.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
78
79
80
81
82
83
84
85
86
87
88
89
  */
  
  #include <linux/module.h>
  #include <linux/reboot.h>
  #include <linux/spinlock.h>
  #include <linux/interrupt.h>
  #include <linux/moduleparam.h>
  #include <linux/errno.h>
  #include <linux/types.h>
  #include <linux/delay.h>
  #include <linux/pci.h>
  #include <linux/time.h>
a12e25bd5   Jes Sorensen   [SCSI] sem2mutex ...
90
  #include <linux/mutex.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
91
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92
93
  #include <asm/io.h>
  #include <asm/irq.h>
7c0f6ba68   Linus Torvalds   Replace <asm/uacc...
94
  #include <linux/uaccess.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
95
96
97
98
99
100
101
  #include <scsi/scsi.h>
  #include <scsi/scsi_host.h>
  #include <scsi/scsi_tcq.h>
  #include <scsi/scsi_cmnd.h>
  #include "3w-9xxx.h"
  
  /* Globals */
4deedd845   Adam Radford   [SCSI] 3w-xxxx, 3...
102
  #define TW_DRIVER_VERSION "2.26.02.014"
c45d15d24   Arnd Bergmann   scsi: autoconvert...
103
  static DEFINE_MUTEX(twa_chrdev_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
105
106
107
108
109
  static TW_Device_Extension *twa_device_extension_list[TW_MAX_SLOT];
  static unsigned int twa_device_extension_count;
  static int twa_major = -1;
  extern struct timezone sys_tz;
  
  /* Module parameters */
4deedd845   Adam Radford   [SCSI] 3w-xxxx, 3...
110
  MODULE_AUTHOR ("LSI");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
111
112
113
  MODULE_DESCRIPTION ("3ware 9000 Storage Controller Linux Driver");
  MODULE_LICENSE("GPL");
  MODULE_VERSION(TW_DRIVER_VERSION);
3dabec717   Adam Radford   [SCSI] 3w-9xxx: a...
114
115
116
  static int use_msi = 0;
  module_param(use_msi, int, S_IRUGO);
  MODULE_PARM_DESC(use_msi, "Use Message Signaled Interrupts.  Default: 0");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
117
118
119
120
121
  /* Function prototypes */
  static void twa_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header);
  static int twa_aen_read_queue(TW_Device_Extension *tw_dev, int request_id);
  static char *twa_aen_severity_lookup(unsigned char severity_code);
  static void twa_aen_sync_time(TW_Device_Extension *tw_dev, int request_id);
f4927c45b   Arnd Bergmann   scsi: Push down B...
122
  static long twa_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  static int twa_chrdev_open(struct inode *inode, struct file *file);
  static int twa_fill_sense(TW_Device_Extension *tw_dev, int request_id, int copy_sense, int print_host);
  static void twa_free_request_id(TW_Device_Extension *tw_dev,int request_id);
  static void twa_get_request_id(TW_Device_Extension *tw_dev, int *request_id);
  static int twa_initconnection(TW_Device_Extension *tw_dev, int message_credits,
   			      u32 set_features, unsigned short current_fw_srl, 
  			      unsigned short current_fw_arch_id, 
  			      unsigned short current_fw_branch, 
  			      unsigned short current_fw_build, 
  			      unsigned short *fw_on_ctlr_srl, 
  			      unsigned short *fw_on_ctlr_arch_id, 
  			      unsigned short *fw_on_ctlr_branch, 
  			      unsigned short *fw_on_ctlr_build, 
  			      u32 *init_connect_result);
0e78d158b   Adam Radford   [SCSI] 3w-9xxx: a...
137
  static void twa_load_sgl(TW_Device_Extension *tw_dev, TW_Command_Full *full_command_packet, int request_id, dma_addr_t dma_handle, int length);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
138
139
140
  static int twa_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds);
  static int twa_poll_status_gone(TW_Device_Extension *tw_dev, u32 flag, int seconds);
  static int twa_post_command_packet(TW_Device_Extension *tw_dev, int request_id, char internal);
0e78d158b   Adam Radford   [SCSI] 3w-9xxx: a...
141
  static int twa_reset_device_extension(TW_Device_Extension *tw_dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
142
  static int twa_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset);
09968e504   Nathan Chancellor   scsi: 3w-sas: 3w-...
143
144
145
  static int twa_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id,
  				   unsigned char *cdb, int use_sg,
  				   TW_SG_Entry *sglistarg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
146
147
  static void twa_scsiop_execute_scsi_complete(TW_Device_Extension *tw_dev, int request_id);
  static char *twa_string_lookup(twa_message_type *table, unsigned int aen_code);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
148
149
150
151
  
  /* Functions */
  
  /* Show some statistics about the card */
ee959b00c   Tony Jones   SCSI: convert str...
152
153
  static ssize_t twa_show_stats(struct device *dev,
  			      struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
154
  {
ee959b00c   Tony Jones   SCSI: convert str...
155
  	struct Scsi_Host *host = class_to_shost(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
  	TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
  	unsigned long flags = 0;
  	ssize_t len;
  
  	spin_lock_irqsave(tw_dev->host->host_lock, flags);
  	len = snprintf(buf, PAGE_SIZE, "3w-9xxx Driver version: %s
  "
  		       "Current commands posted:   %4d
  "
  		       "Max commands posted:       %4d
  "
  		       "Current pending commands:  %4d
  "
  		       "Max pending commands:      %4d
  "
  		       "Last sgl length:           %4d
  "
  		       "Max sgl length:            %4d
  "
  		       "Last sector count:         %4d
  "
  		       "Max sector count:          %4d
  "
  		       "SCSI Host Resets:          %4d
  "
  		       "AEN's:                     %4d
  ", 
  		       TW_DRIVER_VERSION,
  		       tw_dev->posted_request_count,
  		       tw_dev->max_posted_request_count,
  		       tw_dev->pending_request_count,
  		       tw_dev->max_pending_request_count,
  		       tw_dev->sgl_entries,
  		       tw_dev->max_sgl_entries,
  		       tw_dev->sector_count,
  		       tw_dev->max_sector_count,
  		       tw_dev->num_resets,
  		       tw_dev->aen_count);
  	spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
  	return len;
  } /* End twa_show_stats() */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
197
  /* Create sysfs 'stats' entry */
ee959b00c   Tony Jones   SCSI: convert str...
198
  static struct device_attribute twa_host_stats_attr = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
199
200
201
202
203
204
205
206
  	.attr = {
  		.name = 	"stats",
  		.mode =		S_IRUGO,
  	},
  	.show = twa_show_stats
  };
  
  /* Host attributes initializer */
ee959b00c   Tony Jones   SCSI: convert str...
207
  static struct device_attribute *twa_host_attrs[] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
208
209
210
211
212
  	&twa_host_stats_attr,
  	NULL,
  };
  
  /* File operations struct for character device */
00977a59b   Arjan van de Ven   [PATCH] mark stru...
213
  static const struct file_operations twa_fops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
214
  	.owner		= THIS_MODULE,
f4927c45b   Arnd Bergmann   scsi: Push down B...
215
  	.unlocked_ioctl	= twa_chrdev_ioctl,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
216
  	.open		= twa_chrdev_open,
6038f373a   Arnd Bergmann   llseek: automatic...
217
218
  	.release	= NULL,
  	.llseek		= noop_llseek,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
219
  };
15e3d5a28   Christoph Hellwig   3w-9xxx: don't un...
220
221
222
223
224
225
226
227
228
229
  /*
   * The controllers use an inline buffer instead of a mapped SGL for small,
   * single entry buffers.  Note that we treat a zero-length transfer like
   * a mapped SGL.
   */
  static bool twa_command_mapped(struct scsi_cmnd *cmd)
  {
  	return scsi_sg_count(cmd) != 1 ||
  		scsi_bufflen(cmd) >= TW_MIN_SGL_LENGTH;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
230
231
232
233
234
235
236
237
238
239
240
  /* This function will complete an aen request from the isr */
  static int twa_aen_complete(TW_Device_Extension *tw_dev, int request_id)
  {
  	TW_Command_Full *full_command_packet;
  	TW_Command *command_packet;
  	TW_Command_Apache_Header *header;
  	unsigned short aen;
  	int retval = 1;
  
  	header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
  	tw_dev->posted_request_count--;
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
241
  	aen = le16_to_cpu(header->status_block.error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
  	full_command_packet = tw_dev->command_packet_virt[request_id];
  	command_packet = &full_command_packet->command.oldcommand;
  
  	/* First check for internal completion of set param for time sync */
  	if (TW_OP_OUT(command_packet->opcode__sgloffset) == TW_OP_SET_PARAM) {
  		/* Keep reading the queue in case there are more aen's */
  		if (twa_aen_read_queue(tw_dev, request_id))
  			goto out2;
  	        else {
  			retval = 0;
  			goto out;
  		}
  	}
  
  	switch (aen) {
  	case TW_AEN_QUEUE_EMPTY:
  		/* Quit reading the queue if this is the last one */
  		break;
  	case TW_AEN_SYNC_TIME_WITH_HOST:
  		twa_aen_sync_time(tw_dev, request_id);
  		retval = 0;
  		goto out;
  	default:
  		twa_aen_queue_event(tw_dev, header);
  
  		/* If there are more aen's, keep reading the queue */
  		if (twa_aen_read_queue(tw_dev, request_id))
  			goto out2;
  		else {
  			retval = 0;
  			goto out;
  		}
  	}
  	retval = 0;
  out2:
  	tw_dev->state[request_id] = TW_S_COMPLETED;
  	twa_free_request_id(tw_dev, request_id);
  	clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
  out:
  	return retval;
  } /* End twa_aen_complete() */
  
  /* This function will drain aen queue */
  static int twa_aen_drain_queue(TW_Device_Extension *tw_dev, int no_check_reset)
  {
  	int request_id = 0;
09968e504   Nathan Chancellor   scsi: 3w-sas: 3w-...
288
  	unsigned char cdb[TW_MAX_CDB_LEN];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
  	TW_SG_Entry sglist[1];
  	int finished = 0, count = 0;
  	TW_Command_Full *full_command_packet;
  	TW_Command_Apache_Header *header;
  	unsigned short aen;
  	int first_reset = 0, queue = 0, retval = 1;
  
  	if (no_check_reset)
  		first_reset = 0;
  	else
  		first_reset = 1;
  
  	full_command_packet = tw_dev->command_packet_virt[request_id];
  	memset(full_command_packet, 0, sizeof(TW_Command_Full));
  
  	/* Initialize cdb */
  	memset(&cdb, 0, TW_MAX_CDB_LEN);
  	cdb[0] = REQUEST_SENSE; /* opcode */
  	cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
  
  	/* Initialize sglist */
  	memset(&sglist, 0, sizeof(TW_SG_Entry));
  	sglist[0].length = TW_SECTOR_SIZE;
  	sglist[0].address = tw_dev->generic_buffer_phys[request_id];
  
  	if (sglist[0].address & TW_ALIGNMENT_9000_SGL) {
  		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1, "Found unaligned address during AEN drain");
  		goto out;
  	}
  
  	/* Mark internal command */
  	tw_dev->srb[request_id] = NULL;
  
  	do {
  		/* Send command to the board */
  		if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
  			TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2, "Error posting request sense");
  			goto out;
  		}
  
  		/* Now poll for completion */
  		if (twa_poll_response(tw_dev, request_id, 30)) {
  			TW_PRINTK(tw_dev->host, TW_DRIVER, 0x3, "No valid response while draining AEN queue");
  			tw_dev->posted_request_count--;
  			goto out;
  		}
  
  		tw_dev->posted_request_count--;
  		header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
338
  		aen = le16_to_cpu(header->status_block.error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
  		queue = 0;
  		count++;
  
  		switch (aen) {
  		case TW_AEN_QUEUE_EMPTY:
  			if (first_reset != 1)
  				goto out;
  			else
  				finished = 1;
  			break;
  		case TW_AEN_SOFT_RESET:
  			if (first_reset == 0)
  				first_reset = 1;
  			else
  				queue = 1;
  			break;
  		case TW_AEN_SYNC_TIME_WITH_HOST:
  			break;
  		default:
  			queue = 1;
  		}
  
  		/* Now queue an event info */
  		if (queue)
  			twa_aen_queue_event(tw_dev, header);
  	} while ((finished == 0) && (count < TW_MAX_AEN_DRAIN));
  
  	if (count == TW_MAX_AEN_DRAIN)
  		goto out;
  
  	retval = 0;
  out:
  	tw_dev->state[request_id] = TW_S_INITIAL;
  	return retval;
  } /* End twa_aen_drain_queue() */
  
  /* This function will queue an event */
  static void twa_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header)
  {
  	u32 local_time;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
  	TW_Event *event;
  	unsigned short aen;
  	char host[16];
  	char *error_str;
  
  	tw_dev->aen_count++;
  
  	/* Fill out event info */
  	event = tw_dev->event_queue[tw_dev->error_index];
  
  	/* Check for clobber */
  	host[0] = '\0';
  	if (tw_dev->host) {
  		sprintf(host, " scsi%d:", tw_dev->host->host_no);
  		if (event->retrieved == TW_AEN_NOT_RETRIEVED)
  			tw_dev->aen_clobber = 1;
  	}
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
396
  	aen = le16_to_cpu(header->status_block.error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
397
398
399
  	memset(event, 0, sizeof(TW_Event));
  
  	event->severity = TW_SEV_OUT(header->status_block.severity__reserved);
9c88673f9   Arnd Bergmann   scsi: 3ware: fix ...
400
401
  	/* event->time_stamp_sec overflows in y2106 */
  	local_time = (u32)(ktime_get_real_seconds() - (sys_tz.tz_minuteswest * 60));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
402
403
404
405
406
407
408
409
410
411
412
  	event->time_stamp_sec = local_time;
  	event->aen_code = aen;
  	event->retrieved = TW_AEN_NOT_RETRIEVED;
  	event->sequence_id = tw_dev->error_sequence_id;
  	tw_dev->error_sequence_id++;
  
  	/* Check for embedded error string */
  	error_str = &(header->err_specific_desc[strlen(header->err_specific_desc)+1]);
  
  	header->err_specific_desc[sizeof(header->err_specific_desc) - 1] = '\0';
  	event->parameter_len = strlen(header->err_specific_desc);
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
413
  	memcpy(event->parameter_data, header->err_specific_desc, event->parameter_len + (error_str[0] == '\0' ? 0 : (1 + strlen(error_str))));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
  	if (event->severity != TW_AEN_SEVERITY_DEBUG)
  		printk(KERN_WARNING "3w-9xxx:%s AEN: %s (0x%02X:0x%04X): %s:%s.
  ",
  		       host,
  		       twa_aen_severity_lookup(TW_SEV_OUT(header->status_block.severity__reserved)),
  		       TW_MESSAGE_SOURCE_CONTROLLER_EVENT, aen,
  		       error_str[0] == '\0' ? twa_string_lookup(twa_aen_table, aen) : error_str,
  		       header->err_specific_desc);
  	else
  		tw_dev->aen_count--;
  
  	if ((tw_dev->error_index + 1) == TW_Q_LENGTH)
  		tw_dev->event_queue_wrapped = 1;
  	tw_dev->error_index = (tw_dev->error_index + 1 ) % TW_Q_LENGTH;
  } /* End twa_aen_queue_event() */
  
  /* This function will read the aen queue from the isr */
  static int twa_aen_read_queue(TW_Device_Extension *tw_dev, int request_id)
  {
09968e504   Nathan Chancellor   scsi: 3w-sas: 3w-...
433
  	unsigned char cdb[TW_MAX_CDB_LEN];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
  	TW_SG_Entry sglist[1];
  	TW_Command_Full *full_command_packet;
  	int retval = 1;
  
  	full_command_packet = tw_dev->command_packet_virt[request_id];
  	memset(full_command_packet, 0, sizeof(TW_Command_Full));
  
  	/* Initialize cdb */
  	memset(&cdb, 0, TW_MAX_CDB_LEN);
  	cdb[0] = REQUEST_SENSE; /* opcode */
  	cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
  
  	/* Initialize sglist */
  	memset(&sglist, 0, sizeof(TW_SG_Entry));
  	sglist[0].length = TW_SECTOR_SIZE;
  	sglist[0].address = tw_dev->generic_buffer_phys[request_id];
  
  	/* Mark internal command */
  	tw_dev->srb[request_id] = NULL;
  
  	/* Now post the command packet */
  	if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
  		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x4, "Post failed while reading AEN queue");
  		goto out;
  	}
  	retval = 0;
  out:
  	return retval;
  } /* End twa_aen_read_queue() */
  
  /* This function will look up an AEN severity string */
  static char *twa_aen_severity_lookup(unsigned char severity_code)
  {
  	char *retval = NULL;
  
  	if ((severity_code < (unsigned char) TW_AEN_SEVERITY_ERROR) ||
  	    (severity_code > (unsigned char) TW_AEN_SEVERITY_DEBUG))
  		goto out;
  
  	retval = twa_aen_severity_table[severity_code];
  out:
  	return retval;
  } /* End twa_aen_severity_lookup() */
  
  /* This function will sync firmware time with the host time */
  static void twa_aen_sync_time(TW_Device_Extension *tw_dev, int request_id)
  {
  	u32 schedulertime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
482
483
484
  	TW_Command_Full *full_command_packet;
  	TW_Command *command_packet;
  	TW_Param_Apache *param;
bc8f91665   Arnd Bergmann   scsi: 3ware: use ...
485
  	time64_t local_time;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
486
487
488
489
490
491
492
  
  	/* Fill out the command packet */
  	full_command_packet = tw_dev->command_packet_virt[request_id];
  	memset(full_command_packet, 0, sizeof(TW_Command_Full));
  	command_packet = &full_command_packet->command.oldcommand;
  	command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_SET_PARAM);
  	command_packet->request_id = request_id;
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
493
494
  	command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
  	command_packet->byte8_offset.param.sgl[0].length = cpu_to_le32(TW_SECTOR_SIZE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
495
  	command_packet->size = TW_COMMAND_SIZE;
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
496
  	command_packet->byte6_offset.parameter_count = cpu_to_le16(1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
497
498
499
500
  
  	/* Setup the param */
  	param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
  	memset(param, 0, TW_SECTOR_SIZE);
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
501
502
503
  	param->table_id = cpu_to_le16(TW_TIMEKEEP_TABLE | 0x8000); /* Controller time keep table */
  	param->parameter_id = cpu_to_le16(0x3); /* SchedulerTime */
  	param->parameter_size_bytes = cpu_to_le16(4);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
504
505
506
  
  	/* Convert system time in UTC to local time seconds since last 
             Sunday 12:00AM */
bc8f91665   Arnd Bergmann   scsi: 3ware: use ...
507
508
  	local_time = (ktime_get_real_seconds() - (sys_tz.tz_minuteswest * 60));
  	div_u64_rem(local_time - (3 * 86400), 604800, &schedulertime);
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
509
  	schedulertime = cpu_to_le32(schedulertime % 604800);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
  
  	memcpy(param->data, &schedulertime, sizeof(u32));
  
  	/* Mark internal command */
  	tw_dev->srb[request_id] = NULL;
  
  	/* Now post the command */
  	twa_post_command_packet(tw_dev, request_id, 1);
  } /* End twa_aen_sync_time() */
  
  /* This function will allocate memory and check if it is correctly aligned */
  static int twa_allocate_memory(TW_Device_Extension *tw_dev, int size, int which)
  {
  	int i;
  	dma_addr_t dma_handle;
  	unsigned long *cpu_addr;
  	int retval = 1;
b000bced5   Christoph Hellwig   scsi: 3w-9xxx: fu...
527
528
  	cpu_addr = dma_alloc_coherent(&tw_dev->tw_pci_dev->dev,
  			size * TW_Q_LENGTH, &dma_handle, GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
529
530
531
532
533
534
535
  	if (!cpu_addr) {
  		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x5, "Memory allocation failed");
  		goto out;
  	}
  
  	if ((unsigned long)cpu_addr % (TW_ALIGNMENT_9000)) {
  		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x6, "Failed to allocate correctly aligned memory");
b000bced5   Christoph Hellwig   scsi: 3w-9xxx: fu...
536
537
  		dma_free_coherent(&tw_dev->tw_pci_dev->dev, size * TW_Q_LENGTH,
  				cpu_addr, dma_handle);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
  		goto out;
  	}
  
  	memset(cpu_addr, 0, size*TW_Q_LENGTH);
  
  	for (i = 0; i < TW_Q_LENGTH; i++) {
  		switch(which) {
  		case 0:
  			tw_dev->command_packet_phys[i] = dma_handle+(i*size);
  			tw_dev->command_packet_virt[i] = (TW_Command_Full *)((unsigned char *)cpu_addr + (i*size));
  			break;
  		case 1:
  			tw_dev->generic_buffer_phys[i] = dma_handle+(i*size);
  			tw_dev->generic_buffer_virt[i] = (unsigned long *)((unsigned char *)cpu_addr + (i*size));
  			break;
  		}
  	}
  	retval = 0;
  out:
  	return retval;
  } /* End twa_allocate_memory() */
  
  /* This function will check the status register for unexpected bits */
  static int twa_check_bits(u32 status_reg_value)
  {
  	int retval = 1;
  
  	if ((status_reg_value & TW_STATUS_EXPECTED_BITS) != TW_STATUS_EXPECTED_BITS)
  		goto out;
  	if ((status_reg_value & TW_STATUS_UNEXPECTED_BITS) != 0)
  		goto out;
  
  	retval = 0;
  out:
  	return retval;
  } /* End twa_check_bits() */
  
  /* This function will check the srl and decide if we are compatible  */
  static int twa_check_srl(TW_Device_Extension *tw_dev, int *flashed)
  {
  	int retval = 1;
  	unsigned short fw_on_ctlr_srl = 0, fw_on_ctlr_arch_id = 0;
  	unsigned short fw_on_ctlr_branch = 0, fw_on_ctlr_build = 0;
  	u32 init_connect_result = 0;
  
  	if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
  			       TW_EXTENDED_INIT_CONNECT, TW_CURRENT_DRIVER_SRL,
  			       TW_9000_ARCH_ID, TW_CURRENT_DRIVER_BRANCH,
  			       TW_CURRENT_DRIVER_BUILD, &fw_on_ctlr_srl,
  			       &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
  			       &fw_on_ctlr_build, &init_connect_result)) {
  		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x7, "Initconnection failed while checking SRL");
  		goto out;
  	}
4039c30ef   Adam Radford   [SCSI] 3ware 9000...
592
593
594
  	tw_dev->tw_compat_info.working_srl = fw_on_ctlr_srl;
  	tw_dev->tw_compat_info.working_branch = fw_on_ctlr_branch;
  	tw_dev->tw_compat_info.working_build = fw_on_ctlr_build;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
  
  	/* Try base mode compatibility */
  	if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
  		if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
  				       TW_EXTENDED_INIT_CONNECT,
  				       TW_BASE_FW_SRL, TW_9000_ARCH_ID,
  				       TW_BASE_FW_BRANCH, TW_BASE_FW_BUILD,
  				       &fw_on_ctlr_srl, &fw_on_ctlr_arch_id,
  				       &fw_on_ctlr_branch, &fw_on_ctlr_build,
  				       &init_connect_result)) {
  			TW_PRINTK(tw_dev->host, TW_DRIVER, 0xa, "Initconnection (base mode) failed while checking SRL");
  			goto out;
  		}
  		if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
  			if (TW_CURRENT_DRIVER_SRL > fw_on_ctlr_srl) {
  				TW_PRINTK(tw_dev->host, TW_DRIVER, 0x32, "Firmware and driver incompatibility: please upgrade firmware");
  			} else {
  				TW_PRINTK(tw_dev->host, TW_DRIVER, 0x33, "Firmware and driver incompatibility: please upgrade driver");
  			}
  			goto out;
  		}
4039c30ef   Adam Radford   [SCSI] 3ware 9000...
616
617
618
619
620
621
  		tw_dev->tw_compat_info.working_srl = TW_BASE_FW_SRL;
  		tw_dev->tw_compat_info.working_branch = TW_BASE_FW_BRANCH;
  		tw_dev->tw_compat_info.working_build = TW_BASE_FW_BUILD;
  	}
  
  	/* Load rest of compatibility struct */
2195d9690   Rickard Strandqvist   3w-9xxx.c: Cleani...
622
623
  	strlcpy(tw_dev->tw_compat_info.driver_version, TW_DRIVER_VERSION,
  		sizeof(tw_dev->tw_compat_info.driver_version));
4039c30ef   Adam Radford   [SCSI] 3ware 9000...
624
625
626
627
628
629
630
631
632
  	tw_dev->tw_compat_info.driver_srl_high = TW_CURRENT_DRIVER_SRL;
  	tw_dev->tw_compat_info.driver_branch_high = TW_CURRENT_DRIVER_BRANCH;
  	tw_dev->tw_compat_info.driver_build_high = TW_CURRENT_DRIVER_BUILD;
  	tw_dev->tw_compat_info.driver_srl_low = TW_BASE_FW_SRL;
  	tw_dev->tw_compat_info.driver_branch_low = TW_BASE_FW_BRANCH;
  	tw_dev->tw_compat_info.driver_build_low = TW_BASE_FW_BUILD;
  	tw_dev->tw_compat_info.fw_on_ctlr_srl = fw_on_ctlr_srl;
  	tw_dev->tw_compat_info.fw_on_ctlr_branch = fw_on_ctlr_branch;
  	tw_dev->tw_compat_info.fw_on_ctlr_build = fw_on_ctlr_build;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
633
634
635
636
637
638
  	retval = 0;
  out:
  	return retval;
  } /* End twa_check_srl() */
  
  /* This function handles ioctl for the character device */
f4927c45b   Arnd Bergmann   scsi: Push down B...
639
  static long twa_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
640
  {
496ad9aa8   Al Viro   new helper: file_...
641
  	struct inode *inode = file_inode(file);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
642
643
644
645
646
647
648
649
650
651
652
653
  	long timeout;
  	unsigned long *cpu_addr, data_buffer_length_adjusted = 0, flags = 0;
  	dma_addr_t dma_handle;
  	int request_id = 0;
  	unsigned int sequence_id = 0;
  	unsigned char event_index, start_index;
  	TW_Ioctl_Driver_Command driver_command;
  	TW_Ioctl_Buf_Apache *tw_ioctl;
  	TW_Lock *tw_lock;
  	TW_Command_Full *full_command_packet;
  	TW_Compatibility_Info *tw_compat_info;
  	TW_Event *event;
07ffd4ce8   Arnd Bergmann   scsi: 3w-9xxx: re...
654
  	ktime_t current_time;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
655
656
657
  	TW_Device_Extension *tw_dev = twa_device_extension_list[iminor(inode)];
  	int retval = TW_IOCTL_ERROR_OS_EFAULT;
  	void __user *argp = (void __user *)arg;
c45d15d24   Arnd Bergmann   scsi: autoconvert...
658
  	mutex_lock(&twa_chrdev_mutex);
f4927c45b   Arnd Bergmann   scsi: Push down B...
659

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
660
  	/* Only let one of these through at a time */
a12e25bd5   Jes Sorensen   [SCSI] sem2mutex ...
661
  	if (mutex_lock_interruptible(&tw_dev->ioctl_lock)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
662
663
664
665
666
667
668
669
670
  		retval = TW_IOCTL_ERROR_OS_EINTR;
  		goto out;
  	}
  
  	/* First copy down the driver command */
  	if (copy_from_user(&driver_command, argp, sizeof(TW_Ioctl_Driver_Command)))
  		goto out2;
  
  	/* Check data buffer size */
4039c30ef   Adam Radford   [SCSI] 3ware 9000...
671
  	if (driver_command.buffer_length > TW_MAX_SECTORS * 2048) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
  		retval = TW_IOCTL_ERROR_OS_EINVAL;
  		goto out2;
  	}
  
  	/* Hardware can only do multiple of 512 byte transfers */
  	data_buffer_length_adjusted = (driver_command.buffer_length + 511) & ~511;
  
  	/* Now allocate ioctl buf memory */
  	cpu_addr = dma_alloc_coherent(&tw_dev->tw_pci_dev->dev, data_buffer_length_adjusted+sizeof(TW_Ioctl_Buf_Apache) - 1, &dma_handle, GFP_KERNEL);
  	if (!cpu_addr) {
  		retval = TW_IOCTL_ERROR_OS_ENOMEM;
  		goto out2;
  	}
  
  	tw_ioctl = (TW_Ioctl_Buf_Apache *)cpu_addr;
  
  	/* Now copy down the entire ioctl */
  	if (copy_from_user(tw_ioctl, argp, driver_command.buffer_length + sizeof(TW_Ioctl_Buf_Apache) - 1))
  		goto out3;
  
  	/* See which ioctl we are doing */
  	switch (cmd) {
  	case TW_IOCTL_FIRMWARE_PASS_THROUGH:
  		spin_lock_irqsave(tw_dev->host->host_lock, flags);
  		twa_get_request_id(tw_dev, &request_id);
  
  		/* Flag internal command */
  		tw_dev->srb[request_id] = NULL;
  
  		/* Flag chrdev ioctl */
  		tw_dev->chrdev_request_id = request_id;
  
  		full_command_packet = &tw_ioctl->firmware_command;
  
  		/* Load request id and sglist for both command types */
0e78d158b   Adam Radford   [SCSI] 3w-9xxx: a...
707
  		twa_load_sgl(tw_dev, full_command_packet, request_id, dma_handle, data_buffer_length_adjusted);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
708
709
710
711
712
713
714
715
716
717
718
  
  		memcpy(tw_dev->command_packet_virt[request_id], &(tw_ioctl->firmware_command), sizeof(TW_Command_Full));
  
  		/* Now post the command packet to the controller */
  		twa_post_command_packet(tw_dev, request_id, 1);
  		spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
  
  		timeout = TW_IOCTL_CHRDEV_TIMEOUT*HZ;
  
  		/* Now wait for command to complete */
  		timeout = wait_event_timeout(tw_dev->ioctl_wqueue, tw_dev->chrdev_request_id == TW_IOCTL_CHRDEV_FREE, timeout);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
719
720
721
722
723
  		/* We timed out, and didn't get an interrupt */
  		if (tw_dev->chrdev_request_id != TW_IOCTL_CHRDEV_FREE) {
  			/* Now we need to reset the board */
  			printk(KERN_WARNING "3w-9xxx: scsi%d: WARNING: (0x%02X:0x%04X): Character ioctl (0x%x) timed out, resetting card.
  ",
0e78d158b   Adam Radford   [SCSI] 3w-9xxx: a...
724
  			       tw_dev->host->host_no, TW_DRIVER, 0x37,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
725
726
  			       cmd);
  			retval = TW_IOCTL_ERROR_OS_EIO;
0e78d158b   Adam Radford   [SCSI] 3w-9xxx: a...
727
  			twa_reset_device_extension(tw_dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
  			goto out3;
  		}
  
  		/* Now copy in the command packet response */
  		memcpy(&(tw_ioctl->firmware_command), tw_dev->command_packet_virt[request_id], sizeof(TW_Command_Full));
  		
  		/* Now complete the io */
  		spin_lock_irqsave(tw_dev->host->host_lock, flags);
  		tw_dev->posted_request_count--;
  		tw_dev->state[request_id] = TW_S_COMPLETED;
  		twa_free_request_id(tw_dev, request_id);
  		spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
  		break;
  	case TW_IOCTL_GET_COMPATIBILITY_INFO:
  		tw_ioctl->driver_command.status = 0;
06fe9fb41   Dirk Hohndel   tree-wide: fix a ...
743
  		/* Copy compatibility struct into ioctl data buffer */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
744
  		tw_compat_info = (TW_Compatibility_Info *)tw_ioctl->data_buffer;
4039c30ef   Adam Radford   [SCSI] 3ware 9000...
745
  		memcpy(tw_compat_info, &tw_dev->tw_compat_info, sizeof(TW_Compatibility_Info));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
  		break;
  	case TW_IOCTL_GET_LAST_EVENT:
  		if (tw_dev->event_queue_wrapped) {
  			if (tw_dev->aen_clobber) {
  				tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
  				tw_dev->aen_clobber = 0;
  			} else
  				tw_ioctl->driver_command.status = 0;
  		} else {
  			if (!tw_dev->error_index) {
  				tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
  				break;
  			}
  			tw_ioctl->driver_command.status = 0;
  		}
  		event_index = (tw_dev->error_index - 1 + TW_Q_LENGTH) % TW_Q_LENGTH;
  		memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
  		tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
  		break;
  	case TW_IOCTL_GET_FIRST_EVENT:
  		if (tw_dev->event_queue_wrapped) {
  			if (tw_dev->aen_clobber) {
  				tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
  				tw_dev->aen_clobber = 0;
  			} else 
  				tw_ioctl->driver_command.status = 0;
  			event_index = tw_dev->error_index;
  		} else {
  			if (!tw_dev->error_index) {
  				tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
  				break;
  			}
  			tw_ioctl->driver_command.status = 0;
  			event_index = 0;
  		}
  		memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
  		tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
  		break;
  	case TW_IOCTL_GET_NEXT_EVENT:
  		event = (TW_Event *)tw_ioctl->data_buffer;
  		sequence_id = event->sequence_id;
  		tw_ioctl->driver_command.status = 0;
  
  		if (tw_dev->event_queue_wrapped) {
  			if (tw_dev->aen_clobber) {
  				tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
  				tw_dev->aen_clobber = 0;
  			}
  			start_index = tw_dev->error_index;
  		} else {
  			if (!tw_dev->error_index) {
  				tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
  				break;
  			}
  			start_index = 0;
  		}
  		event_index = (start_index + sequence_id - tw_dev->event_queue[start_index]->sequence_id + 1) % TW_Q_LENGTH;
  
  		if (!(tw_dev->event_queue[event_index]->sequence_id > sequence_id)) {
  			if (tw_ioctl->driver_command.status == TW_IOCTL_ERROR_STATUS_AEN_CLOBBER)
  				tw_dev->aen_clobber = 1;
  			tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
  			break;
  		}
  		memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
  		tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
  		break;
  	case TW_IOCTL_GET_PREVIOUS_EVENT:
  		event = (TW_Event *)tw_ioctl->data_buffer;
  		sequence_id = event->sequence_id;
  		tw_ioctl->driver_command.status = 0;
  
  		if (tw_dev->event_queue_wrapped) {
  			if (tw_dev->aen_clobber) {
  				tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
  				tw_dev->aen_clobber = 0;
  			}
  			start_index = tw_dev->error_index;
  		} else {
  			if (!tw_dev->error_index) {
  				tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
  				break;
  			}
  			start_index = 0;
  		}
  		event_index = (start_index + sequence_id - tw_dev->event_queue[start_index]->sequence_id - 1) % TW_Q_LENGTH;
  
  		if (!(tw_dev->event_queue[event_index]->sequence_id < sequence_id)) {
  			if (tw_ioctl->driver_command.status == TW_IOCTL_ERROR_STATUS_AEN_CLOBBER)
  				tw_dev->aen_clobber = 1;
  			tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
  			break;
  		}
  		memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
  		tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
  		break;
  	case TW_IOCTL_GET_LOCK:
  		tw_lock = (TW_Lock *)tw_ioctl->data_buffer;
07ffd4ce8   Arnd Bergmann   scsi: 3w-9xxx: re...
844
  		current_time = ktime_get();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
845

07ffd4ce8   Arnd Bergmann   scsi: 3w-9xxx: re...
846
847
  		if ((tw_lock->force_flag == 1) || (tw_dev->ioctl_sem_lock == 0) ||
  		    ktime_after(current_time, tw_dev->ioctl_time)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
848
  			tw_dev->ioctl_sem_lock = 1;
07ffd4ce8   Arnd Bergmann   scsi: 3w-9xxx: re...
849
  			tw_dev->ioctl_time = ktime_add_ms(current_time, tw_lock->timeout_msec);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
850
851
852
853
  			tw_ioctl->driver_command.status = 0;
  			tw_lock->time_remaining_msec = tw_lock->timeout_msec;
  		} else {
  			tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_LOCKED;
07ffd4ce8   Arnd Bergmann   scsi: 3w-9xxx: re...
854
  			tw_lock->time_remaining_msec = ktime_ms_delta(tw_dev->ioctl_time, current_time);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
  		}
  		break;
  	case TW_IOCTL_RELEASE_LOCK:
  		if (tw_dev->ioctl_sem_lock == 1) {
  			tw_dev->ioctl_sem_lock = 0;
  			tw_ioctl->driver_command.status = 0;
  		} else {
  			tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NOT_LOCKED;
  		}
  		break;
  	default:
  		retval = TW_IOCTL_ERROR_OS_ENOTTY;
  		goto out3;
  	}
  
  	/* Now copy the entire response to userspace */
  	if (copy_to_user(argp, tw_ioctl, sizeof(TW_Ioctl_Buf_Apache) + driver_command.buffer_length - 1) == 0)
  		retval = 0;
  out3:
  	/* Now free ioctl buf memory */
  	dma_free_coherent(&tw_dev->tw_pci_dev->dev, data_buffer_length_adjusted+sizeof(TW_Ioctl_Buf_Apache) - 1, cpu_addr, dma_handle);
  out2:
a12e25bd5   Jes Sorensen   [SCSI] sem2mutex ...
877
  	mutex_unlock(&tw_dev->ioctl_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
878
  out:
c45d15d24   Arnd Bergmann   scsi: autoconvert...
879
  	mutex_unlock(&twa_chrdev_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
880
881
882
883
  	return retval;
  } /* End twa_chrdev_ioctl() */
  
  /* This function handles open for the character device */
f2b9857ee   Jonathan Corbet   Add a bunch of cy...
884
  /* NOTE that this function will race with remove. */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
885
886
887
888
  static int twa_chrdev_open(struct inode *inode, struct file *file)
  {
  	unsigned int minor_number;
  	int retval = TW_IOCTL_ERROR_OS_ENODEV;
c9318a3e0   Wenwen Wang   scsi: 3w-9xxx: fi...
889
890
891
892
  	if (!capable(CAP_SYS_ADMIN)) {
  		retval = -EACCES;
  		goto out;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
  	minor_number = iminor(inode);
  	if (minor_number >= twa_device_extension_count)
  		goto out;
  	retval = 0;
  out:
  	return retval;
  } /* End twa_chrdev_open() */
  
  /* This function will print readable messages from status register errors */
  static int twa_decode_bits(TW_Device_Extension *tw_dev, u32 status_reg_value)
  {
  	int retval = 1;
  
  	/* Check for various error conditions and handle them appropriately */
  	if (status_reg_value & TW_STATUS_PCI_PARITY_ERROR) {
  		TW_PRINTK(tw_dev->host, TW_DRIVER, 0xc, "PCI Parity Error: clearing");
  		writel(TW_CONTROL_CLEAR_PARITY_ERROR, TW_CONTROL_REG_ADDR(tw_dev));
  	}
  
  	if (status_reg_value & TW_STATUS_PCI_ABORT) {
  		TW_PRINTK(tw_dev->host, TW_DRIVER, 0xd, "PCI Abort: clearing");
  		writel(TW_CONTROL_CLEAR_PCI_ABORT, TW_CONTROL_REG_ADDR(tw_dev));
  		pci_write_config_word(tw_dev->tw_pci_dev, PCI_STATUS, TW_PCI_CLEAR_PCI_ABORT);
  	}
  
  	if (status_reg_value & TW_STATUS_QUEUE_ERROR) {
0e78d158b   Adam Radford   [SCSI] 3w-9xxx: a...
919
920
921
  		if (((tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9650SE) &&
  		     (tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9690SA)) ||
  		    (!test_bit(TW_IN_RESET, &tw_dev->flags)))
4039c30ef   Adam Radford   [SCSI] 3ware 9000...
922
  			TW_PRINTK(tw_dev->host, TW_DRIVER, 0xe, "Controller Queue Error: clearing");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
923
924
  		writel(TW_CONTROL_CLEAR_QUEUE_ERROR, TW_CONTROL_REG_ADDR(tw_dev));
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
  	if (status_reg_value & TW_STATUS_MICROCONTROLLER_ERROR) {
  		if (tw_dev->reset_print == 0) {
  			TW_PRINTK(tw_dev->host, TW_DRIVER, 0x10, "Microcontroller Error: clearing");
  			tw_dev->reset_print = 1;
  		}
  		goto out;
  	}
  	retval = 0;
  out:
  	return retval;
  } /* End twa_decode_bits() */
  
  /* This function will empty the response queue */
  static int twa_empty_response_queue(TW_Device_Extension *tw_dev)
  {
  	u32 status_reg_value, response_que_value;
  	int count = 0, retval = 1;
  
  	status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
  
  	while (((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) && (count < TW_MAX_RESPONSE_DRAIN)) {
  		response_que_value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
  		status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
  		count++;
  	}
  	if (count == TW_MAX_RESPONSE_DRAIN)
  		goto out;
  
  	retval = 0;
  out:
  	return retval;
  } /* End twa_empty_response_queue() */
49bfd8db4   Adam Radford   [SCSI] 3ware 9000...
957
958
959
  /* This function will clear the pchip/response queue on 9550SX */
  static int twa_empty_response_queue_large(TW_Device_Extension *tw_dev)
  {
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
960
961
962
  	u32 response_que_value = 0;
  	unsigned long before;
  	int retval = 1;
49bfd8db4   Adam Radford   [SCSI] 3ware 9000...
963

0e78d158b   Adam Radford   [SCSI] 3w-9xxx: a...
964
  	if (tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9000) {
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
965
966
  		before = jiffies;
  		while ((response_que_value & TW_9550SX_DRAIN_COMPLETED) != TW_9550SX_DRAIN_COMPLETED) {
49bfd8db4   Adam Radford   [SCSI] 3ware 9000...
967
  			response_que_value = readl(TW_RESPONSE_QUEUE_REG_ADDR_LARGE(tw_dev));
4039c30ef   Adam Radford   [SCSI] 3ware 9000...
968
  			msleep(1);
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
969
  			if (time_after(jiffies, before + HZ * 30))
49bfd8db4   Adam Radford   [SCSI] 3ware 9000...
970
  				goto out;
49bfd8db4   Adam Radford   [SCSI] 3ware 9000...
971
  		}
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
972
973
  		/* P-chip settle time */
  		msleep(500);
49bfd8db4   Adam Radford   [SCSI] 3ware 9000...
974
975
976
977
978
979
  		retval = 0;
  	} else
  		retval = 0;
  out:
  	return retval;
  } /* End twa_empty_response_queue_large() */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
980
981
982
983
984
985
986
987
988
989
990
991
992
993
  /* This function passes sense keys from firmware to scsi layer */
  static int twa_fill_sense(TW_Device_Extension *tw_dev, int request_id, int copy_sense, int print_host)
  {
  	TW_Command_Full *full_command_packet;
  	unsigned short error;
  	int retval = 1;
  	char *error_str;
  
  	full_command_packet = tw_dev->command_packet_virt[request_id];
  
  	/* Check for embedded error string */
  	error_str = &(full_command_packet->header.err_specific_desc[strlen(full_command_packet->header.err_specific_desc) + 1]);
  
  	/* Don't print error for Logical unit not supported during rollcall */
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
994
  	error = le16_to_cpu(full_command_packet->header.status_block.error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
  	if ((error != TW_ERROR_LOGICAL_UNIT_NOT_SUPPORTED) && (error != TW_ERROR_UNIT_OFFLINE)) {
  		if (print_host)
  			printk(KERN_WARNING "3w-9xxx: scsi%d: ERROR: (0x%02X:0x%04X): %s:%s.
  ",
  			       tw_dev->host->host_no,
  			       TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
  			       full_command_packet->header.status_block.error,
  			       error_str[0] == '\0' ?
  			       twa_string_lookup(twa_error_table,
  						 full_command_packet->header.status_block.error) : error_str,
  			       full_command_packet->header.err_specific_desc);
  		else
  			printk(KERN_WARNING "3w-9xxx: ERROR: (0x%02X:0x%04X): %s:%s.
  ",
  			       TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
  			       full_command_packet->header.status_block.error,
  			       error_str[0] == '\0' ?
  			       twa_string_lookup(twa_error_table,
  						 full_command_packet->header.status_block.error) : error_str,
  			       full_command_packet->header.err_specific_desc);
  	}
  
  	if (copy_sense) {
  		memcpy(tw_dev->srb[request_id]->sense_buffer, full_command_packet->header.sense_data, TW_SENSE_DATA_LENGTH);
  		tw_dev->srb[request_id]->result = (full_command_packet->command.newcommand.status << 1);
  		retval = TW_ISR_DONT_RESULT;
  		goto out;
  	}
  	retval = 0;
  out:
  	return retval;
  } /* End twa_fill_sense() */
  
  /* This function will free up device extension resources */
  static void twa_free_device_extension(TW_Device_Extension *tw_dev)
  {
  	if (tw_dev->command_packet_virt[0])
b000bced5   Christoph Hellwig   scsi: 3w-9xxx: fu...
1032
1033
1034
1035
  		dma_free_coherent(&tw_dev->tw_pci_dev->dev,
  				sizeof(TW_Command_Full) * TW_Q_LENGTH,
  				tw_dev->command_packet_virt[0],
  				tw_dev->command_packet_phys[0]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1036
1037
  
  	if (tw_dev->generic_buffer_virt[0])
b000bced5   Christoph Hellwig   scsi: 3w-9xxx: fu...
1038
1039
1040
1041
  		dma_free_coherent(&tw_dev->tw_pci_dev->dev,
  				TW_SECTOR_SIZE * TW_Q_LENGTH,
  				tw_dev->generic_buffer_virt[0],
  				tw_dev->generic_buffer_phys[0]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1042

c9475cb0c   Jesper Juhl   [PATCH] kfree cle...
1043
  	kfree(tw_dev->event_queue[0]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1044
1045
1046
1047
1048
1049
1050
1051
1052
  } /* End twa_free_device_extension() */
  
  /* This function will free a request id */
  static void twa_free_request_id(TW_Device_Extension *tw_dev, int request_id)
  {
  	tw_dev->free_queue[tw_dev->free_tail] = request_id;
  	tw_dev->state[request_id] = TW_S_FINISHED;
  	tw_dev->free_tail = (tw_dev->free_tail + 1) % TW_Q_LENGTH;
  } /* End twa_free_request_id() */
7f927fcc2   Alexey Dobriyan   [PATCH] Typo fixes
1053
  /* This function will get parameter table entries from the firmware */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1054
1055
1056
1057
1058
  static void *twa_get_param(TW_Device_Extension *tw_dev, int request_id, int table_id, int parameter_id, int parameter_size_bytes)
  {
  	TW_Command_Full *full_command_packet;
  	TW_Command *command_packet;
  	TW_Param_Apache *param;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
  	void *retval = NULL;
  
  	/* Setup the command packet */
  	full_command_packet = tw_dev->command_packet_virt[request_id];
  	memset(full_command_packet, 0, sizeof(TW_Command_Full));
  	command_packet = &full_command_packet->command.oldcommand;
  
  	command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_GET_PARAM);
  	command_packet->size              = TW_COMMAND_SIZE;
  	command_packet->request_id        = request_id;
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
1069
  	command_packet->byte6_offset.block_count = cpu_to_le16(1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1070
1071
1072
1073
  
  	/* Now setup the param */
  	param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
  	memset(param, 0, TW_SECTOR_SIZE);
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
1074
1075
1076
  	param->table_id = cpu_to_le16(table_id | 0x8000);
  	param->parameter_id = cpu_to_le16(parameter_id);
  	param->parameter_size_bytes = cpu_to_le16(parameter_size_bytes);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1077

3dabec717   Adam Radford   [SCSI] 3w-9xxx: a...
1078
  	command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
1079
  	command_packet->byte8_offset.param.sgl[0].length = cpu_to_le32(TW_SECTOR_SIZE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
  
  	/* Post the command packet to the board */
  	twa_post_command_packet(tw_dev, request_id, 1);
  
  	/* Poll for completion */
  	if (twa_poll_response(tw_dev, request_id, 30))
  		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x13, "No valid response during get param")
  	else
  		retval = (void *)&(param->data[0]);
  
  	tw_dev->posted_request_count--;
  	tw_dev->state[request_id] = TW_S_INITIAL;
  
  	return retval;
  } /* End twa_get_param() */
  
  /* This function will assign an available request id */
  static void twa_get_request_id(TW_Device_Extension *tw_dev, int *request_id)
  {
  	*request_id = tw_dev->free_queue[tw_dev->free_head];
  	tw_dev->free_head = (tw_dev->free_head + 1) % TW_Q_LENGTH;
  	tw_dev->state[*request_id] = TW_S_STARTED;
  } /* End twa_get_request_id() */
  
  /* This function will send an initconnection command to controller */
  static int twa_initconnection(TW_Device_Extension *tw_dev, int message_credits,
   			      u32 set_features, unsigned short current_fw_srl, 
  			      unsigned short current_fw_arch_id, 
  			      unsigned short current_fw_branch, 
  			      unsigned short current_fw_build, 
  			      unsigned short *fw_on_ctlr_srl, 
  			      unsigned short *fw_on_ctlr_arch_id, 
  			      unsigned short *fw_on_ctlr_branch, 
  			      unsigned short *fw_on_ctlr_build, 
  			      u32 *init_connect_result)
  {
  	TW_Command_Full *full_command_packet;
  	TW_Initconnect *tw_initconnect;
  	int request_id = 0, retval = 1;
  
  	/* Initialize InitConnection command packet */
  	full_command_packet = tw_dev->command_packet_virt[request_id];
  	memset(full_command_packet, 0, sizeof(TW_Command_Full));
  	full_command_packet->header.header_desc.size_header = 128;
  	
  	tw_initconnect = (TW_Initconnect *)&full_command_packet->command.oldcommand;
  	tw_initconnect->opcode__reserved = TW_OPRES_IN(0, TW_OP_INIT_CONNECTION);
  	tw_initconnect->request_id = request_id;
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
1128
  	tw_initconnect->message_credits = cpu_to_le16(message_credits);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1129
1130
1131
1132
  	tw_initconnect->features = set_features;
  
  	/* Turn on 64-bit sgl support if we need to */
  	tw_initconnect->features |= sizeof(dma_addr_t) > 4 ? 1 : 0;
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
1133
  	tw_initconnect->features = cpu_to_le32(tw_initconnect->features);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1134
1135
  	if (set_features & TW_EXTENDED_INIT_CONNECT) {
  		tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE_EXTENDED;
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
1136
1137
1138
1139
  		tw_initconnect->fw_srl = cpu_to_le16(current_fw_srl);
  		tw_initconnect->fw_arch_id = cpu_to_le16(current_fw_arch_id);
  		tw_initconnect->fw_branch = cpu_to_le16(current_fw_branch);
  		tw_initconnect->fw_build = cpu_to_le16(current_fw_build);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
  	} else 
  		tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE;
  
  	/* Send command packet to the board */
  	twa_post_command_packet(tw_dev, request_id, 1);
  
  	/* Poll for completion */
  	if (twa_poll_response(tw_dev, request_id, 30)) {
  		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x15, "No valid response during init connection");
  	} else {
  		if (set_features & TW_EXTENDED_INIT_CONNECT) {
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
1151
1152
1153
1154
1155
  			*fw_on_ctlr_srl = le16_to_cpu(tw_initconnect->fw_srl);
  			*fw_on_ctlr_arch_id = le16_to_cpu(tw_initconnect->fw_arch_id);
  			*fw_on_ctlr_branch = le16_to_cpu(tw_initconnect->fw_branch);
  			*fw_on_ctlr_build = le16_to_cpu(tw_initconnect->fw_build);
  			*init_connect_result = le32_to_cpu(tw_initconnect->result);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
  		}
  		retval = 0;
  	}
  
  	tw_dev->posted_request_count--;
  	tw_dev->state[request_id] = TW_S_INITIAL;
  
  	return retval;
  } /* End twa_initconnection() */
  
  /* This function will initialize the fields of a device extension */
  static int twa_initialize_device_extension(TW_Device_Extension *tw_dev)
  {
  	int i, retval = 1;
  
  	/* Initialize command packet buffers */
  	if (twa_allocate_memory(tw_dev, sizeof(TW_Command_Full), 0)) {
  		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x16, "Command packet memory allocation failed");
  		goto out;
  	}
  
  	/* Initialize generic buffer */
  	if (twa_allocate_memory(tw_dev, TW_SECTOR_SIZE, 1)) {
  		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x17, "Generic memory allocation failed");
  		goto out;
  	}
  
  	/* Allocate event info space */
dd00cc486   Yoann Padioleau   some kmalloc/mems...
1184
  	tw_dev->event_queue[0] = kcalloc(TW_Q_LENGTH, sizeof(TW_Event), GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1185
1186
1187
1188
  	if (!tw_dev->event_queue[0]) {
  		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x18, "Event info memory allocation failed");
  		goto out;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
  
  	for (i = 0; i < TW_Q_LENGTH; i++) {
  		tw_dev->event_queue[i] = (TW_Event *)((unsigned char *)tw_dev->event_queue[0] + (i * sizeof(TW_Event)));
  		tw_dev->free_queue[i] = i;
  		tw_dev->state[i] = TW_S_INITIAL;
  	}
  
  	tw_dev->pending_head = TW_Q_START;
  	tw_dev->pending_tail = TW_Q_START;
  	tw_dev->free_head = TW_Q_START;
  	tw_dev->free_tail = TW_Q_START;
  	tw_dev->error_sequence_id = 1;
  	tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
a12e25bd5   Jes Sorensen   [SCSI] sem2mutex ...
1202
  	mutex_init(&tw_dev->ioctl_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1203
1204
1205
1206
1207
1208
1209
1210
  	init_waitqueue_head(&tw_dev->ioctl_wqueue);
  
  	retval = 0;
  out:
  	return retval;
  } /* End twa_initialize_device_extension() */
  
  /* This function is the interrupt service routine */
7d12e780e   David Howells   IRQ: Maintain reg...
1211
  static irqreturn_t twa_interrupt(int irq, void *dev_instance)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1212
1213
1214
1215
1216
  {
  	int request_id, error = 0;
  	u32 status_reg_value;
  	TW_Response_Queue response_que;
  	TW_Command_Full *full_command_packet;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
  	TW_Device_Extension *tw_dev = (TW_Device_Extension *)dev_instance;
  	int handled = 0;
  
  	/* Get the per adapter lock */
  	spin_lock(tw_dev->host->host_lock);
  
  	/* Read the registers */
  	status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
  
  	/* Check if this is our interrupt, otherwise bail */
  	if (!(status_reg_value & TW_STATUS_VALID_INTERRUPT))
  		goto twa_interrupt_bail;
  
  	handled = 1;
4039c30ef   Adam Radford   [SCSI] 3ware 9000...
1231
1232
1233
  	/* If we are resetting, bail */
  	if (test_bit(TW_IN_RESET, &tw_dev->flags))
  		goto twa_interrupt_bail;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
  	/* Check controller for errors */
  	if (twa_check_bits(status_reg_value)) {
  		if (twa_decode_bits(tw_dev, status_reg_value)) {
  			TW_CLEAR_ALL_INTERRUPTS(tw_dev);
  			goto twa_interrupt_bail;
  		}
  	}
  
  	/* Handle host interrupt */
  	if (status_reg_value & TW_STATUS_HOST_INTERRUPT)
  		TW_CLEAR_HOST_INTERRUPT(tw_dev);
  
  	/* Handle attention interrupt */
  	if (status_reg_value & TW_STATUS_ATTENTION_INTERRUPT) {
  		TW_CLEAR_ATTENTION_INTERRUPT(tw_dev);
  		if (!(test_and_set_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags))) {
  			twa_get_request_id(tw_dev, &request_id);
  
  			error = twa_aen_read_queue(tw_dev, request_id);
  			if (error) {
  				tw_dev->state[request_id] = TW_S_COMPLETED;
  				twa_free_request_id(tw_dev, request_id);
  				clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
  			}
  		}
  	}
  
  	/* Handle command interrupt */
  	if (status_reg_value & TW_STATUS_COMMAND_INTERRUPT) {
  		TW_MASK_COMMAND_INTERRUPT(tw_dev);
  		/* Drain as many pending commands as we can */
  		while (tw_dev->pending_request_count > 0) {
  			request_id = tw_dev->pending_queue[tw_dev->pending_head];
  			if (tw_dev->state[request_id] != TW_S_PENDING) {
  				TW_PRINTK(tw_dev->host, TW_DRIVER, 0x19, "Found request id that wasn't pending");
  				TW_CLEAR_ALL_INTERRUPTS(tw_dev);
  				goto twa_interrupt_bail;
  			}
  			if (twa_post_command_packet(tw_dev, request_id, 1)==0) {
  				tw_dev->pending_head = (tw_dev->pending_head + 1) % TW_Q_LENGTH;
  				tw_dev->pending_request_count--;
  			} else {
  				/* If we get here, we will continue re-posting on the next command interrupt */
  				break;
  			}
  		}
  	}
  
  	/* Handle response interrupt */
  	if (status_reg_value & TW_STATUS_RESPONSE_INTERRUPT) {
  
  		/* Drain the response queue from the board */
  		while ((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) {
  			/* Complete the response */
  			response_que.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
  			request_id = TW_RESID_OUT(response_que.response_id);
  			full_command_packet = tw_dev->command_packet_virt[request_id];
  			error = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1292
1293
  			/* Check for command packet errors */
  			if (full_command_packet->command.newcommand.status != 0) {
9bcf09108   Harvey Harrison   scsi: fix integer...
1294
  				if (tw_dev->srb[request_id] != NULL) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
  					error = twa_fill_sense(tw_dev, request_id, 1, 1);
  				} else {
  					/* Skip ioctl error prints */
  					if (request_id != tw_dev->chrdev_request_id) {
  						error = twa_fill_sense(tw_dev, request_id, 0, 1);
  					}
  				}
  			}
  
  			/* Check for correct state */
  			if (tw_dev->state[request_id] != TW_S_POSTED) {
9bcf09108   Harvey Harrison   scsi: fix integer...
1306
  				if (tw_dev->srb[request_id] != NULL) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1307
1308
1309
1310
1311
1312
1313
  					TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1a, "Received a request id that wasn't posted");
  					TW_CLEAR_ALL_INTERRUPTS(tw_dev);
  					goto twa_interrupt_bail;
  				}
  			}
  
  			/* Check for internal command completion */
9bcf09108   Harvey Harrison   scsi: fix integer...
1314
  			if (tw_dev->srb[request_id] == NULL) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1315
1316
1317
1318
1319
1320
1321
1322
  				if (request_id != tw_dev->chrdev_request_id) {
  					if (twa_aen_complete(tw_dev, request_id))
  						TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1b, "Error completing AEN during attention interrupt");
  				} else {
  					tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
  					wake_up(&tw_dev->ioctl_wqueue);
  				}
  			} else {
0debe01d3   FUJITA Tomonori   [SCSI] 3w-9xxx: c...
1323
1324
1325
  				struct scsi_cmnd *cmd;
  
  				cmd = tw_dev->srb[request_id];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1326
1327
1328
  				twa_scsiop_execute_scsi_complete(tw_dev, request_id);
  				/* If no error command was a success */
  				if (error == 0) {
0debe01d3   FUJITA Tomonori   [SCSI] 3w-9xxx: c...
1329
  					cmd->result = (DID_OK << 16);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1330
1331
1332
1333
1334
  				}
  
  				/* If error, command failed */
  				if (error == 1) {
  					/* Ask for a host reset */
0debe01d3   FUJITA Tomonori   [SCSI] 3w-9xxx: c...
1335
  					cmd->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1336
1337
1338
  				}
  
  				/* Report residual bytes for single sgl */
0debe01d3   FUJITA Tomonori   [SCSI] 3w-9xxx: c...
1339
1340
1341
  				if ((scsi_sg_count(cmd) <= 1) && (full_command_packet->command.newcommand.status == 0)) {
  					if (full_command_packet->command.newcommand.sg_list[0].length < scsi_bufflen(tw_dev->srb[request_id]))
  						scsi_set_resid(cmd, scsi_bufflen(cmd) - full_command_packet->command.newcommand.sg_list[0].length);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1342
1343
1344
  				}
  
  				/* Now complete the io */
15e3d5a28   Christoph Hellwig   3w-9xxx: don't un...
1345
1346
  				if (twa_command_mapped(cmd))
  					scsi_dma_unmap(cmd);
118c855b5   Christoph Hellwig   3w-9xxx: fix comm...
1347
  				cmd->scsi_done(cmd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1348
1349
1350
  				tw_dev->state[request_id] = TW_S_COMPLETED;
  				twa_free_request_id(tw_dev, request_id);
  				tw_dev->posted_request_count--;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
  			}
  
  			/* Check for valid status after each drain */
  			status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
  			if (twa_check_bits(status_reg_value)) {
  				if (twa_decode_bits(tw_dev, status_reg_value)) {
  					TW_CLEAR_ALL_INTERRUPTS(tw_dev);
  					goto twa_interrupt_bail;
  				}
  			}
  		}
  	}
  
  twa_interrupt_bail:
  	spin_unlock(tw_dev->host->host_lock);
  	return IRQ_RETVAL(handled);
  } /* End twa_interrupt() */
  
  /* This function will load the request id and various sgls for ioctls */
0e78d158b   Adam Radford   [SCSI] 3w-9xxx: a...
1370
  static void twa_load_sgl(TW_Device_Extension *tw_dev, TW_Command_Full *full_command_packet, int request_id, dma_addr_t dma_handle, int length)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1371
1372
1373
1374
  {
  	TW_Command *oldcommand;
  	TW_Command_Apache *newcommand;
  	TW_SG_Entry *sgl;
0e78d158b   Adam Radford   [SCSI] 3w-9xxx: a...
1375
1376
1377
1378
  	unsigned int pae = 0;
  
  	if ((sizeof(long) < 8) && (sizeof(dma_addr_t) > 4))
  		pae = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1379
1380
1381
  
  	if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
  		newcommand = &full_command_packet->command.newcommand;
4039c30ef   Adam Radford   [SCSI] 3ware 9000...
1382
1383
  		newcommand->request_id__lunl =
  			cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->request_id__lunl), request_id));
53ca35359   Adam Radford   [SCSI] 3w-9xxx fi...
1384
1385
1386
1387
  		if (length) {
  			newcommand->sg_list[0].address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1);
  			newcommand->sg_list[0].length = cpu_to_le32(length);
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1388
  		newcommand->sgl_entries__lunh =
53ca35359   Adam Radford   [SCSI] 3w-9xxx fi...
1389
  			cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->sgl_entries__lunh), length ? 1 : 0));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1390
1391
1392
1393
1394
1395
  	} else {
  		oldcommand = &full_command_packet->command.oldcommand;
  		oldcommand->request_id = request_id;
  
  		if (TW_SGL_OUT(oldcommand->opcode__sgloffset)) {
  			/* Load the sg list */
0e78d158b   Adam Radford   [SCSI] 3w-9xxx: a...
1396
1397
1398
1399
  			if (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)
  				sgl = (TW_SG_Entry *)((u32 *)oldcommand+oldcommand->size - (sizeof(TW_SG_Entry)/4) + pae);
  			else
  				sgl = (TW_SG_Entry *)((u32 *)oldcommand+TW_SGL_OUT(oldcommand->opcode__sgloffset));
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
1400
1401
  			sgl->address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1);
  			sgl->length = cpu_to_le32(length);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1402

0e78d158b   Adam Radford   [SCSI] 3w-9xxx: a...
1403
  			oldcommand->size += pae;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1404
1405
1406
  		}
  	}
  } /* End twa_load_sgl() */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
  /* This function will poll for a response interrupt of a request */
  static int twa_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds)
  {
  	int retval = 1, found = 0, response_request_id;
  	TW_Response_Queue response_queue;
  	TW_Command_Full *full_command_packet = tw_dev->command_packet_virt[request_id];
  
  	if (twa_poll_status_gone(tw_dev, TW_STATUS_RESPONSE_QUEUE_EMPTY, seconds) == 0) {
  		response_queue.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
  		response_request_id = TW_RESID_OUT(response_queue.response_id);
  		if (request_id != response_request_id) {
  			TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1e, "Found unexpected request id while polling for response");
  			goto out;
  		}
  		if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
  			if (full_command_packet->command.newcommand.status != 0) {
  				/* bad response */
  				twa_fill_sense(tw_dev, request_id, 0, 0);
  				goto out;
  			}
  			found = 1;
  		} else {
  			if (full_command_packet->command.oldcommand.status != 0) {
  				/* bad response */
  				twa_fill_sense(tw_dev, request_id, 0, 0);
  				goto out;
  			}
  			found = 1;
  		}
  	}
  
  	if (found)
  		retval = 0;
  out:
  	return retval;
  } /* End twa_poll_response() */
  
  /* This function will poll the status register for a flag */
  static int twa_poll_status(TW_Device_Extension *tw_dev, u32 flag, int seconds)
  {
  	u32 status_reg_value; 
  	unsigned long before;
  	int retval = 1;
  
  	status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
  	before = jiffies;
  
  	if (twa_check_bits(status_reg_value))
  		twa_decode_bits(tw_dev, status_reg_value);
  
  	while ((status_reg_value & flag) != flag) {
  		status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
  
  		if (twa_check_bits(status_reg_value))
  			twa_decode_bits(tw_dev, status_reg_value);
  
  		if (time_after(jiffies, before + HZ * seconds))
  			goto out;
  
  		msleep(50);
  	}
  	retval = 0;
  out:
  	return retval;
  } /* End twa_poll_status() */
  
  /* This function will poll the status register for disappearance of a flag */
  static int twa_poll_status_gone(TW_Device_Extension *tw_dev, u32 flag, int seconds)
  {
  	u32 status_reg_value;
  	unsigned long before;
  	int retval = 1;
  
  	status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
  	before = jiffies;
  
  	if (twa_check_bits(status_reg_value))
  		twa_decode_bits(tw_dev, status_reg_value);
  
  	while ((status_reg_value & flag) != 0) {
  		status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
  		if (twa_check_bits(status_reg_value))
  			twa_decode_bits(tw_dev, status_reg_value);
  
  		if (time_after(jiffies, before + HZ * seconds))
  			goto out;
  
  		msleep(50);
  	}
  	retval = 0;
  out:
  	return retval;
  } /* End twa_poll_status_gone() */
  
  /* This function will attempt to post a command packet to the board */
  static int twa_post_command_packet(TW_Device_Extension *tw_dev, int request_id, char internal)
  {
  	u32 status_reg_value;
  	dma_addr_t command_que_value;
  	int retval = 1;
  
  	command_que_value = tw_dev->command_packet_phys[request_id];
4039c30ef   Adam Radford   [SCSI] 3ware 9000...
1509
1510
  
  	/* For 9650SE write low 4 bytes first */
0e78d158b   Adam Radford   [SCSI] 3w-9xxx: a...
1511
1512
  	if ((tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
  	    (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)) {
4039c30ef   Adam Radford   [SCSI] 3ware 9000...
1513
1514
1515
  		command_que_value += TW_COMMAND_OFFSET;
  		writel((u32)command_que_value, TW_COMMAND_QUEUE_REG_ADDR_LARGE(tw_dev));
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
  	status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
  
  	if (twa_check_bits(status_reg_value))
  		twa_decode_bits(tw_dev, status_reg_value);
  
  	if (((tw_dev->pending_request_count > 0) && (tw_dev->state[request_id] != TW_S_PENDING)) || (status_reg_value & TW_STATUS_COMMAND_QUEUE_FULL)) {
  
  		/* Only pend internal driver commands */
  		if (!internal) {
  			retval = SCSI_MLQUEUE_HOST_BUSY;
  			goto out;
  		}
  
  		/* Couldn't post the command packet, so we do it later */
  		if (tw_dev->state[request_id] != TW_S_PENDING) {
  			tw_dev->state[request_id] = TW_S_PENDING;
  			tw_dev->pending_request_count++;
  			if (tw_dev->pending_request_count > tw_dev->max_pending_request_count) {
  				tw_dev->max_pending_request_count = tw_dev->pending_request_count;
  			}
  			tw_dev->pending_queue[tw_dev->pending_tail] = request_id;
  			tw_dev->pending_tail = (tw_dev->pending_tail + 1) % TW_Q_LENGTH;
  		}
  		TW_UNMASK_COMMAND_INTERRUPT(tw_dev);
  		goto out;
  	} else {
0e78d158b   Adam Radford   [SCSI] 3w-9xxx: a...
1542
1543
  		if ((tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
  		    (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)) {
4039c30ef   Adam Radford   [SCSI] 3ware 9000...
1544
1545
  			/* Now write upper 4 bytes */
  			writel((u32)((u64)command_que_value >> 32), TW_COMMAND_QUEUE_REG_ADDR_LARGE(tw_dev) + 0x4);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1546
  		} else {
4039c30ef   Adam Radford   [SCSI] 3ware 9000...
1547
1548
1549
1550
1551
1552
1553
  			if (sizeof(dma_addr_t) > 4) {
  				command_que_value += TW_COMMAND_OFFSET;
  				writel((u32)command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
  				writel((u32)((u64)command_que_value >> 32), TW_COMMAND_QUEUE_REG_ADDR(tw_dev) + 0x4);
  			} else {
  				writel(TW_COMMAND_OFFSET + command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
  		}
  		tw_dev->state[request_id] = TW_S_POSTED;
  		tw_dev->posted_request_count++;
  		if (tw_dev->posted_request_count > tw_dev->max_posted_request_count) {
  			tw_dev->max_posted_request_count = tw_dev->posted_request_count;
  		}
  	}
  	retval = 0;
  out:
  	return retval;
  } /* End twa_post_command_packet() */
  
  /* This function will reset a device extension */
0e78d158b   Adam Radford   [SCSI] 3w-9xxx: a...
1567
  static int twa_reset_device_extension(TW_Device_Extension *tw_dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
  {
  	int i = 0;
  	int retval = 1;
  	unsigned long flags = 0;
  
  	set_bit(TW_IN_RESET, &tw_dev->flags);
  	TW_DISABLE_INTERRUPTS(tw_dev);
  	TW_MASK_COMMAND_INTERRUPT(tw_dev);
  	spin_lock_irqsave(tw_dev->host->host_lock, flags);
  
  	/* Abort all requests that are in progress */
  	for (i = 0; i < TW_Q_LENGTH; i++) {
  		if ((tw_dev->state[i] != TW_S_FINISHED) &&
  		    (tw_dev->state[i] != TW_S_INITIAL) &&
  		    (tw_dev->state[i] != TW_S_COMPLETED)) {
  			if (tw_dev->srb[i]) {
118c855b5   Christoph Hellwig   3w-9xxx: fix comm...
1584
1585
1586
  				struct scsi_cmnd *cmd = tw_dev->srb[i];
  
  				cmd->result = (DID_RESET << 16);
15e3d5a28   Christoph Hellwig   3w-9xxx: don't un...
1587
1588
  				if (twa_command_mapped(cmd))
  					scsi_dma_unmap(cmd);
118c855b5   Christoph Hellwig   3w-9xxx: fix comm...
1589
  				cmd->scsi_done(cmd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
  			}
  		}
  	}
  
  	/* Reset queues and counts */
  	for (i = 0; i < TW_Q_LENGTH; i++) {
  		tw_dev->free_queue[i] = i;
  		tw_dev->state[i] = TW_S_INITIAL;
  	}
  	tw_dev->free_head = TW_Q_START;
  	tw_dev->free_tail = TW_Q_START;
  	tw_dev->posted_request_count = 0;
  	tw_dev->pending_request_count = 0;
  	tw_dev->pending_head = TW_Q_START;
  	tw_dev->pending_tail = TW_Q_START;
  	tw_dev->reset_print = 0;
  
  	spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
  
  	if (twa_reset_sequence(tw_dev, 1))
  		goto out;
  
  	TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
4039c30ef   Adam Radford   [SCSI] 3ware 9000...
1613
1614
  	clear_bit(TW_IN_RESET, &tw_dev->flags);
  	tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1615

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
  	retval = 0;
  out:
  	return retval;
  } /* End twa_reset_device_extension() */
  
  /* This function will reset a controller */
  static int twa_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset)
  {
  	int tries = 0, retval = 1, flashed = 0, do_soft_reset = soft_reset;
  
  	while (tries < TW_MAX_RESET_TRIES) {
49bfd8db4   Adam Radford   [SCSI] 3ware 9000...
1627
  		if (do_soft_reset) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1628
  			TW_SOFT_RESET(tw_dev);
49bfd8db4   Adam Radford   [SCSI] 3ware 9000...
1629
1630
1631
1632
1633
1634
1635
1636
  			/* Clear pchip/response queue on 9550SX */
  			if (twa_empty_response_queue_large(tw_dev)) {
  				TW_PRINTK(tw_dev->host, TW_DRIVER, 0x36, "Response queue (large) empty failed during reset sequence");
  				do_soft_reset = 1;
  				tries++;
  				continue;
  			}
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
  
  		/* Make sure controller is in a good state */
  		if (twa_poll_status(tw_dev, TW_STATUS_MICROCONTROLLER_READY | (do_soft_reset == 1 ? TW_STATUS_ATTENTION_INTERRUPT : 0), 60)) {
  			TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1f, "Microcontroller not ready during reset sequence");
  			do_soft_reset = 1;
  			tries++;
  			continue;
  		}
  
  		/* Empty response queue */
  		if (twa_empty_response_queue(tw_dev)) {
  			TW_PRINTK(tw_dev->host, TW_DRIVER, 0x20, "Response queue empty failed during reset sequence");
  			do_soft_reset = 1;
  			tries++;
  			continue;
  		}
  
  		flashed = 0;
  
  		/* Check for compatibility/flash */
  		if (twa_check_srl(tw_dev, &flashed)) {
  			TW_PRINTK(tw_dev->host, TW_DRIVER, 0x21, "Compatibility check failed during reset sequence");
  			do_soft_reset = 1;
  			tries++;
  			continue;
  		} else {
  			if (flashed) {
  				tries++;
  				continue;
  			}
  		}
  
  		/* Drain the AEN queue */
  		if (twa_aen_drain_queue(tw_dev, soft_reset)) {
  			TW_PRINTK(tw_dev->host, TW_DRIVER, 0x22, "AEN drain failed during reset sequence");
  			do_soft_reset = 1;
  			tries++;
  			continue;
  		}
  
  		/* If we got here, controller is in a good state */
  		retval = 0;
  		goto out;
  	}
  out:
  	return retval;
  } /* End twa_reset_sequence() */
  
  /* This funciton returns unit geometry in cylinders/heads/sectors */
  static int twa_scsi_biosparam(struct scsi_device *sdev, struct block_device *bdev, sector_t capacity, int geom[])
  {
  	int heads, sectors, cylinders;
  	TW_Device_Extension *tw_dev;
  
  	tw_dev = (TW_Device_Extension *)sdev->host->hostdata;
  
  	if (capacity >= 0x200000) {
  		heads = 255;
  		sectors = 63;
  		cylinders = sector_div(capacity, heads * sectors);
  	} else {
  		heads = 64;
  		sectors = 32;
  		cylinders = sector_div(capacity, heads * sectors);
  	}
  
  	geom[0] = heads;
  	geom[1] = sectors;
  	geom[2] = cylinders;
  
  	return 0;
  } /* End twa_scsi_biosparam() */
  
  /* This is the new scsi eh reset function */
  static int twa_scsi_eh_reset(struct scsi_cmnd *SCpnt)
  {
  	TW_Device_Extension *tw_dev = NULL;
  	int retval = FAILED;
  
  	tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1717
  	tw_dev->num_resets++;
017560fca   Jeff Garzik   [SCSI] use sfoo_p...
1718
1719
1720
1721
  	sdev_printk(KERN_WARNING, SCpnt->device,
  		"WARNING: (0x%02X:0x%04X): Command (0x%x) timed out, resetting card.
  ",
  		TW_DRIVER, 0x2c, SCpnt->cmnd[0]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1722

4039c30ef   Adam Radford   [SCSI] 3ware 9000...
1723
1724
  	/* Make sure we are not issuing an ioctl or resetting from ioctl */
  	mutex_lock(&tw_dev->ioctl_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1725
  	/* Now reset the card and some of the device extension data */
0e78d158b   Adam Radford   [SCSI] 3w-9xxx: a...
1726
  	if (twa_reset_device_extension(tw_dev)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1727
1728
1729
1730
1731
1732
  		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2b, "Controller reset failed during scsi host reset");
  		goto out;
  	}
  
  	retval = SUCCESS;
  out:
4039c30ef   Adam Radford   [SCSI] 3ware 9000...
1733
  	mutex_unlock(&tw_dev->ioctl_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1734
1735
1736
1737
  	return retval;
  } /* End twa_scsi_eh_reset() */
  
  /* This is the main scsi queue function to handle scsi opcodes */
f281233d3   Jeff Garzik   SCSI host lock pu...
1738
  static int twa_scsi_queue_lck(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1739
1740
1741
  {
  	int request_id, retval;
  	TW_Device_Extension *tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
4039c30ef   Adam Radford   [SCSI] 3ware 9000...
1742
1743
1744
1745
1746
  	/* If we are resetting due to timed out ioctl, report as busy */
  	if (test_bit(TW_IN_RESET, &tw_dev->flags)) {
  		retval = SCSI_MLQUEUE_HOST_BUSY;
  		goto out;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1747
  	/* Check if this FW supports luns */
4039c30ef   Adam Radford   [SCSI] 3ware 9000...
1748
  	if ((SCpnt->device->lun != 0) && (tw_dev->tw_compat_info.working_srl < TW_FW_SRL_LUNS_SUPPORTED)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
  		SCpnt->result = (DID_BAD_TARGET << 16);
  		done(SCpnt);
  		retval = 0;
  		goto out;
  	}
  
  	/* Save done function into scsi_cmnd struct */
  	SCpnt->scsi_done = done;
  		
  	/* Get a free request id */
  	twa_get_request_id(tw_dev, &request_id);
  
  	/* Save the scsi command for use by the ISR */
  	tw_dev->srb[request_id] = SCpnt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1763
1764
1765
  	retval = twa_scsiop_execute_scsi(tw_dev, request_id, NULL, 0, NULL);
  	switch (retval) {
  	case SCSI_MLQUEUE_HOST_BUSY:
15e3d5a28   Christoph Hellwig   3w-9xxx: don't un...
1766
1767
  		if (twa_command_mapped(SCpnt))
  			scsi_dma_unmap(SCpnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1768
1769
1770
  		twa_free_request_id(tw_dev, request_id);
  		break;
  	case 1:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1771
  		SCpnt->result = (DID_ERROR << 16);
15e3d5a28   Christoph Hellwig   3w-9xxx: don't un...
1772
1773
  		if (twa_command_mapped(SCpnt))
  			scsi_dma_unmap(SCpnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1774
  		done(SCpnt);
118c855b5   Christoph Hellwig   3w-9xxx: fix comm...
1775
1776
  		tw_dev->state[request_id] = TW_S_COMPLETED;
  		twa_free_request_id(tw_dev, request_id);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1777
1778
1779
1780
1781
  		retval = 0;
  	}
  out:
  	return retval;
  } /* End twa_scsi_queue() */
f281233d3   Jeff Garzik   SCSI host lock pu...
1782
  static DEF_SCSI_QCMD(twa_scsi_queue)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1783
  /* This function hands scsi cdb's to the firmware */
09968e504   Nathan Chancellor   scsi: 3w-sas: 3w-...
1784
1785
1786
  static int twa_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id,
  				   unsigned char *cdb, int use_sg,
  				   TW_SG_Entry *sglistarg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1787
1788
1789
1790
1791
1792
  {
  	TW_Command_Full *full_command_packet;
  	TW_Command_Apache *command_packet;
  	u32 num_sectors = 0x0;
  	int i, sg_count;
  	struct scsi_cmnd *srb = NULL;
0debe01d3   FUJITA Tomonori   [SCSI] 3w-9xxx: c...
1793
  	struct scatterlist *sglist = NULL, *sg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1794
1795
1796
  	int retval = 1;
  
  	if (tw_dev->srb[request_id]) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1797
  		srb = tw_dev->srb[request_id];
0debe01d3   FUJITA Tomonori   [SCSI] 3w-9xxx: c...
1798
1799
  		if (scsi_sglist(srb))
  			sglist = scsi_sglist(srb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
  	}
  
  	/* Initialize command packet */
  	full_command_packet = tw_dev->command_packet_virt[request_id];
  	full_command_packet->header.header_desc.size_header = 128;
  	full_command_packet->header.status_block.error = 0;
  	full_command_packet->header.status_block.severity__reserved = 0;
  
  	command_packet = &full_command_packet->command.newcommand;
  	command_packet->status = 0;
  	command_packet->opcode__reserved = TW_OPRES_IN(0, TW_OP_EXECUTE_SCSI);
  
  	/* We forced 16 byte cdb use earlier */
  	if (!cdb)
  		memcpy(command_packet->cdb, srb->cmnd, TW_MAX_CDB_LEN);
  	else
  		memcpy(command_packet->cdb, cdb, TW_MAX_CDB_LEN);
  
  	if (srb) {
  		command_packet->unit = srb->device->id;
  		command_packet->request_id__lunl =
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
1821
  			cpu_to_le16(TW_REQ_LUN_IN(srb->device->lun, request_id));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1822
1823
  	} else {
  		command_packet->request_id__lunl =
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
1824
  			cpu_to_le16(TW_REQ_LUN_IN(0, request_id));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1825
1826
1827
1828
1829
1830
1831
  		command_packet->unit = 0;
  	}
  
  	command_packet->sgl_offset = 16;
  
  	if (!sglistarg) {
  		/* Map sglist from scsi layer to cmd packet */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1832

0debe01d3   FUJITA Tomonori   [SCSI] 3w-9xxx: c...
1833
  		if (scsi_sg_count(srb)) {
15e3d5a28   Christoph Hellwig   3w-9xxx: don't un...
1834
  			if (!twa_command_mapped(srb)) {
035f5e065   FUJITA Tomonori   [SCSI] 3w-9xxx: u...
1835
1836
1837
1838
1839
  				if (srb->sc_data_direction == DMA_TO_DEVICE ||
  				    srb->sc_data_direction == DMA_BIDIRECTIONAL)
  					scsi_sg_copy_to_buffer(srb,
  							       tw_dev->generic_buffer_virt[request_id],
  							       TW_SECTOR_SIZE);
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
1840
1841
  				command_packet->sg_list[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
  				command_packet->sg_list[0].length = cpu_to_le32(TW_MIN_SGL_LENGTH);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1842
  			} else {
118c855b5   Christoph Hellwig   3w-9xxx: fix comm...
1843
1844
  				sg_count = scsi_dma_map(srb);
  				if (sg_count < 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1845
  					goto out;
0debe01d3   FUJITA Tomonori   [SCSI] 3w-9xxx: c...
1846
1847
1848
  				scsi_for_each_sg(srb, sg, sg_count, i) {
  					command_packet->sg_list[i].address = TW_CPU_TO_SGL(sg_dma_address(sg));
  					command_packet->sg_list[i].length = cpu_to_le32(sg_dma_len(sg));
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
1849
  					if (command_packet->sg_list[i].address & TW_CPU_TO_SGL(TW_ALIGNMENT_9000_SGL)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1850
1851
1852
1853
1854
  						TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2e, "Found unaligned sgl address during execute scsi");
  						goto out;
  					}
  				}
  			}
0debe01d3   FUJITA Tomonori   [SCSI] 3w-9xxx: c...
1855
  			command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN((srb->device->lun >> 4), scsi_sg_count(tw_dev->srb[request_id])));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1856
1857
1858
1859
  		}
  	} else {
  		/* Internal cdb post */
  		for (i = 0; i < use_sg; i++) {
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
1860
1861
1862
  			command_packet->sg_list[i].address = TW_CPU_TO_SGL(sglistarg[i].address);
  			command_packet->sg_list[i].length = cpu_to_le32(sglistarg[i].length);
  			if (command_packet->sg_list[i].address & TW_CPU_TO_SGL(TW_ALIGNMENT_9000_SGL)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1863
1864
1865
1866
  				TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2f, "Found unaligned sgl address during internal post");
  				goto out;
  			}
  		}
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
1867
  		command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN(0, use_sg));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
  	}
  
  	if (srb) {
  		if (srb->cmnd[0] == READ_6 || srb->cmnd[0] == WRITE_6)
  			num_sectors = (u32)srb->cmnd[4];
  
  		if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10)
  			num_sectors = (u32)srb->cmnd[8] | ((u32)srb->cmnd[7] << 8);
  	}
  
  	/* Update sector statistic */
  	tw_dev->sector_count = num_sectors;
  	if (tw_dev->sector_count > tw_dev->max_sector_count)
  		tw_dev->max_sector_count = tw_dev->sector_count;
  
  	/* Update SG statistics */
  	if (srb) {
0debe01d3   FUJITA Tomonori   [SCSI] 3w-9xxx: c...
1885
  		tw_dev->sgl_entries = scsi_sg_count(tw_dev->srb[request_id]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
  		if (tw_dev->sgl_entries > tw_dev->max_sgl_entries)
  			tw_dev->max_sgl_entries = tw_dev->sgl_entries;
  	}
  
  	/* Now post the command to the board */
  	if (srb) {
  		retval = twa_post_command_packet(tw_dev, request_id, 0);
  	} else {
  		twa_post_command_packet(tw_dev, request_id, 1);
  		retval = 0;
  	}
  out:
  	return retval;
  } /* End twa_scsiop_execute_scsi() */
  
  /* This function completes an execute scsi operation */
  static void twa_scsiop_execute_scsi_complete(TW_Device_Extension *tw_dev, int request_id)
  {
0debe01d3   FUJITA Tomonori   [SCSI] 3w-9xxx: c...
1904
  	struct scsi_cmnd *cmd = tw_dev->srb[request_id];
0debe01d3   FUJITA Tomonori   [SCSI] 3w-9xxx: c...
1905

15e3d5a28   Christoph Hellwig   3w-9xxx: don't un...
1906
  	if (!twa_command_mapped(cmd) &&
0debe01d3   FUJITA Tomonori   [SCSI] 3w-9xxx: c...
1907
1908
  	    (cmd->sc_data_direction == DMA_FROM_DEVICE ||
  	     cmd->sc_data_direction == DMA_BIDIRECTIONAL)) {
b1192d5eb   FUJITA Tomonori   [SCSI] 3w-9xxx: e...
1909
  		if (scsi_sg_count(cmd) == 1) {
035f5e065   FUJITA Tomonori   [SCSI] 3w-9xxx: u...
1910
  			void *buf = tw_dev->generic_buffer_virt[request_id];
035f5e065   FUJITA Tomonori   [SCSI] 3w-9xxx: u...
1911
  			scsi_sg_copy_from_buffer(cmd, buf, TW_SECTOR_SIZE);
d327d0823   Adam Radford   [SCSI] 3ware 9000...
1912
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1913
1914
1915
1916
1917
1918
1919
1920
  	}
  } /* End twa_scsiop_execute_scsi_complete() */
  
  /* This function tells the controller to shut down */
  static void __twa_shutdown(TW_Device_Extension *tw_dev)
  {
  	/* Disable interrupts */
  	TW_DISABLE_INTERRUPTS(tw_dev);
4039c30ef   Adam Radford   [SCSI] 3ware 9000...
1921
1922
  	/* Free up the IRQ */
  	free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
  	printk(KERN_WARNING "3w-9xxx: Shutting down host %d.
  ", tw_dev->host->host_no);
  
  	/* Tell the card we are shutting down */
  	if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
  		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x31, "Connection shutdown failed");
  	} else {
  		printk(KERN_WARNING "3w-9xxx: Shutdown complete.
  ");
  	}
  
  	/* Clear all interrupts just before exit */
  	TW_CLEAR_ALL_INTERRUPTS(tw_dev);
  } /* End __twa_shutdown() */
  
  /* Wrapper for __twa_shutdown */
d18c3db58   Greg Kroah-Hartman   [PATCH] PCI: make...
1939
  static void twa_shutdown(struct pci_dev *pdev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1940
  {
d18c3db58   Greg Kroah-Hartman   [PATCH] PCI: make...
1941
  	struct Scsi_Host *host = pci_get_drvdata(pdev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
  	TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
  
  	__twa_shutdown(tw_dev);
  } /* End twa_shutdown() */
  
  /* This function will look up a string */
  static char *twa_string_lookup(twa_message_type *table, unsigned int code)
  {
  	int index;
  
  	for (index = 0; ((code != table[index].code) &&
  		      (table[index].text != (char *)0)); index++);
  	return(table[index].text);
  } /* End twa_string_lookup() */
4deedd845   Adam Radford   [SCSI] 3w-xxxx, 3...
1956
1957
1958
1959
1960
1961
1962
1963
  /* This function gets called when a disk is coming on-line */
  static int twa_slave_configure(struct scsi_device *sdev)
  {
  	/* Force 60 second timeout */
  	blk_queue_rq_timeout(sdev->request_queue, 60 * HZ);
  
  	return 0;
  } /* End twa_slave_configure() */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1964
1965
1966
1967
1968
1969
1970
  /* scsi_host_template initializer */
  static struct scsi_host_template driver_template = {
  	.module			= THIS_MODULE,
  	.name			= "3ware 9000 Storage Controller",
  	.queuecommand		= twa_scsi_queue,
  	.eh_host_reset_handler	= twa_scsi_eh_reset,
  	.bios_param		= twa_scsi_biosparam,
db5ed4dfd   Christoph Hellwig   scsi: drop reason...
1971
  	.change_queue_depth	= scsi_change_queue_depth,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1972
  	.can_queue		= TW_Q_LENGTH-2,
4deedd845   Adam Radford   [SCSI] 3w-xxxx, 3...
1973
  	.slave_configure	= twa_slave_configure,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1974
1975
1976
1977
  	.this_id		= -1,
  	.sg_tablesize		= TW_APACHE_MAX_SGL_LENGTH,
  	.max_sectors		= TW_MAX_SECTORS,
  	.cmd_per_lun		= TW_MAX_CMDS_PER_LUN,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1978
  	.shost_attrs		= twa_host_attrs,
54b2b50c2   Martin K. Petersen   [SCSI] Disable WR...
1979
1980
  	.emulated		= 1,
  	.no_write_same		= 1,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1981
1982
1983
  };
  
  /* This function will probe and initialize a card */
6f0397905   Greg Kroah-Hartman   Drivers: scsi: re...
1984
  static int twa_probe(struct pci_dev *pdev, const struct pci_device_id *dev_id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1985
1986
1987
  {
  	struct Scsi_Host *host = NULL;
  	TW_Device_Extension *tw_dev;
3dabec717   Adam Radford   [SCSI] 3w-9xxx: a...
1988
  	unsigned long mem_addr, mem_len;
33d666741   Hannes Reinecke   scsi: 3w-9xxx: fi...
1989
  	int retval;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1990
1991
1992
1993
1994
1995
1996
1997
  
  	retval = pci_enable_device(pdev);
  	if (retval) {
  		TW_PRINTK(host, TW_DRIVER, 0x34, "Failed to enable pci device");
  		goto out_disable_device;
  	}
  
  	pci_set_master(pdev);
1e6c38cec   Tony Battersby   [SCSI] 3w-9xxx: f...
1998
  	pci_try_set_mwi(pdev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1999

33d666741   Hannes Reinecke   scsi: 3w-9xxx: fi...
2000
2001
2002
2003
  	retval = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  	if (retval)
  		retval = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  	if (retval) {
b000bced5   Christoph Hellwig   scsi: 3w-9xxx: fu...
2004
2005
2006
2007
  		TW_PRINTK(host, TW_DRIVER, 0x23, "Failed to set dma mask");
  		retval = -ENODEV;
  		goto out_disable_device;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2008
2009
2010
2011
2012
2013
2014
2015
  
  	host = scsi_host_alloc(&driver_template, sizeof(TW_Device_Extension));
  	if (!host) {
  		TW_PRINTK(host, TW_DRIVER, 0x24, "Failed to allocate memory for device extension");
  		retval = -ENOMEM;
  		goto out_disable_device;
  	}
  	tw_dev = (TW_Device_Extension *)host->hostdata;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2016
2017
2018
2019
2020
2021
  	/* Save values to device extension */
  	tw_dev->host = host;
  	tw_dev->tw_pci_dev = pdev;
  
  	if (twa_initialize_device_extension(tw_dev)) {
  		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x25, "Failed to initialize device extension");
4dc98c199   Anton Vasilyev   scsi: 3ware: fix ...
2022
  		retval = -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2023
2024
2025
2026
2027
2028
2029
2030
2031
  		goto out_free_device_extension;
  	}
  
  	/* Request IO regions */
  	retval = pci_request_regions(pdev, "3w-9xxx");
  	if (retval) {
  		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x26, "Failed to get mem region");
  		goto out_free_device_extension;
  	}
3dabec717   Adam Radford   [SCSI] 3w-9xxx: a...
2032
  	if (pdev->device == PCI_DEVICE_ID_3WARE_9000) {
49bfd8db4   Adam Radford   [SCSI] 3ware 9000...
2033
  		mem_addr = pci_resource_start(pdev, 1);
3dabec717   Adam Radford   [SCSI] 3w-9xxx: a...
2034
2035
  		mem_len = pci_resource_len(pdev, 1);
  	} else {
49bfd8db4   Adam Radford   [SCSI] 3ware 9000...
2036
  		mem_addr = pci_resource_start(pdev, 2);
3dabec717   Adam Radford   [SCSI] 3w-9xxx: a...
2037
2038
  		mem_len = pci_resource_len(pdev, 2);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2039
2040
  
  	/* Save base address */
3dabec717   Adam Radford   [SCSI] 3w-9xxx: a...
2041
  	tw_dev->base_addr = ioremap(mem_addr, mem_len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2042
2043
  	if (!tw_dev->base_addr) {
  		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x35, "Failed to ioremap");
4dc98c199   Anton Vasilyev   scsi: 3ware: fix ...
2044
  		retval = -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2045
2046
2047
2048
2049
2050
2051
  		goto out_release_mem_region;
  	}
  
  	/* Disable interrupts on the card */
  	TW_DISABLE_INTERRUPTS(tw_dev);
  
  	/* Initialize the card */
4dc98c199   Anton Vasilyev   scsi: 3ware: fix ...
2052
2053
  	if (twa_reset_sequence(tw_dev, 0)) {
  		retval = -ENOMEM;
4039c30ef   Adam Radford   [SCSI] 3ware 9000...
2054
  		goto out_iounmap;
4dc98c199   Anton Vasilyev   scsi: 3ware: fix ...
2055
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2056
2057
  
  	/* Set host specific parameters */
0e78d158b   Adam Radford   [SCSI] 3w-9xxx: a...
2058
2059
  	if ((pdev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
  	    (pdev->device == PCI_DEVICE_ID_3WARE_9690SA))
4039c30ef   Adam Radford   [SCSI] 3ware 9000...
2060
2061
2062
  		host->max_id = TW_MAX_UNITS_9650SE;
  	else
  		host->max_id = TW_MAX_UNITS;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2063
2064
2065
  	host->max_cmd_len = TW_MAX_CDB_LEN;
  
  	/* Channels aren't supported by adapter */
4039c30ef   Adam Radford   [SCSI] 3ware 9000...
2066
  	host->max_lun = TW_MAX_LUNS(tw_dev->tw_compat_info.working_srl);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2067
2068
2069
2070
2071
2072
  	host->max_channel = 0;
  
  	/* Register the card with the kernel SCSI layer */
  	retval = scsi_add_host(host, &pdev->dev);
  	if (retval) {
  		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x27, "scsi add host failed");
4039c30ef   Adam Radford   [SCSI] 3ware 9000...
2073
  		goto out_iounmap;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2074
2075
2076
  	}
  
  	pci_set_drvdata(pdev, host);
3dabec717   Adam Radford   [SCSI] 3w-9xxx: a...
2077
2078
  	printk(KERN_WARNING "3w-9xxx: scsi%d: Found a 3ware 9000 Storage Controller at 0x%lx, IRQ: %d.
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2079
2080
2081
2082
2083
2084
2085
2086
  	       host->host_no, mem_addr, pdev->irq);
  	printk(KERN_WARNING "3w-9xxx: scsi%d: Firmware %s, BIOS %s, Ports: %d.
  ",
  	       host->host_no,
  	       (char *)twa_get_param(tw_dev, 0, TW_VERSION_TABLE,
  				     TW_PARAM_FWVER, TW_PARAM_FWVER_LENGTH),
  	       (char *)twa_get_param(tw_dev, 1, TW_VERSION_TABLE,
  				     TW_PARAM_BIOSVER, TW_PARAM_BIOSVER_LENGTH),
75913d9bb   Adam Radford   [SCSI] 3ware 9000...
2087
2088
  	       le32_to_cpu(*(int *)twa_get_param(tw_dev, 2, TW_INFORMATION_TABLE,
  				     TW_PARAM_PORTCOUNT, TW_PARAM_PORTCOUNT_LENGTH)));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2089

3dabec717   Adam Radford   [SCSI] 3w-9xxx: a...
2090
2091
2092
2093
  	/* Try to enable MSI */
  	if (use_msi && (pdev->device != PCI_DEVICE_ID_3WARE_9000) &&
  	    !pci_enable_msi(pdev))
  		set_bit(TW_USING_MSI, &tw_dev->flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2094
  	/* Now setup the interrupt handler */
1d6f359a2   Thomas Gleixner   [PATCH] irq-flags...
2095
  	retval = request_irq(pdev->irq, twa_interrupt, IRQF_SHARED, "3w-9xxx", tw_dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
  	if (retval) {
  		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x30, "Error requesting IRQ");
  		goto out_remove_host;
  	}
  
  	twa_device_extension_list[twa_device_extension_count] = tw_dev;
  	twa_device_extension_count++;
  
  	/* Re-enable interrupts on the card */
  	TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
  
  	/* Finally, scan the host */
  	scsi_scan_host(host);
  
  	if (twa_major == -1) {
  		if ((twa_major = register_chrdev (0, "twa", &twa_fops)) < 0)
  			TW_PRINTK(host, TW_DRIVER, 0x29, "Failed to register character device");
  	}
  	return 0;
  
  out_remove_host:
3dabec717   Adam Radford   [SCSI] 3w-9xxx: a...
2117
2118
  	if (test_bit(TW_USING_MSI, &tw_dev->flags))
  		pci_disable_msi(pdev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2119
  	scsi_remove_host(host);
4039c30ef   Adam Radford   [SCSI] 3ware 9000...
2120
2121
  out_iounmap:
  	iounmap(tw_dev->base_addr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
  out_release_mem_region:
  	pci_release_regions(pdev);
  out_free_device_extension:
  	twa_free_device_extension(tw_dev);
  	scsi_host_put(host);
  out_disable_device:
  	pci_disable_device(pdev);
  
  	return retval;
  } /* End twa_probe() */
  
  /* This function is called to remove a device */
  static void twa_remove(struct pci_dev *pdev)
  {
  	struct Scsi_Host *host = pci_get_drvdata(pdev);
  	TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
  
  	scsi_remove_host(tw_dev->host);
  
  	/* Unregister character device */
  	if (twa_major >= 0) {
  		unregister_chrdev(twa_major, "twa");
  		twa_major = -1;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2146
2147
  	/* Shutdown the card */
  	__twa_shutdown(tw_dev);
3dabec717   Adam Radford   [SCSI] 3w-9xxx: a...
2148
2149
2150
  	/* Disable MSI if enabled */
  	if (test_bit(TW_USING_MSI, &tw_dev->flags))
  		pci_disable_msi(pdev);
4039c30ef   Adam Radford   [SCSI] 3ware 9000...
2151
2152
  	/* Free IO remapping */
  	iounmap(tw_dev->base_addr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
  	/* Free up the mem region */
  	pci_release_regions(pdev);
  
  	/* Free up device extension resources */
  	twa_free_device_extension(tw_dev);
  
  	scsi_host_put(tw_dev->host);
  	pci_disable_device(pdev);
  	twa_device_extension_count--;
  } /* End twa_remove() */
7a252fe7b   Adam Radford   [SCSI] 3w-9xxx: a...
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
  #ifdef CONFIG_PM
  /* This function is called on PCI suspend */
  static int twa_suspend(struct pci_dev *pdev, pm_message_t state)
  {
  	struct Scsi_Host *host = pci_get_drvdata(pdev);
  	TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
  
  	printk(KERN_WARNING "3w-9xxx: Suspending host %d.
  ", tw_dev->host->host_no);
  
  	TW_DISABLE_INTERRUPTS(tw_dev);
  	free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
  
  	if (test_bit(TW_USING_MSI, &tw_dev->flags))
  		pci_disable_msi(pdev);
  
  	/* Tell the card we are shutting down */
  	if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
  		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x38, "Connection shutdown failed during suspend");
  	} else {
  		printk(KERN_WARNING "3w-9xxx: Suspend complete.
  ");
  	}
  	TW_CLEAR_ALL_INTERRUPTS(tw_dev);
  
  	pci_save_state(pdev);
  	pci_disable_device(pdev);
  	pci_set_power_state(pdev, pci_choose_state(pdev, state));
  
  	return 0;
  } /* End twa_suspend() */
  
  /* This function is called on PCI resume */
  static int twa_resume(struct pci_dev *pdev)
  {
  	int retval = 0;
  	struct Scsi_Host *host = pci_get_drvdata(pdev);
  	TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
  
  	printk(KERN_WARNING "3w-9xxx: Resuming host %d.
  ", tw_dev->host->host_no);
  	pci_set_power_state(pdev, PCI_D0);
  	pci_enable_wake(pdev, PCI_D0, 0);
  	pci_restore_state(pdev);
  
  	retval = pci_enable_device(pdev);
  	if (retval) {
  		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x39, "Enable device failed during resume");
  		return retval;
  	}
  
  	pci_set_master(pdev);
  	pci_try_set_mwi(pdev);
33d666741   Hannes Reinecke   scsi: 3w-9xxx: fi...
2216
2217
2218
2219
  	retval = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  	if (retval)
  		retval = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  	if (retval) {
b000bced5   Christoph Hellwig   scsi: 3w-9xxx: fu...
2220
2221
2222
2223
  		TW_PRINTK(host, TW_DRIVER, 0x40, "Failed to set dma mask during resume");
  		retval = -ENODEV;
  		goto out_disable_device;
  	}
7a252fe7b   Adam Radford   [SCSI] 3w-9xxx: a...
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
  
  	/* Initialize the card */
  	if (twa_reset_sequence(tw_dev, 0)) {
  		retval = -ENODEV;
  		goto out_disable_device;
  	}
  
  	/* Now setup the interrupt handler */
  	retval = request_irq(pdev->irq, twa_interrupt, IRQF_SHARED, "3w-9xxx", tw_dev);
  	if (retval) {
  		TW_PRINTK(tw_dev->host, TW_DRIVER, 0x42, "Error requesting IRQ during resume");
  		retval = -ENODEV;
  		goto out_disable_device;
  	}
  
  	/* Now enable MSI if enabled */
  	if (test_bit(TW_USING_MSI, &tw_dev->flags))
  		pci_enable_msi(pdev);
  
  	/* Re-enable interrupts on the card */
  	TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
  
  	printk(KERN_WARNING "3w-9xxx: Resume complete.
  ");
  	return 0;
  
  out_disable_device:
  	scsi_remove_host(host);
  	pci_disable_device(pdev);
  
  	return retval;
  } /* End twa_resume() */
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2257
  /* PCI Devices supported by this driver */
6f0397905   Greg Kroah-Hartman   Drivers: scsi: re...
2258
  static struct pci_device_id twa_pci_tbl[] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2259
2260
  	{ PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9000,
  	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
49bfd8db4   Adam Radford   [SCSI] 3ware 9000...
2261
2262
  	{ PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9550SX,
  	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4039c30ef   Adam Radford   [SCSI] 3ware 9000...
2263
2264
  	{ PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9650SE,
  	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
0e78d158b   Adam Radford   [SCSI] 3w-9xxx: a...
2265
2266
  	{ PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9690SA,
  	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
  	{ }
  };
  MODULE_DEVICE_TABLE(pci, twa_pci_tbl);
  
  /* pci_driver initializer */
  static struct pci_driver twa_driver = {
  	.name		= "3w-9xxx",
  	.id_table	= twa_pci_tbl,
  	.probe		= twa_probe,
  	.remove		= twa_remove,
7a252fe7b   Adam Radford   [SCSI] 3w-9xxx: a...
2277
2278
2279
2280
  #ifdef CONFIG_PM
  	.suspend	= twa_suspend,
  	.resume		= twa_resume,
  #endif
d18c3db58   Greg Kroah-Hartman   [PATCH] PCI: make...
2281
  	.shutdown	= twa_shutdown
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2282
2283
2284
2285
2286
2287
2288
  };
  
  /* This function is called on driver initialization */
  static int __init twa_init(void)
  {
  	printk(KERN_WARNING "3ware 9000 Storage Controller device driver for Linux v%s.
  ", TW_DRIVER_VERSION);
dcbccbde0   Henrik Kretzschmar   [SCSI] pci_module...
2289
  	return pci_register_driver(&twa_driver);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
  } /* End twa_init() */
  
  /* This function is called on driver exit */
  static void __exit twa_exit(void)
  {
  	pci_unregister_driver(&twa_driver);
  } /* End twa_exit() */
  
  module_init(twa_init);
  module_exit(twa_exit);