[SCSI] m68k: new mac_esp scsi driver
[safe/jmp/linux-2.6] / drivers / scsi / scsi_transport_sas.c
1 /*
2  * Copyright (C) 2005-2006 Dell Inc.
3  *      Released under GPL v2.
4  *
5  * Serial Attached SCSI (SAS) transport class.
6  *
7  * The SAS transport class contains common code to deal with SAS HBAs,
8  * an aproximated representation of SAS topologies in the driver model,
9  * and various sysfs attributes to expose these topologies and management
10  * interfaces to userspace.
11  *
12  * In addition to the basic SCSI core objects this transport class
13  * introduces two additional intermediate objects:  The SAS PHY
14  * as represented by struct sas_phy defines an "outgoing" PHY on
15  * a SAS HBA or Expander, and the SAS remote PHY represented by
16  * struct sas_rphy defines an "incoming" PHY on a SAS Expander or
17  * end device.  Note that this is purely a software concept, the
18  * underlying hardware for a PHY and a remote PHY is the exactly
19  * the same.
20  *
21  * There is no concept of a SAS port in this code, users can see
22  * what PHYs form a wide port based on the port_identifier attribute,
23  * which is the same for all PHYs in a port.
24  */
25
26 #include <linux/init.h>
27 #include <linux/module.h>
28 #include <linux/jiffies.h>
29 #include <linux/err.h>
30 #include <linux/slab.h>
31 #include <linux/string.h>
32 #include <linux/blkdev.h>
33 #include <linux/bsg.h>
34
35 #include <scsi/scsi.h>
36 #include <scsi/scsi_device.h>
37 #include <scsi/scsi_host.h>
38 #include <scsi/scsi_transport.h>
39 #include <scsi/scsi_transport_sas.h>
40
41 #include "scsi_sas_internal.h"
42 struct sas_host_attrs {
43         struct list_head rphy_list;
44         struct mutex lock;
45         struct request_queue *q;
46         u32 next_target_id;
47         u32 next_expander_id;
48         int next_port_id;
49 };
50 #define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data)
51
52
53 /*
54  * Hack to allow attributes of the same name in different objects.
55  */
56 #define SAS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
57         struct device_attribute dev_attr_##_prefix##_##_name = \
58         __ATTR(_name,_mode,_show,_store)
59
60
61 /*
62  * Pretty printing helpers
63  */
64
65 #define sas_bitfield_name_match(title, table)                   \
66 static ssize_t                                                  \
67 get_sas_##title##_names(u32 table_key, char *buf)               \
68 {                                                               \
69         char *prefix = "";                                      \
70         ssize_t len = 0;                                        \
71         int i;                                                  \
72                                                                 \
73         for (i = 0; i < ARRAY_SIZE(table); i++) {               \
74                 if (table[i].value & table_key) {               \
75                         len += sprintf(buf + len, "%s%s",       \
76                                 prefix, table[i].name);         \
77                         prefix = ", ";                          \
78                 }                                               \
79         }                                                       \
80         len += sprintf(buf + len, "\n");                        \
81         return len;                                             \
82 }
83
84 #define sas_bitfield_name_set(title, table)                     \
85 static ssize_t                                                  \
86 set_sas_##title##_names(u32 *table_key, const char *buf)        \
87 {                                                               \
88         ssize_t len = 0;                                        \
89         int i;                                                  \
90                                                                 \
91         for (i = 0; i < ARRAY_SIZE(table); i++) {               \
92                 len = strlen(table[i].name);                    \
93                 if (strncmp(buf, table[i].name, len) == 0 &&    \
94                     (buf[len] == '\n' || buf[len] == '\0')) {   \
95                         *table_key = table[i].value;            \
96                         return 0;                               \
97                 }                                               \
98         }                                                       \
99         return -EINVAL;                                         \
100 }
101
102 #define sas_bitfield_name_search(title, table)                  \
103 static ssize_t                                                  \
104 get_sas_##title##_names(u32 table_key, char *buf)               \
105 {                                                               \
106         ssize_t len = 0;                                        \
107         int i;                                                  \
108                                                                 \
109         for (i = 0; i < ARRAY_SIZE(table); i++) {               \
110                 if (table[i].value == table_key) {              \
111                         len += sprintf(buf + len, "%s",         \
112                                 table[i].name);                 \
113                         break;                                  \
114                 }                                               \
115         }                                                       \
116         len += sprintf(buf + len, "\n");                        \
117         return len;                                             \
118 }
119
120 static struct {
121         u32             value;
122         char            *name;
123 } sas_device_type_names[] = {
124         { SAS_PHY_UNUSED,               "unused" },
125         { SAS_END_DEVICE,               "end device" },
126         { SAS_EDGE_EXPANDER_DEVICE,     "edge expander" },
127         { SAS_FANOUT_EXPANDER_DEVICE,   "fanout expander" },
128 };
129 sas_bitfield_name_search(device_type, sas_device_type_names)
130
131
132 static struct {
133         u32             value;
134         char            *name;
135 } sas_protocol_names[] = {
136         { SAS_PROTOCOL_SATA,            "sata" },
137         { SAS_PROTOCOL_SMP,             "smp" },
138         { SAS_PROTOCOL_STP,             "stp" },
139         { SAS_PROTOCOL_SSP,             "ssp" },
140 };
141 sas_bitfield_name_match(protocol, sas_protocol_names)
142
143 static struct {
144         u32             value;
145         char            *name;
146 } sas_linkspeed_names[] = {
147         { SAS_LINK_RATE_UNKNOWN,        "Unknown" },
148         { SAS_PHY_DISABLED,             "Phy disabled" },
149         { SAS_LINK_RATE_FAILED,         "Link Rate failed" },
150         { SAS_SATA_SPINUP_HOLD,         "Spin-up hold" },
151         { SAS_LINK_RATE_1_5_GBPS,       "1.5 Gbit" },
152         { SAS_LINK_RATE_3_0_GBPS,       "3.0 Gbit" },
153         { SAS_LINK_RATE_6_0_GBPS,       "6.0 Gbit" },
154 };
155 sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
156 sas_bitfield_name_set(linkspeed, sas_linkspeed_names)
157
158 static void sas_smp_request(struct request_queue *q, struct Scsi_Host *shost,
159                             struct sas_rphy *rphy)
160 {
161         struct request *req;
162         int ret;
163         int (*handler)(struct Scsi_Host *, struct sas_rphy *, struct request *);
164
165         while (!blk_queue_plugged(q)) {
166                 req = elv_next_request(q);
167                 if (!req)
168                         break;
169
170                 blkdev_dequeue_request(req);
171
172                 spin_unlock_irq(q->queue_lock);
173
174                 handler = to_sas_internal(shost->transportt)->f->smp_handler;
175                 ret = handler(shost, rphy, req);
176                 req->errors = ret;
177
178                 spin_lock_irq(q->queue_lock);
179
180                 req->end_io(req, ret);
181         }
182 }
183
184 static void sas_host_smp_request(struct request_queue *q)
185 {
186         sas_smp_request(q, (struct Scsi_Host *)q->queuedata, NULL);
187 }
188
189 static void sas_non_host_smp_request(struct request_queue *q)
190 {
191         struct sas_rphy *rphy = q->queuedata;
192         sas_smp_request(q, rphy_to_shost(rphy), rphy);
193 }
194
195 static void sas_host_release(struct device *dev)
196 {
197         struct Scsi_Host *shost = dev_to_shost(dev);
198         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
199         struct request_queue *q = sas_host->q;
200
201         if (q)
202                 blk_cleanup_queue(q);
203 }
204
205 static int sas_bsg_initialize(struct Scsi_Host *shost, struct sas_rphy *rphy)
206 {
207         struct request_queue *q;
208         int error;
209         struct device *dev;
210         char namebuf[BUS_ID_SIZE];
211         const char *name;
212         void (*release)(struct device *);
213
214         if (!to_sas_internal(shost->transportt)->f->smp_handler) {
215                 printk("%s can't handle SMP requests\n", shost->hostt->name);
216                 return 0;
217         }
218
219         if (rphy) {
220                 q = blk_init_queue(sas_non_host_smp_request, NULL);
221                 dev = &rphy->dev;
222                 name = dev->bus_id;
223                 release = NULL;
224         } else {
225                 q = blk_init_queue(sas_host_smp_request, NULL);
226                 dev = &shost->shost_gendev;
227                 snprintf(namebuf, sizeof(namebuf),
228                          "sas_host%d", shost->host_no);
229                 name = namebuf;
230                 release = sas_host_release;
231         }
232         if (!q)
233                 return -ENOMEM;
234
235         error = bsg_register_queue(q, dev, name, release);
236         if (error) {
237                 blk_cleanup_queue(q);
238                 return -ENOMEM;
239         }
240
241         if (rphy)
242                 rphy->q = q;
243         else
244                 to_sas_host_attrs(shost)->q = q;
245
246         if (rphy)
247                 q->queuedata = rphy;
248         else
249                 q->queuedata = shost;
250
251         set_bit(QUEUE_FLAG_BIDI, &q->queue_flags);
252
253         return 0;
254 }
255
256 static void sas_bsg_remove(struct Scsi_Host *shost, struct sas_rphy *rphy)
257 {
258         struct request_queue *q;
259
260         if (rphy)
261                 q = rphy->q;
262         else
263                 q = to_sas_host_attrs(shost)->q;
264
265         if (!q)
266                 return;
267
268         bsg_unregister_queue(q);
269 }
270
271 /*
272  * SAS host attributes
273  */
274
275 static int sas_host_setup(struct transport_container *tc, struct device *dev,
276                           struct device *cdev)
277 {
278         struct Scsi_Host *shost = dev_to_shost(dev);
279         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
280
281         INIT_LIST_HEAD(&sas_host->rphy_list);
282         mutex_init(&sas_host->lock);
283         sas_host->next_target_id = 0;
284         sas_host->next_expander_id = 0;
285         sas_host->next_port_id = 0;
286
287         if (sas_bsg_initialize(shost, NULL))
288                 dev_printk(KERN_ERR, dev, "fail to a bsg device %d\n",
289                            shost->host_no);
290
291         return 0;
292 }
293
294 static int sas_host_remove(struct transport_container *tc, struct device *dev,
295                            struct device *cdev)
296 {
297         struct Scsi_Host *shost = dev_to_shost(dev);
298
299         sas_bsg_remove(shost, NULL);
300
301         return 0;
302 }
303
304 static DECLARE_TRANSPORT_CLASS(sas_host_class,
305                 "sas_host", sas_host_setup, sas_host_remove, NULL);
306
307 static int sas_host_match(struct attribute_container *cont,
308                             struct device *dev)
309 {
310         struct Scsi_Host *shost;
311         struct sas_internal *i;
312
313         if (!scsi_is_host_device(dev))
314                 return 0;
315         shost = dev_to_shost(dev);
316
317         if (!shost->transportt)
318                 return 0;
319         if (shost->transportt->host_attrs.ac.class !=
320                         &sas_host_class.class)
321                 return 0;
322
323         i = to_sas_internal(shost->transportt);
324         return &i->t.host_attrs.ac == cont;
325 }
326
327 static int do_sas_phy_delete(struct device *dev, void *data)
328 {
329         int pass = (int)(unsigned long)data;
330
331         if (pass == 0 && scsi_is_sas_port(dev))
332                 sas_port_delete(dev_to_sas_port(dev));
333         else if (pass == 1 && scsi_is_sas_phy(dev))
334                 sas_phy_delete(dev_to_phy(dev));
335         return 0;
336 }
337
338 /**
339  * sas_remove_children  -  tear down a devices SAS data structures
340  * @dev:        device belonging to the sas object
341  *
342  * Removes all SAS PHYs and remote PHYs for a given object
343  */
344 void sas_remove_children(struct device *dev)
345 {
346         device_for_each_child(dev, (void *)0, do_sas_phy_delete);
347         device_for_each_child(dev, (void *)1, do_sas_phy_delete);
348 }
349 EXPORT_SYMBOL(sas_remove_children);
350
351 /**
352  * sas_remove_host  -  tear down a Scsi_Host's SAS data structures
353  * @shost:      Scsi Host that is torn down
354  *
355  * Removes all SAS PHYs and remote PHYs for a given Scsi_Host.
356  * Must be called just before scsi_remove_host for SAS HBAs.
357  */
358 void sas_remove_host(struct Scsi_Host *shost)
359 {
360         sas_remove_children(&shost->shost_gendev);
361 }
362 EXPORT_SYMBOL(sas_remove_host);
363
364
365 /*
366  * SAS Phy attributes
367  */
368
369 #define sas_phy_show_simple(field, name, format_string, cast)           \
370 static ssize_t                                                          \
371 show_sas_phy_##name(struct device *dev,                                 \
372                     struct device_attribute *attr, char *buf)           \
373 {                                                                       \
374         struct sas_phy *phy = transport_class_to_phy(dev);              \
375                                                                         \
376         return snprintf(buf, 20, format_string, cast phy->field);       \
377 }
378
379 #define sas_phy_simple_attr(field, name, format_string, type)           \
380         sas_phy_show_simple(field, name, format_string, (type)) \
381 static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
382
383 #define sas_phy_show_protocol(field, name)                              \
384 static ssize_t                                                          \
385 show_sas_phy_##name(struct device *dev,                                 \
386                     struct device_attribute *attr, char *buf)           \
387 {                                                                       \
388         struct sas_phy *phy = transport_class_to_phy(dev);              \
389                                                                         \
390         if (!phy->field)                                                \
391                 return snprintf(buf, 20, "none\n");                     \
392         return get_sas_protocol_names(phy->field, buf);         \
393 }
394
395 #define sas_phy_protocol_attr(field, name)                              \
396         sas_phy_show_protocol(field, name)                              \
397 static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
398
399 #define sas_phy_show_linkspeed(field)                                   \
400 static ssize_t                                                          \
401 show_sas_phy_##field(struct device *dev,                                \
402                      struct device_attribute *attr, char *buf)          \
403 {                                                                       \
404         struct sas_phy *phy = transport_class_to_phy(dev);              \
405                                                                         \
406         return get_sas_linkspeed_names(phy->field, buf);                \
407 }
408
409 /* Fudge to tell if we're minimum or maximum */
410 #define sas_phy_store_linkspeed(field)                                  \
411 static ssize_t                                                          \
412 store_sas_phy_##field(struct device *dev,                               \
413                       struct device_attribute *attr,                    \
414                       const char *buf,  size_t count)                   \
415 {                                                                       \
416         struct sas_phy *phy = transport_class_to_phy(dev);              \
417         struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);        \
418         struct sas_internal *i = to_sas_internal(shost->transportt);    \
419         u32 value;                                                      \
420         struct sas_phy_linkrates rates = {0};                           \
421         int error;                                                      \
422                                                                         \
423         error = set_sas_linkspeed_names(&value, buf);                   \
424         if (error)                                                      \
425                 return error;                                           \
426         rates.field = value;                                            \
427         error = i->f->set_phy_speed(phy, &rates);                       \
428                                                                         \
429         return error ? error : count;                                   \
430 }
431
432 #define sas_phy_linkspeed_rw_attr(field)                                \
433         sas_phy_show_linkspeed(field)                                   \
434         sas_phy_store_linkspeed(field)                                  \
435 static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field,                \
436         store_sas_phy_##field)
437
438 #define sas_phy_linkspeed_attr(field)                                   \
439         sas_phy_show_linkspeed(field)                                   \
440 static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
441
442
443 #define sas_phy_show_linkerror(field)                                   \
444 static ssize_t                                                          \
445 show_sas_phy_##field(struct device *dev,                                \
446                      struct device_attribute *attr, char *buf)          \
447 {                                                                       \
448         struct sas_phy *phy = transport_class_to_phy(dev);              \
449         struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);        \
450         struct sas_internal *i = to_sas_internal(shost->transportt);    \
451         int error;                                                      \
452                                                                         \
453         error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0;   \
454         if (error)                                                      \
455                 return error;                                           \
456         return snprintf(buf, 20, "%u\n", phy->field);                   \
457 }
458
459 #define sas_phy_linkerror_attr(field)                                   \
460         sas_phy_show_linkerror(field)                                   \
461 static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
462
463
464 static ssize_t
465 show_sas_device_type(struct device *dev,
466                      struct device_attribute *attr, char *buf)
467 {
468         struct sas_phy *phy = transport_class_to_phy(dev);
469
470         if (!phy->identify.device_type)
471                 return snprintf(buf, 20, "none\n");
472         return get_sas_device_type_names(phy->identify.device_type, buf);
473 }
474 static DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
475
476 static ssize_t do_sas_phy_enable(struct device *dev,
477                 size_t count, int enable)
478 {
479         struct sas_phy *phy = transport_class_to_phy(dev);
480         struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
481         struct sas_internal *i = to_sas_internal(shost->transportt);
482         int error;
483
484         error = i->f->phy_enable(phy, enable);
485         if (error)
486                 return error;
487         phy->enabled = enable;
488         return count;
489 };
490
491 static ssize_t
492 store_sas_phy_enable(struct device *dev, struct device_attribute *attr,
493                      const char *buf, size_t count)
494 {
495         if (count < 1)
496                 return -EINVAL;
497
498         switch (buf[0]) {
499         case '0':
500                 do_sas_phy_enable(dev, count, 0);
501                 break;
502         case '1':
503                 do_sas_phy_enable(dev, count, 1);
504                 break;
505         default:
506                 return -EINVAL;
507         }
508
509         return count;
510 }
511
512 static ssize_t
513 show_sas_phy_enable(struct device *dev, struct device_attribute *attr,
514                     char *buf)
515 {
516         struct sas_phy *phy = transport_class_to_phy(dev);
517
518         return snprintf(buf, 20, "%d", phy->enabled);
519 }
520
521 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, show_sas_phy_enable,
522                          store_sas_phy_enable);
523
524 static ssize_t
525 do_sas_phy_reset(struct device *dev, size_t count, int hard_reset)
526 {
527         struct sas_phy *phy = transport_class_to_phy(dev);
528         struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
529         struct sas_internal *i = to_sas_internal(shost->transportt);
530         int error;
531
532         error = i->f->phy_reset(phy, hard_reset);
533         if (error)
534                 return error;
535         return count;
536 };
537
538 static ssize_t
539 store_sas_link_reset(struct device *dev, struct device_attribute *attr,
540                      const char *buf, size_t count)
541 {
542         return do_sas_phy_reset(dev, count, 0);
543 }
544 static DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
545
546 static ssize_t
547 store_sas_hard_reset(struct device *dev, struct device_attribute *attr,
548                      const char *buf, size_t count)
549 {
550         return do_sas_phy_reset(dev, count, 1);
551 }
552 static DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
553
554 sas_phy_protocol_attr(identify.initiator_port_protocols,
555                 initiator_port_protocols);
556 sas_phy_protocol_attr(identify.target_port_protocols,
557                 target_port_protocols);
558 sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
559                 unsigned long long);
560 sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
561 //sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", int);
562 sas_phy_linkspeed_attr(negotiated_linkrate);
563 sas_phy_linkspeed_attr(minimum_linkrate_hw);
564 sas_phy_linkspeed_rw_attr(minimum_linkrate);
565 sas_phy_linkspeed_attr(maximum_linkrate_hw);
566 sas_phy_linkspeed_rw_attr(maximum_linkrate);
567 sas_phy_linkerror_attr(invalid_dword_count);
568 sas_phy_linkerror_attr(running_disparity_error_count);
569 sas_phy_linkerror_attr(loss_of_dword_sync_count);
570 sas_phy_linkerror_attr(phy_reset_problem_count);
571
572
573 static DECLARE_TRANSPORT_CLASS(sas_phy_class,
574                 "sas_phy", NULL, NULL, NULL);
575
576 static int sas_phy_match(struct attribute_container *cont, struct device *dev)
577 {
578         struct Scsi_Host *shost;
579         struct sas_internal *i;
580
581         if (!scsi_is_sas_phy(dev))
582                 return 0;
583         shost = dev_to_shost(dev->parent);
584
585         if (!shost->transportt)
586                 return 0;
587         if (shost->transportt->host_attrs.ac.class !=
588                         &sas_host_class.class)
589                 return 0;
590
591         i = to_sas_internal(shost->transportt);
592         return &i->phy_attr_cont.ac == cont;
593 }
594
595 static void sas_phy_release(struct device *dev)
596 {
597         struct sas_phy *phy = dev_to_phy(dev);
598
599         put_device(dev->parent);
600         kfree(phy);
601 }
602
603 /**
604  * sas_phy_alloc  -  allocates and initialize a SAS PHY structure
605  * @parent:     Parent device
606  * @number:     Phy index
607  *
608  * Allocates an SAS PHY structure.  It will be added in the device tree
609  * below the device specified by @parent, which has to be either a Scsi_Host
610  * or sas_rphy.
611  *
612  * Returns:
613  *      SAS PHY allocated or %NULL if the allocation failed.
614  */
615 struct sas_phy *sas_phy_alloc(struct device *parent, int number)
616 {
617         struct Scsi_Host *shost = dev_to_shost(parent);
618         struct sas_phy *phy;
619
620         phy = kzalloc(sizeof(*phy), GFP_KERNEL);
621         if (!phy)
622                 return NULL;
623
624         phy->number = number;
625         phy->enabled = 1;
626
627         device_initialize(&phy->dev);
628         phy->dev.parent = get_device(parent);
629         phy->dev.release = sas_phy_release;
630         INIT_LIST_HEAD(&phy->port_siblings);
631         if (scsi_is_sas_expander_device(parent)) {
632                 struct sas_rphy *rphy = dev_to_rphy(parent);
633                 sprintf(phy->dev.bus_id, "phy-%d:%d:%d", shost->host_no,
634                         rphy->scsi_target_id, number);
635         } else
636                 sprintf(phy->dev.bus_id, "phy-%d:%d", shost->host_no, number);
637
638         transport_setup_device(&phy->dev);
639
640         return phy;
641 }
642 EXPORT_SYMBOL(sas_phy_alloc);
643
644 /**
645  * sas_phy_add  -  add a SAS PHY to the device hierarchy
646  * @phy:        The PHY to be added
647  *
648  * Publishes a SAS PHY to the rest of the system.
649  */
650 int sas_phy_add(struct sas_phy *phy)
651 {
652         int error;
653
654         error = device_add(&phy->dev);
655         if (!error) {
656                 transport_add_device(&phy->dev);
657                 transport_configure_device(&phy->dev);
658         }
659
660         return error;
661 }
662 EXPORT_SYMBOL(sas_phy_add);
663
664 /**
665  * sas_phy_free  -  free a SAS PHY
666  * @phy:        SAS PHY to free
667  *
668  * Frees the specified SAS PHY.
669  *
670  * Note:
671  *   This function must only be called on a PHY that has not
672  *   sucessfully been added using sas_phy_add().
673  */
674 void sas_phy_free(struct sas_phy *phy)
675 {
676         transport_destroy_device(&phy->dev);
677         put_device(&phy->dev);
678 }
679 EXPORT_SYMBOL(sas_phy_free);
680
681 /**
682  * sas_phy_delete  -  remove SAS PHY
683  * @phy:        SAS PHY to remove
684  *
685  * Removes the specified SAS PHY.  If the SAS PHY has an
686  * associated remote PHY it is removed before.
687  */
688 void
689 sas_phy_delete(struct sas_phy *phy)
690 {
691         struct device *dev = &phy->dev;
692
693         /* this happens if the phy is still part of a port when deleted */
694         BUG_ON(!list_empty(&phy->port_siblings));
695
696         transport_remove_device(dev);
697         device_del(dev);
698         transport_destroy_device(dev);
699         put_device(dev);
700 }
701 EXPORT_SYMBOL(sas_phy_delete);
702
703 /**
704  * scsi_is_sas_phy  -  check if a struct device represents a SAS PHY
705  * @dev:        device to check
706  *
707  * Returns:
708  *      %1 if the device represents a SAS PHY, %0 else
709  */
710 int scsi_is_sas_phy(const struct device *dev)
711 {
712         return dev->release == sas_phy_release;
713 }
714 EXPORT_SYMBOL(scsi_is_sas_phy);
715
716 /*
717  * SAS Port attributes
718  */
719 #define sas_port_show_simple(field, name, format_string, cast)          \
720 static ssize_t                                                          \
721 show_sas_port_##name(struct device *dev,                                \
722                      struct device_attribute *attr, char *buf)          \
723 {                                                                       \
724         struct sas_port *port = transport_class_to_sas_port(dev);       \
725                                                                         \
726         return snprintf(buf, 20, format_string, cast port->field);      \
727 }
728
729 #define sas_port_simple_attr(field, name, format_string, type)          \
730         sas_port_show_simple(field, name, format_string, (type))        \
731 static DEVICE_ATTR(name, S_IRUGO, show_sas_port_##name, NULL)
732
733 sas_port_simple_attr(num_phys, num_phys, "%d\n", int);
734
735 static DECLARE_TRANSPORT_CLASS(sas_port_class,
736                                "sas_port", NULL, NULL, NULL);
737
738 static int sas_port_match(struct attribute_container *cont, struct device *dev)
739 {
740         struct Scsi_Host *shost;
741         struct sas_internal *i;
742
743         if (!scsi_is_sas_port(dev))
744                 return 0;
745         shost = dev_to_shost(dev->parent);
746
747         if (!shost->transportt)
748                 return 0;
749         if (shost->transportt->host_attrs.ac.class !=
750                         &sas_host_class.class)
751                 return 0;
752
753         i = to_sas_internal(shost->transportt);
754         return &i->port_attr_cont.ac == cont;
755 }
756
757
758 static void sas_port_release(struct device *dev)
759 {
760         struct sas_port *port = dev_to_sas_port(dev);
761
762         BUG_ON(!list_empty(&port->phy_list));
763
764         put_device(dev->parent);
765         kfree(port);
766 }
767
768 static void sas_port_create_link(struct sas_port *port,
769                                  struct sas_phy *phy)
770 {
771         int res;
772
773         res = sysfs_create_link(&port->dev.kobj, &phy->dev.kobj,
774                                 phy->dev.bus_id);
775         if (res)
776                 goto err;
777         res = sysfs_create_link(&phy->dev.kobj, &port->dev.kobj, "port");
778         if (res)
779                 goto err;
780         return;
781 err:
782         printk(KERN_ERR "%s: Cannot create port links, err=%d\n",
783                __FUNCTION__, res);
784 }
785
786 static void sas_port_delete_link(struct sas_port *port,
787                                  struct sas_phy *phy)
788 {
789         sysfs_remove_link(&port->dev.kobj, phy->dev.bus_id);
790         sysfs_remove_link(&phy->dev.kobj, "port");
791 }
792
793 /** sas_port_alloc - allocate and initialize a SAS port structure
794  *
795  * @parent:     parent device
796  * @port_id:    port number
797  *
798  * Allocates a SAS port structure.  It will be added to the device tree
799  * below the device specified by @parent which must be either a Scsi_Host
800  * or a sas_expander_device.
801  *
802  * Returns %NULL on error
803  */
804 struct sas_port *sas_port_alloc(struct device *parent, int port_id)
805 {
806         struct Scsi_Host *shost = dev_to_shost(parent);
807         struct sas_port *port;
808
809         port = kzalloc(sizeof(*port), GFP_KERNEL);
810         if (!port)
811                 return NULL;
812
813         port->port_identifier = port_id;
814
815         device_initialize(&port->dev);
816
817         port->dev.parent = get_device(parent);
818         port->dev.release = sas_port_release;
819
820         mutex_init(&port->phy_list_mutex);
821         INIT_LIST_HEAD(&port->phy_list);
822
823         if (scsi_is_sas_expander_device(parent)) {
824                 struct sas_rphy *rphy = dev_to_rphy(parent);
825                 sprintf(port->dev.bus_id, "port-%d:%d:%d", shost->host_no,
826                         rphy->scsi_target_id, port->port_identifier);
827         } else
828                 sprintf(port->dev.bus_id, "port-%d:%d", shost->host_no,
829                         port->port_identifier);
830
831         transport_setup_device(&port->dev);
832
833         return port;
834 }
835 EXPORT_SYMBOL(sas_port_alloc);
836
837 /** sas_port_alloc_num - allocate and initialize a SAS port structure
838  *
839  * @parent:     parent device
840  *
841  * Allocates a SAS port structure and a number to go with it.  This
842  * interface is really for adapters where the port number has no
843  * meansing, so the sas class should manage them.  It will be added to
844  * the device tree below the device specified by @parent which must be
845  * either a Scsi_Host or a sas_expander_device.
846  *
847  * Returns %NULL on error
848  */
849 struct sas_port *sas_port_alloc_num(struct device *parent)
850 {
851         int index;
852         struct Scsi_Host *shost = dev_to_shost(parent);
853         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
854
855         /* FIXME: use idr for this eventually */
856         mutex_lock(&sas_host->lock);
857         if (scsi_is_sas_expander_device(parent)) {
858                 struct sas_rphy *rphy = dev_to_rphy(parent);
859                 struct sas_expander_device *exp = rphy_to_expander_device(rphy);
860
861                 index = exp->next_port_id++;
862         } else
863                 index = sas_host->next_port_id++;
864         mutex_unlock(&sas_host->lock);
865         return sas_port_alloc(parent, index);
866 }
867 EXPORT_SYMBOL(sas_port_alloc_num);
868
869 /**
870  * sas_port_add - add a SAS port to the device hierarchy
871  * @port:       port to be added
872  *
873  * publishes a port to the rest of the system
874  */
875 int sas_port_add(struct sas_port *port)
876 {
877         int error;
878
879         /* No phys should be added until this is made visible */
880         BUG_ON(!list_empty(&port->phy_list));
881
882         error = device_add(&port->dev);
883
884         if (error)
885                 return error;
886
887         transport_add_device(&port->dev);
888         transport_configure_device(&port->dev);
889
890         return 0;
891 }
892 EXPORT_SYMBOL(sas_port_add);
893
894 /**
895  * sas_port_free  -  free a SAS PORT
896  * @port:       SAS PORT to free
897  *
898  * Frees the specified SAS PORT.
899  *
900  * Note:
901  *   This function must only be called on a PORT that has not
902  *   sucessfully been added using sas_port_add().
903  */
904 void sas_port_free(struct sas_port *port)
905 {
906         transport_destroy_device(&port->dev);
907         put_device(&port->dev);
908 }
909 EXPORT_SYMBOL(sas_port_free);
910
911 /**
912  * sas_port_delete  -  remove SAS PORT
913  * @port:       SAS PORT to remove
914  *
915  * Removes the specified SAS PORT.  If the SAS PORT has an
916  * associated phys, unlink them from the port as well.
917  */
918 void sas_port_delete(struct sas_port *port)
919 {
920         struct device *dev = &port->dev;
921         struct sas_phy *phy, *tmp_phy;
922
923         if (port->rphy) {
924                 sas_rphy_delete(port->rphy);
925                 port->rphy = NULL;
926         }
927
928         mutex_lock(&port->phy_list_mutex);
929         list_for_each_entry_safe(phy, tmp_phy, &port->phy_list,
930                                  port_siblings) {
931                 sas_port_delete_link(port, phy);
932                 list_del_init(&phy->port_siblings);
933         }
934         mutex_unlock(&port->phy_list_mutex);
935
936         if (port->is_backlink) {
937                 struct device *parent = port->dev.parent;
938
939                 sysfs_remove_link(&port->dev.kobj, parent->bus_id);
940                 port->is_backlink = 0;
941         }
942
943         transport_remove_device(dev);
944         device_del(dev);
945         transport_destroy_device(dev);
946         put_device(dev);
947 }
948 EXPORT_SYMBOL(sas_port_delete);
949
950 /**
951  * scsi_is_sas_port -  check if a struct device represents a SAS port
952  * @dev:        device to check
953  *
954  * Returns:
955  *      %1 if the device represents a SAS Port, %0 else
956  */
957 int scsi_is_sas_port(const struct device *dev)
958 {
959         return dev->release == sas_port_release;
960 }
961 EXPORT_SYMBOL(scsi_is_sas_port);
962
963 /**
964  * sas_port_add_phy - add another phy to a port to form a wide port
965  * @port:       port to add the phy to
966  * @phy:        phy to add
967  *
968  * When a port is initially created, it is empty (has no phys).  All
969  * ports must have at least one phy to operated, and all wide ports
970  * must have at least two.  The current code makes no difference
971  * between ports and wide ports, but the only object that can be
972  * connected to a remote device is a port, so ports must be formed on
973  * all devices with phys if they're connected to anything.
974  */
975 void sas_port_add_phy(struct sas_port *port, struct sas_phy *phy)
976 {
977         mutex_lock(&port->phy_list_mutex);
978         if (unlikely(!list_empty(&phy->port_siblings))) {
979                 /* make sure we're already on this port */
980                 struct sas_phy *tmp;
981
982                 list_for_each_entry(tmp, &port->phy_list, port_siblings)
983                         if (tmp == phy)
984                                 break;
985                 /* If this trips, you added a phy that was already
986                  * part of a different port */
987                 if (unlikely(tmp != phy)) {
988                         dev_printk(KERN_ERR, &port->dev, "trying to add phy %s fails: it's already part of another port\n", phy->dev.bus_id);
989                         BUG();
990                 }
991         } else {
992                 sas_port_create_link(port, phy);
993                 list_add_tail(&phy->port_siblings, &port->phy_list);
994                 port->num_phys++;
995         }
996         mutex_unlock(&port->phy_list_mutex);
997 }
998 EXPORT_SYMBOL(sas_port_add_phy);
999
1000 /**
1001  * sas_port_delete_phy - remove a phy from a port or wide port
1002  * @port:       port to remove the phy from
1003  * @phy:        phy to remove
1004  *
1005  * This operation is used for tearing down ports again.  It must be
1006  * done to every port or wide port before calling sas_port_delete.
1007  */
1008 void sas_port_delete_phy(struct sas_port *port, struct sas_phy *phy)
1009 {
1010         mutex_lock(&port->phy_list_mutex);
1011         sas_port_delete_link(port, phy);
1012         list_del_init(&phy->port_siblings);
1013         port->num_phys--;
1014         mutex_unlock(&port->phy_list_mutex);
1015 }
1016 EXPORT_SYMBOL(sas_port_delete_phy);
1017
1018 void sas_port_mark_backlink(struct sas_port *port)
1019 {
1020         int res;
1021         struct device *parent = port->dev.parent->parent->parent;
1022
1023         if (port->is_backlink)
1024                 return;
1025         port->is_backlink = 1;
1026         res = sysfs_create_link(&port->dev.kobj, &parent->kobj,
1027                                 parent->bus_id);
1028         if (res)
1029                 goto err;
1030         return;
1031 err:
1032         printk(KERN_ERR "%s: Cannot create port backlink, err=%d\n",
1033                __FUNCTION__, res);
1034
1035 }
1036 EXPORT_SYMBOL(sas_port_mark_backlink);
1037
1038 /*
1039  * SAS remote PHY attributes.
1040  */
1041
1042 #define sas_rphy_show_simple(field, name, format_string, cast)          \
1043 static ssize_t                                                          \
1044 show_sas_rphy_##name(struct device *dev,                                \
1045                      struct device_attribute *attr, char *buf)          \
1046 {                                                                       \
1047         struct sas_rphy *rphy = transport_class_to_rphy(dev);           \
1048                                                                         \
1049         return snprintf(buf, 20, format_string, cast rphy->field);      \
1050 }
1051
1052 #define sas_rphy_simple_attr(field, name, format_string, type)          \
1053         sas_rphy_show_simple(field, name, format_string, (type))        \
1054 static SAS_DEVICE_ATTR(rphy, name, S_IRUGO,                     \
1055                 show_sas_rphy_##name, NULL)
1056
1057 #define sas_rphy_show_protocol(field, name)                             \
1058 static ssize_t                                                          \
1059 show_sas_rphy_##name(struct device *dev,                                \
1060                      struct device_attribute *attr, char *buf)          \
1061 {                                                                       \
1062         struct sas_rphy *rphy = transport_class_to_rphy(dev);           \
1063                                                                         \
1064         if (!rphy->field)                                       \
1065                 return snprintf(buf, 20, "none\n");                     \
1066         return get_sas_protocol_names(rphy->field, buf);        \
1067 }
1068
1069 #define sas_rphy_protocol_attr(field, name)                             \
1070         sas_rphy_show_protocol(field, name)                             \
1071 static SAS_DEVICE_ATTR(rphy, name, S_IRUGO,                     \
1072                 show_sas_rphy_##name, NULL)
1073
1074 static ssize_t
1075 show_sas_rphy_device_type(struct device *dev,
1076                           struct device_attribute *attr, char *buf)
1077 {
1078         struct sas_rphy *rphy = transport_class_to_rphy(dev);
1079
1080         if (!rphy->identify.device_type)
1081                 return snprintf(buf, 20, "none\n");
1082         return get_sas_device_type_names(
1083                         rphy->identify.device_type, buf);
1084 }
1085
1086 static SAS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
1087                 show_sas_rphy_device_type, NULL);
1088
1089 static ssize_t
1090 show_sas_rphy_enclosure_identifier(struct device *dev,
1091                                    struct device_attribute *attr, char *buf)
1092 {
1093         struct sas_rphy *rphy = transport_class_to_rphy(dev);
1094         struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
1095         struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1096         struct sas_internal *i = to_sas_internal(shost->transportt);
1097         u64 identifier;
1098         int error;
1099
1100         /*
1101          * Only devices behind an expander are supported, because the
1102          * enclosure identifier is a SMP feature.
1103          */
1104         if (scsi_is_sas_phy_local(phy))
1105                 return -EINVAL;
1106
1107         error = i->f->get_enclosure_identifier(rphy, &identifier);
1108         if (error)
1109                 return error;
1110         return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
1111 }
1112
1113 static SAS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
1114                 show_sas_rphy_enclosure_identifier, NULL);
1115
1116 static ssize_t
1117 show_sas_rphy_bay_identifier(struct device *dev,
1118                              struct device_attribute *attr, char *buf)
1119 {
1120         struct sas_rphy *rphy = transport_class_to_rphy(dev);
1121         struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
1122         struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1123         struct sas_internal *i = to_sas_internal(shost->transportt);
1124         int val;
1125
1126         if (scsi_is_sas_phy_local(phy))
1127                 return -EINVAL;
1128
1129         val = i->f->get_bay_identifier(rphy);
1130         if (val < 0)
1131                 return val;
1132         return sprintf(buf, "%d\n", val);
1133 }
1134
1135 static SAS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
1136                 show_sas_rphy_bay_identifier, NULL);
1137
1138 sas_rphy_protocol_attr(identify.initiator_port_protocols,
1139                 initiator_port_protocols);
1140 sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
1141 sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
1142                 unsigned long long);
1143 sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
1144
1145 /* only need 8 bytes of data plus header (4 or 8) */
1146 #define BUF_SIZE 64
1147
1148 int sas_read_port_mode_page(struct scsi_device *sdev)
1149 {
1150         char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata;
1151         struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
1152         struct sas_end_device *rdev;
1153         struct scsi_mode_data mode_data;
1154         int res, error;
1155
1156         BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
1157
1158         rdev = rphy_to_end_device(rphy);
1159
1160         if (!buffer)
1161                 return -ENOMEM;
1162
1163         res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3,
1164                               &mode_data, NULL);
1165
1166         error = -EINVAL;
1167         if (!scsi_status_is_good(res))
1168                 goto out;
1169
1170         msdata = buffer +  mode_data.header_length +
1171                 mode_data.block_descriptor_length;
1172
1173         if (msdata - buffer > BUF_SIZE - 8)
1174                 goto out;
1175
1176         error = 0;
1177
1178         rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0;
1179         rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5];
1180         rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7];
1181
1182  out:
1183         kfree(buffer);
1184         return error;
1185 }
1186 EXPORT_SYMBOL(sas_read_port_mode_page);
1187
1188 static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
1189                                "sas_end_device", NULL, NULL, NULL);
1190
1191 #define sas_end_dev_show_simple(field, name, format_string, cast)       \
1192 static ssize_t                                                          \
1193 show_sas_end_dev_##name(struct device *dev,                             \
1194                         struct device_attribute *attr, char *buf)       \
1195 {                                                                       \
1196         struct sas_rphy *rphy = transport_class_to_rphy(dev);           \
1197         struct sas_end_device *rdev = rphy_to_end_device(rphy);         \
1198                                                                         \
1199         return snprintf(buf, 20, format_string, cast rdev->field);      \
1200 }
1201
1202 #define sas_end_dev_simple_attr(field, name, format_string, type)       \
1203         sas_end_dev_show_simple(field, name, format_string, (type))     \
1204 static SAS_DEVICE_ATTR(end_dev, name, S_IRUGO,                  \
1205                 show_sas_end_dev_##name, NULL)
1206
1207 sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int);
1208 sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout,
1209                         "%d\n", int);
1210 sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout,
1211                         "%d\n", int);
1212
1213 static DECLARE_TRANSPORT_CLASS(sas_expander_class,
1214                                "sas_expander", NULL, NULL, NULL);
1215
1216 #define sas_expander_show_simple(field, name, format_string, cast)      \
1217 static ssize_t                                                          \
1218 show_sas_expander_##name(struct device *dev,                            \
1219                          struct device_attribute *attr, char *buf)      \
1220 {                                                                       \
1221         struct sas_rphy *rphy = transport_class_to_rphy(dev);           \
1222         struct sas_expander_device *edev = rphy_to_expander_device(rphy); \
1223                                                                         \
1224         return snprintf(buf, 20, format_string, cast edev->field);      \
1225 }
1226
1227 #define sas_expander_simple_attr(field, name, format_string, type)      \
1228         sas_expander_show_simple(field, name, format_string, (type))    \
1229 static SAS_DEVICE_ATTR(expander, name, S_IRUGO,                         \
1230                 show_sas_expander_##name, NULL)
1231
1232 sas_expander_simple_attr(vendor_id, vendor_id, "%s\n", char *);
1233 sas_expander_simple_attr(product_id, product_id, "%s\n", char *);
1234 sas_expander_simple_attr(product_rev, product_rev, "%s\n", char *);
1235 sas_expander_simple_attr(component_vendor_id, component_vendor_id,
1236                          "%s\n", char *);
1237 sas_expander_simple_attr(component_id, component_id, "%u\n", unsigned int);
1238 sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n",
1239                          unsigned int);
1240 sas_expander_simple_attr(level, level, "%d\n", int);
1241
1242 static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
1243                 "sas_device", NULL, NULL, NULL);
1244
1245 static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
1246 {
1247         struct Scsi_Host *shost;
1248         struct sas_internal *i;
1249
1250         if (!scsi_is_sas_rphy(dev))
1251                 return 0;
1252         shost = dev_to_shost(dev->parent->parent);
1253
1254         if (!shost->transportt)
1255                 return 0;
1256         if (shost->transportt->host_attrs.ac.class !=
1257                         &sas_host_class.class)
1258                 return 0;
1259
1260         i = to_sas_internal(shost->transportt);
1261         return &i->rphy_attr_cont.ac == cont;
1262 }
1263
1264 static int sas_end_dev_match(struct attribute_container *cont,
1265                              struct device *dev)
1266 {
1267         struct Scsi_Host *shost;
1268         struct sas_internal *i;
1269         struct sas_rphy *rphy;
1270
1271         if (!scsi_is_sas_rphy(dev))
1272                 return 0;
1273         shost = dev_to_shost(dev->parent->parent);
1274         rphy = dev_to_rphy(dev);
1275
1276         if (!shost->transportt)
1277                 return 0;
1278         if (shost->transportt->host_attrs.ac.class !=
1279                         &sas_host_class.class)
1280                 return 0;
1281
1282         i = to_sas_internal(shost->transportt);
1283         return &i->end_dev_attr_cont.ac == cont &&
1284                 rphy->identify.device_type == SAS_END_DEVICE;
1285 }
1286
1287 static int sas_expander_match(struct attribute_container *cont,
1288                               struct device *dev)
1289 {
1290         struct Scsi_Host *shost;
1291         struct sas_internal *i;
1292         struct sas_rphy *rphy;
1293
1294         if (!scsi_is_sas_rphy(dev))
1295                 return 0;
1296         shost = dev_to_shost(dev->parent->parent);
1297         rphy = dev_to_rphy(dev);
1298
1299         if (!shost->transportt)
1300                 return 0;
1301         if (shost->transportt->host_attrs.ac.class !=
1302                         &sas_host_class.class)
1303                 return 0;
1304
1305         i = to_sas_internal(shost->transportt);
1306         return &i->expander_attr_cont.ac == cont &&
1307                 (rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
1308                  rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE);
1309 }
1310
1311 static void sas_expander_release(struct device *dev)
1312 {
1313         struct sas_rphy *rphy = dev_to_rphy(dev);
1314         struct sas_expander_device *edev = rphy_to_expander_device(rphy);
1315
1316         if (rphy->q)
1317                 blk_cleanup_queue(rphy->q);
1318
1319         put_device(dev->parent);
1320         kfree(edev);
1321 }
1322
1323 static void sas_end_device_release(struct device *dev)
1324 {
1325         struct sas_rphy *rphy = dev_to_rphy(dev);
1326         struct sas_end_device *edev = rphy_to_end_device(rphy);
1327
1328         if (rphy->q)
1329                 blk_cleanup_queue(rphy->q);
1330
1331         put_device(dev->parent);
1332         kfree(edev);
1333 }
1334
1335 /**
1336  * sas_rphy_initialize - common rphy intialization
1337  * @rphy:       rphy to initialise
1338  *
1339  * Used by both sas_end_device_alloc() and sas_expander_alloc() to
1340  * initialise the common rphy component of each.
1341  */
1342 static void sas_rphy_initialize(struct sas_rphy *rphy)
1343 {
1344         INIT_LIST_HEAD(&rphy->list);
1345 }
1346
1347 /**
1348  * sas_end_device_alloc - allocate an rphy for an end device
1349  * @parent: which port
1350  *
1351  * Allocates an SAS remote PHY structure, connected to @parent.
1352  *
1353  * Returns:
1354  *      SAS PHY allocated or %NULL if the allocation failed.
1355  */
1356 struct sas_rphy *sas_end_device_alloc(struct sas_port *parent)
1357 {
1358         struct Scsi_Host *shost = dev_to_shost(&parent->dev);
1359         struct sas_end_device *rdev;
1360
1361         rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1362         if (!rdev) {
1363                 return NULL;
1364         }
1365
1366         device_initialize(&rdev->rphy.dev);
1367         rdev->rphy.dev.parent = get_device(&parent->dev);
1368         rdev->rphy.dev.release = sas_end_device_release;
1369         if (scsi_is_sas_expander_device(parent->dev.parent)) {
1370                 struct sas_rphy *rphy = dev_to_rphy(parent->dev.parent);
1371                 sprintf(rdev->rphy.dev.bus_id, "end_device-%d:%d:%d",
1372                         shost->host_no, rphy->scsi_target_id, parent->port_identifier);
1373         } else
1374                 sprintf(rdev->rphy.dev.bus_id, "end_device-%d:%d",
1375                         shost->host_no, parent->port_identifier);
1376         rdev->rphy.identify.device_type = SAS_END_DEVICE;
1377         sas_rphy_initialize(&rdev->rphy);
1378         transport_setup_device(&rdev->rphy.dev);
1379
1380         return &rdev->rphy;
1381 }
1382 EXPORT_SYMBOL(sas_end_device_alloc);
1383
1384 /**
1385  * sas_expander_alloc - allocate an rphy for an end device
1386  * @parent: which port
1387  * @type: SAS_EDGE_EXPANDER_DEVICE or SAS_FANOUT_EXPANDER_DEVICE
1388  *
1389  * Allocates an SAS remote PHY structure, connected to @parent.
1390  *
1391  * Returns:
1392  *      SAS PHY allocated or %NULL if the allocation failed.
1393  */
1394 struct sas_rphy *sas_expander_alloc(struct sas_port *parent,
1395                                     enum sas_device_type type)
1396 {
1397         struct Scsi_Host *shost = dev_to_shost(&parent->dev);
1398         struct sas_expander_device *rdev;
1399         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1400
1401         BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE &&
1402                type != SAS_FANOUT_EXPANDER_DEVICE);
1403
1404         rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1405         if (!rdev) {
1406                 return NULL;
1407         }
1408
1409         device_initialize(&rdev->rphy.dev);
1410         rdev->rphy.dev.parent = get_device(&parent->dev);
1411         rdev->rphy.dev.release = sas_expander_release;
1412         mutex_lock(&sas_host->lock);
1413         rdev->rphy.scsi_target_id = sas_host->next_expander_id++;
1414         mutex_unlock(&sas_host->lock);
1415         sprintf(rdev->rphy.dev.bus_id, "expander-%d:%d",
1416                 shost->host_no, rdev->rphy.scsi_target_id);
1417         rdev->rphy.identify.device_type = type;
1418         sas_rphy_initialize(&rdev->rphy);
1419         transport_setup_device(&rdev->rphy.dev);
1420
1421         return &rdev->rphy;
1422 }
1423 EXPORT_SYMBOL(sas_expander_alloc);
1424
1425 /**
1426  * sas_rphy_add  -  add a SAS remote PHY to the device hierarchy
1427  * @rphy:       The remote PHY to be added
1428  *
1429  * Publishes a SAS remote PHY to the rest of the system.
1430  */
1431 int sas_rphy_add(struct sas_rphy *rphy)
1432 {
1433         struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
1434         struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
1435         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1436         struct sas_identify *identify = &rphy->identify;
1437         int error;
1438
1439         if (parent->rphy)
1440                 return -ENXIO;
1441         parent->rphy = rphy;
1442
1443         error = device_add(&rphy->dev);
1444         if (error)
1445                 return error;
1446         transport_add_device(&rphy->dev);
1447         transport_configure_device(&rphy->dev);
1448         if (sas_bsg_initialize(shost, rphy))
1449                 printk("fail to a bsg device %s\n", rphy->dev.bus_id);
1450
1451
1452         mutex_lock(&sas_host->lock);
1453         list_add_tail(&rphy->list, &sas_host->rphy_list);
1454         if (identify->device_type == SAS_END_DEVICE &&
1455             (identify->target_port_protocols &
1456              (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
1457                 rphy->scsi_target_id = sas_host->next_target_id++;
1458         else if (identify->device_type == SAS_END_DEVICE)
1459                 rphy->scsi_target_id = -1;
1460         mutex_unlock(&sas_host->lock);
1461
1462         if (identify->device_type == SAS_END_DEVICE &&
1463             rphy->scsi_target_id != -1) {
1464                 scsi_scan_target(&rphy->dev, 0,
1465                                 rphy->scsi_target_id, SCAN_WILD_CARD, 0);
1466         }
1467
1468         return 0;
1469 }
1470 EXPORT_SYMBOL(sas_rphy_add);
1471
1472 /**
1473  * sas_rphy_free  -  free a SAS remote PHY
1474  * @rphy: SAS remote PHY to free
1475  *
1476  * Frees the specified SAS remote PHY.
1477  *
1478  * Note:
1479  *   This function must only be called on a remote
1480  *   PHY that has not sucessfully been added using
1481  *   sas_rphy_add() (or has been sas_rphy_remove()'d)
1482  */
1483 void sas_rphy_free(struct sas_rphy *rphy)
1484 {
1485         struct device *dev = &rphy->dev;
1486         struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
1487         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1488
1489         mutex_lock(&sas_host->lock);
1490         list_del(&rphy->list);
1491         mutex_unlock(&sas_host->lock);
1492
1493         sas_bsg_remove(shost, rphy);
1494
1495         transport_destroy_device(dev);
1496
1497         put_device(dev);
1498 }
1499 EXPORT_SYMBOL(sas_rphy_free);
1500
1501 /**
1502  * sas_rphy_delete  -  remove and free SAS remote PHY
1503  * @rphy:       SAS remote PHY to remove and free
1504  *
1505  * Removes the specified SAS remote PHY and frees it.
1506  */
1507 void
1508 sas_rphy_delete(struct sas_rphy *rphy)
1509 {
1510         sas_rphy_remove(rphy);
1511         sas_rphy_free(rphy);
1512 }
1513 EXPORT_SYMBOL(sas_rphy_delete);
1514
1515 /**
1516  * sas_rphy_remove  -  remove SAS remote PHY
1517  * @rphy:       SAS remote phy to remove
1518  *
1519  * Removes the specified SAS remote PHY.
1520  */
1521 void
1522 sas_rphy_remove(struct sas_rphy *rphy)
1523 {
1524         struct device *dev = &rphy->dev;
1525         struct sas_port *parent = dev_to_sas_port(dev->parent);
1526
1527         switch (rphy->identify.device_type) {
1528         case SAS_END_DEVICE:
1529                 scsi_remove_target(dev);
1530                 break;
1531         case SAS_EDGE_EXPANDER_DEVICE:
1532         case SAS_FANOUT_EXPANDER_DEVICE:
1533                 sas_remove_children(dev);
1534                 break;
1535         default:
1536                 break;
1537         }
1538
1539         transport_remove_device(dev);
1540         device_del(dev);
1541
1542         parent->rphy = NULL;
1543 }
1544 EXPORT_SYMBOL(sas_rphy_remove);
1545
1546 /**
1547  * scsi_is_sas_rphy  -  check if a struct device represents a SAS remote PHY
1548  * @dev:        device to check
1549  *
1550  * Returns:
1551  *      %1 if the device represents a SAS remote PHY, %0 else
1552  */
1553 int scsi_is_sas_rphy(const struct device *dev)
1554 {
1555         return dev->release == sas_end_device_release ||
1556                 dev->release == sas_expander_release;
1557 }
1558 EXPORT_SYMBOL(scsi_is_sas_rphy);
1559
1560
1561 /*
1562  * SCSI scan helper
1563  */
1564
1565 static int sas_user_scan(struct Scsi_Host *shost, uint channel,
1566                 uint id, uint lun)
1567 {
1568         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1569         struct sas_rphy *rphy;
1570
1571         mutex_lock(&sas_host->lock);
1572         list_for_each_entry(rphy, &sas_host->rphy_list, list) {
1573                 if (rphy->identify.device_type != SAS_END_DEVICE ||
1574                     rphy->scsi_target_id == -1)
1575                         continue;
1576
1577                 if ((channel == SCAN_WILD_CARD || channel == 0) &&
1578                     (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
1579                         scsi_scan_target(&rphy->dev, 0,
1580                                          rphy->scsi_target_id, lun, 1);
1581                 }
1582         }
1583         mutex_unlock(&sas_host->lock);
1584
1585         return 0;
1586 }
1587
1588
1589 /*
1590  * Setup / Teardown code
1591  */
1592
1593 #define SETUP_TEMPLATE(attrb, field, perm, test)                        \
1594         i->private_##attrb[count] = dev_attr_##field;           \
1595         i->private_##attrb[count].attr.mode = perm;                     \
1596         i->attrb[count] = &i->private_##attrb[count];                   \
1597         if (test)                                                       \
1598                 count++
1599
1600 #define SETUP_TEMPLATE_RW(attrb, field, perm, test, ro_test, ro_perm)   \
1601         i->private_##attrb[count] = dev_attr_##field;           \
1602         i->private_##attrb[count].attr.mode = perm;                     \
1603         if (ro_test) {                                                  \
1604                 i->private_##attrb[count].attr.mode = ro_perm;          \
1605                 i->private_##attrb[count].store = NULL;                 \
1606         }                                                               \
1607         i->attrb[count] = &i->private_##attrb[count];                   \
1608         if (test)                                                       \
1609                 count++
1610
1611 #define SETUP_RPORT_ATTRIBUTE(field)                                    \
1612         SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
1613
1614 #define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func)                     \
1615         SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
1616
1617 #define SETUP_PHY_ATTRIBUTE(field)                                      \
1618         SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
1619
1620 #define SETUP_PHY_ATTRIBUTE_RW(field)                                   \
1621         SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1,       \
1622                         !i->f->set_phy_speed, S_IRUGO)
1623
1624 #define SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(field, func)                    \
1625         SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1,       \
1626                           !i->f->func, S_IRUGO)
1627
1628 #define SETUP_PORT_ATTRIBUTE(field)                                     \
1629         SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
1630
1631 #define SETUP_OPTIONAL_PHY_ATTRIBUTE(field, func)                       \
1632         SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
1633
1634 #define SETUP_PHY_ATTRIBUTE_WRONLY(field)                               \
1635         SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, 1)
1636
1637 #define SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(field, func)                \
1638         SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, i->f->func)
1639
1640 #define SETUP_END_DEV_ATTRIBUTE(field)                                  \
1641         SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
1642
1643 #define SETUP_EXPANDER_ATTRIBUTE(field)                                 \
1644         SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1)
1645
1646 /**
1647  * sas_attach_transport  -  instantiate SAS transport template
1648  * @ft:         SAS transport class function template
1649  */
1650 struct scsi_transport_template *
1651 sas_attach_transport(struct sas_function_template *ft)
1652 {
1653         struct sas_internal *i;
1654         int count;
1655
1656         i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
1657         if (!i)
1658                 return NULL;
1659
1660         i->t.user_scan = sas_user_scan;
1661
1662         i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1663         i->t.host_attrs.ac.class = &sas_host_class.class;
1664         i->t.host_attrs.ac.match = sas_host_match;
1665         transport_container_register(&i->t.host_attrs);
1666         i->t.host_size = sizeof(struct sas_host_attrs);
1667
1668         i->phy_attr_cont.ac.class = &sas_phy_class.class;
1669         i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
1670         i->phy_attr_cont.ac.match = sas_phy_match;
1671         transport_container_register(&i->phy_attr_cont);
1672
1673         i->port_attr_cont.ac.class = &sas_port_class.class;
1674         i->port_attr_cont.ac.attrs = &i->port_attrs[0];
1675         i->port_attr_cont.ac.match = sas_port_match;
1676         transport_container_register(&i->port_attr_cont);
1677
1678         i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
1679         i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
1680         i->rphy_attr_cont.ac.match = sas_rphy_match;
1681         transport_container_register(&i->rphy_attr_cont);
1682
1683         i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class;
1684         i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0];
1685         i->end_dev_attr_cont.ac.match = sas_end_dev_match;
1686         transport_container_register(&i->end_dev_attr_cont);
1687
1688         i->expander_attr_cont.ac.class = &sas_expander_class.class;
1689         i->expander_attr_cont.ac.attrs = &i->expander_attrs[0];
1690         i->expander_attr_cont.ac.match = sas_expander_match;
1691         transport_container_register(&i->expander_attr_cont);
1692
1693         i->f = ft;
1694
1695         count = 0;
1696         SETUP_PORT_ATTRIBUTE(num_phys);
1697         i->host_attrs[count] = NULL;
1698
1699         count = 0;
1700         SETUP_PHY_ATTRIBUTE(initiator_port_protocols);
1701         SETUP_PHY_ATTRIBUTE(target_port_protocols);
1702         SETUP_PHY_ATTRIBUTE(device_type);
1703         SETUP_PHY_ATTRIBUTE(sas_address);
1704         SETUP_PHY_ATTRIBUTE(phy_identifier);
1705         //SETUP_PHY_ATTRIBUTE(port_identifier);
1706         SETUP_PHY_ATTRIBUTE(negotiated_linkrate);
1707         SETUP_PHY_ATTRIBUTE(minimum_linkrate_hw);
1708         SETUP_PHY_ATTRIBUTE_RW(minimum_linkrate);
1709         SETUP_PHY_ATTRIBUTE(maximum_linkrate_hw);
1710         SETUP_PHY_ATTRIBUTE_RW(maximum_linkrate);
1711
1712         SETUP_PHY_ATTRIBUTE(invalid_dword_count);
1713         SETUP_PHY_ATTRIBUTE(running_disparity_error_count);
1714         SETUP_PHY_ATTRIBUTE(loss_of_dword_sync_count);
1715         SETUP_PHY_ATTRIBUTE(phy_reset_problem_count);
1716         SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(link_reset, phy_reset);
1717         SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
1718         SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(enable, phy_enable);
1719         i->phy_attrs[count] = NULL;
1720
1721         count = 0;
1722         SETUP_PORT_ATTRIBUTE(num_phys);
1723         i->port_attrs[count] = NULL;
1724
1725         count = 0;
1726         SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
1727         SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
1728         SETUP_RPORT_ATTRIBUTE(rphy_device_type);
1729         SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
1730         SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
1731         SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
1732                                        get_enclosure_identifier);
1733         SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
1734                                        get_bay_identifier);
1735         i->rphy_attrs[count] = NULL;
1736
1737         count = 0;
1738         SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning);
1739         SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout);
1740         SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout);
1741         i->end_dev_attrs[count] = NULL;
1742
1743         count = 0;
1744         SETUP_EXPANDER_ATTRIBUTE(vendor_id);
1745         SETUP_EXPANDER_ATTRIBUTE(product_id);
1746         SETUP_EXPANDER_ATTRIBUTE(product_rev);
1747         SETUP_EXPANDER_ATTRIBUTE(component_vendor_id);
1748         SETUP_EXPANDER_ATTRIBUTE(component_id);
1749         SETUP_EXPANDER_ATTRIBUTE(component_revision_id);
1750         SETUP_EXPANDER_ATTRIBUTE(level);
1751         i->expander_attrs[count] = NULL;
1752
1753         return &i->t;
1754 }
1755 EXPORT_SYMBOL(sas_attach_transport);
1756
1757 /**
1758  * sas_release_transport  -  release SAS transport template instance
1759  * @t:          transport template instance
1760  */
1761 void sas_release_transport(struct scsi_transport_template *t)
1762 {
1763         struct sas_internal *i = to_sas_internal(t);
1764
1765         transport_container_unregister(&i->t.host_attrs);
1766         transport_container_unregister(&i->phy_attr_cont);
1767         transport_container_unregister(&i->port_attr_cont);
1768         transport_container_unregister(&i->rphy_attr_cont);
1769         transport_container_unregister(&i->end_dev_attr_cont);
1770         transport_container_unregister(&i->expander_attr_cont);
1771
1772         kfree(i);
1773 }
1774 EXPORT_SYMBOL(sas_release_transport);
1775
1776 static __init int sas_transport_init(void)
1777 {
1778         int error;
1779
1780         error = transport_class_register(&sas_host_class);
1781         if (error)
1782                 goto out;
1783         error = transport_class_register(&sas_phy_class);
1784         if (error)
1785                 goto out_unregister_transport;
1786         error = transport_class_register(&sas_port_class);
1787         if (error)
1788                 goto out_unregister_phy;
1789         error = transport_class_register(&sas_rphy_class);
1790         if (error)
1791                 goto out_unregister_port;
1792         error = transport_class_register(&sas_end_dev_class);
1793         if (error)
1794                 goto out_unregister_rphy;
1795         error = transport_class_register(&sas_expander_class);
1796         if (error)
1797                 goto out_unregister_end_dev;
1798
1799         return 0;
1800
1801  out_unregister_end_dev:
1802         transport_class_unregister(&sas_end_dev_class);
1803  out_unregister_rphy:
1804         transport_class_unregister(&sas_rphy_class);
1805  out_unregister_port:
1806         transport_class_unregister(&sas_port_class);
1807  out_unregister_phy:
1808         transport_class_unregister(&sas_phy_class);
1809  out_unregister_transport:
1810         transport_class_unregister(&sas_host_class);
1811  out:
1812         return error;
1813
1814 }
1815
1816 static void __exit sas_transport_exit(void)
1817 {
1818         transport_class_unregister(&sas_host_class);
1819         transport_class_unregister(&sas_phy_class);
1820         transport_class_unregister(&sas_port_class);
1821         transport_class_unregister(&sas_rphy_class);
1822         transport_class_unregister(&sas_end_dev_class);
1823         transport_class_unregister(&sas_expander_class);
1824 }
1825
1826 MODULE_AUTHOR("Christoph Hellwig");
1827 MODULE_DESCRIPTION("SAS Transport Attributes");
1828 MODULE_LICENSE("GPL");
1829
1830 module_init(sas_transport_init);
1831 module_exit(sas_transport_exit);