pm8001_init.c 26 KB

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