cmf.c 34 KB

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