connection.c 22 KB

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