sas_expander.c 52 KB

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