btintel_pcie.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761
  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 <linux/devcoredump.h>
  17. #include <net/bluetooth/bluetooth.h>
  18. #include <net/bluetooth/hci_core.h>
  19. #include <net/bluetooth/hci_drv.h>
  20. #include "btintel.h"
  21. #include "btintel_pcie.h"
  22. #define VERSION "0.1"
  23. #define BTINTEL_PCI_DEVICE(dev, subdev) \
  24. .vendor = PCI_VENDOR_ID_INTEL, \
  25. .device = (dev), \
  26. .subvendor = PCI_ANY_ID, \
  27. .subdevice = (subdev), \
  28. .driver_data = 0
  29. #define POLL_INTERVAL_US 10
  30. /* Intel Bluetooth PCIe device id table */
  31. static const struct pci_device_id btintel_pcie_table[] = {
  32. /* BlazarI, Wildcat Lake */
  33. { BTINTEL_PCI_DEVICE(0x4D76, PCI_ANY_ID) },
  34. /* BlazarI, Lunar Lake */
  35. { BTINTEL_PCI_DEVICE(0xA876, PCI_ANY_ID) },
  36. /* Scorpious, Panther Lake-H484 */
  37. { BTINTEL_PCI_DEVICE(0xE376, PCI_ANY_ID) },
  38. /* Scorpious, Panther Lake-H404 */
  39. { BTINTEL_PCI_DEVICE(0xE476, PCI_ANY_ID) },
  40. { 0 }
  41. };
  42. MODULE_DEVICE_TABLE(pci, btintel_pcie_table);
  43. struct btintel_pcie_dev_recovery {
  44. struct list_head list;
  45. u8 count;
  46. time64_t last_error;
  47. char name[];
  48. };
  49. /* Intel PCIe uses 4 bytes of HCI type instead of 1 byte BT SIG HCI type */
  50. #define BTINTEL_PCIE_HCI_TYPE_LEN 4
  51. #define BTINTEL_PCIE_HCI_CMD_PKT 0x00000001
  52. #define BTINTEL_PCIE_HCI_ACL_PKT 0x00000002
  53. #define BTINTEL_PCIE_HCI_SCO_PKT 0x00000003
  54. #define BTINTEL_PCIE_HCI_EVT_PKT 0x00000004
  55. #define BTINTEL_PCIE_HCI_ISO_PKT 0x00000005
  56. #define BTINTEL_PCIE_MAGIC_NUM 0xA5A5A5A5
  57. #define BTINTEL_PCIE_BLZR_HWEXP_SIZE 1024
  58. #define BTINTEL_PCIE_BLZR_HWEXP_DMP_ADDR 0xB00A7C00
  59. #define BTINTEL_PCIE_SCP_HWEXP_SIZE 4096
  60. #define BTINTEL_PCIE_SCP_HWEXP_DMP_ADDR 0xB030F800
  61. #define BTINTEL_PCIE_MAGIC_NUM 0xA5A5A5A5
  62. #define BTINTEL_PCIE_TRIGGER_REASON_USER_TRIGGER 0x17A2
  63. #define BTINTEL_PCIE_TRIGGER_REASON_FW_ASSERT 0x1E61
  64. #define BTINTEL_PCIE_RESET_WINDOW_SECS 5
  65. #define BTINTEL_PCIE_FLR_MAX_RETRY 1
  66. /* Alive interrupt context */
  67. enum {
  68. BTINTEL_PCIE_ROM,
  69. BTINTEL_PCIE_FW_DL,
  70. BTINTEL_PCIE_HCI_RESET,
  71. BTINTEL_PCIE_INTEL_HCI_RESET1,
  72. BTINTEL_PCIE_INTEL_HCI_RESET2,
  73. BTINTEL_PCIE_D0,
  74. BTINTEL_PCIE_D3
  75. };
  76. /* Structure for dbgc fragment buffer
  77. * @buf_addr_lsb: LSB of the buffer's physical address
  78. * @buf_addr_msb: MSB of the buffer's physical address
  79. * @buf_size: Total size of the buffer
  80. */
  81. struct btintel_pcie_dbgc_ctxt_buf {
  82. u32 buf_addr_lsb;
  83. u32 buf_addr_msb;
  84. u32 buf_size;
  85. };
  86. /* Structure for dbgc fragment
  87. * @magic_num: 0XA5A5A5A5
  88. * @ver: For Driver-FW compatibility
  89. * @total_size: Total size of the payload debug info
  90. * @num_buf: Num of allocated debug bufs
  91. * @bufs: All buffer's addresses and sizes
  92. */
  93. struct btintel_pcie_dbgc_ctxt {
  94. u32 magic_num;
  95. u32 ver;
  96. u32 total_size;
  97. u32 num_buf;
  98. struct btintel_pcie_dbgc_ctxt_buf bufs[BTINTEL_PCIE_DBGC_BUFFER_COUNT];
  99. };
  100. struct btintel_pcie_removal {
  101. struct pci_dev *pdev;
  102. struct work_struct work;
  103. };
  104. static LIST_HEAD(btintel_pcie_recovery_list);
  105. static DEFINE_SPINLOCK(btintel_pcie_recovery_lock);
  106. static inline char *btintel_pcie_alivectxt_state2str(u32 alive_intr_ctxt)
  107. {
  108. switch (alive_intr_ctxt) {
  109. case BTINTEL_PCIE_ROM:
  110. return "rom";
  111. case BTINTEL_PCIE_FW_DL:
  112. return "fw_dl";
  113. case BTINTEL_PCIE_D0:
  114. return "d0";
  115. case BTINTEL_PCIE_D3:
  116. return "d3";
  117. case BTINTEL_PCIE_HCI_RESET:
  118. return "hci_reset";
  119. case BTINTEL_PCIE_INTEL_HCI_RESET1:
  120. return "intel_reset1";
  121. case BTINTEL_PCIE_INTEL_HCI_RESET2:
  122. return "intel_reset2";
  123. default:
  124. return "unknown";
  125. }
  126. }
  127. /* This function initializes the memory for DBGC buffers and formats the
  128. * DBGC fragment which consists header info and DBGC buffer's LSB, MSB and
  129. * size as the payload
  130. */
  131. static int btintel_pcie_setup_dbgc(struct btintel_pcie_data *data)
  132. {
  133. struct btintel_pcie_dbgc_ctxt db_frag;
  134. struct data_buf *buf;
  135. int i;
  136. data->dbgc.count = BTINTEL_PCIE_DBGC_BUFFER_COUNT;
  137. data->dbgc.bufs = devm_kcalloc(&data->pdev->dev, data->dbgc.count,
  138. sizeof(*buf), GFP_KERNEL);
  139. if (!data->dbgc.bufs)
  140. return -ENOMEM;
  141. data->dbgc.buf_v_addr = dmam_alloc_coherent(&data->pdev->dev,
  142. data->dbgc.count *
  143. BTINTEL_PCIE_DBGC_BUFFER_SIZE,
  144. &data->dbgc.buf_p_addr,
  145. GFP_KERNEL | __GFP_NOWARN);
  146. if (!data->dbgc.buf_v_addr)
  147. return -ENOMEM;
  148. data->dbgc.frag_v_addr = dmam_alloc_coherent(&data->pdev->dev,
  149. sizeof(struct btintel_pcie_dbgc_ctxt),
  150. &data->dbgc.frag_p_addr,
  151. GFP_KERNEL | __GFP_NOWARN);
  152. if (!data->dbgc.frag_v_addr)
  153. return -ENOMEM;
  154. data->dbgc.frag_size = sizeof(struct btintel_pcie_dbgc_ctxt);
  155. db_frag.magic_num = BTINTEL_PCIE_MAGIC_NUM;
  156. db_frag.ver = BTINTEL_PCIE_DBGC_FRAG_VERSION;
  157. db_frag.total_size = BTINTEL_PCIE_DBGC_FRAG_PAYLOAD_SIZE;
  158. db_frag.num_buf = BTINTEL_PCIE_DBGC_FRAG_BUFFER_COUNT;
  159. for (i = 0; i < data->dbgc.count; i++) {
  160. buf = &data->dbgc.bufs[i];
  161. buf->data_p_addr = data->dbgc.buf_p_addr + i * BTINTEL_PCIE_DBGC_BUFFER_SIZE;
  162. buf->data = data->dbgc.buf_v_addr + i * BTINTEL_PCIE_DBGC_BUFFER_SIZE;
  163. db_frag.bufs[i].buf_addr_lsb = lower_32_bits(buf->data_p_addr);
  164. db_frag.bufs[i].buf_addr_msb = upper_32_bits(buf->data_p_addr);
  165. db_frag.bufs[i].buf_size = BTINTEL_PCIE_DBGC_BUFFER_SIZE;
  166. }
  167. memcpy(data->dbgc.frag_v_addr, &db_frag, sizeof(db_frag));
  168. return 0;
  169. }
  170. static inline void ipc_print_ia_ring(struct hci_dev *hdev, struct ia *ia,
  171. u16 queue_num)
  172. {
  173. bt_dev_dbg(hdev, "IA: %s: tr-h:%02u tr-t:%02u cr-h:%02u cr-t:%02u",
  174. queue_num == BTINTEL_PCIE_TXQ_NUM ? "TXQ" : "RXQ",
  175. ia->tr_hia[queue_num], ia->tr_tia[queue_num],
  176. ia->cr_hia[queue_num], ia->cr_tia[queue_num]);
  177. }
  178. static inline void ipc_print_urbd1(struct hci_dev *hdev, struct urbd1 *urbd1,
  179. u16 index)
  180. {
  181. bt_dev_dbg(hdev, "RXQ:urbd1(%u) frbd_tag:%u status: 0x%x fixed:0x%x",
  182. index, urbd1->frbd_tag, urbd1->status, urbd1->fixed);
  183. }
  184. static struct btintel_pcie_data *btintel_pcie_get_data(struct msix_entry *entry)
  185. {
  186. u8 queue = entry->entry;
  187. struct msix_entry *entries = entry - queue;
  188. return container_of(entries, struct btintel_pcie_data, msix_entries[0]);
  189. }
  190. /* Set the doorbell for TXQ to notify the device that @index (actually index-1)
  191. * of the TFD is updated and ready to transmit.
  192. */
  193. static void btintel_pcie_set_tx_db(struct btintel_pcie_data *data, u16 index)
  194. {
  195. u32 val;
  196. val = index;
  197. val |= (BTINTEL_PCIE_TX_DB_VEC << 16);
  198. btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_HBUS_TARG_WRPTR, val);
  199. }
  200. /* Copy the data to next(@tfd_index) data buffer and update the TFD(transfer
  201. * descriptor) with the data length and the DMA address of the data buffer.
  202. */
  203. static void btintel_pcie_prepare_tx(struct txq *txq, u16 tfd_index,
  204. struct sk_buff *skb)
  205. {
  206. struct data_buf *buf;
  207. struct tfd *tfd;
  208. tfd = &txq->tfds[tfd_index];
  209. memset(tfd, 0, sizeof(*tfd));
  210. buf = &txq->bufs[tfd_index];
  211. tfd->size = skb->len;
  212. tfd->addr = buf->data_p_addr;
  213. /* Copy the outgoing data to DMA buffer */
  214. memcpy(buf->data, skb->data, tfd->size);
  215. }
  216. static inline void btintel_pcie_dump_debug_registers(struct hci_dev *hdev)
  217. {
  218. struct btintel_pcie_data *data = hci_get_drvdata(hdev);
  219. u16 cr_hia, cr_tia;
  220. u32 reg, mbox_reg;
  221. struct sk_buff *skb;
  222. u8 buf[80];
  223. skb = alloc_skb(1024, GFP_ATOMIC);
  224. if (!skb)
  225. return;
  226. snprintf(buf, sizeof(buf), "%s", "---- Dump of debug registers ---");
  227. bt_dev_dbg(hdev, "%s", buf);
  228. skb_put_data(skb, buf, strlen(buf));
  229. reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_BOOT_STAGE_REG);
  230. snprintf(buf, sizeof(buf), "boot stage: 0x%8.8x", reg);
  231. bt_dev_dbg(hdev, "%s", buf);
  232. skb_put_data(skb, buf, strlen(buf));
  233. data->boot_stage_cache = reg;
  234. reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_IPC_STATUS_REG);
  235. snprintf(buf, sizeof(buf), "ipc status: 0x%8.8x", reg);
  236. skb_put_data(skb, buf, strlen(buf));
  237. bt_dev_dbg(hdev, "%s", buf);
  238. reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_IPC_CONTROL_REG);
  239. snprintf(buf, sizeof(buf), "ipc control: 0x%8.8x", reg);
  240. skb_put_data(skb, buf, strlen(buf));
  241. bt_dev_dbg(hdev, "%s", buf);
  242. reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_IPC_SLEEP_CTL_REG);
  243. snprintf(buf, sizeof(buf), "ipc sleep control: 0x%8.8x", reg);
  244. skb_put_data(skb, buf, strlen(buf));
  245. bt_dev_dbg(hdev, "%s", buf);
  246. /*Read the Mail box status and registers*/
  247. reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_MBOX_STATUS_REG);
  248. snprintf(buf, sizeof(buf), "mbox status: 0x%8.8x", reg);
  249. skb_put_data(skb, buf, strlen(buf));
  250. if (reg & BTINTEL_PCIE_CSR_MBOX_STATUS_MBOX1) {
  251. mbox_reg = btintel_pcie_rd_reg32(data,
  252. BTINTEL_PCIE_CSR_MBOX_1_REG);
  253. snprintf(buf, sizeof(buf), "mbox_1: 0x%8.8x", mbox_reg);
  254. skb_put_data(skb, buf, strlen(buf));
  255. bt_dev_dbg(hdev, "%s", buf);
  256. }
  257. if (reg & BTINTEL_PCIE_CSR_MBOX_STATUS_MBOX2) {
  258. mbox_reg = btintel_pcie_rd_reg32(data,
  259. BTINTEL_PCIE_CSR_MBOX_2_REG);
  260. snprintf(buf, sizeof(buf), "mbox_2: 0x%8.8x", mbox_reg);
  261. skb_put_data(skb, buf, strlen(buf));
  262. bt_dev_dbg(hdev, "%s", buf);
  263. }
  264. if (reg & BTINTEL_PCIE_CSR_MBOX_STATUS_MBOX3) {
  265. mbox_reg = btintel_pcie_rd_reg32(data,
  266. BTINTEL_PCIE_CSR_MBOX_3_REG);
  267. snprintf(buf, sizeof(buf), "mbox_3: 0x%8.8x", mbox_reg);
  268. skb_put_data(skb, buf, strlen(buf));
  269. bt_dev_dbg(hdev, "%s", buf);
  270. }
  271. if (reg & BTINTEL_PCIE_CSR_MBOX_STATUS_MBOX4) {
  272. mbox_reg = btintel_pcie_rd_reg32(data,
  273. BTINTEL_PCIE_CSR_MBOX_4_REG);
  274. snprintf(buf, sizeof(buf), "mbox_4: 0x%8.8x", mbox_reg);
  275. skb_put_data(skb, buf, strlen(buf));
  276. bt_dev_dbg(hdev, "%s", buf);
  277. }
  278. cr_hia = data->ia.cr_hia[BTINTEL_PCIE_RXQ_NUM];
  279. cr_tia = data->ia.cr_tia[BTINTEL_PCIE_RXQ_NUM];
  280. snprintf(buf, sizeof(buf), "rxq: cr_tia: %u cr_hia: %u", cr_tia, cr_hia);
  281. skb_put_data(skb, buf, strlen(buf));
  282. bt_dev_dbg(hdev, "%s", buf);
  283. cr_hia = data->ia.cr_hia[BTINTEL_PCIE_TXQ_NUM];
  284. cr_tia = data->ia.cr_tia[BTINTEL_PCIE_TXQ_NUM];
  285. snprintf(buf, sizeof(buf), "txq: cr_tia: %u cr_hia: %u", cr_tia, cr_hia);
  286. skb_put_data(skb, buf, strlen(buf));
  287. bt_dev_dbg(hdev, "%s", buf);
  288. snprintf(buf, sizeof(buf), "--------------------------------");
  289. bt_dev_dbg(hdev, "%s", buf);
  290. hci_recv_diag(hdev, skb);
  291. }
  292. static int btintel_pcie_send_sync(struct btintel_pcie_data *data,
  293. struct sk_buff *skb, u32 pkt_type, u16 opcode)
  294. {
  295. int ret;
  296. u16 tfd_index;
  297. u32 old_ctxt;
  298. bool wait_on_alive = false;
  299. struct hci_dev *hdev = data->hdev;
  300. struct txq *txq = &data->txq;
  301. tfd_index = data->ia.tr_hia[BTINTEL_PCIE_TXQ_NUM];
  302. if (tfd_index > txq->count)
  303. return -ERANGE;
  304. /* Firmware raises alive interrupt on HCI_OP_RESET or
  305. * BTINTEL_HCI_OP_RESET
  306. */
  307. wait_on_alive = (pkt_type == BTINTEL_PCIE_HCI_CMD_PKT &&
  308. (opcode == BTINTEL_HCI_OP_RESET || opcode == HCI_OP_RESET));
  309. if (wait_on_alive) {
  310. data->gp0_received = false;
  311. old_ctxt = data->alive_intr_ctxt;
  312. data->alive_intr_ctxt =
  313. (opcode == BTINTEL_HCI_OP_RESET ? BTINTEL_PCIE_INTEL_HCI_RESET1 :
  314. BTINTEL_PCIE_HCI_RESET);
  315. bt_dev_dbg(data->hdev, "sending cmd: 0x%4.4x alive context changed: %s -> %s",
  316. opcode, btintel_pcie_alivectxt_state2str(old_ctxt),
  317. btintel_pcie_alivectxt_state2str(data->alive_intr_ctxt));
  318. }
  319. memcpy(skb_push(skb, BTINTEL_PCIE_HCI_TYPE_LEN), &pkt_type,
  320. BTINTEL_PCIE_HCI_TYPE_LEN);
  321. /* Prepare for TX. It updates the TFD with the length of data and
  322. * address of the DMA buffer, and copy the data to the DMA buffer
  323. */
  324. btintel_pcie_prepare_tx(txq, tfd_index, skb);
  325. tfd_index = (tfd_index + 1) % txq->count;
  326. data->ia.tr_hia[BTINTEL_PCIE_TXQ_NUM] = tfd_index;
  327. /* Arm wait event condition */
  328. data->tx_wait_done = false;
  329. /* Set the doorbell to notify the device */
  330. btintel_pcie_set_tx_db(data, tfd_index);
  331. /* Wait for the complete interrupt - URBD0 */
  332. ret = wait_event_timeout(data->tx_wait_q, data->tx_wait_done,
  333. msecs_to_jiffies(BTINTEL_PCIE_TX_WAIT_TIMEOUT_MS));
  334. if (!ret) {
  335. bt_dev_err(data->hdev, "Timeout (%u ms) on tx completion",
  336. BTINTEL_PCIE_TX_WAIT_TIMEOUT_MS);
  337. btintel_pcie_dump_debug_registers(data->hdev);
  338. return -ETIME;
  339. }
  340. if (wait_on_alive) {
  341. ret = wait_event_timeout(data->gp0_wait_q,
  342. data->gp0_received,
  343. msecs_to_jiffies(BTINTEL_DEFAULT_INTR_TIMEOUT_MS));
  344. if (!ret) {
  345. hdev->stat.err_tx++;
  346. bt_dev_err(hdev, "Timeout (%u ms) on alive interrupt, alive context: %s",
  347. BTINTEL_DEFAULT_INTR_TIMEOUT_MS,
  348. btintel_pcie_alivectxt_state2str(data->alive_intr_ctxt));
  349. return -ETIME;
  350. }
  351. }
  352. return 0;
  353. }
  354. /* Set the doorbell for RXQ to notify the device that @index (actually index-1)
  355. * is available to receive the data
  356. */
  357. static void btintel_pcie_set_rx_db(struct btintel_pcie_data *data, u16 index)
  358. {
  359. u32 val;
  360. val = index;
  361. val |= (BTINTEL_PCIE_RX_DB_VEC << 16);
  362. btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_HBUS_TARG_WRPTR, val);
  363. }
  364. /* Update the FRBD (free buffer descriptor) with the @frbd_index and the
  365. * DMA address of the free buffer.
  366. */
  367. static void btintel_pcie_prepare_rx(struct rxq *rxq, u16 frbd_index)
  368. {
  369. struct data_buf *buf;
  370. struct frbd *frbd;
  371. /* Get the buffer of the FRBD for DMA */
  372. buf = &rxq->bufs[frbd_index];
  373. frbd = &rxq->frbds[frbd_index];
  374. memset(frbd, 0, sizeof(*frbd));
  375. /* Update FRBD */
  376. frbd->tag = frbd_index;
  377. frbd->addr = buf->data_p_addr;
  378. }
  379. static int btintel_pcie_submit_rx(struct btintel_pcie_data *data)
  380. {
  381. u16 frbd_index;
  382. struct rxq *rxq = &data->rxq;
  383. frbd_index = data->ia.tr_hia[BTINTEL_PCIE_RXQ_NUM];
  384. if (frbd_index > rxq->count)
  385. return -ERANGE;
  386. /* Prepare for RX submit. It updates the FRBD with the address of DMA
  387. * buffer
  388. */
  389. btintel_pcie_prepare_rx(rxq, frbd_index);
  390. frbd_index = (frbd_index + 1) % rxq->count;
  391. data->ia.tr_hia[BTINTEL_PCIE_RXQ_NUM] = frbd_index;
  392. ipc_print_ia_ring(data->hdev, &data->ia, BTINTEL_PCIE_RXQ_NUM);
  393. /* Set the doorbell to notify the device */
  394. btintel_pcie_set_rx_db(data, frbd_index);
  395. return 0;
  396. }
  397. static int btintel_pcie_start_rx(struct btintel_pcie_data *data)
  398. {
  399. int i, ret;
  400. struct rxq *rxq = &data->rxq;
  401. /* Post (BTINTEL_PCIE_RX_DESCS_COUNT - 3) buffers to overcome the
  402. * hardware issues leading to race condition at the firmware.
  403. */
  404. for (i = 0; i < rxq->count - 3; i++) {
  405. ret = btintel_pcie_submit_rx(data);
  406. if (ret)
  407. return ret;
  408. }
  409. return 0;
  410. }
  411. static void btintel_pcie_reset_ia(struct btintel_pcie_data *data)
  412. {
  413. memset(data->ia.tr_hia, 0, sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES);
  414. memset(data->ia.tr_tia, 0, sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES);
  415. memset(data->ia.cr_hia, 0, sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES);
  416. memset(data->ia.cr_tia, 0, sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES);
  417. }
  418. static int btintel_pcie_reset_bt(struct btintel_pcie_data *data)
  419. {
  420. u32 reg;
  421. int retry = 3;
  422. reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG);
  423. reg &= ~(BTINTEL_PCIE_CSR_FUNC_CTRL_FUNC_ENA |
  424. BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_INIT |
  425. BTINTEL_PCIE_CSR_FUNC_CTRL_FUNC_INIT);
  426. reg |= BTINTEL_PCIE_CSR_FUNC_CTRL_BUS_MASTER_DISCON;
  427. btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG, reg);
  428. do {
  429. reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG);
  430. if (reg & BTINTEL_PCIE_CSR_FUNC_CTRL_BUS_MASTER_STS)
  431. break;
  432. usleep_range(10000, 12000);
  433. } while (--retry > 0);
  434. usleep_range(10000, 12000);
  435. reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG);
  436. reg &= ~(BTINTEL_PCIE_CSR_FUNC_CTRL_FUNC_ENA |
  437. BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_INIT |
  438. BTINTEL_PCIE_CSR_FUNC_CTRL_FUNC_INIT);
  439. reg |= BTINTEL_PCIE_CSR_FUNC_CTRL_SW_RESET;
  440. btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG, reg);
  441. usleep_range(10000, 12000);
  442. reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG);
  443. bt_dev_dbg(data->hdev, "csr register after reset: 0x%8.8x", reg);
  444. reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_BOOT_STAGE_REG);
  445. /* If shared hardware reset is success then boot stage register shall be
  446. * set to 0
  447. */
  448. return reg == 0 ? 0 : -ENODEV;
  449. }
  450. static void btintel_pcie_mac_init(struct btintel_pcie_data *data)
  451. {
  452. u32 reg;
  453. /* Set MAC_INIT bit to start primary bootloader */
  454. reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG);
  455. reg &= ~(BTINTEL_PCIE_CSR_FUNC_CTRL_FUNC_INIT |
  456. BTINTEL_PCIE_CSR_FUNC_CTRL_BUS_MASTER_DISCON |
  457. BTINTEL_PCIE_CSR_FUNC_CTRL_SW_RESET);
  458. reg |= (BTINTEL_PCIE_CSR_FUNC_CTRL_FUNC_ENA |
  459. BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_INIT);
  460. btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG, reg);
  461. }
  462. static int btintel_pcie_get_mac_access(struct btintel_pcie_data *data)
  463. {
  464. u32 reg;
  465. int retry = 15;
  466. reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG);
  467. reg |= BTINTEL_PCIE_CSR_FUNC_CTRL_STOP_MAC_ACCESS_DIS;
  468. reg |= BTINTEL_PCIE_CSR_FUNC_CTRL_XTAL_CLK_REQ;
  469. if ((reg & BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_ACCESS_STS) == 0)
  470. reg |= BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_ACCESS_REQ;
  471. btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG, reg);
  472. do {
  473. reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG);
  474. if (reg & BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_ACCESS_STS)
  475. return 0;
  476. /* Need delay here for Target Access harwdware to settle down*/
  477. usleep_range(1000, 1200);
  478. } while (--retry > 0);
  479. return -ETIME;
  480. }
  481. static void btintel_pcie_release_mac_access(struct btintel_pcie_data *data)
  482. {
  483. u32 reg;
  484. reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG);
  485. if (reg & BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_ACCESS_REQ)
  486. reg &= ~BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_ACCESS_REQ;
  487. if (reg & BTINTEL_PCIE_CSR_FUNC_CTRL_STOP_MAC_ACCESS_DIS)
  488. reg &= ~BTINTEL_PCIE_CSR_FUNC_CTRL_STOP_MAC_ACCESS_DIS;
  489. if (reg & BTINTEL_PCIE_CSR_FUNC_CTRL_XTAL_CLK_REQ)
  490. reg &= ~BTINTEL_PCIE_CSR_FUNC_CTRL_XTAL_CLK_REQ;
  491. btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG, reg);
  492. }
  493. static void *btintel_pcie_copy_tlv(void *dest, enum btintel_pcie_tlv_type type,
  494. void *data, size_t size)
  495. {
  496. struct intel_tlv *tlv;
  497. tlv = dest;
  498. tlv->type = type;
  499. tlv->len = size;
  500. memcpy(tlv->val, data, tlv->len);
  501. return dest + sizeof(*tlv) + size;
  502. }
  503. static int btintel_pcie_read_dram_buffers(struct btintel_pcie_data *data)
  504. {
  505. u32 offset, prev_size, wr_ptr_status, dump_size, data_len;
  506. struct btintel_pcie_dbgc *dbgc = &data->dbgc;
  507. struct hci_dev *hdev = data->hdev;
  508. u8 *pdata, *p, buf_idx;
  509. struct intel_tlv *tlv;
  510. struct timespec64 now;
  511. struct tm tm_now;
  512. char fw_build[128];
  513. char ts[128];
  514. char vendor[64];
  515. char driver[64];
  516. if (!IS_ENABLED(CONFIG_DEV_COREDUMP))
  517. return -EOPNOTSUPP;
  518. wr_ptr_status = btintel_pcie_rd_dev_mem(data, BTINTEL_PCIE_DBGC_CUR_DBGBUFF_STATUS);
  519. offset = wr_ptr_status & BTINTEL_PCIE_DBG_OFFSET_BIT_MASK;
  520. buf_idx = BTINTEL_PCIE_DBGC_DBG_BUF_IDX(wr_ptr_status);
  521. if (buf_idx > dbgc->count) {
  522. bt_dev_warn(hdev, "Buffer index is invalid");
  523. return -EINVAL;
  524. }
  525. prev_size = buf_idx * BTINTEL_PCIE_DBGC_BUFFER_SIZE;
  526. if (prev_size + offset >= prev_size)
  527. data->dmp_hdr.write_ptr = prev_size + offset;
  528. else
  529. return -EINVAL;
  530. snprintf(vendor, sizeof(vendor), "Vendor: Intel\n");
  531. snprintf(driver, sizeof(driver), "Driver: %s\n",
  532. data->dmp_hdr.driver_name);
  533. ktime_get_real_ts64(&now);
  534. time64_to_tm(now.tv_sec, 0, &tm_now);
  535. snprintf(ts, sizeof(ts), "Dump Time: %02d-%02d-%04ld %02d:%02d:%02d",
  536. tm_now.tm_mday, tm_now.tm_mon + 1, tm_now.tm_year + 1900,
  537. tm_now.tm_hour, tm_now.tm_min, tm_now.tm_sec);
  538. snprintf(fw_build, sizeof(fw_build),
  539. "Firmware Timestamp: Year %u WW %02u buildtype %u build %u",
  540. 2000 + (data->dmp_hdr.fw_timestamp >> 8),
  541. data->dmp_hdr.fw_timestamp & 0xff, data->dmp_hdr.fw_build_type,
  542. data->dmp_hdr.fw_build_num);
  543. data_len = sizeof(*tlv) + sizeof(data->dmp_hdr.cnvi_bt) +
  544. sizeof(*tlv) + sizeof(data->dmp_hdr.write_ptr) +
  545. sizeof(*tlv) + sizeof(data->dmp_hdr.wrap_ctr) +
  546. sizeof(*tlv) + sizeof(data->dmp_hdr.trigger_reason) +
  547. sizeof(*tlv) + sizeof(data->dmp_hdr.fw_git_sha1) +
  548. sizeof(*tlv) + sizeof(data->dmp_hdr.cnvr_top) +
  549. sizeof(*tlv) + sizeof(data->dmp_hdr.cnvi_top) +
  550. sizeof(*tlv) + strlen(ts) +
  551. sizeof(*tlv) + strlen(fw_build) +
  552. sizeof(*tlv) + strlen(vendor) +
  553. sizeof(*tlv) + strlen(driver);
  554. /*
  555. * sizeof(u32) - signature
  556. * sizeof(data_len) - to store tlv data size
  557. * data_len - TLV data
  558. */
  559. dump_size = sizeof(u32) + sizeof(data_len) + data_len;
  560. /* Add debug buffers data length to dump size */
  561. dump_size += BTINTEL_PCIE_DBGC_BUFFER_SIZE * dbgc->count;
  562. pdata = vmalloc(dump_size);
  563. if (!pdata)
  564. return -ENOMEM;
  565. p = pdata;
  566. *(u32 *)p = BTINTEL_PCIE_MAGIC_NUM;
  567. p += sizeof(u32);
  568. *(u32 *)p = data_len;
  569. p += sizeof(u32);
  570. p = btintel_pcie_copy_tlv(p, BTINTEL_VENDOR, vendor, strlen(vendor));
  571. p = btintel_pcie_copy_tlv(p, BTINTEL_DRIVER, driver, strlen(driver));
  572. p = btintel_pcie_copy_tlv(p, BTINTEL_DUMP_TIME, ts, strlen(ts));
  573. p = btintel_pcie_copy_tlv(p, BTINTEL_FW_BUILD, fw_build,
  574. strlen(fw_build));
  575. p = btintel_pcie_copy_tlv(p, BTINTEL_CNVI_BT, &data->dmp_hdr.cnvi_bt,
  576. sizeof(data->dmp_hdr.cnvi_bt));
  577. p = btintel_pcie_copy_tlv(p, BTINTEL_WRITE_PTR, &data->dmp_hdr.write_ptr,
  578. sizeof(data->dmp_hdr.write_ptr));
  579. p = btintel_pcie_copy_tlv(p, BTINTEL_WRAP_CTR, &data->dmp_hdr.wrap_ctr,
  580. sizeof(data->dmp_hdr.wrap_ctr));
  581. data->dmp_hdr.wrap_ctr = btintel_pcie_rd_dev_mem(data,
  582. BTINTEL_PCIE_DBGC_DBGBUFF_WRAP_ARND);
  583. p = btintel_pcie_copy_tlv(p, BTINTEL_TRIGGER_REASON, &data->dmp_hdr.trigger_reason,
  584. sizeof(data->dmp_hdr.trigger_reason));
  585. p = btintel_pcie_copy_tlv(p, BTINTEL_FW_SHA, &data->dmp_hdr.fw_git_sha1,
  586. sizeof(data->dmp_hdr.fw_git_sha1));
  587. p = btintel_pcie_copy_tlv(p, BTINTEL_CNVR_TOP, &data->dmp_hdr.cnvr_top,
  588. sizeof(data->dmp_hdr.cnvr_top));
  589. p = btintel_pcie_copy_tlv(p, BTINTEL_CNVI_TOP, &data->dmp_hdr.cnvi_top,
  590. sizeof(data->dmp_hdr.cnvi_top));
  591. memcpy(p, dbgc->bufs[0].data, dbgc->count * BTINTEL_PCIE_DBGC_BUFFER_SIZE);
  592. dev_coredumpv(&hdev->dev, pdata, dump_size, GFP_KERNEL);
  593. return 0;
  594. }
  595. static void btintel_pcie_dump_traces(struct hci_dev *hdev)
  596. {
  597. struct btintel_pcie_data *data = hci_get_drvdata(hdev);
  598. int ret = 0;
  599. ret = btintel_pcie_get_mac_access(data);
  600. if (ret) {
  601. bt_dev_err(hdev, "Failed to get mac access: (%d)", ret);
  602. return;
  603. }
  604. ret = btintel_pcie_read_dram_buffers(data);
  605. btintel_pcie_release_mac_access(data);
  606. if (ret)
  607. bt_dev_err(hdev, "Failed to dump traces: (%d)", ret);
  608. }
  609. /* This function enables BT function by setting BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_INIT bit in
  610. * BTINTEL_PCIE_CSR_FUNC_CTRL_REG register and wait for MSI-X with
  611. * BTINTEL_PCIE_MSIX_HW_INT_CAUSES_GP0.
  612. * Then the host reads firmware version from BTINTEL_CSR_F2D_MBX and the boot stage
  613. * from BTINTEL_PCIE_CSR_BOOT_STAGE_REG.
  614. */
  615. static int btintel_pcie_enable_bt(struct btintel_pcie_data *data)
  616. {
  617. int err;
  618. u32 reg;
  619. data->gp0_received = false;
  620. /* Update the DMA address of CI struct to CSR */
  621. btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_CI_ADDR_LSB_REG,
  622. data->ci_p_addr & 0xffffffff);
  623. btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_CI_ADDR_MSB_REG,
  624. (u64)data->ci_p_addr >> 32);
  625. /* Reset the cached value of boot stage. it is updated by the MSI-X
  626. * gp0 interrupt handler.
  627. */
  628. data->boot_stage_cache = 0x0;
  629. /* Set MAC_INIT bit to start primary bootloader */
  630. reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG);
  631. reg &= ~(BTINTEL_PCIE_CSR_FUNC_CTRL_FUNC_INIT |
  632. BTINTEL_PCIE_CSR_FUNC_CTRL_BUS_MASTER_DISCON |
  633. BTINTEL_PCIE_CSR_FUNC_CTRL_SW_RESET);
  634. reg |= (BTINTEL_PCIE_CSR_FUNC_CTRL_FUNC_ENA |
  635. BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_INIT);
  636. btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG, reg);
  637. /* MAC is ready. Enable BT FUNC */
  638. btintel_pcie_set_reg_bits(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG,
  639. BTINTEL_PCIE_CSR_FUNC_CTRL_FUNC_INIT);
  640. btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG);
  641. /* wait for interrupt from the device after booting up to primary
  642. * bootloader.
  643. */
  644. data->alive_intr_ctxt = BTINTEL_PCIE_ROM;
  645. err = wait_event_timeout(data->gp0_wait_q, data->gp0_received,
  646. msecs_to_jiffies(BTINTEL_DEFAULT_INTR_TIMEOUT_MS));
  647. if (!err)
  648. return -ETIME;
  649. /* Check cached boot stage is BTINTEL_PCIE_CSR_BOOT_STAGE_ROM(BIT(0)) */
  650. if (~data->boot_stage_cache & BTINTEL_PCIE_CSR_BOOT_STAGE_ROM)
  651. return -ENODEV;
  652. return 0;
  653. }
  654. static inline bool btintel_pcie_in_op(struct btintel_pcie_data *data)
  655. {
  656. return data->boot_stage_cache & BTINTEL_PCIE_CSR_BOOT_STAGE_OPFW;
  657. }
  658. static inline bool btintel_pcie_in_iml(struct btintel_pcie_data *data)
  659. {
  660. return data->boot_stage_cache & BTINTEL_PCIE_CSR_BOOT_STAGE_IML &&
  661. !(data->boot_stage_cache & BTINTEL_PCIE_CSR_BOOT_STAGE_OPFW);
  662. }
  663. static inline bool btintel_pcie_in_d3(struct btintel_pcie_data *data)
  664. {
  665. return data->boot_stage_cache & BTINTEL_PCIE_CSR_BOOT_STAGE_D3_STATE_READY;
  666. }
  667. static inline bool btintel_pcie_in_d0(struct btintel_pcie_data *data)
  668. {
  669. return !(data->boot_stage_cache & BTINTEL_PCIE_CSR_BOOT_STAGE_D3_STATE_READY);
  670. }
  671. static inline bool btintel_pcie_in_device_halt(struct btintel_pcie_data *data)
  672. {
  673. return data->boot_stage_cache & BTINTEL_PCIE_CSR_BOOT_STAGE_DEVICE_HALTED;
  674. }
  675. static void btintel_pcie_wr_sleep_cntrl(struct btintel_pcie_data *data,
  676. u32 dxstate)
  677. {
  678. bt_dev_dbg(data->hdev, "writing sleep_ctl_reg: 0x%8.8x", dxstate);
  679. btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_IPC_SLEEP_CTL_REG, dxstate);
  680. }
  681. static int btintel_pcie_read_device_mem(struct btintel_pcie_data *data,
  682. void *buf, u32 dev_addr, int len)
  683. {
  684. int err;
  685. u32 *val = buf;
  686. /* Get device mac access */
  687. err = btintel_pcie_get_mac_access(data);
  688. if (err) {
  689. bt_dev_err(data->hdev, "Failed to get mac access %d", err);
  690. return err;
  691. }
  692. for (; len > 0; len -= 4, dev_addr += 4, val++)
  693. *val = btintel_pcie_rd_dev_mem(data, dev_addr);
  694. btintel_pcie_release_mac_access(data);
  695. return 0;
  696. }
  697. static inline bool btintel_pcie_in_lockdown(struct btintel_pcie_data *data)
  698. {
  699. return (data->boot_stage_cache &
  700. BTINTEL_PCIE_CSR_BOOT_STAGE_ROM_LOCKDOWN) ||
  701. (data->boot_stage_cache &
  702. BTINTEL_PCIE_CSR_BOOT_STAGE_IML_LOCKDOWN);
  703. }
  704. static inline bool btintel_pcie_in_error(struct btintel_pcie_data *data)
  705. {
  706. return (data->boot_stage_cache & BTINTEL_PCIE_CSR_BOOT_STAGE_DEVICE_ERR) ||
  707. (data->boot_stage_cache & BTINTEL_PCIE_CSR_BOOT_STAGE_ABORT_HANDLER);
  708. }
  709. static void btintel_pcie_msix_gp1_handler(struct btintel_pcie_data *data)
  710. {
  711. bt_dev_err(data->hdev, "Received gp1 mailbox interrupt");
  712. btintel_pcie_dump_debug_registers(data->hdev);
  713. }
  714. /* This function handles the MSI-X interrupt for gp0 cause (bit 0 in
  715. * BTINTEL_PCIE_CSR_MSIX_HW_INT_CAUSES) which is sent for boot stage and image response.
  716. */
  717. static void btintel_pcie_msix_gp0_handler(struct btintel_pcie_data *data)
  718. {
  719. bool submit_rx, signal_waitq;
  720. u32 reg, old_ctxt;
  721. /* This interrupt is for three different causes and it is not easy to
  722. * know what causes the interrupt. So, it compares each register value
  723. * with cached value and update it before it wake up the queue.
  724. */
  725. reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_BOOT_STAGE_REG);
  726. if (reg != data->boot_stage_cache)
  727. data->boot_stage_cache = reg;
  728. bt_dev_dbg(data->hdev, "Alive context: %s old_boot_stage: 0x%8.8x new_boot_stage: 0x%8.8x",
  729. btintel_pcie_alivectxt_state2str(data->alive_intr_ctxt),
  730. data->boot_stage_cache, reg);
  731. reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_IMG_RESPONSE_REG);
  732. if (reg != data->img_resp_cache)
  733. data->img_resp_cache = reg;
  734. if (btintel_pcie_in_error(data)) {
  735. bt_dev_err(data->hdev, "Controller in error state");
  736. btintel_pcie_dump_debug_registers(data->hdev);
  737. return;
  738. }
  739. if (btintel_pcie_in_lockdown(data)) {
  740. bt_dev_err(data->hdev, "Controller in lockdown state");
  741. btintel_pcie_dump_debug_registers(data->hdev);
  742. return;
  743. }
  744. data->gp0_received = true;
  745. old_ctxt = data->alive_intr_ctxt;
  746. submit_rx = false;
  747. signal_waitq = false;
  748. switch (data->alive_intr_ctxt) {
  749. case BTINTEL_PCIE_ROM:
  750. data->alive_intr_ctxt = BTINTEL_PCIE_FW_DL;
  751. signal_waitq = true;
  752. break;
  753. case BTINTEL_PCIE_FW_DL:
  754. /* Error case is already handled. Ideally control shall not
  755. * reach here
  756. */
  757. break;
  758. case BTINTEL_PCIE_INTEL_HCI_RESET1:
  759. if (btintel_pcie_in_op(data)) {
  760. submit_rx = true;
  761. signal_waitq = true;
  762. break;
  763. }
  764. if (btintel_pcie_in_iml(data)) {
  765. submit_rx = true;
  766. signal_waitq = true;
  767. data->alive_intr_ctxt = BTINTEL_PCIE_FW_DL;
  768. break;
  769. }
  770. break;
  771. case BTINTEL_PCIE_INTEL_HCI_RESET2:
  772. if (btintel_test_and_clear_flag(data->hdev, INTEL_WAIT_FOR_D0)) {
  773. btintel_wake_up_flag(data->hdev, INTEL_WAIT_FOR_D0);
  774. data->alive_intr_ctxt = BTINTEL_PCIE_D0;
  775. }
  776. break;
  777. case BTINTEL_PCIE_D0:
  778. if (btintel_pcie_in_d3(data)) {
  779. data->alive_intr_ctxt = BTINTEL_PCIE_D3;
  780. signal_waitq = true;
  781. break;
  782. }
  783. break;
  784. case BTINTEL_PCIE_D3:
  785. if (btintel_pcie_in_d0(data)) {
  786. data->alive_intr_ctxt = BTINTEL_PCIE_D0;
  787. submit_rx = true;
  788. signal_waitq = true;
  789. break;
  790. }
  791. break;
  792. case BTINTEL_PCIE_HCI_RESET:
  793. data->alive_intr_ctxt = BTINTEL_PCIE_D0;
  794. submit_rx = true;
  795. signal_waitq = true;
  796. break;
  797. default:
  798. bt_dev_err(data->hdev, "Unknown state: 0x%2.2x",
  799. data->alive_intr_ctxt);
  800. break;
  801. }
  802. if (submit_rx) {
  803. btintel_pcie_reset_ia(data);
  804. btintel_pcie_start_rx(data);
  805. }
  806. if (signal_waitq) {
  807. bt_dev_dbg(data->hdev, "wake up gp0 wait_q");
  808. wake_up(&data->gp0_wait_q);
  809. }
  810. if (old_ctxt != data->alive_intr_ctxt)
  811. bt_dev_dbg(data->hdev, "alive context changed: %s -> %s",
  812. btintel_pcie_alivectxt_state2str(old_ctxt),
  813. btintel_pcie_alivectxt_state2str(data->alive_intr_ctxt));
  814. }
  815. /* This function handles the MSX-X interrupt for rx queue 0 which is for TX
  816. */
  817. static void btintel_pcie_msix_tx_handle(struct btintel_pcie_data *data)
  818. {
  819. u16 cr_tia, cr_hia;
  820. struct txq *txq;
  821. struct urbd0 *urbd0;
  822. cr_tia = data->ia.cr_tia[BTINTEL_PCIE_TXQ_NUM];
  823. cr_hia = data->ia.cr_hia[BTINTEL_PCIE_TXQ_NUM];
  824. if (cr_tia == cr_hia)
  825. return;
  826. txq = &data->txq;
  827. while (cr_tia != cr_hia) {
  828. data->tx_wait_done = true;
  829. wake_up(&data->tx_wait_q);
  830. urbd0 = &txq->urbd0s[cr_tia];
  831. if (urbd0->tfd_index > txq->count)
  832. return;
  833. cr_tia = (cr_tia + 1) % txq->count;
  834. data->ia.cr_tia[BTINTEL_PCIE_TXQ_NUM] = cr_tia;
  835. ipc_print_ia_ring(data->hdev, &data->ia, BTINTEL_PCIE_TXQ_NUM);
  836. }
  837. }
  838. static int btintel_pcie_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
  839. {
  840. struct hci_event_hdr *hdr = (void *)skb->data;
  841. struct btintel_pcie_data *data = hci_get_drvdata(hdev);
  842. if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
  843. hdr->plen > 0) {
  844. const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
  845. unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
  846. if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
  847. switch (skb->data[2]) {
  848. case 0x02:
  849. /* When switching to the operational firmware
  850. * the device sends a vendor specific event
  851. * indicating that the bootup completed.
  852. */
  853. btintel_bootup(hdev, ptr, len);
  854. /* If bootup event is from operational image,
  855. * driver needs to write sleep control register to
  856. * move into D0 state
  857. */
  858. if (btintel_pcie_in_op(data)) {
  859. btintel_pcie_wr_sleep_cntrl(data, BTINTEL_PCIE_STATE_D0);
  860. data->alive_intr_ctxt = BTINTEL_PCIE_INTEL_HCI_RESET2;
  861. kfree_skb(skb);
  862. return 0;
  863. }
  864. if (btintel_pcie_in_iml(data)) {
  865. /* In case of IML, there is no concept
  866. * of D0 transition. Just mimic as if
  867. * IML moved to D0 by clearing INTEL_WAIT_FOR_D0
  868. * bit and waking up the task waiting on
  869. * INTEL_WAIT_FOR_D0. This is required
  870. * as intel_boot() is common function for
  871. * both IML and OP image loading.
  872. */
  873. if (btintel_test_and_clear_flag(data->hdev,
  874. INTEL_WAIT_FOR_D0))
  875. btintel_wake_up_flag(data->hdev,
  876. INTEL_WAIT_FOR_D0);
  877. }
  878. kfree_skb(skb);
  879. return 0;
  880. case 0x06:
  881. /* When the firmware loading completes the
  882. * device sends out a vendor specific event
  883. * indicating the result of the firmware
  884. * loading.
  885. */
  886. btintel_secure_send_result(hdev, ptr, len);
  887. kfree_skb(skb);
  888. return 0;
  889. }
  890. }
  891. /* This is a debug event that comes from IML and OP image when it
  892. * starts execution. There is no need pass this event to stack.
  893. */
  894. if (skb->data[2] == 0x97) {
  895. hci_recv_diag(hdev, skb);
  896. return 0;
  897. }
  898. }
  899. return hci_recv_frame(hdev, skb);
  900. }
  901. /* Process the received rx data
  902. * It check the frame header to identify the data type and create skb
  903. * and calling HCI API
  904. */
  905. static int btintel_pcie_recv_frame(struct btintel_pcie_data *data,
  906. struct sk_buff *skb)
  907. {
  908. int ret;
  909. u8 pkt_type;
  910. u16 plen;
  911. u32 pcie_pkt_type;
  912. void *pdata;
  913. struct hci_dev *hdev = data->hdev;
  914. spin_lock(&data->hci_rx_lock);
  915. /* The first 4 bytes indicates the Intel PCIe specific packet type */
  916. pdata = skb_pull_data(skb, BTINTEL_PCIE_HCI_TYPE_LEN);
  917. if (!pdata) {
  918. bt_dev_err(hdev, "Corrupted packet received");
  919. ret = -EILSEQ;
  920. goto exit_error;
  921. }
  922. pcie_pkt_type = get_unaligned_le32(pdata);
  923. switch (pcie_pkt_type) {
  924. case BTINTEL_PCIE_HCI_ACL_PKT:
  925. if (skb->len >= HCI_ACL_HDR_SIZE) {
  926. plen = HCI_ACL_HDR_SIZE + __le16_to_cpu(hci_acl_hdr(skb)->dlen);
  927. pkt_type = HCI_ACLDATA_PKT;
  928. } else {
  929. bt_dev_err(hdev, "ACL packet is too short");
  930. ret = -EILSEQ;
  931. goto exit_error;
  932. }
  933. break;
  934. case BTINTEL_PCIE_HCI_SCO_PKT:
  935. if (skb->len >= HCI_SCO_HDR_SIZE) {
  936. plen = HCI_SCO_HDR_SIZE + hci_sco_hdr(skb)->dlen;
  937. pkt_type = HCI_SCODATA_PKT;
  938. } else {
  939. bt_dev_err(hdev, "SCO packet is too short");
  940. ret = -EILSEQ;
  941. goto exit_error;
  942. }
  943. break;
  944. case BTINTEL_PCIE_HCI_EVT_PKT:
  945. if (skb->len >= HCI_EVENT_HDR_SIZE) {
  946. plen = HCI_EVENT_HDR_SIZE + hci_event_hdr(skb)->plen;
  947. pkt_type = HCI_EVENT_PKT;
  948. } else {
  949. bt_dev_err(hdev, "Event packet is too short");
  950. ret = -EILSEQ;
  951. goto exit_error;
  952. }
  953. break;
  954. case BTINTEL_PCIE_HCI_ISO_PKT:
  955. if (skb->len >= HCI_ISO_HDR_SIZE) {
  956. plen = HCI_ISO_HDR_SIZE + __le16_to_cpu(hci_iso_hdr(skb)->dlen);
  957. pkt_type = HCI_ISODATA_PKT;
  958. } else {
  959. bt_dev_err(hdev, "ISO packet is too short");
  960. ret = -EILSEQ;
  961. goto exit_error;
  962. }
  963. break;
  964. default:
  965. bt_dev_err(hdev, "Invalid packet type received: 0x%4.4x",
  966. pcie_pkt_type);
  967. ret = -EINVAL;
  968. goto exit_error;
  969. }
  970. if (skb->len < plen) {
  971. bt_dev_err(hdev, "Received corrupted packet. type: 0x%2.2x",
  972. pkt_type);
  973. ret = -EILSEQ;
  974. goto exit_error;
  975. }
  976. bt_dev_dbg(hdev, "pkt_type: 0x%2.2x len: %u", pkt_type, plen);
  977. hci_skb_pkt_type(skb) = pkt_type;
  978. hdev->stat.byte_rx += plen;
  979. skb_trim(skb, plen);
  980. if (pcie_pkt_type == BTINTEL_PCIE_HCI_EVT_PKT)
  981. ret = btintel_pcie_recv_event(hdev, skb);
  982. else
  983. ret = hci_recv_frame(hdev, skb);
  984. skb = NULL; /* skb is freed in the callee */
  985. exit_error:
  986. kfree_skb(skb);
  987. if (ret)
  988. hdev->stat.err_rx++;
  989. spin_unlock(&data->hci_rx_lock);
  990. return ret;
  991. }
  992. static void btintel_pcie_read_hwexp(struct btintel_pcie_data *data)
  993. {
  994. int len, err, offset, pending;
  995. struct sk_buff *skb;
  996. u8 *buf, prefix[64];
  997. u32 addr, val;
  998. u16 pkt_len;
  999. struct tlv {
  1000. u8 type;
  1001. __le16 len;
  1002. u8 val[];
  1003. } __packed;
  1004. struct tlv *tlv;
  1005. switch (data->dmp_hdr.cnvi_top & 0xfff) {
  1006. case BTINTEL_CNVI_BLAZARI:
  1007. case BTINTEL_CNVI_BLAZARIW:
  1008. /* only from step B0 onwards */
  1009. if (INTEL_CNVX_TOP_STEP(data->dmp_hdr.cnvi_top) != 0x01)
  1010. return;
  1011. len = BTINTEL_PCIE_BLZR_HWEXP_SIZE; /* exception data length */
  1012. addr = BTINTEL_PCIE_BLZR_HWEXP_DMP_ADDR;
  1013. break;
  1014. case BTINTEL_CNVI_SCP:
  1015. len = BTINTEL_PCIE_SCP_HWEXP_SIZE;
  1016. addr = BTINTEL_PCIE_SCP_HWEXP_DMP_ADDR;
  1017. break;
  1018. default:
  1019. bt_dev_err(data->hdev, "Unsupported cnvi 0x%8.8x", data->dmp_hdr.cnvi_top);
  1020. return;
  1021. }
  1022. buf = kzalloc(len, GFP_KERNEL);
  1023. if (!buf)
  1024. goto exit_on_error;
  1025. btintel_pcie_mac_init(data);
  1026. err = btintel_pcie_read_device_mem(data, buf, addr, len);
  1027. if (err)
  1028. goto exit_on_error;
  1029. val = get_unaligned_le32(buf);
  1030. if (val != BTINTEL_PCIE_MAGIC_NUM) {
  1031. bt_dev_err(data->hdev, "Invalid exception dump signature: 0x%8.8x",
  1032. val);
  1033. goto exit_on_error;
  1034. }
  1035. snprintf(prefix, sizeof(prefix), "Bluetooth: %s: ", bt_dev_name(data->hdev));
  1036. offset = 4;
  1037. do {
  1038. pending = len - offset;
  1039. if (pending < sizeof(*tlv))
  1040. break;
  1041. tlv = (struct tlv *)(buf + offset);
  1042. /* If type == 0, then there are no more TLVs to be parsed */
  1043. if (!tlv->type) {
  1044. bt_dev_dbg(data->hdev, "Invalid TLV type 0");
  1045. break;
  1046. }
  1047. pkt_len = le16_to_cpu(tlv->len);
  1048. offset += sizeof(*tlv);
  1049. pending = len - offset;
  1050. if (pkt_len > pending)
  1051. break;
  1052. offset += pkt_len;
  1053. /* Only TLVs of type == 1 are HCI events, no need to process other
  1054. * TLVs
  1055. */
  1056. if (tlv->type != 1)
  1057. continue;
  1058. bt_dev_dbg(data->hdev, "TLV packet length: %u", pkt_len);
  1059. if (pkt_len > HCI_MAX_EVENT_SIZE)
  1060. break;
  1061. skb = bt_skb_alloc(pkt_len, GFP_KERNEL);
  1062. if (!skb)
  1063. goto exit_on_error;
  1064. hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
  1065. skb_put_data(skb, tlv->val, pkt_len);
  1066. /* copy Intel specific pcie packet type */
  1067. val = BTINTEL_PCIE_HCI_EVT_PKT;
  1068. memcpy(skb_push(skb, BTINTEL_PCIE_HCI_TYPE_LEN), &val,
  1069. BTINTEL_PCIE_HCI_TYPE_LEN);
  1070. print_hex_dump(KERN_DEBUG, prefix, DUMP_PREFIX_OFFSET, 16, 1,
  1071. tlv->val, pkt_len, false);
  1072. btintel_pcie_recv_frame(data, skb);
  1073. } while (offset < len);
  1074. exit_on_error:
  1075. kfree(buf);
  1076. }
  1077. static void btintel_pcie_msix_hw_exp_handler(struct btintel_pcie_data *data)
  1078. {
  1079. bt_dev_err(data->hdev, "Received hw exception interrupt");
  1080. if (test_and_set_bit(BTINTEL_PCIE_CORE_HALTED, &data->flags))
  1081. return;
  1082. if (test_and_set_bit(BTINTEL_PCIE_HWEXP_INPROGRESS, &data->flags))
  1083. return;
  1084. /* Trigger device core dump when there is HW exception */
  1085. if (!test_and_set_bit(BTINTEL_PCIE_COREDUMP_INPROGRESS, &data->flags))
  1086. data->dmp_hdr.trigger_reason = BTINTEL_PCIE_TRIGGER_REASON_FW_ASSERT;
  1087. queue_work(data->workqueue, &data->rx_work);
  1088. }
  1089. static void btintel_pcie_rx_work(struct work_struct *work)
  1090. {
  1091. struct btintel_pcie_data *data = container_of(work,
  1092. struct btintel_pcie_data, rx_work);
  1093. struct sk_buff *skb;
  1094. if (test_bit(BTINTEL_PCIE_COREDUMP_INPROGRESS, &data->flags)) {
  1095. btintel_pcie_dump_traces(data->hdev);
  1096. clear_bit(BTINTEL_PCIE_COREDUMP_INPROGRESS, &data->flags);
  1097. }
  1098. if (test_bit(BTINTEL_PCIE_HWEXP_INPROGRESS, &data->flags)) {
  1099. /* Unlike usb products, controller will not send hardware
  1100. * exception event on exception. Instead controller writes the
  1101. * hardware event to device memory along with optional debug
  1102. * events, raises MSIX and halts. Driver shall read the
  1103. * exception event from device memory and passes it stack for
  1104. * further processing.
  1105. */
  1106. btintel_pcie_read_hwexp(data);
  1107. clear_bit(BTINTEL_PCIE_HWEXP_INPROGRESS, &data->flags);
  1108. }
  1109. /* Process the sk_buf in queue and send to the HCI layer */
  1110. while ((skb = skb_dequeue(&data->rx_skb_q))) {
  1111. btintel_pcie_recv_frame(data, skb);
  1112. }
  1113. }
  1114. /* create sk_buff with data and save it to queue and start RX work */
  1115. static int btintel_pcie_submit_rx_work(struct btintel_pcie_data *data, u8 status,
  1116. void *buf)
  1117. {
  1118. int ret, len;
  1119. struct rfh_hdr *rfh_hdr;
  1120. struct sk_buff *skb;
  1121. rfh_hdr = buf;
  1122. len = rfh_hdr->packet_len;
  1123. if (len <= 0) {
  1124. ret = -EINVAL;
  1125. goto resubmit;
  1126. }
  1127. /* Remove RFH header */
  1128. buf += sizeof(*rfh_hdr);
  1129. skb = alloc_skb(len, GFP_ATOMIC);
  1130. if (!skb)
  1131. goto resubmit;
  1132. skb_put_data(skb, buf, len);
  1133. skb_queue_tail(&data->rx_skb_q, skb);
  1134. queue_work(data->workqueue, &data->rx_work);
  1135. resubmit:
  1136. ret = btintel_pcie_submit_rx(data);
  1137. return ret;
  1138. }
  1139. /* Handles the MSI-X interrupt for rx queue 1 which is for RX */
  1140. static void btintel_pcie_msix_rx_handle(struct btintel_pcie_data *data)
  1141. {
  1142. u16 cr_hia, cr_tia;
  1143. struct rxq *rxq;
  1144. struct urbd1 *urbd1;
  1145. struct data_buf *buf;
  1146. int ret;
  1147. struct hci_dev *hdev = data->hdev;
  1148. cr_hia = data->ia.cr_hia[BTINTEL_PCIE_RXQ_NUM];
  1149. cr_tia = data->ia.cr_tia[BTINTEL_PCIE_RXQ_NUM];
  1150. bt_dev_dbg(hdev, "RXQ: cr_hia: %u cr_tia: %u", cr_hia, cr_tia);
  1151. /* Check CR_TIA and CR_HIA for change */
  1152. if (cr_tia == cr_hia)
  1153. return;
  1154. rxq = &data->rxq;
  1155. /* The firmware sends multiple CD in a single MSI-X and it needs to
  1156. * process all received CDs in this interrupt.
  1157. */
  1158. while (cr_tia != cr_hia) {
  1159. urbd1 = &rxq->urbd1s[cr_tia];
  1160. ipc_print_urbd1(data->hdev, urbd1, cr_tia);
  1161. buf = &rxq->bufs[urbd1->frbd_tag];
  1162. if (!buf) {
  1163. bt_dev_err(hdev, "RXQ: failed to get the DMA buffer for %d",
  1164. urbd1->frbd_tag);
  1165. return;
  1166. }
  1167. ret = btintel_pcie_submit_rx_work(data, urbd1->status,
  1168. buf->data);
  1169. if (ret) {
  1170. bt_dev_err(hdev, "RXQ: failed to submit rx request");
  1171. return;
  1172. }
  1173. cr_tia = (cr_tia + 1) % rxq->count;
  1174. data->ia.cr_tia[BTINTEL_PCIE_RXQ_NUM] = cr_tia;
  1175. ipc_print_ia_ring(data->hdev, &data->ia, BTINTEL_PCIE_RXQ_NUM);
  1176. }
  1177. }
  1178. static inline bool btintel_pcie_is_rxq_empty(struct btintel_pcie_data *data)
  1179. {
  1180. return data->ia.cr_hia[BTINTEL_PCIE_RXQ_NUM] == data->ia.cr_tia[BTINTEL_PCIE_RXQ_NUM];
  1181. }
  1182. static inline bool btintel_pcie_is_txackq_empty(struct btintel_pcie_data *data)
  1183. {
  1184. return data->ia.cr_tia[BTINTEL_PCIE_TXQ_NUM] == data->ia.cr_hia[BTINTEL_PCIE_TXQ_NUM];
  1185. }
  1186. static irqreturn_t btintel_pcie_irq_msix_handler(int irq, void *dev_id)
  1187. {
  1188. struct msix_entry *entry = dev_id;
  1189. struct btintel_pcie_data *data = btintel_pcie_get_data(entry);
  1190. u32 intr_fh, intr_hw;
  1191. spin_lock(&data->irq_lock);
  1192. intr_fh = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_MSIX_FH_INT_CAUSES);
  1193. intr_hw = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_MSIX_HW_INT_CAUSES);
  1194. /* Clear causes registers to avoid being handling the same cause */
  1195. btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_MSIX_FH_INT_CAUSES, intr_fh);
  1196. btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_MSIX_HW_INT_CAUSES, intr_hw);
  1197. spin_unlock(&data->irq_lock);
  1198. if (unlikely(!(intr_fh | intr_hw))) {
  1199. /* Ignore interrupt, inta == 0 */
  1200. return IRQ_NONE;
  1201. }
  1202. /* This interrupt is raised when there is an hardware exception */
  1203. if (intr_hw & BTINTEL_PCIE_MSIX_HW_INT_CAUSES_HWEXP)
  1204. btintel_pcie_msix_hw_exp_handler(data);
  1205. if (intr_hw & BTINTEL_PCIE_MSIX_HW_INT_CAUSES_GP1)
  1206. btintel_pcie_msix_gp1_handler(data);
  1207. /* For TX */
  1208. if (intr_fh & BTINTEL_PCIE_MSIX_FH_INT_CAUSES_0) {
  1209. btintel_pcie_msix_tx_handle(data);
  1210. if (!btintel_pcie_is_rxq_empty(data))
  1211. btintel_pcie_msix_rx_handle(data);
  1212. }
  1213. /* For RX */
  1214. if (intr_fh & BTINTEL_PCIE_MSIX_FH_INT_CAUSES_1) {
  1215. btintel_pcie_msix_rx_handle(data);
  1216. if (!btintel_pcie_is_txackq_empty(data))
  1217. btintel_pcie_msix_tx_handle(data);
  1218. }
  1219. /* This interrupt is triggered by the firmware after updating
  1220. * boot_stage register and image_response register
  1221. */
  1222. if (intr_hw & BTINTEL_PCIE_MSIX_HW_INT_CAUSES_GP0)
  1223. btintel_pcie_msix_gp0_handler(data);
  1224. /*
  1225. * Before sending the interrupt the HW disables it to prevent a nested
  1226. * interrupt. This is done by writing 1 to the corresponding bit in
  1227. * the mask register. After handling the interrupt, it should be
  1228. * re-enabled by clearing this bit. This register is defined as write 1
  1229. * clear (W1C) register, meaning that it's cleared by writing 1
  1230. * to the bit.
  1231. */
  1232. btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_MSIX_AUTOMASK_ST,
  1233. BIT(entry->entry));
  1234. return IRQ_HANDLED;
  1235. }
  1236. /* This function requests the irq for MSI-X and registers the handlers per irq.
  1237. * Currently, it requests only 1 irq for all interrupt causes.
  1238. */
  1239. static int btintel_pcie_setup_irq(struct btintel_pcie_data *data)
  1240. {
  1241. int err;
  1242. int num_irqs, i;
  1243. for (i = 0; i < BTINTEL_PCIE_MSIX_VEC_MAX; i++)
  1244. data->msix_entries[i].entry = i;
  1245. num_irqs = pci_alloc_irq_vectors(data->pdev, BTINTEL_PCIE_MSIX_VEC_MIN,
  1246. BTINTEL_PCIE_MSIX_VEC_MAX, PCI_IRQ_MSIX);
  1247. if (num_irqs < 0)
  1248. return num_irqs;
  1249. data->alloc_vecs = num_irqs;
  1250. data->msix_enabled = 1;
  1251. data->def_irq = 0;
  1252. /* setup irq handler */
  1253. for (i = 0; i < data->alloc_vecs; i++) {
  1254. struct msix_entry *msix_entry;
  1255. msix_entry = &data->msix_entries[i];
  1256. msix_entry->vector = pci_irq_vector(data->pdev, i);
  1257. err = devm_request_threaded_irq(&data->pdev->dev,
  1258. msix_entry->vector,
  1259. NULL,
  1260. btintel_pcie_irq_msix_handler,
  1261. IRQF_ONESHOT | IRQF_SHARED,
  1262. KBUILD_MODNAME,
  1263. msix_entry);
  1264. if (err) {
  1265. pci_free_irq_vectors(data->pdev);
  1266. data->alloc_vecs = 0;
  1267. return err;
  1268. }
  1269. }
  1270. return 0;
  1271. }
  1272. struct btintel_pcie_causes_list {
  1273. u32 cause;
  1274. u32 mask_reg;
  1275. u8 cause_num;
  1276. };
  1277. static struct btintel_pcie_causes_list causes_list[] = {
  1278. { BTINTEL_PCIE_MSIX_FH_INT_CAUSES_0, BTINTEL_PCIE_CSR_MSIX_FH_INT_MASK, 0x00 },
  1279. { BTINTEL_PCIE_MSIX_FH_INT_CAUSES_1, BTINTEL_PCIE_CSR_MSIX_FH_INT_MASK, 0x01 },
  1280. { BTINTEL_PCIE_MSIX_HW_INT_CAUSES_GP0, BTINTEL_PCIE_CSR_MSIX_HW_INT_MASK, 0x20 },
  1281. { BTINTEL_PCIE_MSIX_HW_INT_CAUSES_HWEXP, BTINTEL_PCIE_CSR_MSIX_HW_INT_MASK, 0x23 },
  1282. };
  1283. /* This function configures the interrupt masks for both HW_INT_CAUSES and
  1284. * FH_INT_CAUSES which are meaningful to us.
  1285. *
  1286. * After resetting BT function via PCIE FLR or FUNC_CTRL reset, the driver
  1287. * need to call this function again to configure since the masks
  1288. * are reset to 0xFFFFFFFF after reset.
  1289. */
  1290. static void btintel_pcie_config_msix(struct btintel_pcie_data *data)
  1291. {
  1292. int i;
  1293. int val = data->def_irq | BTINTEL_PCIE_MSIX_NON_AUTO_CLEAR_CAUSE;
  1294. /* Set Non Auto Clear Cause */
  1295. for (i = 0; i < ARRAY_SIZE(causes_list); i++) {
  1296. btintel_pcie_wr_reg8(data,
  1297. BTINTEL_PCIE_CSR_MSIX_IVAR(causes_list[i].cause_num),
  1298. val);
  1299. btintel_pcie_clr_reg_bits(data,
  1300. causes_list[i].mask_reg,
  1301. causes_list[i].cause);
  1302. }
  1303. /* Save the initial interrupt mask */
  1304. data->fh_init_mask = ~btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_MSIX_FH_INT_MASK);
  1305. data->hw_init_mask = ~btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_MSIX_HW_INT_MASK);
  1306. }
  1307. static int btintel_pcie_config_pcie(struct pci_dev *pdev,
  1308. struct btintel_pcie_data *data)
  1309. {
  1310. int err;
  1311. err = pcim_enable_device(pdev);
  1312. if (err)
  1313. return err;
  1314. pci_set_master(pdev);
  1315. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  1316. if (err) {
  1317. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  1318. if (err)
  1319. return err;
  1320. }
  1321. data->base_addr = pcim_iomap_region(pdev, 0, KBUILD_MODNAME);
  1322. if (IS_ERR(data->base_addr))
  1323. return PTR_ERR(data->base_addr);
  1324. err = btintel_pcie_setup_irq(data);
  1325. if (err)
  1326. return err;
  1327. /* Configure MSI-X with causes list */
  1328. btintel_pcie_config_msix(data);
  1329. return 0;
  1330. }
  1331. static void btintel_pcie_init_ci(struct btintel_pcie_data *data,
  1332. struct ctx_info *ci)
  1333. {
  1334. ci->version = 0x1;
  1335. ci->size = sizeof(*ci);
  1336. ci->config = 0x0000;
  1337. ci->addr_cr_hia = data->ia.cr_hia_p_addr;
  1338. ci->addr_tr_tia = data->ia.tr_tia_p_addr;
  1339. ci->addr_cr_tia = data->ia.cr_tia_p_addr;
  1340. ci->addr_tr_hia = data->ia.tr_hia_p_addr;
  1341. ci->num_cr_ia = BTINTEL_PCIE_NUM_QUEUES;
  1342. ci->num_tr_ia = BTINTEL_PCIE_NUM_QUEUES;
  1343. ci->addr_urbdq0 = data->txq.urbd0s_p_addr;
  1344. ci->addr_tfdq = data->txq.tfds_p_addr;
  1345. ci->num_tfdq = data->txq.count;
  1346. ci->num_urbdq0 = data->txq.count;
  1347. ci->tfdq_db_vec = BTINTEL_PCIE_TXQ_NUM;
  1348. ci->urbdq0_db_vec = BTINTEL_PCIE_TXQ_NUM;
  1349. ci->rbd_size = BTINTEL_PCIE_RBD_SIZE_4K;
  1350. ci->addr_frbdq = data->rxq.frbds_p_addr;
  1351. ci->num_frbdq = data->rxq.count;
  1352. ci->frbdq_db_vec = BTINTEL_PCIE_RXQ_NUM;
  1353. ci->addr_urbdq1 = data->rxq.urbd1s_p_addr;
  1354. ci->num_urbdq1 = data->rxq.count;
  1355. ci->urbdq_db_vec = BTINTEL_PCIE_RXQ_NUM;
  1356. ci->dbg_output_mode = 0x01;
  1357. ci->dbgc_addr = data->dbgc.frag_p_addr;
  1358. ci->dbgc_size = data->dbgc.frag_size;
  1359. ci->dbg_preset = 0x00;
  1360. }
  1361. static void btintel_pcie_free_txq_bufs(struct btintel_pcie_data *data,
  1362. struct txq *txq)
  1363. {
  1364. /* Free data buffers first */
  1365. dma_free_coherent(&data->pdev->dev, txq->count * BTINTEL_PCIE_BUFFER_SIZE,
  1366. txq->buf_v_addr, txq->buf_p_addr);
  1367. kfree(txq->bufs);
  1368. }
  1369. static int btintel_pcie_setup_txq_bufs(struct btintel_pcie_data *data,
  1370. struct txq *txq)
  1371. {
  1372. int i;
  1373. struct data_buf *buf;
  1374. /* Allocate the same number of buffers as the descriptor */
  1375. txq->bufs = kmalloc_objs(*buf, txq->count);
  1376. if (!txq->bufs)
  1377. return -ENOMEM;
  1378. /* Allocate full chunk of data buffer for DMA first and do indexing and
  1379. * initialization next, so it can be freed easily
  1380. */
  1381. txq->buf_v_addr = dma_alloc_coherent(&data->pdev->dev,
  1382. txq->count * BTINTEL_PCIE_BUFFER_SIZE,
  1383. &txq->buf_p_addr,
  1384. GFP_KERNEL | __GFP_NOWARN);
  1385. if (!txq->buf_v_addr) {
  1386. kfree(txq->bufs);
  1387. return -ENOMEM;
  1388. }
  1389. /* Setup the allocated DMA buffer to bufs. Each data_buf should
  1390. * have virtual address and physical address
  1391. */
  1392. for (i = 0; i < txq->count; i++) {
  1393. buf = &txq->bufs[i];
  1394. buf->data_p_addr = txq->buf_p_addr + (i * BTINTEL_PCIE_BUFFER_SIZE);
  1395. buf->data = txq->buf_v_addr + (i * BTINTEL_PCIE_BUFFER_SIZE);
  1396. }
  1397. return 0;
  1398. }
  1399. static void btintel_pcie_free_rxq_bufs(struct btintel_pcie_data *data,
  1400. struct rxq *rxq)
  1401. {
  1402. /* Free data buffers first */
  1403. dma_free_coherent(&data->pdev->dev, rxq->count * BTINTEL_PCIE_BUFFER_SIZE,
  1404. rxq->buf_v_addr, rxq->buf_p_addr);
  1405. kfree(rxq->bufs);
  1406. }
  1407. static int btintel_pcie_setup_rxq_bufs(struct btintel_pcie_data *data,
  1408. struct rxq *rxq)
  1409. {
  1410. int i;
  1411. struct data_buf *buf;
  1412. /* Allocate the same number of buffers as the descriptor */
  1413. rxq->bufs = kmalloc_objs(*buf, rxq->count);
  1414. if (!rxq->bufs)
  1415. return -ENOMEM;
  1416. /* Allocate full chunk of data buffer for DMA first and do indexing and
  1417. * initialization next, so it can be freed easily
  1418. */
  1419. rxq->buf_v_addr = dma_alloc_coherent(&data->pdev->dev,
  1420. rxq->count * BTINTEL_PCIE_BUFFER_SIZE,
  1421. &rxq->buf_p_addr,
  1422. GFP_KERNEL | __GFP_NOWARN);
  1423. if (!rxq->buf_v_addr) {
  1424. kfree(rxq->bufs);
  1425. return -ENOMEM;
  1426. }
  1427. /* Setup the allocated DMA buffer to bufs. Each data_buf should
  1428. * have virtual address and physical address
  1429. */
  1430. for (i = 0; i < rxq->count; i++) {
  1431. buf = &rxq->bufs[i];
  1432. buf->data_p_addr = rxq->buf_p_addr + (i * BTINTEL_PCIE_BUFFER_SIZE);
  1433. buf->data = rxq->buf_v_addr + (i * BTINTEL_PCIE_BUFFER_SIZE);
  1434. }
  1435. return 0;
  1436. }
  1437. static void btintel_pcie_setup_ia(struct btintel_pcie_data *data,
  1438. dma_addr_t p_addr, void *v_addr,
  1439. struct ia *ia)
  1440. {
  1441. /* TR Head Index Array */
  1442. ia->tr_hia_p_addr = p_addr;
  1443. ia->tr_hia = v_addr;
  1444. /* TR Tail Index Array */
  1445. ia->tr_tia_p_addr = p_addr + sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES;
  1446. ia->tr_tia = v_addr + sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES;
  1447. /* CR Head index Array */
  1448. ia->cr_hia_p_addr = p_addr + (sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES * 2);
  1449. ia->cr_hia = v_addr + (sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES * 2);
  1450. /* CR Tail Index Array */
  1451. ia->cr_tia_p_addr = p_addr + (sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES * 3);
  1452. ia->cr_tia = v_addr + (sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES * 3);
  1453. }
  1454. static void btintel_pcie_free(struct btintel_pcie_data *data)
  1455. {
  1456. btintel_pcie_free_rxq_bufs(data, &data->rxq);
  1457. btintel_pcie_free_txq_bufs(data, &data->txq);
  1458. dma_pool_free(data->dma_pool, data->dma_v_addr, data->dma_p_addr);
  1459. dma_pool_destroy(data->dma_pool);
  1460. }
  1461. /* Allocate tx and rx queues, any related data structures and buffers.
  1462. */
  1463. static int btintel_pcie_alloc(struct btintel_pcie_data *data)
  1464. {
  1465. int err = 0;
  1466. size_t total;
  1467. dma_addr_t p_addr;
  1468. void *v_addr;
  1469. /* Allocate the chunk of DMA memory for descriptors, index array, and
  1470. * context information, instead of allocating individually.
  1471. * The DMA memory for data buffer is allocated while setting up the
  1472. * each queue.
  1473. *
  1474. * Total size is sum of the following
  1475. * + size of TFD * Number of descriptors in queue
  1476. * + size of URBD0 * Number of descriptors in queue
  1477. * + size of FRBD * Number of descriptors in queue
  1478. * + size of URBD1 * Number of descriptors in queue
  1479. * + size of index * Number of queues(2) * type of index array(4)
  1480. * + size of context information
  1481. */
  1482. total = (sizeof(struct tfd) + sizeof(struct urbd0)) * BTINTEL_PCIE_TX_DESCS_COUNT;
  1483. total += (sizeof(struct frbd) + sizeof(struct urbd1)) * BTINTEL_PCIE_RX_DESCS_COUNT;
  1484. /* Add the sum of size of index array and size of ci struct */
  1485. total += (sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES * 4) + sizeof(struct ctx_info);
  1486. /* Allocate DMA Pool */
  1487. data->dma_pool = dma_pool_create(KBUILD_MODNAME, &data->pdev->dev,
  1488. total, BTINTEL_PCIE_DMA_POOL_ALIGNMENT, 0);
  1489. if (!data->dma_pool) {
  1490. err = -ENOMEM;
  1491. goto exit_error;
  1492. }
  1493. v_addr = dma_pool_zalloc(data->dma_pool, GFP_KERNEL | __GFP_NOWARN,
  1494. &p_addr);
  1495. if (!v_addr) {
  1496. dma_pool_destroy(data->dma_pool);
  1497. err = -ENOMEM;
  1498. goto exit_error;
  1499. }
  1500. data->dma_p_addr = p_addr;
  1501. data->dma_v_addr = v_addr;
  1502. /* Setup descriptor count */
  1503. data->txq.count = BTINTEL_PCIE_TX_DESCS_COUNT;
  1504. data->rxq.count = BTINTEL_PCIE_RX_DESCS_COUNT;
  1505. /* Setup tfds */
  1506. data->txq.tfds_p_addr = p_addr;
  1507. data->txq.tfds = v_addr;
  1508. p_addr += (sizeof(struct tfd) * BTINTEL_PCIE_TX_DESCS_COUNT);
  1509. v_addr += (sizeof(struct tfd) * BTINTEL_PCIE_TX_DESCS_COUNT);
  1510. /* Setup urbd0 */
  1511. data->txq.urbd0s_p_addr = p_addr;
  1512. data->txq.urbd0s = v_addr;
  1513. p_addr += (sizeof(struct urbd0) * BTINTEL_PCIE_TX_DESCS_COUNT);
  1514. v_addr += (sizeof(struct urbd0) * BTINTEL_PCIE_TX_DESCS_COUNT);
  1515. /* Setup FRBD*/
  1516. data->rxq.frbds_p_addr = p_addr;
  1517. data->rxq.frbds = v_addr;
  1518. p_addr += (sizeof(struct frbd) * BTINTEL_PCIE_RX_DESCS_COUNT);
  1519. v_addr += (sizeof(struct frbd) * BTINTEL_PCIE_RX_DESCS_COUNT);
  1520. /* Setup urbd1 */
  1521. data->rxq.urbd1s_p_addr = p_addr;
  1522. data->rxq.urbd1s = v_addr;
  1523. p_addr += (sizeof(struct urbd1) * BTINTEL_PCIE_RX_DESCS_COUNT);
  1524. v_addr += (sizeof(struct urbd1) * BTINTEL_PCIE_RX_DESCS_COUNT);
  1525. /* Setup data buffers for txq */
  1526. err = btintel_pcie_setup_txq_bufs(data, &data->txq);
  1527. if (err)
  1528. goto exit_error_pool;
  1529. /* Setup data buffers for rxq */
  1530. err = btintel_pcie_setup_rxq_bufs(data, &data->rxq);
  1531. if (err)
  1532. goto exit_error_txq;
  1533. /* Setup Index Array */
  1534. btintel_pcie_setup_ia(data, p_addr, v_addr, &data->ia);
  1535. /* Setup data buffers for dbgc */
  1536. err = btintel_pcie_setup_dbgc(data);
  1537. if (err)
  1538. goto exit_error_txq;
  1539. /* Setup Context Information */
  1540. p_addr += sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES * 4;
  1541. v_addr += sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES * 4;
  1542. data->ci = v_addr;
  1543. data->ci_p_addr = p_addr;
  1544. /* Initialize the CI */
  1545. btintel_pcie_init_ci(data, data->ci);
  1546. return 0;
  1547. exit_error_txq:
  1548. btintel_pcie_free_txq_bufs(data, &data->txq);
  1549. exit_error_pool:
  1550. dma_pool_free(data->dma_pool, data->dma_v_addr, data->dma_p_addr);
  1551. dma_pool_destroy(data->dma_pool);
  1552. exit_error:
  1553. return err;
  1554. }
  1555. static int btintel_pcie_open(struct hci_dev *hdev)
  1556. {
  1557. bt_dev_dbg(hdev, "");
  1558. return 0;
  1559. }
  1560. static int btintel_pcie_close(struct hci_dev *hdev)
  1561. {
  1562. bt_dev_dbg(hdev, "");
  1563. return 0;
  1564. }
  1565. static int btintel_pcie_inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
  1566. {
  1567. struct sk_buff *skb;
  1568. struct hci_event_hdr *hdr;
  1569. struct hci_ev_cmd_complete *evt;
  1570. skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
  1571. if (!skb)
  1572. return -ENOMEM;
  1573. hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
  1574. hdr->evt = HCI_EV_CMD_COMPLETE;
  1575. hdr->plen = sizeof(*evt) + 1;
  1576. evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
  1577. evt->ncmd = 0x01;
  1578. evt->opcode = cpu_to_le16(opcode);
  1579. *(u8 *)skb_put(skb, 1) = 0x00;
  1580. hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
  1581. return hci_recv_frame(hdev, skb);
  1582. }
  1583. static int btintel_pcie_send_frame(struct hci_dev *hdev,
  1584. struct sk_buff *skb)
  1585. {
  1586. struct btintel_pcie_data *data = hci_get_drvdata(hdev);
  1587. struct hci_command_hdr *cmd;
  1588. __u16 opcode = ~0;
  1589. int ret;
  1590. u32 type;
  1591. if (test_bit(BTINTEL_PCIE_CORE_HALTED, &data->flags))
  1592. return -ENODEV;
  1593. /* Due to the fw limitation, the type header of the packet should be
  1594. * 4 bytes unlike 1 byte for UART. In UART, the firmware can read
  1595. * the first byte to get the packet type and redirect the rest of data
  1596. * packet to the right handler.
  1597. *
  1598. * But for PCIe, THF(Transfer Flow Handler) fetches the 4 bytes of data
  1599. * from DMA memory and by the time it reads the first 4 bytes, it has
  1600. * already consumed some part of packet. Thus the packet type indicator
  1601. * for iBT PCIe is 4 bytes.
  1602. *
  1603. * Luckily, when HCI core creates the skb, it allocates 8 bytes of
  1604. * head room for profile and driver use, and before sending the data
  1605. * to the device, append the iBT PCIe packet type in the front.
  1606. */
  1607. switch (hci_skb_pkt_type(skb)) {
  1608. case HCI_COMMAND_PKT:
  1609. type = BTINTEL_PCIE_HCI_CMD_PKT;
  1610. cmd = (void *)skb->data;
  1611. opcode = le16_to_cpu(cmd->opcode);
  1612. if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
  1613. struct hci_command_hdr *cmd = (void *)skb->data;
  1614. __u16 opcode = le16_to_cpu(cmd->opcode);
  1615. /* When the BTINTEL_HCI_OP_RESET command is issued to
  1616. * boot into the operational firmware, it will actually
  1617. * not send a command complete event. To keep the flow
  1618. * control working inject that event here.
  1619. */
  1620. if (opcode == BTINTEL_HCI_OP_RESET)
  1621. btintel_pcie_inject_cmd_complete(hdev, opcode);
  1622. }
  1623. hdev->stat.cmd_tx++;
  1624. break;
  1625. case HCI_ACLDATA_PKT:
  1626. type = BTINTEL_PCIE_HCI_ACL_PKT;
  1627. hdev->stat.acl_tx++;
  1628. break;
  1629. case HCI_SCODATA_PKT:
  1630. type = BTINTEL_PCIE_HCI_SCO_PKT;
  1631. hdev->stat.sco_tx++;
  1632. break;
  1633. case HCI_ISODATA_PKT:
  1634. type = BTINTEL_PCIE_HCI_ISO_PKT;
  1635. break;
  1636. default:
  1637. bt_dev_err(hdev, "Unknown HCI packet type");
  1638. return -EILSEQ;
  1639. }
  1640. ret = btintel_pcie_send_sync(data, skb, type, opcode);
  1641. if (ret) {
  1642. hdev->stat.err_tx++;
  1643. bt_dev_err(hdev, "Failed to send frame (%d)", ret);
  1644. goto exit_error;
  1645. }
  1646. hdev->stat.byte_tx += skb->len;
  1647. kfree_skb(skb);
  1648. exit_error:
  1649. return ret;
  1650. }
  1651. static void btintel_pcie_release_hdev(struct btintel_pcie_data *data)
  1652. {
  1653. struct hci_dev *hdev;
  1654. hdev = data->hdev;
  1655. hci_unregister_dev(hdev);
  1656. hci_free_dev(hdev);
  1657. data->hdev = NULL;
  1658. }
  1659. static void btintel_pcie_disable_interrupts(struct btintel_pcie_data *data)
  1660. {
  1661. spin_lock(&data->irq_lock);
  1662. btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_MSIX_FH_INT_MASK, data->fh_init_mask);
  1663. btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_MSIX_HW_INT_MASK, data->hw_init_mask);
  1664. spin_unlock(&data->irq_lock);
  1665. }
  1666. static void btintel_pcie_enable_interrupts(struct btintel_pcie_data *data)
  1667. {
  1668. spin_lock(&data->irq_lock);
  1669. btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_MSIX_FH_INT_MASK, ~data->fh_init_mask);
  1670. btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_MSIX_HW_INT_MASK, ~data->hw_init_mask);
  1671. spin_unlock(&data->irq_lock);
  1672. }
  1673. static void btintel_pcie_synchronize_irqs(struct btintel_pcie_data *data)
  1674. {
  1675. for (int i = 0; i < data->alloc_vecs; i++)
  1676. synchronize_irq(data->msix_entries[i].vector);
  1677. }
  1678. static int btintel_pcie_setup_internal(struct hci_dev *hdev)
  1679. {
  1680. struct btintel_pcie_data *data = hci_get_drvdata(hdev);
  1681. const u8 param[1] = { 0xFF };
  1682. struct intel_version_tlv ver_tlv;
  1683. struct sk_buff *skb;
  1684. int err;
  1685. BT_DBG("%s", hdev->name);
  1686. skb = __hci_cmd_sync(hdev, 0xfc05, 1, param, HCI_CMD_TIMEOUT);
  1687. if (IS_ERR(skb)) {
  1688. bt_dev_err(hdev, "Reading Intel version command failed (%ld)",
  1689. PTR_ERR(skb));
  1690. return PTR_ERR(skb);
  1691. }
  1692. /* Check the status */
  1693. if (skb->data[0]) {
  1694. bt_dev_err(hdev, "Intel Read Version command failed (%02x)",
  1695. skb->data[0]);
  1696. err = -EIO;
  1697. goto exit_error;
  1698. }
  1699. /* Apply the common HCI quirks for Intel device */
  1700. hci_set_quirk(hdev, HCI_QUIRK_STRICT_DUPLICATE_FILTER);
  1701. hci_set_quirk(hdev, HCI_QUIRK_SIMULTANEOUS_DISCOVERY);
  1702. hci_set_quirk(hdev, HCI_QUIRK_NON_PERSISTENT_DIAG);
  1703. /* Set up the quality report callback for Intel devices */
  1704. hdev->set_quality_report = btintel_set_quality_report;
  1705. memset(&ver_tlv, 0, sizeof(ver_tlv));
  1706. /* For TLV type device, parse the tlv data */
  1707. err = btintel_parse_version_tlv(hdev, &ver_tlv, skb);
  1708. if (err) {
  1709. bt_dev_err(hdev, "Failed to parse TLV version information");
  1710. goto exit_error;
  1711. }
  1712. switch (INTEL_HW_PLATFORM(ver_tlv.cnvi_bt)) {
  1713. case 0x37:
  1714. break;
  1715. default:
  1716. bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)",
  1717. INTEL_HW_PLATFORM(ver_tlv.cnvi_bt));
  1718. err = -EINVAL;
  1719. goto exit_error;
  1720. }
  1721. /* Check for supported iBT hardware variants of this firmware
  1722. * loading method.
  1723. *
  1724. * This check has been put in place to ensure correct forward
  1725. * compatibility options when newer hardware variants come
  1726. * along.
  1727. */
  1728. switch (INTEL_HW_VARIANT(ver_tlv.cnvi_bt)) {
  1729. case 0x1e: /* BzrI */
  1730. case 0x1f: /* ScP */
  1731. case 0x22: /* BzrIW */
  1732. /* Display version information of TLV type */
  1733. btintel_version_info_tlv(hdev, &ver_tlv);
  1734. /* Apply the device specific HCI quirks for TLV based devices
  1735. *
  1736. * All TLV based devices support WBS
  1737. */
  1738. hci_set_quirk(hdev, HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED);
  1739. /* Setup MSFT Extension support */
  1740. btintel_set_msft_opcode(hdev,
  1741. INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
  1742. err = btintel_bootloader_setup_tlv(hdev, &ver_tlv);
  1743. if (err)
  1744. goto exit_error;
  1745. break;
  1746. default:
  1747. bt_dev_err(hdev, "Unsupported Intel hw variant (%u)",
  1748. INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
  1749. err = -EINVAL;
  1750. goto exit_error;
  1751. break;
  1752. }
  1753. data->dmp_hdr.cnvi_top = ver_tlv.cnvi_top;
  1754. data->dmp_hdr.cnvr_top = ver_tlv.cnvr_top;
  1755. data->dmp_hdr.fw_timestamp = ver_tlv.timestamp;
  1756. data->dmp_hdr.fw_build_type = ver_tlv.build_type;
  1757. data->dmp_hdr.fw_build_num = ver_tlv.build_num;
  1758. data->dmp_hdr.cnvi_bt = ver_tlv.cnvi_bt;
  1759. if (ver_tlv.img_type == 0x02 || ver_tlv.img_type == 0x03)
  1760. data->dmp_hdr.fw_git_sha1 = ver_tlv.git_sha1;
  1761. btintel_print_fseq_info(hdev);
  1762. exit_error:
  1763. kfree_skb(skb);
  1764. return err;
  1765. }
  1766. static int btintel_pcie_setup(struct hci_dev *hdev)
  1767. {
  1768. int err, fw_dl_retry = 0;
  1769. struct btintel_pcie_data *data = hci_get_drvdata(hdev);
  1770. while ((err = btintel_pcie_setup_internal(hdev)) && fw_dl_retry++ < 1) {
  1771. bt_dev_err(hdev, "Firmware download retry count: %d",
  1772. fw_dl_retry);
  1773. btintel_pcie_dump_debug_registers(hdev);
  1774. btintel_pcie_disable_interrupts(data);
  1775. btintel_pcie_synchronize_irqs(data);
  1776. err = btintel_pcie_reset_bt(data);
  1777. if (err) {
  1778. bt_dev_err(hdev, "Failed to do shr reset: %d", err);
  1779. break;
  1780. }
  1781. usleep_range(10000, 12000);
  1782. btintel_pcie_reset_ia(data);
  1783. btintel_pcie_enable_interrupts(data);
  1784. btintel_pcie_config_msix(data);
  1785. err = btintel_pcie_enable_bt(data);
  1786. if (err) {
  1787. bt_dev_err(hdev, "Failed to enable hardware: %d", err);
  1788. break;
  1789. }
  1790. btintel_pcie_start_rx(data);
  1791. }
  1792. if (!err)
  1793. set_bit(BTINTEL_PCIE_SETUP_DONE, &data->flags);
  1794. return err;
  1795. }
  1796. static struct btintel_pcie_dev_recovery *
  1797. btintel_pcie_get_recovery(struct pci_dev *pdev, struct device *dev)
  1798. {
  1799. struct btintel_pcie_dev_recovery *tmp, *data = NULL;
  1800. const char *name = pci_name(pdev);
  1801. const size_t name_len = strlen(name) + 1;
  1802. struct hci_dev *hdev = to_hci_dev(dev);
  1803. spin_lock(&btintel_pcie_recovery_lock);
  1804. list_for_each_entry(tmp, &btintel_pcie_recovery_list, list) {
  1805. if (strcmp(tmp->name, name))
  1806. continue;
  1807. data = tmp;
  1808. break;
  1809. }
  1810. spin_unlock(&btintel_pcie_recovery_lock);
  1811. if (data) {
  1812. bt_dev_dbg(hdev, "Found restart data for BDF: %s", data->name);
  1813. return data;
  1814. }
  1815. data = kzalloc_flex(*data, name, name_len, GFP_ATOMIC);
  1816. if (!data)
  1817. return NULL;
  1818. strscpy(data->name, name, name_len);
  1819. spin_lock(&btintel_pcie_recovery_lock);
  1820. list_add_tail(&data->list, &btintel_pcie_recovery_list);
  1821. spin_unlock(&btintel_pcie_recovery_lock);
  1822. return data;
  1823. }
  1824. static void btintel_pcie_free_restart_list(void)
  1825. {
  1826. struct btintel_pcie_dev_recovery *tmp;
  1827. while ((tmp = list_first_entry_or_null(&btintel_pcie_recovery_list,
  1828. typeof(*tmp), list))) {
  1829. list_del(&tmp->list);
  1830. kfree(tmp);
  1831. }
  1832. }
  1833. static void btintel_pcie_inc_recovery_count(struct pci_dev *pdev,
  1834. struct device *dev)
  1835. {
  1836. struct btintel_pcie_dev_recovery *data;
  1837. time64_t retry_window;
  1838. data = btintel_pcie_get_recovery(pdev, dev);
  1839. if (!data)
  1840. return;
  1841. retry_window = ktime_get_boottime_seconds() - data->last_error;
  1842. if (data->count == 0) {
  1843. data->last_error = ktime_get_boottime_seconds();
  1844. data->count++;
  1845. } else if (retry_window < BTINTEL_PCIE_RESET_WINDOW_SECS &&
  1846. data->count <= BTINTEL_PCIE_FLR_MAX_RETRY) {
  1847. data->count++;
  1848. } else if (retry_window > BTINTEL_PCIE_RESET_WINDOW_SECS) {
  1849. data->last_error = 0;
  1850. data->count = 0;
  1851. }
  1852. }
  1853. static int btintel_pcie_setup_hdev(struct btintel_pcie_data *data);
  1854. static void btintel_pcie_removal_work(struct work_struct *wk)
  1855. {
  1856. struct btintel_pcie_removal *removal =
  1857. container_of(wk, struct btintel_pcie_removal, work);
  1858. struct pci_dev *pdev = removal->pdev;
  1859. struct btintel_pcie_data *data;
  1860. int err;
  1861. pci_lock_rescan_remove();
  1862. if (!pdev->bus)
  1863. goto error;
  1864. data = pci_get_drvdata(pdev);
  1865. btintel_pcie_disable_interrupts(data);
  1866. btintel_pcie_synchronize_irqs(data);
  1867. flush_work(&data->rx_work);
  1868. bt_dev_dbg(data->hdev, "Release bluetooth interface");
  1869. btintel_pcie_release_hdev(data);
  1870. err = pci_reset_function(pdev);
  1871. if (err) {
  1872. BT_ERR("Failed resetting the pcie device (%d)", err);
  1873. goto error;
  1874. }
  1875. btintel_pcie_enable_interrupts(data);
  1876. btintel_pcie_config_msix(data);
  1877. err = btintel_pcie_enable_bt(data);
  1878. if (err) {
  1879. BT_ERR("Failed to enable bluetooth hardware after reset (%d)",
  1880. err);
  1881. goto error;
  1882. }
  1883. btintel_pcie_reset_ia(data);
  1884. btintel_pcie_start_rx(data);
  1885. data->flags = 0;
  1886. err = btintel_pcie_setup_hdev(data);
  1887. if (err) {
  1888. BT_ERR("Failed registering hdev (%d)", err);
  1889. goto error;
  1890. }
  1891. error:
  1892. pci_dev_put(pdev);
  1893. pci_unlock_rescan_remove();
  1894. kfree(removal);
  1895. }
  1896. static void btintel_pcie_reset(struct hci_dev *hdev)
  1897. {
  1898. struct btintel_pcie_removal *removal;
  1899. struct btintel_pcie_data *data;
  1900. data = hci_get_drvdata(hdev);
  1901. if (!test_bit(BTINTEL_PCIE_SETUP_DONE, &data->flags))
  1902. return;
  1903. if (test_and_set_bit(BTINTEL_PCIE_RECOVERY_IN_PROGRESS, &data->flags))
  1904. return;
  1905. removal = kzalloc_obj(*removal, GFP_ATOMIC);
  1906. if (!removal)
  1907. return;
  1908. removal->pdev = data->pdev;
  1909. INIT_WORK(&removal->work, btintel_pcie_removal_work);
  1910. pci_dev_get(removal->pdev);
  1911. schedule_work(&removal->work);
  1912. }
  1913. static void btintel_pcie_hw_error(struct hci_dev *hdev, u8 code)
  1914. {
  1915. struct btintel_pcie_dev_recovery *data;
  1916. struct btintel_pcie_data *dev_data = hci_get_drvdata(hdev);
  1917. struct pci_dev *pdev = dev_data->pdev;
  1918. time64_t retry_window;
  1919. if (code == 0x13) {
  1920. bt_dev_err(hdev, "Encountered top exception");
  1921. return;
  1922. }
  1923. data = btintel_pcie_get_recovery(pdev, &hdev->dev);
  1924. if (!data)
  1925. return;
  1926. retry_window = ktime_get_boottime_seconds() - data->last_error;
  1927. if (retry_window < BTINTEL_PCIE_RESET_WINDOW_SECS &&
  1928. data->count >= BTINTEL_PCIE_FLR_MAX_RETRY) {
  1929. bt_dev_err(hdev, "Exhausted maximum: %d recovery attempts: %d",
  1930. BTINTEL_PCIE_FLR_MAX_RETRY, data->count);
  1931. bt_dev_dbg(hdev, "Boot time: %lld seconds",
  1932. ktime_get_boottime_seconds());
  1933. bt_dev_dbg(hdev, "last error at: %lld seconds",
  1934. data->last_error);
  1935. return;
  1936. }
  1937. btintel_pcie_inc_recovery_count(pdev, &hdev->dev);
  1938. btintel_pcie_reset(hdev);
  1939. }
  1940. static bool btintel_pcie_wakeup(struct hci_dev *hdev)
  1941. {
  1942. struct btintel_pcie_data *data = hci_get_drvdata(hdev);
  1943. return device_may_wakeup(&data->pdev->dev);
  1944. }
  1945. static const struct {
  1946. u16 opcode;
  1947. const char *desc;
  1948. } btintel_pcie_hci_drv_supported_commands[] = {
  1949. /* Common commands */
  1950. { HCI_DRV_OP_READ_INFO, "Read Info" },
  1951. };
  1952. static int btintel_pcie_hci_drv_read_info(struct hci_dev *hdev, void *data,
  1953. u16 data_len)
  1954. {
  1955. struct hci_drv_rp_read_info *rp;
  1956. size_t rp_size;
  1957. int err, i;
  1958. u16 opcode, num_supported_commands =
  1959. ARRAY_SIZE(btintel_pcie_hci_drv_supported_commands);
  1960. rp_size = sizeof(*rp) + num_supported_commands * 2;
  1961. rp = kmalloc(rp_size, GFP_KERNEL);
  1962. if (!rp)
  1963. return -ENOMEM;
  1964. strscpy_pad(rp->driver_name, KBUILD_MODNAME);
  1965. rp->num_supported_commands = cpu_to_le16(num_supported_commands);
  1966. for (i = 0; i < num_supported_commands; i++) {
  1967. opcode = btintel_pcie_hci_drv_supported_commands[i].opcode;
  1968. bt_dev_dbg(hdev,
  1969. "Supported HCI Drv command (0x%02x|0x%04x): %s",
  1970. hci_opcode_ogf(opcode),
  1971. hci_opcode_ocf(opcode),
  1972. btintel_pcie_hci_drv_supported_commands[i].desc);
  1973. rp->supported_commands[i] = cpu_to_le16(opcode);
  1974. }
  1975. err = hci_drv_cmd_complete(hdev, HCI_DRV_OP_READ_INFO,
  1976. HCI_DRV_STATUS_SUCCESS,
  1977. rp, rp_size);
  1978. kfree(rp);
  1979. return err;
  1980. }
  1981. static const struct hci_drv_handler btintel_pcie_hci_drv_common_handlers[] = {
  1982. { btintel_pcie_hci_drv_read_info, HCI_DRV_READ_INFO_SIZE },
  1983. };
  1984. static const struct hci_drv_handler btintel_pcie_hci_drv_specific_handlers[] = {};
  1985. static struct hci_drv btintel_pcie_hci_drv = {
  1986. .common_handler_count = ARRAY_SIZE(btintel_pcie_hci_drv_common_handlers),
  1987. .common_handlers = btintel_pcie_hci_drv_common_handlers,
  1988. .specific_handler_count = ARRAY_SIZE(btintel_pcie_hci_drv_specific_handlers),
  1989. .specific_handlers = btintel_pcie_hci_drv_specific_handlers,
  1990. };
  1991. static int btintel_pcie_setup_hdev(struct btintel_pcie_data *data)
  1992. {
  1993. int err;
  1994. struct hci_dev *hdev;
  1995. hdev = hci_alloc_dev_priv(sizeof(struct btintel_data));
  1996. if (!hdev)
  1997. return -ENOMEM;
  1998. hdev->bus = HCI_PCI;
  1999. hci_set_drvdata(hdev, data);
  2000. data->hdev = hdev;
  2001. SET_HCIDEV_DEV(hdev, &data->pdev->dev);
  2002. hdev->manufacturer = 2;
  2003. hdev->open = btintel_pcie_open;
  2004. hdev->close = btintel_pcie_close;
  2005. hdev->send = btintel_pcie_send_frame;
  2006. hdev->setup = btintel_pcie_setup;
  2007. hdev->shutdown = btintel_shutdown_combined;
  2008. hdev->hw_error = btintel_pcie_hw_error;
  2009. hdev->set_diag = btintel_set_diag;
  2010. hdev->set_bdaddr = btintel_set_bdaddr;
  2011. hdev->reset = btintel_pcie_reset;
  2012. hdev->wakeup = btintel_pcie_wakeup;
  2013. hdev->hci_drv = &btintel_pcie_hci_drv;
  2014. err = hci_register_dev(hdev);
  2015. if (err < 0) {
  2016. BT_ERR("Failed to register to hdev (%d)", err);
  2017. goto exit_error;
  2018. }
  2019. data->dmp_hdr.driver_name = KBUILD_MODNAME;
  2020. return 0;
  2021. exit_error:
  2022. hci_free_dev(hdev);
  2023. return err;
  2024. }
  2025. static int btintel_pcie_probe(struct pci_dev *pdev,
  2026. const struct pci_device_id *ent)
  2027. {
  2028. int err;
  2029. struct btintel_pcie_data *data;
  2030. if (!pdev)
  2031. return -ENODEV;
  2032. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  2033. if (!data)
  2034. return -ENOMEM;
  2035. data->pdev = pdev;
  2036. spin_lock_init(&data->irq_lock);
  2037. spin_lock_init(&data->hci_rx_lock);
  2038. init_waitqueue_head(&data->gp0_wait_q);
  2039. data->gp0_received = false;
  2040. init_waitqueue_head(&data->tx_wait_q);
  2041. data->tx_wait_done = false;
  2042. data->workqueue = alloc_ordered_workqueue(KBUILD_MODNAME, WQ_HIGHPRI);
  2043. if (!data->workqueue)
  2044. return -ENOMEM;
  2045. skb_queue_head_init(&data->rx_skb_q);
  2046. INIT_WORK(&data->rx_work, btintel_pcie_rx_work);
  2047. data->boot_stage_cache = 0x00;
  2048. data->img_resp_cache = 0x00;
  2049. err = btintel_pcie_config_pcie(pdev, data);
  2050. if (err)
  2051. goto exit_error;
  2052. pci_set_drvdata(pdev, data);
  2053. err = btintel_pcie_alloc(data);
  2054. if (err)
  2055. goto exit_error;
  2056. err = btintel_pcie_enable_bt(data);
  2057. if (err)
  2058. goto exit_error;
  2059. /* CNV information (CNVi and CNVr) is in CSR */
  2060. data->cnvi = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_HW_REV_REG);
  2061. data->cnvr = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_RF_ID_REG);
  2062. err = btintel_pcie_start_rx(data);
  2063. if (err)
  2064. goto exit_error;
  2065. err = btintel_pcie_setup_hdev(data);
  2066. if (err)
  2067. goto exit_error;
  2068. bt_dev_dbg(data->hdev, "cnvi: 0x%8.8x cnvr: 0x%8.8x", data->cnvi,
  2069. data->cnvr);
  2070. return 0;
  2071. exit_error:
  2072. /* reset device before exit */
  2073. btintel_pcie_reset_bt(data);
  2074. pci_clear_master(pdev);
  2075. pci_set_drvdata(pdev, NULL);
  2076. return err;
  2077. }
  2078. static void btintel_pcie_remove(struct pci_dev *pdev)
  2079. {
  2080. struct btintel_pcie_data *data;
  2081. data = pci_get_drvdata(pdev);
  2082. btintel_pcie_disable_interrupts(data);
  2083. btintel_pcie_synchronize_irqs(data);
  2084. flush_work(&data->rx_work);
  2085. btintel_pcie_reset_bt(data);
  2086. for (int i = 0; i < data->alloc_vecs; i++) {
  2087. struct msix_entry *msix_entry;
  2088. msix_entry = &data->msix_entries[i];
  2089. free_irq(msix_entry->vector, msix_entry);
  2090. }
  2091. pci_free_irq_vectors(pdev);
  2092. btintel_pcie_release_hdev(data);
  2093. destroy_workqueue(data->workqueue);
  2094. btintel_pcie_free(data);
  2095. pci_clear_master(pdev);
  2096. pci_set_drvdata(pdev, NULL);
  2097. }
  2098. #ifdef CONFIG_DEV_COREDUMP
  2099. static void btintel_pcie_coredump(struct device *dev)
  2100. {
  2101. struct pci_dev *pdev = to_pci_dev(dev);
  2102. struct btintel_pcie_data *data = pci_get_drvdata(pdev);
  2103. if (test_and_set_bit(BTINTEL_PCIE_COREDUMP_INPROGRESS, &data->flags))
  2104. return;
  2105. data->dmp_hdr.trigger_reason = BTINTEL_PCIE_TRIGGER_REASON_USER_TRIGGER;
  2106. queue_work(data->workqueue, &data->rx_work);
  2107. }
  2108. #endif
  2109. static int btintel_pcie_set_dxstate(struct btintel_pcie_data *data, u32 dxstate)
  2110. {
  2111. int retry = 0, status;
  2112. u32 dx_intr_timeout_ms = 200;
  2113. do {
  2114. data->gp0_received = false;
  2115. btintel_pcie_wr_sleep_cntrl(data, dxstate);
  2116. status = wait_event_timeout(data->gp0_wait_q, data->gp0_received,
  2117. msecs_to_jiffies(dx_intr_timeout_ms));
  2118. if (status)
  2119. return 0;
  2120. bt_dev_warn(data->hdev,
  2121. "Timeout (%u ms) on alive interrupt for D%d entry, retry count %d",
  2122. dx_intr_timeout_ms, dxstate, retry);
  2123. /* clear gp0 cause */
  2124. btintel_pcie_clr_reg_bits(data,
  2125. BTINTEL_PCIE_CSR_MSIX_HW_INT_CAUSES,
  2126. BTINTEL_PCIE_MSIX_HW_INT_CAUSES_GP0);
  2127. /* A hardware bug may cause the alive interrupt to be missed.
  2128. * Check if the controller reached the expected state and retry
  2129. * the operation only if it hasn't.
  2130. */
  2131. if (dxstate == BTINTEL_PCIE_STATE_D0) {
  2132. if (btintel_pcie_in_d0(data))
  2133. return 0;
  2134. } else {
  2135. if (btintel_pcie_in_d3(data))
  2136. return 0;
  2137. }
  2138. } while (++retry < BTINTEL_PCIE_DX_TRANSITION_MAX_RETRIES);
  2139. return -EBUSY;
  2140. }
  2141. static int btintel_pcie_suspend_late(struct device *dev, pm_message_t mesg)
  2142. {
  2143. struct pci_dev *pdev = to_pci_dev(dev);
  2144. struct btintel_pcie_data *data;
  2145. ktime_t start;
  2146. u32 dxstate;
  2147. int err;
  2148. data = pci_get_drvdata(pdev);
  2149. dxstate = (mesg.event == PM_EVENT_SUSPEND ?
  2150. BTINTEL_PCIE_STATE_D3_HOT : BTINTEL_PCIE_STATE_D3_COLD);
  2151. data->pm_sx_event = mesg.event;
  2152. start = ktime_get();
  2153. /* Refer: 6.4.11.7 -> Platform power management */
  2154. err = btintel_pcie_set_dxstate(data, dxstate);
  2155. if (err)
  2156. return err;
  2157. bt_dev_dbg(data->hdev,
  2158. "device entered into d3 state from d0 in %lld us",
  2159. ktime_to_us(ktime_get() - start));
  2160. return err;
  2161. }
  2162. static int btintel_pcie_suspend(struct device *dev)
  2163. {
  2164. return btintel_pcie_suspend_late(dev, PMSG_SUSPEND);
  2165. }
  2166. static int btintel_pcie_hibernate(struct device *dev)
  2167. {
  2168. return btintel_pcie_suspend_late(dev, PMSG_HIBERNATE);
  2169. }
  2170. static int btintel_pcie_freeze(struct device *dev)
  2171. {
  2172. return btintel_pcie_suspend_late(dev, PMSG_FREEZE);
  2173. }
  2174. static int btintel_pcie_resume(struct device *dev)
  2175. {
  2176. struct pci_dev *pdev = to_pci_dev(dev);
  2177. struct btintel_pcie_data *data;
  2178. ktime_t start;
  2179. int err;
  2180. data = pci_get_drvdata(pdev);
  2181. data->gp0_received = false;
  2182. start = ktime_get();
  2183. /* When the system enters S4 (hibernate) mode, bluetooth device loses
  2184. * power, which results in the erasure of its loaded firmware.
  2185. * Consequently, function level reset (flr) is required on system
  2186. * resume to bring the controller back into an operational state by
  2187. * initiating a new firmware download.
  2188. */
  2189. if (data->pm_sx_event == PM_EVENT_FREEZE ||
  2190. data->pm_sx_event == PM_EVENT_HIBERNATE) {
  2191. set_bit(BTINTEL_PCIE_CORE_HALTED, &data->flags);
  2192. btintel_pcie_reset(data->hdev);
  2193. return 0;
  2194. }
  2195. /* Refer: 6.4.11.7 -> Platform power management */
  2196. err = btintel_pcie_set_dxstate(data, BTINTEL_PCIE_STATE_D0);
  2197. if (err == 0) {
  2198. bt_dev_dbg(data->hdev,
  2199. "device entered into d0 state from d3 in %lld us",
  2200. ktime_to_us(ktime_get() - start));
  2201. return err;
  2202. }
  2203. /* Trigger function level reset if the controller is in error
  2204. * state during resume() to bring back the controller to
  2205. * operational mode
  2206. */
  2207. data->boot_stage_cache = btintel_pcie_rd_reg32(data,
  2208. BTINTEL_PCIE_CSR_BOOT_STAGE_REG);
  2209. if (btintel_pcie_in_error(data) ||
  2210. btintel_pcie_in_device_halt(data)) {
  2211. bt_dev_err(data->hdev, "Controller in error state for D0 entry");
  2212. if (!test_and_set_bit(BTINTEL_PCIE_COREDUMP_INPROGRESS,
  2213. &data->flags)) {
  2214. data->dmp_hdr.trigger_reason =
  2215. BTINTEL_PCIE_TRIGGER_REASON_FW_ASSERT;
  2216. queue_work(data->workqueue, &data->rx_work);
  2217. }
  2218. set_bit(BTINTEL_PCIE_CORE_HALTED, &data->flags);
  2219. btintel_pcie_reset(data->hdev);
  2220. }
  2221. return err;
  2222. }
  2223. static const struct dev_pm_ops btintel_pcie_pm_ops = {
  2224. .suspend = btintel_pcie_suspend,
  2225. .resume = btintel_pcie_resume,
  2226. .freeze = btintel_pcie_freeze,
  2227. .thaw = btintel_pcie_resume,
  2228. .poweroff = btintel_pcie_hibernate,
  2229. .restore = btintel_pcie_resume,
  2230. };
  2231. static struct pci_driver btintel_pcie_driver = {
  2232. .name = KBUILD_MODNAME,
  2233. .id_table = btintel_pcie_table,
  2234. .probe = btintel_pcie_probe,
  2235. .remove = btintel_pcie_remove,
  2236. .driver.pm = pm_sleep_ptr(&btintel_pcie_pm_ops),
  2237. #ifdef CONFIG_DEV_COREDUMP
  2238. .driver.coredump = btintel_pcie_coredump
  2239. #endif
  2240. };
  2241. static int __init btintel_pcie_init(void)
  2242. {
  2243. return pci_register_driver(&btintel_pcie_driver);
  2244. }
  2245. static void __exit btintel_pcie_exit(void)
  2246. {
  2247. pci_unregister_driver(&btintel_pcie_driver);
  2248. btintel_pcie_free_restart_list();
  2249. }
  2250. module_init(btintel_pcie_init);
  2251. module_exit(btintel_pcie_exit);
  2252. MODULE_AUTHOR("Tedd Ho-Jeong An <tedd.an@intel.com>");
  2253. MODULE_DESCRIPTION("Intel Bluetooth PCIe transport driver ver " VERSION);
  2254. MODULE_VERSION(VERSION);
  2255. MODULE_LICENSE("GPL");