connection.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Greybus connections
  4. *
  5. * Copyright 2014 Google Inc.
  6. * Copyright 2014 Linaro Ltd.
  7. */
  8. #include <linux/workqueue.h>
  9. #include <linux/greybus.h>
  10. #include "greybus_trace.h"
  11. #define GB_CONNECTION_CPORT_QUIESCE_TIMEOUT 1000
  12. static void gb_connection_kref_release(struct kref *kref);
  13. static DEFINE_SPINLOCK(gb_connections_lock);
  14. static DEFINE_MUTEX(gb_connection_mutex);
  15. /* Caller holds gb_connection_mutex. */
  16. static bool gb_connection_cport_in_use(struct gb_interface *intf, u16 cport_id)
  17. {
  18. struct gb_host_device *hd = intf->hd;
  19. struct gb_connection *connection;
  20. list_for_each_entry(connection, &hd->connections, hd_links) {
  21. if (connection->intf == intf &&
  22. connection->intf_cport_id == cport_id)
  23. return true;
  24. }
  25. return false;
  26. }
  27. static void gb_connection_get(struct gb_connection *connection)
  28. {
  29. kref_get(&connection->kref);
  30. trace_gb_connection_get(connection);
  31. }
  32. static void gb_connection_put(struct gb_connection *connection)
  33. {
  34. trace_gb_connection_put(connection);
  35. kref_put(&connection->kref, gb_connection_kref_release);
  36. }
  37. /*
  38. * Returns a reference-counted pointer to the connection if found.
  39. */
  40. static struct gb_connection *
  41. gb_connection_hd_find(struct gb_host_device *hd, u16 cport_id)
  42. {
  43. struct gb_connection *connection;
  44. unsigned long flags;
  45. spin_lock_irqsave(&gb_connections_lock, flags);
  46. list_for_each_entry(connection, &hd->connections, hd_links)
  47. if (connection->hd_cport_id == cport_id) {
  48. gb_connection_get(connection);
  49. goto found;
  50. }
  51. connection = NULL;
  52. found:
  53. spin_unlock_irqrestore(&gb_connections_lock, flags);
  54. return connection;
  55. }
  56. /*
  57. * Callback from the host driver to let us know that data has been
  58. * received on the bundle.
  59. */
  60. void greybus_data_rcvd(struct gb_host_device *hd, u16 cport_id,
  61. u8 *data, size_t length)
  62. {
  63. struct gb_connection *connection;
  64. trace_gb_hd_in(hd);
  65. connection = gb_connection_hd_find(hd, cport_id);
  66. if (!connection) {
  67. dev_err(&hd->dev,
  68. "nonexistent connection (%zu bytes dropped)\n", length);
  69. return;
  70. }
  71. gb_connection_recv(connection, data, length);
  72. gb_connection_put(connection);
  73. }
  74. EXPORT_SYMBOL_GPL(greybus_data_rcvd);
  75. static void gb_connection_kref_release(struct kref *kref)
  76. {
  77. struct gb_connection *connection;
  78. connection = container_of(kref, struct gb_connection, kref);
  79. trace_gb_connection_release(connection);
  80. kfree(connection);
  81. }
  82. static void gb_connection_init_name(struct gb_connection *connection)
  83. {
  84. u16 hd_cport_id = connection->hd_cport_id;
  85. u16 cport_id = 0;
  86. u8 intf_id = 0;
  87. if (connection->intf) {
  88. intf_id = connection->intf->interface_id;
  89. cport_id = connection->intf_cport_id;
  90. }
  91. snprintf(connection->name, sizeof(connection->name),
  92. "%u/%u:%u", hd_cport_id, intf_id, cport_id);
  93. }
  94. /*
  95. * _gb_connection_create() - create a Greybus connection
  96. * @hd: host device of the connection
  97. * @hd_cport_id: host-device cport id, or -1 for dynamic allocation
  98. * @intf: remote interface, or NULL for static connections
  99. * @bundle: remote-interface bundle (may be NULL)
  100. * @cport_id: remote-interface cport id, or 0 for static connections
  101. * @handler: request handler (may be NULL)
  102. * @flags: connection flags
  103. *
  104. * Create a Greybus connection, representing the bidirectional link
  105. * between a CPort on a (local) Greybus host device and a CPort on
  106. * another Greybus interface.
  107. *
  108. * A connection also maintains the state of operations sent over the
  109. * connection.
  110. *
  111. * Serialised against concurrent create and destroy using the
  112. * gb_connection_mutex.
  113. *
  114. * Return: A pointer to the new connection if successful, or an ERR_PTR
  115. * otherwise.
  116. */
  117. static struct gb_connection *
  118. _gb_connection_create(struct gb_host_device *hd, int hd_cport_id,
  119. struct gb_interface *intf,
  120. struct gb_bundle *bundle, int cport_id,
  121. gb_request_handler_t handler,
  122. unsigned long flags)
  123. {
  124. struct gb_connection *connection;
  125. int ret;
  126. mutex_lock(&gb_connection_mutex);
  127. if (intf && gb_connection_cport_in_use(intf, cport_id)) {
  128. dev_err(&intf->dev, "cport %u already in use\n", cport_id);
  129. ret = -EBUSY;
  130. goto err_unlock;
  131. }
  132. ret = gb_hd_cport_allocate(hd, hd_cport_id, flags);
  133. if (ret < 0) {
  134. dev_err(&hd->dev, "failed to allocate cport: %d\n", ret);
  135. goto err_unlock;
  136. }
  137. hd_cport_id = ret;
  138. connection = kzalloc(sizeof(*connection), GFP_KERNEL);
  139. if (!connection) {
  140. ret = -ENOMEM;
  141. goto err_hd_cport_release;
  142. }
  143. connection->hd_cport_id = hd_cport_id;
  144. connection->intf_cport_id = cport_id;
  145. connection->hd = hd;
  146. connection->intf = intf;
  147. connection->bundle = bundle;
  148. connection->handler = handler;
  149. connection->flags = flags;
  150. if (intf && (intf->quirks & GB_INTERFACE_QUIRK_NO_CPORT_FEATURES))
  151. connection->flags |= GB_CONNECTION_FLAG_NO_FLOWCTRL;
  152. connection->state = GB_CONNECTION_STATE_DISABLED;
  153. atomic_set(&connection->op_cycle, 0);
  154. mutex_init(&connection->mutex);
  155. spin_lock_init(&connection->lock);
  156. INIT_LIST_HEAD(&connection->operations);
  157. connection->wq = alloc_workqueue("%s:%d", WQ_UNBOUND, 1,
  158. dev_name(&hd->dev), hd_cport_id);
  159. if (!connection->wq) {
  160. ret = -ENOMEM;
  161. goto err_free_connection;
  162. }
  163. kref_init(&connection->kref);
  164. gb_connection_init_name(connection);
  165. spin_lock_irq(&gb_connections_lock);
  166. list_add(&connection->hd_links, &hd->connections);
  167. if (bundle)
  168. list_add(&connection->bundle_links, &bundle->connections);
  169. else
  170. INIT_LIST_HEAD(&connection->bundle_links);
  171. spin_unlock_irq(&gb_connections_lock);
  172. mutex_unlock(&gb_connection_mutex);
  173. trace_gb_connection_create(connection);
  174. return connection;
  175. err_free_connection:
  176. kfree(connection);
  177. err_hd_cport_release:
  178. gb_hd_cport_release(hd, hd_cport_id);
  179. err_unlock:
  180. mutex_unlock(&gb_connection_mutex);
  181. return ERR_PTR(ret);
  182. }
  183. struct gb_connection *
  184. gb_connection_create_static(struct gb_host_device *hd, u16 hd_cport_id,
  185. gb_request_handler_t handler)
  186. {
  187. return _gb_connection_create(hd, hd_cport_id, NULL, NULL, 0, handler,
  188. GB_CONNECTION_FLAG_HIGH_PRIO);
  189. }
  190. struct gb_connection *
  191. gb_connection_create_control(struct gb_interface *intf)
  192. {
  193. return _gb_connection_create(intf->hd, -1, intf, NULL, 0, NULL,
  194. GB_CONNECTION_FLAG_CONTROL |
  195. GB_CONNECTION_FLAG_HIGH_PRIO);
  196. }
  197. struct gb_connection *
  198. gb_connection_create(struct gb_bundle *bundle, u16 cport_id,
  199. gb_request_handler_t handler)
  200. {
  201. struct gb_interface *intf = bundle->intf;
  202. return _gb_connection_create(intf->hd, -1, intf, bundle, cport_id,
  203. handler, 0);
  204. }
  205. EXPORT_SYMBOL_GPL(gb_connection_create);
  206. struct gb_connection *
  207. gb_connection_create_flags(struct gb_bundle *bundle, u16 cport_id,
  208. gb_request_handler_t handler,
  209. unsigned long flags)
  210. {
  211. struct gb_interface *intf = bundle->intf;
  212. if (WARN_ON_ONCE(flags & GB_CONNECTION_FLAG_CORE_MASK))
  213. flags &= ~GB_CONNECTION_FLAG_CORE_MASK;
  214. return _gb_connection_create(intf->hd, -1, intf, bundle, cport_id,
  215. handler, flags);
  216. }
  217. EXPORT_SYMBOL_GPL(gb_connection_create_flags);
  218. struct gb_connection *
  219. gb_connection_create_offloaded(struct gb_bundle *bundle, u16 cport_id,
  220. unsigned long flags)
  221. {
  222. flags |= GB_CONNECTION_FLAG_OFFLOADED;
  223. return gb_connection_create_flags(bundle, cport_id, NULL, flags);
  224. }
  225. EXPORT_SYMBOL_GPL(gb_connection_create_offloaded);
  226. static int gb_connection_hd_cport_enable(struct gb_connection *connection)
  227. {
  228. struct gb_host_device *hd = connection->hd;
  229. int ret;
  230. if (!hd->driver->cport_enable)
  231. return 0;
  232. ret = hd->driver->cport_enable(hd, connection->hd_cport_id,
  233. connection->flags);
  234. if (ret) {
  235. dev_err(&hd->dev, "%s: failed to enable host cport: %d\n",
  236. connection->name, ret);
  237. return ret;
  238. }
  239. return 0;
  240. }
  241. static void gb_connection_hd_cport_disable(struct gb_connection *connection)
  242. {
  243. struct gb_host_device *hd = connection->hd;
  244. int ret;
  245. if (!hd->driver->cport_disable)
  246. return;
  247. ret = hd->driver->cport_disable(hd, connection->hd_cport_id);
  248. if (ret) {
  249. dev_err(&hd->dev, "%s: failed to disable host cport: %d\n",
  250. connection->name, ret);
  251. }
  252. }
  253. static int gb_connection_hd_cport_connected(struct gb_connection *connection)
  254. {
  255. struct gb_host_device *hd = connection->hd;
  256. int ret;
  257. if (!hd->driver->cport_connected)
  258. return 0;
  259. ret = hd->driver->cport_connected(hd, connection->hd_cport_id);
  260. if (ret) {
  261. dev_err(&hd->dev, "%s: failed to set connected state: %d\n",
  262. connection->name, ret);
  263. return ret;
  264. }
  265. return 0;
  266. }
  267. static int gb_connection_hd_cport_flush(struct gb_connection *connection)
  268. {
  269. struct gb_host_device *hd = connection->hd;
  270. int ret;
  271. if (!hd->driver->cport_flush)
  272. return 0;
  273. ret = hd->driver->cport_flush(hd, connection->hd_cport_id);
  274. if (ret) {
  275. dev_err(&hd->dev, "%s: failed to flush host cport: %d\n",
  276. connection->name, ret);
  277. return ret;
  278. }
  279. return 0;
  280. }
  281. static int gb_connection_hd_cport_quiesce(struct gb_connection *connection)
  282. {
  283. struct gb_host_device *hd = connection->hd;
  284. size_t peer_space;
  285. int ret;
  286. if (!hd->driver->cport_quiesce)
  287. return 0;
  288. peer_space = sizeof(struct gb_operation_msg_hdr) +
  289. sizeof(struct gb_cport_shutdown_request);
  290. if (connection->mode_switch)
  291. peer_space += sizeof(struct gb_operation_msg_hdr);
  292. ret = hd->driver->cport_quiesce(hd, connection->hd_cport_id,
  293. peer_space,
  294. GB_CONNECTION_CPORT_QUIESCE_TIMEOUT);
  295. if (ret) {
  296. dev_err(&hd->dev, "%s: failed to quiesce host cport: %d\n",
  297. connection->name, ret);
  298. return ret;
  299. }
  300. return 0;
  301. }
  302. static int gb_connection_hd_cport_clear(struct gb_connection *connection)
  303. {
  304. struct gb_host_device *hd = connection->hd;
  305. int ret;
  306. if (!hd->driver->cport_clear)
  307. return 0;
  308. ret = hd->driver->cport_clear(hd, connection->hd_cport_id);
  309. if (ret) {
  310. dev_err(&hd->dev, "%s: failed to clear host cport: %d\n",
  311. connection->name, ret);
  312. return ret;
  313. }
  314. return 0;
  315. }
  316. /*
  317. * Request the SVC to create a connection from AP's cport to interface's
  318. * cport.
  319. */
  320. static int
  321. gb_connection_svc_connection_create(struct gb_connection *connection)
  322. {
  323. struct gb_host_device *hd = connection->hd;
  324. struct gb_interface *intf;
  325. u8 cport_flags;
  326. int ret;
  327. if (gb_connection_is_static(connection))
  328. return 0;
  329. intf = connection->intf;
  330. /*
  331. * Enable either E2EFC or CSD, unless no flow control is requested.
  332. */
  333. cport_flags = GB_SVC_CPORT_FLAG_CSV_N;
  334. if (gb_connection_flow_control_disabled(connection)) {
  335. cport_flags |= GB_SVC_CPORT_FLAG_CSD_N;
  336. } else if (gb_connection_e2efc_enabled(connection)) {
  337. cport_flags |= GB_SVC_CPORT_FLAG_CSD_N |
  338. GB_SVC_CPORT_FLAG_E2EFC;
  339. }
  340. ret = gb_svc_connection_create(hd->svc,
  341. hd->svc->ap_intf_id,
  342. connection->hd_cport_id,
  343. intf->interface_id,
  344. connection->intf_cport_id,
  345. cport_flags);
  346. if (ret) {
  347. dev_err(&connection->hd->dev,
  348. "%s: failed to create svc connection: %d\n",
  349. connection->name, ret);
  350. return ret;
  351. }
  352. return 0;
  353. }
  354. static void
  355. gb_connection_svc_connection_destroy(struct gb_connection *connection)
  356. {
  357. if (gb_connection_is_static(connection))
  358. return;
  359. gb_svc_connection_destroy(connection->hd->svc,
  360. connection->hd->svc->ap_intf_id,
  361. connection->hd_cport_id,
  362. connection->intf->interface_id,
  363. connection->intf_cport_id);
  364. }
  365. /* Inform Interface about active CPorts */
  366. static int gb_connection_control_connected(struct gb_connection *connection)
  367. {
  368. struct gb_control *control;
  369. u16 cport_id = connection->intf_cport_id;
  370. int ret;
  371. if (gb_connection_is_static(connection))
  372. return 0;
  373. if (gb_connection_is_control(connection))
  374. return 0;
  375. control = connection->intf->control;
  376. ret = gb_control_connected_operation(control, cport_id);
  377. if (ret) {
  378. dev_err(&connection->bundle->dev,
  379. "failed to connect cport: %d\n", ret);
  380. return ret;
  381. }
  382. return 0;
  383. }
  384. static void
  385. gb_connection_control_disconnecting(struct gb_connection *connection)
  386. {
  387. struct gb_control *control;
  388. u16 cport_id = connection->intf_cport_id;
  389. int ret;
  390. if (gb_connection_is_static(connection))
  391. return;
  392. control = connection->intf->control;
  393. ret = gb_control_disconnecting_operation(control, cport_id);
  394. if (ret) {
  395. dev_err(&connection->hd->dev,
  396. "%s: failed to send disconnecting: %d\n",
  397. connection->name, ret);
  398. }
  399. }
  400. static void
  401. gb_connection_control_disconnected(struct gb_connection *connection)
  402. {
  403. struct gb_control *control;
  404. u16 cport_id = connection->intf_cport_id;
  405. int ret;
  406. if (gb_connection_is_static(connection))
  407. return;
  408. control = connection->intf->control;
  409. if (gb_connection_is_control(connection)) {
  410. if (connection->mode_switch) {
  411. ret = gb_control_mode_switch_operation(control);
  412. if (ret) {
  413. /*
  414. * Allow mode switch to time out waiting for
  415. * mailbox event.
  416. */
  417. return;
  418. }
  419. }
  420. return;
  421. }
  422. ret = gb_control_disconnected_operation(control, cport_id);
  423. if (ret) {
  424. dev_warn(&connection->bundle->dev,
  425. "failed to disconnect cport: %d\n", ret);
  426. }
  427. }
  428. static int gb_connection_shutdown_operation(struct gb_connection *connection,
  429. u8 phase)
  430. {
  431. struct gb_cport_shutdown_request *req;
  432. struct gb_operation *operation;
  433. int ret;
  434. operation = gb_operation_create_core(connection,
  435. GB_REQUEST_TYPE_CPORT_SHUTDOWN,
  436. sizeof(*req), 0, 0,
  437. GFP_KERNEL);
  438. if (!operation)
  439. return -ENOMEM;
  440. req = operation->request->payload;
  441. req->phase = phase;
  442. ret = gb_operation_request_send_sync(operation);
  443. gb_operation_put(operation);
  444. return ret;
  445. }
  446. static int gb_connection_cport_shutdown(struct gb_connection *connection,
  447. u8 phase)
  448. {
  449. struct gb_host_device *hd = connection->hd;
  450. const struct gb_hd_driver *drv = hd->driver;
  451. int ret;
  452. if (gb_connection_is_static(connection))
  453. return 0;
  454. if (gb_connection_is_offloaded(connection)) {
  455. if (!drv->cport_shutdown)
  456. return 0;
  457. ret = drv->cport_shutdown(hd, connection->hd_cport_id, phase,
  458. GB_OPERATION_TIMEOUT_DEFAULT);
  459. } else {
  460. ret = gb_connection_shutdown_operation(connection, phase);
  461. }
  462. if (ret) {
  463. dev_err(&hd->dev, "%s: failed to send cport shutdown (phase %d): %d\n",
  464. connection->name, phase, ret);
  465. return ret;
  466. }
  467. return 0;
  468. }
  469. static int
  470. gb_connection_cport_shutdown_phase_1(struct gb_connection *connection)
  471. {
  472. return gb_connection_cport_shutdown(connection, 1);
  473. }
  474. static int
  475. gb_connection_cport_shutdown_phase_2(struct gb_connection *connection)
  476. {
  477. return gb_connection_cport_shutdown(connection, 2);
  478. }
  479. /*
  480. * Cancel all active operations on a connection.
  481. *
  482. * Locking: Called with connection lock held and state set to DISABLED or
  483. * DISCONNECTING.
  484. */
  485. static void gb_connection_cancel_operations(struct gb_connection *connection,
  486. int errno)
  487. __must_hold(&connection->lock)
  488. {
  489. struct gb_operation *operation;
  490. while (!list_empty(&connection->operations)) {
  491. operation = list_last_entry(&connection->operations,
  492. struct gb_operation, links);
  493. gb_operation_get(operation);
  494. spin_unlock_irq(&connection->lock);
  495. if (gb_operation_is_incoming(operation))
  496. gb_operation_cancel_incoming(operation, errno);
  497. else
  498. gb_operation_cancel(operation, errno);
  499. gb_operation_put(operation);
  500. spin_lock_irq(&connection->lock);
  501. }
  502. }
  503. /*
  504. * Cancel all active incoming operations on a connection.
  505. *
  506. * Locking: Called with connection lock held and state set to ENABLED_TX.
  507. */
  508. static void
  509. gb_connection_flush_incoming_operations(struct gb_connection *connection,
  510. int errno)
  511. __must_hold(&connection->lock)
  512. {
  513. struct gb_operation *operation;
  514. bool incoming;
  515. while (!list_empty(&connection->operations)) {
  516. incoming = false;
  517. list_for_each_entry(operation, &connection->operations,
  518. links) {
  519. if (gb_operation_is_incoming(operation)) {
  520. gb_operation_get(operation);
  521. incoming = true;
  522. break;
  523. }
  524. }
  525. if (!incoming)
  526. break;
  527. spin_unlock_irq(&connection->lock);
  528. /* FIXME: flush, not cancel? */
  529. gb_operation_cancel_incoming(operation, errno);
  530. gb_operation_put(operation);
  531. spin_lock_irq(&connection->lock);
  532. }
  533. }
  534. /*
  535. * _gb_connection_enable() - enable a connection
  536. * @connection: connection to enable
  537. * @rx: whether to enable incoming requests
  538. *
  539. * Connection-enable helper for DISABLED->ENABLED, DISABLED->ENABLED_TX, and
  540. * ENABLED_TX->ENABLED state transitions.
  541. *
  542. * Locking: Caller holds connection->mutex.
  543. */
  544. static int _gb_connection_enable(struct gb_connection *connection, bool rx)
  545. {
  546. int ret;
  547. /* Handle ENABLED_TX -> ENABLED transitions. */
  548. if (connection->state == GB_CONNECTION_STATE_ENABLED_TX) {
  549. if (!(connection->handler && rx))
  550. return 0;
  551. spin_lock_irq(&connection->lock);
  552. connection->state = GB_CONNECTION_STATE_ENABLED;
  553. spin_unlock_irq(&connection->lock);
  554. return 0;
  555. }
  556. ret = gb_connection_hd_cport_enable(connection);
  557. if (ret)
  558. return ret;
  559. ret = gb_connection_svc_connection_create(connection);
  560. if (ret)
  561. goto err_hd_cport_clear;
  562. ret = gb_connection_hd_cport_connected(connection);
  563. if (ret)
  564. goto err_svc_connection_destroy;
  565. spin_lock_irq(&connection->lock);
  566. if (connection->handler && rx)
  567. connection->state = GB_CONNECTION_STATE_ENABLED;
  568. else
  569. connection->state = GB_CONNECTION_STATE_ENABLED_TX;
  570. spin_unlock_irq(&connection->lock);
  571. ret = gb_connection_control_connected(connection);
  572. if (ret)
  573. goto err_control_disconnecting;
  574. return 0;
  575. err_control_disconnecting:
  576. spin_lock_irq(&connection->lock);
  577. connection->state = GB_CONNECTION_STATE_DISCONNECTING;
  578. gb_connection_cancel_operations(connection, -ESHUTDOWN);
  579. spin_unlock_irq(&connection->lock);
  580. /* Transmit queue should already be empty. */
  581. gb_connection_hd_cport_flush(connection);
  582. gb_connection_control_disconnecting(connection);
  583. gb_connection_cport_shutdown_phase_1(connection);
  584. gb_connection_hd_cport_quiesce(connection);
  585. gb_connection_cport_shutdown_phase_2(connection);
  586. gb_connection_control_disconnected(connection);
  587. connection->state = GB_CONNECTION_STATE_DISABLED;
  588. err_svc_connection_destroy:
  589. gb_connection_svc_connection_destroy(connection);
  590. err_hd_cport_clear:
  591. gb_connection_hd_cport_clear(connection);
  592. gb_connection_hd_cport_disable(connection);
  593. return ret;
  594. }
  595. int gb_connection_enable(struct gb_connection *connection)
  596. {
  597. int ret = 0;
  598. mutex_lock(&connection->mutex);
  599. if (connection->state == GB_CONNECTION_STATE_ENABLED)
  600. goto out_unlock;
  601. ret = _gb_connection_enable(connection, true);
  602. if (!ret)
  603. trace_gb_connection_enable(connection);
  604. out_unlock:
  605. mutex_unlock(&connection->mutex);
  606. return ret;
  607. }
  608. EXPORT_SYMBOL_GPL(gb_connection_enable);
  609. int gb_connection_enable_tx(struct gb_connection *connection)
  610. {
  611. int ret = 0;
  612. mutex_lock(&connection->mutex);
  613. if (connection->state == GB_CONNECTION_STATE_ENABLED) {
  614. ret = -EINVAL;
  615. goto out_unlock;
  616. }
  617. if (connection->state == GB_CONNECTION_STATE_ENABLED_TX)
  618. goto out_unlock;
  619. ret = _gb_connection_enable(connection, false);
  620. if (!ret)
  621. trace_gb_connection_enable(connection);
  622. out_unlock:
  623. mutex_unlock(&connection->mutex);
  624. return ret;
  625. }
  626. EXPORT_SYMBOL_GPL(gb_connection_enable_tx);
  627. void gb_connection_disable_rx(struct gb_connection *connection)
  628. {
  629. mutex_lock(&connection->mutex);
  630. spin_lock_irq(&connection->lock);
  631. if (connection->state != GB_CONNECTION_STATE_ENABLED) {
  632. spin_unlock_irq(&connection->lock);
  633. goto out_unlock;
  634. }
  635. connection->state = GB_CONNECTION_STATE_ENABLED_TX;
  636. gb_connection_flush_incoming_operations(connection, -ESHUTDOWN);
  637. spin_unlock_irq(&connection->lock);
  638. trace_gb_connection_disable(connection);
  639. out_unlock:
  640. mutex_unlock(&connection->mutex);
  641. }
  642. EXPORT_SYMBOL_GPL(gb_connection_disable_rx);
  643. void gb_connection_mode_switch_prepare(struct gb_connection *connection)
  644. {
  645. connection->mode_switch = true;
  646. }
  647. void gb_connection_mode_switch_complete(struct gb_connection *connection)
  648. {
  649. gb_connection_svc_connection_destroy(connection);
  650. gb_connection_hd_cport_clear(connection);
  651. gb_connection_hd_cport_disable(connection);
  652. connection->mode_switch = false;
  653. }
  654. void gb_connection_disable(struct gb_connection *connection)
  655. {
  656. mutex_lock(&connection->mutex);
  657. if (connection->state == GB_CONNECTION_STATE_DISABLED)
  658. goto out_unlock;
  659. trace_gb_connection_disable(connection);
  660. spin_lock_irq(&connection->lock);
  661. connection->state = GB_CONNECTION_STATE_DISCONNECTING;
  662. gb_connection_cancel_operations(connection, -ESHUTDOWN);
  663. spin_unlock_irq(&connection->lock);
  664. gb_connection_hd_cport_flush(connection);
  665. gb_connection_control_disconnecting(connection);
  666. gb_connection_cport_shutdown_phase_1(connection);
  667. gb_connection_hd_cport_quiesce(connection);
  668. gb_connection_cport_shutdown_phase_2(connection);
  669. gb_connection_control_disconnected(connection);
  670. connection->state = GB_CONNECTION_STATE_DISABLED;
  671. /* control-connection tear down is deferred when mode switching */
  672. if (!connection->mode_switch) {
  673. gb_connection_svc_connection_destroy(connection);
  674. gb_connection_hd_cport_clear(connection);
  675. gb_connection_hd_cport_disable(connection);
  676. }
  677. out_unlock:
  678. mutex_unlock(&connection->mutex);
  679. }
  680. EXPORT_SYMBOL_GPL(gb_connection_disable);
  681. /* Disable a connection without communicating with the remote end. */
  682. void gb_connection_disable_forced(struct gb_connection *connection)
  683. {
  684. mutex_lock(&connection->mutex);
  685. if (connection->state == GB_CONNECTION_STATE_DISABLED)
  686. goto out_unlock;
  687. trace_gb_connection_disable(connection);
  688. spin_lock_irq(&connection->lock);
  689. connection->state = GB_CONNECTION_STATE_DISABLED;
  690. gb_connection_cancel_operations(connection, -ESHUTDOWN);
  691. spin_unlock_irq(&connection->lock);
  692. gb_connection_hd_cport_flush(connection);
  693. gb_connection_svc_connection_destroy(connection);
  694. gb_connection_hd_cport_clear(connection);
  695. gb_connection_hd_cport_disable(connection);
  696. out_unlock:
  697. mutex_unlock(&connection->mutex);
  698. }
  699. EXPORT_SYMBOL_GPL(gb_connection_disable_forced);
  700. /* Caller must have disabled the connection before destroying it. */
  701. void gb_connection_destroy(struct gb_connection *connection)
  702. {
  703. if (!connection)
  704. return;
  705. if (WARN_ON(connection->state != GB_CONNECTION_STATE_DISABLED))
  706. gb_connection_disable(connection);
  707. mutex_lock(&gb_connection_mutex);
  708. spin_lock_irq(&gb_connections_lock);
  709. list_del(&connection->bundle_links);
  710. list_del(&connection->hd_links);
  711. spin_unlock_irq(&gb_connections_lock);
  712. destroy_workqueue(connection->wq);
  713. gb_hd_cport_release(connection->hd, connection->hd_cport_id);
  714. connection->hd_cport_id = CPORT_ID_BAD;
  715. mutex_unlock(&gb_connection_mutex);
  716. gb_connection_put(connection);
  717. }
  718. EXPORT_SYMBOL_GPL(gb_connection_destroy);
  719. void gb_connection_latency_tag_enable(struct gb_connection *connection)
  720. {
  721. struct gb_host_device *hd = connection->hd;
  722. int ret;
  723. if (!hd->driver->latency_tag_enable)
  724. return;
  725. ret = hd->driver->latency_tag_enable(hd, connection->hd_cport_id);
  726. if (ret) {
  727. dev_err(&connection->hd->dev,
  728. "%s: failed to enable latency tag: %d\n",
  729. connection->name, ret);
  730. }
  731. }
  732. EXPORT_SYMBOL_GPL(gb_connection_latency_tag_enable);
  733. void gb_connection_latency_tag_disable(struct gb_connection *connection)
  734. {
  735. struct gb_host_device *hd = connection->hd;
  736. int ret;
  737. if (!hd->driver->latency_tag_disable)
  738. return;
  739. ret = hd->driver->latency_tag_disable(hd, connection->hd_cport_id);
  740. if (ret) {
  741. dev_err(&connection->hd->dev,
  742. "%s: failed to disable latency tag: %d\n",
  743. connection->name, ret);
  744. }
  745. }
  746. EXPORT_SYMBOL_GPL(gb_connection_latency_tag_disable);