qib_diag.c 23 KB

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