driver.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  1. /*
  2. * Copyright(c) 2015, 2016 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <linux/spinlock.h>
  48. #include <linux/pci.h>
  49. #include <linux/io.h>
  50. #include <linux/delay.h>
  51. #include <linux/netdevice.h>
  52. #include <linux/vmalloc.h>
  53. #include <linux/module.h>
  54. #include <linux/prefetch.h>
  55. #include <rdma/ib_verbs.h>
  56. #include "hfi.h"
  57. #include "trace.h"
  58. #include "qp.h"
  59. #include "sdma.h"
  60. #undef pr_fmt
  61. #define pr_fmt(fmt) DRIVER_NAME ": " fmt
  62. /*
  63. * The size has to be longer than this string, so we can append
  64. * board/chip information to it in the initialization code.
  65. */
  66. const char ib_hfi1_version[] = HFI1_DRIVER_VERSION "\n";
  67. DEFINE_SPINLOCK(hfi1_devs_lock);
  68. LIST_HEAD(hfi1_dev_list);
  69. DEFINE_MUTEX(hfi1_mutex); /* general driver use */
  70. unsigned int hfi1_max_mtu = HFI1_DEFAULT_MAX_MTU;
  71. module_param_named(max_mtu, hfi1_max_mtu, uint, S_IRUGO);
  72. MODULE_PARM_DESC(max_mtu, "Set max MTU bytes, default is " __stringify(
  73. HFI1_DEFAULT_MAX_MTU));
  74. unsigned int hfi1_cu = 1;
  75. module_param_named(cu, hfi1_cu, uint, S_IRUGO);
  76. MODULE_PARM_DESC(cu, "Credit return units");
  77. unsigned long hfi1_cap_mask = HFI1_CAP_MASK_DEFAULT;
  78. static int hfi1_caps_set(const char *, const struct kernel_param *);
  79. static int hfi1_caps_get(char *, const struct kernel_param *);
  80. static const struct kernel_param_ops cap_ops = {
  81. .set = hfi1_caps_set,
  82. .get = hfi1_caps_get
  83. };
  84. module_param_cb(cap_mask, &cap_ops, &hfi1_cap_mask, S_IWUSR | S_IRUGO);
  85. MODULE_PARM_DESC(cap_mask, "Bit mask of enabled/disabled HW features");
  86. MODULE_LICENSE("Dual BSD/GPL");
  87. MODULE_DESCRIPTION("Intel Omni-Path Architecture driver");
  88. MODULE_VERSION(HFI1_DRIVER_VERSION);
  89. /*
  90. * MAX_PKT_RCV is the max # if packets processed per receive interrupt.
  91. */
  92. #define MAX_PKT_RECV 64
  93. /*
  94. * MAX_PKT_THREAD_RCV is the max # of packets processed before
  95. * the qp_wait_list queue is flushed.
  96. */
  97. #define MAX_PKT_RECV_THREAD (MAX_PKT_RECV * 4)
  98. #define EGR_HEAD_UPDATE_THRESHOLD 16
  99. struct hfi1_ib_stats hfi1_stats;
  100. static int hfi1_caps_set(const char *val, const struct kernel_param *kp)
  101. {
  102. int ret = 0;
  103. unsigned long *cap_mask_ptr = (unsigned long *)kp->arg,
  104. cap_mask = *cap_mask_ptr, value, diff,
  105. write_mask = ((HFI1_CAP_WRITABLE_MASK << HFI1_CAP_USER_SHIFT) |
  106. HFI1_CAP_WRITABLE_MASK);
  107. ret = kstrtoul(val, 0, &value);
  108. if (ret) {
  109. pr_warn("Invalid module parameter value for 'cap_mask'\n");
  110. goto done;
  111. }
  112. /* Get the changed bits (except the locked bit) */
  113. diff = value ^ (cap_mask & ~HFI1_CAP_LOCKED_SMASK);
  114. /* Remove any bits that are not allowed to change after driver load */
  115. if (HFI1_CAP_LOCKED() && (diff & ~write_mask)) {
  116. pr_warn("Ignoring non-writable capability bits %#lx\n",
  117. diff & ~write_mask);
  118. diff &= write_mask;
  119. }
  120. /* Mask off any reserved bits */
  121. diff &= ~HFI1_CAP_RESERVED_MASK;
  122. /* Clear any previously set and changing bits */
  123. cap_mask &= ~diff;
  124. /* Update the bits with the new capability */
  125. cap_mask |= (value & diff);
  126. /* Check for any kernel/user restrictions */
  127. diff = (cap_mask & (HFI1_CAP_MUST_HAVE_KERN << HFI1_CAP_USER_SHIFT)) ^
  128. ((cap_mask & HFI1_CAP_MUST_HAVE_KERN) << HFI1_CAP_USER_SHIFT);
  129. cap_mask &= ~diff;
  130. /* Set the bitmask to the final set */
  131. *cap_mask_ptr = cap_mask;
  132. done:
  133. return ret;
  134. }
  135. static int hfi1_caps_get(char *buffer, const struct kernel_param *kp)
  136. {
  137. unsigned long cap_mask = *(unsigned long *)kp->arg;
  138. cap_mask &= ~HFI1_CAP_LOCKED_SMASK;
  139. cap_mask |= ((cap_mask & HFI1_CAP_K2U) << HFI1_CAP_USER_SHIFT);
  140. return scnprintf(buffer, PAGE_SIZE, "0x%lx", cap_mask);
  141. }
  142. const char *get_unit_name(int unit)
  143. {
  144. static char iname[16];
  145. snprintf(iname, sizeof(iname), DRIVER_NAME "_%u", unit);
  146. return iname;
  147. }
  148. const char *get_card_name(struct rvt_dev_info *rdi)
  149. {
  150. struct hfi1_ibdev *ibdev = container_of(rdi, struct hfi1_ibdev, rdi);
  151. struct hfi1_devdata *dd = container_of(ibdev,
  152. struct hfi1_devdata, verbs_dev);
  153. return get_unit_name(dd->unit);
  154. }
  155. struct pci_dev *get_pci_dev(struct rvt_dev_info *rdi)
  156. {
  157. struct hfi1_ibdev *ibdev = container_of(rdi, struct hfi1_ibdev, rdi);
  158. struct hfi1_devdata *dd = container_of(ibdev,
  159. struct hfi1_devdata, verbs_dev);
  160. return dd->pcidev;
  161. }
  162. /*
  163. * Return count of units with at least one port ACTIVE.
  164. */
  165. int hfi1_count_active_units(void)
  166. {
  167. struct hfi1_devdata *dd;
  168. struct hfi1_pportdata *ppd;
  169. unsigned long flags;
  170. int pidx, nunits_active = 0;
  171. spin_lock_irqsave(&hfi1_devs_lock, flags);
  172. list_for_each_entry(dd, &hfi1_dev_list, list) {
  173. if (!(dd->flags & HFI1_PRESENT) || !dd->kregbase)
  174. continue;
  175. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  176. ppd = dd->pport + pidx;
  177. if (ppd->lid && ppd->linkup) {
  178. nunits_active++;
  179. break;
  180. }
  181. }
  182. }
  183. spin_unlock_irqrestore(&hfi1_devs_lock, flags);
  184. return nunits_active;
  185. }
  186. /*
  187. * Return count of all units, optionally return in arguments
  188. * the number of usable (present) units, and the number of
  189. * ports that are up.
  190. */
  191. int hfi1_count_units(int *npresentp, int *nupp)
  192. {
  193. int nunits = 0, npresent = 0, nup = 0;
  194. struct hfi1_devdata *dd;
  195. unsigned long flags;
  196. int pidx;
  197. struct hfi1_pportdata *ppd;
  198. spin_lock_irqsave(&hfi1_devs_lock, flags);
  199. list_for_each_entry(dd, &hfi1_dev_list, list) {
  200. nunits++;
  201. if ((dd->flags & HFI1_PRESENT) && dd->kregbase)
  202. npresent++;
  203. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  204. ppd = dd->pport + pidx;
  205. if (ppd->lid && ppd->linkup)
  206. nup++;
  207. }
  208. }
  209. spin_unlock_irqrestore(&hfi1_devs_lock, flags);
  210. if (npresentp)
  211. *npresentp = npresent;
  212. if (nupp)
  213. *nupp = nup;
  214. return nunits;
  215. }
  216. /*
  217. * Get address of eager buffer from it's index (allocated in chunks, not
  218. * contiguous).
  219. */
  220. static inline void *get_egrbuf(const struct hfi1_ctxtdata *rcd, u64 rhf,
  221. u8 *update)
  222. {
  223. u32 idx = rhf_egr_index(rhf), offset = rhf_egr_buf_offset(rhf);
  224. *update |= !(idx & (rcd->egrbufs.threshold - 1)) && !offset;
  225. return (void *)(((u64)(rcd->egrbufs.rcvtids[idx].addr)) +
  226. (offset * RCV_BUF_BLOCK_SIZE));
  227. }
  228. /*
  229. * Validate and encode the a given RcvArray Buffer size.
  230. * The function will check whether the given size falls within
  231. * allowed size ranges for the respective type and, optionally,
  232. * return the proper encoding.
  233. */
  234. int hfi1_rcvbuf_validate(u32 size, u8 type, u16 *encoded)
  235. {
  236. if (unlikely(!PAGE_ALIGNED(size)))
  237. return 0;
  238. if (unlikely(size < MIN_EAGER_BUFFER))
  239. return 0;
  240. if (size >
  241. (type == PT_EAGER ? MAX_EAGER_BUFFER : MAX_EXPECTED_BUFFER))
  242. return 0;
  243. if (encoded)
  244. *encoded = ilog2(size / PAGE_SIZE) + 1;
  245. return 1;
  246. }
  247. static void rcv_hdrerr(struct hfi1_ctxtdata *rcd, struct hfi1_pportdata *ppd,
  248. struct hfi1_packet *packet)
  249. {
  250. struct ib_header *rhdr = packet->hdr;
  251. u32 rte = rhf_rcv_type_err(packet->rhf);
  252. int lnh = be16_to_cpu(rhdr->lrh[0]) & 3;
  253. struct hfi1_ibport *ibp = rcd_to_iport(rcd);
  254. struct hfi1_devdata *dd = ppd->dd;
  255. struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
  256. if (packet->rhf & (RHF_VCRC_ERR | RHF_ICRC_ERR))
  257. return;
  258. if (packet->rhf & RHF_TID_ERR) {
  259. /* For TIDERR and RC QPs preemptively schedule a NAK */
  260. struct ib_other_headers *ohdr = NULL;
  261. u32 tlen = rhf_pkt_len(packet->rhf); /* in bytes */
  262. u16 lid = be16_to_cpu(rhdr->lrh[1]);
  263. u32 qp_num;
  264. u32 rcv_flags = 0;
  265. /* Sanity check packet */
  266. if (tlen < 24)
  267. goto drop;
  268. /* Check for GRH */
  269. if (lnh == HFI1_LRH_BTH) {
  270. ohdr = &rhdr->u.oth;
  271. } else if (lnh == HFI1_LRH_GRH) {
  272. u32 vtf;
  273. ohdr = &rhdr->u.l.oth;
  274. if (rhdr->u.l.grh.next_hdr != IB_GRH_NEXT_HDR)
  275. goto drop;
  276. vtf = be32_to_cpu(rhdr->u.l.grh.version_tclass_flow);
  277. if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
  278. goto drop;
  279. rcv_flags |= HFI1_HAS_GRH;
  280. } else {
  281. goto drop;
  282. }
  283. /* Get the destination QP number. */
  284. qp_num = be32_to_cpu(ohdr->bth[1]) & RVT_QPN_MASK;
  285. if (lid < be16_to_cpu(IB_MULTICAST_LID_BASE)) {
  286. struct rvt_qp *qp;
  287. unsigned long flags;
  288. rcu_read_lock();
  289. qp = rvt_lookup_qpn(rdi, &ibp->rvp, qp_num);
  290. if (!qp) {
  291. rcu_read_unlock();
  292. goto drop;
  293. }
  294. /*
  295. * Handle only RC QPs - for other QP types drop error
  296. * packet.
  297. */
  298. spin_lock_irqsave(&qp->r_lock, flags);
  299. /* Check for valid receive state. */
  300. if (!(ib_rvt_state_ops[qp->state] &
  301. RVT_PROCESS_RECV_OK)) {
  302. ibp->rvp.n_pkt_drops++;
  303. }
  304. switch (qp->ibqp.qp_type) {
  305. case IB_QPT_RC:
  306. hfi1_rc_hdrerr(
  307. rcd,
  308. rhdr,
  309. rcv_flags,
  310. qp);
  311. break;
  312. default:
  313. /* For now don't handle any other QP types */
  314. break;
  315. }
  316. spin_unlock_irqrestore(&qp->r_lock, flags);
  317. rcu_read_unlock();
  318. } /* Unicast QP */
  319. } /* Valid packet with TIDErr */
  320. /* handle "RcvTypeErr" flags */
  321. switch (rte) {
  322. case RHF_RTE_ERROR_OP_CODE_ERR:
  323. {
  324. u32 opcode;
  325. void *ebuf = NULL;
  326. __be32 *bth = NULL;
  327. if (rhf_use_egr_bfr(packet->rhf))
  328. ebuf = packet->ebuf;
  329. if (!ebuf)
  330. goto drop; /* this should never happen */
  331. if (lnh == HFI1_LRH_BTH)
  332. bth = (__be32 *)ebuf;
  333. else if (lnh == HFI1_LRH_GRH)
  334. bth = (__be32 *)((char *)ebuf + sizeof(struct ib_grh));
  335. else
  336. goto drop;
  337. opcode = be32_to_cpu(bth[0]) >> 24;
  338. opcode &= 0xff;
  339. if (opcode == IB_OPCODE_CNP) {
  340. /*
  341. * Only in pre-B0 h/w is the CNP_OPCODE handled
  342. * via this code path.
  343. */
  344. struct rvt_qp *qp = NULL;
  345. u32 lqpn, rqpn;
  346. u16 rlid;
  347. u8 svc_type, sl, sc5;
  348. sc5 = hdr2sc(rhdr, packet->rhf);
  349. sl = ibp->sc_to_sl[sc5];
  350. lqpn = be32_to_cpu(bth[1]) & RVT_QPN_MASK;
  351. rcu_read_lock();
  352. qp = rvt_lookup_qpn(rdi, &ibp->rvp, lqpn);
  353. if (!qp) {
  354. rcu_read_unlock();
  355. goto drop;
  356. }
  357. switch (qp->ibqp.qp_type) {
  358. case IB_QPT_UD:
  359. rlid = 0;
  360. rqpn = 0;
  361. svc_type = IB_CC_SVCTYPE_UD;
  362. break;
  363. case IB_QPT_UC:
  364. rlid = be16_to_cpu(rhdr->lrh[3]);
  365. rqpn = qp->remote_qpn;
  366. svc_type = IB_CC_SVCTYPE_UC;
  367. break;
  368. default:
  369. goto drop;
  370. }
  371. process_becn(ppd, sl, rlid, lqpn, rqpn, svc_type);
  372. rcu_read_unlock();
  373. }
  374. packet->rhf &= ~RHF_RCV_TYPE_ERR_SMASK;
  375. break;
  376. }
  377. default:
  378. break;
  379. }
  380. drop:
  381. return;
  382. }
  383. static inline void init_packet(struct hfi1_ctxtdata *rcd,
  384. struct hfi1_packet *packet)
  385. {
  386. packet->rsize = rcd->rcvhdrqentsize; /* words */
  387. packet->maxcnt = rcd->rcvhdrq_cnt * packet->rsize; /* words */
  388. packet->rcd = rcd;
  389. packet->updegr = 0;
  390. packet->etail = -1;
  391. packet->rhf_addr = get_rhf_addr(rcd);
  392. packet->rhf = rhf_to_cpu(packet->rhf_addr);
  393. packet->rhqoff = rcd->head;
  394. packet->numpkt = 0;
  395. packet->rcv_flags = 0;
  396. }
  397. void hfi1_process_ecn_slowpath(struct rvt_qp *qp, struct hfi1_packet *pkt,
  398. bool do_cnp)
  399. {
  400. struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
  401. struct ib_header *hdr = pkt->hdr;
  402. struct ib_other_headers *ohdr = pkt->ohdr;
  403. struct ib_grh *grh = NULL;
  404. u32 rqpn = 0, bth1;
  405. u16 rlid, dlid = be16_to_cpu(hdr->lrh[1]);
  406. u8 sc, svc_type;
  407. bool is_mcast = false;
  408. if (pkt->rcv_flags & HFI1_HAS_GRH)
  409. grh = &hdr->u.l.grh;
  410. switch (qp->ibqp.qp_type) {
  411. case IB_QPT_SMI:
  412. case IB_QPT_GSI:
  413. case IB_QPT_UD:
  414. rlid = be16_to_cpu(hdr->lrh[3]);
  415. rqpn = be32_to_cpu(ohdr->u.ud.deth[1]) & RVT_QPN_MASK;
  416. svc_type = IB_CC_SVCTYPE_UD;
  417. is_mcast = (dlid > be16_to_cpu(IB_MULTICAST_LID_BASE)) &&
  418. (dlid != be16_to_cpu(IB_LID_PERMISSIVE));
  419. break;
  420. case IB_QPT_UC:
  421. rlid = qp->remote_ah_attr.dlid;
  422. rqpn = qp->remote_qpn;
  423. svc_type = IB_CC_SVCTYPE_UC;
  424. break;
  425. case IB_QPT_RC:
  426. rlid = qp->remote_ah_attr.dlid;
  427. rqpn = qp->remote_qpn;
  428. svc_type = IB_CC_SVCTYPE_RC;
  429. break;
  430. default:
  431. return;
  432. }
  433. sc = hdr2sc(hdr, pkt->rhf);
  434. bth1 = be32_to_cpu(ohdr->bth[1]);
  435. if (do_cnp && (bth1 & HFI1_FECN_SMASK)) {
  436. u16 pkey = (u16)be32_to_cpu(ohdr->bth[0]);
  437. return_cnp(ibp, qp, rqpn, pkey, dlid, rlid, sc, grh);
  438. }
  439. if (!is_mcast && (bth1 & HFI1_BECN_SMASK)) {
  440. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  441. u32 lqpn = bth1 & RVT_QPN_MASK;
  442. u8 sl = ibp->sc_to_sl[sc];
  443. process_becn(ppd, sl, rlid, lqpn, rqpn, svc_type);
  444. }
  445. }
  446. struct ps_mdata {
  447. struct hfi1_ctxtdata *rcd;
  448. u32 rsize;
  449. u32 maxcnt;
  450. u32 ps_head;
  451. u32 ps_tail;
  452. u32 ps_seq;
  453. };
  454. static inline void init_ps_mdata(struct ps_mdata *mdata,
  455. struct hfi1_packet *packet)
  456. {
  457. struct hfi1_ctxtdata *rcd = packet->rcd;
  458. mdata->rcd = rcd;
  459. mdata->rsize = packet->rsize;
  460. mdata->maxcnt = packet->maxcnt;
  461. mdata->ps_head = packet->rhqoff;
  462. if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
  463. mdata->ps_tail = get_rcvhdrtail(rcd);
  464. if (rcd->ctxt == HFI1_CTRL_CTXT)
  465. mdata->ps_seq = rcd->seq_cnt;
  466. else
  467. mdata->ps_seq = 0; /* not used with DMA_RTAIL */
  468. } else {
  469. mdata->ps_tail = 0; /* used only with DMA_RTAIL*/
  470. mdata->ps_seq = rcd->seq_cnt;
  471. }
  472. }
  473. static inline int ps_done(struct ps_mdata *mdata, u64 rhf,
  474. struct hfi1_ctxtdata *rcd)
  475. {
  476. if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL))
  477. return mdata->ps_head == mdata->ps_tail;
  478. return mdata->ps_seq != rhf_rcv_seq(rhf);
  479. }
  480. static inline int ps_skip(struct ps_mdata *mdata, u64 rhf,
  481. struct hfi1_ctxtdata *rcd)
  482. {
  483. /*
  484. * Control context can potentially receive an invalid rhf.
  485. * Drop such packets.
  486. */
  487. if ((rcd->ctxt == HFI1_CTRL_CTXT) && (mdata->ps_head != mdata->ps_tail))
  488. return mdata->ps_seq != rhf_rcv_seq(rhf);
  489. return 0;
  490. }
  491. static inline void update_ps_mdata(struct ps_mdata *mdata,
  492. struct hfi1_ctxtdata *rcd)
  493. {
  494. mdata->ps_head += mdata->rsize;
  495. if (mdata->ps_head >= mdata->maxcnt)
  496. mdata->ps_head = 0;
  497. /* Control context must do seq counting */
  498. if (!HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL) ||
  499. (rcd->ctxt == HFI1_CTRL_CTXT)) {
  500. if (++mdata->ps_seq > 13)
  501. mdata->ps_seq = 1;
  502. }
  503. }
  504. /*
  505. * prescan_rxq - search through the receive queue looking for packets
  506. * containing Excplicit Congestion Notifications (FECNs, or BECNs).
  507. * When an ECN is found, process the Congestion Notification, and toggle
  508. * it off.
  509. * This is declared as a macro to allow quick checking of the port to avoid
  510. * the overhead of a function call if not enabled.
  511. */
  512. #define prescan_rxq(rcd, packet) \
  513. do { \
  514. if (rcd->ppd->cc_prescan) \
  515. __prescan_rxq(packet); \
  516. } while (0)
  517. static void __prescan_rxq(struct hfi1_packet *packet)
  518. {
  519. struct hfi1_ctxtdata *rcd = packet->rcd;
  520. struct ps_mdata mdata;
  521. init_ps_mdata(&mdata, packet);
  522. while (1) {
  523. struct hfi1_devdata *dd = rcd->dd;
  524. struct hfi1_ibport *ibp = rcd_to_iport(rcd);
  525. __le32 *rhf_addr = (__le32 *)rcd->rcvhdrq + mdata.ps_head +
  526. dd->rhf_offset;
  527. struct rvt_qp *qp;
  528. struct ib_header *hdr;
  529. struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
  530. u64 rhf = rhf_to_cpu(rhf_addr);
  531. u32 etype = rhf_rcv_type(rhf), qpn, bth1;
  532. int is_ecn = 0;
  533. u8 lnh;
  534. if (ps_done(&mdata, rhf, rcd))
  535. break;
  536. if (ps_skip(&mdata, rhf, rcd))
  537. goto next;
  538. if (etype != RHF_RCV_TYPE_IB)
  539. goto next;
  540. packet->hdr = hfi1_get_msgheader(dd, rhf_addr);
  541. hdr = packet->hdr;
  542. lnh = be16_to_cpu(hdr->lrh[0]) & 3;
  543. if (lnh == HFI1_LRH_BTH) {
  544. packet->ohdr = &hdr->u.oth;
  545. } else if (lnh == HFI1_LRH_GRH) {
  546. packet->ohdr = &hdr->u.l.oth;
  547. packet->rcv_flags |= HFI1_HAS_GRH;
  548. } else {
  549. goto next; /* just in case */
  550. }
  551. bth1 = be32_to_cpu(packet->ohdr->bth[1]);
  552. is_ecn = !!(bth1 & (HFI1_FECN_SMASK | HFI1_BECN_SMASK));
  553. if (!is_ecn)
  554. goto next;
  555. qpn = bth1 & RVT_QPN_MASK;
  556. rcu_read_lock();
  557. qp = rvt_lookup_qpn(rdi, &ibp->rvp, qpn);
  558. if (!qp) {
  559. rcu_read_unlock();
  560. goto next;
  561. }
  562. process_ecn(qp, packet, true);
  563. rcu_read_unlock();
  564. /* turn off BECN, FECN */
  565. bth1 &= ~(HFI1_FECN_SMASK | HFI1_BECN_SMASK);
  566. packet->ohdr->bth[1] = cpu_to_be32(bth1);
  567. next:
  568. update_ps_mdata(&mdata, rcd);
  569. }
  570. }
  571. static void process_rcv_qp_work(struct hfi1_ctxtdata *rcd)
  572. {
  573. struct rvt_qp *qp, *nqp;
  574. /*
  575. * Iterate over all QPs waiting to respond.
  576. * The list won't change since the IRQ is only run on one CPU.
  577. */
  578. list_for_each_entry_safe(qp, nqp, &rcd->qp_wait_list, rspwait) {
  579. list_del_init(&qp->rspwait);
  580. if (qp->r_flags & RVT_R_RSP_NAK) {
  581. qp->r_flags &= ~RVT_R_RSP_NAK;
  582. hfi1_send_rc_ack(rcd, qp, 0);
  583. }
  584. if (qp->r_flags & RVT_R_RSP_SEND) {
  585. unsigned long flags;
  586. qp->r_flags &= ~RVT_R_RSP_SEND;
  587. spin_lock_irqsave(&qp->s_lock, flags);
  588. if (ib_rvt_state_ops[qp->state] &
  589. RVT_PROCESS_OR_FLUSH_SEND)
  590. hfi1_schedule_send(qp);
  591. spin_unlock_irqrestore(&qp->s_lock, flags);
  592. }
  593. rvt_put_qp(qp);
  594. }
  595. }
  596. static noinline int max_packet_exceeded(struct hfi1_packet *packet, int thread)
  597. {
  598. if (thread) {
  599. if ((packet->numpkt & (MAX_PKT_RECV_THREAD - 1)) == 0)
  600. /* allow defered processing */
  601. process_rcv_qp_work(packet->rcd);
  602. cond_resched();
  603. return RCV_PKT_OK;
  604. } else {
  605. this_cpu_inc(*packet->rcd->dd->rcv_limit);
  606. return RCV_PKT_LIMIT;
  607. }
  608. }
  609. static inline int check_max_packet(struct hfi1_packet *packet, int thread)
  610. {
  611. int ret = RCV_PKT_OK;
  612. if (unlikely((packet->numpkt & (MAX_PKT_RECV - 1)) == 0))
  613. ret = max_packet_exceeded(packet, thread);
  614. return ret;
  615. }
  616. static noinline int skip_rcv_packet(struct hfi1_packet *packet, int thread)
  617. {
  618. int ret;
  619. /* Set up for the next packet */
  620. packet->rhqoff += packet->rsize;
  621. if (packet->rhqoff >= packet->maxcnt)
  622. packet->rhqoff = 0;
  623. packet->numpkt++;
  624. ret = check_max_packet(packet, thread);
  625. packet->rhf_addr = (__le32 *)packet->rcd->rcvhdrq + packet->rhqoff +
  626. packet->rcd->dd->rhf_offset;
  627. packet->rhf = rhf_to_cpu(packet->rhf_addr);
  628. return ret;
  629. }
  630. static inline int process_rcv_packet(struct hfi1_packet *packet, int thread)
  631. {
  632. int ret;
  633. packet->hdr = hfi1_get_msgheader(packet->rcd->dd,
  634. packet->rhf_addr);
  635. packet->hlen = (u8 *)packet->rhf_addr - (u8 *)packet->hdr;
  636. packet->etype = rhf_rcv_type(packet->rhf);
  637. /* total length */
  638. packet->tlen = rhf_pkt_len(packet->rhf); /* in bytes */
  639. /* retrieve eager buffer details */
  640. packet->ebuf = NULL;
  641. if (rhf_use_egr_bfr(packet->rhf)) {
  642. packet->etail = rhf_egr_index(packet->rhf);
  643. packet->ebuf = get_egrbuf(packet->rcd, packet->rhf,
  644. &packet->updegr);
  645. /*
  646. * Prefetch the contents of the eager buffer. It is
  647. * OK to send a negative length to prefetch_range().
  648. * The +2 is the size of the RHF.
  649. */
  650. prefetch_range(packet->ebuf,
  651. packet->tlen - ((packet->rcd->rcvhdrqentsize -
  652. (rhf_hdrq_offset(packet->rhf)
  653. + 2)) * 4));
  654. }
  655. /*
  656. * Call a type specific handler for the packet. We
  657. * should be able to trust that etype won't be beyond
  658. * the range of valid indexes. If so something is really
  659. * wrong and we can probably just let things come
  660. * crashing down. There is no need to eat another
  661. * comparison in this performance critical code.
  662. */
  663. packet->rcd->dd->rhf_rcv_function_map[packet->etype](packet);
  664. packet->numpkt++;
  665. /* Set up for the next packet */
  666. packet->rhqoff += packet->rsize;
  667. if (packet->rhqoff >= packet->maxcnt)
  668. packet->rhqoff = 0;
  669. ret = check_max_packet(packet, thread);
  670. packet->rhf_addr = (__le32 *)packet->rcd->rcvhdrq + packet->rhqoff +
  671. packet->rcd->dd->rhf_offset;
  672. packet->rhf = rhf_to_cpu(packet->rhf_addr);
  673. return ret;
  674. }
  675. static inline void process_rcv_update(int last, struct hfi1_packet *packet)
  676. {
  677. /*
  678. * Update head regs etc., every 16 packets, if not last pkt,
  679. * to help prevent rcvhdrq overflows, when many packets
  680. * are processed and queue is nearly full.
  681. * Don't request an interrupt for intermediate updates.
  682. */
  683. if (!last && !(packet->numpkt & 0xf)) {
  684. update_usrhead(packet->rcd, packet->rhqoff, packet->updegr,
  685. packet->etail, 0, 0);
  686. packet->updegr = 0;
  687. }
  688. packet->rcv_flags = 0;
  689. }
  690. static inline void finish_packet(struct hfi1_packet *packet)
  691. {
  692. /*
  693. * Nothing we need to free for the packet.
  694. *
  695. * The only thing we need to do is a final update and call for an
  696. * interrupt
  697. */
  698. update_usrhead(packet->rcd, packet->rcd->head, packet->updegr,
  699. packet->etail, rcv_intr_dynamic, packet->numpkt);
  700. }
  701. /*
  702. * Handle receive interrupts when using the no dma rtail option.
  703. */
  704. int handle_receive_interrupt_nodma_rtail(struct hfi1_ctxtdata *rcd, int thread)
  705. {
  706. u32 seq;
  707. int last = RCV_PKT_OK;
  708. struct hfi1_packet packet;
  709. init_packet(rcd, &packet);
  710. seq = rhf_rcv_seq(packet.rhf);
  711. if (seq != rcd->seq_cnt) {
  712. last = RCV_PKT_DONE;
  713. goto bail;
  714. }
  715. prescan_rxq(rcd, &packet);
  716. while (last == RCV_PKT_OK) {
  717. last = process_rcv_packet(&packet, thread);
  718. seq = rhf_rcv_seq(packet.rhf);
  719. if (++rcd->seq_cnt > 13)
  720. rcd->seq_cnt = 1;
  721. if (seq != rcd->seq_cnt)
  722. last = RCV_PKT_DONE;
  723. process_rcv_update(last, &packet);
  724. }
  725. process_rcv_qp_work(rcd);
  726. rcd->head = packet.rhqoff;
  727. bail:
  728. finish_packet(&packet);
  729. return last;
  730. }
  731. int handle_receive_interrupt_dma_rtail(struct hfi1_ctxtdata *rcd, int thread)
  732. {
  733. u32 hdrqtail;
  734. int last = RCV_PKT_OK;
  735. struct hfi1_packet packet;
  736. init_packet(rcd, &packet);
  737. hdrqtail = get_rcvhdrtail(rcd);
  738. if (packet.rhqoff == hdrqtail) {
  739. last = RCV_PKT_DONE;
  740. goto bail;
  741. }
  742. smp_rmb(); /* prevent speculative reads of dma'ed hdrq */
  743. prescan_rxq(rcd, &packet);
  744. while (last == RCV_PKT_OK) {
  745. last = process_rcv_packet(&packet, thread);
  746. if (packet.rhqoff == hdrqtail)
  747. last = RCV_PKT_DONE;
  748. process_rcv_update(last, &packet);
  749. }
  750. process_rcv_qp_work(rcd);
  751. rcd->head = packet.rhqoff;
  752. bail:
  753. finish_packet(&packet);
  754. return last;
  755. }
  756. static inline void set_all_nodma_rtail(struct hfi1_devdata *dd)
  757. {
  758. int i;
  759. for (i = HFI1_CTRL_CTXT + 1; i < dd->first_user_ctxt; i++)
  760. dd->rcd[i]->do_interrupt =
  761. &handle_receive_interrupt_nodma_rtail;
  762. }
  763. static inline void set_all_dma_rtail(struct hfi1_devdata *dd)
  764. {
  765. int i;
  766. for (i = HFI1_CTRL_CTXT + 1; i < dd->first_user_ctxt; i++)
  767. dd->rcd[i]->do_interrupt =
  768. &handle_receive_interrupt_dma_rtail;
  769. }
  770. void set_all_slowpath(struct hfi1_devdata *dd)
  771. {
  772. int i;
  773. /* HFI1_CTRL_CTXT must always use the slow path interrupt handler */
  774. for (i = HFI1_CTRL_CTXT + 1; i < dd->first_user_ctxt; i++)
  775. dd->rcd[i]->do_interrupt = &handle_receive_interrupt;
  776. }
  777. static inline int set_armed_to_active(struct hfi1_ctxtdata *rcd,
  778. struct hfi1_packet *packet,
  779. struct hfi1_devdata *dd)
  780. {
  781. struct work_struct *lsaw = &rcd->ppd->linkstate_active_work;
  782. struct ib_header *hdr = hfi1_get_msgheader(packet->rcd->dd,
  783. packet->rhf_addr);
  784. u8 etype = rhf_rcv_type(packet->rhf);
  785. if (etype == RHF_RCV_TYPE_IB && hdr2sc(hdr, packet->rhf) != 0xf) {
  786. int hwstate = read_logical_state(dd);
  787. if (hwstate != LSTATE_ACTIVE) {
  788. dd_dev_info(dd, "Unexpected link state %d\n", hwstate);
  789. return 0;
  790. }
  791. queue_work(rcd->ppd->hfi1_wq, lsaw);
  792. return 1;
  793. }
  794. return 0;
  795. }
  796. /*
  797. * handle_receive_interrupt - receive a packet
  798. * @rcd: the context
  799. *
  800. * Called from interrupt handler for errors or receive interrupt.
  801. * This is the slow path interrupt handler.
  802. */
  803. int handle_receive_interrupt(struct hfi1_ctxtdata *rcd, int thread)
  804. {
  805. struct hfi1_devdata *dd = rcd->dd;
  806. u32 hdrqtail;
  807. int needset, last = RCV_PKT_OK;
  808. struct hfi1_packet packet;
  809. int skip_pkt = 0;
  810. /* Control context will always use the slow path interrupt handler */
  811. needset = (rcd->ctxt == HFI1_CTRL_CTXT) ? 0 : 1;
  812. init_packet(rcd, &packet);
  813. if (!HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
  814. u32 seq = rhf_rcv_seq(packet.rhf);
  815. if (seq != rcd->seq_cnt) {
  816. last = RCV_PKT_DONE;
  817. goto bail;
  818. }
  819. hdrqtail = 0;
  820. } else {
  821. hdrqtail = get_rcvhdrtail(rcd);
  822. if (packet.rhqoff == hdrqtail) {
  823. last = RCV_PKT_DONE;
  824. goto bail;
  825. }
  826. smp_rmb(); /* prevent speculative reads of dma'ed hdrq */
  827. /*
  828. * Control context can potentially receive an invalid
  829. * rhf. Drop such packets.
  830. */
  831. if (rcd->ctxt == HFI1_CTRL_CTXT) {
  832. u32 seq = rhf_rcv_seq(packet.rhf);
  833. if (seq != rcd->seq_cnt)
  834. skip_pkt = 1;
  835. }
  836. }
  837. prescan_rxq(rcd, &packet);
  838. while (last == RCV_PKT_OK) {
  839. if (unlikely(dd->do_drop &&
  840. atomic_xchg(&dd->drop_packet, DROP_PACKET_OFF) ==
  841. DROP_PACKET_ON)) {
  842. dd->do_drop = 0;
  843. /* On to the next packet */
  844. packet.rhqoff += packet.rsize;
  845. packet.rhf_addr = (__le32 *)rcd->rcvhdrq +
  846. packet.rhqoff +
  847. dd->rhf_offset;
  848. packet.rhf = rhf_to_cpu(packet.rhf_addr);
  849. } else if (skip_pkt) {
  850. last = skip_rcv_packet(&packet, thread);
  851. skip_pkt = 0;
  852. } else {
  853. /* Auto activate link on non-SC15 packet receive */
  854. if (unlikely(rcd->ppd->host_link_state ==
  855. HLS_UP_ARMED) &&
  856. set_armed_to_active(rcd, &packet, dd))
  857. goto bail;
  858. last = process_rcv_packet(&packet, thread);
  859. }
  860. if (!HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
  861. u32 seq = rhf_rcv_seq(packet.rhf);
  862. if (++rcd->seq_cnt > 13)
  863. rcd->seq_cnt = 1;
  864. if (seq != rcd->seq_cnt)
  865. last = RCV_PKT_DONE;
  866. if (needset) {
  867. dd_dev_info(dd, "Switching to NO_DMA_RTAIL\n");
  868. set_all_nodma_rtail(dd);
  869. needset = 0;
  870. }
  871. } else {
  872. if (packet.rhqoff == hdrqtail)
  873. last = RCV_PKT_DONE;
  874. /*
  875. * Control context can potentially receive an invalid
  876. * rhf. Drop such packets.
  877. */
  878. if (rcd->ctxt == HFI1_CTRL_CTXT) {
  879. u32 seq = rhf_rcv_seq(packet.rhf);
  880. if (++rcd->seq_cnt > 13)
  881. rcd->seq_cnt = 1;
  882. if (!last && (seq != rcd->seq_cnt))
  883. skip_pkt = 1;
  884. }
  885. if (needset) {
  886. dd_dev_info(dd,
  887. "Switching to DMA_RTAIL\n");
  888. set_all_dma_rtail(dd);
  889. needset = 0;
  890. }
  891. }
  892. process_rcv_update(last, &packet);
  893. }
  894. process_rcv_qp_work(rcd);
  895. rcd->head = packet.rhqoff;
  896. bail:
  897. /*
  898. * Always write head at end, and setup rcv interrupt, even
  899. * if no packets were processed.
  900. */
  901. finish_packet(&packet);
  902. return last;
  903. }
  904. /*
  905. * We may discover in the interrupt that the hardware link state has
  906. * changed from ARMED to ACTIVE (due to the arrival of a non-SC15 packet),
  907. * and we need to update the driver's notion of the link state. We cannot
  908. * run set_link_state from interrupt context, so we queue this function on
  909. * a workqueue.
  910. *
  911. * We delay the regular interrupt processing until after the state changes
  912. * so that the link will be in the correct state by the time any application
  913. * we wake up attempts to send a reply to any message it received.
  914. * (Subsequent receive interrupts may possibly force the wakeup before we
  915. * update the link state.)
  916. *
  917. * The rcd is freed in hfi1_free_ctxtdata after hfi1_postinit_cleanup invokes
  918. * dd->f_cleanup(dd) to disable the interrupt handler and flush workqueues,
  919. * so we're safe from use-after-free of the rcd.
  920. */
  921. void receive_interrupt_work(struct work_struct *work)
  922. {
  923. struct hfi1_pportdata *ppd = container_of(work, struct hfi1_pportdata,
  924. linkstate_active_work);
  925. struct hfi1_devdata *dd = ppd->dd;
  926. int i;
  927. /* Received non-SC15 packet implies neighbor_normal */
  928. ppd->neighbor_normal = 1;
  929. set_link_state(ppd, HLS_UP_ACTIVE);
  930. /*
  931. * Interrupt all kernel contexts that could have had an
  932. * interrupt during auto activation.
  933. */
  934. for (i = HFI1_CTRL_CTXT; i < dd->first_user_ctxt; i++)
  935. force_recv_intr(dd->rcd[i]);
  936. }
  937. /*
  938. * Convert a given MTU size to the on-wire MAD packet enumeration.
  939. * Return -1 if the size is invalid.
  940. */
  941. int mtu_to_enum(u32 mtu, int default_if_bad)
  942. {
  943. switch (mtu) {
  944. case 0: return OPA_MTU_0;
  945. case 256: return OPA_MTU_256;
  946. case 512: return OPA_MTU_512;
  947. case 1024: return OPA_MTU_1024;
  948. case 2048: return OPA_MTU_2048;
  949. case 4096: return OPA_MTU_4096;
  950. case 8192: return OPA_MTU_8192;
  951. case 10240: return OPA_MTU_10240;
  952. }
  953. return default_if_bad;
  954. }
  955. u16 enum_to_mtu(int mtu)
  956. {
  957. switch (mtu) {
  958. case OPA_MTU_0: return 0;
  959. case OPA_MTU_256: return 256;
  960. case OPA_MTU_512: return 512;
  961. case OPA_MTU_1024: return 1024;
  962. case OPA_MTU_2048: return 2048;
  963. case OPA_MTU_4096: return 4096;
  964. case OPA_MTU_8192: return 8192;
  965. case OPA_MTU_10240: return 10240;
  966. default: return 0xffff;
  967. }
  968. }
  969. /*
  970. * set_mtu - set the MTU
  971. * @ppd: the per port data
  972. *
  973. * We can handle "any" incoming size, the issue here is whether we
  974. * need to restrict our outgoing size. We do not deal with what happens
  975. * to programs that are already running when the size changes.
  976. */
  977. int set_mtu(struct hfi1_pportdata *ppd)
  978. {
  979. struct hfi1_devdata *dd = ppd->dd;
  980. int i, drain, ret = 0, is_up = 0;
  981. ppd->ibmtu = 0;
  982. for (i = 0; i < ppd->vls_supported; i++)
  983. if (ppd->ibmtu < dd->vld[i].mtu)
  984. ppd->ibmtu = dd->vld[i].mtu;
  985. ppd->ibmaxlen = ppd->ibmtu + lrh_max_header_bytes(ppd->dd);
  986. mutex_lock(&ppd->hls_lock);
  987. if (ppd->host_link_state == HLS_UP_INIT ||
  988. ppd->host_link_state == HLS_UP_ARMED ||
  989. ppd->host_link_state == HLS_UP_ACTIVE)
  990. is_up = 1;
  991. drain = !is_ax(dd) && is_up;
  992. if (drain)
  993. /*
  994. * MTU is specified per-VL. To ensure that no packet gets
  995. * stuck (due, e.g., to the MTU for the packet's VL being
  996. * reduced), empty the per-VL FIFOs before adjusting MTU.
  997. */
  998. ret = stop_drain_data_vls(dd);
  999. if (ret) {
  1000. dd_dev_err(dd, "%s: cannot stop/drain VLs - refusing to change per-VL MTUs\n",
  1001. __func__);
  1002. goto err;
  1003. }
  1004. hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_MTU, 0);
  1005. if (drain)
  1006. open_fill_data_vls(dd); /* reopen all VLs */
  1007. err:
  1008. mutex_unlock(&ppd->hls_lock);
  1009. return ret;
  1010. }
  1011. int hfi1_set_lid(struct hfi1_pportdata *ppd, u32 lid, u8 lmc)
  1012. {
  1013. struct hfi1_devdata *dd = ppd->dd;
  1014. ppd->lid = lid;
  1015. ppd->lmc = lmc;
  1016. hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_LIDLMC, 0);
  1017. dd_dev_info(dd, "port %u: got a lid: 0x%x\n", ppd->port, lid);
  1018. return 0;
  1019. }
  1020. void shutdown_led_override(struct hfi1_pportdata *ppd)
  1021. {
  1022. struct hfi1_devdata *dd = ppd->dd;
  1023. /*
  1024. * This pairs with the memory barrier in hfi1_start_led_override to
  1025. * ensure that we read the correct state of LED beaconing represented
  1026. * by led_override_timer_active
  1027. */
  1028. smp_rmb();
  1029. if (atomic_read(&ppd->led_override_timer_active)) {
  1030. del_timer_sync(&ppd->led_override_timer);
  1031. atomic_set(&ppd->led_override_timer_active, 0);
  1032. /* Ensure the atomic_set is visible to all CPUs */
  1033. smp_wmb();
  1034. }
  1035. /* Hand control of the LED to the DC for normal operation */
  1036. write_csr(dd, DCC_CFG_LED_CNTRL, 0);
  1037. }
  1038. static void run_led_override(unsigned long opaque)
  1039. {
  1040. struct hfi1_pportdata *ppd = (struct hfi1_pportdata *)opaque;
  1041. struct hfi1_devdata *dd = ppd->dd;
  1042. unsigned long timeout;
  1043. int phase_idx;
  1044. if (!(dd->flags & HFI1_INITTED))
  1045. return;
  1046. phase_idx = ppd->led_override_phase & 1;
  1047. setextled(dd, phase_idx);
  1048. timeout = ppd->led_override_vals[phase_idx];
  1049. /* Set up for next phase */
  1050. ppd->led_override_phase = !ppd->led_override_phase;
  1051. mod_timer(&ppd->led_override_timer, jiffies + timeout);
  1052. }
  1053. /*
  1054. * To have the LED blink in a particular pattern, provide timeon and timeoff
  1055. * in milliseconds.
  1056. * To turn off custom blinking and return to normal operation, use
  1057. * shutdown_led_override()
  1058. */
  1059. void hfi1_start_led_override(struct hfi1_pportdata *ppd, unsigned int timeon,
  1060. unsigned int timeoff)
  1061. {
  1062. if (!(ppd->dd->flags & HFI1_INITTED))
  1063. return;
  1064. /* Convert to jiffies for direct use in timer */
  1065. ppd->led_override_vals[0] = msecs_to_jiffies(timeoff);
  1066. ppd->led_override_vals[1] = msecs_to_jiffies(timeon);
  1067. /* Arbitrarily start from LED on phase */
  1068. ppd->led_override_phase = 1;
  1069. /*
  1070. * If the timer has not already been started, do so. Use a "quick"
  1071. * timeout so the handler will be called soon to look at our request.
  1072. */
  1073. if (!timer_pending(&ppd->led_override_timer)) {
  1074. setup_timer(&ppd->led_override_timer, run_led_override,
  1075. (unsigned long)ppd);
  1076. ppd->led_override_timer.expires = jiffies + 1;
  1077. add_timer(&ppd->led_override_timer);
  1078. atomic_set(&ppd->led_override_timer_active, 1);
  1079. /* Ensure the atomic_set is visible to all CPUs */
  1080. smp_wmb();
  1081. }
  1082. }
  1083. /**
  1084. * hfi1_reset_device - reset the chip if possible
  1085. * @unit: the device to reset
  1086. *
  1087. * Whether or not reset is successful, we attempt to re-initialize the chip
  1088. * (that is, much like a driver unload/reload). We clear the INITTED flag
  1089. * so that the various entry points will fail until we reinitialize. For
  1090. * now, we only allow this if no user contexts are open that use chip resources
  1091. */
  1092. int hfi1_reset_device(int unit)
  1093. {
  1094. int ret, i;
  1095. struct hfi1_devdata *dd = hfi1_lookup(unit);
  1096. struct hfi1_pportdata *ppd;
  1097. unsigned long flags;
  1098. int pidx;
  1099. if (!dd) {
  1100. ret = -ENODEV;
  1101. goto bail;
  1102. }
  1103. dd_dev_info(dd, "Reset on unit %u requested\n", unit);
  1104. if (!dd->kregbase || !(dd->flags & HFI1_PRESENT)) {
  1105. dd_dev_info(dd,
  1106. "Invalid unit number %u or not initialized or not present\n",
  1107. unit);
  1108. ret = -ENXIO;
  1109. goto bail;
  1110. }
  1111. spin_lock_irqsave(&dd->uctxt_lock, flags);
  1112. if (dd->rcd)
  1113. for (i = dd->first_user_ctxt; i < dd->num_rcv_contexts; i++) {
  1114. if (!dd->rcd[i] || !dd->rcd[i]->cnt)
  1115. continue;
  1116. spin_unlock_irqrestore(&dd->uctxt_lock, flags);
  1117. ret = -EBUSY;
  1118. goto bail;
  1119. }
  1120. spin_unlock_irqrestore(&dd->uctxt_lock, flags);
  1121. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  1122. ppd = dd->pport + pidx;
  1123. shutdown_led_override(ppd);
  1124. }
  1125. if (dd->flags & HFI1_HAS_SEND_DMA)
  1126. sdma_exit(dd);
  1127. hfi1_reset_cpu_counters(dd);
  1128. ret = hfi1_init(dd, 1);
  1129. if (ret)
  1130. dd_dev_err(dd,
  1131. "Reinitialize unit %u after reset failed with %d\n",
  1132. unit, ret);
  1133. else
  1134. dd_dev_info(dd, "Reinitialized unit %u after resetting\n",
  1135. unit);
  1136. bail:
  1137. return ret;
  1138. }
  1139. void handle_eflags(struct hfi1_packet *packet)
  1140. {
  1141. struct hfi1_ctxtdata *rcd = packet->rcd;
  1142. u32 rte = rhf_rcv_type_err(packet->rhf);
  1143. rcv_hdrerr(rcd, rcd->ppd, packet);
  1144. if (rhf_err_flags(packet->rhf))
  1145. dd_dev_err(rcd->dd,
  1146. "receive context %d: rhf 0x%016llx, errs [ %s%s%s%s%s%s%s%s] rte 0x%x\n",
  1147. rcd->ctxt, packet->rhf,
  1148. packet->rhf & RHF_K_HDR_LEN_ERR ? "k_hdr_len " : "",
  1149. packet->rhf & RHF_DC_UNC_ERR ? "dc_unc " : "",
  1150. packet->rhf & RHF_DC_ERR ? "dc " : "",
  1151. packet->rhf & RHF_TID_ERR ? "tid " : "",
  1152. packet->rhf & RHF_LEN_ERR ? "len " : "",
  1153. packet->rhf & RHF_ECC_ERR ? "ecc " : "",
  1154. packet->rhf & RHF_VCRC_ERR ? "vcrc " : "",
  1155. packet->rhf & RHF_ICRC_ERR ? "icrc " : "",
  1156. rte);
  1157. }
  1158. /*
  1159. * The following functions are called by the interrupt handler. They are type
  1160. * specific handlers for each packet type.
  1161. */
  1162. int process_receive_ib(struct hfi1_packet *packet)
  1163. {
  1164. trace_hfi1_rcvhdr(packet->rcd->ppd->dd,
  1165. packet->rcd->ctxt,
  1166. rhf_err_flags(packet->rhf),
  1167. RHF_RCV_TYPE_IB,
  1168. packet->hlen,
  1169. packet->tlen,
  1170. packet->updegr,
  1171. rhf_egr_index(packet->rhf));
  1172. if (unlikely(rhf_err_flags(packet->rhf))) {
  1173. handle_eflags(packet);
  1174. return RHF_RCV_CONTINUE;
  1175. }
  1176. hfi1_ib_rcv(packet);
  1177. return RHF_RCV_CONTINUE;
  1178. }
  1179. int process_receive_bypass(struct hfi1_packet *packet)
  1180. {
  1181. struct hfi1_devdata *dd = packet->rcd->dd;
  1182. if (unlikely(rhf_err_flags(packet->rhf)))
  1183. handle_eflags(packet);
  1184. dd_dev_err(dd,
  1185. "Bypass packets are not supported in normal operation. Dropping\n");
  1186. incr_cntr64(&dd->sw_rcv_bypass_packet_errors);
  1187. if (!(dd->err_info_rcvport.status_and_code & OPA_EI_STATUS_SMASK)) {
  1188. u64 *flits = packet->ebuf;
  1189. if (flits && !(packet->rhf & RHF_LEN_ERR)) {
  1190. dd->err_info_rcvport.packet_flit1 = flits[0];
  1191. dd->err_info_rcvport.packet_flit2 =
  1192. packet->tlen > sizeof(flits[0]) ? flits[1] : 0;
  1193. }
  1194. dd->err_info_rcvport.status_and_code |=
  1195. (OPA_EI_STATUS_SMASK | BAD_L2_ERR);
  1196. }
  1197. return RHF_RCV_CONTINUE;
  1198. }
  1199. int process_receive_error(struct hfi1_packet *packet)
  1200. {
  1201. handle_eflags(packet);
  1202. if (unlikely(rhf_err_flags(packet->rhf)))
  1203. dd_dev_err(packet->rcd->dd,
  1204. "Unhandled error packet received. Dropping.\n");
  1205. return RHF_RCV_CONTINUE;
  1206. }
  1207. int kdeth_process_expected(struct hfi1_packet *packet)
  1208. {
  1209. if (unlikely(rhf_err_flags(packet->rhf)))
  1210. handle_eflags(packet);
  1211. dd_dev_err(packet->rcd->dd,
  1212. "Unhandled expected packet received. Dropping.\n");
  1213. return RHF_RCV_CONTINUE;
  1214. }
  1215. int kdeth_process_eager(struct hfi1_packet *packet)
  1216. {
  1217. if (unlikely(rhf_err_flags(packet->rhf)))
  1218. handle_eflags(packet);
  1219. dd_dev_err(packet->rcd->dd,
  1220. "Unhandled eager packet received. Dropping.\n");
  1221. return RHF_RCV_CONTINUE;
  1222. }
  1223. int process_receive_invalid(struct hfi1_packet *packet)
  1224. {
  1225. dd_dev_err(packet->rcd->dd, "Invalid packet type %d. Dropping\n",
  1226. rhf_rcv_type(packet->rhf));
  1227. return RHF_RCV_CONTINUE;
  1228. }