cmf.c 34 KB

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