portdrv_core.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * File: portdrv_core.c
  3. * Purpose: PCI Express Port Bus Driver's Core Functions
  4. *
  5. * Copyright (C) 2004 Intel
  6. * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
  7. */
  8. #include <linux/module.h>
  9. #include <linux/pci.h>
  10. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/pm.h>
  13. #include <linux/string.h>
  14. #include <linux/slab.h>
  15. #include <linux/pcieport_if.h>
  16. #include "../pci.h"
  17. #include "portdrv.h"
  18. /**
  19. * release_pcie_device - free PCI Express port service device structure
  20. * @dev: Port service device to release
  21. *
  22. * Invoked automatically when device is being removed in response to
  23. * device_unregister(dev). Release all resources being claimed.
  24. */
  25. static void release_pcie_device(struct device *dev)
  26. {
  27. kfree(to_pcie_device(dev));
  28. }
  29. /**
  30. * pcie_port_msix_add_entry - add entry to given array of MSI-X entries
  31. * @entries: Array of MSI-X entries
  32. * @new_entry: Index of the entry to add to the array
  33. * @nr_entries: Number of entries aleady in the array
  34. *
  35. * Return value: Position of the added entry in the array
  36. */
  37. static int pcie_port_msix_add_entry(
  38. struct msix_entry *entries, int new_entry, int nr_entries)
  39. {
  40. int j;
  41. for (j = 0; j < nr_entries; j++)
  42. if (entries[j].entry == new_entry)
  43. return j;
  44. entries[j].entry = new_entry;
  45. return j;
  46. }
  47. /**
  48. * pcie_port_enable_msix - try to set up MSI-X as interrupt mode for given port
  49. * @dev: PCI Express port to handle
  50. * @vectors: Array of interrupt vectors to populate
  51. * @mask: Bitmask of port capabilities returned by get_port_device_capability()
  52. *
  53. * Return value: 0 on success, error code on failure
  54. */
  55. static int pcie_port_enable_msix(struct pci_dev *dev, int *vectors, int mask)
  56. {
  57. struct msix_entry *msix_entries;
  58. int idx[PCIE_PORT_DEVICE_MAXSERVICES];
  59. int nr_entries, status, pos, i, nvec;
  60. u16 reg16;
  61. u32 reg32;
  62. nr_entries = pci_msix_table_size(dev);
  63. if (!nr_entries)
  64. return -EINVAL;
  65. if (nr_entries > PCIE_PORT_MAX_MSIX_ENTRIES)
  66. nr_entries = PCIE_PORT_MAX_MSIX_ENTRIES;
  67. msix_entries = kzalloc(sizeof(*msix_entries) * nr_entries, GFP_KERNEL);
  68. if (!msix_entries)
  69. return -ENOMEM;
  70. /*
  71. * Allocate as many entries as the port wants, so that we can check
  72. * which of them will be useful. Moreover, if nr_entries is correctly
  73. * equal to the number of entries this port actually uses, we'll happily
  74. * go through without any tricks.
  75. */
  76. for (i = 0; i < nr_entries; i++)
  77. msix_entries[i].entry = i;
  78. status = pci_enable_msix(dev, msix_entries, nr_entries);
  79. if (status)
  80. goto Exit;
  81. for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
  82. idx[i] = -1;
  83. status = -EIO;
  84. nvec = 0;
  85. if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP)) {
  86. int entry;
  87. /*
  88. * The code below follows the PCI Express Base Specification 2.0
  89. * stating in Section 6.1.6 that "PME and Hot-Plug Event
  90. * interrupts (when both are implemented) always share the same
  91. * MSI or MSI-X vector, as indicated by the Interrupt Message
  92. * Number field in the PCI Express Capabilities register", where
  93. * according to Section 7.8.2 of the specification "For MSI-X,
  94. * the value in this field indicates which MSI-X Table entry is
  95. * used to generate the interrupt message."
  96. */
  97. pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  98. pci_read_config_word(dev, pos + PCIE_CAPABILITIES_REG, &reg16);
  99. entry = (reg16 >> 9) & PCIE_PORT_MSI_VECTOR_MASK;
  100. if (entry >= nr_entries)
  101. goto Error;
  102. i = pcie_port_msix_add_entry(msix_entries, entry, nvec);
  103. if (i == nvec)
  104. nvec++;
  105. idx[PCIE_PORT_SERVICE_PME_SHIFT] = i;
  106. idx[PCIE_PORT_SERVICE_HP_SHIFT] = i;
  107. }
  108. if (mask & PCIE_PORT_SERVICE_AER) {
  109. int entry;
  110. /*
  111. * The code below follows Section 7.10.10 of the PCI Express
  112. * Base Specification 2.0 stating that bits 31-27 of the Root
  113. * Error Status Register contain a value indicating which of the
  114. * MSI/MSI-X vectors assigned to the port is going to be used
  115. * for AER, where "For MSI-X, the value in this register
  116. * indicates which MSI-X Table entry is used to generate the
  117. * interrupt message."
  118. */
  119. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  120. pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &reg32);
  121. entry = reg32 >> 27;
  122. if (entry >= nr_entries)
  123. goto Error;
  124. i = pcie_port_msix_add_entry(msix_entries, entry, nvec);
  125. if (i == nvec)
  126. nvec++;
  127. idx[PCIE_PORT_SERVICE_AER_SHIFT] = i;
  128. }
  129. /*
  130. * If nvec is equal to the allocated number of entries, we can just use
  131. * what we have. Otherwise, the port has some extra entries not for the
  132. * services we know and we need to work around that.
  133. */
  134. if (nvec == nr_entries) {
  135. status = 0;
  136. } else {
  137. /* Drop the temporary MSI-X setup */
  138. pci_disable_msix(dev);
  139. /* Now allocate the MSI-X vectors for real */
  140. status = pci_enable_msix(dev, msix_entries, nvec);
  141. if (status)
  142. goto Exit;
  143. }
  144. for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
  145. vectors[i] = idx[i] >= 0 ? msix_entries[idx[i]].vector : -1;
  146. Exit:
  147. kfree(msix_entries);
  148. return status;
  149. Error:
  150. pci_disable_msix(dev);
  151. goto Exit;
  152. }
  153. /**
  154. * assign_interrupt_mode - choose interrupt mode for PCI Express port services
  155. * (INTx, MSI-X, MSI) and set up vectors
  156. * @dev: PCI Express port to handle
  157. * @vectors: Array of interrupt vectors to populate
  158. * @mask: Bitmask of port capabilities returned by get_port_device_capability()
  159. *
  160. * Return value: Interrupt mode associated with the port
  161. */
  162. static int assign_interrupt_mode(struct pci_dev *dev, int *vectors, int mask)
  163. {
  164. struct pcie_port_data *port_data = pci_get_drvdata(dev);
  165. int irq, interrupt_mode = PCIE_PORT_NO_IRQ;
  166. int i;
  167. /* Check MSI quirk */
  168. if (port_data->port_type == PCIE_RC_PORT && pcie_mch_quirk)
  169. goto Fallback;
  170. /* Try to use MSI-X if supported */
  171. if (!pcie_port_enable_msix(dev, vectors, mask))
  172. return PCIE_PORT_MSIX_MODE;
  173. /* We're not going to use MSI-X, so try MSI and fall back to INTx */
  174. if (!pci_enable_msi(dev))
  175. interrupt_mode = PCIE_PORT_MSI_MODE;
  176. Fallback:
  177. if (interrupt_mode == PCIE_PORT_NO_IRQ && dev->pin)
  178. interrupt_mode = PCIE_PORT_INTx_MODE;
  179. irq = interrupt_mode != PCIE_PORT_NO_IRQ ? dev->irq : -1;
  180. for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
  181. vectors[i] = irq;
  182. vectors[PCIE_PORT_SERVICE_VC_SHIFT] = -1;
  183. return interrupt_mode;
  184. }
  185. /**
  186. * get_port_device_capability - discover capabilities of a PCI Express port
  187. * @dev: PCI Express port to examine
  188. *
  189. * The capabilities are read from the port's PCI Express configuration registers
  190. * as described in PCI Express Base Specification 1.0a sections 7.8.2, 7.8.9 and
  191. * 7.9 - 7.11.
  192. *
  193. * Return value: Bitmask of discovered port capabilities
  194. */
  195. static int get_port_device_capability(struct pci_dev *dev)
  196. {
  197. int services = 0, pos;
  198. u16 reg16;
  199. u32 reg32;
  200. pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  201. pci_read_config_word(dev, pos + PCIE_CAPABILITIES_REG, &reg16);
  202. /* Hot-Plug Capable */
  203. if (reg16 & PORT_TO_SLOT_MASK) {
  204. pci_read_config_dword(dev,
  205. pos + PCIE_SLOT_CAPABILITIES_REG, &reg32);
  206. if (reg32 & SLOT_HP_CAPABLE_MASK)
  207. services |= PCIE_PORT_SERVICE_HP;
  208. }
  209. /* AER capable */
  210. if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR))
  211. services |= PCIE_PORT_SERVICE_AER;
  212. /* VC support */
  213. if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_VC))
  214. services |= PCIE_PORT_SERVICE_VC;
  215. return services;
  216. }
  217. /**
  218. * pcie_device_init - initialize PCI Express port service device
  219. * @dev: Port service device to initialize
  220. * @parent: PCI Express port to associate the service device with
  221. * @port_type: Type of the port
  222. * @service_type: Type of service to associate with the service device
  223. * @irq: Interrupt vector to associate with the service device
  224. */
  225. static void pcie_device_init(struct pci_dev *parent, struct pcie_device *dev,
  226. int service_type, int irq)
  227. {
  228. struct pcie_port_data *port_data = pci_get_drvdata(parent);
  229. struct device *device;
  230. int port_type = port_data->port_type;
  231. dev->port = parent;
  232. dev->irq = irq;
  233. dev->service = service_type;
  234. /* Initialize generic device interface */
  235. device = &dev->device;
  236. memset(device, 0, sizeof(struct device));
  237. device->bus = &pcie_port_bus_type;
  238. device->driver = NULL;
  239. device->driver_data = NULL;
  240. device->release = release_pcie_device; /* callback to free pcie dev */
  241. dev_set_name(device, "%s:pcie%02x",
  242. pci_name(parent), get_descriptor_id(port_type, service_type));
  243. device->parent = &parent->dev;
  244. }
  245. /**
  246. * alloc_pcie_device - allocate PCI Express port service device structure
  247. * @parent: PCI Express port to associate the service device with
  248. * @port_type: Type of the port
  249. * @service_type: Type of service to associate with the service device
  250. * @irq: Interrupt vector to associate with the service device
  251. */
  252. static struct pcie_device* alloc_pcie_device(struct pci_dev *parent,
  253. int service_type, int irq)
  254. {
  255. struct pcie_device *device;
  256. device = kzalloc(sizeof(struct pcie_device), GFP_KERNEL);
  257. if (!device)
  258. return NULL;
  259. pcie_device_init(parent, device, service_type, irq);
  260. return device;
  261. }
  262. /**
  263. * pcie_port_device_probe - check if device is a PCI Express port
  264. * @dev: Device to check
  265. */
  266. int pcie_port_device_probe(struct pci_dev *dev)
  267. {
  268. int pos, type;
  269. u16 reg;
  270. if (!(pos = pci_find_capability(dev, PCI_CAP_ID_EXP)))
  271. return -ENODEV;
  272. pci_read_config_word(dev, pos + PCIE_CAPABILITIES_REG, &reg);
  273. type = (reg >> 4) & PORT_TYPE_MASK;
  274. if ( type == PCIE_RC_PORT || type == PCIE_SW_UPSTREAM_PORT ||
  275. type == PCIE_SW_DOWNSTREAM_PORT )
  276. return 0;
  277. return -ENODEV;
  278. }
  279. /**
  280. * pcie_port_device_register - register PCI Express port
  281. * @dev: PCI Express port to register
  282. *
  283. * Allocate the port extension structure and register services associated with
  284. * the port.
  285. */
  286. int pcie_port_device_register(struct pci_dev *dev)
  287. {
  288. struct pcie_port_data *port_data;
  289. int status, capabilities, irq_mode, i, nr_serv;
  290. int vectors[PCIE_PORT_DEVICE_MAXSERVICES];
  291. u16 reg16;
  292. port_data = kzalloc(sizeof(*port_data), GFP_KERNEL);
  293. if (!port_data)
  294. return -ENOMEM;
  295. pci_set_drvdata(dev, port_data);
  296. /* Get port type */
  297. pci_read_config_word(dev,
  298. pci_find_capability(dev, PCI_CAP_ID_EXP) +
  299. PCIE_CAPABILITIES_REG, &reg16);
  300. port_data->port_type = (reg16 >> 4) & PORT_TYPE_MASK;
  301. capabilities = get_port_device_capability(dev);
  302. /* Root ports are capable of generating PME too */
  303. if (port_data->port_type == PCIE_RC_PORT)
  304. capabilities |= PCIE_PORT_SERVICE_PME;
  305. irq_mode = assign_interrupt_mode(dev, vectors, capabilities);
  306. if (irq_mode == PCIE_PORT_NO_IRQ) {
  307. /*
  308. * Don't use service devices that require interrupts if there is
  309. * no way to generate them.
  310. */
  311. if (!(capabilities & PCIE_PORT_SERVICE_VC)) {
  312. status = -ENODEV;
  313. goto Error;
  314. }
  315. capabilities = PCIE_PORT_SERVICE_VC;
  316. }
  317. port_data->port_irq_mode = irq_mode;
  318. status = pci_enable_device(dev);
  319. if (status)
  320. goto Error;
  321. pci_set_master(dev);
  322. /* Allocate child services if any */
  323. for (i = 0, nr_serv = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) {
  324. struct pcie_device *child;
  325. int service = 1 << i;
  326. if (!(capabilities & service))
  327. continue;
  328. child = alloc_pcie_device(dev, service, vectors[i]);
  329. if (!child)
  330. continue;
  331. status = device_register(&child->device);
  332. if (status) {
  333. kfree(child);
  334. continue;
  335. }
  336. get_device(&child->device);
  337. nr_serv++;
  338. }
  339. if (!nr_serv) {
  340. pci_disable_device(dev);
  341. status = -ENODEV;
  342. goto Error;
  343. }
  344. return 0;
  345. Error:
  346. kfree(port_data);
  347. return status;
  348. }
  349. #ifdef CONFIG_PM
  350. static int suspend_iter(struct device *dev, void *data)
  351. {
  352. struct pcie_port_service_driver *service_driver;
  353. if ((dev->bus == &pcie_port_bus_type) &&
  354. (dev->driver)) {
  355. service_driver = to_service_driver(dev->driver);
  356. if (service_driver->suspend)
  357. service_driver->suspend(to_pcie_device(dev));
  358. }
  359. return 0;
  360. }
  361. /**
  362. * pcie_port_device_suspend - suspend port services associated with a PCIe port
  363. * @dev: PCI Express port to handle
  364. */
  365. int pcie_port_device_suspend(struct device *dev)
  366. {
  367. return device_for_each_child(dev, NULL, suspend_iter);
  368. }
  369. static int resume_iter(struct device *dev, void *data)
  370. {
  371. struct pcie_port_service_driver *service_driver;
  372. if ((dev->bus == &pcie_port_bus_type) &&
  373. (dev->driver)) {
  374. service_driver = to_service_driver(dev->driver);
  375. if (service_driver->resume)
  376. service_driver->resume(to_pcie_device(dev));
  377. }
  378. return 0;
  379. }
  380. /**
  381. * pcie_port_device_suspend - resume port services associated with a PCIe port
  382. * @dev: PCI Express port to handle
  383. */
  384. int pcie_port_device_resume(struct device *dev)
  385. {
  386. return device_for_each_child(dev, NULL, resume_iter);
  387. }
  388. #endif /* PM */
  389. static int remove_iter(struct device *dev, void *data)
  390. {
  391. if (dev->bus == &pcie_port_bus_type) {
  392. put_device(dev);
  393. device_unregister(dev);
  394. }
  395. return 0;
  396. }
  397. /**
  398. * pcie_port_device_remove - unregister PCI Express port service devices
  399. * @dev: PCI Express port the service devices to unregister are associated with
  400. *
  401. * Remove PCI Express port service devices associated with given port and
  402. * disable MSI-X or MSI for the port.
  403. */
  404. void pcie_port_device_remove(struct pci_dev *dev)
  405. {
  406. struct pcie_port_data *port_data = pci_get_drvdata(dev);
  407. device_for_each_child(&dev->dev, NULL, remove_iter);
  408. pci_disable_device(dev);
  409. switch (port_data->port_irq_mode) {
  410. case PCIE_PORT_MSIX_MODE:
  411. pci_disable_msix(dev);
  412. break;
  413. case PCIE_PORT_MSI_MODE:
  414. pci_disable_msi(dev);
  415. break;
  416. }
  417. kfree(port_data);
  418. }
  419. /**
  420. * pcie_port_probe_service - probe driver for given PCI Express port service
  421. * @dev: PCI Express port service device to probe against
  422. *
  423. * If PCI Express port service driver is registered with
  424. * pcie_port_service_register(), this function will be called by the driver core
  425. * whenever match is found between the driver and a port service device.
  426. */
  427. static int pcie_port_probe_service(struct device *dev)
  428. {
  429. struct pcie_device *pciedev;
  430. struct pcie_port_service_driver *driver;
  431. int status;
  432. if (!dev || !dev->driver)
  433. return -ENODEV;
  434. driver = to_service_driver(dev->driver);
  435. if (!driver || !driver->probe)
  436. return -ENODEV;
  437. pciedev = to_pcie_device(dev);
  438. status = driver->probe(pciedev);
  439. if (!status) {
  440. dev_printk(KERN_DEBUG, dev, "service driver %s loaded\n",
  441. driver->name);
  442. get_device(dev);
  443. }
  444. return status;
  445. }
  446. /**
  447. * pcie_port_remove_service - detach driver from given PCI Express port service
  448. * @dev: PCI Express port service device to handle
  449. *
  450. * If PCI Express port service driver is registered with
  451. * pcie_port_service_register(), this function will be called by the driver core
  452. * when device_unregister() is called for the port service device associated
  453. * with the driver.
  454. */
  455. static int pcie_port_remove_service(struct device *dev)
  456. {
  457. struct pcie_device *pciedev;
  458. struct pcie_port_service_driver *driver;
  459. if (!dev || !dev->driver)
  460. return 0;
  461. pciedev = to_pcie_device(dev);
  462. driver = to_service_driver(dev->driver);
  463. if (driver && driver->remove) {
  464. dev_printk(KERN_DEBUG, dev, "unloading service driver %s\n",
  465. driver->name);
  466. driver->remove(pciedev);
  467. put_device(dev);
  468. }
  469. return 0;
  470. }
  471. /**
  472. * pcie_port_shutdown_service - shut down given PCI Express port service
  473. * @dev: PCI Express port service device to handle
  474. *
  475. * If PCI Express port service driver is registered with
  476. * pcie_port_service_register(), this function will be called by the driver core
  477. * when device_shutdown() is called for the port service device associated
  478. * with the driver.
  479. */
  480. static void pcie_port_shutdown_service(struct device *dev) {}
  481. /**
  482. * pcie_port_service_register - register PCI Express port service driver
  483. * @new: PCI Express port service driver to register
  484. */
  485. int pcie_port_service_register(struct pcie_port_service_driver *new)
  486. {
  487. new->driver.name = (char *)new->name;
  488. new->driver.bus = &pcie_port_bus_type;
  489. new->driver.probe = pcie_port_probe_service;
  490. new->driver.remove = pcie_port_remove_service;
  491. new->driver.shutdown = pcie_port_shutdown_service;
  492. return driver_register(&new->driver);
  493. }
  494. /**
  495. * pcie_port_service_unregister - unregister PCI Express port service driver
  496. * @drv: PCI Express port service driver to unregister
  497. */
  498. void pcie_port_service_unregister(struct pcie_port_service_driver *drv)
  499. {
  500. driver_unregister(&drv->driver);
  501. }
  502. EXPORT_SYMBOL(pcie_port_service_register);
  503. EXPORT_SYMBOL(pcie_port_service_unregister);