blob: 6fb1f3afd1e0dcfc736cb2d3db228a6b5b85aa0b [file] [log] [blame]
James Bottomley2908d772006-08-29 09:22:51 -05001/*
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
James Bottomley2908d772006-08-29 09:22:51 -050025#include <linux/scatterlist.h>
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +090026#include <linux/blkdev.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
James Bottomley2908d772006-08-29 09:22:51 -050028
29#include "sas_internal.h"
30
Dan Williamsb52df412011-11-30 23:23:33 -080031#include <scsi/sas_ata.h>
James Bottomley2908d772006-08-29 09:22:51 -050032#include <scsi/scsi_transport.h>
33#include <scsi/scsi_transport_sas.h>
34#include "../scsi_sas_internal.h"
35
36static int sas_discover_expander(struct domain_device *dev);
37static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr);
38static int sas_configure_phy(struct domain_device *dev, int phy_id,
39 u8 *sas_addr, int include);
40static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr);
41
James Bottomley2908d772006-08-29 09:22:51 -050042/* ---------- SMP task management ---------- */
43
44static void smp_task_timedout(unsigned long _task)
45{
46 struct sas_task *task = (void *) _task;
47 unsigned long flags;
48
49 spin_lock_irqsave(&task->task_state_lock, flags);
50 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
51 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
52 spin_unlock_irqrestore(&task->task_state_lock, flags);
53
54 complete(&task->completion);
55}
56
57static void smp_task_done(struct sas_task *task)
58{
59 if (!del_timer(&task->timer))
60 return;
61 complete(&task->completion);
62}
63
64/* Give it some long enough timeout. In seconds. */
65#define SMP_TIMEOUT 10
66
67static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
68 void *resp, int resp_size)
69{
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070070 int res, retry;
71 struct sas_task *task = NULL;
James Bottomley2908d772006-08-29 09:22:51 -050072 struct sas_internal *i =
73 to_sas_internal(dev->port->ha->core.shost->transportt);
74
Jeff Skirvin89d3cf62011-11-16 09:44:13 +000075 mutex_lock(&dev->ex_dev.cmd_mutex);
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070076 for (retry = 0; retry < 3; retry++) {
Dan Williams3a9c5562011-12-21 15:19:56 -080077 if (test_bit(SAS_DEV_GONE, &dev->state)) {
78 res = -ECOMM;
79 break;
80 }
81
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070082 task = sas_alloc_task(GFP_KERNEL);
Jeff Skirvin89d3cf62011-11-16 09:44:13 +000083 if (!task) {
84 res = -ENOMEM;
85 break;
86 }
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070087 task->dev = dev;
88 task->task_proto = dev->tproto;
89 sg_init_one(&task->smp_task.smp_req, req, req_size);
90 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
James Bottomley2908d772006-08-29 09:22:51 -050091
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070092 task->task_done = smp_task_done;
James Bottomley2908d772006-08-29 09:22:51 -050093
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070094 task->timer.data = (unsigned long) task;
95 task->timer.function = smp_task_timedout;
96 task->timer.expires = jiffies + SMP_TIMEOUT*HZ;
97 add_timer(&task->timer);
James Bottomley2908d772006-08-29 09:22:51 -050098
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070099 res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
James Bottomley2908d772006-08-29 09:22:51 -0500100
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700101 if (res) {
102 del_timer(&task->timer);
103 SAS_DPRINTK("executing SMP task failed:%d\n", res);
Jeff Skirvin89d3cf62011-11-16 09:44:13 +0000104 break;
James Bottomley2908d772006-08-29 09:22:51 -0500105 }
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700106
107 wait_for_completion(&task->completion);
James Bottomley32e8ae32007-12-30 12:37:31 -0600108 res = -ECOMM;
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700109 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
110 SAS_DPRINTK("smp task timed out or aborted\n");
111 i->dft->lldd_abort_task(task);
112 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
113 SAS_DPRINTK("SMP task aborted and not done\n");
Jeff Skirvin89d3cf62011-11-16 09:44:13 +0000114 break;
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700115 }
116 }
117 if (task->task_status.resp == SAS_TASK_COMPLETE &&
James Bottomleydf64d3c2010-07-27 15:51:13 -0500118 task->task_status.stat == SAM_STAT_GOOD) {
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700119 res = 0;
120 break;
Jeff Skirvin89d3cf62011-11-16 09:44:13 +0000121 }
122 if (task->task_status.resp == SAS_TASK_COMPLETE &&
123 task->task_status.stat == SAS_DATA_UNDERRUN) {
James Bottomley2d4b63e2007-12-29 11:49:53 -0600124 /* no error, but return the number of bytes of
125 * underrun */
126 res = task->task_status.residual;
127 break;
Jeff Skirvin89d3cf62011-11-16 09:44:13 +0000128 }
129 if (task->task_status.resp == SAS_TASK_COMPLETE &&
130 task->task_status.stat == SAS_DATA_OVERRUN) {
James Bottomley2d4b63e2007-12-29 11:49:53 -0600131 res = -EMSGSIZE;
132 break;
Dan Williams36a39942011-11-17 17:59:54 -0800133 }
134 if (task->task_status.resp == SAS_TASK_UNDELIVERED &&
135 task->task_status.stat == SAS_DEVICE_UNKNOWN)
136 break;
137 else {
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700138 SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700139 "status 0x%x\n", __func__,
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700140 SAS_ADDR(dev->sas_addr),
141 task->task_status.resp,
142 task->task_status.stat);
143 sas_free_task(task);
144 task = NULL;
145 }
James Bottomley2908d772006-08-29 09:22:51 -0500146 }
Jeff Skirvin89d3cf62011-11-16 09:44:13 +0000147 mutex_unlock(&dev->ex_dev.cmd_mutex);
148
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700149 BUG_ON(retry == 3 && task != NULL);
Jeff Skirvin89d3cf62011-11-16 09:44:13 +0000150 sas_free_task(task);
James Bottomley2908d772006-08-29 09:22:51 -0500151 return res;
152}
153
154/* ---------- Allocations ---------- */
155
156static inline void *alloc_smp_req(int size)
157{
158 u8 *p = kzalloc(size, GFP_KERNEL);
159 if (p)
160 p[0] = SMP_REQUEST;
161 return p;
162}
163
164static inline void *alloc_smp_resp(int size)
165{
166 return kzalloc(size, GFP_KERNEL);
167}
168
169/* ---------- Expander configuration ---------- */
170
171static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
172 void *disc_resp)
173{
174 struct expander_device *ex = &dev->ex_dev;
175 struct ex_phy *phy = &ex->ex_phy[phy_id];
176 struct smp_resp *resp = disc_resp;
177 struct discover_resp *dr = &resp->disc;
178 struct sas_rphy *rphy = dev->rphy;
179 int rediscover = (phy->phy != NULL);
180
181 if (!rediscover) {
182 phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
183
184 /* FIXME: error_handling */
185 BUG_ON(!phy->phy);
186 }
187
188 switch (resp->result) {
189 case SMP_RESP_PHY_VACANT:
190 phy->phy_state = PHY_VACANT;
Jack Wang2bc72c92010-10-06 16:05:35 +0800191 break;
James Bottomley2908d772006-08-29 09:22:51 -0500192 default:
193 phy->phy_state = PHY_NOT_PRESENT;
Jack Wang2bc72c92010-10-06 16:05:35 +0800194 break;
James Bottomley2908d772006-08-29 09:22:51 -0500195 case SMP_RESP_FUNC_ACC:
196 phy->phy_state = PHY_EMPTY; /* do not know yet */
197 break;
198 }
199
200 phy->phy_id = phy_id;
201 phy->attached_dev_type = dr->attached_dev_type;
202 phy->linkrate = dr->linkrate;
203 phy->attached_sata_host = dr->attached_sata_host;
204 phy->attached_sata_dev = dr->attached_sata_dev;
205 phy->attached_sata_ps = dr->attached_sata_ps;
206 phy->attached_iproto = dr->iproto << 1;
207 phy->attached_tproto = dr->tproto << 1;
208 memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
209 phy->attached_phy_id = dr->attached_phy_id;
210 phy->phy_change_count = dr->change_count;
211 phy->routing_attr = dr->routing_attr;
212 phy->virtual = dr->virtual;
213 phy->last_da_index = -1;
214
Jack Wangbb041a02011-09-23 14:32:32 +0800215 phy->phy->identify.sas_address = SAS_ADDR(phy->attached_sas_addr);
216 phy->phy->identify.device_type = phy->attached_dev_type;
James Bottomley2908d772006-08-29 09:22:51 -0500217 phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
218 phy->phy->identify.target_port_protocols = phy->attached_tproto;
219 phy->phy->identify.phy_identifier = phy_id;
James Bottomleya01e70e2006-09-06 19:28:07 -0500220 phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
221 phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
222 phy->phy->minimum_linkrate = dr->pmin_linkrate;
223 phy->phy->maximum_linkrate = dr->pmax_linkrate;
James Bottomley88edf742006-09-06 17:36:13 -0500224 phy->phy->negotiated_linkrate = phy->linkrate;
James Bottomley2908d772006-08-29 09:22:51 -0500225
226 if (!rediscover)
Jack Wang2bc72c92010-10-06 16:05:35 +0800227 if (sas_phy_add(phy->phy)) {
228 sas_phy_free(phy->phy);
229 return;
230 }
James Bottomley2908d772006-08-29 09:22:51 -0500231
232 SAS_DPRINTK("ex %016llx phy%02d:%c attached: %016llx\n",
233 SAS_ADDR(dev->sas_addr), phy->phy_id,
234 phy->routing_attr == TABLE_ROUTING ? 'T' :
235 phy->routing_attr == DIRECT_ROUTING ? 'D' :
236 phy->routing_attr == SUBTRACTIVE_ROUTING ? 'S' : '?',
237 SAS_ADDR(phy->attached_sas_addr));
238
239 return;
240}
241
Dan Williamsb52df412011-11-30 23:23:33 -0800242/* check if we have an existing attached ata device on this expander phy */
Dan Williams81c757b2011-12-02 16:07:01 -0800243struct domain_device *sas_ex_to_ata(struct domain_device *ex_dev, int phy_id)
Dan Williamsb52df412011-11-30 23:23:33 -0800244{
245 struct ex_phy *ex_phy = &ex_dev->ex_dev.ex_phy[phy_id];
246 struct domain_device *dev;
247 struct sas_rphy *rphy;
248
249 if (!ex_phy->port)
250 return NULL;
251
252 rphy = ex_phy->port->rphy;
253 if (!rphy)
254 return NULL;
255
256 dev = sas_find_dev_by_rphy(rphy);
257
258 if (dev && dev_is_sata(dev))
259 return dev;
260
261 return NULL;
262}
263
James Bottomley2908d772006-08-29 09:22:51 -0500264#define DISCOVER_REQ_SIZE 16
265#define DISCOVER_RESP_SIZE 56
266
James Bottomley1acce192006-08-22 12:39:19 -0500267static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
268 u8 *disc_resp, int single)
269{
Dan Williamsb52df412011-11-30 23:23:33 -0800270 struct domain_device *ata_dev = sas_ex_to_ata(dev, single);
James Bottomley1acce192006-08-22 12:39:19 -0500271 int i, res;
272
273 disc_req[9] = single;
274 for (i = 1 ; i < 3; i++) {
275 struct discover_resp *dr;
276
277 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
278 disc_resp, DISCOVER_RESP_SIZE);
279 if (res)
280 return res;
James Bottomley1acce192006-08-22 12:39:19 -0500281 dr = &((struct smp_resp *)disc_resp)->disc;
jack_wang183ce892011-02-19 18:20:53 +0800282 if (memcmp(dev->sas_addr, dr->attached_sas_addr,
283 SAS_ADDR_SIZE) == 0) {
284 sas_printk("Found loopback topology, just ignore it!\n");
285 return 0;
286 }
Dan Williamsb52df412011-11-30 23:23:33 -0800287
288 /* This is detecting a failure to transmit initial
289 * dev to host FIS as described in section J.5 of
290 * sas-2 r16
291 */
James Bottomley1acce192006-08-22 12:39:19 -0500292 if (!(dr->attached_dev_type == 0 &&
293 dr->attached_sata_dev))
294 break;
Dan Williamsb52df412011-11-30 23:23:33 -0800295
296 /* In order to generate the dev to host FIS, we send a
297 * link reset to the expander port. If a device was
298 * previously detected on this port we ask libata to
299 * manage the reset and link recovery.
300 */
301 if (ata_dev) {
302 sas_ata_schedule_reset(ata_dev);
303 break;
304 }
James Bottomley38e2f032006-09-07 15:52:09 -0500305 sas_smp_phy_control(dev, single, PHY_FUNC_LINK_RESET, NULL);
James Bottomley1acce192006-08-22 12:39:19 -0500306 /* Wait for the reset to trigger the negotiation */
307 msleep(500);
308 }
309 sas_set_ex_phy(dev, single, disc_resp);
310 return 0;
311}
312
James Bottomley2908d772006-08-29 09:22:51 -0500313static int sas_ex_phy_discover(struct domain_device *dev, int single)
314{
315 struct expander_device *ex = &dev->ex_dev;
316 int res = 0;
317 u8 *disc_req;
318 u8 *disc_resp;
319
320 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
321 if (!disc_req)
322 return -ENOMEM;
323
324 disc_resp = alloc_smp_req(DISCOVER_RESP_SIZE);
325 if (!disc_resp) {
326 kfree(disc_req);
327 return -ENOMEM;
328 }
329
330 disc_req[1] = SMP_DISCOVER;
331
332 if (0 <= single && single < ex->num_phys) {
James Bottomley1acce192006-08-22 12:39:19 -0500333 res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
James Bottomley2908d772006-08-29 09:22:51 -0500334 } else {
335 int i;
336
337 for (i = 0; i < ex->num_phys; i++) {
James Bottomley1acce192006-08-22 12:39:19 -0500338 res = sas_ex_phy_discover_helper(dev, disc_req,
339 disc_resp, i);
James Bottomley2908d772006-08-29 09:22:51 -0500340 if (res)
341 goto out_err;
James Bottomley2908d772006-08-29 09:22:51 -0500342 }
343 }
344out_err:
345 kfree(disc_resp);
346 kfree(disc_req);
347 return res;
348}
349
350static int sas_expander_discover(struct domain_device *dev)
351{
352 struct expander_device *ex = &dev->ex_dev;
353 int res = -ENOMEM;
354
355 ex->ex_phy = kzalloc(sizeof(*ex->ex_phy)*ex->num_phys, GFP_KERNEL);
356 if (!ex->ex_phy)
357 return -ENOMEM;
358
359 res = sas_ex_phy_discover(dev, -1);
360 if (res)
361 goto out_err;
362
363 return 0;
364 out_err:
365 kfree(ex->ex_phy);
366 ex->ex_phy = NULL;
367 return res;
368}
369
370#define MAX_EXPANDER_PHYS 128
371
372static void ex_assign_report_general(struct domain_device *dev,
373 struct smp_resp *resp)
374{
375 struct report_general_resp *rg = &resp->rg;
376
377 dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
378 dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
379 dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
Luben Tuikovffaac8f2011-09-22 09:41:36 -0700380 dev->ex_dev.t2t_supp = rg->t2t_supp;
James Bottomley2908d772006-08-29 09:22:51 -0500381 dev->ex_dev.conf_route_table = rg->conf_route_table;
382 dev->ex_dev.configuring = rg->configuring;
383 memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
384}
385
386#define RG_REQ_SIZE 8
387#define RG_RESP_SIZE 32
388
389static int sas_ex_general(struct domain_device *dev)
390{
391 u8 *rg_req;
392 struct smp_resp *rg_resp;
393 int res;
394 int i;
395
396 rg_req = alloc_smp_req(RG_REQ_SIZE);
397 if (!rg_req)
398 return -ENOMEM;
399
400 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
401 if (!rg_resp) {
402 kfree(rg_req);
403 return -ENOMEM;
404 }
405
406 rg_req[1] = SMP_REPORT_GENERAL;
407
408 for (i = 0; i < 5; i++) {
409 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
410 RG_RESP_SIZE);
411
412 if (res) {
413 SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
414 SAS_ADDR(dev->sas_addr), res);
415 goto out;
416 } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
417 SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
418 SAS_ADDR(dev->sas_addr), rg_resp->result);
419 res = rg_resp->result;
420 goto out;
421 }
422
423 ex_assign_report_general(dev, rg_resp);
424
425 if (dev->ex_dev.configuring) {
426 SAS_DPRINTK("RG: ex %llx self-configuring...\n",
427 SAS_ADDR(dev->sas_addr));
428 schedule_timeout_interruptible(5*HZ);
429 } else
430 break;
431 }
432out:
433 kfree(rg_req);
434 kfree(rg_resp);
435 return res;
436}
437
438static void ex_assign_manuf_info(struct domain_device *dev, void
439 *_mi_resp)
440{
441 u8 *mi_resp = _mi_resp;
442 struct sas_rphy *rphy = dev->rphy;
443 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
444
445 memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
446 memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
447 memcpy(edev->product_rev, mi_resp + 36,
448 SAS_EXPANDER_PRODUCT_REV_LEN);
449
450 if (mi_resp[8] & 1) {
451 memcpy(edev->component_vendor_id, mi_resp + 40,
452 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
453 edev->component_id = mi_resp[48] << 8 | mi_resp[49];
454 edev->component_revision_id = mi_resp[50];
455 }
456}
457
458#define MI_REQ_SIZE 8
459#define MI_RESP_SIZE 64
460
461static int sas_ex_manuf_info(struct domain_device *dev)
462{
463 u8 *mi_req;
464 u8 *mi_resp;
465 int res;
466
467 mi_req = alloc_smp_req(MI_REQ_SIZE);
468 if (!mi_req)
469 return -ENOMEM;
470
471 mi_resp = alloc_smp_resp(MI_RESP_SIZE);
472 if (!mi_resp) {
473 kfree(mi_req);
474 return -ENOMEM;
475 }
476
477 mi_req[1] = SMP_REPORT_MANUF_INFO;
478
479 res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
480 if (res) {
481 SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
482 SAS_ADDR(dev->sas_addr), res);
483 goto out;
484 } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
485 SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
486 SAS_ADDR(dev->sas_addr), mi_resp[2]);
487 goto out;
488 }
489
490 ex_assign_manuf_info(dev, mi_resp);
491out:
492 kfree(mi_req);
493 kfree(mi_resp);
494 return res;
495}
496
497#define PC_REQ_SIZE 44
498#define PC_RESP_SIZE 8
499
500int sas_smp_phy_control(struct domain_device *dev, int phy_id,
James Bottomleya01e70e2006-09-06 19:28:07 -0500501 enum phy_func phy_func,
502 struct sas_phy_linkrates *rates)
James Bottomley2908d772006-08-29 09:22:51 -0500503{
504 u8 *pc_req;
505 u8 *pc_resp;
506 int res;
507
508 pc_req = alloc_smp_req(PC_REQ_SIZE);
509 if (!pc_req)
510 return -ENOMEM;
511
512 pc_resp = alloc_smp_resp(PC_RESP_SIZE);
513 if (!pc_resp) {
514 kfree(pc_req);
515 return -ENOMEM;
516 }
517
518 pc_req[1] = SMP_PHY_CONTROL;
519 pc_req[9] = phy_id;
520 pc_req[10]= phy_func;
James Bottomleya01e70e2006-09-06 19:28:07 -0500521 if (rates) {
522 pc_req[32] = rates->minimum_linkrate << 4;
523 pc_req[33] = rates->maximum_linkrate << 4;
524 }
James Bottomley2908d772006-08-29 09:22:51 -0500525
526 res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
527
528 kfree(pc_resp);
529 kfree(pc_req);
530 return res;
531}
532
533static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
534{
535 struct expander_device *ex = &dev->ex_dev;
536 struct ex_phy *phy = &ex->ex_phy[phy_id];
537
James Bottomleya01e70e2006-09-06 19:28:07 -0500538 sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
James Bottomley88edf742006-09-06 17:36:13 -0500539 phy->linkrate = SAS_PHY_DISABLED;
James Bottomley2908d772006-08-29 09:22:51 -0500540}
541
542static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
543{
544 struct expander_device *ex = &dev->ex_dev;
545 int i;
546
547 for (i = 0; i < ex->num_phys; i++) {
548 struct ex_phy *phy = &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) == SAS_ADDR(sas_addr))
555 sas_ex_disable_phy(dev, i);
556 }
557}
558
559static int sas_dev_present_in_domain(struct asd_sas_port *port,
560 u8 *sas_addr)
561{
562 struct domain_device *dev;
563
564 if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
565 return 1;
566 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
567 if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
568 return 1;
569 }
570 return 0;
571}
572
573#define RPEL_REQ_SIZE 16
574#define RPEL_RESP_SIZE 32
575int sas_smp_get_phy_events(struct sas_phy *phy)
576{
577 int res;
Jesper Juhl92631fa2007-07-28 01:13:33 +0200578 u8 *req;
579 u8 *resp;
James Bottomley2908d772006-08-29 09:22:51 -0500580 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
581 struct domain_device *dev = sas_find_dev_by_rphy(rphy);
James Bottomley2908d772006-08-29 09:22:51 -0500582
Jesper Juhl92631fa2007-07-28 01:13:33 +0200583 req = alloc_smp_req(RPEL_REQ_SIZE);
584 if (!req)
James Bottomley2908d772006-08-29 09:22:51 -0500585 return -ENOMEM;
586
Jesper Juhl92631fa2007-07-28 01:13:33 +0200587 resp = alloc_smp_resp(RPEL_RESP_SIZE);
588 if (!resp) {
589 kfree(req);
590 return -ENOMEM;
591 }
592
James Bottomley2908d772006-08-29 09:22:51 -0500593 req[1] = SMP_REPORT_PHY_ERR_LOG;
594 req[9] = phy->number;
595
596 res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
597 resp, RPEL_RESP_SIZE);
598
599 if (!res)
600 goto out;
601
602 phy->invalid_dword_count = scsi_to_u32(&resp[12]);
603 phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
604 phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
605 phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
606
607 out:
608 kfree(resp);
609 return res;
610
611}
612
James Bottomleyb9142172007-07-22 13:15:55 -0500613#ifdef CONFIG_SCSI_SAS_ATA
614
James Bottomley2908d772006-08-29 09:22:51 -0500615#define RPS_REQ_SIZE 16
616#define RPS_RESP_SIZE 60
617
618static int sas_get_report_phy_sata(struct domain_device *dev,
619 int phy_id,
620 struct smp_resp *rps_resp)
621{
622 int res;
623 u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
James Bottomley1acce192006-08-22 12:39:19 -0500624 u8 *resp = (u8 *)rps_resp;
James Bottomley2908d772006-08-29 09:22:51 -0500625
626 if (!rps_req)
627 return -ENOMEM;
628
629 rps_req[1] = SMP_REPORT_PHY_SATA;
630 rps_req[9] = phy_id;
631
632 res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
633 rps_resp, RPS_RESP_SIZE);
634
James Bottomley1acce192006-08-22 12:39:19 -0500635 /* 0x34 is the FIS type for the D2H fis. There's a potential
636 * standards cockup here. sas-2 explicitly specifies the FIS
637 * should be encoded so that FIS type is in resp[24].
638 * However, some expanders endian reverse this. Undo the
639 * reversal here */
640 if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
641 int i;
642
643 for (i = 0; i < 5; i++) {
644 int j = 24 + (i*4);
645 u8 a, b;
646 a = resp[j + 0];
647 b = resp[j + 1];
648 resp[j + 0] = resp[j + 3];
649 resp[j + 1] = resp[j + 2];
650 resp[j + 2] = b;
651 resp[j + 3] = a;
652 }
653 }
654
James Bottomley2908d772006-08-29 09:22:51 -0500655 kfree(rps_req);
James Bottomley1acce192006-08-22 12:39:19 -0500656 return res;
James Bottomley2908d772006-08-29 09:22:51 -0500657}
James Bottomleyb9142172007-07-22 13:15:55 -0500658#endif
James Bottomley2908d772006-08-29 09:22:51 -0500659
660static void sas_ex_get_linkrate(struct domain_device *parent,
661 struct domain_device *child,
662 struct ex_phy *parent_phy)
663{
664 struct expander_device *parent_ex = &parent->ex_dev;
665 struct sas_port *port;
666 int i;
667
668 child->pathways = 0;
669
670 port = parent_phy->port;
671
672 for (i = 0; i < parent_ex->num_phys; i++) {
673 struct ex_phy *phy = &parent_ex->ex_phy[i];
674
675 if (phy->phy_state == PHY_VACANT ||
676 phy->phy_state == PHY_NOT_PRESENT)
677 continue;
678
679 if (SAS_ADDR(phy->attached_sas_addr) ==
680 SAS_ADDR(child->sas_addr)) {
681
682 child->min_linkrate = min(parent->min_linkrate,
683 phy->linkrate);
684 child->max_linkrate = max(parent->max_linkrate,
685 phy->linkrate);
686 child->pathways++;
687 sas_port_add_phy(port, phy->phy);
688 }
689 }
690 child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
691 child->pathways = min(child->pathways, parent->pathways);
692}
693
694static struct domain_device *sas_ex_discover_end_dev(
695 struct domain_device *parent, int phy_id)
696{
697 struct expander_device *parent_ex = &parent->ex_dev;
698 struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
699 struct domain_device *child = NULL;
700 struct sas_rphy *rphy;
701 int res;
702
703 if (phy->attached_sata_host || phy->attached_sata_ps)
704 return NULL;
705
Dan Williams735f7d22011-11-17 17:59:47 -0800706 child = sas_alloc_device();
James Bottomley2908d772006-08-29 09:22:51 -0500707 if (!child)
708 return NULL;
709
Dan Williams735f7d22011-11-17 17:59:47 -0800710 kref_get(&parent->kref);
James Bottomley2908d772006-08-29 09:22:51 -0500711 child->parent = parent;
712 child->port = parent->port;
713 child->iproto = phy->attached_iproto;
714 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
715 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
James Bottomley024879e2006-11-15 18:03:07 -0600716 if (!phy->port) {
717 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
718 if (unlikely(!phy->port))
719 goto out_err;
720 if (unlikely(sas_port_add(phy->port) != 0)) {
721 sas_port_free(phy->port);
722 goto out_err;
723 }
724 }
James Bottomley2908d772006-08-29 09:22:51 -0500725 sas_ex_get_linkrate(parent, child, phy);
726
James Bottomleyb9142172007-07-22 13:15:55 -0500727#ifdef CONFIG_SCSI_SAS_ATA
Darrick J. Wong5929faf2007-11-05 11:51:17 -0800728 if ((phy->attached_tproto & SAS_PROTOCOL_STP) || phy->attached_sata_dev) {
James Bottomley2908d772006-08-29 09:22:51 -0500729 child->dev_type = SATA_DEV;
Darrick J. Wong5929faf2007-11-05 11:51:17 -0800730 if (phy->attached_tproto & SAS_PROTOCOL_STP)
James Bottomley2908d772006-08-29 09:22:51 -0500731 child->tproto = phy->attached_tproto;
732 if (phy->attached_sata_dev)
733 child->tproto |= SATA_DEV;
734 res = sas_get_report_phy_sata(parent, phy_id,
735 &child->sata_dev.rps_resp);
736 if (res) {
737 SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
738 "0x%x\n", SAS_ADDR(parent->sas_addr),
739 phy_id, res);
James Bottomley024879e2006-11-15 18:03:07 -0600740 goto out_free;
James Bottomley2908d772006-08-29 09:22:51 -0500741 }
742 memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
743 sizeof(struct dev_to_host_fis));
James Bottomley1acce192006-08-22 12:39:19 -0500744
745 rphy = sas_end_device_alloc(phy->port);
James Bottomley528fd552006-10-16 10:57:05 -0500746 if (unlikely(!rphy))
747 goto out_free;
James Bottomley1acce192006-08-22 12:39:19 -0500748
James Bottomley2908d772006-08-29 09:22:51 -0500749 sas_init_dev(child);
James Bottomley1acce192006-08-22 12:39:19 -0500750
751 child->rphy = rphy;
752
Dan Williams87c83312011-11-17 17:59:51 -0800753 list_add_tail(&child->disco_list_node, &parent->port->disco_list);
James Bottomley1acce192006-08-22 12:39:19 -0500754
James Bottomley2908d772006-08-29 09:22:51 -0500755 res = sas_discover_sata(child);
756 if (res) {
757 SAS_DPRINTK("sas_discover_sata() for device %16llx at "
758 "%016llx:0x%x returned 0x%x\n",
759 SAS_ADDR(child->sas_addr),
760 SAS_ADDR(parent->sas_addr), phy_id, res);
James Bottomley1acce192006-08-22 12:39:19 -0500761 goto out_list_del;
James Bottomley2908d772006-08-29 09:22:51 -0500762 }
James Bottomleyb9142172007-07-22 13:15:55 -0500763 } else
764#endif
Darrick J. Wong5929faf2007-11-05 11:51:17 -0800765 if (phy->attached_tproto & SAS_PROTOCOL_SSP) {
James Bottomley2908d772006-08-29 09:22:51 -0500766 child->dev_type = SAS_END_DEV;
767 rphy = sas_end_device_alloc(phy->port);
768 /* FIXME: error handling */
James Bottomley024879e2006-11-15 18:03:07 -0600769 if (unlikely(!rphy))
770 goto out_free;
James Bottomley2908d772006-08-29 09:22:51 -0500771 child->tproto = phy->attached_tproto;
772 sas_init_dev(child);
773
774 child->rphy = rphy;
775 sas_fill_in_rphy(child, rphy);
776
James Bottomley9d720d82007-07-16 13:15:51 -0500777 spin_lock_irq(&parent->port->dev_list_lock);
James Bottomley2908d772006-08-29 09:22:51 -0500778 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
James Bottomley9d720d82007-07-16 13:15:51 -0500779 spin_unlock_irq(&parent->port->dev_list_lock);
James Bottomley2908d772006-08-29 09:22:51 -0500780
781 res = sas_discover_end_dev(child);
782 if (res) {
783 SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
784 "at %016llx:0x%x returned 0x%x\n",
785 SAS_ADDR(child->sas_addr),
786 SAS_ADDR(parent->sas_addr), phy_id, res);
James Bottomley024879e2006-11-15 18:03:07 -0600787 goto out_list_del;
James Bottomley2908d772006-08-29 09:22:51 -0500788 }
789 } else {
790 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
791 phy->attached_tproto, SAS_ADDR(parent->sas_addr),
792 phy_id);
James Bottomleyb9142172007-07-22 13:15:55 -0500793 goto out_free;
James Bottomley2908d772006-08-29 09:22:51 -0500794 }
795
796 list_add_tail(&child->siblings, &parent_ex->children);
797 return child;
James Bottomley024879e2006-11-15 18:03:07 -0600798
799 out_list_del:
Darrick J. Wong6f63caa2007-01-26 14:08:43 -0800800 sas_rphy_free(child->rphy);
801 child->rphy = NULL;
Dan Williams1a34c062011-09-21 22:05:34 -0700802
Dan Williams87c83312011-11-17 17:59:51 -0800803 list_del(&child->disco_list_node);
Dan Williams1a34c062011-09-21 22:05:34 -0700804 spin_lock_irq(&parent->port->dev_list_lock);
James Bottomley024879e2006-11-15 18:03:07 -0600805 list_del(&child->dev_list_node);
Dan Williams1a34c062011-09-21 22:05:34 -0700806 spin_unlock_irq(&parent->port->dev_list_lock);
James Bottomley024879e2006-11-15 18:03:07 -0600807 out_free:
808 sas_port_delete(phy->port);
809 out_err:
810 phy->port = NULL;
Dan Williams735f7d22011-11-17 17:59:47 -0800811 sas_put_device(child);
James Bottomley024879e2006-11-15 18:03:07 -0600812 return NULL;
James Bottomley2908d772006-08-29 09:22:51 -0500813}
814
Darrick J. Wong423f7cf2007-01-30 12:07:27 -0800815/* See if this phy is part of a wide port */
816static int sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
817{
818 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
819 int i;
820
821 for (i = 0; i < parent->ex_dev.num_phys; i++) {
822 struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
823
824 if (ephy == phy)
825 continue;
826
827 if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
828 SAS_ADDR_SIZE) && ephy->port) {
829 sas_port_add_phy(ephy->port, phy->phy);
Tom Peng19252de2009-07-17 16:02:04 +0800830 phy->port = ephy->port;
Darrick J. Wong423f7cf2007-01-30 12:07:27 -0800831 phy->phy_state = PHY_DEVICE_DISCOVERED;
832 return 0;
833 }
834 }
835
836 return -ENODEV;
837}
838
James Bottomley2908d772006-08-29 09:22:51 -0500839static struct domain_device *sas_ex_discover_expander(
840 struct domain_device *parent, int phy_id)
841{
842 struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
843 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
844 struct domain_device *child = NULL;
845 struct sas_rphy *rphy;
846 struct sas_expander_device *edev;
847 struct asd_sas_port *port;
848 int res;
849
850 if (phy->routing_attr == DIRECT_ROUTING) {
851 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
852 "allowed\n",
853 SAS_ADDR(parent->sas_addr), phy_id,
854 SAS_ADDR(phy->attached_sas_addr),
855 phy->attached_phy_id);
856 return NULL;
857 }
Dan Williams735f7d22011-11-17 17:59:47 -0800858 child = sas_alloc_device();
James Bottomley2908d772006-08-29 09:22:51 -0500859 if (!child)
860 return NULL;
861
862 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
863 /* FIXME: better error handling */
864 BUG_ON(sas_port_add(phy->port) != 0);
865
866
867 switch (phy->attached_dev_type) {
868 case EDGE_DEV:
869 rphy = sas_expander_alloc(phy->port,
870 SAS_EDGE_EXPANDER_DEVICE);
871 break;
872 case FANOUT_DEV:
873 rphy = sas_expander_alloc(phy->port,
874 SAS_FANOUT_EXPANDER_DEVICE);
875 break;
876 default:
877 rphy = NULL; /* shut gcc up */
878 BUG();
879 }
880 port = parent->port;
881 child->rphy = rphy;
882 edev = rphy_to_expander_device(rphy);
883 child->dev_type = phy->attached_dev_type;
Dan Williams735f7d22011-11-17 17:59:47 -0800884 kref_get(&parent->kref);
James Bottomley2908d772006-08-29 09:22:51 -0500885 child->parent = parent;
886 child->port = port;
887 child->iproto = phy->attached_iproto;
888 child->tproto = phy->attached_tproto;
889 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
890 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
891 sas_ex_get_linkrate(parent, child, phy);
892 edev->level = parent_ex->level + 1;
893 parent->port->disc.max_level = max(parent->port->disc.max_level,
894 edev->level);
895 sas_init_dev(child);
896 sas_fill_in_rphy(child, rphy);
897 sas_rphy_add(rphy);
898
James Bottomley9d720d82007-07-16 13:15:51 -0500899 spin_lock_irq(&parent->port->dev_list_lock);
James Bottomley2908d772006-08-29 09:22:51 -0500900 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
James Bottomley9d720d82007-07-16 13:15:51 -0500901 spin_unlock_irq(&parent->port->dev_list_lock);
James Bottomley2908d772006-08-29 09:22:51 -0500902
903 res = sas_discover_expander(child);
904 if (res) {
Luben Tuikov5911e962011-07-26 23:10:48 -0700905 spin_lock_irq(&parent->port->dev_list_lock);
906 list_del(&child->dev_list_node);
907 spin_unlock_irq(&parent->port->dev_list_lock);
Dan Williams735f7d22011-11-17 17:59:47 -0800908 sas_put_device(child);
James Bottomley2908d772006-08-29 09:22:51 -0500909 return NULL;
910 }
911 list_add_tail(&child->siblings, &parent->ex_dev.children);
912 return child;
913}
914
915static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
916{
917 struct expander_device *ex = &dev->ex_dev;
918 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
919 struct domain_device *child = NULL;
920 int res = 0;
921
922 /* Phy state */
James Bottomley88edf742006-09-06 17:36:13 -0500923 if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
James Bottomleya01e70e2006-09-06 19:28:07 -0500924 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
James Bottomley2908d772006-08-29 09:22:51 -0500925 res = sas_ex_phy_discover(dev, phy_id);
926 if (res)
927 return res;
928 }
929
930 /* Parent and domain coherency */
931 if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
932 SAS_ADDR(dev->port->sas_addr))) {
933 sas_add_parent_port(dev, phy_id);
934 return 0;
935 }
936 if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
937 SAS_ADDR(dev->parent->sas_addr))) {
938 sas_add_parent_port(dev, phy_id);
939 if (ex_phy->routing_attr == TABLE_ROUTING)
940 sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
941 return 0;
942 }
943
944 if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
945 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
946
947 if (ex_phy->attached_dev_type == NO_DEVICE) {
948 if (ex_phy->routing_attr == DIRECT_ROUTING) {
949 memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
950 sas_configure_routing(dev, ex_phy->attached_sas_addr);
951 }
952 return 0;
James Bottomley88edf742006-09-06 17:36:13 -0500953 } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
James Bottomley2908d772006-08-29 09:22:51 -0500954 return 0;
955
956 if (ex_phy->attached_dev_type != SAS_END_DEV &&
957 ex_phy->attached_dev_type != FANOUT_DEV &&
958 ex_phy->attached_dev_type != EDGE_DEV) {
959 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
960 "phy 0x%x\n", ex_phy->attached_dev_type,
961 SAS_ADDR(dev->sas_addr),
962 phy_id);
963 return 0;
964 }
965
966 res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
967 if (res) {
968 SAS_DPRINTK("configure routing for dev %016llx "
969 "reported 0x%x. Forgotten\n",
970 SAS_ADDR(ex_phy->attached_sas_addr), res);
971 sas_disable_routing(dev, ex_phy->attached_sas_addr);
972 return res;
973 }
974
Darrick J. Wong423f7cf2007-01-30 12:07:27 -0800975 res = sas_ex_join_wide_port(dev, phy_id);
976 if (!res) {
977 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
978 phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
979 return res;
980 }
981
James Bottomley2908d772006-08-29 09:22:51 -0500982 switch (ex_phy->attached_dev_type) {
983 case SAS_END_DEV:
984 child = sas_ex_discover_end_dev(dev, phy_id);
985 break;
986 case FANOUT_DEV:
987 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
988 SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
989 "attached to ex %016llx phy 0x%x\n",
990 SAS_ADDR(ex_phy->attached_sas_addr),
991 ex_phy->attached_phy_id,
992 SAS_ADDR(dev->sas_addr),
993 phy_id);
994 sas_ex_disable_phy(dev, phy_id);
995 break;
996 } else
997 memcpy(dev->port->disc.fanout_sas_addr,
998 ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
999 /* fallthrough */
1000 case EDGE_DEV:
1001 child = sas_ex_discover_expander(dev, phy_id);
1002 break;
1003 default:
1004 break;
1005 }
1006
1007 if (child) {
1008 int i;
1009
1010 for (i = 0; i < ex->num_phys; i++) {
1011 if (ex->ex_phy[i].phy_state == PHY_VACANT ||
1012 ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
1013 continue;
Tom Peng19252de2009-07-17 16:02:04 +08001014 /*
1015 * Due to races, the phy might not get added to the
1016 * wide port, so we add the phy to the wide port here.
1017 */
James Bottomley2908d772006-08-29 09:22:51 -05001018 if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
Tom Peng19252de2009-07-17 16:02:04 +08001019 SAS_ADDR(child->sas_addr)) {
James Bottomley2908d772006-08-29 09:22:51 -05001020 ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
Tom Peng19252de2009-07-17 16:02:04 +08001021 res = sas_ex_join_wide_port(dev, i);
1022 if (!res)
1023 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
1024 i, SAS_ADDR(ex->ex_phy[i].attached_sas_addr));
1025
1026 }
James Bottomley2908d772006-08-29 09:22:51 -05001027 }
1028 }
1029
1030 return res;
1031}
1032
1033static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
1034{
1035 struct expander_device *ex = &dev->ex_dev;
1036 int i;
1037
1038 for (i = 0; i < ex->num_phys; i++) {
1039 struct ex_phy *phy = &ex->ex_phy[i];
1040
1041 if (phy->phy_state == PHY_VACANT ||
1042 phy->phy_state == PHY_NOT_PRESENT)
1043 continue;
1044
1045 if ((phy->attached_dev_type == EDGE_DEV ||
1046 phy->attached_dev_type == FANOUT_DEV) &&
1047 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1048
1049 memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
1050
1051 return 1;
1052 }
1053 }
1054 return 0;
1055}
1056
1057static int sas_check_level_subtractive_boundary(struct domain_device *dev)
1058{
1059 struct expander_device *ex = &dev->ex_dev;
1060 struct domain_device *child;
1061 u8 sub_addr[8] = {0, };
1062
1063 list_for_each_entry(child, &ex->children, siblings) {
1064 if (child->dev_type != EDGE_DEV &&
1065 child->dev_type != FANOUT_DEV)
1066 continue;
1067 if (sub_addr[0] == 0) {
1068 sas_find_sub_addr(child, sub_addr);
1069 continue;
1070 } else {
1071 u8 s2[8];
1072
1073 if (sas_find_sub_addr(child, s2) &&
1074 (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
1075
1076 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
1077 "diverges from subtractive "
1078 "boundary %016llx\n",
1079 SAS_ADDR(dev->sas_addr),
1080 SAS_ADDR(child->sas_addr),
1081 SAS_ADDR(s2),
1082 SAS_ADDR(sub_addr));
1083
1084 sas_ex_disable_port(child, s2);
1085 }
1086 }
1087 }
1088 return 0;
1089}
1090/**
1091 * sas_ex_discover_devices -- discover devices attached to this expander
1092 * dev: pointer to the expander domain device
1093 * single: if you want to do a single phy, else set to -1;
1094 *
1095 * Configure this expander for use with its devices and register the
1096 * devices of this expander.
1097 */
1098static int sas_ex_discover_devices(struct domain_device *dev, int single)
1099{
1100 struct expander_device *ex = &dev->ex_dev;
1101 int i = 0, end = ex->num_phys;
1102 int res = 0;
1103
1104 if (0 <= single && single < end) {
1105 i = single;
1106 end = i+1;
1107 }
1108
1109 for ( ; i < end; i++) {
1110 struct ex_phy *ex_phy = &ex->ex_phy[i];
1111
1112 if (ex_phy->phy_state == PHY_VACANT ||
1113 ex_phy->phy_state == PHY_NOT_PRESENT ||
1114 ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
1115 continue;
1116
1117 switch (ex_phy->linkrate) {
James Bottomley88edf742006-09-06 17:36:13 -05001118 case SAS_PHY_DISABLED:
1119 case SAS_PHY_RESET_PROBLEM:
1120 case SAS_SATA_PORT_SELECTOR:
James Bottomley2908d772006-08-29 09:22:51 -05001121 continue;
1122 default:
1123 res = sas_ex_discover_dev(dev, i);
1124 if (res)
1125 break;
1126 continue;
1127 }
1128 }
1129
1130 if (!res)
1131 sas_check_level_subtractive_boundary(dev);
1132
1133 return res;
1134}
1135
1136static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
1137{
1138 struct expander_device *ex = &dev->ex_dev;
1139 int i;
1140 u8 *sub_sas_addr = NULL;
1141
1142 if (dev->dev_type != EDGE_DEV)
1143 return 0;
1144
1145 for (i = 0; i < ex->num_phys; i++) {
1146 struct ex_phy *phy = &ex->ex_phy[i];
1147
1148 if (phy->phy_state == PHY_VACANT ||
1149 phy->phy_state == PHY_NOT_PRESENT)
1150 continue;
1151
1152 if ((phy->attached_dev_type == FANOUT_DEV ||
1153 phy->attached_dev_type == EDGE_DEV) &&
1154 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1155
1156 if (!sub_sas_addr)
1157 sub_sas_addr = &phy->attached_sas_addr[0];
1158 else if (SAS_ADDR(sub_sas_addr) !=
1159 SAS_ADDR(phy->attached_sas_addr)) {
1160
1161 SAS_DPRINTK("ex %016llx phy 0x%x "
1162 "diverges(%016llx) on subtractive "
1163 "boundary(%016llx). Disabled\n",
1164 SAS_ADDR(dev->sas_addr), i,
1165 SAS_ADDR(phy->attached_sas_addr),
1166 SAS_ADDR(sub_sas_addr));
1167 sas_ex_disable_phy(dev, i);
1168 }
1169 }
1170 }
1171 return 0;
1172}
1173
1174static void sas_print_parent_topology_bug(struct domain_device *child,
1175 struct ex_phy *parent_phy,
1176 struct ex_phy *child_phy)
1177{
1178 static const char ra_char[] = {
1179 [DIRECT_ROUTING] = 'D',
1180 [SUBTRACTIVE_ROUTING] = 'S',
1181 [TABLE_ROUTING] = 'T',
1182 };
1183 static const char *ex_type[] = {
1184 [EDGE_DEV] = "edge",
1185 [FANOUT_DEV] = "fanout",
1186 };
1187 struct domain_device *parent = child->parent;
1188
Luben Tuikovffaac8f2011-09-22 09:41:36 -07001189 sas_printk("%s ex %016llx (T2T supp:%d) phy 0x%x <--> %s ex %016llx "
1190 "(T2T supp:%d) phy 0x%x has %c:%c routing link!\n",
James Bottomley2908d772006-08-29 09:22:51 -05001191
1192 ex_type[parent->dev_type],
1193 SAS_ADDR(parent->sas_addr),
Luben Tuikovffaac8f2011-09-22 09:41:36 -07001194 parent->ex_dev.t2t_supp,
James Bottomley2908d772006-08-29 09:22:51 -05001195 parent_phy->phy_id,
1196
1197 ex_type[child->dev_type],
1198 SAS_ADDR(child->sas_addr),
Luben Tuikovffaac8f2011-09-22 09:41:36 -07001199 child->ex_dev.t2t_supp,
James Bottomley2908d772006-08-29 09:22:51 -05001200 child_phy->phy_id,
1201
1202 ra_char[parent_phy->routing_attr],
1203 ra_char[child_phy->routing_attr]);
1204}
1205
1206static int sas_check_eeds(struct domain_device *child,
1207 struct ex_phy *parent_phy,
1208 struct ex_phy *child_phy)
1209{
1210 int res = 0;
1211 struct domain_device *parent = child->parent;
1212
1213 if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1214 res = -ENODEV;
1215 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1216 "phy S:0x%x, while there is a fanout ex %016llx\n",
1217 SAS_ADDR(parent->sas_addr),
1218 parent_phy->phy_id,
1219 SAS_ADDR(child->sas_addr),
1220 child_phy->phy_id,
1221 SAS_ADDR(parent->port->disc.fanout_sas_addr));
1222 } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1223 memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1224 SAS_ADDR_SIZE);
1225 memcpy(parent->port->disc.eeds_b, child->sas_addr,
1226 SAS_ADDR_SIZE);
1227 } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1228 SAS_ADDR(parent->sas_addr)) ||
1229 (SAS_ADDR(parent->port->disc.eeds_a) ==
1230 SAS_ADDR(child->sas_addr)))
1231 &&
1232 ((SAS_ADDR(parent->port->disc.eeds_b) ==
1233 SAS_ADDR(parent->sas_addr)) ||
1234 (SAS_ADDR(parent->port->disc.eeds_b) ==
1235 SAS_ADDR(child->sas_addr))))
1236 ;
1237 else {
1238 res = -ENODEV;
1239 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1240 "phy 0x%x link forms a third EEDS!\n",
1241 SAS_ADDR(parent->sas_addr),
1242 parent_phy->phy_id,
1243 SAS_ADDR(child->sas_addr),
1244 child_phy->phy_id);
1245 }
1246
1247 return res;
1248}
1249
1250/* Here we spill over 80 columns. It is intentional.
1251 */
1252static int sas_check_parent_topology(struct domain_device *child)
1253{
1254 struct expander_device *child_ex = &child->ex_dev;
1255 struct expander_device *parent_ex;
1256 int i;
1257 int res = 0;
1258
1259 if (!child->parent)
1260 return 0;
1261
1262 if (child->parent->dev_type != EDGE_DEV &&
1263 child->parent->dev_type != FANOUT_DEV)
1264 return 0;
1265
1266 parent_ex = &child->parent->ex_dev;
1267
1268 for (i = 0; i < parent_ex->num_phys; i++) {
1269 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1270 struct ex_phy *child_phy;
1271
1272 if (parent_phy->phy_state == PHY_VACANT ||
1273 parent_phy->phy_state == PHY_NOT_PRESENT)
1274 continue;
1275
1276 if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1277 continue;
1278
1279 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1280
1281 switch (child->parent->dev_type) {
1282 case EDGE_DEV:
1283 if (child->dev_type == FANOUT_DEV) {
1284 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1285 child_phy->routing_attr != TABLE_ROUTING) {
1286 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1287 res = -ENODEV;
1288 }
1289 } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1290 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1291 res = sas_check_eeds(child, parent_phy, child_phy);
1292 } else if (child_phy->routing_attr != TABLE_ROUTING) {
1293 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1294 res = -ENODEV;
1295 }
Luben Tuikovffaac8f2011-09-22 09:41:36 -07001296 } else if (parent_phy->routing_attr == TABLE_ROUTING) {
1297 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING ||
1298 (child_phy->routing_attr == TABLE_ROUTING &&
1299 child_ex->t2t_supp && parent_ex->t2t_supp)) {
1300 /* All good */;
1301 } else {
1302 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1303 res = -ENODEV;
1304 }
James Bottomley2908d772006-08-29 09:22:51 -05001305 }
1306 break;
1307 case FANOUT_DEV:
1308 if (parent_phy->routing_attr != TABLE_ROUTING ||
1309 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1310 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1311 res = -ENODEV;
1312 }
1313 break;
1314 default:
1315 break;
1316 }
1317 }
1318
1319 return res;
1320}
1321
1322#define RRI_REQ_SIZE 16
1323#define RRI_RESP_SIZE 44
1324
1325static int sas_configure_present(struct domain_device *dev, int phy_id,
1326 u8 *sas_addr, int *index, int *present)
1327{
1328 int i, res = 0;
1329 struct expander_device *ex = &dev->ex_dev;
1330 struct ex_phy *phy = &ex->ex_phy[phy_id];
1331 u8 *rri_req;
1332 u8 *rri_resp;
1333
1334 *present = 0;
1335 *index = 0;
1336
1337 rri_req = alloc_smp_req(RRI_REQ_SIZE);
1338 if (!rri_req)
1339 return -ENOMEM;
1340
1341 rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1342 if (!rri_resp) {
1343 kfree(rri_req);
1344 return -ENOMEM;
1345 }
1346
1347 rri_req[1] = SMP_REPORT_ROUTE_INFO;
1348 rri_req[9] = phy_id;
1349
1350 for (i = 0; i < ex->max_route_indexes ; i++) {
1351 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1352 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1353 RRI_RESP_SIZE);
1354 if (res)
1355 goto out;
1356 res = rri_resp[2];
1357 if (res == SMP_RESP_NO_INDEX) {
1358 SAS_DPRINTK("overflow of indexes: dev %016llx "
1359 "phy 0x%x index 0x%x\n",
1360 SAS_ADDR(dev->sas_addr), phy_id, i);
1361 goto out;
1362 } else if (res != SMP_RESP_FUNC_ACC) {
1363 SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001364 "result 0x%x\n", __func__,
James Bottomley2908d772006-08-29 09:22:51 -05001365 SAS_ADDR(dev->sas_addr), phy_id, i, res);
1366 goto out;
1367 }
1368 if (SAS_ADDR(sas_addr) != 0) {
1369 if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1370 *index = i;
1371 if ((rri_resp[12] & 0x80) == 0x80)
1372 *present = 0;
1373 else
1374 *present = 1;
1375 goto out;
1376 } else if (SAS_ADDR(rri_resp+16) == 0) {
1377 *index = i;
1378 *present = 0;
1379 goto out;
1380 }
1381 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1382 phy->last_da_index < i) {
1383 phy->last_da_index = i;
1384 *index = i;
1385 *present = 0;
1386 goto out;
1387 }
1388 }
1389 res = -1;
1390out:
1391 kfree(rri_req);
1392 kfree(rri_resp);
1393 return res;
1394}
1395
1396#define CRI_REQ_SIZE 44
1397#define CRI_RESP_SIZE 8
1398
1399static int sas_configure_set(struct domain_device *dev, int phy_id,
1400 u8 *sas_addr, int index, int include)
1401{
1402 int res;
1403 u8 *cri_req;
1404 u8 *cri_resp;
1405
1406 cri_req = alloc_smp_req(CRI_REQ_SIZE);
1407 if (!cri_req)
1408 return -ENOMEM;
1409
1410 cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1411 if (!cri_resp) {
1412 kfree(cri_req);
1413 return -ENOMEM;
1414 }
1415
1416 cri_req[1] = SMP_CONF_ROUTE_INFO;
1417 *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1418 cri_req[9] = phy_id;
1419 if (SAS_ADDR(sas_addr) == 0 || !include)
1420 cri_req[12] |= 0x80;
1421 memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1422
1423 res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1424 CRI_RESP_SIZE);
1425 if (res)
1426 goto out;
1427 res = cri_resp[2];
1428 if (res == SMP_RESP_NO_INDEX) {
1429 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1430 "index 0x%x\n",
1431 SAS_ADDR(dev->sas_addr), phy_id, index);
1432 }
1433out:
1434 kfree(cri_req);
1435 kfree(cri_resp);
1436 return res;
1437}
1438
1439static int sas_configure_phy(struct domain_device *dev, int phy_id,
1440 u8 *sas_addr, int include)
1441{
1442 int index;
1443 int present;
1444 int res;
1445
1446 res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1447 if (res)
1448 return res;
1449 if (include ^ present)
1450 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1451
1452 return res;
1453}
1454
1455/**
1456 * sas_configure_parent -- configure routing table of parent
1457 * parent: parent expander
1458 * child: child expander
1459 * sas_addr: SAS port identifier of device directly attached to child
1460 */
1461static int sas_configure_parent(struct domain_device *parent,
1462 struct domain_device *child,
1463 u8 *sas_addr, int include)
1464{
1465 struct expander_device *ex_parent = &parent->ex_dev;
1466 int res = 0;
1467 int i;
1468
1469 if (parent->parent) {
1470 res = sas_configure_parent(parent->parent, parent, sas_addr,
1471 include);
1472 if (res)
1473 return res;
1474 }
1475
1476 if (ex_parent->conf_route_table == 0) {
1477 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1478 SAS_ADDR(parent->sas_addr));
1479 return 0;
1480 }
1481
1482 for (i = 0; i < ex_parent->num_phys; i++) {
1483 struct ex_phy *phy = &ex_parent->ex_phy[i];
1484
1485 if ((phy->routing_attr == TABLE_ROUTING) &&
1486 (SAS_ADDR(phy->attached_sas_addr) ==
1487 SAS_ADDR(child->sas_addr))) {
1488 res = sas_configure_phy(parent, i, sas_addr, include);
1489 if (res)
1490 return res;
1491 }
1492 }
1493
1494 return res;
1495}
1496
1497/**
1498 * sas_configure_routing -- configure routing
1499 * dev: expander device
1500 * sas_addr: port identifier of device directly attached to the expander device
1501 */
1502static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1503{
1504 if (dev->parent)
1505 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1506 return 0;
1507}
1508
1509static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
1510{
1511 if (dev->parent)
1512 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1513 return 0;
1514}
1515
James Bottomley2908d772006-08-29 09:22:51 -05001516/**
1517 * sas_discover_expander -- expander discovery
1518 * @ex: pointer to expander domain device
1519 *
1520 * See comment in sas_discover_sata().
1521 */
1522static int sas_discover_expander(struct domain_device *dev)
1523{
1524 int res;
1525
1526 res = sas_notify_lldd_dev_found(dev);
1527 if (res)
1528 return res;
1529
1530 res = sas_ex_general(dev);
1531 if (res)
1532 goto out_err;
1533 res = sas_ex_manuf_info(dev);
1534 if (res)
1535 goto out_err;
1536
1537 res = sas_expander_discover(dev);
1538 if (res) {
1539 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1540 SAS_ADDR(dev->sas_addr), res);
1541 goto out_err;
1542 }
1543
1544 sas_check_ex_subtractive_boundary(dev);
1545 res = sas_check_parent_topology(dev);
1546 if (res)
1547 goto out_err;
1548 return 0;
1549out_err:
1550 sas_notify_lldd_dev_gone(dev);
1551 return res;
1552}
1553
1554static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1555{
1556 int res = 0;
1557 struct domain_device *dev;
1558
1559 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1560 if (dev->dev_type == EDGE_DEV ||
1561 dev->dev_type == FANOUT_DEV) {
1562 struct sas_expander_device *ex =
1563 rphy_to_expander_device(dev->rphy);
1564
1565 if (level == ex->level)
1566 res = sas_ex_discover_devices(dev, -1);
1567 else if (level > 0)
1568 res = sas_ex_discover_devices(port->port_dev, -1);
1569
1570 }
1571 }
1572
1573 return res;
1574}
1575
1576static int sas_ex_bfs_disc(struct asd_sas_port *port)
1577{
1578 int res;
1579 int level;
1580
1581 do {
1582 level = port->disc.max_level;
1583 res = sas_ex_level_discovery(port, level);
1584 mb();
1585 } while (level < port->disc.max_level);
1586
1587 return res;
1588}
1589
1590int sas_discover_root_expander(struct domain_device *dev)
1591{
1592 int res;
1593 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1594
Darrick J. Wongbf451202007-01-11 14:14:52 -08001595 res = sas_rphy_add(dev->rphy);
1596 if (res)
1597 goto out_err;
James Bottomley2908d772006-08-29 09:22:51 -05001598
1599 ex->level = dev->port->disc.max_level; /* 0 */
1600 res = sas_discover_expander(dev);
Darrick J. Wongbf451202007-01-11 14:14:52 -08001601 if (res)
1602 goto out_err2;
James Bottomley2908d772006-08-29 09:22:51 -05001603
Darrick J. Wongbf451202007-01-11 14:14:52 -08001604 sas_ex_bfs_disc(dev->port);
1605
1606 return res;
1607
1608out_err2:
Darrick J. Wong6f63caa2007-01-26 14:08:43 -08001609 sas_rphy_remove(dev->rphy);
Darrick J. Wongbf451202007-01-11 14:14:52 -08001610out_err:
James Bottomley2908d772006-08-29 09:22:51 -05001611 return res;
1612}
1613
1614/* ---------- Domain revalidation ---------- */
1615
1616static int sas_get_phy_discover(struct domain_device *dev,
1617 int phy_id, struct smp_resp *disc_resp)
1618{
1619 int res;
1620 u8 *disc_req;
1621
1622 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1623 if (!disc_req)
1624 return -ENOMEM;
1625
1626 disc_req[1] = SMP_DISCOVER;
1627 disc_req[9] = phy_id;
1628
1629 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1630 disc_resp, DISCOVER_RESP_SIZE);
1631 if (res)
1632 goto out;
1633 else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1634 res = disc_resp->result;
1635 goto out;
1636 }
1637out:
1638 kfree(disc_req);
1639 return res;
1640}
1641
1642static int sas_get_phy_change_count(struct domain_device *dev,
1643 int phy_id, int *pcc)
1644{
1645 int res;
1646 struct smp_resp *disc_resp;
1647
1648 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1649 if (!disc_resp)
1650 return -ENOMEM;
1651
1652 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1653 if (!res)
1654 *pcc = disc_resp->disc.change_count;
1655
1656 kfree(disc_resp);
1657 return res;
1658}
1659
Dan Williams36a39942011-11-17 17:59:54 -08001660int sas_get_phy_attached_sas_addr(struct domain_device *dev, int phy_id,
1661 u8 *attached_sas_addr)
James Bottomley2908d772006-08-29 09:22:51 -05001662{
1663 int res;
1664 struct smp_resp *disc_resp;
1665 struct discover_resp *dr;
1666
1667 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1668 if (!disc_resp)
1669 return -ENOMEM;
1670 dr = &disc_resp->disc;
1671
1672 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1673 if (!res) {
1674 memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
1675 if (dr->attached_dev_type == 0)
1676 memset(attached_sas_addr, 0, 8);
1677 }
1678 kfree(disc_resp);
1679 return res;
1680}
1681
1682static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
Tom Peng19252de2009-07-17 16:02:04 +08001683 int from_phy, bool update)
James Bottomley2908d772006-08-29 09:22:51 -05001684{
1685 struct expander_device *ex = &dev->ex_dev;
1686 int res = 0;
1687 int i;
1688
1689 for (i = from_phy; i < ex->num_phys; i++) {
1690 int phy_change_count = 0;
1691
1692 res = sas_get_phy_change_count(dev, i, &phy_change_count);
1693 if (res)
1694 goto out;
1695 else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
Tom Peng19252de2009-07-17 16:02:04 +08001696 if (update)
1697 ex->ex_phy[i].phy_change_count =
1698 phy_change_count;
James Bottomley2908d772006-08-29 09:22:51 -05001699 *phy_id = i;
1700 return 0;
1701 }
1702 }
1703out:
1704 return res;
1705}
1706
1707static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1708{
1709 int res;
1710 u8 *rg_req;
1711 struct smp_resp *rg_resp;
1712
1713 rg_req = alloc_smp_req(RG_REQ_SIZE);
1714 if (!rg_req)
1715 return -ENOMEM;
1716
1717 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1718 if (!rg_resp) {
1719 kfree(rg_req);
1720 return -ENOMEM;
1721 }
1722
1723 rg_req[1] = SMP_REPORT_GENERAL;
1724
1725 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1726 RG_RESP_SIZE);
1727 if (res)
1728 goto out;
1729 if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1730 res = rg_resp->result;
1731 goto out;
1732 }
1733
1734 *ecc = be16_to_cpu(rg_resp->rg.change_count);
1735out:
1736 kfree(rg_resp);
1737 kfree(rg_req);
1738 return res;
1739}
Tom Peng19252de2009-07-17 16:02:04 +08001740/**
1741 * sas_find_bcast_dev - find the device issue BROADCAST(CHANGE).
1742 * @dev:domain device to be detect.
1743 * @src_dev: the device which originated BROADCAST(CHANGE).
1744 *
1745 * Add self-configuration expander suport. Suppose two expander cascading,
1746 * when the first level expander is self-configuring, hotplug the disks in
1747 * second level expander, BROADCAST(CHANGE) will not only be originated
1748 * in the second level expander, but also be originated in the first level
1749 * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say,
1750 * expander changed count in two level expanders will all increment at least
1751 * once, but the phy which chang count has changed is the source device which
1752 * we concerned.
1753 */
James Bottomley2908d772006-08-29 09:22:51 -05001754
1755static int sas_find_bcast_dev(struct domain_device *dev,
1756 struct domain_device **src_dev)
1757{
1758 struct expander_device *ex = &dev->ex_dev;
1759 int ex_change_count = -1;
Tom Peng19252de2009-07-17 16:02:04 +08001760 int phy_id = -1;
James Bottomley2908d772006-08-29 09:22:51 -05001761 int res;
Tom Peng19252de2009-07-17 16:02:04 +08001762 struct domain_device *ch;
James Bottomley2908d772006-08-29 09:22:51 -05001763
1764 res = sas_get_ex_change_count(dev, &ex_change_count);
1765 if (res)
1766 goto out;
Tom Peng19252de2009-07-17 16:02:04 +08001767 if (ex_change_count != -1 && ex_change_count != ex->ex_change_count) {
1768 /* Just detect if this expander phys phy change count changed,
1769 * in order to determine if this expander originate BROADCAST,
1770 * and do not update phy change count field in our structure.
1771 */
1772 res = sas_find_bcast_phy(dev, &phy_id, 0, false);
1773 if (phy_id != -1) {
1774 *src_dev = dev;
1775 ex->ex_change_count = ex_change_count;
1776 SAS_DPRINTK("Expander phy change count has changed\n");
1777 return res;
1778 } else
1779 SAS_DPRINTK("Expander phys DID NOT change\n");
1780 }
1781 list_for_each_entry(ch, &ex->children, siblings) {
1782 if (ch->dev_type == EDGE_DEV || ch->dev_type == FANOUT_DEV) {
1783 res = sas_find_bcast_dev(ch, src_dev);
Mark Salyzyn24926da2011-09-01 06:11:17 -07001784 if (*src_dev)
Tom Peng19252de2009-07-17 16:02:04 +08001785 return res;
James Bottomley2908d772006-08-29 09:22:51 -05001786 }
1787 }
1788out:
1789 return res;
1790}
1791
Dan Williams1a34c062011-09-21 22:05:34 -07001792static void sas_unregister_ex_tree(struct asd_sas_port *port, struct domain_device *dev)
James Bottomley2908d772006-08-29 09:22:51 -05001793{
1794 struct expander_device *ex = &dev->ex_dev;
1795 struct domain_device *child, *n;
1796
1797 list_for_each_entry_safe(child, n, &ex->children, siblings) {
Dan Williamse1399422012-01-07 08:52:39 +00001798 set_bit(SAS_DEV_GONE, &child->state);
James Bottomley2908d772006-08-29 09:22:51 -05001799 if (child->dev_type == EDGE_DEV ||
1800 child->dev_type == FANOUT_DEV)
Dan Williams1a34c062011-09-21 22:05:34 -07001801 sas_unregister_ex_tree(port, child);
James Bottomley2908d772006-08-29 09:22:51 -05001802 else
Dan Williams1a34c062011-09-21 22:05:34 -07001803 sas_unregister_dev(port, child);
James Bottomley2908d772006-08-29 09:22:51 -05001804 }
Dan Williams1a34c062011-09-21 22:05:34 -07001805 sas_unregister_dev(port, dev);
James Bottomley2908d772006-08-29 09:22:51 -05001806}
1807
1808static void sas_unregister_devs_sas_addr(struct domain_device *parent,
Tom Peng19252de2009-07-17 16:02:04 +08001809 int phy_id, bool last)
James Bottomley2908d772006-08-29 09:22:51 -05001810{
1811 struct expander_device *ex_dev = &parent->ex_dev;
1812 struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
1813 struct domain_device *child, *n;
Tom Peng19252de2009-07-17 16:02:04 +08001814 if (last) {
1815 list_for_each_entry_safe(child, n,
1816 &ex_dev->children, siblings) {
1817 if (SAS_ADDR(child->sas_addr) ==
1818 SAS_ADDR(phy->attached_sas_addr)) {
Dan Williamse1399422012-01-07 08:52:39 +00001819 set_bit(SAS_DEV_GONE, &child->state);
Tom Peng19252de2009-07-17 16:02:04 +08001820 if (child->dev_type == EDGE_DEV ||
1821 child->dev_type == FANOUT_DEV)
Dan Williams1a34c062011-09-21 22:05:34 -07001822 sas_unregister_ex_tree(parent->port, child);
Tom Peng19252de2009-07-17 16:02:04 +08001823 else
Dan Williams1a34c062011-09-21 22:05:34 -07001824 sas_unregister_dev(parent->port, child);
Tom Peng19252de2009-07-17 16:02:04 +08001825 break;
1826 }
James Bottomley2908d772006-08-29 09:22:51 -05001827 }
Tom Peng19252de2009-07-17 16:02:04 +08001828 sas_disable_routing(parent, phy->attached_sas_addr);
James Bottomley2908d772006-08-29 09:22:51 -05001829 }
James Bottomley2908d772006-08-29 09:22:51 -05001830 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
Mark Salyzyna73914c2011-09-22 08:32:23 -07001831 if (phy->port) {
1832 sas_port_delete_phy(phy->port, phy->phy);
1833 if (phy->port->num_phys == 0)
1834 sas_port_delete(phy->port);
1835 phy->port = NULL;
1836 }
James Bottomley2908d772006-08-29 09:22:51 -05001837}
1838
1839static int sas_discover_bfs_by_root_level(struct domain_device *root,
1840 const int level)
1841{
1842 struct expander_device *ex_root = &root->ex_dev;
1843 struct domain_device *child;
1844 int res = 0;
1845
1846 list_for_each_entry(child, &ex_root->children, siblings) {
1847 if (child->dev_type == EDGE_DEV ||
1848 child->dev_type == FANOUT_DEV) {
1849 struct sas_expander_device *ex =
1850 rphy_to_expander_device(child->rphy);
1851
1852 if (level > ex->level)
1853 res = sas_discover_bfs_by_root_level(child,
1854 level);
1855 else if (level == ex->level)
1856 res = sas_ex_discover_devices(child, -1);
1857 }
1858 }
1859 return res;
1860}
1861
1862static int sas_discover_bfs_by_root(struct domain_device *dev)
1863{
1864 int res;
1865 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1866 int level = ex->level+1;
1867
1868 res = sas_ex_discover_devices(dev, -1);
1869 if (res)
1870 goto out;
1871 do {
1872 res = sas_discover_bfs_by_root_level(dev, level);
1873 mb();
1874 level += 1;
1875 } while (level <= dev->port->disc.max_level);
1876out:
1877 return res;
1878}
1879
1880static int sas_discover_new(struct domain_device *dev, int phy_id)
1881{
1882 struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1883 struct domain_device *child;
Tom Peng19252de2009-07-17 16:02:04 +08001884 bool found = false;
1885 int res, i;
James Bottomley2908d772006-08-29 09:22:51 -05001886
1887 SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1888 SAS_ADDR(dev->sas_addr), phy_id);
1889 res = sas_ex_phy_discover(dev, phy_id);
1890 if (res)
1891 goto out;
Tom Peng19252de2009-07-17 16:02:04 +08001892 /* to support the wide port inserted */
1893 for (i = 0; i < dev->ex_dev.num_phys; i++) {
1894 struct ex_phy *ex_phy_temp = &dev->ex_dev.ex_phy[i];
1895 if (i == phy_id)
1896 continue;
1897 if (SAS_ADDR(ex_phy_temp->attached_sas_addr) ==
1898 SAS_ADDR(ex_phy->attached_sas_addr)) {
1899 found = true;
1900 break;
1901 }
1902 }
1903 if (found) {
1904 sas_ex_join_wide_port(dev, phy_id);
1905 return 0;
1906 }
James Bottomley2908d772006-08-29 09:22:51 -05001907 res = sas_ex_discover_devices(dev, phy_id);
Tom Peng19252de2009-07-17 16:02:04 +08001908 if (!res)
James Bottomley2908d772006-08-29 09:22:51 -05001909 goto out;
1910 list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1911 if (SAS_ADDR(child->sas_addr) ==
1912 SAS_ADDR(ex_phy->attached_sas_addr)) {
1913 if (child->dev_type == EDGE_DEV ||
1914 child->dev_type == FANOUT_DEV)
1915 res = sas_discover_bfs_by_root(child);
1916 break;
1917 }
1918 }
1919out:
1920 return res;
1921}
1922
Tom Peng19252de2009-07-17 16:02:04 +08001923static int sas_rediscover_dev(struct domain_device *dev, int phy_id, bool last)
James Bottomley2908d772006-08-29 09:22:51 -05001924{
1925 struct expander_device *ex = &dev->ex_dev;
1926 struct ex_phy *phy = &ex->ex_phy[phy_id];
1927 u8 attached_sas_addr[8];
1928 int res;
1929
1930 res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
1931 switch (res) {
1932 case SMP_RESP_NO_PHY:
1933 phy->phy_state = PHY_NOT_PRESENT;
Tom Peng19252de2009-07-17 16:02:04 +08001934 sas_unregister_devs_sas_addr(dev, phy_id, last);
James Bottomley2908d772006-08-29 09:22:51 -05001935 goto out; break;
1936 case SMP_RESP_PHY_VACANT:
1937 phy->phy_state = PHY_VACANT;
Tom Peng19252de2009-07-17 16:02:04 +08001938 sas_unregister_devs_sas_addr(dev, phy_id, last);
James Bottomley2908d772006-08-29 09:22:51 -05001939 goto out; break;
1940 case SMP_RESP_FUNC_ACC:
1941 break;
1942 }
1943
1944 if (SAS_ADDR(attached_sas_addr) == 0) {
1945 phy->phy_state = PHY_EMPTY;
Tom Peng19252de2009-07-17 16:02:04 +08001946 sas_unregister_devs_sas_addr(dev, phy_id, last);
James Bottomley2908d772006-08-29 09:22:51 -05001947 } else if (SAS_ADDR(attached_sas_addr) ==
1948 SAS_ADDR(phy->attached_sas_addr)) {
1949 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
1950 SAS_ADDR(dev->sas_addr), phy_id);
James Bottomleya01e70e2006-09-06 19:28:07 -05001951 sas_ex_phy_discover(dev, phy_id);
James Bottomley2908d772006-08-29 09:22:51 -05001952 } else
1953 res = sas_discover_new(dev, phy_id);
1954out:
1955 return res;
1956}
1957
Tom Peng19252de2009-07-17 16:02:04 +08001958/**
1959 * sas_rediscover - revalidate the domain.
1960 * @dev:domain device to be detect.
1961 * @phy_id: the phy id will be detected.
1962 *
1963 * NOTE: this process _must_ quit (return) as soon as any connection
1964 * errors are encountered. Connection recovery is done elsewhere.
1965 * Discover process only interrogates devices in order to discover the
1966 * domain.For plugging out, we un-register the device only when it is
1967 * the last phy in the port, for other phys in this port, we just delete it
1968 * from the port.For inserting, we do discovery when it is the
1969 * first phy,for other phys in this port, we add it to the port to
1970 * forming the wide-port.
1971 */
James Bottomley2908d772006-08-29 09:22:51 -05001972static int sas_rediscover(struct domain_device *dev, const int phy_id)
1973{
1974 struct expander_device *ex = &dev->ex_dev;
1975 struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
1976 int res = 0;
1977 int i;
Tom Peng19252de2009-07-17 16:02:04 +08001978 bool last = true; /* is this the last phy of the port */
James Bottomley2908d772006-08-29 09:22:51 -05001979
1980 SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
1981 SAS_ADDR(dev->sas_addr), phy_id);
1982
1983 if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
1984 for (i = 0; i < ex->num_phys; i++) {
1985 struct ex_phy *phy = &ex->ex_phy[i];
1986
1987 if (i == phy_id)
1988 continue;
1989 if (SAS_ADDR(phy->attached_sas_addr) ==
1990 SAS_ADDR(changed_phy->attached_sas_addr)) {
1991 SAS_DPRINTK("phy%d part of wide port with "
1992 "phy%d\n", phy_id, i);
Tom Peng19252de2009-07-17 16:02:04 +08001993 last = false;
1994 break;
James Bottomley2908d772006-08-29 09:22:51 -05001995 }
1996 }
Tom Peng19252de2009-07-17 16:02:04 +08001997 res = sas_rediscover_dev(dev, phy_id, last);
James Bottomley2908d772006-08-29 09:22:51 -05001998 } else
1999 res = sas_discover_new(dev, phy_id);
James Bottomley2908d772006-08-29 09:22:51 -05002000 return res;
2001}
2002
2003/**
2004 * sas_revalidate_domain -- revalidate the domain
2005 * @port: port to the domain of interest
2006 *
2007 * NOTE: this process _must_ quit (return) as soon as any connection
2008 * errors are encountered. Connection recovery is done elsewhere.
2009 * Discover process only interrogates devices in order to discover the
2010 * domain.
2011 */
2012int sas_ex_revalidate_domain(struct domain_device *port_dev)
2013{
2014 int res;
2015 struct domain_device *dev = NULL;
2016
2017 res = sas_find_bcast_dev(port_dev, &dev);
2018 if (res)
2019 goto out;
2020 if (dev) {
2021 struct expander_device *ex = &dev->ex_dev;
2022 int i = 0, phy_id;
2023
2024 do {
2025 phy_id = -1;
Tom Peng19252de2009-07-17 16:02:04 +08002026 res = sas_find_bcast_phy(dev, &phy_id, i, true);
James Bottomley2908d772006-08-29 09:22:51 -05002027 if (phy_id == -1)
2028 break;
2029 res = sas_rediscover(dev, phy_id);
2030 i = phy_id + 1;
2031 } while (i < ex->num_phys);
2032 }
2033out:
2034 return res;
2035}
2036
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002037int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
2038 struct request *req)
2039{
2040 struct domain_device *dev;
Darrick J. Wong2cd614c2007-07-24 09:33:49 -07002041 int ret, type;
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002042 struct request *rsp = req->next_rq;
2043
2044 if (!rsp) {
2045 printk("%s: space for a smp response is missing\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002046 __func__);
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002047 return -EINVAL;
2048 }
2049
Darrick J. Wong2cd614c2007-07-24 09:33:49 -07002050 /* no rphy means no smp target support (ie aic94xx host) */
James Bottomleyb98e66f2007-12-28 16:35:17 -06002051 if (!rphy)
2052 return sas_smp_host_handler(shost, req, rsp);
2053
Darrick J. Wong2cd614c2007-07-24 09:33:49 -07002054 type = rphy->identify.device_type;
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002055
2056 if (type != SAS_EDGE_EXPANDER_DEVICE &&
2057 type != SAS_FANOUT_EXPANDER_DEVICE) {
2058 printk("%s: can we send a smp request to a device?\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002059 __func__);
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002060 return -EINVAL;
2061 }
2062
2063 dev = sas_find_dev_by_rphy(rphy);
2064 if (!dev) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002065 printk("%s: fail to find a domain_device?\n", __func__);
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002066 return -EINVAL;
2067 }
2068
2069 /* do we need to support multiple segments? */
2070 if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
2071 printk("%s: multiple segments req %u %u, rsp %u %u\n",
Tejun Heob0790412009-05-07 22:24:42 +09002072 __func__, req->bio->bi_vcnt, blk_rq_bytes(req),
2073 rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002074 return -EINVAL;
2075 }
2076
Tejun Heob0790412009-05-07 22:24:42 +09002077 ret = smp_execute_task(dev, bio_data(req->bio), blk_rq_bytes(req),
2078 bio_data(rsp->bio), blk_rq_bytes(rsp));
James Bottomley2d4b63e2007-12-29 11:49:53 -06002079 if (ret > 0) {
2080 /* positive number is the untransferred residual */
Tejun Heoc3a4d782009-05-07 22:24:37 +09002081 rsp->resid_len = ret;
Tejun Heo5f49f632009-05-19 18:33:05 +09002082 req->resid_len = 0;
James Bottomley2d4b63e2007-12-29 11:49:53 -06002083 ret = 0;
Tejun Heo5f49f632009-05-19 18:33:05 +09002084 } else if (ret == 0) {
2085 rsp->resid_len = 0;
2086 req->resid_len = 0;
James Bottomley2d4b63e2007-12-29 11:49:53 -06002087 }
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002088
2089 return ret;
2090}