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