aic94xx_init.c 27 KB

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