qib_driver.c 21 KB

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