qib_diag.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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/export.h>
  48. #include <linux/fs.h>
  49. #include <linux/uaccess.h>
  50. #include "qib.h"
  51. #include "qib_common.h"
  52. /*
  53. * Each client that opens the diag device must read then write
  54. * offset 0, to prevent lossage from random cat or od. diag_state
  55. * sequences this "handshake".
  56. */
  57. enum diag_state { UNUSED = 0, OPENED, INIT, READY };
  58. /* State for an individual client. PID so children cannot abuse handshake */
  59. static struct qib_diag_client {
  60. struct qib_diag_client *next;
  61. struct qib_devdata *dd;
  62. pid_t pid;
  63. enum diag_state state;
  64. } *client_pool;
  65. /*
  66. * Get a client struct. Recycled if possible, else kmalloc.
  67. * Must be called with qib_mutex held
  68. */
  69. static struct qib_diag_client *get_client(struct qib_devdata *dd)
  70. {
  71. struct qib_diag_client *dc;
  72. dc = client_pool;
  73. if (dc)
  74. /* got from pool remove it and use */
  75. client_pool = dc->next;
  76. else
  77. /* None in pool, alloc and init */
  78. dc = kmalloc(sizeof *dc, GFP_KERNEL);
  79. if (dc) {
  80. dc->next = NULL;
  81. dc->dd = dd;
  82. dc->pid = current->pid;
  83. dc->state = OPENED;
  84. }
  85. return dc;
  86. }
  87. /*
  88. * Return to pool. Must be called with qib_mutex held
  89. */
  90. static void return_client(struct qib_diag_client *dc)
  91. {
  92. struct qib_devdata *dd = dc->dd;
  93. struct qib_diag_client *tdc, *rdc;
  94. rdc = NULL;
  95. if (dc == dd->diag_client) {
  96. dd->diag_client = dc->next;
  97. rdc = dc;
  98. } else {
  99. tdc = dc->dd->diag_client;
  100. while (tdc) {
  101. if (dc == tdc->next) {
  102. tdc->next = dc->next;
  103. rdc = dc;
  104. break;
  105. }
  106. tdc = tdc->next;
  107. }
  108. }
  109. if (rdc) {
  110. rdc->state = UNUSED;
  111. rdc->dd = NULL;
  112. rdc->pid = 0;
  113. rdc->next = client_pool;
  114. client_pool = rdc;
  115. }
  116. }
  117. static int qib_diag_open(struct inode *in, struct file *fp);
  118. static int qib_diag_release(struct inode *in, struct file *fp);
  119. static ssize_t qib_diag_read(struct file *fp, char __user *data,
  120. size_t count, loff_t *off);
  121. static ssize_t qib_diag_write(struct file *fp, const char __user *data,
  122. size_t count, loff_t *off);
  123. static const struct file_operations diag_file_ops = {
  124. .owner = THIS_MODULE,
  125. .write = qib_diag_write,
  126. .read = qib_diag_read,
  127. .open = qib_diag_open,
  128. .release = qib_diag_release,
  129. .llseek = default_llseek,
  130. };
  131. static atomic_t diagpkt_count = ATOMIC_INIT(0);
  132. static struct cdev *diagpkt_cdev;
  133. static struct device *diagpkt_device;
  134. static ssize_t qib_diagpkt_write(struct file *fp, const char __user *data,
  135. size_t count, loff_t *off);
  136. static const struct file_operations diagpkt_file_ops = {
  137. .owner = THIS_MODULE,
  138. .write = qib_diagpkt_write,
  139. .llseek = noop_llseek,
  140. };
  141. int qib_diag_add(struct qib_devdata *dd)
  142. {
  143. char name[16];
  144. int ret = 0;
  145. if (atomic_inc_return(&diagpkt_count) == 1) {
  146. ret = qib_cdev_init(QIB_DIAGPKT_MINOR, "ipath_diagpkt",
  147. &diagpkt_file_ops, &diagpkt_cdev,
  148. &diagpkt_device);
  149. if (ret)
  150. goto done;
  151. }
  152. snprintf(name, sizeof(name), "ipath_diag%d", dd->unit);
  153. ret = qib_cdev_init(QIB_DIAG_MINOR_BASE + dd->unit, name,
  154. &diag_file_ops, &dd->diag_cdev,
  155. &dd->diag_device);
  156. done:
  157. return ret;
  158. }
  159. static void qib_unregister_observers(struct qib_devdata *dd);
  160. void qib_diag_remove(struct qib_devdata *dd)
  161. {
  162. struct qib_diag_client *dc;
  163. if (atomic_dec_and_test(&diagpkt_count))
  164. qib_cdev_cleanup(&diagpkt_cdev, &diagpkt_device);
  165. qib_cdev_cleanup(&dd->diag_cdev, &dd->diag_device);
  166. /*
  167. * Return all diag_clients of this device. There should be none,
  168. * as we are "guaranteed" that no clients are still open
  169. */
  170. while (dd->diag_client)
  171. return_client(dd->diag_client);
  172. /* Now clean up all unused client structs */
  173. while (client_pool) {
  174. dc = client_pool;
  175. client_pool = dc->next;
  176. kfree(dc);
  177. }
  178. /* Clean up observer list */
  179. qib_unregister_observers(dd);
  180. }
  181. /* qib_remap_ioaddr32 - remap an offset into chip address space to __iomem *
  182. *
  183. * @dd: the qlogic_ib device
  184. * @offs: the offset in chip-space
  185. * @cntp: Pointer to max (byte) count for transfer starting at offset
  186. * This returns a u32 __iomem * so it can be used for both 64 and 32-bit
  187. * mapping. It is needed because with the use of PAT for control of
  188. * write-combining, the logically contiguous address-space of the chip
  189. * may be split into virtually non-contiguous spaces, with different
  190. * attributes, which are them mapped to contiguous physical space
  191. * based from the first BAR.
  192. *
  193. * The code below makes the same assumptions as were made in
  194. * init_chip_wc_pat() (qib_init.c), copied here:
  195. * Assumes chip address space looks like:
  196. * - kregs + sregs + cregs + uregs (in any order)
  197. * - piobufs (2K and 4K bufs in either order)
  198. * or:
  199. * - kregs + sregs + cregs (in any order)
  200. * - piobufs (2K and 4K bufs in either order)
  201. * - uregs
  202. *
  203. * If cntp is non-NULL, returns how many bytes from offset can be accessed
  204. * Returns 0 if the offset is not mapped.
  205. */
  206. static u32 __iomem *qib_remap_ioaddr32(struct qib_devdata *dd, u32 offset,
  207. u32 *cntp)
  208. {
  209. u32 kreglen;
  210. u32 snd_bottom, snd_lim = 0;
  211. u32 __iomem *krb32 = (u32 __iomem *)dd->kregbase;
  212. u32 __iomem *map = NULL;
  213. u32 cnt = 0;
  214. u32 tot4k, offs4k;
  215. /* First, simplest case, offset is within the first map. */
  216. kreglen = (dd->kregend - dd->kregbase) * sizeof(u64);
  217. if (offset < kreglen) {
  218. map = krb32 + (offset / sizeof(u32));
  219. cnt = kreglen - offset;
  220. goto mapped;
  221. }
  222. /*
  223. * Next check for user regs, the next most common case,
  224. * and a cheap check because if they are not in the first map
  225. * they are last in chip.
  226. */
  227. if (dd->userbase) {
  228. /* If user regs mapped, they are after send, so set limit. */
  229. u32 ulim = (dd->cfgctxts * dd->ureg_align) + dd->uregbase;
  230. if (!dd->piovl15base)
  231. snd_lim = dd->uregbase;
  232. krb32 = (u32 __iomem *)dd->userbase;
  233. if (offset >= dd->uregbase && offset < ulim) {
  234. map = krb32 + (offset - dd->uregbase) / sizeof(u32);
  235. cnt = ulim - offset;
  236. goto mapped;
  237. }
  238. }
  239. /*
  240. * Lastly, check for offset within Send Buffers.
  241. * This is gnarly because struct devdata is deliberately vague
  242. * about things like 7322 VL15 buffers, and we are not in
  243. * chip-specific code here, so should not make many assumptions.
  244. * The one we _do_ make is that the only chip that has more sndbufs
  245. * than we admit is the 7322, and it has userregs above that, so
  246. * we know the snd_lim.
  247. */
  248. /* Assume 2K buffers are first. */
  249. snd_bottom = dd->pio2k_bufbase;
  250. if (snd_lim == 0) {
  251. u32 tot2k = dd->piobcnt2k * ALIGN(dd->piosize2k, dd->palign);
  252. snd_lim = snd_bottom + tot2k;
  253. }
  254. /* If 4k buffers exist, account for them by bumping
  255. * appropriate limit.
  256. */
  257. tot4k = dd->piobcnt4k * dd->align4k;
  258. offs4k = dd->piobufbase >> 32;
  259. if (dd->piobcnt4k) {
  260. if (snd_bottom > offs4k)
  261. snd_bottom = offs4k;
  262. else {
  263. /* 4k above 2k. Bump snd_lim, if needed*/
  264. if (!dd->userbase || dd->piovl15base)
  265. snd_lim = offs4k + tot4k;
  266. }
  267. }
  268. /*
  269. * Judgement call: can we ignore the space between SendBuffs and
  270. * UserRegs, where we would like to see vl15 buffs, but not more?
  271. */
  272. if (offset >= snd_bottom && offset < snd_lim) {
  273. offset -= snd_bottom;
  274. map = (u32 __iomem *)dd->piobase + (offset / sizeof(u32));
  275. cnt = snd_lim - offset;
  276. }
  277. if (!map && offs4k && dd->piovl15base) {
  278. snd_lim = offs4k + tot4k + 2 * dd->align4k;
  279. if (offset >= (offs4k + tot4k) && offset < snd_lim) {
  280. map = (u32 __iomem *)dd->piovl15base +
  281. ((offset - (offs4k + tot4k)) / sizeof(u32));
  282. cnt = snd_lim - offset;
  283. }
  284. }
  285. mapped:
  286. if (cntp)
  287. *cntp = cnt;
  288. return map;
  289. }
  290. /*
  291. * qib_read_umem64 - read a 64-bit quantity from the chip into user space
  292. * @dd: the qlogic_ib device
  293. * @uaddr: the location to store the data in user memory
  294. * @regoffs: the offset from BAR0 (_NOT_ full pointer, anymore)
  295. * @count: number of bytes to copy (multiple of 32 bits)
  296. *
  297. * This function also localizes all chip memory accesses.
  298. * The copy should be written such that we read full cacheline packets
  299. * from the chip. This is usually used for a single qword
  300. *
  301. * NOTE: This assumes the chip address is 64-bit aligned.
  302. */
  303. static int qib_read_umem64(struct qib_devdata *dd, void __user *uaddr,
  304. u32 regoffs, size_t count)
  305. {
  306. const u64 __iomem *reg_addr;
  307. const u64 __iomem *reg_end;
  308. u32 limit;
  309. int ret;
  310. reg_addr = (const u64 __iomem *)qib_remap_ioaddr32(dd, regoffs, &limit);
  311. if (reg_addr == NULL || limit == 0 || !(dd->flags & QIB_PRESENT)) {
  312. ret = -EINVAL;
  313. goto bail;
  314. }
  315. if (count >= limit)
  316. count = limit;
  317. reg_end = reg_addr + (count / sizeof(u64));
  318. /* not very efficient, but it works for now */
  319. while (reg_addr < reg_end) {
  320. u64 data = readq(reg_addr);
  321. if (copy_to_user(uaddr, &data, sizeof(u64))) {
  322. ret = -EFAULT;
  323. goto bail;
  324. }
  325. reg_addr++;
  326. uaddr += sizeof(u64);
  327. }
  328. ret = 0;
  329. bail:
  330. return ret;
  331. }
  332. /*
  333. * qib_write_umem64 - write a 64-bit quantity to the chip from user space
  334. * @dd: the qlogic_ib device
  335. * @regoffs: the offset from BAR0 (_NOT_ full pointer, anymore)
  336. * @uaddr: the source of the data in user memory
  337. * @count: the number of bytes to copy (multiple of 32 bits)
  338. *
  339. * This is usually used for a single qword
  340. * NOTE: This assumes the chip address is 64-bit aligned.
  341. */
  342. static int qib_write_umem64(struct qib_devdata *dd, u32 regoffs,
  343. const void __user *uaddr, size_t count)
  344. {
  345. u64 __iomem *reg_addr;
  346. const u64 __iomem *reg_end;
  347. u32 limit;
  348. int ret;
  349. reg_addr = (u64 __iomem *)qib_remap_ioaddr32(dd, regoffs, &limit);
  350. if (reg_addr == NULL || limit == 0 || !(dd->flags & QIB_PRESENT)) {
  351. ret = -EINVAL;
  352. goto bail;
  353. }
  354. if (count >= limit)
  355. count = limit;
  356. reg_end = reg_addr + (count / sizeof(u64));
  357. /* not very efficient, but it works for now */
  358. while (reg_addr < reg_end) {
  359. u64 data;
  360. if (copy_from_user(&data, uaddr, sizeof(data))) {
  361. ret = -EFAULT;
  362. goto bail;
  363. }
  364. writeq(data, reg_addr);
  365. reg_addr++;
  366. uaddr += sizeof(u64);
  367. }
  368. ret = 0;
  369. bail:
  370. return ret;
  371. }
  372. /*
  373. * qib_read_umem32 - read a 32-bit quantity from the chip into user space
  374. * @dd: the qlogic_ib device
  375. * @uaddr: the location to store the data in user memory
  376. * @regoffs: the offset from BAR0 (_NOT_ full pointer, anymore)
  377. * @count: number of bytes to copy
  378. *
  379. * read 32 bit values, not 64 bit; for memories that only
  380. * support 32 bit reads; usually a single dword.
  381. */
  382. static int qib_read_umem32(struct qib_devdata *dd, void __user *uaddr,
  383. u32 regoffs, size_t count)
  384. {
  385. const u32 __iomem *reg_addr;
  386. const u32 __iomem *reg_end;
  387. u32 limit;
  388. int ret;
  389. reg_addr = qib_remap_ioaddr32(dd, regoffs, &limit);
  390. if (reg_addr == NULL || limit == 0 || !(dd->flags & QIB_PRESENT)) {
  391. ret = -EINVAL;
  392. goto bail;
  393. }
  394. if (count >= limit)
  395. count = limit;
  396. reg_end = reg_addr + (count / sizeof(u32));
  397. /* not very efficient, but it works for now */
  398. while (reg_addr < reg_end) {
  399. u32 data = readl(reg_addr);
  400. if (copy_to_user(uaddr, &data, sizeof(data))) {
  401. ret = -EFAULT;
  402. goto bail;
  403. }
  404. reg_addr++;
  405. uaddr += sizeof(u32);
  406. }
  407. ret = 0;
  408. bail:
  409. return ret;
  410. }
  411. /*
  412. * qib_write_umem32 - write a 32-bit quantity to the chip from user space
  413. * @dd: the qlogic_ib device
  414. * @regoffs: the offset from BAR0 (_NOT_ full pointer, anymore)
  415. * @uaddr: the source of the data in user memory
  416. * @count: number of bytes to copy
  417. *
  418. * write 32 bit values, not 64 bit; for memories that only
  419. * support 32 bit write; usually a single dword.
  420. */
  421. static int qib_write_umem32(struct qib_devdata *dd, u32 regoffs,
  422. const void __user *uaddr, size_t count)
  423. {
  424. u32 __iomem *reg_addr;
  425. const u32 __iomem *reg_end;
  426. u32 limit;
  427. int ret;
  428. reg_addr = qib_remap_ioaddr32(dd, regoffs, &limit);
  429. if (reg_addr == NULL || limit == 0 || !(dd->flags & QIB_PRESENT)) {
  430. ret = -EINVAL;
  431. goto bail;
  432. }
  433. if (count >= limit)
  434. count = limit;
  435. reg_end = reg_addr + (count / sizeof(u32));
  436. while (reg_addr < reg_end) {
  437. u32 data;
  438. if (copy_from_user(&data, uaddr, sizeof(data))) {
  439. ret = -EFAULT;
  440. goto bail;
  441. }
  442. writel(data, reg_addr);
  443. reg_addr++;
  444. uaddr += sizeof(u32);
  445. }
  446. ret = 0;
  447. bail:
  448. return ret;
  449. }
  450. static int qib_diag_open(struct inode *in, struct file *fp)
  451. {
  452. int unit = iminor(in) - QIB_DIAG_MINOR_BASE;
  453. struct qib_devdata *dd;
  454. struct qib_diag_client *dc;
  455. int ret;
  456. mutex_lock(&qib_mutex);
  457. dd = qib_lookup(unit);
  458. if (dd == NULL || !(dd->flags & QIB_PRESENT) ||
  459. !dd->kregbase) {
  460. ret = -ENODEV;
  461. goto bail;
  462. }
  463. dc = get_client(dd);
  464. if (!dc) {
  465. ret = -ENOMEM;
  466. goto bail;
  467. }
  468. dc->next = dd->diag_client;
  469. dd->diag_client = dc;
  470. fp->private_data = dc;
  471. ret = 0;
  472. bail:
  473. mutex_unlock(&qib_mutex);
  474. return ret;
  475. }
  476. /**
  477. * qib_diagpkt_write - write an IB packet
  478. * @fp: the diag data device file pointer
  479. * @data: qib_diag_pkt structure saying where to get the packet
  480. * @count: size of data to write
  481. * @off: unused by this code
  482. */
  483. static ssize_t qib_diagpkt_write(struct file *fp,
  484. const char __user *data,
  485. size_t count, loff_t *off)
  486. {
  487. u32 __iomem *piobuf;
  488. u32 plen, clen, pbufn;
  489. struct qib_diag_xpkt dp;
  490. u32 *tmpbuf = NULL;
  491. struct qib_devdata *dd;
  492. struct qib_pportdata *ppd;
  493. ssize_t ret = 0;
  494. if (count != sizeof(dp)) {
  495. ret = -EINVAL;
  496. goto bail;
  497. }
  498. if (copy_from_user(&dp, data, sizeof(dp))) {
  499. ret = -EFAULT;
  500. goto bail;
  501. }
  502. dd = qib_lookup(dp.unit);
  503. if (!dd || !(dd->flags & QIB_PRESENT) || !dd->kregbase) {
  504. ret = -ENODEV;
  505. goto bail;
  506. }
  507. if (!(dd->flags & QIB_INITTED)) {
  508. /* no hardware, freeze, etc. */
  509. ret = -ENODEV;
  510. goto bail;
  511. }
  512. if (dp.version != _DIAG_XPKT_VERS) {
  513. qib_dev_err(dd, "Invalid version %u for diagpkt_write\n",
  514. dp.version);
  515. ret = -EINVAL;
  516. goto bail;
  517. }
  518. /* send count must be an exact number of dwords */
  519. if (dp.len & 3) {
  520. ret = -EINVAL;
  521. goto bail;
  522. }
  523. if (!dp.port || dp.port > dd->num_pports) {
  524. ret = -EINVAL;
  525. goto bail;
  526. }
  527. ppd = &dd->pport[dp.port - 1];
  528. /* need total length before first word written */
  529. /* +1 word is for the qword padding */
  530. plen = sizeof(u32) + dp.len;
  531. clen = dp.len >> 2;
  532. if ((plen + 4) > ppd->ibmaxlen) {
  533. ret = -EINVAL;
  534. goto bail; /* before writing pbc */
  535. }
  536. tmpbuf = vmalloc(plen);
  537. if (!tmpbuf) {
  538. qib_devinfo(dd->pcidev, "Unable to allocate tmp buffer, "
  539. "failing\n");
  540. ret = -ENOMEM;
  541. goto bail;
  542. }
  543. if (copy_from_user(tmpbuf,
  544. (const void __user *) (unsigned long) dp.data,
  545. dp.len)) {
  546. ret = -EFAULT;
  547. goto bail;
  548. }
  549. plen >>= 2; /* in dwords */
  550. if (dp.pbc_wd == 0)
  551. dp.pbc_wd = plen;
  552. piobuf = dd->f_getsendbuf(ppd, dp.pbc_wd, &pbufn);
  553. if (!piobuf) {
  554. ret = -EBUSY;
  555. goto bail;
  556. }
  557. /* disarm it just to be extra sure */
  558. dd->f_sendctrl(dd->pport, QIB_SENDCTRL_DISARM_BUF(pbufn));
  559. /* disable header check on pbufn for this packet */
  560. dd->f_txchk_change(dd, pbufn, 1, TXCHK_CHG_TYPE_DIS1, NULL);
  561. writeq(dp.pbc_wd, piobuf);
  562. /*
  563. * Copy all but the trigger word, then flush, so it's written
  564. * to chip before trigger word, then write trigger word, then
  565. * flush again, so packet is sent.
  566. */
  567. if (dd->flags & QIB_PIO_FLUSH_WC) {
  568. qib_flush_wc();
  569. qib_pio_copy(piobuf + 2, tmpbuf, clen - 1);
  570. qib_flush_wc();
  571. __raw_writel(tmpbuf[clen - 1], piobuf + clen + 1);
  572. } else
  573. qib_pio_copy(piobuf + 2, tmpbuf, clen);
  574. if (dd->flags & QIB_USE_SPCL_TRIG) {
  575. u32 spcl_off = (pbufn >= dd->piobcnt2k) ? 2047 : 1023;
  576. qib_flush_wc();
  577. __raw_writel(0xaebecede, piobuf + spcl_off);
  578. }
  579. /*
  580. * Ensure buffer is written to the chip, then re-enable
  581. * header checks (if supported by chip). The txchk
  582. * code will ensure seen by chip before returning.
  583. */
  584. qib_flush_wc();
  585. qib_sendbuf_done(dd, pbufn);
  586. dd->f_txchk_change(dd, pbufn, 1, TXCHK_CHG_TYPE_ENAB1, NULL);
  587. ret = sizeof(dp);
  588. bail:
  589. vfree(tmpbuf);
  590. return ret;
  591. }
  592. static int qib_diag_release(struct inode *in, struct file *fp)
  593. {
  594. mutex_lock(&qib_mutex);
  595. return_client(fp->private_data);
  596. fp->private_data = NULL;
  597. mutex_unlock(&qib_mutex);
  598. return 0;
  599. }
  600. /*
  601. * Chip-specific code calls to register its interest in
  602. * a specific range.
  603. */
  604. struct diag_observer_list_elt {
  605. struct diag_observer_list_elt *next;
  606. const struct diag_observer *op;
  607. };
  608. int qib_register_observer(struct qib_devdata *dd,
  609. const struct diag_observer *op)
  610. {
  611. struct diag_observer_list_elt *olp;
  612. int ret = -EINVAL;
  613. if (!dd || !op)
  614. goto bail;
  615. ret = -ENOMEM;
  616. olp = vmalloc(sizeof *olp);
  617. if (!olp) {
  618. printk(KERN_ERR QIB_DRV_NAME ": vmalloc for observer failed\n");
  619. goto bail;
  620. }
  621. if (olp) {
  622. unsigned long flags;
  623. spin_lock_irqsave(&dd->qib_diag_trans_lock, flags);
  624. olp->op = op;
  625. olp->next = dd->diag_observer_list;
  626. dd->diag_observer_list = olp;
  627. spin_unlock_irqrestore(&dd->qib_diag_trans_lock, flags);
  628. ret = 0;
  629. }
  630. bail:
  631. return ret;
  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. }