pm8001_init.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. /*
  2. * PMC-Sierra SPC 8001 SAS/SATA based host adapters driver
  3. *
  4. * Copyright (c) 2008-2009 USI Co., Ltd.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions, and the following disclaimer,
  12. * without modification.
  13. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  14. * substantially similar to the "NO WARRANTY" disclaimer below
  15. * ("Disclaimer") and any redistribution must be conditioned upon
  16. * including a substantially similar Disclaimer requirement for further
  17. * binary redistribution.
  18. * 3. Neither the names of the above-listed copyright holders nor the names
  19. * of any contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * Alternatively, this software may be distributed under the terms of the
  23. * GNU General Public License ("GPL") version 2 as published by the Free
  24. * Software Foundation.
  25. *
  26. * NO WARRANTY
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  30. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  33. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  35. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  36. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGES.
  38. *
  39. */
  40. #include "pm8001_sas.h"
  41. #include "pm8001_chips.h"
  42. static struct scsi_transport_template *pm8001_stt;
  43. static const struct pm8001_chip_info pm8001_chips[] = {
  44. [chip_8001] = { 8, &pm8001_8001_dispatch,},
  45. };
  46. static int pm8001_id;
  47. LIST_HEAD(hba_list);
  48. /**
  49. * The main structure which LLDD must register for scsi core.
  50. */
  51. static struct scsi_host_template pm8001_sht = {
  52. .module = THIS_MODULE,
  53. .name = DRV_NAME,
  54. .queuecommand = sas_queuecommand,
  55. .target_alloc = sas_target_alloc,
  56. .slave_configure = pm8001_slave_configure,
  57. .slave_destroy = sas_slave_destroy,
  58. .scan_finished = pm8001_scan_finished,
  59. .scan_start = pm8001_scan_start,
  60. .change_queue_depth = sas_change_queue_depth,
  61. .change_queue_type = sas_change_queue_type,
  62. .bios_param = sas_bios_param,
  63. .can_queue = 1,
  64. .cmd_per_lun = 1,
  65. .this_id = -1,
  66. .sg_tablesize = SG_ALL,
  67. .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
  68. .use_clustering = ENABLE_CLUSTERING,
  69. .eh_device_reset_handler = sas_eh_device_reset_handler,
  70. .eh_bus_reset_handler = sas_eh_bus_reset_handler,
  71. .slave_alloc = pm8001_slave_alloc,
  72. .target_destroy = sas_target_destroy,
  73. .ioctl = sas_ioctl,
  74. .shost_attrs = pm8001_host_attrs,
  75. };
  76. /**
  77. * Sas layer call this function to execute specific task.
  78. */
  79. static struct sas_domain_function_template pm8001_transport_ops = {
  80. .lldd_dev_found = pm8001_dev_found,
  81. .lldd_dev_gone = pm8001_dev_gone,
  82. .lldd_execute_task = pm8001_queue_command,
  83. .lldd_control_phy = pm8001_phy_control,
  84. .lldd_abort_task = pm8001_abort_task,
  85. .lldd_abort_task_set = pm8001_abort_task_set,
  86. .lldd_clear_aca = pm8001_clear_aca,
  87. .lldd_clear_task_set = pm8001_clear_task_set,
  88. .lldd_I_T_nexus_reset = pm8001_I_T_nexus_reset,
  89. .lldd_lu_reset = pm8001_lu_reset,
  90. .lldd_query_task = pm8001_query_task,
  91. };
  92. /**
  93. *pm8001_phy_init - initiate our adapter phys
  94. *@pm8001_ha: our hba structure.
  95. *@phy_id: phy id.
  96. */
  97. static void __devinit pm8001_phy_init(struct pm8001_hba_info *pm8001_ha,
  98. int phy_id)
  99. {
  100. struct pm8001_phy *phy = &pm8001_ha->phy[phy_id];
  101. struct asd_sas_phy *sas_phy = &phy->sas_phy;
  102. phy->phy_state = 0;
  103. phy->pm8001_ha = pm8001_ha;
  104. sas_phy->enabled = (phy_id < pm8001_ha->chip->n_phy) ? 1 : 0;
  105. sas_phy->class = SAS;
  106. sas_phy->iproto = SAS_PROTOCOL_ALL;
  107. sas_phy->tproto = 0;
  108. sas_phy->type = PHY_TYPE_PHYSICAL;
  109. sas_phy->role = PHY_ROLE_INITIATOR;
  110. sas_phy->oob_mode = OOB_NOT_CONNECTED;
  111. sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
  112. sas_phy->id = phy_id;
  113. sas_phy->sas_addr = &pm8001_ha->sas_addr[0];
  114. sas_phy->frame_rcvd = &phy->frame_rcvd[0];
  115. sas_phy->ha = (struct sas_ha_struct *)pm8001_ha->shost->hostdata;
  116. sas_phy->lldd_phy = phy;
  117. }
  118. /**
  119. *pm8001_free - free hba
  120. *@pm8001_ha: our hba structure.
  121. *
  122. */
  123. static void pm8001_free(struct pm8001_hba_info *pm8001_ha)
  124. {
  125. int i;
  126. struct pm8001_wq *wq;
  127. if (!pm8001_ha)
  128. return;
  129. for (i = 0; i < USI_MAX_MEMCNT; i++) {
  130. if (pm8001_ha->memoryMap.region[i].virt_ptr != NULL) {
  131. pci_free_consistent(pm8001_ha->pdev,
  132. pm8001_ha->memoryMap.region[i].element_size,
  133. pm8001_ha->memoryMap.region[i].virt_ptr,
  134. pm8001_ha->memoryMap.region[i].phys_addr);
  135. }
  136. }
  137. PM8001_CHIP_DISP->chip_iounmap(pm8001_ha);
  138. if (pm8001_ha->shost)
  139. scsi_host_put(pm8001_ha->shost);
  140. list_for_each_entry(wq, &pm8001_ha->wq_list, entry)
  141. cancel_delayed_work(&wq->work_q);
  142. kfree(pm8001_ha->tags);
  143. kfree(pm8001_ha);
  144. }
  145. #ifdef PM8001_USE_TASKLET
  146. static void pm8001_tasklet(unsigned long opaque)
  147. {
  148. struct pm8001_hba_info *pm8001_ha;
  149. pm8001_ha = (struct pm8001_hba_info *)opaque;;
  150. if (unlikely(!pm8001_ha))
  151. BUG_ON(1);
  152. PM8001_CHIP_DISP->isr(pm8001_ha);
  153. }
  154. #endif
  155. /**
  156. * pm8001_interrupt - when HBA originate a interrupt,we should invoke this
  157. * dispatcher to handle each case.
  158. * @irq: irq number.
  159. * @opaque: the passed general host adapter struct
  160. */
  161. static irqreturn_t pm8001_interrupt(int irq, void *opaque)
  162. {
  163. struct pm8001_hba_info *pm8001_ha;
  164. irqreturn_t ret = IRQ_HANDLED;
  165. struct sas_ha_struct *sha = opaque;
  166. pm8001_ha = sha->lldd_ha;
  167. if (unlikely(!pm8001_ha))
  168. return IRQ_NONE;
  169. if (!PM8001_CHIP_DISP->is_our_interupt(pm8001_ha))
  170. return IRQ_NONE;
  171. #ifdef PM8001_USE_TASKLET
  172. tasklet_schedule(&pm8001_ha->tasklet);
  173. #else
  174. ret = PM8001_CHIP_DISP->isr(pm8001_ha);
  175. #endif
  176. return ret;
  177. }
  178. /**
  179. * pm8001_alloc - initiate our hba structure and 6 DMAs area.
  180. * @pm8001_ha:our hba structure.
  181. *
  182. */
  183. static int __devinit pm8001_alloc(struct pm8001_hba_info *pm8001_ha)
  184. {
  185. int i;
  186. spin_lock_init(&pm8001_ha->lock);
  187. for (i = 0; i < pm8001_ha->chip->n_phy; i++)
  188. pm8001_phy_init(pm8001_ha, i);
  189. pm8001_ha->tags = kzalloc(PM8001_MAX_CCB, GFP_KERNEL);
  190. if (!pm8001_ha->tags)
  191. goto err_out;
  192. /* MPI Memory region 1 for AAP Event Log for fw */
  193. pm8001_ha->memoryMap.region[AAP1].num_elements = 1;
  194. pm8001_ha->memoryMap.region[AAP1].element_size = PM8001_EVENT_LOG_SIZE;
  195. pm8001_ha->memoryMap.region[AAP1].total_len = PM8001_EVENT_LOG_SIZE;
  196. pm8001_ha->memoryMap.region[AAP1].alignment = 32;
  197. /* MPI Memory region 2 for IOP Event Log for fw */
  198. pm8001_ha->memoryMap.region[IOP].num_elements = 1;
  199. pm8001_ha->memoryMap.region[IOP].element_size = PM8001_EVENT_LOG_SIZE;
  200. pm8001_ha->memoryMap.region[IOP].total_len = PM8001_EVENT_LOG_SIZE;
  201. pm8001_ha->memoryMap.region[IOP].alignment = 32;
  202. /* MPI Memory region 3 for consumer Index of inbound queues */
  203. pm8001_ha->memoryMap.region[CI].num_elements = 1;
  204. pm8001_ha->memoryMap.region[CI].element_size = 4;
  205. pm8001_ha->memoryMap.region[CI].total_len = 4;
  206. pm8001_ha->memoryMap.region[CI].alignment = 4;
  207. /* MPI Memory region 4 for producer Index of outbound queues */
  208. pm8001_ha->memoryMap.region[PI].num_elements = 1;
  209. pm8001_ha->memoryMap.region[PI].element_size = 4;
  210. pm8001_ha->memoryMap.region[PI].total_len = 4;
  211. pm8001_ha->memoryMap.region[PI].alignment = 4;
  212. /* MPI Memory region 5 inbound queues */
  213. pm8001_ha->memoryMap.region[IB].num_elements = 256;
  214. pm8001_ha->memoryMap.region[IB].element_size = 64;
  215. pm8001_ha->memoryMap.region[IB].total_len = 256 * 64;
  216. pm8001_ha->memoryMap.region[IB].alignment = 64;
  217. /* MPI Memory region 6 inbound queues */
  218. pm8001_ha->memoryMap.region[OB].num_elements = 256;
  219. pm8001_ha->memoryMap.region[OB].element_size = 64;
  220. pm8001_ha->memoryMap.region[OB].total_len = 256 * 64;
  221. pm8001_ha->memoryMap.region[OB].alignment = 64;
  222. /* Memory region write DMA*/
  223. pm8001_ha->memoryMap.region[NVMD].num_elements = 1;
  224. pm8001_ha->memoryMap.region[NVMD].element_size = 4096;
  225. pm8001_ha->memoryMap.region[NVMD].total_len = 4096;
  226. /* Memory region for devices*/
  227. pm8001_ha->memoryMap.region[DEV_MEM].num_elements = 1;
  228. pm8001_ha->memoryMap.region[DEV_MEM].element_size = PM8001_MAX_DEVICES *
  229. sizeof(struct pm8001_device);
  230. pm8001_ha->memoryMap.region[DEV_MEM].total_len = PM8001_MAX_DEVICES *
  231. sizeof(struct pm8001_device);
  232. /* Memory region for ccb_info*/
  233. pm8001_ha->memoryMap.region[CCB_MEM].num_elements = 1;
  234. pm8001_ha->memoryMap.region[CCB_MEM].element_size = PM8001_MAX_CCB *
  235. sizeof(struct pm8001_ccb_info);
  236. pm8001_ha->memoryMap.region[CCB_MEM].total_len = PM8001_MAX_CCB *
  237. sizeof(struct pm8001_ccb_info);
  238. for (i = 0; i < USI_MAX_MEMCNT; i++) {
  239. if (pm8001_mem_alloc(pm8001_ha->pdev,
  240. &pm8001_ha->memoryMap.region[i].virt_ptr,
  241. &pm8001_ha->memoryMap.region[i].phys_addr,
  242. &pm8001_ha->memoryMap.region[i].phys_addr_hi,
  243. &pm8001_ha->memoryMap.region[i].phys_addr_lo,
  244. pm8001_ha->memoryMap.region[i].total_len,
  245. pm8001_ha->memoryMap.region[i].alignment) != 0) {
  246. PM8001_FAIL_DBG(pm8001_ha,
  247. pm8001_printk("Mem%d alloc failed\n",
  248. i));
  249. goto err_out;
  250. }
  251. }
  252. pm8001_ha->devices = pm8001_ha->memoryMap.region[DEV_MEM].virt_ptr;
  253. for (i = 0; i < PM8001_MAX_DEVICES; i++) {
  254. pm8001_ha->devices[i].dev_type = NO_DEVICE;
  255. pm8001_ha->devices[i].id = i;
  256. pm8001_ha->devices[i].device_id = PM8001_MAX_DEVICES;
  257. pm8001_ha->devices[i].running_req = 0;
  258. }
  259. pm8001_ha->ccb_info = pm8001_ha->memoryMap.region[CCB_MEM].virt_ptr;
  260. for (i = 0; i < PM8001_MAX_CCB; i++) {
  261. pm8001_ha->ccb_info[i].ccb_dma_handle =
  262. pm8001_ha->memoryMap.region[CCB_MEM].phys_addr +
  263. i * sizeof(struct pm8001_ccb_info);
  264. pm8001_ha->ccb_info[i].task = NULL;
  265. pm8001_ha->ccb_info[i].ccb_tag = 0xffffffff;
  266. pm8001_ha->ccb_info[i].device = NULL;
  267. ++pm8001_ha->tags_num;
  268. }
  269. pm8001_ha->flags = PM8001F_INIT_TIME;
  270. /* Initialize tags */
  271. pm8001_tag_init(pm8001_ha);
  272. return 0;
  273. err_out:
  274. return 1;
  275. }
  276. /**
  277. * pm8001_ioremap - remap the pci high physical address to kernal virtual
  278. * address so that we can access them.
  279. * @pm8001_ha:our hba structure.
  280. */
  281. static int pm8001_ioremap(struct pm8001_hba_info *pm8001_ha)
  282. {
  283. u32 bar;
  284. u32 logicalBar = 0;
  285. struct pci_dev *pdev;
  286. pdev = pm8001_ha->pdev;
  287. /* map pci mem (PMC pci base 0-3)*/
  288. for (bar = 0; bar < 6; bar++) {
  289. /*
  290. ** logical BARs for SPC:
  291. ** bar 0 and 1 - logical BAR0
  292. ** bar 2 and 3 - logical BAR1
  293. ** bar4 - logical BAR2
  294. ** bar5 - logical BAR3
  295. ** Skip the appropriate assignments:
  296. */
  297. if ((bar == 1) || (bar == 3))
  298. continue;
  299. if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
  300. pm8001_ha->io_mem[logicalBar].membase =
  301. pci_resource_start(pdev, bar);
  302. pm8001_ha->io_mem[logicalBar].membase &=
  303. (u32)PCI_BASE_ADDRESS_MEM_MASK;
  304. pm8001_ha->io_mem[logicalBar].memsize =
  305. pci_resource_len(pdev, bar);
  306. pm8001_ha->io_mem[logicalBar].memvirtaddr =
  307. ioremap(pm8001_ha->io_mem[logicalBar].membase,
  308. pm8001_ha->io_mem[logicalBar].memsize);
  309. PM8001_INIT_DBG(pm8001_ha,
  310. pm8001_printk("PCI: bar %d, logicalBar %d "
  311. "virt_addr=%lx,len=%d\n", bar, logicalBar,
  312. (unsigned long)
  313. pm8001_ha->io_mem[logicalBar].memvirtaddr,
  314. pm8001_ha->io_mem[logicalBar].memsize));
  315. } else {
  316. pm8001_ha->io_mem[logicalBar].membase = 0;
  317. pm8001_ha->io_mem[logicalBar].memsize = 0;
  318. pm8001_ha->io_mem[logicalBar].memvirtaddr = 0;
  319. }
  320. logicalBar++;
  321. }
  322. return 0;
  323. }
  324. /**
  325. * pm8001_pci_alloc - initialize our ha card structure
  326. * @pdev: pci device.
  327. * @ent: ent
  328. * @shost: scsi host struct which has been initialized before.
  329. */
  330. static struct pm8001_hba_info *__devinit
  331. pm8001_pci_alloc(struct pci_dev *pdev, u32 chip_id, struct Scsi_Host *shost)
  332. {
  333. struct pm8001_hba_info *pm8001_ha;
  334. struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
  335. pm8001_ha = sha->lldd_ha;
  336. if (!pm8001_ha)
  337. return NULL;
  338. pm8001_ha->pdev = pdev;
  339. pm8001_ha->dev = &pdev->dev;
  340. pm8001_ha->chip_id = chip_id;
  341. pm8001_ha->chip = &pm8001_chips[pm8001_ha->chip_id];
  342. pm8001_ha->irq = pdev->irq;
  343. pm8001_ha->sas = sha;
  344. pm8001_ha->shost = shost;
  345. pm8001_ha->id = pm8001_id++;
  346. INIT_LIST_HEAD(&pm8001_ha->wq_list);
  347. pm8001_ha->logging_level = 0x01;
  348. sprintf(pm8001_ha->name, "%s%d", DRV_NAME, pm8001_ha->id);
  349. #ifdef PM8001_USE_TASKLET
  350. tasklet_init(&pm8001_ha->tasklet, pm8001_tasklet,
  351. (unsigned long)pm8001_ha);
  352. #endif
  353. pm8001_ioremap(pm8001_ha);
  354. if (!pm8001_alloc(pm8001_ha))
  355. return pm8001_ha;
  356. pm8001_free(pm8001_ha);
  357. return NULL;
  358. }
  359. /**
  360. * pci_go_44 - pm8001 specified, its DMA is 44 bit rather than 64 bit
  361. * @pdev: pci device.
  362. */
  363. static int pci_go_44(struct pci_dev *pdev)
  364. {
  365. int rc;
  366. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(44))) {
  367. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(44));
  368. if (rc) {
  369. rc = pci_set_consistent_dma_mask(pdev,
  370. DMA_BIT_MASK(32));
  371. if (rc) {
  372. dev_printk(KERN_ERR, &pdev->dev,
  373. "44-bit DMA enable failed\n");
  374. return rc;
  375. }
  376. }
  377. } else {
  378. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  379. if (rc) {
  380. dev_printk(KERN_ERR, &pdev->dev,
  381. "32-bit DMA enable failed\n");
  382. return rc;
  383. }
  384. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  385. if (rc) {
  386. dev_printk(KERN_ERR, &pdev->dev,
  387. "32-bit consistent DMA enable failed\n");
  388. return rc;
  389. }
  390. }
  391. return rc;
  392. }
  393. /**
  394. * pm8001_prep_sas_ha_init - allocate memory in general hba struct && init them.
  395. * @shost: scsi host which has been allocated outside.
  396. * @chip_info: our ha struct.
  397. */
  398. static int __devinit pm8001_prep_sas_ha_init(struct Scsi_Host * shost,
  399. const struct pm8001_chip_info *chip_info)
  400. {
  401. int phy_nr, port_nr;
  402. struct asd_sas_phy **arr_phy;
  403. struct asd_sas_port **arr_port;
  404. struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
  405. phy_nr = chip_info->n_phy;
  406. port_nr = phy_nr;
  407. memset(sha, 0x00, sizeof(*sha));
  408. arr_phy = kcalloc(phy_nr, sizeof(void *), GFP_KERNEL);
  409. if (!arr_phy)
  410. goto exit;
  411. arr_port = kcalloc(port_nr, sizeof(void *), GFP_KERNEL);
  412. if (!arr_port)
  413. goto exit_free2;
  414. sha->sas_phy = arr_phy;
  415. sha->sas_port = arr_port;
  416. sha->lldd_ha = kzalloc(sizeof(struct pm8001_hba_info), GFP_KERNEL);
  417. if (!sha->lldd_ha)
  418. goto exit_free1;
  419. shost->transportt = pm8001_stt;
  420. shost->max_id = PM8001_MAX_DEVICES;
  421. shost->max_lun = 8;
  422. shost->max_channel = 0;
  423. shost->unique_id = pm8001_id;
  424. shost->max_cmd_len = 16;
  425. shost->can_queue = PM8001_CAN_QUEUE;
  426. shost->cmd_per_lun = 32;
  427. return 0;
  428. exit_free1:
  429. kfree(arr_port);
  430. exit_free2:
  431. kfree(arr_phy);
  432. exit:
  433. return -1;
  434. }
  435. /**
  436. * pm8001_post_sas_ha_init - initialize general hba struct defined in libsas
  437. * @shost: scsi host which has been allocated outside
  438. * @chip_info: our ha struct.
  439. */
  440. static void __devinit pm8001_post_sas_ha_init(struct Scsi_Host *shost,
  441. const struct pm8001_chip_info *chip_info)
  442. {
  443. int i = 0;
  444. struct pm8001_hba_info *pm8001_ha;
  445. struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
  446. pm8001_ha = sha->lldd_ha;
  447. for (i = 0; i < chip_info->n_phy; i++) {
  448. sha->sas_phy[i] = &pm8001_ha->phy[i].sas_phy;
  449. sha->sas_port[i] = &pm8001_ha->port[i].sas_port;
  450. }
  451. sha->sas_ha_name = DRV_NAME;
  452. sha->dev = pm8001_ha->dev;
  453. sha->lldd_module = THIS_MODULE;
  454. sha->sas_addr = &pm8001_ha->sas_addr[0];
  455. sha->num_phys = chip_info->n_phy;
  456. sha->lldd_max_execute_num = 1;
  457. sha->lldd_queue_size = PM8001_CAN_QUEUE;
  458. sha->core.shost = shost;
  459. }
  460. /**
  461. * pm8001_init_sas_add - initialize sas address
  462. * @chip_info: our ha struct.
  463. *
  464. * Currently we just set the fixed SAS address to our HBA,for manufacture,
  465. * it should read from the EEPROM
  466. */
  467. static void pm8001_init_sas_add(struct pm8001_hba_info *pm8001_ha)
  468. {
  469. u8 i;
  470. #ifdef PM8001_READ_VPD
  471. DECLARE_COMPLETION_ONSTACK(completion);
  472. pm8001_ha->nvmd_completion = &completion;
  473. PM8001_CHIP_DISP->get_nvmd_req(pm8001_ha, 0, 0);
  474. wait_for_completion(&completion);
  475. for (i = 0; i < pm8001_ha->chip->n_phy; i++) {
  476. memcpy(&pm8001_ha->phy[i].dev_sas_addr, pm8001_ha->sas_addr,
  477. SAS_ADDR_SIZE);
  478. PM8001_INIT_DBG(pm8001_ha,
  479. pm8001_printk("phy %d sas_addr = %x \n", i,
  480. (u64)pm8001_ha->phy[i].dev_sas_addr));
  481. }
  482. #else
  483. for (i = 0; i < pm8001_ha->chip->n_phy; i++) {
  484. pm8001_ha->phy[i].dev_sas_addr = 0x500e004010000004ULL;
  485. pm8001_ha->phy[i].dev_sas_addr =
  486. cpu_to_be64((u64)
  487. (*(u64 *)&pm8001_ha->phy[i].dev_sas_addr));
  488. }
  489. memcpy(pm8001_ha->sas_addr, &pm8001_ha->phy[0].dev_sas_addr,
  490. SAS_ADDR_SIZE);
  491. #endif
  492. }
  493. #ifdef PM8001_USE_MSIX
  494. /**
  495. * pm8001_setup_msix - enable MSI-X interrupt
  496. * @chip_info: our ha struct.
  497. * @irq_handler: irq_handler
  498. */
  499. static u32 pm8001_setup_msix(struct pm8001_hba_info *pm8001_ha,
  500. irq_handler_t irq_handler)
  501. {
  502. u32 i = 0, j = 0;
  503. u32 number_of_intr = 1;
  504. int flag = 0;
  505. u32 max_entry;
  506. int rc;
  507. max_entry = sizeof(pm8001_ha->msix_entries) /
  508. sizeof(pm8001_ha->msix_entries[0]);
  509. flag |= IRQF_DISABLED;
  510. for (i = 0; i < max_entry ; i++)
  511. pm8001_ha->msix_entries[i].entry = i;
  512. rc = pci_enable_msix(pm8001_ha->pdev, pm8001_ha->msix_entries,
  513. number_of_intr);
  514. pm8001_ha->number_of_intr = number_of_intr;
  515. if (!rc) {
  516. for (i = 0; i < number_of_intr; i++) {
  517. if (request_irq(pm8001_ha->msix_entries[i].vector,
  518. irq_handler, flag, DRV_NAME,
  519. SHOST_TO_SAS_HA(pm8001_ha->shost))) {
  520. for (j = 0; j < i; j++)
  521. free_irq(
  522. pm8001_ha->msix_entries[j].vector,
  523. SHOST_TO_SAS_HA(pm8001_ha->shost));
  524. pci_disable_msix(pm8001_ha->pdev);
  525. break;
  526. }
  527. }
  528. }
  529. return rc;
  530. }
  531. #endif
  532. /**
  533. * pm8001_request_irq - register interrupt
  534. * @chip_info: our ha struct.
  535. */
  536. static u32 pm8001_request_irq(struct pm8001_hba_info *pm8001_ha)
  537. {
  538. struct pci_dev *pdev;
  539. irq_handler_t irq_handler = pm8001_interrupt;
  540. int rc;
  541. pdev = pm8001_ha->pdev;
  542. #ifdef PM8001_USE_MSIX
  543. if (pci_find_capability(pdev, PCI_CAP_ID_MSIX))
  544. return pm8001_setup_msix(pm8001_ha, irq_handler);
  545. else
  546. goto intx;
  547. #endif
  548. intx:
  549. /* intialize the INT-X interrupt */
  550. rc = request_irq(pdev->irq, irq_handler, IRQF_SHARED, DRV_NAME,
  551. SHOST_TO_SAS_HA(pm8001_ha->shost));
  552. return rc;
  553. }
  554. /**
  555. * pm8001_pci_probe - probe supported device
  556. * @pdev: pci device which kernel has been prepared for.
  557. * @ent: pci device id
  558. *
  559. * This function is the main initialization function, when register a new
  560. * pci driver it is invoked, all struct an hardware initilization should be done
  561. * here, also, register interrupt
  562. */
  563. static int __devinit pm8001_pci_probe(struct pci_dev *pdev,
  564. const struct pci_device_id *ent)
  565. {
  566. unsigned int rc;
  567. u32 pci_reg;
  568. struct pm8001_hba_info *pm8001_ha;
  569. struct Scsi_Host *shost = NULL;
  570. const struct pm8001_chip_info *chip;
  571. dev_printk(KERN_INFO, &pdev->dev,
  572. "pm8001: driver version %s\n", DRV_VERSION);
  573. rc = pci_enable_device(pdev);
  574. if (rc)
  575. goto err_out_enable;
  576. pci_set_master(pdev);
  577. /*
  578. * Enable pci slot busmaster by setting pci command register.
  579. * This is required by FW for Cyclone card.
  580. */
  581. pci_read_config_dword(pdev, PCI_COMMAND, &pci_reg);
  582. pci_reg |= 0x157;
  583. pci_write_config_dword(pdev, PCI_COMMAND, pci_reg);
  584. rc = pci_request_regions(pdev, DRV_NAME);
  585. if (rc)
  586. goto err_out_disable;
  587. rc = pci_go_44(pdev);
  588. if (rc)
  589. goto err_out_regions;
  590. shost = scsi_host_alloc(&pm8001_sht, sizeof(void *));
  591. if (!shost) {
  592. rc = -ENOMEM;
  593. goto err_out_regions;
  594. }
  595. chip = &pm8001_chips[ent->driver_data];
  596. SHOST_TO_SAS_HA(shost) =
  597. kcalloc(1, sizeof(struct sas_ha_struct), GFP_KERNEL);
  598. if (!SHOST_TO_SAS_HA(shost)) {
  599. rc = -ENOMEM;
  600. goto err_out_free_host;
  601. }
  602. rc = pm8001_prep_sas_ha_init(shost, chip);
  603. if (rc) {
  604. rc = -ENOMEM;
  605. goto err_out_free;
  606. }
  607. pci_set_drvdata(pdev, SHOST_TO_SAS_HA(shost));
  608. pm8001_ha = pm8001_pci_alloc(pdev, chip_8001, shost);
  609. if (!pm8001_ha) {
  610. rc = -ENOMEM;
  611. goto err_out_free;
  612. }
  613. list_add_tail(&pm8001_ha->list, &hba_list);
  614. PM8001_CHIP_DISP->chip_soft_rst(pm8001_ha, 0x252acbcd);
  615. rc = PM8001_CHIP_DISP->chip_init(pm8001_ha);
  616. if (rc)
  617. goto err_out_ha_free;
  618. rc = scsi_add_host(shost, &pdev->dev);
  619. if (rc)
  620. goto err_out_ha_free;
  621. rc = pm8001_request_irq(pm8001_ha);
  622. if (rc)
  623. goto err_out_shost;
  624. PM8001_CHIP_DISP->interrupt_enable(pm8001_ha);
  625. pm8001_init_sas_add(pm8001_ha);
  626. pm8001_post_sas_ha_init(shost, chip);
  627. rc = sas_register_ha(SHOST_TO_SAS_HA(shost));
  628. if (rc)
  629. goto err_out_shost;
  630. scsi_scan_host(pm8001_ha->shost);
  631. return 0;
  632. err_out_shost:
  633. scsi_remove_host(pm8001_ha->shost);
  634. err_out_ha_free:
  635. pm8001_free(pm8001_ha);
  636. err_out_free:
  637. kfree(SHOST_TO_SAS_HA(shost));
  638. err_out_free_host:
  639. kfree(shost);
  640. err_out_regions:
  641. pci_release_regions(pdev);
  642. err_out_disable:
  643. pci_disable_device(pdev);
  644. err_out_enable:
  645. return rc;
  646. }
  647. static void __devexit pm8001_pci_remove(struct pci_dev *pdev)
  648. {
  649. struct sas_ha_struct *sha = pci_get_drvdata(pdev);
  650. struct pm8001_hba_info *pm8001_ha;
  651. int i;
  652. pm8001_ha = sha->lldd_ha;
  653. pci_set_drvdata(pdev, NULL);
  654. sas_unregister_ha(sha);
  655. sas_remove_host(pm8001_ha->shost);
  656. list_del(&pm8001_ha->list);
  657. scsi_remove_host(pm8001_ha->shost);
  658. PM8001_CHIP_DISP->interrupt_disable(pm8001_ha);
  659. PM8001_CHIP_DISP->chip_soft_rst(pm8001_ha, 0x252acbcd);
  660. #ifdef PM8001_USE_MSIX
  661. for (i = 0; i < pm8001_ha->number_of_intr; i++)
  662. synchronize_irq(pm8001_ha->msix_entries[i].vector);
  663. for (i = 0; i < pm8001_ha->number_of_intr; i++)
  664. free_irq(pm8001_ha->msix_entries[i].vector, sha);
  665. pci_disable_msix(pdev);
  666. #else
  667. free_irq(pm8001_ha->irq, sha);
  668. #endif
  669. #ifdef PM8001_USE_TASKLET
  670. tasklet_kill(&pm8001_ha->tasklet);
  671. #endif
  672. pm8001_free(pm8001_ha);
  673. kfree(sha->sas_phy);
  674. kfree(sha->sas_port);
  675. kfree(sha);
  676. pci_release_regions(pdev);
  677. pci_disable_device(pdev);
  678. }
  679. /**
  680. * pm8001_pci_suspend - power management suspend main entry point
  681. * @pdev: PCI device struct
  682. * @state: PM state change to (usually PCI_D3)
  683. *
  684. * Returns 0 success, anything else error.
  685. */
  686. static int pm8001_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  687. {
  688. struct sas_ha_struct *sha = pci_get_drvdata(pdev);
  689. struct pm8001_hba_info *pm8001_ha;
  690. int i , pos;
  691. u32 device_state;
  692. pm8001_ha = sha->lldd_ha;
  693. flush_scheduled_work();
  694. scsi_block_requests(pm8001_ha->shost);
  695. pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
  696. if (pos == 0) {
  697. printk(KERN_ERR " PCI PM not supported\n");
  698. return -ENODEV;
  699. }
  700. PM8001_CHIP_DISP->interrupt_disable(pm8001_ha);
  701. PM8001_CHIP_DISP->chip_soft_rst(pm8001_ha, 0x252acbcd);
  702. #ifdef PM8001_USE_MSIX
  703. for (i = 0; i < pm8001_ha->number_of_intr; i++)
  704. synchronize_irq(pm8001_ha->msix_entries[i].vector);
  705. for (i = 0; i < pm8001_ha->number_of_intr; i++)
  706. free_irq(pm8001_ha->msix_entries[i].vector, sha);
  707. pci_disable_msix(pdev);
  708. #else
  709. free_irq(pm8001_ha->irq, sha);
  710. #endif
  711. #ifdef PM8001_USE_TASKLET
  712. tasklet_kill(&pm8001_ha->tasklet);
  713. #endif
  714. device_state = pci_choose_state(pdev, state);
  715. pm8001_printk("pdev=0x%p, slot=%s, entering "
  716. "operating state [D%d]\n", pdev,
  717. pm8001_ha->name, device_state);
  718. pci_save_state(pdev);
  719. pci_disable_device(pdev);
  720. pci_set_power_state(pdev, device_state);
  721. return 0;
  722. }
  723. /**
  724. * pm8001_pci_resume - power management resume main entry point
  725. * @pdev: PCI device struct
  726. *
  727. * Returns 0 success, anything else error.
  728. */
  729. static int pm8001_pci_resume(struct pci_dev *pdev)
  730. {
  731. struct sas_ha_struct *sha = pci_get_drvdata(pdev);
  732. struct pm8001_hba_info *pm8001_ha;
  733. int rc;
  734. u32 device_state;
  735. pm8001_ha = sha->lldd_ha;
  736. device_state = pdev->current_state;
  737. pm8001_printk("pdev=0x%p, slot=%s, resuming from previous "
  738. "operating state [D%d]\n", pdev, pm8001_ha->name, device_state);
  739. pci_set_power_state(pdev, PCI_D0);
  740. pci_enable_wake(pdev, PCI_D0, 0);
  741. pci_restore_state(pdev);
  742. rc = pci_enable_device(pdev);
  743. if (rc) {
  744. pm8001_printk("slot=%s Enable device failed during resume\n",
  745. pm8001_ha->name);
  746. goto err_out_enable;
  747. }
  748. pci_set_master(pdev);
  749. rc = pci_go_44(pdev);
  750. if (rc)
  751. goto err_out_disable;
  752. PM8001_CHIP_DISP->chip_soft_rst(pm8001_ha, 0x252acbcd);
  753. rc = PM8001_CHIP_DISP->chip_init(pm8001_ha);
  754. if (rc)
  755. goto err_out_disable;
  756. PM8001_CHIP_DISP->interrupt_disable(pm8001_ha);
  757. rc = pm8001_request_irq(pm8001_ha);
  758. if (rc)
  759. goto err_out_disable;
  760. #ifdef PM8001_USE_TASKLET
  761. tasklet_init(&pm8001_ha->tasklet, pm8001_tasklet,
  762. (unsigned long)pm8001_ha);
  763. #endif
  764. PM8001_CHIP_DISP->interrupt_enable(pm8001_ha);
  765. scsi_unblock_requests(pm8001_ha->shost);
  766. return 0;
  767. err_out_disable:
  768. scsi_remove_host(pm8001_ha->shost);
  769. pci_disable_device(pdev);
  770. err_out_enable:
  771. return rc;
  772. }
  773. static struct pci_device_id __devinitdata pm8001_pci_table[] = {
  774. {
  775. PCI_VDEVICE(PMC_Sierra, 0x8001), chip_8001
  776. },
  777. {
  778. PCI_DEVICE(0x117c, 0x0042),
  779. .driver_data = chip_8001
  780. },
  781. {} /* terminate list */
  782. };
  783. static struct pci_driver pm8001_pci_driver = {
  784. .name = DRV_NAME,
  785. .id_table = pm8001_pci_table,
  786. .probe = pm8001_pci_probe,
  787. .remove = __devexit_p(pm8001_pci_remove),
  788. .suspend = pm8001_pci_suspend,
  789. .resume = pm8001_pci_resume,
  790. };
  791. /**
  792. * pm8001_init - initialize scsi transport template
  793. */
  794. static int __init pm8001_init(void)
  795. {
  796. int rc;
  797. pm8001_id = 0;
  798. pm8001_stt = sas_domain_attach_transport(&pm8001_transport_ops);
  799. if (!pm8001_stt)
  800. return -ENOMEM;
  801. rc = pci_register_driver(&pm8001_pci_driver);
  802. if (rc)
  803. goto err_out;
  804. return 0;
  805. err_out:
  806. sas_release_transport(pm8001_stt);
  807. return rc;
  808. }
  809. static void __exit pm8001_exit(void)
  810. {
  811. pci_unregister_driver(&pm8001_pci_driver);
  812. sas_release_transport(pm8001_stt);
  813. }
  814. module_init(pm8001_init);
  815. module_exit(pm8001_exit);
  816. MODULE_AUTHOR("Jack Wang <jack_wang@usish.com>");
  817. MODULE_DESCRIPTION("PMC-Sierra PM8001 SAS/SATA controller driver");
  818. MODULE_VERSION(DRV_VERSION);
  819. MODULE_LICENSE("GPL");
  820. MODULE_DEVICE_TABLE(pci, pm8001_pci_table);