aic94xx_init.c 26 KB

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