pm8001_init.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  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, 0);
  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, 0);
  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. const struct pci_device_id *ent)
  187. {
  188. int i;
  189. spin_lock_init(&pm8001_ha->lock);
  190. PM8001_INIT_DBG(pm8001_ha,
  191. pm8001_printk("pm8001_alloc: PHY:%x\n",
  192. pm8001_ha->chip->n_phy));
  193. for (i = 0; i < pm8001_ha->chip->n_phy; i++) {
  194. pm8001_phy_init(pm8001_ha, i);
  195. pm8001_ha->port[i].wide_port_phymap = 0;
  196. pm8001_ha->port[i].port_attached = 0;
  197. pm8001_ha->port[i].port_state = 0;
  198. INIT_LIST_HEAD(&pm8001_ha->port[i].list);
  199. }
  200. pm8001_ha->tags = kzalloc(PM8001_MAX_CCB, GFP_KERNEL);
  201. if (!pm8001_ha->tags)
  202. goto err_out;
  203. /* MPI Memory region 1 for AAP Event Log for fw */
  204. pm8001_ha->memoryMap.region[AAP1].num_elements = 1;
  205. pm8001_ha->memoryMap.region[AAP1].element_size = PM8001_EVENT_LOG_SIZE;
  206. pm8001_ha->memoryMap.region[AAP1].total_len = PM8001_EVENT_LOG_SIZE;
  207. pm8001_ha->memoryMap.region[AAP1].alignment = 32;
  208. /* MPI Memory region 2 for IOP Event Log for fw */
  209. pm8001_ha->memoryMap.region[IOP].num_elements = 1;
  210. pm8001_ha->memoryMap.region[IOP].element_size = PM8001_EVENT_LOG_SIZE;
  211. pm8001_ha->memoryMap.region[IOP].total_len = PM8001_EVENT_LOG_SIZE;
  212. pm8001_ha->memoryMap.region[IOP].alignment = 32;
  213. for (i = 0; i < PM8001_MAX_SPCV_INB_NUM; i++) {
  214. /* MPI Memory region 3 for consumer Index of inbound queues */
  215. pm8001_ha->memoryMap.region[CI+i].num_elements = 1;
  216. pm8001_ha->memoryMap.region[CI+i].element_size = 4;
  217. pm8001_ha->memoryMap.region[CI+i].total_len = 4;
  218. pm8001_ha->memoryMap.region[CI+i].alignment = 4;
  219. if ((ent->driver_data) != chip_8001) {
  220. /* MPI Memory region 5 inbound queues */
  221. pm8001_ha->memoryMap.region[IB+i].num_elements =
  222. PM8001_MPI_QUEUE;
  223. pm8001_ha->memoryMap.region[IB+i].element_size = 128;
  224. pm8001_ha->memoryMap.region[IB+i].total_len =
  225. PM8001_MPI_QUEUE * 128;
  226. pm8001_ha->memoryMap.region[IB+i].alignment = 128;
  227. } else {
  228. pm8001_ha->memoryMap.region[IB+i].num_elements =
  229. PM8001_MPI_QUEUE;
  230. pm8001_ha->memoryMap.region[IB+i].element_size = 64;
  231. pm8001_ha->memoryMap.region[IB+i].total_len =
  232. PM8001_MPI_QUEUE * 64;
  233. pm8001_ha->memoryMap.region[IB+i].alignment = 64;
  234. }
  235. }
  236. for (i = 0; i < PM8001_MAX_SPCV_OUTB_NUM; i++) {
  237. /* MPI Memory region 4 for producer Index of outbound queues */
  238. pm8001_ha->memoryMap.region[PI+i].num_elements = 1;
  239. pm8001_ha->memoryMap.region[PI+i].element_size = 4;
  240. pm8001_ha->memoryMap.region[PI+i].total_len = 4;
  241. pm8001_ha->memoryMap.region[PI+i].alignment = 4;
  242. if (ent->driver_data != chip_8001) {
  243. /* MPI Memory region 6 Outbound queues */
  244. pm8001_ha->memoryMap.region[OB+i].num_elements =
  245. PM8001_MPI_QUEUE;
  246. pm8001_ha->memoryMap.region[OB+i].element_size = 128;
  247. pm8001_ha->memoryMap.region[OB+i].total_len =
  248. PM8001_MPI_QUEUE * 128;
  249. pm8001_ha->memoryMap.region[OB+i].alignment = 128;
  250. } else {
  251. /* MPI Memory region 6 Outbound queues */
  252. pm8001_ha->memoryMap.region[OB+i].num_elements =
  253. PM8001_MPI_QUEUE;
  254. pm8001_ha->memoryMap.region[OB+i].element_size = 64;
  255. pm8001_ha->memoryMap.region[OB+i].total_len =
  256. PM8001_MPI_QUEUE * 64;
  257. pm8001_ha->memoryMap.region[OB+i].alignment = 64;
  258. }
  259. }
  260. /* Memory region write DMA*/
  261. pm8001_ha->memoryMap.region[NVMD].num_elements = 1;
  262. pm8001_ha->memoryMap.region[NVMD].element_size = 4096;
  263. pm8001_ha->memoryMap.region[NVMD].total_len = 4096;
  264. /* Memory region for devices*/
  265. pm8001_ha->memoryMap.region[DEV_MEM].num_elements = 1;
  266. pm8001_ha->memoryMap.region[DEV_MEM].element_size = PM8001_MAX_DEVICES *
  267. sizeof(struct pm8001_device);
  268. pm8001_ha->memoryMap.region[DEV_MEM].total_len = PM8001_MAX_DEVICES *
  269. sizeof(struct pm8001_device);
  270. /* Memory region for ccb_info*/
  271. pm8001_ha->memoryMap.region[CCB_MEM].num_elements = 1;
  272. pm8001_ha->memoryMap.region[CCB_MEM].element_size = PM8001_MAX_CCB *
  273. sizeof(struct pm8001_ccb_info);
  274. pm8001_ha->memoryMap.region[CCB_MEM].total_len = PM8001_MAX_CCB *
  275. sizeof(struct pm8001_ccb_info);
  276. for (i = 0; i < USI_MAX_MEMCNT; i++) {
  277. if (pm8001_mem_alloc(pm8001_ha->pdev,
  278. &pm8001_ha->memoryMap.region[i].virt_ptr,
  279. &pm8001_ha->memoryMap.region[i].phys_addr,
  280. &pm8001_ha->memoryMap.region[i].phys_addr_hi,
  281. &pm8001_ha->memoryMap.region[i].phys_addr_lo,
  282. pm8001_ha->memoryMap.region[i].total_len,
  283. pm8001_ha->memoryMap.region[i].alignment) != 0) {
  284. PM8001_FAIL_DBG(pm8001_ha,
  285. pm8001_printk("Mem%d alloc failed\n",
  286. i));
  287. goto err_out;
  288. }
  289. }
  290. pm8001_ha->devices = pm8001_ha->memoryMap.region[DEV_MEM].virt_ptr;
  291. for (i = 0; i < PM8001_MAX_DEVICES; i++) {
  292. pm8001_ha->devices[i].dev_type = NO_DEVICE;
  293. pm8001_ha->devices[i].id = i;
  294. pm8001_ha->devices[i].device_id = PM8001_MAX_DEVICES;
  295. pm8001_ha->devices[i].running_req = 0;
  296. }
  297. pm8001_ha->ccb_info = pm8001_ha->memoryMap.region[CCB_MEM].virt_ptr;
  298. for (i = 0; i < PM8001_MAX_CCB; i++) {
  299. pm8001_ha->ccb_info[i].ccb_dma_handle =
  300. pm8001_ha->memoryMap.region[CCB_MEM].phys_addr +
  301. i * sizeof(struct pm8001_ccb_info);
  302. pm8001_ha->ccb_info[i].task = NULL;
  303. pm8001_ha->ccb_info[i].ccb_tag = 0xffffffff;
  304. pm8001_ha->ccb_info[i].device = NULL;
  305. ++pm8001_ha->tags_num;
  306. }
  307. pm8001_ha->flags = PM8001F_INIT_TIME;
  308. /* Initialize tags */
  309. pm8001_tag_init(pm8001_ha);
  310. return 0;
  311. err_out:
  312. return 1;
  313. }
  314. /**
  315. * pm8001_ioremap - remap the pci high physical address to kernal virtual
  316. * address so that we can access them.
  317. * @pm8001_ha:our hba structure.
  318. */
  319. static int pm8001_ioremap(struct pm8001_hba_info *pm8001_ha)
  320. {
  321. u32 bar;
  322. u32 logicalBar = 0;
  323. struct pci_dev *pdev;
  324. pdev = pm8001_ha->pdev;
  325. /* map pci mem (PMC pci base 0-3)*/
  326. for (bar = 0; bar < 6; bar++) {
  327. /*
  328. ** logical BARs for SPC:
  329. ** bar 0 and 1 - logical BAR0
  330. ** bar 2 and 3 - logical BAR1
  331. ** bar4 - logical BAR2
  332. ** bar5 - logical BAR3
  333. ** Skip the appropriate assignments:
  334. */
  335. if ((bar == 1) || (bar == 3))
  336. continue;
  337. if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
  338. pm8001_ha->io_mem[logicalBar].membase =
  339. pci_resource_start(pdev, bar);
  340. pm8001_ha->io_mem[logicalBar].membase &=
  341. (u32)PCI_BASE_ADDRESS_MEM_MASK;
  342. pm8001_ha->io_mem[logicalBar].memsize =
  343. pci_resource_len(pdev, bar);
  344. pm8001_ha->io_mem[logicalBar].memvirtaddr =
  345. ioremap(pm8001_ha->io_mem[logicalBar].membase,
  346. pm8001_ha->io_mem[logicalBar].memsize);
  347. PM8001_INIT_DBG(pm8001_ha,
  348. pm8001_printk("PCI: bar %d, logicalBar %d ",
  349. bar, logicalBar));
  350. PM8001_INIT_DBG(pm8001_ha, pm8001_printk(
  351. "base addr %llx virt_addr=%llx len=%d\n",
  352. (u64)pm8001_ha->io_mem[logicalBar].membase,
  353. (u64)pm8001_ha->io_mem[logicalBar].memvirtaddr,
  354. pm8001_ha->io_mem[logicalBar].memsize));
  355. } else {
  356. pm8001_ha->io_mem[logicalBar].membase = 0;
  357. pm8001_ha->io_mem[logicalBar].memsize = 0;
  358. pm8001_ha->io_mem[logicalBar].memvirtaddr = 0;
  359. }
  360. logicalBar++;
  361. }
  362. return 0;
  363. }
  364. /**
  365. * pm8001_pci_alloc - initialize our ha card structure
  366. * @pdev: pci device.
  367. * @ent: ent
  368. * @shost: scsi host struct which has been initialized before.
  369. */
  370. static struct pm8001_hba_info *pm8001_pci_alloc(struct pci_dev *pdev,
  371. const struct pci_device_id *ent,
  372. struct Scsi_Host *shost)
  373. {
  374. struct pm8001_hba_info *pm8001_ha;
  375. struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
  376. pm8001_ha = sha->lldd_ha;
  377. if (!pm8001_ha)
  378. return NULL;
  379. pm8001_ha->pdev = pdev;
  380. pm8001_ha->dev = &pdev->dev;
  381. pm8001_ha->chip_id = ent->driver_data;
  382. pm8001_ha->chip = &pm8001_chips[pm8001_ha->chip_id];
  383. pm8001_ha->irq = pdev->irq;
  384. pm8001_ha->sas = sha;
  385. pm8001_ha->shost = shost;
  386. pm8001_ha->id = pm8001_id++;
  387. pm8001_ha->logging_level = 0x01;
  388. sprintf(pm8001_ha->name, "%s%d", DRV_NAME, pm8001_ha->id);
  389. /* IOMB size is 128 for 8088/89 controllers */
  390. if (pm8001_ha->chip_id != chip_8001)
  391. pm8001_ha->iomb_size = IOMB_SIZE_SPCV;
  392. else
  393. pm8001_ha->iomb_size = IOMB_SIZE_SPC;
  394. #ifdef PM8001_USE_TASKLET
  395. tasklet_init(&pm8001_ha->tasklet, pm8001_tasklet,
  396. (unsigned long)pm8001_ha);
  397. #endif
  398. pm8001_ioremap(pm8001_ha);
  399. if (!pm8001_alloc(pm8001_ha, ent))
  400. return pm8001_ha;
  401. pm8001_free(pm8001_ha);
  402. return NULL;
  403. }
  404. /**
  405. * pci_go_44 - pm8001 specified, its DMA is 44 bit rather than 64 bit
  406. * @pdev: pci device.
  407. */
  408. static int pci_go_44(struct pci_dev *pdev)
  409. {
  410. int rc;
  411. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(44))) {
  412. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(44));
  413. if (rc) {
  414. rc = pci_set_consistent_dma_mask(pdev,
  415. DMA_BIT_MASK(32));
  416. if (rc) {
  417. dev_printk(KERN_ERR, &pdev->dev,
  418. "44-bit DMA enable failed\n");
  419. return rc;
  420. }
  421. }
  422. } else {
  423. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  424. if (rc) {
  425. dev_printk(KERN_ERR, &pdev->dev,
  426. "32-bit DMA enable failed\n");
  427. return rc;
  428. }
  429. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  430. if (rc) {
  431. dev_printk(KERN_ERR, &pdev->dev,
  432. "32-bit consistent DMA enable failed\n");
  433. return rc;
  434. }
  435. }
  436. return rc;
  437. }
  438. /**
  439. * pm8001_prep_sas_ha_init - allocate memory in general hba struct && init them.
  440. * @shost: scsi host which has been allocated outside.
  441. * @chip_info: our ha struct.
  442. */
  443. static int pm8001_prep_sas_ha_init(struct Scsi_Host *shost,
  444. const struct pm8001_chip_info *chip_info)
  445. {
  446. int phy_nr, port_nr;
  447. struct asd_sas_phy **arr_phy;
  448. struct asd_sas_port **arr_port;
  449. struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
  450. phy_nr = chip_info->n_phy;
  451. port_nr = phy_nr;
  452. memset(sha, 0x00, sizeof(*sha));
  453. arr_phy = kcalloc(phy_nr, sizeof(void *), GFP_KERNEL);
  454. if (!arr_phy)
  455. goto exit;
  456. arr_port = kcalloc(port_nr, sizeof(void *), GFP_KERNEL);
  457. if (!arr_port)
  458. goto exit_free2;
  459. sha->sas_phy = arr_phy;
  460. sha->sas_port = arr_port;
  461. sha->lldd_ha = kzalloc(sizeof(struct pm8001_hba_info), GFP_KERNEL);
  462. if (!sha->lldd_ha)
  463. goto exit_free1;
  464. shost->transportt = pm8001_stt;
  465. shost->max_id = PM8001_MAX_DEVICES;
  466. shost->max_lun = 8;
  467. shost->max_channel = 0;
  468. shost->unique_id = pm8001_id;
  469. shost->max_cmd_len = 16;
  470. shost->can_queue = PM8001_CAN_QUEUE;
  471. shost->cmd_per_lun = 32;
  472. return 0;
  473. exit_free1:
  474. kfree(arr_port);
  475. exit_free2:
  476. kfree(arr_phy);
  477. exit:
  478. return -1;
  479. }
  480. /**
  481. * pm8001_post_sas_ha_init - initialize general hba struct defined in libsas
  482. * @shost: scsi host which has been allocated outside
  483. * @chip_info: our ha struct.
  484. */
  485. static void pm8001_post_sas_ha_init(struct Scsi_Host *shost,
  486. const struct pm8001_chip_info *chip_info)
  487. {
  488. int i = 0;
  489. struct pm8001_hba_info *pm8001_ha;
  490. struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
  491. pm8001_ha = sha->lldd_ha;
  492. for (i = 0; i < chip_info->n_phy; i++) {
  493. sha->sas_phy[i] = &pm8001_ha->phy[i].sas_phy;
  494. sha->sas_port[i] = &pm8001_ha->port[i].sas_port;
  495. }
  496. sha->sas_ha_name = DRV_NAME;
  497. sha->dev = pm8001_ha->dev;
  498. sha->lldd_module = THIS_MODULE;
  499. sha->sas_addr = &pm8001_ha->sas_addr[0];
  500. sha->num_phys = chip_info->n_phy;
  501. sha->lldd_max_execute_num = 1;
  502. sha->lldd_queue_size = PM8001_CAN_QUEUE;
  503. sha->core.shost = shost;
  504. }
  505. /**
  506. * pm8001_init_sas_add - initialize sas address
  507. * @chip_info: our ha struct.
  508. *
  509. * Currently we just set the fixed SAS address to our HBA,for manufacture,
  510. * it should read from the EEPROM
  511. */
  512. static void pm8001_init_sas_add(struct pm8001_hba_info *pm8001_ha)
  513. {
  514. u8 i;
  515. #ifdef PM8001_READ_VPD
  516. DECLARE_COMPLETION_ONSTACK(completion);
  517. struct pm8001_ioctl_payload payload;
  518. pm8001_ha->nvmd_completion = &completion;
  519. payload.minor_function = 0;
  520. payload.length = 128;
  521. payload.func_specific = kzalloc(128, GFP_KERNEL);
  522. PM8001_CHIP_DISP->get_nvmd_req(pm8001_ha, &payload);
  523. wait_for_completion(&completion);
  524. for (i = 0; i < pm8001_ha->chip->n_phy; i++) {
  525. memcpy(&pm8001_ha->phy[i].dev_sas_addr, pm8001_ha->sas_addr,
  526. SAS_ADDR_SIZE);
  527. PM8001_INIT_DBG(pm8001_ha,
  528. pm8001_printk("phy %d sas_addr = %016llx \n", i,
  529. pm8001_ha->phy[i].dev_sas_addr));
  530. }
  531. #else
  532. for (i = 0; i < pm8001_ha->chip->n_phy; i++) {
  533. pm8001_ha->phy[i].dev_sas_addr = 0x50010c600047f9d0ULL;
  534. pm8001_ha->phy[i].dev_sas_addr =
  535. cpu_to_be64((u64)
  536. (*(u64 *)&pm8001_ha->phy[i].dev_sas_addr));
  537. }
  538. memcpy(pm8001_ha->sas_addr, &pm8001_ha->phy[0].dev_sas_addr,
  539. SAS_ADDR_SIZE);
  540. #endif
  541. }
  542. #ifdef PM8001_USE_MSIX
  543. /**
  544. * pm8001_setup_msix - enable MSI-X interrupt
  545. * @chip_info: our ha struct.
  546. * @irq_handler: irq_handler
  547. */
  548. static u32 pm8001_setup_msix(struct pm8001_hba_info *pm8001_ha,
  549. irq_handler_t irq_handler)
  550. {
  551. u32 i = 0, j = 0;
  552. u32 number_of_intr = 1;
  553. int flag = 0;
  554. u32 max_entry;
  555. int rc;
  556. max_entry = sizeof(pm8001_ha->msix_entries) /
  557. sizeof(pm8001_ha->msix_entries[0]);
  558. flag |= IRQF_DISABLED;
  559. for (i = 0; i < max_entry ; i++)
  560. pm8001_ha->msix_entries[i].entry = i;
  561. rc = pci_enable_msix(pm8001_ha->pdev, pm8001_ha->msix_entries,
  562. number_of_intr);
  563. pm8001_ha->number_of_intr = number_of_intr;
  564. if (!rc) {
  565. for (i = 0; i < number_of_intr; i++) {
  566. if (request_irq(pm8001_ha->msix_entries[i].vector,
  567. irq_handler, flag, DRV_NAME,
  568. SHOST_TO_SAS_HA(pm8001_ha->shost))) {
  569. for (j = 0; j < i; j++)
  570. free_irq(
  571. pm8001_ha->msix_entries[j].vector,
  572. SHOST_TO_SAS_HA(pm8001_ha->shost));
  573. pci_disable_msix(pm8001_ha->pdev);
  574. break;
  575. }
  576. }
  577. }
  578. return rc;
  579. }
  580. #endif
  581. /**
  582. * pm8001_request_irq - register interrupt
  583. * @chip_info: our ha struct.
  584. */
  585. static u32 pm8001_request_irq(struct pm8001_hba_info *pm8001_ha)
  586. {
  587. struct pci_dev *pdev;
  588. irq_handler_t irq_handler = pm8001_interrupt;
  589. int rc;
  590. pdev = pm8001_ha->pdev;
  591. #ifdef PM8001_USE_MSIX
  592. if (pci_find_capability(pdev, PCI_CAP_ID_MSIX))
  593. return pm8001_setup_msix(pm8001_ha, irq_handler);
  594. else
  595. goto intx;
  596. #endif
  597. intx:
  598. /* initialize the INT-X interrupt */
  599. rc = request_irq(pdev->irq, irq_handler, IRQF_SHARED, DRV_NAME,
  600. SHOST_TO_SAS_HA(pm8001_ha->shost));
  601. return rc;
  602. }
  603. /**
  604. * pm8001_pci_probe - probe supported device
  605. * @pdev: pci device which kernel has been prepared for.
  606. * @ent: pci device id
  607. *
  608. * This function is the main initialization function, when register a new
  609. * pci driver it is invoked, all struct an hardware initilization should be done
  610. * here, also, register interrupt
  611. */
  612. static int pm8001_pci_probe(struct pci_dev *pdev,
  613. const struct pci_device_id *ent)
  614. {
  615. unsigned int rc;
  616. u32 pci_reg;
  617. struct pm8001_hba_info *pm8001_ha;
  618. struct Scsi_Host *shost = NULL;
  619. const struct pm8001_chip_info *chip;
  620. dev_printk(KERN_INFO, &pdev->dev,
  621. "pm8001: driver version %s\n", DRV_VERSION);
  622. rc = pci_enable_device(pdev);
  623. if (rc)
  624. goto err_out_enable;
  625. pci_set_master(pdev);
  626. /*
  627. * Enable pci slot busmaster by setting pci command register.
  628. * This is required by FW for Cyclone card.
  629. */
  630. pci_read_config_dword(pdev, PCI_COMMAND, &pci_reg);
  631. pci_reg |= 0x157;
  632. pci_write_config_dword(pdev, PCI_COMMAND, pci_reg);
  633. rc = pci_request_regions(pdev, DRV_NAME);
  634. if (rc)
  635. goto err_out_disable;
  636. rc = pci_go_44(pdev);
  637. if (rc)
  638. goto err_out_regions;
  639. shost = scsi_host_alloc(&pm8001_sht, sizeof(void *));
  640. if (!shost) {
  641. rc = -ENOMEM;
  642. goto err_out_regions;
  643. }
  644. chip = &pm8001_chips[ent->driver_data];
  645. SHOST_TO_SAS_HA(shost) =
  646. kzalloc(sizeof(struct sas_ha_struct), GFP_KERNEL);
  647. if (!SHOST_TO_SAS_HA(shost)) {
  648. rc = -ENOMEM;
  649. goto err_out_free_host;
  650. }
  651. rc = pm8001_prep_sas_ha_init(shost, chip);
  652. if (rc) {
  653. rc = -ENOMEM;
  654. goto err_out_free;
  655. }
  656. pci_set_drvdata(pdev, SHOST_TO_SAS_HA(shost));
  657. /* ent->driver variable is used to differentiate between controllers */
  658. pm8001_ha = pm8001_pci_alloc(pdev, ent, shost);
  659. if (!pm8001_ha) {
  660. rc = -ENOMEM;
  661. goto err_out_free;
  662. }
  663. list_add_tail(&pm8001_ha->list, &hba_list);
  664. PM8001_CHIP_DISP->chip_soft_rst(pm8001_ha, 0x252acbcd);
  665. rc = PM8001_CHIP_DISP->chip_init(pm8001_ha);
  666. if (rc)
  667. goto err_out_ha_free;
  668. rc = scsi_add_host(shost, &pdev->dev);
  669. if (rc)
  670. goto err_out_ha_free;
  671. rc = pm8001_request_irq(pm8001_ha);
  672. if (rc)
  673. goto err_out_shost;
  674. PM8001_CHIP_DISP->interrupt_enable(pm8001_ha, 0);
  675. pm8001_init_sas_add(pm8001_ha);
  676. pm8001_post_sas_ha_init(shost, chip);
  677. rc = sas_register_ha(SHOST_TO_SAS_HA(shost));
  678. if (rc)
  679. goto err_out_shost;
  680. scsi_scan_host(pm8001_ha->shost);
  681. return 0;
  682. err_out_shost:
  683. scsi_remove_host(pm8001_ha->shost);
  684. err_out_ha_free:
  685. pm8001_free(pm8001_ha);
  686. err_out_free:
  687. kfree(SHOST_TO_SAS_HA(shost));
  688. err_out_free_host:
  689. kfree(shost);
  690. err_out_regions:
  691. pci_release_regions(pdev);
  692. err_out_disable:
  693. pci_disable_device(pdev);
  694. err_out_enable:
  695. return rc;
  696. }
  697. static void pm8001_pci_remove(struct pci_dev *pdev)
  698. {
  699. struct sas_ha_struct *sha = pci_get_drvdata(pdev);
  700. struct pm8001_hba_info *pm8001_ha;
  701. int i;
  702. pm8001_ha = sha->lldd_ha;
  703. pci_set_drvdata(pdev, NULL);
  704. sas_unregister_ha(sha);
  705. sas_remove_host(pm8001_ha->shost);
  706. list_del(&pm8001_ha->list);
  707. scsi_remove_host(pm8001_ha->shost);
  708. PM8001_CHIP_DISP->interrupt_disable(pm8001_ha, 0);
  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. pm8001_free(pm8001_ha);
  723. kfree(sha->sas_phy);
  724. kfree(sha->sas_port);
  725. kfree(sha);
  726. pci_release_regions(pdev);
  727. pci_disable_device(pdev);
  728. }
  729. /**
  730. * pm8001_pci_suspend - power management suspend main entry point
  731. * @pdev: PCI device struct
  732. * @state: PM state change to (usually PCI_D3)
  733. *
  734. * Returns 0 success, anything else error.
  735. */
  736. static int pm8001_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  737. {
  738. struct sas_ha_struct *sha = pci_get_drvdata(pdev);
  739. struct pm8001_hba_info *pm8001_ha;
  740. int i , pos;
  741. u32 device_state;
  742. pm8001_ha = sha->lldd_ha;
  743. flush_workqueue(pm8001_wq);
  744. scsi_block_requests(pm8001_ha->shost);
  745. pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
  746. if (pos == 0) {
  747. printk(KERN_ERR " PCI PM not supported\n");
  748. return -ENODEV;
  749. }
  750. PM8001_CHIP_DISP->interrupt_disable(pm8001_ha, 0);
  751. PM8001_CHIP_DISP->chip_soft_rst(pm8001_ha, 0x252acbcd);
  752. #ifdef PM8001_USE_MSIX
  753. for (i = 0; i < pm8001_ha->number_of_intr; i++)
  754. synchronize_irq(pm8001_ha->msix_entries[i].vector);
  755. for (i = 0; i < pm8001_ha->number_of_intr; i++)
  756. free_irq(pm8001_ha->msix_entries[i].vector, sha);
  757. pci_disable_msix(pdev);
  758. #else
  759. free_irq(pm8001_ha->irq, sha);
  760. #endif
  761. #ifdef PM8001_USE_TASKLET
  762. tasklet_kill(&pm8001_ha->tasklet);
  763. #endif
  764. device_state = pci_choose_state(pdev, state);
  765. pm8001_printk("pdev=0x%p, slot=%s, entering "
  766. "operating state [D%d]\n", pdev,
  767. pm8001_ha->name, device_state);
  768. pci_save_state(pdev);
  769. pci_disable_device(pdev);
  770. pci_set_power_state(pdev, device_state);
  771. return 0;
  772. }
  773. /**
  774. * pm8001_pci_resume - power management resume main entry point
  775. * @pdev: PCI device struct
  776. *
  777. * Returns 0 success, anything else error.
  778. */
  779. static int pm8001_pci_resume(struct pci_dev *pdev)
  780. {
  781. struct sas_ha_struct *sha = pci_get_drvdata(pdev);
  782. struct pm8001_hba_info *pm8001_ha;
  783. int rc;
  784. u32 device_state;
  785. pm8001_ha = sha->lldd_ha;
  786. device_state = pdev->current_state;
  787. pm8001_printk("pdev=0x%p, slot=%s, resuming from previous "
  788. "operating state [D%d]\n", pdev, pm8001_ha->name, device_state);
  789. pci_set_power_state(pdev, PCI_D0);
  790. pci_enable_wake(pdev, PCI_D0, 0);
  791. pci_restore_state(pdev);
  792. rc = pci_enable_device(pdev);
  793. if (rc) {
  794. pm8001_printk("slot=%s Enable device failed during resume\n",
  795. pm8001_ha->name);
  796. goto err_out_enable;
  797. }
  798. pci_set_master(pdev);
  799. rc = pci_go_44(pdev);
  800. if (rc)
  801. goto err_out_disable;
  802. PM8001_CHIP_DISP->chip_soft_rst(pm8001_ha, 0x252acbcd);
  803. rc = PM8001_CHIP_DISP->chip_init(pm8001_ha);
  804. if (rc)
  805. goto err_out_disable;
  806. PM8001_CHIP_DISP->interrupt_disable(pm8001_ha, 0);
  807. rc = pm8001_request_irq(pm8001_ha);
  808. if (rc)
  809. goto err_out_disable;
  810. #ifdef PM8001_USE_TASKLET
  811. tasklet_init(&pm8001_ha->tasklet, pm8001_tasklet,
  812. (unsigned long)pm8001_ha);
  813. #endif
  814. PM8001_CHIP_DISP->interrupt_enable(pm8001_ha, 0);
  815. scsi_unblock_requests(pm8001_ha->shost);
  816. return 0;
  817. err_out_disable:
  818. scsi_remove_host(pm8001_ha->shost);
  819. pci_disable_device(pdev);
  820. err_out_enable:
  821. return rc;
  822. }
  823. /* update of pci device, vendor id and driver data with
  824. * unique value for each of the controller
  825. */
  826. static struct pci_device_id pm8001_pci_table[] = {
  827. { PCI_VDEVICE(PMC_Sierra, 0x8001), chip_8001 },
  828. {
  829. PCI_DEVICE(0x117c, 0x0042),
  830. .driver_data = chip_8001
  831. },
  832. /* Support for SPC/SPCv/SPCve controllers */
  833. { PCI_VDEVICE(ADAPTEC2, 0x8001), chip_8001 },
  834. { PCI_VDEVICE(PMC_Sierra, 0x8008), chip_8008 },
  835. { PCI_VDEVICE(ADAPTEC2, 0x8008), chip_8008 },
  836. { PCI_VDEVICE(PMC_Sierra, 0x8018), chip_8018 },
  837. { PCI_VDEVICE(ADAPTEC2, 0x8018), chip_8018 },
  838. { PCI_VDEVICE(PMC_Sierra, 0x8009), chip_8009 },
  839. { PCI_VDEVICE(ADAPTEC2, 0x8009), chip_8009 },
  840. { PCI_VDEVICE(PMC_Sierra, 0x8019), chip_8019 },
  841. { PCI_VDEVICE(ADAPTEC2, 0x8019), chip_8019 },
  842. { PCI_VENDOR_ID_ADAPTEC2, 0x8081,
  843. PCI_VENDOR_ID_ADAPTEC2, 0x0400, 0, 0, chip_8001 },
  844. { PCI_VENDOR_ID_ADAPTEC2, 0x8081,
  845. PCI_VENDOR_ID_ADAPTEC2, 0x0800, 0, 0, chip_8001 },
  846. { PCI_VENDOR_ID_ADAPTEC2, 0x8088,
  847. PCI_VENDOR_ID_ADAPTEC2, 0x0008, 0, 0, chip_8008 },
  848. { PCI_VENDOR_ID_ADAPTEC2, 0x8088,
  849. PCI_VENDOR_ID_ADAPTEC2, 0x0800, 0, 0, chip_8008 },
  850. { PCI_VENDOR_ID_ADAPTEC2, 0x8089,
  851. PCI_VENDOR_ID_ADAPTEC2, 0x0008, 0, 0, chip_8009 },
  852. { PCI_VENDOR_ID_ADAPTEC2, 0x8089,
  853. PCI_VENDOR_ID_ADAPTEC2, 0x0800, 0, 0, chip_8009 },
  854. { PCI_VENDOR_ID_ADAPTEC2, 0x8088,
  855. PCI_VENDOR_ID_ADAPTEC2, 0x0016, 0, 0, chip_8018 },
  856. { PCI_VENDOR_ID_ADAPTEC2, 0x8088,
  857. PCI_VENDOR_ID_ADAPTEC2, 0x1600, 0, 0, chip_8018 },
  858. { PCI_VENDOR_ID_ADAPTEC2, 0x8089,
  859. PCI_VENDOR_ID_ADAPTEC2, 0x0016, 0, 0, chip_8019 },
  860. { PCI_VENDOR_ID_ADAPTEC2, 0x8089,
  861. PCI_VENDOR_ID_ADAPTEC2, 0x1600, 0, 0, chip_8019 },
  862. {} /* terminate list */
  863. };
  864. static struct pci_driver pm8001_pci_driver = {
  865. .name = DRV_NAME,
  866. .id_table = pm8001_pci_table,
  867. .probe = pm8001_pci_probe,
  868. .remove = pm8001_pci_remove,
  869. .suspend = pm8001_pci_suspend,
  870. .resume = pm8001_pci_resume,
  871. };
  872. /**
  873. * pm8001_init - initialize scsi transport template
  874. */
  875. static int __init pm8001_init(void)
  876. {
  877. int rc = -ENOMEM;
  878. pm8001_wq = alloc_workqueue("pm8001", 0, 0);
  879. if (!pm8001_wq)
  880. goto err;
  881. pm8001_id = 0;
  882. pm8001_stt = sas_domain_attach_transport(&pm8001_transport_ops);
  883. if (!pm8001_stt)
  884. goto err_wq;
  885. rc = pci_register_driver(&pm8001_pci_driver);
  886. if (rc)
  887. goto err_tp;
  888. return 0;
  889. err_tp:
  890. sas_release_transport(pm8001_stt);
  891. err_wq:
  892. destroy_workqueue(pm8001_wq);
  893. err:
  894. return rc;
  895. }
  896. static void __exit pm8001_exit(void)
  897. {
  898. pci_unregister_driver(&pm8001_pci_driver);
  899. sas_release_transport(pm8001_stt);
  900. destroy_workqueue(pm8001_wq);
  901. }
  902. module_init(pm8001_init);
  903. module_exit(pm8001_exit);
  904. MODULE_AUTHOR("Jack Wang <jack_wang@usish.com>");
  905. MODULE_DESCRIPTION(
  906. "PMC-Sierra PM8001/8081/8088/8089 SAS/SATA controller driver");
  907. MODULE_VERSION(DRV_VERSION);
  908. MODULE_LICENSE("GPL");
  909. MODULE_DEVICE_TABLE(pci, pm8001_pci_table);