sas_expander.c 51 KB

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