qib_diag.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /*
  2. * Copyright (c) 2012 Intel Corporation. All rights reserved.
  3. * Copyright (c) 2006 - 2012 QLogic Corporation. All rights reserved.
  4. * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. /*
  35. * This file contains support for diagnostic functions. It is accessed by
  36. * opening the qib_diag device, normally minor number 129. Diagnostic use
  37. * of the QLogic_IB chip may render the chip or board unusable until the
  38. * driver is unloaded, or in some cases, until the system is rebooted.
  39. *
  40. * Accesses to the chip through this interface are not similar to going
  41. * through the /sys/bus/pci resource mmap interface.
  42. */
  43. #include <linux/io.h>
  44. #include <linux/pci.h>
  45. #include <linux/poll.h>
  46. #include <linux/vmalloc.h>
  47. #include <linux/export.h>
  48. #include <linux/fs.h>
  49. #include <linux/uaccess.h>
  50. #include "qib.h"
  51. #include "qib_common.h"
  52. #undef pr_fmt
  53. #define pr_fmt(fmt) QIB_DRV_NAME ": " fmt
  54. /*
  55. * Each client that opens the diag device must read then write
  56. * offset 0, to prevent lossage from random cat or od. diag_state
  57. * sequences this "handshake".
  58. */
  59. enum diag_state { UNUSED = 0, OPENED, INIT, READY };
  60. /* State for an individual client. PID so children cannot abuse handshake */
  61. static struct qib_diag_client {
  62. struct qib_diag_client *next;
  63. struct qib_devdata *dd;
  64. pid_t pid;
  65. enum diag_state state;
  66. } *client_pool;
  67. /*
  68. * Get a client struct. Recycled if possible, else kmalloc.
  69. * Must be called with qib_mutex held
  70. */
  71. static struct qib_diag_client *get_client(struct qib_devdata *dd)
  72. {
  73. struct qib_diag_client *dc;
  74. dc = client_pool;
  75. if (dc)
  76. /* got from pool remove it and use */
  77. client_pool = dc->next;
  78. else
  79. /* None in pool, alloc and init */
  80. dc = kmalloc(sizeof(*dc), GFP_KERNEL);
  81. if (dc) {
  82. dc->next = NULL;
  83. dc->dd = dd;
  84. dc->pid = current->pid;
  85. dc->state = OPENED;
  86. }
  87. return dc;
  88. }
  89. /*
  90. * Return to pool. Must be called with qib_mutex held
  91. */
  92. static void return_client(struct qib_diag_client *dc)
  93. {
  94. struct qib_devdata *dd = dc->dd;
  95. struct qib_diag_client *tdc, *rdc;
  96. rdc = NULL;
  97. if (dc == dd->diag_client) {
  98. dd->diag_client = dc->next;
  99. rdc = dc;
  100. } else {
  101. tdc = dc->dd->diag_client;
  102. while (tdc) {
  103. if (dc == tdc->next) {
  104. tdc->next = dc->next;
  105. rdc = dc;
  106. break;
  107. }
  108. tdc = tdc->next;
  109. }
  110. }
  111. if (rdc) {
  112. rdc->state = UNUSED;
  113. rdc->dd = NULL;
  114. rdc->pid = 0;
  115. rdc->next = client_pool;
  116. client_pool = rdc;
  117. }
  118. }
  119. static int qib_diag_open(struct inode *in, struct file *fp);
  120. static int qib_diag_release(struct inode *in, struct file *fp);
  121. static ssize_t qib_diag_read(struct file *fp, char __user *data,
  122. size_t count, loff_t *off);
  123. static ssize_t qib_diag_write(struct file *fp, const char __user *data,
  124. size_t count, loff_t *off);
  125. static const struct file_operations diag_file_ops = {
  126. .owner = THIS_MODULE,
  127. .write = qib_diag_write,
  128. .read = qib_diag_read,
  129. .open = qib_diag_open,
  130. .release = qib_diag_release,
  131. .llseek = default_llseek,
  132. };
  133. static atomic_t diagpkt_count = ATOMIC_INIT(0);
  134. static struct cdev *diagpkt_cdev;
  135. static struct device *diagpkt_device;
  136. static ssize_t qib_diagpkt_write(struct file *fp, const char __user *data,
  137. size_t count, loff_t *off);
  138. static const struct file_operations diagpkt_file_ops = {
  139. .owner = THIS_MODULE,
  140. .write = qib_diagpkt_write,
  141. .llseek = noop_llseek,
  142. };
  143. int qib_diag_add(struct qib_devdata *dd)
  144. {
  145. char name[16];
  146. int ret = 0;
  147. if (atomic_inc_return(&diagpkt_count) == 1) {
  148. ret = qib_cdev_init(QIB_DIAGPKT_MINOR, "ipath_diagpkt",
  149. &diagpkt_file_ops, &diagpkt_cdev,
  150. &diagpkt_device);
  151. if (ret)
  152. goto done;
  153. }
  154. snprintf(name, sizeof(name), "ipath_diag%d", dd->unit);
  155. ret = qib_cdev_init(QIB_DIAG_MINOR_BASE + dd->unit, name,
  156. &diag_file_ops, &dd->diag_cdev,
  157. &dd->diag_device);
  158. done:
  159. return ret;
  160. }
  161. static void qib_unregister_observers(struct qib_devdata *dd);
  162. void qib_diag_remove(struct qib_devdata *dd)
  163. {
  164. struct qib_diag_client *dc;
  165. if (atomic_dec_and_test(&diagpkt_count))
  166. qib_cdev_cleanup(&diagpkt_cdev, &diagpkt_device);
  167. qib_cdev_cleanup(&dd->diag_cdev, &dd->diag_device);
  168. /*
  169. * Return all diag_clients of this device. There should be none,
  170. * as we are "guaranteed" that no clients are still open
  171. */
  172. while (dd->diag_client)
  173. return_client(dd->diag_client);
  174. /* Now clean up all unused client structs */
  175. while (client_pool) {
  176. dc = client_pool;
  177. client_pool = dc->next;
  178. kfree(dc);
  179. }
  180. /* Clean up observer list */
  181. qib_unregister_observers(dd);
  182. }
  183. /* qib_remap_ioaddr32 - remap an offset into chip address space to __iomem *
  184. *
  185. * @dd: the qlogic_ib device
  186. * @offs: the offset in chip-space
  187. * @cntp: Pointer to max (byte) count for transfer starting at offset
  188. * This returns a u32 __iomem * so it can be used for both 64 and 32-bit
  189. * mapping. It is needed because with the use of PAT for control of
  190. * write-combining, the logically contiguous address-space of the chip
  191. * may be split into virtually non-contiguous spaces, with different
  192. * attributes, which are them mapped to contiguous physical space
  193. * based from the first BAR.
  194. *
  195. * The code below makes the same assumptions as were made in
  196. * init_chip_wc_pat() (qib_init.c), copied here:
  197. * Assumes chip address space looks like:
  198. * - kregs + sregs + cregs + uregs (in any order)
  199. * - piobufs (2K and 4K bufs in either order)
  200. * or:
  201. * - kregs + sregs + cregs (in any order)
  202. * - piobufs (2K and 4K bufs in either order)
  203. * - uregs
  204. *
  205. * If cntp is non-NULL, returns how many bytes from offset can be accessed
  206. * Returns 0 if the offset is not mapped.
  207. */
  208. static u32 __iomem *qib_remap_ioaddr32(struct qib_devdata *dd, u32 offset,
  209. u32 *cntp)
  210. {
  211. u32 kreglen;
  212. u32 snd_bottom, snd_lim = 0;
  213. u32 __iomem *krb32 = (u32 __iomem *)dd->kregbase;
  214. u32 __iomem *map = NULL;
  215. u32 cnt = 0;
  216. u32 tot4k, offs4k;
  217. /* First, simplest case, offset is within the first map. */
  218. kreglen = (dd->kregend - dd->kregbase) * sizeof(u64);
  219. if (offset < kreglen) {
  220. map = krb32 + (offset / sizeof(u32));
  221. cnt = kreglen - offset;
  222. goto mapped;
  223. }
  224. /*
  225. * Next check for user regs, the next most common case,
  226. * and a cheap check because if they are not in the first map
  227. * they are last in chip.
  228. */
  229. if (dd->userbase) {
  230. /* If user regs mapped, they are after send, so set limit. */
  231. u32 ulim = (dd->cfgctxts * dd->ureg_align) + dd->uregbase;
  232. if (!dd->piovl15base)
  233. snd_lim = dd->uregbase;
  234. krb32 = (u32 __iomem *)dd->userbase;
  235. if (offset >= dd->uregbase && offset < ulim) {
  236. map = krb32 + (offset - dd->uregbase) / sizeof(u32);
  237. cnt = ulim - offset;
  238. goto mapped;
  239. }
  240. }
  241. /*
  242. * Lastly, check for offset within Send Buffers.
  243. * This is gnarly because struct devdata is deliberately vague
  244. * about things like 7322 VL15 buffers, and we are not in
  245. * chip-specific code here, so should not make many assumptions.
  246. * The one we _do_ make is that the only chip that has more sndbufs
  247. * than we admit is the 7322, and it has userregs above that, so
  248. * we know the snd_lim.
  249. */
  250. /* Assume 2K buffers are first. */
  251. snd_bottom = dd->pio2k_bufbase;
  252. if (snd_lim == 0) {
  253. u32 tot2k = dd->piobcnt2k * ALIGN(dd->piosize2k, dd->palign);
  254. snd_lim = snd_bottom + tot2k;
  255. }
  256. /* If 4k buffers exist, account for them by bumping
  257. * appropriate limit.
  258. */
  259. tot4k = dd->piobcnt4k * dd->align4k;
  260. offs4k = dd->piobufbase >> 32;
  261. if (dd->piobcnt4k) {
  262. if (snd_bottom > offs4k)
  263. snd_bottom = offs4k;
  264. else {
  265. /* 4k above 2k. Bump snd_lim, if needed*/
  266. if (!dd->userbase || dd->piovl15base)
  267. snd_lim = offs4k + tot4k;
  268. }
  269. }
  270. /*
  271. * Judgement call: can we ignore the space between SendBuffs and
  272. * UserRegs, where we would like to see vl15 buffs, but not more?
  273. */
  274. if (offset >= snd_bottom && offset < snd_lim) {
  275. offset -= snd_bottom;
  276. map = (u32 __iomem *)dd->piobase + (offset / sizeof(u32));
  277. cnt = snd_lim - offset;
  278. }
  279. if (!map && offs4k && dd->piovl15base) {
  280. snd_lim = offs4k + tot4k + 2 * dd->align4k;
  281. if (offset >= (offs4k + tot4k) && offset < snd_lim) {
  282. map = (u32 __iomem *)dd->piovl15base +
  283. ((offset - (offs4k + tot4k)) / sizeof(u32));
  284. cnt = snd_lim - offset;
  285. }
  286. }
  287. mapped:
  288. if (cntp)
  289. *cntp = cnt;
  290. return map;
  291. }
  292. /*
  293. * qib_read_umem64 - read a 64-bit quantity from the chip into user space
  294. * @dd: the qlogic_ib device
  295. * @uaddr: the location to store the data in user memory
  296. * @regoffs: the offset from BAR0 (_NOT_ full pointer, anymore)
  297. * @count: number of bytes to copy (multiple of 32 bits)
  298. *
  299. * This function also localizes all chip memory accesses.
  300. * The copy should be written such that we read full cacheline packets
  301. * from the chip. This is usually used for a single qword
  302. *
  303. * NOTE: This assumes the chip address is 64-bit aligned.
  304. */
  305. static int qib_read_umem64(struct qib_devdata *dd, void __user *uaddr,
  306. u32 regoffs, size_t count)
  307. {
  308. const u64 __iomem *reg_addr;
  309. const u64 __iomem *reg_end;
  310. u32 limit;
  311. int ret;
  312. reg_addr = (const u64 __iomem *)qib_remap_ioaddr32(dd, regoffs, &limit);
  313. if (reg_addr == NULL || limit == 0 || !(dd->flags & QIB_PRESENT)) {
  314. ret = -EINVAL;
  315. goto bail;
  316. }
  317. if (count >= limit)
  318. count = limit;
  319. reg_end = reg_addr + (count / sizeof(u64));
  320. /* not very efficient, but it works for now */
  321. while (reg_addr < reg_end) {
  322. u64 data = readq(reg_addr);
  323. if (copy_to_user(uaddr, &data, sizeof(u64))) {
  324. ret = -EFAULT;
  325. goto bail;
  326. }
  327. reg_addr++;
  328. uaddr += sizeof(u64);
  329. }
  330. ret = 0;
  331. bail:
  332. return ret;
  333. }
  334. /*
  335. * qib_write_umem64 - write a 64-bit quantity to the chip from user space
  336. * @dd: the qlogic_ib device
  337. * @regoffs: the offset from BAR0 (_NOT_ full pointer, anymore)
  338. * @uaddr: the source of the data in user memory
  339. * @count: the number of bytes to copy (multiple of 32 bits)
  340. *
  341. * This is usually used for a single qword
  342. * NOTE: This assumes the chip address is 64-bit aligned.
  343. */
  344. static int qib_write_umem64(struct qib_devdata *dd, u32 regoffs,
  345. const void __user *uaddr, size_t count)
  346. {
  347. u64 __iomem *reg_addr;
  348. const u64 __iomem *reg_end;
  349. u32 limit;
  350. int ret;
  351. reg_addr = (u64 __iomem *)qib_remap_ioaddr32(dd, regoffs, &limit);
  352. if (reg_addr == NULL || limit == 0 || !(dd->flags & QIB_PRESENT)) {
  353. ret = -EINVAL;
  354. goto bail;
  355. }
  356. if (count >= limit)
  357. count = limit;
  358. reg_end = reg_addr + (count / sizeof(u64));
  359. /* not very efficient, but it works for now */
  360. while (reg_addr < reg_end) {
  361. u64 data;
  362. if (copy_from_user(&data, uaddr, sizeof(data))) {
  363. ret = -EFAULT;
  364. goto bail;
  365. }
  366. writeq(data, reg_addr);
  367. reg_addr++;
  368. uaddr += sizeof(u64);
  369. }
  370. ret = 0;
  371. bail:
  372. return ret;
  373. }
  374. /*
  375. * qib_read_umem32 - read a 32-bit quantity from the chip into user space
  376. * @dd: the qlogic_ib device
  377. * @uaddr: the location to store the data in user memory
  378. * @regoffs: the offset from BAR0 (_NOT_ full pointer, anymore)
  379. * @count: number of bytes to copy
  380. *
  381. * read 32 bit values, not 64 bit; for memories that only
  382. * support 32 bit reads; usually a single dword.
  383. */
  384. static int qib_read_umem32(struct qib_devdata *dd, void __user *uaddr,
  385. u32 regoffs, size_t count)
  386. {
  387. const u32 __iomem *reg_addr;
  388. const u32 __iomem *reg_end;
  389. u32 limit;
  390. int ret;
  391. reg_addr = qib_remap_ioaddr32(dd, regoffs, &limit);
  392. if (reg_addr == NULL || limit == 0 || !(dd->flags & QIB_PRESENT)) {
  393. ret = -EINVAL;
  394. goto bail;
  395. }
  396. if (count >= limit)
  397. count = limit;
  398. reg_end = reg_addr + (count / sizeof(u32));
  399. /* not very efficient, but it works for now */
  400. while (reg_addr < reg_end) {
  401. u32 data = readl(reg_addr);
  402. if (copy_to_user(uaddr, &data, sizeof(data))) {
  403. ret = -EFAULT;
  404. goto bail;
  405. }
  406. reg_addr++;
  407. uaddr += sizeof(u32);
  408. }
  409. ret = 0;
  410. bail:
  411. return ret;
  412. }
  413. /*
  414. * qib_write_umem32 - write a 32-bit quantity to the chip from user space
  415. * @dd: the qlogic_ib device
  416. * @regoffs: the offset from BAR0 (_NOT_ full pointer, anymore)
  417. * @uaddr: the source of the data in user memory
  418. * @count: number of bytes to copy
  419. *
  420. * write 32 bit values, not 64 bit; for memories that only
  421. * support 32 bit write; usually a single dword.
  422. */
  423. static int qib_write_umem32(struct qib_devdata *dd, u32 regoffs,
  424. const void __user *uaddr, size_t count)
  425. {
  426. u32 __iomem *reg_addr;
  427. const u32 __iomem *reg_end;
  428. u32 limit;
  429. int ret;
  430. reg_addr = qib_remap_ioaddr32(dd, regoffs, &limit);
  431. if (reg_addr == NULL || limit == 0 || !(dd->flags & QIB_PRESENT)) {
  432. ret = -EINVAL;
  433. goto bail;
  434. }
  435. if (count >= limit)
  436. count = limit;
  437. reg_end = reg_addr + (count / sizeof(u32));
  438. while (reg_addr < reg_end) {
  439. u32 data;
  440. if (copy_from_user(&data, uaddr, sizeof(data))) {
  441. ret = -EFAULT;
  442. goto bail;
  443. }
  444. writel(data, reg_addr);
  445. reg_addr++;
  446. uaddr += sizeof(u32);
  447. }
  448. ret = 0;
  449. bail:
  450. return ret;
  451. }
  452. static int qib_diag_open(struct inode *in, struct file *fp)
  453. {
  454. int unit = iminor(in) - QIB_DIAG_MINOR_BASE;
  455. struct qib_devdata *dd;
  456. struct qib_diag_client *dc;
  457. int ret;
  458. mutex_lock(&qib_mutex);
  459. dd = qib_lookup(unit);
  460. if (dd == NULL || !(dd->flags & QIB_PRESENT) ||
  461. !dd->kregbase) {
  462. ret = -ENODEV;
  463. goto bail;
  464. }
  465. dc = get_client(dd);
  466. if (!dc) {
  467. ret = -ENOMEM;
  468. goto bail;
  469. }
  470. dc->next = dd->diag_client;
  471. dd->diag_client = dc;
  472. fp->private_data = dc;
  473. ret = 0;
  474. bail:
  475. mutex_unlock(&qib_mutex);
  476. return ret;
  477. }
  478. /**
  479. * qib_diagpkt_write - write an IB packet
  480. * @fp: the diag data device file pointer
  481. * @data: qib_diag_pkt structure saying where to get the packet
  482. * @count: size of data to write
  483. * @off: unused by this code
  484. */
  485. static ssize_t qib_diagpkt_write(struct file *fp,
  486. const char __user *data,
  487. size_t count, loff_t *off)
  488. {
  489. u32 __iomem *piobuf;
  490. u32 plen, pbufn, maxlen_reserve;
  491. struct qib_diag_xpkt dp;
  492. u32 *tmpbuf = NULL;
  493. struct qib_devdata *dd;
  494. struct qib_pportdata *ppd;
  495. ssize_t ret = 0;
  496. if (count != sizeof(dp)) {
  497. ret = -EINVAL;
  498. goto bail;
  499. }
  500. if (copy_from_user(&dp, data, sizeof(dp))) {
  501. ret = -EFAULT;
  502. goto bail;
  503. }
  504. dd = qib_lookup(dp.unit);
  505. if (!dd || !(dd->flags & QIB_PRESENT) || !dd->kregbase) {
  506. ret = -ENODEV;
  507. goto bail;
  508. }
  509. if (!(dd->flags & QIB_INITTED)) {
  510. /* no hardware, freeze, etc. */
  511. ret = -ENODEV;
  512. goto bail;
  513. }
  514. if (dp.version != _DIAG_XPKT_VERS) {
  515. qib_dev_err(dd, "Invalid version %u for diagpkt_write\n",
  516. dp.version);
  517. ret = -EINVAL;
  518. goto bail;
  519. }
  520. /* send count must be an exact number of dwords */
  521. if (dp.len & 3) {
  522. ret = -EINVAL;
  523. goto bail;
  524. }
  525. if (!dp.port || dp.port > dd->num_pports) {
  526. ret = -EINVAL;
  527. goto bail;
  528. }
  529. ppd = &dd->pport[dp.port - 1];
  530. /*
  531. * need total length before first word written, plus 2 Dwords. One Dword
  532. * is for padding so we get the full user data when not aligned on
  533. * a word boundary. The other Dword is to make sure we have room for the
  534. * ICRC which gets tacked on later.
  535. */
  536. maxlen_reserve = 2 * sizeof(u32);
  537. if (dp.len > ppd->ibmaxlen - maxlen_reserve) {
  538. ret = -EINVAL;
  539. goto bail;
  540. }
  541. plen = sizeof(u32) + dp.len;
  542. tmpbuf = vmalloc(plen);
  543. if (!tmpbuf) {
  544. qib_devinfo(dd->pcidev,
  545. "Unable to allocate tmp buffer, failing\n");
  546. ret = -ENOMEM;
  547. goto bail;
  548. }
  549. if (copy_from_user(tmpbuf,
  550. (const void __user *) (unsigned long) dp.data,
  551. dp.len)) {
  552. ret = -EFAULT;
  553. goto bail;
  554. }
  555. plen >>= 2; /* in dwords */
  556. if (dp.pbc_wd == 0)
  557. dp.pbc_wd = plen;
  558. piobuf = dd->f_getsendbuf(ppd, dp.pbc_wd, &pbufn);
  559. if (!piobuf) {
  560. ret = -EBUSY;
  561. goto bail;
  562. }
  563. /* disarm it just to be extra sure */
  564. dd->f_sendctrl(dd->pport, QIB_SENDCTRL_DISARM_BUF(pbufn));
  565. /* disable header check on pbufn for this packet */
  566. dd->f_txchk_change(dd, pbufn, 1, TXCHK_CHG_TYPE_DIS1, NULL);
  567. writeq(dp.pbc_wd, piobuf);
  568. /*
  569. * Copy all but the trigger word, then flush, so it's written
  570. * to chip before trigger word, then write trigger word, then
  571. * flush again, so packet is sent.
  572. */
  573. if (dd->flags & QIB_PIO_FLUSH_WC) {
  574. qib_flush_wc();
  575. qib_pio_copy(piobuf + 2, tmpbuf, plen - 1);
  576. qib_flush_wc();
  577. __raw_writel(tmpbuf[plen - 1], piobuf + plen + 1);
  578. } else
  579. qib_pio_copy(piobuf + 2, tmpbuf, plen);
  580. if (dd->flags & QIB_USE_SPCL_TRIG) {
  581. u32 spcl_off = (pbufn >= dd->piobcnt2k) ? 2047 : 1023;
  582. qib_flush_wc();
  583. __raw_writel(0xaebecede, piobuf + spcl_off);
  584. }
  585. /*
  586. * Ensure buffer is written to the chip, then re-enable
  587. * header checks (if supported by chip). The txchk
  588. * code will ensure seen by chip before returning.
  589. */
  590. qib_flush_wc();
  591. qib_sendbuf_done(dd, pbufn);
  592. dd->f_txchk_change(dd, pbufn, 1, TXCHK_CHG_TYPE_ENAB1, NULL);
  593. ret = sizeof(dp);
  594. bail:
  595. vfree(tmpbuf);
  596. return ret;
  597. }
  598. static int qib_diag_release(struct inode *in, struct file *fp)
  599. {
  600. mutex_lock(&qib_mutex);
  601. return_client(fp->private_data);
  602. fp->private_data = NULL;
  603. mutex_unlock(&qib_mutex);
  604. return 0;
  605. }
  606. /*
  607. * Chip-specific code calls to register its interest in
  608. * a specific range.
  609. */
  610. struct diag_observer_list_elt {
  611. struct diag_observer_list_elt *next;
  612. const struct diag_observer *op;
  613. };
  614. int qib_register_observer(struct qib_devdata *dd,
  615. const struct diag_observer *op)
  616. {
  617. struct diag_observer_list_elt *olp;
  618. unsigned long flags;
  619. if (!dd || !op)
  620. return -EINVAL;
  621. olp = vmalloc(sizeof(*olp));
  622. if (!olp) {
  623. pr_err("vmalloc for observer failed\n");
  624. return -ENOMEM;
  625. }
  626. spin_lock_irqsave(&dd->qib_diag_trans_lock, flags);
  627. olp->op = op;
  628. olp->next = dd->diag_observer_list;
  629. dd->diag_observer_list = olp;
  630. spin_unlock_irqrestore(&dd->qib_diag_trans_lock, flags);
  631. return 0;
  632. }
  633. /* Remove all registered observers when device is closed */
  634. static void qib_unregister_observers(struct qib_devdata *dd)
  635. {
  636. struct diag_observer_list_elt *olp;
  637. unsigned long flags;
  638. spin_lock_irqsave(&dd->qib_diag_trans_lock, flags);
  639. olp = dd->diag_observer_list;
  640. while (olp) {
  641. /* Pop one observer, let go of lock */
  642. dd->diag_observer_list = olp->next;
  643. spin_unlock_irqrestore(&dd->qib_diag_trans_lock, flags);
  644. vfree(olp);
  645. /* try again. */
  646. spin_lock_irqsave(&dd->qib_diag_trans_lock, flags);
  647. olp = dd->diag_observer_list;
  648. }
  649. spin_unlock_irqrestore(&dd->qib_diag_trans_lock, flags);
  650. }
  651. /*
  652. * Find the observer, if any, for the specified address. Initial implementation
  653. * is simple stack of observers. This must be called with diag transaction
  654. * lock held.
  655. */
  656. static const struct diag_observer *diag_get_observer(struct qib_devdata *dd,
  657. u32 addr)
  658. {
  659. struct diag_observer_list_elt *olp;
  660. const struct diag_observer *op = NULL;
  661. olp = dd->diag_observer_list;
  662. while (olp) {
  663. op = olp->op;
  664. if (addr >= op->bottom && addr <= op->top)
  665. break;
  666. olp = olp->next;
  667. }
  668. if (!olp)
  669. op = NULL;
  670. return op;
  671. }
  672. static ssize_t qib_diag_read(struct file *fp, char __user *data,
  673. size_t count, loff_t *off)
  674. {
  675. struct qib_diag_client *dc = fp->private_data;
  676. struct qib_devdata *dd = dc->dd;
  677. void __iomem *kreg_base;
  678. ssize_t ret;
  679. if (dc->pid != current->pid) {
  680. ret = -EPERM;
  681. goto bail;
  682. }
  683. kreg_base = dd->kregbase;
  684. if (count == 0)
  685. ret = 0;
  686. else if ((count % 4) || (*off % 4))
  687. /* address or length is not 32-bit aligned, hence invalid */
  688. ret = -EINVAL;
  689. else if (dc->state < READY && (*off || count != 8))
  690. ret = -EINVAL; /* prevent cat /dev/qib_diag* */
  691. else {
  692. unsigned long flags;
  693. u64 data64 = 0;
  694. int use_32;
  695. const struct diag_observer *op;
  696. use_32 = (count % 8) || (*off % 8);
  697. ret = -1;
  698. spin_lock_irqsave(&dd->qib_diag_trans_lock, flags);
  699. /*
  700. * Check for observer on this address range.
  701. * we only support a single 32 or 64-bit read
  702. * via observer, currently.
  703. */
  704. op = diag_get_observer(dd, *off);
  705. if (op) {
  706. u32 offset = *off;
  707. ret = op->hook(dd, op, offset, &data64, 0, use_32);
  708. }
  709. /*
  710. * We need to release lock before any copy_to_user(),
  711. * whether implicit in qib_read_umem* or explicit below.
  712. */
  713. spin_unlock_irqrestore(&dd->qib_diag_trans_lock, flags);
  714. if (!op) {
  715. if (use_32)
  716. /*
  717. * Address or length is not 64-bit aligned;
  718. * do 32-bit rd
  719. */
  720. ret = qib_read_umem32(dd, data, (u32) *off,
  721. count);
  722. else
  723. ret = qib_read_umem64(dd, data, (u32) *off,
  724. count);
  725. } else if (ret == count) {
  726. /* Below finishes case where observer existed */
  727. ret = copy_to_user(data, &data64, use_32 ?
  728. sizeof(u32) : sizeof(u64));
  729. if (ret)
  730. ret = -EFAULT;
  731. }
  732. }
  733. if (ret >= 0) {
  734. *off += count;
  735. ret = count;
  736. if (dc->state == OPENED)
  737. dc->state = INIT;
  738. }
  739. bail:
  740. return ret;
  741. }
  742. static ssize_t qib_diag_write(struct file *fp, const char __user *data,
  743. size_t count, loff_t *off)
  744. {
  745. struct qib_diag_client *dc = fp->private_data;
  746. struct qib_devdata *dd = dc->dd;
  747. void __iomem *kreg_base;
  748. ssize_t ret;
  749. if (dc->pid != current->pid) {
  750. ret = -EPERM;
  751. goto bail;
  752. }
  753. kreg_base = dd->kregbase;
  754. if (count == 0)
  755. ret = 0;
  756. else if ((count % 4) || (*off % 4))
  757. /* address or length is not 32-bit aligned, hence invalid */
  758. ret = -EINVAL;
  759. else if (dc->state < READY &&
  760. ((*off || count != 8) || dc->state != INIT))
  761. /* No writes except second-step of init seq */
  762. ret = -EINVAL; /* before any other write allowed */
  763. else {
  764. unsigned long flags;
  765. const struct diag_observer *op = NULL;
  766. int use_32 = (count % 8) || (*off % 8);
  767. /*
  768. * Check for observer on this address range.
  769. * We only support a single 32 or 64-bit write
  770. * via observer, currently. This helps, because
  771. * we would otherwise have to jump through hoops
  772. * to make "diag transaction" meaningful when we
  773. * cannot do a copy_from_user while holding the lock.
  774. */
  775. if (count == 4 || count == 8) {
  776. u64 data64;
  777. u32 offset = *off;
  778. ret = copy_from_user(&data64, data, count);
  779. if (ret) {
  780. ret = -EFAULT;
  781. goto bail;
  782. }
  783. spin_lock_irqsave(&dd->qib_diag_trans_lock, flags);
  784. op = diag_get_observer(dd, *off);
  785. if (op)
  786. ret = op->hook(dd, op, offset, &data64, ~0Ull,
  787. use_32);
  788. spin_unlock_irqrestore(&dd->qib_diag_trans_lock, flags);
  789. }
  790. if (!op) {
  791. if (use_32)
  792. /*
  793. * Address or length is not 64-bit aligned;
  794. * do 32-bit write
  795. */
  796. ret = qib_write_umem32(dd, (u32) *off, data,
  797. count);
  798. else
  799. ret = qib_write_umem64(dd, (u32) *off, data,
  800. count);
  801. }
  802. }
  803. if (ret >= 0) {
  804. *off += count;
  805. ret = count;
  806. if (dc->state == INIT)
  807. dc->state = READY; /* all read/write OK now */
  808. }
  809. bail:
  810. return ret;
  811. }