cpci_hotplug_core.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * CompactPCI Hot Plug Driver
  4. *
  5. * Copyright (C) 2002,2005 SOMA Networks, Inc.
  6. * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
  7. * Copyright (C) 2001 IBM Corp.
  8. *
  9. * All rights reserved.
  10. *
  11. * Send feedback to <scottm@somanetworks.com>
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/sched/signal.h>
  16. #include <linux/slab.h>
  17. #include <linux/pci.h>
  18. #include <linux/pci_hotplug.h>
  19. #include <linux/init.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/atomic.h>
  22. #include <linux/delay.h>
  23. #include <linux/kthread.h>
  24. #include "cpci_hotplug.h"
  25. #define DRIVER_AUTHOR "Scott Murray <scottm@somanetworks.com>"
  26. #define DRIVER_DESC "CompactPCI Hot Plug Core"
  27. #define MY_NAME "cpci_hotplug"
  28. #define dbg(format, arg...) \
  29. do { \
  30. if (cpci_debug) \
  31. printk(KERN_DEBUG "%s: " format "\n", \
  32. MY_NAME, ## arg); \
  33. } while (0)
  34. #define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME, ## arg)
  35. #define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME, ## arg)
  36. #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME, ## arg)
  37. /* local variables */
  38. static DECLARE_RWSEM(list_rwsem);
  39. static LIST_HEAD(slot_list);
  40. static int slots;
  41. static atomic_t extracting;
  42. int cpci_debug;
  43. static struct cpci_hp_controller *controller;
  44. static struct task_struct *cpci_thread;
  45. static int thread_finished;
  46. static int enable_slot(struct hotplug_slot *slot);
  47. static int disable_slot(struct hotplug_slot *slot);
  48. static int set_attention_status(struct hotplug_slot *slot, u8 value);
  49. static int get_power_status(struct hotplug_slot *slot, u8 *value);
  50. static int get_attention_status(struct hotplug_slot *slot, u8 *value);
  51. static int get_adapter_status(struct hotplug_slot *slot, u8 *value);
  52. static int get_latch_status(struct hotplug_slot *slot, u8 *value);
  53. static struct hotplug_slot_ops cpci_hotplug_slot_ops = {
  54. .enable_slot = enable_slot,
  55. .disable_slot = disable_slot,
  56. .set_attention_status = set_attention_status,
  57. .get_power_status = get_power_status,
  58. .get_attention_status = get_attention_status,
  59. .get_adapter_status = get_adapter_status,
  60. .get_latch_status = get_latch_status,
  61. };
  62. static int
  63. update_latch_status(struct hotplug_slot *hotplug_slot, u8 value)
  64. {
  65. struct hotplug_slot_info info;
  66. memcpy(&info, hotplug_slot->info, sizeof(struct hotplug_slot_info));
  67. info.latch_status = value;
  68. return pci_hp_change_slot_info(hotplug_slot, &info);
  69. }
  70. static int
  71. update_adapter_status(struct hotplug_slot *hotplug_slot, u8 value)
  72. {
  73. struct hotplug_slot_info info;
  74. memcpy(&info, hotplug_slot->info, sizeof(struct hotplug_slot_info));
  75. info.adapter_status = value;
  76. return pci_hp_change_slot_info(hotplug_slot, &info);
  77. }
  78. static int
  79. enable_slot(struct hotplug_slot *hotplug_slot)
  80. {
  81. struct slot *slot = hotplug_slot->private;
  82. int retval = 0;
  83. dbg("%s - physical_slot = %s", __func__, slot_name(slot));
  84. if (controller->ops->set_power)
  85. retval = controller->ops->set_power(slot, 1);
  86. return retval;
  87. }
  88. static int
  89. disable_slot(struct hotplug_slot *hotplug_slot)
  90. {
  91. struct slot *slot = hotplug_slot->private;
  92. int retval = 0;
  93. dbg("%s - physical_slot = %s", __func__, slot_name(slot));
  94. down_write(&list_rwsem);
  95. /* Unconfigure device */
  96. dbg("%s - unconfiguring slot %s", __func__, slot_name(slot));
  97. retval = cpci_unconfigure_slot(slot);
  98. if (retval) {
  99. err("%s - could not unconfigure slot %s",
  100. __func__, slot_name(slot));
  101. goto disable_error;
  102. }
  103. dbg("%s - finished unconfiguring slot %s", __func__, slot_name(slot));
  104. /* Clear EXT (by setting it) */
  105. if (cpci_clear_ext(slot)) {
  106. err("%s - could not clear EXT for slot %s",
  107. __func__, slot_name(slot));
  108. retval = -ENODEV;
  109. goto disable_error;
  110. }
  111. cpci_led_on(slot);
  112. if (controller->ops->set_power) {
  113. retval = controller->ops->set_power(slot, 0);
  114. if (retval)
  115. goto disable_error;
  116. }
  117. if (update_adapter_status(slot->hotplug_slot, 0))
  118. warn("failure to update adapter file");
  119. if (slot->extracting) {
  120. slot->extracting = 0;
  121. atomic_dec(&extracting);
  122. }
  123. disable_error:
  124. up_write(&list_rwsem);
  125. return retval;
  126. }
  127. static u8
  128. cpci_get_power_status(struct slot *slot)
  129. {
  130. u8 power = 1;
  131. if (controller->ops->get_power)
  132. power = controller->ops->get_power(slot);
  133. return power;
  134. }
  135. static int
  136. get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  137. {
  138. struct slot *slot = hotplug_slot->private;
  139. *value = cpci_get_power_status(slot);
  140. return 0;
  141. }
  142. static int
  143. get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
  144. {
  145. struct slot *slot = hotplug_slot->private;
  146. *value = cpci_get_attention_status(slot);
  147. return 0;
  148. }
  149. static int
  150. set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
  151. {
  152. return cpci_set_attention_status(hotplug_slot->private, status);
  153. }
  154. static int
  155. get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  156. {
  157. *value = hotplug_slot->info->adapter_status;
  158. return 0;
  159. }
  160. static int
  161. get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
  162. {
  163. *value = hotplug_slot->info->latch_status;
  164. return 0;
  165. }
  166. static void release_slot(struct slot *slot)
  167. {
  168. kfree(slot->hotplug_slot->info);
  169. kfree(slot->hotplug_slot);
  170. pci_dev_put(slot->dev);
  171. kfree(slot);
  172. }
  173. #define SLOT_NAME_SIZE 6
  174. int
  175. cpci_hp_register_bus(struct pci_bus *bus, u8 first, u8 last)
  176. {
  177. struct slot *slot;
  178. struct hotplug_slot *hotplug_slot;
  179. struct hotplug_slot_info *info;
  180. char name[SLOT_NAME_SIZE];
  181. int status;
  182. int i;
  183. if (!(controller && bus))
  184. return -ENODEV;
  185. /*
  186. * Create a structure for each slot, and register that slot
  187. * with the pci_hotplug subsystem.
  188. */
  189. for (i = first; i <= last; ++i) {
  190. slot = kzalloc(sizeof(struct slot), GFP_KERNEL);
  191. if (!slot) {
  192. status = -ENOMEM;
  193. goto error;
  194. }
  195. hotplug_slot =
  196. kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
  197. if (!hotplug_slot) {
  198. status = -ENOMEM;
  199. goto error_slot;
  200. }
  201. slot->hotplug_slot = hotplug_slot;
  202. info = kzalloc(sizeof(struct hotplug_slot_info), GFP_KERNEL);
  203. if (!info) {
  204. status = -ENOMEM;
  205. goto error_hpslot;
  206. }
  207. hotplug_slot->info = info;
  208. slot->bus = bus;
  209. slot->number = i;
  210. slot->devfn = PCI_DEVFN(i, 0);
  211. snprintf(name, SLOT_NAME_SIZE, "%02x:%02x", bus->number, i);
  212. hotplug_slot->private = slot;
  213. hotplug_slot->ops = &cpci_hotplug_slot_ops;
  214. /*
  215. * Initialize the slot info structure with some known
  216. * good values.
  217. */
  218. dbg("initializing slot %s", name);
  219. info->power_status = cpci_get_power_status(slot);
  220. info->attention_status = cpci_get_attention_status(slot);
  221. dbg("registering slot %s", name);
  222. status = pci_hp_register(slot->hotplug_slot, bus, i, name);
  223. if (status) {
  224. err("pci_hp_register failed with error %d", status);
  225. goto error_info;
  226. }
  227. dbg("slot registered with name: %s", slot_name(slot));
  228. /* Add slot to our internal list */
  229. down_write(&list_rwsem);
  230. list_add(&slot->slot_list, &slot_list);
  231. slots++;
  232. up_write(&list_rwsem);
  233. }
  234. return 0;
  235. error_info:
  236. kfree(info);
  237. error_hpslot:
  238. kfree(hotplug_slot);
  239. error_slot:
  240. kfree(slot);
  241. error:
  242. return status;
  243. }
  244. EXPORT_SYMBOL_GPL(cpci_hp_register_bus);
  245. int
  246. cpci_hp_unregister_bus(struct pci_bus *bus)
  247. {
  248. struct slot *slot;
  249. struct slot *tmp;
  250. int status = 0;
  251. down_write(&list_rwsem);
  252. if (!slots) {
  253. up_write(&list_rwsem);
  254. return -1;
  255. }
  256. list_for_each_entry_safe(slot, tmp, &slot_list, slot_list) {
  257. if (slot->bus == bus) {
  258. list_del(&slot->slot_list);
  259. slots--;
  260. dbg("deregistering slot %s", slot_name(slot));
  261. pci_hp_deregister(slot->hotplug_slot);
  262. release_slot(slot);
  263. }
  264. }
  265. up_write(&list_rwsem);
  266. return status;
  267. }
  268. EXPORT_SYMBOL_GPL(cpci_hp_unregister_bus);
  269. /* This is the interrupt mode interrupt handler */
  270. static irqreturn_t
  271. cpci_hp_intr(int irq, void *data)
  272. {
  273. dbg("entered cpci_hp_intr");
  274. /* Check to see if it was our interrupt */
  275. if ((controller->irq_flags & IRQF_SHARED) &&
  276. !controller->ops->check_irq(controller->dev_id)) {
  277. dbg("exited cpci_hp_intr, not our interrupt");
  278. return IRQ_NONE;
  279. }
  280. /* Disable ENUM interrupt */
  281. controller->ops->disable_irq();
  282. /* Trigger processing by the event thread */
  283. wake_up_process(cpci_thread);
  284. return IRQ_HANDLED;
  285. }
  286. /*
  287. * According to PICMG 2.1 R2.0, section 6.3.2, upon
  288. * initialization, the system driver shall clear the
  289. * INS bits of the cold-inserted devices.
  290. */
  291. static int
  292. init_slots(int clear_ins)
  293. {
  294. struct slot *slot;
  295. struct pci_dev *dev;
  296. dbg("%s - enter", __func__);
  297. down_read(&list_rwsem);
  298. if (!slots) {
  299. up_read(&list_rwsem);
  300. return -1;
  301. }
  302. list_for_each_entry(slot, &slot_list, slot_list) {
  303. dbg("%s - looking at slot %s", __func__, slot_name(slot));
  304. if (clear_ins && cpci_check_and_clear_ins(slot))
  305. dbg("%s - cleared INS for slot %s",
  306. __func__, slot_name(slot));
  307. dev = pci_get_slot(slot->bus, PCI_DEVFN(slot->number, 0));
  308. if (dev) {
  309. if (update_adapter_status(slot->hotplug_slot, 1))
  310. warn("failure to update adapter file");
  311. if (update_latch_status(slot->hotplug_slot, 1))
  312. warn("failure to update latch file");
  313. slot->dev = dev;
  314. }
  315. }
  316. up_read(&list_rwsem);
  317. dbg("%s - exit", __func__);
  318. return 0;
  319. }
  320. static int
  321. check_slots(void)
  322. {
  323. struct slot *slot;
  324. int extracted;
  325. int inserted;
  326. u16 hs_csr;
  327. down_read(&list_rwsem);
  328. if (!slots) {
  329. up_read(&list_rwsem);
  330. err("no slots registered, shutting down");
  331. return -1;
  332. }
  333. extracted = inserted = 0;
  334. list_for_each_entry(slot, &slot_list, slot_list) {
  335. dbg("%s - looking at slot %s", __func__, slot_name(slot));
  336. if (cpci_check_and_clear_ins(slot)) {
  337. /*
  338. * Some broken hardware (e.g. PLX 9054AB) asserts
  339. * ENUM# twice...
  340. */
  341. if (slot->dev) {
  342. warn("slot %s already inserted",
  343. slot_name(slot));
  344. inserted++;
  345. continue;
  346. }
  347. /* Process insertion */
  348. dbg("%s - slot %s inserted", __func__, slot_name(slot));
  349. /* GSM, debug */
  350. hs_csr = cpci_get_hs_csr(slot);
  351. dbg("%s - slot %s HS_CSR (1) = %04x",
  352. __func__, slot_name(slot), hs_csr);
  353. /* Configure device */
  354. dbg("%s - configuring slot %s",
  355. __func__, slot_name(slot));
  356. if (cpci_configure_slot(slot)) {
  357. err("%s - could not configure slot %s",
  358. __func__, slot_name(slot));
  359. continue;
  360. }
  361. dbg("%s - finished configuring slot %s",
  362. __func__, slot_name(slot));
  363. /* GSM, debug */
  364. hs_csr = cpci_get_hs_csr(slot);
  365. dbg("%s - slot %s HS_CSR (2) = %04x",
  366. __func__, slot_name(slot), hs_csr);
  367. if (update_latch_status(slot->hotplug_slot, 1))
  368. warn("failure to update latch file");
  369. if (update_adapter_status(slot->hotplug_slot, 1))
  370. warn("failure to update adapter file");
  371. cpci_led_off(slot);
  372. /* GSM, debug */
  373. hs_csr = cpci_get_hs_csr(slot);
  374. dbg("%s - slot %s HS_CSR (3) = %04x",
  375. __func__, slot_name(slot), hs_csr);
  376. inserted++;
  377. } else if (cpci_check_ext(slot)) {
  378. /* Process extraction request */
  379. dbg("%s - slot %s extracted",
  380. __func__, slot_name(slot));
  381. /* GSM, debug */
  382. hs_csr = cpci_get_hs_csr(slot);
  383. dbg("%s - slot %s HS_CSR = %04x",
  384. __func__, slot_name(slot), hs_csr);
  385. if (!slot->extracting) {
  386. if (update_latch_status(slot->hotplug_slot, 0))
  387. warn("failure to update latch file");
  388. slot->extracting = 1;
  389. atomic_inc(&extracting);
  390. }
  391. extracted++;
  392. } else if (slot->extracting) {
  393. hs_csr = cpci_get_hs_csr(slot);
  394. if (hs_csr == 0xffff) {
  395. /*
  396. * Hmmm, we're likely hosed at this point, should we
  397. * bother trying to tell the driver or not?
  398. */
  399. err("card in slot %s was improperly removed",
  400. slot_name(slot));
  401. if (update_adapter_status(slot->hotplug_slot, 0))
  402. warn("failure to update adapter file");
  403. slot->extracting = 0;
  404. atomic_dec(&extracting);
  405. }
  406. }
  407. }
  408. up_read(&list_rwsem);
  409. dbg("inserted=%d, extracted=%d, extracting=%d",
  410. inserted, extracted, atomic_read(&extracting));
  411. if (inserted || extracted)
  412. return extracted;
  413. else if (!atomic_read(&extracting)) {
  414. err("cannot find ENUM# source, shutting down");
  415. return -1;
  416. }
  417. return 0;
  418. }
  419. /* This is the interrupt mode worker thread body */
  420. static int
  421. event_thread(void *data)
  422. {
  423. int rc;
  424. dbg("%s - event thread started", __func__);
  425. while (1) {
  426. dbg("event thread sleeping");
  427. set_current_state(TASK_INTERRUPTIBLE);
  428. schedule();
  429. if (kthread_should_stop())
  430. break;
  431. do {
  432. rc = check_slots();
  433. if (rc > 0) {
  434. /* Give userspace a chance to handle extraction */
  435. msleep(500);
  436. } else if (rc < 0) {
  437. dbg("%s - error checking slots", __func__);
  438. thread_finished = 1;
  439. goto out;
  440. }
  441. } while (atomic_read(&extracting) && !kthread_should_stop());
  442. if (kthread_should_stop())
  443. break;
  444. /* Re-enable ENUM# interrupt */
  445. dbg("%s - re-enabling irq", __func__);
  446. controller->ops->enable_irq();
  447. }
  448. out:
  449. return 0;
  450. }
  451. /* This is the polling mode worker thread body */
  452. static int
  453. poll_thread(void *data)
  454. {
  455. int rc;
  456. while (1) {
  457. if (kthread_should_stop() || signal_pending(current))
  458. break;
  459. if (controller->ops->query_enum()) {
  460. do {
  461. rc = check_slots();
  462. if (rc > 0) {
  463. /* Give userspace a chance to handle extraction */
  464. msleep(500);
  465. } else if (rc < 0) {
  466. dbg("%s - error checking slots", __func__);
  467. thread_finished = 1;
  468. goto out;
  469. }
  470. } while (atomic_read(&extracting) && !kthread_should_stop());
  471. }
  472. msleep(100);
  473. }
  474. out:
  475. return 0;
  476. }
  477. static int
  478. cpci_start_thread(void)
  479. {
  480. if (controller->irq)
  481. cpci_thread = kthread_run(event_thread, NULL, "cpci_hp_eventd");
  482. else
  483. cpci_thread = kthread_run(poll_thread, NULL, "cpci_hp_polld");
  484. if (IS_ERR(cpci_thread)) {
  485. err("Can't start up our thread");
  486. return PTR_ERR(cpci_thread);
  487. }
  488. thread_finished = 0;
  489. return 0;
  490. }
  491. static void
  492. cpci_stop_thread(void)
  493. {
  494. kthread_stop(cpci_thread);
  495. thread_finished = 1;
  496. }
  497. int
  498. cpci_hp_register_controller(struct cpci_hp_controller *new_controller)
  499. {
  500. int status = 0;
  501. if (controller)
  502. return -1;
  503. if (!(new_controller && new_controller->ops))
  504. return -EINVAL;
  505. if (new_controller->irq) {
  506. if (!(new_controller->ops->enable_irq &&
  507. new_controller->ops->disable_irq))
  508. status = -EINVAL;
  509. if (request_irq(new_controller->irq,
  510. cpci_hp_intr,
  511. new_controller->irq_flags,
  512. MY_NAME,
  513. new_controller->dev_id)) {
  514. err("Can't get irq %d for the hotplug cPCI controller",
  515. new_controller->irq);
  516. status = -ENODEV;
  517. }
  518. dbg("%s - acquired controller irq %d",
  519. __func__, new_controller->irq);
  520. }
  521. if (!status)
  522. controller = new_controller;
  523. return status;
  524. }
  525. EXPORT_SYMBOL_GPL(cpci_hp_register_controller);
  526. static void
  527. cleanup_slots(void)
  528. {
  529. struct slot *slot;
  530. struct slot *tmp;
  531. /*
  532. * Unregister all of our slots with the pci_hotplug subsystem,
  533. * and free up all memory that we had allocated.
  534. */
  535. down_write(&list_rwsem);
  536. if (!slots)
  537. goto cleanup_null;
  538. list_for_each_entry_safe(slot, tmp, &slot_list, slot_list) {
  539. list_del(&slot->slot_list);
  540. pci_hp_deregister(slot->hotplug_slot);
  541. release_slot(slot);
  542. }
  543. cleanup_null:
  544. up_write(&list_rwsem);
  545. return;
  546. }
  547. int
  548. cpci_hp_unregister_controller(struct cpci_hp_controller *old_controller)
  549. {
  550. int status = 0;
  551. if (controller) {
  552. if (!thread_finished)
  553. cpci_stop_thread();
  554. if (controller->irq)
  555. free_irq(controller->irq, controller->dev_id);
  556. controller = NULL;
  557. cleanup_slots();
  558. } else
  559. status = -ENODEV;
  560. return status;
  561. }
  562. EXPORT_SYMBOL_GPL(cpci_hp_unregister_controller);
  563. int
  564. cpci_hp_start(void)
  565. {
  566. static int first = 1;
  567. int status;
  568. dbg("%s - enter", __func__);
  569. if (!controller)
  570. return -ENODEV;
  571. down_read(&list_rwsem);
  572. if (list_empty(&slot_list)) {
  573. up_read(&list_rwsem);
  574. return -ENODEV;
  575. }
  576. up_read(&list_rwsem);
  577. status = init_slots(first);
  578. if (first)
  579. first = 0;
  580. if (status)
  581. return status;
  582. status = cpci_start_thread();
  583. if (status)
  584. return status;
  585. dbg("%s - thread started", __func__);
  586. if (controller->irq) {
  587. /* Start enum interrupt processing */
  588. dbg("%s - enabling irq", __func__);
  589. controller->ops->enable_irq();
  590. }
  591. dbg("%s - exit", __func__);
  592. return 0;
  593. }
  594. EXPORT_SYMBOL_GPL(cpci_hp_start);
  595. int
  596. cpci_hp_stop(void)
  597. {
  598. if (!controller)
  599. return -ENODEV;
  600. if (controller->irq) {
  601. /* Stop enum interrupt processing */
  602. dbg("%s - disabling irq", __func__);
  603. controller->ops->disable_irq();
  604. }
  605. cpci_stop_thread();
  606. return 0;
  607. }
  608. EXPORT_SYMBOL_GPL(cpci_hp_stop);
  609. int __init
  610. cpci_hotplug_init(int debug)
  611. {
  612. cpci_debug = debug;
  613. return 0;
  614. }