pm8001_init.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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 <linux/slab.h>
  41. #include "pm8001_sas.h"
  42. #include "pm8001_chips.h"
  43. static struct scsi_transport_template *pm8001_stt;
  44. static const struct pm8001_chip_info pm8001_chips[] = {
  45. [chip_8001] = { 8, &pm8001_8001_dispatch,},
  46. };
  47. static int pm8001_id;
  48. LIST_HEAD(hba_list);
  49. struct workqueue_struct *pm8001_wq;
  50. /**
  51. * The main structure which LLDD must register for scsi core.
  52. */
  53. static struct scsi_host_template pm8001_sht = {
  54. .module = THIS_MODULE,
  55. .name = DRV_NAME,
  56. .queuecommand = sas_queuecommand,
  57. .target_alloc = sas_target_alloc,
  58. .slave_configure = sas_slave_configure,
  59. .scan_finished = pm8001_scan_finished,
  60. .scan_start = pm8001_scan_start,
  61. .change_queue_depth = sas_change_queue_depth,
  62. .change_queue_type = sas_change_queue_type,
  63. .bios_param = sas_bios_param,
  64. .can_queue = 1,
  65. .cmd_per_lun = 1,
  66. .this_id = -1,
  67. .sg_tablesize = SG_ALL,
  68. .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
  69. .use_clustering = ENABLE_CLUSTERING,
  70. .eh_device_reset_handler = sas_eh_device_reset_handler,
  71. .eh_bus_reset_handler = sas_eh_bus_reset_handler,
  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. if (!pm8001_ha)
  127. return;
  128. for (i = 0; i < USI_MAX_MEMCNT; i++) {
  129. if (pm8001_ha->memoryMap.region[i].virt_ptr != NULL) {
  130. pci_free_consistent(pm8001_ha->pdev,
  131. pm8001_ha->memoryMap.region[i].element_size,
  132. pm8001_ha->memoryMap.region[i].virt_ptr,
  133. pm8001_ha->memoryMap.region[i].phys_addr);
  134. }
  135. }
  136. PM8001_CHIP_DISP->chip_iounmap(pm8001_ha);
  137. if (pm8001_ha->shost)
  138. scsi_host_put(pm8001_ha->shost);
  139. flush_workqueue(pm8001_wq);
  140. kfree(pm8001_ha->tags);
  141. kfree(pm8001_ha);
  142. }
  143. #ifdef PM8001_USE_TASKLET
  144. static void pm8001_tasklet(unsigned long opaque)
  145. {
  146. struct pm8001_hba_info *pm8001_ha;
  147. pm8001_ha = (struct pm8001_hba_info *)opaque;
  148. if (unlikely(!pm8001_ha))
  149. BUG_ON(1);
  150. PM8001_CHIP_DISP->isr(pm8001_ha);
  151. }
  152. #endif
  153. /**
  154. * pm8001_interrupt - when HBA originate a interrupt,we should invoke this
  155. * dispatcher to handle each case.
  156. * @irq: irq number.
  157. * @opaque: the passed general host adapter struct
  158. */
  159. static irqreturn_t pm8001_interrupt(int irq, void *opaque)
  160. {
  161. struct pm8001_hba_info *pm8001_ha;
  162. irqreturn_t ret = IRQ_HANDLED;
  163. struct sas_ha_struct *sha = opaque;
  164. pm8001_ha = sha->lldd_ha;
  165. if (unlikely(!pm8001_ha))
  166. return IRQ_NONE;
  167. if (!PM8001_CHIP_DISP->is_our_interupt(pm8001_ha))
  168. return IRQ_NONE;
  169. #ifdef PM8001_USE_TASKLET
  170. tasklet_schedule(&pm8001_ha->tasklet);
  171. #else
  172. ret = PM8001_CHIP_DISP->isr(pm8001_ha);
  173. #endif
  174. return ret;
  175. }
  176. /**
  177. * pm8001_alloc - initiate our hba structure and 6 DMAs area.
  178. * @pm8001_ha:our hba structure.
  179. *
  180. */
  181. static int __devinit pm8001_alloc(struct pm8001_hba_info *pm8001_ha)
  182. {
  183. int i;
  184. spin_lock_init(&pm8001_ha->lock);
  185. for (i = 0; i < pm8001_ha->chip->n_phy; i++) {
  186. pm8001_phy_init(pm8001_ha, i);
  187. pm8001_ha->port[i].wide_port_phymap = 0;
  188. pm8001_ha->port[i].port_attached = 0;
  189. pm8001_ha->port[i].port_state = 0;
  190. INIT_LIST_HEAD(&pm8001_ha->port[i].list);
  191. }
  192. pm8001_ha->tags = kzalloc(PM8001_MAX_CCB, GFP_KERNEL);
  193. if (!pm8001_ha->tags)
  194. goto err_out;
  195. /* MPI Memory region 1 for AAP Event Log for fw */
  196. pm8001_ha->memoryMap.region[AAP1].num_elements = 1;
  197. pm8001_ha->memoryMap.region[AAP1].element_size = PM8001_EVENT_LOG_SIZE;
  198. pm8001_ha->memoryMap.region[AAP1].total_len = PM8001_EVENT_LOG_SIZE;
  199. pm8001_ha->memoryMap.region[AAP1].alignment = 32;
  200. /* MPI Memory region 2 for IOP Event Log for fw */
  201. pm8001_ha->memoryMap.region[IOP].num_elements = 1;
  202. pm8001_ha->memoryMap.region[IOP].element_size = PM8001_EVENT_LOG_SIZE;
  203. pm8001_ha->memoryMap.region[IOP].total_len = PM8001_EVENT_LOG_SIZE;
  204. pm8001_ha->memoryMap.region[IOP].alignment = 32;
  205. /* MPI Memory region 3 for consumer Index of inbound queues */
  206. pm8001_ha->memoryMap.region[CI].num_elements = 1;
  207. pm8001_ha->memoryMap.region[CI].element_size = 4;
  208. pm8001_ha->memoryMap.region[CI].total_len = 4;
  209. pm8001_ha->memoryMap.region[CI].alignment = 4;
  210. /* MPI Memory region 4 for producer Index of outbound queues */
  211. pm8001_ha->memoryMap.region[PI].num_elements = 1;
  212. pm8001_ha->memoryMap.region[PI].element_size = 4;
  213. pm8001_ha->memoryMap.region[PI].total_len = 4;
  214. pm8001_ha->memoryMap.region[PI].alignment = 4;
  215. /* MPI Memory region 5 inbound queues */
  216. pm8001_ha->memoryMap.region[IB].num_elements = 256;
  217. pm8001_ha->memoryMap.region[IB].element_size = 64;
  218. pm8001_ha->memoryMap.region[IB].total_len = 256 * 64;
  219. pm8001_ha->memoryMap.region[IB].alignment = 64;
  220. /* MPI Memory region 6 inbound queues */
  221. pm8001_ha->memoryMap.region[OB].num_elements = 256;
  222. pm8001_ha->memoryMap.region[OB].element_size = 64;
  223. pm8001_ha->memoryMap.region[OB].total_len = 256 * 64;
  224. pm8001_ha->memoryMap.region[OB].alignment = 64;
  225. /* Memory region write DMA*/
  226. pm8001_ha->memoryMap.region[NVMD].num_elements = 1;
  227. pm8001_ha->memoryMap.region[NVMD].element_size = 4096;
  228. pm8001_ha->memoryMap.region[NVMD].total_len = 4096;
  229. /* Memory region for devices*/
  230. pm8001_ha->memoryMap.region[DEV_MEM].num_elements = 1;
  231. pm8001_ha->memoryMap.region[DEV_MEM].element_size = PM8001_MAX_DEVICES *
  232. sizeof(struct pm8001_device);
  233. pm8001_ha->memoryMap.region[DEV_MEM].total_len = PM8001_MAX_DEVICES *
  234. sizeof(struct pm8001_device);
  235. /* Memory region for ccb_info*/
  236. pm8001_ha->memoryMap.region[CCB_MEM].num_elements = 1;
  237. pm8001_ha->memoryMap.region[CCB_MEM].element_size = PM8001_MAX_CCB *
  238. sizeof(struct pm8001_ccb_info);
  239. pm8001_ha->memoryMap.region[CCB_MEM].total_len = PM8001_MAX_CCB *
  240. sizeof(struct pm8001_ccb_info);
  241. for (i = 0; i < USI_MAX_MEMCNT; i++) {
  242. if (pm8001_mem_alloc(pm8001_ha->pdev,
  243. &pm8001_ha->memoryMap.region[i].virt_ptr,
  244. &pm8001_ha->memoryMap.region[i].phys_addr,
  245. &pm8001_ha->memoryMap.region[i].phys_addr_hi,
  246. &pm8001_ha->memoryMap.region[i].phys_addr_lo,
  247. pm8001_ha->memoryMap.region[i].total_len,
  248. pm8001_ha->memoryMap.region[i].alignment) != 0) {
  249. PM8001_FAIL_DBG(pm8001_ha,
  250. pm8001_printk("Mem%d alloc failed\n",
  251. i));
  252. goto err_out;
  253. }
  254. }
  255. pm8001_ha->devices = pm8001_ha->memoryMap.region[DEV_MEM].virt_ptr;
  256. for (i = 0; i < PM8001_MAX_DEVICES; i++) {
  257. pm8001_ha->devices[i].dev_type = NO_DEVICE;
  258. pm8001_ha->devices[i].id = i;
  259. pm8001_ha->devices[i].device_id = PM8001_MAX_DEVICES;
  260. pm8001_ha->devices[i].running_req = 0;
  261. }
  262. pm8001_ha->ccb_info = pm8001_ha->memoryMap.region[CCB_MEM].virt_ptr;
  263. for (i = 0; i < PM8001_MAX_CCB; i++) {
  264. pm8001_ha->ccb_info[i].ccb_dma_handle =
  265. pm8001_ha->memoryMap.region[CCB_MEM].phys_addr +
  266. i * sizeof(struct pm8001_ccb_info);
  267. pm8001_ha->ccb_info[i].task = NULL;
  268. pm8001_ha->ccb_info[i].ccb_tag = 0xffffffff;
  269. pm8001_ha->ccb_info[i].device = NULL;
  270. ++pm8001_ha->tags_num;
  271. }
  272. pm8001_ha->flags = PM8001F_INIT_TIME;
  273. /* Initialize tags */
  274. pm8001_tag_init(pm8001_ha);
  275. return 0;
  276. err_out:
  277. return 1;
  278. }
  279. /**
  280. * pm8001_ioremap - remap the pci high physical address to kernal virtual
  281. * address so that we can access them.
  282. * @pm8001_ha:our hba structure.
  283. */
  284. static int pm8001_ioremap(struct pm8001_hba_info *pm8001_ha)
  285. {
  286. u32 bar;
  287. u32 logicalBar = 0;
  288. struct pci_dev *pdev;
  289. pdev = pm8001_ha->pdev;
  290. /* map pci mem (PMC pci base 0-3)*/
  291. for (bar = 0; bar < 6; bar++) {
  292. /*
  293. ** logical BARs for SPC:
  294. ** bar 0 and 1 - logical BAR0
  295. ** bar 2 and 3 - logical BAR1
  296. ** bar4 - logical BAR2
  297. ** bar5 - logical BAR3
  298. ** Skip the appropriate assignments:
  299. */
  300. if ((bar == 1) || (bar == 3))
  301. continue;
  302. if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
  303. pm8001_ha->io_mem[logicalBar].membase =
  304. pci_resource_start(pdev, bar);
  305. pm8001_ha->io_mem[logicalBar].membase &=
  306. (u32)PCI_BASE_ADDRESS_MEM_MASK;
  307. pm8001_ha->io_mem[logicalBar].memsize =
  308. pci_resource_len(pdev, bar);
  309. pm8001_ha->io_mem[logicalBar].memvirtaddr =
  310. ioremap(pm8001_ha->io_mem[logicalBar].membase,
  311. pm8001_ha->io_mem[logicalBar].memsize);
  312. PM8001_INIT_DBG(pm8001_ha,
  313. pm8001_printk("PCI: bar %d, logicalBar %d "
  314. "virt_addr=%lx,len=%d\n", bar, logicalBar,
  315. (unsigned long)
  316. pm8001_ha->io_mem[logicalBar].memvirtaddr,
  317. pm8001_ha->io_mem[logicalBar].memsize));
  318. } else {
  319. pm8001_ha->io_mem[logicalBar].membase = 0;
  320. pm8001_ha->io_mem[logicalBar].memsize = 0;
  321. pm8001_ha->io_mem[logicalBar].memvirtaddr = 0;
  322. }
  323. logicalBar++;
  324. }
  325. return 0;
  326. }
  327. /**
  328. * pm8001_pci_alloc - initialize our ha card structure
  329. * @pdev: pci device.
  330. * @ent: ent
  331. * @shost: scsi host struct which has been initialized before.
  332. */
  333. static struct pm8001_hba_info *__devinit
  334. pm8001_pci_alloc(struct pci_dev *pdev, u32 chip_id, struct Scsi_Host *shost)
  335. {
  336. struct pm8001_hba_info *pm8001_ha;
  337. struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
  338. pm8001_ha = sha->lldd_ha;
  339. if (!pm8001_ha)
  340. return NULL;
  341. pm8001_ha->pdev = pdev;
  342. pm8001_ha->dev = &pdev->dev;
  343. pm8001_ha->chip_id = chip_id;
  344. pm8001_ha->chip = &pm8001_chips[pm8001_ha->chip_id];
  345. pm8001_ha->irq = pdev->irq;
  346. pm8001_ha->sas = sha;
  347. pm8001_ha->shost = shost;
  348. pm8001_ha->id = pm8001_id++;
  349. pm8001_ha->logging_level = 0x01;
  350. sprintf(pm8001_ha->name, "%s%d", DRV_NAME, pm8001_ha->id);
  351. #ifdef PM8001_USE_TASKLET
  352. tasklet_init(&pm8001_ha->tasklet, pm8001_tasklet,
  353. (unsigned long)pm8001_ha);
  354. #endif
  355. pm8001_ioremap(pm8001_ha);
  356. if (!pm8001_alloc(pm8001_ha))
  357. return pm8001_ha;
  358. pm8001_free(pm8001_ha);
  359. return NULL;
  360. }
  361. /**
  362. * pci_go_44 - pm8001 specified, its DMA is 44 bit rather than 64 bit
  363. * @pdev: pci device.
  364. */
  365. static int pci_go_44(struct pci_dev *pdev)
  366. {
  367. int rc;
  368. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(44))) {
  369. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(44));
  370. if (rc) {
  371. rc = pci_set_consistent_dma_mask(pdev,
  372. DMA_BIT_MASK(32));
  373. if (rc) {
  374. dev_printk(KERN_ERR, &pdev->dev,
  375. "44-bit DMA enable failed\n");
  376. return rc;
  377. }
  378. }
  379. } else {
  380. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  381. if (rc) {
  382. dev_printk(KERN_ERR, &pdev->dev,
  383. "32-bit DMA enable failed\n");
  384. return rc;
  385. }
  386. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  387. if (rc) {
  388. dev_printk(KERN_ERR, &pdev->dev,
  389. "32-bit consistent DMA enable failed\n");
  390. return rc;
  391. }
  392. }
  393. return rc;
  394. }
  395. /**
  396. * pm8001_prep_sas_ha_init - allocate memory in general hba struct && init them.
  397. * @shost: scsi host which has been allocated outside.
  398. * @chip_info: our ha struct.
  399. */
  400. static int __devinit pm8001_prep_sas_ha_init(struct Scsi_Host * shost,
  401. const struct pm8001_chip_info *chip_info)
  402. {
  403. int phy_nr, port_nr;
  404. struct asd_sas_phy **arr_phy;
  405. struct asd_sas_port **arr_port;
  406. struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
  407. phy_nr = chip_info->n_phy;
  408. port_nr = phy_nr;
  409. memset(sha, 0x00, sizeof(*sha));
  410. arr_phy = kcalloc(phy_nr, sizeof(void *), GFP_KERNEL);
  411. if (!arr_phy)
  412. goto exit;
  413. arr_port = kcalloc(port_nr, sizeof(void *), GFP_KERNEL);
  414. if (!arr_port)
  415. goto exit_free2;
  416. sha->sas_phy = arr_phy;
  417. sha->sas_port = arr_port;
  418. sha->lldd_ha = kzalloc(sizeof(struct pm8001_hba_info), GFP_KERNEL);
  419. if (!sha->lldd_ha)
  420. goto exit_free1;
  421. shost->transportt = pm8001_stt;
  422. shost->max_id = PM8001_MAX_DEVICES;
  423. shost->max_lun = 8;
  424. shost->max_channel = 0;
  425. shost->unique_id = pm8001_id;
  426. shost->max_cmd_len = 16;
  427. shost->can_queue = PM8001_CAN_QUEUE;
  428. shost->cmd_per_lun = 32;
  429. return 0;
  430. exit_free1:
  431. kfree(arr_port);
  432. exit_free2:
  433. kfree(arr_phy);
  434. exit:
  435. return -1;
  436. }
  437. /**
  438. * pm8001_post_sas_ha_init - initialize general hba struct defined in libsas
  439. * @shost: scsi host which has been allocated outside
  440. * @chip_info: our ha struct.
  441. */
  442. static void __devinit pm8001_post_sas_ha_init(struct Scsi_Host *shost,
  443. const struct pm8001_chip_info *chip_info)
  444. {
  445. int i = 0;
  446. struct pm8001_hba_info *pm8001_ha;
  447. struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
  448. pm8001_ha = sha->lldd_ha;
  449. for (i = 0; i < chip_info->n_phy; i++) {
  450. sha->sas_phy[i] = &pm8001_ha->phy[i].sas_phy;
  451. sha->sas_port[i] = &pm8001_ha->port[i].sas_port;
  452. }
  453. sha->sas_ha_name = DRV_NAME;
  454. sha->dev = pm8001_ha->dev;
  455. sha->lldd_module = THIS_MODULE;
  456. sha->sas_addr = &pm8001_ha->sas_addr[0];
  457. sha->num_phys = chip_info->n_phy;
  458. sha->lldd_max_execute_num = 1;
  459. sha->lldd_queue_size = PM8001_CAN_QUEUE;
  460. sha->core.shost = shost;
  461. }
  462. /**
  463. * pm8001_init_sas_add - initialize sas address
  464. * @chip_info: our ha struct.
  465. *
  466. * Currently we just set the fixed SAS address to our HBA,for manufacture,
  467. * it should read from the EEPROM
  468. */
  469. static void pm8001_init_sas_add(struct pm8001_hba_info *pm8001_ha)
  470. {
  471. u8 i;
  472. #ifdef PM8001_READ_VPD
  473. DECLARE_COMPLETION_ONSTACK(completion);
  474. struct pm8001_ioctl_payload payload;
  475. pm8001_ha->nvmd_completion = &completion;
  476. payload.minor_function = 0;
  477. payload.length = 128;
  478. payload.func_specific = kzalloc(128, GFP_KERNEL);
  479. PM8001_CHIP_DISP->get_nvmd_req(pm8001_ha, &payload);
  480. wait_for_completion(&completion);
  481. for (i = 0; i < pm8001_ha->chip->n_phy; i++) {
  482. memcpy(&pm8001_ha->phy[i].dev_sas_addr, pm8001_ha->sas_addr,
  483. SAS_ADDR_SIZE);
  484. PM8001_INIT_DBG(pm8001_ha,
  485. pm8001_printk("phy %d sas_addr = %016llx \n", i,
  486. pm8001_ha->phy[i].dev_sas_addr));
  487. }
  488. #else
  489. for (i = 0; i < pm8001_ha->chip->n_phy; i++) {
  490. pm8001_ha->phy[i].dev_sas_addr = 0x50010c600047f9d0ULL;
  491. pm8001_ha->phy[i].dev_sas_addr =
  492. cpu_to_be64((u64)
  493. (*(u64 *)&pm8001_ha->phy[i].dev_sas_addr));
  494. }
  495. memcpy(pm8001_ha->sas_addr, &pm8001_ha->phy[0].dev_sas_addr,
  496. SAS_ADDR_SIZE);
  497. #endif
  498. }
  499. #ifdef PM8001_USE_MSIX
  500. /**
  501. * pm8001_setup_msix - enable MSI-X interrupt
  502. * @chip_info: our ha struct.
  503. * @irq_handler: irq_handler
  504. */
  505. static u32 pm8001_setup_msix(struct pm8001_hba_info *pm8001_ha,
  506. irq_handler_t irq_handler)
  507. {
  508. u32 i = 0, j = 0;
  509. u32 number_of_intr = 1;
  510. int flag = 0;
  511. u32 max_entry;
  512. int rc;
  513. max_entry = sizeof(pm8001_ha->msix_entries) /
  514. sizeof(pm8001_ha->msix_entries[0]);
  515. flag |= IRQF_DISABLED;
  516. for (i = 0; i < max_entry ; i++)
  517. pm8001_ha->msix_entries[i].entry = i;
  518. rc = pci_enable_msix(pm8001_ha->pdev, pm8001_ha->msix_entries,
  519. number_of_intr);
  520. pm8001_ha->number_of_intr = number_of_intr;
  521. if (!rc) {
  522. for (i = 0; i < number_of_intr; i++) {
  523. if (request_irq(pm8001_ha->msix_entries[i].vector,
  524. irq_handler, flag, DRV_NAME,
  525. SHOST_TO_SAS_HA(pm8001_ha->shost))) {
  526. for (j = 0; j < i; j++)
  527. free_irq(
  528. pm8001_ha->msix_entries[j].vector,
  529. SHOST_TO_SAS_HA(pm8001_ha->shost));
  530. pci_disable_msix(pm8001_ha->pdev);
  531. break;
  532. }
  533. }
  534. }
  535. return rc;
  536. }
  537. #endif
  538. /**
  539. * pm8001_request_irq - register interrupt
  540. * @chip_info: our ha struct.
  541. */
  542. static u32 pm8001_request_irq(struct pm8001_hba_info *pm8001_ha)
  543. {
  544. struct pci_dev *pdev;
  545. irq_handler_t irq_handler = pm8001_interrupt;
  546. int rc;
  547. pdev = pm8001_ha->pdev;
  548. #ifdef PM8001_USE_MSIX
  549. if (pci_find_capability(pdev, PCI_CAP_ID_MSIX))
  550. return pm8001_setup_msix(pm8001_ha, irq_handler);
  551. else
  552. goto intx;
  553. #endif
  554. intx:
  555. /* initialize the INT-X interrupt */
  556. rc = request_irq(pdev->irq, irq_handler, IRQF_SHARED, DRV_NAME,
  557. SHOST_TO_SAS_HA(pm8001_ha->shost));
  558. return rc;
  559. }
  560. /**
  561. * pm8001_pci_probe - probe supported device
  562. * @pdev: pci device which kernel has been prepared for.
  563. * @ent: pci device id
  564. *
  565. * This function is the main initialization function, when register a new
  566. * pci driver it is invoked, all struct an hardware initilization should be done
  567. * here, also, register interrupt
  568. */
  569. static int __devinit pm8001_pci_probe(struct pci_dev *pdev,
  570. const struct pci_device_id *ent)
  571. {
  572. unsigned int rc;
  573. u32 pci_reg;
  574. struct pm8001_hba_info *pm8001_ha;
  575. struct Scsi_Host *shost = NULL;
  576. const struct pm8001_chip_info *chip;
  577. dev_printk(KERN_INFO, &pdev->dev,
  578. "pm8001: driver version %s\n", DRV_VERSION);
  579. rc = pci_enable_device(pdev);
  580. if (rc)
  581. goto err_out_enable;
  582. pci_set_master(pdev);
  583. /*
  584. * Enable pci slot busmaster by setting pci command register.
  585. * This is required by FW for Cyclone card.
  586. */
  587. pci_read_config_dword(pdev, PCI_COMMAND, &pci_reg);
  588. pci_reg |= 0x157;
  589. pci_write_config_dword(pdev, PCI_COMMAND, pci_reg);
  590. rc = pci_request_regions(pdev, DRV_NAME);
  591. if (rc)
  592. goto err_out_disable;
  593. rc = pci_go_44(pdev);
  594. if (rc)
  595. goto err_out_regions;
  596. shost = scsi_host_alloc(&pm8001_sht, sizeof(void *));
  597. if (!shost) {
  598. rc = -ENOMEM;
  599. goto err_out_regions;
  600. }
  601. chip = &pm8001_chips[ent->driver_data];
  602. SHOST_TO_SAS_HA(shost) =
  603. kzalloc(sizeof(struct sas_ha_struct), GFP_KERNEL);
  604. if (!SHOST_TO_SAS_HA(shost)) {
  605. rc = -ENOMEM;
  606. goto err_out_free_host;
  607. }
  608. rc = pm8001_prep_sas_ha_init(shost, chip);
  609. if (rc) {
  610. rc = -ENOMEM;
  611. goto err_out_free;
  612. }
  613. pci_set_drvdata(pdev, SHOST_TO_SAS_HA(shost));
  614. pm8001_ha = pm8001_pci_alloc(pdev, chip_8001, shost);
  615. if (!pm8001_ha) {
  616. rc = -ENOMEM;
  617. goto err_out_free;
  618. }
  619. list_add_tail(&pm8001_ha->list, &hba_list);
  620. PM8001_CHIP_DISP->chip_soft_rst(pm8001_ha, 0x252acbcd);
  621. rc = PM8001_CHIP_DISP->chip_init(pm8001_ha);
  622. if (rc)
  623. goto err_out_ha_free;
  624. rc = scsi_add_host(shost, &pdev->dev);
  625. if (rc)
  626. goto err_out_ha_free;
  627. rc = pm8001_request_irq(pm8001_ha);
  628. if (rc)
  629. goto err_out_shost;
  630. PM8001_CHIP_DISP->interrupt_enable(pm8001_ha);
  631. pm8001_init_sas_add(pm8001_ha);
  632. pm8001_post_sas_ha_init(shost, chip);
  633. rc = sas_register_ha(SHOST_TO_SAS_HA(shost));
  634. if (rc)
  635. goto err_out_shost;
  636. scsi_scan_host(pm8001_ha->shost);
  637. return 0;
  638. err_out_shost:
  639. scsi_remove_host(pm8001_ha->shost);
  640. err_out_ha_free:
  641. pm8001_free(pm8001_ha);
  642. err_out_free:
  643. kfree(SHOST_TO_SAS_HA(shost));
  644. err_out_free_host:
  645. kfree(shost);
  646. err_out_regions:
  647. pci_release_regions(pdev);
  648. err_out_disable:
  649. pci_disable_device(pdev);
  650. err_out_enable:
  651. return rc;
  652. }
  653. static void __devexit pm8001_pci_remove(struct pci_dev *pdev)
  654. {
  655. struct sas_ha_struct *sha = pci_get_drvdata(pdev);
  656. struct pm8001_hba_info *pm8001_ha;
  657. int i;
  658. pm8001_ha = sha->lldd_ha;
  659. pci_set_drvdata(pdev, NULL);
  660. sas_unregister_ha(sha);
  661. sas_remove_host(pm8001_ha->shost);
  662. list_del(&pm8001_ha->list);
  663. scsi_remove_host(pm8001_ha->shost);
  664. PM8001_CHIP_DISP->interrupt_disable(pm8001_ha);
  665. PM8001_CHIP_DISP->chip_soft_rst(pm8001_ha, 0x252acbcd);
  666. #ifdef PM8001_USE_MSIX
  667. for (i = 0; i < pm8001_ha->number_of_intr; i++)
  668. synchronize_irq(pm8001_ha->msix_entries[i].vector);
  669. for (i = 0; i < pm8001_ha->number_of_intr; i++)
  670. free_irq(pm8001_ha->msix_entries[i].vector, sha);
  671. pci_disable_msix(pdev);
  672. #else
  673. free_irq(pm8001_ha->irq, sha);
  674. #endif
  675. #ifdef PM8001_USE_TASKLET
  676. tasklet_kill(&pm8001_ha->tasklet);
  677. #endif
  678. pm8001_free(pm8001_ha);
  679. kfree(sha->sas_phy);
  680. kfree(sha->sas_port);
  681. kfree(sha);
  682. pci_release_regions(pdev);
  683. pci_disable_device(pdev);
  684. }
  685. /**
  686. * pm8001_pci_suspend - power management suspend main entry point
  687. * @pdev: PCI device struct
  688. * @state: PM state change to (usually PCI_D3)
  689. *
  690. * Returns 0 success, anything else error.
  691. */
  692. static int pm8001_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  693. {
  694. struct sas_ha_struct *sha = pci_get_drvdata(pdev);
  695. struct pm8001_hba_info *pm8001_ha;
  696. int i , pos;
  697. u32 device_state;
  698. pm8001_ha = sha->lldd_ha;
  699. flush_workqueue(pm8001_wq);
  700. scsi_block_requests(pm8001_ha->shost);
  701. pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
  702. if (pos == 0) {
  703. printk(KERN_ERR " PCI PM not supported\n");
  704. return -ENODEV;
  705. }
  706. PM8001_CHIP_DISP->interrupt_disable(pm8001_ha);
  707. PM8001_CHIP_DISP->chip_soft_rst(pm8001_ha, 0x252acbcd);
  708. #ifdef PM8001_USE_MSIX
  709. for (i = 0; i < pm8001_ha->number_of_intr; i++)
  710. synchronize_irq(pm8001_ha->msix_entries[i].vector);
  711. for (i = 0; i < pm8001_ha->number_of_intr; i++)
  712. free_irq(pm8001_ha->msix_entries[i].vector, sha);
  713. pci_disable_msix(pdev);
  714. #else
  715. free_irq(pm8001_ha->irq, sha);
  716. #endif
  717. #ifdef PM8001_USE_TASKLET
  718. tasklet_kill(&pm8001_ha->tasklet);
  719. #endif
  720. device_state = pci_choose_state(pdev, state);
  721. pm8001_printk("pdev=0x%p, slot=%s, entering "
  722. "operating state [D%d]\n", pdev,
  723. pm8001_ha->name, device_state);
  724. pci_save_state(pdev);
  725. pci_disable_device(pdev);
  726. pci_set_power_state(pdev, device_state);
  727. return 0;
  728. }
  729. /**
  730. * pm8001_pci_resume - power management resume main entry point
  731. * @pdev: PCI device struct
  732. *
  733. * Returns 0 success, anything else error.
  734. */
  735. static int pm8001_pci_resume(struct pci_dev *pdev)
  736. {
  737. struct sas_ha_struct *sha = pci_get_drvdata(pdev);
  738. struct pm8001_hba_info *pm8001_ha;
  739. int rc;
  740. u32 device_state;
  741. pm8001_ha = sha->lldd_ha;
  742. device_state = pdev->current_state;
  743. pm8001_printk("pdev=0x%p, slot=%s, resuming from previous "
  744. "operating state [D%d]\n", pdev, pm8001_ha->name, device_state);
  745. pci_set_power_state(pdev, PCI_D0);
  746. pci_enable_wake(pdev, PCI_D0, 0);
  747. pci_restore_state(pdev);
  748. rc = pci_enable_device(pdev);
  749. if (rc) {
  750. pm8001_printk("slot=%s Enable device failed during resume\n",
  751. pm8001_ha->name);
  752. goto err_out_enable;
  753. }
  754. pci_set_master(pdev);
  755. rc = pci_go_44(pdev);
  756. if (rc)
  757. goto err_out_disable;
  758. PM8001_CHIP_DISP->chip_soft_rst(pm8001_ha, 0x252acbcd);
  759. rc = PM8001_CHIP_DISP->chip_init(pm8001_ha);
  760. if (rc)
  761. goto err_out_disable;
  762. PM8001_CHIP_DISP->interrupt_disable(pm8001_ha);
  763. rc = pm8001_request_irq(pm8001_ha);
  764. if (rc)
  765. goto err_out_disable;
  766. #ifdef PM8001_USE_TASKLET
  767. tasklet_init(&pm8001_ha->tasklet, pm8001_tasklet,
  768. (unsigned long)pm8001_ha);
  769. #endif
  770. PM8001_CHIP_DISP->interrupt_enable(pm8001_ha);
  771. scsi_unblock_requests(pm8001_ha->shost);
  772. return 0;
  773. err_out_disable:
  774. scsi_remove_host(pm8001_ha->shost);
  775. pci_disable_device(pdev);
  776. err_out_enable:
  777. return rc;
  778. }
  779. static struct pci_device_id __devinitdata pm8001_pci_table[] = {
  780. {
  781. PCI_VDEVICE(PMC_Sierra, 0x8001), chip_8001
  782. },
  783. {
  784. PCI_DEVICE(0x117c, 0x0042),
  785. .driver_data = chip_8001
  786. },
  787. {} /* terminate list */
  788. };
  789. static struct pci_driver pm8001_pci_driver = {
  790. .name = DRV_NAME,
  791. .id_table = pm8001_pci_table,
  792. .probe = pm8001_pci_probe,
  793. .remove = __devexit_p(pm8001_pci_remove),
  794. .suspend = pm8001_pci_suspend,
  795. .resume = pm8001_pci_resume,
  796. };
  797. /**
  798. * pm8001_init - initialize scsi transport template
  799. */
  800. static int __init pm8001_init(void)
  801. {
  802. int rc = -ENOMEM;
  803. pm8001_wq = alloc_workqueue("pm8001", 0, 0);
  804. if (!pm8001_wq)
  805. goto err;
  806. pm8001_id = 0;
  807. pm8001_stt = sas_domain_attach_transport(&pm8001_transport_ops);
  808. if (!pm8001_stt)
  809. goto err_wq;
  810. rc = pci_register_driver(&pm8001_pci_driver);
  811. if (rc)
  812. goto err_tp;
  813. return 0;
  814. err_tp:
  815. sas_release_transport(pm8001_stt);
  816. err_wq:
  817. destroy_workqueue(pm8001_wq);
  818. err:
  819. return rc;
  820. }
  821. static void __exit pm8001_exit(void)
  822. {
  823. pci_unregister_driver(&pm8001_pci_driver);
  824. sas_release_transport(pm8001_stt);
  825. destroy_workqueue(pm8001_wq);
  826. }
  827. module_init(pm8001_init);
  828. module_exit(pm8001_exit);
  829. MODULE_AUTHOR("Jack Wang <jack_wang@usish.com>");
  830. MODULE_DESCRIPTION("PMC-Sierra PM8001 SAS/SATA controller driver");
  831. MODULE_VERSION(DRV_VERSION);
  832. MODULE_LICENSE("GPL");
  833. MODULE_DEVICE_TABLE(pci, pm8001_pci_table);