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