File Index
Files and directories
Sign in
FILE

aic94xx_init.c

File preview with syntax highlighting.

linux-6.19.14/drivers/scsi/aic94xx/aic94xx_init.c C
   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
  30static int use_msi = 0;
  31module_param_named(use_msi, use_msi, int, S_IRUGO);
  32MODULE_PARM_DESC(use_msi, "\n"
  33	"\tEnable(1) or disable(0) using PCI MSI.\n"
  34	"\tDefault: 0");
  35
  36static struct scsi_transport_template *aic94xx_transport_template;
  37static int asd_scan_finished(struct Scsi_Host *, unsigned long);
  38static void asd_scan_start(struct Scsi_Host *);
  39static const struct attribute_group *asd_sdev_groups[];
  40
  41static 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
  51static 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;
  84Err_unreq:
  85	pci_release_region(asd_ha->pcidev, i);
  86Err:
  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
  95static 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
 108static 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
 132static void asd_unmap_ioport(struct asd_ha_struct *asd_ha)
 133{
 134	pci_release_region(asd_ha->pcidev, PCI_IOBAR_OFFSET);
 135}
 136
 137static 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;
 165Err:
 166	return err;
 167}
 168
 169static 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
 177static const char *asd_dev_rev[30] = {
 178	[0] = "A0",
 179	[1] = "A1",
 180	[8] = "B0",
 181};
 182
 183static 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;
 215Err:
 216	return err;
 217}
 218
 219static 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
 234static 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
 249static 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}
 256static DEVICE_ATTR(aic_revision, S_IRUGO, asd_show_dev_rev, NULL);
 257
 258static 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}
 264static DEVICE_ATTR(bios_build, S_IRUGO, asd_show_dev_bios_build, NULL);
 265
 266static 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}
 272static 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
 278struct flash_command {
 279     u8      command[8];
 280     int     code;
 281};
 282
 283static 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
 290struct error_bios {
 291     char    *reason;
 292     int     err_code;
 293};
 294
 295static 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
 313static 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
 415out2:
 416	release_firmware(asd_ha->bios_image);
 417out1:
 418	kfree(cmd_ptr);
 419out:
 420	asd_ha->bios_status = err;
 421
 422	if (!err)
 423		return count;
 424	else
 425		return -err;
 426}
 427
 428static 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
 446static DEVICE_ATTR(update_bios, S_IRUGO|S_IWUSR,
 447	asd_show_update_bios, asd_store_update_bios);
 448
 449static 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
 470err_update_bios:
 471	device_remove_file(&asd_ha->pcidev->dev, &dev_attr_pcba_sn);
 472err_biosb:
 473	device_remove_file(&asd_ha->pcidev->dev, &dev_attr_bios_build);
 474err_rev:
 475	device_remove_file(&asd_ha->pcidev->dev, &dev_attr_aic_revision);
 476	return err;
 477}
 478
 479static 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 */
 490static 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
 506static 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 */
 524static 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
 535static 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
 550static 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
 595struct kmem_cache *asd_dma_token_cache;
 596struct kmem_cache *asd_ascb_cache;
 597
 598static 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;
 626Err:
 627	kmem_cache_destroy(asd_dma_token_cache);
 628	asd_dma_token_cache = NULL;
 629	return -ENOMEM;
 630}
 631
 632static 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
 641static 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
 670static 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
 685static 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
 807Err_reg_sas:
 808	asd_remove_dev_attrs(asd_ha);
 809Err_dev_attrs:
 810Err_escbs:
 811	asd_disable_ints(asd_ha);
 812	free_irq(dev->irq, asd_ha);
 813Err_irq:
 814	if (use_msi)
 815		pci_disable_msi(dev);
 816	asd_chip_hardrst(asd_ha);
 817Err_free_cache:
 818	asd_destroy_ha_caches(asd_ha);
 819Err_unmap:
 820	asd_unmap_ha(asd_ha);
 821Err_remove:
 822	scsi_remove_host(shost);
 823Err_free:
 824	kfree(asd_ha);
 825Err_put:
 826	scsi_host_put(shost);
 827Err:
 828	pci_disable_device(dev);
 829	return err;
 830}
 831
 832static 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		timer_delete_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
 863static 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
 874static 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	/* Ensure all scheduled tasklets complete before freeing resources */
 886	tasklet_kill(&asd_ha->seq.dl_tasklet);
 887
 888	asd_remove_dev_attrs(asd_ha);
 889
 890	/* XXX more here as needed */
 891
 892	free_irq(dev->irq, asd_ha);
 893	if (use_msi)
 894		pci_disable_msi(asd_ha->pcidev);
 895	asd_turn_off_leds(asd_ha);
 896	asd_chip_hardrst(asd_ha);
 897	asd_free_queues(asd_ha);
 898	asd_destroy_ha_caches(asd_ha);
 899	asd_unmap_ha(asd_ha);
 900	kfree(asd_ha);
 901	pci_disable_device(dev);
 902	return;
 903}
 904
 905static void asd_scan_start(struct Scsi_Host *shost)
 906{
 907	struct asd_ha_struct *asd_ha;
 908	int err;
 909
 910	asd_ha = SHOST_TO_SAS_HA(shost)->lldd_ha;
 911	err = asd_enable_phys(asd_ha, asd_ha->hw_prof.enabled_phys);
 912	if (err)
 913		asd_printk("Couldn't enable phys, err:%d\n", err);
 914}
 915
 916static int asd_scan_finished(struct Scsi_Host *shost, unsigned long time)
 917{
 918	/* give the phy enabling interrupt event time to come in (1s
 919	 * is empirically about all it takes) */
 920	if (time < HZ)
 921		return 0;
 922	/* Wait for discovery to finish */
 923	sas_drain_work(SHOST_TO_SAS_HA(shost));
 924	return 1;
 925}
 926
 927static ssize_t version_show(struct device_driver *driver, char *buf)
 928{
 929	return snprintf(buf, PAGE_SIZE, "%s\n", ASD_DRIVER_VERSION);
 930}
 931static DRIVER_ATTR_RO(version);
 932
 933static int asd_create_driver_attrs(struct device_driver *driver)
 934{
 935	return driver_create_file(driver, &driver_attr_version);
 936}
 937
 938static void asd_remove_driver_attrs(struct device_driver *driver)
 939{
 940	driver_remove_file(driver, &driver_attr_version);
 941}
 942
 943static const struct attribute_group *asd_sdev_groups[] = {
 944	&sas_ata_sdev_attr_group,
 945	NULL
 946};
 947
 948static struct sas_domain_function_template aic94xx_transport_functions = {
 949	.lldd_dev_found		= asd_dev_found,
 950	.lldd_dev_gone		= asd_dev_gone,
 951
 952	.lldd_execute_task	= asd_execute_task,
 953
 954	.lldd_abort_task	= asd_abort_task,
 955	.lldd_abort_task_set	= asd_abort_task_set,
 956	.lldd_clear_task_set	= asd_clear_task_set,
 957	.lldd_I_T_nexus_reset	= asd_I_T_nexus_reset,
 958	.lldd_lu_reset		= asd_lu_reset,
 959	.lldd_query_task	= asd_query_task,
 960
 961	.lldd_clear_nexus_port	= asd_clear_nexus_port,
 962	.lldd_clear_nexus_ha	= asd_clear_nexus_ha,
 963
 964	.lldd_control_phy	= asd_control_phy,
 965
 966	.lldd_ata_set_dmamode	= asd_set_dmamode,
 967};
 968
 969static const struct pci_device_id aic94xx_pci_table[] = {
 970	{PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x410),0, 0, 1},
 971	{PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x412),0, 0, 1},
 972	{PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x416),0, 0, 1},
 973	{PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x41E),0, 0, 1},
 974	{PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x41F),0, 0, 1},
 975	{PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x430),0, 0, 2},
 976	{PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x432),0, 0, 2},
 977	{PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x43E),0, 0, 2},
 978	{PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x43F),0, 0, 2},
 979	{}
 980};
 981
 982MODULE_DEVICE_TABLE(pci, aic94xx_pci_table);
 983
 984static struct pci_driver aic94xx_pci_driver = {
 985	.name		= ASD_DRIVER_NAME,
 986	.id_table	= aic94xx_pci_table,
 987	.probe		= asd_pci_probe,
 988	.remove		= asd_pci_remove,
 989};
 990
 991static int __init aic94xx_init(void)
 992{
 993	int err;
 994
 995
 996	asd_printk("%s version %s loaded\n", ASD_DRIVER_DESCRIPTION,
 997		   ASD_DRIVER_VERSION);
 998
 999	err = asd_create_global_caches();
1000	if (err)
1001		return err;
1002
1003	aic94xx_transport_template =
1004		sas_domain_attach_transport(&aic94xx_transport_functions);
1005	if (!aic94xx_transport_template) {
1006		err = -ENOMEM;
1007		goto out_destroy_caches;
1008	}
1009
1010	err = pci_register_driver(&aic94xx_pci_driver);
1011	if (err)
1012		goto out_release_transport;
1013
1014	err = asd_create_driver_attrs(&aic94xx_pci_driver.driver);
1015	if (err)
1016		goto out_unregister_pcidrv;
1017
1018	return err;
1019
1020 out_unregister_pcidrv:
1021	pci_unregister_driver(&aic94xx_pci_driver);
1022 out_release_transport:
1023	sas_release_transport(aic94xx_transport_template);
1024 out_destroy_caches:
1025	asd_destroy_global_caches();
1026
1027	return err;
1028}
1029
1030static void __exit aic94xx_exit(void)
1031{
1032	asd_remove_driver_attrs(&aic94xx_pci_driver.driver);
1033	pci_unregister_driver(&aic94xx_pci_driver);
1034	sas_release_transport(aic94xx_transport_template);
1035	asd_release_firmware();
1036	asd_destroy_global_caches();
1037	asd_printk("%s version %s unloaded\n", ASD_DRIVER_DESCRIPTION,
1038		   ASD_DRIVER_VERSION);
1039}
1040
1041module_init(aic94xx_init);
1042module_exit(aic94xx_exit);
1043
1044MODULE_AUTHOR("Luben Tuikov <luben_tuikov@adaptec.com>");
1045MODULE_DESCRIPTION(ASD_DRIVER_DESCRIPTION);
1046MODULE_LICENSE("GPL v2");
1047MODULE_VERSION(ASD_DRIVER_VERSION);
~/ Read-only file preview.