1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Aic94xx SAS/SATA driver initialization.
4  *
5  * Copyright (C) 2005 Adaptec, Inc.  All rights reserved.
6  * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
7  */
8 
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/pci.h>
13 #include <linux/delay.h>
14 #include <linux/firmware.h>
15 #include <linux/slab.h>
16 
17 #include <scsi/scsi_host.h>
18 
19 #include "aic94xx.h"
20 #include "aic94xx_reg.h"
21 #include "aic94xx_hwi.h"
22 #include "aic94xx_seq.h"
23 #include "aic94xx_sds.h"
24 
25 /* The format is "version.release.patchlevel" */
26 #define ASD_DRIVER_VERSION "1.0.3"
27 
28 static int use_msi = 0;
29 module_param_named(use_msi, use_msi, int, S_IRUGO);
30 MODULE_PARM_DESC(use_msi, "\n"
31 	"\tEnable(1) or disable(0) using PCI MSI.\n"
32 	"\tDefault: 0");
33 
34 static struct scsi_transport_template *aic94xx_transport_template;
35 static int asd_scan_finished(struct Scsi_Host *, unsigned long);
36 static void asd_scan_start(struct Scsi_Host *);
37 
38 static struct scsi_host_template aic94xx_sht = {
39 	.module			= THIS_MODULE,
40 	/* .name is initialized */
41 	.name			= "aic94xx",
42 	.queuecommand		= sas_queuecommand,
43 	.dma_need_drain		= ata_scsi_dma_need_drain,
44 	.target_alloc		= sas_target_alloc,
45 	.slave_configure	= sas_slave_configure,
46 	.scan_finished		= asd_scan_finished,
47 	.scan_start		= asd_scan_start,
48 	.change_queue_depth	= sas_change_queue_depth,
49 	.bios_param		= sas_bios_param,
50 	.can_queue		= 1,
51 	.this_id		= -1,
52 	.sg_tablesize		= SG_ALL,
53 	.max_sectors		= SCSI_DEFAULT_MAX_SECTORS,
54 	.eh_device_reset_handler	= sas_eh_device_reset_handler,
55 	.eh_target_reset_handler	= sas_eh_target_reset_handler,
56 	.slave_alloc		= sas_slave_alloc,
57 	.target_destroy		= sas_target_destroy,
58 	.ioctl			= sas_ioctl,
59 #ifdef CONFIG_COMPAT
60 	.compat_ioctl		= sas_ioctl,
61 #endif
62 	.track_queue_depth	= 1,
63 };
64 
65 static int asd_map_memio(struct asd_ha_struct *asd_ha)
66 {
67 	int err, i;
68 	struct asd_ha_addrspace *io_handle;
69 
70 	asd_ha->iospace = 0;
71 	for (i = 0; i < 3; i += 2) {
72 		io_handle = &asd_ha->io_handle[i==0?0:1];
73 		io_handle->start = pci_resource_start(asd_ha->pcidev, i);
74 		io_handle->len   = pci_resource_len(asd_ha->pcidev, i);
75 		io_handle->flags = pci_resource_flags(asd_ha->pcidev, i);
76 		err = -ENODEV;
77 		if (!io_handle->start || !io_handle->len) {
78 			asd_printk("MBAR%d start or length for %s is 0.\n",
79 				   i==0?0:1, pci_name(asd_ha->pcidev));
80 			goto Err;
81 		}
82 		err = pci_request_region(asd_ha->pcidev, i, ASD_DRIVER_NAME);
83 		if (err) {
84 			asd_printk("couldn't reserve memory region for %s\n",
85 				   pci_name(asd_ha->pcidev));
86 			goto Err;
87 		}
88 		io_handle->addr = ioremap(io_handle->start, io_handle->len);
89 		if (!io_handle->addr) {
90 			asd_printk("couldn't map MBAR%d of %s\n", i==0?0:1,
91 				   pci_name(asd_ha->pcidev));
92 			err = -ENOMEM;
93 			goto Err_unreq;
94 		}
95 	}
96 
97 	return 0;
98 Err_unreq:
99 	pci_release_region(asd_ha->pcidev, i);
100 Err:
101 	if (i > 0) {
102 		io_handle = &asd_ha->io_handle[0];
103 		iounmap(io_handle->addr);
104 		pci_release_region(asd_ha->pcidev, 0);
105 	}
106 	return err;
107 }
108 
109 static void asd_unmap_memio(struct asd_ha_struct *asd_ha)
110 {
111 	struct asd_ha_addrspace *io_handle;
112 
113 	io_handle = &asd_ha->io_handle[1];
114 	iounmap(io_handle->addr);
115 	pci_release_region(asd_ha->pcidev, 2);
116 
117 	io_handle = &asd_ha->io_handle[0];
118 	iounmap(io_handle->addr);
119 	pci_release_region(asd_ha->pcidev, 0);
120 }
121 
122 static int asd_map_ioport(struct asd_ha_struct *asd_ha)
123 {
124 	int i = PCI_IOBAR_OFFSET, err;
125 	struct asd_ha_addrspace *io_handle = &asd_ha->io_handle[0];
126 
127 	asd_ha->iospace = 1;
128 	io_handle->start = pci_resource_start(asd_ha->pcidev, i);
129 	io_handle->len   = pci_resource_len(asd_ha->pcidev, i);
130 	io_handle->flags = pci_resource_flags(asd_ha->pcidev, i);
131 	io_handle->addr  = (void __iomem *) io_handle->start;
132 	if (!io_handle->start || !io_handle->len) {
133 		asd_printk("couldn't get IO ports for %s\n",
134 			   pci_name(asd_ha->pcidev));
135 		return -ENODEV;
136 	}
137 	err = pci_request_region(asd_ha->pcidev, i, ASD_DRIVER_NAME);
138 	if (err) {
139 		asd_printk("couldn't reserve io space for %s\n",
140 			   pci_name(asd_ha->pcidev));
141 	}
142 
143 	return err;
144 }
145 
146 static void asd_unmap_ioport(struct asd_ha_struct *asd_ha)
147 {
148 	pci_release_region(asd_ha->pcidev, PCI_IOBAR_OFFSET);
149 }
150 
151 static int asd_map_ha(struct asd_ha_struct *asd_ha)
152 {
153 	int err;
154 	u16 cmd_reg;
155 
156 	err = pci_read_config_word(asd_ha->pcidev, PCI_COMMAND, &cmd_reg);
157 	if (err) {
158 		asd_printk("couldn't read command register of %s\n",
159 			   pci_name(asd_ha->pcidev));
160 		goto Err;
161 	}
162 
163 	err = -ENODEV;
164 	if (cmd_reg & PCI_COMMAND_MEMORY) {
165 		if ((err = asd_map_memio(asd_ha)))
166 			goto Err;
167 	} else if (cmd_reg & PCI_COMMAND_IO) {
168 		if ((err = asd_map_ioport(asd_ha)))
169 			goto Err;
170 		asd_printk("%s ioport mapped -- upgrade your hardware\n",
171 			   pci_name(asd_ha->pcidev));
172 	} else {
173 		asd_printk("no proper device access to %s\n",
174 			   pci_name(asd_ha->pcidev));
175 		goto Err;
176 	}
177 
178 	return 0;
179 Err:
180 	return err;
181 }
182 
183 static void asd_unmap_ha(struct asd_ha_struct *asd_ha)
184 {
185 	if (asd_ha->iospace)
186 		asd_unmap_ioport(asd_ha);
187 	else
188 		asd_unmap_memio(asd_ha);
189 }
190 
191 static const char *asd_dev_rev[30] = {
192 	[0] = "A0",
193 	[1] = "A1",
194 	[8] = "B0",
195 };
196 
197 static int asd_common_setup(struct asd_ha_struct *asd_ha)
198 {
199 	int err, i;
200 
201 	asd_ha->revision_id = asd_ha->pcidev->revision;
202 
203 	err = -ENODEV;
204 	if (asd_ha->revision_id < AIC9410_DEV_REV_B0) {
205 		asd_printk("%s is revision %s (%X), which is not supported\n",
206 			   pci_name(asd_ha->pcidev),
207 			   asd_dev_rev[asd_ha->revision_id],
208 			   asd_ha->revision_id);
209 		goto Err;
210 	}
211 	/* Provide some sane default values. */
212 	asd_ha->hw_prof.max_scbs = 512;
213 	asd_ha->hw_prof.max_ddbs = ASD_MAX_DDBS;
214 	asd_ha->hw_prof.num_phys = ASD_MAX_PHYS;
215 	/* All phys are enabled, by default. */
216 	asd_ha->hw_prof.enabled_phys = 0xFF;
217 	for (i = 0; i < ASD_MAX_PHYS; i++) {
218 		asd_ha->hw_prof.phy_desc[i].max_sas_lrate =
219 			SAS_LINK_RATE_3_0_GBPS;
220 		asd_ha->hw_prof.phy_desc[i].min_sas_lrate =
221 			SAS_LINK_RATE_1_5_GBPS;
222 		asd_ha->hw_prof.phy_desc[i].max_sata_lrate =
223 			SAS_LINK_RATE_1_5_GBPS;
224 		asd_ha->hw_prof.phy_desc[i].min_sata_lrate =
225 			SAS_LINK_RATE_1_5_GBPS;
226 	}
227 
228 	return 0;
229 Err:
230 	return err;
231 }
232 
233 static int asd_aic9410_setup(struct asd_ha_struct *asd_ha)
234 {
235 	int err = asd_common_setup(asd_ha);
236 
237 	if (err)
238 		return err;
239 
240 	asd_ha->hw_prof.addr_range = 8;
241 	asd_ha->hw_prof.port_name_base = 0;
242 	asd_ha->hw_prof.dev_name_base = 8;
243 	asd_ha->hw_prof.sata_name_base = 16;
244 
245 	return 0;
246 }
247 
248 static int asd_aic9405_setup(struct asd_ha_struct *asd_ha)
249 {
250 	int err = asd_common_setup(asd_ha);
251 
252 	if (err)
253 		return err;
254 
255 	asd_ha->hw_prof.addr_range = 4;
256 	asd_ha->hw_prof.port_name_base = 0;
257 	asd_ha->hw_prof.dev_name_base = 4;
258 	asd_ha->hw_prof.sata_name_base = 8;
259 
260 	return 0;
261 }
262 
263 static ssize_t asd_show_dev_rev(struct device *dev,
264 				struct device_attribute *attr, char *buf)
265 {
266 	struct asd_ha_struct *asd_ha = dev_to_asd_ha(dev);
267 	return snprintf(buf, PAGE_SIZE, "%s\n",
268 			asd_dev_rev[asd_ha->revision_id]);
269 }
270 static DEVICE_ATTR(aic_revision, S_IRUGO, asd_show_dev_rev, NULL);
271 
272 static ssize_t asd_show_dev_bios_build(struct device *dev,
273 				       struct device_attribute *attr,char *buf)
274 {
275 	struct asd_ha_struct *asd_ha = dev_to_asd_ha(dev);
276 	return snprintf(buf, PAGE_SIZE, "%d\n", asd_ha->hw_prof.bios.bld);
277 }
278 static DEVICE_ATTR(bios_build, S_IRUGO, asd_show_dev_bios_build, NULL);
279 
280 static ssize_t asd_show_dev_pcba_sn(struct device *dev,
281 				    struct device_attribute *attr, char *buf)
282 {
283 	struct asd_ha_struct *asd_ha = dev_to_asd_ha(dev);
284 	return snprintf(buf, PAGE_SIZE, "%s\n", asd_ha->hw_prof.pcba_sn);
285 }
286 static DEVICE_ATTR(pcba_sn, S_IRUGO, asd_show_dev_pcba_sn, NULL);
287 
288 #define FLASH_CMD_NONE      0x00
289 #define FLASH_CMD_UPDATE    0x01
290 #define FLASH_CMD_VERIFY    0x02
291 
292 struct flash_command {
293      u8      command[8];
294      int     code;
295 };
296 
297 static struct flash_command flash_command_table[] =
298 {
299      {"verify",      FLASH_CMD_VERIFY},
300      {"update",      FLASH_CMD_UPDATE},
301      {"",            FLASH_CMD_NONE}      /* Last entry should be NULL. */
302 };
303 
304 struct error_bios {
305      char    *reason;
306      int     err_code;
307 };
308 
309 static struct error_bios flash_error_table[] =
310 {
311      {"Failed to open bios image file",      FAIL_OPEN_BIOS_FILE},
312      {"PCI ID mismatch",                     FAIL_CHECK_PCI_ID},
313      {"Checksum mismatch",                   FAIL_CHECK_SUM},
314      {"Unknown Error",                       FAIL_UNKNOWN},
315      {"Failed to verify.",                   FAIL_VERIFY},
316      {"Failed to reset flash chip.",         FAIL_RESET_FLASH},
317      {"Failed to find flash chip type.",     FAIL_FIND_FLASH_ID},
318      {"Failed to erash flash chip.",         FAIL_ERASE_FLASH},
319      {"Failed to program flash chip.",       FAIL_WRITE_FLASH},
320      {"Flash in progress",                   FLASH_IN_PROGRESS},
321      {"Image file size Error",               FAIL_FILE_SIZE},
322      {"Input parameter error",               FAIL_PARAMETERS},
323      {"Out of memory",                       FAIL_OUT_MEMORY},
324      {"OK", 0}	/* Last entry err_code = 0. */
325 };
326 
327 static ssize_t asd_store_update_bios(struct device *dev,
328 	struct device_attribute *attr,
329 	const char *buf, size_t count)
330 {
331 	struct asd_ha_struct *asd_ha = dev_to_asd_ha(dev);
332 	char *cmd_ptr, *filename_ptr;
333 	struct bios_file_header header, *hdr_ptr;
334 	int res, i;
335 	u32 csum = 0;
336 	int flash_command = FLASH_CMD_NONE;
337 	int err = 0;
338 
339 	cmd_ptr = kcalloc(count, 2, GFP_KERNEL);
340 
341 	if (!cmd_ptr) {
342 		err = FAIL_OUT_MEMORY;
343 		goto out;
344 	}
345 
346 	filename_ptr = cmd_ptr + count;
347 	res = sscanf(buf, "%s %s", cmd_ptr, filename_ptr);
348 	if (res != 2) {
349 		err = FAIL_PARAMETERS;
350 		goto out1;
351 	}
352 
353 	for (i = 0; flash_command_table[i].code != FLASH_CMD_NONE; i++) {
354 		if (!memcmp(flash_command_table[i].command,
355 				 cmd_ptr, strlen(cmd_ptr))) {
356 			flash_command = flash_command_table[i].code;
357 			break;
358 		}
359 	}
360 	if (flash_command == FLASH_CMD_NONE) {
361 		err = FAIL_PARAMETERS;
362 		goto out1;
363 	}
364 
365 	if (asd_ha->bios_status == FLASH_IN_PROGRESS) {
366 		err = FLASH_IN_PROGRESS;
367 		goto out1;
368 	}
369 	err = request_firmware(&asd_ha->bios_image,
370 				   filename_ptr,
371 				   &asd_ha->pcidev->dev);
372 	if (err) {
373 		asd_printk("Failed to load bios image file %s, error %d\n",
374 			   filename_ptr, err);
375 		err = FAIL_OPEN_BIOS_FILE;
376 		goto out1;
377 	}
378 
379 	hdr_ptr = (struct bios_file_header *)asd_ha->bios_image->data;
380 
381 	if ((hdr_ptr->contrl_id.vendor != asd_ha->pcidev->vendor ||
382 		hdr_ptr->contrl_id.device != asd_ha->pcidev->device) &&
383 		(hdr_ptr->contrl_id.sub_vendor != asd_ha->pcidev->vendor ||
384 		hdr_ptr->contrl_id.sub_device != asd_ha->pcidev->device)) {
385 
386 		ASD_DPRINTK("The PCI vendor or device id does not match\n");
387 		ASD_DPRINTK("vendor=%x dev=%x sub_vendor=%x sub_dev=%x"
388 		" pci vendor=%x pci dev=%x\n",
389 		hdr_ptr->contrl_id.vendor,
390 		hdr_ptr->contrl_id.device,
391 		hdr_ptr->contrl_id.sub_vendor,
392 		hdr_ptr->contrl_id.sub_device,
393 		asd_ha->pcidev->vendor,
394 		asd_ha->pcidev->device);
395 		err = FAIL_CHECK_PCI_ID;
396 		goto out2;
397 	}
398 
399 	if (hdr_ptr->filelen != asd_ha->bios_image->size) {
400 		err = FAIL_FILE_SIZE;
401 		goto out2;
402 	}
403 
404 	/* calculate checksum */
405 	for (i = 0; i < hdr_ptr->filelen; i++)
406 		csum += asd_ha->bios_image->data[i];
407 
408 	if ((csum & 0x0000ffff) != hdr_ptr->checksum) {
409 		ASD_DPRINTK("BIOS file checksum mismatch\n");
410 		err = FAIL_CHECK_SUM;
411 		goto out2;
412 	}
413 	if (flash_command == FLASH_CMD_UPDATE) {
414 		asd_ha->bios_status = FLASH_IN_PROGRESS;
415 		err = asd_write_flash_seg(asd_ha,
416 			&asd_ha->bios_image->data[sizeof(*hdr_ptr)],
417 			0, hdr_ptr->filelen-sizeof(*hdr_ptr));
418 		if (!err)
419 			err = asd_verify_flash_seg(asd_ha,
420 				&asd_ha->bios_image->data[sizeof(*hdr_ptr)],
421 				0, hdr_ptr->filelen-sizeof(*hdr_ptr));
422 	} else {
423 		asd_ha->bios_status = FLASH_IN_PROGRESS;
424 		err = asd_verify_flash_seg(asd_ha,
425 			&asd_ha->bios_image->data[sizeof(header)],
426 			0, hdr_ptr->filelen-sizeof(header));
427 	}
428 
429 out2:
430 	release_firmware(asd_ha->bios_image);
431 out1:
432 	kfree(cmd_ptr);
433 out:
434 	asd_ha->bios_status = err;
435 
436 	if (!err)
437 		return count;
438 	else
439 		return -err;
440 }
441 
442 static ssize_t asd_show_update_bios(struct device *dev,
443 				    struct device_attribute *attr, char *buf)
444 {
445 	int i;
446 	struct asd_ha_struct *asd_ha = dev_to_asd_ha(dev);
447 
448 	for (i = 0; flash_error_table[i].err_code != 0; i++) {
449 		if (flash_error_table[i].err_code == asd_ha->bios_status)
450 			break;
451 	}
452 	if (asd_ha->bios_status != FLASH_IN_PROGRESS)
453 		asd_ha->bios_status = FLASH_OK;
454 
455 	return snprintf(buf, PAGE_SIZE, "status=%x %s\n",
456 			flash_error_table[i].err_code,
457 			flash_error_table[i].reason);
458 }
459 
460 static DEVICE_ATTR(update_bios, S_IRUGO|S_IWUSR,
461 	asd_show_update_bios, asd_store_update_bios);
462 
463 static int asd_create_dev_attrs(struct asd_ha_struct *asd_ha)
464 {
465 	int err;
466 
467 	err = device_create_file(&asd_ha->pcidev->dev, &dev_attr_aic_revision);
468 	if (err)
469 		return err;
470 
471 	err = device_create_file(&asd_ha->pcidev->dev, &dev_attr_bios_build);
472 	if (err)
473 		goto err_rev;
474 
475 	err = device_create_file(&asd_ha->pcidev->dev, &dev_attr_pcba_sn);
476 	if (err)
477 		goto err_biosb;
478 	err = device_create_file(&asd_ha->pcidev->dev, &dev_attr_update_bios);
479 	if (err)
480 		goto err_update_bios;
481 
482 	return 0;
483 
484 err_update_bios:
485 	device_remove_file(&asd_ha->pcidev->dev, &dev_attr_pcba_sn);
486 err_biosb:
487 	device_remove_file(&asd_ha->pcidev->dev, &dev_attr_bios_build);
488 err_rev:
489 	device_remove_file(&asd_ha->pcidev->dev, &dev_attr_aic_revision);
490 	return err;
491 }
492 
493 static void asd_remove_dev_attrs(struct asd_ha_struct *asd_ha)
494 {
495 	device_remove_file(&asd_ha->pcidev->dev, &dev_attr_aic_revision);
496 	device_remove_file(&asd_ha->pcidev->dev, &dev_attr_bios_build);
497 	device_remove_file(&asd_ha->pcidev->dev, &dev_attr_pcba_sn);
498 	device_remove_file(&asd_ha->pcidev->dev, &dev_attr_update_bios);
499 }
500 
501 /* The first entry, 0, is used for dynamic ids, the rest for devices
502  * we know about.
503  */
504 static const struct asd_pcidev_struct {
505 	const char * name;
506 	int (*setup)(struct asd_ha_struct *asd_ha);
507 } asd_pcidev_data[] = {
508 	/* Id 0 is used for dynamic ids. */
509 	{ .name  = "Adaptec AIC-94xx SAS/SATA Host Adapter",
510 	  .setup = asd_aic9410_setup
511 	},
512 	{ .name  = "Adaptec AIC-9410W SAS/SATA Host Adapter",
513 	  .setup = asd_aic9410_setup
514 	},
515 	{ .name  = "Adaptec AIC-9405W SAS/SATA Host Adapter",
516 	  .setup = asd_aic9405_setup
517 	},
518 };
519 
520 static int asd_create_ha_caches(struct asd_ha_struct *asd_ha)
521 {
522 	asd_ha->scb_pool = dma_pool_create(ASD_DRIVER_NAME "_scb_pool",
523 					   &asd_ha->pcidev->dev,
524 					   sizeof(struct scb),
525 					   8, 0);
526 	if (!asd_ha->scb_pool) {
527 		asd_printk("couldn't create scb pool\n");
528 		return -ENOMEM;
529 	}
530 
531 	return 0;
532 }
533 
534 /*
535  * asd_free_edbs -- free empty data buffers
536  * asd_ha: pointer to host adapter structure
537  */
538 static void asd_free_edbs(struct asd_ha_struct *asd_ha)
539 {
540 	struct asd_seq_data *seq = &asd_ha->seq;
541 	int i;
542 
543 	for (i = 0; i < seq->num_edbs; i++)
544 		asd_free_coherent(asd_ha, seq->edb_arr[i]);
545 	kfree(seq->edb_arr);
546 	seq->edb_arr = NULL;
547 }
548 
549 static void asd_free_escbs(struct asd_ha_struct *asd_ha)
550 {
551 	struct asd_seq_data *seq = &asd_ha->seq;
552 	int i;
553 
554 	for (i = 0; i < seq->num_escbs; i++) {
555 		if (!list_empty(&seq->escb_arr[i]->list))
556 			list_del_init(&seq->escb_arr[i]->list);
557 
558 		asd_ascb_free(seq->escb_arr[i]);
559 	}
560 	kfree(seq->escb_arr);
561 	seq->escb_arr = NULL;
562 }
563 
564 static void asd_destroy_ha_caches(struct asd_ha_struct *asd_ha)
565 {
566 	int i;
567 
568 	if (asd_ha->hw_prof.ddb_ext)
569 		asd_free_coherent(asd_ha, asd_ha->hw_prof.ddb_ext);
570 	if (asd_ha->hw_prof.scb_ext)
571 		asd_free_coherent(asd_ha, asd_ha->hw_prof.scb_ext);
572 
573 	kfree(asd_ha->hw_prof.ddb_bitmap);
574 	asd_ha->hw_prof.ddb_bitmap = NULL;
575 
576 	for (i = 0; i < ASD_MAX_PHYS; i++) {
577 		struct asd_phy *phy = &asd_ha->phys[i];
578 
579 		asd_free_coherent(asd_ha, phy->id_frm_tok);
580 	}
581 	if (asd_ha->seq.escb_arr)
582 		asd_free_escbs(asd_ha);
583 	if (asd_ha->seq.edb_arr)
584 		asd_free_edbs(asd_ha);
585 	if (asd_ha->hw_prof.ue.area) {
586 		kfree(asd_ha->hw_prof.ue.area);
587 		asd_ha->hw_prof.ue.area = NULL;
588 	}
589 	if (asd_ha->seq.tc_index_array) {
590 		kfree(asd_ha->seq.tc_index_array);
591 		kfree(asd_ha->seq.tc_index_bitmap);
592 		asd_ha->seq.tc_index_array = NULL;
593 		asd_ha->seq.tc_index_bitmap = NULL;
594 	}
595 	if (asd_ha->seq.actual_dl) {
596 			asd_free_coherent(asd_ha, asd_ha->seq.actual_dl);
597 			asd_ha->seq.actual_dl = NULL;
598 			asd_ha->seq.dl = NULL;
599 	}
600 	if (asd_ha->seq.next_scb.vaddr) {
601 		dma_pool_free(asd_ha->scb_pool, asd_ha->seq.next_scb.vaddr,
602 			      asd_ha->seq.next_scb.dma_handle);
603 		asd_ha->seq.next_scb.vaddr = NULL;
604 	}
605 	dma_pool_destroy(asd_ha->scb_pool);
606 	asd_ha->scb_pool = NULL;
607 }
608 
609 struct kmem_cache *asd_dma_token_cache;
610 struct kmem_cache *asd_ascb_cache;
611 
612 static int asd_create_global_caches(void)
613 {
614 	if (!asd_dma_token_cache) {
615 		asd_dma_token_cache
616 			= kmem_cache_create(ASD_DRIVER_NAME "_dma_token",
617 					    sizeof(struct asd_dma_tok),
618 					    0,
619 					    SLAB_HWCACHE_ALIGN,
620 					    NULL);
621 		if (!asd_dma_token_cache) {
622 			asd_printk("couldn't create dma token cache\n");
623 			return -ENOMEM;
624 		}
625 	}
626 
627 	if (!asd_ascb_cache) {
628 		asd_ascb_cache = kmem_cache_create(ASD_DRIVER_NAME "_ascb",
629 						   sizeof(struct asd_ascb),
630 						   0,
631 						   SLAB_HWCACHE_ALIGN,
632 						   NULL);
633 		if (!asd_ascb_cache) {
634 			asd_printk("couldn't create ascb cache\n");
635 			goto Err;
636 		}
637 	}
638 
639 	return 0;
640 Err:
641 	kmem_cache_destroy(asd_dma_token_cache);
642 	asd_dma_token_cache = NULL;
643 	return -ENOMEM;
644 }
645 
646 static void asd_destroy_global_caches(void)
647 {
648 	kmem_cache_destroy(asd_dma_token_cache);
649 	asd_dma_token_cache = NULL;
650 
651 	kmem_cache_destroy(asd_ascb_cache);
652 	asd_ascb_cache = NULL;
653 }
654 
655 static int asd_register_sas_ha(struct asd_ha_struct *asd_ha)
656 {
657 	int i;
658 	struct asd_sas_phy   **sas_phys =
659 		kcalloc(ASD_MAX_PHYS, sizeof(*sas_phys), GFP_KERNEL);
660 	struct asd_sas_port  **sas_ports =
661 		kcalloc(ASD_MAX_PHYS, sizeof(*sas_ports), GFP_KERNEL);
662 
663 	if (!sas_phys || !sas_ports) {
664 		kfree(sas_phys);
665 		kfree(sas_ports);
666 		return -ENOMEM;
667 	}
668 
669 	asd_ha->sas_ha.sas_ha_name = (char *) asd_ha->name;
670 	asd_ha->sas_ha.lldd_module = THIS_MODULE;
671 	asd_ha->sas_ha.sas_addr = &asd_ha->hw_prof.sas_addr[0];
672 
673 	for (i = 0; i < ASD_MAX_PHYS; i++) {
674 		sas_phys[i] = &asd_ha->phys[i].sas_phy;
675 		sas_ports[i] = &asd_ha->ports[i];
676 	}
677 
678 	asd_ha->sas_ha.sas_phy = sas_phys;
679 	asd_ha->sas_ha.sas_port= sas_ports;
680 	asd_ha->sas_ha.num_phys= ASD_MAX_PHYS;
681 
682 	return sas_register_ha(&asd_ha->sas_ha);
683 }
684 
685 static int asd_unregister_sas_ha(struct asd_ha_struct *asd_ha)
686 {
687 	int err;
688 
689 	err = sas_unregister_ha(&asd_ha->sas_ha);
690 
691 	sas_remove_host(asd_ha->sas_ha.core.shost);
692 	scsi_host_put(asd_ha->sas_ha.core.shost);
693 
694 	kfree(asd_ha->sas_ha.sas_phy);
695 	kfree(asd_ha->sas_ha.sas_port);
696 
697 	return err;
698 }
699 
700 static int asd_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
701 {
702 	const struct asd_pcidev_struct *asd_dev;
703 	unsigned asd_id = (unsigned) id->driver_data;
704 	struct asd_ha_struct *asd_ha;
705 	struct Scsi_Host *shost;
706 	int err;
707 
708 	if (asd_id >= ARRAY_SIZE(asd_pcidev_data)) {
709 		asd_printk("wrong driver_data in PCI table\n");
710 		return -ENODEV;
711 	}
712 
713 	if ((err = pci_enable_device(dev))) {
714 		asd_printk("couldn't enable device %s\n", pci_name(dev));
715 		return err;
716 	}
717 
718 	pci_set_master(dev);
719 
720 	err = -ENOMEM;
721 
722 	shost = scsi_host_alloc(&aic94xx_sht, sizeof(void *));
723 	if (!shost)
724 		goto Err;
725 
726 	asd_dev = &asd_pcidev_data[asd_id];
727 
728 	asd_ha = kzalloc(sizeof(*asd_ha), GFP_KERNEL);
729 	if (!asd_ha) {
730 		asd_printk("out of memory\n");
731 		goto Err_put;
732 	}
733 	asd_ha->pcidev = dev;
734 	asd_ha->sas_ha.dev = &asd_ha->pcidev->dev;
735 	asd_ha->sas_ha.lldd_ha = asd_ha;
736 
737 	asd_ha->bios_status = FLASH_OK;
738 	asd_ha->name = asd_dev->name;
739 	asd_printk("found %s, device %s\n", asd_ha->name, pci_name(dev));
740 
741 	SHOST_TO_SAS_HA(shost) = &asd_ha->sas_ha;
742 	asd_ha->sas_ha.core.shost = shost;
743 	shost->transportt = aic94xx_transport_template;
744 	shost->max_id = ~0;
745 	shost->max_lun = ~0;
746 	shost->max_cmd_len = 16;
747 
748 	err = scsi_add_host(shost, &dev->dev);
749 	if (err)
750 		goto Err_free;
751 
752 	err = asd_dev->setup(asd_ha);
753 	if (err)
754 		goto Err_remove;
755 
756 	err = dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(64));
757 	if (err)
758 		err = dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
759 	if (err) {
760 		err = -ENODEV;
761 		asd_printk("no suitable DMA mask for %s\n", pci_name(dev));
762 		goto Err_remove;
763 	}
764 
765 	pci_set_drvdata(dev, asd_ha);
766 
767 	err = asd_map_ha(asd_ha);
768 	if (err)
769 		goto Err_remove;
770 
771 	err = asd_create_ha_caches(asd_ha);
772         if (err)
773 		goto Err_unmap;
774 
775 	err = asd_init_hw(asd_ha);
776 	if (err)
777 		goto Err_free_cache;
778 
779 	asd_printk("device %s: SAS addr %llx, PCBA SN %s, %d phys, %d enabled "
780 		   "phys, flash %s, BIOS %s%d\n",
781 		   pci_name(dev), SAS_ADDR(asd_ha->hw_prof.sas_addr),
782 		   asd_ha->hw_prof.pcba_sn, asd_ha->hw_prof.max_phys,
783 		   asd_ha->hw_prof.num_phys,
784 		   asd_ha->hw_prof.flash.present ? "present" : "not present",
785 		   asd_ha->hw_prof.bios.present ? "build " : "not present",
786 		   asd_ha->hw_prof.bios.bld);
787 
788 	shost->can_queue = asd_ha->seq.can_queue;
789 
790 	if (use_msi)
791 		pci_enable_msi(asd_ha->pcidev);
792 
793 	err = request_irq(asd_ha->pcidev->irq, asd_hw_isr, IRQF_SHARED,
794 			  ASD_DRIVER_NAME, asd_ha);
795 	if (err) {
796 		asd_printk("couldn't get irq %d for %s\n",
797 			   asd_ha->pcidev->irq, pci_name(asd_ha->pcidev));
798 		goto Err_irq;
799 	}
800 	asd_enable_ints(asd_ha);
801 
802 	err = asd_init_post_escbs(asd_ha);
803 	if (err) {
804 		asd_printk("couldn't post escbs for %s\n",
805 			   pci_name(asd_ha->pcidev));
806 		goto Err_escbs;
807 	}
808 	ASD_DPRINTK("escbs posted\n");
809 
810 	err = asd_create_dev_attrs(asd_ha);
811 	if (err)
812 		goto Err_dev_attrs;
813 
814 	err = asd_register_sas_ha(asd_ha);
815 	if (err)
816 		goto Err_reg_sas;
817 
818 	scsi_scan_host(shost);
819 
820 	return 0;
821 
822 Err_reg_sas:
823 	asd_remove_dev_attrs(asd_ha);
824 Err_dev_attrs:
825 Err_escbs:
826 	asd_disable_ints(asd_ha);
827 	free_irq(dev->irq, asd_ha);
828 Err_irq:
829 	if (use_msi)
830 		pci_disable_msi(dev);
831 	asd_chip_hardrst(asd_ha);
832 Err_free_cache:
833 	asd_destroy_ha_caches(asd_ha);
834 Err_unmap:
835 	asd_unmap_ha(asd_ha);
836 Err_remove:
837 	scsi_remove_host(shost);
838 Err_free:
839 	kfree(asd_ha);
840 Err_put:
841 	scsi_host_put(shost);
842 Err:
843 	pci_disable_device(dev);
844 	return err;
845 }
846 
847 static void asd_free_queues(struct asd_ha_struct *asd_ha)
848 {
849 	unsigned long flags;
850 	LIST_HEAD(pending);
851 	struct list_head *n, *pos;
852 
853 	spin_lock_irqsave(&asd_ha->seq.pend_q_lock, flags);
854 	asd_ha->seq.pending = 0;
855 	list_splice_init(&asd_ha->seq.pend_q, &pending);
856 	spin_unlock_irqrestore(&asd_ha->seq.pend_q_lock, flags);
857 
858 	if (!list_empty(&pending))
859 		ASD_DPRINTK("Uh-oh! Pending is not empty!\n");
860 
861 	list_for_each_safe(pos, n, &pending) {
862 		struct asd_ascb *ascb = list_entry(pos, struct asd_ascb, list);
863 		/*
864 		 * Delete unexpired ascb timers.  This may happen if we issue
865 		 * a CONTROL PHY scb to an adapter and rmmod before the scb
866 		 * times out.  Apparently we don't wait for the CONTROL PHY
867 		 * to complete, so it doesn't matter if we kill the timer.
868 		 */
869 		del_timer_sync(&ascb->timer);
870 		WARN_ON(ascb->scb->header.opcode != CONTROL_PHY);
871 
872 		list_del_init(pos);
873 		ASD_DPRINTK("freeing from pending\n");
874 		asd_ascb_free(ascb);
875 	}
876 }
877 
878 static void asd_turn_off_leds(struct asd_ha_struct *asd_ha)
879 {
880 	u8 phy_mask = asd_ha->hw_prof.enabled_phys;
881 	u8 i;
882 
883 	for_each_phy(phy_mask, phy_mask, i) {
884 		asd_turn_led(asd_ha, i, 0);
885 		asd_control_led(asd_ha, i, 0);
886 	}
887 }
888 
889 static void asd_pci_remove(struct pci_dev *dev)
890 {
891 	struct asd_ha_struct *asd_ha = pci_get_drvdata(dev);
892 
893 	if (!asd_ha)
894 		return;
895 
896 	asd_unregister_sas_ha(asd_ha);
897 
898 	asd_disable_ints(asd_ha);
899 
900 	asd_remove_dev_attrs(asd_ha);
901 
902 	/* XXX more here as needed */
903 
904 	free_irq(dev->irq, asd_ha);
905 	if (use_msi)
906 		pci_disable_msi(asd_ha->pcidev);
907 	asd_turn_off_leds(asd_ha);
908 	asd_chip_hardrst(asd_ha);
909 	asd_free_queues(asd_ha);
910 	asd_destroy_ha_caches(asd_ha);
911 	asd_unmap_ha(asd_ha);
912 	kfree(asd_ha);
913 	pci_disable_device(dev);
914 	return;
915 }
916 
917 static void asd_scan_start(struct Scsi_Host *shost)
918 {
919 	struct asd_ha_struct *asd_ha;
920 	int err;
921 
922 	asd_ha = SHOST_TO_SAS_HA(shost)->lldd_ha;
923 	err = asd_enable_phys(asd_ha, asd_ha->hw_prof.enabled_phys);
924 	if (err)
925 		asd_printk("Couldn't enable phys, err:%d\n", err);
926 }
927 
928 static int asd_scan_finished(struct Scsi_Host *shost, unsigned long time)
929 {
930 	/* give the phy enabling interrupt event time to come in (1s
931 	 * is empirically about all it takes) */
932 	if (time < HZ)
933 		return 0;
934 	/* Wait for discovery to finish */
935 	sas_drain_work(SHOST_TO_SAS_HA(shost));
936 	return 1;
937 }
938 
939 static ssize_t version_show(struct device_driver *driver, char *buf)
940 {
941 	return snprintf(buf, PAGE_SIZE, "%s\n", ASD_DRIVER_VERSION);
942 }
943 static DRIVER_ATTR_RO(version);
944 
945 static int asd_create_driver_attrs(struct device_driver *driver)
946 {
947 	return driver_create_file(driver, &driver_attr_version);
948 }
949 
950 static void asd_remove_driver_attrs(struct device_driver *driver)
951 {
952 	driver_remove_file(driver, &driver_attr_version);
953 }
954 
955 static struct sas_domain_function_template aic94xx_transport_functions = {
956 	.lldd_dev_found		= asd_dev_found,
957 	.lldd_dev_gone		= asd_dev_gone,
958 
959 	.lldd_execute_task	= asd_execute_task,
960 
961 	.lldd_abort_task	= asd_abort_task,
962 	.lldd_abort_task_set	= asd_abort_task_set,
963 	.lldd_clear_aca		= asd_clear_aca,
964 	.lldd_clear_task_set	= asd_clear_task_set,
965 	.lldd_I_T_nexus_reset	= asd_I_T_nexus_reset,
966 	.lldd_lu_reset		= asd_lu_reset,
967 	.lldd_query_task	= asd_query_task,
968 
969 	.lldd_clear_nexus_port	= asd_clear_nexus_port,
970 	.lldd_clear_nexus_ha	= asd_clear_nexus_ha,
971 
972 	.lldd_control_phy	= asd_control_phy,
973 
974 	.lldd_ata_set_dmamode	= asd_set_dmamode,
975 };
976 
977 static const struct pci_device_id aic94xx_pci_table[] = {
978 	{PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x410),0, 0, 1},
979 	{PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x412),0, 0, 1},
980 	{PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x416),0, 0, 1},
981 	{PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x41E),0, 0, 1},
982 	{PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x41F),0, 0, 1},
983 	{PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x430),0, 0, 2},
984 	{PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x432),0, 0, 2},
985 	{PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x43E),0, 0, 2},
986 	{PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x43F),0, 0, 2},
987 	{}
988 };
989 
990 MODULE_DEVICE_TABLE(pci, aic94xx_pci_table);
991 
992 static struct pci_driver aic94xx_pci_driver = {
993 	.name		= ASD_DRIVER_NAME,
994 	.id_table	= aic94xx_pci_table,
995 	.probe		= asd_pci_probe,
996 	.remove		= asd_pci_remove,
997 };
998 
999 static int __init aic94xx_init(void)
1000 {
1001 	int err;
1002 
1003 
1004 	asd_printk("%s version %s loaded\n", ASD_DRIVER_DESCRIPTION,
1005 		   ASD_DRIVER_VERSION);
1006 
1007 	err = asd_create_global_caches();
1008 	if (err)
1009 		return err;
1010 
1011 	aic94xx_transport_template =
1012 		sas_domain_attach_transport(&aic94xx_transport_functions);
1013 	if (!aic94xx_transport_template) {
1014 		err = -ENOMEM;
1015 		goto out_destroy_caches;
1016 	}
1017 
1018 	err = pci_register_driver(&aic94xx_pci_driver);
1019 	if (err)
1020 		goto out_release_transport;
1021 
1022 	err = asd_create_driver_attrs(&aic94xx_pci_driver.driver);
1023 	if (err)
1024 		goto out_unregister_pcidrv;
1025 
1026 	return err;
1027 
1028  out_unregister_pcidrv:
1029 	pci_unregister_driver(&aic94xx_pci_driver);
1030  out_release_transport:
1031 	sas_release_transport(aic94xx_transport_template);
1032  out_destroy_caches:
1033 	asd_destroy_global_caches();
1034 
1035 	return err;
1036 }
1037 
1038 static void __exit aic94xx_exit(void)
1039 {
1040 	asd_remove_driver_attrs(&aic94xx_pci_driver.driver);
1041 	pci_unregister_driver(&aic94xx_pci_driver);
1042 	sas_release_transport(aic94xx_transport_template);
1043 	asd_release_firmware();
1044 	asd_destroy_global_caches();
1045 	asd_printk("%s version %s unloaded\n", ASD_DRIVER_DESCRIPTION,
1046 		   ASD_DRIVER_VERSION);
1047 }
1048 
1049 module_init(aic94xx_init);
1050 module_exit(aic94xx_exit);
1051 
1052 MODULE_AUTHOR("Luben Tuikov <luben_tuikov@adaptec.com>");
1053 MODULE_DESCRIPTION(ASD_DRIVER_DESCRIPTION);
1054 MODULE_LICENSE("GPL v2");
1055 MODULE_VERSION(ASD_DRIVER_VERSION);
1056