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