blob: add419d6ff34996ed4aab8a145aee637ee987dbf [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153/* Functions */
154
155/* Show some statistics about the card */
Tony Jonesee959b02008-02-22 00:13:36 +0100156static ssize_t twa_show_stats(struct device *dev,
157 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Tony Jonesee959b02008-02-22 00:13:36 +0100159 struct Scsi_Host *host = class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
161 unsigned long flags = 0;
162 ssize_t len;
163
164 spin_lock_irqsave(tw_dev->host->host_lock, flags);
165 len = snprintf(buf, PAGE_SIZE, "3w-9xxx Driver version: %s\n"
166 "Current commands posted: %4d\n"
167 "Max commands posted: %4d\n"
168 "Current pending commands: %4d\n"
169 "Max pending commands: %4d\n"
170 "Last sgl length: %4d\n"
171 "Max sgl length: %4d\n"
172 "Last sector count: %4d\n"
173 "Max sector count: %4d\n"
174 "SCSI Host Resets: %4d\n"
175 "AEN's: %4d\n",
176 TW_DRIVER_VERSION,
177 tw_dev->posted_request_count,
178 tw_dev->max_posted_request_count,
179 tw_dev->pending_request_count,
180 tw_dev->max_pending_request_count,
181 tw_dev->sgl_entries,
182 tw_dev->max_sgl_entries,
183 tw_dev->sector_count,
184 tw_dev->max_sector_count,
185 tw_dev->num_resets,
186 tw_dev->aen_count);
187 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
188 return len;
189} /* End twa_show_stats() */
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/* Create sysfs 'stats' entry */
Tony Jonesee959b02008-02-22 00:13:36 +0100192static struct device_attribute twa_host_stats_attr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 .attr = {
194 .name = "stats",
195 .mode = S_IRUGO,
196 },
197 .show = twa_show_stats
198};
199
200/* Host attributes initializer */
Tony Jonesee959b02008-02-22 00:13:36 +0100201static struct device_attribute *twa_host_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 &twa_host_stats_attr,
203 NULL,
204};
205
206/* File operations struct for character device */
Arjan van de Ven00977a52007-02-12 00:55:34 -0800207static const struct file_operations twa_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 .owner = THIS_MODULE,
Arnd Bergmannf4927c42010-04-27 00:24:01 +0200209 .unlocked_ioctl = twa_chrdev_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 .open = twa_chrdev_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200211 .release = NULL,
212 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213};
214
215/* This function will complete an aen request from the isr */
216static int twa_aen_complete(TW_Device_Extension *tw_dev, int request_id)
217{
218 TW_Command_Full *full_command_packet;
219 TW_Command *command_packet;
220 TW_Command_Apache_Header *header;
221 unsigned short aen;
222 int retval = 1;
223
224 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
225 tw_dev->posted_request_count--;
adam radford75913d92006-03-15 12:43:19 -0800226 aen = le16_to_cpu(header->status_block.error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 full_command_packet = tw_dev->command_packet_virt[request_id];
228 command_packet = &full_command_packet->command.oldcommand;
229
230 /* First check for internal completion of set param for time sync */
231 if (TW_OP_OUT(command_packet->opcode__sgloffset) == TW_OP_SET_PARAM) {
232 /* Keep reading the queue in case there are more aen's */
233 if (twa_aen_read_queue(tw_dev, request_id))
234 goto out2;
235 else {
236 retval = 0;
237 goto out;
238 }
239 }
240
241 switch (aen) {
242 case TW_AEN_QUEUE_EMPTY:
243 /* Quit reading the queue if this is the last one */
244 break;
245 case TW_AEN_SYNC_TIME_WITH_HOST:
246 twa_aen_sync_time(tw_dev, request_id);
247 retval = 0;
248 goto out;
249 default:
250 twa_aen_queue_event(tw_dev, header);
251
252 /* If there are more aen's, keep reading the queue */
253 if (twa_aen_read_queue(tw_dev, request_id))
254 goto out2;
255 else {
256 retval = 0;
257 goto out;
258 }
259 }
260 retval = 0;
261out2:
262 tw_dev->state[request_id] = TW_S_COMPLETED;
263 twa_free_request_id(tw_dev, request_id);
264 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
265out:
266 return retval;
267} /* End twa_aen_complete() */
268
269/* This function will drain aen queue */
270static int twa_aen_drain_queue(TW_Device_Extension *tw_dev, int no_check_reset)
271{
272 int request_id = 0;
273 char cdb[TW_MAX_CDB_LEN];
274 TW_SG_Entry sglist[1];
275 int finished = 0, count = 0;
276 TW_Command_Full *full_command_packet;
277 TW_Command_Apache_Header *header;
278 unsigned short aen;
279 int first_reset = 0, queue = 0, retval = 1;
280
281 if (no_check_reset)
282 first_reset = 0;
283 else
284 first_reset = 1;
285
286 full_command_packet = tw_dev->command_packet_virt[request_id];
287 memset(full_command_packet, 0, sizeof(TW_Command_Full));
288
289 /* Initialize cdb */
290 memset(&cdb, 0, TW_MAX_CDB_LEN);
291 cdb[0] = REQUEST_SENSE; /* opcode */
292 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
293
294 /* Initialize sglist */
295 memset(&sglist, 0, sizeof(TW_SG_Entry));
296 sglist[0].length = TW_SECTOR_SIZE;
297 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
298
299 if (sglist[0].address & TW_ALIGNMENT_9000_SGL) {
300 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1, "Found unaligned address during AEN drain");
301 goto out;
302 }
303
304 /* Mark internal command */
305 tw_dev->srb[request_id] = NULL;
306
307 do {
308 /* Send command to the board */
309 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
310 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2, "Error posting request sense");
311 goto out;
312 }
313
314 /* Now poll for completion */
315 if (twa_poll_response(tw_dev, request_id, 30)) {
316 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x3, "No valid response while draining AEN queue");
317 tw_dev->posted_request_count--;
318 goto out;
319 }
320
321 tw_dev->posted_request_count--;
322 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
adam radford75913d92006-03-15 12:43:19 -0800323 aen = le16_to_cpu(header->status_block.error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 queue = 0;
325 count++;
326
327 switch (aen) {
328 case TW_AEN_QUEUE_EMPTY:
329 if (first_reset != 1)
330 goto out;
331 else
332 finished = 1;
333 break;
334 case TW_AEN_SOFT_RESET:
335 if (first_reset == 0)
336 first_reset = 1;
337 else
338 queue = 1;
339 break;
340 case TW_AEN_SYNC_TIME_WITH_HOST:
341 break;
342 default:
343 queue = 1;
344 }
345
346 /* Now queue an event info */
347 if (queue)
348 twa_aen_queue_event(tw_dev, header);
349 } while ((finished == 0) && (count < TW_MAX_AEN_DRAIN));
350
351 if (count == TW_MAX_AEN_DRAIN)
352 goto out;
353
354 retval = 0;
355out:
356 tw_dev->state[request_id] = TW_S_INITIAL;
357 return retval;
358} /* End twa_aen_drain_queue() */
359
360/* This function will queue an event */
361static void twa_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header)
362{
363 u32 local_time;
364 struct timeval time;
365 TW_Event *event;
366 unsigned short aen;
367 char host[16];
368 char *error_str;
369
370 tw_dev->aen_count++;
371
372 /* Fill out event info */
373 event = tw_dev->event_queue[tw_dev->error_index];
374
375 /* Check for clobber */
376 host[0] = '\0';
377 if (tw_dev->host) {
378 sprintf(host, " scsi%d:", tw_dev->host->host_no);
379 if (event->retrieved == TW_AEN_NOT_RETRIEVED)
380 tw_dev->aen_clobber = 1;
381 }
382
adam radford75913d92006-03-15 12:43:19 -0800383 aen = le16_to_cpu(header->status_block.error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 memset(event, 0, sizeof(TW_Event));
385
386 event->severity = TW_SEV_OUT(header->status_block.severity__reserved);
387 do_gettimeofday(&time);
388 local_time = (u32)(time.tv_sec - (sys_tz.tz_minuteswest * 60));
389 event->time_stamp_sec = local_time;
390 event->aen_code = aen;
391 event->retrieved = TW_AEN_NOT_RETRIEVED;
392 event->sequence_id = tw_dev->error_sequence_id;
393 tw_dev->error_sequence_id++;
394
395 /* Check for embedded error string */
396 error_str = &(header->err_specific_desc[strlen(header->err_specific_desc)+1]);
397
398 header->err_specific_desc[sizeof(header->err_specific_desc) - 1] = '\0';
399 event->parameter_len = strlen(header->err_specific_desc);
adam radford75913d92006-03-15 12:43:19 -0800400 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 -0700401 if (event->severity != TW_AEN_SEVERITY_DEBUG)
402 printk(KERN_WARNING "3w-9xxx:%s AEN: %s (0x%02X:0x%04X): %s:%s.\n",
403 host,
404 twa_aen_severity_lookup(TW_SEV_OUT(header->status_block.severity__reserved)),
405 TW_MESSAGE_SOURCE_CONTROLLER_EVENT, aen,
406 error_str[0] == '\0' ? twa_string_lookup(twa_aen_table, aen) : error_str,
407 header->err_specific_desc);
408 else
409 tw_dev->aen_count--;
410
411 if ((tw_dev->error_index + 1) == TW_Q_LENGTH)
412 tw_dev->event_queue_wrapped = 1;
413 tw_dev->error_index = (tw_dev->error_index + 1 ) % TW_Q_LENGTH;
414} /* End twa_aen_queue_event() */
415
416/* This function will read the aen queue from the isr */
417static int twa_aen_read_queue(TW_Device_Extension *tw_dev, int request_id)
418{
419 char cdb[TW_MAX_CDB_LEN];
420 TW_SG_Entry sglist[1];
421 TW_Command_Full *full_command_packet;
422 int retval = 1;
423
424 full_command_packet = tw_dev->command_packet_virt[request_id];
425 memset(full_command_packet, 0, sizeof(TW_Command_Full));
426
427 /* Initialize cdb */
428 memset(&cdb, 0, TW_MAX_CDB_LEN);
429 cdb[0] = REQUEST_SENSE; /* opcode */
430 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
431
432 /* Initialize sglist */
433 memset(&sglist, 0, sizeof(TW_SG_Entry));
434 sglist[0].length = TW_SECTOR_SIZE;
435 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
436
437 /* Mark internal command */
438 tw_dev->srb[request_id] = NULL;
439
440 /* Now post the command packet */
441 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
442 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x4, "Post failed while reading AEN queue");
443 goto out;
444 }
445 retval = 0;
446out:
447 return retval;
448} /* End twa_aen_read_queue() */
449
450/* This function will look up an AEN severity string */
451static char *twa_aen_severity_lookup(unsigned char severity_code)
452{
453 char *retval = NULL;
454
455 if ((severity_code < (unsigned char) TW_AEN_SEVERITY_ERROR) ||
456 (severity_code > (unsigned char) TW_AEN_SEVERITY_DEBUG))
457 goto out;
458
459 retval = twa_aen_severity_table[severity_code];
460out:
461 return retval;
462} /* End twa_aen_severity_lookup() */
463
464/* This function will sync firmware time with the host time */
465static void twa_aen_sync_time(TW_Device_Extension *tw_dev, int request_id)
466{
467 u32 schedulertime;
468 struct timeval utc;
469 TW_Command_Full *full_command_packet;
470 TW_Command *command_packet;
471 TW_Param_Apache *param;
472 u32 local_time;
473
474 /* Fill out the command packet */
475 full_command_packet = tw_dev->command_packet_virt[request_id];
476 memset(full_command_packet, 0, sizeof(TW_Command_Full));
477 command_packet = &full_command_packet->command.oldcommand;
478 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_SET_PARAM);
479 command_packet->request_id = request_id;
adam radford75913d92006-03-15 12:43:19 -0800480 command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
481 command_packet->byte8_offset.param.sgl[0].length = cpu_to_le32(TW_SECTOR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 command_packet->size = TW_COMMAND_SIZE;
adam radford75913d92006-03-15 12:43:19 -0800483 command_packet->byte6_offset.parameter_count = cpu_to_le16(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 /* Setup the param */
486 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
487 memset(param, 0, TW_SECTOR_SIZE);
adam radford75913d92006-03-15 12:43:19 -0800488 param->table_id = cpu_to_le16(TW_TIMEKEEP_TABLE | 0x8000); /* Controller time keep table */
489 param->parameter_id = cpu_to_le16(0x3); /* SchedulerTime */
490 param->parameter_size_bytes = cpu_to_le16(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 /* Convert system time in UTC to local time seconds since last
493 Sunday 12:00AM */
494 do_gettimeofday(&utc);
495 local_time = (u32)(utc.tv_sec - (sys_tz.tz_minuteswest * 60));
496 schedulertime = local_time - (3 * 86400);
adam radford75913d92006-03-15 12:43:19 -0800497 schedulertime = cpu_to_le32(schedulertime % 604800);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 memcpy(param->data, &schedulertime, sizeof(u32));
500
501 /* Mark internal command */
502 tw_dev->srb[request_id] = NULL;
503
504 /* Now post the command */
505 twa_post_command_packet(tw_dev, request_id, 1);
506} /* End twa_aen_sync_time() */
507
508/* This function will allocate memory and check if it is correctly aligned */
509static int twa_allocate_memory(TW_Device_Extension *tw_dev, int size, int which)
510{
511 int i;
512 dma_addr_t dma_handle;
513 unsigned long *cpu_addr;
514 int retval = 1;
515
516 cpu_addr = pci_alloc_consistent(tw_dev->tw_pci_dev, size*TW_Q_LENGTH, &dma_handle);
517 if (!cpu_addr) {
518 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x5, "Memory allocation failed");
519 goto out;
520 }
521
522 if ((unsigned long)cpu_addr % (TW_ALIGNMENT_9000)) {
523 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x6, "Failed to allocate correctly aligned memory");
524 pci_free_consistent(tw_dev->tw_pci_dev, size*TW_Q_LENGTH, cpu_addr, dma_handle);
525 goto out;
526 }
527
528 memset(cpu_addr, 0, size*TW_Q_LENGTH);
529
530 for (i = 0; i < TW_Q_LENGTH; i++) {
531 switch(which) {
532 case 0:
533 tw_dev->command_packet_phys[i] = dma_handle+(i*size);
534 tw_dev->command_packet_virt[i] = (TW_Command_Full *)((unsigned char *)cpu_addr + (i*size));
535 break;
536 case 1:
537 tw_dev->generic_buffer_phys[i] = dma_handle+(i*size);
538 tw_dev->generic_buffer_virt[i] = (unsigned long *)((unsigned char *)cpu_addr + (i*size));
539 break;
540 }
541 }
542 retval = 0;
543out:
544 return retval;
545} /* End twa_allocate_memory() */
546
547/* This function will check the status register for unexpected bits */
548static int twa_check_bits(u32 status_reg_value)
549{
550 int retval = 1;
551
552 if ((status_reg_value & TW_STATUS_EXPECTED_BITS) != TW_STATUS_EXPECTED_BITS)
553 goto out;
554 if ((status_reg_value & TW_STATUS_UNEXPECTED_BITS) != 0)
555 goto out;
556
557 retval = 0;
558out:
559 return retval;
560} /* End twa_check_bits() */
561
562/* This function will check the srl and decide if we are compatible */
563static int twa_check_srl(TW_Device_Extension *tw_dev, int *flashed)
564{
565 int retval = 1;
566 unsigned short fw_on_ctlr_srl = 0, fw_on_ctlr_arch_id = 0;
567 unsigned short fw_on_ctlr_branch = 0, fw_on_ctlr_build = 0;
568 u32 init_connect_result = 0;
569
570 if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
571 TW_EXTENDED_INIT_CONNECT, TW_CURRENT_DRIVER_SRL,
572 TW_9000_ARCH_ID, TW_CURRENT_DRIVER_BRANCH,
573 TW_CURRENT_DRIVER_BUILD, &fw_on_ctlr_srl,
574 &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
575 &fw_on_ctlr_build, &init_connect_result)) {
576 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x7, "Initconnection failed while checking SRL");
577 goto out;
578 }
579
adam radford4039c302006-10-26 18:01:06 -0700580 tw_dev->tw_compat_info.working_srl = fw_on_ctlr_srl;
581 tw_dev->tw_compat_info.working_branch = fw_on_ctlr_branch;
582 tw_dev->tw_compat_info.working_build = fw_on_ctlr_build;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 /* Try base mode compatibility */
585 if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
586 if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
587 TW_EXTENDED_INIT_CONNECT,
588 TW_BASE_FW_SRL, TW_9000_ARCH_ID,
589 TW_BASE_FW_BRANCH, TW_BASE_FW_BUILD,
590 &fw_on_ctlr_srl, &fw_on_ctlr_arch_id,
591 &fw_on_ctlr_branch, &fw_on_ctlr_build,
592 &init_connect_result)) {
593 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xa, "Initconnection (base mode) failed while checking SRL");
594 goto out;
595 }
596 if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
597 if (TW_CURRENT_DRIVER_SRL > fw_on_ctlr_srl) {
598 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x32, "Firmware and driver incompatibility: please upgrade firmware");
599 } else {
600 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x33, "Firmware and driver incompatibility: please upgrade driver");
601 }
602 goto out;
603 }
adam radford4039c302006-10-26 18:01:06 -0700604 tw_dev->tw_compat_info.working_srl = TW_BASE_FW_SRL;
605 tw_dev->tw_compat_info.working_branch = TW_BASE_FW_BRANCH;
606 tw_dev->tw_compat_info.working_build = TW_BASE_FW_BUILD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
adam radford4039c302006-10-26 18:01:06 -0700608
609 /* Load rest of compatibility struct */
Rickard Strandqvist2195d962014-07-27 17:11:18 +0200610 strlcpy(tw_dev->tw_compat_info.driver_version, TW_DRIVER_VERSION,
611 sizeof(tw_dev->tw_compat_info.driver_version));
adam radford4039c302006-10-26 18:01:06 -0700612 tw_dev->tw_compat_info.driver_srl_high = TW_CURRENT_DRIVER_SRL;
613 tw_dev->tw_compat_info.driver_branch_high = TW_CURRENT_DRIVER_BRANCH;
614 tw_dev->tw_compat_info.driver_build_high = TW_CURRENT_DRIVER_BUILD;
615 tw_dev->tw_compat_info.driver_srl_low = TW_BASE_FW_SRL;
616 tw_dev->tw_compat_info.driver_branch_low = TW_BASE_FW_BRANCH;
617 tw_dev->tw_compat_info.driver_build_low = TW_BASE_FW_BUILD;
618 tw_dev->tw_compat_info.fw_on_ctlr_srl = fw_on_ctlr_srl;
619 tw_dev->tw_compat_info.fw_on_ctlr_branch = fw_on_ctlr_branch;
620 tw_dev->tw_compat_info.fw_on_ctlr_build = fw_on_ctlr_build;
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 retval = 0;
623out:
624 return retval;
625} /* End twa_check_srl() */
626
627/* This function handles ioctl for the character device */
Arnd Bergmannf4927c42010-04-27 00:24:01 +0200628static long twa_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Al Viro496ad9a2013-01-23 17:07:38 -0500630 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 long timeout;
632 unsigned long *cpu_addr, data_buffer_length_adjusted = 0, flags = 0;
633 dma_addr_t dma_handle;
634 int request_id = 0;
635 unsigned int sequence_id = 0;
636 unsigned char event_index, start_index;
637 TW_Ioctl_Driver_Command driver_command;
638 TW_Ioctl_Buf_Apache *tw_ioctl;
639 TW_Lock *tw_lock;
640 TW_Command_Full *full_command_packet;
641 TW_Compatibility_Info *tw_compat_info;
642 TW_Event *event;
643 struct timeval current_time;
644 u32 current_time_ms;
645 TW_Device_Extension *tw_dev = twa_device_extension_list[iminor(inode)];
646 int retval = TW_IOCTL_ERROR_OS_EFAULT;
647 void __user *argp = (void __user *)arg;
648
Arnd Bergmannc45d15d2010-06-02 14:28:52 +0200649 mutex_lock(&twa_chrdev_mutex);
Arnd Bergmannf4927c42010-04-27 00:24:01 +0200650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 /* Only let one of these through at a time */
Jes Sorensena12e25bd2006-01-11 08:39:45 -0500652 if (mutex_lock_interruptible(&tw_dev->ioctl_lock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 retval = TW_IOCTL_ERROR_OS_EINTR;
654 goto out;
655 }
656
657 /* First copy down the driver command */
658 if (copy_from_user(&driver_command, argp, sizeof(TW_Ioctl_Driver_Command)))
659 goto out2;
660
661 /* Check data buffer size */
adam radford4039c302006-10-26 18:01:06 -0700662 if (driver_command.buffer_length > TW_MAX_SECTORS * 2048) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 retval = TW_IOCTL_ERROR_OS_EINVAL;
664 goto out2;
665 }
666
667 /* Hardware can only do multiple of 512 byte transfers */
668 data_buffer_length_adjusted = (driver_command.buffer_length + 511) & ~511;
669
670 /* Now allocate ioctl buf memory */
671 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);
672 if (!cpu_addr) {
673 retval = TW_IOCTL_ERROR_OS_ENOMEM;
674 goto out2;
675 }
676
677 tw_ioctl = (TW_Ioctl_Buf_Apache *)cpu_addr;
678
679 /* Now copy down the entire ioctl */
680 if (copy_from_user(tw_ioctl, argp, driver_command.buffer_length + sizeof(TW_Ioctl_Buf_Apache) - 1))
681 goto out3;
682
683 /* See which ioctl we are doing */
684 switch (cmd) {
685 case TW_IOCTL_FIRMWARE_PASS_THROUGH:
686 spin_lock_irqsave(tw_dev->host->host_lock, flags);
687 twa_get_request_id(tw_dev, &request_id);
688
689 /* Flag internal command */
690 tw_dev->srb[request_id] = NULL;
691
692 /* Flag chrdev ioctl */
693 tw_dev->chrdev_request_id = request_id;
694
695 full_command_packet = &tw_ioctl->firmware_command;
696
697 /* Load request id and sglist for both command types */
adam radford0e78d152007-07-20 15:28:28 -0700698 twa_load_sgl(tw_dev, full_command_packet, request_id, dma_handle, data_buffer_length_adjusted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 memcpy(tw_dev->command_packet_virt[request_id], &(tw_ioctl->firmware_command), sizeof(TW_Command_Full));
701
702 /* Now post the command packet to the controller */
703 twa_post_command_packet(tw_dev, request_id, 1);
704 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
705
706 timeout = TW_IOCTL_CHRDEV_TIMEOUT*HZ;
707
708 /* Now wait for command to complete */
709 timeout = wait_event_timeout(tw_dev->ioctl_wqueue, tw_dev->chrdev_request_id == TW_IOCTL_CHRDEV_FREE, timeout);
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 /* We timed out, and didn't get an interrupt */
712 if (tw_dev->chrdev_request_id != TW_IOCTL_CHRDEV_FREE) {
713 /* Now we need to reset the board */
714 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 -0700715 tw_dev->host->host_no, TW_DRIVER, 0x37,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 cmd);
717 retval = TW_IOCTL_ERROR_OS_EIO;
adam radford0e78d152007-07-20 15:28:28 -0700718 twa_reset_device_extension(tw_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 goto out3;
720 }
721
722 /* Now copy in the command packet response */
723 memcpy(&(tw_ioctl->firmware_command), tw_dev->command_packet_virt[request_id], sizeof(TW_Command_Full));
724
725 /* Now complete the io */
726 spin_lock_irqsave(tw_dev->host->host_lock, flags);
727 tw_dev->posted_request_count--;
728 tw_dev->state[request_id] = TW_S_COMPLETED;
729 twa_free_request_id(tw_dev, request_id);
730 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
731 break;
732 case TW_IOCTL_GET_COMPATIBILITY_INFO:
733 tw_ioctl->driver_command.status = 0;
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400734 /* Copy compatibility struct into ioctl data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 tw_compat_info = (TW_Compatibility_Info *)tw_ioctl->data_buffer;
adam radford4039c302006-10-26 18:01:06 -0700736 memcpy(tw_compat_info, &tw_dev->tw_compat_info, sizeof(TW_Compatibility_Info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 break;
738 case TW_IOCTL_GET_LAST_EVENT:
739 if (tw_dev->event_queue_wrapped) {
740 if (tw_dev->aen_clobber) {
741 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
742 tw_dev->aen_clobber = 0;
743 } else
744 tw_ioctl->driver_command.status = 0;
745 } else {
746 if (!tw_dev->error_index) {
747 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
748 break;
749 }
750 tw_ioctl->driver_command.status = 0;
751 }
752 event_index = (tw_dev->error_index - 1 + TW_Q_LENGTH) % TW_Q_LENGTH;
753 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
754 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
755 break;
756 case TW_IOCTL_GET_FIRST_EVENT:
757 if (tw_dev->event_queue_wrapped) {
758 if (tw_dev->aen_clobber) {
759 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
760 tw_dev->aen_clobber = 0;
761 } else
762 tw_ioctl->driver_command.status = 0;
763 event_index = tw_dev->error_index;
764 } else {
765 if (!tw_dev->error_index) {
766 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
767 break;
768 }
769 tw_ioctl->driver_command.status = 0;
770 event_index = 0;
771 }
772 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
773 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
774 break;
775 case TW_IOCTL_GET_NEXT_EVENT:
776 event = (TW_Event *)tw_ioctl->data_buffer;
777 sequence_id = event->sequence_id;
778 tw_ioctl->driver_command.status = 0;
779
780 if (tw_dev->event_queue_wrapped) {
781 if (tw_dev->aen_clobber) {
782 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
783 tw_dev->aen_clobber = 0;
784 }
785 start_index = tw_dev->error_index;
786 } else {
787 if (!tw_dev->error_index) {
788 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
789 break;
790 }
791 start_index = 0;
792 }
793 event_index = (start_index + sequence_id - tw_dev->event_queue[start_index]->sequence_id + 1) % TW_Q_LENGTH;
794
795 if (!(tw_dev->event_queue[event_index]->sequence_id > sequence_id)) {
796 if (tw_ioctl->driver_command.status == TW_IOCTL_ERROR_STATUS_AEN_CLOBBER)
797 tw_dev->aen_clobber = 1;
798 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
799 break;
800 }
801 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
802 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
803 break;
804 case TW_IOCTL_GET_PREVIOUS_EVENT:
805 event = (TW_Event *)tw_ioctl->data_buffer;
806 sequence_id = event->sequence_id;
807 tw_ioctl->driver_command.status = 0;
808
809 if (tw_dev->event_queue_wrapped) {
810 if (tw_dev->aen_clobber) {
811 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
812 tw_dev->aen_clobber = 0;
813 }
814 start_index = tw_dev->error_index;
815 } else {
816 if (!tw_dev->error_index) {
817 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
818 break;
819 }
820 start_index = 0;
821 }
822 event_index = (start_index + sequence_id - tw_dev->event_queue[start_index]->sequence_id - 1) % TW_Q_LENGTH;
823
824 if (!(tw_dev->event_queue[event_index]->sequence_id < sequence_id)) {
825 if (tw_ioctl->driver_command.status == TW_IOCTL_ERROR_STATUS_AEN_CLOBBER)
826 tw_dev->aen_clobber = 1;
827 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
828 break;
829 }
830 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
831 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
832 break;
833 case TW_IOCTL_GET_LOCK:
834 tw_lock = (TW_Lock *)tw_ioctl->data_buffer;
835 do_gettimeofday(&current_time);
836 current_time_ms = (current_time.tv_sec * 1000) + (current_time.tv_usec / 1000);
837
838 if ((tw_lock->force_flag == 1) || (tw_dev->ioctl_sem_lock == 0) || (current_time_ms >= tw_dev->ioctl_msec)) {
839 tw_dev->ioctl_sem_lock = 1;
840 tw_dev->ioctl_msec = current_time_ms + tw_lock->timeout_msec;
841 tw_ioctl->driver_command.status = 0;
842 tw_lock->time_remaining_msec = tw_lock->timeout_msec;
843 } else {
844 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_LOCKED;
845 tw_lock->time_remaining_msec = tw_dev->ioctl_msec - current_time_ms;
846 }
847 break;
848 case TW_IOCTL_RELEASE_LOCK:
849 if (tw_dev->ioctl_sem_lock == 1) {
850 tw_dev->ioctl_sem_lock = 0;
851 tw_ioctl->driver_command.status = 0;
852 } else {
853 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NOT_LOCKED;
854 }
855 break;
856 default:
857 retval = TW_IOCTL_ERROR_OS_ENOTTY;
858 goto out3;
859 }
860
861 /* Now copy the entire response to userspace */
862 if (copy_to_user(argp, tw_ioctl, sizeof(TW_Ioctl_Buf_Apache) + driver_command.buffer_length - 1) == 0)
863 retval = 0;
864out3:
865 /* Now free ioctl buf memory */
866 dma_free_coherent(&tw_dev->tw_pci_dev->dev, data_buffer_length_adjusted+sizeof(TW_Ioctl_Buf_Apache) - 1, cpu_addr, dma_handle);
867out2:
Jes Sorensena12e25bd2006-01-11 08:39:45 -0500868 mutex_unlock(&tw_dev->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869out:
Arnd Bergmannc45d15d2010-06-02 14:28:52 +0200870 mutex_unlock(&twa_chrdev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return retval;
872} /* End twa_chrdev_ioctl() */
873
874/* This function handles open for the character device */
Jonathan Corbetf2b98572008-05-18 15:32:43 -0600875/* NOTE that this function will race with remove. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876static int twa_chrdev_open(struct inode *inode, struct file *file)
877{
878 unsigned int minor_number;
879 int retval = TW_IOCTL_ERROR_OS_ENODEV;
880
881 minor_number = iminor(inode);
882 if (minor_number >= twa_device_extension_count)
883 goto out;
884 retval = 0;
885out:
886 return retval;
887} /* End twa_chrdev_open() */
888
889/* This function will print readable messages from status register errors */
890static int twa_decode_bits(TW_Device_Extension *tw_dev, u32 status_reg_value)
891{
892 int retval = 1;
893
894 /* Check for various error conditions and handle them appropriately */
895 if (status_reg_value & TW_STATUS_PCI_PARITY_ERROR) {
896 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xc, "PCI Parity Error: clearing");
897 writel(TW_CONTROL_CLEAR_PARITY_ERROR, TW_CONTROL_REG_ADDR(tw_dev));
898 }
899
900 if (status_reg_value & TW_STATUS_PCI_ABORT) {
901 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xd, "PCI Abort: clearing");
902 writel(TW_CONTROL_CLEAR_PCI_ABORT, TW_CONTROL_REG_ADDR(tw_dev));
903 pci_write_config_word(tw_dev->tw_pci_dev, PCI_STATUS, TW_PCI_CLEAR_PCI_ABORT);
904 }
905
906 if (status_reg_value & TW_STATUS_QUEUE_ERROR) {
adam radford0e78d152007-07-20 15:28:28 -0700907 if (((tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9650SE) &&
908 (tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9690SA)) ||
909 (!test_bit(TW_IN_RESET, &tw_dev->flags)))
adam radford4039c302006-10-26 18:01:06 -0700910 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xe, "Controller Queue Error: clearing");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 writel(TW_CONTROL_CLEAR_QUEUE_ERROR, TW_CONTROL_REG_ADDR(tw_dev));
912 }
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (status_reg_value & TW_STATUS_MICROCONTROLLER_ERROR) {
915 if (tw_dev->reset_print == 0) {
916 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x10, "Microcontroller Error: clearing");
917 tw_dev->reset_print = 1;
918 }
919 goto out;
920 }
921 retval = 0;
922out:
923 return retval;
924} /* End twa_decode_bits() */
925
926/* This function will empty the response queue */
927static int twa_empty_response_queue(TW_Device_Extension *tw_dev)
928{
929 u32 status_reg_value, response_que_value;
930 int count = 0, retval = 1;
931
932 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
933
934 while (((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) && (count < TW_MAX_RESPONSE_DRAIN)) {
935 response_que_value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
936 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
937 count++;
938 }
939 if (count == TW_MAX_RESPONSE_DRAIN)
940 goto out;
941
942 retval = 0;
943out:
944 return retval;
945} /* End twa_empty_response_queue() */
946
adam radford49bfd8d2005-09-21 17:20:14 -0700947/* This function will clear the pchip/response queue on 9550SX */
948static int twa_empty_response_queue_large(TW_Device_Extension *tw_dev)
949{
adam radford75913d92006-03-15 12:43:19 -0800950 u32 response_que_value = 0;
951 unsigned long before;
952 int retval = 1;
adam radford49bfd8d2005-09-21 17:20:14 -0700953
adam radford0e78d152007-07-20 15:28:28 -0700954 if (tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9000) {
adam radford75913d92006-03-15 12:43:19 -0800955 before = jiffies;
956 while ((response_que_value & TW_9550SX_DRAIN_COMPLETED) != TW_9550SX_DRAIN_COMPLETED) {
adam radford49bfd8d2005-09-21 17:20:14 -0700957 response_que_value = readl(TW_RESPONSE_QUEUE_REG_ADDR_LARGE(tw_dev));
adam radford4039c302006-10-26 18:01:06 -0700958 msleep(1);
adam radford75913d92006-03-15 12:43:19 -0800959 if (time_after(jiffies, before + HZ * 30))
adam radford49bfd8d2005-09-21 17:20:14 -0700960 goto out;
adam radford49bfd8d2005-09-21 17:20:14 -0700961 }
adam radford75913d92006-03-15 12:43:19 -0800962 /* P-chip settle time */
963 msleep(500);
adam radford49bfd8d2005-09-21 17:20:14 -0700964 retval = 0;
965 } else
966 retval = 0;
967out:
968 return retval;
969} /* End twa_empty_response_queue_large() */
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971/* This function passes sense keys from firmware to scsi layer */
972static int twa_fill_sense(TW_Device_Extension *tw_dev, int request_id, int copy_sense, int print_host)
973{
974 TW_Command_Full *full_command_packet;
975 unsigned short error;
976 int retval = 1;
977 char *error_str;
978
979 full_command_packet = tw_dev->command_packet_virt[request_id];
980
981 /* Check for embedded error string */
982 error_str = &(full_command_packet->header.err_specific_desc[strlen(full_command_packet->header.err_specific_desc) + 1]);
983
984 /* Don't print error for Logical unit not supported during rollcall */
adam radford75913d92006-03-15 12:43:19 -0800985 error = le16_to_cpu(full_command_packet->header.status_block.error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if ((error != TW_ERROR_LOGICAL_UNIT_NOT_SUPPORTED) && (error != TW_ERROR_UNIT_OFFLINE)) {
987 if (print_host)
988 printk(KERN_WARNING "3w-9xxx: scsi%d: ERROR: (0x%02X:0x%04X): %s:%s.\n",
989 tw_dev->host->host_no,
990 TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
991 full_command_packet->header.status_block.error,
992 error_str[0] == '\0' ?
993 twa_string_lookup(twa_error_table,
994 full_command_packet->header.status_block.error) : error_str,
995 full_command_packet->header.err_specific_desc);
996 else
997 printk(KERN_WARNING "3w-9xxx: ERROR: (0x%02X:0x%04X): %s:%s.\n",
998 TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
999 full_command_packet->header.status_block.error,
1000 error_str[0] == '\0' ?
1001 twa_string_lookup(twa_error_table,
1002 full_command_packet->header.status_block.error) : error_str,
1003 full_command_packet->header.err_specific_desc);
1004 }
1005
1006 if (copy_sense) {
1007 memcpy(tw_dev->srb[request_id]->sense_buffer, full_command_packet->header.sense_data, TW_SENSE_DATA_LENGTH);
1008 tw_dev->srb[request_id]->result = (full_command_packet->command.newcommand.status << 1);
1009 retval = TW_ISR_DONT_RESULT;
1010 goto out;
1011 }
1012 retval = 0;
1013out:
1014 return retval;
1015} /* End twa_fill_sense() */
1016
1017/* This function will free up device extension resources */
1018static void twa_free_device_extension(TW_Device_Extension *tw_dev)
1019{
1020 if (tw_dev->command_packet_virt[0])
1021 pci_free_consistent(tw_dev->tw_pci_dev,
1022 sizeof(TW_Command_Full)*TW_Q_LENGTH,
1023 tw_dev->command_packet_virt[0],
1024 tw_dev->command_packet_phys[0]);
1025
1026 if (tw_dev->generic_buffer_virt[0])
1027 pci_free_consistent(tw_dev->tw_pci_dev,
1028 TW_SECTOR_SIZE*TW_Q_LENGTH,
1029 tw_dev->generic_buffer_virt[0],
1030 tw_dev->generic_buffer_phys[0]);
1031
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001032 kfree(tw_dev->event_queue[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033} /* End twa_free_device_extension() */
1034
1035/* This function will free a request id */
1036static void twa_free_request_id(TW_Device_Extension *tw_dev, int request_id)
1037{
1038 tw_dev->free_queue[tw_dev->free_tail] = request_id;
1039 tw_dev->state[request_id] = TW_S_FINISHED;
1040 tw_dev->free_tail = (tw_dev->free_tail + 1) % TW_Q_LENGTH;
1041} /* End twa_free_request_id() */
1042
Alexey Dobriyan7f927fc2006-03-28 01:56:53 -08001043/* This function will get parameter table entries from the firmware */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044static void *twa_get_param(TW_Device_Extension *tw_dev, int request_id, int table_id, int parameter_id, int parameter_size_bytes)
1045{
1046 TW_Command_Full *full_command_packet;
1047 TW_Command *command_packet;
1048 TW_Param_Apache *param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 void *retval = NULL;
1050
1051 /* Setup the command packet */
1052 full_command_packet = tw_dev->command_packet_virt[request_id];
1053 memset(full_command_packet, 0, sizeof(TW_Command_Full));
1054 command_packet = &full_command_packet->command.oldcommand;
1055
1056 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_GET_PARAM);
1057 command_packet->size = TW_COMMAND_SIZE;
1058 command_packet->request_id = request_id;
adam radford75913d92006-03-15 12:43:19 -08001059 command_packet->byte6_offset.block_count = cpu_to_le16(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 /* Now setup the param */
1062 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
1063 memset(param, 0, TW_SECTOR_SIZE);
adam radford75913d92006-03-15 12:43:19 -08001064 param->table_id = cpu_to_le16(table_id | 0x8000);
1065 param->parameter_id = cpu_to_le16(parameter_id);
1066 param->parameter_size_bytes = cpu_to_le16(parameter_size_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
adam radford3dabec72008-07-22 16:47:40 -07001068 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 -08001069 command_packet->byte8_offset.param.sgl[0].length = cpu_to_le32(TW_SECTOR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 /* Post the command packet to the board */
1072 twa_post_command_packet(tw_dev, request_id, 1);
1073
1074 /* Poll for completion */
1075 if (twa_poll_response(tw_dev, request_id, 30))
1076 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x13, "No valid response during get param")
1077 else
1078 retval = (void *)&(param->data[0]);
1079
1080 tw_dev->posted_request_count--;
1081 tw_dev->state[request_id] = TW_S_INITIAL;
1082
1083 return retval;
1084} /* End twa_get_param() */
1085
1086/* This function will assign an available request id */
1087static void twa_get_request_id(TW_Device_Extension *tw_dev, int *request_id)
1088{
1089 *request_id = tw_dev->free_queue[tw_dev->free_head];
1090 tw_dev->free_head = (tw_dev->free_head + 1) % TW_Q_LENGTH;
1091 tw_dev->state[*request_id] = TW_S_STARTED;
1092} /* End twa_get_request_id() */
1093
1094/* This function will send an initconnection command to controller */
1095static int twa_initconnection(TW_Device_Extension *tw_dev, int message_credits,
1096 u32 set_features, unsigned short current_fw_srl,
1097 unsigned short current_fw_arch_id,
1098 unsigned short current_fw_branch,
1099 unsigned short current_fw_build,
1100 unsigned short *fw_on_ctlr_srl,
1101 unsigned short *fw_on_ctlr_arch_id,
1102 unsigned short *fw_on_ctlr_branch,
1103 unsigned short *fw_on_ctlr_build,
1104 u32 *init_connect_result)
1105{
1106 TW_Command_Full *full_command_packet;
1107 TW_Initconnect *tw_initconnect;
1108 int request_id = 0, retval = 1;
1109
1110 /* Initialize InitConnection command packet */
1111 full_command_packet = tw_dev->command_packet_virt[request_id];
1112 memset(full_command_packet, 0, sizeof(TW_Command_Full));
1113 full_command_packet->header.header_desc.size_header = 128;
1114
1115 tw_initconnect = (TW_Initconnect *)&full_command_packet->command.oldcommand;
1116 tw_initconnect->opcode__reserved = TW_OPRES_IN(0, TW_OP_INIT_CONNECTION);
1117 tw_initconnect->request_id = request_id;
adam radford75913d92006-03-15 12:43:19 -08001118 tw_initconnect->message_credits = cpu_to_le16(message_credits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 tw_initconnect->features = set_features;
1120
1121 /* Turn on 64-bit sgl support if we need to */
1122 tw_initconnect->features |= sizeof(dma_addr_t) > 4 ? 1 : 0;
1123
adam radford75913d92006-03-15 12:43:19 -08001124 tw_initconnect->features = cpu_to_le32(tw_initconnect->features);
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (set_features & TW_EXTENDED_INIT_CONNECT) {
1127 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE_EXTENDED;
adam radford75913d92006-03-15 12:43:19 -08001128 tw_initconnect->fw_srl = cpu_to_le16(current_fw_srl);
1129 tw_initconnect->fw_arch_id = cpu_to_le16(current_fw_arch_id);
1130 tw_initconnect->fw_branch = cpu_to_le16(current_fw_branch);
1131 tw_initconnect->fw_build = cpu_to_le16(current_fw_build);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 } else
1133 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE;
1134
1135 /* Send command packet to the board */
1136 twa_post_command_packet(tw_dev, request_id, 1);
1137
1138 /* Poll for completion */
1139 if (twa_poll_response(tw_dev, request_id, 30)) {
1140 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x15, "No valid response during init connection");
1141 } else {
1142 if (set_features & TW_EXTENDED_INIT_CONNECT) {
adam radford75913d92006-03-15 12:43:19 -08001143 *fw_on_ctlr_srl = le16_to_cpu(tw_initconnect->fw_srl);
1144 *fw_on_ctlr_arch_id = le16_to_cpu(tw_initconnect->fw_arch_id);
1145 *fw_on_ctlr_branch = le16_to_cpu(tw_initconnect->fw_branch);
1146 *fw_on_ctlr_build = le16_to_cpu(tw_initconnect->fw_build);
1147 *init_connect_result = le32_to_cpu(tw_initconnect->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
1149 retval = 0;
1150 }
1151
1152 tw_dev->posted_request_count--;
1153 tw_dev->state[request_id] = TW_S_INITIAL;
1154
1155 return retval;
1156} /* End twa_initconnection() */
1157
1158/* This function will initialize the fields of a device extension */
1159static int twa_initialize_device_extension(TW_Device_Extension *tw_dev)
1160{
1161 int i, retval = 1;
1162
1163 /* Initialize command packet buffers */
1164 if (twa_allocate_memory(tw_dev, sizeof(TW_Command_Full), 0)) {
1165 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x16, "Command packet memory allocation failed");
1166 goto out;
1167 }
1168
1169 /* Initialize generic buffer */
1170 if (twa_allocate_memory(tw_dev, TW_SECTOR_SIZE, 1)) {
1171 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x17, "Generic memory allocation failed");
1172 goto out;
1173 }
1174
1175 /* Allocate event info space */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001176 tw_dev->event_queue[0] = kcalloc(TW_Q_LENGTH, sizeof(TW_Event), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (!tw_dev->event_queue[0]) {
1178 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x18, "Event info memory allocation failed");
1179 goto out;
1180 }
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 for (i = 0; i < TW_Q_LENGTH; i++) {
1184 tw_dev->event_queue[i] = (TW_Event *)((unsigned char *)tw_dev->event_queue[0] + (i * sizeof(TW_Event)));
1185 tw_dev->free_queue[i] = i;
1186 tw_dev->state[i] = TW_S_INITIAL;
1187 }
1188
1189 tw_dev->pending_head = TW_Q_START;
1190 tw_dev->pending_tail = TW_Q_START;
1191 tw_dev->free_head = TW_Q_START;
1192 tw_dev->free_tail = TW_Q_START;
1193 tw_dev->error_sequence_id = 1;
1194 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1195
Jes Sorensena12e25bd2006-01-11 08:39:45 -05001196 mutex_init(&tw_dev->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 init_waitqueue_head(&tw_dev->ioctl_wqueue);
1198
1199 retval = 0;
1200out:
1201 return retval;
1202} /* End twa_initialize_device_extension() */
1203
1204/* This function is the interrupt service routine */
David Howells7d12e782006-10-05 14:55:46 +01001205static irqreturn_t twa_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
1207 int request_id, error = 0;
1208 u32 status_reg_value;
1209 TW_Response_Queue response_que;
1210 TW_Command_Full *full_command_packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 TW_Device_Extension *tw_dev = (TW_Device_Extension *)dev_instance;
1212 int handled = 0;
1213
1214 /* Get the per adapter lock */
1215 spin_lock(tw_dev->host->host_lock);
1216
1217 /* Read the registers */
1218 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1219
1220 /* Check if this is our interrupt, otherwise bail */
1221 if (!(status_reg_value & TW_STATUS_VALID_INTERRUPT))
1222 goto twa_interrupt_bail;
1223
1224 handled = 1;
1225
adam radford4039c302006-10-26 18:01:06 -07001226 /* If we are resetting, bail */
1227 if (test_bit(TW_IN_RESET, &tw_dev->flags))
1228 goto twa_interrupt_bail;
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 /* Check controller for errors */
1231 if (twa_check_bits(status_reg_value)) {
1232 if (twa_decode_bits(tw_dev, status_reg_value)) {
1233 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1234 goto twa_interrupt_bail;
1235 }
1236 }
1237
1238 /* Handle host interrupt */
1239 if (status_reg_value & TW_STATUS_HOST_INTERRUPT)
1240 TW_CLEAR_HOST_INTERRUPT(tw_dev);
1241
1242 /* Handle attention interrupt */
1243 if (status_reg_value & TW_STATUS_ATTENTION_INTERRUPT) {
1244 TW_CLEAR_ATTENTION_INTERRUPT(tw_dev);
1245 if (!(test_and_set_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags))) {
1246 twa_get_request_id(tw_dev, &request_id);
1247
1248 error = twa_aen_read_queue(tw_dev, request_id);
1249 if (error) {
1250 tw_dev->state[request_id] = TW_S_COMPLETED;
1251 twa_free_request_id(tw_dev, request_id);
1252 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
1253 }
1254 }
1255 }
1256
1257 /* Handle command interrupt */
1258 if (status_reg_value & TW_STATUS_COMMAND_INTERRUPT) {
1259 TW_MASK_COMMAND_INTERRUPT(tw_dev);
1260 /* Drain as many pending commands as we can */
1261 while (tw_dev->pending_request_count > 0) {
1262 request_id = tw_dev->pending_queue[tw_dev->pending_head];
1263 if (tw_dev->state[request_id] != TW_S_PENDING) {
1264 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x19, "Found request id that wasn't pending");
1265 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1266 goto twa_interrupt_bail;
1267 }
1268 if (twa_post_command_packet(tw_dev, request_id, 1)==0) {
1269 tw_dev->pending_head = (tw_dev->pending_head + 1) % TW_Q_LENGTH;
1270 tw_dev->pending_request_count--;
1271 } else {
1272 /* If we get here, we will continue re-posting on the next command interrupt */
1273 break;
1274 }
1275 }
1276 }
1277
1278 /* Handle response interrupt */
1279 if (status_reg_value & TW_STATUS_RESPONSE_INTERRUPT) {
1280
1281 /* Drain the response queue from the board */
1282 while ((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) {
1283 /* Complete the response */
1284 response_que.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
1285 request_id = TW_RESID_OUT(response_que.response_id);
1286 full_command_packet = tw_dev->command_packet_virt[request_id];
1287 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 /* Check for command packet errors */
1289 if (full_command_packet->command.newcommand.status != 0) {
Harvey Harrison9bcf0912008-05-22 15:45:07 -07001290 if (tw_dev->srb[request_id] != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 error = twa_fill_sense(tw_dev, request_id, 1, 1);
1292 } else {
1293 /* Skip ioctl error prints */
1294 if (request_id != tw_dev->chrdev_request_id) {
1295 error = twa_fill_sense(tw_dev, request_id, 0, 1);
1296 }
1297 }
1298 }
1299
1300 /* Check for correct state */
1301 if (tw_dev->state[request_id] != TW_S_POSTED) {
Harvey Harrison9bcf0912008-05-22 15:45:07 -07001302 if (tw_dev->srb[request_id] != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1a, "Received a request id that wasn't posted");
1304 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1305 goto twa_interrupt_bail;
1306 }
1307 }
1308
1309 /* Check for internal command completion */
Harvey Harrison9bcf0912008-05-22 15:45:07 -07001310 if (tw_dev->srb[request_id] == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (request_id != tw_dev->chrdev_request_id) {
1312 if (twa_aen_complete(tw_dev, request_id))
1313 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1b, "Error completing AEN during attention interrupt");
1314 } else {
1315 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1316 wake_up(&tw_dev->ioctl_wqueue);
1317 }
1318 } else {
FUJITA Tomonori0debe012007-05-26 02:33:31 +09001319 struct scsi_cmnd *cmd;
1320
1321 cmd = tw_dev->srb[request_id];
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 twa_scsiop_execute_scsi_complete(tw_dev, request_id);
1324 /* If no error command was a success */
1325 if (error == 0) {
FUJITA Tomonori0debe012007-05-26 02:33:31 +09001326 cmd->result = (DID_OK << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
1328
1329 /* If error, command failed */
1330 if (error == 1) {
1331 /* Ask for a host reset */
FUJITA Tomonori0debe012007-05-26 02:33:31 +09001332 cmd->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 }
1334
1335 /* Report residual bytes for single sgl */
FUJITA Tomonori0debe012007-05-26 02:33:31 +09001336 if ((scsi_sg_count(cmd) <= 1) && (full_command_packet->command.newcommand.status == 0)) {
1337 if (full_command_packet->command.newcommand.sg_list[0].length < scsi_bufflen(tw_dev->srb[request_id]))
1338 scsi_set_resid(cmd, scsi_bufflen(cmd) - full_command_packet->command.newcommand.sg_list[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
1340
1341 /* Now complete the io */
Christoph Hellwig118c8552015-04-23 09:48:51 +02001342 scsi_dma_unmap(cmd);
1343 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 tw_dev->state[request_id] = TW_S_COMPLETED;
1345 twa_free_request_id(tw_dev, request_id);
1346 tw_dev->posted_request_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
1348
1349 /* Check for valid status after each drain */
1350 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1351 if (twa_check_bits(status_reg_value)) {
1352 if (twa_decode_bits(tw_dev, status_reg_value)) {
1353 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1354 goto twa_interrupt_bail;
1355 }
1356 }
1357 }
1358 }
1359
1360twa_interrupt_bail:
1361 spin_unlock(tw_dev->host->host_lock);
1362 return IRQ_RETVAL(handled);
1363} /* End twa_interrupt() */
1364
1365/* This function will load the request id and various sgls for ioctls */
adam radford0e78d152007-07-20 15:28:28 -07001366static 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 -07001367{
1368 TW_Command *oldcommand;
1369 TW_Command_Apache *newcommand;
1370 TW_SG_Entry *sgl;
adam radford0e78d152007-07-20 15:28:28 -07001371 unsigned int pae = 0;
1372
1373 if ((sizeof(long) < 8) && (sizeof(dma_addr_t) > 4))
1374 pae = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
1377 newcommand = &full_command_packet->command.newcommand;
adam radford4039c302006-10-26 18:01:06 -07001378 newcommand->request_id__lunl =
1379 cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->request_id__lunl), request_id));
adam radford53ca3532009-12-10 11:53:31 -08001380 if (length) {
1381 newcommand->sg_list[0].address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1);
1382 newcommand->sg_list[0].length = cpu_to_le32(length);
1383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 newcommand->sgl_entries__lunh =
adam radford53ca3532009-12-10 11:53:31 -08001385 cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->sgl_entries__lunh), length ? 1 : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 } else {
1387 oldcommand = &full_command_packet->command.oldcommand;
1388 oldcommand->request_id = request_id;
1389
1390 if (TW_SGL_OUT(oldcommand->opcode__sgloffset)) {
1391 /* Load the sg list */
adam radford0e78d152007-07-20 15:28:28 -07001392 if (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)
1393 sgl = (TW_SG_Entry *)((u32 *)oldcommand+oldcommand->size - (sizeof(TW_SG_Entry)/4) + pae);
1394 else
1395 sgl = (TW_SG_Entry *)((u32 *)oldcommand+TW_SGL_OUT(oldcommand->opcode__sgloffset));
adam radford75913d92006-03-15 12:43:19 -08001396 sgl->address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1);
1397 sgl->length = cpu_to_le32(length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
adam radford0e78d152007-07-20 15:28:28 -07001399 oldcommand->size += pae;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
1401 }
1402} /* End twa_load_sgl() */
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404/* This function will poll for a response interrupt of a request */
1405static int twa_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds)
1406{
1407 int retval = 1, found = 0, response_request_id;
1408 TW_Response_Queue response_queue;
1409 TW_Command_Full *full_command_packet = tw_dev->command_packet_virt[request_id];
1410
1411 if (twa_poll_status_gone(tw_dev, TW_STATUS_RESPONSE_QUEUE_EMPTY, seconds) == 0) {
1412 response_queue.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
1413 response_request_id = TW_RESID_OUT(response_queue.response_id);
1414 if (request_id != response_request_id) {
1415 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1e, "Found unexpected request id while polling for response");
1416 goto out;
1417 }
1418 if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
1419 if (full_command_packet->command.newcommand.status != 0) {
1420 /* bad response */
1421 twa_fill_sense(tw_dev, request_id, 0, 0);
1422 goto out;
1423 }
1424 found = 1;
1425 } else {
1426 if (full_command_packet->command.oldcommand.status != 0) {
1427 /* bad response */
1428 twa_fill_sense(tw_dev, request_id, 0, 0);
1429 goto out;
1430 }
1431 found = 1;
1432 }
1433 }
1434
1435 if (found)
1436 retval = 0;
1437out:
1438 return retval;
1439} /* End twa_poll_response() */
1440
1441/* This function will poll the status register for a flag */
1442static int twa_poll_status(TW_Device_Extension *tw_dev, u32 flag, int seconds)
1443{
1444 u32 status_reg_value;
1445 unsigned long before;
1446 int retval = 1;
1447
1448 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1449 before = jiffies;
1450
1451 if (twa_check_bits(status_reg_value))
1452 twa_decode_bits(tw_dev, status_reg_value);
1453
1454 while ((status_reg_value & flag) != flag) {
1455 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1456
1457 if (twa_check_bits(status_reg_value))
1458 twa_decode_bits(tw_dev, status_reg_value);
1459
1460 if (time_after(jiffies, before + HZ * seconds))
1461 goto out;
1462
1463 msleep(50);
1464 }
1465 retval = 0;
1466out:
1467 return retval;
1468} /* End twa_poll_status() */
1469
1470/* This function will poll the status register for disappearance of a flag */
1471static int twa_poll_status_gone(TW_Device_Extension *tw_dev, u32 flag, int seconds)
1472{
1473 u32 status_reg_value;
1474 unsigned long before;
1475 int retval = 1;
1476
1477 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1478 before = jiffies;
1479
1480 if (twa_check_bits(status_reg_value))
1481 twa_decode_bits(tw_dev, status_reg_value);
1482
1483 while ((status_reg_value & flag) != 0) {
1484 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1485 if (twa_check_bits(status_reg_value))
1486 twa_decode_bits(tw_dev, status_reg_value);
1487
1488 if (time_after(jiffies, before + HZ * seconds))
1489 goto out;
1490
1491 msleep(50);
1492 }
1493 retval = 0;
1494out:
1495 return retval;
1496} /* End twa_poll_status_gone() */
1497
1498/* This function will attempt to post a command packet to the board */
1499static int twa_post_command_packet(TW_Device_Extension *tw_dev, int request_id, char internal)
1500{
1501 u32 status_reg_value;
1502 dma_addr_t command_que_value;
1503 int retval = 1;
1504
1505 command_que_value = tw_dev->command_packet_phys[request_id];
adam radford4039c302006-10-26 18:01:06 -07001506
1507 /* For 9650SE write low 4 bytes first */
adam radford0e78d152007-07-20 15:28:28 -07001508 if ((tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
1509 (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)) {
adam radford4039c302006-10-26 18:01:06 -07001510 command_que_value += TW_COMMAND_OFFSET;
1511 writel((u32)command_que_value, TW_COMMAND_QUEUE_REG_ADDR_LARGE(tw_dev));
1512 }
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1515
1516 if (twa_check_bits(status_reg_value))
1517 twa_decode_bits(tw_dev, status_reg_value);
1518
1519 if (((tw_dev->pending_request_count > 0) && (tw_dev->state[request_id] != TW_S_PENDING)) || (status_reg_value & TW_STATUS_COMMAND_QUEUE_FULL)) {
1520
1521 /* Only pend internal driver commands */
1522 if (!internal) {
1523 retval = SCSI_MLQUEUE_HOST_BUSY;
1524 goto out;
1525 }
1526
1527 /* Couldn't post the command packet, so we do it later */
1528 if (tw_dev->state[request_id] != TW_S_PENDING) {
1529 tw_dev->state[request_id] = TW_S_PENDING;
1530 tw_dev->pending_request_count++;
1531 if (tw_dev->pending_request_count > tw_dev->max_pending_request_count) {
1532 tw_dev->max_pending_request_count = tw_dev->pending_request_count;
1533 }
1534 tw_dev->pending_queue[tw_dev->pending_tail] = request_id;
1535 tw_dev->pending_tail = (tw_dev->pending_tail + 1) % TW_Q_LENGTH;
1536 }
1537 TW_UNMASK_COMMAND_INTERRUPT(tw_dev);
1538 goto out;
1539 } else {
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 /* Now write upper 4 bytes */
1543 writel((u32)((u64)command_que_value >> 32), TW_COMMAND_QUEUE_REG_ADDR_LARGE(tw_dev) + 0x4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 } else {
adam radford4039c302006-10-26 18:01:06 -07001545 if (sizeof(dma_addr_t) > 4) {
1546 command_que_value += TW_COMMAND_OFFSET;
1547 writel((u32)command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
1548 writel((u32)((u64)command_que_value >> 32), TW_COMMAND_QUEUE_REG_ADDR(tw_dev) + 0x4);
1549 } else {
1550 writel(TW_COMMAND_OFFSET + command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
1551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
1553 tw_dev->state[request_id] = TW_S_POSTED;
1554 tw_dev->posted_request_count++;
1555 if (tw_dev->posted_request_count > tw_dev->max_posted_request_count) {
1556 tw_dev->max_posted_request_count = tw_dev->posted_request_count;
1557 }
1558 }
1559 retval = 0;
1560out:
1561 return retval;
1562} /* End twa_post_command_packet() */
1563
1564/* This function will reset a device extension */
adam radford0e78d152007-07-20 15:28:28 -07001565static int twa_reset_device_extension(TW_Device_Extension *tw_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
1567 int i = 0;
1568 int retval = 1;
1569 unsigned long flags = 0;
1570
1571 set_bit(TW_IN_RESET, &tw_dev->flags);
1572 TW_DISABLE_INTERRUPTS(tw_dev);
1573 TW_MASK_COMMAND_INTERRUPT(tw_dev);
1574 spin_lock_irqsave(tw_dev->host->host_lock, flags);
1575
1576 /* Abort all requests that are in progress */
1577 for (i = 0; i < TW_Q_LENGTH; i++) {
1578 if ((tw_dev->state[i] != TW_S_FINISHED) &&
1579 (tw_dev->state[i] != TW_S_INITIAL) &&
1580 (tw_dev->state[i] != TW_S_COMPLETED)) {
1581 if (tw_dev->srb[i]) {
Christoph Hellwig118c8552015-04-23 09:48:51 +02001582 struct scsi_cmnd *cmd = tw_dev->srb[i];
1583
1584 cmd->result = (DID_RESET << 16);
1585 scsi_dma_unmap(cmd);
1586 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 }
1588 }
1589 }
1590
1591 /* Reset queues and counts */
1592 for (i = 0; i < TW_Q_LENGTH; i++) {
1593 tw_dev->free_queue[i] = i;
1594 tw_dev->state[i] = TW_S_INITIAL;
1595 }
1596 tw_dev->free_head = TW_Q_START;
1597 tw_dev->free_tail = TW_Q_START;
1598 tw_dev->posted_request_count = 0;
1599 tw_dev->pending_request_count = 0;
1600 tw_dev->pending_head = TW_Q_START;
1601 tw_dev->pending_tail = TW_Q_START;
1602 tw_dev->reset_print = 0;
1603
1604 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
1605
1606 if (twa_reset_sequence(tw_dev, 1))
1607 goto out;
1608
1609 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
adam radford4039c302006-10-26 18:01:06 -07001610 clear_bit(TW_IN_RESET, &tw_dev->flags);
1611 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 retval = 0;
1614out:
1615 return retval;
1616} /* End twa_reset_device_extension() */
1617
1618/* This function will reset a controller */
1619static int twa_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset)
1620{
1621 int tries = 0, retval = 1, flashed = 0, do_soft_reset = soft_reset;
1622
1623 while (tries < TW_MAX_RESET_TRIES) {
adam radford49bfd8d2005-09-21 17:20:14 -07001624 if (do_soft_reset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 TW_SOFT_RESET(tw_dev);
adam radford49bfd8d2005-09-21 17:20:14 -07001626 /* Clear pchip/response queue on 9550SX */
1627 if (twa_empty_response_queue_large(tw_dev)) {
1628 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x36, "Response queue (large) empty failed during reset sequence");
1629 do_soft_reset = 1;
1630 tries++;
1631 continue;
1632 }
1633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 /* Make sure controller is in a good state */
1636 if (twa_poll_status(tw_dev, TW_STATUS_MICROCONTROLLER_READY | (do_soft_reset == 1 ? TW_STATUS_ATTENTION_INTERRUPT : 0), 60)) {
1637 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1f, "Microcontroller not ready during reset sequence");
1638 do_soft_reset = 1;
1639 tries++;
1640 continue;
1641 }
1642
1643 /* Empty response queue */
1644 if (twa_empty_response_queue(tw_dev)) {
1645 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x20, "Response queue empty failed during reset sequence");
1646 do_soft_reset = 1;
1647 tries++;
1648 continue;
1649 }
1650
1651 flashed = 0;
1652
1653 /* Check for compatibility/flash */
1654 if (twa_check_srl(tw_dev, &flashed)) {
1655 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x21, "Compatibility check failed during reset sequence");
1656 do_soft_reset = 1;
1657 tries++;
1658 continue;
1659 } else {
1660 if (flashed) {
1661 tries++;
1662 continue;
1663 }
1664 }
1665
1666 /* Drain the AEN queue */
1667 if (twa_aen_drain_queue(tw_dev, soft_reset)) {
1668 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x22, "AEN drain failed during reset sequence");
1669 do_soft_reset = 1;
1670 tries++;
1671 continue;
1672 }
1673
1674 /* If we got here, controller is in a good state */
1675 retval = 0;
1676 goto out;
1677 }
1678out:
1679 return retval;
1680} /* End twa_reset_sequence() */
1681
1682/* This funciton returns unit geometry in cylinders/heads/sectors */
1683static int twa_scsi_biosparam(struct scsi_device *sdev, struct block_device *bdev, sector_t capacity, int geom[])
1684{
1685 int heads, sectors, cylinders;
1686 TW_Device_Extension *tw_dev;
1687
1688 tw_dev = (TW_Device_Extension *)sdev->host->hostdata;
1689
1690 if (capacity >= 0x200000) {
1691 heads = 255;
1692 sectors = 63;
1693 cylinders = sector_div(capacity, heads * sectors);
1694 } else {
1695 heads = 64;
1696 sectors = 32;
1697 cylinders = sector_div(capacity, heads * sectors);
1698 }
1699
1700 geom[0] = heads;
1701 geom[1] = sectors;
1702 geom[2] = cylinders;
1703
1704 return 0;
1705} /* End twa_scsi_biosparam() */
1706
1707/* This is the new scsi eh reset function */
1708static int twa_scsi_eh_reset(struct scsi_cmnd *SCpnt)
1709{
1710 TW_Device_Extension *tw_dev = NULL;
1711 int retval = FAILED;
1712
1713 tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 tw_dev->num_resets++;
1716
Jeff Garzik017560f2005-10-24 18:04:36 -04001717 sdev_printk(KERN_WARNING, SCpnt->device,
1718 "WARNING: (0x%02X:0x%04X): Command (0x%x) timed out, resetting card.\n",
1719 TW_DRIVER, 0x2c, SCpnt->cmnd[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
adam radford4039c302006-10-26 18:01:06 -07001721 /* Make sure we are not issuing an ioctl or resetting from ioctl */
1722 mutex_lock(&tw_dev->ioctl_lock);
1723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 /* Now reset the card and some of the device extension data */
adam radford0e78d152007-07-20 15:28:28 -07001725 if (twa_reset_device_extension(tw_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2b, "Controller reset failed during scsi host reset");
1727 goto out;
1728 }
1729
1730 retval = SUCCESS;
1731out:
adam radford4039c302006-10-26 18:01:06 -07001732 mutex_unlock(&tw_dev->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 return retval;
1734} /* End twa_scsi_eh_reset() */
1735
1736/* This is the main scsi queue function to handle scsi opcodes */
Jeff Garzikf2812332010-11-16 02:10:29 -05001737static int twa_scsi_queue_lck(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738{
1739 int request_id, retval;
1740 TW_Device_Extension *tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1741
adam radford4039c302006-10-26 18:01:06 -07001742 /* If we are resetting due to timed out ioctl, report as busy */
1743 if (test_bit(TW_IN_RESET, &tw_dev->flags)) {
1744 retval = SCSI_MLQUEUE_HOST_BUSY;
1745 goto out;
1746 }
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 /* Check if this FW supports luns */
adam radford4039c302006-10-26 18:01:06 -07001749 if ((SCpnt->device->lun != 0) && (tw_dev->tw_compat_info.working_srl < TW_FW_SRL_LUNS_SUPPORTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 SCpnt->result = (DID_BAD_TARGET << 16);
1751 done(SCpnt);
1752 retval = 0;
1753 goto out;
1754 }
1755
1756 /* Save done function into scsi_cmnd struct */
1757 SCpnt->scsi_done = done;
1758
1759 /* Get a free request id */
1760 twa_get_request_id(tw_dev, &request_id);
1761
1762 /* Save the scsi command for use by the ISR */
1763 tw_dev->srb[request_id] = SCpnt;
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 retval = twa_scsiop_execute_scsi(tw_dev, request_id, NULL, 0, NULL);
1766 switch (retval) {
1767 case SCSI_MLQUEUE_HOST_BUSY:
Christoph Hellwig118c8552015-04-23 09:48:51 +02001768 scsi_dma_unmap(SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 twa_free_request_id(tw_dev, request_id);
1770 break;
1771 case 1:
Christoph Hellwig118c8552015-04-23 09:48:51 +02001772 SCpnt->result = (DID_ERROR << 16);
1773 scsi_dma_unmap(SCpnt);
1774 done(SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 tw_dev->state[request_id] = TW_S_COMPLETED;
1776 twa_free_request_id(tw_dev, request_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 retval = 0;
1778 }
1779out:
1780 return retval;
1781} /* End twa_scsi_queue() */
1782
Jeff Garzikf2812332010-11-16 02:10:29 -05001783static DEF_SCSI_QCMD(twa_scsi_queue)
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785/* This function hands scsi cdb's to the firmware */
1786static int twa_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id, char *cdb, int use_sg, TW_SG_Entry *sglistarg)
1787{
1788 TW_Command_Full *full_command_packet;
1789 TW_Command_Apache *command_packet;
1790 u32 num_sectors = 0x0;
1791 int i, sg_count;
1792 struct scsi_cmnd *srb = NULL;
FUJITA Tomonori0debe012007-05-26 02:33:31 +09001793 struct scatterlist *sglist = NULL, *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 int retval = 1;
1795
1796 if (tw_dev->srb[request_id]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 srb = tw_dev->srb[request_id];
FUJITA Tomonori0debe012007-05-26 02:33:31 +09001798 if (scsi_sglist(srb))
1799 sglist = scsi_sglist(srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 }
1801
1802 /* Initialize command packet */
1803 full_command_packet = tw_dev->command_packet_virt[request_id];
1804 full_command_packet->header.header_desc.size_header = 128;
1805 full_command_packet->header.status_block.error = 0;
1806 full_command_packet->header.status_block.severity__reserved = 0;
1807
1808 command_packet = &full_command_packet->command.newcommand;
1809 command_packet->status = 0;
1810 command_packet->opcode__reserved = TW_OPRES_IN(0, TW_OP_EXECUTE_SCSI);
1811
1812 /* We forced 16 byte cdb use earlier */
1813 if (!cdb)
1814 memcpy(command_packet->cdb, srb->cmnd, TW_MAX_CDB_LEN);
1815 else
1816 memcpy(command_packet->cdb, cdb, TW_MAX_CDB_LEN);
1817
1818 if (srb) {
1819 command_packet->unit = srb->device->id;
1820 command_packet->request_id__lunl =
adam radford75913d92006-03-15 12:43:19 -08001821 cpu_to_le16(TW_REQ_LUN_IN(srb->device->lun, request_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 } else {
1823 command_packet->request_id__lunl =
adam radford75913d92006-03-15 12:43:19 -08001824 cpu_to_le16(TW_REQ_LUN_IN(0, request_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 command_packet->unit = 0;
1826 }
1827
1828 command_packet->sgl_offset = 16;
1829
1830 if (!sglistarg) {
1831 /* Map sglist from scsi layer to cmd packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
FUJITA Tomonori0debe012007-05-26 02:33:31 +09001833 if (scsi_sg_count(srb)) {
1834 if ((scsi_sg_count(srb) == 1) &&
1835 (scsi_bufflen(srb) < TW_MIN_SGL_LENGTH)) {
FUJITA Tomonori035f5e02008-03-09 13:44:37 +09001836 if (srb->sc_data_direction == DMA_TO_DEVICE ||
1837 srb->sc_data_direction == DMA_BIDIRECTIONAL)
1838 scsi_sg_copy_to_buffer(srb,
1839 tw_dev->generic_buffer_virt[request_id],
1840 TW_SECTOR_SIZE);
adam radford75913d92006-03-15 12:43:19 -08001841 command_packet->sg_list[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
1842 command_packet->sg_list[0].length = cpu_to_le32(TW_MIN_SGL_LENGTH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 } else {
Christoph Hellwig118c8552015-04-23 09:48:51 +02001844 sg_count = scsi_dma_map(srb);
1845 if (sg_count < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 goto out;
1847
FUJITA Tomonori0debe012007-05-26 02:33:31 +09001848 scsi_for_each_sg(srb, sg, sg_count, i) {
1849 command_packet->sg_list[i].address = TW_CPU_TO_SGL(sg_dma_address(sg));
1850 command_packet->sg_list[i].length = cpu_to_le32(sg_dma_len(sg));
adam radford75913d92006-03-15 12:43:19 -08001851 if (command_packet->sg_list[i].address & TW_CPU_TO_SGL(TW_ALIGNMENT_9000_SGL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2e, "Found unaligned sgl address during execute scsi");
1853 goto out;
1854 }
1855 }
1856 }
FUJITA Tomonori0debe012007-05-26 02:33:31 +09001857 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 -07001858 }
1859 } else {
1860 /* Internal cdb post */
1861 for (i = 0; i < use_sg; i++) {
adam radford75913d92006-03-15 12:43:19 -08001862 command_packet->sg_list[i].address = TW_CPU_TO_SGL(sglistarg[i].address);
1863 command_packet->sg_list[i].length = cpu_to_le32(sglistarg[i].length);
1864 if (command_packet->sg_list[i].address & TW_CPU_TO_SGL(TW_ALIGNMENT_9000_SGL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2f, "Found unaligned sgl address during internal post");
1866 goto out;
1867 }
1868 }
adam radford75913d92006-03-15 12:43:19 -08001869 command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN(0, use_sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 }
1871
1872 if (srb) {
1873 if (srb->cmnd[0] == READ_6 || srb->cmnd[0] == WRITE_6)
1874 num_sectors = (u32)srb->cmnd[4];
1875
1876 if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10)
1877 num_sectors = (u32)srb->cmnd[8] | ((u32)srb->cmnd[7] << 8);
1878 }
1879
1880 /* Update sector statistic */
1881 tw_dev->sector_count = num_sectors;
1882 if (tw_dev->sector_count > tw_dev->max_sector_count)
1883 tw_dev->max_sector_count = tw_dev->sector_count;
1884
1885 /* Update SG statistics */
1886 if (srb) {
FUJITA Tomonori0debe012007-05-26 02:33:31 +09001887 tw_dev->sgl_entries = scsi_sg_count(tw_dev->srb[request_id]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 if (tw_dev->sgl_entries > tw_dev->max_sgl_entries)
1889 tw_dev->max_sgl_entries = tw_dev->sgl_entries;
1890 }
1891
1892 /* Now post the command to the board */
1893 if (srb) {
1894 retval = twa_post_command_packet(tw_dev, request_id, 0);
1895 } else {
1896 twa_post_command_packet(tw_dev, request_id, 1);
1897 retval = 0;
1898 }
1899out:
1900 return retval;
1901} /* End twa_scsiop_execute_scsi() */
1902
1903/* This function completes an execute scsi operation */
1904static void twa_scsiop_execute_scsi_complete(TW_Device_Extension *tw_dev, int request_id)
1905{
FUJITA Tomonori0debe012007-05-26 02:33:31 +09001906 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
FUJITA Tomonori0debe012007-05-26 02:33:31 +09001907
1908 if (scsi_bufflen(cmd) < TW_MIN_SGL_LENGTH &&
1909 (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1910 cmd->sc_data_direction == DMA_BIDIRECTIONAL)) {
FUJITA Tomonorib1192d52007-05-31 15:24:03 +09001911 if (scsi_sg_count(cmd) == 1) {
FUJITA Tomonori035f5e02008-03-09 13:44:37 +09001912 void *buf = tw_dev->generic_buffer_virt[request_id];
1913
FUJITA Tomonori035f5e02008-03-09 13:44:37 +09001914 scsi_sg_copy_from_buffer(cmd, buf, TW_SECTOR_SIZE);
adam radfordd327d082005-09-09 15:55:13 -07001915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 }
1917} /* End twa_scsiop_execute_scsi_complete() */
1918
1919/* This function tells the controller to shut down */
1920static void __twa_shutdown(TW_Device_Extension *tw_dev)
1921{
1922 /* Disable interrupts */
1923 TW_DISABLE_INTERRUPTS(tw_dev);
1924
adam radford4039c302006-10-26 18:01:06 -07001925 /* Free up the IRQ */
1926 free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
1927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 printk(KERN_WARNING "3w-9xxx: Shutting down host %d.\n", tw_dev->host->host_no);
1929
1930 /* Tell the card we are shutting down */
1931 if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
1932 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x31, "Connection shutdown failed");
1933 } else {
1934 printk(KERN_WARNING "3w-9xxx: Shutdown complete.\n");
1935 }
1936
1937 /* Clear all interrupts just before exit */
1938 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1939} /* End __twa_shutdown() */
1940
1941/* Wrapper for __twa_shutdown */
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07001942static void twa_shutdown(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07001944 struct Scsi_Host *host = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
1946
1947 __twa_shutdown(tw_dev);
1948} /* End twa_shutdown() */
1949
1950/* This function will look up a string */
1951static char *twa_string_lookup(twa_message_type *table, unsigned int code)
1952{
1953 int index;
1954
1955 for (index = 0; ((code != table[index].code) &&
1956 (table[index].text != (char *)0)); index++);
1957 return(table[index].text);
1958} /* End twa_string_lookup() */
1959
adam radford4deedd82010-03-08 12:37:46 -08001960/* This function gets called when a disk is coming on-line */
1961static int twa_slave_configure(struct scsi_device *sdev)
1962{
1963 /* Force 60 second timeout */
1964 blk_queue_rq_timeout(sdev->request_queue, 60 * HZ);
1965
1966 return 0;
1967} /* End twa_slave_configure() */
1968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969/* scsi_host_template initializer */
1970static struct scsi_host_template driver_template = {
1971 .module = THIS_MODULE,
1972 .name = "3ware 9000 Storage Controller",
1973 .queuecommand = twa_scsi_queue,
1974 .eh_host_reset_handler = twa_scsi_eh_reset,
1975 .bios_param = twa_scsi_biosparam,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001976 .change_queue_depth = scsi_change_queue_depth,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 .can_queue = TW_Q_LENGTH-2,
adam radford4deedd82010-03-08 12:37:46 -08001978 .slave_configure = twa_slave_configure,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 .this_id = -1,
1980 .sg_tablesize = TW_APACHE_MAX_SGL_LENGTH,
1981 .max_sectors = TW_MAX_SECTORS,
1982 .cmd_per_lun = TW_MAX_CMDS_PER_LUN,
1983 .use_clustering = ENABLE_CLUSTERING,
1984 .shost_attrs = twa_host_attrs,
Martin K. Petersen54b2b502013-10-23 06:25:40 -04001985 .emulated = 1,
1986 .no_write_same = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987};
1988
1989/* This function will probe and initialize a card */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08001990static int twa_probe(struct pci_dev *pdev, const struct pci_device_id *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991{
1992 struct Scsi_Host *host = NULL;
1993 TW_Device_Extension *tw_dev;
adam radford3dabec72008-07-22 16:47:40 -07001994 unsigned long mem_addr, mem_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 int retval = -ENODEV;
1996
1997 retval = pci_enable_device(pdev);
1998 if (retval) {
1999 TW_PRINTK(host, TW_DRIVER, 0x34, "Failed to enable pci device");
2000 goto out_disable_device;
2001 }
2002
2003 pci_set_master(pdev);
Tony Battersby1e6c38c2007-11-09 13:04:35 -05002004 pci_try_set_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Yang Hongyang6a355282009-04-06 19:01:13 -07002006 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))
2007 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
Yang Hongyang284901a2009-04-06 19:01:15 -07002008 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
2009 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
adam radford0e78d152007-07-20 15:28:28 -07002010 TW_PRINTK(host, TW_DRIVER, 0x23, "Failed to set dma mask");
2011 retval = -ENODEV;
2012 goto out_disable_device;
2013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015 host = scsi_host_alloc(&driver_template, sizeof(TW_Device_Extension));
2016 if (!host) {
2017 TW_PRINTK(host, TW_DRIVER, 0x24, "Failed to allocate memory for device extension");
2018 retval = -ENOMEM;
2019 goto out_disable_device;
2020 }
2021 tw_dev = (TW_Device_Extension *)host->hostdata;
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 /* Save values to device extension */
2024 tw_dev->host = host;
2025 tw_dev->tw_pci_dev = pdev;
2026
2027 if (twa_initialize_device_extension(tw_dev)) {
2028 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x25, "Failed to initialize device extension");
2029 goto out_free_device_extension;
2030 }
2031
2032 /* Request IO regions */
2033 retval = pci_request_regions(pdev, "3w-9xxx");
2034 if (retval) {
2035 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x26, "Failed to get mem region");
2036 goto out_free_device_extension;
2037 }
2038
adam radford3dabec72008-07-22 16:47:40 -07002039 if (pdev->device == PCI_DEVICE_ID_3WARE_9000) {
adam radford49bfd8d2005-09-21 17:20:14 -07002040 mem_addr = pci_resource_start(pdev, 1);
adam radford3dabec72008-07-22 16:47:40 -07002041 mem_len = pci_resource_len(pdev, 1);
2042 } else {
adam radford49bfd8d2005-09-21 17:20:14 -07002043 mem_addr = pci_resource_start(pdev, 2);
adam radford3dabec72008-07-22 16:47:40 -07002044 mem_len = pci_resource_len(pdev, 2);
2045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047 /* Save base address */
adam radford3dabec72008-07-22 16:47:40 -07002048 tw_dev->base_addr = ioremap(mem_addr, mem_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if (!tw_dev->base_addr) {
2050 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x35, "Failed to ioremap");
2051 goto out_release_mem_region;
2052 }
2053
2054 /* Disable interrupts on the card */
2055 TW_DISABLE_INTERRUPTS(tw_dev);
2056
2057 /* Initialize the card */
2058 if (twa_reset_sequence(tw_dev, 0))
adam radford4039c302006-10-26 18:01:06 -07002059 goto out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 /* Set host specific parameters */
adam radford0e78d152007-07-20 15:28:28 -07002062 if ((pdev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
2063 (pdev->device == PCI_DEVICE_ID_3WARE_9690SA))
adam radford4039c302006-10-26 18:01:06 -07002064 host->max_id = TW_MAX_UNITS_9650SE;
2065 else
2066 host->max_id = TW_MAX_UNITS;
2067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 host->max_cmd_len = TW_MAX_CDB_LEN;
2069
2070 /* Channels aren't supported by adapter */
adam radford4039c302006-10-26 18:01:06 -07002071 host->max_lun = TW_MAX_LUNS(tw_dev->tw_compat_info.working_srl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 host->max_channel = 0;
2073
2074 /* Register the card with the kernel SCSI layer */
2075 retval = scsi_add_host(host, &pdev->dev);
2076 if (retval) {
2077 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x27, "scsi add host failed");
adam radford4039c302006-10-26 18:01:06 -07002078 goto out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 }
2080
2081 pci_set_drvdata(pdev, host);
2082
adam radford3dabec72008-07-22 16:47:40 -07002083 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 -07002084 host->host_no, mem_addr, pdev->irq);
2085 printk(KERN_WARNING "3w-9xxx: scsi%d: Firmware %s, BIOS %s, Ports: %d.\n",
2086 host->host_no,
2087 (char *)twa_get_param(tw_dev, 0, TW_VERSION_TABLE,
2088 TW_PARAM_FWVER, TW_PARAM_FWVER_LENGTH),
2089 (char *)twa_get_param(tw_dev, 1, TW_VERSION_TABLE,
2090 TW_PARAM_BIOSVER, TW_PARAM_BIOSVER_LENGTH),
adam radford75913d92006-03-15 12:43:19 -08002091 le32_to_cpu(*(int *)twa_get_param(tw_dev, 2, TW_INFORMATION_TABLE,
2092 TW_PARAM_PORTCOUNT, TW_PARAM_PORTCOUNT_LENGTH)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
adam radford3dabec72008-07-22 16:47:40 -07002094 /* Try to enable MSI */
2095 if (use_msi && (pdev->device != PCI_DEVICE_ID_3WARE_9000) &&
2096 !pci_enable_msi(pdev))
2097 set_bit(TW_USING_MSI, &tw_dev->flags);
2098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 /* Now setup the interrupt handler */
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07002100 retval = request_irq(pdev->irq, twa_interrupt, IRQF_SHARED, "3w-9xxx", tw_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (retval) {
2102 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x30, "Error requesting IRQ");
2103 goto out_remove_host;
2104 }
2105
2106 twa_device_extension_list[twa_device_extension_count] = tw_dev;
2107 twa_device_extension_count++;
2108
2109 /* Re-enable interrupts on the card */
2110 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
2111
2112 /* Finally, scan the host */
2113 scsi_scan_host(host);
2114
2115 if (twa_major == -1) {
2116 if ((twa_major = register_chrdev (0, "twa", &twa_fops)) < 0)
2117 TW_PRINTK(host, TW_DRIVER, 0x29, "Failed to register character device");
2118 }
2119 return 0;
2120
2121out_remove_host:
adam radford3dabec72008-07-22 16:47:40 -07002122 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2123 pci_disable_msi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 scsi_remove_host(host);
adam radford4039c302006-10-26 18:01:06 -07002125out_iounmap:
2126 iounmap(tw_dev->base_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127out_release_mem_region:
2128 pci_release_regions(pdev);
2129out_free_device_extension:
2130 twa_free_device_extension(tw_dev);
2131 scsi_host_put(host);
2132out_disable_device:
2133 pci_disable_device(pdev);
2134
2135 return retval;
2136} /* End twa_probe() */
2137
2138/* This function is called to remove a device */
2139static void twa_remove(struct pci_dev *pdev)
2140{
2141 struct Scsi_Host *host = pci_get_drvdata(pdev);
2142 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2143
2144 scsi_remove_host(tw_dev->host);
2145
2146 /* Unregister character device */
2147 if (twa_major >= 0) {
2148 unregister_chrdev(twa_major, "twa");
2149 twa_major = -1;
2150 }
2151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 /* Shutdown the card */
2153 __twa_shutdown(tw_dev);
2154
adam radford3dabec72008-07-22 16:47:40 -07002155 /* Disable MSI if enabled */
2156 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2157 pci_disable_msi(pdev);
2158
adam radford4039c302006-10-26 18:01:06 -07002159 /* Free IO remapping */
2160 iounmap(tw_dev->base_addr);
2161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 /* Free up the mem region */
2163 pci_release_regions(pdev);
2164
2165 /* Free up device extension resources */
2166 twa_free_device_extension(tw_dev);
2167
2168 scsi_host_put(tw_dev->host);
2169 pci_disable_device(pdev);
2170 twa_device_extension_count--;
2171} /* End twa_remove() */
2172
adam radford7a252fe2009-03-09 12:15:01 -08002173#ifdef CONFIG_PM
2174/* This function is called on PCI suspend */
2175static int twa_suspend(struct pci_dev *pdev, pm_message_t state)
2176{
2177 struct Scsi_Host *host = pci_get_drvdata(pdev);
2178 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2179
2180 printk(KERN_WARNING "3w-9xxx: Suspending host %d.\n", tw_dev->host->host_no);
2181
2182 TW_DISABLE_INTERRUPTS(tw_dev);
2183 free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
2184
2185 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2186 pci_disable_msi(pdev);
2187
2188 /* Tell the card we are shutting down */
2189 if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
2190 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x38, "Connection shutdown failed during suspend");
2191 } else {
2192 printk(KERN_WARNING "3w-9xxx: Suspend complete.\n");
2193 }
2194 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
2195
2196 pci_save_state(pdev);
2197 pci_disable_device(pdev);
2198 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2199
2200 return 0;
2201} /* End twa_suspend() */
2202
2203/* This function is called on PCI resume */
2204static int twa_resume(struct pci_dev *pdev)
2205{
2206 int retval = 0;
2207 struct Scsi_Host *host = pci_get_drvdata(pdev);
2208 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2209
2210 printk(KERN_WARNING "3w-9xxx: Resuming host %d.\n", tw_dev->host->host_no);
2211 pci_set_power_state(pdev, PCI_D0);
2212 pci_enable_wake(pdev, PCI_D0, 0);
2213 pci_restore_state(pdev);
2214
2215 retval = pci_enable_device(pdev);
2216 if (retval) {
2217 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x39, "Enable device failed during resume");
2218 return retval;
2219 }
2220
2221 pci_set_master(pdev);
2222 pci_try_set_mwi(pdev);
2223
Yang Hongyange9304382009-04-13 14:40:14 -07002224 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))
2225 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
2226 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
2227 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
adam radford7a252fe2009-03-09 12:15:01 -08002228 TW_PRINTK(host, TW_DRIVER, 0x40, "Failed to set dma mask during resume");
2229 retval = -ENODEV;
2230 goto out_disable_device;
2231 }
2232
2233 /* Initialize the card */
2234 if (twa_reset_sequence(tw_dev, 0)) {
2235 retval = -ENODEV;
2236 goto out_disable_device;
2237 }
2238
2239 /* Now setup the interrupt handler */
2240 retval = request_irq(pdev->irq, twa_interrupt, IRQF_SHARED, "3w-9xxx", tw_dev);
2241 if (retval) {
2242 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x42, "Error requesting IRQ during resume");
2243 retval = -ENODEV;
2244 goto out_disable_device;
2245 }
2246
2247 /* Now enable MSI if enabled */
2248 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2249 pci_enable_msi(pdev);
2250
2251 /* Re-enable interrupts on the card */
2252 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
2253
2254 printk(KERN_WARNING "3w-9xxx: Resume complete.\n");
2255 return 0;
2256
2257out_disable_device:
2258 scsi_remove_host(host);
2259 pci_disable_device(pdev);
2260
2261 return retval;
2262} /* End twa_resume() */
2263#endif
2264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265/* PCI Devices supported by this driver */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08002266static struct pci_device_id twa_pci_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9000,
2268 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
adam radford49bfd8d2005-09-21 17:20:14 -07002269 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9550SX,
2270 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
adam radford4039c302006-10-26 18:01:06 -07002271 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9650SE,
2272 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
adam radford0e78d152007-07-20 15:28:28 -07002273 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9690SA,
2274 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 { }
2276};
2277MODULE_DEVICE_TABLE(pci, twa_pci_tbl);
2278
2279/* pci_driver initializer */
2280static struct pci_driver twa_driver = {
2281 .name = "3w-9xxx",
2282 .id_table = twa_pci_tbl,
2283 .probe = twa_probe,
2284 .remove = twa_remove,
adam radford7a252fe2009-03-09 12:15:01 -08002285#ifdef CONFIG_PM
2286 .suspend = twa_suspend,
2287 .resume = twa_resume,
2288#endif
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07002289 .shutdown = twa_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290};
2291
2292/* This function is called on driver initialization */
2293static int __init twa_init(void)
2294{
2295 printk(KERN_WARNING "3ware 9000 Storage Controller device driver for Linux v%s.\n", TW_DRIVER_VERSION);
2296
Henrik Kretzschmardcbccbde2006-09-25 16:58:58 -07002297 return pci_register_driver(&twa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298} /* End twa_init() */
2299
2300/* This function is called on driver exit */
2301static void __exit twa_exit(void)
2302{
2303 pci_unregister_driver(&twa_driver);
2304} /* End twa_exit() */
2305
2306module_init(twa_init);
2307module_exit(twa_exit);
2308