]> err.no Git - linux-2.6/blob - drivers/scsi/aic94xx/aic94xx_init.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / drivers / scsi / aic94xx / aic94xx_init.c
1 /*
2  * Aic94xx SAS/SATA driver initialization.
3  *
4  * Copyright (C) 2005 Adaptec, Inc.  All rights reserved.
5  * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6  *
7  * This file is licensed under GPLv2.
8  *
9  * This file is part of the aic94xx driver.
10  *
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
14  * License.
15  *
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.
20  *
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
24  *
25  */
26
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>
33
34 #include <scsi/scsi_host.h>
35
36 #include "aic94xx.h"
37 #include "aic94xx_reg.h"
38 #include "aic94xx_hwi.h"
39 #include "aic94xx_seq.h"
40 #include "aic94xx_sds.h"
41
42 /* The format is "version.release.patchlevel" */
43 #define ASD_DRIVER_VERSION "1.0.3"
44
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"
49         "\tDefault: 0");
50
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");
58
59 char sas_addr_str[2*SAS_ADDR_SIZE + 1] = "";
60
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 *);
64
65 static struct scsi_host_template aic94xx_sht = {
66         .module                 = THIS_MODULE,
67         /* .name is initialized */
68         .name                   = "aic94xx",
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,
78         .can_queue              = 1,
79         .cmd_per_lun            = 1,
80         .this_id                = -1,
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,
88         .ioctl                  = sas_ioctl,
89 };
90
91 static int __devinit asd_map_memio(struct asd_ha_struct *asd_ha)
92 {
93         int err, i;
94         struct asd_ha_addrspace *io_handle;
95
96         asd_ha->iospace = 0;
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);
102                 err = -ENODEV;
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));
106                         goto Err;
107                 }
108                 err = pci_request_region(asd_ha->pcidev, i, ASD_DRIVER_NAME);
109                 if (err) {
110                         asd_printk("couldn't reserve memory region for %s\n",
111                                    pci_name(asd_ha->pcidev));
112                         goto Err;
113                 }
114                 if (io_handle->flags & IORESOURCE_CACHEABLE)
115                         io_handle->addr = ioremap(io_handle->start,
116                                                   io_handle->len);
117                 else
118                         io_handle->addr = ioremap_nocache(io_handle->start,
119                                                           io_handle->len);
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));
123                         goto Err_unreq;
124                 }
125         }
126
127         return 0;
128 Err_unreq:
129         pci_release_region(asd_ha->pcidev, i);
130 Err:
131         if (i > 0) {
132                 io_handle = &asd_ha->io_handle[0];
133                 iounmap(io_handle->addr);
134                 pci_release_region(asd_ha->pcidev, 0);
135         }
136         return err;
137 }
138
139 static void asd_unmap_memio(struct asd_ha_struct *asd_ha)
140 {
141         struct asd_ha_addrspace *io_handle;
142
143         io_handle = &asd_ha->io_handle[1];
144         iounmap(io_handle->addr);
145         pci_release_region(asd_ha->pcidev, 2);
146
147         io_handle = &asd_ha->io_handle[0];
148         iounmap(io_handle->addr);
149         pci_release_region(asd_ha->pcidev, 0);
150 }
151
152 static int __devinit asd_map_ioport(struct asd_ha_struct *asd_ha)
153 {
154         int i = PCI_IOBAR_OFFSET, err;
155         struct asd_ha_addrspace *io_handle = &asd_ha->io_handle[0];
156
157         asd_ha->iospace = 1;
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));
165                 return -ENODEV;
166         }
167         err = pci_request_region(asd_ha->pcidev, i, ASD_DRIVER_NAME);
168         if (err) {
169                 asd_printk("couldn't reserve io space for %s\n",
170                            pci_name(asd_ha->pcidev));
171         }
172
173         return err;
174 }
175
176 static void asd_unmap_ioport(struct asd_ha_struct *asd_ha)
177 {
178         pci_release_region(asd_ha->pcidev, PCI_IOBAR_OFFSET);
179 }
180
181 static int __devinit asd_map_ha(struct asd_ha_struct *asd_ha)
182 {
183         int err;
184         u16 cmd_reg;
185
186         err = pci_read_config_word(asd_ha->pcidev, PCI_COMMAND, &cmd_reg);
187         if (err) {
188                 asd_printk("couldn't read command register of %s\n",
189                            pci_name(asd_ha->pcidev));
190                 goto Err;
191         }
192
193         err = -ENODEV;
194         if (cmd_reg & PCI_COMMAND_MEMORY) {
195                 if ((err = asd_map_memio(asd_ha)))
196                         goto Err;
197         } else if (cmd_reg & PCI_COMMAND_IO) {
198                 if ((err = asd_map_ioport(asd_ha)))
199                         goto Err;
200                 asd_printk("%s ioport mapped -- upgrade your hardware\n",
201                            pci_name(asd_ha->pcidev));
202         } else {
203                 asd_printk("no proper device access to %s\n",
204                            pci_name(asd_ha->pcidev));
205                 goto Err;
206         }
207
208         return 0;
209 Err:
210         return err;
211 }
212
213 static void asd_unmap_ha(struct asd_ha_struct *asd_ha)
214 {
215         if (asd_ha->iospace)
216                 asd_unmap_ioport(asd_ha);
217         else
218                 asd_unmap_memio(asd_ha);
219 }
220
221 static const char *asd_dev_rev[30] = {
222         [0] = "A0",
223         [1] = "A1",
224         [8] = "B0",
225 };
226
227 static int __devinit asd_common_setup(struct asd_ha_struct *asd_ha)
228 {
229         int err, i;
230
231         asd_ha->revision_id = asd_ha->pcidev->revision;
232
233         err = -ENODEV;
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);
239                 goto Err;
240         }
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;
256         }
257
258         return 0;
259 Err:
260         return err;
261 }
262
263 static int __devinit asd_aic9410_setup(struct asd_ha_struct *asd_ha)
264 {
265         int err = asd_common_setup(asd_ha);
266
267         if (err)
268                 return err;
269
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;
274
275         return 0;
276 }
277
278 static int __devinit asd_aic9405_setup(struct asd_ha_struct *asd_ha)
279 {
280         int err = asd_common_setup(asd_ha);
281
282         if (err)
283                 return err;
284
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;
289
290         return 0;
291 }
292
293 static ssize_t asd_show_dev_rev(struct device *dev,
294                                 struct device_attribute *attr, char *buf)
295 {
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]);
299 }
300 static DEVICE_ATTR(revision, S_IRUGO, asd_show_dev_rev, NULL);
301
302 static ssize_t asd_show_dev_bios_build(struct device *dev,
303                                        struct device_attribute *attr,char *buf)
304 {
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);
307 }
308 static DEVICE_ATTR(bios_build, S_IRUGO, asd_show_dev_bios_build, NULL);
309
310 static ssize_t asd_show_dev_pcba_sn(struct device *dev,
311                                     struct device_attribute *attr, char *buf)
312 {
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);
315 }
316 static DEVICE_ATTR(pcba_sn, S_IRUGO, asd_show_dev_pcba_sn, NULL);
317
318 #define FLASH_CMD_NONE      0x00
319 #define FLASH_CMD_UPDATE    0x01
320 #define FLASH_CMD_VERIFY    0x02
321
322 struct flash_command {
323      u8      command[8];
324      int     code;
325 };
326
327 static struct flash_command flash_command_table[] =
328 {
329      {"verify",      FLASH_CMD_VERIFY},
330      {"update",      FLASH_CMD_UPDATE},
331      {"",            FLASH_CMD_NONE}      /* Last entry should be NULL. */
332 };
333
334 struct error_bios {
335      char    *reason;
336      int     err_code;
337 };
338
339 static struct error_bios flash_error_table[] =
340 {
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. */
355 };
356
357 static ssize_t asd_store_update_bios(struct device *dev,
358         struct device_attribute *attr,
359         const char *buf, size_t count)
360 {
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;
364         int res, i;
365         u32 csum = 0;
366         int flash_command = FLASH_CMD_NONE;
367         int err = 0;
368
369         cmd_ptr = kzalloc(count*2, GFP_KERNEL);
370
371         if (!cmd_ptr) {
372                 err = FAIL_OUT_MEMORY;
373                 goto out;
374         }
375
376         filename_ptr = cmd_ptr + count;
377         res = sscanf(buf, "%s %s", cmd_ptr, filename_ptr);
378         if (res != 2) {
379                 err = FAIL_PARAMETERS;
380                 goto out1;
381         }
382
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;
387                         break;
388                 }
389         }
390         if (flash_command == FLASH_CMD_NONE) {
391                 err = FAIL_PARAMETERS;
392                 goto out1;
393         }
394
395         if (asd_ha->bios_status == FLASH_IN_PROGRESS) {
396                 err = FLASH_IN_PROGRESS;
397                 goto out1;
398         }
399         err = request_firmware(&asd_ha->bios_image,
400                                    filename_ptr,
401                                    &asd_ha->pcidev->dev);
402         if (err) {
403                 asd_printk("Failed to load bios image file %s, error %d\n",
404                            filename_ptr, err);
405                 err = FAIL_OPEN_BIOS_FILE;
406                 goto out1;
407         }
408
409         hdr_ptr = (struct bios_file_header *)asd_ha->bios_image->data;
410
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)) {
415
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;
426                 goto out2;
427         }
428
429         if (hdr_ptr->filelen != asd_ha->bios_image->size) {
430                 err = FAIL_FILE_SIZE;
431                 goto out2;
432         }
433
434         /* calculate checksum */
435         for (i = 0; i < hdr_ptr->filelen; i++)
436                 csum += asd_ha->bios_image->data[i];
437
438         if ((csum & 0x0000ffff) != hdr_ptr->checksum) {
439                 ASD_DPRINTK("BIOS file checksum mismatch\n");
440                 err = FAIL_CHECK_SUM;
441                 goto out2;
442         }
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));
448                 if (!err)
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));
452         } else {
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));
457         }
458
459 out2:
460         release_firmware(asd_ha->bios_image);
461 out1:
462         kfree(cmd_ptr);
463 out:
464         asd_ha->bios_status = err;
465
466         if (!err)
467                 return count;
468         else
469                 return -err;
470 }
471
472 static ssize_t asd_show_update_bios(struct device *dev,
473                                     struct device_attribute *attr, char *buf)
474 {
475         int i;
476         struct asd_ha_struct *asd_ha = dev_to_asd_ha(dev);
477
478         for (i = 0; flash_error_table[i].err_code != 0; i++) {
479                 if (flash_error_table[i].err_code == asd_ha->bios_status)
480                         break;
481         }
482         if (asd_ha->bios_status != FLASH_IN_PROGRESS)
483                 asd_ha->bios_status = FLASH_OK;
484
485         return snprintf(buf, PAGE_SIZE, "status=%x %s\n",
486                         flash_error_table[i].err_code,
487                         flash_error_table[i].reason);
488 }
489
490 static DEVICE_ATTR(update_bios, S_IRUGO|S_IWUGO,
491         asd_show_update_bios, asd_store_update_bios);
492
493 static int asd_create_dev_attrs(struct asd_ha_struct *asd_ha)
494 {
495         int err;
496
497         err = device_create_file(&asd_ha->pcidev->dev, &dev_attr_revision);
498         if (err)
499                 return err;
500
501         err = device_create_file(&asd_ha->pcidev->dev, &dev_attr_bios_build);
502         if (err)
503                 goto err_rev;
504
505         err = device_create_file(&asd_ha->pcidev->dev, &dev_attr_pcba_sn);
506         if (err)
507                 goto err_biosb;
508         err = device_create_file(&asd_ha->pcidev->dev, &dev_attr_update_bios);
509         if (err)
510                 goto err_update_bios;
511
512         return 0;
513
514 err_update_bios:
515         device_remove_file(&asd_ha->pcidev->dev, &dev_attr_pcba_sn);
516 err_biosb:
517         device_remove_file(&asd_ha->pcidev->dev, &dev_attr_bios_build);
518 err_rev:
519         device_remove_file(&asd_ha->pcidev->dev, &dev_attr_revision);
520         return err;
521 }
522
523 static void asd_remove_dev_attrs(struct asd_ha_struct *asd_ha)
524 {
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);
529 }
530
531 /* The first entry, 0, is used for dynamic ids, the rest for devices
532  * we know about.
533  */
534 static struct asd_pcidev_struct {
535         const char * name;
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
541         },
542         { .name  = "Adaptec AIC-9410W SAS/SATA Host Adapter",
543           .setup = asd_aic9410_setup
544         },
545         { .name  = "Adaptec AIC-9405W SAS/SATA Host Adapter",
546           .setup = asd_aic9405_setup
547         },
548 };
549
550 static inline int asd_create_ha_caches(struct asd_ha_struct *asd_ha)
551 {
552         asd_ha->scb_pool = dma_pool_create(ASD_DRIVER_NAME "_scb_pool",
553                                            &asd_ha->pcidev->dev,
554                                            sizeof(struct scb),
555                                            8, 0);
556         if (!asd_ha->scb_pool) {
557                 asd_printk("couldn't create scb pool\n");
558                 return -ENOMEM;
559         }
560
561         return 0;
562 }
563
564 /**
565  * asd_free_edbs -- free empty data buffers
566  * asd_ha: pointer to host adapter structure
567  */
568 static inline void asd_free_edbs(struct asd_ha_struct *asd_ha)
569 {
570         struct asd_seq_data *seq = &asd_ha->seq;
571         int i;
572
573         for (i = 0; i < seq->num_edbs; i++)
574                 asd_free_coherent(asd_ha, seq->edb_arr[i]);
575         kfree(seq->edb_arr);
576         seq->edb_arr = NULL;
577 }
578
579 static inline void asd_free_escbs(struct asd_ha_struct *asd_ha)
580 {
581         struct asd_seq_data *seq = &asd_ha->seq;
582         int i;
583
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);
587
588                 asd_ascb_free(seq->escb_arr[i]);
589         }
590         kfree(seq->escb_arr);
591         seq->escb_arr = NULL;
592 }
593
594 static inline void asd_destroy_ha_caches(struct asd_ha_struct *asd_ha)
595 {
596         int i;
597
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);
602
603         if (asd_ha->hw_prof.ddb_bitmap)
604                 kfree(asd_ha->hw_prof.ddb_bitmap);
605         asd_ha->hw_prof.ddb_bitmap = NULL;
606
607         for (i = 0; i < ASD_MAX_PHYS; i++) {
608                 struct asd_phy *phy = &asd_ha->phys[i];
609
610                 asd_free_coherent(asd_ha, phy->id_frm_tok);
611         }
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;
619         }
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;
625         }
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;
630         }
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;
635         }
636         dma_pool_destroy(asd_ha->scb_pool);
637         asd_ha->scb_pool = NULL;
638 }
639
640 struct kmem_cache *asd_dma_token_cache;
641 struct kmem_cache *asd_ascb_cache;
642
643 static int asd_create_global_caches(void)
644 {
645         if (!asd_dma_token_cache) {
646                 asd_dma_token_cache
647                         = kmem_cache_create(ASD_DRIVER_NAME "_dma_token",
648                                             sizeof(struct asd_dma_tok),
649                                             0,
650                                             SLAB_HWCACHE_ALIGN,
651                                             NULL);
652                 if (!asd_dma_token_cache) {
653                         asd_printk("couldn't create dma token cache\n");
654                         return -ENOMEM;
655                 }
656         }
657
658         if (!asd_ascb_cache) {
659                 asd_ascb_cache = kmem_cache_create(ASD_DRIVER_NAME "_ascb",
660                                                    sizeof(struct asd_ascb),
661                                                    0,
662                                                    SLAB_HWCACHE_ALIGN,
663                                                    NULL);
664                 if (!asd_ascb_cache) {
665                         asd_printk("couldn't create ascb cache\n");
666                         goto Err;
667                 }
668         }
669
670         return 0;
671 Err:
672         kmem_cache_destroy(asd_dma_token_cache);
673         asd_dma_token_cache = NULL;
674         return -ENOMEM;
675 }
676
677 static void asd_destroy_global_caches(void)
678 {
679         if (asd_dma_token_cache)
680                 kmem_cache_destroy(asd_dma_token_cache);
681         asd_dma_token_cache = NULL;
682
683         if (asd_ascb_cache)
684                 kmem_cache_destroy(asd_ascb_cache);
685         asd_ascb_cache = NULL;
686 }
687
688 static int asd_register_sas_ha(struct asd_ha_struct *asd_ha)
689 {
690         int i;
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);
695
696         if (!sas_phys || !sas_ports) {
697                 kfree(sas_phys);
698                 kfree(sas_ports);
699                 return -ENOMEM;
700         }
701
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];
705
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];
709         }
710
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;
714
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;
717
718         return sas_register_ha(&asd_ha->sas_ha);
719 }
720
721 static int asd_unregister_sas_ha(struct asd_ha_struct *asd_ha)
722 {
723         int err;
724
725         err = sas_unregister_ha(&asd_ha->sas_ha);
726
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);
730
731         kfree(asd_ha->sas_ha.sas_phy);
732         kfree(asd_ha->sas_ha.sas_port);
733
734         return err;
735 }
736
737 static int __devinit asd_pci_probe(struct pci_dev *dev,
738                                    const struct pci_device_id *id)
739 {
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;
744         int err;
745
746         if (asd_id >= ARRAY_SIZE(asd_pcidev_data)) {
747                 asd_printk("wrong driver_data in PCI table\n");
748                 return -ENODEV;
749         }
750
751         if ((err = pci_enable_device(dev))) {
752                 asd_printk("couldn't enable device %s\n", pci_name(dev));
753                 return err;
754         }
755
756         pci_set_master(dev);
757
758         err = -ENOMEM;
759
760         shost = scsi_host_alloc(&aic94xx_sht, sizeof(void *));
761         if (!shost)
762                 goto Err;
763
764         asd_dev = &asd_pcidev_data[asd_id];
765
766         asd_ha = kzalloc(sizeof(*asd_ha), GFP_KERNEL);
767         if (!asd_ha) {
768                 asd_printk("out of memory\n");
769                 goto Err_put;
770         }
771         asd_ha->pcidev = dev;
772         asd_ha->sas_ha.dev = &asd_ha->pcidev->dev;
773         asd_ha->sas_ha.lldd_ha = asd_ha;
774
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));
778
779         SHOST_TO_SAS_HA(shost) = &asd_ha->sas_ha;
780         asd_ha->sas_ha.core.shost = shost;
781         shost->transportt = aic94xx_transport_template;
782         shost->max_id = ~0;
783         shost->max_lun = ~0;
784         shost->max_cmd_len = 16;
785
786         err = scsi_add_host(shost, &dev->dev);
787         if (err)
788                 goto Err_free;
789
790         err = asd_dev->setup(asd_ha);
791         if (err)
792                 goto Err_remove;
793
794         err = -ENODEV;
795         if (!pci_set_dma_mask(dev, DMA_64BIT_MASK)
796             && !pci_set_consistent_dma_mask(dev, DMA_64BIT_MASK))
797                 ;
798         else if (!pci_set_dma_mask(dev, DMA_32BIT_MASK)
799                  && !pci_set_consistent_dma_mask(dev, DMA_32BIT_MASK))
800                 ;
801         else {
802                 asd_printk("no suitable DMA mask for %s\n", pci_name(dev));
803                 goto Err_remove;
804         }
805
806         pci_set_drvdata(dev, asd_ha);
807
808         err = asd_map_ha(asd_ha);
809         if (err)
810                 goto Err_remove;
811
812         err = asd_create_ha_caches(asd_ha);
813         if (err)
814                 goto Err_unmap;
815
816         err = asd_init_hw(asd_ha);
817         if (err)
818                 goto Err_free_cache;
819
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);
828
829         shost->can_queue = asd_ha->seq.can_queue;
830
831         if (use_msi)
832                 pci_enable_msi(asd_ha->pcidev);
833
834         err = request_irq(asd_ha->pcidev->irq, asd_hw_isr, IRQF_SHARED,
835                           ASD_DRIVER_NAME, asd_ha);
836         if (err) {
837                 asd_printk("couldn't get irq %d for %s\n",
838                            asd_ha->pcidev->irq, pci_name(asd_ha->pcidev));
839                 goto Err_irq;
840         }
841         asd_enable_ints(asd_ha);
842
843         err = asd_init_post_escbs(asd_ha);
844         if (err) {
845                 asd_printk("couldn't post escbs for %s\n",
846                            pci_name(asd_ha->pcidev));
847                 goto Err_escbs;
848         }
849         ASD_DPRINTK("escbs posted\n");
850
851         err = asd_create_dev_attrs(asd_ha);
852         if (err)
853                 goto Err_dev_attrs;
854
855         err = asd_register_sas_ha(asd_ha);
856         if (err)
857                 goto Err_reg_sas;
858
859         scsi_scan_host(shost);
860
861         return 0;
862
863 Err_reg_sas:
864         asd_remove_dev_attrs(asd_ha);
865 Err_dev_attrs:
866 Err_escbs:
867         asd_disable_ints(asd_ha);
868         free_irq(dev->irq, asd_ha);
869 Err_irq:
870         if (use_msi)
871                 pci_disable_msi(dev);
872         asd_chip_hardrst(asd_ha);
873 Err_free_cache:
874         asd_destroy_ha_caches(asd_ha);
875 Err_unmap:
876         asd_unmap_ha(asd_ha);
877 Err_remove:
878         scsi_remove_host(shost);
879 Err_free:
880         kfree(asd_ha);
881 Err_put:
882         scsi_host_put(shost);
883 Err:
884         pci_disable_device(dev);
885         return err;
886 }
887
888 static void asd_free_queues(struct asd_ha_struct *asd_ha)
889 {
890         unsigned long flags;
891         LIST_HEAD(pending);
892         struct list_head *n, *pos;
893
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);
898
899         if (!list_empty(&pending))
900                 ASD_DPRINTK("Uh-oh! Pending is not empty!\n");
901
902         list_for_each_safe(pos, n, &pending) {
903                 struct asd_ascb *ascb = list_entry(pos, struct asd_ascb, list);
904                 /*
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.
909                  */
910                 del_timer_sync(&ascb->timer);
911                 WARN_ON(ascb->scb->header.opcode != CONTROL_PHY);
912
913                 list_del_init(pos);
914                 ASD_DPRINTK("freeing from pending\n");
915                 asd_ascb_free(ascb);
916         }
917 }
918
919 static void asd_turn_off_leds(struct asd_ha_struct *asd_ha)
920 {
921         u8 phy_mask = asd_ha->hw_prof.enabled_phys;
922         u8 i;
923
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);
927         }
928 }
929
930 static void __devexit asd_pci_remove(struct pci_dev *dev)
931 {
932         struct asd_ha_struct *asd_ha = pci_get_drvdata(dev);
933
934         if (!asd_ha)
935                 return;
936
937         asd_unregister_sas_ha(asd_ha);
938
939         asd_disable_ints(asd_ha);
940
941         asd_remove_dev_attrs(asd_ha);
942
943         /* XXX more here as needed */
944
945         free_irq(dev->irq, asd_ha);
946         if (use_msi)
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);
953         kfree(asd_ha);
954         pci_disable_device(dev);
955         return;
956 }
957
958 static void asd_scan_start(struct Scsi_Host *shost)
959 {
960         struct asd_ha_struct *asd_ha;
961         int err;
962
963         asd_ha = SHOST_TO_SAS_HA(shost)->lldd_ha;
964         err = asd_enable_phys(asd_ha, asd_ha->hw_prof.enabled_phys);
965         if (err)
966                 asd_printk("Couldn't enable phys, err:%d\n", err);
967 }
968
969 static int asd_scan_finished(struct Scsi_Host *shost, unsigned long time)
970 {
971         /* give the phy enabling interrupt event time to come in (1s
972          * is empirically about all it takes) */
973         if (time < HZ)
974                 return 0;
975         /* Wait for discovery to finish */
976         scsi_flush_work(shost);
977         return 1;
978 }
979
980 static ssize_t asd_version_show(struct device_driver *driver, char *buf)
981 {
982         return snprintf(buf, PAGE_SIZE, "%s\n", ASD_DRIVER_VERSION);
983 }
984 static DRIVER_ATTR(version, S_IRUGO, asd_version_show, NULL);
985
986 static int asd_create_driver_attrs(struct device_driver *driver)
987 {
988         return driver_create_file(driver, &driver_attr_version);
989 }
990
991 static void asd_remove_driver_attrs(struct device_driver *driver)
992 {
993         driver_remove_file(driver, &driver_attr_version);
994 }
995
996 static struct sas_domain_function_template aic94xx_transport_functions = {
997         .lldd_dev_found         = asd_dev_found,
998         .lldd_dev_gone          = asd_dev_gone,
999
1000         .lldd_execute_task      = asd_execute_task,
1001
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   = asd_I_T_nexus_reset,
1007         .lldd_lu_reset          = asd_lu_reset,
1008         .lldd_query_task        = asd_query_task,
1009
1010         .lldd_clear_nexus_port  = asd_clear_nexus_port,
1011         .lldd_clear_nexus_ha    = asd_clear_nexus_ha,
1012
1013         .lldd_control_phy       = asd_control_phy,
1014 };
1015
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},
1026         {}
1027 };
1028
1029 MODULE_DEVICE_TABLE(pci, aic94xx_pci_table);
1030
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),
1036 };
1037
1038 static int __init aic94xx_init(void)
1039 {
1040         int err;
1041
1042
1043         asd_printk("%s version %s loaded\n", ASD_DRIVER_DESCRIPTION,
1044                    ASD_DRIVER_VERSION);
1045
1046         err = asd_create_global_caches();
1047         if (err)
1048                 return err;
1049
1050         aic94xx_transport_template =
1051                 sas_domain_attach_transport(&aic94xx_transport_functions);
1052         if (!aic94xx_transport_template)
1053                 goto out_destroy_caches;
1054
1055         err = pci_register_driver(&aic94xx_pci_driver);
1056         if (err)
1057                 goto out_release_transport;
1058
1059         err = asd_create_driver_attrs(&aic94xx_pci_driver.driver);
1060         if (err)
1061                 goto out_unregister_pcidrv;
1062
1063         return err;
1064
1065  out_unregister_pcidrv:
1066         pci_unregister_driver(&aic94xx_pci_driver);
1067  out_release_transport:
1068         sas_release_transport(aic94xx_transport_template);
1069  out_destroy_caches:
1070         asd_destroy_global_caches();
1071
1072         return err;
1073 }
1074
1075 static void __exit aic94xx_exit(void)
1076 {
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);
1084 }
1085
1086 module_init(aic94xx_init);
1087 module_exit(aic94xx_exit);
1088
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);