aic94xx_init.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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. #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_aca = asd_clear_aca,
  816. .lldd_clear_task_set = asd_clear_task_set,
  817. .lldd_I_T_nexus_reset = asd_I_T_nexus_reset,
  818. .lldd_lu_reset = asd_lu_reset,
  819. .lldd_query_task = asd_query_task,
  820. .lldd_clear_nexus_port = asd_clear_nexus_port,
  821. .lldd_clear_nexus_ha = asd_clear_nexus_ha,
  822. .lldd_control_phy = asd_control_phy,
  823. .lldd_ata_set_dmamode = asd_set_dmamode,
  824. };
  825. static const struct pci_device_id aic94xx_pci_table[] = {
  826. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x410),0, 0, 1},
  827. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x412),0, 0, 1},
  828. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x416),0, 0, 1},
  829. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x41E),0, 0, 1},
  830. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x41F),0, 0, 1},
  831. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x430),0, 0, 2},
  832. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x432),0, 0, 2},
  833. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x43E),0, 0, 2},
  834. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x43F),0, 0, 2},
  835. {}
  836. };
  837. MODULE_DEVICE_TABLE(pci, aic94xx_pci_table);
  838. static struct pci_driver aic94xx_pci_driver = {
  839. .name = ASD_DRIVER_NAME,
  840. .id_table = aic94xx_pci_table,
  841. .probe = asd_pci_probe,
  842. .remove = asd_pci_remove,
  843. };
  844. static int __init aic94xx_init(void)
  845. {
  846. int err;
  847. asd_printk("%s version %s loaded\n", ASD_DRIVER_DESCRIPTION,
  848. ASD_DRIVER_VERSION);
  849. err = asd_create_global_caches();
  850. if (err)
  851. return err;
  852. aic94xx_transport_template =
  853. sas_domain_attach_transport(&aic94xx_transport_functions);
  854. if (!aic94xx_transport_template) {
  855. err = -ENOMEM;
  856. goto out_destroy_caches;
  857. }
  858. err = pci_register_driver(&aic94xx_pci_driver);
  859. if (err)
  860. goto out_release_transport;
  861. err = asd_create_driver_attrs(&aic94xx_pci_driver.driver);
  862. if (err)
  863. goto out_unregister_pcidrv;
  864. return err;
  865. out_unregister_pcidrv:
  866. pci_unregister_driver(&aic94xx_pci_driver);
  867. out_release_transport:
  868. sas_release_transport(aic94xx_transport_template);
  869. out_destroy_caches:
  870. asd_destroy_global_caches();
  871. return err;
  872. }
  873. static void __exit aic94xx_exit(void)
  874. {
  875. asd_remove_driver_attrs(&aic94xx_pci_driver.driver);
  876. pci_unregister_driver(&aic94xx_pci_driver);
  877. sas_release_transport(aic94xx_transport_template);
  878. asd_release_firmware();
  879. asd_destroy_global_caches();
  880. asd_printk("%s version %s unloaded\n", ASD_DRIVER_DESCRIPTION,
  881. ASD_DRIVER_VERSION);
  882. }
  883. module_init(aic94xx_init);
  884. module_exit(aic94xx_exit);
  885. MODULE_AUTHOR("Luben Tuikov <luben_tuikov@adaptec.com>");
  886. MODULE_DESCRIPTION(ASD_DRIVER_DESCRIPTION);
  887. MODULE_LICENSE("GPL v2");
  888. MODULE_VERSION(ASD_DRIVER_VERSION);