aic94xx_init.c 28 KB

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