aic94xx_init.c 28 KB

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