sas_expander.c 51 KB

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