sas_expander.c 52 KB

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