aic94xx_init.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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 <scsi/scsi_host.h>
  32. #include "aic94xx.h"
  33. #include "aic94xx_reg.h"
  34. #include "aic94xx_hwi.h"
  35. #include "aic94xx_seq.h"
  36. /* The format is "version.release.patchlevel" */
  37. #define ASD_DRIVER_VERSION "1.0.3"
  38. static int use_msi = 0;
  39. module_param_named(use_msi, use_msi, int, S_IRUGO);
  40. MODULE_PARM_DESC(use_msi, "\n"
  41. "\tEnable(1) or disable(0) using PCI MSI.\n"
  42. "\tDefault: 0");
  43. static int lldd_max_execute_num = 0;
  44. module_param_named(collector, lldd_max_execute_num, int, S_IRUGO);
  45. MODULE_PARM_DESC(collector, "\n"
  46. "\tIf greater than one, tells the SAS Layer to run in Task Collector\n"
  47. "\tMode. If 1 or 0, tells the SAS Layer to run in Direct Mode.\n"
  48. "\tThe aic94xx SAS LLDD supports both modes.\n"
  49. "\tDefault: 0 (Direct Mode).\n");
  50. char sas_addr_str[2*SAS_ADDR_SIZE + 1] = "";
  51. static struct scsi_transport_template *aic94xx_transport_template;
  52. static int asd_scan_finished(struct Scsi_Host *, unsigned long);
  53. static void asd_scan_start(struct Scsi_Host *);
  54. static struct scsi_host_template aic94xx_sht = {
  55. .module = THIS_MODULE,
  56. /* .name is initialized */
  57. .name = "aic94xx",
  58. .queuecommand = sas_queuecommand,
  59. .target_alloc = sas_target_alloc,
  60. .slave_configure = sas_slave_configure,
  61. .slave_destroy = sas_slave_destroy,
  62. .scan_finished = asd_scan_finished,
  63. .scan_start = asd_scan_start,
  64. .change_queue_depth = sas_change_queue_depth,
  65. .change_queue_type = sas_change_queue_type,
  66. .bios_param = sas_bios_param,
  67. .can_queue = 1,
  68. .cmd_per_lun = 1,
  69. .this_id = -1,
  70. .sg_tablesize = SG_ALL,
  71. .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
  72. .use_clustering = ENABLE_CLUSTERING,
  73. .eh_device_reset_handler = sas_eh_device_reset_handler,
  74. .eh_bus_reset_handler = sas_eh_bus_reset_handler,
  75. .slave_alloc = sas_slave_alloc,
  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 __devexit 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 __devexit 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 __devexit 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. static int asd_create_dev_attrs(struct asd_ha_struct *asd_ha)
  276. {
  277. int err;
  278. err = device_create_file(&asd_ha->pcidev->dev, &dev_attr_revision);
  279. if (err)
  280. return err;
  281. err = device_create_file(&asd_ha->pcidev->dev, &dev_attr_bios_build);
  282. if (err)
  283. goto err_rev;
  284. err = device_create_file(&asd_ha->pcidev->dev, &dev_attr_pcba_sn);
  285. if (err)
  286. goto err_biosb;
  287. return 0;
  288. err_biosb:
  289. device_remove_file(&asd_ha->pcidev->dev, &dev_attr_bios_build);
  290. err_rev:
  291. device_remove_file(&asd_ha->pcidev->dev, &dev_attr_revision);
  292. return err;
  293. }
  294. static void asd_remove_dev_attrs(struct asd_ha_struct *asd_ha)
  295. {
  296. device_remove_file(&asd_ha->pcidev->dev, &dev_attr_revision);
  297. device_remove_file(&asd_ha->pcidev->dev, &dev_attr_bios_build);
  298. device_remove_file(&asd_ha->pcidev->dev, &dev_attr_pcba_sn);
  299. }
  300. /* The first entry, 0, is used for dynamic ids, the rest for devices
  301. * we know about.
  302. */
  303. static struct asd_pcidev_struct {
  304. const char * name;
  305. int (*setup)(struct asd_ha_struct *asd_ha);
  306. } asd_pcidev_data[] = {
  307. /* Id 0 is used for dynamic ids. */
  308. { .name = "Adaptec AIC-94xx SAS/SATA Host Adapter",
  309. .setup = asd_aic9410_setup
  310. },
  311. { .name = "Adaptec AIC-9410W SAS/SATA Host Adapter",
  312. .setup = asd_aic9410_setup
  313. },
  314. { .name = "Adaptec AIC-9405W SAS/SATA Host Adapter",
  315. .setup = asd_aic9405_setup
  316. },
  317. };
  318. static inline int asd_create_ha_caches(struct asd_ha_struct *asd_ha)
  319. {
  320. asd_ha->scb_pool = dma_pool_create(ASD_DRIVER_NAME "_scb_pool",
  321. &asd_ha->pcidev->dev,
  322. sizeof(struct scb),
  323. 8, 0);
  324. if (!asd_ha->scb_pool) {
  325. asd_printk("couldn't create scb pool\n");
  326. return -ENOMEM;
  327. }
  328. return 0;
  329. }
  330. /**
  331. * asd_free_edbs -- free empty data buffers
  332. * asd_ha: pointer to host adapter structure
  333. */
  334. static inline void asd_free_edbs(struct asd_ha_struct *asd_ha)
  335. {
  336. struct asd_seq_data *seq = &asd_ha->seq;
  337. int i;
  338. for (i = 0; i < seq->num_edbs; i++)
  339. asd_free_coherent(asd_ha, seq->edb_arr[i]);
  340. kfree(seq->edb_arr);
  341. seq->edb_arr = NULL;
  342. }
  343. static inline void asd_free_escbs(struct asd_ha_struct *asd_ha)
  344. {
  345. struct asd_seq_data *seq = &asd_ha->seq;
  346. int i;
  347. for (i = 0; i < seq->num_escbs; i++) {
  348. if (!list_empty(&seq->escb_arr[i]->list))
  349. list_del_init(&seq->escb_arr[i]->list);
  350. asd_ascb_free(seq->escb_arr[i]);
  351. }
  352. kfree(seq->escb_arr);
  353. seq->escb_arr = NULL;
  354. }
  355. static inline void asd_destroy_ha_caches(struct asd_ha_struct *asd_ha)
  356. {
  357. int i;
  358. if (asd_ha->hw_prof.ddb_ext)
  359. asd_free_coherent(asd_ha, asd_ha->hw_prof.ddb_ext);
  360. if (asd_ha->hw_prof.scb_ext)
  361. asd_free_coherent(asd_ha, asd_ha->hw_prof.scb_ext);
  362. if (asd_ha->hw_prof.ddb_bitmap)
  363. kfree(asd_ha->hw_prof.ddb_bitmap);
  364. asd_ha->hw_prof.ddb_bitmap = NULL;
  365. for (i = 0; i < ASD_MAX_PHYS; i++) {
  366. struct asd_phy *phy = &asd_ha->phys[i];
  367. asd_free_coherent(asd_ha, phy->id_frm_tok);
  368. }
  369. if (asd_ha->seq.escb_arr)
  370. asd_free_escbs(asd_ha);
  371. if (asd_ha->seq.edb_arr)
  372. asd_free_edbs(asd_ha);
  373. if (asd_ha->hw_prof.ue.area) {
  374. kfree(asd_ha->hw_prof.ue.area);
  375. asd_ha->hw_prof.ue.area = NULL;
  376. }
  377. if (asd_ha->seq.tc_index_array) {
  378. kfree(asd_ha->seq.tc_index_array);
  379. kfree(asd_ha->seq.tc_index_bitmap);
  380. asd_ha->seq.tc_index_array = NULL;
  381. asd_ha->seq.tc_index_bitmap = NULL;
  382. }
  383. if (asd_ha->seq.actual_dl) {
  384. asd_free_coherent(asd_ha, asd_ha->seq.actual_dl);
  385. asd_ha->seq.actual_dl = NULL;
  386. asd_ha->seq.dl = NULL;
  387. }
  388. if (asd_ha->seq.next_scb.vaddr) {
  389. dma_pool_free(asd_ha->scb_pool, asd_ha->seq.next_scb.vaddr,
  390. asd_ha->seq.next_scb.dma_handle);
  391. asd_ha->seq.next_scb.vaddr = NULL;
  392. }
  393. dma_pool_destroy(asd_ha->scb_pool);
  394. asd_ha->scb_pool = NULL;
  395. }
  396. struct kmem_cache *asd_dma_token_cache;
  397. struct kmem_cache *asd_ascb_cache;
  398. static int asd_create_global_caches(void)
  399. {
  400. if (!asd_dma_token_cache) {
  401. asd_dma_token_cache
  402. = kmem_cache_create(ASD_DRIVER_NAME "_dma_token",
  403. sizeof(struct asd_dma_tok),
  404. 0,
  405. SLAB_HWCACHE_ALIGN,
  406. NULL);
  407. if (!asd_dma_token_cache) {
  408. asd_printk("couldn't create dma token cache\n");
  409. return -ENOMEM;
  410. }
  411. }
  412. if (!asd_ascb_cache) {
  413. asd_ascb_cache = kmem_cache_create(ASD_DRIVER_NAME "_ascb",
  414. sizeof(struct asd_ascb),
  415. 0,
  416. SLAB_HWCACHE_ALIGN,
  417. NULL);
  418. if (!asd_ascb_cache) {
  419. asd_printk("couldn't create ascb cache\n");
  420. goto Err;
  421. }
  422. }
  423. return 0;
  424. Err:
  425. kmem_cache_destroy(asd_dma_token_cache);
  426. asd_dma_token_cache = NULL;
  427. return -ENOMEM;
  428. }
  429. static void asd_destroy_global_caches(void)
  430. {
  431. if (asd_dma_token_cache)
  432. kmem_cache_destroy(asd_dma_token_cache);
  433. asd_dma_token_cache = NULL;
  434. if (asd_ascb_cache)
  435. kmem_cache_destroy(asd_ascb_cache);
  436. asd_ascb_cache = NULL;
  437. }
  438. static int asd_register_sas_ha(struct asd_ha_struct *asd_ha)
  439. {
  440. int i;
  441. struct asd_sas_phy **sas_phys =
  442. kmalloc(ASD_MAX_PHYS * sizeof(struct asd_sas_phy), GFP_KERNEL);
  443. struct asd_sas_port **sas_ports =
  444. kmalloc(ASD_MAX_PHYS * sizeof(struct asd_sas_port), GFP_KERNEL);
  445. if (!sas_phys || !sas_ports) {
  446. kfree(sas_phys);
  447. kfree(sas_ports);
  448. return -ENOMEM;
  449. }
  450. asd_ha->sas_ha.sas_ha_name = (char *) asd_ha->name;
  451. asd_ha->sas_ha.lldd_module = THIS_MODULE;
  452. asd_ha->sas_ha.sas_addr = &asd_ha->hw_prof.sas_addr[0];
  453. for (i = 0; i < ASD_MAX_PHYS; i++) {
  454. sas_phys[i] = &asd_ha->phys[i].sas_phy;
  455. sas_ports[i] = &asd_ha->ports[i];
  456. }
  457. asd_ha->sas_ha.sas_phy = sas_phys;
  458. asd_ha->sas_ha.sas_port= sas_ports;
  459. asd_ha->sas_ha.num_phys= ASD_MAX_PHYS;
  460. asd_ha->sas_ha.lldd_queue_size = asd_ha->seq.can_queue;
  461. asd_ha->sas_ha.lldd_max_execute_num = lldd_max_execute_num;
  462. return sas_register_ha(&asd_ha->sas_ha);
  463. }
  464. static int asd_unregister_sas_ha(struct asd_ha_struct *asd_ha)
  465. {
  466. int err;
  467. err = sas_unregister_ha(&asd_ha->sas_ha);
  468. sas_remove_host(asd_ha->sas_ha.core.shost);
  469. scsi_remove_host(asd_ha->sas_ha.core.shost);
  470. scsi_host_put(asd_ha->sas_ha.core.shost);
  471. kfree(asd_ha->sas_ha.sas_phy);
  472. kfree(asd_ha->sas_ha.sas_port);
  473. return err;
  474. }
  475. static int __devinit asd_pci_probe(struct pci_dev *dev,
  476. const struct pci_device_id *id)
  477. {
  478. struct asd_pcidev_struct *asd_dev;
  479. unsigned asd_id = (unsigned) id->driver_data;
  480. struct asd_ha_struct *asd_ha;
  481. struct Scsi_Host *shost;
  482. int err;
  483. if (asd_id >= ARRAY_SIZE(asd_pcidev_data)) {
  484. asd_printk("wrong driver_data in PCI table\n");
  485. return -ENODEV;
  486. }
  487. if ((err = pci_enable_device(dev))) {
  488. asd_printk("couldn't enable device %s\n", pci_name(dev));
  489. return err;
  490. }
  491. pci_set_master(dev);
  492. err = -ENOMEM;
  493. shost = scsi_host_alloc(&aic94xx_sht, sizeof(void *));
  494. if (!shost)
  495. goto Err;
  496. asd_dev = &asd_pcidev_data[asd_id];
  497. asd_ha = kzalloc(sizeof(*asd_ha), GFP_KERNEL);
  498. if (!asd_ha) {
  499. asd_printk("out of memory\n");
  500. goto Err_put;
  501. }
  502. asd_ha->pcidev = dev;
  503. asd_ha->sas_ha.dev = &asd_ha->pcidev->dev;
  504. asd_ha->sas_ha.lldd_ha = asd_ha;
  505. asd_ha->name = asd_dev->name;
  506. asd_printk("found %s, device %s\n", asd_ha->name, pci_name(dev));
  507. SHOST_TO_SAS_HA(shost) = &asd_ha->sas_ha;
  508. asd_ha->sas_ha.core.shost = shost;
  509. shost->transportt = aic94xx_transport_template;
  510. shost->max_id = ~0;
  511. shost->max_lun = ~0;
  512. shost->max_cmd_len = 16;
  513. err = scsi_add_host(shost, &dev->dev);
  514. if (err)
  515. goto Err_free;
  516. err = asd_dev->setup(asd_ha);
  517. if (err)
  518. goto Err_remove;
  519. err = -ENODEV;
  520. if (!pci_set_dma_mask(dev, DMA_64BIT_MASK)
  521. && !pci_set_consistent_dma_mask(dev, DMA_64BIT_MASK))
  522. ;
  523. else if (!pci_set_dma_mask(dev, DMA_32BIT_MASK)
  524. && !pci_set_consistent_dma_mask(dev, DMA_32BIT_MASK))
  525. ;
  526. else {
  527. asd_printk("no suitable DMA mask for %s\n", pci_name(dev));
  528. goto Err_remove;
  529. }
  530. pci_set_drvdata(dev, asd_ha);
  531. err = asd_map_ha(asd_ha);
  532. if (err)
  533. goto Err_remove;
  534. err = asd_create_ha_caches(asd_ha);
  535. if (err)
  536. goto Err_unmap;
  537. err = asd_init_hw(asd_ha);
  538. if (err)
  539. goto Err_free_cache;
  540. asd_printk("device %s: SAS addr %llx, PCBA SN %s, %d phys, %d enabled "
  541. "phys, flash %s, BIOS %s%d\n",
  542. pci_name(dev), SAS_ADDR(asd_ha->hw_prof.sas_addr),
  543. asd_ha->hw_prof.pcba_sn, asd_ha->hw_prof.max_phys,
  544. asd_ha->hw_prof.num_phys,
  545. asd_ha->hw_prof.flash.present ? "present" : "not present",
  546. asd_ha->hw_prof.bios.present ? "build " : "not present",
  547. asd_ha->hw_prof.bios.bld);
  548. shost->can_queue = asd_ha->seq.can_queue;
  549. if (use_msi)
  550. pci_enable_msi(asd_ha->pcidev);
  551. err = request_irq(asd_ha->pcidev->irq, asd_hw_isr, IRQF_SHARED,
  552. ASD_DRIVER_NAME, asd_ha);
  553. if (err) {
  554. asd_printk("couldn't get irq %d for %s\n",
  555. asd_ha->pcidev->irq, pci_name(asd_ha->pcidev));
  556. goto Err_irq;
  557. }
  558. asd_enable_ints(asd_ha);
  559. err = asd_init_post_escbs(asd_ha);
  560. if (err) {
  561. asd_printk("couldn't post escbs for %s\n",
  562. pci_name(asd_ha->pcidev));
  563. goto Err_escbs;
  564. }
  565. ASD_DPRINTK("escbs posted\n");
  566. err = asd_create_dev_attrs(asd_ha);
  567. if (err)
  568. goto Err_dev_attrs;
  569. err = asd_register_sas_ha(asd_ha);
  570. if (err)
  571. goto Err_reg_sas;
  572. scsi_scan_host(shost);
  573. return 0;
  574. Err_reg_sas:
  575. asd_remove_dev_attrs(asd_ha);
  576. Err_dev_attrs:
  577. Err_escbs:
  578. asd_disable_ints(asd_ha);
  579. free_irq(dev->irq, asd_ha);
  580. Err_irq:
  581. if (use_msi)
  582. pci_disable_msi(dev);
  583. asd_chip_hardrst(asd_ha);
  584. Err_free_cache:
  585. asd_destroy_ha_caches(asd_ha);
  586. Err_unmap:
  587. asd_unmap_ha(asd_ha);
  588. Err_remove:
  589. scsi_remove_host(shost);
  590. Err_free:
  591. kfree(asd_ha);
  592. Err_put:
  593. scsi_host_put(shost);
  594. Err:
  595. pci_disable_device(dev);
  596. return err;
  597. }
  598. static void asd_free_queues(struct asd_ha_struct *asd_ha)
  599. {
  600. unsigned long flags;
  601. LIST_HEAD(pending);
  602. struct list_head *n, *pos;
  603. spin_lock_irqsave(&asd_ha->seq.pend_q_lock, flags);
  604. asd_ha->seq.pending = 0;
  605. list_splice_init(&asd_ha->seq.pend_q, &pending);
  606. spin_unlock_irqrestore(&asd_ha->seq.pend_q_lock, flags);
  607. if (!list_empty(&pending))
  608. ASD_DPRINTK("Uh-oh! Pending is not empty!\n");
  609. list_for_each_safe(pos, n, &pending) {
  610. struct asd_ascb *ascb = list_entry(pos, struct asd_ascb, list);
  611. /*
  612. * Delete unexpired ascb timers. This may happen if we issue
  613. * a CONTROL PHY scb to an adapter and rmmod before the scb
  614. * times out. Apparently we don't wait for the CONTROL PHY
  615. * to complete, so it doesn't matter if we kill the timer.
  616. */
  617. del_timer_sync(&ascb->timer);
  618. WARN_ON(ascb->scb->header.opcode != CONTROL_PHY);
  619. list_del_init(pos);
  620. ASD_DPRINTK("freeing from pending\n");
  621. asd_ascb_free(ascb);
  622. }
  623. }
  624. static void asd_turn_off_leds(struct asd_ha_struct *asd_ha)
  625. {
  626. u8 phy_mask = asd_ha->hw_prof.enabled_phys;
  627. u8 i;
  628. for_each_phy(phy_mask, phy_mask, i) {
  629. asd_turn_led(asd_ha, i, 0);
  630. asd_control_led(asd_ha, i, 0);
  631. }
  632. }
  633. static void __devexit asd_pci_remove(struct pci_dev *dev)
  634. {
  635. struct asd_ha_struct *asd_ha = pci_get_drvdata(dev);
  636. if (!asd_ha)
  637. return;
  638. asd_unregister_sas_ha(asd_ha);
  639. asd_disable_ints(asd_ha);
  640. asd_remove_dev_attrs(asd_ha);
  641. /* XXX more here as needed */
  642. free_irq(dev->irq, asd_ha);
  643. if (use_msi)
  644. pci_disable_msi(asd_ha->pcidev);
  645. asd_turn_off_leds(asd_ha);
  646. asd_chip_hardrst(asd_ha);
  647. asd_free_queues(asd_ha);
  648. asd_destroy_ha_caches(asd_ha);
  649. asd_unmap_ha(asd_ha);
  650. kfree(asd_ha);
  651. pci_disable_device(dev);
  652. return;
  653. }
  654. static void asd_scan_start(struct Scsi_Host *shost)
  655. {
  656. struct asd_ha_struct *asd_ha;
  657. int err;
  658. asd_ha = SHOST_TO_SAS_HA(shost)->lldd_ha;
  659. err = asd_enable_phys(asd_ha, asd_ha->hw_prof.enabled_phys);
  660. if (err)
  661. asd_printk("Couldn't enable phys, err:%d\n", err);
  662. }
  663. static int asd_scan_finished(struct Scsi_Host *shost, unsigned long time)
  664. {
  665. /* give the phy enabling interrupt event time to come in (1s
  666. * is empirically about all it takes) */
  667. if (time < HZ)
  668. return 0;
  669. /* Wait for discovery to finish */
  670. scsi_flush_work(shost);
  671. return 1;
  672. }
  673. static ssize_t asd_version_show(struct device_driver *driver, char *buf)
  674. {
  675. return snprintf(buf, PAGE_SIZE, "%s\n", ASD_DRIVER_VERSION);
  676. }
  677. static DRIVER_ATTR(version, S_IRUGO, asd_version_show, NULL);
  678. static int asd_create_driver_attrs(struct device_driver *driver)
  679. {
  680. return driver_create_file(driver, &driver_attr_version);
  681. }
  682. static void asd_remove_driver_attrs(struct device_driver *driver)
  683. {
  684. driver_remove_file(driver, &driver_attr_version);
  685. }
  686. static struct sas_domain_function_template aic94xx_transport_functions = {
  687. .lldd_dev_found = asd_dev_found,
  688. .lldd_dev_gone = asd_dev_gone,
  689. .lldd_execute_task = asd_execute_task,
  690. .lldd_abort_task = asd_abort_task,
  691. .lldd_abort_task_set = asd_abort_task_set,
  692. .lldd_clear_aca = asd_clear_aca,
  693. .lldd_clear_task_set = asd_clear_task_set,
  694. .lldd_I_T_nexus_reset = NULL,
  695. .lldd_lu_reset = asd_lu_reset,
  696. .lldd_query_task = asd_query_task,
  697. .lldd_clear_nexus_port = asd_clear_nexus_port,
  698. .lldd_clear_nexus_ha = asd_clear_nexus_ha,
  699. .lldd_control_phy = asd_control_phy,
  700. };
  701. static const struct pci_device_id aic94xx_pci_table[] __devinitdata = {
  702. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x410),0, 0, 1},
  703. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x412),0, 0, 1},
  704. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x416),0, 0, 1},
  705. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x41E),0, 0, 1},
  706. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x41F),0, 0, 1},
  707. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x430),0, 0, 2},
  708. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x432),0, 0, 2},
  709. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x43E),0, 0, 2},
  710. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x43F),0, 0, 2},
  711. {}
  712. };
  713. MODULE_DEVICE_TABLE(pci, aic94xx_pci_table);
  714. static struct pci_driver aic94xx_pci_driver = {
  715. .name = ASD_DRIVER_NAME,
  716. .id_table = aic94xx_pci_table,
  717. .probe = asd_pci_probe,
  718. .remove = __devexit_p(asd_pci_remove),
  719. };
  720. static int __init aic94xx_init(void)
  721. {
  722. int err;
  723. asd_printk("%s version %s loaded\n", ASD_DRIVER_DESCRIPTION,
  724. ASD_DRIVER_VERSION);
  725. err = asd_create_global_caches();
  726. if (err)
  727. return err;
  728. aic94xx_transport_template =
  729. sas_domain_attach_transport(&aic94xx_transport_functions);
  730. if (!aic94xx_transport_template)
  731. goto out_destroy_caches;
  732. err = pci_register_driver(&aic94xx_pci_driver);
  733. if (err)
  734. goto out_release_transport;
  735. err = asd_create_driver_attrs(&aic94xx_pci_driver.driver);
  736. if (err)
  737. goto out_unregister_pcidrv;
  738. return err;
  739. out_unregister_pcidrv:
  740. pci_unregister_driver(&aic94xx_pci_driver);
  741. out_release_transport:
  742. sas_release_transport(aic94xx_transport_template);
  743. out_destroy_caches:
  744. asd_destroy_global_caches();
  745. return err;
  746. }
  747. static void __exit aic94xx_exit(void)
  748. {
  749. asd_remove_driver_attrs(&aic94xx_pci_driver.driver);
  750. pci_unregister_driver(&aic94xx_pci_driver);
  751. sas_release_transport(aic94xx_transport_template);
  752. asd_release_firmware();
  753. asd_destroy_global_caches();
  754. asd_printk("%s version %s unloaded\n", ASD_DRIVER_DESCRIPTION,
  755. ASD_DRIVER_VERSION);
  756. }
  757. module_init(aic94xx_init);
  758. module_exit(aic94xx_exit);
  759. MODULE_AUTHOR("Luben Tuikov <luben_tuikov@adaptec.com>");
  760. MODULE_DESCRIPTION(ASD_DRIVER_DESCRIPTION);
  761. MODULE_LICENSE("GPL v2");
  762. MODULE_VERSION(ASD_DRIVER_VERSION);