qib_driver.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /*
  2. * Copyright (c) 2013 Intel Corporation. All rights reserved.
  3. * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.
  4. * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/spinlock.h>
  35. #include <linux/pci.h>
  36. #include <linux/io.h>
  37. #include <linux/delay.h>
  38. #include <linux/netdevice.h>
  39. #include <linux/vmalloc.h>
  40. #include <linux/module.h>
  41. #include <linux/prefetch.h>
  42. #include "qib.h"
  43. /*
  44. * The size has to be longer than this string, so we can append
  45. * board/chip information to it in the init code.
  46. */
  47. const char ib_qib_version[] = QIB_DRIVER_VERSION "\n";
  48. DEFINE_SPINLOCK(qib_devs_lock);
  49. LIST_HEAD(qib_dev_list);
  50. DEFINE_MUTEX(qib_mutex); /* general driver use */
  51. unsigned qib_ibmtu;
  52. module_param_named(ibmtu, qib_ibmtu, uint, S_IRUGO);
  53. MODULE_PARM_DESC(ibmtu, "Set max IB MTU (0=2KB, 1=256, 2=512, ... 5=4096");
  54. unsigned qib_compat_ddr_negotiate = 1;
  55. module_param_named(compat_ddr_negotiate, qib_compat_ddr_negotiate, uint,
  56. S_IWUSR | S_IRUGO);
  57. MODULE_PARM_DESC(compat_ddr_negotiate,
  58. "Attempt pre-IBTA 1.2 DDR speed negotiation");
  59. MODULE_LICENSE("Dual BSD/GPL");
  60. MODULE_AUTHOR("Intel <ibsupport@intel.com>");
  61. MODULE_DESCRIPTION("Intel IB driver");
  62. MODULE_VERSION(QIB_DRIVER_VERSION);
  63. /*
  64. * QIB_PIO_MAXIBHDR is the max IB header size allowed for in our
  65. * PIO send buffers. This is well beyond anything currently
  66. * defined in the InfiniBand spec.
  67. */
  68. #define QIB_PIO_MAXIBHDR 128
  69. /*
  70. * QIB_MAX_PKT_RCV is the max # if packets processed per receive interrupt.
  71. */
  72. #define QIB_MAX_PKT_RECV 64
  73. struct qlogic_ib_stats qib_stats;
  74. const char *qib_get_unit_name(int unit)
  75. {
  76. static char iname[16];
  77. snprintf(iname, sizeof(iname), "infinipath%u", unit);
  78. return iname;
  79. }
  80. const char *qib_get_card_name(struct rvt_dev_info *rdi)
  81. {
  82. struct qib_ibdev *ibdev = container_of(rdi, struct qib_ibdev, rdi);
  83. struct qib_devdata *dd = container_of(ibdev,
  84. struct qib_devdata, verbs_dev);
  85. return qib_get_unit_name(dd->unit);
  86. }
  87. struct pci_dev *qib_get_pci_dev(struct rvt_dev_info *rdi)
  88. {
  89. struct qib_ibdev *ibdev = container_of(rdi, struct qib_ibdev, rdi);
  90. struct qib_devdata *dd = container_of(ibdev,
  91. struct qib_devdata, verbs_dev);
  92. return dd->pcidev;
  93. }
  94. /*
  95. * Return count of units with at least one port ACTIVE.
  96. */
  97. int qib_count_active_units(void)
  98. {
  99. struct qib_devdata *dd;
  100. struct qib_pportdata *ppd;
  101. unsigned long flags;
  102. int pidx, nunits_active = 0;
  103. spin_lock_irqsave(&qib_devs_lock, flags);
  104. list_for_each_entry(dd, &qib_dev_list, list) {
  105. if (!(dd->flags & QIB_PRESENT) || !dd->kregbase)
  106. continue;
  107. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  108. ppd = dd->pport + pidx;
  109. if (ppd->lid && (ppd->lflags & (QIBL_LINKINIT |
  110. QIBL_LINKARMED | QIBL_LINKACTIVE))) {
  111. nunits_active++;
  112. break;
  113. }
  114. }
  115. }
  116. spin_unlock_irqrestore(&qib_devs_lock, flags);
  117. return nunits_active;
  118. }
  119. /*
  120. * Return count of all units, optionally return in arguments
  121. * the number of usable (present) units, and the number of
  122. * ports that are up.
  123. */
  124. int qib_count_units(int *npresentp, int *nupp)
  125. {
  126. int nunits = 0, npresent = 0, nup = 0;
  127. struct qib_devdata *dd;
  128. unsigned long flags;
  129. int pidx;
  130. struct qib_pportdata *ppd;
  131. spin_lock_irqsave(&qib_devs_lock, flags);
  132. list_for_each_entry(dd, &qib_dev_list, list) {
  133. nunits++;
  134. if ((dd->flags & QIB_PRESENT) && dd->kregbase)
  135. npresent++;
  136. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  137. ppd = dd->pport + pidx;
  138. if (ppd->lid && (ppd->lflags & (QIBL_LINKINIT |
  139. QIBL_LINKARMED | QIBL_LINKACTIVE)))
  140. nup++;
  141. }
  142. }
  143. spin_unlock_irqrestore(&qib_devs_lock, flags);
  144. if (npresentp)
  145. *npresentp = npresent;
  146. if (nupp)
  147. *nupp = nup;
  148. return nunits;
  149. }
  150. /**
  151. * qib_wait_linkstate - wait for an IB link state change to occur
  152. * @dd: the qlogic_ib device
  153. * @state: the state to wait for
  154. * @msecs: the number of milliseconds to wait
  155. *
  156. * wait up to msecs milliseconds for IB link state change to occur for
  157. * now, take the easy polling route. Currently used only by
  158. * qib_set_linkstate. Returns 0 if state reached, otherwise
  159. * -ETIMEDOUT state can have multiple states set, for any of several
  160. * transitions.
  161. */
  162. int qib_wait_linkstate(struct qib_pportdata *ppd, u32 state, int msecs)
  163. {
  164. int ret;
  165. unsigned long flags;
  166. spin_lock_irqsave(&ppd->lflags_lock, flags);
  167. if (ppd->state_wanted) {
  168. spin_unlock_irqrestore(&ppd->lflags_lock, flags);
  169. ret = -EBUSY;
  170. goto bail;
  171. }
  172. ppd->state_wanted = state;
  173. spin_unlock_irqrestore(&ppd->lflags_lock, flags);
  174. wait_event_interruptible_timeout(ppd->state_wait,
  175. (ppd->lflags & state),
  176. msecs_to_jiffies(msecs));
  177. spin_lock_irqsave(&ppd->lflags_lock, flags);
  178. ppd->state_wanted = 0;
  179. spin_unlock_irqrestore(&ppd->lflags_lock, flags);
  180. if (!(ppd->lflags & state))
  181. ret = -ETIMEDOUT;
  182. else
  183. ret = 0;
  184. bail:
  185. return ret;
  186. }
  187. int qib_set_linkstate(struct qib_pportdata *ppd, u8 newstate)
  188. {
  189. u32 lstate;
  190. int ret;
  191. struct qib_devdata *dd = ppd->dd;
  192. unsigned long flags;
  193. switch (newstate) {
  194. case QIB_IB_LINKDOWN_ONLY:
  195. dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LSTATE,
  196. IB_LINKCMD_DOWN | IB_LINKINITCMD_NOP);
  197. /* don't wait */
  198. ret = 0;
  199. goto bail;
  200. case QIB_IB_LINKDOWN:
  201. dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LSTATE,
  202. IB_LINKCMD_DOWN | IB_LINKINITCMD_POLL);
  203. /* don't wait */
  204. ret = 0;
  205. goto bail;
  206. case QIB_IB_LINKDOWN_SLEEP:
  207. dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LSTATE,
  208. IB_LINKCMD_DOWN | IB_LINKINITCMD_SLEEP);
  209. /* don't wait */
  210. ret = 0;
  211. goto bail;
  212. case QIB_IB_LINKDOWN_DISABLE:
  213. dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LSTATE,
  214. IB_LINKCMD_DOWN | IB_LINKINITCMD_DISABLE);
  215. /* don't wait */
  216. ret = 0;
  217. goto bail;
  218. case QIB_IB_LINKARM:
  219. if (ppd->lflags & QIBL_LINKARMED) {
  220. ret = 0;
  221. goto bail;
  222. }
  223. if (!(ppd->lflags & (QIBL_LINKINIT | QIBL_LINKACTIVE))) {
  224. ret = -EINVAL;
  225. goto bail;
  226. }
  227. /*
  228. * Since the port can be ACTIVE when we ask for ARMED,
  229. * clear QIBL_LINKV so we can wait for a transition.
  230. * If the link isn't ARMED, then something else happened
  231. * and there is no point waiting for ARMED.
  232. */
  233. spin_lock_irqsave(&ppd->lflags_lock, flags);
  234. ppd->lflags &= ~QIBL_LINKV;
  235. spin_unlock_irqrestore(&ppd->lflags_lock, flags);
  236. dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LSTATE,
  237. IB_LINKCMD_ARMED | IB_LINKINITCMD_NOP);
  238. lstate = QIBL_LINKV;
  239. break;
  240. case QIB_IB_LINKACTIVE:
  241. if (ppd->lflags & QIBL_LINKACTIVE) {
  242. ret = 0;
  243. goto bail;
  244. }
  245. if (!(ppd->lflags & QIBL_LINKARMED)) {
  246. ret = -EINVAL;
  247. goto bail;
  248. }
  249. dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LSTATE,
  250. IB_LINKCMD_ACTIVE | IB_LINKINITCMD_NOP);
  251. lstate = QIBL_LINKACTIVE;
  252. break;
  253. default:
  254. ret = -EINVAL;
  255. goto bail;
  256. }
  257. ret = qib_wait_linkstate(ppd, lstate, 10);
  258. bail:
  259. return ret;
  260. }
  261. /*
  262. * Get address of eager buffer from it's index (allocated in chunks, not
  263. * contiguous).
  264. */
  265. static inline void *qib_get_egrbuf(const struct qib_ctxtdata *rcd, u32 etail)
  266. {
  267. const u32 chunk = etail >> rcd->rcvegrbufs_perchunk_shift;
  268. const u32 idx = etail & ((u32)rcd->rcvegrbufs_perchunk - 1);
  269. return rcd->rcvegrbuf[chunk] + (idx << rcd->dd->rcvegrbufsize_shift);
  270. }
  271. /*
  272. * Returns 1 if error was a CRC, else 0.
  273. * Needed for some chip's synthesized error counters.
  274. */
  275. static u32 qib_rcv_hdrerr(struct qib_ctxtdata *rcd, struct qib_pportdata *ppd,
  276. u32 ctxt, u32 eflags, u32 l, u32 etail,
  277. __le32 *rhf_addr, struct qib_message_header *rhdr)
  278. {
  279. u32 ret = 0;
  280. if (eflags & (QLOGIC_IB_RHF_H_ICRCERR | QLOGIC_IB_RHF_H_VCRCERR))
  281. ret = 1;
  282. else if (eflags == QLOGIC_IB_RHF_H_TIDERR) {
  283. /* For TIDERR and RC QPs premptively schedule a NAK */
  284. struct ib_header *hdr = (struct ib_header *)rhdr;
  285. struct ib_other_headers *ohdr = NULL;
  286. struct qib_ibport *ibp = &ppd->ibport_data;
  287. struct qib_devdata *dd = ppd->dd;
  288. struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
  289. struct rvt_qp *qp = NULL;
  290. u32 tlen = qib_hdrget_length_in_bytes(rhf_addr);
  291. u16 lid = be16_to_cpu(hdr->lrh[1]);
  292. int lnh = be16_to_cpu(hdr->lrh[0]) & 3;
  293. u32 qp_num;
  294. u32 opcode;
  295. u32 psn;
  296. int diff;
  297. /* Sanity check packet */
  298. if (tlen < 24)
  299. goto drop;
  300. if (lid < be16_to_cpu(IB_MULTICAST_LID_BASE)) {
  301. lid &= ~((1 << ppd->lmc) - 1);
  302. if (unlikely(lid != ppd->lid))
  303. goto drop;
  304. }
  305. /* Check for GRH */
  306. if (lnh == QIB_LRH_BTH)
  307. ohdr = &hdr->u.oth;
  308. else if (lnh == QIB_LRH_GRH) {
  309. u32 vtf;
  310. ohdr = &hdr->u.l.oth;
  311. if (hdr->u.l.grh.next_hdr != IB_GRH_NEXT_HDR)
  312. goto drop;
  313. vtf = be32_to_cpu(hdr->u.l.grh.version_tclass_flow);
  314. if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
  315. goto drop;
  316. } else
  317. goto drop;
  318. /* Get opcode and PSN from packet */
  319. opcode = be32_to_cpu(ohdr->bth[0]);
  320. opcode >>= 24;
  321. psn = be32_to_cpu(ohdr->bth[2]);
  322. /* Get the destination QP number. */
  323. qp_num = be32_to_cpu(ohdr->bth[1]) & RVT_QPN_MASK;
  324. if (qp_num != QIB_MULTICAST_QPN) {
  325. int ruc_res;
  326. rcu_read_lock();
  327. qp = rvt_lookup_qpn(rdi, &ibp->rvp, qp_num);
  328. if (!qp) {
  329. rcu_read_unlock();
  330. goto drop;
  331. }
  332. /*
  333. * Handle only RC QPs - for other QP types drop error
  334. * packet.
  335. */
  336. spin_lock(&qp->r_lock);
  337. /* Check for valid receive state. */
  338. if (!(ib_rvt_state_ops[qp->state] &
  339. RVT_PROCESS_RECV_OK)) {
  340. ibp->rvp.n_pkt_drops++;
  341. goto unlock;
  342. }
  343. switch (qp->ibqp.qp_type) {
  344. case IB_QPT_RC:
  345. ruc_res =
  346. qib_ruc_check_hdr(
  347. ibp, hdr,
  348. lnh == QIB_LRH_GRH,
  349. qp,
  350. be32_to_cpu(ohdr->bth[0]));
  351. if (ruc_res)
  352. goto unlock;
  353. /* Only deal with RDMA Writes for now */
  354. if (opcode <
  355. IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST) {
  356. diff = qib_cmp24(psn, qp->r_psn);
  357. if (!qp->r_nak_state && diff >= 0) {
  358. ibp->rvp.n_rc_seqnak++;
  359. qp->r_nak_state =
  360. IB_NAK_PSN_ERROR;
  361. /* Use the expected PSN. */
  362. qp->r_ack_psn = qp->r_psn;
  363. /*
  364. * Wait to send the sequence
  365. * NAK until all packets
  366. * in the receive queue have
  367. * been processed.
  368. * Otherwise, we end up
  369. * propagating congestion.
  370. */
  371. if (list_empty(&qp->rspwait)) {
  372. qp->r_flags |=
  373. RVT_R_RSP_NAK;
  374. rvt_get_qp(qp);
  375. list_add_tail(
  376. &qp->rspwait,
  377. &rcd->qp_wait_list);
  378. }
  379. } /* Out of sequence NAK */
  380. } /* QP Request NAKs */
  381. break;
  382. case IB_QPT_SMI:
  383. case IB_QPT_GSI:
  384. case IB_QPT_UD:
  385. case IB_QPT_UC:
  386. default:
  387. /* For now don't handle any other QP types */
  388. break;
  389. }
  390. unlock:
  391. spin_unlock(&qp->r_lock);
  392. rcu_read_unlock();
  393. } /* Unicast QP */
  394. } /* Valid packet with TIDErr */
  395. drop:
  396. return ret;
  397. }
  398. /*
  399. * qib_kreceive - receive a packet
  400. * @rcd: the qlogic_ib context
  401. * @llic: gets count of good packets needed to clear lli,
  402. * (used with chips that need need to track crcs for lli)
  403. *
  404. * called from interrupt handler for errors or receive interrupt
  405. * Returns number of CRC error packets, needed by some chips for
  406. * local link integrity tracking. crcs are adjusted down by following
  407. * good packets, if any, and count of good packets is also tracked.
  408. */
  409. u32 qib_kreceive(struct qib_ctxtdata *rcd, u32 *llic, u32 *npkts)
  410. {
  411. struct qib_devdata *dd = rcd->dd;
  412. struct qib_pportdata *ppd = rcd->ppd;
  413. __le32 *rhf_addr;
  414. void *ebuf;
  415. const u32 rsize = dd->rcvhdrentsize; /* words */
  416. const u32 maxcnt = dd->rcvhdrcnt * rsize; /* words */
  417. u32 etail = -1, l, hdrqtail;
  418. struct qib_message_header *hdr;
  419. u32 eflags, etype, tlen, i = 0, updegr = 0, crcs = 0;
  420. int last;
  421. u64 lval;
  422. struct rvt_qp *qp, *nqp;
  423. l = rcd->head;
  424. rhf_addr = (__le32 *) rcd->rcvhdrq + l + dd->rhf_offset;
  425. if (dd->flags & QIB_NODMA_RTAIL) {
  426. u32 seq = qib_hdrget_seq(rhf_addr);
  427. if (seq != rcd->seq_cnt)
  428. goto bail;
  429. hdrqtail = 0;
  430. } else {
  431. hdrqtail = qib_get_rcvhdrtail(rcd);
  432. if (l == hdrqtail)
  433. goto bail;
  434. smp_rmb(); /* prevent speculative reads of dma'ed hdrq */
  435. }
  436. for (last = 0, i = 1; !last; i += !last) {
  437. hdr = dd->f_get_msgheader(dd, rhf_addr);
  438. eflags = qib_hdrget_err_flags(rhf_addr);
  439. etype = qib_hdrget_rcv_type(rhf_addr);
  440. /* total length */
  441. tlen = qib_hdrget_length_in_bytes(rhf_addr);
  442. ebuf = NULL;
  443. if ((dd->flags & QIB_NODMA_RTAIL) ?
  444. qib_hdrget_use_egr_buf(rhf_addr) :
  445. (etype != RCVHQ_RCV_TYPE_EXPECTED)) {
  446. etail = qib_hdrget_index(rhf_addr);
  447. updegr = 1;
  448. if (tlen > sizeof(*hdr) ||
  449. etype >= RCVHQ_RCV_TYPE_NON_KD) {
  450. ebuf = qib_get_egrbuf(rcd, etail);
  451. prefetch_range(ebuf, tlen - sizeof(*hdr));
  452. }
  453. }
  454. if (!eflags) {
  455. u16 lrh_len = be16_to_cpu(hdr->lrh[2]) << 2;
  456. if (lrh_len != tlen) {
  457. qib_stats.sps_lenerrs++;
  458. goto move_along;
  459. }
  460. }
  461. if (etype == RCVHQ_RCV_TYPE_NON_KD && !eflags &&
  462. ebuf == NULL &&
  463. tlen > (dd->rcvhdrentsize - 2 + 1 -
  464. qib_hdrget_offset(rhf_addr)) << 2) {
  465. goto move_along;
  466. }
  467. /*
  468. * Both tiderr and qibhdrerr are set for all plain IB
  469. * packets; only qibhdrerr should be set.
  470. */
  471. if (unlikely(eflags))
  472. crcs += qib_rcv_hdrerr(rcd, ppd, rcd->ctxt, eflags, l,
  473. etail, rhf_addr, hdr);
  474. else if (etype == RCVHQ_RCV_TYPE_NON_KD) {
  475. qib_ib_rcv(rcd, hdr, ebuf, tlen);
  476. if (crcs)
  477. crcs--;
  478. else if (llic && *llic)
  479. --*llic;
  480. }
  481. move_along:
  482. l += rsize;
  483. if (l >= maxcnt)
  484. l = 0;
  485. if (i == QIB_MAX_PKT_RECV)
  486. last = 1;
  487. rhf_addr = (__le32 *) rcd->rcvhdrq + l + dd->rhf_offset;
  488. if (dd->flags & QIB_NODMA_RTAIL) {
  489. u32 seq = qib_hdrget_seq(rhf_addr);
  490. if (++rcd->seq_cnt > 13)
  491. rcd->seq_cnt = 1;
  492. if (seq != rcd->seq_cnt)
  493. last = 1;
  494. } else if (l == hdrqtail)
  495. last = 1;
  496. /*
  497. * Update head regs etc., every 16 packets, if not last pkt,
  498. * to help prevent rcvhdrq overflows, when many packets
  499. * are processed and queue is nearly full.
  500. * Don't request an interrupt for intermediate updates.
  501. */
  502. lval = l;
  503. if (!last && !(i & 0xf)) {
  504. dd->f_update_usrhead(rcd, lval, updegr, etail, i);
  505. updegr = 0;
  506. }
  507. }
  508. rcd->head = l;
  509. /*
  510. * Iterate over all QPs waiting to respond.
  511. * The list won't change since the IRQ is only run on one CPU.
  512. */
  513. list_for_each_entry_safe(qp, nqp, &rcd->qp_wait_list, rspwait) {
  514. list_del_init(&qp->rspwait);
  515. if (qp->r_flags & RVT_R_RSP_NAK) {
  516. qp->r_flags &= ~RVT_R_RSP_NAK;
  517. qib_send_rc_ack(qp);
  518. }
  519. if (qp->r_flags & RVT_R_RSP_SEND) {
  520. unsigned long flags;
  521. qp->r_flags &= ~RVT_R_RSP_SEND;
  522. spin_lock_irqsave(&qp->s_lock, flags);
  523. if (ib_rvt_state_ops[qp->state] &
  524. RVT_PROCESS_OR_FLUSH_SEND)
  525. qib_schedule_send(qp);
  526. spin_unlock_irqrestore(&qp->s_lock, flags);
  527. }
  528. rvt_put_qp(qp);
  529. }
  530. bail:
  531. /* Report number of packets consumed */
  532. if (npkts)
  533. *npkts = i;
  534. /*
  535. * Always write head at end, and setup rcv interrupt, even
  536. * if no packets were processed.
  537. */
  538. lval = (u64)rcd->head | dd->rhdrhead_intr_off;
  539. dd->f_update_usrhead(rcd, lval, updegr, etail, i);
  540. return crcs;
  541. }
  542. /**
  543. * qib_set_mtu - set the MTU
  544. * @ppd: the perport data
  545. * @arg: the new MTU
  546. *
  547. * We can handle "any" incoming size, the issue here is whether we
  548. * need to restrict our outgoing size. For now, we don't do any
  549. * sanity checking on this, and we don't deal with what happens to
  550. * programs that are already running when the size changes.
  551. * NOTE: changing the MTU will usually cause the IBC to go back to
  552. * link INIT state...
  553. */
  554. int qib_set_mtu(struct qib_pportdata *ppd, u16 arg)
  555. {
  556. u32 piosize;
  557. int ret, chk;
  558. if (arg != 256 && arg != 512 && arg != 1024 && arg != 2048 &&
  559. arg != 4096) {
  560. ret = -EINVAL;
  561. goto bail;
  562. }
  563. chk = ib_mtu_enum_to_int(qib_ibmtu);
  564. if (chk > 0 && arg > chk) {
  565. ret = -EINVAL;
  566. goto bail;
  567. }
  568. piosize = ppd->ibmaxlen;
  569. ppd->ibmtu = arg;
  570. if (arg >= (piosize - QIB_PIO_MAXIBHDR)) {
  571. /* Only if it's not the initial value (or reset to it) */
  572. if (piosize != ppd->init_ibmaxlen) {
  573. if (arg > piosize && arg <= ppd->init_ibmaxlen)
  574. piosize = ppd->init_ibmaxlen - 2 * sizeof(u32);
  575. ppd->ibmaxlen = piosize;
  576. }
  577. } else if ((arg + QIB_PIO_MAXIBHDR) != ppd->ibmaxlen) {
  578. piosize = arg + QIB_PIO_MAXIBHDR - 2 * sizeof(u32);
  579. ppd->ibmaxlen = piosize;
  580. }
  581. ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_MTU, 0);
  582. ret = 0;
  583. bail:
  584. return ret;
  585. }
  586. int qib_set_lid(struct qib_pportdata *ppd, u32 lid, u8 lmc)
  587. {
  588. struct qib_devdata *dd = ppd->dd;
  589. ppd->lid = lid;
  590. ppd->lmc = lmc;
  591. dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LIDLMC,
  592. lid | (~((1U << lmc) - 1)) << 16);
  593. qib_devinfo(dd->pcidev, "IB%u:%u got a lid: 0x%x\n",
  594. dd->unit, ppd->port, lid);
  595. return 0;
  596. }
  597. /*
  598. * Following deal with the "obviously simple" task of overriding the state
  599. * of the LEDS, which normally indicate link physical and logical status.
  600. * The complications arise in dealing with different hardware mappings
  601. * and the board-dependent routine being called from interrupts.
  602. * and then there's the requirement to _flash_ them.
  603. */
  604. #define LED_OVER_FREQ_SHIFT 8
  605. #define LED_OVER_FREQ_MASK (0xFF<<LED_OVER_FREQ_SHIFT)
  606. /* Below is "non-zero" to force override, but both actual LEDs are off */
  607. #define LED_OVER_BOTH_OFF (8)
  608. static void qib_run_led_override(unsigned long opaque)
  609. {
  610. struct qib_pportdata *ppd = (struct qib_pportdata *)opaque;
  611. struct qib_devdata *dd = ppd->dd;
  612. int timeoff;
  613. int ph_idx;
  614. if (!(dd->flags & QIB_INITTED))
  615. return;
  616. ph_idx = ppd->led_override_phase++ & 1;
  617. ppd->led_override = ppd->led_override_vals[ph_idx];
  618. timeoff = ppd->led_override_timeoff;
  619. dd->f_setextled(ppd, 1);
  620. /*
  621. * don't re-fire the timer if user asked for it to be off; we let
  622. * it fire one more time after they turn it off to simplify
  623. */
  624. if (ppd->led_override_vals[0] || ppd->led_override_vals[1])
  625. mod_timer(&ppd->led_override_timer, jiffies + timeoff);
  626. }
  627. void qib_set_led_override(struct qib_pportdata *ppd, unsigned int val)
  628. {
  629. struct qib_devdata *dd = ppd->dd;
  630. int timeoff, freq;
  631. if (!(dd->flags & QIB_INITTED))
  632. return;
  633. /* First check if we are blinking. If not, use 1HZ polling */
  634. timeoff = HZ;
  635. freq = (val & LED_OVER_FREQ_MASK) >> LED_OVER_FREQ_SHIFT;
  636. if (freq) {
  637. /* For blink, set each phase from one nybble of val */
  638. ppd->led_override_vals[0] = val & 0xF;
  639. ppd->led_override_vals[1] = (val >> 4) & 0xF;
  640. timeoff = (HZ << 4)/freq;
  641. } else {
  642. /* Non-blink set both phases the same. */
  643. ppd->led_override_vals[0] = val & 0xF;
  644. ppd->led_override_vals[1] = val & 0xF;
  645. }
  646. ppd->led_override_timeoff = timeoff;
  647. /*
  648. * If the timer has not already been started, do so. Use a "quick"
  649. * timeout so the function will be called soon, to look at our request.
  650. */
  651. if (atomic_inc_return(&ppd->led_override_timer_active) == 1) {
  652. /* Need to start timer */
  653. init_timer(&ppd->led_override_timer);
  654. ppd->led_override_timer.function = qib_run_led_override;
  655. ppd->led_override_timer.data = (unsigned long) ppd;
  656. ppd->led_override_timer.expires = jiffies + 1;
  657. add_timer(&ppd->led_override_timer);
  658. } else {
  659. if (ppd->led_override_vals[0] || ppd->led_override_vals[1])
  660. mod_timer(&ppd->led_override_timer, jiffies + 1);
  661. atomic_dec(&ppd->led_override_timer_active);
  662. }
  663. }
  664. /**
  665. * qib_reset_device - reset the chip if possible
  666. * @unit: the device to reset
  667. *
  668. * Whether or not reset is successful, we attempt to re-initialize the chip
  669. * (that is, much like a driver unload/reload). We clear the INITTED flag
  670. * so that the various entry points will fail until we reinitialize. For
  671. * now, we only allow this if no user contexts are open that use chip resources
  672. */
  673. int qib_reset_device(int unit)
  674. {
  675. int ret, i;
  676. struct qib_devdata *dd = qib_lookup(unit);
  677. struct qib_pportdata *ppd;
  678. unsigned long flags;
  679. int pidx;
  680. if (!dd) {
  681. ret = -ENODEV;
  682. goto bail;
  683. }
  684. qib_devinfo(dd->pcidev, "Reset on unit %u requested\n", unit);
  685. if (!dd->kregbase || !(dd->flags & QIB_PRESENT)) {
  686. qib_devinfo(dd->pcidev,
  687. "Invalid unit number %u or not initialized or not present\n",
  688. unit);
  689. ret = -ENXIO;
  690. goto bail;
  691. }
  692. spin_lock_irqsave(&dd->uctxt_lock, flags);
  693. if (dd->rcd)
  694. for (i = dd->first_user_ctxt; i < dd->cfgctxts; i++) {
  695. if (!dd->rcd[i] || !dd->rcd[i]->cnt)
  696. continue;
  697. spin_unlock_irqrestore(&dd->uctxt_lock, flags);
  698. ret = -EBUSY;
  699. goto bail;
  700. }
  701. spin_unlock_irqrestore(&dd->uctxt_lock, flags);
  702. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  703. ppd = dd->pport + pidx;
  704. if (atomic_read(&ppd->led_override_timer_active)) {
  705. /* Need to stop LED timer, _then_ shut off LEDs */
  706. del_timer_sync(&ppd->led_override_timer);
  707. atomic_set(&ppd->led_override_timer_active, 0);
  708. }
  709. /* Shut off LEDs after we are sure timer is not running */
  710. ppd->led_override = LED_OVER_BOTH_OFF;
  711. dd->f_setextled(ppd, 0);
  712. if (dd->flags & QIB_HAS_SEND_DMA)
  713. qib_teardown_sdma(ppd);
  714. }
  715. ret = dd->f_reset(dd);
  716. if (ret == 1)
  717. ret = qib_init(dd, 1);
  718. else
  719. ret = -EAGAIN;
  720. if (ret)
  721. qib_dev_err(dd,
  722. "Reinitialize unit %u after reset failed with %d\n",
  723. unit, ret);
  724. else
  725. qib_devinfo(dd->pcidev,
  726. "Reinitialized unit %u after resetting\n",
  727. unit);
  728. bail:
  729. return ret;
  730. }