cmf.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Linux on zSeries Channel Measurement Facility support
  4. *
  5. * Copyright IBM Corp. 2000, 2006
  6. *
  7. * Authors: Arnd Bergmann <[email protected]>
  8. * Cornelia Huck <[email protected]>
  9. *
  10. * original idea from Natarajan Krishnaswami <[email protected]>
  11. */
  12. #define KMSG_COMPONENT "cio"
  13. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  14. #include <linux/memblock.h>
  15. #include <linux/device.h>
  16. #include <linux/init.h>
  17. #include <linux/list.h>
  18. #include <linux/export.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/slab.h>
  21. #include <linux/timex.h> /* get_tod_clock() */
  22. #include <asm/ccwdev.h>
  23. #include <asm/cio.h>
  24. #include <asm/cmb.h>
  25. #include <asm/div64.h>
  26. #include "cio.h"
  27. #include "css.h"
  28. #include "device.h"
  29. #include "ioasm.h"
  30. #include "chsc.h"
  31. /*
  32. * parameter to enable cmf during boot, possible uses are:
  33. * "s390cmf" -- enable cmf and allocate 2 MB of ram so measuring can be
  34. * used on any subchannel
  35. * "s390cmf=<num>" -- enable cmf and allocate enough memory to measure
  36. * <num> subchannel, where <num> is an integer
  37. * between 1 and 65535, default is 1024
  38. */
  39. #define ARGSTRING "s390cmf"
  40. /* indices for READCMB */
  41. enum cmb_index {
  42. avg_utilization = -1,
  43. /* basic and exended format: */
  44. cmb_ssch_rsch_count = 0,
  45. cmb_sample_count,
  46. cmb_device_connect_time,
  47. cmb_function_pending_time,
  48. cmb_device_disconnect_time,
  49. cmb_control_unit_queuing_time,
  50. cmb_device_active_only_time,
  51. /* extended format only: */
  52. cmb_device_busy_time,
  53. cmb_initial_command_response_time,
  54. };
  55. /**
  56. * enum cmb_format - types of supported measurement block formats
  57. *
  58. * @CMF_BASIC: traditional channel measurement blocks supported
  59. * by all machines that we run on
  60. * @CMF_EXTENDED: improved format that was introduced with the z990
  61. * machine
  62. * @CMF_AUTODETECT: default: use extended format when running on a machine
  63. * supporting extended format, otherwise fall back to
  64. * basic format
  65. */
  66. enum cmb_format {
  67. CMF_BASIC,
  68. CMF_EXTENDED,
  69. CMF_AUTODETECT = -1,
  70. };
  71. /*
  72. * format - actual format for all measurement blocks
  73. *
  74. * The format module parameter can be set to a value of 0 (zero)
  75. * or 1, indicating basic or extended format as described for
  76. * enum cmb_format.
  77. */
  78. static int format = CMF_AUTODETECT;
  79. module_param(format, bint, 0444);
  80. /**
  81. * struct cmb_operations - functions to use depending on cmb_format
  82. *
  83. * Most of these functions operate on a struct ccw_device. There is only
  84. * one instance of struct cmb_operations because the format of the measurement
  85. * data is guaranteed to be the same for every ccw_device.
  86. *
  87. * @alloc: allocate memory for a channel measurement block,
  88. * either with the help of a special pool or with kmalloc
  89. * @free: free memory allocated with @alloc
  90. * @set: enable or disable measurement
  91. * @read: read a measurement entry at an index
  92. * @readall: read a measurement block in a common format
  93. * @reset: clear the data in the associated measurement block and
  94. * reset its time stamp
  95. */
  96. struct cmb_operations {
  97. int (*alloc) (struct ccw_device *);
  98. void (*free) (struct ccw_device *);
  99. int (*set) (struct ccw_device *, u32);
  100. u64 (*read) (struct ccw_device *, int);
  101. int (*readall)(struct ccw_device *, struct cmbdata *);
  102. void (*reset) (struct ccw_device *);
  103. /* private: */
  104. struct attribute_group *attr_group;
  105. };
  106. static struct cmb_operations *cmbops;
  107. struct cmb_data {
  108. void *hw_block; /* Pointer to block updated by hardware */
  109. void *last_block; /* Last changed block copied from hardware block */
  110. int size; /* Size of hw_block and last_block */
  111. unsigned long long last_update; /* when last_block was updated */
  112. };
  113. /*
  114. * Our user interface is designed in terms of nanoseconds,
  115. * while the hardware measures total times in its own
  116. * unit.
  117. */
  118. static inline u64 time_to_nsec(u32 value)
  119. {
  120. return ((u64)value) * 128000ull;
  121. }
  122. /*
  123. * Users are usually interested in average times,
  124. * not accumulated time.
  125. * This also helps us with atomicity problems
  126. * when reading sinlge values.
  127. */
  128. static inline u64 time_to_avg_nsec(u32 value, u32 count)
  129. {
  130. u64 ret;
  131. /* no samples yet, avoid division by 0 */
  132. if (count == 0)
  133. return 0;
  134. /* value comes in units of 128 µsec */
  135. ret = time_to_nsec(value);
  136. do_div(ret, count);
  137. return ret;
  138. }
  139. #define CMF_OFF 0
  140. #define CMF_ON 2
  141. /*
  142. * Activate or deactivate the channel monitor. When area is NULL,
  143. * the monitor is deactivated. The channel monitor needs to
  144. * be active in order to measure subchannels, which also need
  145. * to be enabled.
  146. */
  147. static inline void cmf_activate(void *area, unsigned int onoff)
  148. {
  149. /* activate channel measurement */
  150. asm volatile(
  151. " lgr 1,%[r1]\n"
  152. " lgr 2,%[mbo]\n"
  153. " schm\n"
  154. :
  155. : [r1] "d" ((unsigned long)onoff), [mbo] "d" (area)
  156. : "1", "2");
  157. }
  158. static int set_schib(struct ccw_device *cdev, u32 mme, int mbfc,
  159. unsigned long address)
  160. {
  161. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  162. int ret;
  163. sch->config.mme = mme;
  164. sch->config.mbfc = mbfc;
  165. /* address can be either a block address or a block index */
  166. if (mbfc)
  167. sch->config.mba = address;
  168. else
  169. sch->config.mbi = address;
  170. ret = cio_commit_config(sch);
  171. if (!mme && ret == -ENODEV) {
  172. /*
  173. * The task was to disable measurement block updates but
  174. * the subchannel is already gone. Report success.
  175. */
  176. ret = 0;
  177. }
  178. return ret;
  179. }
  180. struct set_schib_struct {
  181. u32 mme;
  182. int mbfc;
  183. unsigned long address;
  184. wait_queue_head_t wait;
  185. int ret;
  186. };
  187. #define CMF_PENDING 1
  188. #define SET_SCHIB_TIMEOUT (10 * HZ)
  189. static int set_schib_wait(struct ccw_device *cdev, u32 mme,
  190. int mbfc, unsigned long address)
  191. {
  192. struct set_schib_struct set_data;
  193. int ret = -ENODEV;
  194. spin_lock_irq(cdev->ccwlock);
  195. if (!cdev->private->cmb)
  196. goto out;
  197. ret = set_schib(cdev, mme, mbfc, address);
  198. if (ret != -EBUSY)
  199. goto out;
  200. /* if the device is not online, don't even try again */
  201. if (cdev->private->state != DEV_STATE_ONLINE)
  202. goto out;
  203. init_waitqueue_head(&set_data.wait);
  204. set_data.mme = mme;
  205. set_data.mbfc = mbfc;
  206. set_data.address = address;
  207. set_data.ret = CMF_PENDING;
  208. cdev->private->state = DEV_STATE_CMFCHANGE;
  209. cdev->private->cmb_wait = &set_data;
  210. spin_unlock_irq(cdev->ccwlock);
  211. ret = wait_event_interruptible_timeout(set_data.wait,
  212. set_data.ret != CMF_PENDING,
  213. SET_SCHIB_TIMEOUT);
  214. spin_lock_irq(cdev->ccwlock);
  215. if (ret <= 0) {
  216. if (set_data.ret == CMF_PENDING) {
  217. set_data.ret = (ret == 0) ? -ETIME : ret;
  218. if (cdev->private->state == DEV_STATE_CMFCHANGE)
  219. cdev->private->state = DEV_STATE_ONLINE;
  220. }
  221. }
  222. cdev->private->cmb_wait = NULL;
  223. ret = set_data.ret;
  224. out:
  225. spin_unlock_irq(cdev->ccwlock);
  226. return ret;
  227. }
  228. void retry_set_schib(struct ccw_device *cdev)
  229. {
  230. struct set_schib_struct *set_data = cdev->private->cmb_wait;
  231. if (!set_data)
  232. return;
  233. set_data->ret = set_schib(cdev, set_data->mme, set_data->mbfc,
  234. set_data->address);
  235. wake_up(&set_data->wait);
  236. }
  237. static int cmf_copy_block(struct ccw_device *cdev)
  238. {
  239. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  240. struct cmb_data *cmb_data;
  241. void *hw_block;
  242. if (cio_update_schib(sch))
  243. return -ENODEV;
  244. if (scsw_fctl(&sch->schib.scsw) & SCSW_FCTL_START_FUNC) {
  245. /* Don't copy if a start function is in progress. */
  246. if ((!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_SUSPENDED)) &&
  247. (scsw_actl(&sch->schib.scsw) &
  248. (SCSW_ACTL_DEVACT | SCSW_ACTL_SCHACT)) &&
  249. (!(scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_SEC_STATUS)))
  250. return -EBUSY;
  251. }
  252. cmb_data = cdev->private->cmb;
  253. hw_block = cmb_data->hw_block;
  254. memcpy(cmb_data->last_block, hw_block, cmb_data->size);
  255. cmb_data->last_update = get_tod_clock();
  256. return 0;
  257. }
  258. struct copy_block_struct {
  259. wait_queue_head_t wait;
  260. int ret;
  261. };
  262. static int cmf_cmb_copy_wait(struct ccw_device *cdev)
  263. {
  264. struct copy_block_struct copy_block;
  265. int ret = -ENODEV;
  266. spin_lock_irq(cdev->ccwlock);
  267. if (!cdev->private->cmb)
  268. goto out;
  269. ret = cmf_copy_block(cdev);
  270. if (ret != -EBUSY)
  271. goto out;
  272. if (cdev->private->state != DEV_STATE_ONLINE)
  273. goto out;
  274. init_waitqueue_head(&copy_block.wait);
  275. copy_block.ret = CMF_PENDING;
  276. cdev->private->state = DEV_STATE_CMFUPDATE;
  277. cdev->private->cmb_wait = &copy_block;
  278. spin_unlock_irq(cdev->ccwlock);
  279. ret = wait_event_interruptible(copy_block.wait,
  280. copy_block.ret != CMF_PENDING);
  281. spin_lock_irq(cdev->ccwlock);
  282. if (ret) {
  283. if (copy_block.ret == CMF_PENDING) {
  284. copy_block.ret = -ERESTARTSYS;
  285. if (cdev->private->state == DEV_STATE_CMFUPDATE)
  286. cdev->private->state = DEV_STATE_ONLINE;
  287. }
  288. }
  289. cdev->private->cmb_wait = NULL;
  290. ret = copy_block.ret;
  291. out:
  292. spin_unlock_irq(cdev->ccwlock);
  293. return ret;
  294. }
  295. void cmf_retry_copy_block(struct ccw_device *cdev)
  296. {
  297. struct copy_block_struct *copy_block = cdev->private->cmb_wait;
  298. if (!copy_block)
  299. return;
  300. copy_block->ret = cmf_copy_block(cdev);
  301. wake_up(&copy_block->wait);
  302. }
  303. static void cmf_generic_reset(struct ccw_device *cdev)
  304. {
  305. struct cmb_data *cmb_data;
  306. spin_lock_irq(cdev->ccwlock);
  307. cmb_data = cdev->private->cmb;
  308. if (cmb_data) {
  309. memset(cmb_data->last_block, 0, cmb_data->size);
  310. /*
  311. * Need to reset hw block as well to make the hardware start
  312. * from 0 again.
  313. */
  314. memset(cmb_data->hw_block, 0, cmb_data->size);
  315. cmb_data->last_update = 0;
  316. }
  317. cdev->private->cmb_start_time = get_tod_clock();
  318. spin_unlock_irq(cdev->ccwlock);
  319. }
  320. /**
  321. * struct cmb_area - container for global cmb data
  322. *
  323. * @mem: pointer to CMBs (only in basic measurement mode)
  324. * @list: contains a linked list of all subchannels
  325. * @num_channels: number of channels to be measured
  326. * @lock: protect concurrent access to @mem and @list
  327. */
  328. struct cmb_area {
  329. struct cmb *mem;
  330. struct list_head list;
  331. int num_channels;
  332. spinlock_t lock;
  333. };
  334. static struct cmb_area cmb_area = {
  335. .lock = __SPIN_LOCK_UNLOCKED(cmb_area.lock),
  336. .list = LIST_HEAD_INIT(cmb_area.list),
  337. .num_channels = 1024,
  338. };
  339. /* ****** old style CMB handling ********/
  340. /*
  341. * Basic channel measurement blocks are allocated in one contiguous
  342. * block of memory, which can not be moved as long as any channel
  343. * is active. Therefore, a maximum number of subchannels needs to
  344. * be defined somewhere. This is a module parameter, defaulting to
  345. * a reasonable value of 1024, or 32 kb of memory.
  346. * Current kernels don't allow kmalloc with more than 128kb, so the
  347. * maximum is 4096.
  348. */
  349. module_param_named(maxchannels, cmb_area.num_channels, uint, 0444);
  350. /**
  351. * struct cmb - basic channel measurement block
  352. * @ssch_rsch_count: number of ssch and rsch
  353. * @sample_count: number of samples
  354. * @device_connect_time: time of device connect
  355. * @function_pending_time: time of function pending
  356. * @device_disconnect_time: time of device disconnect
  357. * @control_unit_queuing_time: time of control unit queuing
  358. * @device_active_only_time: time of device active only
  359. * @reserved: unused in basic measurement mode
  360. *
  361. * The measurement block as used by the hardware. The fields are described
  362. * further in z/Architecture Principles of Operation, chapter 17.
  363. *
  364. * The cmb area made up from these blocks must be a contiguous array and may
  365. * not be reallocated or freed.
  366. * Only one cmb area can be present in the system.
  367. */
  368. struct cmb {
  369. u16 ssch_rsch_count;
  370. u16 sample_count;
  371. u32 device_connect_time;
  372. u32 function_pending_time;
  373. u32 device_disconnect_time;
  374. u32 control_unit_queuing_time;
  375. u32 device_active_only_time;
  376. u32 reserved[2];
  377. };
  378. /*
  379. * Insert a single device into the cmb_area list.
  380. * Called with cmb_area.lock held from alloc_cmb.
  381. */
  382. static int alloc_cmb_single(struct ccw_device *cdev,
  383. struct cmb_data *cmb_data)
  384. {
  385. struct cmb *cmb;
  386. struct ccw_device_private *node;
  387. int ret;
  388. spin_lock_irq(cdev->ccwlock);
  389. if (!list_empty(&cdev->private->cmb_list)) {
  390. ret = -EBUSY;
  391. goto out;
  392. }
  393. /*
  394. * Find first unused cmb in cmb_area.mem.
  395. * This is a little tricky: cmb_area.list
  396. * remains sorted by ->cmb->hw_data pointers.
  397. */
  398. cmb = cmb_area.mem;
  399. list_for_each_entry(node, &cmb_area.list, cmb_list) {
  400. struct cmb_data *data;
  401. data = node->cmb;
  402. if ((struct cmb*)data->hw_block > cmb)
  403. break;
  404. cmb++;
  405. }
  406. if (cmb - cmb_area.mem >= cmb_area.num_channels) {
  407. ret = -ENOMEM;
  408. goto out;
  409. }
  410. /* insert new cmb */
  411. list_add_tail(&cdev->private->cmb_list, &node->cmb_list);
  412. cmb_data->hw_block = cmb;
  413. cdev->private->cmb = cmb_data;
  414. ret = 0;
  415. out:
  416. spin_unlock_irq(cdev->ccwlock);
  417. return ret;
  418. }
  419. static int alloc_cmb(struct ccw_device *cdev)
  420. {
  421. int ret;
  422. struct cmb *mem;
  423. ssize_t size;
  424. struct cmb_data *cmb_data;
  425. /* Allocate private cmb_data. */
  426. cmb_data = kzalloc(sizeof(struct cmb_data), GFP_KERNEL);
  427. if (!cmb_data)
  428. return -ENOMEM;
  429. cmb_data->last_block = kzalloc(sizeof(struct cmb), GFP_KERNEL);
  430. if (!cmb_data->last_block) {
  431. kfree(cmb_data);
  432. return -ENOMEM;
  433. }
  434. cmb_data->size = sizeof(struct cmb);
  435. spin_lock(&cmb_area.lock);
  436. if (!cmb_area.mem) {
  437. /* there is no user yet, so we need a new area */
  438. size = sizeof(struct cmb) * cmb_area.num_channels;
  439. WARN_ON(!list_empty(&cmb_area.list));
  440. spin_unlock(&cmb_area.lock);
  441. mem = (void*)__get_free_pages(GFP_KERNEL | GFP_DMA,
  442. get_order(size));
  443. spin_lock(&cmb_area.lock);
  444. if (cmb_area.mem) {
  445. /* ok, another thread was faster */
  446. free_pages((unsigned long)mem, get_order(size));
  447. } else if (!mem) {
  448. /* no luck */
  449. ret = -ENOMEM;
  450. goto out;
  451. } else {
  452. /* everything ok */
  453. memset(mem, 0, size);
  454. cmb_area.mem = mem;
  455. cmf_activate(cmb_area.mem, CMF_ON);
  456. }
  457. }
  458. /* do the actual allocation */
  459. ret = alloc_cmb_single(cdev, cmb_data);
  460. out:
  461. spin_unlock(&cmb_area.lock);
  462. if (ret) {
  463. kfree(cmb_data->last_block);
  464. kfree(cmb_data);
  465. }
  466. return ret;
  467. }
  468. static void free_cmb(struct ccw_device *cdev)
  469. {
  470. struct ccw_device_private *priv;
  471. struct cmb_data *cmb_data;
  472. spin_lock(&cmb_area.lock);
  473. spin_lock_irq(cdev->ccwlock);
  474. priv = cdev->private;
  475. cmb_data = priv->cmb;
  476. priv->cmb = NULL;
  477. if (cmb_data)
  478. kfree(cmb_data->last_block);
  479. kfree(cmb_data);
  480. list_del_init(&priv->cmb_list);
  481. if (list_empty(&cmb_area.list)) {
  482. ssize_t size;
  483. size = sizeof(struct cmb) * cmb_area.num_channels;
  484. cmf_activate(NULL, CMF_OFF);
  485. free_pages((unsigned long)cmb_area.mem, get_order(size));
  486. cmb_area.mem = NULL;
  487. }
  488. spin_unlock_irq(cdev->ccwlock);
  489. spin_unlock(&cmb_area.lock);
  490. }
  491. static int set_cmb(struct ccw_device *cdev, u32 mme)
  492. {
  493. u16 offset;
  494. struct cmb_data *cmb_data;
  495. unsigned long flags;
  496. spin_lock_irqsave(cdev->ccwlock, flags);
  497. if (!cdev->private->cmb) {
  498. spin_unlock_irqrestore(cdev->ccwlock, flags);
  499. return -EINVAL;
  500. }
  501. cmb_data = cdev->private->cmb;
  502. offset = mme ? (struct cmb *)cmb_data->hw_block - cmb_area.mem : 0;
  503. spin_unlock_irqrestore(cdev->ccwlock, flags);
  504. return set_schib_wait(cdev, mme, 0, offset);
  505. }
  506. /* calculate utilization in 0.1 percent units */
  507. static u64 __cmb_utilization(u64 device_connect_time, u64 function_pending_time,
  508. u64 device_disconnect_time, u64 start_time)
  509. {
  510. u64 utilization, elapsed_time;
  511. utilization = time_to_nsec(device_connect_time +
  512. function_pending_time +
  513. device_disconnect_time);
  514. elapsed_time = get_tod_clock() - start_time;
  515. elapsed_time = tod_to_ns(elapsed_time);
  516. elapsed_time /= 1000;
  517. return elapsed_time ? (utilization / elapsed_time) : 0;
  518. }
  519. static u64 read_cmb(struct ccw_device *cdev, int index)
  520. {
  521. struct cmb_data *cmb_data;
  522. unsigned long flags;
  523. struct cmb *cmb;
  524. u64 ret = 0;
  525. u32 val;
  526. spin_lock_irqsave(cdev->ccwlock, flags);
  527. cmb_data = cdev->private->cmb;
  528. if (!cmb_data)
  529. goto out;
  530. cmb = cmb_data->hw_block;
  531. switch (index) {
  532. case avg_utilization:
  533. ret = __cmb_utilization(cmb->device_connect_time,
  534. cmb->function_pending_time,
  535. cmb->device_disconnect_time,
  536. cdev->private->cmb_start_time);
  537. goto out;
  538. case cmb_ssch_rsch_count:
  539. ret = cmb->ssch_rsch_count;
  540. goto out;
  541. case cmb_sample_count:
  542. ret = cmb->sample_count;
  543. goto out;
  544. case cmb_device_connect_time:
  545. val = cmb->device_connect_time;
  546. break;
  547. case cmb_function_pending_time:
  548. val = cmb->function_pending_time;
  549. break;
  550. case cmb_device_disconnect_time:
  551. val = cmb->device_disconnect_time;
  552. break;
  553. case cmb_control_unit_queuing_time:
  554. val = cmb->control_unit_queuing_time;
  555. break;
  556. case cmb_device_active_only_time:
  557. val = cmb->device_active_only_time;
  558. break;
  559. default:
  560. goto out;
  561. }
  562. ret = time_to_avg_nsec(val, cmb->sample_count);
  563. out:
  564. spin_unlock_irqrestore(cdev->ccwlock, flags);
  565. return ret;
  566. }
  567. static int readall_cmb(struct ccw_device *cdev, struct cmbdata *data)
  568. {
  569. struct cmb *cmb;
  570. struct cmb_data *cmb_data;
  571. u64 time;
  572. unsigned long flags;
  573. int ret;
  574. ret = cmf_cmb_copy_wait(cdev);
  575. if (ret < 0)
  576. return ret;
  577. spin_lock_irqsave(cdev->ccwlock, flags);
  578. cmb_data = cdev->private->cmb;
  579. if (!cmb_data) {
  580. ret = -ENODEV;
  581. goto out;
  582. }
  583. if (cmb_data->last_update == 0) {
  584. ret = -EAGAIN;
  585. goto out;
  586. }
  587. cmb = cmb_data->last_block;
  588. time = cmb_data->last_update - cdev->private->cmb_start_time;
  589. memset(data, 0, sizeof(struct cmbdata));
  590. /* we only know values before device_busy_time */
  591. data->size = offsetof(struct cmbdata, device_busy_time);
  592. data->elapsed_time = tod_to_ns(time);
  593. /* copy data to new structure */
  594. data->ssch_rsch_count = cmb->ssch_rsch_count;
  595. data->sample_count = cmb->sample_count;
  596. /* time fields are converted to nanoseconds while copying */
  597. data->device_connect_time = time_to_nsec(cmb->device_connect_time);
  598. data->function_pending_time = time_to_nsec(cmb->function_pending_time);
  599. data->device_disconnect_time =
  600. time_to_nsec(cmb->device_disconnect_time);
  601. data->control_unit_queuing_time
  602. = time_to_nsec(cmb->control_unit_queuing_time);
  603. data->device_active_only_time
  604. = time_to_nsec(cmb->device_active_only_time);
  605. ret = 0;
  606. out:
  607. spin_unlock_irqrestore(cdev->ccwlock, flags);
  608. return ret;
  609. }
  610. static void reset_cmb(struct ccw_device *cdev)
  611. {
  612. cmf_generic_reset(cdev);
  613. }
  614. static int cmf_enabled(struct ccw_device *cdev)
  615. {
  616. int enabled;
  617. spin_lock_irq(cdev->ccwlock);
  618. enabled = !!cdev->private->cmb;
  619. spin_unlock_irq(cdev->ccwlock);
  620. return enabled;
  621. }
  622. static struct attribute_group cmf_attr_group;
  623. static struct cmb_operations cmbops_basic = {
  624. .alloc = alloc_cmb,
  625. .free = free_cmb,
  626. .set = set_cmb,
  627. .read = read_cmb,
  628. .readall = readall_cmb,
  629. .reset = reset_cmb,
  630. .attr_group = &cmf_attr_group,
  631. };
  632. /* ******** extended cmb handling ********/
  633. /**
  634. * struct cmbe - extended channel measurement block
  635. * @ssch_rsch_count: number of ssch and rsch
  636. * @sample_count: number of samples
  637. * @device_connect_time: time of device connect
  638. * @function_pending_time: time of function pending
  639. * @device_disconnect_time: time of device disconnect
  640. * @control_unit_queuing_time: time of control unit queuing
  641. * @device_active_only_time: time of device active only
  642. * @device_busy_time: time of device busy
  643. * @initial_command_response_time: initial command response time
  644. * @reserved: unused
  645. *
  646. * The measurement block as used by the hardware. May be in any 64 bit physical
  647. * location.
  648. * The fields are described further in z/Architecture Principles of Operation,
  649. * third edition, chapter 17.
  650. */
  651. struct cmbe {
  652. u32 ssch_rsch_count;
  653. u32 sample_count;
  654. u32 device_connect_time;
  655. u32 function_pending_time;
  656. u32 device_disconnect_time;
  657. u32 control_unit_queuing_time;
  658. u32 device_active_only_time;
  659. u32 device_busy_time;
  660. u32 initial_command_response_time;
  661. u32 reserved[7];
  662. } __packed __aligned(64);
  663. static struct kmem_cache *cmbe_cache;
  664. static int alloc_cmbe(struct ccw_device *cdev)
  665. {
  666. struct cmb_data *cmb_data;
  667. struct cmbe *cmbe;
  668. int ret = -ENOMEM;
  669. cmbe = kmem_cache_zalloc(cmbe_cache, GFP_KERNEL);
  670. if (!cmbe)
  671. return ret;
  672. cmb_data = kzalloc(sizeof(*cmb_data), GFP_KERNEL);
  673. if (!cmb_data)
  674. goto out_free;
  675. cmb_data->last_block = kzalloc(sizeof(struct cmbe), GFP_KERNEL);
  676. if (!cmb_data->last_block)
  677. goto out_free;
  678. cmb_data->size = sizeof(*cmbe);
  679. cmb_data->hw_block = cmbe;
  680. spin_lock(&cmb_area.lock);
  681. spin_lock_irq(cdev->ccwlock);
  682. if (cdev->private->cmb)
  683. goto out_unlock;
  684. cdev->private->cmb = cmb_data;
  685. /* activate global measurement if this is the first channel */
  686. if (list_empty(&cmb_area.list))
  687. cmf_activate(NULL, CMF_ON);
  688. list_add_tail(&cdev->private->cmb_list, &cmb_area.list);
  689. spin_unlock_irq(cdev->ccwlock);
  690. spin_unlock(&cmb_area.lock);
  691. return 0;
  692. out_unlock:
  693. spin_unlock_irq(cdev->ccwlock);
  694. spin_unlock(&cmb_area.lock);
  695. ret = -EBUSY;
  696. out_free:
  697. if (cmb_data)
  698. kfree(cmb_data->last_block);
  699. kfree(cmb_data);
  700. kmem_cache_free(cmbe_cache, cmbe);
  701. return ret;
  702. }
  703. static void free_cmbe(struct ccw_device *cdev)
  704. {
  705. struct cmb_data *cmb_data;
  706. spin_lock(&cmb_area.lock);
  707. spin_lock_irq(cdev->ccwlock);
  708. cmb_data = cdev->private->cmb;
  709. cdev->private->cmb = NULL;
  710. if (cmb_data) {
  711. kfree(cmb_data->last_block);
  712. kmem_cache_free(cmbe_cache, cmb_data->hw_block);
  713. }
  714. kfree(cmb_data);
  715. /* deactivate global measurement if this is the last channel */
  716. list_del_init(&cdev->private->cmb_list);
  717. if (list_empty(&cmb_area.list))
  718. cmf_activate(NULL, CMF_OFF);
  719. spin_unlock_irq(cdev->ccwlock);
  720. spin_unlock(&cmb_area.lock);
  721. }
  722. static int set_cmbe(struct ccw_device *cdev, u32 mme)
  723. {
  724. unsigned long mba;
  725. struct cmb_data *cmb_data;
  726. unsigned long flags;
  727. spin_lock_irqsave(cdev->ccwlock, flags);
  728. if (!cdev->private->cmb) {
  729. spin_unlock_irqrestore(cdev->ccwlock, flags);
  730. return -EINVAL;
  731. }
  732. cmb_data = cdev->private->cmb;
  733. mba = mme ? (unsigned long) cmb_data->hw_block : 0;
  734. spin_unlock_irqrestore(cdev->ccwlock, flags);
  735. return set_schib_wait(cdev, mme, 1, mba);
  736. }
  737. static u64 read_cmbe(struct ccw_device *cdev, int index)
  738. {
  739. struct cmb_data *cmb_data;
  740. unsigned long flags;
  741. struct cmbe *cmb;
  742. u64 ret = 0;
  743. u32 val;
  744. spin_lock_irqsave(cdev->ccwlock, flags);
  745. cmb_data = cdev->private->cmb;
  746. if (!cmb_data)
  747. goto out;
  748. cmb = cmb_data->hw_block;
  749. switch (index) {
  750. case avg_utilization:
  751. ret = __cmb_utilization(cmb->device_connect_time,
  752. cmb->function_pending_time,
  753. cmb->device_disconnect_time,
  754. cdev->private->cmb_start_time);
  755. goto out;
  756. case cmb_ssch_rsch_count:
  757. ret = cmb->ssch_rsch_count;
  758. goto out;
  759. case cmb_sample_count:
  760. ret = cmb->sample_count;
  761. goto out;
  762. case cmb_device_connect_time:
  763. val = cmb->device_connect_time;
  764. break;
  765. case cmb_function_pending_time:
  766. val = cmb->function_pending_time;
  767. break;
  768. case cmb_device_disconnect_time:
  769. val = cmb->device_disconnect_time;
  770. break;
  771. case cmb_control_unit_queuing_time:
  772. val = cmb->control_unit_queuing_time;
  773. break;
  774. case cmb_device_active_only_time:
  775. val = cmb->device_active_only_time;
  776. break;
  777. case cmb_device_busy_time:
  778. val = cmb->device_busy_time;
  779. break;
  780. case cmb_initial_command_response_time:
  781. val = cmb->initial_command_response_time;
  782. break;
  783. default:
  784. goto out;
  785. }
  786. ret = time_to_avg_nsec(val, cmb->sample_count);
  787. out:
  788. spin_unlock_irqrestore(cdev->ccwlock, flags);
  789. return ret;
  790. }
  791. static int readall_cmbe(struct ccw_device *cdev, struct cmbdata *data)
  792. {
  793. struct cmbe *cmb;
  794. struct cmb_data *cmb_data;
  795. u64 time;
  796. unsigned long flags;
  797. int ret;
  798. ret = cmf_cmb_copy_wait(cdev);
  799. if (ret < 0)
  800. return ret;
  801. spin_lock_irqsave(cdev->ccwlock, flags);
  802. cmb_data = cdev->private->cmb;
  803. if (!cmb_data) {
  804. ret = -ENODEV;
  805. goto out;
  806. }
  807. if (cmb_data->last_update == 0) {
  808. ret = -EAGAIN;
  809. goto out;
  810. }
  811. time = cmb_data->last_update - cdev->private->cmb_start_time;
  812. memset (data, 0, sizeof(struct cmbdata));
  813. /* we only know values before device_busy_time */
  814. data->size = offsetof(struct cmbdata, device_busy_time);
  815. data->elapsed_time = tod_to_ns(time);
  816. cmb = cmb_data->last_block;
  817. /* copy data to new structure */
  818. data->ssch_rsch_count = cmb->ssch_rsch_count;
  819. data->sample_count = cmb->sample_count;
  820. /* time fields are converted to nanoseconds while copying */
  821. data->device_connect_time = time_to_nsec(cmb->device_connect_time);
  822. data->function_pending_time = time_to_nsec(cmb->function_pending_time);
  823. data->device_disconnect_time =
  824. time_to_nsec(cmb->device_disconnect_time);
  825. data->control_unit_queuing_time
  826. = time_to_nsec(cmb->control_unit_queuing_time);
  827. data->device_active_only_time
  828. = time_to_nsec(cmb->device_active_only_time);
  829. data->device_busy_time = time_to_nsec(cmb->device_busy_time);
  830. data->initial_command_response_time
  831. = time_to_nsec(cmb->initial_command_response_time);
  832. ret = 0;
  833. out:
  834. spin_unlock_irqrestore(cdev->ccwlock, flags);
  835. return ret;
  836. }
  837. static void reset_cmbe(struct ccw_device *cdev)
  838. {
  839. cmf_generic_reset(cdev);
  840. }
  841. static struct attribute_group cmf_attr_group_ext;
  842. static struct cmb_operations cmbops_extended = {
  843. .alloc = alloc_cmbe,
  844. .free = free_cmbe,
  845. .set = set_cmbe,
  846. .read = read_cmbe,
  847. .readall = readall_cmbe,
  848. .reset = reset_cmbe,
  849. .attr_group = &cmf_attr_group_ext,
  850. };
  851. static ssize_t cmb_show_attr(struct device *dev, char *buf, enum cmb_index idx)
  852. {
  853. return sprintf(buf, "%lld\n",
  854. (unsigned long long) cmf_read(to_ccwdev(dev), idx));
  855. }
  856. static ssize_t cmb_show_avg_sample_interval(struct device *dev,
  857. struct device_attribute *attr,
  858. char *buf)
  859. {
  860. struct ccw_device *cdev = to_ccwdev(dev);
  861. unsigned long count;
  862. long interval;
  863. count = cmf_read(cdev, cmb_sample_count);
  864. spin_lock_irq(cdev->ccwlock);
  865. if (count) {
  866. interval = get_tod_clock() - cdev->private->cmb_start_time;
  867. interval = tod_to_ns(interval);
  868. interval /= count;
  869. } else
  870. interval = -1;
  871. spin_unlock_irq(cdev->ccwlock);
  872. return sprintf(buf, "%ld\n", interval);
  873. }
  874. static ssize_t cmb_show_avg_utilization(struct device *dev,
  875. struct device_attribute *attr,
  876. char *buf)
  877. {
  878. unsigned long u = cmf_read(to_ccwdev(dev), avg_utilization);
  879. return sprintf(buf, "%02lu.%01lu%%\n", u / 10, u % 10);
  880. }
  881. #define cmf_attr(name) \
  882. static ssize_t show_##name(struct device *dev, \
  883. struct device_attribute *attr, char *buf) \
  884. { return cmb_show_attr((dev), buf, cmb_##name); } \
  885. static DEVICE_ATTR(name, 0444, show_##name, NULL);
  886. #define cmf_attr_avg(name) \
  887. static ssize_t show_avg_##name(struct device *dev, \
  888. struct device_attribute *attr, char *buf) \
  889. { return cmb_show_attr((dev), buf, cmb_##name); } \
  890. static DEVICE_ATTR(avg_##name, 0444, show_avg_##name, NULL);
  891. cmf_attr(ssch_rsch_count);
  892. cmf_attr(sample_count);
  893. cmf_attr_avg(device_connect_time);
  894. cmf_attr_avg(function_pending_time);
  895. cmf_attr_avg(device_disconnect_time);
  896. cmf_attr_avg(control_unit_queuing_time);
  897. cmf_attr_avg(device_active_only_time);
  898. cmf_attr_avg(device_busy_time);
  899. cmf_attr_avg(initial_command_response_time);
  900. static DEVICE_ATTR(avg_sample_interval, 0444, cmb_show_avg_sample_interval,
  901. NULL);
  902. static DEVICE_ATTR(avg_utilization, 0444, cmb_show_avg_utilization, NULL);
  903. static struct attribute *cmf_attributes[] = {
  904. &dev_attr_avg_sample_interval.attr,
  905. &dev_attr_avg_utilization.attr,
  906. &dev_attr_ssch_rsch_count.attr,
  907. &dev_attr_sample_count.attr,
  908. &dev_attr_avg_device_connect_time.attr,
  909. &dev_attr_avg_function_pending_time.attr,
  910. &dev_attr_avg_device_disconnect_time.attr,
  911. &dev_attr_avg_control_unit_queuing_time.attr,
  912. &dev_attr_avg_device_active_only_time.attr,
  913. NULL,
  914. };
  915. static struct attribute_group cmf_attr_group = {
  916. .name = "cmf",
  917. .attrs = cmf_attributes,
  918. };
  919. static struct attribute *cmf_attributes_ext[] = {
  920. &dev_attr_avg_sample_interval.attr,
  921. &dev_attr_avg_utilization.attr,
  922. &dev_attr_ssch_rsch_count.attr,
  923. &dev_attr_sample_count.attr,
  924. &dev_attr_avg_device_connect_time.attr,
  925. &dev_attr_avg_function_pending_time.attr,
  926. &dev_attr_avg_device_disconnect_time.attr,
  927. &dev_attr_avg_control_unit_queuing_time.attr,
  928. &dev_attr_avg_device_active_only_time.attr,
  929. &dev_attr_avg_device_busy_time.attr,
  930. &dev_attr_avg_initial_command_response_time.attr,
  931. NULL,
  932. };
  933. static struct attribute_group cmf_attr_group_ext = {
  934. .name = "cmf",
  935. .attrs = cmf_attributes_ext,
  936. };
  937. static ssize_t cmb_enable_show(struct device *dev,
  938. struct device_attribute *attr,
  939. char *buf)
  940. {
  941. struct ccw_device *cdev = to_ccwdev(dev);
  942. return sprintf(buf, "%d\n", cmf_enabled(cdev));
  943. }
  944. static ssize_t cmb_enable_store(struct device *dev,
  945. struct device_attribute *attr, const char *buf,
  946. size_t c)
  947. {
  948. struct ccw_device *cdev = to_ccwdev(dev);
  949. unsigned long val;
  950. int ret;
  951. ret = kstrtoul(buf, 16, &val);
  952. if (ret)
  953. return ret;
  954. switch (val) {
  955. case 0:
  956. ret = disable_cmf(cdev);
  957. break;
  958. case 1:
  959. ret = enable_cmf(cdev);
  960. break;
  961. default:
  962. ret = -EINVAL;
  963. }
  964. return ret ? ret : c;
  965. }
  966. DEVICE_ATTR_RW(cmb_enable);
  967. /**
  968. * enable_cmf() - switch on the channel measurement for a specific device
  969. * @cdev: The ccw device to be enabled
  970. *
  971. * Enable channel measurements for @cdev. If this is called on a device
  972. * for which channel measurement is already enabled a reset of the
  973. * measurement data is triggered.
  974. * Returns: %0 for success or a negative error value.
  975. * Context:
  976. * non-atomic
  977. */
  978. int enable_cmf(struct ccw_device *cdev)
  979. {
  980. int ret = 0;
  981. device_lock(&cdev->dev);
  982. if (cmf_enabled(cdev)) {
  983. cmbops->reset(cdev);
  984. goto out_unlock;
  985. }
  986. get_device(&cdev->dev);
  987. ret = cmbops->alloc(cdev);
  988. if (ret)
  989. goto out;
  990. cmbops->reset(cdev);
  991. ret = sysfs_create_group(&cdev->dev.kobj, cmbops->attr_group);
  992. if (ret) {
  993. cmbops->free(cdev);
  994. goto out;
  995. }
  996. ret = cmbops->set(cdev, 2);
  997. if (ret) {
  998. sysfs_remove_group(&cdev->dev.kobj, cmbops->attr_group);
  999. cmbops->free(cdev);
  1000. }
  1001. out:
  1002. if (ret)
  1003. put_device(&cdev->dev);
  1004. out_unlock:
  1005. device_unlock(&cdev->dev);
  1006. return ret;
  1007. }
  1008. /**
  1009. * __disable_cmf() - switch off the channel measurement for a specific device
  1010. * @cdev: The ccw device to be disabled
  1011. *
  1012. * Returns: %0 for success or a negative error value.
  1013. *
  1014. * Context:
  1015. * non-atomic, device_lock() held.
  1016. */
  1017. int __disable_cmf(struct ccw_device *cdev)
  1018. {
  1019. int ret;
  1020. ret = cmbops->set(cdev, 0);
  1021. if (ret)
  1022. return ret;
  1023. sysfs_remove_group(&cdev->dev.kobj, cmbops->attr_group);
  1024. cmbops->free(cdev);
  1025. put_device(&cdev->dev);
  1026. return ret;
  1027. }
  1028. /**
  1029. * disable_cmf() - switch off the channel measurement for a specific device
  1030. * @cdev: The ccw device to be disabled
  1031. *
  1032. * Returns: %0 for success or a negative error value.
  1033. *
  1034. * Context:
  1035. * non-atomic
  1036. */
  1037. int disable_cmf(struct ccw_device *cdev)
  1038. {
  1039. int ret;
  1040. device_lock(&cdev->dev);
  1041. ret = __disable_cmf(cdev);
  1042. device_unlock(&cdev->dev);
  1043. return ret;
  1044. }
  1045. /**
  1046. * cmf_read() - read one value from the current channel measurement block
  1047. * @cdev: the channel to be read
  1048. * @index: the index of the value to be read
  1049. *
  1050. * Returns: The value read or %0 if the value cannot be read.
  1051. *
  1052. * Context:
  1053. * any
  1054. */
  1055. u64 cmf_read(struct ccw_device *cdev, int index)
  1056. {
  1057. return cmbops->read(cdev, index);
  1058. }
  1059. /**
  1060. * cmf_readall() - read the current channel measurement block
  1061. * @cdev: the channel to be read
  1062. * @data: a pointer to a data block that will be filled
  1063. *
  1064. * Returns: %0 on success, a negative error value otherwise.
  1065. *
  1066. * Context:
  1067. * any
  1068. */
  1069. int cmf_readall(struct ccw_device *cdev, struct cmbdata *data)
  1070. {
  1071. return cmbops->readall(cdev, data);
  1072. }
  1073. /* Reenable cmf when a disconnected device becomes available again. */
  1074. int cmf_reenable(struct ccw_device *cdev)
  1075. {
  1076. cmbops->reset(cdev);
  1077. return cmbops->set(cdev, 2);
  1078. }
  1079. /**
  1080. * cmf_reactivate() - reactivate measurement block updates
  1081. *
  1082. * Use this during resume from hibernate.
  1083. */
  1084. void cmf_reactivate(void)
  1085. {
  1086. spin_lock(&cmb_area.lock);
  1087. if (!list_empty(&cmb_area.list))
  1088. cmf_activate(cmb_area.mem, CMF_ON);
  1089. spin_unlock(&cmb_area.lock);
  1090. }
  1091. static int __init init_cmbe(void)
  1092. {
  1093. cmbe_cache = kmem_cache_create("cmbe_cache", sizeof(struct cmbe),
  1094. __alignof__(struct cmbe), 0, NULL);
  1095. return cmbe_cache ? 0 : -ENOMEM;
  1096. }
  1097. static int __init init_cmf(void)
  1098. {
  1099. char *format_string;
  1100. char *detect_string;
  1101. int ret;
  1102. /*
  1103. * If the user did not give a parameter, see if we are running on a
  1104. * machine supporting extended measurement blocks, otherwise fall back
  1105. * to basic mode.
  1106. */
  1107. if (format == CMF_AUTODETECT) {
  1108. if (!css_general_characteristics.ext_mb) {
  1109. format = CMF_BASIC;
  1110. } else {
  1111. format = CMF_EXTENDED;
  1112. }
  1113. detect_string = "autodetected";
  1114. } else {
  1115. detect_string = "parameter";
  1116. }
  1117. switch (format) {
  1118. case CMF_BASIC:
  1119. format_string = "basic";
  1120. cmbops = &cmbops_basic;
  1121. break;
  1122. case CMF_EXTENDED:
  1123. format_string = "extended";
  1124. cmbops = &cmbops_extended;
  1125. ret = init_cmbe();
  1126. if (ret)
  1127. return ret;
  1128. break;
  1129. default:
  1130. return -EINVAL;
  1131. }
  1132. pr_info("Channel measurement facility initialized using format "
  1133. "%s (mode %s)\n", format_string, detect_string);
  1134. return 0;
  1135. }
  1136. device_initcall(init_cmf);
  1137. EXPORT_SYMBOL_GPL(enable_cmf);
  1138. EXPORT_SYMBOL_GPL(disable_cmf);
  1139. EXPORT_SYMBOL_GPL(cmf_read);
  1140. EXPORT_SYMBOL_GPL(cmf_readall);