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