cmf.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. /*
  2. * linux/drivers/s390/cio/cmf.c ($Revision: 1.16 $)
  3. *
  4. * Linux on zSeries Channel Measurement Facility support
  5. *
  6. * Copyright 2000,2003 IBM Corporation
  7. *
  8. * Author: Arnd Bergmann <arndb@de.ibm.com>
  9. *
  10. * original idea from Natarajan Krishnaswami <nkrishna@us.ibm.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. */
  26. #include <linux/bootmem.h>
  27. #include <linux/device.h>
  28. #include <linux/init.h>
  29. #include <linux/list.h>
  30. #include <linux/module.h>
  31. #include <linux/moduleparam.h>
  32. #include <asm/ccwdev.h>
  33. #include <asm/cio.h>
  34. #include <asm/cmb.h>
  35. #include "cio.h"
  36. #include "css.h"
  37. #include "device.h"
  38. #include "ioasm.h"
  39. #include "chsc.h"
  40. /* parameter to enable cmf during boot, possible uses are:
  41. * "s390cmf" -- enable cmf and allocate 2 MB of ram so measuring can be
  42. * used on any subchannel
  43. * "s390cmf=<num>" -- enable cmf and allocate enough memory to measure
  44. * <num> subchannel, where <num> is an integer
  45. * between 1 and 65535, default is 1024
  46. */
  47. #define ARGSTRING "s390cmf"
  48. /* indices for READCMB */
  49. enum cmb_index {
  50. /* basic and exended format: */
  51. cmb_ssch_rsch_count,
  52. cmb_sample_count,
  53. cmb_device_connect_time,
  54. cmb_function_pending_time,
  55. cmb_device_disconnect_time,
  56. cmb_control_unit_queuing_time,
  57. cmb_device_active_only_time,
  58. /* extended format only: */
  59. cmb_device_busy_time,
  60. cmb_initial_command_response_time,
  61. };
  62. /**
  63. * enum cmb_format - types of supported measurement block formats
  64. *
  65. * @CMF_BASIC: traditional channel measurement blocks supported
  66. * by all machines that we run on
  67. * @CMF_EXTENDED: improved format that was introduced with the z990
  68. * machine
  69. * @CMF_AUTODETECT: default: use extended format when running on a z990
  70. * or later machine, otherwise fall back to basic format
  71. **/
  72. enum cmb_format {
  73. CMF_BASIC,
  74. CMF_EXTENDED,
  75. CMF_AUTODETECT = -1,
  76. };
  77. /**
  78. * format - actual format for all measurement blocks
  79. *
  80. * The format module parameter can be set to a value of 0 (zero)
  81. * or 1, indicating basic or extended format as described for
  82. * enum cmb_format.
  83. */
  84. static int format = CMF_AUTODETECT;
  85. module_param(format, bool, 0444);
  86. /**
  87. * struct cmb_operations - functions to use depending on cmb_format
  88. *
  89. * all these functions operate on a struct cmf_device. There is only
  90. * one instance of struct cmb_operations because all cmf_device
  91. * objects are guaranteed to be of the same type.
  92. *
  93. * @alloc: allocate memory for a channel measurement block,
  94. * either with the help of a special pool or with kmalloc
  95. * @free: free memory allocated with @alloc
  96. * @set: enable or disable measurement
  97. * @readall: read a measurement block in a common format
  98. * @reset: clear the data in the associated measurement block and
  99. * reset its time stamp
  100. */
  101. struct cmb_operations {
  102. int (*alloc) (struct ccw_device*);
  103. void(*free) (struct ccw_device*);
  104. int (*set) (struct ccw_device*, u32);
  105. u64 (*read) (struct ccw_device*, int);
  106. int (*readall)(struct ccw_device*, struct cmbdata *);
  107. void (*reset) (struct ccw_device*);
  108. struct attribute_group *attr_group;
  109. };
  110. static struct cmb_operations *cmbops;
  111. /* our user interface is designed in terms of nanoseconds,
  112. * while the hardware measures total times in its own
  113. * unit.*/
  114. static inline u64 time_to_nsec(u32 value)
  115. {
  116. return ((u64)value) * 128000ull;
  117. }
  118. /*
  119. * Users are usually interested in average times,
  120. * not accumulated time.
  121. * This also helps us with atomicity problems
  122. * when reading sinlge values.
  123. */
  124. static inline u64 time_to_avg_nsec(u32 value, u32 count)
  125. {
  126. u64 ret;
  127. /* no samples yet, avoid division by 0 */
  128. if (count == 0)
  129. return 0;
  130. /* value comes in units of 128 µsec */
  131. ret = time_to_nsec(value);
  132. do_div(ret, count);
  133. return ret;
  134. }
  135. /* activate or deactivate the channel monitor. When area is NULL,
  136. * the monitor is deactivated. The channel monitor needs to
  137. * be active in order to measure subchannels, which also need
  138. * to be enabled. */
  139. static inline void
  140. cmf_activate(void *area, unsigned int onoff)
  141. {
  142. register void * __gpr2 asm("2");
  143. register long __gpr1 asm("1");
  144. __gpr2 = area;
  145. __gpr1 = onoff ? 2 : 0;
  146. /* activate channel measurement */
  147. asm("schm" : : "d" (__gpr2), "d" (__gpr1) );
  148. }
  149. static int
  150. set_schib(struct ccw_device *cdev, u32 mme, int mbfc, unsigned long address)
  151. {
  152. int ret;
  153. int retry;
  154. struct subchannel *sch;
  155. struct schib *schib;
  156. sch = to_subchannel(cdev->dev.parent);
  157. schib = &sch->schib;
  158. /* msch can silently fail, so do it again if necessary */
  159. for (retry = 0; retry < 3; retry++) {
  160. /* prepare schib */
  161. stsch(sch->irq, schib);
  162. schib->pmcw.mme = mme;
  163. schib->pmcw.mbfc = mbfc;
  164. /* address can be either a block address or a block index */
  165. if (mbfc)
  166. schib->mba = address;
  167. else
  168. schib->pmcw.mbi = address;
  169. /* try to submit it */
  170. switch(ret = msch_err(sch->irq, schib)) {
  171. case 0:
  172. break;
  173. case 1:
  174. case 2: /* in I/O or status pending */
  175. ret = -EBUSY;
  176. break;
  177. case 3: /* subchannel is no longer valid */
  178. ret = -ENODEV;
  179. break;
  180. default: /* msch caught an exception */
  181. ret = -EINVAL;
  182. break;
  183. }
  184. stsch(sch->irq, schib); /* restore the schib */
  185. if (ret)
  186. break;
  187. /* check if it worked */
  188. if (schib->pmcw.mme == mme &&
  189. schib->pmcw.mbfc == mbfc &&
  190. (mbfc ? (schib->mba == address)
  191. : (schib->pmcw.mbi == address)))
  192. return 0;
  193. ret = -EINVAL;
  194. }
  195. return ret;
  196. }
  197. struct set_schib_struct {
  198. u32 mme;
  199. int mbfc;
  200. unsigned long address;
  201. wait_queue_head_t wait;
  202. int ret;
  203. };
  204. static int set_schib_wait(struct ccw_device *cdev, u32 mme,
  205. int mbfc, unsigned long address)
  206. {
  207. struct set_schib_struct s = {
  208. .mme = mme,
  209. .mbfc = mbfc,
  210. .address = address,
  211. .wait = __WAIT_QUEUE_HEAD_INITIALIZER(s.wait),
  212. };
  213. spin_lock_irq(cdev->ccwlock);
  214. s.ret = set_schib(cdev, mme, mbfc, address);
  215. if (s.ret != -EBUSY) {
  216. goto out_nowait;
  217. }
  218. if (cdev->private->state != DEV_STATE_ONLINE) {
  219. s.ret = -EBUSY;
  220. /* if the device is not online, don't even try again */
  221. goto out_nowait;
  222. }
  223. cdev->private->state = DEV_STATE_CMFCHANGE;
  224. cdev->private->cmb_wait = &s;
  225. s.ret = 1;
  226. spin_unlock_irq(cdev->ccwlock);
  227. if (wait_event_interruptible(s.wait, s.ret != 1)) {
  228. spin_lock_irq(cdev->ccwlock);
  229. if (s.ret == 1) {
  230. s.ret = -ERESTARTSYS;
  231. cdev->private->cmb_wait = 0;
  232. if (cdev->private->state == DEV_STATE_CMFCHANGE)
  233. cdev->private->state = DEV_STATE_ONLINE;
  234. }
  235. spin_unlock_irq(cdev->ccwlock);
  236. }
  237. return s.ret;
  238. out_nowait:
  239. spin_unlock_irq(cdev->ccwlock);
  240. return s.ret;
  241. }
  242. void retry_set_schib(struct ccw_device *cdev)
  243. {
  244. struct set_schib_struct *s;
  245. s = cdev->private->cmb_wait;
  246. cdev->private->cmb_wait = 0;
  247. if (!s) {
  248. WARN_ON(1);
  249. return;
  250. }
  251. s->ret = set_schib(cdev, s->mme, s->mbfc, s->address);
  252. wake_up(&s->wait);
  253. }
  254. /**
  255. * struct cmb_area - container for global cmb data
  256. *
  257. * @mem: pointer to CMBs (only in basic measurement mode)
  258. * @list: contains a linked list of all subchannels
  259. * @lock: protect concurrent access to @mem and @list
  260. */
  261. struct cmb_area {
  262. struct cmb *mem;
  263. struct list_head list;
  264. int num_channels;
  265. spinlock_t lock;
  266. };
  267. static struct cmb_area cmb_area = {
  268. .lock = SPIN_LOCK_UNLOCKED,
  269. .list = LIST_HEAD_INIT(cmb_area.list),
  270. .num_channels = 1024,
  271. };
  272. /* ****** old style CMB handling ********/
  273. /** int maxchannels
  274. *
  275. * Basic channel measurement blocks are allocated in one contiguous
  276. * block of memory, which can not be moved as long as any channel
  277. * is active. Therefore, a maximum number of subchannels needs to
  278. * be defined somewhere. This is a module parameter, defaulting to
  279. * a resonable value of 1024, or 32 kb of memory.
  280. * Current kernels don't allow kmalloc with more than 128kb, so the
  281. * maximum is 4096
  282. */
  283. module_param_named(maxchannels, cmb_area.num_channels, uint, 0444);
  284. /**
  285. * struct cmb - basic channel measurement block
  286. *
  287. * cmb as used by the hardware the fields are described in z/Architecture
  288. * Principles of Operation, chapter 17.
  289. * The area to be a contiguous array and may not be reallocated or freed.
  290. * Only one cmb area can be present in the system.
  291. */
  292. struct cmb {
  293. u16 ssch_rsch_count;
  294. u16 sample_count;
  295. u32 device_connect_time;
  296. u32 function_pending_time;
  297. u32 device_disconnect_time;
  298. u32 control_unit_queuing_time;
  299. u32 device_active_only_time;
  300. u32 reserved[2];
  301. };
  302. /* insert a single device into the cmb_area list
  303. * called with cmb_area.lock held from alloc_cmb
  304. */
  305. static inline int
  306. alloc_cmb_single (struct ccw_device *cdev)
  307. {
  308. struct cmb *cmb;
  309. struct ccw_device_private *node;
  310. int ret;
  311. spin_lock_irq(cdev->ccwlock);
  312. if (!list_empty(&cdev->private->cmb_list)) {
  313. ret = -EBUSY;
  314. goto out;
  315. }
  316. /* find first unused cmb in cmb_area.mem.
  317. * this is a little tricky: cmb_area.list
  318. * remains sorted by ->cmb pointers */
  319. cmb = cmb_area.mem;
  320. list_for_each_entry(node, &cmb_area.list, cmb_list) {
  321. if ((struct cmb*)node->cmb > cmb)
  322. break;
  323. cmb++;
  324. }
  325. if (cmb - cmb_area.mem >= cmb_area.num_channels) {
  326. ret = -ENOMEM;
  327. goto out;
  328. }
  329. /* insert new cmb */
  330. list_add_tail(&cdev->private->cmb_list, &node->cmb_list);
  331. cdev->private->cmb = cmb;
  332. ret = 0;
  333. out:
  334. spin_unlock_irq(cdev->ccwlock);
  335. return ret;
  336. }
  337. static int
  338. alloc_cmb (struct ccw_device *cdev)
  339. {
  340. int ret;
  341. struct cmb *mem;
  342. ssize_t size;
  343. spin_lock(&cmb_area.lock);
  344. if (!cmb_area.mem) {
  345. /* there is no user yet, so we need a new area */
  346. size = sizeof(struct cmb) * cmb_area.num_channels;
  347. WARN_ON(!list_empty(&cmb_area.list));
  348. spin_unlock(&cmb_area.lock);
  349. mem = (void*)__get_free_pages(GFP_KERNEL | GFP_DMA,
  350. get_order(size));
  351. spin_lock(&cmb_area.lock);
  352. if (cmb_area.mem) {
  353. /* ok, another thread was faster */
  354. free_pages((unsigned long)mem, get_order(size));
  355. } else if (!mem) {
  356. /* no luck */
  357. ret = -ENOMEM;
  358. goto out;
  359. } else {
  360. /* everything ok */
  361. memset(mem, 0, size);
  362. cmb_area.mem = mem;
  363. cmf_activate(cmb_area.mem, 1);
  364. }
  365. }
  366. /* do the actual allocation */
  367. ret = alloc_cmb_single(cdev);
  368. out:
  369. spin_unlock(&cmb_area.lock);
  370. return ret;
  371. }
  372. static void
  373. free_cmb(struct ccw_device *cdev)
  374. {
  375. struct ccw_device_private *priv;
  376. priv = cdev->private;
  377. spin_lock(&cmb_area.lock);
  378. spin_lock_irq(cdev->ccwlock);
  379. if (list_empty(&priv->cmb_list)) {
  380. /* already freed */
  381. goto out;
  382. }
  383. priv->cmb = NULL;
  384. list_del_init(&priv->cmb_list);
  385. if (list_empty(&cmb_area.list)) {
  386. ssize_t size;
  387. size = sizeof(struct cmb) * cmb_area.num_channels;
  388. cmf_activate(NULL, 0);
  389. free_pages((unsigned long)cmb_area.mem, get_order(size));
  390. cmb_area.mem = NULL;
  391. }
  392. out:
  393. spin_unlock_irq(cdev->ccwlock);
  394. spin_unlock(&cmb_area.lock);
  395. }
  396. static int
  397. set_cmb(struct ccw_device *cdev, u32 mme)
  398. {
  399. u16 offset;
  400. if (!cdev->private->cmb)
  401. return -EINVAL;
  402. offset = mme ? (struct cmb *)cdev->private->cmb - cmb_area.mem : 0;
  403. return set_schib_wait(cdev, mme, 0, offset);
  404. }
  405. static u64
  406. read_cmb (struct ccw_device *cdev, int index)
  407. {
  408. /* yes, we have to put it on the stack
  409. * because the cmb must only be accessed
  410. * atomically, e.g. with mvc */
  411. struct cmb cmb;
  412. unsigned long flags;
  413. u32 val;
  414. spin_lock_irqsave(cdev->ccwlock, flags);
  415. if (!cdev->private->cmb) {
  416. spin_unlock_irqrestore(cdev->ccwlock, flags);
  417. return 0;
  418. }
  419. cmb = *(struct cmb*)cdev->private->cmb;
  420. spin_unlock_irqrestore(cdev->ccwlock, flags);
  421. switch (index) {
  422. case cmb_ssch_rsch_count:
  423. return cmb.ssch_rsch_count;
  424. case cmb_sample_count:
  425. return cmb.sample_count;
  426. case cmb_device_connect_time:
  427. val = cmb.device_connect_time;
  428. break;
  429. case cmb_function_pending_time:
  430. val = cmb.function_pending_time;
  431. break;
  432. case cmb_device_disconnect_time:
  433. val = cmb.device_disconnect_time;
  434. break;
  435. case cmb_control_unit_queuing_time:
  436. val = cmb.control_unit_queuing_time;
  437. break;
  438. case cmb_device_active_only_time:
  439. val = cmb.device_active_only_time;
  440. break;
  441. default:
  442. return 0;
  443. }
  444. return time_to_avg_nsec(val, cmb.sample_count);
  445. }
  446. static int
  447. readall_cmb (struct ccw_device *cdev, struct cmbdata *data)
  448. {
  449. /* yes, we have to put it on the stack
  450. * because the cmb must only be accessed
  451. * atomically, e.g. with mvc */
  452. struct cmb cmb;
  453. unsigned long flags;
  454. u64 time;
  455. spin_lock_irqsave(cdev->ccwlock, flags);
  456. if (!cdev->private->cmb) {
  457. spin_unlock_irqrestore(cdev->ccwlock, flags);
  458. return -ENODEV;
  459. }
  460. cmb = *(struct cmb*)cdev->private->cmb;
  461. time = get_clock() - cdev->private->cmb_start_time;
  462. spin_unlock_irqrestore(cdev->ccwlock, flags);
  463. memset(data, 0, sizeof(struct cmbdata));
  464. /* we only know values before device_busy_time */
  465. data->size = offsetof(struct cmbdata, device_busy_time);
  466. /* convert to nanoseconds */
  467. data->elapsed_time = (time * 1000) >> 12;
  468. /* copy data to new structure */
  469. data->ssch_rsch_count = cmb.ssch_rsch_count;
  470. data->sample_count = cmb.sample_count;
  471. /* time fields are converted to nanoseconds while copying */
  472. data->device_connect_time = time_to_nsec(cmb.device_connect_time);
  473. data->function_pending_time = time_to_nsec(cmb.function_pending_time);
  474. data->device_disconnect_time = time_to_nsec(cmb.device_disconnect_time);
  475. data->control_unit_queuing_time
  476. = time_to_nsec(cmb.control_unit_queuing_time);
  477. data->device_active_only_time
  478. = time_to_nsec(cmb.device_active_only_time);
  479. return 0;
  480. }
  481. static void
  482. reset_cmb(struct ccw_device *cdev)
  483. {
  484. struct cmb *cmb;
  485. spin_lock_irq(cdev->ccwlock);
  486. cmb = cdev->private->cmb;
  487. if (cmb)
  488. memset (cmb, 0, sizeof (*cmb));
  489. cdev->private->cmb_start_time = get_clock();
  490. spin_unlock_irq(cdev->ccwlock);
  491. }
  492. static struct attribute_group cmf_attr_group;
  493. static struct cmb_operations cmbops_basic = {
  494. .alloc = alloc_cmb,
  495. .free = free_cmb,
  496. .set = set_cmb,
  497. .read = read_cmb,
  498. .readall = readall_cmb,
  499. .reset = reset_cmb,
  500. .attr_group = &cmf_attr_group,
  501. };
  502. /* ******** extended cmb handling ********/
  503. /**
  504. * struct cmbe - extended channel measurement block
  505. *
  506. * cmb as used by the hardware, may be in any 64 bit physical location,
  507. * the fields are described in z/Architecture Principles of Operation,
  508. * third edition, chapter 17.
  509. */
  510. struct cmbe {
  511. u32 ssch_rsch_count;
  512. u32 sample_count;
  513. u32 device_connect_time;
  514. u32 function_pending_time;
  515. u32 device_disconnect_time;
  516. u32 control_unit_queuing_time;
  517. u32 device_active_only_time;
  518. u32 device_busy_time;
  519. u32 initial_command_response_time;
  520. u32 reserved[7];
  521. };
  522. /* kmalloc only guarantees 8 byte alignment, but we need cmbe
  523. * pointers to be naturally aligned. Make sure to allocate
  524. * enough space for two cmbes */
  525. static inline struct cmbe* cmbe_align(struct cmbe *c)
  526. {
  527. unsigned long addr;
  528. addr = ((unsigned long)c + sizeof (struct cmbe) - sizeof(long)) &
  529. ~(sizeof (struct cmbe) - sizeof(long));
  530. return (struct cmbe*)addr;
  531. }
  532. static int
  533. alloc_cmbe (struct ccw_device *cdev)
  534. {
  535. struct cmbe *cmbe;
  536. cmbe = kmalloc (sizeof (*cmbe) * 2, GFP_KERNEL);
  537. if (!cmbe)
  538. return -ENOMEM;
  539. spin_lock_irq(cdev->ccwlock);
  540. if (cdev->private->cmb) {
  541. kfree(cmbe);
  542. spin_unlock_irq(cdev->ccwlock);
  543. return -EBUSY;
  544. }
  545. cdev->private->cmb = cmbe;
  546. spin_unlock_irq(cdev->ccwlock);
  547. /* activate global measurement if this is the first channel */
  548. spin_lock(&cmb_area.lock);
  549. if (list_empty(&cmb_area.list))
  550. cmf_activate(NULL, 1);
  551. list_add_tail(&cdev->private->cmb_list, &cmb_area.list);
  552. spin_unlock(&cmb_area.lock);
  553. return 0;
  554. }
  555. static void
  556. free_cmbe (struct ccw_device *cdev)
  557. {
  558. spin_lock_irq(cdev->ccwlock);
  559. if (cdev->private->cmb)
  560. kfree(cdev->private->cmb);
  561. cdev->private->cmb = NULL;
  562. spin_unlock_irq(cdev->ccwlock);
  563. /* deactivate global measurement if this is the last channel */
  564. spin_lock(&cmb_area.lock);
  565. list_del_init(&cdev->private->cmb_list);
  566. if (list_empty(&cmb_area.list))
  567. cmf_activate(NULL, 0);
  568. spin_unlock(&cmb_area.lock);
  569. }
  570. static int
  571. set_cmbe(struct ccw_device *cdev, u32 mme)
  572. {
  573. unsigned long mba;
  574. if (!cdev->private->cmb)
  575. return -EINVAL;
  576. mba = mme ? (unsigned long) cmbe_align(cdev->private->cmb) : 0;
  577. return set_schib_wait(cdev, mme, 1, mba);
  578. }
  579. u64
  580. read_cmbe (struct ccw_device *cdev, int index)
  581. {
  582. /* yes, we have to put it on the stack
  583. * because the cmb must only be accessed
  584. * atomically, e.g. with mvc */
  585. struct cmbe cmb;
  586. unsigned long flags;
  587. u32 val;
  588. spin_lock_irqsave(cdev->ccwlock, flags);
  589. if (!cdev->private->cmb) {
  590. spin_unlock_irqrestore(cdev->ccwlock, flags);
  591. return 0;
  592. }
  593. cmb = *cmbe_align(cdev->private->cmb);
  594. spin_unlock_irqrestore(cdev->ccwlock, flags);
  595. switch (index) {
  596. case cmb_ssch_rsch_count:
  597. return cmb.ssch_rsch_count;
  598. case cmb_sample_count:
  599. return cmb.sample_count;
  600. case cmb_device_connect_time:
  601. val = cmb.device_connect_time;
  602. break;
  603. case cmb_function_pending_time:
  604. val = cmb.function_pending_time;
  605. break;
  606. case cmb_device_disconnect_time:
  607. val = cmb.device_disconnect_time;
  608. break;
  609. case cmb_control_unit_queuing_time:
  610. val = cmb.control_unit_queuing_time;
  611. break;
  612. case cmb_device_active_only_time:
  613. val = cmb.device_active_only_time;
  614. break;
  615. case cmb_device_busy_time:
  616. val = cmb.device_busy_time;
  617. break;
  618. case cmb_initial_command_response_time:
  619. val = cmb.initial_command_response_time;
  620. break;
  621. default:
  622. return 0;
  623. }
  624. return time_to_avg_nsec(val, cmb.sample_count);
  625. }
  626. static int
  627. readall_cmbe (struct ccw_device *cdev, struct cmbdata *data)
  628. {
  629. /* yes, we have to put it on the stack
  630. * because the cmb must only be accessed
  631. * atomically, e.g. with mvc */
  632. struct cmbe cmb;
  633. unsigned long flags;
  634. u64 time;
  635. spin_lock_irqsave(cdev->ccwlock, flags);
  636. if (!cdev->private->cmb) {
  637. spin_unlock_irqrestore(cdev->ccwlock, flags);
  638. return -ENODEV;
  639. }
  640. cmb = *cmbe_align(cdev->private->cmb);
  641. time = get_clock() - cdev->private->cmb_start_time;
  642. spin_unlock_irqrestore(cdev->ccwlock, flags);
  643. memset (data, 0, sizeof(struct cmbdata));
  644. /* we only know values before device_busy_time */
  645. data->size = offsetof(struct cmbdata, device_busy_time);
  646. /* conver to nanoseconds */
  647. data->elapsed_time = (time * 1000) >> 12;
  648. /* copy data to new structure */
  649. data->ssch_rsch_count = cmb.ssch_rsch_count;
  650. data->sample_count = cmb.sample_count;
  651. /* time fields are converted to nanoseconds while copying */
  652. data->device_connect_time = time_to_nsec(cmb.device_connect_time);
  653. data->function_pending_time = time_to_nsec(cmb.function_pending_time);
  654. data->device_disconnect_time = time_to_nsec(cmb.device_disconnect_time);
  655. data->control_unit_queuing_time
  656. = time_to_nsec(cmb.control_unit_queuing_time);
  657. data->device_active_only_time
  658. = time_to_nsec(cmb.device_active_only_time);
  659. data->device_busy_time = time_to_nsec(cmb.device_busy_time);
  660. data->initial_command_response_time
  661. = time_to_nsec(cmb.initial_command_response_time);
  662. return 0;
  663. }
  664. static void
  665. reset_cmbe(struct ccw_device *cdev)
  666. {
  667. struct cmbe *cmb;
  668. spin_lock_irq(cdev->ccwlock);
  669. cmb = cmbe_align(cdev->private->cmb);
  670. if (cmb)
  671. memset (cmb, 0, sizeof (*cmb));
  672. cdev->private->cmb_start_time = get_clock();
  673. spin_unlock_irq(cdev->ccwlock);
  674. }
  675. static struct attribute_group cmf_attr_group_ext;
  676. static struct cmb_operations cmbops_extended = {
  677. .alloc = alloc_cmbe,
  678. .free = free_cmbe,
  679. .set = set_cmbe,
  680. .read = read_cmbe,
  681. .readall = readall_cmbe,
  682. .reset = reset_cmbe,
  683. .attr_group = &cmf_attr_group_ext,
  684. };
  685. static ssize_t
  686. cmb_show_attr(struct device *dev, char *buf, enum cmb_index idx)
  687. {
  688. return sprintf(buf, "%lld\n",
  689. (unsigned long long) cmf_read(to_ccwdev(dev), idx));
  690. }
  691. static ssize_t
  692. cmb_show_avg_sample_interval(struct device *dev, struct device_attribute *attr, char *buf)
  693. {
  694. struct ccw_device *cdev;
  695. long interval;
  696. unsigned long count;
  697. cdev = to_ccwdev(dev);
  698. interval = get_clock() - cdev->private->cmb_start_time;
  699. count = cmf_read(cdev, cmb_sample_count);
  700. if (count)
  701. interval /= count;
  702. else
  703. interval = -1;
  704. return sprintf(buf, "%ld\n", interval);
  705. }
  706. static ssize_t
  707. cmb_show_avg_utilization(struct device *dev, struct device_attribute *attr, char *buf)
  708. {
  709. struct cmbdata data;
  710. u64 utilization;
  711. unsigned long t, u;
  712. int ret;
  713. ret = cmf_readall(to_ccwdev(dev), &data);
  714. if (ret)
  715. return ret;
  716. utilization = data.device_connect_time +
  717. data.function_pending_time +
  718. data.device_disconnect_time;
  719. /* shift to avoid long long division */
  720. while (-1ul < (data.elapsed_time | utilization)) {
  721. utilization >>= 8;
  722. data.elapsed_time >>= 8;
  723. }
  724. /* calculate value in 0.1 percent units */
  725. t = (unsigned long) data.elapsed_time / 1000;
  726. u = (unsigned long) utilization / t;
  727. return sprintf(buf, "%02ld.%01ld%%\n", u/ 10, u - (u/ 10) * 10);
  728. }
  729. #define cmf_attr(name) \
  730. static ssize_t show_ ## name (struct device * dev, struct device_attribute *attr, char * buf) \
  731. { return cmb_show_attr((dev), buf, cmb_ ## name); } \
  732. static DEVICE_ATTR(name, 0444, show_ ## name, NULL);
  733. #define cmf_attr_avg(name) \
  734. static ssize_t show_avg_ ## name (struct device * dev, struct device_attribute *attr, char * buf) \
  735. { return cmb_show_attr((dev), buf, cmb_ ## name); } \
  736. static DEVICE_ATTR(avg_ ## name, 0444, show_avg_ ## name, NULL);
  737. cmf_attr(ssch_rsch_count);
  738. cmf_attr(sample_count);
  739. cmf_attr_avg(device_connect_time);
  740. cmf_attr_avg(function_pending_time);
  741. cmf_attr_avg(device_disconnect_time);
  742. cmf_attr_avg(control_unit_queuing_time);
  743. cmf_attr_avg(device_active_only_time);
  744. cmf_attr_avg(device_busy_time);
  745. cmf_attr_avg(initial_command_response_time);
  746. static DEVICE_ATTR(avg_sample_interval, 0444, cmb_show_avg_sample_interval, NULL);
  747. static DEVICE_ATTR(avg_utilization, 0444, cmb_show_avg_utilization, NULL);
  748. static struct attribute *cmf_attributes[] = {
  749. &dev_attr_avg_sample_interval.attr,
  750. &dev_attr_avg_utilization.attr,
  751. &dev_attr_ssch_rsch_count.attr,
  752. &dev_attr_sample_count.attr,
  753. &dev_attr_avg_device_connect_time.attr,
  754. &dev_attr_avg_function_pending_time.attr,
  755. &dev_attr_avg_device_disconnect_time.attr,
  756. &dev_attr_avg_control_unit_queuing_time.attr,
  757. &dev_attr_avg_device_active_only_time.attr,
  758. 0,
  759. };
  760. static struct attribute_group cmf_attr_group = {
  761. .name = "cmf",
  762. .attrs = cmf_attributes,
  763. };
  764. static struct attribute *cmf_attributes_ext[] = {
  765. &dev_attr_avg_sample_interval.attr,
  766. &dev_attr_avg_utilization.attr,
  767. &dev_attr_ssch_rsch_count.attr,
  768. &dev_attr_sample_count.attr,
  769. &dev_attr_avg_device_connect_time.attr,
  770. &dev_attr_avg_function_pending_time.attr,
  771. &dev_attr_avg_device_disconnect_time.attr,
  772. &dev_attr_avg_control_unit_queuing_time.attr,
  773. &dev_attr_avg_device_active_only_time.attr,
  774. &dev_attr_avg_device_busy_time.attr,
  775. &dev_attr_avg_initial_command_response_time.attr,
  776. 0,
  777. };
  778. static struct attribute_group cmf_attr_group_ext = {
  779. .name = "cmf",
  780. .attrs = cmf_attributes_ext,
  781. };
  782. static ssize_t cmb_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
  783. {
  784. return sprintf(buf, "%d\n", to_ccwdev(dev)->private->cmb ? 1 : 0);
  785. }
  786. static ssize_t cmb_enable_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t c)
  787. {
  788. struct ccw_device *cdev;
  789. int ret;
  790. cdev = to_ccwdev(dev);
  791. switch (buf[0]) {
  792. case '0':
  793. ret = disable_cmf(cdev);
  794. if (ret)
  795. printk(KERN_INFO "disable_cmf failed (%d)\n", ret);
  796. break;
  797. case '1':
  798. ret = enable_cmf(cdev);
  799. if (ret && ret != -EBUSY)
  800. printk(KERN_INFO "enable_cmf failed (%d)\n", ret);
  801. break;
  802. }
  803. return c;
  804. }
  805. DEVICE_ATTR(cmb_enable, 0644, cmb_enable_show, cmb_enable_store);
  806. /* enable_cmf/disable_cmf: module interface for cmf (de)activation */
  807. int
  808. enable_cmf(struct ccw_device *cdev)
  809. {
  810. int ret;
  811. ret = cmbops->alloc(cdev);
  812. cmbops->reset(cdev);
  813. if (ret)
  814. return ret;
  815. ret = cmbops->set(cdev, 2);
  816. if (ret) {
  817. cmbops->free(cdev);
  818. return ret;
  819. }
  820. ret = sysfs_create_group(&cdev->dev.kobj, cmbops->attr_group);
  821. if (!ret)
  822. return 0;
  823. cmbops->set(cdev, 0); //FIXME: this can fail
  824. cmbops->free(cdev);
  825. return ret;
  826. }
  827. int
  828. disable_cmf(struct ccw_device *cdev)
  829. {
  830. int ret;
  831. ret = cmbops->set(cdev, 0);
  832. if (ret)
  833. return ret;
  834. cmbops->free(cdev);
  835. sysfs_remove_group(&cdev->dev.kobj, cmbops->attr_group);
  836. return ret;
  837. }
  838. u64
  839. cmf_read(struct ccw_device *cdev, int index)
  840. {
  841. return cmbops->read(cdev, index);
  842. }
  843. int
  844. cmf_readall(struct ccw_device *cdev, struct cmbdata *data)
  845. {
  846. return cmbops->readall(cdev, data);
  847. }
  848. static int __init
  849. init_cmf(void)
  850. {
  851. char *format_string;
  852. char *detect_string = "parameter";
  853. /* We cannot really autoprobe this. If the user did not give a parameter,
  854. see if we are running on z990 or up, otherwise fall back to basic mode. */
  855. if (format == CMF_AUTODETECT) {
  856. if (!css_characteristics_avail ||
  857. !css_general_characteristics.ext_mb) {
  858. format = CMF_BASIC;
  859. } else {
  860. format = CMF_EXTENDED;
  861. }
  862. detect_string = "autodetected";
  863. } else {
  864. detect_string = "parameter";
  865. }
  866. switch (format) {
  867. case CMF_BASIC:
  868. format_string = "basic";
  869. cmbops = &cmbops_basic;
  870. if (cmb_area.num_channels > 4096 || cmb_area.num_channels < 1) {
  871. printk(KERN_ERR "Basic channel measurement facility"
  872. " can only use 1 to 4096 devices\n"
  873. KERN_ERR "when the cmf driver is built"
  874. " as a loadable module\n");
  875. return 1;
  876. }
  877. break;
  878. case CMF_EXTENDED:
  879. format_string = "extended";
  880. cmbops = &cmbops_extended;
  881. break;
  882. default:
  883. printk(KERN_ERR "Invalid format %d for channel "
  884. "measurement facility\n", format);
  885. return 1;
  886. }
  887. printk(KERN_INFO "Channel measurement facility using %s format (%s)\n",
  888. format_string, detect_string);
  889. return 0;
  890. }
  891. module_init(init_cmf);
  892. MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
  893. MODULE_LICENSE("GPL");
  894. MODULE_DESCRIPTION("channel measurement facility base driver\n"
  895. "Copyright 2003 IBM Corporation\n");
  896. EXPORT_SYMBOL_GPL(enable_cmf);
  897. EXPORT_SYMBOL_GPL(disable_cmf);
  898. EXPORT_SYMBOL_GPL(cmf_read);
  899. EXPORT_SYMBOL_GPL(cmf_readall);