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