aic94xx_init.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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 __devinit 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 __devinit 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 __devinit 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 __devinit 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 __devinit 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 __devinit 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[] __devinitconst = {
  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 __devinit asd_pci_probe(struct pci_dev *dev,
  634. const struct pci_device_id *id)
  635. {
  636. const struct asd_pcidev_struct *asd_dev;
  637. unsigned asd_id = (unsigned) id->driver_data;
  638. struct asd_ha_struct *asd_ha;
  639. struct Scsi_Host *shost;
  640. int err;
  641. if (asd_id >= ARRAY_SIZE(asd_pcidev_data)) {
  642. asd_printk("wrong driver_data in PCI table\n");
  643. return -ENODEV;
  644. }
  645. if ((err = pci_enable_device(dev))) {
  646. asd_printk("couldn't enable device %s\n", pci_name(dev));
  647. return err;
  648. }
  649. pci_set_master(dev);
  650. err = -ENOMEM;
  651. shost = scsi_host_alloc(&aic94xx_sht, sizeof(void *));
  652. if (!shost)
  653. goto Err;
  654. asd_dev = &asd_pcidev_data[asd_id];
  655. asd_ha = kzalloc(sizeof(*asd_ha), GFP_KERNEL);
  656. if (!asd_ha) {
  657. asd_printk("out of memory\n");
  658. goto Err_put;
  659. }
  660. asd_ha->pcidev = dev;
  661. asd_ha->sas_ha.dev = &asd_ha->pcidev->dev;
  662. asd_ha->sas_ha.lldd_ha = asd_ha;
  663. asd_ha->bios_status = FLASH_OK;
  664. asd_ha->name = asd_dev->name;
  665. asd_printk("found %s, device %s\n", asd_ha->name, pci_name(dev));
  666. SHOST_TO_SAS_HA(shost) = &asd_ha->sas_ha;
  667. asd_ha->sas_ha.core.shost = shost;
  668. shost->transportt = aic94xx_transport_template;
  669. shost->max_id = ~0;
  670. shost->max_lun = ~0;
  671. shost->max_cmd_len = 16;
  672. err = scsi_add_host(shost, &dev->dev);
  673. if (err)
  674. goto Err_free;
  675. err = asd_dev->setup(asd_ha);
  676. if (err)
  677. goto Err_remove;
  678. err = -ENODEV;
  679. if (!pci_set_dma_mask(dev, DMA_BIT_MASK(64))
  680. && !pci_set_consistent_dma_mask(dev, DMA_BIT_MASK(64)))
  681. ;
  682. else if (!pci_set_dma_mask(dev, DMA_BIT_MASK(32))
  683. && !pci_set_consistent_dma_mask(dev, DMA_BIT_MASK(32)))
  684. ;
  685. else {
  686. asd_printk("no suitable DMA mask for %s\n", pci_name(dev));
  687. goto Err_remove;
  688. }
  689. pci_set_drvdata(dev, asd_ha);
  690. err = asd_map_ha(asd_ha);
  691. if (err)
  692. goto Err_remove;
  693. err = asd_create_ha_caches(asd_ha);
  694. if (err)
  695. goto Err_unmap;
  696. err = asd_init_hw(asd_ha);
  697. if (err)
  698. goto Err_free_cache;
  699. asd_printk("device %s: SAS addr %llx, PCBA SN %s, %d phys, %d enabled "
  700. "phys, flash %s, BIOS %s%d\n",
  701. pci_name(dev), SAS_ADDR(asd_ha->hw_prof.sas_addr),
  702. asd_ha->hw_prof.pcba_sn, asd_ha->hw_prof.max_phys,
  703. asd_ha->hw_prof.num_phys,
  704. asd_ha->hw_prof.flash.present ? "present" : "not present",
  705. asd_ha->hw_prof.bios.present ? "build " : "not present",
  706. asd_ha->hw_prof.bios.bld);
  707. shost->can_queue = asd_ha->seq.can_queue;
  708. if (use_msi)
  709. pci_enable_msi(asd_ha->pcidev);
  710. err = request_irq(asd_ha->pcidev->irq, asd_hw_isr, IRQF_SHARED,
  711. ASD_DRIVER_NAME, asd_ha);
  712. if (err) {
  713. asd_printk("couldn't get irq %d for %s\n",
  714. asd_ha->pcidev->irq, pci_name(asd_ha->pcidev));
  715. goto Err_irq;
  716. }
  717. asd_enable_ints(asd_ha);
  718. err = asd_init_post_escbs(asd_ha);
  719. if (err) {
  720. asd_printk("couldn't post escbs for %s\n",
  721. pci_name(asd_ha->pcidev));
  722. goto Err_escbs;
  723. }
  724. ASD_DPRINTK("escbs posted\n");
  725. err = asd_create_dev_attrs(asd_ha);
  726. if (err)
  727. goto Err_dev_attrs;
  728. err = asd_register_sas_ha(asd_ha);
  729. if (err)
  730. goto Err_reg_sas;
  731. scsi_scan_host(shost);
  732. return 0;
  733. Err_reg_sas:
  734. asd_remove_dev_attrs(asd_ha);
  735. Err_dev_attrs:
  736. Err_escbs:
  737. asd_disable_ints(asd_ha);
  738. free_irq(dev->irq, asd_ha);
  739. Err_irq:
  740. if (use_msi)
  741. pci_disable_msi(dev);
  742. asd_chip_hardrst(asd_ha);
  743. Err_free_cache:
  744. asd_destroy_ha_caches(asd_ha);
  745. Err_unmap:
  746. asd_unmap_ha(asd_ha);
  747. Err_remove:
  748. scsi_remove_host(shost);
  749. Err_free:
  750. kfree(asd_ha);
  751. Err_put:
  752. scsi_host_put(shost);
  753. Err:
  754. pci_disable_device(dev);
  755. return err;
  756. }
  757. static void asd_free_queues(struct asd_ha_struct *asd_ha)
  758. {
  759. unsigned long flags;
  760. LIST_HEAD(pending);
  761. struct list_head *n, *pos;
  762. spin_lock_irqsave(&asd_ha->seq.pend_q_lock, flags);
  763. asd_ha->seq.pending = 0;
  764. list_splice_init(&asd_ha->seq.pend_q, &pending);
  765. spin_unlock_irqrestore(&asd_ha->seq.pend_q_lock, flags);
  766. if (!list_empty(&pending))
  767. ASD_DPRINTK("Uh-oh! Pending is not empty!\n");
  768. list_for_each_safe(pos, n, &pending) {
  769. struct asd_ascb *ascb = list_entry(pos, struct asd_ascb, list);
  770. /*
  771. * Delete unexpired ascb timers. This may happen if we issue
  772. * a CONTROL PHY scb to an adapter and rmmod before the scb
  773. * times out. Apparently we don't wait for the CONTROL PHY
  774. * to complete, so it doesn't matter if we kill the timer.
  775. */
  776. del_timer_sync(&ascb->timer);
  777. WARN_ON(ascb->scb->header.opcode != CONTROL_PHY);
  778. list_del_init(pos);
  779. ASD_DPRINTK("freeing from pending\n");
  780. asd_ascb_free(ascb);
  781. }
  782. }
  783. static void asd_turn_off_leds(struct asd_ha_struct *asd_ha)
  784. {
  785. u8 phy_mask = asd_ha->hw_prof.enabled_phys;
  786. u8 i;
  787. for_each_phy(phy_mask, phy_mask, i) {
  788. asd_turn_led(asd_ha, i, 0);
  789. asd_control_led(asd_ha, i, 0);
  790. }
  791. }
  792. static void __devexit asd_pci_remove(struct pci_dev *dev)
  793. {
  794. struct asd_ha_struct *asd_ha = pci_get_drvdata(dev);
  795. if (!asd_ha)
  796. return;
  797. asd_unregister_sas_ha(asd_ha);
  798. asd_disable_ints(asd_ha);
  799. asd_remove_dev_attrs(asd_ha);
  800. /* XXX more here as needed */
  801. free_irq(dev->irq, asd_ha);
  802. if (use_msi)
  803. pci_disable_msi(asd_ha->pcidev);
  804. asd_turn_off_leds(asd_ha);
  805. asd_chip_hardrst(asd_ha);
  806. asd_free_queues(asd_ha);
  807. asd_destroy_ha_caches(asd_ha);
  808. asd_unmap_ha(asd_ha);
  809. kfree(asd_ha);
  810. pci_disable_device(dev);
  811. return;
  812. }
  813. static void asd_scan_start(struct Scsi_Host *shost)
  814. {
  815. struct asd_ha_struct *asd_ha;
  816. int err;
  817. asd_ha = SHOST_TO_SAS_HA(shost)->lldd_ha;
  818. err = asd_enable_phys(asd_ha, asd_ha->hw_prof.enabled_phys);
  819. if (err)
  820. asd_printk("Couldn't enable phys, err:%d\n", err);
  821. }
  822. static int asd_scan_finished(struct Scsi_Host *shost, unsigned long time)
  823. {
  824. /* give the phy enabling interrupt event time to come in (1s
  825. * is empirically about all it takes) */
  826. if (time < HZ)
  827. return 0;
  828. /* Wait for discovery to finish */
  829. sas_drain_work(SHOST_TO_SAS_HA(shost));
  830. return 1;
  831. }
  832. static ssize_t asd_version_show(struct device_driver *driver, char *buf)
  833. {
  834. return snprintf(buf, PAGE_SIZE, "%s\n", ASD_DRIVER_VERSION);
  835. }
  836. static DRIVER_ATTR(version, S_IRUGO, asd_version_show, NULL);
  837. static int asd_create_driver_attrs(struct device_driver *driver)
  838. {
  839. return driver_create_file(driver, &driver_attr_version);
  840. }
  841. static void asd_remove_driver_attrs(struct device_driver *driver)
  842. {
  843. driver_remove_file(driver, &driver_attr_version);
  844. }
  845. static struct sas_domain_function_template aic94xx_transport_functions = {
  846. .lldd_dev_found = asd_dev_found,
  847. .lldd_dev_gone = asd_dev_gone,
  848. .lldd_execute_task = asd_execute_task,
  849. .lldd_abort_task = asd_abort_task,
  850. .lldd_abort_task_set = asd_abort_task_set,
  851. .lldd_clear_aca = asd_clear_aca,
  852. .lldd_clear_task_set = asd_clear_task_set,
  853. .lldd_I_T_nexus_reset = asd_I_T_nexus_reset,
  854. .lldd_lu_reset = asd_lu_reset,
  855. .lldd_query_task = asd_query_task,
  856. .lldd_clear_nexus_port = asd_clear_nexus_port,
  857. .lldd_clear_nexus_ha = asd_clear_nexus_ha,
  858. .lldd_control_phy = asd_control_phy,
  859. .lldd_ata_set_dmamode = asd_set_dmamode,
  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);