device.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571
  1. /*
  2. * Copyright (c) 2009-2010 Chelsio, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/module.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/debugfs.h>
  35. #include <linux/vmalloc.h>
  36. #include <linux/math64.h>
  37. #include <rdma/ib_verbs.h>
  38. #include "iw_cxgb4.h"
  39. #define DRV_VERSION "0.1"
  40. MODULE_AUTHOR("Steve Wise");
  41. MODULE_DESCRIPTION("Chelsio T4/T5 RDMA Driver");
  42. MODULE_LICENSE("Dual BSD/GPL");
  43. MODULE_VERSION(DRV_VERSION);
  44. static int allow_db_fc_on_t5;
  45. module_param(allow_db_fc_on_t5, int, 0644);
  46. MODULE_PARM_DESC(allow_db_fc_on_t5,
  47. "Allow DB Flow Control on T5 (default = 0)");
  48. static int allow_db_coalescing_on_t5;
  49. module_param(allow_db_coalescing_on_t5, int, 0644);
  50. MODULE_PARM_DESC(allow_db_coalescing_on_t5,
  51. "Allow DB Coalescing on T5 (default = 0)");
  52. int c4iw_wr_log = 0;
  53. module_param(c4iw_wr_log, int, 0444);
  54. MODULE_PARM_DESC(c4iw_wr_log, "Enables logging of work request timing data.");
  55. static int c4iw_wr_log_size_order = 12;
  56. module_param(c4iw_wr_log_size_order, int, 0444);
  57. MODULE_PARM_DESC(c4iw_wr_log_size_order,
  58. "Number of entries (log2) in the work request timing log.");
  59. struct uld_ctx {
  60. struct list_head entry;
  61. struct cxgb4_lld_info lldi;
  62. struct c4iw_dev *dev;
  63. };
  64. static LIST_HEAD(uld_ctx_list);
  65. static DEFINE_MUTEX(dev_mutex);
  66. #define DB_FC_RESUME_SIZE 64
  67. #define DB_FC_RESUME_DELAY 1
  68. #define DB_FC_DRAIN_THRESH 0
  69. static struct dentry *c4iw_debugfs_root;
  70. struct c4iw_debugfs_data {
  71. struct c4iw_dev *devp;
  72. char *buf;
  73. int bufsize;
  74. int pos;
  75. };
  76. static int count_idrs(int id, void *p, void *data)
  77. {
  78. int *countp = data;
  79. *countp = *countp + 1;
  80. return 0;
  81. }
  82. static ssize_t debugfs_read(struct file *file, char __user *buf, size_t count,
  83. loff_t *ppos)
  84. {
  85. struct c4iw_debugfs_data *d = file->private_data;
  86. return simple_read_from_buffer(buf, count, ppos, d->buf, d->pos);
  87. }
  88. void c4iw_log_wr_stats(struct t4_wq *wq, struct t4_cqe *cqe)
  89. {
  90. struct wr_log_entry le;
  91. int idx;
  92. if (!wq->rdev->wr_log)
  93. return;
  94. idx = (atomic_inc_return(&wq->rdev->wr_log_idx) - 1) &
  95. (wq->rdev->wr_log_size - 1);
  96. le.poll_sge_ts = cxgb4_read_sge_timestamp(wq->rdev->lldi.ports[0]);
  97. getnstimeofday(&le.poll_host_ts);
  98. le.valid = 1;
  99. le.cqe_sge_ts = CQE_TS(cqe);
  100. if (SQ_TYPE(cqe)) {
  101. le.qid = wq->sq.qid;
  102. le.opcode = CQE_OPCODE(cqe);
  103. le.post_host_ts = wq->sq.sw_sq[wq->sq.cidx].host_ts;
  104. le.post_sge_ts = wq->sq.sw_sq[wq->sq.cidx].sge_ts;
  105. le.wr_id = CQE_WRID_SQ_IDX(cqe);
  106. } else {
  107. le.qid = wq->rq.qid;
  108. le.opcode = FW_RI_RECEIVE;
  109. le.post_host_ts = wq->rq.sw_rq[wq->rq.cidx].host_ts;
  110. le.post_sge_ts = wq->rq.sw_rq[wq->rq.cidx].sge_ts;
  111. le.wr_id = CQE_WRID_MSN(cqe);
  112. }
  113. wq->rdev->wr_log[idx] = le;
  114. }
  115. static int wr_log_show(struct seq_file *seq, void *v)
  116. {
  117. struct c4iw_dev *dev = seq->private;
  118. struct timespec prev_ts = {0, 0};
  119. struct wr_log_entry *lep;
  120. int prev_ts_set = 0;
  121. int idx, end;
  122. #define ts2ns(ts) div64_u64((ts) * dev->rdev.lldi.cclk_ps, 1000)
  123. idx = atomic_read(&dev->rdev.wr_log_idx) &
  124. (dev->rdev.wr_log_size - 1);
  125. end = idx - 1;
  126. if (end < 0)
  127. end = dev->rdev.wr_log_size - 1;
  128. lep = &dev->rdev.wr_log[idx];
  129. while (idx != end) {
  130. if (lep->valid) {
  131. if (!prev_ts_set) {
  132. prev_ts_set = 1;
  133. prev_ts = lep->poll_host_ts;
  134. }
  135. seq_printf(seq, "%04u: sec %lu nsec %lu qid %u opcode "
  136. "%u %s 0x%x host_wr_delta sec %lu nsec %lu "
  137. "post_sge_ts 0x%llx cqe_sge_ts 0x%llx "
  138. "poll_sge_ts 0x%llx post_poll_delta_ns %llu "
  139. "cqe_poll_delta_ns %llu\n",
  140. idx,
  141. timespec_sub(lep->poll_host_ts,
  142. prev_ts).tv_sec,
  143. timespec_sub(lep->poll_host_ts,
  144. prev_ts).tv_nsec,
  145. lep->qid, lep->opcode,
  146. lep->opcode == FW_RI_RECEIVE ?
  147. "msn" : "wrid",
  148. lep->wr_id,
  149. timespec_sub(lep->poll_host_ts,
  150. lep->post_host_ts).tv_sec,
  151. timespec_sub(lep->poll_host_ts,
  152. lep->post_host_ts).tv_nsec,
  153. lep->post_sge_ts, lep->cqe_sge_ts,
  154. lep->poll_sge_ts,
  155. ts2ns(lep->poll_sge_ts - lep->post_sge_ts),
  156. ts2ns(lep->poll_sge_ts - lep->cqe_sge_ts));
  157. prev_ts = lep->poll_host_ts;
  158. }
  159. idx++;
  160. if (idx > (dev->rdev.wr_log_size - 1))
  161. idx = 0;
  162. lep = &dev->rdev.wr_log[idx];
  163. }
  164. #undef ts2ns
  165. return 0;
  166. }
  167. static int wr_log_open(struct inode *inode, struct file *file)
  168. {
  169. return single_open(file, wr_log_show, inode->i_private);
  170. }
  171. static ssize_t wr_log_clear(struct file *file, const char __user *buf,
  172. size_t count, loff_t *pos)
  173. {
  174. struct c4iw_dev *dev = ((struct seq_file *)file->private_data)->private;
  175. int i;
  176. if (dev->rdev.wr_log)
  177. for (i = 0; i < dev->rdev.wr_log_size; i++)
  178. dev->rdev.wr_log[i].valid = 0;
  179. return count;
  180. }
  181. static const struct file_operations wr_log_debugfs_fops = {
  182. .owner = THIS_MODULE,
  183. .open = wr_log_open,
  184. .release = single_release,
  185. .read = seq_read,
  186. .llseek = seq_lseek,
  187. .write = wr_log_clear,
  188. };
  189. static struct sockaddr_in zero_sin = {
  190. .sin_family = AF_INET,
  191. };
  192. static struct sockaddr_in6 zero_sin6 = {
  193. .sin6_family = AF_INET6,
  194. };
  195. static void set_ep_sin_addrs(struct c4iw_ep *ep,
  196. struct sockaddr_in **lsin,
  197. struct sockaddr_in **rsin,
  198. struct sockaddr_in **m_lsin,
  199. struct sockaddr_in **m_rsin)
  200. {
  201. struct iw_cm_id *id = ep->com.cm_id;
  202. *lsin = (struct sockaddr_in *)&ep->com.local_addr;
  203. *rsin = (struct sockaddr_in *)&ep->com.remote_addr;
  204. if (id) {
  205. *m_lsin = (struct sockaddr_in *)&id->m_local_addr;
  206. *m_rsin = (struct sockaddr_in *)&id->m_remote_addr;
  207. } else {
  208. *m_lsin = &zero_sin;
  209. *m_rsin = &zero_sin;
  210. }
  211. }
  212. static void set_ep_sin6_addrs(struct c4iw_ep *ep,
  213. struct sockaddr_in6 **lsin6,
  214. struct sockaddr_in6 **rsin6,
  215. struct sockaddr_in6 **m_lsin6,
  216. struct sockaddr_in6 **m_rsin6)
  217. {
  218. struct iw_cm_id *id = ep->com.cm_id;
  219. *lsin6 = (struct sockaddr_in6 *)&ep->com.local_addr;
  220. *rsin6 = (struct sockaddr_in6 *)&ep->com.remote_addr;
  221. if (id) {
  222. *m_lsin6 = (struct sockaddr_in6 *)&id->m_local_addr;
  223. *m_rsin6 = (struct sockaddr_in6 *)&id->m_remote_addr;
  224. } else {
  225. *m_lsin6 = &zero_sin6;
  226. *m_rsin6 = &zero_sin6;
  227. }
  228. }
  229. static int dump_qp(int id, void *p, void *data)
  230. {
  231. struct c4iw_qp *qp = p;
  232. struct c4iw_debugfs_data *qpd = data;
  233. int space;
  234. int cc;
  235. if (id != qp->wq.sq.qid)
  236. return 0;
  237. space = qpd->bufsize - qpd->pos - 1;
  238. if (space == 0)
  239. return 1;
  240. if (qp->ep) {
  241. struct c4iw_ep *ep = qp->ep;
  242. if (ep->com.local_addr.ss_family == AF_INET) {
  243. struct sockaddr_in *lsin;
  244. struct sockaddr_in *rsin;
  245. struct sockaddr_in *m_lsin;
  246. struct sockaddr_in *m_rsin;
  247. set_ep_sin_addrs(ep, &lsin, &rsin, &m_lsin, &m_rsin);
  248. cc = snprintf(qpd->buf + qpd->pos, space,
  249. "rc qp sq id %u rq id %u state %u "
  250. "onchip %u ep tid %u state %u "
  251. "%pI4:%u/%u->%pI4:%u/%u\n",
  252. qp->wq.sq.qid, qp->wq.rq.qid,
  253. (int)qp->attr.state,
  254. qp->wq.sq.flags & T4_SQ_ONCHIP,
  255. ep->hwtid, (int)ep->com.state,
  256. &lsin->sin_addr, ntohs(lsin->sin_port),
  257. ntohs(m_lsin->sin_port),
  258. &rsin->sin_addr, ntohs(rsin->sin_port),
  259. ntohs(m_rsin->sin_port));
  260. } else {
  261. struct sockaddr_in6 *lsin6;
  262. struct sockaddr_in6 *rsin6;
  263. struct sockaddr_in6 *m_lsin6;
  264. struct sockaddr_in6 *m_rsin6;
  265. set_ep_sin6_addrs(ep, &lsin6, &rsin6, &m_lsin6,
  266. &m_rsin6);
  267. cc = snprintf(qpd->buf + qpd->pos, space,
  268. "rc qp sq id %u rq id %u state %u "
  269. "onchip %u ep tid %u state %u "
  270. "%pI6:%u/%u->%pI6:%u/%u\n",
  271. qp->wq.sq.qid, qp->wq.rq.qid,
  272. (int)qp->attr.state,
  273. qp->wq.sq.flags & T4_SQ_ONCHIP,
  274. ep->hwtid, (int)ep->com.state,
  275. &lsin6->sin6_addr,
  276. ntohs(lsin6->sin6_port),
  277. ntohs(m_lsin6->sin6_port),
  278. &rsin6->sin6_addr,
  279. ntohs(rsin6->sin6_port),
  280. ntohs(m_rsin6->sin6_port));
  281. }
  282. } else
  283. cc = snprintf(qpd->buf + qpd->pos, space,
  284. "qp sq id %u rq id %u state %u onchip %u\n",
  285. qp->wq.sq.qid, qp->wq.rq.qid,
  286. (int)qp->attr.state,
  287. qp->wq.sq.flags & T4_SQ_ONCHIP);
  288. if (cc < space)
  289. qpd->pos += cc;
  290. return 0;
  291. }
  292. static int qp_release(struct inode *inode, struct file *file)
  293. {
  294. struct c4iw_debugfs_data *qpd = file->private_data;
  295. if (!qpd) {
  296. printk(KERN_INFO "%s null qpd?\n", __func__);
  297. return 0;
  298. }
  299. vfree(qpd->buf);
  300. kfree(qpd);
  301. return 0;
  302. }
  303. static int qp_open(struct inode *inode, struct file *file)
  304. {
  305. struct c4iw_debugfs_data *qpd;
  306. int count = 1;
  307. qpd = kmalloc(sizeof *qpd, GFP_KERNEL);
  308. if (!qpd)
  309. return -ENOMEM;
  310. qpd->devp = inode->i_private;
  311. qpd->pos = 0;
  312. spin_lock_irq(&qpd->devp->lock);
  313. idr_for_each(&qpd->devp->qpidr, count_idrs, &count);
  314. spin_unlock_irq(&qpd->devp->lock);
  315. qpd->bufsize = count * 180;
  316. qpd->buf = vmalloc(qpd->bufsize);
  317. if (!qpd->buf) {
  318. kfree(qpd);
  319. return -ENOMEM;
  320. }
  321. spin_lock_irq(&qpd->devp->lock);
  322. idr_for_each(&qpd->devp->qpidr, dump_qp, qpd);
  323. spin_unlock_irq(&qpd->devp->lock);
  324. qpd->buf[qpd->pos++] = 0;
  325. file->private_data = qpd;
  326. return 0;
  327. }
  328. static const struct file_operations qp_debugfs_fops = {
  329. .owner = THIS_MODULE,
  330. .open = qp_open,
  331. .release = qp_release,
  332. .read = debugfs_read,
  333. .llseek = default_llseek,
  334. };
  335. static int dump_stag(int id, void *p, void *data)
  336. {
  337. struct c4iw_debugfs_data *stagd = data;
  338. int space;
  339. int cc;
  340. struct fw_ri_tpte tpte;
  341. int ret;
  342. space = stagd->bufsize - stagd->pos - 1;
  343. if (space == 0)
  344. return 1;
  345. ret = cxgb4_read_tpte(stagd->devp->rdev.lldi.ports[0], (u32)id<<8,
  346. (__be32 *)&tpte);
  347. if (ret) {
  348. dev_err(&stagd->devp->rdev.lldi.pdev->dev,
  349. "%s cxgb4_read_tpte err %d\n", __func__, ret);
  350. return ret;
  351. }
  352. cc = snprintf(stagd->buf + stagd->pos, space,
  353. "stag: idx 0x%x valid %d key 0x%x state %d pdid %d "
  354. "perm 0x%x ps %d len 0x%llx va 0x%llx\n",
  355. (u32)id<<8,
  356. FW_RI_TPTE_VALID_G(ntohl(tpte.valid_to_pdid)),
  357. FW_RI_TPTE_STAGKEY_G(ntohl(tpte.valid_to_pdid)),
  358. FW_RI_TPTE_STAGSTATE_G(ntohl(tpte.valid_to_pdid)),
  359. FW_RI_TPTE_PDID_G(ntohl(tpte.valid_to_pdid)),
  360. FW_RI_TPTE_PERM_G(ntohl(tpte.locread_to_qpid)),
  361. FW_RI_TPTE_PS_G(ntohl(tpte.locread_to_qpid)),
  362. ((u64)ntohl(tpte.len_hi) << 32) | ntohl(tpte.len_lo),
  363. ((u64)ntohl(tpte.va_hi) << 32) | ntohl(tpte.va_lo_fbo));
  364. if (cc < space)
  365. stagd->pos += cc;
  366. return 0;
  367. }
  368. static int stag_release(struct inode *inode, struct file *file)
  369. {
  370. struct c4iw_debugfs_data *stagd = file->private_data;
  371. if (!stagd) {
  372. printk(KERN_INFO "%s null stagd?\n", __func__);
  373. return 0;
  374. }
  375. vfree(stagd->buf);
  376. kfree(stagd);
  377. return 0;
  378. }
  379. static int stag_open(struct inode *inode, struct file *file)
  380. {
  381. struct c4iw_debugfs_data *stagd;
  382. int ret = 0;
  383. int count = 1;
  384. stagd = kmalloc(sizeof *stagd, GFP_KERNEL);
  385. if (!stagd) {
  386. ret = -ENOMEM;
  387. goto out;
  388. }
  389. stagd->devp = inode->i_private;
  390. stagd->pos = 0;
  391. spin_lock_irq(&stagd->devp->lock);
  392. idr_for_each(&stagd->devp->mmidr, count_idrs, &count);
  393. spin_unlock_irq(&stagd->devp->lock);
  394. stagd->bufsize = count * 256;
  395. stagd->buf = vmalloc(stagd->bufsize);
  396. if (!stagd->buf) {
  397. ret = -ENOMEM;
  398. goto err1;
  399. }
  400. spin_lock_irq(&stagd->devp->lock);
  401. idr_for_each(&stagd->devp->mmidr, dump_stag, stagd);
  402. spin_unlock_irq(&stagd->devp->lock);
  403. stagd->buf[stagd->pos++] = 0;
  404. file->private_data = stagd;
  405. goto out;
  406. err1:
  407. kfree(stagd);
  408. out:
  409. return ret;
  410. }
  411. static const struct file_operations stag_debugfs_fops = {
  412. .owner = THIS_MODULE,
  413. .open = stag_open,
  414. .release = stag_release,
  415. .read = debugfs_read,
  416. .llseek = default_llseek,
  417. };
  418. static char *db_state_str[] = {"NORMAL", "FLOW_CONTROL", "RECOVERY", "STOPPED"};
  419. static int stats_show(struct seq_file *seq, void *v)
  420. {
  421. struct c4iw_dev *dev = seq->private;
  422. seq_printf(seq, " Object: %10s %10s %10s %10s\n", "Total", "Current",
  423. "Max", "Fail");
  424. seq_printf(seq, " PDID: %10llu %10llu %10llu %10llu\n",
  425. dev->rdev.stats.pd.total, dev->rdev.stats.pd.cur,
  426. dev->rdev.stats.pd.max, dev->rdev.stats.pd.fail);
  427. seq_printf(seq, " QID: %10llu %10llu %10llu %10llu\n",
  428. dev->rdev.stats.qid.total, dev->rdev.stats.qid.cur,
  429. dev->rdev.stats.qid.max, dev->rdev.stats.qid.fail);
  430. seq_printf(seq, " TPTMEM: %10llu %10llu %10llu %10llu\n",
  431. dev->rdev.stats.stag.total, dev->rdev.stats.stag.cur,
  432. dev->rdev.stats.stag.max, dev->rdev.stats.stag.fail);
  433. seq_printf(seq, " PBLMEM: %10llu %10llu %10llu %10llu\n",
  434. dev->rdev.stats.pbl.total, dev->rdev.stats.pbl.cur,
  435. dev->rdev.stats.pbl.max, dev->rdev.stats.pbl.fail);
  436. seq_printf(seq, " RQTMEM: %10llu %10llu %10llu %10llu\n",
  437. dev->rdev.stats.rqt.total, dev->rdev.stats.rqt.cur,
  438. dev->rdev.stats.rqt.max, dev->rdev.stats.rqt.fail);
  439. seq_printf(seq, " OCQPMEM: %10llu %10llu %10llu %10llu\n",
  440. dev->rdev.stats.ocqp.total, dev->rdev.stats.ocqp.cur,
  441. dev->rdev.stats.ocqp.max, dev->rdev.stats.ocqp.fail);
  442. seq_printf(seq, " DB FULL: %10llu\n", dev->rdev.stats.db_full);
  443. seq_printf(seq, " DB EMPTY: %10llu\n", dev->rdev.stats.db_empty);
  444. seq_printf(seq, " DB DROP: %10llu\n", dev->rdev.stats.db_drop);
  445. seq_printf(seq, " DB State: %s Transitions %llu FC Interruptions %llu\n",
  446. db_state_str[dev->db_state],
  447. dev->rdev.stats.db_state_transitions,
  448. dev->rdev.stats.db_fc_interruptions);
  449. seq_printf(seq, "TCAM_FULL: %10llu\n", dev->rdev.stats.tcam_full);
  450. seq_printf(seq, "ACT_OFLD_CONN_FAILS: %10llu\n",
  451. dev->rdev.stats.act_ofld_conn_fails);
  452. seq_printf(seq, "PAS_OFLD_CONN_FAILS: %10llu\n",
  453. dev->rdev.stats.pas_ofld_conn_fails);
  454. seq_printf(seq, "NEG_ADV_RCVD: %10llu\n", dev->rdev.stats.neg_adv);
  455. seq_printf(seq, "AVAILABLE IRD: %10u\n", dev->avail_ird);
  456. return 0;
  457. }
  458. static int stats_open(struct inode *inode, struct file *file)
  459. {
  460. return single_open(file, stats_show, inode->i_private);
  461. }
  462. static ssize_t stats_clear(struct file *file, const char __user *buf,
  463. size_t count, loff_t *pos)
  464. {
  465. struct c4iw_dev *dev = ((struct seq_file *)file->private_data)->private;
  466. mutex_lock(&dev->rdev.stats.lock);
  467. dev->rdev.stats.pd.max = 0;
  468. dev->rdev.stats.pd.fail = 0;
  469. dev->rdev.stats.qid.max = 0;
  470. dev->rdev.stats.qid.fail = 0;
  471. dev->rdev.stats.stag.max = 0;
  472. dev->rdev.stats.stag.fail = 0;
  473. dev->rdev.stats.pbl.max = 0;
  474. dev->rdev.stats.pbl.fail = 0;
  475. dev->rdev.stats.rqt.max = 0;
  476. dev->rdev.stats.rqt.fail = 0;
  477. dev->rdev.stats.ocqp.max = 0;
  478. dev->rdev.stats.ocqp.fail = 0;
  479. dev->rdev.stats.db_full = 0;
  480. dev->rdev.stats.db_empty = 0;
  481. dev->rdev.stats.db_drop = 0;
  482. dev->rdev.stats.db_state_transitions = 0;
  483. dev->rdev.stats.tcam_full = 0;
  484. dev->rdev.stats.act_ofld_conn_fails = 0;
  485. dev->rdev.stats.pas_ofld_conn_fails = 0;
  486. mutex_unlock(&dev->rdev.stats.lock);
  487. return count;
  488. }
  489. static const struct file_operations stats_debugfs_fops = {
  490. .owner = THIS_MODULE,
  491. .open = stats_open,
  492. .release = single_release,
  493. .read = seq_read,
  494. .llseek = seq_lseek,
  495. .write = stats_clear,
  496. };
  497. static int dump_ep(int id, void *p, void *data)
  498. {
  499. struct c4iw_ep *ep = p;
  500. struct c4iw_debugfs_data *epd = data;
  501. int space;
  502. int cc;
  503. space = epd->bufsize - epd->pos - 1;
  504. if (space == 0)
  505. return 1;
  506. if (ep->com.local_addr.ss_family == AF_INET) {
  507. struct sockaddr_in *lsin;
  508. struct sockaddr_in *rsin;
  509. struct sockaddr_in *m_lsin;
  510. struct sockaddr_in *m_rsin;
  511. set_ep_sin_addrs(ep, &lsin, &rsin, &m_lsin, &m_rsin);
  512. cc = snprintf(epd->buf + epd->pos, space,
  513. "ep %p cm_id %p qp %p state %d flags 0x%lx "
  514. "history 0x%lx hwtid %d atid %d "
  515. "conn_na %u abort_na %u "
  516. "%pI4:%d/%d <-> %pI4:%d/%d\n",
  517. ep, ep->com.cm_id, ep->com.qp,
  518. (int)ep->com.state, ep->com.flags,
  519. ep->com.history, ep->hwtid, ep->atid,
  520. ep->stats.connect_neg_adv,
  521. ep->stats.abort_neg_adv,
  522. &lsin->sin_addr, ntohs(lsin->sin_port),
  523. ntohs(m_lsin->sin_port),
  524. &rsin->sin_addr, ntohs(rsin->sin_port),
  525. ntohs(m_rsin->sin_port));
  526. } else {
  527. struct sockaddr_in6 *lsin6;
  528. struct sockaddr_in6 *rsin6;
  529. struct sockaddr_in6 *m_lsin6;
  530. struct sockaddr_in6 *m_rsin6;
  531. set_ep_sin6_addrs(ep, &lsin6, &rsin6, &m_lsin6, &m_rsin6);
  532. cc = snprintf(epd->buf + epd->pos, space,
  533. "ep %p cm_id %p qp %p state %d flags 0x%lx "
  534. "history 0x%lx hwtid %d atid %d "
  535. "conn_na %u abort_na %u "
  536. "%pI6:%d/%d <-> %pI6:%d/%d\n",
  537. ep, ep->com.cm_id, ep->com.qp,
  538. (int)ep->com.state, ep->com.flags,
  539. ep->com.history, ep->hwtid, ep->atid,
  540. ep->stats.connect_neg_adv,
  541. ep->stats.abort_neg_adv,
  542. &lsin6->sin6_addr, ntohs(lsin6->sin6_port),
  543. ntohs(m_lsin6->sin6_port),
  544. &rsin6->sin6_addr, ntohs(rsin6->sin6_port),
  545. ntohs(m_rsin6->sin6_port));
  546. }
  547. if (cc < space)
  548. epd->pos += cc;
  549. return 0;
  550. }
  551. static int dump_listen_ep(int id, void *p, void *data)
  552. {
  553. struct c4iw_listen_ep *ep = p;
  554. struct c4iw_debugfs_data *epd = data;
  555. int space;
  556. int cc;
  557. space = epd->bufsize - epd->pos - 1;
  558. if (space == 0)
  559. return 1;
  560. if (ep->com.local_addr.ss_family == AF_INET) {
  561. struct sockaddr_in *lsin = (struct sockaddr_in *)
  562. &ep->com.cm_id->local_addr;
  563. struct sockaddr_in *m_lsin = (struct sockaddr_in *)
  564. &ep->com.cm_id->m_local_addr;
  565. cc = snprintf(epd->buf + epd->pos, space,
  566. "ep %p cm_id %p state %d flags 0x%lx stid %d "
  567. "backlog %d %pI4:%d/%d\n",
  568. ep, ep->com.cm_id, (int)ep->com.state,
  569. ep->com.flags, ep->stid, ep->backlog,
  570. &lsin->sin_addr, ntohs(lsin->sin_port),
  571. ntohs(m_lsin->sin_port));
  572. } else {
  573. struct sockaddr_in6 *lsin6 = (struct sockaddr_in6 *)
  574. &ep->com.cm_id->local_addr;
  575. struct sockaddr_in6 *m_lsin6 = (struct sockaddr_in6 *)
  576. &ep->com.cm_id->m_local_addr;
  577. cc = snprintf(epd->buf + epd->pos, space,
  578. "ep %p cm_id %p state %d flags 0x%lx stid %d "
  579. "backlog %d %pI6:%d/%d\n",
  580. ep, ep->com.cm_id, (int)ep->com.state,
  581. ep->com.flags, ep->stid, ep->backlog,
  582. &lsin6->sin6_addr, ntohs(lsin6->sin6_port),
  583. ntohs(m_lsin6->sin6_port));
  584. }
  585. if (cc < space)
  586. epd->pos += cc;
  587. return 0;
  588. }
  589. static int ep_release(struct inode *inode, struct file *file)
  590. {
  591. struct c4iw_debugfs_data *epd = file->private_data;
  592. if (!epd) {
  593. pr_info("%s null qpd?\n", __func__);
  594. return 0;
  595. }
  596. vfree(epd->buf);
  597. kfree(epd);
  598. return 0;
  599. }
  600. static int ep_open(struct inode *inode, struct file *file)
  601. {
  602. struct c4iw_debugfs_data *epd;
  603. int ret = 0;
  604. int count = 1;
  605. epd = kmalloc(sizeof(*epd), GFP_KERNEL);
  606. if (!epd) {
  607. ret = -ENOMEM;
  608. goto out;
  609. }
  610. epd->devp = inode->i_private;
  611. epd->pos = 0;
  612. spin_lock_irq(&epd->devp->lock);
  613. idr_for_each(&epd->devp->hwtid_idr, count_idrs, &count);
  614. idr_for_each(&epd->devp->atid_idr, count_idrs, &count);
  615. idr_for_each(&epd->devp->stid_idr, count_idrs, &count);
  616. spin_unlock_irq(&epd->devp->lock);
  617. epd->bufsize = count * 240;
  618. epd->buf = vmalloc(epd->bufsize);
  619. if (!epd->buf) {
  620. ret = -ENOMEM;
  621. goto err1;
  622. }
  623. spin_lock_irq(&epd->devp->lock);
  624. idr_for_each(&epd->devp->hwtid_idr, dump_ep, epd);
  625. idr_for_each(&epd->devp->atid_idr, dump_ep, epd);
  626. idr_for_each(&epd->devp->stid_idr, dump_listen_ep, epd);
  627. spin_unlock_irq(&epd->devp->lock);
  628. file->private_data = epd;
  629. goto out;
  630. err1:
  631. kfree(epd);
  632. out:
  633. return ret;
  634. }
  635. static const struct file_operations ep_debugfs_fops = {
  636. .owner = THIS_MODULE,
  637. .open = ep_open,
  638. .release = ep_release,
  639. .read = debugfs_read,
  640. };
  641. static int setup_debugfs(struct c4iw_dev *devp)
  642. {
  643. if (!devp->debugfs_root)
  644. return -1;
  645. debugfs_create_file_size("qps", S_IWUSR, devp->debugfs_root,
  646. (void *)devp, &qp_debugfs_fops, 4096);
  647. debugfs_create_file_size("stags", S_IWUSR, devp->debugfs_root,
  648. (void *)devp, &stag_debugfs_fops, 4096);
  649. debugfs_create_file_size("stats", S_IWUSR, devp->debugfs_root,
  650. (void *)devp, &stats_debugfs_fops, 4096);
  651. debugfs_create_file_size("eps", S_IWUSR, devp->debugfs_root,
  652. (void *)devp, &ep_debugfs_fops, 4096);
  653. if (c4iw_wr_log)
  654. debugfs_create_file_size("wr_log", S_IWUSR, devp->debugfs_root,
  655. (void *)devp, &wr_log_debugfs_fops, 4096);
  656. return 0;
  657. }
  658. void c4iw_release_dev_ucontext(struct c4iw_rdev *rdev,
  659. struct c4iw_dev_ucontext *uctx)
  660. {
  661. struct list_head *pos, *nxt;
  662. struct c4iw_qid_list *entry;
  663. mutex_lock(&uctx->lock);
  664. list_for_each_safe(pos, nxt, &uctx->qpids) {
  665. entry = list_entry(pos, struct c4iw_qid_list, entry);
  666. list_del_init(&entry->entry);
  667. if (!(entry->qid & rdev->qpmask)) {
  668. c4iw_put_resource(&rdev->resource.qid_table,
  669. entry->qid);
  670. mutex_lock(&rdev->stats.lock);
  671. rdev->stats.qid.cur -= rdev->qpmask + 1;
  672. mutex_unlock(&rdev->stats.lock);
  673. }
  674. kfree(entry);
  675. }
  676. list_for_each_safe(pos, nxt, &uctx->qpids) {
  677. entry = list_entry(pos, struct c4iw_qid_list, entry);
  678. list_del_init(&entry->entry);
  679. kfree(entry);
  680. }
  681. mutex_unlock(&uctx->lock);
  682. }
  683. void c4iw_init_dev_ucontext(struct c4iw_rdev *rdev,
  684. struct c4iw_dev_ucontext *uctx)
  685. {
  686. INIT_LIST_HEAD(&uctx->qpids);
  687. INIT_LIST_HEAD(&uctx->cqids);
  688. mutex_init(&uctx->lock);
  689. }
  690. /* Caller takes care of locking if needed */
  691. static int c4iw_rdev_open(struct c4iw_rdev *rdev)
  692. {
  693. int err;
  694. c4iw_init_dev_ucontext(rdev, &rdev->uctx);
  695. /*
  696. * This implementation assumes udb_density == ucq_density! Eventually
  697. * we might need to support this but for now fail the open. Also the
  698. * cqid and qpid range must match for now.
  699. */
  700. if (rdev->lldi.udb_density != rdev->lldi.ucq_density) {
  701. pr_err(MOD "%s: unsupported udb/ucq densities %u/%u\n",
  702. pci_name(rdev->lldi.pdev), rdev->lldi.udb_density,
  703. rdev->lldi.ucq_density);
  704. return -EINVAL;
  705. }
  706. if (rdev->lldi.vr->qp.start != rdev->lldi.vr->cq.start ||
  707. rdev->lldi.vr->qp.size != rdev->lldi.vr->cq.size) {
  708. pr_err(MOD "%s: unsupported qp and cq id ranges "
  709. "qp start %u size %u cq start %u size %u\n",
  710. pci_name(rdev->lldi.pdev), rdev->lldi.vr->qp.start,
  711. rdev->lldi.vr->qp.size, rdev->lldi.vr->cq.size,
  712. rdev->lldi.vr->cq.size);
  713. return -EINVAL;
  714. }
  715. rdev->qpmask = rdev->lldi.udb_density - 1;
  716. rdev->cqmask = rdev->lldi.ucq_density - 1;
  717. PDBG("%s dev %s stag start 0x%0x size 0x%0x num stags %d "
  718. "pbl start 0x%0x size 0x%0x rq start 0x%0x size 0x%0x "
  719. "qp qid start %u size %u cq qid start %u size %u\n",
  720. __func__, pci_name(rdev->lldi.pdev), rdev->lldi.vr->stag.start,
  721. rdev->lldi.vr->stag.size, c4iw_num_stags(rdev),
  722. rdev->lldi.vr->pbl.start,
  723. rdev->lldi.vr->pbl.size, rdev->lldi.vr->rq.start,
  724. rdev->lldi.vr->rq.size,
  725. rdev->lldi.vr->qp.start,
  726. rdev->lldi.vr->qp.size,
  727. rdev->lldi.vr->cq.start,
  728. rdev->lldi.vr->cq.size);
  729. PDBG("udb %pR db_reg %p gts_reg %p "
  730. "qpmask 0x%x cqmask 0x%x\n",
  731. &rdev->lldi.pdev->resource[2],
  732. rdev->lldi.db_reg, rdev->lldi.gts_reg,
  733. rdev->qpmask, rdev->cqmask);
  734. if (c4iw_num_stags(rdev) == 0)
  735. return -EINVAL;
  736. rdev->stats.pd.total = T4_MAX_NUM_PD;
  737. rdev->stats.stag.total = rdev->lldi.vr->stag.size;
  738. rdev->stats.pbl.total = rdev->lldi.vr->pbl.size;
  739. rdev->stats.rqt.total = rdev->lldi.vr->rq.size;
  740. rdev->stats.ocqp.total = rdev->lldi.vr->ocq.size;
  741. rdev->stats.qid.total = rdev->lldi.vr->qp.size;
  742. err = c4iw_init_resource(rdev, c4iw_num_stags(rdev), T4_MAX_NUM_PD);
  743. if (err) {
  744. printk(KERN_ERR MOD "error %d initializing resources\n", err);
  745. return err;
  746. }
  747. err = c4iw_pblpool_create(rdev);
  748. if (err) {
  749. printk(KERN_ERR MOD "error %d initializing pbl pool\n", err);
  750. goto destroy_resource;
  751. }
  752. err = c4iw_rqtpool_create(rdev);
  753. if (err) {
  754. printk(KERN_ERR MOD "error %d initializing rqt pool\n", err);
  755. goto destroy_pblpool;
  756. }
  757. err = c4iw_ocqp_pool_create(rdev);
  758. if (err) {
  759. printk(KERN_ERR MOD "error %d initializing ocqp pool\n", err);
  760. goto destroy_rqtpool;
  761. }
  762. rdev->status_page = (struct t4_dev_status_page *)
  763. __get_free_page(GFP_KERNEL);
  764. if (!rdev->status_page) {
  765. err = -ENOMEM;
  766. goto destroy_ocqp_pool;
  767. }
  768. rdev->status_page->qp_start = rdev->lldi.vr->qp.start;
  769. rdev->status_page->qp_size = rdev->lldi.vr->qp.size;
  770. rdev->status_page->cq_start = rdev->lldi.vr->cq.start;
  771. rdev->status_page->cq_size = rdev->lldi.vr->cq.size;
  772. if (c4iw_wr_log) {
  773. rdev->wr_log = kzalloc((1 << c4iw_wr_log_size_order) *
  774. sizeof(*rdev->wr_log), GFP_KERNEL);
  775. if (rdev->wr_log) {
  776. rdev->wr_log_size = 1 << c4iw_wr_log_size_order;
  777. atomic_set(&rdev->wr_log_idx, 0);
  778. }
  779. }
  780. rdev->free_workq = create_singlethread_workqueue("iw_cxgb4_free");
  781. if (!rdev->free_workq) {
  782. err = -ENOMEM;
  783. goto err_free_status_page;
  784. }
  785. rdev->status_page->db_off = 0;
  786. return 0;
  787. err_free_status_page:
  788. free_page((unsigned long)rdev->status_page);
  789. destroy_ocqp_pool:
  790. c4iw_ocqp_pool_destroy(rdev);
  791. destroy_rqtpool:
  792. c4iw_rqtpool_destroy(rdev);
  793. destroy_pblpool:
  794. c4iw_pblpool_destroy(rdev);
  795. destroy_resource:
  796. c4iw_destroy_resource(&rdev->resource);
  797. return err;
  798. }
  799. static void c4iw_rdev_close(struct c4iw_rdev *rdev)
  800. {
  801. destroy_workqueue(rdev->free_workq);
  802. kfree(rdev->wr_log);
  803. free_page((unsigned long)rdev->status_page);
  804. c4iw_pblpool_destroy(rdev);
  805. c4iw_rqtpool_destroy(rdev);
  806. c4iw_destroy_resource(&rdev->resource);
  807. }
  808. static void c4iw_dealloc(struct uld_ctx *ctx)
  809. {
  810. c4iw_rdev_close(&ctx->dev->rdev);
  811. WARN_ON_ONCE(!idr_is_empty(&ctx->dev->cqidr));
  812. idr_destroy(&ctx->dev->cqidr);
  813. WARN_ON_ONCE(!idr_is_empty(&ctx->dev->qpidr));
  814. idr_destroy(&ctx->dev->qpidr);
  815. WARN_ON_ONCE(!idr_is_empty(&ctx->dev->mmidr));
  816. idr_destroy(&ctx->dev->mmidr);
  817. wait_event(ctx->dev->wait, idr_is_empty(&ctx->dev->hwtid_idr));
  818. idr_destroy(&ctx->dev->hwtid_idr);
  819. idr_destroy(&ctx->dev->stid_idr);
  820. idr_destroy(&ctx->dev->atid_idr);
  821. if (ctx->dev->rdev.bar2_kva)
  822. iounmap(ctx->dev->rdev.bar2_kva);
  823. if (ctx->dev->rdev.oc_mw_kva)
  824. iounmap(ctx->dev->rdev.oc_mw_kva);
  825. ib_dealloc_device(&ctx->dev->ibdev);
  826. ctx->dev = NULL;
  827. }
  828. static void c4iw_remove(struct uld_ctx *ctx)
  829. {
  830. PDBG("%s c4iw_dev %p\n", __func__, ctx->dev);
  831. c4iw_unregister_device(ctx->dev);
  832. c4iw_dealloc(ctx);
  833. }
  834. static int rdma_supported(const struct cxgb4_lld_info *infop)
  835. {
  836. return infop->vr->stag.size > 0 && infop->vr->pbl.size > 0 &&
  837. infop->vr->rq.size > 0 && infop->vr->qp.size > 0 &&
  838. infop->vr->cq.size > 0;
  839. }
  840. static struct c4iw_dev *c4iw_alloc(const struct cxgb4_lld_info *infop)
  841. {
  842. struct c4iw_dev *devp;
  843. int ret;
  844. if (!rdma_supported(infop)) {
  845. printk(KERN_INFO MOD "%s: RDMA not supported on this device.\n",
  846. pci_name(infop->pdev));
  847. return ERR_PTR(-ENOSYS);
  848. }
  849. if (!ocqp_supported(infop))
  850. pr_info("%s: On-Chip Queues not supported on this device.\n",
  851. pci_name(infop->pdev));
  852. devp = (struct c4iw_dev *)ib_alloc_device(sizeof(*devp));
  853. if (!devp) {
  854. printk(KERN_ERR MOD "Cannot allocate ib device\n");
  855. return ERR_PTR(-ENOMEM);
  856. }
  857. devp->rdev.lldi = *infop;
  858. /* init various hw-queue params based on lld info */
  859. PDBG("%s: Ing. padding boundary is %d, egrsstatuspagesize = %d\n",
  860. __func__, devp->rdev.lldi.sge_ingpadboundary,
  861. devp->rdev.lldi.sge_egrstatuspagesize);
  862. devp->rdev.hw_queue.t4_eq_status_entries =
  863. devp->rdev.lldi.sge_ingpadboundary > 64 ? 2 : 1;
  864. devp->rdev.hw_queue.t4_max_eq_size = 65520;
  865. devp->rdev.hw_queue.t4_max_iq_size = 65520;
  866. devp->rdev.hw_queue.t4_max_rq_size = 8192 -
  867. devp->rdev.hw_queue.t4_eq_status_entries - 1;
  868. devp->rdev.hw_queue.t4_max_sq_size =
  869. devp->rdev.hw_queue.t4_max_eq_size -
  870. devp->rdev.hw_queue.t4_eq_status_entries - 1;
  871. devp->rdev.hw_queue.t4_max_qp_depth =
  872. devp->rdev.hw_queue.t4_max_rq_size;
  873. devp->rdev.hw_queue.t4_max_cq_depth =
  874. devp->rdev.hw_queue.t4_max_iq_size - 2;
  875. devp->rdev.hw_queue.t4_stat_len =
  876. devp->rdev.lldi.sge_egrstatuspagesize;
  877. /*
  878. * For T5/T6 devices, we map all of BAR2 with WC.
  879. * For T4 devices with onchip qp mem, we map only that part
  880. * of BAR2 with WC.
  881. */
  882. devp->rdev.bar2_pa = pci_resource_start(devp->rdev.lldi.pdev, 2);
  883. if (!is_t4(devp->rdev.lldi.adapter_type)) {
  884. devp->rdev.bar2_kva = ioremap_wc(devp->rdev.bar2_pa,
  885. pci_resource_len(devp->rdev.lldi.pdev, 2));
  886. if (!devp->rdev.bar2_kva) {
  887. pr_err(MOD "Unable to ioremap BAR2\n");
  888. ib_dealloc_device(&devp->ibdev);
  889. return ERR_PTR(-EINVAL);
  890. }
  891. } else if (ocqp_supported(infop)) {
  892. devp->rdev.oc_mw_pa =
  893. pci_resource_start(devp->rdev.lldi.pdev, 2) +
  894. pci_resource_len(devp->rdev.lldi.pdev, 2) -
  895. roundup_pow_of_two(devp->rdev.lldi.vr->ocq.size);
  896. devp->rdev.oc_mw_kva = ioremap_wc(devp->rdev.oc_mw_pa,
  897. devp->rdev.lldi.vr->ocq.size);
  898. if (!devp->rdev.oc_mw_kva) {
  899. pr_err(MOD "Unable to ioremap onchip mem\n");
  900. ib_dealloc_device(&devp->ibdev);
  901. return ERR_PTR(-EINVAL);
  902. }
  903. }
  904. PDBG(KERN_INFO MOD "ocq memory: "
  905. "hw_start 0x%x size %u mw_pa 0x%lx mw_kva %p\n",
  906. devp->rdev.lldi.vr->ocq.start, devp->rdev.lldi.vr->ocq.size,
  907. devp->rdev.oc_mw_pa, devp->rdev.oc_mw_kva);
  908. ret = c4iw_rdev_open(&devp->rdev);
  909. if (ret) {
  910. printk(KERN_ERR MOD "Unable to open CXIO rdev err %d\n", ret);
  911. ib_dealloc_device(&devp->ibdev);
  912. return ERR_PTR(ret);
  913. }
  914. idr_init(&devp->cqidr);
  915. idr_init(&devp->qpidr);
  916. idr_init(&devp->mmidr);
  917. idr_init(&devp->hwtid_idr);
  918. idr_init(&devp->stid_idr);
  919. idr_init(&devp->atid_idr);
  920. spin_lock_init(&devp->lock);
  921. mutex_init(&devp->rdev.stats.lock);
  922. mutex_init(&devp->db_mutex);
  923. INIT_LIST_HEAD(&devp->db_fc_list);
  924. init_waitqueue_head(&devp->wait);
  925. devp->avail_ird = devp->rdev.lldi.max_ird_adapter;
  926. if (c4iw_debugfs_root) {
  927. devp->debugfs_root = debugfs_create_dir(
  928. pci_name(devp->rdev.lldi.pdev),
  929. c4iw_debugfs_root);
  930. setup_debugfs(devp);
  931. }
  932. return devp;
  933. }
  934. static void *c4iw_uld_add(const struct cxgb4_lld_info *infop)
  935. {
  936. struct uld_ctx *ctx;
  937. static int vers_printed;
  938. int i;
  939. if (!vers_printed++)
  940. pr_info("Chelsio T4/T5 RDMA Driver - version %s\n",
  941. DRV_VERSION);
  942. ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
  943. if (!ctx) {
  944. ctx = ERR_PTR(-ENOMEM);
  945. goto out;
  946. }
  947. ctx->lldi = *infop;
  948. PDBG("%s found device %s nchan %u nrxq %u ntxq %u nports %u\n",
  949. __func__, pci_name(ctx->lldi.pdev),
  950. ctx->lldi.nchan, ctx->lldi.nrxq,
  951. ctx->lldi.ntxq, ctx->lldi.nports);
  952. mutex_lock(&dev_mutex);
  953. list_add_tail(&ctx->entry, &uld_ctx_list);
  954. mutex_unlock(&dev_mutex);
  955. for (i = 0; i < ctx->lldi.nrxq; i++)
  956. PDBG("rxqid[%u] %u\n", i, ctx->lldi.rxq_ids[i]);
  957. out:
  958. return ctx;
  959. }
  960. static inline struct sk_buff *copy_gl_to_skb_pkt(const struct pkt_gl *gl,
  961. const __be64 *rsp,
  962. u32 pktshift)
  963. {
  964. struct sk_buff *skb;
  965. /*
  966. * Allocate space for cpl_pass_accept_req which will be synthesized by
  967. * driver. Once the driver synthesizes the request the skb will go
  968. * through the regular cpl_pass_accept_req processing.
  969. * The math here assumes sizeof cpl_pass_accept_req >= sizeof
  970. * cpl_rx_pkt.
  971. */
  972. skb = alloc_skb(gl->tot_len + sizeof(struct cpl_pass_accept_req) +
  973. sizeof(struct rss_header) - pktshift, GFP_ATOMIC);
  974. if (unlikely(!skb))
  975. return NULL;
  976. __skb_put(skb, gl->tot_len + sizeof(struct cpl_pass_accept_req) +
  977. sizeof(struct rss_header) - pktshift);
  978. /*
  979. * This skb will contain:
  980. * rss_header from the rspq descriptor (1 flit)
  981. * cpl_rx_pkt struct from the rspq descriptor (2 flits)
  982. * space for the difference between the size of an
  983. * rx_pkt and pass_accept_req cpl (1 flit)
  984. * the packet data from the gl
  985. */
  986. skb_copy_to_linear_data(skb, rsp, sizeof(struct cpl_pass_accept_req) +
  987. sizeof(struct rss_header));
  988. skb_copy_to_linear_data_offset(skb, sizeof(struct rss_header) +
  989. sizeof(struct cpl_pass_accept_req),
  990. gl->va + pktshift,
  991. gl->tot_len - pktshift);
  992. return skb;
  993. }
  994. static inline int recv_rx_pkt(struct c4iw_dev *dev, const struct pkt_gl *gl,
  995. const __be64 *rsp)
  996. {
  997. unsigned int opcode = *(u8 *)rsp;
  998. struct sk_buff *skb;
  999. if (opcode != CPL_RX_PKT)
  1000. goto out;
  1001. skb = copy_gl_to_skb_pkt(gl , rsp, dev->rdev.lldi.sge_pktshift);
  1002. if (skb == NULL)
  1003. goto out;
  1004. if (c4iw_handlers[opcode] == NULL) {
  1005. pr_info("%s no handler opcode 0x%x...\n", __func__,
  1006. opcode);
  1007. kfree_skb(skb);
  1008. goto out;
  1009. }
  1010. c4iw_handlers[opcode](dev, skb);
  1011. return 1;
  1012. out:
  1013. return 0;
  1014. }
  1015. static int c4iw_uld_rx_handler(void *handle, const __be64 *rsp,
  1016. const struct pkt_gl *gl)
  1017. {
  1018. struct uld_ctx *ctx = handle;
  1019. struct c4iw_dev *dev = ctx->dev;
  1020. struct sk_buff *skb;
  1021. u8 opcode;
  1022. if (gl == NULL) {
  1023. /* omit RSS and rsp_ctrl at end of descriptor */
  1024. unsigned int len = 64 - sizeof(struct rsp_ctrl) - 8;
  1025. skb = alloc_skb(256, GFP_ATOMIC);
  1026. if (!skb)
  1027. goto nomem;
  1028. __skb_put(skb, len);
  1029. skb_copy_to_linear_data(skb, &rsp[1], len);
  1030. } else if (gl == CXGB4_MSG_AN) {
  1031. const struct rsp_ctrl *rc = (void *)rsp;
  1032. u32 qid = be32_to_cpu(rc->pldbuflen_qid);
  1033. c4iw_ev_handler(dev, qid);
  1034. return 0;
  1035. } else if (unlikely(*(u8 *)rsp != *(u8 *)gl->va)) {
  1036. if (recv_rx_pkt(dev, gl, rsp))
  1037. return 0;
  1038. pr_info("%s: unexpected FL contents at %p, " \
  1039. "RSS %#llx, FL %#llx, len %u\n",
  1040. pci_name(ctx->lldi.pdev), gl->va,
  1041. (unsigned long long)be64_to_cpu(*rsp),
  1042. (unsigned long long)be64_to_cpu(
  1043. *(__force __be64 *)gl->va),
  1044. gl->tot_len);
  1045. return 0;
  1046. } else {
  1047. skb = cxgb4_pktgl_to_skb(gl, 128, 128);
  1048. if (unlikely(!skb))
  1049. goto nomem;
  1050. }
  1051. opcode = *(u8 *)rsp;
  1052. if (c4iw_handlers[opcode]) {
  1053. c4iw_handlers[opcode](dev, skb);
  1054. } else {
  1055. pr_info("%s no handler opcode 0x%x...\n", __func__,
  1056. opcode);
  1057. kfree_skb(skb);
  1058. }
  1059. return 0;
  1060. nomem:
  1061. return -1;
  1062. }
  1063. static int c4iw_uld_state_change(void *handle, enum cxgb4_state new_state)
  1064. {
  1065. struct uld_ctx *ctx = handle;
  1066. PDBG("%s new_state %u\n", __func__, new_state);
  1067. switch (new_state) {
  1068. case CXGB4_STATE_UP:
  1069. printk(KERN_INFO MOD "%s: Up\n", pci_name(ctx->lldi.pdev));
  1070. if (!ctx->dev) {
  1071. int ret;
  1072. ctx->dev = c4iw_alloc(&ctx->lldi);
  1073. if (IS_ERR(ctx->dev)) {
  1074. printk(KERN_ERR MOD
  1075. "%s: initialization failed: %ld\n",
  1076. pci_name(ctx->lldi.pdev),
  1077. PTR_ERR(ctx->dev));
  1078. ctx->dev = NULL;
  1079. break;
  1080. }
  1081. ret = c4iw_register_device(ctx->dev);
  1082. if (ret) {
  1083. printk(KERN_ERR MOD
  1084. "%s: RDMA registration failed: %d\n",
  1085. pci_name(ctx->lldi.pdev), ret);
  1086. c4iw_dealloc(ctx);
  1087. }
  1088. }
  1089. break;
  1090. case CXGB4_STATE_DOWN:
  1091. printk(KERN_INFO MOD "%s: Down\n",
  1092. pci_name(ctx->lldi.pdev));
  1093. if (ctx->dev)
  1094. c4iw_remove(ctx);
  1095. break;
  1096. case CXGB4_STATE_START_RECOVERY:
  1097. printk(KERN_INFO MOD "%s: Fatal Error\n",
  1098. pci_name(ctx->lldi.pdev));
  1099. if (ctx->dev) {
  1100. struct ib_event event;
  1101. ctx->dev->rdev.flags |= T4_FATAL_ERROR;
  1102. memset(&event, 0, sizeof event);
  1103. event.event = IB_EVENT_DEVICE_FATAL;
  1104. event.device = &ctx->dev->ibdev;
  1105. ib_dispatch_event(&event);
  1106. c4iw_remove(ctx);
  1107. }
  1108. break;
  1109. case CXGB4_STATE_DETACH:
  1110. printk(KERN_INFO MOD "%s: Detach\n",
  1111. pci_name(ctx->lldi.pdev));
  1112. if (ctx->dev)
  1113. c4iw_remove(ctx);
  1114. break;
  1115. }
  1116. return 0;
  1117. }
  1118. static int disable_qp_db(int id, void *p, void *data)
  1119. {
  1120. struct c4iw_qp *qp = p;
  1121. t4_disable_wq_db(&qp->wq);
  1122. return 0;
  1123. }
  1124. static void stop_queues(struct uld_ctx *ctx)
  1125. {
  1126. unsigned long flags;
  1127. spin_lock_irqsave(&ctx->dev->lock, flags);
  1128. ctx->dev->rdev.stats.db_state_transitions++;
  1129. ctx->dev->db_state = STOPPED;
  1130. if (ctx->dev->rdev.flags & T4_STATUS_PAGE_DISABLED)
  1131. idr_for_each(&ctx->dev->qpidr, disable_qp_db, NULL);
  1132. else
  1133. ctx->dev->rdev.status_page->db_off = 1;
  1134. spin_unlock_irqrestore(&ctx->dev->lock, flags);
  1135. }
  1136. static int enable_qp_db(int id, void *p, void *data)
  1137. {
  1138. struct c4iw_qp *qp = p;
  1139. t4_enable_wq_db(&qp->wq);
  1140. return 0;
  1141. }
  1142. static void resume_rc_qp(struct c4iw_qp *qp)
  1143. {
  1144. spin_lock(&qp->lock);
  1145. t4_ring_sq_db(&qp->wq, qp->wq.sq.wq_pidx_inc, NULL);
  1146. qp->wq.sq.wq_pidx_inc = 0;
  1147. t4_ring_rq_db(&qp->wq, qp->wq.rq.wq_pidx_inc, NULL);
  1148. qp->wq.rq.wq_pidx_inc = 0;
  1149. spin_unlock(&qp->lock);
  1150. }
  1151. static void resume_a_chunk(struct uld_ctx *ctx)
  1152. {
  1153. int i;
  1154. struct c4iw_qp *qp;
  1155. for (i = 0; i < DB_FC_RESUME_SIZE; i++) {
  1156. qp = list_first_entry(&ctx->dev->db_fc_list, struct c4iw_qp,
  1157. db_fc_entry);
  1158. list_del_init(&qp->db_fc_entry);
  1159. resume_rc_qp(qp);
  1160. if (list_empty(&ctx->dev->db_fc_list))
  1161. break;
  1162. }
  1163. }
  1164. static void resume_queues(struct uld_ctx *ctx)
  1165. {
  1166. spin_lock_irq(&ctx->dev->lock);
  1167. if (ctx->dev->db_state != STOPPED)
  1168. goto out;
  1169. ctx->dev->db_state = FLOW_CONTROL;
  1170. while (1) {
  1171. if (list_empty(&ctx->dev->db_fc_list)) {
  1172. WARN_ON(ctx->dev->db_state != FLOW_CONTROL);
  1173. ctx->dev->db_state = NORMAL;
  1174. ctx->dev->rdev.stats.db_state_transitions++;
  1175. if (ctx->dev->rdev.flags & T4_STATUS_PAGE_DISABLED) {
  1176. idr_for_each(&ctx->dev->qpidr, enable_qp_db,
  1177. NULL);
  1178. } else {
  1179. ctx->dev->rdev.status_page->db_off = 0;
  1180. }
  1181. break;
  1182. } else {
  1183. if (cxgb4_dbfifo_count(ctx->dev->rdev.lldi.ports[0], 1)
  1184. < (ctx->dev->rdev.lldi.dbfifo_int_thresh <<
  1185. DB_FC_DRAIN_THRESH)) {
  1186. resume_a_chunk(ctx);
  1187. }
  1188. if (!list_empty(&ctx->dev->db_fc_list)) {
  1189. spin_unlock_irq(&ctx->dev->lock);
  1190. if (DB_FC_RESUME_DELAY) {
  1191. set_current_state(TASK_UNINTERRUPTIBLE);
  1192. schedule_timeout(DB_FC_RESUME_DELAY);
  1193. }
  1194. spin_lock_irq(&ctx->dev->lock);
  1195. if (ctx->dev->db_state != FLOW_CONTROL)
  1196. break;
  1197. }
  1198. }
  1199. }
  1200. out:
  1201. if (ctx->dev->db_state != NORMAL)
  1202. ctx->dev->rdev.stats.db_fc_interruptions++;
  1203. spin_unlock_irq(&ctx->dev->lock);
  1204. }
  1205. struct qp_list {
  1206. unsigned idx;
  1207. struct c4iw_qp **qps;
  1208. };
  1209. static int add_and_ref_qp(int id, void *p, void *data)
  1210. {
  1211. struct qp_list *qp_listp = data;
  1212. struct c4iw_qp *qp = p;
  1213. c4iw_qp_add_ref(&qp->ibqp);
  1214. qp_listp->qps[qp_listp->idx++] = qp;
  1215. return 0;
  1216. }
  1217. static int count_qps(int id, void *p, void *data)
  1218. {
  1219. unsigned *countp = data;
  1220. (*countp)++;
  1221. return 0;
  1222. }
  1223. static void deref_qps(struct qp_list *qp_list)
  1224. {
  1225. int idx;
  1226. for (idx = 0; idx < qp_list->idx; idx++)
  1227. c4iw_qp_rem_ref(&qp_list->qps[idx]->ibqp);
  1228. }
  1229. static void recover_lost_dbs(struct uld_ctx *ctx, struct qp_list *qp_list)
  1230. {
  1231. int idx;
  1232. int ret;
  1233. for (idx = 0; idx < qp_list->idx; idx++) {
  1234. struct c4iw_qp *qp = qp_list->qps[idx];
  1235. spin_lock_irq(&qp->rhp->lock);
  1236. spin_lock(&qp->lock);
  1237. ret = cxgb4_sync_txq_pidx(qp->rhp->rdev.lldi.ports[0],
  1238. qp->wq.sq.qid,
  1239. t4_sq_host_wq_pidx(&qp->wq),
  1240. t4_sq_wq_size(&qp->wq));
  1241. if (ret) {
  1242. pr_err(MOD "%s: Fatal error - "
  1243. "DB overflow recovery failed - "
  1244. "error syncing SQ qid %u\n",
  1245. pci_name(ctx->lldi.pdev), qp->wq.sq.qid);
  1246. spin_unlock(&qp->lock);
  1247. spin_unlock_irq(&qp->rhp->lock);
  1248. return;
  1249. }
  1250. qp->wq.sq.wq_pidx_inc = 0;
  1251. ret = cxgb4_sync_txq_pidx(qp->rhp->rdev.lldi.ports[0],
  1252. qp->wq.rq.qid,
  1253. t4_rq_host_wq_pidx(&qp->wq),
  1254. t4_rq_wq_size(&qp->wq));
  1255. if (ret) {
  1256. pr_err(MOD "%s: Fatal error - "
  1257. "DB overflow recovery failed - "
  1258. "error syncing RQ qid %u\n",
  1259. pci_name(ctx->lldi.pdev), qp->wq.rq.qid);
  1260. spin_unlock(&qp->lock);
  1261. spin_unlock_irq(&qp->rhp->lock);
  1262. return;
  1263. }
  1264. qp->wq.rq.wq_pidx_inc = 0;
  1265. spin_unlock(&qp->lock);
  1266. spin_unlock_irq(&qp->rhp->lock);
  1267. /* Wait for the dbfifo to drain */
  1268. while (cxgb4_dbfifo_count(qp->rhp->rdev.lldi.ports[0], 1) > 0) {
  1269. set_current_state(TASK_UNINTERRUPTIBLE);
  1270. schedule_timeout(usecs_to_jiffies(10));
  1271. }
  1272. }
  1273. }
  1274. static void recover_queues(struct uld_ctx *ctx)
  1275. {
  1276. int count = 0;
  1277. struct qp_list qp_list;
  1278. int ret;
  1279. /* slow everybody down */
  1280. set_current_state(TASK_UNINTERRUPTIBLE);
  1281. schedule_timeout(usecs_to_jiffies(1000));
  1282. /* flush the SGE contexts */
  1283. ret = cxgb4_flush_eq_cache(ctx->dev->rdev.lldi.ports[0]);
  1284. if (ret) {
  1285. printk(KERN_ERR MOD "%s: Fatal error - DB overflow recovery failed\n",
  1286. pci_name(ctx->lldi.pdev));
  1287. return;
  1288. }
  1289. /* Count active queues so we can build a list of queues to recover */
  1290. spin_lock_irq(&ctx->dev->lock);
  1291. WARN_ON(ctx->dev->db_state != STOPPED);
  1292. ctx->dev->db_state = RECOVERY;
  1293. idr_for_each(&ctx->dev->qpidr, count_qps, &count);
  1294. qp_list.qps = kzalloc(count * sizeof *qp_list.qps, GFP_ATOMIC);
  1295. if (!qp_list.qps) {
  1296. spin_unlock_irq(&ctx->dev->lock);
  1297. return;
  1298. }
  1299. qp_list.idx = 0;
  1300. /* add and ref each qp so it doesn't get freed */
  1301. idr_for_each(&ctx->dev->qpidr, add_and_ref_qp, &qp_list);
  1302. spin_unlock_irq(&ctx->dev->lock);
  1303. /* now traverse the list in a safe context to recover the db state*/
  1304. recover_lost_dbs(ctx, &qp_list);
  1305. /* we're almost done! deref the qps and clean up */
  1306. deref_qps(&qp_list);
  1307. kfree(qp_list.qps);
  1308. spin_lock_irq(&ctx->dev->lock);
  1309. WARN_ON(ctx->dev->db_state != RECOVERY);
  1310. ctx->dev->db_state = STOPPED;
  1311. spin_unlock_irq(&ctx->dev->lock);
  1312. }
  1313. static int c4iw_uld_control(void *handle, enum cxgb4_control control, ...)
  1314. {
  1315. struct uld_ctx *ctx = handle;
  1316. switch (control) {
  1317. case CXGB4_CONTROL_DB_FULL:
  1318. stop_queues(ctx);
  1319. ctx->dev->rdev.stats.db_full++;
  1320. break;
  1321. case CXGB4_CONTROL_DB_EMPTY:
  1322. resume_queues(ctx);
  1323. mutex_lock(&ctx->dev->rdev.stats.lock);
  1324. ctx->dev->rdev.stats.db_empty++;
  1325. mutex_unlock(&ctx->dev->rdev.stats.lock);
  1326. break;
  1327. case CXGB4_CONTROL_DB_DROP:
  1328. recover_queues(ctx);
  1329. mutex_lock(&ctx->dev->rdev.stats.lock);
  1330. ctx->dev->rdev.stats.db_drop++;
  1331. mutex_unlock(&ctx->dev->rdev.stats.lock);
  1332. break;
  1333. default:
  1334. printk(KERN_WARNING MOD "%s: unknown control cmd %u\n",
  1335. pci_name(ctx->lldi.pdev), control);
  1336. break;
  1337. }
  1338. return 0;
  1339. }
  1340. static struct cxgb4_uld_info c4iw_uld_info = {
  1341. .name = DRV_NAME,
  1342. .nrxq = MAX_ULD_QSETS,
  1343. .ntxq = MAX_ULD_QSETS,
  1344. .rxq_size = 511,
  1345. .ciq = true,
  1346. .lro = false,
  1347. .add = c4iw_uld_add,
  1348. .rx_handler = c4iw_uld_rx_handler,
  1349. .state_change = c4iw_uld_state_change,
  1350. .control = c4iw_uld_control,
  1351. };
  1352. static int __init c4iw_init_module(void)
  1353. {
  1354. int err;
  1355. err = c4iw_cm_init();
  1356. if (err)
  1357. return err;
  1358. c4iw_debugfs_root = debugfs_create_dir(DRV_NAME, NULL);
  1359. if (!c4iw_debugfs_root)
  1360. printk(KERN_WARNING MOD
  1361. "could not create debugfs entry, continuing\n");
  1362. cxgb4_register_uld(CXGB4_ULD_RDMA, &c4iw_uld_info);
  1363. return 0;
  1364. }
  1365. static void __exit c4iw_exit_module(void)
  1366. {
  1367. struct uld_ctx *ctx, *tmp;
  1368. mutex_lock(&dev_mutex);
  1369. list_for_each_entry_safe(ctx, tmp, &uld_ctx_list, entry) {
  1370. if (ctx->dev)
  1371. c4iw_remove(ctx);
  1372. kfree(ctx);
  1373. }
  1374. mutex_unlock(&dev_mutex);
  1375. cxgb4_unregister_uld(CXGB4_ULD_RDMA);
  1376. c4iw_cm_term();
  1377. debugfs_remove_recursive(c4iw_debugfs_root);
  1378. }
  1379. module_init(c4iw_init_module);
  1380. module_exit(c4iw_exit_module);