hinic_hw_dev.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. /*
  2. * Huawei HiNIC PCI Express Linux driver
  3. * Copyright(c) 2017 Huawei Technologies Co., Ltd
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/types.h>
  17. #include <linux/pci.h>
  18. #include <linux/device.h>
  19. #include <linux/errno.h>
  20. #include <linux/slab.h>
  21. #include <linux/bitops.h>
  22. #include <linux/delay.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/log2.h>
  25. #include <linux/err.h>
  26. #include "hinic_hw_if.h"
  27. #include "hinic_hw_eqs.h"
  28. #include "hinic_hw_mgmt.h"
  29. #include "hinic_hw_qp_ctxt.h"
  30. #include "hinic_hw_qp.h"
  31. #include "hinic_hw_io.h"
  32. #include "hinic_hw_dev.h"
  33. #define IO_STATUS_TIMEOUT 100
  34. #define OUTBOUND_STATE_TIMEOUT 100
  35. #define DB_STATE_TIMEOUT 100
  36. #define MAX_IRQS(max_qps, num_aeqs, num_ceqs) \
  37. (2 * (max_qps) + (num_aeqs) + (num_ceqs))
  38. #define ADDR_IN_4BYTES(addr) ((addr) >> 2)
  39. enum intr_type {
  40. INTR_MSIX_TYPE,
  41. };
  42. enum io_status {
  43. IO_STOPPED = 0,
  44. IO_RUNNING = 1,
  45. };
  46. enum hw_ioctxt_set_cmdq_depth {
  47. HW_IOCTXT_SET_CMDQ_DEPTH_DEFAULT,
  48. };
  49. /* HW struct */
  50. struct hinic_dev_cap {
  51. u8 status;
  52. u8 version;
  53. u8 rsvd0[6];
  54. u8 rsvd1[5];
  55. u8 intr_type;
  56. u8 rsvd2[66];
  57. u16 max_sqs;
  58. u16 max_rqs;
  59. u8 rsvd3[208];
  60. };
  61. /**
  62. * get_capability - convert device capabilities to NIC capabilities
  63. * @hwdev: the HW device to set and convert device capabilities for
  64. * @dev_cap: device capabilities from FW
  65. *
  66. * Return 0 - Success, negative - Failure
  67. **/
  68. static int get_capability(struct hinic_hwdev *hwdev,
  69. struct hinic_dev_cap *dev_cap)
  70. {
  71. struct hinic_cap *nic_cap = &hwdev->nic_cap;
  72. int num_aeqs, num_ceqs, num_irqs;
  73. if (!HINIC_IS_PF(hwdev->hwif) && !HINIC_IS_PPF(hwdev->hwif))
  74. return -EINVAL;
  75. if (dev_cap->intr_type != INTR_MSIX_TYPE)
  76. return -EFAULT;
  77. num_aeqs = HINIC_HWIF_NUM_AEQS(hwdev->hwif);
  78. num_ceqs = HINIC_HWIF_NUM_CEQS(hwdev->hwif);
  79. num_irqs = HINIC_HWIF_NUM_IRQS(hwdev->hwif);
  80. /* Each QP has its own (SQ + RQ) interrupts */
  81. nic_cap->num_qps = (num_irqs - (num_aeqs + num_ceqs)) / 2;
  82. if (nic_cap->num_qps > HINIC_Q_CTXT_MAX)
  83. nic_cap->num_qps = HINIC_Q_CTXT_MAX;
  84. /* num_qps must be power of 2 */
  85. nic_cap->num_qps = BIT(fls(nic_cap->num_qps) - 1);
  86. nic_cap->max_qps = dev_cap->max_sqs + 1;
  87. if (nic_cap->max_qps != (dev_cap->max_rqs + 1))
  88. return -EFAULT;
  89. if (nic_cap->num_qps > nic_cap->max_qps)
  90. nic_cap->num_qps = nic_cap->max_qps;
  91. return 0;
  92. }
  93. /**
  94. * get_cap_from_fw - get device capabilities from FW
  95. * @pfhwdev: the PF HW device to get capabilities for
  96. *
  97. * Return 0 - Success, negative - Failure
  98. **/
  99. static int get_cap_from_fw(struct hinic_pfhwdev *pfhwdev)
  100. {
  101. struct hinic_hwdev *hwdev = &pfhwdev->hwdev;
  102. struct hinic_hwif *hwif = hwdev->hwif;
  103. struct pci_dev *pdev = hwif->pdev;
  104. struct hinic_dev_cap dev_cap;
  105. u16 in_len, out_len;
  106. int err;
  107. in_len = 0;
  108. out_len = sizeof(dev_cap);
  109. err = hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_CFGM,
  110. HINIC_CFG_NIC_CAP, &dev_cap, in_len, &dev_cap,
  111. &out_len, HINIC_MGMT_MSG_SYNC);
  112. if (err) {
  113. dev_err(&pdev->dev, "Failed to get capability from FW\n");
  114. return err;
  115. }
  116. return get_capability(hwdev, &dev_cap);
  117. }
  118. /**
  119. * get_dev_cap - get device capabilities
  120. * @hwdev: the NIC HW device to get capabilities for
  121. *
  122. * Return 0 - Success, negative - Failure
  123. **/
  124. static int get_dev_cap(struct hinic_hwdev *hwdev)
  125. {
  126. struct hinic_hwif *hwif = hwdev->hwif;
  127. struct pci_dev *pdev = hwif->pdev;
  128. struct hinic_pfhwdev *pfhwdev;
  129. int err;
  130. switch (HINIC_FUNC_TYPE(hwif)) {
  131. case HINIC_PPF:
  132. case HINIC_PF:
  133. pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
  134. err = get_cap_from_fw(pfhwdev);
  135. if (err) {
  136. dev_err(&pdev->dev, "Failed to get capability from FW\n");
  137. return err;
  138. }
  139. break;
  140. default:
  141. dev_err(&pdev->dev, "Unsupported PCI Function type\n");
  142. return -EINVAL;
  143. }
  144. return 0;
  145. }
  146. /**
  147. * init_msix - enable the msix and save the entries
  148. * @hwdev: the NIC HW device
  149. *
  150. * Return 0 - Success, negative - Failure
  151. **/
  152. static int init_msix(struct hinic_hwdev *hwdev)
  153. {
  154. struct hinic_hwif *hwif = hwdev->hwif;
  155. struct pci_dev *pdev = hwif->pdev;
  156. int nr_irqs, num_aeqs, num_ceqs;
  157. size_t msix_entries_size;
  158. int i, err;
  159. num_aeqs = HINIC_HWIF_NUM_AEQS(hwif);
  160. num_ceqs = HINIC_HWIF_NUM_CEQS(hwif);
  161. nr_irqs = MAX_IRQS(HINIC_MAX_QPS, num_aeqs, num_ceqs);
  162. if (nr_irqs > HINIC_HWIF_NUM_IRQS(hwif))
  163. nr_irqs = HINIC_HWIF_NUM_IRQS(hwif);
  164. msix_entries_size = nr_irqs * sizeof(*hwdev->msix_entries);
  165. hwdev->msix_entries = devm_kzalloc(&pdev->dev, msix_entries_size,
  166. GFP_KERNEL);
  167. if (!hwdev->msix_entries)
  168. return -ENOMEM;
  169. for (i = 0; i < nr_irqs; i++)
  170. hwdev->msix_entries[i].entry = i;
  171. err = pci_enable_msix_exact(pdev, hwdev->msix_entries, nr_irqs);
  172. if (err) {
  173. dev_err(&pdev->dev, "Failed to enable pci msix\n");
  174. return err;
  175. }
  176. return 0;
  177. }
  178. /**
  179. * disable_msix - disable the msix
  180. * @hwdev: the NIC HW device
  181. **/
  182. static void disable_msix(struct hinic_hwdev *hwdev)
  183. {
  184. struct hinic_hwif *hwif = hwdev->hwif;
  185. struct pci_dev *pdev = hwif->pdev;
  186. pci_disable_msix(pdev);
  187. }
  188. /**
  189. * hinic_port_msg_cmd - send port msg to mgmt
  190. * @hwdev: the NIC HW device
  191. * @cmd: the port command
  192. * @buf_in: input buffer
  193. * @in_size: input size
  194. * @buf_out: output buffer
  195. * @out_size: returned output size
  196. *
  197. * Return 0 - Success, negative - Failure
  198. **/
  199. int hinic_port_msg_cmd(struct hinic_hwdev *hwdev, enum hinic_port_cmd cmd,
  200. void *buf_in, u16 in_size, void *buf_out, u16 *out_size)
  201. {
  202. struct hinic_hwif *hwif = hwdev->hwif;
  203. struct pci_dev *pdev = hwif->pdev;
  204. struct hinic_pfhwdev *pfhwdev;
  205. if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
  206. dev_err(&pdev->dev, "unsupported PCI Function type\n");
  207. return -EINVAL;
  208. }
  209. pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
  210. return hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_L2NIC, cmd,
  211. buf_in, in_size, buf_out, out_size,
  212. HINIC_MGMT_MSG_SYNC);
  213. }
  214. /**
  215. * init_fw_ctxt- Init Firmware tables before network mgmt and io operations
  216. * @hwdev: the NIC HW device
  217. *
  218. * Return 0 - Success, negative - Failure
  219. **/
  220. static int init_fw_ctxt(struct hinic_hwdev *hwdev)
  221. {
  222. struct hinic_hwif *hwif = hwdev->hwif;
  223. struct pci_dev *pdev = hwif->pdev;
  224. struct hinic_cmd_fw_ctxt fw_ctxt;
  225. struct hinic_pfhwdev *pfhwdev;
  226. u16 out_size;
  227. int err;
  228. if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
  229. dev_err(&pdev->dev, "Unsupported PCI Function type\n");
  230. return -EINVAL;
  231. }
  232. fw_ctxt.func_idx = HINIC_HWIF_FUNC_IDX(hwif);
  233. fw_ctxt.rx_buf_sz = HINIC_RX_BUF_SZ;
  234. pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
  235. err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_FWCTXT_INIT,
  236. &fw_ctxt, sizeof(fw_ctxt),
  237. &fw_ctxt, &out_size);
  238. if (err || (out_size != sizeof(fw_ctxt)) || fw_ctxt.status) {
  239. dev_err(&pdev->dev, "Failed to init FW ctxt, ret = %d\n",
  240. fw_ctxt.status);
  241. return -EFAULT;
  242. }
  243. return 0;
  244. }
  245. /**
  246. * set_hw_ioctxt - set the shape of the IO queues in FW
  247. * @hwdev: the NIC HW device
  248. * @rq_depth: rq depth
  249. * @sq_depth: sq depth
  250. *
  251. * Return 0 - Success, negative - Failure
  252. **/
  253. static int set_hw_ioctxt(struct hinic_hwdev *hwdev, unsigned int rq_depth,
  254. unsigned int sq_depth)
  255. {
  256. struct hinic_hwif *hwif = hwdev->hwif;
  257. struct hinic_cmd_hw_ioctxt hw_ioctxt;
  258. struct pci_dev *pdev = hwif->pdev;
  259. struct hinic_pfhwdev *pfhwdev;
  260. if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
  261. dev_err(&pdev->dev, "Unsupported PCI Function type\n");
  262. return -EINVAL;
  263. }
  264. hw_ioctxt.func_idx = HINIC_HWIF_FUNC_IDX(hwif);
  265. hw_ioctxt.set_cmdq_depth = HW_IOCTXT_SET_CMDQ_DEPTH_DEFAULT;
  266. hw_ioctxt.cmdq_depth = 0;
  267. hw_ioctxt.rq_depth = ilog2(rq_depth);
  268. hw_ioctxt.rx_buf_sz_idx = HINIC_RX_BUF_SZ_IDX;
  269. hw_ioctxt.sq_depth = ilog2(sq_depth);
  270. pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
  271. return hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_COMM,
  272. HINIC_COMM_CMD_HWCTXT_SET,
  273. &hw_ioctxt, sizeof(hw_ioctxt), NULL,
  274. NULL, HINIC_MGMT_MSG_SYNC);
  275. }
  276. static int wait_for_outbound_state(struct hinic_hwdev *hwdev)
  277. {
  278. enum hinic_outbound_state outbound_state;
  279. struct hinic_hwif *hwif = hwdev->hwif;
  280. struct pci_dev *pdev = hwif->pdev;
  281. unsigned long end;
  282. end = jiffies + msecs_to_jiffies(OUTBOUND_STATE_TIMEOUT);
  283. do {
  284. outbound_state = hinic_outbound_state_get(hwif);
  285. if (outbound_state == HINIC_OUTBOUND_ENABLE)
  286. return 0;
  287. msleep(20);
  288. } while (time_before(jiffies, end));
  289. dev_err(&pdev->dev, "Wait for OUTBOUND - Timeout\n");
  290. return -EFAULT;
  291. }
  292. static int wait_for_db_state(struct hinic_hwdev *hwdev)
  293. {
  294. struct hinic_hwif *hwif = hwdev->hwif;
  295. struct pci_dev *pdev = hwif->pdev;
  296. enum hinic_db_state db_state;
  297. unsigned long end;
  298. end = jiffies + msecs_to_jiffies(DB_STATE_TIMEOUT);
  299. do {
  300. db_state = hinic_db_state_get(hwif);
  301. if (db_state == HINIC_DB_ENABLE)
  302. return 0;
  303. msleep(20);
  304. } while (time_before(jiffies, end));
  305. dev_err(&pdev->dev, "Wait for DB - Timeout\n");
  306. return -EFAULT;
  307. }
  308. static int wait_for_io_stopped(struct hinic_hwdev *hwdev)
  309. {
  310. struct hinic_cmd_io_status cmd_io_status;
  311. struct hinic_hwif *hwif = hwdev->hwif;
  312. struct pci_dev *pdev = hwif->pdev;
  313. struct hinic_pfhwdev *pfhwdev;
  314. unsigned long end;
  315. u16 out_size;
  316. int err;
  317. if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
  318. dev_err(&pdev->dev, "Unsupported PCI Function type\n");
  319. return -EINVAL;
  320. }
  321. pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
  322. cmd_io_status.func_idx = HINIC_HWIF_FUNC_IDX(hwif);
  323. end = jiffies + msecs_to_jiffies(IO_STATUS_TIMEOUT);
  324. do {
  325. err = hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_COMM,
  326. HINIC_COMM_CMD_IO_STATUS_GET,
  327. &cmd_io_status, sizeof(cmd_io_status),
  328. &cmd_io_status, &out_size,
  329. HINIC_MGMT_MSG_SYNC);
  330. if ((err) || (out_size != sizeof(cmd_io_status))) {
  331. dev_err(&pdev->dev, "Failed to get IO status, ret = %d\n",
  332. err);
  333. return err;
  334. }
  335. if (cmd_io_status.status == IO_STOPPED) {
  336. dev_info(&pdev->dev, "IO stopped\n");
  337. return 0;
  338. }
  339. msleep(20);
  340. } while (time_before(jiffies, end));
  341. dev_err(&pdev->dev, "Wait for IO stopped - Timeout\n");
  342. return -ETIMEDOUT;
  343. }
  344. /**
  345. * clear_io_resource - set the IO resources as not active in the NIC
  346. * @hwdev: the NIC HW device
  347. *
  348. * Return 0 - Success, negative - Failure
  349. **/
  350. static int clear_io_resources(struct hinic_hwdev *hwdev)
  351. {
  352. struct hinic_cmd_clear_io_res cmd_clear_io_res;
  353. struct hinic_hwif *hwif = hwdev->hwif;
  354. struct pci_dev *pdev = hwif->pdev;
  355. struct hinic_pfhwdev *pfhwdev;
  356. int err;
  357. if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
  358. dev_err(&pdev->dev, "Unsupported PCI Function type\n");
  359. return -EINVAL;
  360. }
  361. err = wait_for_io_stopped(hwdev);
  362. if (err) {
  363. dev_err(&pdev->dev, "IO has not stopped yet\n");
  364. return err;
  365. }
  366. cmd_clear_io_res.func_idx = HINIC_HWIF_FUNC_IDX(hwif);
  367. pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
  368. err = hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_COMM,
  369. HINIC_COMM_CMD_IO_RES_CLEAR, &cmd_clear_io_res,
  370. sizeof(cmd_clear_io_res), NULL, NULL,
  371. HINIC_MGMT_MSG_SYNC);
  372. if (err) {
  373. dev_err(&pdev->dev, "Failed to clear IO resources\n");
  374. return err;
  375. }
  376. return 0;
  377. }
  378. /**
  379. * set_resources_state - set the state of the resources in the NIC
  380. * @hwdev: the NIC HW device
  381. * @state: the state to set
  382. *
  383. * Return 0 - Success, negative - Failure
  384. **/
  385. static int set_resources_state(struct hinic_hwdev *hwdev,
  386. enum hinic_res_state state)
  387. {
  388. struct hinic_cmd_set_res_state res_state;
  389. struct hinic_hwif *hwif = hwdev->hwif;
  390. struct pci_dev *pdev = hwif->pdev;
  391. struct hinic_pfhwdev *pfhwdev;
  392. if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
  393. dev_err(&pdev->dev, "Unsupported PCI Function type\n");
  394. return -EINVAL;
  395. }
  396. res_state.func_idx = HINIC_HWIF_FUNC_IDX(hwif);
  397. res_state.state = state;
  398. pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
  399. return hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt,
  400. HINIC_MOD_COMM,
  401. HINIC_COMM_CMD_RES_STATE_SET,
  402. &res_state, sizeof(res_state), NULL,
  403. NULL, HINIC_MGMT_MSG_SYNC);
  404. }
  405. /**
  406. * get_base_qpn - get the first qp number
  407. * @hwdev: the NIC HW device
  408. * @base_qpn: returned qp number
  409. *
  410. * Return 0 - Success, negative - Failure
  411. **/
  412. static int get_base_qpn(struct hinic_hwdev *hwdev, u16 *base_qpn)
  413. {
  414. struct hinic_cmd_base_qpn cmd_base_qpn;
  415. struct hinic_hwif *hwif = hwdev->hwif;
  416. struct pci_dev *pdev = hwif->pdev;
  417. u16 out_size;
  418. int err;
  419. cmd_base_qpn.func_idx = HINIC_HWIF_FUNC_IDX(hwif);
  420. err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_GET_GLOBAL_QPN,
  421. &cmd_base_qpn, sizeof(cmd_base_qpn),
  422. &cmd_base_qpn, &out_size);
  423. if (err || (out_size != sizeof(cmd_base_qpn)) || cmd_base_qpn.status) {
  424. dev_err(&pdev->dev, "Failed to get base qpn, status = %d\n",
  425. cmd_base_qpn.status);
  426. return -EFAULT;
  427. }
  428. *base_qpn = cmd_base_qpn.qpn;
  429. return 0;
  430. }
  431. /**
  432. * hinic_hwdev_ifup - Preparing the HW for passing IO
  433. * @hwdev: the NIC HW device
  434. *
  435. * Return 0 - Success, negative - Failure
  436. **/
  437. int hinic_hwdev_ifup(struct hinic_hwdev *hwdev)
  438. {
  439. struct hinic_func_to_io *func_to_io = &hwdev->func_to_io;
  440. struct hinic_cap *nic_cap = &hwdev->nic_cap;
  441. struct hinic_hwif *hwif = hwdev->hwif;
  442. int err, num_aeqs, num_ceqs, num_qps;
  443. struct msix_entry *ceq_msix_entries;
  444. struct msix_entry *sq_msix_entries;
  445. struct msix_entry *rq_msix_entries;
  446. struct pci_dev *pdev = hwif->pdev;
  447. u16 base_qpn;
  448. err = get_base_qpn(hwdev, &base_qpn);
  449. if (err) {
  450. dev_err(&pdev->dev, "Failed to get global base qp number\n");
  451. return err;
  452. }
  453. num_aeqs = HINIC_HWIF_NUM_AEQS(hwif);
  454. num_ceqs = HINIC_HWIF_NUM_CEQS(hwif);
  455. ceq_msix_entries = &hwdev->msix_entries[num_aeqs];
  456. err = hinic_io_init(func_to_io, hwif, nic_cap->max_qps, num_ceqs,
  457. ceq_msix_entries);
  458. if (err) {
  459. dev_err(&pdev->dev, "Failed to init IO channel\n");
  460. return err;
  461. }
  462. num_qps = nic_cap->num_qps;
  463. sq_msix_entries = &hwdev->msix_entries[num_aeqs + num_ceqs];
  464. rq_msix_entries = &hwdev->msix_entries[num_aeqs + num_ceqs + num_qps];
  465. err = hinic_io_create_qps(func_to_io, base_qpn, num_qps,
  466. sq_msix_entries, rq_msix_entries);
  467. if (err) {
  468. dev_err(&pdev->dev, "Failed to create QPs\n");
  469. goto err_create_qps;
  470. }
  471. err = wait_for_db_state(hwdev);
  472. if (err) {
  473. dev_warn(&pdev->dev, "db - disabled, try again\n");
  474. hinic_db_state_set(hwif, HINIC_DB_ENABLE);
  475. }
  476. err = set_hw_ioctxt(hwdev, HINIC_SQ_DEPTH, HINIC_RQ_DEPTH);
  477. if (err) {
  478. dev_err(&pdev->dev, "Failed to set HW IO ctxt\n");
  479. goto err_hw_ioctxt;
  480. }
  481. return 0;
  482. err_hw_ioctxt:
  483. hinic_io_destroy_qps(func_to_io, num_qps);
  484. err_create_qps:
  485. hinic_io_free(func_to_io);
  486. return err;
  487. }
  488. /**
  489. * hinic_hwdev_ifdown - Closing the HW for passing IO
  490. * @hwdev: the NIC HW device
  491. *
  492. **/
  493. void hinic_hwdev_ifdown(struct hinic_hwdev *hwdev)
  494. {
  495. struct hinic_func_to_io *func_to_io = &hwdev->func_to_io;
  496. struct hinic_cap *nic_cap = &hwdev->nic_cap;
  497. clear_io_resources(hwdev);
  498. hinic_io_destroy_qps(func_to_io, nic_cap->num_qps);
  499. hinic_io_free(func_to_io);
  500. }
  501. /**
  502. * hinic_hwdev_cb_register - register callback handler for MGMT events
  503. * @hwdev: the NIC HW device
  504. * @cmd: the mgmt event
  505. * @handle: private data for the handler
  506. * @handler: event handler
  507. **/
  508. void hinic_hwdev_cb_register(struct hinic_hwdev *hwdev,
  509. enum hinic_mgmt_msg_cmd cmd, void *handle,
  510. void (*handler)(void *handle, void *buf_in,
  511. u16 in_size, void *buf_out,
  512. u16 *out_size))
  513. {
  514. struct hinic_hwif *hwif = hwdev->hwif;
  515. struct pci_dev *pdev = hwif->pdev;
  516. struct hinic_pfhwdev *pfhwdev;
  517. struct hinic_nic_cb *nic_cb;
  518. u8 cmd_cb;
  519. if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
  520. dev_err(&pdev->dev, "unsupported PCI Function type\n");
  521. return;
  522. }
  523. pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
  524. cmd_cb = cmd - HINIC_MGMT_MSG_CMD_BASE;
  525. nic_cb = &pfhwdev->nic_cb[cmd_cb];
  526. nic_cb->handler = handler;
  527. nic_cb->handle = handle;
  528. nic_cb->cb_state = HINIC_CB_ENABLED;
  529. }
  530. /**
  531. * hinic_hwdev_cb_unregister - unregister callback handler for MGMT events
  532. * @hwdev: the NIC HW device
  533. * @cmd: the mgmt event
  534. **/
  535. void hinic_hwdev_cb_unregister(struct hinic_hwdev *hwdev,
  536. enum hinic_mgmt_msg_cmd cmd)
  537. {
  538. struct hinic_hwif *hwif = hwdev->hwif;
  539. struct pci_dev *pdev = hwif->pdev;
  540. struct hinic_pfhwdev *pfhwdev;
  541. struct hinic_nic_cb *nic_cb;
  542. u8 cmd_cb;
  543. if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
  544. dev_err(&pdev->dev, "unsupported PCI Function type\n");
  545. return;
  546. }
  547. pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
  548. cmd_cb = cmd - HINIC_MGMT_MSG_CMD_BASE;
  549. nic_cb = &pfhwdev->nic_cb[cmd_cb];
  550. nic_cb->cb_state &= ~HINIC_CB_ENABLED;
  551. while (nic_cb->cb_state & HINIC_CB_RUNNING)
  552. schedule();
  553. nic_cb->handler = NULL;
  554. }
  555. /**
  556. * nic_mgmt_msg_handler - nic mgmt event handler
  557. * @handle: private data for the handler
  558. * @buf_in: input buffer
  559. * @in_size: input size
  560. * @buf_out: output buffer
  561. * @out_size: returned output size
  562. **/
  563. static void nic_mgmt_msg_handler(void *handle, u8 cmd, void *buf_in,
  564. u16 in_size, void *buf_out, u16 *out_size)
  565. {
  566. struct hinic_pfhwdev *pfhwdev = handle;
  567. enum hinic_cb_state cb_state;
  568. struct hinic_nic_cb *nic_cb;
  569. struct hinic_hwdev *hwdev;
  570. struct hinic_hwif *hwif;
  571. struct pci_dev *pdev;
  572. u8 cmd_cb;
  573. hwdev = &pfhwdev->hwdev;
  574. hwif = hwdev->hwif;
  575. pdev = hwif->pdev;
  576. if ((cmd < HINIC_MGMT_MSG_CMD_BASE) ||
  577. (cmd >= HINIC_MGMT_MSG_CMD_MAX)) {
  578. dev_err(&pdev->dev, "unknown L2NIC event, cmd = %d\n", cmd);
  579. return;
  580. }
  581. cmd_cb = cmd - HINIC_MGMT_MSG_CMD_BASE;
  582. nic_cb = &pfhwdev->nic_cb[cmd_cb];
  583. cb_state = cmpxchg(&nic_cb->cb_state,
  584. HINIC_CB_ENABLED,
  585. HINIC_CB_ENABLED | HINIC_CB_RUNNING);
  586. if ((cb_state == HINIC_CB_ENABLED) && (nic_cb->handler))
  587. nic_cb->handler(nic_cb->handle, buf_in,
  588. in_size, buf_out, out_size);
  589. else
  590. dev_err(&pdev->dev, "Unhandled NIC Event %d\n", cmd);
  591. nic_cb->cb_state &= ~HINIC_CB_RUNNING;
  592. }
  593. /**
  594. * init_pfhwdev - Initialize the extended components of PF
  595. * @pfhwdev: the HW device for PF
  596. *
  597. * Return 0 - success, negative - failure
  598. **/
  599. static int init_pfhwdev(struct hinic_pfhwdev *pfhwdev)
  600. {
  601. struct hinic_hwdev *hwdev = &pfhwdev->hwdev;
  602. struct hinic_hwif *hwif = hwdev->hwif;
  603. struct pci_dev *pdev = hwif->pdev;
  604. int err;
  605. err = hinic_pf_to_mgmt_init(&pfhwdev->pf_to_mgmt, hwif);
  606. if (err) {
  607. dev_err(&pdev->dev, "Failed to initialize PF to MGMT channel\n");
  608. return err;
  609. }
  610. hinic_register_mgmt_msg_cb(&pfhwdev->pf_to_mgmt, HINIC_MOD_L2NIC,
  611. pfhwdev, nic_mgmt_msg_handler);
  612. hinic_set_pf_action(hwif, HINIC_PF_MGMT_ACTIVE);
  613. return 0;
  614. }
  615. /**
  616. * free_pfhwdev - Free the extended components of PF
  617. * @pfhwdev: the HW device for PF
  618. **/
  619. static void free_pfhwdev(struct hinic_pfhwdev *pfhwdev)
  620. {
  621. struct hinic_hwdev *hwdev = &pfhwdev->hwdev;
  622. hinic_set_pf_action(hwdev->hwif, HINIC_PF_MGMT_INIT);
  623. hinic_unregister_mgmt_msg_cb(&pfhwdev->pf_to_mgmt, HINIC_MOD_L2NIC);
  624. hinic_pf_to_mgmt_free(&pfhwdev->pf_to_mgmt);
  625. }
  626. /**
  627. * hinic_init_hwdev - Initialize the NIC HW
  628. * @pdev: the NIC pci device
  629. *
  630. * Return initialized NIC HW device
  631. *
  632. * Initialize the NIC HW device and return a pointer to it
  633. **/
  634. struct hinic_hwdev *hinic_init_hwdev(struct pci_dev *pdev)
  635. {
  636. struct hinic_pfhwdev *pfhwdev;
  637. struct hinic_hwdev *hwdev;
  638. struct hinic_hwif *hwif;
  639. int err, num_aeqs;
  640. hwif = devm_kzalloc(&pdev->dev, sizeof(*hwif), GFP_KERNEL);
  641. if (!hwif)
  642. return ERR_PTR(-ENOMEM);
  643. err = hinic_init_hwif(hwif, pdev);
  644. if (err) {
  645. dev_err(&pdev->dev, "Failed to init HW interface\n");
  646. return ERR_PTR(err);
  647. }
  648. if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
  649. dev_err(&pdev->dev, "Unsupported PCI Function type\n");
  650. err = -EFAULT;
  651. goto err_func_type;
  652. }
  653. pfhwdev = devm_kzalloc(&pdev->dev, sizeof(*pfhwdev), GFP_KERNEL);
  654. if (!pfhwdev) {
  655. err = -ENOMEM;
  656. goto err_pfhwdev_alloc;
  657. }
  658. hwdev = &pfhwdev->hwdev;
  659. hwdev->hwif = hwif;
  660. err = init_msix(hwdev);
  661. if (err) {
  662. dev_err(&pdev->dev, "Failed to init msix\n");
  663. goto err_init_msix;
  664. }
  665. err = wait_for_outbound_state(hwdev);
  666. if (err) {
  667. dev_warn(&pdev->dev, "outbound - disabled, try again\n");
  668. hinic_outbound_state_set(hwif, HINIC_OUTBOUND_ENABLE);
  669. }
  670. num_aeqs = HINIC_HWIF_NUM_AEQS(hwif);
  671. err = hinic_aeqs_init(&hwdev->aeqs, hwif, num_aeqs,
  672. HINIC_DEFAULT_AEQ_LEN, HINIC_EQ_PAGE_SIZE,
  673. hwdev->msix_entries);
  674. if (err) {
  675. dev_err(&pdev->dev, "Failed to init async event queues\n");
  676. goto err_aeqs_init;
  677. }
  678. err = init_pfhwdev(pfhwdev);
  679. if (err) {
  680. dev_err(&pdev->dev, "Failed to init PF HW device\n");
  681. goto err_init_pfhwdev;
  682. }
  683. err = get_dev_cap(hwdev);
  684. if (err) {
  685. dev_err(&pdev->dev, "Failed to get device capabilities\n");
  686. goto err_dev_cap;
  687. }
  688. err = init_fw_ctxt(hwdev);
  689. if (err) {
  690. dev_err(&pdev->dev, "Failed to init function table\n");
  691. goto err_init_fw_ctxt;
  692. }
  693. err = set_resources_state(hwdev, HINIC_RES_ACTIVE);
  694. if (err) {
  695. dev_err(&pdev->dev, "Failed to set resources state\n");
  696. goto err_resources_state;
  697. }
  698. return hwdev;
  699. err_resources_state:
  700. err_init_fw_ctxt:
  701. err_dev_cap:
  702. free_pfhwdev(pfhwdev);
  703. err_init_pfhwdev:
  704. hinic_aeqs_free(&hwdev->aeqs);
  705. err_aeqs_init:
  706. disable_msix(hwdev);
  707. err_init_msix:
  708. err_pfhwdev_alloc:
  709. err_func_type:
  710. hinic_free_hwif(hwif);
  711. return ERR_PTR(err);
  712. }
  713. /**
  714. * hinic_free_hwdev - Free the NIC HW device
  715. * @hwdev: the NIC HW device
  716. **/
  717. void hinic_free_hwdev(struct hinic_hwdev *hwdev)
  718. {
  719. struct hinic_pfhwdev *pfhwdev = container_of(hwdev,
  720. struct hinic_pfhwdev,
  721. hwdev);
  722. set_resources_state(hwdev, HINIC_RES_CLEAN);
  723. free_pfhwdev(pfhwdev);
  724. hinic_aeqs_free(&hwdev->aeqs);
  725. disable_msix(hwdev);
  726. hinic_free_hwif(hwdev->hwif);
  727. }
  728. /**
  729. * hinic_hwdev_num_qps - return the number QPs available for use
  730. * @hwdev: the NIC HW device
  731. *
  732. * Return number QPs available for use
  733. **/
  734. int hinic_hwdev_num_qps(struct hinic_hwdev *hwdev)
  735. {
  736. struct hinic_cap *nic_cap = &hwdev->nic_cap;
  737. return nic_cap->num_qps;
  738. }
  739. /**
  740. * hinic_hwdev_get_sq - get SQ
  741. * @hwdev: the NIC HW device
  742. * @i: the position of the SQ
  743. *
  744. * Return: the SQ in the i position
  745. **/
  746. struct hinic_sq *hinic_hwdev_get_sq(struct hinic_hwdev *hwdev, int i)
  747. {
  748. struct hinic_func_to_io *func_to_io = &hwdev->func_to_io;
  749. struct hinic_qp *qp = &func_to_io->qps[i];
  750. if (i >= hinic_hwdev_num_qps(hwdev))
  751. return NULL;
  752. return &qp->sq;
  753. }
  754. /**
  755. * hinic_hwdev_get_sq - get RQ
  756. * @hwdev: the NIC HW device
  757. * @i: the position of the RQ
  758. *
  759. * Return: the RQ in the i position
  760. **/
  761. struct hinic_rq *hinic_hwdev_get_rq(struct hinic_hwdev *hwdev, int i)
  762. {
  763. struct hinic_func_to_io *func_to_io = &hwdev->func_to_io;
  764. struct hinic_qp *qp = &func_to_io->qps[i];
  765. if (i >= hinic_hwdev_num_qps(hwdev))
  766. return NULL;
  767. return &qp->rq;
  768. }
  769. /**
  770. * hinic_hwdev_msix_cnt_set - clear message attribute counters for msix entry
  771. * @hwdev: the NIC HW device
  772. * @msix_index: msix_index
  773. *
  774. * Return 0 - Success, negative - Failure
  775. **/
  776. int hinic_hwdev_msix_cnt_set(struct hinic_hwdev *hwdev, u16 msix_index)
  777. {
  778. return hinic_msix_attr_cnt_clear(hwdev->hwif, msix_index);
  779. }
  780. /**
  781. * hinic_hwdev_msix_set - set message attribute for msix entry
  782. * @hwdev: the NIC HW device
  783. * @msix_index: msix_index
  784. * @pending_limit: the maximum pending interrupt events (unit 8)
  785. * @coalesc_timer: coalesc period for interrupt (unit 8 us)
  786. * @lli_timer: replenishing period for low latency credit (unit 8 us)
  787. * @lli_credit_limit: maximum credits for low latency msix messages (unit 8)
  788. * @resend_timer: maximum wait for resending msix (unit coalesc period)
  789. *
  790. * Return 0 - Success, negative - Failure
  791. **/
  792. int hinic_hwdev_msix_set(struct hinic_hwdev *hwdev, u16 msix_index,
  793. u8 pending_limit, u8 coalesc_timer,
  794. u8 lli_timer_cfg, u8 lli_credit_limit,
  795. u8 resend_timer)
  796. {
  797. return hinic_msix_attr_set(hwdev->hwif, msix_index,
  798. pending_limit, coalesc_timer,
  799. lli_timer_cfg, lli_credit_limit,
  800. resend_timer);
  801. }
  802. /**
  803. * hinic_hwdev_hw_ci_addr_set - set cons idx addr and attributes in HW for sq
  804. * @hwdev: the NIC HW device
  805. * @sq: send queue
  806. * @pending_limit: the maximum pending update ci events (unit 8)
  807. * @coalesc_timer: coalesc period for update ci (unit 8 us)
  808. *
  809. * Return 0 - Success, negative - Failure
  810. **/
  811. int hinic_hwdev_hw_ci_addr_set(struct hinic_hwdev *hwdev, struct hinic_sq *sq,
  812. u8 pending_limit, u8 coalesc_timer)
  813. {
  814. struct hinic_qp *qp = container_of(sq, struct hinic_qp, sq);
  815. struct hinic_hwif *hwif = hwdev->hwif;
  816. struct pci_dev *pdev = hwif->pdev;
  817. struct hinic_pfhwdev *pfhwdev;
  818. struct hinic_cmd_hw_ci hw_ci;
  819. if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
  820. dev_err(&pdev->dev, "Unsupported PCI Function type\n");
  821. return -EINVAL;
  822. }
  823. hw_ci.dma_attr_off = 0;
  824. hw_ci.pending_limit = pending_limit;
  825. hw_ci.coalesc_timer = coalesc_timer;
  826. hw_ci.msix_en = 1;
  827. hw_ci.msix_entry_idx = sq->msix_entry;
  828. hw_ci.func_idx = HINIC_HWIF_FUNC_IDX(hwif);
  829. hw_ci.sq_id = qp->q_id;
  830. hw_ci.ci_addr = ADDR_IN_4BYTES(sq->hw_ci_dma_addr);
  831. pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
  832. return hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt,
  833. HINIC_MOD_COMM,
  834. HINIC_COMM_CMD_SQ_HI_CI_SET,
  835. &hw_ci, sizeof(hw_ci), NULL,
  836. NULL, HINIC_MGMT_MSG_SYNC);
  837. }