cpci_hotplug_core.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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 hotplug_slot *hotplug_slot)
  167. {
  168. struct slot *slot = hotplug_slot->private;
  169. kfree(slot->hotplug_slot->info);
  170. kfree(slot->hotplug_slot);
  171. pci_dev_put(slot->dev);
  172. kfree(slot);
  173. }
  174. #define SLOT_NAME_SIZE 6
  175. int
  176. cpci_hp_register_bus(struct pci_bus *bus, u8 first, u8 last)
  177. {
  178. struct slot *slot;
  179. struct hotplug_slot *hotplug_slot;
  180. struct hotplug_slot_info *info;
  181. char name[SLOT_NAME_SIZE];
  182. int status;
  183. int i;
  184. if (!(controller && bus))
  185. return -ENODEV;
  186. /*
  187. * Create a structure for each slot, and register that slot
  188. * with the pci_hotplug subsystem.
  189. */
  190. for (i = first; i <= last; ++i) {
  191. slot = kzalloc(sizeof(struct slot), GFP_KERNEL);
  192. if (!slot) {
  193. status = -ENOMEM;
  194. goto error;
  195. }
  196. hotplug_slot =
  197. kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
  198. if (!hotplug_slot) {
  199. status = -ENOMEM;
  200. goto error_slot;
  201. }
  202. slot->hotplug_slot = hotplug_slot;
  203. info = kzalloc(sizeof(struct hotplug_slot_info), GFP_KERNEL);
  204. if (!info) {
  205. status = -ENOMEM;
  206. goto error_hpslot;
  207. }
  208. hotplug_slot->info = info;
  209. slot->bus = bus;
  210. slot->number = i;
  211. slot->devfn = PCI_DEVFN(i, 0);
  212. snprintf(name, SLOT_NAME_SIZE, "%02x:%02x", bus->number, i);
  213. hotplug_slot->private = slot;
  214. hotplug_slot->release = &release_slot;
  215. hotplug_slot->ops = &cpci_hotplug_slot_ops;
  216. /*
  217. * Initialize the slot info structure with some known
  218. * good values.
  219. */
  220. dbg("initializing slot %s", name);
  221. info->power_status = cpci_get_power_status(slot);
  222. info->attention_status = cpci_get_attention_status(slot);
  223. dbg("registering slot %s", name);
  224. status = pci_hp_register(slot->hotplug_slot, bus, i, name);
  225. if (status) {
  226. err("pci_hp_register failed with error %d", status);
  227. goto error_info;
  228. }
  229. dbg("slot registered with name: %s", slot_name(slot));
  230. /* Add slot to our internal list */
  231. down_write(&list_rwsem);
  232. list_add(&slot->slot_list, &slot_list);
  233. slots++;
  234. up_write(&list_rwsem);
  235. }
  236. return 0;
  237. error_info:
  238. kfree(info);
  239. error_hpslot:
  240. kfree(hotplug_slot);
  241. error_slot:
  242. kfree(slot);
  243. error:
  244. return status;
  245. }
  246. EXPORT_SYMBOL_GPL(cpci_hp_register_bus);
  247. int
  248. cpci_hp_unregister_bus(struct pci_bus *bus)
  249. {
  250. struct slot *slot;
  251. struct slot *tmp;
  252. int status = 0;
  253. down_write(&list_rwsem);
  254. if (!slots) {
  255. up_write(&list_rwsem);
  256. return -1;
  257. }
  258. list_for_each_entry_safe(slot, tmp, &slot_list, slot_list) {
  259. if (slot->bus == bus) {
  260. list_del(&slot->slot_list);
  261. slots--;
  262. dbg("deregistering slot %s", slot_name(slot));
  263. status = pci_hp_deregister(slot->hotplug_slot);
  264. if (status) {
  265. err("pci_hp_deregister failed with error %d",
  266. status);
  267. break;
  268. }
  269. }
  270. }
  271. up_write(&list_rwsem);
  272. return status;
  273. }
  274. EXPORT_SYMBOL_GPL(cpci_hp_unregister_bus);
  275. /* This is the interrupt mode interrupt handler */
  276. static irqreturn_t
  277. cpci_hp_intr(int irq, void *data)
  278. {
  279. dbg("entered cpci_hp_intr");
  280. /* Check to see if it was our interrupt */
  281. if ((controller->irq_flags & IRQF_SHARED) &&
  282. !controller->ops->check_irq(controller->dev_id)) {
  283. dbg("exited cpci_hp_intr, not our interrupt");
  284. return IRQ_NONE;
  285. }
  286. /* Disable ENUM interrupt */
  287. controller->ops->disable_irq();
  288. /* Trigger processing by the event thread */
  289. wake_up_process(cpci_thread);
  290. return IRQ_HANDLED;
  291. }
  292. /*
  293. * According to PICMG 2.1 R2.0, section 6.3.2, upon
  294. * initialization, the system driver shall clear the
  295. * INS bits of the cold-inserted devices.
  296. */
  297. static int
  298. init_slots(int clear_ins)
  299. {
  300. struct slot *slot;
  301. struct pci_dev *dev;
  302. dbg("%s - enter", __func__);
  303. down_read(&list_rwsem);
  304. if (!slots) {
  305. up_read(&list_rwsem);
  306. return -1;
  307. }
  308. list_for_each_entry(slot, &slot_list, slot_list) {
  309. dbg("%s - looking at slot %s", __func__, slot_name(slot));
  310. if (clear_ins && cpci_check_and_clear_ins(slot))
  311. dbg("%s - cleared INS for slot %s",
  312. __func__, slot_name(slot));
  313. dev = pci_get_slot(slot->bus, PCI_DEVFN(slot->number, 0));
  314. if (dev) {
  315. if (update_adapter_status(slot->hotplug_slot, 1))
  316. warn("failure to update adapter file");
  317. if (update_latch_status(slot->hotplug_slot, 1))
  318. warn("failure to update latch file");
  319. slot->dev = dev;
  320. }
  321. }
  322. up_read(&list_rwsem);
  323. dbg("%s - exit", __func__);
  324. return 0;
  325. }
  326. static int
  327. check_slots(void)
  328. {
  329. struct slot *slot;
  330. int extracted;
  331. int inserted;
  332. u16 hs_csr;
  333. down_read(&list_rwsem);
  334. if (!slots) {
  335. up_read(&list_rwsem);
  336. err("no slots registered, shutting down");
  337. return -1;
  338. }
  339. extracted = inserted = 0;
  340. list_for_each_entry(slot, &slot_list, slot_list) {
  341. dbg("%s - looking at slot %s", __func__, slot_name(slot));
  342. if (cpci_check_and_clear_ins(slot)) {
  343. /*
  344. * Some broken hardware (e.g. PLX 9054AB) asserts
  345. * ENUM# twice...
  346. */
  347. if (slot->dev) {
  348. warn("slot %s already inserted",
  349. slot_name(slot));
  350. inserted++;
  351. continue;
  352. }
  353. /* Process insertion */
  354. dbg("%s - slot %s inserted", __func__, slot_name(slot));
  355. /* GSM, debug */
  356. hs_csr = cpci_get_hs_csr(slot);
  357. dbg("%s - slot %s HS_CSR (1) = %04x",
  358. __func__, slot_name(slot), hs_csr);
  359. /* Configure device */
  360. dbg("%s - configuring slot %s",
  361. __func__, slot_name(slot));
  362. if (cpci_configure_slot(slot)) {
  363. err("%s - could not configure slot %s",
  364. __func__, slot_name(slot));
  365. continue;
  366. }
  367. dbg("%s - finished configuring slot %s",
  368. __func__, slot_name(slot));
  369. /* GSM, debug */
  370. hs_csr = cpci_get_hs_csr(slot);
  371. dbg("%s - slot %s HS_CSR (2) = %04x",
  372. __func__, slot_name(slot), hs_csr);
  373. if (update_latch_status(slot->hotplug_slot, 1))
  374. warn("failure to update latch file");
  375. if (update_adapter_status(slot->hotplug_slot, 1))
  376. warn("failure to update adapter file");
  377. cpci_led_off(slot);
  378. /* GSM, debug */
  379. hs_csr = cpci_get_hs_csr(slot);
  380. dbg("%s - slot %s HS_CSR (3) = %04x",
  381. __func__, slot_name(slot), hs_csr);
  382. inserted++;
  383. } else if (cpci_check_ext(slot)) {
  384. /* Process extraction request */
  385. dbg("%s - slot %s extracted",
  386. __func__, slot_name(slot));
  387. /* GSM, debug */
  388. hs_csr = cpci_get_hs_csr(slot);
  389. dbg("%s - slot %s HS_CSR = %04x",
  390. __func__, slot_name(slot), hs_csr);
  391. if (!slot->extracting) {
  392. if (update_latch_status(slot->hotplug_slot, 0))
  393. warn("failure to update latch file");
  394. slot->extracting = 1;
  395. atomic_inc(&extracting);
  396. }
  397. extracted++;
  398. } else if (slot->extracting) {
  399. hs_csr = cpci_get_hs_csr(slot);
  400. if (hs_csr == 0xffff) {
  401. /*
  402. * Hmmm, we're likely hosed at this point, should we
  403. * bother trying to tell the driver or not?
  404. */
  405. err("card in slot %s was improperly removed",
  406. slot_name(slot));
  407. if (update_adapter_status(slot->hotplug_slot, 0))
  408. warn("failure to update adapter file");
  409. slot->extracting = 0;
  410. atomic_dec(&extracting);
  411. }
  412. }
  413. }
  414. up_read(&list_rwsem);
  415. dbg("inserted=%d, extracted=%d, extracting=%d",
  416. inserted, extracted, atomic_read(&extracting));
  417. if (inserted || extracted)
  418. return extracted;
  419. else if (!atomic_read(&extracting)) {
  420. err("cannot find ENUM# source, shutting down");
  421. return -1;
  422. }
  423. return 0;
  424. }
  425. /* This is the interrupt mode worker thread body */
  426. static int
  427. event_thread(void *data)
  428. {
  429. int rc;
  430. dbg("%s - event thread started", __func__);
  431. while (1) {
  432. dbg("event thread sleeping");
  433. set_current_state(TASK_INTERRUPTIBLE);
  434. schedule();
  435. if (kthread_should_stop())
  436. break;
  437. do {
  438. rc = check_slots();
  439. if (rc > 0) {
  440. /* Give userspace a chance to handle extraction */
  441. msleep(500);
  442. } else if (rc < 0) {
  443. dbg("%s - error checking slots", __func__);
  444. thread_finished = 1;
  445. goto out;
  446. }
  447. } while (atomic_read(&extracting) && !kthread_should_stop());
  448. if (kthread_should_stop())
  449. break;
  450. /* Re-enable ENUM# interrupt */
  451. dbg("%s - re-enabling irq", __func__);
  452. controller->ops->enable_irq();
  453. }
  454. out:
  455. return 0;
  456. }
  457. /* This is the polling mode worker thread body */
  458. static int
  459. poll_thread(void *data)
  460. {
  461. int rc;
  462. while (1) {
  463. if (kthread_should_stop() || signal_pending(current))
  464. break;
  465. if (controller->ops->query_enum()) {
  466. do {
  467. rc = check_slots();
  468. if (rc > 0) {
  469. /* Give userspace a chance to handle extraction */
  470. msleep(500);
  471. } else if (rc < 0) {
  472. dbg("%s - error checking slots", __func__);
  473. thread_finished = 1;
  474. goto out;
  475. }
  476. } while (atomic_read(&extracting) && !kthread_should_stop());
  477. }
  478. msleep(100);
  479. }
  480. out:
  481. return 0;
  482. }
  483. static int
  484. cpci_start_thread(void)
  485. {
  486. if (controller->irq)
  487. cpci_thread = kthread_run(event_thread, NULL, "cpci_hp_eventd");
  488. else
  489. cpci_thread = kthread_run(poll_thread, NULL, "cpci_hp_polld");
  490. if (IS_ERR(cpci_thread)) {
  491. err("Can't start up our thread");
  492. return PTR_ERR(cpci_thread);
  493. }
  494. thread_finished = 0;
  495. return 0;
  496. }
  497. static void
  498. cpci_stop_thread(void)
  499. {
  500. kthread_stop(cpci_thread);
  501. thread_finished = 1;
  502. }
  503. int
  504. cpci_hp_register_controller(struct cpci_hp_controller *new_controller)
  505. {
  506. int status = 0;
  507. if (controller)
  508. return -1;
  509. if (!(new_controller && new_controller->ops))
  510. return -EINVAL;
  511. if (new_controller->irq) {
  512. if (!(new_controller->ops->enable_irq &&
  513. new_controller->ops->disable_irq))
  514. status = -EINVAL;
  515. if (request_irq(new_controller->irq,
  516. cpci_hp_intr,
  517. new_controller->irq_flags,
  518. MY_NAME,
  519. new_controller->dev_id)) {
  520. err("Can't get irq %d for the hotplug cPCI controller",
  521. new_controller->irq);
  522. status = -ENODEV;
  523. }
  524. dbg("%s - acquired controller irq %d",
  525. __func__, new_controller->irq);
  526. }
  527. if (!status)
  528. controller = new_controller;
  529. return status;
  530. }
  531. EXPORT_SYMBOL_GPL(cpci_hp_register_controller);
  532. static void
  533. cleanup_slots(void)
  534. {
  535. struct slot *slot;
  536. struct slot *tmp;
  537. /*
  538. * Unregister all of our slots with the pci_hotplug subsystem,
  539. * and free up all memory that we had allocated.
  540. */
  541. down_write(&list_rwsem);
  542. if (!slots)
  543. goto cleanup_null;
  544. list_for_each_entry_safe(slot, tmp, &slot_list, slot_list) {
  545. list_del(&slot->slot_list);
  546. pci_hp_deregister(slot->hotplug_slot);
  547. }
  548. cleanup_null:
  549. up_write(&list_rwsem);
  550. return;
  551. }
  552. int
  553. cpci_hp_unregister_controller(struct cpci_hp_controller *old_controller)
  554. {
  555. int status = 0;
  556. if (controller) {
  557. if (!thread_finished)
  558. cpci_stop_thread();
  559. if (controller->irq)
  560. free_irq(controller->irq, controller->dev_id);
  561. controller = NULL;
  562. cleanup_slots();
  563. } else
  564. status = -ENODEV;
  565. return status;
  566. }
  567. EXPORT_SYMBOL_GPL(cpci_hp_unregister_controller);
  568. int
  569. cpci_hp_start(void)
  570. {
  571. static int first = 1;
  572. int status;
  573. dbg("%s - enter", __func__);
  574. if (!controller)
  575. return -ENODEV;
  576. down_read(&list_rwsem);
  577. if (list_empty(&slot_list)) {
  578. up_read(&list_rwsem);
  579. return -ENODEV;
  580. }
  581. up_read(&list_rwsem);
  582. status = init_slots(first);
  583. if (first)
  584. first = 0;
  585. if (status)
  586. return status;
  587. status = cpci_start_thread();
  588. if (status)
  589. return status;
  590. dbg("%s - thread started", __func__);
  591. if (controller->irq) {
  592. /* Start enum interrupt processing */
  593. dbg("%s - enabling irq", __func__);
  594. controller->ops->enable_irq();
  595. }
  596. dbg("%s - exit", __func__);
  597. return 0;
  598. }
  599. EXPORT_SYMBOL_GPL(cpci_hp_start);
  600. int
  601. cpci_hp_stop(void)
  602. {
  603. if (!controller)
  604. return -ENODEV;
  605. if (controller->irq) {
  606. /* Stop enum interrupt processing */
  607. dbg("%s - disabling irq", __func__);
  608. controller->ops->disable_irq();
  609. }
  610. cpci_stop_thread();
  611. return 0;
  612. }
  613. EXPORT_SYMBOL_GPL(cpci_hp_stop);
  614. int __init
  615. cpci_hotplug_init(int debug)
  616. {
  617. cpci_debug = debug;
  618. return 0;
  619. }