pm8001_init.c 27 KB

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