aic94xx_init.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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. };
  76. static int __devinit asd_map_memio(struct asd_ha_struct *asd_ha)
  77. {
  78. int err, i;
  79. struct asd_ha_addrspace *io_handle;
  80. asd_ha->iospace = 0;
  81. for (i = 0; i < 3; i += 2) {
  82. io_handle = &asd_ha->io_handle[i==0?0:1];
  83. io_handle->start = pci_resource_start(asd_ha->pcidev, i);
  84. io_handle->len = pci_resource_len(asd_ha->pcidev, i);
  85. io_handle->flags = pci_resource_flags(asd_ha->pcidev, i);
  86. err = -ENODEV;
  87. if (!io_handle->start || !io_handle->len) {
  88. asd_printk("MBAR%d start or length for %s is 0.\n",
  89. i==0?0:1, pci_name(asd_ha->pcidev));
  90. goto Err;
  91. }
  92. err = pci_request_region(asd_ha->pcidev, i, ASD_DRIVER_NAME);
  93. if (err) {
  94. asd_printk("couldn't reserve memory region for %s\n",
  95. pci_name(asd_ha->pcidev));
  96. goto Err;
  97. }
  98. if (io_handle->flags & IORESOURCE_CACHEABLE)
  99. io_handle->addr = ioremap(io_handle->start,
  100. io_handle->len);
  101. else
  102. io_handle->addr = ioremap_nocache(io_handle->start,
  103. io_handle->len);
  104. if (!io_handle->addr) {
  105. asd_printk("couldn't map MBAR%d of %s\n", i==0?0:1,
  106. pci_name(asd_ha->pcidev));
  107. goto Err_unreq;
  108. }
  109. }
  110. return 0;
  111. Err_unreq:
  112. pci_release_region(asd_ha->pcidev, i);
  113. Err:
  114. if (i > 0) {
  115. io_handle = &asd_ha->io_handle[0];
  116. iounmap(io_handle->addr);
  117. pci_release_region(asd_ha->pcidev, 0);
  118. }
  119. return err;
  120. }
  121. static void __devexit asd_unmap_memio(struct asd_ha_struct *asd_ha)
  122. {
  123. struct asd_ha_addrspace *io_handle;
  124. io_handle = &asd_ha->io_handle[1];
  125. iounmap(io_handle->addr);
  126. pci_release_region(asd_ha->pcidev, 2);
  127. io_handle = &asd_ha->io_handle[0];
  128. iounmap(io_handle->addr);
  129. pci_release_region(asd_ha->pcidev, 0);
  130. }
  131. static int __devinit asd_map_ioport(struct asd_ha_struct *asd_ha)
  132. {
  133. int i = PCI_IOBAR_OFFSET, err;
  134. struct asd_ha_addrspace *io_handle = &asd_ha->io_handle[0];
  135. asd_ha->iospace = 1;
  136. io_handle->start = pci_resource_start(asd_ha->pcidev, i);
  137. io_handle->len = pci_resource_len(asd_ha->pcidev, i);
  138. io_handle->flags = pci_resource_flags(asd_ha->pcidev, i);
  139. io_handle->addr = (void __iomem *) io_handle->start;
  140. if (!io_handle->start || !io_handle->len) {
  141. asd_printk("couldn't get IO ports for %s\n",
  142. pci_name(asd_ha->pcidev));
  143. return -ENODEV;
  144. }
  145. err = pci_request_region(asd_ha->pcidev, i, ASD_DRIVER_NAME);
  146. if (err) {
  147. asd_printk("couldn't reserve io space for %s\n",
  148. pci_name(asd_ha->pcidev));
  149. }
  150. return err;
  151. }
  152. static void __devexit asd_unmap_ioport(struct asd_ha_struct *asd_ha)
  153. {
  154. pci_release_region(asd_ha->pcidev, PCI_IOBAR_OFFSET);
  155. }
  156. static int __devinit asd_map_ha(struct asd_ha_struct *asd_ha)
  157. {
  158. int err;
  159. u16 cmd_reg;
  160. err = pci_read_config_word(asd_ha->pcidev, PCI_COMMAND, &cmd_reg);
  161. if (err) {
  162. asd_printk("couldn't read command register of %s\n",
  163. pci_name(asd_ha->pcidev));
  164. goto Err;
  165. }
  166. err = -ENODEV;
  167. if (cmd_reg & PCI_COMMAND_MEMORY) {
  168. if ((err = asd_map_memio(asd_ha)))
  169. goto Err;
  170. } else if (cmd_reg & PCI_COMMAND_IO) {
  171. if ((err = asd_map_ioport(asd_ha)))
  172. goto Err;
  173. asd_printk("%s ioport mapped -- upgrade your hardware\n",
  174. pci_name(asd_ha->pcidev));
  175. } else {
  176. asd_printk("no proper device access to %s\n",
  177. pci_name(asd_ha->pcidev));
  178. goto Err;
  179. }
  180. return 0;
  181. Err:
  182. return err;
  183. }
  184. static void __devexit asd_unmap_ha(struct asd_ha_struct *asd_ha)
  185. {
  186. if (asd_ha->iospace)
  187. asd_unmap_ioport(asd_ha);
  188. else
  189. asd_unmap_memio(asd_ha);
  190. }
  191. static const char *asd_dev_rev[30] = {
  192. [0] = "A0",
  193. [1] = "A1",
  194. [8] = "B0",
  195. };
  196. static int __devinit asd_common_setup(struct asd_ha_struct *asd_ha)
  197. {
  198. int err, i;
  199. err = pci_read_config_byte(asd_ha->pcidev, PCI_REVISION_ID,
  200. &asd_ha->revision_id);
  201. if (err) {
  202. asd_printk("couldn't read REVISION ID register of %s\n",
  203. pci_name(asd_ha->pcidev));
  204. goto Err;
  205. }
  206. err = -ENODEV;
  207. if (asd_ha->revision_id < AIC9410_DEV_REV_B0) {
  208. asd_printk("%s is revision %s (%X), which is not supported\n",
  209. pci_name(asd_ha->pcidev),
  210. asd_dev_rev[asd_ha->revision_id],
  211. asd_ha->revision_id);
  212. goto Err;
  213. }
  214. /* Provide some sane default values. */
  215. asd_ha->hw_prof.max_scbs = 512;
  216. asd_ha->hw_prof.max_ddbs = ASD_MAX_DDBS;
  217. asd_ha->hw_prof.num_phys = ASD_MAX_PHYS;
  218. /* All phys are enabled, by default. */
  219. asd_ha->hw_prof.enabled_phys = 0xFF;
  220. for (i = 0; i < ASD_MAX_PHYS; i++) {
  221. asd_ha->hw_prof.phy_desc[i].max_sas_lrate =
  222. SAS_LINK_RATE_3_0_GBPS;
  223. asd_ha->hw_prof.phy_desc[i].min_sas_lrate =
  224. SAS_LINK_RATE_1_5_GBPS;
  225. asd_ha->hw_prof.phy_desc[i].max_sata_lrate =
  226. SAS_LINK_RATE_1_5_GBPS;
  227. asd_ha->hw_prof.phy_desc[i].min_sata_lrate =
  228. SAS_LINK_RATE_1_5_GBPS;
  229. }
  230. return 0;
  231. Err:
  232. return err;
  233. }
  234. static int __devinit asd_aic9410_setup(struct asd_ha_struct *asd_ha)
  235. {
  236. int err = asd_common_setup(asd_ha);
  237. if (err)
  238. return err;
  239. asd_ha->hw_prof.addr_range = 8;
  240. asd_ha->hw_prof.port_name_base = 0;
  241. asd_ha->hw_prof.dev_name_base = 8;
  242. asd_ha->hw_prof.sata_name_base = 16;
  243. return 0;
  244. }
  245. static int __devinit asd_aic9405_setup(struct asd_ha_struct *asd_ha)
  246. {
  247. int err = asd_common_setup(asd_ha);
  248. if (err)
  249. return err;
  250. asd_ha->hw_prof.addr_range = 4;
  251. asd_ha->hw_prof.port_name_base = 0;
  252. asd_ha->hw_prof.dev_name_base = 4;
  253. asd_ha->hw_prof.sata_name_base = 8;
  254. return 0;
  255. }
  256. static ssize_t asd_show_dev_rev(struct device *dev,
  257. struct device_attribute *attr, char *buf)
  258. {
  259. struct asd_ha_struct *asd_ha = dev_to_asd_ha(dev);
  260. return snprintf(buf, PAGE_SIZE, "%s\n",
  261. asd_dev_rev[asd_ha->revision_id]);
  262. }
  263. static DEVICE_ATTR(revision, S_IRUGO, asd_show_dev_rev, NULL);
  264. static ssize_t asd_show_dev_bios_build(struct device *dev,
  265. struct device_attribute *attr,char *buf)
  266. {
  267. struct asd_ha_struct *asd_ha = dev_to_asd_ha(dev);
  268. return snprintf(buf, PAGE_SIZE, "%d\n", asd_ha->hw_prof.bios.bld);
  269. }
  270. static DEVICE_ATTR(bios_build, S_IRUGO, asd_show_dev_bios_build, NULL);
  271. static ssize_t asd_show_dev_pcba_sn(struct device *dev,
  272. struct device_attribute *attr, char *buf)
  273. {
  274. struct asd_ha_struct *asd_ha = dev_to_asd_ha(dev);
  275. return snprintf(buf, PAGE_SIZE, "%s\n", asd_ha->hw_prof.pcba_sn);
  276. }
  277. static DEVICE_ATTR(pcba_sn, S_IRUGO, asd_show_dev_pcba_sn, NULL);
  278. static int asd_create_dev_attrs(struct asd_ha_struct *asd_ha)
  279. {
  280. int err;
  281. err = device_create_file(&asd_ha->pcidev->dev, &dev_attr_revision);
  282. if (err)
  283. return err;
  284. err = device_create_file(&asd_ha->pcidev->dev, &dev_attr_bios_build);
  285. if (err)
  286. goto err_rev;
  287. err = device_create_file(&asd_ha->pcidev->dev, &dev_attr_pcba_sn);
  288. if (err)
  289. goto err_biosb;
  290. return 0;
  291. err_biosb:
  292. device_remove_file(&asd_ha->pcidev->dev, &dev_attr_bios_build);
  293. err_rev:
  294. device_remove_file(&asd_ha->pcidev->dev, &dev_attr_revision);
  295. return err;
  296. }
  297. static void asd_remove_dev_attrs(struct asd_ha_struct *asd_ha)
  298. {
  299. device_remove_file(&asd_ha->pcidev->dev, &dev_attr_revision);
  300. device_remove_file(&asd_ha->pcidev->dev, &dev_attr_bios_build);
  301. device_remove_file(&asd_ha->pcidev->dev, &dev_attr_pcba_sn);
  302. }
  303. /* The first entry, 0, is used for dynamic ids, the rest for devices
  304. * we know about.
  305. */
  306. static struct asd_pcidev_struct {
  307. const char * name;
  308. int (*setup)(struct asd_ha_struct *asd_ha);
  309. } asd_pcidev_data[] = {
  310. /* Id 0 is used for dynamic ids. */
  311. { .name = "Adaptec AIC-94xx SAS/SATA Host Adapter",
  312. .setup = asd_aic9410_setup
  313. },
  314. { .name = "Adaptec AIC-9410W SAS/SATA Host Adapter",
  315. .setup = asd_aic9410_setup
  316. },
  317. { .name = "Adaptec AIC-9405W SAS/SATA Host Adapter",
  318. .setup = asd_aic9405_setup
  319. },
  320. };
  321. static inline int asd_create_ha_caches(struct asd_ha_struct *asd_ha)
  322. {
  323. asd_ha->scb_pool = dma_pool_create(ASD_DRIVER_NAME "_scb_pool",
  324. &asd_ha->pcidev->dev,
  325. sizeof(struct scb),
  326. 8, 0);
  327. if (!asd_ha->scb_pool) {
  328. asd_printk("couldn't create scb pool\n");
  329. return -ENOMEM;
  330. }
  331. return 0;
  332. }
  333. /**
  334. * asd_free_edbs -- free empty data buffers
  335. * asd_ha: pointer to host adapter structure
  336. */
  337. static inline void asd_free_edbs(struct asd_ha_struct *asd_ha)
  338. {
  339. struct asd_seq_data *seq = &asd_ha->seq;
  340. int i;
  341. for (i = 0; i < seq->num_edbs; i++)
  342. asd_free_coherent(asd_ha, seq->edb_arr[i]);
  343. kfree(seq->edb_arr);
  344. seq->edb_arr = NULL;
  345. }
  346. static inline void asd_free_escbs(struct asd_ha_struct *asd_ha)
  347. {
  348. struct asd_seq_data *seq = &asd_ha->seq;
  349. int i;
  350. for (i = 0; i < seq->num_escbs; i++) {
  351. if (!list_empty(&seq->escb_arr[i]->list))
  352. list_del_init(&seq->escb_arr[i]->list);
  353. asd_ascb_free(seq->escb_arr[i]);
  354. }
  355. kfree(seq->escb_arr);
  356. seq->escb_arr = NULL;
  357. }
  358. static inline void asd_destroy_ha_caches(struct asd_ha_struct *asd_ha)
  359. {
  360. int i;
  361. if (asd_ha->hw_prof.ddb_ext)
  362. asd_free_coherent(asd_ha, asd_ha->hw_prof.ddb_ext);
  363. if (asd_ha->hw_prof.scb_ext)
  364. asd_free_coherent(asd_ha, asd_ha->hw_prof.scb_ext);
  365. if (asd_ha->hw_prof.ddb_bitmap)
  366. kfree(asd_ha->hw_prof.ddb_bitmap);
  367. asd_ha->hw_prof.ddb_bitmap = NULL;
  368. for (i = 0; i < ASD_MAX_PHYS; i++) {
  369. struct asd_phy *phy = &asd_ha->phys[i];
  370. asd_free_coherent(asd_ha, phy->id_frm_tok);
  371. }
  372. if (asd_ha->seq.escb_arr)
  373. asd_free_escbs(asd_ha);
  374. if (asd_ha->seq.edb_arr)
  375. asd_free_edbs(asd_ha);
  376. if (asd_ha->hw_prof.ue.area) {
  377. kfree(asd_ha->hw_prof.ue.area);
  378. asd_ha->hw_prof.ue.area = NULL;
  379. }
  380. if (asd_ha->seq.tc_index_array) {
  381. kfree(asd_ha->seq.tc_index_array);
  382. kfree(asd_ha->seq.tc_index_bitmap);
  383. asd_ha->seq.tc_index_array = NULL;
  384. asd_ha->seq.tc_index_bitmap = NULL;
  385. }
  386. if (asd_ha->seq.actual_dl) {
  387. asd_free_coherent(asd_ha, asd_ha->seq.actual_dl);
  388. asd_ha->seq.actual_dl = NULL;
  389. asd_ha->seq.dl = NULL;
  390. }
  391. if (asd_ha->seq.next_scb.vaddr) {
  392. dma_pool_free(asd_ha->scb_pool, asd_ha->seq.next_scb.vaddr,
  393. asd_ha->seq.next_scb.dma_handle);
  394. asd_ha->seq.next_scb.vaddr = NULL;
  395. }
  396. dma_pool_destroy(asd_ha->scb_pool);
  397. asd_ha->scb_pool = NULL;
  398. }
  399. struct kmem_cache *asd_dma_token_cache;
  400. struct kmem_cache *asd_ascb_cache;
  401. static int asd_create_global_caches(void)
  402. {
  403. if (!asd_dma_token_cache) {
  404. asd_dma_token_cache
  405. = kmem_cache_create(ASD_DRIVER_NAME "_dma_token",
  406. sizeof(struct asd_dma_tok),
  407. 0,
  408. SLAB_HWCACHE_ALIGN,
  409. NULL, NULL);
  410. if (!asd_dma_token_cache) {
  411. asd_printk("couldn't create dma token cache\n");
  412. return -ENOMEM;
  413. }
  414. }
  415. if (!asd_ascb_cache) {
  416. asd_ascb_cache = kmem_cache_create(ASD_DRIVER_NAME "_ascb",
  417. sizeof(struct asd_ascb),
  418. 0,
  419. SLAB_HWCACHE_ALIGN,
  420. NULL, NULL);
  421. if (!asd_ascb_cache) {
  422. asd_printk("couldn't create ascb cache\n");
  423. goto Err;
  424. }
  425. }
  426. return 0;
  427. Err:
  428. kmem_cache_destroy(asd_dma_token_cache);
  429. asd_dma_token_cache = NULL;
  430. return -ENOMEM;
  431. }
  432. static void asd_destroy_global_caches(void)
  433. {
  434. if (asd_dma_token_cache)
  435. kmem_cache_destroy(asd_dma_token_cache);
  436. asd_dma_token_cache = NULL;
  437. if (asd_ascb_cache)
  438. kmem_cache_destroy(asd_ascb_cache);
  439. asd_ascb_cache = NULL;
  440. }
  441. static int asd_register_sas_ha(struct asd_ha_struct *asd_ha)
  442. {
  443. int i;
  444. struct asd_sas_phy **sas_phys =
  445. kmalloc(ASD_MAX_PHYS * sizeof(struct asd_sas_phy), GFP_KERNEL);
  446. struct asd_sas_port **sas_ports =
  447. kmalloc(ASD_MAX_PHYS * sizeof(struct asd_sas_port), GFP_KERNEL);
  448. if (!sas_phys || !sas_ports) {
  449. kfree(sas_phys);
  450. kfree(sas_ports);
  451. return -ENOMEM;
  452. }
  453. asd_ha->sas_ha.sas_ha_name = (char *) asd_ha->name;
  454. asd_ha->sas_ha.lldd_module = THIS_MODULE;
  455. asd_ha->sas_ha.sas_addr = &asd_ha->hw_prof.sas_addr[0];
  456. for (i = 0; i < ASD_MAX_PHYS; i++) {
  457. sas_phys[i] = &asd_ha->phys[i].sas_phy;
  458. sas_ports[i] = &asd_ha->ports[i];
  459. }
  460. asd_ha->sas_ha.sas_phy = sas_phys;
  461. asd_ha->sas_ha.sas_port= sas_ports;
  462. asd_ha->sas_ha.num_phys= ASD_MAX_PHYS;
  463. asd_ha->sas_ha.lldd_queue_size = asd_ha->seq.can_queue;
  464. asd_ha->sas_ha.lldd_max_execute_num = lldd_max_execute_num;
  465. return sas_register_ha(&asd_ha->sas_ha);
  466. }
  467. static int asd_unregister_sas_ha(struct asd_ha_struct *asd_ha)
  468. {
  469. int err;
  470. err = sas_unregister_ha(&asd_ha->sas_ha);
  471. sas_remove_host(asd_ha->sas_ha.core.shost);
  472. scsi_remove_host(asd_ha->sas_ha.core.shost);
  473. scsi_host_put(asd_ha->sas_ha.core.shost);
  474. kfree(asd_ha->sas_ha.sas_phy);
  475. kfree(asd_ha->sas_ha.sas_port);
  476. return err;
  477. }
  478. static int __devinit asd_pci_probe(struct pci_dev *dev,
  479. const struct pci_device_id *id)
  480. {
  481. struct asd_pcidev_struct *asd_dev;
  482. unsigned asd_id = (unsigned) id->driver_data;
  483. struct asd_ha_struct *asd_ha;
  484. struct Scsi_Host *shost;
  485. int err;
  486. if (asd_id >= ARRAY_SIZE(asd_pcidev_data)) {
  487. asd_printk("wrong driver_data in PCI table\n");
  488. return -ENODEV;
  489. }
  490. if ((err = pci_enable_device(dev))) {
  491. asd_printk("couldn't enable device %s\n", pci_name(dev));
  492. return err;
  493. }
  494. pci_set_master(dev);
  495. err = -ENOMEM;
  496. shost = scsi_host_alloc(&aic94xx_sht, sizeof(void *));
  497. if (!shost)
  498. goto Err;
  499. asd_dev = &asd_pcidev_data[asd_id];
  500. asd_ha = kzalloc(sizeof(*asd_ha), GFP_KERNEL);
  501. if (!asd_ha) {
  502. asd_printk("out of memory\n");
  503. goto Err;
  504. }
  505. asd_ha->pcidev = dev;
  506. asd_ha->sas_ha.pcidev = asd_ha->pcidev;
  507. asd_ha->sas_ha.lldd_ha = asd_ha;
  508. asd_ha->name = asd_dev->name;
  509. asd_printk("found %s, device %s\n", asd_ha->name, pci_name(dev));
  510. SHOST_TO_SAS_HA(shost) = &asd_ha->sas_ha;
  511. asd_ha->sas_ha.core.shost = shost;
  512. shost->transportt = aic94xx_transport_template;
  513. shost->max_id = ~0;
  514. shost->max_lun = ~0;
  515. shost->max_cmd_len = 16;
  516. err = scsi_add_host(shost, &dev->dev);
  517. if (err) {
  518. scsi_host_put(shost);
  519. goto Err_free;
  520. }
  521. err = asd_dev->setup(asd_ha);
  522. if (err)
  523. goto Err_free;
  524. err = -ENODEV;
  525. if (!pci_set_dma_mask(dev, DMA_64BIT_MASK)
  526. && !pci_set_consistent_dma_mask(dev, DMA_64BIT_MASK))
  527. ;
  528. else if (!pci_set_dma_mask(dev, DMA_32BIT_MASK)
  529. && !pci_set_consistent_dma_mask(dev, DMA_32BIT_MASK))
  530. ;
  531. else {
  532. asd_printk("no suitable DMA mask for %s\n", pci_name(dev));
  533. goto Err_free;
  534. }
  535. pci_set_drvdata(dev, asd_ha);
  536. err = asd_map_ha(asd_ha);
  537. if (err)
  538. goto Err_free;
  539. err = asd_create_ha_caches(asd_ha);
  540. if (err)
  541. goto Err_unmap;
  542. err = asd_init_hw(asd_ha);
  543. if (err)
  544. goto Err_free_cache;
  545. asd_printk("device %s: SAS addr %llx, PCBA SN %s, %d phys, %d enabled "
  546. "phys, flash %s, BIOS %s%d\n",
  547. pci_name(dev), SAS_ADDR(asd_ha->hw_prof.sas_addr),
  548. asd_ha->hw_prof.pcba_sn, asd_ha->hw_prof.max_phys,
  549. asd_ha->hw_prof.num_phys,
  550. asd_ha->hw_prof.flash.present ? "present" : "not present",
  551. asd_ha->hw_prof.bios.present ? "build " : "not present",
  552. asd_ha->hw_prof.bios.bld);
  553. shost->can_queue = asd_ha->seq.can_queue;
  554. if (use_msi)
  555. pci_enable_msi(asd_ha->pcidev);
  556. err = request_irq(asd_ha->pcidev->irq, asd_hw_isr, IRQF_SHARED,
  557. ASD_DRIVER_NAME, asd_ha);
  558. if (err) {
  559. asd_printk("couldn't get irq %d for %s\n",
  560. asd_ha->pcidev->irq, pci_name(asd_ha->pcidev));
  561. goto Err_irq;
  562. }
  563. asd_enable_ints(asd_ha);
  564. err = asd_init_post_escbs(asd_ha);
  565. if (err) {
  566. asd_printk("couldn't post escbs for %s\n",
  567. pci_name(asd_ha->pcidev));
  568. goto Err_escbs;
  569. }
  570. ASD_DPRINTK("escbs posted\n");
  571. err = asd_create_dev_attrs(asd_ha);
  572. if (err)
  573. goto Err_dev_attrs;
  574. err = asd_register_sas_ha(asd_ha);
  575. if (err)
  576. goto Err_reg_sas;
  577. scsi_scan_host(shost);
  578. return 0;
  579. Err_reg_sas:
  580. asd_remove_dev_attrs(asd_ha);
  581. Err_dev_attrs:
  582. Err_escbs:
  583. asd_disable_ints(asd_ha);
  584. free_irq(dev->irq, asd_ha);
  585. Err_irq:
  586. if (use_msi)
  587. pci_disable_msi(dev);
  588. asd_chip_hardrst(asd_ha);
  589. Err_free_cache:
  590. asd_destroy_ha_caches(asd_ha);
  591. Err_unmap:
  592. asd_unmap_ha(asd_ha);
  593. Err_free:
  594. kfree(asd_ha);
  595. scsi_remove_host(shost);
  596. Err:
  597. pci_disable_device(dev);
  598. return err;
  599. }
  600. static void asd_free_queues(struct asd_ha_struct *asd_ha)
  601. {
  602. unsigned long flags;
  603. LIST_HEAD(pending);
  604. struct list_head *n, *pos;
  605. spin_lock_irqsave(&asd_ha->seq.pend_q_lock, flags);
  606. asd_ha->seq.pending = 0;
  607. list_splice_init(&asd_ha->seq.pend_q, &pending);
  608. spin_unlock_irqrestore(&asd_ha->seq.pend_q_lock, flags);
  609. if (!list_empty(&pending))
  610. ASD_DPRINTK("Uh-oh! Pending is not empty!\n");
  611. list_for_each_safe(pos, n, &pending) {
  612. struct asd_ascb *ascb = list_entry(pos, struct asd_ascb, list);
  613. /*
  614. * Delete unexpired ascb timers. This may happen if we issue
  615. * a CONTROL PHY scb to an adapter and rmmod before the scb
  616. * times out. Apparently we don't wait for the CONTROL PHY
  617. * to complete, so it doesn't matter if we kill the timer.
  618. */
  619. del_timer_sync(&ascb->timer);
  620. WARN_ON(ascb->scb->header.opcode != CONTROL_PHY);
  621. list_del_init(pos);
  622. ASD_DPRINTK("freeing from pending\n");
  623. asd_ascb_free(ascb);
  624. }
  625. }
  626. static void asd_turn_off_leds(struct asd_ha_struct *asd_ha)
  627. {
  628. u8 phy_mask = asd_ha->hw_prof.enabled_phys;
  629. u8 i;
  630. for_each_phy(phy_mask, phy_mask, i) {
  631. asd_turn_led(asd_ha, i, 0);
  632. asd_control_led(asd_ha, i, 0);
  633. }
  634. }
  635. static void __devexit asd_pci_remove(struct pci_dev *dev)
  636. {
  637. struct asd_ha_struct *asd_ha = pci_get_drvdata(dev);
  638. if (!asd_ha)
  639. return;
  640. asd_unregister_sas_ha(asd_ha);
  641. asd_disable_ints(asd_ha);
  642. asd_remove_dev_attrs(asd_ha);
  643. /* XXX more here as needed */
  644. free_irq(dev->irq, asd_ha);
  645. if (use_msi)
  646. pci_disable_msi(asd_ha->pcidev);
  647. asd_turn_off_leds(asd_ha);
  648. asd_chip_hardrst(asd_ha);
  649. asd_free_queues(asd_ha);
  650. asd_destroy_ha_caches(asd_ha);
  651. asd_unmap_ha(asd_ha);
  652. kfree(asd_ha);
  653. pci_disable_device(dev);
  654. return;
  655. }
  656. static void asd_scan_start(struct Scsi_Host *shost)
  657. {
  658. struct asd_ha_struct *asd_ha;
  659. int err;
  660. asd_ha = SHOST_TO_SAS_HA(shost)->lldd_ha;
  661. err = asd_enable_phys(asd_ha, asd_ha->hw_prof.enabled_phys);
  662. if (err)
  663. asd_printk("Couldn't enable phys, err:%d\n", err);
  664. }
  665. static int asd_scan_finished(struct Scsi_Host *shost, unsigned long time)
  666. {
  667. /* give the phy enabling interrupt event time to come in (1s
  668. * is empirically about all it takes) */
  669. if (time < HZ)
  670. return 0;
  671. /* Wait for discovery to finish */
  672. scsi_flush_work(shost);
  673. return 1;
  674. }
  675. static ssize_t asd_version_show(struct device_driver *driver, char *buf)
  676. {
  677. return snprintf(buf, PAGE_SIZE, "%s\n", ASD_DRIVER_VERSION);
  678. }
  679. static DRIVER_ATTR(version, S_IRUGO, asd_version_show, NULL);
  680. static int asd_create_driver_attrs(struct device_driver *driver)
  681. {
  682. return driver_create_file(driver, &driver_attr_version);
  683. }
  684. static void asd_remove_driver_attrs(struct device_driver *driver)
  685. {
  686. driver_remove_file(driver, &driver_attr_version);
  687. }
  688. static struct sas_domain_function_template aic94xx_transport_functions = {
  689. .lldd_dev_found = asd_dev_found,
  690. .lldd_dev_gone = asd_dev_gone,
  691. .lldd_execute_task = asd_execute_task,
  692. .lldd_abort_task = asd_abort_task,
  693. .lldd_abort_task_set = asd_abort_task_set,
  694. .lldd_clear_aca = asd_clear_aca,
  695. .lldd_clear_task_set = asd_clear_task_set,
  696. .lldd_I_T_nexus_reset = NULL,
  697. .lldd_lu_reset = asd_lu_reset,
  698. .lldd_query_task = asd_query_task,
  699. .lldd_clear_nexus_port = asd_clear_nexus_port,
  700. .lldd_clear_nexus_ha = asd_clear_nexus_ha,
  701. .lldd_control_phy = asd_control_phy,
  702. };
  703. static const struct pci_device_id aic94xx_pci_table[] __devinitdata = {
  704. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_RAZOR10),
  705. 0, 0, 1},
  706. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_RAZOR12),
  707. 0, 0, 1},
  708. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_RAZOR1E),
  709. 0, 0, 1},
  710. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_RAZOR1F),
  711. 0, 0, 1},
  712. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_RAZOR30),
  713. 0, 0, 2},
  714. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_RAZOR32),
  715. 0, 0, 2},
  716. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_RAZOR3E),
  717. 0, 0, 2},
  718. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_RAZOR3F),
  719. 0, 0, 2},
  720. {}
  721. };
  722. MODULE_DEVICE_TABLE(pci, aic94xx_pci_table);
  723. static struct pci_driver aic94xx_pci_driver = {
  724. .name = ASD_DRIVER_NAME,
  725. .id_table = aic94xx_pci_table,
  726. .probe = asd_pci_probe,
  727. .remove = __devexit_p(asd_pci_remove),
  728. };
  729. static int __init aic94xx_init(void)
  730. {
  731. int err;
  732. asd_printk("%s version %s loaded\n", ASD_DRIVER_DESCRIPTION,
  733. ASD_DRIVER_VERSION);
  734. err = asd_create_global_caches();
  735. if (err)
  736. return err;
  737. aic94xx_transport_template =
  738. sas_domain_attach_transport(&aic94xx_transport_functions);
  739. if (!aic94xx_transport_template)
  740. goto out_destroy_caches;
  741. err = pci_register_driver(&aic94xx_pci_driver);
  742. if (err)
  743. goto out_release_transport;
  744. err = asd_create_driver_attrs(&aic94xx_pci_driver.driver);
  745. if (err)
  746. goto out_unregister_pcidrv;
  747. return err;
  748. out_unregister_pcidrv:
  749. pci_unregister_driver(&aic94xx_pci_driver);
  750. out_release_transport:
  751. sas_release_transport(aic94xx_transport_template);
  752. out_destroy_caches:
  753. asd_destroy_global_caches();
  754. return err;
  755. }
  756. static void __exit aic94xx_exit(void)
  757. {
  758. asd_remove_driver_attrs(&aic94xx_pci_driver.driver);
  759. pci_unregister_driver(&aic94xx_pci_driver);
  760. sas_release_transport(aic94xx_transport_template);
  761. asd_release_firmware();
  762. asd_destroy_global_caches();
  763. asd_printk("%s version %s unloaded\n", ASD_DRIVER_DESCRIPTION,
  764. ASD_DRIVER_VERSION);
  765. }
  766. module_init(aic94xx_init);
  767. module_exit(aic94xx_exit);
  768. MODULE_AUTHOR("Luben Tuikov <luben_tuikov@adaptec.com>");
  769. MODULE_DESCRIPTION(ASD_DRIVER_DESCRIPTION);
  770. MODULE_LICENSE("GPL v2");
  771. MODULE_VERSION(ASD_DRIVER_VERSION);