qib_driver.c 21 KB

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