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