2 * Aic94xx SAS/SATA driver initialization.
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
7 * This file is licensed under GPLv2.
9 * This file is part of the aic94xx driver.
11 * The aic94xx driver is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; version 2 of the
16 * The aic94xx driver is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with the aic94xx driver; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/kernel.h>
30 #include <linux/pci.h>
31 #include <linux/delay.h>
32 #include <linux/firmware.h>
34 #include <scsi/scsi_host.h>
37 #include "aic94xx_reg.h"
38 #include "aic94xx_hwi.h"
39 #include "aic94xx_seq.h"
40 #include "aic94xx_sds.h"
42 /* The format is "version.release.patchlevel" */
43 #define ASD_DRIVER_VERSION "1.0.3"
45 static int use_msi = 0;
46 module_param_named(use_msi, use_msi, int, S_IRUGO);
47 MODULE_PARM_DESC(use_msi, "\n"
48 "\tEnable(1) or disable(0) using PCI MSI.\n"
51 static int lldd_max_execute_num = 0;
52 module_param_named(collector, lldd_max_execute_num, int, S_IRUGO);
53 MODULE_PARM_DESC(collector, "\n"
54 "\tIf greater than one, tells the SAS Layer to run in Task Collector\n"
55 "\tMode. If 1 or 0, tells the SAS Layer to run in Direct Mode.\n"
56 "\tThe aic94xx SAS LLDD supports both modes.\n"
57 "\tDefault: 0 (Direct Mode).\n");
59 char sas_addr_str[2*SAS_ADDR_SIZE + 1] = "";
61 static struct scsi_transport_template *aic94xx_transport_template;
62 static int asd_scan_finished(struct Scsi_Host *, unsigned long);
63 static void asd_scan_start(struct Scsi_Host *);
65 static struct scsi_host_template aic94xx_sht = {
66 .module = THIS_MODULE,
67 /* .name is initialized */
69 .queuecommand = sas_queuecommand,
70 .target_alloc = sas_target_alloc,
71 .slave_configure = sas_slave_configure,
72 .slave_destroy = sas_slave_destroy,
73 .scan_finished = asd_scan_finished,
74 .scan_start = asd_scan_start,
75 .change_queue_depth = sas_change_queue_depth,
76 .change_queue_type = sas_change_queue_type,
77 .bios_param = sas_bios_param,
81 .sg_tablesize = SG_ALL,
82 .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
83 .use_clustering = ENABLE_CLUSTERING,
84 .eh_device_reset_handler = sas_eh_device_reset_handler,
85 .eh_bus_reset_handler = sas_eh_bus_reset_handler,
86 .slave_alloc = sas_slave_alloc,
87 .target_destroy = sas_target_destroy,
91 static int __devinit asd_map_memio(struct asd_ha_struct *asd_ha)
94 struct asd_ha_addrspace *io_handle;
97 for (i = 0; i < 3; i += 2) {
98 io_handle = &asd_ha->io_handle[i==0?0:1];
99 io_handle->start = pci_resource_start(asd_ha->pcidev, i);
100 io_handle->len = pci_resource_len(asd_ha->pcidev, i);
101 io_handle->flags = pci_resource_flags(asd_ha->pcidev, i);
103 if (!io_handle->start || !io_handle->len) {
104 asd_printk("MBAR%d start or length for %s is 0.\n",
105 i==0?0:1, pci_name(asd_ha->pcidev));
108 err = pci_request_region(asd_ha->pcidev, i, ASD_DRIVER_NAME);
110 asd_printk("couldn't reserve memory region for %s\n",
111 pci_name(asd_ha->pcidev));
114 if (io_handle->flags & IORESOURCE_CACHEABLE)
115 io_handle->addr = ioremap(io_handle->start,
118 io_handle->addr = ioremap_nocache(io_handle->start,
120 if (!io_handle->addr) {
121 asd_printk("couldn't map MBAR%d of %s\n", i==0?0:1,
122 pci_name(asd_ha->pcidev));
129 pci_release_region(asd_ha->pcidev, i);
132 io_handle = &asd_ha->io_handle[0];
133 iounmap(io_handle->addr);
134 pci_release_region(asd_ha->pcidev, 0);
139 static void asd_unmap_memio(struct asd_ha_struct *asd_ha)
141 struct asd_ha_addrspace *io_handle;
143 io_handle = &asd_ha->io_handle[1];
144 iounmap(io_handle->addr);
145 pci_release_region(asd_ha->pcidev, 2);
147 io_handle = &asd_ha->io_handle[0];
148 iounmap(io_handle->addr);
149 pci_release_region(asd_ha->pcidev, 0);
152 static int __devinit asd_map_ioport(struct asd_ha_struct *asd_ha)
154 int i = PCI_IOBAR_OFFSET, err;
155 struct asd_ha_addrspace *io_handle = &asd_ha->io_handle[0];
158 io_handle->start = pci_resource_start(asd_ha->pcidev, i);
159 io_handle->len = pci_resource_len(asd_ha->pcidev, i);
160 io_handle->flags = pci_resource_flags(asd_ha->pcidev, i);
161 io_handle->addr = (void __iomem *) io_handle->start;
162 if (!io_handle->start || !io_handle->len) {
163 asd_printk("couldn't get IO ports for %s\n",
164 pci_name(asd_ha->pcidev));
167 err = pci_request_region(asd_ha->pcidev, i, ASD_DRIVER_NAME);
169 asd_printk("couldn't reserve io space for %s\n",
170 pci_name(asd_ha->pcidev));
176 static void asd_unmap_ioport(struct asd_ha_struct *asd_ha)
178 pci_release_region(asd_ha->pcidev, PCI_IOBAR_OFFSET);
181 static int __devinit asd_map_ha(struct asd_ha_struct *asd_ha)
186 err = pci_read_config_word(asd_ha->pcidev, PCI_COMMAND, &cmd_reg);
188 asd_printk("couldn't read command register of %s\n",
189 pci_name(asd_ha->pcidev));
194 if (cmd_reg & PCI_COMMAND_MEMORY) {
195 if ((err = asd_map_memio(asd_ha)))
197 } else if (cmd_reg & PCI_COMMAND_IO) {
198 if ((err = asd_map_ioport(asd_ha)))
200 asd_printk("%s ioport mapped -- upgrade your hardware\n",
201 pci_name(asd_ha->pcidev));
203 asd_printk("no proper device access to %s\n",
204 pci_name(asd_ha->pcidev));
213 static void asd_unmap_ha(struct asd_ha_struct *asd_ha)
216 asd_unmap_ioport(asd_ha);
218 asd_unmap_memio(asd_ha);
221 static const char *asd_dev_rev[30] = {
227 static int __devinit asd_common_setup(struct asd_ha_struct *asd_ha)
231 asd_ha->revision_id = asd_ha->pcidev->revision;
234 if (asd_ha->revision_id < AIC9410_DEV_REV_B0) {
235 asd_printk("%s is revision %s (%X), which is not supported\n",
236 pci_name(asd_ha->pcidev),
237 asd_dev_rev[asd_ha->revision_id],
238 asd_ha->revision_id);
241 /* Provide some sane default values. */
242 asd_ha->hw_prof.max_scbs = 512;
243 asd_ha->hw_prof.max_ddbs = ASD_MAX_DDBS;
244 asd_ha->hw_prof.num_phys = ASD_MAX_PHYS;
245 /* All phys are enabled, by default. */
246 asd_ha->hw_prof.enabled_phys = 0xFF;
247 for (i = 0; i < ASD_MAX_PHYS; i++) {
248 asd_ha->hw_prof.phy_desc[i].max_sas_lrate =
249 SAS_LINK_RATE_3_0_GBPS;
250 asd_ha->hw_prof.phy_desc[i].min_sas_lrate =
251 SAS_LINK_RATE_1_5_GBPS;
252 asd_ha->hw_prof.phy_desc[i].max_sata_lrate =
253 SAS_LINK_RATE_1_5_GBPS;
254 asd_ha->hw_prof.phy_desc[i].min_sata_lrate =
255 SAS_LINK_RATE_1_5_GBPS;
263 static int __devinit asd_aic9410_setup(struct asd_ha_struct *asd_ha)
265 int err = asd_common_setup(asd_ha);
270 asd_ha->hw_prof.addr_range = 8;
271 asd_ha->hw_prof.port_name_base = 0;
272 asd_ha->hw_prof.dev_name_base = 8;
273 asd_ha->hw_prof.sata_name_base = 16;
278 static int __devinit asd_aic9405_setup(struct asd_ha_struct *asd_ha)
280 int err = asd_common_setup(asd_ha);
285 asd_ha->hw_prof.addr_range = 4;
286 asd_ha->hw_prof.port_name_base = 0;
287 asd_ha->hw_prof.dev_name_base = 4;
288 asd_ha->hw_prof.sata_name_base = 8;
293 static ssize_t asd_show_dev_rev(struct device *dev,
294 struct device_attribute *attr, char *buf)
296 struct asd_ha_struct *asd_ha = dev_to_asd_ha(dev);
297 return snprintf(buf, PAGE_SIZE, "%s\n",
298 asd_dev_rev[asd_ha->revision_id]);
300 static DEVICE_ATTR(revision, S_IRUGO, asd_show_dev_rev, NULL);
302 static ssize_t asd_show_dev_bios_build(struct device *dev,
303 struct device_attribute *attr,char *buf)
305 struct asd_ha_struct *asd_ha = dev_to_asd_ha(dev);
306 return snprintf(buf, PAGE_SIZE, "%d\n", asd_ha->hw_prof.bios.bld);
308 static DEVICE_ATTR(bios_build, S_IRUGO, asd_show_dev_bios_build, NULL);
310 static ssize_t asd_show_dev_pcba_sn(struct device *dev,
311 struct device_attribute *attr, char *buf)
313 struct asd_ha_struct *asd_ha = dev_to_asd_ha(dev);
314 return snprintf(buf, PAGE_SIZE, "%s\n", asd_ha->hw_prof.pcba_sn);
316 static DEVICE_ATTR(pcba_sn, S_IRUGO, asd_show_dev_pcba_sn, NULL);
318 #define FLASH_CMD_NONE 0x00
319 #define FLASH_CMD_UPDATE 0x01
320 #define FLASH_CMD_VERIFY 0x02
322 struct flash_command {
327 static struct flash_command flash_command_table[] =
329 {"verify", FLASH_CMD_VERIFY},
330 {"update", FLASH_CMD_UPDATE},
331 {"", FLASH_CMD_NONE} /* Last entry should be NULL. */
339 static struct error_bios flash_error_table[] =
341 {"Failed to open bios image file", FAIL_OPEN_BIOS_FILE},
342 {"PCI ID mismatch", FAIL_CHECK_PCI_ID},
343 {"Checksum mismatch", FAIL_CHECK_SUM},
344 {"Unknown Error", FAIL_UNKNOWN},
345 {"Failed to verify.", FAIL_VERIFY},
346 {"Failed to reset flash chip.", FAIL_RESET_FLASH},
347 {"Failed to find flash chip type.", FAIL_FIND_FLASH_ID},
348 {"Failed to erash flash chip.", FAIL_ERASE_FLASH},
349 {"Failed to program flash chip.", FAIL_WRITE_FLASH},
350 {"Flash in progress", FLASH_IN_PROGRESS},
351 {"Image file size Error", FAIL_FILE_SIZE},
352 {"Input parameter error", FAIL_PARAMETERS},
353 {"Out of memory", FAIL_OUT_MEMORY},
354 {"OK", 0} /* Last entry err_code = 0. */
357 static ssize_t asd_store_update_bios(struct device *dev,
358 struct device_attribute *attr,
359 const char *buf, size_t count)
361 struct asd_ha_struct *asd_ha = dev_to_asd_ha(dev);
362 char *cmd_ptr, *filename_ptr;
363 struct bios_file_header header, *hdr_ptr;
366 int flash_command = FLASH_CMD_NONE;
369 cmd_ptr = kzalloc(count*2, GFP_KERNEL);
372 err = FAIL_OUT_MEMORY;
376 filename_ptr = cmd_ptr + count;
377 res = sscanf(buf, "%s %s", cmd_ptr, filename_ptr);
379 err = FAIL_PARAMETERS;
383 for (i = 0; flash_command_table[i].code != FLASH_CMD_NONE; i++) {
384 if (!memcmp(flash_command_table[i].command,
385 cmd_ptr, strlen(cmd_ptr))) {
386 flash_command = flash_command_table[i].code;
390 if (flash_command == FLASH_CMD_NONE) {
391 err = FAIL_PARAMETERS;
395 if (asd_ha->bios_status == FLASH_IN_PROGRESS) {
396 err = FLASH_IN_PROGRESS;
399 err = request_firmware(&asd_ha->bios_image,
401 &asd_ha->pcidev->dev);
403 asd_printk("Failed to load bios image file %s, error %d\n",
405 err = FAIL_OPEN_BIOS_FILE;
409 hdr_ptr = (struct bios_file_header *)asd_ha->bios_image->data;
411 if ((hdr_ptr->contrl_id.vendor != asd_ha->pcidev->vendor ||
412 hdr_ptr->contrl_id.device != asd_ha->pcidev->device) &&
413 (hdr_ptr->contrl_id.sub_vendor != asd_ha->pcidev->vendor ||
414 hdr_ptr->contrl_id.sub_device != asd_ha->pcidev->device)) {
416 ASD_DPRINTK("The PCI vendor or device id does not match\n");
417 ASD_DPRINTK("vendor=%x dev=%x sub_vendor=%x sub_dev=%x"
418 " pci vendor=%x pci dev=%x\n",
419 hdr_ptr->contrl_id.vendor,
420 hdr_ptr->contrl_id.device,
421 hdr_ptr->contrl_id.sub_vendor,
422 hdr_ptr->contrl_id.sub_device,
423 asd_ha->pcidev->vendor,
424 asd_ha->pcidev->device);
425 err = FAIL_CHECK_PCI_ID;
429 if (hdr_ptr->filelen != asd_ha->bios_image->size) {
430 err = FAIL_FILE_SIZE;
434 /* calculate checksum */
435 for (i = 0; i < hdr_ptr->filelen; i++)
436 csum += asd_ha->bios_image->data[i];
438 if ((csum & 0x0000ffff) != hdr_ptr->checksum) {
439 ASD_DPRINTK("BIOS file checksum mismatch\n");
440 err = FAIL_CHECK_SUM;
443 if (flash_command == FLASH_CMD_UPDATE) {
444 asd_ha->bios_status = FLASH_IN_PROGRESS;
445 err = asd_write_flash_seg(asd_ha,
446 &asd_ha->bios_image->data[sizeof(*hdr_ptr)],
447 0, hdr_ptr->filelen-sizeof(*hdr_ptr));
449 err = asd_verify_flash_seg(asd_ha,
450 &asd_ha->bios_image->data[sizeof(*hdr_ptr)],
451 0, hdr_ptr->filelen-sizeof(*hdr_ptr));
453 asd_ha->bios_status = FLASH_IN_PROGRESS;
454 err = asd_verify_flash_seg(asd_ha,
455 &asd_ha->bios_image->data[sizeof(header)],
456 0, hdr_ptr->filelen-sizeof(header));
460 release_firmware(asd_ha->bios_image);
464 asd_ha->bios_status = err;
472 static ssize_t asd_show_update_bios(struct device *dev,
473 struct device_attribute *attr, char *buf)
476 struct asd_ha_struct *asd_ha = dev_to_asd_ha(dev);
478 for (i = 0; flash_error_table[i].err_code != 0; i++) {
479 if (flash_error_table[i].err_code == asd_ha->bios_status)
482 if (asd_ha->bios_status != FLASH_IN_PROGRESS)
483 asd_ha->bios_status = FLASH_OK;
485 return snprintf(buf, PAGE_SIZE, "status=%x %s\n",
486 flash_error_table[i].err_code,
487 flash_error_table[i].reason);
490 static DEVICE_ATTR(update_bios, S_IRUGO|S_IWUGO,
491 asd_show_update_bios, asd_store_update_bios);
493 static int asd_create_dev_attrs(struct asd_ha_struct *asd_ha)
497 err = device_create_file(&asd_ha->pcidev->dev, &dev_attr_revision);
501 err = device_create_file(&asd_ha->pcidev->dev, &dev_attr_bios_build);
505 err = device_create_file(&asd_ha->pcidev->dev, &dev_attr_pcba_sn);
508 err = device_create_file(&asd_ha->pcidev->dev, &dev_attr_update_bios);
510 goto err_update_bios;
515 device_remove_file(&asd_ha->pcidev->dev, &dev_attr_pcba_sn);
517 device_remove_file(&asd_ha->pcidev->dev, &dev_attr_bios_build);
519 device_remove_file(&asd_ha->pcidev->dev, &dev_attr_revision);
523 static void asd_remove_dev_attrs(struct asd_ha_struct *asd_ha)
525 device_remove_file(&asd_ha->pcidev->dev, &dev_attr_revision);
526 device_remove_file(&asd_ha->pcidev->dev, &dev_attr_bios_build);
527 device_remove_file(&asd_ha->pcidev->dev, &dev_attr_pcba_sn);
528 device_remove_file(&asd_ha->pcidev->dev, &dev_attr_update_bios);
531 /* The first entry, 0, is used for dynamic ids, the rest for devices
534 static struct asd_pcidev_struct {
536 int (*setup)(struct asd_ha_struct *asd_ha);
537 } asd_pcidev_data[] = {
538 /* Id 0 is used for dynamic ids. */
539 { .name = "Adaptec AIC-94xx SAS/SATA Host Adapter",
540 .setup = asd_aic9410_setup
542 { .name = "Adaptec AIC-9410W SAS/SATA Host Adapter",
543 .setup = asd_aic9410_setup
545 { .name = "Adaptec AIC-9405W SAS/SATA Host Adapter",
546 .setup = asd_aic9405_setup
550 static inline int asd_create_ha_caches(struct asd_ha_struct *asd_ha)
552 asd_ha->scb_pool = dma_pool_create(ASD_DRIVER_NAME "_scb_pool",
553 &asd_ha->pcidev->dev,
556 if (!asd_ha->scb_pool) {
557 asd_printk("couldn't create scb pool\n");
565 * asd_free_edbs -- free empty data buffers
566 * asd_ha: pointer to host adapter structure
568 static inline void asd_free_edbs(struct asd_ha_struct *asd_ha)
570 struct asd_seq_data *seq = &asd_ha->seq;
573 for (i = 0; i < seq->num_edbs; i++)
574 asd_free_coherent(asd_ha, seq->edb_arr[i]);
579 static inline void asd_free_escbs(struct asd_ha_struct *asd_ha)
581 struct asd_seq_data *seq = &asd_ha->seq;
584 for (i = 0; i < seq->num_escbs; i++) {
585 if (!list_empty(&seq->escb_arr[i]->list))
586 list_del_init(&seq->escb_arr[i]->list);
588 asd_ascb_free(seq->escb_arr[i]);
590 kfree(seq->escb_arr);
591 seq->escb_arr = NULL;
594 static inline void asd_destroy_ha_caches(struct asd_ha_struct *asd_ha)
598 if (asd_ha->hw_prof.ddb_ext)
599 asd_free_coherent(asd_ha, asd_ha->hw_prof.ddb_ext);
600 if (asd_ha->hw_prof.scb_ext)
601 asd_free_coherent(asd_ha, asd_ha->hw_prof.scb_ext);
603 if (asd_ha->hw_prof.ddb_bitmap)
604 kfree(asd_ha->hw_prof.ddb_bitmap);
605 asd_ha->hw_prof.ddb_bitmap = NULL;
607 for (i = 0; i < ASD_MAX_PHYS; i++) {
608 struct asd_phy *phy = &asd_ha->phys[i];
610 asd_free_coherent(asd_ha, phy->id_frm_tok);
612 if (asd_ha->seq.escb_arr)
613 asd_free_escbs(asd_ha);
614 if (asd_ha->seq.edb_arr)
615 asd_free_edbs(asd_ha);
616 if (asd_ha->hw_prof.ue.area) {
617 kfree(asd_ha->hw_prof.ue.area);
618 asd_ha->hw_prof.ue.area = NULL;
620 if (asd_ha->seq.tc_index_array) {
621 kfree(asd_ha->seq.tc_index_array);
622 kfree(asd_ha->seq.tc_index_bitmap);
623 asd_ha->seq.tc_index_array = NULL;
624 asd_ha->seq.tc_index_bitmap = NULL;
626 if (asd_ha->seq.actual_dl) {
627 asd_free_coherent(asd_ha, asd_ha->seq.actual_dl);
628 asd_ha->seq.actual_dl = NULL;
629 asd_ha->seq.dl = NULL;
631 if (asd_ha->seq.next_scb.vaddr) {
632 dma_pool_free(asd_ha->scb_pool, asd_ha->seq.next_scb.vaddr,
633 asd_ha->seq.next_scb.dma_handle);
634 asd_ha->seq.next_scb.vaddr = NULL;
636 dma_pool_destroy(asd_ha->scb_pool);
637 asd_ha->scb_pool = NULL;
640 struct kmem_cache *asd_dma_token_cache;
641 struct kmem_cache *asd_ascb_cache;
643 static int asd_create_global_caches(void)
645 if (!asd_dma_token_cache) {
647 = kmem_cache_create(ASD_DRIVER_NAME "_dma_token",
648 sizeof(struct asd_dma_tok),
652 if (!asd_dma_token_cache) {
653 asd_printk("couldn't create dma token cache\n");
658 if (!asd_ascb_cache) {
659 asd_ascb_cache = kmem_cache_create(ASD_DRIVER_NAME "_ascb",
660 sizeof(struct asd_ascb),
664 if (!asd_ascb_cache) {
665 asd_printk("couldn't create ascb cache\n");
672 kmem_cache_destroy(asd_dma_token_cache);
673 asd_dma_token_cache = NULL;
677 static void asd_destroy_global_caches(void)
679 if (asd_dma_token_cache)
680 kmem_cache_destroy(asd_dma_token_cache);
681 asd_dma_token_cache = NULL;
684 kmem_cache_destroy(asd_ascb_cache);
685 asd_ascb_cache = NULL;
688 static int asd_register_sas_ha(struct asd_ha_struct *asd_ha)
691 struct asd_sas_phy **sas_phys =
692 kmalloc(ASD_MAX_PHYS * sizeof(struct asd_sas_phy), GFP_KERNEL);
693 struct asd_sas_port **sas_ports =
694 kmalloc(ASD_MAX_PHYS * sizeof(struct asd_sas_port), GFP_KERNEL);
696 if (!sas_phys || !sas_ports) {
702 asd_ha->sas_ha.sas_ha_name = (char *) asd_ha->name;
703 asd_ha->sas_ha.lldd_module = THIS_MODULE;
704 asd_ha->sas_ha.sas_addr = &asd_ha->hw_prof.sas_addr[0];
706 for (i = 0; i < ASD_MAX_PHYS; i++) {
707 sas_phys[i] = &asd_ha->phys[i].sas_phy;
708 sas_ports[i] = &asd_ha->ports[i];
711 asd_ha->sas_ha.sas_phy = sas_phys;
712 asd_ha->sas_ha.sas_port= sas_ports;
713 asd_ha->sas_ha.num_phys= ASD_MAX_PHYS;
715 asd_ha->sas_ha.lldd_queue_size = asd_ha->seq.can_queue;
716 asd_ha->sas_ha.lldd_max_execute_num = lldd_max_execute_num;
718 return sas_register_ha(&asd_ha->sas_ha);
721 static int asd_unregister_sas_ha(struct asd_ha_struct *asd_ha)
725 err = sas_unregister_ha(&asd_ha->sas_ha);
727 sas_remove_host(asd_ha->sas_ha.core.shost);
728 scsi_remove_host(asd_ha->sas_ha.core.shost);
729 scsi_host_put(asd_ha->sas_ha.core.shost);
731 kfree(asd_ha->sas_ha.sas_phy);
732 kfree(asd_ha->sas_ha.sas_port);
737 static int __devinit asd_pci_probe(struct pci_dev *dev,
738 const struct pci_device_id *id)
740 struct asd_pcidev_struct *asd_dev;
741 unsigned asd_id = (unsigned) id->driver_data;
742 struct asd_ha_struct *asd_ha;
743 struct Scsi_Host *shost;
746 if (asd_id >= ARRAY_SIZE(asd_pcidev_data)) {
747 asd_printk("wrong driver_data in PCI table\n");
751 if ((err = pci_enable_device(dev))) {
752 asd_printk("couldn't enable device %s\n", pci_name(dev));
760 shost = scsi_host_alloc(&aic94xx_sht, sizeof(void *));
764 asd_dev = &asd_pcidev_data[asd_id];
766 asd_ha = kzalloc(sizeof(*asd_ha), GFP_KERNEL);
768 asd_printk("out of memory\n");
771 asd_ha->pcidev = dev;
772 asd_ha->sas_ha.dev = &asd_ha->pcidev->dev;
773 asd_ha->sas_ha.lldd_ha = asd_ha;
775 asd_ha->bios_status = FLASH_OK;
776 asd_ha->name = asd_dev->name;
777 asd_printk("found %s, device %s\n", asd_ha->name, pci_name(dev));
779 SHOST_TO_SAS_HA(shost) = &asd_ha->sas_ha;
780 asd_ha->sas_ha.core.shost = shost;
781 shost->transportt = aic94xx_transport_template;
784 shost->max_cmd_len = 16;
786 err = scsi_add_host(shost, &dev->dev);
790 err = asd_dev->setup(asd_ha);
795 if (!pci_set_dma_mask(dev, DMA_64BIT_MASK)
796 && !pci_set_consistent_dma_mask(dev, DMA_64BIT_MASK))
798 else if (!pci_set_dma_mask(dev, DMA_32BIT_MASK)
799 && !pci_set_consistent_dma_mask(dev, DMA_32BIT_MASK))
802 asd_printk("no suitable DMA mask for %s\n", pci_name(dev));
806 pci_set_drvdata(dev, asd_ha);
808 err = asd_map_ha(asd_ha);
812 err = asd_create_ha_caches(asd_ha);
816 err = asd_init_hw(asd_ha);
820 asd_printk("device %s: SAS addr %llx, PCBA SN %s, %d phys, %d enabled "
821 "phys, flash %s, BIOS %s%d\n",
822 pci_name(dev), SAS_ADDR(asd_ha->hw_prof.sas_addr),
823 asd_ha->hw_prof.pcba_sn, asd_ha->hw_prof.max_phys,
824 asd_ha->hw_prof.num_phys,
825 asd_ha->hw_prof.flash.present ? "present" : "not present",
826 asd_ha->hw_prof.bios.present ? "build " : "not present",
827 asd_ha->hw_prof.bios.bld);
829 shost->can_queue = asd_ha->seq.can_queue;
832 pci_enable_msi(asd_ha->pcidev);
834 err = request_irq(asd_ha->pcidev->irq, asd_hw_isr, IRQF_SHARED,
835 ASD_DRIVER_NAME, asd_ha);
837 asd_printk("couldn't get irq %d for %s\n",
838 asd_ha->pcidev->irq, pci_name(asd_ha->pcidev));
841 asd_enable_ints(asd_ha);
843 err = asd_init_post_escbs(asd_ha);
845 asd_printk("couldn't post escbs for %s\n",
846 pci_name(asd_ha->pcidev));
849 ASD_DPRINTK("escbs posted\n");
851 err = asd_create_dev_attrs(asd_ha);
855 err = asd_register_sas_ha(asd_ha);
859 scsi_scan_host(shost);
864 asd_remove_dev_attrs(asd_ha);
867 asd_disable_ints(asd_ha);
868 free_irq(dev->irq, asd_ha);
871 pci_disable_msi(dev);
872 asd_chip_hardrst(asd_ha);
874 asd_destroy_ha_caches(asd_ha);
876 asd_unmap_ha(asd_ha);
878 scsi_remove_host(shost);
882 scsi_host_put(shost);
884 pci_disable_device(dev);
888 static void asd_free_queues(struct asd_ha_struct *asd_ha)
892 struct list_head *n, *pos;
894 spin_lock_irqsave(&asd_ha->seq.pend_q_lock, flags);
895 asd_ha->seq.pending = 0;
896 list_splice_init(&asd_ha->seq.pend_q, &pending);
897 spin_unlock_irqrestore(&asd_ha->seq.pend_q_lock, flags);
899 if (!list_empty(&pending))
900 ASD_DPRINTK("Uh-oh! Pending is not empty!\n");
902 list_for_each_safe(pos, n, &pending) {
903 struct asd_ascb *ascb = list_entry(pos, struct asd_ascb, list);
905 * Delete unexpired ascb timers. This may happen if we issue
906 * a CONTROL PHY scb to an adapter and rmmod before the scb
907 * times out. Apparently we don't wait for the CONTROL PHY
908 * to complete, so it doesn't matter if we kill the timer.
910 del_timer_sync(&ascb->timer);
911 WARN_ON(ascb->scb->header.opcode != CONTROL_PHY);
914 ASD_DPRINTK("freeing from pending\n");
919 static void asd_turn_off_leds(struct asd_ha_struct *asd_ha)
921 u8 phy_mask = asd_ha->hw_prof.enabled_phys;
924 for_each_phy(phy_mask, phy_mask, i) {
925 asd_turn_led(asd_ha, i, 0);
926 asd_control_led(asd_ha, i, 0);
930 static void __devexit asd_pci_remove(struct pci_dev *dev)
932 struct asd_ha_struct *asd_ha = pci_get_drvdata(dev);
937 asd_unregister_sas_ha(asd_ha);
939 asd_disable_ints(asd_ha);
941 asd_remove_dev_attrs(asd_ha);
943 /* XXX more here as needed */
945 free_irq(dev->irq, asd_ha);
947 pci_disable_msi(asd_ha->pcidev);
948 asd_turn_off_leds(asd_ha);
949 asd_chip_hardrst(asd_ha);
950 asd_free_queues(asd_ha);
951 asd_destroy_ha_caches(asd_ha);
952 asd_unmap_ha(asd_ha);
954 pci_disable_device(dev);
958 static void asd_scan_start(struct Scsi_Host *shost)
960 struct asd_ha_struct *asd_ha;
963 asd_ha = SHOST_TO_SAS_HA(shost)->lldd_ha;
964 err = asd_enable_phys(asd_ha, asd_ha->hw_prof.enabled_phys);
966 asd_printk("Couldn't enable phys, err:%d\n", err);
969 static int asd_scan_finished(struct Scsi_Host *shost, unsigned long time)
971 /* give the phy enabling interrupt event time to come in (1s
972 * is empirically about all it takes) */
975 /* Wait for discovery to finish */
976 scsi_flush_work(shost);
980 static ssize_t asd_version_show(struct device_driver *driver, char *buf)
982 return snprintf(buf, PAGE_SIZE, "%s\n", ASD_DRIVER_VERSION);
984 static DRIVER_ATTR(version, S_IRUGO, asd_version_show, NULL);
986 static int asd_create_driver_attrs(struct device_driver *driver)
988 return driver_create_file(driver, &driver_attr_version);
991 static void asd_remove_driver_attrs(struct device_driver *driver)
993 driver_remove_file(driver, &driver_attr_version);
996 static struct sas_domain_function_template aic94xx_transport_functions = {
997 .lldd_dev_found = asd_dev_found,
998 .lldd_dev_gone = asd_dev_gone,
1000 .lldd_execute_task = asd_execute_task,
1002 .lldd_abort_task = asd_abort_task,
1003 .lldd_abort_task_set = asd_abort_task_set,
1004 .lldd_clear_aca = asd_clear_aca,
1005 .lldd_clear_task_set = asd_clear_task_set,
1006 .lldd_I_T_nexus_reset = NULL,
1007 .lldd_lu_reset = asd_lu_reset,
1008 .lldd_query_task = asd_query_task,
1010 .lldd_clear_nexus_port = asd_clear_nexus_port,
1011 .lldd_clear_nexus_ha = asd_clear_nexus_ha,
1013 .lldd_control_phy = asd_control_phy,
1016 static const struct pci_device_id aic94xx_pci_table[] __devinitdata = {
1017 {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x410),0, 0, 1},
1018 {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x412),0, 0, 1},
1019 {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x416),0, 0, 1},
1020 {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x41E),0, 0, 1},
1021 {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x41F),0, 0, 1},
1022 {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x430),0, 0, 2},
1023 {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x432),0, 0, 2},
1024 {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x43E),0, 0, 2},
1025 {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x43F),0, 0, 2},
1029 MODULE_DEVICE_TABLE(pci, aic94xx_pci_table);
1031 static struct pci_driver aic94xx_pci_driver = {
1032 .name = ASD_DRIVER_NAME,
1033 .id_table = aic94xx_pci_table,
1034 .probe = asd_pci_probe,
1035 .remove = __devexit_p(asd_pci_remove),
1038 static int __init aic94xx_init(void)
1043 asd_printk("%s version %s loaded\n", ASD_DRIVER_DESCRIPTION,
1044 ASD_DRIVER_VERSION);
1046 err = asd_create_global_caches();
1050 aic94xx_transport_template =
1051 sas_domain_attach_transport(&aic94xx_transport_functions);
1052 if (!aic94xx_transport_template)
1053 goto out_destroy_caches;
1055 err = pci_register_driver(&aic94xx_pci_driver);
1057 goto out_release_transport;
1059 err = asd_create_driver_attrs(&aic94xx_pci_driver.driver);
1061 goto out_unregister_pcidrv;
1065 out_unregister_pcidrv:
1066 pci_unregister_driver(&aic94xx_pci_driver);
1067 out_release_transport:
1068 sas_release_transport(aic94xx_transport_template);
1070 asd_destroy_global_caches();
1075 static void __exit aic94xx_exit(void)
1077 asd_remove_driver_attrs(&aic94xx_pci_driver.driver);
1078 pci_unregister_driver(&aic94xx_pci_driver);
1079 sas_release_transport(aic94xx_transport_template);
1080 asd_release_firmware();
1081 asd_destroy_global_caches();
1082 asd_printk("%s version %s unloaded\n", ASD_DRIVER_DESCRIPTION,
1083 ASD_DRIVER_VERSION);
1086 module_init(aic94xx_init);
1087 module_exit(aic94xx_exit);
1089 MODULE_AUTHOR("Luben Tuikov <luben_tuikov@adaptec.com>");
1090 MODULE_DESCRIPTION(ASD_DRIVER_DESCRIPTION);
1091 MODULE_LICENSE("GPL v2");
1092 MODULE_VERSION(ASD_DRIVER_VERSION);