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#include <linux/module.h>
57#include <linux/reboot.h>
58#include <linux/spinlock.h>
59#include <linux/interrupt.h>
60#include <linux/moduleparam.h>
61#include <linux/errno.h>
62#include <linux/types.h>
63#include <linux/delay.h>
64#include <linux/pci.h>
65#include <linux/time.h>
66#include <linux/mutex.h>
67#include <linux/slab.h>
68#include <asm/io.h>
69#include <asm/irq.h>
70#include <asm/uaccess.h>
71#include <scsi/scsi.h>
72#include <scsi/scsi_host.h>
73#include <scsi/scsi_tcq.h>
74#include <scsi/scsi_cmnd.h>
75#include "3w-sas.h"
76
77
78#define TW_DRIVER_VERSION "3.26.02.000"
79static DEFINE_MUTEX(twl_chrdev_mutex);
80static TW_Device_Extension *twl_device_extension_list[TW_MAX_SLOT];
81static unsigned int twl_device_extension_count;
82static int twl_major = -1;
83extern struct timezone sys_tz;
84
85
86MODULE_AUTHOR ("LSI");
87MODULE_DESCRIPTION ("LSI 3ware SAS/SATA-RAID Linux Driver");
88MODULE_LICENSE("GPL");
89MODULE_VERSION(TW_DRIVER_VERSION);
90
91static int use_msi;
92module_param(use_msi, int, S_IRUGO);
93MODULE_PARM_DESC(use_msi, "Use Message Signaled Interrupts. Default: 0");
94
95
96static int twl_reset_device_extension(TW_Device_Extension *tw_dev, int ioctl_reset);
97
98
99
100
101static ssize_t twl_sysfs_aen_read(struct file *filp, struct kobject *kobj,
102 struct bin_attribute *bin_attr,
103 char *outbuf, loff_t offset, size_t count)
104{
105 struct device *dev = container_of(kobj, struct device, kobj);
106 struct Scsi_Host *shost = class_to_shost(dev);
107 TW_Device_Extension *tw_dev = (TW_Device_Extension *)shost->hostdata;
108 unsigned long flags = 0;
109 ssize_t ret;
110
111 if (!capable(CAP_SYS_ADMIN))
112 return -EACCES;
113
114 spin_lock_irqsave(tw_dev->host->host_lock, flags);
115 ret = memory_read_from_buffer(outbuf, count, &offset, tw_dev->event_queue[0], sizeof(TW_Event) * TW_Q_LENGTH);
116 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
117
118 return ret;
119}
120
121
122static struct bin_attribute twl_sysfs_aen_read_attr = {
123 .attr = {
124 .name = "3ware_aen_read",
125 .mode = S_IRUSR,
126 },
127 .size = 0,
128 .read = twl_sysfs_aen_read
129};
130
131
132static ssize_t twl_sysfs_compat_info(struct file *filp, struct kobject *kobj,
133 struct bin_attribute *bin_attr,
134 char *outbuf, loff_t offset, size_t count)
135{
136 struct device *dev = container_of(kobj, struct device, kobj);
137 struct Scsi_Host *shost = class_to_shost(dev);
138 TW_Device_Extension *tw_dev = (TW_Device_Extension *)shost->hostdata;
139 unsigned long flags = 0;
140 ssize_t ret;
141
142 if (!capable(CAP_SYS_ADMIN))
143 return -EACCES;
144
145 spin_lock_irqsave(tw_dev->host->host_lock, flags);
146 ret = memory_read_from_buffer(outbuf, count, &offset, &tw_dev->tw_compat_info, sizeof(TW_Compatibility_Info));
147 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
148
149 return ret;
150}
151
152
153static struct bin_attribute twl_sysfs_compat_info_attr = {
154 .attr = {
155 .name = "3ware_compat_info",
156 .mode = S_IRUSR,
157 },
158 .size = 0,
159 .read = twl_sysfs_compat_info
160};
161
162
163static ssize_t twl_show_stats(struct device *dev,
164 struct device_attribute *attr, char *buf)
165{
166 struct Scsi_Host *host = class_to_shost(dev);
167 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
168 unsigned long flags = 0;
169 ssize_t len;
170
171 spin_lock_irqsave(tw_dev->host->host_lock, flags);
172 len = snprintf(buf, PAGE_SIZE, "3w-sas Driver version: %s\n"
173 "Current commands posted: %4d\n"
174 "Max commands posted: %4d\n"
175 "Last sgl length: %4d\n"
176 "Max sgl length: %4d\n"
177 "Last sector count: %4d\n"
178 "Max sector count: %4d\n"
179 "SCSI Host Resets: %4d\n"
180 "AEN's: %4d\n",
181 TW_DRIVER_VERSION,
182 tw_dev->posted_request_count,
183 tw_dev->max_posted_request_count,
184 tw_dev->sgl_entries,
185 tw_dev->max_sgl_entries,
186 tw_dev->sector_count,
187 tw_dev->max_sector_count,
188 tw_dev->num_resets,
189 tw_dev->aen_count);
190 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
191 return len;
192}
193
194
195static int twl_change_queue_depth(struct scsi_device *sdev, int queue_depth,
196 int reason)
197{
198 if (reason != SCSI_QDEPTH_DEFAULT)
199 return -EOPNOTSUPP;
200
201 if (queue_depth > TW_Q_LENGTH-2)
202 queue_depth = TW_Q_LENGTH-2;
203 scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, queue_depth);
204 return queue_depth;
205}
206
207
208static struct device_attribute twl_host_stats_attr = {
209 .attr = {
210 .name = "3ware_stats",
211 .mode = S_IRUGO,
212 },
213 .show = twl_show_stats
214};
215
216
217static struct device_attribute *twl_host_attrs[] = {
218 &twl_host_stats_attr,
219 NULL,
220};
221
222
223static char *twl_aen_severity_lookup(unsigned char severity_code)
224{
225 char *retval = NULL;
226
227 if ((severity_code < (unsigned char) TW_AEN_SEVERITY_ERROR) ||
228 (severity_code > (unsigned char) TW_AEN_SEVERITY_DEBUG))
229 goto out;
230
231 retval = twl_aen_severity_table[severity_code];
232out:
233 return retval;
234}
235
236
237static void twl_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header)
238{
239 u32 local_time;
240 struct timeval time;
241 TW_Event *event;
242 unsigned short aen;
243 char host[16];
244 char *error_str;
245
246 tw_dev->aen_count++;
247
248
249 event = tw_dev->event_queue[tw_dev->error_index];
250
251 host[0] = '\0';
252 if (tw_dev->host)
253 sprintf(host, " scsi%d:", tw_dev->host->host_no);
254
255 aen = le16_to_cpu(header->status_block.error);
256 memset(event, 0, sizeof(TW_Event));
257
258 event->severity = TW_SEV_OUT(header->status_block.severity__reserved);
259 do_gettimeofday(&time);
260 local_time = (u32)(time.tv_sec - (sys_tz.tz_minuteswest * 60));
261 event->time_stamp_sec = local_time;
262 event->aen_code = aen;
263 event->retrieved = TW_AEN_NOT_RETRIEVED;
264 event->sequence_id = tw_dev->error_sequence_id;
265 tw_dev->error_sequence_id++;
266
267
268 error_str = &(header->err_specific_desc[strlen(header->err_specific_desc)+1]);
269
270 header->err_specific_desc[sizeof(header->err_specific_desc) - 1] = '\0';
271 event->parameter_len = strlen(header->err_specific_desc);
272 memcpy(event->parameter_data, header->err_specific_desc, event->parameter_len + 1 + strlen(error_str));
273 if (event->severity != TW_AEN_SEVERITY_DEBUG)
274 printk(KERN_WARNING "3w-sas:%s AEN: %s (0x%02X:0x%04X): %s:%s.\n",
275 host,
276 twl_aen_severity_lookup(TW_SEV_OUT(header->status_block.severity__reserved)),
277 TW_MESSAGE_SOURCE_CONTROLLER_EVENT, aen, error_str,
278 header->err_specific_desc);
279 else
280 tw_dev->aen_count--;
281
282 tw_dev->error_index = (tw_dev->error_index + 1 ) % TW_Q_LENGTH;
283}
284
285
286static int twl_post_command_packet(TW_Device_Extension *tw_dev, int request_id)
287{
288 dma_addr_t command_que_value;
289
290 command_que_value = tw_dev->command_packet_phys[request_id];
291 command_que_value += TW_COMMAND_OFFSET;
292
293
294 writel((u32)((u64)command_que_value >> 32), TWL_HIBQPH_REG_ADDR(tw_dev));
295
296 writel((u32)(command_que_value | TWL_PULL_MODE), TWL_HIBQPL_REG_ADDR(tw_dev));
297
298 tw_dev->state[request_id] = TW_S_POSTED;
299 tw_dev->posted_request_count++;
300 if (tw_dev->posted_request_count > tw_dev->max_posted_request_count)
301 tw_dev->max_posted_request_count = tw_dev->posted_request_count;
302
303 return 0;
304}
305
306
307static int twl_map_scsi_sg_data(TW_Device_Extension *tw_dev, int request_id)
308{
309 int use_sg;
310 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
311
312 use_sg = scsi_dma_map(cmd);
313 if (!use_sg)
314 return 0;
315 else if (use_sg < 0) {
316 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1, "Failed to map scatter gather list");
317 return 0;
318 }
319
320 cmd->SCp.phase = TW_PHASE_SGLIST;
321 cmd->SCp.have_data_in = use_sg;
322
323 return use_sg;
324}
325
326
327static int twl_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id, char *cdb, int use_sg, TW_SG_Entry_ISO *sglistarg)
328{
329 TW_Command_Full *full_command_packet;
330 TW_Command_Apache *command_packet;
331 int i, sg_count;
332 struct scsi_cmnd *srb = NULL;
333 struct scatterlist *sglist = NULL, *sg;
334 int retval = 1;
335
336 if (tw_dev->srb[request_id]) {
337 srb = tw_dev->srb[request_id];
338 if (scsi_sglist(srb))
339 sglist = scsi_sglist(srb);
340 }
341
342
343 full_command_packet = tw_dev->command_packet_virt[request_id];
344 full_command_packet->header.header_desc.size_header = 128;
345 full_command_packet->header.status_block.error = 0;
346 full_command_packet->header.status_block.severity__reserved = 0;
347
348 command_packet = &full_command_packet->command.newcommand;
349 command_packet->status = 0;
350 command_packet->opcode__reserved = TW_OPRES_IN(0, TW_OP_EXECUTE_SCSI);
351
352
353 if (!cdb)
354 memcpy(command_packet->cdb, srb->cmnd, TW_MAX_CDB_LEN);
355 else
356 memcpy(command_packet->cdb, cdb, TW_MAX_CDB_LEN);
357
358 if (srb) {
359 command_packet->unit = srb->device->id;
360 command_packet->request_id__lunl =
361 cpu_to_le16(TW_REQ_LUN_IN(srb->device->lun, request_id));
362 } else {
363 command_packet->request_id__lunl =
364 cpu_to_le16(TW_REQ_LUN_IN(0, request_id));
365 command_packet->unit = 0;
366 }
367
368 command_packet->sgl_offset = 16;
369
370 if (!sglistarg) {
371
372 if (scsi_sg_count(srb)) {
373 sg_count = twl_map_scsi_sg_data(tw_dev, request_id);
374 if (sg_count == 0)
375 goto out;
376
377 scsi_for_each_sg(srb, sg, sg_count, i) {
378 command_packet->sg_list[i].address = TW_CPU_TO_SGL(sg_dma_address(sg));
379 command_packet->sg_list[i].length = TW_CPU_TO_SGL(sg_dma_len(sg));
380 }
381 command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN((srb->device->lun >> 4), scsi_sg_count(tw_dev->srb[request_id])));
382 }
383 } else {
384
385 for (i = 0; i < use_sg; i++) {
386 command_packet->sg_list[i].address = TW_CPU_TO_SGL(sglistarg[i].address);
387 command_packet->sg_list[i].length = TW_CPU_TO_SGL(sglistarg[i].length);
388 }
389 command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN(0, use_sg));
390 }
391
392
393 if (srb) {
394 tw_dev->sector_count = scsi_bufflen(srb) / 512;
395 if (tw_dev->sector_count > tw_dev->max_sector_count)
396 tw_dev->max_sector_count = tw_dev->sector_count;
397 tw_dev->sgl_entries = scsi_sg_count(srb);
398 if (tw_dev->sgl_entries > tw_dev->max_sgl_entries)
399 tw_dev->max_sgl_entries = tw_dev->sgl_entries;
400 }
401
402
403 retval = twl_post_command_packet(tw_dev, request_id);
404
405out:
406 return retval;
407}
408
409
410static int twl_aen_read_queue(TW_Device_Extension *tw_dev, int request_id)
411{
412 char cdb[TW_MAX_CDB_LEN];
413 TW_SG_Entry_ISO sglist[1];
414 TW_Command_Full *full_command_packet;
415 int retval = 1;
416
417 full_command_packet = tw_dev->command_packet_virt[request_id];
418 memset(full_command_packet, 0, sizeof(TW_Command_Full));
419
420
421 memset(&cdb, 0, TW_MAX_CDB_LEN);
422 cdb[0] = REQUEST_SENSE;
423 cdb[4] = TW_ALLOCATION_LENGTH;
424
425
426 memset(&sglist, 0, sizeof(TW_SG_Entry_ISO));
427 sglist[0].length = TW_SECTOR_SIZE;
428 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
429
430
431 tw_dev->srb[request_id] = NULL;
432
433
434 if (twl_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
435 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2, "Post failed while reading AEN queue");
436 goto out;
437 }
438 retval = 0;
439out:
440 return retval;
441}
442
443
444static void twl_aen_sync_time(TW_Device_Extension *tw_dev, int request_id)
445{
446 u32 schedulertime;
447 struct timeval utc;
448 TW_Command_Full *full_command_packet;
449 TW_Command *command_packet;
450 TW_Param_Apache *param;
451 u32 local_time;
452
453
454 full_command_packet = tw_dev->command_packet_virt[request_id];
455 memset(full_command_packet, 0, sizeof(TW_Command_Full));
456 command_packet = &full_command_packet->command.oldcommand;
457 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_SET_PARAM);
458 command_packet->request_id = request_id;
459 command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
460 command_packet->byte8_offset.param.sgl[0].length = TW_CPU_TO_SGL(TW_SECTOR_SIZE);
461 command_packet->size = TW_COMMAND_SIZE;
462 command_packet->byte6_offset.parameter_count = cpu_to_le16(1);
463
464
465 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
466 memset(param, 0, TW_SECTOR_SIZE);
467 param->table_id = cpu_to_le16(TW_TIMEKEEP_TABLE | 0x8000);
468 param->parameter_id = cpu_to_le16(0x3);
469 param->parameter_size_bytes = cpu_to_le16(4);
470
471
472
473 do_gettimeofday(&utc);
474 local_time = (u32)(utc.tv_sec - (sys_tz.tz_minuteswest * 60));
475 schedulertime = local_time - (3 * 86400);
476 schedulertime = cpu_to_le32(schedulertime % 604800);
477
478 memcpy(param->data, &schedulertime, sizeof(u32));
479
480
481 tw_dev->srb[request_id] = NULL;
482
483
484 twl_post_command_packet(tw_dev, request_id);
485}
486
487
488static void twl_get_request_id(TW_Device_Extension *tw_dev, int *request_id)
489{
490 *request_id = tw_dev->free_queue[tw_dev->free_head];
491 tw_dev->free_head = (tw_dev->free_head + 1) % TW_Q_LENGTH;
492 tw_dev->state[*request_id] = TW_S_STARTED;
493}
494
495
496static void twl_free_request_id(TW_Device_Extension *tw_dev, int request_id)
497{
498 tw_dev->free_queue[tw_dev->free_tail] = request_id;
499 tw_dev->state[request_id] = TW_S_FINISHED;
500 tw_dev->free_tail = (tw_dev->free_tail + 1) % TW_Q_LENGTH;
501}
502
503
504static int twl_aen_complete(TW_Device_Extension *tw_dev, int request_id)
505{
506 TW_Command_Full *full_command_packet;
507 TW_Command *command_packet;
508 TW_Command_Apache_Header *header;
509 unsigned short aen;
510 int retval = 1;
511
512 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
513 tw_dev->posted_request_count--;
514 aen = le16_to_cpu(header->status_block.error);
515 full_command_packet = tw_dev->command_packet_virt[request_id];
516 command_packet = &full_command_packet->command.oldcommand;
517
518
519 if (TW_OP_OUT(command_packet->opcode__sgloffset) == TW_OP_SET_PARAM) {
520
521 if (twl_aen_read_queue(tw_dev, request_id))
522 goto out2;
523 else {
524 retval = 0;
525 goto out;
526 }
527 }
528
529 switch (aen) {
530 case TW_AEN_QUEUE_EMPTY:
531
532 break;
533 case TW_AEN_SYNC_TIME_WITH_HOST:
534 twl_aen_sync_time(tw_dev, request_id);
535 retval = 0;
536 goto out;
537 default:
538 twl_aen_queue_event(tw_dev, header);
539
540
541 if (twl_aen_read_queue(tw_dev, request_id))
542 goto out2;
543 else {
544 retval = 0;
545 goto out;
546 }
547 }
548 retval = 0;
549out2:
550 tw_dev->state[request_id] = TW_S_COMPLETED;
551 twl_free_request_id(tw_dev, request_id);
552 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
553out:
554 return retval;
555}
556
557
558static int twl_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds)
559{
560 unsigned long before;
561 dma_addr_t mfa;
562 u32 regh, regl;
563 u32 response;
564 int retval = 1;
565 int found = 0;
566
567 before = jiffies;
568
569 while (!found) {
570 if (sizeof(dma_addr_t) > 4) {
571 regh = readl(TWL_HOBQPH_REG_ADDR(tw_dev));
572 regl = readl(TWL_HOBQPL_REG_ADDR(tw_dev));
573 mfa = ((u64)regh << 32) | regl;
574 } else
575 mfa = readl(TWL_HOBQPL_REG_ADDR(tw_dev));
576
577 response = (u32)mfa;
578
579 if (TW_RESID_OUT(response) == request_id)
580 found = 1;
581
582 if (time_after(jiffies, before + HZ * seconds))
583 goto out;
584
585 msleep(50);
586 }
587 retval = 0;
588out:
589 return retval;
590}
591
592
593static int twl_aen_drain_queue(TW_Device_Extension *tw_dev, int no_check_reset)
594{
595 int request_id = 0;
596 char cdb[TW_MAX_CDB_LEN];
597 TW_SG_Entry_ISO sglist[1];
598 int finished = 0, count = 0;
599 TW_Command_Full *full_command_packet;
600 TW_Command_Apache_Header *header;
601 unsigned short aen;
602 int first_reset = 0, queue = 0, retval = 1;
603
604 if (no_check_reset)
605 first_reset = 0;
606 else
607 first_reset = 1;
608
609 full_command_packet = tw_dev->command_packet_virt[request_id];
610 memset(full_command_packet, 0, sizeof(TW_Command_Full));
611
612
613 memset(&cdb, 0, TW_MAX_CDB_LEN);
614 cdb[0] = REQUEST_SENSE;
615 cdb[4] = TW_ALLOCATION_LENGTH;
616
617
618 memset(&sglist, 0, sizeof(TW_SG_Entry_ISO));
619 sglist[0].length = TW_SECTOR_SIZE;
620 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
621
622
623 tw_dev->srb[request_id] = NULL;
624
625 do {
626
627 if (twl_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
628 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x3, "Error posting request sense");
629 goto out;
630 }
631
632
633 if (twl_poll_response(tw_dev, request_id, 30)) {
634 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x4, "No valid response while draining AEN queue");
635 tw_dev->posted_request_count--;
636 goto out;
637 }
638
639 tw_dev->posted_request_count--;
640 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
641 aen = le16_to_cpu(header->status_block.error);
642 queue = 0;
643 count++;
644
645 switch (aen) {
646 case TW_AEN_QUEUE_EMPTY:
647 if (first_reset != 1)
648 goto out;
649 else
650 finished = 1;
651 break;
652 case TW_AEN_SOFT_RESET:
653 if (first_reset == 0)
654 first_reset = 1;
655 else
656 queue = 1;
657 break;
658 case TW_AEN_SYNC_TIME_WITH_HOST:
659 break;
660 default:
661 queue = 1;
662 }
663
664
665 if (queue)
666 twl_aen_queue_event(tw_dev, header);
667 } while ((finished == 0) && (count < TW_MAX_AEN_DRAIN));
668
669 if (count == TW_MAX_AEN_DRAIN)
670 goto out;
671
672 retval = 0;
673out:
674 tw_dev->state[request_id] = TW_S_INITIAL;
675 return retval;
676}
677
678
679static int twl_allocate_memory(TW_Device_Extension *tw_dev, int size, int which)
680{
681 int i;
682 dma_addr_t dma_handle;
683 unsigned long *cpu_addr;
684 int retval = 1;
685
686 cpu_addr = pci_zalloc_consistent(tw_dev->tw_pci_dev, size * TW_Q_LENGTH,
687 &dma_handle);
688 if (!cpu_addr) {
689 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x5, "Memory allocation failed");
690 goto out;
691 }
692
693 for (i = 0; i < TW_Q_LENGTH; i++) {
694 switch(which) {
695 case 0:
696 tw_dev->command_packet_phys[i] = dma_handle+(i*size);
697 tw_dev->command_packet_virt[i] = (TW_Command_Full *)((unsigned char *)cpu_addr + (i*size));
698 break;
699 case 1:
700 tw_dev->generic_buffer_phys[i] = dma_handle+(i*size);
701 tw_dev->generic_buffer_virt[i] = (unsigned long *)((unsigned char *)cpu_addr + (i*size));
702 break;
703 case 2:
704 tw_dev->sense_buffer_phys[i] = dma_handle+(i*size);
705 tw_dev->sense_buffer_virt[i] = (TW_Command_Apache_Header *)((unsigned char *)cpu_addr + (i*size));
706 break;
707 }
708 }
709 retval = 0;
710out:
711 return retval;
712}
713
714
715static void twl_load_sgl(TW_Device_Extension *tw_dev, TW_Command_Full *full_command_packet, int request_id, dma_addr_t dma_handle, int length)
716{
717 TW_Command *oldcommand;
718 TW_Command_Apache *newcommand;
719 TW_SG_Entry_ISO *sgl;
720 unsigned int pae = 0;
721
722 if ((sizeof(long) < 8) && (sizeof(dma_addr_t) > 4))
723 pae = 1;
724
725 if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
726 newcommand = &full_command_packet->command.newcommand;
727 newcommand->request_id__lunl =
728 cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->request_id__lunl), request_id));
729 if (length) {
730 newcommand->sg_list[0].address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1);
731 newcommand->sg_list[0].length = TW_CPU_TO_SGL(length);
732 }
733 newcommand->sgl_entries__lunh =
734 cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->sgl_entries__lunh), length ? 1 : 0));
735 } else {
736 oldcommand = &full_command_packet->command.oldcommand;
737 oldcommand->request_id = request_id;
738
739 if (TW_SGL_OUT(oldcommand->opcode__sgloffset)) {
740
741 sgl = (TW_SG_Entry_ISO *)((u32 *)oldcommand+oldcommand->size - (sizeof(TW_SG_Entry_ISO)/4) + pae + (sizeof(dma_addr_t) > 4 ? 1 : 0));
742 sgl->address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1);
743 sgl->length = TW_CPU_TO_SGL(length);
744 oldcommand->size += pae;
745 oldcommand->size += sizeof(dma_addr_t) > 4 ? 1 : 0;
746 }
747 }
748}
749
750
751
752static long twl_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
753{
754 long timeout;
755 unsigned long *cpu_addr, data_buffer_length_adjusted = 0, flags = 0;
756 dma_addr_t dma_handle;
757 int request_id = 0;
758 TW_Ioctl_Driver_Command driver_command;
759 struct inode *inode = file_inode(file);
760 TW_Ioctl_Buf_Apache *tw_ioctl;
761 TW_Command_Full *full_command_packet;
762 TW_Device_Extension *tw_dev = twl_device_extension_list[iminor(inode)];
763 int retval = -EFAULT;
764 void __user *argp = (void __user *)arg;
765
766 mutex_lock(&twl_chrdev_mutex);
767
768
769 if (mutex_lock_interruptible(&tw_dev->ioctl_lock)) {
770 retval = -EINTR;
771 goto out;
772 }
773
774
775 if (copy_from_user(&driver_command, argp, sizeof(TW_Ioctl_Driver_Command)))
776 goto out2;
777
778
779 if (driver_command.buffer_length > TW_MAX_SECTORS * 2048) {
780 retval = -EINVAL;
781 goto out2;
782 }
783
784
785 data_buffer_length_adjusted = (driver_command.buffer_length + 511) & ~511;
786
787
788 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);
789 if (!cpu_addr) {
790 retval = -ENOMEM;
791 goto out2;
792 }
793
794 tw_ioctl = (TW_Ioctl_Buf_Apache *)cpu_addr;
795
796
797 if (copy_from_user(tw_ioctl, argp, driver_command.buffer_length + sizeof(TW_Ioctl_Buf_Apache) - 1))
798 goto out3;
799
800
801 switch (cmd) {
802 case TW_IOCTL_FIRMWARE_PASS_THROUGH:
803 spin_lock_irqsave(tw_dev->host->host_lock, flags);
804 twl_get_request_id(tw_dev, &request_id);
805
806
807 tw_dev->srb[request_id] = NULL;
808
809
810 tw_dev->chrdev_request_id = request_id;
811
812 full_command_packet = (TW_Command_Full *)&tw_ioctl->firmware_command;
813
814
815 twl_load_sgl(tw_dev, full_command_packet, request_id, dma_handle, data_buffer_length_adjusted);
816
817 memcpy(tw_dev->command_packet_virt[request_id], &(tw_ioctl->firmware_command), sizeof(TW_Command_Full));
818
819
820 twl_post_command_packet(tw_dev, request_id);
821 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
822
823 timeout = TW_IOCTL_CHRDEV_TIMEOUT*HZ;
824
825
826 timeout = wait_event_timeout(tw_dev->ioctl_wqueue, tw_dev->chrdev_request_id == TW_IOCTL_CHRDEV_FREE, timeout);
827
828
829 if (tw_dev->chrdev_request_id != TW_IOCTL_CHRDEV_FREE) {
830
831 printk(KERN_WARNING "3w-sas: scsi%d: WARNING: (0x%02X:0x%04X): Character ioctl (0x%x) timed out, resetting card.\n",
832 tw_dev->host->host_no, TW_DRIVER, 0x6,
833 cmd);
834 retval = -EIO;
835 twl_reset_device_extension(tw_dev, 1);
836 goto out3;
837 }
838
839
840 memcpy(&(tw_ioctl->firmware_command), tw_dev->command_packet_virt[request_id], sizeof(TW_Command_Full));
841
842
843 spin_lock_irqsave(tw_dev->host->host_lock, flags);
844 tw_dev->posted_request_count--;
845 tw_dev->state[request_id] = TW_S_COMPLETED;
846 twl_free_request_id(tw_dev, request_id);
847 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
848 break;
849 default:
850 retval = -ENOTTY;
851 goto out3;
852 }
853
854
855 if (copy_to_user(argp, tw_ioctl, sizeof(TW_Ioctl_Buf_Apache) + driver_command.buffer_length - 1) == 0)
856 retval = 0;
857out3:
858
859 dma_free_coherent(&tw_dev->tw_pci_dev->dev, data_buffer_length_adjusted+sizeof(TW_Ioctl_Buf_Apache) - 1, cpu_addr, dma_handle);
860out2:
861 mutex_unlock(&tw_dev->ioctl_lock);
862out:
863 mutex_unlock(&twl_chrdev_mutex);
864 return retval;
865}
866
867
868static int twl_chrdev_open(struct inode *inode, struct file *file)
869{
870 unsigned int minor_number;
871 int retval = -ENODEV;
872
873 if (!capable(CAP_SYS_ADMIN)) {
874 retval = -EACCES;
875 goto out;
876 }
877
878 minor_number = iminor(inode);
879 if (minor_number >= twl_device_extension_count)
880 goto out;
881 retval = 0;
882out:
883 return retval;
884}
885
886
887static const struct file_operations twl_fops = {
888 .owner = THIS_MODULE,
889 .unlocked_ioctl = twl_chrdev_ioctl,
890 .open = twl_chrdev_open,
891 .release = NULL,
892 .llseek = noop_llseek,
893};
894
895
896static int twl_fill_sense(TW_Device_Extension *tw_dev, int i, int request_id, int copy_sense, int print_host)
897{
898 TW_Command_Apache_Header *header;
899 TW_Command_Full *full_command_packet;
900 unsigned short error;
901 char *error_str;
902 int retval = 1;
903
904 header = tw_dev->sense_buffer_virt[i];
905 full_command_packet = tw_dev->command_packet_virt[request_id];
906
907
908 error_str = &(header->err_specific_desc[strlen(header->err_specific_desc) + 1]);
909
910
911 error = le16_to_cpu(header->status_block.error);
912 if ((error != TW_ERROR_LOGICAL_UNIT_NOT_SUPPORTED) && (error != TW_ERROR_UNIT_OFFLINE) && (error != TW_ERROR_INVALID_FIELD_IN_CDB)) {
913 if (print_host)
914 printk(KERN_WARNING "3w-sas: scsi%d: ERROR: (0x%02X:0x%04X): %s:%s.\n",
915 tw_dev->host->host_no,
916 TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
917 header->status_block.error,
918 error_str,
919 header->err_specific_desc);
920 else
921 printk(KERN_WARNING "3w-sas: ERROR: (0x%02X:0x%04X): %s:%s.\n",
922 TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
923 header->status_block.error,
924 error_str,
925 header->err_specific_desc);
926 }
927
928 if (copy_sense) {
929 memcpy(tw_dev->srb[request_id]->sense_buffer, header->sense_data, TW_SENSE_DATA_LENGTH);
930 tw_dev->srb[request_id]->result = (full_command_packet->command.newcommand.status << 1);
931 goto out;
932 }
933out:
934 return retval;
935}
936
937
938static void twl_free_device_extension(TW_Device_Extension *tw_dev)
939{
940 if (tw_dev->command_packet_virt[0])
941 pci_free_consistent(tw_dev->tw_pci_dev,
942 sizeof(TW_Command_Full)*TW_Q_LENGTH,
943 tw_dev->command_packet_virt[0],
944 tw_dev->command_packet_phys[0]);
945
946 if (tw_dev->generic_buffer_virt[0])
947 pci_free_consistent(tw_dev->tw_pci_dev,
948 TW_SECTOR_SIZE*TW_Q_LENGTH,
949 tw_dev->generic_buffer_virt[0],
950 tw_dev->generic_buffer_phys[0]);
951
952 if (tw_dev->sense_buffer_virt[0])
953 pci_free_consistent(tw_dev->tw_pci_dev,
954 sizeof(TW_Command_Apache_Header)*
955 TW_Q_LENGTH,
956 tw_dev->sense_buffer_virt[0],
957 tw_dev->sense_buffer_phys[0]);
958
959 kfree(tw_dev->event_queue[0]);
960}
961
962
963static void *twl_get_param(TW_Device_Extension *tw_dev, int request_id, int table_id, int parameter_id, int parameter_size_bytes)
964{
965 TW_Command_Full *full_command_packet;
966 TW_Command *command_packet;
967 TW_Param_Apache *param;
968 void *retval = NULL;
969
970
971 full_command_packet = tw_dev->command_packet_virt[request_id];
972 memset(full_command_packet, 0, sizeof(TW_Command_Full));
973 command_packet = &full_command_packet->command.oldcommand;
974
975 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_GET_PARAM);
976 command_packet->size = TW_COMMAND_SIZE;
977 command_packet->request_id = request_id;
978 command_packet->byte6_offset.block_count = cpu_to_le16(1);
979
980
981 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
982 memset(param, 0, TW_SECTOR_SIZE);
983 param->table_id = cpu_to_le16(table_id | 0x8000);
984 param->parameter_id = cpu_to_le16(parameter_id);
985 param->parameter_size_bytes = cpu_to_le16(parameter_size_bytes);
986
987 command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
988 command_packet->byte8_offset.param.sgl[0].length = TW_CPU_TO_SGL(TW_SECTOR_SIZE);
989
990
991 twl_post_command_packet(tw_dev, request_id);
992
993
994 if (twl_poll_response(tw_dev, request_id, 30))
995 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x7, "No valid response during get param")
996 else
997 retval = (void *)&(param->data[0]);
998
999 tw_dev->posted_request_count--;
1000 tw_dev->state[request_id] = TW_S_INITIAL;
1001
1002 return retval;
1003}
1004
1005
1006static int twl_initconnection(TW_Device_Extension *tw_dev, int message_credits,
1007 u32 set_features, unsigned short current_fw_srl,
1008 unsigned short current_fw_arch_id,
1009 unsigned short current_fw_branch,
1010 unsigned short current_fw_build,
1011 unsigned short *fw_on_ctlr_srl,
1012 unsigned short *fw_on_ctlr_arch_id,
1013 unsigned short *fw_on_ctlr_branch,
1014 unsigned short *fw_on_ctlr_build,
1015 u32 *init_connect_result)
1016{
1017 TW_Command_Full *full_command_packet;
1018 TW_Initconnect *tw_initconnect;
1019 int request_id = 0, retval = 1;
1020
1021
1022 full_command_packet = tw_dev->command_packet_virt[request_id];
1023 memset(full_command_packet, 0, sizeof(TW_Command_Full));
1024 full_command_packet->header.header_desc.size_header = 128;
1025
1026 tw_initconnect = (TW_Initconnect *)&full_command_packet->command.oldcommand;
1027 tw_initconnect->opcode__reserved = TW_OPRES_IN(0, TW_OP_INIT_CONNECTION);
1028 tw_initconnect->request_id = request_id;
1029 tw_initconnect->message_credits = cpu_to_le16(message_credits);
1030 tw_initconnect->features = set_features;
1031
1032
1033 tw_initconnect->features |= sizeof(dma_addr_t) > 4 ? 1 : 0;
1034
1035 tw_initconnect->features = cpu_to_le32(tw_initconnect->features);
1036
1037 if (set_features & TW_EXTENDED_INIT_CONNECT) {
1038 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE_EXTENDED;
1039 tw_initconnect->fw_srl = cpu_to_le16(current_fw_srl);
1040 tw_initconnect->fw_arch_id = cpu_to_le16(current_fw_arch_id);
1041 tw_initconnect->fw_branch = cpu_to_le16(current_fw_branch);
1042 tw_initconnect->fw_build = cpu_to_le16(current_fw_build);
1043 } else
1044 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE;
1045
1046
1047 twl_post_command_packet(tw_dev, request_id);
1048
1049
1050 if (twl_poll_response(tw_dev, request_id, 30)) {
1051 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x8, "No valid response during init connection");
1052 } else {
1053 if (set_features & TW_EXTENDED_INIT_CONNECT) {
1054 *fw_on_ctlr_srl = le16_to_cpu(tw_initconnect->fw_srl);
1055 *fw_on_ctlr_arch_id = le16_to_cpu(tw_initconnect->fw_arch_id);
1056 *fw_on_ctlr_branch = le16_to_cpu(tw_initconnect->fw_branch);
1057 *fw_on_ctlr_build = le16_to_cpu(tw_initconnect->fw_build);
1058 *init_connect_result = le32_to_cpu(tw_initconnect->result);
1059 }
1060 retval = 0;
1061 }
1062
1063 tw_dev->posted_request_count--;
1064 tw_dev->state[request_id] = TW_S_INITIAL;
1065
1066 return retval;
1067}
1068
1069
1070static int twl_initialize_device_extension(TW_Device_Extension *tw_dev)
1071{
1072 int i, retval = 1;
1073
1074
1075 if (twl_allocate_memory(tw_dev, sizeof(TW_Command_Full), 0)) {
1076 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x9, "Command packet memory allocation failed");
1077 goto out;
1078 }
1079
1080
1081 if (twl_allocate_memory(tw_dev, TW_SECTOR_SIZE, 1)) {
1082 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xa, "Generic memory allocation failed");
1083 goto out;
1084 }
1085
1086
1087 if (twl_allocate_memory(tw_dev, sizeof(TW_Command_Apache_Header), 2)) {
1088 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xb, "Sense buffer allocation failed");
1089 goto out;
1090 }
1091
1092
1093 tw_dev->event_queue[0] = kcalloc(TW_Q_LENGTH, sizeof(TW_Event), GFP_KERNEL);
1094 if (!tw_dev->event_queue[0]) {
1095 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xc, "Event info memory allocation failed");
1096 goto out;
1097 }
1098
1099 for (i = 0; i < TW_Q_LENGTH; i++) {
1100 tw_dev->event_queue[i] = (TW_Event *)((unsigned char *)tw_dev->event_queue[0] + (i * sizeof(TW_Event)));
1101 tw_dev->free_queue[i] = i;
1102 tw_dev->state[i] = TW_S_INITIAL;
1103 }
1104
1105 tw_dev->free_head = TW_Q_START;
1106 tw_dev->free_tail = TW_Q_START;
1107 tw_dev->error_sequence_id = 1;
1108 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1109
1110 mutex_init(&tw_dev->ioctl_lock);
1111 init_waitqueue_head(&tw_dev->ioctl_wqueue);
1112
1113 retval = 0;
1114out:
1115 return retval;
1116}
1117
1118
1119static void twl_unmap_scsi_data(TW_Device_Extension *tw_dev, int request_id)
1120{
1121 struct scsi_cmnd *cmd = tw_dev->srb[request_id];
1122
1123 if (cmd->SCp.phase == TW_PHASE_SGLIST)
1124 scsi_dma_unmap(cmd);
1125}
1126
1127
1128static int twl_handle_attention_interrupt(TW_Device_Extension *tw_dev)
1129{
1130 int retval = 1;
1131 u32 request_id, doorbell;
1132
1133
1134 doorbell = readl(TWL_HOBDB_REG_ADDR(tw_dev));
1135
1136
1137 if (doorbell & TWL_DOORBELL_CONTROLLER_ERROR) {
1138 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xd, "Microcontroller Error: clearing");
1139 goto out;
1140 }
1141
1142
1143 if (doorbell & TWL_DOORBELL_ATTENTION_INTERRUPT) {
1144 if (!(test_and_set_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags))) {
1145 twl_get_request_id(tw_dev, &request_id);
1146 if (twl_aen_read_queue(tw_dev, request_id)) {
1147 tw_dev->state[request_id] = TW_S_COMPLETED;
1148 twl_free_request_id(tw_dev, request_id);
1149 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
1150 }
1151 }
1152 }
1153
1154 retval = 0;
1155out:
1156
1157 TWL_CLEAR_DB_INTERRUPT(tw_dev);
1158
1159
1160 readl(TWL_HOBDBC_REG_ADDR(tw_dev));
1161
1162 return retval;
1163}
1164
1165
1166static irqreturn_t twl_interrupt(int irq, void *dev_instance)
1167{
1168 TW_Device_Extension *tw_dev = (TW_Device_Extension *)dev_instance;
1169 int i, handled = 0, error = 0;
1170 dma_addr_t mfa = 0;
1171 u32 reg, regl, regh, response, request_id = 0;
1172 struct scsi_cmnd *cmd;
1173 TW_Command_Full *full_command_packet;
1174
1175 spin_lock(tw_dev->host->host_lock);
1176
1177
1178 reg = readl(TWL_HISTAT_REG_ADDR(tw_dev));
1179
1180
1181 if (!(reg & TWL_HISTATUS_VALID_INTERRUPT))
1182 goto twl_interrupt_bail;
1183
1184 handled = 1;
1185
1186
1187 if (test_bit(TW_IN_RESET, &tw_dev->flags))
1188 goto twl_interrupt_bail;
1189
1190
1191 if (reg & TWL_HISTATUS_ATTENTION_INTERRUPT) {
1192 if (twl_handle_attention_interrupt(tw_dev)) {
1193 TWL_MASK_INTERRUPTS(tw_dev);
1194 goto twl_interrupt_bail;
1195 }
1196 }
1197
1198
1199 while (reg & TWL_HISTATUS_RESPONSE_INTERRUPT) {
1200 if (sizeof(dma_addr_t) > 4) {
1201 regh = readl(TWL_HOBQPH_REG_ADDR(tw_dev));
1202 regl = readl(TWL_HOBQPL_REG_ADDR(tw_dev));
1203 mfa = ((u64)regh << 32) | regl;
1204 } else
1205 mfa = readl(TWL_HOBQPL_REG_ADDR(tw_dev));
1206
1207 error = 0;
1208 response = (u32)mfa;
1209
1210
1211 if (!TW_NOTMFA_OUT(response)) {
1212 for (i=0;i<TW_Q_LENGTH;i++) {
1213 if (tw_dev->sense_buffer_phys[i] == mfa) {
1214 request_id = le16_to_cpu(tw_dev->sense_buffer_virt[i]->header_desc.request_id);
1215 if (tw_dev->srb[request_id] != NULL)
1216 error = twl_fill_sense(tw_dev, i, request_id, 1, 1);
1217 else {
1218
1219 if (request_id != tw_dev->chrdev_request_id)
1220 error = twl_fill_sense(tw_dev, i, request_id, 0, 1);
1221 else
1222 memcpy(tw_dev->command_packet_virt[request_id], tw_dev->sense_buffer_virt[i], sizeof(TW_Command_Apache_Header));
1223 }
1224
1225
1226 writel((u32)((u64)tw_dev->sense_buffer_phys[i] >> 32), TWL_HOBQPH_REG_ADDR(tw_dev));
1227 writel((u32)tw_dev->sense_buffer_phys[i], TWL_HOBQPL_REG_ADDR(tw_dev));
1228 break;
1229 }
1230 }
1231 } else
1232 request_id = TW_RESID_OUT(response);
1233
1234 full_command_packet = tw_dev->command_packet_virt[request_id];
1235
1236
1237 if (tw_dev->state[request_id] != TW_S_POSTED) {
1238 if (tw_dev->srb[request_id] != NULL) {
1239 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xe, "Received a request id that wasn't posted");
1240 TWL_MASK_INTERRUPTS(tw_dev);
1241 goto twl_interrupt_bail;
1242 }
1243 }
1244
1245
1246 if (tw_dev->srb[request_id] == NULL) {
1247 if (request_id != tw_dev->chrdev_request_id) {
1248 if (twl_aen_complete(tw_dev, request_id))
1249 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xf, "Error completing AEN during attention interrupt");
1250 } else {
1251 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1252 wake_up(&tw_dev->ioctl_wqueue);
1253 }
1254 } else {
1255 cmd = tw_dev->srb[request_id];
1256
1257 if (!error)
1258 cmd->result = (DID_OK << 16);
1259
1260
1261 if ((scsi_sg_count(cmd) <= 1) && (full_command_packet->command.newcommand.status == 0)) {
1262 if (full_command_packet->command.newcommand.sg_list[0].length < scsi_bufflen(tw_dev->srb[request_id]))
1263 scsi_set_resid(cmd, scsi_bufflen(cmd) - full_command_packet->command.newcommand.sg_list[0].length);
1264 }
1265
1266
1267 tw_dev->state[request_id] = TW_S_COMPLETED;
1268 twl_free_request_id(tw_dev, request_id);
1269 tw_dev->posted_request_count--;
1270 tw_dev->srb[request_id]->scsi_done(tw_dev->srb[request_id]);
1271 twl_unmap_scsi_data(tw_dev, request_id);
1272 }
1273
1274
1275 reg = readl(TWL_HISTAT_REG_ADDR(tw_dev));
1276 }
1277
1278twl_interrupt_bail:
1279 spin_unlock(tw_dev->host->host_lock);
1280 return IRQ_RETVAL(handled);
1281}
1282
1283
1284static int twl_poll_register(TW_Device_Extension *tw_dev, void *reg, u32 value, u32 result, int seconds)
1285{
1286 unsigned long before;
1287 int retval = 1;
1288 u32 reg_value;
1289
1290 reg_value = readl(reg);
1291 before = jiffies;
1292
1293 while ((reg_value & value) != result) {
1294 reg_value = readl(reg);
1295 if (time_after(jiffies, before + HZ * seconds))
1296 goto out;
1297 msleep(50);
1298 }
1299 retval = 0;
1300out:
1301 return retval;
1302}
1303
1304
1305static int twl_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset)
1306{
1307 int retval = 1;
1308 int i = 0;
1309 u32 status = 0;
1310 unsigned short fw_on_ctlr_srl = 0, fw_on_ctlr_arch_id = 0;
1311 unsigned short fw_on_ctlr_branch = 0, fw_on_ctlr_build = 0;
1312 u32 init_connect_result = 0;
1313 int tries = 0;
1314 int do_soft_reset = soft_reset;
1315
1316 while (tries < TW_MAX_RESET_TRIES) {
1317
1318 if (do_soft_reset) {
1319 TWL_SOFT_RESET(tw_dev);
1320
1321
1322 if (twl_poll_register(tw_dev, TWL_SCRPD3_REG_ADDR(tw_dev), TWL_CONTROLLER_READY, 0x0, 30)) {
1323 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x10, "Controller never went non-ready during reset sequence");
1324 tries++;
1325 continue;
1326 }
1327 if (twl_poll_register(tw_dev, TWL_SCRPD3_REG_ADDR(tw_dev), TWL_CONTROLLER_READY, TWL_CONTROLLER_READY, 60)) {
1328 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x11, "Controller not ready during reset sequence");
1329 tries++;
1330 continue;
1331 }
1332 }
1333
1334
1335 if (twl_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
1336 TW_EXTENDED_INIT_CONNECT, TW_CURRENT_DRIVER_SRL,
1337 TW_9750_ARCH_ID, TW_CURRENT_DRIVER_BRANCH,
1338 TW_CURRENT_DRIVER_BUILD, &fw_on_ctlr_srl,
1339 &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
1340 &fw_on_ctlr_build, &init_connect_result)) {
1341 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x12, "Initconnection failed while checking SRL");
1342 do_soft_reset = 1;
1343 tries++;
1344 continue;
1345 }
1346
1347
1348 while (i < TW_Q_LENGTH) {
1349 writel((u32)((u64)tw_dev->sense_buffer_phys[i] >> 32), TWL_HOBQPH_REG_ADDR(tw_dev));
1350 writel((u32)tw_dev->sense_buffer_phys[i], TWL_HOBQPL_REG_ADDR(tw_dev));
1351
1352
1353 status = readl(TWL_STATUS_REG_ADDR(tw_dev));
1354 if (!(status & TWL_STATUS_OVERRUN_SUBMIT))
1355 i++;
1356 }
1357
1358
1359 status = readl(TWL_STATUS_REG_ADDR(tw_dev));
1360 if (status) {
1361 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x13, "Bad controller status after loading sense buffers");
1362 do_soft_reset = 1;
1363 tries++;
1364 continue;
1365 }
1366
1367
1368 if (twl_aen_drain_queue(tw_dev, soft_reset)) {
1369 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x14, "AEN drain failed during reset sequence");
1370 do_soft_reset = 1;
1371 tries++;
1372 continue;
1373 }
1374
1375
1376 strncpy(tw_dev->tw_compat_info.driver_version, TW_DRIVER_VERSION, strlen(TW_DRIVER_VERSION));
1377 tw_dev->tw_compat_info.driver_srl_high = TW_CURRENT_DRIVER_SRL;
1378 tw_dev->tw_compat_info.driver_branch_high = TW_CURRENT_DRIVER_BRANCH;
1379 tw_dev->tw_compat_info.driver_build_high = TW_CURRENT_DRIVER_BUILD;
1380 tw_dev->tw_compat_info.driver_srl_low = TW_BASE_FW_SRL;
1381 tw_dev->tw_compat_info.driver_branch_low = TW_BASE_FW_BRANCH;
1382 tw_dev->tw_compat_info.driver_build_low = TW_BASE_FW_BUILD;
1383 tw_dev->tw_compat_info.fw_on_ctlr_srl = fw_on_ctlr_srl;
1384 tw_dev->tw_compat_info.fw_on_ctlr_branch = fw_on_ctlr_branch;
1385 tw_dev->tw_compat_info.fw_on_ctlr_build = fw_on_ctlr_build;
1386
1387
1388 retval = 0;
1389 goto out;
1390 }
1391out:
1392 return retval;
1393}
1394
1395
1396static int twl_reset_device_extension(TW_Device_Extension *tw_dev, int ioctl_reset)
1397{
1398 int i = 0, retval = 1;
1399 unsigned long flags = 0;
1400
1401
1402 if (ioctl_reset)
1403 scsi_block_requests(tw_dev->host);
1404
1405 set_bit(TW_IN_RESET, &tw_dev->flags);
1406 TWL_MASK_INTERRUPTS(tw_dev);
1407 TWL_CLEAR_DB_INTERRUPT(tw_dev);
1408
1409 spin_lock_irqsave(tw_dev->host->host_lock, flags);
1410
1411
1412 for (i = 0; i < TW_Q_LENGTH; i++) {
1413 if ((tw_dev->state[i] != TW_S_FINISHED) &&
1414 (tw_dev->state[i] != TW_S_INITIAL) &&
1415 (tw_dev->state[i] != TW_S_COMPLETED)) {
1416 if (tw_dev->srb[i]) {
1417 tw_dev->srb[i]->result = (DID_RESET << 16);
1418 tw_dev->srb[i]->scsi_done(tw_dev->srb[i]);
1419 twl_unmap_scsi_data(tw_dev, i);
1420 }
1421 }
1422 }
1423
1424
1425 for (i = 0; i < TW_Q_LENGTH; i++) {
1426 tw_dev->free_queue[i] = i;
1427 tw_dev->state[i] = TW_S_INITIAL;
1428 }
1429 tw_dev->free_head = TW_Q_START;
1430 tw_dev->free_tail = TW_Q_START;
1431 tw_dev->posted_request_count = 0;
1432
1433 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
1434
1435 if (twl_reset_sequence(tw_dev, 1))
1436 goto out;
1437
1438 TWL_UNMASK_INTERRUPTS(tw_dev);
1439
1440 clear_bit(TW_IN_RESET, &tw_dev->flags);
1441 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1442
1443 retval = 0;
1444out:
1445 if (ioctl_reset)
1446 scsi_unblock_requests(tw_dev->host);
1447 return retval;
1448}
1449
1450
1451static int twl_scsi_biosparam(struct scsi_device *sdev, struct block_device *bdev, sector_t capacity, int geom[])
1452{
1453 int heads, sectors;
1454 TW_Device_Extension *tw_dev;
1455
1456 tw_dev = (TW_Device_Extension *)sdev->host->hostdata;
1457
1458 if (capacity >= 0x200000) {
1459 heads = 255;
1460 sectors = 63;
1461 } else {
1462 heads = 64;
1463 sectors = 32;
1464 }
1465
1466 geom[0] = heads;
1467 geom[1] = sectors;
1468 geom[2] = sector_div(capacity, heads * sectors);
1469
1470 return 0;
1471}
1472
1473
1474static int twl_scsi_eh_reset(struct scsi_cmnd *SCpnt)
1475{
1476 TW_Device_Extension *tw_dev = NULL;
1477 int retval = FAILED;
1478
1479 tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1480
1481 tw_dev->num_resets++;
1482
1483 sdev_printk(KERN_WARNING, SCpnt->device,
1484 "WARNING: (0x%02X:0x%04X): Command (0x%x) timed out, resetting card.\n",
1485 TW_DRIVER, 0x2c, SCpnt->cmnd[0]);
1486
1487
1488 mutex_lock(&tw_dev->ioctl_lock);
1489
1490
1491 if (twl_reset_device_extension(tw_dev, 0)) {
1492 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x15, "Controller reset failed during scsi host reset");
1493 goto out;
1494 }
1495
1496 retval = SUCCESS;
1497out:
1498 mutex_unlock(&tw_dev->ioctl_lock);
1499 return retval;
1500}
1501
1502
1503static int twl_scsi_queue_lck(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
1504{
1505 int request_id, retval;
1506 TW_Device_Extension *tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1507
1508
1509 if (test_bit(TW_IN_RESET, &tw_dev->flags)) {
1510 retval = SCSI_MLQUEUE_HOST_BUSY;
1511 goto out;
1512 }
1513
1514
1515 SCpnt->scsi_done = done;
1516
1517
1518 twl_get_request_id(tw_dev, &request_id);
1519
1520
1521 tw_dev->srb[request_id] = SCpnt;
1522
1523
1524 SCpnt->SCp.phase = TW_PHASE_INITIAL;
1525
1526 retval = twl_scsiop_execute_scsi(tw_dev, request_id, NULL, 0, NULL);
1527 if (retval) {
1528 tw_dev->state[request_id] = TW_S_COMPLETED;
1529 twl_free_request_id(tw_dev, request_id);
1530 SCpnt->result = (DID_ERROR << 16);
1531 done(SCpnt);
1532 retval = 0;
1533 }
1534out:
1535 return retval;
1536}
1537
1538static DEF_SCSI_QCMD(twl_scsi_queue)
1539
1540
1541static void __twl_shutdown(TW_Device_Extension *tw_dev)
1542{
1543
1544 TWL_MASK_INTERRUPTS(tw_dev);
1545
1546
1547 free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
1548
1549 printk(KERN_WARNING "3w-sas: Shutting down host %d.\n", tw_dev->host->host_no);
1550
1551
1552 if (twl_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
1553 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x16, "Connection shutdown failed");
1554 } else {
1555 printk(KERN_WARNING "3w-sas: Shutdown complete.\n");
1556 }
1557
1558
1559 TWL_CLEAR_DB_INTERRUPT(tw_dev);
1560}
1561
1562
1563static void twl_shutdown(struct pci_dev *pdev)
1564{
1565 struct Scsi_Host *host = pci_get_drvdata(pdev);
1566 TW_Device_Extension *tw_dev;
1567
1568 if (!host)
1569 return;
1570
1571 tw_dev = (TW_Device_Extension *)host->hostdata;
1572
1573 if (tw_dev->online)
1574 __twl_shutdown(tw_dev);
1575}
1576
1577
1578static int twl_slave_configure(struct scsi_device *sdev)
1579{
1580
1581 blk_queue_rq_timeout(sdev->request_queue, 60 * HZ);
1582
1583 return 0;
1584}
1585
1586
1587static struct scsi_host_template driver_template = {
1588 .module = THIS_MODULE,
1589 .name = "3w-sas",
1590 .queuecommand = twl_scsi_queue,
1591 .eh_host_reset_handler = twl_scsi_eh_reset,
1592 .bios_param = twl_scsi_biosparam,
1593 .change_queue_depth = twl_change_queue_depth,
1594 .can_queue = TW_Q_LENGTH-2,
1595 .slave_configure = twl_slave_configure,
1596 .this_id = -1,
1597 .sg_tablesize = TW_LIBERATOR_MAX_SGL_LENGTH,
1598 .max_sectors = TW_MAX_SECTORS,
1599 .cmd_per_lun = TW_MAX_CMDS_PER_LUN,
1600 .use_clustering = ENABLE_CLUSTERING,
1601 .shost_attrs = twl_host_attrs,
1602 .emulated = 1,
1603 .no_write_same = 1,
1604};
1605
1606
1607static int twl_probe(struct pci_dev *pdev, const struct pci_device_id *dev_id)
1608{
1609 struct Scsi_Host *host = NULL;
1610 TW_Device_Extension *tw_dev;
1611 int retval = -ENODEV;
1612 int *ptr_phycount, phycount=0;
1613
1614 retval = pci_enable_device(pdev);
1615 if (retval) {
1616 TW_PRINTK(host, TW_DRIVER, 0x17, "Failed to enable pci device");
1617 goto out_disable_device;
1618 }
1619
1620 pci_set_master(pdev);
1621 pci_try_set_mwi(pdev);
1622
1623 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))
1624 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
1625 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
1626 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
1627 TW_PRINTK(host, TW_DRIVER, 0x18, "Failed to set dma mask");
1628 retval = -ENODEV;
1629 goto out_disable_device;
1630 }
1631
1632 host = scsi_host_alloc(&driver_template, sizeof(TW_Device_Extension));
1633 if (!host) {
1634 TW_PRINTK(host, TW_DRIVER, 0x19, "Failed to allocate memory for device extension");
1635 retval = -ENOMEM;
1636 goto out_disable_device;
1637 }
1638 tw_dev = shost_priv(host);
1639
1640
1641 tw_dev->host = host;
1642 tw_dev->tw_pci_dev = pdev;
1643
1644 if (twl_initialize_device_extension(tw_dev)) {
1645 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1a, "Failed to initialize device extension");
1646 goto out_free_device_extension;
1647 }
1648
1649
1650 retval = pci_request_regions(pdev, "3w-sas");
1651 if (retval) {
1652 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1b, "Failed to get mem region");
1653 goto out_free_device_extension;
1654 }
1655
1656
1657 tw_dev->base_addr = pci_iomap(pdev, 1, 0);
1658 if (!tw_dev->base_addr) {
1659 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1c, "Failed to ioremap");
1660 goto out_release_mem_region;
1661 }
1662
1663
1664 TWL_MASK_INTERRUPTS(tw_dev);
1665
1666
1667 if (twl_reset_sequence(tw_dev, 0)) {
1668 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1d, "Controller reset failed during probe");
1669 goto out_iounmap;
1670 }
1671
1672
1673 host->max_id = TW_MAX_UNITS;
1674 host->max_cmd_len = TW_MAX_CDB_LEN;
1675 host->max_lun = TW_MAX_LUNS;
1676 host->max_channel = 0;
1677
1678
1679 retval = scsi_add_host(host, &pdev->dev);
1680 if (retval) {
1681 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1e, "scsi add host failed");
1682 goto out_iounmap;
1683 }
1684
1685 pci_set_drvdata(pdev, host);
1686
1687 printk(KERN_WARNING "3w-sas: scsi%d: Found an LSI 3ware %s Controller at 0x%llx, IRQ: %d.\n",
1688 host->host_no,
1689 (char *)twl_get_param(tw_dev, 1, TW_VERSION_TABLE,
1690 TW_PARAM_MODEL, TW_PARAM_MODEL_LENGTH),
1691 (u64)pci_resource_start(pdev, 1), pdev->irq);
1692
1693 ptr_phycount = twl_get_param(tw_dev, 2, TW_PARAM_PHY_SUMMARY_TABLE,
1694 TW_PARAM_PHYCOUNT, TW_PARAM_PHYCOUNT_LENGTH);
1695 if (ptr_phycount)
1696 phycount = le32_to_cpu(*(int *)ptr_phycount);
1697
1698 printk(KERN_WARNING "3w-sas: scsi%d: Firmware %s, BIOS %s, Phys: %d.\n",
1699 host->host_no,
1700 (char *)twl_get_param(tw_dev, 1, TW_VERSION_TABLE,
1701 TW_PARAM_FWVER, TW_PARAM_FWVER_LENGTH),
1702 (char *)twl_get_param(tw_dev, 2, TW_VERSION_TABLE,
1703 TW_PARAM_BIOSVER, TW_PARAM_BIOSVER_LENGTH),
1704 phycount);
1705
1706
1707 if (use_msi && !pci_enable_msi(pdev))
1708 set_bit(TW_USING_MSI, &tw_dev->flags);
1709
1710
1711 retval = request_irq(pdev->irq, twl_interrupt, IRQF_SHARED, "3w-sas", tw_dev);
1712 if (retval) {
1713 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1f, "Error requesting IRQ");
1714 goto out_remove_host;
1715 }
1716
1717 twl_device_extension_list[twl_device_extension_count] = tw_dev;
1718 twl_device_extension_count++;
1719
1720
1721 TWL_UNMASK_INTERRUPTS(tw_dev);
1722
1723
1724 scsi_scan_host(host);
1725
1726
1727 if (sysfs_create_bin_file(&host->shost_dev.kobj, &twl_sysfs_aen_read_attr))
1728 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x20, "Failed to create sysfs binary file: 3ware_aen_read");
1729 if (sysfs_create_bin_file(&host->shost_dev.kobj, &twl_sysfs_compat_info_attr))
1730 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x21, "Failed to create sysfs binary file: 3ware_compat_info");
1731
1732 if (twl_major == -1) {
1733 if ((twl_major = register_chrdev (0, "twl", &twl_fops)) < 0)
1734 TW_PRINTK(host, TW_DRIVER, 0x22, "Failed to register character device");
1735 }
1736 tw_dev->online = 1;
1737 return 0;
1738
1739out_remove_host:
1740 if (test_bit(TW_USING_MSI, &tw_dev->flags))
1741 pci_disable_msi(pdev);
1742 scsi_remove_host(host);
1743out_iounmap:
1744 iounmap(tw_dev->base_addr);
1745out_release_mem_region:
1746 pci_release_regions(pdev);
1747out_free_device_extension:
1748 twl_free_device_extension(tw_dev);
1749 scsi_host_put(host);
1750out_disable_device:
1751 pci_disable_device(pdev);
1752
1753 return retval;
1754}
1755
1756
1757static void twl_remove(struct pci_dev *pdev)
1758{
1759 struct Scsi_Host *host = pci_get_drvdata(pdev);
1760 TW_Device_Extension *tw_dev;
1761
1762 if (!host)
1763 return;
1764
1765 tw_dev = (TW_Device_Extension *)host->hostdata;
1766
1767 if (!tw_dev->online)
1768 return;
1769
1770
1771 sysfs_remove_bin_file(&host->shost_dev.kobj, &twl_sysfs_aen_read_attr);
1772 sysfs_remove_bin_file(&host->shost_dev.kobj, &twl_sysfs_compat_info_attr);
1773
1774 scsi_remove_host(tw_dev->host);
1775
1776
1777 if (twl_major >= 0) {
1778 unregister_chrdev(twl_major, "twl");
1779 twl_major = -1;
1780 }
1781
1782
1783 __twl_shutdown(tw_dev);
1784
1785
1786 if (test_bit(TW_USING_MSI, &tw_dev->flags))
1787 pci_disable_msi(pdev);
1788
1789
1790 iounmap(tw_dev->base_addr);
1791
1792
1793 pci_release_regions(pdev);
1794
1795
1796 twl_free_device_extension(tw_dev);
1797
1798 scsi_host_put(tw_dev->host);
1799 pci_disable_device(pdev);
1800 twl_device_extension_count--;
1801}
1802
1803#ifdef CONFIG_PM
1804
1805static int twl_suspend(struct pci_dev *pdev, pm_message_t state)
1806{
1807 struct Scsi_Host *host = pci_get_drvdata(pdev);
1808 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
1809
1810 printk(KERN_WARNING "3w-sas: Suspending host %d.\n", tw_dev->host->host_no);
1811
1812 TWL_MASK_INTERRUPTS(tw_dev);
1813
1814 free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
1815
1816
1817 if (twl_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
1818 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x23, "Connection shutdown failed during suspend");
1819 } else {
1820 printk(KERN_WARNING "3w-sas: Suspend complete.\n");
1821 }
1822
1823
1824 TWL_CLEAR_DB_INTERRUPT(tw_dev);
1825
1826 pci_save_state(pdev);
1827 pci_disable_device(pdev);
1828 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1829
1830 return 0;
1831}
1832
1833
1834static int twl_resume(struct pci_dev *pdev)
1835{
1836 int retval = 0;
1837 struct Scsi_Host *host = pci_get_drvdata(pdev);
1838 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
1839
1840 printk(KERN_WARNING "3w-sas: Resuming host %d.\n", tw_dev->host->host_no);
1841 pci_set_power_state(pdev, PCI_D0);
1842 pci_enable_wake(pdev, PCI_D0, 0);
1843 pci_restore_state(pdev);
1844
1845 retval = pci_enable_device(pdev);
1846 if (retval) {
1847 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x24, "Enable device failed during resume");
1848 return retval;
1849 }
1850
1851 pci_set_master(pdev);
1852 pci_try_set_mwi(pdev);
1853
1854 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))
1855 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
1856 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
1857 || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
1858 TW_PRINTK(host, TW_DRIVER, 0x25, "Failed to set dma mask during resume");
1859 retval = -ENODEV;
1860 goto out_disable_device;
1861 }
1862
1863
1864 if (twl_reset_sequence(tw_dev, 0)) {
1865 retval = -ENODEV;
1866 goto out_disable_device;
1867 }
1868
1869
1870 retval = request_irq(pdev->irq, twl_interrupt, IRQF_SHARED, "3w-sas", tw_dev);
1871 if (retval) {
1872 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x26, "Error requesting IRQ during resume");
1873 retval = -ENODEV;
1874 goto out_disable_device;
1875 }
1876
1877
1878 if (test_bit(TW_USING_MSI, &tw_dev->flags))
1879 pci_enable_msi(pdev);
1880
1881
1882 TWL_UNMASK_INTERRUPTS(tw_dev);
1883
1884 printk(KERN_WARNING "3w-sas: Resume complete.\n");
1885 return 0;
1886
1887out_disable_device:
1888 scsi_remove_host(host);
1889 pci_disable_device(pdev);
1890
1891 return retval;
1892}
1893#endif
1894
1895
1896static struct pci_device_id twl_pci_tbl[] = {
1897 { PCI_VDEVICE(3WARE, PCI_DEVICE_ID_3WARE_9750) },
1898 { }
1899};
1900MODULE_DEVICE_TABLE(pci, twl_pci_tbl);
1901
1902
1903static struct pci_driver twl_driver = {
1904 .name = "3w-sas",
1905 .id_table = twl_pci_tbl,
1906 .probe = twl_probe,
1907 .remove = twl_remove,
1908#ifdef CONFIG_PM
1909 .suspend = twl_suspend,
1910 .resume = twl_resume,
1911#endif
1912 .shutdown = twl_shutdown
1913};
1914
1915
1916static int __init twl_init(void)
1917{
1918 printk(KERN_INFO "LSI 3ware SAS/SATA-RAID Controller device driver for Linux v%s.\n", TW_DRIVER_VERSION);
1919
1920 return pci_register_driver(&twl_driver);
1921}
1922
1923
1924static void __exit twl_exit(void)
1925{
1926 pci_unregister_driver(&twl_driver);
1927}
1928
1929module_init(twl_init);
1930module_exit(twl_exit);
1931
1932