btintel_pcie.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * Bluetooth support for Intel PCIe devices
  5. *
  6. * Copyright (C) 2024 Intel Corporation
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/firmware.h>
  11. #include <linux/pci.h>
  12. #include <linux/wait.h>
  13. #include <linux/delay.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/unaligned.h>
  16. #include <net/bluetooth/bluetooth.h>
  17. #include <net/bluetooth/hci_core.h>
  18. #include "btintel.h"
  19. #include "btintel_pcie.h"
  20. #define VERSION "0.1"
  21. #define BTINTEL_PCI_DEVICE(dev, subdev) \
  22. .vendor = PCI_VENDOR_ID_INTEL, \
  23. .device = (dev), \
  24. .subvendor = PCI_ANY_ID, \
  25. .subdevice = (subdev), \
  26. .driver_data = 0
  27. #define POLL_INTERVAL_US 10
  28. /* Intel Bluetooth PCIe device id table */
  29. static const struct pci_device_id btintel_pcie_table[] = {
  30. { BTINTEL_PCI_DEVICE(0xA876, PCI_ANY_ID) },
  31. { BTINTEL_PCI_DEVICE(0xE476, PCI_ANY_ID) },
  32. { 0 }
  33. };
  34. MODULE_DEVICE_TABLE(pci, btintel_pcie_table);
  35. /* Intel PCIe uses 4 bytes of HCI type instead of 1 byte BT SIG HCI type */
  36. #define BTINTEL_PCIE_HCI_TYPE_LEN 4
  37. #define BTINTEL_PCIE_HCI_CMD_PKT 0x00000001
  38. #define BTINTEL_PCIE_HCI_ACL_PKT 0x00000002
  39. #define BTINTEL_PCIE_HCI_SCO_PKT 0x00000003
  40. #define BTINTEL_PCIE_HCI_EVT_PKT 0x00000004
  41. #define BTINTEL_PCIE_HCI_ISO_PKT 0x00000005
  42. /* Alive interrupt context */
  43. enum {
  44. BTINTEL_PCIE_ROM,
  45. BTINTEL_PCIE_FW_DL,
  46. BTINTEL_PCIE_HCI_RESET,
  47. BTINTEL_PCIE_INTEL_HCI_RESET1,
  48. BTINTEL_PCIE_INTEL_HCI_RESET2,
  49. BTINTEL_PCIE_D0,
  50. BTINTEL_PCIE_D3
  51. };
  52. static inline void ipc_print_ia_ring(struct hci_dev *hdev, struct ia *ia,
  53. u16 queue_num)
  54. {
  55. bt_dev_dbg(hdev, "IA: %s: tr-h:%02u tr-t:%02u cr-h:%02u cr-t:%02u",
  56. queue_num == BTINTEL_PCIE_TXQ_NUM ? "TXQ" : "RXQ",
  57. ia->tr_hia[queue_num], ia->tr_tia[queue_num],
  58. ia->cr_hia[queue_num], ia->cr_tia[queue_num]);
  59. }
  60. static inline void ipc_print_urbd1(struct hci_dev *hdev, struct urbd1 *urbd1,
  61. u16 index)
  62. {
  63. bt_dev_dbg(hdev, "RXQ:urbd1(%u) frbd_tag:%u status: 0x%x fixed:0x%x",
  64. index, urbd1->frbd_tag, urbd1->status, urbd1->fixed);
  65. }
  66. static int btintel_pcie_poll_bit(struct btintel_pcie_data *data, u32 offset,
  67. u32 bits, u32 mask, int timeout_us)
  68. {
  69. int t = 0;
  70. u32 reg;
  71. do {
  72. reg = btintel_pcie_rd_reg32(data, offset);
  73. if ((reg & mask) == (bits & mask))
  74. return t;
  75. udelay(POLL_INTERVAL_US);
  76. t += POLL_INTERVAL_US;
  77. } while (t < timeout_us);
  78. return -ETIMEDOUT;
  79. }
  80. static struct btintel_pcie_data *btintel_pcie_get_data(struct msix_entry *entry)
  81. {
  82. u8 queue = entry->entry;
  83. struct msix_entry *entries = entry - queue;
  84. return container_of(entries, struct btintel_pcie_data, msix_entries[0]);
  85. }
  86. /* Set the doorbell for TXQ to notify the device that @index (actually index-1)
  87. * of the TFD is updated and ready to transmit.
  88. */
  89. static void btintel_pcie_set_tx_db(struct btintel_pcie_data *data, u16 index)
  90. {
  91. u32 val;
  92. val = index;
  93. val |= (BTINTEL_PCIE_TX_DB_VEC << 16);
  94. btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_HBUS_TARG_WRPTR, val);
  95. }
  96. /* Copy the data to next(@tfd_index) data buffer and update the TFD(transfer
  97. * descriptor) with the data length and the DMA address of the data buffer.
  98. */
  99. static void btintel_pcie_prepare_tx(struct txq *txq, u16 tfd_index,
  100. struct sk_buff *skb)
  101. {
  102. struct data_buf *buf;
  103. struct tfd *tfd;
  104. tfd = &txq->tfds[tfd_index];
  105. memset(tfd, 0, sizeof(*tfd));
  106. buf = &txq->bufs[tfd_index];
  107. tfd->size = skb->len;
  108. tfd->addr = buf->data_p_addr;
  109. /* Copy the outgoing data to DMA buffer */
  110. memcpy(buf->data, skb->data, tfd->size);
  111. }
  112. static int btintel_pcie_send_sync(struct btintel_pcie_data *data,
  113. struct sk_buff *skb)
  114. {
  115. int ret;
  116. u16 tfd_index;
  117. struct txq *txq = &data->txq;
  118. tfd_index = data->ia.tr_hia[BTINTEL_PCIE_TXQ_NUM];
  119. if (tfd_index > txq->count)
  120. return -ERANGE;
  121. /* Prepare for TX. It updates the TFD with the length of data and
  122. * address of the DMA buffer, and copy the data to the DMA buffer
  123. */
  124. btintel_pcie_prepare_tx(txq, tfd_index, skb);
  125. tfd_index = (tfd_index + 1) % txq->count;
  126. data->ia.tr_hia[BTINTEL_PCIE_TXQ_NUM] = tfd_index;
  127. /* Arm wait event condition */
  128. data->tx_wait_done = false;
  129. /* Set the doorbell to notify the device */
  130. btintel_pcie_set_tx_db(data, tfd_index);
  131. /* Wait for the complete interrupt - URBD0 */
  132. ret = wait_event_timeout(data->tx_wait_q, data->tx_wait_done,
  133. msecs_to_jiffies(BTINTEL_PCIE_TX_WAIT_TIMEOUT_MS));
  134. if (!ret)
  135. return -ETIME;
  136. return 0;
  137. }
  138. /* Set the doorbell for RXQ to notify the device that @index (actually index-1)
  139. * is available to receive the data
  140. */
  141. static void btintel_pcie_set_rx_db(struct btintel_pcie_data *data, u16 index)
  142. {
  143. u32 val;
  144. val = index;
  145. val |= (BTINTEL_PCIE_RX_DB_VEC << 16);
  146. btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_HBUS_TARG_WRPTR, val);
  147. }
  148. /* Update the FRBD (free buffer descriptor) with the @frbd_index and the
  149. * DMA address of the free buffer.
  150. */
  151. static void btintel_pcie_prepare_rx(struct rxq *rxq, u16 frbd_index)
  152. {
  153. struct data_buf *buf;
  154. struct frbd *frbd;
  155. /* Get the buffer of the FRBD for DMA */
  156. buf = &rxq->bufs[frbd_index];
  157. frbd = &rxq->frbds[frbd_index];
  158. memset(frbd, 0, sizeof(*frbd));
  159. /* Update FRBD */
  160. frbd->tag = frbd_index;
  161. frbd->addr = buf->data_p_addr;
  162. }
  163. static int btintel_pcie_submit_rx(struct btintel_pcie_data *data)
  164. {
  165. u16 frbd_index;
  166. struct rxq *rxq = &data->rxq;
  167. frbd_index = data->ia.tr_hia[BTINTEL_PCIE_RXQ_NUM];
  168. if (frbd_index > rxq->count)
  169. return -ERANGE;
  170. /* Prepare for RX submit. It updates the FRBD with the address of DMA
  171. * buffer
  172. */
  173. btintel_pcie_prepare_rx(rxq, frbd_index);
  174. frbd_index = (frbd_index + 1) % rxq->count;
  175. data->ia.tr_hia[BTINTEL_PCIE_RXQ_NUM] = frbd_index;
  176. ipc_print_ia_ring(data->hdev, &data->ia, BTINTEL_PCIE_RXQ_NUM);
  177. /* Set the doorbell to notify the device */
  178. btintel_pcie_set_rx_db(data, frbd_index);
  179. return 0;
  180. }
  181. static int btintel_pcie_start_rx(struct btintel_pcie_data *data)
  182. {
  183. int i, ret;
  184. struct rxq *rxq = &data->rxq;
  185. /* Post (BTINTEL_PCIE_RX_DESCS_COUNT - 3) buffers to overcome the
  186. * hardware issues leading to race condition at the firmware.
  187. */
  188. for (i = 0; i < rxq->count - 3; i++) {
  189. ret = btintel_pcie_submit_rx(data);
  190. if (ret)
  191. return ret;
  192. }
  193. return 0;
  194. }
  195. static void btintel_pcie_reset_ia(struct btintel_pcie_data *data)
  196. {
  197. memset(data->ia.tr_hia, 0, sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES);
  198. memset(data->ia.tr_tia, 0, sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES);
  199. memset(data->ia.cr_hia, 0, sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES);
  200. memset(data->ia.cr_tia, 0, sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES);
  201. }
  202. static void btintel_pcie_reset_bt(struct btintel_pcie_data *data)
  203. {
  204. btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG,
  205. BTINTEL_PCIE_CSR_FUNC_CTRL_SW_RESET);
  206. }
  207. /* This function enables BT function by setting BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_INIT bit in
  208. * BTINTEL_PCIE_CSR_FUNC_CTRL_REG register and wait for MSI-X with
  209. * BTINTEL_PCIE_MSIX_HW_INT_CAUSES_GP0.
  210. * Then the host reads firmware version from BTINTEL_CSR_F2D_MBX and the boot stage
  211. * from BTINTEL_PCIE_CSR_BOOT_STAGE_REG.
  212. */
  213. static int btintel_pcie_enable_bt(struct btintel_pcie_data *data)
  214. {
  215. int err;
  216. data->gp0_received = false;
  217. /* Update the DMA address of CI struct to CSR */
  218. btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_CI_ADDR_LSB_REG,
  219. data->ci_p_addr & 0xffffffff);
  220. btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_CI_ADDR_MSB_REG,
  221. (u64)data->ci_p_addr >> 32);
  222. /* Reset the cached value of boot stage. it is updated by the MSI-X
  223. * gp0 interrupt handler.
  224. */
  225. data->boot_stage_cache = 0x0;
  226. /* Set MAC_INIT bit to start primary bootloader */
  227. btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG);
  228. btintel_pcie_set_reg_bits(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG,
  229. BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_INIT);
  230. /* Wait until MAC_ACCESS is granted */
  231. err = btintel_pcie_poll_bit(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG,
  232. BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_ACCESS_STS,
  233. BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_ACCESS_STS,
  234. BTINTEL_DEFAULT_MAC_ACCESS_TIMEOUT_US);
  235. if (err < 0)
  236. return -ENODEV;
  237. /* MAC is ready. Enable BT FUNC */
  238. btintel_pcie_set_reg_bits(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG,
  239. BTINTEL_PCIE_CSR_FUNC_CTRL_FUNC_ENA |
  240. BTINTEL_PCIE_CSR_FUNC_CTRL_FUNC_INIT);
  241. btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG);
  242. /* wait for interrupt from the device after booting up to primary
  243. * bootloader.
  244. */
  245. data->alive_intr_ctxt = BTINTEL_PCIE_ROM;
  246. err = wait_event_timeout(data->gp0_wait_q, data->gp0_received,
  247. msecs_to_jiffies(BTINTEL_DEFAULT_INTR_TIMEOUT_MS));
  248. if (!err)
  249. return -ETIME;
  250. /* Check cached boot stage is BTINTEL_PCIE_CSR_BOOT_STAGE_ROM(BIT(0)) */
  251. if (~data->boot_stage_cache & BTINTEL_PCIE_CSR_BOOT_STAGE_ROM)
  252. return -ENODEV;
  253. return 0;
  254. }
  255. /* BIT(0) - ROM, BIT(1) - IML and BIT(3) - OP
  256. * Sometimes during firmware image switching from ROM to IML or IML to OP image,
  257. * the previous image bit is not cleared by firmware when alive interrupt is
  258. * received. Driver needs to take care of these sticky bits when deciding the
  259. * current image running on controller.
  260. * Ex: 0x10 and 0x11 - both represents that controller is running IML
  261. */
  262. static inline bool btintel_pcie_in_rom(struct btintel_pcie_data *data)
  263. {
  264. return data->boot_stage_cache & BTINTEL_PCIE_CSR_BOOT_STAGE_ROM &&
  265. !(data->boot_stage_cache & BTINTEL_PCIE_CSR_BOOT_STAGE_IML) &&
  266. !(data->boot_stage_cache & BTINTEL_PCIE_CSR_BOOT_STAGE_OPFW);
  267. }
  268. static inline bool btintel_pcie_in_op(struct btintel_pcie_data *data)
  269. {
  270. return data->boot_stage_cache & BTINTEL_PCIE_CSR_BOOT_STAGE_OPFW;
  271. }
  272. static inline bool btintel_pcie_in_iml(struct btintel_pcie_data *data)
  273. {
  274. return data->boot_stage_cache & BTINTEL_PCIE_CSR_BOOT_STAGE_IML &&
  275. !(data->boot_stage_cache & BTINTEL_PCIE_CSR_BOOT_STAGE_OPFW);
  276. }
  277. static inline bool btintel_pcie_in_d3(struct btintel_pcie_data *data)
  278. {
  279. return data->boot_stage_cache & BTINTEL_PCIE_CSR_BOOT_STAGE_D3_STATE_READY;
  280. }
  281. static inline bool btintel_pcie_in_d0(struct btintel_pcie_data *data)
  282. {
  283. return !(data->boot_stage_cache & BTINTEL_PCIE_CSR_BOOT_STAGE_D3_STATE_READY);
  284. }
  285. static void btintel_pcie_wr_sleep_cntrl(struct btintel_pcie_data *data,
  286. u32 dxstate)
  287. {
  288. bt_dev_dbg(data->hdev, "writing sleep_ctl_reg: 0x%8.8x", dxstate);
  289. btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_IPC_SLEEP_CTL_REG, dxstate);
  290. }
  291. static inline char *btintel_pcie_alivectxt_state2str(u32 alive_intr_ctxt)
  292. {
  293. switch (alive_intr_ctxt) {
  294. case BTINTEL_PCIE_ROM:
  295. return "rom";
  296. case BTINTEL_PCIE_FW_DL:
  297. return "fw_dl";
  298. case BTINTEL_PCIE_D0:
  299. return "d0";
  300. case BTINTEL_PCIE_D3:
  301. return "d3";
  302. case BTINTEL_PCIE_HCI_RESET:
  303. return "hci_reset";
  304. case BTINTEL_PCIE_INTEL_HCI_RESET1:
  305. return "intel_reset1";
  306. case BTINTEL_PCIE_INTEL_HCI_RESET2:
  307. return "intel_reset2";
  308. default:
  309. return "unknown";
  310. }
  311. return "null";
  312. }
  313. /* This function handles the MSI-X interrupt for gp0 cause (bit 0 in
  314. * BTINTEL_PCIE_CSR_MSIX_HW_INT_CAUSES) which is sent for boot stage and image response.
  315. */
  316. static void btintel_pcie_msix_gp0_handler(struct btintel_pcie_data *data)
  317. {
  318. bool submit_rx, signal_waitq;
  319. u32 reg, old_ctxt;
  320. /* This interrupt is for three different causes and it is not easy to
  321. * know what causes the interrupt. So, it compares each register value
  322. * with cached value and update it before it wake up the queue.
  323. */
  324. reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_BOOT_STAGE_REG);
  325. if (reg != data->boot_stage_cache)
  326. data->boot_stage_cache = reg;
  327. bt_dev_dbg(data->hdev, "Alive context: %s old_boot_stage: 0x%8.8x new_boot_stage: 0x%8.8x",
  328. btintel_pcie_alivectxt_state2str(data->alive_intr_ctxt),
  329. data->boot_stage_cache, reg);
  330. reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_IMG_RESPONSE_REG);
  331. if (reg != data->img_resp_cache)
  332. data->img_resp_cache = reg;
  333. data->gp0_received = true;
  334. old_ctxt = data->alive_intr_ctxt;
  335. submit_rx = false;
  336. signal_waitq = false;
  337. switch (data->alive_intr_ctxt) {
  338. case BTINTEL_PCIE_ROM:
  339. data->alive_intr_ctxt = BTINTEL_PCIE_FW_DL;
  340. signal_waitq = true;
  341. break;
  342. case BTINTEL_PCIE_FW_DL:
  343. /* Error case is already handled. Ideally control shall not
  344. * reach here
  345. */
  346. break;
  347. case BTINTEL_PCIE_INTEL_HCI_RESET1:
  348. if (btintel_pcie_in_op(data)) {
  349. submit_rx = true;
  350. break;
  351. }
  352. if (btintel_pcie_in_iml(data)) {
  353. submit_rx = true;
  354. data->alive_intr_ctxt = BTINTEL_PCIE_FW_DL;
  355. break;
  356. }
  357. break;
  358. case BTINTEL_PCIE_INTEL_HCI_RESET2:
  359. if (btintel_test_and_clear_flag(data->hdev, INTEL_WAIT_FOR_D0)) {
  360. btintel_wake_up_flag(data->hdev, INTEL_WAIT_FOR_D0);
  361. data->alive_intr_ctxt = BTINTEL_PCIE_D0;
  362. }
  363. break;
  364. case BTINTEL_PCIE_D0:
  365. if (btintel_pcie_in_d3(data)) {
  366. data->alive_intr_ctxt = BTINTEL_PCIE_D3;
  367. signal_waitq = true;
  368. break;
  369. }
  370. break;
  371. case BTINTEL_PCIE_D3:
  372. if (btintel_pcie_in_d0(data)) {
  373. data->alive_intr_ctxt = BTINTEL_PCIE_D0;
  374. submit_rx = true;
  375. signal_waitq = true;
  376. break;
  377. }
  378. break;
  379. case BTINTEL_PCIE_HCI_RESET:
  380. data->alive_intr_ctxt = BTINTEL_PCIE_D0;
  381. submit_rx = true;
  382. signal_waitq = true;
  383. break;
  384. default:
  385. bt_dev_err(data->hdev, "Unknown state: 0x%2.2x",
  386. data->alive_intr_ctxt);
  387. break;
  388. }
  389. if (submit_rx) {
  390. btintel_pcie_reset_ia(data);
  391. btintel_pcie_start_rx(data);
  392. }
  393. if (signal_waitq) {
  394. bt_dev_dbg(data->hdev, "wake up gp0 wait_q");
  395. wake_up(&data->gp0_wait_q);
  396. }
  397. if (old_ctxt != data->alive_intr_ctxt)
  398. bt_dev_dbg(data->hdev, "alive context changed: %s -> %s",
  399. btintel_pcie_alivectxt_state2str(old_ctxt),
  400. btintel_pcie_alivectxt_state2str(data->alive_intr_ctxt));
  401. }
  402. /* This function handles the MSX-X interrupt for rx queue 0 which is for TX
  403. */
  404. static void btintel_pcie_msix_tx_handle(struct btintel_pcie_data *data)
  405. {
  406. u16 cr_tia, cr_hia;
  407. struct txq *txq;
  408. struct urbd0 *urbd0;
  409. cr_tia = data->ia.cr_tia[BTINTEL_PCIE_TXQ_NUM];
  410. cr_hia = data->ia.cr_hia[BTINTEL_PCIE_TXQ_NUM];
  411. if (cr_tia == cr_hia)
  412. return;
  413. txq = &data->txq;
  414. while (cr_tia != cr_hia) {
  415. data->tx_wait_done = true;
  416. wake_up(&data->tx_wait_q);
  417. urbd0 = &txq->urbd0s[cr_tia];
  418. if (urbd0->tfd_index > txq->count)
  419. return;
  420. cr_tia = (cr_tia + 1) % txq->count;
  421. data->ia.cr_tia[BTINTEL_PCIE_TXQ_NUM] = cr_tia;
  422. ipc_print_ia_ring(data->hdev, &data->ia, BTINTEL_PCIE_TXQ_NUM);
  423. }
  424. }
  425. static int btintel_pcie_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
  426. {
  427. struct hci_event_hdr *hdr = (void *)skb->data;
  428. const char diagnostics_hdr[] = { 0x87, 0x80, 0x03 };
  429. struct btintel_pcie_data *data = hci_get_drvdata(hdev);
  430. if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
  431. hdr->plen > 0) {
  432. const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
  433. unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
  434. if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
  435. switch (skb->data[2]) {
  436. case 0x02:
  437. /* When switching to the operational firmware
  438. * the device sends a vendor specific event
  439. * indicating that the bootup completed.
  440. */
  441. btintel_bootup(hdev, ptr, len);
  442. /* If bootup event is from operational image,
  443. * driver needs to write sleep control register to
  444. * move into D0 state
  445. */
  446. if (btintel_pcie_in_op(data)) {
  447. btintel_pcie_wr_sleep_cntrl(data, BTINTEL_PCIE_STATE_D0);
  448. data->alive_intr_ctxt = BTINTEL_PCIE_INTEL_HCI_RESET2;
  449. kfree_skb(skb);
  450. return 0;
  451. }
  452. if (btintel_pcie_in_iml(data)) {
  453. /* In case of IML, there is no concept
  454. * of D0 transition. Just mimic as if
  455. * IML moved to D0 by clearing INTEL_WAIT_FOR_D0
  456. * bit and waking up the task waiting on
  457. * INTEL_WAIT_FOR_D0. This is required
  458. * as intel_boot() is common function for
  459. * both IML and OP image loading.
  460. */
  461. if (btintel_test_and_clear_flag(data->hdev,
  462. INTEL_WAIT_FOR_D0))
  463. btintel_wake_up_flag(data->hdev,
  464. INTEL_WAIT_FOR_D0);
  465. }
  466. kfree_skb(skb);
  467. return 0;
  468. case 0x06:
  469. /* When the firmware loading completes the
  470. * device sends out a vendor specific event
  471. * indicating the result of the firmware
  472. * loading.
  473. */
  474. btintel_secure_send_result(hdev, ptr, len);
  475. kfree_skb(skb);
  476. return 0;
  477. }
  478. }
  479. /* Handle all diagnostics events separately. May still call
  480. * hci_recv_frame.
  481. */
  482. if (len >= sizeof(diagnostics_hdr) &&
  483. memcmp(&skb->data[2], diagnostics_hdr,
  484. sizeof(diagnostics_hdr)) == 0) {
  485. return btintel_diagnostics(hdev, skb);
  486. }
  487. /* This is a debug event that comes from IML and OP image when it
  488. * starts execution. There is no need pass this event to stack.
  489. */
  490. if (skb->data[2] == 0x97) {
  491. hci_recv_diag(hdev, skb);
  492. return 0;
  493. }
  494. }
  495. return hci_recv_frame(hdev, skb);
  496. }
  497. /* Process the received rx data
  498. * It check the frame header to identify the data type and create skb
  499. * and calling HCI API
  500. */
  501. static int btintel_pcie_recv_frame(struct btintel_pcie_data *data,
  502. struct sk_buff *skb)
  503. {
  504. int ret;
  505. u8 pkt_type;
  506. u16 plen;
  507. u32 pcie_pkt_type;
  508. void *pdata;
  509. struct hci_dev *hdev = data->hdev;
  510. spin_lock(&data->hci_rx_lock);
  511. /* The first 4 bytes indicates the Intel PCIe specific packet type */
  512. pdata = skb_pull_data(skb, BTINTEL_PCIE_HCI_TYPE_LEN);
  513. if (!pdata) {
  514. bt_dev_err(hdev, "Corrupted packet received");
  515. ret = -EILSEQ;
  516. goto exit_error;
  517. }
  518. pcie_pkt_type = get_unaligned_le32(pdata);
  519. switch (pcie_pkt_type) {
  520. case BTINTEL_PCIE_HCI_ACL_PKT:
  521. if (skb->len >= HCI_ACL_HDR_SIZE) {
  522. plen = HCI_ACL_HDR_SIZE + __le16_to_cpu(hci_acl_hdr(skb)->dlen);
  523. pkt_type = HCI_ACLDATA_PKT;
  524. } else {
  525. bt_dev_err(hdev, "ACL packet is too short");
  526. ret = -EILSEQ;
  527. goto exit_error;
  528. }
  529. break;
  530. case BTINTEL_PCIE_HCI_SCO_PKT:
  531. if (skb->len >= HCI_SCO_HDR_SIZE) {
  532. plen = HCI_SCO_HDR_SIZE + hci_sco_hdr(skb)->dlen;
  533. pkt_type = HCI_SCODATA_PKT;
  534. } else {
  535. bt_dev_err(hdev, "SCO packet is too short");
  536. ret = -EILSEQ;
  537. goto exit_error;
  538. }
  539. break;
  540. case BTINTEL_PCIE_HCI_EVT_PKT:
  541. if (skb->len >= HCI_EVENT_HDR_SIZE) {
  542. plen = HCI_EVENT_HDR_SIZE + hci_event_hdr(skb)->plen;
  543. pkt_type = HCI_EVENT_PKT;
  544. } else {
  545. bt_dev_err(hdev, "Event packet is too short");
  546. ret = -EILSEQ;
  547. goto exit_error;
  548. }
  549. break;
  550. case BTINTEL_PCIE_HCI_ISO_PKT:
  551. if (skb->len >= HCI_ISO_HDR_SIZE) {
  552. plen = HCI_ISO_HDR_SIZE + __le16_to_cpu(hci_iso_hdr(skb)->dlen);
  553. pkt_type = HCI_ISODATA_PKT;
  554. } else {
  555. bt_dev_err(hdev, "ISO packet is too short");
  556. ret = -EILSEQ;
  557. goto exit_error;
  558. }
  559. break;
  560. default:
  561. bt_dev_err(hdev, "Invalid packet type received: 0x%4.4x",
  562. pcie_pkt_type);
  563. ret = -EINVAL;
  564. goto exit_error;
  565. }
  566. if (skb->len < plen) {
  567. bt_dev_err(hdev, "Received corrupted packet. type: 0x%2.2x",
  568. pkt_type);
  569. ret = -EILSEQ;
  570. goto exit_error;
  571. }
  572. bt_dev_dbg(hdev, "pkt_type: 0x%2.2x len: %u", pkt_type, plen);
  573. hci_skb_pkt_type(skb) = pkt_type;
  574. hdev->stat.byte_rx += plen;
  575. skb_trim(skb, plen);
  576. if (pcie_pkt_type == BTINTEL_PCIE_HCI_EVT_PKT)
  577. ret = btintel_pcie_recv_event(hdev, skb);
  578. else
  579. ret = hci_recv_frame(hdev, skb);
  580. skb = NULL; /* skb is freed in the callee */
  581. exit_error:
  582. if (skb)
  583. kfree_skb(skb);
  584. if (ret)
  585. hdev->stat.err_rx++;
  586. spin_unlock(&data->hci_rx_lock);
  587. return ret;
  588. }
  589. static void btintel_pcie_rx_work(struct work_struct *work)
  590. {
  591. struct btintel_pcie_data *data = container_of(work,
  592. struct btintel_pcie_data, rx_work);
  593. struct sk_buff *skb;
  594. /* Process the sk_buf in queue and send to the HCI layer */
  595. while ((skb = skb_dequeue(&data->rx_skb_q))) {
  596. btintel_pcie_recv_frame(data, skb);
  597. }
  598. }
  599. /* create sk_buff with data and save it to queue and start RX work */
  600. static int btintel_pcie_submit_rx_work(struct btintel_pcie_data *data, u8 status,
  601. void *buf)
  602. {
  603. int ret, len;
  604. struct rfh_hdr *rfh_hdr;
  605. struct sk_buff *skb;
  606. rfh_hdr = buf;
  607. len = rfh_hdr->packet_len;
  608. if (len <= 0) {
  609. ret = -EINVAL;
  610. goto resubmit;
  611. }
  612. /* Remove RFH header */
  613. buf += sizeof(*rfh_hdr);
  614. skb = alloc_skb(len, GFP_ATOMIC);
  615. if (!skb) {
  616. ret = -ENOMEM;
  617. goto resubmit;
  618. }
  619. skb_put_data(skb, buf, len);
  620. skb_queue_tail(&data->rx_skb_q, skb);
  621. queue_work(data->workqueue, &data->rx_work);
  622. resubmit:
  623. ret = btintel_pcie_submit_rx(data);
  624. return ret;
  625. }
  626. /* Handles the MSI-X interrupt for rx queue 1 which is for RX */
  627. static void btintel_pcie_msix_rx_handle(struct btintel_pcie_data *data)
  628. {
  629. u16 cr_hia, cr_tia;
  630. struct rxq *rxq;
  631. struct urbd1 *urbd1;
  632. struct data_buf *buf;
  633. int ret;
  634. struct hci_dev *hdev = data->hdev;
  635. cr_hia = data->ia.cr_hia[BTINTEL_PCIE_RXQ_NUM];
  636. cr_tia = data->ia.cr_tia[BTINTEL_PCIE_RXQ_NUM];
  637. bt_dev_dbg(hdev, "RXQ: cr_hia: %u cr_tia: %u", cr_hia, cr_tia);
  638. /* Check CR_TIA and CR_HIA for change */
  639. if (cr_tia == cr_hia)
  640. return;
  641. rxq = &data->rxq;
  642. /* The firmware sends multiple CD in a single MSI-X and it needs to
  643. * process all received CDs in this interrupt.
  644. */
  645. while (cr_tia != cr_hia) {
  646. urbd1 = &rxq->urbd1s[cr_tia];
  647. ipc_print_urbd1(data->hdev, urbd1, cr_tia);
  648. buf = &rxq->bufs[urbd1->frbd_tag];
  649. if (!buf) {
  650. bt_dev_err(hdev, "RXQ: failed to get the DMA buffer for %d",
  651. urbd1->frbd_tag);
  652. return;
  653. }
  654. ret = btintel_pcie_submit_rx_work(data, urbd1->status,
  655. buf->data);
  656. if (ret) {
  657. bt_dev_err(hdev, "RXQ: failed to submit rx request");
  658. return;
  659. }
  660. cr_tia = (cr_tia + 1) % rxq->count;
  661. data->ia.cr_tia[BTINTEL_PCIE_RXQ_NUM] = cr_tia;
  662. ipc_print_ia_ring(data->hdev, &data->ia, BTINTEL_PCIE_RXQ_NUM);
  663. }
  664. }
  665. static irqreturn_t btintel_pcie_msix_isr(int irq, void *data)
  666. {
  667. return IRQ_WAKE_THREAD;
  668. }
  669. static inline bool btintel_pcie_is_rxq_empty(struct btintel_pcie_data *data)
  670. {
  671. return data->ia.cr_hia[BTINTEL_PCIE_RXQ_NUM] == data->ia.cr_tia[BTINTEL_PCIE_RXQ_NUM];
  672. }
  673. static inline bool btintel_pcie_is_txackq_empty(struct btintel_pcie_data *data)
  674. {
  675. return data->ia.cr_tia[BTINTEL_PCIE_TXQ_NUM] == data->ia.cr_hia[BTINTEL_PCIE_TXQ_NUM];
  676. }
  677. static irqreturn_t btintel_pcie_irq_msix_handler(int irq, void *dev_id)
  678. {
  679. struct msix_entry *entry = dev_id;
  680. struct btintel_pcie_data *data = btintel_pcie_get_data(entry);
  681. u32 intr_fh, intr_hw;
  682. spin_lock(&data->irq_lock);
  683. intr_fh = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_MSIX_FH_INT_CAUSES);
  684. intr_hw = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_MSIX_HW_INT_CAUSES);
  685. /* Clear causes registers to avoid being handling the same cause */
  686. btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_MSIX_FH_INT_CAUSES, intr_fh);
  687. btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_MSIX_HW_INT_CAUSES, intr_hw);
  688. spin_unlock(&data->irq_lock);
  689. if (unlikely(!(intr_fh | intr_hw))) {
  690. /* Ignore interrupt, inta == 0 */
  691. return IRQ_NONE;
  692. }
  693. /* This interrupt is triggered by the firmware after updating
  694. * boot_stage register and image_response register
  695. */
  696. if (intr_hw & BTINTEL_PCIE_MSIX_HW_INT_CAUSES_GP0)
  697. btintel_pcie_msix_gp0_handler(data);
  698. /* For TX */
  699. if (intr_fh & BTINTEL_PCIE_MSIX_FH_INT_CAUSES_0) {
  700. btintel_pcie_msix_tx_handle(data);
  701. if (!btintel_pcie_is_rxq_empty(data))
  702. btintel_pcie_msix_rx_handle(data);
  703. }
  704. /* For RX */
  705. if (intr_fh & BTINTEL_PCIE_MSIX_FH_INT_CAUSES_1) {
  706. btintel_pcie_msix_rx_handle(data);
  707. if (!btintel_pcie_is_txackq_empty(data))
  708. btintel_pcie_msix_tx_handle(data);
  709. }
  710. /*
  711. * Before sending the interrupt the HW disables it to prevent a nested
  712. * interrupt. This is done by writing 1 to the corresponding bit in
  713. * the mask register. After handling the interrupt, it should be
  714. * re-enabled by clearing this bit. This register is defined as write 1
  715. * clear (W1C) register, meaning that it's cleared by writing 1
  716. * to the bit.
  717. */
  718. btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_MSIX_AUTOMASK_ST,
  719. BIT(entry->entry));
  720. return IRQ_HANDLED;
  721. }
  722. /* This function requests the irq for MSI-X and registers the handlers per irq.
  723. * Currently, it requests only 1 irq for all interrupt causes.
  724. */
  725. static int btintel_pcie_setup_irq(struct btintel_pcie_data *data)
  726. {
  727. int err;
  728. int num_irqs, i;
  729. for (i = 0; i < BTINTEL_PCIE_MSIX_VEC_MAX; i++)
  730. data->msix_entries[i].entry = i;
  731. num_irqs = pci_alloc_irq_vectors(data->pdev, BTINTEL_PCIE_MSIX_VEC_MIN,
  732. BTINTEL_PCIE_MSIX_VEC_MAX, PCI_IRQ_MSIX);
  733. if (num_irqs < 0)
  734. return num_irqs;
  735. data->alloc_vecs = num_irqs;
  736. data->msix_enabled = 1;
  737. data->def_irq = 0;
  738. /* setup irq handler */
  739. for (i = 0; i < data->alloc_vecs; i++) {
  740. struct msix_entry *msix_entry;
  741. msix_entry = &data->msix_entries[i];
  742. msix_entry->vector = pci_irq_vector(data->pdev, i);
  743. err = devm_request_threaded_irq(&data->pdev->dev,
  744. msix_entry->vector,
  745. btintel_pcie_msix_isr,
  746. btintel_pcie_irq_msix_handler,
  747. IRQF_SHARED,
  748. KBUILD_MODNAME,
  749. msix_entry);
  750. if (err) {
  751. pci_free_irq_vectors(data->pdev);
  752. data->alloc_vecs = 0;
  753. return err;
  754. }
  755. }
  756. return 0;
  757. }
  758. struct btintel_pcie_causes_list {
  759. u32 cause;
  760. u32 mask_reg;
  761. u8 cause_num;
  762. };
  763. static struct btintel_pcie_causes_list causes_list[] = {
  764. { BTINTEL_PCIE_MSIX_FH_INT_CAUSES_0, BTINTEL_PCIE_CSR_MSIX_FH_INT_MASK, 0x00 },
  765. { BTINTEL_PCIE_MSIX_FH_INT_CAUSES_1, BTINTEL_PCIE_CSR_MSIX_FH_INT_MASK, 0x01 },
  766. { BTINTEL_PCIE_MSIX_HW_INT_CAUSES_GP0, BTINTEL_PCIE_CSR_MSIX_HW_INT_MASK, 0x20 },
  767. };
  768. /* This function configures the interrupt masks for both HW_INT_CAUSES and
  769. * FH_INT_CAUSES which are meaningful to us.
  770. *
  771. * After resetting BT function via PCIE FLR or FUNC_CTRL reset, the driver
  772. * need to call this function again to configure since the masks
  773. * are reset to 0xFFFFFFFF after reset.
  774. */
  775. static void btintel_pcie_config_msix(struct btintel_pcie_data *data)
  776. {
  777. int i;
  778. int val = data->def_irq | BTINTEL_PCIE_MSIX_NON_AUTO_CLEAR_CAUSE;
  779. /* Set Non Auto Clear Cause */
  780. for (i = 0; i < ARRAY_SIZE(causes_list); i++) {
  781. btintel_pcie_wr_reg8(data,
  782. BTINTEL_PCIE_CSR_MSIX_IVAR(causes_list[i].cause_num),
  783. val);
  784. btintel_pcie_clr_reg_bits(data,
  785. causes_list[i].mask_reg,
  786. causes_list[i].cause);
  787. }
  788. /* Save the initial interrupt mask */
  789. data->fh_init_mask = ~btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_MSIX_FH_INT_MASK);
  790. data->hw_init_mask = ~btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_MSIX_HW_INT_MASK);
  791. }
  792. static int btintel_pcie_config_pcie(struct pci_dev *pdev,
  793. struct btintel_pcie_data *data)
  794. {
  795. int err;
  796. err = pcim_enable_device(pdev);
  797. if (err)
  798. return err;
  799. pci_set_master(pdev);
  800. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  801. if (err) {
  802. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  803. if (err)
  804. return err;
  805. }
  806. err = pcim_iomap_regions(pdev, BIT(0), KBUILD_MODNAME);
  807. if (err)
  808. return err;
  809. data->base_addr = pcim_iomap_table(pdev)[0];
  810. if (!data->base_addr)
  811. return -ENODEV;
  812. err = btintel_pcie_setup_irq(data);
  813. if (err)
  814. return err;
  815. /* Configure MSI-X with causes list */
  816. btintel_pcie_config_msix(data);
  817. return 0;
  818. }
  819. static void btintel_pcie_init_ci(struct btintel_pcie_data *data,
  820. struct ctx_info *ci)
  821. {
  822. ci->version = 0x1;
  823. ci->size = sizeof(*ci);
  824. ci->config = 0x0000;
  825. ci->addr_cr_hia = data->ia.cr_hia_p_addr;
  826. ci->addr_tr_tia = data->ia.tr_tia_p_addr;
  827. ci->addr_cr_tia = data->ia.cr_tia_p_addr;
  828. ci->addr_tr_hia = data->ia.tr_hia_p_addr;
  829. ci->num_cr_ia = BTINTEL_PCIE_NUM_QUEUES;
  830. ci->num_tr_ia = BTINTEL_PCIE_NUM_QUEUES;
  831. ci->addr_urbdq0 = data->txq.urbd0s_p_addr;
  832. ci->addr_tfdq = data->txq.tfds_p_addr;
  833. ci->num_tfdq = data->txq.count;
  834. ci->num_urbdq0 = data->txq.count;
  835. ci->tfdq_db_vec = BTINTEL_PCIE_TXQ_NUM;
  836. ci->urbdq0_db_vec = BTINTEL_PCIE_TXQ_NUM;
  837. ci->rbd_size = BTINTEL_PCIE_RBD_SIZE_4K;
  838. ci->addr_frbdq = data->rxq.frbds_p_addr;
  839. ci->num_frbdq = data->rxq.count;
  840. ci->frbdq_db_vec = BTINTEL_PCIE_RXQ_NUM;
  841. ci->addr_urbdq1 = data->rxq.urbd1s_p_addr;
  842. ci->num_urbdq1 = data->rxq.count;
  843. ci->urbdq_db_vec = BTINTEL_PCIE_RXQ_NUM;
  844. }
  845. static void btintel_pcie_free_txq_bufs(struct btintel_pcie_data *data,
  846. struct txq *txq)
  847. {
  848. /* Free data buffers first */
  849. dma_free_coherent(&data->pdev->dev, txq->count * BTINTEL_PCIE_BUFFER_SIZE,
  850. txq->buf_v_addr, txq->buf_p_addr);
  851. kfree(txq->bufs);
  852. }
  853. static int btintel_pcie_setup_txq_bufs(struct btintel_pcie_data *data,
  854. struct txq *txq)
  855. {
  856. int i;
  857. struct data_buf *buf;
  858. /* Allocate the same number of buffers as the descriptor */
  859. txq->bufs = kmalloc_array(txq->count, sizeof(*buf), GFP_KERNEL);
  860. if (!txq->bufs)
  861. return -ENOMEM;
  862. /* Allocate full chunk of data buffer for DMA first and do indexing and
  863. * initialization next, so it can be freed easily
  864. */
  865. txq->buf_v_addr = dma_alloc_coherent(&data->pdev->dev,
  866. txq->count * BTINTEL_PCIE_BUFFER_SIZE,
  867. &txq->buf_p_addr,
  868. GFP_KERNEL | __GFP_NOWARN);
  869. if (!txq->buf_v_addr) {
  870. kfree(txq->bufs);
  871. return -ENOMEM;
  872. }
  873. /* Setup the allocated DMA buffer to bufs. Each data_buf should
  874. * have virtual address and physical address
  875. */
  876. for (i = 0; i < txq->count; i++) {
  877. buf = &txq->bufs[i];
  878. buf->data_p_addr = txq->buf_p_addr + (i * BTINTEL_PCIE_BUFFER_SIZE);
  879. buf->data = txq->buf_v_addr + (i * BTINTEL_PCIE_BUFFER_SIZE);
  880. }
  881. return 0;
  882. }
  883. static void btintel_pcie_free_rxq_bufs(struct btintel_pcie_data *data,
  884. struct rxq *rxq)
  885. {
  886. /* Free data buffers first */
  887. dma_free_coherent(&data->pdev->dev, rxq->count * BTINTEL_PCIE_BUFFER_SIZE,
  888. rxq->buf_v_addr, rxq->buf_p_addr);
  889. kfree(rxq->bufs);
  890. }
  891. static int btintel_pcie_setup_rxq_bufs(struct btintel_pcie_data *data,
  892. struct rxq *rxq)
  893. {
  894. int i;
  895. struct data_buf *buf;
  896. /* Allocate the same number of buffers as the descriptor */
  897. rxq->bufs = kmalloc_array(rxq->count, sizeof(*buf), GFP_KERNEL);
  898. if (!rxq->bufs)
  899. return -ENOMEM;
  900. /* Allocate full chunk of data buffer for DMA first and do indexing and
  901. * initialization next, so it can be freed easily
  902. */
  903. rxq->buf_v_addr = dma_alloc_coherent(&data->pdev->dev,
  904. rxq->count * BTINTEL_PCIE_BUFFER_SIZE,
  905. &rxq->buf_p_addr,
  906. GFP_KERNEL | __GFP_NOWARN);
  907. if (!rxq->buf_v_addr) {
  908. kfree(rxq->bufs);
  909. return -ENOMEM;
  910. }
  911. /* Setup the allocated DMA buffer to bufs. Each data_buf should
  912. * have virtual address and physical address
  913. */
  914. for (i = 0; i < rxq->count; i++) {
  915. buf = &rxq->bufs[i];
  916. buf->data_p_addr = rxq->buf_p_addr + (i * BTINTEL_PCIE_BUFFER_SIZE);
  917. buf->data = rxq->buf_v_addr + (i * BTINTEL_PCIE_BUFFER_SIZE);
  918. }
  919. return 0;
  920. }
  921. static void btintel_pcie_setup_ia(struct btintel_pcie_data *data,
  922. dma_addr_t p_addr, void *v_addr,
  923. struct ia *ia)
  924. {
  925. /* TR Head Index Array */
  926. ia->tr_hia_p_addr = p_addr;
  927. ia->tr_hia = v_addr;
  928. /* TR Tail Index Array */
  929. ia->tr_tia_p_addr = p_addr + sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES;
  930. ia->tr_tia = v_addr + sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES;
  931. /* CR Head index Array */
  932. ia->cr_hia_p_addr = p_addr + (sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES * 2);
  933. ia->cr_hia = v_addr + (sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES * 2);
  934. /* CR Tail Index Array */
  935. ia->cr_tia_p_addr = p_addr + (sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES * 3);
  936. ia->cr_tia = v_addr + (sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES * 3);
  937. }
  938. static void btintel_pcie_free(struct btintel_pcie_data *data)
  939. {
  940. btintel_pcie_free_rxq_bufs(data, &data->rxq);
  941. btintel_pcie_free_txq_bufs(data, &data->txq);
  942. dma_pool_free(data->dma_pool, data->dma_v_addr, data->dma_p_addr);
  943. dma_pool_destroy(data->dma_pool);
  944. }
  945. /* Allocate tx and rx queues, any related data structures and buffers.
  946. */
  947. static int btintel_pcie_alloc(struct btintel_pcie_data *data)
  948. {
  949. int err = 0;
  950. size_t total;
  951. dma_addr_t p_addr;
  952. void *v_addr;
  953. /* Allocate the chunk of DMA memory for descriptors, index array, and
  954. * context information, instead of allocating individually.
  955. * The DMA memory for data buffer is allocated while setting up the
  956. * each queue.
  957. *
  958. * Total size is sum of the following
  959. * + size of TFD * Number of descriptors in queue
  960. * + size of URBD0 * Number of descriptors in queue
  961. * + size of FRBD * Number of descriptors in queue
  962. * + size of URBD1 * Number of descriptors in queue
  963. * + size of index * Number of queues(2) * type of index array(4)
  964. * + size of context information
  965. */
  966. total = (sizeof(struct tfd) + sizeof(struct urbd0)) * BTINTEL_PCIE_TX_DESCS_COUNT;
  967. total += (sizeof(struct frbd) + sizeof(struct urbd1)) * BTINTEL_PCIE_RX_DESCS_COUNT;
  968. /* Add the sum of size of index array and size of ci struct */
  969. total += (sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES * 4) + sizeof(struct ctx_info);
  970. /* Allocate DMA Pool */
  971. data->dma_pool = dma_pool_create(KBUILD_MODNAME, &data->pdev->dev,
  972. total, BTINTEL_PCIE_DMA_POOL_ALIGNMENT, 0);
  973. if (!data->dma_pool) {
  974. err = -ENOMEM;
  975. goto exit_error;
  976. }
  977. v_addr = dma_pool_zalloc(data->dma_pool, GFP_KERNEL | __GFP_NOWARN,
  978. &p_addr);
  979. if (!v_addr) {
  980. dma_pool_destroy(data->dma_pool);
  981. err = -ENOMEM;
  982. goto exit_error;
  983. }
  984. data->dma_p_addr = p_addr;
  985. data->dma_v_addr = v_addr;
  986. /* Setup descriptor count */
  987. data->txq.count = BTINTEL_PCIE_TX_DESCS_COUNT;
  988. data->rxq.count = BTINTEL_PCIE_RX_DESCS_COUNT;
  989. /* Setup tfds */
  990. data->txq.tfds_p_addr = p_addr;
  991. data->txq.tfds = v_addr;
  992. p_addr += (sizeof(struct tfd) * BTINTEL_PCIE_TX_DESCS_COUNT);
  993. v_addr += (sizeof(struct tfd) * BTINTEL_PCIE_TX_DESCS_COUNT);
  994. /* Setup urbd0 */
  995. data->txq.urbd0s_p_addr = p_addr;
  996. data->txq.urbd0s = v_addr;
  997. p_addr += (sizeof(struct urbd0) * BTINTEL_PCIE_TX_DESCS_COUNT);
  998. v_addr += (sizeof(struct urbd0) * BTINTEL_PCIE_TX_DESCS_COUNT);
  999. /* Setup FRBD*/
  1000. data->rxq.frbds_p_addr = p_addr;
  1001. data->rxq.frbds = v_addr;
  1002. p_addr += (sizeof(struct frbd) * BTINTEL_PCIE_RX_DESCS_COUNT);
  1003. v_addr += (sizeof(struct frbd) * BTINTEL_PCIE_RX_DESCS_COUNT);
  1004. /* Setup urbd1 */
  1005. data->rxq.urbd1s_p_addr = p_addr;
  1006. data->rxq.urbd1s = v_addr;
  1007. p_addr += (sizeof(struct urbd1) * BTINTEL_PCIE_RX_DESCS_COUNT);
  1008. v_addr += (sizeof(struct urbd1) * BTINTEL_PCIE_RX_DESCS_COUNT);
  1009. /* Setup data buffers for txq */
  1010. err = btintel_pcie_setup_txq_bufs(data, &data->txq);
  1011. if (err)
  1012. goto exit_error_pool;
  1013. /* Setup data buffers for rxq */
  1014. err = btintel_pcie_setup_rxq_bufs(data, &data->rxq);
  1015. if (err)
  1016. goto exit_error_txq;
  1017. /* Setup Index Array */
  1018. btintel_pcie_setup_ia(data, p_addr, v_addr, &data->ia);
  1019. /* Setup Context Information */
  1020. p_addr += sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES * 4;
  1021. v_addr += sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES * 4;
  1022. data->ci = v_addr;
  1023. data->ci_p_addr = p_addr;
  1024. /* Initialize the CI */
  1025. btintel_pcie_init_ci(data, data->ci);
  1026. return 0;
  1027. exit_error_txq:
  1028. btintel_pcie_free_txq_bufs(data, &data->txq);
  1029. exit_error_pool:
  1030. dma_pool_free(data->dma_pool, data->dma_v_addr, data->dma_p_addr);
  1031. dma_pool_destroy(data->dma_pool);
  1032. exit_error:
  1033. return err;
  1034. }
  1035. static int btintel_pcie_open(struct hci_dev *hdev)
  1036. {
  1037. bt_dev_dbg(hdev, "");
  1038. return 0;
  1039. }
  1040. static int btintel_pcie_close(struct hci_dev *hdev)
  1041. {
  1042. bt_dev_dbg(hdev, "");
  1043. return 0;
  1044. }
  1045. static int btintel_pcie_inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
  1046. {
  1047. struct sk_buff *skb;
  1048. struct hci_event_hdr *hdr;
  1049. struct hci_ev_cmd_complete *evt;
  1050. skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
  1051. if (!skb)
  1052. return -ENOMEM;
  1053. hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
  1054. hdr->evt = HCI_EV_CMD_COMPLETE;
  1055. hdr->plen = sizeof(*evt) + 1;
  1056. evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
  1057. evt->ncmd = 0x01;
  1058. evt->opcode = cpu_to_le16(opcode);
  1059. *(u8 *)skb_put(skb, 1) = 0x00;
  1060. hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
  1061. return hci_recv_frame(hdev, skb);
  1062. }
  1063. static int btintel_pcie_send_frame(struct hci_dev *hdev,
  1064. struct sk_buff *skb)
  1065. {
  1066. struct btintel_pcie_data *data = hci_get_drvdata(hdev);
  1067. struct hci_command_hdr *cmd;
  1068. __u16 opcode = ~0;
  1069. int ret;
  1070. u32 type;
  1071. u32 old_ctxt;
  1072. /* Due to the fw limitation, the type header of the packet should be
  1073. * 4 bytes unlike 1 byte for UART. In UART, the firmware can read
  1074. * the first byte to get the packet type and redirect the rest of data
  1075. * packet to the right handler.
  1076. *
  1077. * But for PCIe, THF(Transfer Flow Handler) fetches the 4 bytes of data
  1078. * from DMA memory and by the time it reads the first 4 bytes, it has
  1079. * already consumed some part of packet. Thus the packet type indicator
  1080. * for iBT PCIe is 4 bytes.
  1081. *
  1082. * Luckily, when HCI core creates the skb, it allocates 8 bytes of
  1083. * head room for profile and driver use, and before sending the data
  1084. * to the device, append the iBT PCIe packet type in the front.
  1085. */
  1086. switch (hci_skb_pkt_type(skb)) {
  1087. case HCI_COMMAND_PKT:
  1088. type = BTINTEL_PCIE_HCI_CMD_PKT;
  1089. cmd = (void *)skb->data;
  1090. opcode = le16_to_cpu(cmd->opcode);
  1091. if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
  1092. struct hci_command_hdr *cmd = (void *)skb->data;
  1093. __u16 opcode = le16_to_cpu(cmd->opcode);
  1094. /* When the 0xfc01 command is issued to boot into
  1095. * the operational firmware, it will actually not
  1096. * send a command complete event. To keep the flow
  1097. * control working inject that event here.
  1098. */
  1099. if (opcode == 0xfc01)
  1100. btintel_pcie_inject_cmd_complete(hdev, opcode);
  1101. }
  1102. /* Firmware raises alive interrupt on HCI_OP_RESET */
  1103. if (opcode == HCI_OP_RESET)
  1104. data->gp0_received = false;
  1105. hdev->stat.cmd_tx++;
  1106. break;
  1107. case HCI_ACLDATA_PKT:
  1108. type = BTINTEL_PCIE_HCI_ACL_PKT;
  1109. hdev->stat.acl_tx++;
  1110. break;
  1111. case HCI_SCODATA_PKT:
  1112. type = BTINTEL_PCIE_HCI_SCO_PKT;
  1113. hdev->stat.sco_tx++;
  1114. break;
  1115. case HCI_ISODATA_PKT:
  1116. type = BTINTEL_PCIE_HCI_ISO_PKT;
  1117. break;
  1118. default:
  1119. bt_dev_err(hdev, "Unknown HCI packet type");
  1120. return -EILSEQ;
  1121. }
  1122. memcpy(skb_push(skb, BTINTEL_PCIE_HCI_TYPE_LEN), &type,
  1123. BTINTEL_PCIE_HCI_TYPE_LEN);
  1124. ret = btintel_pcie_send_sync(data, skb);
  1125. if (ret) {
  1126. hdev->stat.err_tx++;
  1127. bt_dev_err(hdev, "Failed to send frame (%d)", ret);
  1128. goto exit_error;
  1129. }
  1130. if (type == BTINTEL_PCIE_HCI_CMD_PKT &&
  1131. (opcode == HCI_OP_RESET || opcode == 0xfc01)) {
  1132. old_ctxt = data->alive_intr_ctxt;
  1133. data->alive_intr_ctxt =
  1134. (opcode == 0xfc01 ? BTINTEL_PCIE_INTEL_HCI_RESET1 :
  1135. BTINTEL_PCIE_HCI_RESET);
  1136. bt_dev_dbg(data->hdev, "sent cmd: 0x%4.4x alive context changed: %s -> %s",
  1137. opcode, btintel_pcie_alivectxt_state2str(old_ctxt),
  1138. btintel_pcie_alivectxt_state2str(data->alive_intr_ctxt));
  1139. if (opcode == HCI_OP_RESET) {
  1140. ret = wait_event_timeout(data->gp0_wait_q,
  1141. data->gp0_received,
  1142. msecs_to_jiffies(BTINTEL_DEFAULT_INTR_TIMEOUT_MS));
  1143. if (!ret) {
  1144. hdev->stat.err_tx++;
  1145. bt_dev_err(hdev, "No alive interrupt received for %s",
  1146. btintel_pcie_alivectxt_state2str(data->alive_intr_ctxt));
  1147. ret = -ETIME;
  1148. goto exit_error;
  1149. }
  1150. }
  1151. }
  1152. hdev->stat.byte_tx += skb->len;
  1153. kfree_skb(skb);
  1154. exit_error:
  1155. return ret;
  1156. }
  1157. static void btintel_pcie_release_hdev(struct btintel_pcie_data *data)
  1158. {
  1159. struct hci_dev *hdev;
  1160. hdev = data->hdev;
  1161. hci_unregister_dev(hdev);
  1162. hci_free_dev(hdev);
  1163. data->hdev = NULL;
  1164. }
  1165. static int btintel_pcie_setup(struct hci_dev *hdev)
  1166. {
  1167. const u8 param[1] = { 0xFF };
  1168. struct intel_version_tlv ver_tlv;
  1169. struct sk_buff *skb;
  1170. int err;
  1171. BT_DBG("%s", hdev->name);
  1172. skb = __hci_cmd_sync(hdev, 0xfc05, 1, param, HCI_CMD_TIMEOUT);
  1173. if (IS_ERR(skb)) {
  1174. bt_dev_err(hdev, "Reading Intel version command failed (%ld)",
  1175. PTR_ERR(skb));
  1176. return PTR_ERR(skb);
  1177. }
  1178. /* Check the status */
  1179. if (skb->data[0]) {
  1180. bt_dev_err(hdev, "Intel Read Version command failed (%02x)",
  1181. skb->data[0]);
  1182. err = -EIO;
  1183. goto exit_error;
  1184. }
  1185. /* Apply the common HCI quirks for Intel device */
  1186. set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
  1187. set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
  1188. set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
  1189. /* Set up the quality report callback for Intel devices */
  1190. hdev->set_quality_report = btintel_set_quality_report;
  1191. memset(&ver_tlv, 0, sizeof(ver_tlv));
  1192. /* For TLV type device, parse the tlv data */
  1193. err = btintel_parse_version_tlv(hdev, &ver_tlv, skb);
  1194. if (err) {
  1195. bt_dev_err(hdev, "Failed to parse TLV version information");
  1196. goto exit_error;
  1197. }
  1198. switch (INTEL_HW_PLATFORM(ver_tlv.cnvi_bt)) {
  1199. case 0x37:
  1200. break;
  1201. default:
  1202. bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)",
  1203. INTEL_HW_PLATFORM(ver_tlv.cnvi_bt));
  1204. err = -EINVAL;
  1205. goto exit_error;
  1206. }
  1207. /* Check for supported iBT hardware variants of this firmware
  1208. * loading method.
  1209. *
  1210. * This check has been put in place to ensure correct forward
  1211. * compatibility options when newer hardware variants come
  1212. * along.
  1213. */
  1214. switch (INTEL_HW_VARIANT(ver_tlv.cnvi_bt)) {
  1215. case 0x1e: /* BzrI */
  1216. /* Display version information of TLV type */
  1217. btintel_version_info_tlv(hdev, &ver_tlv);
  1218. /* Apply the device specific HCI quirks for TLV based devices
  1219. *
  1220. * All TLV based devices support WBS
  1221. */
  1222. set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
  1223. /* Setup MSFT Extension support */
  1224. btintel_set_msft_opcode(hdev,
  1225. INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
  1226. err = btintel_bootloader_setup_tlv(hdev, &ver_tlv);
  1227. if (err)
  1228. goto exit_error;
  1229. break;
  1230. default:
  1231. bt_dev_err(hdev, "Unsupported Intel hw variant (%u)",
  1232. INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
  1233. err = -EINVAL;
  1234. goto exit_error;
  1235. break;
  1236. }
  1237. btintel_print_fseq_info(hdev);
  1238. exit_error:
  1239. kfree_skb(skb);
  1240. return err;
  1241. }
  1242. static int btintel_pcie_setup_hdev(struct btintel_pcie_data *data)
  1243. {
  1244. int err;
  1245. struct hci_dev *hdev;
  1246. hdev = hci_alloc_dev_priv(sizeof(struct btintel_data));
  1247. if (!hdev)
  1248. return -ENOMEM;
  1249. hdev->bus = HCI_PCI;
  1250. hci_set_drvdata(hdev, data);
  1251. data->hdev = hdev;
  1252. SET_HCIDEV_DEV(hdev, &data->pdev->dev);
  1253. hdev->manufacturer = 2;
  1254. hdev->open = btintel_pcie_open;
  1255. hdev->close = btintel_pcie_close;
  1256. hdev->send = btintel_pcie_send_frame;
  1257. hdev->setup = btintel_pcie_setup;
  1258. hdev->shutdown = btintel_shutdown_combined;
  1259. hdev->hw_error = btintel_hw_error;
  1260. hdev->set_diag = btintel_set_diag;
  1261. hdev->set_bdaddr = btintel_set_bdaddr;
  1262. err = hci_register_dev(hdev);
  1263. if (err < 0) {
  1264. BT_ERR("Failed to register to hdev (%d)", err);
  1265. goto exit_error;
  1266. }
  1267. return 0;
  1268. exit_error:
  1269. hci_free_dev(hdev);
  1270. return err;
  1271. }
  1272. static int btintel_pcie_probe(struct pci_dev *pdev,
  1273. const struct pci_device_id *ent)
  1274. {
  1275. int err;
  1276. struct btintel_pcie_data *data;
  1277. if (!pdev)
  1278. return -ENODEV;
  1279. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  1280. if (!data)
  1281. return -ENOMEM;
  1282. data->pdev = pdev;
  1283. spin_lock_init(&data->irq_lock);
  1284. spin_lock_init(&data->hci_rx_lock);
  1285. init_waitqueue_head(&data->gp0_wait_q);
  1286. data->gp0_received = false;
  1287. init_waitqueue_head(&data->tx_wait_q);
  1288. data->tx_wait_done = false;
  1289. data->workqueue = alloc_ordered_workqueue(KBUILD_MODNAME, WQ_HIGHPRI);
  1290. if (!data->workqueue)
  1291. return -ENOMEM;
  1292. skb_queue_head_init(&data->rx_skb_q);
  1293. INIT_WORK(&data->rx_work, btintel_pcie_rx_work);
  1294. data->boot_stage_cache = 0x00;
  1295. data->img_resp_cache = 0x00;
  1296. err = btintel_pcie_config_pcie(pdev, data);
  1297. if (err)
  1298. goto exit_error;
  1299. pci_set_drvdata(pdev, data);
  1300. err = btintel_pcie_alloc(data);
  1301. if (err)
  1302. goto exit_error;
  1303. err = btintel_pcie_enable_bt(data);
  1304. if (err)
  1305. goto exit_error;
  1306. /* CNV information (CNVi and CNVr) is in CSR */
  1307. data->cnvi = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_HW_REV_REG);
  1308. data->cnvr = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_RF_ID_REG);
  1309. err = btintel_pcie_start_rx(data);
  1310. if (err)
  1311. goto exit_error;
  1312. err = btintel_pcie_setup_hdev(data);
  1313. if (err)
  1314. goto exit_error;
  1315. bt_dev_dbg(data->hdev, "cnvi: 0x%8.8x cnvr: 0x%8.8x", data->cnvi,
  1316. data->cnvr);
  1317. return 0;
  1318. exit_error:
  1319. /* reset device before exit */
  1320. btintel_pcie_reset_bt(data);
  1321. pci_clear_master(pdev);
  1322. pci_set_drvdata(pdev, NULL);
  1323. return err;
  1324. }
  1325. static void btintel_pcie_remove(struct pci_dev *pdev)
  1326. {
  1327. struct btintel_pcie_data *data;
  1328. data = pci_get_drvdata(pdev);
  1329. btintel_pcie_reset_bt(data);
  1330. for (int i = 0; i < data->alloc_vecs; i++) {
  1331. struct msix_entry *msix_entry;
  1332. msix_entry = &data->msix_entries[i];
  1333. free_irq(msix_entry->vector, msix_entry);
  1334. }
  1335. pci_free_irq_vectors(pdev);
  1336. btintel_pcie_release_hdev(data);
  1337. flush_work(&data->rx_work);
  1338. destroy_workqueue(data->workqueue);
  1339. btintel_pcie_free(data);
  1340. pci_clear_master(pdev);
  1341. pci_set_drvdata(pdev, NULL);
  1342. }
  1343. static struct pci_driver btintel_pcie_driver = {
  1344. .name = KBUILD_MODNAME,
  1345. .id_table = btintel_pcie_table,
  1346. .probe = btintel_pcie_probe,
  1347. .remove = btintel_pcie_remove,
  1348. };
  1349. module_pci_driver(btintel_pcie_driver);
  1350. MODULE_AUTHOR("Tedd Ho-Jeong An <tedd.an@intel.com>");
  1351. MODULE_DESCRIPTION("Intel Bluetooth PCIe transport driver ver " VERSION);
  1352. MODULE_VERSION(VERSION);
  1353. MODULE_LICENSE("GPL");