1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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>
94#include <linux/mutex.h>
95#include <linux/slab.h>
96#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
106#define TW_DRIVER_VERSION "2.26.02.014.rh1"
107static DEFINE_MUTEX(twa_chrdev_mutex);
108static 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
114MODULE_AUTHOR ("LSI");
115MODULE_DESCRIPTION ("3ware 9000 Storage Controller Linux Driver");
116MODULE_LICENSE("GPL");
117MODULE_VERSION(TW_DRIVER_VERSION);
118
119static int use_msi = 0;
120module_param(use_msi, int, S_IRUGO);
121MODULE_PARM_DESC(use_msi, "Use Message Signaled Interrupts. Default: 0");
122
123
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);
128static long twa_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
129static 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);
143static 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);
144static 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);
147static int twa_reset_device_extension(TW_Device_Extension *tw_dev);
148static 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);
152
153
154
155
156static ssize_t twa_show_stats(struct device *dev,
157 struct device_attribute *attr, char *buf)
158{
159 struct Scsi_Host *host = class_to_shost(dev);
160 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}
190
191
192static int twa_change_queue_depth(struct scsi_device *sdev, int queue_depth,
193 int reason)
194{
195 if (reason != SCSI_QDEPTH_DEFAULT)
196 return -EOPNOTSUPP;
197
198 if (queue_depth > TW_Q_LENGTH-2)
199 queue_depth = TW_Q_LENGTH-2;
200 scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, queue_depth);
201 return queue_depth;
202}
203
204
205static struct device_attribute twa_host_stats_attr = {
206 .attr = {
207 .name = "stats",
208 .mode = S_IRUGO,
209 },
210 .show = twa_show_stats
211};
212
213
214static struct device_attribute *twa_host_attrs[] = {
215 &twa_host_stats_attr,
216 NULL,
217};
218
219
220static const struct file_operations twa_fops = {
221 .owner = THIS_MODULE,
222 .unlocked_ioctl = twa_chrdev_ioctl,
223 .open = twa_chrdev_open,
224 .release = NULL,
225 .llseek = noop_llseek,
226};
227
228
229
230
231
232
233static bool twa_command_mapped(struct scsi_cmnd *cmd)
234{
235 return scsi_sg_count(cmd) != 1 ||
236 scsi_bufflen(cmd) >= TW_MIN_SGL_LENGTH;
237}
238
239
240static int twa_aen_complete(TW_Device_Extension *tw_dev, int request_id)
241{
242 TW_Command_Full *full_command_packet;
243 TW_Command *command_packet;
244 TW_Command_Apache_Header *header;
245 unsigned short aen;
246 int retval = 1;
247
248 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
249 tw_dev->posted_request_count--;
250 aen = le16_to_cpu(header->status_block.error);
251 full_command_packet = tw_dev->command_packet_virt[request_id];
252 command_packet = &full_command_packet->command.oldcommand;
253
254
255 if (TW_OP_OUT(command_packet->opcode__sgloffset) == TW_OP_SET_PARAM) {
256
257 if (twa_aen_read_queue(tw_dev, request_id))
258 goto out2;
259 else {
260 retval = 0;
261 goto out;
262 }
263 }
264
265 switch (aen) {
266 case TW_AEN_QUEUE_EMPTY:
267
268 break;
269 case TW_AEN_SYNC_TIME_WITH_HOST:
270 twa_aen_sync_time(tw_dev, request_id);
271 retval = 0;
272 goto out;
273 default:
274 twa_aen_queue_event(tw_dev, header);
275
276
277 if (twa_aen_read_queue(tw_dev, request_id))
278 goto out2;
279 else {
280 retval = 0;
281 goto out;
282 }
283 }
284 retval = 0;
285out2:
286 tw_dev->state[request_id] = TW_S_COMPLETED;
287 twa_free_request_id(tw_dev, request_id);
288 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
289out:
290 return retval;
291}
292
293
294static int twa_aen_drain_queue(TW_Device_Extension *tw_dev, int no_check_reset)
295{
296 int request_id = 0;
297 char cdb[TW_MAX_CDB_LEN];
298 TW_SG_Entry sglist[1];
299 int finished = 0, count = 0;
300 TW_Command_Full *full_command_packet;
301 TW_Command_Apache_Header *header;
302 unsigned short aen;
303 int first_reset = 0, queue = 0, retval = 1;
304
305 if (no_check_reset)
306 first_reset = 0;
307 else
308 first_reset = 1;
309
310 full_command_packet = tw_dev->command_packet_virt[request_id];
311 memset(full_command_packet, 0, sizeof(TW_Command_Full));
312
313
314 memset(&cdb, 0, TW_MAX_CDB_LEN);
315 cdb[0] = REQUEST_SENSE;
316 cdb[4] = TW_ALLOCATION_LENGTH;
317
318
319 memset(&sglist, 0, sizeof(TW_SG_Entry));
320 sglist[0].length = TW_SECTOR_SIZE;
321 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
322
323 if (sglist[0].address & TW_ALIGNMENT_9000_SGL) {
324 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1, "Found unaligned address during AEN drain");
325 goto out;
326 }
327
328
329 tw_dev->srb[request_id] = NULL;
330
331 do {
332
333 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
334 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2, "Error posting request sense");
335 goto out;
336 }
337
338
339 if (twa_poll_response(tw_dev, request_id, 30)) {
340 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x3, "No valid response while draining AEN queue");
341 tw_dev->posted_request_count--;
342 goto out;
343 }
344
345 tw_dev->posted_request_count--;
346 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
347 aen = le16_to_cpu(header->status_block.error);
348 queue = 0;
349 count++;
350
351 switch (aen) {
352 case TW_AEN_QUEUE_EMPTY:
353 if (first_reset != 1)
354 goto out;
355 else
356 finished = 1;
357 break;
358 case TW_AEN_SOFT_RESET:
359 if (first_reset == 0)
360 first_reset = 1;
361 else
362 queue = 1;
363 break;
364 case TW_AEN_SYNC_TIME_WITH_HOST:
365 break;
366 default:
367 queue = 1;
368 }
369
370
371 if (queue)
372 twa_aen_queue_event(tw_dev, header);
373 } while ((finished == 0) && (count < TW_MAX_AEN_DRAIN));
374
375 if (count == TW_MAX_AEN_DRAIN)
376 goto out;
377
378 retval = 0;
379out:
380 tw_dev->state[request_id] = TW_S_INITIAL;
381 return retval;
382}
383
384
385static void twa_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header)
386{
387 u32 local_time;
388 struct timeval time;
389 TW_Event *event;
390 unsigned short aen;
391 char host[16];
392 char *error_str;
393
394 tw_dev->aen_count++;
395
396
397 event = tw_dev->event_queue[tw_dev->error_index];
398
399
400 host[0] = '\0';
401 if (tw_dev->host) {
402 sprintf(host, " scsi%d:", tw_dev->host->host_no);
403 if (event->retrieved == TW_AEN_NOT_RETRIEVED)
404 tw_dev->aen_clobber = 1;
405 }
406
407 aen = le16_to_cpu(header->status_block.error);
408 memset(event, 0, sizeof(TW_Event));
409
410 event->severity = TW_SEV_OUT(header->status_block.severity__reserved);
411 do_gettimeofday(&time);
412 local_time = (u32)(time.tv_sec - (sys_tz.tz_minuteswest * 60));
413 event->time_stamp_sec = local_time;
414 event->aen_code = aen;
415 event->retrieved = TW_AEN_NOT_RETRIEVED;
416 event->sequence_id = tw_dev->error_sequence_id;
417 tw_dev->error_sequence_id++;
418
419
420 error_str = &(header->err_specific_desc[strlen(header->err_specific_desc)+1]);
421
422 header->err_specific_desc[sizeof(header->err_specific_desc) - 1] = '\0';
423 event->parameter_len = strlen(header->err_specific_desc);
424 memcpy(event->parameter_data, header->err_specific_desc, event->parameter_len + (error_str[0] == '\0' ? 0 : (1 + strlen(error_str))));
425 if (event->severity != TW_AEN_SEVERITY_DEBUG)
426 printk(KERN_WARNING "3w-9xxx:%s AEN: %s (0x%02X:0x%04X): %s:%s.\n",
427 host,
428 twa_aen_severity_lookup(TW_SEV_OUT(header->status_block.severity__reserved)),
429 TW_MESSAGE_SOURCE_CONTROLLER_EVENT, aen,
430 error_str[0] == '\0' ? twa_string_lookup(twa_aen_table, aen) : error_str,
431 header->err_specific_desc);
432 else
433 tw_dev->aen_count--;
434
435 if ((tw_dev->error_index + 1) == TW_Q_LENGTH)
436 tw_dev->event_queue_wrapped = 1;
437 tw_dev->error_index = (tw_dev->error_index + 1 ) % TW_Q_LENGTH;
438}
439
440
441static int twa_aen_read_queue(TW_Device_Extension *tw_dev, int request_id)
442{
443 char cdb[TW_MAX_CDB_LEN];
444 TW_SG_Entry sglist[1];
445 TW_Command_Full *full_command_packet;
446 int retval = 1;
447
448 full_command_packet = tw_dev->command_packet_virt[request_id];
449 memset(full_command_packet, 0, sizeof(TW_Command_Full));
450
451
452 memset(&cdb, 0, TW_MAX_CDB_LEN);
453 cdb[0] = REQUEST_SENSE;
454 cdb[4] = TW_ALLOCATION_LENGTH;
455
456
457 memset(&sglist, 0, sizeof(TW_SG_Entry));
458 sglist[0].length = TW_SECTOR_SIZE;
459 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
460
461
462 tw_dev->srb[request_id] = NULL;
463
464
465 if (twa_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
466 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x4, "Post failed while reading AEN queue");
467 goto out;
468 }
469 retval = 0;
470out:
471 return retval;
472}
473
474
475static char *twa_aen_severity_lookup(unsigned char severity_code)
476{
477 char *retval = NULL;
478
479 if ((severity_code < (unsigned char) TW_AEN_SEVERITY_ERROR) ||
480 (severity_code > (unsigned char) TW_AEN_SEVERITY_DEBUG))
481 goto out;
482
483 retval = twa_aen_severity_table[severity_code];
484out:
485 return retval;
486}
487
488
489static void twa_aen_sync_time(TW_Device_Extension *tw_dev, int request_id)
490{
491 u32 schedulertime;
492 struct timeval utc;
493 TW_Command_Full *full_command_packet;
494 TW_Command *command_packet;
495 TW_Param_Apache *param;
496 u32 local_time;
497
498
499 full_command_packet = tw_dev->command_packet_virt[request_id];
500 memset(full_command_packet, 0, sizeof(TW_Command_Full));
501 command_packet = &full_command_packet->command.oldcommand;
502 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_SET_PARAM);
503 command_packet->request_id = request_id;
504 command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
505 command_packet->byte8_offset.param.sgl[0].length = cpu_to_le32(TW_SECTOR_SIZE);
506 command_packet->size = TW_COMMAND_SIZE;
507 command_packet->byte6_offset.parameter_count = cpu_to_le16(1);
508
509
510 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
511 memset(param, 0, TW_SECTOR_SIZE);
512 param->table_id = cpu_to_le16(TW_TIMEKEEP_TABLE | 0x8000);
513 param->parameter_id = cpu_to_le16(0x3);
514 param->parameter_size_bytes = cpu_to_le16(4);
515
516
517
518 do_gettimeofday(&utc);
519 local_time = (u32)(utc.tv_sec - (sys_tz.tz_minuteswest * 60));
520 schedulertime = local_time - (3 * 86400);
521 schedulertime = cpu_to_le32(schedulertime % 604800);
522
523 memcpy(param->data, &schedulertime, sizeof(u32));
524
525
526 tw_dev->srb[request_id] = NULL;
527
528
529 twa_post_command_packet(tw_dev, request_id, 1);
530}
531
532
533static int twa_allocate_memory(TW_Device_Extension *tw_dev, int size, int which)
534{
535 int i;
536 dma_addr_t dma_handle;
537 unsigned long *cpu_addr;
538 int retval = 1;
539
540 cpu_addr = pci_alloc_consistent(tw_dev->tw_pci_dev, size*TW_Q_LENGTH, &dma_handle);
541 if (!cpu_addr) {
542 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x5, "Memory allocation failed");
543 goto out;
544 }
545
546 if ((unsigned long)cpu_addr % (TW_ALIGNMENT_9000)) {
547 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x6, "Failed to allocate correctly aligned memory");
548 pci_free_consistent(tw_dev->tw_pci_dev, size*TW_Q_LENGTH, cpu_addr, dma_handle);
549 goto out;
550 }
551
552 memset(cpu_addr, 0, size*TW_Q_LENGTH);
553
554 for (i = 0; i < TW_Q_LENGTH; i++) {
555 switch(which) {
556 case 0:
557 tw_dev->command_packet_phys[i] = dma_handle+(i*size);
558 tw_dev->command_packet_virt[i] = (TW_Command_Full *)((unsigned char *)cpu_addr + (i*size));
559 break;
560 case 1:
561 tw_dev->generic_buffer_phys[i] = dma_handle+(i*size);
562 tw_dev->generic_buffer_virt[i] = (unsigned long *)((unsigned char *)cpu_addr + (i*size));
563 break;
564 }
565 }
566 retval = 0;
567out:
568 return retval;
569}
570
571
572static int twa_check_bits(u32 status_reg_value)
573{
574 int retval = 1;
575
576 if ((status_reg_value & TW_STATUS_EXPECTED_BITS) != TW_STATUS_EXPECTED_BITS)
577 goto out;
578 if ((status_reg_value & TW_STATUS_UNEXPECTED_BITS) != 0)
579 goto out;
580
581 retval = 0;
582out:
583 return retval;
584}
585
586
587static int twa_check_srl(TW_Device_Extension *tw_dev, int *flashed)
588{
589 int retval = 1;
590 unsigned short fw_on_ctlr_srl = 0, fw_on_ctlr_arch_id = 0;
591 unsigned short fw_on_ctlr_branch = 0, fw_on_ctlr_build = 0;
592 u32 init_connect_result = 0;
593
594 if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
595 TW_EXTENDED_INIT_CONNECT, TW_CURRENT_DRIVER_SRL,
596 TW_9000_ARCH_ID, TW_CURRENT_DRIVER_BRANCH,
597 TW_CURRENT_DRIVER_BUILD, &fw_on_ctlr_srl,
598 &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
599 &fw_on_ctlr_build, &init_connect_result)) {
600 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x7, "Initconnection failed while checking SRL");
601 goto out;
602 }
603
604 tw_dev->tw_compat_info.working_srl = fw_on_ctlr_srl;
605 tw_dev->tw_compat_info.working_branch = fw_on_ctlr_branch;
606 tw_dev->tw_compat_info.working_build = fw_on_ctlr_build;
607
608
609 if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
610 if (twa_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
611 TW_EXTENDED_INIT_CONNECT,
612 TW_BASE_FW_SRL, TW_9000_ARCH_ID,
613 TW_BASE_FW_BRANCH, TW_BASE_FW_BUILD,
614 &fw_on_ctlr_srl, &fw_on_ctlr_arch_id,
615 &fw_on_ctlr_branch, &fw_on_ctlr_build,
616 &init_connect_result)) {
617 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xa, "Initconnection (base mode) failed while checking SRL");
618 goto out;
619 }
620 if (!(init_connect_result & TW_CTLR_FW_COMPATIBLE)) {
621 if (TW_CURRENT_DRIVER_SRL > fw_on_ctlr_srl) {
622 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x32, "Firmware and driver incompatibility: please upgrade firmware");
623 } else {
624 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x33, "Firmware and driver incompatibility: please upgrade driver");
625 }
626 goto out;
627 }
628 tw_dev->tw_compat_info.working_srl = TW_BASE_FW_SRL;
629 tw_dev->tw_compat_info.working_branch = TW_BASE_FW_BRANCH;
630 tw_dev->tw_compat_info.working_build = TW_BASE_FW_BUILD;
631 }
632
633
634 strncpy(tw_dev->tw_compat_info.driver_version, TW_DRIVER_VERSION, strlen(TW_DRIVER_VERSION));
635 tw_dev->tw_compat_info.driver_srl_high = TW_CURRENT_DRIVER_SRL;
636 tw_dev->tw_compat_info.driver_branch_high = TW_CURRENT_DRIVER_BRANCH;
637 tw_dev->tw_compat_info.driver_build_high = TW_CURRENT_DRIVER_BUILD;
638 tw_dev->tw_compat_info.driver_srl_low = TW_BASE_FW_SRL;
639 tw_dev->tw_compat_info.driver_branch_low = TW_BASE_FW_BRANCH;
640 tw_dev->tw_compat_info.driver_build_low = TW_BASE_FW_BUILD;
641 tw_dev->tw_compat_info.fw_on_ctlr_srl = fw_on_ctlr_srl;
642 tw_dev->tw_compat_info.fw_on_ctlr_branch = fw_on_ctlr_branch;
643 tw_dev->tw_compat_info.fw_on_ctlr_build = fw_on_ctlr_build;
644
645 retval = 0;
646out:
647 return retval;
648}
649
650
651static long twa_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
652{
653 struct inode *inode = file_inode(file);
654 long timeout;
655 unsigned long *cpu_addr, data_buffer_length_adjusted = 0, flags = 0;
656 dma_addr_t dma_handle;
657 int request_id = 0;
658 unsigned int sequence_id = 0;
659 unsigned char event_index, start_index;
660 TW_Ioctl_Driver_Command driver_command;
661 TW_Ioctl_Buf_Apache *tw_ioctl;
662 TW_Lock *tw_lock;
663 TW_Command_Full *full_command_packet;
664 TW_Compatibility_Info *tw_compat_info;
665 TW_Event *event;
666 struct timeval current_time;
667 u32 current_time_ms;
668 TW_Device_Extension *tw_dev = twa_device_extension_list[iminor(inode)];
669 int retval = TW_IOCTL_ERROR_OS_EFAULT;
670 void __user *argp = (void __user *)arg;
671
672 mutex_lock(&twa_chrdev_mutex);
673
674
675 if (mutex_lock_interruptible(&tw_dev->ioctl_lock)) {
676 retval = TW_IOCTL_ERROR_OS_EINTR;
677 goto out;
678 }
679
680
681 if (copy_from_user(&driver_command, argp, sizeof(TW_Ioctl_Driver_Command)))
682 goto out2;
683
684
685 if (driver_command.buffer_length > TW_MAX_SECTORS * 2048) {
686 retval = TW_IOCTL_ERROR_OS_EINVAL;
687 goto out2;
688 }
689
690
691 data_buffer_length_adjusted = (driver_command.buffer_length + 511) & ~511;
692
693
694 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);
695 if (!cpu_addr) {
696 retval = TW_IOCTL_ERROR_OS_ENOMEM;
697 goto out2;
698 }
699
700 tw_ioctl = (TW_Ioctl_Buf_Apache *)cpu_addr;
701
702
703 if (copy_from_user(tw_ioctl, argp, driver_command.buffer_length + sizeof(TW_Ioctl_Buf_Apache) - 1))
704 goto out3;
705
706
707 switch (cmd) {
708 case TW_IOCTL_FIRMWARE_PASS_THROUGH:
709 spin_lock_irqsave(tw_dev->host->host_lock, flags);
710 twa_get_request_id(tw_dev, &request_id);
711
712
713 tw_dev->srb[request_id] = NULL;
714
715
716 tw_dev->chrdev_request_id = request_id;
717
718 full_command_packet = &tw_ioctl->firmware_command;
719
720
721 twa_load_sgl(tw_dev, full_command_packet, request_id, dma_handle, data_buffer_length_adjusted);
722
723 memcpy(tw_dev->command_packet_virt[request_id], &(tw_ioctl->firmware_command), sizeof(TW_Command_Full));
724
725
726 twa_post_command_packet(tw_dev, request_id, 1);
727 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
728
729 timeout = TW_IOCTL_CHRDEV_TIMEOUT*HZ;
730
731
732 timeout = wait_event_timeout(tw_dev->ioctl_wqueue, tw_dev->chrdev_request_id == TW_IOCTL_CHRDEV_FREE, timeout);
733
734
735 if (tw_dev->chrdev_request_id != TW_IOCTL_CHRDEV_FREE) {
736
737 printk(KERN_WARNING "3w-9xxx: scsi%d: WARNING: (0x%02X:0x%04X): Character ioctl (0x%x) timed out, resetting card.\n",
738 tw_dev->host->host_no, TW_DRIVER, 0x37,
739 cmd);
740 retval = TW_IOCTL_ERROR_OS_EIO;
741 twa_reset_device_extension(tw_dev);
742 goto out3;
743 }
744
745
746 memcpy(&(tw_ioctl->firmware_command), tw_dev->command_packet_virt[request_id], sizeof(TW_Command_Full));
747
748
749 spin_lock_irqsave(tw_dev->host->host_lock, flags);
750 tw_dev->posted_request_count--;
751 tw_dev->state[request_id] = TW_S_COMPLETED;
752 twa_free_request_id(tw_dev, request_id);
753 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
754 break;
755 case TW_IOCTL_GET_COMPATIBILITY_INFO:
756 tw_ioctl->driver_command.status = 0;
757
758 tw_compat_info = (TW_Compatibility_Info *)tw_ioctl->data_buffer;
759 memcpy(tw_compat_info, &tw_dev->tw_compat_info, sizeof(TW_Compatibility_Info));
760 break;
761 case TW_IOCTL_GET_LAST_EVENT:
762 if (tw_dev->event_queue_wrapped) {
763 if (tw_dev->aen_clobber) {
764 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
765 tw_dev->aen_clobber = 0;
766 } else
767 tw_ioctl->driver_command.status = 0;
768 } else {
769 if (!tw_dev->error_index) {
770 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
771 break;
772 }
773 tw_ioctl->driver_command.status = 0;
774 }
775 event_index = (tw_dev->error_index - 1 + TW_Q_LENGTH) % TW_Q_LENGTH;
776 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
777 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
778 break;
779 case TW_IOCTL_GET_FIRST_EVENT:
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 } else
785 tw_ioctl->driver_command.status = 0;
786 event_index = tw_dev->error_index;
787 } else {
788 if (!tw_dev->error_index) {
789 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
790 break;
791 }
792 tw_ioctl->driver_command.status = 0;
793 event_index = 0;
794 }
795 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
796 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
797 break;
798 case TW_IOCTL_GET_NEXT_EVENT:
799 event = (TW_Event *)tw_ioctl->data_buffer;
800 sequence_id = event->sequence_id;
801 tw_ioctl->driver_command.status = 0;
802
803 if (tw_dev->event_queue_wrapped) {
804 if (tw_dev->aen_clobber) {
805 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
806 tw_dev->aen_clobber = 0;
807 }
808 start_index = tw_dev->error_index;
809 } else {
810 if (!tw_dev->error_index) {
811 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
812 break;
813 }
814 start_index = 0;
815 }
816 event_index = (start_index + sequence_id - tw_dev->event_queue[start_index]->sequence_id + 1) % TW_Q_LENGTH;
817
818 if (!(tw_dev->event_queue[event_index]->sequence_id > sequence_id)) {
819 if (tw_ioctl->driver_command.status == TW_IOCTL_ERROR_STATUS_AEN_CLOBBER)
820 tw_dev->aen_clobber = 1;
821 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
822 break;
823 }
824 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
825 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
826 break;
827 case TW_IOCTL_GET_PREVIOUS_EVENT:
828 event = (TW_Event *)tw_ioctl->data_buffer;
829 sequence_id = event->sequence_id;
830 tw_ioctl->driver_command.status = 0;
831
832 if (tw_dev->event_queue_wrapped) {
833 if (tw_dev->aen_clobber) {
834 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_AEN_CLOBBER;
835 tw_dev->aen_clobber = 0;
836 }
837 start_index = tw_dev->error_index;
838 } else {
839 if (!tw_dev->error_index) {
840 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
841 break;
842 }
843 start_index = 0;
844 }
845 event_index = (start_index + sequence_id - tw_dev->event_queue[start_index]->sequence_id - 1) % TW_Q_LENGTH;
846
847 if (!(tw_dev->event_queue[event_index]->sequence_id < sequence_id)) {
848 if (tw_ioctl->driver_command.status == TW_IOCTL_ERROR_STATUS_AEN_CLOBBER)
849 tw_dev->aen_clobber = 1;
850 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NO_MORE_EVENTS;
851 break;
852 }
853 memcpy(tw_ioctl->data_buffer, tw_dev->event_queue[event_index], sizeof(TW_Event));
854 tw_dev->event_queue[event_index]->retrieved = TW_AEN_RETRIEVED;
855 break;
856 case TW_IOCTL_GET_LOCK:
857 tw_lock = (TW_Lock *)tw_ioctl->data_buffer;
858 do_gettimeofday(¤t_time);
859 current_time_ms = (current_time.tv_sec * 1000) + (current_time.tv_usec / 1000);
860
861 if ((tw_lock->force_flag == 1) || (tw_dev->ioctl_sem_lock == 0) || (current_time_ms >= tw_dev->ioctl_msec)) {
862 tw_dev->ioctl_sem_lock = 1;
863 tw_dev->ioctl_msec = current_time_ms + tw_lock->timeout_msec;
864 tw_ioctl->driver_command.status = 0;
865 tw_lock->time_remaining_msec = tw_lock->timeout_msec;
866 } else {
867 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_LOCKED;
868 tw_lock->time_remaining_msec = tw_dev->ioctl_msec - current_time_ms;
869 }
870 break;
871 case TW_IOCTL_RELEASE_LOCK:
872 if (tw_dev->ioctl_sem_lock == 1) {
873 tw_dev->ioctl_sem_lock = 0;
874 tw_ioctl->driver_command.status = 0;
875 } else {
876 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_NOT_LOCKED;
877 }
878 break;
879 default:
880 retval = TW_IOCTL_ERROR_OS_ENOTTY;
881 goto out3;
882 }
883
884
885 if (copy_to_user(argp, tw_ioctl, sizeof(TW_Ioctl_Buf_Apache) + driver_command.buffer_length - 1) == 0)
886 retval = 0;
887out3:
888
889 dma_free_coherent(&tw_dev->tw_pci_dev->dev, data_buffer_length_adjusted+sizeof(TW_Ioctl_Buf_Apache) - 1, cpu_addr, dma_handle);
890out2:
891 mutex_unlock(&tw_dev->ioctl_lock);
892out:
893 mutex_unlock(&twa_chrdev_mutex);
894 return retval;
895}
896
897
898
899static int twa_chrdev_open(struct inode *inode, struct file *file)
900{
901 unsigned int minor_number;
902 int retval = TW_IOCTL_ERROR_OS_ENODEV;
903
904 minor_number = iminor(inode);
905 if (minor_number >= twa_device_extension_count)
906 goto out;
907 retval = 0;
908out:
909 return retval;
910}
911
912
913static int twa_decode_bits(TW_Device_Extension *tw_dev, u32 status_reg_value)
914{
915 int retval = 1;
916
917
918 if (status_reg_value & TW_STATUS_PCI_PARITY_ERROR) {
919 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xc, "PCI Parity Error: clearing");
920 writel(TW_CONTROL_CLEAR_PARITY_ERROR, TW_CONTROL_REG_ADDR(tw_dev));
921 }
922
923 if (status_reg_value & TW_STATUS_PCI_ABORT) {
924 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xd, "PCI Abort: clearing");
925 writel(TW_CONTROL_CLEAR_PCI_ABORT, TW_CONTROL_REG_ADDR(tw_dev));
926 pci_write_config_word(tw_dev->tw_pci_dev, PCI_STATUS, TW_PCI_CLEAR_PCI_ABORT);
927 }
928
929 if (status_reg_value & TW_STATUS_QUEUE_ERROR) {
930 if (((tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9650SE) &&
931 (tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9690SA)) ||
932 (!test_bit(TW_IN_RESET, &tw_dev->flags)))
933 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xe, "Controller Queue Error: clearing");
934 writel(TW_CONTROL_CLEAR_QUEUE_ERROR, TW_CONTROL_REG_ADDR(tw_dev));
935 }
936
937 if (status_reg_value & TW_STATUS_MICROCONTROLLER_ERROR) {
938 if (tw_dev->reset_print == 0) {
939 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x10, "Microcontroller Error: clearing");
940 tw_dev->reset_print = 1;
941 }
942 goto out;
943 }
944 retval = 0;
945out:
946 return retval;
947}
948
949
950static int twa_empty_response_queue(TW_Device_Extension *tw_dev)
951{
952 u32 status_reg_value, response_que_value;
953 int count = 0, retval = 1;
954
955 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
956
957 while (((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) && (count < TW_MAX_RESPONSE_DRAIN)) {
958 response_que_value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
959 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
960 count++;
961 }
962 if (count == TW_MAX_RESPONSE_DRAIN)
963 goto out;
964
965 retval = 0;
966out:
967 return retval;
968}
969
970
971static int twa_empty_response_queue_large(TW_Device_Extension *tw_dev)
972{
973 u32 response_que_value = 0;
974 unsigned long before;
975 int retval = 1;
976
977 if (tw_dev->tw_pci_dev->device != PCI_DEVICE_ID_3WARE_9000) {
978 before = jiffies;
979 while ((response_que_value & TW_9550SX_DRAIN_COMPLETED) != TW_9550SX_DRAIN_COMPLETED) {
980 response_que_value = readl(TW_RESPONSE_QUEUE_REG_ADDR_LARGE(tw_dev));
981 msleep(1);
982 if (time_after(jiffies, before + HZ * 30))
983 goto out;
984 }
985
986 msleep(500);
987 retval = 0;
988 } else
989 retval = 0;
990out:
991 return retval;
992}
993
994
995static int twa_fill_sense(TW_Device_Extension *tw_dev, int request_id, int copy_sense, int print_host)
996{
997 TW_Command_Full *full_command_packet;
998 unsigned short error;
999 int retval = 1;
1000 char *error_str;
1001
1002 full_command_packet = tw_dev->command_packet_virt[request_id];
1003
1004
1005 error_str = &(full_command_packet->header.err_specific_desc[strlen(full_command_packet->header.err_specific_desc) + 1]);
1006
1007
1008 error = le16_to_cpu(full_command_packet->header.status_block.error);
1009 if ((error != TW_ERROR_LOGICAL_UNIT_NOT_SUPPORTED) && (error != TW_ERROR_UNIT_OFFLINE)) {
1010 if (print_host)
1011 printk(KERN_WARNING "3w-9xxx: scsi%d: ERROR: (0x%02X:0x%04X): %s:%s.\n",
1012 tw_dev->host->host_no,
1013 TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
1014 full_command_packet->header.status_block.error,
1015 error_str[0] == '\0' ?
1016 twa_string_lookup(twa_error_table,
1017 full_command_packet->header.status_block.error) : error_str,
1018 full_command_packet->header.err_specific_desc);
1019 else
1020 printk(KERN_WARNING "3w-9xxx: ERROR: (0x%02X:0x%04X): %s:%s.\n",
1021 TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
1022 full_command_packet->header.status_block.error,
1023 error_str[0] == '\0' ?
1024 twa_string_lookup(twa_error_table,
1025 full_command_packet->header.status_block.error) : error_str,
1026 full_command_packet->header.err_specific_desc);
1027 }
1028
1029 if (copy_sense) {
1030 memcpy(tw_dev->srb[request_id]->sense_buffer, full_command_packet->header.sense_data, TW_SENSE_DATA_LENGTH);
1031 tw_dev->srb[request_id]->result = (full_command_packet->command.newcommand.status << 1);
1032 retval = TW_ISR_DONT_RESULT;
1033 goto out;
1034 }
1035 retval = 0;
1036out:
1037 return retval;
1038}
1039
1040
1041static void twa_free_device_extension(TW_Device_Extension *tw_dev)
1042{
1043 if (tw_dev->command_packet_virt[0])
1044 pci_free_consistent(tw_dev->tw_pci_dev,
1045 sizeof(TW_Command_Full)*TW_Q_LENGTH,
1046 tw_dev->command_packet_virt[0],
1047 tw_dev->command_packet_phys[0]);
1048
1049 if (tw_dev->generic_buffer_virt[0])
1050 pci_free_consistent(tw_dev->tw_pci_dev,
1051 TW_SECTOR_SIZE*TW_Q_LENGTH,
1052 tw_dev->generic_buffer_virt[0],
1053 tw_dev->generic_buffer_phys[0]);
1054
1055 kfree(tw_dev->event_queue[0]);
1056}
1057
1058
1059static void twa_free_request_id(TW_Device_Extension *tw_dev, int request_id)
1060{
1061 tw_dev->free_queue[tw_dev->free_tail] = request_id;
1062 tw_dev->state[request_id] = TW_S_FINISHED;
1063 tw_dev->free_tail = (tw_dev->free_tail + 1) % TW_Q_LENGTH;
1064}
1065
1066
1067static void *twa_get_param(TW_Device_Extension *tw_dev, int request_id, int table_id, int parameter_id, int parameter_size_bytes)
1068{
1069 TW_Command_Full *full_command_packet;
1070 TW_Command *command_packet;
1071 TW_Param_Apache *param;
1072 void *retval = NULL;
1073
1074
1075 full_command_packet = tw_dev->command_packet_virt[request_id];
1076 memset(full_command_packet, 0, sizeof(TW_Command_Full));
1077 command_packet = &full_command_packet->command.oldcommand;
1078
1079 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_GET_PARAM);
1080 command_packet->size = TW_COMMAND_SIZE;
1081 command_packet->request_id = request_id;
1082 command_packet->byte6_offset.block_count = cpu_to_le16(1);
1083
1084
1085 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
1086 memset(param, 0, TW_SECTOR_SIZE);
1087 param->table_id = cpu_to_le16(table_id | 0x8000);
1088 param->parameter_id = cpu_to_le16(parameter_id);
1089 param->parameter_size_bytes = cpu_to_le16(parameter_size_bytes);
1090
1091 command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
1092 command_packet->byte8_offset.param.sgl[0].length = cpu_to_le32(TW_SECTOR_SIZE);
1093
1094
1095 twa_post_command_packet(tw_dev, request_id, 1);
1096
1097
1098 if (twa_poll_response(tw_dev, request_id, 30))
1099 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x13, "No valid response during get param")
1100 else
1101 retval = (void *)&(param->data[0]);
1102
1103 tw_dev->posted_request_count--;
1104 tw_dev->state[request_id] = TW_S_INITIAL;
1105
1106 return retval;
1107}
1108
1109
1110static void twa_get_request_id(TW_Device_Extension *tw_dev, int *request_id)
1111{
1112 *request_id = tw_dev->free_queue[tw_dev->free_head];
1113 tw_dev->free_head = (tw_dev->free_head + 1) % TW_Q_LENGTH;
1114 tw_dev->state[*request_id] = TW_S_STARTED;
1115}
1116
1117
1118static int twa_initconnection(TW_Device_Extension *tw_dev, int message_credits,
1119 u32 set_features, unsigned short current_fw_srl,
1120 unsigned short current_fw_arch_id,
1121 unsigned short current_fw_branch,
1122 unsigned short current_fw_build,
1123 unsigned short *fw_on_ctlr_srl,
1124 unsigned short *fw_on_ctlr_arch_id,
1125 unsigned short *fw_on_ctlr_branch,
1126 unsigned short *fw_on_ctlr_build,
1127 u32 *init_connect_result)
1128{
1129 TW_Command_Full *full_command_packet;
1130 TW_Initconnect *tw_initconnect;
1131 int request_id = 0, retval = 1;
1132
1133
1134 full_command_packet = tw_dev->command_packet_virt[request_id];
1135 memset(full_command_packet, 0, sizeof(TW_Command_Full));
1136 full_command_packet->header.header_desc.size_header = 128;
1137
1138 tw_initconnect = (TW_Initconnect *)&full_command_packet->command.oldcommand;
1139 tw_initconnect->opcode__reserved = TW_OPRES_IN(0, TW_OP_INIT_CONNECTION);
1140 tw_initconnect->request_id = request_id;
1141 tw_initconnect->message_credits = cpu_to_le16(message_credits);
1142 tw_initconnect->features = set_features;
1143
1144
1145 tw_initconnect->features |= sizeof(dma_addr_t) > 4 ? 1 : 0;
1146
1147 tw_initconnect->features = cpu_to_le32(tw_initconnect->features);
1148
1149 if (set_features & TW_EXTENDED_INIT_CONNECT) {
1150 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE_EXTENDED;
1151 tw_initconnect->fw_srl = cpu_to_le16(current_fw_srl);
1152 tw_initconnect->fw_arch_id = cpu_to_le16(current_fw_arch_id);
1153 tw_initconnect->fw_branch = cpu_to_le16(current_fw_branch);
1154 tw_initconnect->fw_build = cpu_to_le16(current_fw_build);
1155 } else
1156 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE;
1157
1158
1159 twa_post_command_packet(tw_dev, request_id, 1);
1160
1161
1162 if (twa_poll_response(tw_dev, request_id, 30)) {
1163 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x15, "No valid response during init connection");
1164 } else {
1165 if (set_features & TW_EXTENDED_INIT_CONNECT) {
1166 *fw_on_ctlr_srl = le16_to_cpu(tw_initconnect->fw_srl);
1167 *fw_on_ctlr_arch_id = le16_to_cpu(tw_initconnect->fw_arch_id);
1168 *fw_on_ctlr_branch = le16_to_cpu(tw_initconnect->fw_branch);
1169 *fw_on_ctlr_build = le16_to_cpu(tw_initconnect->fw_build);
1170 *init_connect_result = le32_to_cpu(tw_initconnect->result);
1171 }
1172 retval = 0;
1173 }
1174
1175 tw_dev->posted_request_count--;
1176 tw_dev->state[request_id] = TW_S_INITIAL;
1177
1178 return retval;
1179}
1180
1181
1182static int twa_initialize_device_extension(TW_Device_Extension *tw_dev)
1183{
1184 int i, retval = 1;
1185
1186
1187 if (twa_allocate_memory(tw_dev, sizeof(TW_Command_Full), 0)) {
1188 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x16, "Command packet memory allocation failed");
1189 goto out;
1190 }
1191
1192
1193 if (twa_allocate_memory(tw_dev, TW_SECTOR_SIZE, 1)) {
1194 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x17, "Generic memory allocation failed");
1195 goto out;
1196 }
1197
1198
1199 tw_dev->event_queue[0] = kcalloc(TW_Q_LENGTH, sizeof(TW_Event), GFP_KERNEL);
1200 if (!tw_dev->event_queue[0]) {
1201 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x18, "Event info memory allocation failed");
1202 goto out;
1203 }
1204
1205
1206 for (i = 0; i < TW_Q_LENGTH; i++) {
1207 tw_dev->event_queue[i] = (TW_Event *)((unsigned char *)tw_dev->event_queue[0] + (i * sizeof(TW_Event)));
1208 tw_dev->free_queue[i] = i;
1209 tw_dev->state[i] = TW_S_INITIAL;
1210 }
1211
1212 tw_dev->pending_head = TW_Q_START;
1213 tw_dev->pending_tail = TW_Q_START;
1214 tw_dev->free_head = TW_Q_START;
1215 tw_dev->free_tail = TW_Q_START;
1216 tw_dev->error_sequence_id = 1;
1217 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1218
1219 mutex_init(&tw_dev->ioctl_lock);
1220 init_waitqueue_head(&tw_dev->ioctl_wqueue);
1221
1222 retval = 0;
1223out:
1224 return retval;
1225}
1226
1227
1228static irqreturn_t twa_interrupt(int irq, void *dev_instance)
1229{
1230 int request_id, error = 0;
1231 u32 status_reg_value;
1232 TW_Response_Queue response_que;
1233 TW_Command_Full *full_command_packet;
1234 TW_Device_Extension *tw_dev = (TW_Device_Extension *)dev_instance;
1235 int handled = 0;
1236
1237
1238 spin_lock(tw_dev->host->host_lock);
1239
1240
1241 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1242
1243
1244 if (!(status_reg_value & TW_STATUS_VALID_INTERRUPT))
1245 goto twa_interrupt_bail;
1246
1247 handled = 1;
1248
1249
1250 if (test_bit(TW_IN_RESET, &tw_dev->flags))
1251 goto twa_interrupt_bail;
1252
1253
1254 if (twa_check_bits(status_reg_value)) {
1255 if (twa_decode_bits(tw_dev, status_reg_value)) {
1256 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1257 goto twa_interrupt_bail;
1258 }
1259 }
1260
1261
1262 if (status_reg_value & TW_STATUS_HOST_INTERRUPT)
1263 TW_CLEAR_HOST_INTERRUPT(tw_dev);
1264
1265
1266 if (status_reg_value & TW_STATUS_ATTENTION_INTERRUPT) {
1267 TW_CLEAR_ATTENTION_INTERRUPT(tw_dev);
1268 if (!(test_and_set_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags))) {
1269 twa_get_request_id(tw_dev, &request_id);
1270
1271 error = twa_aen_read_queue(tw_dev, request_id);
1272 if (error) {
1273 tw_dev->state[request_id] = TW_S_COMPLETED;
1274 twa_free_request_id(tw_dev, request_id);
1275 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
1276 }
1277 }
1278 }
1279
1280
1281 if (status_reg_value & TW_STATUS_COMMAND_INTERRUPT) {
1282 TW_MASK_COMMAND_INTERRUPT(tw_dev);
1283
1284 while (tw_dev->pending_request_count > 0) {
1285 request_id = tw_dev->pending_queue[tw_dev->pending_head];
1286 if (tw_dev->state[request_id] != TW_S_PENDING) {
1287 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x19, "Found request id that wasn't pending");
1288 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1289 goto twa_interrupt_bail;
1290 }
1291 if (twa_post_command_packet(tw_dev, request_id, 1)==0) {
1292 tw_dev->pending_head = (tw_dev->pending_head + 1) % TW_Q_LENGTH;
1293 tw_dev->pending_request_count--;
1294 } else {
1295
1296 break;
1297 }
1298 }
1299 }
1300
1301
1302 if (status_reg_value & TW_STATUS_RESPONSE_INTERRUPT) {
1303
1304
1305 while ((status_reg_value & TW_STATUS_RESPONSE_QUEUE_EMPTY) == 0) {
1306
1307 response_que.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
1308 request_id = TW_RESID_OUT(response_que.response_id);
1309 full_command_packet = tw_dev->command_packet_virt[request_id];
1310 error = 0;
1311
1312 if (full_command_packet->command.newcommand.status != 0) {
1313 if (tw_dev->srb[request_id] != NULL) {
1314 error = twa_fill_sense(tw_dev, request_id, 1, 1);
1315 } else {
1316
1317 if (request_id != tw_dev->chrdev_request_id) {
1318 error = twa_fill_sense(tw_dev, request_id, 0, 1);
1319 }
1320 }
1321 }
1322
1323
1324 if (tw_dev->state[request_id] != TW_S_POSTED) {
1325 if (tw_dev->srb[request_id] != NULL) {
1326 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1a, "Received a request id that wasn't posted");
1327 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1328 goto twa_interrupt_bail;
1329 }
1330 }
1331
1332
1333 if (tw_dev->srb[request_id] == NULL) {
1334 if (request_id != tw_dev->chrdev_request_id) {
1335 if (twa_aen_complete(tw_dev, request_id))
1336 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1b, "Error completing AEN during attention interrupt");
1337 } else {
1338 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1339 wake_up(&tw_dev->ioctl_wqueue);
1340 }
1341 } else {
1342 struct scsi_cmnd *cmd;
1343
1344 cmd = tw_dev->srb[request_id];
1345
1346 twa_scsiop_execute_scsi_complete(tw_dev, request_id);
1347
1348 if (error == 0) {
1349 cmd->result = (DID_OK << 16);
1350 }
1351
1352
1353 if (error == 1) {
1354
1355 cmd->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
1356 }
1357
1358
1359 if ((scsi_sg_count(cmd) <= 1) && (full_command_packet->command.newcommand.status == 0)) {
1360 if (full_command_packet->command.newcommand.sg_list[0].length < scsi_bufflen(tw_dev->srb[request_id]))
1361 scsi_set_resid(cmd, scsi_bufflen(cmd) - full_command_packet->command.newcommand.sg_list[0].length);
1362 }
1363
1364
1365 if (twa_command_mapped(cmd))
1366 scsi_dma_unmap(cmd);
1367 cmd->scsi_done(cmd);
1368 tw_dev->state[request_id] = TW_S_COMPLETED;
1369 twa_free_request_id(tw_dev, request_id);
1370 tw_dev->posted_request_count--;
1371 }
1372
1373
1374 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1375 if (twa_check_bits(status_reg_value)) {
1376 if (twa_decode_bits(tw_dev, status_reg_value)) {
1377 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1378 goto twa_interrupt_bail;
1379 }
1380 }
1381 }
1382 }
1383
1384twa_interrupt_bail:
1385 spin_unlock(tw_dev->host->host_lock);
1386 return IRQ_RETVAL(handled);
1387}
1388
1389
1390static 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)
1391{
1392 TW_Command *oldcommand;
1393 TW_Command_Apache *newcommand;
1394 TW_SG_Entry *sgl;
1395 unsigned int pae = 0;
1396
1397 if ((sizeof(long) < 8) && (sizeof(dma_addr_t) > 4))
1398 pae = 1;
1399
1400 if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
1401 newcommand = &full_command_packet->command.newcommand;
1402 newcommand->request_id__lunl =
1403 cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->request_id__lunl), request_id));
1404 if (length) {
1405 newcommand->sg_list[0].address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1);
1406 newcommand->sg_list[0].length = cpu_to_le32(length);
1407 }
1408 newcommand->sgl_entries__lunh =
1409 cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->sgl_entries__lunh), length ? 1 : 0));
1410 } else {
1411 oldcommand = &full_command_packet->command.oldcommand;
1412 oldcommand->request_id = request_id;
1413
1414 if (TW_SGL_OUT(oldcommand->opcode__sgloffset)) {
1415
1416 if (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)
1417 sgl = (TW_SG_Entry *)((u32 *)oldcommand+oldcommand->size - (sizeof(TW_SG_Entry)/4) + pae);
1418 else
1419 sgl = (TW_SG_Entry *)((u32 *)oldcommand+TW_SGL_OUT(oldcommand->opcode__sgloffset));
1420 sgl->address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1);
1421 sgl->length = cpu_to_le32(length);
1422
1423 oldcommand->size += pae;
1424 }
1425 }
1426}
1427
1428
1429static int twa_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds)
1430{
1431 int retval = 1, found = 0, response_request_id;
1432 TW_Response_Queue response_queue;
1433 TW_Command_Full *full_command_packet = tw_dev->command_packet_virt[request_id];
1434
1435 if (twa_poll_status_gone(tw_dev, TW_STATUS_RESPONSE_QUEUE_EMPTY, seconds) == 0) {
1436 response_queue.value = readl(TW_RESPONSE_QUEUE_REG_ADDR(tw_dev));
1437 response_request_id = TW_RESID_OUT(response_queue.response_id);
1438 if (request_id != response_request_id) {
1439 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1e, "Found unexpected request id while polling for response");
1440 goto out;
1441 }
1442 if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
1443 if (full_command_packet->command.newcommand.status != 0) {
1444
1445 twa_fill_sense(tw_dev, request_id, 0, 0);
1446 goto out;
1447 }
1448 found = 1;
1449 } else {
1450 if (full_command_packet->command.oldcommand.status != 0) {
1451
1452 twa_fill_sense(tw_dev, request_id, 0, 0);
1453 goto out;
1454 }
1455 found = 1;
1456 }
1457 }
1458
1459 if (found)
1460 retval = 0;
1461out:
1462 return retval;
1463}
1464
1465
1466static int twa_poll_status(TW_Device_Extension *tw_dev, u32 flag, int seconds)
1467{
1468 u32 status_reg_value;
1469 unsigned long before;
1470 int retval = 1;
1471
1472 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1473 before = jiffies;
1474
1475 if (twa_check_bits(status_reg_value))
1476 twa_decode_bits(tw_dev, status_reg_value);
1477
1478 while ((status_reg_value & flag) != flag) {
1479 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1480
1481 if (twa_check_bits(status_reg_value))
1482 twa_decode_bits(tw_dev, status_reg_value);
1483
1484 if (time_after(jiffies, before + HZ * seconds))
1485 goto out;
1486
1487 msleep(50);
1488 }
1489 retval = 0;
1490out:
1491 return retval;
1492}
1493
1494
1495static int twa_poll_status_gone(TW_Device_Extension *tw_dev, u32 flag, int seconds)
1496{
1497 u32 status_reg_value;
1498 unsigned long before;
1499 int retval = 1;
1500
1501 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1502 before = jiffies;
1503
1504 if (twa_check_bits(status_reg_value))
1505 twa_decode_bits(tw_dev, status_reg_value);
1506
1507 while ((status_reg_value & flag) != 0) {
1508 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1509 if (twa_check_bits(status_reg_value))
1510 twa_decode_bits(tw_dev, status_reg_value);
1511
1512 if (time_after(jiffies, before + HZ * seconds))
1513 goto out;
1514
1515 msleep(50);
1516 }
1517 retval = 0;
1518out:
1519 return retval;
1520}
1521
1522
1523static int twa_post_command_packet(TW_Device_Extension *tw_dev, int request_id, char internal)
1524{
1525 u32 status_reg_value;
1526 dma_addr_t command_que_value;
1527 int retval = 1;
1528
1529 command_que_value = tw_dev->command_packet_phys[request_id];
1530
1531
1532 if ((tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
1533 (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)) {
1534 command_que_value += TW_COMMAND_OFFSET;
1535 writel((u32)command_que_value, TW_COMMAND_QUEUE_REG_ADDR_LARGE(tw_dev));
1536 }
1537
1538 status_reg_value = readl(TW_STATUS_REG_ADDR(tw_dev));
1539
1540 if (twa_check_bits(status_reg_value))
1541 twa_decode_bits(tw_dev, status_reg_value);
1542
1543 if (((tw_dev->pending_request_count > 0) && (tw_dev->state[request_id] != TW_S_PENDING)) || (status_reg_value & TW_STATUS_COMMAND_QUEUE_FULL)) {
1544
1545
1546 if (!internal) {
1547 retval = SCSI_MLQUEUE_HOST_BUSY;
1548 goto out;
1549 }
1550
1551
1552 if (tw_dev->state[request_id] != TW_S_PENDING) {
1553 tw_dev->state[request_id] = TW_S_PENDING;
1554 tw_dev->pending_request_count++;
1555 if (tw_dev->pending_request_count > tw_dev->max_pending_request_count) {
1556 tw_dev->max_pending_request_count = tw_dev->pending_request_count;
1557 }
1558 tw_dev->pending_queue[tw_dev->pending_tail] = request_id;
1559 tw_dev->pending_tail = (tw_dev->pending_tail + 1) % TW_Q_LENGTH;
1560 }
1561 TW_UNMASK_COMMAND_INTERRUPT(tw_dev);
1562 goto out;
1563 } else {
1564 if ((tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
1565 (tw_dev->tw_pci_dev->device == PCI_DEVICE_ID_3WARE_9690SA)) {
1566
1567 writel((u32)((u64)command_que_value >> 32), TW_COMMAND_QUEUE_REG_ADDR_LARGE(tw_dev) + 0x4);
1568 } else {
1569 if (sizeof(dma_addr_t) > 4) {
1570 command_que_value += TW_COMMAND_OFFSET;
1571 writel((u32)command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
1572 writel((u32)((u64)command_que_value >> 32), TW_COMMAND_QUEUE_REG_ADDR(tw_dev) + 0x4);
1573 } else {
1574 writel(TW_COMMAND_OFFSET + command_que_value, TW_COMMAND_QUEUE_REG_ADDR(tw_dev));
1575 }
1576 }
1577 tw_dev->state[request_id] = TW_S_POSTED;
1578 tw_dev->posted_request_count++;
1579 if (tw_dev->posted_request_count > tw_dev->max_posted_request_count) {
1580 tw_dev->max_posted_request_count = tw_dev->posted_request_count;
1581 }
1582 }
1583 retval = 0;
1584out:
1585 return retval;
1586}
1587
1588
1589static int twa_reset_device_extension(TW_Device_Extension *tw_dev)
1590{
1591 int i = 0;
1592 int retval = 1;
1593 unsigned long flags = 0;
1594
1595 set_bit(TW_IN_RESET, &tw_dev->flags);
1596 TW_DISABLE_INTERRUPTS(tw_dev);
1597 TW_MASK_COMMAND_INTERRUPT(tw_dev);
1598 spin_lock_irqsave(tw_dev->host->host_lock, flags);
1599
1600
1601 for (i = 0; i < TW_Q_LENGTH; i++) {
1602 if ((tw_dev->state[i] != TW_S_FINISHED) &&
1603 (tw_dev->state[i] != TW_S_INITIAL) &&
1604 (tw_dev->state[i] != TW_S_COMPLETED)) {
1605 if (tw_dev->srb[i]) {
1606 struct scsi_cmnd *cmd = tw_dev->srb[i];
1607
1608 cmd->result = (DID_RESET << 16);
1609 if (twa_command_mapped(cmd))
1610 scsi_dma_unmap(cmd);
1611 cmd->scsi_done(cmd);
1612 }
1613 }
1614 }
1615
1616
1617 for (i = 0; i < TW_Q_LENGTH; i++) {
1618 tw_dev->free_queue[i] = i;
1619 tw_dev->state[i] = TW_S_INITIAL;
1620 }
1621 tw_dev->free_head = TW_Q_START;
1622 tw_dev->free_tail = TW_Q_START;
1623 tw_dev->posted_request_count = 0;
1624 tw_dev->pending_request_count = 0;
1625 tw_dev->pending_head = TW_Q_START;
1626 tw_dev->pending_tail = TW_Q_START;
1627 tw_dev->reset_print = 0;
1628
1629 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
1630
1631 if (twa_reset_sequence(tw_dev, 1))
1632 goto out;
1633
1634 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
1635 clear_bit(TW_IN_RESET, &tw_dev->flags);
1636 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1637
1638 retval = 0;
1639out:
1640 return retval;
1641}
1642
1643
1644static int twa_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset)
1645{
1646 int tries = 0, retval = 1, flashed = 0, do_soft_reset = soft_reset;
1647
1648 while (tries < TW_MAX_RESET_TRIES) {
1649 if (do_soft_reset) {
1650 TW_SOFT_RESET(tw_dev);
1651
1652 if (twa_empty_response_queue_large(tw_dev)) {
1653 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x36, "Response queue (large) empty failed during reset sequence");
1654 do_soft_reset = 1;
1655 tries++;
1656 continue;
1657 }
1658 }
1659
1660
1661 if (twa_poll_status(tw_dev, TW_STATUS_MICROCONTROLLER_READY | (do_soft_reset == 1 ? TW_STATUS_ATTENTION_INTERRUPT : 0), 60)) {
1662 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1f, "Microcontroller not ready during reset sequence");
1663 do_soft_reset = 1;
1664 tries++;
1665 continue;
1666 }
1667
1668
1669 if (twa_empty_response_queue(tw_dev)) {
1670 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x20, "Response queue empty failed during reset sequence");
1671 do_soft_reset = 1;
1672 tries++;
1673 continue;
1674 }
1675
1676 flashed = 0;
1677
1678
1679 if (twa_check_srl(tw_dev, &flashed)) {
1680 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x21, "Compatibility check failed during reset sequence");
1681 do_soft_reset = 1;
1682 tries++;
1683 continue;
1684 } else {
1685 if (flashed) {
1686 tries++;
1687 continue;
1688 }
1689 }
1690
1691
1692 if (twa_aen_drain_queue(tw_dev, soft_reset)) {
1693 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x22, "AEN drain failed during reset sequence");
1694 do_soft_reset = 1;
1695 tries++;
1696 continue;
1697 }
1698
1699
1700 retval = 0;
1701 goto out;
1702 }
1703out:
1704 return retval;
1705}
1706
1707
1708static int twa_scsi_biosparam(struct scsi_device *sdev, struct block_device *bdev, sector_t capacity, int geom[])
1709{
1710 int heads, sectors, cylinders;
1711 TW_Device_Extension *tw_dev;
1712
1713 tw_dev = (TW_Device_Extension *)sdev->host->hostdata;
1714
1715 if (capacity >= 0x200000) {
1716 heads = 255;
1717 sectors = 63;
1718 cylinders = sector_div(capacity, heads * sectors);
1719 } else {
1720 heads = 64;
1721 sectors = 32;
1722 cylinders = sector_div(capacity, heads * sectors);
1723 }
1724
1725 geom[0] = heads;
1726 geom[1] = sectors;
1727 geom[2] = cylinders;
1728
1729 return 0;
1730}
1731
1732
1733static int twa_scsi_eh_reset(struct scsi_cmnd *SCpnt)
1734{
1735 TW_Device_Extension *tw_dev = NULL;
1736 int retval = FAILED;
1737
1738 tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1739
1740 tw_dev->num_resets++;
1741
1742 sdev_printk(KERN_WARNING, SCpnt->device,
1743 "WARNING: (0x%02X:0x%04X): Command (0x%x) timed out, resetting card.\n",
1744 TW_DRIVER, 0x2c, SCpnt->cmnd[0]);
1745
1746
1747 mutex_lock(&tw_dev->ioctl_lock);
1748
1749
1750 if (twa_reset_device_extension(tw_dev)) {
1751 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2b, "Controller reset failed during scsi host reset");
1752 goto out;
1753 }
1754
1755 retval = SUCCESS;
1756out:
1757 mutex_unlock(&tw_dev->ioctl_lock);
1758 return retval;
1759}
1760
1761
1762static int twa_scsi_queue_lck(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
1763{
1764 int request_id, retval;
1765 TW_Device_Extension *tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1766
1767
1768 if (test_bit(TW_IN_RESET, &tw_dev->flags)) {
1769 retval = SCSI_MLQUEUE_HOST_BUSY;
1770 goto out;
1771 }
1772
1773
1774 if ((SCpnt->device->lun != 0) && (tw_dev->tw_compat_info.working_srl < TW_FW_SRL_LUNS_SUPPORTED)) {
1775 SCpnt->result = (DID_BAD_TARGET << 16);
1776 done(SCpnt);
1777 retval = 0;
1778 goto out;
1779 }
1780
1781
1782 SCpnt->scsi_done = done;
1783
1784
1785 twa_get_request_id(tw_dev, &request_id);
1786
1787
1788 tw_dev->srb[request_id] = SCpnt;
1789
1790 retval = twa_scsiop_execute_scsi(tw_dev, request_id, NULL, 0, NULL);
1791 switch (retval) {
1792 case SCSI_MLQUEUE_HOST_BUSY:
1793 if (twa_command_mapped(SCpnt))
1794 scsi_dma_unmap(SCpnt);
1795 twa_free_request_id(tw_dev, request_id);
1796 break;
1797 case 1:
1798 SCpnt->result = (DID_ERROR << 16);
1799 if (twa_command_mapped(SCpnt))
1800 scsi_dma_unmap(SCpnt);
1801 done(SCpnt);
1802 tw_dev->state[request_id] = TW_S_COMPLETED;
1803 twa_free_request_id(tw_dev, request_id);
1804 retval = 0;
1805 }
1806out:
1807 return retval;
1808}
1809
1810static DEF_SCSI_QCMD(twa_scsi_queue)
1811
1812
1813static int twa_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id, char *cdb, int use_sg, TW_SG_Entry *sglistarg)
1814{
1815 TW_Command_Full *full_command_packet;
1816 TW_Command_Apache *command_packet;
1817 u32 num_sectors = 0x0;
1818 int i, sg_count;
1819 struct scsi_cmnd *srb = NULL;
1820 struct scatterlist *sglist = NULL, *sg;
1821 int retval = 1;
1822
1823 if (tw_dev->srb[request_id]) {
1824 srb = tw_dev->srb[request_id];
1825 if (scsi_sglist(srb))
1826 sglist = scsi_sglist(srb);
1827 }
1828
1829
1830 full_command_packet = tw_dev->command_packet_virt[request_id];
1831 full_command_packet->header.header_desc.size_header = 128;
1832 full_command_packet->header.status_block.error = 0;
1833 full_command_packet->header.status_block.severity__reserved = 0;
1834
1835 command_packet = &full_command_packet->command.newcommand;
1836 command_packet->status = 0;
1837 command_packet->opcode__reserved = TW_OPRES_IN(0, TW_OP_EXECUTE_SCSI);
1838
1839
1840 if (!cdb)
1841 memcpy(command_packet->cdb, srb->cmnd, TW_MAX_CDB_LEN);
1842 else
1843 memcpy(command_packet->cdb, cdb, TW_MAX_CDB_LEN);
1844
1845 if (srb) {
1846 command_packet->unit = srb->device->id;
1847 command_packet->request_id__lunl =
1848 cpu_to_le16(TW_REQ_LUN_IN(srb->device->lun, request_id));
1849 } else {
1850 command_packet->request_id__lunl =
1851 cpu_to_le16(TW_REQ_LUN_IN(0, request_id));
1852 command_packet->unit = 0;
1853 }
1854
1855 command_packet->sgl_offset = 16;
1856
1857 if (!sglistarg) {
1858
1859
1860 if (scsi_sg_count(srb)) {
1861 if (!twa_command_mapped(srb)) {
1862 if (srb->sc_data_direction == DMA_TO_DEVICE ||
1863 srb->sc_data_direction == DMA_BIDIRECTIONAL)
1864 scsi_sg_copy_to_buffer(srb,
1865 tw_dev->generic_buffer_virt[request_id],
1866 TW_SECTOR_SIZE);
1867 command_packet->sg_list[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
1868 command_packet->sg_list[0].length = cpu_to_le32(TW_MIN_SGL_LENGTH);
1869 } else {
1870 sg_count = scsi_dma_map(srb);
1871 if (sg_count < 0)
1872 goto out;
1873
1874 scsi_for_each_sg(srb, sg, sg_count, i) {
1875 command_packet->sg_list[i].address = TW_CPU_TO_SGL(sg_dma_address(sg));
1876 command_packet->sg_list[i].length = cpu_to_le32(sg_dma_len(sg));
1877 if (command_packet->sg_list[i].address & TW_CPU_TO_SGL(TW_ALIGNMENT_9000_SGL)) {
1878 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2e, "Found unaligned sgl address during execute scsi");
1879 goto out;
1880 }
1881 }
1882 }
1883 command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN((srb->device->lun >> 4), scsi_sg_count(tw_dev->srb[request_id])));
1884 }
1885 } else {
1886
1887 for (i = 0; i < use_sg; i++) {
1888 command_packet->sg_list[i].address = TW_CPU_TO_SGL(sglistarg[i].address);
1889 command_packet->sg_list[i].length = cpu_to_le32(sglistarg[i].length);
1890 if (command_packet->sg_list[i].address & TW_CPU_TO_SGL(TW_ALIGNMENT_9000_SGL)) {
1891 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2f, "Found unaligned sgl address during internal post");
1892 goto out;
1893 }
1894 }
1895 command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN(0, use_sg));
1896 }
1897
1898 if (srb) {
1899 if (srb->cmnd[0] == READ_6 || srb->cmnd[0] == WRITE_6)
1900 num_sectors = (u32)srb->cmnd[4];
1901
1902 if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10)
1903 num_sectors = (u32)srb->cmnd[8] | ((u32)srb->cmnd[7] << 8);
1904 }
1905
1906
1907 tw_dev->sector_count = num_sectors;
1908 if (tw_dev->sector_count > tw_dev->max_sector_count)
1909 tw_dev->max_sector_count = tw_dev->sector_count;
1910
1911
1912 if (srb) {
1913 tw_dev->sgl_entries = scsi_sg_count(tw_dev->srb[request_id]);
1914 if (tw_dev->sgl_entries > tw_dev->max_sgl_entries)
1915 tw_dev->max_sgl_entries = tw_dev->sgl_entries;
1916 }
1917
1918
1919 if (srb) {
1920 retval = twa_post_command_packet(tw_dev, request_id, 0);
1921 } else {
1922 twa_post_command_packet(tw_dev, request_id, 1);
1923 retval = 0;
1924 }
1925out:
1926 return retval;
1927}
1928
1929
1930static void twa_scsiop_execute_scsi_complete(TW_Device_Extension *tw_dev, int request_id)
1931{
1932 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
1933
1934 if (!twa_command_mapped(cmd) &&
1935 (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1936 cmd->sc_data_direction == DMA_BIDIRECTIONAL)) {
1937 if (scsi_sg_count(cmd) == 1) {
1938 void *buf = tw_dev->generic_buffer_virt[request_id];
1939
1940 scsi_sg_copy_from_buffer(cmd, buf, TW_SECTOR_SIZE);
1941 }
1942 }
1943}
1944
1945
1946static void __twa_shutdown(TW_Device_Extension *tw_dev)
1947{
1948
1949 TW_DISABLE_INTERRUPTS(tw_dev);
1950
1951
1952 free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
1953
1954 printk(KERN_WARNING "3w-9xxx: Shutting down host %d.\n", tw_dev->host->host_no);
1955
1956
1957 if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
1958 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x31, "Connection shutdown failed");
1959 } else {
1960 printk(KERN_WARNING "3w-9xxx: Shutdown complete.\n");
1961 }
1962
1963
1964 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
1965}
1966
1967
1968static void twa_shutdown(struct pci_dev *pdev)
1969{
1970 struct Scsi_Host *host = pci_get_drvdata(pdev);
1971 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
1972
1973 __twa_shutdown(tw_dev);
1974}
1975
1976
1977static char *twa_string_lookup(twa_message_type *table, unsigned int code)
1978{
1979 int index;
1980
1981 for (index = 0; ((code != table[index].code) &&
1982 (table[index].text != (char *)0)); index++);
1983 return(table[index].text);
1984}
1985
1986
1987static int twa_slave_configure(struct scsi_device *sdev)
1988{
1989
1990 blk_queue_rq_timeout(sdev->request_queue, 60 * HZ);
1991
1992 return 0;
1993}
1994
1995
1996static struct scsi_host_template driver_template = {
1997 .module = THIS_MODULE,
1998 .name = "3ware 9000 Storage Controller",
1999 .queuecommand = twa_scsi_queue,
2000 .eh_host_reset_handler = twa_scsi_eh_reset,
2001 .bios_param = twa_scsi_biosparam,
2002 .change_queue_depth = twa_change_queue_depth,
2003 .can_queue = TW_Q_LENGTH-2,
2004 .slave_configure = twa_slave_configure,
2005 .this_id = -1,
2006 .sg_tablesize = TW_APACHE_MAX_SGL_LENGTH,
2007 .max_sectors = TW_MAX_SECTORS,
2008 .cmd_per_lun = TW_MAX_CMDS_PER_LUN,
2009 .use_clustering = ENABLE_CLUSTERING,
2010 .shost_attrs = twa_host_attrs,
2011 .emulated = 1,
2012 .no_write_same = 1,
2013};
2014
2015
2016static int twa_probe(struct pci_dev *pdev, const struct pci_device_id *dev_id)
2017{
2018 struct Scsi_Host *host = NULL;
2019 TW_Device_Extension *tw_dev;
2020 unsigned long mem_addr, mem_len;
2021 int retval = -ENODEV;
2022
2023 retval = pci_enable_device(pdev);
2024 if (retval) {
2025 TW_PRINTK(host, TW_DRIVER, 0x34, "Failed to enable pci device");
2026 goto out_disable_device;
2027 }
2028
2029 pci_set_master(pdev);
2030 pci_try_set_mwi(pdev);
2031
2032 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))
2033 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
2034 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
2035 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
2036 TW_PRINTK(host, TW_DRIVER, 0x23, "Failed to set dma mask");
2037 retval = -ENODEV;
2038 goto out_disable_device;
2039 }
2040
2041 host = scsi_host_alloc(&driver_template, sizeof(TW_Device_Extension));
2042 if (!host) {
2043 TW_PRINTK(host, TW_DRIVER, 0x24, "Failed to allocate memory for device extension");
2044 retval = -ENOMEM;
2045 goto out_disable_device;
2046 }
2047 tw_dev = (TW_Device_Extension *)host->hostdata;
2048
2049
2050 tw_dev->host = host;
2051 tw_dev->tw_pci_dev = pdev;
2052
2053 if (twa_initialize_device_extension(tw_dev)) {
2054 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x25, "Failed to initialize device extension");
2055 goto out_free_device_extension;
2056 }
2057
2058
2059 retval = pci_request_regions(pdev, "3w-9xxx");
2060 if (retval) {
2061 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x26, "Failed to get mem region");
2062 goto out_free_device_extension;
2063 }
2064
2065 if (pdev->device == PCI_DEVICE_ID_3WARE_9000) {
2066 mem_addr = pci_resource_start(pdev, 1);
2067 mem_len = pci_resource_len(pdev, 1);
2068 } else {
2069 mem_addr = pci_resource_start(pdev, 2);
2070 mem_len = pci_resource_len(pdev, 2);
2071 }
2072
2073
2074 tw_dev->base_addr = ioremap(mem_addr, mem_len);
2075 if (!tw_dev->base_addr) {
2076 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x35, "Failed to ioremap");
2077 goto out_release_mem_region;
2078 }
2079
2080
2081 TW_DISABLE_INTERRUPTS(tw_dev);
2082
2083
2084 if (twa_reset_sequence(tw_dev, 0))
2085 goto out_iounmap;
2086
2087
2088 if ((pdev->device == PCI_DEVICE_ID_3WARE_9650SE) ||
2089 (pdev->device == PCI_DEVICE_ID_3WARE_9690SA))
2090 host->max_id = TW_MAX_UNITS_9650SE;
2091 else
2092 host->max_id = TW_MAX_UNITS;
2093
2094 host->max_cmd_len = TW_MAX_CDB_LEN;
2095
2096
2097 host->max_lun = TW_MAX_LUNS(tw_dev->tw_compat_info.working_srl);
2098 host->max_channel = 0;
2099
2100
2101 retval = scsi_add_host(host, &pdev->dev);
2102 if (retval) {
2103 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x27, "scsi add host failed");
2104 goto out_iounmap;
2105 }
2106
2107 pci_set_drvdata(pdev, host);
2108
2109 printk(KERN_WARNING "3w-9xxx: scsi%d: Found a 3ware 9000 Storage Controller at 0x%lx, IRQ: %d.\n",
2110 host->host_no, mem_addr, pdev->irq);
2111 printk(KERN_WARNING "3w-9xxx: scsi%d: Firmware %s, BIOS %s, Ports: %d.\n",
2112 host->host_no,
2113 (char *)twa_get_param(tw_dev, 0, TW_VERSION_TABLE,
2114 TW_PARAM_FWVER, TW_PARAM_FWVER_LENGTH),
2115 (char *)twa_get_param(tw_dev, 1, TW_VERSION_TABLE,
2116 TW_PARAM_BIOSVER, TW_PARAM_BIOSVER_LENGTH),
2117 le32_to_cpu(*(int *)twa_get_param(tw_dev, 2, TW_INFORMATION_TABLE,
2118 TW_PARAM_PORTCOUNT, TW_PARAM_PORTCOUNT_LENGTH)));
2119
2120
2121 if (use_msi && (pdev->device != PCI_DEVICE_ID_3WARE_9000) &&
2122 !pci_enable_msi(pdev))
2123 set_bit(TW_USING_MSI, &tw_dev->flags);
2124
2125
2126 retval = request_irq(pdev->irq, twa_interrupt, IRQF_SHARED, "3w-9xxx", tw_dev);
2127 if (retval) {
2128 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x30, "Error requesting IRQ");
2129 goto out_remove_host;
2130 }
2131
2132 twa_device_extension_list[twa_device_extension_count] = tw_dev;
2133 twa_device_extension_count++;
2134
2135
2136 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
2137
2138
2139 scsi_scan_host(host);
2140
2141 if (twa_major == -1) {
2142 if ((twa_major = register_chrdev (0, "twa", &twa_fops)) < 0)
2143 TW_PRINTK(host, TW_DRIVER, 0x29, "Failed to register character device");
2144 }
2145 return 0;
2146
2147out_remove_host:
2148 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2149 pci_disable_msi(pdev);
2150 scsi_remove_host(host);
2151out_iounmap:
2152 iounmap(tw_dev->base_addr);
2153out_release_mem_region:
2154 pci_release_regions(pdev);
2155out_free_device_extension:
2156 twa_free_device_extension(tw_dev);
2157 scsi_host_put(host);
2158out_disable_device:
2159 pci_disable_device(pdev);
2160
2161 return retval;
2162}
2163
2164
2165static void twa_remove(struct pci_dev *pdev)
2166{
2167 struct Scsi_Host *host = pci_get_drvdata(pdev);
2168 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2169
2170 scsi_remove_host(tw_dev->host);
2171
2172
2173 if (twa_major >= 0) {
2174 unregister_chrdev(twa_major, "twa");
2175 twa_major = -1;
2176 }
2177
2178
2179 __twa_shutdown(tw_dev);
2180
2181
2182 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2183 pci_disable_msi(pdev);
2184
2185
2186 iounmap(tw_dev->base_addr);
2187
2188
2189 pci_release_regions(pdev);
2190
2191
2192 twa_free_device_extension(tw_dev);
2193
2194 scsi_host_put(tw_dev->host);
2195 pci_disable_device(pdev);
2196 twa_device_extension_count--;
2197}
2198
2199#ifdef CONFIG_PM
2200
2201static int twa_suspend(struct pci_dev *pdev, pm_message_t state)
2202{
2203 struct Scsi_Host *host = pci_get_drvdata(pdev);
2204 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2205
2206 printk(KERN_WARNING "3w-9xxx: Suspending host %d.\n", tw_dev->host->host_no);
2207
2208 TW_DISABLE_INTERRUPTS(tw_dev);
2209 free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
2210
2211 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2212 pci_disable_msi(pdev);
2213
2214
2215 if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
2216 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x38, "Connection shutdown failed during suspend");
2217 } else {
2218 printk(KERN_WARNING "3w-9xxx: Suspend complete.\n");
2219 }
2220 TW_CLEAR_ALL_INTERRUPTS(tw_dev);
2221
2222 pci_save_state(pdev);
2223 pci_disable_device(pdev);
2224 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2225
2226 return 0;
2227}
2228
2229
2230static int twa_resume(struct pci_dev *pdev)
2231{
2232 int retval = 0;
2233 struct Scsi_Host *host = pci_get_drvdata(pdev);
2234 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
2235
2236 printk(KERN_WARNING "3w-9xxx: Resuming host %d.\n", tw_dev->host->host_no);
2237 pci_set_power_state(pdev, PCI_D0);
2238 pci_enable_wake(pdev, PCI_D0, 0);
2239 pci_restore_state(pdev);
2240
2241 retval = pci_enable_device(pdev);
2242 if (retval) {
2243 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x39, "Enable device failed during resume");
2244 return retval;
2245 }
2246
2247 pci_set_master(pdev);
2248 pci_try_set_mwi(pdev);
2249
2250 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))
2251 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
2252 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
2253 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
2254 TW_PRINTK(host, TW_DRIVER, 0x40, "Failed to set dma mask during resume");
2255 retval = -ENODEV;
2256 goto out_disable_device;
2257 }
2258
2259
2260 if (twa_reset_sequence(tw_dev, 0)) {
2261 retval = -ENODEV;
2262 goto out_disable_device;
2263 }
2264
2265
2266 retval = request_irq(pdev->irq, twa_interrupt, IRQF_SHARED, "3w-9xxx", tw_dev);
2267 if (retval) {
2268 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x42, "Error requesting IRQ during resume");
2269 retval = -ENODEV;
2270 goto out_disable_device;
2271 }
2272
2273
2274 if (test_bit(TW_USING_MSI, &tw_dev->flags))
2275 pci_enable_msi(pdev);
2276
2277
2278 TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
2279
2280 printk(KERN_WARNING "3w-9xxx: Resume complete.\n");
2281 return 0;
2282
2283out_disable_device:
2284 scsi_remove_host(host);
2285 pci_disable_device(pdev);
2286
2287 return retval;
2288}
2289#endif
2290
2291
2292static struct pci_device_id twa_pci_tbl[] = {
2293 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9000,
2294 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2295 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9550SX,
2296 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2297 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9650SE,
2298 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2299 { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9690SA,
2300 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2301 { }
2302};
2303MODULE_DEVICE_TABLE(pci, twa_pci_tbl);
2304
2305
2306static struct pci_driver twa_driver = {
2307 .name = "3w-9xxx",
2308 .id_table = twa_pci_tbl,
2309 .probe = twa_probe,
2310 .remove = twa_remove,
2311#ifdef CONFIG_PM
2312 .suspend = twa_suspend,
2313 .resume = twa_resume,
2314#endif
2315 .shutdown = twa_shutdown
2316};
2317
2318
2319static int __init twa_init(void)
2320{
2321 printk(KERN_WARNING "3ware 9000 Storage Controller device driver for Linux v%s.\n", TW_DRIVER_VERSION);
2322
2323 return pci_register_driver(&twa_driver);
2324}
2325
2326
2327static void __exit twa_exit(void)
2328{
2329 pci_unregister_driver(&twa_driver);
2330}
2331
2332module_init(twa_init);
2333module_exit(twa_exit);
2334
2335