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