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