aic94xx_init.c 27 KB

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