qib_diag.c 23 KB

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