[SCSI] bsg: unexport sg v3 helper functions
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / scsi / libsas / sas_expander.c
CommitLineData
2908d778
JB
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
2908d778 25#include <linux/scatterlist.h>
ba1fc175 26#include <linux/blkdev.h>
2908d778
JB
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
2908d778
JB
40/* ---------- SMP task management ---------- */
41
42static void smp_task_timedout(unsigned long _task)
43{
44 struct sas_task *task = (void *) _task;
45 unsigned long flags;
46
47 spin_lock_irqsave(&task->task_state_lock, flags);
48 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
49 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
50 spin_unlock_irqrestore(&task->task_state_lock, flags);
51
52 complete(&task->completion);
53}
54
55static void smp_task_done(struct sas_task *task)
56{
57 if (!del_timer(&task->timer))
58 return;
59 complete(&task->completion);
60}
61
62/* Give it some long enough timeout. In seconds. */
63#define SMP_TIMEOUT 10
64
65static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
66 void *resp, int resp_size)
67{
42961ee8 68 int res, retry;
69 struct sas_task *task = NULL;
2908d778
JB
70 struct sas_internal *i =
71 to_sas_internal(dev->port->ha->core.shost->transportt);
72
42961ee8 73 for (retry = 0; retry < 3; retry++) {
74 task = sas_alloc_task(GFP_KERNEL);
75 if (!task)
76 return -ENOMEM;
2908d778 77
42961ee8 78 task->dev = dev;
79 task->task_proto = dev->tproto;
80 sg_init_one(&task->smp_task.smp_req, req, req_size);
81 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
2908d778 82
42961ee8 83 task->task_done = smp_task_done;
2908d778 84
42961ee8 85 task->timer.data = (unsigned long) task;
86 task->timer.function = smp_task_timedout;
87 task->timer.expires = jiffies + SMP_TIMEOUT*HZ;
88 add_timer(&task->timer);
2908d778 89
42961ee8 90 res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
2908d778 91
42961ee8 92 if (res) {
93 del_timer(&task->timer);
94 SAS_DPRINTK("executing SMP task failed:%d\n", res);
2908d778
JB
95 goto ex_err;
96 }
42961ee8 97
98 wait_for_completion(&task->completion);
99 res = -ETASK;
100 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
101 SAS_DPRINTK("smp task timed out or aborted\n");
102 i->dft->lldd_abort_task(task);
103 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
104 SAS_DPRINTK("SMP task aborted and not done\n");
105 goto ex_err;
106 }
107 }
108 if (task->task_status.resp == SAS_TASK_COMPLETE &&
109 task->task_status.stat == SAM_GOOD) {
110 res = 0;
111 break;
112 } else {
113 SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
114 "status 0x%x\n", __FUNCTION__,
115 SAS_ADDR(dev->sas_addr),
116 task->task_status.resp,
117 task->task_status.stat);
118 sas_free_task(task);
119 task = NULL;
120 }
2908d778 121 }
2908d778 122ex_err:
42961ee8 123 BUG_ON(retry == 3 && task != NULL);
124 if (task != NULL) {
125 sas_free_task(task);
126 }
2908d778
JB
127 return res;
128}
129
130/* ---------- Allocations ---------- */
131
132static inline void *alloc_smp_req(int size)
133{
134 u8 *p = kzalloc(size, GFP_KERNEL);
135 if (p)
136 p[0] = SMP_REQUEST;
137 return p;
138}
139
140static inline void *alloc_smp_resp(int size)
141{
142 return kzalloc(size, GFP_KERNEL);
143}
144
145/* ---------- Expander configuration ---------- */
146
147static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
148 void *disc_resp)
149{
150 struct expander_device *ex = &dev->ex_dev;
151 struct ex_phy *phy = &ex->ex_phy[phy_id];
152 struct smp_resp *resp = disc_resp;
153 struct discover_resp *dr = &resp->disc;
154 struct sas_rphy *rphy = dev->rphy;
155 int rediscover = (phy->phy != NULL);
156
157 if (!rediscover) {
158 phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
159
160 /* FIXME: error_handling */
161 BUG_ON(!phy->phy);
162 }
163
164 switch (resp->result) {
165 case SMP_RESP_PHY_VACANT:
166 phy->phy_state = PHY_VACANT;
167 return;
168 default:
169 phy->phy_state = PHY_NOT_PRESENT;
170 return;
171 case SMP_RESP_FUNC_ACC:
172 phy->phy_state = PHY_EMPTY; /* do not know yet */
173 break;
174 }
175
176 phy->phy_id = phy_id;
177 phy->attached_dev_type = dr->attached_dev_type;
178 phy->linkrate = dr->linkrate;
179 phy->attached_sata_host = dr->attached_sata_host;
180 phy->attached_sata_dev = dr->attached_sata_dev;
181 phy->attached_sata_ps = dr->attached_sata_ps;
182 phy->attached_iproto = dr->iproto << 1;
183 phy->attached_tproto = dr->tproto << 1;
184 memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
185 phy->attached_phy_id = dr->attached_phy_id;
186 phy->phy_change_count = dr->change_count;
187 phy->routing_attr = dr->routing_attr;
188 phy->virtual = dr->virtual;
189 phy->last_da_index = -1;
190
191 phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
192 phy->phy->identify.target_port_protocols = phy->attached_tproto;
193 phy->phy->identify.phy_identifier = phy_id;
a01e70e5
JB
194 phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
195 phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
196 phy->phy->minimum_linkrate = dr->pmin_linkrate;
197 phy->phy->maximum_linkrate = dr->pmax_linkrate;
88edf746 198 phy->phy->negotiated_linkrate = phy->linkrate;
2908d778
JB
199
200 if (!rediscover)
201 sas_phy_add(phy->phy);
202
203 SAS_DPRINTK("ex %016llx phy%02d:%c attached: %016llx\n",
204 SAS_ADDR(dev->sas_addr), phy->phy_id,
205 phy->routing_attr == TABLE_ROUTING ? 'T' :
206 phy->routing_attr == DIRECT_ROUTING ? 'D' :
207 phy->routing_attr == SUBTRACTIVE_ROUTING ? 'S' : '?',
208 SAS_ADDR(phy->attached_sas_addr));
209
210 return;
211}
212
213#define DISCOVER_REQ_SIZE 16
214#define DISCOVER_RESP_SIZE 56
215
1acce194
JB
216static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
217 u8 *disc_resp, int single)
218{
219 int i, res;
220
221 disc_req[9] = single;
222 for (i = 1 ; i < 3; i++) {
223 struct discover_resp *dr;
224
225 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
226 disc_resp, DISCOVER_RESP_SIZE);
227 if (res)
228 return res;
229 /* This is detecting a failure to transmit inital
230 * dev to host FIS as described in section G.5 of
231 * sas-2 r 04b */
232 dr = &((struct smp_resp *)disc_resp)->disc;
233 if (!(dr->attached_dev_type == 0 &&
234 dr->attached_sata_dev))
235 break;
236 /* In order to generate the dev to host FIS, we
237 * send a link reset to the expander port */
38e2f035 238 sas_smp_phy_control(dev, single, PHY_FUNC_LINK_RESET, NULL);
1acce194
JB
239 /* Wait for the reset to trigger the negotiation */
240 msleep(500);
241 }
242 sas_set_ex_phy(dev, single, disc_resp);
243 return 0;
244}
245
2908d778
JB
246static int sas_ex_phy_discover(struct domain_device *dev, int single)
247{
248 struct expander_device *ex = &dev->ex_dev;
249 int res = 0;
250 u8 *disc_req;
251 u8 *disc_resp;
252
253 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
254 if (!disc_req)
255 return -ENOMEM;
256
257 disc_resp = alloc_smp_req(DISCOVER_RESP_SIZE);
258 if (!disc_resp) {
259 kfree(disc_req);
260 return -ENOMEM;
261 }
262
263 disc_req[1] = SMP_DISCOVER;
264
265 if (0 <= single && single < ex->num_phys) {
1acce194 266 res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
2908d778
JB
267 } else {
268 int i;
269
270 for (i = 0; i < ex->num_phys; i++) {
1acce194
JB
271 res = sas_ex_phy_discover_helper(dev, disc_req,
272 disc_resp, i);
2908d778
JB
273 if (res)
274 goto out_err;
2908d778
JB
275 }
276 }
277out_err:
278 kfree(disc_resp);
279 kfree(disc_req);
280 return res;
281}
282
283static int sas_expander_discover(struct domain_device *dev)
284{
285 struct expander_device *ex = &dev->ex_dev;
286 int res = -ENOMEM;
287
288 ex->ex_phy = kzalloc(sizeof(*ex->ex_phy)*ex->num_phys, GFP_KERNEL);
289 if (!ex->ex_phy)
290 return -ENOMEM;
291
292 res = sas_ex_phy_discover(dev, -1);
293 if (res)
294 goto out_err;
295
296 return 0;
297 out_err:
298 kfree(ex->ex_phy);
299 ex->ex_phy = NULL;
300 return res;
301}
302
303#define MAX_EXPANDER_PHYS 128
304
305static void ex_assign_report_general(struct domain_device *dev,
306 struct smp_resp *resp)
307{
308 struct report_general_resp *rg = &resp->rg;
309
310 dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
311 dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
312 dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
313 dev->ex_dev.conf_route_table = rg->conf_route_table;
314 dev->ex_dev.configuring = rg->configuring;
315 memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
316}
317
318#define RG_REQ_SIZE 8
319#define RG_RESP_SIZE 32
320
321static int sas_ex_general(struct domain_device *dev)
322{
323 u8 *rg_req;
324 struct smp_resp *rg_resp;
325 int res;
326 int i;
327
328 rg_req = alloc_smp_req(RG_REQ_SIZE);
329 if (!rg_req)
330 return -ENOMEM;
331
332 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
333 if (!rg_resp) {
334 kfree(rg_req);
335 return -ENOMEM;
336 }
337
338 rg_req[1] = SMP_REPORT_GENERAL;
339
340 for (i = 0; i < 5; i++) {
341 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
342 RG_RESP_SIZE);
343
344 if (res) {
345 SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
346 SAS_ADDR(dev->sas_addr), res);
347 goto out;
348 } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
349 SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
350 SAS_ADDR(dev->sas_addr), rg_resp->result);
351 res = rg_resp->result;
352 goto out;
353 }
354
355 ex_assign_report_general(dev, rg_resp);
356
357 if (dev->ex_dev.configuring) {
358 SAS_DPRINTK("RG: ex %llx self-configuring...\n",
359 SAS_ADDR(dev->sas_addr));
360 schedule_timeout_interruptible(5*HZ);
361 } else
362 break;
363 }
364out:
365 kfree(rg_req);
366 kfree(rg_resp);
367 return res;
368}
369
370static void ex_assign_manuf_info(struct domain_device *dev, void
371 *_mi_resp)
372{
373 u8 *mi_resp = _mi_resp;
374 struct sas_rphy *rphy = dev->rphy;
375 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
376
377 memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
378 memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
379 memcpy(edev->product_rev, mi_resp + 36,
380 SAS_EXPANDER_PRODUCT_REV_LEN);
381
382 if (mi_resp[8] & 1) {
383 memcpy(edev->component_vendor_id, mi_resp + 40,
384 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
385 edev->component_id = mi_resp[48] << 8 | mi_resp[49];
386 edev->component_revision_id = mi_resp[50];
387 }
388}
389
390#define MI_REQ_SIZE 8
391#define MI_RESP_SIZE 64
392
393static int sas_ex_manuf_info(struct domain_device *dev)
394{
395 u8 *mi_req;
396 u8 *mi_resp;
397 int res;
398
399 mi_req = alloc_smp_req(MI_REQ_SIZE);
400 if (!mi_req)
401 return -ENOMEM;
402
403 mi_resp = alloc_smp_resp(MI_RESP_SIZE);
404 if (!mi_resp) {
405 kfree(mi_req);
406 return -ENOMEM;
407 }
408
409 mi_req[1] = SMP_REPORT_MANUF_INFO;
410
411 res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
412 if (res) {
413 SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
414 SAS_ADDR(dev->sas_addr), res);
415 goto out;
416 } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
417 SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
418 SAS_ADDR(dev->sas_addr), mi_resp[2]);
419 goto out;
420 }
421
422 ex_assign_manuf_info(dev, mi_resp);
423out:
424 kfree(mi_req);
425 kfree(mi_resp);
426 return res;
427}
428
429#define PC_REQ_SIZE 44
430#define PC_RESP_SIZE 8
431
432int sas_smp_phy_control(struct domain_device *dev, int phy_id,
a01e70e5
JB
433 enum phy_func phy_func,
434 struct sas_phy_linkrates *rates)
2908d778
JB
435{
436 u8 *pc_req;
437 u8 *pc_resp;
438 int res;
439
440 pc_req = alloc_smp_req(PC_REQ_SIZE);
441 if (!pc_req)
442 return -ENOMEM;
443
444 pc_resp = alloc_smp_resp(PC_RESP_SIZE);
445 if (!pc_resp) {
446 kfree(pc_req);
447 return -ENOMEM;
448 }
449
450 pc_req[1] = SMP_PHY_CONTROL;
451 pc_req[9] = phy_id;
452 pc_req[10]= phy_func;
a01e70e5
JB
453 if (rates) {
454 pc_req[32] = rates->minimum_linkrate << 4;
455 pc_req[33] = rates->maximum_linkrate << 4;
456 }
2908d778
JB
457
458 res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
459
460 kfree(pc_resp);
461 kfree(pc_req);
462 return res;
463}
464
465static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
466{
467 struct expander_device *ex = &dev->ex_dev;
468 struct ex_phy *phy = &ex->ex_phy[phy_id];
469
a01e70e5 470 sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
88edf746 471 phy->linkrate = SAS_PHY_DISABLED;
2908d778
JB
472}
473
474static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
475{
476 struct expander_device *ex = &dev->ex_dev;
477 int i;
478
479 for (i = 0; i < ex->num_phys; i++) {
480 struct ex_phy *phy = &ex->ex_phy[i];
481
482 if (phy->phy_state == PHY_VACANT ||
483 phy->phy_state == PHY_NOT_PRESENT)
484 continue;
485
486 if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
487 sas_ex_disable_phy(dev, i);
488 }
489}
490
491static int sas_dev_present_in_domain(struct asd_sas_port *port,
492 u8 *sas_addr)
493{
494 struct domain_device *dev;
495
496 if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
497 return 1;
498 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
499 if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
500 return 1;
501 }
502 return 0;
503}
504
505#define RPEL_REQ_SIZE 16
506#define RPEL_RESP_SIZE 32
507int sas_smp_get_phy_events(struct sas_phy *phy)
508{
509 int res;
510 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
511 struct domain_device *dev = sas_find_dev_by_rphy(rphy);
512 u8 *req = alloc_smp_req(RPEL_REQ_SIZE);
513 u8 *resp = kzalloc(RPEL_RESP_SIZE, GFP_KERNEL);
514
515 if (!resp)
516 return -ENOMEM;
517
518 req[1] = SMP_REPORT_PHY_ERR_LOG;
519 req[9] = phy->number;
520
521 res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
522 resp, RPEL_RESP_SIZE);
523
524 if (!res)
525 goto out;
526
527 phy->invalid_dword_count = scsi_to_u32(&resp[12]);
528 phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
529 phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
530 phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
531
532 out:
533 kfree(resp);
534 return res;
535
536}
537
538#define RPS_REQ_SIZE 16
539#define RPS_RESP_SIZE 60
540
541static int sas_get_report_phy_sata(struct domain_device *dev,
542 int phy_id,
543 struct smp_resp *rps_resp)
544{
545 int res;
546 u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
1acce194 547 u8 *resp = (u8 *)rps_resp;
2908d778
JB
548
549 if (!rps_req)
550 return -ENOMEM;
551
552 rps_req[1] = SMP_REPORT_PHY_SATA;
553 rps_req[9] = phy_id;
554
555 res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
556 rps_resp, RPS_RESP_SIZE);
557
1acce194
JB
558 /* 0x34 is the FIS type for the D2H fis. There's a potential
559 * standards cockup here. sas-2 explicitly specifies the FIS
560 * should be encoded so that FIS type is in resp[24].
561 * However, some expanders endian reverse this. Undo the
562 * reversal here */
563 if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
564 int i;
565
566 for (i = 0; i < 5; i++) {
567 int j = 24 + (i*4);
568 u8 a, b;
569 a = resp[j + 0];
570 b = resp[j + 1];
571 resp[j + 0] = resp[j + 3];
572 resp[j + 1] = resp[j + 2];
573 resp[j + 2] = b;
574 resp[j + 3] = a;
575 }
576 }
577
2908d778 578 kfree(rps_req);
1acce194 579 return res;
2908d778
JB
580}
581
582static void sas_ex_get_linkrate(struct domain_device *parent,
583 struct domain_device *child,
584 struct ex_phy *parent_phy)
585{
586 struct expander_device *parent_ex = &parent->ex_dev;
587 struct sas_port *port;
588 int i;
589
590 child->pathways = 0;
591
592 port = parent_phy->port;
593
594 for (i = 0; i < parent_ex->num_phys; i++) {
595 struct ex_phy *phy = &parent_ex->ex_phy[i];
596
597 if (phy->phy_state == PHY_VACANT ||
598 phy->phy_state == PHY_NOT_PRESENT)
599 continue;
600
601 if (SAS_ADDR(phy->attached_sas_addr) ==
602 SAS_ADDR(child->sas_addr)) {
603
604 child->min_linkrate = min(parent->min_linkrate,
605 phy->linkrate);
606 child->max_linkrate = max(parent->max_linkrate,
607 phy->linkrate);
608 child->pathways++;
609 sas_port_add_phy(port, phy->phy);
610 }
611 }
612 child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
613 child->pathways = min(child->pathways, parent->pathways);
614}
615
616static struct domain_device *sas_ex_discover_end_dev(
617 struct domain_device *parent, int phy_id)
618{
619 struct expander_device *parent_ex = &parent->ex_dev;
620 struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
621 struct domain_device *child = NULL;
622 struct sas_rphy *rphy;
623 int res;
624
625 if (phy->attached_sata_host || phy->attached_sata_ps)
626 return NULL;
627
628 child = kzalloc(sizeof(*child), GFP_KERNEL);
629 if (!child)
630 return NULL;
631
632 child->parent = parent;
633 child->port = parent->port;
634 child->iproto = phy->attached_iproto;
635 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
636 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
024879ea
JB
637 if (!phy->port) {
638 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
639 if (unlikely(!phy->port))
640 goto out_err;
641 if (unlikely(sas_port_add(phy->port) != 0)) {
642 sas_port_free(phy->port);
643 goto out_err;
644 }
645 }
2908d778
JB
646 sas_ex_get_linkrate(parent, child, phy);
647
648 if ((phy->attached_tproto & SAS_PROTO_STP) || phy->attached_sata_dev) {
649 child->dev_type = SATA_DEV;
650 if (phy->attached_tproto & SAS_PROTO_STP)
651 child->tproto = phy->attached_tproto;
652 if (phy->attached_sata_dev)
653 child->tproto |= SATA_DEV;
654 res = sas_get_report_phy_sata(parent, phy_id,
655 &child->sata_dev.rps_resp);
656 if (res) {
657 SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
658 "0x%x\n", SAS_ADDR(parent->sas_addr),
659 phy_id, res);
024879ea 660 goto out_free;
2908d778
JB
661 }
662 memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
663 sizeof(struct dev_to_host_fis));
1acce194
JB
664
665 rphy = sas_end_device_alloc(phy->port);
528fd552
JB
666 if (unlikely(!rphy))
667 goto out_free;
1acce194 668
2908d778 669 sas_init_dev(child);
1acce194
JB
670
671 child->rphy = rphy;
672
9d720d82 673 spin_lock_irq(&parent->port->dev_list_lock);
1acce194 674 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
9d720d82 675 spin_unlock_irq(&parent->port->dev_list_lock);
1acce194 676
2908d778
JB
677 res = sas_discover_sata(child);
678 if (res) {
679 SAS_DPRINTK("sas_discover_sata() for device %16llx at "
680 "%016llx:0x%x returned 0x%x\n",
681 SAS_ADDR(child->sas_addr),
682 SAS_ADDR(parent->sas_addr), phy_id, res);
1acce194 683 goto out_list_del;
2908d778
JB
684 }
685 } else if (phy->attached_tproto & SAS_PROTO_SSP) {
686 child->dev_type = SAS_END_DEV;
687 rphy = sas_end_device_alloc(phy->port);
688 /* FIXME: error handling */
024879ea
JB
689 if (unlikely(!rphy))
690 goto out_free;
2908d778
JB
691 child->tproto = phy->attached_tproto;
692 sas_init_dev(child);
693
694 child->rphy = rphy;
695 sas_fill_in_rphy(child, rphy);
696
9d720d82 697 spin_lock_irq(&parent->port->dev_list_lock);
2908d778 698 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
9d720d82 699 spin_unlock_irq(&parent->port->dev_list_lock);
2908d778
JB
700
701 res = sas_discover_end_dev(child);
702 if (res) {
703 SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
704 "at %016llx:0x%x returned 0x%x\n",
705 SAS_ADDR(child->sas_addr),
706 SAS_ADDR(parent->sas_addr), phy_id, res);
024879ea 707 goto out_list_del;
2908d778
JB
708 }
709 } else {
710 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
711 phy->attached_tproto, SAS_ADDR(parent->sas_addr),
712 phy_id);
713 }
714
715 list_add_tail(&child->siblings, &parent_ex->children);
716 return child;
024879ea
JB
717
718 out_list_del:
6f63caae
DW
719 sas_rphy_free(child->rphy);
720 child->rphy = NULL;
024879ea 721 list_del(&child->dev_list_node);
024879ea
JB
722 out_free:
723 sas_port_delete(phy->port);
724 out_err:
725 phy->port = NULL;
726 kfree(child);
727 return NULL;
2908d778
JB
728}
729
423f7cf4
DW
730/* See if this phy is part of a wide port */
731static int sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
732{
733 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
734 int i;
735
736 for (i = 0; i < parent->ex_dev.num_phys; i++) {
737 struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
738
739 if (ephy == phy)
740 continue;
741
742 if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
743 SAS_ADDR_SIZE) && ephy->port) {
744 sas_port_add_phy(ephy->port, phy->phy);
745 phy->phy_state = PHY_DEVICE_DISCOVERED;
746 return 0;
747 }
748 }
749
750 return -ENODEV;
751}
752
2908d778
JB
753static struct domain_device *sas_ex_discover_expander(
754 struct domain_device *parent, int phy_id)
755{
756 struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
757 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
758 struct domain_device *child = NULL;
759 struct sas_rphy *rphy;
760 struct sas_expander_device *edev;
761 struct asd_sas_port *port;
762 int res;
763
764 if (phy->routing_attr == DIRECT_ROUTING) {
765 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
766 "allowed\n",
767 SAS_ADDR(parent->sas_addr), phy_id,
768 SAS_ADDR(phy->attached_sas_addr),
769 phy->attached_phy_id);
770 return NULL;
771 }
772 child = kzalloc(sizeof(*child), GFP_KERNEL);
773 if (!child)
774 return NULL;
775
776 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
777 /* FIXME: better error handling */
778 BUG_ON(sas_port_add(phy->port) != 0);
779
780
781 switch (phy->attached_dev_type) {
782 case EDGE_DEV:
783 rphy = sas_expander_alloc(phy->port,
784 SAS_EDGE_EXPANDER_DEVICE);
785 break;
786 case FANOUT_DEV:
787 rphy = sas_expander_alloc(phy->port,
788 SAS_FANOUT_EXPANDER_DEVICE);
789 break;
790 default:
791 rphy = NULL; /* shut gcc up */
792 BUG();
793 }
794 port = parent->port;
795 child->rphy = rphy;
796 edev = rphy_to_expander_device(rphy);
797 child->dev_type = phy->attached_dev_type;
798 child->parent = parent;
799 child->port = port;
800 child->iproto = phy->attached_iproto;
801 child->tproto = phy->attached_tproto;
802 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
803 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
804 sas_ex_get_linkrate(parent, child, phy);
805 edev->level = parent_ex->level + 1;
806 parent->port->disc.max_level = max(parent->port->disc.max_level,
807 edev->level);
808 sas_init_dev(child);
809 sas_fill_in_rphy(child, rphy);
810 sas_rphy_add(rphy);
811
9d720d82 812 spin_lock_irq(&parent->port->dev_list_lock);
2908d778 813 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
9d720d82 814 spin_unlock_irq(&parent->port->dev_list_lock);
2908d778
JB
815
816 res = sas_discover_expander(child);
817 if (res) {
818 kfree(child);
819 return NULL;
820 }
821 list_add_tail(&child->siblings, &parent->ex_dev.children);
822 return child;
823}
824
825static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
826{
827 struct expander_device *ex = &dev->ex_dev;
828 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
829 struct domain_device *child = NULL;
830 int res = 0;
831
832 /* Phy state */
88edf746 833 if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
a01e70e5 834 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
2908d778
JB
835 res = sas_ex_phy_discover(dev, phy_id);
836 if (res)
837 return res;
838 }
839
840 /* Parent and domain coherency */
841 if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
842 SAS_ADDR(dev->port->sas_addr))) {
843 sas_add_parent_port(dev, phy_id);
844 return 0;
845 }
846 if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
847 SAS_ADDR(dev->parent->sas_addr))) {
848 sas_add_parent_port(dev, phy_id);
849 if (ex_phy->routing_attr == TABLE_ROUTING)
850 sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
851 return 0;
852 }
853
854 if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
855 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
856
857 if (ex_phy->attached_dev_type == NO_DEVICE) {
858 if (ex_phy->routing_attr == DIRECT_ROUTING) {
859 memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
860 sas_configure_routing(dev, ex_phy->attached_sas_addr);
861 }
862 return 0;
88edf746 863 } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
2908d778
JB
864 return 0;
865
866 if (ex_phy->attached_dev_type != SAS_END_DEV &&
867 ex_phy->attached_dev_type != FANOUT_DEV &&
868 ex_phy->attached_dev_type != EDGE_DEV) {
869 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
870 "phy 0x%x\n", ex_phy->attached_dev_type,
871 SAS_ADDR(dev->sas_addr),
872 phy_id);
873 return 0;
874 }
875
876 res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
877 if (res) {
878 SAS_DPRINTK("configure routing for dev %016llx "
879 "reported 0x%x. Forgotten\n",
880 SAS_ADDR(ex_phy->attached_sas_addr), res);
881 sas_disable_routing(dev, ex_phy->attached_sas_addr);
882 return res;
883 }
884
423f7cf4
DW
885 res = sas_ex_join_wide_port(dev, phy_id);
886 if (!res) {
887 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
888 phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
889 return res;
890 }
891
2908d778
JB
892 switch (ex_phy->attached_dev_type) {
893 case SAS_END_DEV:
894 child = sas_ex_discover_end_dev(dev, phy_id);
895 break;
896 case FANOUT_DEV:
897 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
898 SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
899 "attached to ex %016llx phy 0x%x\n",
900 SAS_ADDR(ex_phy->attached_sas_addr),
901 ex_phy->attached_phy_id,
902 SAS_ADDR(dev->sas_addr),
903 phy_id);
904 sas_ex_disable_phy(dev, phy_id);
905 break;
906 } else
907 memcpy(dev->port->disc.fanout_sas_addr,
908 ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
909 /* fallthrough */
910 case EDGE_DEV:
911 child = sas_ex_discover_expander(dev, phy_id);
912 break;
913 default:
914 break;
915 }
916
917 if (child) {
918 int i;
919
920 for (i = 0; i < ex->num_phys; i++) {
921 if (ex->ex_phy[i].phy_state == PHY_VACANT ||
922 ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
923 continue;
924
925 if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
926 SAS_ADDR(child->sas_addr))
927 ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
928 }
929 }
930
931 return res;
932}
933
934static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
935{
936 struct expander_device *ex = &dev->ex_dev;
937 int i;
938
939 for (i = 0; i < ex->num_phys; i++) {
940 struct ex_phy *phy = &ex->ex_phy[i];
941
942 if (phy->phy_state == PHY_VACANT ||
943 phy->phy_state == PHY_NOT_PRESENT)
944 continue;
945
946 if ((phy->attached_dev_type == EDGE_DEV ||
947 phy->attached_dev_type == FANOUT_DEV) &&
948 phy->routing_attr == SUBTRACTIVE_ROUTING) {
949
950 memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
951
952 return 1;
953 }
954 }
955 return 0;
956}
957
958static int sas_check_level_subtractive_boundary(struct domain_device *dev)
959{
960 struct expander_device *ex = &dev->ex_dev;
961 struct domain_device *child;
962 u8 sub_addr[8] = {0, };
963
964 list_for_each_entry(child, &ex->children, siblings) {
965 if (child->dev_type != EDGE_DEV &&
966 child->dev_type != FANOUT_DEV)
967 continue;
968 if (sub_addr[0] == 0) {
969 sas_find_sub_addr(child, sub_addr);
970 continue;
971 } else {
972 u8 s2[8];
973
974 if (sas_find_sub_addr(child, s2) &&
975 (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
976
977 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
978 "diverges from subtractive "
979 "boundary %016llx\n",
980 SAS_ADDR(dev->sas_addr),
981 SAS_ADDR(child->sas_addr),
982 SAS_ADDR(s2),
983 SAS_ADDR(sub_addr));
984
985 sas_ex_disable_port(child, s2);
986 }
987 }
988 }
989 return 0;
990}
991/**
992 * sas_ex_discover_devices -- discover devices attached to this expander
993 * dev: pointer to the expander domain device
994 * single: if you want to do a single phy, else set to -1;
995 *
996 * Configure this expander for use with its devices and register the
997 * devices of this expander.
998 */
999static int sas_ex_discover_devices(struct domain_device *dev, int single)
1000{
1001 struct expander_device *ex = &dev->ex_dev;
1002 int i = 0, end = ex->num_phys;
1003 int res = 0;
1004
1005 if (0 <= single && single < end) {
1006 i = single;
1007 end = i+1;
1008 }
1009
1010 for ( ; i < end; i++) {
1011 struct ex_phy *ex_phy = &ex->ex_phy[i];
1012
1013 if (ex_phy->phy_state == PHY_VACANT ||
1014 ex_phy->phy_state == PHY_NOT_PRESENT ||
1015 ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
1016 continue;
1017
1018 switch (ex_phy->linkrate) {
88edf746
JB
1019 case SAS_PHY_DISABLED:
1020 case SAS_PHY_RESET_PROBLEM:
1021 case SAS_SATA_PORT_SELECTOR:
2908d778
JB
1022 continue;
1023 default:
1024 res = sas_ex_discover_dev(dev, i);
1025 if (res)
1026 break;
1027 continue;
1028 }
1029 }
1030
1031 if (!res)
1032 sas_check_level_subtractive_boundary(dev);
1033
1034 return res;
1035}
1036
1037static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
1038{
1039 struct expander_device *ex = &dev->ex_dev;
1040 int i;
1041 u8 *sub_sas_addr = NULL;
1042
1043 if (dev->dev_type != EDGE_DEV)
1044 return 0;
1045
1046 for (i = 0; i < ex->num_phys; i++) {
1047 struct ex_phy *phy = &ex->ex_phy[i];
1048
1049 if (phy->phy_state == PHY_VACANT ||
1050 phy->phy_state == PHY_NOT_PRESENT)
1051 continue;
1052
1053 if ((phy->attached_dev_type == FANOUT_DEV ||
1054 phy->attached_dev_type == EDGE_DEV) &&
1055 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1056
1057 if (!sub_sas_addr)
1058 sub_sas_addr = &phy->attached_sas_addr[0];
1059 else if (SAS_ADDR(sub_sas_addr) !=
1060 SAS_ADDR(phy->attached_sas_addr)) {
1061
1062 SAS_DPRINTK("ex %016llx phy 0x%x "
1063 "diverges(%016llx) on subtractive "
1064 "boundary(%016llx). Disabled\n",
1065 SAS_ADDR(dev->sas_addr), i,
1066 SAS_ADDR(phy->attached_sas_addr),
1067 SAS_ADDR(sub_sas_addr));
1068 sas_ex_disable_phy(dev, i);
1069 }
1070 }
1071 }
1072 return 0;
1073}
1074
1075static void sas_print_parent_topology_bug(struct domain_device *child,
1076 struct ex_phy *parent_phy,
1077 struct ex_phy *child_phy)
1078{
1079 static const char ra_char[] = {
1080 [DIRECT_ROUTING] = 'D',
1081 [SUBTRACTIVE_ROUTING] = 'S',
1082 [TABLE_ROUTING] = 'T',
1083 };
1084 static const char *ex_type[] = {
1085 [EDGE_DEV] = "edge",
1086 [FANOUT_DEV] = "fanout",
1087 };
1088 struct domain_device *parent = child->parent;
1089
1090 sas_printk("%s ex %016llx phy 0x%x <--> %s ex %016llx phy 0x%x "
1091 "has %c:%c routing link!\n",
1092
1093 ex_type[parent->dev_type],
1094 SAS_ADDR(parent->sas_addr),
1095 parent_phy->phy_id,
1096
1097 ex_type[child->dev_type],
1098 SAS_ADDR(child->sas_addr),
1099 child_phy->phy_id,
1100
1101 ra_char[parent_phy->routing_attr],
1102 ra_char[child_phy->routing_attr]);
1103}
1104
1105static int sas_check_eeds(struct domain_device *child,
1106 struct ex_phy *parent_phy,
1107 struct ex_phy *child_phy)
1108{
1109 int res = 0;
1110 struct domain_device *parent = child->parent;
1111
1112 if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1113 res = -ENODEV;
1114 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1115 "phy S:0x%x, while there is a fanout ex %016llx\n",
1116 SAS_ADDR(parent->sas_addr),
1117 parent_phy->phy_id,
1118 SAS_ADDR(child->sas_addr),
1119 child_phy->phy_id,
1120 SAS_ADDR(parent->port->disc.fanout_sas_addr));
1121 } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1122 memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1123 SAS_ADDR_SIZE);
1124 memcpy(parent->port->disc.eeds_b, child->sas_addr,
1125 SAS_ADDR_SIZE);
1126 } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1127 SAS_ADDR(parent->sas_addr)) ||
1128 (SAS_ADDR(parent->port->disc.eeds_a) ==
1129 SAS_ADDR(child->sas_addr)))
1130 &&
1131 ((SAS_ADDR(parent->port->disc.eeds_b) ==
1132 SAS_ADDR(parent->sas_addr)) ||
1133 (SAS_ADDR(parent->port->disc.eeds_b) ==
1134 SAS_ADDR(child->sas_addr))))
1135 ;
1136 else {
1137 res = -ENODEV;
1138 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1139 "phy 0x%x link forms a third EEDS!\n",
1140 SAS_ADDR(parent->sas_addr),
1141 parent_phy->phy_id,
1142 SAS_ADDR(child->sas_addr),
1143 child_phy->phy_id);
1144 }
1145
1146 return res;
1147}
1148
1149/* Here we spill over 80 columns. It is intentional.
1150 */
1151static int sas_check_parent_topology(struct domain_device *child)
1152{
1153 struct expander_device *child_ex = &child->ex_dev;
1154 struct expander_device *parent_ex;
1155 int i;
1156 int res = 0;
1157
1158 if (!child->parent)
1159 return 0;
1160
1161 if (child->parent->dev_type != EDGE_DEV &&
1162 child->parent->dev_type != FANOUT_DEV)
1163 return 0;
1164
1165 parent_ex = &child->parent->ex_dev;
1166
1167 for (i = 0; i < parent_ex->num_phys; i++) {
1168 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1169 struct ex_phy *child_phy;
1170
1171 if (parent_phy->phy_state == PHY_VACANT ||
1172 parent_phy->phy_state == PHY_NOT_PRESENT)
1173 continue;
1174
1175 if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1176 continue;
1177
1178 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1179
1180 switch (child->parent->dev_type) {
1181 case EDGE_DEV:
1182 if (child->dev_type == FANOUT_DEV) {
1183 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1184 child_phy->routing_attr != TABLE_ROUTING) {
1185 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1186 res = -ENODEV;
1187 }
1188 } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1189 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1190 res = sas_check_eeds(child, parent_phy, child_phy);
1191 } else if (child_phy->routing_attr != TABLE_ROUTING) {
1192 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1193 res = -ENODEV;
1194 }
1195 } else if (parent_phy->routing_attr == TABLE_ROUTING &&
1196 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1197 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1198 res = -ENODEV;
1199 }
1200 break;
1201 case FANOUT_DEV:
1202 if (parent_phy->routing_attr != TABLE_ROUTING ||
1203 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1204 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1205 res = -ENODEV;
1206 }
1207 break;
1208 default:
1209 break;
1210 }
1211 }
1212
1213 return res;
1214}
1215
1216#define RRI_REQ_SIZE 16
1217#define RRI_RESP_SIZE 44
1218
1219static int sas_configure_present(struct domain_device *dev, int phy_id,
1220 u8 *sas_addr, int *index, int *present)
1221{
1222 int i, res = 0;
1223 struct expander_device *ex = &dev->ex_dev;
1224 struct ex_phy *phy = &ex->ex_phy[phy_id];
1225 u8 *rri_req;
1226 u8 *rri_resp;
1227
1228 *present = 0;
1229 *index = 0;
1230
1231 rri_req = alloc_smp_req(RRI_REQ_SIZE);
1232 if (!rri_req)
1233 return -ENOMEM;
1234
1235 rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1236 if (!rri_resp) {
1237 kfree(rri_req);
1238 return -ENOMEM;
1239 }
1240
1241 rri_req[1] = SMP_REPORT_ROUTE_INFO;
1242 rri_req[9] = phy_id;
1243
1244 for (i = 0; i < ex->max_route_indexes ; i++) {
1245 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1246 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1247 RRI_RESP_SIZE);
1248 if (res)
1249 goto out;
1250 res = rri_resp[2];
1251 if (res == SMP_RESP_NO_INDEX) {
1252 SAS_DPRINTK("overflow of indexes: dev %016llx "
1253 "phy 0x%x index 0x%x\n",
1254 SAS_ADDR(dev->sas_addr), phy_id, i);
1255 goto out;
1256 } else if (res != SMP_RESP_FUNC_ACC) {
1257 SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
1258 "result 0x%x\n", __FUNCTION__,
1259 SAS_ADDR(dev->sas_addr), phy_id, i, res);
1260 goto out;
1261 }
1262 if (SAS_ADDR(sas_addr) != 0) {
1263 if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1264 *index = i;
1265 if ((rri_resp[12] & 0x80) == 0x80)
1266 *present = 0;
1267 else
1268 *present = 1;
1269 goto out;
1270 } else if (SAS_ADDR(rri_resp+16) == 0) {
1271 *index = i;
1272 *present = 0;
1273 goto out;
1274 }
1275 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1276 phy->last_da_index < i) {
1277 phy->last_da_index = i;
1278 *index = i;
1279 *present = 0;
1280 goto out;
1281 }
1282 }
1283 res = -1;
1284out:
1285 kfree(rri_req);
1286 kfree(rri_resp);
1287 return res;
1288}
1289
1290#define CRI_REQ_SIZE 44
1291#define CRI_RESP_SIZE 8
1292
1293static int sas_configure_set(struct domain_device *dev, int phy_id,
1294 u8 *sas_addr, int index, int include)
1295{
1296 int res;
1297 u8 *cri_req;
1298 u8 *cri_resp;
1299
1300 cri_req = alloc_smp_req(CRI_REQ_SIZE);
1301 if (!cri_req)
1302 return -ENOMEM;
1303
1304 cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1305 if (!cri_resp) {
1306 kfree(cri_req);
1307 return -ENOMEM;
1308 }
1309
1310 cri_req[1] = SMP_CONF_ROUTE_INFO;
1311 *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1312 cri_req[9] = phy_id;
1313 if (SAS_ADDR(sas_addr) == 0 || !include)
1314 cri_req[12] |= 0x80;
1315 memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1316
1317 res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1318 CRI_RESP_SIZE);
1319 if (res)
1320 goto out;
1321 res = cri_resp[2];
1322 if (res == SMP_RESP_NO_INDEX) {
1323 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1324 "index 0x%x\n",
1325 SAS_ADDR(dev->sas_addr), phy_id, index);
1326 }
1327out:
1328 kfree(cri_req);
1329 kfree(cri_resp);
1330 return res;
1331}
1332
1333static int sas_configure_phy(struct domain_device *dev, int phy_id,
1334 u8 *sas_addr, int include)
1335{
1336 int index;
1337 int present;
1338 int res;
1339
1340 res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1341 if (res)
1342 return res;
1343 if (include ^ present)
1344 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1345
1346 return res;
1347}
1348
1349/**
1350 * sas_configure_parent -- configure routing table of parent
1351 * parent: parent expander
1352 * child: child expander
1353 * sas_addr: SAS port identifier of device directly attached to child
1354 */
1355static int sas_configure_parent(struct domain_device *parent,
1356 struct domain_device *child,
1357 u8 *sas_addr, int include)
1358{
1359 struct expander_device *ex_parent = &parent->ex_dev;
1360 int res = 0;
1361 int i;
1362
1363 if (parent->parent) {
1364 res = sas_configure_parent(parent->parent, parent, sas_addr,
1365 include);
1366 if (res)
1367 return res;
1368 }
1369
1370 if (ex_parent->conf_route_table == 0) {
1371 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1372 SAS_ADDR(parent->sas_addr));
1373 return 0;
1374 }
1375
1376 for (i = 0; i < ex_parent->num_phys; i++) {
1377 struct ex_phy *phy = &ex_parent->ex_phy[i];
1378
1379 if ((phy->routing_attr == TABLE_ROUTING) &&
1380 (SAS_ADDR(phy->attached_sas_addr) ==
1381 SAS_ADDR(child->sas_addr))) {
1382 res = sas_configure_phy(parent, i, sas_addr, include);
1383 if (res)
1384 return res;
1385 }
1386 }
1387
1388 return res;
1389}
1390
1391/**
1392 * sas_configure_routing -- configure routing
1393 * dev: expander device
1394 * sas_addr: port identifier of device directly attached to the expander device
1395 */
1396static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1397{
1398 if (dev->parent)
1399 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1400 return 0;
1401}
1402
1403static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
1404{
1405 if (dev->parent)
1406 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1407 return 0;
1408}
1409
2908d778
JB
1410/**
1411 * sas_discover_expander -- expander discovery
1412 * @ex: pointer to expander domain device
1413 *
1414 * See comment in sas_discover_sata().
1415 */
1416static int sas_discover_expander(struct domain_device *dev)
1417{
1418 int res;
1419
1420 res = sas_notify_lldd_dev_found(dev);
1421 if (res)
1422 return res;
1423
1424 res = sas_ex_general(dev);
1425 if (res)
1426 goto out_err;
1427 res = sas_ex_manuf_info(dev);
1428 if (res)
1429 goto out_err;
1430
1431 res = sas_expander_discover(dev);
1432 if (res) {
1433 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1434 SAS_ADDR(dev->sas_addr), res);
1435 goto out_err;
1436 }
1437
1438 sas_check_ex_subtractive_boundary(dev);
1439 res = sas_check_parent_topology(dev);
1440 if (res)
1441 goto out_err;
1442 return 0;
1443out_err:
1444 sas_notify_lldd_dev_gone(dev);
1445 return res;
1446}
1447
1448static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1449{
1450 int res = 0;
1451 struct domain_device *dev;
1452
1453 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1454 if (dev->dev_type == EDGE_DEV ||
1455 dev->dev_type == FANOUT_DEV) {
1456 struct sas_expander_device *ex =
1457 rphy_to_expander_device(dev->rphy);
1458
1459 if (level == ex->level)
1460 res = sas_ex_discover_devices(dev, -1);
1461 else if (level > 0)
1462 res = sas_ex_discover_devices(port->port_dev, -1);
1463
1464 }
1465 }
1466
1467 return res;
1468}
1469
1470static int sas_ex_bfs_disc(struct asd_sas_port *port)
1471{
1472 int res;
1473 int level;
1474
1475 do {
1476 level = port->disc.max_level;
1477 res = sas_ex_level_discovery(port, level);
1478 mb();
1479 } while (level < port->disc.max_level);
1480
1481 return res;
1482}
1483
1484int sas_discover_root_expander(struct domain_device *dev)
1485{
1486 int res;
1487 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1488
bf451207
DW
1489 res = sas_rphy_add(dev->rphy);
1490 if (res)
1491 goto out_err;
2908d778
JB
1492
1493 ex->level = dev->port->disc.max_level; /* 0 */
1494 res = sas_discover_expander(dev);
bf451207
DW
1495 if (res)
1496 goto out_err2;
1497
1498 sas_ex_bfs_disc(dev->port);
2908d778
JB
1499
1500 return res;
bf451207
DW
1501
1502out_err2:
6f63caae 1503 sas_rphy_remove(dev->rphy);
bf451207 1504out_err:
bf451207 1505 return res;
2908d778
JB
1506}
1507
1508/* ---------- Domain revalidation ---------- */
1509
1510static int sas_get_phy_discover(struct domain_device *dev,
1511 int phy_id, struct smp_resp *disc_resp)
1512{
1513 int res;
1514 u8 *disc_req;
1515
1516 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1517 if (!disc_req)
1518 return -ENOMEM;
1519
1520 disc_req[1] = SMP_DISCOVER;
1521 disc_req[9] = phy_id;
1522
1523 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1524 disc_resp, DISCOVER_RESP_SIZE);
1525 if (res)
1526 goto out;
1527 else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1528 res = disc_resp->result;
1529 goto out;
1530 }
1531out:
1532 kfree(disc_req);
1533 return res;
1534}
1535
1536static int sas_get_phy_change_count(struct domain_device *dev,
1537 int phy_id, int *pcc)
1538{
1539 int res;
1540 struct smp_resp *disc_resp;
1541
1542 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1543 if (!disc_resp)
1544 return -ENOMEM;
1545
1546 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1547 if (!res)
1548 *pcc = disc_resp->disc.change_count;
1549
1550 kfree(disc_resp);
1551 return res;
1552}
1553
1554static int sas_get_phy_attached_sas_addr(struct domain_device *dev,
1555 int phy_id, u8 *attached_sas_addr)
1556{
1557 int res;
1558 struct smp_resp *disc_resp;
1559 struct discover_resp *dr;
1560
1561 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1562 if (!disc_resp)
1563 return -ENOMEM;
1564 dr = &disc_resp->disc;
1565
1566 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1567 if (!res) {
1568 memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
1569 if (dr->attached_dev_type == 0)
1570 memset(attached_sas_addr, 0, 8);
1571 }
1572 kfree(disc_resp);
1573 return res;
1574}
1575
1576static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
1577 int from_phy)
1578{
1579 struct expander_device *ex = &dev->ex_dev;
1580 int res = 0;
1581 int i;
1582
1583 for (i = from_phy; i < ex->num_phys; i++) {
1584 int phy_change_count = 0;
1585
1586 res = sas_get_phy_change_count(dev, i, &phy_change_count);
1587 if (res)
1588 goto out;
1589 else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
1590 ex->ex_phy[i].phy_change_count = phy_change_count;
1591 *phy_id = i;
1592 return 0;
1593 }
1594 }
1595out:
1596 return res;
1597}
1598
1599static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1600{
1601 int res;
1602 u8 *rg_req;
1603 struct smp_resp *rg_resp;
1604
1605 rg_req = alloc_smp_req(RG_REQ_SIZE);
1606 if (!rg_req)
1607 return -ENOMEM;
1608
1609 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1610 if (!rg_resp) {
1611 kfree(rg_req);
1612 return -ENOMEM;
1613 }
1614
1615 rg_req[1] = SMP_REPORT_GENERAL;
1616
1617 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1618 RG_RESP_SIZE);
1619 if (res)
1620 goto out;
1621 if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1622 res = rg_resp->result;
1623 goto out;
1624 }
1625
1626 *ecc = be16_to_cpu(rg_resp->rg.change_count);
1627out:
1628 kfree(rg_resp);
1629 kfree(rg_req);
1630 return res;
1631}
1632
1633static int sas_find_bcast_dev(struct domain_device *dev,
1634 struct domain_device **src_dev)
1635{
1636 struct expander_device *ex = &dev->ex_dev;
1637 int ex_change_count = -1;
1638 int res;
1639
1640 res = sas_get_ex_change_count(dev, &ex_change_count);
1641 if (res)
1642 goto out;
1643 if (ex_change_count != -1 &&
1644 ex_change_count != ex->ex_change_count) {
1645 *src_dev = dev;
1646 ex->ex_change_count = ex_change_count;
1647 } else {
1648 struct domain_device *ch;
1649
1650 list_for_each_entry(ch, &ex->children, siblings) {
1651 if (ch->dev_type == EDGE_DEV ||
1652 ch->dev_type == FANOUT_DEV) {
1653 res = sas_find_bcast_dev(ch, src_dev);
1654 if (src_dev)
1655 return res;
1656 }
1657 }
1658 }
1659out:
1660 return res;
1661}
1662
1663static void sas_unregister_ex_tree(struct domain_device *dev)
1664{
1665 struct expander_device *ex = &dev->ex_dev;
1666 struct domain_device *child, *n;
1667
1668 list_for_each_entry_safe(child, n, &ex->children, siblings) {
1669 if (child->dev_type == EDGE_DEV ||
1670 child->dev_type == FANOUT_DEV)
1671 sas_unregister_ex_tree(child);
1672 else
1673 sas_unregister_dev(child);
1674 }
1675 sas_unregister_dev(dev);
1676}
1677
1678static void sas_unregister_devs_sas_addr(struct domain_device *parent,
1679 int phy_id)
1680{
1681 struct expander_device *ex_dev = &parent->ex_dev;
1682 struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
1683 struct domain_device *child, *n;
1684
1685 list_for_each_entry_safe(child, n, &ex_dev->children, siblings) {
1686 if (SAS_ADDR(child->sas_addr) ==
1687 SAS_ADDR(phy->attached_sas_addr)) {
1688 if (child->dev_type == EDGE_DEV ||
1689 child->dev_type == FANOUT_DEV)
1690 sas_unregister_ex_tree(child);
1691 else
1692 sas_unregister_dev(child);
1693 break;
1694 }
1695 }
1696 sas_disable_routing(parent, phy->attached_sas_addr);
1697 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
1698 sas_port_delete_phy(phy->port, phy->phy);
1699 if (phy->port->num_phys == 0)
1700 sas_port_delete(phy->port);
1701 phy->port = NULL;
1702}
1703
1704static int sas_discover_bfs_by_root_level(struct domain_device *root,
1705 const int level)
1706{
1707 struct expander_device *ex_root = &root->ex_dev;
1708 struct domain_device *child;
1709 int res = 0;
1710
1711 list_for_each_entry(child, &ex_root->children, siblings) {
1712 if (child->dev_type == EDGE_DEV ||
1713 child->dev_type == FANOUT_DEV) {
1714 struct sas_expander_device *ex =
1715 rphy_to_expander_device(child->rphy);
1716
1717 if (level > ex->level)
1718 res = sas_discover_bfs_by_root_level(child,
1719 level);
1720 else if (level == ex->level)
1721 res = sas_ex_discover_devices(child, -1);
1722 }
1723 }
1724 return res;
1725}
1726
1727static int sas_discover_bfs_by_root(struct domain_device *dev)
1728{
1729 int res;
1730 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1731 int level = ex->level+1;
1732
1733 res = sas_ex_discover_devices(dev, -1);
1734 if (res)
1735 goto out;
1736 do {
1737 res = sas_discover_bfs_by_root_level(dev, level);
1738 mb();
1739 level += 1;
1740 } while (level <= dev->port->disc.max_level);
1741out:
1742 return res;
1743}
1744
1745static int sas_discover_new(struct domain_device *dev, int phy_id)
1746{
1747 struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1748 struct domain_device *child;
1749 int res;
1750
1751 SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1752 SAS_ADDR(dev->sas_addr), phy_id);
1753 res = sas_ex_phy_discover(dev, phy_id);
1754 if (res)
1755 goto out;
1756 res = sas_ex_discover_devices(dev, phy_id);
1757 if (res)
1758 goto out;
1759 list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1760 if (SAS_ADDR(child->sas_addr) ==
1761 SAS_ADDR(ex_phy->attached_sas_addr)) {
1762 if (child->dev_type == EDGE_DEV ||
1763 child->dev_type == FANOUT_DEV)
1764 res = sas_discover_bfs_by_root(child);
1765 break;
1766 }
1767 }
1768out:
1769 return res;
1770}
1771
1772static int sas_rediscover_dev(struct domain_device *dev, int phy_id)
1773{
1774 struct expander_device *ex = &dev->ex_dev;
1775 struct ex_phy *phy = &ex->ex_phy[phy_id];
1776 u8 attached_sas_addr[8];
1777 int res;
1778
1779 res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
1780 switch (res) {
1781 case SMP_RESP_NO_PHY:
1782 phy->phy_state = PHY_NOT_PRESENT;
1783 sas_unregister_devs_sas_addr(dev, phy_id);
1784 goto out; break;
1785 case SMP_RESP_PHY_VACANT:
1786 phy->phy_state = PHY_VACANT;
1787 sas_unregister_devs_sas_addr(dev, phy_id);
1788 goto out; break;
1789 case SMP_RESP_FUNC_ACC:
1790 break;
1791 }
1792
1793 if (SAS_ADDR(attached_sas_addr) == 0) {
1794 phy->phy_state = PHY_EMPTY;
1795 sas_unregister_devs_sas_addr(dev, phy_id);
1796 } else if (SAS_ADDR(attached_sas_addr) ==
1797 SAS_ADDR(phy->attached_sas_addr)) {
1798 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
1799 SAS_ADDR(dev->sas_addr), phy_id);
a01e70e5 1800 sas_ex_phy_discover(dev, phy_id);
2908d778
JB
1801 } else
1802 res = sas_discover_new(dev, phy_id);
1803out:
1804 return res;
1805}
1806
1807static int sas_rediscover(struct domain_device *dev, const int phy_id)
1808{
1809 struct expander_device *ex = &dev->ex_dev;
1810 struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
1811 int res = 0;
1812 int i;
1813
1814 SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
1815 SAS_ADDR(dev->sas_addr), phy_id);
1816
1817 if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
1818 for (i = 0; i < ex->num_phys; i++) {
1819 struct ex_phy *phy = &ex->ex_phy[i];
1820
1821 if (i == phy_id)
1822 continue;
1823 if (SAS_ADDR(phy->attached_sas_addr) ==
1824 SAS_ADDR(changed_phy->attached_sas_addr)) {
1825 SAS_DPRINTK("phy%d part of wide port with "
1826 "phy%d\n", phy_id, i);
1827 goto out;
1828 }
1829 }
1830 res = sas_rediscover_dev(dev, phy_id);
1831 } else
1832 res = sas_discover_new(dev, phy_id);
1833out:
1834 return res;
1835}
1836
1837/**
1838 * sas_revalidate_domain -- revalidate the domain
1839 * @port: port to the domain of interest
1840 *
1841 * NOTE: this process _must_ quit (return) as soon as any connection
1842 * errors are encountered. Connection recovery is done elsewhere.
1843 * Discover process only interrogates devices in order to discover the
1844 * domain.
1845 */
1846int sas_ex_revalidate_domain(struct domain_device *port_dev)
1847{
1848 int res;
1849 struct domain_device *dev = NULL;
1850
1851 res = sas_find_bcast_dev(port_dev, &dev);
1852 if (res)
1853 goto out;
1854 if (dev) {
1855 struct expander_device *ex = &dev->ex_dev;
1856 int i = 0, phy_id;
1857
1858 do {
1859 phy_id = -1;
1860 res = sas_find_bcast_phy(dev, &phy_id, i);
1861 if (phy_id == -1)
1862 break;
1863 res = sas_rediscover(dev, phy_id);
1864 i = phy_id + 1;
1865 } while (i < ex->num_phys);
1866 }
1867out:
1868 return res;
1869}
1870
ba1fc175
FT
1871int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
1872 struct request *req)
1873{
1874 struct domain_device *dev;
1875 int ret, type = rphy->identify.device_type;
1876 struct request *rsp = req->next_rq;
1877
1878 if (!rsp) {
1879 printk("%s: space for a smp response is missing\n",
1880 __FUNCTION__);
1881 return -EINVAL;
1882 }
1883
1884 /* seems aic94xx doesn't support */
1885 if (!rphy) {
1886 printk("%s: can we send a smp request to a host?\n",
1887 __FUNCTION__);
1888 return -EINVAL;
1889 }
1890
1891 if (type != SAS_EDGE_EXPANDER_DEVICE &&
1892 type != SAS_FANOUT_EXPANDER_DEVICE) {
1893 printk("%s: can we send a smp request to a device?\n",
1894 __FUNCTION__);
1895 return -EINVAL;
1896 }
1897
1898 dev = sas_find_dev_by_rphy(rphy);
1899 if (!dev) {
1900 printk("%s: fail to find a domain_device?\n", __FUNCTION__);
1901 return -EINVAL;
1902 }
1903
1904 /* do we need to support multiple segments? */
1905 if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
1906 printk("%s: multiple segments req %u %u, rsp %u %u\n",
1907 __FUNCTION__, req->bio->bi_vcnt, req->data_len,
1908 rsp->bio->bi_vcnt, rsp->data_len);
1909 return -EINVAL;
1910 }
1911
1912 ret = smp_execute_task(dev, bio_data(req->bio), req->data_len,
1913 bio_data(rsp->bio), rsp->data_len);
1914
1915 return ret;
1916}