qib_driver.c 21 KB

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