[SCSI] libsas: SMP request handler shouldn't crash when rphy is NULL
[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
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
34 static int sas_discover_expander(struct domain_device *dev);
35 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr);
36 static int sas_configure_phy(struct domain_device *dev, int phy_id,
37                              u8 *sas_addr, int include);
38 static int sas_disable_routing(struct domain_device *dev,  u8 *sas_addr);
39
40 /* ---------- SMP task management ---------- */
41
42 static 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
55 static 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
65 static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
66                             void *resp, int resp_size)
67 {
68         int res, retry;
69         struct sas_task *task = NULL;
70         struct sas_internal *i =
71                 to_sas_internal(dev->port->ha->core.shost->transportt);
72
73         for (retry = 0; retry < 3; retry++) {
74                 task = sas_alloc_task(GFP_KERNEL);
75                 if (!task)
76                         return -ENOMEM;
77
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);
82
83                 task->task_done = smp_task_done;
84
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);
89
90                 res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
91
92                 if (res) {
93                         del_timer(&task->timer);
94                         SAS_DPRINTK("executing SMP task failed:%d\n", res);
95                         goto ex_err;
96                 }
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                 }
121         }
122 ex_err:
123         BUG_ON(retry == 3 && task != NULL);
124         if (task != NULL) {
125                 sas_free_task(task);
126         }
127         return res;
128 }
129
130 /* ---------- Allocations ---------- */
131
132 static 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
140 static inline void *alloc_smp_resp(int size)
141 {
142         return kzalloc(size, GFP_KERNEL);
143 }
144
145 /* ---------- Expander configuration ---------- */
146
147 static 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;
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;
198         phy->phy->negotiated_linkrate = phy->linkrate;
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
216 static 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 */
238                 sas_smp_phy_control(dev, single, PHY_FUNC_LINK_RESET, NULL);
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
246 static 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) {
266                 res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
267         } else {
268                 int i;
269
270                 for (i = 0; i < ex->num_phys; i++) {
271                         res = sas_ex_phy_discover_helper(dev, disc_req,
272                                                          disc_resp, i);
273                         if (res)
274                                 goto out_err;
275                 }
276         }
277 out_err:
278         kfree(disc_resp);
279         kfree(disc_req);
280         return res;
281 }
282
283 static 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
305 static 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
321 static 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         }
364 out:
365         kfree(rg_req);
366         kfree(rg_resp);
367         return res;
368 }
369
370 static 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
393 static 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);
423 out:
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
432 int sas_smp_phy_control(struct domain_device *dev, int phy_id,
433                         enum phy_func phy_func,
434                         struct sas_phy_linkrates *rates)
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;
453         if (rates) {
454                 pc_req[32] = rates->minimum_linkrate << 4;
455                 pc_req[33] = rates->maximum_linkrate << 4;
456         }
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
465 static 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
470         sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
471         phy->linkrate = SAS_PHY_DISABLED;
472 }
473
474 static 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
491 static 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
507 int 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 #ifdef CONFIG_SCSI_SAS_ATA
539
540 #define RPS_REQ_SIZE  16
541 #define RPS_RESP_SIZE 60
542
543 static int sas_get_report_phy_sata(struct domain_device *dev,
544                                           int phy_id,
545                                           struct smp_resp *rps_resp)
546 {
547         int res;
548         u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
549         u8 *resp = (u8 *)rps_resp;
550
551         if (!rps_req)
552                 return -ENOMEM;
553
554         rps_req[1] = SMP_REPORT_PHY_SATA;
555         rps_req[9] = phy_id;
556
557         res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
558                                     rps_resp, RPS_RESP_SIZE);
559
560         /* 0x34 is the FIS type for the D2H fis.  There's a potential
561          * standards cockup here.  sas-2 explicitly specifies the FIS
562          * should be encoded so that FIS type is in resp[24].
563          * However, some expanders endian reverse this.  Undo the
564          * reversal here */
565         if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
566                 int i;
567
568                 for (i = 0; i < 5; i++) {
569                         int j = 24 + (i*4);
570                         u8 a, b;
571                         a = resp[j + 0];
572                         b = resp[j + 1];
573                         resp[j + 0] = resp[j + 3];
574                         resp[j + 1] = resp[j + 2];
575                         resp[j + 2] = b;
576                         resp[j + 3] = a;
577                 }
578         }
579
580         kfree(rps_req);
581         return res;
582 }
583 #endif
584
585 static void sas_ex_get_linkrate(struct domain_device *parent,
586                                        struct domain_device *child,
587                                        struct ex_phy *parent_phy)
588 {
589         struct expander_device *parent_ex = &parent->ex_dev;
590         struct sas_port *port;
591         int i;
592
593         child->pathways = 0;
594
595         port = parent_phy->port;
596
597         for (i = 0; i < parent_ex->num_phys; i++) {
598                 struct ex_phy *phy = &parent_ex->ex_phy[i];
599
600                 if (phy->phy_state == PHY_VACANT ||
601                     phy->phy_state == PHY_NOT_PRESENT)
602                         continue;
603
604                 if (SAS_ADDR(phy->attached_sas_addr) ==
605                     SAS_ADDR(child->sas_addr)) {
606
607                         child->min_linkrate = min(parent->min_linkrate,
608                                                   phy->linkrate);
609                         child->max_linkrate = max(parent->max_linkrate,
610                                                   phy->linkrate);
611                         child->pathways++;
612                         sas_port_add_phy(port, phy->phy);
613                 }
614         }
615         child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
616         child->pathways = min(child->pathways, parent->pathways);
617 }
618
619 static struct domain_device *sas_ex_discover_end_dev(
620         struct domain_device *parent, int phy_id)
621 {
622         struct expander_device *parent_ex = &parent->ex_dev;
623         struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
624         struct domain_device *child = NULL;
625         struct sas_rphy *rphy;
626         int res;
627
628         if (phy->attached_sata_host || phy->attached_sata_ps)
629                 return NULL;
630
631         child = kzalloc(sizeof(*child), GFP_KERNEL);
632         if (!child)
633                 return NULL;
634
635         child->parent = parent;
636         child->port   = parent->port;
637         child->iproto = phy->attached_iproto;
638         memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
639         sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
640         if (!phy->port) {
641                 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
642                 if (unlikely(!phy->port))
643                         goto out_err;
644                 if (unlikely(sas_port_add(phy->port) != 0)) {
645                         sas_port_free(phy->port);
646                         goto out_err;
647                 }
648         }
649         sas_ex_get_linkrate(parent, child, phy);
650
651 #ifdef CONFIG_SCSI_SAS_ATA
652         if ((phy->attached_tproto & SAS_PROTO_STP) || phy->attached_sata_dev) {
653                 child->dev_type = SATA_DEV;
654                 if (phy->attached_tproto & SAS_PROTO_STP)
655                         child->tproto = phy->attached_tproto;
656                 if (phy->attached_sata_dev)
657                         child->tproto |= SATA_DEV;
658                 res = sas_get_report_phy_sata(parent, phy_id,
659                                               &child->sata_dev.rps_resp);
660                 if (res) {
661                         SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
662                                     "0x%x\n", SAS_ADDR(parent->sas_addr),
663                                     phy_id, res);
664                         goto out_free;
665                 }
666                 memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
667                        sizeof(struct dev_to_host_fis));
668
669                 rphy = sas_end_device_alloc(phy->port);
670                 if (unlikely(!rphy))
671                         goto out_free;
672
673                 sas_init_dev(child);
674
675                 child->rphy = rphy;
676
677                 spin_lock_irq(&parent->port->dev_list_lock);
678                 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
679                 spin_unlock_irq(&parent->port->dev_list_lock);
680
681                 res = sas_discover_sata(child);
682                 if (res) {
683                         SAS_DPRINTK("sas_discover_sata() for device %16llx at "
684                                     "%016llx:0x%x returned 0x%x\n",
685                                     SAS_ADDR(child->sas_addr),
686                                     SAS_ADDR(parent->sas_addr), phy_id, res);
687                         goto out_list_del;
688                 }
689         } else
690 #endif
691           if (phy->attached_tproto & SAS_PROTO_SSP) {
692                 child->dev_type = SAS_END_DEV;
693                 rphy = sas_end_device_alloc(phy->port);
694                 /* FIXME: error handling */
695                 if (unlikely(!rphy))
696                         goto out_free;
697                 child->tproto = phy->attached_tproto;
698                 sas_init_dev(child);
699
700                 child->rphy = rphy;
701                 sas_fill_in_rphy(child, rphy);
702
703                 spin_lock_irq(&parent->port->dev_list_lock);
704                 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
705                 spin_unlock_irq(&parent->port->dev_list_lock);
706
707                 res = sas_discover_end_dev(child);
708                 if (res) {
709                         SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
710                                     "at %016llx:0x%x returned 0x%x\n",
711                                     SAS_ADDR(child->sas_addr),
712                                     SAS_ADDR(parent->sas_addr), phy_id, res);
713                         goto out_list_del;
714                 }
715         } else {
716                 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
717                             phy->attached_tproto, SAS_ADDR(parent->sas_addr),
718                             phy_id);
719                 goto out_free;
720         }
721
722         list_add_tail(&child->siblings, &parent_ex->children);
723         return child;
724
725  out_list_del:
726         sas_rphy_free(child->rphy);
727         child->rphy = NULL;
728         list_del(&child->dev_list_node);
729  out_free:
730         sas_port_delete(phy->port);
731  out_err:
732         phy->port = NULL;
733         kfree(child);
734         return NULL;
735 }
736
737 /* See if this phy is part of a wide port */
738 static int sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
739 {
740         struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
741         int i;
742
743         for (i = 0; i < parent->ex_dev.num_phys; i++) {
744                 struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
745
746                 if (ephy == phy)
747                         continue;
748
749                 if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
750                             SAS_ADDR_SIZE) && ephy->port) {
751                         sas_port_add_phy(ephy->port, phy->phy);
752                         phy->phy_state = PHY_DEVICE_DISCOVERED;
753                         return 0;
754                 }
755         }
756
757         return -ENODEV;
758 }
759
760 static struct domain_device *sas_ex_discover_expander(
761         struct domain_device *parent, int phy_id)
762 {
763         struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
764         struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
765         struct domain_device *child = NULL;
766         struct sas_rphy *rphy;
767         struct sas_expander_device *edev;
768         struct asd_sas_port *port;
769         int res;
770
771         if (phy->routing_attr == DIRECT_ROUTING) {
772                 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
773                             "allowed\n",
774                             SAS_ADDR(parent->sas_addr), phy_id,
775                             SAS_ADDR(phy->attached_sas_addr),
776                             phy->attached_phy_id);
777                 return NULL;
778         }
779         child = kzalloc(sizeof(*child), GFP_KERNEL);
780         if (!child)
781                 return NULL;
782
783         phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
784         /* FIXME: better error handling */
785         BUG_ON(sas_port_add(phy->port) != 0);
786
787
788         switch (phy->attached_dev_type) {
789         case EDGE_DEV:
790                 rphy = sas_expander_alloc(phy->port,
791                                           SAS_EDGE_EXPANDER_DEVICE);
792                 break;
793         case FANOUT_DEV:
794                 rphy = sas_expander_alloc(phy->port,
795                                           SAS_FANOUT_EXPANDER_DEVICE);
796                 break;
797         default:
798                 rphy = NULL;    /* shut gcc up */
799                 BUG();
800         }
801         port = parent->port;
802         child->rphy = rphy;
803         edev = rphy_to_expander_device(rphy);
804         child->dev_type = phy->attached_dev_type;
805         child->parent = parent;
806         child->port = port;
807         child->iproto = phy->attached_iproto;
808         child->tproto = phy->attached_tproto;
809         memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
810         sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
811         sas_ex_get_linkrate(parent, child, phy);
812         edev->level = parent_ex->level + 1;
813         parent->port->disc.max_level = max(parent->port->disc.max_level,
814                                            edev->level);
815         sas_init_dev(child);
816         sas_fill_in_rphy(child, rphy);
817         sas_rphy_add(rphy);
818
819         spin_lock_irq(&parent->port->dev_list_lock);
820         list_add_tail(&child->dev_list_node, &parent->port->dev_list);
821         spin_unlock_irq(&parent->port->dev_list_lock);
822
823         res = sas_discover_expander(child);
824         if (res) {
825                 kfree(child);
826                 return NULL;
827         }
828         list_add_tail(&child->siblings, &parent->ex_dev.children);
829         return child;
830 }
831
832 static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
833 {
834         struct expander_device *ex = &dev->ex_dev;
835         struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
836         struct domain_device *child = NULL;
837         int res = 0;
838
839         /* Phy state */
840         if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
841                 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
842                         res = sas_ex_phy_discover(dev, phy_id);
843                 if (res)
844                         return res;
845         }
846
847         /* Parent and domain coherency */
848         if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
849                              SAS_ADDR(dev->port->sas_addr))) {
850                 sas_add_parent_port(dev, phy_id);
851                 return 0;
852         }
853         if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
854                             SAS_ADDR(dev->parent->sas_addr))) {
855                 sas_add_parent_port(dev, phy_id);
856                 if (ex_phy->routing_attr == TABLE_ROUTING)
857                         sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
858                 return 0;
859         }
860
861         if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
862                 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
863
864         if (ex_phy->attached_dev_type == NO_DEVICE) {
865                 if (ex_phy->routing_attr == DIRECT_ROUTING) {
866                         memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
867                         sas_configure_routing(dev, ex_phy->attached_sas_addr);
868                 }
869                 return 0;
870         } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
871                 return 0;
872
873         if (ex_phy->attached_dev_type != SAS_END_DEV &&
874             ex_phy->attached_dev_type != FANOUT_DEV &&
875             ex_phy->attached_dev_type != EDGE_DEV) {
876                 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
877                             "phy 0x%x\n", ex_phy->attached_dev_type,
878                             SAS_ADDR(dev->sas_addr),
879                             phy_id);
880                 return 0;
881         }
882
883         res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
884         if (res) {
885                 SAS_DPRINTK("configure routing for dev %016llx "
886                             "reported 0x%x. Forgotten\n",
887                             SAS_ADDR(ex_phy->attached_sas_addr), res);
888                 sas_disable_routing(dev, ex_phy->attached_sas_addr);
889                 return res;
890         }
891
892         res = sas_ex_join_wide_port(dev, phy_id);
893         if (!res) {
894                 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
895                             phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
896                 return res;
897         }
898
899         switch (ex_phy->attached_dev_type) {
900         case SAS_END_DEV:
901                 child = sas_ex_discover_end_dev(dev, phy_id);
902                 break;
903         case FANOUT_DEV:
904                 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
905                         SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
906                                     "attached to ex %016llx phy 0x%x\n",
907                                     SAS_ADDR(ex_phy->attached_sas_addr),
908                                     ex_phy->attached_phy_id,
909                                     SAS_ADDR(dev->sas_addr),
910                                     phy_id);
911                         sas_ex_disable_phy(dev, phy_id);
912                         break;
913                 } else
914                         memcpy(dev->port->disc.fanout_sas_addr,
915                                ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
916                 /* fallthrough */
917         case EDGE_DEV:
918                 child = sas_ex_discover_expander(dev, phy_id);
919                 break;
920         default:
921                 break;
922         }
923
924         if (child) {
925                 int i;
926
927                 for (i = 0; i < ex->num_phys; i++) {
928                         if (ex->ex_phy[i].phy_state == PHY_VACANT ||
929                             ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
930                                 continue;
931
932                         if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
933                             SAS_ADDR(child->sas_addr))
934                                 ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
935                 }
936         }
937
938         return res;
939 }
940
941 static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
942 {
943         struct expander_device *ex = &dev->ex_dev;
944         int i;
945
946         for (i = 0; i < ex->num_phys; i++) {
947                 struct ex_phy *phy = &ex->ex_phy[i];
948
949                 if (phy->phy_state == PHY_VACANT ||
950                     phy->phy_state == PHY_NOT_PRESENT)
951                         continue;
952
953                 if ((phy->attached_dev_type == EDGE_DEV ||
954                      phy->attached_dev_type == FANOUT_DEV) &&
955                     phy->routing_attr == SUBTRACTIVE_ROUTING) {
956
957                         memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
958
959                         return 1;
960                 }
961         }
962         return 0;
963 }
964
965 static int sas_check_level_subtractive_boundary(struct domain_device *dev)
966 {
967         struct expander_device *ex = &dev->ex_dev;
968         struct domain_device *child;
969         u8 sub_addr[8] = {0, };
970
971         list_for_each_entry(child, &ex->children, siblings) {
972                 if (child->dev_type != EDGE_DEV &&
973                     child->dev_type != FANOUT_DEV)
974                         continue;
975                 if (sub_addr[0] == 0) {
976                         sas_find_sub_addr(child, sub_addr);
977                         continue;
978                 } else {
979                         u8 s2[8];
980
981                         if (sas_find_sub_addr(child, s2) &&
982                             (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
983
984                                 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
985                                             "diverges from subtractive "
986                                             "boundary %016llx\n",
987                                             SAS_ADDR(dev->sas_addr),
988                                             SAS_ADDR(child->sas_addr),
989                                             SAS_ADDR(s2),
990                                             SAS_ADDR(sub_addr));
991
992                                 sas_ex_disable_port(child, s2);
993                         }
994                 }
995         }
996         return 0;
997 }
998 /**
999  * sas_ex_discover_devices -- discover devices attached to this expander
1000  * dev: pointer to the expander domain device
1001  * single: if you want to do a single phy, else set to -1;
1002  *
1003  * Configure this expander for use with its devices and register the
1004  * devices of this expander.
1005  */
1006 static int sas_ex_discover_devices(struct domain_device *dev, int single)
1007 {
1008         struct expander_device *ex = &dev->ex_dev;
1009         int i = 0, end = ex->num_phys;
1010         int res = 0;
1011
1012         if (0 <= single && single < end) {
1013                 i = single;
1014                 end = i+1;
1015         }
1016
1017         for ( ; i < end; i++) {
1018                 struct ex_phy *ex_phy = &ex->ex_phy[i];
1019
1020                 if (ex_phy->phy_state == PHY_VACANT ||
1021                     ex_phy->phy_state == PHY_NOT_PRESENT ||
1022                     ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
1023                         continue;
1024
1025                 switch (ex_phy->linkrate) {
1026                 case SAS_PHY_DISABLED:
1027                 case SAS_PHY_RESET_PROBLEM:
1028                 case SAS_SATA_PORT_SELECTOR:
1029                         continue;
1030                 default:
1031                         res = sas_ex_discover_dev(dev, i);
1032                         if (res)
1033                                 break;
1034                         continue;
1035                 }
1036         }
1037
1038         if (!res)
1039                 sas_check_level_subtractive_boundary(dev);
1040
1041         return res;
1042 }
1043
1044 static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
1045 {
1046         struct expander_device *ex = &dev->ex_dev;
1047         int i;
1048         u8  *sub_sas_addr = NULL;
1049
1050         if (dev->dev_type != EDGE_DEV)
1051                 return 0;
1052
1053         for (i = 0; i < ex->num_phys; i++) {
1054                 struct ex_phy *phy = &ex->ex_phy[i];
1055
1056                 if (phy->phy_state == PHY_VACANT ||
1057                     phy->phy_state == PHY_NOT_PRESENT)
1058                         continue;
1059
1060                 if ((phy->attached_dev_type == FANOUT_DEV ||
1061                      phy->attached_dev_type == EDGE_DEV) &&
1062                     phy->routing_attr == SUBTRACTIVE_ROUTING) {
1063
1064                         if (!sub_sas_addr)
1065                                 sub_sas_addr = &phy->attached_sas_addr[0];
1066                         else if (SAS_ADDR(sub_sas_addr) !=
1067                                  SAS_ADDR(phy->attached_sas_addr)) {
1068
1069                                 SAS_DPRINTK("ex %016llx phy 0x%x "
1070                                             "diverges(%016llx) on subtractive "
1071                                             "boundary(%016llx). Disabled\n",
1072                                             SAS_ADDR(dev->sas_addr), i,
1073                                             SAS_ADDR(phy->attached_sas_addr),
1074                                             SAS_ADDR(sub_sas_addr));
1075                                 sas_ex_disable_phy(dev, i);
1076                         }
1077                 }
1078         }
1079         return 0;
1080 }
1081
1082 static void sas_print_parent_topology_bug(struct domain_device *child,
1083                                                  struct ex_phy *parent_phy,
1084                                                  struct ex_phy *child_phy)
1085 {
1086         static const char ra_char[] = {
1087                 [DIRECT_ROUTING] = 'D',
1088                 [SUBTRACTIVE_ROUTING] = 'S',
1089                 [TABLE_ROUTING] = 'T',
1090         };
1091         static const char *ex_type[] = {
1092                 [EDGE_DEV] = "edge",
1093                 [FANOUT_DEV] = "fanout",
1094         };
1095         struct domain_device *parent = child->parent;
1096
1097         sas_printk("%s ex %016llx phy 0x%x <--> %s ex %016llx phy 0x%x "
1098                    "has %c:%c routing link!\n",
1099
1100                    ex_type[parent->dev_type],
1101                    SAS_ADDR(parent->sas_addr),
1102                    parent_phy->phy_id,
1103
1104                    ex_type[child->dev_type],
1105                    SAS_ADDR(child->sas_addr),
1106                    child_phy->phy_id,
1107
1108                    ra_char[parent_phy->routing_attr],
1109                    ra_char[child_phy->routing_attr]);
1110 }
1111
1112 static int sas_check_eeds(struct domain_device *child,
1113                                  struct ex_phy *parent_phy,
1114                                  struct ex_phy *child_phy)
1115 {
1116         int res = 0;
1117         struct domain_device *parent = child->parent;
1118
1119         if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1120                 res = -ENODEV;
1121                 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1122                             "phy S:0x%x, while there is a fanout ex %016llx\n",
1123                             SAS_ADDR(parent->sas_addr),
1124                             parent_phy->phy_id,
1125                             SAS_ADDR(child->sas_addr),
1126                             child_phy->phy_id,
1127                             SAS_ADDR(parent->port->disc.fanout_sas_addr));
1128         } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1129                 memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1130                        SAS_ADDR_SIZE);
1131                 memcpy(parent->port->disc.eeds_b, child->sas_addr,
1132                        SAS_ADDR_SIZE);
1133         } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1134                     SAS_ADDR(parent->sas_addr)) ||
1135                    (SAS_ADDR(parent->port->disc.eeds_a) ==
1136                     SAS_ADDR(child->sas_addr)))
1137                    &&
1138                    ((SAS_ADDR(parent->port->disc.eeds_b) ==
1139                      SAS_ADDR(parent->sas_addr)) ||
1140                     (SAS_ADDR(parent->port->disc.eeds_b) ==
1141                      SAS_ADDR(child->sas_addr))))
1142                 ;
1143         else {
1144                 res = -ENODEV;
1145                 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1146                             "phy 0x%x link forms a third EEDS!\n",
1147                             SAS_ADDR(parent->sas_addr),
1148                             parent_phy->phy_id,
1149                             SAS_ADDR(child->sas_addr),
1150                             child_phy->phy_id);
1151         }
1152
1153         return res;
1154 }
1155
1156 /* Here we spill over 80 columns.  It is intentional.
1157  */
1158 static int sas_check_parent_topology(struct domain_device *child)
1159 {
1160         struct expander_device *child_ex = &child->ex_dev;
1161         struct expander_device *parent_ex;
1162         int i;
1163         int res = 0;
1164
1165         if (!child->parent)
1166                 return 0;
1167
1168         if (child->parent->dev_type != EDGE_DEV &&
1169             child->parent->dev_type != FANOUT_DEV)
1170                 return 0;
1171
1172         parent_ex = &child->parent->ex_dev;
1173
1174         for (i = 0; i < parent_ex->num_phys; i++) {
1175                 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1176                 struct ex_phy *child_phy;
1177
1178                 if (parent_phy->phy_state == PHY_VACANT ||
1179                     parent_phy->phy_state == PHY_NOT_PRESENT)
1180                         continue;
1181
1182                 if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1183                         continue;
1184
1185                 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1186
1187                 switch (child->parent->dev_type) {
1188                 case EDGE_DEV:
1189                         if (child->dev_type == FANOUT_DEV) {
1190                                 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1191                                     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 == SUBTRACTIVE_ROUTING) {
1196                                 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1197                                         res = sas_check_eeds(child, parent_phy, child_phy);
1198                                 } else if (child_phy->routing_attr != TABLE_ROUTING) {
1199                                         sas_print_parent_topology_bug(child, parent_phy, child_phy);
1200                                         res = -ENODEV;
1201                                 }
1202                         } else 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                 case FANOUT_DEV:
1209                         if (parent_phy->routing_attr != TABLE_ROUTING ||
1210                             child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1211                                 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1212                                 res = -ENODEV;
1213                         }
1214                         break;
1215                 default:
1216                         break;
1217                 }
1218         }
1219
1220         return res;
1221 }
1222
1223 #define RRI_REQ_SIZE  16
1224 #define RRI_RESP_SIZE 44
1225
1226 static int sas_configure_present(struct domain_device *dev, int phy_id,
1227                                  u8 *sas_addr, int *index, int *present)
1228 {
1229         int i, res = 0;
1230         struct expander_device *ex = &dev->ex_dev;
1231         struct ex_phy *phy = &ex->ex_phy[phy_id];
1232         u8 *rri_req;
1233         u8 *rri_resp;
1234
1235         *present = 0;
1236         *index = 0;
1237
1238         rri_req = alloc_smp_req(RRI_REQ_SIZE);
1239         if (!rri_req)
1240                 return -ENOMEM;
1241
1242         rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1243         if (!rri_resp) {
1244                 kfree(rri_req);
1245                 return -ENOMEM;
1246         }
1247
1248         rri_req[1] = SMP_REPORT_ROUTE_INFO;
1249         rri_req[9] = phy_id;
1250
1251         for (i = 0; i < ex->max_route_indexes ; i++) {
1252                 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1253                 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1254                                        RRI_RESP_SIZE);
1255                 if (res)
1256                         goto out;
1257                 res = rri_resp[2];
1258                 if (res == SMP_RESP_NO_INDEX) {
1259                         SAS_DPRINTK("overflow of indexes: dev %016llx "
1260                                     "phy 0x%x index 0x%x\n",
1261                                     SAS_ADDR(dev->sas_addr), phy_id, i);
1262                         goto out;
1263                 } else if (res != SMP_RESP_FUNC_ACC) {
1264                         SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
1265                                     "result 0x%x\n", __FUNCTION__,
1266                                     SAS_ADDR(dev->sas_addr), phy_id, i, res);
1267                         goto out;
1268                 }
1269                 if (SAS_ADDR(sas_addr) != 0) {
1270                         if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1271                                 *index = i;
1272                                 if ((rri_resp[12] & 0x80) == 0x80)
1273                                         *present = 0;
1274                                 else
1275                                         *present = 1;
1276                                 goto out;
1277                         } else if (SAS_ADDR(rri_resp+16) == 0) {
1278                                 *index = i;
1279                                 *present = 0;
1280                                 goto out;
1281                         }
1282                 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1283                            phy->last_da_index < i) {
1284                         phy->last_da_index = i;
1285                         *index = i;
1286                         *present = 0;
1287                         goto out;
1288                 }
1289         }
1290         res = -1;
1291 out:
1292         kfree(rri_req);
1293         kfree(rri_resp);
1294         return res;
1295 }
1296
1297 #define CRI_REQ_SIZE  44
1298 #define CRI_RESP_SIZE  8
1299
1300 static int sas_configure_set(struct domain_device *dev, int phy_id,
1301                              u8 *sas_addr, int index, int include)
1302 {
1303         int res;
1304         u8 *cri_req;
1305         u8 *cri_resp;
1306
1307         cri_req = alloc_smp_req(CRI_REQ_SIZE);
1308         if (!cri_req)
1309                 return -ENOMEM;
1310
1311         cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1312         if (!cri_resp) {
1313                 kfree(cri_req);
1314                 return -ENOMEM;
1315         }
1316
1317         cri_req[1] = SMP_CONF_ROUTE_INFO;
1318         *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1319         cri_req[9] = phy_id;
1320         if (SAS_ADDR(sas_addr) == 0 || !include)
1321                 cri_req[12] |= 0x80;
1322         memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1323
1324         res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1325                                CRI_RESP_SIZE);
1326         if (res)
1327                 goto out;
1328         res = cri_resp[2];
1329         if (res == SMP_RESP_NO_INDEX) {
1330                 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1331                             "index 0x%x\n",
1332                             SAS_ADDR(dev->sas_addr), phy_id, index);
1333         }
1334 out:
1335         kfree(cri_req);
1336         kfree(cri_resp);
1337         return res;
1338 }
1339
1340 static int sas_configure_phy(struct domain_device *dev, int phy_id,
1341                                     u8 *sas_addr, int include)
1342 {
1343         int index;
1344         int present;
1345         int res;
1346
1347         res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1348         if (res)
1349                 return res;
1350         if (include ^ present)
1351                 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1352
1353         return res;
1354 }
1355
1356 /**
1357  * sas_configure_parent -- configure routing table of parent
1358  * parent: parent expander
1359  * child: child expander
1360  * sas_addr: SAS port identifier of device directly attached to child
1361  */
1362 static int sas_configure_parent(struct domain_device *parent,
1363                                 struct domain_device *child,
1364                                 u8 *sas_addr, int include)
1365 {
1366         struct expander_device *ex_parent = &parent->ex_dev;
1367         int res = 0;
1368         int i;
1369
1370         if (parent->parent) {
1371                 res = sas_configure_parent(parent->parent, parent, sas_addr,
1372                                            include);
1373                 if (res)
1374                         return res;
1375         }
1376
1377         if (ex_parent->conf_route_table == 0) {
1378                 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1379                             SAS_ADDR(parent->sas_addr));
1380                 return 0;
1381         }
1382
1383         for (i = 0; i < ex_parent->num_phys; i++) {
1384                 struct ex_phy *phy = &ex_parent->ex_phy[i];
1385
1386                 if ((phy->routing_attr == TABLE_ROUTING) &&
1387                     (SAS_ADDR(phy->attached_sas_addr) ==
1388                      SAS_ADDR(child->sas_addr))) {
1389                         res = sas_configure_phy(parent, i, sas_addr, include);
1390                         if (res)
1391                                 return res;
1392                 }
1393         }
1394
1395         return res;
1396 }
1397
1398 /**
1399  * sas_configure_routing -- configure routing
1400  * dev: expander device
1401  * sas_addr: port identifier of device directly attached to the expander device
1402  */
1403 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1404 {
1405         if (dev->parent)
1406                 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1407         return 0;
1408 }
1409
1410 static int sas_disable_routing(struct domain_device *dev,  u8 *sas_addr)
1411 {
1412         if (dev->parent)
1413                 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1414         return 0;
1415 }
1416
1417 /**
1418  * sas_discover_expander -- expander discovery
1419  * @ex: pointer to expander domain device
1420  *
1421  * See comment in sas_discover_sata().
1422  */
1423 static int sas_discover_expander(struct domain_device *dev)
1424 {
1425         int res;
1426
1427         res = sas_notify_lldd_dev_found(dev);
1428         if (res)
1429                 return res;
1430
1431         res = sas_ex_general(dev);
1432         if (res)
1433                 goto out_err;
1434         res = sas_ex_manuf_info(dev);
1435         if (res)
1436                 goto out_err;
1437
1438         res = sas_expander_discover(dev);
1439         if (res) {
1440                 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1441                             SAS_ADDR(dev->sas_addr), res);
1442                 goto out_err;
1443         }
1444
1445         sas_check_ex_subtractive_boundary(dev);
1446         res = sas_check_parent_topology(dev);
1447         if (res)
1448                 goto out_err;
1449         return 0;
1450 out_err:
1451         sas_notify_lldd_dev_gone(dev);
1452         return res;
1453 }
1454
1455 static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1456 {
1457         int res = 0;
1458         struct domain_device *dev;
1459
1460         list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1461                 if (dev->dev_type == EDGE_DEV ||
1462                     dev->dev_type == FANOUT_DEV) {
1463                         struct sas_expander_device *ex =
1464                                 rphy_to_expander_device(dev->rphy);
1465
1466                         if (level == ex->level)
1467                                 res = sas_ex_discover_devices(dev, -1);
1468                         else if (level > 0)
1469                                 res = sas_ex_discover_devices(port->port_dev, -1);
1470
1471                 }
1472         }
1473
1474         return res;
1475 }
1476
1477 static int sas_ex_bfs_disc(struct asd_sas_port *port)
1478 {
1479         int res;
1480         int level;
1481
1482         do {
1483                 level = port->disc.max_level;
1484                 res = sas_ex_level_discovery(port, level);
1485                 mb();
1486         } while (level < port->disc.max_level);
1487
1488         return res;
1489 }
1490
1491 int sas_discover_root_expander(struct domain_device *dev)
1492 {
1493         int res;
1494         struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1495
1496         res = sas_rphy_add(dev->rphy);
1497         if (res)
1498                 goto out_err;
1499
1500         ex->level = dev->port->disc.max_level; /* 0 */
1501         res = sas_discover_expander(dev);
1502         if (res)
1503                 goto out_err2;
1504
1505         sas_ex_bfs_disc(dev->port);
1506
1507         return res;
1508
1509 out_err2:
1510         sas_rphy_remove(dev->rphy);
1511 out_err:
1512         return res;
1513 }
1514
1515 /* ---------- Domain revalidation ---------- */
1516
1517 static int sas_get_phy_discover(struct domain_device *dev,
1518                                 int phy_id, struct smp_resp *disc_resp)
1519 {
1520         int res;
1521         u8 *disc_req;
1522
1523         disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1524         if (!disc_req)
1525                 return -ENOMEM;
1526
1527         disc_req[1] = SMP_DISCOVER;
1528         disc_req[9] = phy_id;
1529
1530         res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1531                                disc_resp, DISCOVER_RESP_SIZE);
1532         if (res)
1533                 goto out;
1534         else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1535                 res = disc_resp->result;
1536                 goto out;
1537         }
1538 out:
1539         kfree(disc_req);
1540         return res;
1541 }
1542
1543 static int sas_get_phy_change_count(struct domain_device *dev,
1544                                     int phy_id, int *pcc)
1545 {
1546         int res;
1547         struct smp_resp *disc_resp;
1548
1549         disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1550         if (!disc_resp)
1551                 return -ENOMEM;
1552
1553         res = sas_get_phy_discover(dev, phy_id, disc_resp);
1554         if (!res)
1555                 *pcc = disc_resp->disc.change_count;
1556
1557         kfree(disc_resp);
1558         return res;
1559 }
1560
1561 static int sas_get_phy_attached_sas_addr(struct domain_device *dev,
1562                                          int phy_id, u8 *attached_sas_addr)
1563 {
1564         int res;
1565         struct smp_resp *disc_resp;
1566         struct discover_resp *dr;
1567
1568         disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1569         if (!disc_resp)
1570                 return -ENOMEM;
1571         dr = &disc_resp->disc;
1572
1573         res = sas_get_phy_discover(dev, phy_id, disc_resp);
1574         if (!res) {
1575                 memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
1576                 if (dr->attached_dev_type == 0)
1577                         memset(attached_sas_addr, 0, 8);
1578         }
1579         kfree(disc_resp);
1580         return res;
1581 }
1582
1583 static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
1584                               int from_phy)
1585 {
1586         struct expander_device *ex = &dev->ex_dev;
1587         int res = 0;
1588         int i;
1589
1590         for (i = from_phy; i < ex->num_phys; i++) {
1591                 int phy_change_count = 0;
1592
1593                 res = sas_get_phy_change_count(dev, i, &phy_change_count);
1594                 if (res)
1595                         goto out;
1596                 else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
1597                         ex->ex_phy[i].phy_change_count = phy_change_count;
1598                         *phy_id = i;
1599                         return 0;
1600                 }
1601         }
1602 out:
1603         return res;
1604 }
1605
1606 static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1607 {
1608         int res;
1609         u8  *rg_req;
1610         struct smp_resp  *rg_resp;
1611
1612         rg_req = alloc_smp_req(RG_REQ_SIZE);
1613         if (!rg_req)
1614                 return -ENOMEM;
1615
1616         rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1617         if (!rg_resp) {
1618                 kfree(rg_req);
1619                 return -ENOMEM;
1620         }
1621
1622         rg_req[1] = SMP_REPORT_GENERAL;
1623
1624         res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1625                                RG_RESP_SIZE);
1626         if (res)
1627                 goto out;
1628         if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1629                 res = rg_resp->result;
1630                 goto out;
1631         }
1632
1633         *ecc = be16_to_cpu(rg_resp->rg.change_count);
1634 out:
1635         kfree(rg_resp);
1636         kfree(rg_req);
1637         return res;
1638 }
1639
1640 static int sas_find_bcast_dev(struct domain_device *dev,
1641                               struct domain_device **src_dev)
1642 {
1643         struct expander_device *ex = &dev->ex_dev;
1644         int ex_change_count = -1;
1645         int res;
1646
1647         res = sas_get_ex_change_count(dev, &ex_change_count);
1648         if (res)
1649                 goto out;
1650         if (ex_change_count != -1 &&
1651             ex_change_count != ex->ex_change_count) {
1652                 *src_dev = dev;
1653                 ex->ex_change_count = ex_change_count;
1654         } else {
1655                 struct domain_device *ch;
1656
1657                 list_for_each_entry(ch, &ex->children, siblings) {
1658                         if (ch->dev_type == EDGE_DEV ||
1659                             ch->dev_type == FANOUT_DEV) {
1660                                 res = sas_find_bcast_dev(ch, src_dev);
1661                                 if (src_dev)
1662                                         return res;
1663                         }
1664                 }
1665         }
1666 out:
1667         return res;
1668 }
1669
1670 static void sas_unregister_ex_tree(struct domain_device *dev)
1671 {
1672         struct expander_device *ex = &dev->ex_dev;
1673         struct domain_device *child, *n;
1674
1675         list_for_each_entry_safe(child, n, &ex->children, siblings) {
1676                 if (child->dev_type == EDGE_DEV ||
1677                     child->dev_type == FANOUT_DEV)
1678                         sas_unregister_ex_tree(child);
1679                 else
1680                         sas_unregister_dev(child);
1681         }
1682         sas_unregister_dev(dev);
1683 }
1684
1685 static void sas_unregister_devs_sas_addr(struct domain_device *parent,
1686                                          int phy_id)
1687 {
1688         struct expander_device *ex_dev = &parent->ex_dev;
1689         struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
1690         struct domain_device *child, *n;
1691
1692         list_for_each_entry_safe(child, n, &ex_dev->children, siblings) {
1693                 if (SAS_ADDR(child->sas_addr) ==
1694                     SAS_ADDR(phy->attached_sas_addr)) {
1695                         if (child->dev_type == EDGE_DEV ||
1696                             child->dev_type == FANOUT_DEV)
1697                                 sas_unregister_ex_tree(child);
1698                         else
1699                                 sas_unregister_dev(child);
1700                         break;
1701                 }
1702         }
1703         sas_disable_routing(parent, phy->attached_sas_addr);
1704         memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
1705         sas_port_delete_phy(phy->port, phy->phy);
1706         if (phy->port->num_phys == 0)
1707                 sas_port_delete(phy->port);
1708         phy->port = NULL;
1709 }
1710
1711 static int sas_discover_bfs_by_root_level(struct domain_device *root,
1712                                           const int level)
1713 {
1714         struct expander_device *ex_root = &root->ex_dev;
1715         struct domain_device *child;
1716         int res = 0;
1717
1718         list_for_each_entry(child, &ex_root->children, siblings) {
1719                 if (child->dev_type == EDGE_DEV ||
1720                     child->dev_type == FANOUT_DEV) {
1721                         struct sas_expander_device *ex =
1722                                 rphy_to_expander_device(child->rphy);
1723
1724                         if (level > ex->level)
1725                                 res = sas_discover_bfs_by_root_level(child,
1726                                                                      level);
1727                         else if (level == ex->level)
1728                                 res = sas_ex_discover_devices(child, -1);
1729                 }
1730         }
1731         return res;
1732 }
1733
1734 static int sas_discover_bfs_by_root(struct domain_device *dev)
1735 {
1736         int res;
1737         struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1738         int level = ex->level+1;
1739
1740         res = sas_ex_discover_devices(dev, -1);
1741         if (res)
1742                 goto out;
1743         do {
1744                 res = sas_discover_bfs_by_root_level(dev, level);
1745                 mb();
1746                 level += 1;
1747         } while (level <= dev->port->disc.max_level);
1748 out:
1749         return res;
1750 }
1751
1752 static int sas_discover_new(struct domain_device *dev, int phy_id)
1753 {
1754         struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1755         struct domain_device *child;
1756         int res;
1757
1758         SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1759                     SAS_ADDR(dev->sas_addr), phy_id);
1760         res = sas_ex_phy_discover(dev, phy_id);
1761         if (res)
1762                 goto out;
1763         res = sas_ex_discover_devices(dev, phy_id);
1764         if (res)
1765                 goto out;
1766         list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1767                 if (SAS_ADDR(child->sas_addr) ==
1768                     SAS_ADDR(ex_phy->attached_sas_addr)) {
1769                         if (child->dev_type == EDGE_DEV ||
1770                             child->dev_type == FANOUT_DEV)
1771                                 res = sas_discover_bfs_by_root(child);
1772                         break;
1773                 }
1774         }
1775 out:
1776         return res;
1777 }
1778
1779 static int sas_rediscover_dev(struct domain_device *dev, int phy_id)
1780 {
1781         struct expander_device *ex = &dev->ex_dev;
1782         struct ex_phy *phy = &ex->ex_phy[phy_id];
1783         u8 attached_sas_addr[8];
1784         int res;
1785
1786         res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
1787         switch (res) {
1788         case SMP_RESP_NO_PHY:
1789                 phy->phy_state = PHY_NOT_PRESENT;
1790                 sas_unregister_devs_sas_addr(dev, phy_id);
1791                 goto out; break;
1792         case SMP_RESP_PHY_VACANT:
1793                 phy->phy_state = PHY_VACANT;
1794                 sas_unregister_devs_sas_addr(dev, phy_id);
1795                 goto out; break;
1796         case SMP_RESP_FUNC_ACC:
1797                 break;
1798         }
1799
1800         if (SAS_ADDR(attached_sas_addr) == 0) {
1801                 phy->phy_state = PHY_EMPTY;
1802                 sas_unregister_devs_sas_addr(dev, phy_id);
1803         } else if (SAS_ADDR(attached_sas_addr) ==
1804                    SAS_ADDR(phy->attached_sas_addr)) {
1805                 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
1806                             SAS_ADDR(dev->sas_addr), phy_id);
1807                 sas_ex_phy_discover(dev, phy_id);
1808         } else
1809                 res = sas_discover_new(dev, phy_id);
1810 out:
1811         return res;
1812 }
1813
1814 static int sas_rediscover(struct domain_device *dev, const int phy_id)
1815 {
1816         struct expander_device *ex = &dev->ex_dev;
1817         struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
1818         int res = 0;
1819         int i;
1820
1821         SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
1822                     SAS_ADDR(dev->sas_addr), phy_id);
1823
1824         if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
1825                 for (i = 0; i < ex->num_phys; i++) {
1826                         struct ex_phy *phy = &ex->ex_phy[i];
1827
1828                         if (i == phy_id)
1829                                 continue;
1830                         if (SAS_ADDR(phy->attached_sas_addr) ==
1831                             SAS_ADDR(changed_phy->attached_sas_addr)) {
1832                                 SAS_DPRINTK("phy%d part of wide port with "
1833                                             "phy%d\n", phy_id, i);
1834                                 goto out;
1835                         }
1836                 }
1837                 res = sas_rediscover_dev(dev, phy_id);
1838         } else
1839                 res = sas_discover_new(dev, phy_id);
1840 out:
1841         return res;
1842 }
1843
1844 /**
1845  * sas_revalidate_domain -- revalidate the domain
1846  * @port: port to the domain of interest
1847  *
1848  * NOTE: this process _must_ quit (return) as soon as any connection
1849  * errors are encountered.  Connection recovery is done elsewhere.
1850  * Discover process only interrogates devices in order to discover the
1851  * domain.
1852  */
1853 int sas_ex_revalidate_domain(struct domain_device *port_dev)
1854 {
1855         int res;
1856         struct domain_device *dev = NULL;
1857
1858         res = sas_find_bcast_dev(port_dev, &dev);
1859         if (res)
1860                 goto out;
1861         if (dev) {
1862                 struct expander_device *ex = &dev->ex_dev;
1863                 int i = 0, phy_id;
1864
1865                 do {
1866                         phy_id = -1;
1867                         res = sas_find_bcast_phy(dev, &phy_id, i);
1868                         if (phy_id == -1)
1869                                 break;
1870                         res = sas_rediscover(dev, phy_id);
1871                         i = phy_id + 1;
1872                 } while (i < ex->num_phys);
1873         }
1874 out:
1875         return res;
1876 }
1877
1878 int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
1879                     struct request *req)
1880 {
1881         struct domain_device *dev;
1882         int ret, type;
1883         struct request *rsp = req->next_rq;
1884
1885         if (!rsp) {
1886                 printk("%s: space for a smp response is missing\n",
1887                        __FUNCTION__);
1888                 return -EINVAL;
1889         }
1890
1891         /* no rphy means no smp target support (ie aic94xx host) */
1892         if (!rphy) {
1893                 printk("%s: can we send a smp request to a host?\n",
1894                        __FUNCTION__);
1895                 return -EINVAL;
1896         }
1897         type = rphy->identify.device_type;
1898
1899         if (type != SAS_EDGE_EXPANDER_DEVICE &&
1900             type != SAS_FANOUT_EXPANDER_DEVICE) {
1901                 printk("%s: can we send a smp request to a device?\n",
1902                        __FUNCTION__);
1903                 return -EINVAL;
1904         }
1905
1906         dev = sas_find_dev_by_rphy(rphy);
1907         if (!dev) {
1908                 printk("%s: fail to find a domain_device?\n", __FUNCTION__);
1909                 return -EINVAL;
1910         }
1911
1912         /* do we need to support multiple segments? */
1913         if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
1914                 printk("%s: multiple segments req %u %u, rsp %u %u\n",
1915                        __FUNCTION__, req->bio->bi_vcnt, req->data_len,
1916                        rsp->bio->bi_vcnt, rsp->data_len);
1917                 return -EINVAL;
1918         }
1919
1920         ret = smp_execute_task(dev, bio_data(req->bio), req->data_len,
1921                                bio_data(rsp->bio), rsp->data_len);
1922
1923         return ret;
1924 }