blob: 5006a656e16a775b055d0c94b8809577ee34fcb3 [file] [log] [blame]
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001/*
Christoph Hellwiga0125642006-02-16 13:31:47 +01002 * Copyright (C) 2005-2006 Dell Inc.
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02003 * 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,
Joe Perchesb1c11812008-02-03 17:28:22 +02009 * and various sysfs attributes to expose these topologies and management
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020010 * 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>
Al Virof6a57032006-10-18 01:47:25 -040028#include <linux/jiffies.h>
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020029#include <linux/err.h>
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080030#include <linux/slab.h>
31#include <linux/string.h>
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +090032#include <linux/blkdev.h>
33#include <linux/bsg.h>
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020034
Christoph Hellwige02f3f52006-01-13 19:04:00 +010035#include <scsi/scsi.h>
Bart Van Asscheca18d6f2017-06-20 11:15:41 -070036#include <scsi/scsi_cmnd.h>
Christoph Hellwig82ed4db2017-01-27 09:46:29 +010037#include <scsi/scsi_request.h>
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020038#include <scsi/scsi_device.h>
39#include <scsi/scsi_host.h>
40#include <scsi/scsi_transport.h>
41#include <scsi/scsi_transport_sas.h>
42
James Bottomleyd6159c12006-03-27 16:45:34 -060043#include "scsi_sas_internal.h"
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020044struct sas_host_attrs {
45 struct list_head rphy_list;
Christoph Hellwige02f3f52006-01-13 19:04:00 +010046 struct mutex lock;
James Bottomleyb6aff662007-07-20 11:10:05 -050047 struct request_queue *q;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020048 u32 next_target_id;
James Bottomley79cb1812006-03-13 13:50:04 -060049 u32 next_expander_id;
James Bottomleyc9fefeb2006-07-02 11:10:18 -050050 int next_port_id;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020051};
52#define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data)
53
54
55/*
56 * Hack to allow attributes of the same name in different objects.
57 */
Tony Jonesee959b02008-02-22 00:13:36 +010058#define SAS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
59 struct device_attribute dev_attr_##_prefix##_##_name = \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020060 __ATTR(_name,_mode,_show,_store)
61
62
63/*
64 * Pretty printing helpers
65 */
66
67#define sas_bitfield_name_match(title, table) \
68static ssize_t \
69get_sas_##title##_names(u32 table_key, char *buf) \
70{ \
71 char *prefix = ""; \
72 ssize_t len = 0; \
73 int i; \
74 \
Tobias Klauser6391a112006-06-08 22:23:48 -070075 for (i = 0; i < ARRAY_SIZE(table); i++) { \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020076 if (table[i].value & table_key) { \
77 len += sprintf(buf + len, "%s%s", \
78 prefix, table[i].name); \
79 prefix = ", "; \
80 } \
81 } \
82 len += sprintf(buf + len, "\n"); \
83 return len; \
84}
85
James Bottomleyd24e1ee2006-09-06 19:25:22 -050086#define sas_bitfield_name_set(title, table) \
87static ssize_t \
88set_sas_##title##_names(u32 *table_key, const char *buf) \
89{ \
90 ssize_t len = 0; \
91 int i; \
92 \
93 for (i = 0; i < ARRAY_SIZE(table); i++) { \
94 len = strlen(table[i].name); \
95 if (strncmp(buf, table[i].name, len) == 0 && \
96 (buf[len] == '\n' || buf[len] == '\0')) { \
97 *table_key = table[i].value; \
98 return 0; \
99 } \
100 } \
101 return -EINVAL; \
102}
103
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200104#define sas_bitfield_name_search(title, table) \
105static ssize_t \
106get_sas_##title##_names(u32 table_key, char *buf) \
107{ \
108 ssize_t len = 0; \
109 int i; \
110 \
Tobias Klauser6391a112006-06-08 22:23:48 -0700111 for (i = 0; i < ARRAY_SIZE(table); i++) { \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200112 if (table[i].value == table_key) { \
113 len += sprintf(buf + len, "%s", \
114 table[i].name); \
115 break; \
116 } \
117 } \
118 len += sprintf(buf + len, "\n"); \
119 return len; \
120}
121
122static struct {
123 u32 value;
124 char *name;
125} sas_device_type_names[] = {
126 { SAS_PHY_UNUSED, "unused" },
127 { SAS_END_DEVICE, "end device" },
128 { SAS_EDGE_EXPANDER_DEVICE, "edge expander" },
129 { SAS_FANOUT_EXPANDER_DEVICE, "fanout expander" },
130};
131sas_bitfield_name_search(device_type, sas_device_type_names)
132
133
134static struct {
135 u32 value;
136 char *name;
137} sas_protocol_names[] = {
138 { SAS_PROTOCOL_SATA, "sata" },
139 { SAS_PROTOCOL_SMP, "smp" },
140 { SAS_PROTOCOL_STP, "stp" },
141 { SAS_PROTOCOL_SSP, "ssp" },
142};
143sas_bitfield_name_match(protocol, sas_protocol_names)
144
145static struct {
146 u32 value;
147 char *name;
148} sas_linkspeed_names[] = {
149 { SAS_LINK_RATE_UNKNOWN, "Unknown" },
150 { SAS_PHY_DISABLED, "Phy disabled" },
151 { SAS_LINK_RATE_FAILED, "Link Rate failed" },
152 { SAS_SATA_SPINUP_HOLD, "Spin-up hold" },
153 { SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" },
154 { SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" },
James Bottomley7e6dff62006-03-02 14:12:56 -0600155 { SAS_LINK_RATE_6_0_GBPS, "6.0 Gbit" },
Sreekanth Reddyd84fd392012-11-30 07:43:11 +0530156 { SAS_LINK_RATE_12_0_GBPS, "12.0 Gbit" },
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200157};
158sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
James Bottomleyd24e1ee2006-09-06 19:25:22 -0500159sas_bitfield_name_set(linkspeed, sas_linkspeed_names)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200160
James Bottomley0f880092010-01-18 10:14:51 -0600161static struct sas_end_device *sas_sdev_to_rdev(struct scsi_device *sdev)
162{
163 struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
164 struct sas_end_device *rdev;
165
166 BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
167
168 rdev = rphy_to_end_device(rphy);
169 return rdev;
170}
171
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900172static void sas_smp_request(struct request_queue *q, struct Scsi_Host *shost,
173 struct sas_rphy *rphy)
174{
175 struct request *req;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200176 blk_status_t ret;
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900177 int (*handler)(struct Scsi_Host *, struct sas_rphy *, struct request *);
178
Jens Axboe7eaceac2011-03-10 08:52:07 +0100179 while ((req = blk_fetch_request(q)) != NULL) {
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900180 spin_unlock_irq(q->queue_lock);
181
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100182 scsi_req(req)->resid_len = blk_rq_bytes(req);
183 if (req->next_rq)
184 scsi_req(req->next_rq)->resid_len =
185 blk_rq_bytes(req->next_rq);
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900186 handler = to_sas_internal(shost->transportt)->f->smp_handler;
187 ret = handler(shost, rphy, req);
Christoph Hellwig17d53632017-04-20 16:03:01 +0200188 scsi_req(req)->result = ret;
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900189
Christoph Hellwigd188b902017-04-26 09:34:20 +0200190 blk_end_request_all(req, 0);
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900191
FUJITA Tomonori93bdcba2009-06-17 15:10:10 +0900192 spin_lock_irq(q->queue_lock);
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900193 }
194}
195
196static void sas_host_smp_request(struct request_queue *q)
197{
198 sas_smp_request(q, (struct Scsi_Host *)q->queuedata, NULL);
199}
200
201static void sas_non_host_smp_request(struct request_queue *q)
202{
203 struct sas_rphy *rphy = q->queuedata;
204 sas_smp_request(q, rphy_to_shost(rphy), rphy);
205}
206
FUJITA Tomonori93c20a52008-04-19 00:43:15 +0900207static void sas_host_release(struct device *dev)
208{
209 struct Scsi_Host *shost = dev_to_shost(dev);
210 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
211 struct request_queue *q = sas_host->q;
212
213 if (q)
214 blk_cleanup_queue(q);
215}
216
James Bottomley39dca552007-07-20 18:22:17 -0500217static int sas_bsg_initialize(struct Scsi_Host *shost, struct sas_rphy *rphy)
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900218{
219 struct request_queue *q;
220 int error;
James Bottomley39dca552007-07-20 18:22:17 -0500221 struct device *dev;
Kay Sievers71610f52008-12-03 22:41:36 +0100222 char namebuf[20];
James Bottomley39dca552007-07-20 18:22:17 -0500223 const char *name;
FUJITA Tomonori93c20a52008-04-19 00:43:15 +0900224 void (*release)(struct device *);
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900225
226 if (!to_sas_internal(shost->transportt)->f->smp_handler) {
227 printk("%s can't handle SMP requests\n", shost->hostt->name);
228 return 0;
229 }
230
Omar Sandovalbd1599d2017-02-21 10:03:50 -0800231 q = blk_alloc_queue(GFP_KERNEL);
232 if (!q)
233 return -ENOMEM;
Bart Van Asscheca18d6f2017-06-20 11:15:41 -0700234 q->initialize_rq_fn = scsi_initialize_rq;
Omar Sandovalbd1599d2017-02-21 10:03:50 -0800235 q->cmd_size = sizeof(struct scsi_request);
236
James Bottomley39dca552007-07-20 18:22:17 -0500237 if (rphy) {
Omar Sandovalbd1599d2017-02-21 10:03:50 -0800238 q->request_fn = sas_non_host_smp_request;
James Bottomley39dca552007-07-20 18:22:17 -0500239 dev = &rphy->dev;
Kay Sievers71610f52008-12-03 22:41:36 +0100240 name = dev_name(dev);
FUJITA Tomonori93c20a52008-04-19 00:43:15 +0900241 release = NULL;
James Bottomley39dca552007-07-20 18:22:17 -0500242 } else {
Omar Sandovalbd1599d2017-02-21 10:03:50 -0800243 q->request_fn = sas_host_smp_request;
James Bottomley39dca552007-07-20 18:22:17 -0500244 dev = &shost->shost_gendev;
245 snprintf(namebuf, sizeof(namebuf),
246 "sas_host%d", shost->host_no);
247 name = namebuf;
FUJITA Tomonori93c20a52008-04-19 00:43:15 +0900248 release = sas_host_release;
James Bottomley39dca552007-07-20 18:22:17 -0500249 }
Omar Sandovalbd1599d2017-02-21 10:03:50 -0800250 error = blk_init_allocated_queue(q);
251 if (error)
252 goto out_cleanup_queue;
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900253
Christoph Hellwig0bf65952017-06-19 09:26:25 +0200254 /*
255 * by default assume old behaviour and bounce for any highmem page
256 */
257 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
258
FUJITA Tomonori93c20a52008-04-19 00:43:15 +0900259 error = bsg_register_queue(q, dev, name, release);
Omar Sandovalbd1599d2017-02-21 10:03:50 -0800260 if (error)
261 goto out_cleanup_queue;
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900262
263 if (rphy)
James Bottomleyb6aff662007-07-20 11:10:05 -0500264 rphy->q = q;
265 else
266 to_sas_host_attrs(shost)->q = q;
267
268 if (rphy)
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900269 q->queuedata = rphy;
270 else
271 q->queuedata = shost;
272
Nick Piggin75ad23b2008-04-29 14:48:33 +0200273 queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
Bart Van Assche9efc1602017-05-31 14:43:46 -0700274 queue_flag_set_unlocked(QUEUE_FLAG_SCSI_PASSTHROUGH, q);
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900275 return 0;
Omar Sandovalbd1599d2017-02-21 10:03:50 -0800276
277out_cleanup_queue:
278 blk_cleanup_queue(q);
279 return error;
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900280}
281
James Bottomleyb6aff662007-07-20 11:10:05 -0500282static void sas_bsg_remove(struct Scsi_Host *shost, struct sas_rphy *rphy)
283{
284 struct request_queue *q;
285
286 if (rphy)
287 q = rphy->q;
288 else
289 q = to_sas_host_attrs(shost)->q;
290
291 if (!q)
292 return;
293
294 bsg_unregister_queue(q);
James Bottomleyb6aff662007-07-20 11:10:05 -0500295}
296
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200297/*
298 * SAS host attributes
299 */
300
James Bottomley37be6ee2005-09-09 18:38:27 -0500301static int sas_host_setup(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100302 struct device *cdev)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200303{
304 struct Scsi_Host *shost = dev_to_shost(dev);
305 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
306
307 INIT_LIST_HEAD(&sas_host->rphy_list);
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100308 mutex_init(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200309 sas_host->next_target_id = 0;
James Bottomley79cb1812006-03-13 13:50:04 -0600310 sas_host->next_expander_id = 0;
James Bottomleyc9fefeb2006-07-02 11:10:18 -0500311 sas_host->next_port_id = 0;
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900312
James Bottomley39dca552007-07-20 18:22:17 -0500313 if (sas_bsg_initialize(shost, NULL))
FUJITA Tomonori7aa68e82007-07-09 12:52:06 +0900314 dev_printk(KERN_ERR, dev, "fail to a bsg device %d\n",
315 shost->host_no);
316
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200317 return 0;
318}
319
James Bottomleyb6aff662007-07-20 11:10:05 -0500320static int sas_host_remove(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100321 struct device *cdev)
James Bottomleyb6aff662007-07-20 11:10:05 -0500322{
323 struct Scsi_Host *shost = dev_to_shost(dev);
324
325 sas_bsg_remove(shost, NULL);
326
327 return 0;
328}
329
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200330static DECLARE_TRANSPORT_CLASS(sas_host_class,
James Bottomleyb6aff662007-07-20 11:10:05 -0500331 "sas_host", sas_host_setup, sas_host_remove, NULL);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200332
333static int sas_host_match(struct attribute_container *cont,
334 struct device *dev)
335{
336 struct Scsi_Host *shost;
337 struct sas_internal *i;
338
339 if (!scsi_is_host_device(dev))
340 return 0;
341 shost = dev_to_shost(dev);
342
343 if (!shost->transportt)
344 return 0;
345 if (shost->transportt->host_attrs.ac.class !=
346 &sas_host_class.class)
347 return 0;
348
349 i = to_sas_internal(shost->transportt);
350 return &i->t.host_attrs.ac == cont;
351}
352
353static int do_sas_phy_delete(struct device *dev, void *data)
354{
James Bottomley65c92b02006-06-28 12:22:50 -0400355 int pass = (int)(unsigned long)data;
356
357 if (pass == 0 && scsi_is_sas_port(dev))
358 sas_port_delete(dev_to_sas_port(dev));
359 else if (pass == 1 && scsi_is_sas_phy(dev))
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200360 sas_phy_delete(dev_to_phy(dev));
361 return 0;
362}
363
364/**
Rob Landleyeb448202007-11-03 13:30:39 -0500365 * sas_remove_children - tear down a devices SAS data structures
James Bottomley65c92b02006-06-28 12:22:50 -0400366 * @dev: device belonging to the sas object
367 *
368 * Removes all SAS PHYs and remote PHYs for a given object
369 */
370void sas_remove_children(struct device *dev)
371{
372 device_for_each_child(dev, (void *)0, do_sas_phy_delete);
373 device_for_each_child(dev, (void *)1, do_sas_phy_delete);
374}
375EXPORT_SYMBOL(sas_remove_children);
376
377/**
Rob Landleyeb448202007-11-03 13:30:39 -0500378 * sas_remove_host - tear down a Scsi_Host's SAS data structures
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200379 * @shost: Scsi Host that is torn down
380 *
Johannes Thumshirnc5ce0ab2017-04-21 14:11:41 +0200381 * Removes all SAS PHYs and remote PHYs for a given Scsi_Host and remove the
382 * Scsi_Host as well.
383 *
384 * Note: Do not call scsi_remove_host() on the Scsi_Host any more, as it is
385 * already removed.
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200386 */
387void sas_remove_host(struct Scsi_Host *shost)
388{
James Bottomley65c92b02006-06-28 12:22:50 -0400389 sas_remove_children(&shost->shost_gendev);
Johannes Thumshirnc5ce0ab2017-04-21 14:11:41 +0200390 scsi_remove_host(shost);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200391}
392EXPORT_SYMBOL(sas_remove_host);
393
James Bottomley0f880092010-01-18 10:14:51 -0600394/**
James Bottomleybcf508c2015-12-09 11:13:35 -0800395 * sas_get_address - return the SAS address of the device
396 * @sdev: scsi device
397 *
398 * Returns the SAS address of the scsi device
399 */
400u64 sas_get_address(struct scsi_device *sdev)
401{
402 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
403
404 return rdev->rphy.identify.sas_address;
405}
406EXPORT_SYMBOL(sas_get_address);
407
408/**
James Bottomley0f880092010-01-18 10:14:51 -0600409 * sas_tlr_supported - checking TLR bit in vpd 0x90
410 * @sdev: scsi device struct
411 *
412 * Check Transport Layer Retries are supported or not.
413 * If vpd page 0x90 is present, TRL is supported.
414 *
415 */
416unsigned int
417sas_tlr_supported(struct scsi_device *sdev)
418{
419 const int vpd_len = 32;
420 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
421 char *buffer = kzalloc(vpd_len, GFP_KERNEL);
422 int ret = 0;
423
424 if (scsi_get_vpd_page(sdev, 0x90, buffer, vpd_len))
425 goto out;
426
427 /*
428 * Magic numbers: the VPD Protocol page (0x90)
429 * has a 4 byte header and then one entry per device port
430 * the TLR bit is at offset 8 on each port entry
431 * if we take the first port, that's at total offset 12
432 */
433 ret = buffer[12] & 0x01;
434
435 out:
436 kfree(buffer);
437 rdev->tlr_supported = ret;
438 return ret;
439
440}
441EXPORT_SYMBOL_GPL(sas_tlr_supported);
442
443/**
444 * sas_disable_tlr - setting TLR flags
445 * @sdev: scsi device struct
446 *
447 * Seting tlr_enabled flag to 0.
448 *
449 */
450void
451sas_disable_tlr(struct scsi_device *sdev)
452{
453 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
454
455 rdev->tlr_enabled = 0;
456}
457EXPORT_SYMBOL_GPL(sas_disable_tlr);
458
459/**
460 * sas_enable_tlr - setting TLR flags
461 * @sdev: scsi device struct
462 *
463 * Seting tlr_enabled flag 1.
464 *
465 */
466void sas_enable_tlr(struct scsi_device *sdev)
467{
468 unsigned int tlr_supported = 0;
469 tlr_supported = sas_tlr_supported(sdev);
470
471 if (tlr_supported) {
472 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
473
474 rdev->tlr_enabled = 1;
475 }
476
477 return;
478}
479EXPORT_SYMBOL_GPL(sas_enable_tlr);
480
481unsigned int sas_is_tlr_enabled(struct scsi_device *sdev)
482{
483 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
484 return rdev->tlr_enabled;
485}
486EXPORT_SYMBOL_GPL(sas_is_tlr_enabled);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200487
488/*
James Bottomley65c92b02006-06-28 12:22:50 -0400489 * SAS Phy attributes
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200490 */
491
492#define sas_phy_show_simple(field, name, format_string, cast) \
493static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100494show_sas_phy_##name(struct device *dev, \
495 struct device_attribute *attr, char *buf) \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200496{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100497 struct sas_phy *phy = transport_class_to_phy(dev); \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200498 \
499 return snprintf(buf, 20, format_string, cast phy->field); \
500}
501
502#define sas_phy_simple_attr(field, name, format_string, type) \
503 sas_phy_show_simple(field, name, format_string, (type)) \
Tony Jonesee959b02008-02-22 00:13:36 +0100504static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200505
506#define sas_phy_show_protocol(field, name) \
507static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100508show_sas_phy_##name(struct device *dev, \
509 struct device_attribute *attr, char *buf) \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200510{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100511 struct sas_phy *phy = transport_class_to_phy(dev); \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200512 \
513 if (!phy->field) \
514 return snprintf(buf, 20, "none\n"); \
515 return get_sas_protocol_names(phy->field, buf); \
516}
517
518#define sas_phy_protocol_attr(field, name) \
519 sas_phy_show_protocol(field, name) \
Tony Jonesee959b02008-02-22 00:13:36 +0100520static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200521
522#define sas_phy_show_linkspeed(field) \
523static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100524show_sas_phy_##field(struct device *dev, \
525 struct device_attribute *attr, char *buf) \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200526{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100527 struct sas_phy *phy = transport_class_to_phy(dev); \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200528 \
529 return get_sas_linkspeed_names(phy->field, buf); \
530}
531
James Bottomleyd24e1ee2006-09-06 19:25:22 -0500532/* Fudge to tell if we're minimum or maximum */
533#define sas_phy_store_linkspeed(field) \
534static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100535store_sas_phy_##field(struct device *dev, \
536 struct device_attribute *attr, \
537 const char *buf, size_t count) \
James Bottomleyd24e1ee2006-09-06 19:25:22 -0500538{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100539 struct sas_phy *phy = transport_class_to_phy(dev); \
James Bottomleyd24e1ee2006-09-06 19:25:22 -0500540 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
541 struct sas_internal *i = to_sas_internal(shost->transportt); \
542 u32 value; \
543 struct sas_phy_linkrates rates = {0}; \
544 int error; \
545 \
546 error = set_sas_linkspeed_names(&value, buf); \
547 if (error) \
548 return error; \
549 rates.field = value; \
550 error = i->f->set_phy_speed(phy, &rates); \
551 \
552 return error ? error : count; \
553}
554
555#define sas_phy_linkspeed_rw_attr(field) \
556 sas_phy_show_linkspeed(field) \
557 sas_phy_store_linkspeed(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100558static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, \
James Bottomleyd24e1ee2006-09-06 19:25:22 -0500559 store_sas_phy_##field)
560
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200561#define sas_phy_linkspeed_attr(field) \
562 sas_phy_show_linkspeed(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100563static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200564
James Bottomleyd24e1ee2006-09-06 19:25:22 -0500565
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +0200566#define sas_phy_show_linkerror(field) \
567static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100568show_sas_phy_##field(struct device *dev, \
569 struct device_attribute *attr, char *buf) \
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +0200570{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100571 struct sas_phy *phy = transport_class_to_phy(dev); \
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +0200572 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
573 struct sas_internal *i = to_sas_internal(shost->transportt); \
574 int error; \
575 \
James Bottomleydd9fbb522006-03-02 16:01:31 -0600576 error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0; \
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +0200577 if (error) \
578 return error; \
579 return snprintf(buf, 20, "%u\n", phy->field); \
580}
581
582#define sas_phy_linkerror_attr(field) \
583 sas_phy_show_linkerror(field) \
Tony Jonesee959b02008-02-22 00:13:36 +0100584static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +0200585
586
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200587static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100588show_sas_device_type(struct device *dev,
589 struct device_attribute *attr, char *buf)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200590{
Tony Jonesee959b02008-02-22 00:13:36 +0100591 struct sas_phy *phy = transport_class_to_phy(dev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200592
593 if (!phy->identify.device_type)
594 return snprintf(buf, 20, "none\n");
595 return get_sas_device_type_names(phy->identify.device_type, buf);
596}
Tony Jonesee959b02008-02-22 00:13:36 +0100597static DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200598
Tony Jonesee959b02008-02-22 00:13:36 +0100599static ssize_t do_sas_phy_enable(struct device *dev,
Darrick J. Wongacbf1672007-01-11 14:14:57 -0800600 size_t count, int enable)
601{
Tony Jonesee959b02008-02-22 00:13:36 +0100602 struct sas_phy *phy = transport_class_to_phy(dev);
Darrick J. Wongacbf1672007-01-11 14:14:57 -0800603 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
604 struct sas_internal *i = to_sas_internal(shost->transportt);
605 int error;
606
607 error = i->f->phy_enable(phy, enable);
608 if (error)
609 return error;
610 phy->enabled = enable;
611 return count;
612};
613
Tony Jonesee959b02008-02-22 00:13:36 +0100614static ssize_t
615store_sas_phy_enable(struct device *dev, struct device_attribute *attr,
616 const char *buf, size_t count)
Darrick J. Wongacbf1672007-01-11 14:14:57 -0800617{
618 if (count < 1)
619 return -EINVAL;
620
621 switch (buf[0]) {
622 case '0':
Tony Jonesee959b02008-02-22 00:13:36 +0100623 do_sas_phy_enable(dev, count, 0);
Darrick J. Wongacbf1672007-01-11 14:14:57 -0800624 break;
625 case '1':
Tony Jonesee959b02008-02-22 00:13:36 +0100626 do_sas_phy_enable(dev, count, 1);
Darrick J. Wongacbf1672007-01-11 14:14:57 -0800627 break;
628 default:
629 return -EINVAL;
630 }
631
632 return count;
633}
634
Tony Jonesee959b02008-02-22 00:13:36 +0100635static ssize_t
636show_sas_phy_enable(struct device *dev, struct device_attribute *attr,
637 char *buf)
Darrick J. Wongacbf1672007-01-11 14:14:57 -0800638{
Tony Jonesee959b02008-02-22 00:13:36 +0100639 struct sas_phy *phy = transport_class_to_phy(dev);
Darrick J. Wongacbf1672007-01-11 14:14:57 -0800640
641 return snprintf(buf, 20, "%d", phy->enabled);
642}
643
Tony Jonesee959b02008-02-22 00:13:36 +0100644static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, show_sas_phy_enable,
Darrick J. Wongacbf1672007-01-11 14:14:57 -0800645 store_sas_phy_enable);
646
Tony Jonesee959b02008-02-22 00:13:36 +0100647static ssize_t
648do_sas_phy_reset(struct device *dev, size_t count, int hard_reset)
Christoph Hellwig07ba3a92005-10-19 20:01:31 +0200649{
Tony Jonesee959b02008-02-22 00:13:36 +0100650 struct sas_phy *phy = transport_class_to_phy(dev);
Christoph Hellwig07ba3a92005-10-19 20:01:31 +0200651 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
652 struct sas_internal *i = to_sas_internal(shost->transportt);
653 int error;
654
Christoph Hellwig07ba3a92005-10-19 20:01:31 +0200655 error = i->f->phy_reset(phy, hard_reset);
656 if (error)
657 return error;
Dan Williams16d3db12012-01-30 22:53:51 -0800658 phy->enabled = 1;
Christoph Hellwig07ba3a92005-10-19 20:01:31 +0200659 return count;
660};
661
Tony Jonesee959b02008-02-22 00:13:36 +0100662static ssize_t
663store_sas_link_reset(struct device *dev, struct device_attribute *attr,
664 const char *buf, size_t count)
Christoph Hellwig07ba3a92005-10-19 20:01:31 +0200665{
Tony Jonesee959b02008-02-22 00:13:36 +0100666 return do_sas_phy_reset(dev, count, 0);
Christoph Hellwig07ba3a92005-10-19 20:01:31 +0200667}
Tony Jonesee959b02008-02-22 00:13:36 +0100668static DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
Christoph Hellwig07ba3a92005-10-19 20:01:31 +0200669
Tony Jonesee959b02008-02-22 00:13:36 +0100670static ssize_t
671store_sas_hard_reset(struct device *dev, struct device_attribute *attr,
672 const char *buf, size_t count)
Christoph Hellwig07ba3a92005-10-19 20:01:31 +0200673{
Tony Jonesee959b02008-02-22 00:13:36 +0100674 return do_sas_phy_reset(dev, count, 1);
Christoph Hellwig07ba3a92005-10-19 20:01:31 +0200675}
Tony Jonesee959b02008-02-22 00:13:36 +0100676static DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
Christoph Hellwig07ba3a92005-10-19 20:01:31 +0200677
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200678sas_phy_protocol_attr(identify.initiator_port_protocols,
679 initiator_port_protocols);
680sas_phy_protocol_attr(identify.target_port_protocols,
681 target_port_protocols);
682sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
683 unsigned long long);
684sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
James Bottomleyc9fefeb2006-07-02 11:10:18 -0500685//sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", int);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200686sas_phy_linkspeed_attr(negotiated_linkrate);
687sas_phy_linkspeed_attr(minimum_linkrate_hw);
James Bottomleyd24e1ee2006-09-06 19:25:22 -0500688sas_phy_linkspeed_rw_attr(minimum_linkrate);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200689sas_phy_linkspeed_attr(maximum_linkrate_hw);
James Bottomleyd24e1ee2006-09-06 19:25:22 -0500690sas_phy_linkspeed_rw_attr(maximum_linkrate);
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +0200691sas_phy_linkerror_attr(invalid_dword_count);
692sas_phy_linkerror_attr(running_disparity_error_count);
693sas_phy_linkerror_attr(loss_of_dword_sync_count);
694sas_phy_linkerror_attr(phy_reset_problem_count);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200695
Dan Williams0b3e09d2011-12-20 01:03:48 -0800696static int sas_phy_setup(struct transport_container *tc, struct device *dev,
697 struct device *cdev)
698{
699 struct sas_phy *phy = dev_to_phy(dev);
700 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
701 struct sas_internal *i = to_sas_internal(shost->transportt);
702
703 if (i->f->phy_setup)
704 i->f->phy_setup(phy);
705
706 return 0;
707}
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200708
709static DECLARE_TRANSPORT_CLASS(sas_phy_class,
Dan Williams0b3e09d2011-12-20 01:03:48 -0800710 "sas_phy", sas_phy_setup, NULL, NULL);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200711
712static int sas_phy_match(struct attribute_container *cont, struct device *dev)
713{
714 struct Scsi_Host *shost;
715 struct sas_internal *i;
716
717 if (!scsi_is_sas_phy(dev))
718 return 0;
719 shost = dev_to_shost(dev->parent);
720
721 if (!shost->transportt)
722 return 0;
723 if (shost->transportt->host_attrs.ac.class !=
724 &sas_host_class.class)
725 return 0;
726
727 i = to_sas_internal(shost->transportt);
728 return &i->phy_attr_cont.ac == cont;
729}
730
731static void sas_phy_release(struct device *dev)
732{
733 struct sas_phy *phy = dev_to_phy(dev);
Dan Williams0b3e09d2011-12-20 01:03:48 -0800734 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
735 struct sas_internal *i = to_sas_internal(shost->transportt);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200736
Dan Williams0b3e09d2011-12-20 01:03:48 -0800737 if (i->f->phy_release)
738 i->f->phy_release(phy);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200739 put_device(dev->parent);
740 kfree(phy);
741}
742
743/**
Rob Landleyeb448202007-11-03 13:30:39 -0500744 * sas_phy_alloc - allocates and initialize a SAS PHY structure
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200745 * @parent: Parent device
Moore, Ericd99ca412006-01-26 16:20:02 -0700746 * @number: Phy index
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200747 *
748 * Allocates an SAS PHY structure. It will be added in the device tree
749 * below the device specified by @parent, which has to be either a Scsi_Host
750 * or sas_rphy.
751 *
752 * Returns:
753 * SAS PHY allocated or %NULL if the allocation failed.
754 */
755struct sas_phy *sas_phy_alloc(struct device *parent, int number)
756{
757 struct Scsi_Host *shost = dev_to_shost(parent);
758 struct sas_phy *phy;
759
Jes Sorensen24669f752006-01-16 10:31:18 -0500760 phy = kzalloc(sizeof(*phy), GFP_KERNEL);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200761 if (!phy)
762 return NULL;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200763
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200764 phy->number = number;
Darrick J. Wongacbf1672007-01-11 14:14:57 -0800765 phy->enabled = 1;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200766
767 device_initialize(&phy->dev);
768 phy->dev.parent = get_device(parent);
769 phy->dev.release = sas_phy_release;
James Bottomley65c92b02006-06-28 12:22:50 -0400770 INIT_LIST_HEAD(&phy->port_siblings);
James Bottomley79cb1812006-03-13 13:50:04 -0600771 if (scsi_is_sas_expander_device(parent)) {
772 struct sas_rphy *rphy = dev_to_rphy(parent);
Kay Sievers71610f52008-12-03 22:41:36 +0100773 dev_set_name(&phy->dev, "phy-%d:%d:%d", shost->host_no,
James Bottomley79cb1812006-03-13 13:50:04 -0600774 rphy->scsi_target_id, number);
775 } else
Kay Sievers71610f52008-12-03 22:41:36 +0100776 dev_set_name(&phy->dev, "phy-%d:%d", shost->host_no, number);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200777
778 transport_setup_device(&phy->dev);
779
780 return phy;
781}
782EXPORT_SYMBOL(sas_phy_alloc);
783
784/**
Rob Landleyeb448202007-11-03 13:30:39 -0500785 * sas_phy_add - add a SAS PHY to the device hierarchy
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200786 * @phy: The PHY to be added
787 *
788 * Publishes a SAS PHY to the rest of the system.
789 */
790int sas_phy_add(struct sas_phy *phy)
791{
792 int error;
793
794 error = device_add(&phy->dev);
795 if (!error) {
796 transport_add_device(&phy->dev);
797 transport_configure_device(&phy->dev);
798 }
799
800 return error;
801}
802EXPORT_SYMBOL(sas_phy_add);
803
804/**
Rob Landleyeb448202007-11-03 13:30:39 -0500805 * sas_phy_free - free a SAS PHY
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200806 * @phy: SAS PHY to free
807 *
808 * Frees the specified SAS PHY.
809 *
810 * Note:
811 * This function must only be called on a PHY that has not
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200812 * successfully been added using sas_phy_add().
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200813 */
814void sas_phy_free(struct sas_phy *phy)
815{
816 transport_destroy_device(&phy->dev);
Mike Anderson92aab642006-03-27 09:37:28 -0800817 put_device(&phy->dev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200818}
819EXPORT_SYMBOL(sas_phy_free);
820
821/**
Rob Landleyeb448202007-11-03 13:30:39 -0500822 * sas_phy_delete - remove SAS PHY
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200823 * @phy: SAS PHY to remove
824 *
825 * Removes the specified SAS PHY. If the SAS PHY has an
826 * associated remote PHY it is removed before.
827 */
828void
829sas_phy_delete(struct sas_phy *phy)
830{
831 struct device *dev = &phy->dev;
832
James Bottomley65c92b02006-06-28 12:22:50 -0400833 /* this happens if the phy is still part of a port when deleted */
834 BUG_ON(!list_empty(&phy->port_siblings));
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200835
836 transport_remove_device(dev);
837 device_del(dev);
838 transport_destroy_device(dev);
Mike Anderson92aab642006-03-27 09:37:28 -0800839 put_device(dev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200840}
841EXPORT_SYMBOL(sas_phy_delete);
842
843/**
Rob Landleyeb448202007-11-03 13:30:39 -0500844 * scsi_is_sas_phy - check if a struct device represents a SAS PHY
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200845 * @dev: device to check
846 *
847 * Returns:
848 * %1 if the device represents a SAS PHY, %0 else
849 */
850int scsi_is_sas_phy(const struct device *dev)
851{
852 return dev->release == sas_phy_release;
853}
854EXPORT_SYMBOL(scsi_is_sas_phy);
855
856/*
James Bottomley65c92b02006-06-28 12:22:50 -0400857 * SAS Port attributes
858 */
859#define sas_port_show_simple(field, name, format_string, cast) \
860static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100861show_sas_port_##name(struct device *dev, \
862 struct device_attribute *attr, char *buf) \
James Bottomley65c92b02006-06-28 12:22:50 -0400863{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100864 struct sas_port *port = transport_class_to_sas_port(dev); \
James Bottomley65c92b02006-06-28 12:22:50 -0400865 \
866 return snprintf(buf, 20, format_string, cast port->field); \
867}
868
869#define sas_port_simple_attr(field, name, format_string, type) \
870 sas_port_show_simple(field, name, format_string, (type)) \
Tony Jonesee959b02008-02-22 00:13:36 +0100871static DEVICE_ATTR(name, S_IRUGO, show_sas_port_##name, NULL)
James Bottomley65c92b02006-06-28 12:22:50 -0400872
873sas_port_simple_attr(num_phys, num_phys, "%d\n", int);
874
875static DECLARE_TRANSPORT_CLASS(sas_port_class,
876 "sas_port", NULL, NULL, NULL);
877
878static int sas_port_match(struct attribute_container *cont, struct device *dev)
879{
880 struct Scsi_Host *shost;
881 struct sas_internal *i;
882
883 if (!scsi_is_sas_port(dev))
884 return 0;
885 shost = dev_to_shost(dev->parent);
886
887 if (!shost->transportt)
888 return 0;
889 if (shost->transportt->host_attrs.ac.class !=
890 &sas_host_class.class)
891 return 0;
892
893 i = to_sas_internal(shost->transportt);
894 return &i->port_attr_cont.ac == cont;
895}
896
897
898static void sas_port_release(struct device *dev)
899{
900 struct sas_port *port = dev_to_sas_port(dev);
901
902 BUG_ON(!list_empty(&port->phy_list));
903
904 put_device(dev->parent);
905 kfree(port);
906}
907
908static void sas_port_create_link(struct sas_port *port,
909 struct sas_phy *phy)
910{
Darrick J. Wong21434962007-01-26 14:08:46 -0800911 int res;
912
913 res = sysfs_create_link(&port->dev.kobj, &phy->dev.kobj,
Kay Sievers71610f52008-12-03 22:41:36 +0100914 dev_name(&phy->dev));
Darrick J. Wong21434962007-01-26 14:08:46 -0800915 if (res)
916 goto err;
917 res = sysfs_create_link(&phy->dev.kobj, &port->dev.kobj, "port");
918 if (res)
919 goto err;
920 return;
921err:
922 printk(KERN_ERR "%s: Cannot create port links, err=%d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700923 __func__, res);
James Bottomley65c92b02006-06-28 12:22:50 -0400924}
925
926static void sas_port_delete_link(struct sas_port *port,
927 struct sas_phy *phy)
928{
Kay Sievers71610f52008-12-03 22:41:36 +0100929 sysfs_remove_link(&port->dev.kobj, dev_name(&phy->dev));
James Bottomley65c92b02006-06-28 12:22:50 -0400930 sysfs_remove_link(&phy->dev.kobj, "port");
931}
932
933/** sas_port_alloc - allocate and initialize a SAS port structure
934 *
935 * @parent: parent device
936 * @port_id: port number
937 *
938 * Allocates a SAS port structure. It will be added to the device tree
939 * below the device specified by @parent which must be either a Scsi_Host
940 * or a sas_expander_device.
941 *
942 * Returns %NULL on error
943 */
944struct sas_port *sas_port_alloc(struct device *parent, int port_id)
945{
946 struct Scsi_Host *shost = dev_to_shost(parent);
947 struct sas_port *port;
948
949 port = kzalloc(sizeof(*port), GFP_KERNEL);
950 if (!port)
951 return NULL;
952
953 port->port_identifier = port_id;
954
955 device_initialize(&port->dev);
956
957 port->dev.parent = get_device(parent);
958 port->dev.release = sas_port_release;
959
960 mutex_init(&port->phy_list_mutex);
961 INIT_LIST_HEAD(&port->phy_list);
962
963 if (scsi_is_sas_expander_device(parent)) {
964 struct sas_rphy *rphy = dev_to_rphy(parent);
Kay Sievers71610f52008-12-03 22:41:36 +0100965 dev_set_name(&port->dev, "port-%d:%d:%d", shost->host_no,
966 rphy->scsi_target_id, port->port_identifier);
James Bottomley65c92b02006-06-28 12:22:50 -0400967 } else
Kay Sievers71610f52008-12-03 22:41:36 +0100968 dev_set_name(&port->dev, "port-%d:%d", shost->host_no,
969 port->port_identifier);
James Bottomley65c92b02006-06-28 12:22:50 -0400970
971 transport_setup_device(&port->dev);
972
973 return port;
974}
975EXPORT_SYMBOL(sas_port_alloc);
976
James Bottomleyc9fefeb2006-07-02 11:10:18 -0500977/** sas_port_alloc_num - allocate and initialize a SAS port structure
978 *
979 * @parent: parent device
980 *
981 * Allocates a SAS port structure and a number to go with it. This
982 * interface is really for adapters where the port number has no
983 * meansing, so the sas class should manage them. It will be added to
984 * the device tree below the device specified by @parent which must be
985 * either a Scsi_Host or a sas_expander_device.
986 *
987 * Returns %NULL on error
988 */
989struct sas_port *sas_port_alloc_num(struct device *parent)
990{
991 int index;
992 struct Scsi_Host *shost = dev_to_shost(parent);
993 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
994
995 /* FIXME: use idr for this eventually */
996 mutex_lock(&sas_host->lock);
997 if (scsi_is_sas_expander_device(parent)) {
998 struct sas_rphy *rphy = dev_to_rphy(parent);
999 struct sas_expander_device *exp = rphy_to_expander_device(rphy);
1000
1001 index = exp->next_port_id++;
1002 } else
1003 index = sas_host->next_port_id++;
1004 mutex_unlock(&sas_host->lock);
1005 return sas_port_alloc(parent, index);
1006}
1007EXPORT_SYMBOL(sas_port_alloc_num);
1008
James Bottomley65c92b02006-06-28 12:22:50 -04001009/**
1010 * sas_port_add - add a SAS port to the device hierarchy
James Bottomley65c92b02006-06-28 12:22:50 -04001011 * @port: port to be added
1012 *
1013 * publishes a port to the rest of the system
1014 */
1015int sas_port_add(struct sas_port *port)
1016{
1017 int error;
1018
1019 /* No phys should be added until this is made visible */
1020 BUG_ON(!list_empty(&port->phy_list));
1021
1022 error = device_add(&port->dev);
1023
1024 if (error)
1025 return error;
1026
1027 transport_add_device(&port->dev);
1028 transport_configure_device(&port->dev);
1029
1030 return 0;
1031}
1032EXPORT_SYMBOL(sas_port_add);
1033
1034/**
Rob Landleyeb448202007-11-03 13:30:39 -05001035 * sas_port_free - free a SAS PORT
James Bottomley65c92b02006-06-28 12:22:50 -04001036 * @port: SAS PORT to free
1037 *
1038 * Frees the specified SAS PORT.
1039 *
1040 * Note:
1041 * This function must only be called on a PORT that has not
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001042 * successfully been added using sas_port_add().
James Bottomley65c92b02006-06-28 12:22:50 -04001043 */
1044void sas_port_free(struct sas_port *port)
1045{
1046 transport_destroy_device(&port->dev);
1047 put_device(&port->dev);
1048}
1049EXPORT_SYMBOL(sas_port_free);
1050
1051/**
Rob Landleyeb448202007-11-03 13:30:39 -05001052 * sas_port_delete - remove SAS PORT
James Bottomley65c92b02006-06-28 12:22:50 -04001053 * @port: SAS PORT to remove
1054 *
1055 * Removes the specified SAS PORT. If the SAS PORT has an
1056 * associated phys, unlink them from the port as well.
1057 */
1058void sas_port_delete(struct sas_port *port)
1059{
1060 struct device *dev = &port->dev;
1061 struct sas_phy *phy, *tmp_phy;
1062
1063 if (port->rphy) {
1064 sas_rphy_delete(port->rphy);
1065 port->rphy = NULL;
1066 }
1067
1068 mutex_lock(&port->phy_list_mutex);
1069 list_for_each_entry_safe(phy, tmp_phy, &port->phy_list,
1070 port_siblings) {
1071 sas_port_delete_link(port, phy);
1072 list_del_init(&phy->port_siblings);
1073 }
1074 mutex_unlock(&port->phy_list_mutex);
1075
James Bottomleya0e1b6e2006-07-09 12:38:19 -05001076 if (port->is_backlink) {
1077 struct device *parent = port->dev.parent;
1078
Kay Sievers71610f52008-12-03 22:41:36 +01001079 sysfs_remove_link(&port->dev.kobj, dev_name(parent));
James Bottomleya0e1b6e2006-07-09 12:38:19 -05001080 port->is_backlink = 0;
1081 }
1082
James Bottomley65c92b02006-06-28 12:22:50 -04001083 transport_remove_device(dev);
1084 device_del(dev);
1085 transport_destroy_device(dev);
1086 put_device(dev);
1087}
1088EXPORT_SYMBOL(sas_port_delete);
1089
1090/**
Rob Landleyeb448202007-11-03 13:30:39 -05001091 * scsi_is_sas_port - check if a struct device represents a SAS port
James Bottomley65c92b02006-06-28 12:22:50 -04001092 * @dev: device to check
1093 *
1094 * Returns:
1095 * %1 if the device represents a SAS Port, %0 else
1096 */
1097int scsi_is_sas_port(const struct device *dev)
1098{
1099 return dev->release == sas_port_release;
1100}
1101EXPORT_SYMBOL(scsi_is_sas_port);
1102
1103/**
Dan Williamsf41a0c42011-12-21 21:33:17 -08001104 * sas_port_get_phy - try to take a reference on a port member
1105 * @port: port to check
1106 */
1107struct sas_phy *sas_port_get_phy(struct sas_port *port)
1108{
1109 struct sas_phy *phy;
1110
1111 mutex_lock(&port->phy_list_mutex);
1112 if (list_empty(&port->phy_list))
1113 phy = NULL;
1114 else {
1115 struct list_head *ent = port->phy_list.next;
1116
1117 phy = list_entry(ent, typeof(*phy), port_siblings);
1118 get_device(&phy->dev);
1119 }
1120 mutex_unlock(&port->phy_list_mutex);
1121
1122 return phy;
1123}
1124EXPORT_SYMBOL(sas_port_get_phy);
1125
1126/**
James Bottomley65c92b02006-06-28 12:22:50 -04001127 * sas_port_add_phy - add another phy to a port to form a wide port
1128 * @port: port to add the phy to
1129 * @phy: phy to add
1130 *
1131 * When a port is initially created, it is empty (has no phys). All
1132 * ports must have at least one phy to operated, and all wide ports
1133 * must have at least two. The current code makes no difference
1134 * between ports and wide ports, but the only object that can be
1135 * connected to a remote device is a port, so ports must be formed on
1136 * all devices with phys if they're connected to anything.
1137 */
1138void sas_port_add_phy(struct sas_port *port, struct sas_phy *phy)
1139{
1140 mutex_lock(&port->phy_list_mutex);
1141 if (unlikely(!list_empty(&phy->port_siblings))) {
1142 /* make sure we're already on this port */
1143 struct sas_phy *tmp;
1144
1145 list_for_each_entry(tmp, &port->phy_list, port_siblings)
1146 if (tmp == phy)
1147 break;
1148 /* If this trips, you added a phy that was already
1149 * part of a different port */
1150 if (unlikely(tmp != phy)) {
Kay Sievers71610f52008-12-03 22:41:36 +01001151 dev_printk(KERN_ERR, &port->dev, "trying to add phy %s fails: it's already part of another port\n",
1152 dev_name(&phy->dev));
James Bottomley65c92b02006-06-28 12:22:50 -04001153 BUG();
1154 }
1155 } else {
1156 sas_port_create_link(port, phy);
1157 list_add_tail(&phy->port_siblings, &port->phy_list);
1158 port->num_phys++;
1159 }
1160 mutex_unlock(&port->phy_list_mutex);
1161}
1162EXPORT_SYMBOL(sas_port_add_phy);
1163
1164/**
1165 * sas_port_delete_phy - remove a phy from a port or wide port
1166 * @port: port to remove the phy from
1167 * @phy: phy to remove
1168 *
1169 * This operation is used for tearing down ports again. It must be
1170 * done to every port or wide port before calling sas_port_delete.
1171 */
1172void sas_port_delete_phy(struct sas_port *port, struct sas_phy *phy)
1173{
1174 mutex_lock(&port->phy_list_mutex);
1175 sas_port_delete_link(port, phy);
1176 list_del_init(&phy->port_siblings);
1177 port->num_phys--;
1178 mutex_unlock(&port->phy_list_mutex);
1179}
1180EXPORT_SYMBOL(sas_port_delete_phy);
1181
James Bottomleya0e1b6e2006-07-09 12:38:19 -05001182void sas_port_mark_backlink(struct sas_port *port)
1183{
Darrick J. Wong21434962007-01-26 14:08:46 -08001184 int res;
James Bottomleya0e1b6e2006-07-09 12:38:19 -05001185 struct device *parent = port->dev.parent->parent->parent;
1186
1187 if (port->is_backlink)
1188 return;
1189 port->is_backlink = 1;
Darrick J. Wong21434962007-01-26 14:08:46 -08001190 res = sysfs_create_link(&port->dev.kobj, &parent->kobj,
Kay Sievers71610f52008-12-03 22:41:36 +01001191 dev_name(parent));
Darrick J. Wong21434962007-01-26 14:08:46 -08001192 if (res)
1193 goto err;
1194 return;
1195err:
1196 printk(KERN_ERR "%s: Cannot create port backlink, err=%d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001197 __func__, res);
James Bottomleya0e1b6e2006-07-09 12:38:19 -05001198
1199}
1200EXPORT_SYMBOL(sas_port_mark_backlink);
1201
James Bottomley65c92b02006-06-28 12:22:50 -04001202/*
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001203 * SAS remote PHY attributes.
1204 */
1205
1206#define sas_rphy_show_simple(field, name, format_string, cast) \
1207static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001208show_sas_rphy_##name(struct device *dev, \
1209 struct device_attribute *attr, char *buf) \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001210{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001211 struct sas_rphy *rphy = transport_class_to_rphy(dev); \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001212 \
1213 return snprintf(buf, 20, format_string, cast rphy->field); \
1214}
1215
1216#define sas_rphy_simple_attr(field, name, format_string, type) \
1217 sas_rphy_show_simple(field, name, format_string, (type)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001218static SAS_DEVICE_ATTR(rphy, name, S_IRUGO, \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001219 show_sas_rphy_##name, NULL)
1220
1221#define sas_rphy_show_protocol(field, name) \
1222static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001223show_sas_rphy_##name(struct device *dev, \
1224 struct device_attribute *attr, char *buf) \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001225{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001226 struct sas_rphy *rphy = transport_class_to_rphy(dev); \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001227 \
1228 if (!rphy->field) \
1229 return snprintf(buf, 20, "none\n"); \
1230 return get_sas_protocol_names(rphy->field, buf); \
1231}
1232
1233#define sas_rphy_protocol_attr(field, name) \
1234 sas_rphy_show_protocol(field, name) \
Tony Jonesee959b02008-02-22 00:13:36 +01001235static SAS_DEVICE_ATTR(rphy, name, S_IRUGO, \
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001236 show_sas_rphy_##name, NULL)
1237
1238static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001239show_sas_rphy_device_type(struct device *dev,
1240 struct device_attribute *attr, char *buf)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001241{
Tony Jonesee959b02008-02-22 00:13:36 +01001242 struct sas_rphy *rphy = transport_class_to_rphy(dev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001243
1244 if (!rphy->identify.device_type)
1245 return snprintf(buf, 20, "none\n");
1246 return get_sas_device_type_names(
1247 rphy->identify.device_type, buf);
1248}
1249
Tony Jonesee959b02008-02-22 00:13:36 +01001250static SAS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001251 show_sas_rphy_device_type, NULL);
1252
Christoph Hellwiga0125642006-02-16 13:31:47 +01001253static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001254show_sas_rphy_enclosure_identifier(struct device *dev,
1255 struct device_attribute *attr, char *buf)
Christoph Hellwiga0125642006-02-16 13:31:47 +01001256{
Tony Jonesee959b02008-02-22 00:13:36 +01001257 struct sas_rphy *rphy = transport_class_to_rphy(dev);
Christoph Hellwiga0125642006-02-16 13:31:47 +01001258 struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
1259 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1260 struct sas_internal *i = to_sas_internal(shost->transportt);
1261 u64 identifier;
1262 int error;
1263
Christoph Hellwiga0125642006-02-16 13:31:47 +01001264 error = i->f->get_enclosure_identifier(rphy, &identifier);
1265 if (error)
1266 return error;
1267 return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
1268}
1269
Tony Jonesee959b02008-02-22 00:13:36 +01001270static SAS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
Christoph Hellwiga0125642006-02-16 13:31:47 +01001271 show_sas_rphy_enclosure_identifier, NULL);
1272
1273static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001274show_sas_rphy_bay_identifier(struct device *dev,
1275 struct device_attribute *attr, char *buf)
Christoph Hellwiga0125642006-02-16 13:31:47 +01001276{
Tony Jonesee959b02008-02-22 00:13:36 +01001277 struct sas_rphy *rphy = transport_class_to_rphy(dev);
Christoph Hellwiga0125642006-02-16 13:31:47 +01001278 struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
1279 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1280 struct sas_internal *i = to_sas_internal(shost->transportt);
1281 int val;
1282
Christoph Hellwiga0125642006-02-16 13:31:47 +01001283 val = i->f->get_bay_identifier(rphy);
1284 if (val < 0)
1285 return val;
1286 return sprintf(buf, "%d\n", val);
1287}
1288
Tony Jonesee959b02008-02-22 00:13:36 +01001289static SAS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
Christoph Hellwiga0125642006-02-16 13:31:47 +01001290 show_sas_rphy_bay_identifier, NULL);
1291
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001292sas_rphy_protocol_attr(identify.initiator_port_protocols,
1293 initiator_port_protocols);
1294sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
1295sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
1296 unsigned long long);
1297sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
Hannes Reineckecdc43ae2016-03-14 10:43:08 +01001298sas_rphy_simple_attr(scsi_target_id, scsi_target_id, "%d\n", u32);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001299
James Bottomley42ab0362006-03-04 09:10:18 -06001300/* only need 8 bytes of data plus header (4 or 8) */
1301#define BUF_SIZE 64
1302
1303int sas_read_port_mode_page(struct scsi_device *sdev)
1304{
1305 char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata;
James Bottomley0f880092010-01-18 10:14:51 -06001306 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
James Bottomley42ab0362006-03-04 09:10:18 -06001307 struct scsi_mode_data mode_data;
1308 int res, error;
1309
James Bottomley42ab0362006-03-04 09:10:18 -06001310 if (!buffer)
1311 return -ENOMEM;
1312
1313 res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3,
1314 &mode_data, NULL);
1315
1316 error = -EINVAL;
1317 if (!scsi_status_is_good(res))
1318 goto out;
1319
1320 msdata = buffer + mode_data.header_length +
1321 mode_data.block_descriptor_length;
1322
1323 if (msdata - buffer > BUF_SIZE - 8)
1324 goto out;
1325
1326 error = 0;
1327
1328 rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0;
1329 rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5];
1330 rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7];
1331
1332 out:
1333 kfree(buffer);
1334 return error;
1335}
1336EXPORT_SYMBOL(sas_read_port_mode_page);
1337
James Bottomley79cb1812006-03-13 13:50:04 -06001338static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
1339 "sas_end_device", NULL, NULL, NULL);
1340
James Bottomley42ab0362006-03-04 09:10:18 -06001341#define sas_end_dev_show_simple(field, name, format_string, cast) \
1342static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001343show_sas_end_dev_##name(struct device *dev, \
1344 struct device_attribute *attr, char *buf) \
James Bottomley42ab0362006-03-04 09:10:18 -06001345{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001346 struct sas_rphy *rphy = transport_class_to_rphy(dev); \
James Bottomley42ab0362006-03-04 09:10:18 -06001347 struct sas_end_device *rdev = rphy_to_end_device(rphy); \
1348 \
1349 return snprintf(buf, 20, format_string, cast rdev->field); \
1350}
1351
1352#define sas_end_dev_simple_attr(field, name, format_string, type) \
1353 sas_end_dev_show_simple(field, name, format_string, (type)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001354static SAS_DEVICE_ATTR(end_dev, name, S_IRUGO, \
James Bottomley42ab0362006-03-04 09:10:18 -06001355 show_sas_end_dev_##name, NULL)
1356
1357sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int);
1358sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout,
1359 "%d\n", int);
1360sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout,
1361 "%d\n", int);
James Bottomley0f880092010-01-18 10:14:51 -06001362sas_end_dev_simple_attr(tlr_supported, tlr_supported,
1363 "%d\n", int);
1364sas_end_dev_simple_attr(tlr_enabled, tlr_enabled,
1365 "%d\n", int);
James Bottomley42ab0362006-03-04 09:10:18 -06001366
James Bottomley79cb1812006-03-13 13:50:04 -06001367static DECLARE_TRANSPORT_CLASS(sas_expander_class,
1368 "sas_expander", NULL, NULL, NULL);
1369
1370#define sas_expander_show_simple(field, name, format_string, cast) \
1371static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001372show_sas_expander_##name(struct device *dev, \
1373 struct device_attribute *attr, char *buf) \
James Bottomley79cb1812006-03-13 13:50:04 -06001374{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001375 struct sas_rphy *rphy = transport_class_to_rphy(dev); \
James Bottomley79cb1812006-03-13 13:50:04 -06001376 struct sas_expander_device *edev = rphy_to_expander_device(rphy); \
1377 \
1378 return snprintf(buf, 20, format_string, cast edev->field); \
1379}
1380
1381#define sas_expander_simple_attr(field, name, format_string, type) \
1382 sas_expander_show_simple(field, name, format_string, (type)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001383static SAS_DEVICE_ATTR(expander, name, S_IRUGO, \
James Bottomley79cb1812006-03-13 13:50:04 -06001384 show_sas_expander_##name, NULL)
1385
1386sas_expander_simple_attr(vendor_id, vendor_id, "%s\n", char *);
1387sas_expander_simple_attr(product_id, product_id, "%s\n", char *);
1388sas_expander_simple_attr(product_rev, product_rev, "%s\n", char *);
1389sas_expander_simple_attr(component_vendor_id, component_vendor_id,
1390 "%s\n", char *);
1391sas_expander_simple_attr(component_id, component_id, "%u\n", unsigned int);
1392sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n",
1393 unsigned int);
1394sas_expander_simple_attr(level, level, "%d\n", int);
James Bottomley42ab0362006-03-04 09:10:18 -06001395
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001396static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
James Bottomley2f8600d2006-03-18 15:00:50 -06001397 "sas_device", NULL, NULL, NULL);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001398
1399static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
1400{
1401 struct Scsi_Host *shost;
1402 struct sas_internal *i;
1403
1404 if (!scsi_is_sas_rphy(dev))
1405 return 0;
1406 shost = dev_to_shost(dev->parent->parent);
1407
1408 if (!shost->transportt)
1409 return 0;
1410 if (shost->transportt->host_attrs.ac.class !=
1411 &sas_host_class.class)
1412 return 0;
1413
1414 i = to_sas_internal(shost->transportt);
1415 return &i->rphy_attr_cont.ac == cont;
1416}
1417
James Bottomley42ab0362006-03-04 09:10:18 -06001418static int sas_end_dev_match(struct attribute_container *cont,
1419 struct device *dev)
1420{
1421 struct Scsi_Host *shost;
1422 struct sas_internal *i;
1423 struct sas_rphy *rphy;
1424
1425 if (!scsi_is_sas_rphy(dev))
1426 return 0;
1427 shost = dev_to_shost(dev->parent->parent);
1428 rphy = dev_to_rphy(dev);
1429
1430 if (!shost->transportt)
1431 return 0;
1432 if (shost->transportt->host_attrs.ac.class !=
1433 &sas_host_class.class)
1434 return 0;
1435
1436 i = to_sas_internal(shost->transportt);
1437 return &i->end_dev_attr_cont.ac == cont &&
James Bottomley2f8600d2006-03-18 15:00:50 -06001438 rphy->identify.device_type == SAS_END_DEVICE;
James Bottomley42ab0362006-03-04 09:10:18 -06001439}
1440
James Bottomley79cb1812006-03-13 13:50:04 -06001441static int sas_expander_match(struct attribute_container *cont,
1442 struct device *dev)
1443{
1444 struct Scsi_Host *shost;
1445 struct sas_internal *i;
1446 struct sas_rphy *rphy;
1447
1448 if (!scsi_is_sas_rphy(dev))
1449 return 0;
1450 shost = dev_to_shost(dev->parent->parent);
1451 rphy = dev_to_rphy(dev);
1452
1453 if (!shost->transportt)
1454 return 0;
1455 if (shost->transportt->host_attrs.ac.class !=
1456 &sas_host_class.class)
1457 return 0;
1458
1459 i = to_sas_internal(shost->transportt);
1460 return &i->expander_attr_cont.ac == cont &&
1461 (rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
James Bottomley2f8600d2006-03-18 15:00:50 -06001462 rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE);
James Bottomley79cb1812006-03-13 13:50:04 -06001463}
1464
James Bottomley2f8600d2006-03-18 15:00:50 -06001465static void sas_expander_release(struct device *dev)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001466{
1467 struct sas_rphy *rphy = dev_to_rphy(dev);
James Bottomley2f8600d2006-03-18 15:00:50 -06001468 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001469
FUJITA Tomonori93c20a52008-04-19 00:43:15 +09001470 if (rphy->q)
1471 blk_cleanup_queue(rphy->q);
1472
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001473 put_device(dev->parent);
James Bottomley2f8600d2006-03-18 15:00:50 -06001474 kfree(edev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001475}
1476
James Bottomley2f8600d2006-03-18 15:00:50 -06001477static void sas_end_device_release(struct device *dev)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001478{
James Bottomley2f8600d2006-03-18 15:00:50 -06001479 struct sas_rphy *rphy = dev_to_rphy(dev);
1480 struct sas_end_device *edev = rphy_to_end_device(rphy);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001481
FUJITA Tomonori93c20a52008-04-19 00:43:15 +09001482 if (rphy->q)
1483 blk_cleanup_queue(rphy->q);
1484
James Bottomley2f8600d2006-03-18 15:00:50 -06001485 put_device(dev->parent);
1486 kfree(edev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001487}
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001488
1489/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08001490 * sas_rphy_initialize - common rphy initialization
James Bottomleyc5943d32006-06-12 09:09:18 -05001491 * @rphy: rphy to initialise
1492 *
1493 * Used by both sas_end_device_alloc() and sas_expander_alloc() to
1494 * initialise the common rphy component of each.
1495 */
1496static void sas_rphy_initialize(struct sas_rphy *rphy)
1497{
1498 INIT_LIST_HEAD(&rphy->list);
1499}
1500
1501/**
James Bottomley42ab0362006-03-04 09:10:18 -06001502 * sas_end_device_alloc - allocate an rphy for an end device
Rob Landleyeb448202007-11-03 13:30:39 -05001503 * @parent: which port
James Bottomley42ab0362006-03-04 09:10:18 -06001504 *
1505 * Allocates an SAS remote PHY structure, connected to @parent.
1506 *
1507 * Returns:
1508 * SAS PHY allocated or %NULL if the allocation failed.
1509 */
James Bottomley65c92b02006-06-28 12:22:50 -04001510struct sas_rphy *sas_end_device_alloc(struct sas_port *parent)
James Bottomley42ab0362006-03-04 09:10:18 -06001511{
1512 struct Scsi_Host *shost = dev_to_shost(&parent->dev);
1513 struct sas_end_device *rdev;
1514
1515 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1516 if (!rdev) {
James Bottomley42ab0362006-03-04 09:10:18 -06001517 return NULL;
1518 }
1519
1520 device_initialize(&rdev->rphy.dev);
1521 rdev->rphy.dev.parent = get_device(&parent->dev);
James Bottomley2f8600d2006-03-18 15:00:50 -06001522 rdev->rphy.dev.release = sas_end_device_release;
James Bottomley65c92b02006-06-28 12:22:50 -04001523 if (scsi_is_sas_expander_device(parent->dev.parent)) {
1524 struct sas_rphy *rphy = dev_to_rphy(parent->dev.parent);
Kay Sievers71610f52008-12-03 22:41:36 +01001525 dev_set_name(&rdev->rphy.dev, "end_device-%d:%d:%d",
1526 shost->host_no, rphy->scsi_target_id,
1527 parent->port_identifier);
James Bottomley65c92b02006-06-28 12:22:50 -04001528 } else
Kay Sievers71610f52008-12-03 22:41:36 +01001529 dev_set_name(&rdev->rphy.dev, "end_device-%d:%d",
1530 shost->host_no, parent->port_identifier);
James Bottomley42ab0362006-03-04 09:10:18 -06001531 rdev->rphy.identify.device_type = SAS_END_DEVICE;
James Bottomleyc5943d32006-06-12 09:09:18 -05001532 sas_rphy_initialize(&rdev->rphy);
James Bottomley42ab0362006-03-04 09:10:18 -06001533 transport_setup_device(&rdev->rphy.dev);
1534
1535 return &rdev->rphy;
1536}
1537EXPORT_SYMBOL(sas_end_device_alloc);
1538
James Bottomley79cb1812006-03-13 13:50:04 -06001539/**
1540 * sas_expander_alloc - allocate an rphy for an end device
Rob Landleyeb448202007-11-03 13:30:39 -05001541 * @parent: which port
1542 * @type: SAS_EDGE_EXPANDER_DEVICE or SAS_FANOUT_EXPANDER_DEVICE
James Bottomley79cb1812006-03-13 13:50:04 -06001543 *
1544 * Allocates an SAS remote PHY structure, connected to @parent.
1545 *
1546 * Returns:
1547 * SAS PHY allocated or %NULL if the allocation failed.
1548 */
James Bottomley65c92b02006-06-28 12:22:50 -04001549struct sas_rphy *sas_expander_alloc(struct sas_port *parent,
James Bottomley79cb1812006-03-13 13:50:04 -06001550 enum sas_device_type type)
1551{
1552 struct Scsi_Host *shost = dev_to_shost(&parent->dev);
1553 struct sas_expander_device *rdev;
1554 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1555
1556 BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE &&
1557 type != SAS_FANOUT_EXPANDER_DEVICE);
1558
1559 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1560 if (!rdev) {
James Bottomley79cb1812006-03-13 13:50:04 -06001561 return NULL;
1562 }
1563
1564 device_initialize(&rdev->rphy.dev);
1565 rdev->rphy.dev.parent = get_device(&parent->dev);
James Bottomley2f8600d2006-03-18 15:00:50 -06001566 rdev->rphy.dev.release = sas_expander_release;
James Bottomley79cb1812006-03-13 13:50:04 -06001567 mutex_lock(&sas_host->lock);
1568 rdev->rphy.scsi_target_id = sas_host->next_expander_id++;
1569 mutex_unlock(&sas_host->lock);
Kay Sievers71610f52008-12-03 22:41:36 +01001570 dev_set_name(&rdev->rphy.dev, "expander-%d:%d",
1571 shost->host_no, rdev->rphy.scsi_target_id);
James Bottomley79cb1812006-03-13 13:50:04 -06001572 rdev->rphy.identify.device_type = type;
James Bottomleyc5943d32006-06-12 09:09:18 -05001573 sas_rphy_initialize(&rdev->rphy);
James Bottomley79cb1812006-03-13 13:50:04 -06001574 transport_setup_device(&rdev->rphy.dev);
1575
1576 return &rdev->rphy;
1577}
1578EXPORT_SYMBOL(sas_expander_alloc);
James Bottomley42ab0362006-03-04 09:10:18 -06001579
1580/**
Rob Landleyeb448202007-11-03 13:30:39 -05001581 * sas_rphy_add - add a SAS remote PHY to the device hierarchy
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001582 * @rphy: The remote PHY to be added
1583 *
1584 * Publishes a SAS remote PHY to the rest of the system.
1585 */
1586int sas_rphy_add(struct sas_rphy *rphy)
1587{
James Bottomley65c92b02006-06-28 12:22:50 -04001588 struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001589 struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
1590 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1591 struct sas_identify *identify = &rphy->identify;
1592 int error;
1593
1594 if (parent->rphy)
1595 return -ENXIO;
1596 parent->rphy = rphy;
1597
1598 error = device_add(&rphy->dev);
1599 if (error)
1600 return error;
1601 transport_add_device(&rphy->dev);
1602 transport_configure_device(&rphy->dev);
James Bottomley39dca552007-07-20 18:22:17 -05001603 if (sas_bsg_initialize(shost, rphy))
Kay Sievers71610f52008-12-03 22:41:36 +01001604 printk("fail to a bsg device %s\n", dev_name(&rphy->dev));
James Bottomley39dca552007-07-20 18:22:17 -05001605
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001606
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001607 mutex_lock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001608 list_add_tail(&rphy->list, &sas_host->rphy_list);
1609 if (identify->device_type == SAS_END_DEVICE &&
1610 (identify->target_port_protocols &
1611 (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
1612 rphy->scsi_target_id = sas_host->next_target_id++;
James Bottomley7676f832006-04-14 09:47:59 -05001613 else if (identify->device_type == SAS_END_DEVICE)
1614 rphy->scsi_target_id = -1;
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001615 mutex_unlock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001616
James Bottomley79cb1812006-03-13 13:50:04 -06001617 if (identify->device_type == SAS_END_DEVICE &&
1618 rphy->scsi_target_id != -1) {
Dan Williams2fc62e22011-09-20 15:10:19 -07001619 int lun;
1620
1621 if (identify->target_port_protocols & SAS_PROTOCOL_SSP)
1622 lun = SCAN_WILD_CARD;
1623 else
1624 lun = 0;
1625
Hannes Reinecke1d645082016-03-17 08:39:45 +01001626 scsi_scan_target(&rphy->dev, 0, rphy->scsi_target_id, lun,
1627 SCSI_SCAN_INITIAL);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001628 }
1629
1630 return 0;
1631}
1632EXPORT_SYMBOL(sas_rphy_add);
1633
1634/**
Rob Landleyeb448202007-11-03 13:30:39 -05001635 * sas_rphy_free - free a SAS remote PHY
1636 * @rphy: SAS remote PHY to free
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001637 *
1638 * Frees the specified SAS remote PHY.
1639 *
1640 * Note:
1641 * This function must only be called on a remote
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001642 * PHY that has not successfully been added using
Darrick J. Wong6f63caa2007-01-26 14:08:43 -08001643 * sas_rphy_add() (or has been sas_rphy_remove()'d)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001644 */
1645void sas_rphy_free(struct sas_rphy *rphy)
1646{
Mike Anderson92aab642006-03-27 09:37:28 -08001647 struct device *dev = &rphy->dev;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001648 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
1649 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1650
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001651 mutex_lock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001652 list_del(&rphy->list);
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001653 mutex_unlock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001654
Mike Anderson92aab642006-03-27 09:37:28 -08001655 transport_destroy_device(dev);
James Bottomley2f8600d2006-03-18 15:00:50 -06001656
Mike Anderson92aab642006-03-27 09:37:28 -08001657 put_device(dev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001658}
1659EXPORT_SYMBOL(sas_rphy_free);
1660
1661/**
Rob Landleyeb448202007-11-03 13:30:39 -05001662 * sas_rphy_delete - remove and free SAS remote PHY
Darrick J. Wong6f63caa2007-01-26 14:08:43 -08001663 * @rphy: SAS remote PHY to remove and free
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001664 *
Darrick J. Wong6f63caa2007-01-26 14:08:43 -08001665 * Removes the specified SAS remote PHY and frees it.
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001666 */
1667void
1668sas_rphy_delete(struct sas_rphy *rphy)
1669{
Darrick J. Wong6f63caa2007-01-26 14:08:43 -08001670 sas_rphy_remove(rphy);
1671 sas_rphy_free(rphy);
1672}
1673EXPORT_SYMBOL(sas_rphy_delete);
1674
1675/**
Dan Williams87c83312011-11-17 17:59:51 -08001676 * sas_rphy_unlink - unlink SAS remote PHY
1677 * @rphy: SAS remote phy to unlink from its parent port
1678 *
1679 * Removes port reference to an rphy
1680 */
1681void sas_rphy_unlink(struct sas_rphy *rphy)
1682{
1683 struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
1684
1685 parent->rphy = NULL;
1686}
1687EXPORT_SYMBOL(sas_rphy_unlink);
1688
1689/**
Rob Landleyeb448202007-11-03 13:30:39 -05001690 * sas_rphy_remove - remove SAS remote PHY
Darrick J. Wong6f63caa2007-01-26 14:08:43 -08001691 * @rphy: SAS remote phy to remove
1692 *
1693 * Removes the specified SAS remote PHY.
1694 */
1695void
1696sas_rphy_remove(struct sas_rphy *rphy)
1697{
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001698 struct device *dev = &rphy->dev;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001699
Christoph Hellwigd4054232006-01-04 13:45:20 +01001700 switch (rphy->identify.device_type) {
1701 case SAS_END_DEVICE:
1702 scsi_remove_target(dev);
1703 break;
1704 case SAS_EDGE_EXPANDER_DEVICE:
1705 case SAS_FANOUT_EXPANDER_DEVICE:
James Bottomley65c92b02006-06-28 12:22:50 -04001706 sas_remove_children(dev);
Christoph Hellwigd4054232006-01-04 13:45:20 +01001707 break;
1708 default:
1709 break;
1710 }
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001711
Dan Williams87c83312011-11-17 17:59:51 -08001712 sas_rphy_unlink(rphy);
Joe Lawrence6aa6caf2014-05-22 17:30:54 -04001713 sas_bsg_remove(NULL, rphy);
Christoph Hellwigfe8b2302005-09-25 23:10:33 +02001714 transport_remove_device(dev);
1715 device_del(dev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001716}
Darrick J. Wong6f63caa2007-01-26 14:08:43 -08001717EXPORT_SYMBOL(sas_rphy_remove);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001718
1719/**
Rob Landleyeb448202007-11-03 13:30:39 -05001720 * scsi_is_sas_rphy - check if a struct device represents a SAS remote PHY
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001721 * @dev: device to check
1722 *
1723 * Returns:
1724 * %1 if the device represents a SAS remote PHY, %0 else
1725 */
1726int scsi_is_sas_rphy(const struct device *dev)
1727{
James Bottomley2f8600d2006-03-18 15:00:50 -06001728 return dev->release == sas_end_device_release ||
1729 dev->release == sas_expander_release;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001730}
1731EXPORT_SYMBOL(scsi_is_sas_rphy);
1732
1733
1734/*
1735 * SCSI scan helper
1736 */
1737
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001738static int sas_user_scan(struct Scsi_Host *shost, uint channel,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001739 uint id, u64 lun)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001740{
1741 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1742 struct sas_rphy *rphy;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001743
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001744 mutex_lock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001745 list_for_each_entry(rphy, &sas_host->rphy_list, list) {
James Bottomley6d99a3f2006-05-19 10:49:37 -05001746 if (rphy->identify.device_type != SAS_END_DEVICE ||
1747 rphy->scsi_target_id == -1)
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001748 continue;
1749
James Bottomleye8bf3942006-07-11 17:49:34 -04001750 if ((channel == SCAN_WILD_CARD || channel == 0) &&
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001751 (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
Hannes Reinecke1d645082016-03-17 08:39:45 +01001752 scsi_scan_target(&rphy->dev, 0, rphy->scsi_target_id,
1753 lun, SCSI_SCAN_MANUAL);
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001754 }
1755 }
1756 mutex_unlock(&sas_host->lock);
1757
1758 return 0;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001759}
1760
1761
1762/*
1763 * Setup / Teardown code
1764 */
1765
James Bottomleyd24e1ee2006-09-06 19:25:22 -05001766#define SETUP_TEMPLATE(attrb, field, perm, test) \
Tony Jonesee959b02008-02-22 00:13:36 +01001767 i->private_##attrb[count] = dev_attr_##field; \
James Bottomley42ab0362006-03-04 09:10:18 -06001768 i->private_##attrb[count].attr.mode = perm; \
James Bottomley42ab0362006-03-04 09:10:18 -06001769 i->attrb[count] = &i->private_##attrb[count]; \
1770 if (test) \
1771 count++
1772
James Bottomleyd24e1ee2006-09-06 19:25:22 -05001773#define SETUP_TEMPLATE_RW(attrb, field, perm, test, ro_test, ro_perm) \
Tony Jonesee959b02008-02-22 00:13:36 +01001774 i->private_##attrb[count] = dev_attr_##field; \
James Bottomleyd24e1ee2006-09-06 19:25:22 -05001775 i->private_##attrb[count].attr.mode = perm; \
1776 if (ro_test) { \
1777 i->private_##attrb[count].attr.mode = ro_perm; \
1778 i->private_##attrb[count].store = NULL; \
1779 } \
1780 i->attrb[count] = &i->private_##attrb[count]; \
1781 if (test) \
1782 count++
James Bottomley42ab0362006-03-04 09:10:18 -06001783
1784#define SETUP_RPORT_ATTRIBUTE(field) \
1785 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001786
James Bottomleydd9fbb522006-03-02 16:01:31 -06001787#define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func) \
James Bottomley42ab0362006-03-04 09:10:18 -06001788 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
James Bottomleydd9fbb522006-03-02 16:01:31 -06001789
James Bottomley65c92b02006-06-28 12:22:50 -04001790#define SETUP_PHY_ATTRIBUTE(field) \
James Bottomley42ab0362006-03-04 09:10:18 -06001791 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001792
James Bottomleyd24e1ee2006-09-06 19:25:22 -05001793#define SETUP_PHY_ATTRIBUTE_RW(field) \
1794 SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1, \
1795 !i->f->set_phy_speed, S_IRUGO)
1796
Darrick J. Wongacbf1672007-01-11 14:14:57 -08001797#define SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(field, func) \
1798 SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1, \
1799 !i->f->func, S_IRUGO)
1800
James Bottomley65c92b02006-06-28 12:22:50 -04001801#define SETUP_PORT_ATTRIBUTE(field) \
1802 SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
1803
1804#define SETUP_OPTIONAL_PHY_ATTRIBUTE(field, func) \
James Bottomley42ab0362006-03-04 09:10:18 -06001805 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
James Bottomleydd9fbb522006-03-02 16:01:31 -06001806
James Bottomley65c92b02006-06-28 12:22:50 -04001807#define SETUP_PHY_ATTRIBUTE_WRONLY(field) \
Darrick J. Wongfe3b5bf2007-01-11 14:15:35 -08001808 SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, 1)
Christoph Hellwig07ba3a92005-10-19 20:01:31 +02001809
James Bottomley65c92b02006-06-28 12:22:50 -04001810#define SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(field, func) \
Darrick J. Wongfe3b5bf2007-01-11 14:15:35 -08001811 SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, i->f->func)
James Bottomleydd9fbb522006-03-02 16:01:31 -06001812
James Bottomley42ab0362006-03-04 09:10:18 -06001813#define SETUP_END_DEV_ATTRIBUTE(field) \
1814 SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001815
James Bottomley79cb1812006-03-13 13:50:04 -06001816#define SETUP_EXPANDER_ATTRIBUTE(field) \
1817 SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1)
1818
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001819/**
Rob Landleyeb448202007-11-03 13:30:39 -05001820 * sas_attach_transport - instantiate SAS transport template
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001821 * @ft: SAS transport class function template
1822 */
1823struct scsi_transport_template *
1824sas_attach_transport(struct sas_function_template *ft)
1825{
1826 struct sas_internal *i;
1827 int count;
1828
Jes Sorensen24669f752006-01-16 10:31:18 -05001829 i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001830 if (!i)
1831 return NULL;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001832
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001833 i->t.user_scan = sas_user_scan;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001834
1835 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1836 i->t.host_attrs.ac.class = &sas_host_class.class;
1837 i->t.host_attrs.ac.match = sas_host_match;
1838 transport_container_register(&i->t.host_attrs);
1839 i->t.host_size = sizeof(struct sas_host_attrs);
1840
1841 i->phy_attr_cont.ac.class = &sas_phy_class.class;
1842 i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
1843 i->phy_attr_cont.ac.match = sas_phy_match;
1844 transport_container_register(&i->phy_attr_cont);
1845
James Bottomley65c92b02006-06-28 12:22:50 -04001846 i->port_attr_cont.ac.class = &sas_port_class.class;
1847 i->port_attr_cont.ac.attrs = &i->port_attrs[0];
1848 i->port_attr_cont.ac.match = sas_port_match;
1849 transport_container_register(&i->port_attr_cont);
1850
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001851 i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
1852 i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
1853 i->rphy_attr_cont.ac.match = sas_rphy_match;
1854 transport_container_register(&i->rphy_attr_cont);
1855
James Bottomley42ab0362006-03-04 09:10:18 -06001856 i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class;
1857 i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0];
1858 i->end_dev_attr_cont.ac.match = sas_end_dev_match;
1859 transport_container_register(&i->end_dev_attr_cont);
1860
James Bottomley79cb1812006-03-13 13:50:04 -06001861 i->expander_attr_cont.ac.class = &sas_expander_class.class;
1862 i->expander_attr_cont.ac.attrs = &i->expander_attrs[0];
1863 i->expander_attr_cont.ac.match = sas_expander_match;
1864 transport_container_register(&i->expander_attr_cont);
1865
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001866 i->f = ft;
1867
1868 count = 0;
James Bottomley65c92b02006-06-28 12:22:50 -04001869 SETUP_PHY_ATTRIBUTE(initiator_port_protocols);
1870 SETUP_PHY_ATTRIBUTE(target_port_protocols);
1871 SETUP_PHY_ATTRIBUTE(device_type);
1872 SETUP_PHY_ATTRIBUTE(sas_address);
1873 SETUP_PHY_ATTRIBUTE(phy_identifier);
1874 //SETUP_PHY_ATTRIBUTE(port_identifier);
1875 SETUP_PHY_ATTRIBUTE(negotiated_linkrate);
1876 SETUP_PHY_ATTRIBUTE(minimum_linkrate_hw);
James Bottomleyd24e1ee2006-09-06 19:25:22 -05001877 SETUP_PHY_ATTRIBUTE_RW(minimum_linkrate);
James Bottomley65c92b02006-06-28 12:22:50 -04001878 SETUP_PHY_ATTRIBUTE(maximum_linkrate_hw);
James Bottomleyd24e1ee2006-09-06 19:25:22 -05001879 SETUP_PHY_ATTRIBUTE_RW(maximum_linkrate);
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +02001880
James Bottomley65c92b02006-06-28 12:22:50 -04001881 SETUP_PHY_ATTRIBUTE(invalid_dword_count);
1882 SETUP_PHY_ATTRIBUTE(running_disparity_error_count);
1883 SETUP_PHY_ATTRIBUTE(loss_of_dword_sync_count);
1884 SETUP_PHY_ATTRIBUTE(phy_reset_problem_count);
1885 SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(link_reset, phy_reset);
1886 SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
Darrick J. Wongacbf1672007-01-11 14:14:57 -08001887 SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(enable, phy_enable);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001888 i->phy_attrs[count] = NULL;
1889
1890 count = 0;
James Bottomley65c92b02006-06-28 12:22:50 -04001891 SETUP_PORT_ATTRIBUTE(num_phys);
1892 i->port_attrs[count] = NULL;
1893
1894 count = 0;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001895 SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
1896 SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
1897 SETUP_RPORT_ATTRIBUTE(rphy_device_type);
1898 SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
1899 SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
Hannes Reineckecdc43ae2016-03-14 10:43:08 +01001900 SETUP_RPORT_ATTRIBUTE(rphy_scsi_target_id);
James Bottomleydd9fbb522006-03-02 16:01:31 -06001901 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
1902 get_enclosure_identifier);
1903 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
1904 get_bay_identifier);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001905 i->rphy_attrs[count] = NULL;
1906
James Bottomley42ab0362006-03-04 09:10:18 -06001907 count = 0;
1908 SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning);
1909 SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout);
1910 SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout);
James Bottomley0f880092010-01-18 10:14:51 -06001911 SETUP_END_DEV_ATTRIBUTE(end_dev_tlr_supported);
1912 SETUP_END_DEV_ATTRIBUTE(end_dev_tlr_enabled);
James Bottomley42ab0362006-03-04 09:10:18 -06001913 i->end_dev_attrs[count] = NULL;
1914
James Bottomley79cb1812006-03-13 13:50:04 -06001915 count = 0;
1916 SETUP_EXPANDER_ATTRIBUTE(vendor_id);
1917 SETUP_EXPANDER_ATTRIBUTE(product_id);
1918 SETUP_EXPANDER_ATTRIBUTE(product_rev);
1919 SETUP_EXPANDER_ATTRIBUTE(component_vendor_id);
1920 SETUP_EXPANDER_ATTRIBUTE(component_id);
1921 SETUP_EXPANDER_ATTRIBUTE(component_revision_id);
1922 SETUP_EXPANDER_ATTRIBUTE(level);
1923 i->expander_attrs[count] = NULL;
1924
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001925 return &i->t;
1926}
1927EXPORT_SYMBOL(sas_attach_transport);
1928
1929/**
Rob Landleyeb448202007-11-03 13:30:39 -05001930 * sas_release_transport - release SAS transport template instance
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001931 * @t: transport template instance
1932 */
1933void sas_release_transport(struct scsi_transport_template *t)
1934{
1935 struct sas_internal *i = to_sas_internal(t);
1936
1937 transport_container_unregister(&i->t.host_attrs);
1938 transport_container_unregister(&i->phy_attr_cont);
James Bottomley65c92b02006-06-28 12:22:50 -04001939 transport_container_unregister(&i->port_attr_cont);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001940 transport_container_unregister(&i->rphy_attr_cont);
James Bottomleydb82f842006-03-09 22:06:36 -05001941 transport_container_unregister(&i->end_dev_attr_cont);
James Bottomley79cb1812006-03-13 13:50:04 -06001942 transport_container_unregister(&i->expander_attr_cont);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001943
1944 kfree(i);
1945}
1946EXPORT_SYMBOL(sas_release_transport);
1947
1948static __init int sas_transport_init(void)
1949{
1950 int error;
1951
1952 error = transport_class_register(&sas_host_class);
1953 if (error)
1954 goto out;
1955 error = transport_class_register(&sas_phy_class);
1956 if (error)
1957 goto out_unregister_transport;
James Bottomley65c92b02006-06-28 12:22:50 -04001958 error = transport_class_register(&sas_port_class);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001959 if (error)
1960 goto out_unregister_phy;
James Bottomley65c92b02006-06-28 12:22:50 -04001961 error = transport_class_register(&sas_rphy_class);
1962 if (error)
1963 goto out_unregister_port;
James Bottomley42ab0362006-03-04 09:10:18 -06001964 error = transport_class_register(&sas_end_dev_class);
1965 if (error)
1966 goto out_unregister_rphy;
James Bottomley79cb1812006-03-13 13:50:04 -06001967 error = transport_class_register(&sas_expander_class);
1968 if (error)
1969 goto out_unregister_end_dev;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001970
1971 return 0;
1972
James Bottomley79cb1812006-03-13 13:50:04 -06001973 out_unregister_end_dev:
1974 transport_class_unregister(&sas_end_dev_class);
James Bottomley42ab0362006-03-04 09:10:18 -06001975 out_unregister_rphy:
1976 transport_class_unregister(&sas_rphy_class);
James Bottomley65c92b02006-06-28 12:22:50 -04001977 out_unregister_port:
1978 transport_class_unregister(&sas_port_class);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001979 out_unregister_phy:
1980 transport_class_unregister(&sas_phy_class);
1981 out_unregister_transport:
1982 transport_class_unregister(&sas_host_class);
1983 out:
1984 return error;
1985
1986}
1987
1988static void __exit sas_transport_exit(void)
1989{
1990 transport_class_unregister(&sas_host_class);
1991 transport_class_unregister(&sas_phy_class);
James Bottomley65c92b02006-06-28 12:22:50 -04001992 transport_class_unregister(&sas_port_class);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001993 transport_class_unregister(&sas_rphy_class);
James Bottomley42ab0362006-03-04 09:10:18 -06001994 transport_class_unregister(&sas_end_dev_class);
James Bottomley79cb1812006-03-13 13:50:04 -06001995 transport_class_unregister(&sas_expander_class);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001996}
1997
1998MODULE_AUTHOR("Christoph Hellwig");
Alexis Bruemmer86b9c4c2007-01-16 15:36:12 -08001999MODULE_DESCRIPTION("SAS Transport Attributes");
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02002000MODULE_LICENSE("GPL");
2001
2002module_init(sas_transport_init);
2003module_exit(sas_transport_exit);