qib_diag.c 22 KB

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