hisi_sas_main.c 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2015 Linaro Ltd.
  4. * Copyright (c) 2015 Hisilicon Limited.
  5. */
  6. #include "hisi_sas.h"
  7. #define DRV_NAME "hisi_sas"
  8. #define DEV_IS_GONE(dev) \
  9. ((!dev) || (dev->dev_type == SAS_PHY_UNUSED))
  10. static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
  11. u8 *lun, struct hisi_sas_tmf_task *tmf);
  12. static int
  13. hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
  14. struct domain_device *device,
  15. int abort_flag, int tag);
  16. static int hisi_sas_softreset_ata_disk(struct domain_device *device);
  17. static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
  18. void *funcdata);
  19. static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
  20. struct domain_device *device);
  21. static void hisi_sas_dev_gone(struct domain_device *device);
  22. u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis, int direction)
  23. {
  24. switch (fis->command) {
  25. case ATA_CMD_FPDMA_WRITE:
  26. case ATA_CMD_FPDMA_READ:
  27. case ATA_CMD_FPDMA_RECV:
  28. case ATA_CMD_FPDMA_SEND:
  29. case ATA_CMD_NCQ_NON_DATA:
  30. return HISI_SAS_SATA_PROTOCOL_FPDMA;
  31. case ATA_CMD_DOWNLOAD_MICRO:
  32. case ATA_CMD_ID_ATA:
  33. case ATA_CMD_PMP_READ:
  34. case ATA_CMD_READ_LOG_EXT:
  35. case ATA_CMD_PIO_READ:
  36. case ATA_CMD_PIO_READ_EXT:
  37. case ATA_CMD_PMP_WRITE:
  38. case ATA_CMD_WRITE_LOG_EXT:
  39. case ATA_CMD_PIO_WRITE:
  40. case ATA_CMD_PIO_WRITE_EXT:
  41. return HISI_SAS_SATA_PROTOCOL_PIO;
  42. case ATA_CMD_DSM:
  43. case ATA_CMD_DOWNLOAD_MICRO_DMA:
  44. case ATA_CMD_PMP_READ_DMA:
  45. case ATA_CMD_PMP_WRITE_DMA:
  46. case ATA_CMD_READ:
  47. case ATA_CMD_READ_EXT:
  48. case ATA_CMD_READ_LOG_DMA_EXT:
  49. case ATA_CMD_READ_STREAM_DMA_EXT:
  50. case ATA_CMD_TRUSTED_RCV_DMA:
  51. case ATA_CMD_TRUSTED_SND_DMA:
  52. case ATA_CMD_WRITE:
  53. case ATA_CMD_WRITE_EXT:
  54. case ATA_CMD_WRITE_FUA_EXT:
  55. case ATA_CMD_WRITE_QUEUED:
  56. case ATA_CMD_WRITE_LOG_DMA_EXT:
  57. case ATA_CMD_WRITE_STREAM_DMA_EXT:
  58. case ATA_CMD_ZAC_MGMT_IN:
  59. return HISI_SAS_SATA_PROTOCOL_DMA;
  60. case ATA_CMD_CHK_POWER:
  61. case ATA_CMD_DEV_RESET:
  62. case ATA_CMD_EDD:
  63. case ATA_CMD_FLUSH:
  64. case ATA_CMD_FLUSH_EXT:
  65. case ATA_CMD_VERIFY:
  66. case ATA_CMD_VERIFY_EXT:
  67. case ATA_CMD_SET_FEATURES:
  68. case ATA_CMD_STANDBY:
  69. case ATA_CMD_STANDBYNOW1:
  70. case ATA_CMD_ZAC_MGMT_OUT:
  71. return HISI_SAS_SATA_PROTOCOL_NONDATA;
  72. case ATA_CMD_SET_MAX:
  73. switch (fis->features) {
  74. case ATA_SET_MAX_PASSWD:
  75. case ATA_SET_MAX_LOCK:
  76. return HISI_SAS_SATA_PROTOCOL_PIO;
  77. case ATA_SET_MAX_PASSWD_DMA:
  78. case ATA_SET_MAX_UNLOCK_DMA:
  79. return HISI_SAS_SATA_PROTOCOL_DMA;
  80. default:
  81. return HISI_SAS_SATA_PROTOCOL_NONDATA;
  82. }
  83. default:
  84. {
  85. if (direction == DMA_NONE)
  86. return HISI_SAS_SATA_PROTOCOL_NONDATA;
  87. return HISI_SAS_SATA_PROTOCOL_PIO;
  88. }
  89. }
  90. }
  91. EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol);
  92. void hisi_sas_sata_done(struct sas_task *task,
  93. struct hisi_sas_slot *slot)
  94. {
  95. struct task_status_struct *ts = &task->task_status;
  96. struct ata_task_resp *resp = (struct ata_task_resp *)ts->buf;
  97. struct hisi_sas_status_buffer *status_buf =
  98. hisi_sas_status_buf_addr_mem(slot);
  99. u8 *iu = &status_buf->iu[0];
  100. struct dev_to_host_fis *d2h = (struct dev_to_host_fis *)iu;
  101. resp->frame_len = sizeof(struct dev_to_host_fis);
  102. memcpy(&resp->ending_fis[0], d2h, sizeof(struct dev_to_host_fis));
  103. ts->buf_valid_size = sizeof(*resp);
  104. }
  105. EXPORT_SYMBOL_GPL(hisi_sas_sata_done);
  106. /*
  107. * This function assumes linkrate mask fits in 8 bits, which it
  108. * does for all HW versions supported.
  109. */
  110. u8 hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max)
  111. {
  112. u8 rate = 0;
  113. int i;
  114. max -= SAS_LINK_RATE_1_5_GBPS;
  115. for (i = 0; i <= max; i++)
  116. rate |= 1 << (i * 2);
  117. return rate;
  118. }
  119. EXPORT_SYMBOL_GPL(hisi_sas_get_prog_phy_linkrate_mask);
  120. static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
  121. {
  122. return device->port->ha->lldd_ha;
  123. }
  124. struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port)
  125. {
  126. return container_of(sas_port, struct hisi_sas_port, sas_port);
  127. }
  128. EXPORT_SYMBOL_GPL(to_hisi_sas_port);
  129. void hisi_sas_stop_phys(struct hisi_hba *hisi_hba)
  130. {
  131. int phy_no;
  132. for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++)
  133. hisi_sas_phy_enable(hisi_hba, phy_no, 0);
  134. }
  135. EXPORT_SYMBOL_GPL(hisi_sas_stop_phys);
  136. static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx)
  137. {
  138. void *bitmap = hisi_hba->slot_index_tags;
  139. clear_bit(slot_idx, bitmap);
  140. }
  141. static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx)
  142. {
  143. if (hisi_hba->hw->slot_index_alloc ||
  144. slot_idx >= HISI_SAS_UNRESERVED_IPTT) {
  145. spin_lock(&hisi_hba->lock);
  146. hisi_sas_slot_index_clear(hisi_hba, slot_idx);
  147. spin_unlock(&hisi_hba->lock);
  148. }
  149. }
  150. static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
  151. {
  152. void *bitmap = hisi_hba->slot_index_tags;
  153. set_bit(slot_idx, bitmap);
  154. }
  155. static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba,
  156. struct scsi_cmnd *scsi_cmnd)
  157. {
  158. int index;
  159. void *bitmap = hisi_hba->slot_index_tags;
  160. if (scsi_cmnd)
  161. return scsi_cmnd->request->tag;
  162. spin_lock(&hisi_hba->lock);
  163. index = find_next_zero_bit(bitmap, hisi_hba->slot_index_count,
  164. hisi_hba->last_slot_index + 1);
  165. if (index >= hisi_hba->slot_index_count) {
  166. index = find_next_zero_bit(bitmap,
  167. hisi_hba->slot_index_count,
  168. HISI_SAS_UNRESERVED_IPTT);
  169. if (index >= hisi_hba->slot_index_count) {
  170. spin_unlock(&hisi_hba->lock);
  171. return -SAS_QUEUE_FULL;
  172. }
  173. }
  174. hisi_sas_slot_index_set(hisi_hba, index);
  175. hisi_hba->last_slot_index = index;
  176. spin_unlock(&hisi_hba->lock);
  177. return index;
  178. }
  179. static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
  180. {
  181. int i;
  182. for (i = 0; i < hisi_hba->slot_index_count; ++i)
  183. hisi_sas_slot_index_clear(hisi_hba, i);
  184. }
  185. void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
  186. struct hisi_sas_slot *slot)
  187. {
  188. int device_id = slot->device_id;
  189. struct hisi_sas_device *sas_dev = &hisi_hba->devices[device_id];
  190. if (task) {
  191. struct device *dev = hisi_hba->dev;
  192. if (!task->lldd_task)
  193. return;
  194. task->lldd_task = NULL;
  195. if (!sas_protocol_ata(task->task_proto)) {
  196. if (slot->n_elem)
  197. dma_unmap_sg(dev, task->scatter,
  198. task->num_scatter,
  199. task->data_dir);
  200. if (slot->n_elem_dif) {
  201. struct sas_ssp_task *ssp_task = &task->ssp_task;
  202. struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
  203. dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
  204. scsi_prot_sg_count(scsi_cmnd),
  205. task->data_dir);
  206. }
  207. }
  208. }
  209. spin_lock(&sas_dev->lock);
  210. list_del_init(&slot->entry);
  211. spin_unlock(&sas_dev->lock);
  212. memset(slot, 0, offsetof(struct hisi_sas_slot, buf));
  213. hisi_sas_slot_index_free(hisi_hba, slot->idx);
  214. }
  215. EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
  216. static void hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
  217. struct hisi_sas_slot *slot)
  218. {
  219. hisi_hba->hw->prep_smp(hisi_hba, slot);
  220. }
  221. static void hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
  222. struct hisi_sas_slot *slot)
  223. {
  224. hisi_hba->hw->prep_ssp(hisi_hba, slot);
  225. }
  226. static void hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
  227. struct hisi_sas_slot *slot)
  228. {
  229. hisi_hba->hw->prep_stp(hisi_hba, slot);
  230. }
  231. static void hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
  232. struct hisi_sas_slot *slot,
  233. int device_id, int abort_flag, int tag_to_abort)
  234. {
  235. hisi_hba->hw->prep_abort(hisi_hba, slot,
  236. device_id, abort_flag, tag_to_abort);
  237. }
  238. static void hisi_sas_dma_unmap(struct hisi_hba *hisi_hba,
  239. struct sas_task *task, int n_elem,
  240. int n_elem_req)
  241. {
  242. struct device *dev = hisi_hba->dev;
  243. if (!sas_protocol_ata(task->task_proto)) {
  244. if (task->num_scatter) {
  245. if (n_elem)
  246. dma_unmap_sg(dev, task->scatter,
  247. task->num_scatter,
  248. task->data_dir);
  249. } else if (task->task_proto & SAS_PROTOCOL_SMP) {
  250. if (n_elem_req)
  251. dma_unmap_sg(dev, &task->smp_task.smp_req,
  252. 1, DMA_TO_DEVICE);
  253. }
  254. }
  255. }
  256. static int hisi_sas_dma_map(struct hisi_hba *hisi_hba,
  257. struct sas_task *task, int *n_elem,
  258. int *n_elem_req)
  259. {
  260. struct device *dev = hisi_hba->dev;
  261. int rc;
  262. if (sas_protocol_ata(task->task_proto)) {
  263. *n_elem = task->num_scatter;
  264. } else {
  265. unsigned int req_len;
  266. if (task->num_scatter) {
  267. *n_elem = dma_map_sg(dev, task->scatter,
  268. task->num_scatter, task->data_dir);
  269. if (!*n_elem) {
  270. rc = -ENOMEM;
  271. goto prep_out;
  272. }
  273. } else if (task->task_proto & SAS_PROTOCOL_SMP) {
  274. *n_elem_req = dma_map_sg(dev, &task->smp_task.smp_req,
  275. 1, DMA_TO_DEVICE);
  276. if (!*n_elem_req) {
  277. rc = -ENOMEM;
  278. goto prep_out;
  279. }
  280. req_len = sg_dma_len(&task->smp_task.smp_req);
  281. if (req_len & 0x3) {
  282. rc = -EINVAL;
  283. goto err_out_dma_unmap;
  284. }
  285. }
  286. }
  287. if (*n_elem > HISI_SAS_SGE_PAGE_CNT) {
  288. dev_err(dev, "task prep: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT\n",
  289. *n_elem);
  290. rc = -EINVAL;
  291. goto err_out_dma_unmap;
  292. }
  293. return 0;
  294. err_out_dma_unmap:
  295. /* It would be better to call dma_unmap_sg() here, but it's messy */
  296. hisi_sas_dma_unmap(hisi_hba, task, *n_elem,
  297. *n_elem_req);
  298. prep_out:
  299. return rc;
  300. }
  301. static void hisi_sas_dif_dma_unmap(struct hisi_hba *hisi_hba,
  302. struct sas_task *task, int n_elem_dif)
  303. {
  304. struct device *dev = hisi_hba->dev;
  305. if (n_elem_dif) {
  306. struct sas_ssp_task *ssp_task = &task->ssp_task;
  307. struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
  308. dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
  309. scsi_prot_sg_count(scsi_cmnd),
  310. task->data_dir);
  311. }
  312. }
  313. static int hisi_sas_dif_dma_map(struct hisi_hba *hisi_hba,
  314. int *n_elem_dif, struct sas_task *task)
  315. {
  316. struct device *dev = hisi_hba->dev;
  317. struct sas_ssp_task *ssp_task;
  318. struct scsi_cmnd *scsi_cmnd;
  319. int rc;
  320. if (task->num_scatter) {
  321. ssp_task = &task->ssp_task;
  322. scsi_cmnd = ssp_task->cmd;
  323. if (scsi_prot_sg_count(scsi_cmnd)) {
  324. *n_elem_dif = dma_map_sg(dev,
  325. scsi_prot_sglist(scsi_cmnd),
  326. scsi_prot_sg_count(scsi_cmnd),
  327. task->data_dir);
  328. if (!*n_elem_dif)
  329. return -ENOMEM;
  330. if (*n_elem_dif > HISI_SAS_SGE_DIF_PAGE_CNT) {
  331. dev_err(dev, "task prep: n_elem_dif(%d) too large\n",
  332. *n_elem_dif);
  333. rc = -EINVAL;
  334. goto err_out_dif_dma_unmap;
  335. }
  336. }
  337. }
  338. return 0;
  339. err_out_dif_dma_unmap:
  340. dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
  341. scsi_prot_sg_count(scsi_cmnd), task->data_dir);
  342. return rc;
  343. }
  344. static int hisi_sas_task_prep(struct sas_task *task,
  345. struct hisi_sas_dq **dq_pointer,
  346. bool is_tmf, struct hisi_sas_tmf_task *tmf,
  347. int *pass)
  348. {
  349. struct domain_device *device = task->dev;
  350. struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
  351. struct hisi_sas_device *sas_dev = device->lldd_dev;
  352. struct hisi_sas_port *port;
  353. struct hisi_sas_slot *slot;
  354. struct hisi_sas_cmd_hdr *cmd_hdr_base;
  355. struct asd_sas_port *sas_port = device->port;
  356. struct device *dev = hisi_hba->dev;
  357. int dlvry_queue_slot, dlvry_queue, rc, slot_idx;
  358. int n_elem = 0, n_elem_dif = 0, n_elem_req = 0;
  359. struct scsi_cmnd *scmd = NULL;
  360. struct hisi_sas_dq *dq;
  361. unsigned long flags;
  362. int wr_q_index;
  363. if (DEV_IS_GONE(sas_dev)) {
  364. if (sas_dev)
  365. dev_info(dev, "task prep: device %d not ready\n",
  366. sas_dev->device_id);
  367. else
  368. dev_info(dev, "task prep: device %016llx not ready\n",
  369. SAS_ADDR(device->sas_addr));
  370. return -ECOMM;
  371. }
  372. if (task->uldd_task) {
  373. struct ata_queued_cmd *qc;
  374. if (dev_is_sata(device)) {
  375. qc = task->uldd_task;
  376. scmd = qc->scsicmd;
  377. } else {
  378. scmd = task->uldd_task;
  379. }
  380. }
  381. if (scmd && hisi_hba->shost->nr_hw_queues) {
  382. unsigned int dq_index;
  383. u32 blk_tag;
  384. blk_tag = blk_mq_unique_tag(scmd->request);
  385. dq_index = blk_mq_unique_tag_to_hwq(blk_tag);
  386. *dq_pointer = dq = &hisi_hba->dq[dq_index];
  387. } else if (hisi_hba->shost->nr_hw_queues) {
  388. struct Scsi_Host *shost = hisi_hba->shost;
  389. struct blk_mq_queue_map *qmap = &shost->tag_set.map[HCTX_TYPE_DEFAULT];
  390. int queue = qmap->mq_map[raw_smp_processor_id()];
  391. *dq_pointer = dq = &hisi_hba->dq[queue];
  392. } else {
  393. *dq_pointer = dq = sas_dev->dq;
  394. }
  395. port = to_hisi_sas_port(sas_port);
  396. if (port && !port->port_attached) {
  397. dev_info(dev, "task prep: %s port%d not attach device\n",
  398. (dev_is_sata(device)) ?
  399. "SATA/STP" : "SAS",
  400. device->port->id);
  401. return -ECOMM;
  402. }
  403. rc = hisi_sas_dma_map(hisi_hba, task, &n_elem,
  404. &n_elem_req);
  405. if (rc < 0)
  406. goto prep_out;
  407. if (!sas_protocol_ata(task->task_proto)) {
  408. rc = hisi_sas_dif_dma_map(hisi_hba, &n_elem_dif, task);
  409. if (rc < 0)
  410. goto err_out_dma_unmap;
  411. }
  412. if (hisi_hba->hw->slot_index_alloc)
  413. rc = hisi_hba->hw->slot_index_alloc(hisi_hba, device);
  414. else
  415. rc = hisi_sas_slot_index_alloc(hisi_hba, scmd);
  416. if (rc < 0)
  417. goto err_out_dif_dma_unmap;
  418. slot_idx = rc;
  419. slot = &hisi_hba->slot_info[slot_idx];
  420. spin_lock(&dq->lock);
  421. wr_q_index = dq->wr_point;
  422. dq->wr_point = (dq->wr_point + 1) % HISI_SAS_QUEUE_SLOTS;
  423. list_add_tail(&slot->delivery, &dq->list);
  424. spin_unlock(&dq->lock);
  425. spin_lock(&sas_dev->lock);
  426. list_add_tail(&slot->entry, &sas_dev->list);
  427. spin_unlock(&sas_dev->lock);
  428. dlvry_queue = dq->id;
  429. dlvry_queue_slot = wr_q_index;
  430. slot->device_id = sas_dev->device_id;
  431. slot->n_elem = n_elem;
  432. slot->n_elem_dif = n_elem_dif;
  433. slot->dlvry_queue = dlvry_queue;
  434. slot->dlvry_queue_slot = dlvry_queue_slot;
  435. cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
  436. slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
  437. slot->task = task;
  438. slot->port = port;
  439. slot->tmf = tmf;
  440. slot->is_internal = is_tmf;
  441. task->lldd_task = slot;
  442. memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
  443. memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
  444. memset(hisi_sas_status_buf_addr_mem(slot), 0,
  445. sizeof(struct hisi_sas_err_record));
  446. switch (task->task_proto) {
  447. case SAS_PROTOCOL_SMP:
  448. hisi_sas_task_prep_smp(hisi_hba, slot);
  449. break;
  450. case SAS_PROTOCOL_SSP:
  451. hisi_sas_task_prep_ssp(hisi_hba, slot);
  452. break;
  453. case SAS_PROTOCOL_SATA:
  454. case SAS_PROTOCOL_STP:
  455. case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
  456. hisi_sas_task_prep_ata(hisi_hba, slot);
  457. break;
  458. default:
  459. dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
  460. task->task_proto);
  461. break;
  462. }
  463. spin_lock_irqsave(&task->task_state_lock, flags);
  464. task->task_state_flags |= SAS_TASK_AT_INITIATOR;
  465. spin_unlock_irqrestore(&task->task_state_lock, flags);
  466. ++(*pass);
  467. WRITE_ONCE(slot->ready, 1);
  468. return 0;
  469. err_out_dif_dma_unmap:
  470. if (!sas_protocol_ata(task->task_proto))
  471. hisi_sas_dif_dma_unmap(hisi_hba, task, n_elem_dif);
  472. err_out_dma_unmap:
  473. hisi_sas_dma_unmap(hisi_hba, task, n_elem,
  474. n_elem_req);
  475. prep_out:
  476. dev_err(dev, "task prep: failed[%d]!\n", rc);
  477. return rc;
  478. }
  479. static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
  480. bool is_tmf, struct hisi_sas_tmf_task *tmf)
  481. {
  482. u32 rc;
  483. u32 pass = 0;
  484. struct hisi_hba *hisi_hba;
  485. struct device *dev;
  486. struct domain_device *device = task->dev;
  487. struct asd_sas_port *sas_port = device->port;
  488. struct hisi_sas_dq *dq = NULL;
  489. if (!sas_port) {
  490. struct task_status_struct *ts = &task->task_status;
  491. ts->resp = SAS_TASK_UNDELIVERED;
  492. ts->stat = SAS_PHY_DOWN;
  493. /*
  494. * libsas will use dev->port, should
  495. * not call task_done for sata
  496. */
  497. if (device->dev_type != SAS_SATA_DEV)
  498. task->task_done(task);
  499. return -ECOMM;
  500. }
  501. hisi_hba = dev_to_hisi_hba(device);
  502. dev = hisi_hba->dev;
  503. if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags))) {
  504. /*
  505. * For IOs from upper layer, it may already disable preempt
  506. * in the IO path, if disable preempt again in down(),
  507. * function schedule() will report schedule_bug(), so check
  508. * preemptible() before goto down().
  509. */
  510. if (!preemptible())
  511. return -EINVAL;
  512. down(&hisi_hba->sem);
  513. up(&hisi_hba->sem);
  514. }
  515. /* protect task_prep and start_delivery sequence */
  516. rc = hisi_sas_task_prep(task, &dq, is_tmf, tmf, &pass);
  517. if (rc)
  518. dev_err(dev, "task exec: failed[%d]!\n", rc);
  519. if (likely(pass)) {
  520. spin_lock(&dq->lock);
  521. hisi_hba->hw->start_delivery(dq);
  522. spin_unlock(&dq->lock);
  523. }
  524. return rc;
  525. }
  526. static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no)
  527. {
  528. struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
  529. struct asd_sas_phy *sas_phy = &phy->sas_phy;
  530. if (!phy->phy_attached)
  531. return;
  532. if (test_bit(HISI_SAS_PM_BIT, &hisi_hba->flags) &&
  533. !sas_phy->suspended) {
  534. dev_warn(hisi_hba->dev, "phy%d during suspend filtered out\n", phy_no);
  535. return;
  536. }
  537. sas_notify_phy_event(sas_phy, PHYE_OOB_DONE);
  538. if (sas_phy->phy) {
  539. struct sas_phy *sphy = sas_phy->phy;
  540. sphy->negotiated_linkrate = sas_phy->linkrate;
  541. sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
  542. sphy->maximum_linkrate_hw =
  543. hisi_hba->hw->phy_get_max_linkrate();
  544. if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN)
  545. sphy->minimum_linkrate = phy->minimum_linkrate;
  546. if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN)
  547. sphy->maximum_linkrate = phy->maximum_linkrate;
  548. }
  549. if (phy->phy_type & PORT_TYPE_SAS) {
  550. struct sas_identify_frame *id;
  551. id = (struct sas_identify_frame *)phy->frame_rcvd;
  552. id->dev_type = phy->identify.device_type;
  553. id->initiator_bits = SAS_PROTOCOL_ALL;
  554. id->target_bits = phy->identify.target_port_protocols;
  555. } else if (phy->phy_type & PORT_TYPE_SATA) {
  556. /* Nothing */
  557. }
  558. sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
  559. sas_notify_port_event(sas_phy, PORTE_BYTES_DMAED);
  560. }
  561. static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
  562. {
  563. struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
  564. struct hisi_sas_device *sas_dev = NULL;
  565. int last = hisi_hba->last_dev_id;
  566. int first = (hisi_hba->last_dev_id + 1) % HISI_SAS_MAX_DEVICES;
  567. int i;
  568. spin_lock(&hisi_hba->lock);
  569. for (i = first; i != last; i %= HISI_SAS_MAX_DEVICES) {
  570. if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
  571. int queue = i % hisi_hba->queue_count;
  572. struct hisi_sas_dq *dq = &hisi_hba->dq[queue];
  573. hisi_hba->devices[i].device_id = i;
  574. sas_dev = &hisi_hba->devices[i];
  575. sas_dev->dev_status = HISI_SAS_DEV_INIT;
  576. sas_dev->dev_type = device->dev_type;
  577. sas_dev->hisi_hba = hisi_hba;
  578. sas_dev->sas_device = device;
  579. sas_dev->dq = dq;
  580. spin_lock_init(&sas_dev->lock);
  581. INIT_LIST_HEAD(&hisi_hba->devices[i].list);
  582. break;
  583. }
  584. i++;
  585. }
  586. hisi_hba->last_dev_id = i;
  587. spin_unlock(&hisi_hba->lock);
  588. return sas_dev;
  589. }
  590. #define HISI_SAS_DISK_RECOVER_CNT 3
  591. static int hisi_sas_init_device(struct domain_device *device)
  592. {
  593. int rc = TMF_RESP_FUNC_COMPLETE;
  594. struct scsi_lun lun;
  595. struct hisi_sas_tmf_task tmf_task;
  596. int retry = HISI_SAS_DISK_RECOVER_CNT;
  597. struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
  598. struct device *dev = hisi_hba->dev;
  599. struct sas_phy *local_phy;
  600. switch (device->dev_type) {
  601. case SAS_END_DEVICE:
  602. int_to_scsilun(0, &lun);
  603. tmf_task.tmf = TMF_CLEAR_TASK_SET;
  604. while (retry-- > 0) {
  605. rc = hisi_sas_debug_issue_ssp_tmf(device, lun.scsi_lun,
  606. &tmf_task);
  607. if (rc == TMF_RESP_FUNC_COMPLETE) {
  608. hisi_sas_release_task(hisi_hba, device);
  609. break;
  610. }
  611. }
  612. break;
  613. case SAS_SATA_DEV:
  614. case SAS_SATA_PM:
  615. case SAS_SATA_PM_PORT:
  616. case SAS_SATA_PENDING:
  617. /*
  618. * send HARD RESET to clear previous affiliation of
  619. * STP target port
  620. */
  621. local_phy = sas_get_local_phy(device);
  622. if (!scsi_is_sas_phy_local(local_phy) &&
  623. !test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
  624. unsigned long deadline = ata_deadline(jiffies, 20000);
  625. struct sata_device *sata_dev = &device->sata_dev;
  626. struct ata_host *ata_host = sata_dev->ata_host;
  627. struct ata_port_operations *ops = ata_host->ops;
  628. struct ata_port *ap = sata_dev->ap;
  629. struct ata_link *link;
  630. unsigned int classes;
  631. ata_for_each_link(link, ap, EDGE)
  632. rc = ops->hardreset(link, &classes,
  633. deadline);
  634. }
  635. sas_put_local_phy(local_phy);
  636. if (rc) {
  637. dev_warn(dev, "SATA disk hardreset fail: %d\n", rc);
  638. return rc;
  639. }
  640. while (retry-- > 0) {
  641. rc = hisi_sas_softreset_ata_disk(device);
  642. if (!rc)
  643. break;
  644. }
  645. break;
  646. default:
  647. break;
  648. }
  649. return rc;
  650. }
  651. static int hisi_sas_dev_found(struct domain_device *device)
  652. {
  653. struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
  654. struct domain_device *parent_dev = device->parent;
  655. struct hisi_sas_device *sas_dev;
  656. struct device *dev = hisi_hba->dev;
  657. int rc;
  658. if (hisi_hba->hw->alloc_dev)
  659. sas_dev = hisi_hba->hw->alloc_dev(device);
  660. else
  661. sas_dev = hisi_sas_alloc_dev(device);
  662. if (!sas_dev) {
  663. dev_err(dev, "fail alloc dev: max support %d devices\n",
  664. HISI_SAS_MAX_DEVICES);
  665. return -EINVAL;
  666. }
  667. device->lldd_dev = sas_dev;
  668. hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
  669. if (parent_dev && dev_is_expander(parent_dev->dev_type)) {
  670. int phy_no;
  671. u8 phy_num = parent_dev->ex_dev.num_phys;
  672. struct ex_phy *phy;
  673. for (phy_no = 0; phy_no < phy_num; phy_no++) {
  674. phy = &parent_dev->ex_dev.ex_phy[phy_no];
  675. if (SAS_ADDR(phy->attached_sas_addr) ==
  676. SAS_ADDR(device->sas_addr))
  677. break;
  678. }
  679. if (phy_no == phy_num) {
  680. dev_info(dev, "dev found: no attached "
  681. "dev:%016llx at ex:%016llx\n",
  682. SAS_ADDR(device->sas_addr),
  683. SAS_ADDR(parent_dev->sas_addr));
  684. rc = -EINVAL;
  685. goto err_out;
  686. }
  687. }
  688. dev_info(dev, "dev[%d:%x] found\n",
  689. sas_dev->device_id, sas_dev->dev_type);
  690. rc = hisi_sas_init_device(device);
  691. if (rc)
  692. goto err_out;
  693. sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
  694. return 0;
  695. err_out:
  696. hisi_sas_dev_gone(device);
  697. return rc;
  698. }
  699. int hisi_sas_slave_configure(struct scsi_device *sdev)
  700. {
  701. struct domain_device *dev = sdev_to_domain_dev(sdev);
  702. int ret = sas_slave_configure(sdev);
  703. if (ret)
  704. return ret;
  705. if (!dev_is_sata(dev))
  706. sas_change_queue_depth(sdev, 64);
  707. return 0;
  708. }
  709. EXPORT_SYMBOL_GPL(hisi_sas_slave_configure);
  710. void hisi_sas_scan_start(struct Scsi_Host *shost)
  711. {
  712. struct hisi_hba *hisi_hba = shost_priv(shost);
  713. hisi_hba->hw->phys_init(hisi_hba);
  714. }
  715. EXPORT_SYMBOL_GPL(hisi_sas_scan_start);
  716. int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
  717. {
  718. struct hisi_hba *hisi_hba = shost_priv(shost);
  719. struct sas_ha_struct *sha = &hisi_hba->sha;
  720. /* Wait for PHY up interrupt to occur */
  721. if (time < HZ)
  722. return 0;
  723. sas_drain_work(sha);
  724. return 1;
  725. }
  726. EXPORT_SYMBOL_GPL(hisi_sas_scan_finished);
  727. static void hisi_sas_phyup_work(struct work_struct *work)
  728. {
  729. struct hisi_sas_phy *phy =
  730. container_of(work, typeof(*phy), works[HISI_PHYE_PHY_UP]);
  731. struct hisi_hba *hisi_hba = phy->hisi_hba;
  732. struct asd_sas_phy *sas_phy = &phy->sas_phy;
  733. int phy_no = sas_phy->id;
  734. if (phy->identify.target_port_protocols == SAS_PROTOCOL_SSP)
  735. hisi_hba->hw->sl_notify_ssp(hisi_hba, phy_no);
  736. hisi_sas_bytes_dmaed(hisi_hba, phy_no);
  737. }
  738. static void hisi_sas_linkreset_work(struct work_struct *work)
  739. {
  740. struct hisi_sas_phy *phy =
  741. container_of(work, typeof(*phy), works[HISI_PHYE_LINK_RESET]);
  742. struct asd_sas_phy *sas_phy = &phy->sas_phy;
  743. hisi_sas_control_phy(sas_phy, PHY_FUNC_LINK_RESET, NULL);
  744. }
  745. static const work_func_t hisi_sas_phye_fns[HISI_PHYES_NUM] = {
  746. [HISI_PHYE_PHY_UP] = hisi_sas_phyup_work,
  747. [HISI_PHYE_LINK_RESET] = hisi_sas_linkreset_work,
  748. };
  749. bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
  750. enum hisi_sas_phy_event event)
  751. {
  752. struct hisi_hba *hisi_hba = phy->hisi_hba;
  753. if (WARN_ON(event >= HISI_PHYES_NUM))
  754. return false;
  755. return queue_work(hisi_hba->wq, &phy->works[event]);
  756. }
  757. EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event);
  758. static void hisi_sas_wait_phyup_timedout(struct timer_list *t)
  759. {
  760. struct hisi_sas_phy *phy = from_timer(phy, t, timer);
  761. struct hisi_hba *hisi_hba = phy->hisi_hba;
  762. struct device *dev = hisi_hba->dev;
  763. int phy_no = phy->sas_phy.id;
  764. dev_warn(dev, "phy%d wait phyup timeout, issuing link reset\n", phy_no);
  765. hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET);
  766. }
  767. void hisi_sas_phy_oob_ready(struct hisi_hba *hisi_hba, int phy_no)
  768. {
  769. struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
  770. struct device *dev = hisi_hba->dev;
  771. dev_dbg(dev, "phy%d OOB ready\n", phy_no);
  772. if (phy->phy_attached)
  773. return;
  774. if (!timer_pending(&phy->timer)) {
  775. phy->timer.expires = jiffies + HISI_SAS_WAIT_PHYUP_TIMEOUT * HZ;
  776. add_timer(&phy->timer);
  777. }
  778. }
  779. EXPORT_SYMBOL_GPL(hisi_sas_phy_oob_ready);
  780. static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
  781. {
  782. struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
  783. struct asd_sas_phy *sas_phy = &phy->sas_phy;
  784. int i;
  785. phy->hisi_hba = hisi_hba;
  786. phy->port = NULL;
  787. phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
  788. phy->maximum_linkrate = hisi_hba->hw->phy_get_max_linkrate();
  789. sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
  790. sas_phy->class = SAS;
  791. sas_phy->iproto = SAS_PROTOCOL_ALL;
  792. sas_phy->tproto = 0;
  793. sas_phy->type = PHY_TYPE_PHYSICAL;
  794. sas_phy->role = PHY_ROLE_INITIATOR;
  795. sas_phy->oob_mode = OOB_NOT_CONNECTED;
  796. sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
  797. sas_phy->id = phy_no;
  798. sas_phy->sas_addr = &hisi_hba->sas_addr[0];
  799. sas_phy->frame_rcvd = &phy->frame_rcvd[0];
  800. sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
  801. sas_phy->lldd_phy = phy;
  802. for (i = 0; i < HISI_PHYES_NUM; i++)
  803. INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]);
  804. spin_lock_init(&phy->lock);
  805. timer_setup(&phy->timer, hisi_sas_wait_phyup_timedout, 0);
  806. }
  807. /* Wrapper to ensure we track hisi_sas_phy.enable properly */
  808. void hisi_sas_phy_enable(struct hisi_hba *hisi_hba, int phy_no, int enable)
  809. {
  810. struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
  811. struct asd_sas_phy *aphy = &phy->sas_phy;
  812. struct sas_phy *sphy = aphy->phy;
  813. unsigned long flags;
  814. spin_lock_irqsave(&phy->lock, flags);
  815. if (enable) {
  816. /* We may have been enabled already; if so, don't touch */
  817. if (!phy->enable)
  818. sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
  819. hisi_hba->hw->phy_start(hisi_hba, phy_no);
  820. } else {
  821. sphy->negotiated_linkrate = SAS_PHY_DISABLED;
  822. hisi_hba->hw->phy_disable(hisi_hba, phy_no);
  823. }
  824. phy->enable = enable;
  825. spin_unlock_irqrestore(&phy->lock, flags);
  826. }
  827. EXPORT_SYMBOL_GPL(hisi_sas_phy_enable);
  828. static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
  829. {
  830. struct sas_ha_struct *sas_ha = sas_phy->ha;
  831. struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
  832. struct hisi_sas_phy *phy = sas_phy->lldd_phy;
  833. struct asd_sas_port *sas_port = sas_phy->port;
  834. struct hisi_sas_port *port;
  835. unsigned long flags;
  836. if (!sas_port)
  837. return;
  838. port = to_hisi_sas_port(sas_port);
  839. spin_lock_irqsave(&hisi_hba->lock, flags);
  840. port->port_attached = 1;
  841. port->id = phy->port_id;
  842. phy->port = port;
  843. sas_port->lldd_port = port;
  844. spin_unlock_irqrestore(&hisi_hba->lock, flags);
  845. }
  846. static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task,
  847. struct hisi_sas_slot *slot)
  848. {
  849. if (task) {
  850. unsigned long flags;
  851. struct task_status_struct *ts;
  852. ts = &task->task_status;
  853. ts->resp = SAS_TASK_COMPLETE;
  854. ts->stat = SAS_ABORTED_TASK;
  855. spin_lock_irqsave(&task->task_state_lock, flags);
  856. task->task_state_flags &=
  857. ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
  858. if (!slot->is_internal && task->task_proto != SAS_PROTOCOL_SMP)
  859. task->task_state_flags |= SAS_TASK_STATE_DONE;
  860. spin_unlock_irqrestore(&task->task_state_lock, flags);
  861. }
  862. hisi_sas_slot_task_free(hisi_hba, task, slot);
  863. }
  864. static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
  865. struct domain_device *device)
  866. {
  867. struct hisi_sas_slot *slot, *slot2;
  868. struct hisi_sas_device *sas_dev = device->lldd_dev;
  869. list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
  870. hisi_sas_do_release_task(hisi_hba, slot->task, slot);
  871. }
  872. void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
  873. {
  874. struct hisi_sas_device *sas_dev;
  875. struct domain_device *device;
  876. int i;
  877. for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
  878. sas_dev = &hisi_hba->devices[i];
  879. device = sas_dev->sas_device;
  880. if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
  881. !device)
  882. continue;
  883. hisi_sas_release_task(hisi_hba, device);
  884. }
  885. }
  886. EXPORT_SYMBOL_GPL(hisi_sas_release_tasks);
  887. static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba,
  888. struct domain_device *device)
  889. {
  890. if (hisi_hba->hw->dereg_device)
  891. hisi_hba->hw->dereg_device(hisi_hba, device);
  892. }
  893. static void hisi_sas_dev_gone(struct domain_device *device)
  894. {
  895. struct hisi_sas_device *sas_dev = device->lldd_dev;
  896. struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
  897. struct device *dev = hisi_hba->dev;
  898. int ret = 0;
  899. dev_info(dev, "dev[%d:%x] is gone\n",
  900. sas_dev->device_id, sas_dev->dev_type);
  901. down(&hisi_hba->sem);
  902. if (!test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
  903. hisi_sas_internal_task_abort(hisi_hba, device,
  904. HISI_SAS_INT_ABT_DEV, 0);
  905. hisi_sas_dereg_device(hisi_hba, device);
  906. ret = hisi_hba->hw->clear_itct(hisi_hba, sas_dev);
  907. device->lldd_dev = NULL;
  908. }
  909. if (hisi_hba->hw->free_device)
  910. hisi_hba->hw->free_device(sas_dev);
  911. /* Don't mark it as SAS_PHY_UNUSED if failed to clear ITCT */
  912. if (!ret)
  913. sas_dev->dev_type = SAS_PHY_UNUSED;
  914. sas_dev->sas_device = NULL;
  915. up(&hisi_hba->sem);
  916. }
  917. static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
  918. {
  919. return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
  920. }
  921. static int hisi_sas_phy_set_linkrate(struct hisi_hba *hisi_hba, int phy_no,
  922. struct sas_phy_linkrates *r)
  923. {
  924. struct sas_phy_linkrates _r;
  925. struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
  926. struct asd_sas_phy *sas_phy = &phy->sas_phy;
  927. enum sas_linkrate min, max;
  928. if (r->minimum_linkrate > SAS_LINK_RATE_1_5_GBPS)
  929. return -EINVAL;
  930. if (r->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) {
  931. max = sas_phy->phy->maximum_linkrate;
  932. min = r->minimum_linkrate;
  933. } else if (r->minimum_linkrate == SAS_LINK_RATE_UNKNOWN) {
  934. max = r->maximum_linkrate;
  935. min = sas_phy->phy->minimum_linkrate;
  936. } else
  937. return -EINVAL;
  938. _r.maximum_linkrate = max;
  939. _r.minimum_linkrate = min;
  940. sas_phy->phy->maximum_linkrate = max;
  941. sas_phy->phy->minimum_linkrate = min;
  942. hisi_sas_phy_enable(hisi_hba, phy_no, 0);
  943. msleep(100);
  944. hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, &_r);
  945. hisi_sas_phy_enable(hisi_hba, phy_no, 1);
  946. return 0;
  947. }
  948. static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
  949. void *funcdata)
  950. {
  951. struct sas_ha_struct *sas_ha = sas_phy->ha;
  952. struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
  953. int phy_no = sas_phy->id;
  954. switch (func) {
  955. case PHY_FUNC_HARD_RESET:
  956. hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
  957. break;
  958. case PHY_FUNC_LINK_RESET:
  959. hisi_sas_phy_enable(hisi_hba, phy_no, 0);
  960. msleep(100);
  961. hisi_sas_phy_enable(hisi_hba, phy_no, 1);
  962. break;
  963. case PHY_FUNC_DISABLE:
  964. hisi_sas_phy_enable(hisi_hba, phy_no, 0);
  965. break;
  966. case PHY_FUNC_SET_LINK_RATE:
  967. return hisi_sas_phy_set_linkrate(hisi_hba, phy_no, funcdata);
  968. case PHY_FUNC_GET_EVENTS:
  969. if (hisi_hba->hw->get_events) {
  970. hisi_hba->hw->get_events(hisi_hba, phy_no);
  971. break;
  972. }
  973. fallthrough;
  974. case PHY_FUNC_RELEASE_SPINUP_HOLD:
  975. default:
  976. return -EOPNOTSUPP;
  977. }
  978. return 0;
  979. }
  980. static void hisi_sas_task_done(struct sas_task *task)
  981. {
  982. del_timer(&task->slow_task->timer);
  983. complete(&task->slow_task->completion);
  984. }
  985. static void hisi_sas_tmf_timedout(struct timer_list *t)
  986. {
  987. struct sas_task_slow *slow = from_timer(slow, t, timer);
  988. struct sas_task *task = slow->task;
  989. unsigned long flags;
  990. bool is_completed = true;
  991. spin_lock_irqsave(&task->task_state_lock, flags);
  992. if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
  993. task->task_state_flags |= SAS_TASK_STATE_ABORTED;
  994. is_completed = false;
  995. }
  996. spin_unlock_irqrestore(&task->task_state_lock, flags);
  997. if (!is_completed)
  998. complete(&task->slow_task->completion);
  999. }
  1000. #define TASK_TIMEOUT 20
  1001. #define TASK_RETRY 3
  1002. #define INTERNAL_ABORT_TIMEOUT 6
  1003. static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
  1004. void *parameter, u32 para_len,
  1005. struct hisi_sas_tmf_task *tmf)
  1006. {
  1007. struct hisi_sas_device *sas_dev = device->lldd_dev;
  1008. struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
  1009. struct device *dev = hisi_hba->dev;
  1010. struct sas_task *task;
  1011. int res, retry;
  1012. for (retry = 0; retry < TASK_RETRY; retry++) {
  1013. task = sas_alloc_slow_task(GFP_KERNEL);
  1014. if (!task)
  1015. return -ENOMEM;
  1016. task->dev = device;
  1017. task->task_proto = device->tproto;
  1018. if (dev_is_sata(device)) {
  1019. task->ata_task.device_control_reg_update = 1;
  1020. memcpy(&task->ata_task.fis, parameter, para_len);
  1021. } else {
  1022. memcpy(&task->ssp_task, parameter, para_len);
  1023. }
  1024. task->task_done = hisi_sas_task_done;
  1025. task->slow_task->timer.function = hisi_sas_tmf_timedout;
  1026. task->slow_task->timer.expires = jiffies + TASK_TIMEOUT * HZ;
  1027. add_timer(&task->slow_task->timer);
  1028. res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
  1029. if (res) {
  1030. del_timer(&task->slow_task->timer);
  1031. dev_err(dev, "abort tmf: executing internal task failed: %d\n",
  1032. res);
  1033. goto ex_err;
  1034. }
  1035. wait_for_completion(&task->slow_task->completion);
  1036. res = TMF_RESP_FUNC_FAILED;
  1037. /* Even TMF timed out, return direct. */
  1038. if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  1039. if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
  1040. struct hisi_sas_slot *slot = task->lldd_task;
  1041. dev_err(dev, "abort tmf: TMF task timeout and not done\n");
  1042. if (slot) {
  1043. struct hisi_sas_cq *cq =
  1044. &hisi_hba->cq[slot->dlvry_queue];
  1045. /*
  1046. * sync irq to avoid free'ing task
  1047. * before using task in IO completion
  1048. */
  1049. synchronize_irq(cq->irq_no);
  1050. slot->task = NULL;
  1051. }
  1052. goto ex_err;
  1053. } else
  1054. dev_err(dev, "abort tmf: TMF task timeout\n");
  1055. }
  1056. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  1057. task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
  1058. res = TMF_RESP_FUNC_COMPLETE;
  1059. break;
  1060. }
  1061. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  1062. task->task_status.stat == TMF_RESP_FUNC_SUCC) {
  1063. res = TMF_RESP_FUNC_SUCC;
  1064. break;
  1065. }
  1066. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  1067. task->task_status.stat == SAS_DATA_UNDERRUN) {
  1068. /* no error, but return the number of bytes of
  1069. * underrun
  1070. */
  1071. dev_warn(dev, "abort tmf: task to dev %016llx resp: 0x%x sts 0x%x underrun\n",
  1072. SAS_ADDR(device->sas_addr),
  1073. task->task_status.resp,
  1074. task->task_status.stat);
  1075. res = task->task_status.residual;
  1076. break;
  1077. }
  1078. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  1079. task->task_status.stat == SAS_DATA_OVERRUN) {
  1080. dev_warn(dev, "abort tmf: blocked task error\n");
  1081. res = -EMSGSIZE;
  1082. break;
  1083. }
  1084. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  1085. task->task_status.stat == SAS_OPEN_REJECT) {
  1086. dev_warn(dev, "abort tmf: open reject failed\n");
  1087. res = -EIO;
  1088. } else {
  1089. dev_warn(dev, "abort tmf: task to dev %016llx resp: 0x%x status 0x%x\n",
  1090. SAS_ADDR(device->sas_addr),
  1091. task->task_status.resp,
  1092. task->task_status.stat);
  1093. }
  1094. sas_free_task(task);
  1095. task = NULL;
  1096. }
  1097. ex_err:
  1098. if (retry == TASK_RETRY)
  1099. dev_warn(dev, "abort tmf: executing internal task failed!\n");
  1100. sas_free_task(task);
  1101. return res;
  1102. }
  1103. static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
  1104. bool reset, int pmp, u8 *fis)
  1105. {
  1106. struct ata_taskfile tf;
  1107. ata_tf_init(dev, &tf);
  1108. if (reset)
  1109. tf.ctl |= ATA_SRST;
  1110. else
  1111. tf.ctl &= ~ATA_SRST;
  1112. tf.command = ATA_CMD_DEV_RESET;
  1113. ata_tf_to_fis(&tf, pmp, 0, fis);
  1114. }
  1115. static int hisi_sas_softreset_ata_disk(struct domain_device *device)
  1116. {
  1117. u8 fis[20] = {0};
  1118. struct ata_port *ap = device->sata_dev.ap;
  1119. struct ata_link *link;
  1120. int rc = TMF_RESP_FUNC_FAILED;
  1121. struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
  1122. struct device *dev = hisi_hba->dev;
  1123. int s = sizeof(struct host_to_dev_fis);
  1124. ata_for_each_link(link, ap, EDGE) {
  1125. int pmp = sata_srst_pmp(link);
  1126. hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
  1127. rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
  1128. if (rc != TMF_RESP_FUNC_COMPLETE)
  1129. break;
  1130. }
  1131. if (rc == TMF_RESP_FUNC_COMPLETE) {
  1132. ata_for_each_link(link, ap, EDGE) {
  1133. int pmp = sata_srst_pmp(link);
  1134. hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
  1135. rc = hisi_sas_exec_internal_tmf_task(device, fis,
  1136. s, NULL);
  1137. if (rc != TMF_RESP_FUNC_COMPLETE)
  1138. dev_err(dev, "ata disk de-reset failed\n");
  1139. }
  1140. } else {
  1141. dev_err(dev, "ata disk reset failed\n");
  1142. }
  1143. if (rc == TMF_RESP_FUNC_COMPLETE)
  1144. hisi_sas_release_task(hisi_hba, device);
  1145. return rc;
  1146. }
  1147. static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
  1148. u8 *lun, struct hisi_sas_tmf_task *tmf)
  1149. {
  1150. struct sas_ssp_task ssp_task;
  1151. if (!(device->tproto & SAS_PROTOCOL_SSP))
  1152. return TMF_RESP_FUNC_ESUPP;
  1153. memcpy(ssp_task.LUN, lun, 8);
  1154. return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
  1155. sizeof(ssp_task), tmf);
  1156. }
  1157. static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba)
  1158. {
  1159. u32 state = hisi_hba->hw->get_phys_state(hisi_hba);
  1160. int i;
  1161. for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
  1162. struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
  1163. struct domain_device *device = sas_dev->sas_device;
  1164. struct asd_sas_port *sas_port;
  1165. struct hisi_sas_port *port;
  1166. struct hisi_sas_phy *phy = NULL;
  1167. struct asd_sas_phy *sas_phy;
  1168. if ((sas_dev->dev_type == SAS_PHY_UNUSED)
  1169. || !device || !device->port)
  1170. continue;
  1171. sas_port = device->port;
  1172. port = to_hisi_sas_port(sas_port);
  1173. list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el)
  1174. if (state & BIT(sas_phy->id)) {
  1175. phy = sas_phy->lldd_phy;
  1176. break;
  1177. }
  1178. if (phy) {
  1179. port->id = phy->port_id;
  1180. /* Update linkrate of directly attached device. */
  1181. if (!device->parent)
  1182. device->linkrate = phy->sas_phy.linkrate;
  1183. hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
  1184. } else
  1185. port->id = 0xff;
  1186. }
  1187. }
  1188. static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 state)
  1189. {
  1190. struct asd_sas_port *_sas_port = NULL;
  1191. int phy_no;
  1192. for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
  1193. struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
  1194. struct asd_sas_phy *sas_phy = &phy->sas_phy;
  1195. struct asd_sas_port *sas_port = sas_phy->port;
  1196. bool do_port_check = _sas_port != sas_port;
  1197. if (!sas_phy->phy->enabled)
  1198. continue;
  1199. /* Report PHY state change to libsas */
  1200. if (state & BIT(phy_no)) {
  1201. if (do_port_check && sas_port && sas_port->port_dev) {
  1202. struct domain_device *dev = sas_port->port_dev;
  1203. _sas_port = sas_port;
  1204. if (dev_is_expander(dev->dev_type))
  1205. sas_notify_port_event(sas_phy,
  1206. PORTE_BROADCAST_RCVD);
  1207. }
  1208. } else {
  1209. hisi_sas_phy_down(hisi_hba, phy_no, 0);
  1210. }
  1211. }
  1212. }
  1213. static void hisi_sas_reset_init_all_devices(struct hisi_hba *hisi_hba)
  1214. {
  1215. struct hisi_sas_device *sas_dev;
  1216. struct domain_device *device;
  1217. int i;
  1218. for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
  1219. sas_dev = &hisi_hba->devices[i];
  1220. device = sas_dev->sas_device;
  1221. if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
  1222. continue;
  1223. hisi_sas_init_device(device);
  1224. }
  1225. }
  1226. static void hisi_sas_send_ata_reset_each_phy(struct hisi_hba *hisi_hba,
  1227. struct asd_sas_port *sas_port,
  1228. struct domain_device *device)
  1229. {
  1230. struct hisi_sas_tmf_task tmf_task = { .force_phy = 1 };
  1231. struct ata_port *ap = device->sata_dev.ap;
  1232. struct device *dev = hisi_hba->dev;
  1233. int s = sizeof(struct host_to_dev_fis);
  1234. int rc = TMF_RESP_FUNC_FAILED;
  1235. struct asd_sas_phy *sas_phy;
  1236. struct ata_link *link;
  1237. u8 fis[20] = {0};
  1238. u32 state;
  1239. state = hisi_hba->hw->get_phys_state(hisi_hba);
  1240. list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el) {
  1241. if (!(state & BIT(sas_phy->id)))
  1242. continue;
  1243. ata_for_each_link(link, ap, EDGE) {
  1244. int pmp = sata_srst_pmp(link);
  1245. tmf_task.phy_id = sas_phy->id;
  1246. hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
  1247. rc = hisi_sas_exec_internal_tmf_task(device, fis, s,
  1248. &tmf_task);
  1249. if (rc != TMF_RESP_FUNC_COMPLETE) {
  1250. dev_err(dev, "phy%d ata reset failed rc=%d\n",
  1251. sas_phy->id, rc);
  1252. break;
  1253. }
  1254. }
  1255. }
  1256. }
  1257. static void hisi_sas_terminate_stp_reject(struct hisi_hba *hisi_hba)
  1258. {
  1259. struct device *dev = hisi_hba->dev;
  1260. int port_no, rc, i;
  1261. for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
  1262. struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
  1263. struct domain_device *device = sas_dev->sas_device;
  1264. if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
  1265. continue;
  1266. rc = hisi_sas_internal_task_abort(hisi_hba, device,
  1267. HISI_SAS_INT_ABT_DEV, 0);
  1268. if (rc < 0)
  1269. dev_err(dev, "STP reject: abort dev failed %d\n", rc);
  1270. }
  1271. for (port_no = 0; port_no < hisi_hba->n_phy; port_no++) {
  1272. struct hisi_sas_port *port = &hisi_hba->port[port_no];
  1273. struct asd_sas_port *sas_port = &port->sas_port;
  1274. struct domain_device *port_dev = sas_port->port_dev;
  1275. struct domain_device *device;
  1276. if (!port_dev || !dev_is_expander(port_dev->dev_type))
  1277. continue;
  1278. /* Try to find a SATA device */
  1279. list_for_each_entry(device, &sas_port->dev_list,
  1280. dev_list_node) {
  1281. if (dev_is_sata(device)) {
  1282. hisi_sas_send_ata_reset_each_phy(hisi_hba,
  1283. sas_port,
  1284. device);
  1285. break;
  1286. }
  1287. }
  1288. }
  1289. }
  1290. void hisi_sas_controller_reset_prepare(struct hisi_hba *hisi_hba)
  1291. {
  1292. struct Scsi_Host *shost = hisi_hba->shost;
  1293. down(&hisi_hba->sem);
  1294. hisi_hba->phy_state = hisi_hba->hw->get_phys_state(hisi_hba);
  1295. scsi_block_requests(shost);
  1296. hisi_hba->hw->wait_cmds_complete_timeout(hisi_hba, 100, 5000);
  1297. if (timer_pending(&hisi_hba->timer))
  1298. del_timer_sync(&hisi_hba->timer);
  1299. set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
  1300. }
  1301. EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_prepare);
  1302. void hisi_sas_controller_reset_done(struct hisi_hba *hisi_hba)
  1303. {
  1304. struct Scsi_Host *shost = hisi_hba->shost;
  1305. /* Init and wait for PHYs to come up and all libsas event finished. */
  1306. hisi_hba->hw->phys_init(hisi_hba);
  1307. msleep(1000);
  1308. hisi_sas_refresh_port_id(hisi_hba);
  1309. clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
  1310. if (hisi_hba->reject_stp_links_msk)
  1311. hisi_sas_terminate_stp_reject(hisi_hba);
  1312. hisi_sas_reset_init_all_devices(hisi_hba);
  1313. up(&hisi_hba->sem);
  1314. scsi_unblock_requests(shost);
  1315. clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
  1316. hisi_sas_rescan_topology(hisi_hba, hisi_hba->phy_state);
  1317. }
  1318. EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_done);
  1319. static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
  1320. {
  1321. struct device *dev = hisi_hba->dev;
  1322. struct Scsi_Host *shost = hisi_hba->shost;
  1323. int rc;
  1324. if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct[0].itct)
  1325. queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
  1326. if (!hisi_hba->hw->soft_reset)
  1327. return -1;
  1328. if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
  1329. return -1;
  1330. dev_info(dev, "controller resetting...\n");
  1331. hisi_sas_controller_reset_prepare(hisi_hba);
  1332. rc = hisi_hba->hw->soft_reset(hisi_hba);
  1333. if (rc) {
  1334. dev_warn(dev, "controller reset failed (%d)\n", rc);
  1335. clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
  1336. up(&hisi_hba->sem);
  1337. scsi_unblock_requests(shost);
  1338. clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
  1339. return rc;
  1340. }
  1341. hisi_sas_controller_reset_done(hisi_hba);
  1342. dev_info(dev, "controller reset complete\n");
  1343. return 0;
  1344. }
  1345. static int hisi_sas_abort_task(struct sas_task *task)
  1346. {
  1347. struct scsi_lun lun;
  1348. struct hisi_sas_tmf_task tmf_task;
  1349. struct domain_device *device = task->dev;
  1350. struct hisi_sas_device *sas_dev = device->lldd_dev;
  1351. struct hisi_hba *hisi_hba;
  1352. struct device *dev;
  1353. int rc = TMF_RESP_FUNC_FAILED;
  1354. unsigned long flags;
  1355. if (!sas_dev)
  1356. return TMF_RESP_FUNC_FAILED;
  1357. hisi_hba = dev_to_hisi_hba(task->dev);
  1358. dev = hisi_hba->dev;
  1359. spin_lock_irqsave(&task->task_state_lock, flags);
  1360. if (task->task_state_flags & SAS_TASK_STATE_DONE) {
  1361. struct hisi_sas_slot *slot = task->lldd_task;
  1362. struct hisi_sas_cq *cq;
  1363. if (slot) {
  1364. /*
  1365. * sync irq to avoid free'ing task
  1366. * before using task in IO completion
  1367. */
  1368. cq = &hisi_hba->cq[slot->dlvry_queue];
  1369. synchronize_irq(cq->irq_no);
  1370. }
  1371. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1372. rc = TMF_RESP_FUNC_COMPLETE;
  1373. goto out;
  1374. }
  1375. task->task_state_flags |= SAS_TASK_STATE_ABORTED;
  1376. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1377. if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
  1378. struct scsi_cmnd *cmnd = task->uldd_task;
  1379. struct hisi_sas_slot *slot = task->lldd_task;
  1380. u16 tag = slot->idx;
  1381. int rc2;
  1382. int_to_scsilun(cmnd->device->lun, &lun);
  1383. tmf_task.tmf = TMF_ABORT_TASK;
  1384. tmf_task.tag_of_task_to_be_managed = tag;
  1385. rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
  1386. &tmf_task);
  1387. rc2 = hisi_sas_internal_task_abort(hisi_hba, device,
  1388. HISI_SAS_INT_ABT_CMD, tag);
  1389. if (rc2 < 0) {
  1390. dev_err(dev, "abort task: internal abort (%d)\n", rc2);
  1391. return TMF_RESP_FUNC_FAILED;
  1392. }
  1393. /*
  1394. * If the TMF finds that the IO is not in the device and also
  1395. * the internal abort does not succeed, then it is safe to
  1396. * free the slot.
  1397. * Note: if the internal abort succeeds then the slot
  1398. * will have already been completed
  1399. */
  1400. if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
  1401. if (task->lldd_task)
  1402. hisi_sas_do_release_task(hisi_hba, task, slot);
  1403. }
  1404. } else if (task->task_proto & SAS_PROTOCOL_SATA ||
  1405. task->task_proto & SAS_PROTOCOL_STP) {
  1406. if (task->dev->dev_type == SAS_SATA_DEV) {
  1407. rc = hisi_sas_internal_task_abort(hisi_hba, device,
  1408. HISI_SAS_INT_ABT_DEV,
  1409. 0);
  1410. if (rc < 0) {
  1411. dev_err(dev, "abort task: internal abort failed\n");
  1412. goto out;
  1413. }
  1414. hisi_sas_dereg_device(hisi_hba, device);
  1415. rc = hisi_sas_softreset_ata_disk(device);
  1416. }
  1417. } else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) {
  1418. /* SMP */
  1419. struct hisi_sas_slot *slot = task->lldd_task;
  1420. u32 tag = slot->idx;
  1421. struct hisi_sas_cq *cq = &hisi_hba->cq[slot->dlvry_queue];
  1422. rc = hisi_sas_internal_task_abort(hisi_hba, device,
  1423. HISI_SAS_INT_ABT_CMD, tag);
  1424. if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) &&
  1425. task->lldd_task) {
  1426. /*
  1427. * sync irq to avoid free'ing task
  1428. * before using task in IO completion
  1429. */
  1430. synchronize_irq(cq->irq_no);
  1431. slot->task = NULL;
  1432. }
  1433. }
  1434. out:
  1435. if (rc != TMF_RESP_FUNC_COMPLETE)
  1436. dev_notice(dev, "abort task: rc=%d\n", rc);
  1437. return rc;
  1438. }
  1439. static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
  1440. {
  1441. struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
  1442. struct device *dev = hisi_hba->dev;
  1443. struct hisi_sas_tmf_task tmf_task;
  1444. int rc;
  1445. rc = hisi_sas_internal_task_abort(hisi_hba, device,
  1446. HISI_SAS_INT_ABT_DEV, 0);
  1447. if (rc < 0) {
  1448. dev_err(dev, "abort task set: internal abort rc=%d\n", rc);
  1449. return TMF_RESP_FUNC_FAILED;
  1450. }
  1451. hisi_sas_dereg_device(hisi_hba, device);
  1452. tmf_task.tmf = TMF_ABORT_TASK_SET;
  1453. rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
  1454. if (rc == TMF_RESP_FUNC_COMPLETE)
  1455. hisi_sas_release_task(hisi_hba, device);
  1456. return rc;
  1457. }
  1458. static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
  1459. {
  1460. struct hisi_sas_tmf_task tmf_task;
  1461. int rc;
  1462. tmf_task.tmf = TMF_CLEAR_ACA;
  1463. rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
  1464. return rc;
  1465. }
  1466. static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
  1467. {
  1468. struct sas_phy *local_phy = sas_get_local_phy(device);
  1469. struct hisi_sas_device *sas_dev = device->lldd_dev;
  1470. struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
  1471. struct sas_ha_struct *sas_ha = &hisi_hba->sha;
  1472. DECLARE_COMPLETION_ONSTACK(phyreset);
  1473. int rc, reset_type;
  1474. if (!local_phy->enabled) {
  1475. sas_put_local_phy(local_phy);
  1476. return -ENODEV;
  1477. }
  1478. if (scsi_is_sas_phy_local(local_phy)) {
  1479. struct asd_sas_phy *sas_phy =
  1480. sas_ha->sas_phy[local_phy->number];
  1481. struct hisi_sas_phy *phy =
  1482. container_of(sas_phy, struct hisi_sas_phy, sas_phy);
  1483. phy->in_reset = 1;
  1484. phy->reset_completion = &phyreset;
  1485. }
  1486. reset_type = (sas_dev->dev_status == HISI_SAS_DEV_INIT ||
  1487. !dev_is_sata(device)) ? true : false;
  1488. rc = sas_phy_reset(local_phy, reset_type);
  1489. sas_put_local_phy(local_phy);
  1490. if (scsi_is_sas_phy_local(local_phy)) {
  1491. struct asd_sas_phy *sas_phy =
  1492. sas_ha->sas_phy[local_phy->number];
  1493. struct hisi_sas_phy *phy =
  1494. container_of(sas_phy, struct hisi_sas_phy, sas_phy);
  1495. int ret = wait_for_completion_timeout(&phyreset, 2 * HZ);
  1496. unsigned long flags;
  1497. spin_lock_irqsave(&phy->lock, flags);
  1498. phy->reset_completion = NULL;
  1499. phy->in_reset = 0;
  1500. spin_unlock_irqrestore(&phy->lock, flags);
  1501. /* report PHY down if timed out */
  1502. if (!ret)
  1503. hisi_sas_phy_down(hisi_hba, sas_phy->id, 0);
  1504. } else if (sas_dev->dev_status != HISI_SAS_DEV_INIT) {
  1505. /*
  1506. * If in init state, we rely on caller to wait for link to be
  1507. * ready; otherwise, except phy reset is fail, delay.
  1508. */
  1509. if (!rc)
  1510. msleep(2000);
  1511. }
  1512. return rc;
  1513. }
  1514. static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
  1515. {
  1516. struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
  1517. struct device *dev = hisi_hba->dev;
  1518. int rc;
  1519. rc = hisi_sas_internal_task_abort(hisi_hba, device,
  1520. HISI_SAS_INT_ABT_DEV, 0);
  1521. if (rc < 0) {
  1522. dev_err(dev, "I_T nexus reset: internal abort (%d)\n", rc);
  1523. return TMF_RESP_FUNC_FAILED;
  1524. }
  1525. hisi_sas_dereg_device(hisi_hba, device);
  1526. if (dev_is_sata(device)) {
  1527. rc = hisi_sas_softreset_ata_disk(device);
  1528. if (rc == TMF_RESP_FUNC_FAILED)
  1529. return TMF_RESP_FUNC_FAILED;
  1530. }
  1531. rc = hisi_sas_debug_I_T_nexus_reset(device);
  1532. if ((rc == TMF_RESP_FUNC_COMPLETE) || (rc == -ENODEV))
  1533. hisi_sas_release_task(hisi_hba, device);
  1534. return rc;
  1535. }
  1536. static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
  1537. {
  1538. struct hisi_sas_device *sas_dev = device->lldd_dev;
  1539. struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
  1540. struct device *dev = hisi_hba->dev;
  1541. int rc = TMF_RESP_FUNC_FAILED;
  1542. /* Clear internal IO and then lu reset */
  1543. rc = hisi_sas_internal_task_abort(hisi_hba, device,
  1544. HISI_SAS_INT_ABT_DEV, 0);
  1545. if (rc < 0) {
  1546. dev_err(dev, "lu_reset: internal abort failed\n");
  1547. goto out;
  1548. }
  1549. hisi_sas_dereg_device(hisi_hba, device);
  1550. if (dev_is_sata(device)) {
  1551. struct sas_phy *phy;
  1552. phy = sas_get_local_phy(device);
  1553. rc = sas_phy_reset(phy, true);
  1554. if (rc == 0)
  1555. hisi_sas_release_task(hisi_hba, device);
  1556. sas_put_local_phy(phy);
  1557. } else {
  1558. struct hisi_sas_tmf_task tmf_task = { .tmf = TMF_LU_RESET };
  1559. rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
  1560. if (rc == TMF_RESP_FUNC_COMPLETE)
  1561. hisi_sas_release_task(hisi_hba, device);
  1562. }
  1563. out:
  1564. if (rc != TMF_RESP_FUNC_COMPLETE)
  1565. dev_err(dev, "lu_reset: for device[%d]:rc= %d\n",
  1566. sas_dev->device_id, rc);
  1567. return rc;
  1568. }
  1569. static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
  1570. {
  1571. struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
  1572. struct device *dev = hisi_hba->dev;
  1573. HISI_SAS_DECLARE_RST_WORK_ON_STACK(r);
  1574. int rc, i;
  1575. queue_work(hisi_hba->wq, &r.work);
  1576. wait_for_completion(r.completion);
  1577. if (!r.done)
  1578. return TMF_RESP_FUNC_FAILED;
  1579. for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
  1580. struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
  1581. struct domain_device *device = sas_dev->sas_device;
  1582. if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device ||
  1583. dev_is_expander(device->dev_type))
  1584. continue;
  1585. rc = hisi_sas_debug_I_T_nexus_reset(device);
  1586. if (rc != TMF_RESP_FUNC_COMPLETE)
  1587. dev_info(dev, "clear nexus ha: for device[%d] rc=%d\n",
  1588. sas_dev->device_id, rc);
  1589. }
  1590. hisi_sas_release_tasks(hisi_hba);
  1591. return TMF_RESP_FUNC_COMPLETE;
  1592. }
  1593. static int hisi_sas_query_task(struct sas_task *task)
  1594. {
  1595. struct scsi_lun lun;
  1596. struct hisi_sas_tmf_task tmf_task;
  1597. int rc = TMF_RESP_FUNC_FAILED;
  1598. if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
  1599. struct scsi_cmnd *cmnd = task->uldd_task;
  1600. struct domain_device *device = task->dev;
  1601. struct hisi_sas_slot *slot = task->lldd_task;
  1602. u32 tag = slot->idx;
  1603. int_to_scsilun(cmnd->device->lun, &lun);
  1604. tmf_task.tmf = TMF_QUERY_TASK;
  1605. tmf_task.tag_of_task_to_be_managed = tag;
  1606. rc = hisi_sas_debug_issue_ssp_tmf(device,
  1607. lun.scsi_lun,
  1608. &tmf_task);
  1609. switch (rc) {
  1610. /* The task is still in Lun, release it then */
  1611. case TMF_RESP_FUNC_SUCC:
  1612. /* The task is not in Lun or failed, reset the phy */
  1613. case TMF_RESP_FUNC_FAILED:
  1614. case TMF_RESP_FUNC_COMPLETE:
  1615. break;
  1616. default:
  1617. rc = TMF_RESP_FUNC_FAILED;
  1618. break;
  1619. }
  1620. }
  1621. return rc;
  1622. }
  1623. static int
  1624. hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
  1625. struct sas_task *task, int abort_flag,
  1626. int task_tag, struct hisi_sas_dq *dq)
  1627. {
  1628. struct domain_device *device = task->dev;
  1629. struct hisi_sas_device *sas_dev = device->lldd_dev;
  1630. struct device *dev = hisi_hba->dev;
  1631. struct hisi_sas_port *port;
  1632. struct hisi_sas_slot *slot;
  1633. struct asd_sas_port *sas_port = device->port;
  1634. struct hisi_sas_cmd_hdr *cmd_hdr_base;
  1635. int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
  1636. unsigned long flags;
  1637. int wr_q_index;
  1638. if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
  1639. return -EINVAL;
  1640. if (!device->port)
  1641. return -1;
  1642. port = to_hisi_sas_port(sas_port);
  1643. /* simply get a slot and send abort command */
  1644. rc = hisi_sas_slot_index_alloc(hisi_hba, NULL);
  1645. if (rc < 0)
  1646. goto err_out;
  1647. slot_idx = rc;
  1648. slot = &hisi_hba->slot_info[slot_idx];
  1649. spin_lock(&dq->lock);
  1650. wr_q_index = dq->wr_point;
  1651. dq->wr_point = (dq->wr_point + 1) % HISI_SAS_QUEUE_SLOTS;
  1652. list_add_tail(&slot->delivery, &dq->list);
  1653. spin_unlock(&dq->lock);
  1654. spin_lock(&sas_dev->lock);
  1655. list_add_tail(&slot->entry, &sas_dev->list);
  1656. spin_unlock(&sas_dev->lock);
  1657. dlvry_queue = dq->id;
  1658. dlvry_queue_slot = wr_q_index;
  1659. slot->device_id = sas_dev->device_id;
  1660. slot->n_elem = n_elem;
  1661. slot->dlvry_queue = dlvry_queue;
  1662. slot->dlvry_queue_slot = dlvry_queue_slot;
  1663. cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
  1664. slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
  1665. slot->task = task;
  1666. slot->port = port;
  1667. slot->is_internal = true;
  1668. task->lldd_task = slot;
  1669. memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
  1670. memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
  1671. memset(hisi_sas_status_buf_addr_mem(slot), 0,
  1672. sizeof(struct hisi_sas_err_record));
  1673. hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
  1674. abort_flag, task_tag);
  1675. spin_lock_irqsave(&task->task_state_lock, flags);
  1676. task->task_state_flags |= SAS_TASK_AT_INITIATOR;
  1677. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1678. WRITE_ONCE(slot->ready, 1);
  1679. /* send abort command to the chip */
  1680. spin_lock(&dq->lock);
  1681. hisi_hba->hw->start_delivery(dq);
  1682. spin_unlock(&dq->lock);
  1683. return 0;
  1684. err_out:
  1685. dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
  1686. return rc;
  1687. }
  1688. /**
  1689. * _hisi_sas_internal_task_abort -- execute an internal
  1690. * abort command for single IO command or a device
  1691. * @hisi_hba: host controller struct
  1692. * @device: domain device
  1693. * @abort_flag: mode of operation, device or single IO
  1694. * @tag: tag of IO to be aborted (only relevant to single
  1695. * IO mode)
  1696. * @dq: delivery queue for this internal abort command
  1697. */
  1698. static int
  1699. _hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
  1700. struct domain_device *device, int abort_flag,
  1701. int tag, struct hisi_sas_dq *dq)
  1702. {
  1703. struct sas_task *task;
  1704. struct hisi_sas_device *sas_dev = device->lldd_dev;
  1705. struct device *dev = hisi_hba->dev;
  1706. int res;
  1707. /*
  1708. * The interface is not realized means this HW don't support internal
  1709. * abort, or don't need to do internal abort. Then here, we return
  1710. * TMF_RESP_FUNC_FAILED and let other steps go on, which depends that
  1711. * the internal abort has been executed and returned CQ.
  1712. */
  1713. if (!hisi_hba->hw->prep_abort)
  1714. return TMF_RESP_FUNC_FAILED;
  1715. task = sas_alloc_slow_task(GFP_KERNEL);
  1716. if (!task)
  1717. return -ENOMEM;
  1718. task->dev = device;
  1719. task->task_proto = device->tproto;
  1720. task->task_done = hisi_sas_task_done;
  1721. task->slow_task->timer.function = hisi_sas_tmf_timedout;
  1722. task->slow_task->timer.expires = jiffies + INTERNAL_ABORT_TIMEOUT * HZ;
  1723. add_timer(&task->slow_task->timer);
  1724. res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
  1725. task, abort_flag, tag, dq);
  1726. if (res) {
  1727. del_timer(&task->slow_task->timer);
  1728. dev_err(dev, "internal task abort: executing internal task failed: %d\n",
  1729. res);
  1730. goto exit;
  1731. }
  1732. wait_for_completion(&task->slow_task->completion);
  1733. res = TMF_RESP_FUNC_FAILED;
  1734. /* Internal abort timed out */
  1735. if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  1736. if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct[0].itct)
  1737. queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
  1738. if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
  1739. struct hisi_sas_slot *slot = task->lldd_task;
  1740. if (slot) {
  1741. struct hisi_sas_cq *cq =
  1742. &hisi_hba->cq[slot->dlvry_queue];
  1743. /*
  1744. * sync irq to avoid free'ing task
  1745. * before using task in IO completion
  1746. */
  1747. synchronize_irq(cq->irq_no);
  1748. slot->task = NULL;
  1749. }
  1750. dev_err(dev, "internal task abort: timeout and not done.\n");
  1751. res = -EIO;
  1752. goto exit;
  1753. } else
  1754. dev_err(dev, "internal task abort: timeout.\n");
  1755. }
  1756. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  1757. task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
  1758. res = TMF_RESP_FUNC_COMPLETE;
  1759. goto exit;
  1760. }
  1761. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  1762. task->task_status.stat == TMF_RESP_FUNC_SUCC) {
  1763. res = TMF_RESP_FUNC_SUCC;
  1764. goto exit;
  1765. }
  1766. exit:
  1767. dev_dbg(dev, "internal task abort: task to dev %016llx task=%pK resp: 0x%x sts 0x%x\n",
  1768. SAS_ADDR(device->sas_addr), task,
  1769. task->task_status.resp, /* 0 is complete, -1 is undelivered */
  1770. task->task_status.stat);
  1771. sas_free_task(task);
  1772. return res;
  1773. }
  1774. static int
  1775. hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
  1776. struct domain_device *device,
  1777. int abort_flag, int tag)
  1778. {
  1779. struct hisi_sas_slot *slot;
  1780. struct device *dev = hisi_hba->dev;
  1781. struct hisi_sas_dq *dq;
  1782. int i, rc;
  1783. switch (abort_flag) {
  1784. case HISI_SAS_INT_ABT_CMD:
  1785. slot = &hisi_hba->slot_info[tag];
  1786. dq = &hisi_hba->dq[slot->dlvry_queue];
  1787. return _hisi_sas_internal_task_abort(hisi_hba, device,
  1788. abort_flag, tag, dq);
  1789. case HISI_SAS_INT_ABT_DEV:
  1790. for (i = 0; i < hisi_hba->cq_nvecs; i++) {
  1791. struct hisi_sas_cq *cq = &hisi_hba->cq[i];
  1792. const struct cpumask *mask = cq->irq_mask;
  1793. if (mask && !cpumask_intersects(cpu_online_mask, mask))
  1794. continue;
  1795. dq = &hisi_hba->dq[i];
  1796. rc = _hisi_sas_internal_task_abort(hisi_hba, device,
  1797. abort_flag, tag,
  1798. dq);
  1799. if (rc)
  1800. return rc;
  1801. }
  1802. break;
  1803. default:
  1804. dev_err(dev, "Unrecognised internal abort flag (%d)\n",
  1805. abort_flag);
  1806. return -EINVAL;
  1807. }
  1808. return 0;
  1809. }
  1810. static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
  1811. {
  1812. hisi_sas_port_notify_formed(sas_phy);
  1813. }
  1814. static int hisi_sas_write_gpio(struct sas_ha_struct *sha, u8 reg_type,
  1815. u8 reg_index, u8 reg_count, u8 *write_data)
  1816. {
  1817. struct hisi_hba *hisi_hba = sha->lldd_ha;
  1818. if (!hisi_hba->hw->write_gpio)
  1819. return -EOPNOTSUPP;
  1820. return hisi_hba->hw->write_gpio(hisi_hba, reg_type,
  1821. reg_index, reg_count, write_data);
  1822. }
  1823. static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
  1824. {
  1825. struct asd_sas_phy *sas_phy = &phy->sas_phy;
  1826. struct sas_phy *sphy = sas_phy->phy;
  1827. unsigned long flags;
  1828. phy->phy_attached = 0;
  1829. phy->phy_type = 0;
  1830. phy->port = NULL;
  1831. spin_lock_irqsave(&phy->lock, flags);
  1832. if (phy->enable)
  1833. sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
  1834. else
  1835. sphy->negotiated_linkrate = SAS_PHY_DISABLED;
  1836. spin_unlock_irqrestore(&phy->lock, flags);
  1837. }
  1838. void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
  1839. {
  1840. struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
  1841. struct asd_sas_phy *sas_phy = &phy->sas_phy;
  1842. struct device *dev = hisi_hba->dev;
  1843. if (rdy) {
  1844. /* Phy down but ready */
  1845. hisi_sas_bytes_dmaed(hisi_hba, phy_no);
  1846. hisi_sas_port_notify_formed(sas_phy);
  1847. } else {
  1848. struct hisi_sas_port *port = phy->port;
  1849. if (test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags) ||
  1850. phy->in_reset) {
  1851. dev_info(dev, "ignore flutter phy%d down\n", phy_no);
  1852. return;
  1853. }
  1854. /* Phy down and not ready */
  1855. sas_notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
  1856. sas_phy_disconnected(sas_phy);
  1857. if (port) {
  1858. if (phy->phy_type & PORT_TYPE_SAS) {
  1859. int port_id = port->id;
  1860. if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
  1861. port_id))
  1862. port->port_attached = 0;
  1863. } else if (phy->phy_type & PORT_TYPE_SATA)
  1864. port->port_attached = 0;
  1865. }
  1866. hisi_sas_phy_disconnected(phy);
  1867. }
  1868. }
  1869. EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
  1870. void hisi_sas_sync_irqs(struct hisi_hba *hisi_hba)
  1871. {
  1872. int i;
  1873. for (i = 0; i < hisi_hba->cq_nvecs; i++) {
  1874. struct hisi_sas_cq *cq = &hisi_hba->cq[i];
  1875. synchronize_irq(cq->irq_no);
  1876. }
  1877. }
  1878. EXPORT_SYMBOL_GPL(hisi_sas_sync_irqs);
  1879. int hisi_sas_host_reset(struct Scsi_Host *shost, int reset_type)
  1880. {
  1881. struct hisi_hba *hisi_hba = shost_priv(shost);
  1882. if (reset_type != SCSI_ADAPTER_RESET)
  1883. return -EOPNOTSUPP;
  1884. queue_work(hisi_hba->wq, &hisi_hba->rst_work);
  1885. return 0;
  1886. }
  1887. EXPORT_SYMBOL_GPL(hisi_sas_host_reset);
  1888. struct scsi_transport_template *hisi_sas_stt;
  1889. EXPORT_SYMBOL_GPL(hisi_sas_stt);
  1890. static struct sas_domain_function_template hisi_sas_transport_ops = {
  1891. .lldd_dev_found = hisi_sas_dev_found,
  1892. .lldd_dev_gone = hisi_sas_dev_gone,
  1893. .lldd_execute_task = hisi_sas_queue_command,
  1894. .lldd_control_phy = hisi_sas_control_phy,
  1895. .lldd_abort_task = hisi_sas_abort_task,
  1896. .lldd_abort_task_set = hisi_sas_abort_task_set,
  1897. .lldd_clear_aca = hisi_sas_clear_aca,
  1898. .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
  1899. .lldd_lu_reset = hisi_sas_lu_reset,
  1900. .lldd_query_task = hisi_sas_query_task,
  1901. .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha,
  1902. .lldd_port_formed = hisi_sas_port_formed,
  1903. .lldd_write_gpio = hisi_sas_write_gpio,
  1904. };
  1905. void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
  1906. {
  1907. int i, s, j, max_command_entries = HISI_SAS_MAX_COMMANDS;
  1908. struct hisi_sas_breakpoint *sata_breakpoint = hisi_hba->sata_breakpoint;
  1909. for (i = 0; i < hisi_hba->queue_count; i++) {
  1910. struct hisi_sas_cq *cq = &hisi_hba->cq[i];
  1911. struct hisi_sas_dq *dq = &hisi_hba->dq[i];
  1912. struct hisi_sas_cmd_hdr *cmd_hdr = hisi_hba->cmd_hdr[i];
  1913. s = sizeof(struct hisi_sas_cmd_hdr);
  1914. for (j = 0; j < HISI_SAS_QUEUE_SLOTS; j++)
  1915. memset(&cmd_hdr[j], 0, s);
  1916. dq->wr_point = 0;
  1917. s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
  1918. memset(hisi_hba->complete_hdr[i], 0, s);
  1919. cq->rd_point = 0;
  1920. }
  1921. s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
  1922. memset(hisi_hba->initial_fis, 0, s);
  1923. s = max_command_entries * sizeof(struct hisi_sas_iost);
  1924. memset(hisi_hba->iost, 0, s);
  1925. s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
  1926. memset(hisi_hba->breakpoint, 0, s);
  1927. s = sizeof(struct hisi_sas_sata_breakpoint);
  1928. for (j = 0; j < HISI_SAS_MAX_ITCT_ENTRIES; j++)
  1929. memset(&sata_breakpoint[j], 0, s);
  1930. }
  1931. EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
  1932. int hisi_sas_alloc(struct hisi_hba *hisi_hba)
  1933. {
  1934. struct device *dev = hisi_hba->dev;
  1935. int i, j, s, max_command_entries = HISI_SAS_MAX_COMMANDS;
  1936. int max_command_entries_ru, sz_slot_buf_ru;
  1937. int blk_cnt, slots_per_blk;
  1938. sema_init(&hisi_hba->sem, 1);
  1939. spin_lock_init(&hisi_hba->lock);
  1940. for (i = 0; i < hisi_hba->n_phy; i++) {
  1941. hisi_sas_phy_init(hisi_hba, i);
  1942. hisi_hba->port[i].port_attached = 0;
  1943. hisi_hba->port[i].id = -1;
  1944. }
  1945. for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
  1946. hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
  1947. hisi_hba->devices[i].device_id = i;
  1948. hisi_hba->devices[i].dev_status = HISI_SAS_DEV_INIT;
  1949. }
  1950. for (i = 0; i < hisi_hba->queue_count; i++) {
  1951. struct hisi_sas_cq *cq = &hisi_hba->cq[i];
  1952. struct hisi_sas_dq *dq = &hisi_hba->dq[i];
  1953. /* Completion queue structure */
  1954. cq->id = i;
  1955. cq->hisi_hba = hisi_hba;
  1956. /* Delivery queue structure */
  1957. spin_lock_init(&dq->lock);
  1958. INIT_LIST_HEAD(&dq->list);
  1959. dq->id = i;
  1960. dq->hisi_hba = hisi_hba;
  1961. /* Delivery queue */
  1962. s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
  1963. hisi_hba->cmd_hdr[i] = dmam_alloc_coherent(dev, s,
  1964. &hisi_hba->cmd_hdr_dma[i],
  1965. GFP_KERNEL);
  1966. if (!hisi_hba->cmd_hdr[i])
  1967. goto err_out;
  1968. /* Completion queue */
  1969. s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
  1970. hisi_hba->complete_hdr[i] = dmam_alloc_coherent(dev, s,
  1971. &hisi_hba->complete_hdr_dma[i],
  1972. GFP_KERNEL);
  1973. if (!hisi_hba->complete_hdr[i])
  1974. goto err_out;
  1975. }
  1976. s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
  1977. hisi_hba->itct = dmam_alloc_coherent(dev, s, &hisi_hba->itct_dma,
  1978. GFP_KERNEL);
  1979. if (!hisi_hba->itct)
  1980. goto err_out;
  1981. hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
  1982. sizeof(struct hisi_sas_slot),
  1983. GFP_KERNEL);
  1984. if (!hisi_hba->slot_info)
  1985. goto err_out;
  1986. /* roundup to avoid overly large block size */
  1987. max_command_entries_ru = roundup(max_command_entries, 64);
  1988. if (hisi_hba->prot_mask & HISI_SAS_DIX_PROT_MASK)
  1989. sz_slot_buf_ru = sizeof(struct hisi_sas_slot_dif_buf_table);
  1990. else
  1991. sz_slot_buf_ru = sizeof(struct hisi_sas_slot_buf_table);
  1992. sz_slot_buf_ru = roundup(sz_slot_buf_ru, 64);
  1993. s = max(lcm(max_command_entries_ru, sz_slot_buf_ru), PAGE_SIZE);
  1994. blk_cnt = (max_command_entries_ru * sz_slot_buf_ru) / s;
  1995. slots_per_blk = s / sz_slot_buf_ru;
  1996. for (i = 0; i < blk_cnt; i++) {
  1997. int slot_index = i * slots_per_blk;
  1998. dma_addr_t buf_dma;
  1999. void *buf;
  2000. buf = dmam_alloc_coherent(dev, s, &buf_dma,
  2001. GFP_KERNEL);
  2002. if (!buf)
  2003. goto err_out;
  2004. for (j = 0; j < slots_per_blk; j++, slot_index++) {
  2005. struct hisi_sas_slot *slot;
  2006. slot = &hisi_hba->slot_info[slot_index];
  2007. slot->buf = buf;
  2008. slot->buf_dma = buf_dma;
  2009. slot->idx = slot_index;
  2010. buf += sz_slot_buf_ru;
  2011. buf_dma += sz_slot_buf_ru;
  2012. }
  2013. }
  2014. s = max_command_entries * sizeof(struct hisi_sas_iost);
  2015. hisi_hba->iost = dmam_alloc_coherent(dev, s, &hisi_hba->iost_dma,
  2016. GFP_KERNEL);
  2017. if (!hisi_hba->iost)
  2018. goto err_out;
  2019. s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
  2020. hisi_hba->breakpoint = dmam_alloc_coherent(dev, s,
  2021. &hisi_hba->breakpoint_dma,
  2022. GFP_KERNEL);
  2023. if (!hisi_hba->breakpoint)
  2024. goto err_out;
  2025. hisi_hba->slot_index_count = max_command_entries;
  2026. s = hisi_hba->slot_index_count / BITS_PER_BYTE;
  2027. hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
  2028. if (!hisi_hba->slot_index_tags)
  2029. goto err_out;
  2030. s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
  2031. hisi_hba->initial_fis = dmam_alloc_coherent(dev, s,
  2032. &hisi_hba->initial_fis_dma,
  2033. GFP_KERNEL);
  2034. if (!hisi_hba->initial_fis)
  2035. goto err_out;
  2036. s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
  2037. hisi_hba->sata_breakpoint = dmam_alloc_coherent(dev, s,
  2038. &hisi_hba->sata_breakpoint_dma,
  2039. GFP_KERNEL);
  2040. if (!hisi_hba->sata_breakpoint)
  2041. goto err_out;
  2042. hisi_sas_slot_index_init(hisi_hba);
  2043. hisi_hba->last_slot_index = HISI_SAS_UNRESERVED_IPTT;
  2044. hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
  2045. if (!hisi_hba->wq) {
  2046. dev_err(dev, "sas_alloc: failed to create workqueue\n");
  2047. goto err_out;
  2048. }
  2049. return 0;
  2050. err_out:
  2051. return -ENOMEM;
  2052. }
  2053. EXPORT_SYMBOL_GPL(hisi_sas_alloc);
  2054. void hisi_sas_free(struct hisi_hba *hisi_hba)
  2055. {
  2056. int i;
  2057. for (i = 0; i < hisi_hba->n_phy; i++) {
  2058. struct hisi_sas_phy *phy = &hisi_hba->phy[i];
  2059. del_timer_sync(&phy->timer);
  2060. }
  2061. if (hisi_hba->wq)
  2062. destroy_workqueue(hisi_hba->wq);
  2063. }
  2064. EXPORT_SYMBOL_GPL(hisi_sas_free);
  2065. void hisi_sas_rst_work_handler(struct work_struct *work)
  2066. {
  2067. struct hisi_hba *hisi_hba =
  2068. container_of(work, struct hisi_hba, rst_work);
  2069. hisi_sas_controller_reset(hisi_hba);
  2070. }
  2071. EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
  2072. void hisi_sas_sync_rst_work_handler(struct work_struct *work)
  2073. {
  2074. struct hisi_sas_rst *rst =
  2075. container_of(work, struct hisi_sas_rst, work);
  2076. if (!hisi_sas_controller_reset(rst->hisi_hba))
  2077. rst->done = true;
  2078. complete(rst->completion);
  2079. }
  2080. EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler);
  2081. int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
  2082. {
  2083. struct device *dev = hisi_hba->dev;
  2084. struct platform_device *pdev = hisi_hba->platform_dev;
  2085. struct device_node *np = pdev ? pdev->dev.of_node : NULL;
  2086. struct clk *refclk;
  2087. if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
  2088. SAS_ADDR_SIZE)) {
  2089. dev_err(dev, "could not get property sas-addr\n");
  2090. return -ENOENT;
  2091. }
  2092. if (np) {
  2093. /*
  2094. * These properties are only required for platform device-based
  2095. * controller with DT firmware.
  2096. */
  2097. hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
  2098. "hisilicon,sas-syscon");
  2099. if (IS_ERR(hisi_hba->ctrl)) {
  2100. dev_err(dev, "could not get syscon\n");
  2101. return -ENOENT;
  2102. }
  2103. if (device_property_read_u32(dev, "ctrl-reset-reg",
  2104. &hisi_hba->ctrl_reset_reg)) {
  2105. dev_err(dev, "could not get property ctrl-reset-reg\n");
  2106. return -ENOENT;
  2107. }
  2108. if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
  2109. &hisi_hba->ctrl_reset_sts_reg)) {
  2110. dev_err(dev, "could not get property ctrl-reset-sts-reg\n");
  2111. return -ENOENT;
  2112. }
  2113. if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
  2114. &hisi_hba->ctrl_clock_ena_reg)) {
  2115. dev_err(dev, "could not get property ctrl-clock-ena-reg\n");
  2116. return -ENOENT;
  2117. }
  2118. }
  2119. refclk = devm_clk_get(dev, NULL);
  2120. if (IS_ERR(refclk))
  2121. dev_dbg(dev, "no ref clk property\n");
  2122. else
  2123. hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
  2124. if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
  2125. dev_err(dev, "could not get property phy-count\n");
  2126. return -ENOENT;
  2127. }
  2128. if (device_property_read_u32(dev, "queue-count",
  2129. &hisi_hba->queue_count)) {
  2130. dev_err(dev, "could not get property queue-count\n");
  2131. return -ENOENT;
  2132. }
  2133. return 0;
  2134. }
  2135. EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info);
  2136. static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
  2137. const struct hisi_sas_hw *hw)
  2138. {
  2139. struct resource *res;
  2140. struct Scsi_Host *shost;
  2141. struct hisi_hba *hisi_hba;
  2142. struct device *dev = &pdev->dev;
  2143. int error;
  2144. shost = scsi_host_alloc(hw->sht, sizeof(*hisi_hba));
  2145. if (!shost) {
  2146. dev_err(dev, "scsi host alloc failed\n");
  2147. return NULL;
  2148. }
  2149. hisi_hba = shost_priv(shost);
  2150. INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
  2151. hisi_hba->hw = hw;
  2152. hisi_hba->dev = dev;
  2153. hisi_hba->platform_dev = pdev;
  2154. hisi_hba->shost = shost;
  2155. SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
  2156. timer_setup(&hisi_hba->timer, NULL, 0);
  2157. if (hisi_sas_get_fw_info(hisi_hba) < 0)
  2158. goto err_out;
  2159. error = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
  2160. if (error)
  2161. error = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  2162. if (error) {
  2163. dev_err(dev, "No usable DMA addressing method\n");
  2164. goto err_out;
  2165. }
  2166. hisi_hba->regs = devm_platform_ioremap_resource(pdev, 0);
  2167. if (IS_ERR(hisi_hba->regs))
  2168. goto err_out;
  2169. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  2170. if (res) {
  2171. hisi_hba->sgpio_regs = devm_ioremap_resource(dev, res);
  2172. if (IS_ERR(hisi_hba->sgpio_regs))
  2173. goto err_out;
  2174. }
  2175. if (hisi_sas_alloc(hisi_hba)) {
  2176. hisi_sas_free(hisi_hba);
  2177. goto err_out;
  2178. }
  2179. return shost;
  2180. err_out:
  2181. scsi_host_put(shost);
  2182. dev_err(dev, "shost alloc failed\n");
  2183. return NULL;
  2184. }
  2185. int hisi_sas_probe(struct platform_device *pdev,
  2186. const struct hisi_sas_hw *hw)
  2187. {
  2188. struct Scsi_Host *shost;
  2189. struct hisi_hba *hisi_hba;
  2190. struct device *dev = &pdev->dev;
  2191. struct asd_sas_phy **arr_phy;
  2192. struct asd_sas_port **arr_port;
  2193. struct sas_ha_struct *sha;
  2194. int rc, phy_nr, port_nr, i;
  2195. shost = hisi_sas_shost_alloc(pdev, hw);
  2196. if (!shost)
  2197. return -ENOMEM;
  2198. sha = SHOST_TO_SAS_HA(shost);
  2199. hisi_hba = shost_priv(shost);
  2200. platform_set_drvdata(pdev, sha);
  2201. phy_nr = port_nr = hisi_hba->n_phy;
  2202. arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
  2203. arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
  2204. if (!arr_phy || !arr_port) {
  2205. rc = -ENOMEM;
  2206. goto err_out_ha;
  2207. }
  2208. sha->sas_phy = arr_phy;
  2209. sha->sas_port = arr_port;
  2210. sha->lldd_ha = hisi_hba;
  2211. shost->transportt = hisi_sas_stt;
  2212. shost->max_id = HISI_SAS_MAX_DEVICES;
  2213. shost->max_lun = ~0;
  2214. shost->max_channel = 1;
  2215. shost->max_cmd_len = 16;
  2216. if (hisi_hba->hw->slot_index_alloc) {
  2217. shost->can_queue = HISI_SAS_MAX_COMMANDS;
  2218. shost->cmd_per_lun = HISI_SAS_MAX_COMMANDS;
  2219. } else {
  2220. shost->can_queue = HISI_SAS_UNRESERVED_IPTT;
  2221. shost->cmd_per_lun = HISI_SAS_UNRESERVED_IPTT;
  2222. }
  2223. sha->sas_ha_name = DRV_NAME;
  2224. sha->dev = hisi_hba->dev;
  2225. sha->lldd_module = THIS_MODULE;
  2226. sha->sas_addr = &hisi_hba->sas_addr[0];
  2227. sha->num_phys = hisi_hba->n_phy;
  2228. sha->core.shost = hisi_hba->shost;
  2229. for (i = 0; i < hisi_hba->n_phy; i++) {
  2230. sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
  2231. sha->sas_port[i] = &hisi_hba->port[i].sas_port;
  2232. }
  2233. rc = scsi_add_host(shost, &pdev->dev);
  2234. if (rc)
  2235. goto err_out_ha;
  2236. rc = sas_register_ha(sha);
  2237. if (rc)
  2238. goto err_out_register_ha;
  2239. rc = hisi_hba->hw->hw_init(hisi_hba);
  2240. if (rc)
  2241. goto err_out_register_ha;
  2242. scsi_scan_host(shost);
  2243. return 0;
  2244. err_out_register_ha:
  2245. scsi_remove_host(shost);
  2246. err_out_ha:
  2247. hisi_sas_debugfs_exit(hisi_hba);
  2248. hisi_sas_free(hisi_hba);
  2249. scsi_host_put(shost);
  2250. return rc;
  2251. }
  2252. EXPORT_SYMBOL_GPL(hisi_sas_probe);
  2253. struct dentry *hisi_sas_debugfs_dir;
  2254. static void hisi_sas_debugfs_snapshot_cq_reg(struct hisi_hba *hisi_hba)
  2255. {
  2256. int queue_entry_size = hisi_hba->hw->complete_hdr_size;
  2257. int dump_index = hisi_hba->debugfs_dump_index;
  2258. int i;
  2259. for (i = 0; i < hisi_hba->queue_count; i++)
  2260. memcpy(hisi_hba->debugfs_cq[dump_index][i].complete_hdr,
  2261. hisi_hba->complete_hdr[i],
  2262. HISI_SAS_QUEUE_SLOTS * queue_entry_size);
  2263. }
  2264. static void hisi_sas_debugfs_snapshot_dq_reg(struct hisi_hba *hisi_hba)
  2265. {
  2266. int queue_entry_size = sizeof(struct hisi_sas_cmd_hdr);
  2267. int dump_index = hisi_hba->debugfs_dump_index;
  2268. int i;
  2269. for (i = 0; i < hisi_hba->queue_count; i++) {
  2270. struct hisi_sas_cmd_hdr *debugfs_cmd_hdr, *cmd_hdr;
  2271. int j;
  2272. debugfs_cmd_hdr = hisi_hba->debugfs_dq[dump_index][i].hdr;
  2273. cmd_hdr = hisi_hba->cmd_hdr[i];
  2274. for (j = 0; j < HISI_SAS_QUEUE_SLOTS; j++)
  2275. memcpy(&debugfs_cmd_hdr[j], &cmd_hdr[j],
  2276. queue_entry_size);
  2277. }
  2278. }
  2279. static void hisi_sas_debugfs_snapshot_port_reg(struct hisi_hba *hisi_hba)
  2280. {
  2281. int dump_index = hisi_hba->debugfs_dump_index;
  2282. const struct hisi_sas_debugfs_reg *port =
  2283. hisi_hba->hw->debugfs_reg_port;
  2284. int i, phy_cnt;
  2285. u32 offset;
  2286. u32 *databuf;
  2287. for (phy_cnt = 0; phy_cnt < hisi_hba->n_phy; phy_cnt++) {
  2288. databuf = hisi_hba->debugfs_port_reg[dump_index][phy_cnt].data;
  2289. for (i = 0; i < port->count; i++, databuf++) {
  2290. offset = port->base_off + 4 * i;
  2291. *databuf = port->read_port_reg(hisi_hba, phy_cnt,
  2292. offset);
  2293. }
  2294. }
  2295. }
  2296. static void hisi_sas_debugfs_snapshot_global_reg(struct hisi_hba *hisi_hba)
  2297. {
  2298. int dump_index = hisi_hba->debugfs_dump_index;
  2299. u32 *databuf = hisi_hba->debugfs_regs[dump_index][DEBUGFS_GLOBAL].data;
  2300. const struct hisi_sas_hw *hw = hisi_hba->hw;
  2301. const struct hisi_sas_debugfs_reg *global =
  2302. hw->debugfs_reg_array[DEBUGFS_GLOBAL];
  2303. int i;
  2304. for (i = 0; i < global->count; i++, databuf++)
  2305. *databuf = global->read_global_reg(hisi_hba, 4 * i);
  2306. }
  2307. static void hisi_sas_debugfs_snapshot_axi_reg(struct hisi_hba *hisi_hba)
  2308. {
  2309. int dump_index = hisi_hba->debugfs_dump_index;
  2310. u32 *databuf = hisi_hba->debugfs_regs[dump_index][DEBUGFS_AXI].data;
  2311. const struct hisi_sas_hw *hw = hisi_hba->hw;
  2312. const struct hisi_sas_debugfs_reg *axi =
  2313. hw->debugfs_reg_array[DEBUGFS_AXI];
  2314. int i;
  2315. for (i = 0; i < axi->count; i++, databuf++)
  2316. *databuf = axi->read_global_reg(hisi_hba,
  2317. 4 * i + axi->base_off);
  2318. }
  2319. static void hisi_sas_debugfs_snapshot_ras_reg(struct hisi_hba *hisi_hba)
  2320. {
  2321. int dump_index = hisi_hba->debugfs_dump_index;
  2322. u32 *databuf = hisi_hba->debugfs_regs[dump_index][DEBUGFS_RAS].data;
  2323. const struct hisi_sas_hw *hw = hisi_hba->hw;
  2324. const struct hisi_sas_debugfs_reg *ras =
  2325. hw->debugfs_reg_array[DEBUGFS_RAS];
  2326. int i;
  2327. for (i = 0; i < ras->count; i++, databuf++)
  2328. *databuf = ras->read_global_reg(hisi_hba,
  2329. 4 * i + ras->base_off);
  2330. }
  2331. static void hisi_sas_debugfs_snapshot_itct_reg(struct hisi_hba *hisi_hba)
  2332. {
  2333. int dump_index = hisi_hba->debugfs_dump_index;
  2334. void *cachebuf = hisi_hba->debugfs_itct_cache[dump_index].cache;
  2335. void *databuf = hisi_hba->debugfs_itct[dump_index].itct;
  2336. struct hisi_sas_itct *itct;
  2337. int i;
  2338. hisi_hba->hw->read_iost_itct_cache(hisi_hba, HISI_SAS_ITCT_CACHE,
  2339. cachebuf);
  2340. itct = hisi_hba->itct;
  2341. for (i = 0; i < HISI_SAS_MAX_ITCT_ENTRIES; i++, itct++) {
  2342. memcpy(databuf, itct, sizeof(struct hisi_sas_itct));
  2343. databuf += sizeof(struct hisi_sas_itct);
  2344. }
  2345. }
  2346. static void hisi_sas_debugfs_snapshot_iost_reg(struct hisi_hba *hisi_hba)
  2347. {
  2348. int dump_index = hisi_hba->debugfs_dump_index;
  2349. int max_command_entries = HISI_SAS_MAX_COMMANDS;
  2350. void *cachebuf = hisi_hba->debugfs_iost_cache[dump_index].cache;
  2351. void *databuf = hisi_hba->debugfs_iost[dump_index].iost;
  2352. struct hisi_sas_iost *iost;
  2353. int i;
  2354. hisi_hba->hw->read_iost_itct_cache(hisi_hba, HISI_SAS_IOST_CACHE,
  2355. cachebuf);
  2356. iost = hisi_hba->iost;
  2357. for (i = 0; i < max_command_entries; i++, iost++) {
  2358. memcpy(databuf, iost, sizeof(struct hisi_sas_iost));
  2359. databuf += sizeof(struct hisi_sas_iost);
  2360. }
  2361. }
  2362. static const char *
  2363. hisi_sas_debugfs_to_reg_name(int off, int base_off,
  2364. const struct hisi_sas_debugfs_reg_lu *lu)
  2365. {
  2366. for (; lu->name; lu++) {
  2367. if (off == lu->off - base_off)
  2368. return lu->name;
  2369. }
  2370. return NULL;
  2371. }
  2372. static void hisi_sas_debugfs_print_reg(u32 *regs_val, const void *ptr,
  2373. struct seq_file *s)
  2374. {
  2375. const struct hisi_sas_debugfs_reg *reg = ptr;
  2376. int i;
  2377. for (i = 0; i < reg->count; i++) {
  2378. int off = i * 4;
  2379. const char *name;
  2380. name = hisi_sas_debugfs_to_reg_name(off, reg->base_off,
  2381. reg->lu);
  2382. if (name)
  2383. seq_printf(s, "0x%08x 0x%08x %s\n", off,
  2384. regs_val[i], name);
  2385. else
  2386. seq_printf(s, "0x%08x 0x%08x\n", off,
  2387. regs_val[i]);
  2388. }
  2389. }
  2390. static int hisi_sas_debugfs_global_show(struct seq_file *s, void *p)
  2391. {
  2392. struct hisi_sas_debugfs_regs *global = s->private;
  2393. struct hisi_hba *hisi_hba = global->hisi_hba;
  2394. const struct hisi_sas_hw *hw = hisi_hba->hw;
  2395. const void *reg_global = hw->debugfs_reg_array[DEBUGFS_GLOBAL];
  2396. hisi_sas_debugfs_print_reg(global->data,
  2397. reg_global, s);
  2398. return 0;
  2399. }
  2400. static int hisi_sas_debugfs_global_open(struct inode *inode, struct file *filp)
  2401. {
  2402. return single_open(filp, hisi_sas_debugfs_global_show,
  2403. inode->i_private);
  2404. }
  2405. static const struct file_operations hisi_sas_debugfs_global_fops = {
  2406. .open = hisi_sas_debugfs_global_open,
  2407. .read = seq_read,
  2408. .llseek = seq_lseek,
  2409. .release = single_release,
  2410. .owner = THIS_MODULE,
  2411. };
  2412. static int hisi_sas_debugfs_axi_show(struct seq_file *s, void *p)
  2413. {
  2414. struct hisi_sas_debugfs_regs *axi = s->private;
  2415. struct hisi_hba *hisi_hba = axi->hisi_hba;
  2416. const struct hisi_sas_hw *hw = hisi_hba->hw;
  2417. const void *reg_axi = hw->debugfs_reg_array[DEBUGFS_AXI];
  2418. hisi_sas_debugfs_print_reg(axi->data,
  2419. reg_axi, s);
  2420. return 0;
  2421. }
  2422. static int hisi_sas_debugfs_axi_open(struct inode *inode, struct file *filp)
  2423. {
  2424. return single_open(filp, hisi_sas_debugfs_axi_show,
  2425. inode->i_private);
  2426. }
  2427. static const struct file_operations hisi_sas_debugfs_axi_fops = {
  2428. .open = hisi_sas_debugfs_axi_open,
  2429. .read = seq_read,
  2430. .llseek = seq_lseek,
  2431. .release = single_release,
  2432. .owner = THIS_MODULE,
  2433. };
  2434. static int hisi_sas_debugfs_ras_show(struct seq_file *s, void *p)
  2435. {
  2436. struct hisi_sas_debugfs_regs *ras = s->private;
  2437. struct hisi_hba *hisi_hba = ras->hisi_hba;
  2438. const struct hisi_sas_hw *hw = hisi_hba->hw;
  2439. const void *reg_ras = hw->debugfs_reg_array[DEBUGFS_RAS];
  2440. hisi_sas_debugfs_print_reg(ras->data,
  2441. reg_ras, s);
  2442. return 0;
  2443. }
  2444. static int hisi_sas_debugfs_ras_open(struct inode *inode, struct file *filp)
  2445. {
  2446. return single_open(filp, hisi_sas_debugfs_ras_show,
  2447. inode->i_private);
  2448. }
  2449. static const struct file_operations hisi_sas_debugfs_ras_fops = {
  2450. .open = hisi_sas_debugfs_ras_open,
  2451. .read = seq_read,
  2452. .llseek = seq_lseek,
  2453. .release = single_release,
  2454. .owner = THIS_MODULE,
  2455. };
  2456. static int hisi_sas_debugfs_port_show(struct seq_file *s, void *p)
  2457. {
  2458. struct hisi_sas_debugfs_port *port = s->private;
  2459. struct hisi_sas_phy *phy = port->phy;
  2460. struct hisi_hba *hisi_hba = phy->hisi_hba;
  2461. const struct hisi_sas_hw *hw = hisi_hba->hw;
  2462. const struct hisi_sas_debugfs_reg *reg_port = hw->debugfs_reg_port;
  2463. hisi_sas_debugfs_print_reg(port->data, reg_port, s);
  2464. return 0;
  2465. }
  2466. static int hisi_sas_debugfs_port_open(struct inode *inode, struct file *filp)
  2467. {
  2468. return single_open(filp, hisi_sas_debugfs_port_show, inode->i_private);
  2469. }
  2470. static const struct file_operations hisi_sas_debugfs_port_fops = {
  2471. .open = hisi_sas_debugfs_port_open,
  2472. .read = seq_read,
  2473. .llseek = seq_lseek,
  2474. .release = single_release,
  2475. .owner = THIS_MODULE,
  2476. };
  2477. static void hisi_sas_show_row_64(struct seq_file *s, int index,
  2478. int sz, __le64 *ptr)
  2479. {
  2480. int i;
  2481. /* completion header size not fixed per HW version */
  2482. seq_printf(s, "index %04d:\n\t", index);
  2483. for (i = 1; i <= sz / 8; i++, ptr++) {
  2484. seq_printf(s, " 0x%016llx", le64_to_cpu(*ptr));
  2485. if (!(i % 2))
  2486. seq_puts(s, "\n\t");
  2487. }
  2488. seq_puts(s, "\n");
  2489. }
  2490. static void hisi_sas_show_row_32(struct seq_file *s, int index,
  2491. int sz, __le32 *ptr)
  2492. {
  2493. int i;
  2494. /* completion header size not fixed per HW version */
  2495. seq_printf(s, "index %04d:\n\t", index);
  2496. for (i = 1; i <= sz / 4; i++, ptr++) {
  2497. seq_printf(s, " 0x%08x", le32_to_cpu(*ptr));
  2498. if (!(i % 4))
  2499. seq_puts(s, "\n\t");
  2500. }
  2501. seq_puts(s, "\n");
  2502. }
  2503. static void hisi_sas_cq_show_slot(struct seq_file *s, int slot,
  2504. struct hisi_sas_debugfs_cq *debugfs_cq)
  2505. {
  2506. struct hisi_sas_cq *cq = debugfs_cq->cq;
  2507. struct hisi_hba *hisi_hba = cq->hisi_hba;
  2508. __le32 *complete_hdr = debugfs_cq->complete_hdr +
  2509. (hisi_hba->hw->complete_hdr_size * slot);
  2510. hisi_sas_show_row_32(s, slot,
  2511. hisi_hba->hw->complete_hdr_size,
  2512. complete_hdr);
  2513. }
  2514. static int hisi_sas_debugfs_cq_show(struct seq_file *s, void *p)
  2515. {
  2516. struct hisi_sas_debugfs_cq *debugfs_cq = s->private;
  2517. int slot;
  2518. for (slot = 0; slot < HISI_SAS_QUEUE_SLOTS; slot++) {
  2519. hisi_sas_cq_show_slot(s, slot, debugfs_cq);
  2520. }
  2521. return 0;
  2522. }
  2523. static int hisi_sas_debugfs_cq_open(struct inode *inode, struct file *filp)
  2524. {
  2525. return single_open(filp, hisi_sas_debugfs_cq_show, inode->i_private);
  2526. }
  2527. static const struct file_operations hisi_sas_debugfs_cq_fops = {
  2528. .open = hisi_sas_debugfs_cq_open,
  2529. .read = seq_read,
  2530. .llseek = seq_lseek,
  2531. .release = single_release,
  2532. .owner = THIS_MODULE,
  2533. };
  2534. static void hisi_sas_dq_show_slot(struct seq_file *s, int slot, void *dq_ptr)
  2535. {
  2536. struct hisi_sas_debugfs_dq *debugfs_dq = dq_ptr;
  2537. void *cmd_queue = debugfs_dq->hdr;
  2538. __le32 *cmd_hdr = cmd_queue +
  2539. sizeof(struct hisi_sas_cmd_hdr) * slot;
  2540. hisi_sas_show_row_32(s, slot, sizeof(struct hisi_sas_cmd_hdr), cmd_hdr);
  2541. }
  2542. static int hisi_sas_debugfs_dq_show(struct seq_file *s, void *p)
  2543. {
  2544. int slot;
  2545. for (slot = 0; slot < HISI_SAS_QUEUE_SLOTS; slot++) {
  2546. hisi_sas_dq_show_slot(s, slot, s->private);
  2547. }
  2548. return 0;
  2549. }
  2550. static int hisi_sas_debugfs_dq_open(struct inode *inode, struct file *filp)
  2551. {
  2552. return single_open(filp, hisi_sas_debugfs_dq_show, inode->i_private);
  2553. }
  2554. static const struct file_operations hisi_sas_debugfs_dq_fops = {
  2555. .open = hisi_sas_debugfs_dq_open,
  2556. .read = seq_read,
  2557. .llseek = seq_lseek,
  2558. .release = single_release,
  2559. .owner = THIS_MODULE,
  2560. };
  2561. static int hisi_sas_debugfs_iost_show(struct seq_file *s, void *p)
  2562. {
  2563. struct hisi_sas_debugfs_iost *debugfs_iost = s->private;
  2564. struct hisi_sas_iost *iost = debugfs_iost->iost;
  2565. int i, max_command_entries = HISI_SAS_MAX_COMMANDS;
  2566. for (i = 0; i < max_command_entries; i++, iost++) {
  2567. __le64 *data = &iost->qw0;
  2568. hisi_sas_show_row_64(s, i, sizeof(*iost), data);
  2569. }
  2570. return 0;
  2571. }
  2572. static int hisi_sas_debugfs_iost_open(struct inode *inode, struct file *filp)
  2573. {
  2574. return single_open(filp, hisi_sas_debugfs_iost_show, inode->i_private);
  2575. }
  2576. static const struct file_operations hisi_sas_debugfs_iost_fops = {
  2577. .open = hisi_sas_debugfs_iost_open,
  2578. .read = seq_read,
  2579. .llseek = seq_lseek,
  2580. .release = single_release,
  2581. .owner = THIS_MODULE,
  2582. };
  2583. static int hisi_sas_debugfs_iost_cache_show(struct seq_file *s, void *p)
  2584. {
  2585. struct hisi_sas_debugfs_iost_cache *debugfs_iost_cache = s->private;
  2586. struct hisi_sas_iost_itct_cache *iost_cache = debugfs_iost_cache->cache;
  2587. u32 cache_size = HISI_SAS_IOST_ITCT_CACHE_DW_SZ * 4;
  2588. int i, tab_idx;
  2589. __le64 *iost;
  2590. for (i = 0; i < HISI_SAS_IOST_ITCT_CACHE_NUM; i++, iost_cache++) {
  2591. /*
  2592. * Data struct of IOST cache:
  2593. * Data[1]: BIT0~15: Table index
  2594. * Bit16: Valid mask
  2595. * Data[2]~[9]: IOST table
  2596. */
  2597. tab_idx = (iost_cache->data[1] & 0xffff);
  2598. iost = (__le64 *)iost_cache;
  2599. hisi_sas_show_row_64(s, tab_idx, cache_size, iost);
  2600. }
  2601. return 0;
  2602. }
  2603. static int hisi_sas_debugfs_iost_cache_open(struct inode *inode,
  2604. struct file *filp)
  2605. {
  2606. return single_open(filp, hisi_sas_debugfs_iost_cache_show,
  2607. inode->i_private);
  2608. }
  2609. static const struct file_operations hisi_sas_debugfs_iost_cache_fops = {
  2610. .open = hisi_sas_debugfs_iost_cache_open,
  2611. .read = seq_read,
  2612. .llseek = seq_lseek,
  2613. .release = single_release,
  2614. .owner = THIS_MODULE,
  2615. };
  2616. static int hisi_sas_debugfs_itct_show(struct seq_file *s, void *p)
  2617. {
  2618. int i;
  2619. struct hisi_sas_debugfs_itct *debugfs_itct = s->private;
  2620. struct hisi_sas_itct *itct = debugfs_itct->itct;
  2621. for (i = 0; i < HISI_SAS_MAX_ITCT_ENTRIES; i++, itct++) {
  2622. __le64 *data = &itct->qw0;
  2623. hisi_sas_show_row_64(s, i, sizeof(*itct), data);
  2624. }
  2625. return 0;
  2626. }
  2627. static int hisi_sas_debugfs_itct_open(struct inode *inode, struct file *filp)
  2628. {
  2629. return single_open(filp, hisi_sas_debugfs_itct_show, inode->i_private);
  2630. }
  2631. static const struct file_operations hisi_sas_debugfs_itct_fops = {
  2632. .open = hisi_sas_debugfs_itct_open,
  2633. .read = seq_read,
  2634. .llseek = seq_lseek,
  2635. .release = single_release,
  2636. .owner = THIS_MODULE,
  2637. };
  2638. static int hisi_sas_debugfs_itct_cache_show(struct seq_file *s, void *p)
  2639. {
  2640. struct hisi_sas_debugfs_itct_cache *debugfs_itct_cache = s->private;
  2641. struct hisi_sas_iost_itct_cache *itct_cache = debugfs_itct_cache->cache;
  2642. u32 cache_size = HISI_SAS_IOST_ITCT_CACHE_DW_SZ * 4;
  2643. int i, tab_idx;
  2644. __le64 *itct;
  2645. for (i = 0; i < HISI_SAS_IOST_ITCT_CACHE_NUM; i++, itct_cache++) {
  2646. /*
  2647. * Data struct of ITCT cache:
  2648. * Data[1]: BIT0~15: Table index
  2649. * Bit16: Valid mask
  2650. * Data[2]~[9]: ITCT table
  2651. */
  2652. tab_idx = itct_cache->data[1] & 0xffff;
  2653. itct = (__le64 *)itct_cache;
  2654. hisi_sas_show_row_64(s, tab_idx, cache_size, itct);
  2655. }
  2656. return 0;
  2657. }
  2658. static int hisi_sas_debugfs_itct_cache_open(struct inode *inode,
  2659. struct file *filp)
  2660. {
  2661. return single_open(filp, hisi_sas_debugfs_itct_cache_show,
  2662. inode->i_private);
  2663. }
  2664. static const struct file_operations hisi_sas_debugfs_itct_cache_fops = {
  2665. .open = hisi_sas_debugfs_itct_cache_open,
  2666. .read = seq_read,
  2667. .llseek = seq_lseek,
  2668. .release = single_release,
  2669. .owner = THIS_MODULE,
  2670. };
  2671. static void hisi_sas_debugfs_create_files(struct hisi_hba *hisi_hba)
  2672. {
  2673. u64 *debugfs_timestamp;
  2674. int dump_index = hisi_hba->debugfs_dump_index;
  2675. struct dentry *dump_dentry;
  2676. struct dentry *dentry;
  2677. char name[256];
  2678. int p;
  2679. int c;
  2680. int d;
  2681. snprintf(name, 256, "%d", dump_index);
  2682. dump_dentry = debugfs_create_dir(name, hisi_hba->debugfs_dump_dentry);
  2683. debugfs_timestamp = &hisi_hba->debugfs_timestamp[dump_index];
  2684. debugfs_create_u64("timestamp", 0400, dump_dentry,
  2685. debugfs_timestamp);
  2686. debugfs_create_file("global", 0400, dump_dentry,
  2687. &hisi_hba->debugfs_regs[dump_index][DEBUGFS_GLOBAL],
  2688. &hisi_sas_debugfs_global_fops);
  2689. /* Create port dir and files */
  2690. dentry = debugfs_create_dir("port", dump_dentry);
  2691. for (p = 0; p < hisi_hba->n_phy; p++) {
  2692. snprintf(name, 256, "%d", p);
  2693. debugfs_create_file(name, 0400, dentry,
  2694. &hisi_hba->debugfs_port_reg[dump_index][p],
  2695. &hisi_sas_debugfs_port_fops);
  2696. }
  2697. /* Create CQ dir and files */
  2698. dentry = debugfs_create_dir("cq", dump_dentry);
  2699. for (c = 0; c < hisi_hba->queue_count; c++) {
  2700. snprintf(name, 256, "%d", c);
  2701. debugfs_create_file(name, 0400, dentry,
  2702. &hisi_hba->debugfs_cq[dump_index][c],
  2703. &hisi_sas_debugfs_cq_fops);
  2704. }
  2705. /* Create DQ dir and files */
  2706. dentry = debugfs_create_dir("dq", dump_dentry);
  2707. for (d = 0; d < hisi_hba->queue_count; d++) {
  2708. snprintf(name, 256, "%d", d);
  2709. debugfs_create_file(name, 0400, dentry,
  2710. &hisi_hba->debugfs_dq[dump_index][d],
  2711. &hisi_sas_debugfs_dq_fops);
  2712. }
  2713. debugfs_create_file("iost", 0400, dump_dentry,
  2714. &hisi_hba->debugfs_iost[dump_index],
  2715. &hisi_sas_debugfs_iost_fops);
  2716. debugfs_create_file("iost_cache", 0400, dump_dentry,
  2717. &hisi_hba->debugfs_iost_cache[dump_index],
  2718. &hisi_sas_debugfs_iost_cache_fops);
  2719. debugfs_create_file("itct", 0400, dump_dentry,
  2720. &hisi_hba->debugfs_itct[dump_index],
  2721. &hisi_sas_debugfs_itct_fops);
  2722. debugfs_create_file("itct_cache", 0400, dump_dentry,
  2723. &hisi_hba->debugfs_itct_cache[dump_index],
  2724. &hisi_sas_debugfs_itct_cache_fops);
  2725. debugfs_create_file("axi", 0400, dump_dentry,
  2726. &hisi_hba->debugfs_regs[dump_index][DEBUGFS_AXI],
  2727. &hisi_sas_debugfs_axi_fops);
  2728. debugfs_create_file("ras", 0400, dump_dentry,
  2729. &hisi_hba->debugfs_regs[dump_index][DEBUGFS_RAS],
  2730. &hisi_sas_debugfs_ras_fops);
  2731. return;
  2732. }
  2733. static void hisi_sas_debugfs_snapshot_regs(struct hisi_hba *hisi_hba)
  2734. {
  2735. hisi_hba->hw->snapshot_prepare(hisi_hba);
  2736. hisi_sas_debugfs_snapshot_global_reg(hisi_hba);
  2737. hisi_sas_debugfs_snapshot_port_reg(hisi_hba);
  2738. hisi_sas_debugfs_snapshot_axi_reg(hisi_hba);
  2739. hisi_sas_debugfs_snapshot_ras_reg(hisi_hba);
  2740. hisi_sas_debugfs_snapshot_cq_reg(hisi_hba);
  2741. hisi_sas_debugfs_snapshot_dq_reg(hisi_hba);
  2742. hisi_sas_debugfs_snapshot_itct_reg(hisi_hba);
  2743. hisi_sas_debugfs_snapshot_iost_reg(hisi_hba);
  2744. hisi_sas_debugfs_create_files(hisi_hba);
  2745. hisi_hba->hw->snapshot_restore(hisi_hba);
  2746. }
  2747. static ssize_t hisi_sas_debugfs_trigger_dump_write(struct file *file,
  2748. const char __user *user_buf,
  2749. size_t count, loff_t *ppos)
  2750. {
  2751. struct hisi_hba *hisi_hba = file->f_inode->i_private;
  2752. char buf[8];
  2753. if (hisi_hba->debugfs_dump_index >= hisi_sas_debugfs_dump_count)
  2754. return -EFAULT;
  2755. if (count > 8)
  2756. return -EFAULT;
  2757. if (copy_from_user(buf, user_buf, count))
  2758. return -EFAULT;
  2759. if (buf[0] != '1')
  2760. return -EFAULT;
  2761. queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
  2762. return count;
  2763. }
  2764. static const struct file_operations hisi_sas_debugfs_trigger_dump_fops = {
  2765. .write = &hisi_sas_debugfs_trigger_dump_write,
  2766. .owner = THIS_MODULE,
  2767. };
  2768. enum {
  2769. HISI_SAS_BIST_LOOPBACK_MODE_DIGITAL = 0,
  2770. HISI_SAS_BIST_LOOPBACK_MODE_SERDES,
  2771. HISI_SAS_BIST_LOOPBACK_MODE_REMOTE,
  2772. };
  2773. static const struct {
  2774. int value;
  2775. char *name;
  2776. } hisi_sas_debugfs_loop_linkrate[] = {
  2777. { SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" },
  2778. { SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" },
  2779. { SAS_LINK_RATE_6_0_GBPS, "6.0 Gbit" },
  2780. { SAS_LINK_RATE_12_0_GBPS, "12.0 Gbit" },
  2781. };
  2782. static int hisi_sas_debugfs_bist_linkrate_show(struct seq_file *s, void *p)
  2783. {
  2784. struct hisi_hba *hisi_hba = s->private;
  2785. int i;
  2786. for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_linkrate); i++) {
  2787. int match = (hisi_hba->debugfs_bist_linkrate ==
  2788. hisi_sas_debugfs_loop_linkrate[i].value);
  2789. seq_printf(s, "%s%s%s ", match ? "[" : "",
  2790. hisi_sas_debugfs_loop_linkrate[i].name,
  2791. match ? "]" : "");
  2792. }
  2793. seq_puts(s, "\n");
  2794. return 0;
  2795. }
  2796. static ssize_t hisi_sas_debugfs_bist_linkrate_write(struct file *filp,
  2797. const char __user *buf,
  2798. size_t count, loff_t *ppos)
  2799. {
  2800. struct seq_file *m = filp->private_data;
  2801. struct hisi_hba *hisi_hba = m->private;
  2802. char kbuf[16] = {}, *pkbuf;
  2803. bool found = false;
  2804. int i;
  2805. if (hisi_hba->debugfs_bist_enable)
  2806. return -EPERM;
  2807. if (count >= sizeof(kbuf))
  2808. return -EOVERFLOW;
  2809. if (copy_from_user(kbuf, buf, count))
  2810. return -EINVAL;
  2811. pkbuf = strstrip(kbuf);
  2812. for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_linkrate); i++) {
  2813. if (!strncmp(hisi_sas_debugfs_loop_linkrate[i].name,
  2814. pkbuf, 16)) {
  2815. hisi_hba->debugfs_bist_linkrate =
  2816. hisi_sas_debugfs_loop_linkrate[i].value;
  2817. found = true;
  2818. break;
  2819. }
  2820. }
  2821. if (!found)
  2822. return -EINVAL;
  2823. return count;
  2824. }
  2825. static int hisi_sas_debugfs_bist_linkrate_open(struct inode *inode,
  2826. struct file *filp)
  2827. {
  2828. return single_open(filp, hisi_sas_debugfs_bist_linkrate_show,
  2829. inode->i_private);
  2830. }
  2831. static const struct file_operations hisi_sas_debugfs_bist_linkrate_ops = {
  2832. .open = hisi_sas_debugfs_bist_linkrate_open,
  2833. .read = seq_read,
  2834. .write = hisi_sas_debugfs_bist_linkrate_write,
  2835. .llseek = seq_lseek,
  2836. .release = single_release,
  2837. .owner = THIS_MODULE,
  2838. };
  2839. static const struct {
  2840. int value;
  2841. char *name;
  2842. } hisi_sas_debugfs_loop_code_mode[] = {
  2843. { HISI_SAS_BIST_CODE_MODE_PRBS7, "PRBS7" },
  2844. { HISI_SAS_BIST_CODE_MODE_PRBS23, "PRBS23" },
  2845. { HISI_SAS_BIST_CODE_MODE_PRBS31, "PRBS31" },
  2846. { HISI_SAS_BIST_CODE_MODE_JTPAT, "JTPAT" },
  2847. { HISI_SAS_BIST_CODE_MODE_CJTPAT, "CJTPAT" },
  2848. { HISI_SAS_BIST_CODE_MODE_SCRAMBED_0, "SCRAMBED_0" },
  2849. { HISI_SAS_BIST_CODE_MODE_TRAIN, "TRAIN" },
  2850. { HISI_SAS_BIST_CODE_MODE_TRAIN_DONE, "TRAIN_DONE" },
  2851. { HISI_SAS_BIST_CODE_MODE_HFTP, "HFTP" },
  2852. { HISI_SAS_BIST_CODE_MODE_MFTP, "MFTP" },
  2853. { HISI_SAS_BIST_CODE_MODE_LFTP, "LFTP" },
  2854. { HISI_SAS_BIST_CODE_MODE_FIXED_DATA, "FIXED_DATA" },
  2855. };
  2856. static int hisi_sas_debugfs_bist_code_mode_show(struct seq_file *s, void *p)
  2857. {
  2858. struct hisi_hba *hisi_hba = s->private;
  2859. int i;
  2860. for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_code_mode); i++) {
  2861. int match = (hisi_hba->debugfs_bist_code_mode ==
  2862. hisi_sas_debugfs_loop_code_mode[i].value);
  2863. seq_printf(s, "%s%s%s ", match ? "[" : "",
  2864. hisi_sas_debugfs_loop_code_mode[i].name,
  2865. match ? "]" : "");
  2866. }
  2867. seq_puts(s, "\n");
  2868. return 0;
  2869. }
  2870. static ssize_t hisi_sas_debugfs_bist_code_mode_write(struct file *filp,
  2871. const char __user *buf,
  2872. size_t count,
  2873. loff_t *ppos)
  2874. {
  2875. struct seq_file *m = filp->private_data;
  2876. struct hisi_hba *hisi_hba = m->private;
  2877. char kbuf[16] = {}, *pkbuf;
  2878. bool found = false;
  2879. int i;
  2880. if (hisi_hba->debugfs_bist_enable)
  2881. return -EPERM;
  2882. if (count >= sizeof(kbuf))
  2883. return -EINVAL;
  2884. if (copy_from_user(kbuf, buf, count))
  2885. return -EOVERFLOW;
  2886. pkbuf = strstrip(kbuf);
  2887. for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_code_mode); i++) {
  2888. if (!strncmp(hisi_sas_debugfs_loop_code_mode[i].name,
  2889. pkbuf, 16)) {
  2890. hisi_hba->debugfs_bist_code_mode =
  2891. hisi_sas_debugfs_loop_code_mode[i].value;
  2892. found = true;
  2893. break;
  2894. }
  2895. }
  2896. if (!found)
  2897. return -EINVAL;
  2898. return count;
  2899. }
  2900. static int hisi_sas_debugfs_bist_code_mode_open(struct inode *inode,
  2901. struct file *filp)
  2902. {
  2903. return single_open(filp, hisi_sas_debugfs_bist_code_mode_show,
  2904. inode->i_private);
  2905. }
  2906. static const struct file_operations hisi_sas_debugfs_bist_code_mode_ops = {
  2907. .open = hisi_sas_debugfs_bist_code_mode_open,
  2908. .read = seq_read,
  2909. .write = hisi_sas_debugfs_bist_code_mode_write,
  2910. .llseek = seq_lseek,
  2911. .release = single_release,
  2912. .owner = THIS_MODULE,
  2913. };
  2914. static ssize_t hisi_sas_debugfs_bist_phy_write(struct file *filp,
  2915. const char __user *buf,
  2916. size_t count, loff_t *ppos)
  2917. {
  2918. struct seq_file *m = filp->private_data;
  2919. struct hisi_hba *hisi_hba = m->private;
  2920. unsigned int phy_no;
  2921. int val;
  2922. if (hisi_hba->debugfs_bist_enable)
  2923. return -EPERM;
  2924. val = kstrtouint_from_user(buf, count, 0, &phy_no);
  2925. if (val)
  2926. return val;
  2927. if (phy_no >= hisi_hba->n_phy)
  2928. return -EINVAL;
  2929. hisi_hba->debugfs_bist_phy_no = phy_no;
  2930. return count;
  2931. }
  2932. static int hisi_sas_debugfs_bist_phy_show(struct seq_file *s, void *p)
  2933. {
  2934. struct hisi_hba *hisi_hba = s->private;
  2935. seq_printf(s, "%d\n", hisi_hba->debugfs_bist_phy_no);
  2936. return 0;
  2937. }
  2938. static int hisi_sas_debugfs_bist_phy_open(struct inode *inode,
  2939. struct file *filp)
  2940. {
  2941. return single_open(filp, hisi_sas_debugfs_bist_phy_show,
  2942. inode->i_private);
  2943. }
  2944. static const struct file_operations hisi_sas_debugfs_bist_phy_ops = {
  2945. .open = hisi_sas_debugfs_bist_phy_open,
  2946. .read = seq_read,
  2947. .write = hisi_sas_debugfs_bist_phy_write,
  2948. .llseek = seq_lseek,
  2949. .release = single_release,
  2950. .owner = THIS_MODULE,
  2951. };
  2952. static const struct {
  2953. int value;
  2954. char *name;
  2955. } hisi_sas_debugfs_loop_modes[] = {
  2956. { HISI_SAS_BIST_LOOPBACK_MODE_DIGITAL, "digital" },
  2957. { HISI_SAS_BIST_LOOPBACK_MODE_SERDES, "serdes" },
  2958. { HISI_SAS_BIST_LOOPBACK_MODE_REMOTE, "remote" },
  2959. };
  2960. static int hisi_sas_debugfs_bist_mode_show(struct seq_file *s, void *p)
  2961. {
  2962. struct hisi_hba *hisi_hba = s->private;
  2963. int i;
  2964. for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_modes); i++) {
  2965. int match = (hisi_hba->debugfs_bist_mode ==
  2966. hisi_sas_debugfs_loop_modes[i].value);
  2967. seq_printf(s, "%s%s%s ", match ? "[" : "",
  2968. hisi_sas_debugfs_loop_modes[i].name,
  2969. match ? "]" : "");
  2970. }
  2971. seq_puts(s, "\n");
  2972. return 0;
  2973. }
  2974. static ssize_t hisi_sas_debugfs_bist_mode_write(struct file *filp,
  2975. const char __user *buf,
  2976. size_t count, loff_t *ppos)
  2977. {
  2978. struct seq_file *m = filp->private_data;
  2979. struct hisi_hba *hisi_hba = m->private;
  2980. char kbuf[16] = {}, *pkbuf;
  2981. bool found = false;
  2982. int i;
  2983. if (hisi_hba->debugfs_bist_enable)
  2984. return -EPERM;
  2985. if (count >= sizeof(kbuf))
  2986. return -EINVAL;
  2987. if (copy_from_user(kbuf, buf, count))
  2988. return -EOVERFLOW;
  2989. pkbuf = strstrip(kbuf);
  2990. for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_modes); i++) {
  2991. if (!strncmp(hisi_sas_debugfs_loop_modes[i].name, pkbuf, 16)) {
  2992. hisi_hba->debugfs_bist_mode =
  2993. hisi_sas_debugfs_loop_modes[i].value;
  2994. found = true;
  2995. break;
  2996. }
  2997. }
  2998. if (!found)
  2999. return -EINVAL;
  3000. return count;
  3001. }
  3002. static int hisi_sas_debugfs_bist_mode_open(struct inode *inode,
  3003. struct file *filp)
  3004. {
  3005. return single_open(filp, hisi_sas_debugfs_bist_mode_show,
  3006. inode->i_private);
  3007. }
  3008. static const struct file_operations hisi_sas_debugfs_bist_mode_ops = {
  3009. .open = hisi_sas_debugfs_bist_mode_open,
  3010. .read = seq_read,
  3011. .write = hisi_sas_debugfs_bist_mode_write,
  3012. .llseek = seq_lseek,
  3013. .release = single_release,
  3014. .owner = THIS_MODULE,
  3015. };
  3016. static ssize_t hisi_sas_debugfs_bist_enable_write(struct file *filp,
  3017. const char __user *buf,
  3018. size_t count, loff_t *ppos)
  3019. {
  3020. struct seq_file *m = filp->private_data;
  3021. struct hisi_hba *hisi_hba = m->private;
  3022. unsigned int enable;
  3023. int val;
  3024. val = kstrtouint_from_user(buf, count, 0, &enable);
  3025. if (val)
  3026. return val;
  3027. if (enable > 1)
  3028. return -EINVAL;
  3029. if (enable == hisi_hba->debugfs_bist_enable)
  3030. return count;
  3031. if (!hisi_hba->hw->set_bist)
  3032. return -EPERM;
  3033. val = hisi_hba->hw->set_bist(hisi_hba, enable);
  3034. if (val < 0)
  3035. return val;
  3036. hisi_hba->debugfs_bist_enable = enable;
  3037. return count;
  3038. }
  3039. static int hisi_sas_debugfs_bist_enable_show(struct seq_file *s, void *p)
  3040. {
  3041. struct hisi_hba *hisi_hba = s->private;
  3042. seq_printf(s, "%d\n", hisi_hba->debugfs_bist_enable);
  3043. return 0;
  3044. }
  3045. static int hisi_sas_debugfs_bist_enable_open(struct inode *inode,
  3046. struct file *filp)
  3047. {
  3048. return single_open(filp, hisi_sas_debugfs_bist_enable_show,
  3049. inode->i_private);
  3050. }
  3051. static const struct file_operations hisi_sas_debugfs_bist_enable_ops = {
  3052. .open = hisi_sas_debugfs_bist_enable_open,
  3053. .read = seq_read,
  3054. .write = hisi_sas_debugfs_bist_enable_write,
  3055. .llseek = seq_lseek,
  3056. .release = single_release,
  3057. .owner = THIS_MODULE,
  3058. };
  3059. static const struct {
  3060. char *name;
  3061. } hisi_sas_debugfs_ffe_name[FFE_CFG_MAX] = {
  3062. { "SAS_1_5_GBPS" },
  3063. { "SAS_3_0_GBPS" },
  3064. { "SAS_6_0_GBPS" },
  3065. { "SAS_12_0_GBPS" },
  3066. { "FFE_RESV" },
  3067. { "SATA_1_5_GBPS" },
  3068. { "SATA_3_0_GBPS" },
  3069. { "SATA_6_0_GBPS" },
  3070. };
  3071. static ssize_t hisi_sas_debugfs_write(struct file *filp,
  3072. const char __user *buf,
  3073. size_t count, loff_t *ppos)
  3074. {
  3075. struct seq_file *m = filp->private_data;
  3076. u32 *val = m->private;
  3077. int res;
  3078. res = kstrtouint_from_user(buf, count, 0, val);
  3079. if (res)
  3080. return res;
  3081. return count;
  3082. }
  3083. static int hisi_sas_debugfs_show(struct seq_file *s, void *p)
  3084. {
  3085. u32 *val = s->private;
  3086. seq_printf(s, "0x%x\n", *val);
  3087. return 0;
  3088. }
  3089. static int hisi_sas_debugfs_open(struct inode *inode, struct file *filp)
  3090. {
  3091. return single_open(filp, hisi_sas_debugfs_show,
  3092. inode->i_private);
  3093. }
  3094. static const struct file_operations hisi_sas_debugfs_ops = {
  3095. .open = hisi_sas_debugfs_open,
  3096. .read = seq_read,
  3097. .write = hisi_sas_debugfs_write,
  3098. .llseek = seq_lseek,
  3099. .release = single_release,
  3100. .owner = THIS_MODULE,
  3101. };
  3102. static ssize_t hisi_sas_debugfs_phy_down_cnt_write(struct file *filp,
  3103. const char __user *buf,
  3104. size_t count, loff_t *ppos)
  3105. {
  3106. struct seq_file *s = filp->private_data;
  3107. struct hisi_sas_phy *phy = s->private;
  3108. unsigned int set_val;
  3109. int res;
  3110. res = kstrtouint_from_user(buf, count, 0, &set_val);
  3111. if (res)
  3112. return res;
  3113. if (set_val > 0)
  3114. return -EINVAL;
  3115. atomic_set(&phy->down_cnt, 0);
  3116. return count;
  3117. }
  3118. static int hisi_sas_debugfs_phy_down_cnt_show(struct seq_file *s, void *p)
  3119. {
  3120. struct hisi_sas_phy *phy = s->private;
  3121. seq_printf(s, "%d\n", atomic_read(&phy->down_cnt));
  3122. return 0;
  3123. }
  3124. static int hisi_sas_debugfs_phy_down_cnt_open(struct inode *inode,
  3125. struct file *filp)
  3126. {
  3127. return single_open(filp, hisi_sas_debugfs_phy_down_cnt_show,
  3128. inode->i_private);
  3129. }
  3130. static const struct file_operations hisi_sas_debugfs_phy_down_cnt_ops = {
  3131. .open = hisi_sas_debugfs_phy_down_cnt_open,
  3132. .read = seq_read,
  3133. .write = hisi_sas_debugfs_phy_down_cnt_write,
  3134. .llseek = seq_lseek,
  3135. .release = single_release,
  3136. .owner = THIS_MODULE,
  3137. };
  3138. void hisi_sas_debugfs_work_handler(struct work_struct *work)
  3139. {
  3140. struct hisi_hba *hisi_hba =
  3141. container_of(work, struct hisi_hba, debugfs_work);
  3142. int debugfs_dump_index = hisi_hba->debugfs_dump_index;
  3143. struct device *dev = hisi_hba->dev;
  3144. u64 timestamp = local_clock();
  3145. if (debugfs_dump_index >= hisi_sas_debugfs_dump_count) {
  3146. dev_warn(dev, "dump count exceeded!\n");
  3147. return;
  3148. }
  3149. do_div(timestamp, NSEC_PER_MSEC);
  3150. hisi_hba->debugfs_timestamp[debugfs_dump_index] = timestamp;
  3151. hisi_sas_debugfs_snapshot_regs(hisi_hba);
  3152. hisi_hba->debugfs_dump_index++;
  3153. }
  3154. EXPORT_SYMBOL_GPL(hisi_sas_debugfs_work_handler);
  3155. static void hisi_sas_debugfs_release(struct hisi_hba *hisi_hba, int dump_index)
  3156. {
  3157. struct device *dev = hisi_hba->dev;
  3158. int i;
  3159. devm_kfree(dev, hisi_hba->debugfs_iost_cache[dump_index].cache);
  3160. devm_kfree(dev, hisi_hba->debugfs_itct_cache[dump_index].cache);
  3161. devm_kfree(dev, hisi_hba->debugfs_iost[dump_index].iost);
  3162. devm_kfree(dev, hisi_hba->debugfs_itct[dump_index].itct);
  3163. for (i = 0; i < hisi_hba->queue_count; i++)
  3164. devm_kfree(dev, hisi_hba->debugfs_dq[dump_index][i].hdr);
  3165. for (i = 0; i < hisi_hba->queue_count; i++)
  3166. devm_kfree(dev,
  3167. hisi_hba->debugfs_cq[dump_index][i].complete_hdr);
  3168. for (i = 0; i < DEBUGFS_REGS_NUM; i++)
  3169. devm_kfree(dev, hisi_hba->debugfs_regs[dump_index][i].data);
  3170. for (i = 0; i < hisi_hba->n_phy; i++)
  3171. devm_kfree(dev, hisi_hba->debugfs_port_reg[dump_index][i].data);
  3172. }
  3173. static int hisi_sas_debugfs_alloc(struct hisi_hba *hisi_hba, int dump_index)
  3174. {
  3175. const struct hisi_sas_hw *hw = hisi_hba->hw;
  3176. struct device *dev = hisi_hba->dev;
  3177. int p, c, d, r, i;
  3178. size_t sz;
  3179. for (r = 0; r < DEBUGFS_REGS_NUM; r++) {
  3180. struct hisi_sas_debugfs_regs *regs =
  3181. &hisi_hba->debugfs_regs[dump_index][r];
  3182. sz = hw->debugfs_reg_array[r]->count * 4;
  3183. regs->data = devm_kmalloc(dev, sz, GFP_KERNEL);
  3184. if (!regs->data)
  3185. goto fail;
  3186. regs->hisi_hba = hisi_hba;
  3187. }
  3188. sz = hw->debugfs_reg_port->count * 4;
  3189. for (p = 0; p < hisi_hba->n_phy; p++) {
  3190. struct hisi_sas_debugfs_port *port =
  3191. &hisi_hba->debugfs_port_reg[dump_index][p];
  3192. port->data = devm_kmalloc(dev, sz, GFP_KERNEL);
  3193. if (!port->data)
  3194. goto fail;
  3195. port->phy = &hisi_hba->phy[p];
  3196. }
  3197. sz = hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
  3198. for (c = 0; c < hisi_hba->queue_count; c++) {
  3199. struct hisi_sas_debugfs_cq *cq =
  3200. &hisi_hba->debugfs_cq[dump_index][c];
  3201. cq->complete_hdr = devm_kmalloc(dev, sz, GFP_KERNEL);
  3202. if (!cq->complete_hdr)
  3203. goto fail;
  3204. cq->cq = &hisi_hba->cq[c];
  3205. }
  3206. sz = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
  3207. for (d = 0; d < hisi_hba->queue_count; d++) {
  3208. struct hisi_sas_debugfs_dq *dq =
  3209. &hisi_hba->debugfs_dq[dump_index][d];
  3210. dq->hdr = devm_kmalloc(dev, sz, GFP_KERNEL);
  3211. if (!dq->hdr)
  3212. goto fail;
  3213. dq->dq = &hisi_hba->dq[d];
  3214. }
  3215. sz = HISI_SAS_MAX_COMMANDS * sizeof(struct hisi_sas_iost);
  3216. hisi_hba->debugfs_iost[dump_index].iost =
  3217. devm_kmalloc(dev, sz, GFP_KERNEL);
  3218. if (!hisi_hba->debugfs_iost[dump_index].iost)
  3219. goto fail;
  3220. sz = HISI_SAS_IOST_ITCT_CACHE_NUM *
  3221. sizeof(struct hisi_sas_iost_itct_cache);
  3222. hisi_hba->debugfs_iost_cache[dump_index].cache =
  3223. devm_kmalloc(dev, sz, GFP_KERNEL);
  3224. if (!hisi_hba->debugfs_iost_cache[dump_index].cache)
  3225. goto fail;
  3226. sz = HISI_SAS_IOST_ITCT_CACHE_NUM *
  3227. sizeof(struct hisi_sas_iost_itct_cache);
  3228. hisi_hba->debugfs_itct_cache[dump_index].cache =
  3229. devm_kmalloc(dev, sz, GFP_KERNEL);
  3230. if (!hisi_hba->debugfs_itct_cache[dump_index].cache)
  3231. goto fail;
  3232. /* New memory allocation must be locate before itct */
  3233. sz = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
  3234. hisi_hba->debugfs_itct[dump_index].itct =
  3235. devm_kmalloc(dev, sz, GFP_KERNEL);
  3236. if (!hisi_hba->debugfs_itct[dump_index].itct)
  3237. goto fail;
  3238. return 0;
  3239. fail:
  3240. for (i = 0; i < hisi_sas_debugfs_dump_count; i++)
  3241. hisi_sas_debugfs_release(hisi_hba, i);
  3242. return -ENOMEM;
  3243. }
  3244. static void hisi_sas_debugfs_phy_down_cnt_init(struct hisi_hba *hisi_hba)
  3245. {
  3246. struct dentry *dir = debugfs_create_dir("phy_down_cnt",
  3247. hisi_hba->debugfs_dir);
  3248. char name[16];
  3249. int phy_no;
  3250. for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
  3251. snprintf(name, 16, "%d", phy_no);
  3252. debugfs_create_file(name, 0600, dir,
  3253. &hisi_hba->phy[phy_no],
  3254. &hisi_sas_debugfs_phy_down_cnt_ops);
  3255. }
  3256. }
  3257. static void hisi_sas_debugfs_bist_init(struct hisi_hba *hisi_hba)
  3258. {
  3259. struct dentry *ports_dentry;
  3260. int phy_no;
  3261. hisi_hba->debugfs_bist_dentry =
  3262. debugfs_create_dir("bist", hisi_hba->debugfs_dir);
  3263. debugfs_create_file("link_rate", 0600,
  3264. hisi_hba->debugfs_bist_dentry, hisi_hba,
  3265. &hisi_sas_debugfs_bist_linkrate_ops);
  3266. debugfs_create_file("code_mode", 0600,
  3267. hisi_hba->debugfs_bist_dentry, hisi_hba,
  3268. &hisi_sas_debugfs_bist_code_mode_ops);
  3269. debugfs_create_file("fixed_code", 0600,
  3270. hisi_hba->debugfs_bist_dentry,
  3271. &hisi_hba->debugfs_bist_fixed_code[0],
  3272. &hisi_sas_debugfs_ops);
  3273. debugfs_create_file("fixed_code_1", 0600,
  3274. hisi_hba->debugfs_bist_dentry,
  3275. &hisi_hba->debugfs_bist_fixed_code[1],
  3276. &hisi_sas_debugfs_ops);
  3277. debugfs_create_file("phy_id", 0600, hisi_hba->debugfs_bist_dentry,
  3278. hisi_hba, &hisi_sas_debugfs_bist_phy_ops);
  3279. debugfs_create_u32("cnt", 0600, hisi_hba->debugfs_bist_dentry,
  3280. &hisi_hba->debugfs_bist_cnt);
  3281. debugfs_create_file("loopback_mode", 0600,
  3282. hisi_hba->debugfs_bist_dentry,
  3283. hisi_hba, &hisi_sas_debugfs_bist_mode_ops);
  3284. debugfs_create_file("enable", 0600, hisi_hba->debugfs_bist_dentry,
  3285. hisi_hba, &hisi_sas_debugfs_bist_enable_ops);
  3286. ports_dentry = debugfs_create_dir("port", hisi_hba->debugfs_bist_dentry);
  3287. for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
  3288. struct dentry *port_dentry;
  3289. struct dentry *ffe_dentry;
  3290. char name[256];
  3291. int i;
  3292. snprintf(name, 256, "%d", phy_no);
  3293. port_dentry = debugfs_create_dir(name, ports_dentry);
  3294. ffe_dentry = debugfs_create_dir("ffe", port_dentry);
  3295. for (i = 0; i < FFE_CFG_MAX; i++) {
  3296. if (i == FFE_RESV)
  3297. continue;
  3298. debugfs_create_file(hisi_sas_debugfs_ffe_name[i].name,
  3299. 0600, ffe_dentry,
  3300. &hisi_hba->debugfs_bist_ffe[phy_no][i],
  3301. &hisi_sas_debugfs_ops);
  3302. }
  3303. }
  3304. hisi_hba->debugfs_bist_linkrate = SAS_LINK_RATE_1_5_GBPS;
  3305. }
  3306. void hisi_sas_debugfs_init(struct hisi_hba *hisi_hba)
  3307. {
  3308. struct device *dev = hisi_hba->dev;
  3309. int i;
  3310. hisi_hba->debugfs_dir = debugfs_create_dir(dev_name(dev),
  3311. hisi_sas_debugfs_dir);
  3312. debugfs_create_file("trigger_dump", 0200,
  3313. hisi_hba->debugfs_dir,
  3314. hisi_hba,
  3315. &hisi_sas_debugfs_trigger_dump_fops);
  3316. /* create bist structures */
  3317. hisi_sas_debugfs_bist_init(hisi_hba);
  3318. hisi_hba->debugfs_dump_dentry =
  3319. debugfs_create_dir("dump", hisi_hba->debugfs_dir);
  3320. hisi_sas_debugfs_phy_down_cnt_init(hisi_hba);
  3321. for (i = 0; i < hisi_sas_debugfs_dump_count; i++) {
  3322. if (hisi_sas_debugfs_alloc(hisi_hba, i)) {
  3323. debugfs_remove_recursive(hisi_hba->debugfs_dir);
  3324. dev_dbg(dev, "failed to init debugfs!\n");
  3325. break;
  3326. }
  3327. }
  3328. }
  3329. EXPORT_SYMBOL_GPL(hisi_sas_debugfs_init);
  3330. void hisi_sas_debugfs_exit(struct hisi_hba *hisi_hba)
  3331. {
  3332. debugfs_remove_recursive(hisi_hba->debugfs_dir);
  3333. }
  3334. EXPORT_SYMBOL_GPL(hisi_sas_debugfs_exit);
  3335. int hisi_sas_remove(struct platform_device *pdev)
  3336. {
  3337. struct sas_ha_struct *sha = platform_get_drvdata(pdev);
  3338. struct hisi_hba *hisi_hba = sha->lldd_ha;
  3339. struct Scsi_Host *shost = sha->core.shost;
  3340. if (timer_pending(&hisi_hba->timer))
  3341. del_timer(&hisi_hba->timer);
  3342. sas_unregister_ha(sha);
  3343. sas_remove_host(sha->core.shost);
  3344. hisi_sas_free(hisi_hba);
  3345. scsi_host_put(shost);
  3346. return 0;
  3347. }
  3348. EXPORT_SYMBOL_GPL(hisi_sas_remove);
  3349. bool hisi_sas_debugfs_enable;
  3350. EXPORT_SYMBOL_GPL(hisi_sas_debugfs_enable);
  3351. module_param_named(debugfs_enable, hisi_sas_debugfs_enable, bool, 0444);
  3352. MODULE_PARM_DESC(hisi_sas_debugfs_enable, "Enable driver debugfs (default disabled)");
  3353. u32 hisi_sas_debugfs_dump_count = 1;
  3354. EXPORT_SYMBOL_GPL(hisi_sas_debugfs_dump_count);
  3355. module_param_named(debugfs_dump_count, hisi_sas_debugfs_dump_count, uint, 0444);
  3356. MODULE_PARM_DESC(hisi_sas_debugfs_dump_count, "Number of debugfs dumps to allow");
  3357. static __init int hisi_sas_init(void)
  3358. {
  3359. hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
  3360. if (!hisi_sas_stt)
  3361. return -ENOMEM;
  3362. if (hisi_sas_debugfs_enable) {
  3363. hisi_sas_debugfs_dir = debugfs_create_dir("hisi_sas", NULL);
  3364. if (hisi_sas_debugfs_dump_count > HISI_SAS_MAX_DEBUGFS_DUMP) {
  3365. pr_info("hisi_sas: Limiting debugfs dump count\n");
  3366. hisi_sas_debugfs_dump_count = HISI_SAS_MAX_DEBUGFS_DUMP;
  3367. }
  3368. }
  3369. return 0;
  3370. }
  3371. static __exit void hisi_sas_exit(void)
  3372. {
  3373. sas_release_transport(hisi_sas_stt);
  3374. debugfs_remove(hisi_sas_debugfs_dir);
  3375. }
  3376. module_init(hisi_sas_init);
  3377. module_exit(hisi_sas_exit);
  3378. MODULE_LICENSE("GPL");
  3379. MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
  3380. MODULE_DESCRIPTION("HISILICON SAS controller driver");
  3381. MODULE_ALIAS("platform:" DRV_NAME);