sas_expander.c 54 KB

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