Merge with master.kernel.org:/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
[powerpc.git] / drivers / message / i2o / device.c
1 /*
2  *      Functions to handle I2O devices
3  *
4  *      Copyright (C) 2004      Markus Lidel <Markus.Lidel@shadowconnect.com>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation; either version 2 of the License, or (at your
9  *      option) any later version.
10  *
11  *      Fixes/additions:
12  *              Markus Lidel <Markus.Lidel@shadowconnect.com>
13  *                      initial version.
14  */
15
16 #include <linux/module.h>
17 #include <linux/i2o.h>
18 #include <linux/delay.h>
19 #include "core.h"
20
21 /**
22  *      i2o_device_issue_claim - claim or release a device
23  *      @dev: I2O device to claim or release
24  *      @cmd: claim or release command
25  *      @type: type of claim
26  *
27  *      Issue I2O UTIL_CLAIM or UTIL_RELEASE messages. The message to be sent
28  *      is set by cmd. dev is the I2O device which should be claim or
29  *      released and the type is the claim type (see the I2O spec).
30  *
31  *      Returs 0 on success or negative error code on failure.
32  */
33 static inline int i2o_device_issue_claim(struct i2o_device *dev, u32 cmd,
34                                          u32 type)
35 {
36         struct i2o_message __iomem *msg;
37         u32 m;
38
39         m = i2o_msg_get_wait(dev->iop, &msg, I2O_TIMEOUT_MESSAGE_GET);
40         if (m == I2O_QUEUE_EMPTY)
41                 return -ETIMEDOUT;
42
43         writel(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
44         writel(cmd << 24 | HOST_TID << 12 | dev->lct_data.tid, &msg->u.head[1]);
45         writel(type, &msg->body[0]);
46
47         return i2o_msg_post_wait(dev->iop, m, 60);
48 };
49
50 /**
51  *      i2o_device_claim - claim a device for use by an OSM
52  *      @dev: I2O device to claim
53  *      @drv: I2O driver which wants to claim the device
54  *
55  *      Do the leg work to assign a device to a given OSM. If the claim succeed
56  *      the owner of the rimary. If the attempt fails a negative errno code
57  *      is returned. On success zero is returned.
58  */
59 int i2o_device_claim(struct i2o_device *dev)
60 {
61         int rc = 0;
62
63         down(&dev->lock);
64
65         rc = i2o_device_issue_claim(dev, I2O_CMD_UTIL_CLAIM, I2O_CLAIM_PRIMARY);
66         if (!rc)
67                 pr_debug("i2o: claim of device %d succeded\n",
68                          dev->lct_data.tid);
69         else
70                 pr_debug("i2o: claim of device %d failed %d\n",
71                          dev->lct_data.tid, rc);
72
73         up(&dev->lock);
74
75         return rc;
76 };
77
78 /**
79  *      i2o_device_claim_release - release a device that the OSM is using
80  *      @dev: device to release
81  *      @drv: driver which claimed the device
82  *
83  *      Drop a claim by an OSM on a given I2O device.
84  *
85  *      AC - some devices seem to want to refuse an unclaim until they have
86  *      finished internal processing. It makes sense since you don't want a
87  *      new device to go reconfiguring the entire system until you are done.
88  *      Thus we are prepared to wait briefly.
89  *
90  *      Returns 0 on success or negative error code on failure.
91  */
92 int i2o_device_claim_release(struct i2o_device *dev)
93 {
94         int tries;
95         int rc = 0;
96
97         down(&dev->lock);
98
99         /*
100          *      If the controller takes a nonblocking approach to
101          *      releases we have to sleep/poll for a few times.
102          */
103         for (tries = 0; tries < 10; tries++) {
104                 rc = i2o_device_issue_claim(dev, I2O_CMD_UTIL_RELEASE,
105                                             I2O_CLAIM_PRIMARY);
106                 if (!rc)
107                         break;
108
109                 ssleep(1);
110         }
111
112         if (!rc)
113                 pr_debug("i2o: claim release of device %d succeded\n",
114                          dev->lct_data.tid);
115         else
116                 pr_debug("i2o: claim release of device %d failed %d\n",
117                          dev->lct_data.tid, rc);
118
119         up(&dev->lock);
120
121         return rc;
122 };
123
124 /**
125  *      i2o_device_release - release the memory for a I2O device
126  *      @dev: I2O device which should be released
127  *
128  *      Release the allocated memory. This function is called if refcount of
129  *      device reaches 0 automatically.
130  */
131 static void i2o_device_release(struct device *dev)
132 {
133         struct i2o_device *i2o_dev = to_i2o_device(dev);
134
135         pr_debug("i2o: device %s released\n", dev->bus_id);
136
137         kfree(i2o_dev);
138 };
139
140 /**
141  *      i2o_device_class_release - Remove I2O device attributes
142  *      @cd: I2O class device which is added to the I2O device class
143  *
144  *      Removes attributes from the I2O device again. Also search each device
145  *      on the controller for I2O devices which refert to this device as parent
146  *      or user and remove this links also.
147  */
148 static void i2o_device_class_release(struct class_device *cd)
149 {
150         struct i2o_device *i2o_dev, *tmp;
151         struct i2o_controller *c;
152
153         i2o_dev = to_i2o_device(cd->dev);
154         c = i2o_dev->iop;
155
156         sysfs_remove_link(&i2o_dev->device.kobj, "parent");
157         sysfs_remove_link(&i2o_dev->device.kobj, "user");
158
159         list_for_each_entry(tmp, &c->devices, list) {
160                 if (tmp->lct_data.parent_tid == i2o_dev->lct_data.tid)
161                         sysfs_remove_link(&tmp->device.kobj, "parent");
162                 if (tmp->lct_data.user_tid == i2o_dev->lct_data.tid)
163                         sysfs_remove_link(&tmp->device.kobj, "user");
164         }
165 };
166
167 /* I2O device class */
168 static struct class i2o_device_class = {
169         .name = "i2o_device",
170         .release = i2o_device_class_release
171 };
172
173 /**
174  *      i2o_device_alloc - Allocate a I2O device and initialize it
175  *
176  *      Allocate the memory for a I2O device and initialize locks and lists
177  *
178  *      Returns the allocated I2O device or a negative error code if the device
179  *      could not be allocated.
180  */
181 static struct i2o_device *i2o_device_alloc(void)
182 {
183         struct i2o_device *dev;
184
185         dev = kmalloc(sizeof(*dev), GFP_KERNEL);
186         if (!dev)
187                 return ERR_PTR(-ENOMEM);
188
189         memset(dev, 0, sizeof(*dev));
190
191         INIT_LIST_HEAD(&dev->list);
192         init_MUTEX(&dev->lock);
193
194         dev->device.bus = &i2o_bus_type;
195         dev->device.release = &i2o_device_release;
196         dev->classdev.class = &i2o_device_class;
197         dev->classdev.dev = &dev->device;
198
199         return dev;
200 };
201
202 /**
203  *      i2o_device_add - allocate a new I2O device and add it to the IOP
204  *      @iop: I2O controller where the device is on
205  *      @entry: LCT entry of the I2O device
206  *
207  *      Allocate a new I2O device and initialize it with the LCT entry. The
208  *      device is appended to the device list of the controller.
209  *
210  *      Returns a pointer to the I2O device on success or negative error code
211  *      on failure.
212  */
213 static struct i2o_device *i2o_device_add(struct i2o_controller *c,
214                                          i2o_lct_entry * entry)
215 {
216         struct i2o_device *dev;
217
218         dev = i2o_device_alloc();
219         if (IS_ERR(dev)) {
220                 printk(KERN_ERR "i2o: unable to allocate i2o device\n");
221                 return dev;
222         }
223
224         dev->lct_data = *entry;
225
226         snprintf(dev->device.bus_id, BUS_ID_SIZE, "%d:%03x", c->unit,
227                  dev->lct_data.tid);
228
229         snprintf(dev->classdev.class_id, BUS_ID_SIZE, "%d:%03x", c->unit,
230                  dev->lct_data.tid);
231
232         dev->iop = c;
233         dev->device.parent = &c->device;
234
235         device_register(&dev->device);
236
237         list_add_tail(&dev->list, &c->devices);
238
239         class_device_register(&dev->classdev);
240
241         i2o_driver_notify_device_add_all(dev);
242
243         pr_debug("i2o: device %s added\n", dev->device.bus_id);
244
245         return dev;
246 };
247
248 /**
249  *      i2o_device_remove - remove an I2O device from the I2O core
250  *      @dev: I2O device which should be released
251  *
252  *      Is used on I2O controller removal or LCT modification, when the device
253  *      is removed from the system. Note that the device could still hang
254  *      around until the refcount reaches 0.
255  */
256 void i2o_device_remove(struct i2o_device *i2o_dev)
257 {
258         i2o_driver_notify_device_remove_all(i2o_dev);
259         class_device_unregister(&i2o_dev->classdev);
260         list_del(&i2o_dev->list);
261         device_unregister(&i2o_dev->device);
262 };
263
264 /**
265  *      i2o_device_parse_lct - Parse a previously fetched LCT and create devices
266  *      @c: I2O controller from which the LCT should be parsed.
267  *
268  *      The Logical Configuration Table tells us what we can talk to on the
269  *      board. For every entry we create an I2O device, which is registered in
270  *      the I2O core.
271  *
272  *      Returns 0 on success or negative error code on failure.
273  */
274 int i2o_device_parse_lct(struct i2o_controller *c)
275 {
276         struct i2o_device *dev, *tmp;
277         i2o_lct *lct;
278         int i;
279         int max;
280
281         down(&c->lct_lock);
282
283         kfree(c->lct);
284
285         lct = c->dlct.virt;
286
287         c->lct = kmalloc(lct->table_size * 4, GFP_KERNEL);
288         if (!c->lct) {
289                 up(&c->lct_lock);
290                 return -ENOMEM;
291         }
292
293         if (lct->table_size * 4 > c->dlct.len) {
294                 memcpy(c->lct, c->dlct.virt, c->dlct.len);
295                 up(&c->lct_lock);
296                 return -EAGAIN;
297         }
298
299         memcpy(c->lct, c->dlct.virt, lct->table_size * 4);
300
301         lct = c->lct;
302
303         max = (lct->table_size - 3) / 9;
304
305         pr_debug("%s: LCT has %d entries (LCT size: %d)\n", c->name, max,
306                  lct->table_size);
307
308         /* remove devices, which are not in the LCT anymore */
309         list_for_each_entry_safe(dev, tmp, &c->devices, list) {
310                 int found = 0;
311
312                 for (i = 0; i < max; i++) {
313                         if (lct->lct_entry[i].tid == dev->lct_data.tid) {
314                                 found = 1;
315                                 break;
316                         }
317                 }
318
319                 if (!found)
320                         i2o_device_remove(dev);
321         }
322
323         /* add new devices, which are new in the LCT */
324         for (i = 0; i < max; i++) {
325                 int found = 0;
326
327                 list_for_each_entry_safe(dev, tmp, &c->devices, list) {
328                         if (lct->lct_entry[i].tid == dev->lct_data.tid) {
329                                 found = 1;
330                                 break;
331                         }
332                 }
333
334                 if (!found)
335                         i2o_device_add(c, &lct->lct_entry[i]);
336         }
337         up(&c->lct_lock);
338
339         return 0;
340 };
341
342 /**
343  *      i2o_device_class_show_class_id - Displays class id of I2O device
344  *      @cd: class device of which the class id should be displayed
345  *      @buf: buffer into which the class id should be printed
346  *
347  *      Returns the number of bytes which are printed into the buffer.
348  */
349 static ssize_t i2o_device_class_show_class_id(struct class_device *cd,
350                                               char *buf)
351 {
352         struct i2o_device *dev = to_i2o_device(cd->dev);
353
354         sprintf(buf, "0x%03x\n", dev->lct_data.class_id);
355         return strlen(buf) + 1;
356 };
357
358 /**
359  *      i2o_device_class_show_tid - Displays TID of I2O device
360  *      @cd: class device of which the TID should be displayed
361  *      @buf: buffer into which the class id should be printed
362  *
363  *      Returns the number of bytes which are printed into the buffer.
364  */
365 static ssize_t i2o_device_class_show_tid(struct class_device *cd, char *buf)
366 {
367         struct i2o_device *dev = to_i2o_device(cd->dev);
368
369         sprintf(buf, "0x%03x\n", dev->lct_data.tid);
370         return strlen(buf) + 1;
371 };
372
373 /* I2O device class attributes */
374 static CLASS_DEVICE_ATTR(class_id, S_IRUGO, i2o_device_class_show_class_id,
375                          NULL);
376 static CLASS_DEVICE_ATTR(tid, S_IRUGO, i2o_device_class_show_tid, NULL);
377
378 /**
379  *      i2o_device_class_add - Adds attributes to the I2O device
380  *      @cd: I2O class device which is added to the I2O device class
381  *
382  *      This function get called when a I2O device is added to the class. It
383  *      creates the attributes for each device and creates user/parent symlink
384  *      if necessary.
385  *
386  *      Returns 0 on success or negative error code on failure.
387  */
388 static int i2o_device_class_add(struct class_device *cd)
389 {
390         struct i2o_device *i2o_dev, *tmp;
391         struct i2o_controller *c;
392
393         i2o_dev = to_i2o_device(cd->dev);
394         c = i2o_dev->iop;
395
396         class_device_create_file(cd, &class_device_attr_class_id);
397         class_device_create_file(cd, &class_device_attr_tid);
398
399         /* create user entries for this device */
400         tmp = i2o_iop_find_device(i2o_dev->iop, i2o_dev->lct_data.user_tid);
401         if (tmp && (tmp != i2o_dev))
402                 sysfs_create_link(&i2o_dev->device.kobj, &tmp->device.kobj,
403                                   "user");
404
405         /* create user entries refering to this device */
406         list_for_each_entry(tmp, &c->devices, list)
407             if ((tmp->lct_data.user_tid == i2o_dev->lct_data.tid)
408                 && (tmp != i2o_dev))
409                 sysfs_create_link(&tmp->device.kobj,
410                                   &i2o_dev->device.kobj, "user");
411
412         /* create parent entries for this device */
413         tmp = i2o_iop_find_device(i2o_dev->iop, i2o_dev->lct_data.parent_tid);
414         if (tmp && (tmp != i2o_dev))
415                 sysfs_create_link(&i2o_dev->device.kobj, &tmp->device.kobj,
416                                   "parent");
417
418         /* create parent entries refering to this device */
419         list_for_each_entry(tmp, &c->devices, list)
420             if ((tmp->lct_data.parent_tid == i2o_dev->lct_data.tid)
421                 && (tmp != i2o_dev))
422                 sysfs_create_link(&tmp->device.kobj,
423                                   &i2o_dev->device.kobj, "parent");
424
425         return 0;
426 };
427
428 /* I2O device class interface */
429 static struct class_interface i2o_device_class_interface = {
430         .class = &i2o_device_class,
431         .add = i2o_device_class_add
432 };
433
434 /*
435  *      Run time support routines
436  */
437
438 /*      Issue UTIL_PARAMS_GET or UTIL_PARAMS_SET
439  *
440  *      This function can be used for all UtilParamsGet/Set operations.
441  *      The OperationList is given in oplist-buffer,
442  *      and results are returned in reslist-buffer.
443  *      Note that the minimum sized reslist is 8 bytes and contains
444  *      ResultCount, ErrorInfoSize, BlockStatus and BlockSize.
445  */
446 int i2o_parm_issue(struct i2o_device *i2o_dev, int cmd, void *oplist,
447                           int oplen, void *reslist, int reslen)
448 {
449         struct i2o_message __iomem *msg;
450         u32 m;
451         u32 *res32 = (u32 *) reslist;
452         u32 *restmp = (u32 *) reslist;
453         int len = 0;
454         int i = 0;
455         int rc;
456         struct i2o_dma res;
457         struct i2o_controller *c = i2o_dev->iop;
458         struct device *dev = &c->pdev->dev;
459
460         res.virt = NULL;
461
462         if (i2o_dma_alloc(dev, &res, reslen, GFP_KERNEL))
463                 return -ENOMEM;
464
465         m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
466         if (m == I2O_QUEUE_EMPTY) {
467                 i2o_dma_free(dev, &res);
468                 return -ETIMEDOUT;
469         }
470
471         i = 0;
472         writel(cmd << 24 | HOST_TID << 12 | i2o_dev->lct_data.tid,
473                &msg->u.head[1]);
474         writel(0, &msg->body[i++]);
475         writel(0x4C000000 | oplen, &msg->body[i++]);    /* OperationList */
476         memcpy_toio(&msg->body[i], oplist, oplen);
477         i += (oplen / 4 + (oplen % 4 ? 1 : 0));
478         writel(0xD0000000 | res.len, &msg->body[i++]);  /* ResultList */
479         writel(res.phys, &msg->body[i++]);
480
481         writel(I2O_MESSAGE_SIZE(i + sizeof(struct i2o_message) / 4) |
482                SGL_OFFSET_5, &msg->u.head[0]);
483
484         rc = i2o_msg_post_wait_mem(c, m, 10, &res);
485
486         /* This only looks like a memory leak - don't "fix" it. */
487         if (rc == -ETIMEDOUT)
488                 return rc;
489
490         memcpy(reslist, res.virt, res.len);
491         i2o_dma_free(dev, &res);
492
493         /* Query failed */
494         if (rc)
495                 return rc;
496         /*
497          * Calculate number of bytes of Result LIST
498          * We need to loop through each Result BLOCK and grab the length
499          */
500         restmp = res32 + 1;
501         len = 1;
502         for (i = 0; i < (res32[0] & 0X0000FFFF); i++) {
503                 if (restmp[0] & 0x00FF0000) {   /* BlockStatus != SUCCESS */
504                         printk(KERN_WARNING
505                                "%s - Error:\n  ErrorInfoSize = 0x%02x, "
506                                "BlockStatus = 0x%02x, BlockSize = 0x%04x\n",
507                                (cmd ==
508                                 I2O_CMD_UTIL_PARAMS_SET) ? "PARAMS_SET" :
509                                "PARAMS_GET", res32[1] >> 24,
510                                (res32[1] >> 16) & 0xFF, res32[1] & 0xFFFF);
511
512                         /*
513                          *      If this is the only request,than we return an error
514                          */
515                         if ((res32[0] & 0x0000FFFF) == 1) {
516                                 return -((res32[1] >> 16) & 0xFF);      /* -BlockStatus */
517                         }
518                 }
519                 len += restmp[0] & 0x0000FFFF;  /* Length of res BLOCK */
520                 restmp += restmp[0] & 0x0000FFFF;       /* Skip to next BLOCK */
521         }
522         return (len << 2);      /* bytes used by result list */
523 }
524
525 /*
526  *       Query one field group value or a whole scalar group.
527  */
528 int i2o_parm_field_get(struct i2o_device *i2o_dev, int group, int field,
529                        void *buf, int buflen)
530 {
531         u16 opblk[] = { 1, 0, I2O_PARAMS_FIELD_GET, group, 1, field };
532         u8 *resblk;             /* 8 bytes for header */
533         int size;
534
535         if (field == -1)        /* whole group */
536                 opblk[4] = -1;
537
538         resblk = kmalloc(buflen + 8, GFP_KERNEL | GFP_ATOMIC);
539         if (!resblk)
540                 return -ENOMEM;
541
542         size = i2o_parm_issue(i2o_dev, I2O_CMD_UTIL_PARAMS_GET, opblk,
543                               sizeof(opblk), resblk, buflen + 8);
544
545         memcpy(buf, resblk + 8, buflen);        /* cut off header */
546
547         kfree(resblk);
548
549         if (size > buflen)
550                 return buflen;
551
552         return size;
553 }
554
555 /*
556  *      if oper == I2O_PARAMS_TABLE_GET, get from all rows
557  *              if fieldcount == -1 return all fields
558  *                      ibuf and ibuflen are unused (use NULL, 0)
559  *              else return specific fields
560  *                      ibuf contains fieldindexes
561  *
562  *      if oper == I2O_PARAMS_LIST_GET, get from specific rows
563  *              if fieldcount == -1 return all fields
564  *                      ibuf contains rowcount, keyvalues
565  *              else return specific fields
566  *                      fieldcount is # of fieldindexes
567  *                      ibuf contains fieldindexes, rowcount, keyvalues
568  *
569  *      You could also use directly function i2o_issue_params().
570  */
571 int i2o_parm_table_get(struct i2o_device *dev, int oper, int group,
572                        int fieldcount, void *ibuf, int ibuflen, void *resblk,
573                        int reslen)
574 {
575         u16 *opblk;
576         int size;
577
578         size = 10 + ibuflen;
579         if (size % 4)
580                 size += 4 - size % 4;
581
582         opblk = kmalloc(size, GFP_KERNEL);
583         if (opblk == NULL) {
584                 printk(KERN_ERR "i2o: no memory for query buffer.\n");
585                 return -ENOMEM;
586         }
587
588         opblk[0] = 1;           /* operation count */
589         opblk[1] = 0;           /* pad */
590         opblk[2] = oper;
591         opblk[3] = group;
592         opblk[4] = fieldcount;
593         memcpy(opblk + 5, ibuf, ibuflen);       /* other params */
594
595         size = i2o_parm_issue(dev, I2O_CMD_UTIL_PARAMS_GET, opblk,
596                               size, resblk, reslen);
597
598         kfree(opblk);
599         if (size > reslen)
600                 return reslen;
601
602         return size;
603 }
604
605 /**
606  *      i2o_device_init - Initialize I2O devices
607  *
608  *      Registers the I2O device class.
609  *
610  *      Returns 0 on success or negative error code on failure.
611  */
612 int i2o_device_init(void)
613 {
614         int rc;
615
616         rc = class_register(&i2o_device_class);
617         if (rc)
618                 return rc;
619
620         return class_interface_register(&i2o_device_class_interface);
621 };
622
623 /**
624  *      i2o_device_exit - I2O devices exit function
625  *
626  *      Unregisters the I2O device class.
627  */
628 void i2o_device_exit(void)
629 {
630         class_interface_register(&i2o_device_class_interface);
631         class_unregister(&i2o_device_class);
632 };
633
634 EXPORT_SYMBOL(i2o_device_claim);
635 EXPORT_SYMBOL(i2o_device_claim_release);
636 EXPORT_SYMBOL(i2o_parm_field_get);
637 EXPORT_SYMBOL(i2o_parm_table_get);
638 EXPORT_SYMBOL(i2o_parm_issue);