mv_init.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. /*
  2. * Marvell 88SE64xx/88SE94xx pci init
  3. *
  4. * Copyright 2007 Red Hat, Inc.
  5. * Copyright 2008 Marvell. <kewei@marvell.com>
  6. * Copyright 2009-2011 Marvell. <yuxiangl@marvell.com>
  7. *
  8. * This file is licensed under GPLv2.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; version 2 of the
  13. * License.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  23. * USA
  24. */
  25. #include "mv_sas.h"
  26. int interrupt_coalescing = 0x80;
  27. static struct scsi_transport_template *mvs_stt;
  28. static const struct mvs_chip_info mvs_chips[] = {
  29. [chip_6320] = { 1, 2, 0x400, 17, 16, 6, 9, &mvs_64xx_dispatch, },
  30. [chip_6440] = { 1, 4, 0x400, 17, 16, 6, 9, &mvs_64xx_dispatch, },
  31. [chip_6485] = { 1, 8, 0x800, 33, 32, 6, 10, &mvs_64xx_dispatch, },
  32. [chip_9180] = { 2, 4, 0x800, 17, 64, 8, 9, &mvs_94xx_dispatch, },
  33. [chip_9480] = { 2, 4, 0x800, 17, 64, 8, 9, &mvs_94xx_dispatch, },
  34. [chip_9445] = { 1, 4, 0x800, 17, 64, 8, 11, &mvs_94xx_dispatch, },
  35. [chip_9485] = { 2, 4, 0x800, 17, 64, 8, 11, &mvs_94xx_dispatch, },
  36. [chip_1300] = { 1, 4, 0x400, 17, 16, 6, 9, &mvs_64xx_dispatch, },
  37. [chip_1320] = { 2, 4, 0x800, 17, 64, 8, 9, &mvs_94xx_dispatch, },
  38. };
  39. struct device_attribute *mvst_host_attrs[];
  40. #define SOC_SAS_NUM 2
  41. static struct scsi_host_template mvs_sht = {
  42. .module = THIS_MODULE,
  43. .name = DRV_NAME,
  44. .queuecommand = sas_queuecommand,
  45. .target_alloc = sas_target_alloc,
  46. .slave_configure = sas_slave_configure,
  47. .scan_finished = mvs_scan_finished,
  48. .scan_start = mvs_scan_start,
  49. .change_queue_depth = sas_change_queue_depth,
  50. .bios_param = sas_bios_param,
  51. .can_queue = 1,
  52. .this_id = -1,
  53. .sg_tablesize = SG_ALL,
  54. .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
  55. .use_clustering = ENABLE_CLUSTERING,
  56. .eh_device_reset_handler = sas_eh_device_reset_handler,
  57. .eh_bus_reset_handler = sas_eh_bus_reset_handler,
  58. .target_destroy = sas_target_destroy,
  59. .ioctl = sas_ioctl,
  60. .shost_attrs = mvst_host_attrs,
  61. .track_queue_depth = 1,
  62. };
  63. static struct sas_domain_function_template mvs_transport_ops = {
  64. .lldd_dev_found = mvs_dev_found,
  65. .lldd_dev_gone = mvs_dev_gone,
  66. .lldd_execute_task = mvs_queue_command,
  67. .lldd_control_phy = mvs_phy_control,
  68. .lldd_abort_task = mvs_abort_task,
  69. .lldd_abort_task_set = mvs_abort_task_set,
  70. .lldd_clear_aca = mvs_clear_aca,
  71. .lldd_clear_task_set = mvs_clear_task_set,
  72. .lldd_I_T_nexus_reset = mvs_I_T_nexus_reset,
  73. .lldd_lu_reset = mvs_lu_reset,
  74. .lldd_query_task = mvs_query_task,
  75. .lldd_port_formed = mvs_port_formed,
  76. .lldd_port_deformed = mvs_port_deformed,
  77. .lldd_write_gpio = mvs_gpio_write,
  78. };
  79. static void mvs_phy_init(struct mvs_info *mvi, int phy_id)
  80. {
  81. struct mvs_phy *phy = &mvi->phy[phy_id];
  82. struct asd_sas_phy *sas_phy = &phy->sas_phy;
  83. phy->mvi = mvi;
  84. phy->port = NULL;
  85. init_timer(&phy->timer);
  86. sas_phy->enabled = (phy_id < mvi->chip->n_phy) ? 1 : 0;
  87. sas_phy->class = SAS;
  88. sas_phy->iproto = SAS_PROTOCOL_ALL;
  89. sas_phy->tproto = 0;
  90. sas_phy->type = PHY_TYPE_PHYSICAL;
  91. sas_phy->role = PHY_ROLE_INITIATOR;
  92. sas_phy->oob_mode = OOB_NOT_CONNECTED;
  93. sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
  94. sas_phy->id = phy_id;
  95. sas_phy->sas_addr = &mvi->sas_addr[0];
  96. sas_phy->frame_rcvd = &phy->frame_rcvd[0];
  97. sas_phy->ha = (struct sas_ha_struct *)mvi->shost->hostdata;
  98. sas_phy->lldd_phy = phy;
  99. }
  100. static void mvs_free(struct mvs_info *mvi)
  101. {
  102. struct mvs_wq *mwq;
  103. int slot_nr;
  104. if (!mvi)
  105. return;
  106. if (mvi->flags & MVF_FLAG_SOC)
  107. slot_nr = MVS_SOC_SLOTS;
  108. else
  109. slot_nr = MVS_CHIP_SLOT_SZ;
  110. if (mvi->dma_pool)
  111. pci_pool_destroy(mvi->dma_pool);
  112. if (mvi->tx)
  113. dma_free_coherent(mvi->dev,
  114. sizeof(*mvi->tx) * MVS_CHIP_SLOT_SZ,
  115. mvi->tx, mvi->tx_dma);
  116. if (mvi->rx_fis)
  117. dma_free_coherent(mvi->dev, MVS_RX_FISL_SZ,
  118. mvi->rx_fis, mvi->rx_fis_dma);
  119. if (mvi->rx)
  120. dma_free_coherent(mvi->dev,
  121. sizeof(*mvi->rx) * (MVS_RX_RING_SZ + 1),
  122. mvi->rx, mvi->rx_dma);
  123. if (mvi->slot)
  124. dma_free_coherent(mvi->dev,
  125. sizeof(*mvi->slot) * slot_nr,
  126. mvi->slot, mvi->slot_dma);
  127. if (mvi->bulk_buffer)
  128. dma_free_coherent(mvi->dev, TRASH_BUCKET_SIZE,
  129. mvi->bulk_buffer, mvi->bulk_buffer_dma);
  130. if (mvi->bulk_buffer1)
  131. dma_free_coherent(mvi->dev, TRASH_BUCKET_SIZE,
  132. mvi->bulk_buffer1, mvi->bulk_buffer_dma1);
  133. MVS_CHIP_DISP->chip_iounmap(mvi);
  134. if (mvi->shost)
  135. scsi_host_put(mvi->shost);
  136. list_for_each_entry(mwq, &mvi->wq_list, entry)
  137. cancel_delayed_work(&mwq->work_q);
  138. kfree(mvi->tags);
  139. kfree(mvi);
  140. }
  141. #ifdef CONFIG_SCSI_MVSAS_TASKLET
  142. static void mvs_tasklet(unsigned long opaque)
  143. {
  144. u32 stat;
  145. u16 core_nr, i = 0;
  146. struct mvs_info *mvi;
  147. struct sas_ha_struct *sha = (struct sas_ha_struct *)opaque;
  148. core_nr = ((struct mvs_prv_info *)sha->lldd_ha)->n_host;
  149. mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[0];
  150. if (unlikely(!mvi))
  151. BUG_ON(1);
  152. stat = MVS_CHIP_DISP->isr_status(mvi, mvi->pdev->irq);
  153. if (!stat)
  154. goto out;
  155. for (i = 0; i < core_nr; i++) {
  156. mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[i];
  157. MVS_CHIP_DISP->isr(mvi, mvi->pdev->irq, stat);
  158. }
  159. out:
  160. MVS_CHIP_DISP->interrupt_enable(mvi);
  161. }
  162. #endif
  163. static irqreturn_t mvs_interrupt(int irq, void *opaque)
  164. {
  165. u32 core_nr;
  166. u32 stat;
  167. struct mvs_info *mvi;
  168. struct sas_ha_struct *sha = opaque;
  169. #ifndef CONFIG_SCSI_MVSAS_TASKLET
  170. u32 i;
  171. #endif
  172. core_nr = ((struct mvs_prv_info *)sha->lldd_ha)->n_host;
  173. mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[0];
  174. if (unlikely(!mvi))
  175. return IRQ_NONE;
  176. #ifdef CONFIG_SCSI_MVSAS_TASKLET
  177. MVS_CHIP_DISP->interrupt_disable(mvi);
  178. #endif
  179. stat = MVS_CHIP_DISP->isr_status(mvi, irq);
  180. if (!stat) {
  181. #ifdef CONFIG_SCSI_MVSAS_TASKLET
  182. MVS_CHIP_DISP->interrupt_enable(mvi);
  183. #endif
  184. return IRQ_NONE;
  185. }
  186. #ifdef CONFIG_SCSI_MVSAS_TASKLET
  187. tasklet_schedule(&((struct mvs_prv_info *)sha->lldd_ha)->mv_tasklet);
  188. #else
  189. for (i = 0; i < core_nr; i++) {
  190. mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[i];
  191. MVS_CHIP_DISP->isr(mvi, irq, stat);
  192. }
  193. #endif
  194. return IRQ_HANDLED;
  195. }
  196. static int mvs_alloc(struct mvs_info *mvi, struct Scsi_Host *shost)
  197. {
  198. int i = 0, slot_nr;
  199. char pool_name[32];
  200. if (mvi->flags & MVF_FLAG_SOC)
  201. slot_nr = MVS_SOC_SLOTS;
  202. else
  203. slot_nr = MVS_CHIP_SLOT_SZ;
  204. spin_lock_init(&mvi->lock);
  205. for (i = 0; i < mvi->chip->n_phy; i++) {
  206. mvs_phy_init(mvi, i);
  207. mvi->port[i].wide_port_phymap = 0;
  208. mvi->port[i].port_attached = 0;
  209. INIT_LIST_HEAD(&mvi->port[i].list);
  210. }
  211. for (i = 0; i < MVS_MAX_DEVICES; i++) {
  212. mvi->devices[i].taskfileset = MVS_ID_NOT_MAPPED;
  213. mvi->devices[i].dev_type = SAS_PHY_UNUSED;
  214. mvi->devices[i].device_id = i;
  215. mvi->devices[i].dev_status = MVS_DEV_NORMAL;
  216. init_timer(&mvi->devices[i].timer);
  217. }
  218. /*
  219. * alloc and init our DMA areas
  220. */
  221. mvi->tx = dma_alloc_coherent(mvi->dev,
  222. sizeof(*mvi->tx) * MVS_CHIP_SLOT_SZ,
  223. &mvi->tx_dma, GFP_KERNEL);
  224. if (!mvi->tx)
  225. goto err_out;
  226. memset(mvi->tx, 0, sizeof(*mvi->tx) * MVS_CHIP_SLOT_SZ);
  227. mvi->rx_fis = dma_alloc_coherent(mvi->dev, MVS_RX_FISL_SZ,
  228. &mvi->rx_fis_dma, GFP_KERNEL);
  229. if (!mvi->rx_fis)
  230. goto err_out;
  231. memset(mvi->rx_fis, 0, MVS_RX_FISL_SZ);
  232. mvi->rx = dma_alloc_coherent(mvi->dev,
  233. sizeof(*mvi->rx) * (MVS_RX_RING_SZ + 1),
  234. &mvi->rx_dma, GFP_KERNEL);
  235. if (!mvi->rx)
  236. goto err_out;
  237. memset(mvi->rx, 0, sizeof(*mvi->rx) * (MVS_RX_RING_SZ + 1));
  238. mvi->rx[0] = cpu_to_le32(0xfff);
  239. mvi->rx_cons = 0xfff;
  240. mvi->slot = dma_alloc_coherent(mvi->dev,
  241. sizeof(*mvi->slot) * slot_nr,
  242. &mvi->slot_dma, GFP_KERNEL);
  243. if (!mvi->slot)
  244. goto err_out;
  245. memset(mvi->slot, 0, sizeof(*mvi->slot) * slot_nr);
  246. mvi->bulk_buffer = dma_alloc_coherent(mvi->dev,
  247. TRASH_BUCKET_SIZE,
  248. &mvi->bulk_buffer_dma, GFP_KERNEL);
  249. if (!mvi->bulk_buffer)
  250. goto err_out;
  251. mvi->bulk_buffer1 = dma_alloc_coherent(mvi->dev,
  252. TRASH_BUCKET_SIZE,
  253. &mvi->bulk_buffer_dma1, GFP_KERNEL);
  254. if (!mvi->bulk_buffer1)
  255. goto err_out;
  256. sprintf(pool_name, "%s%d", "mvs_dma_pool", mvi->id);
  257. mvi->dma_pool = pci_pool_create(pool_name, mvi->pdev, MVS_SLOT_BUF_SZ, 16, 0);
  258. if (!mvi->dma_pool) {
  259. printk(KERN_DEBUG "failed to create dma pool %s.\n", pool_name);
  260. goto err_out;
  261. }
  262. mvi->tags_num = slot_nr;
  263. /* Initialize tags */
  264. mvs_tag_init(mvi);
  265. return 0;
  266. err_out:
  267. return 1;
  268. }
  269. int mvs_ioremap(struct mvs_info *mvi, int bar, int bar_ex)
  270. {
  271. unsigned long res_start, res_len, res_flag, res_flag_ex = 0;
  272. struct pci_dev *pdev = mvi->pdev;
  273. if (bar_ex != -1) {
  274. /*
  275. * ioremap main and peripheral registers
  276. */
  277. res_start = pci_resource_start(pdev, bar_ex);
  278. res_len = pci_resource_len(pdev, bar_ex);
  279. if (!res_start || !res_len)
  280. goto err_out;
  281. res_flag_ex = pci_resource_flags(pdev, bar_ex);
  282. if (res_flag_ex & IORESOURCE_MEM)
  283. mvi->regs_ex = ioremap(res_start, res_len);
  284. else
  285. mvi->regs_ex = (void *)res_start;
  286. if (!mvi->regs_ex)
  287. goto err_out;
  288. }
  289. res_start = pci_resource_start(pdev, bar);
  290. res_len = pci_resource_len(pdev, bar);
  291. if (!res_start || !res_len) {
  292. iounmap(mvi->regs_ex);
  293. mvi->regs_ex = NULL;
  294. goto err_out;
  295. }
  296. res_flag = pci_resource_flags(pdev, bar);
  297. mvi->regs = ioremap(res_start, res_len);
  298. if (!mvi->regs) {
  299. if (mvi->regs_ex && (res_flag_ex & IORESOURCE_MEM))
  300. iounmap(mvi->regs_ex);
  301. mvi->regs_ex = NULL;
  302. goto err_out;
  303. }
  304. return 0;
  305. err_out:
  306. return -1;
  307. }
  308. void mvs_iounmap(void __iomem *regs)
  309. {
  310. iounmap(regs);
  311. }
  312. static struct mvs_info *mvs_pci_alloc(struct pci_dev *pdev,
  313. const struct pci_device_id *ent,
  314. struct Scsi_Host *shost, unsigned int id)
  315. {
  316. struct mvs_info *mvi = NULL;
  317. struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
  318. mvi = kzalloc(sizeof(*mvi) +
  319. (1L << mvs_chips[ent->driver_data].slot_width) *
  320. sizeof(struct mvs_slot_info), GFP_KERNEL);
  321. if (!mvi)
  322. return NULL;
  323. mvi->pdev = pdev;
  324. mvi->dev = &pdev->dev;
  325. mvi->chip_id = ent->driver_data;
  326. mvi->chip = &mvs_chips[mvi->chip_id];
  327. INIT_LIST_HEAD(&mvi->wq_list);
  328. ((struct mvs_prv_info *)sha->lldd_ha)->mvi[id] = mvi;
  329. ((struct mvs_prv_info *)sha->lldd_ha)->n_phy = mvi->chip->n_phy;
  330. mvi->id = id;
  331. mvi->sas = sha;
  332. mvi->shost = shost;
  333. mvi->tags = kzalloc(MVS_CHIP_SLOT_SZ>>3, GFP_KERNEL);
  334. if (!mvi->tags)
  335. goto err_out;
  336. if (MVS_CHIP_DISP->chip_ioremap(mvi))
  337. goto err_out;
  338. if (!mvs_alloc(mvi, shost))
  339. return mvi;
  340. err_out:
  341. mvs_free(mvi);
  342. return NULL;
  343. }
  344. static int pci_go_64(struct pci_dev *pdev)
  345. {
  346. int rc;
  347. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  348. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  349. if (rc) {
  350. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  351. if (rc) {
  352. dev_printk(KERN_ERR, &pdev->dev,
  353. "64-bit DMA enable failed\n");
  354. return rc;
  355. }
  356. }
  357. } else {
  358. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  359. if (rc) {
  360. dev_printk(KERN_ERR, &pdev->dev,
  361. "32-bit DMA enable failed\n");
  362. return rc;
  363. }
  364. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  365. if (rc) {
  366. dev_printk(KERN_ERR, &pdev->dev,
  367. "32-bit consistent DMA enable failed\n");
  368. return rc;
  369. }
  370. }
  371. return rc;
  372. }
  373. static int mvs_prep_sas_ha_init(struct Scsi_Host *shost,
  374. const struct mvs_chip_info *chip_info)
  375. {
  376. int phy_nr, port_nr; unsigned short core_nr;
  377. struct asd_sas_phy **arr_phy;
  378. struct asd_sas_port **arr_port;
  379. struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
  380. core_nr = chip_info->n_host;
  381. phy_nr = core_nr * chip_info->n_phy;
  382. port_nr = phy_nr;
  383. memset(sha, 0x00, sizeof(struct sas_ha_struct));
  384. arr_phy = kcalloc(phy_nr, sizeof(void *), GFP_KERNEL);
  385. arr_port = kcalloc(port_nr, sizeof(void *), GFP_KERNEL);
  386. if (!arr_phy || !arr_port)
  387. goto exit_free;
  388. sha->sas_phy = arr_phy;
  389. sha->sas_port = arr_port;
  390. sha->core.shost = shost;
  391. sha->lldd_ha = kzalloc(sizeof(struct mvs_prv_info), GFP_KERNEL);
  392. if (!sha->lldd_ha)
  393. goto exit_free;
  394. ((struct mvs_prv_info *)sha->lldd_ha)->n_host = core_nr;
  395. shost->transportt = mvs_stt;
  396. shost->max_id = MVS_MAX_DEVICES;
  397. shost->max_lun = ~0;
  398. shost->max_channel = 1;
  399. shost->max_cmd_len = 16;
  400. return 0;
  401. exit_free:
  402. kfree(arr_phy);
  403. kfree(arr_port);
  404. return -1;
  405. }
  406. static void mvs_post_sas_ha_init(struct Scsi_Host *shost,
  407. const struct mvs_chip_info *chip_info)
  408. {
  409. int can_queue, i = 0, j = 0;
  410. struct mvs_info *mvi = NULL;
  411. struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
  412. unsigned short nr_core = ((struct mvs_prv_info *)sha->lldd_ha)->n_host;
  413. for (j = 0; j < nr_core; j++) {
  414. mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[j];
  415. for (i = 0; i < chip_info->n_phy; i++) {
  416. sha->sas_phy[j * chip_info->n_phy + i] =
  417. &mvi->phy[i].sas_phy;
  418. sha->sas_port[j * chip_info->n_phy + i] =
  419. &mvi->port[i].sas_port;
  420. }
  421. }
  422. sha->sas_ha_name = DRV_NAME;
  423. sha->dev = mvi->dev;
  424. sha->lldd_module = THIS_MODULE;
  425. sha->sas_addr = &mvi->sas_addr[0];
  426. sha->num_phys = nr_core * chip_info->n_phy;
  427. if (mvi->flags & MVF_FLAG_SOC)
  428. can_queue = MVS_SOC_CAN_QUEUE;
  429. else
  430. can_queue = MVS_CHIP_SLOT_SZ;
  431. shost->sg_tablesize = min_t(u16, SG_ALL, MVS_MAX_SG);
  432. shost->can_queue = can_queue;
  433. mvi->shost->cmd_per_lun = MVS_QUEUE_SIZE;
  434. sha->core.shost = mvi->shost;
  435. }
  436. static void mvs_init_sas_add(struct mvs_info *mvi)
  437. {
  438. u8 i;
  439. for (i = 0; i < mvi->chip->n_phy; i++) {
  440. mvi->phy[i].dev_sas_addr = 0x5005043011ab0000ULL;
  441. mvi->phy[i].dev_sas_addr =
  442. cpu_to_be64((u64)(*(u64 *)&mvi->phy[i].dev_sas_addr));
  443. }
  444. memcpy(mvi->sas_addr, &mvi->phy[0].dev_sas_addr, SAS_ADDR_SIZE);
  445. }
  446. static int mvs_pci_init(struct pci_dev *pdev, const struct pci_device_id *ent)
  447. {
  448. unsigned int rc, nhost = 0;
  449. struct mvs_info *mvi;
  450. struct mvs_prv_info *mpi;
  451. irq_handler_t irq_handler = mvs_interrupt;
  452. struct Scsi_Host *shost = NULL;
  453. const struct mvs_chip_info *chip;
  454. dev_printk(KERN_INFO, &pdev->dev,
  455. "mvsas: driver version %s\n", DRV_VERSION);
  456. rc = pci_enable_device(pdev);
  457. if (rc)
  458. goto err_out_enable;
  459. pci_set_master(pdev);
  460. rc = pci_request_regions(pdev, DRV_NAME);
  461. if (rc)
  462. goto err_out_disable;
  463. rc = pci_go_64(pdev);
  464. if (rc)
  465. goto err_out_regions;
  466. shost = scsi_host_alloc(&mvs_sht, sizeof(void *));
  467. if (!shost) {
  468. rc = -ENOMEM;
  469. goto err_out_regions;
  470. }
  471. chip = &mvs_chips[ent->driver_data];
  472. SHOST_TO_SAS_HA(shost) =
  473. kcalloc(1, sizeof(struct sas_ha_struct), GFP_KERNEL);
  474. if (!SHOST_TO_SAS_HA(shost)) {
  475. kfree(shost);
  476. rc = -ENOMEM;
  477. goto err_out_regions;
  478. }
  479. rc = mvs_prep_sas_ha_init(shost, chip);
  480. if (rc) {
  481. kfree(shost);
  482. rc = -ENOMEM;
  483. goto err_out_regions;
  484. }
  485. pci_set_drvdata(pdev, SHOST_TO_SAS_HA(shost));
  486. do {
  487. mvi = mvs_pci_alloc(pdev, ent, shost, nhost);
  488. if (!mvi) {
  489. rc = -ENOMEM;
  490. goto err_out_regions;
  491. }
  492. memset(&mvi->hba_info_param, 0xFF,
  493. sizeof(struct hba_info_page));
  494. mvs_init_sas_add(mvi);
  495. mvi->instance = nhost;
  496. rc = MVS_CHIP_DISP->chip_init(mvi);
  497. if (rc) {
  498. mvs_free(mvi);
  499. goto err_out_regions;
  500. }
  501. nhost++;
  502. } while (nhost < chip->n_host);
  503. mpi = (struct mvs_prv_info *)(SHOST_TO_SAS_HA(shost)->lldd_ha);
  504. #ifdef CONFIG_SCSI_MVSAS_TASKLET
  505. tasklet_init(&(mpi->mv_tasklet), mvs_tasklet,
  506. (unsigned long)SHOST_TO_SAS_HA(shost));
  507. #endif
  508. mvs_post_sas_ha_init(shost, chip);
  509. rc = scsi_add_host(shost, &pdev->dev);
  510. if (rc)
  511. goto err_out_shost;
  512. rc = sas_register_ha(SHOST_TO_SAS_HA(shost));
  513. if (rc)
  514. goto err_out_shost;
  515. rc = request_irq(pdev->irq, irq_handler, IRQF_SHARED,
  516. DRV_NAME, SHOST_TO_SAS_HA(shost));
  517. if (rc)
  518. goto err_not_sas;
  519. MVS_CHIP_DISP->interrupt_enable(mvi);
  520. scsi_scan_host(mvi->shost);
  521. return 0;
  522. err_not_sas:
  523. sas_unregister_ha(SHOST_TO_SAS_HA(shost));
  524. err_out_shost:
  525. scsi_remove_host(mvi->shost);
  526. err_out_regions:
  527. pci_release_regions(pdev);
  528. err_out_disable:
  529. pci_disable_device(pdev);
  530. err_out_enable:
  531. return rc;
  532. }
  533. static void mvs_pci_remove(struct pci_dev *pdev)
  534. {
  535. unsigned short core_nr, i = 0;
  536. struct sas_ha_struct *sha = pci_get_drvdata(pdev);
  537. struct mvs_info *mvi = NULL;
  538. core_nr = ((struct mvs_prv_info *)sha->lldd_ha)->n_host;
  539. mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[0];
  540. #ifdef CONFIG_SCSI_MVSAS_TASKLET
  541. tasklet_kill(&((struct mvs_prv_info *)sha->lldd_ha)->mv_tasklet);
  542. #endif
  543. scsi_remove_host(mvi->shost);
  544. sas_unregister_ha(sha);
  545. sas_remove_host(mvi->shost);
  546. MVS_CHIP_DISP->interrupt_disable(mvi);
  547. free_irq(mvi->pdev->irq, sha);
  548. for (i = 0; i < core_nr; i++) {
  549. mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[i];
  550. mvs_free(mvi);
  551. }
  552. kfree(sha->sas_phy);
  553. kfree(sha->sas_port);
  554. kfree(sha);
  555. pci_release_regions(pdev);
  556. pci_disable_device(pdev);
  557. return;
  558. }
  559. static struct pci_device_id mvs_pci_table[] = {
  560. { PCI_VDEVICE(MARVELL, 0x6320), chip_6320 },
  561. { PCI_VDEVICE(MARVELL, 0x6340), chip_6440 },
  562. {
  563. .vendor = PCI_VENDOR_ID_MARVELL,
  564. .device = 0x6440,
  565. .subvendor = PCI_ANY_ID,
  566. .subdevice = 0x6480,
  567. .class = 0,
  568. .class_mask = 0,
  569. .driver_data = chip_6485,
  570. },
  571. { PCI_VDEVICE(MARVELL, 0x6440), chip_6440 },
  572. { PCI_VDEVICE(MARVELL, 0x6485), chip_6485 },
  573. { PCI_VDEVICE(MARVELL, 0x9480), chip_9480 },
  574. { PCI_VDEVICE(MARVELL, 0x9180), chip_9180 },
  575. { PCI_VDEVICE(ARECA, PCI_DEVICE_ID_ARECA_1300), chip_1300 },
  576. { PCI_VDEVICE(ARECA, PCI_DEVICE_ID_ARECA_1320), chip_1320 },
  577. { PCI_VDEVICE(ADAPTEC2, 0x0450), chip_6440 },
  578. { PCI_VDEVICE(TTI, 0x2710), chip_9480 },
  579. { PCI_VDEVICE(TTI, 0x2720), chip_9480 },
  580. { PCI_VDEVICE(TTI, 0x2721), chip_9480 },
  581. { PCI_VDEVICE(TTI, 0x2722), chip_9480 },
  582. { PCI_VDEVICE(TTI, 0x2740), chip_9480 },
  583. { PCI_VDEVICE(TTI, 0x2744), chip_9480 },
  584. { PCI_VDEVICE(TTI, 0x2760), chip_9480 },
  585. {
  586. .vendor = PCI_VENDOR_ID_MARVELL_EXT,
  587. .device = 0x9480,
  588. .subvendor = PCI_ANY_ID,
  589. .subdevice = 0x9480,
  590. .class = 0,
  591. .class_mask = 0,
  592. .driver_data = chip_9480,
  593. },
  594. {
  595. .vendor = PCI_VENDOR_ID_MARVELL_EXT,
  596. .device = 0x9445,
  597. .subvendor = PCI_ANY_ID,
  598. .subdevice = 0x9480,
  599. .class = 0,
  600. .class_mask = 0,
  601. .driver_data = chip_9445,
  602. },
  603. { PCI_VDEVICE(MARVELL_EXT, 0x9485), chip_9485 }, /* Marvell 9480/9485 (any vendor/model) */
  604. { PCI_VDEVICE(OCZ, 0x1021), chip_9485}, /* OCZ RevoDrive3 */
  605. { PCI_VDEVICE(OCZ, 0x1022), chip_9485}, /* OCZ RevoDrive3/zDriveR4 (exact model unknown) */
  606. { PCI_VDEVICE(OCZ, 0x1040), chip_9485}, /* OCZ RevoDrive3/zDriveR4 (exact model unknown) */
  607. { PCI_VDEVICE(OCZ, 0x1041), chip_9485}, /* OCZ RevoDrive3/zDriveR4 (exact model unknown) */
  608. { PCI_VDEVICE(OCZ, 0x1042), chip_9485}, /* OCZ RevoDrive3/zDriveR4 (exact model unknown) */
  609. { PCI_VDEVICE(OCZ, 0x1043), chip_9485}, /* OCZ RevoDrive3/zDriveR4 (exact model unknown) */
  610. { PCI_VDEVICE(OCZ, 0x1044), chip_9485}, /* OCZ RevoDrive3/zDriveR4 (exact model unknown) */
  611. { PCI_VDEVICE(OCZ, 0x1080), chip_9485}, /* OCZ RevoDrive3/zDriveR4 (exact model unknown) */
  612. { PCI_VDEVICE(OCZ, 0x1083), chip_9485}, /* OCZ RevoDrive3/zDriveR4 (exact model unknown) */
  613. { PCI_VDEVICE(OCZ, 0x1084), chip_9485}, /* OCZ RevoDrive3/zDriveR4 (exact model unknown) */
  614. { } /* terminate list */
  615. };
  616. static struct pci_driver mvs_pci_driver = {
  617. .name = DRV_NAME,
  618. .id_table = mvs_pci_table,
  619. .probe = mvs_pci_init,
  620. .remove = mvs_pci_remove,
  621. };
  622. static ssize_t
  623. mvs_show_driver_version(struct device *cdev,
  624. struct device_attribute *attr, char *buffer)
  625. {
  626. return snprintf(buffer, PAGE_SIZE, "%s\n", DRV_VERSION);
  627. }
  628. static DEVICE_ATTR(driver_version,
  629. S_IRUGO,
  630. mvs_show_driver_version,
  631. NULL);
  632. static ssize_t
  633. mvs_store_interrupt_coalescing(struct device *cdev,
  634. struct device_attribute *attr,
  635. const char *buffer, size_t size)
  636. {
  637. unsigned int val = 0;
  638. struct mvs_info *mvi = NULL;
  639. struct Scsi_Host *shost = class_to_shost(cdev);
  640. struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
  641. u8 i, core_nr;
  642. if (buffer == NULL)
  643. return size;
  644. if (sscanf(buffer, "%u", &val) != 1)
  645. return -EINVAL;
  646. if (val >= 0x10000) {
  647. mv_dprintk("interrupt coalescing timer %d us is"
  648. "too long\n", val);
  649. return strlen(buffer);
  650. }
  651. interrupt_coalescing = val;
  652. core_nr = ((struct mvs_prv_info *)sha->lldd_ha)->n_host;
  653. mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[0];
  654. if (unlikely(!mvi))
  655. return -EINVAL;
  656. for (i = 0; i < core_nr; i++) {
  657. mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[i];
  658. if (MVS_CHIP_DISP->tune_interrupt)
  659. MVS_CHIP_DISP->tune_interrupt(mvi,
  660. interrupt_coalescing);
  661. }
  662. mv_dprintk("set interrupt coalescing time to %d us\n",
  663. interrupt_coalescing);
  664. return strlen(buffer);
  665. }
  666. static ssize_t mvs_show_interrupt_coalescing(struct device *cdev,
  667. struct device_attribute *attr, char *buffer)
  668. {
  669. return snprintf(buffer, PAGE_SIZE, "%d\n", interrupt_coalescing);
  670. }
  671. static DEVICE_ATTR(interrupt_coalescing,
  672. S_IRUGO|S_IWUSR,
  673. mvs_show_interrupt_coalescing,
  674. mvs_store_interrupt_coalescing);
  675. /* task handler */
  676. struct task_struct *mvs_th;
  677. static int __init mvs_init(void)
  678. {
  679. int rc;
  680. mvs_stt = sas_domain_attach_transport(&mvs_transport_ops);
  681. if (!mvs_stt)
  682. return -ENOMEM;
  683. rc = pci_register_driver(&mvs_pci_driver);
  684. if (rc)
  685. goto err_out;
  686. return 0;
  687. err_out:
  688. sas_release_transport(mvs_stt);
  689. return rc;
  690. }
  691. static void __exit mvs_exit(void)
  692. {
  693. pci_unregister_driver(&mvs_pci_driver);
  694. sas_release_transport(mvs_stt);
  695. }
  696. struct device_attribute *mvst_host_attrs[] = {
  697. &dev_attr_driver_version,
  698. &dev_attr_interrupt_coalescing,
  699. NULL,
  700. };
  701. module_init(mvs_init);
  702. module_exit(mvs_exit);
  703. MODULE_AUTHOR("Jeff Garzik <jgarzik@pobox.com>");
  704. MODULE_DESCRIPTION("Marvell 88SE6440 SAS/SATA controller driver");
  705. MODULE_VERSION(DRV_VERSION);
  706. MODULE_LICENSE("GPL");
  707. #ifdef CONFIG_PCI
  708. MODULE_DEVICE_TABLE(pci, mvs_pci_table);
  709. #endif