]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/scsi/3w-9xxx.c
scsi: 3ware: use 64-bit times for FW time sync
[mirror_ubuntu-hirsute-kernel.git] / drivers / scsi / 3w-9xxx.c
CommitLineData
1da177e4
LT
1/*
2 3w-9xxx.c -- 3ware 9000 Storage Controller device driver for Linux.
3
2c9bce5b
AR
4 Written By: Adam Radford <aradford@gmail.com>
5 Modifications By: Tom Couch
1da177e4 6
7a252fe7 7 Copyright (C) 2004-2009 Applied Micro Circuits Corporation.
4deedd84 8 Copyright (C) 2010 LSI Corporation.
1da177e4
LT
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; version 2 of the License.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 NO WARRANTY
20 THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
21 CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
22 LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
23 MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
24 solely responsible for determining the appropriateness of using and
25 distributing the Program and assumes all risks associated with its
26 exercise of rights under this Agreement, including but not limited to
27 the risks and costs of program errors, damage to or loss of data,
28 programs or equipment, and unavailability or interruption of operations.
29
30 DISCLAIMER OF LIABILITY
31 NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
32 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
34 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
35 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
36 USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
37 HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
38
39 You should have received a copy of the GNU General Public License
40 along with this program; if not, write to the Free Software
41 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
42
43 Bugs/Comments/Suggestions should be mailed to:
2c9bce5b 44 aradford@gmail.com
1da177e4
LT
45
46 Note: This version of the driver does not contain a bundled firmware
47 image.
48
49 History
50 -------
51 2.26.02.000 - Driver cleanup for kernel submission.
52 2.26.02.001 - Replace schedule_timeout() calls with msleep().
53 2.26.02.002 - Add support for PAE mode.
54 Add lun support.
55 Fix twa_remove() to free irq handler/unregister_chrdev()
56 before shutting down card.
57 Change to new 'change_queue_depth' api.
58 Fix 'handled=1' ISR usage, remove bogus IRQ check.
59 Remove un-needed eh_abort handler.
60 Add support for embedded firmware error strings.
d327d082 61 2.26.02.003 - Correctly handle single sgl's with use_sg=1.
49bfd8db 62 2.26.02.004 - Add support for 9550SX controllers.
62288f10 63 2.26.02.005 - Fix use_sg == 0 mapping on systems with 4GB or higher.
75913d9b
AR
64 2.26.02.006 - Fix 9550SX pchip reset timeout.
65 Add big endian support.
1e08dcb3 66 2.26.02.007 - Disable local interrupts during kmap/unmap_atomic().
4039c30e
AR
67 2.26.02.008 - Free irq handler in __twa_shutdown().
68 Serialize reset code.
69 Add support for 9650SE controllers.
0e78d158
AR
70 2.26.02.009 - Fix dma mask setting to fallback to 32-bit if 64-bit fails.
71 2.26.02.010 - Add support for 9690SA controllers.
3dabec71
AR
72 2.26.02.011 - Increase max AENs drained to 256.
73 Add MSI support and "use_msi" module parameter.
74 Fix bug in twa_get_param() on 4GB+.
75 Use pci_resource_len() for ioremap().
7a252fe7 76 2.26.02.012 - Add power management support.
53ca3535 77 2.26.02.013 - Fix bug in twa_load_sgl().
4deedd84 78 2.26.02.014 - Force 60 second timeout default.
1da177e4
LT
79*/
80
81#include <linux/module.h>
82#include <linux/reboot.h>
83#include <linux/spinlock.h>
84#include <linux/interrupt.h>
85#include <linux/moduleparam.h>
86#include <linux/errno.h>
87#include <linux/types.h>
88#include <linux/delay.h>
89#include <linux/pci.h>
90#include <linux/time.h>
a12e25bd 91#include <linux/mutex.h>
5a0e3ad6 92#include <linux/slab.h>
1da177e4
LT
93#include <asm/io.h>
94#include <asm/irq.h>
7c0f6ba6 95#include <linux/uaccess.h>
1da177e4
LT
96#include <scsi/scsi.h>
97#include <scsi/scsi_host.h>
98#include <scsi/scsi_tcq.h>
99#include <scsi/scsi_cmnd.h>
100#include "3w-9xxx.h"
101
102/* Globals */
4deedd84 103#define TW_DRIVER_VERSION "2.26.02.014"
c45d15d2 104static DEFINE_MUTEX(twa_chrdev_mutex);
1da177e4
LT
105static TW_Device_Extension *twa_device_extension_list[TW_MAX_SLOT];
106static unsigned int twa_device_extension_count;
107static int twa_major = -1;
108extern struct timezone sys_tz;
109
110/* Module parameters */
4deedd84 111MODULE_AUTHOR ("LSI");
1da177e4
LT
112MODULE_DESCRIPTION ("3ware 9000 Storage Controller Linux Driver");
113MODULE_LICENSE("GPL");
114MODULE_VERSION(TW_DRIVER_VERSION);
115
3dabec71
AR
116static int use_msi = 0;
117module_param(use_msi, int, S_IRUGO);
118MODULE_PARM_DESC(use_msi, "Use Message Signaled Interrupts. Default: 0");
119
1da177e4
LT
120/* Function prototypes */
121static void twa_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header);
122static int twa_aen_read_queue(TW_Device_Extension *tw_dev, int request_id);
123static char *twa_aen_severity_lookup(unsigned char severity_code);
124static void twa_aen_sync_time(TW_Device_Extension *tw_dev, int request_id);
f4927c45 125static long twa_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
1da177e4
LT
126static int twa_chrdev_open(struct inode *inode, struct file *file);
127static int twa_fill_sense(TW_Device_Extension *tw_dev, int request_id, int copy_sense, int print_host);
128static void twa_free_request_id(TW_Device_Extension *tw_dev,int request_id);
129static void twa_get_request_id(TW_Device_Extension *tw_dev, int *request_id);
130static int twa_initconnection(TW_Device_Extension *tw_dev, int message_credits,
131 u32 set_features, unsigned short current_fw_srl,
132 unsigned short current_fw_arch_id,
133 unsigned short current_fw_branch,
134 unsigned short current_fw_build,
135 unsigned short *fw_on_ctlr_srl,
136 unsigned short *fw_on_ctlr_arch_id,
137 unsigned short *fw_on_ctlr_branch,
138 unsigned short *fw_on_ctlr_build,
139 u32 *init_connect_result);
0e78d158 140static 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);
1da177e4
LT
141static int twa_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds);
142static int twa_poll_status_gone(TW_Device_Extension *tw_dev, u32 flag, int seconds);
143static int twa_post_command_packet(TW_Device_Extension *tw_dev, int request_id, char internal);
0e78d158 144static int twa_reset_device_extension(TW_Device_Extension *tw_dev);
1da177e4
LT
145static int twa_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset);
146static int twa_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id, char *cdb, int use_sg, TW_SG_Entry *sglistarg);
147static void twa_scsiop_execute_scsi_complete(TW_Device_Extension *tw_dev, int request_id);
148static char *twa_string_lookup(twa_message_type *table, unsigned int aen_code);
1da177e4
LT
149
150/* Functions */
151
152/* Show some statistics about the card */
ee959b00
TJ
153static ssize_t twa_show_stats(struct device *dev,
154 struct device_attribute *attr, char *buf)
1da177e4 155{
ee959b00 156 struct Scsi_Host *host = class_to_shost(dev);
1da177e4
LT
157 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
158 unsigned long flags = 0;
159 ssize_t len;
160
161 spin_lock_irqsave(tw_dev->host->host_lock, flags);
162 len = snprintf(buf, PAGE_SIZE, "3w-9xxx Driver version: %s\n"
163 "Current commands posted: %4d\n"
164 "Max commands posted: %4d\n"
165 "Current pending commands: %4d\n"
166 "Max pending commands: %4d\n"
167 "Last sgl length: %4d\n"
168 "Max sgl length: %4d\n"
169 "Last sector count: %4d\n"
170 "Max sector count: %4d\n"
171 "SCSI Host Resets: %4d\n"
172 "AEN's: %4d\n",
173 TW_DRIVER_VERSION,
174 tw_dev->posted_request_count,
175 tw_dev->max_posted_request_count,
176 tw_dev->pending_request_count,
177 tw_dev->max_pending_request_count,
178 tw_dev->sgl_entries,
179 tw_dev->max_sgl_entries,
180 tw_dev->sector_count,
181 tw_dev->max_sector_count,
182 tw_dev->num_resets,
183 tw_dev->aen_count);
184 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
185 return len;
186} /* End twa_show_stats() */
187
1da177e4 188/* Create sysfs 'stats' entry */
ee959b00 189static struct device_attribute twa_host_stats_attr = {
1da177e4
LT
190 .attr = {
191 .name = "stats",
192 .mode = S_IRUGO,
193 },
194 .show = twa_show_stats
195};
196
197/* Host attributes initializer */
ee959b00 198static struct device_attribute *twa_host_attrs[] = {
1da177e4
LT
199 &twa_host_stats_attr,
200 NULL,
201};
202
203/* File operations struct for character device */
00977a59 204static const struct file_operations twa_fops = {
1da177e4 205 .owner = THIS_MODULE,
f4927c45 206 .unlocked_ioctl = twa_chrdev_ioctl,
1da177e4 207 .open = twa_chrdev_open,
6038f373
AB
208 .release = NULL,
209 .llseek = noop_llseek,
1da177e4
LT
210};
211
15e3d5a2
CH
212/*
213 * The controllers use an inline buffer instead of a mapped SGL for small,
214 * single entry buffers. Note that we treat a zero-length transfer like
215 * a mapped SGL.
216 */
217static bool twa_command_mapped(struct scsi_cmnd *cmd)
218{
219 return scsi_sg_count(cmd) != 1 ||
220 scsi_bufflen(cmd) >= TW_MIN_SGL_LENGTH;
221}
222
1da177e4
LT
223/* This function will complete an aen request from the isr */
224static int twa_aen_complete(TW_Device_Extension *tw_dev, int request_id)
225{
226 TW_Command_Full *full_command_packet;
227 TW_Command *command_packet;
228 TW_Command_Apache_Header *header;
229 unsigned short aen;
230 int retval = 1;
231
232 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
233 tw_dev->posted_request_count--;
75913d9b 234 aen = le16_to_cpu(header->status_block.error);
1da177e4
LT
235 full_command_packet = tw_dev->command_packet_virt[request_id];
236 command_packet = &full_command_packet->command.oldcommand;
237
238 /* First check for internal completion of set param for time sync */
239 if (TW_OP_OUT(command_packet->opcode__sgloffset) == TW_OP_SET_PARAM) {
240 /* Keep reading the queue in case there are more aen's */
241 if (twa_aen_read_queue(tw_dev, request_id))
242 goto out2;
243 else {
244 retval = 0;
245 goto out;
246 }
247 }
248
249 switch (aen) {
250 case TW_AEN_QUEUE_EMPTY:
251 /* Quit reading the queue if this is the last one */
252 break;
253 case TW_AEN_SYNC_TIME_WITH_HOST:
254 twa_aen_sync_time(tw_dev, request_id);
255 retval = 0;
256 goto out;
257 default:
258 twa_aen_queue_event(tw_dev, header);
259
260 /* If there are more aen's, keep reading the queue */
261 if (twa_aen_read_queue(tw_dev, request_id))
262 goto out2;
263 else {
264 retval = 0;
265 goto out;
266 }
267 }
268 retval = 0;
269out2:
270 tw_dev->state[request_id] = TW_S_COMPLETED;
271 twa_free_request_id(tw_dev, request_id);
272 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
273out:
274 return retval;
275} /* End twa_aen_complete() */
276
277/* This function will drain aen queue */
278static int twa_aen_drain_queue(TW_Device_Extension *tw_dev, int no_check_reset)
279{
280 int request_id = 0;
281 char cdb[TW_MAX_CDB_LEN];
282 TW_SG_Entry sglist[1];
283 int finished = 0, count = 0;
284 TW_Command_Full *full_command_packet;
285 TW_Command_Apache_Header *header;
286 unsigned short aen;
287 int first_reset = 0, queue = 0, retval = 1;
288
289 if (no_check_reset)
290 first_reset = 0;
291 else
292 first_reset = 1;
293
294 full_command_packet = tw_dev->command_packet_virt[request_id];
295 memset(full_command_packet, 0, sizeof(TW_Command_Full));
296
297 /* Initialize cdb */
298 memset(&cdb, 0, TW_MAX_CDB_LEN);
299 cdb[0] = REQUEST_SENSE; /* opcode */
300 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
301
302 /* Initialize sglist */
303 memset(&sglist, 0, sizeof(TW_SG_Entry));
304 sglist[0].length = TW_SECTOR_SIZE;
305 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
306
307 if (sglist[0].address & TW_ALIGNMENT_9000_SGL) {
308 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1, "Found unaligned address during AEN drain");
309 goto out;
310 }
311
312 /* Mark internal command */
313 tw_dev->srb[request_id] = NULL;
314
315 do {
316 /* Send command to the board */
317 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
318 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2, "Error posting request sense");
319 goto out;
320 }
321
322 /* Now poll for completion */
323 if (twa_poll_response(tw_dev, request_id, 30)) {
324 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x3, "No valid response while draining AEN queue");
325 tw_dev->posted_request_count--;
326 goto out;
327 }
328
329 tw_dev->posted_request_count--;
330 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
75913d9b 331 aen = le16_to_cpu(header->status_block.error);
1da177e4
LT
332 queue = 0;
333 count++;
334
335 switch (aen) {
336 case TW_AEN_QUEUE_EMPTY:
337 if (first_reset != 1)
338 goto out;
339 else
340 finished = 1;
341 break;
342 case TW_AEN_SOFT_RESET:
343 if (first_reset == 0)
344 first_reset = 1;
345 else
346 queue = 1;
347 break;
348 case TW_AEN_SYNC_TIME_WITH_HOST:
349 break;
350 default:
351 queue = 1;
352 }
353
354 /* Now queue an event info */
355 if (queue)
356 twa_aen_queue_event(tw_dev, header);
357 } while ((finished == 0) && (count < TW_MAX_AEN_DRAIN));
358
359 if (count == TW_MAX_AEN_DRAIN)
360 goto out;
361
362 retval = 0;
363out:
364 tw_dev->state[request_id] = TW_S_INITIAL;
365 return retval;
366} /* End twa_aen_drain_queue() */
367
368/* This function will queue an event */
369static void twa_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header)
370{
371 u32 local_time;
1da177e4
LT
372 TW_Event *event;
373 unsigned short aen;
374 char host[16];
375 char *error_str;
376
377 tw_dev->aen_count++;
378
379 /* Fill out event info */
380 event = tw_dev->event_queue[tw_dev->error_index];
381
382 /* Check for clobber */
383 host[0] = '\0';
384 if (tw_dev->host) {
385 sprintf(host, " scsi%d:", tw_dev->host->host_no);
386 if (event->retrieved == TW_AEN_NOT_RETRIEVED)
387 tw_dev->aen_clobber = 1;
388 }
389
75913d9b 390 aen = le16_to_cpu(header->status_block.error);
1da177e4
LT
391 memset(event, 0, sizeof(TW_Event));
392
393 event->severity = TW_SEV_OUT(header->status_block.severity__reserved);
9c88673f
AB
394 /* event->time_stamp_sec overflows in y2106 */
395 local_time = (u32)(ktime_get_real_seconds() - (sys_tz.tz_minuteswest * 60));
1da177e4
LT
396 event->time_stamp_sec = local_time;
397 event->aen_code = aen;
398 event->retrieved = TW_AEN_NOT_RETRIEVED;
399 event->sequence_id = tw_dev->error_sequence_id;
400 tw_dev->error_sequence_id++;
401
402 /* Check for embedded error string */
403 error_str = &(header->err_specific_desc[strlen(header->err_specific_desc)+1]);
404
405 header->err_specific_desc[sizeof(header->err_specific_desc) - 1] = '\0';
406 event->parameter_len = strlen(header->err_specific_desc);
75913d9b 407 memcpy(event->parameter_data, header->err_specific_desc, event->parameter_len + (error_str[0] == '\0' ? 0 : (1 + strlen(error_str))));
1da177e4
LT
408 if (event->severity != TW_AEN_SEVERITY_DEBUG)
409 printk(KERN_WARNING "3w-9xxx:%s AEN: %s (0x%02X:0x%04X): %s:%s.\n",
410 host,
411 twa_aen_severity_lookup(TW_SEV_OUT(header->status_block.severity__reserved)),
412 TW_MESSAGE_SOURCE_CONTROLLER_EVENT, aen,
413 error_str[0] == '\0' ? twa_string_lookup(twa_aen_table, aen) : error_str,
414 header->err_specific_desc);
415 else
416 tw_dev->aen_count--;
417
418 if ((tw_dev->error_index + 1) == TW_Q_LENGTH)
419 tw_dev->event_queue_wrapped = 1;
420 tw_dev->error_index = (tw_dev->error_index + 1 ) % TW_Q_LENGTH;
421} /* End twa_aen_queue_event() */
422
423/* This function will read the aen queue from the isr */
424static int twa_aen_read_queue(TW_Device_Extension *tw_dev, int request_id)
425{
426 char cdb[TW_MAX_CDB_LEN];
427 TW_SG_Entry sglist[1];
428 TW_Command_Full *full_command_packet;
429 int retval = 1;
430
431 full_command_packet = tw_dev->command_packet_virt[request_id];
432 memset(full_command_packet, 0, sizeof(TW_Command_Full));
433
434 /* Initialize cdb */
435 memset(&cdb, 0, TW_MAX_CDB_LEN);
436 cdb[0] = REQUEST_SENSE; /* opcode */
437 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
438
439 /* Initialize sglist */
440 memset(&sglist, 0, sizeof(TW_SG_Entry));
441 sglist[0].length = TW_SECTOR_SIZE;
442 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
443
444 /* Mark internal command */
445 tw_dev->srb[request_id] = NULL;
446
447 /* Now post the command packet */
448 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
449 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x4, "Post failed while reading AEN queue");
450 goto out;
451 }
452 retval = 0;
453out:
454 return retval;
455} /* End twa_aen_read_queue() */
456
457/* This function will look up an AEN severity string */
458static char *twa_aen_severity_lookup(unsigned char severity_code)
459{
460 char *retval = NULL;
461
462 if ((severity_code < (unsigned char) TW_AEN_SEVERITY_ERROR) ||
463 (severity_code > (unsigned char) TW_AEN_SEVERITY_DEBUG))
464 goto out;
465
466 retval = twa_aen_severity_table[severity_code];
467out:
468 return retval;
469} /* End twa_aen_severity_lookup() */
470
471/* This function will sync firmware time with the host time */
472static void twa_aen_sync_time(TW_Device_Extension *tw_dev, int request_id)
473{
474 u32 schedulertime;
1da177e4
LT
475 TW_Command_Full *full_command_packet;
476 TW_Command *command_packet;
477 TW_Param_Apache *param;
bc8f9166 478 time64_t local_time;
1da177e4
LT
479
480 /* Fill out the command packet */
481 full_command_packet = tw_dev->command_packet_virt[request_id];
482 memset(full_command_packet, 0, sizeof(TW_Command_Full));
483 command_packet = &full_command_packet->command.oldcommand;
484 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_SET_PARAM);
485 command_packet->request_id = request_id;
75913d9b
AR
486 command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
487 command_packet->byte8_offset.param.sgl[0].length = cpu_to_le32(TW_SECTOR_SIZE);
1da177e4 488 command_packet->size = TW_COMMAND_SIZE;
75913d9b 489 command_packet->byte6_offset.parameter_count = cpu_to_le16(1);
1da177e4
LT
490
491 /* Setup the param */
492 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
493 memset(param, 0, TW_SECTOR_SIZE);
75913d9b
AR
494 param->table_id = cpu_to_le16(TW_TIMEKEEP_TABLE | 0x8000); /* Controller time keep table */
495 param->parameter_id = cpu_to_le16(0x3); /* SchedulerTime */
496 param->parameter_size_bytes = cpu_to_le16(4);
1da177e4
LT
497
498 /* Convert system time in UTC to local time seconds since last
499 Sunday 12:00AM */
bc8f9166
AB
500 local_time = (ktime_get_real_seconds() - (sys_tz.tz_minuteswest * 60));
501 div_u64_rem(local_time - (3 * 86400), 604800, &schedulertime);
75913d9b 502 schedulertime = cpu_to_le32(schedulertime % 604800);
1da177e4
LT
503
504 memcpy(param->data, &schedulertime, sizeof(u32));
505
506 /* Mark internal command */
507 tw_dev->srb[request_id] = NULL;
508
509 /* Now post the command */
510 twa_post_command_packet(tw_dev, request_id, 1);
511} /* End twa_aen_sync_time() */
512
513/* This function will allocate memory and check if it is correctly aligned */
514static int twa_allocate_memory(TW_Device_Extension *tw_dev, int size, int which)
515{
516 int i;
517 dma_addr_t dma_handle;
518 unsigned long *cpu_addr;
519 int retval = 1;
520
521 cpu_addr = pci_alloc_consistent(tw_dev->tw_pci_dev, size*TW_Q_LENGTH, &dma_handle);
522 if (!cpu_addr) {
523 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x5, "Memory allocation failed");
524 goto out;
525 }
526
527 if ((unsigned long)cpu_addr % (TW_ALIGNMENT_9000)) {
528 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x6, "Failed to allocate correctly aligned memory");
529 pci_free_consistent(tw_dev->tw_pci_dev, size*TW_Q_LENGTH, cpu_addr, dma_handle);
530 goto out;
531 }
532
533 memset(cpu_addr, 0, size*TW_Q_LENGTH);
534
535 for (i = 0; i < TW_Q_LENGTH; i++) {
536 switch(which) {
537 case 0:
538 tw_dev->command_packet_phys[i] = dma_handle+(i*size);
539 tw_dev->command_packet_virt[i] = (TW_Command_Full *)((unsigned char *)cpu_addr + (i*size));
540 break;
541 case 1:
542 tw_dev->generic_buffer_phys[i] = dma_handle+(i*size);
543 tw_dev->generic_buffer_virt[i] = (unsigned long *)((unsigned char *)cpu_addr + (i*size));
544 break;
545 }
546 }
547 retval = 0;
548out:
549 return retval;
550} /* End twa_allocate_memory() */
551
552/* This function will check the status register for unexpected bits */
553static int twa_check_bits(u32 status_reg_value)
554{
555 int retval = 1;
556
557 if ((status_reg_value & TW_STATUS_EXPECTED_BITS) != TW_STATUS_EXPECTED_BITS)
558 goto out;
559 if ((status_reg_value & TW_STATUS_UNEXPECTED_BITS) != 0)
560 goto out;
561
562 retval = 0;
563out:
564 return retval;
565} /* End twa_check_bits() */
566
567/* This function will check the srl and decide if we are compatible */
568static int twa_check_srl(TW_Device_Extension *tw_dev, int *flashed)
569{
570 int retval = 1;
571 unsigned short fw_on_ctlr_srl = 0, fw_on_ctlr_arch_id = 0;
572 unsigned short fw_on_ctlr_branch = 0, fw_on_ctlr_build = 0;
573 u32 init_connect_result = 0;
574
575 if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
576 TW_EXTENDED_INIT_CONNECT, TW_CURRENT_DRIVER_SRL,
577 TW_9000_ARCH_ID, TW_CURRENT_DRIVER_BRANCH,
578 TW_CURRENT_DRIVER_BUILD, &fw_on_ctlr_srl,
579 &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
580 &fw_on_ctlr_build, &init_connect_result)) {
581 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x7, "Initconnection failed while checking SRL");
582 goto out;
583 }
584
4039c30e
AR
585 tw_dev->tw_compat_info.working_srl = fw_on_ctlr_srl;
586 tw_dev->tw_compat_info.working_branch = fw_on_ctlr_branch;
587 tw_dev->tw_compat_info.working_build = fw_on_ctlr_build;
1da177e4
LT
588
589 /* Try base mode compatibility */
590 if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
591 if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
592 TW_EXTENDED_INIT_CONNECT,
593 TW_BASE_FW_SRL, TW_9000_ARCH_ID,
594 TW_BASE_FW_BRANCH, TW_BASE_FW_BUILD,
595 &fw_on_ctlr_srl, &fw_on_ctlr_arch_id,
596 &fw_on_ctlr_branch, &fw_on_ctlr_build,
597 &init_connect_result)) {
598 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xa, "Initconnection (base mode) failed while checking SRL");
599 goto out;
600 }
601 if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
602 if (TW_CURRENT_DRIVER_SRL > fw_on_ctlr_srl) {
603 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x32, "Firmware and driver incompatibility: please upgrade firmware");
604 } else {
605 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x33, "Firmware and driver incompatibility: please upgrade driver");
606 }
607 goto out;
608 }
4039c30e
AR
609 tw_dev->tw_compat_info.working_srl = TW_BASE_FW_SRL;
610 tw_dev->tw_compat_info.working_branch = TW_BASE_FW_BRANCH;
611 tw_dev->tw_compat_info.working_build = TW_BASE_FW_BUILD;
612 }
613
614 /* Load rest of compatibility struct */
2195d969
RS
615 strlcpy(tw_dev->tw_compat_info.driver_version, TW_DRIVER_VERSION,
616 sizeof(tw_dev->tw_compat_info.driver_version));
4039c30e
AR
617 tw_dev->tw_compat_info.driver_srl_high = TW_CURRENT_DRIVER_SRL;
618 tw_dev->tw_compat_info.driver_branch_high = TW_CURRENT_DRIVER_BRANCH;
619 tw_dev->tw_compat_info.driver_build_high = TW_CURRENT_DRIVER_BUILD;
620 tw_dev->tw_compat_info.driver_srl_low = TW_BASE_FW_SRL;
621 tw_dev->tw_compat_info.driver_branch_low = TW_BASE_FW_BRANCH;
622 tw_dev->tw_compat_info.driver_build_low = TW_BASE_FW_BUILD;
623 tw_dev->tw_compat_info.fw_on_ctlr_srl = fw_on_ctlr_srl;
624 tw_dev->tw_compat_info.fw_on_ctlr_branch = fw_on_ctlr_branch;
625 tw_dev->tw_compat_info.fw_on_ctlr_build = fw_on_ctlr_build;
626
1da177e4
LT
627 retval = 0;
628out:
629 return retval;
630} /* End twa_check_srl() */
631
632/* This function handles ioctl for the character device */
f4927c45 633static long twa_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1da177e4 634{
496ad9aa 635 struct inode *inode = file_inode(file);
1da177e4
LT
636 long timeout;
637 unsigned long *cpu_addr, data_buffer_length_adjusted = 0, flags = 0;
638 dma_addr_t dma_handle;
639 int request_id = 0;
640 unsigned int sequence_id = 0;
641 unsigned char event_index, start_index;
642 TW_Ioctl_Driver_Command driver_command;
643 TW_Ioctl_Buf_Apache *tw_ioctl;
644 TW_Lock *tw_lock;
645 TW_Command_Full *full_command_packet;
646 TW_Compatibility_Info *tw_compat_info;
647 TW_Event *event;
648 struct timeval current_time;
649 u32 current_time_ms;
650 TW_Device_Extension *tw_dev = twa_device_extension_list[iminor(inode)];
651 int retval = TW_IOCTL_ERROR_OS_EFAULT;
652 void __user *argp = (void __user *)arg;
653
c45d15d2 654 mutex_lock(&twa_chrdev_mutex);
f4927c45 655
1da177e4 656 /* Only let one of these through at a time */
a12e25bd 657 if (mutex_lock_interruptible(&tw_dev->ioctl_lock)) {
1da177e4
LT
658 retval = TW_IOCTL_ERROR_OS_EINTR;
659 goto out;
660 }
661
662 /* First copy down the driver command */
663 if (copy_from_user(&driver_command, argp, sizeof(TW_Ioctl_Driver_Command)))
664 goto out2;
665
666 /* Check data buffer size */
4039c30e 667 if (driver_command.buffer_length > TW_MAX_SECTORS * 2048) {
1da177e4
LT
668 retval = TW_IOCTL_ERROR_OS_EINVAL;
669 goto out2;
670 }
671
672 /* Hardware can only do multiple of 512 byte transfers */
673 data_buffer_length_adjusted = (driver_command.buffer_length + 511) & ~511;
674
675 /* Now allocate ioctl buf memory */
676 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);
677 if (!cpu_addr) {
678 retval = TW_IOCTL_ERROR_OS_ENOMEM;
679 goto out2;
680 }
681
682 tw_ioctl = (TW_Ioctl_Buf_Apache *)cpu_addr;
683
684 /* Now copy down the entire ioctl */
685 if (copy_from_user(tw_ioctl, argp, driver_command.buffer_length + sizeof(TW_Ioctl_Buf_Apache) - 1))
686 goto out3;
687
688 /* See which ioctl we are doing */
689 switch (cmd) {
690 case TW_IOCTL_FIRMWARE_PASS_THROUGH:
691 spin_lock_irqsave(tw_dev->host->host_lock, flags);
692 twa_get_request_id(tw_dev, &request_id);
693
694 /* Flag internal command */
695 tw_dev->srb[request_id] = NULL;
696
697 /* Flag chrdev ioctl */
698 tw_dev->chrdev_request_id = request_id;
699
700 full_command_packet = &tw_ioctl->firmware_command;
701
702 /* Load request id and sglist for both command types */
0e78d158 703 twa_load_sgl(tw_dev, full_command_packet, request_id, dma_handle, data_buffer_length_adjusted);
1da177e4
LT
704
705 memcpy(tw_dev->command_packet_virt[request_id], &(tw_ioctl->firmware_command), sizeof(TW_Command_Full));
706
707 /* Now post the command packet to the controller */
708 twa_post_command_packet(tw_dev, request_id, 1);
709 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
710
711 timeout = TW_IOCTL_CHRDEV_TIMEOUT*HZ;
712
713 /* Now wait for command to complete */
714 timeout = wait_event_timeout(tw_dev->ioctl_wqueue, tw_dev->chrdev_request_id == TW_IOCTL_CHRDEV_FREE, timeout);
715
1da177e4
LT
716 /* We timed out, and didn't get an interrupt */
717 if (tw_dev->chrdev_request_id != TW_IOCTL_CHRDEV_FREE) {
718 /* Now we need to reset the board */
719 printk(KERN_WARNING "3w-9xxx: scsi%d: WARNING: (0x%02X:0x%04X): Character ioctl (0x%x) timed out, resetting card.\n",
0e78d158 720 tw_dev->host->host_no, TW_DRIVER, 0x37,
1da177e4
LT
721 cmd);
722 retval = TW_IOCTL_ERROR_OS_EIO;
0e78d158 723 twa_reset_device_extension(tw_dev);
1da177e4
LT
724 goto out3;
725 }
726
727 /* Now copy in the command packet response */
728 memcpy(&(tw_ioctl->firmware_command), tw_dev->command_packet_virt[request_id], sizeof(TW_Command_Full));
729
730 /* Now complete the io */
731 spin_lock_irqsave(tw_dev->host->host_lock, flags);
732 tw_dev->posted_request_count--;
733 tw_dev->state[request_id] = TW_S_COMPLETED;
734 twa_free_request_id(tw_dev, request_id);
735 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
736 break;
737 case TW_IOCTL_GET_COMPATIBILITY_INFO:
738 tw_ioctl->driver_command.status = 0;
06fe9fb4 739 /* Copy compatibility struct into ioctl data buffer */
1da177e4 740 tw_compat_info = (TW_Compatibility_Info *)tw_ioctl->data_buffer;
4039c30e 741 memcpy(tw_compat_info, &tw_dev->tw_compat_info, sizeof(TW_Compatibility_Info));
1da177e4
LT
742 break;
743 case TW_IOCTL_GET_LAST_EVENT:
744 if (tw_dev->event_queue_wrapped) {
745 if (tw_dev->aen_clobber) {
746 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
747 tw_dev->aen_clobber = 0;
748 } else
749 tw_ioctl->driver_command.status = 0;
750 } else {
751 if (!tw_dev->error_index) {
752 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
753 break;
754 }
755 tw_ioctl->driver_command.status = 0;
756 }
757 event_index = (tw_dev->error_index - 1 + TW_Q_LENGTH) % TW_Q_LENGTH;
758 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
759 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
760 break;
761 case TW_IOCTL_GET_FIRST_EVENT:
762 if (tw_dev->event_queue_wrapped) {
763 if (tw_dev->aen_clobber) {
764 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
765 tw_dev->aen_clobber = 0;
766 } else
767 tw_ioctl->driver_command.status = 0;
768 event_index = tw_dev->error_index;
769 } else {
770 if (!tw_dev->error_index) {
771 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
772 break;
773 }
774 tw_ioctl->driver_command.status = 0;
775 event_index = 0;
776 }
777 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
778 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
779 break;
780 case TW_IOCTL_GET_NEXT_EVENT:
781 event = (TW_Event *)tw_ioctl->data_buffer;
782 sequence_id = event->sequence_id;
783 tw_ioctl->driver_command.status = 0;
784
785 if (tw_dev->event_queue_wrapped) {
786 if (tw_dev->aen_clobber) {
787 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
788 tw_dev->aen_clobber = 0;
789 }
790 start_index = tw_dev->error_index;
791 } else {
792 if (!tw_dev->error_index) {
793 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
794 break;
795 }
796 start_index = 0;
797 }
798 event_index = (start_index + sequence_id - tw_dev->event_queue[start_index]->sequence_id + 1) % TW_Q_LENGTH;
799
800 if (!(tw_dev->event_queue[event_index]->sequence_id > sequence_id)) {
801 if (tw_ioctl->driver_command.status == TW_IOCTL_ERROR_STATUS_AEN_CLOBBER)
802 tw_dev->aen_clobber = 1;
803 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
804 break;
805 }
806 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
807 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
808 break;
809 case TW_IOCTL_GET_PREVIOUS_EVENT:
810 event = (TW_Event *)tw_ioctl->data_buffer;
811 sequence_id = event->sequence_id;
812 tw_ioctl->driver_command.status = 0;
813
814 if (tw_dev->event_queue_wrapped) {
815 if (tw_dev->aen_clobber) {
816 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
817 tw_dev->aen_clobber = 0;
818 }
819 start_index = tw_dev->error_index;
820 } else {
821 if (!tw_dev->error_index) {
822 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
823 break;
824 }
825 start_index = 0;
826 }
827 event_index = (start_index + sequence_id - tw_dev->event_queue[start_index]->sequence_id - 1) % TW_Q_LENGTH;
828
829 if (!(tw_dev->event_queue[event_index]->sequence_id < sequence_id)) {
830 if (tw_ioctl->driver_command.status == TW_IOCTL_ERROR_STATUS_AEN_CLOBBER)
831 tw_dev->aen_clobber = 1;
832 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
833 break;
834 }
835 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
836 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
837 break;
838 case TW_IOCTL_GET_LOCK:
839 tw_lock = (TW_Lock *)tw_ioctl->data_buffer;
840 do_gettimeofday(&current_time);
841 current_time_ms = (current_time.tv_sec * 1000) + (current_time.tv_usec / 1000);
842
843 if ((tw_lock->force_flag == 1) || (tw_dev->ioctl_sem_lock == 0) || (current_time_ms >= tw_dev->ioctl_msec)) {
844 tw_dev->ioctl_sem_lock = 1;
845 tw_dev->ioctl_msec = current_time_ms + tw_lock->timeout_msec;
846 tw_ioctl->driver_command.status = 0;
847 tw_lock->time_remaining_msec = tw_lock->timeout_msec;
848 } else {
849 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_LOCKED;
850 tw_lock->time_remaining_msec = tw_dev->ioctl_msec - current_time_ms;
851 }
852 break;
853 case TW_IOCTL_RELEASE_LOCK:
854 if (tw_dev->ioctl_sem_lock == 1) {
855 tw_dev->ioctl_sem_lock = 0;
856 tw_ioctl->driver_command.status = 0;
857 } else {
858 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NOT_LOCKED;
859 }
860 break;
861 default:
862 retval = TW_IOCTL_ERROR_OS_ENOTTY;
863 goto out3;
864 }
865
866 /* Now copy the entire response to userspace */
867 if (copy_to_user(argp, tw_ioctl, sizeof(TW_Ioctl_Buf_Apache) + driver_command.buffer_length - 1) == 0)
868 retval = 0;
869out3:
870 /* Now free ioctl buf memory */
871 dma_free_coherent(&tw_dev->tw_pci_dev->dev, data_buffer_length_adjusted+sizeof(TW_Ioctl_Buf_Apache) - 1, cpu_addr, dma_handle);
872out2:
a12e25bd 873 mutex_unlock(&tw_dev->ioctl_lock);
1da177e4 874out:
c45d15d2 875 mutex_unlock(&twa_chrdev_mutex);
1da177e4
LT
876 return retval;
877} /* End twa_chrdev_ioctl() */
878
879/* This function handles open for the character device */
f2b9857e 880/* NOTE that this function will race with remove. */
1da177e4
LT
881static int twa_chrdev_open(struct inode *inode, struct file *file)
882{
883 unsigned int minor_number;
884 int retval = TW_IOCTL_ERROR_OS_ENODEV;
885
886 minor_number = iminor(inode);
887 if (minor_number >= twa_device_extension_count)
888 goto out;
889 retval = 0;
890out:
891 return retval;
892} /* End twa_chrdev_open() */
893
894/* This function will print readable messages from status register errors */
895static int twa_decode_bits(TW_Device_Extension *tw_dev, u32 status_reg_value)
896{
897 int retval = 1;
898
899 /* Check for various error conditions and handle them appropriately */
900 if (status_reg_value & TW_STATUS_PCI_PARITY_ERROR) {
901 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xc, "PCI Parity Error: clearing");
902 writel(TW_CONTROL_CLEAR_PARITY_ERROR, TW_CONTROL_REG_ADDR(tw_dev));
903 }
904
905 if (status_reg_value & TW_STATUS_PCI_ABORT) {
906 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xd, "PCI Abort: clearing");
907 writel(TW_CONTROL_CLEAR_PCI_ABORT, TW_CONTROL_REG_ADDR(tw_dev));
908 pci_write_config_word(tw_dev->tw_pci_dev, PCI_STATUS, TW_PCI_CLEAR_PCI_ABORT);
909 }
910
911 if (status_reg_value & TW_STATUS_QUEUE_ERROR) {
0e78d158
AR
912 if (((tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9650SE) &&
913 (tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9690SA)) ||
914 (!test_bit(TW_IN_RESET, &tw_dev->flags)))
4039c30e 915 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xe, "Controller Queue Error: clearing");
1da177e4
LT
916 writel(TW_CONTROL_CLEAR_QUEUE_ERROR, TW_CONTROL_REG_ADDR(tw_dev));
917 }
918
1da177e4
LT
919 if (status_reg_value & TW_STATUS_MICROCONTROLLER_ERROR) {
920 if (tw_dev->reset_print == 0) {
921 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x10, "Microcontroller Error: clearing");
922 tw_dev->reset_print = 1;
923 }
924 goto out;
925 }
926 retval = 0;
927out:
928 return retval;
929} /* End twa_decode_bits() */
930
931/* This function will empty the response queue */
932static int twa_empty_response_queue(TW_Device_Extension *tw_dev)
933{
934 u32 status_reg_value, response_que_value;
935 int count = 0, retval = 1;
936
937 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
938
939 while (((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) && (count < TW_MAX_RESPONSE_DRAIN)) {
940 response_que_value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
941 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
942 count++;
943 }
944 if (count == TW_MAX_RESPONSE_DRAIN)
945 goto out;
946
947 retval = 0;
948out:
949 return retval;
950} /* End twa_empty_response_queue() */
951
49bfd8db
AR
952/* This function will clear the pchip/response queue on 9550SX */
953static int twa_empty_response_queue_large(TW_Device_Extension *tw_dev)
954{
75913d9b
AR
955 u32 response_que_value = 0;
956 unsigned long before;
957 int retval = 1;
49bfd8db 958
0e78d158 959 if (tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9000) {
75913d9b
AR
960 before = jiffies;
961 while ((response_que_value & TW_9550SX_DRAIN_COMPLETED) != TW_9550SX_DRAIN_COMPLETED) {
49bfd8db 962 response_que_value = readl(TW_RESPONSE_QUEUE_REG_ADDR_LARGE(tw_dev));
4039c30e 963 msleep(1);
75913d9b 964 if (time_after(jiffies, before + HZ * 30))
49bfd8db 965 goto out;
49bfd8db 966 }
75913d9b
AR
967 /* P-chip settle time */
968 msleep(500);
49bfd8db
AR
969 retval = 0;
970 } else
971 retval = 0;
972out:
973 return retval;
974} /* End twa_empty_response_queue_large() */
975
1da177e4
LT
976/* This function passes sense keys from firmware to scsi layer */
977static int twa_fill_sense(TW_Device_Extension *tw_dev, int request_id, int copy_sense, int print_host)
978{
979 TW_Command_Full *full_command_packet;
980 unsigned short error;
981 int retval = 1;
982 char *error_str;
983
984 full_command_packet = tw_dev->command_packet_virt[request_id];
985
986 /* Check for embedded error string */
987 error_str = &(full_command_packet->header.err_specific_desc[strlen(full_command_packet->header.err_specific_desc) + 1]);
988
989 /* Don't print error for Logical unit not supported during rollcall */
75913d9b 990 error = le16_to_cpu(full_command_packet->header.status_block.error);
1da177e4
LT
991 if ((error != TW_ERROR_LOGICAL_UNIT_NOT_SUPPORTED) && (error != TW_ERROR_UNIT_OFFLINE)) {
992 if (print_host)
993 printk(KERN_WARNING "3w-9xxx: scsi%d: ERROR: (0x%02X:0x%04X): %s:%s.\n",
994 tw_dev->host->host_no,
995 TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
996 full_command_packet->header.status_block.error,
997 error_str[0] == '\0' ?
998 twa_string_lookup(twa_error_table,
999 full_command_packet->header.status_block.error) : error_str,
1000 full_command_packet->header.err_specific_desc);
1001 else
1002 printk(KERN_WARNING "3w-9xxx: ERROR: (0x%02X:0x%04X): %s:%s.\n",
1003 TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
1004 full_command_packet->header.status_block.error,
1005 error_str[0] == '\0' ?
1006 twa_string_lookup(twa_error_table,
1007 full_command_packet->header.status_block.error) : error_str,
1008 full_command_packet->header.err_specific_desc);
1009 }
1010
1011 if (copy_sense) {
1012 memcpy(tw_dev->srb[request_id]->sense_buffer, full_command_packet->header.sense_data, TW_SENSE_DATA_LENGTH);
1013 tw_dev->srb[request_id]->result = (full_command_packet->command.newcommand.status << 1);
1014 retval = TW_ISR_DONT_RESULT;
1015 goto out;
1016 }
1017 retval = 0;
1018out:
1019 return retval;
1020} /* End twa_fill_sense() */
1021
1022/* This function will free up device extension resources */
1023static void twa_free_device_extension(TW_Device_Extension *tw_dev)
1024{
1025 if (tw_dev->command_packet_virt[0])
1026 pci_free_consistent(tw_dev->tw_pci_dev,
1027 sizeof(TW_Command_Full)*TW_Q_LENGTH,
1028 tw_dev->command_packet_virt[0],
1029 tw_dev->command_packet_phys[0]);
1030
1031 if (tw_dev->generic_buffer_virt[0])
1032 pci_free_consistent(tw_dev->tw_pci_dev,
1033 TW_SECTOR_SIZE*TW_Q_LENGTH,
1034 tw_dev->generic_buffer_virt[0],
1035 tw_dev->generic_buffer_phys[0]);
1036
c9475cb0 1037 kfree(tw_dev->event_queue[0]);
1da177e4
LT
1038} /* End twa_free_device_extension() */
1039
1040/* This function will free a request id */
1041static void twa_free_request_id(TW_Device_Extension *tw_dev, int request_id)
1042{
1043 tw_dev->free_queue[tw_dev->free_tail] = request_id;
1044 tw_dev->state[request_id] = TW_S_FINISHED;
1045 tw_dev->free_tail = (tw_dev->free_tail + 1) % TW_Q_LENGTH;
1046} /* End twa_free_request_id() */
1047
7f927fcc 1048/* This function will get parameter table entries from the firmware */
1da177e4
LT
1049static void *twa_get_param(TW_Device_Extension *tw_dev, int request_id, int table_id, int parameter_id, int parameter_size_bytes)
1050{
1051 TW_Command_Full *full_command_packet;
1052 TW_Command *command_packet;
1053 TW_Param_Apache *param;
1da177e4
LT
1054 void *retval = NULL;
1055
1056 /* Setup the command packet */
1057 full_command_packet = tw_dev->command_packet_virt[request_id];
1058 memset(full_command_packet, 0, sizeof(TW_Command_Full));
1059 command_packet = &full_command_packet->command.oldcommand;
1060
1061 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_GET_PARAM);
1062 command_packet->size = TW_COMMAND_SIZE;
1063 command_packet->request_id = request_id;
75913d9b 1064 command_packet->byte6_offset.block_count = cpu_to_le16(1);
1da177e4
LT
1065
1066 /* Now setup the param */
1067 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
1068 memset(param, 0, TW_SECTOR_SIZE);
75913d9b
AR
1069 param->table_id = cpu_to_le16(table_id | 0x8000);
1070 param->parameter_id = cpu_to_le16(parameter_id);
1071 param->parameter_size_bytes = cpu_to_le16(parameter_size_bytes);
1da177e4 1072
3dabec71 1073 command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
75913d9b 1074 command_packet->byte8_offset.param.sgl[0].length = cpu_to_le32(TW_SECTOR_SIZE);
1da177e4
LT
1075
1076 /* Post the command packet to the board */
1077 twa_post_command_packet(tw_dev, request_id, 1);
1078
1079 /* Poll for completion */
1080 if (twa_poll_response(tw_dev, request_id, 30))
1081 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x13, "No valid response during get param")
1082 else
1083 retval = (void *)&(param->data[0]);
1084
1085 tw_dev->posted_request_count--;
1086 tw_dev->state[request_id] = TW_S_INITIAL;
1087
1088 return retval;
1089} /* End twa_get_param() */
1090
1091/* This function will assign an available request id */
1092static void twa_get_request_id(TW_Device_Extension *tw_dev, int *request_id)
1093{
1094 *request_id = tw_dev->free_queue[tw_dev->free_head];
1095 tw_dev->free_head = (tw_dev->free_head + 1) % TW_Q_LENGTH;
1096 tw_dev->state[*request_id] = TW_S_STARTED;
1097} /* End twa_get_request_id() */
1098
1099/* This function will send an initconnection command to controller */
1100static int twa_initconnection(TW_Device_Extension *tw_dev, int message_credits,
1101 u32 set_features, unsigned short current_fw_srl,
1102 unsigned short current_fw_arch_id,
1103 unsigned short current_fw_branch,
1104 unsigned short current_fw_build,
1105 unsigned short *fw_on_ctlr_srl,
1106 unsigned short *fw_on_ctlr_arch_id,
1107 unsigned short *fw_on_ctlr_branch,
1108 unsigned short *fw_on_ctlr_build,
1109 u32 *init_connect_result)
1110{
1111 TW_Command_Full *full_command_packet;
1112 TW_Initconnect *tw_initconnect;
1113 int request_id = 0, retval = 1;
1114
1115 /* Initialize InitConnection command packet */
1116 full_command_packet = tw_dev->command_packet_virt[request_id];
1117 memset(full_command_packet, 0, sizeof(TW_Command_Full));
1118 full_command_packet->header.header_desc.size_header = 128;
1119
1120 tw_initconnect = (TW_Initconnect *)&full_command_packet->command.oldcommand;
1121 tw_initconnect->opcode__reserved = TW_OPRES_IN(0, TW_OP_INIT_CONNECTION);
1122 tw_initconnect->request_id = request_id;
75913d9b 1123 tw_initconnect->message_credits = cpu_to_le16(message_credits);
1da177e4
LT
1124 tw_initconnect->features = set_features;
1125
1126 /* Turn on 64-bit sgl support if we need to */
1127 tw_initconnect->features |= sizeof(dma_addr_t) > 4 ? 1 : 0;
1128
75913d9b
AR
1129 tw_initconnect->features = cpu_to_le32(tw_initconnect->features);
1130
1da177e4
LT
1131 if (set_features & TW_EXTENDED_INIT_CONNECT) {
1132 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE_EXTENDED;
75913d9b
AR
1133 tw_initconnect->fw_srl = cpu_to_le16(current_fw_srl);
1134 tw_initconnect->fw_arch_id = cpu_to_le16(current_fw_arch_id);
1135 tw_initconnect->fw_branch = cpu_to_le16(current_fw_branch);
1136 tw_initconnect->fw_build = cpu_to_le16(current_fw_build);
1da177e4
LT
1137 } else
1138 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE;
1139
1140 /* Send command packet to the board */
1141 twa_post_command_packet(tw_dev, request_id, 1);
1142
1143 /* Poll for completion */
1144 if (twa_poll_response(tw_dev, request_id, 30)) {
1145 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x15, "No valid response during init connection");
1146 } else {
1147 if (set_features & TW_EXTENDED_INIT_CONNECT) {
75913d9b
AR
1148 *fw_on_ctlr_srl = le16_to_cpu(tw_initconnect->fw_srl);
1149 *fw_on_ctlr_arch_id = le16_to_cpu(tw_initconnect->fw_arch_id);
1150 *fw_on_ctlr_branch = le16_to_cpu(tw_initconnect->fw_branch);
1151 *fw_on_ctlr_build = le16_to_cpu(tw_initconnect->fw_build);
1152 *init_connect_result = le32_to_cpu(tw_initconnect->result);
1da177e4
LT
1153 }
1154 retval = 0;
1155 }
1156
1157 tw_dev->posted_request_count--;
1158 tw_dev->state[request_id] = TW_S_INITIAL;
1159
1160 return retval;
1161} /* End twa_initconnection() */
1162
1163/* This function will initialize the fields of a device extension */
1164static int twa_initialize_device_extension(TW_Device_Extension *tw_dev)
1165{
1166 int i, retval = 1;
1167
1168 /* Initialize command packet buffers */
1169 if (twa_allocate_memory(tw_dev, sizeof(TW_Command_Full), 0)) {
1170 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x16, "Command packet memory allocation failed");
1171 goto out;
1172 }
1173
1174 /* Initialize generic buffer */
1175 if (twa_allocate_memory(tw_dev, TW_SECTOR_SIZE, 1)) {
1176 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x17, "Generic memory allocation failed");
1177 goto out;
1178 }
1179
1180 /* Allocate event info space */
dd00cc48 1181 tw_dev->event_queue[0] = kcalloc(TW_Q_LENGTH, sizeof(TW_Event), GFP_KERNEL);
1da177e4
LT
1182 if (!tw_dev->event_queue[0]) {
1183 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x18, "Event info memory allocation failed");
1184 goto out;
1185 }
1186
1da177e4
LT
1187
1188 for (i = 0; i < TW_Q_LENGTH; i++) {
1189 tw_dev->event_queue[i] = (TW_Event *)((unsigned char *)tw_dev->event_queue[0] + (i * sizeof(TW_Event)));
1190 tw_dev->free_queue[i] = i;
1191 tw_dev->state[i] = TW_S_INITIAL;
1192 }
1193
1194 tw_dev->pending_head = TW_Q_START;
1195 tw_dev->pending_tail = TW_Q_START;
1196 tw_dev->free_head = TW_Q_START;
1197 tw_dev->free_tail = TW_Q_START;
1198 tw_dev->error_sequence_id = 1;
1199 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1200
a12e25bd 1201 mutex_init(&tw_dev->ioctl_lock);
1da177e4
LT
1202 init_waitqueue_head(&tw_dev->ioctl_wqueue);
1203
1204 retval = 0;
1205out:
1206 return retval;
1207} /* End twa_initialize_device_extension() */
1208
1209/* This function is the interrupt service routine */
7d12e780 1210static irqreturn_t twa_interrupt(int irq, void *dev_instance)
1da177e4
LT
1211{
1212 int request_id, error = 0;
1213 u32 status_reg_value;
1214 TW_Response_Queue response_que;
1215 TW_Command_Full *full_command_packet;
1da177e4
LT
1216 TW_Device_Extension *tw_dev = (TW_Device_Extension *)dev_instance;
1217 int handled = 0;
1218
1219 /* Get the per adapter lock */
1220 spin_lock(tw_dev->host->host_lock);
1221
1222 /* Read the registers */
1223 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1224
1225 /* Check if this is our interrupt, otherwise bail */
1226 if (!(status_reg_value & TW_STATUS_VALID_INTERRUPT))
1227 goto twa_interrupt_bail;
1228
1229 handled = 1;
1230
4039c30e
AR
1231 /* If we are resetting, bail */
1232 if (test_bit(TW_IN_RESET, &tw_dev->flags))
1233 goto twa_interrupt_bail;
1234
1da177e4
LT
1235 /* Check controller for errors */
1236 if (twa_check_bits(status_reg_value)) {
1237 if (twa_decode_bits(tw_dev, status_reg_value)) {
1238 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1239 goto twa_interrupt_bail;
1240 }
1241 }
1242
1243 /* Handle host interrupt */
1244 if (status_reg_value & TW_STATUS_HOST_INTERRUPT)
1245 TW_CLEAR_HOST_INTERRUPT(tw_dev);
1246
1247 /* Handle attention interrupt */
1248 if (status_reg_value & TW_STATUS_ATTENTION_INTERRUPT) {
1249 TW_CLEAR_ATTENTION_INTERRUPT(tw_dev);
1250 if (!(test_and_set_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags))) {
1251 twa_get_request_id(tw_dev, &request_id);
1252
1253 error = twa_aen_read_queue(tw_dev, request_id);
1254 if (error) {
1255 tw_dev->state[request_id] = TW_S_COMPLETED;
1256 twa_free_request_id(tw_dev, request_id);
1257 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
1258 }
1259 }
1260 }
1261
1262 /* Handle command interrupt */
1263 if (status_reg_value & TW_STATUS_COMMAND_INTERRUPT) {
1264 TW_MASK_COMMAND_INTERRUPT(tw_dev);
1265 /* Drain as many pending commands as we can */
1266 while (tw_dev->pending_request_count > 0) {
1267 request_id = tw_dev->pending_queue[tw_dev->pending_head];
1268 if (tw_dev->state[request_id] != TW_S_PENDING) {
1269 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x19, "Found request id that wasn't pending");
1270 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1271 goto twa_interrupt_bail;
1272 }
1273 if (twa_post_command_packet(tw_dev, request_id, 1)==0) {
1274 tw_dev->pending_head = (tw_dev->pending_head + 1) % TW_Q_LENGTH;
1275 tw_dev->pending_request_count--;
1276 } else {
1277 /* If we get here, we will continue re-posting on the next command interrupt */
1278 break;
1279 }
1280 }
1281 }
1282
1283 /* Handle response interrupt */
1284 if (status_reg_value & TW_STATUS_RESPONSE_INTERRUPT) {
1285
1286 /* Drain the response queue from the board */
1287 while ((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) {
1288 /* Complete the response */
1289 response_que.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
1290 request_id = TW_RESID_OUT(response_que.response_id);
1291 full_command_packet = tw_dev->command_packet_virt[request_id];
1292 error = 0;
1da177e4
LT
1293 /* Check for command packet errors */
1294 if (full_command_packet->command.newcommand.status != 0) {
9bcf0910 1295 if (tw_dev->srb[request_id] != NULL) {
1da177e4
LT
1296 error = twa_fill_sense(tw_dev, request_id, 1, 1);
1297 } else {
1298 /* Skip ioctl error prints */
1299 if (request_id != tw_dev->chrdev_request_id) {
1300 error = twa_fill_sense(tw_dev, request_id, 0, 1);
1301 }
1302 }
1303 }
1304
1305 /* Check for correct state */
1306 if (tw_dev->state[request_id] != TW_S_POSTED) {
9bcf0910 1307 if (tw_dev->srb[request_id] != NULL) {
1da177e4
LT
1308 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1a, "Received a request id that wasn't posted");
1309 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1310 goto twa_interrupt_bail;
1311 }
1312 }
1313
1314 /* Check for internal command completion */
9bcf0910 1315 if (tw_dev->srb[request_id] == NULL) {
1da177e4
LT
1316 if (request_id != tw_dev->chrdev_request_id) {
1317 if (twa_aen_complete(tw_dev, request_id))
1318 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1b, "Error completing AEN during attention interrupt");
1319 } else {
1320 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1321 wake_up(&tw_dev->ioctl_wqueue);
1322 }
1323 } else {
0debe01d
FT
1324 struct scsi_cmnd *cmd;
1325
1326 cmd = tw_dev->srb[request_id];
1327
1da177e4
LT
1328 twa_scsiop_execute_scsi_complete(tw_dev, request_id);
1329 /* If no error command was a success */
1330 if (error == 0) {
0debe01d 1331 cmd->result = (DID_OK << 16);
1da177e4
LT
1332 }
1333
1334 /* If error, command failed */
1335 if (error == 1) {
1336 /* Ask for a host reset */
0debe01d 1337 cmd->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
1da177e4
LT
1338 }
1339
1340 /* Report residual bytes for single sgl */
0debe01d
FT
1341 if ((scsi_sg_count(cmd) <= 1) && (full_command_packet->command.newcommand.status == 0)) {
1342 if (full_command_packet->command.newcommand.sg_list[0].length < scsi_bufflen(tw_dev->srb[request_id]))
1343 scsi_set_resid(cmd, scsi_bufflen(cmd) - full_command_packet->command.newcommand.sg_list[0].length);
1da177e4
LT
1344 }
1345
1346 /* Now complete the io */
15e3d5a2
CH
1347 if (twa_command_mapped(cmd))
1348 scsi_dma_unmap(cmd);
118c855b 1349 cmd->scsi_done(cmd);
1da177e4
LT
1350 tw_dev->state[request_id] = TW_S_COMPLETED;
1351 twa_free_request_id(tw_dev, request_id);
1352 tw_dev->posted_request_count--;
1da177e4
LT
1353 }
1354
1355 /* Check for valid status after each drain */
1356 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1357 if (twa_check_bits(status_reg_value)) {
1358 if (twa_decode_bits(tw_dev, status_reg_value)) {
1359 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1360 goto twa_interrupt_bail;
1361 }
1362 }
1363 }
1364 }
1365
1366twa_interrupt_bail:
1367 spin_unlock(tw_dev->host->host_lock);
1368 return IRQ_RETVAL(handled);
1369} /* End twa_interrupt() */
1370
1371/* This function will load the request id and various sgls for ioctls */
0e78d158 1372static 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)
1da177e4
LT
1373{
1374 TW_Command *oldcommand;
1375 TW_Command_Apache *newcommand;
1376 TW_SG_Entry *sgl;
0e78d158
AR
1377 unsigned int pae = 0;
1378
1379 if ((sizeof(long) < 8) && (sizeof(dma_addr_t) > 4))
1380 pae = 1;
1da177e4
LT
1381
1382 if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
1383 newcommand = &full_command_packet->command.newcommand;
4039c30e
AR
1384 newcommand->request_id__lunl =
1385 cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->request_id__lunl), request_id));
53ca3535
AR
1386 if (length) {
1387 newcommand->sg_list[0].address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1);
1388 newcommand->sg_list[0].length = cpu_to_le32(length);
1389 }
1da177e4 1390 newcommand->sgl_entries__lunh =
53ca3535 1391 cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->sgl_entries__lunh), length ? 1 : 0));
1da177e4
LT
1392 } else {
1393 oldcommand = &full_command_packet->command.oldcommand;
1394 oldcommand->request_id = request_id;
1395
1396 if (TW_SGL_OUT(oldcommand->opcode__sgloffset)) {
1397 /* Load the sg list */
0e78d158
AR
1398 if (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)
1399 sgl = (TW_SG_Entry *)((u32 *)oldcommand+oldcommand->size - (sizeof(TW_SG_Entry)/4) + pae);
1400 else
1401 sgl = (TW_SG_Entry *)((u32 *)oldcommand+TW_SGL_OUT(oldcommand->opcode__sgloffset));
75913d9b
AR
1402 sgl->address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1);
1403 sgl->length = cpu_to_le32(length);
1da177e4 1404
0e78d158 1405 oldcommand->size += pae;
1da177e4
LT
1406 }
1407 }
1408} /* End twa_load_sgl() */
1409
1da177e4
LT
1410/* This function will poll for a response interrupt of a request */
1411static int twa_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds)
1412{
1413 int retval = 1, found = 0, response_request_id;
1414 TW_Response_Queue response_queue;
1415 TW_Command_Full *full_command_packet = tw_dev->command_packet_virt[request_id];
1416
1417 if (twa_poll_status_gone(tw_dev, TW_STATUS_RESPONSE_QUEUE_EMPTY, seconds) == 0) {
1418 response_queue.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
1419 response_request_id = TW_RESID_OUT(response_queue.response_id);
1420 if (request_id != response_request_id) {
1421 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1e, "Found unexpected request id while polling for response");
1422 goto out;
1423 }
1424 if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
1425 if (full_command_packet->command.newcommand.status != 0) {
1426 /* bad response */
1427 twa_fill_sense(tw_dev, request_id, 0, 0);
1428 goto out;
1429 }
1430 found = 1;
1431 } else {
1432 if (full_command_packet->command.oldcommand.status != 0) {
1433 /* bad response */
1434 twa_fill_sense(tw_dev, request_id, 0, 0);
1435 goto out;
1436 }
1437 found = 1;
1438 }
1439 }
1440
1441 if (found)
1442 retval = 0;
1443out:
1444 return retval;
1445} /* End twa_poll_response() */
1446
1447/* This function will poll the status register for a flag */
1448static int twa_poll_status(TW_Device_Extension *tw_dev, u32 flag, int seconds)
1449{
1450 u32 status_reg_value;
1451 unsigned long before;
1452 int retval = 1;
1453
1454 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1455 before = jiffies;
1456
1457 if (twa_check_bits(status_reg_value))
1458 twa_decode_bits(tw_dev, status_reg_value);
1459
1460 while ((status_reg_value & flag) != flag) {
1461 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1462
1463 if (twa_check_bits(status_reg_value))
1464 twa_decode_bits(tw_dev, status_reg_value);
1465
1466 if (time_after(jiffies, before + HZ * seconds))
1467 goto out;
1468
1469 msleep(50);
1470 }
1471 retval = 0;
1472out:
1473 return retval;
1474} /* End twa_poll_status() */
1475
1476/* This function will poll the status register for disappearance of a flag */
1477static int twa_poll_status_gone(TW_Device_Extension *tw_dev, u32 flag, int seconds)
1478{
1479 u32 status_reg_value;
1480 unsigned long before;
1481 int retval = 1;
1482
1483 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1484 before = jiffies;
1485
1486 if (twa_check_bits(status_reg_value))
1487 twa_decode_bits(tw_dev, status_reg_value);
1488
1489 while ((status_reg_value & flag) != 0) {
1490 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1491 if (twa_check_bits(status_reg_value))
1492 twa_decode_bits(tw_dev, status_reg_value);
1493
1494 if (time_after(jiffies, before + HZ * seconds))
1495 goto out;
1496
1497 msleep(50);
1498 }
1499 retval = 0;
1500out:
1501 return retval;
1502} /* End twa_poll_status_gone() */
1503
1504/* This function will attempt to post a command packet to the board */
1505static int twa_post_command_packet(TW_Device_Extension *tw_dev, int request_id, char internal)
1506{
1507 u32 status_reg_value;
1508 dma_addr_t command_que_value;
1509 int retval = 1;
1510
1511 command_que_value = tw_dev->command_packet_phys[request_id];
4039c30e
AR
1512
1513 /* For 9650SE write low 4 bytes first */
0e78d158
AR
1514 if ((tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
1515 (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)) {
4039c30e
AR
1516 command_que_value += TW_COMMAND_OFFSET;
1517 writel((u32)command_que_value, TW_COMMAND_QUEUE_REG_ADDR_LARGE(tw_dev));
1518 }
1519
1da177e4
LT
1520 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1521
1522 if (twa_check_bits(status_reg_value))
1523 twa_decode_bits(tw_dev, status_reg_value);
1524
1525 if (((tw_dev->pending_request_count > 0) && (tw_dev->state[request_id] != TW_S_PENDING)) || (status_reg_value & TW_STATUS_COMMAND_QUEUE_FULL)) {
1526
1527 /* Only pend internal driver commands */
1528 if (!internal) {
1529 retval = SCSI_MLQUEUE_HOST_BUSY;
1530 goto out;
1531 }
1532
1533 /* Couldn't post the command packet, so we do it later */
1534 if (tw_dev->state[request_id] != TW_S_PENDING) {
1535 tw_dev->state[request_id] = TW_S_PENDING;
1536 tw_dev->pending_request_count++;
1537 if (tw_dev->pending_request_count > tw_dev->max_pending_request_count) {
1538 tw_dev->max_pending_request_count = tw_dev->pending_request_count;
1539 }
1540 tw_dev->pending_queue[tw_dev->pending_tail] = request_id;
1541 tw_dev->pending_tail = (tw_dev->pending_tail + 1) % TW_Q_LENGTH;
1542 }
1543 TW_UNMASK_COMMAND_INTERRUPT(tw_dev);
1544 goto out;
1545 } else {
0e78d158
AR
1546 if ((tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
1547 (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)) {
4039c30e
AR
1548 /* Now write upper 4 bytes */
1549 writel((u32)((u64)command_que_value >> 32), TW_COMMAND_QUEUE_REG_ADDR_LARGE(tw_dev) + 0x4);
1da177e4 1550 } else {
4039c30e
AR
1551 if (sizeof(dma_addr_t) > 4) {
1552 command_que_value += TW_COMMAND_OFFSET;
1553 writel((u32)command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
1554 writel((u32)((u64)command_que_value >> 32), TW_COMMAND_QUEUE_REG_ADDR(tw_dev) + 0x4);
1555 } else {
1556 writel(TW_COMMAND_OFFSET + command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
1557 }
1da177e4
LT
1558 }
1559 tw_dev->state[request_id] = TW_S_POSTED;
1560 tw_dev->posted_request_count++;
1561 if (tw_dev->posted_request_count > tw_dev->max_posted_request_count) {
1562 tw_dev->max_posted_request_count = tw_dev->posted_request_count;
1563 }
1564 }
1565 retval = 0;
1566out:
1567 return retval;
1568} /* End twa_post_command_packet() */
1569
1570/* This function will reset a device extension */
0e78d158 1571static int twa_reset_device_extension(TW_Device_Extension *tw_dev)
1da177e4
LT
1572{
1573 int i = 0;
1574 int retval = 1;
1575 unsigned long flags = 0;
1576
1577 set_bit(TW_IN_RESET, &tw_dev->flags);
1578 TW_DISABLE_INTERRUPTS(tw_dev);
1579 TW_MASK_COMMAND_INTERRUPT(tw_dev);
1580 spin_lock_irqsave(tw_dev->host->host_lock, flags);
1581
1582 /* Abort all requests that are in progress */
1583 for (i = 0; i < TW_Q_LENGTH; i++) {
1584 if ((tw_dev->state[i] != TW_S_FINISHED) &&
1585 (tw_dev->state[i] != TW_S_INITIAL) &&
1586 (tw_dev->state[i] != TW_S_COMPLETED)) {
1587 if (tw_dev->srb[i]) {
118c855b
CH
1588 struct scsi_cmnd *cmd = tw_dev->srb[i];
1589
1590 cmd->result = (DID_RESET << 16);
15e3d5a2
CH
1591 if (twa_command_mapped(cmd))
1592 scsi_dma_unmap(cmd);
118c855b 1593 cmd->scsi_done(cmd);
1da177e4
LT
1594 }
1595 }
1596 }
1597
1598 /* Reset queues and counts */
1599 for (i = 0; i < TW_Q_LENGTH; i++) {
1600 tw_dev->free_queue[i] = i;
1601 tw_dev->state[i] = TW_S_INITIAL;
1602 }
1603 tw_dev->free_head = TW_Q_START;
1604 tw_dev->free_tail = TW_Q_START;
1605 tw_dev->posted_request_count = 0;
1606 tw_dev->pending_request_count = 0;
1607 tw_dev->pending_head = TW_Q_START;
1608 tw_dev->pending_tail = TW_Q_START;
1609 tw_dev->reset_print = 0;
1610
1611 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
1612
1613 if (twa_reset_sequence(tw_dev, 1))
1614 goto out;
1615
1616 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
4039c30e
AR
1617 clear_bit(TW_IN_RESET, &tw_dev->flags);
1618 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1da177e4 1619
1da177e4
LT
1620 retval = 0;
1621out:
1622 return retval;
1623} /* End twa_reset_device_extension() */
1624
1625/* This function will reset a controller */
1626static int twa_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset)
1627{
1628 int tries = 0, retval = 1, flashed = 0, do_soft_reset = soft_reset;
1629
1630 while (tries < TW_MAX_RESET_TRIES) {
49bfd8db 1631 if (do_soft_reset) {
1da177e4 1632 TW_SOFT_RESET(tw_dev);
49bfd8db
AR
1633 /* Clear pchip/response queue on 9550SX */
1634 if (twa_empty_response_queue_large(tw_dev)) {
1635 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x36, "Response queue (large) empty failed during reset sequence");
1636 do_soft_reset = 1;
1637 tries++;
1638 continue;
1639 }
1640 }
1da177e4
LT
1641
1642 /* Make sure controller is in a good state */
1643 if (twa_poll_status(tw_dev, TW_STATUS_MICROCONTROLLER_READY | (do_soft_reset == 1 ? TW_STATUS_ATTENTION_INTERRUPT : 0), 60)) {
1644 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1f, "Microcontroller not ready during reset sequence");
1645 do_soft_reset = 1;
1646 tries++;
1647 continue;
1648 }
1649
1650 /* Empty response queue */
1651 if (twa_empty_response_queue(tw_dev)) {
1652 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x20, "Response queue empty failed during reset sequence");
1653 do_soft_reset = 1;
1654 tries++;
1655 continue;
1656 }
1657
1658 flashed = 0;
1659
1660 /* Check for compatibility/flash */
1661 if (twa_check_srl(tw_dev, &flashed)) {
1662 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x21, "Compatibility check failed during reset sequence");
1663 do_soft_reset = 1;
1664 tries++;
1665 continue;
1666 } else {
1667 if (flashed) {
1668 tries++;
1669 continue;
1670 }
1671 }
1672
1673 /* Drain the AEN queue */
1674 if (twa_aen_drain_queue(tw_dev, soft_reset)) {
1675 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x22, "AEN drain failed during reset sequence");
1676 do_soft_reset = 1;
1677 tries++;
1678 continue;
1679 }
1680
1681 /* If we got here, controller is in a good state */
1682 retval = 0;
1683 goto out;
1684 }
1685out:
1686 return retval;
1687} /* End twa_reset_sequence() */
1688
1689/* This funciton returns unit geometry in cylinders/heads/sectors */
1690static int twa_scsi_biosparam(struct scsi_device *sdev, struct block_device *bdev, sector_t capacity, int geom[])
1691{
1692 int heads, sectors, cylinders;
1693 TW_Device_Extension *tw_dev;
1694
1695 tw_dev = (TW_Device_Extension *)sdev->host->hostdata;
1696
1697 if (capacity >= 0x200000) {
1698 heads = 255;
1699 sectors = 63;
1700 cylinders = sector_div(capacity, heads * sectors);
1701 } else {
1702 heads = 64;
1703 sectors = 32;
1704 cylinders = sector_div(capacity, heads * sectors);
1705 }
1706
1707 geom[0] = heads;
1708 geom[1] = sectors;
1709 geom[2] = cylinders;
1710
1711 return 0;
1712} /* End twa_scsi_biosparam() */
1713
1714/* This is the new scsi eh reset function */
1715static int twa_scsi_eh_reset(struct scsi_cmnd *SCpnt)
1716{
1717 TW_Device_Extension *tw_dev = NULL;
1718 int retval = FAILED;
1719
1720 tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1721
1da177e4
LT
1722 tw_dev->num_resets++;
1723
017560fc
JG
1724 sdev_printk(KERN_WARNING, SCpnt->device,
1725 "WARNING: (0x%02X:0x%04X): Command (0x%x) timed out, resetting card.\n",
1726 TW_DRIVER, 0x2c, SCpnt->cmnd[0]);
1da177e4 1727
4039c30e
AR
1728 /* Make sure we are not issuing an ioctl or resetting from ioctl */
1729 mutex_lock(&tw_dev->ioctl_lock);
1730
1da177e4 1731 /* Now reset the card and some of the device extension data */
0e78d158 1732 if (twa_reset_device_extension(tw_dev)) {
1da177e4
LT
1733 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2b, "Controller reset failed during scsi host reset");
1734 goto out;
1735 }
1736
1737 retval = SUCCESS;
1738out:
4039c30e 1739 mutex_unlock(&tw_dev->ioctl_lock);
1da177e4
LT
1740 return retval;
1741} /* End twa_scsi_eh_reset() */
1742
1743/* This is the main scsi queue function to handle scsi opcodes */
f281233d 1744static int twa_scsi_queue_lck(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
1da177e4
LT
1745{
1746 int request_id, retval;
1747 TW_Device_Extension *tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1748
4039c30e
AR
1749 /* If we are resetting due to timed out ioctl, report as busy */
1750 if (test_bit(TW_IN_RESET, &tw_dev->flags)) {
1751 retval = SCSI_MLQUEUE_HOST_BUSY;
1752 goto out;
1753 }
1754
1da177e4 1755 /* Check if this FW supports luns */
4039c30e 1756 if ((SCpnt->device->lun != 0) && (tw_dev->tw_compat_info.working_srl < TW_FW_SRL_LUNS_SUPPORTED)) {
1da177e4
LT
1757 SCpnt->result = (DID_BAD_TARGET << 16);
1758 done(SCpnt);
1759 retval = 0;
1760 goto out;
1761 }
1762
1763 /* Save done function into scsi_cmnd struct */
1764 SCpnt->scsi_done = done;
1765
1766 /* Get a free request id */
1767 twa_get_request_id(tw_dev, &request_id);
1768
1769 /* Save the scsi command for use by the ISR */
1770 tw_dev->srb[request_id] = SCpnt;
1771
1da177e4
LT
1772 retval = twa_scsiop_execute_scsi(tw_dev, request_id, NULL, 0, NULL);
1773 switch (retval) {
1774 case SCSI_MLQUEUE_HOST_BUSY:
15e3d5a2
CH
1775 if (twa_command_mapped(SCpnt))
1776 scsi_dma_unmap(SCpnt);
1da177e4
LT
1777 twa_free_request_id(tw_dev, request_id);
1778 break;
1779 case 1:
1da177e4 1780 SCpnt->result = (DID_ERROR << 16);
15e3d5a2
CH
1781 if (twa_command_mapped(SCpnt))
1782 scsi_dma_unmap(SCpnt);
1da177e4 1783 done(SCpnt);
118c855b
CH
1784 tw_dev->state[request_id] = TW_S_COMPLETED;
1785 twa_free_request_id(tw_dev, request_id);
1da177e4
LT
1786 retval = 0;
1787 }
1788out:
1789 return retval;
1790} /* End twa_scsi_queue() */
1791
f281233d
JG
1792static DEF_SCSI_QCMD(twa_scsi_queue)
1793
1da177e4
LT
1794/* This function hands scsi cdb's to the firmware */
1795static int twa_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id, char *cdb, int use_sg, TW_SG_Entry *sglistarg)
1796{
1797 TW_Command_Full *full_command_packet;
1798 TW_Command_Apache *command_packet;
1799 u32 num_sectors = 0x0;
1800 int i, sg_count;
1801 struct scsi_cmnd *srb = NULL;
0debe01d 1802 struct scatterlist *sglist = NULL, *sg;
1da177e4
LT
1803 int retval = 1;
1804
1805 if (tw_dev->srb[request_id]) {
1da177e4 1806 srb = tw_dev->srb[request_id];
0debe01d
FT
1807 if (scsi_sglist(srb))
1808 sglist = scsi_sglist(srb);
1da177e4
LT
1809 }
1810
1811 /* Initialize command packet */
1812 full_command_packet = tw_dev->command_packet_virt[request_id];
1813 full_command_packet->header.header_desc.size_header = 128;
1814 full_command_packet->header.status_block.error = 0;
1815 full_command_packet->header.status_block.severity__reserved = 0;
1816
1817 command_packet = &full_command_packet->command.newcommand;
1818 command_packet->status = 0;
1819 command_packet->opcode__reserved = TW_OPRES_IN(0, TW_OP_EXECUTE_SCSI);
1820
1821 /* We forced 16 byte cdb use earlier */
1822 if (!cdb)
1823 memcpy(command_packet->cdb, srb->cmnd, TW_MAX_CDB_LEN);
1824 else
1825 memcpy(command_packet->cdb, cdb, TW_MAX_CDB_LEN);
1826
1827 if (srb) {
1828 command_packet->unit = srb->device->id;
1829 command_packet->request_id__lunl =
75913d9b 1830 cpu_to_le16(TW_REQ_LUN_IN(srb->device->lun, request_id));
1da177e4
LT
1831 } else {
1832 command_packet->request_id__lunl =
75913d9b 1833 cpu_to_le16(TW_REQ_LUN_IN(0, request_id));
1da177e4
LT
1834 command_packet->unit = 0;
1835 }
1836
1837 command_packet->sgl_offset = 16;
1838
1839 if (!sglistarg) {
1840 /* Map sglist from scsi layer to cmd packet */
1da177e4 1841
0debe01d 1842 if (scsi_sg_count(srb)) {
15e3d5a2 1843 if (!twa_command_mapped(srb)) {
035f5e06
FT
1844 if (srb->sc_data_direction == DMA_TO_DEVICE ||
1845 srb->sc_data_direction == DMA_BIDIRECTIONAL)
1846 scsi_sg_copy_to_buffer(srb,
1847 tw_dev->generic_buffer_virt[request_id],
1848 TW_SECTOR_SIZE);
75913d9b
AR
1849 command_packet->sg_list[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
1850 command_packet->sg_list[0].length = cpu_to_le32(TW_MIN_SGL_LENGTH);
1da177e4 1851 } else {
118c855b
CH
1852 sg_count = scsi_dma_map(srb);
1853 if (sg_count < 0)
1da177e4
LT
1854 goto out;
1855
0debe01d
FT
1856 scsi_for_each_sg(srb, sg, sg_count, i) {
1857 command_packet->sg_list[i].address = TW_CPU_TO_SGL(sg_dma_address(sg));
1858 command_packet->sg_list[i].length = cpu_to_le32(sg_dma_len(sg));
75913d9b 1859 if (command_packet->sg_list[i].address & TW_CPU_TO_SGL(TW_ALIGNMENT_9000_SGL)) {
1da177e4
LT
1860 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2e, "Found unaligned sgl address during execute scsi");
1861 goto out;
1862 }
1863 }
1864 }
0debe01d 1865 command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN((srb->device->lun >> 4), scsi_sg_count(tw_dev->srb[request_id])));
1da177e4
LT
1866 }
1867 } else {
1868 /* Internal cdb post */
1869 for (i = 0; i < use_sg; i++) {
75913d9b
AR
1870 command_packet->sg_list[i].address = TW_CPU_TO_SGL(sglistarg[i].address);
1871 command_packet->sg_list[i].length = cpu_to_le32(sglistarg[i].length);
1872 if (command_packet->sg_list[i].address & TW_CPU_TO_SGL(TW_ALIGNMENT_9000_SGL)) {
1da177e4
LT
1873 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2f, "Found unaligned sgl address during internal post");
1874 goto out;
1875 }
1876 }
75913d9b 1877 command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN(0, use_sg));
1da177e4
LT
1878 }
1879
1880 if (srb) {
1881 if (srb->cmnd[0] == READ_6 || srb->cmnd[0] == WRITE_6)
1882 num_sectors = (u32)srb->cmnd[4];
1883
1884 if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10)
1885 num_sectors = (u32)srb->cmnd[8] | ((u32)srb->cmnd[7] << 8);
1886 }
1887
1888 /* Update sector statistic */
1889 tw_dev->sector_count = num_sectors;
1890 if (tw_dev->sector_count > tw_dev->max_sector_count)
1891 tw_dev->max_sector_count = tw_dev->sector_count;
1892
1893 /* Update SG statistics */
1894 if (srb) {
0debe01d 1895 tw_dev->sgl_entries = scsi_sg_count(tw_dev->srb[request_id]);
1da177e4
LT
1896 if (tw_dev->sgl_entries > tw_dev->max_sgl_entries)
1897 tw_dev->max_sgl_entries = tw_dev->sgl_entries;
1898 }
1899
1900 /* Now post the command to the board */
1901 if (srb) {
1902 retval = twa_post_command_packet(tw_dev, request_id, 0);
1903 } else {
1904 twa_post_command_packet(tw_dev, request_id, 1);
1905 retval = 0;
1906 }
1907out:
1908 return retval;
1909} /* End twa_scsiop_execute_scsi() */
1910
1911/* This function completes an execute scsi operation */
1912static void twa_scsiop_execute_scsi_complete(TW_Device_Extension *tw_dev, int request_id)
1913{
0debe01d 1914 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
0debe01d 1915
15e3d5a2 1916 if (!twa_command_mapped(cmd) &&
0debe01d
FT
1917 (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1918 cmd->sc_data_direction == DMA_BIDIRECTIONAL)) {
b1192d5e 1919 if (scsi_sg_count(cmd) == 1) {
035f5e06
FT
1920 void *buf = tw_dev->generic_buffer_virt[request_id];
1921
035f5e06 1922 scsi_sg_copy_from_buffer(cmd, buf, TW_SECTOR_SIZE);
d327d082 1923 }
1da177e4
LT
1924 }
1925} /* End twa_scsiop_execute_scsi_complete() */
1926
1927/* This function tells the controller to shut down */
1928static void __twa_shutdown(TW_Device_Extension *tw_dev)
1929{
1930 /* Disable interrupts */
1931 TW_DISABLE_INTERRUPTS(tw_dev);
1932
4039c30e
AR
1933 /* Free up the IRQ */
1934 free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
1935
1da177e4
LT
1936 printk(KERN_WARNING "3w-9xxx: Shutting down host %d.\n", tw_dev->host->host_no);
1937
1938 /* Tell the card we are shutting down */
1939 if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
1940 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x31, "Connection shutdown failed");
1941 } else {
1942 printk(KERN_WARNING "3w-9xxx: Shutdown complete.\n");
1943 }
1944
1945 /* Clear all interrupts just before exit */
1946 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1947} /* End __twa_shutdown() */
1948
1949/* Wrapper for __twa_shutdown */
d18c3db5 1950static void twa_shutdown(struct pci_dev *pdev)
1da177e4 1951{
d18c3db5 1952 struct Scsi_Host *host = pci_get_drvdata(pdev);
1da177e4
LT
1953 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
1954
1955 __twa_shutdown(tw_dev);
1956} /* End twa_shutdown() */
1957
1958/* This function will look up a string */
1959static char *twa_string_lookup(twa_message_type *table, unsigned int code)
1960{
1961 int index;
1962
1963 for (index = 0; ((code != table[index].code) &&
1964 (table[index].text != (char *)0)); index++);
1965 return(table[index].text);
1966} /* End twa_string_lookup() */
1967
4deedd84
AR
1968/* This function gets called when a disk is coming on-line */
1969static int twa_slave_configure(struct scsi_device *sdev)
1970{
1971 /* Force 60 second timeout */
1972 blk_queue_rq_timeout(sdev->request_queue, 60 * HZ);
1973
1974 return 0;
1975} /* End twa_slave_configure() */
1976
1da177e4
LT
1977/* scsi_host_template initializer */
1978static struct scsi_host_template driver_template = {
1979 .module = THIS_MODULE,
1980 .name = "3ware 9000 Storage Controller",
1981 .queuecommand = twa_scsi_queue,
1982 .eh_host_reset_handler = twa_scsi_eh_reset,
1983 .bios_param = twa_scsi_biosparam,
db5ed4df 1984 .change_queue_depth = scsi_change_queue_depth,
1da177e4 1985 .can_queue = TW_Q_LENGTH-2,
4deedd84 1986 .slave_configure = twa_slave_configure,
1da177e4
LT
1987 .this_id = -1,
1988 .sg_tablesize = TW_APACHE_MAX_SGL_LENGTH,
1989 .max_sectors = TW_MAX_SECTORS,
1990 .cmd_per_lun = TW_MAX_CMDS_PER_LUN,
1991 .use_clustering = ENABLE_CLUSTERING,
1992 .shost_attrs = twa_host_attrs,
54b2b50c
MP
1993 .emulated = 1,
1994 .no_write_same = 1,
1da177e4
LT
1995};
1996
1997/* This function will probe and initialize a card */
6f039790 1998static int twa_probe(struct pci_dev *pdev, const struct pci_device_id *dev_id)
1da177e4
LT
1999{
2000 struct Scsi_Host *host = NULL;
2001 TW_Device_Extension *tw_dev;
3dabec71 2002 unsigned long mem_addr, mem_len;
1da177e4
LT
2003 int retval = -ENODEV;
2004
2005 retval = pci_enable_device(pdev);
2006 if (retval) {
2007 TW_PRINTK(host, TW_DRIVER, 0x34, "Failed to enable pci device");
2008 goto out_disable_device;
2009 }
2010
2011 pci_set_master(pdev);
1e6c38ce 2012 pci_try_set_mwi(pdev);
1da177e4 2013
6a35528a
YH
2014 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))
2015 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
284901a9
YH
2016 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
2017 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
0e78d158
AR
2018 TW_PRINTK(host, TW_DRIVER, 0x23, "Failed to set dma mask");
2019 retval = -ENODEV;
2020 goto out_disable_device;
2021 }
1da177e4
LT
2022
2023 host = scsi_host_alloc(&driver_template, sizeof(TW_Device_Extension));
2024 if (!host) {
2025 TW_PRINTK(host, TW_DRIVER, 0x24, "Failed to allocate memory for device extension");
2026 retval = -ENOMEM;
2027 goto out_disable_device;
2028 }
2029 tw_dev = (TW_Device_Extension *)host->hostdata;
2030
1da177e4
LT
2031 /* Save values to device extension */
2032 tw_dev->host = host;
2033 tw_dev->tw_pci_dev = pdev;
2034
2035 if (twa_initialize_device_extension(tw_dev)) {
2036 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x25, "Failed to initialize device extension");
2037 goto out_free_device_extension;
2038 }
2039
2040 /* Request IO regions */
2041 retval = pci_request_regions(pdev, "3w-9xxx");
2042 if (retval) {
2043 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x26, "Failed to get mem region");
2044 goto out_free_device_extension;
2045 }
2046
3dabec71 2047 if (pdev->device == PCI_DEVICE_ID_3WARE_9000) {
49bfd8db 2048 mem_addr = pci_resource_start(pdev, 1);
3dabec71
AR
2049 mem_len = pci_resource_len(pdev, 1);
2050 } else {
49bfd8db 2051 mem_addr = pci_resource_start(pdev, 2);
3dabec71
AR
2052 mem_len = pci_resource_len(pdev, 2);
2053 }
1da177e4
LT
2054
2055 /* Save base address */
3dabec71 2056 tw_dev->base_addr = ioremap(mem_addr, mem_len);
1da177e4
LT
2057 if (!tw_dev->base_addr) {
2058 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x35, "Failed to ioremap");
2059 goto out_release_mem_region;
2060 }
2061
2062 /* Disable interrupts on the card */
2063 TW_DISABLE_INTERRUPTS(tw_dev);
2064
2065 /* Initialize the card */
2066 if (twa_reset_sequence(tw_dev, 0))
4039c30e 2067 goto out_iounmap;
1da177e4
LT
2068
2069 /* Set host specific parameters */
0e78d158
AR
2070 if ((pdev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
2071 (pdev->device == PCI_DEVICE_ID_3WARE_9690SA))
4039c30e
AR
2072 host->max_id = TW_MAX_UNITS_9650SE;
2073 else
2074 host->max_id = TW_MAX_UNITS;
2075
1da177e4
LT
2076 host->max_cmd_len = TW_MAX_CDB_LEN;
2077
2078 /* Channels aren't supported by adapter */
4039c30e 2079 host->max_lun = TW_MAX_LUNS(tw_dev->tw_compat_info.working_srl);
1da177e4
LT
2080 host->max_channel = 0;
2081
2082 /* Register the card with the kernel SCSI layer */
2083 retval = scsi_add_host(host, &pdev->dev);
2084 if (retval) {
2085 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x27, "scsi add host failed");
4039c30e 2086 goto out_iounmap;
1da177e4
LT
2087 }
2088
2089 pci_set_drvdata(pdev, host);
2090
3dabec71 2091 printk(KERN_WARNING "3w-9xxx: scsi%d: Found a 3ware 9000 Storage Controller at 0x%lx, IRQ: %d.\n",
1da177e4
LT
2092 host->host_no, mem_addr, pdev->irq);
2093 printk(KERN_WARNING "3w-9xxx: scsi%d: Firmware %s, BIOS %s, Ports: %d.\n",
2094 host->host_no,
2095 (char *)twa_get_param(tw_dev, 0, TW_VERSION_TABLE,
2096 TW_PARAM_FWVER, TW_PARAM_FWVER_LENGTH),
2097 (char *)twa_get_param(tw_dev, 1, TW_VERSION_TABLE,
2098 TW_PARAM_BIOSVER, TW_PARAM_BIOSVER_LENGTH),
75913d9b
AR
2099 le32_to_cpu(*(int *)twa_get_param(tw_dev, 2, TW_INFORMATION_TABLE,
2100 TW_PARAM_PORTCOUNT, TW_PARAM_PORTCOUNT_LENGTH)));
1da177e4 2101
3dabec71
AR
2102 /* Try to enable MSI */
2103 if (use_msi && (pdev->device != PCI_DEVICE_ID_3WARE_9000) &&
2104 !pci_enable_msi(pdev))
2105 set_bit(TW_USING_MSI, &tw_dev->flags);
2106
1da177e4 2107 /* Now setup the interrupt handler */
1d6f359a 2108 retval = request_irq(pdev->irq, twa_interrupt, IRQF_SHARED, "3w-9xxx", tw_dev);
1da177e4
LT
2109 if (retval) {
2110 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x30, "Error requesting IRQ");
2111 goto out_remove_host;
2112 }
2113
2114 twa_device_extension_list[twa_device_extension_count] = tw_dev;
2115 twa_device_extension_count++;
2116
2117 /* Re-enable interrupts on the card */
2118 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
2119
2120 /* Finally, scan the host */
2121 scsi_scan_host(host);
2122
2123 if (twa_major == -1) {
2124 if ((twa_major = register_chrdev (0, "twa", &twa_fops)) < 0)
2125 TW_PRINTK(host, TW_DRIVER, 0x29, "Failed to register character device");
2126 }
2127 return 0;
2128
2129out_remove_host:
3dabec71
AR
2130 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2131 pci_disable_msi(pdev);
1da177e4 2132 scsi_remove_host(host);
4039c30e
AR
2133out_iounmap:
2134 iounmap(tw_dev->base_addr);
1da177e4
LT
2135out_release_mem_region:
2136 pci_release_regions(pdev);
2137out_free_device_extension:
2138 twa_free_device_extension(tw_dev);
2139 scsi_host_put(host);
2140out_disable_device:
2141 pci_disable_device(pdev);
2142
2143 return retval;
2144} /* End twa_probe() */
2145
2146/* This function is called to remove a device */
2147static void twa_remove(struct pci_dev *pdev)
2148{
2149 struct Scsi_Host *host = pci_get_drvdata(pdev);
2150 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2151
2152 scsi_remove_host(tw_dev->host);
2153
2154 /* Unregister character device */
2155 if (twa_major >= 0) {
2156 unregister_chrdev(twa_major, "twa");
2157 twa_major = -1;
2158 }
2159
1da177e4
LT
2160 /* Shutdown the card */
2161 __twa_shutdown(tw_dev);
2162
3dabec71
AR
2163 /* Disable MSI if enabled */
2164 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2165 pci_disable_msi(pdev);
2166
4039c30e
AR
2167 /* Free IO remapping */
2168 iounmap(tw_dev->base_addr);
2169
1da177e4
LT
2170 /* Free up the mem region */
2171 pci_release_regions(pdev);
2172
2173 /* Free up device extension resources */
2174 twa_free_device_extension(tw_dev);
2175
2176 scsi_host_put(tw_dev->host);
2177 pci_disable_device(pdev);
2178 twa_device_extension_count--;
2179} /* End twa_remove() */
2180
7a252fe7
AR
2181#ifdef CONFIG_PM
2182/* This function is called on PCI suspend */
2183static int twa_suspend(struct pci_dev *pdev, pm_message_t state)
2184{
2185 struct Scsi_Host *host = pci_get_drvdata(pdev);
2186 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2187
2188 printk(KERN_WARNING "3w-9xxx: Suspending host %d.\n", tw_dev->host->host_no);
2189
2190 TW_DISABLE_INTERRUPTS(tw_dev);
2191 free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
2192
2193 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2194 pci_disable_msi(pdev);
2195
2196 /* Tell the card we are shutting down */
2197 if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
2198 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x38, "Connection shutdown failed during suspend");
2199 } else {
2200 printk(KERN_WARNING "3w-9xxx: Suspend complete.\n");
2201 }
2202 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
2203
2204 pci_save_state(pdev);
2205 pci_disable_device(pdev);
2206 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2207
2208 return 0;
2209} /* End twa_suspend() */
2210
2211/* This function is called on PCI resume */
2212static int twa_resume(struct pci_dev *pdev)
2213{
2214 int retval = 0;
2215 struct Scsi_Host *host = pci_get_drvdata(pdev);
2216 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2217
2218 printk(KERN_WARNING "3w-9xxx: Resuming host %d.\n", tw_dev->host->host_no);
2219 pci_set_power_state(pdev, PCI_D0);
2220 pci_enable_wake(pdev, PCI_D0, 0);
2221 pci_restore_state(pdev);
2222
2223 retval = pci_enable_device(pdev);
2224 if (retval) {
2225 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x39, "Enable device failed during resume");
2226 return retval;
2227 }
2228
2229 pci_set_master(pdev);
2230 pci_try_set_mwi(pdev);
2231
e930438c
YH
2232 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))
2233 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
2234 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
2235 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
7a252fe7
AR
2236 TW_PRINTK(host, TW_DRIVER, 0x40, "Failed to set dma mask during resume");
2237 retval = -ENODEV;
2238 goto out_disable_device;
2239 }
2240
2241 /* Initialize the card */
2242 if (twa_reset_sequence(tw_dev, 0)) {
2243 retval = -ENODEV;
2244 goto out_disable_device;
2245 }
2246
2247 /* Now setup the interrupt handler */
2248 retval = request_irq(pdev->irq, twa_interrupt, IRQF_SHARED, "3w-9xxx", tw_dev);
2249 if (retval) {
2250 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x42, "Error requesting IRQ during resume");
2251 retval = -ENODEV;
2252 goto out_disable_device;
2253 }
2254
2255 /* Now enable MSI if enabled */
2256 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2257 pci_enable_msi(pdev);
2258
2259 /* Re-enable interrupts on the card */
2260 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
2261
2262 printk(KERN_WARNING "3w-9xxx: Resume complete.\n");
2263 return 0;
2264
2265out_disable_device:
2266 scsi_remove_host(host);
2267 pci_disable_device(pdev);
2268
2269 return retval;
2270} /* End twa_resume() */
2271#endif
2272
1da177e4 2273/* PCI Devices supported by this driver */
6f039790 2274static struct pci_device_id twa_pci_tbl[] = {
1da177e4
LT
2275 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9000,
2276 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
49bfd8db
AR
2277 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9550SX,
2278 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4039c30e
AR
2279 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9650SE,
2280 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
0e78d158
AR
2281 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9690SA,
2282 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1da177e4
LT
2283 { }
2284};
2285MODULE_DEVICE_TABLE(pci, twa_pci_tbl);
2286
2287/* pci_driver initializer */
2288static struct pci_driver twa_driver = {
2289 .name = "3w-9xxx",
2290 .id_table = twa_pci_tbl,
2291 .probe = twa_probe,
2292 .remove = twa_remove,
7a252fe7
AR
2293#ifdef CONFIG_PM
2294 .suspend = twa_suspend,
2295 .resume = twa_resume,
2296#endif
d18c3db5 2297 .shutdown = twa_shutdown
1da177e4
LT
2298};
2299
2300/* This function is called on driver initialization */
2301static int __init twa_init(void)
2302{
2303 printk(KERN_WARNING "3ware 9000 Storage Controller device driver for Linux v%s.\n", TW_DRIVER_VERSION);
2304
dcbccbde 2305 return pci_register_driver(&twa_driver);
1da177e4
LT
2306} /* End twa_init() */
2307
2308/* This function is called on driver exit */
2309static void __exit twa_exit(void)
2310{
2311 pci_unregister_driver(&twa_driver);
2312} /* End twa_exit() */
2313
2314module_init(twa_init);
2315module_exit(twa_exit);
2316