aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/scsi/libsas
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/scsi/libsas')
-rw-r--r--drivers/scsi/libsas/Kconfig39
-rw-r--r--drivers/scsi/libsas/Makefile36
-rw-r--r--drivers/scsi/libsas/sas_discover.c749
-rw-r--r--drivers/scsi/libsas/sas_dump.c76
-rw-r--r--drivers/scsi/libsas/sas_dump.h42
-rw-r--r--drivers/scsi/libsas/sas_event.c75
-rw-r--r--drivers/scsi/libsas/sas_expander.c1855
-rw-r--r--drivers/scsi/libsas/sas_init.c267
-rw-r--r--drivers/scsi/libsas/sas_internal.h146
-rw-r--r--drivers/scsi/libsas/sas_phy.c158
-rw-r--r--drivers/scsi/libsas/sas_port.c279
-rw-r--r--drivers/scsi/libsas/sas_scsi_host.c786
12 files changed, 4508 insertions, 0 deletions
diff --git a/drivers/scsi/libsas/Kconfig b/drivers/scsi/libsas/Kconfig
new file mode 100644
index 000000000000..aafdc92f8312
--- /dev/null
+++ b/drivers/scsi/libsas/Kconfig
@@ -0,0 +1,39 @@
1#
2# Kernel configuration file for the SAS Class
3#
4# Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5# Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6#
7# This file is licensed under GPLv2.
8#
9# This program is free software; you can redistribute it and/or
10# modify it under the terms of the GNU General Public License as
11# published by the Free Software Foundation; version 2 of the
12# License.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17# General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22# USA
23#
24
25config SCSI_SAS_LIBSAS
26 tristate "SAS Domain Transport Attributes"
27 depends on SCSI
28 select SCSI_SAS_ATTRS
29 help
30 This provides transport specific helpers for SAS drivers which
31 use the domain device construct (like the aic94xxx).
32
33config SCSI_SAS_LIBSAS_DEBUG
34 bool "Compile the SAS Domain Transport Attributes in debug mode"
35 default y
36 depends on SCSI_SAS_LIBSAS
37 help
38 Compiles the SAS Layer in debug mode. In debug mode, the
39 SAS Layer prints diagnostic and debug messages.
diff --git a/drivers/scsi/libsas/Makefile b/drivers/scsi/libsas/Makefile
new file mode 100644
index 000000000000..44d972a3b4bd
--- /dev/null
+++ b/drivers/scsi/libsas/Makefile
@@ -0,0 +1,36 @@
1#
2# Kernel Makefile for the libsas helpers
3#
4# Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5# Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6#
7# This file is licensed under GPLv2.
8#
9# This program is free software; you can redistribute it and/or
10# modify it under the terms of the GNU General Public License as
11# published by the Free Software Foundation; version 2 of the
12# License.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17# General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22# USA
23
24ifeq ($(CONFIG_SCSI_SAS_LIBSAS_DEBUG),y)
25 EXTRA_CFLAGS += -DSAS_DEBUG
26endif
27
28obj-$(CONFIG_SCSI_SAS_LIBSAS) += libsas.o
29libsas-y += sas_init.o \
30 sas_phy.o \
31 sas_port.o \
32 sas_event.o \
33 sas_dump.o \
34 sas_discover.o \
35 sas_expander.o \
36 sas_scsi_host.o
diff --git a/drivers/scsi/libsas/sas_discover.c b/drivers/scsi/libsas/sas_discover.c
new file mode 100644
index 000000000000..d977bd492d8d
--- /dev/null
+++ b/drivers/scsi/libsas/sas_discover.c
@@ -0,0 +1,749 @@
1/*
2 * Serial Attached SCSI (SAS) Discover process
3 *
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6 *
7 * This file is licensed under GPLv2.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 */
24
25#include <linux/pci.h>
26#include <linux/scatterlist.h>
27#include <scsi/scsi_host.h>
28#include <scsi/scsi_eh.h>
29#include "sas_internal.h"
30
31#include <scsi/scsi_transport.h>
32#include <scsi/scsi_transport_sas.h>
33#include "../scsi_sas_internal.h"
34
35/* ---------- Basic task processing for discovery purposes ---------- */
36
37void sas_init_dev(struct domain_device *dev)
38{
39 INIT_LIST_HEAD(&dev->siblings);
40 INIT_LIST_HEAD(&dev->dev_list_node);
41 switch (dev->dev_type) {
42 case SAS_END_DEV:
43 break;
44 case EDGE_DEV:
45 case FANOUT_DEV:
46 INIT_LIST_HEAD(&dev->ex_dev.children);
47 break;
48 case SATA_DEV:
49 case SATA_PM:
50 case SATA_PM_PORT:
51 INIT_LIST_HEAD(&dev->sata_dev.children);
52 break;
53 default:
54 break;
55 }
56}
57
58static void sas_task_timedout(unsigned long _task)
59{
60 struct sas_task *task = (void *) _task;
61 unsigned long flags;
62
63 spin_lock_irqsave(&task->task_state_lock, flags);
64 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
65 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
66 spin_unlock_irqrestore(&task->task_state_lock, flags);
67
68 complete(&task->completion);
69}
70
71static void sas_disc_task_done(struct sas_task *task)
72{
73 if (!del_timer(&task->timer))
74 return;
75 complete(&task->completion);
76}
77
78#define SAS_DEV_TIMEOUT 10
79
80/**
81 * sas_execute_task -- Basic task processing for discovery
82 * @task: the task to be executed
83 * @buffer: pointer to buffer to do I/O
84 * @size: size of @buffer
85 * @pci_dma_dir: PCI_DMA_...
86 */
87static int sas_execute_task(struct sas_task *task, void *buffer, int size,
88 int pci_dma_dir)
89{
90 int res = 0;
91 struct scatterlist *scatter = NULL;
92 struct task_status_struct *ts = &task->task_status;
93 int num_scatter = 0;
94 int retries = 0;
95 struct sas_internal *i =
96 to_sas_internal(task->dev->port->ha->core.shost->transportt);
97
98 if (pci_dma_dir != PCI_DMA_NONE) {
99 scatter = kzalloc(sizeof(*scatter), GFP_KERNEL);
100 if (!scatter)
101 goto out;
102
103 sg_init_one(scatter, buffer, size);
104 num_scatter = 1;
105 }
106
107 task->task_proto = task->dev->tproto;
108 task->scatter = scatter;
109 task->num_scatter = num_scatter;
110 task->total_xfer_len = size;
111 task->data_dir = pci_dma_dir;
112 task->task_done = sas_disc_task_done;
113
114 for (retries = 0; retries < 5; retries++) {
115 task->task_state_flags = SAS_TASK_STATE_PENDING;
116 init_completion(&task->completion);
117
118 task->timer.data = (unsigned long) task;
119 task->timer.function = sas_task_timedout;
120 task->timer.expires = jiffies + SAS_DEV_TIMEOUT*HZ;
121 add_timer(&task->timer);
122
123 res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
124 if (res) {
125 del_timer(&task->timer);
126 SAS_DPRINTK("executing SAS discovery task failed:%d\n",
127 res);
128 goto ex_err;
129 }
130 wait_for_completion(&task->completion);
131 res = -ETASK;
132 if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
133 int res2;
134 SAS_DPRINTK("task aborted, flags:0x%x\n",
135 task->task_state_flags);
136 res2 = i->dft->lldd_abort_task(task);
137 SAS_DPRINTK("came back from abort task\n");
138 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
139 if (res2 == TMF_RESP_FUNC_COMPLETE)
140 continue; /* Retry the task */
141 else
142 goto ex_err;
143 }
144 }
145 if (task->task_status.stat == SAM_BUSY ||
146 task->task_status.stat == SAM_TASK_SET_FULL ||
147 task->task_status.stat == SAS_QUEUE_FULL) {
148 SAS_DPRINTK("task: q busy, sleeping...\n");
149 schedule_timeout_interruptible(HZ);
150 } else if (task->task_status.stat == SAM_CHECK_COND) {
151 struct scsi_sense_hdr shdr;
152
153 if (!scsi_normalize_sense(ts->buf, ts->buf_valid_size,
154 &shdr)) {
155 SAS_DPRINTK("couldn't normalize sense\n");
156 continue;
157 }
158 if ((shdr.sense_key == 6 && shdr.asc == 0x29) ||
159 (shdr.sense_key == 2 && shdr.asc == 4 &&
160 shdr.ascq == 1)) {
161 SAS_DPRINTK("device %016llx LUN: %016llx "
162 "powering up or not ready yet, "
163 "sleeping...\n",
164 SAS_ADDR(task->dev->sas_addr),
165 SAS_ADDR(task->ssp_task.LUN));
166
167 schedule_timeout_interruptible(5*HZ);
168 } else if (shdr.sense_key == 1) {
169 res = 0;
170 break;
171 } else if (shdr.sense_key == 5) {
172 break;
173 } else {
174 SAS_DPRINTK("dev %016llx LUN: %016llx "
175 "sense key:0x%x ASC:0x%x ASCQ:0x%x"
176 "\n",
177 SAS_ADDR(task->dev->sas_addr),
178 SAS_ADDR(task->ssp_task.LUN),
179 shdr.sense_key,
180 shdr.asc, shdr.ascq);
181 }
182 } else if (task->task_status.resp != SAS_TASK_COMPLETE ||
183 task->task_status.stat != SAM_GOOD) {
184 SAS_DPRINTK("task finished with resp:0x%x, "
185 "stat:0x%x\n",
186 task->task_status.resp,
187 task->task_status.stat);
188 goto ex_err;
189 } else {
190 res = 0;
191 break;
192 }
193 }
194ex_err:
195 if (pci_dma_dir != PCI_DMA_NONE)
196 kfree(scatter);
197out:
198 return res;
199}
200
201/* ---------- Domain device discovery ---------- */
202
203/**
204 * sas_get_port_device -- Discover devices which caused port creation
205 * @port: pointer to struct sas_port of interest
206 *
207 * Devices directly attached to a HA port, have no parent. This is
208 * how we know they are (domain) "root" devices. All other devices
209 * do, and should have their "parent" pointer set appropriately as
210 * soon as a child device is discovered.
211 */
212static int sas_get_port_device(struct asd_sas_port *port)
213{
214 unsigned long flags;
215 struct asd_sas_phy *phy;
216 struct sas_rphy *rphy;
217 struct domain_device *dev;
218
219 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
220 if (!dev)
221 return -ENOMEM;
222
223 spin_lock_irqsave(&port->phy_list_lock, flags);
224 if (list_empty(&port->phy_list)) {
225 spin_unlock_irqrestore(&port->phy_list_lock, flags);
226 kfree(dev);
227 return -ENODEV;
228 }
229 phy = container_of(port->phy_list.next, struct asd_sas_phy, port_phy_el);
230 spin_lock(&phy->frame_rcvd_lock);
231 memcpy(dev->frame_rcvd, phy->frame_rcvd, min(sizeof(dev->frame_rcvd),
232 (size_t)phy->frame_rcvd_size));
233 spin_unlock(&phy->frame_rcvd_lock);
234 spin_unlock_irqrestore(&port->phy_list_lock, flags);
235
236 if (dev->frame_rcvd[0] == 0x34 && port->oob_mode == SATA_OOB_MODE) {
237 struct dev_to_host_fis *fis =
238 (struct dev_to_host_fis *) dev->frame_rcvd;
239 if (fis->interrupt_reason == 1 && fis->lbal == 1 &&
240 fis->byte_count_low==0x69 && fis->byte_count_high == 0x96
241 && (fis->device & ~0x10) == 0)
242 dev->dev_type = SATA_PM;
243 else
244 dev->dev_type = SATA_DEV;
245 dev->tproto = SATA_PROTO;
246 } else {
247 struct sas_identify_frame *id =
248 (struct sas_identify_frame *) dev->frame_rcvd;
249 dev->dev_type = id->dev_type;
250 dev->iproto = id->initiator_bits;
251 dev->tproto = id->target_bits;
252 }
253
254 sas_init_dev(dev);
255
256 switch (dev->dev_type) {
257 case SAS_END_DEV:
258 rphy = sas_end_device_alloc(port->port);
259 break;
260 case EDGE_DEV:
261 rphy = sas_expander_alloc(port->port,
262 SAS_EDGE_EXPANDER_DEVICE);
263 break;
264 case FANOUT_DEV:
265 rphy = sas_expander_alloc(port->port,
266 SAS_FANOUT_EXPANDER_DEVICE);
267 break;
268 case SATA_DEV:
269 default:
270 printk("ERROR: Unidentified device type %d\n", dev->dev_type);
271 rphy = NULL;
272 break;
273 }
274
275 if (!rphy) {
276 kfree(dev);
277 return -ENODEV;
278 }
279 rphy->identify.phy_identifier = phy->phy->identify.phy_identifier;
280 memcpy(dev->sas_addr, port->attached_sas_addr, SAS_ADDR_SIZE);
281 sas_fill_in_rphy(dev, rphy);
282 sas_hash_addr(dev->hashed_sas_addr, dev->sas_addr);
283 port->port_dev = dev;
284 dev->port = port;
285 dev->linkrate = port->linkrate;
286 dev->min_linkrate = port->linkrate;
287 dev->max_linkrate = port->linkrate;
288 dev->pathways = port->num_phys;
289 memset(port->disc.fanout_sas_addr, 0, SAS_ADDR_SIZE);
290 memset(port->disc.eeds_a, 0, SAS_ADDR_SIZE);
291 memset(port->disc.eeds_b, 0, SAS_ADDR_SIZE);
292 port->disc.max_level = 0;
293
294 dev->rphy = rphy;
295 spin_lock(&port->dev_list_lock);
296 list_add_tail(&dev->dev_list_node, &port->dev_list);
297 spin_unlock(&port->dev_list_lock);
298
299 return 0;
300}
301
302/* ---------- Discover and Revalidate ---------- */
303
304/* ---------- SATA ---------- */
305
306static void sas_get_ata_command_set(struct domain_device *dev)
307{
308 struct dev_to_host_fis *fis =
309 (struct dev_to_host_fis *) dev->frame_rcvd;
310
311 if ((fis->sector_count == 1 && /* ATA */
312 fis->lbal == 1 &&
313 fis->lbam == 0 &&
314 fis->lbah == 0 &&
315 fis->device == 0)
316 ||
317 (fis->sector_count == 0 && /* CE-ATA (mATA) */
318 fis->lbal == 0 &&
319 fis->lbam == 0xCE &&
320 fis->lbah == 0xAA &&
321 (fis->device & ~0x10) == 0))
322
323 dev->sata_dev.command_set = ATA_COMMAND_SET;
324
325 else if ((fis->interrupt_reason == 1 && /* ATAPI */
326 fis->lbal == 1 &&
327 fis->byte_count_low == 0x14 &&
328 fis->byte_count_high == 0xEB &&
329 (fis->device & ~0x10) == 0))
330
331 dev->sata_dev.command_set = ATAPI_COMMAND_SET;
332
333 else if ((fis->sector_count == 1 && /* SEMB */
334 fis->lbal == 1 &&
335 fis->lbam == 0x3C &&
336 fis->lbah == 0xC3 &&
337 fis->device == 0)
338 ||
339 (fis->interrupt_reason == 1 && /* SATA PM */
340 fis->lbal == 1 &&
341 fis->byte_count_low == 0x69 &&
342 fis->byte_count_high == 0x96 &&
343 (fis->device & ~0x10) == 0))
344
345 /* Treat it as a superset? */
346 dev->sata_dev.command_set = ATAPI_COMMAND_SET;
347}
348
349/**
350 * sas_issue_ata_cmd -- Basic SATA command processing for discovery
351 * @dev: the device to send the command to
352 * @command: the command register
353 * @features: the features register
354 * @buffer: pointer to buffer to do I/O
355 * @size: size of @buffer
356 * @pci_dma_dir: PCI_DMA_...
357 */
358static int sas_issue_ata_cmd(struct domain_device *dev, u8 command,
359 u8 features, void *buffer, int size,
360 int pci_dma_dir)
361{
362 int res = 0;
363 struct sas_task *task;
364 struct dev_to_host_fis *d2h_fis = (struct dev_to_host_fis *)
365 &dev->frame_rcvd[0];
366
367 res = -ENOMEM;
368 task = sas_alloc_task(GFP_KERNEL);
369 if (!task)
370 goto out;
371
372 task->dev = dev;
373
374 task->ata_task.fis.command = command;
375 task->ata_task.fis.features = features;
376 task->ata_task.fis.device = d2h_fis->device;
377 task->ata_task.retry_count = 1;
378
379 res = sas_execute_task(task, buffer, size, pci_dma_dir);
380
381 sas_free_task(task);
382out:
383 return res;
384}
385
386static void sas_sata_propagate_sas_addr(struct domain_device *dev)
387{
388 unsigned long flags;
389 struct asd_sas_port *port = dev->port;
390 struct asd_sas_phy *phy;
391
392 BUG_ON(dev->parent);
393
394 memcpy(port->attached_sas_addr, dev->sas_addr, SAS_ADDR_SIZE);
395 spin_lock_irqsave(&port->phy_list_lock, flags);
396 list_for_each_entry(phy, &port->phy_list, port_phy_el)
397 memcpy(phy->attached_sas_addr, dev->sas_addr, SAS_ADDR_SIZE);
398 spin_unlock_irqrestore(&port->phy_list_lock, flags);
399}
400
401#define ATA_IDENTIFY_DEV 0xEC
402#define ATA_IDENTIFY_PACKET_DEV 0xA1
403#define ATA_SET_FEATURES 0xEF
404#define ATA_FEATURE_PUP_STBY_SPIN_UP 0x07
405
406/**
407 * sas_discover_sata_dev -- discover a STP/SATA device (SATA_DEV)
408 * @dev: STP/SATA device of interest (ATA/ATAPI)
409 *
410 * The LLDD has already been notified of this device, so that we can
411 * send FISes to it. Here we try to get IDENTIFY DEVICE or IDENTIFY
412 * PACKET DEVICE, if ATAPI device, so that the LLDD can fine-tune its
413 * performance for this device.
414 */
415static int sas_discover_sata_dev(struct domain_device *dev)
416{
417 int res;
418 __le16 *identify_x;
419 u8 command;
420
421 identify_x = kzalloc(512, GFP_KERNEL);
422 if (!identify_x)
423 return -ENOMEM;
424
425 if (dev->sata_dev.command_set == ATA_COMMAND_SET) {
426 dev->sata_dev.identify_device = identify_x;
427 command = ATA_IDENTIFY_DEV;
428 } else {
429 dev->sata_dev.identify_packet_device = identify_x;
430 command = ATA_IDENTIFY_PACKET_DEV;
431 }
432
433 res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
434 PCI_DMA_FROMDEVICE);
435 if (res)
436 goto out_err;
437
438 /* lives on the media? */
439 if (le16_to_cpu(identify_x[0]) & 4) {
440 /* incomplete response */
441 SAS_DPRINTK("sending SET FEATURE/PUP_STBY_SPIN_UP to "
442 "dev %llx\n", SAS_ADDR(dev->sas_addr));
443 if (!le16_to_cpu(identify_x[83] & (1<<6)))
444 goto cont1;
445 res = sas_issue_ata_cmd(dev, ATA_SET_FEATURES,
446 ATA_FEATURE_PUP_STBY_SPIN_UP,
447 NULL, 0, PCI_DMA_NONE);
448 if (res)
449 goto cont1;
450
451 schedule_timeout_interruptible(5*HZ); /* More time? */
452 res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
453 PCI_DMA_FROMDEVICE);
454 if (res)
455 goto out_err;
456 }
457cont1:
458 /* Get WWN */
459 if (dev->port->oob_mode != SATA_OOB_MODE) {
460 memcpy(dev->sas_addr, dev->sata_dev.rps_resp.rps.stp_sas_addr,
461 SAS_ADDR_SIZE);
462 } else if (dev->sata_dev.command_set == ATA_COMMAND_SET &&
463 (le16_to_cpu(dev->sata_dev.identify_device[108]) & 0xF000)
464 == 0x5000) {
465 int i;
466
467 for (i = 0; i < 4; i++) {
468 dev->sas_addr[2*i] =
469 (le16_to_cpu(dev->sata_dev.identify_device[108+i]) & 0xFF00) >> 8;
470 dev->sas_addr[2*i+1] =
471 le16_to_cpu(dev->sata_dev.identify_device[108+i]) & 0x00FF;
472 }
473 }
474 sas_hash_addr(dev->hashed_sas_addr, dev->sas_addr);
475 if (!dev->parent)
476 sas_sata_propagate_sas_addr(dev);
477
478 /* XXX Hint: register this SATA device with SATL.
479 When this returns, dev->sata_dev->lu is alive and
480 present.
481 sas_satl_register_dev(dev);
482 */
483 return 0;
484out_err:
485 dev->sata_dev.identify_packet_device = NULL;
486 dev->sata_dev.identify_device = NULL;
487 kfree(identify_x);
488 return res;
489}
490
491static int sas_discover_sata_pm(struct domain_device *dev)
492{
493 return -ENODEV;
494}
495
496int sas_notify_lldd_dev_found(struct domain_device *dev)
497{
498 int res = 0;
499 struct sas_ha_struct *sas_ha = dev->port->ha;
500 struct Scsi_Host *shost = sas_ha->core.shost;
501 struct sas_internal *i = to_sas_internal(shost->transportt);
502
503 if (i->dft->lldd_dev_found) {
504 res = i->dft->lldd_dev_found(dev);
505 if (res) {
506 printk("sas: driver on pcidev %s cannot handle "
507 "device %llx, error:%d\n",
508 pci_name(sas_ha->pcidev),
509 SAS_ADDR(dev->sas_addr), res);
510 }
511 }
512 return res;
513}
514
515
516void sas_notify_lldd_dev_gone(struct domain_device *dev)
517{
518 struct sas_ha_struct *sas_ha = dev->port->ha;
519 struct Scsi_Host *shost = sas_ha->core.shost;
520 struct sas_internal *i = to_sas_internal(shost->transportt);
521
522 if (i->dft->lldd_dev_gone)
523 i->dft->lldd_dev_gone(dev);
524}
525
526/* ---------- Common/dispatchers ---------- */
527
528/**
529 * sas_discover_sata -- discover an STP/SATA domain device
530 * @dev: pointer to struct domain_device of interest
531 *
532 * First we notify the LLDD of this device, so we can send frames to
533 * it. Then depending on the type of device we call the appropriate
534 * discover functions. Once device discover is done, we notify the
535 * LLDD so that it can fine-tune its parameters for the device, by
536 * removing it and then adding it. That is, the second time around,
537 * the driver would have certain fields, that it is looking at, set.
538 * Finally we initialize the kobj so that the device can be added to
539 * the system at registration time. Devices directly attached to a HA
540 * port, have no parents. All other devices do, and should have their
541 * "parent" pointer set appropriately before calling this function.
542 */
543int sas_discover_sata(struct domain_device *dev)
544{
545 int res;
546
547 sas_get_ata_command_set(dev);
548
549 res = sas_notify_lldd_dev_found(dev);
550 if (res)
551 return res;
552
553 switch (dev->dev_type) {
554 case SATA_DEV:
555 res = sas_discover_sata_dev(dev);
556 break;
557 case SATA_PM:
558 res = sas_discover_sata_pm(dev);
559 break;
560 default:
561 break;
562 }
563
564 sas_notify_lldd_dev_gone(dev);
565 if (!res) {
566 sas_notify_lldd_dev_found(dev);
567 }
568 return res;
569}
570
571/**
572 * sas_discover_end_dev -- discover an end device (SSP, etc)
573 * @end: pointer to domain device of interest
574 *
575 * See comment in sas_discover_sata().
576 */
577int sas_discover_end_dev(struct domain_device *dev)
578{
579 int res;
580
581 res = sas_notify_lldd_dev_found(dev);
582 if (res)
583 return res;
584
585 res = sas_rphy_add(dev->rphy);
586 if (res)
587 goto out_err;
588
589 /* do this to get the end device port attributes which will have
590 * been scanned in sas_rphy_add */
591 sas_notify_lldd_dev_gone(dev);
592 sas_notify_lldd_dev_found(dev);
593
594 return 0;
595
596out_err:
597 sas_notify_lldd_dev_gone(dev);
598 return res;
599}
600
601/* ---------- Device registration and unregistration ---------- */
602
603static inline void sas_unregister_common_dev(struct domain_device *dev)
604{
605 sas_notify_lldd_dev_gone(dev);
606 if (!dev->parent)
607 dev->port->port_dev = NULL;
608 else
609 list_del_init(&dev->siblings);
610 list_del_init(&dev->dev_list_node);
611}
612
613void sas_unregister_dev(struct domain_device *dev)
614{
615 if (dev->rphy) {
616 sas_remove_children(&dev->rphy->dev);
617 sas_rphy_delete(dev->rphy);
618 dev->rphy = NULL;
619 }
620 if (dev->dev_type == EDGE_DEV || dev->dev_type == FANOUT_DEV) {
621 /* remove the phys and ports, everything else should be gone */
622 kfree(dev->ex_dev.ex_phy);
623 dev->ex_dev.ex_phy = NULL;
624 }
625 sas_unregister_common_dev(dev);
626}
627
628void sas_unregister_domain_devices(struct asd_sas_port *port)
629{
630 struct domain_device *dev, *n;
631
632 list_for_each_entry_safe_reverse(dev,n,&port->dev_list,dev_list_node)
633 sas_unregister_dev(dev);
634
635 port->port->rphy = NULL;
636
637}
638
639/* ---------- Discovery and Revalidation ---------- */
640
641/**
642 * sas_discover_domain -- discover the domain
643 * @port: port to the domain of interest
644 *
645 * NOTE: this process _must_ quit (return) as soon as any connection
646 * errors are encountered. Connection recovery is done elsewhere.
647 * Discover process only interrogates devices in order to discover the
648 * domain.
649 */
650static void sas_discover_domain(void *data)
651{
652 int error = 0;
653 struct asd_sas_port *port = data;
654
655 sas_begin_event(DISCE_DISCOVER_DOMAIN, &port->disc.disc_event_lock,
656 &port->disc.pending);
657
658 if (port->port_dev)
659 return ;
660 else {
661 error = sas_get_port_device(port);
662 if (error)
663 return;
664 }
665
666 SAS_DPRINTK("DOING DISCOVERY on port %d, pid:%d\n", port->id,
667 current->pid);
668
669 switch (port->port_dev->dev_type) {
670 case SAS_END_DEV:
671 error = sas_discover_end_dev(port->port_dev);
672 break;
673 case EDGE_DEV:
674 case FANOUT_DEV:
675 error = sas_discover_root_expander(port->port_dev);
676 break;
677 case SATA_DEV:
678 case SATA_PM:
679 error = sas_discover_sata(port->port_dev);
680 break;
681 default:
682 SAS_DPRINTK("unhandled device %d\n", port->port_dev->dev_type);
683 break;
684 }
685
686 if (error) {
687 kfree(port->port_dev); /* not kobject_register-ed yet */
688 port->port_dev = NULL;
689 }
690
691 SAS_DPRINTK("DONE DISCOVERY on port %d, pid:%d, result:%d\n", port->id,
692 current->pid, error);
693}
694
695static void sas_revalidate_domain(void *data)
696{
697 int res = 0;
698 struct asd_sas_port *port = data;
699
700 sas_begin_event(DISCE_REVALIDATE_DOMAIN, &port->disc.disc_event_lock,
701 &port->disc.pending);
702
703 SAS_DPRINTK("REVALIDATING DOMAIN on port %d, pid:%d\n", port->id,
704 current->pid);
705 if (port->port_dev)
706 res = sas_ex_revalidate_domain(port->port_dev);
707
708 SAS_DPRINTK("done REVALIDATING DOMAIN on port %d, pid:%d, res 0x%x\n",
709 port->id, current->pid, res);
710}
711
712/* ---------- Events ---------- */
713
714int sas_discover_event(struct asd_sas_port *port, enum discover_event ev)
715{
716 struct sas_discovery *disc;
717
718 if (!port)
719 return 0;
720 disc = &port->disc;
721
722 BUG_ON(ev >= DISC_NUM_EVENTS);
723
724 sas_queue_event(ev, &disc->disc_event_lock, &disc->pending,
725 &disc->disc_work[ev], port->ha->core.shost);
726
727 return 0;
728}
729
730/**
731 * sas_init_disc -- initialize the discovery struct in the port
732 * @port: pointer to struct port
733 *
734 * Called when the ports are being initialized.
735 */
736void sas_init_disc(struct sas_discovery *disc, struct asd_sas_port *port)
737{
738 int i;
739
740 static void (*sas_event_fns[DISC_NUM_EVENTS])(void *) = {
741 [DISCE_DISCOVER_DOMAIN] = sas_discover_domain,
742 [DISCE_REVALIDATE_DOMAIN] = sas_revalidate_domain,
743 };
744
745 spin_lock_init(&disc->disc_event_lock);
746 disc->pending = 0;
747 for (i = 0; i < DISC_NUM_EVENTS; i++)
748 INIT_WORK(&disc->disc_work[i], sas_event_fns[i], port);
749}
diff --git a/drivers/scsi/libsas/sas_dump.c b/drivers/scsi/libsas/sas_dump.c
new file mode 100644
index 000000000000..f1246d2c9bef
--- /dev/null
+++ b/drivers/scsi/libsas/sas_dump.c
@@ -0,0 +1,76 @@
1/*
2 * Serial Attached SCSI (SAS) Dump/Debugging routines
3 *
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6 *
7 * This file is licensed under GPLv2.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 */
24
25#include "sas_dump.h"
26
27#ifdef SAS_DEBUG
28
29static const char *sas_hae_str[] = {
30 [0] = "HAE_RESET",
31};
32
33static const char *sas_porte_str[] = {
34 [0] = "PORTE_BYTES_DMAED",
35 [1] = "PORTE_BROADCAST_RCVD",
36 [2] = "PORTE_LINK_RESET_ERR",
37 [3] = "PORTE_TIMER_EVENT",
38 [4] = "PORTE_HARD_RESET",
39};
40
41static const char *sas_phye_str[] = {
42 [0] = "PHYE_LOSS_OF_SIGNAL",
43 [1] = "PHYE_OOB_DONE",
44 [2] = "PHYE_OOB_ERROR",
45 [3] = "PHYE_SPINUP_HOLD",
46};
47
48void sas_dprint_porte(int phyid, enum port_event pe)
49{
50 SAS_DPRINTK("phy%d: port event: %s\n", phyid, sas_porte_str[pe]);
51}
52void sas_dprint_phye(int phyid, enum phy_event pe)
53{
54 SAS_DPRINTK("phy%d: phy event: %s\n", phyid, sas_phye_str[pe]);
55}
56
57void sas_dprint_hae(struct sas_ha_struct *sas_ha, enum ha_event he)
58{
59 SAS_DPRINTK("ha %s: %s event\n", pci_name(sas_ha->pcidev),
60 sas_hae_str[he]);
61}
62
63void sas_dump_port(struct asd_sas_port *port)
64{
65 SAS_DPRINTK("port%d: class:0x%x\n", port->id, port->class);
66 SAS_DPRINTK("port%d: sas_addr:%llx\n", port->id,
67 SAS_ADDR(port->sas_addr));
68 SAS_DPRINTK("port%d: attached_sas_addr:%llx\n", port->id,
69 SAS_ADDR(port->attached_sas_addr));
70 SAS_DPRINTK("port%d: iproto:0x%x\n", port->id, port->iproto);
71 SAS_DPRINTK("port%d: tproto:0x%x\n", port->id, port->tproto);
72 SAS_DPRINTK("port%d: oob_mode:0x%x\n", port->id, port->oob_mode);
73 SAS_DPRINTK("port%d: num_phys:%d\n", port->id, port->num_phys);
74}
75
76#endif /* SAS_DEBUG */
diff --git a/drivers/scsi/libsas/sas_dump.h b/drivers/scsi/libsas/sas_dump.h
new file mode 100644
index 000000000000..47b45d4f5258
--- /dev/null
+++ b/drivers/scsi/libsas/sas_dump.h
@@ -0,0 +1,42 @@
1/*
2 * Serial Attached SCSI (SAS) Dump/Debugging routines header file
3 *
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6 *
7 * This file is licensed under GPLv2.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 */
24
25#include "sas_internal.h"
26
27#ifdef SAS_DEBUG
28
29void sas_dprint_porte(int phyid, enum port_event pe);
30void sas_dprint_phye(int phyid, enum phy_event pe);
31void sas_dprint_hae(struct sas_ha_struct *sas_ha, enum ha_event he);
32void sas_dump_port(struct asd_sas_port *port);
33
34#else /* SAS_DEBUG */
35
36static inline void sas_dprint_porte(int phyid, enum port_event pe) { }
37static inline void sas_dprint_phye(int phyid, enum phy_event pe) { }
38static inline void sas_dprint_hae(struct sas_ha_struct *sas_ha,
39 enum ha_event he) { }
40static inline void sas_dump_port(struct asd_sas_port *port) { }
41
42#endif /* SAS_DEBUG */
diff --git a/drivers/scsi/libsas/sas_event.c b/drivers/scsi/libsas/sas_event.c
new file mode 100644
index 000000000000..19110ed1c89c
--- /dev/null
+++ b/drivers/scsi/libsas/sas_event.c
@@ -0,0 +1,75 @@
1/*
2 * Serial Attached SCSI (SAS) Event processing
3 *
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6 *
7 * This file is licensed under GPLv2.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 */
24
25#include <scsi/scsi_host.h>
26#include "sas_internal.h"
27#include "sas_dump.h"
28
29static void notify_ha_event(struct sas_ha_struct *sas_ha, enum ha_event event)
30{
31 BUG_ON(event >= HA_NUM_EVENTS);
32
33 sas_queue_event(event, &sas_ha->event_lock, &sas_ha->pending,
34 &sas_ha->ha_events[event], sas_ha->core.shost);
35}
36
37static void notify_port_event(struct asd_sas_phy *phy, enum port_event event)
38{
39 struct sas_ha_struct *ha = phy->ha;
40
41 BUG_ON(event >= PORT_NUM_EVENTS);
42
43 sas_queue_event(event, &ha->event_lock, &phy->port_events_pending,
44 &phy->port_events[event], ha->core.shost);
45}
46
47static void notify_phy_event(struct asd_sas_phy *phy, enum phy_event event)
48{
49 struct sas_ha_struct *ha = phy->ha;
50
51 BUG_ON(event >= PHY_NUM_EVENTS);
52
53 sas_queue_event(event, &ha->event_lock, &phy->phy_events_pending,
54 &phy->phy_events[event], ha->core.shost);
55}
56
57int sas_init_events(struct sas_ha_struct *sas_ha)
58{
59 static void (*sas_ha_event_fns[HA_NUM_EVENTS])(void *) = {
60 [HAE_RESET] = sas_hae_reset,
61 };
62
63 int i;
64
65 spin_lock_init(&sas_ha->event_lock);
66
67 for (i = 0; i < HA_NUM_EVENTS; i++)
68 INIT_WORK(&sas_ha->ha_events[i], sas_ha_event_fns[i], sas_ha);
69
70 sas_ha->notify_ha_event = notify_ha_event;
71 sas_ha->notify_port_event = notify_port_event;
72 sas_ha->notify_phy_event = notify_phy_event;
73
74 return 0;
75}
diff --git a/drivers/scsi/libsas/sas_expander.c b/drivers/scsi/libsas/sas_expander.c
new file mode 100644
index 000000000000..30b8014bcc7a
--- /dev/null
+++ b/drivers/scsi/libsas/sas_expander.c
@@ -0,0 +1,1855 @@
1/*
2 * Serial Attached SCSI (SAS) Expander discovery and configuration
3 *
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6 *
7 * This file is licensed under GPLv2.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 */
24
25#include <linux/pci.h>
26#include <linux/scatterlist.h>
27
28#include "sas_internal.h"
29
30#include <scsi/scsi_transport.h>
31#include <scsi/scsi_transport_sas.h>
32#include "../scsi_sas_internal.h"
33
34static int sas_discover_expander(struct domain_device *dev);
35static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr);
36static int sas_configure_phy(struct domain_device *dev, int phy_id,
37 u8 *sas_addr, int include);
38static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr);
39
40#if 0
41/* FIXME: smp needs to migrate into the sas class */
42static ssize_t smp_portal_read(struct kobject *, char *, loff_t, size_t);
43static ssize_t smp_portal_write(struct kobject *, char *, loff_t, size_t);
44#endif
45
46/* ---------- SMP task management ---------- */
47
48static void smp_task_timedout(unsigned long _task)
49{
50 struct sas_task *task = (void *) _task;
51 unsigned long flags;
52
53 spin_lock_irqsave(&task->task_state_lock, flags);
54 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
55 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
56 spin_unlock_irqrestore(&task->task_state_lock, flags);
57
58 complete(&task->completion);
59}
60
61static void smp_task_done(struct sas_task *task)
62{
63 if (!del_timer(&task->timer))
64 return;
65 complete(&task->completion);
66}
67
68/* Give it some long enough timeout. In seconds. */
69#define SMP_TIMEOUT 10
70
71static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
72 void *resp, int resp_size)
73{
74 int res;
75 struct sas_task *task = sas_alloc_task(GFP_KERNEL);
76 struct sas_internal *i =
77 to_sas_internal(dev->port->ha->core.shost->transportt);
78
79 if (!task)
80 return -ENOMEM;
81
82 task->dev = dev;
83 task->task_proto = dev->tproto;
84 sg_init_one(&task->smp_task.smp_req, req, req_size);
85 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
86
87 task->task_done = smp_task_done;
88
89 task->timer.data = (unsigned long) task;
90 task->timer.function = smp_task_timedout;
91 task->timer.expires = jiffies + SMP_TIMEOUT*HZ;
92 add_timer(&task->timer);
93
94 res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
95
96 if (res) {
97 del_timer(&task->timer);
98 SAS_DPRINTK("executing SMP task failed:%d\n", res);
99 goto ex_err;
100 }
101
102 wait_for_completion(&task->completion);
103 res = -ETASK;
104 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
105 SAS_DPRINTK("smp task timed out or aborted\n");
106 i->dft->lldd_abort_task(task);
107 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
108 SAS_DPRINTK("SMP task aborted and not done\n");
109 goto ex_err;
110 }
111 }
112 if (task->task_status.resp == SAS_TASK_COMPLETE &&
113 task->task_status.stat == SAM_GOOD)
114 res = 0;
115 else
116 SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
117 "status 0x%x\n", __FUNCTION__,
118 SAS_ADDR(dev->sas_addr),
119 task->task_status.resp,
120 task->task_status.stat);
121ex_err:
122 sas_free_task(task);
123 return res;
124}
125
126/* ---------- Allocations ---------- */
127
128static inline void *alloc_smp_req(int size)
129{
130 u8 *p = kzalloc(size, GFP_KERNEL);
131 if (p)
132 p[0] = SMP_REQUEST;
133 return p;
134}
135
136static inline void *alloc_smp_resp(int size)
137{
138 return kzalloc(size, GFP_KERNEL);
139}
140
141/* ---------- Expander configuration ---------- */
142
143static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
144 void *disc_resp)
145{
146 struct expander_device *ex = &dev->ex_dev;
147 struct ex_phy *phy = &ex->ex_phy[phy_id];
148 struct smp_resp *resp = disc_resp;
149 struct discover_resp *dr = &resp->disc;
150 struct sas_rphy *rphy = dev->rphy;
151 int rediscover = (phy->phy != NULL);
152
153 if (!rediscover) {
154 phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
155
156 /* FIXME: error_handling */
157 BUG_ON(!phy->phy);
158 }
159
160 switch (resp->result) {
161 case SMP_RESP_PHY_VACANT:
162 phy->phy_state = PHY_VACANT;
163 return;
164 default:
165 phy->phy_state = PHY_NOT_PRESENT;
166 return;
167 case SMP_RESP_FUNC_ACC:
168 phy->phy_state = PHY_EMPTY; /* do not know yet */
169 break;
170 }
171
172 phy->phy_id = phy_id;
173 phy->attached_dev_type = dr->attached_dev_type;
174 phy->linkrate = dr->linkrate;
175 phy->attached_sata_host = dr->attached_sata_host;
176 phy->attached_sata_dev = dr->attached_sata_dev;
177 phy->attached_sata_ps = dr->attached_sata_ps;
178 phy->attached_iproto = dr->iproto << 1;
179 phy->attached_tproto = dr->tproto << 1;
180 memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
181 phy->attached_phy_id = dr->attached_phy_id;
182 phy->phy_change_count = dr->change_count;
183 phy->routing_attr = dr->routing_attr;
184 phy->virtual = dr->virtual;
185 phy->last_da_index = -1;
186
187 phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
188 phy->phy->identify.target_port_protocols = phy->attached_tproto;
189 phy->phy->identify.phy_identifier = phy_id;
190 phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
191 phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
192 phy->phy->minimum_linkrate = dr->pmin_linkrate;
193 phy->phy->maximum_linkrate = dr->pmax_linkrate;
194 phy->phy->negotiated_linkrate = phy->linkrate;
195
196 if (!rediscover)
197 sas_phy_add(phy->phy);
198
199 SAS_DPRINTK("ex %016llx phy%02d:%c attached: %016llx\n",
200 SAS_ADDR(dev->sas_addr), phy->phy_id,
201 phy->routing_attr == TABLE_ROUTING ? 'T' :
202 phy->routing_attr == DIRECT_ROUTING ? 'D' :
203 phy->routing_attr == SUBTRACTIVE_ROUTING ? 'S' : '?',
204 SAS_ADDR(phy->attached_sas_addr));
205
206 return;
207}
208
209#define DISCOVER_REQ_SIZE 16
210#define DISCOVER_RESP_SIZE 56
211
212static int sas_ex_phy_discover(struct domain_device *dev, int single)
213{
214 struct expander_device *ex = &dev->ex_dev;
215 int res = 0;
216 u8 *disc_req;
217 u8 *disc_resp;
218
219 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
220 if (!disc_req)
221 return -ENOMEM;
222
223 disc_resp = alloc_smp_req(DISCOVER_RESP_SIZE);
224 if (!disc_resp) {
225 kfree(disc_req);
226 return -ENOMEM;
227 }
228
229 disc_req[1] = SMP_DISCOVER;
230
231 if (0 <= single && single < ex->num_phys) {
232 disc_req[9] = single;
233 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
234 disc_resp, DISCOVER_RESP_SIZE);
235 if (res)
236 goto out_err;
237 sas_set_ex_phy(dev, single, disc_resp);
238 } else {
239 int i;
240
241 for (i = 0; i < ex->num_phys; i++) {
242 disc_req[9] = i;
243 res = smp_execute_task(dev, disc_req,
244 DISCOVER_REQ_SIZE, disc_resp,
245 DISCOVER_RESP_SIZE);
246 if (res)
247 goto out_err;
248 sas_set_ex_phy(dev, i, disc_resp);
249 }
250 }
251out_err:
252 kfree(disc_resp);
253 kfree(disc_req);
254 return res;
255}
256
257static int sas_expander_discover(struct domain_device *dev)
258{
259 struct expander_device *ex = &dev->ex_dev;
260 int res = -ENOMEM;
261
262 ex->ex_phy = kzalloc(sizeof(*ex->ex_phy)*ex->num_phys, GFP_KERNEL);
263 if (!ex->ex_phy)
264 return -ENOMEM;
265
266 res = sas_ex_phy_discover(dev, -1);
267 if (res)
268 goto out_err;
269
270 return 0;
271 out_err:
272 kfree(ex->ex_phy);
273 ex->ex_phy = NULL;
274 return res;
275}
276
277#define MAX_EXPANDER_PHYS 128
278
279static void ex_assign_report_general(struct domain_device *dev,
280 struct smp_resp *resp)
281{
282 struct report_general_resp *rg = &resp->rg;
283
284 dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
285 dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
286 dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
287 dev->ex_dev.conf_route_table = rg->conf_route_table;
288 dev->ex_dev.configuring = rg->configuring;
289 memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
290}
291
292#define RG_REQ_SIZE 8
293#define RG_RESP_SIZE 32
294
295static int sas_ex_general(struct domain_device *dev)
296{
297 u8 *rg_req;
298 struct smp_resp *rg_resp;
299 int res;
300 int i;
301
302 rg_req = alloc_smp_req(RG_REQ_SIZE);
303 if (!rg_req)
304 return -ENOMEM;
305
306 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
307 if (!rg_resp) {
308 kfree(rg_req);
309 return -ENOMEM;
310 }
311
312 rg_req[1] = SMP_REPORT_GENERAL;
313
314 for (i = 0; i < 5; i++) {
315 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
316 RG_RESP_SIZE);
317
318 if (res) {
319 SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
320 SAS_ADDR(dev->sas_addr), res);
321 goto out;
322 } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
323 SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
324 SAS_ADDR(dev->sas_addr), rg_resp->result);
325 res = rg_resp->result;
326 goto out;
327 }
328
329 ex_assign_report_general(dev, rg_resp);
330
331 if (dev->ex_dev.configuring) {
332 SAS_DPRINTK("RG: ex %llx self-configuring...\n",
333 SAS_ADDR(dev->sas_addr));
334 schedule_timeout_interruptible(5*HZ);
335 } else
336 break;
337 }
338out:
339 kfree(rg_req);
340 kfree(rg_resp);
341 return res;
342}
343
344static void ex_assign_manuf_info(struct domain_device *dev, void
345 *_mi_resp)
346{
347 u8 *mi_resp = _mi_resp;
348 struct sas_rphy *rphy = dev->rphy;
349 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
350
351 memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
352 memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
353 memcpy(edev->product_rev, mi_resp + 36,
354 SAS_EXPANDER_PRODUCT_REV_LEN);
355
356 if (mi_resp[8] & 1) {
357 memcpy(edev->component_vendor_id, mi_resp + 40,
358 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
359 edev->component_id = mi_resp[48] << 8 | mi_resp[49];
360 edev->component_revision_id = mi_resp[50];
361 }
362}
363
364#define MI_REQ_SIZE 8
365#define MI_RESP_SIZE 64
366
367static int sas_ex_manuf_info(struct domain_device *dev)
368{
369 u8 *mi_req;
370 u8 *mi_resp;
371 int res;
372
373 mi_req = alloc_smp_req(MI_REQ_SIZE);
374 if (!mi_req)
375 return -ENOMEM;
376
377 mi_resp = alloc_smp_resp(MI_RESP_SIZE);
378 if (!mi_resp) {
379 kfree(mi_req);
380 return -ENOMEM;
381 }
382
383 mi_req[1] = SMP_REPORT_MANUF_INFO;
384
385 res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
386 if (res) {
387 SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
388 SAS_ADDR(dev->sas_addr), res);
389 goto out;
390 } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
391 SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
392 SAS_ADDR(dev->sas_addr), mi_resp[2]);
393 goto out;
394 }
395
396 ex_assign_manuf_info(dev, mi_resp);
397out:
398 kfree(mi_req);
399 kfree(mi_resp);
400 return res;
401}
402
403#define PC_REQ_SIZE 44
404#define PC_RESP_SIZE 8
405
406int sas_smp_phy_control(struct domain_device *dev, int phy_id,
407 enum phy_func phy_func,
408 struct sas_phy_linkrates *rates)
409{
410 u8 *pc_req;
411 u8 *pc_resp;
412 int res;
413
414 pc_req = alloc_smp_req(PC_REQ_SIZE);
415 if (!pc_req)
416 return -ENOMEM;
417
418 pc_resp = alloc_smp_resp(PC_RESP_SIZE);
419 if (!pc_resp) {
420 kfree(pc_req);
421 return -ENOMEM;
422 }
423
424 pc_req[1] = SMP_PHY_CONTROL;
425 pc_req[9] = phy_id;
426 pc_req[10]= phy_func;
427 if (rates) {
428 pc_req[32] = rates->minimum_linkrate << 4;
429 pc_req[33] = rates->maximum_linkrate << 4;
430 }
431
432 res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
433
434 kfree(pc_resp);
435 kfree(pc_req);
436 return res;
437}
438
439static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
440{
441 struct expander_device *ex = &dev->ex_dev;
442 struct ex_phy *phy = &ex->ex_phy[phy_id];
443
444 sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
445 phy->linkrate = SAS_PHY_DISABLED;
446}
447
448static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
449{
450 struct expander_device *ex = &dev->ex_dev;
451 int i;
452
453 for (i = 0; i < ex->num_phys; i++) {
454 struct ex_phy *phy = &ex->ex_phy[i];
455
456 if (phy->phy_state == PHY_VACANT ||
457 phy->phy_state == PHY_NOT_PRESENT)
458 continue;
459
460 if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
461 sas_ex_disable_phy(dev, i);
462 }
463}
464
465static int sas_dev_present_in_domain(struct asd_sas_port *port,
466 u8 *sas_addr)
467{
468 struct domain_device *dev;
469
470 if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
471 return 1;
472 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
473 if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
474 return 1;
475 }
476 return 0;
477}
478
479#define RPEL_REQ_SIZE 16
480#define RPEL_RESP_SIZE 32
481int sas_smp_get_phy_events(struct sas_phy *phy)
482{
483 int res;
484 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
485 struct domain_device *dev = sas_find_dev_by_rphy(rphy);
486 u8 *req = alloc_smp_req(RPEL_REQ_SIZE);
487 u8 *resp = kzalloc(RPEL_RESP_SIZE, GFP_KERNEL);
488
489 if (!resp)
490 return -ENOMEM;
491
492 req[1] = SMP_REPORT_PHY_ERR_LOG;
493 req[9] = phy->number;
494
495 res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
496 resp, RPEL_RESP_SIZE);
497
498 if (!res)
499 goto out;
500
501 phy->invalid_dword_count = scsi_to_u32(&resp[12]);
502 phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
503 phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
504 phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
505
506 out:
507 kfree(resp);
508 return res;
509
510}
511
512#define RPS_REQ_SIZE 16
513#define RPS_RESP_SIZE 60
514
515static int sas_get_report_phy_sata(struct domain_device *dev,
516 int phy_id,
517 struct smp_resp *rps_resp)
518{
519 int res;
520 u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
521
522 if (!rps_req)
523 return -ENOMEM;
524
525 rps_req[1] = SMP_REPORT_PHY_SATA;
526 rps_req[9] = phy_id;
527
528 res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
529 rps_resp, RPS_RESP_SIZE);
530
531 kfree(rps_req);
532 return 0;
533}
534
535static void sas_ex_get_linkrate(struct domain_device *parent,
536 struct domain_device *child,
537 struct ex_phy *parent_phy)
538{
539 struct expander_device *parent_ex = &parent->ex_dev;
540 struct sas_port *port;
541 int i;
542
543 child->pathways = 0;
544
545 port = parent_phy->port;
546
547 for (i = 0; i < parent_ex->num_phys; i++) {
548 struct ex_phy *phy = &parent_ex->ex_phy[i];
549
550 if (phy->phy_state == PHY_VACANT ||
551 phy->phy_state == PHY_NOT_PRESENT)
552 continue;
553
554 if (SAS_ADDR(phy->attached_sas_addr) ==
555 SAS_ADDR(child->sas_addr)) {
556
557 child->min_linkrate = min(parent->min_linkrate,
558 phy->linkrate);
559 child->max_linkrate = max(parent->max_linkrate,
560 phy->linkrate);
561 child->pathways++;
562 sas_port_add_phy(port, phy->phy);
563 }
564 }
565 child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
566 child->pathways = min(child->pathways, parent->pathways);
567}
568
569static struct domain_device *sas_ex_discover_end_dev(
570 struct domain_device *parent, int phy_id)
571{
572 struct expander_device *parent_ex = &parent->ex_dev;
573 struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
574 struct domain_device *child = NULL;
575 struct sas_rphy *rphy;
576 int res;
577
578 if (phy->attached_sata_host || phy->attached_sata_ps)
579 return NULL;
580
581 child = kzalloc(sizeof(*child), GFP_KERNEL);
582 if (!child)
583 return NULL;
584
585 child->parent = parent;
586 child->port = parent->port;
587 child->iproto = phy->attached_iproto;
588 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
589 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
590 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
591 BUG_ON(!phy->port);
592 /* FIXME: better error handling*/
593 BUG_ON(sas_port_add(phy->port) != 0);
594 sas_ex_get_linkrate(parent, child, phy);
595
596 if ((phy->attached_tproto & SAS_PROTO_STP) || phy->attached_sata_dev) {
597 child->dev_type = SATA_DEV;
598 if (phy->attached_tproto & SAS_PROTO_STP)
599 child->tproto = phy->attached_tproto;
600 if (phy->attached_sata_dev)
601 child->tproto |= SATA_DEV;
602 res = sas_get_report_phy_sata(parent, phy_id,
603 &child->sata_dev.rps_resp);
604 if (res) {
605 SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
606 "0x%x\n", SAS_ADDR(parent->sas_addr),
607 phy_id, res);
608 kfree(child);
609 return NULL;
610 }
611 memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
612 sizeof(struct dev_to_host_fis));
613 sas_init_dev(child);
614 res = sas_discover_sata(child);
615 if (res) {
616 SAS_DPRINTK("sas_discover_sata() for device %16llx at "
617 "%016llx:0x%x returned 0x%x\n",
618 SAS_ADDR(child->sas_addr),
619 SAS_ADDR(parent->sas_addr), phy_id, res);
620 kfree(child);
621 return NULL;
622 }
623 } else if (phy->attached_tproto & SAS_PROTO_SSP) {
624 child->dev_type = SAS_END_DEV;
625 rphy = sas_end_device_alloc(phy->port);
626 /* FIXME: error handling */
627 BUG_ON(!rphy);
628 child->tproto = phy->attached_tproto;
629 sas_init_dev(child);
630
631 child->rphy = rphy;
632 sas_fill_in_rphy(child, rphy);
633
634 spin_lock(&parent->port->dev_list_lock);
635 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
636 spin_unlock(&parent->port->dev_list_lock);
637
638 res = sas_discover_end_dev(child);
639 if (res) {
640 SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
641 "at %016llx:0x%x returned 0x%x\n",
642 SAS_ADDR(child->sas_addr),
643 SAS_ADDR(parent->sas_addr), phy_id, res);
644 /* FIXME: this kfrees list elements without removing them */
645 //kfree(child);
646 return NULL;
647 }
648 } else {
649 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
650 phy->attached_tproto, SAS_ADDR(parent->sas_addr),
651 phy_id);
652 }
653
654 list_add_tail(&child->siblings, &parent_ex->children);
655 return child;
656}
657
658static struct domain_device *sas_ex_discover_expander(
659 struct domain_device *parent, int phy_id)
660{
661 struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
662 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
663 struct domain_device *child = NULL;
664 struct sas_rphy *rphy;
665 struct sas_expander_device *edev;
666 struct asd_sas_port *port;
667 int res;
668
669 if (phy->routing_attr == DIRECT_ROUTING) {
670 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
671 "allowed\n",
672 SAS_ADDR(parent->sas_addr), phy_id,
673 SAS_ADDR(phy->attached_sas_addr),
674 phy->attached_phy_id);
675 return NULL;
676 }
677 child = kzalloc(sizeof(*child), GFP_KERNEL);
678 if (!child)
679 return NULL;
680
681 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
682 /* FIXME: better error handling */
683 BUG_ON(sas_port_add(phy->port) != 0);
684
685
686 switch (phy->attached_dev_type) {
687 case EDGE_DEV:
688 rphy = sas_expander_alloc(phy->port,
689 SAS_EDGE_EXPANDER_DEVICE);
690 break;
691 case FANOUT_DEV:
692 rphy = sas_expander_alloc(phy->port,
693 SAS_FANOUT_EXPANDER_DEVICE);
694 break;
695 default:
696 rphy = NULL; /* shut gcc up */
697 BUG();
698 }
699 port = parent->port;
700 child->rphy = rphy;
701 edev = rphy_to_expander_device(rphy);
702 child->dev_type = phy->attached_dev_type;
703 child->parent = parent;
704 child->port = port;
705 child->iproto = phy->attached_iproto;
706 child->tproto = phy->attached_tproto;
707 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
708 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
709 sas_ex_get_linkrate(parent, child, phy);
710 edev->level = parent_ex->level + 1;
711 parent->port->disc.max_level = max(parent->port->disc.max_level,
712 edev->level);
713 sas_init_dev(child);
714 sas_fill_in_rphy(child, rphy);
715 sas_rphy_add(rphy);
716
717 spin_lock(&parent->port->dev_list_lock);
718 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
719 spin_unlock(&parent->port->dev_list_lock);
720
721 res = sas_discover_expander(child);
722 if (res) {
723 kfree(child);
724 return NULL;
725 }
726 list_add_tail(&child->siblings, &parent->ex_dev.children);
727 return child;
728}
729
730static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
731{
732 struct expander_device *ex = &dev->ex_dev;
733 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
734 struct domain_device *child = NULL;
735 int res = 0;
736
737 /* Phy state */
738 if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
739 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
740 res = sas_ex_phy_discover(dev, phy_id);
741 if (res)
742 return res;
743 }
744
745 /* Parent and domain coherency */
746 if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
747 SAS_ADDR(dev->port->sas_addr))) {
748 sas_add_parent_port(dev, phy_id);
749 return 0;
750 }
751 if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
752 SAS_ADDR(dev->parent->sas_addr))) {
753 sas_add_parent_port(dev, phy_id);
754 if (ex_phy->routing_attr == TABLE_ROUTING)
755 sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
756 return 0;
757 }
758
759 if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
760 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
761
762 if (ex_phy->attached_dev_type == NO_DEVICE) {
763 if (ex_phy->routing_attr == DIRECT_ROUTING) {
764 memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
765 sas_configure_routing(dev, ex_phy->attached_sas_addr);
766 }
767 return 0;
768 } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
769 return 0;
770
771 if (ex_phy->attached_dev_type != SAS_END_DEV &&
772 ex_phy->attached_dev_type != FANOUT_DEV &&
773 ex_phy->attached_dev_type != EDGE_DEV) {
774 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
775 "phy 0x%x\n", ex_phy->attached_dev_type,
776 SAS_ADDR(dev->sas_addr),
777 phy_id);
778 return 0;
779 }
780
781 res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
782 if (res) {
783 SAS_DPRINTK("configure routing for dev %016llx "
784 "reported 0x%x. Forgotten\n",
785 SAS_ADDR(ex_phy->attached_sas_addr), res);
786 sas_disable_routing(dev, ex_phy->attached_sas_addr);
787 return res;
788 }
789
790 switch (ex_phy->attached_dev_type) {
791 case SAS_END_DEV:
792 child = sas_ex_discover_end_dev(dev, phy_id);
793 break;
794 case FANOUT_DEV:
795 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
796 SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
797 "attached to ex %016llx phy 0x%x\n",
798 SAS_ADDR(ex_phy->attached_sas_addr),
799 ex_phy->attached_phy_id,
800 SAS_ADDR(dev->sas_addr),
801 phy_id);
802 sas_ex_disable_phy(dev, phy_id);
803 break;
804 } else
805 memcpy(dev->port->disc.fanout_sas_addr,
806 ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
807 /* fallthrough */
808 case EDGE_DEV:
809 child = sas_ex_discover_expander(dev, phy_id);
810 break;
811 default:
812 break;
813 }
814
815 if (child) {
816 int i;
817
818 for (i = 0; i < ex->num_phys; i++) {
819 if (ex->ex_phy[i].phy_state == PHY_VACANT ||
820 ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
821 continue;
822
823 if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
824 SAS_ADDR(child->sas_addr))
825 ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
826 }
827 }
828
829 return res;
830}
831
832static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
833{
834 struct expander_device *ex = &dev->ex_dev;
835 int i;
836
837 for (i = 0; i < ex->num_phys; i++) {
838 struct ex_phy *phy = &ex->ex_phy[i];
839
840 if (phy->phy_state == PHY_VACANT ||
841 phy->phy_state == PHY_NOT_PRESENT)
842 continue;
843
844 if ((phy->attached_dev_type == EDGE_DEV ||
845 phy->attached_dev_type == FANOUT_DEV) &&
846 phy->routing_attr == SUBTRACTIVE_ROUTING) {
847
848 memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
849
850 return 1;
851 }
852 }
853 return 0;
854}
855
856static int sas_check_level_subtractive_boundary(struct domain_device *dev)
857{
858 struct expander_device *ex = &dev->ex_dev;
859 struct domain_device *child;
860 u8 sub_addr[8] = {0, };
861
862 list_for_each_entry(child, &ex->children, siblings) {
863 if (child->dev_type != EDGE_DEV &&
864 child->dev_type != FANOUT_DEV)
865 continue;
866 if (sub_addr[0] == 0) {
867 sas_find_sub_addr(child, sub_addr);
868 continue;
869 } else {
870 u8 s2[8];
871
872 if (sas_find_sub_addr(child, s2) &&
873 (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
874
875 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
876 "diverges from subtractive "
877 "boundary %016llx\n",
878 SAS_ADDR(dev->sas_addr),
879 SAS_ADDR(child->sas_addr),
880 SAS_ADDR(s2),
881 SAS_ADDR(sub_addr));
882
883 sas_ex_disable_port(child, s2);
884 }
885 }
886 }
887 return 0;
888}
889/**
890 * sas_ex_discover_devices -- discover devices attached to this expander
891 * dev: pointer to the expander domain device
892 * single: if you want to do a single phy, else set to -1;
893 *
894 * Configure this expander for use with its devices and register the
895 * devices of this expander.
896 */
897static int sas_ex_discover_devices(struct domain_device *dev, int single)
898{
899 struct expander_device *ex = &dev->ex_dev;
900 int i = 0, end = ex->num_phys;
901 int res = 0;
902
903 if (0 <= single && single < end) {
904 i = single;
905 end = i+1;
906 }
907
908 for ( ; i < end; i++) {
909 struct ex_phy *ex_phy = &ex->ex_phy[i];
910
911 if (ex_phy->phy_state == PHY_VACANT ||
912 ex_phy->phy_state == PHY_NOT_PRESENT ||
913 ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
914 continue;
915
916 switch (ex_phy->linkrate) {
917 case SAS_PHY_DISABLED:
918 case SAS_PHY_RESET_PROBLEM:
919 case SAS_SATA_PORT_SELECTOR:
920 continue;
921 default:
922 res = sas_ex_discover_dev(dev, i);
923 if (res)
924 break;
925 continue;
926 }
927 }
928
929 if (!res)
930 sas_check_level_subtractive_boundary(dev);
931
932 return res;
933}
934
935static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
936{
937 struct expander_device *ex = &dev->ex_dev;
938 int i;
939 u8 *sub_sas_addr = NULL;
940
941 if (dev->dev_type != EDGE_DEV)
942 return 0;
943
944 for (i = 0; i < ex->num_phys; i++) {
945 struct ex_phy *phy = &ex->ex_phy[i];
946
947 if (phy->phy_state == PHY_VACANT ||
948 phy->phy_state == PHY_NOT_PRESENT)
949 continue;
950
951 if ((phy->attached_dev_type == FANOUT_DEV ||
952 phy->attached_dev_type == EDGE_DEV) &&
953 phy->routing_attr == SUBTRACTIVE_ROUTING) {
954
955 if (!sub_sas_addr)
956 sub_sas_addr = &phy->attached_sas_addr[0];
957 else if (SAS_ADDR(sub_sas_addr) !=
958 SAS_ADDR(phy->attached_sas_addr)) {
959
960 SAS_DPRINTK("ex %016llx phy 0x%x "
961 "diverges(%016llx) on subtractive "
962 "boundary(%016llx). Disabled\n",
963 SAS_ADDR(dev->sas_addr), i,
964 SAS_ADDR(phy->attached_sas_addr),
965 SAS_ADDR(sub_sas_addr));
966 sas_ex_disable_phy(dev, i);
967 }
968 }
969 }
970 return 0;
971}
972
973static void sas_print_parent_topology_bug(struct domain_device *child,
974 struct ex_phy *parent_phy,
975 struct ex_phy *child_phy)
976{
977 static const char ra_char[] = {
978 [DIRECT_ROUTING] = 'D',
979 [SUBTRACTIVE_ROUTING] = 'S',
980 [TABLE_ROUTING] = 'T',
981 };
982 static const char *ex_type[] = {
983 [EDGE_DEV] = "edge",
984 [FANOUT_DEV] = "fanout",
985 };
986 struct domain_device *parent = child->parent;
987
988 sas_printk("%s ex %016llx phy 0x%x <--> %s ex %016llx phy 0x%x "
989 "has %c:%c routing link!\n",
990
991 ex_type[parent->dev_type],
992 SAS_ADDR(parent->sas_addr),
993 parent_phy->phy_id,
994
995 ex_type[child->dev_type],
996 SAS_ADDR(child->sas_addr),
997 child_phy->phy_id,
998
999 ra_char[parent_phy->routing_attr],
1000 ra_char[child_phy->routing_attr]);
1001}
1002
1003static int sas_check_eeds(struct domain_device *child,
1004 struct ex_phy *parent_phy,
1005 struct ex_phy *child_phy)
1006{
1007 int res = 0;
1008 struct domain_device *parent = child->parent;
1009
1010 if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1011 res = -ENODEV;
1012 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1013 "phy S:0x%x, while there is a fanout ex %016llx\n",
1014 SAS_ADDR(parent->sas_addr),
1015 parent_phy->phy_id,
1016 SAS_ADDR(child->sas_addr),
1017 child_phy->phy_id,
1018 SAS_ADDR(parent->port->disc.fanout_sas_addr));
1019 } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1020 memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1021 SAS_ADDR_SIZE);
1022 memcpy(parent->port->disc.eeds_b, child->sas_addr,
1023 SAS_ADDR_SIZE);
1024 } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1025 SAS_ADDR(parent->sas_addr)) ||
1026 (SAS_ADDR(parent->port->disc.eeds_a) ==
1027 SAS_ADDR(child->sas_addr)))
1028 &&
1029 ((SAS_ADDR(parent->port->disc.eeds_b) ==
1030 SAS_ADDR(parent->sas_addr)) ||
1031 (SAS_ADDR(parent->port->disc.eeds_b) ==
1032 SAS_ADDR(child->sas_addr))))
1033 ;
1034 else {
1035 res = -ENODEV;
1036 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1037 "phy 0x%x link forms a third EEDS!\n",
1038 SAS_ADDR(parent->sas_addr),
1039 parent_phy->phy_id,
1040 SAS_ADDR(child->sas_addr),
1041 child_phy->phy_id);
1042 }
1043
1044 return res;
1045}
1046
1047/* Here we spill over 80 columns. It is intentional.
1048 */
1049static int sas_check_parent_topology(struct domain_device *child)
1050{
1051 struct expander_device *child_ex = &child->ex_dev;
1052 struct expander_device *parent_ex;
1053 int i;
1054 int res = 0;
1055
1056 if (!child->parent)
1057 return 0;
1058
1059 if (child->parent->dev_type != EDGE_DEV &&
1060 child->parent->dev_type != FANOUT_DEV)
1061 return 0;
1062
1063 parent_ex = &child->parent->ex_dev;
1064
1065 for (i = 0; i < parent_ex->num_phys; i++) {
1066 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1067 struct ex_phy *child_phy;
1068
1069 if (parent_phy->phy_state == PHY_VACANT ||
1070 parent_phy->phy_state == PHY_NOT_PRESENT)
1071 continue;
1072
1073 if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1074 continue;
1075
1076 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1077
1078 switch (child->parent->dev_type) {
1079 case EDGE_DEV:
1080 if (child->dev_type == FANOUT_DEV) {
1081 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1082 child_phy->routing_attr != TABLE_ROUTING) {
1083 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1084 res = -ENODEV;
1085 }
1086 } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1087 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1088 res = sas_check_eeds(child, parent_phy, child_phy);
1089 } else if (child_phy->routing_attr != TABLE_ROUTING) {
1090 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1091 res = -ENODEV;
1092 }
1093 } else if (parent_phy->routing_attr == TABLE_ROUTING &&
1094 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1095 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1096 res = -ENODEV;
1097 }
1098 break;
1099 case FANOUT_DEV:
1100 if (parent_phy->routing_attr != TABLE_ROUTING ||
1101 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1102 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1103 res = -ENODEV;
1104 }
1105 break;
1106 default:
1107 break;
1108 }
1109 }
1110
1111 return res;
1112}
1113
1114#define RRI_REQ_SIZE 16
1115#define RRI_RESP_SIZE 44
1116
1117static int sas_configure_present(struct domain_device *dev, int phy_id,
1118 u8 *sas_addr, int *index, int *present)
1119{
1120 int i, res = 0;
1121 struct expander_device *ex = &dev->ex_dev;
1122 struct ex_phy *phy = &ex->ex_phy[phy_id];
1123 u8 *rri_req;
1124 u8 *rri_resp;
1125
1126 *present = 0;
1127 *index = 0;
1128
1129 rri_req = alloc_smp_req(RRI_REQ_SIZE);
1130 if (!rri_req)
1131 return -ENOMEM;
1132
1133 rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1134 if (!rri_resp) {
1135 kfree(rri_req);
1136 return -ENOMEM;
1137 }
1138
1139 rri_req[1] = SMP_REPORT_ROUTE_INFO;
1140 rri_req[9] = phy_id;
1141
1142 for (i = 0; i < ex->max_route_indexes ; i++) {
1143 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1144 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1145 RRI_RESP_SIZE);
1146 if (res)
1147 goto out;
1148 res = rri_resp[2];
1149 if (res == SMP_RESP_NO_INDEX) {
1150 SAS_DPRINTK("overflow of indexes: dev %016llx "
1151 "phy 0x%x index 0x%x\n",
1152 SAS_ADDR(dev->sas_addr), phy_id, i);
1153 goto out;
1154 } else if (res != SMP_RESP_FUNC_ACC) {
1155 SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
1156 "result 0x%x\n", __FUNCTION__,
1157 SAS_ADDR(dev->sas_addr), phy_id, i, res);
1158 goto out;
1159 }
1160 if (SAS_ADDR(sas_addr) != 0) {
1161 if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1162 *index = i;
1163 if ((rri_resp[12] & 0x80) == 0x80)
1164 *present = 0;
1165 else
1166 *present = 1;
1167 goto out;
1168 } else if (SAS_ADDR(rri_resp+16) == 0) {
1169 *index = i;
1170 *present = 0;
1171 goto out;
1172 }
1173 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1174 phy->last_da_index < i) {
1175 phy->last_da_index = i;
1176 *index = i;
1177 *present = 0;
1178 goto out;
1179 }
1180 }
1181 res = -1;
1182out:
1183 kfree(rri_req);
1184 kfree(rri_resp);
1185 return res;
1186}
1187
1188#define CRI_REQ_SIZE 44
1189#define CRI_RESP_SIZE 8
1190
1191static int sas_configure_set(struct domain_device *dev, int phy_id,
1192 u8 *sas_addr, int index, int include)
1193{
1194 int res;
1195 u8 *cri_req;
1196 u8 *cri_resp;
1197
1198 cri_req = alloc_smp_req(CRI_REQ_SIZE);
1199 if (!cri_req)
1200 return -ENOMEM;
1201
1202 cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1203 if (!cri_resp) {
1204 kfree(cri_req);
1205 return -ENOMEM;
1206 }
1207
1208 cri_req[1] = SMP_CONF_ROUTE_INFO;
1209 *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1210 cri_req[9] = phy_id;
1211 if (SAS_ADDR(sas_addr) == 0 || !include)
1212 cri_req[12] |= 0x80;
1213 memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1214
1215 res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1216 CRI_RESP_SIZE);
1217 if (res)
1218 goto out;
1219 res = cri_resp[2];
1220 if (res == SMP_RESP_NO_INDEX) {
1221 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1222 "index 0x%x\n",
1223 SAS_ADDR(dev->sas_addr), phy_id, index);
1224 }
1225out:
1226 kfree(cri_req);
1227 kfree(cri_resp);
1228 return res;
1229}
1230
1231static int sas_configure_phy(struct domain_device *dev, int phy_id,
1232 u8 *sas_addr, int include)
1233{
1234 int index;
1235 int present;
1236 int res;
1237
1238 res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1239 if (res)
1240 return res;
1241 if (include ^ present)
1242 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1243
1244 return res;
1245}
1246
1247/**
1248 * sas_configure_parent -- configure routing table of parent
1249 * parent: parent expander
1250 * child: child expander
1251 * sas_addr: SAS port identifier of device directly attached to child
1252 */
1253static int sas_configure_parent(struct domain_device *parent,
1254 struct domain_device *child,
1255 u8 *sas_addr, int include)
1256{
1257 struct expander_device *ex_parent = &parent->ex_dev;
1258 int res = 0;
1259 int i;
1260
1261 if (parent->parent) {
1262 res = sas_configure_parent(parent->parent, parent, sas_addr,
1263 include);
1264 if (res)
1265 return res;
1266 }
1267
1268 if (ex_parent->conf_route_table == 0) {
1269 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1270 SAS_ADDR(parent->sas_addr));
1271 return 0;
1272 }
1273
1274 for (i = 0; i < ex_parent->num_phys; i++) {
1275 struct ex_phy *phy = &ex_parent->ex_phy[i];
1276
1277 if ((phy->routing_attr == TABLE_ROUTING) &&
1278 (SAS_ADDR(phy->attached_sas_addr) ==
1279 SAS_ADDR(child->sas_addr))) {
1280 res = sas_configure_phy(parent, i, sas_addr, include);
1281 if (res)
1282 return res;
1283 }
1284 }
1285
1286 return res;
1287}
1288
1289/**
1290 * sas_configure_routing -- configure routing
1291 * dev: expander device
1292 * sas_addr: port identifier of device directly attached to the expander device
1293 */
1294static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1295{
1296 if (dev->parent)
1297 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1298 return 0;
1299}
1300
1301static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
1302{
1303 if (dev->parent)
1304 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1305 return 0;
1306}
1307
1308#if 0
1309#define SMP_BIN_ATTR_NAME "smp_portal"
1310
1311static void sas_ex_smp_hook(struct domain_device *dev)
1312{
1313 struct expander_device *ex_dev = &dev->ex_dev;
1314 struct bin_attribute *bin_attr = &ex_dev->smp_bin_attr;
1315
1316 memset(bin_attr, 0, sizeof(*bin_attr));
1317
1318 bin_attr->attr.name = SMP_BIN_ATTR_NAME;
1319 bin_attr->attr.owner = THIS_MODULE;
1320 bin_attr->attr.mode = 0600;
1321
1322 bin_attr->size = 0;
1323 bin_attr->private = NULL;
1324 bin_attr->read = smp_portal_read;
1325 bin_attr->write= smp_portal_write;
1326 bin_attr->mmap = NULL;
1327
1328 ex_dev->smp_portal_pid = -1;
1329 init_MUTEX(&ex_dev->smp_sema);
1330}
1331#endif
1332
1333/**
1334 * sas_discover_expander -- expander discovery
1335 * @ex: pointer to expander domain device
1336 *
1337 * See comment in sas_discover_sata().
1338 */
1339static int sas_discover_expander(struct domain_device *dev)
1340{
1341 int res;
1342
1343 res = sas_notify_lldd_dev_found(dev);
1344 if (res)
1345 return res;
1346
1347 res = sas_ex_general(dev);
1348 if (res)
1349 goto out_err;
1350 res = sas_ex_manuf_info(dev);
1351 if (res)
1352 goto out_err;
1353
1354 res = sas_expander_discover(dev);
1355 if (res) {
1356 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1357 SAS_ADDR(dev->sas_addr), res);
1358 goto out_err;
1359 }
1360
1361 sas_check_ex_subtractive_boundary(dev);
1362 res = sas_check_parent_topology(dev);
1363 if (res)
1364 goto out_err;
1365 return 0;
1366out_err:
1367 sas_notify_lldd_dev_gone(dev);
1368 return res;
1369}
1370
1371static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1372{
1373 int res = 0;
1374 struct domain_device *dev;
1375
1376 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1377 if (dev->dev_type == EDGE_DEV ||
1378 dev->dev_type == FANOUT_DEV) {
1379 struct sas_expander_device *ex =
1380 rphy_to_expander_device(dev->rphy);
1381
1382 if (level == ex->level)
1383 res = sas_ex_discover_devices(dev, -1);
1384 else if (level > 0)
1385 res = sas_ex_discover_devices(port->port_dev, -1);
1386
1387 }
1388 }
1389
1390 return res;
1391}
1392
1393static int sas_ex_bfs_disc(struct asd_sas_port *port)
1394{
1395 int res;
1396 int level;
1397
1398 do {
1399 level = port->disc.max_level;
1400 res = sas_ex_level_discovery(port, level);
1401 mb();
1402 } while (level < port->disc.max_level);
1403
1404 return res;
1405}
1406
1407int sas_discover_root_expander(struct domain_device *dev)
1408{
1409 int res;
1410 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1411
1412 sas_rphy_add(dev->rphy);
1413
1414 ex->level = dev->port->disc.max_level; /* 0 */
1415 res = sas_discover_expander(dev);
1416 if (!res)
1417 sas_ex_bfs_disc(dev->port);
1418
1419 return res;
1420}
1421
1422/* ---------- Domain revalidation ---------- */
1423
1424static int sas_get_phy_discover(struct domain_device *dev,
1425 int phy_id, struct smp_resp *disc_resp)
1426{
1427 int res;
1428 u8 *disc_req;
1429
1430 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1431 if (!disc_req)
1432 return -ENOMEM;
1433
1434 disc_req[1] = SMP_DISCOVER;
1435 disc_req[9] = phy_id;
1436
1437 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1438 disc_resp, DISCOVER_RESP_SIZE);
1439 if (res)
1440 goto out;
1441 else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1442 res = disc_resp->result;
1443 goto out;
1444 }
1445out:
1446 kfree(disc_req);
1447 return res;
1448}
1449
1450static int sas_get_phy_change_count(struct domain_device *dev,
1451 int phy_id, int *pcc)
1452{
1453 int res;
1454 struct smp_resp *disc_resp;
1455
1456 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1457 if (!disc_resp)
1458 return -ENOMEM;
1459
1460 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1461 if (!res)
1462 *pcc = disc_resp->disc.change_count;
1463
1464 kfree(disc_resp);
1465 return res;
1466}
1467
1468static int sas_get_phy_attached_sas_addr(struct domain_device *dev,
1469 int phy_id, u8 *attached_sas_addr)
1470{
1471 int res;
1472 struct smp_resp *disc_resp;
1473 struct discover_resp *dr;
1474
1475 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1476 if (!disc_resp)
1477 return -ENOMEM;
1478 dr = &disc_resp->disc;
1479
1480 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1481 if (!res) {
1482 memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
1483 if (dr->attached_dev_type == 0)
1484 memset(attached_sas_addr, 0, 8);
1485 }
1486 kfree(disc_resp);
1487 return res;
1488}
1489
1490static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
1491 int from_phy)
1492{
1493 struct expander_device *ex = &dev->ex_dev;
1494 int res = 0;
1495 int i;
1496
1497 for (i = from_phy; i < ex->num_phys; i++) {
1498 int phy_change_count = 0;
1499
1500 res = sas_get_phy_change_count(dev, i, &phy_change_count);
1501 if (res)
1502 goto out;
1503 else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
1504 ex->ex_phy[i].phy_change_count = phy_change_count;
1505 *phy_id = i;
1506 return 0;
1507 }
1508 }
1509out:
1510 return res;
1511}
1512
1513static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1514{
1515 int res;
1516 u8 *rg_req;
1517 struct smp_resp *rg_resp;
1518
1519 rg_req = alloc_smp_req(RG_REQ_SIZE);
1520 if (!rg_req)
1521 return -ENOMEM;
1522
1523 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1524 if (!rg_resp) {
1525 kfree(rg_req);
1526 return -ENOMEM;
1527 }
1528
1529 rg_req[1] = SMP_REPORT_GENERAL;
1530
1531 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1532 RG_RESP_SIZE);
1533 if (res)
1534 goto out;
1535 if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1536 res = rg_resp->result;
1537 goto out;
1538 }
1539
1540 *ecc = be16_to_cpu(rg_resp->rg.change_count);
1541out:
1542 kfree(rg_resp);
1543 kfree(rg_req);
1544 return res;
1545}
1546
1547static int sas_find_bcast_dev(struct domain_device *dev,
1548 struct domain_device **src_dev)
1549{
1550 struct expander_device *ex = &dev->ex_dev;
1551 int ex_change_count = -1;
1552 int res;
1553
1554 res = sas_get_ex_change_count(dev, &ex_change_count);
1555 if (res)
1556 goto out;
1557 if (ex_change_count != -1 &&
1558 ex_change_count != ex->ex_change_count) {
1559 *src_dev = dev;
1560 ex->ex_change_count = ex_change_count;
1561 } else {
1562 struct domain_device *ch;
1563
1564 list_for_each_entry(ch, &ex->children, siblings) {
1565 if (ch->dev_type == EDGE_DEV ||
1566 ch->dev_type == FANOUT_DEV) {
1567 res = sas_find_bcast_dev(ch, src_dev);
1568 if (src_dev)
1569 return res;
1570 }
1571 }
1572 }
1573out:
1574 return res;
1575}
1576
1577static void sas_unregister_ex_tree(struct domain_device *dev)
1578{
1579 struct expander_device *ex = &dev->ex_dev;
1580 struct domain_device *child, *n;
1581
1582 list_for_each_entry_safe(child, n, &ex->children, siblings) {
1583 if (child->dev_type == EDGE_DEV ||
1584 child->dev_type == FANOUT_DEV)
1585 sas_unregister_ex_tree(child);
1586 else
1587 sas_unregister_dev(child);
1588 }
1589 sas_unregister_dev(dev);
1590}
1591
1592static void sas_unregister_devs_sas_addr(struct domain_device *parent,
1593 int phy_id)
1594{
1595 struct expander_device *ex_dev = &parent->ex_dev;
1596 struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
1597 struct domain_device *child, *n;
1598
1599 list_for_each_entry_safe(child, n, &ex_dev->children, siblings) {
1600 if (SAS_ADDR(child->sas_addr) ==
1601 SAS_ADDR(phy->attached_sas_addr)) {
1602 if (child->dev_type == EDGE_DEV ||
1603 child->dev_type == FANOUT_DEV)
1604 sas_unregister_ex_tree(child);
1605 else
1606 sas_unregister_dev(child);
1607 break;
1608 }
1609 }
1610 sas_disable_routing(parent, phy->attached_sas_addr);
1611 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
1612 sas_port_delete_phy(phy->port, phy->phy);
1613 if (phy->port->num_phys == 0)
1614 sas_port_delete(phy->port);
1615 phy->port = NULL;
1616}
1617
1618static int sas_discover_bfs_by_root_level(struct domain_device *root,
1619 const int level)
1620{
1621 struct expander_device *ex_root = &root->ex_dev;
1622 struct domain_device *child;
1623 int res = 0;
1624
1625 list_for_each_entry(child, &ex_root->children, siblings) {
1626 if (child->dev_type == EDGE_DEV ||
1627 child->dev_type == FANOUT_DEV) {
1628 struct sas_expander_device *ex =
1629 rphy_to_expander_device(child->rphy);
1630
1631 if (level > ex->level)
1632 res = sas_discover_bfs_by_root_level(child,
1633 level);
1634 else if (level == ex->level)
1635 res = sas_ex_discover_devices(child, -1);
1636 }
1637 }
1638 return res;
1639}
1640
1641static int sas_discover_bfs_by_root(struct domain_device *dev)
1642{
1643 int res;
1644 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1645 int level = ex->level+1;
1646
1647 res = sas_ex_discover_devices(dev, -1);
1648 if (res)
1649 goto out;
1650 do {
1651 res = sas_discover_bfs_by_root_level(dev, level);
1652 mb();
1653 level += 1;
1654 } while (level <= dev->port->disc.max_level);
1655out:
1656 return res;
1657}
1658
1659static int sas_discover_new(struct domain_device *dev, int phy_id)
1660{
1661 struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1662 struct domain_device *child;
1663 int res;
1664
1665 SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1666 SAS_ADDR(dev->sas_addr), phy_id);
1667 res = sas_ex_phy_discover(dev, phy_id);
1668 if (res)
1669 goto out;
1670 res = sas_ex_discover_devices(dev, phy_id);
1671 if (res)
1672 goto out;
1673 list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1674 if (SAS_ADDR(child->sas_addr) ==
1675 SAS_ADDR(ex_phy->attached_sas_addr)) {
1676 if (child->dev_type == EDGE_DEV ||
1677 child->dev_type == FANOUT_DEV)
1678 res = sas_discover_bfs_by_root(child);
1679 break;
1680 }
1681 }
1682out:
1683 return res;
1684}
1685
1686static int sas_rediscover_dev(struct domain_device *dev, int phy_id)
1687{
1688 struct expander_device *ex = &dev->ex_dev;
1689 struct ex_phy *phy = &ex->ex_phy[phy_id];
1690 u8 attached_sas_addr[8];
1691 int res;
1692
1693 res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
1694 switch (res) {
1695 case SMP_RESP_NO_PHY:
1696 phy->phy_state = PHY_NOT_PRESENT;
1697 sas_unregister_devs_sas_addr(dev, phy_id);
1698 goto out; break;
1699 case SMP_RESP_PHY_VACANT:
1700 phy->phy_state = PHY_VACANT;
1701 sas_unregister_devs_sas_addr(dev, phy_id);
1702 goto out; break;
1703 case SMP_RESP_FUNC_ACC:
1704 break;
1705 }
1706
1707 if (SAS_ADDR(attached_sas_addr) == 0) {
1708 phy->phy_state = PHY_EMPTY;
1709 sas_unregister_devs_sas_addr(dev, phy_id);
1710 } else if (SAS_ADDR(attached_sas_addr) ==
1711 SAS_ADDR(phy->attached_sas_addr)) {
1712 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
1713 SAS_ADDR(dev->sas_addr), phy_id);
1714 sas_ex_phy_discover(dev, phy_id);
1715 } else
1716 res = sas_discover_new(dev, phy_id);
1717out:
1718 return res;
1719}
1720
1721static int sas_rediscover(struct domain_device *dev, const int phy_id)
1722{
1723 struct expander_device *ex = &dev->ex_dev;
1724 struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
1725 int res = 0;
1726 int i;
1727
1728 SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
1729 SAS_ADDR(dev->sas_addr), phy_id);
1730
1731 if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
1732 for (i = 0; i < ex->num_phys; i++) {
1733 struct ex_phy *phy = &ex->ex_phy[i];
1734
1735 if (i == phy_id)
1736 continue;
1737 if (SAS_ADDR(phy->attached_sas_addr) ==
1738 SAS_ADDR(changed_phy->attached_sas_addr)) {
1739 SAS_DPRINTK("phy%d part of wide port with "
1740 "phy%d\n", phy_id, i);
1741 goto out;
1742 }
1743 }
1744 res = sas_rediscover_dev(dev, phy_id);
1745 } else
1746 res = sas_discover_new(dev, phy_id);
1747out:
1748 return res;
1749}
1750
1751/**
1752 * sas_revalidate_domain -- revalidate the domain
1753 * @port: port to the domain of interest
1754 *
1755 * NOTE: this process _must_ quit (return) as soon as any connection
1756 * errors are encountered. Connection recovery is done elsewhere.
1757 * Discover process only interrogates devices in order to discover the
1758 * domain.
1759 */
1760int sas_ex_revalidate_domain(struct domain_device *port_dev)
1761{
1762 int res;
1763 struct domain_device *dev = NULL;
1764
1765 res = sas_find_bcast_dev(port_dev, &dev);
1766 if (res)
1767 goto out;
1768 if (dev) {
1769 struct expander_device *ex = &dev->ex_dev;
1770 int i = 0, phy_id;
1771
1772 do {
1773 phy_id = -1;
1774 res = sas_find_bcast_phy(dev, &phy_id, i);
1775 if (phy_id == -1)
1776 break;
1777 res = sas_rediscover(dev, phy_id);
1778 i = phy_id + 1;
1779 } while (i < ex->num_phys);
1780 }
1781out:
1782 return res;
1783}
1784
1785#if 0
1786/* ---------- SMP portal ---------- */
1787
1788static ssize_t smp_portal_write(struct kobject *kobj, char *buf, loff_t offs,
1789 size_t size)
1790{
1791 struct domain_device *dev = to_dom_device(kobj);
1792 struct expander_device *ex = &dev->ex_dev;
1793
1794 if (offs != 0)
1795 return -EFBIG;
1796 else if (size == 0)
1797 return 0;
1798
1799 down_interruptible(&ex->smp_sema);
1800 if (ex->smp_req)
1801 kfree(ex->smp_req);
1802 ex->smp_req = kzalloc(size, GFP_USER);
1803 if (!ex->smp_req) {
1804 up(&ex->smp_sema);
1805 return -ENOMEM;
1806 }
1807 memcpy(ex->smp_req, buf, size);
1808 ex->smp_req_size = size;
1809 ex->smp_portal_pid = current->pid;
1810 up(&ex->smp_sema);
1811
1812 return size;
1813}
1814
1815static ssize_t smp_portal_read(struct kobject *kobj, char *buf, loff_t offs,
1816 size_t size)
1817{
1818 struct domain_device *dev = to_dom_device(kobj);
1819 struct expander_device *ex = &dev->ex_dev;
1820 u8 *smp_resp;
1821 int res = -EINVAL;
1822
1823 /* XXX: sysfs gives us an offset of 0x10 or 0x8 while in fact
1824 * it should be 0.
1825 */
1826
1827 down_interruptible(&ex->smp_sema);
1828 if (!ex->smp_req || ex->smp_portal_pid != current->pid)
1829 goto out;
1830
1831 res = 0;
1832 if (size == 0)
1833 goto out;
1834
1835 res = -ENOMEM;
1836 smp_resp = alloc_smp_resp(size);
1837 if (!smp_resp)
1838 goto out;
1839 res = smp_execute_task(dev, ex->smp_req, ex->smp_req_size,
1840 smp_resp, size);
1841 if (!res) {
1842 memcpy(buf, smp_resp, size);
1843 res = size;
1844 }
1845
1846 kfree(smp_resp);
1847out:
1848 kfree(ex->smp_req);
1849 ex->smp_req = NULL;
1850 ex->smp_req_size = 0;
1851 ex->smp_portal_pid = -1;
1852 up(&ex->smp_sema);
1853 return res;
1854}
1855#endif
diff --git a/drivers/scsi/libsas/sas_init.c b/drivers/scsi/libsas/sas_init.c
new file mode 100644
index 000000000000..c836a237fb79
--- /dev/null
+++ b/drivers/scsi/libsas/sas_init.c
@@ -0,0 +1,267 @@
1/*
2 * Serial Attached SCSI (SAS) Transport Layer initialization
3 *
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6 *
7 * This file is licensed under GPLv2.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 *
24 */
25
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/device.h>
29#include <linux/spinlock.h>
30#include <scsi/scsi_host.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_transport.h>
33#include <scsi/scsi_transport_sas.h>
34
35#include "sas_internal.h"
36
37#include "../scsi_sas_internal.h"
38
39kmem_cache_t *sas_task_cache;
40
41/*------------ SAS addr hash -----------*/
42void sas_hash_addr(u8 *hashed, const u8 *sas_addr)
43{
44 const u32 poly = 0x00DB2777;
45 u32 r = 0;
46 int i;
47
48 for (i = 0; i < 8; i++) {
49 int b;
50 for (b = 7; b >= 0; b--) {
51 r <<= 1;
52 if ((1 << b) & sas_addr[i]) {
53 if (!(r & 0x01000000))
54 r ^= poly;
55 } else if (r & 0x01000000)
56 r ^= poly;
57 }
58 }
59
60 hashed[0] = (r >> 16) & 0xFF;
61 hashed[1] = (r >> 8) & 0xFF ;
62 hashed[2] = r & 0xFF;
63}
64
65
66/* ---------- HA events ---------- */
67
68void sas_hae_reset(void *data)
69{
70 struct sas_ha_struct *ha = data;
71
72 sas_begin_event(HAE_RESET, &ha->event_lock,
73 &ha->pending);
74}
75
76int sas_register_ha(struct sas_ha_struct *sas_ha)
77{
78 int error = 0;
79
80 spin_lock_init(&sas_ha->phy_port_lock);
81 sas_hash_addr(sas_ha->hashed_sas_addr, sas_ha->sas_addr);
82
83 if (sas_ha->lldd_queue_size == 0)
84 sas_ha->lldd_queue_size = 1;
85 else if (sas_ha->lldd_queue_size == -1)
86 sas_ha->lldd_queue_size = 128; /* Sanity */
87
88 error = sas_register_phys(sas_ha);
89 if (error) {
90 printk(KERN_NOTICE "couldn't register sas phys:%d\n", error);
91 return error;
92 }
93
94 error = sas_register_ports(sas_ha);
95 if (error) {
96 printk(KERN_NOTICE "couldn't register sas ports:%d\n", error);
97 goto Undo_phys;
98 }
99
100 error = sas_init_events(sas_ha);
101 if (error) {
102 printk(KERN_NOTICE "couldn't start event thread:%d\n", error);
103 goto Undo_ports;
104 }
105
106 if (sas_ha->lldd_max_execute_num > 1) {
107 error = sas_init_queue(sas_ha);
108 if (error) {
109 printk(KERN_NOTICE "couldn't start queue thread:%d, "
110 "running in direct mode\n", error);
111 sas_ha->lldd_max_execute_num = 1;
112 }
113 }
114
115 return 0;
116
117Undo_ports:
118 sas_unregister_ports(sas_ha);
119Undo_phys:
120
121 return error;
122}
123
124int sas_unregister_ha(struct sas_ha_struct *sas_ha)
125{
126 if (sas_ha->lldd_max_execute_num > 1) {
127 sas_shutdown_queue(sas_ha);
128 }
129
130 sas_unregister_ports(sas_ha);
131
132 return 0;
133}
134
135static int sas_get_linkerrors(struct sas_phy *phy)
136{
137 if (scsi_is_sas_phy_local(phy))
138 /* FIXME: we have no local phy stats
139 * gathering at this time */
140 return -EINVAL;
141
142 return sas_smp_get_phy_events(phy);
143}
144
145static int sas_phy_reset(struct sas_phy *phy, int hard_reset)
146{
147 int ret;
148 enum phy_func reset_type;
149
150 if (hard_reset)
151 reset_type = PHY_FUNC_HARD_RESET;
152 else
153 reset_type = PHY_FUNC_LINK_RESET;
154
155 if (scsi_is_sas_phy_local(phy)) {
156 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
157 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
158 struct asd_sas_phy *asd_phy = sas_ha->sas_phy[phy->number];
159 struct sas_internal *i =
160 to_sas_internal(sas_ha->core.shost->transportt);
161
162 ret = i->dft->lldd_control_phy(asd_phy, reset_type, NULL);
163 } else {
164 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
165 struct domain_device *ddev = sas_find_dev_by_rphy(rphy);
166 ret = sas_smp_phy_control(ddev, phy->number, reset_type, NULL);
167 }
168 return ret;
169}
170
171static int sas_set_phy_speed(struct sas_phy *phy,
172 struct sas_phy_linkrates *rates)
173{
174 int ret;
175
176 if ((rates->minimum_linkrate &&
177 rates->minimum_linkrate > phy->maximum_linkrate) ||
178 (rates->maximum_linkrate &&
179 rates->maximum_linkrate < phy->minimum_linkrate))
180 return -EINVAL;
181
182 if (rates->minimum_linkrate &&
183 rates->minimum_linkrate < phy->minimum_linkrate_hw)
184 rates->minimum_linkrate = phy->minimum_linkrate_hw;
185
186 if (rates->maximum_linkrate &&
187 rates->maximum_linkrate > phy->maximum_linkrate_hw)
188 rates->maximum_linkrate = phy->maximum_linkrate_hw;
189
190 if (scsi_is_sas_phy_local(phy)) {
191 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
192 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
193 struct asd_sas_phy *asd_phy = sas_ha->sas_phy[phy->number];
194 struct sas_internal *i =
195 to_sas_internal(sas_ha->core.shost->transportt);
196
197 ret = i->dft->lldd_control_phy(asd_phy, PHY_FUNC_SET_LINK_RATE,
198 rates);
199 } else {
200 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
201 struct domain_device *ddev = sas_find_dev_by_rphy(rphy);
202 ret = sas_smp_phy_control(ddev, phy->number,
203 PHY_FUNC_LINK_RESET, rates);
204
205 }
206
207 return ret;
208}
209
210static struct sas_function_template sft = {
211 .phy_reset = sas_phy_reset,
212 .set_phy_speed = sas_set_phy_speed,
213 .get_linkerrors = sas_get_linkerrors,
214};
215
216struct scsi_transport_template *
217sas_domain_attach_transport(struct sas_domain_function_template *dft)
218{
219 struct scsi_transport_template *stt = sas_attach_transport(&sft);
220 struct sas_internal *i;
221
222 if (!stt)
223 return stt;
224
225 i = to_sas_internal(stt);
226 i->dft = dft;
227 stt->create_work_queue = 1;
228 stt->eh_timed_out = sas_scsi_timed_out;
229 stt->eh_strategy_handler = sas_scsi_recover_host;
230
231 return stt;
232}
233EXPORT_SYMBOL_GPL(sas_domain_attach_transport);
234
235
236void sas_domain_release_transport(struct scsi_transport_template *stt)
237{
238 sas_release_transport(stt);
239}
240EXPORT_SYMBOL_GPL(sas_domain_release_transport);
241
242/* ---------- SAS Class register/unregister ---------- */
243
244static int __init sas_class_init(void)
245{
246 sas_task_cache = kmem_cache_create("sas_task", sizeof(struct sas_task),
247 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
248 if (!sas_task_cache)
249 return -ENOMEM;
250
251 return 0;
252}
253
254static void __exit sas_class_exit(void)
255{
256 kmem_cache_destroy(sas_task_cache);
257}
258
259MODULE_AUTHOR("Luben Tuikov <luben_tuikov@adaptec.com>");
260MODULE_DESCRIPTION("SAS Transport Layer");
261MODULE_LICENSE("GPL v2");
262
263module_init(sas_class_init);
264module_exit(sas_class_exit);
265
266EXPORT_SYMBOL_GPL(sas_register_ha);
267EXPORT_SYMBOL_GPL(sas_unregister_ha);
diff --git a/drivers/scsi/libsas/sas_internal.h b/drivers/scsi/libsas/sas_internal.h
new file mode 100644
index 000000000000..bffcee474921
--- /dev/null
+++ b/drivers/scsi/libsas/sas_internal.h
@@ -0,0 +1,146 @@
1/*
2 * Serial Attached SCSI (SAS) class internal header file
3 *
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6 *
7 * This file is licensed under GPLv2.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 *
24 */
25
26#ifndef _SAS_INTERNAL_H_
27#define _SAS_INTERNAL_H_
28
29#include <scsi/scsi.h>
30#include <scsi/scsi_host.h>
31#include <scsi/scsi_transport_sas.h>
32#include <scsi/libsas.h>
33
34#define sas_printk(fmt, ...) printk(KERN_NOTICE "sas: " fmt, ## __VA_ARGS__)
35
36#ifdef SAS_DEBUG
37#define SAS_DPRINTK(fmt, ...) printk(KERN_NOTICE "sas: " fmt, ## __VA_ARGS__)
38#else
39#define SAS_DPRINTK(fmt, ...)
40#endif
41
42void sas_scsi_recover_host(struct Scsi_Host *shost);
43
44int sas_show_class(enum sas_class class, char *buf);
45int sas_show_proto(enum sas_proto proto, char *buf);
46int sas_show_linkrate(enum sas_linkrate linkrate, char *buf);
47int sas_show_oob_mode(enum sas_oob_mode oob_mode, char *buf);
48
49int sas_register_phys(struct sas_ha_struct *sas_ha);
50void sas_unregister_phys(struct sas_ha_struct *sas_ha);
51
52int sas_register_ports(struct sas_ha_struct *sas_ha);
53void sas_unregister_ports(struct sas_ha_struct *sas_ha);
54
55enum scsi_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *);
56
57int sas_init_queue(struct sas_ha_struct *sas_ha);
58int sas_init_events(struct sas_ha_struct *sas_ha);
59void sas_shutdown_queue(struct sas_ha_struct *sas_ha);
60
61void sas_deform_port(struct asd_sas_phy *phy);
62
63void sas_porte_bytes_dmaed(void *);
64void sas_porte_broadcast_rcvd(void *);
65void sas_porte_link_reset_err(void *);
66void sas_porte_timer_event(void *);
67void sas_porte_hard_reset(void *);
68
69int sas_notify_lldd_dev_found(struct domain_device *);
70void sas_notify_lldd_dev_gone(struct domain_device *);
71
72int sas_smp_phy_control(struct domain_device *dev, int phy_id,
73 enum phy_func phy_func, struct sas_phy_linkrates *);
74int sas_smp_get_phy_events(struct sas_phy *phy);
75
76struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy);
77
78void sas_hae_reset(void *);
79
80static inline void sas_queue_event(int event, spinlock_t *lock,
81 unsigned long *pending,
82 struct work_struct *work,
83 struct Scsi_Host *shost)
84{
85 unsigned long flags;
86
87 spin_lock_irqsave(lock, flags);
88 if (test_bit(event, pending)) {
89 spin_unlock_irqrestore(lock, flags);
90 return;
91 }
92 __set_bit(event, pending);
93 spin_unlock_irqrestore(lock, flags);
94 scsi_queue_work(shost, work);
95}
96
97static inline void sas_begin_event(int event, spinlock_t *lock,
98 unsigned long *pending)
99{
100 unsigned long flags;
101
102 spin_lock_irqsave(lock, flags);
103 __clear_bit(event, pending);
104 spin_unlock_irqrestore(lock, flags);
105}
106
107static inline void sas_fill_in_rphy(struct domain_device *dev,
108 struct sas_rphy *rphy)
109{
110 rphy->identify.sas_address = SAS_ADDR(dev->sas_addr);
111 rphy->identify.initiator_port_protocols = dev->iproto;
112 rphy->identify.target_port_protocols = dev->tproto;
113 switch (dev->dev_type) {
114 case SATA_DEV:
115 /* FIXME: need sata device type */
116 case SAS_END_DEV:
117 rphy->identify.device_type = SAS_END_DEVICE;
118 break;
119 case EDGE_DEV:
120 rphy->identify.device_type = SAS_EDGE_EXPANDER_DEVICE;
121 break;
122 case FANOUT_DEV:
123 rphy->identify.device_type = SAS_FANOUT_EXPANDER_DEVICE;
124 break;
125 default:
126 rphy->identify.device_type = SAS_PHY_UNUSED;
127 break;
128 }
129}
130
131static inline void sas_add_parent_port(struct domain_device *dev, int phy_id)
132{
133 struct expander_device *ex = &dev->ex_dev;
134 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
135
136 if (!ex->parent_port) {
137 ex->parent_port = sas_port_alloc(&dev->rphy->dev, phy_id);
138 /* FIXME: error handling */
139 BUG_ON(!ex->parent_port);
140 BUG_ON(sas_port_add(ex->parent_port));
141 sas_port_mark_backlink(ex->parent_port);
142 }
143 sas_port_add_phy(ex->parent_port, ex_phy->phy);
144}
145
146#endif /* _SAS_INTERNAL_H_ */
diff --git a/drivers/scsi/libsas/sas_phy.c b/drivers/scsi/libsas/sas_phy.c
new file mode 100644
index 000000000000..9340cdbae4a3
--- /dev/null
+++ b/drivers/scsi/libsas/sas_phy.c
@@ -0,0 +1,158 @@
1/*
2 * Serial Attached SCSI (SAS) Phy class
3 *
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6 *
7 * This file is licensed under GPLv2.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 */
24
25#include "sas_internal.h"
26#include <scsi/scsi_host.h>
27#include <scsi/scsi_transport.h>
28#include <scsi/scsi_transport_sas.h>
29#include "../scsi_sas_internal.h"
30
31/* ---------- Phy events ---------- */
32
33static void sas_phye_loss_of_signal(void *data)
34{
35 struct asd_sas_phy *phy = data;
36
37 sas_begin_event(PHYE_LOSS_OF_SIGNAL, &phy->ha->event_lock,
38 &phy->phy_events_pending);
39 phy->error = 0;
40 sas_deform_port(phy);
41}
42
43static void sas_phye_oob_done(void *data)
44{
45 struct asd_sas_phy *phy = data;
46
47 sas_begin_event(PHYE_OOB_DONE, &phy->ha->event_lock,
48 &phy->phy_events_pending);
49 phy->error = 0;
50}
51
52static void sas_phye_oob_error(void *data)
53{
54 struct asd_sas_phy *phy = data;
55 struct sas_ha_struct *sas_ha = phy->ha;
56 struct asd_sas_port *port = phy->port;
57 struct sas_internal *i =
58 to_sas_internal(sas_ha->core.shost->transportt);
59
60 sas_begin_event(PHYE_OOB_ERROR, &phy->ha->event_lock,
61 &phy->phy_events_pending);
62
63 sas_deform_port(phy);
64
65 if (!port && phy->enabled && i->dft->lldd_control_phy) {
66 phy->error++;
67 switch (phy->error) {
68 case 1:
69 case 2:
70 i->dft->lldd_control_phy(phy, PHY_FUNC_HARD_RESET,
71 NULL);
72 break;
73 case 3:
74 default:
75 phy->error = 0;
76 phy->enabled = 0;
77 i->dft->lldd_control_phy(phy, PHY_FUNC_DISABLE, NULL);
78 break;
79 }
80 }
81}
82
83static void sas_phye_spinup_hold(void *data)
84{
85 struct asd_sas_phy *phy = data;
86 struct sas_ha_struct *sas_ha = phy->ha;
87 struct sas_internal *i =
88 to_sas_internal(sas_ha->core.shost->transportt);
89
90 sas_begin_event(PHYE_SPINUP_HOLD, &phy->ha->event_lock,
91 &phy->phy_events_pending);
92
93 phy->error = 0;
94 i->dft->lldd_control_phy(phy, PHY_FUNC_RELEASE_SPINUP_HOLD, NULL);
95}
96
97/* ---------- Phy class registration ---------- */
98
99int sas_register_phys(struct sas_ha_struct *sas_ha)
100{
101 int i;
102
103 static void (*sas_phy_event_fns[PHY_NUM_EVENTS])(void *) = {
104 [PHYE_LOSS_OF_SIGNAL] = sas_phye_loss_of_signal,
105 [PHYE_OOB_DONE] = sas_phye_oob_done,
106 [PHYE_OOB_ERROR] = sas_phye_oob_error,
107 [PHYE_SPINUP_HOLD] = sas_phye_spinup_hold,
108 };
109
110 static void (*sas_port_event_fns[PORT_NUM_EVENTS])(void *) = {
111 [PORTE_BYTES_DMAED] = sas_porte_bytes_dmaed,
112 [PORTE_BROADCAST_RCVD] = sas_porte_broadcast_rcvd,
113 [PORTE_LINK_RESET_ERR] = sas_porte_link_reset_err,
114 [PORTE_TIMER_EVENT] = sas_porte_timer_event,
115 [PORTE_HARD_RESET] = sas_porte_hard_reset,
116 };
117
118 /* Now register the phys. */
119 for (i = 0; i < sas_ha->num_phys; i++) {
120 int k;
121 struct asd_sas_phy *phy = sas_ha->sas_phy[i];
122
123 phy->error = 0;
124 INIT_LIST_HEAD(&phy->port_phy_el);
125 for (k = 0; k < PORT_NUM_EVENTS; k++)
126 INIT_WORK(&phy->port_events[k], sas_port_event_fns[k],
127 phy);
128
129 for (k = 0; k < PHY_NUM_EVENTS; k++)
130 INIT_WORK(&phy->phy_events[k], sas_phy_event_fns[k],
131 phy);
132 phy->port = NULL;
133 phy->ha = sas_ha;
134 spin_lock_init(&phy->frame_rcvd_lock);
135 spin_lock_init(&phy->sas_prim_lock);
136 phy->frame_rcvd_size = 0;
137
138 phy->phy = sas_phy_alloc(&sas_ha->core.shost->shost_gendev,
139 i);
140 if (!phy->phy)
141 return -ENOMEM;
142
143 phy->phy->identify.initiator_port_protocols =
144 phy->iproto;
145 phy->phy->identify.target_port_protocols = phy->tproto;
146 phy->phy->identify.sas_address = SAS_ADDR(sas_ha->sas_addr);
147 phy->phy->identify.phy_identifier = i;
148 phy->phy->minimum_linkrate_hw = SAS_LINK_RATE_UNKNOWN;
149 phy->phy->maximum_linkrate_hw = SAS_LINK_RATE_UNKNOWN;
150 phy->phy->minimum_linkrate = SAS_LINK_RATE_UNKNOWN;
151 phy->phy->maximum_linkrate = SAS_LINK_RATE_UNKNOWN;
152 phy->phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
153
154 sas_phy_add(phy->phy);
155 }
156
157 return 0;
158}
diff --git a/drivers/scsi/libsas/sas_port.c b/drivers/scsi/libsas/sas_port.c
new file mode 100644
index 000000000000..253cdcf306a2
--- /dev/null
+++ b/drivers/scsi/libsas/sas_port.c
@@ -0,0 +1,279 @@
1/*
2 * Serial Attached SCSI (SAS) Port class
3 *
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6 *
7 * This file is licensed under GPLv2.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 */
24
25#include "sas_internal.h"
26
27#include <scsi/scsi_transport.h>
28#include <scsi/scsi_transport_sas.h>
29#include "../scsi_sas_internal.h"
30
31/**
32 * sas_form_port -- add this phy to a port
33 * @phy: the phy of interest
34 *
35 * This function adds this phy to an existing port, thus creating a wide
36 * port, or it creates a port and adds the phy to the port.
37 */
38static void sas_form_port(struct asd_sas_phy *phy)
39{
40 int i;
41 struct sas_ha_struct *sas_ha = phy->ha;
42 struct asd_sas_port *port = phy->port;
43 struct sas_internal *si =
44 to_sas_internal(sas_ha->core.shost->transportt);
45
46 if (port) {
47 if (memcmp(port->attached_sas_addr, phy->attached_sas_addr,
48 SAS_ADDR_SIZE) == 0)
49 sas_deform_port(phy);
50 else {
51 SAS_DPRINTK("%s: phy%d belongs to port%d already(%d)!\n",
52 __FUNCTION__, phy->id, phy->port->id,
53 phy->port->num_phys);
54 return;
55 }
56 }
57
58 /* find a port */
59 spin_lock(&sas_ha->phy_port_lock);
60 for (i = 0; i < sas_ha->num_phys; i++) {
61 port = sas_ha->sas_port[i];
62 spin_lock(&port->phy_list_lock);
63 if (*(u64 *) port->sas_addr &&
64 memcmp(port->attached_sas_addr,
65 phy->attached_sas_addr, SAS_ADDR_SIZE) == 0 &&
66 port->num_phys > 0) {
67 /* wide port */
68 SAS_DPRINTK("phy%d matched wide port%d\n", phy->id,
69 port->id);
70 break;
71 } else if (*(u64 *) port->sas_addr == 0 && port->num_phys==0) {
72 memcpy(port->sas_addr, phy->sas_addr, SAS_ADDR_SIZE);
73 break;
74 }
75 spin_unlock(&port->phy_list_lock);
76 }
77
78 if (i >= sas_ha->num_phys) {
79 printk(KERN_NOTICE "%s: couldn't find a free port, bug?\n",
80 __FUNCTION__);
81 spin_unlock(&sas_ha->phy_port_lock);
82 return;
83 }
84
85 /* add the phy to the port */
86 list_add_tail(&phy->port_phy_el, &port->phy_list);
87 phy->port = port;
88 port->num_phys++;
89 port->phy_mask |= (1U << phy->id);
90
91 if (!port->phy)
92 port->phy = phy->phy;
93
94 SAS_DPRINTK("phy%d added to port%d, phy_mask:0x%x\n", phy->id,
95 port->id, port->phy_mask);
96
97 if (*(u64 *)port->attached_sas_addr == 0) {
98 port->class = phy->class;
99 memcpy(port->attached_sas_addr, phy->attached_sas_addr,
100 SAS_ADDR_SIZE);
101 port->iproto = phy->iproto;
102 port->tproto = phy->tproto;
103 port->oob_mode = phy->oob_mode;
104 port->linkrate = phy->linkrate;
105 } else
106 port->linkrate = max(port->linkrate, phy->linkrate);
107 spin_unlock(&port->phy_list_lock);
108 spin_unlock(&sas_ha->phy_port_lock);
109
110 if (!port->port) {
111 port->port = sas_port_alloc(phy->phy->dev.parent, port->id);
112 BUG_ON(!port->port);
113 sas_port_add(port->port);
114 }
115 sas_port_add_phy(port->port, phy->phy);
116
117 if (port->port_dev)
118 port->port_dev->pathways = port->num_phys;
119
120 /* Tell the LLDD about this port formation. */
121 if (si->dft->lldd_port_formed)
122 si->dft->lldd_port_formed(phy);
123
124 sas_discover_event(phy->port, DISCE_DISCOVER_DOMAIN);
125}
126
127/**
128 * sas_deform_port -- remove this phy from the port it belongs to
129 * @phy: the phy of interest
130 *
131 * This is called when the physical link to the other phy has been
132 * lost (on this phy), in Event thread context. We cannot delay here.
133 */
134void sas_deform_port(struct asd_sas_phy *phy)
135{
136 struct sas_ha_struct *sas_ha = phy->ha;
137 struct asd_sas_port *port = phy->port;
138 struct sas_internal *si =
139 to_sas_internal(sas_ha->core.shost->transportt);
140
141 if (!port)
142 return; /* done by a phy event */
143
144 if (port->port_dev)
145 port->port_dev->pathways--;
146
147 if (port->num_phys == 1) {
148 sas_unregister_domain_devices(port);
149 sas_port_delete(port->port);
150 port->port = NULL;
151 } else
152 sas_port_delete_phy(port->port, phy->phy);
153
154
155 if (si->dft->lldd_port_deformed)
156 si->dft->lldd_port_deformed(phy);
157
158 spin_lock(&sas_ha->phy_port_lock);
159 spin_lock(&port->phy_list_lock);
160
161 list_del_init(&phy->port_phy_el);
162 phy->port = NULL;
163 port->num_phys--;
164 port->phy_mask &= ~(1U << phy->id);
165
166 if (port->num_phys == 0) {
167 INIT_LIST_HEAD(&port->phy_list);
168 memset(port->sas_addr, 0, SAS_ADDR_SIZE);
169 memset(port->attached_sas_addr, 0, SAS_ADDR_SIZE);
170 port->class = 0;
171 port->iproto = 0;
172 port->tproto = 0;
173 port->oob_mode = 0;
174 port->phy_mask = 0;
175 }
176 spin_unlock(&port->phy_list_lock);
177 spin_unlock(&sas_ha->phy_port_lock);
178
179 return;
180}
181
182/* ---------- SAS port events ---------- */
183
184void sas_porte_bytes_dmaed(void *data)
185{
186 struct asd_sas_phy *phy = data;
187
188 sas_begin_event(PORTE_BYTES_DMAED, &phy->ha->event_lock,
189 &phy->port_events_pending);
190
191 sas_form_port(phy);
192}
193
194void sas_porte_broadcast_rcvd(void *data)
195{
196 unsigned long flags;
197 u32 prim;
198 struct asd_sas_phy *phy = data;
199
200 sas_begin_event(PORTE_BROADCAST_RCVD, &phy->ha->event_lock,
201 &phy->port_events_pending);
202
203 spin_lock_irqsave(&phy->sas_prim_lock, flags);
204 prim = phy->sas_prim;
205 spin_unlock_irqrestore(&phy->sas_prim_lock, flags);
206
207 SAS_DPRINTK("broadcast received: %d\n", prim);
208 sas_discover_event(phy->port, DISCE_REVALIDATE_DOMAIN);
209}
210
211void sas_porte_link_reset_err(void *data)
212{
213 struct asd_sas_phy *phy = data;
214
215 sas_begin_event(PORTE_LINK_RESET_ERR, &phy->ha->event_lock,
216 &phy->port_events_pending);
217
218 sas_deform_port(phy);
219}
220
221void sas_porte_timer_event(void *data)
222{
223 struct asd_sas_phy *phy = data;
224
225 sas_begin_event(PORTE_TIMER_EVENT, &phy->ha->event_lock,
226 &phy->port_events_pending);
227
228 sas_deform_port(phy);
229}
230
231void sas_porte_hard_reset(void *data)
232{
233 struct asd_sas_phy *phy = data;
234
235 sas_begin_event(PORTE_HARD_RESET, &phy->ha->event_lock,
236 &phy->port_events_pending);
237
238 sas_deform_port(phy);
239}
240
241/* ---------- SAS port registration ---------- */
242
243static void sas_init_port(struct asd_sas_port *port,
244 struct sas_ha_struct *sas_ha, int i)
245{
246 port->id = i;
247 INIT_LIST_HEAD(&port->dev_list);
248 spin_lock_init(&port->phy_list_lock);
249 INIT_LIST_HEAD(&port->phy_list);
250 port->num_phys = 0;
251 port->phy_mask = 0;
252 port->ha = sas_ha;
253
254 spin_lock_init(&port->dev_list_lock);
255}
256
257int sas_register_ports(struct sas_ha_struct *sas_ha)
258{
259 int i;
260
261 /* initialize the ports and discovery */
262 for (i = 0; i < sas_ha->num_phys; i++) {
263 struct asd_sas_port *port = sas_ha->sas_port[i];
264
265 sas_init_port(port, sas_ha, i);
266 sas_init_disc(&port->disc, port);
267 }
268 return 0;
269}
270
271void sas_unregister_ports(struct sas_ha_struct *sas_ha)
272{
273 int i;
274
275 for (i = 0; i < sas_ha->num_phys; i++)
276 if (sas_ha->sas_phy[i]->port)
277 sas_deform_port(sas_ha->sas_phy[i]);
278
279}
diff --git a/drivers/scsi/libsas/sas_scsi_host.c b/drivers/scsi/libsas/sas_scsi_host.c
new file mode 100644
index 000000000000..43e0e4e36934
--- /dev/null
+++ b/drivers/scsi/libsas/sas_scsi_host.c
@@ -0,0 +1,786 @@
1/*
2 * Serial Attached SCSI (SAS) class SCSI Host glue.
3 *
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6 *
7 * This file is licensed under GPLv2.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 *
24 */
25
26#include "sas_internal.h"
27
28#include <scsi/scsi_host.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_tcq.h>
31#include <scsi/scsi.h>
32#include <scsi/scsi_transport.h>
33#include <scsi/scsi_transport_sas.h>
34#include "../scsi_sas_internal.h"
35
36#include <linux/err.h>
37#include <linux/blkdev.h>
38#include <linux/scatterlist.h>
39
40/* ---------- SCSI Host glue ---------- */
41
42#define TO_SAS_TASK(_scsi_cmd) ((void *)(_scsi_cmd)->host_scribble)
43#define ASSIGN_SAS_TASK(_sc, _t) do { (_sc)->host_scribble = (void *) _t; } while (0)
44
45static void sas_scsi_task_done(struct sas_task *task)
46{
47 struct task_status_struct *ts = &task->task_status;
48 struct scsi_cmnd *sc = task->uldd_task;
49 unsigned ts_flags = task->task_state_flags;
50 int hs = 0, stat = 0;
51
52 if (unlikely(!sc)) {
53 SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
54 list_del_init(&task->list);
55 sas_free_task(task);
56 return;
57 }
58
59 if (ts->resp == SAS_TASK_UNDELIVERED) {
60 /* transport error */
61 hs = DID_NO_CONNECT;
62 } else { /* ts->resp == SAS_TASK_COMPLETE */
63 /* task delivered, what happened afterwards? */
64 switch (ts->stat) {
65 case SAS_DEV_NO_RESPONSE:
66 case SAS_INTERRUPTED:
67 case SAS_PHY_DOWN:
68 case SAS_NAK_R_ERR:
69 case SAS_OPEN_TO:
70 hs = DID_NO_CONNECT;
71 break;
72 case SAS_DATA_UNDERRUN:
73 sc->resid = ts->residual;
74 if (sc->request_bufflen - sc->resid < sc->underflow)
75 hs = DID_ERROR;
76 break;
77 case SAS_DATA_OVERRUN:
78 hs = DID_ERROR;
79 break;
80 case SAS_QUEUE_FULL:
81 hs = DID_SOFT_ERROR; /* retry */
82 break;
83 case SAS_DEVICE_UNKNOWN:
84 hs = DID_BAD_TARGET;
85 break;
86 case SAS_SG_ERR:
87 hs = DID_PARITY;
88 break;
89 case SAS_OPEN_REJECT:
90 if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
91 hs = DID_SOFT_ERROR; /* retry */
92 else
93 hs = DID_ERROR;
94 break;
95 case SAS_PROTO_RESPONSE:
96 SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
97 "task; please report this\n",
98 task->dev->port->ha->sas_ha_name);
99 break;
100 case SAS_ABORTED_TASK:
101 hs = DID_ABORT;
102 break;
103 case SAM_CHECK_COND:
104 memcpy(sc->sense_buffer, ts->buf,
105 max(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
106 stat = SAM_CHECK_COND;
107 break;
108 default:
109 stat = ts->stat;
110 break;
111 }
112 }
113 ASSIGN_SAS_TASK(sc, NULL);
114 sc->result = (hs << 16) | stat;
115 list_del_init(&task->list);
116 sas_free_task(task);
117 /* This is very ugly but this is how SCSI Core works. */
118 if (ts_flags & SAS_TASK_STATE_ABORTED)
119 scsi_finish_command(sc);
120 else
121 sc->scsi_done(sc);
122}
123
124static enum task_attribute sas_scsi_get_task_attr(struct scsi_cmnd *cmd)
125{
126 enum task_attribute ta = TASK_ATTR_SIMPLE;
127 if (cmd->request && blk_rq_tagged(cmd->request)) {
128 if (cmd->device->ordered_tags &&
129 (cmd->request->flags & REQ_HARDBARRIER))
130 ta = TASK_ATTR_HOQ;
131 }
132 return ta;
133}
134
135static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
136 struct domain_device *dev,
137 unsigned long gfp_flags)
138{
139 struct sas_task *task = sas_alloc_task(gfp_flags);
140 struct scsi_lun lun;
141
142 if (!task)
143 return NULL;
144
145 *(u32 *)cmd->sense_buffer = 0;
146 task->uldd_task = cmd;
147 ASSIGN_SAS_TASK(cmd, task);
148
149 task->dev = dev;
150 task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
151
152 task->ssp_task.retry_count = 1;
153 int_to_scsilun(cmd->device->lun, &lun);
154 memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
155 task->ssp_task.task_attr = sas_scsi_get_task_attr(cmd);
156 memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
157
158 task->scatter = cmd->request_buffer;
159 task->num_scatter = cmd->use_sg;
160 task->total_xfer_len = cmd->request_bufflen;
161 task->data_dir = cmd->sc_data_direction;
162
163 task->task_done = sas_scsi_task_done;
164
165 return task;
166}
167
168static int sas_queue_up(struct sas_task *task)
169{
170 struct sas_ha_struct *sas_ha = task->dev->port->ha;
171 struct scsi_core *core = &sas_ha->core;
172 unsigned long flags;
173 LIST_HEAD(list);
174
175 spin_lock_irqsave(&core->task_queue_lock, flags);
176 if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
177 spin_unlock_irqrestore(&core->task_queue_lock, flags);
178 return -SAS_QUEUE_FULL;
179 }
180 list_add_tail(&task->list, &core->task_queue);
181 core->task_queue_size += 1;
182 spin_unlock_irqrestore(&core->task_queue_lock, flags);
183 up(&core->queue_thread_sema);
184
185 return 0;
186}
187
188/**
189 * sas_queuecommand -- Enqueue a command for processing
190 * @parameters: See SCSI Core documentation
191 *
192 * Note: XXX: Remove the host unlock/lock pair when SCSI Core can
193 * call us without holding an IRQ spinlock...
194 */
195int sas_queuecommand(struct scsi_cmnd *cmd,
196 void (*scsi_done)(struct scsi_cmnd *))
197{
198 int res = 0;
199 struct domain_device *dev = cmd_to_domain_dev(cmd);
200 struct Scsi_Host *host = cmd->device->host;
201 struct sas_internal *i = to_sas_internal(host->transportt);
202
203 spin_unlock_irq(host->host_lock);
204
205 {
206 struct sas_ha_struct *sas_ha = dev->port->ha;
207 struct sas_task *task;
208
209 res = -ENOMEM;
210 task = sas_create_task(cmd, dev, GFP_ATOMIC);
211 if (!task)
212 goto out;
213
214 cmd->scsi_done = scsi_done;
215 /* Queue up, Direct Mode or Task Collector Mode. */
216 if (sas_ha->lldd_max_execute_num < 2)
217 res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
218 else
219 res = sas_queue_up(task);
220
221 /* Examine */
222 if (res) {
223 SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
224 ASSIGN_SAS_TASK(cmd, NULL);
225 sas_free_task(task);
226 if (res == -SAS_QUEUE_FULL) {
227 cmd->result = DID_SOFT_ERROR << 16; /* retry */
228 res = 0;
229 scsi_done(cmd);
230 }
231 goto out;
232 }
233 }
234out:
235 spin_lock_irq(host->host_lock);
236 return res;
237}
238
239static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
240{
241 struct scsi_cmnd *cmd, *n;
242
243 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
244 if (cmd == my_cmd)
245 list_del_init(&cmd->eh_entry);
246 }
247}
248
249static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
250 struct domain_device *dev)
251{
252 struct scsi_cmnd *cmd, *n;
253
254 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
255 struct domain_device *x = cmd_to_domain_dev(cmd);
256
257 if (x == dev)
258 list_del_init(&cmd->eh_entry);
259 }
260}
261
262static void sas_scsi_clear_queue_port(struct list_head *error_q,
263 struct asd_sas_port *port)
264{
265 struct scsi_cmnd *cmd, *n;
266
267 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
268 struct domain_device *dev = cmd_to_domain_dev(cmd);
269 struct asd_sas_port *x = dev->port;
270
271 if (x == port)
272 list_del_init(&cmd->eh_entry);
273 }
274}
275
276enum task_disposition {
277 TASK_IS_DONE,
278 TASK_IS_ABORTED,
279 TASK_IS_AT_LU,
280 TASK_IS_NOT_AT_LU,
281};
282
283static enum task_disposition sas_scsi_find_task(struct sas_task *task)
284{
285 struct sas_ha_struct *ha = task->dev->port->ha;
286 unsigned long flags;
287 int i, res;
288 struct sas_internal *si =
289 to_sas_internal(task->dev->port->ha->core.shost->transportt);
290
291 if (ha->lldd_max_execute_num > 1) {
292 struct scsi_core *core = &ha->core;
293 struct sas_task *t, *n;
294
295 spin_lock_irqsave(&core->task_queue_lock, flags);
296 list_for_each_entry_safe(t, n, &core->task_queue, list) {
297 if (task == t) {
298 list_del_init(&t->list);
299 spin_unlock_irqrestore(&core->task_queue_lock,
300 flags);
301 SAS_DPRINTK("%s: task 0x%p aborted from "
302 "task_queue\n",
303 __FUNCTION__, task);
304 return TASK_IS_ABORTED;
305 }
306 }
307 spin_unlock_irqrestore(&core->task_queue_lock, flags);
308 }
309
310 for (i = 0; i < 5; i++) {
311 SAS_DPRINTK("%s: aborting task 0x%p\n", __FUNCTION__, task);
312 res = si->dft->lldd_abort_task(task);
313
314 spin_lock_irqsave(&task->task_state_lock, flags);
315 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
316 spin_unlock_irqrestore(&task->task_state_lock, flags);
317 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
318 task);
319 return TASK_IS_DONE;
320 }
321 spin_unlock_irqrestore(&task->task_state_lock, flags);
322
323 if (res == TMF_RESP_FUNC_COMPLETE) {
324 SAS_DPRINTK("%s: task 0x%p is aborted\n",
325 __FUNCTION__, task);
326 return TASK_IS_ABORTED;
327 } else if (si->dft->lldd_query_task) {
328 SAS_DPRINTK("%s: querying task 0x%p\n",
329 __FUNCTION__, task);
330 res = si->dft->lldd_query_task(task);
331 if (res == TMF_RESP_FUNC_SUCC) {
332 SAS_DPRINTK("%s: task 0x%p at LU\n",
333 __FUNCTION__, task);
334 return TASK_IS_AT_LU;
335 } else if (res == TMF_RESP_FUNC_COMPLETE) {
336 SAS_DPRINTK("%s: task 0x%p not at LU\n",
337 __FUNCTION__, task);
338 return TASK_IS_NOT_AT_LU;
339 }
340 }
341 }
342 return res;
343}
344
345static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
346{
347 int res = TMF_RESP_FUNC_FAILED;
348 struct scsi_lun lun;
349 struct sas_internal *i =
350 to_sas_internal(dev->port->ha->core.shost->transportt);
351
352 int_to_scsilun(cmd->device->lun, &lun);
353
354 SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
355 SAS_ADDR(dev->sas_addr),
356 cmd->device->lun);
357
358 if (i->dft->lldd_abort_task_set)
359 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
360
361 if (res == TMF_RESP_FUNC_FAILED) {
362 if (i->dft->lldd_clear_task_set)
363 res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
364 }
365
366 if (res == TMF_RESP_FUNC_FAILED) {
367 if (i->dft->lldd_lu_reset)
368 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
369 }
370
371 return res;
372}
373
374static int sas_recover_I_T(struct domain_device *dev)
375{
376 int res = TMF_RESP_FUNC_FAILED;
377 struct sas_internal *i =
378 to_sas_internal(dev->port->ha->core.shost->transportt);
379
380 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
381 SAS_ADDR(dev->sas_addr));
382
383 if (i->dft->lldd_I_T_nexus_reset)
384 res = i->dft->lldd_I_T_nexus_reset(dev);
385
386 return res;
387}
388
389void sas_scsi_recover_host(struct Scsi_Host *shost)
390{
391 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
392 unsigned long flags;
393 LIST_HEAD(error_q);
394 struct scsi_cmnd *cmd, *n;
395 enum task_disposition res = TASK_IS_DONE;
396 int tmf_resp;
397 struct sas_internal *i = to_sas_internal(shost->transportt);
398
399 spin_lock_irqsave(shost->host_lock, flags);
400 list_splice_init(&shost->eh_cmd_q, &error_q);
401 spin_unlock_irqrestore(shost->host_lock, flags);
402
403 SAS_DPRINTK("Enter %s\n", __FUNCTION__);
404
405 /* All tasks on this list were marked SAS_TASK_STATE_ABORTED
406 * by sas_scsi_timed_out() callback.
407 */
408Again:
409 SAS_DPRINTK("going over list...\n");
410 list_for_each_entry_safe(cmd, n, &error_q, eh_entry) {
411 struct sas_task *task = TO_SAS_TASK(cmd);
412
413 SAS_DPRINTK("trying to find task 0x%p\n", task);
414 list_del_init(&cmd->eh_entry);
415 res = sas_scsi_find_task(task);
416
417 cmd->eh_eflags = 0;
418 shost->host_failed--;
419
420 switch (res) {
421 case TASK_IS_DONE:
422 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
423 task);
424 task->task_done(task);
425 continue;
426 case TASK_IS_ABORTED:
427 SAS_DPRINTK("%s: task 0x%p is aborted\n",
428 __FUNCTION__, task);
429 task->task_done(task);
430 continue;
431 case TASK_IS_AT_LU:
432 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
433 tmf_resp = sas_recover_lu(task->dev, cmd);
434 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
435 SAS_DPRINTK("dev %016llx LU %x is "
436 "recovered\n",
437 SAS_ADDR(task->dev),
438 cmd->device->lun);
439 task->task_done(task);
440 sas_scsi_clear_queue_lu(&error_q, cmd);
441 goto Again;
442 }
443 /* fallthrough */
444 case TASK_IS_NOT_AT_LU:
445 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
446 task);
447 tmf_resp = sas_recover_I_T(task->dev);
448 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
449 SAS_DPRINTK("I_T %016llx recovered\n",
450 SAS_ADDR(task->dev->sas_addr));
451 task->task_done(task);
452 sas_scsi_clear_queue_I_T(&error_q, task->dev);
453 goto Again;
454 }
455 /* Hammer time :-) */
456 if (i->dft->lldd_clear_nexus_port) {
457 struct asd_sas_port *port = task->dev->port;
458 SAS_DPRINTK("clearing nexus for port:%d\n",
459 port->id);
460 res = i->dft->lldd_clear_nexus_port(port);
461 if (res == TMF_RESP_FUNC_COMPLETE) {
462 SAS_DPRINTK("clear nexus port:%d "
463 "succeeded\n", port->id);
464 task->task_done(task);
465 sas_scsi_clear_queue_port(&error_q,
466 port);
467 goto Again;
468 }
469 }
470 if (i->dft->lldd_clear_nexus_ha) {
471 SAS_DPRINTK("clear nexus ha\n");
472 res = i->dft->lldd_clear_nexus_ha(ha);
473 if (res == TMF_RESP_FUNC_COMPLETE) {
474 SAS_DPRINTK("clear nexus ha "
475 "succeeded\n");
476 task->task_done(task);
477 goto out;
478 }
479 }
480 /* If we are here -- this means that no amount
481 * of effort could recover from errors. Quite
482 * possibly the HA just disappeared.
483 */
484 SAS_DPRINTK("error from device %llx, LUN %x "
485 "couldn't be recovered in any way\n",
486 SAS_ADDR(task->dev->sas_addr),
487 cmd->device->lun);
488
489 task->task_done(task);
490 goto clear_q;
491 }
492 }
493out:
494 SAS_DPRINTK("--- Exit %s\n", __FUNCTION__);
495 return;
496clear_q:
497 SAS_DPRINTK("--- Exit %s -- clear_q\n", __FUNCTION__);
498 list_for_each_entry_safe(cmd, n, &error_q, eh_entry) {
499 struct sas_task *task = TO_SAS_TASK(cmd);
500 list_del_init(&cmd->eh_entry);
501 task->task_done(task);
502 }
503}
504
505enum scsi_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
506{
507 struct sas_task *task = TO_SAS_TASK(cmd);
508 unsigned long flags;
509
510 if (!task) {
511 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_HANDLED\n",
512 cmd, task);
513 return EH_HANDLED;
514 }
515
516 spin_lock_irqsave(&task->task_state_lock, flags);
517 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
518 spin_unlock_irqrestore(&task->task_state_lock, flags);
519 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_HANDLED\n",
520 cmd, task);
521 return EH_HANDLED;
522 }
523 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
524 spin_unlock_irqrestore(&task->task_state_lock, flags);
525
526 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_NOT_HANDLED\n",
527 cmd, task);
528
529 return EH_NOT_HANDLED;
530}
531
532struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
533{
534 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
535 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
536 struct domain_device *found_dev = NULL;
537 int i;
538
539 spin_lock(&ha->phy_port_lock);
540 for (i = 0; i < ha->num_phys; i++) {
541 struct asd_sas_port *port = ha->sas_port[i];
542 struct domain_device *dev;
543
544 spin_lock(&port->dev_list_lock);
545 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
546 if (rphy == dev->rphy) {
547 found_dev = dev;
548 spin_unlock(&port->dev_list_lock);
549 goto found;
550 }
551 }
552 spin_unlock(&port->dev_list_lock);
553 }
554 found:
555 spin_unlock(&ha->phy_port_lock);
556
557 return found_dev;
558}
559
560static inline struct domain_device *sas_find_target(struct scsi_target *starget)
561{
562 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
563
564 return sas_find_dev_by_rphy(rphy);
565}
566
567int sas_target_alloc(struct scsi_target *starget)
568{
569 struct domain_device *found_dev = sas_find_target(starget);
570
571 if (!found_dev)
572 return -ENODEV;
573
574 starget->hostdata = found_dev;
575 return 0;
576}
577
578#define SAS_DEF_QD 32
579#define SAS_MAX_QD 64
580
581int sas_slave_configure(struct scsi_device *scsi_dev)
582{
583 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
584 struct sas_ha_struct *sas_ha;
585
586 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
587
588 sas_ha = dev->port->ha;
589
590 sas_read_port_mode_page(scsi_dev);
591
592 if (scsi_dev->tagged_supported) {
593 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
594 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
595 } else {
596 SAS_DPRINTK("device %llx, LUN %x doesn't support "
597 "TCQ\n", SAS_ADDR(dev->sas_addr),
598 scsi_dev->lun);
599 scsi_dev->tagged_supported = 0;
600 scsi_set_tag_type(scsi_dev, 0);
601 scsi_deactivate_tcq(scsi_dev, 1);
602 }
603
604 return 0;
605}
606
607void sas_slave_destroy(struct scsi_device *scsi_dev)
608{
609}
610
611int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth)
612{
613 int res = min(new_depth, SAS_MAX_QD);
614
615 if (scsi_dev->tagged_supported)
616 scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev),
617 res);
618 else {
619 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
620 sas_printk("device %llx LUN %x queue depth changed to 1\n",
621 SAS_ADDR(dev->sas_addr),
622 scsi_dev->lun);
623 scsi_adjust_queue_depth(scsi_dev, 0, 1);
624 res = 1;
625 }
626
627 return res;
628}
629
630int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
631{
632 if (!scsi_dev->tagged_supported)
633 return 0;
634
635 scsi_deactivate_tcq(scsi_dev, 1);
636
637 scsi_set_tag_type(scsi_dev, qt);
638 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
639
640 return qt;
641}
642
643int sas_bios_param(struct scsi_device *scsi_dev,
644 struct block_device *bdev,
645 sector_t capacity, int *hsc)
646{
647 hsc[0] = 255;
648 hsc[1] = 63;
649 sector_div(capacity, 255*63);
650 hsc[2] = capacity;
651
652 return 0;
653}
654
655/* ---------- Task Collector Thread implementation ---------- */
656
657static void sas_queue(struct sas_ha_struct *sas_ha)
658{
659 struct scsi_core *core = &sas_ha->core;
660 unsigned long flags;
661 LIST_HEAD(q);
662 int can_queue;
663 int res;
664 struct sas_internal *i = to_sas_internal(core->shost->transportt);
665
666 spin_lock_irqsave(&core->task_queue_lock, flags);
667 while (!core->queue_thread_kill &&
668 !list_empty(&core->task_queue)) {
669
670 can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
671 if (can_queue >= 0) {
672 can_queue = core->task_queue_size;
673 list_splice_init(&core->task_queue, &q);
674 } else {
675 struct list_head *a, *n;
676
677 can_queue = sas_ha->lldd_queue_size;
678 list_for_each_safe(a, n, &core->task_queue) {
679 list_move_tail(a, &q);
680 if (--can_queue == 0)
681 break;
682 }
683 can_queue = sas_ha->lldd_queue_size;
684 }
685 core->task_queue_size -= can_queue;
686 spin_unlock_irqrestore(&core->task_queue_lock, flags);
687 {
688 struct sas_task *task = list_entry(q.next,
689 struct sas_task,
690 list);
691 list_del_init(&q);
692 res = i->dft->lldd_execute_task(task, can_queue,
693 GFP_KERNEL);
694 if (unlikely(res))
695 __list_add(&q, task->list.prev, &task->list);
696 }
697 spin_lock_irqsave(&core->task_queue_lock, flags);
698 if (res) {
699 list_splice_init(&q, &core->task_queue); /*at head*/
700 core->task_queue_size += can_queue;
701 }
702 }
703 spin_unlock_irqrestore(&core->task_queue_lock, flags);
704}
705
706static DECLARE_COMPLETION(queue_th_comp);
707
708/**
709 * sas_queue_thread -- The Task Collector thread
710 * @_sas_ha: pointer to struct sas_ha
711 */
712static int sas_queue_thread(void *_sas_ha)
713{
714 struct sas_ha_struct *sas_ha = _sas_ha;
715 struct scsi_core *core = &sas_ha->core;
716
717 daemonize("sas_queue_%d", core->shost->host_no);
718 current->flags |= PF_NOFREEZE;
719
720 complete(&queue_th_comp);
721
722 while (1) {
723 down_interruptible(&core->queue_thread_sema);
724 sas_queue(sas_ha);
725 if (core->queue_thread_kill)
726 break;
727 }
728
729 complete(&queue_th_comp);
730
731 return 0;
732}
733
734int sas_init_queue(struct sas_ha_struct *sas_ha)
735{
736 int res;
737 struct scsi_core *core = &sas_ha->core;
738
739 spin_lock_init(&core->task_queue_lock);
740 core->task_queue_size = 0;
741 INIT_LIST_HEAD(&core->task_queue);
742 init_MUTEX_LOCKED(&core->queue_thread_sema);
743
744 res = kernel_thread(sas_queue_thread, sas_ha, 0);
745 if (res >= 0)
746 wait_for_completion(&queue_th_comp);
747
748 return res < 0 ? res : 0;
749}
750
751void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
752{
753 unsigned long flags;
754 struct scsi_core *core = &sas_ha->core;
755 struct sas_task *task, *n;
756
757 init_completion(&queue_th_comp);
758 core->queue_thread_kill = 1;
759 up(&core->queue_thread_sema);
760 wait_for_completion(&queue_th_comp);
761
762 if (!list_empty(&core->task_queue))
763 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
764 SAS_ADDR(sas_ha->sas_addr));
765
766 spin_lock_irqsave(&core->task_queue_lock, flags);
767 list_for_each_entry_safe(task, n, &core->task_queue, list) {
768 struct scsi_cmnd *cmd = task->uldd_task;
769
770 list_del_init(&task->list);
771
772 ASSIGN_SAS_TASK(cmd, NULL);
773 sas_free_task(task);
774 cmd->result = DID_ABORT << 16;
775 cmd->scsi_done(cmd);
776 }
777 spin_unlock_irqrestore(&core->task_queue_lock, flags);
778}
779
780EXPORT_SYMBOL_GPL(sas_queuecommand);
781EXPORT_SYMBOL_GPL(sas_target_alloc);
782EXPORT_SYMBOL_GPL(sas_slave_configure);
783EXPORT_SYMBOL_GPL(sas_slave_destroy);
784EXPORT_SYMBOL_GPL(sas_change_queue_depth);
785EXPORT_SYMBOL_GPL(sas_change_queue_type);
786EXPORT_SYMBOL_GPL(sas_bios_param);