blob: 09a139f61ca4cf1aed924e08b84cf3ecb8f84c56 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 3w-9xxx.c -- 3ware 9000 Storage Controller device driver for Linux.
3
adam radford4deedd82010-03-08 12:37:46 -08004 Written By: Adam Radford <linuxraid@lsi.com>
5 Modifications By: Tom Couch <linuxraid@lsi.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006
adam radford7a252fe2009-03-09 12:15:01 -08007 Copyright (C) 2004-2009 Applied Micro Circuits Corporation.
adam radford4deedd82010-03-08 12:37:46 -08008 Copyright (C) 2010 LSI Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; version 2 of the License.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 NO WARRANTY
20 THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
21 CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
22 LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
23 MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
24 solely responsible for determining the appropriateness of using and
25 distributing the Program and assumes all risks associated with its
26 exercise of rights under this Agreement, including but not limited to
27 the risks and costs of program errors, damage to or loss of data,
28 programs or equipment, and unavailability or interruption of operations.
29
30 DISCLAIMER OF LIABILITY
31 NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
32 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
34 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
35 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
36 USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
37 HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
38
39 You should have received a copy of the GNU General Public License
40 along with this program; if not, write to the Free Software
41 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
42
43 Bugs/Comments/Suggestions should be mailed to:
adam radford4deedd82010-03-08 12:37:46 -080044 linuxraid@lsi.com
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46 For more information, goto:
adam radford4deedd82010-03-08 12:37:46 -080047 http://www.lsi.com
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49 Note: This version of the driver does not contain a bundled firmware
50 image.
51
52 History
53 -------
54 2.26.02.000 - Driver cleanup for kernel submission.
55 2.26.02.001 - Replace schedule_timeout() calls with msleep().
56 2.26.02.002 - Add support for PAE mode.
57 Add lun support.
58 Fix twa_remove() to free irq handler/unregister_chrdev()
59 before shutting down card.
60 Change to new 'change_queue_depth' api.
61 Fix 'handled=1' ISR usage, remove bogus IRQ check.
62 Remove un-needed eh_abort handler.
63 Add support for embedded firmware error strings.
adam radfordd327d082005-09-09 15:55:13 -070064 2.26.02.003 - Correctly handle single sgl's with use_sg=1.
adam radford49bfd8d2005-09-21 17:20:14 -070065 2.26.02.004 - Add support for 9550SX controllers.
adam radford62288f12006-02-05 14:51:43 -080066 2.26.02.005 - Fix use_sg == 0 mapping on systems with 4GB or higher.
adam radford75913d92006-03-15 12:43:19 -080067 2.26.02.006 - Fix 9550SX pchip reset timeout.
68 Add big endian support.
adam radford1e08dcb2006-04-11 11:25:09 -070069 2.26.02.007 - Disable local interrupts during kmap/unmap_atomic().
adam radford4039c302006-10-26 18:01:06 -070070 2.26.02.008 - Free irq handler in __twa_shutdown().
71 Serialize reset code.
72 Add support for 9650SE controllers.
adam radford0e78d152007-07-20 15:28:28 -070073 2.26.02.009 - Fix dma mask setting to fallback to 32-bit if 64-bit fails.
74 2.26.02.010 - Add support for 9690SA controllers.
adam radford3dabec72008-07-22 16:47:40 -070075 2.26.02.011 - Increase max AENs drained to 256.
76 Add MSI support and "use_msi" module parameter.
77 Fix bug in twa_get_param() on 4GB+.
78 Use pci_resource_len() for ioremap().
adam radford7a252fe2009-03-09 12:15:01 -080079 2.26.02.012 - Add power management support.
adam radford53ca3532009-12-10 11:53:31 -080080 2.26.02.013 - Fix bug in twa_load_sgl().
adam radford4deedd82010-03-08 12:37:46 -080081 2.26.02.014 - Force 60 second timeout default.
Linus Torvalds1da177e2005-04-16 15:20:36 -070082*/
83
84#include <linux/module.h>
85#include <linux/reboot.h>
86#include <linux/spinlock.h>
87#include <linux/interrupt.h>
88#include <linux/moduleparam.h>
89#include <linux/errno.h>
90#include <linux/types.h>
91#include <linux/delay.h>
92#include <linux/pci.h>
93#include <linux/time.h>
Jes Sorensena12e25bd2006-01-11 08:39:45 -050094#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090095#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#include <asm/io.h>
97#include <asm/irq.h>
98#include <asm/uaccess.h>
99#include <scsi/scsi.h>
100#include <scsi/scsi_host.h>
101#include <scsi/scsi_tcq.h>
102#include <scsi/scsi_cmnd.h>
103#include "3w-9xxx.h"
104
105/* Globals */
adam radford4deedd82010-03-08 12:37:46 -0800106#define TW_DRIVER_VERSION "2.26.02.014"
Arnd Bergmannc45d15d2010-06-02 14:28:52 +0200107static DEFINE_MUTEX(twa_chrdev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static TW_Device_Extension *twa_device_extension_list[TW_MAX_SLOT];
109static unsigned int twa_device_extension_count;
110static int twa_major = -1;
111extern struct timezone sys_tz;
112
113/* Module parameters */
adam radford4deedd82010-03-08 12:37:46 -0800114MODULE_AUTHOR ("LSI");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115MODULE_DESCRIPTION ("3ware 9000 Storage Controller Linux Driver");
116MODULE_LICENSE("GPL");
117MODULE_VERSION(TW_DRIVER_VERSION);
118
adam radford3dabec72008-07-22 16:47:40 -0700119static int use_msi = 0;
120module_param(use_msi, int, S_IRUGO);
121MODULE_PARM_DESC(use_msi, "Use Message Signaled Interrupts. Default: 0");
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/* Function prototypes */
124static void twa_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header);
125static int twa_aen_read_queue(TW_Device_Extension *tw_dev, int request_id);
126static char *twa_aen_severity_lookup(unsigned char severity_code);
127static void twa_aen_sync_time(TW_Device_Extension *tw_dev, int request_id);
Arnd Bergmannf4927c42010-04-27 00:24:01 +0200128static long twa_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static int twa_chrdev_open(struct inode *inode, struct file *file);
130static int twa_fill_sense(TW_Device_Extension *tw_dev, int request_id, int copy_sense, int print_host);
131static void twa_free_request_id(TW_Device_Extension *tw_dev,int request_id);
132static void twa_get_request_id(TW_Device_Extension *tw_dev, int *request_id);
133static int twa_initconnection(TW_Device_Extension *tw_dev, int message_credits,
134 u32 set_features, unsigned short current_fw_srl,
135 unsigned short current_fw_arch_id,
136 unsigned short current_fw_branch,
137 unsigned short current_fw_build,
138 unsigned short *fw_on_ctlr_srl,
139 unsigned short *fw_on_ctlr_arch_id,
140 unsigned short *fw_on_ctlr_branch,
141 unsigned short *fw_on_ctlr_build,
142 u32 *init_connect_result);
adam radford0e78d152007-07-20 15:28:28 -0700143static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static int twa_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds);
145static int twa_poll_status_gone(TW_Device_Extension *tw_dev, u32 flag, int seconds);
146static int twa_post_command_packet(TW_Device_Extension *tw_dev, int request_id, char internal);
adam radford0e78d152007-07-20 15:28:28 -0700147static int twa_reset_device_extension(TW_Device_Extension *tw_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148static int twa_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset);
149static int twa_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id, char *cdb, int use_sg, TW_SG_Entry *sglistarg);
150static void twa_scsiop_execute_scsi_complete(TW_Device_Extension *tw_dev, int request_id);
151static char *twa_string_lookup(twa_message_type *table, unsigned int aen_code);
152static void twa_unmap_scsi_data(TW_Device_Extension *tw_dev, int request_id);
153
154/* Functions */
155
156/* Show some statistics about the card */
Tony Jonesee959b02008-02-22 00:13:36 +0100157static ssize_t twa_show_stats(struct device *dev,
158 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Tony Jonesee959b02008-02-22 00:13:36 +0100160 struct Scsi_Host *host = class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
162 unsigned long flags = 0;
163 ssize_t len;
164
165 spin_lock_irqsave(tw_dev->host->host_lock, flags);
166 len = snprintf(buf, PAGE_SIZE, "3w-9xxx Driver version: %s\n"
167 "Current commands posted: %4d\n"
168 "Max commands posted: %4d\n"
169 "Current pending commands: %4d\n"
170 "Max pending commands: %4d\n"
171 "Last sgl length: %4d\n"
172 "Max sgl length: %4d\n"
173 "Last sector count: %4d\n"
174 "Max sector count: %4d\n"
175 "SCSI Host Resets: %4d\n"
176 "AEN's: %4d\n",
177 TW_DRIVER_VERSION,
178 tw_dev->posted_request_count,
179 tw_dev->max_posted_request_count,
180 tw_dev->pending_request_count,
181 tw_dev->max_pending_request_count,
182 tw_dev->sgl_entries,
183 tw_dev->max_sgl_entries,
184 tw_dev->sector_count,
185 tw_dev->max_sector_count,
186 tw_dev->num_resets,
187 tw_dev->aen_count);
188 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
189 return len;
190} /* End twa_show_stats() */
191
192/* This function will set a devices queue depth */
Mike Christiee881a172009-10-15 17:46:39 -0700193static int twa_change_queue_depth(struct scsi_device *sdev, int queue_depth,
194 int reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Mike Christiee881a172009-10-15 17:46:39 -0700196 if (reason != SCSI_QDEPTH_DEFAULT)
197 return -EOPNOTSUPP;
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (queue_depth > TW_Q_LENGTH-2)
200 queue_depth = TW_Q_LENGTH-2;
201 scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, queue_depth);
202 return queue_depth;
203} /* End twa_change_queue_depth() */
204
205/* Create sysfs 'stats' entry */
Tony Jonesee959b02008-02-22 00:13:36 +0100206static struct device_attribute twa_host_stats_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 .attr = {
208 .name = "stats",
209 .mode = S_IRUGO,
210 },
211 .show = twa_show_stats
212};
213
214/* Host attributes initializer */
Tony Jonesee959b02008-02-22 00:13:36 +0100215static struct device_attribute *twa_host_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 &twa_host_stats_attr,
217 NULL,
218};
219
220/* File operations struct for character device */
Arjan van de Ven00977a52007-02-12 00:55:34 -0800221static const struct file_operations twa_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 .owner = THIS_MODULE,
Arnd Bergmannf4927c42010-04-27 00:24:01 +0200223 .unlocked_ioctl = twa_chrdev_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 .open = twa_chrdev_open,
225 .release = NULL
226};
227
228/* This function will complete an aen request from the isr */
229static int twa_aen_complete(TW_Device_Extension *tw_dev, int request_id)
230{
231 TW_Command_Full *full_command_packet;
232 TW_Command *command_packet;
233 TW_Command_Apache_Header *header;
234 unsigned short aen;
235 int retval = 1;
236
237 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
238 tw_dev->posted_request_count--;
adam radford75913d92006-03-15 12:43:19 -0800239 aen = le16_to_cpu(header->status_block.error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 full_command_packet = tw_dev->command_packet_virt[request_id];
241 command_packet = &full_command_packet->command.oldcommand;
242
243 /* First check for internal completion of set param for time sync */
244 if (TW_OP_OUT(command_packet->opcode__sgloffset) == TW_OP_SET_PARAM) {
245 /* Keep reading the queue in case there are more aen's */
246 if (twa_aen_read_queue(tw_dev, request_id))
247 goto out2;
248 else {
249 retval = 0;
250 goto out;
251 }
252 }
253
254 switch (aen) {
255 case TW_AEN_QUEUE_EMPTY:
256 /* Quit reading the queue if this is the last one */
257 break;
258 case TW_AEN_SYNC_TIME_WITH_HOST:
259 twa_aen_sync_time(tw_dev, request_id);
260 retval = 0;
261 goto out;
262 default:
263 twa_aen_queue_event(tw_dev, header);
264
265 /* If there are more aen's, keep reading the queue */
266 if (twa_aen_read_queue(tw_dev, request_id))
267 goto out2;
268 else {
269 retval = 0;
270 goto out;
271 }
272 }
273 retval = 0;
274out2:
275 tw_dev->state[request_id] = TW_S_COMPLETED;
276 twa_free_request_id(tw_dev, request_id);
277 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
278out:
279 return retval;
280} /* End twa_aen_complete() */
281
282/* This function will drain aen queue */
283static int twa_aen_drain_queue(TW_Device_Extension *tw_dev, int no_check_reset)
284{
285 int request_id = 0;
286 char cdb[TW_MAX_CDB_LEN];
287 TW_SG_Entry sglist[1];
288 int finished = 0, count = 0;
289 TW_Command_Full *full_command_packet;
290 TW_Command_Apache_Header *header;
291 unsigned short aen;
292 int first_reset = 0, queue = 0, retval = 1;
293
294 if (no_check_reset)
295 first_reset = 0;
296 else
297 first_reset = 1;
298
299 full_command_packet = tw_dev->command_packet_virt[request_id];
300 memset(full_command_packet, 0, sizeof(TW_Command_Full));
301
302 /* Initialize cdb */
303 memset(&cdb, 0, TW_MAX_CDB_LEN);
304 cdb[0] = REQUEST_SENSE; /* opcode */
305 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
306
307 /* Initialize sglist */
308 memset(&sglist, 0, sizeof(TW_SG_Entry));
309 sglist[0].length = TW_SECTOR_SIZE;
310 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
311
312 if (sglist[0].address & TW_ALIGNMENT_9000_SGL) {
313 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1, "Found unaligned address during AEN drain");
314 goto out;
315 }
316
317 /* Mark internal command */
318 tw_dev->srb[request_id] = NULL;
319
320 do {
321 /* Send command to the board */
322 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
323 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2, "Error posting request sense");
324 goto out;
325 }
326
327 /* Now poll for completion */
328 if (twa_poll_response(tw_dev, request_id, 30)) {
329 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x3, "No valid response while draining AEN queue");
330 tw_dev->posted_request_count--;
331 goto out;
332 }
333
334 tw_dev->posted_request_count--;
335 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
adam radford75913d92006-03-15 12:43:19 -0800336 aen = le16_to_cpu(header->status_block.error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 queue = 0;
338 count++;
339
340 switch (aen) {
341 case TW_AEN_QUEUE_EMPTY:
342 if (first_reset != 1)
343 goto out;
344 else
345 finished = 1;
346 break;
347 case TW_AEN_SOFT_RESET:
348 if (first_reset == 0)
349 first_reset = 1;
350 else
351 queue = 1;
352 break;
353 case TW_AEN_SYNC_TIME_WITH_HOST:
354 break;
355 default:
356 queue = 1;
357 }
358
359 /* Now queue an event info */
360 if (queue)
361 twa_aen_queue_event(tw_dev, header);
362 } while ((finished == 0) && (count < TW_MAX_AEN_DRAIN));
363
364 if (count == TW_MAX_AEN_DRAIN)
365 goto out;
366
367 retval = 0;
368out:
369 tw_dev->state[request_id] = TW_S_INITIAL;
370 return retval;
371} /* End twa_aen_drain_queue() */
372
373/* This function will queue an event */
374static void twa_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header)
375{
376 u32 local_time;
377 struct timeval time;
378 TW_Event *event;
379 unsigned short aen;
380 char host[16];
381 char *error_str;
382
383 tw_dev->aen_count++;
384
385 /* Fill out event info */
386 event = tw_dev->event_queue[tw_dev->error_index];
387
388 /* Check for clobber */
389 host[0] = '\0';
390 if (tw_dev->host) {
391 sprintf(host, " scsi%d:", tw_dev->host->host_no);
392 if (event->retrieved == TW_AEN_NOT_RETRIEVED)
393 tw_dev->aen_clobber = 1;
394 }
395
adam radford75913d92006-03-15 12:43:19 -0800396 aen = le16_to_cpu(header->status_block.error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 memset(event, 0, sizeof(TW_Event));
398
399 event->severity = TW_SEV_OUT(header->status_block.severity__reserved);
400 do_gettimeofday(&time);
401 local_time = (u32)(time.tv_sec - (sys_tz.tz_minuteswest * 60));
402 event->time_stamp_sec = local_time;
403 event->aen_code = aen;
404 event->retrieved = TW_AEN_NOT_RETRIEVED;
405 event->sequence_id = tw_dev->error_sequence_id;
406 tw_dev->error_sequence_id++;
407
408 /* Check for embedded error string */
409 error_str = &(header->err_specific_desc[strlen(header->err_specific_desc)+1]);
410
411 header->err_specific_desc[sizeof(header->err_specific_desc) - 1] = '\0';
412 event->parameter_len = strlen(header->err_specific_desc);
adam radford75913d92006-03-15 12:43:19 -0800413 memcpy(event->parameter_data, header->err_specific_desc, event->parameter_len + (error_str[0] == '\0' ? 0 : (1 + strlen(error_str))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (event->severity != TW_AEN_SEVERITY_DEBUG)
415 printk(KERN_WARNING "3w-9xxx:%s AEN: %s (0x%02X:0x%04X): %s:%s.\n",
416 host,
417 twa_aen_severity_lookup(TW_SEV_OUT(header->status_block.severity__reserved)),
418 TW_MESSAGE_SOURCE_CONTROLLER_EVENT, aen,
419 error_str[0] == '\0' ? twa_string_lookup(twa_aen_table, aen) : error_str,
420 header->err_specific_desc);
421 else
422 tw_dev->aen_count--;
423
424 if ((tw_dev->error_index + 1) == TW_Q_LENGTH)
425 tw_dev->event_queue_wrapped = 1;
426 tw_dev->error_index = (tw_dev->error_index + 1 ) % TW_Q_LENGTH;
427} /* End twa_aen_queue_event() */
428
429/* This function will read the aen queue from the isr */
430static int twa_aen_read_queue(TW_Device_Extension *tw_dev, int request_id)
431{
432 char cdb[TW_MAX_CDB_LEN];
433 TW_SG_Entry sglist[1];
434 TW_Command_Full *full_command_packet;
435 int retval = 1;
436
437 full_command_packet = tw_dev->command_packet_virt[request_id];
438 memset(full_command_packet, 0, sizeof(TW_Command_Full));
439
440 /* Initialize cdb */
441 memset(&cdb, 0, TW_MAX_CDB_LEN);
442 cdb[0] = REQUEST_SENSE; /* opcode */
443 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
444
445 /* Initialize sglist */
446 memset(&sglist, 0, sizeof(TW_SG_Entry));
447 sglist[0].length = TW_SECTOR_SIZE;
448 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
449
450 /* Mark internal command */
451 tw_dev->srb[request_id] = NULL;
452
453 /* Now post the command packet */
454 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
455 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x4, "Post failed while reading AEN queue");
456 goto out;
457 }
458 retval = 0;
459out:
460 return retval;
461} /* End twa_aen_read_queue() */
462
463/* This function will look up an AEN severity string */
464static char *twa_aen_severity_lookup(unsigned char severity_code)
465{
466 char *retval = NULL;
467
468 if ((severity_code < (unsigned char) TW_AEN_SEVERITY_ERROR) ||
469 (severity_code > (unsigned char) TW_AEN_SEVERITY_DEBUG))
470 goto out;
471
472 retval = twa_aen_severity_table[severity_code];
473out:
474 return retval;
475} /* End twa_aen_severity_lookup() */
476
477/* This function will sync firmware time with the host time */
478static void twa_aen_sync_time(TW_Device_Extension *tw_dev, int request_id)
479{
480 u32 schedulertime;
481 struct timeval utc;
482 TW_Command_Full *full_command_packet;
483 TW_Command *command_packet;
484 TW_Param_Apache *param;
485 u32 local_time;
486
487 /* Fill out the command packet */
488 full_command_packet = tw_dev->command_packet_virt[request_id];
489 memset(full_command_packet, 0, sizeof(TW_Command_Full));
490 command_packet = &full_command_packet->command.oldcommand;
491 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_SET_PARAM);
492 command_packet->request_id = request_id;
adam radford75913d92006-03-15 12:43:19 -0800493 command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
494 command_packet->byte8_offset.param.sgl[0].length = cpu_to_le32(TW_SECTOR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 command_packet->size = TW_COMMAND_SIZE;
adam radford75913d92006-03-15 12:43:19 -0800496 command_packet->byte6_offset.parameter_count = cpu_to_le16(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 /* Setup the param */
499 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
500 memset(param, 0, TW_SECTOR_SIZE);
adam radford75913d92006-03-15 12:43:19 -0800501 param->table_id = cpu_to_le16(TW_TIMEKEEP_TABLE | 0x8000); /* Controller time keep table */
502 param->parameter_id = cpu_to_le16(0x3); /* SchedulerTime */
503 param->parameter_size_bytes = cpu_to_le16(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 /* Convert system time in UTC to local time seconds since last
506 Sunday 12:00AM */
507 do_gettimeofday(&utc);
508 local_time = (u32)(utc.tv_sec - (sys_tz.tz_minuteswest * 60));
509 schedulertime = local_time - (3 * 86400);
adam radford75913d92006-03-15 12:43:19 -0800510 schedulertime = cpu_to_le32(schedulertime % 604800);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 memcpy(param->data, &schedulertime, sizeof(u32));
513
514 /* Mark internal command */
515 tw_dev->srb[request_id] = NULL;
516
517 /* Now post the command */
518 twa_post_command_packet(tw_dev, request_id, 1);
519} /* End twa_aen_sync_time() */
520
521/* This function will allocate memory and check if it is correctly aligned */
522static int twa_allocate_memory(TW_Device_Extension *tw_dev, int size, int which)
523{
524 int i;
525 dma_addr_t dma_handle;
526 unsigned long *cpu_addr;
527 int retval = 1;
528
529 cpu_addr = pci_alloc_consistent(tw_dev->tw_pci_dev, size*TW_Q_LENGTH, &dma_handle);
530 if (!cpu_addr) {
531 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x5, "Memory allocation failed");
532 goto out;
533 }
534
535 if ((unsigned long)cpu_addr % (TW_ALIGNMENT_9000)) {
536 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x6, "Failed to allocate correctly aligned memory");
537 pci_free_consistent(tw_dev->tw_pci_dev, size*TW_Q_LENGTH, cpu_addr, dma_handle);
538 goto out;
539 }
540
541 memset(cpu_addr, 0, size*TW_Q_LENGTH);
542
543 for (i = 0; i < TW_Q_LENGTH; i++) {
544 switch(which) {
545 case 0:
546 tw_dev->command_packet_phys[i] = dma_handle+(i*size);
547 tw_dev->command_packet_virt[i] = (TW_Command_Full *)((unsigned char *)cpu_addr + (i*size));
548 break;
549 case 1:
550 tw_dev->generic_buffer_phys[i] = dma_handle+(i*size);
551 tw_dev->generic_buffer_virt[i] = (unsigned long *)((unsigned char *)cpu_addr + (i*size));
552 break;
553 }
554 }
555 retval = 0;
556out:
557 return retval;
558} /* End twa_allocate_memory() */
559
560/* This function will check the status register for unexpected bits */
561static int twa_check_bits(u32 status_reg_value)
562{
563 int retval = 1;
564
565 if ((status_reg_value & TW_STATUS_EXPECTED_BITS) != TW_STATUS_EXPECTED_BITS)
566 goto out;
567 if ((status_reg_value & TW_STATUS_UNEXPECTED_BITS) != 0)
568 goto out;
569
570 retval = 0;
571out:
572 return retval;
573} /* End twa_check_bits() */
574
575/* This function will check the srl and decide if we are compatible */
576static int twa_check_srl(TW_Device_Extension *tw_dev, int *flashed)
577{
578 int retval = 1;
579 unsigned short fw_on_ctlr_srl = 0, fw_on_ctlr_arch_id = 0;
580 unsigned short fw_on_ctlr_branch = 0, fw_on_ctlr_build = 0;
581 u32 init_connect_result = 0;
582
583 if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
584 TW_EXTENDED_INIT_CONNECT, TW_CURRENT_DRIVER_SRL,
585 TW_9000_ARCH_ID, TW_CURRENT_DRIVER_BRANCH,
586 TW_CURRENT_DRIVER_BUILD, &fw_on_ctlr_srl,
587 &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
588 &fw_on_ctlr_build, &init_connect_result)) {
589 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x7, "Initconnection failed while checking SRL");
590 goto out;
591 }
592
adam radford4039c302006-10-26 18:01:06 -0700593 tw_dev->tw_compat_info.working_srl = fw_on_ctlr_srl;
594 tw_dev->tw_compat_info.working_branch = fw_on_ctlr_branch;
595 tw_dev->tw_compat_info.working_build = fw_on_ctlr_build;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 /* Try base mode compatibility */
598 if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
599 if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
600 TW_EXTENDED_INIT_CONNECT,
601 TW_BASE_FW_SRL, TW_9000_ARCH_ID,
602 TW_BASE_FW_BRANCH, TW_BASE_FW_BUILD,
603 &fw_on_ctlr_srl, &fw_on_ctlr_arch_id,
604 &fw_on_ctlr_branch, &fw_on_ctlr_build,
605 &init_connect_result)) {
606 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xa, "Initconnection (base mode) failed while checking SRL");
607 goto out;
608 }
609 if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
610 if (TW_CURRENT_DRIVER_SRL > fw_on_ctlr_srl) {
611 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x32, "Firmware and driver incompatibility: please upgrade firmware");
612 } else {
613 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x33, "Firmware and driver incompatibility: please upgrade driver");
614 }
615 goto out;
616 }
adam radford4039c302006-10-26 18:01:06 -0700617 tw_dev->tw_compat_info.working_srl = TW_BASE_FW_SRL;
618 tw_dev->tw_compat_info.working_branch = TW_BASE_FW_BRANCH;
619 tw_dev->tw_compat_info.working_build = TW_BASE_FW_BUILD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
adam radford4039c302006-10-26 18:01:06 -0700621
622 /* Load rest of compatibility struct */
623 strncpy(tw_dev->tw_compat_info.driver_version, TW_DRIVER_VERSION, strlen(TW_DRIVER_VERSION));
624 tw_dev->tw_compat_info.driver_srl_high = TW_CURRENT_DRIVER_SRL;
625 tw_dev->tw_compat_info.driver_branch_high = TW_CURRENT_DRIVER_BRANCH;
626 tw_dev->tw_compat_info.driver_build_high = TW_CURRENT_DRIVER_BUILD;
627 tw_dev->tw_compat_info.driver_srl_low = TW_BASE_FW_SRL;
628 tw_dev->tw_compat_info.driver_branch_low = TW_BASE_FW_BRANCH;
629 tw_dev->tw_compat_info.driver_build_low = TW_BASE_FW_BUILD;
630 tw_dev->tw_compat_info.fw_on_ctlr_srl = fw_on_ctlr_srl;
631 tw_dev->tw_compat_info.fw_on_ctlr_branch = fw_on_ctlr_branch;
632 tw_dev->tw_compat_info.fw_on_ctlr_build = fw_on_ctlr_build;
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 retval = 0;
635out:
636 return retval;
637} /* End twa_check_srl() */
638
639/* This function handles ioctl for the character device */
Arnd Bergmannf4927c42010-04-27 00:24:01 +0200640static long twa_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Arnd Bergmannf4927c42010-04-27 00:24:01 +0200642 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 long timeout;
644 unsigned long *cpu_addr, data_buffer_length_adjusted = 0, flags = 0;
645 dma_addr_t dma_handle;
646 int request_id = 0;
647 unsigned int sequence_id = 0;
648 unsigned char event_index, start_index;
649 TW_Ioctl_Driver_Command driver_command;
650 TW_Ioctl_Buf_Apache *tw_ioctl;
651 TW_Lock *tw_lock;
652 TW_Command_Full *full_command_packet;
653 TW_Compatibility_Info *tw_compat_info;
654 TW_Event *event;
655 struct timeval current_time;
656 u32 current_time_ms;
657 TW_Device_Extension *tw_dev = twa_device_extension_list[iminor(inode)];
658 int retval = TW_IOCTL_ERROR_OS_EFAULT;
659 void __user *argp = (void __user *)arg;
660
Arnd Bergmannc45d15d2010-06-02 14:28:52 +0200661 mutex_lock(&twa_chrdev_mutex);
Arnd Bergmannf4927c42010-04-27 00:24:01 +0200662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /* Only let one of these through at a time */
Jes Sorensena12e25bd2006-01-11 08:39:45 -0500664 if (mutex_lock_interruptible(&tw_dev->ioctl_lock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 retval = TW_IOCTL_ERROR_OS_EINTR;
666 goto out;
667 }
668
669 /* First copy down the driver command */
670 if (copy_from_user(&driver_command, argp, sizeof(TW_Ioctl_Driver_Command)))
671 goto out2;
672
673 /* Check data buffer size */
adam radford4039c302006-10-26 18:01:06 -0700674 if (driver_command.buffer_length > TW_MAX_SECTORS * 2048) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 retval = TW_IOCTL_ERROR_OS_EINVAL;
676 goto out2;
677 }
678
679 /* Hardware can only do multiple of 512 byte transfers */
680 data_buffer_length_adjusted = (driver_command.buffer_length + 511) & ~511;
681
682 /* Now allocate ioctl buf memory */
683 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);
684 if (!cpu_addr) {
685 retval = TW_IOCTL_ERROR_OS_ENOMEM;
686 goto out2;
687 }
688
689 tw_ioctl = (TW_Ioctl_Buf_Apache *)cpu_addr;
690
691 /* Now copy down the entire ioctl */
692 if (copy_from_user(tw_ioctl, argp, driver_command.buffer_length + sizeof(TW_Ioctl_Buf_Apache) - 1))
693 goto out3;
694
695 /* See which ioctl we are doing */
696 switch (cmd) {
697 case TW_IOCTL_FIRMWARE_PASS_THROUGH:
698 spin_lock_irqsave(tw_dev->host->host_lock, flags);
699 twa_get_request_id(tw_dev, &request_id);
700
701 /* Flag internal command */
702 tw_dev->srb[request_id] = NULL;
703
704 /* Flag chrdev ioctl */
705 tw_dev->chrdev_request_id = request_id;
706
707 full_command_packet = &tw_ioctl->firmware_command;
708
709 /* Load request id and sglist for both command types */
adam radford0e78d152007-07-20 15:28:28 -0700710 twa_load_sgl(tw_dev, full_command_packet, request_id, dma_handle, data_buffer_length_adjusted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 memcpy(tw_dev->command_packet_virt[request_id], &(tw_ioctl->firmware_command), sizeof(TW_Command_Full));
713
714 /* Now post the command packet to the controller */
715 twa_post_command_packet(tw_dev, request_id, 1);
716 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
717
718 timeout = TW_IOCTL_CHRDEV_TIMEOUT*HZ;
719
720 /* Now wait for command to complete */
721 timeout = wait_event_timeout(tw_dev->ioctl_wqueue, tw_dev->chrdev_request_id == TW_IOCTL_CHRDEV_FREE, timeout);
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 /* We timed out, and didn't get an interrupt */
724 if (tw_dev->chrdev_request_id != TW_IOCTL_CHRDEV_FREE) {
725 /* Now we need to reset the board */
726 printk(KERN_WARNING "3w-9xxx: scsi%d: WARNING: (0x%02X:0x%04X): Character ioctl (0x%x) timed out, resetting card.\n",
adam radford0e78d152007-07-20 15:28:28 -0700727 tw_dev->host->host_no, TW_DRIVER, 0x37,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 cmd);
729 retval = TW_IOCTL_ERROR_OS_EIO;
adam radford0e78d152007-07-20 15:28:28 -0700730 twa_reset_device_extension(tw_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 goto out3;
732 }
733
734 /* Now copy in the command packet response */
735 memcpy(&(tw_ioctl->firmware_command), tw_dev->command_packet_virt[request_id], sizeof(TW_Command_Full));
736
737 /* Now complete the io */
738 spin_lock_irqsave(tw_dev->host->host_lock, flags);
739 tw_dev->posted_request_count--;
740 tw_dev->state[request_id] = TW_S_COMPLETED;
741 twa_free_request_id(tw_dev, request_id);
742 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
743 break;
744 case TW_IOCTL_GET_COMPATIBILITY_INFO:
745 tw_ioctl->driver_command.status = 0;
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400746 /* Copy compatibility struct into ioctl data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 tw_compat_info = (TW_Compatibility_Info *)tw_ioctl->data_buffer;
adam radford4039c302006-10-26 18:01:06 -0700748 memcpy(tw_compat_info, &tw_dev->tw_compat_info, sizeof(TW_Compatibility_Info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 break;
750 case TW_IOCTL_GET_LAST_EVENT:
751 if (tw_dev->event_queue_wrapped) {
752 if (tw_dev->aen_clobber) {
753 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
754 tw_dev->aen_clobber = 0;
755 } else
756 tw_ioctl->driver_command.status = 0;
757 } else {
758 if (!tw_dev->error_index) {
759 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
760 break;
761 }
762 tw_ioctl->driver_command.status = 0;
763 }
764 event_index = (tw_dev->error_index - 1 + TW_Q_LENGTH) % TW_Q_LENGTH;
765 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
766 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
767 break;
768 case TW_IOCTL_GET_FIRST_EVENT:
769 if (tw_dev->event_queue_wrapped) {
770 if (tw_dev->aen_clobber) {
771 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
772 tw_dev->aen_clobber = 0;
773 } else
774 tw_ioctl->driver_command.status = 0;
775 event_index = tw_dev->error_index;
776 } else {
777 if (!tw_dev->error_index) {
778 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
779 break;
780 }
781 tw_ioctl->driver_command.status = 0;
782 event_index = 0;
783 }
784 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
785 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
786 break;
787 case TW_IOCTL_GET_NEXT_EVENT:
788 event = (TW_Event *)tw_ioctl->data_buffer;
789 sequence_id = event->sequence_id;
790 tw_ioctl->driver_command.status = 0;
791
792 if (tw_dev->event_queue_wrapped) {
793 if (tw_dev->aen_clobber) {
794 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
795 tw_dev->aen_clobber = 0;
796 }
797 start_index = tw_dev->error_index;
798 } else {
799 if (!tw_dev->error_index) {
800 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
801 break;
802 }
803 start_index = 0;
804 }
805 event_index = (start_index + sequence_id - tw_dev->event_queue[start_index]->sequence_id + 1) % TW_Q_LENGTH;
806
807 if (!(tw_dev->event_queue[event_index]->sequence_id > sequence_id)) {
808 if (tw_ioctl->driver_command.status == TW_IOCTL_ERROR_STATUS_AEN_CLOBBER)
809 tw_dev->aen_clobber = 1;
810 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
811 break;
812 }
813 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
814 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
815 break;
816 case TW_IOCTL_GET_PREVIOUS_EVENT:
817 event = (TW_Event *)tw_ioctl->data_buffer;
818 sequence_id = event->sequence_id;
819 tw_ioctl->driver_command.status = 0;
820
821 if (tw_dev->event_queue_wrapped) {
822 if (tw_dev->aen_clobber) {
823 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
824 tw_dev->aen_clobber = 0;
825 }
826 start_index = tw_dev->error_index;
827 } else {
828 if (!tw_dev->error_index) {
829 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
830 break;
831 }
832 start_index = 0;
833 }
834 event_index = (start_index + sequence_id - tw_dev->event_queue[start_index]->sequence_id - 1) % TW_Q_LENGTH;
835
836 if (!(tw_dev->event_queue[event_index]->sequence_id < sequence_id)) {
837 if (tw_ioctl->driver_command.status == TW_IOCTL_ERROR_STATUS_AEN_CLOBBER)
838 tw_dev->aen_clobber = 1;
839 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
840 break;
841 }
842 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
843 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
844 break;
845 case TW_IOCTL_GET_LOCK:
846 tw_lock = (TW_Lock *)tw_ioctl->data_buffer;
847 do_gettimeofday(&current_time);
848 current_time_ms = (current_time.tv_sec * 1000) + (current_time.tv_usec / 1000);
849
850 if ((tw_lock->force_flag == 1) || (tw_dev->ioctl_sem_lock == 0) || (current_time_ms >= tw_dev->ioctl_msec)) {
851 tw_dev->ioctl_sem_lock = 1;
852 tw_dev->ioctl_msec = current_time_ms + tw_lock->timeout_msec;
853 tw_ioctl->driver_command.status = 0;
854 tw_lock->time_remaining_msec = tw_lock->timeout_msec;
855 } else {
856 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_LOCKED;
857 tw_lock->time_remaining_msec = tw_dev->ioctl_msec - current_time_ms;
858 }
859 break;
860 case TW_IOCTL_RELEASE_LOCK:
861 if (tw_dev->ioctl_sem_lock == 1) {
862 tw_dev->ioctl_sem_lock = 0;
863 tw_ioctl->driver_command.status = 0;
864 } else {
865 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NOT_LOCKED;
866 }
867 break;
868 default:
869 retval = TW_IOCTL_ERROR_OS_ENOTTY;
870 goto out3;
871 }
872
873 /* Now copy the entire response to userspace */
874 if (copy_to_user(argp, tw_ioctl, sizeof(TW_Ioctl_Buf_Apache) + driver_command.buffer_length - 1) == 0)
875 retval = 0;
876out3:
877 /* Now free ioctl buf memory */
878 dma_free_coherent(&tw_dev->tw_pci_dev->dev, data_buffer_length_adjusted+sizeof(TW_Ioctl_Buf_Apache) - 1, cpu_addr, dma_handle);
879out2:
Jes Sorensena12e25bd2006-01-11 08:39:45 -0500880 mutex_unlock(&tw_dev->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881out:
Arnd Bergmannc45d15d2010-06-02 14:28:52 +0200882 mutex_unlock(&twa_chrdev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return retval;
884} /* End twa_chrdev_ioctl() */
885
886/* This function handles open for the character device */
Jonathan Corbetf2b98572008-05-18 15:32:43 -0600887/* NOTE that this function will race with remove. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888static int twa_chrdev_open(struct inode *inode, struct file *file)
889{
890 unsigned int minor_number;
891 int retval = TW_IOCTL_ERROR_OS_ENODEV;
892
893 minor_number = iminor(inode);
894 if (minor_number >= twa_device_extension_count)
895 goto out;
896 retval = 0;
897out:
898 return retval;
899} /* End twa_chrdev_open() */
900
901/* This function will print readable messages from status register errors */
902static int twa_decode_bits(TW_Device_Extension *tw_dev, u32 status_reg_value)
903{
904 int retval = 1;
905
906 /* Check for various error conditions and handle them appropriately */
907 if (status_reg_value & TW_STATUS_PCI_PARITY_ERROR) {
908 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xc, "PCI Parity Error: clearing");
909 writel(TW_CONTROL_CLEAR_PARITY_ERROR, TW_CONTROL_REG_ADDR(tw_dev));
910 }
911
912 if (status_reg_value & TW_STATUS_PCI_ABORT) {
913 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xd, "PCI Abort: clearing");
914 writel(TW_CONTROL_CLEAR_PCI_ABORT, TW_CONTROL_REG_ADDR(tw_dev));
915 pci_write_config_word(tw_dev->tw_pci_dev, PCI_STATUS, TW_PCI_CLEAR_PCI_ABORT);
916 }
917
918 if (status_reg_value & TW_STATUS_QUEUE_ERROR) {
adam radford0e78d152007-07-20 15:28:28 -0700919 if (((tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9650SE) &&
920 (tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9690SA)) ||
921 (!test_bit(TW_IN_RESET, &tw_dev->flags)))
adam radford4039c302006-10-26 18:01:06 -0700922 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xe, "Controller Queue Error: clearing");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 writel(TW_CONTROL_CLEAR_QUEUE_ERROR, TW_CONTROL_REG_ADDR(tw_dev));
924 }
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (status_reg_value & TW_STATUS_MICROCONTROLLER_ERROR) {
927 if (tw_dev->reset_print == 0) {
928 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x10, "Microcontroller Error: clearing");
929 tw_dev->reset_print = 1;
930 }
931 goto out;
932 }
933 retval = 0;
934out:
935 return retval;
936} /* End twa_decode_bits() */
937
938/* This function will empty the response queue */
939static int twa_empty_response_queue(TW_Device_Extension *tw_dev)
940{
941 u32 status_reg_value, response_que_value;
942 int count = 0, retval = 1;
943
944 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
945
946 while (((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) && (count < TW_MAX_RESPONSE_DRAIN)) {
947 response_que_value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
948 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
949 count++;
950 }
951 if (count == TW_MAX_RESPONSE_DRAIN)
952 goto out;
953
954 retval = 0;
955out:
956 return retval;
957} /* End twa_empty_response_queue() */
958
adam radford49bfd8d2005-09-21 17:20:14 -0700959/* This function will clear the pchip/response queue on 9550SX */
960static int twa_empty_response_queue_large(TW_Device_Extension *tw_dev)
961{
adam radford75913d92006-03-15 12:43:19 -0800962 u32 response_que_value = 0;
963 unsigned long before;
964 int retval = 1;
adam radford49bfd8d2005-09-21 17:20:14 -0700965
adam radford0e78d152007-07-20 15:28:28 -0700966 if (tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9000) {
adam radford75913d92006-03-15 12:43:19 -0800967 before = jiffies;
968 while ((response_que_value & TW_9550SX_DRAIN_COMPLETED) != TW_9550SX_DRAIN_COMPLETED) {
adam radford49bfd8d2005-09-21 17:20:14 -0700969 response_que_value = readl(TW_RESPONSE_QUEUE_REG_ADDR_LARGE(tw_dev));
adam radford4039c302006-10-26 18:01:06 -0700970 msleep(1);
adam radford75913d92006-03-15 12:43:19 -0800971 if (time_after(jiffies, before + HZ * 30))
adam radford49bfd8d2005-09-21 17:20:14 -0700972 goto out;
adam radford49bfd8d2005-09-21 17:20:14 -0700973 }
adam radford75913d92006-03-15 12:43:19 -0800974 /* P-chip settle time */
975 msleep(500);
adam radford49bfd8d2005-09-21 17:20:14 -0700976 retval = 0;
977 } else
978 retval = 0;
979out:
980 return retval;
981} /* End twa_empty_response_queue_large() */
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983/* This function passes sense keys from firmware to scsi layer */
984static int twa_fill_sense(TW_Device_Extension *tw_dev, int request_id, int copy_sense, int print_host)
985{
986 TW_Command_Full *full_command_packet;
987 unsigned short error;
988 int retval = 1;
989 char *error_str;
990
991 full_command_packet = tw_dev->command_packet_virt[request_id];
992
993 /* Check for embedded error string */
994 error_str = &(full_command_packet->header.err_specific_desc[strlen(full_command_packet->header.err_specific_desc) + 1]);
995
996 /* Don't print error for Logical unit not supported during rollcall */
adam radford75913d92006-03-15 12:43:19 -0800997 error = le16_to_cpu(full_command_packet->header.status_block.error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if ((error != TW_ERROR_LOGICAL_UNIT_NOT_SUPPORTED) && (error != TW_ERROR_UNIT_OFFLINE)) {
999 if (print_host)
1000 printk(KERN_WARNING "3w-9xxx: scsi%d: ERROR: (0x%02X:0x%04X): %s:%s.\n",
1001 tw_dev->host->host_no,
1002 TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
1003 full_command_packet->header.status_block.error,
1004 error_str[0] == '\0' ?
1005 twa_string_lookup(twa_error_table,
1006 full_command_packet->header.status_block.error) : error_str,
1007 full_command_packet->header.err_specific_desc);
1008 else
1009 printk(KERN_WARNING "3w-9xxx: ERROR: (0x%02X:0x%04X): %s:%s.\n",
1010 TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
1011 full_command_packet->header.status_block.error,
1012 error_str[0] == '\0' ?
1013 twa_string_lookup(twa_error_table,
1014 full_command_packet->header.status_block.error) : error_str,
1015 full_command_packet->header.err_specific_desc);
1016 }
1017
1018 if (copy_sense) {
1019 memcpy(tw_dev->srb[request_id]->sense_buffer, full_command_packet->header.sense_data, TW_SENSE_DATA_LENGTH);
1020 tw_dev->srb[request_id]->result = (full_command_packet->command.newcommand.status << 1);
1021 retval = TW_ISR_DONT_RESULT;
1022 goto out;
1023 }
1024 retval = 0;
1025out:
1026 return retval;
1027} /* End twa_fill_sense() */
1028
1029/* This function will free up device extension resources */
1030static void twa_free_device_extension(TW_Device_Extension *tw_dev)
1031{
1032 if (tw_dev->command_packet_virt[0])
1033 pci_free_consistent(tw_dev->tw_pci_dev,
1034 sizeof(TW_Command_Full)*TW_Q_LENGTH,
1035 tw_dev->command_packet_virt[0],
1036 tw_dev->command_packet_phys[0]);
1037
1038 if (tw_dev->generic_buffer_virt[0])
1039 pci_free_consistent(tw_dev->tw_pci_dev,
1040 TW_SECTOR_SIZE*TW_Q_LENGTH,
1041 tw_dev->generic_buffer_virt[0],
1042 tw_dev->generic_buffer_phys[0]);
1043
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001044 kfree(tw_dev->event_queue[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045} /* End twa_free_device_extension() */
1046
1047/* This function will free a request id */
1048static void twa_free_request_id(TW_Device_Extension *tw_dev, int request_id)
1049{
1050 tw_dev->free_queue[tw_dev->free_tail] = request_id;
1051 tw_dev->state[request_id] = TW_S_FINISHED;
1052 tw_dev->free_tail = (tw_dev->free_tail + 1) % TW_Q_LENGTH;
1053} /* End twa_free_request_id() */
1054
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -08001055/* This function will get parameter table entries from the firmware */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056static void *twa_get_param(TW_Device_Extension *tw_dev, int request_id, int table_id, int parameter_id, int parameter_size_bytes)
1057{
1058 TW_Command_Full *full_command_packet;
1059 TW_Command *command_packet;
1060 TW_Param_Apache *param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 void *retval = NULL;
1062
1063 /* Setup the command packet */
1064 full_command_packet = tw_dev->command_packet_virt[request_id];
1065 memset(full_command_packet, 0, sizeof(TW_Command_Full));
1066 command_packet = &full_command_packet->command.oldcommand;
1067
1068 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_GET_PARAM);
1069 command_packet->size = TW_COMMAND_SIZE;
1070 command_packet->request_id = request_id;
adam radford75913d92006-03-15 12:43:19 -08001071 command_packet->byte6_offset.block_count = cpu_to_le16(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 /* Now setup the param */
1074 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
1075 memset(param, 0, TW_SECTOR_SIZE);
adam radford75913d92006-03-15 12:43:19 -08001076 param->table_id = cpu_to_le16(table_id | 0x8000);
1077 param->parameter_id = cpu_to_le16(parameter_id);
1078 param->parameter_size_bytes = cpu_to_le16(parameter_size_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
adam radford3dabec72008-07-22 16:47:40 -07001080 command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
adam radford75913d92006-03-15 12:43:19 -08001081 command_packet->byte8_offset.param.sgl[0].length = cpu_to_le32(TW_SECTOR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 /* Post the command packet to the board */
1084 twa_post_command_packet(tw_dev, request_id, 1);
1085
1086 /* Poll for completion */
1087 if (twa_poll_response(tw_dev, request_id, 30))
1088 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x13, "No valid response during get param")
1089 else
1090 retval = (void *)&(param->data[0]);
1091
1092 tw_dev->posted_request_count--;
1093 tw_dev->state[request_id] = TW_S_INITIAL;
1094
1095 return retval;
1096} /* End twa_get_param() */
1097
1098/* This function will assign an available request id */
1099static void twa_get_request_id(TW_Device_Extension *tw_dev, int *request_id)
1100{
1101 *request_id = tw_dev->free_queue[tw_dev->free_head];
1102 tw_dev->free_head = (tw_dev->free_head + 1) % TW_Q_LENGTH;
1103 tw_dev->state[*request_id] = TW_S_STARTED;
1104} /* End twa_get_request_id() */
1105
1106/* This function will send an initconnection command to controller */
1107static int twa_initconnection(TW_Device_Extension *tw_dev, int message_credits,
1108 u32 set_features, unsigned short current_fw_srl,
1109 unsigned short current_fw_arch_id,
1110 unsigned short current_fw_branch,
1111 unsigned short current_fw_build,
1112 unsigned short *fw_on_ctlr_srl,
1113 unsigned short *fw_on_ctlr_arch_id,
1114 unsigned short *fw_on_ctlr_branch,
1115 unsigned short *fw_on_ctlr_build,
1116 u32 *init_connect_result)
1117{
1118 TW_Command_Full *full_command_packet;
1119 TW_Initconnect *tw_initconnect;
1120 int request_id = 0, retval = 1;
1121
1122 /* Initialize InitConnection command packet */
1123 full_command_packet = tw_dev->command_packet_virt[request_id];
1124 memset(full_command_packet, 0, sizeof(TW_Command_Full));
1125 full_command_packet->header.header_desc.size_header = 128;
1126
1127 tw_initconnect = (TW_Initconnect *)&full_command_packet->command.oldcommand;
1128 tw_initconnect->opcode__reserved = TW_OPRES_IN(0, TW_OP_INIT_CONNECTION);
1129 tw_initconnect->request_id = request_id;
adam radford75913d92006-03-15 12:43:19 -08001130 tw_initconnect->message_credits = cpu_to_le16(message_credits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 tw_initconnect->features = set_features;
1132
1133 /* Turn on 64-bit sgl support if we need to */
1134 tw_initconnect->features |= sizeof(dma_addr_t) > 4 ? 1 : 0;
1135
adam radford75913d92006-03-15 12:43:19 -08001136 tw_initconnect->features = cpu_to_le32(tw_initconnect->features);
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (set_features & TW_EXTENDED_INIT_CONNECT) {
1139 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE_EXTENDED;
adam radford75913d92006-03-15 12:43:19 -08001140 tw_initconnect->fw_srl = cpu_to_le16(current_fw_srl);
1141 tw_initconnect->fw_arch_id = cpu_to_le16(current_fw_arch_id);
1142 tw_initconnect->fw_branch = cpu_to_le16(current_fw_branch);
1143 tw_initconnect->fw_build = cpu_to_le16(current_fw_build);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 } else
1145 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE;
1146
1147 /* Send command packet to the board */
1148 twa_post_command_packet(tw_dev, request_id, 1);
1149
1150 /* Poll for completion */
1151 if (twa_poll_response(tw_dev, request_id, 30)) {
1152 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x15, "No valid response during init connection");
1153 } else {
1154 if (set_features & TW_EXTENDED_INIT_CONNECT) {
adam radford75913d92006-03-15 12:43:19 -08001155 *fw_on_ctlr_srl = le16_to_cpu(tw_initconnect->fw_srl);
1156 *fw_on_ctlr_arch_id = le16_to_cpu(tw_initconnect->fw_arch_id);
1157 *fw_on_ctlr_branch = le16_to_cpu(tw_initconnect->fw_branch);
1158 *fw_on_ctlr_build = le16_to_cpu(tw_initconnect->fw_build);
1159 *init_connect_result = le32_to_cpu(tw_initconnect->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
1161 retval = 0;
1162 }
1163
1164 tw_dev->posted_request_count--;
1165 tw_dev->state[request_id] = TW_S_INITIAL;
1166
1167 return retval;
1168} /* End twa_initconnection() */
1169
1170/* This function will initialize the fields of a device extension */
1171static int twa_initialize_device_extension(TW_Device_Extension *tw_dev)
1172{
1173 int i, retval = 1;
1174
1175 /* Initialize command packet buffers */
1176 if (twa_allocate_memory(tw_dev, sizeof(TW_Command_Full), 0)) {
1177 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x16, "Command packet memory allocation failed");
1178 goto out;
1179 }
1180
1181 /* Initialize generic buffer */
1182 if (twa_allocate_memory(tw_dev, TW_SECTOR_SIZE, 1)) {
1183 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x17, "Generic memory allocation failed");
1184 goto out;
1185 }
1186
1187 /* Allocate event info space */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001188 tw_dev->event_queue[0] = kcalloc(TW_Q_LENGTH, sizeof(TW_Event), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (!tw_dev->event_queue[0]) {
1190 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x18, "Event info memory allocation failed");
1191 goto out;
1192 }
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 for (i = 0; i < TW_Q_LENGTH; i++) {
1196 tw_dev->event_queue[i] = (TW_Event *)((unsigned char *)tw_dev->event_queue[0] + (i * sizeof(TW_Event)));
1197 tw_dev->free_queue[i] = i;
1198 tw_dev->state[i] = TW_S_INITIAL;
1199 }
1200
1201 tw_dev->pending_head = TW_Q_START;
1202 tw_dev->pending_tail = TW_Q_START;
1203 tw_dev->free_head = TW_Q_START;
1204 tw_dev->free_tail = TW_Q_START;
1205 tw_dev->error_sequence_id = 1;
1206 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1207
Jes Sorensena12e25bd2006-01-11 08:39:45 -05001208 mutex_init(&tw_dev->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 init_waitqueue_head(&tw_dev->ioctl_wqueue);
1210
1211 retval = 0;
1212out:
1213 return retval;
1214} /* End twa_initialize_device_extension() */
1215
1216/* This function is the interrupt service routine */
David Howells7d12e782006-10-05 14:55:46 +01001217static irqreturn_t twa_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
1219 int request_id, error = 0;
1220 u32 status_reg_value;
1221 TW_Response_Queue response_que;
1222 TW_Command_Full *full_command_packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 TW_Device_Extension *tw_dev = (TW_Device_Extension *)dev_instance;
1224 int handled = 0;
1225
1226 /* Get the per adapter lock */
1227 spin_lock(tw_dev->host->host_lock);
1228
1229 /* Read the registers */
1230 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1231
1232 /* Check if this is our interrupt, otherwise bail */
1233 if (!(status_reg_value & TW_STATUS_VALID_INTERRUPT))
1234 goto twa_interrupt_bail;
1235
1236 handled = 1;
1237
adam radford4039c302006-10-26 18:01:06 -07001238 /* If we are resetting, bail */
1239 if (test_bit(TW_IN_RESET, &tw_dev->flags))
1240 goto twa_interrupt_bail;
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 /* Check controller for errors */
1243 if (twa_check_bits(status_reg_value)) {
1244 if (twa_decode_bits(tw_dev, status_reg_value)) {
1245 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1246 goto twa_interrupt_bail;
1247 }
1248 }
1249
1250 /* Handle host interrupt */
1251 if (status_reg_value & TW_STATUS_HOST_INTERRUPT)
1252 TW_CLEAR_HOST_INTERRUPT(tw_dev);
1253
1254 /* Handle attention interrupt */
1255 if (status_reg_value & TW_STATUS_ATTENTION_INTERRUPT) {
1256 TW_CLEAR_ATTENTION_INTERRUPT(tw_dev);
1257 if (!(test_and_set_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags))) {
1258 twa_get_request_id(tw_dev, &request_id);
1259
1260 error = twa_aen_read_queue(tw_dev, request_id);
1261 if (error) {
1262 tw_dev->state[request_id] = TW_S_COMPLETED;
1263 twa_free_request_id(tw_dev, request_id);
1264 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
1265 }
1266 }
1267 }
1268
1269 /* Handle command interrupt */
1270 if (status_reg_value & TW_STATUS_COMMAND_INTERRUPT) {
1271 TW_MASK_COMMAND_INTERRUPT(tw_dev);
1272 /* Drain as many pending commands as we can */
1273 while (tw_dev->pending_request_count > 0) {
1274 request_id = tw_dev->pending_queue[tw_dev->pending_head];
1275 if (tw_dev->state[request_id] != TW_S_PENDING) {
1276 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x19, "Found request id that wasn't pending");
1277 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1278 goto twa_interrupt_bail;
1279 }
1280 if (twa_post_command_packet(tw_dev, request_id, 1)==0) {
1281 tw_dev->pending_head = (tw_dev->pending_head + 1) % TW_Q_LENGTH;
1282 tw_dev->pending_request_count--;
1283 } else {
1284 /* If we get here, we will continue re-posting on the next command interrupt */
1285 break;
1286 }
1287 }
1288 }
1289
1290 /* Handle response interrupt */
1291 if (status_reg_value & TW_STATUS_RESPONSE_INTERRUPT) {
1292
1293 /* Drain the response queue from the board */
1294 while ((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) {
1295 /* Complete the response */
1296 response_que.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
1297 request_id = TW_RESID_OUT(response_que.response_id);
1298 full_command_packet = tw_dev->command_packet_virt[request_id];
1299 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 /* Check for command packet errors */
1301 if (full_command_packet->command.newcommand.status != 0) {
Harvey Harrison9bcf0912008-05-22 15:45:07 -07001302 if (tw_dev->srb[request_id] != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 error = twa_fill_sense(tw_dev, request_id, 1, 1);
1304 } else {
1305 /* Skip ioctl error prints */
1306 if (request_id != tw_dev->chrdev_request_id) {
1307 error = twa_fill_sense(tw_dev, request_id, 0, 1);
1308 }
1309 }
1310 }
1311
1312 /* Check for correct state */
1313 if (tw_dev->state[request_id] != TW_S_POSTED) {
Harvey Harrison9bcf0912008-05-22 15:45:07 -07001314 if (tw_dev->srb[request_id] != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1a, "Received a request id that wasn't posted");
1316 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1317 goto twa_interrupt_bail;
1318 }
1319 }
1320
1321 /* Check for internal command completion */
Harvey Harrison9bcf0912008-05-22 15:45:07 -07001322 if (tw_dev->srb[request_id] == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (request_id != tw_dev->chrdev_request_id) {
1324 if (twa_aen_complete(tw_dev, request_id))
1325 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1b, "Error completing AEN during attention interrupt");
1326 } else {
1327 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1328 wake_up(&tw_dev->ioctl_wqueue);
1329 }
1330 } else {
FUJITA Tomonori0debe012007-05-26 02:33:31 +09001331 struct scsi_cmnd *cmd;
1332
1333 cmd = tw_dev->srb[request_id];
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 twa_scsiop_execute_scsi_complete(tw_dev, request_id);
1336 /* If no error command was a success */
1337 if (error == 0) {
FUJITA Tomonori0debe012007-05-26 02:33:31 +09001338 cmd->result = (DID_OK << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
1340
1341 /* If error, command failed */
1342 if (error == 1) {
1343 /* Ask for a host reset */
FUJITA Tomonori0debe012007-05-26 02:33:31 +09001344 cmd->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346
1347 /* Report residual bytes for single sgl */
FUJITA Tomonori0debe012007-05-26 02:33:31 +09001348 if ((scsi_sg_count(cmd) <= 1) && (full_command_packet->command.newcommand.status == 0)) {
1349 if (full_command_packet->command.newcommand.sg_list[0].length < scsi_bufflen(tw_dev->srb[request_id]))
1350 scsi_set_resid(cmd, scsi_bufflen(cmd) - full_command_packet->command.newcommand.sg_list[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
1352
1353 /* Now complete the io */
1354 tw_dev->state[request_id] = TW_S_COMPLETED;
1355 twa_free_request_id(tw_dev, request_id);
1356 tw_dev->posted_request_count--;
1357 tw_dev->srb[request_id]->scsi_done(tw_dev->srb[request_id]);
1358 twa_unmap_scsi_data(tw_dev, request_id);
1359 }
1360
1361 /* Check for valid status after each drain */
1362 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1363 if (twa_check_bits(status_reg_value)) {
1364 if (twa_decode_bits(tw_dev, status_reg_value)) {
1365 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1366 goto twa_interrupt_bail;
1367 }
1368 }
1369 }
1370 }
1371
1372twa_interrupt_bail:
1373 spin_unlock(tw_dev->host->host_lock);
1374 return IRQ_RETVAL(handled);
1375} /* End twa_interrupt() */
1376
1377/* This function will load the request id and various sgls for ioctls */
adam radford0e78d152007-07-20 15:28:28 -07001378static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
1380 TW_Command *oldcommand;
1381 TW_Command_Apache *newcommand;
1382 TW_SG_Entry *sgl;
adam radford0e78d152007-07-20 15:28:28 -07001383 unsigned int pae = 0;
1384
1385 if ((sizeof(long) < 8) && (sizeof(dma_addr_t) > 4))
1386 pae = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
1389 newcommand = &full_command_packet->command.newcommand;
adam radford4039c302006-10-26 18:01:06 -07001390 newcommand->request_id__lunl =
1391 cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->request_id__lunl), request_id));
adam radford53ca3532009-12-10 11:53:31 -08001392 if (length) {
1393 newcommand->sg_list[0].address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1);
1394 newcommand->sg_list[0].length = cpu_to_le32(length);
1395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 newcommand->sgl_entries__lunh =
adam radford53ca3532009-12-10 11:53:31 -08001397 cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->sgl_entries__lunh), length ? 1 : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 } else {
1399 oldcommand = &full_command_packet->command.oldcommand;
1400 oldcommand->request_id = request_id;
1401
1402 if (TW_SGL_OUT(oldcommand->opcode__sgloffset)) {
1403 /* Load the sg list */
adam radford0e78d152007-07-20 15:28:28 -07001404 if (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)
1405 sgl = (TW_SG_Entry *)((u32 *)oldcommand+oldcommand->size - (sizeof(TW_SG_Entry)/4) + pae);
1406 else
1407 sgl = (TW_SG_Entry *)((u32 *)oldcommand+TW_SGL_OUT(oldcommand->opcode__sgloffset));
adam radford75913d92006-03-15 12:43:19 -08001408 sgl->address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1);
1409 sgl->length = cpu_to_le32(length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
adam radford0e78d152007-07-20 15:28:28 -07001411 oldcommand->size += pae;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 }
1413 }
1414} /* End twa_load_sgl() */
1415
1416/* This function will perform a pci-dma mapping for a scatter gather list */
1417static int twa_map_scsi_sg_data(TW_Device_Extension *tw_dev, int request_id)
1418{
1419 int use_sg;
1420 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
FUJITA Tomonori0debe012007-05-26 02:33:31 +09001422 use_sg = scsi_dma_map(cmd);
1423 if (!use_sg)
1424 return 0;
1425 else if (use_sg < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1c, "Failed to map scatter gather list");
FUJITA Tomonori0debe012007-05-26 02:33:31 +09001427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
1429
1430 cmd->SCp.phase = TW_PHASE_SGLIST;
1431 cmd->SCp.have_data_in = use_sg;
FUJITA Tomonori0debe012007-05-26 02:33:31 +09001432
1433 return use_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434} /* End twa_map_scsi_sg_data() */
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436/* This function will poll for a response interrupt of a request */
1437static int twa_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds)
1438{
1439 int retval = 1, found = 0, response_request_id;
1440 TW_Response_Queue response_queue;
1441 TW_Command_Full *full_command_packet = tw_dev->command_packet_virt[request_id];
1442
1443 if (twa_poll_status_gone(tw_dev, TW_STATUS_RESPONSE_QUEUE_EMPTY, seconds) == 0) {
1444 response_queue.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
1445 response_request_id = TW_RESID_OUT(response_queue.response_id);
1446 if (request_id != response_request_id) {
1447 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1e, "Found unexpected request id while polling for response");
1448 goto out;
1449 }
1450 if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
1451 if (full_command_packet->command.newcommand.status != 0) {
1452 /* bad response */
1453 twa_fill_sense(tw_dev, request_id, 0, 0);
1454 goto out;
1455 }
1456 found = 1;
1457 } else {
1458 if (full_command_packet->command.oldcommand.status != 0) {
1459 /* bad response */
1460 twa_fill_sense(tw_dev, request_id, 0, 0);
1461 goto out;
1462 }
1463 found = 1;
1464 }
1465 }
1466
1467 if (found)
1468 retval = 0;
1469out:
1470 return retval;
1471} /* End twa_poll_response() */
1472
1473/* This function will poll the status register for a flag */
1474static int twa_poll_status(TW_Device_Extension *tw_dev, u32 flag, int seconds)
1475{
1476 u32 status_reg_value;
1477 unsigned long before;
1478 int retval = 1;
1479
1480 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1481 before = jiffies;
1482
1483 if (twa_check_bits(status_reg_value))
1484 twa_decode_bits(tw_dev, status_reg_value);
1485
1486 while ((status_reg_value & flag) != flag) {
1487 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1488
1489 if (twa_check_bits(status_reg_value))
1490 twa_decode_bits(tw_dev, status_reg_value);
1491
1492 if (time_after(jiffies, before + HZ * seconds))
1493 goto out;
1494
1495 msleep(50);
1496 }
1497 retval = 0;
1498out:
1499 return retval;
1500} /* End twa_poll_status() */
1501
1502/* This function will poll the status register for disappearance of a flag */
1503static int twa_poll_status_gone(TW_Device_Extension *tw_dev, u32 flag, int seconds)
1504{
1505 u32 status_reg_value;
1506 unsigned long before;
1507 int retval = 1;
1508
1509 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1510 before = jiffies;
1511
1512 if (twa_check_bits(status_reg_value))
1513 twa_decode_bits(tw_dev, status_reg_value);
1514
1515 while ((status_reg_value & flag) != 0) {
1516 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1517 if (twa_check_bits(status_reg_value))
1518 twa_decode_bits(tw_dev, status_reg_value);
1519
1520 if (time_after(jiffies, before + HZ * seconds))
1521 goto out;
1522
1523 msleep(50);
1524 }
1525 retval = 0;
1526out:
1527 return retval;
1528} /* End twa_poll_status_gone() */
1529
1530/* This function will attempt to post a command packet to the board */
1531static int twa_post_command_packet(TW_Device_Extension *tw_dev, int request_id, char internal)
1532{
1533 u32 status_reg_value;
1534 dma_addr_t command_que_value;
1535 int retval = 1;
1536
1537 command_que_value = tw_dev->command_packet_phys[request_id];
adam radford4039c302006-10-26 18:01:06 -07001538
1539 /* For 9650SE write low 4 bytes first */
adam radford0e78d152007-07-20 15:28:28 -07001540 if ((tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
1541 (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)) {
adam radford4039c302006-10-26 18:01:06 -07001542 command_que_value += TW_COMMAND_OFFSET;
1543 writel((u32)command_que_value, TW_COMMAND_QUEUE_REG_ADDR_LARGE(tw_dev));
1544 }
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1547
1548 if (twa_check_bits(status_reg_value))
1549 twa_decode_bits(tw_dev, status_reg_value);
1550
1551 if (((tw_dev->pending_request_count > 0) && (tw_dev->state[request_id] != TW_S_PENDING)) || (status_reg_value & TW_STATUS_COMMAND_QUEUE_FULL)) {
1552
1553 /* Only pend internal driver commands */
1554 if (!internal) {
1555 retval = SCSI_MLQUEUE_HOST_BUSY;
1556 goto out;
1557 }
1558
1559 /* Couldn't post the command packet, so we do it later */
1560 if (tw_dev->state[request_id] != TW_S_PENDING) {
1561 tw_dev->state[request_id] = TW_S_PENDING;
1562 tw_dev->pending_request_count++;
1563 if (tw_dev->pending_request_count > tw_dev->max_pending_request_count) {
1564 tw_dev->max_pending_request_count = tw_dev->pending_request_count;
1565 }
1566 tw_dev->pending_queue[tw_dev->pending_tail] = request_id;
1567 tw_dev->pending_tail = (tw_dev->pending_tail + 1) % TW_Q_LENGTH;
1568 }
1569 TW_UNMASK_COMMAND_INTERRUPT(tw_dev);
1570 goto out;
1571 } else {
adam radford0e78d152007-07-20 15:28:28 -07001572 if ((tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
1573 (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)) {
adam radford4039c302006-10-26 18:01:06 -07001574 /* Now write upper 4 bytes */
1575 writel((u32)((u64)command_que_value >> 32), TW_COMMAND_QUEUE_REG_ADDR_LARGE(tw_dev) + 0x4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 } else {
adam radford4039c302006-10-26 18:01:06 -07001577 if (sizeof(dma_addr_t) > 4) {
1578 command_que_value += TW_COMMAND_OFFSET;
1579 writel((u32)command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
1580 writel((u32)((u64)command_que_value >> 32), TW_COMMAND_QUEUE_REG_ADDR(tw_dev) + 0x4);
1581 } else {
1582 writel(TW_COMMAND_OFFSET + command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
1583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
1585 tw_dev->state[request_id] = TW_S_POSTED;
1586 tw_dev->posted_request_count++;
1587 if (tw_dev->posted_request_count > tw_dev->max_posted_request_count) {
1588 tw_dev->max_posted_request_count = tw_dev->posted_request_count;
1589 }
1590 }
1591 retval = 0;
1592out:
1593 return retval;
1594} /* End twa_post_command_packet() */
1595
1596/* This function will reset a device extension */
adam radford0e78d152007-07-20 15:28:28 -07001597static int twa_reset_device_extension(TW_Device_Extension *tw_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
1599 int i = 0;
1600 int retval = 1;
1601 unsigned long flags = 0;
1602
1603 set_bit(TW_IN_RESET, &tw_dev->flags);
1604 TW_DISABLE_INTERRUPTS(tw_dev);
1605 TW_MASK_COMMAND_INTERRUPT(tw_dev);
1606 spin_lock_irqsave(tw_dev->host->host_lock, flags);
1607
1608 /* Abort all requests that are in progress */
1609 for (i = 0; i < TW_Q_LENGTH; i++) {
1610 if ((tw_dev->state[i] != TW_S_FINISHED) &&
1611 (tw_dev->state[i] != TW_S_INITIAL) &&
1612 (tw_dev->state[i] != TW_S_COMPLETED)) {
1613 if (tw_dev->srb[i]) {
1614 tw_dev->srb[i]->result = (DID_RESET << 16);
1615 tw_dev->srb[i]->scsi_done(tw_dev->srb[i]);
1616 twa_unmap_scsi_data(tw_dev, i);
1617 }
1618 }
1619 }
1620
1621 /* Reset queues and counts */
1622 for (i = 0; i < TW_Q_LENGTH; i++) {
1623 tw_dev->free_queue[i] = i;
1624 tw_dev->state[i] = TW_S_INITIAL;
1625 }
1626 tw_dev->free_head = TW_Q_START;
1627 tw_dev->free_tail = TW_Q_START;
1628 tw_dev->posted_request_count = 0;
1629 tw_dev->pending_request_count = 0;
1630 tw_dev->pending_head = TW_Q_START;
1631 tw_dev->pending_tail = TW_Q_START;
1632 tw_dev->reset_print = 0;
1633
1634 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
1635
1636 if (twa_reset_sequence(tw_dev, 1))
1637 goto out;
1638
1639 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
adam radford4039c302006-10-26 18:01:06 -07001640 clear_bit(TW_IN_RESET, &tw_dev->flags);
1641 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 retval = 0;
1644out:
1645 return retval;
1646} /* End twa_reset_device_extension() */
1647
1648/* This function will reset a controller */
1649static int twa_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset)
1650{
1651 int tries = 0, retval = 1, flashed = 0, do_soft_reset = soft_reset;
1652
1653 while (tries < TW_MAX_RESET_TRIES) {
adam radford49bfd8d2005-09-21 17:20:14 -07001654 if (do_soft_reset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 TW_SOFT_RESET(tw_dev);
adam radford49bfd8d2005-09-21 17:20:14 -07001656 /* Clear pchip/response queue on 9550SX */
1657 if (twa_empty_response_queue_large(tw_dev)) {
1658 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x36, "Response queue (large) empty failed during reset sequence");
1659 do_soft_reset = 1;
1660 tries++;
1661 continue;
1662 }
1663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 /* Make sure controller is in a good state */
1666 if (twa_poll_status(tw_dev, TW_STATUS_MICROCONTROLLER_READY | (do_soft_reset == 1 ? TW_STATUS_ATTENTION_INTERRUPT : 0), 60)) {
1667 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1f, "Microcontroller not ready during reset sequence");
1668 do_soft_reset = 1;
1669 tries++;
1670 continue;
1671 }
1672
1673 /* Empty response queue */
1674 if (twa_empty_response_queue(tw_dev)) {
1675 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x20, "Response queue empty failed during reset sequence");
1676 do_soft_reset = 1;
1677 tries++;
1678 continue;
1679 }
1680
1681 flashed = 0;
1682
1683 /* Check for compatibility/flash */
1684 if (twa_check_srl(tw_dev, &flashed)) {
1685 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x21, "Compatibility check failed during reset sequence");
1686 do_soft_reset = 1;
1687 tries++;
1688 continue;
1689 } else {
1690 if (flashed) {
1691 tries++;
1692 continue;
1693 }
1694 }
1695
1696 /* Drain the AEN queue */
1697 if (twa_aen_drain_queue(tw_dev, soft_reset)) {
1698 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x22, "AEN drain failed during reset sequence");
1699 do_soft_reset = 1;
1700 tries++;
1701 continue;
1702 }
1703
1704 /* If we got here, controller is in a good state */
1705 retval = 0;
1706 goto out;
1707 }
1708out:
1709 return retval;
1710} /* End twa_reset_sequence() */
1711
1712/* This funciton returns unit geometry in cylinders/heads/sectors */
1713static int twa_scsi_biosparam(struct scsi_device *sdev, struct block_device *bdev, sector_t capacity, int geom[])
1714{
1715 int heads, sectors, cylinders;
1716 TW_Device_Extension *tw_dev;
1717
1718 tw_dev = (TW_Device_Extension *)sdev->host->hostdata;
1719
1720 if (capacity >= 0x200000) {
1721 heads = 255;
1722 sectors = 63;
1723 cylinders = sector_div(capacity, heads * sectors);
1724 } else {
1725 heads = 64;
1726 sectors = 32;
1727 cylinders = sector_div(capacity, heads * sectors);
1728 }
1729
1730 geom[0] = heads;
1731 geom[1] = sectors;
1732 geom[2] = cylinders;
1733
1734 return 0;
1735} /* End twa_scsi_biosparam() */
1736
1737/* This is the new scsi eh reset function */
1738static int twa_scsi_eh_reset(struct scsi_cmnd *SCpnt)
1739{
1740 TW_Device_Extension *tw_dev = NULL;
1741 int retval = FAILED;
1742
1743 tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 tw_dev->num_resets++;
1746
Jeff Garzik017560f2005-10-24 18:04:36 -04001747 sdev_printk(KERN_WARNING, SCpnt->device,
1748 "WARNING: (0x%02X:0x%04X): Command (0x%x) timed out, resetting card.\n",
1749 TW_DRIVER, 0x2c, SCpnt->cmnd[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
adam radford4039c302006-10-26 18:01:06 -07001751 /* Make sure we are not issuing an ioctl or resetting from ioctl */
1752 mutex_lock(&tw_dev->ioctl_lock);
1753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 /* Now reset the card and some of the device extension data */
adam radford0e78d152007-07-20 15:28:28 -07001755 if (twa_reset_device_extension(tw_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2b, "Controller reset failed during scsi host reset");
1757 goto out;
1758 }
1759
1760 retval = SUCCESS;
1761out:
adam radford4039c302006-10-26 18:01:06 -07001762 mutex_unlock(&tw_dev->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 return retval;
1764} /* End twa_scsi_eh_reset() */
1765
1766/* This is the main scsi queue function to handle scsi opcodes */
1767static int twa_scsi_queue(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
1768{
1769 int request_id, retval;
1770 TW_Device_Extension *tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1771
adam radford4039c302006-10-26 18:01:06 -07001772 /* If we are resetting due to timed out ioctl, report as busy */
1773 if (test_bit(TW_IN_RESET, &tw_dev->flags)) {
1774 retval = SCSI_MLQUEUE_HOST_BUSY;
1775 goto out;
1776 }
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 /* Check if this FW supports luns */
adam radford4039c302006-10-26 18:01:06 -07001779 if ((SCpnt->device->lun != 0) && (tw_dev->tw_compat_info.working_srl < TW_FW_SRL_LUNS_SUPPORTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 SCpnt->result = (DID_BAD_TARGET << 16);
1781 done(SCpnt);
1782 retval = 0;
1783 goto out;
1784 }
1785
1786 /* Save done function into scsi_cmnd struct */
1787 SCpnt->scsi_done = done;
1788
1789 /* Get a free request id */
1790 twa_get_request_id(tw_dev, &request_id);
1791
1792 /* Save the scsi command for use by the ISR */
1793 tw_dev->srb[request_id] = SCpnt;
1794
1795 /* Initialize phase to zero */
1796 SCpnt->SCp.phase = TW_PHASE_INITIAL;
1797
1798 retval = twa_scsiop_execute_scsi(tw_dev, request_id, NULL, 0, NULL);
1799 switch (retval) {
1800 case SCSI_MLQUEUE_HOST_BUSY:
1801 twa_free_request_id(tw_dev, request_id);
1802 break;
1803 case 1:
1804 tw_dev->state[request_id] = TW_S_COMPLETED;
1805 twa_free_request_id(tw_dev, request_id);
1806 SCpnt->result = (DID_ERROR << 16);
1807 done(SCpnt);
1808 retval = 0;
1809 }
1810out:
1811 return retval;
1812} /* End twa_scsi_queue() */
1813
1814/* This function hands scsi cdb's to the firmware */
1815static int twa_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id, char *cdb, int use_sg, TW_SG_Entry *sglistarg)
1816{
1817 TW_Command_Full *full_command_packet;
1818 TW_Command_Apache *command_packet;
1819 u32 num_sectors = 0x0;
1820 int i, sg_count;
1821 struct scsi_cmnd *srb = NULL;
FUJITA Tomonori0debe012007-05-26 02:33:31 +09001822 struct scatterlist *sglist = NULL, *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 int retval = 1;
1824
1825 if (tw_dev->srb[request_id]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 srb = tw_dev->srb[request_id];
FUJITA Tomonori0debe012007-05-26 02:33:31 +09001827 if (scsi_sglist(srb))
1828 sglist = scsi_sglist(srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 }
1830
1831 /* Initialize command packet */
1832 full_command_packet = tw_dev->command_packet_virt[request_id];
1833 full_command_packet->header.header_desc.size_header = 128;
1834 full_command_packet->header.status_block.error = 0;
1835 full_command_packet->header.status_block.severity__reserved = 0;
1836
1837 command_packet = &full_command_packet->command.newcommand;
1838 command_packet->status = 0;
1839 command_packet->opcode__reserved = TW_OPRES_IN(0, TW_OP_EXECUTE_SCSI);
1840
1841 /* We forced 16 byte cdb use earlier */
1842 if (!cdb)
1843 memcpy(command_packet->cdb, srb->cmnd, TW_MAX_CDB_LEN);
1844 else
1845 memcpy(command_packet->cdb, cdb, TW_MAX_CDB_LEN);
1846
1847 if (srb) {
1848 command_packet->unit = srb->device->id;
1849 command_packet->request_id__lunl =
adam radford75913d92006-03-15 12:43:19 -08001850 cpu_to_le16(TW_REQ_LUN_IN(srb->device->lun, request_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 } else {
1852 command_packet->request_id__lunl =
adam radford75913d92006-03-15 12:43:19 -08001853 cpu_to_le16(TW_REQ_LUN_IN(0, request_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 command_packet->unit = 0;
1855 }
1856
1857 command_packet->sgl_offset = 16;
1858
1859 if (!sglistarg) {
1860 /* Map sglist from scsi layer to cmd packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
FUJITA Tomonori0debe012007-05-26 02:33:31 +09001862 if (scsi_sg_count(srb)) {
1863 if ((scsi_sg_count(srb) == 1) &&
1864 (scsi_bufflen(srb) < TW_MIN_SGL_LENGTH)) {
FUJITA Tomonori035f5e02008-03-09 13:44:37 +09001865 if (srb->sc_data_direction == DMA_TO_DEVICE ||
1866 srb->sc_data_direction == DMA_BIDIRECTIONAL)
1867 scsi_sg_copy_to_buffer(srb,
1868 tw_dev->generic_buffer_virt[request_id],
1869 TW_SECTOR_SIZE);
adam radford75913d92006-03-15 12:43:19 -08001870 command_packet->sg_list[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
1871 command_packet->sg_list[0].length = cpu_to_le32(TW_MIN_SGL_LENGTH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 } else {
1873 sg_count = twa_map_scsi_sg_data(tw_dev, request_id);
1874 if (sg_count == 0)
1875 goto out;
1876
FUJITA Tomonori0debe012007-05-26 02:33:31 +09001877 scsi_for_each_sg(srb, sg, sg_count, i) {
1878 command_packet->sg_list[i].address = TW_CPU_TO_SGL(sg_dma_address(sg));
1879 command_packet->sg_list[i].length = cpu_to_le32(sg_dma_len(sg));
adam radford75913d92006-03-15 12:43:19 -08001880 if (command_packet->sg_list[i].address & TW_CPU_TO_SGL(TW_ALIGNMENT_9000_SGL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2e, "Found unaligned sgl address during execute scsi");
1882 goto out;
1883 }
1884 }
1885 }
FUJITA Tomonori0debe012007-05-26 02:33:31 +09001886 command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN((srb->device->lun >> 4), scsi_sg_count(tw_dev->srb[request_id])));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 }
1888 } else {
1889 /* Internal cdb post */
1890 for (i = 0; i < use_sg; i++) {
adam radford75913d92006-03-15 12:43:19 -08001891 command_packet->sg_list[i].address = TW_CPU_TO_SGL(sglistarg[i].address);
1892 command_packet->sg_list[i].length = cpu_to_le32(sglistarg[i].length);
1893 if (command_packet->sg_list[i].address & TW_CPU_TO_SGL(TW_ALIGNMENT_9000_SGL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2f, "Found unaligned sgl address during internal post");
1895 goto out;
1896 }
1897 }
adam radford75913d92006-03-15 12:43:19 -08001898 command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN(0, use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 }
1900
1901 if (srb) {
1902 if (srb->cmnd[0] == READ_6 || srb->cmnd[0] == WRITE_6)
1903 num_sectors = (u32)srb->cmnd[4];
1904
1905 if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10)
1906 num_sectors = (u32)srb->cmnd[8] | ((u32)srb->cmnd[7] << 8);
1907 }
1908
1909 /* Update sector statistic */
1910 tw_dev->sector_count = num_sectors;
1911 if (tw_dev->sector_count > tw_dev->max_sector_count)
1912 tw_dev->max_sector_count = tw_dev->sector_count;
1913
1914 /* Update SG statistics */
1915 if (srb) {
FUJITA Tomonori0debe012007-05-26 02:33:31 +09001916 tw_dev->sgl_entries = scsi_sg_count(tw_dev->srb[request_id]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (tw_dev->sgl_entries > tw_dev->max_sgl_entries)
1918 tw_dev->max_sgl_entries = tw_dev->sgl_entries;
1919 }
1920
1921 /* Now post the command to the board */
1922 if (srb) {
1923 retval = twa_post_command_packet(tw_dev, request_id, 0);
1924 } else {
1925 twa_post_command_packet(tw_dev, request_id, 1);
1926 retval = 0;
1927 }
1928out:
1929 return retval;
1930} /* End twa_scsiop_execute_scsi() */
1931
1932/* This function completes an execute scsi operation */
1933static void twa_scsiop_execute_scsi_complete(TW_Device_Extension *tw_dev, int request_id)
1934{
FUJITA Tomonori0debe012007-05-26 02:33:31 +09001935 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
FUJITA Tomonori0debe012007-05-26 02:33:31 +09001936
1937 if (scsi_bufflen(cmd) < TW_MIN_SGL_LENGTH &&
1938 (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1939 cmd->sc_data_direction == DMA_BIDIRECTIONAL)) {
FUJITA Tomonorib1192d52007-05-31 15:24:03 +09001940 if (scsi_sg_count(cmd) == 1) {
FUJITA Tomonori035f5e02008-03-09 13:44:37 +09001941 void *buf = tw_dev->generic_buffer_virt[request_id];
1942
FUJITA Tomonori035f5e02008-03-09 13:44:37 +09001943 scsi_sg_copy_from_buffer(cmd, buf, TW_SECTOR_SIZE);
adam radfordd327d082005-09-09 15:55:13 -07001944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
1946} /* End twa_scsiop_execute_scsi_complete() */
1947
1948/* This function tells the controller to shut down */
1949static void __twa_shutdown(TW_Device_Extension *tw_dev)
1950{
1951 /* Disable interrupts */
1952 TW_DISABLE_INTERRUPTS(tw_dev);
1953
adam radford4039c302006-10-26 18:01:06 -07001954 /* Free up the IRQ */
1955 free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 printk(KERN_WARNING "3w-9xxx: Shutting down host %d.\n", tw_dev->host->host_no);
1958
1959 /* Tell the card we are shutting down */
1960 if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
1961 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x31, "Connection shutdown failed");
1962 } else {
1963 printk(KERN_WARNING "3w-9xxx: Shutdown complete.\n");
1964 }
1965
1966 /* Clear all interrupts just before exit */
1967 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1968} /* End __twa_shutdown() */
1969
1970/* Wrapper for __twa_shutdown */
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07001971static void twa_shutdown(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07001973 struct Scsi_Host *host = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
1975
1976 __twa_shutdown(tw_dev);
1977} /* End twa_shutdown() */
1978
1979/* This function will look up a string */
1980static char *twa_string_lookup(twa_message_type *table, unsigned int code)
1981{
1982 int index;
1983
1984 for (index = 0; ((code != table[index].code) &&
1985 (table[index].text != (char *)0)); index++);
1986 return(table[index].text);
1987} /* End twa_string_lookup() */
1988
1989/* This function will perform a pci-dma unmap */
1990static void twa_unmap_scsi_data(TW_Device_Extension *tw_dev, int request_id)
1991{
1992 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
adam radford8454e982009-05-05 11:45:37 -07001994 if (cmd->SCp.phase == TW_PHASE_SGLIST)
1995 scsi_dma_unmap(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996} /* End twa_unmap_scsi_data() */
1997
adam radford4deedd82010-03-08 12:37:46 -08001998/* This function gets called when a disk is coming on-line */
1999static int twa_slave_configure(struct scsi_device *sdev)
2000{
2001 /* Force 60 second timeout */
2002 blk_queue_rq_timeout(sdev->request_queue, 60 * HZ);
2003
2004 return 0;
2005} /* End twa_slave_configure() */
2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007/* scsi_host_template initializer */
2008static struct scsi_host_template driver_template = {
2009 .module = THIS_MODULE,
2010 .name = "3ware 9000 Storage Controller",
2011 .queuecommand = twa_scsi_queue,
2012 .eh_host_reset_handler = twa_scsi_eh_reset,
2013 .bios_param = twa_scsi_biosparam,
2014 .change_queue_depth = twa_change_queue_depth,
2015 .can_queue = TW_Q_LENGTH-2,
adam radford4deedd82010-03-08 12:37:46 -08002016 .slave_configure = twa_slave_configure,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 .this_id = -1,
2018 .sg_tablesize = TW_APACHE_MAX_SGL_LENGTH,
2019 .max_sectors = TW_MAX_SECTORS,
2020 .cmd_per_lun = TW_MAX_CMDS_PER_LUN,
2021 .use_clustering = ENABLE_CLUSTERING,
2022 .shost_attrs = twa_host_attrs,
2023 .emulated = 1
2024};
2025
2026/* This function will probe and initialize a card */
2027static int __devinit twa_probe(struct pci_dev *pdev, const struct pci_device_id *dev_id)
2028{
2029 struct Scsi_Host *host = NULL;
2030 TW_Device_Extension *tw_dev;
adam radford3dabec72008-07-22 16:47:40 -07002031 unsigned long mem_addr, mem_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 int retval = -ENODEV;
2033
2034 retval = pci_enable_device(pdev);
2035 if (retval) {
2036 TW_PRINTK(host, TW_DRIVER, 0x34, "Failed to enable pci device");
2037 goto out_disable_device;
2038 }
2039
2040 pci_set_master(pdev);
Tony Battersby1e6c38c2007-11-09 13:04:35 -05002041 pci_try_set_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Yang Hongyang6a355282009-04-06 19:01:13 -07002043 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))
2044 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
Yang Hongyang284901a2009-04-06 19:01:15 -07002045 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
2046 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
adam radford0e78d152007-07-20 15:28:28 -07002047 TW_PRINTK(host, TW_DRIVER, 0x23, "Failed to set dma mask");
2048 retval = -ENODEV;
2049 goto out_disable_device;
2050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
2052 host = scsi_host_alloc(&driver_template, sizeof(TW_Device_Extension));
2053 if (!host) {
2054 TW_PRINTK(host, TW_DRIVER, 0x24, "Failed to allocate memory for device extension");
2055 retval = -ENOMEM;
2056 goto out_disable_device;
2057 }
2058 tw_dev = (TW_Device_Extension *)host->hostdata;
2059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 /* Save values to device extension */
2061 tw_dev->host = host;
2062 tw_dev->tw_pci_dev = pdev;
2063
2064 if (twa_initialize_device_extension(tw_dev)) {
2065 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x25, "Failed to initialize device extension");
2066 goto out_free_device_extension;
2067 }
2068
2069 /* Request IO regions */
2070 retval = pci_request_regions(pdev, "3w-9xxx");
2071 if (retval) {
2072 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x26, "Failed to get mem region");
2073 goto out_free_device_extension;
2074 }
2075
adam radford3dabec72008-07-22 16:47:40 -07002076 if (pdev->device == PCI_DEVICE_ID_3WARE_9000) {
adam radford49bfd8d2005-09-21 17:20:14 -07002077 mem_addr = pci_resource_start(pdev, 1);
adam radford3dabec72008-07-22 16:47:40 -07002078 mem_len = pci_resource_len(pdev, 1);
2079 } else {
adam radford49bfd8d2005-09-21 17:20:14 -07002080 mem_addr = pci_resource_start(pdev, 2);
adam radford3dabec72008-07-22 16:47:40 -07002081 mem_len = pci_resource_len(pdev, 2);
2082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 /* Save base address */
adam radford3dabec72008-07-22 16:47:40 -07002085 tw_dev->base_addr = ioremap(mem_addr, mem_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 if (!tw_dev->base_addr) {
2087 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x35, "Failed to ioremap");
2088 goto out_release_mem_region;
2089 }
2090
2091 /* Disable interrupts on the card */
2092 TW_DISABLE_INTERRUPTS(tw_dev);
2093
2094 /* Initialize the card */
2095 if (twa_reset_sequence(tw_dev, 0))
adam radford4039c302006-10-26 18:01:06 -07002096 goto out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098 /* Set host specific parameters */
adam radford0e78d152007-07-20 15:28:28 -07002099 if ((pdev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
2100 (pdev->device == PCI_DEVICE_ID_3WARE_9690SA))
adam radford4039c302006-10-26 18:01:06 -07002101 host->max_id = TW_MAX_UNITS_9650SE;
2102 else
2103 host->max_id = TW_MAX_UNITS;
2104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 host->max_cmd_len = TW_MAX_CDB_LEN;
2106
2107 /* Channels aren't supported by adapter */
adam radford4039c302006-10-26 18:01:06 -07002108 host->max_lun = TW_MAX_LUNS(tw_dev->tw_compat_info.working_srl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 host->max_channel = 0;
2110
2111 /* Register the card with the kernel SCSI layer */
2112 retval = scsi_add_host(host, &pdev->dev);
2113 if (retval) {
2114 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x27, "scsi add host failed");
adam radford4039c302006-10-26 18:01:06 -07002115 goto out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 }
2117
2118 pci_set_drvdata(pdev, host);
2119
adam radford3dabec72008-07-22 16:47:40 -07002120 printk(KERN_WARNING "3w-9xxx: scsi%d: Found a 3ware 9000 Storage Controller at 0x%lx, IRQ: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 host->host_no, mem_addr, pdev->irq);
2122 printk(KERN_WARNING "3w-9xxx: scsi%d: Firmware %s, BIOS %s, Ports: %d.\n",
2123 host->host_no,
2124 (char *)twa_get_param(tw_dev, 0, TW_VERSION_TABLE,
2125 TW_PARAM_FWVER, TW_PARAM_FWVER_LENGTH),
2126 (char *)twa_get_param(tw_dev, 1, TW_VERSION_TABLE,
2127 TW_PARAM_BIOSVER, TW_PARAM_BIOSVER_LENGTH),
adam radford75913d92006-03-15 12:43:19 -08002128 le32_to_cpu(*(int *)twa_get_param(tw_dev, 2, TW_INFORMATION_TABLE,
2129 TW_PARAM_PORTCOUNT, TW_PARAM_PORTCOUNT_LENGTH)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
adam radford3dabec72008-07-22 16:47:40 -07002131 /* Try to enable MSI */
2132 if (use_msi && (pdev->device != PCI_DEVICE_ID_3WARE_9000) &&
2133 !pci_enable_msi(pdev))
2134 set_bit(TW_USING_MSI, &tw_dev->flags);
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 /* Now setup the interrupt handler */
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07002137 retval = request_irq(pdev->irq, twa_interrupt, IRQF_SHARED, "3w-9xxx", tw_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 if (retval) {
2139 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x30, "Error requesting IRQ");
2140 goto out_remove_host;
2141 }
2142
2143 twa_device_extension_list[twa_device_extension_count] = tw_dev;
2144 twa_device_extension_count++;
2145
2146 /* Re-enable interrupts on the card */
2147 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
2148
2149 /* Finally, scan the host */
2150 scsi_scan_host(host);
2151
2152 if (twa_major == -1) {
2153 if ((twa_major = register_chrdev (0, "twa", &twa_fops)) < 0)
2154 TW_PRINTK(host, TW_DRIVER, 0x29, "Failed to register character device");
2155 }
2156 return 0;
2157
2158out_remove_host:
adam radford3dabec72008-07-22 16:47:40 -07002159 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2160 pci_disable_msi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 scsi_remove_host(host);
adam radford4039c302006-10-26 18:01:06 -07002162out_iounmap:
2163 iounmap(tw_dev->base_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164out_release_mem_region:
2165 pci_release_regions(pdev);
2166out_free_device_extension:
2167 twa_free_device_extension(tw_dev);
2168 scsi_host_put(host);
2169out_disable_device:
2170 pci_disable_device(pdev);
2171
2172 return retval;
2173} /* End twa_probe() */
2174
2175/* This function is called to remove a device */
2176static void twa_remove(struct pci_dev *pdev)
2177{
2178 struct Scsi_Host *host = pci_get_drvdata(pdev);
2179 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2180
2181 scsi_remove_host(tw_dev->host);
2182
2183 /* Unregister character device */
2184 if (twa_major >= 0) {
2185 unregister_chrdev(twa_major, "twa");
2186 twa_major = -1;
2187 }
2188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 /* Shutdown the card */
2190 __twa_shutdown(tw_dev);
2191
adam radford3dabec72008-07-22 16:47:40 -07002192 /* Disable MSI if enabled */
2193 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2194 pci_disable_msi(pdev);
2195
adam radford4039c302006-10-26 18:01:06 -07002196 /* Free IO remapping */
2197 iounmap(tw_dev->base_addr);
2198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 /* Free up the mem region */
2200 pci_release_regions(pdev);
2201
2202 /* Free up device extension resources */
2203 twa_free_device_extension(tw_dev);
2204
2205 scsi_host_put(tw_dev->host);
2206 pci_disable_device(pdev);
2207 twa_device_extension_count--;
2208} /* End twa_remove() */
2209
adam radford7a252fe2009-03-09 12:15:01 -08002210#ifdef CONFIG_PM
2211/* This function is called on PCI suspend */
2212static int twa_suspend(struct pci_dev *pdev, pm_message_t state)
2213{
2214 struct Scsi_Host *host = pci_get_drvdata(pdev);
2215 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2216
2217 printk(KERN_WARNING "3w-9xxx: Suspending host %d.\n", tw_dev->host->host_no);
2218
2219 TW_DISABLE_INTERRUPTS(tw_dev);
2220 free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
2221
2222 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2223 pci_disable_msi(pdev);
2224
2225 /* Tell the card we are shutting down */
2226 if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
2227 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x38, "Connection shutdown failed during suspend");
2228 } else {
2229 printk(KERN_WARNING "3w-9xxx: Suspend complete.\n");
2230 }
2231 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
2232
2233 pci_save_state(pdev);
2234 pci_disable_device(pdev);
2235 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2236
2237 return 0;
2238} /* End twa_suspend() */
2239
2240/* This function is called on PCI resume */
2241static int twa_resume(struct pci_dev *pdev)
2242{
2243 int retval = 0;
2244 struct Scsi_Host *host = pci_get_drvdata(pdev);
2245 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2246
2247 printk(KERN_WARNING "3w-9xxx: Resuming host %d.\n", tw_dev->host->host_no);
2248 pci_set_power_state(pdev, PCI_D0);
2249 pci_enable_wake(pdev, PCI_D0, 0);
2250 pci_restore_state(pdev);
2251
2252 retval = pci_enable_device(pdev);
2253 if (retval) {
2254 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x39, "Enable device failed during resume");
2255 return retval;
2256 }
2257
2258 pci_set_master(pdev);
2259 pci_try_set_mwi(pdev);
2260
Yang Hongyange9304382009-04-13 14:40:14 -07002261 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))
2262 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
2263 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
2264 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
adam radford7a252fe2009-03-09 12:15:01 -08002265 TW_PRINTK(host, TW_DRIVER, 0x40, "Failed to set dma mask during resume");
2266 retval = -ENODEV;
2267 goto out_disable_device;
2268 }
2269
2270 /* Initialize the card */
2271 if (twa_reset_sequence(tw_dev, 0)) {
2272 retval = -ENODEV;
2273 goto out_disable_device;
2274 }
2275
2276 /* Now setup the interrupt handler */
2277 retval = request_irq(pdev->irq, twa_interrupt, IRQF_SHARED, "3w-9xxx", tw_dev);
2278 if (retval) {
2279 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x42, "Error requesting IRQ during resume");
2280 retval = -ENODEV;
2281 goto out_disable_device;
2282 }
2283
2284 /* Now enable MSI if enabled */
2285 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2286 pci_enable_msi(pdev);
2287
2288 /* Re-enable interrupts on the card */
2289 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
2290
2291 printk(KERN_WARNING "3w-9xxx: Resume complete.\n");
2292 return 0;
2293
2294out_disable_device:
2295 scsi_remove_host(host);
2296 pci_disable_device(pdev);
2297
2298 return retval;
2299} /* End twa_resume() */
2300#endif
2301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302/* PCI Devices supported by this driver */
2303static struct pci_device_id twa_pci_tbl[] __devinitdata = {
2304 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9000,
2305 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
adam radford49bfd8d2005-09-21 17:20:14 -07002306 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9550SX,
2307 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
adam radford4039c302006-10-26 18:01:06 -07002308 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9650SE,
2309 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
adam radford0e78d152007-07-20 15:28:28 -07002310 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9690SA,
2311 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 { }
2313};
2314MODULE_DEVICE_TABLE(pci, twa_pci_tbl);
2315
2316/* pci_driver initializer */
2317static struct pci_driver twa_driver = {
2318 .name = "3w-9xxx",
2319 .id_table = twa_pci_tbl,
2320 .probe = twa_probe,
2321 .remove = twa_remove,
adam radford7a252fe2009-03-09 12:15:01 -08002322#ifdef CONFIG_PM
2323 .suspend = twa_suspend,
2324 .resume = twa_resume,
2325#endif
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07002326 .shutdown = twa_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327};
2328
2329/* This function is called on driver initialization */
2330static int __init twa_init(void)
2331{
2332 printk(KERN_WARNING "3ware 9000 Storage Controller device driver for Linux v%s.\n", TW_DRIVER_VERSION);
2333
Henrik Kretzschmardcbccbde2006-09-25 16:58:58 -07002334 return pci_register_driver(&twa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335} /* End twa_init() */
2336
2337/* This function is called on driver exit */
2338static void __exit twa_exit(void)
2339{
2340 pci_unregister_driver(&twa_driver);
2341} /* End twa_exit() */
2342
2343module_init(twa_init);
2344module_exit(twa_exit);
2345